diff options
| author | ruki <[email protected]> | 2025-12-07 17:47:01 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2025-12-07 17:47:01 +0800 |
| commit | f4cd87b0135d84932f186bc068a5232912ee43b9 (patch) | |
| tree | 1e1bba0e8a5160cc4a9cd8f5de0bdef15ea2ba64 | |
| parent | 35aeaac3f956f26f188ebab40c443b64d2cb2f95 (diff) | |
update bin2c
| -rw-r--r-- | core/src/xmake/binutils/bin2c.c (renamed from core/src/xmake/utils/bin2c.c) | 28 | ||||
| -rw-r--r-- | core/src/xmake/binutils/bin2coff.c (renamed from core/src/xmake/utils/bin2coff.c) | 34 | ||||
| -rw-r--r-- | core/src/xmake/binutils/bin2elf.c (renamed from core/src/xmake/utils/bin2elf.c) | 26 | ||||
| -rw-r--r-- | core/src/xmake/binutils/bin2macho.c (renamed from core/src/xmake/utils/bin2macho.c) | 62 | ||||
| -rw-r--r-- | core/src/xmake/binutils/prefix.h (renamed from core/src/xmake/utils/prefix.h) | 7 | ||||
| -rw-r--r-- | core/src/xmake/engine.c | 26 | ||||
| -rwxr-xr-x | core/src/xmake/xmake.sh | 2 | ||||
| -rw-r--r-- | xmake/core/base/binutils.lua | 61 | ||||
| -rw-r--r-- | xmake/core/base/utils.lua | 8 | ||||
| -rw-r--r-- | xmake/core/sandbox/modules/import/core/base/binutils.lua | 62 | ||||
| -rw-r--r-- | xmake/core/sandbox/modules/utils.lua | 39 | ||||
| -rw-r--r-- | xmake/modules/utils/binary/bin2c.lua | 5 | ||||
| -rw-r--r-- | xmake/modules/utils/binary/bin2obj.lua | 67 |
13 files changed, 240 insertions, 187 deletions
diff --git a/core/src/xmake/utils/bin2c.c b/core/src/xmake/binutils/bin2c.c index a1ca2a25d..b5054c55f 100644 --- a/core/src/xmake/utils/bin2c.c +++ b/core/src/xmake/binutils/bin2c.c @@ -43,21 +43,21 @@ */ // optimized hex conversion table -static tb_char_t const *xm_utils_bin2c_digits = "0123456789ABCDEF"; +static tb_char_t const *xm_binutils_bin2c_digits = "0123456789ABCDEF"; // inline hex conversion for better performance -static __tb_inline__ tb_void_t xm_utils_bin2c_write_hex(tb_char_t *str, tb_byte_t value) { +static __tb_inline__ tb_void_t xm_binutils_bin2c_write_hex(tb_char_t *str, tb_byte_t value) { str[0] = ' '; str[1] = '0'; str[2] = 'x'; - str[3] = xm_utils_bin2c_digits[(value >> 4) & 15]; - str[4] = xm_utils_bin2c_digits[value & 15]; + str[3] = xm_binutils_bin2c_digits[(value >> 4) & 15]; + str[4] = xm_binutils_bin2c_digits[value & 15]; } -static tb_bool_t xm_utils_bin2c_dump(tb_stream_ref_t istream, - tb_stream_ref_t ostream, - tb_int_t linewidth, - tb_bool_t nozeroend) { +static tb_bool_t xm_binutils_bin2c_dump(tb_stream_ref_t istream, + tb_stream_ref_t ostream, + tb_int_t linewidth, + tb_bool_t nozeroend) { tb_bool_t first = tb_true; tb_bool_t zero_pending = tb_false; @@ -81,9 +81,7 @@ static tb_bool_t xm_utils_bin2c_dump(tb_stream_ref_t istream, } else { tb_hong_t left = tb_stream_left(istream); tb_size_t to_read = (tb_size_t)tb_min(left, (tb_hong_t)XM_BIN2C_DATA_SIZE); - if (!to_read) { - break; - } + tb_check_break(to_read); if (!tb_stream_bread(istream, data, to_read)) { break; @@ -138,7 +136,7 @@ static tb_bool_t xm_utils_bin2c_dump(tb_stream_ref_t istream, } // write hex value (inline for performance) - xm_utils_bin2c_write_hex(line + linesize, data[data_pos]); + xm_binutils_bin2c_write_hex(line + linesize, data[data_pos]); linesize += 5; bytes_in_line++; data_pos++; @@ -162,9 +160,9 @@ static tb_bool_t xm_utils_bin2c_dump(tb_stream_ref_t istream, /* generate c/c++ code from the binary file * - * local ok, errors = utils.bin2c(binaryfile, outputfile, linewidth, nozeroend) + * local ok, errors = binutils.bin2c(binaryfile, outputfile, linewidth, nozeroend) */ -tb_int_t xm_utils_bin2c(lua_State *lua) { +tb_int_t xm_binutils_bin2c(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); // get the binaryfile @@ -199,7 +197,7 @@ tb_int_t xm_utils_bin2c(lua_State *lua) { break; } - if (!xm_utils_bin2c_dump(istream, ostream, linewidth, nozeroend)) { + if (!xm_binutils_bin2c_dump(istream, ostream, linewidth, nozeroend)) { lua_pushboolean(lua, tb_false); lua_pushfstring(lua, "bin2c: dump data failed"); break; diff --git a/core/src/xmake/utils/bin2coff.c b/core/src/xmake/binutils/bin2coff.c index 976f18202..686b89fac 100644 --- a/core/src/xmake/utils/bin2coff.c +++ b/core/src/xmake/binutils/bin2coff.c @@ -103,7 +103,7 @@ typedef struct __xm_coff_symbol_tail_t { /* ////////////////////////////////////////////////////////////////////////////////////// * private implementation */ -static tb_uint16_t xm_utils_bin2coff_get_machine(tb_char_t const *arch) { +static tb_uint16_t xm_binutils_bin2coff_get_machine(tb_char_t const *arch) { if (!arch) { return XM_COFF_MACHINE_I386; } @@ -119,7 +119,7 @@ static tb_uint16_t xm_utils_bin2coff_get_machine(tb_char_t const *arch) { return XM_COFF_MACHINE_I386; } -static tb_void_t xm_utils_bin2coff_write_string(tb_stream_ref_t ostream, tb_char_t const *str, tb_size_t len) { +static tb_void_t xm_binutils_bin2coff_write_string(tb_stream_ref_t ostream, tb_char_t const *str, tb_size_t len) { tb_assert_and_check_return(ostream && str); if (len == 0) { len = tb_strlen(str); @@ -127,7 +127,7 @@ static tb_void_t xm_utils_bin2coff_write_string(tb_stream_ref_t ostream, tb_char tb_stream_bwrit(ostream, (tb_byte_t const *)str, len); } -static tb_void_t xm_utils_bin2coff_write_padding(tb_stream_ref_t ostream, tb_size_t count) { +static tb_void_t xm_binutils_bin2coff_write_padding(tb_stream_ref_t ostream, tb_size_t count) { tb_assert_and_check_return(ostream); tb_byte_t zero = 0; while (count-- > 0) { @@ -135,14 +135,14 @@ static tb_void_t xm_utils_bin2coff_write_padding(tb_stream_ref_t ostream, tb_siz } } -static tb_void_t xm_utils_bin2coff_write_symbol_name(tb_stream_ref_t ostream, tb_char_t const *name, tb_uint32_t *strtab_offset) { +static tb_void_t xm_binutils_bin2coff_write_symbol_name(tb_stream_ref_t ostream, tb_char_t const *name, tb_uint32_t *strtab_offset) { tb_assert_and_check_return(ostream && name && strtab_offset); tb_size_t len = tb_strlen(name); if (len <= 8) { // short name: store directly in symbol name field - xm_utils_bin2coff_write_string(ostream, name, len); + xm_binutils_bin2coff_write_string(ostream, name, len); if (len < 8) { - xm_utils_bin2coff_write_padding(ostream, 8 - len); + xm_binutils_bin2coff_write_padding(ostream, 8 - len); } } else { // long name: store offset in string table @@ -153,7 +153,7 @@ static tb_void_t xm_utils_bin2coff_write_symbol_name(tb_stream_ref_t ostream, tb } } -static tb_bool_t xm_utils_bin2coff_dump(tb_stream_ref_t istream, +static tb_bool_t xm_binutils_bin2coff_dump(tb_stream_ref_t istream, tb_stream_ref_t ostream, tb_char_t const *symbol_prefix, tb_char_t const *arch, @@ -223,7 +223,7 @@ static tb_bool_t xm_utils_bin2coff_dump(tb_stream_ref_t istream, // write COFF header xm_coff_header_t header; tb_memset(&header, 0, sizeof(header)); - header.machine = xm_utils_bin2coff_get_machine(arch); + header.machine = xm_binutils_bin2coff_get_machine(arch); header.nsects = 1; header.time = 0; header.symtabofs = symbol_table_ofs; @@ -252,7 +252,7 @@ static tb_bool_t xm_utils_bin2coff_dump(tb_stream_ref_t istream, } // write section data - if (!xm_utils_stream_copy(istream, ostream, filesize)) { + if (!xm_binutils_stream_copy(istream, ostream, filesize)) { return tb_false; } // append null terminator if zeroend is true @@ -266,7 +266,7 @@ static tb_bool_t xm_utils_bin2coff_dump(tb_stream_ref_t istream, // align to 4 bytes tb_uint32_t padding = (4 - (section_data_size & 3)) & 3; if (padding > 0) { - xm_utils_bin2coff_write_padding(ostream, padding); + xm_binutils_bin2coff_write_padding(ostream, padding); } // write symbol table @@ -294,7 +294,7 @@ static tb_bool_t xm_utils_bin2coff_dump(tb_stream_ref_t istream, // symbol 1: _binary_xxx_start tb_uint32_t strtab_offset = 4; // start after size field - xm_utils_bin2coff_write_symbol_name(ostream, symbol_start, &strtab_offset); + xm_binutils_bin2coff_write_symbol_name(ostream, symbol_start, &strtab_offset); xm_coff_symbol_tail_t sym_start_tail; tb_memset(&sym_start_tail, 0, sizeof(sym_start_tail)); sym_start_tail.value = 0; @@ -305,7 +305,7 @@ static tb_bool_t xm_utils_bin2coff_dump(tb_stream_ref_t istream, } // symbol 2: _binary_xxx_end - xm_utils_bin2coff_write_symbol_name(ostream, symbol_end, &strtab_offset); + xm_binutils_bin2coff_write_symbol_name(ostream, symbol_end, &strtab_offset); xm_coff_symbol_tail_t sym_end_tail; tb_memset(&sym_end_tail, 0, sizeof(sym_end_tail)); sym_end_tail.value = datasize; @@ -318,12 +318,12 @@ static tb_bool_t xm_utils_bin2coff_dump(tb_stream_ref_t istream, // write string table tb_stream_bwrit(ostream, (tb_byte_t const *)&string_table_size, 4); if (start_len > 8) { - xm_utils_bin2coff_write_string(ostream, symbol_start, start_len); + xm_binutils_bin2coff_write_string(ostream, symbol_start, start_len); tb_byte_t null = 0; tb_stream_bwrit(ostream, &null, 1); } if (end_len > 8) { - xm_utils_bin2coff_write_string(ostream, symbol_end, end_len); + xm_binutils_bin2coff_write_string(ostream, symbol_end, end_len); tb_byte_t null = 0; tb_stream_bwrit(ostream, &null, 1); } @@ -337,9 +337,9 @@ static tb_bool_t xm_utils_bin2coff_dump(tb_stream_ref_t istream, /* generate COFF object file from binary file * - * local ok, errors = utils.bin2coff(binaryfile, outputfile, symbol_prefix, arch, basename) + * local ok, errors = binutils.bin2coff(binaryfile, outputfile, symbol_prefix, arch, basename) */ -tb_int_t xm_utils_bin2coff(lua_State *lua) { +tb_int_t xm_binutils_bin2coff(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); // get the binaryfile @@ -380,7 +380,7 @@ tb_int_t xm_utils_bin2coff(lua_State *lua) { break; } - if (!xm_utils_bin2coff_dump(istream, ostream, symbol_prefix, arch, basename, zeroend)) { + if (!xm_binutils_bin2coff_dump(istream, ostream, symbol_prefix, arch, basename, zeroend)) { lua_pushboolean(lua, tb_false); lua_pushfstring(lua, "bin2coff: dump data failed"); break; diff --git a/core/src/xmake/utils/bin2elf.c b/core/src/xmake/binutils/bin2elf.c index 7fe8e5576..bb2507a34 100644 --- a/core/src/xmake/utils/bin2elf.c +++ b/core/src/xmake/binutils/bin2elf.c @@ -135,7 +135,7 @@ typedef struct __xm_elf64_symbol_t { /* ////////////////////////////////////////////////////////////////////////////////////// * private implementation */ -static tb_uint16_t xm_utils_bin2elf_get_machine(tb_char_t const *arch) { +static tb_uint16_t xm_binutils_bin2elf_get_machine(tb_char_t const *arch) { if (!arch) { return XM_ELF_MACHINE_X86_64; } @@ -153,7 +153,7 @@ static tb_uint16_t xm_utils_bin2elf_get_machine(tb_char_t const *arch) { return XM_ELF_MACHINE_X86_64; } -static tb_bool_t xm_utils_bin2elf_is_64bit(tb_char_t const *arch) { +static tb_bool_t xm_binutils_bin2elf_is_64bit(tb_char_t const *arch) { if (!arch) { return tb_true; } @@ -165,7 +165,7 @@ static tb_bool_t xm_utils_bin2elf_is_64bit(tb_char_t const *arch) { return tb_false; } -static tb_bool_t xm_utils_bin2elf_dump_32(tb_stream_ref_t istream, +static tb_bool_t xm_binutils_bin2elf_dump_32(tb_stream_ref_t istream, tb_stream_ref_t ostream, tb_char_t const *symbol_prefix, tb_char_t const *arch, @@ -256,7 +256,7 @@ static tb_bool_t xm_utils_bin2elf_dump_32(tb_stream_ref_t istream, header.e_ident[6] = 1; // EV_CURRENT header.e_ident[7] = 0; // ELFOSABI_SYSV header.e_type = 1; // ET_REL - header.e_machine = xm_utils_bin2elf_get_machine(arch); + header.e_machine = xm_binutils_bin2elf_get_machine(arch); header.e_version = 1; header.e_shoff = section_headers_ofs; header.e_ehsize = header_size; @@ -340,7 +340,7 @@ static tb_bool_t xm_utils_bin2elf_dump_32(tb_stream_ref_t istream, } // write .rodata section data - if (!xm_utils_stream_copy(istream, ostream, filesize)) { + if (!xm_binutils_stream_copy(istream, ostream, filesize)) { return tb_false; } // append null terminator if zeroend is true @@ -469,7 +469,7 @@ static tb_bool_t xm_utils_bin2elf_dump_32(tb_stream_ref_t istream, return tb_true; } -static tb_bool_t xm_utils_bin2elf_dump_64(tb_stream_ref_t istream, +static tb_bool_t xm_binutils_bin2elf_dump_64(tb_stream_ref_t istream, tb_stream_ref_t ostream, tb_char_t const *symbol_prefix, tb_char_t const *arch, @@ -560,7 +560,7 @@ static tb_bool_t xm_utils_bin2elf_dump_64(tb_stream_ref_t istream, header.e_ident[6] = 1; // EV_CURRENT header.e_ident[7] = 0; // ELFOSABI_SYSV header.e_type = 1; // ET_REL - header.e_machine = xm_utils_bin2elf_get_machine(arch); + header.e_machine = xm_binutils_bin2elf_get_machine(arch); header.e_version = 1; header.e_shoff = section_headers_ofs; header.e_ehsize = header_size; @@ -644,7 +644,7 @@ static tb_bool_t xm_utils_bin2elf_dump_64(tb_stream_ref_t istream, } // write .rodata section data - if (!xm_utils_stream_copy(istream, ostream, filesize)) { + if (!xm_binutils_stream_copy(istream, ostream, filesize)) { return tb_false; } // append null terminator if zeroend is true @@ -779,9 +779,9 @@ static tb_bool_t xm_utils_bin2elf_dump_64(tb_stream_ref_t istream, /* generate ELF object file from binary file * - * local ok, errors = utils.bin2elf(binaryfile, outputfile, symbol_prefix, arch, basename, zeroend) + * local ok, errors = binutils.bin2elf(binaryfile, outputfile, symbol_prefix, arch, basename, zeroend) */ -tb_int_t xm_utils_bin2elf(lua_State *lua) { +tb_int_t xm_binutils_bin2elf(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); // get the binaryfile @@ -823,15 +823,15 @@ tb_int_t xm_utils_bin2elf(lua_State *lua) { } // choose 32-bit or 64-bit ELF based on architecture - tb_bool_t is_64bit = xm_utils_bin2elf_is_64bit(arch); + tb_bool_t is_64bit = xm_binutils_bin2elf_is_64bit(arch); if (is_64bit) { - if (!xm_utils_bin2elf_dump_64(istream, ostream, symbol_prefix, arch, basename, zeroend)) { + if (!xm_binutils_bin2elf_dump_64(istream, ostream, symbol_prefix, arch, basename, zeroend)) { lua_pushboolean(lua, tb_false); lua_pushfstring(lua, "bin2elf: dump data failed"); break; } } else { - if (!xm_utils_bin2elf_dump_32(istream, ostream, symbol_prefix, arch, basename, zeroend)) { + if (!xm_binutils_bin2elf_dump_32(istream, ostream, symbol_prefix, arch, basename, zeroend)) { lua_pushboolean(lua, tb_false); lua_pushfstring(lua, "bin2elf: dump data failed"); break; diff --git a/core/src/xmake/utils/bin2macho.c b/core/src/xmake/binutils/bin2macho.c index 04733e1b2..5add65d6c 100644 --- a/core/src/xmake/utils/bin2macho.c +++ b/core/src/xmake/binutils/bin2macho.c @@ -191,7 +191,7 @@ typedef struct __xm_macho_nlist_64_t { /* ////////////////////////////////////////////////////////////////////////////////////// * private implementation */ -static tb_uint32_t xm_utils_bin2macho_get_cputype(tb_char_t const *arch) { +static tb_uint32_t xm_binutils_bin2macho_get_cputype(tb_char_t const *arch) { if (!arch) { return XM_MACHO_CPU_TYPE_X86_64; } @@ -207,7 +207,7 @@ static tb_uint32_t xm_utils_bin2macho_get_cputype(tb_char_t const *arch) { return XM_MACHO_CPU_TYPE_X86_64; } -static tb_uint32_t xm_utils_bin2macho_get_cpusubtype(tb_char_t const *arch) { +static tb_uint32_t xm_binutils_bin2macho_get_cpusubtype(tb_char_t const *arch) { if (!arch) { return XM_MACHO_CPU_SUBTYPE_X86_64; } @@ -223,7 +223,7 @@ static tb_uint32_t xm_utils_bin2macho_get_cpusubtype(tb_char_t const *arch) { return XM_MACHO_CPU_SUBTYPE_X86_64; } -static tb_bool_t xm_utils_bin2macho_is_64bit(tb_char_t const *arch) { +static tb_bool_t xm_binutils_bin2macho_is_64bit(tb_char_t const *arch) { if (!arch) { return tb_true; } @@ -239,11 +239,11 @@ static tb_bool_t xm_utils_bin2macho_is_64bit(tb_char_t const *arch) { return tb_true; } -static tb_uint32_t xm_utils_bin2macho_align(tb_uint32_t value, tb_uint32_t align) { +static tb_uint32_t xm_binutils_bin2macho_align(tb_uint32_t value, tb_uint32_t align) { return ((value + align - 1) & ~(align - 1)); } -static tb_uint32_t xm_utils_bin2macho_get_platform(tb_char_t const *platform) { +static tb_uint32_t xm_binutils_bin2macho_get_platform(tb_char_t const *platform) { if (!platform) { return XM_MACHO_PLATFORM_MACOS; } @@ -261,7 +261,7 @@ static tb_uint32_t xm_utils_bin2macho_get_platform(tb_char_t const *platform) { // parse version string (e.g., "10.0" or "18.2") to Mach-O format (0x000a0000) // format: (major << 16) | (minor << 8) | patch -static tb_uint32_t xm_utils_bin2macho_parse_version(tb_char_t const *version_str) { +static tb_uint32_t xm_binutils_bin2macho_parse_version(tb_char_t const *version_str) { if (!version_str || !version_str[0]) { return 0x000a0000; // default: 10.0.0 } @@ -293,7 +293,7 @@ static tb_uint32_t xm_utils_bin2macho_parse_version(tb_char_t const *version_str return (major << 16) | (minor << 8) | patch; } -static tb_bool_t xm_utils_bin2macho_dump_64(tb_stream_ref_t istream, +static tb_bool_t xm_binutils_bin2macho_dump_64(tb_stream_ref_t istream, tb_stream_ref_t ostream, tb_char_t const *symbol_prefix, tb_char_t const *plat, @@ -354,10 +354,10 @@ static tb_bool_t xm_utils_bin2macho_dump_64(tb_stream_ref_t istream, tb_uint32_t symtab_cmd_size = sizeof(xm_macho_symtab_command_t); tb_uint32_t build_version_cmd_size = sizeof(xm_macho_build_version_command_t); tb_uint32_t segment_cmd_total_size = segment_cmd_size + section_size; - tb_uint32_t data_offset = xm_utils_bin2macho_align(header_size + segment_cmd_total_size + symtab_cmd_size + build_version_cmd_size, 8); + tb_uint32_t data_offset = xm_binutils_bin2macho_align(header_size + segment_cmd_total_size + symtab_cmd_size + build_version_cmd_size, 8); tb_uint32_t data_size = datasize; tb_uint32_t data_end_offset = data_offset + data_size; - tb_uint32_t symtab_offset = xm_utils_bin2macho_align(data_end_offset, 8); + tb_uint32_t symtab_offset = xm_binutils_bin2macho_align(data_end_offset, 8); tb_uint32_t nlist_size = sizeof(xm_macho_nlist_64_t); tb_uint32_t nlist_count = 2; // start, end tb_uint32_t strtab_offset = symtab_offset + nlist_size * nlist_count; @@ -366,14 +366,14 @@ static tb_bool_t xm_utils_bin2macho_dump_64(tb_stream_ref_t istream, tb_size_t end_len = tb_strlen(symbol_end); strtab_size += (tb_uint32_t)(start_len + 1); strtab_size += (tb_uint32_t)(end_len + 1); - strtab_size = xm_utils_bin2macho_align(strtab_size, 8); + strtab_size = xm_binutils_bin2macho_align(strtab_size, 8); // write Mach-O header xm_macho_header_64_t header; tb_memset(&header, 0, sizeof(header)); header.magic = XM_MACHO_MAGIC_64; - header.cputype = xm_utils_bin2macho_get_cputype(arch); - header.cpusubtype = xm_utils_bin2macho_get_cpusubtype(arch); + header.cputype = xm_binutils_bin2macho_get_cputype(arch); + header.cpusubtype = xm_binutils_bin2macho_get_cpusubtype(arch); header.filetype = XM_MACHO_FILE_TYPE_OBJECT; header.ncmds = 3; // segment + symtab + build_version header.sizeofcmds = segment_cmd_total_size + symtab_cmd_size + build_version_cmd_size; @@ -434,7 +434,7 @@ static tb_bool_t xm_utils_bin2macho_dump_64(tb_stream_ref_t istream, tb_memset(&build_version, 0, sizeof(build_version)); build_version.cmd = XM_MACHO_LC_BUILD_VERSION; build_version.cmdsize = build_version_cmd_size; - build_version.platform = xm_utils_bin2macho_get_platform(plat); + build_version.platform = xm_binutils_bin2macho_get_platform(plat); build_version.minos = minos; build_version.sdk = sdk; build_version.ntools = 0; @@ -454,7 +454,7 @@ static tb_bool_t xm_utils_bin2macho_dump_64(tb_stream_ref_t istream, } // write section data - if (!xm_utils_stream_copy(istream, ostream, filesize)) { + if (!xm_binutils_stream_copy(istream, ostream, filesize)) { return tb_false; } // append null terminator if zeroend is true @@ -538,7 +538,7 @@ static tb_bool_t xm_utils_bin2macho_dump_64(tb_stream_ref_t istream, return tb_true; } -static tb_bool_t xm_utils_bin2macho_dump_32(tb_stream_ref_t istream, +static tb_bool_t xm_binutils_bin2macho_dump_32(tb_stream_ref_t istream, tb_stream_ref_t ostream, tb_char_t const *symbol_prefix, tb_char_t const *plat, @@ -599,10 +599,10 @@ static tb_bool_t xm_utils_bin2macho_dump_32(tb_stream_ref_t istream, tb_uint32_t symtab_cmd_size = sizeof(xm_macho_symtab_command_t); tb_uint32_t build_version_cmd_size = sizeof(xm_macho_build_version_command_t); tb_uint32_t segment_cmd_total_size = segment_cmd_size + section_size; - tb_uint32_t data_offset = xm_utils_bin2macho_align(header_size + segment_cmd_total_size + symtab_cmd_size + build_version_cmd_size, 4); + tb_uint32_t data_offset = xm_binutils_bin2macho_align(header_size + segment_cmd_total_size + symtab_cmd_size + build_version_cmd_size, 4); tb_uint32_t data_size = datasize; tb_uint32_t data_end_offset = data_offset + data_size; - tb_uint32_t symtab_offset = xm_utils_bin2macho_align(data_end_offset, 4); + tb_uint32_t symtab_offset = xm_binutils_bin2macho_align(data_end_offset, 4); tb_uint32_t nlist_size = sizeof(xm_macho_nlist_t); tb_uint32_t nlist_count = 2; // start, end tb_uint32_t strtab_offset = symtab_offset + nlist_size * nlist_count; @@ -611,14 +611,14 @@ static tb_bool_t xm_utils_bin2macho_dump_32(tb_stream_ref_t istream, tb_size_t end_len = tb_strlen(symbol_end); strtab_size += (tb_uint32_t)(start_len + 1); strtab_size += (tb_uint32_t)(end_len + 1); - strtab_size = xm_utils_bin2macho_align(strtab_size, 4); + strtab_size = xm_binutils_bin2macho_align(strtab_size, 4); // write Mach-O header xm_macho_header_t header; tb_memset(&header, 0, sizeof(header)); header.magic = XM_MACHO_MAGIC_32; - header.cputype = xm_utils_bin2macho_get_cputype(arch); - header.cpusubtype = xm_utils_bin2macho_get_cpusubtype(arch); + header.cputype = xm_binutils_bin2macho_get_cputype(arch); + header.cpusubtype = xm_binutils_bin2macho_get_cpusubtype(arch); header.filetype = XM_MACHO_FILE_TYPE_OBJECT; header.ncmds = 3; // segment + symtab + build_version header.sizeofcmds = segment_cmd_total_size + symtab_cmd_size + build_version_cmd_size; @@ -681,7 +681,7 @@ static tb_bool_t xm_utils_bin2macho_dump_32(tb_stream_ref_t istream, tb_memset(&build_version, 0, sizeof(build_version)); build_version.cmd = XM_MACHO_LC_BUILD_VERSION; build_version.cmdsize = build_version_cmd_size; - build_version.platform = xm_utils_bin2macho_get_platform(plat); + build_version.platform = xm_binutils_bin2macho_get_platform(plat); build_version.minos = minos; build_version.sdk = sdk; build_version.ntools = 0; @@ -701,7 +701,7 @@ static tb_bool_t xm_utils_bin2macho_dump_32(tb_stream_ref_t istream, } // write section data - if (!xm_utils_stream_copy(istream, ostream, filesize)) { + if (!xm_binutils_stream_copy(istream, ostream, filesize)) { return tb_false; } // append null terminator if zeroend is true @@ -785,7 +785,7 @@ static tb_bool_t xm_utils_bin2macho_dump_32(tb_stream_ref_t istream, return tb_true; } -static tb_bool_t xm_utils_bin2macho_dump(tb_stream_ref_t istream, +static tb_bool_t xm_binutils_bin2macho_dump(tb_stream_ref_t istream, tb_stream_ref_t ostream, tb_char_t const *symbol_prefix, tb_char_t const *plat, @@ -794,10 +794,10 @@ static tb_bool_t xm_utils_bin2macho_dump(tb_stream_ref_t istream, tb_uint32_t minos, tb_uint32_t sdk, tb_bool_t zeroend) { - if (xm_utils_bin2macho_is_64bit(arch)) { - return xm_utils_bin2macho_dump_64(istream, ostream, symbol_prefix, plat, arch, basename, minos, sdk, zeroend); + if (xm_binutils_bin2macho_is_64bit(arch)) { + return xm_binutils_bin2macho_dump_64(istream, ostream, symbol_prefix, plat, arch, basename, minos, sdk, zeroend); } else { - return xm_utils_bin2macho_dump_32(istream, ostream, symbol_prefix, plat, arch, basename, minos, sdk, zeroend); + return xm_binutils_bin2macho_dump_32(istream, ostream, symbol_prefix, plat, arch, basename, minos, sdk, zeroend); } } @@ -807,9 +807,9 @@ static tb_bool_t xm_utils_bin2macho_dump(tb_stream_ref_t istream, /* generate Mach-O object file from binary file * - * local ok, errors = utils.bin2macho(binaryfile, outputfile, symbol_prefix, plat, arch, basename) + * local ok, errors = binutils.bin2macho(binaryfile, outputfile, symbol_prefix, plat, arch, basename) */ -tb_int_t xm_utils_bin2macho(lua_State *lua) { +tb_int_t xm_binutils_bin2macho(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); // get the binaryfile @@ -834,11 +834,11 @@ tb_int_t xm_utils_bin2macho(lua_State *lua) { // get minos version string (optional) tb_char_t const *minos_str = lua_isstring(lua, 7) ? lua_tostring(lua, 7) : tb_null; - tb_uint32_t minos = xm_utils_bin2macho_parse_version(minos_str); + tb_uint32_t minos = xm_binutils_bin2macho_parse_version(minos_str); // get sdk version string (optional) tb_char_t const *sdk_str = lua_isstring(lua, 8) ? lua_tostring(lua, 8) : tb_null; - tb_uint32_t sdk = xm_utils_bin2macho_parse_version(sdk_str); + tb_uint32_t sdk = xm_binutils_bin2macho_parse_version(sdk_str); // get zeroend (optional, default: false) tb_bool_t zeroend = lua_toboolean(lua, 9); @@ -861,7 +861,7 @@ tb_int_t xm_utils_bin2macho(lua_State *lua) { break; } - if (!xm_utils_bin2macho_dump(istream, ostream, symbol_prefix, plat, arch, basename, minos, sdk, zeroend)) { + if (!xm_binutils_bin2macho_dump(istream, ostream, symbol_prefix, plat, arch, basename, minos, sdk, zeroend)) { lua_pushboolean(lua, tb_false); lua_pushfstring(lua, "bin2macho: dump data failed"); break; diff --git a/core/src/xmake/utils/prefix.h b/core/src/xmake/binutils/prefix.h index 077b4027f..739e220f7 100644 --- a/core/src/xmake/utils/prefix.h +++ b/core/src/xmake/binutils/prefix.h @@ -18,8 +18,8 @@ * @file prefix.h * */ -#ifndef XM_UTILS_PREFIX_H -#define XM_UTILS_PREFIX_H +#ifndef XM_BINUTILS_PREFIX_H +#define XM_BINUTILS_PREFIX_H /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -38,7 +38,7 @@ * * @return tb_true on success, tb_false on failure */ -static __tb_inline__ tb_bool_t xm_utils_stream_copy(tb_stream_ref_t istream, tb_stream_ref_t ostream, tb_hize_t size) { +static __tb_inline__ tb_bool_t xm_binutils_stream_copy(tb_stream_ref_t istream, tb_stream_ref_t ostream, tb_hize_t size) { tb_assert_and_check_return_val(istream && ostream && size > 0, tb_false); tb_byte_t data[TB_STREAM_BLOCK_MAXN]; @@ -67,3 +67,4 @@ static __tb_inline__ tb_bool_t xm_utils_stream_copy(tb_stream_ref_t istream, tb_ } #endif + diff --git a/core/src/xmake/engine.c b/core/src/xmake/engine.c index 71a4d4b95..13e8747e0 100644 --- a/core/src/xmake/engine.c +++ b/core/src/xmake/engine.c @@ -334,11 +334,11 @@ tb_int_t xm_tty_term_mode(lua_State *lua); // the package functions tb_int_t xm_package_loadxmi(lua_State *lua); -// the utils functions -tb_int_t xm_utils_bin2c(lua_State *lua); -tb_int_t xm_utils_bin2coff(lua_State *lua); -tb_int_t xm_utils_bin2macho(lua_State *lua); -tb_int_t xm_utils_bin2elf(lua_State *lua); +// the binutils functions +tb_int_t xm_binutils_bin2c(lua_State *lua); +tb_int_t xm_binutils_bin2coff(lua_State *lua); +tb_int_t xm_binutils_bin2macho(lua_State *lua); +tb_int_t xm_binutils_bin2elf(lua_State *lua); #ifdef XM_CONFIG_API_HAVE_CURSES // register curses functions @@ -655,12 +655,12 @@ static luaL_Reg const g_package_functions[] = { { tb_null, tb_null }, }; -// the utils functions -static luaL_Reg const g_utils_functions[] = { - { "bin2c", xm_utils_bin2c }, - { "bin2coff", xm_utils_bin2coff }, - { "bin2macho", xm_utils_bin2macho }, - { "bin2elf", xm_utils_bin2elf }, +// the binutils functions +static luaL_Reg const g_binutils_functions[] = { + { "bin2c", xm_binutils_bin2c }, + { "bin2coff", xm_binutils_bin2coff }, + { "bin2macho", xm_binutils_bin2macho }, + { "bin2elf", xm_binutils_bin2elf }, { tb_null, tb_null }, }; @@ -1561,8 +1561,8 @@ xm_engine_ref_t xm_engine_init(tb_char_t const *name, xm_engine_lni_initalizer_c // bind package functions xm_lua_register(engine->lua, "package", g_package_functions); - // bind utils functions - xm_lua_register(engine->lua, "utils", g_utils_functions); + // bind binutils functions + xm_lua_register(engine->lua, "binutils", g_binutils_functions); // bind thread functions xm_lua_register(engine->lua, "thread", g_thread_functions); diff --git a/core/src/xmake/xmake.sh b/core/src/xmake/xmake.sh index 731d1d783..6cdbb042d 100755 --- a/core/src/xmake/xmake.sh +++ b/core/src/xmake/xmake.sh @@ -71,7 +71,7 @@ target "xmake" add_files "semver/*.c" add_files "string/*.c" add_files "tty/*.c" - add_files "utils/*.c" + add_files "binutils/*.c" add_files "thread/*.c" if is_plat "mingw"; then add_files "winos/*.c" diff --git a/xmake/core/base/binutils.lua b/xmake/core/base/binutils.lua new file mode 100644 index 000000000..b21de2276 --- /dev/null +++ b/xmake/core/base/binutils.lua @@ -0,0 +1,61 @@ +--!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 binutils.lua +-- + +-- define module +local binutils = binutils or {} + +-- 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 + +-- generate c/c++ code from the binary file +function binutils.bin2c(binaryfile, outputfile, opt) + opt = opt or {} + if binutils._bin2c then + return binutils._bin2c(binaryfile, outputfile, opt.linewidth or 32, opt.nozeroend or false) + else + -- fallback to old implementation if C implementation not available + return nil, "bin2c: C implementation not available" + end +end + +-- generate COFF object file from the binary file +function binutils.bin2coff(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 + +-- 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) +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 + +-- return module +return binutils + diff --git a/xmake/core/base/utils.lua b/xmake/core/base/utils.lua index ac9884851..ddf8c127f 100644 --- a/xmake/core/base/utils.lua +++ b/xmake/core/base/utils.lua @@ -30,8 +30,6 @@ local io = require("base/io") local dump = require("base/dump") local text = require("base/text") --- save original interfaces -utils._bin2c = utils._bin2c or utils.bin2c -- dump values function utils.dump(...) @@ -337,11 +335,5 @@ function utils.vtable(data, opt) utils.vprintf(text.table(data, opt)) end --- generate c/c++ code from the binary file -function utils.bin2c(binaryfile, outputfile, opt) - opt = opt or {} - return utils._bin2c(binaryfile, outputfile, opt.linewidth or 32, opt.nozeroend or false) -end - -- return module return utils diff --git a/xmake/core/sandbox/modules/import/core/base/binutils.lua b/xmake/core/sandbox/modules/import/core/base/binutils.lua new file mode 100644 index 000000000..38aad9b73 --- /dev/null +++ b/xmake/core/sandbox/modules/import/core/base/binutils.lua @@ -0,0 +1,62 @@ +--!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 binutils.lua +-- + +-- define module +local sandbox_core_base_binutils = sandbox_core_base_binutils or {} + +-- load modules +local binutils = require("base/binutils") +local raise = require("sandbox/modules/raise") + +-- generate c/c++ code from the binary file +function sandbox_core_base_binutils.bin2c(binaryfile, outputfile, opt) + local ok, errors = binutils.bin2c(binaryfile, outputfile, opt) + if not ok then + raise("bin2c: %s", errors or "unknown errors") + 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) + if not ok then + raise("bin2coff: %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 + +-- return module +return sandbox_core_base_binutils + diff --git a/xmake/core/sandbox/modules/utils.lua b/xmake/core/sandbox/modules/utils.lua index ef1361db9..92977be62 100644 --- a/xmake/core/sandbox/modules/utils.lua +++ b/xmake/core/sandbox/modules/utils.lua @@ -148,45 +148,6 @@ function sandbox_utils.assert(value, format, ...) return value end --- generate c/c++ code from the binary file -if utils._bin2c then - function sandbox_utils.bin2c(binaryfile, outputfile, opt) - local ok, errors = utils.bin2c(binaryfile, outputfile, opt) - if not ok then - os.raise(errors) - end - end -end - --- generate COFF object file from the binary file -if utils.bin2coff then - function sandbox_utils.bin2coff(binaryfile, outputfile, symbol_prefix, arch, basename, zeroend) - local ok, errors = utils.bin2coff(binaryfile, outputfile, symbol_prefix, arch, basename, zeroend) - if not ok then - os.raise(errors) - end - end -end - --- generate Mach-O object file from the binary file -if utils.bin2macho then - function sandbox_utils.bin2macho(binaryfile, outputfile, symbol_prefix, plat, arch, basename, target_minver, xcode_sdkver, zeroend) - local ok, errors = utils.bin2macho(binaryfile, outputfile, symbol_prefix, plat, arch, basename, target_minver, xcode_sdkver, zeroend) - if not ok then - os.raise(errors) - end - end -end - --- generate ELF object file from the binary file -if utils.bin2elf then - function sandbox_utils.bin2elf(binaryfile, outputfile, symbol_prefix, arch, basename, zeroend) - local ok, errors = utils.bin2elf(binaryfile, outputfile, symbol_prefix, arch, basename, zeroend) - if not ok then - os.raise(errors) - end - end -end -- return module return sandbox_utils diff --git a/xmake/modules/utils/binary/bin2c.lua b/xmake/modules/utils/binary/bin2c.lua index ce7336e36..34d9c4e02 100644 --- a/xmake/modules/utils/binary/bin2c.lua +++ b/xmake/modules/utils/binary/bin2c.lua @@ -21,6 +21,7 @@ -- imports import("core.base.bytes") import("core.base.option") +import("core.base.binutils") local options = { {'w', "linewidth", "kv", nil, "Set the line width"}, @@ -98,8 +99,8 @@ function _do_bin2c(binarypath, outputpath, opt) end -- do dump - if utils.bin2c then - utils.bin2c(binarypath, outputpath, opt) + if binutils.bin2c then + binutils.bin2c(binarypath, outputpath, opt) else local binarydata = bytes(io.readfile(binarypath, {encoding = "binary"})) local outputfile = io.open(outputpath, 'w') diff --git a/xmake/modules/utils/binary/bin2obj.lua b/xmake/modules/utils/binary/bin2obj.lua index afb04dc3b..10d396551 100644 --- a/xmake/modules/utils/binary/bin2obj.lua +++ b/xmake/modules/utils/binary/bin2obj.lua @@ -20,6 +20,7 @@ -- imports import("core.base.option") +import("core.base.binutils") local options = { {'i', "binarypath", "kv", nil, "Set the binary file path."}, @@ -34,28 +35,23 @@ local options = { } function _do_bin2obj_coff(binarypath, outputpath, opt) - -- get symbol prefix - local symbol_prefix = opt.symbol_prefix or "_binary_" - - -- get architecture (default: x86_64) - local arch = opt.arch or "x86_64" - - -- get zeroend (default: false) - local zeroend = opt.zeroend or false - -- 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 utils.bin2coff then - utils.bin2coff(binarypath, outputpath, symbol_prefix, arch, basename, zeroend) + if binutils.bin2coff then + binutils.bin2coff(binarypath, outputpath, opt) else - raise("bin2obj: utils.bin2coff not available (C implementation not compiled)") + raise("bin2obj: binutils.bin2coff not available (C implementation not compiled)") end -- trace @@ -63,28 +59,23 @@ function _do_bin2obj_coff(binarypath, outputpath, opt) end function _do_bin2obj_elf(binarypath, outputpath, opt) - -- get symbol prefix - local symbol_prefix = opt.symbol_prefix or "_binary_" - - -- get architecture (default: x86_64) - local arch = opt.arch or "x86_64" - - -- get zeroend (default: false) - local zeroend = opt.zeroend or false - -- 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 utils.bin2elf then - utils.bin2elf(binarypath, outputpath, symbol_prefix, arch, basename, zeroend) + if binutils.bin2elf then + binutils.bin2elf(binarypath, outputpath, opt) else - raise("bin2obj: utils.bin2elf not available (C implementation not compiled)") + raise("bin2obj: binutils.bin2elf not available (C implementation not compiled)") end -- trace @@ -92,37 +83,23 @@ function _do_bin2obj_elf(binarypath, outputpath, opt) end function _do_bin2obj_macho(binarypath, outputpath, opt) - -- get symbol prefix - local symbol_prefix = opt.symbol_prefix or "_binary_" - - -- get platform (default: macosx) - local plat = opt.plat or "macosx" - - -- get architecture (default: x86_64) - local arch = opt.arch or "x86_64" - - -- get target minimum version (default: nil, will use default 10.0.0 in C) - local target_minver = opt.target_minver - - -- get xcode sdk version (default: nil, will use default 10.0.0 in C) - local xcode_sdkver = opt.xcode_sdkver - - -- get zeroend (default: false) - local zeroend = opt.zeroend or false - -- 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 utils.bin2macho then - utils.bin2macho(binarypath, outputpath, symbol_prefix, plat, arch, basename, target_minver, xcode_sdkver, zeroend) + if binutils.bin2macho then + binutils.bin2macho(binarypath, outputpath, opt) else - raise("bin2obj: utils.bin2macho not available (C implementation not compiled)") + raise("bin2obj: binutils.bin2macho not available (C implementation not compiled)") end -- trace |
