From 9baf560a2b48912e905fc2ff1801d00080875198 Mon Sep 17 00:00:00 2001 From: Mitya Selivanov Date: Wed, 17 Jul 2024 12:53:21 +0200 Subject: word and dword order --- bxgen.c | 84 +++++++++++++++++++++++++++++++++-------------------------------- 1 file changed, 43 insertions(+), 41 deletions(-) (limited to 'bxgen.c') diff --git a/bxgen.c b/bxgen.c index b87568a..ea1cdfe 100755 --- a/bxgen.c +++ b/bxgen.c @@ -1247,7 +1247,7 @@ u32 ar_find_symbol_offset_by_name(u8 *ar_symbol_table, u8 *ar_end, c8 *name, c8 BX_CHECK(name != NULL, "Invalid arguments", -1); BX_CHECK(name_end > name, "Invalid arguments", -1); - u32 count = read_u32((HO & ~BYTE_ORDER_MASK) | BYTE_BE, ar_symbol_table, ar_end); + u32 count = read_u32((LE & ~BYTE_ORDER_MASK) | BYTE_BE, ar_symbol_table, ar_end); i64 len = name_end - name; @@ -1257,7 +1257,7 @@ u32 ar_find_symbol_offset_by_name(u8 *ar_symbol_table, u8 *ar_end, c8 *name, c8 for (; index < count; ++index) { BX_CHECK(s + len <= (c8 *) ar_end, "Buffer overflow", -1); if (s[len] == '\0' && bx_mem_eq(s, name, len)) - return read_u32((HO & ~BYTE_ORDER_MASK) | BYTE_BE, ar_symbol_table + 4 * (index + 1), ar_end); + return read_u32((LE & ~BYTE_ORDER_MASK) | BYTE_BE, ar_symbol_table + 4 * (index + 1), ar_end); while (*s != '\0' && s < (c8 *) ar_end) ++s; BX_CHECK(s < (c8 *) ar_end, "Buffer overflow", -1); @@ -1272,7 +1272,7 @@ u16 elf_section_names_table_index( u8 *elf_begin, u8 *elf_end ) { - return read_u16(HO, elf_begin + 62, elf_end); + return read_u16(LE, elf_begin + 62, elf_end); } Offset_Count elf_section_headers( @@ -1280,8 +1280,8 @@ Offset_Count elf_section_headers( u8 *elf_end ) { return (Offset_Count) { - .offset = read_u64(HO, elf_begin + 40, elf_end), - .count = read_u16(HO, elf_begin + 60, elf_end), + .offset = read_u64(LE, elf_begin + 40, elf_end), + .count = read_u16(LE, elf_begin + 60, elf_end), }; } @@ -1302,8 +1302,8 @@ Offset_Size elf_section_names_data( u8 *begin = elf_begin + elf_section_header_offset(elf_begin, elf_end, string_table_index); return (Offset_Size) { - .offset = read_u64(HO, begin + 24, elf_end), - .size = read_u64(HO, begin + 32, elf_end), + .offset = read_u64(LE, begin + 24, elf_end), + .size = read_u64(LE, begin + 32, elf_end), }; } @@ -1336,7 +1336,7 @@ u16 elf_find_section_index_by_name( for (u16 i = 0; i < headers.count; ++i) { u8 *begin = elf_begin + headers.offset + i * ELF_SECTION_HEADER_SIZE; - u32 name_offset = read_u32(HO, begin, elf_end); + u32 name_offset = read_u32(LE, begin, elf_end); if (name_offset + name_size <= names.size && bx_mem_eq(elf_begin + names.offset + name_offset, name, name_size)) @@ -1353,7 +1353,7 @@ Section_Header elf_section( ) { Offset_Size names = elf_section_names_data(elf_begin, elf_end); u8 * begin = elf_begin + elf_section_header_offset(elf_begin, elf_end, index); - u32 name_index = read_u32(HO, begin, elf_end); + u32 name_index = read_u32(LE, begin, elf_end); return (Section_Header) { .name = elf_name_in_string_table( @@ -1362,11 +1362,11 @@ Section_Header elf_section( names, name_index ), - .type = read_u32(HO, begin + 4, elf_end), - .flags = read_u64(HO, begin + 8, elf_end), + .type = read_u32(LE, begin + 4, elf_end), + .flags = read_u64(LE, begin + 8, elf_end), .data = { - .offset = read_u64(HO, begin + 24, elf_end), - .size = read_u64(HO, begin + 32, elf_end), + .offset = read_u64(LE, begin + 24, elf_end), + .size = read_u64(LE, begin + 32, elf_end), }, }; } @@ -1393,11 +1393,11 @@ Symbol_Entry elf_symbol( BX_CHECK(end <= elf_end, "Buffer overflow", (Symbol_Entry) {0}); - u32 sym_name = read_u32(HO, begin, end); - u8 sym_info = read_u8 (HO, begin + 4, end); - u16 sym_shndx = read_u16(HO, begin + 6, end); - u64 sym_value = read_u64(HO, begin + 8, end); - u64 sym_size = read_u64(HO, begin + 16, end); + u32 sym_name = read_u32(LE, begin, end); + u8 sym_info = read_u8 (LE, begin + 4, end); + u16 sym_shndx = read_u16(LE, begin + 6, end); + u64 sym_value = read_u64(LE, begin + 8, end); + u64 sym_size = read_u64(LE, begin + 16, end); Offset_Size dst = elf_section(elf_begin, elf_end, sym_shndx).data; @@ -1635,36 +1635,36 @@ void unit_write(Pool *pool, i64 unit, u16 target, i64 io_out, void *io_user_data u16 string_table_index; u16 string_table_offset; - BX_CHECK(read_u8(HO, elf_begin, elf_end) == ELF_MAGIC[0], "Invalid ELF file",); - BX_CHECK(read_u8(HO, elf_begin + 1, elf_end) == ELF_MAGIC[1], "Invalid ELF file",); - BX_CHECK(read_u8(HO, elf_begin + 2, elf_end) == ELF_MAGIC[2], "Invalid ELF file",); - BX_CHECK(read_u8(HO, elf_begin + 3, elf_end) == ELF_MAGIC[3], "Invalid ELF file",); - BX_CHECK(read_u8(HO, elf_begin + 4, elf_end) == ELF_64, "Invalid ELF file",); - BX_CHECK(read_u8(HO, elf_begin + 5, elf_end) == ELF_2_LE, "Invalid ELF file",); - BX_CHECK(read_u8(HO, elf_begin + 6, elf_end) == ELF_VERSION, "Invalid ELF file",); + BX_CHECK(read_u8(LE, elf_begin, elf_end) == ELF_MAGIC[0], "Invalid ELF file",); + BX_CHECK(read_u8(LE, elf_begin + 1, elf_end) == ELF_MAGIC[1], "Invalid ELF file",); + BX_CHECK(read_u8(LE, elf_begin + 2, elf_end) == ELF_MAGIC[2], "Invalid ELF file",); + BX_CHECK(read_u8(LE, elf_begin + 3, elf_end) == ELF_MAGIC[3], "Invalid ELF file",); + BX_CHECK(read_u8(LE, elf_begin + 4, elf_end) == ELF_64, "Invalid ELF file",); + BX_CHECK(read_u8(LE, elf_begin + 5, elf_end) == ELF_2_LE, "Invalid ELF file",); + BX_CHECK(read_u8(LE, elf_begin + 6, elf_end) == ELF_VERSION, "Invalid ELF file",); - u8 osabi = read_u8(HO, elf_begin + 7, elf_end); + u8 osabi = read_u8(LE, elf_begin + 7, elf_end); BX_CHECK(osabi == ELF_SYS_V || osabi == ELF_LINUX, "Invalid ELF file",); - BX_CHECK(read_u8(HO, elf_begin + 8, elf_end) == ELF_ABI_VERSION, "Invalid ELF file",); + BX_CHECK(read_u8(LE, elf_begin + 8, elf_end) == ELF_ABI_VERSION, "Invalid ELF file",); - BX_CHECK(read_u16(HO, elf_begin + 16, elf_end) == ELF_RELOCATABLE, "Invalid ELF file",); - BX_CHECK(read_u16(HO, elf_begin + 18, elf_end) == ELF_X86_64, "Invalid ELF file",); - BX_CHECK(read_u32(HO, elf_begin + 20, elf_end) == ELF_VERSION, "Invalid ELF file",); - BX_CHECK(read_u64(HO, elf_begin + 24, elf_end) == 0, "Invalid ELF file",); // entry - BX_CHECK(read_u64(HO, elf_begin + 32, elf_end) == 0, "Invalid ELF file",); // program header offset + BX_CHECK(read_u16(LE, elf_begin + 16, elf_end) == ELF_RELOCATABLE, "Invalid ELF file",); + BX_CHECK(read_u16(LE, elf_begin + 18, elf_end) == ELF_X86_64, "Invalid ELF file",); + BX_CHECK(read_u32(LE, elf_begin + 20, elf_end) == ELF_VERSION, "Invalid ELF file",); + BX_CHECK(read_u64(LE, elf_begin + 24, elf_end) == 0, "Invalid ELF file",); // entry + BX_CHECK(read_u64(LE, elf_begin + 32, elf_end) == 0, "Invalid ELF file",); // program header offset - section_header_offset = read_u64(HO, elf_begin + 40, elf_end); + section_header_offset = read_u64(LE, elf_begin + 40, elf_end); - BX_CHECK(read_u32(HO, elf_begin + 48, elf_end) == 0, "Invalid ELF file",); // flags - BX_CHECK(read_u16(HO, elf_begin + 52, elf_end) == ELF_HEADER_SIZE, "Invalid ELF file",); - BX_CHECK(read_u16(HO, elf_begin + 54, elf_end) == 0, "Invalid ELF file",); // program header size - BX_CHECK(read_u16(HO, elf_begin + 56, elf_end) == 0, "Invalid ELF file",); // program header count - BX_CHECK(read_u16(HO, elf_begin + 58, elf_end) == ELF_SECTION_HEADER_SIZE, "Invalid ELF file",); + BX_CHECK(read_u32(LE, elf_begin + 48, elf_end) == 0, "Invalid ELF file",); // flags + BX_CHECK(read_u16(LE, elf_begin + 52, elf_end) == ELF_HEADER_SIZE, "Invalid ELF file",); + BX_CHECK(read_u16(LE, elf_begin + 54, elf_end) == 0, "Invalid ELF file",); // program header size + BX_CHECK(read_u16(LE, elf_begin + 56, elf_end) == 0, "Invalid ELF file",); // program header count + BX_CHECK(read_u16(LE, elf_begin + 58, elf_end) == ELF_SECTION_HEADER_SIZE, "Invalid ELF file",); - section_count = read_u16(HO, elf_begin + 60, elf_end); - string_table_index = read_u16(HO, elf_begin + 62, elf_end); + section_count = read_u16(LE, elf_begin + 60, elf_end); + string_table_index = read_u16(LE, elf_begin + 62, elf_end); - string_table_offset = read_u64(HO, elf_begin + section_header_offset + string_table_index * ELF_SECTION_HEADER_SIZE + 24, elf_end); + string_table_offset = read_u64(LE, elf_begin + section_header_offset + string_table_index * ELF_SECTION_HEADER_SIZE + 24, elf_end); (void) ar_symbol_table; (void) ar_string_table; @@ -2675,6 +2675,8 @@ int main(int argc, char **argv) { printf("Writing ELF x86_64 executable...\n"); u_elf_x86_64(u, "test_foo"); + BX_CHECK(HO == LE, "Host data ordering is not compatible", -1); + i32 ret = system("./test_foo"); BX_CHECK(WEXITSTATUS(ret) == 42, "Failure", -1); -- cgit v1.2.3