diff options
| author | ruki <[email protected]> | 2025-12-09 22:09:11 +0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2025-12-09 22:09:11 +0800 |
| commit | f2dcbc2e1450c84ed192e57c7f8b03e51717d71e (patch) | |
| tree | 6abc959ce5b5ea6a19823021bb00ae364eb8e66b /xmake/modules/utils/binary | |
| parent | 35d91e1636263028b8431cea85b0fd7854392a8a (diff) | |
| parent | 19859fd57908ea9b08b8b6baa426051d2e7c7814 (diff) | |
Merge pull request #7109 from xmake-io/binutils
Improve binutils to readsyms from binary file
Diffstat (limited to 'xmake/modules/utils/binary')
| -rw-r--r-- | xmake/modules/utils/binary/bin2obj.lua | 97 | ||||
| -rw-r--r-- | xmake/modules/utils/binary/readsyms.lua | 106 |
2 files changed, 121 insertions, 82 deletions
diff --git a/xmake/modules/utils/binary/bin2obj.lua b/xmake/modules/utils/binary/bin2obj.lua index 10d396551..95e705ca6 100644 --- a/xmake/modules/utils/binary/bin2obj.lua +++ b/xmake/modules/utils/binary/bin2obj.lua @@ -34,105 +34,38 @@ local options = { {nil, "zeroend", "k", nil, "Append a null terminator ('\\0') at the end of data."} } -function _do_bin2obj_coff(binarypath, outputpath, opt) - -- get filename from binary path (with extension, dots replaced with underscores) - local filename = path.filename(binarypath) - -- replace dots with underscores for symbol name (e.g., data.bin -> data_bin) - local basename = filename:gsub("%.", "_") - - -- prepare opt +function _do_bin2obj(binarypath, outputpath, opt) + -- init source directory and options opt = opt or {} - opt.basename = basename - - -- trace - print("converting binary file %s to COFF object file %s ..", binarypath, outputpath) - - -- do dump - if binutils.bin2coff then - binutils.bin2coff(binarypath, outputpath, opt) - else - raise("bin2obj: binutils.bin2coff not available (C implementation not compiled)") - end - - -- trace - cprint("${bright}%s generated!", outputpath) -end + binarypath = path.absolute(binarypath) + outputpath = path.absolute(outputpath) + assert(os.isfile(binarypath), "%s not found!", binarypath) -function _do_bin2obj_elf(binarypath, outputpath, opt) -- get filename from binary path (with extension, dots replaced with underscores) local filename = path.filename(binarypath) -- replace dots with underscores for symbol name (e.g., data.bin -> data_bin) local basename = filename:gsub("%.", "_") - - -- prepare opt - opt = opt or {} opt.basename = basename - -- trace - print("converting binary file %s to ELF object file %s ..", binarypath, outputpath) - - -- do dump - if binutils.bin2elf then - binutils.bin2elf(binarypath, outputpath, opt) - else - raise("bin2obj: binutils.bin2elf not available (C implementation not compiled)") + -- validate format + local format = opt.format + if format then + format = format:lower() + if format ~= "coff" and format ~= "elf" and format ~= "macho" then + raise("bin2obj: unsupported format '%s' (supported: coff, elf, macho)", format) + end end -- trace - cprint("${bright}%s generated!", outputpath) -end - -function _do_bin2obj_macho(binarypath, outputpath, opt) - -- get filename from binary path (with extension, dots replaced with underscores) - local filename = path.filename(binarypath) - -- replace dots with underscores for symbol name (e.g., data.bin -> data_bin) - local basename = filename:gsub("%.", "_") - - -- prepare opt - opt = opt or {} - opt.basename = basename - - -- trace - print("converting binary file %s to Mach-O object file %s ..", binarypath, outputpath) + print("converting binary file %s to %s object file %s ..", binarypath, format or "coff", outputpath) - -- do dump - if binutils.bin2macho then - binutils.bin2macho(binarypath, outputpath, opt) - else - raise("bin2obj: binutils.bin2macho not available (C implementation not compiled)") - end + -- do conversion + binutils.bin2obj(binarypath, outputpath, opt) -- trace cprint("${bright}%s generated!", outputpath) end -function _do_bin2obj(binarypath, outputpath, opt) - - -- init source directory and options - opt = opt or {} - binarypath = path.absolute(binarypath) - outputpath = path.absolute(outputpath) - assert(os.isfile(binarypath), "%s not found!", binarypath) - - -- get format (default: coff) - local format = opt.format or "coff" - format = format:lower() - - -- validate format - if format ~= "coff" and format ~= "elf" and format ~= "macho" then - raise("bin2obj: unsupported format '%s' (supported: coff, elf, macho)", format) - end - - -- do conversion based on format - if format == "coff" then - _do_bin2obj_coff(binarypath, outputpath, opt) - elseif format == "elf" then - _do_bin2obj_elf(binarypath, outputpath, opt) - elseif format == "macho" then - _do_bin2obj_macho(binarypath, outputpath, opt) - end -end - function main(...) -- parse arguments diff --git a/xmake/modules/utils/binary/readsyms.lua b/xmake/modules/utils/binary/readsyms.lua new file mode 100644 index 000000000..6058e8121 --- /dev/null +++ b/xmake/modules/utils/binary/readsyms.lua @@ -0,0 +1,106 @@ +--!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) + + return binutils.readsyms(binaryfile) +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 + -- calculate column widths for alignment + local max_name_len = 0 + local max_type_len = 0 + + for i, sym in ipairs(symbols) do + if sym.name then + max_name_len = math.max(max_name_len, #sym.name) + end + if sym.type then + max_type_len = math.max(max_type_len, #sym.type) + end + end + + -- calculate column widths + local type_width = math.max(max_type_len, 4) + local name_width = math.max(max_name_len, 4) + + -- print header + print("") + print("Symbols:") + local header_format = " %-" .. type_width .. "s %s" + print(string.format(header_format, "TYPE", "NAME")) + print(string.rep("-", 80)) + + -- print symbols + local format_str = " %-" .. type_width .. "s %s" + + for i, sym in ipairs(symbols) do + local type_str = sym.type or "unknown" + local name_str = sym.name or "" + + print(string.format(format_str, type_str, name_str)) + end + print("") + cprint("${bright}%d symbols found!", #symbols) + else + print("") + cprint("${bright}No symbols found!") + end +end + +-- read symbols from object file(s) (auto-detect format: ELF, COFF, Mach-O) +-- +-- @param binaryfiles the object file path or table of object files (required) +-- @return the symbols table (all symbols from all files if table is provided) +function main(binaryfiles) + assert(binaryfiles, "usage: xmake l utils.binary.readsyms <binaryfile> or readsyms(binaryfiles)") + + local all_symbols = {} + if type(binaryfiles) == "string" then + return _get_symbols(binaryfiles) + else + for _, binaryfile in ipairs(binaryfiles) do + local symbols = _get_symbols(binaryfile) + if symbols then + for _, sym in ipairs(symbols) do + table.insert(all_symbols, sym) + end + end + end + return all_symbols + end +end + |
