summaryrefslogtreecommitdiff
path: root/xmake/core/base/binutils.lua
diff options
context:
space:
mode:
authorruki <[email protected]>2025-12-09 22:09:11 +0800
committerGitHub <[email protected]>2025-12-09 22:09:11 +0800
commitf2dcbc2e1450c84ed192e57c7f8b03e51717d71e (patch)
tree6abc959ce5b5ea6a19823021bb00ae364eb8e66b /xmake/core/base/binutils.lua
parent35d91e1636263028b8431cea85b0fd7854392a8a (diff)
parent19859fd57908ea9b08b8b6baa426051d2e7c7814 (diff)
Merge pull request #7109 from xmake-io/binutils
Improve binutils to readsyms from binary file
Diffstat (limited to 'xmake/core/base/binutils.lua')
-rw-r--r--xmake/core/base/binutils.lua68
1 files changed, 56 insertions, 12 deletions
diff --git a/xmake/core/base/binutils.lua b/xmake/core/base/binutils.lua
index b21de2276..77b17dc92 100644
--- a/xmake/core/base/binutils.lua
+++ b/xmake/core/base/binutils.lua
@@ -21,11 +21,15 @@
-- define module
local binutils = binutils or {}
+-- load modules
+local os = require("base/os")
+
-- save original interfaces
binutils._bin2c = binutils._bin2c or binutils.bin2c
binutils._bin2coff = binutils._bin2coff or binutils.bin2coff
binutils._bin2macho = binutils._bin2macho or binutils.bin2macho
binutils._bin2elf = binutils._bin2elf or binutils.bin2elf
+binutils._readsyms = binutils._readsyms or binutils.readsyms
-- generate c/c++ code from the binary file
function binutils.bin2c(binaryfile, outputfile, opt)
@@ -38,22 +42,62 @@ function binutils.bin2c(binaryfile, outputfile, opt)
end
end
--- generate COFF object file from the binary file
-function binutils.bin2coff(binaryfile, outputfile, opt)
+-- generate object file from the binary file
+-- @param binaryfile the binary file path
+-- @param outputfile the output object file path
+-- @param opt the options
+-- - format: the object file format (coff, elf, macho), required
+-- - symbol_prefix: the symbol prefix (default: _binary_)
+-- - arch: the target architecture (default: x86_64)
+-- - plat: the target platform (default: macosx, only for macho)
+-- - basename: the base name for symbols
+-- - target_minver: the target minimum version (only for macho)
+-- - xcode_sdkver: the Xcode SDK version (only for macho)
+-- - zeroend: append null terminator (default: false)
+function binutils.bin2obj(binaryfile, outputfile, opt)
opt = opt or {}
- return binutils._bin2coff(binaryfile, outputfile, opt.symbol_prefix or "_binary_", opt.arch or "x86_64", opt.basename, opt.zeroend or false)
-end
+ local format = opt.format
+ if not format then
+ -- auto-detect format based on host platform
+ local host = os.host()
+ if host == "windows" or host == "mingw" or host == "msys" or host == "cygwin" then
+ format = "coff"
+ elseif host == "macosx" or host == "iphoneos" or host == "watchos" or host == "appletvos" then
+ format = "macho"
+ else
+ format = "elf"
+ end
+ end
+ format = format:lower()
--- generate Mach-O object file from the binary file
-function binutils.bin2macho(binaryfile, outputfile, opt)
- opt = opt or {}
- return binutils._bin2macho(binaryfile, outputfile, opt.symbol_prefix or "_binary_", opt.plat or "macosx", opt.arch or "x86_64", opt.basename, opt.target_minver, opt.xcode_sdkver, opt.zeroend or false)
+ if format == "coff" then
+ if not binutils._bin2coff then
+ return nil, "bin2obj: binutils._bin2coff not available (C implementation not compiled)"
+ end
+ return binutils._bin2coff(binaryfile, outputfile, opt.symbol_prefix or "_binary_", opt.arch or "x86_64", opt.basename, opt.zeroend or false)
+ elseif format == "macho" then
+ if not binutils._bin2macho then
+ return nil, "bin2obj: binutils._bin2macho not available (C implementation not compiled)"
+ end
+ return binutils._bin2macho(binaryfile, outputfile, opt.symbol_prefix or "_binary_", opt.plat or "macosx", opt.arch or "x86_64", opt.basename, opt.target_minver, opt.xcode_sdkver, opt.zeroend or false)
+ elseif format == "elf" then
+ if not binutils._bin2elf then
+ return nil, "bin2obj: 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)
+ else
+ return nil, string.format("bin2obj: unsupported format '%s' (supported: coff, elf, macho)", format)
+ end
end
--- generate ELF object file from the binary file
-function binutils.bin2elf(binaryfile, outputfile, opt)
- opt = opt or {}
- return binutils._bin2elf(binaryfile, outputfile, opt.symbol_prefix or "_binary_", opt.arch or "x86_64", opt.basename, opt.zeroend or false)
+
+-- read symbols from object file (auto-detect format: COFF, ELF, or Mach-O)
+function binutils.readsyms(binaryfile)
+ if binutils._readsyms then
+ return binutils._readsyms(binaryfile)
+ else
+ return nil, "readsyms: C implementation not available"
+ end
end
-- return module