From ba293bc4b8840da34220e1f54313d531d18dde8e Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 9 Dec 2025 00:29:51 +0800 Subject: add binary readsyms --- xmake/core/base/binutils.lua | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'xmake/core/base/binutils.lua') 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 -- cgit v1.3.1 From 102507b96dab0403592c07c8760644f08ed66dff Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 9 Dec 2025 22:32:28 +0800 Subject: improve bin2obj --- xmake/core/base/binutils.lua | 49 +++++++++--- .../sandbox/modules/import/core/base/binutils.lua | 23 +----- xmake/modules/utils/binary/bin2obj.lua | 93 +++------------------- 3 files changed, 53 insertions(+), 112 deletions(-) (limited to 'xmake/core/base/binutils.lua') diff --git a/xmake/core/base/binutils.lua b/xmake/core/base/binutils.lua index 8047046b2..995f0d0a7 100644 --- a/xmake/core/base/binutils.lua +++ b/xmake/core/base/binutils.lua @@ -39,23 +39,46 @@ 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 + return nil, "bin2obj: format is required (coff, elf, or macho)" + 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) -end -- read symbols from object file (auto-detect format: COFF, ELF, or Mach-O) function binutils.readsyms(binaryfile) diff --git a/xmake/core/sandbox/modules/import/core/base/binutils.lua b/xmake/core/sandbox/modules/import/core/base/binutils.lua index 9e371d777..b9a502f8d 100644 --- a/xmake/core/sandbox/modules/import/core/base/binutils.lua +++ b/xmake/core/sandbox/modules/import/core/base/binutils.lua @@ -33,29 +33,14 @@ function sandbox_core_base_binutils.bin2c(binaryfile, outputfile, opt) end end --- generate COFF object file from the binary file -function sandbox_core_base_binutils.bin2coff(binaryfile, outputfile, opt) - local ok, errors = binutils.bin2coff(binaryfile, outputfile, opt) +-- generate object file from the binary file +function sandbox_core_base_binutils.bin2obj(binaryfile, outputfile, opt) + local ok, errors = binutils.bin2obj(binaryfile, outputfile, opt) if not ok then - raise("bin2coff: %s", errors or "unknown errors") + raise("bin2obj: %s", errors or "unknown errors") end end --- generate Mach-O object file from the binary file -function sandbox_core_base_binutils.bin2macho(binaryfile, outputfile, opt) - local ok, errors = binutils.bin2macho(binaryfile, outputfile, opt) - if not ok then - raise("bin2macho: %s", errors or "unknown errors") - end -end - --- generate ELF object file from the binary file -function sandbox_core_base_binutils.bin2elf(binaryfile, outputfile, opt) - local ok, errors = binutils.bin2elf(binaryfile, outputfile, opt) - if not ok then - raise("bin2elf: %s", errors or "unknown errors") - end -end -- read symbols from object file (auto-detect format: ELF, COFF, Mach-O) function sandbox_core_base_binutils.readsyms(binaryfile) diff --git a/xmake/modules/utils/binary/bin2obj.lua b/xmake/modules/utils/binary/bin2obj.lua index 10d396551..7870e2544 100644 --- a/xmake/modules/utils/binary/bin2obj.lua +++ b/xmake/modules/utils/binary/bin2obj.lua @@ -34,86 +34,19 @@ 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 - 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 - -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 +function _do_bin2obj(binarypath, outputpath, opt) + -- init source directory and options 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)") - 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_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) - - -- do dump - if binutils.bin2macho then - binutils.bin2macho(binarypath, outputpath, opt) - else - raise("bin2obj: binutils.bin2macho not available (C implementation not compiled)") - end - - -- 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() @@ -123,14 +56,14 @@ function _do_bin2obj(binarypath, outputpath, opt) 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 + -- trace + print("converting binary file %s to %s object file %s ..", binarypath, format, outputpath) + + -- do conversion + binutils.bin2obj(binarypath, outputpath, opt) + + -- trace + cprint("${bright}%s generated!", outputpath) end function main(...) -- cgit v1.3.1 From 5931e48282914758fd7c4eeaf9ca05a16521692b Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 9 Dec 2025 22:34:36 +0800 Subject: fix compile error --- core/src/xmake/binutils/elf/prefix.h | 2 +- xmake/core/base/binutils.lua | 13 ++++++++++++- xmake/modules/utils/binary/bin2obj.lua | 14 +++++++------- 3 files changed, 20 insertions(+), 9 deletions(-) (limited to 'xmake/core/base/binutils.lua') diff --git a/core/src/xmake/binutils/elf/prefix.h b/core/src/xmake/binutils/elf/prefix.h index d79fe3e11..bd3cc9129 100644 --- a/core/src/xmake/binutils/elf/prefix.h +++ b/core/src/xmake/binutils/elf/prefix.h @@ -278,7 +278,7 @@ static __tb_inline__ tb_bool_t xm_binutils_elf_is_64bit(tb_char_t const *arch) { * @param offset the string offset * @return the string (static buffer, valid until next call) */ -static __tb_inline__ tb_bool_t xm_binutils_elf_read_string(tb_stream_ref_t istream, tb_uint32_t strtab_offset, tb_uint32_t offset, tb_char_t *name, tb_size_t name_size) { +static __tb_inline__ tb_bool_t xm_binutils_elf_read_string(tb_stream_ref_t istream, tb_uint64_t strtab_offset, tb_uint32_t offset, tb_char_t *name, tb_size_t name_size) { tb_assert_and_check_return_val(istream && name && name_size > 0, tb_false); tb_hize_t saved_pos = tb_stream_offset(istream); diff --git a/xmake/core/base/binutils.lua b/xmake/core/base/binutils.lua index 995f0d0a7..77b17dc92 100644 --- a/xmake/core/base/binutils.lua +++ b/xmake/core/base/binutils.lua @@ -21,6 +21,9 @@ -- 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 @@ -55,7 +58,15 @@ function binutils.bin2obj(binaryfile, outputfile, opt) opt = opt or {} local format = opt.format if not format then - return nil, "bin2obj: format is required (coff, elf, or macho)" + -- 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() diff --git a/xmake/modules/utils/binary/bin2obj.lua b/xmake/modules/utils/binary/bin2obj.lua index 7870e2544..95e705ca6 100644 --- a/xmake/modules/utils/binary/bin2obj.lua +++ b/xmake/modules/utils/binary/bin2obj.lua @@ -47,17 +47,17 @@ function _do_bin2obj(binarypath, outputpath, opt) local basename = filename:gsub("%.", "_") opt.basename = basename - -- 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) + 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 - print("converting binary file %s to %s object file %s ..", binarypath, format, outputpath) + print("converting binary file %s to %s object file %s ..", binarypath, format or "coff", outputpath) -- do conversion binutils.bin2obj(binarypath, outputpath, opt) -- cgit v1.3.1