summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xbxgen.c80
1 files changed, 38 insertions, 42 deletions
diff --git a/bxgen.c b/bxgen.c
index 7e7440e..42b4058 100755
--- a/bxgen.c
+++ b/bxgen.c
@@ -244,12 +244,6 @@ enum {
CONV_FASTCALL,
CONV_THISCALL,
- // Primitive data types
- //
-
- TYPE_PTR = 0,
- TYPE_I32,
-
// Unit types
//
@@ -2572,9 +2566,9 @@ i64 unit_write_in_memory(
i64 rx_code_address = base_address + program_offset;
i64 rx_code_size = codegen->offset_code;
- i64 rw_zval_size = 0;
- i64 rw_data_size = 0;
i64 ro_data_size = codegen->offset_ro_data;
+ i64 rw_data_size = 0;
+ i64 rw_zval_size = 0;
i64 rw_tls_data_size = 0;
i64 rw_tls_zval_size = 0;
i64 rw_got_size = ELF_GOT_ENTRY_SIZE;
@@ -2673,16 +2667,16 @@ i64 unit_write_in_memory(
}
}
- i64 rw_zval_address = align(rx_code_address + rx_code_size, X86_64_PAGE_SIZE);
- i64 rw_data_address = align(rw_zval_address + rw_zval_size, X86_64_PAGE_SIZE);
- i64 ro_data_address = align(rw_data_address + rw_data_size, X86_64_PAGE_SIZE);
- i64 rw_tls_data_address = align(ro_data_address + ro_data_size, X86_64_PAGE_SIZE);
+ i64 ro_data_address = align(rx_code_address + rx_code_size, X86_64_PAGE_SIZE);
+ i64 rw_data_address = align(ro_data_address + ro_data_size, X86_64_PAGE_SIZE);
+ i64 rw_zval_address = align(rw_data_address + rw_data_size, X86_64_PAGE_SIZE);
+ i64 rw_tls_data_address = align(rw_zval_address + rw_zval_size, X86_64_PAGE_SIZE);
i64 rw_tls_zval_address = align(rw_tls_data_address + rw_tls_data_size, X86_64_PAGE_SIZE);
i64 rx_code_offset = program_offset;
- i64 rw_data_offset = align(rx_code_offset + rx_code_size, X86_64_PAGE_SIZE);
- i64 ro_data_offset = align(rw_data_offset + rw_data_size, X86_64_PAGE_SIZE);
- i64 rw_tls_data_offset = align(ro_data_offset + ro_data_size, X86_64_PAGE_SIZE);
+ i64 ro_data_offset = align(rx_code_offset + rx_code_size, X86_64_PAGE_SIZE);
+ i64 rw_data_offset = align(ro_data_offset + ro_data_size, X86_64_PAGE_SIZE);
+ i64 rw_tls_data_offset = align(rw_data_offset + rw_data_size, X86_64_PAGE_SIZE);
for (i64 elf_index = 0, sec_index_global = 0; elf_index < linker->num_obj_files; ++elf_index) {
Buffer_Context buf = elf_buffer_context(pool, linker, linker->num_obj_files, elf_index);
@@ -2967,11 +2961,11 @@ i64 unit_write_in_memory(
sec_index_global += num_sections - 1;
}
- i64 rw_got_address = align(ro_data_address + ro_data_size, X86_64_PAGE_SIZE);
- i64 rw_dynamic_address = align(rw_got_address + rw_got_size, X86_64_PAGE_SIZE);
+ i64 rw_got_address = align(rw_tls_zval_address + rw_tls_zval_size, X86_64_PAGE_SIZE);
+ i64 rw_dynamic_address = align(rw_got_address + rw_got_size, X86_64_PAGE_SIZE);
- i64 rw_got_offset = align(ro_data_offset + ro_data_size, X86_64_PAGE_SIZE);
- i64 rw_dynamic_offset = align(rw_got_offset + rw_got_size, X86_64_PAGE_SIZE);
+ i64 rw_got_offset = align(rw_tls_data_offset + rw_tls_data_size, X86_64_PAGE_SIZE);
+ i64 rw_dynamic_offset = align(rw_got_offset + rw_got_size, X86_64_PAGE_SIZE);
linker->symbols[sym_index_got] .address = rw_got_address;
linker->symbols[sym_index_got] .size = rw_got_size;
@@ -3281,14 +3275,16 @@ i64 unit_write_in_memory(
// Program headers
//
- CHECK(rx_code_offset % X86_64_PAGE_SIZE == rx_code_address % X86_64_PAGE_SIZE, "Invalid alignment",);
- CHECK(rw_data_offset % X86_64_PAGE_SIZE == rw_data_address % X86_64_PAGE_SIZE, "Invalid alignment",);
- CHECK(ro_data_offset % X86_64_PAGE_SIZE == ro_data_address % X86_64_PAGE_SIZE, "Invalid alignment",);
- CHECK(rw_got_offset % X86_64_PAGE_SIZE == rw_got_address % X86_64_PAGE_SIZE, "Invalid alignemtn",);
- CHECK(rw_dynamic_offset % X86_64_PAGE_SIZE == rw_dynamic_address % X86_64_PAGE_SIZE, "Invalid alignemtn",);
+ CHECK(rx_code_offset % X86_64_PAGE_SIZE == rx_code_address % X86_64_PAGE_SIZE, "Invalid alignment",);
+ CHECK(rw_data_offset % X86_64_PAGE_SIZE == rw_data_address % X86_64_PAGE_SIZE, "Invalid alignment",);
+ CHECK(ro_data_offset % X86_64_PAGE_SIZE == ro_data_address % X86_64_PAGE_SIZE, "Invalid alignment",);
+ CHECK(rw_tls_data_offset % X86_64_PAGE_SIZE == rw_tls_data_address % X86_64_PAGE_SIZE, "Invalid alignment",);
+ CHECK(rw_got_offset % X86_64_PAGE_SIZE == rw_got_address % X86_64_PAGE_SIZE, "Invalid alignemtn",);
+ CHECK(rw_dynamic_offset % X86_64_PAGE_SIZE == rw_dynamic_address % X86_64_PAGE_SIZE, "Invalid alignemtn",);
- // r/o elf header
u8 *h = o + ELF_HEADER_SIZE;
+
+ // r/o elf header
write_u32(LE, 1, h, o_end); // type (PT_LOAD)
write_u32(LE, 4, h + 4, o_end); // flags (PF_R)
write_i64(LE, 0, h + 8, o_end);
@@ -3310,14 +3306,14 @@ i64 unit_write_in_memory(
write_i64(LE, X86_64_ALIGNMENT, h + 48, o_end);
h += ELF_PROGRAM_HEADER_SIZE;
- // r/w zero values
+ // r/o data
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, rw_data_offset, h + 8, o_end);
- write_i64(LE, rw_zval_address, h + 16, o_end); // virtual address
- write_i64(LE, rw_zval_address, h + 24, o_end); // phisical address
- write_i64(LE, 0, h + 32, o_end); // size in file
- write_i64(LE, rw_zval_size, h + 40, o_end); // size in memory
+ write_u32(LE, 4, h + 4, o_end); // flags (PF_R)
+ write_i64(LE, ro_data_offset, h + 8, o_end);
+ write_i64(LE, ro_data_address, h + 16, o_end); // virtual address
+ write_i64(LE, ro_data_address, h + 24, o_end); // phisical address
+ write_i64(LE, ro_data_size, h + 32, o_end); // size in file
+ write_i64(LE, ro_data_size, h + 40, o_end); // size in memory
write_i64(LE, X86_64_ALIGNMENT, h + 48, o_end);
h += ELF_PROGRAM_HEADER_SIZE;
@@ -3332,26 +3328,26 @@ i64 unit_write_in_memory(
write_i64(LE, X86_64_ALIGNMENT, h + 48, o_end);
h += ELF_PROGRAM_HEADER_SIZE;
- // r/o data
+ // r/w zero values
write_u32(LE, 1, h, o_end); // type (PT_LOAD)
- write_u32(LE, 4, h + 4, o_end); // flags (PF_R)
- write_i64(LE, ro_data_offset, h + 8, o_end);
- write_i64(LE, ro_data_address, h + 16, o_end); // virtual address
- write_i64(LE, ro_data_address, h + 24, o_end); // phisical address
- write_i64(LE, ro_data_size, h + 32, o_end); // size in file
- write_i64(LE, ro_data_size, h + 40, o_end); // size in memory
+ write_u32(LE, 6, h + 4, o_end); // flags (PF_R | PF_W)
+ write_i64(LE, rw_got_offset, h + 8, o_end);
+ write_i64(LE, rw_zval_address, h + 16, o_end); // virtual address
+ write_i64(LE, rw_zval_address, h + 24, o_end); // phisical address
+ write_i64(LE, 0, h + 32, o_end); // size in file
+ write_i64(LE, rw_zval_size, h + 40, o_end); // size in memory
write_i64(LE, X86_64_ALIGNMENT, h + 48, o_end);
h += ELF_PROGRAM_HEADER_SIZE;
// TLS segment
- i64 tls_size = rw_tls_zval_size + rw_tls_data_size;
+ i64 tls_size_total = rw_tls_zval_size + rw_tls_data_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, rw_tls_data_offset, h + 8, o_end);
write_i64(LE, rw_tls_data_address, h + 16, o_end); // virtual address
write_i64(LE, rw_tls_data_address, h + 24, o_end); // phisical address
write_i64(LE, rw_tls_data_size, h + 32, o_end); // size in file
- write_i64(LE, tls_size, h + 40, o_end); // size in memory
+ write_i64(LE, tls_size_total, h + 40, o_end); // size in memory
write_i64(LE, X86_64_ALIGNMENT, h + 48, o_end);
h += ELF_PROGRAM_HEADER_SIZE;
@@ -3369,7 +3365,7 @@ i64 unit_write_in_memory(
// r/w TLS zero values
write_u32(LE, 7, h, o_end); // type (PT_TLS)
write_u32(LE, 6, h + 4, o_end); // flags (PF_R | PF_W)
- write_i64(LE, rw_got_offset, h + 8, o_end);
+ write_i64(LE, output_size, h + 8, o_end);
write_i64(LE, rw_tls_zval_address, h + 16, o_end); // virtual address
write_i64(LE, rw_tls_zval_address, h + 24, o_end); // phisical address
write_i64(LE, 0, h + 32, o_end); // size in file