/* * Layout (file order) without .data: * [0..64) ELF header * [64..120) one program header (PT_LOAD R+X) * [120..0x1000) zero pad * [0x1000..) .text bytes * * With .data (any input .o has writable globals): * [0..64) ELF header * [64..176) two program headers (PT_LOAD R+X, PT_LOAD R+W) * [176..0x1000) zero pad * [0x1000..) .text bytes * [data_off..) .data bytes (file offset and vaddr page-aligned) */ #include "l.h" #include #include #include #define ET_EXEC 2 #define EM_X86_64 62 #define EV_CURRENT 1 #define ELFCLASS64 2 #define ELFDATA2LSB 1 #define PT_LOAD 1 #define PF_X 1 #define PF_W 2 #define PF_R 4 #pragma pack(push, 1) typedef struct { u8 e_ident[16]; u16 e_type, e_machine; u32 e_version; u64 e_entry, e_phoff, e_shoff; u32 e_flags; u16 e_ehsize, e_phentsize, e_phnum, e_shentsize, e_shnum, e_shstrndx; } Ehdr; typedef struct { u32 p_type, p_flags; u64 p_offset, p_vaddr, p_paddr; u64 p_filesz, p_memsz, p_align; } Phdr; #pragma pack(pop) int l_emit_elf(Lnk *l, FILE *f, u64 base, u64 entry) { /* Dispatch: any loaded shared object plus any dynamic ref means * we owe the loader a real PT_INTERP/PT_DYNAMIC binary. */ if (l->sos != NULL && l->dyn_n > 0) return l_emit_dyn_elf(l, f, base, entry); const u64 page = 0x1000; const u64 text_off = 0x1000; const u64 rx_end = text_off + l->textlen; const int has_data = (l->datalen > 0); /* data goes at the next page boundary so the loader can grant a * fresh page of R+W permissions without overlapping the R+X mapping. */ const u64 data_off = has_data ? ((rx_end + page - 1) & ~(page - 1)) : 0; const u64 data_va = has_data ? (base + data_off) : 0; /* Apply relocations now that the layout's text_va/data_va are * known. Deferred from main.c so the dynamic-link path can use * its own data_va. */ if (l_relocate(l, base + text_off, data_va) != 0) return 1; /* BSS optimisation: trailing zero bytes in .data can be left out * of the file. The loader zero-fills the gap between p_filesz and * p_memsz, so this shrinks the binary without changing semantics. * Scan after l_relocate has applied DATAR patches — anything still * zero at the tail genuinely is zero-init. */ u64 bsslen = 0; if (has_data) { while (bsslen < l->datalen && l->data[l->datalen - 1 - bsslen] == 0) bsslen++; } const u64 data_file_len = l->datalen - bsslen; Ehdr eh = {0}; memcpy(eh.e_ident, "\x7f""ELF", 4); eh.e_ident[4] = ELFCLASS64; eh.e_ident[5] = ELFDATA2LSB; eh.e_ident[6] = EV_CURRENT; eh.e_type = ET_EXEC; eh.e_machine = EM_X86_64; eh.e_version = EV_CURRENT; eh.e_entry = entry; eh.e_phoff = sizeof(Ehdr); eh.e_ehsize = sizeof(Ehdr); eh.e_phentsize = sizeof(Phdr); eh.e_phnum = has_data ? 2 : 1; Phdr phx = {0}; phx.p_type = PT_LOAD; phx.p_flags = PF_R | PF_X; phx.p_offset = 0; phx.p_vaddr = base; phx.p_paddr = base; phx.p_filesz = rx_end; /* The loader page-rounds mappings; when present, data_off starts * independently on the next page, so padding is not R+X extent. */ phx.p_memsz = rx_end; phx.p_align = page; Phdr phw = {0}; if (has_data) { phw.p_type = PT_LOAD; phw.p_flags = PF_R | PF_W; phw.p_offset = data_off; phw.p_vaddr = base + data_off; phw.p_paddr = base + data_off; phw.p_filesz = data_file_len; phw.p_memsz = l->datalen; phw.p_align = page; } fwrite(&eh, 1, sizeof eh, f); fwrite(&phx, 1, sizeof phx, f); if (has_data) fwrite(&phw, 1, sizeof phw, f); long here = ftell(f); for (long i = here; i < (long)text_off; i++) fputc(0, f); if (l->textlen) fwrite(l->text, 1, l->textlen, f); if (has_data && data_file_len > 0) { here = ftell(f); for (long i = here; i < (long)data_off; i++) fputc(0, f); fwrite(l->data, 1, data_file_len, f); } return 0; }