From 98d5c2ad147330e7b18d4b2ee36eb26cb7e40a84 Mon Sep 17 00:00:00 2001 From: Mitya Selivanov Date: Wed, 31 Jul 2024 21:14:30 +0200 Subject: Fix num program headers --- bxgen.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/bxgen.c b/bxgen.c index 24c1f4e..27c37ce 100755 --- a/bxgen.c +++ b/bxgen.c @@ -2649,7 +2649,7 @@ i64 unit_write_in_memory( emit_unit(pool, codegen, unit, arch); - u16 num_program_headers = 4; + u16 num_program_headers = 5; i64 program_offset = bx_align(ELF_HEADER_SIZE + ELF_PROGRAM_HEADER_SIZE * num_program_headers, X86_64_ALIGNMENT); i64 base_address = X86_64_BASE_ADDRESS; @@ -3198,9 +3198,9 @@ i64 unit_write_in_memory( write_i64(LE, ELF_HEADER_SIZE, h + 32, o_end); // size in file write_i64(LE, ELF_HEADER_SIZE, h + 40, o_end); // size in memory write_i64(LE, X86_64_ALIGNMENT, h + 48, o_end); + h += ELF_PROGRAM_HEADER_SIZE; // r/x code - h += ELF_PROGRAM_HEADER_SIZE; write_u32(LE, 1, h, o_end); // type (PT_LOAD) write_u32(LE, 5, h + 4, o_end); // flags (PF_X | PF_R) write_i64(LE, rxcode_offset, h + 8, o_end); @@ -3209,9 +3209,9 @@ i64 unit_write_in_memory( write_i64(LE, rxcode_size, h + 32, o_end); // size in file write_i64(LE, rxcode_size, h + 40, o_end); // size in memory write_i64(LE, X86_64_ALIGNMENT, h + 48, o_end); + h += ELF_PROGRAM_HEADER_SIZE; // r/w zero values - h += ELF_PROGRAM_HEADER_SIZE; write_u32(LE, 1, h, o_end); // type (PT_LOAD) write_u32(LE, 6, h + 4, o_end); // flags (PF_R | PF_W) write_i64(LE, rwdata_offset, h + 8, o_end); @@ -3220,9 +3220,9 @@ i64 unit_write_in_memory( write_i64(LE, 0, h + 32, o_end); // size in file write_i64(LE, rwzval_size, h + 40, o_end); // size in memory write_i64(LE, X86_64_ALIGNMENT, h + 48, o_end); + h += ELF_PROGRAM_HEADER_SIZE; // r/w data - h += ELF_PROGRAM_HEADER_SIZE; write_u32(LE, 1, h, o_end); // type (PT_LOAD) write_u32(LE, 6, h + 4, o_end); // flags (PF_R | PF_W) write_i64(LE, rwdata_offset, h + 8, o_end); @@ -3231,9 +3231,9 @@ i64 unit_write_in_memory( write_i64(LE, rwdata_size, h + 32, o_end); // size in file write_i64(LE, rwdata_size, h + 40, o_end); // size in memory write_i64(LE, X86_64_ALIGNMENT, h + 48, o_end); + h += ELF_PROGRAM_HEADER_SIZE; // r/o data - h += ELF_PROGRAM_HEADER_SIZE; write_u32(LE, 1, h, o_end); // type (PT_LOAD) write_u32(LE, 4, h + 4, o_end); // flags (PF_R) write_i64(LE, rodata_offset, h + 8, o_end); @@ -3242,8 +3242,10 @@ i64 unit_write_in_memory( write_i64(LE, rodata_size, h + 32, o_end); // size in file write_i64(LE, rodata_size, h + 40, o_end); // size in memory write_i64(LE, X86_64_ALIGNMENT, h + 48, o_end); + h += ELF_PROGRAM_HEADER_SIZE; - CHECK(rxcode_offset >= 288, "Sanity",); + CHECK(h == o + ELF_HEADER_SIZE + num_program_headers * ELF_PROGRAM_HEADER_SIZE, "Sanity",); + CHECK(rxcode_offset >= h - o, "Sanity",); // Code // -- cgit v1.2.3