diff options
author | Mitya Selivanov <automainint@guattari.tech> | 2024-07-24 19:57:18 +0200 |
---|---|---|
committer | Mitya Selivanov <automainint@guattari.tech> | 2024-07-24 19:57:18 +0200 |
commit | 0ffe986458935ca02d4ed0312a614e9cb8d491da (patch) | |
tree | c294c82d19f611014b07ee0bd0410883d75b6f96 | |
parent | 6a546e1cda2da728edc72305c009236b1ecf51ef (diff) | |
download | bxgen-0ffe986458935ca02d4ed0312a614e9cb8d491da.zip |
Fix alignment
-rwxr-xr-x | bxgen.c | 38 |
1 files changed, 22 insertions, 16 deletions
@@ -56,7 +56,7 @@ #/ #/ Bugs #/ -#/ - Segmentation fault when assembling all sections +#/ - ... #/ #/ Done features #/ @@ -2081,8 +2081,9 @@ void unit_write(Pool *pool, i64 unit, u16 target, i64 io_out, void *io_user_data 0x0f, 0x05, // syscall }; - u16 num_program_headers = 1; + u16 num_program_headers = 4; + i64 page_size = 4096; i64 program_offset = bx_align(ELF_HEADER_SIZE + ELF_PROGRAM_HEADER_SIZE * num_program_headers, X86_64_ALIGNMENT); i64 code_size = bx_align(sizeof code, X86_64_ALIGNMENT); i64 entry_offset = 0; @@ -2220,6 +2221,11 @@ void unit_write(Pool *pool, i64 unit, u16 target, i64 io_out, void *io_user_data } } + rotext_size = bx_align(rotext_size, page_size); + rwzval_size = bx_align(rwzval_size, page_size); + rwdata_size = bx_align(rwdata_size, page_size); + rodata_size = bx_align(rodata_size, page_size); + i64 rwzval_address = rotext_address + rotext_size; i64 rwdata_address = rwzval_address + rwzval_size; i64 rodata_address = rwdata_address + rwdata_size; @@ -2239,7 +2245,7 @@ void unit_write(Pool *pool, i64 unit, u16 target, i64 io_out, void *io_user_data continue; if (section.exec) - pool->section_addresses[sec_index_global] += rotext_address; + pool->section_addresses[sec_index_global] += rotext_address + code_size; else if (section.write && section.type == SEC_NOBITS) pool->section_addresses[sec_index_global] += rwzval_address; else if (section.write) @@ -2510,8 +2516,7 @@ void unit_write(Pool *pool, i64 unit, u16 target, i64 io_out, void *io_user_data // Writing the ELF executable // - //i64 output_size = bx_align(program_offset + rotext_size + rwzval_size + rwdata_size + rodata_size, X86_64_ALIGNMENT); - i64 output_size = bx_align(program_offset + code_size, X86_64_ALIGNMENT); + i64 output_size = bx_align(program_offset + rotext_size + rwzval_size + rwdata_size + rodata_size, X86_64_ALIGNMENT); BX_CHECK(output_size <= pool->max_output_size, "Out of memory",); @@ -2529,12 +2534,12 @@ void unit_write(Pool *pool, i64 unit, u16 target, i64 io_out, void *io_user_data BX_LOG(VERBOSE, "Writing ELF x86_64 executable"); - // ELF header - // - u8 *o = pool->output_buffer; u8 *o_end = o + pool->max_output_size; + // ELF header + // + bx_mem_cpy(o, ELF_MAGIC, 4); write_u8 (LE, ELF_64, o + 4, o_end); @@ -2566,6 +2571,11 @@ void unit_write(Pool *pool, i64 unit, u16 target, i64 io_out, void *io_user_data i64 rwdata_offset = rwzval_offset; i64 rodata_offset = rwdata_offset + rwdata_size; + BX_CHECK(rotext_offset % page_size == rotext_address % page_size, "Invalid alignment",); + BX_CHECK(rwzval_offset % page_size == rwzval_address % page_size, "Invalid alignment",); + BX_CHECK(rwdata_offset % page_size == rwdata_address % page_size, "Invalid alignment",); + BX_CHECK(rodata_offset % page_size == rodata_address % page_size, "Invalid alignment",); + // .rotext write_u32(LE, 1, o + 64, o_end); // type (PT_LOAD) write_u32(LE, 5, o + 68, o_end); // flags (PF_X | PF_R) @@ -2576,7 +2586,6 @@ void unit_write(Pool *pool, i64 unit, u16 target, i64 io_out, void *io_user_data write_i64(LE, rotext_size, o + 104, o_end); // size in memory write_i64(LE, X86_64_ALIGNMENT, o + 112, o_end); - /* // .rwzval write_u32(LE, 1, o + 120, o_end); // type (PT_LOAD) write_u32(LE, 6, o + 124, o_end); // flags (PF_R | PF_W) @@ -2606,9 +2615,8 @@ void unit_write(Pool *pool, i64 unit, u16 target, i64 io_out, void *io_user_data write_i64(LE, rodata_size, o + 264, o_end); // size in file write_i64(LE, rodata_size, o + 272, o_end); // size in memory write_i64(LE, X86_64_ALIGNMENT, o + 280, o_end); - */ - //BX_CHECK(rotext_offset >= 288, "Sanity",); + BX_CHECK(rotext_offset >= 288, "Sanity",); // Code // @@ -2617,9 +2625,8 @@ void unit_write(Pool *pool, i64 unit, u16 target, i64 io_out, void *io_user_data // ============================================================== // - // TODO Write sections into the output buffer + // Write sections into the output buffer - /* for (i64 elf_index = 0; elf_index < num_obj_files; ++elf_index) { Buffer_Context buf = elf_buffer_context(pool, num_obj_files, elf_index); Offset_Num headers = elf_section_headers(buf); @@ -2631,12 +2638,11 @@ void unit_write(Pool *pool, i64 unit, u16 target, i64 io_out, void *io_user_data section.data.size == 0) continue; u8 *p = o + (pool->section_addresses[sec_index] - base_address); - BX_CHECK(p >= pool->output_buffer + program_offset, "Buffer overflow",); - BX_CHECK(p + section.data.size <= pool->output_buffer + output_size, "Buffer overflow",); + BX_CHECK(p >= pool->output_buffer + program_offset + code_size, "Buffer overflow",); + BX_CHECK(p + section.data.size <= pool->output_buffer + output_size, "Buffer overflow",); bx_mem_cpy(p, buf.begin + section.data.offset, section.data.size); } } - */ // ============================================================== // |