summaryrefslogtreecommitdiff
path: root/xmake
diff options
context:
space:
mode:
authorruki <[email protected]>2025-12-09 00:29:51 +0800
committerruki <[email protected]>2025-12-09 00:29:51 +0800
commitba293bc4b8840da34220e1f54313d531d18dde8e (patch)
treee6b6325ccf446b845b26a3a21f06f0bd84914170 /xmake
parentf1726a8f0f63160ae2185f28b38a740caf6d1ab1 (diff)
add binary readsyms
Diffstat (limited to 'xmake')
-rw-r--r--xmake/core/base/binutils.lua10
-rw-r--r--xmake/core/sandbox/modules/import/core/base/binutils.lua10
-rw-r--r--xmake/core/sandbox/modules/utils.lua2
-rw-r--r--xmake/modules/utils/binary/readsyms.lua92
4 files changed, 113 insertions, 1 deletions
diff --git a/xmake/core/base/binutils.lua b/xmake/core/base/binutils.lua
index b21de2276..8047046b2 100644
--- a/xmake/core/base/binutils.lua
+++ b/xmake/core/base/binutils.lua
@@ -26,6 +26,7 @@ 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)
@@ -56,6 +57,15 @@ function binutils.bin2elf(binaryfile, outputfile, opt)
return binutils._bin2elf(binaryfile, outputfile, opt.symbol_prefix or "_binary_", opt.arch or "x86_64", opt.basename, opt.zeroend or false)
end
+-- 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
return binutils
diff --git a/xmake/core/sandbox/modules/import/core/base/binutils.lua b/xmake/core/sandbox/modules/import/core/base/binutils.lua
index 38aad9b73..9e371d777 100644
--- a/xmake/core/sandbox/modules/import/core/base/binutils.lua
+++ b/xmake/core/sandbox/modules/import/core/base/binutils.lua
@@ -57,6 +57,16 @@ function sandbox_core_base_binutils.bin2elf(binaryfile, outputfile, opt)
end
end
+-- read symbols from object file (auto-detect format: ELF, COFF, Mach-O)
+function sandbox_core_base_binutils.readsyms(binaryfile)
+ local symbols, errors = binutils.readsyms(binaryfile)
+ if symbols then
+ return symbols
+ else
+ raise("readsyms: %s", errors or "unknown errors")
+ end
+end
+
-- return module
return sandbox_core_base_binutils
diff --git a/xmake/core/sandbox/modules/utils.lua b/xmake/core/sandbox/modules/utils.lua
index df90492be..1be9c7c08 100644
--- a/xmake/core/sandbox/modules/utils.lua
+++ b/xmake/core/sandbox/modules/utils.lua
@@ -150,7 +150,7 @@ function sandbox_utils.assert(value, format, ...)
end
-- generate c/c++ code from the binary file (deprecated, use binutils.bin2c instead)
-function sandbox_utils.bin2c(binaryfile, outputfile, opt)
+ function sandbox_utils.bin2c(binaryfile, outputfile, opt)
deprecated.add("binutils.bin2c", "utils.bin2c")
return binutils.bin2c(binaryfile, outputfile, opt)
end
diff --git a/xmake/modules/utils/binary/readsyms.lua b/xmake/modules/utils/binary/readsyms.lua
new file mode 100644
index 000000000..ace1c8be3
--- /dev/null
+++ b/xmake/modules/utils/binary/readsyms.lua
@@ -0,0 +1,92 @@
+--!A cross-platform build utility based on Lua
+--
+-- Licensed under the Apache License, Version 2.0 (the "License");
+-- you may not use this file except in compliance with the License.
+-- You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+-- Copyright (C) 2015-present, Xmake Open Source Community.
+--
+-- @author ruki
+-- @file readsyms.lua
+--
+
+-- imports
+import("core.base.binutils")
+
+-- get symbols from binary file (auto-detect format: ELF, COFF, Mach-O)
+--
+-- @param binaryfile the binary file path (required)
+-- @return the symbols table
+function _get_symbols(binaryfile)
+ assert(binaryfile, "usage: xmake l utils.binary.readsyms <binaryfile>")
+
+ binaryfile = path.absolute(binaryfile)
+ assert(os.isfile(binaryfile), "%s not found!", binaryfile)
+
+ if binutils.readsyms then
+ local ok, symbols = binutils.readsyms(binaryfile)
+ if ok then
+ return symbols
+ else
+ raise("readsyms: %s", symbols or "unknown error")
+ end
+ else
+ raise("readsyms: binutils.readsyms not available (C implementation not compiled)")
+ end
+end
+
+-- dump symbols to console
+--
+-- @param binaryfile the object file path (required)
+function dump(binaryfile)
+ local symbols = _get_symbols(binaryfile)
+ if symbols and #symbols > 0 then
+ print("")
+ print("Symbols:")
+ for i, sym in ipairs(symbols) do
+ local value_str = ""
+ if sym.value then
+ value_str = string.format("0x%x", sym.value)
+ end
+ local size_str = ""
+ if sym.size then
+ size_str = string.format(" size=%d", sym.size)
+ end
+ local section_str = ""
+ if sym.section and sym.section > 0 then
+ section_str = string.format(" section=%d", sym.section)
+ end
+ local type_str = ""
+ if sym.type then
+ type_str = string.format(" type=%s", sym.type)
+ end
+ local bind_str = ""
+ if sym.bind then
+ bind_str = string.format(" bind=%s", sym.bind)
+ end
+ print(string.format(" %s %s%s%s%s%s", sym.name or "", value_str, size_str, section_str, type_str, bind_str))
+ end
+ print("")
+ cprint("${bright}%d symbols found!", #symbols)
+ else
+ print("")
+ cprint("${bright}No symbols found!")
+ end
+end
+
+-- read symbols from object file (auto-detect format: ELF, COFF, Mach-O)
+--
+-- @param binaryfile the object file path (required)
+-- @return the symbols table
+function main(binaryfile)
+ return _get_symbols(binaryfile)
+end
+