summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2025-12-19 22:55:22 +0800
committerruki <[email protected]>2025-12-19 22:55:22 +0800
commit2c497bc57fc27af645afbf3de2e4e6068ea853dc (patch)
treedef81c9e79c644381502f0bfe7c0ee7547536241
parentbbe585b9b8927d71f79f8c723110224e31348381 (diff)
add rpath list in binutils
-rw-r--r--core/src/xmake/binutils/coff/prefix.h2
-rw-r--r--core/src/xmake/binutils/elf/rpath.c343
-rw-r--r--core/src/xmake/binutils/macho/deplibs.c2
-rw-r--r--core/src/xmake/binutils/macho/prefix.h39
-rw-r--r--core/src/xmake/binutils/macho/rpath.c146
-rw-r--r--core/src/xmake/binutils/prefix.h2
-rw-r--r--core/src/xmake/binutils/rpath.c111
-rw-r--r--core/src/xmake/engine.c2
-rw-r--r--xmake/core/base/binutils.lua10
-rw-r--r--xmake/core/sandbox/modules/import/core/base/binutils.lua10
-rw-r--r--xmake/modules/utils/binary/rpath.lua8
11 files changed, 660 insertions, 15 deletions
diff --git a/core/src/xmake/binutils/coff/prefix.h b/core/src/xmake/binutils/coff/prefix.h
index 8017beeff..8b882f0f6 100644
--- a/core/src/xmake/binutils/coff/prefix.h
+++ b/core/src/xmake/binutils/coff/prefix.h
@@ -34,7 +34,7 @@
#define XM_COFF_MACHINE_ARM 0x01c0
#define XM_COFF_MACHINE_ARM64 0xaa64
-/* PE Optional Header Magic */
+// PE Optional Header Magic
#define XM_PE32_MAGIC 0x10b
#define XM_PE32P_MAGIC 0x20b
diff --git a/core/src/xmake/binutils/elf/rpath.c b/core/src/xmake/binutils/elf/rpath.c
new file mode 100644
index 000000000..2267ca09f
--- /dev/null
+++ b/core/src/xmake/binutils/elf/rpath.c
@@ -0,0 +1,343 @@
+/*!A cross-platform build utility based on Lua
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Copyright (C) 2015-present, Xmake Open Source Community.
+ *
+ * @author ruki
+ * @file rpath.c
+ *
+ */
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * trace
+ */
+#define TB_TRACE_MODULE_NAME "rpath_elf"
+#define TB_TRACE_MODULE_DEBUG (0)
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * includes
+ */
+#include "prefix.h"
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * private implementation
+ */
+static tb_void_t xm_binutils_elf_add_rpaths(lua_State *lua, tb_char_t const* rpath, tb_size_t* pcount) {
+ if (!rpath || !*rpath) return;
+
+ tb_char_t const* p = rpath;
+ tb_char_t const* e = tb_null;
+ while (*p) {
+ e = tb_strchr(p, ':');
+ tb_size_t n = e ? (tb_size_t)(e - p) : tb_strlen(p);
+ if (n > 0) {
+ tb_char_t path[TB_PATH_MAXN];
+ tb_strncpy(path, p, n);
+ path[n] = '\0';
+
+ lua_pushinteger(lua, *pcount + 1);
+ lua_pushstring(lua, path);
+ lua_settable(lua, -3);
+ (*pcount)++;
+ }
+ if (e) p = e + 1;
+ else break;
+ }
+}
+
+static tb_bool_t xm_binutils_elf_rpath_list_32(tb_stream_ref_t istream, tb_hize_t base_offset, lua_State *lua) {
+ tb_assert_and_check_return_val(istream && lua, tb_false);
+
+ // read ELF header
+ xm_elf32_header_t header;
+ if (!tb_stream_seek(istream, base_offset)) {
+ return tb_false;
+ }
+ if (!tb_stream_bread(istream, (tb_byte_t*)&header, sizeof(header))) {
+ return tb_false;
+ }
+
+ // find .dynamic section info
+ tb_uint32_t dynamic_offset = 0;
+ tb_uint32_t dynamic_size = 0;
+ tb_uint32_t strtab_offset = 0;
+
+ // try to find from section headers first
+ if (header.e_shoff != 0 && header.e_shnum > 0) {
+ if (tb_stream_seek(istream, base_offset + header.e_shoff)) {
+ for (tb_uint16_t i = 0; i < header.e_shnum; i++) {
+ xm_elf32_section_t section;
+ if (!tb_stream_bread(istream, (tb_byte_t*)&section, sizeof(section))) {
+ break;
+ }
+
+ if (section.sh_type == XM_ELF_SHT_DYNAMIC) {
+ dynamic_offset = section.sh_offset;
+ dynamic_size = section.sh_size;
+
+ // find string table via sh_link
+ xm_elf32_section_t strtab_section;
+ if (tb_stream_seek(istream, base_offset + header.e_shoff + section.sh_link * sizeof(xm_elf32_section_t)) &&
+ tb_stream_bread(istream, (tb_byte_t*)&strtab_section, sizeof(strtab_section))) {
+ strtab_offset = strtab_section.sh_offset;
+ }
+ break;
+ }
+ }
+ }
+ }
+
+ // fallback to program headers if not found in sections
+ if ((dynamic_offset == 0 || strtab_offset == 0) && header.e_phoff != 0 && header.e_phnum > 0) {
+ if (tb_stream_seek(istream, base_offset + header.e_phoff)) {
+ for (tb_uint16_t i = 0; i < header.e_phnum; i++) {
+ xm_elf32_phdr_t phdr;
+ if (!tb_stream_bread(istream, (tb_byte_t*)&phdr, sizeof(phdr))) {
+ break;
+ }
+ if (phdr.p_type == XM_ELF_PT_DYNAMIC) {
+ dynamic_offset = phdr.p_offset;
+ dynamic_size = phdr.p_memsz; // usually p_filesz == p_memsz for dynamic
+ break;
+ }
+ }
+ }
+
+ if (dynamic_offset > 0 && dynamic_size > 0) {
+ // read dynamic entries to find strtab address
+ tb_uint32_t strtab_vaddr = 0;
+ tb_uint32_t count = dynamic_size / sizeof(xm_elf32_dynamic_t);
+ if (tb_stream_seek(istream, base_offset + dynamic_offset)) {
+ for (tb_uint32_t i = 0; i < count; i++) {
+ xm_elf32_dynamic_t dyn;
+ if (!tb_stream_bread(istream, (tb_byte_t*)&dyn, sizeof(dyn))) {
+ break;
+ }
+ if (dyn.d_tag == XM_ELF_DT_STRTAB) {
+ strtab_vaddr = dyn.d_un.d_val;
+ break;
+ }
+ }
+ }
+
+ if (strtab_vaddr > 0) {
+ // map strtab vaddr to file offset using PT_LOAD
+ if (tb_stream_seek(istream, base_offset + header.e_phoff)) {
+ for (tb_uint16_t i = 0; i < header.e_phnum; i++) {
+ xm_elf32_phdr_t phdr;
+ if (!tb_stream_bread(istream, (tb_byte_t*)&phdr, sizeof(phdr))) {
+ break;
+ }
+ if (phdr.p_type == XM_ELF_PT_LOAD && strtab_vaddr >= phdr.p_vaddr && strtab_vaddr < phdr.p_vaddr + phdr.p_memsz) {
+ strtab_offset = phdr.p_offset + (strtab_vaddr - phdr.p_vaddr);
+ break;
+ }
+ }
+ }
+ }
+ }
+ }
+
+ if (dynamic_offset == 0 || strtab_offset == 0) {
+ return tb_true; // no dynamic section or strtab found
+ }
+
+ // read dynamic entries
+ tb_uint32_t count = dynamic_size / sizeof(xm_elf32_dynamic_t);
+ if (!tb_stream_seek(istream, base_offset + dynamic_offset)) {
+ return tb_false;
+ }
+
+ tb_char_t rpath[8192] = {0};
+ tb_char_t runpath[8192] = {0};
+ for (tb_uint32_t i = 0; i < count; i++) {
+ xm_elf32_dynamic_t dyn;
+ if (!tb_stream_bread(istream, (tb_byte_t*)&dyn, sizeof(dyn))) {
+ break;
+ }
+
+ if (dyn.d_tag == XM_ELF_DT_NULL) {
+ break;
+ }
+
+ if (dyn.d_tag == XM_ELF_DT_RPATH) {
+ xm_binutils_read_string(istream, base_offset + strtab_offset + dyn.d_un.d_val, rpath, sizeof(rpath));
+ } else if (dyn.d_tag == XM_ELF_DT_RUNPATH) {
+ xm_binutils_read_string(istream, base_offset + strtab_offset + dyn.d_un.d_val, runpath, sizeof(runpath));
+ }
+ }
+
+ tb_size_t result_count = 0;
+ if (runpath[0]) {
+ xm_binutils_elf_add_rpaths(lua, runpath, &result_count);
+ } else if (rpath[0]) {
+ xm_binutils_elf_add_rpaths(lua, rpath, &result_count);
+ }
+
+ return tb_true;
+}
+
+static tb_bool_t xm_binutils_elf_rpath_list_64(tb_stream_ref_t istream, tb_hize_t base_offset, lua_State *lua) {
+ tb_assert_and_check_return_val(istream && lua, tb_false);
+
+ // read ELF header
+ xm_elf64_header_t header;
+ if (!tb_stream_seek(istream, base_offset)) {
+ return tb_false;
+ }
+ if (!tb_stream_bread(istream, (tb_byte_t*)&header, sizeof(header))) {
+ return tb_false;
+ }
+
+ // find .dynamic section info
+ tb_uint64_t dynamic_offset = 0;
+ tb_uint64_t dynamic_size = 0;
+ tb_uint64_t strtab_offset = 0;
+
+ // try to find from section headers first
+ if (header.e_shoff != 0 && header.e_shnum > 0) {
+ if (tb_stream_seek(istream, base_offset + header.e_shoff)) {
+ for (tb_uint16_t i = 0; i < header.e_shnum; i++) {
+ xm_elf64_section_t section;
+ if (!tb_stream_bread(istream, (tb_byte_t*)&section, sizeof(section))) {
+ break;
+ }
+
+ if (section.sh_type == XM_ELF_SHT_DYNAMIC) {
+ dynamic_offset = section.sh_offset;
+ dynamic_size = section.sh_size;
+
+ // find string table via sh_link
+ xm_elf64_section_t strtab_section;
+ if (tb_stream_seek(istream, base_offset + header.e_shoff + section.sh_link * sizeof(xm_elf64_section_t)) &&
+ tb_stream_bread(istream, (tb_byte_t*)&strtab_section, sizeof(strtab_section))) {
+ strtab_offset = strtab_section.sh_offset;
+ }
+ break;
+ }
+ }
+ }
+ }
+
+ // fallback to program headers if not found in sections
+ if ((dynamic_offset == 0 || strtab_offset == 0) && header.e_phoff != 0 && header.e_phnum > 0) {
+ if (tb_stream_seek(istream, base_offset + header.e_phoff)) {
+ for (tb_uint16_t i = 0; i < header.e_phnum; i++) {
+ xm_elf64_phdr_t phdr;
+ if (!tb_stream_bread(istream, (tb_byte_t*)&phdr, sizeof(phdr))) {
+ break;
+ }
+ if (phdr.p_type == XM_ELF_PT_DYNAMIC) {
+ dynamic_offset = phdr.p_offset;
+ dynamic_size = phdr.p_memsz;
+ break;
+ }
+ }
+ }
+
+ if (dynamic_offset > 0 && dynamic_size > 0) {
+ // read dynamic entries to find strtab address
+ tb_uint64_t strtab_vaddr = 0;
+ tb_uint32_t count = dynamic_size / sizeof(xm_elf64_dynamic_t);
+ if (tb_stream_seek(istream, base_offset + dynamic_offset)) {
+ for (tb_uint32_t i = 0; i < count; i++) {
+ xm_elf64_dynamic_t dyn;
+ if (!tb_stream_bread(istream, (tb_byte_t*)&dyn, sizeof(dyn))) {
+ break;
+ }
+ if (dyn.d_tag == XM_ELF_DT_STRTAB) {
+ strtab_vaddr = dyn.d_un.d_val;
+ break;
+ }
+ }
+ }
+
+ if (strtab_vaddr > 0) {
+ // map strtab vaddr to file offset using PT_LOAD
+ if (tb_stream_seek(istream, base_offset + header.e_phoff)) {
+ for (tb_uint16_t i = 0; i < header.e_phnum; i++) {
+ xm_elf64_phdr_t phdr;
+ if (!tb_stream_bread(istream, (tb_byte_t*)&phdr, sizeof(phdr))) {
+ break;
+ }
+ if (phdr.p_type == XM_ELF_PT_LOAD && strtab_vaddr >= phdr.p_vaddr && strtab_vaddr < phdr.p_vaddr + phdr.p_memsz) {
+ strtab_offset = phdr.p_offset + (strtab_vaddr - phdr.p_vaddr);
+ break;
+ }
+ }
+ }
+ }
+ }
+ }
+
+ if (dynamic_offset == 0 || strtab_offset == 0) {
+ return tb_true; // no dynamic section or strtab found
+ }
+
+ // read dynamic entries
+ tb_uint32_t count = dynamic_size / sizeof(xm_elf64_dynamic_t);
+ if (!tb_stream_seek(istream, base_offset + dynamic_offset)) {
+ return tb_false;
+ }
+
+ tb_char_t rpath[8192] = {0};
+ tb_char_t runpath[8192] = {0};
+ for (tb_uint32_t i = 0; i < count; i++) {
+ xm_elf64_dynamic_t dyn;
+ if (!tb_stream_bread(istream, (tb_byte_t*)&dyn, sizeof(dyn))) {
+ break;
+ }
+
+ if (dyn.d_tag == XM_ELF_DT_NULL) {
+ break;
+ }
+
+ if (dyn.d_tag == XM_ELF_DT_RPATH) {
+ xm_binutils_read_string(istream, base_offset + strtab_offset + dyn.d_un.d_val, rpath, sizeof(rpath));
+ } else if (dyn.d_tag == XM_ELF_DT_RUNPATH) {
+ xm_binutils_read_string(istream, base_offset + strtab_offset + dyn.d_un.d_val, runpath, sizeof(runpath));
+ }
+ }
+
+ tb_size_t result_count = 0;
+ if (runpath[0]) {
+ xm_binutils_elf_add_rpaths(lua, runpath, &result_count);
+ } else if (rpath[0]) {
+ xm_binutils_elf_add_rpaths(lua, rpath, &result_count);
+ }
+
+ return tb_true;
+}
+
+tb_bool_t xm_binutils_elf_rpath_list(tb_stream_ref_t istream, tb_hize_t base_offset, lua_State *lua) {
+ tb_assert_and_check_return_val(istream && lua, tb_false);
+
+ // read ident
+ tb_byte_t ident[16];
+ if (!tb_stream_seek(istream, base_offset)) {
+ return tb_false;
+ }
+ if (!tb_stream_bread(istream, ident, sizeof(ident))) {
+ return tb_false;
+ }
+
+ // check class
+ if (ident[4] == 1) {
+ return xm_binutils_elf_rpath_list_32(istream, base_offset, lua);
+ } else if (ident[4] == 2) {
+ return xm_binutils_elf_rpath_list_64(istream, base_offset, lua);
+ }
+
+ return tb_false;
+}
diff --git a/core/src/xmake/binutils/macho/deplibs.c b/core/src/xmake/binutils/macho/deplibs.c
index 40d19b5cb..286a40d20 100644
--- a/core/src/xmake/binutils/macho/deplibs.c
+++ b/core/src/xmake/binutils/macho/deplibs.c
@@ -104,7 +104,7 @@ tb_bool_t xm_binutils_macho_deplibs(tb_stream_ref_t istream, tb_hize_t base_offs
}
xm_binutils_macho_swap_load_command(&lc, swap);
- /* check for LC_LOAD_DYLIB, LC_LOAD_WEAK_DYLIB, LC_REEXPORT_DYLIB, LC_ID_DYLIB */
+ // check for LC_LOAD_DYLIB, LC_LOAD_WEAK_DYLIB, LC_REEXPORT_DYLIB, LC_ID_DYLIB
if (lc.cmd == XM_MACHO_LC_LOAD_DYLIB || lc.cmd == XM_MACHO_LC_ID_DYLIB ||
lc.cmd == XM_MACHO_LC_LOAD_WEAK_DYLIB || lc.cmd == XM_MACHO_LC_REEXPORT_DYLIB) {
diff --git a/core/src/xmake/binutils/macho/prefix.h b/core/src/xmake/binutils/macho/prefix.h
index 4216bbdd1..612033029 100644
--- a/core/src/xmake/binutils/macho/prefix.h
+++ b/core/src/xmake/binutils/macho/prefix.h
@@ -29,10 +29,10 @@
/* //////////////////////////////////////////////////////////////////////////////////////
* macros
*/
-#define XM_MACHO_MAGIC_32 0xfeedface /* MH_MAGIC - little endian */
-#define XM_MACHO_MAGIC_64 0xfeedfacf /* MH_MAGIC_64 - little endian */
-#define XM_MACHO_MAGIC_32_BE 0xcefaedfe /* MH_CIGAM - big endian */
-#define XM_MACHO_MAGIC_64_BE 0xcffaedfe /* MH_CIGAM_64 - big endian */
+#define XM_MACHO_MAGIC_32 0xfeedface // MH_MAGIC - little endian
+#define XM_MACHO_MAGIC_64 0xfeedfacf // MH_MAGIC_64 - little endian
+#define XM_MACHO_MAGIC_32_BE 0xcefaedfe // MH_CIGAM - big endian
+#define XM_MACHO_MAGIC_64_BE 0xcffaedfe // MH_CIGAM_64 - big endian
#define XM_MACHO_MAGIC_FAT 0xcafebabe
#define XM_MACHO_CPU_TYPE_X86 7
@@ -52,6 +52,7 @@
#define XM_MACHO_LC_SYMTAB 0x2
#define XM_MACHO_LC_LOAD_DYLIB 0xc
#define XM_MACHO_LC_ID_DYLIB 0xd
+#define XM_MACHO_LC_RPATH (0x1c | 0x80000000)
#define XM_MACHO_LC_LOAD_WEAK_DYLIB (0x18 | 0x80000000)
#define XM_MACHO_LC_REEXPORT_DYLIB (0x1f | 0x80000000)
#define XM_MACHO_LC_BUILD_VERSION 0x32
@@ -98,6 +99,13 @@ typedef struct __xm_macho_header_64_t {
tb_uint32_t reserved;
} __tb_packed__ xm_macho_header_64_t;
+typedef struct __xm_macho_rpath_command_t {
+ tb_uint32_t cmd;
+ tb_uint32_t cmdsize;
+ tb_uint32_t path_offset;
+} __tb_packed__ xm_macho_rpath_command_t;
+#include "tbox/prefix/packed.h"
+
typedef struct __xm_macho_segment_command_t {
tb_uint32_t cmd;
tb_uint32_t cmdsize;
@@ -213,7 +221,7 @@ typedef struct __xm_macho_dylib_command_t {
* inline implementation
*/
-/* byte-swap Mach-O header fields if needed */
+// byte-swap Mach-O header fields if needed
static __tb_inline__ tb_void_t xm_binutils_macho_swap_header_32(xm_macho_header_t *header, tb_bool_t swap) {
if (swap) {
header->magic = tb_bits_swap_u32(header->magic);
@@ -226,7 +234,7 @@ static __tb_inline__ tb_void_t xm_binutils_macho_swap_header_32(xm_macho_header_
}
}
-/* byte-swap Mach-O header 64 fields if needed */
+// byte-swap Mach-O header 64 fields if needed
static __tb_inline__ tb_void_t xm_binutils_macho_swap_header_64(xm_macho_header_64_t *header, tb_bool_t swap) {
if (swap) {
header->magic = tb_bits_swap_u32(header->magic);
@@ -240,7 +248,7 @@ static __tb_inline__ tb_void_t xm_binutils_macho_swap_header_64(xm_macho_header_
}
}
-/* byte-swap load command fields if needed */
+// byte-swap load command fields if needed
static __tb_inline__ tb_void_t xm_binutils_macho_swap_load_command(xm_macho_load_command_t *lc, tb_bool_t swap) {
if (swap) {
lc->cmd = tb_bits_swap_u32(lc->cmd);
@@ -248,7 +256,7 @@ static __tb_inline__ tb_void_t xm_binutils_macho_swap_load_command(xm_macho_load
}
}
-/* byte-swap dylib command fields if needed */
+// byte-swap dylib command fields if needed
static __tb_inline__ tb_void_t xm_binutils_macho_swap_dylib_command(xm_macho_dylib_command_t *dc, tb_bool_t swap) {
if (swap) {
dc->cmd = tb_bits_swap_u32(dc->cmd);
@@ -260,7 +268,16 @@ static __tb_inline__ tb_void_t xm_binutils_macho_swap_dylib_command(xm_macho_dyl
}
}
-/* byte-swap symtab command fields if needed */
+// byte-swap rpath command fields if needed
+static __tb_inline__ tb_void_t xm_binutils_macho_swap_rpath_command(xm_macho_rpath_command_t *rc, tb_bool_t swap) {
+ if (swap) {
+ rc->cmd = tb_bits_swap_u32(rc->cmd);
+ rc->cmdsize = tb_bits_swap_u32(rc->cmdsize);
+ rc->path_offset = tb_bits_swap_u32(rc->path_offset);
+ }
+}
+
+// byte-swap symtab command fields if needed
static __tb_inline__ tb_void_t xm_binutils_macho_swap_symtab_command(xm_macho_symtab_command_t *cmd, tb_bool_t swap) {
if (swap) {
cmd->cmd = tb_bits_swap_u32(cmd->cmd);
@@ -272,7 +289,7 @@ static __tb_inline__ tb_void_t xm_binutils_macho_swap_symtab_command(xm_macho_sy
}
}
-/* byte-swap nlist 32 fields if needed */
+// byte-swap nlist 32 fields if needed
static __tb_inline__ tb_void_t xm_binutils_macho_swap_nlist_32(xm_macho_nlist_t *nlist, tb_bool_t swap) {
if (swap) {
nlist->strx = tb_bits_swap_u32(nlist->strx);
@@ -281,7 +298,7 @@ static __tb_inline__ tb_void_t xm_binutils_macho_swap_nlist_32(xm_macho_nlist_t
}
}
-/* byte-swap nlist 64 fields if needed */
+// byte-swap nlist 64 fields if needed
static __tb_inline__ tb_void_t xm_binutils_macho_swap_nlist_64(xm_macho_nlist_64_t *nlist, tb_bool_t swap) {
if (swap) {
nlist->strx = tb_bits_swap_u32(nlist->strx);
diff --git a/core/src/xmake/binutils/macho/rpath.c b/core/src/xmake/binutils/macho/rpath.c
new file mode 100644
index 000000000..e67c2edfd
--- /dev/null
+++ b/core/src/xmake/binutils/macho/rpath.c
@@ -0,0 +1,146 @@
+/*!A cross-platform build utility based on Lua
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Copyright (C) 2015-present, Xmake Open Source Community.
+ *
+ * @author ruki
+ * @file rpath.c
+ *
+ */
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * trace
+ */
+#define TB_TRACE_MODULE_NAME "rpath_macho"
+#define TB_TRACE_MODULE_DEBUG (0)
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * includes
+ */
+#include "prefix.h"
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * implementation
+ */
+tb_bool_t xm_binutils_macho_rpath_list(tb_stream_ref_t istream, tb_hize_t base_offset, lua_State *lua) {
+ tb_assert_and_check_return_val(istream && lua, tb_false);
+
+ // read Mach-O header
+ xm_macho_header_t header;
+ if (!tb_stream_seek(istream, base_offset)) {
+ return tb_false;
+ }
+ if (!tb_stream_bread(istream, (tb_byte_t*)&header, sizeof(header))) {
+ return tb_false;
+ }
+
+ tb_bool_t swap = tb_false;
+ tb_bool_t is64 = tb_false;
+
+ // check magic
+ if (header.magic == XM_MACHO_MAGIC_32) {
+ is64 = tb_false;
+ } else if (header.magic == XM_MACHO_MAGIC_32_BE) {
+ is64 = tb_false;
+ swap = tb_true;
+ } else if (header.magic == XM_MACHO_MAGIC_64) {
+ is64 = tb_true;
+ } else if (header.magic == XM_MACHO_MAGIC_64_BE) {
+ is64 = tb_true;
+ swap = tb_true;
+ } else {
+ return tb_false; // Not a Mach-O file
+ }
+
+ // re-read header for 64-bit if needed, or just use 32-bit part and skip reserved
+ tb_uint32_t ncmds = 0;
+
+ if (is64) {
+ xm_macho_header_64_t header64;
+ if (!tb_stream_seek(istream, base_offset)) {
+ return tb_false;
+ }
+ if (!tb_stream_bread(istream, (tb_byte_t*)&header64, sizeof(header64))) {
+ return tb_false;
+ }
+ xm_binutils_macho_swap_header_64(&header64, swap);
+ ncmds = header64.ncmds;
+ } else {
+ xm_binutils_macho_swap_header_32(&header, swap);
+ ncmds = header.ncmds;
+ }
+
+ // skip header to reach load commands
+ tb_size_t header_size = is64 ? sizeof(xm_macho_header_64_t) : sizeof(xm_macho_header_t);
+ if (!tb_stream_seek(istream, base_offset + header_size)) {
+ return tb_false;
+ }
+
+ tb_size_t result_count = 0;
+
+ // iterate load commands
+ for (tb_uint32_t i = 0; i < ncmds; i++) {
+ xm_macho_load_command_t lc;
+ tb_hize_t current_cmd_offset = tb_stream_offset(istream);
+
+ if (!tb_stream_bread(istream, (tb_byte_t*)&lc, sizeof(lc))) {
+ return tb_false;
+ }
+ xm_binutils_macho_swap_load_command(&lc, swap);
+
+ // check for LC_RPATH
+ if (lc.cmd == XM_MACHO_LC_RPATH) {
+
+ xm_macho_rpath_command_t rc;
+ if (tb_stream_seek(istream, current_cmd_offset)) {
+ if (tb_stream_bread(istream, (tb_byte_t*)&rc, sizeof(rc))) {
+ xm_binutils_macho_swap_rpath_command(&rc, swap);
+
+ tb_uint32_t name_offset = rc.path_offset;
+ if (name_offset < lc.cmdsize) {
+ // name is at current_cmd_offset + name_offset
+ if (tb_stream_seek(istream, current_cmd_offset + name_offset)) {
+ tb_char_t rpath[TB_PATH_MAXN];
+ tb_size_t max_len = lc.cmdsize - name_offset;
+ if (max_len > sizeof(rpath) - 1) max_len = sizeof(rpath) - 1;
+
+ tb_size_t pos = 0;
+ tb_byte_t c;
+ while (pos < max_len) {
+ if (!tb_stream_bread(istream, &c, 1)) break;
+ if (c == 0) break;
+ rpath[pos++] = (tb_char_t)c;
+ }
+ rpath[pos] = '\0';
+
+ if (pos > 0) {
+ lua_pushinteger(lua, result_count + 1);
+ lua_pushstring(lua, rpath);
+ lua_settable(lua, -3);
+ result_count++;
+ }
+ }
+ }
+ }
+ }
+ }
+
+ // move to next command
+ if (!tb_stream_seek(istream, current_cmd_offset + lc.cmdsize)) {
+ break;
+ }
+ }
+
+ return tb_true;
+}
diff --git a/core/src/xmake/binutils/prefix.h b/core/src/xmake/binutils/prefix.h
index 68408eea5..e7811652a 100644
--- a/core/src/xmake/binutils/prefix.h
+++ b/core/src/xmake/binutils/prefix.h
@@ -36,7 +36,7 @@
#define XM_BINUTILS_FORMAT_PE 5
#define XM_BINUTILS_FORMAT_UNKNOWN 0
-/* COFF machine types (for format detection) */
+// COFF machine types (for format detection)
#define XM_BINUTILS_COFF_MACHINE_I386 0x014c
#define XM_BINUTILS_COFF_MACHINE_AMD64 0x8664
#define XM_BINUTILS_COFF_MACHINE_ARM 0x01c0
diff --git a/core/src/xmake/binutils/rpath.c b/core/src/xmake/binutils/rpath.c
new file mode 100644
index 000000000..50f729309
--- /dev/null
+++ b/core/src/xmake/binutils/rpath.c
@@ -0,0 +1,111 @@
+/*!A cross-platform build utility based on Lua
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Copyright (C) 2015-present, Xmake Open Source Community.
+ *
+ * @author ruki
+ * @file rpath.c
+ *
+ */
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * trace
+ */
+#define TB_TRACE_MODULE_NAME "rpath"
+#define TB_TRACE_MODULE_DEBUG (0)
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * includes
+ */
+#include "prefix.h"
+#include "elf/prefix.h"
+#include "macho/prefix.h"
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * extern
+ */
+tb_bool_t xm_binutils_elf_rpath_list(tb_stream_ref_t istream, tb_hize_t base_offset, lua_State *lua);
+tb_bool_t xm_binutils_macho_rpath_list(tb_stream_ref_t istream, tb_hize_t base_offset, lua_State *lua);
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * implementation
+ */
+
+/* get rpath list from binary file (auto-detect format)
+ *
+ * @param lua the lua state
+ * @return 1 on success (table on stack), 2 on failure (with error message on stack)
+ */
+tb_int_t xm_binutils_rpath_list(lua_State *lua) {
+ tb_assert_and_check_return_val(lua, 0);
+
+ // get the binary file path
+ tb_char_t const *binaryfile = luaL_checkstring(lua, 1);
+ tb_check_return_val(binaryfile, 0);
+
+ // open file
+ tb_stream_ref_t istream = tb_stream_init_from_file(binaryfile, TB_FILE_MODE_RO);
+ if (!istream) {
+ lua_pushboolean(lua, tb_false);
+ lua_pushfstring(lua, "rpath_list: open %s failed", binaryfile);
+ return 2;
+ }
+
+ tb_bool_t ok = tb_false;
+ do {
+ if (!tb_stream_open(istream)) {
+ lua_pushboolean(lua, tb_false);
+ lua_pushfstring(lua, "rpath_list: open %s failed", binaryfile);
+ break;
+ }
+
+ // detect format
+ tb_int_t format = xm_binutils_detect_format(istream);
+ if (format < 0) {
+ lua_pushboolean(lua, tb_false);
+ lua_pushfstring(lua, "rpath_list: cannot detect file format");
+ break;
+ }
+
+ // create result list
+ lua_newtable(lua);
+
+ // get rpath list based on format
+ if (format == XM_BINUTILS_FORMAT_ELF) {
+ if (!xm_binutils_elf_rpath_list(istream, 0, lua)) {
+ lua_pop(lua, 1); // pop table
+ lua_pushboolean(lua, tb_false);
+ lua_pushfstring(lua, "rpath_list: failed to parse ELF");
+ break;
+ }
+ } else if (format == XM_BINUTILS_FORMAT_MACHO) {
+ if (!xm_binutils_macho_rpath_list(istream, 0, lua)) {
+ lua_pop(lua, 1); // pop table
+ lua_pushboolean(lua, tb_false);
+ lua_pushfstring(lua, "rpath_list: failed to parse Mach-O");
+ break;
+ }
+ } else {
+ /* not supported or no rpath for this format
+ * return empty table
+ */
+ }
+
+ ok = tb_true;
+
+ } while (0);
+
+ if (istream) tb_stream_exit(istream);
+ return ok ? 1 : 2;
+}
diff --git a/core/src/xmake/engine.c b/core/src/xmake/engine.c
index ea9dc2af1..069e3af1a 100644
--- a/core/src/xmake/engine.c
+++ b/core/src/xmake/engine.c
@@ -342,6 +342,7 @@ tb_int_t xm_binutils_bin2macho(lua_State *lua);
tb_int_t xm_binutils_bin2elf(lua_State *lua);
tb_int_t xm_binutils_readsyms(lua_State *lua);
tb_int_t xm_binutils_deplibs(lua_State *lua);
+tb_int_t xm_binutils_rpath_list(lua_State *lua);
tb_int_t xm_binutils_extractlib(lua_State *lua);
#ifdef XM_CONFIG_API_HAVE_CURSES
@@ -668,6 +669,7 @@ static luaL_Reg const g_binutils_functions[] = {
{ "bin2elf", xm_binutils_bin2elf },
{ "readsyms", xm_binutils_readsyms },
{ "deplibs", xm_binutils_deplibs },
+ { "rpath_list", xm_binutils_rpath_list },
{ "extractlib", xm_binutils_extractlib },
{ tb_null, tb_null },
};
diff --git a/xmake/core/base/binutils.lua b/xmake/core/base/binutils.lua
index af71e8ae7..5bed5de21 100644
--- a/xmake/core/base/binutils.lua
+++ b/xmake/core/base/binutils.lua
@@ -31,6 +31,7 @@ binutils._bin2macho = binutils._bin2macho or binutils.bin2macho
binutils._bin2elf = binutils._bin2elf or binutils.bin2elf
binutils._readsyms = binutils._readsyms or binutils.readsyms
binutils._deplibs = binutils._deplibs or binutils.deplibs
+binutils._rpath_list = binutils._rpath_list or binutils.rpath_list
binutils._extractlib = binutils._extractlib or binutils.extractlib
-- generate c/c++ code from the binary file
@@ -111,6 +112,15 @@ function binutils.deplibs(binaryfile)
end
end
+-- get rpath list from binary file (auto-detect format: ELF or Mach-O)
+function binutils.rpath_list(binaryfile)
+ if binutils._rpath_list then
+ return binutils._rpath_list(binaryfile)
+ else
+ return nil, "binutils: internal rpath_list not supported!"
+ end
+end
+
-- extract static library to directory
-- Supports AR format (.a) and MSVC lib format (.lib)
-- @param libraryfile the static library file path (.a or .lib)
diff --git a/xmake/core/sandbox/modules/import/core/base/binutils.lua b/xmake/core/sandbox/modules/import/core/base/binutils.lua
index 11f432340..b7b8a1792 100644
--- a/xmake/core/sandbox/modules/import/core/base/binutils.lua
+++ b/xmake/core/sandbox/modules/import/core/base/binutils.lua
@@ -62,6 +62,16 @@ function sandbox_core_base_binutils.deplibs(binaryfile)
end
end
+-- get rpath list from binary file (auto-detect format: ELF or Mach-O)
+function sandbox_core_base_binutils.rpath_list(binaryfile)
+ local rpaths, errors = binutils.rpath_list(binaryfile)
+ if rpaths then
+ return rpaths
+ else
+ raise("rpath_list: %s", errors or "unknown errors")
+ end
+end
+
-- extract static library to directory
function sandbox_core_base_binutils.extractlib(libraryfile, outputdir, opt)
local ok, errors = binutils.extractlib(libraryfile, outputdir, opt)
diff --git a/xmake/modules/utils/binary/rpath.lua b/xmake/modules/utils/binary/rpath.lua
index 271ef8939..6152d4d14 100644
--- a/xmake/modules/utils/binary/rpath.lua
+++ b/xmake/modules/utils/binary/rpath.lua
@@ -20,6 +20,7 @@
-- imports
import("core.base.option")
+import("core.base.binutils")
import("lib.detect.find_tool")
function _replace_rpath_vars(rpath, opt)
@@ -34,6 +35,10 @@ function _replace_rpath_vars(rpath, opt)
return rpath
end
+function _get_rpath_list_by_binutils(binaryfile, opt)
+ return binutils.rpath_list(binaryfile)
+end
+
function _get_rpath_list_by_objdump(binaryfile, opt)
local list
local plat = opt.plat or os.host()
@@ -296,7 +301,8 @@ function list(binaryfile, opt)
local ops = {
_get_rpath_list_by_objdump,
_get_rpath_list_by_readelf,
- _get_rpath_list_by_patchelf
+ _get_rpath_list_by_patchelf,
+ _get_rpath_list_by_binutils
}
if is_host("macosx") then
table.insert(ops, 1, _get_rpath_list_by_otool)