summaryrefslogtreecommitdiff
path: root/core/src
diff options
context:
space:
mode:
authorruki <[email protected]>2025-12-12 00:56:54 +0800
committerruki <[email protected]>2025-12-12 09:00:46 +0800
commite08e654aad4f829fcf1990dae32a14394055d96c (patch)
tree469bad643e764684a536997c72e199c082a85073 /core/src
parent0ac00bffb4b671cd1e08680824476c909bcf37a5 (diff)
format code
Diffstat (limited to 'core/src')
-rw-r--r--core/src/xmake/binutils/ar/extractlib.c150
-rw-r--r--core/src/xmake/binutils/ar/prefix.h131
-rw-r--r--core/src/xmake/binutils/ar/readsyms.c152
-rw-r--r--core/src/xmake/binutils/coff/bin2coff.c6
-rw-r--r--core/src/xmake/binutils/coff/readsyms.c8
-rw-r--r--core/src/xmake/binutils/elf/bin2elf.c12
-rw-r--r--core/src/xmake/binutils/macho/bin2macho.c12
-rw-r--r--core/src/xmake/binutils/mslib/extractlib.c15
-rw-r--r--core/src/xmake/binutils/mslib/prefix.h12
-rw-r--r--core/src/xmake/binutils/mslib/readsyms.c56
-rw-r--r--core/src/xmake/binutils/prefix.h19
-rw-r--r--core/src/xmake/binutils/readsyms.c11
12 files changed, 256 insertions, 328 deletions
diff --git a/core/src/xmake/binutils/ar/extractlib.c b/core/src/xmake/binutils/ar/extractlib.c
index 4ae349544..d9629394a 100644
--- a/core/src/xmake/binutils/ar/extractlib.c
+++ b/core/src/xmake/binutils/ar/extractlib.c
@@ -34,130 +34,6 @@
* implementation
*/
-/* get member name from AR header, handling extended names (#N/L format)
- *
- * @param istream the input stream
- * @param header the AR header
- * @param name output buffer for the name
- * @param name_size size of the name buffer
- * @param name_len output: actual name length
- * @param bytes_read output: total bytes read from stream (including newline, for extended names)
- * @return tb_true on success, tb_false on failure
- */
-static tb_bool_t xm_binutils_ar_get_member_name(tb_stream_ref_t istream, xm_ar_header_t const *header, tb_char_t *name, tb_size_t name_size, tb_size_t *name_len, tb_hize_t *bytes_read) {
- tb_assert_and_check_return_val(istream && header && name && name_size > 0 && name_len && bytes_read, tb_false);
- *bytes_read = 0;
-
- /* check for extended name format (#N/L or #1/N)
- * In BSD AR format:
- * - #1/N means name is directly after header, N is total length (including name)
- * - #N/L means name length is N, total length is L
- * - #1/N can also mean name is in long name table at offset 1
- * We'll try to read the name directly from stream first
- */
- if (header->name[0] == '#') {
- // find the '/' separator
- tb_size_t slash_pos = 0;
- for (tb_size_t i = 1; i < 16; i++) {
- if (header->name[i] == '/') {
- slash_pos = i;
- break;
- }
- }
-
- if (slash_pos > 0 && slash_pos < 16) {
- // parse the number before '/' (could be name length or offset)
- tb_int64_t first_num = xm_binutils_ar_parse_decimal(header->name + 1, slash_pos - 1);
- // parse the number after '/' (total length)
- tb_int64_t total_length = xm_binutils_ar_parse_decimal(header->name + slash_pos + 1, 16 - slash_pos - 1);
-
- if (first_num <= 0 || total_length <= 0) {
- return tb_false;
- }
-
- /* In BSD AR format, extended name is directly after header
- * The name data starts immediately after the header, no newline
- * Read exactly total_length bytes for the name section
- */
- tb_byte_t c;
- tb_size_t name_bytes = 0;
- tb_hize_t bytes_read_so_far = 0;
-
- // Read name characters until we hit null terminator or reach total_length
- while (bytes_read_so_far < (tb_hize_t)total_length && name_bytes < name_size - 1) {
- if (!tb_stream_bread(istream, &c, 1)) {
- return tb_false;
- }
- bytes_read_so_far++;
-
- if (c == '\0') {
- // Stop reading name at null terminator, but continue reading to reach total_length
- break;
- }
- // Include all characters in the name, including newlines if present
- name[name_bytes++] = (tb_char_t)c;
- }
- name[name_bytes] = '\0';
- *name_len = name_bytes;
-
- // Skip remaining bytes to reach total_length (there may be padding or null terminators)
- if (bytes_read_so_far < (tb_hize_t)total_length) {
- tb_hize_t remaining_to_read = (tb_hize_t)total_length - bytes_read_so_far;
- if (!tb_stream_skip(istream, remaining_to_read)) {
- return tb_false;
- }
- }
-
- // Total bytes read = name + padding = total_length
- *bytes_read = (tb_hize_t)total_length;
- return tb_true;
- }
- }
-
- // regular name (null-terminated or space-padded)
- tb_size_t i = 0;
- for (i = 0; i < 16 && i < name_size - 1; i++) {
- if (header->name[i] == ' ' || header->name[i] == '\0' || header->name[i] == '/') {
- break;
- }
- name[i] = header->name[i];
- }
- name[i] = '\0';
- *name_len = i;
- *bytes_read = 0; // Regular names are in header, not read from stream
- return tb_true;
-}
-
-/* check if member is a symbol table (should be skipped)
- *
- * @param name the member name
- * @return tb_true if it's a symbol table, tb_false otherwise
- */
-static __tb_inline__ tb_bool_t xm_binutils_ar_is_symbol_table(tb_char_t const *name) {
- tb_assert_and_check_return_val(name, tb_false);
- return (tb_strcmp(name, "__.SYMDEF") == 0 || tb_strcmp(name, "__.SYMDEF SORTED") == 0 ||
- tb_strcmp(name, "/") == 0 || tb_strcmp(name, "//") == 0 ||
- tb_strncmp(name, "__.SYMDEF", 9) == 0);
-}
-
-/* check if member is an object file (based on extension)
- *
- * @param name the member name
- * @return tb_true if it's likely an object file, tb_false otherwise
- */
-static __tb_inline__ tb_bool_t xm_binutils_ar_is_object_file(tb_char_t const *name) {
- tb_assert_and_check_return_val(name, tb_false);
- tb_size_t len = tb_strlen(name);
- if (len == 0) return tb_false;
-
- // check common object file extensions
- if (len >= 2 && name[len - 2] == '.' && name[len - 1] == 'o') return tb_true;
- if (len >= 4 && tb_strcmp(name + len - 4, ".obj") == 0) return tb_true;
-
- // check if it's a COFF/ELF/Mach-O file by detecting format
- // For now, we'll extract all non-symbol-table members
- return tb_true;
-}
/* generate unique filename to handle name conflicts
*
@@ -221,7 +97,6 @@ tb_bool_t xm_binutils_ar_extract(tb_stream_ref_t istream, tb_char_t const *outpu
}
tb_bool_t ok = tb_true;
- tb_byte_t* buffer = tb_null;
// iterate through AR members
while (ok) {
@@ -260,7 +135,9 @@ tb_bool_t xm_binutils_ar_extract(tb_stream_ref_t istream, tb_char_t const *outpu
if (skip) {
// skip remaining data + padding using sequential read
tb_hize_t skip_size = (tb_hize_t)member_size - name_bytes_read;
- if (member_size % 2) skip_size++; // add padding
+ if (member_size % 2) {
+ skip_size++; // add padding
+ }
if (!tb_stream_skip(istream, skip_size)) {
ok = tb_false;
break;
@@ -330,25 +207,9 @@ tb_bool_t xm_binutils_ar_extract(tb_stream_ref_t istream, tb_char_t const *outpu
// copy member data to output file
// member_size includes the name if extended format was used, so subtract name_bytes_read
- if (!buffer) buffer = tb_malloc_bytes(TB_STREAM_BLOCK_MAXN);
- if (!buffer) {
- tb_stream_exit(ostream);
- ok = tb_false;
- break;
- }
-
tb_hize_t remaining = (tb_hize_t)member_size - name_bytes_read;
- while (remaining > 0) {
- tb_size_t to_read = (tb_size_t)tb_min(remaining, (tb_hize_t)TB_STREAM_BLOCK_MAXN);
- if (!tb_stream_bread(istream, buffer, to_read)) {
- ok = tb_false;
- break;
- }
- if (!tb_stream_bwrit(ostream, buffer, to_read)) {
- ok = tb_false;
- break;
- }
- remaining -= to_read;
+ if (!xm_binutils_stream_copy(istream, ostream, remaining)) {
+ ok = tb_false;
}
tb_stream_clos(ostream);
@@ -365,6 +226,5 @@ tb_bool_t xm_binutils_ar_extract(tb_stream_ref_t istream, tb_char_t const *outpu
}
}
- if (buffer) tb_free(buffer);
return ok;
}
diff --git a/core/src/xmake/binutils/ar/prefix.h b/core/src/xmake/binutils/ar/prefix.h
index fd534f0bb..862a3923b 100644
--- a/core/src/xmake/binutils/ar/prefix.h
+++ b/core/src/xmake/binutils/ar/prefix.h
@@ -78,6 +78,100 @@ static __tb_inline__ tb_int64_t xm_binutils_ar_parse_decimal(tb_char_t const *st
return result;
}
+/* get member name from AR header, handling extended names (#N/L format)
+ *
+ * @param istream the input stream
+ * @param header the AR header
+ * @param name output buffer for the name
+ * @param name_size size of the name buffer
+ * @param name_len output: actual name length
+ * @param bytes_read output: total bytes read from stream (including newline, for extended names)
+ * @return tb_true on success, tb_false on failure
+ */
+static __tb_inline__ tb_bool_t xm_binutils_ar_get_member_name(tb_stream_ref_t istream, xm_ar_header_t const* header, tb_char_t* name, tb_size_t name_size, tb_size_t* name_len, tb_hize_t* bytes_read) {
+ tb_assert_and_check_return_val(istream && header && name && name_size > 0 && name_len && bytes_read, tb_false);
+ *bytes_read = 0;
+
+ /* check for extended name format (#N/L or #1/N)
+ * In BSD AR format:
+ * - #1/N means name is directly after header, N is total length (including name)
+ * - #N/L means name length is N, total length is L
+ * - #1/N can also mean name is in long name table at offset 1
+ * We'll try to read the name directly from stream first
+ */
+ if (header->name[0] == '#') {
+ // find the '/' separator
+ tb_size_t slash_pos = 0;
+ for (tb_size_t i = 1; i < 16; i++) {
+ if (header->name[i] == '/') {
+ slash_pos = i;
+ break;
+ }
+ }
+
+ if (slash_pos > 0 && slash_pos < 16) {
+ // parse the number before '/' (could be name length or offset)
+ tb_int64_t first_num = xm_binutils_ar_parse_decimal(header->name + 1, slash_pos - 1);
+ // parse the number after '/' (total length)
+ tb_int64_t total_length = xm_binutils_ar_parse_decimal(header->name + slash_pos + 1, 16 - slash_pos - 1);
+
+ if (first_num <= 0 || total_length <= 0) {
+ return tb_false;
+ }
+
+ /* In BSD AR format, extended name is directly after header
+ * The name data starts immediately after the header, no newline
+ * Read exactly total_length bytes for the name section
+ */
+ tb_byte_t c;
+ tb_size_t name_bytes = 0;
+ tb_hize_t bytes_read_so_far = 0;
+
+ // Read name characters until we hit null terminator or reach total_length
+ while (bytes_read_so_far < (tb_hize_t)total_length && name_bytes < name_size - 1) {
+ if (!tb_stream_bread(istream, &c, 1)) {
+ return tb_false;
+ }
+ bytes_read_so_far++;
+
+ if (c == '\0') {
+ // Stop reading name at null terminator, but continue reading to reach total_length
+ break;
+ }
+ // Include all characters in the name, including newlines if present
+ name[name_bytes++] = (tb_char_t)c;
+ }
+ name[name_bytes] = '\0';
+ *name_len = name_bytes;
+
+ // Skip remaining bytes to reach total_length (there may be padding or null terminators)
+ if (bytes_read_so_far < (tb_hize_t)total_length) {
+ tb_hize_t remaining_to_read = (tb_hize_t)total_length - bytes_read_so_far;
+ if (!tb_stream_skip(istream, remaining_to_read)) {
+ return tb_false;
+ }
+ }
+
+ // Total bytes read = name + padding = total_length
+ *bytes_read = (tb_hize_t)total_length;
+ return tb_true;
+ }
+ }
+
+ // regular name (null-terminated or space-padded)
+ tb_size_t i = 0;
+ for (i = 0; i < 16 && i < name_size - 1; i++) {
+ if (header->name[i] == ' ' || header->name[i] == '\0' || header->name[i] == '/') {
+ break;
+ }
+ name[i] = header->name[i];
+ }
+ name[i] = '\0';
+ *name_len = i;
+ *bytes_read = 0; // Regular names are in header, not read from stream
+ return tb_true;
+}
+
/* check AR magic (!<arch>\n)
*
* @param istream the input stream
@@ -100,4 +194,41 @@ static __tb_inline__ tb_bool_t xm_binutils_ar_check_magic(tb_stream_ref_t istrea
return tb_true;
}
+/* check if member is a symbol table (should be skipped)
+ *
+ * @param name the member name
+ * @return tb_true if it's a symbol table, tb_false otherwise
+ */
+static __tb_inline__ tb_bool_t xm_binutils_ar_is_symbol_table(tb_char_t const *name) {
+ tb_assert_and_check_return_val(name, tb_false);
+ return (tb_strcmp(name, "__.SYMDEF") == 0 || tb_strcmp(name, "__.SYMDEF SORTED") == 0 ||
+ tb_strcmp(name, "/") == 0 || tb_strcmp(name, "//") == 0 ||
+ tb_strncmp(name, "__.SYMDEF", 9) == 0);
+}
+
+/* check if member is an object file (based on extension)
+ *
+ * @param name the member name
+ * @return tb_true if it's likely an object file, tb_false otherwise
+ */
+static __tb_inline__ tb_bool_t xm_binutils_ar_is_object_file(tb_char_t const *name) {
+ tb_assert_and_check_return_val(name, tb_false);
+ tb_size_t len = tb_strlen(name);
+ if (len == 0) {
+ return tb_false;
+ }
+
+ // check common object file extensions
+ if (len >= 2 && name[len - 2] == '.' && name[len - 1] == 'o') {
+ return tb_true;
+ }
+ if (len >= 4 && tb_strcmp(name + len - 4, ".obj") == 0) {
+ return tb_true;
+ }
+
+ // check if it's a COFF/ELF/Mach-O file by detecting format
+ // For now, we'll extract all non-symbol-table members
+ return tb_true;
+}
+
#endif
diff --git a/core/src/xmake/binutils/ar/readsyms.c b/core/src/xmake/binutils/ar/readsyms.c
index 307b465df..a41bf2e33 100644
--- a/core/src/xmake/binutils/ar/readsyms.c
+++ b/core/src/xmake/binutils/ar/readsyms.c
@@ -31,130 +31,6 @@
* private implementation
*/
-/* get member name from AR header, handling extended names (#N/L format)
- *
- * @param istream the input stream
- * @param header the AR header
- * @param name output buffer for the name
- * @param name_size size of the name buffer
- * @param name_len output: actual name length
- * @param bytes_read output: total bytes read from stream (including newline, for extended names)
- * @return tb_true on success, tb_false on failure
- */
-static tb_bool_t xm_binutils_ar_get_member_name(tb_stream_ref_t istream, xm_ar_header_t const* header, tb_char_t* name, tb_size_t name_size, tb_size_t* name_len, tb_hize_t* bytes_read) {
- tb_assert_and_check_return_val(istream && header && name && name_size > 0 && name_len && bytes_read, tb_false);
- *bytes_read = 0;
-
- /* check for extended name format (#N/L or #1/N)
- * In BSD AR format:
- * - #1/N means name is directly after header, N is total length (including name)
- * - #N/L means name length is N, total length is L
- * - #1/N can also mean name is in long name table at offset 1
- * We'll try to read the name directly from stream first
- */
- if (header->name[0] == '#') {
- // find the '/' separator
- tb_size_t slash_pos = 0;
- for (tb_size_t i = 1; i < 16; i++) {
- if (header->name[i] == '/') {
- slash_pos = i;
- break;
- }
- }
-
- if (slash_pos > 0 && slash_pos < 16) {
- // parse the number before '/' (could be name length or offset)
- tb_int64_t first_num = xm_binutils_ar_parse_decimal(header->name + 1, slash_pos - 1);
- // parse the number after '/' (total length)
- tb_int64_t total_length = xm_binutils_ar_parse_decimal(header->name + slash_pos + 1, 16 - slash_pos - 1);
-
- if (first_num <= 0 || total_length <= 0) {
- return tb_false;
- }
-
- /* In BSD AR format, extended name is directly after header
- * The name data starts immediately after the header, no newline
- * Read exactly total_length bytes for the name section
- */
- tb_byte_t c;
- tb_size_t name_bytes = 0;
- tb_hize_t bytes_read_so_far = 0;
-
- // Read name characters until we hit null terminator or reach total_length
- while (bytes_read_so_far < (tb_hize_t)total_length && name_bytes < name_size - 1) {
- if (!tb_stream_bread(istream, &c, 1)) {
- return tb_false;
- }
- bytes_read_so_far++;
-
- if (c == '\0') {
- // Stop reading name at null terminator, but continue reading to reach total_length
- break;
- }
- // Include all characters in the name, including newlines if present
- name[name_bytes++] = (tb_char_t)c;
- }
- name[name_bytes] = '\0';
- *name_len = name_bytes;
-
- // Skip remaining bytes to reach total_length (there may be padding or null terminators)
- if (bytes_read_so_far < (tb_hize_t)total_length) {
- tb_hize_t remaining_to_read = (tb_hize_t)total_length - bytes_read_so_far;
- if (!tb_stream_skip(istream, remaining_to_read)) {
- return tb_false;
- }
- }
-
- // Total bytes read = name + padding = total_length
- *bytes_read = (tb_hize_t)total_length;
- return tb_true;
- }
- }
-
- // regular name (null-terminated or space-padded)
- tb_size_t i = 0;
- for (i = 0; i < 16 && i < name_size - 1; i++) {
- if (header->name[i] == ' ' || header->name[i] == '\0' || header->name[i] == '/') {
- break;
- }
- name[i] = header->name[i];
- }
- name[i] = '\0';
- *name_len = i;
- *bytes_read = 0; // Regular names are in header, not read from stream
- return tb_true;
-}
-
-/* check if member is a symbol table (should be skipped)
- *
- * @param name the member name
- * @return tb_true if it's a symbol table, tb_false otherwise
- */
-static __tb_inline__ tb_bool_t xm_binutils_ar_is_symbol_table(tb_char_t const* name) {
- tb_assert_and_check_return_val(name, tb_false);
- return (tb_strcmp(name, "__.SYMDEF") == 0 || tb_strcmp(name, "__.SYMDEF SORTED") == 0 ||
- tb_strcmp(name, "/") == 0 || tb_strcmp(name, "//") == 0 ||
- tb_strncmp(name, "__.SYMDEF", 9) == 0);
-}
-
-/* check if member is an object file (based on extension)
- *
- * @param name the member name
- * @return tb_true if it's likely an object file, tb_false otherwise
- */
-static __tb_inline__ tb_bool_t xm_binutils_ar_is_object_file(tb_char_t const* name) {
- tb_assert_and_check_return_val(name, tb_false);
- tb_size_t len = tb_strlen(name);
- if (len == 0) return tb_false;
-
- // check common object file extensions
- if (len >= 2 && name[len - 2] == '.' && name[len - 1] == 'o') return tb_true;
- if (len >= 4 && tb_strcmp(name + len - 4, ".obj") == 0) return tb_true;
-
- // check if it's a COFF/ELF/Mach-O file by detecting format
- // For now, we'll extract all non-symbol-table members
- return tb_true;
-}
/* //////////////////////////////////////////////////////////////////////////////////////
* implementation
@@ -178,7 +54,9 @@ static tb_bool_t xm_binutils_ar_parse_bsd_symdef(tb_stream_ref_t istream, tb_hiz
// read size of ranlib array
tb_uint32_t ranlib_size = 0;
- if (!tb_stream_bread_u32_le(istream, &ranlib_size)) return tb_false;
+ if (!tb_stream_bread_u32_le(istream, &ranlib_size)) {
+ return tb_false;
+ }
// sanity check
if (ranlib_size == 0 || ranlib_size >= member_size) {
@@ -194,8 +72,12 @@ static tb_bool_t xm_binutils_ar_parse_bsd_symdef(tb_stream_ref_t istream, tb_hiz
tb_uint32_t* ran_off = tb_nalloc_type(num_ranlibs, tb_uint32_t);
if (!ran_strx || !ran_off) {
- if (ran_strx) tb_free(ran_strx);
- if (ran_off) tb_free(ran_off);
+ if (ran_strx) {
+ tb_free(ran_strx);
+ }
+ if (ran_off) {
+ tb_free(ran_off);
+ }
tb_stream_seek(istream, start_pos);
return tb_false;
}
@@ -287,7 +169,9 @@ static tb_bool_t xm_binutils_ar_parse_sysv_symdef(tb_stream_ref_t istream, tb_hi
// read number of symbols
tb_uint32_t num_symbols = 0;
- if (!tb_stream_bread_u32_be(istream, &num_symbols)) return tb_false;
+ if (!tb_stream_bread_u32_be(istream, &num_symbols)) {
+ return tb_false;
+ }
// sanity check
if (num_symbols == 0 || num_symbols * 4 >= member_size) {
@@ -333,7 +217,9 @@ static tb_bool_t xm_binutils_ar_parse_sysv_symdef(tb_stream_ref_t istream, tb_hi
tb_char_t* end = strtab + strtab_size;
for (i = 0; i < num_symbols; i++) {
- if (p >= end) break;
+ if (p >= end) {
+ break;
+ }
tb_char_t* name = p;
tb_size_t len = tb_strlen(name);
@@ -451,7 +337,9 @@ tb_bool_t xm_binutils_ar_read_symbols(tb_stream_ref_t istream, tb_hize_t base_of
if (skip) {
// skip remaining data + padding using sequential read
tb_hize_t skip_size = (tb_hize_t)member_size - name_bytes_read;
- if (member_size % 2) skip_size++; // add padding
+ if (member_size % 2) {
+ skip_size++; // add padding
+ }
if (!tb_stream_skip(istream, skip_size)) {
ok = tb_false;
break;
@@ -515,7 +403,9 @@ tb_bool_t xm_binutils_ar_read_symbols(tb_stream_ref_t istream, tb_hize_t base_of
// skip to next member
tb_hize_t member_data_read = tb_stream_offset(istream) - current_pos;
tb_hize_t remaining_size = (tb_hize_t)member_size - name_bytes_read - member_data_read;
- if (member_size % 2) remaining_size++; // add padding
+ if (member_size % 2) {
+ remaining_size++; // add padding
+ }
if (remaining_size > 0) {
if (!tb_stream_skip(istream, remaining_size)) {
diff --git a/core/src/xmake/binutils/coff/bin2coff.c b/core/src/xmake/binutils/coff/bin2coff.c
index 2a09cd504..ceb984a5c 100644
--- a/core/src/xmake/binutils/coff/bin2coff.c
+++ b/core/src/xmake/binutils/coff/bin2coff.c
@@ -90,11 +90,7 @@ static tb_bool_t xm_binutils_bin2coff_dump(tb_stream_ref_t istream,
}
// 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] = '_';
- }
- }
+ xm_binutils_sanitize_symbol_name(symbol_name);
tb_snprintf(symbol_start, sizeof(symbol_start), "%s_start", symbol_name);
tb_snprintf(symbol_end, sizeof(symbol_end), "%s_end", symbol_name);
diff --git a/core/src/xmake/binutils/coff/readsyms.c b/core/src/xmake/binutils/coff/readsyms.c
index 705a543ce..f45524a61 100644
--- a/core/src/xmake/binutils/coff/readsyms.c
+++ b/core/src/xmake/binutils/coff/readsyms.c
@@ -165,7 +165,9 @@ tb_bool_t xm_binutils_coff_read_symbols(tb_stream_ref_t istream, tb_hize_t base_
// read symbol
xm_coff_symbol_t sym;
if (!tb_stream_bread(istream, (tb_byte_t*)&sym, sizeof(sym))) {
- if (sections) tb_free(sections);
+ if (sections) {
+ tb_free(sections);
+ }
return tb_false;
}
@@ -208,7 +210,9 @@ tb_bool_t xm_binutils_coff_read_symbols(tb_stream_ref_t istream, tb_hize_t base_
if (sym.naux > 0) {
sym_index += sym.naux;
if (!tb_stream_seek(istream, tb_stream_offset(istream) + sym.naux * 18)) {
- if (sections) tb_free(sections);
+ if (sections) {
+ tb_free(sections);
+ }
return tb_false;
}
}
diff --git a/core/src/xmake/binutils/elf/bin2elf.c b/core/src/xmake/binutils/elf/bin2elf.c
index e6a9e7998..ab93bcde2 100644
--- a/core/src/xmake/binutils/elf/bin2elf.c
+++ b/core/src/xmake/binutils/elf/bin2elf.c
@@ -74,11 +74,7 @@ static tb_bool_t xm_binutils_bin2elf_dump_32(tb_stream_ref_t istream,
}
// 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] = '_';
- }
- }
+ xm_binutils_sanitize_symbol_name(symbol_name);
tb_snprintf(symbol_start, sizeof(symbol_start), "%s_start", symbol_name);
tb_snprintf(symbol_end, sizeof(symbol_end), "%s_end", symbol_name);
@@ -378,11 +374,7 @@ static tb_bool_t xm_binutils_bin2elf_dump_64(tb_stream_ref_t istream,
}
// 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] = '_';
- }
- }
+ xm_binutils_sanitize_symbol_name(symbol_name);
tb_snprintf(symbol_start, sizeof(symbol_start), "%s_start", symbol_name);
tb_snprintf(symbol_end, sizeof(symbol_end), "%s_end", symbol_name);
diff --git a/core/src/xmake/binutils/macho/bin2macho.c b/core/src/xmake/binutils/macho/bin2macho.c
index a9068625a..8abd020e9 100644
--- a/core/src/xmake/binutils/macho/bin2macho.c
+++ b/core/src/xmake/binutils/macho/bin2macho.c
@@ -79,11 +79,7 @@ static tb_bool_t xm_binutils_bin2macho_dump_64(tb_stream_ref_t istream,
}
// 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] = '_';
- }
- }
+ xm_binutils_sanitize_symbol_name(symbol_name);
tb_snprintf(symbol_start, sizeof(symbol_start), "%s_start", symbol_name);
tb_snprintf(symbol_end, sizeof(symbol_end), "%s_end", symbol_name);
@@ -324,11 +320,7 @@ static tb_bool_t xm_binutils_bin2macho_dump_32(tb_stream_ref_t istream,
}
// 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] = '_';
- }
- }
+ xm_binutils_sanitize_symbol_name(symbol_name);
tb_snprintf(symbol_start, sizeof(symbol_start), "%s_start", symbol_name);
tb_snprintf(symbol_end, sizeof(symbol_end), "%s_end", symbol_name);
diff --git a/core/src/xmake/binutils/mslib/extractlib.c b/core/src/xmake/binutils/mslib/extractlib.c
index 9b9f7b587..f61c1f720 100644
--- a/core/src/xmake/binutils/mslib/extractlib.c
+++ b/core/src/xmake/binutils/mslib/extractlib.c
@@ -198,7 +198,9 @@ tb_bool_t xm_binutils_mslib_extract(tb_stream_ref_t istream, tb_char_t const *ou
*/
tb_size_t name_len = tb_strlen(member_name);
for (tb_size_t i = 0; i < name_len; i++) {
- if (member_name[i] == '\\') member_name[i] = '/';
+ if (member_name[i] == '\\') {
+ member_name[i] = '/';
+ }
}
// check output path length
@@ -206,8 +208,11 @@ tb_bool_t xm_binutils_mslib_extract(tb_stream_ref_t istream, tb_char_t const *ou
if (plain) {
// get filename only
tb_char_t const* name = tb_strrchr(member_name, '/');
- if (name) name++;
- else name = member_name;
+ if (name) {
+ name++;
+ } else {
+ name = member_name;
+ }
// check conflicts
tb_char_t output_name[512];
@@ -294,6 +299,8 @@ tb_bool_t xm_binutils_mslib_extract(tb_stream_ref_t istream, tb_char_t const *ou
}
}
- if (longnames) tb_free(longnames);
+ if (longnames) {
+ tb_free(longnames);
+ }
return ok;
}
diff --git a/core/src/xmake/binutils/mslib/prefix.h b/core/src/xmake/binutils/mslib/prefix.h
index 1ea9e40b2..521755fb2 100644
--- a/core/src/xmake/binutils/mslib/prefix.h
+++ b/core/src/xmake/binutils/mslib/prefix.h
@@ -31,6 +31,7 @@
*/
// MSVC lib header
+#include "tbox/prefix/packed.h"
typedef struct __xm_mslib_header_t {
tb_char_t name[16];
tb_char_t date[12];
@@ -39,7 +40,8 @@ typedef struct __xm_mslib_header_t {
tb_char_t mode[8];
tb_char_t size[10];
tb_char_t fmag[2];
-} xm_mslib_header_t;
+} __tb_packed__ xm_mslib_header_t;
+#include "tbox/prefix/packed.h"
/* //////////////////////////////////////////////////////////////////////////////////////
* interfaces
@@ -49,7 +51,9 @@ static __tb_inline__ tb_int64_t xm_binutils_mslib_parse_decimal(tb_char_t const
tb_assert_and_check_return_val(p && n > 0, -1);
tb_int64_t v = 0;
tb_char_t const* e = p + n;
- while (p < e && *p == ' ') p++;
+ while (p < e && *p == ' ') {
+ p++;
+ }
while (p < e && *p >= '0' && *p <= '9') {
v = v * 10 + (*p - '0');
p++;
@@ -59,7 +63,9 @@ static __tb_inline__ tb_int64_t xm_binutils_mslib_parse_decimal(tb_char_t const
static __tb_inline__ tb_bool_t xm_binutils_mslib_check_magic(tb_stream_ref_t istream) {
tb_char_t magic[8];
- if (!tb_stream_bread(istream, (tb_byte_t*)magic, 8)) return tb_false;
+ if (!tb_stream_bread(istream, (tb_byte_t*)magic, 8)) {
+ return tb_false;
+ }
return tb_strncmp(magic, "!<arch>\n", 8) == 0;
}
diff --git a/core/src/xmake/binutils/mslib/readsyms.c b/core/src/xmake/binutils/mslib/readsyms.c
index 188fda297..de9dc9d62 100644
--- a/core/src/xmake/binutils/mslib/readsyms.c
+++ b/core/src/xmake/binutils/mslib/readsyms.c
@@ -51,10 +51,14 @@ static tb_bool_t xm_binutils_mslib_parse_archive_symbols(tb_stream_ref_t istream
do {
// read number of members
tb_uint32_t num_members = 0;
- if (!tb_stream_bread_u32_le(istream, &num_members)) break;
+ if (!tb_stream_bread_u32_le(istream, &num_members)) {
+ break;
+ }
// sanity check
- if (num_members == 0 || num_members > 65536 || num_members * 4 >= member_size) break;
+ if (num_members == 0 || num_members > 65536 || num_members * 4 >= member_size) {
+ break;
+ }
// read offsets
offsets = tb_nalloc_type(num_members, tb_uint32_t);
@@ -62,24 +66,36 @@ static tb_bool_t xm_binutils_mslib_parse_archive_symbols(tb_stream_ref_t istream
tb_size_t i;
for (i = 0; i < num_members; i++) {
- if (!tb_stream_bread_u32_le(istream, &offsets[i])) break;
+ if (!tb_stream_bread_u32_le(istream, &offsets[i])) {
+ break;
+ }
+ }
+ if (i < num_members) {
+ break;
}
- if (i < num_members) break;
// read number of symbols
tb_uint32_t num_symbols = 0;
- if (!tb_stream_bread_u32_le(istream, &num_symbols)) break;
+ if (!tb_stream_bread_u32_le(istream, &num_symbols)) {
+ break;
+ }
- if (num_symbols == 0 || num_symbols > 1000000) break;
+ if (num_symbols == 0 || num_symbols > 1000000) {
+ break;
+ }
// read indices
indices = tb_nalloc_type(num_symbols, tb_uint16_t);
tb_check_break(indices);
for (i = 0; i < num_symbols; i++) {
- if (!tb_stream_bread_u16_le(istream, &indices[i])) break;
+ if (!tb_stream_bread_u16_le(istream, &indices[i])) {
+ break;
+ }
+ }
+ if (i < num_symbols) {
+ break;
}
- if (i < num_symbols) break;
// read string table
tb_hize_t current = tb_stream_offset(istream);
@@ -88,14 +104,18 @@ static tb_bool_t xm_binutils_mslib_parse_archive_symbols(tb_stream_ref_t istream
string_table = (tb_char_t*)tb_malloc_bytes((tb_size_t)string_table_size);
tb_check_break(string_table);
- if (!tb_stream_bread(istream, (tb_byte_t*)string_table, (tb_size_t)string_table_size)) break;
+ if (!tb_stream_bread(istream, (tb_byte_t*)string_table, (tb_size_t)string_table_size)) {
+ break;
+ }
// populate map
tb_char_t* p = string_table;
tb_char_t* end = string_table + string_table_size;
for (i = 0; i < num_symbols; i++) {
- if (p >= end) break;
+ if (p >= end) {
+ break;
+ }
tb_char_t* sym_name = p;
tb_size_t sym_len = tb_strlen(sym_name);
@@ -124,9 +144,15 @@ static tb_bool_t xm_binutils_mslib_parse_archive_symbols(tb_stream_ref_t istream
} while (0);
- if (offsets) tb_free(offsets);
- if (indices) tb_free(indices);
- if (string_table) tb_free(string_table);
+ if (offsets) {
+ tb_free(offsets);
+ }
+ if (indices) {
+ tb_free(indices);
+ }
+ if (string_table) {
+ tb_free(string_table);
+ }
if (!ok) {
tb_stream_seek(istream, start_pos);
@@ -368,7 +394,9 @@ tb_bool_t xm_binutils_mslib_read_symbols(tb_stream_ref_t istream, tb_hize_t base
}
}
- if (longnames) tb_free(longnames);
+ if (longnames) {
+ tb_free(longnames);
+ }
lua_remove(lua, map_idx);
return ok;
}
diff --git a/core/src/xmake/binutils/prefix.h b/core/src/xmake/binutils/prefix.h
index 0a0972c6c..492ed691c 100644
--- a/core/src/xmake/binutils/prefix.h
+++ b/core/src/xmake/binutils/prefix.h
@@ -145,7 +145,10 @@ static __tb_inline__ tb_int_t xm_binutils_detect_format(tb_stream_ref_t istream)
* @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_assert_and_check_return_val(istream && ostream, tb_false);
+ if (size == 0) {
+ return tb_true;
+ }
tb_byte_t data[TB_STREAM_BLOCK_MAXN];
tb_hize_t writ = 0;
@@ -167,5 +170,19 @@ static __tb_inline__ tb_bool_t xm_binutils_stream_copy(tb_stream_ref_t istream,
return tb_true;
}
+/* sanitize symbol name (replace non-alphanumeric characters with underscores)
+ *
+ * @param name the symbol name
+ */
+static __tb_inline__ void xm_binutils_sanitize_symbol_name(tb_char_t* name) {
+ tb_assert_and_check_return(name);
+ for (tb_size_t i = 0; name[i]; i++) {
+ if (!tb_isalpha(name[i]) && !tb_isdigit(name[i]) && name[i] != '_') {
+ name[i] = '_';
+ }
+ }
+}
+
+
#endif
diff --git a/core/src/xmake/binutils/readsyms.c b/core/src/xmake/binutils/readsyms.c
index 145488c93..407ba7399 100644
--- a/core/src/xmake/binutils/readsyms.c
+++ b/core/src/xmake/binutils/readsyms.c
@@ -120,9 +120,14 @@ tb_int_t xm_binutils_readsyms(lua_State *lua) {
// object name
lua_pushstring(lua, "objectfile");
tb_char_t const* name = tb_strrchr(objectfile, '/');
- if (!name) name = tb_strrchr(objectfile, '\\');
- if (!name) name = objectfile;
- else name++;
+ if (!name) {
+ name = tb_strrchr(objectfile, '\\');
+ }
+ if (!name) {
+ name = objectfile;
+ } else {
+ name++;
+ }
lua_pushstring(lua, name);
lua_settable(lua, -3);