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 /xmake | |
| parent | 6b3632901ad14e99d306232c15cf788f6b081066 (diff) | |
detect obj flags for bin2elf
Diffstat (limited to 'xmake')
| -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 |
4 files changed, 30 insertions, 3 deletions
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) |
