summaryrefslogtreecommitdiff
path: root/xmake/rules/utils/bin2obj/utils.lua
diff options
context:
space:
mode:
authorruki <[email protected]>2026-07-18 20:56:26 +0800
committerruki <[email protected]>2026-07-18 20:56:26 +0800
commitf2868ddfe82e0ce73888deebbf940dc9f511ef5e (patch)
tree457cafa9833ecf395c464a6873b7e291d4f4b8f5 /xmake/rules/utils/bin2obj/utils.lua
parent6b3632901ad14e99d306232c15cf788f6b081066 (diff)
detect obj flags for bin2elf
Diffstat (limited to 'xmake/rules/utils/bin2obj/utils.lua')
-rw-r--r--xmake/rules/utils/bin2obj/utils.lua24
1 files changed, 24 insertions, 0 deletions
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