diff options
| author | ruki <[email protected]> | 2026-07-18 20:56:26 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2026-07-18 20:56:26 +0800 |
| commit | f2868ddfe82e0ce73888deebbf940dc9f511ef5e (patch) | |
| tree | 457cafa9833ecf395c464a6873b7e291d4f4b8f5 | |
| parent | 6b3632901ad14e99d306232c15cf788f6b081066 (diff) | |
detect obj flags for bin2elf
| -rw-r--r-- | core/src/xmake/binutils/elf/bin2elf.c | 69 | ||||
| -rw-r--r-- | xmake/core/base/binutils.lua | 4 | ||||
| -rw-r--r-- | xmake/modules/cli/binutils/bin2obj.lua | 3 | ||||
| -rw-r--r-- | xmake/rules/utils/bin2obj/utils.lua | 24 | ||||
| -rw-r--r-- | xmake/rules/utils/bin2obj/xmake.lua | 2 |
5 files changed, 91 insertions, 11 deletions
diff --git a/core/src/xmake/binutils/elf/bin2elf.c b/core/src/xmake/binutils/elf/bin2elf.c index ff3f941f1..ee18e6113 100644 --- a/core/src/xmake/binutils/elf/bin2elf.c +++ b/core/src/xmake/binutils/elf/bin2elf.c @@ -60,12 +60,51 @@ static tb_bool_t xm_binutils_bin2elf_bwrit_symbol_64(tb_stream_ref_t ostream, xm return tb_stream_bwrit(ostream, (tb_byte_t const *)s, sizeof(*s)); } +// read the identity (class/endianness/machine/e_flags) from a reference ELF object. +// returns tb_true and updates the out-params on success; leaves them untouched on any failure +// (missing file, too small, bad magic), so the caller keeps its arch-derived defaults. +static tb_bool_t xm_binutils_bin2elf_read_refobj(tb_char_t const *refobj, + tb_bool_t *pis_64bit, tb_bool_t *pis_bigendian, tb_uint16_t *pe_machine, tb_uint32_t *pe_flags) { + tb_assert_and_check_return_val(refobj && pis_64bit && pis_bigendian && pe_machine && pe_flags, tb_false); + + tb_bool_t ok = tb_false; + tb_stream_ref_t stream = tb_stream_init_from_file(refobj, TB_FILE_MODE_RO); + do { + // the 32-bit ELF header is 52 bytes; the 64-bit e_flags ends at offset 52 too + tb_byte_t hdr[52]; + if (!stream || !tb_stream_open(stream)) break; + if (!tb_stream_bread(stream, hdr, sizeof(hdr))) break; + + // verify the ELF magic (0x7f 'E' 'L' 'F') + if (hdr[0] != 0x7f || hdr[1] != 'E' || hdr[2] != 'L' || hdr[3] != 'F') break; + + tb_bool_t is_64bit = (hdr[XM_ELF_EI_CLASS] == XM_ELF_CLASS64); + tb_bool_t is_bigendian = (hdr[5] == XM_ELF_DATA2MSB); + + // e_machine at offset 18 (2 bytes); e_flags at offset 36 (32-bit) / 48 (64-bit), in target endianness + tb_uint16_t e_machine = is_bigendian? tb_bits_get_u16_be(hdr + 18) : tb_bits_get_u16_le(hdr + 18); + tb_byte_t const *pflags = hdr + (is_64bit? 48 : 36); + tb_uint32_t e_flags = is_bigendian? tb_bits_get_u32_be(pflags) : tb_bits_get_u32_le(pflags); + + *pis_64bit = is_64bit; + *pis_bigendian = is_bigendian; + *pe_machine = e_machine; + *pe_flags = e_flags; + ok = tb_true; + + } while (0); + if (stream) tb_stream_clos(stream); + return ok; +} + static tb_bool_t xm_binutils_bin2elf_dump_32(tb_stream_ref_t istream, tb_stream_ref_t ostream, tb_char_t const *symbol_prefix, tb_char_t const *arch, tb_char_t const *basename, tb_bool_t bigendian, + tb_uint16_t e_machine, + tb_uint32_t e_flags, tb_bool_t zeroend) { tb_assert_and_check_return_val(istream && ostream, tb_false); @@ -148,9 +187,9 @@ static tb_bool_t xm_binutils_bin2elf_dump_32(tb_stream_ref_t istream, header.e_ident[6] = 1; // EV_CURRENT header.e_ident[7] = 0; // ELFOSABI_SYSV header.e_type = 1; // ET_REL - header.e_machine = xm_binutils_elf_get_machine(arch); + header.e_machine = e_machine; header.e_version = 1; - header.e_flags = xm_binutils_elf_get_flags(arch); + header.e_flags = e_flags; header.e_shoff = section_headers_ofs; header.e_ehsize = header_size; header.e_shentsize = section_header_size; @@ -368,6 +407,8 @@ static tb_bool_t xm_binutils_bin2elf_dump_64(tb_stream_ref_t istream, tb_char_t const *arch, tb_char_t const *basename, tb_bool_t bigendian, + tb_uint16_t e_machine, + tb_uint32_t e_flags, tb_bool_t zeroend) { tb_assert_and_check_return_val(istream && ostream, tb_false); @@ -450,9 +491,9 @@ static tb_bool_t xm_binutils_bin2elf_dump_64(tb_stream_ref_t istream, header.e_ident[6] = 1; // EV_CURRENT header.e_ident[7] = 0; // ELFOSABI_SYSV header.e_type = 1; // ET_REL - header.e_machine = xm_binutils_elf_get_machine(arch); + header.e_machine = e_machine; header.e_version = 1; - header.e_flags = xm_binutils_elf_get_flags(arch); + header.e_flags = e_flags; header.e_shoff = section_headers_ofs; header.e_ehsize = header_size; header.e_shentsize = section_header_size; @@ -670,7 +711,7 @@ static tb_bool_t xm_binutils_bin2elf_dump_64(tb_stream_ref_t istream, /* generate ELF object file from binary file * - * local ok, errors = binutils.bin2elf(binaryfile, outputfile, symbol_prefix, arch, basename, zeroend) + * local ok, errors = binutils.bin2elf(binaryfile, outputfile, symbol_prefix, arch, basename, zeroend, refobj) */ tb_int_t xm_binutils_bin2elf(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); @@ -695,6 +736,12 @@ tb_int_t xm_binutils_bin2elf(lua_State *lua) { // get zeroend (optional, default: false) tb_bool_t zeroend = lua_toboolean(lua, 6); + // get the reference object (optional): a real object emitted by the target toolchain. + // we mirror its class/endianness/machine/e_flags so the output matches exactly, instead of + // guessing from the (sometimes ambiguous) arch name. when absent/unreadable we fall back to + // deriving everything from the arch name. + tb_char_t const *refobj = lua_isstring(lua, 7) ? lua_tostring(lua, 7) : tb_null; + // do dump tb_bool_t ok = tb_false; tb_stream_ref_t istream = tb_stream_init_from_file(binaryfile, TB_FILE_MODE_RO); @@ -713,17 +760,23 @@ tb_int_t xm_binutils_bin2elf(lua_State *lua) { break; } - // choose 32-bit or 64-bit ELF based on architecture, and little/big endian + // resolve class/endian/machine/flags: derive from the arch name, then mirror the + // reference object if one was given and is a readable ELF (it wins over the heuristic) tb_bool_t is_64bit = xm_binutils_elf_is_64bit(arch); tb_bool_t is_bigendian = xm_binutils_elf_is_bigendian(arch); + tb_uint16_t e_machine = xm_binutils_elf_get_machine(arch); + tb_uint32_t e_flags = xm_binutils_elf_get_flags(arch); + if (refobj) { + xm_binutils_bin2elf_read_refobj(refobj, &is_64bit, &is_bigendian, &e_machine, &e_flags); + } if (is_64bit) { - if (!xm_binutils_bin2elf_dump_64(istream, ostream, symbol_prefix, arch, basename, is_bigendian, zeroend)) { + if (!xm_binutils_bin2elf_dump_64(istream, ostream, symbol_prefix, arch, basename, is_bigendian, e_machine, e_flags, zeroend)) { lua_pushboolean(lua, tb_false); lua_pushfstring(lua, "bin2elf: dump data failed"); break; } } else { - if (!xm_binutils_bin2elf_dump_32(istream, ostream, symbol_prefix, arch, basename, is_bigendian, zeroend)) { + if (!xm_binutils_bin2elf_dump_32(istream, ostream, symbol_prefix, arch, basename, is_bigendian, e_machine, e_flags, zeroend)) { lua_pushboolean(lua, tb_false); lua_pushfstring(lua, "bin2elf: dump data failed"); break; diff --git a/xmake/core/base/binutils.lua b/xmake/core/base/binutils.lua index 7adfe6efa..400d48332 100644 --- a/xmake/core/base/binutils.lua +++ b/xmake/core/base/binutils.lua @@ -64,6 +64,8 @@ end -- - target_minver: the target minimum version (only for macho) -- - xcode_sdkver: the Xcode SDK version (only for macho) -- - zeroend: append null terminator (default: false) +-- - refobj: a reference elf object whose class/endianness/machine/e_flags +-- are mirrored into the output (default: derived from arch) function binutils.bin2obj(binaryfile, outputfile, opt) opt = opt or {} local format = opt.format @@ -94,7 +96,7 @@ function binutils.bin2obj(binaryfile, outputfile, opt) if not binutils._bin2elf then return nil, "binutils._bin2elf not available (C implementation not compiled)" end - return binutils._bin2elf(binaryfile, outputfile, opt.symbol_prefix or "_binary_", opt.arch or "x86_64", opt.basename, opt.zeroend or false) + return binutils._bin2elf(binaryfile, outputfile, opt.symbol_prefix or "_binary_", opt.arch or "x86_64", opt.basename, opt.zeroend or false, opt.refobj) else return nil, string.format("unsupported format '%s' (supported: coff, elf, macho)", format) end diff --git a/xmake/modules/cli/binutils/bin2obj.lua b/xmake/modules/cli/binutils/bin2obj.lua index 06493143a..c8314594f 100644 --- a/xmake/modules/cli/binutils/bin2obj.lua +++ b/xmake/modules/cli/binutils/bin2obj.lua @@ -32,7 +32,8 @@ local options = { {nil, "target_minver", "kv", nil, "Set the target minimum version (e.g., 10.0, 18.2)."}, {nil, "xcode_sdkver", "kv", nil, "Set the Xcode SDK version (e.g., 10.0, 18.2)."}, {nil, "zeroend", "k", nil, "Append a null terminator ('\\0') at the end of data."}, - {nil, "cosmocc", "k", nil, "Enable cosmocc support (generate concomitant object file)."} + {nil, "cosmocc", "k", nil, "Enable cosmocc support (generate concomitant object file)."}, + {nil, "refobj", "kv", nil, "Mirror class/endianness/machine/e_flags from a reference elf object."} } function main(...) diff --git a/xmake/rules/utils/bin2obj/utils.lua b/xmake/rules/utils/bin2obj/utils.lua index 3e8802e97..a14ba829a 100644 --- a/xmake/rules/utils/bin2obj/utils.lua +++ b/xmake/rules/utils/bin2obj/utils.lua @@ -18,6 +18,21 @@ -- @file utils.lua -- +-- pick a reference object already compiled by the target's own toolchain +-- +-- bin2obj mirrors its class/endianness/machine/e_flags, so the generated object matches the +-- toolchain exactly instead of guessing from the (sometimes ambiguous) arch name. we reuse one +-- of the target's own objects rather than compiling a dedicated probe; any object the toolchain +-- emits carries the same identity. we return the first one that already exists on disk, skipping +-- the object we are generating right now (which does not exist yet anyway). +function _get_refobj(target, objectfile) + for _, obj in ipairs(target:objectfiles()) do + if obj ~= objectfile and os.isfile(obj) then + return obj + end + end +end + -- generate object file from binary file -- -- @param target the target @@ -116,6 +131,15 @@ function generate_objectfile(target, batchcmds, binaryfile, opt) if is_cosmocc then table.insert(argv, "--cosmocc") end + + -- mirror the toolchain's elf identity (endianness/machine/e_flags) from one of the target's + -- own compiled objects, so the output matches and links regardless of how the arch name maps + if format == "elf" and not is_cosmocc then + local refobj = _get_refobj(target, objectfile) + if refobj then + table.insert(argv, "--refobj=" .. refobj) + end + end batchcmds:vlua("cli.binutils.bin2obj", argv) return objectfile diff --git a/xmake/rules/utils/bin2obj/xmake.lua b/xmake/rules/utils/bin2obj/xmake.lua index 760b9d536..b826fbd99 100644 --- a/xmake/rules/utils/bin2obj/xmake.lua +++ b/xmake/rules/utils/bin2obj/xmake.lua @@ -21,7 +21,7 @@ rule("utils.bin2obj") set_extensions(".bin") add_orders("utils.bin2obj", "c++.build.modules.builder") - on_buildcmd_file(function (target, batchcmds, sourcefile_bin, opt) + after_buildcmd_file(function (target, batchcmds, sourcefile_bin, opt) import("rules.utils.bin2obj.utils", {alias = "bin2obj_utils", rootdir = os.programdir()}) -- get zeroend (default: false) |
