From 5bdfdc142885aa12a88d4f2fd3fe866067ce1a79 Mon Sep 17 00:00:00 2001 From: ruki Date: Sat, 6 Dec 2025 23:13:59 +0800 Subject: add bin2coff --- core/src/xmake/engine.c | 2 + core/src/xmake/utils/bin2coff.c | 406 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 408 insertions(+) create mode 100644 core/src/xmake/utils/bin2coff.c (limited to 'core/src') diff --git a/core/src/xmake/engine.c b/core/src/xmake/engine.c index d71c05c62..34202d9e7 100644 --- a/core/src/xmake/engine.c +++ b/core/src/xmake/engine.c @@ -336,6 +336,7 @@ 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); #ifdef XM_CONFIG_API_HAVE_CURSES // register curses functions @@ -655,6 +656,7 @@ static luaL_Reg const g_package_functions[] = { // the utils functions static luaL_Reg const g_utils_functions[] = { { "bin2c", xm_utils_bin2c }, + { "bin2coff", xm_utils_bin2coff }, { tb_null, tb_null }, }; diff --git a/core/src/xmake/utils/bin2coff.c b/core/src/xmake/utils/bin2coff.c new file mode 100644 index 000000000..02085b9a9 --- /dev/null +++ b/core/src/xmake/utils/bin2coff.c @@ -0,0 +1,406 @@ +/*!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 bin2coff.c + * + */ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * trace + */ +#define TB_TRACE_MODULE_NAME "bin2coff" +#define TB_TRACE_MODULE_DEBUG (0) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ +#define XM_COFF_MACHINE_I386 0x014c +#define XM_COFF_MACHINE_AMD64 0x8664 +#define XM_COFF_MACHINE_ARM 0x01c0 +#define XM_COFF_MACHINE_ARM64 0xaa64 + +#define XM_COFF_SECTION_RDATA 0x40000040 // IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ +#pragma pack(push, 1) +typedef struct { + tb_uint16_t machine; + tb_uint16_t nsects; + tb_uint32_t time; + tb_uint32_t symtabofs; + tb_uint32_t nsyms; + tb_uint16_t opthdr; + tb_uint16_t flags; +} xm_coff_header_t; + +typedef struct { + tb_char_t name[8]; + tb_uint32_t vsize; + tb_uint32_t vaddr; + tb_uint32_t size; + tb_uint32_t ofs; + tb_uint32_t relocofs; + tb_uint32_t linenoofs; + tb_uint16_t nreloc; + tb_uint16_t nlineno; + tb_uint32_t flags; +} xm_coff_section_t; + +typedef struct { + union { + struct { + tb_char_t name[8]; + } shortname; + struct { + tb_uint32_t zeros; + tb_uint32_t offset; + } longname; + } n; + tb_uint32_t value; + tb_int16_t sect; + tb_uint16_t type; + tb_uint8_t scl; + tb_uint8_t naux; +} xm_coff_symbol_t; +#pragma pack(pop) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * private implementation + */ +static tb_uint16_t xm_utils_bin2coff_get_machine(tb_char_t const *arch) { + if (!arch) { + return XM_COFF_MACHINE_I386; + } + if (tb_strcmp(arch, "x86_64") == 0 || tb_strcmp(arch, "x64") == 0) { + return XM_COFF_MACHINE_AMD64; + } else if (tb_strcmp(arch, "arm64") == 0 || tb_strcmp(arch, "aarch64") == 0) { + return XM_COFF_MACHINE_ARM64; + } else if (tb_strcmp(arch, "arm") == 0) { + return XM_COFF_MACHINE_ARM; + } + 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) { + tb_assert_and_check_return(ostream && str); + if (len == 0) { + len = tb_strlen(str); + } + 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) { + tb_assert_and_check_return(ostream); + tb_byte_t zero = 0; + while (count-- > 0) { + tb_stream_bwrit(ostream, &zero, 1); + } +} + +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) { + 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); + if (len < 8) { + xm_utils_bin2coff_write_padding(ostream, 8 - len); + } + } else { + // long name: store offset in string table + tb_uint32_t zeros = 0; + tb_stream_bwrit(ostream, (tb_byte_t const *)&zeros, 4); + tb_stream_bwrit(ostream, (tb_byte_t const *)strtab_offset, 4); + *strtab_offset += (tb_uint32_t)(len + 1); // +1 for null terminator + } +} + +static tb_bool_t xm_utils_bin2coff_dump(tb_stream_ref_t istream, + tb_stream_ref_t ostream, + tb_char_t const *symbol_prefix, + tb_char_t const *arch, + tb_char_t const *basename) { + tb_assert_and_check_return_val(istream && ostream, tb_false); + + // get file size + tb_hong_t filesize = tb_stream_size(istream); + if (filesize < 0) { + return tb_false; + } + tb_uint32_t datasize = (tb_uint32_t)filesize; + + // generate symbol names from filename + tb_char_t symbol_name[256] = {0}; + tb_char_t symbol_start[256] = {0}; + tb_char_t symbol_end[256] = {0}; + tb_char_t symbol_size[256] = {0}; + + // use basename or default to "data" + if (!basename || !basename[0]) { + basename = "data"; + } + + // build symbol name + if (symbol_prefix) { + tb_snprintf(symbol_name, sizeof(symbol_name), "%s%s", symbol_prefix, basename); + } else { + tb_snprintf(symbol_name, sizeof(symbol_name), "_binary_%s", basename); + } + + // replace non-alphanumeric with underscore + for (tb_size_t i = 0; symbol_name[i]; i++) { + if (!tb_isalnum(symbol_name[i]) && symbol_name[i] != '_') { + symbol_name[i] = '_'; + } + } + + tb_snprintf(symbol_start, sizeof(symbol_start), "%s_start", symbol_name); + tb_snprintf(symbol_end, sizeof(symbol_end), "%s_end", symbol_name); + tb_snprintf(symbol_size, sizeof(symbol_size), "%s_size", symbol_name); + + // calculate offsets + tb_uint32_t header_size = sizeof(xm_coff_header_t); + tb_uint32_t section_header_size = sizeof(xm_coff_section_t); + tb_uint32_t section_data_ofs = header_size + section_header_size; + tb_uint32_t section_data_size = datasize; + tb_uint32_t symbol_table_ofs = section_data_ofs + ((section_data_size + 3) & ~3); // align to 4 bytes + tb_uint32_t string_table_ofs = 0; + tb_uint32_t string_table_size = 4; // initial 4-byte size field + + // calculate string table size + tb_size_t start_len = tb_strlen(symbol_start); + tb_size_t end_len = tb_strlen(symbol_end); + tb_size_t size_len = tb_strlen(symbol_size); + if (start_len > 8) { + string_table_size += (tb_uint32_t)(start_len + 1); + } + if (end_len > 8) { + string_table_size += (tb_uint32_t)(end_len + 1); + } + if (size_len > 8) { + string_table_size += (tb_uint32_t)(size_len + 1); + } + + // write COFF header + xm_coff_header_t header; + tb_memset(&header, 0, sizeof(header)); + header.machine = xm_utils_bin2coff_get_machine(arch); + header.nsects = 1; + header.time = 0; + header.symtabofs = symbol_table_ofs; + header.nsyms = 4; // .rdata section symbol + 3 data symbols + header.opthdr = 0; + header.flags = 0; + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&header, sizeof(header))) { + return tb_false; + } + + // write section header (.rdata) + xm_coff_section_t section; + tb_memset(§ion, 0, sizeof(section)); + tb_strncpy(section.name, ".rdata", 8); + section.vsize = datasize; + section.vaddr = 0; + section.size = datasize; + section.ofs = section_data_ofs; + section.relocofs = 0; + section.linenoofs = 0; + section.nreloc = 0; + section.nlineno = 0; + section.flags = XM_COFF_SECTION_RDATA; + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)§ion, sizeof(section))) { + return tb_false; + } + + // write section data + tb_byte_t buffer[8192]; + tb_hong_t left = filesize; + while (left > 0) { + tb_size_t to_read = (tb_size_t)tb_min(left, (tb_hong_t)sizeof(buffer)); + if (!tb_stream_bread(istream, buffer, to_read)) { + return tb_false; + } + if (!tb_stream_bwrit(ostream, buffer, to_read)) { + return tb_false; + } + left -= to_read; + } + + // align to 4 bytes + tb_uint32_t padding = (4 - (section_data_size & 3)) & 3; + if (padding > 0) { + xm_utils_bin2coff_write_padding(ostream, padding); + } + + // write symbol table + // symbol 0: .rdata section symbol + xm_coff_symbol_t sym_section; + tb_memset(&sym_section, 0, sizeof(sym_section)); + tb_strncpy(sym_section.n.shortname.name, ".rdata", 8); + sym_section.value = 0; + sym_section.sect = 1; // section index (1-based) + sym_section.type = 0; // IMAGE_SYM_TYPE_NULL + sym_section.scl = 3; // IMAGE_SYM_CLASS_STATIC + sym_section.naux = 1; // auxiliary entry + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&sym_section, sizeof(sym_section))) { + return tb_false; + } + // auxiliary entry for section + tb_uint32_t aux_section_size = datasize; + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&aux_section_size, 4)) { + return tb_false; + } + xm_utils_bin2coff_write_padding(ostream, 14); // rest of auxiliary entry + + // symbol 1: _binary_xxx_start + tb_uint32_t strtab_offset = 4; // start after size field + xm_coff_symbol_t sym_start; + tb_memset(&sym_start, 0, sizeof(sym_start)); + xm_utils_bin2coff_write_symbol_name(ostream, symbol_start, &strtab_offset); + sym_start.value = 0; + sym_start.sect = 1; + sym_start.type = 0; + sym_start.scl = 2; // IMAGE_SYM_CLASS_EXTERNAL + sym_start.naux = 0; + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&sym_start.value, sizeof(sym_start) - 8)) { + return tb_false; + } + + // symbol 2: _binary_xxx_end + xm_coff_symbol_t sym_end; + tb_memset(&sym_end, 0, sizeof(sym_end)); + xm_utils_bin2coff_write_symbol_name(ostream, symbol_end, &strtab_offset); + sym_end.value = datasize; + sym_end.sect = 1; + sym_end.type = 0; + sym_end.scl = 2; + sym_end.naux = 0; + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&sym_end.value, sizeof(sym_end) - 8)) { + return tb_false; + } + + // symbol 3: _binary_xxx_size + xm_coff_symbol_t sym_size; + tb_memset(&sym_size, 0, sizeof(sym_size)); + xm_utils_bin2coff_write_symbol_name(ostream, symbol_size, &strtab_offset); + sym_size.value = datasize; + sym_size.sect = -1; // absolute symbol + sym_size.type = 0; + sym_size.scl = 2; + sym_size.naux = 0; + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&sym_size.value, sizeof(sym_size) - 8)) { + return tb_false; + } + + // write string table + tb_stream_bwrit(ostream, (tb_byte_t const *)&string_table_size, 4); + if (tb_strlen(symbol_start) > 8) { + xm_utils_bin2coff_write_string(ostream, symbol_start, 0); + tb_byte_t null = 0; + tb_stream_bwrit(ostream, &null, 1); + } + if (tb_strlen(symbol_end) > 8) { + xm_utils_bin2coff_write_string(ostream, symbol_end, 0); + tb_byte_t null = 0; + tb_stream_bwrit(ostream, &null, 1); + } + if (tb_strlen(symbol_size) > 8) { + xm_utils_bin2coff_write_string(ostream, symbol_size, 0); + tb_byte_t null = 0; + tb_stream_bwrit(ostream, &null, 1); + } + + return tb_true; +} + +/* ////////////////////////////////////////////////////////////////////////////////////// + * implementation + */ + +/* generate COFF object file from binary file + * + * local ok, errors = utils.bin2coff(binaryfile, outputfile, symbol_prefix, arch, basename) + */ +tb_int_t xm_utils_bin2coff(lua_State *lua) { + tb_assert_and_check_return_val(lua, 0); + + // get the binaryfile + tb_char_t const *binaryfile = luaL_checkstring(lua, 1); + tb_check_return_val(binaryfile, 0); + + // get the outputfile + tb_char_t const *outputfile = luaL_checkstring(lua, 2); + tb_check_return_val(outputfile, 0); + + // get symbol prefix (optional) + tb_char_t const *symbol_prefix = lua_isstring(lua, 3) ? lua_tostring(lua, 3) : tb_null; + + // get arch (optional) + tb_char_t const *arch = lua_isstring(lua, 4) ? lua_tostring(lua, 4) : tb_null; + + // get basename (optional) + tb_char_t const *basename = lua_isstring(lua, 5) ? lua_tostring(lua, 5) : tb_null; + + // do dump + tb_bool_t ok = tb_false; + tb_stream_ref_t istream = tb_stream_init_from_file(binaryfile, TB_FILE_MODE_RO); + tb_stream_ref_t ostream = tb_stream_init_from_file(outputfile, + TB_FILE_MODE_RW | TB_FILE_MODE_CREAT | TB_FILE_MODE_TRUNC); + do { + if (!tb_stream_open(istream)) { + lua_pushboolean(lua, tb_false); + lua_pushfstring(lua, "bin2coff: open %s failed", binaryfile); + break; + } + + if (!tb_stream_open(ostream)) { + lua_pushboolean(lua, tb_false); + lua_pushfstring(lua, "bin2coff: open %s failed", outputfile); + break; + } + + if (!xm_utils_bin2coff_dump(istream, ostream, symbol_prefix, arch, basename)) { + lua_pushboolean(lua, tb_false); + lua_pushfstring(lua, "bin2coff: dump data failed"); + break; + } + + ok = tb_true; + lua_pushboolean(lua, ok); + + } while (0); + + if (istream) { + tb_stream_clos(istream); + } + istream = tb_null; + + if (ostream) { + tb_stream_clos(ostream); + } + ostream = tb_null; + + return ok ? 1 : 2; +} + -- cgit v1.3.1 From b0964cd6672d74a2c7b8a0d3b45480ffb201af1a Mon Sep 17 00:00:00 2001 From: ruki Date: Sat, 6 Dec 2025 23:22:57 +0800 Subject: fix compile error --- core/src/xmake/utils/bin2coff.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'core/src') diff --git a/core/src/xmake/utils/bin2coff.c b/core/src/xmake/utils/bin2coff.c index 02085b9a9..97d7a6722 100644 --- a/core/src/xmake/utils/bin2coff.c +++ b/core/src/xmake/utils/bin2coff.c @@ -170,7 +170,7 @@ static tb_bool_t xm_utils_bin2coff_dump(tb_stream_ref_t istream, // replace non-alphanumeric with underscore for (tb_size_t i = 0; symbol_name[i]; i++) { - if (!tb_isalnum(symbol_name[i]) && symbol_name[i] != '_') { + if (!tb_isalpha(symbol_name[i]) && !tb_isdigit(symbol_name[i]) && symbol_name[i] != '_') { symbol_name[i] = '_'; } } -- cgit v1.3.1 From 6902ff1501858cda51b4a1bf46eb76ffd4022b54 Mon Sep 17 00:00:00 2001 From: ruki Date: Sat, 6 Dec 2025 23:39:08 +0800 Subject: fix return value --- core/src/xmake/utils/bin2coff.c | 8 ++++---- xmake/modules/private/utils/bin2coff.lua | 5 +---- 2 files changed, 5 insertions(+), 8 deletions(-) (limited to 'core/src') diff --git a/core/src/xmake/utils/bin2coff.c b/core/src/xmake/utils/bin2coff.c index 97d7a6722..6c6b3c636 100644 --- a/core/src/xmake/utils/bin2coff.c +++ b/core/src/xmake/utils/bin2coff.c @@ -155,26 +155,26 @@ static tb_bool_t xm_utils_bin2coff_dump(tb_stream_ref_t istream, tb_char_t symbol_start[256] = {0}; tb_char_t symbol_end[256] = {0}; tb_char_t symbol_size[256] = {0}; - + // use basename or default to "data" if (!basename || !basename[0]) { basename = "data"; } - + // build symbol name if (symbol_prefix) { tb_snprintf(symbol_name, sizeof(symbol_name), "%s%s", symbol_prefix, basename); } else { tb_snprintf(symbol_name, sizeof(symbol_name), "_binary_%s", basename); } - + // replace non-alphanumeric with underscore for (tb_size_t i = 0; symbol_name[i]; i++) { if (!tb_isalpha(symbol_name[i]) && !tb_isdigit(symbol_name[i]) && symbol_name[i] != '_') { symbol_name[i] = '_'; } } - + tb_snprintf(symbol_start, sizeof(symbol_start), "%s_start", symbol_name); tb_snprintf(symbol_end, sizeof(symbol_end), "%s_end", symbol_name); tb_snprintf(symbol_size, sizeof(symbol_size), "%s_size", symbol_name); diff --git a/xmake/modules/private/utils/bin2coff.lua b/xmake/modules/private/utils/bin2coff.lua index e64162b03..305a1da65 100644 --- a/xmake/modules/private/utils/bin2coff.lua +++ b/xmake/modules/private/utils/bin2coff.lua @@ -52,10 +52,7 @@ function _do_bin2coff(binarypath, outputpath, opt) -- do dump if utils.bin2coff then - local ok, err = utils.bin2coff(binarypath, outputpath, symbol_prefix, arch, basename) - if not ok then - raise(err or "bin2coff: conversion failed") - end + utils.bin2coff(binarypath, outputpath, symbol_prefix, arch, basename) else raise("bin2coff: utils.bin2coff not available (C implementation not compiled)") end -- cgit v1.3.1 From 916b3d47146b3b538d5bb9165703dbaa927e1ac8 Mon Sep 17 00:00:00 2001 From: ruki Date: Sat, 6 Dec 2025 23:44:52 +0800 Subject: fix bin2coff --- core/src/xmake/utils/bin2coff.c | 55 ++++++++++++++++++++++------------------- 1 file changed, 30 insertions(+), 25 deletions(-) (limited to 'core/src') diff --git a/core/src/xmake/utils/bin2coff.c b/core/src/xmake/utils/bin2coff.c index 6c6b3c636..2ac8c5ad9 100644 --- a/core/src/xmake/utils/bin2coff.c +++ b/core/src/xmake/utils/bin2coff.c @@ -185,7 +185,6 @@ static tb_bool_t xm_utils_bin2coff_dump(tb_stream_ref_t istream, tb_uint32_t section_data_ofs = header_size + section_header_size; tb_uint32_t section_data_size = datasize; tb_uint32_t symbol_table_ofs = section_data_ofs + ((section_data_size + 3) & ~3); // align to 4 bytes - tb_uint32_t string_table_ofs = 0; tb_uint32_t string_table_size = 4; // initial 4-byte size field // calculate string table size @@ -275,41 +274,47 @@ 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_coff_symbol_t sym_start; - tb_memset(&sym_start, 0, sizeof(sym_start)); xm_utils_bin2coff_write_symbol_name(ostream, symbol_start, &strtab_offset); - sym_start.value = 0; - sym_start.sect = 1; - sym_start.type = 0; - sym_start.scl = 2; // IMAGE_SYM_CLASS_EXTERNAL - sym_start.naux = 0; - if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&sym_start.value, sizeof(sym_start) - 8)) { + tb_uint32_t sym_start_value = 0; + tb_int16_t sym_start_sect = 1; + tb_uint16_t sym_start_type = 0; + tb_uint8_t sym_start_scl = 2; // IMAGE_SYM_CLASS_EXTERNAL + tb_uint8_t sym_start_naux = 0; + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&sym_start_value, 4) || + !tb_stream_bwrit(ostream, (tb_byte_t const *)&sym_start_sect, 2) || + !tb_stream_bwrit(ostream, (tb_byte_t const *)&sym_start_type, 2) || + !tb_stream_bwrit(ostream, (tb_byte_t const *)&sym_start_scl, 1) || + !tb_stream_bwrit(ostream, (tb_byte_t const *)&sym_start_naux, 1)) { return tb_false; } // symbol 2: _binary_xxx_end - xm_coff_symbol_t sym_end; - tb_memset(&sym_end, 0, sizeof(sym_end)); xm_utils_bin2coff_write_symbol_name(ostream, symbol_end, &strtab_offset); - sym_end.value = datasize; - sym_end.sect = 1; - sym_end.type = 0; - sym_end.scl = 2; - sym_end.naux = 0; - if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&sym_end.value, sizeof(sym_end) - 8)) { + tb_uint32_t sym_end_value = datasize; + tb_int16_t sym_end_sect = 1; + tb_uint16_t sym_end_type = 0; + tb_uint8_t sym_end_scl = 2; + tb_uint8_t sym_end_naux = 0; + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&sym_end_value, 4) || + !tb_stream_bwrit(ostream, (tb_byte_t const *)&sym_end_sect, 2) || + !tb_stream_bwrit(ostream, (tb_byte_t const *)&sym_end_type, 2) || + !tb_stream_bwrit(ostream, (tb_byte_t const *)&sym_end_scl, 1) || + !tb_stream_bwrit(ostream, (tb_byte_t const *)&sym_end_naux, 1)) { return tb_false; } // symbol 3: _binary_xxx_size - xm_coff_symbol_t sym_size; - tb_memset(&sym_size, 0, sizeof(sym_size)); xm_utils_bin2coff_write_symbol_name(ostream, symbol_size, &strtab_offset); - sym_size.value = datasize; - sym_size.sect = -1; // absolute symbol - sym_size.type = 0; - sym_size.scl = 2; - sym_size.naux = 0; - if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&sym_size.value, sizeof(sym_size) - 8)) { + tb_uint32_t sym_size_value = datasize; + tb_int16_t sym_size_sect = -1; // absolute symbol + tb_uint16_t sym_size_type = 0; + tb_uint8_t sym_size_scl = 2; + tb_uint8_t sym_size_naux = 0; + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&sym_size_value, 4) || + !tb_stream_bwrit(ostream, (tb_byte_t const *)&sym_size_sect, 2) || + !tb_stream_bwrit(ostream, (tb_byte_t const *)&sym_size_type, 2) || + !tb_stream_bwrit(ostream, (tb_byte_t const *)&sym_size_scl, 1) || + !tb_stream_bwrit(ostream, (tb_byte_t const *)&sym_size_naux, 1)) { return tb_false; } -- cgit v1.3.1 From a81d6d40cfee8c7b774662e818a9fc9e4e8a2110 Mon Sep 17 00:00:00 2001 From: ruki Date: Sat, 6 Dec 2025 23:47:09 +0800 Subject: fix bin2coff again --- core/src/xmake/utils/bin2coff.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) (limited to 'core/src') diff --git a/core/src/xmake/utils/bin2coff.c b/core/src/xmake/utils/bin2coff.c index 2ac8c5ad9..24c7d1b2a 100644 --- a/core/src/xmake/utils/bin2coff.c +++ b/core/src/xmake/utils/bin2coff.c @@ -208,7 +208,7 @@ static tb_bool_t xm_utils_bin2coff_dump(tb_stream_ref_t istream, header.nsects = 1; header.time = 0; header.symtabofs = symbol_table_ofs; - header.nsyms = 4; // .rdata section symbol + 3 data symbols + header.nsyms = 5; // .rdata section symbol (1) + auxiliary entry (1) + 3 data symbols (3) header.opthdr = 0; header.flags = 0; if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&header, sizeof(header))) { @@ -265,12 +265,17 @@ static tb_bool_t xm_utils_bin2coff_dump(tb_stream_ref_t istream, if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&sym_section, sizeof(sym_section))) { return tb_false; } - // auxiliary entry for section + // auxiliary entry for section (18 bytes total) + // format: 4 bytes size, 2 bytes nreloc, 2 bytes nlineno, 10 bytes unused tb_uint32_t aux_section_size = datasize; - if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&aux_section_size, 4)) { + tb_uint16_t aux_section_nreloc = 0; + tb_uint16_t aux_section_nlineno = 0; + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&aux_section_size, 4) || + !tb_stream_bwrit(ostream, (tb_byte_t const *)&aux_section_nreloc, 2) || + !tb_stream_bwrit(ostream, (tb_byte_t const *)&aux_section_nlineno, 2)) { return tb_false; } - xm_utils_bin2coff_write_padding(ostream, 14); // rest of auxiliary entry + xm_utils_bin2coff_write_padding(ostream, 10); // rest of auxiliary entry (unused) // symbol 1: _binary_xxx_start tb_uint32_t strtab_offset = 4; // start after size field -- cgit v1.3.1 From 10bc661bedcd77f98993650a96f3fae476685bb3 Mon Sep 17 00:00:00 2001 From: ruki Date: Sun, 7 Dec 2025 00:19:02 +0800 Subject: fix symbol size in coff --- core/src/xmake/utils/bin2coff.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'core/src') diff --git a/core/src/xmake/utils/bin2coff.c b/core/src/xmake/utils/bin2coff.c index 24c7d1b2a..076dd50cd 100644 --- a/core/src/xmake/utils/bin2coff.c +++ b/core/src/xmake/utils/bin2coff.c @@ -309,11 +309,12 @@ static tb_bool_t xm_utils_bin2coff_dump(tb_stream_ref_t istream, } // symbol 3: _binary_xxx_size + // Note: use section symbol instead of absolute symbol to avoid relocation issues xm_utils_bin2coff_write_symbol_name(ostream, symbol_size, &strtab_offset); tb_uint32_t sym_size_value = datasize; - tb_int16_t sym_size_sect = -1; // absolute symbol + tb_int16_t sym_size_sect = 1; // same section as data tb_uint16_t sym_size_type = 0; - tb_uint8_t sym_size_scl = 2; + tb_uint8_t sym_size_scl = 2; // IMAGE_SYM_CLASS_EXTERNAL tb_uint8_t sym_size_naux = 0; if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&sym_size_value, 4) || !tb_stream_bwrit(ostream, (tb_byte_t const *)&sym_size_sect, 2) || -- cgit v1.3.1 From 05b3a19bef94606fc56c60b1bd16a3a5bf70ca29 Mon Sep 17 00:00:00 2001 From: ruki Date: Sun, 7 Dec 2025 00:22:25 +0800 Subject: fix size again --- core/src/xmake/utils/bin2coff.c | 22 ++++++++++++++-------- tests/projects/other/bin2obj/src/main.c | 8 ++++---- 2 files changed, 18 insertions(+), 12 deletions(-) (limited to 'core/src') diff --git a/core/src/xmake/utils/bin2coff.c b/core/src/xmake/utils/bin2coff.c index 076dd50cd..730d0dd27 100644 --- a/core/src/xmake/utils/bin2coff.c +++ b/core/src/xmake/utils/bin2coff.c @@ -180,10 +180,11 @@ static tb_bool_t xm_utils_bin2coff_dump(tb_stream_ref_t istream, tb_snprintf(symbol_size, sizeof(symbol_size), "%s_size", symbol_name); // calculate offsets + // Note: we append the size value (4 bytes) at the end of data section tb_uint32_t header_size = sizeof(xm_coff_header_t); tb_uint32_t section_header_size = sizeof(xm_coff_section_t); tb_uint32_t section_data_ofs = header_size + section_header_size; - tb_uint32_t section_data_size = datasize; + tb_uint32_t section_data_size = datasize + 4; // data + size value tb_uint32_t symbol_table_ofs = section_data_ofs + ((section_data_size + 3) & ~3); // align to 4 bytes tb_uint32_t string_table_size = 4; // initial 4-byte size field @@ -219,9 +220,9 @@ static tb_bool_t xm_utils_bin2coff_dump(tb_stream_ref_t istream, xm_coff_section_t section; tb_memset(§ion, 0, sizeof(section)); tb_strncpy(section.name, ".rdata", 8); - section.vsize = datasize; + section.vsize = section_data_size; // include size value section.vaddr = 0; - section.size = datasize; + section.size = section_data_size; // include size value section.ofs = section_data_ofs; section.relocofs = 0; section.linenoofs = 0; @@ -246,12 +247,17 @@ static tb_bool_t xm_utils_bin2coff_dump(tb_stream_ref_t istream, left -= to_read; } - // align to 4 bytes - tb_uint32_t padding = (4 - (section_data_size & 3)) & 3; + // align data to 4 bytes + tb_uint32_t padding = (4 - (datasize & 3)) & 3; if (padding > 0) { xm_utils_bin2coff_write_padding(ostream, padding); } + // append size value at the end of data section + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&datasize, 4)) { + return tb_false; + } + // write symbol table // symbol 0: .rdata section symbol xm_coff_symbol_t sym_section; @@ -267,7 +273,7 @@ static tb_bool_t xm_utils_bin2coff_dump(tb_stream_ref_t istream, } // auxiliary entry for section (18 bytes total) // format: 4 bytes size, 2 bytes nreloc, 2 bytes nlineno, 10 bytes unused - tb_uint32_t aux_section_size = datasize; + tb_uint32_t aux_section_size = section_data_size; tb_uint16_t aux_section_nreloc = 0; tb_uint16_t aux_section_nlineno = 0; if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&aux_section_size, 4) || @@ -309,9 +315,9 @@ static tb_bool_t xm_utils_bin2coff_dump(tb_stream_ref_t istream, } // symbol 3: _binary_xxx_size - // Note: use section symbol instead of absolute symbol to avoid relocation issues + // Note: point to the size value appended at the end of data section xm_utils_bin2coff_write_symbol_name(ostream, symbol_size, &strtab_offset); - tb_uint32_t sym_size_value = datasize; + tb_uint32_t sym_size_value = ((datasize + padding + 3) & ~3); // offset to size value (aligned) tb_int16_t sym_size_sect = 1; // same section as data tb_uint16_t sym_size_type = 0; tb_uint8_t sym_size_scl = 2; // IMAGE_SYM_CLASS_EXTERNAL diff --git a/tests/projects/other/bin2obj/src/main.c b/tests/projects/other/bin2obj/src/main.c index 038e3f514..8c748b7bf 100644 --- a/tests/projects/other/bin2obj/src/main.c +++ b/tests/projects/other/bin2obj/src/main.c @@ -13,11 +13,11 @@ int main(int argc, char** argv) { printf("data.bin size: %u\n", (unsigned int)_binary_data_bin_size); printf("data.bin start: %p\n", _binary_data_bin_start); printf("data.bin end: %p\n", _binary_data_bin_end); - + printf("image.png size: %u\n", (unsigned int)_binary_image_png_size); printf("image.png start: %p\n", _binary_image_png_start); printf("image.png end: %p\n", _binary_image_png_end); - + // print first few bytes of data.bin if (_binary_data_bin_size > 0) { printf("data.bin first byte: 0x%02x\n", _binary_data_bin_start[0]); @@ -25,7 +25,7 @@ int main(int argc, char** argv) { if (_binary_data_bin_size > 1) { printf("data.bin second byte: 0x%02x\n", _binary_data_bin_start[1]); } - + // print first few bytes of image.png if (_binary_image_png_size > 0) { printf("image.png first byte: 0x%02x\n", _binary_image_png_start[0]); @@ -33,6 +33,6 @@ int main(int argc, char** argv) { if (_binary_image_png_size > 1) { printf("image.png second byte: 0x%02x\n", _binary_image_png_start[1]); } - + return 0; } -- cgit v1.3.1 From 2e9c0aa9908b850bf426f0a1d065815d257b84d9 Mon Sep 17 00:00:00 2001 From: ruki Date: Sun, 7 Dec 2025 00:33:32 +0800 Subject: remove size symbols --- core/src/xmake/utils/bin2coff.c | 47 +++++---------------------------- tests/projects/other/bin2obj/src/main.c | 14 +++++----- 2 files changed, 15 insertions(+), 46 deletions(-) (limited to 'core/src') diff --git a/core/src/xmake/utils/bin2coff.c b/core/src/xmake/utils/bin2coff.c index 730d0dd27..d48d89e09 100644 --- a/core/src/xmake/utils/bin2coff.c +++ b/core/src/xmake/utils/bin2coff.c @@ -154,7 +154,6 @@ static tb_bool_t xm_utils_bin2coff_dump(tb_stream_ref_t istream, tb_char_t symbol_name[256] = {0}; tb_char_t symbol_start[256] = {0}; tb_char_t symbol_end[256] = {0}; - tb_char_t symbol_size[256] = {0}; // use basename or default to "data" if (!basename || !basename[0]) { @@ -177,30 +176,24 @@ static tb_bool_t xm_utils_bin2coff_dump(tb_stream_ref_t istream, tb_snprintf(symbol_start, sizeof(symbol_start), "%s_start", symbol_name); tb_snprintf(symbol_end, sizeof(symbol_end), "%s_end", symbol_name); - tb_snprintf(symbol_size, sizeof(symbol_size), "%s_size", symbol_name); // calculate offsets - // Note: we append the size value (4 bytes) at the end of data section tb_uint32_t header_size = sizeof(xm_coff_header_t); tb_uint32_t section_header_size = sizeof(xm_coff_section_t); tb_uint32_t section_data_ofs = header_size + section_header_size; - tb_uint32_t section_data_size = datasize + 4; // data + size value + tb_uint32_t section_data_size = datasize; tb_uint32_t symbol_table_ofs = section_data_ofs + ((section_data_size + 3) & ~3); // align to 4 bytes tb_uint32_t string_table_size = 4; // initial 4-byte size field // calculate string table size tb_size_t start_len = tb_strlen(symbol_start); tb_size_t end_len = tb_strlen(symbol_end); - tb_size_t size_len = tb_strlen(symbol_size); if (start_len > 8) { string_table_size += (tb_uint32_t)(start_len + 1); } if (end_len > 8) { string_table_size += (tb_uint32_t)(end_len + 1); } - if (size_len > 8) { - string_table_size += (tb_uint32_t)(size_len + 1); - } // write COFF header xm_coff_header_t header; @@ -209,7 +202,7 @@ static tb_bool_t xm_utils_bin2coff_dump(tb_stream_ref_t istream, header.nsects = 1; header.time = 0; header.symtabofs = symbol_table_ofs; - header.nsyms = 5; // .rdata section symbol (1) + auxiliary entry (1) + 3 data symbols (3) + header.nsyms = 4; // .rdata section symbol (1) + auxiliary entry (1) + 2 data symbols (start, end) header.opthdr = 0; header.flags = 0; if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&header, sizeof(header))) { @@ -220,9 +213,9 @@ static tb_bool_t xm_utils_bin2coff_dump(tb_stream_ref_t istream, xm_coff_section_t section; tb_memset(§ion, 0, sizeof(section)); tb_strncpy(section.name, ".rdata", 8); - section.vsize = section_data_size; // include size value + section.vsize = datasize; section.vaddr = 0; - section.size = section_data_size; // include size value + section.size = datasize; section.ofs = section_data_ofs; section.relocofs = 0; section.linenoofs = 0; @@ -247,17 +240,12 @@ static tb_bool_t xm_utils_bin2coff_dump(tb_stream_ref_t istream, left -= to_read; } - // align data to 4 bytes - tb_uint32_t padding = (4 - (datasize & 3)) & 3; + // align to 4 bytes + tb_uint32_t padding = (4 - (section_data_size & 3)) & 3; if (padding > 0) { xm_utils_bin2coff_write_padding(ostream, padding); } - // append size value at the end of data section - if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&datasize, 4)) { - return tb_false; - } - // write symbol table // symbol 0: .rdata section symbol xm_coff_symbol_t sym_section; @@ -273,7 +261,7 @@ static tb_bool_t xm_utils_bin2coff_dump(tb_stream_ref_t istream, } // auxiliary entry for section (18 bytes total) // format: 4 bytes size, 2 bytes nreloc, 2 bytes nlineno, 10 bytes unused - tb_uint32_t aux_section_size = section_data_size; + tb_uint32_t aux_section_size = datasize; tb_uint16_t aux_section_nreloc = 0; tb_uint16_t aux_section_nlineno = 0; if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&aux_section_size, 4) || @@ -314,22 +302,6 @@ static tb_bool_t xm_utils_bin2coff_dump(tb_stream_ref_t istream, return tb_false; } - // symbol 3: _binary_xxx_size - // Note: point to the size value appended at the end of data section - xm_utils_bin2coff_write_symbol_name(ostream, symbol_size, &strtab_offset); - tb_uint32_t sym_size_value = ((datasize + padding + 3) & ~3); // offset to size value (aligned) - tb_int16_t sym_size_sect = 1; // same section as data - tb_uint16_t sym_size_type = 0; - tb_uint8_t sym_size_scl = 2; // IMAGE_SYM_CLASS_EXTERNAL - tb_uint8_t sym_size_naux = 0; - if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&sym_size_value, 4) || - !tb_stream_bwrit(ostream, (tb_byte_t const *)&sym_size_sect, 2) || - !tb_stream_bwrit(ostream, (tb_byte_t const *)&sym_size_type, 2) || - !tb_stream_bwrit(ostream, (tb_byte_t const *)&sym_size_scl, 1) || - !tb_stream_bwrit(ostream, (tb_byte_t const *)&sym_size_naux, 1)) { - return tb_false; - } - // write string table tb_stream_bwrit(ostream, (tb_byte_t const *)&string_table_size, 4); if (tb_strlen(symbol_start) > 8) { @@ -342,11 +314,6 @@ static tb_bool_t xm_utils_bin2coff_dump(tb_stream_ref_t istream, tb_byte_t null = 0; tb_stream_bwrit(ostream, &null, 1); } - if (tb_strlen(symbol_size) > 8) { - xm_utils_bin2coff_write_string(ostream, symbol_size, 0); - tb_byte_t null = 0; - tb_stream_bwrit(ostream, &null, 1); - } return tb_true; } diff --git a/tests/projects/other/bin2obj/src/main.c b/tests/projects/other/bin2obj/src/main.c index 8c748b7bf..4325873da 100644 --- a/tests/projects/other/bin2obj/src/main.c +++ b/tests/projects/other/bin2obj/src/main.c @@ -3,21 +3,23 @@ extern const uint8_t _binary_data_bin_start[]; extern const uint8_t _binary_data_bin_end[]; -extern const uint32_t _binary_data_bin_size; extern const uint8_t _binary_image_png_start[]; extern const uint8_t _binary_image_png_end[]; -extern const uint32_t _binary_image_png_size; int main(int argc, char** argv) { + // calculate size from start and end + const uint32_t _binary_data_bin_size = (uint32_t)(_binary_data_bin_end - _binary_data_bin_start); + const uint32_t _binary_image_png_size = (uint32_t)(_binary_image_png_end - _binary_image_png_start); + printf("data.bin size: %u\n", (unsigned int)_binary_data_bin_size); printf("data.bin start: %p\n", _binary_data_bin_start); printf("data.bin end: %p\n", _binary_data_bin_end); - + printf("image.png size: %u\n", (unsigned int)_binary_image_png_size); printf("image.png start: %p\n", _binary_image_png_start); printf("image.png end: %p\n", _binary_image_png_end); - + // print first few bytes of data.bin if (_binary_data_bin_size > 0) { printf("data.bin first byte: 0x%02x\n", _binary_data_bin_start[0]); @@ -25,7 +27,7 @@ int main(int argc, char** argv) { if (_binary_data_bin_size > 1) { printf("data.bin second byte: 0x%02x\n", _binary_data_bin_start[1]); } - + // print first few bytes of image.png if (_binary_image_png_size > 0) { printf("image.png first byte: 0x%02x\n", _binary_image_png_start[0]); @@ -33,6 +35,6 @@ int main(int argc, char** argv) { if (_binary_image_png_size > 1) { printf("image.png second byte: 0x%02x\n", _binary_image_png_start[1]); } - + return 0; } -- cgit v1.3.1 From 1c5bf925dc824a6032832c7c8b7461981f512edb Mon Sep 17 00:00:00 2001 From: ruki Date: Sun, 7 Dec 2025 08:06:04 +0800 Subject: fix some review tips --- core/src/xmake/utils/bin2coff.c | 10 +++++----- xmake/modules/private/utils/bin2obj.lua | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) (limited to 'core/src') diff --git a/core/src/xmake/utils/bin2coff.c b/core/src/xmake/utils/bin2coff.c index d48d89e09..426aab089 100644 --- a/core/src/xmake/utils/bin2coff.c +++ b/core/src/xmake/utils/bin2coff.c @@ -145,7 +145,7 @@ static tb_bool_t xm_utils_bin2coff_dump(tb_stream_ref_t istream, // get file size tb_hong_t filesize = tb_stream_size(istream); - if (filesize < 0) { + if (filesize < 0 || filesize > 0xffffffffU) { return tb_false; } tb_uint32_t datasize = (tb_uint32_t)filesize; @@ -304,13 +304,13 @@ 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 (tb_strlen(symbol_start) > 8) { - xm_utils_bin2coff_write_string(ostream, symbol_start, 0); + if (start_len > 8) { + xm_utils_bin2coff_write_string(ostream, symbol_start, start_len); tb_byte_t null = 0; tb_stream_bwrit(ostream, &null, 1); } - if (tb_strlen(symbol_end) > 8) { - xm_utils_bin2coff_write_string(ostream, symbol_end, 0); + if (end_len > 8) { + xm_utils_bin2coff_write_string(ostream, symbol_end, end_len); tb_byte_t null = 0; tb_stream_bwrit(ostream, &null, 1); } diff --git a/xmake/modules/private/utils/bin2obj.lua b/xmake/modules/private/utils/bin2obj.lua index 7ee7ea3a1..37c78b633 100644 --- a/xmake/modules/private/utils/bin2obj.lua +++ b/xmake/modules/private/utils/bin2obj.lua @@ -31,7 +31,7 @@ local options = { function _do_bin2obj_coff(binarypath, outputpath, opt) -- get symbol prefix - local symbol_prefix = opt["symbol-prefix"] or opt.symbol_prefix or "_binary_" + local symbol_prefix = opt["symbol-prefix"] or "_binary_" -- get architecture local arch = opt.arch -- cgit v1.3.1 From 422561f32a8bb65b478abf9ce19427cc14789343 Mon Sep 17 00:00:00 2001 From: ruki Date: Sun, 7 Dec 2025 08:13:19 +0800 Subject: use struct to write --- core/src/xmake/utils/bin2coff.c | 61 ++++++++++++++++++++++------------------- 1 file changed, 33 insertions(+), 28 deletions(-) (limited to 'core/src') diff --git a/core/src/xmake/utils/bin2coff.c b/core/src/xmake/utils/bin2coff.c index 426aab089..3f5eeb6d1 100644 --- a/core/src/xmake/utils/bin2coff.c +++ b/core/src/xmake/utils/bin2coff.c @@ -83,6 +83,21 @@ typedef struct { tb_uint8_t scl; tb_uint8_t naux; } xm_coff_symbol_t; + +typedef struct { + tb_uint32_t length; + tb_uint16_t nreloc; + tb_uint16_t nlineno; + tb_uint8_t reserved[10]; +} xm_coff_aux_section_t; + +typedef struct { + tb_uint32_t value; + tb_int16_t sect; + tb_uint16_t type; + tb_uint8_t scl; + tb_uint8_t naux; +} xm_coff_symbol_tail_t; #pragma pack(pop) /* ////////////////////////////////////////////////////////////////////////////////////// @@ -260,45 +275,35 @@ static tb_bool_t xm_utils_bin2coff_dump(tb_stream_ref_t istream, return tb_false; } // auxiliary entry for section (18 bytes total) - // format: 4 bytes size, 2 bytes nreloc, 2 bytes nlineno, 10 bytes unused - tb_uint32_t aux_section_size = datasize; - tb_uint16_t aux_section_nreloc = 0; - tb_uint16_t aux_section_nlineno = 0; - if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&aux_section_size, 4) || - !tb_stream_bwrit(ostream, (tb_byte_t const *)&aux_section_nreloc, 2) || - !tb_stream_bwrit(ostream, (tb_byte_t const *)&aux_section_nlineno, 2)) { + xm_coff_aux_section_t aux_section; + tb_memset(&aux_section, 0, sizeof(aux_section)); + aux_section.length = datasize; + aux_section.nreloc = 0; + aux_section.nlineno = 0; + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&aux_section, sizeof(aux_section))) { return tb_false; } - xm_utils_bin2coff_write_padding(ostream, 10); // rest of auxiliary entry (unused) // 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); - tb_uint32_t sym_start_value = 0; - tb_int16_t sym_start_sect = 1; - tb_uint16_t sym_start_type = 0; - tb_uint8_t sym_start_scl = 2; // IMAGE_SYM_CLASS_EXTERNAL - tb_uint8_t sym_start_naux = 0; - if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&sym_start_value, 4) || - !tb_stream_bwrit(ostream, (tb_byte_t const *)&sym_start_sect, 2) || - !tb_stream_bwrit(ostream, (tb_byte_t const *)&sym_start_type, 2) || - !tb_stream_bwrit(ostream, (tb_byte_t const *)&sym_start_scl, 1) || - !tb_stream_bwrit(ostream, (tb_byte_t const *)&sym_start_naux, 1)) { + xm_coff_symbol_tail_t sym_start_tail; + tb_memset(&sym_start_tail, 0, sizeof(sym_start_tail)); + sym_start_tail.value = 0; + sym_start_tail.sect = 1; + sym_start_tail.scl = 2; // IMAGE_SYM_CLASS_EXTERNAL + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&sym_start_tail, sizeof(sym_start_tail))) { return tb_false; } // symbol 2: _binary_xxx_end xm_utils_bin2coff_write_symbol_name(ostream, symbol_end, &strtab_offset); - tb_uint32_t sym_end_value = datasize; - tb_int16_t sym_end_sect = 1; - tb_uint16_t sym_end_type = 0; - tb_uint8_t sym_end_scl = 2; - tb_uint8_t sym_end_naux = 0; - if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&sym_end_value, 4) || - !tb_stream_bwrit(ostream, (tb_byte_t const *)&sym_end_sect, 2) || - !tb_stream_bwrit(ostream, (tb_byte_t const *)&sym_end_type, 2) || - !tb_stream_bwrit(ostream, (tb_byte_t const *)&sym_end_scl, 1) || - !tb_stream_bwrit(ostream, (tb_byte_t const *)&sym_end_naux, 1)) { + xm_coff_symbol_tail_t sym_end_tail; + tb_memset(&sym_end_tail, 0, sizeof(sym_end_tail)); + sym_end_tail.value = datasize; + sym_end_tail.sect = 1; + sym_end_tail.scl = 2; // IMAGE_SYM_CLASS_EXTERNAL + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&sym_end_tail, sizeof(sym_end_tail))) { return tb_false; } -- cgit v1.3.1 From 7cc6060b3bd3fd0340aa5c5e6972ef83986613bf Mon Sep 17 00:00:00 2001 From: ruki Date: Sun, 7 Dec 2025 08:18:21 +0800 Subject: improve structs --- core/src/xmake/utils/bin2coff.c | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) (limited to 'core/src') diff --git a/core/src/xmake/utils/bin2coff.c b/core/src/xmake/utils/bin2coff.c index 3f5eeb6d1..ce591dc9e 100644 --- a/core/src/xmake/utils/bin2coff.c +++ b/core/src/xmake/utils/bin2coff.c @@ -43,8 +43,9 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * types */ -#pragma pack(push, 1) -typedef struct { +#include "tbox/prefix/packed.h" +typedef struct __xm_coff_header_t +{ tb_uint16_t machine; tb_uint16_t nsects; tb_uint32_t time; @@ -52,9 +53,10 @@ typedef struct { tb_uint32_t nsyms; tb_uint16_t opthdr; tb_uint16_t flags; -} xm_coff_header_t; +} __tb_packed__ xm_coff_header_t; -typedef struct { +typedef struct __xm_coff_section_t +{ tb_char_t name[8]; tb_uint32_t vsize; tb_uint32_t vaddr; @@ -65,9 +67,10 @@ typedef struct { tb_uint16_t nreloc; tb_uint16_t nlineno; tb_uint32_t flags; -} xm_coff_section_t; +} __tb_packed__ xm_coff_section_t; -typedef struct { +typedef struct __xm_coff_symbol_t +{ union { struct { tb_char_t name[8]; @@ -82,23 +85,25 @@ typedef struct { tb_uint16_t type; tb_uint8_t scl; tb_uint8_t naux; -} xm_coff_symbol_t; +} __tb_packed__ xm_coff_symbol_t; -typedef struct { +typedef struct __xm_coff_aux_section_t +{ tb_uint32_t length; tb_uint16_t nreloc; tb_uint16_t nlineno; tb_uint8_t reserved[10]; -} xm_coff_aux_section_t; +} __tb_packed__ xm_coff_aux_section_t; -typedef struct { +typedef struct __xm_coff_symbol_tail_t +{ tb_uint32_t value; tb_int16_t sect; tb_uint16_t type; tb_uint8_t scl; tb_uint8_t naux; -} xm_coff_symbol_tail_t; -#pragma pack(pop) +} __tb_packed__ xm_coff_symbol_tail_t; +#include "tbox/prefix/packed.h" /* ////////////////////////////////////////////////////////////////////////////////////// * private implementation -- cgit v1.3.1 From 195bd8faf9c402196abcec370380a84b7fb4653d Mon Sep 17 00:00:00 2001 From: ruki Date: Sun, 7 Dec 2025 10:22:13 +0800 Subject: add bin2macho --- core/src/xmake/engine.c | 2 + core/src/xmake/utils/bin2coff.c | 15 +- core/src/xmake/utils/bin2macho.c | 531 ++++++++++++++++++++++++++++++++ xmake/core/sandbox/modules/utils.lua | 10 + xmake/modules/private/utils/bin2obj.lua | 24 +- 5 files changed, 571 insertions(+), 11 deletions(-) create mode 100644 core/src/xmake/utils/bin2macho.c (limited to 'core/src') diff --git a/core/src/xmake/engine.c b/core/src/xmake/engine.c index 34202d9e7..802543700 100644 --- a/core/src/xmake/engine.c +++ b/core/src/xmake/engine.c @@ -337,6 +337,7 @@ 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); #ifdef XM_CONFIG_API_HAVE_CURSES // register curses functions @@ -657,6 +658,7 @@ static luaL_Reg const g_package_functions[] = { static luaL_Reg const g_utils_functions[] = { { "bin2c", xm_utils_bin2c }, { "bin2coff", xm_utils_bin2coff }, + { "bin2macho", xm_utils_bin2macho }, { tb_null, tb_null }, }; diff --git a/core/src/xmake/utils/bin2coff.c b/core/src/xmake/utils/bin2coff.c index ce591dc9e..be8a313ad 100644 --- a/core/src/xmake/utils/bin2coff.c +++ b/core/src/xmake/utils/bin2coff.c @@ -44,8 +44,7 @@ * types */ #include "tbox/prefix/packed.h" -typedef struct __xm_coff_header_t -{ +typedef struct __xm_coff_header_t { tb_uint16_t machine; tb_uint16_t nsects; tb_uint32_t time; @@ -55,8 +54,7 @@ typedef struct __xm_coff_header_t tb_uint16_t flags; } __tb_packed__ xm_coff_header_t; -typedef struct __xm_coff_section_t -{ +typedef struct __xm_coff_section_t { tb_char_t name[8]; tb_uint32_t vsize; tb_uint32_t vaddr; @@ -69,8 +67,7 @@ typedef struct __xm_coff_section_t tb_uint32_t flags; } __tb_packed__ xm_coff_section_t; -typedef struct __xm_coff_symbol_t -{ +typedef struct __xm_coff_symbol_t { union { struct { tb_char_t name[8]; @@ -87,16 +84,14 @@ typedef struct __xm_coff_symbol_t tb_uint8_t naux; } __tb_packed__ xm_coff_symbol_t; -typedef struct __xm_coff_aux_section_t -{ +typedef struct __xm_coff_aux_section_t { tb_uint32_t length; tb_uint16_t nreloc; tb_uint16_t nlineno; tb_uint8_t reserved[10]; } __tb_packed__ xm_coff_aux_section_t; -typedef struct __xm_coff_symbol_tail_t -{ +typedef struct __xm_coff_symbol_tail_t { tb_uint32_t value; tb_int16_t sect; tb_uint16_t type; diff --git a/core/src/xmake/utils/bin2macho.c b/core/src/xmake/utils/bin2macho.c new file mode 100644 index 000000000..08575dc8a --- /dev/null +++ b/core/src/xmake/utils/bin2macho.c @@ -0,0 +1,531 @@ +/*!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 bin2macho.c + * + */ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * trace + */ +#define TB_TRACE_MODULE_NAME "bin2macho" +#define TB_TRACE_MODULE_DEBUG (0) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ +#define XM_MACHO_MAGIC_32 0xfeedface +#define XM_MACHO_MAGIC_64 0xfeedfacf +#define XM_MACHO_MAGIC_FAT 0xcafebabe + +#define XM_MACHO_CPU_TYPE_X86 7 +#define XM_MACHO_CPU_TYPE_X86_64 0x01000007 +#define XM_MACHO_CPU_TYPE_ARM 12 +#define XM_MACHO_CPU_TYPE_ARM64 0x0100000c + +#define XM_MACHO_CPU_SUBTYPE_X86 3 +#define XM_MACHO_CPU_SUBTYPE_X86_64 3 +#define XM_MACHO_CPU_SUBTYPE_ARM 9 +#define XM_MACHO_CPU_SUBTYPE_ARM64 0 + +#define XM_MACHO_FILE_TYPE_OBJECT 1 + +#define XM_MACHO_LC_SEGMENT 0x1 +#define XM_MACHO_LC_SEGMENT_64 0x19 +#define XM_MACHO_LC_SYMTAB 0x2 + +#define XM_MACHO_SECT_TYPE_REGULAR 0x0 +#define XM_MACHO_SECT_ATTR_SOME_INITS 0x400 +#define XM_MACHO_SECT_ATTR_PURE_INSTRUCTIONS 0x80000000 + +#define XM_MACHO_N_TYPE_MASK 0x0e +#define XM_MACHO_N_TYPE_SECT 0x0e +#define XM_MACHO_N_EXT 0x01 + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ +#include "tbox/prefix/packed.h" +typedef struct __xm_macho_header_t { + tb_uint32_t magic; + tb_uint32_t cputype; + tb_uint32_t cpusubtype; + tb_uint32_t filetype; + tb_uint32_t ncmds; + tb_uint32_t sizeofcmds; + tb_uint32_t flags; +} __tb_packed__ xm_macho_header_t; + +typedef struct __xm_macho_header_64_t { + tb_uint32_t magic; + tb_uint32_t cputype; + tb_uint32_t cpusubtype; + tb_uint32_t filetype; + tb_uint32_t ncmds; + tb_uint32_t sizeofcmds; + tb_uint32_t flags; + tb_uint32_t reserved; +} __tb_packed__ xm_macho_header_64_t; + +typedef struct __xm_macho_segment_command_t { + tb_uint32_t cmd; + tb_uint32_t cmdsize; + tb_char_t segname[16]; + tb_uint32_t vmaddr; + tb_uint32_t vmsize; + tb_uint32_t fileoff; + tb_uint32_t filesize; + tb_uint32_t maxprot; + tb_uint32_t initprot; + tb_uint32_t nsects; + tb_uint32_t flags; +} __tb_packed__ xm_macho_segment_command_t; + +typedef struct __xm_macho_segment_command_64_t { + tb_uint32_t cmd; + tb_uint32_t cmdsize; + tb_char_t segname[16]; + tb_uint64_t vmaddr; + tb_uint64_t vmsize; + tb_uint64_t fileoff; + tb_uint64_t filesize; + tb_uint32_t maxprot; + tb_uint32_t initprot; + tb_uint32_t nsects; + tb_uint32_t flags; +} __tb_packed__ xm_macho_segment_command_64_t; + +typedef struct __xm_macho_section_t { + tb_char_t sectname[16]; + tb_char_t segname[16]; + tb_uint32_t addr; + tb_uint32_t size; + tb_uint32_t offset; + tb_uint32_t align; + tb_uint32_t reloff; + tb_uint32_t nreloc; + tb_uint32_t flags; + tb_uint32_t reserved1; + tb_uint32_t reserved2; +} __tb_packed__ xm_macho_section_t; + +typedef struct __xm_macho_section_64_t { + tb_char_t sectname[16]; + tb_char_t segname[16]; + tb_uint64_t addr; + tb_uint64_t size; + tb_uint32_t offset; + tb_uint32_t align; + tb_uint32_t reloff; + tb_uint32_t nreloc; + tb_uint32_t flags; + tb_uint32_t reserved1; + tb_uint32_t reserved2; + tb_uint32_t reserved3; +} __tb_packed__ xm_macho_section_64_t; + +typedef struct __xm_macho_symtab_command_t { + tb_uint32_t cmd; + tb_uint32_t cmdsize; + tb_uint32_t symoff; + tb_uint32_t nsyms; + tb_uint32_t stroff; + tb_uint32_t strsize; +} __tb_packed__ xm_macho_symtab_command_t; + +typedef struct __xm_macho_nlist_t { + tb_uint32_t strx; + tb_uint8_t type; + tb_uint8_t sect; + tb_int16_t desc; + tb_uint32_t value; +} __tb_packed__ xm_macho_nlist_t; + +typedef struct __xm_macho_nlist_64_t { + tb_uint32_t strx; + tb_uint8_t type; + tb_uint8_t sect; + tb_uint16_t desc; + tb_uint64_t value; +} __tb_packed__ xm_macho_nlist_64_t; +#include "tbox/prefix/packed.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * private implementation + */ +static tb_uint32_t xm_utils_bin2macho_get_cputype(tb_char_t const *arch) { + if (!arch) { + return XM_MACHO_CPU_TYPE_X86_64; + } + if (tb_strcmp(arch, "x86_64") == 0 || tb_strcmp(arch, "x64") == 0) { + return XM_MACHO_CPU_TYPE_X86_64; + } else if (tb_strcmp(arch, "arm64") == 0 || tb_strcmp(arch, "aarch64") == 0) { + return XM_MACHO_CPU_TYPE_ARM64; + } else if (tb_strcmp(arch, "arm") == 0) { + return XM_MACHO_CPU_TYPE_ARM; + } else if (tb_strcmp(arch, "x86") == 0 || tb_strcmp(arch, "i386") == 0) { + return XM_MACHO_CPU_TYPE_X86; + } + return XM_MACHO_CPU_TYPE_X86_64; +} + +static tb_uint32_t xm_utils_bin2macho_get_cpusubtype(tb_char_t const *arch) { + if (!arch) { + return XM_MACHO_CPU_SUBTYPE_X86_64; + } + if (tb_strcmp(arch, "x86_64") == 0 || tb_strcmp(arch, "x64") == 0) { + return XM_MACHO_CPU_SUBTYPE_X86_64; + } else if (tb_strcmp(arch, "arm64") == 0 || tb_strcmp(arch, "aarch64") == 0) { + return XM_MACHO_CPU_SUBTYPE_ARM64; + } else if (tb_strcmp(arch, "arm") == 0) { + return XM_MACHO_CPU_SUBTYPE_ARM; + } else if (tb_strcmp(arch, "x86") == 0 || tb_strcmp(arch, "i386") == 0) { + return XM_MACHO_CPU_SUBTYPE_X86; + } + return XM_MACHO_CPU_SUBTYPE_X86_64; +} + +static tb_bool_t xm_utils_bin2macho_is_64bit(tb_char_t const *arch) { + if (!arch) { + return tb_true; + } + if (tb_strcmp(arch, "x86_64") == 0 || tb_strcmp(arch, "x64") == 0) { + return tb_true; + } else if (tb_strcmp(arch, "arm64") == 0 || tb_strcmp(arch, "aarch64") == 0) { + return tb_true; + } else if (tb_strcmp(arch, "arm") == 0) { + return tb_false; + } else if (tb_strcmp(arch, "x86") == 0 || tb_strcmp(arch, "i386") == 0) { + return tb_false; + } + return tb_true; +} + +static tb_uint32_t xm_utils_bin2macho_align(tb_uint32_t value, tb_uint32_t align) { + return ((value + align - 1) & ~(align - 1)); +} + +static tb_bool_t xm_utils_bin2macho_dump_64(tb_stream_ref_t istream, + tb_stream_ref_t ostream, + tb_char_t const *symbol_prefix, + tb_char_t const *arch, + tb_char_t const *basename) { + tb_assert_and_check_return_val(istream && ostream, tb_false); + + // get file size + tb_hong_t filesize = tb_stream_size(istream); + if (filesize < 0 || filesize > 0xffffffffU) { + return tb_false; + } + tb_uint32_t datasize = (tb_uint32_t)filesize; + + // generate symbol names from filename + tb_char_t symbol_name[256] = {0}; + tb_char_t symbol_start[256] = {0}; + tb_char_t symbol_end[256] = {0}; + + // use basename or default to "data" + if (!basename || !basename[0]) { + basename = "data"; + } + + // build symbol name + // On macOS, C compiler adds an underscore prefix, so we generate symbols with two underscores + // (C code declares _binary_xxx, compiler generates __binary_xxx in object file, so we define __binary_xxx) + if (symbol_prefix) { + tb_snprintf(symbol_name, sizeof(symbol_name), "_%s%s", symbol_prefix, basename); + } else { + tb_snprintf(symbol_name, sizeof(symbol_name), "__binary_%s", basename); + } + + // replace non-alphanumeric with underscore + for (tb_size_t i = 0; symbol_name[i]; i++) { + if (!tb_isalpha(symbol_name[i]) && !tb_isdigit(symbol_name[i]) && symbol_name[i] != '_') { + symbol_name[i] = '_'; + } + } + + tb_snprintf(symbol_start, sizeof(symbol_start), "%s_start", symbol_name); + tb_snprintf(symbol_end, sizeof(symbol_end), "%s_end", symbol_name); + + // calculate offsets + tb_uint32_t header_size = sizeof(xm_macho_header_64_t); + tb_uint32_t segment_cmd_size = sizeof(xm_macho_segment_command_64_t); + tb_uint32_t section_size = sizeof(xm_macho_section_64_t); + tb_uint32_t symtab_cmd_size = sizeof(xm_macho_symtab_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, 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 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; + tb_uint32_t strtab_size = 4; // initial 4-byte size field + tb_size_t start_len = tb_strlen(symbol_start); + 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); + + // 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.filetype = XM_MACHO_FILE_TYPE_OBJECT; + header.ncmds = 2; // segment + symtab + header.sizeofcmds = segment_cmd_total_size + symtab_cmd_size; + header.flags = 0; + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&header, sizeof(header))) { + return tb_false; + } + + // write segment command + xm_macho_segment_command_64_t segment; + tb_memset(&segment, 0, sizeof(segment)); + segment.cmd = XM_MACHO_LC_SEGMENT_64; + segment.cmdsize = segment_cmd_total_size; + tb_strncpy(segment.segname, "__DATA", 16); + segment.vmaddr = 0; + segment.vmsize = data_size; + segment.fileoff = data_offset; + segment.filesize = data_size; + segment.maxprot = 1; // VM_PROT_READ + segment.initprot = 1; // VM_PROT_READ + segment.nsects = 1; + segment.flags = 0; + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&segment, sizeof(segment))) { + return tb_false; + } + + // write section + xm_macho_section_64_t section; + tb_memset(§ion, 0, sizeof(section)); + tb_strncpy(section.sectname, "__data", 16); + tb_strncpy(section.segname, "__DATA", 16); + section.addr = 0; + section.size = data_size; + section.offset = data_offset; + section.align = 3; // 2^3 = 8 bytes + section.reloff = 0; + section.nreloc = 0; + section.flags = XM_MACHO_SECT_TYPE_REGULAR | XM_MACHO_SECT_ATTR_SOME_INITS; + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)§ion, sizeof(section))) { + return tb_false; + } + + // write symtab command + xm_macho_symtab_command_t symtab; + tb_memset(&symtab, 0, sizeof(symtab)); + symtab.cmd = XM_MACHO_LC_SYMTAB; + symtab.cmdsize = symtab_cmd_size; + symtab.symoff = symtab_offset; + symtab.nsyms = nlist_count; + symtab.stroff = strtab_offset; + symtab.strsize = strtab_size; + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&symtab, sizeof(symtab))) { + return tb_false; + } + + // align to 8 bytes + tb_uint32_t padding = data_offset - (header_size + segment_cmd_total_size + symtab_cmd_size); + if (padding > 0) { + tb_byte_t zero = 0; + while (padding-- > 0) { + if (!tb_stream_bwrit(ostream, &zero, 1)) { + return tb_false; + } + } + } + + // write section data + tb_byte_t buffer[8192]; + tb_hong_t left = filesize; + while (left > 0) { + tb_size_t to_read = (tb_size_t)tb_min(left, (tb_hong_t)sizeof(buffer)); + if (!tb_stream_bread(istream, buffer, to_read)) { + return tb_false; + } + if (!tb_stream_bwrit(ostream, buffer, to_read)) { + return tb_false; + } + left -= to_read; + } + + // align to 8 bytes + padding = symtab_offset - data_end_offset; + if (padding > 0) { + tb_byte_t zero = 0; + while (padding-- > 0) { + if (!tb_stream_bwrit(ostream, &zero, 1)) { + return tb_false; + } + } + } + + // write symbol table + // strx starts from 4 (after 4-byte size field) + tb_uint32_t strx = 4; + // symbol 0: _binary_xxx_start + xm_macho_nlist_64_t nlist_start; + tb_memset(&nlist_start, 0, sizeof(nlist_start)); + nlist_start.strx = strx; + nlist_start.type = XM_MACHO_N_TYPE_SECT | XM_MACHO_N_EXT; // N_SECT | N_EXT + nlist_start.sect = 1; + nlist_start.desc = 0; + nlist_start.value = 0; + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&nlist_start, sizeof(nlist_start))) { + return tb_false; + } + strx += (tb_uint32_t)(start_len + 1); + + // symbol 1: _binary_xxx_end + xm_macho_nlist_64_t nlist_end; + tb_memset(&nlist_end, 0, sizeof(nlist_end)); + nlist_end.strx = strx; + nlist_end.type = XM_MACHO_N_TYPE_SECT | XM_MACHO_N_EXT; // N_SECT | N_EXT + nlist_end.sect = 1; + nlist_end.desc = 0; + nlist_end.value = data_size; + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&nlist_end, sizeof(nlist_end))) { + return tb_false; + } + strx += (tb_uint32_t)(end_len + 1); + + // align to 8 bytes + padding = strtab_offset - (symtab_offset + nlist_size * nlist_count); + if (padding > 0) { + tb_byte_t zero = 0; + while (padding-- > 0) { + if (!tb_stream_bwrit(ostream, &zero, 1)) { + return tb_false; + } + } + } + + // write string table + tb_stream_bwrit(ostream, (tb_byte_t const *)&strtab_size, 4); + tb_stream_bwrit(ostream, (tb_byte_t const *)symbol_start, start_len); + tb_byte_t null = 0; + tb_stream_bwrit(ostream, &null, 1); + tb_stream_bwrit(ostream, (tb_byte_t const *)symbol_end, end_len); + tb_stream_bwrit(ostream, &null, 1); + + // align string table to 8 bytes + padding = strtab_size - (4 + start_len + 1 + end_len + 1); + if (padding > 0) { + tb_byte_t zero = 0; + while (padding-- > 0) { + if (!tb_stream_bwrit(ostream, &zero, 1)) { + return tb_false; + } + } + } + + return tb_true; +} + +static tb_bool_t xm_utils_bin2macho_dump(tb_stream_ref_t istream, + tb_stream_ref_t ostream, + tb_char_t const *symbol_prefix, + tb_char_t const *arch, + tb_char_t const *basename) { + if (xm_utils_bin2macho_is_64bit(arch)) { + return xm_utils_bin2macho_dump_64(istream, ostream, symbol_prefix, arch, basename); + } else { + // 32-bit not implemented yet + return tb_false; + } +} + +/* ////////////////////////////////////////////////////////////////////////////////////// + * implementation + */ + +/* generate Mach-O object file from binary file + * + * local ok, errors = utils.bin2macho(binaryfile, outputfile, symbol_prefix, arch, basename) + */ +tb_int_t xm_utils_bin2macho(lua_State *lua) { + tb_assert_and_check_return_val(lua, 0); + + // get the binaryfile + tb_char_t const *binaryfile = luaL_checkstring(lua, 1); + tb_check_return_val(binaryfile, 0); + + // get the outputfile + tb_char_t const *outputfile = luaL_checkstring(lua, 2); + tb_check_return_val(outputfile, 0); + + // get symbol prefix (optional) + tb_char_t const *symbol_prefix = lua_isstring(lua, 3) ? lua_tostring(lua, 3) : tb_null; + + // get arch (optional) + tb_char_t const *arch = lua_isstring(lua, 4) ? lua_tostring(lua, 4) : tb_null; + + // get basename (optional) + tb_char_t const *basename = lua_isstring(lua, 5) ? lua_tostring(lua, 5) : tb_null; + + // do dump + tb_bool_t ok = tb_false; + tb_stream_ref_t istream = tb_stream_init_from_file(binaryfile, TB_FILE_MODE_RO); + tb_stream_ref_t ostream = tb_stream_init_from_file(outputfile, + TB_FILE_MODE_RW | TB_FILE_MODE_CREAT | TB_FILE_MODE_TRUNC); + do { + if (!tb_stream_open(istream)) { + lua_pushboolean(lua, tb_false); + lua_pushfstring(lua, "bin2macho: open %s failed", binaryfile); + break; + } + + if (!tb_stream_open(ostream)) { + lua_pushboolean(lua, tb_false); + lua_pushfstring(lua, "bin2macho: open %s failed", outputfile); + break; + } + + if (!xm_utils_bin2macho_dump(istream, ostream, symbol_prefix, arch, basename)) { + lua_pushboolean(lua, tb_false); + lua_pushfstring(lua, "bin2macho: dump data failed"); + break; + } + + ok = tb_true; + lua_pushboolean(lua, ok); + + } while (0); + + if (istream) { + tb_stream_clos(istream); + } + istream = tb_null; + + if (ostream) { + tb_stream_clos(ostream); + } + ostream = tb_null; + + return ok ? 1 : 2; +} + diff --git a/xmake/core/sandbox/modules/utils.lua b/xmake/core/sandbox/modules/utils.lua index f366459aa..694447924 100644 --- a/xmake/core/sandbox/modules/utils.lua +++ b/xmake/core/sandbox/modules/utils.lua @@ -168,6 +168,16 @@ if utils.bin2coff then end end +-- generate Mach-O object file from the binary file +if utils.bin2macho then + function sandbox_utils.bin2macho(binaryfile, outputfile, symbol_prefix, arch, basename) + local ok, errors = utils.bin2macho(binaryfile, outputfile, symbol_prefix, arch, basename) + if not ok then + os.raise(errors) + end + end +end + -- return module return sandbox_utils diff --git a/xmake/modules/private/utils/bin2obj.lua b/xmake/modules/private/utils/bin2obj.lua index 37c78b633..2221adf3c 100644 --- a/xmake/modules/private/utils/bin2obj.lua +++ b/xmake/modules/private/utils/bin2obj.lua @@ -60,7 +60,29 @@ function _do_bin2obj_elf(binarypath, outputpath, opt) end function _do_bin2obj_macho(binarypath, outputpath, opt) - raise("bin2obj: Mach-O format not yet implemented") + -- get symbol prefix + local symbol_prefix = opt["symbol-prefix"] or "_binary_" + + -- get architecture + local arch = opt.arch + + -- 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("%.", "_") + + -- 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, arch, basename) + else + raise("bin2obj: utils.bin2macho not available (C implementation not compiled)") + end + + -- trace + cprint("${bright}%s generated!", outputpath) end function _do_bin2obj(binarypath, outputpath, opt) -- cgit v1.3.1 From 00f6b0ba403a86ad542a1fbecc142064aa641f0d Mon Sep 17 00:00:00 2001 From: ruki Date: Sun, 7 Dec 2025 10:24:44 +0800 Subject: fix ld warnings --- core/src/xmake/utils/bin2macho.c | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) (limited to 'core/src') diff --git a/core/src/xmake/utils/bin2macho.c b/core/src/xmake/utils/bin2macho.c index 08575dc8a..4d69d0656 100644 --- a/core/src/xmake/utils/bin2macho.c +++ b/core/src/xmake/utils/bin2macho.c @@ -52,6 +52,7 @@ #define XM_MACHO_LC_SEGMENT 0x1 #define XM_MACHO_LC_SEGMENT_64 0x19 #define XM_MACHO_LC_SYMTAB 0x2 +#define XM_MACHO_LC_BUILD_VERSION 0x32 #define XM_MACHO_SECT_TYPE_REGULAR 0x0 #define XM_MACHO_SECT_ATTR_SOME_INITS 0x400 @@ -152,6 +153,15 @@ typedef struct __xm_macho_symtab_command_t { tb_uint32_t strsize; } __tb_packed__ xm_macho_symtab_command_t; +typedef struct __xm_macho_build_version_command_t { + tb_uint32_t cmd; + tb_uint32_t cmdsize; + tb_uint32_t platform; + tb_uint32_t minos; + tb_uint32_t sdk; + tb_uint32_t ntools; +} __tb_packed__ xm_macho_build_version_command_t; + typedef struct __xm_macho_nlist_t { tb_uint32_t strx; tb_uint8_t type; @@ -272,8 +282,9 @@ static tb_bool_t xm_utils_bin2macho_dump_64(tb_stream_ref_t istream, tb_uint32_t segment_cmd_size = sizeof(xm_macho_segment_command_64_t); tb_uint32_t section_size = sizeof(xm_macho_section_64_t); 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, 8); + 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_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); @@ -294,8 +305,8 @@ static tb_bool_t xm_utils_bin2macho_dump_64(tb_stream_ref_t istream, header.cputype = xm_utils_bin2macho_get_cputype(arch); header.cpusubtype = xm_utils_bin2macho_get_cpusubtype(arch); header.filetype = XM_MACHO_FILE_TYPE_OBJECT; - header.ncmds = 2; // segment + symtab - header.sizeofcmds = segment_cmd_total_size + symtab_cmd_size; + header.ncmds = 3; // segment + symtab + build_version + header.sizeofcmds = segment_cmd_total_size + symtab_cmd_size + build_version_cmd_size; header.flags = 0; if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&header, sizeof(header))) { return tb_false; @@ -348,8 +359,21 @@ static tb_bool_t xm_utils_bin2macho_dump_64(tb_stream_ref_t istream, return tb_false; } + // write build version command + xm_macho_build_version_command_t build_version; + 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 = 1; // PLATFORM_MACOS + build_version.minos = 0x000a0000; // 10.0.0 + build_version.sdk = 0x000a0000; // 10.0.0 + build_version.ntools = 0; + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&build_version, sizeof(build_version))) { + return tb_false; + } + // align to 8 bytes - tb_uint32_t padding = data_offset - (header_size + segment_cmd_total_size + symtab_cmd_size); + tb_uint32_t padding = data_offset - (header_size + segment_cmd_total_size + symtab_cmd_size + build_version_cmd_size); if (padding > 0) { tb_byte_t zero = 0; while (padding-- > 0) { -- cgit v1.3.1 From cae5b0a17dc7d0eb5599c0d5a92fab69d39983d5 Mon Sep 17 00:00:00 2001 From: ruki Date: Sun, 7 Dec 2025 11:17:46 +0800 Subject: support for iphoneos in bin2macho --- core/src/xmake/utils/bin2macho.c | 38 +++++++++++++++++++++++++++------ xmake/core/sandbox/modules/utils.lua | 4 ++-- xmake/modules/private/utils/bin2obj.lua | 19 +++++++++++------ xmake/rules/utils/bin2obj/xmake.lua | 6 +++++- 4 files changed, 52 insertions(+), 15 deletions(-) (limited to 'core/src') diff --git a/core/src/xmake/utils/bin2macho.c b/core/src/xmake/utils/bin2macho.c index 4d69d0656..b5a97c21f 100644 --- a/core/src/xmake/utils/bin2macho.c +++ b/core/src/xmake/utils/bin2macho.c @@ -54,6 +54,11 @@ #define XM_MACHO_LC_SYMTAB 0x2 #define XM_MACHO_LC_BUILD_VERSION 0x32 +#define XM_MACHO_PLATFORM_MACOS 1 +#define XM_MACHO_PLATFORM_IOS 2 +#define XM_MACHO_PLATFORM_TVOS 3 +#define XM_MACHO_PLATFORM_WATCHOS 4 + #define XM_MACHO_SECT_TYPE_REGULAR 0x0 #define XM_MACHO_SECT_ATTR_SOME_INITS 0x400 #define XM_MACHO_SECT_ATTR_PURE_INSTRUCTIONS 0x80000000 @@ -234,9 +239,26 @@ static tb_uint32_t xm_utils_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) { + if (!platform) { + return XM_MACHO_PLATFORM_MACOS; + } + if (tb_strcmp(platform, "macosx") == 0 || tb_strcmp(platform, "macos") == 0) { + return XM_MACHO_PLATFORM_MACOS; + } else if (tb_strcmp(platform, "iphoneos") == 0 || tb_strcmp(platform, "ios") == 0) { + return XM_MACHO_PLATFORM_IOS; + } else if (tb_strcmp(platform, "appletvos") == 0 || tb_strcmp(platform, "tvos") == 0) { + return XM_MACHO_PLATFORM_TVOS; + } else if (tb_strcmp(platform, "watchos") == 0) { + return XM_MACHO_PLATFORM_WATCHOS; + } + return XM_MACHO_PLATFORM_MACOS; +} + static tb_bool_t xm_utils_bin2macho_dump_64(tb_stream_ref_t istream, tb_stream_ref_t ostream, tb_char_t const *symbol_prefix, + tb_char_t const *plat, tb_char_t const *arch, tb_char_t const *basename) { tb_assert_and_check_return_val(istream && ostream, tb_false); @@ -364,7 +386,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 = 1; // PLATFORM_MACOS + build_version.platform = xm_utils_bin2macho_get_platform(plat); build_version.minos = 0x000a0000; // 10.0.0 build_version.sdk = 0x000a0000; // 10.0.0 build_version.ntools = 0; @@ -473,10 +495,11 @@ static tb_bool_t xm_utils_bin2macho_dump_64(tb_stream_ref_t istream, static tb_bool_t xm_utils_bin2macho_dump(tb_stream_ref_t istream, tb_stream_ref_t ostream, tb_char_t const *symbol_prefix, + tb_char_t const *plat, tb_char_t const *arch, tb_char_t const *basename) { if (xm_utils_bin2macho_is_64bit(arch)) { - return xm_utils_bin2macho_dump_64(istream, ostream, symbol_prefix, arch, basename); + return xm_utils_bin2macho_dump_64(istream, ostream, symbol_prefix, plat, arch, basename); } else { // 32-bit not implemented yet return tb_false; @@ -489,7 +512,7 @@ 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, arch, basename) + * local ok, errors = utils.bin2macho(binaryfile, outputfile, symbol_prefix, plat, arch, basename) */ tb_int_t xm_utils_bin2macho(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); @@ -505,11 +528,14 @@ tb_int_t xm_utils_bin2macho(lua_State *lua) { // get symbol prefix (optional) tb_char_t const *symbol_prefix = lua_isstring(lua, 3) ? lua_tostring(lua, 3) : tb_null; + // get plat (optional) + tb_char_t const *plat = lua_isstring(lua, 4) ? lua_tostring(lua, 4) : tb_null; + // get arch (optional) - tb_char_t const *arch = lua_isstring(lua, 4) ? lua_tostring(lua, 4) : tb_null; + tb_char_t const *arch = lua_isstring(lua, 5) ? lua_tostring(lua, 5) : tb_null; // get basename (optional) - tb_char_t const *basename = lua_isstring(lua, 5) ? lua_tostring(lua, 5) : tb_null; + tb_char_t const *basename = lua_isstring(lua, 6) ? lua_tostring(lua, 6) : tb_null; // do dump tb_bool_t ok = tb_false; @@ -529,7 +555,7 @@ tb_int_t xm_utils_bin2macho(lua_State *lua) { break; } - if (!xm_utils_bin2macho_dump(istream, ostream, symbol_prefix, arch, basename)) { + if (!xm_utils_bin2macho_dump(istream, ostream, symbol_prefix, plat, arch, basename)) { lua_pushboolean(lua, tb_false); lua_pushfstring(lua, "bin2macho: dump data failed"); break; diff --git a/xmake/core/sandbox/modules/utils.lua b/xmake/core/sandbox/modules/utils.lua index 694447924..b6dfedd45 100644 --- a/xmake/core/sandbox/modules/utils.lua +++ b/xmake/core/sandbox/modules/utils.lua @@ -170,8 +170,8 @@ end -- generate Mach-O object file from the binary file if utils.bin2macho then - function sandbox_utils.bin2macho(binaryfile, outputfile, symbol_prefix, arch, basename) - local ok, errors = utils.bin2macho(binaryfile, outputfile, symbol_prefix, arch, basename) + function sandbox_utils.bin2macho(binaryfile, outputfile, symbol_prefix, plat, arch, basename) + local ok, errors = utils.bin2macho(binaryfile, outputfile, symbol_prefix, plat, arch, basename) if not ok then os.raise(errors) end diff --git a/xmake/modules/private/utils/bin2obj.lua b/xmake/modules/private/utils/bin2obj.lua index 2221adf3c..a27f5c5dc 100644 --- a/xmake/modules/private/utils/bin2obj.lua +++ b/xmake/modules/private/utils/bin2obj.lua @@ -26,15 +26,19 @@ local options = { {'o', "outputpath", "kv", nil, "Set the output object file path."}, {'f', "format", "kv", nil, "Set the object file format (coff, elf, macho)."}, {nil, "symbol-prefix", "kv", nil, "Set the symbol prefix (default: _binary_)."}, - {'a', "arch", "kv", nil, "Set the target architecture."} + {'a', "arch", "kv", nil, "Set the target architecture."}, + {'p', "plat", "kv", nil, "Set the target platform (macosx, iphoneos, etc.)."} } function _do_bin2obj_coff(binarypath, outputpath, opt) -- get symbol prefix local symbol_prefix = opt["symbol-prefix"] or "_binary_" - -- get architecture - local arch = opt.arch + -- get architecture (default: x86_64) + local arch = opt.arch or "x86_64" + + -- get platform (not used for COFF, but passed for consistency) + local platform = opt.plat -- get filename from binary path (with extension, dots replaced with underscores) local filename = path.filename(binarypath) @@ -63,8 +67,11 @@ function _do_bin2obj_macho(binarypath, outputpath, opt) -- get symbol prefix local symbol_prefix = opt["symbol-prefix"] or "_binary_" - -- get architecture - local arch = opt.arch + -- get platform (default: macosx) + local plat = opt.plat or "macosx" + + -- get architecture (default: x86_64) + local arch = opt.arch or "x86_64" -- get filename from binary path (with extension, dots replaced with underscores) local filename = path.filename(binarypath) @@ -76,7 +83,7 @@ function _do_bin2obj_macho(binarypath, outputpath, opt) -- do dump if utils.bin2macho then - utils.bin2macho(binarypath, outputpath, symbol_prefix, arch, basename) + utils.bin2macho(binarypath, outputpath, symbol_prefix, plat, arch, basename) else raise("bin2obj: utils.bin2macho not available (C implementation not compiled)") end diff --git a/xmake/rules/utils/bin2obj/xmake.lua b/xmake/rules/utils/bin2obj/xmake.lua index 2bdd9c3a8..def3f56e8 100644 --- a/xmake/rules/utils/bin2obj/xmake.lua +++ b/xmake/rules/utils/bin2obj/xmake.lua @@ -50,12 +50,16 @@ rule("utils.bin2obj") -- get architecture local arch = target:arch() + -- get platform + local plat = target:plat() + -- convert binary file to object file local argv = { "-i", path(sourcefile_bin), "-o", path(objectfile), "-f", format, - "-a", arch + "-a", arch, + "-p", plat } if symbol_prefix ~= "_binary_" then table.insert(argv, "--symbol-prefix") -- cgit v1.3.1 From 1ef6dddad4c6a3a2e3b3b79667e7f4568f7cfda0 Mon Sep 17 00:00:00 2001 From: ruki Date: Sun, 7 Dec 2025 11:36:02 +0800 Subject: improve minos and sdkver --- core/src/xmake/utils/bin2macho.c | 72 ++++++++++++++++++++++++++++----- xmake/core/sandbox/modules/utils.lua | 4 +- xmake/modules/private/utils/bin2obj.lua | 18 ++++++--- xmake/rules/utils/bin2obj/xmake.lua | 20 ++++++++- 4 files changed, 94 insertions(+), 20 deletions(-) (limited to 'core/src') diff --git a/core/src/xmake/utils/bin2macho.c b/core/src/xmake/utils/bin2macho.c index b5a97c21f..5499ee103 100644 --- a/core/src/xmake/utils/bin2macho.c +++ b/core/src/xmake/utils/bin2macho.c @@ -67,6 +67,10 @@ #define XM_MACHO_N_TYPE_SECT 0x0e #define XM_MACHO_N_EXT 0x01 +#define XM_MACHO_VM_PROT_READ 1 +#define XM_MACHO_VM_PROT_WRITE 2 +#define XM_MACHO_VM_PROT_EXECUTE 4 + /* ////////////////////////////////////////////////////////////////////////////////////// * types */ @@ -255,12 +259,48 @@ static tb_uint32_t xm_utils_bin2macho_get_platform(tb_char_t const *platform) { return XM_MACHO_PLATFORM_MACOS; } +// 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) { + if (!version_str || !version_str[0]) { + return 0x000a0000; // default: 10.0.0 + } + tb_uint32_t major = 0; + tb_uint32_t minor = 0; + tb_uint32_t patch = 0; + tb_char_t const *p = version_str; + // parse major + while (*p && tb_isdigit(*p)) { + major = major * 10 + (*p - '0'); + p++; + } + if (*p == '.') { + p++; + // parse minor + while (*p && tb_isdigit(*p)) { + minor = minor * 10 + (*p - '0'); + p++; + } + if (*p == '.') { + p++; + // parse patch + while (*p && tb_isdigit(*p)) { + patch = patch * 10 + (*p - '0'); + p++; + } + } + } + return (major << 16) | (minor << 8) | patch; +} + static tb_bool_t xm_utils_bin2macho_dump_64(tb_stream_ref_t istream, tb_stream_ref_t ostream, tb_char_t const *symbol_prefix, tb_char_t const *plat, tb_char_t const *arch, - tb_char_t const *basename) { + tb_char_t const *basename, + tb_uint32_t minos, + tb_uint32_t sdk) { tb_assert_and_check_return_val(istream && ostream, tb_false); // get file size @@ -339,13 +379,13 @@ static tb_bool_t xm_utils_bin2macho_dump_64(tb_stream_ref_t istream, tb_memset(&segment, 0, sizeof(segment)); segment.cmd = XM_MACHO_LC_SEGMENT_64; segment.cmdsize = segment_cmd_total_size; - tb_strncpy(segment.segname, "__DATA", 16); + tb_strncpy(segment.segname, "__TEXT", 16); segment.vmaddr = 0; segment.vmsize = data_size; segment.fileoff = data_offset; segment.filesize = data_size; - segment.maxprot = 1; // VM_PROT_READ - segment.initprot = 1; // VM_PROT_READ + segment.maxprot = XM_MACHO_VM_PROT_READ; // VM_PROT_READ (read-only) + segment.initprot = XM_MACHO_VM_PROT_READ; // VM_PROT_READ (read-only) segment.nsects = 1; segment.flags = 0; if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&segment, sizeof(segment))) { @@ -355,8 +395,8 @@ static tb_bool_t xm_utils_bin2macho_dump_64(tb_stream_ref_t istream, // write section xm_macho_section_64_t section; tb_memset(§ion, 0, sizeof(section)); - tb_strncpy(section.sectname, "__data", 16); - tb_strncpy(section.segname, "__DATA", 16); + tb_strncpy(section.sectname, "__const", 16); + tb_strncpy(section.segname, "__TEXT", 16); section.addr = 0; section.size = data_size; section.offset = data_offset; @@ -387,8 +427,8 @@ static tb_bool_t xm_utils_bin2macho_dump_64(tb_stream_ref_t istream, 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.minos = 0x000a0000; // 10.0.0 - build_version.sdk = 0x000a0000; // 10.0.0 + build_version.minos = minos; + build_version.sdk = sdk; build_version.ntools = 0; if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&build_version, sizeof(build_version))) { return tb_false; @@ -497,9 +537,11 @@ static tb_bool_t xm_utils_bin2macho_dump(tb_stream_ref_t istream, tb_char_t const *symbol_prefix, tb_char_t const *plat, tb_char_t const *arch, - tb_char_t const *basename) { + tb_char_t const *basename, + tb_uint32_t minos, + tb_uint32_t sdk) { if (xm_utils_bin2macho_is_64bit(arch)) { - return xm_utils_bin2macho_dump_64(istream, ostream, symbol_prefix, plat, arch, basename); + return xm_utils_bin2macho_dump_64(istream, ostream, symbol_prefix, plat, arch, basename, minos, sdk); } else { // 32-bit not implemented yet return tb_false; @@ -537,6 +579,14 @@ tb_int_t xm_utils_bin2macho(lua_State *lua) { // get basename (optional) tb_char_t const *basename = lua_isstring(lua, 6) ? lua_tostring(lua, 6) : tb_null; + // 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); + + // 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); + // do dump tb_bool_t ok = tb_false; tb_stream_ref_t istream = tb_stream_init_from_file(binaryfile, TB_FILE_MODE_RO); @@ -555,7 +605,7 @@ tb_int_t xm_utils_bin2macho(lua_State *lua) { break; } - if (!xm_utils_bin2macho_dump(istream, ostream, symbol_prefix, plat, arch, basename)) { + if (!xm_utils_bin2macho_dump(istream, ostream, symbol_prefix, plat, arch, basename, minos, sdk)) { lua_pushboolean(lua, tb_false); lua_pushfstring(lua, "bin2macho: dump data failed"); break; diff --git a/xmake/core/sandbox/modules/utils.lua b/xmake/core/sandbox/modules/utils.lua index b6dfedd45..b3453cb92 100644 --- a/xmake/core/sandbox/modules/utils.lua +++ b/xmake/core/sandbox/modules/utils.lua @@ -170,8 +170,8 @@ 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) - local ok, errors = utils.bin2macho(binaryfile, outputfile, symbol_prefix, plat, arch, basename) + function sandbox_utils.bin2macho(binaryfile, outputfile, symbol_prefix, plat, arch, basename, target_minver, xcode_sdkver) + local ok, errors = utils.bin2macho(binaryfile, outputfile, symbol_prefix, plat, arch, basename, target_minver, xcode_sdkver) if not ok then os.raise(errors) end diff --git a/xmake/modules/private/utils/bin2obj.lua b/xmake/modules/private/utils/bin2obj.lua index a27f5c5dc..a41c919fe 100644 --- a/xmake/modules/private/utils/bin2obj.lua +++ b/xmake/modules/private/utils/bin2obj.lua @@ -25,14 +25,16 @@ local options = { {'i', "binarypath", "kv", nil, "Set the binary file path."}, {'o', "outputpath", "kv", nil, "Set the output object file path."}, {'f', "format", "kv", nil, "Set the object file format (coff, elf, macho)."}, - {nil, "symbol-prefix", "kv", nil, "Set the symbol prefix (default: _binary_)."}, + {nil, "symbol_prefix", "kv", nil, "Set the symbol prefix (default: _binary_)."}, {'a', "arch", "kv", nil, "Set the target architecture."}, - {'p', "plat", "kv", nil, "Set the target platform (macosx, iphoneos, etc.)."} + {'p', "plat", "kv", nil, "Set the target platform (macosx, iphoneos, etc.)."}, + {nil, "target_minver", "kv", nil, "Set the target minimum version (e.g., 10.0, 18.2)."}, + {nil, "xcode_sdkver", "kv", nil, "Set the Xcode SDK version (e.g., 10.0, 18.2)."} } function _do_bin2obj_coff(binarypath, outputpath, opt) -- get symbol prefix - local symbol_prefix = opt["symbol-prefix"] or "_binary_" + local symbol_prefix = opt.symbol_prefix or "_binary_" -- get architecture (default: x86_64) local arch = opt.arch or "x86_64" @@ -65,7 +67,7 @@ end function _do_bin2obj_macho(binarypath, outputpath, opt) -- get symbol prefix - local symbol_prefix = opt["symbol-prefix"] or "_binary_" + local symbol_prefix = opt.symbol_prefix or "_binary_" -- get platform (default: macosx) local plat = opt.plat or "macosx" @@ -73,6 +75,12 @@ function _do_bin2obj_macho(binarypath, outputpath, opt) -- 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 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) @@ -83,7 +91,7 @@ function _do_bin2obj_macho(binarypath, outputpath, opt) -- do dump if utils.bin2macho then - utils.bin2macho(binarypath, outputpath, symbol_prefix, plat, arch, basename) + utils.bin2macho(binarypath, outputpath, symbol_prefix, plat, arch, basename, target_minver, xcode_sdkver) else raise("bin2obj: utils.bin2macho not available (C implementation not compiled)") end diff --git a/xmake/rules/utils/bin2obj/xmake.lua b/xmake/rules/utils/bin2obj/xmake.lua index def3f56e8..1f8e6e0b2 100644 --- a/xmake/rules/utils/bin2obj/xmake.lua +++ b/xmake/rules/utils/bin2obj/xmake.lua @@ -53,6 +53,17 @@ rule("utils.bin2obj") -- get platform local plat = target:plat() + -- get target_minver and xcode_sdkver from xcode toolchain (if available) + local target_minver = nil + local xcode_sdkver = nil + if format == "macho" then + local toolchain = target:toolchain("xcode") + if toolchain then + target_minver = toolchain:config("target_minver") + xcode_sdkver = toolchain:config("xcode_sdkver") + end + end + -- convert binary file to object file local argv = { "-i", path(sourcefile_bin), @@ -62,8 +73,13 @@ rule("utils.bin2obj") "-p", plat } if symbol_prefix ~= "_binary_" then - table.insert(argv, "--symbol-prefix") - table.insert(argv, symbol_prefix) + table.insert(argv, "--symbol_prefix=" .. symbol_prefix) + end + if target_minver then + table.insert(argv, "--target_minver=" .. target_minver) + end + if xcode_sdkver then + table.insert(argv, "--xcode_sdkver=" .. xcode_sdkver) end batchcmds:vlua("private.utils.bin2obj", argv) -- cgit v1.3.1 From 774c5110ef60f2878d3a2f503c7a55db4943a1cb Mon Sep 17 00:00:00 2001 From: ruki Date: Sun, 7 Dec 2025 12:06:33 +0800 Subject: add zeroend --- core/src/xmake/utils/bin2coff.c | 25 +++++++++++++++++++++++-- core/src/xmake/utils/bin2macho.c | 27 +++++++++++++++++++++++---- tests/projects/other/bin2obj/xmake.lua | 1 - xmake/core/sandbox/modules/utils.lua | 8 ++++---- xmake/modules/private/utils/bin2obj.lua | 13 ++++++++++--- xmake/rules/utils/bin2obj/xmake.lua | 6 ++++++ 6 files changed, 66 insertions(+), 14 deletions(-) (limited to 'core/src') diff --git a/core/src/xmake/utils/bin2coff.c b/core/src/xmake/utils/bin2coff.c index be8a313ad..58c05cd33 100644 --- a/core/src/xmake/utils/bin2coff.c +++ b/core/src/xmake/utils/bin2coff.c @@ -155,7 +155,8 @@ static tb_bool_t xm_utils_bin2coff_dump(tb_stream_ref_t istream, tb_stream_ref_t ostream, tb_char_t const *symbol_prefix, tb_char_t const *arch, - tb_char_t const *basename) { + tb_char_t const *basename, + tb_bool_t zeroend) { tb_assert_and_check_return_val(istream && ostream, tb_false); // get file size @@ -164,6 +165,13 @@ static tb_bool_t xm_utils_bin2coff_dump(tb_stream_ref_t istream, return tb_false; } tb_uint32_t datasize = (tb_uint32_t)filesize; + // add null terminator if zeroend is true + if (zeroend) { + if (datasize >= 0xffffffffU) { + return tb_false; // would overflow + } + datasize++; + } // generate symbol names from filename tb_char_t symbol_name[256] = {0}; @@ -254,6 +262,13 @@ static tb_bool_t xm_utils_bin2coff_dump(tb_stream_ref_t istream, } left -= to_read; } + // append null terminator if zeroend is true + if (zeroend) { + tb_byte_t zero = 0; + if (!tb_stream_bwrit(ostream, &zero, 1)) { + return tb_false; + } + } // align to 4 bytes tb_uint32_t padding = (4 - (section_data_size & 3)) & 3; @@ -351,6 +366,12 @@ tb_int_t xm_utils_bin2coff(lua_State *lua) { // get basename (optional) tb_char_t const *basename = lua_isstring(lua, 5) ? lua_tostring(lua, 5) : tb_null; + // get platform (optional, not used but passed for consistency) + tb_char_t const *plat = lua_isstring(lua, 6) ? lua_tostring(lua, 6) : tb_null; + + // get zeroend (optional, default: false) + tb_bool_t zeroend = lua_toboolean(lua, 7); + // do dump tb_bool_t ok = tb_false; tb_stream_ref_t istream = tb_stream_init_from_file(binaryfile, TB_FILE_MODE_RO); @@ -369,7 +390,7 @@ tb_int_t xm_utils_bin2coff(lua_State *lua) { break; } - if (!xm_utils_bin2coff_dump(istream, ostream, symbol_prefix, arch, basename)) { + if (!xm_utils_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/bin2macho.c b/core/src/xmake/utils/bin2macho.c index 5499ee103..c0798fa02 100644 --- a/core/src/xmake/utils/bin2macho.c +++ b/core/src/xmake/utils/bin2macho.c @@ -300,7 +300,8 @@ static tb_bool_t xm_utils_bin2macho_dump_64(tb_stream_ref_t istream, tb_char_t const *arch, tb_char_t const *basename, tb_uint32_t minos, - tb_uint32_t sdk) { + tb_uint32_t sdk, + tb_bool_t zeroend) { tb_assert_and_check_return_val(istream && ostream, tb_false); // get file size @@ -309,6 +310,13 @@ static tb_bool_t xm_utils_bin2macho_dump_64(tb_stream_ref_t istream, return tb_false; } tb_uint32_t datasize = (tb_uint32_t)filesize; + // add null terminator if zeroend is true + if (zeroend) { + if (datasize >= 0xffffffffU) { + return tb_false; // would overflow + } + datasize++; + } // generate symbol names from filename tb_char_t symbol_name[256] = {0}; @@ -458,6 +466,13 @@ static tb_bool_t xm_utils_bin2macho_dump_64(tb_stream_ref_t istream, } left -= to_read; } + // append null terminator if zeroend is true + if (zeroend) { + tb_byte_t zero = 0; + if (!tb_stream_bwrit(ostream, &zero, 1)) { + return tb_false; + } + } // align to 8 bytes padding = symtab_offset - data_end_offset; @@ -539,9 +554,10 @@ static tb_bool_t xm_utils_bin2macho_dump(tb_stream_ref_t istream, tb_char_t const *arch, tb_char_t const *basename, tb_uint32_t minos, - tb_uint32_t sdk) { + 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); + return xm_utils_bin2macho_dump_64(istream, ostream, symbol_prefix, plat, arch, basename, minos, sdk, zeroend); } else { // 32-bit not implemented yet return tb_false; @@ -587,6 +603,9 @@ tb_int_t xm_utils_bin2macho(lua_State *lua) { 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); + // get zeroend (optional, default: false) + tb_bool_t zeroend = lua_toboolean(lua, 9); + // do dump tb_bool_t ok = tb_false; tb_stream_ref_t istream = tb_stream_init_from_file(binaryfile, TB_FILE_MODE_RO); @@ -605,7 +624,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)) { + if (!xm_utils_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/tests/projects/other/bin2obj/xmake.lua b/tests/projects/other/bin2obj/xmake.lua index bcd64fff5..e20a329e1 100644 --- a/tests/projects/other/bin2obj/xmake.lua +++ b/tests/projects/other/bin2obj/xmake.lua @@ -6,4 +6,3 @@ target("test") add_files("src/*.c") add_files("src/*.bin") add_files("src/*.png") - diff --git a/xmake/core/sandbox/modules/utils.lua b/xmake/core/sandbox/modules/utils.lua index b3453cb92..3601c1470 100644 --- a/xmake/core/sandbox/modules/utils.lua +++ b/xmake/core/sandbox/modules/utils.lua @@ -160,8 +160,8 @@ end -- generate COFF object file from the binary file if utils.bin2coff then - function sandbox_utils.bin2coff(binaryfile, outputfile, symbol_prefix, arch, basename) - local ok, errors = utils.bin2coff(binaryfile, outputfile, symbol_prefix, arch, basename) + function sandbox_utils.bin2coff(binaryfile, outputfile, symbol_prefix, arch, basename, plat, zeroend) + local ok, errors = utils.bin2coff(binaryfile, outputfile, symbol_prefix, arch, basename, plat, zeroend) if not ok then os.raise(errors) end @@ -170,8 +170,8 @@ 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) - local ok, errors = utils.bin2macho(binaryfile, outputfile, symbol_prefix, plat, arch, basename, target_minver, xcode_sdkver) + 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 diff --git a/xmake/modules/private/utils/bin2obj.lua b/xmake/modules/private/utils/bin2obj.lua index a41c919fe..2e2e4bd94 100644 --- a/xmake/modules/private/utils/bin2obj.lua +++ b/xmake/modules/private/utils/bin2obj.lua @@ -29,7 +29,8 @@ local options = { {'a', "arch", "kv", nil, "Set the target architecture."}, {'p', "plat", "kv", nil, "Set the target platform (macosx, iphoneos, etc.)."}, {nil, "target_minver", "kv", nil, "Set the target minimum version (e.g., 10.0, 18.2)."}, - {nil, "xcode_sdkver", "kv", nil, "Set the Xcode SDK version (e.g., 10.0, 18.2)."} + {nil, "xcode_sdkver", "kv", nil, "Set the Xcode SDK version (e.g., 10.0, 18.2)."}, + {nil, "zeroend", "k", nil, "Append a null terminator ('\\0') at the end of data."} } function _do_bin2obj_coff(binarypath, outputpath, opt) @@ -42,6 +43,9 @@ function _do_bin2obj_coff(binarypath, outputpath, opt) -- get platform (not used for COFF, but passed for consistency) local platform = opt.plat + -- 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) @@ -52,7 +56,7 @@ function _do_bin2obj_coff(binarypath, outputpath, opt) -- do dump if utils.bin2coff then - utils.bin2coff(binarypath, outputpath, symbol_prefix, arch, basename) + utils.bin2coff(binarypath, outputpath, symbol_prefix, arch, basename, platform, zeroend) else raise("bin2obj: utils.bin2coff not available (C implementation not compiled)") end @@ -81,6 +85,9 @@ function _do_bin2obj_macho(binarypath, outputpath, opt) -- 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) @@ -91,7 +98,7 @@ function _do_bin2obj_macho(binarypath, outputpath, opt) -- do dump if utils.bin2macho then - utils.bin2macho(binarypath, outputpath, symbol_prefix, plat, arch, basename, target_minver, xcode_sdkver) + utils.bin2macho(binarypath, outputpath, symbol_prefix, plat, arch, basename, target_minver, xcode_sdkver, zeroend) else raise("bin2obj: utils.bin2macho not available (C implementation not compiled)") end diff --git a/xmake/rules/utils/bin2obj/xmake.lua b/xmake/rules/utils/bin2obj/xmake.lua index 1f8e6e0b2..53ceb6a51 100644 --- a/xmake/rules/utils/bin2obj/xmake.lua +++ b/xmake/rules/utils/bin2obj/xmake.lua @@ -47,6 +47,9 @@ rule("utils.bin2obj") -- get symbol prefix (default: _binary_) local symbol_prefix = target:extraconf("rules", "utils.bin2obj", "symbol_prefix") or "_binary_" + -- get zeroend (default: false) + local zeroend = target:extraconf("rules", "utils.bin2obj", "zeroend") or false + -- get architecture local arch = target:arch() @@ -81,6 +84,9 @@ rule("utils.bin2obj") if xcode_sdkver then table.insert(argv, "--xcode_sdkver=" .. xcode_sdkver) end + if zeroend then + table.insert(argv, "--zeroend") + end batchcmds:vlua("private.utils.bin2obj", argv) -- add deps -- cgit v1.3.1 From 282349621e4e7c02fa3cb7a6df2d523fbba3c9d7 Mon Sep 17 00:00:00 2001 From: ruki Date: Sun, 7 Dec 2025 12:19:22 +0800 Subject: add file zeroend support --- core/src/xmake/utils/bin2macho.c | 2 +- tests/projects/other/bin2obj/xmake.lua | 2 +- xmake/rules/utils/bin2obj/xmake.lua | 4 +++- 3 files changed, 5 insertions(+), 3 deletions(-) (limited to 'core/src') diff --git a/core/src/xmake/utils/bin2macho.c b/core/src/xmake/utils/bin2macho.c index c0798fa02..07c6fc347 100644 --- a/core/src/xmake/utils/bin2macho.c +++ b/core/src/xmake/utils/bin2macho.c @@ -534,7 +534,7 @@ static tb_bool_t xm_utils_bin2macho_dump_64(tb_stream_ref_t istream, tb_stream_bwrit(ostream, &null, 1); // align string table to 8 bytes - padding = strtab_size - (4 + start_len + 1 + end_len + 1); + padding = strtab_size - (4 + (tb_uint32_t)start_len + 1 + (tb_uint32_t)end_len + 1); if (padding > 0) { tb_byte_t zero = 0; while (padding-- > 0) { diff --git a/tests/projects/other/bin2obj/xmake.lua b/tests/projects/other/bin2obj/xmake.lua index e20a329e1..3f00aebf9 100644 --- a/tests/projects/other/bin2obj/xmake.lua +++ b/tests/projects/other/bin2obj/xmake.lua @@ -4,5 +4,5 @@ target("test") set_kind("binary") add_rules("utils.bin2obj", {extensions = {".bin", ".png"}}) add_files("src/*.c") - add_files("src/*.bin") + add_files("src/data.bin", {zeroend = true}) add_files("src/*.png") diff --git a/xmake/rules/utils/bin2obj/xmake.lua b/xmake/rules/utils/bin2obj/xmake.lua index 53ceb6a51..d35e50606 100644 --- a/xmake/rules/utils/bin2obj/xmake.lua +++ b/xmake/rules/utils/bin2obj/xmake.lua @@ -48,7 +48,9 @@ rule("utils.bin2obj") local symbol_prefix = target:extraconf("rules", "utils.bin2obj", "symbol_prefix") or "_binary_" -- get zeroend (default: false) - local zeroend = target:extraconf("rules", "utils.bin2obj", "zeroend") or false + -- check file-level config first, then rule-level config + local fileconfig = target:fileconfig(sourcefile_bin) + local zeroend = (fileconfig and fileconfig.zeroend) or target:extraconf("rules", "utils.bin2obj", "zeroend") or false -- get architecture local arch = target:arch() -- cgit v1.3.1 From b18cf3e29067c0b9159880bf2028eb5555176841 Mon Sep 17 00:00:00 2001 From: ruki Date: Sun, 7 Dec 2025 12:48:43 +0800 Subject: improve to copy stream data --- core/src/xmake/utils/bin2coff.c | 18 +++------------ core/src/xmake/utils/bin2macho.c | 13 ++--------- core/src/xmake/utils/prefix.h | 40 +++++++++++++++++++++++++++++++++ xmake/core/sandbox/modules/utils.lua | 4 ++-- xmake/modules/private/utils/bin2obj.lua | 5 +---- 5 files changed, 48 insertions(+), 32 deletions(-) (limited to 'core/src') diff --git a/core/src/xmake/utils/bin2coff.c b/core/src/xmake/utils/bin2coff.c index 58c05cd33..25c54035c 100644 --- a/core/src/xmake/utils/bin2coff.c +++ b/core/src/xmake/utils/bin2coff.c @@ -250,17 +250,8 @@ static tb_bool_t xm_utils_bin2coff_dump(tb_stream_ref_t istream, } // write section data - tb_byte_t buffer[8192]; - tb_hong_t left = filesize; - while (left > 0) { - tb_size_t to_read = (tb_size_t)tb_min(left, (tb_hong_t)sizeof(buffer)); - if (!tb_stream_bread(istream, buffer, to_read)) { - return tb_false; - } - if (!tb_stream_bwrit(ostream, buffer, to_read)) { - return tb_false; - } - left -= to_read; + if (!xm_utils_stream_copy(istream, ostream, filesize)) { + return tb_false; } // append null terminator if zeroend is true if (zeroend) { @@ -366,11 +357,8 @@ tb_int_t xm_utils_bin2coff(lua_State *lua) { // get basename (optional) tb_char_t const *basename = lua_isstring(lua, 5) ? lua_tostring(lua, 5) : tb_null; - // get platform (optional, not used but passed for consistency) - tb_char_t const *plat = lua_isstring(lua, 6) ? lua_tostring(lua, 6) : tb_null; - // get zeroend (optional, default: false) - tb_bool_t zeroend = lua_toboolean(lua, 7); + tb_bool_t zeroend = lua_toboolean(lua, 6); // do dump tb_bool_t ok = tb_false; diff --git a/core/src/xmake/utils/bin2macho.c b/core/src/xmake/utils/bin2macho.c index 07c6fc347..50b03c5e3 100644 --- a/core/src/xmake/utils/bin2macho.c +++ b/core/src/xmake/utils/bin2macho.c @@ -454,17 +454,8 @@ static tb_bool_t xm_utils_bin2macho_dump_64(tb_stream_ref_t istream, } // write section data - tb_byte_t buffer[8192]; - tb_hong_t left = filesize; - while (left > 0) { - tb_size_t to_read = (tb_size_t)tb_min(left, (tb_hong_t)sizeof(buffer)); - if (!tb_stream_bread(istream, buffer, to_read)) { - return tb_false; - } - if (!tb_stream_bwrit(ostream, buffer, to_read)) { - return tb_false; - } - left -= to_read; + if (!xm_utils_stream_copy(istream, ostream, filesize)) { + return tb_false; } // append null terminator if zeroend is true if (zeroend) { diff --git a/core/src/xmake/utils/prefix.h b/core/src/xmake/utils/prefix.h index a9f509e72..077b4027f 100644 --- a/core/src/xmake/utils/prefix.h +++ b/core/src/xmake/utils/prefix.h @@ -26,4 +26,44 @@ */ #include "../prefix.h" +/* ////////////////////////////////////////////////////////////////////////////////////// + * inline implementation + */ + +/* copy data from input stream to output stream + * + * @param istream the input stream + * @param ostream the output stream + * @param size the size to copy + * + * @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) { + tb_assert_and_check_return_val(istream && ostream && size > 0, tb_false); + + tb_byte_t data[TB_STREAM_BLOCK_MAXN]; + tb_hize_t writ = 0; + do { + tb_size_t need = (tb_size_t)tb_min(size - writ, (tb_hize_t)TB_STREAM_BLOCK_MAXN); + tb_check_break(need); + + tb_long_t real = tb_stream_read(istream, data, need); + if (real > 0) { + if (!tb_stream_bwrit(ostream, data, (tb_size_t)real)) { + return tb_false; + } + writ += real; + } else if (!real) { + tb_long_t wait = tb_stream_wait(istream, TB_STREAM_WAIT_READ, tb_stream_timeout(istream)); + tb_check_break(wait > 0 && (wait & TB_STREAM_WAIT_READ)); + } else { + return tb_false; + } + + tb_check_break(writ < size); + } while (1); + + return tb_true; +} + #endif diff --git a/xmake/core/sandbox/modules/utils.lua b/xmake/core/sandbox/modules/utils.lua index 3601c1470..e1017863b 100644 --- a/xmake/core/sandbox/modules/utils.lua +++ b/xmake/core/sandbox/modules/utils.lua @@ -160,8 +160,8 @@ end -- generate COFF object file from the binary file if utils.bin2coff then - function sandbox_utils.bin2coff(binaryfile, outputfile, symbol_prefix, arch, basename, plat, zeroend) - local ok, errors = utils.bin2coff(binaryfile, outputfile, symbol_prefix, arch, basename, plat, zeroend) + 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 diff --git a/xmake/modules/private/utils/bin2obj.lua b/xmake/modules/private/utils/bin2obj.lua index 2e2e4bd94..9b8dfa4c2 100644 --- a/xmake/modules/private/utils/bin2obj.lua +++ b/xmake/modules/private/utils/bin2obj.lua @@ -40,9 +40,6 @@ function _do_bin2obj_coff(binarypath, outputpath, opt) -- get architecture (default: x86_64) local arch = opt.arch or "x86_64" - -- get platform (not used for COFF, but passed for consistency) - local platform = opt.plat - -- get zeroend (default: false) local zeroend = opt.zeroend or false @@ -56,7 +53,7 @@ function _do_bin2obj_coff(binarypath, outputpath, opt) -- do dump if utils.bin2coff then - utils.bin2coff(binarypath, outputpath, symbol_prefix, arch, basename, platform, zeroend) + utils.bin2coff(binarypath, outputpath, symbol_prefix, arch, basename, zeroend) else raise("bin2obj: utils.bin2coff not available (C implementation not compiled)") end -- cgit v1.3.1 From 832de3edc75ba2bb1e6ea4f26ee39513f35d4bd4 Mon Sep 17 00:00:00 2001 From: ruki Date: Sun, 7 Dec 2025 14:19:46 +0800 Subject: add bin2elf --- core/src/xmake/engine.c | 2 + core/src/xmake/utils/bin2elf.c | 469 ++++++++++++++++++++++++++++++++ xmake/core/sandbox/modules/utils.lua | 10 + xmake/modules/private/utils/bin2obj.lua | 27 +- 4 files changed, 507 insertions(+), 1 deletion(-) create mode 100644 core/src/xmake/utils/bin2elf.c (limited to 'core/src') diff --git a/core/src/xmake/engine.c b/core/src/xmake/engine.c index 802543700..71a4d4b95 100644 --- a/core/src/xmake/engine.c +++ b/core/src/xmake/engine.c @@ -338,6 +338,7 @@ tb_int_t xm_package_loadxmi(lua_State *lua); 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); #ifdef XM_CONFIG_API_HAVE_CURSES // register curses functions @@ -659,6 +660,7 @@ static luaL_Reg const g_utils_functions[] = { { "bin2c", xm_utils_bin2c }, { "bin2coff", xm_utils_bin2coff }, { "bin2macho", xm_utils_bin2macho }, + { "bin2elf", xm_utils_bin2elf }, { tb_null, tb_null }, }; diff --git a/core/src/xmake/utils/bin2elf.c b/core/src/xmake/utils/bin2elf.c new file mode 100644 index 000000000..a55cf8b1f --- /dev/null +++ b/core/src/xmake/utils/bin2elf.c @@ -0,0 +1,469 @@ +/*!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 bin2elf.c + * + */ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * trace + */ +#define TB_TRACE_MODULE_NAME "bin2elf" +#define TB_TRACE_MODULE_DEBUG (0) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ +#define XM_ELF_MACHINE_X86_64 0x3e +#define XM_ELF_MACHINE_ARM64 0xb7 +#define XM_ELF_MACHINE_ARM 0x28 +#define XM_ELF_MACHINE_I386 0x03 + +#define XM_ELF_SHT_PROGBITS 0x1 +#define XM_ELF_SHT_SYMTAB 0x2 +#define XM_ELF_SHT_STRTAB 0x3 + +#define XM_ELF_SHF_ALLOC 0x2 +#define XM_ELF_SHF_WRITE 0x1 + +#define XM_ELF_STB_GLOBAL 0x1 +#define XM_ELF_STT_OBJECT 0x1 + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ +#include "tbox/prefix/packed.h" +typedef struct __xm_elf64_header_t { + tb_uint8_t e_ident[16]; + tb_uint16_t e_type; + tb_uint16_t e_machine; + tb_uint32_t e_version; + tb_uint64_t e_entry; + tb_uint64_t e_phoff; + tb_uint64_t e_shoff; + tb_uint32_t e_flags; + tb_uint16_t e_ehsize; + tb_uint16_t e_phentsize; + tb_uint16_t e_phnum; + tb_uint16_t e_shentsize; + tb_uint16_t e_shnum; + tb_uint16_t e_shstrndx; +} __tb_packed__ xm_elf64_header_t; + +typedef struct __xm_elf64_section_t { + tb_uint32_t sh_name; + tb_uint32_t sh_type; + tb_uint64_t sh_flags; + tb_uint64_t sh_addr; + tb_uint64_t sh_offset; + tb_uint64_t sh_size; + tb_uint32_t sh_link; + tb_uint32_t sh_info; + tb_uint64_t sh_addralign; + tb_uint64_t sh_entsize; +} __tb_packed__ xm_elf64_section_t; + +typedef struct __xm_elf64_symbol_t { + tb_uint32_t st_name; + tb_uint8_t st_info; + tb_uint8_t st_other; + tb_uint16_t st_shndx; + tb_uint64_t st_value; + tb_uint64_t st_size; +} __tb_packed__ xm_elf64_symbol_t; + +#include "tbox/prefix/packed.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * private implementation + */ +static tb_uint16_t xm_utils_bin2elf_get_machine(tb_char_t const *arch) { + if (!arch) { + return XM_ELF_MACHINE_X86_64; + } + if (tb_strcmp(arch, "x86_64") == 0 || tb_strcmp(arch, "x64") == 0) { + return XM_ELF_MACHINE_X86_64; + } else if (tb_strcmp(arch, "arm64") == 0 || tb_strcmp(arch, "aarch64") == 0) { + return XM_ELF_MACHINE_ARM64; + } else if (tb_strcmp(arch, "arm") == 0 || tb_strcmp(arch, "armv7") == 0) { + return XM_ELF_MACHINE_ARM; + } else if (tb_strcmp(arch, "i386") == 0 || tb_strcmp(arch, "x86") == 0) { + return XM_ELF_MACHINE_I386; + } + return XM_ELF_MACHINE_X86_64; +} + +static tb_bool_t xm_utils_bin2elf_dump(tb_stream_ref_t istream, + tb_stream_ref_t ostream, + tb_char_t const *symbol_prefix, + tb_char_t const *arch, + tb_char_t const *basename, + tb_bool_t zeroend) { + tb_assert_and_check_return_val(istream && ostream, tb_false); + + // get file size + tb_hong_t filesize = tb_stream_size(istream); + if (filesize < 0 || filesize > 0xffffffffU) { + return tb_false; + } + tb_uint32_t datasize = (tb_uint32_t)filesize; + // add null terminator if zeroend is true + if (zeroend) { + if (datasize >= 0xffffffffU) { + return tb_false; // would overflow + } + datasize++; + } + + // generate symbol names from filename + tb_char_t symbol_name[256] = {0}; + tb_char_t symbol_start[256] = {0}; + tb_char_t symbol_end[256] = {0}; + + // use basename or default to "data" + if (!basename || !basename[0]) { + basename = "data"; + } + + // build symbol name + if (symbol_prefix) { + tb_snprintf(symbol_name, sizeof(symbol_name), "%s%s", symbol_prefix, basename); + } else { + tb_snprintf(symbol_name, sizeof(symbol_name), "_binary_%s", basename); + } + + // replace non-alphanumeric with underscore + for (tb_size_t i = 0; symbol_name[i]; i++) { + if (!tb_isalpha(symbol_name[i]) && !tb_isdigit(symbol_name[i]) && symbol_name[i] != '_') { + symbol_name[i] = '_'; + } + } + + tb_snprintf(symbol_start, sizeof(symbol_start), "%s_start", symbol_name); + tb_snprintf(symbol_end, sizeof(symbol_end), "%s_end", symbol_name); + + // calculate offsets + tb_uint32_t header_size = sizeof(xm_elf64_header_t); + tb_uint32_t section_header_size = sizeof(xm_elf64_section_t); + tb_uint32_t section_count = 5; // NULL, .rodata, .symtab, .strtab, .shstrtab + tb_uint32_t section_headers_ofs = header_size; + tb_uint32_t rodata_ofs = section_headers_ofs + section_count * section_header_size; + tb_uint32_t rodata_size = datasize; + tb_uint32_t rodata_padding = (8 - (rodata_size & 7)) & 7; + tb_uint32_t symtab_ofs = rodata_ofs + rodata_size + rodata_padding; + tb_uint32_t symtab_size = 3 * sizeof(xm_elf64_symbol_t); // NULL, start, end + tb_uint32_t symtab_padding = (8 - (symtab_size & 7)) & 7; + tb_uint32_t strtab_ofs = symtab_ofs + symtab_size + symtab_padding; + + // calculate string table size + tb_size_t start_len = tb_strlen(symbol_start); + tb_size_t end_len = tb_strlen(symbol_end); + tb_uint32_t strtab_size = 1; // initial null byte + strtab_size += (tb_uint32_t)(start_len + 1); + strtab_size += (tb_uint32_t)(end_len + 1); + tb_uint32_t strtab_padding = (8 - (strtab_size & 7)) & 7; + + // calculate section header string table size + tb_uint32_t shstrtab_size = 1; // initial null byte + shstrtab_size += 8; // ".rodata\0" (7 + 1) + shstrtab_size += 8; // ".symtab\0" (7 + 1) + shstrtab_size += 8; // ".strtab\0" (7 + 1) + shstrtab_size += 10; // ".shstrtab\0" (9 + 1) + tb_uint32_t shstrtab_ofs = strtab_ofs + strtab_size + strtab_padding; + + // write ELF header + xm_elf64_header_t header; + tb_memset(&header, 0, sizeof(header)); + header.e_ident[0] = 0x7f; + header.e_ident[1] = 'E'; + header.e_ident[2] = 'L'; + header.e_ident[3] = 'F'; + header.e_ident[4] = 2; // ELFCLASS64 + header.e_ident[5] = 1; // ELFDATA2LSB + 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_version = 1; + header.e_shoff = section_headers_ofs; + header.e_ehsize = header_size; + header.e_shentsize = section_header_size; + header.e_shnum = section_count; + header.e_shstrndx = 4; // .shstrtab section index + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&header, sizeof(header))) { + return tb_false; + } + + // write section headers + xm_elf64_section_t section_null; + tb_memset(§ion_null, 0, sizeof(section_null)); + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)§ion_null, sizeof(section_null))) { + return tb_false; + } + + // write .rodata section header + xm_elf64_section_t section_rodata; + tb_memset(§ion_rodata, 0, sizeof(section_rodata)); + section_rodata.sh_name = 1; // ".rodata" in shstrtab + section_rodata.sh_type = XM_ELF_SHT_PROGBITS; + section_rodata.sh_flags = XM_ELF_SHF_ALLOC; + section_rodata.sh_offset = rodata_ofs; + section_rodata.sh_size = rodata_size; + section_rodata.sh_addralign = 8; + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)§ion_rodata, sizeof(section_rodata))) { + return tb_false; + } + + // write .symtab section header + xm_elf64_section_t section_symtab; + tb_memset(§ion_symtab, 0, sizeof(section_symtab)); + section_symtab.sh_name = 9; // ".symtab" in shstrtab + section_symtab.sh_type = XM_ELF_SHT_SYMTAB; + section_symtab.sh_offset = symtab_ofs; + section_symtab.sh_size = symtab_size; + section_symtab.sh_link = 3; // .strtab section index + section_symtab.sh_info = 1; // first global symbol index + section_symtab.sh_addralign = 8; + section_symtab.sh_entsize = sizeof(xm_elf64_symbol_t); + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)§ion_symtab, sizeof(section_symtab))) { + return tb_false; + } + + // write .strtab section header + xm_elf64_section_t section_strtab; + tb_memset(§ion_strtab, 0, sizeof(section_strtab)); + section_strtab.sh_name = 17; // ".strtab" in shstrtab + section_strtab.sh_type = XM_ELF_SHT_STRTAB; + section_strtab.sh_offset = strtab_ofs; + section_strtab.sh_size = strtab_size; + section_strtab.sh_addralign = 1; + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)§ion_strtab, sizeof(section_strtab))) { + return tb_false; + } + + // write .shstrtab section header + xm_elf64_section_t section_shstrtab; + tb_memset(§ion_shstrtab, 0, sizeof(section_shstrtab)); + section_shstrtab.sh_name = 25; // ".shstrtab" in shstrtab + section_shstrtab.sh_type = XM_ELF_SHT_STRTAB; + section_shstrtab.sh_offset = shstrtab_ofs; // points to initial null byte + section_shstrtab.sh_size = shstrtab_size; // size includes initial null and all strings + section_shstrtab.sh_addralign = 1; + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)§ion_shstrtab, sizeof(section_shstrtab))) { + return tb_false; + } + + // write .rodata section data + if (!xm_utils_stream_copy(istream, ostream, filesize)) { + return tb_false; + } + // append null terminator if zeroend is true + if (zeroend) { + tb_byte_t zero = 0; + if (!tb_stream_bwrit(ostream, &zero, 1)) { + return tb_false; + } + } + + // align .rodata to 8 bytes + if (rodata_padding > 0) { + tb_byte_t zero = 0; + while (rodata_padding-- > 0) { + if (!tb_stream_bwrit(ostream, &zero, 1)) { + return tb_false; + } + } + } + + // write symbol table + // symbol 0: NULL symbol + xm_elf64_symbol_t sym_null; + tb_memset(&sym_null, 0, sizeof(sym_null)); + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&sym_null, sizeof(sym_null))) { + return tb_false; + } + + // symbol 1: _binary_xxx_start + xm_elf64_symbol_t sym_start; + tb_memset(&sym_start, 0, sizeof(sym_start)); + sym_start.st_name = 1; // offset in .strtab (after initial null) + sym_start.st_info = (XM_ELF_STB_GLOBAL << 4) | XM_ELF_STT_OBJECT; + sym_start.st_shndx = 1; // .rodata section index + sym_start.st_value = 0; + sym_start.st_size = 0; + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&sym_start, sizeof(sym_start))) { + return tb_false; + } + + // symbol 2: _binary_xxx_end + xm_elf64_symbol_t sym_end; + tb_memset(&sym_end, 0, sizeof(sym_end)); + sym_end.st_name = 1 + (tb_uint32_t)(start_len + 1); // offset in .strtab + sym_end.st_info = (XM_ELF_STB_GLOBAL << 4) | XM_ELF_STT_OBJECT; + sym_end.st_shndx = 1; // .rodata section index + sym_end.st_value = rodata_size; + sym_end.st_size = 0; + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&sym_end, sizeof(sym_end))) { + return tb_false; + } + + // align .symtab to 8 bytes + if (symtab_padding > 0) { + tb_byte_t zero = 0; + while (symtab_padding-- > 0) { + if (!tb_stream_bwrit(ostream, &zero, 1)) { + return tb_false; + } + } + } + + // write string table + tb_byte_t null = 0; + if (!tb_stream_bwrit(ostream, &null, 1)) { + return tb_false; + } + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)symbol_start, start_len)) { + return tb_false; + } + if (!tb_stream_bwrit(ostream, &null, 1)) { + return tb_false; + } + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)symbol_end, end_len)) { + return tb_false; + } + if (!tb_stream_bwrit(ostream, &null, 1)) { + return tb_false; + } + + // align .strtab to 8 bytes + if (strtab_padding > 0) { + tb_byte_t zero = 0; + while (strtab_padding-- > 0) { + if (!tb_stream_bwrit(ostream, &zero, 1)) { + return tb_false; + } + } + } + + // write section header string table + if (!tb_stream_bwrit(ostream, &null, 1)) { + return tb_false; + } + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)".rodata", 7)) { + return tb_false; + } + if (!tb_stream_bwrit(ostream, &null, 1)) { + return tb_false; + } + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)".symtab", 7)) { + return tb_false; + } + if (!tb_stream_bwrit(ostream, &null, 1)) { + return tb_false; + } + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)".strtab", 7)) { + return tb_false; + } + if (!tb_stream_bwrit(ostream, &null, 1)) { + return tb_false; + } + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)".shstrtab", 9)) { + return tb_false; + } + if (!tb_stream_bwrit(ostream, &null, 1)) { + return tb_false; + } + + return tb_true; +} + +/* ////////////////////////////////////////////////////////////////////////////////////// + * implementation + */ + +/* generate ELF object file from binary file + * + * local ok, errors = utils.bin2elf(binaryfile, outputfile, symbol_prefix, arch, basename, zeroend) + */ +tb_int_t xm_utils_bin2elf(lua_State *lua) { + tb_assert_and_check_return_val(lua, 0); + + // get the binaryfile + tb_char_t const *binaryfile = luaL_checkstring(lua, 1); + tb_check_return_val(binaryfile, 0); + + // get the outputfile + tb_char_t const *outputfile = luaL_checkstring(lua, 2); + tb_check_return_val(outputfile, 0); + + // get symbol prefix (optional) + tb_char_t const *symbol_prefix = lua_isstring(lua, 3) ? lua_tostring(lua, 3) : tb_null; + + // get arch (optional) + tb_char_t const *arch = lua_isstring(lua, 4) ? lua_tostring(lua, 4) : tb_null; + + // get basename (optional) + tb_char_t const *basename = lua_isstring(lua, 5) ? lua_tostring(lua, 5) : tb_null; + + // get zeroend (optional, default: false) + tb_bool_t zeroend = lua_toboolean(lua, 6); + + // do dump + tb_bool_t ok = tb_false; + tb_stream_ref_t istream = tb_stream_init_from_file(binaryfile, TB_FILE_MODE_RO); + tb_stream_ref_t ostream = tb_stream_init_from_file(outputfile, + TB_FILE_MODE_RW | TB_FILE_MODE_CREAT | TB_FILE_MODE_TRUNC); + do { + if (!tb_stream_open(istream)) { + lua_pushboolean(lua, tb_false); + lua_pushfstring(lua, "bin2elf: open %s failed", binaryfile); + break; + } + + if (!tb_stream_open(ostream)) { + lua_pushboolean(lua, tb_false); + lua_pushfstring(lua, "bin2elf: open %s failed", outputfile); + break; + } + + if (!xm_utils_bin2elf_dump(istream, ostream, symbol_prefix, arch, basename, zeroend)) { + lua_pushboolean(lua, tb_false); + lua_pushfstring(lua, "bin2elf: dump data failed"); + break; + } + + ok = tb_true; + lua_pushboolean(lua, ok); + + } while (0); + + if (istream) + tb_stream_clos(istream); + istream = tb_null; + + if (ostream) + tb_stream_clos(ostream); + ostream = tb_null; + + return ok ? 1 : 2; +} + diff --git a/xmake/core/sandbox/modules/utils.lua b/xmake/core/sandbox/modules/utils.lua index e1017863b..ef1361db9 100644 --- a/xmake/core/sandbox/modules/utils.lua +++ b/xmake/core/sandbox/modules/utils.lua @@ -178,6 +178,16 @@ if utils.bin2macho then 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/private/utils/bin2obj.lua b/xmake/modules/private/utils/bin2obj.lua index 9b8dfa4c2..caa8b2e22 100644 --- a/xmake/modules/private/utils/bin2obj.lua +++ b/xmake/modules/private/utils/bin2obj.lua @@ -63,7 +63,32 @@ function _do_bin2obj_coff(binarypath, outputpath, opt) end function _do_bin2obj_elf(binarypath, outputpath, opt) - raise("bin2obj: ELF format not yet implemented") + -- 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("%.", "_") + + -- 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) + else + raise("bin2obj: utils.bin2elf not available (C implementation not compiled)") + end + + -- trace + cprint("${bright}%s generated!", outputpath) end function _do_bin2obj_macho(binarypath, outputpath, opt) -- cgit v1.3.1 From 7e90fd41cd7f7c66052d2062ea7778d228225e80 Mon Sep 17 00:00:00 2001 From: ruki Date: Sun, 7 Dec 2025 14:22:14 +0800 Subject: suppress ld warnings for elf --- core/src/xmake/utils/bin2elf.c | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) (limited to 'core/src') diff --git a/core/src/xmake/utils/bin2elf.c b/core/src/xmake/utils/bin2elf.c index a55cf8b1f..aef69e312 100644 --- a/core/src/xmake/utils/bin2elf.c +++ b/core/src/xmake/utils/bin2elf.c @@ -164,7 +164,7 @@ static tb_bool_t xm_utils_bin2elf_dump(tb_stream_ref_t istream, // calculate offsets tb_uint32_t header_size = sizeof(xm_elf64_header_t); tb_uint32_t section_header_size = sizeof(xm_elf64_section_t); - tb_uint32_t section_count = 5; // NULL, .rodata, .symtab, .strtab, .shstrtab + tb_uint32_t section_count = 6; // NULL, .rodata, .symtab, .strtab, .shstrtab, .note.GNU-stack tb_uint32_t section_headers_ofs = header_size; tb_uint32_t rodata_ofs = section_headers_ofs + section_count * section_header_size; tb_uint32_t rodata_size = datasize; @@ -188,6 +188,7 @@ static tb_bool_t xm_utils_bin2elf_dump(tb_stream_ref_t istream, shstrtab_size += 8; // ".symtab\0" (7 + 1) shstrtab_size += 8; // ".strtab\0" (7 + 1) shstrtab_size += 10; // ".shstrtab\0" (9 + 1) + shstrtab_size += 16; // ".note.GNU-stack\0" (15 + 1) tb_uint32_t shstrtab_ofs = strtab_ofs + strtab_size + strtab_padding; // write ELF header @@ -272,6 +273,19 @@ static tb_bool_t xm_utils_bin2elf_dump(tb_stream_ref_t istream, return tb_false; } + // write .note.GNU-stack section header (empty section to mark stack as non-executable) + xm_elf64_section_t section_note_gnu_stack; + tb_memset(§ion_note_gnu_stack, 0, sizeof(section_note_gnu_stack)); + section_note_gnu_stack.sh_name = 35; // ".note.GNU-stack" in shstrtab (25 + 10) + section_note_gnu_stack.sh_type = XM_ELF_SHT_PROGBITS; + section_note_gnu_stack.sh_flags = 0; // no flags + section_note_gnu_stack.sh_offset = shstrtab_ofs + shstrtab_size; // after .shstrtab + section_note_gnu_stack.sh_size = 0; // empty section + section_note_gnu_stack.sh_addralign = 1; + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)§ion_note_gnu_stack, sizeof(section_note_gnu_stack))) { + return tb_false; + } + // write .rodata section data if (!xm_utils_stream_copy(istream, ostream, filesize)) { return tb_false; @@ -392,6 +406,12 @@ static tb_bool_t xm_utils_bin2elf_dump(tb_stream_ref_t istream, if (!tb_stream_bwrit(ostream, &null, 1)) { return tb_false; } + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)".note.GNU-stack", 15)) { + return tb_false; + } + if (!tb_stream_bwrit(ostream, &null, 1)) { + return tb_false; + } return tb_true; } -- cgit v1.3.1 From f9d8a7f4384bbf131623eba1205a3c280c8552bb Mon Sep 17 00:00:00 2001 From: ruki Date: Sun, 7 Dec 2025 15:05:58 +0800 Subject: improve bin2elf to support 32bits --- core/src/xmake/utils/bin2elf.c | 391 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 379 insertions(+), 12 deletions(-) (limited to 'core/src') diff --git a/core/src/xmake/utils/bin2elf.c b/core/src/xmake/utils/bin2elf.c index aef69e312..7fe8e5576 100644 --- a/core/src/xmake/utils/bin2elf.c +++ b/core/src/xmake/utils/bin2elf.c @@ -52,6 +52,45 @@ * types */ #include "tbox/prefix/packed.h" +typedef struct __xm_elf32_header_t { + tb_uint8_t e_ident[16]; + tb_uint16_t e_type; + tb_uint16_t e_machine; + tb_uint32_t e_version; + tb_uint32_t e_entry; + tb_uint32_t e_phoff; + tb_uint32_t e_shoff; + tb_uint32_t e_flags; + tb_uint16_t e_ehsize; + tb_uint16_t e_phentsize; + tb_uint16_t e_phnum; + tb_uint16_t e_shentsize; + tb_uint16_t e_shnum; + tb_uint16_t e_shstrndx; +} __tb_packed__ xm_elf32_header_t; + +typedef struct __xm_elf32_section_t { + tb_uint32_t sh_name; + tb_uint32_t sh_type; + tb_uint32_t sh_flags; + tb_uint32_t sh_addr; + tb_uint32_t sh_offset; + tb_uint32_t sh_size; + tb_uint32_t sh_link; + tb_uint32_t sh_info; + tb_uint32_t sh_addralign; + tb_uint32_t sh_entsize; +} __tb_packed__ xm_elf32_section_t; + +typedef struct __xm_elf32_symbol_t { + tb_uint32_t st_name; + tb_uint32_t st_value; + tb_uint32_t st_size; + tb_uint8_t st_info; + tb_uint8_t st_other; + tb_uint16_t st_shndx; +} __tb_packed__ xm_elf32_symbol_t; + typedef struct __xm_elf64_header_t { tb_uint8_t e_ident[16]; tb_uint16_t e_type; @@ -102,9 +141,11 @@ static tb_uint16_t xm_utils_bin2elf_get_machine(tb_char_t const *arch) { } if (tb_strcmp(arch, "x86_64") == 0 || tb_strcmp(arch, "x64") == 0) { return XM_ELF_MACHINE_X86_64; - } else if (tb_strcmp(arch, "arm64") == 0 || tb_strcmp(arch, "aarch64") == 0) { + } else if (tb_strcmp(arch, "arm64") == 0 || tb_strcmp(arch, "aarch64") == 0 || + tb_strcmp(arch, "arm64-v8a") == 0) { return XM_ELF_MACHINE_ARM64; - } else if (tb_strcmp(arch, "arm") == 0 || tb_strcmp(arch, "armv7") == 0) { + } else if (tb_strcmp(arch, "arm") == 0 || tb_strcmp(arch, "armv7") == 0 || + tb_strcmp(arch, "armeabi-v7a") == 0) { return XM_ELF_MACHINE_ARM; } else if (tb_strcmp(arch, "i386") == 0 || tb_strcmp(arch, "x86") == 0) { return XM_ELF_MACHINE_I386; @@ -112,12 +153,328 @@ 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_dump(tb_stream_ref_t istream, - tb_stream_ref_t ostream, - tb_char_t const *symbol_prefix, - tb_char_t const *arch, - tb_char_t const *basename, - tb_bool_t zeroend) { +static tb_bool_t xm_utils_bin2elf_is_64bit(tb_char_t const *arch) { + if (!arch) { + return tb_true; + } + if (tb_strcmp(arch, "x86_64") == 0 || tb_strcmp(arch, "x64") == 0 || + tb_strcmp(arch, "arm64") == 0 || tb_strcmp(arch, "aarch64") == 0 || + tb_strcmp(arch, "arm64-v8a") == 0) { + return tb_true; + } + return tb_false; +} + +static tb_bool_t xm_utils_bin2elf_dump_32(tb_stream_ref_t istream, + tb_stream_ref_t ostream, + tb_char_t const *symbol_prefix, + tb_char_t const *arch, + tb_char_t const *basename, + tb_bool_t zeroend) { + tb_assert_and_check_return_val(istream && ostream, tb_false); + + // get file size + tb_hong_t filesize = tb_stream_size(istream); + if (filesize < 0 || filesize > 0xffffffffU) { + return tb_false; + } + tb_uint32_t datasize = (tb_uint32_t)filesize; + // add null terminator if zeroend is true + if (zeroend) { + if (datasize >= 0xffffffffU) { + return tb_false; // would overflow + } + datasize++; + } + + // generate symbol names from filename + tb_char_t symbol_name[256] = {0}; + tb_char_t symbol_start[256] = {0}; + tb_char_t symbol_end[256] = {0}; + + // use basename or default to "data" + if (!basename || !basename[0]) { + basename = "data"; + } + + // build symbol name + if (symbol_prefix) { + tb_snprintf(symbol_name, sizeof(symbol_name), "%s%s", symbol_prefix, basename); + } else { + tb_snprintf(symbol_name, sizeof(symbol_name), "_binary_%s", basename); + } + + // replace non-alphanumeric with underscore + for (tb_size_t i = 0; symbol_name[i]; i++) { + if (!tb_isalpha(symbol_name[i]) && !tb_isdigit(symbol_name[i]) && symbol_name[i] != '_') { + symbol_name[i] = '_'; + } + } + + tb_snprintf(symbol_start, sizeof(symbol_start), "%s_start", symbol_name); + tb_snprintf(symbol_end, sizeof(symbol_end), "%s_end", symbol_name); + + // calculate offsets + tb_uint32_t header_size = sizeof(xm_elf32_header_t); + tb_uint32_t section_header_size = sizeof(xm_elf32_section_t); + tb_uint32_t section_count = 6; // NULL, .rodata, .symtab, .strtab, .shstrtab, .note.GNU-stack + tb_uint32_t section_headers_ofs = header_size; + tb_uint32_t rodata_ofs = section_headers_ofs + section_count * section_header_size; + tb_uint32_t rodata_size = datasize; + tb_uint32_t rodata_padding = (4 - (rodata_size & 3)) & 3; // align to 4 bytes for 32-bit + tb_uint32_t symtab_ofs = rodata_ofs + rodata_size + rodata_padding; + tb_uint32_t symtab_size = 3 * sizeof(xm_elf32_symbol_t); // NULL, start, end + tb_uint32_t symtab_padding = (4 - (symtab_size & 3)) & 3; // align to 4 bytes + tb_uint32_t strtab_ofs = symtab_ofs + symtab_size + symtab_padding; + + // calculate string table size + tb_size_t start_len = tb_strlen(symbol_start); + tb_size_t end_len = tb_strlen(symbol_end); + tb_uint32_t strtab_size = 1; // initial null byte + strtab_size += (tb_uint32_t)(start_len + 1); + strtab_size += (tb_uint32_t)(end_len + 1); + tb_uint32_t strtab_padding = (4 - (strtab_size & 3)) & 3; // align to 4 bytes + tb_uint32_t shstrtab_ofs = strtab_ofs + strtab_size + strtab_padding; + + // calculate section header string table size + tb_uint32_t shstrtab_size = 1; // initial null byte + shstrtab_size += 8; // ".rodata\0" (7 + 1) + shstrtab_size += 8; // ".symtab\0" (7 + 1) + shstrtab_size += 8; // ".strtab\0" (7 + 1) + shstrtab_size += 10; // ".shstrtab\0" (9 + 1) + shstrtab_size += 16; // ".note.GNU-stack\0" (15 + 1) + + // write ELF header + xm_elf32_header_t header; + tb_memset(&header, 0, sizeof(header)); + header.e_ident[0] = 0x7f; + header.e_ident[1] = 'E'; + header.e_ident[2] = 'L'; + header.e_ident[3] = 'F'; + header.e_ident[4] = 1; // ELFCLASS32 + header.e_ident[5] = 1; // ELFDATA2LSB + 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_version = 1; + header.e_shoff = section_headers_ofs; + header.e_ehsize = header_size; + header.e_shentsize = section_header_size; + header.e_shnum = section_count; + header.e_shstrndx = 4; // .shstrtab section index + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&header, sizeof(header))) { + return tb_false; + } + + // write section headers + xm_elf32_section_t section_null; + tb_memset(§ion_null, 0, sizeof(section_null)); + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)§ion_null, sizeof(section_null))) { + return tb_false; + } + + // write .rodata section header + xm_elf32_section_t section_rodata; + tb_memset(§ion_rodata, 0, sizeof(section_rodata)); + section_rodata.sh_name = 1; // ".rodata" in shstrtab + section_rodata.sh_type = XM_ELF_SHT_PROGBITS; + section_rodata.sh_flags = XM_ELF_SHF_ALLOC; + section_rodata.sh_offset = rodata_ofs; + section_rodata.sh_size = rodata_size; + section_rodata.sh_addralign = 4; + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)§ion_rodata, sizeof(section_rodata))) { + return tb_false; + } + + // write .symtab section header + xm_elf32_section_t section_symtab; + tb_memset(§ion_symtab, 0, sizeof(section_symtab)); + section_symtab.sh_name = 9; // ".symtab" in shstrtab + section_symtab.sh_type = XM_ELF_SHT_SYMTAB; + section_symtab.sh_offset = symtab_ofs; + section_symtab.sh_size = symtab_size; + section_symtab.sh_link = 3; // .strtab section index + section_symtab.sh_info = 1; // first global symbol index + section_symtab.sh_addralign = 4; + section_symtab.sh_entsize = sizeof(xm_elf32_symbol_t); + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)§ion_symtab, sizeof(section_symtab))) { + return tb_false; + } + + // write .strtab section header + xm_elf32_section_t section_strtab; + tb_memset(§ion_strtab, 0, sizeof(section_strtab)); + section_strtab.sh_name = 17; // ".strtab" in shstrtab + section_strtab.sh_type = XM_ELF_SHT_STRTAB; + section_strtab.sh_offset = strtab_ofs; + section_strtab.sh_size = strtab_size; + section_strtab.sh_addralign = 1; + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)§ion_strtab, sizeof(section_strtab))) { + return tb_false; + } + + // write .shstrtab section header + xm_elf32_section_t section_shstrtab; + tb_memset(§ion_shstrtab, 0, sizeof(section_shstrtab)); + section_shstrtab.sh_name = 25; // ".shstrtab" in shstrtab + section_shstrtab.sh_type = XM_ELF_SHT_STRTAB; + section_shstrtab.sh_offset = shstrtab_ofs; + section_shstrtab.sh_size = shstrtab_size; + section_shstrtab.sh_addralign = 1; + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)§ion_shstrtab, sizeof(section_shstrtab))) { + return tb_false; + } + + // write .note.GNU-stack section header (empty section to mark stack as non-executable) + xm_elf32_section_t section_note_gnu_stack; + tb_memset(§ion_note_gnu_stack, 0, sizeof(section_note_gnu_stack)); + section_note_gnu_stack.sh_name = 35; // ".note.GNU-stack" in shstrtab (25 + 10) + section_note_gnu_stack.sh_type = XM_ELF_SHT_PROGBITS; + section_note_gnu_stack.sh_flags = 0; // no flags + section_note_gnu_stack.sh_offset = shstrtab_ofs + shstrtab_size; // after .shstrtab + section_note_gnu_stack.sh_size = 0; // empty section + section_note_gnu_stack.sh_addralign = 1; + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)§ion_note_gnu_stack, sizeof(section_note_gnu_stack))) { + return tb_false; + } + + // write .rodata section data + if (!xm_utils_stream_copy(istream, ostream, filesize)) { + return tb_false; + } + // append null terminator if zeroend is true + if (zeroend) { + tb_byte_t zero = 0; + if (!tb_stream_bwrit(ostream, &zero, 1)) { + return tb_false; + } + } + + // align .rodata to 4 bytes + if (rodata_padding > 0) { + tb_byte_t zero = 0; + while (rodata_padding-- > 0) { + if (!tb_stream_bwrit(ostream, &zero, 1)) { + return tb_false; + } + } + } + + // write symbol table + // symbol 0: NULL symbol + xm_elf32_symbol_t sym_null; + tb_memset(&sym_null, 0, sizeof(sym_null)); + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&sym_null, sizeof(sym_null))) { + return tb_false; + } + + // symbol 1: _binary_xxx_start + xm_elf32_symbol_t sym_start; + tb_memset(&sym_start, 0, sizeof(sym_start)); + sym_start.st_name = 1; // offset in .strtab (after initial null) + sym_start.st_info = (XM_ELF_STB_GLOBAL << 4) | XM_ELF_STT_OBJECT; + sym_start.st_shndx = 1; // .rodata section index + sym_start.st_value = 0; + sym_start.st_size = 0; + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&sym_start, sizeof(sym_start))) { + return tb_false; + } + + // symbol 2: _binary_xxx_end + xm_elf32_symbol_t sym_end; + tb_memset(&sym_end, 0, sizeof(sym_end)); + sym_end.st_name = 1 + (tb_uint32_t)(start_len + 1); // offset in .strtab + sym_end.st_info = (XM_ELF_STB_GLOBAL << 4) | XM_ELF_STT_OBJECT; + sym_end.st_shndx = 1; // .rodata section index + sym_end.st_value = rodata_size; + sym_end.st_size = 0; + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&sym_end, sizeof(sym_end))) { + return tb_false; + } + + // align .symtab to 4 bytes + if (symtab_padding > 0) { + tb_byte_t zero = 0; + while (symtab_padding-- > 0) { + if (!tb_stream_bwrit(ostream, &zero, 1)) { + return tb_false; + } + } + } + + // write string table + tb_byte_t null = 0; + if (!tb_stream_bwrit(ostream, &null, 1)) { + return tb_false; + } + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)symbol_start, start_len)) { + return tb_false; + } + if (!tb_stream_bwrit(ostream, &null, 1)) { + return tb_false; + } + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)symbol_end, end_len)) { + return tb_false; + } + if (!tb_stream_bwrit(ostream, &null, 1)) { + return tb_false; + } + + // align .strtab to 4 bytes + if (strtab_padding > 0) { + tb_byte_t zero = 0; + while (strtab_padding-- > 0) { + if (!tb_stream_bwrit(ostream, &zero, 1)) { + return tb_false; + } + } + } + + // write section header string table + if (!tb_stream_bwrit(ostream, &null, 1)) { + return tb_false; + } + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)".rodata", 7)) { + return tb_false; + } + if (!tb_stream_bwrit(ostream, &null, 1)) { + return tb_false; + } + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)".symtab", 7)) { + return tb_false; + } + if (!tb_stream_bwrit(ostream, &null, 1)) { + return tb_false; + } + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)".strtab", 7)) { + return tb_false; + } + if (!tb_stream_bwrit(ostream, &null, 1)) { + return tb_false; + } + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)".shstrtab", 9)) { + return tb_false; + } + if (!tb_stream_bwrit(ostream, &null, 1)) { + return tb_false; + } + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)".note.GNU-stack", 15)) { + return tb_false; + } + if (!tb_stream_bwrit(ostream, &null, 1)) { + return tb_false; + } + + return tb_true; +} + +static tb_bool_t xm_utils_bin2elf_dump_64(tb_stream_ref_t istream, + tb_stream_ref_t ostream, + tb_char_t const *symbol_prefix, + tb_char_t const *arch, + tb_char_t const *basename, + tb_bool_t zeroend) { tb_assert_and_check_return_val(istream && ostream, tb_false); // get file size @@ -465,10 +822,20 @@ tb_int_t xm_utils_bin2elf(lua_State *lua) { break; } - if (!xm_utils_bin2elf_dump(istream, ostream, symbol_prefix, arch, basename, zeroend)) { - lua_pushboolean(lua, tb_false); - lua_pushfstring(lua, "bin2elf: dump data failed"); - break; + // choose 32-bit or 64-bit ELF based on architecture + tb_bool_t is_64bit = xm_utils_bin2elf_is_64bit(arch); + if (is_64bit) { + if (!xm_utils_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)) { + lua_pushboolean(lua, tb_false); + lua_pushfstring(lua, "bin2elf: dump data failed"); + break; + } } ok = tb_true; -- cgit v1.3.1 From ef01b10b71166de390f8ca2add93639705416f4f Mon Sep 17 00:00:00 2001 From: ruki Date: Sun, 7 Dec 2025 15:15:57 +0800 Subject: support bin2macho 32bits --- core/src/xmake/utils/bin2macho.c | 250 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 248 insertions(+), 2 deletions(-) (limited to 'core/src') diff --git a/core/src/xmake/utils/bin2macho.c b/core/src/xmake/utils/bin2macho.c index 50b03c5e3..04733e1b2 100644 --- a/core/src/xmake/utils/bin2macho.c +++ b/core/src/xmake/utils/bin2macho.c @@ -538,6 +538,253 @@ 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, + tb_stream_ref_t ostream, + tb_char_t const *symbol_prefix, + tb_char_t const *plat, + tb_char_t const *arch, + tb_char_t const *basename, + tb_uint32_t minos, + tb_uint32_t sdk, + tb_bool_t zeroend) { + tb_assert_and_check_return_val(istream && ostream, tb_false); + + // get file size + tb_hong_t filesize = tb_stream_size(istream); + if (filesize < 0 || filesize > 0xffffffffU) { + return tb_false; + } + tb_uint32_t datasize = (tb_uint32_t)filesize; + // add null terminator if zeroend is true + if (zeroend) { + if (datasize >= 0xffffffffU) { + return tb_false; // would overflow + } + datasize++; + } + + // generate symbol names from filename + tb_char_t symbol_name[256] = {0}; + tb_char_t symbol_start[256] = {0}; + tb_char_t symbol_end[256] = {0}; + + // use basename or default to "data" + if (!basename || !basename[0]) { + basename = "data"; + } + + // build symbol name + // On macOS, C compiler adds an underscore prefix, so we generate symbols with two underscores + // (C code declares _binary_xxx, compiler generates __binary_xxx in object file, so we define __binary_xxx) + if (symbol_prefix) { + tb_snprintf(symbol_name, sizeof(symbol_name), "_%s%s", symbol_prefix, basename); + } else { + tb_snprintf(symbol_name, sizeof(symbol_name), "__binary_%s", basename); + } + + // replace non-alphanumeric with underscore + for (tb_size_t i = 0; symbol_name[i]; i++) { + if (!tb_isalpha(symbol_name[i]) && !tb_isdigit(symbol_name[i]) && symbol_name[i] != '_') { + symbol_name[i] = '_'; + } + } + + tb_snprintf(symbol_start, sizeof(symbol_start), "%s_start", symbol_name); + tb_snprintf(symbol_end, sizeof(symbol_end), "%s_end", symbol_name); + + // calculate offsets + tb_uint32_t header_size = sizeof(xm_macho_header_t); + tb_uint32_t segment_cmd_size = sizeof(xm_macho_segment_command_t); + tb_uint32_t section_size = sizeof(xm_macho_section_t); + 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_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 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; + tb_uint32_t strtab_size = 4; // initial 4-byte size field + tb_size_t start_len = tb_strlen(symbol_start); + 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); + + // 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.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; + header.flags = 0; + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&header, sizeof(header))) { + return tb_false; + } + + // write segment command + xm_macho_segment_command_t segment; + tb_memset(&segment, 0, sizeof(segment)); + segment.cmd = XM_MACHO_LC_SEGMENT; + segment.cmdsize = segment_cmd_total_size; + tb_strncpy(segment.segname, "__TEXT", 16); + segment.vmaddr = 0; + segment.vmsize = data_size; + segment.fileoff = data_offset; + segment.filesize = data_size; + segment.maxprot = XM_MACHO_VM_PROT_READ; // VM_PROT_READ (read-only) + segment.initprot = XM_MACHO_VM_PROT_READ; // VM_PROT_READ (read-only) + segment.nsects = 1; + segment.flags = 0; + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&segment, sizeof(segment))) { + return tb_false; + } + + // write section + xm_macho_section_t section; + tb_memset(§ion, 0, sizeof(section)); + tb_strncpy(section.sectname, "__const", 16); + tb_strncpy(section.segname, "__TEXT", 16); + section.addr = 0; + section.size = data_size; + section.offset = data_offset; + section.align = 2; // 2^2 = 4 bytes + section.reloff = 0; + section.nreloc = 0; + section.flags = XM_MACHO_SECT_TYPE_REGULAR | XM_MACHO_SECT_ATTR_SOME_INITS; + section.reserved1 = 0; + section.reserved2 = 0; + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)§ion, sizeof(section))) { + return tb_false; + } + + // write symtab command + xm_macho_symtab_command_t symtab; + tb_memset(&symtab, 0, sizeof(symtab)); + symtab.cmd = XM_MACHO_LC_SYMTAB; + symtab.cmdsize = symtab_cmd_size; + symtab.symoff = symtab_offset; + symtab.nsyms = nlist_count; + symtab.stroff = strtab_offset; + symtab.strsize = strtab_size; + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&symtab, sizeof(symtab))) { + return tb_false; + } + + // write build version command + xm_macho_build_version_command_t build_version; + 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.minos = minos; + build_version.sdk = sdk; + build_version.ntools = 0; + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&build_version, sizeof(build_version))) { + return tb_false; + } + + // align to 4 bytes + tb_uint32_t padding = data_offset - (header_size + segment_cmd_total_size + symtab_cmd_size + build_version_cmd_size); + if (padding > 0) { + tb_byte_t zero = 0; + while (padding-- > 0) { + if (!tb_stream_bwrit(ostream, &zero, 1)) { + return tb_false; + } + } + } + + // write section data + if (!xm_utils_stream_copy(istream, ostream, filesize)) { + return tb_false; + } + // append null terminator if zeroend is true + if (zeroend) { + tb_byte_t zero = 0; + if (!tb_stream_bwrit(ostream, &zero, 1)) { + return tb_false; + } + } + + // align to 4 bytes + padding = symtab_offset - data_end_offset; + if (padding > 0) { + tb_byte_t zero = 0; + while (padding-- > 0) { + if (!tb_stream_bwrit(ostream, &zero, 1)) { + return tb_false; + } + } + } + + // write symbol table + // strx starts from 4 (after 4-byte size field) + tb_uint32_t strx = 4; + // symbol 0: _binary_xxx_start + xm_macho_nlist_t nlist_start; + tb_memset(&nlist_start, 0, sizeof(nlist_start)); + nlist_start.strx = strx; + nlist_start.type = XM_MACHO_N_TYPE_SECT | XM_MACHO_N_EXT; // N_SECT | N_EXT + nlist_start.sect = 1; + nlist_start.desc = 0; + nlist_start.value = 0; + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&nlist_start, sizeof(nlist_start))) { + return tb_false; + } + strx += (tb_uint32_t)(start_len + 1); + + // symbol 1: _binary_xxx_end + xm_macho_nlist_t nlist_end; + tb_memset(&nlist_end, 0, sizeof(nlist_end)); + nlist_end.strx = strx; + nlist_end.type = XM_MACHO_N_TYPE_SECT | XM_MACHO_N_EXT; // N_SECT | N_EXT + nlist_end.sect = 1; + nlist_end.desc = 0; + nlist_end.value = data_size; + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&nlist_end, sizeof(nlist_end))) { + return tb_false; + } + strx += (tb_uint32_t)(end_len + 1); + + // align to 4 bytes + padding = strtab_offset - (symtab_offset + nlist_size * nlist_count); + if (padding > 0) { + tb_byte_t zero = 0; + while (padding-- > 0) { + if (!tb_stream_bwrit(ostream, &zero, 1)) { + return tb_false; + } + } + } + + // write string table + tb_stream_bwrit(ostream, (tb_byte_t const *)&strtab_size, 4); + tb_stream_bwrit(ostream, (tb_byte_t const *)symbol_start, start_len); + tb_byte_t null = 0; + tb_stream_bwrit(ostream, &null, 1); + tb_stream_bwrit(ostream, (tb_byte_t const *)symbol_end, end_len); + tb_stream_bwrit(ostream, &null, 1); + + // align string table to 4 bytes + padding = strtab_size - (4 + (tb_uint32_t)start_len + 1 + (tb_uint32_t)end_len + 1); + if (padding > 0) { + tb_byte_t zero = 0; + while (padding-- > 0) { + if (!tb_stream_bwrit(ostream, &zero, 1)) { + return tb_false; + } + } + } + + return tb_true; +} + static tb_bool_t xm_utils_bin2macho_dump(tb_stream_ref_t istream, tb_stream_ref_t ostream, tb_char_t const *symbol_prefix, @@ -550,8 +797,7 @@ static tb_bool_t xm_utils_bin2macho_dump(tb_stream_ref_t istream, if (xm_utils_bin2macho_is_64bit(arch)) { return xm_utils_bin2macho_dump_64(istream, ostream, symbol_prefix, plat, arch, basename, minos, sdk, zeroend); } else { - // 32-bit not implemented yet - return tb_false; + return xm_utils_bin2macho_dump_32(istream, ostream, symbol_prefix, plat, arch, basename, minos, sdk, zeroend); } } -- cgit v1.3.1 From a88b0246d856ffac9dfa509d07631dc02fedbc67 Mon Sep 17 00:00:00 2001 From: ruki Date: Sun, 7 Dec 2025 15:17:57 +0800 Subject: improve bin2coff --- core/src/xmake/utils/bin2coff.c | 2 ++ 1 file changed, 2 insertions(+) (limited to 'core/src') diff --git a/core/src/xmake/utils/bin2coff.c b/core/src/xmake/utils/bin2coff.c index 25c54035c..976f18202 100644 --- a/core/src/xmake/utils/bin2coff.c +++ b/core/src/xmake/utils/bin2coff.c @@ -113,6 +113,8 @@ static tb_uint16_t xm_utils_bin2coff_get_machine(tb_char_t const *arch) { return XM_COFF_MACHINE_ARM64; } else if (tb_strcmp(arch, "arm") == 0) { return XM_COFF_MACHINE_ARM; + } else if (tb_strcmp(arch, "i386") == 0 || tb_strcmp(arch, "x86") == 0) { + return XM_COFF_MACHINE_I386; } return XM_COFF_MACHINE_I386; } -- cgit v1.3.1 From f4cd87b0135d84932f186bc068a5232912ee43b9 Mon Sep 17 00:00:00 2001 From: ruki Date: Sun, 7 Dec 2025 17:47:01 +0800 Subject: update bin2c --- core/src/xmake/binutils/bin2c.c | 222 ++++++ core/src/xmake/binutils/bin2coff.c | 406 ++++++++++ core/src/xmake/binutils/bin2elf.c | 856 ++++++++++++++++++++ core/src/xmake/binutils/bin2macho.c | 887 +++++++++++++++++++++ core/src/xmake/binutils/prefix.h | 70 ++ core/src/xmake/engine.c | 26 +- core/src/xmake/utils/bin2c.c | 224 ------ core/src/xmake/utils/bin2coff.c | 406 ---------- core/src/xmake/utils/bin2elf.c | 856 -------------------- core/src/xmake/utils/bin2macho.c | 887 --------------------- core/src/xmake/utils/prefix.h | 69 -- core/src/xmake/xmake.sh | 2 +- xmake/core/base/binutils.lua | 61 ++ xmake/core/base/utils.lua | 8 - .../sandbox/modules/import/core/base/binutils.lua | 62 ++ xmake/core/sandbox/modules/utils.lua | 39 - xmake/modules/utils/binary/bin2c.lua | 5 +- xmake/modules/utils/binary/bin2obj.lua | 67 +- 18 files changed, 2603 insertions(+), 2550 deletions(-) create mode 100644 core/src/xmake/binutils/bin2c.c create mode 100644 core/src/xmake/binutils/bin2coff.c create mode 100644 core/src/xmake/binutils/bin2elf.c create mode 100644 core/src/xmake/binutils/bin2macho.c create mode 100644 core/src/xmake/binutils/prefix.h delete mode 100644 core/src/xmake/utils/bin2c.c delete mode 100644 core/src/xmake/utils/bin2coff.c delete mode 100644 core/src/xmake/utils/bin2elf.c delete mode 100644 core/src/xmake/utils/bin2macho.c delete mode 100644 core/src/xmake/utils/prefix.h create mode 100644 xmake/core/base/binutils.lua create mode 100644 xmake/core/sandbox/modules/import/core/base/binutils.lua (limited to 'core/src') diff --git a/core/src/xmake/binutils/bin2c.c b/core/src/xmake/binutils/bin2c.c new file mode 100644 index 000000000..b5054c55f --- /dev/null +++ b/core/src/xmake/binutils/bin2c.c @@ -0,0 +1,222 @@ +/*!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 bin2c.c + * + */ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * trace + */ +#define TB_TRACE_MODULE_NAME "bin2c" +#define TB_TRACE_MODULE_DEBUG (0) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ + +#define XM_BIN2C_DATA_SIZE (8 * 1024) +#define XM_BIN2C_LINE_SIZE (4 * 1024) +#define XM_BIN2C_LINEWIDTH_MAX ((XM_BIN2C_LINE_SIZE - 2) / 6) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * private implementation + */ + +// optimized hex conversion table +static tb_char_t const *xm_binutils_bin2c_digits = "0123456789ABCDEF"; + +// inline hex conversion for better performance +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_binutils_bin2c_digits[(value >> 4) & 15]; + str[4] = xm_binutils_bin2c_digits[value & 15]; +} + +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; + tb_byte_t data[XM_BIN2C_DATA_SIZE]; + tb_char_t line[XM_BIN2C_LINE_SIZE]; + tb_size_t linesize = 0; + tb_size_t bytes_in_line = 0; + tb_size_t data_pos = 0; + tb_size_t data_size = 0; + tb_assert_and_check_return_val(linewidth > 0 && linewidth <= XM_BIN2C_LINEWIDTH_MAX, tb_false); + + while (!tb_stream_beof(istream) || data_pos < data_size || zero_pending) { + // read a large chunk of data if buffer is empty + if (data_pos >= data_size) { + // handle pending zero terminator + if (zero_pending) { + data[0] = '\0'; + data_size = 1; + data_pos = 0; + zero_pending = tb_false; + } 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); + tb_check_break(to_read); + + if (!tb_stream_bread(istream, data, to_read)) { + break; + } + data_size = to_read; + data_pos = 0; + + // check if we need to add zero terminator at the end + if (!nozeroend && tb_stream_beof(istream)) { + if (data_size < XM_BIN2C_DATA_SIZE) { + // can add directly to current buffer + data[data_size++] = '\0'; + } else { + // buffer is full, need to add zero in next iteration + zero_pending = tb_true; + } + } + } + } + + // process bytes from buffer + while (data_pos < data_size) { + // check if we need a new line + if (bytes_in_line >= (tb_size_t)linewidth) { + // write line (tb_stream_bwrit_line will add newline automatically) + if (tb_stream_bwrit_line(ostream, line, linesize) < 0) { + return tb_false; + } + + linesize = 0; + bytes_in_line = 0; + first = tb_false; + } + + // ensure we have enough space in line buffer (6 chars per byte: ", 0xXX") + if (linesize + 6 > sizeof(line)) { + // flush partial line if buffer is full + if (linesize > 0) { + if (!tb_stream_bwrit(ostream, (tb_byte_t *)line, linesize)) { + return tb_false; + } + linesize = 0; + } + } + + // add separator + if (bytes_in_line == 0 && first) { + line[linesize++] = ' '; + first = tb_false; + } else { + line[linesize++] = ','; + } + + // write hex value (inline for performance) + xm_binutils_bin2c_write_hex(line + linesize, data[data_pos]); + linesize += 5; + bytes_in_line++; + data_pos++; + } + } + + // flush remaining line + if (linesize > 0) { + // write line (tb_stream_bwrit_line will add newline automatically) + if (tb_stream_bwrit_line(ostream, line, linesize) < 0) { + return tb_false; + } + } + + return tb_stream_beof(istream); +} + +/* ////////////////////////////////////////////////////////////////////////////////////// + * implementation + */ + +/* generate c/c++ code from the binary file + * + * local ok, errors = binutils.bin2c(binaryfile, outputfile, linewidth, nozeroend) + */ +tb_int_t xm_binutils_bin2c(lua_State *lua) { + tb_assert_and_check_return_val(lua, 0); + + // get the binaryfile + tb_char_t const *binaryfile = luaL_checkstring(lua, 1); + tb_check_return_val(binaryfile, 0); + + // get the outputfile + tb_char_t const *outputfile = luaL_checkstring(lua, 2); + tb_check_return_val(outputfile, 0); + + // get line width + tb_int_t linewidth = (tb_int_t)lua_tointeger(lua, 3); + + // no zero end? + tb_bool_t nozeroend = (tb_bool_t)lua_toboolean(lua, 4); + + // do dump + tb_bool_t ok = tb_false; + tb_stream_ref_t istream = tb_stream_init_from_file(binaryfile, TB_FILE_MODE_RO); + tb_stream_ref_t ostream = tb_stream_init_from_file(outputfile, + TB_FILE_MODE_RW | TB_FILE_MODE_CREAT | TB_FILE_MODE_TRUNC); + do { + if (!tb_stream_open(istream)) { + lua_pushboolean(lua, tb_false); + lua_pushfstring(lua, "bin2c: open %s failed", binaryfile); + break; + } + + if (!tb_stream_open(ostream)) { + lua_pushboolean(lua, tb_false); + lua_pushfstring(lua, "bin2c: open %s failed", outputfile); + break; + } + + if (!xm_binutils_bin2c_dump(istream, ostream, linewidth, nozeroend)) { + lua_pushboolean(lua, tb_false); + lua_pushfstring(lua, "bin2c: dump data failed"); + break; + } + + ok = tb_true; + lua_pushboolean(lua, ok); + + } while (0); + + if (istream) { + tb_stream_clos(istream); + } + istream = tb_null; + + if (ostream) { + tb_stream_clos(ostream); + } + ostream = tb_null; + + return ok ? 1 : 2; +} diff --git a/core/src/xmake/binutils/bin2coff.c b/core/src/xmake/binutils/bin2coff.c new file mode 100644 index 000000000..686b89fac --- /dev/null +++ b/core/src/xmake/binutils/bin2coff.c @@ -0,0 +1,406 @@ +/*!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 bin2coff.c + * + */ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * trace + */ +#define TB_TRACE_MODULE_NAME "bin2coff" +#define TB_TRACE_MODULE_DEBUG (0) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ +#define XM_COFF_MACHINE_I386 0x014c +#define XM_COFF_MACHINE_AMD64 0x8664 +#define XM_COFF_MACHINE_ARM 0x01c0 +#define XM_COFF_MACHINE_ARM64 0xaa64 + +#define XM_COFF_SECTION_RDATA 0x40000040 // IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ +#include "tbox/prefix/packed.h" +typedef struct __xm_coff_header_t { + tb_uint16_t machine; + tb_uint16_t nsects; + tb_uint32_t time; + tb_uint32_t symtabofs; + tb_uint32_t nsyms; + tb_uint16_t opthdr; + tb_uint16_t flags; +} __tb_packed__ xm_coff_header_t; + +typedef struct __xm_coff_section_t { + tb_char_t name[8]; + tb_uint32_t vsize; + tb_uint32_t vaddr; + tb_uint32_t size; + tb_uint32_t ofs; + tb_uint32_t relocofs; + tb_uint32_t linenoofs; + tb_uint16_t nreloc; + tb_uint16_t nlineno; + tb_uint32_t flags; +} __tb_packed__ xm_coff_section_t; + +typedef struct __xm_coff_symbol_t { + union { + struct { + tb_char_t name[8]; + } shortname; + struct { + tb_uint32_t zeros; + tb_uint32_t offset; + } longname; + } n; + tb_uint32_t value; + tb_int16_t sect; + tb_uint16_t type; + tb_uint8_t scl; + tb_uint8_t naux; +} __tb_packed__ xm_coff_symbol_t; + +typedef struct __xm_coff_aux_section_t { + tb_uint32_t length; + tb_uint16_t nreloc; + tb_uint16_t nlineno; + tb_uint8_t reserved[10]; +} __tb_packed__ xm_coff_aux_section_t; + +typedef struct __xm_coff_symbol_tail_t { + tb_uint32_t value; + tb_int16_t sect; + tb_uint16_t type; + tb_uint8_t scl; + tb_uint8_t naux; +} __tb_packed__ xm_coff_symbol_tail_t; +#include "tbox/prefix/packed.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * private implementation + */ +static tb_uint16_t xm_binutils_bin2coff_get_machine(tb_char_t const *arch) { + if (!arch) { + return XM_COFF_MACHINE_I386; + } + if (tb_strcmp(arch, "x86_64") == 0 || tb_strcmp(arch, "x64") == 0) { + return XM_COFF_MACHINE_AMD64; + } else if (tb_strcmp(arch, "arm64") == 0 || tb_strcmp(arch, "aarch64") == 0) { + return XM_COFF_MACHINE_ARM64; + } else if (tb_strcmp(arch, "arm") == 0) { + return XM_COFF_MACHINE_ARM; + } else if (tb_strcmp(arch, "i386") == 0 || tb_strcmp(arch, "x86") == 0) { + return XM_COFF_MACHINE_I386; + } + return XM_COFF_MACHINE_I386; +} + +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); + } + tb_stream_bwrit(ostream, (tb_byte_t const *)str, len); +} + +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) { + tb_stream_bwrit(ostream, &zero, 1); + } +} + +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_binutils_bin2coff_write_string(ostream, name, len); + if (len < 8) { + xm_binutils_bin2coff_write_padding(ostream, 8 - len); + } + } else { + // long name: store offset in string table + tb_uint32_t zeros = 0; + tb_stream_bwrit(ostream, (tb_byte_t const *)&zeros, 4); + tb_stream_bwrit(ostream, (tb_byte_t const *)strtab_offset, 4); + *strtab_offset += (tb_uint32_t)(len + 1); // +1 for null terminator + } +} + +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, + tb_char_t const *basename, + tb_bool_t zeroend) { + tb_assert_and_check_return_val(istream && ostream, tb_false); + + // get file size + tb_hong_t filesize = tb_stream_size(istream); + if (filesize < 0 || filesize > 0xffffffffU) { + return tb_false; + } + tb_uint32_t datasize = (tb_uint32_t)filesize; + // add null terminator if zeroend is true + if (zeroend) { + if (datasize >= 0xffffffffU) { + return tb_false; // would overflow + } + datasize++; + } + + // generate symbol names from filename + tb_char_t symbol_name[256] = {0}; + tb_char_t symbol_start[256] = {0}; + tb_char_t symbol_end[256] = {0}; + + // use basename or default to "data" + if (!basename || !basename[0]) { + basename = "data"; + } + + // build symbol name + if (symbol_prefix) { + tb_snprintf(symbol_name, sizeof(symbol_name), "%s%s", symbol_prefix, basename); + } else { + tb_snprintf(symbol_name, sizeof(symbol_name), "_binary_%s", basename); + } + + // replace non-alphanumeric with underscore + for (tb_size_t i = 0; symbol_name[i]; i++) { + if (!tb_isalpha(symbol_name[i]) && !tb_isdigit(symbol_name[i]) && symbol_name[i] != '_') { + symbol_name[i] = '_'; + } + } + + tb_snprintf(symbol_start, sizeof(symbol_start), "%s_start", symbol_name); + tb_snprintf(symbol_end, sizeof(symbol_end), "%s_end", symbol_name); + + // calculate offsets + tb_uint32_t header_size = sizeof(xm_coff_header_t); + tb_uint32_t section_header_size = sizeof(xm_coff_section_t); + tb_uint32_t section_data_ofs = header_size + section_header_size; + tb_uint32_t section_data_size = datasize; + tb_uint32_t symbol_table_ofs = section_data_ofs + ((section_data_size + 3) & ~3); // align to 4 bytes + tb_uint32_t string_table_size = 4; // initial 4-byte size field + + // calculate string table size + tb_size_t start_len = tb_strlen(symbol_start); + tb_size_t end_len = tb_strlen(symbol_end); + if (start_len > 8) { + string_table_size += (tb_uint32_t)(start_len + 1); + } + if (end_len > 8) { + string_table_size += (tb_uint32_t)(end_len + 1); + } + + // write COFF header + xm_coff_header_t header; + tb_memset(&header, 0, sizeof(header)); + header.machine = xm_binutils_bin2coff_get_machine(arch); + header.nsects = 1; + header.time = 0; + header.symtabofs = symbol_table_ofs; + header.nsyms = 4; // .rdata section symbol (1) + auxiliary entry (1) + 2 data symbols (start, end) + header.opthdr = 0; + header.flags = 0; + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&header, sizeof(header))) { + return tb_false; + } + + // write section header (.rdata) + xm_coff_section_t section; + tb_memset(§ion, 0, sizeof(section)); + tb_strncpy(section.name, ".rdata", 8); + section.vsize = datasize; + section.vaddr = 0; + section.size = datasize; + section.ofs = section_data_ofs; + section.relocofs = 0; + section.linenoofs = 0; + section.nreloc = 0; + section.nlineno = 0; + section.flags = XM_COFF_SECTION_RDATA; + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)§ion, sizeof(section))) { + return tb_false; + } + + // write section data + if (!xm_binutils_stream_copy(istream, ostream, filesize)) { + return tb_false; + } + // append null terminator if zeroend is true + if (zeroend) { + tb_byte_t zero = 0; + if (!tb_stream_bwrit(ostream, &zero, 1)) { + return tb_false; + } + } + + // align to 4 bytes + tb_uint32_t padding = (4 - (section_data_size & 3)) & 3; + if (padding > 0) { + xm_binutils_bin2coff_write_padding(ostream, padding); + } + + // write symbol table + // symbol 0: .rdata section symbol + xm_coff_symbol_t sym_section; + tb_memset(&sym_section, 0, sizeof(sym_section)); + tb_strncpy(sym_section.n.shortname.name, ".rdata", 8); + sym_section.value = 0; + sym_section.sect = 1; // section index (1-based) + sym_section.type = 0; // IMAGE_SYM_TYPE_NULL + sym_section.scl = 3; // IMAGE_SYM_CLASS_STATIC + sym_section.naux = 1; // auxiliary entry + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&sym_section, sizeof(sym_section))) { + return tb_false; + } + // auxiliary entry for section (18 bytes total) + xm_coff_aux_section_t aux_section; + tb_memset(&aux_section, 0, sizeof(aux_section)); + aux_section.length = datasize; + aux_section.nreloc = 0; + aux_section.nlineno = 0; + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&aux_section, sizeof(aux_section))) { + return tb_false; + } + + // symbol 1: _binary_xxx_start + tb_uint32_t strtab_offset = 4; // start after size field + 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; + sym_start_tail.sect = 1; + sym_start_tail.scl = 2; // IMAGE_SYM_CLASS_EXTERNAL + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&sym_start_tail, sizeof(sym_start_tail))) { + return tb_false; + } + + // symbol 2: _binary_xxx_end + 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; + sym_end_tail.sect = 1; + sym_end_tail.scl = 2; // IMAGE_SYM_CLASS_EXTERNAL + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&sym_end_tail, sizeof(sym_end_tail))) { + return tb_false; + } + + // write string table + tb_stream_bwrit(ostream, (tb_byte_t const *)&string_table_size, 4); + if (start_len > 8) { + 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_binutils_bin2coff_write_string(ostream, symbol_end, end_len); + tb_byte_t null = 0; + tb_stream_bwrit(ostream, &null, 1); + } + + return tb_true; +} + +/* ////////////////////////////////////////////////////////////////////////////////////// + * implementation + */ + +/* generate COFF object file from binary file + * + * local ok, errors = binutils.bin2coff(binaryfile, outputfile, symbol_prefix, arch, basename) + */ +tb_int_t xm_binutils_bin2coff(lua_State *lua) { + tb_assert_and_check_return_val(lua, 0); + + // get the binaryfile + tb_char_t const *binaryfile = luaL_checkstring(lua, 1); + tb_check_return_val(binaryfile, 0); + + // get the outputfile + tb_char_t const *outputfile = luaL_checkstring(lua, 2); + tb_check_return_val(outputfile, 0); + + // get symbol prefix (optional) + tb_char_t const *symbol_prefix = lua_isstring(lua, 3) ? lua_tostring(lua, 3) : tb_null; + + // get arch (optional) + tb_char_t const *arch = lua_isstring(lua, 4) ? lua_tostring(lua, 4) : tb_null; + + // get basename (optional) + tb_char_t const *basename = lua_isstring(lua, 5) ? lua_tostring(lua, 5) : tb_null; + + // get zeroend (optional, default: false) + tb_bool_t zeroend = lua_toboolean(lua, 6); + + // do dump + tb_bool_t ok = tb_false; + tb_stream_ref_t istream = tb_stream_init_from_file(binaryfile, TB_FILE_MODE_RO); + tb_stream_ref_t ostream = tb_stream_init_from_file(outputfile, + TB_FILE_MODE_RW | TB_FILE_MODE_CREAT | TB_FILE_MODE_TRUNC); + do { + if (!tb_stream_open(istream)) { + lua_pushboolean(lua, tb_false); + lua_pushfstring(lua, "bin2coff: open %s failed", binaryfile); + break; + } + + if (!tb_stream_open(ostream)) { + lua_pushboolean(lua, tb_false); + lua_pushfstring(lua, "bin2coff: open %s failed", outputfile); + break; + } + + 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; + } + + ok = tb_true; + lua_pushboolean(lua, ok); + + } while (0); + + if (istream) { + tb_stream_clos(istream); + } + istream = tb_null; + + if (ostream) { + tb_stream_clos(ostream); + } + ostream = tb_null; + + return ok ? 1 : 2; +} + diff --git a/core/src/xmake/binutils/bin2elf.c b/core/src/xmake/binutils/bin2elf.c new file mode 100644 index 000000000..bb2507a34 --- /dev/null +++ b/core/src/xmake/binutils/bin2elf.c @@ -0,0 +1,856 @@ +/*!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 bin2elf.c + * + */ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * trace + */ +#define TB_TRACE_MODULE_NAME "bin2elf" +#define TB_TRACE_MODULE_DEBUG (0) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ +#define XM_ELF_MACHINE_X86_64 0x3e +#define XM_ELF_MACHINE_ARM64 0xb7 +#define XM_ELF_MACHINE_ARM 0x28 +#define XM_ELF_MACHINE_I386 0x03 + +#define XM_ELF_SHT_PROGBITS 0x1 +#define XM_ELF_SHT_SYMTAB 0x2 +#define XM_ELF_SHT_STRTAB 0x3 + +#define XM_ELF_SHF_ALLOC 0x2 +#define XM_ELF_SHF_WRITE 0x1 + +#define XM_ELF_STB_GLOBAL 0x1 +#define XM_ELF_STT_OBJECT 0x1 + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ +#include "tbox/prefix/packed.h" +typedef struct __xm_elf32_header_t { + tb_uint8_t e_ident[16]; + tb_uint16_t e_type; + tb_uint16_t e_machine; + tb_uint32_t e_version; + tb_uint32_t e_entry; + tb_uint32_t e_phoff; + tb_uint32_t e_shoff; + tb_uint32_t e_flags; + tb_uint16_t e_ehsize; + tb_uint16_t e_phentsize; + tb_uint16_t e_phnum; + tb_uint16_t e_shentsize; + tb_uint16_t e_shnum; + tb_uint16_t e_shstrndx; +} __tb_packed__ xm_elf32_header_t; + +typedef struct __xm_elf32_section_t { + tb_uint32_t sh_name; + tb_uint32_t sh_type; + tb_uint32_t sh_flags; + tb_uint32_t sh_addr; + tb_uint32_t sh_offset; + tb_uint32_t sh_size; + tb_uint32_t sh_link; + tb_uint32_t sh_info; + tb_uint32_t sh_addralign; + tb_uint32_t sh_entsize; +} __tb_packed__ xm_elf32_section_t; + +typedef struct __xm_elf32_symbol_t { + tb_uint32_t st_name; + tb_uint32_t st_value; + tb_uint32_t st_size; + tb_uint8_t st_info; + tb_uint8_t st_other; + tb_uint16_t st_shndx; +} __tb_packed__ xm_elf32_symbol_t; + +typedef struct __xm_elf64_header_t { + tb_uint8_t e_ident[16]; + tb_uint16_t e_type; + tb_uint16_t e_machine; + tb_uint32_t e_version; + tb_uint64_t e_entry; + tb_uint64_t e_phoff; + tb_uint64_t e_shoff; + tb_uint32_t e_flags; + tb_uint16_t e_ehsize; + tb_uint16_t e_phentsize; + tb_uint16_t e_phnum; + tb_uint16_t e_shentsize; + tb_uint16_t e_shnum; + tb_uint16_t e_shstrndx; +} __tb_packed__ xm_elf64_header_t; + +typedef struct __xm_elf64_section_t { + tb_uint32_t sh_name; + tb_uint32_t sh_type; + tb_uint64_t sh_flags; + tb_uint64_t sh_addr; + tb_uint64_t sh_offset; + tb_uint64_t sh_size; + tb_uint32_t sh_link; + tb_uint32_t sh_info; + tb_uint64_t sh_addralign; + tb_uint64_t sh_entsize; +} __tb_packed__ xm_elf64_section_t; + +typedef struct __xm_elf64_symbol_t { + tb_uint32_t st_name; + tb_uint8_t st_info; + tb_uint8_t st_other; + tb_uint16_t st_shndx; + tb_uint64_t st_value; + tb_uint64_t st_size; +} __tb_packed__ xm_elf64_symbol_t; + +#include "tbox/prefix/packed.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * private implementation + */ +static tb_uint16_t xm_binutils_bin2elf_get_machine(tb_char_t const *arch) { + if (!arch) { + return XM_ELF_MACHINE_X86_64; + } + if (tb_strcmp(arch, "x86_64") == 0 || tb_strcmp(arch, "x64") == 0) { + return XM_ELF_MACHINE_X86_64; + } else if (tb_strcmp(arch, "arm64") == 0 || tb_strcmp(arch, "aarch64") == 0 || + tb_strcmp(arch, "arm64-v8a") == 0) { + return XM_ELF_MACHINE_ARM64; + } else if (tb_strcmp(arch, "arm") == 0 || tb_strcmp(arch, "armv7") == 0 || + tb_strcmp(arch, "armeabi-v7a") == 0) { + return XM_ELF_MACHINE_ARM; + } else if (tb_strcmp(arch, "i386") == 0 || tb_strcmp(arch, "x86") == 0) { + return XM_ELF_MACHINE_I386; + } + return XM_ELF_MACHINE_X86_64; +} + +static tb_bool_t xm_binutils_bin2elf_is_64bit(tb_char_t const *arch) { + if (!arch) { + return tb_true; + } + if (tb_strcmp(arch, "x86_64") == 0 || tb_strcmp(arch, "x64") == 0 || + tb_strcmp(arch, "arm64") == 0 || tb_strcmp(arch, "aarch64") == 0 || + tb_strcmp(arch, "arm64-v8a") == 0) { + return tb_true; + } + return tb_false; +} + +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, + tb_char_t const *basename, + tb_bool_t zeroend) { + tb_assert_and_check_return_val(istream && ostream, tb_false); + + // get file size + tb_hong_t filesize = tb_stream_size(istream); + if (filesize < 0 || filesize > 0xffffffffU) { + return tb_false; + } + tb_uint32_t datasize = (tb_uint32_t)filesize; + // add null terminator if zeroend is true + if (zeroend) { + if (datasize >= 0xffffffffU) { + return tb_false; // would overflow + } + datasize++; + } + + // generate symbol names from filename + tb_char_t symbol_name[256] = {0}; + tb_char_t symbol_start[256] = {0}; + tb_char_t symbol_end[256] = {0}; + + // use basename or default to "data" + if (!basename || !basename[0]) { + basename = "data"; + } + + // build symbol name + if (symbol_prefix) { + tb_snprintf(symbol_name, sizeof(symbol_name), "%s%s", symbol_prefix, basename); + } else { + tb_snprintf(symbol_name, sizeof(symbol_name), "_binary_%s", basename); + } + + // replace non-alphanumeric with underscore + for (tb_size_t i = 0; symbol_name[i]; i++) { + if (!tb_isalpha(symbol_name[i]) && !tb_isdigit(symbol_name[i]) && symbol_name[i] != '_') { + symbol_name[i] = '_'; + } + } + + tb_snprintf(symbol_start, sizeof(symbol_start), "%s_start", symbol_name); + tb_snprintf(symbol_end, sizeof(symbol_end), "%s_end", symbol_name); + + // calculate offsets + tb_uint32_t header_size = sizeof(xm_elf32_header_t); + tb_uint32_t section_header_size = sizeof(xm_elf32_section_t); + tb_uint32_t section_count = 6; // NULL, .rodata, .symtab, .strtab, .shstrtab, .note.GNU-stack + tb_uint32_t section_headers_ofs = header_size; + tb_uint32_t rodata_ofs = section_headers_ofs + section_count * section_header_size; + tb_uint32_t rodata_size = datasize; + tb_uint32_t rodata_padding = (4 - (rodata_size & 3)) & 3; // align to 4 bytes for 32-bit + tb_uint32_t symtab_ofs = rodata_ofs + rodata_size + rodata_padding; + tb_uint32_t symtab_size = 3 * sizeof(xm_elf32_symbol_t); // NULL, start, end + tb_uint32_t symtab_padding = (4 - (symtab_size & 3)) & 3; // align to 4 bytes + tb_uint32_t strtab_ofs = symtab_ofs + symtab_size + symtab_padding; + + // calculate string table size + tb_size_t start_len = tb_strlen(symbol_start); + tb_size_t end_len = tb_strlen(symbol_end); + tb_uint32_t strtab_size = 1; // initial null byte + strtab_size += (tb_uint32_t)(start_len + 1); + strtab_size += (tb_uint32_t)(end_len + 1); + tb_uint32_t strtab_padding = (4 - (strtab_size & 3)) & 3; // align to 4 bytes + tb_uint32_t shstrtab_ofs = strtab_ofs + strtab_size + strtab_padding; + + // calculate section header string table size + tb_uint32_t shstrtab_size = 1; // initial null byte + shstrtab_size += 8; // ".rodata\0" (7 + 1) + shstrtab_size += 8; // ".symtab\0" (7 + 1) + shstrtab_size += 8; // ".strtab\0" (7 + 1) + shstrtab_size += 10; // ".shstrtab\0" (9 + 1) + shstrtab_size += 16; // ".note.GNU-stack\0" (15 + 1) + + // write ELF header + xm_elf32_header_t header; + tb_memset(&header, 0, sizeof(header)); + header.e_ident[0] = 0x7f; + header.e_ident[1] = 'E'; + header.e_ident[2] = 'L'; + header.e_ident[3] = 'F'; + header.e_ident[4] = 1; // ELFCLASS32 + header.e_ident[5] = 1; // ELFDATA2LSB + header.e_ident[6] = 1; // EV_CURRENT + header.e_ident[7] = 0; // ELFOSABI_SYSV + header.e_type = 1; // ET_REL + header.e_machine = xm_binutils_bin2elf_get_machine(arch); + header.e_version = 1; + header.e_shoff = section_headers_ofs; + header.e_ehsize = header_size; + header.e_shentsize = section_header_size; + header.e_shnum = section_count; + header.e_shstrndx = 4; // .shstrtab section index + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&header, sizeof(header))) { + return tb_false; + } + + // write section headers + xm_elf32_section_t section_null; + tb_memset(§ion_null, 0, sizeof(section_null)); + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)§ion_null, sizeof(section_null))) { + return tb_false; + } + + // write .rodata section header + xm_elf32_section_t section_rodata; + tb_memset(§ion_rodata, 0, sizeof(section_rodata)); + section_rodata.sh_name = 1; // ".rodata" in shstrtab + section_rodata.sh_type = XM_ELF_SHT_PROGBITS; + section_rodata.sh_flags = XM_ELF_SHF_ALLOC; + section_rodata.sh_offset = rodata_ofs; + section_rodata.sh_size = rodata_size; + section_rodata.sh_addralign = 4; + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)§ion_rodata, sizeof(section_rodata))) { + return tb_false; + } + + // write .symtab section header + xm_elf32_section_t section_symtab; + tb_memset(§ion_symtab, 0, sizeof(section_symtab)); + section_symtab.sh_name = 9; // ".symtab" in shstrtab + section_symtab.sh_type = XM_ELF_SHT_SYMTAB; + section_symtab.sh_offset = symtab_ofs; + section_symtab.sh_size = symtab_size; + section_symtab.sh_link = 3; // .strtab section index + section_symtab.sh_info = 1; // first global symbol index + section_symtab.sh_addralign = 4; + section_symtab.sh_entsize = sizeof(xm_elf32_symbol_t); + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)§ion_symtab, sizeof(section_symtab))) { + return tb_false; + } + + // write .strtab section header + xm_elf32_section_t section_strtab; + tb_memset(§ion_strtab, 0, sizeof(section_strtab)); + section_strtab.sh_name = 17; // ".strtab" in shstrtab + section_strtab.sh_type = XM_ELF_SHT_STRTAB; + section_strtab.sh_offset = strtab_ofs; + section_strtab.sh_size = strtab_size; + section_strtab.sh_addralign = 1; + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)§ion_strtab, sizeof(section_strtab))) { + return tb_false; + } + + // write .shstrtab section header + xm_elf32_section_t section_shstrtab; + tb_memset(§ion_shstrtab, 0, sizeof(section_shstrtab)); + section_shstrtab.sh_name = 25; // ".shstrtab" in shstrtab + section_shstrtab.sh_type = XM_ELF_SHT_STRTAB; + section_shstrtab.sh_offset = shstrtab_ofs; + section_shstrtab.sh_size = shstrtab_size; + section_shstrtab.sh_addralign = 1; + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)§ion_shstrtab, sizeof(section_shstrtab))) { + return tb_false; + } + + // write .note.GNU-stack section header (empty section to mark stack as non-executable) + xm_elf32_section_t section_note_gnu_stack; + tb_memset(§ion_note_gnu_stack, 0, sizeof(section_note_gnu_stack)); + section_note_gnu_stack.sh_name = 35; // ".note.GNU-stack" in shstrtab (25 + 10) + section_note_gnu_stack.sh_type = XM_ELF_SHT_PROGBITS; + section_note_gnu_stack.sh_flags = 0; // no flags + section_note_gnu_stack.sh_offset = shstrtab_ofs + shstrtab_size; // after .shstrtab + section_note_gnu_stack.sh_size = 0; // empty section + section_note_gnu_stack.sh_addralign = 1; + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)§ion_note_gnu_stack, sizeof(section_note_gnu_stack))) { + return tb_false; + } + + // write .rodata section data + if (!xm_binutils_stream_copy(istream, ostream, filesize)) { + return tb_false; + } + // append null terminator if zeroend is true + if (zeroend) { + tb_byte_t zero = 0; + if (!tb_stream_bwrit(ostream, &zero, 1)) { + return tb_false; + } + } + + // align .rodata to 4 bytes + if (rodata_padding > 0) { + tb_byte_t zero = 0; + while (rodata_padding-- > 0) { + if (!tb_stream_bwrit(ostream, &zero, 1)) { + return tb_false; + } + } + } + + // write symbol table + // symbol 0: NULL symbol + xm_elf32_symbol_t sym_null; + tb_memset(&sym_null, 0, sizeof(sym_null)); + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&sym_null, sizeof(sym_null))) { + return tb_false; + } + + // symbol 1: _binary_xxx_start + xm_elf32_symbol_t sym_start; + tb_memset(&sym_start, 0, sizeof(sym_start)); + sym_start.st_name = 1; // offset in .strtab (after initial null) + sym_start.st_info = (XM_ELF_STB_GLOBAL << 4) | XM_ELF_STT_OBJECT; + sym_start.st_shndx = 1; // .rodata section index + sym_start.st_value = 0; + sym_start.st_size = 0; + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&sym_start, sizeof(sym_start))) { + return tb_false; + } + + // symbol 2: _binary_xxx_end + xm_elf32_symbol_t sym_end; + tb_memset(&sym_end, 0, sizeof(sym_end)); + sym_end.st_name = 1 + (tb_uint32_t)(start_len + 1); // offset in .strtab + sym_end.st_info = (XM_ELF_STB_GLOBAL << 4) | XM_ELF_STT_OBJECT; + sym_end.st_shndx = 1; // .rodata section index + sym_end.st_value = rodata_size; + sym_end.st_size = 0; + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&sym_end, sizeof(sym_end))) { + return tb_false; + } + + // align .symtab to 4 bytes + if (symtab_padding > 0) { + tb_byte_t zero = 0; + while (symtab_padding-- > 0) { + if (!tb_stream_bwrit(ostream, &zero, 1)) { + return tb_false; + } + } + } + + // write string table + tb_byte_t null = 0; + if (!tb_stream_bwrit(ostream, &null, 1)) { + return tb_false; + } + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)symbol_start, start_len)) { + return tb_false; + } + if (!tb_stream_bwrit(ostream, &null, 1)) { + return tb_false; + } + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)symbol_end, end_len)) { + return tb_false; + } + if (!tb_stream_bwrit(ostream, &null, 1)) { + return tb_false; + } + + // align .strtab to 4 bytes + if (strtab_padding > 0) { + tb_byte_t zero = 0; + while (strtab_padding-- > 0) { + if (!tb_stream_bwrit(ostream, &zero, 1)) { + return tb_false; + } + } + } + + // write section header string table + if (!tb_stream_bwrit(ostream, &null, 1)) { + return tb_false; + } + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)".rodata", 7)) { + return tb_false; + } + if (!tb_stream_bwrit(ostream, &null, 1)) { + return tb_false; + } + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)".symtab", 7)) { + return tb_false; + } + if (!tb_stream_bwrit(ostream, &null, 1)) { + return tb_false; + } + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)".strtab", 7)) { + return tb_false; + } + if (!tb_stream_bwrit(ostream, &null, 1)) { + return tb_false; + } + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)".shstrtab", 9)) { + return tb_false; + } + if (!tb_stream_bwrit(ostream, &null, 1)) { + return tb_false; + } + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)".note.GNU-stack", 15)) { + return tb_false; + } + if (!tb_stream_bwrit(ostream, &null, 1)) { + return tb_false; + } + + return tb_true; +} + +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, + tb_char_t const *basename, + tb_bool_t zeroend) { + tb_assert_and_check_return_val(istream && ostream, tb_false); + + // get file size + tb_hong_t filesize = tb_stream_size(istream); + if (filesize < 0 || filesize > 0xffffffffU) { + return tb_false; + } + tb_uint32_t datasize = (tb_uint32_t)filesize; + // add null terminator if zeroend is true + if (zeroend) { + if (datasize >= 0xffffffffU) { + return tb_false; // would overflow + } + datasize++; + } + + // generate symbol names from filename + tb_char_t symbol_name[256] = {0}; + tb_char_t symbol_start[256] = {0}; + tb_char_t symbol_end[256] = {0}; + + // use basename or default to "data" + if (!basename || !basename[0]) { + basename = "data"; + } + + // build symbol name + if (symbol_prefix) { + tb_snprintf(symbol_name, sizeof(symbol_name), "%s%s", symbol_prefix, basename); + } else { + tb_snprintf(symbol_name, sizeof(symbol_name), "_binary_%s", basename); + } + + // replace non-alphanumeric with underscore + for (tb_size_t i = 0; symbol_name[i]; i++) { + if (!tb_isalpha(symbol_name[i]) && !tb_isdigit(symbol_name[i]) && symbol_name[i] != '_') { + symbol_name[i] = '_'; + } + } + + tb_snprintf(symbol_start, sizeof(symbol_start), "%s_start", symbol_name); + tb_snprintf(symbol_end, sizeof(symbol_end), "%s_end", symbol_name); + + // calculate offsets + tb_uint32_t header_size = sizeof(xm_elf64_header_t); + tb_uint32_t section_header_size = sizeof(xm_elf64_section_t); + tb_uint32_t section_count = 6; // NULL, .rodata, .symtab, .strtab, .shstrtab, .note.GNU-stack + tb_uint32_t section_headers_ofs = header_size; + tb_uint32_t rodata_ofs = section_headers_ofs + section_count * section_header_size; + tb_uint32_t rodata_size = datasize; + tb_uint32_t rodata_padding = (8 - (rodata_size & 7)) & 7; + tb_uint32_t symtab_ofs = rodata_ofs + rodata_size + rodata_padding; + tb_uint32_t symtab_size = 3 * sizeof(xm_elf64_symbol_t); // NULL, start, end + tb_uint32_t symtab_padding = (8 - (symtab_size & 7)) & 7; + tb_uint32_t strtab_ofs = symtab_ofs + symtab_size + symtab_padding; + + // calculate string table size + tb_size_t start_len = tb_strlen(symbol_start); + tb_size_t end_len = tb_strlen(symbol_end); + tb_uint32_t strtab_size = 1; // initial null byte + strtab_size += (tb_uint32_t)(start_len + 1); + strtab_size += (tb_uint32_t)(end_len + 1); + tb_uint32_t strtab_padding = (8 - (strtab_size & 7)) & 7; + + // calculate section header string table size + tb_uint32_t shstrtab_size = 1; // initial null byte + shstrtab_size += 8; // ".rodata\0" (7 + 1) + shstrtab_size += 8; // ".symtab\0" (7 + 1) + shstrtab_size += 8; // ".strtab\0" (7 + 1) + shstrtab_size += 10; // ".shstrtab\0" (9 + 1) + shstrtab_size += 16; // ".note.GNU-stack\0" (15 + 1) + tb_uint32_t shstrtab_ofs = strtab_ofs + strtab_size + strtab_padding; + + // write ELF header + xm_elf64_header_t header; + tb_memset(&header, 0, sizeof(header)); + header.e_ident[0] = 0x7f; + header.e_ident[1] = 'E'; + header.e_ident[2] = 'L'; + header.e_ident[3] = 'F'; + header.e_ident[4] = 2; // ELFCLASS64 + header.e_ident[5] = 1; // ELFDATA2LSB + header.e_ident[6] = 1; // EV_CURRENT + header.e_ident[7] = 0; // ELFOSABI_SYSV + header.e_type = 1; // ET_REL + header.e_machine = xm_binutils_bin2elf_get_machine(arch); + header.e_version = 1; + header.e_shoff = section_headers_ofs; + header.e_ehsize = header_size; + header.e_shentsize = section_header_size; + header.e_shnum = section_count; + header.e_shstrndx = 4; // .shstrtab section index + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&header, sizeof(header))) { + return tb_false; + } + + // write section headers + xm_elf64_section_t section_null; + tb_memset(§ion_null, 0, sizeof(section_null)); + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)§ion_null, sizeof(section_null))) { + return tb_false; + } + + // write .rodata section header + xm_elf64_section_t section_rodata; + tb_memset(§ion_rodata, 0, sizeof(section_rodata)); + section_rodata.sh_name = 1; // ".rodata" in shstrtab + section_rodata.sh_type = XM_ELF_SHT_PROGBITS; + section_rodata.sh_flags = XM_ELF_SHF_ALLOC; + section_rodata.sh_offset = rodata_ofs; + section_rodata.sh_size = rodata_size; + section_rodata.sh_addralign = 8; + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)§ion_rodata, sizeof(section_rodata))) { + return tb_false; + } + + // write .symtab section header + xm_elf64_section_t section_symtab; + tb_memset(§ion_symtab, 0, sizeof(section_symtab)); + section_symtab.sh_name = 9; // ".symtab" in shstrtab + section_symtab.sh_type = XM_ELF_SHT_SYMTAB; + section_symtab.sh_offset = symtab_ofs; + section_symtab.sh_size = symtab_size; + section_symtab.sh_link = 3; // .strtab section index + section_symtab.sh_info = 1; // first global symbol index + section_symtab.sh_addralign = 8; + section_symtab.sh_entsize = sizeof(xm_elf64_symbol_t); + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)§ion_symtab, sizeof(section_symtab))) { + return tb_false; + } + + // write .strtab section header + xm_elf64_section_t section_strtab; + tb_memset(§ion_strtab, 0, sizeof(section_strtab)); + section_strtab.sh_name = 17; // ".strtab" in shstrtab + section_strtab.sh_type = XM_ELF_SHT_STRTAB; + section_strtab.sh_offset = strtab_ofs; + section_strtab.sh_size = strtab_size; + section_strtab.sh_addralign = 1; + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)§ion_strtab, sizeof(section_strtab))) { + return tb_false; + } + + // write .shstrtab section header + xm_elf64_section_t section_shstrtab; + tb_memset(§ion_shstrtab, 0, sizeof(section_shstrtab)); + section_shstrtab.sh_name = 25; // ".shstrtab" in shstrtab + section_shstrtab.sh_type = XM_ELF_SHT_STRTAB; + section_shstrtab.sh_offset = shstrtab_ofs; // points to initial null byte + section_shstrtab.sh_size = shstrtab_size; // size includes initial null and all strings + section_shstrtab.sh_addralign = 1; + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)§ion_shstrtab, sizeof(section_shstrtab))) { + return tb_false; + } + + // write .note.GNU-stack section header (empty section to mark stack as non-executable) + xm_elf64_section_t section_note_gnu_stack; + tb_memset(§ion_note_gnu_stack, 0, sizeof(section_note_gnu_stack)); + section_note_gnu_stack.sh_name = 35; // ".note.GNU-stack" in shstrtab (25 + 10) + section_note_gnu_stack.sh_type = XM_ELF_SHT_PROGBITS; + section_note_gnu_stack.sh_flags = 0; // no flags + section_note_gnu_stack.sh_offset = shstrtab_ofs + shstrtab_size; // after .shstrtab + section_note_gnu_stack.sh_size = 0; // empty section + section_note_gnu_stack.sh_addralign = 1; + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)§ion_note_gnu_stack, sizeof(section_note_gnu_stack))) { + return tb_false; + } + + // write .rodata section data + if (!xm_binutils_stream_copy(istream, ostream, filesize)) { + return tb_false; + } + // append null terminator if zeroend is true + if (zeroend) { + tb_byte_t zero = 0; + if (!tb_stream_bwrit(ostream, &zero, 1)) { + return tb_false; + } + } + + // align .rodata to 8 bytes + if (rodata_padding > 0) { + tb_byte_t zero = 0; + while (rodata_padding-- > 0) { + if (!tb_stream_bwrit(ostream, &zero, 1)) { + return tb_false; + } + } + } + + // write symbol table + // symbol 0: NULL symbol + xm_elf64_symbol_t sym_null; + tb_memset(&sym_null, 0, sizeof(sym_null)); + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&sym_null, sizeof(sym_null))) { + return tb_false; + } + + // symbol 1: _binary_xxx_start + xm_elf64_symbol_t sym_start; + tb_memset(&sym_start, 0, sizeof(sym_start)); + sym_start.st_name = 1; // offset in .strtab (after initial null) + sym_start.st_info = (XM_ELF_STB_GLOBAL << 4) | XM_ELF_STT_OBJECT; + sym_start.st_shndx = 1; // .rodata section index + sym_start.st_value = 0; + sym_start.st_size = 0; + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&sym_start, sizeof(sym_start))) { + return tb_false; + } + + // symbol 2: _binary_xxx_end + xm_elf64_symbol_t sym_end; + tb_memset(&sym_end, 0, sizeof(sym_end)); + sym_end.st_name = 1 + (tb_uint32_t)(start_len + 1); // offset in .strtab + sym_end.st_info = (XM_ELF_STB_GLOBAL << 4) | XM_ELF_STT_OBJECT; + sym_end.st_shndx = 1; // .rodata section index + sym_end.st_value = rodata_size; + sym_end.st_size = 0; + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&sym_end, sizeof(sym_end))) { + return tb_false; + } + + // align .symtab to 8 bytes + if (symtab_padding > 0) { + tb_byte_t zero = 0; + while (symtab_padding-- > 0) { + if (!tb_stream_bwrit(ostream, &zero, 1)) { + return tb_false; + } + } + } + + // write string table + tb_byte_t null = 0; + if (!tb_stream_bwrit(ostream, &null, 1)) { + return tb_false; + } + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)symbol_start, start_len)) { + return tb_false; + } + if (!tb_stream_bwrit(ostream, &null, 1)) { + return tb_false; + } + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)symbol_end, end_len)) { + return tb_false; + } + if (!tb_stream_bwrit(ostream, &null, 1)) { + return tb_false; + } + + // align .strtab to 8 bytes + if (strtab_padding > 0) { + tb_byte_t zero = 0; + while (strtab_padding-- > 0) { + if (!tb_stream_bwrit(ostream, &zero, 1)) { + return tb_false; + } + } + } + + // write section header string table + if (!tb_stream_bwrit(ostream, &null, 1)) { + return tb_false; + } + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)".rodata", 7)) { + return tb_false; + } + if (!tb_stream_bwrit(ostream, &null, 1)) { + return tb_false; + } + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)".symtab", 7)) { + return tb_false; + } + if (!tb_stream_bwrit(ostream, &null, 1)) { + return tb_false; + } + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)".strtab", 7)) { + return tb_false; + } + if (!tb_stream_bwrit(ostream, &null, 1)) { + return tb_false; + } + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)".shstrtab", 9)) { + return tb_false; + } + if (!tb_stream_bwrit(ostream, &null, 1)) { + return tb_false; + } + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)".note.GNU-stack", 15)) { + return tb_false; + } + if (!tb_stream_bwrit(ostream, &null, 1)) { + return tb_false; + } + + return tb_true; +} + +/* ////////////////////////////////////////////////////////////////////////////////////// + * implementation + */ + +/* generate ELF object file from binary file + * + * local ok, errors = binutils.bin2elf(binaryfile, outputfile, symbol_prefix, arch, basename, zeroend) + */ +tb_int_t xm_binutils_bin2elf(lua_State *lua) { + tb_assert_and_check_return_val(lua, 0); + + // get the binaryfile + tb_char_t const *binaryfile = luaL_checkstring(lua, 1); + tb_check_return_val(binaryfile, 0); + + // get the outputfile + tb_char_t const *outputfile = luaL_checkstring(lua, 2); + tb_check_return_val(outputfile, 0); + + // get symbol prefix (optional) + tb_char_t const *symbol_prefix = lua_isstring(lua, 3) ? lua_tostring(lua, 3) : tb_null; + + // get arch (optional) + tb_char_t const *arch = lua_isstring(lua, 4) ? lua_tostring(lua, 4) : tb_null; + + // get basename (optional) + tb_char_t const *basename = lua_isstring(lua, 5) ? lua_tostring(lua, 5) : tb_null; + + // get zeroend (optional, default: false) + tb_bool_t zeroend = lua_toboolean(lua, 6); + + // do dump + tb_bool_t ok = tb_false; + tb_stream_ref_t istream = tb_stream_init_from_file(binaryfile, TB_FILE_MODE_RO); + tb_stream_ref_t ostream = tb_stream_init_from_file(outputfile, + TB_FILE_MODE_RW | TB_FILE_MODE_CREAT | TB_FILE_MODE_TRUNC); + do { + if (!tb_stream_open(istream)) { + lua_pushboolean(lua, tb_false); + lua_pushfstring(lua, "bin2elf: open %s failed", binaryfile); + break; + } + + if (!tb_stream_open(ostream)) { + lua_pushboolean(lua, tb_false); + lua_pushfstring(lua, "bin2elf: open %s failed", outputfile); + break; + } + + // choose 32-bit or 64-bit ELF based on architecture + tb_bool_t is_64bit = xm_binutils_bin2elf_is_64bit(arch); + if (is_64bit) { + 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_binutils_bin2elf_dump_32(istream, ostream, symbol_prefix, arch, basename, zeroend)) { + lua_pushboolean(lua, tb_false); + lua_pushfstring(lua, "bin2elf: dump data failed"); + break; + } + } + + ok = tb_true; + lua_pushboolean(lua, ok); + + } while (0); + + if (istream) + tb_stream_clos(istream); + istream = tb_null; + + if (ostream) + tb_stream_clos(ostream); + ostream = tb_null; + + return ok ? 1 : 2; +} + diff --git a/core/src/xmake/binutils/bin2macho.c b/core/src/xmake/binutils/bin2macho.c new file mode 100644 index 000000000..5add65d6c --- /dev/null +++ b/core/src/xmake/binutils/bin2macho.c @@ -0,0 +1,887 @@ +/*!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 bin2macho.c + * + */ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * trace + */ +#define TB_TRACE_MODULE_NAME "bin2macho" +#define TB_TRACE_MODULE_DEBUG (0) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ +#define XM_MACHO_MAGIC_32 0xfeedface +#define XM_MACHO_MAGIC_64 0xfeedfacf +#define XM_MACHO_MAGIC_FAT 0xcafebabe + +#define XM_MACHO_CPU_TYPE_X86 7 +#define XM_MACHO_CPU_TYPE_X86_64 0x01000007 +#define XM_MACHO_CPU_TYPE_ARM 12 +#define XM_MACHO_CPU_TYPE_ARM64 0x0100000c + +#define XM_MACHO_CPU_SUBTYPE_X86 3 +#define XM_MACHO_CPU_SUBTYPE_X86_64 3 +#define XM_MACHO_CPU_SUBTYPE_ARM 9 +#define XM_MACHO_CPU_SUBTYPE_ARM64 0 + +#define XM_MACHO_FILE_TYPE_OBJECT 1 + +#define XM_MACHO_LC_SEGMENT 0x1 +#define XM_MACHO_LC_SEGMENT_64 0x19 +#define XM_MACHO_LC_SYMTAB 0x2 +#define XM_MACHO_LC_BUILD_VERSION 0x32 + +#define XM_MACHO_PLATFORM_MACOS 1 +#define XM_MACHO_PLATFORM_IOS 2 +#define XM_MACHO_PLATFORM_TVOS 3 +#define XM_MACHO_PLATFORM_WATCHOS 4 + +#define XM_MACHO_SECT_TYPE_REGULAR 0x0 +#define XM_MACHO_SECT_ATTR_SOME_INITS 0x400 +#define XM_MACHO_SECT_ATTR_PURE_INSTRUCTIONS 0x80000000 + +#define XM_MACHO_N_TYPE_MASK 0x0e +#define XM_MACHO_N_TYPE_SECT 0x0e +#define XM_MACHO_N_EXT 0x01 + +#define XM_MACHO_VM_PROT_READ 1 +#define XM_MACHO_VM_PROT_WRITE 2 +#define XM_MACHO_VM_PROT_EXECUTE 4 + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ +#include "tbox/prefix/packed.h" +typedef struct __xm_macho_header_t { + tb_uint32_t magic; + tb_uint32_t cputype; + tb_uint32_t cpusubtype; + tb_uint32_t filetype; + tb_uint32_t ncmds; + tb_uint32_t sizeofcmds; + tb_uint32_t flags; +} __tb_packed__ xm_macho_header_t; + +typedef struct __xm_macho_header_64_t { + tb_uint32_t magic; + tb_uint32_t cputype; + tb_uint32_t cpusubtype; + tb_uint32_t filetype; + tb_uint32_t ncmds; + tb_uint32_t sizeofcmds; + tb_uint32_t flags; + tb_uint32_t reserved; +} __tb_packed__ xm_macho_header_64_t; + +typedef struct __xm_macho_segment_command_t { + tb_uint32_t cmd; + tb_uint32_t cmdsize; + tb_char_t segname[16]; + tb_uint32_t vmaddr; + tb_uint32_t vmsize; + tb_uint32_t fileoff; + tb_uint32_t filesize; + tb_uint32_t maxprot; + tb_uint32_t initprot; + tb_uint32_t nsects; + tb_uint32_t flags; +} __tb_packed__ xm_macho_segment_command_t; + +typedef struct __xm_macho_segment_command_64_t { + tb_uint32_t cmd; + tb_uint32_t cmdsize; + tb_char_t segname[16]; + tb_uint64_t vmaddr; + tb_uint64_t vmsize; + tb_uint64_t fileoff; + tb_uint64_t filesize; + tb_uint32_t maxprot; + tb_uint32_t initprot; + tb_uint32_t nsects; + tb_uint32_t flags; +} __tb_packed__ xm_macho_segment_command_64_t; + +typedef struct __xm_macho_section_t { + tb_char_t sectname[16]; + tb_char_t segname[16]; + tb_uint32_t addr; + tb_uint32_t size; + tb_uint32_t offset; + tb_uint32_t align; + tb_uint32_t reloff; + tb_uint32_t nreloc; + tb_uint32_t flags; + tb_uint32_t reserved1; + tb_uint32_t reserved2; +} __tb_packed__ xm_macho_section_t; + +typedef struct __xm_macho_section_64_t { + tb_char_t sectname[16]; + tb_char_t segname[16]; + tb_uint64_t addr; + tb_uint64_t size; + tb_uint32_t offset; + tb_uint32_t align; + tb_uint32_t reloff; + tb_uint32_t nreloc; + tb_uint32_t flags; + tb_uint32_t reserved1; + tb_uint32_t reserved2; + tb_uint32_t reserved3; +} __tb_packed__ xm_macho_section_64_t; + +typedef struct __xm_macho_symtab_command_t { + tb_uint32_t cmd; + tb_uint32_t cmdsize; + tb_uint32_t symoff; + tb_uint32_t nsyms; + tb_uint32_t stroff; + tb_uint32_t strsize; +} __tb_packed__ xm_macho_symtab_command_t; + +typedef struct __xm_macho_build_version_command_t { + tb_uint32_t cmd; + tb_uint32_t cmdsize; + tb_uint32_t platform; + tb_uint32_t minos; + tb_uint32_t sdk; + tb_uint32_t ntools; +} __tb_packed__ xm_macho_build_version_command_t; + +typedef struct __xm_macho_nlist_t { + tb_uint32_t strx; + tb_uint8_t type; + tb_uint8_t sect; + tb_int16_t desc; + tb_uint32_t value; +} __tb_packed__ xm_macho_nlist_t; + +typedef struct __xm_macho_nlist_64_t { + tb_uint32_t strx; + tb_uint8_t type; + tb_uint8_t sect; + tb_uint16_t desc; + tb_uint64_t value; +} __tb_packed__ xm_macho_nlist_64_t; +#include "tbox/prefix/packed.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * private implementation + */ +static tb_uint32_t xm_binutils_bin2macho_get_cputype(tb_char_t const *arch) { + if (!arch) { + return XM_MACHO_CPU_TYPE_X86_64; + } + if (tb_strcmp(arch, "x86_64") == 0 || tb_strcmp(arch, "x64") == 0) { + return XM_MACHO_CPU_TYPE_X86_64; + } else if (tb_strcmp(arch, "arm64") == 0 || tb_strcmp(arch, "aarch64") == 0) { + return XM_MACHO_CPU_TYPE_ARM64; + } else if (tb_strcmp(arch, "arm") == 0) { + return XM_MACHO_CPU_TYPE_ARM; + } else if (tb_strcmp(arch, "x86") == 0 || tb_strcmp(arch, "i386") == 0) { + return XM_MACHO_CPU_TYPE_X86; + } + return XM_MACHO_CPU_TYPE_X86_64; +} + +static tb_uint32_t xm_binutils_bin2macho_get_cpusubtype(tb_char_t const *arch) { + if (!arch) { + return XM_MACHO_CPU_SUBTYPE_X86_64; + } + if (tb_strcmp(arch, "x86_64") == 0 || tb_strcmp(arch, "x64") == 0) { + return XM_MACHO_CPU_SUBTYPE_X86_64; + } else if (tb_strcmp(arch, "arm64") == 0 || tb_strcmp(arch, "aarch64") == 0) { + return XM_MACHO_CPU_SUBTYPE_ARM64; + } else if (tb_strcmp(arch, "arm") == 0) { + return XM_MACHO_CPU_SUBTYPE_ARM; + } else if (tb_strcmp(arch, "x86") == 0 || tb_strcmp(arch, "i386") == 0) { + return XM_MACHO_CPU_SUBTYPE_X86; + } + return XM_MACHO_CPU_SUBTYPE_X86_64; +} + +static tb_bool_t xm_binutils_bin2macho_is_64bit(tb_char_t const *arch) { + if (!arch) { + return tb_true; + } + if (tb_strcmp(arch, "x86_64") == 0 || tb_strcmp(arch, "x64") == 0) { + return tb_true; + } else if (tb_strcmp(arch, "arm64") == 0 || tb_strcmp(arch, "aarch64") == 0) { + return tb_true; + } else if (tb_strcmp(arch, "arm") == 0) { + return tb_false; + } else if (tb_strcmp(arch, "x86") == 0 || tb_strcmp(arch, "i386") == 0) { + return tb_false; + } + return tb_true; +} + +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_binutils_bin2macho_get_platform(tb_char_t const *platform) { + if (!platform) { + return XM_MACHO_PLATFORM_MACOS; + } + if (tb_strcmp(platform, "macosx") == 0 || tb_strcmp(platform, "macos") == 0) { + return XM_MACHO_PLATFORM_MACOS; + } else if (tb_strcmp(platform, "iphoneos") == 0 || tb_strcmp(platform, "ios") == 0) { + return XM_MACHO_PLATFORM_IOS; + } else if (tb_strcmp(platform, "appletvos") == 0 || tb_strcmp(platform, "tvos") == 0) { + return XM_MACHO_PLATFORM_TVOS; + } else if (tb_strcmp(platform, "watchos") == 0) { + return XM_MACHO_PLATFORM_WATCHOS; + } + return XM_MACHO_PLATFORM_MACOS; +} + +// 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_binutils_bin2macho_parse_version(tb_char_t const *version_str) { + if (!version_str || !version_str[0]) { + return 0x000a0000; // default: 10.0.0 + } + tb_uint32_t major = 0; + tb_uint32_t minor = 0; + tb_uint32_t patch = 0; + tb_char_t const *p = version_str; + // parse major + while (*p && tb_isdigit(*p)) { + major = major * 10 + (*p - '0'); + p++; + } + if (*p == '.') { + p++; + // parse minor + while (*p && tb_isdigit(*p)) { + minor = minor * 10 + (*p - '0'); + p++; + } + if (*p == '.') { + p++; + // parse patch + while (*p && tb_isdigit(*p)) { + patch = patch * 10 + (*p - '0'); + p++; + } + } + } + return (major << 16) | (minor << 8) | patch; +} + +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, + tb_char_t const *arch, + tb_char_t const *basename, + tb_uint32_t minos, + tb_uint32_t sdk, + tb_bool_t zeroend) { + tb_assert_and_check_return_val(istream && ostream, tb_false); + + // get file size + tb_hong_t filesize = tb_stream_size(istream); + if (filesize < 0 || filesize > 0xffffffffU) { + return tb_false; + } + tb_uint32_t datasize = (tb_uint32_t)filesize; + // add null terminator if zeroend is true + if (zeroend) { + if (datasize >= 0xffffffffU) { + return tb_false; // would overflow + } + datasize++; + } + + // generate symbol names from filename + tb_char_t symbol_name[256] = {0}; + tb_char_t symbol_start[256] = {0}; + tb_char_t symbol_end[256] = {0}; + + // use basename or default to "data" + if (!basename || !basename[0]) { + basename = "data"; + } + + // build symbol name + // On macOS, C compiler adds an underscore prefix, so we generate symbols with two underscores + // (C code declares _binary_xxx, compiler generates __binary_xxx in object file, so we define __binary_xxx) + if (symbol_prefix) { + tb_snprintf(symbol_name, sizeof(symbol_name), "_%s%s", symbol_prefix, basename); + } else { + tb_snprintf(symbol_name, sizeof(symbol_name), "__binary_%s", basename); + } + + // replace non-alphanumeric with underscore + for (tb_size_t i = 0; symbol_name[i]; i++) { + if (!tb_isalpha(symbol_name[i]) && !tb_isdigit(symbol_name[i]) && symbol_name[i] != '_') { + symbol_name[i] = '_'; + } + } + + tb_snprintf(symbol_start, sizeof(symbol_start), "%s_start", symbol_name); + tb_snprintf(symbol_end, sizeof(symbol_end), "%s_end", symbol_name); + + // calculate offsets + tb_uint32_t header_size = sizeof(xm_macho_header_64_t); + tb_uint32_t segment_cmd_size = sizeof(xm_macho_segment_command_64_t); + tb_uint32_t section_size = sizeof(xm_macho_section_64_t); + 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_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_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; + tb_uint32_t strtab_size = 4; // initial 4-byte size field + tb_size_t start_len = tb_strlen(symbol_start); + 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_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_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; + header.flags = 0; + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&header, sizeof(header))) { + return tb_false; + } + + // write segment command + xm_macho_segment_command_64_t segment; + tb_memset(&segment, 0, sizeof(segment)); + segment.cmd = XM_MACHO_LC_SEGMENT_64; + segment.cmdsize = segment_cmd_total_size; + tb_strncpy(segment.segname, "__TEXT", 16); + segment.vmaddr = 0; + segment.vmsize = data_size; + segment.fileoff = data_offset; + segment.filesize = data_size; + segment.maxprot = XM_MACHO_VM_PROT_READ; // VM_PROT_READ (read-only) + segment.initprot = XM_MACHO_VM_PROT_READ; // VM_PROT_READ (read-only) + segment.nsects = 1; + segment.flags = 0; + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&segment, sizeof(segment))) { + return tb_false; + } + + // write section + xm_macho_section_64_t section; + tb_memset(§ion, 0, sizeof(section)); + tb_strncpy(section.sectname, "__const", 16); + tb_strncpy(section.segname, "__TEXT", 16); + section.addr = 0; + section.size = data_size; + section.offset = data_offset; + section.align = 3; // 2^3 = 8 bytes + section.reloff = 0; + section.nreloc = 0; + section.flags = XM_MACHO_SECT_TYPE_REGULAR | XM_MACHO_SECT_ATTR_SOME_INITS; + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)§ion, sizeof(section))) { + return tb_false; + } + + // write symtab command + xm_macho_symtab_command_t symtab; + tb_memset(&symtab, 0, sizeof(symtab)); + symtab.cmd = XM_MACHO_LC_SYMTAB; + symtab.cmdsize = symtab_cmd_size; + symtab.symoff = symtab_offset; + symtab.nsyms = nlist_count; + symtab.stroff = strtab_offset; + symtab.strsize = strtab_size; + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&symtab, sizeof(symtab))) { + return tb_false; + } + + // write build version command + xm_macho_build_version_command_t build_version; + 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_binutils_bin2macho_get_platform(plat); + build_version.minos = minos; + build_version.sdk = sdk; + build_version.ntools = 0; + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&build_version, sizeof(build_version))) { + return tb_false; + } + + // align to 8 bytes + tb_uint32_t padding = data_offset - (header_size + segment_cmd_total_size + symtab_cmd_size + build_version_cmd_size); + if (padding > 0) { + tb_byte_t zero = 0; + while (padding-- > 0) { + if (!tb_stream_bwrit(ostream, &zero, 1)) { + return tb_false; + } + } + } + + // write section data + if (!xm_binutils_stream_copy(istream, ostream, filesize)) { + return tb_false; + } + // append null terminator if zeroend is true + if (zeroend) { + tb_byte_t zero = 0; + if (!tb_stream_bwrit(ostream, &zero, 1)) { + return tb_false; + } + } + + // align to 8 bytes + padding = symtab_offset - data_end_offset; + if (padding > 0) { + tb_byte_t zero = 0; + while (padding-- > 0) { + if (!tb_stream_bwrit(ostream, &zero, 1)) { + return tb_false; + } + } + } + + // write symbol table + // strx starts from 4 (after 4-byte size field) + tb_uint32_t strx = 4; + // symbol 0: _binary_xxx_start + xm_macho_nlist_64_t nlist_start; + tb_memset(&nlist_start, 0, sizeof(nlist_start)); + nlist_start.strx = strx; + nlist_start.type = XM_MACHO_N_TYPE_SECT | XM_MACHO_N_EXT; // N_SECT | N_EXT + nlist_start.sect = 1; + nlist_start.desc = 0; + nlist_start.value = 0; + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&nlist_start, sizeof(nlist_start))) { + return tb_false; + } + strx += (tb_uint32_t)(start_len + 1); + + // symbol 1: _binary_xxx_end + xm_macho_nlist_64_t nlist_end; + tb_memset(&nlist_end, 0, sizeof(nlist_end)); + nlist_end.strx = strx; + nlist_end.type = XM_MACHO_N_TYPE_SECT | XM_MACHO_N_EXT; // N_SECT | N_EXT + nlist_end.sect = 1; + nlist_end.desc = 0; + nlist_end.value = data_size; + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&nlist_end, sizeof(nlist_end))) { + return tb_false; + } + strx += (tb_uint32_t)(end_len + 1); + + // align to 8 bytes + padding = strtab_offset - (symtab_offset + nlist_size * nlist_count); + if (padding > 0) { + tb_byte_t zero = 0; + while (padding-- > 0) { + if (!tb_stream_bwrit(ostream, &zero, 1)) { + return tb_false; + } + } + } + + // write string table + tb_stream_bwrit(ostream, (tb_byte_t const *)&strtab_size, 4); + tb_stream_bwrit(ostream, (tb_byte_t const *)symbol_start, start_len); + tb_byte_t null = 0; + tb_stream_bwrit(ostream, &null, 1); + tb_stream_bwrit(ostream, (tb_byte_t const *)symbol_end, end_len); + tb_stream_bwrit(ostream, &null, 1); + + // align string table to 8 bytes + padding = strtab_size - (4 + (tb_uint32_t)start_len + 1 + (tb_uint32_t)end_len + 1); + if (padding > 0) { + tb_byte_t zero = 0; + while (padding-- > 0) { + if (!tb_stream_bwrit(ostream, &zero, 1)) { + return tb_false; + } + } + } + + return tb_true; +} + +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, + tb_char_t const *arch, + tb_char_t const *basename, + tb_uint32_t minos, + tb_uint32_t sdk, + tb_bool_t zeroend) { + tb_assert_and_check_return_val(istream && ostream, tb_false); + + // get file size + tb_hong_t filesize = tb_stream_size(istream); + if (filesize < 0 || filesize > 0xffffffffU) { + return tb_false; + } + tb_uint32_t datasize = (tb_uint32_t)filesize; + // add null terminator if zeroend is true + if (zeroend) { + if (datasize >= 0xffffffffU) { + return tb_false; // would overflow + } + datasize++; + } + + // generate symbol names from filename + tb_char_t symbol_name[256] = {0}; + tb_char_t symbol_start[256] = {0}; + tb_char_t symbol_end[256] = {0}; + + // use basename or default to "data" + if (!basename || !basename[0]) { + basename = "data"; + } + + // build symbol name + // On macOS, C compiler adds an underscore prefix, so we generate symbols with two underscores + // (C code declares _binary_xxx, compiler generates __binary_xxx in object file, so we define __binary_xxx) + if (symbol_prefix) { + tb_snprintf(symbol_name, sizeof(symbol_name), "_%s%s", symbol_prefix, basename); + } else { + tb_snprintf(symbol_name, sizeof(symbol_name), "__binary_%s", basename); + } + + // replace non-alphanumeric with underscore + for (tb_size_t i = 0; symbol_name[i]; i++) { + if (!tb_isalpha(symbol_name[i]) && !tb_isdigit(symbol_name[i]) && symbol_name[i] != '_') { + symbol_name[i] = '_'; + } + } + + tb_snprintf(symbol_start, sizeof(symbol_start), "%s_start", symbol_name); + tb_snprintf(symbol_end, sizeof(symbol_end), "%s_end", symbol_name); + + // calculate offsets + tb_uint32_t header_size = sizeof(xm_macho_header_t); + tb_uint32_t segment_cmd_size = sizeof(xm_macho_segment_command_t); + tb_uint32_t section_size = sizeof(xm_macho_section_t); + 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_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_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; + tb_uint32_t strtab_size = 4; // initial 4-byte size field + tb_size_t start_len = tb_strlen(symbol_start); + 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_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_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; + header.flags = 0; + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&header, sizeof(header))) { + return tb_false; + } + + // write segment command + xm_macho_segment_command_t segment; + tb_memset(&segment, 0, sizeof(segment)); + segment.cmd = XM_MACHO_LC_SEGMENT; + segment.cmdsize = segment_cmd_total_size; + tb_strncpy(segment.segname, "__TEXT", 16); + segment.vmaddr = 0; + segment.vmsize = data_size; + segment.fileoff = data_offset; + segment.filesize = data_size; + segment.maxprot = XM_MACHO_VM_PROT_READ; // VM_PROT_READ (read-only) + segment.initprot = XM_MACHO_VM_PROT_READ; // VM_PROT_READ (read-only) + segment.nsects = 1; + segment.flags = 0; + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&segment, sizeof(segment))) { + return tb_false; + } + + // write section + xm_macho_section_t section; + tb_memset(§ion, 0, sizeof(section)); + tb_strncpy(section.sectname, "__const", 16); + tb_strncpy(section.segname, "__TEXT", 16); + section.addr = 0; + section.size = data_size; + section.offset = data_offset; + section.align = 2; // 2^2 = 4 bytes + section.reloff = 0; + section.nreloc = 0; + section.flags = XM_MACHO_SECT_TYPE_REGULAR | XM_MACHO_SECT_ATTR_SOME_INITS; + section.reserved1 = 0; + section.reserved2 = 0; + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)§ion, sizeof(section))) { + return tb_false; + } + + // write symtab command + xm_macho_symtab_command_t symtab; + tb_memset(&symtab, 0, sizeof(symtab)); + symtab.cmd = XM_MACHO_LC_SYMTAB; + symtab.cmdsize = symtab_cmd_size; + symtab.symoff = symtab_offset; + symtab.nsyms = nlist_count; + symtab.stroff = strtab_offset; + symtab.strsize = strtab_size; + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&symtab, sizeof(symtab))) { + return tb_false; + } + + // write build version command + xm_macho_build_version_command_t build_version; + 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_binutils_bin2macho_get_platform(plat); + build_version.minos = minos; + build_version.sdk = sdk; + build_version.ntools = 0; + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&build_version, sizeof(build_version))) { + return tb_false; + } + + // align to 4 bytes + tb_uint32_t padding = data_offset - (header_size + segment_cmd_total_size + symtab_cmd_size + build_version_cmd_size); + if (padding > 0) { + tb_byte_t zero = 0; + while (padding-- > 0) { + if (!tb_stream_bwrit(ostream, &zero, 1)) { + return tb_false; + } + } + } + + // write section data + if (!xm_binutils_stream_copy(istream, ostream, filesize)) { + return tb_false; + } + // append null terminator if zeroend is true + if (zeroend) { + tb_byte_t zero = 0; + if (!tb_stream_bwrit(ostream, &zero, 1)) { + return tb_false; + } + } + + // align to 4 bytes + padding = symtab_offset - data_end_offset; + if (padding > 0) { + tb_byte_t zero = 0; + while (padding-- > 0) { + if (!tb_stream_bwrit(ostream, &zero, 1)) { + return tb_false; + } + } + } + + // write symbol table + // strx starts from 4 (after 4-byte size field) + tb_uint32_t strx = 4; + // symbol 0: _binary_xxx_start + xm_macho_nlist_t nlist_start; + tb_memset(&nlist_start, 0, sizeof(nlist_start)); + nlist_start.strx = strx; + nlist_start.type = XM_MACHO_N_TYPE_SECT | XM_MACHO_N_EXT; // N_SECT | N_EXT + nlist_start.sect = 1; + nlist_start.desc = 0; + nlist_start.value = 0; + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&nlist_start, sizeof(nlist_start))) { + return tb_false; + } + strx += (tb_uint32_t)(start_len + 1); + + // symbol 1: _binary_xxx_end + xm_macho_nlist_t nlist_end; + tb_memset(&nlist_end, 0, sizeof(nlist_end)); + nlist_end.strx = strx; + nlist_end.type = XM_MACHO_N_TYPE_SECT | XM_MACHO_N_EXT; // N_SECT | N_EXT + nlist_end.sect = 1; + nlist_end.desc = 0; + nlist_end.value = data_size; + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&nlist_end, sizeof(nlist_end))) { + return tb_false; + } + strx += (tb_uint32_t)(end_len + 1); + + // align to 4 bytes + padding = strtab_offset - (symtab_offset + nlist_size * nlist_count); + if (padding > 0) { + tb_byte_t zero = 0; + while (padding-- > 0) { + if (!tb_stream_bwrit(ostream, &zero, 1)) { + return tb_false; + } + } + } + + // write string table + tb_stream_bwrit(ostream, (tb_byte_t const *)&strtab_size, 4); + tb_stream_bwrit(ostream, (tb_byte_t const *)symbol_start, start_len); + tb_byte_t null = 0; + tb_stream_bwrit(ostream, &null, 1); + tb_stream_bwrit(ostream, (tb_byte_t const *)symbol_end, end_len); + tb_stream_bwrit(ostream, &null, 1); + + // align string table to 4 bytes + padding = strtab_size - (4 + (tb_uint32_t)start_len + 1 + (tb_uint32_t)end_len + 1); + if (padding > 0) { + tb_byte_t zero = 0; + while (padding-- > 0) { + if (!tb_stream_bwrit(ostream, &zero, 1)) { + return tb_false; + } + } + } + + return tb_true; +} + +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, + tb_char_t const *arch, + tb_char_t const *basename, + tb_uint32_t minos, + tb_uint32_t sdk, + tb_bool_t 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_binutils_bin2macho_dump_32(istream, ostream, symbol_prefix, plat, arch, basename, minos, sdk, zeroend); + } +} + +/* ////////////////////////////////////////////////////////////////////////////////////// + * implementation + */ + +/* generate Mach-O object file from binary file + * + * local ok, errors = binutils.bin2macho(binaryfile, outputfile, symbol_prefix, plat, arch, basename) + */ +tb_int_t xm_binutils_bin2macho(lua_State *lua) { + tb_assert_and_check_return_val(lua, 0); + + // get the binaryfile + tb_char_t const *binaryfile = luaL_checkstring(lua, 1); + tb_check_return_val(binaryfile, 0); + + // get the outputfile + tb_char_t const *outputfile = luaL_checkstring(lua, 2); + tb_check_return_val(outputfile, 0); + + // get symbol prefix (optional) + tb_char_t const *symbol_prefix = lua_isstring(lua, 3) ? lua_tostring(lua, 3) : tb_null; + + // get plat (optional) + tb_char_t const *plat = lua_isstring(lua, 4) ? lua_tostring(lua, 4) : tb_null; + + // get arch (optional) + tb_char_t const *arch = lua_isstring(lua, 5) ? lua_tostring(lua, 5) : tb_null; + + // get basename (optional) + tb_char_t const *basename = lua_isstring(lua, 6) ? lua_tostring(lua, 6) : tb_null; + + // 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_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_binutils_bin2macho_parse_version(sdk_str); + + // get zeroend (optional, default: false) + tb_bool_t zeroend = lua_toboolean(lua, 9); + + // do dump + tb_bool_t ok = tb_false; + tb_stream_ref_t istream = tb_stream_init_from_file(binaryfile, TB_FILE_MODE_RO); + tb_stream_ref_t ostream = tb_stream_init_from_file(outputfile, + TB_FILE_MODE_RW | TB_FILE_MODE_CREAT | TB_FILE_MODE_TRUNC); + do { + if (!tb_stream_open(istream)) { + lua_pushboolean(lua, tb_false); + lua_pushfstring(lua, "bin2macho: open %s failed", binaryfile); + break; + } + + if (!tb_stream_open(ostream)) { + lua_pushboolean(lua, tb_false); + lua_pushfstring(lua, "bin2macho: open %s failed", outputfile); + break; + } + + 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; + } + + ok = tb_true; + lua_pushboolean(lua, ok); + + } while (0); + + if (istream) { + tb_stream_clos(istream); + } + istream = tb_null; + + if (ostream) { + tb_stream_clos(ostream); + } + ostream = tb_null; + + return ok ? 1 : 2; +} + diff --git a/core/src/xmake/binutils/prefix.h b/core/src/xmake/binutils/prefix.h new file mode 100644 index 000000000..739e220f7 --- /dev/null +++ b/core/src/xmake/binutils/prefix.h @@ -0,0 +1,70 @@ +/*!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 prefix.h + * + */ +#ifndef XM_BINUTILS_PREFIX_H +#define XM_BINUTILS_PREFIX_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "../prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * inline implementation + */ + +/* copy data from input stream to output stream + * + * @param istream the input stream + * @param ostream the output stream + * @param size the size to copy + * + * @return tb_true on success, tb_false on failure + */ +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]; + tb_hize_t writ = 0; + do { + tb_size_t need = (tb_size_t)tb_min(size - writ, (tb_hize_t)TB_STREAM_BLOCK_MAXN); + tb_check_break(need); + + tb_long_t real = tb_stream_read(istream, data, need); + if (real > 0) { + if (!tb_stream_bwrit(ostream, data, (tb_size_t)real)) { + return tb_false; + } + writ += real; + } else if (!real) { + tb_long_t wait = tb_stream_wait(istream, TB_STREAM_WAIT_READ, tb_stream_timeout(istream)); + tb_check_break(wait > 0 && (wait & TB_STREAM_WAIT_READ)); + } else { + return tb_false; + } + + tb_check_break(writ < size); + } while (1); + + return tb_true; +} + +#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/utils/bin2c.c b/core/src/xmake/utils/bin2c.c deleted file mode 100644 index a1ca2a25d..000000000 --- a/core/src/xmake/utils/bin2c.c +++ /dev/null @@ -1,224 +0,0 @@ -/*!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 bin2c.c - * - */ - -/* ////////////////////////////////////////////////////////////////////////////////////// - * trace - */ -#define TB_TRACE_MODULE_NAME "bin2c" -#define TB_TRACE_MODULE_DEBUG (0) - -/* ////////////////////////////////////////////////////////////////////////////////////// - * includes - */ -#include "prefix.h" - -/* ////////////////////////////////////////////////////////////////////////////////////// - * macros - */ - -#define XM_BIN2C_DATA_SIZE (8 * 1024) -#define XM_BIN2C_LINE_SIZE (4 * 1024) -#define XM_BIN2C_LINEWIDTH_MAX ((XM_BIN2C_LINE_SIZE - 2) / 6) - -/* ////////////////////////////////////////////////////////////////////////////////////// - * private implementation - */ - -// optimized hex conversion table -static tb_char_t const *xm_utils_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) { - 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]; -} - -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) { - - tb_bool_t first = tb_true; - tb_bool_t zero_pending = tb_false; - tb_byte_t data[XM_BIN2C_DATA_SIZE]; - tb_char_t line[XM_BIN2C_LINE_SIZE]; - tb_size_t linesize = 0; - tb_size_t bytes_in_line = 0; - tb_size_t data_pos = 0; - tb_size_t data_size = 0; - tb_assert_and_check_return_val(linewidth > 0 && linewidth <= XM_BIN2C_LINEWIDTH_MAX, tb_false); - - while (!tb_stream_beof(istream) || data_pos < data_size || zero_pending) { - // read a large chunk of data if buffer is empty - if (data_pos >= data_size) { - // handle pending zero terminator - if (zero_pending) { - data[0] = '\0'; - data_size = 1; - data_pos = 0; - zero_pending = tb_false; - } 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; - } - - if (!tb_stream_bread(istream, data, to_read)) { - break; - } - data_size = to_read; - data_pos = 0; - - // check if we need to add zero terminator at the end - if (!nozeroend && tb_stream_beof(istream)) { - if (data_size < XM_BIN2C_DATA_SIZE) { - // can add directly to current buffer - data[data_size++] = '\0'; - } else { - // buffer is full, need to add zero in next iteration - zero_pending = tb_true; - } - } - } - } - - // process bytes from buffer - while (data_pos < data_size) { - // check if we need a new line - if (bytes_in_line >= (tb_size_t)linewidth) { - // write line (tb_stream_bwrit_line will add newline automatically) - if (tb_stream_bwrit_line(ostream, line, linesize) < 0) { - return tb_false; - } - - linesize = 0; - bytes_in_line = 0; - first = tb_false; - } - - // ensure we have enough space in line buffer (6 chars per byte: ", 0xXX") - if (linesize + 6 > sizeof(line)) { - // flush partial line if buffer is full - if (linesize > 0) { - if (!tb_stream_bwrit(ostream, (tb_byte_t *)line, linesize)) { - return tb_false; - } - linesize = 0; - } - } - - // add separator - if (bytes_in_line == 0 && first) { - line[linesize++] = ' '; - first = tb_false; - } else { - line[linesize++] = ','; - } - - // write hex value (inline for performance) - xm_utils_bin2c_write_hex(line + linesize, data[data_pos]); - linesize += 5; - bytes_in_line++; - data_pos++; - } - } - - // flush remaining line - if (linesize > 0) { - // write line (tb_stream_bwrit_line will add newline automatically) - if (tb_stream_bwrit_line(ostream, line, linesize) < 0) { - return tb_false; - } - } - - return tb_stream_beof(istream); -} - -/* ////////////////////////////////////////////////////////////////////////////////////// - * implementation - */ - -/* generate c/c++ code from the binary file - * - * local ok, errors = utils.bin2c(binaryfile, outputfile, linewidth, nozeroend) - */ -tb_int_t xm_utils_bin2c(lua_State *lua) { - tb_assert_and_check_return_val(lua, 0); - - // get the binaryfile - tb_char_t const *binaryfile = luaL_checkstring(lua, 1); - tb_check_return_val(binaryfile, 0); - - // get the outputfile - tb_char_t const *outputfile = luaL_checkstring(lua, 2); - tb_check_return_val(outputfile, 0); - - // get line width - tb_int_t linewidth = (tb_int_t)lua_tointeger(lua, 3); - - // no zero end? - tb_bool_t nozeroend = (tb_bool_t)lua_toboolean(lua, 4); - - // do dump - tb_bool_t ok = tb_false; - tb_stream_ref_t istream = tb_stream_init_from_file(binaryfile, TB_FILE_MODE_RO); - tb_stream_ref_t ostream = tb_stream_init_from_file(outputfile, - TB_FILE_MODE_RW | TB_FILE_MODE_CREAT | TB_FILE_MODE_TRUNC); - do { - if (!tb_stream_open(istream)) { - lua_pushboolean(lua, tb_false); - lua_pushfstring(lua, "bin2c: open %s failed", binaryfile); - break; - } - - if (!tb_stream_open(ostream)) { - lua_pushboolean(lua, tb_false); - lua_pushfstring(lua, "bin2c: open %s failed", outputfile); - break; - } - - if (!xm_utils_bin2c_dump(istream, ostream, linewidth, nozeroend)) { - lua_pushboolean(lua, tb_false); - lua_pushfstring(lua, "bin2c: dump data failed"); - break; - } - - ok = tb_true; - lua_pushboolean(lua, ok); - - } while (0); - - if (istream) { - tb_stream_clos(istream); - } - istream = tb_null; - - if (ostream) { - tb_stream_clos(ostream); - } - ostream = tb_null; - - return ok ? 1 : 2; -} diff --git a/core/src/xmake/utils/bin2coff.c b/core/src/xmake/utils/bin2coff.c deleted file mode 100644 index 976f18202..000000000 --- a/core/src/xmake/utils/bin2coff.c +++ /dev/null @@ -1,406 +0,0 @@ -/*!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 bin2coff.c - * - */ - -/* ////////////////////////////////////////////////////////////////////////////////////// - * trace - */ -#define TB_TRACE_MODULE_NAME "bin2coff" -#define TB_TRACE_MODULE_DEBUG (0) - -/* ////////////////////////////////////////////////////////////////////////////////////// - * includes - */ -#include "prefix.h" - -/* ////////////////////////////////////////////////////////////////////////////////////// - * macros - */ -#define XM_COFF_MACHINE_I386 0x014c -#define XM_COFF_MACHINE_AMD64 0x8664 -#define XM_COFF_MACHINE_ARM 0x01c0 -#define XM_COFF_MACHINE_ARM64 0xaa64 - -#define XM_COFF_SECTION_RDATA 0x40000040 // IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ - -/* ////////////////////////////////////////////////////////////////////////////////////// - * types - */ -#include "tbox/prefix/packed.h" -typedef struct __xm_coff_header_t { - tb_uint16_t machine; - tb_uint16_t nsects; - tb_uint32_t time; - tb_uint32_t symtabofs; - tb_uint32_t nsyms; - tb_uint16_t opthdr; - tb_uint16_t flags; -} __tb_packed__ xm_coff_header_t; - -typedef struct __xm_coff_section_t { - tb_char_t name[8]; - tb_uint32_t vsize; - tb_uint32_t vaddr; - tb_uint32_t size; - tb_uint32_t ofs; - tb_uint32_t relocofs; - tb_uint32_t linenoofs; - tb_uint16_t nreloc; - tb_uint16_t nlineno; - tb_uint32_t flags; -} __tb_packed__ xm_coff_section_t; - -typedef struct __xm_coff_symbol_t { - union { - struct { - tb_char_t name[8]; - } shortname; - struct { - tb_uint32_t zeros; - tb_uint32_t offset; - } longname; - } n; - tb_uint32_t value; - tb_int16_t sect; - tb_uint16_t type; - tb_uint8_t scl; - tb_uint8_t naux; -} __tb_packed__ xm_coff_symbol_t; - -typedef struct __xm_coff_aux_section_t { - tb_uint32_t length; - tb_uint16_t nreloc; - tb_uint16_t nlineno; - tb_uint8_t reserved[10]; -} __tb_packed__ xm_coff_aux_section_t; - -typedef struct __xm_coff_symbol_tail_t { - tb_uint32_t value; - tb_int16_t sect; - tb_uint16_t type; - tb_uint8_t scl; - tb_uint8_t naux; -} __tb_packed__ xm_coff_symbol_tail_t; -#include "tbox/prefix/packed.h" - -/* ////////////////////////////////////////////////////////////////////////////////////// - * private implementation - */ -static tb_uint16_t xm_utils_bin2coff_get_machine(tb_char_t const *arch) { - if (!arch) { - return XM_COFF_MACHINE_I386; - } - if (tb_strcmp(arch, "x86_64") == 0 || tb_strcmp(arch, "x64") == 0) { - return XM_COFF_MACHINE_AMD64; - } else if (tb_strcmp(arch, "arm64") == 0 || tb_strcmp(arch, "aarch64") == 0) { - return XM_COFF_MACHINE_ARM64; - } else if (tb_strcmp(arch, "arm") == 0) { - return XM_COFF_MACHINE_ARM; - } else if (tb_strcmp(arch, "i386") == 0 || tb_strcmp(arch, "x86") == 0) { - return XM_COFF_MACHINE_I386; - } - 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) { - tb_assert_and_check_return(ostream && str); - if (len == 0) { - len = tb_strlen(str); - } - 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) { - tb_assert_and_check_return(ostream); - tb_byte_t zero = 0; - while (count-- > 0) { - tb_stream_bwrit(ostream, &zero, 1); - } -} - -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) { - 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); - if (len < 8) { - xm_utils_bin2coff_write_padding(ostream, 8 - len); - } - } else { - // long name: store offset in string table - tb_uint32_t zeros = 0; - tb_stream_bwrit(ostream, (tb_byte_t const *)&zeros, 4); - tb_stream_bwrit(ostream, (tb_byte_t const *)strtab_offset, 4); - *strtab_offset += (tb_uint32_t)(len + 1); // +1 for null terminator - } -} - -static tb_bool_t xm_utils_bin2coff_dump(tb_stream_ref_t istream, - tb_stream_ref_t ostream, - tb_char_t const *symbol_prefix, - tb_char_t const *arch, - tb_char_t const *basename, - tb_bool_t zeroend) { - tb_assert_and_check_return_val(istream && ostream, tb_false); - - // get file size - tb_hong_t filesize = tb_stream_size(istream); - if (filesize < 0 || filesize > 0xffffffffU) { - return tb_false; - } - tb_uint32_t datasize = (tb_uint32_t)filesize; - // add null terminator if zeroend is true - if (zeroend) { - if (datasize >= 0xffffffffU) { - return tb_false; // would overflow - } - datasize++; - } - - // generate symbol names from filename - tb_char_t symbol_name[256] = {0}; - tb_char_t symbol_start[256] = {0}; - tb_char_t symbol_end[256] = {0}; - - // use basename or default to "data" - if (!basename || !basename[0]) { - basename = "data"; - } - - // build symbol name - if (symbol_prefix) { - tb_snprintf(symbol_name, sizeof(symbol_name), "%s%s", symbol_prefix, basename); - } else { - tb_snprintf(symbol_name, sizeof(symbol_name), "_binary_%s", basename); - } - - // replace non-alphanumeric with underscore - for (tb_size_t i = 0; symbol_name[i]; i++) { - if (!tb_isalpha(symbol_name[i]) && !tb_isdigit(symbol_name[i]) && symbol_name[i] != '_') { - symbol_name[i] = '_'; - } - } - - tb_snprintf(symbol_start, sizeof(symbol_start), "%s_start", symbol_name); - tb_snprintf(symbol_end, sizeof(symbol_end), "%s_end", symbol_name); - - // calculate offsets - tb_uint32_t header_size = sizeof(xm_coff_header_t); - tb_uint32_t section_header_size = sizeof(xm_coff_section_t); - tb_uint32_t section_data_ofs = header_size + section_header_size; - tb_uint32_t section_data_size = datasize; - tb_uint32_t symbol_table_ofs = section_data_ofs + ((section_data_size + 3) & ~3); // align to 4 bytes - tb_uint32_t string_table_size = 4; // initial 4-byte size field - - // calculate string table size - tb_size_t start_len = tb_strlen(symbol_start); - tb_size_t end_len = tb_strlen(symbol_end); - if (start_len > 8) { - string_table_size += (tb_uint32_t)(start_len + 1); - } - if (end_len > 8) { - string_table_size += (tb_uint32_t)(end_len + 1); - } - - // write COFF header - xm_coff_header_t header; - tb_memset(&header, 0, sizeof(header)); - header.machine = xm_utils_bin2coff_get_machine(arch); - header.nsects = 1; - header.time = 0; - header.symtabofs = symbol_table_ofs; - header.nsyms = 4; // .rdata section symbol (1) + auxiliary entry (1) + 2 data symbols (start, end) - header.opthdr = 0; - header.flags = 0; - if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&header, sizeof(header))) { - return tb_false; - } - - // write section header (.rdata) - xm_coff_section_t section; - tb_memset(§ion, 0, sizeof(section)); - tb_strncpy(section.name, ".rdata", 8); - section.vsize = datasize; - section.vaddr = 0; - section.size = datasize; - section.ofs = section_data_ofs; - section.relocofs = 0; - section.linenoofs = 0; - section.nreloc = 0; - section.nlineno = 0; - section.flags = XM_COFF_SECTION_RDATA; - if (!tb_stream_bwrit(ostream, (tb_byte_t const *)§ion, sizeof(section))) { - return tb_false; - } - - // write section data - if (!xm_utils_stream_copy(istream, ostream, filesize)) { - return tb_false; - } - // append null terminator if zeroend is true - if (zeroend) { - tb_byte_t zero = 0; - if (!tb_stream_bwrit(ostream, &zero, 1)) { - return tb_false; - } - } - - // align to 4 bytes - tb_uint32_t padding = (4 - (section_data_size & 3)) & 3; - if (padding > 0) { - xm_utils_bin2coff_write_padding(ostream, padding); - } - - // write symbol table - // symbol 0: .rdata section symbol - xm_coff_symbol_t sym_section; - tb_memset(&sym_section, 0, sizeof(sym_section)); - tb_strncpy(sym_section.n.shortname.name, ".rdata", 8); - sym_section.value = 0; - sym_section.sect = 1; // section index (1-based) - sym_section.type = 0; // IMAGE_SYM_TYPE_NULL - sym_section.scl = 3; // IMAGE_SYM_CLASS_STATIC - sym_section.naux = 1; // auxiliary entry - if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&sym_section, sizeof(sym_section))) { - return tb_false; - } - // auxiliary entry for section (18 bytes total) - xm_coff_aux_section_t aux_section; - tb_memset(&aux_section, 0, sizeof(aux_section)); - aux_section.length = datasize; - aux_section.nreloc = 0; - aux_section.nlineno = 0; - if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&aux_section, sizeof(aux_section))) { - return tb_false; - } - - // 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_coff_symbol_tail_t sym_start_tail; - tb_memset(&sym_start_tail, 0, sizeof(sym_start_tail)); - sym_start_tail.value = 0; - sym_start_tail.sect = 1; - sym_start_tail.scl = 2; // IMAGE_SYM_CLASS_EXTERNAL - if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&sym_start_tail, sizeof(sym_start_tail))) { - return tb_false; - } - - // symbol 2: _binary_xxx_end - xm_utils_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; - sym_end_tail.sect = 1; - sym_end_tail.scl = 2; // IMAGE_SYM_CLASS_EXTERNAL - if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&sym_end_tail, sizeof(sym_end_tail))) { - return tb_false; - } - - // 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); - 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); - tb_byte_t null = 0; - tb_stream_bwrit(ostream, &null, 1); - } - - return tb_true; -} - -/* ////////////////////////////////////////////////////////////////////////////////////// - * implementation - */ - -/* generate COFF object file from binary file - * - * local ok, errors = utils.bin2coff(binaryfile, outputfile, symbol_prefix, arch, basename) - */ -tb_int_t xm_utils_bin2coff(lua_State *lua) { - tb_assert_and_check_return_val(lua, 0); - - // get the binaryfile - tb_char_t const *binaryfile = luaL_checkstring(lua, 1); - tb_check_return_val(binaryfile, 0); - - // get the outputfile - tb_char_t const *outputfile = luaL_checkstring(lua, 2); - tb_check_return_val(outputfile, 0); - - // get symbol prefix (optional) - tb_char_t const *symbol_prefix = lua_isstring(lua, 3) ? lua_tostring(lua, 3) : tb_null; - - // get arch (optional) - tb_char_t const *arch = lua_isstring(lua, 4) ? lua_tostring(lua, 4) : tb_null; - - // get basename (optional) - tb_char_t const *basename = lua_isstring(lua, 5) ? lua_tostring(lua, 5) : tb_null; - - // get zeroend (optional, default: false) - tb_bool_t zeroend = lua_toboolean(lua, 6); - - // do dump - tb_bool_t ok = tb_false; - tb_stream_ref_t istream = tb_stream_init_from_file(binaryfile, TB_FILE_MODE_RO); - tb_stream_ref_t ostream = tb_stream_init_from_file(outputfile, - TB_FILE_MODE_RW | TB_FILE_MODE_CREAT | TB_FILE_MODE_TRUNC); - do { - if (!tb_stream_open(istream)) { - lua_pushboolean(lua, tb_false); - lua_pushfstring(lua, "bin2coff: open %s failed", binaryfile); - break; - } - - if (!tb_stream_open(ostream)) { - lua_pushboolean(lua, tb_false); - lua_pushfstring(lua, "bin2coff: open %s failed", outputfile); - break; - } - - if (!xm_utils_bin2coff_dump(istream, ostream, symbol_prefix, arch, basename, zeroend)) { - lua_pushboolean(lua, tb_false); - lua_pushfstring(lua, "bin2coff: dump data failed"); - break; - } - - ok = tb_true; - lua_pushboolean(lua, ok); - - } while (0); - - if (istream) { - tb_stream_clos(istream); - } - istream = tb_null; - - if (ostream) { - tb_stream_clos(ostream); - } - ostream = tb_null; - - return ok ? 1 : 2; -} - diff --git a/core/src/xmake/utils/bin2elf.c b/core/src/xmake/utils/bin2elf.c deleted file mode 100644 index 7fe8e5576..000000000 --- a/core/src/xmake/utils/bin2elf.c +++ /dev/null @@ -1,856 +0,0 @@ -/*!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 bin2elf.c - * - */ - -/* ////////////////////////////////////////////////////////////////////////////////////// - * trace - */ -#define TB_TRACE_MODULE_NAME "bin2elf" -#define TB_TRACE_MODULE_DEBUG (0) - -/* ////////////////////////////////////////////////////////////////////////////////////// - * includes - */ -#include "prefix.h" - -/* ////////////////////////////////////////////////////////////////////////////////////// - * macros - */ -#define XM_ELF_MACHINE_X86_64 0x3e -#define XM_ELF_MACHINE_ARM64 0xb7 -#define XM_ELF_MACHINE_ARM 0x28 -#define XM_ELF_MACHINE_I386 0x03 - -#define XM_ELF_SHT_PROGBITS 0x1 -#define XM_ELF_SHT_SYMTAB 0x2 -#define XM_ELF_SHT_STRTAB 0x3 - -#define XM_ELF_SHF_ALLOC 0x2 -#define XM_ELF_SHF_WRITE 0x1 - -#define XM_ELF_STB_GLOBAL 0x1 -#define XM_ELF_STT_OBJECT 0x1 - -/* ////////////////////////////////////////////////////////////////////////////////////// - * types - */ -#include "tbox/prefix/packed.h" -typedef struct __xm_elf32_header_t { - tb_uint8_t e_ident[16]; - tb_uint16_t e_type; - tb_uint16_t e_machine; - tb_uint32_t e_version; - tb_uint32_t e_entry; - tb_uint32_t e_phoff; - tb_uint32_t e_shoff; - tb_uint32_t e_flags; - tb_uint16_t e_ehsize; - tb_uint16_t e_phentsize; - tb_uint16_t e_phnum; - tb_uint16_t e_shentsize; - tb_uint16_t e_shnum; - tb_uint16_t e_shstrndx; -} __tb_packed__ xm_elf32_header_t; - -typedef struct __xm_elf32_section_t { - tb_uint32_t sh_name; - tb_uint32_t sh_type; - tb_uint32_t sh_flags; - tb_uint32_t sh_addr; - tb_uint32_t sh_offset; - tb_uint32_t sh_size; - tb_uint32_t sh_link; - tb_uint32_t sh_info; - tb_uint32_t sh_addralign; - tb_uint32_t sh_entsize; -} __tb_packed__ xm_elf32_section_t; - -typedef struct __xm_elf32_symbol_t { - tb_uint32_t st_name; - tb_uint32_t st_value; - tb_uint32_t st_size; - tb_uint8_t st_info; - tb_uint8_t st_other; - tb_uint16_t st_shndx; -} __tb_packed__ xm_elf32_symbol_t; - -typedef struct __xm_elf64_header_t { - tb_uint8_t e_ident[16]; - tb_uint16_t e_type; - tb_uint16_t e_machine; - tb_uint32_t e_version; - tb_uint64_t e_entry; - tb_uint64_t e_phoff; - tb_uint64_t e_shoff; - tb_uint32_t e_flags; - tb_uint16_t e_ehsize; - tb_uint16_t e_phentsize; - tb_uint16_t e_phnum; - tb_uint16_t e_shentsize; - tb_uint16_t e_shnum; - tb_uint16_t e_shstrndx; -} __tb_packed__ xm_elf64_header_t; - -typedef struct __xm_elf64_section_t { - tb_uint32_t sh_name; - tb_uint32_t sh_type; - tb_uint64_t sh_flags; - tb_uint64_t sh_addr; - tb_uint64_t sh_offset; - tb_uint64_t sh_size; - tb_uint32_t sh_link; - tb_uint32_t sh_info; - tb_uint64_t sh_addralign; - tb_uint64_t sh_entsize; -} __tb_packed__ xm_elf64_section_t; - -typedef struct __xm_elf64_symbol_t { - tb_uint32_t st_name; - tb_uint8_t st_info; - tb_uint8_t st_other; - tb_uint16_t st_shndx; - tb_uint64_t st_value; - tb_uint64_t st_size; -} __tb_packed__ xm_elf64_symbol_t; - -#include "tbox/prefix/packed.h" - -/* ////////////////////////////////////////////////////////////////////////////////////// - * private implementation - */ -static tb_uint16_t xm_utils_bin2elf_get_machine(tb_char_t const *arch) { - if (!arch) { - return XM_ELF_MACHINE_X86_64; - } - if (tb_strcmp(arch, "x86_64") == 0 || tb_strcmp(arch, "x64") == 0) { - return XM_ELF_MACHINE_X86_64; - } else if (tb_strcmp(arch, "arm64") == 0 || tb_strcmp(arch, "aarch64") == 0 || - tb_strcmp(arch, "arm64-v8a") == 0) { - return XM_ELF_MACHINE_ARM64; - } else if (tb_strcmp(arch, "arm") == 0 || tb_strcmp(arch, "armv7") == 0 || - tb_strcmp(arch, "armeabi-v7a") == 0) { - return XM_ELF_MACHINE_ARM; - } else if (tb_strcmp(arch, "i386") == 0 || tb_strcmp(arch, "x86") == 0) { - return XM_ELF_MACHINE_I386; - } - return XM_ELF_MACHINE_X86_64; -} - -static tb_bool_t xm_utils_bin2elf_is_64bit(tb_char_t const *arch) { - if (!arch) { - return tb_true; - } - if (tb_strcmp(arch, "x86_64") == 0 || tb_strcmp(arch, "x64") == 0 || - tb_strcmp(arch, "arm64") == 0 || tb_strcmp(arch, "aarch64") == 0 || - tb_strcmp(arch, "arm64-v8a") == 0) { - return tb_true; - } - return tb_false; -} - -static tb_bool_t xm_utils_bin2elf_dump_32(tb_stream_ref_t istream, - tb_stream_ref_t ostream, - tb_char_t const *symbol_prefix, - tb_char_t const *arch, - tb_char_t const *basename, - tb_bool_t zeroend) { - tb_assert_and_check_return_val(istream && ostream, tb_false); - - // get file size - tb_hong_t filesize = tb_stream_size(istream); - if (filesize < 0 || filesize > 0xffffffffU) { - return tb_false; - } - tb_uint32_t datasize = (tb_uint32_t)filesize; - // add null terminator if zeroend is true - if (zeroend) { - if (datasize >= 0xffffffffU) { - return tb_false; // would overflow - } - datasize++; - } - - // generate symbol names from filename - tb_char_t symbol_name[256] = {0}; - tb_char_t symbol_start[256] = {0}; - tb_char_t symbol_end[256] = {0}; - - // use basename or default to "data" - if (!basename || !basename[0]) { - basename = "data"; - } - - // build symbol name - if (symbol_prefix) { - tb_snprintf(symbol_name, sizeof(symbol_name), "%s%s", symbol_prefix, basename); - } else { - tb_snprintf(symbol_name, sizeof(symbol_name), "_binary_%s", basename); - } - - // replace non-alphanumeric with underscore - for (tb_size_t i = 0; symbol_name[i]; i++) { - if (!tb_isalpha(symbol_name[i]) && !tb_isdigit(symbol_name[i]) && symbol_name[i] != '_') { - symbol_name[i] = '_'; - } - } - - tb_snprintf(symbol_start, sizeof(symbol_start), "%s_start", symbol_name); - tb_snprintf(symbol_end, sizeof(symbol_end), "%s_end", symbol_name); - - // calculate offsets - tb_uint32_t header_size = sizeof(xm_elf32_header_t); - tb_uint32_t section_header_size = sizeof(xm_elf32_section_t); - tb_uint32_t section_count = 6; // NULL, .rodata, .symtab, .strtab, .shstrtab, .note.GNU-stack - tb_uint32_t section_headers_ofs = header_size; - tb_uint32_t rodata_ofs = section_headers_ofs + section_count * section_header_size; - tb_uint32_t rodata_size = datasize; - tb_uint32_t rodata_padding = (4 - (rodata_size & 3)) & 3; // align to 4 bytes for 32-bit - tb_uint32_t symtab_ofs = rodata_ofs + rodata_size + rodata_padding; - tb_uint32_t symtab_size = 3 * sizeof(xm_elf32_symbol_t); // NULL, start, end - tb_uint32_t symtab_padding = (4 - (symtab_size & 3)) & 3; // align to 4 bytes - tb_uint32_t strtab_ofs = symtab_ofs + symtab_size + symtab_padding; - - // calculate string table size - tb_size_t start_len = tb_strlen(symbol_start); - tb_size_t end_len = tb_strlen(symbol_end); - tb_uint32_t strtab_size = 1; // initial null byte - strtab_size += (tb_uint32_t)(start_len + 1); - strtab_size += (tb_uint32_t)(end_len + 1); - tb_uint32_t strtab_padding = (4 - (strtab_size & 3)) & 3; // align to 4 bytes - tb_uint32_t shstrtab_ofs = strtab_ofs + strtab_size + strtab_padding; - - // calculate section header string table size - tb_uint32_t shstrtab_size = 1; // initial null byte - shstrtab_size += 8; // ".rodata\0" (7 + 1) - shstrtab_size += 8; // ".symtab\0" (7 + 1) - shstrtab_size += 8; // ".strtab\0" (7 + 1) - shstrtab_size += 10; // ".shstrtab\0" (9 + 1) - shstrtab_size += 16; // ".note.GNU-stack\0" (15 + 1) - - // write ELF header - xm_elf32_header_t header; - tb_memset(&header, 0, sizeof(header)); - header.e_ident[0] = 0x7f; - header.e_ident[1] = 'E'; - header.e_ident[2] = 'L'; - header.e_ident[3] = 'F'; - header.e_ident[4] = 1; // ELFCLASS32 - header.e_ident[5] = 1; // ELFDATA2LSB - 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_version = 1; - header.e_shoff = section_headers_ofs; - header.e_ehsize = header_size; - header.e_shentsize = section_header_size; - header.e_shnum = section_count; - header.e_shstrndx = 4; // .shstrtab section index - if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&header, sizeof(header))) { - return tb_false; - } - - // write section headers - xm_elf32_section_t section_null; - tb_memset(§ion_null, 0, sizeof(section_null)); - if (!tb_stream_bwrit(ostream, (tb_byte_t const *)§ion_null, sizeof(section_null))) { - return tb_false; - } - - // write .rodata section header - xm_elf32_section_t section_rodata; - tb_memset(§ion_rodata, 0, sizeof(section_rodata)); - section_rodata.sh_name = 1; // ".rodata" in shstrtab - section_rodata.sh_type = XM_ELF_SHT_PROGBITS; - section_rodata.sh_flags = XM_ELF_SHF_ALLOC; - section_rodata.sh_offset = rodata_ofs; - section_rodata.sh_size = rodata_size; - section_rodata.sh_addralign = 4; - if (!tb_stream_bwrit(ostream, (tb_byte_t const *)§ion_rodata, sizeof(section_rodata))) { - return tb_false; - } - - // write .symtab section header - xm_elf32_section_t section_symtab; - tb_memset(§ion_symtab, 0, sizeof(section_symtab)); - section_symtab.sh_name = 9; // ".symtab" in shstrtab - section_symtab.sh_type = XM_ELF_SHT_SYMTAB; - section_symtab.sh_offset = symtab_ofs; - section_symtab.sh_size = symtab_size; - section_symtab.sh_link = 3; // .strtab section index - section_symtab.sh_info = 1; // first global symbol index - section_symtab.sh_addralign = 4; - section_symtab.sh_entsize = sizeof(xm_elf32_symbol_t); - if (!tb_stream_bwrit(ostream, (tb_byte_t const *)§ion_symtab, sizeof(section_symtab))) { - return tb_false; - } - - // write .strtab section header - xm_elf32_section_t section_strtab; - tb_memset(§ion_strtab, 0, sizeof(section_strtab)); - section_strtab.sh_name = 17; // ".strtab" in shstrtab - section_strtab.sh_type = XM_ELF_SHT_STRTAB; - section_strtab.sh_offset = strtab_ofs; - section_strtab.sh_size = strtab_size; - section_strtab.sh_addralign = 1; - if (!tb_stream_bwrit(ostream, (tb_byte_t const *)§ion_strtab, sizeof(section_strtab))) { - return tb_false; - } - - // write .shstrtab section header - xm_elf32_section_t section_shstrtab; - tb_memset(§ion_shstrtab, 0, sizeof(section_shstrtab)); - section_shstrtab.sh_name = 25; // ".shstrtab" in shstrtab - section_shstrtab.sh_type = XM_ELF_SHT_STRTAB; - section_shstrtab.sh_offset = shstrtab_ofs; - section_shstrtab.sh_size = shstrtab_size; - section_shstrtab.sh_addralign = 1; - if (!tb_stream_bwrit(ostream, (tb_byte_t const *)§ion_shstrtab, sizeof(section_shstrtab))) { - return tb_false; - } - - // write .note.GNU-stack section header (empty section to mark stack as non-executable) - xm_elf32_section_t section_note_gnu_stack; - tb_memset(§ion_note_gnu_stack, 0, sizeof(section_note_gnu_stack)); - section_note_gnu_stack.sh_name = 35; // ".note.GNU-stack" in shstrtab (25 + 10) - section_note_gnu_stack.sh_type = XM_ELF_SHT_PROGBITS; - section_note_gnu_stack.sh_flags = 0; // no flags - section_note_gnu_stack.sh_offset = shstrtab_ofs + shstrtab_size; // after .shstrtab - section_note_gnu_stack.sh_size = 0; // empty section - section_note_gnu_stack.sh_addralign = 1; - if (!tb_stream_bwrit(ostream, (tb_byte_t const *)§ion_note_gnu_stack, sizeof(section_note_gnu_stack))) { - return tb_false; - } - - // write .rodata section data - if (!xm_utils_stream_copy(istream, ostream, filesize)) { - return tb_false; - } - // append null terminator if zeroend is true - if (zeroend) { - tb_byte_t zero = 0; - if (!tb_stream_bwrit(ostream, &zero, 1)) { - return tb_false; - } - } - - // align .rodata to 4 bytes - if (rodata_padding > 0) { - tb_byte_t zero = 0; - while (rodata_padding-- > 0) { - if (!tb_stream_bwrit(ostream, &zero, 1)) { - return tb_false; - } - } - } - - // write symbol table - // symbol 0: NULL symbol - xm_elf32_symbol_t sym_null; - tb_memset(&sym_null, 0, sizeof(sym_null)); - if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&sym_null, sizeof(sym_null))) { - return tb_false; - } - - // symbol 1: _binary_xxx_start - xm_elf32_symbol_t sym_start; - tb_memset(&sym_start, 0, sizeof(sym_start)); - sym_start.st_name = 1; // offset in .strtab (after initial null) - sym_start.st_info = (XM_ELF_STB_GLOBAL << 4) | XM_ELF_STT_OBJECT; - sym_start.st_shndx = 1; // .rodata section index - sym_start.st_value = 0; - sym_start.st_size = 0; - if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&sym_start, sizeof(sym_start))) { - return tb_false; - } - - // symbol 2: _binary_xxx_end - xm_elf32_symbol_t sym_end; - tb_memset(&sym_end, 0, sizeof(sym_end)); - sym_end.st_name = 1 + (tb_uint32_t)(start_len + 1); // offset in .strtab - sym_end.st_info = (XM_ELF_STB_GLOBAL << 4) | XM_ELF_STT_OBJECT; - sym_end.st_shndx = 1; // .rodata section index - sym_end.st_value = rodata_size; - sym_end.st_size = 0; - if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&sym_end, sizeof(sym_end))) { - return tb_false; - } - - // align .symtab to 4 bytes - if (symtab_padding > 0) { - tb_byte_t zero = 0; - while (symtab_padding-- > 0) { - if (!tb_stream_bwrit(ostream, &zero, 1)) { - return tb_false; - } - } - } - - // write string table - tb_byte_t null = 0; - if (!tb_stream_bwrit(ostream, &null, 1)) { - return tb_false; - } - if (!tb_stream_bwrit(ostream, (tb_byte_t const *)symbol_start, start_len)) { - return tb_false; - } - if (!tb_stream_bwrit(ostream, &null, 1)) { - return tb_false; - } - if (!tb_stream_bwrit(ostream, (tb_byte_t const *)symbol_end, end_len)) { - return tb_false; - } - if (!tb_stream_bwrit(ostream, &null, 1)) { - return tb_false; - } - - // align .strtab to 4 bytes - if (strtab_padding > 0) { - tb_byte_t zero = 0; - while (strtab_padding-- > 0) { - if (!tb_stream_bwrit(ostream, &zero, 1)) { - return tb_false; - } - } - } - - // write section header string table - if (!tb_stream_bwrit(ostream, &null, 1)) { - return tb_false; - } - if (!tb_stream_bwrit(ostream, (tb_byte_t const *)".rodata", 7)) { - return tb_false; - } - if (!tb_stream_bwrit(ostream, &null, 1)) { - return tb_false; - } - if (!tb_stream_bwrit(ostream, (tb_byte_t const *)".symtab", 7)) { - return tb_false; - } - if (!tb_stream_bwrit(ostream, &null, 1)) { - return tb_false; - } - if (!tb_stream_bwrit(ostream, (tb_byte_t const *)".strtab", 7)) { - return tb_false; - } - if (!tb_stream_bwrit(ostream, &null, 1)) { - return tb_false; - } - if (!tb_stream_bwrit(ostream, (tb_byte_t const *)".shstrtab", 9)) { - return tb_false; - } - if (!tb_stream_bwrit(ostream, &null, 1)) { - return tb_false; - } - if (!tb_stream_bwrit(ostream, (tb_byte_t const *)".note.GNU-stack", 15)) { - return tb_false; - } - if (!tb_stream_bwrit(ostream, &null, 1)) { - return tb_false; - } - - return tb_true; -} - -static tb_bool_t xm_utils_bin2elf_dump_64(tb_stream_ref_t istream, - tb_stream_ref_t ostream, - tb_char_t const *symbol_prefix, - tb_char_t const *arch, - tb_char_t const *basename, - tb_bool_t zeroend) { - tb_assert_and_check_return_val(istream && ostream, tb_false); - - // get file size - tb_hong_t filesize = tb_stream_size(istream); - if (filesize < 0 || filesize > 0xffffffffU) { - return tb_false; - } - tb_uint32_t datasize = (tb_uint32_t)filesize; - // add null terminator if zeroend is true - if (zeroend) { - if (datasize >= 0xffffffffU) { - return tb_false; // would overflow - } - datasize++; - } - - // generate symbol names from filename - tb_char_t symbol_name[256] = {0}; - tb_char_t symbol_start[256] = {0}; - tb_char_t symbol_end[256] = {0}; - - // use basename or default to "data" - if (!basename || !basename[0]) { - basename = "data"; - } - - // build symbol name - if (symbol_prefix) { - tb_snprintf(symbol_name, sizeof(symbol_name), "%s%s", symbol_prefix, basename); - } else { - tb_snprintf(symbol_name, sizeof(symbol_name), "_binary_%s", basename); - } - - // replace non-alphanumeric with underscore - for (tb_size_t i = 0; symbol_name[i]; i++) { - if (!tb_isalpha(symbol_name[i]) && !tb_isdigit(symbol_name[i]) && symbol_name[i] != '_') { - symbol_name[i] = '_'; - } - } - - tb_snprintf(symbol_start, sizeof(symbol_start), "%s_start", symbol_name); - tb_snprintf(symbol_end, sizeof(symbol_end), "%s_end", symbol_name); - - // calculate offsets - tb_uint32_t header_size = sizeof(xm_elf64_header_t); - tb_uint32_t section_header_size = sizeof(xm_elf64_section_t); - tb_uint32_t section_count = 6; // NULL, .rodata, .symtab, .strtab, .shstrtab, .note.GNU-stack - tb_uint32_t section_headers_ofs = header_size; - tb_uint32_t rodata_ofs = section_headers_ofs + section_count * section_header_size; - tb_uint32_t rodata_size = datasize; - tb_uint32_t rodata_padding = (8 - (rodata_size & 7)) & 7; - tb_uint32_t symtab_ofs = rodata_ofs + rodata_size + rodata_padding; - tb_uint32_t symtab_size = 3 * sizeof(xm_elf64_symbol_t); // NULL, start, end - tb_uint32_t symtab_padding = (8 - (symtab_size & 7)) & 7; - tb_uint32_t strtab_ofs = symtab_ofs + symtab_size + symtab_padding; - - // calculate string table size - tb_size_t start_len = tb_strlen(symbol_start); - tb_size_t end_len = tb_strlen(symbol_end); - tb_uint32_t strtab_size = 1; // initial null byte - strtab_size += (tb_uint32_t)(start_len + 1); - strtab_size += (tb_uint32_t)(end_len + 1); - tb_uint32_t strtab_padding = (8 - (strtab_size & 7)) & 7; - - // calculate section header string table size - tb_uint32_t shstrtab_size = 1; // initial null byte - shstrtab_size += 8; // ".rodata\0" (7 + 1) - shstrtab_size += 8; // ".symtab\0" (7 + 1) - shstrtab_size += 8; // ".strtab\0" (7 + 1) - shstrtab_size += 10; // ".shstrtab\0" (9 + 1) - shstrtab_size += 16; // ".note.GNU-stack\0" (15 + 1) - tb_uint32_t shstrtab_ofs = strtab_ofs + strtab_size + strtab_padding; - - // write ELF header - xm_elf64_header_t header; - tb_memset(&header, 0, sizeof(header)); - header.e_ident[0] = 0x7f; - header.e_ident[1] = 'E'; - header.e_ident[2] = 'L'; - header.e_ident[3] = 'F'; - header.e_ident[4] = 2; // ELFCLASS64 - header.e_ident[5] = 1; // ELFDATA2LSB - 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_version = 1; - header.e_shoff = section_headers_ofs; - header.e_ehsize = header_size; - header.e_shentsize = section_header_size; - header.e_shnum = section_count; - header.e_shstrndx = 4; // .shstrtab section index - if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&header, sizeof(header))) { - return tb_false; - } - - // write section headers - xm_elf64_section_t section_null; - tb_memset(§ion_null, 0, sizeof(section_null)); - if (!tb_stream_bwrit(ostream, (tb_byte_t const *)§ion_null, sizeof(section_null))) { - return tb_false; - } - - // write .rodata section header - xm_elf64_section_t section_rodata; - tb_memset(§ion_rodata, 0, sizeof(section_rodata)); - section_rodata.sh_name = 1; // ".rodata" in shstrtab - section_rodata.sh_type = XM_ELF_SHT_PROGBITS; - section_rodata.sh_flags = XM_ELF_SHF_ALLOC; - section_rodata.sh_offset = rodata_ofs; - section_rodata.sh_size = rodata_size; - section_rodata.sh_addralign = 8; - if (!tb_stream_bwrit(ostream, (tb_byte_t const *)§ion_rodata, sizeof(section_rodata))) { - return tb_false; - } - - // write .symtab section header - xm_elf64_section_t section_symtab; - tb_memset(§ion_symtab, 0, sizeof(section_symtab)); - section_symtab.sh_name = 9; // ".symtab" in shstrtab - section_symtab.sh_type = XM_ELF_SHT_SYMTAB; - section_symtab.sh_offset = symtab_ofs; - section_symtab.sh_size = symtab_size; - section_symtab.sh_link = 3; // .strtab section index - section_symtab.sh_info = 1; // first global symbol index - section_symtab.sh_addralign = 8; - section_symtab.sh_entsize = sizeof(xm_elf64_symbol_t); - if (!tb_stream_bwrit(ostream, (tb_byte_t const *)§ion_symtab, sizeof(section_symtab))) { - return tb_false; - } - - // write .strtab section header - xm_elf64_section_t section_strtab; - tb_memset(§ion_strtab, 0, sizeof(section_strtab)); - section_strtab.sh_name = 17; // ".strtab" in shstrtab - section_strtab.sh_type = XM_ELF_SHT_STRTAB; - section_strtab.sh_offset = strtab_ofs; - section_strtab.sh_size = strtab_size; - section_strtab.sh_addralign = 1; - if (!tb_stream_bwrit(ostream, (tb_byte_t const *)§ion_strtab, sizeof(section_strtab))) { - return tb_false; - } - - // write .shstrtab section header - xm_elf64_section_t section_shstrtab; - tb_memset(§ion_shstrtab, 0, sizeof(section_shstrtab)); - section_shstrtab.sh_name = 25; // ".shstrtab" in shstrtab - section_shstrtab.sh_type = XM_ELF_SHT_STRTAB; - section_shstrtab.sh_offset = shstrtab_ofs; // points to initial null byte - section_shstrtab.sh_size = shstrtab_size; // size includes initial null and all strings - section_shstrtab.sh_addralign = 1; - if (!tb_stream_bwrit(ostream, (tb_byte_t const *)§ion_shstrtab, sizeof(section_shstrtab))) { - return tb_false; - } - - // write .note.GNU-stack section header (empty section to mark stack as non-executable) - xm_elf64_section_t section_note_gnu_stack; - tb_memset(§ion_note_gnu_stack, 0, sizeof(section_note_gnu_stack)); - section_note_gnu_stack.sh_name = 35; // ".note.GNU-stack" in shstrtab (25 + 10) - section_note_gnu_stack.sh_type = XM_ELF_SHT_PROGBITS; - section_note_gnu_stack.sh_flags = 0; // no flags - section_note_gnu_stack.sh_offset = shstrtab_ofs + shstrtab_size; // after .shstrtab - section_note_gnu_stack.sh_size = 0; // empty section - section_note_gnu_stack.sh_addralign = 1; - if (!tb_stream_bwrit(ostream, (tb_byte_t const *)§ion_note_gnu_stack, sizeof(section_note_gnu_stack))) { - return tb_false; - } - - // write .rodata section data - if (!xm_utils_stream_copy(istream, ostream, filesize)) { - return tb_false; - } - // append null terminator if zeroend is true - if (zeroend) { - tb_byte_t zero = 0; - if (!tb_stream_bwrit(ostream, &zero, 1)) { - return tb_false; - } - } - - // align .rodata to 8 bytes - if (rodata_padding > 0) { - tb_byte_t zero = 0; - while (rodata_padding-- > 0) { - if (!tb_stream_bwrit(ostream, &zero, 1)) { - return tb_false; - } - } - } - - // write symbol table - // symbol 0: NULL symbol - xm_elf64_symbol_t sym_null; - tb_memset(&sym_null, 0, sizeof(sym_null)); - if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&sym_null, sizeof(sym_null))) { - return tb_false; - } - - // symbol 1: _binary_xxx_start - xm_elf64_symbol_t sym_start; - tb_memset(&sym_start, 0, sizeof(sym_start)); - sym_start.st_name = 1; // offset in .strtab (after initial null) - sym_start.st_info = (XM_ELF_STB_GLOBAL << 4) | XM_ELF_STT_OBJECT; - sym_start.st_shndx = 1; // .rodata section index - sym_start.st_value = 0; - sym_start.st_size = 0; - if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&sym_start, sizeof(sym_start))) { - return tb_false; - } - - // symbol 2: _binary_xxx_end - xm_elf64_symbol_t sym_end; - tb_memset(&sym_end, 0, sizeof(sym_end)); - sym_end.st_name = 1 + (tb_uint32_t)(start_len + 1); // offset in .strtab - sym_end.st_info = (XM_ELF_STB_GLOBAL << 4) | XM_ELF_STT_OBJECT; - sym_end.st_shndx = 1; // .rodata section index - sym_end.st_value = rodata_size; - sym_end.st_size = 0; - if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&sym_end, sizeof(sym_end))) { - return tb_false; - } - - // align .symtab to 8 bytes - if (symtab_padding > 0) { - tb_byte_t zero = 0; - while (symtab_padding-- > 0) { - if (!tb_stream_bwrit(ostream, &zero, 1)) { - return tb_false; - } - } - } - - // write string table - tb_byte_t null = 0; - if (!tb_stream_bwrit(ostream, &null, 1)) { - return tb_false; - } - if (!tb_stream_bwrit(ostream, (tb_byte_t const *)symbol_start, start_len)) { - return tb_false; - } - if (!tb_stream_bwrit(ostream, &null, 1)) { - return tb_false; - } - if (!tb_stream_bwrit(ostream, (tb_byte_t const *)symbol_end, end_len)) { - return tb_false; - } - if (!tb_stream_bwrit(ostream, &null, 1)) { - return tb_false; - } - - // align .strtab to 8 bytes - if (strtab_padding > 0) { - tb_byte_t zero = 0; - while (strtab_padding-- > 0) { - if (!tb_stream_bwrit(ostream, &zero, 1)) { - return tb_false; - } - } - } - - // write section header string table - if (!tb_stream_bwrit(ostream, &null, 1)) { - return tb_false; - } - if (!tb_stream_bwrit(ostream, (tb_byte_t const *)".rodata", 7)) { - return tb_false; - } - if (!tb_stream_bwrit(ostream, &null, 1)) { - return tb_false; - } - if (!tb_stream_bwrit(ostream, (tb_byte_t const *)".symtab", 7)) { - return tb_false; - } - if (!tb_stream_bwrit(ostream, &null, 1)) { - return tb_false; - } - if (!tb_stream_bwrit(ostream, (tb_byte_t const *)".strtab", 7)) { - return tb_false; - } - if (!tb_stream_bwrit(ostream, &null, 1)) { - return tb_false; - } - if (!tb_stream_bwrit(ostream, (tb_byte_t const *)".shstrtab", 9)) { - return tb_false; - } - if (!tb_stream_bwrit(ostream, &null, 1)) { - return tb_false; - } - if (!tb_stream_bwrit(ostream, (tb_byte_t const *)".note.GNU-stack", 15)) { - return tb_false; - } - if (!tb_stream_bwrit(ostream, &null, 1)) { - return tb_false; - } - - return tb_true; -} - -/* ////////////////////////////////////////////////////////////////////////////////////// - * implementation - */ - -/* generate ELF object file from binary file - * - * local ok, errors = utils.bin2elf(binaryfile, outputfile, symbol_prefix, arch, basename, zeroend) - */ -tb_int_t xm_utils_bin2elf(lua_State *lua) { - tb_assert_and_check_return_val(lua, 0); - - // get the binaryfile - tb_char_t const *binaryfile = luaL_checkstring(lua, 1); - tb_check_return_val(binaryfile, 0); - - // get the outputfile - tb_char_t const *outputfile = luaL_checkstring(lua, 2); - tb_check_return_val(outputfile, 0); - - // get symbol prefix (optional) - tb_char_t const *symbol_prefix = lua_isstring(lua, 3) ? lua_tostring(lua, 3) : tb_null; - - // get arch (optional) - tb_char_t const *arch = lua_isstring(lua, 4) ? lua_tostring(lua, 4) : tb_null; - - // get basename (optional) - tb_char_t const *basename = lua_isstring(lua, 5) ? lua_tostring(lua, 5) : tb_null; - - // get zeroend (optional, default: false) - tb_bool_t zeroend = lua_toboolean(lua, 6); - - // do dump - tb_bool_t ok = tb_false; - tb_stream_ref_t istream = tb_stream_init_from_file(binaryfile, TB_FILE_MODE_RO); - tb_stream_ref_t ostream = tb_stream_init_from_file(outputfile, - TB_FILE_MODE_RW | TB_FILE_MODE_CREAT | TB_FILE_MODE_TRUNC); - do { - if (!tb_stream_open(istream)) { - lua_pushboolean(lua, tb_false); - lua_pushfstring(lua, "bin2elf: open %s failed", binaryfile); - break; - } - - if (!tb_stream_open(ostream)) { - lua_pushboolean(lua, tb_false); - lua_pushfstring(lua, "bin2elf: open %s failed", outputfile); - break; - } - - // choose 32-bit or 64-bit ELF based on architecture - tb_bool_t is_64bit = xm_utils_bin2elf_is_64bit(arch); - if (is_64bit) { - if (!xm_utils_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)) { - lua_pushboolean(lua, tb_false); - lua_pushfstring(lua, "bin2elf: dump data failed"); - break; - } - } - - ok = tb_true; - lua_pushboolean(lua, ok); - - } while (0); - - if (istream) - tb_stream_clos(istream); - istream = tb_null; - - if (ostream) - tb_stream_clos(ostream); - ostream = tb_null; - - return ok ? 1 : 2; -} - diff --git a/core/src/xmake/utils/bin2macho.c b/core/src/xmake/utils/bin2macho.c deleted file mode 100644 index 04733e1b2..000000000 --- a/core/src/xmake/utils/bin2macho.c +++ /dev/null @@ -1,887 +0,0 @@ -/*!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 bin2macho.c - * - */ - -/* ////////////////////////////////////////////////////////////////////////////////////// - * trace - */ -#define TB_TRACE_MODULE_NAME "bin2macho" -#define TB_TRACE_MODULE_DEBUG (0) - -/* ////////////////////////////////////////////////////////////////////////////////////// - * includes - */ -#include "prefix.h" - -/* ////////////////////////////////////////////////////////////////////////////////////// - * macros - */ -#define XM_MACHO_MAGIC_32 0xfeedface -#define XM_MACHO_MAGIC_64 0xfeedfacf -#define XM_MACHO_MAGIC_FAT 0xcafebabe - -#define XM_MACHO_CPU_TYPE_X86 7 -#define XM_MACHO_CPU_TYPE_X86_64 0x01000007 -#define XM_MACHO_CPU_TYPE_ARM 12 -#define XM_MACHO_CPU_TYPE_ARM64 0x0100000c - -#define XM_MACHO_CPU_SUBTYPE_X86 3 -#define XM_MACHO_CPU_SUBTYPE_X86_64 3 -#define XM_MACHO_CPU_SUBTYPE_ARM 9 -#define XM_MACHO_CPU_SUBTYPE_ARM64 0 - -#define XM_MACHO_FILE_TYPE_OBJECT 1 - -#define XM_MACHO_LC_SEGMENT 0x1 -#define XM_MACHO_LC_SEGMENT_64 0x19 -#define XM_MACHO_LC_SYMTAB 0x2 -#define XM_MACHO_LC_BUILD_VERSION 0x32 - -#define XM_MACHO_PLATFORM_MACOS 1 -#define XM_MACHO_PLATFORM_IOS 2 -#define XM_MACHO_PLATFORM_TVOS 3 -#define XM_MACHO_PLATFORM_WATCHOS 4 - -#define XM_MACHO_SECT_TYPE_REGULAR 0x0 -#define XM_MACHO_SECT_ATTR_SOME_INITS 0x400 -#define XM_MACHO_SECT_ATTR_PURE_INSTRUCTIONS 0x80000000 - -#define XM_MACHO_N_TYPE_MASK 0x0e -#define XM_MACHO_N_TYPE_SECT 0x0e -#define XM_MACHO_N_EXT 0x01 - -#define XM_MACHO_VM_PROT_READ 1 -#define XM_MACHO_VM_PROT_WRITE 2 -#define XM_MACHO_VM_PROT_EXECUTE 4 - -/* ////////////////////////////////////////////////////////////////////////////////////// - * types - */ -#include "tbox/prefix/packed.h" -typedef struct __xm_macho_header_t { - tb_uint32_t magic; - tb_uint32_t cputype; - tb_uint32_t cpusubtype; - tb_uint32_t filetype; - tb_uint32_t ncmds; - tb_uint32_t sizeofcmds; - tb_uint32_t flags; -} __tb_packed__ xm_macho_header_t; - -typedef struct __xm_macho_header_64_t { - tb_uint32_t magic; - tb_uint32_t cputype; - tb_uint32_t cpusubtype; - tb_uint32_t filetype; - tb_uint32_t ncmds; - tb_uint32_t sizeofcmds; - tb_uint32_t flags; - tb_uint32_t reserved; -} __tb_packed__ xm_macho_header_64_t; - -typedef struct __xm_macho_segment_command_t { - tb_uint32_t cmd; - tb_uint32_t cmdsize; - tb_char_t segname[16]; - tb_uint32_t vmaddr; - tb_uint32_t vmsize; - tb_uint32_t fileoff; - tb_uint32_t filesize; - tb_uint32_t maxprot; - tb_uint32_t initprot; - tb_uint32_t nsects; - tb_uint32_t flags; -} __tb_packed__ xm_macho_segment_command_t; - -typedef struct __xm_macho_segment_command_64_t { - tb_uint32_t cmd; - tb_uint32_t cmdsize; - tb_char_t segname[16]; - tb_uint64_t vmaddr; - tb_uint64_t vmsize; - tb_uint64_t fileoff; - tb_uint64_t filesize; - tb_uint32_t maxprot; - tb_uint32_t initprot; - tb_uint32_t nsects; - tb_uint32_t flags; -} __tb_packed__ xm_macho_segment_command_64_t; - -typedef struct __xm_macho_section_t { - tb_char_t sectname[16]; - tb_char_t segname[16]; - tb_uint32_t addr; - tb_uint32_t size; - tb_uint32_t offset; - tb_uint32_t align; - tb_uint32_t reloff; - tb_uint32_t nreloc; - tb_uint32_t flags; - tb_uint32_t reserved1; - tb_uint32_t reserved2; -} __tb_packed__ xm_macho_section_t; - -typedef struct __xm_macho_section_64_t { - tb_char_t sectname[16]; - tb_char_t segname[16]; - tb_uint64_t addr; - tb_uint64_t size; - tb_uint32_t offset; - tb_uint32_t align; - tb_uint32_t reloff; - tb_uint32_t nreloc; - tb_uint32_t flags; - tb_uint32_t reserved1; - tb_uint32_t reserved2; - tb_uint32_t reserved3; -} __tb_packed__ xm_macho_section_64_t; - -typedef struct __xm_macho_symtab_command_t { - tb_uint32_t cmd; - tb_uint32_t cmdsize; - tb_uint32_t symoff; - tb_uint32_t nsyms; - tb_uint32_t stroff; - tb_uint32_t strsize; -} __tb_packed__ xm_macho_symtab_command_t; - -typedef struct __xm_macho_build_version_command_t { - tb_uint32_t cmd; - tb_uint32_t cmdsize; - tb_uint32_t platform; - tb_uint32_t minos; - tb_uint32_t sdk; - tb_uint32_t ntools; -} __tb_packed__ xm_macho_build_version_command_t; - -typedef struct __xm_macho_nlist_t { - tb_uint32_t strx; - tb_uint8_t type; - tb_uint8_t sect; - tb_int16_t desc; - tb_uint32_t value; -} __tb_packed__ xm_macho_nlist_t; - -typedef struct __xm_macho_nlist_64_t { - tb_uint32_t strx; - tb_uint8_t type; - tb_uint8_t sect; - tb_uint16_t desc; - tb_uint64_t value; -} __tb_packed__ xm_macho_nlist_64_t; -#include "tbox/prefix/packed.h" - -/* ////////////////////////////////////////////////////////////////////////////////////// - * private implementation - */ -static tb_uint32_t xm_utils_bin2macho_get_cputype(tb_char_t const *arch) { - if (!arch) { - return XM_MACHO_CPU_TYPE_X86_64; - } - if (tb_strcmp(arch, "x86_64") == 0 || tb_strcmp(arch, "x64") == 0) { - return XM_MACHO_CPU_TYPE_X86_64; - } else if (tb_strcmp(arch, "arm64") == 0 || tb_strcmp(arch, "aarch64") == 0) { - return XM_MACHO_CPU_TYPE_ARM64; - } else if (tb_strcmp(arch, "arm") == 0) { - return XM_MACHO_CPU_TYPE_ARM; - } else if (tb_strcmp(arch, "x86") == 0 || tb_strcmp(arch, "i386") == 0) { - return XM_MACHO_CPU_TYPE_X86; - } - return XM_MACHO_CPU_TYPE_X86_64; -} - -static tb_uint32_t xm_utils_bin2macho_get_cpusubtype(tb_char_t const *arch) { - if (!arch) { - return XM_MACHO_CPU_SUBTYPE_X86_64; - } - if (tb_strcmp(arch, "x86_64") == 0 || tb_strcmp(arch, "x64") == 0) { - return XM_MACHO_CPU_SUBTYPE_X86_64; - } else if (tb_strcmp(arch, "arm64") == 0 || tb_strcmp(arch, "aarch64") == 0) { - return XM_MACHO_CPU_SUBTYPE_ARM64; - } else if (tb_strcmp(arch, "arm") == 0) { - return XM_MACHO_CPU_SUBTYPE_ARM; - } else if (tb_strcmp(arch, "x86") == 0 || tb_strcmp(arch, "i386") == 0) { - return XM_MACHO_CPU_SUBTYPE_X86; - } - return XM_MACHO_CPU_SUBTYPE_X86_64; -} - -static tb_bool_t xm_utils_bin2macho_is_64bit(tb_char_t const *arch) { - if (!arch) { - return tb_true; - } - if (tb_strcmp(arch, "x86_64") == 0 || tb_strcmp(arch, "x64") == 0) { - return tb_true; - } else if (tb_strcmp(arch, "arm64") == 0 || tb_strcmp(arch, "aarch64") == 0) { - return tb_true; - } else if (tb_strcmp(arch, "arm") == 0) { - return tb_false; - } else if (tb_strcmp(arch, "x86") == 0 || tb_strcmp(arch, "i386") == 0) { - return tb_false; - } - return tb_true; -} - -static tb_uint32_t xm_utils_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) { - if (!platform) { - return XM_MACHO_PLATFORM_MACOS; - } - if (tb_strcmp(platform, "macosx") == 0 || tb_strcmp(platform, "macos") == 0) { - return XM_MACHO_PLATFORM_MACOS; - } else if (tb_strcmp(platform, "iphoneos") == 0 || tb_strcmp(platform, "ios") == 0) { - return XM_MACHO_PLATFORM_IOS; - } else if (tb_strcmp(platform, "appletvos") == 0 || tb_strcmp(platform, "tvos") == 0) { - return XM_MACHO_PLATFORM_TVOS; - } else if (tb_strcmp(platform, "watchos") == 0) { - return XM_MACHO_PLATFORM_WATCHOS; - } - return XM_MACHO_PLATFORM_MACOS; -} - -// 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) { - if (!version_str || !version_str[0]) { - return 0x000a0000; // default: 10.0.0 - } - tb_uint32_t major = 0; - tb_uint32_t minor = 0; - tb_uint32_t patch = 0; - tb_char_t const *p = version_str; - // parse major - while (*p && tb_isdigit(*p)) { - major = major * 10 + (*p - '0'); - p++; - } - if (*p == '.') { - p++; - // parse minor - while (*p && tb_isdigit(*p)) { - minor = minor * 10 + (*p - '0'); - p++; - } - if (*p == '.') { - p++; - // parse patch - while (*p && tb_isdigit(*p)) { - patch = patch * 10 + (*p - '0'); - p++; - } - } - } - return (major << 16) | (minor << 8) | patch; -} - -static tb_bool_t xm_utils_bin2macho_dump_64(tb_stream_ref_t istream, - tb_stream_ref_t ostream, - tb_char_t const *symbol_prefix, - tb_char_t const *plat, - tb_char_t const *arch, - tb_char_t const *basename, - tb_uint32_t minos, - tb_uint32_t sdk, - tb_bool_t zeroend) { - tb_assert_and_check_return_val(istream && ostream, tb_false); - - // get file size - tb_hong_t filesize = tb_stream_size(istream); - if (filesize < 0 || filesize > 0xffffffffU) { - return tb_false; - } - tb_uint32_t datasize = (tb_uint32_t)filesize; - // add null terminator if zeroend is true - if (zeroend) { - if (datasize >= 0xffffffffU) { - return tb_false; // would overflow - } - datasize++; - } - - // generate symbol names from filename - tb_char_t symbol_name[256] = {0}; - tb_char_t symbol_start[256] = {0}; - tb_char_t symbol_end[256] = {0}; - - // use basename or default to "data" - if (!basename || !basename[0]) { - basename = "data"; - } - - // build symbol name - // On macOS, C compiler adds an underscore prefix, so we generate symbols with two underscores - // (C code declares _binary_xxx, compiler generates __binary_xxx in object file, so we define __binary_xxx) - if (symbol_prefix) { - tb_snprintf(symbol_name, sizeof(symbol_name), "_%s%s", symbol_prefix, basename); - } else { - tb_snprintf(symbol_name, sizeof(symbol_name), "__binary_%s", basename); - } - - // replace non-alphanumeric with underscore - for (tb_size_t i = 0; symbol_name[i]; i++) { - if (!tb_isalpha(symbol_name[i]) && !tb_isdigit(symbol_name[i]) && symbol_name[i] != '_') { - symbol_name[i] = '_'; - } - } - - tb_snprintf(symbol_start, sizeof(symbol_start), "%s_start", symbol_name); - tb_snprintf(symbol_end, sizeof(symbol_end), "%s_end", symbol_name); - - // calculate offsets - tb_uint32_t header_size = sizeof(xm_macho_header_64_t); - tb_uint32_t segment_cmd_size = sizeof(xm_macho_segment_command_64_t); - tb_uint32_t section_size = sizeof(xm_macho_section_64_t); - 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_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 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; - tb_uint32_t strtab_size = 4; // initial 4-byte size field - tb_size_t start_len = tb_strlen(symbol_start); - 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); - - // 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.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; - header.flags = 0; - if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&header, sizeof(header))) { - return tb_false; - } - - // write segment command - xm_macho_segment_command_64_t segment; - tb_memset(&segment, 0, sizeof(segment)); - segment.cmd = XM_MACHO_LC_SEGMENT_64; - segment.cmdsize = segment_cmd_total_size; - tb_strncpy(segment.segname, "__TEXT", 16); - segment.vmaddr = 0; - segment.vmsize = data_size; - segment.fileoff = data_offset; - segment.filesize = data_size; - segment.maxprot = XM_MACHO_VM_PROT_READ; // VM_PROT_READ (read-only) - segment.initprot = XM_MACHO_VM_PROT_READ; // VM_PROT_READ (read-only) - segment.nsects = 1; - segment.flags = 0; - if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&segment, sizeof(segment))) { - return tb_false; - } - - // write section - xm_macho_section_64_t section; - tb_memset(§ion, 0, sizeof(section)); - tb_strncpy(section.sectname, "__const", 16); - tb_strncpy(section.segname, "__TEXT", 16); - section.addr = 0; - section.size = data_size; - section.offset = data_offset; - section.align = 3; // 2^3 = 8 bytes - section.reloff = 0; - section.nreloc = 0; - section.flags = XM_MACHO_SECT_TYPE_REGULAR | XM_MACHO_SECT_ATTR_SOME_INITS; - if (!tb_stream_bwrit(ostream, (tb_byte_t const *)§ion, sizeof(section))) { - return tb_false; - } - - // write symtab command - xm_macho_symtab_command_t symtab; - tb_memset(&symtab, 0, sizeof(symtab)); - symtab.cmd = XM_MACHO_LC_SYMTAB; - symtab.cmdsize = symtab_cmd_size; - symtab.symoff = symtab_offset; - symtab.nsyms = nlist_count; - symtab.stroff = strtab_offset; - symtab.strsize = strtab_size; - if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&symtab, sizeof(symtab))) { - return tb_false; - } - - // write build version command - xm_macho_build_version_command_t build_version; - 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.minos = minos; - build_version.sdk = sdk; - build_version.ntools = 0; - if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&build_version, sizeof(build_version))) { - return tb_false; - } - - // align to 8 bytes - tb_uint32_t padding = data_offset - (header_size + segment_cmd_total_size + symtab_cmd_size + build_version_cmd_size); - if (padding > 0) { - tb_byte_t zero = 0; - while (padding-- > 0) { - if (!tb_stream_bwrit(ostream, &zero, 1)) { - return tb_false; - } - } - } - - // write section data - if (!xm_utils_stream_copy(istream, ostream, filesize)) { - return tb_false; - } - // append null terminator if zeroend is true - if (zeroend) { - tb_byte_t zero = 0; - if (!tb_stream_bwrit(ostream, &zero, 1)) { - return tb_false; - } - } - - // align to 8 bytes - padding = symtab_offset - data_end_offset; - if (padding > 0) { - tb_byte_t zero = 0; - while (padding-- > 0) { - if (!tb_stream_bwrit(ostream, &zero, 1)) { - return tb_false; - } - } - } - - // write symbol table - // strx starts from 4 (after 4-byte size field) - tb_uint32_t strx = 4; - // symbol 0: _binary_xxx_start - xm_macho_nlist_64_t nlist_start; - tb_memset(&nlist_start, 0, sizeof(nlist_start)); - nlist_start.strx = strx; - nlist_start.type = XM_MACHO_N_TYPE_SECT | XM_MACHO_N_EXT; // N_SECT | N_EXT - nlist_start.sect = 1; - nlist_start.desc = 0; - nlist_start.value = 0; - if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&nlist_start, sizeof(nlist_start))) { - return tb_false; - } - strx += (tb_uint32_t)(start_len + 1); - - // symbol 1: _binary_xxx_end - xm_macho_nlist_64_t nlist_end; - tb_memset(&nlist_end, 0, sizeof(nlist_end)); - nlist_end.strx = strx; - nlist_end.type = XM_MACHO_N_TYPE_SECT | XM_MACHO_N_EXT; // N_SECT | N_EXT - nlist_end.sect = 1; - nlist_end.desc = 0; - nlist_end.value = data_size; - if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&nlist_end, sizeof(nlist_end))) { - return tb_false; - } - strx += (tb_uint32_t)(end_len + 1); - - // align to 8 bytes - padding = strtab_offset - (symtab_offset + nlist_size * nlist_count); - if (padding > 0) { - tb_byte_t zero = 0; - while (padding-- > 0) { - if (!tb_stream_bwrit(ostream, &zero, 1)) { - return tb_false; - } - } - } - - // write string table - tb_stream_bwrit(ostream, (tb_byte_t const *)&strtab_size, 4); - tb_stream_bwrit(ostream, (tb_byte_t const *)symbol_start, start_len); - tb_byte_t null = 0; - tb_stream_bwrit(ostream, &null, 1); - tb_stream_bwrit(ostream, (tb_byte_t const *)symbol_end, end_len); - tb_stream_bwrit(ostream, &null, 1); - - // align string table to 8 bytes - padding = strtab_size - (4 + (tb_uint32_t)start_len + 1 + (tb_uint32_t)end_len + 1); - if (padding > 0) { - tb_byte_t zero = 0; - while (padding-- > 0) { - if (!tb_stream_bwrit(ostream, &zero, 1)) { - return tb_false; - } - } - } - - return tb_true; -} - -static tb_bool_t xm_utils_bin2macho_dump_32(tb_stream_ref_t istream, - tb_stream_ref_t ostream, - tb_char_t const *symbol_prefix, - tb_char_t const *plat, - tb_char_t const *arch, - tb_char_t const *basename, - tb_uint32_t minos, - tb_uint32_t sdk, - tb_bool_t zeroend) { - tb_assert_and_check_return_val(istream && ostream, tb_false); - - // get file size - tb_hong_t filesize = tb_stream_size(istream); - if (filesize < 0 || filesize > 0xffffffffU) { - return tb_false; - } - tb_uint32_t datasize = (tb_uint32_t)filesize; - // add null terminator if zeroend is true - if (zeroend) { - if (datasize >= 0xffffffffU) { - return tb_false; // would overflow - } - datasize++; - } - - // generate symbol names from filename - tb_char_t symbol_name[256] = {0}; - tb_char_t symbol_start[256] = {0}; - tb_char_t symbol_end[256] = {0}; - - // use basename or default to "data" - if (!basename || !basename[0]) { - basename = "data"; - } - - // build symbol name - // On macOS, C compiler adds an underscore prefix, so we generate symbols with two underscores - // (C code declares _binary_xxx, compiler generates __binary_xxx in object file, so we define __binary_xxx) - if (symbol_prefix) { - tb_snprintf(symbol_name, sizeof(symbol_name), "_%s%s", symbol_prefix, basename); - } else { - tb_snprintf(symbol_name, sizeof(symbol_name), "__binary_%s", basename); - } - - // replace non-alphanumeric with underscore - for (tb_size_t i = 0; symbol_name[i]; i++) { - if (!tb_isalpha(symbol_name[i]) && !tb_isdigit(symbol_name[i]) && symbol_name[i] != '_') { - symbol_name[i] = '_'; - } - } - - tb_snprintf(symbol_start, sizeof(symbol_start), "%s_start", symbol_name); - tb_snprintf(symbol_end, sizeof(symbol_end), "%s_end", symbol_name); - - // calculate offsets - tb_uint32_t header_size = sizeof(xm_macho_header_t); - tb_uint32_t segment_cmd_size = sizeof(xm_macho_segment_command_t); - tb_uint32_t section_size = sizeof(xm_macho_section_t); - 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_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 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; - tb_uint32_t strtab_size = 4; // initial 4-byte size field - tb_size_t start_len = tb_strlen(symbol_start); - 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); - - // 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.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; - header.flags = 0; - if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&header, sizeof(header))) { - return tb_false; - } - - // write segment command - xm_macho_segment_command_t segment; - tb_memset(&segment, 0, sizeof(segment)); - segment.cmd = XM_MACHO_LC_SEGMENT; - segment.cmdsize = segment_cmd_total_size; - tb_strncpy(segment.segname, "__TEXT", 16); - segment.vmaddr = 0; - segment.vmsize = data_size; - segment.fileoff = data_offset; - segment.filesize = data_size; - segment.maxprot = XM_MACHO_VM_PROT_READ; // VM_PROT_READ (read-only) - segment.initprot = XM_MACHO_VM_PROT_READ; // VM_PROT_READ (read-only) - segment.nsects = 1; - segment.flags = 0; - if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&segment, sizeof(segment))) { - return tb_false; - } - - // write section - xm_macho_section_t section; - tb_memset(§ion, 0, sizeof(section)); - tb_strncpy(section.sectname, "__const", 16); - tb_strncpy(section.segname, "__TEXT", 16); - section.addr = 0; - section.size = data_size; - section.offset = data_offset; - section.align = 2; // 2^2 = 4 bytes - section.reloff = 0; - section.nreloc = 0; - section.flags = XM_MACHO_SECT_TYPE_REGULAR | XM_MACHO_SECT_ATTR_SOME_INITS; - section.reserved1 = 0; - section.reserved2 = 0; - if (!tb_stream_bwrit(ostream, (tb_byte_t const *)§ion, sizeof(section))) { - return tb_false; - } - - // write symtab command - xm_macho_symtab_command_t symtab; - tb_memset(&symtab, 0, sizeof(symtab)); - symtab.cmd = XM_MACHO_LC_SYMTAB; - symtab.cmdsize = symtab_cmd_size; - symtab.symoff = symtab_offset; - symtab.nsyms = nlist_count; - symtab.stroff = strtab_offset; - symtab.strsize = strtab_size; - if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&symtab, sizeof(symtab))) { - return tb_false; - } - - // write build version command - xm_macho_build_version_command_t build_version; - 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.minos = minos; - build_version.sdk = sdk; - build_version.ntools = 0; - if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&build_version, sizeof(build_version))) { - return tb_false; - } - - // align to 4 bytes - tb_uint32_t padding = data_offset - (header_size + segment_cmd_total_size + symtab_cmd_size + build_version_cmd_size); - if (padding > 0) { - tb_byte_t zero = 0; - while (padding-- > 0) { - if (!tb_stream_bwrit(ostream, &zero, 1)) { - return tb_false; - } - } - } - - // write section data - if (!xm_utils_stream_copy(istream, ostream, filesize)) { - return tb_false; - } - // append null terminator if zeroend is true - if (zeroend) { - tb_byte_t zero = 0; - if (!tb_stream_bwrit(ostream, &zero, 1)) { - return tb_false; - } - } - - // align to 4 bytes - padding = symtab_offset - data_end_offset; - if (padding > 0) { - tb_byte_t zero = 0; - while (padding-- > 0) { - if (!tb_stream_bwrit(ostream, &zero, 1)) { - return tb_false; - } - } - } - - // write symbol table - // strx starts from 4 (after 4-byte size field) - tb_uint32_t strx = 4; - // symbol 0: _binary_xxx_start - xm_macho_nlist_t nlist_start; - tb_memset(&nlist_start, 0, sizeof(nlist_start)); - nlist_start.strx = strx; - nlist_start.type = XM_MACHO_N_TYPE_SECT | XM_MACHO_N_EXT; // N_SECT | N_EXT - nlist_start.sect = 1; - nlist_start.desc = 0; - nlist_start.value = 0; - if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&nlist_start, sizeof(nlist_start))) { - return tb_false; - } - strx += (tb_uint32_t)(start_len + 1); - - // symbol 1: _binary_xxx_end - xm_macho_nlist_t nlist_end; - tb_memset(&nlist_end, 0, sizeof(nlist_end)); - nlist_end.strx = strx; - nlist_end.type = XM_MACHO_N_TYPE_SECT | XM_MACHO_N_EXT; // N_SECT | N_EXT - nlist_end.sect = 1; - nlist_end.desc = 0; - nlist_end.value = data_size; - if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&nlist_end, sizeof(nlist_end))) { - return tb_false; - } - strx += (tb_uint32_t)(end_len + 1); - - // align to 4 bytes - padding = strtab_offset - (symtab_offset + nlist_size * nlist_count); - if (padding > 0) { - tb_byte_t zero = 0; - while (padding-- > 0) { - if (!tb_stream_bwrit(ostream, &zero, 1)) { - return tb_false; - } - } - } - - // write string table - tb_stream_bwrit(ostream, (tb_byte_t const *)&strtab_size, 4); - tb_stream_bwrit(ostream, (tb_byte_t const *)symbol_start, start_len); - tb_byte_t null = 0; - tb_stream_bwrit(ostream, &null, 1); - tb_stream_bwrit(ostream, (tb_byte_t const *)symbol_end, end_len); - tb_stream_bwrit(ostream, &null, 1); - - // align string table to 4 bytes - padding = strtab_size - (4 + (tb_uint32_t)start_len + 1 + (tb_uint32_t)end_len + 1); - if (padding > 0) { - tb_byte_t zero = 0; - while (padding-- > 0) { - if (!tb_stream_bwrit(ostream, &zero, 1)) { - return tb_false; - } - } - } - - return tb_true; -} - -static tb_bool_t xm_utils_bin2macho_dump(tb_stream_ref_t istream, - tb_stream_ref_t ostream, - tb_char_t const *symbol_prefix, - tb_char_t const *plat, - tb_char_t const *arch, - tb_char_t const *basename, - 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); - } else { - return xm_utils_bin2macho_dump_32(istream, ostream, symbol_prefix, plat, arch, basename, minos, sdk, zeroend); - } -} - -/* ////////////////////////////////////////////////////////////////////////////////////// - * implementation - */ - -/* generate Mach-O object file from binary file - * - * local ok, errors = utils.bin2macho(binaryfile, outputfile, symbol_prefix, plat, arch, basename) - */ -tb_int_t xm_utils_bin2macho(lua_State *lua) { - tb_assert_and_check_return_val(lua, 0); - - // get the binaryfile - tb_char_t const *binaryfile = luaL_checkstring(lua, 1); - tb_check_return_val(binaryfile, 0); - - // get the outputfile - tb_char_t const *outputfile = luaL_checkstring(lua, 2); - tb_check_return_val(outputfile, 0); - - // get symbol prefix (optional) - tb_char_t const *symbol_prefix = lua_isstring(lua, 3) ? lua_tostring(lua, 3) : tb_null; - - // get plat (optional) - tb_char_t const *plat = lua_isstring(lua, 4) ? lua_tostring(lua, 4) : tb_null; - - // get arch (optional) - tb_char_t const *arch = lua_isstring(lua, 5) ? lua_tostring(lua, 5) : tb_null; - - // get basename (optional) - tb_char_t const *basename = lua_isstring(lua, 6) ? lua_tostring(lua, 6) : tb_null; - - // 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); - - // 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); - - // get zeroend (optional, default: false) - tb_bool_t zeroend = lua_toboolean(lua, 9); - - // do dump - tb_bool_t ok = tb_false; - tb_stream_ref_t istream = tb_stream_init_from_file(binaryfile, TB_FILE_MODE_RO); - tb_stream_ref_t ostream = tb_stream_init_from_file(outputfile, - TB_FILE_MODE_RW | TB_FILE_MODE_CREAT | TB_FILE_MODE_TRUNC); - do { - if (!tb_stream_open(istream)) { - lua_pushboolean(lua, tb_false); - lua_pushfstring(lua, "bin2macho: open %s failed", binaryfile); - break; - } - - if (!tb_stream_open(ostream)) { - lua_pushboolean(lua, tb_false); - lua_pushfstring(lua, "bin2macho: open %s failed", outputfile); - break; - } - - if (!xm_utils_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; - } - - ok = tb_true; - lua_pushboolean(lua, ok); - - } while (0); - - if (istream) { - tb_stream_clos(istream); - } - istream = tb_null; - - if (ostream) { - tb_stream_clos(ostream); - } - ostream = tb_null; - - return ok ? 1 : 2; -} - diff --git a/core/src/xmake/utils/prefix.h b/core/src/xmake/utils/prefix.h deleted file mode 100644 index 077b4027f..000000000 --- a/core/src/xmake/utils/prefix.h +++ /dev/null @@ -1,69 +0,0 @@ -/*!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 prefix.h - * - */ -#ifndef XM_UTILS_PREFIX_H -#define XM_UTILS_PREFIX_H - -/* ////////////////////////////////////////////////////////////////////////////////////// - * includes - */ -#include "../prefix.h" - -/* ////////////////////////////////////////////////////////////////////////////////////// - * inline implementation - */ - -/* copy data from input stream to output stream - * - * @param istream the input stream - * @param ostream the output stream - * @param size the size to copy - * - * @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) { - tb_assert_and_check_return_val(istream && ostream && size > 0, tb_false); - - tb_byte_t data[TB_STREAM_BLOCK_MAXN]; - tb_hize_t writ = 0; - do { - tb_size_t need = (tb_size_t)tb_min(size - writ, (tb_hize_t)TB_STREAM_BLOCK_MAXN); - tb_check_break(need); - - tb_long_t real = tb_stream_read(istream, data, need); - if (real > 0) { - if (!tb_stream_bwrit(ostream, data, (tb_size_t)real)) { - return tb_false; - } - writ += real; - } else if (!real) { - tb_long_t wait = tb_stream_wait(istream, TB_STREAM_WAIT_READ, tb_stream_timeout(istream)); - tb_check_break(wait > 0 && (wait & TB_STREAM_WAIT_READ)); - } else { - return tb_false; - } - - tb_check_break(writ < size); - } while (1); - - return tb_true; -} - -#endif 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 -- cgit v1.3.1 From fe0d27da5b8ba182c6bcbdf3577ee68ea1050b4a Mon Sep 17 00:00:00 2001 From: ruki Date: Sun, 7 Dec 2025 17:50:11 +0800 Subject: fix perms for macho --- core/src/xmake/binutils/bin2macho.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'core/src') diff --git a/core/src/xmake/binutils/bin2macho.c b/core/src/xmake/binutils/bin2macho.c index 5add65d6c..45d244316 100644 --- a/core/src/xmake/binutils/bin2macho.c +++ b/core/src/xmake/binutils/bin2macho.c @@ -392,8 +392,8 @@ static tb_bool_t xm_binutils_bin2macho_dump_64(tb_stream_ref_t istream, segment.vmsize = data_size; segment.fileoff = data_offset; segment.filesize = data_size; - segment.maxprot = XM_MACHO_VM_PROT_READ; // VM_PROT_READ (read-only) - segment.initprot = XM_MACHO_VM_PROT_READ; // VM_PROT_READ (read-only) + segment.maxprot = XM_MACHO_VM_PROT_READ | XM_MACHO_VM_PROT_EXECUTE; // VM_PROT_READ | VM_PROT_EXECUTE (r-x) + segment.initprot = XM_MACHO_VM_PROT_READ | XM_MACHO_VM_PROT_EXECUTE; // VM_PROT_READ | VM_PROT_EXECUTE (r-x) segment.nsects = 1; segment.flags = 0; if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&segment, sizeof(segment))) { @@ -637,8 +637,8 @@ static tb_bool_t xm_binutils_bin2macho_dump_32(tb_stream_ref_t istream, segment.vmsize = data_size; segment.fileoff = data_offset; segment.filesize = data_size; - segment.maxprot = XM_MACHO_VM_PROT_READ; // VM_PROT_READ (read-only) - segment.initprot = XM_MACHO_VM_PROT_READ; // VM_PROT_READ (read-only) + segment.maxprot = XM_MACHO_VM_PROT_READ | XM_MACHO_VM_PROT_EXECUTE; // VM_PROT_READ | VM_PROT_EXECUTE (r-x) + segment.initprot = XM_MACHO_VM_PROT_READ | XM_MACHO_VM_PROT_EXECUTE; // VM_PROT_READ | VM_PROT_EXECUTE (r-x) segment.nsects = 1; segment.flags = 0; if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&segment, sizeof(segment))) { -- cgit v1.3.1 From 14a0571634159329fd9dd4d2313e918284574381 Mon Sep 17 00:00:00 2001 From: ruki Date: Sun, 7 Dec 2025 17:52:41 +0800 Subject: fix mingw x86 for coff --- core/src/xmake/binutils/bin2coff.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'core/src') diff --git a/core/src/xmake/binutils/bin2coff.c b/core/src/xmake/binutils/bin2coff.c index 686b89fac..09274c3ad 100644 --- a/core/src/xmake/binutils/bin2coff.c +++ b/core/src/xmake/binutils/bin2coff.c @@ -227,7 +227,7 @@ static tb_bool_t xm_binutils_bin2coff_dump(tb_stream_ref_t istream, header.nsects = 1; header.time = 0; header.symtabofs = symbol_table_ofs; - header.nsyms = 4; // .rdata section symbol (1) + auxiliary entry (1) + 2 data symbols (start, end) + header.nsyms = 3; // .rdata section symbol (1) + 2 data symbols (start, end) - auxiliary entries are not counted header.opthdr = 0; header.flags = 0; if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&header, sizeof(header))) { -- cgit v1.3.1 From 3a580b353033bde4dcff2bbce12f475c0662c25d Mon Sep 17 00:00:00 2001 From: ruki Date: Sun, 7 Dec 2025 18:14:56 +0800 Subject: revert coff syms --- core/src/xmake/binutils/bin2coff.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'core/src') diff --git a/core/src/xmake/binutils/bin2coff.c b/core/src/xmake/binutils/bin2coff.c index 09274c3ad..42032db10 100644 --- a/core/src/xmake/binutils/bin2coff.c +++ b/core/src/xmake/binutils/bin2coff.c @@ -227,7 +227,12 @@ static tb_bool_t xm_binutils_bin2coff_dump(tb_stream_ref_t istream, header.nsects = 1; header.time = 0; header.symtabofs = symbol_table_ofs; - header.nsyms = 3; // .rdata section symbol (1) + 2 data symbols (start, end) - auxiliary entries are not counted + // Note: nsyms should count all symbol table entries including auxiliary entries + // Symbol 0: section symbol (1 entry) + auxiliary entry (1 entry) = 2 entries + // Symbol 1: _binary_xxx_start (1 entry) + // Symbol 2: _binary_xxx_end (1 entry) + // Total: 4 entries + header.nsyms = 4; header.opthdr = 0; header.flags = 0; if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&header, sizeof(header))) { @@ -299,7 +304,9 @@ static tb_bool_t xm_binutils_bin2coff_dump(tb_stream_ref_t istream, tb_memset(&sym_start_tail, 0, sizeof(sym_start_tail)); sym_start_tail.value = 0; sym_start_tail.sect = 1; + sym_start_tail.type = 0; // IMAGE_SYM_TYPE_NULL sym_start_tail.scl = 2; // IMAGE_SYM_CLASS_EXTERNAL + sym_start_tail.naux = 0; // no auxiliary entry if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&sym_start_tail, sizeof(sym_start_tail))) { return tb_false; } @@ -310,7 +317,9 @@ static tb_bool_t xm_binutils_bin2coff_dump(tb_stream_ref_t istream, tb_memset(&sym_end_tail, 0, sizeof(sym_end_tail)); sym_end_tail.value = datasize; sym_end_tail.sect = 1; + sym_end_tail.type = 0; // IMAGE_SYM_TYPE_NULL sym_end_tail.scl = 2; // IMAGE_SYM_CLASS_EXTERNAL + sym_end_tail.naux = 0; // no auxiliary entry if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&sym_end_tail, sizeof(sym_end_tail))) { return tb_false; } -- cgit v1.3.1 From cbb74b2c9d318410bf832286632438977b577e13 Mon Sep 17 00:00:00 2001 From: ruki Date: Sun, 7 Dec 2025 19:06:11 +0800 Subject: fix mingw i386 for coff --- core/src/xmake/binutils/bin2coff.c | 62 ++++++++++++++++++++++++++------------ 1 file changed, 43 insertions(+), 19 deletions(-) (limited to 'core/src') diff --git a/core/src/xmake/binutils/bin2coff.c b/core/src/xmake/binutils/bin2coff.c index 42032db10..feff478ce 100644 --- a/core/src/xmake/binutils/bin2coff.c +++ b/core/src/xmake/binutils/bin2coff.c @@ -175,6 +175,10 @@ static tb_bool_t xm_binutils_bin2coff_dump(tb_stream_ref_t istream, datasize++; } + // Determine if this is i386 architecture (for symbol prefix adjustment) + tb_uint16_t machine = xm_binutils_bin2coff_get_machine(arch); + tb_bool_t is_i386 = (machine == XM_COFF_MACHINE_I386); + // generate symbol names from filename tb_char_t symbol_name[256] = {0}; tb_char_t symbol_start[256] = {0}; @@ -186,10 +190,22 @@ static tb_bool_t xm_binutils_bin2coff_dump(tb_stream_ref_t istream, } // build symbol name + // Note: On i386 Windows, C compiler automatically adds an underscore prefix to external symbols + // So if we use "_binary_", the actual symbol becomes "__binary_" after compilation + // To match, we need to ensure the prefix has two underscores for i386 if (symbol_prefix) { - tb_snprintf(symbol_name, sizeof(symbol_name), "%s%s", symbol_prefix, basename); + if (is_i386 && symbol_prefix[0] == '_' && symbol_prefix[1] != '_') { + // i386: if prefix starts with single underscore, add another one + tb_snprintf(symbol_name, sizeof(symbol_name), "_%s%s", symbol_prefix, basename); + } else { + tb_snprintf(symbol_name, sizeof(symbol_name), "%s%s", symbol_prefix, basename); + } } else { - tb_snprintf(symbol_name, sizeof(symbol_name), "_binary_%s", basename); + if (is_i386) { + tb_snprintf(symbol_name, sizeof(symbol_name), "__binary_%s", basename); + } else { + tb_snprintf(symbol_name, sizeof(symbol_name), "_binary_%s", basename); + } } // replace non-alphanumeric with underscore @@ -207,18 +223,21 @@ static tb_bool_t xm_binutils_bin2coff_dump(tb_stream_ref_t istream, tb_uint32_t section_header_size = sizeof(xm_coff_section_t); tb_uint32_t section_data_ofs = header_size + section_header_size; tb_uint32_t section_data_size = datasize; - tb_uint32_t symbol_table_ofs = section_data_ofs + ((section_data_size + 3) & ~3); // align to 4 bytes - tb_uint32_t string_table_size = 4; // initial 4-byte size field - - // calculate string table size + tb_uint32_t section_data_padding = (4 - (section_data_size & 3)) & 3; + tb_uint32_t symbol_table_ofs = section_data_ofs + section_data_size + section_data_padding; + + // calculate string table size (content only, excluding the 4-byte size field) + tb_uint32_t string_table_content_size = 0; tb_size_t start_len = tb_strlen(symbol_start); tb_size_t end_len = tb_strlen(symbol_end); if (start_len > 8) { - string_table_size += (tb_uint32_t)(start_len + 1); + string_table_content_size += (tb_uint32_t)(start_len + 1); } if (end_len > 8) { - string_table_size += (tb_uint32_t)(end_len + 1); + string_table_content_size += (tb_uint32_t)(end_len + 1); } + // string table size field should include the size field itself + tb_uint32_t string_table_size = 4 + string_table_content_size; // write COFF header xm_coff_header_t header; @@ -227,12 +246,13 @@ static tb_bool_t xm_binutils_bin2coff_dump(tb_stream_ref_t istream, header.nsects = 1; header.time = 0; header.symtabofs = symbol_table_ofs; - // Note: nsyms should count all symbol table entries including auxiliary entries - // Symbol 0: section symbol (1 entry) + auxiliary entry (1 entry) = 2 entries - // Symbol 1: _binary_xxx_start (1 entry) - // Symbol 2: _binary_xxx_end (1 entry) - // Total: 4 entries - header.nsyms = 4; + // Note: COFF spec says nsyms is the number of symbol table entries (including aux entries) + // - Section symbol (1) + aux entry (1) + start symbol (1) + end symbol (1) = 4 entries + // - Total size: 4 * 18 = 72 bytes + // - i386 linker: calculates string table as symtabofs + nsyms * 18 = symtabofs + 72 (correct) + // - When reading symbols, linker follows naux fields to skip aux entries correctly + // - From mingw i386 analysis: section symbols MUST have aux entry (naux=1) + header.nsyms = 4; // 3 symbols + 1 aux entry header.opthdr = 0; header.flags = 0; if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&header, sizeof(header))) { @@ -269,9 +289,8 @@ static tb_bool_t xm_binutils_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_binutils_bin2coff_write_padding(ostream, padding); + if (section_data_padding > 0) { + xm_binutils_bin2coff_write_padding(ostream, section_data_padding); } // write symbol table @@ -283,7 +302,8 @@ static tb_bool_t xm_binutils_bin2coff_dump(tb_stream_ref_t istream, sym_section.sect = 1; // section index (1-based) sym_section.type = 0; // IMAGE_SYM_TYPE_NULL sym_section.scl = 3; // IMAGE_SYM_CLASS_STATIC - sym_section.naux = 1; // auxiliary entry + // Section symbol MUST have auxiliary entry for i386 compatibility (as seen in mingw-generated files) + sym_section.naux = 1; // auxiliary entry (required for i386) if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&sym_section, sizeof(sym_section))) { return tb_false; } @@ -325,7 +345,11 @@ static tb_bool_t xm_binutils_bin2coff_dump(tb_stream_ref_t istream, } // write string table - tb_stream_bwrit(ostream, (tb_byte_t const *)&string_table_size, 4); + // Symbol table size: section symbol (18) + aux entry (18) + start symbol (18) + end symbol (18) = 72 bytes + // String table starts at symtabofs + 72, which matches nsyms * 18 = 4 * 18 = 72 + if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&string_table_size, 4)) { + return tb_false; + } if (start_len > 8) { xm_binutils_bin2coff_write_string(ostream, symbol_start, start_len); tb_byte_t null = 0; -- cgit v1.3.1 From c4b2eb7751d994d00da94133c397389c70d3b3a8 Mon Sep 17 00:00:00 2001 From: ruki Date: Sun, 7 Dec 2025 19:10:15 +0800 Subject: update comments --- core/src/xmake/binutils/bin2coff.c | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) (limited to 'core/src') diff --git a/core/src/xmake/binutils/bin2coff.c b/core/src/xmake/binutils/bin2coff.c index feff478ce..8224a7607 100644 --- a/core/src/xmake/binutils/bin2coff.c +++ b/core/src/xmake/binutils/bin2coff.c @@ -175,7 +175,7 @@ static tb_bool_t xm_binutils_bin2coff_dump(tb_stream_ref_t istream, datasize++; } - // Determine if this is i386 architecture (for symbol prefix adjustment) + // determine architecture for symbol prefix adjustment tb_uint16_t machine = xm_binutils_bin2coff_get_machine(arch); tb_bool_t is_i386 = (machine == XM_COFF_MACHINE_I386); @@ -190,9 +190,9 @@ static tb_bool_t xm_binutils_bin2coff_dump(tb_stream_ref_t istream, } // build symbol name - // Note: On i386 Windows, C compiler automatically adds an underscore prefix to external symbols - // So if we use "_binary_", the actual symbol becomes "__binary_" after compilation - // To match, we need to ensure the prefix has two underscores for i386 + // note: on i386 Windows, C compiler automatically adds an underscore prefix to external symbols + // so if we use "_binary_", the actual symbol becomes "__binary_" after compilation + // to match, we need to ensure the prefix has two underscores for i386 if (symbol_prefix) { if (is_i386 && symbol_prefix[0] == '_' && symbol_prefix[1] != '_') { // i386: if prefix starts with single underscore, add another one @@ -242,16 +242,16 @@ static tb_bool_t xm_binutils_bin2coff_dump(tb_stream_ref_t istream, // write COFF header xm_coff_header_t header; tb_memset(&header, 0, sizeof(header)); - header.machine = xm_binutils_bin2coff_get_machine(arch); + header.machine = machine; header.nsects = 1; header.time = 0; header.symtabofs = symbol_table_ofs; - // Note: COFF spec says nsyms is the number of symbol table entries (including aux entries) - // - Section symbol (1) + aux entry (1) + start symbol (1) + end symbol (1) = 4 entries - // - Total size: 4 * 18 = 72 bytes - // - i386 linker: calculates string table as symtabofs + nsyms * 18 = symtabofs + 72 (correct) - // - When reading symbols, linker follows naux fields to skip aux entries correctly - // - From mingw i386 analysis: section symbols MUST have aux entry (naux=1) + // note: COFF spec says nsyms is the number of symbol table entries (including aux entries) + // section symbol (1) + aux entry (1) + start symbol (1) + end symbol (1) = 4 entries + // total size: 4 * 18 = 72 bytes + // i386 linker calculates string table as symtabofs + nsyms * 18 = symtabofs + 72 (correct) + // when reading symbols, linker follows naux fields to skip aux entries correctly + // from mingw i386 analysis: section symbols MUST have aux entry (naux=1) header.nsyms = 4; // 3 symbols + 1 aux entry header.opthdr = 0; header.flags = 0; @@ -302,7 +302,7 @@ static tb_bool_t xm_binutils_bin2coff_dump(tb_stream_ref_t istream, sym_section.sect = 1; // section index (1-based) sym_section.type = 0; // IMAGE_SYM_TYPE_NULL sym_section.scl = 3; // IMAGE_SYM_CLASS_STATIC - // Section symbol MUST have auxiliary entry for i386 compatibility (as seen in mingw-generated files) + // section symbol MUST have auxiliary entry for i386 compatibility (as seen in mingw-generated files) sym_section.naux = 1; // auxiliary entry (required for i386) if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&sym_section, sizeof(sym_section))) { return tb_false; @@ -317,7 +317,7 @@ static tb_bool_t xm_binutils_bin2coff_dump(tb_stream_ref_t istream, return tb_false; } - // symbol 1: _binary_xxx_start + // symbol 1: _binary_xxx_start (or __binary_xxx_start for i386) tb_uint32_t strtab_offset = 4; // start after size field xm_binutils_bin2coff_write_symbol_name(ostream, symbol_start, &strtab_offset); xm_coff_symbol_tail_t sym_start_tail; @@ -331,7 +331,7 @@ static tb_bool_t xm_binutils_bin2coff_dump(tb_stream_ref_t istream, return tb_false; } - // symbol 2: _binary_xxx_end + // symbol 2: _binary_xxx_end (or __binary_xxx_end for i386) 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)); @@ -345,8 +345,8 @@ static tb_bool_t xm_binutils_bin2coff_dump(tb_stream_ref_t istream, } // write string table - // Symbol table size: section symbol (18) + aux entry (18) + start symbol (18) + end symbol (18) = 72 bytes - // String table starts at symtabofs + 72, which matches nsyms * 18 = 4 * 18 = 72 + // symbol table size: section symbol (18) + aux entry (18) + start symbol (18) + end symbol (18) = 72 bytes + // string table starts at symtabofs + 72, which matches nsyms * 18 = 4 * 18 = 72 if (!tb_stream_bwrit(ostream, (tb_byte_t const *)&string_table_size, 4)) { return tb_false; } @@ -370,7 +370,8 @@ static tb_bool_t xm_binutils_bin2coff_dump(tb_stream_ref_t istream, /* generate COFF object file from binary file * - * local ok, errors = binutils.bin2coff(binaryfile, outputfile, symbol_prefix, arch, basename) + * @param lua the lua state + * @return 1 on success, 2 on failure (with error message on stack) */ tb_int_t xm_binutils_bin2coff(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); -- cgit v1.3.1 From e17cc0cfe314d1e691d120d2dfa92f6c92790dff Mon Sep 17 00:00:00 2001 From: ruki Date: Sun, 7 Dec 2025 19:31:07 +0800 Subject: improve bin2elf --- core/src/xmake/binutils/bin2coff.c | 2 +- core/src/xmake/binutils/bin2elf.c | 115 ++++++++++++++++++++++++++++++++----- 2 files changed, 102 insertions(+), 15 deletions(-) (limited to 'core/src') diff --git a/core/src/xmake/binutils/bin2coff.c b/core/src/xmake/binutils/bin2coff.c index 8224a7607..9a095a2cd 100644 --- a/core/src/xmake/binutils/bin2coff.c +++ b/core/src/xmake/binutils/bin2coff.c @@ -225,7 +225,7 @@ static tb_bool_t xm_binutils_bin2coff_dump(tb_stream_ref_t istream, tb_uint32_t section_data_size = datasize; tb_uint32_t section_data_padding = (4 - (section_data_size & 3)) & 3; tb_uint32_t symbol_table_ofs = section_data_ofs + section_data_size + section_data_padding; - + // calculate string table size (content only, excluding the 4-byte size field) tb_uint32_t string_table_content_size = 0; tb_size_t start_len = tb_strlen(symbol_start); diff --git a/core/src/xmake/binutils/bin2elf.c b/core/src/xmake/binutils/bin2elf.c index bb2507a34..22339925c 100644 --- a/core/src/xmake/binutils/bin2elf.c +++ b/core/src/xmake/binutils/bin2elf.c @@ -33,10 +33,21 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * macros */ +#define XM_ELF_MACHINE_NONE 0x00 +#define XM_ELF_MACHINE_SPARC 0x02 +#define XM_ELF_MACHINE_I386 0x03 +#define XM_ELF_MACHINE_MIPS 0x08 +#define XM_ELF_MACHINE_POWERPC 0x14 +#define XM_ELF_MACHINE_POWERPC64 0x15 +#define XM_ELF_MACHINE_S390 0x16 +#define XM_ELF_MACHINE_ARM 0x28 +#define XM_ELF_MACHINE_SUPERH 0x2a +#define XM_ELF_MACHINE_SPARC64 0x2b +#define XM_ELF_MACHINE_IA_64 0x32 #define XM_ELF_MACHINE_X86_64 0x3e +#define XM_ELF_MACHINE_RISCV 0xf3 #define XM_ELF_MACHINE_ARM64 0xb7 -#define XM_ELF_MACHINE_ARM 0x28 -#define XM_ELF_MACHINE_I386 0x03 +#define XM_ELF_MACHINE_LOONGARCH 0x102 #define XM_ELF_SHT_PROGBITS 0x1 #define XM_ELF_SHT_SYMTAB 0x2 @@ -139,16 +150,59 @@ static tb_uint16_t xm_binutils_bin2elf_get_machine(tb_char_t const *arch) { if (!arch) { return XM_ELF_MACHINE_X86_64; } + // x86/x86_64 if (tb_strcmp(arch, "x86_64") == 0 || tb_strcmp(arch, "x64") == 0) { return XM_ELF_MACHINE_X86_64; - } else if (tb_strcmp(arch, "arm64") == 0 || tb_strcmp(arch, "aarch64") == 0 || - tb_strcmp(arch, "arm64-v8a") == 0) { + } else if (tb_strcmp(arch, "i386") == 0 || tb_strcmp(arch, "x86") == 0) { + return XM_ELF_MACHINE_I386; + } + // ARM + else if (tb_strcmp(arch, "arm64") == 0 || tb_strcmp(arch, "aarch64") == 0 || + tb_strcmp(arch, "arm64-v8a") == 0) { return XM_ELF_MACHINE_ARM64; } else if (tb_strcmp(arch, "arm") == 0 || tb_strcmp(arch, "armv7") == 0 || - tb_strcmp(arch, "armeabi-v7a") == 0) { + tb_strcmp(arch, "armeabi-v7a") == 0 || tb_strcmp(arch, "armv6") == 0 || + tb_strcmp(arch, "armv5") == 0) { return XM_ELF_MACHINE_ARM; - } else if (tb_strcmp(arch, "i386") == 0 || tb_strcmp(arch, "x86") == 0) { - return XM_ELF_MACHINE_I386; + } + // MIPS (MIPS and MIPS64 use same machine type, distinguished by ELF class) + else if (tb_strncmp(arch, "mips", 4) == 0) { + return XM_ELF_MACHINE_MIPS; + } + // PowerPC + else if (tb_strncmp(arch, "ppc64", 5) == 0 || tb_strncmp(arch, "powerpc64", 9) == 0) { + return XM_ELF_MACHINE_POWERPC64; + } else if (tb_strncmp(arch, "ppc", 3) == 0 || tb_strncmp(arch, "powerpc", 7) == 0) { + return XM_ELF_MACHINE_POWERPC; + } + // RISC-V (RISC-V and RISC-V64 use different machine types) + else if (tb_strncmp(arch, "riscv64", 7) == 0 || + (tb_strncmp(arch, "riscv", 5) == 0 && tb_strstr(arch, "64"))) { + return XM_ELF_MACHINE_RISCV; // RISC-V 64-bit uses same machine type, distinguished by ELF class + } else if (tb_strncmp(arch, "riscv", 5) == 0) { + return XM_ELF_MACHINE_RISCV; + } + // SPARC + else if (tb_strncmp(arch, "sparc64", 7) == 0) { + return XM_ELF_MACHINE_SPARC64; + } else if (tb_strncmp(arch, "sparc", 5) == 0) { + return XM_ELF_MACHINE_SPARC; + } + // s390x + else if (tb_strcmp(arch, "s390x") == 0 || tb_strcmp(arch, "s390") == 0) { + return XM_ELF_MACHINE_S390; + } + // LoongArch (LoongArch and LoongArch64 use same machine type, distinguished by ELF class) + else if (tb_strncmp(arch, "loongarch", 9) == 0 || tb_strncmp(arch, "loong64", 7) == 0) { + return XM_ELF_MACHINE_LOONGARCH; + } + // SuperH + else if (tb_strncmp(arch, "sh", 2) == 0 || tb_strncmp(arch, "superh", 6) == 0) { + return XM_ELF_MACHINE_SUPERH; + } + // IA-64 (Itanium) + else if (tb_strcmp(arch, "ia64") == 0 || tb_strcmp(arch, "itanium") == 0) { + return XM_ELF_MACHINE_IA_64; } return XM_ELF_MACHINE_X86_64; } @@ -157,9 +211,42 @@ static tb_bool_t xm_binutils_bin2elf_is_64bit(tb_char_t const *arch) { if (!arch) { return tb_true; } - if (tb_strcmp(arch, "x86_64") == 0 || tb_strcmp(arch, "x64") == 0 || - tb_strcmp(arch, "arm64") == 0 || tb_strcmp(arch, "aarch64") == 0 || - tb_strcmp(arch, "arm64-v8a") == 0) { + // x86_64 + if (tb_strcmp(arch, "x86_64") == 0 || tb_strcmp(arch, "x64") == 0) { + return tb_true; + } + // ARM64 + else if (tb_strcmp(arch, "arm64") == 0 || tb_strcmp(arch, "aarch64") == 0 || + tb_strcmp(arch, "arm64-v8a") == 0) { + return tb_true; + } + // MIPS64 + else if (tb_strncmp(arch, "mips64", 6) == 0) { + return tb_true; + } + // PowerPC64 + else if (tb_strncmp(arch, "ppc64", 5) == 0 || tb_strncmp(arch, "powerpc64", 9) == 0) { + return tb_true; + } + // RISC-V 64 + else if (tb_strncmp(arch, "riscv64", 7) == 0 || + (tb_strncmp(arch, "riscv", 5) == 0 && tb_strstr(arch, "64"))) { + return tb_true; + } + // SPARC64 + else if (tb_strncmp(arch, "sparc64", 7) == 0) { + return tb_true; + } + // s390x + else if (tb_strcmp(arch, "s390x") == 0) { + return tb_true; + } + // LoongArch64 + else if (tb_strncmp(arch, "loongarch64", 11) == 0) { + return tb_true; + } + // IA-64 + else if (tb_strcmp(arch, "ia64") == 0 || tb_strcmp(arch, "itanium") == 0) { return tb_true; } return tb_false; @@ -226,7 +313,7 @@ static tb_bool_t xm_binutils_bin2elf_dump_32(tb_stream_ref_t istream, tb_uint32_t symtab_size = 3 * sizeof(xm_elf32_symbol_t); // NULL, start, end tb_uint32_t symtab_padding = (4 - (symtab_size & 3)) & 3; // align to 4 bytes tb_uint32_t strtab_ofs = symtab_ofs + symtab_size + symtab_padding; - + // calculate string table size tb_size_t start_len = tb_strlen(symbol_start); tb_size_t end_len = tb_strlen(symbol_end); @@ -235,7 +322,7 @@ static tb_bool_t xm_binutils_bin2elf_dump_32(tb_stream_ref_t istream, strtab_size += (tb_uint32_t)(end_len + 1); tb_uint32_t strtab_padding = (4 - (strtab_size & 3)) & 3; // align to 4 bytes tb_uint32_t shstrtab_ofs = strtab_ofs + strtab_size + strtab_padding; - + // calculate section header string table size tb_uint32_t shstrtab_size = 1; // initial null byte shstrtab_size += 8; // ".rodata\0" (7 + 1) @@ -530,7 +617,7 @@ static tb_bool_t xm_binutils_bin2elf_dump_64(tb_stream_ref_t istream, tb_uint32_t symtab_size = 3 * sizeof(xm_elf64_symbol_t); // NULL, start, end tb_uint32_t symtab_padding = (8 - (symtab_size & 7)) & 7; tb_uint32_t strtab_ofs = symtab_ofs + symtab_size + symtab_padding; - + // calculate string table size tb_size_t start_len = tb_strlen(symbol_start); tb_size_t end_len = tb_strlen(symbol_end); @@ -538,7 +625,7 @@ static tb_bool_t xm_binutils_bin2elf_dump_64(tb_stream_ref_t istream, strtab_size += (tb_uint32_t)(start_len + 1); strtab_size += (tb_uint32_t)(end_len + 1); tb_uint32_t strtab_padding = (8 - (strtab_size & 7)) & 7; - + // calculate section header string table size tb_uint32_t shstrtab_size = 1; // initial null byte shstrtab_size += 8; // ".rodata\0" (7 + 1) -- cgit v1.3.1 From 8e3cc0092d8e356c6387aee941825cf66615541c Mon Sep 17 00:00:00 2001 From: ruki Date: Sun, 7 Dec 2025 19:43:34 +0800 Subject: add wasm support for bin2elf --- core/src/xmake/binutils/bin2elf.c | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'core/src') diff --git a/core/src/xmake/binutils/bin2elf.c b/core/src/xmake/binutils/bin2elf.c index 22339925c..a536143c4 100644 --- a/core/src/xmake/binutils/bin2elf.c +++ b/core/src/xmake/binutils/bin2elf.c @@ -47,6 +47,7 @@ #define XM_ELF_MACHINE_X86_64 0x3e #define XM_ELF_MACHINE_RISCV 0xf3 #define XM_ELF_MACHINE_ARM64 0xb7 +#define XM_ELF_MACHINE_WASM 0xe7 #define XM_ELF_MACHINE_LOONGARCH 0x102 #define XM_ELF_SHT_PROGBITS 0x1 @@ -196,6 +197,10 @@ static tb_uint16_t xm_binutils_bin2elf_get_machine(tb_char_t const *arch) { else if (tb_strncmp(arch, "loongarch", 9) == 0 || tb_strncmp(arch, "loong64", 7) == 0) { return XM_ELF_MACHINE_LOONGARCH; } + // WebAssembly (WASM and WASM64 use same machine type, distinguished by ELF class) + else if (tb_strncmp(arch, "wasm", 4) == 0) { + return XM_ELF_MACHINE_WASM; + } // SuperH else if (tb_strncmp(arch, "sh", 2) == 0 || tb_strncmp(arch, "superh", 6) == 0) { return XM_ELF_MACHINE_SUPERH; @@ -245,6 +250,10 @@ static tb_bool_t xm_binutils_bin2elf_is_64bit(tb_char_t const *arch) { else if (tb_strncmp(arch, "loongarch64", 11) == 0) { return tb_true; } + // WebAssembly 64 + else if (tb_strcmp(arch, "wasm64") == 0) { + return tb_true; + } // IA-64 else if (tb_strcmp(arch, "ia64") == 0 || tb_strcmp(arch, "itanium") == 0) { return tb_true; -- cgit v1.3.1