diff options
| author | ruki <[email protected]> | 2025-12-07 19:31:07 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2025-12-07 19:31:29 +0800 |
| commit | e17cc0cfe314d1e691d120d2dfa92f6c92790dff (patch) | |
| tree | 7fcd3e8539e33c0f3d9c7082b88dc48b40a518a7 /core/src | |
| parent | c4b2eb7751d994d00da94133c397389c70d3b3a8 (diff) | |
improve bin2elf
Diffstat (limited to 'core/src')
| -rw-r--r-- | core/src/xmake/binutils/bin2coff.c | 2 | ||||
| -rw-r--r-- | core/src/xmake/binutils/bin2elf.c | 115 |
2 files changed, 102 insertions, 15 deletions
diff --git a/core/src/xmake/binutils/bin2coff.c b/core/src/xmake/binutils/bin2coff.c index 8224a7607..9a095a2cd 100644 --- a/core/src/xmake/binutils/bin2coff.c +++ b/core/src/xmake/binutils/bin2coff.c @@ -225,7 +225,7 @@ static tb_bool_t xm_binutils_bin2coff_dump(tb_stream_ref_t istream, tb_uint32_t section_data_size = datasize; tb_uint32_t section_data_padding = (4 - (section_data_size & 3)) & 3; tb_uint32_t symbol_table_ofs = section_data_ofs + section_data_size + section_data_padding; - + // calculate string table size (content only, excluding the 4-byte size field) tb_uint32_t string_table_content_size = 0; tb_size_t start_len = tb_strlen(symbol_start); diff --git a/core/src/xmake/binutils/bin2elf.c b/core/src/xmake/binutils/bin2elf.c index bb2507a34..22339925c 100644 --- a/core/src/xmake/binutils/bin2elf.c +++ b/core/src/xmake/binutils/bin2elf.c @@ -33,10 +33,21 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * macros */ +#define XM_ELF_MACHINE_NONE 0x00 +#define XM_ELF_MACHINE_SPARC 0x02 +#define XM_ELF_MACHINE_I386 0x03 +#define XM_ELF_MACHINE_MIPS 0x08 +#define XM_ELF_MACHINE_POWERPC 0x14 +#define XM_ELF_MACHINE_POWERPC64 0x15 +#define XM_ELF_MACHINE_S390 0x16 +#define XM_ELF_MACHINE_ARM 0x28 +#define XM_ELF_MACHINE_SUPERH 0x2a +#define XM_ELF_MACHINE_SPARC64 0x2b +#define XM_ELF_MACHINE_IA_64 0x32 #define XM_ELF_MACHINE_X86_64 0x3e +#define XM_ELF_MACHINE_RISCV 0xf3 #define XM_ELF_MACHINE_ARM64 0xb7 -#define XM_ELF_MACHINE_ARM 0x28 -#define XM_ELF_MACHINE_I386 0x03 +#define XM_ELF_MACHINE_LOONGARCH 0x102 #define XM_ELF_SHT_PROGBITS 0x1 #define XM_ELF_SHT_SYMTAB 0x2 @@ -139,16 +150,59 @@ static tb_uint16_t xm_binutils_bin2elf_get_machine(tb_char_t const *arch) { if (!arch) { return XM_ELF_MACHINE_X86_64; } + // x86/x86_64 if (tb_strcmp(arch, "x86_64") == 0 || tb_strcmp(arch, "x64") == 0) { return XM_ELF_MACHINE_X86_64; - } else if (tb_strcmp(arch, "arm64") == 0 || tb_strcmp(arch, "aarch64") == 0 || - tb_strcmp(arch, "arm64-v8a") == 0) { + } else if (tb_strcmp(arch, "i386") == 0 || tb_strcmp(arch, "x86") == 0) { + return XM_ELF_MACHINE_I386; + } + // ARM + else if (tb_strcmp(arch, "arm64") == 0 || tb_strcmp(arch, "aarch64") == 0 || + tb_strcmp(arch, "arm64-v8a") == 0) { return XM_ELF_MACHINE_ARM64; } else if (tb_strcmp(arch, "arm") == 0 || tb_strcmp(arch, "armv7") == 0 || - tb_strcmp(arch, "armeabi-v7a") == 0) { + tb_strcmp(arch, "armeabi-v7a") == 0 || tb_strcmp(arch, "armv6") == 0 || + tb_strcmp(arch, "armv5") == 0) { return XM_ELF_MACHINE_ARM; - } else if (tb_strcmp(arch, "i386") == 0 || tb_strcmp(arch, "x86") == 0) { - return XM_ELF_MACHINE_I386; + } + // MIPS (MIPS and MIPS64 use same machine type, distinguished by ELF class) + else if (tb_strncmp(arch, "mips", 4) == 0) { + return XM_ELF_MACHINE_MIPS; + } + // PowerPC + else if (tb_strncmp(arch, "ppc64", 5) == 0 || tb_strncmp(arch, "powerpc64", 9) == 0) { + return XM_ELF_MACHINE_POWERPC64; + } else if (tb_strncmp(arch, "ppc", 3) == 0 || tb_strncmp(arch, "powerpc", 7) == 0) { + return XM_ELF_MACHINE_POWERPC; + } + // RISC-V (RISC-V and RISC-V64 use different machine types) + else if (tb_strncmp(arch, "riscv64", 7) == 0 || + (tb_strncmp(arch, "riscv", 5) == 0 && tb_strstr(arch, "64"))) { + return XM_ELF_MACHINE_RISCV; // RISC-V 64-bit uses same machine type, distinguished by ELF class + } else if (tb_strncmp(arch, "riscv", 5) == 0) { + return XM_ELF_MACHINE_RISCV; + } + // SPARC + else if (tb_strncmp(arch, "sparc64", 7) == 0) { + return XM_ELF_MACHINE_SPARC64; + } else if (tb_strncmp(arch, "sparc", 5) == 0) { + return XM_ELF_MACHINE_SPARC; + } + // s390x + else if (tb_strcmp(arch, "s390x") == 0 || tb_strcmp(arch, "s390") == 0) { + return XM_ELF_MACHINE_S390; + } + // LoongArch (LoongArch and LoongArch64 use same machine type, distinguished by ELF class) + else if (tb_strncmp(arch, "loongarch", 9) == 0 || tb_strncmp(arch, "loong64", 7) == 0) { + return XM_ELF_MACHINE_LOONGARCH; + } + // SuperH + else if (tb_strncmp(arch, "sh", 2) == 0 || tb_strncmp(arch, "superh", 6) == 0) { + return XM_ELF_MACHINE_SUPERH; + } + // IA-64 (Itanium) + else if (tb_strcmp(arch, "ia64") == 0 || tb_strcmp(arch, "itanium") == 0) { + return XM_ELF_MACHINE_IA_64; } return XM_ELF_MACHINE_X86_64; } @@ -157,9 +211,42 @@ static tb_bool_t xm_binutils_bin2elf_is_64bit(tb_char_t const *arch) { if (!arch) { return tb_true; } - if (tb_strcmp(arch, "x86_64") == 0 || tb_strcmp(arch, "x64") == 0 || - tb_strcmp(arch, "arm64") == 0 || tb_strcmp(arch, "aarch64") == 0 || - tb_strcmp(arch, "arm64-v8a") == 0) { + // x86_64 + if (tb_strcmp(arch, "x86_64") == 0 || tb_strcmp(arch, "x64") == 0) { + return tb_true; + } + // ARM64 + else if (tb_strcmp(arch, "arm64") == 0 || tb_strcmp(arch, "aarch64") == 0 || + tb_strcmp(arch, "arm64-v8a") == 0) { + return tb_true; + } + // MIPS64 + else if (tb_strncmp(arch, "mips64", 6) == 0) { + return tb_true; + } + // PowerPC64 + else if (tb_strncmp(arch, "ppc64", 5) == 0 || tb_strncmp(arch, "powerpc64", 9) == 0) { + return tb_true; + } + // RISC-V 64 + else if (tb_strncmp(arch, "riscv64", 7) == 0 || + (tb_strncmp(arch, "riscv", 5) == 0 && tb_strstr(arch, "64"))) { + return tb_true; + } + // SPARC64 + else if (tb_strncmp(arch, "sparc64", 7) == 0) { + return tb_true; + } + // s390x + else if (tb_strcmp(arch, "s390x") == 0) { + return tb_true; + } + // LoongArch64 + else if (tb_strncmp(arch, "loongarch64", 11) == 0) { + return tb_true; + } + // IA-64 + else if (tb_strcmp(arch, "ia64") == 0 || tb_strcmp(arch, "itanium") == 0) { return tb_true; } return tb_false; @@ -226,7 +313,7 @@ static tb_bool_t xm_binutils_bin2elf_dump_32(tb_stream_ref_t istream, tb_uint32_t symtab_size = 3 * sizeof(xm_elf32_symbol_t); // NULL, start, end tb_uint32_t symtab_padding = (4 - (symtab_size & 3)) & 3; // align to 4 bytes tb_uint32_t strtab_ofs = symtab_ofs + symtab_size + symtab_padding; - + // calculate string table size tb_size_t start_len = tb_strlen(symbol_start); tb_size_t end_len = tb_strlen(symbol_end); @@ -235,7 +322,7 @@ static tb_bool_t xm_binutils_bin2elf_dump_32(tb_stream_ref_t istream, strtab_size += (tb_uint32_t)(end_len + 1); tb_uint32_t strtab_padding = (4 - (strtab_size & 3)) & 3; // align to 4 bytes tb_uint32_t shstrtab_ofs = strtab_ofs + strtab_size + strtab_padding; - + // calculate section header string table size tb_uint32_t shstrtab_size = 1; // initial null byte shstrtab_size += 8; // ".rodata\0" (7 + 1) @@ -530,7 +617,7 @@ static tb_bool_t xm_binutils_bin2elf_dump_64(tb_stream_ref_t istream, tb_uint32_t symtab_size = 3 * sizeof(xm_elf64_symbol_t); // NULL, start, end tb_uint32_t symtab_padding = (8 - (symtab_size & 7)) & 7; tb_uint32_t strtab_ofs = symtab_ofs + symtab_size + symtab_padding; - + // calculate string table size tb_size_t start_len = tb_strlen(symbol_start); tb_size_t end_len = tb_strlen(symbol_end); @@ -538,7 +625,7 @@ static tb_bool_t xm_binutils_bin2elf_dump_64(tb_stream_ref_t istream, strtab_size += (tb_uint32_t)(start_len + 1); strtab_size += (tb_uint32_t)(end_len + 1); tb_uint32_t strtab_padding = (8 - (strtab_size & 7)) & 7; - + // calculate section header string table size tb_uint32_t shstrtab_size = 1; // initial null byte shstrtab_size += 8; // ".rodata\0" (7 + 1) |
