summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2025-12-12 23:48:37 +0800
committerruki <[email protected]>2025-12-12 23:48:37 +0800
commitc5a01a4d8de7e7775ed410486a46278f0817841e (patch)
treecd42a1077572507732915caeae6fe5ca956af14a
parente0637f873e9c43aa5e521d8c8db9969a45ea7e3f (diff)
parse program header in elf
-rw-r--r--core/src/xmake/binutils/elf/deplibs.c54
-rw-r--r--core/src/xmake/binutils/elf/prefix.h27
2 files changed, 77 insertions, 4 deletions
diff --git a/core/src/xmake/binutils/elf/deplibs.c b/core/src/xmake/binutils/elf/deplibs.c
index 85e346192..c79190634 100644
--- a/core/src/xmake/binutils/elf/deplibs.c
+++ b/core/src/xmake/binutils/elf/deplibs.c
@@ -46,6 +46,30 @@ static tb_bool_t xm_binutils_elf_deplibs_32(tb_stream_ref_t istream, tb_hize_t b
return tb_false;
}
+ tb_size_t result_count = 0;
+
+ // find program interpreter (PT_INTERP)
+ if (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_INTERP) {
+ tb_char_t name[256];
+ if (xm_binutils_read_string(istream, base_offset + phdr.p_offset, name, sizeof(name)) && name[0]) {
+ lua_pushinteger(lua, result_count + 1);
+ lua_pushstring(lua, name);
+ lua_settable(lua, -3);
+ result_count++;
+ }
+ break;
+ }
+ }
+ }
+ }
+
// find .dynamic section
xm_elf32_section_t dynamic_section;
tb_bool_t found_dynamic = tb_false;
@@ -87,7 +111,6 @@ static tb_bool_t xm_binutils_elf_deplibs_32(tb_stream_ref_t istream, tb_hize_t b
return tb_false;
}
- tb_size_t result_count = 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))) {
@@ -98,7 +121,7 @@ static tb_bool_t xm_binutils_elf_deplibs_32(tb_stream_ref_t istream, tb_hize_t b
break;
}
- if (dyn.d_tag == XM_ELF_DT_NEEDED) {
+ if (dyn.d_tag == XM_ELF_DT_NEEDED || dyn.d_tag == XM_ELF_DT_SONAME) {
tb_char_t name[256];
if (xm_binutils_elf_read_string(istream, base_offset + strtab_section.sh_offset, dyn.d_un.d_val, name, sizeof(name)) && name[0]) {
lua_pushinteger(lua, result_count + 1);
@@ -124,6 +147,30 @@ static tb_bool_t xm_binutils_elf_deplibs_64(tb_stream_ref_t istream, tb_hize_t b
return tb_false;
}
+ tb_size_t result_count = 0;
+
+ // find program interpreter (PT_INTERP)
+ if (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_INTERP) {
+ tb_char_t name[256];
+ if (xm_binutils_read_string(istream, base_offset + phdr.p_offset, name, sizeof(name)) && name[0]) {
+ lua_pushinteger(lua, result_count + 1);
+ lua_pushstring(lua, name);
+ lua_settable(lua, -3);
+ result_count++;
+ }
+ break;
+ }
+ }
+ }
+ }
+
// find .dynamic section
xm_elf64_section_t dynamic_section;
tb_bool_t found_dynamic = tb_false;
@@ -165,7 +212,6 @@ static tb_bool_t xm_binutils_elf_deplibs_64(tb_stream_ref_t istream, tb_hize_t b
return tb_false;
}
- tb_size_t result_count = 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))) {
@@ -176,7 +222,7 @@ static tb_bool_t xm_binutils_elf_deplibs_64(tb_stream_ref_t istream, tb_hize_t b
break;
}
- if (dyn.d_tag == XM_ELF_DT_NEEDED) {
+ if (dyn.d_tag == XM_ELF_DT_NEEDED || dyn.d_tag == XM_ELF_DT_SONAME) {
tb_char_t name[256];
if (xm_binutils_elf_read_string(istream, base_offset + strtab_section.sh_offset, (tb_uint32_t)dyn.d_un.d_val, name, sizeof(name)) && name[0]) {
lua_pushinteger(lua, result_count + 1);
diff --git a/core/src/xmake/binutils/elf/prefix.h b/core/src/xmake/binutils/elf/prefix.h
index f69f4ee3a..a967c5ce7 100644
--- a/core/src/xmake/binutils/elf/prefix.h
+++ b/core/src/xmake/binutils/elf/prefix.h
@@ -51,10 +51,15 @@
#define XM_ELF_SHT_STRTAB 0x3
#define XM_ELF_SHT_DYNAMIC 0x6
+#define XM_ELF_PT_LOAD 1
+#define XM_ELF_PT_DYNAMIC 2
+#define XM_ELF_PT_INTERP 3
+
#define XM_ELF_DT_NULL 0
#define XM_ELF_DT_NEEDED 1
#define XM_ELF_DT_STRTAB 5
#define XM_ELF_DT_STRSZ 10
+#define XM_ELF_DT_SONAME 14
#define XM_ELF_SHF_ALLOC 0x2
#define XM_ELF_SHF_WRITE 0x1
@@ -105,6 +110,17 @@ typedef struct __xm_elf32_symbol_t {
tb_uint16_t st_shndx;
} __tb_packed__ xm_elf32_symbol_t;
+typedef struct __xm_elf32_phdr_t {
+ tb_uint32_t p_type;
+ tb_uint32_t p_offset;
+ tb_uint32_t p_vaddr;
+ tb_uint32_t p_paddr;
+ tb_uint32_t p_filesz;
+ tb_uint32_t p_memsz;
+ tb_uint32_t p_flags;
+ tb_uint32_t p_align;
+} __tb_packed__ xm_elf32_phdr_t;
+
typedef struct __xm_elf64_header_t {
tb_uint8_t e_ident[16];
tb_uint16_t e_type;
@@ -144,6 +160,17 @@ typedef struct __xm_elf64_symbol_t {
tb_uint64_t st_size;
} __tb_packed__ xm_elf64_symbol_t;
+typedef struct __xm_elf64_phdr_t {
+ tb_uint32_t p_type;
+ tb_uint32_t p_flags;
+ tb_uint64_t p_offset;
+ tb_uint64_t p_vaddr;
+ tb_uint64_t p_paddr;
+ tb_uint64_t p_filesz;
+ tb_uint64_t p_memsz;
+ tb_uint64_t p_align;
+} __tb_packed__ xm_elf64_phdr_t;
+
typedef struct __xm_elf32_dynamic_t {
tb_int32_t d_tag;
union {