diff options
author | Mitya Selivanov <automainint@guattari.tech> | 2024-07-31 22:38:49 +0200 |
---|---|---|
committer | Mitya Selivanov <automainint@guattari.tech> | 2024-07-31 22:38:49 +0200 |
commit | 823b3f75baae3120218f45acaf2eb8cb2f844cb8 (patch) | |
tree | c52e2f647672eb6c6ae1c49365b87e5aef3efa98 | |
parent | 98d5c2ad147330e7b18d4b2ee36eb26cb7e40a84 (diff) | |
download | bxgen-823b3f75baae3120218f45acaf2eb8cb2f844cb8.zip |
Cleanup
-rwxr-xr-x | bxgen.c | 323 |
1 files changed, 182 insertions, 141 deletions
@@ -84,7 +84,7 @@ #/ #/ Bugs #/ -#/ - Segfault at 0x4018ff +#/ - Segfault at 0x58fdf9 #/ #/ Done features #/ @@ -299,7 +299,7 @@ enum { // REL_ADD_INSTRUCTION_ADDRESS, - REL_ADD_RODATA_ADDRESS, + REL_ADD_RO_DATA_ADDRESS, REL_ADD_PROC_ADDRESS, }; @@ -455,14 +455,14 @@ typedef struct { typedef struct { i64 max_num_rels; i64 max_code_size; - i64 max_rodata_size; + i64 max_ro_data_size; i64 entry_point; i64 num_rels; i64 offset_code; - i64 offset_rodata; + i64 offset_ro_data; Rel_Entry *rels; u8 * buffer_code; - u8 * buffer_rodata; + u8 * buffer_ro_data; } Codegen_Context; typedef struct { @@ -1548,6 +1548,7 @@ enum { ELF_SYMBOL_ENTRY_SIZE = 24, ELF_REL_ENTRY_SIZE = 16, ELF_RELA_ENTRY_SIZE = 24, + ELF_DYNAMIC_ENTRY_SIZE = 16, SEC_NONE = 0, SEC_PROGBITS, @@ -1822,10 +1823,10 @@ void x86_64_emit_node( Node *data = &pool->entities[arg->ref].node; CHECK(data->op == DATA_I8, "Not implemented", 0); - CHECK(codegen->offset_rodata + data->lit.num_bytes <= codegen->max_rodata_size, "Out of memory", 0); + CHECK(codegen->offset_ro_data + data->lit.num_bytes <= codegen->max_ro_data_size, "Out of memory", 0); - i64 arg_offset = codegen->offset_rodata; - mem_cpy(codegen->buffer_rodata + arg_offset, data->lit.as_u8, data->lit.num_bytes); + i64 arg_offset = codegen->offset_ro_data; + mem_cpy(codegen->buffer_ro_data + arg_offset, data->lit.as_u8, data->lit.num_bytes); // Write code and relocations // @@ -1834,7 +1835,7 @@ void x86_64_emit_node( write_u8(LE, 0xbf, begin + 1, end); // rdi codegen->rels[codegen->num_rels++] = (Rel_Entry) { - .type = REL_ADD_RODATA_ADDRESS, + .type = REL_ADD_RO_DATA_ADDRESS, .offset = codegen->offset_code + 2, .size = 8, .value = arg_offset, @@ -1857,7 +1858,7 @@ void x86_64_emit_node( write_u8(LE, 0xd0, begin + 21, end); // rax codegen->offset_code += 22; - codegen->offset_rodata += data->lit.num_bytes; + codegen->offset_ro_data += data->lit.num_bytes; } else if (arg->op == DATA_I32) { CHECK(arg->lit.num_bytes == 4, "Not implemented",); @@ -1975,10 +1976,10 @@ void x86_64_emit_node( Node *dat_2 = &pool->entities[arg_2->ref].node; CHECK(dat_2->op == DATA_PTR, "Not implemented", 0); - CHECK(codegen->offset_rodata + dat_2->lit.num_bytes <= codegen->max_rodata_size, "Out of memory", 0); + CHECK(codegen->offset_ro_data + dat_2->lit.num_bytes <= codegen->max_ro_data_size, "Out of memory", 0); - i64 arg_2_offset = codegen->offset_rodata; - mem_cpy(codegen->buffer_rodata + arg_2_offset, dat_2->lit.as_u8, dat_2->lit.num_bytes); + i64 arg_2_offset = codegen->offset_ro_data; + mem_cpy(codegen->buffer_ro_data + arg_2_offset, dat_2->lit.as_u8, dat_2->lit.num_bytes); // Write code and relocations // @@ -1999,7 +2000,7 @@ void x86_64_emit_node( write_u8 (LE, 0xba, begin + 16, end); // rdx codegen->rels[codegen->num_rels++] = (Rel_Entry) { - .type = REL_ADD_RODATA_ADDRESS, + .type = REL_ADD_RO_DATA_ADDRESS, .offset = codegen->offset_code + 17, .size = 8, .value = arg_2_offset, @@ -2015,33 +2016,40 @@ void x86_64_emit_node( write_u8 (LE, 0x48, begin + 45, end); // movabs write_u8 (LE, 0xb9, begin + 46, end); // r9 write_u64(LE, arg_5->lit.as_u64[0], begin + 47, end); - write_u8 (LE, 0x48, begin + 55, end); // movabs - write_u8 (LE, 0xb8, begin + 56, end); // rax - write_u64(LE, arg_6->lit.as_u64[0], begin + 57, end); - write_u8 (LE, 0x50, begin + 65, end); // push rax - write_u8 (LE, 0x48, begin + 66, end); // movabs - write_u8 (LE, 0xb8, begin + 67, end); // rax + // rsp alignment + write_u8(LE, 0x48, begin + 55, end); // sub rsp + write_u8(LE, 0x83, begin + 56, end); // + write_u8(LE, 0xec, begin + 57, end); // + write_u8(LE, 8, begin + 58, end); // 8 + + write_u8 (LE, 0x48, begin + 59, end); // movabs + write_u8 (LE, 0xb8, begin + 60, end); // rax + write_u64(LE, arg_6->lit.as_u64[0], begin + 61, end); + write_u8 (LE, 0x50, begin + 69, end); // push rax + + write_u8 (LE, 0x48, begin + 70, end); // movabs + write_u8 (LE, 0xb8, begin + 71, end); // rax codegen->rels[codegen->num_rels++] = (Rel_Entry) { .type = REL_ADD_PROC_ADDRESS, - .offset = codegen->offset_code + 68, + .offset = codegen->offset_code + 72, .size = 8, .name_size = n->call.target_name_size, .name = n->call.target_name, .proc = UNDEFINED, // FIXME use zero value }; - write_u8(LE, 0xff, begin + 76, end); // call - write_u8(LE, 0xd0, begin + 77, end); // rax + write_u8(LE, 0xff, begin + 80, end); // call + write_u8(LE, 0xd0, begin + 81, end); // rax - write_u8(LE, 0x48, begin + 78, end); // add rsp - write_u8(LE, 0x83, begin + 79, end); // - write_u8(LE, 0xc4, begin + 80, end); // - write_u8(LE, 8, begin + 81, end); // 8 + write_u8(LE, 0x48, begin + 82, end); // add rsp + write_u8(LE, 0x83, begin + 83, end); // + write_u8(LE, 0xc4, begin + 84, end); // + write_u8(LE, 16, begin + 85, end); // 16 - codegen->offset_code += 82; - codegen->offset_rodata += dat_2->lit.num_bytes; + codegen->offset_code += 86; + codegen->offset_ro_data += dat_2->lit.num_bytes; } break; default: @@ -2649,19 +2657,20 @@ i64 unit_write_in_memory( emit_unit(pool, codegen, unit, arch); - u16 num_program_headers = 5; + u16 num_program_headers = 6; 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; - i64 rxcode_address = base_address + program_offset; - i64 entry = rxcode_address + codegen->entry_point; + i64 base_address = X86_64_BASE_ADDRESS; + i64 rx_code_address = base_address + program_offset; + i64 entry = rx_code_address + codegen->entry_point; - LOG(VERBOSE, "Entry point: 0x%llx (%lld)", entry, entry); + LOG(VERBOSE, "Entry point: 0x%llx", entry); - i64 rxcode_size = codegen->offset_code; - i64 rwzval_size = 0; - i64 rwdata_size = 0; - i64 rodata_size = codegen->offset_rodata; + 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 ro_dynamic_size = ELF_DYNAMIC_ENTRY_SIZE; i64 num_sections_total = 0; i64 num_symbols = 0; @@ -2688,20 +2697,20 @@ i64 unit_write_in_memory( continue; if (section.exec) { - linker->section_offsets[num_sections_total] = rxcode_size; - linker->section_addresses[num_sections_total] = rxcode_size; - rxcode_size += bx_align(section.data.size, X86_64_ALIGNMENT); + linker->section_offsets[num_sections_total] = rx_code_size; + linker->section_addresses[num_sections_total] = rx_code_size; + rx_code_size += bx_align(section.data.size, X86_64_ALIGNMENT); } else if (section.write && section.type == SEC_NOBITS) { - linker->section_addresses[num_sections_total] = rwzval_size; - rwzval_size += bx_align(section.data.size, X86_64_ALIGNMENT); + linker->section_addresses[num_sections_total] = rw_zval_size; + rw_zval_size += bx_align(section.data.size, X86_64_ALIGNMENT); } else if (section.write) { - linker->section_offsets[num_sections_total] = rwdata_size; - linker->section_addresses[num_sections_total] = rwdata_size; - rwdata_size += bx_align(section.data.size, X86_64_ALIGNMENT); + linker->section_offsets[num_sections_total] = rw_data_size; + linker->section_addresses[num_sections_total] = rw_data_size; + rw_data_size += bx_align(section.data.size, X86_64_ALIGNMENT); } else if (section.data.size > 0) { - linker->section_offsets[num_sections_total] = rodata_size; - linker->section_addresses[num_sections_total] = rodata_size; - rodata_size += bx_align(section.data.size, X86_64_ALIGNMENT); + linker->section_offsets[num_sections_total] = ro_data_size; + linker->section_addresses[num_sections_total] = ro_data_size; + ro_data_size += bx_align(section.data.size, X86_64_ALIGNMENT); } else { LAX(0, "Unsupported section type"); continue; @@ -2709,18 +2718,21 @@ i64 unit_write_in_memory( } } - rxcode_size = bx_align(rxcode_size, X86_64_PAGE_SIZE); - rwzval_size = bx_align(rwzval_size, X86_64_PAGE_SIZE); - rwdata_size = bx_align(rwdata_size, X86_64_PAGE_SIZE); - rodata_size = bx_align(rodata_size, X86_64_PAGE_SIZE); + rx_code_size = bx_align(rx_code_size, X86_64_PAGE_SIZE); + rw_zval_size = bx_align(rw_zval_size, X86_64_PAGE_SIZE); + rw_data_size = bx_align(rw_data_size, X86_64_PAGE_SIZE); + ro_data_size = bx_align(ro_data_size, X86_64_PAGE_SIZE); + ro_dynamic_size = bx_align(ro_dynamic_size, X86_64_PAGE_SIZE); - i64 rwzval_address = rxcode_address + rxcode_size; - i64 rwdata_address = rwzval_address + rwzval_size; - i64 rodata_address = rwdata_address + rwdata_size; + i64 rw_zval_address = rx_code_address + rx_code_size; + i64 rw_data_address = rw_zval_address + rw_zval_size; + i64 ro_data_address = rw_data_address + rw_data_size; + i64 ro_dynamic_address = ro_data_address + ro_data_size; - i64 rxcode_offset = program_offset; - i64 rwdata_offset = rxcode_offset + rxcode_size; - i64 rodata_offset = rwdata_offset + rwdata_size; + i64 rx_code_offset = program_offset; + i64 rw_data_offset = rx_code_offset + rx_code_size; + i64 ro_data_offset = rw_data_offset + rw_data_size; + i64 ro_dynamic_offset = ro_data_offset + ro_data_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); @@ -2735,16 +2747,16 @@ i64 unit_write_in_memory( continue; if (section.exec) { - linker->section_offsets[sec_index_global] += rxcode_offset + codegen->offset_code; - linker->section_addresses[sec_index_global] += rxcode_address + codegen->offset_code; + linker->section_offsets[sec_index_global] += rx_code_offset + codegen->offset_code; + linker->section_addresses[sec_index_global] += rx_code_address + codegen->offset_code; } else if (section.write && section.type == SEC_NOBITS) { - linker->section_addresses[sec_index_global] += rwzval_address; + linker->section_addresses[sec_index_global] += rw_zval_address; } else if (section.write) { - linker->section_offsets[sec_index_global] += rwdata_offset; - linker->section_addresses[sec_index_global] += rwdata_address; + linker->section_offsets[sec_index_global] += rw_data_offset; + linker->section_addresses[sec_index_global] += rw_data_address; } else if (section.data.size > 0) { - linker->section_offsets[sec_index_global] += rodata_offset + codegen->offset_rodata; - linker->section_addresses[sec_index_global] += rodata_address + codegen->offset_rodata; + linker->section_offsets[sec_index_global] += ro_data_offset + codegen->offset_ro_data; + linker->section_addresses[sec_index_global] += ro_data_address + codegen->offset_ro_data; } } } @@ -2807,8 +2819,8 @@ i64 unit_write_in_memory( // // TODO Add runtime library symbols // - // _DYNAMIC // _GLOBAL_OFFSET_TABLE_ + // _DYNAMIC // // _Unwind_Resume // _Unwind_Backtrace @@ -2851,7 +2863,7 @@ i64 unit_write_in_memory( // __getf2 // __unordtf2 - CHECK(num_symbols < linker->max_num_symbols, "Too many symbols",); + CHECK(num_symbols + 2 <= linker->max_num_symbols, "Too many symbols",); linker->symbols[num_symbols++] = (Symbol_Entry) { .name_size = 12, @@ -2860,6 +2872,15 @@ i64 unit_write_in_memory( .size = ELF_HEADER_SIZE, }; + // Add the dummy _DYNAMIC segment + + linker->symbols[num_symbols++] = (Symbol_Entry) { + .name_size = 8, + .name = "_DYNAMIC", + .address = ro_dynamic_address, + .size = ELF_DYNAMIC_ENTRY_SIZE, + }; + // ============================================================== // // Apply relocations @@ -2948,7 +2969,8 @@ i64 unit_write_in_memory( // Represents the place (section offset or address) of the Procedure Linkage Table entry for a symbol. i64 L = S; - // if (P >= 0x4018ad && P < 0x4018b2) { + // if (S == 0x5a2478 || S + A == 0x5a2478) { + // LOG(VERBOSE, "%s: type %d, S=0x%llx, A=0x%llx, P=0x%llx", sym_name, relx.type, S, A, P); // LOG(TRACE, ""); // } // LOG(VERBOSE, "--"); @@ -2965,8 +2987,6 @@ i64 unit_write_in_memory( case R_X86_64_GOTTPOFF: case R_X86_64_TPOFF32: - SKIP_(_DYNAMIC); - SKIP_(__preinit_array_start); SKIP_(__preinit_array_end); SKIP_(__init_array_start); @@ -2998,7 +3018,7 @@ i64 unit_write_in_memory( SKIP_(__unordtf2); if (S == 0) - LOG(VERBOSE, "Check symbol: %s", sym_name); + LOG(ERROR, "Check symbol: %s", sym_name); CHECK(S != 0, "Symbol address is 0", 0); break; @@ -3082,13 +3102,13 @@ i64 unit_write_in_memory( switch (rel.type) { case REL_ADD_INSTRUCTION_ADDRESS: { CHECK(rel.size == 8, "Not implemented", 0); - i64 value = rel.value + rxcode_address + rel.offset; + i64 value = rel.value + rx_code_address + rel.offset; write_i64(LE, value, begin, end); } break; - case REL_ADD_RODATA_ADDRESS: { + case REL_ADD_RO_DATA_ADDRESS: { CHECK(rel.size == 8, "Not implemented", 0); - i64 value = rel.value + rodata_address; + i64 value = rel.value + ro_data_address; write_i64(LE, value, begin, end); } break; @@ -3117,7 +3137,7 @@ i64 unit_write_in_memory( Proc *p = &pool->entities[rel.proc].proc; CHECK(p->codegen.emit_done, "No proc address", 0); - i64 value = rel.value + rxcode_address + p->codegen.offset; + i64 value = rel.value + rx_code_address + p->codegen.offset; write_i64(LE, value, begin, end); found = 1; @@ -3137,7 +3157,15 @@ i64 unit_write_in_memory( // Writing the ELF executable // - i64 output_size = bx_align(program_offset + rxcode_size + rwzval_size + rwdata_size + rodata_size, X86_64_ALIGNMENT); + i64 output_size = + bx_align( + program_offset + + rx_code_size + + rw_zval_size + + rw_data_size + + ro_data_size + + ro_dynamic_size, + X86_64_ALIGNMENT); CHECK(output_size <= linker->max_output_size, "Out of memory",); @@ -3145,10 +3173,11 @@ i64 unit_write_in_memory( LOG(VERBOSE, "Total %lld symbols", num_symbols); LOG(VERBOSE, "Total size"); - LOG(VERBOSE, ".rxcode - %7lld bytes", rxcode_size); - LOG(VERBOSE, ".rwzval - %7lld bytes", rwzval_size); - LOG(VERBOSE, ".rwdata - %7lld bytes", rwdata_size); - LOG(VERBOSE, ".rodata - %7lld bytes", rodata_size); + LOG(VERBOSE, "r/x code - %7lld bytes", rx_code_size); + LOG(VERBOSE, "r/w zval - %7lld bytes", rw_zval_size); + LOG(VERBOSE, "r/w data - %7lld bytes", rw_data_size); + LOG(VERBOSE, "r/o data - %7lld bytes", ro_data_size); + LOG(VERBOSE, "r/o dynamic - %7lld bytes", ro_dynamic_size); LOG(VERBOSE, "Writing ELF x86_64 executable"); @@ -3184,74 +3213,86 @@ i64 unit_write_in_memory( // Program headers // - CHECK(rxcode_offset % X86_64_PAGE_SIZE == rxcode_address % X86_64_PAGE_SIZE, "Invalid alignment",); - CHECK(rwdata_offset % X86_64_PAGE_SIZE == rwdata_address % X86_64_PAGE_SIZE, "Invalid alignment",); - CHECK(rodata_offset % X86_64_PAGE_SIZE == rodata_address % X86_64_PAGE_SIZE, "Invalid alignment",); + 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(ro_dynamic_offset % X86_64_PAGE_SIZE == ro_dynamic_offset % X86_64_PAGE_SIZE, "Invalid alignemtn",); // r/o elf header u8 *h = o + ELF_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, 0, h + 8, o_end); - write_i64(LE, base_address, h + 16, o_end); // virtual address - write_i64(LE, base_address, h + 24, o_end); // phisical address - 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); + 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); + write_i64(LE, base_address, h + 16, o_end); // virtual address + write_i64(LE, base_address, h + 24, o_end); // phisical address + 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 - 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); - write_i64(LE, rxcode_address, h + 16, o_end); // virtual address - write_i64(LE, rxcode_address, h + 24, o_end); // phisical address - 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); + 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, rx_code_offset, h + 8, o_end); + write_i64(LE, rx_code_address, h + 16, o_end); // virtual address + write_i64(LE, rx_code_address, h + 24, o_end); // phisical address + write_i64(LE, rx_code_size, h + 32, o_end); // size in file + write_i64(LE, rx_code_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 - 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); - write_i64(LE, rwzval_address, h + 16, o_end); // virtual address - write_i64(LE, rwzval_address, h + 24, o_end); // phisical address - 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); + 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_i64(LE, X86_64_ALIGNMENT, h + 48, o_end); h += ELF_PROGRAM_HEADER_SIZE; // r/w 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, rwdata_offset, h + 8, o_end); - write_i64(LE, rwdata_address, h + 16, o_end); // virtual address - write_i64(LE, rwdata_address, h + 24, o_end); // phisical address - 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); + 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_data_address, h + 16, o_end); // virtual address + write_i64(LE, rw_data_address, h + 24, o_end); // phisical address + write_i64(LE, rw_data_size, h + 32, o_end); // size in file + write_i64(LE, rw_data_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 - 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); - write_i64(LE, rodata_address, h + 16, o_end); // virtual address - write_i64(LE, rodata_address, h + 24, o_end); // phisical address - 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); + 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_i64(LE, X86_64_ALIGNMENT, h + 48, o_end); + h += ELF_PROGRAM_HEADER_SIZE; + + // r/o dynamic + 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_dynamic_offset, h + 8, o_end); + write_i64(LE, ro_dynamic_address, h + 16, o_end); // virtual address + write_i64(LE, ro_dynamic_address, h + 24, o_end); // phisical address + write_i64(LE, ro_dynamic_size, h + 32, o_end); // size in file + write_i64(LE, ro_dynamic_size, h + 40, o_end); // size in memory + write_i64(LE, X86_64_ALIGNMENT, h + 48, o_end); h += ELF_PROGRAM_HEADER_SIZE; - CHECK(h == o + ELF_HEADER_SIZE + num_program_headers * ELF_PROGRAM_HEADER_SIZE, "Sanity",); - CHECK(rxcode_offset >= h - o, "Sanity",); + CHECK(h == o + ELF_HEADER_SIZE + num_program_headers * ELF_PROGRAM_HEADER_SIZE, "Invalid num program headers",); + CHECK(rx_code_offset >= h - o, "Sanity",); // Code // - mem_cpy(o + rxcode_offset, codegen->buffer_code, codegen->offset_code); - mem_cpy(o + rodata_offset, codegen->buffer_rodata, codegen->offset_rodata); + mem_cpy(o + rx_code_offset, codegen->buffer_code, codegen->offset_code); + mem_cpy(o + ro_data_offset, codegen->buffer_ro_data, codegen->offset_ro_data); // ============================================================== // @@ -3384,7 +3425,7 @@ void unit_write( mem_set(codegen->rels, 0, codegen->max_num_rels * sizeof *codegen->rels); mem_set(codegen->buffer_code, 0, codegen->max_code_size); - mem_set(codegen->buffer_rodata, 0, codegen->max_rodata_size); + mem_set(codegen->buffer_ro_data, 0, codegen->max_ro_data_size); mem_set(linker->obj_file_buffer, 0, linker->max_obj_file_size); mem_set(linker->dependencies_buffer, 0, linker->max_dependencies_size); mem_set(linker->obj_file_offsets, 0, linker->max_num_obj_files * sizeof *linker->obj_file_offsets); @@ -3640,18 +3681,18 @@ Pool g_pool = { }; Codegen_Context g_codegen = { - .max_num_rels = MAX_NUM_RELS, - .max_code_size = MAX_CODE_SIZE, - .max_rodata_size = MAX_CODE_SIZE, - - .entry_point = 0, - .num_rels = 0, - .offset_code = 0, - .offset_rodata = 0, - - .rels = (Rel_Entry[MAX_NUM_RELS]) {0}, - .buffer_code = (u8[MAX_CODE_SIZE]) {0}, - .buffer_rodata = (u8[MAX_CODE_SIZE]) {0}, + .max_num_rels = MAX_NUM_RELS, + .max_code_size = MAX_CODE_SIZE, + .max_ro_data_size = MAX_CODE_SIZE, + + .entry_point = 0, + .num_rels = 0, + .offset_code = 0, + .offset_ro_data = 0, + + .rels = (Rel_Entry[MAX_NUM_RELS]) {0}, + .buffer_code = (u8[MAX_CODE_SIZE]) {0}, + .buffer_ro_data = (u8[MAX_CODE_SIZE]) {0}, }; Linker_Context g_linker = { |