summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2026-06-15 21:07:34 +0800
committerruki <[email protected]>2026-06-15 21:07:34 +0800
commit4242e1847c1522f13eb1f3c8d8f60d82b508e2f6 (patch)
tree016da445f35915d6a461c7dddfcfbaa092b71d2a
parent151db560b3960f677f942357eae8ad3a574e79db (diff)
fix clean rpath for elfelf
-rw-r--r--core/src/xmake/binutils/elf/rpath.c30
-rw-r--r--core/src/xmake/binutils/rpath.c22
2 files changed, 34 insertions, 18 deletions
diff --git a/core/src/xmake/binutils/elf/rpath.c b/core/src/xmake/binutils/elf/rpath.c
index b238ad42f..539c8a018 100644
--- a/core/src/xmake/binutils/elf/rpath.c
+++ b/core/src/xmake/binutils/elf/rpath.c
@@ -204,17 +204,33 @@ tb_bool_t xm_binutils_elf_rpath_clean(tb_stream_ref_t istream, tb_hize_t base_of
if (!tb_stream_seek(istream, base_offset)) break;
if (!tb_stream_bread(istream, ident, sizeof(ident))) break;
- // build ELF context and cleanup rpath entries
+ /* build ELF context to locate the .dynamic section
+ *
+ * @note we only need dynamic_offset/dynamic_size here (strtab is not used
+ * when removing entries by tag), so we do not require get_context to fully succeed.
+ */
xm_elf_context_t ctx;
+ tb_memset(&ctx, 0, sizeof(ctx));
if (ident[XM_ELF_EI_CLASS] == XM_ELF_CLASS32) {
- if (xm_binutils_elf_get_context_32(istream, base_offset, &ctx)) {
- if (xm_binutils_elf_rpath_clean_impl(istream, base_offset, &ctx)) ok = tb_true;
- }
+ xm_binutils_elf_get_context_32(istream, base_offset, &ctx);
} else if (ident[XM_ELF_EI_CLASS] == XM_ELF_CLASS64) {
- if (xm_binutils_elf_get_context_64(istream, base_offset, &ctx)) {
- if (xm_binutils_elf_rpath_clean_impl(istream, base_offset, &ctx)) ok = tb_true;
- }
+ xm_binutils_elf_get_context_64(istream, base_offset, &ctx);
+ } else {
+ break;
}
+
+ /* no .dynamic section? e.g. a fully static executable (-static),
+ * there is no rpath/runpath to clean, so just treat it as a no-op success.
+ *
+ * @see https://github.com/xmake-io/xmake/issues/7595
+ */
+ if (ctx.dynamic_offset == 0 || ctx.dynamic_size == 0) {
+ ok = tb_true;
+ break;
+ }
+
+ // remove DT_RPATH/DT_RUNPATH entries
+ ok = xm_binutils_elf_rpath_clean_impl(istream, base_offset, &ctx);
} while (0);
return ok;
}
diff --git a/core/src/xmake/binutils/rpath.c b/core/src/xmake/binutils/rpath.c
index c720141c2..b3b7f5c3a 100644
--- a/core/src/xmake/binutils/rpath.c
+++ b/core/src/xmake/binutils/rpath.c
@@ -61,7 +61,7 @@ tb_int_t xm_binutils_rpath_list(lua_State *lua) {
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);
+ lua_pushfstring(lua, "open %s failed", binaryfile);
return 2;
}
@@ -69,7 +69,7 @@ tb_int_t xm_binutils_rpath_list(lua_State *lua) {
do {
if (!tb_stream_open(istream)) {
lua_pushboolean(lua, tb_false);
- lua_pushfstring(lua, "rpath_list: open %s failed", binaryfile);
+ lua_pushfstring(lua, "open %s failed", binaryfile);
break;
}
@@ -77,7 +77,7 @@ tb_int_t xm_binutils_rpath_list(lua_State *lua) {
tb_int_t format = xm_binutils_format_detect(istream);
if (format < 0) {
lua_pushboolean(lua, tb_false);
- lua_pushfstring(lua, "rpath_list: cannot detect file format");
+ lua_pushfstring(lua, "cannot detect file format");
break;
}
@@ -89,14 +89,14 @@ tb_int_t xm_binutils_rpath_list(lua_State *lua) {
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");
+ lua_pushfstring(lua, "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");
+ lua_pushfstring(lua, "failed to parse Mach-O");
break;
}
} else {
@@ -129,7 +129,7 @@ tb_int_t xm_binutils_rpath_clean(lua_State *lua) {
tb_stream_ref_t istream = tb_stream_init_from_file(binaryfile, TB_FILE_MODE_RW);
if (!istream) {
lua_pushboolean(lua, tb_false);
- lua_pushfstring(lua, "rpath_clean: open %s failed", binaryfile);
+ lua_pushfstring(lua, "open %s failed", binaryfile);
return 2;
}
@@ -137,7 +137,7 @@ tb_int_t xm_binutils_rpath_clean(lua_State *lua) {
do {
if (!tb_stream_open(istream)) {
lua_pushboolean(lua, tb_false);
- lua_pushfstring(lua, "rpath_clean: open %s failed", binaryfile);
+ lua_pushfstring(lua, "open %s failed", binaryfile);
break;
}
@@ -145,7 +145,7 @@ tb_int_t xm_binutils_rpath_clean(lua_State *lua) {
tb_int_t format = xm_binutils_format_detect(istream);
if (format < 0) {
lua_pushboolean(lua, tb_false);
- lua_pushfstring(lua, "rpath_clean: cannot detect file format");
+ lua_pushfstring(lua, "cannot detect file format");
break;
}
@@ -153,18 +153,18 @@ tb_int_t xm_binutils_rpath_clean(lua_State *lua) {
if (format == XM_BINUTILS_FORMAT_ELF) {
if (!xm_binutils_elf_rpath_clean(istream, 0)) {
lua_pushboolean(lua, tb_false);
- lua_pushfstring(lua, "rpath_clean: failed to clean ELF");
+ lua_pushfstring(lua, "failed to clean ELF");
break;
}
} else if (format == XM_BINUTILS_FORMAT_MACHO) {
if (!xm_binutils_macho_rpath_clean(istream, 0)) {
lua_pushboolean(lua, tb_false);
- lua_pushfstring(lua, "rpath_clean: failed to clean Mach-O");
+ lua_pushfstring(lua, "failed to clean Mach-O");
break;
}
} else {
lua_pushboolean(lua, tb_false);
- lua_pushfstring(lua, "rpath_clean: format not supported");
+ lua_pushfstring(lua, "format not supported");
break;
}