diff options
| author | ruki <[email protected]> | 2025-12-09 00:29:51 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2025-12-09 00:29:51 +0800 |
| commit | ba293bc4b8840da34220e1f54313d531d18dde8e (patch) | |
| tree | e6b6325ccf446b845b26a3a21f06f0bd84914170 /core/src/xmake/binutils/macho | |
| parent | f1726a8f0f63160ae2185f28b38a740caf6d1ab1 (diff) | |
add binary readsyms
Diffstat (limited to 'core/src/xmake/binutils/macho')
| -rw-r--r-- | core/src/xmake/binutils/macho/prefix.h | 65 | ||||
| -rw-r--r-- | core/src/xmake/binutils/macho/readsyms.c | 285 |
2 files changed, 350 insertions, 0 deletions
diff --git a/core/src/xmake/binutils/macho/prefix.h b/core/src/xmake/binutils/macho/prefix.h index b3f0fb511..c11fbebb6 100644 --- a/core/src/xmake/binutils/macho/prefix.h +++ b/core/src/xmake/binutils/macho/prefix.h @@ -318,5 +318,70 @@ static __tb_inline__ tb_uint32_t xm_binutils_macho_parse_version(tb_char_t const return (major << 16) | (minor << 8) | patch; } +/* ////////////////////////////////////////////////////////////////////////////////////// + * readsyms inline implementation + */ + +/* read string from Mach-O string table + * + * @param istream the input stream + * @param strtab_offset the string table offset + * @param offset the string offset (relative to string table content, after size field) + * @param name the buffer to store the string + * @param name_size the size of the buffer + * @return tb_true on success, tb_false on failure + */ +static __tb_inline__ tb_bool_t xm_binutils_macho_read_string(tb_stream_ref_t istream, tb_uint32_t strtab_offset, tb_uint32_t offset, tb_char_t *name, tb_size_t name_size) { + tb_assert_and_check_return_val(istream && name && name_size > 0, tb_false); + + tb_hize_t saved_pos = tb_stream_offset(istream); + // string table starts with 4-byte size field, so offset is from after that + if (!tb_stream_seek(istream, strtab_offset + 4 + offset)) { + return tb_false; + } + + tb_size_t pos = 0; + tb_byte_t c; + while (pos < name_size - 1) { + if (!tb_stream_bread(istream, &c, 1)) { + tb_stream_seek(istream, saved_pos); + return tb_false; + } + if (c == 0) { + break; + } + name[pos++] = (tb_char_t)c; + } + name[pos] = '\0'; + + tb_stream_seek(istream, saved_pos); + return tb_true; +} + +/* get symbol type string from Mach-O symbol type + * + * @param type the symbol type byte + * @return the type string + */ +static __tb_inline__ tb_char_t const *xm_binutils_macho_get_symbol_type(tb_uint8_t type) { + tb_uint8_t n_type = type & XM_MACHO_N_TYPE_MASK; + switch (n_type) { + case XM_MACHO_N_TYPE_SECT: return "section"; + default: return "unknown"; + } +} + +/* get symbol bind string from Mach-O symbol type + * + * @param type the symbol type byte + * @return the bind string + */ +static __tb_inline__ tb_char_t const *xm_binutils_macho_get_symbol_bind(tb_uint8_t type) { + if (type & XM_MACHO_N_EXT) { + return "external"; + } + return "local"; +} + #endif diff --git a/core/src/xmake/binutils/macho/readsyms.c b/core/src/xmake/binutils/macho/readsyms.c new file mode 100644 index 000000000..a78daaa5a --- /dev/null +++ b/core/src/xmake/binutils/macho/readsyms.c @@ -0,0 +1,285 @@ +/*!A cross-platform build utility based on Lua + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2015-present, Xmake Open Source Community. + * + * @author ruki + * @file readsyms.c + * + */ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * trace + */ +#define TB_TRACE_MODULE_NAME "readsyms_macho" +#define TB_TRACE_MODULE_DEBUG (0) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * private implementation + */ + +tb_bool_t xm_binutils_macho_read_symbols_32(tb_stream_ref_t istream, lua_State *lua) { + tb_assert_and_check_return_val(istream && lua, tb_false); + + // read Mach-O header + xm_macho_header_t header; + if (!tb_stream_seek(istream, 0)) { + return tb_false; + } + if (!tb_stream_bread(istream, (tb_byte_t*)&header, sizeof(header))) { + return tb_false; + } + + // find LC_SYMTAB command + xm_macho_symtab_command_t symtab_cmd; + tb_bool_t found_symtab = tb_false; + + tb_uint32_t offset = sizeof(header); + for (tb_uint32_t i = 0; i < header.ncmds; i++) { + tb_uint32_t cmd; + tb_uint32_t cmdsize; + + if (!tb_stream_seek(istream, offset)) { + return tb_false; + } + if (!tb_stream_bread(istream, (tb_byte_t*)&cmd, 4)) { + return tb_false; + } + if (!tb_stream_bread(istream, (tb_byte_t*)&cmdsize, 4)) { + return tb_false; + } + + if (cmd == XM_MACHO_LC_SYMTAB) { + if (!tb_stream_seek(istream, offset)) { + return tb_false; + } + if (!tb_stream_bread(istream, (tb_byte_t*)&symtab_cmd, sizeof(symtab_cmd))) { + return tb_false; + } + found_symtab = tb_true; + break; + } + + offset += cmdsize; + } + + if (!found_symtab) { + lua_newtable(lua); + return tb_true; + } + + // create result table + lua_newtable(lua); + + // read symbols + if (!tb_stream_seek(istream, symtab_cmd.symoff)) { + return tb_false; + } + + tb_uint32_t result_count = 0; + for (tb_uint32_t i = 0; i < symtab_cmd.nsyms; i++) { + xm_macho_nlist_t nlist; + if (!tb_stream_bread(istream, (tb_byte_t*)&nlist, sizeof(nlist))) { + return tb_false; + } + + // skip NULL symbols + if (nlist.strx == 0) { + continue; + } + + // get symbol name + tb_char_t name[256]; + if (!xm_binutils_macho_read_string(istream, symtab_cmd.stroff, nlist.strx, name, sizeof(name)) || !name[0]) { + continue; + } + + // create symbol table entry + lua_pushinteger(lua, result_count + 1); + lua_newtable(lua); + + // name + lua_pushstring(lua, "name"); + lua_pushstring(lua, name); + lua_settable(lua, -3); + + // value + lua_pushstring(lua, "value"); + lua_pushinteger(lua, nlist.value); + lua_settable(lua, -3); + + // section + lua_pushstring(lua, "section"); + lua_pushinteger(lua, nlist.sect); + lua_settable(lua, -3); + + // type + lua_pushstring(lua, "type"); + lua_pushstring(lua, xm_binutils_macho_get_symbol_type(nlist.type)); + lua_settable(lua, -3); + + // bind + lua_pushstring(lua, "bind"); + lua_pushstring(lua, xm_binutils_macho_get_symbol_bind(nlist.type)); + lua_settable(lua, -3); + + lua_settable(lua, -3); + result_count++; + } + + return tb_true; +} + +tb_bool_t xm_binutils_macho_read_symbols_64(tb_stream_ref_t istream, lua_State *lua) { + tb_assert_and_check_return_val(istream && lua, tb_false); + + // read Mach-O header + xm_macho_header_64_t header; + if (!tb_stream_seek(istream, 0)) { + return tb_false; + } + if (!tb_stream_bread(istream, (tb_byte_t*)&header, sizeof(header))) { + return tb_false; + } + + // find LC_SYMTAB command + xm_macho_symtab_command_t symtab_cmd; + tb_bool_t found_symtab = tb_false; + + tb_uint32_t offset = sizeof(header); + for (tb_uint32_t i = 0; i < header.ncmds; i++) { + tb_uint32_t cmd; + tb_uint32_t cmdsize; + + if (!tb_stream_seek(istream, offset)) { + return tb_false; + } + if (!tb_stream_bread(istream, (tb_byte_t*)&cmd, 4)) { + return tb_false; + } + if (!tb_stream_bread(istream, (tb_byte_t*)&cmdsize, 4)) { + return tb_false; + } + + if (cmd == XM_MACHO_LC_SYMTAB) { + if (!tb_stream_seek(istream, offset)) { + return tb_false; + } + if (!tb_stream_bread(istream, (tb_byte_t*)&symtab_cmd, sizeof(symtab_cmd))) { + return tb_false; + } + found_symtab = tb_true; + break; + } + + offset += cmdsize; + } + + if (!found_symtab) { + lua_newtable(lua); + return tb_true; + } + + // create result table + lua_newtable(lua); + + // read symbols + if (!tb_stream_seek(istream, symtab_cmd.symoff)) { + return tb_false; + } + + tb_uint32_t result_count = 0; + for (tb_uint32_t i = 0; i < symtab_cmd.nsyms; i++) { + xm_macho_nlist_64_t nlist; + if (!tb_stream_bread(istream, (tb_byte_t*)&nlist, sizeof(nlist))) { + return tb_false; + } + + // skip NULL symbols + if (nlist.strx == 0) { + continue; + } + + // get symbol name + tb_char_t name[256]; + if (!xm_binutils_macho_read_string(istream, symtab_cmd.stroff, nlist.strx, name, sizeof(name)) || !name[0]) { + continue; + } + + // create symbol table entry + lua_pushinteger(lua, result_count + 1); + lua_newtable(lua); + + // name + lua_pushstring(lua, "name"); + lua_pushstring(lua, name); + lua_settable(lua, -3); + + // value + lua_pushstring(lua, "value"); + lua_pushinteger(lua, nlist.value); + lua_settable(lua, -3); + + // section + lua_pushstring(lua, "section"); + lua_pushinteger(lua, nlist.sect); + lua_settable(lua, -3); + + // type + lua_pushstring(lua, "type"); + lua_pushstring(lua, xm_binutils_macho_get_symbol_type(nlist.type)); + lua_settable(lua, -3); + + // bind + lua_pushstring(lua, "bind"); + lua_pushstring(lua, xm_binutils_macho_get_symbol_bind(nlist.type)); + lua_settable(lua, -3); + + lua_settable(lua, -3); + result_count++; + } + + return tb_true; +} + +tb_bool_t xm_binutils_macho_read_symbols(tb_stream_ref_t istream, lua_State *lua) { + tb_assert_and_check_return_val(istream && lua, tb_false); + + // read and check magic + tb_uint8_t magic_bytes[4]; + if (!xm_binutils_read_magic(istream, magic_bytes, 4)) { + return tb_false; + } + + // convert to uint32_t (little endian, Mach-O uses native endian) + tb_uint32_t magic = (tb_uint32_t)magic_bytes[0] | + ((tb_uint32_t)magic_bytes[1] << 8) | + ((tb_uint32_t)magic_bytes[2] << 16) | + ((tb_uint32_t)magic_bytes[3] << 24); + + if (magic == XM_MACHO_MAGIC_32) { + return xm_binutils_macho_read_symbols_32(istream, lua); + } else if (magic == XM_MACHO_MAGIC_64) { + return xm_binutils_macho_read_symbols_64(istream, lua); + } + + return tb_false; +} + + |
