diff options
| author | ruki <[email protected]> | 2025-12-11 23:42:48 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2025-12-12 09:00:44 +0800 |
| commit | 65b4b2932a7c2fd6dab3fe60597055d1440bddc5 (patch) | |
| tree | 440e1f56021740a82f79cb6ebac277645f7cf91c /xmake | |
| parent | b0eb30a4becb5b6d925aa77418ce30c7f1f79d69 (diff) | |
improve readsyms
Diffstat (limited to 'xmake')
| -rw-r--r-- | xmake/modules/utils/binary/readsyms.lua | 93 | ||||
| -rw-r--r-- | xmake/rules/utils/symbols/export_all/export_all.lua | 62 |
2 files changed, 74 insertions, 81 deletions
diff --git a/xmake/modules/utils/binary/readsyms.lua b/xmake/modules/utils/binary/readsyms.lua index 6058e8121..d8aa8674a 100644 --- a/xmake/modules/utils/binary/readsyms.lua +++ b/xmake/modules/utils/binary/readsyms.lua @@ -38,69 +38,62 @@ end -- -- @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 + local objects = _get_symbols(binaryfile) + if objects and #objects > 0 then + for _, obj in ipairs(objects) do + local symbols = obj.symbols + if symbols and #symbols > 0 then + -- print object file + print("") + cprint("${bright}Object: %s", obj.objectfile) + print(string.rep("-", 80)) - 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 for alignment + local max_name_len = 0 + local max_type_len = 0 - -- calculate column widths - local type_width = math.max(max_type_len, 4) - local name_width = math.max(max_name_len, 4) + 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 - -- print header - print("") - print("Symbols:") - local header_format = " %-" .. type_width .. "s %s" - print(string.format(header_format, "TYPE", "NAME")) - print(string.rep("-", 80)) + -- calculate column widths + local type_width = math.max(max_type_len, 4) + local name_width = math.max(max_name_len, 4) - -- print symbols - local format_str = " %-" .. type_width .. "s %s" + -- print header + local header_format = " %-" .. type_width .. "s %s" + print(string.format(header_format, "TYPE", "NAME")) - for i, sym in ipairs(symbols) do - local type_str = sym.type or "unknown" - local name_str = sym.name or "" + -- print symbols + local format_str = " %-" .. type_width .. "s %s" - print(string.format(format_str, type_str, name_str)) + 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) + end 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) +-- read symbols from object file (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 +-- @param binaryfile the object file path (required) +-- @return the symbols table +function main(binaryfile) + assert(binaryfile, "usage: xmake l utils.binary.readsyms <binaryfile>") + return _get_symbols(binaryfile) end diff --git a/xmake/rules/utils/symbols/export_all/export_all.lua b/xmake/rules/utils/symbols/export_all/export_all.lua index 6e077aaf3..fa75038d1 100644 --- a/xmake/rules/utils/symbols/export_all/export_all.lua +++ b/xmake/rules/utils/symbols/export_all/export_all.lua @@ -158,38 +158,38 @@ function _get_allsymbols_by_readsyms(target, opt) _get_sourcefiles_map(target, sourcefiles_map) end local objectfiles = target:objectfiles() - local symbols = readsyms(objectfiles) - if symbols then - for _, sym in ipairs(symbols) do - if sym.name and sym.type then - -- only export function symbols (T/t) for DLL exports - -- skip data (D/d), bss (B/b), other sections (S/s), and undefined (U) symbols - if sym.type == "T" or sym.type == "t" then - local symbol = sym.name - -- we need ignore DllMain, https://github.com/xmake-io/xmake/issues/3992 - if target:is_arch("x86") and symbol:startswith("_") and not symbol:startswith("__") and not symbol:startswith("_DllMain@") then - symbol = symbol:sub(2) - end - if export_filter then - -- find sourcefile for this symbol (approximate match) - local sourcefile = nil - for objfile, srcfile in pairs(sourcefiles_map) do - if objfile:find(path.basename(symbol), 1, true) then - sourcefile = srcfile - break - end - end - if export_filter(symbol, {sourcefile = sourcefile}) then - allsymbols:insert(symbol) - end - elseif not symbol:startswith("__") then - if export_classes or not symbol:startswith("?") then - if export_classes then - if not symbol:startswith("??_G") and not symbol:startswith("??_E") then - allsymbols:insert(symbol) + for _, objectfile in ipairs(objectfiles) do + local objects = readsyms(objectfile) + if objects then + local sourcefile = sourcefiles_map[objectfile] + for _, obj in ipairs(objects) do + local symbols = obj.symbols + if symbols then + for _, sym in ipairs(symbols) do + if sym.name and sym.type then + -- only export function symbols (T/t) for DLL exports + -- skip data (D/d), bss (B/b), other sections (S/s), and undefined (U) symbols + if sym.type == "T" or sym.type == "t" then + local symbol = sym.name + -- we need ignore DllMain, https://github.com/xmake-io/xmake/issues/3992 + if target:is_arch("x86") and symbol:startswith("_") and not symbol:startswith("__") and not symbol:startswith("_DllMain@") then + symbol = symbol:sub(2) + end + if export_filter then + if export_filter(symbol, {sourcefile = sourcefile}) then + allsymbols:insert(symbol) + end + elseif not symbol:startswith("__") then + if export_classes or not symbol:startswith("?") then + if export_classes then + if not symbol:startswith("??_G") and not symbol:startswith("??_E") then + allsymbols:insert(symbol) + end + else + allsymbols:insert(symbol) + end + end end - else - allsymbols:insert(symbol) end end end |
