summaryrefslogtreecommitdiff
path: root/xmake/rules/utils/bin2obj
diff options
context:
space:
mode:
authorruki <[email protected]>2026-07-18 23:17:40 +0800
committerGitHub <[email protected]>2026-07-18 23:17:40 +0800
commit64439096a06b65556fff59f3b0b4e6a439b44129 (patch)
tree7d2efb39c42d227dd32723ca5e02ba95f86cfda4 /xmake/rules/utils/bin2obj
parent108eaa6d115e03f44bd1f105328dbe90a7747b30 (diff)
parentcfebf2a690e1a0ad2255f64eec5bec43eeac0049 (diff)
Merge pull request #7655 from xmake-io/binutilsmaster
fix bin2obj for some archs
Diffstat (limited to 'xmake/rules/utils/bin2obj')
-rw-r--r--xmake/rules/utils/bin2obj/utils.lua24
-rw-r--r--xmake/rules/utils/bin2obj/xmake.lua2
2 files changed, 25 insertions, 1 deletions
diff --git a/xmake/rules/utils/bin2obj/utils.lua b/xmake/rules/utils/bin2obj/utils.lua
index bcee334f0..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
@@ -130,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 58ec87a90..f461e67ef 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()})
-- convert binary file to object file