From b57dc4a49242a5f0ae5d166690655f2611645e56 Mon Sep 17 00:00:00 2001 From: Freya Murphy Date: Thu, 12 Sep 2024 09:42:04 -0400 Subject: [PATCH] align segments --- masm/a.out | Bin 0 -> 4848 bytes masm/asm.c | 34 +++++++++++++++++++++++++++------- masm/asm.h | 1 + masm/out.o | Bin 0 -> 4632 bytes masm/test.asm | 5 +++++ 5 files changed, 33 insertions(+), 7 deletions(-) create mode 100755 masm/a.out create mode 100644 masm/out.o diff --git a/masm/a.out b/masm/a.out new file mode 100755 index 0000000000000000000000000000000000000000..0a4bfeeb799397dfee57890906b085444ad10641 GIT binary patch literal 4848 zcmeHLJxc>Y5Pf&iBw~?^S_l@NjUNb6B%p=pfkqH4Y^=`#aYWbYgPfPfK|XMU=^?mSOu&CRspMkRlq7>6|f5Y zs{+m&{EZG;KHXjGFO)xUxLL!|{VOKm?FM+nG|>0l7feEX-7tDqKY8mk48%jxRgpFbOoW-i3A*of$yx}T4q1FNe?m8 q%fwtoL&pce8ItZR-<8XZ(dFF+RzGI2pCj&hzdGaZyeHz7Vcr+**G{JZ literal 0 HcmV?d00001 diff --git a/masm/asm.c b/masm/asm.c index 1050b18..883cb55 100644 --- a/masm/asm.c +++ b/masm/asm.c @@ -17,6 +17,8 @@ extern char *current_file; #define SYMSEC_STUB -1 #define SYMSEC_EXTERN -1 +#define SEC_ALIGN 0x1000 + static int create_symbol(struct assembler *assembler, const char name[MAX_LEX_LENGTH], ssize_t section_idx, @@ -338,7 +340,7 @@ static int assemble_phdr(struct assembler *assembler, Elf32_Phdr **res, hdr->p_paddr = 0; hdr->p_filesz = htonl(size); hdr->p_memsz = htonl(size); - hdr->p_align = htonl(0x1000); + hdr->p_align = htonl(SEC_ALIGN); } *res = phdr; @@ -530,17 +532,30 @@ static void update_offsets(struct assembler *assembler, Elf32_Ehdr *ehdr) ptr += len * sizeof(Elf32_Rela); } + // section padding + { + uint32_t mod = ptr % SEC_ALIGN; + if (mod != 0) + assembler->secalign = (SEC_ALIGN - mod); + else + assembler->secalign = 0; + ptr += assembler->secalign; + } + // sections + size_t v_addr = 0x00400000; for (uint32_t i = 0; i < assembler->sectab.len; i++) { struct section *sec = &assembler->sectab.sections[i]; - int idx = sec->shdr_idx; + uint32_t idx = sec->shdr_idx; + uint32_t size = ntohl(phdr[i].p_filesz); phdr[i].p_offset = htonl(ptr); - phdr[i].p_vaddr = htonl(ptr); - phdr[i].p_paddr = htonl(ptr); + phdr[i].p_vaddr = htonl(v_addr); + phdr[i].p_paddr = htonl(v_addr); shdr[idx].sh_offset = htonl(ptr); shdr[idx].sh_size = phdr[i].p_filesz; shdr[idx].sh_addr = phdr[i].p_vaddr; - ptr += ntohl(phdr[i].p_filesz); + v_addr += size; + ptr += size; } // symtab @@ -567,8 +582,7 @@ static void update_offsets(struct assembler *assembler, Elf32_Ehdr *ehdr) static void update_sym_shindx(struct assembler *assembler) { - for (size_t i = 0; i < assembler->symtab.len; i++) - { + for (size_t i = 0; i < assembler->symtab.len; i++) { Elf32_Sym *sym = &assembler->symtab.symbols[i]; ssize_t sec = assembler->symtab.sections[i]; @@ -605,6 +619,12 @@ static int write_file(struct assembler *assembler, Elf32_Ehdr *ehdr, fwrite(ptr, sizeof(Elf32_Rela), len, out); } + // section padding + for (uint32_t i = 0; i < assembler->secalign; i++) { + uint8_t zero = 0; + fwrite(&zero, 1, 1, out); + } + // sections for (uint32_t i = 0; i < assembler->sectab.len; i++) { struct section *sec = &assembler->sectab.sections[i]; diff --git a/masm/asm.h b/masm/asm.h index 81099fc..1162164 100644 --- a/masm/asm.h +++ b/masm/asm.h @@ -213,6 +213,7 @@ struct assembler { /// Segments struct section_table sectab; + uint32_t secalign; // align sections to 0x1000 when writing /// program header Elf32_Phdr *phdr; diff --git a/masm/out.o b/masm/out.o new file mode 100644 index 0000000000000000000000000000000000000000..0b697af7e699e823fe702ae4f22b84855c276f9f GIT binary patch literal 4632 zcmeHLu}T9$5Pf%vQHhwvSXfwGA!s3oNDvFb6voOTjURB5!w44x$%3XbKOkC&AK+J6 zTG|PIfp2bigAof0%NcmPv-4)>me<|8>ztiNafDe!SYjRuEx^1!3fV$}3U&)5K_CVoRx ze1cyhSO-GMNdD5#B4)N6PBa4K7k`yVe65PhY=W^JVUS&2!{%cPq@%pB*+pveTZ^=p z*^H&3+sBguDWtsO_P)>jz`tzr;O}0;6VEGzJY4F+%UK>OdBu4irH>)LM+o60)(Oeu z(*F=2WPX0=3qDJ&^g&=6>=O?pM0nFC3nH8kbIhbm&}%6}=E-BW2oXFX80LXXvrOF* PzDsXquJ48CgP(n0K=?AB literal 0 HcmV?d00001 diff --git a/masm/test.asm b/masm/test.asm index b4c1498..d848d29 100644 --- a/masm/test.asm +++ b/masm/test.asm @@ -1,3 +1,8 @@ +.rodata +gay: +.word 3 + + .data str: