diff options
Diffstat (limited to 'xmake/rules/utils/bin2obj')
| -rw-r--r-- | xmake/rules/utils/bin2obj/utils.lua | 38 | ||||
| -rw-r--r-- | xmake/rules/utils/bin2obj/xmake.lua | 8 |
2 files changed, 39 insertions, 7 deletions
diff --git a/xmake/rules/utils/bin2obj/utils.lua b/xmake/rules/utils/bin2obj/utils.lua index 3e8802e97..dcd4b9c80 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 @@ -35,6 +50,7 @@ function generate_objectfile(target, batchcmds, binaryfile, opt) opt = opt or {} local rulename = opt.rulename or "utils.bin2obj" local progress = opt.progress + local fileconfig = target:fileconfig(binaryfile) -- check for cosmocc toolchain local is_cosmocc = target:toolchain("cosmocc") or (target:has_tool("cc", "cosmocc") and target:has_tool("ar", "cosmoar")) @@ -53,6 +69,16 @@ function generate_objectfile(target, batchcmds, binaryfile, opt) end end + -- transform binary data first + -- @see https://github.com/xmake-io/xmake/issues/7513 + local transform = opt.transform or (fileconfig and fileconfig.transform) or target:extraconf("rules", rulename, "transform") + if transform then + batchcmds:show_progress(progress, "${color.build.object}transforming.bin2obj %s", binaryfile) + local transformed_file = target:autogenfile(binaryfile) + batchcmds:call(transform, {binaryfile, transformed_file}, {name = "bin2obj/transform", target = target}) + binaryfile = transformed_file + end + -- get object file local objectfile = opt.objectfile if not objectfile then @@ -75,6 +101,9 @@ function generate_objectfile(target, batchcmds, binaryfile, opt) -- get zeroend (default: false, but can be overridden) local zeroend = opt.zeroend if zeroend == nil then + zeroend = fileconfig and fileconfig.zeroend + end + if zeroend == nil then zeroend = target:extraconf("rules", rulename, "zeroend") or false end @@ -116,6 +145,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..f461e67ef 100644 --- a/xmake/rules/utils/bin2obj/xmake.lua +++ b/xmake/rules/utils/bin2obj/xmake.lua @@ -21,18 +21,12 @@ 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) - -- check file-level config first, then rule-level config - local fileconfig = target:fileconfig(sourcefile_bin) - local zeroend = (fileconfig and fileconfig.zeroend) or target:extraconf("rules", "utils.bin2obj", "zeroend") or false - -- convert binary file to object file local objectfile = bin2obj_utils.generate_objectfile(target, batchcmds, sourcefile_bin, { progress = opt.progress, - zeroend = zeroend }) -- add deps |
