Scan the consolidated .data buffer (post-relocation) for trailing zero bytes; set the R+W PT_LOAD's p_filesz to exclude them while p_memsz covers the full region. The loader zero-fills the gap, so behaviour is unchanged. Saves up to a page per binary on programs whose globals are zero-init. Mirrored in selfhost/cmd/w6l/out.ww so test 992's byte-identity diff still holds. Dynamic-link path is untouched — it still errors on any mutable global; that's the next feature.
145 lines
3.9 KiB
C
145 lines
3.9 KiB
C
/*
|
|
* out.c — emit a static ELF64 executable.
|
|
*
|
|
* 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)
|
|
*
|
|
* No interpreter, no dynamic, no .bss yet. Entry point is the address
|
|
* of the symbol named "_start" (or whatever main supplies via -e).
|
|
*/
|
|
#include "l.h"
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#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);
|
|
|
|
/* 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;
|
|
|
|
/* 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;
|
|
const u64 file_end = has_data ? (data_off + data_file_len) : rx_end;
|
|
(void)data_va;
|
|
|
|
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;
|
|
|
|
/* R+X load covering [0, rx_end). When .data is present we still
|
|
* round up to a page in memsz so the loader doesn't try to give
|
|
* the same page both R+X and R+W permissions. */
|
|
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;
|
|
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);
|
|
|
|
/* pad to text_off */
|
|
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) {
|
|
/* pad to data_off */
|
|
here = ftell(f);
|
|
for (long i = here; i < (long)data_off; i++) fputc(0, f);
|
|
fwrite(l->data, 1, data_file_len, f);
|
|
}
|
|
(void)file_end;
|
|
|
|
return 0;
|
|
}
|