From 7667600fa4ba8f99e3560f53ec2db224fe4ef49b Mon Sep 17 00:00:00 2001 From: Mitya Selivanov Date: Wed, 17 Jul 2024 12:50:21 +0200 Subject: word and dword order --- bxgen.c | 523 +++++++++++++++++++++++++++++++++++----------------------------- 1 file changed, 283 insertions(+), 240 deletions(-) (limited to 'bxgen.c') diff --git a/bxgen.c b/bxgen.c index 28b1b2f..b87568a 100755 --- a/bxgen.c +++ b/bxgen.c @@ -124,57 +124,12 @@ typedef char c8; // 8-bit character // ================================================================ enum { - HOST_Unknown = 0, - HOST_Unix, - HOST_Linux, - HOST_Windows, - HOST_macOS, - HOST_Cygwin, - - #if defined(__CYGWIN__) - HOST = HOST_Cygwin, - #elif defined(_WIN32) - HOST = HOST_Windows, - #elif defined(__linux__) - HOST = HOST_Linux, - #elif defined(__APPLE__) - HOST = HOST_macOS, - #elif defined(__unix__) - HOST = HOST_Unix, - #else - HOST = HOST_Unknown, - #endif - // Log level ERROR = 0, // For indices UNDEFINED = -1, - // Formats - // - - FORMAT_ELF = 1, - FORMAT_COFF, - FORMAT_PE, - FORMAT_OMF, - FORMAT_MATCH_O, - - // Architecture - // - - ARCH_RISC_V = 64, - ARCH_I386, - ARCH_X86_64, - ARCH_ARM32, - ARCH_ARM64, - - // Endianness - // - - LE = 1, - BE = 2, - // Sea of Nodes flow type // @@ -842,35 +797,95 @@ void unit_set_entry_point(Pool *pool, i64 unit, i64 entry_point_proc) { // // * Serialization // +// ---------------------------------------------------------------- +// +// Terms +// +// LO = little endian +// BE = big endian +// +// byte = 8 bits +// word = 2 bytes +// dword = 4 bytes +// qword = 8 bytes +// // ================================================================ +enum { + BIT_LE = 0, + BIT_BE = 1, + BIT_ORDER_MASK = 1, + + BYTE_LE = 0, + BYTE_BE = 2, + BYTE_ORDER_MASK = 2, + + WORD_LE = 0, + WORD_BE = 4, + WORD_ORDER_MASK = 4, + + DWORD_LE = 0, + DWORD_BE = 8, + DWORD_ORDER_MASK = 8, + + F64_DWORD_LE = 0, + F64_DWORD_BE = 16, + F64_DWORD_ORDER_MASK = 16, + + LE = BIT_LE | BYTE_LE | WORD_LE | DWORD_LE | F64_DWORD_LE, + BE = BIT_BE | BYTE_BE | WORD_BE | DWORD_BE | F64_DWORD_BE, +}; + typedef struct { unsigned little:1; } Bits; -i32 host_bit_order() { +u32 host_bit_order() { if ((*(Bits *) &(u8) { 1 }).little == 1) - return LE; - return BE; + return BIT_LE; + return BIT_BE; } -i32 host_byte_order() { +u32 host_byte_order() { if (((u8 *) &(u32) { 1 })[0] == 1) - return LE; - return BE; + return BYTE_LE; + return BYTE_BE; +} + +u32 host_word_order() { + if (((u16 *) &(u32) { 0x100 })[0] == 0x100) + return WORD_LE; + return WORD_BE; } -i32 host_f64_dword_order() { +u32 host_dword_order() { + if (((u32 *) &(u64) { 0x10000 })[0] == 0x10000) + return DWORD_LE; + return DWORD_BE; +} + +u32 host_f64_dword_order() { if ((*(u64 *) &(f64) { -1.4575323640233e-306 } & 0xffffffffull) == 0x40301fcbull) - return host_byte_order(); + return host_dword_order() == DWORD_LE ? F64_DWORD_LE : F64_DWORD_BE; if ((*(u64 *) &(f64) { -1.4575323640233e-306 } & 0xffffffff00000000ull) == 0x40301fcb00000000ull) - return host_byte_order() == LE ? BE : LE; + return host_dword_order() == DWORD_LE ? F64_DWORD_BE : F64_DWORD_LE; + + BX_FAIL("Unknown host floating-point number format", 0); +} - BX_FAIL("Unknown native floating-point number format", 0); +u32 host_data_ordering() { + return host_bit_order() | + host_byte_order() | + host_word_order() | + host_dword_order() | + host_f64_dword_order(); } -u8 read_u8(i32 bit_order, u8 *v) { - if (bit_order == host_bit_order()) +u8 read_u8(u32 ordering, u8 *v, u8 *v_end) { + BX_CHECK(v != NULL, "Invalid arguments", 0); + BX_CHECK(v < v_end, "Invalid arguments", 0); + + if ((ordering & BIT_ORDER_MASK) == host_bit_order()) return *v; return @@ -884,80 +899,71 @@ u8 read_u8(i32 bit_order, u8 *v) { (((*v) & 1) << 7); } -u16 read_u16(i32 bit_order, i32 byte_order, u8 *v, u8 *v_end) { +u16 read_u16(u32 ordering, u8 *v, u8 *v_end) { BX_CHECK(v != NULL, "Invalid arguments", 0); BX_CHECK(v + 2 <= v_end, "Invalid arguments", 0); u16 x; - if (bit_order == host_bit_order() && byte_order == host_byte_order()) + if ((ordering & BIT_ORDER_MASK) == host_bit_order() && + (ordering & BYTE_ORDER_MASK) == host_byte_order()) bx_mem_cpy(&x, v, 2); - else if (byte_order == host_byte_order()) - x = ((u16) read_u8(bit_order, v)) | - (((u16) read_u8(bit_order, v + 1)) << 8); + else if ((ordering & BYTE_ORDER_MASK) == host_byte_order()) + x = ((u16) read_u8(ordering, v, v_end)) | + (((u16) read_u8(ordering, v + 1, v_end)) << 8); else - x = ((u16) read_u8(bit_order, v + 1)) | - (((u16) read_u8(bit_order, v)) << 8); + x = ((u16) read_u8(ordering, v + 1, v_end)) | + (((u16) read_u8(ordering, v, v_end)) << 8); return x; } -u32 read_u32(i32 bit_order, i32 byte_order, u8 *v, u8 *v_end) { +u32 read_u32(u32 ordering, u8 *v, u8 *v_end) { BX_CHECK(v != NULL, "Invalid arguments", 0); BX_CHECK(v + 4 <= v_end, "Invalid arguments", 0); u32 x; - if (bit_order == host_bit_order() && byte_order == host_byte_order()) + if ((ordering & BIT_ORDER_MASK) == host_bit_order() && + (ordering & BYTE_ORDER_MASK) == host_byte_order() && + (ordering & WORD_ORDER_MASK) == host_word_order()) bx_mem_cpy(&x, v, 4); - else if (byte_order == host_byte_order()) - x = ((u32) read_u8(bit_order, v)) | - (((u32) read_u8(bit_order, v + 1)) << 8) | - (((u32) read_u8(bit_order, v + 2)) << 16) | - (((u32) read_u8(bit_order, v + 3)) << 24); + else if ((ordering & WORD_ORDER_MASK) == host_word_order()) + x = ((u32) read_u16(ordering, v, v_end)) | + (((u32) read_u16(ordering, v + 2, v_end)) << 16); else - x = ((u32) read_u8(bit_order, v + 3)) | - (((u32) read_u8(bit_order, v + 2)) << 8) | - (((u32) read_u8(bit_order, v + 1)) << 16) | - (((u32) read_u8(bit_order, v)) << 24); + x = ((u32) read_u16(ordering, v + 2, v_end)) | + (((u32) read_u16(ordering, v, v_end)) << 16); return x; } -u64 read_u64(i32 bit_order, i32 byte_order, u8 *v, u8 *v_end) { +u64 read_u64(u32 ordering, u8 *v, u8 *v_end) { BX_CHECK(v != NULL, "Invalid arguments", 0); BX_CHECK(v + 8 <= v_end, "Invalid arguments", 0); u64 x; - if (bit_order == host_bit_order() && byte_order == host_byte_order()) + if ((ordering & BIT_ORDER_MASK) == host_bit_order() && + (ordering & BYTE_ORDER_MASK) == host_byte_order() && + (ordering & WORD_ORDER_MASK) == host_word_order() && + (ordering & DWORD_ORDER_MASK) == host_dword_order()) bx_mem_cpy(&x, v, 8); - else if (byte_order == host_byte_order()) - x = ((u64) read_u8(bit_order, v)) | - (((u64) read_u8(bit_order, v + 1)) << 8) | - (((u64) read_u8(bit_order, v + 2)) << 16) | - (((u64) read_u8(bit_order, v + 3)) << 24) | - (((u64) read_u8(bit_order, v + 4)) << 32) | - (((u64) read_u8(bit_order, v + 5)) << 40) | - (((u64) read_u8(bit_order, v + 6)) << 48) | - (((u64) read_u8(bit_order, v + 7)) << 56); + else if ((ordering & DWORD_ORDER_MASK) == host_dword_order()) + x = ((u64) read_u32(ordering, v, v_end)) | + (((u64) read_u32(ordering, v + 4, v_end)) << 32); else - x = ((u64) read_u8(bit_order, v + 7)) | - (((u64) read_u8(bit_order, v + 6)) << 8) | - (((u64) read_u8(bit_order, v + 5)) << 16) | - (((u64) read_u8(bit_order, v + 4)) << 24) | - (((u64) read_u8(bit_order, v + 3)) << 32) | - (((u64) read_u8(bit_order, v + 2)) << 40) | - (((u64) read_u8(bit_order, v + 1)) << 48) | - (((u64) read_u8(bit_order, v)) << 56); + x = ((u64) read_u32(ordering, v + 4, v_end)) | + (((u64) read_u32(ordering, v, v_end)) << 32); return x; } -void write_u8(i32 bit_order, u8 x, u8 *v) { +void write_u8(u8 ordering, u8 x, u8 *v, u8 *v_end) { BX_CHECK(v != NULL, "Invalid arguments",); + BX_CHECK(v < v_end, "Invalid arguments",); - if (bit_order == host_bit_order()) + if ((ordering & BIT_ORDER_MASK) == host_bit_order()) *v = x; else *v = @@ -971,125 +977,112 @@ void write_u8(i32 bit_order, u8 x, u8 *v) { (((x) & 1) << 7); } -void write_u16(i32 bit_order, i32 byte_order, u16 x, u8 *v, u8 *v_end) { +void write_u16(u32 ordering, u16 x, u8 *v, u8 *v_end) { BX_CHECK(v != NULL, "Invalid arguments",); BX_CHECK(v + 2 <= v_end, "Invalid arguments",); - if (bit_order == host_bit_order() && byte_order == host_byte_order()) + if ((ordering & BIT_ORDER_MASK) == host_bit_order() && + (ordering & BYTE_ORDER_MASK) == host_byte_order()) bx_mem_cpy(v, &x, 2); - else if (byte_order == host_byte_order()) { - write_u8(bit_order, (u8) ( x & 0xff), v); - write_u8(bit_order, (u8) ((x >> 8) & 0xff), v + 1); + else if ((ordering & BYTE_ORDER_MASK) == host_byte_order()) { + write_u8(ordering, (u8) ( x & 0xff), v, v_end); + write_u8(ordering, (u8) ((x >> 8) & 0xff), v + 1, v_end); } else { - write_u8(bit_order, (u8) ( x & 0xff), v + 1); - write_u8(bit_order, (u8) ((x >> 8) & 0xff), v); + write_u8(ordering, (u8) ( x & 0xff), v + 1, v_end); + write_u8(ordering, (u8) ((x >> 8) & 0xff), v, v_end); } } -void write_u32(i32 bit_order, i32 byte_order, u32 x, u8 *v, u8 *v_end) { +void write_u32(u32 ordering, u32 x, u8 *v, u8 *v_end) { BX_CHECK(v != NULL, "Invalid arguments",); BX_CHECK(v + 4 <= v_end, "Invalid arguments",); - if (bit_order == host_bit_order() && byte_order == host_byte_order()) + if ((ordering & BIT_ORDER_MASK) == host_bit_order() && + (ordering & BYTE_ORDER_MASK) == host_byte_order() && + (ordering & WORD_ORDER_MASK) == host_word_order()) bx_mem_cpy(v, &x, 4); - else if (byte_order == host_byte_order()) { - write_u8(bit_order, (u8) ( x & 0xff), v); - write_u8(bit_order, (u8) ((x >> 8) & 0xff), v + 1); - write_u8(bit_order, (u8) ((x >> 16) & 0xff), v + 2); - write_u8(bit_order, (u8) ((x >> 24) & 0xff), v + 3); + else if ((ordering & WORD_ORDER_MASK) == host_word_order()) { + write_u16(ordering, (u16) ( x & 0xffff), v, v_end); + write_u16(ordering, (u16) ((x >> 16) & 0xffff), v + 2, v_end); } else { - write_u8(bit_order, (u8) ( x & 0xff), v + 3); - write_u8(bit_order, (u8) ((x >> 8) & 0xff), v + 2); - write_u8(bit_order, (u8) ((x >> 16) & 0xff), v + 1); - write_u8(bit_order, (u8) ((x >> 24) & 0xff), v); + write_u16(ordering, (u16) ( x & 0xffff), v + 2, v_end); + write_u16(ordering, (u16) ((x >> 16) & 0xffff), v, v_end); } } -void write_u64(i32 bit_order, i32 byte_order, u64 x, u8 *v, u8 *v_end) { +void write_u64(u32 ordering, u64 x, u8 *v, u8 *v_end) { BX_CHECK(v != NULL, "Invalid arguments",); BX_CHECK(v + 8 <= v_end, "Invalid arguments",); - if (bit_order == host_bit_order() && byte_order == host_byte_order()) + if ((ordering & BIT_ORDER_MASK) == host_bit_order() && + (ordering & BYTE_ORDER_MASK) == host_byte_order() && + (ordering & WORD_ORDER_MASK) == host_word_order() && + (ordering & DWORD_ORDER_MASK) == host_dword_order()) bx_mem_cpy(v, &x, 8); - else if (byte_order == host_byte_order()) { - write_u8(bit_order, (u8) ( x & 0xff), v); - write_u8(bit_order, (u8) ((x >> 8) & 0xff), v + 1); - write_u8(bit_order, (u8) ((x >> 16) & 0xff), v + 2); - write_u8(bit_order, (u8) ((x >> 24) & 0xff), v + 3); - write_u8(bit_order, (u8) ((x >> 32) & 0xff), v + 4); - write_u8(bit_order, (u8) ((x >> 40) & 0xff), v + 5); - write_u8(bit_order, (u8) ((x >> 48) & 0xff), v + 6); - write_u8(bit_order, (u8) ((x >> 56) & 0xff), v + 7); + else if ((ordering & DWORD_ORDER_MASK) == host_dword_order()) { + write_u32(ordering, (u32) ( x & 0xffffffffull), v, v_end); + write_u32(ordering, (u32) ((x >> 32) & 0xffffffffull), v + 4, v_end); } else { - write_u8(bit_order, (u8) ( x & 0xff), v + 7); - write_u8(bit_order, (u8) ((x >> 8) & 0xff), v + 6); - write_u8(bit_order, (u8) ((x >> 16) & 0xff), v + 5); - write_u8(bit_order, (u8) ((x >> 24) & 0xff), v + 4); - write_u8(bit_order, (u8) ((x >> 32) & 0xff), v + 3); - write_u8(bit_order, (u8) ((x >> 40) & 0xff), v + 2); - write_u8(bit_order, (u8) ((x >> 48) & 0xff), v + 1); - write_u8(bit_order, (u8) ((x >> 56) & 0xff), v); + write_u32(ordering, (u32) ( x & 0xffffffffull), v + 4, v_end); + write_u32(ordering, (u32) ((x >> 16) & 0xffffffffull), v, v_end); } } -i16 read_i16(i32 bit_order, i32 byte_order, void *v, void *v_end) { - return (i16) read_u16(bit_order, byte_order, v, v_end); +i16 read_i16(u32 ordering, void *v, void *v_end) { + return (i16) read_u16(ordering, v, v_end); } -i32 read_i32(i32 bit_order, i32 byte_order, void *v, void *v_end) { - return (i32) read_u32(bit_order, byte_order, v, v_end); +i32 read_i32(u32 ordering, void *v, void *v_end) { + return (i32) read_u32(ordering, v, v_end); } -i64 read_i64(i32 bit_order, i32 byte_order, void *v, void *v_end) { - return (i64) read_u64(bit_order, byte_order, v, v_end); +i64 read_i64(u32 ordering, void *v, void *v_end) { + return (i64) read_u64(ordering, v, v_end); } -f32 read_f32(i32 bit_order, i32 byte_order, void *v, void *v_end) { +f32 read_f32(u32 ordering, void *v, void *v_end) { host_f64_dword_order(); // FIXME - return *(f32 *) &(u32) { read_u32(bit_order, byte_order, v, v_end) }; + return *(f32 *) &(u32) { read_u32(ordering, v, v_end) }; } -f64 read_f64(i32 bit_order, i32 byte_order, i32 f64_dword_order, void *v, void *v_end) { - u64 x = read_u64(bit_order, byte_order, v, v_end); +f64 read_f64(u32 ordering, void *v, void *v_end) { + u64 x = read_u64(ordering, v, v_end); - if (f64_dword_order != host_f64_dword_order()) + if ((ordering & F64_DWORD_ORDER_MASK) != host_f64_dword_order()) x = ((x & 0xffffffffull) << 32) | ((x >> 32) & 0xffffffffull); return *(f64 *) &x; } -void write_i16(i32 bit_order, i32 byte_order, i16 x, void *v, void *v_end) { - write_u16(bit_order, byte_order, (u16) x, v, v_end); +void write_i16(u32 ordering, i16 x, void *v, void *v_end) { + write_u16(ordering, (u16) x, v, v_end); } -void write_i32(i32 bit_order, i32 byte_order, i32 x, void *v, void *v_end) { - write_u32(bit_order, byte_order, (u32) x, v, v_end); +void write_i32(u32 ordering, i32 x, void *v, void *v_end) { + write_u32(ordering, (u32) x, v, v_end); } -void write_i64(i32 bit_order, i32 byte_order, i64 x, void *v, void *v_end) { - write_u64(bit_order, byte_order, (u64) x, v, v_end); +void write_i64(u32 ordering, i64 x, void *v, void *v_end) { + write_u64(ordering, (u64) x, v, v_end); } -void write_f32(i32 bit_order, i32 byte_order, f32 x, void *v, void *v_end) { +void write_f32(u32 ordering, f32 x, void *v, void *v_end) { host_f64_dword_order(); // FIXME - write_u32(bit_order, byte_order, *(u32 *) &x, v, v_end); + write_u32(ordering, *(u32 *) &x, v, v_end); } -void write_f64(i32 bit_order, i32 byte_order, i32 f64_dword_order, f64 x, void *v, void *v_end) { - if (f64_dword_order == host_f64_dword_order()) - write_u64(bit_order, byte_order, *(u64 *) &x, v, v_end); +void write_f64(u32 ordering, f64 x, void *v, void *v_end) { + if ((ordering & F64_DWORD_ORDER_MASK) == host_f64_dword_order()) + write_u64(ordering, *(u64 *) &x, v, v_end); else { - write_u32(bit_order, byte_order, *(((u32 *) &x) + 1), (u8 *) v, v_end); - write_u32(bit_order, byte_order, * (u32 *) &x, ((u8 *) v) + 4, v_end); + write_u32(ordering, *(((u32 *) &x) + 1), (u8 *) v, v_end); + write_u32(ordering, * (u32 *) &x, ((u8 *) v) + 4, v_end); } } -#define HO_u8 host_bit_order() -#define HO_u16 host_bit_order(), host_byte_order() -#define HO_u32 host_bit_order(), host_byte_order() -#define HO_u64 host_bit_order(), host_byte_order() -#define HO_f32 host_bit_order(), host_byte_order() -#define HO_f64 host_bit_order(), host_byte_order(), host_f64_dword_order() +// Shortcuts + +#define HO host_data_ordering() // ================================================================ // @@ -1130,6 +1123,87 @@ void write_f64(i32 bit_order, i32 byte_order, i32 f64_dword_order, f64 x, void * #include // TEMP #include // TEMP +enum { + HOST_Unknown = 0, + HOST_Unix, + HOST_Linux, + HOST_Windows, + HOST_macOS, + HOST_Cygwin, + + #if defined(__CYGWIN__) + HOST = HOST_Cygwin, + #elif defined(_WIN32) + HOST = HOST_Windows, + #elif defined(__linux__) + HOST = HOST_Linux, + #elif defined(__APPLE__) + HOST = HOST_macOS, + #elif defined(__unix__) + HOST = HOST_Unix, + #else + HOST = HOST_Unknown, + #endif + + // Formats + // + + FORMAT_ELF = 1, + FORMAT_COFF, + FORMAT_PE, + FORMAT_OMF, + FORMAT_MATCH_O, + + // Architecture + // + + ARCH_RISC_V = 64, + ARCH_I386, + ARCH_X86_64, + ARCH_ARM32, + ARCH_ARM64, + + // x86_64 constants + // + + X86_64_BASE_ADDRESS = 0x400000, + X86_64_ALIGNMENT = 8, + + // ELF format constants + // + + ELF_64 = 2, + ELF_2_LE = 1, + ELF_VERSION = 1, + ELF_SYS_V = 0, + ELF_LINUX = 3, + ELF_ABI_VERSION = 0, + ELF_RELOCATABLE = 1, + ELF_EXECUTABLE = 2, + ELF_X86_64 = 62, + + ELF_HEADER_SIZE = 64, + ELF_PROGRAM_HEADER_SIZE = 56, + ELF_SECTION_HEADER_SIZE = 64, + ELF_SYMBOL_ENTRY_SIZE = 24, + ELF_REL_ENTRY_SIZE = 16, + ELF_RELA_ENTRY_SIZE = 24, +}; + +c8 ELF_MAGIC[4] = "\x7f" "ELF"; +c8 AR_MAGIC[8] = "!\n"; +c8 AR_SYMBOL_TABLE[] = "/ "; +c8 AR_STRING_TABLE[] = "// "; + +c8 SECTION_TEXT[] = ".text"; +c8 SECTION_RELA_TEXT[] = ".rela.text"; +c8 SECTION_DATA[] = ".data"; +c8 SECTION_BSS[] = ".bss"; +c8 SECTION_RODATA[] = ".rodata"; +c8 SECTION_SYMTAB[] = ".symtab"; +c8 SECTION_STRTAB[] = ".strtab"; +c8 SECTION_SHSTRTAB[] = ".shstrtab"; + typedef struct { u64 offset; u64 size; @@ -1166,39 +1240,6 @@ typedef struct { i64 addent; } Rela_Entry; -c8 ELF_MAGIC[4] = "\x7f" "ELF"; -u8 ELF_64 = 2; -u8 ELF_2_LE = 1; -u8 ELF_VERSION = 1; -u8 ELF_SYS_V = 0; -u8 ELF_LINUX = 3; -u8 ELF_ABI_VERSION = 0; -u16 ELF_RELOCATABLE = 1; -u16 ELF_EXECUTABLE = 2; -u16 ELF_X86_64 = 62; - -u16 ELF_HEADER_SIZE = 64; -u16 ELF_PROGRAM_HEADER_SIZE = 56; -u16 ELF_SECTION_HEADER_SIZE = 64; -u16 ELF_SYMBOL_ENTRY_SIZE = 24; -u16 ELF_REL_ENTRY_SIZE = 16; -u16 ELF_RELA_ENTRY_SIZE = 24; - -u64 X86_64_BASE_ADDRESS = 0x400000; -u64 X86_64_ALIGNMENT = 8; -c8 AR_MAGIC[8] = "!\n"; -c8 AR_SYMBOL_TABLE[] = "/ "; -c8 AR_STRING_TABLE[] = "// "; - -c8 SECTION_TEXT[] = ".text"; -c8 SECTION_RELA_TEXT[] = ".rela.text"; -c8 SECTION_DATA[] = ".data"; -c8 SECTION_BSS[] = ".bss"; -c8 SECTION_RODATA[] = ".rodata"; -c8 SECTION_SYMTAB[] = ".symtab"; -c8 SECTION_STRTAB[] = ".strtab"; -c8 SECTION_SHSTRTAB[] = ".shstrtab"; - // ================================================================ u32 ar_find_symbol_offset_by_name(u8 *ar_symbol_table, u8 *ar_end, c8 *name, c8 *name_end) { @@ -1206,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(host_bit_order(), BE, ar_symbol_table, ar_end); + u32 count = read_u32((HO & ~BYTE_ORDER_MASK) | BYTE_BE, ar_symbol_table, ar_end); i64 len = name_end - name; @@ -1216,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(host_bit_order(), BE, ar_symbol_table + 4 * (index + 1), ar_end); + return read_u32((HO & ~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); @@ -1231,7 +1272,7 @@ u16 elf_section_names_table_index( u8 *elf_begin, u8 *elf_end ) { - return read_u16(HO_u16, elf_begin + 62, elf_end); + return read_u16(HO, elf_begin + 62, elf_end); } Offset_Count elf_section_headers( @@ -1239,8 +1280,8 @@ Offset_Count elf_section_headers( u8 *elf_end ) { return (Offset_Count) { - .offset = read_u64(HO_u64, elf_begin + 40, elf_end), - .count = read_u16(HO_u16, elf_begin + 60, elf_end), + .offset = read_u64(HO, elf_begin + 40, elf_end), + .count = read_u16(HO, elf_begin + 60, elf_end), }; } @@ -1261,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_u64, begin + 24, elf_end), - .size = read_u64(HO_u64, begin + 32, elf_end), + .offset = read_u64(HO, begin + 24, elf_end), + .size = read_u64(HO, begin + 32, elf_end), }; } @@ -1295,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_u32, begin, elf_end); + u32 name_offset = read_u32(HO, begin, elf_end); if (name_offset + name_size <= names.size && bx_mem_eq(elf_begin + names.offset + name_offset, name, name_size)) @@ -1312,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_u32, begin, elf_end); + u32 name_index = read_u32(HO, begin, elf_end); return (Section_Header) { .name = elf_name_in_string_table( @@ -1321,11 +1362,11 @@ Section_Header elf_section( names, name_index ), - .type = read_u32(HO_u32, begin + 4, elf_end), - .flags = read_u64(HO_u64, begin + 8, elf_end), + .type = read_u32(HO, begin + 4, elf_end), + .flags = read_u64(HO, begin + 8, elf_end), .data = { - .offset = read_u64(HO_u64, begin + 24, elf_end), - .size = read_u64(HO_u64, begin + 32, elf_end), + .offset = read_u64(HO, begin + 24, elf_end), + .size = read_u64(HO, begin + 32, elf_end), }, }; } @@ -1352,11 +1393,11 @@ Symbol_Entry elf_symbol( BX_CHECK(end <= elf_end, "Buffer overflow", (Symbol_Entry) {0}); - u32 sym_name = read_u32(HO_u32, begin, end); - u8 sym_info = read_u8 (HO_u8, begin + 4); - u16 sym_shndx = read_u16(HO_u16, begin + 6, end); - u64 sym_value = read_u64(HO_u64, begin + 8, end); - u64 sym_size = read_u64(HO_u64, begin + 16, end); + 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); Offset_Size dst = elf_section(elf_begin, elf_end, sym_shndx).data; @@ -1594,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_u8, elf_begin) == ELF_MAGIC[0], "Invalid ELF file",); - BX_CHECK(read_u8(HO_u8, elf_begin + 1) == ELF_MAGIC[1], "Invalid ELF file",); - BX_CHECK(read_u8(HO_u8, elf_begin + 2) == ELF_MAGIC[2], "Invalid ELF file",); - BX_CHECK(read_u8(HO_u8, elf_begin + 3) == ELF_MAGIC[3], "Invalid ELF file",); - BX_CHECK(read_u8(HO_u8, elf_begin + 4) == ELF_64, "Invalid ELF file",); - BX_CHECK(read_u8(HO_u8, elf_begin + 5) == ELF_2_LE, "Invalid ELF file",); - BX_CHECK(read_u8(HO_u8, elf_begin + 6) == ELF_VERSION, "Invalid ELF file",); + 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",); - u8 osabi = read_u8(HO_u8, elf_begin + 7); + u8 osabi = read_u8(HO, elf_begin + 7, elf_end); BX_CHECK(osabi == ELF_SYS_V || osabi == ELF_LINUX, "Invalid ELF file",); - BX_CHECK(read_u8(HO_u8, elf_begin + 8) == ELF_ABI_VERSION, "Invalid ELF file",); + BX_CHECK(read_u8(HO, elf_begin + 8, elf_end) == ELF_ABI_VERSION, "Invalid ELF file",); - BX_CHECK(read_u16(HO_u16, elf_begin + 16, elf_end) == ELF_RELOCATABLE, "Invalid ELF file",); - BX_CHECK(read_u16(HO_u16, elf_begin + 18, elf_end) == ELF_X86_64, "Invalid ELF file",); - BX_CHECK(read_u32(HO_u32, elf_begin + 20, elf_end) == ELF_VERSION, "Invalid ELF file",); - BX_CHECK(read_u64(HO_u64, elf_begin + 24, elf_end) == 0, "Invalid ELF file",); // entry - BX_CHECK(read_u64(HO_u64, elf_begin + 32, elf_end) == 0, "Invalid ELF file",); // program header offset + 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 - section_header_offset = read_u64(HO_u64, elf_begin + 40, elf_end); + section_header_offset = read_u64(HO, elf_begin + 40, elf_end); - BX_CHECK(read_u32(HO_u32, elf_begin + 48, elf_end) == 0, "Invalid ELF file",); // flags - BX_CHECK(read_u16(HO_u16, elf_begin + 52, elf_end) == ELF_HEADER_SIZE, "Invalid ELF file",); - BX_CHECK(read_u16(HO_u16, elf_begin + 54, elf_end) == 0, "Invalid ELF file",); // program header size - BX_CHECK(read_u16(HO_u16, elf_begin + 56, elf_end) == 0, "Invalid ELF file",); // program header count - BX_CHECK(read_u16(HO_u16, elf_begin + 58, elf_end) == ELF_SECTION_HEADER_SIZE, "Invalid ELF file",); + 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",); - section_count = read_u16(HO_u16, elf_begin + 60, elf_end); - string_table_index = read_u16(HO_u16, elf_begin + 62, elf_end); + section_count = read_u16(HO, elf_begin + 60, elf_end); + string_table_index = read_u16(HO, elf_begin + 62, elf_end); - string_table_offset = read_u64(HO_u64, elf_begin + section_header_offset + string_table_index * ELF_SECTION_HEADER_SIZE + 24, elf_end); + string_table_offset = read_u64(HO, elf_begin + section_header_offset + string_table_index * ELF_SECTION_HEADER_SIZE + 24, elf_end); (void) ar_symbol_table; (void) ar_string_table; @@ -2612,9 +2653,11 @@ int main(int argc, char **argv) { (void) argc; (void) argv; - printf("host bit order: %s\n", host_bit_order() == LE ? "LE" : "BE"); - printf("host byte order: %s\n", host_byte_order() == LE ? "LE" : "BE"); - printf("host f64 dword order: %s\n\n", host_f64_dword_order() == LE ? "LE" : "BE"); + printf("host bit order: %s\n", host_bit_order() == BIT_LE ? "LE" : "BE"); + printf("host byte order: %s\n", host_byte_order() == BYTE_LE ? "LE" : "BE"); + printf("host word order: %s\n", host_word_order() == WORD_LE ? "LE" : "BE"); + printf("host dword order: %s\n", host_dword_order() == DWORD_LE ? "LE" : "BE"); + printf("host f64 dword order: %s\n\n", host_f64_dword_order() == F64_DWORD_LE ? "LE" : "BE"); printf("entity - %d bytes\n", (i32) sizeof(Entity)); -- cgit v1.2.3