diff options
| author | ruki <[email protected]> | 2025-12-07 00:22:25 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2025-12-07 17:15:59 +0800 |
| commit | 05b3a19bef94606fc56c60b1bd16a3a5bf70ca29 (patch) | |
| tree | d3afb6b79efe675f630ad13e91ed488e497e36f8 | |
| parent | 10bc661bedcd77f98993650a96f3fae476685bb3 (diff) | |
fix size again
| -rw-r--r-- | core/src/xmake/utils/bin2coff.c | 22 | ||||
| -rw-r--r-- | tests/projects/other/bin2obj/src/main.c | 8 |
2 files changed, 18 insertions, 12 deletions
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; } |
