Step 4: linking and output link.pas marks the chunks reachable from the entry point, places them and patches every fixup. elf.pas writes a static ELF executable with a symbol table. test/handtest.pas builds a program chunk by chunk; it prints and exits with 42, and leaves out the chunk nothing reaches. test/enctest.pas emits one of each instruction for objdump.
| @@ -8,13 +8,19 @@ | ||
| 8 | 8 | $(FPC) $(FPCFLAGS) -ojed src/jed.pas |
| 9 | 9 | |
| 10 | 10 | units: src/*.pas | build |
| 11 | - $(FPC) $(FPCFLAGS) src/scan.pas | |
| 12 | - $(FPC) $(FPCFLAGS) src/sym.pas | |
| 11 | + for u in scan sym x64 link elf; do $(FPC) $(FPCFLAGS) src/$$u.pas || exit 1; done | |
| 13 | 12 | |
| 13 | +# tests of the parts below the parser, built from test/ | |
| 14 | +tests: units | |
| 15 | + $(FPC) $(FPCFLAGS) -Fusrc -obuild/scantest test/scantest.pas | |
| 16 | + $(FPC) $(FPCFLAGS) -Fusrc -obuild/enctest test/enctest.pas | |
| 17 | + $(FPC) $(FPCFLAGS) -Fusrc -obuild/handtest test/handtest.pas | |
| 18 | + build/handtest build/hand && build/hand; test $$? = 42 | |
| 19 | + | |
| 14 | 20 | build: |
| 15 | 21 | mkdir -p build |
| 16 | 22 | |
| 17 | 23 | clean: |
| 18 | 24 | rm -rf build jed |
| 19 | 25 | |
| 20 | -.PHONY: units clean | |
| 26 | +.PHONY: units tests clean |
| @@ -0,0 +1,185 @@ | ||
| 1 | +{ elf.pas: the executable file. | |
| 2 | + | |
| 3 | + Writes a static ELF64 executable for x86-64 Linux: the ELF header, | |
| 4 | + one program header per section that has anything in it, the linked | |
| 5 | + text and rodata, and section headers with a symbol table, so that | |
| 6 | + objdump and gdb can name each procedure and variable. | |
| 7 | + | |
| 8 | + Depends on: link for the images and addresses, x64 for the chunks, | |
| 9 | + scan for names and errors, BaseUnix to write the file. | |
| 10 | + Leaves out: dynamic linking, for now. } | |
| 11 | + | |
| 12 | +unit elf; | |
| 13 | + | |
| 14 | +{$mode objfpc}{$H-} | |
| 15 | + | |
| 16 | +interface | |
| 17 | + | |
| 18 | +uses scan, x64, link; | |
| 19 | + | |
| 20 | +const | |
| 21 | + HeaderSize = 64 + 3 * 56; { the ELF header and up to three program headers } | |
| 22 | + | |
| 23 | +procedure WriteElf(const path: shortstring; entry: longint); | |
| 24 | + | |
| 25 | +implementation | |
| 26 | + | |
| 27 | +uses BaseUnix; | |
| 28 | + | |
| 29 | +var | |
| 30 | + buf: PBytes; | |
| 31 | + len, cap: int64; | |
| 32 | + | |
| 33 | +procedure Room(n: int64); | |
| 34 | +begin | |
| 35 | + if len + n > cap then begin | |
| 36 | + cap := cap * 2 + n; | |
| 37 | + buf := ReAllocMem(buf, cap); | |
| 38 | + end; | |
| 39 | +end; | |
| 40 | + | |
| 41 | +procedure PutN(v: qword; n: longint); | |
| 42 | +var i: longint; | |
| 43 | +begin | |
| 44 | + Room(n); | |
| 45 | + for i := 0 to n - 1 do begin | |
| 46 | + buf^[len] := byte(v); | |
| 47 | + v := v shr 8; | |
| 48 | + inc(len); | |
| 49 | + end; | |
| 50 | +end; | |
| 51 | + | |
| 52 | +procedure PutBytes(p: PBytes; n: int64); | |
| 53 | +begin | |
| 54 | + Room(n); | |
| 55 | + if n > 0 then Move(p^, buf^[len], n); | |
| 56 | + len := len + n; | |
| 57 | +end; | |
| 58 | + | |
| 59 | +procedure PadTo(off: int64); | |
| 60 | +begin | |
| 61 | + while len < off do PutN(0, 1); | |
| 62 | +end; | |
| 63 | + | |
| 64 | +procedure Patch(at: int64; v: qword; n: longint); | |
| 65 | +var i: longint; | |
| 66 | +begin | |
| 67 | + for i := 0 to n - 1 do begin | |
| 68 | + buf^[at + i] := byte(v); | |
| 69 | + v := v shr 8; | |
| 70 | + end; | |
| 71 | +end; | |
| 72 | + | |
| 73 | +procedure Phdr(flags: longint; off, adr, filesz, memsz: int64); | |
| 74 | +begin | |
| 75 | + PutN(1, 4); { PT_LOAD } | |
| 76 | + PutN(flags, 4); | |
| 77 | + PutN(off, 8); | |
| 78 | + PutN(adr, 8); | |
| 79 | + PutN(adr, 8); | |
| 80 | + PutN(filesz, 8); | |
| 81 | + PutN(memsz, 8); | |
| 82 | + PutN(Page, 8); | |
| 83 | +end; | |
| 84 | + | |
| 85 | +procedure Shdr(name, typ: longint; flags, adr, off, size: int64; lnk, info: longint; align, entsize: int64); | |
| 86 | +begin | |
| 87 | + PutN(name, 4); PutN(typ, 4); PutN(flags, 8); PutN(adr, 8); | |
| 88 | + PutN(off, 8); PutN(size, 8); PutN(lnk, 4); PutN(info, 4); | |
| 89 | + PutN(align, 8); PutN(entsize, 8); | |
| 90 | +end; | |
| 91 | + | |
| 92 | +procedure WriteElf(const path: shortstring; entry: longint); | |
| 93 | +const | |
| 94 | + ShNames = #0'.text'#0'.rodata'#0'.bss'#0'.symtab'#0'.strtab'#0'.shstrtab'#0; | |
| 95 | +var | |
| 96 | + nph, c, nsym, fd: longint; | |
| 97 | + symOff, strOff, shstrOff, shOff, strLen: int64; | |
| 98 | + strs: PBytes; | |
| 99 | + s, p: shortstring; | |
| 100 | + sec: longint; | |
| 101 | +begin | |
| 102 | + cap := 1 shl 16; | |
| 103 | + buf := GetMem(cap); | |
| 104 | + len := 0; | |
| 105 | + nph := 1; | |
| 106 | + if roSize > 0 then inc(nph); | |
| 107 | + if bssSize > 0 then inc(nph); | |
| 108 | + | |
| 109 | + { ELF header } | |
| 110 | + PutN($464C457F, 4); PutN(2, 1); PutN(1, 1); PutN(1, 1); PutN(0, 9); | |
| 111 | + PutN(2, 2); { ET_EXEC } | |
| 112 | + PutN(62, 2); { EM_X86_64 } | |
| 113 | + PutN(1, 4); | |
| 114 | + PutN(chunks^[entry].adr, 8); | |
| 115 | + PutN(64, 8); { program headers follow } | |
| 116 | + PutN(0, 8); { section headers: patched below } | |
| 117 | + PutN(0, 4); | |
| 118 | + PutN(64, 2); PutN(56, 2); PutN(nph, 2); | |
| 119 | + PutN(64, 2); PutN(7, 2); PutN(6, 2); | |
| 120 | + | |
| 121 | + { the first segment maps the headers too, so text can follow them } | |
| 122 | + Phdr(5, 0, Base, textOff + textSize, textOff + textSize); | |
| 123 | + if roSize > 0 then Phdr(4, roOff, Base + roOff, roSize, roSize); | |
| 124 | + if bssSize > 0 then Phdr(6, 0, bssAdr, 0, bssSize); | |
| 125 | + | |
| 126 | + PadTo(textOff); | |
| 127 | + PutBytes(textImg, textSize); | |
| 128 | + PadTo(roOff); { even when empty, so that every section lies in the file } | |
| 129 | + PutBytes(roImg, roSize); | |
| 130 | + | |
| 131 | + { the symbol table: one local symbol for each named live chunk } | |
| 132 | + PadTo((len + 7) div 8 * 8); | |
| 133 | + symOff := len; | |
| 134 | + strs := GetMem(1 shl 16); | |
| 135 | + strLen := 1; | |
| 136 | + strs^[0] := 0; | |
| 137 | + PutN(0, 24); | |
| 138 | + nsym := 1; | |
| 139 | + for c := 0 to numChunks - 1 do | |
| 140 | + if chunks^[c].live and (chunks^[c].name >= 0) then begin | |
| 141 | + s := ChunkName(c); | |
| 142 | + if strLen + length(s) + 1 > MemSize(strs) then strs := ReAllocMem(strs, strLen * 2 + 256); | |
| 143 | + Move(s[1], strs^[strLen], length(s)); | |
| 144 | + strs^[strLen + length(s)] := 0; | |
| 145 | + case chunks^[c].sec of | |
| 146 | + secText: sec := 1; | |
| 147 | + secRodata: sec := 2; | |
| 148 | + else sec := 3; | |
| 149 | + end; | |
| 150 | + PutN(strLen, 4); | |
| 151 | + if sec = 1 then PutN(2, 1) else PutN(1, 1); { STT_FUNC, STT_OBJECT } | |
| 152 | + PutN(0, 1); | |
| 153 | + PutN(sec, 2); | |
| 154 | + PutN(chunks^[c].adr, 8); | |
| 155 | + PutN(chunks^[c].size, 8); | |
| 156 | + strLen := strLen + length(s) + 1; | |
| 157 | + inc(nsym); | |
| 158 | + end; | |
| 159 | + strOff := len; | |
| 160 | + PutBytes(strs, strLen); | |
| 161 | + shstrOff := len; | |
| 162 | + s := ShNames; | |
| 163 | + PutBytes(@s[1], length(s)); | |
| 164 | + | |
| 165 | + PadTo((len + 7) div 8 * 8); | |
| 166 | + shOff := len; | |
| 167 | + Patch(40, shOff, 8); | |
| 168 | + Shdr(0, 0, 0, 0, 0, 0, 0, 0, 0, 0); | |
| 169 | + Shdr(1, 1, 6, Base + textOff, textOff, textSize, 0, 0, 16, 0); | |
| 170 | + Shdr(7, 1, 2, Base + roOff, roOff, roSize, 0, 0, 16, 0); | |
| 171 | + Shdr(15, 8, 3, bssAdr, roOff + roSize, bssSize, 0, 0, 16, 0); | |
| 172 | + Shdr(20, 2, 0, 0, symOff, nsym * 24, 5, nsym, 8, 24); | |
| 173 | + Shdr(28, 3, 0, 0, strOff, strLen, 0, 0, 1, 0); | |
| 174 | + Shdr(36, 3, 0, 0, shstrOff, length(ShNames), 0, 0, 1, 0); | |
| 175 | + | |
| 176 | + p := path + #0; | |
| 177 | + fd := FpOpen(@p[1], O_WRONLY or O_CREAT or O_TRUNC, &755); | |
| 178 | + if fd < 0 then Fatal('can''t write ' + path); | |
| 179 | + if FpWrite(fd, buf^, len) <> len then Fatal('can''t write ' + path); | |
| 180 | + FpClose(fd); | |
| 181 | + FreeMem(strs); | |
| 182 | + FreeMem(buf); | |
| 183 | +end; | |
| 184 | + | |
| 185 | +end. |
| @@ -0,0 +1,123 @@ | ||
| 1 | +{ link.pas: tree shaking, layout and patching. | |
| 2 | + | |
| 3 | + Starting from the entry chunk, marks every chunk the program can | |
| 4 | + reach through fixups; the rest is left out. Then gives each live | |
| 5 | + chunk its address and builds the images of text and rodata, with | |
| 6 | + every fixup patched. | |
| 7 | + | |
| 8 | + The file is laid out so that each section can be mapped as it lies: | |
| 9 | + text right after the headers, rodata on the next page, bss after | |
| 10 | + that, in memory only. | |
| 11 | + | |
| 12 | + Depends on: x64 for chunks and fixups, scan for names and errors. | |
| 13 | + Leaves out: the file format; that is elf.pas. } | |
| 14 | + | |
| 15 | +unit link; | |
| 16 | + | |
| 17 | +{$mode objfpc}{$H-} | |
| 18 | + | |
| 19 | +interface | |
| 20 | + | |
| 21 | +uses scan, x64; | |
| 22 | + | |
| 23 | +const | |
| 24 | + Base = $400000; | |
| 25 | + Page = 4096; | |
| 26 | + | |
| 27 | +var | |
| 28 | + textOff, textSize: int64; { file offset and size of text } | |
| 29 | + roOff, roSize: int64; | |
| 30 | + bssAdr, bssSize: int64; | |
| 31 | + textImg, roImg: PBytes; | |
| 32 | + | |
| 33 | +procedure LinkAll(entry: longint; headerSize: longint); | |
| 34 | +function ChunkName(c: longint): shortstring; | |
| 35 | + | |
| 36 | +implementation | |
| 37 | + | |
| 38 | +function ChunkName(c: longint): shortstring; | |
| 39 | +begin | |
| 40 | + if chunks^[c].name < 0 then exit('a literal'); | |
| 41 | + if chunks^[c].module < 0 then exit(NameOf(chunks^[c].name)); | |
| 42 | + ChunkName := NameOf(chunks^[c].module) + '.' + NameOf(chunks^[c].name); | |
| 43 | +end; | |
| 44 | + | |
| 45 | +procedure MarkLive(entry: longint); | |
| 46 | +var stack: PLongs; sp, c, f, t: longint; | |
| 47 | +begin | |
| 48 | + stack := GetMem(numChunks * 4); | |
| 49 | + sp := 0; | |
| 50 | + chunks^[entry].live := true; | |
| 51 | + stack^[0] := entry; | |
| 52 | + sp := 1; | |
| 53 | + while sp > 0 do begin | |
| 54 | + dec(sp); | |
| 55 | + c := stack^[sp]; | |
| 56 | + if chunks^[c].size < 0 then Fatal(ChunkName(c) + ' is declared but never defined'); | |
| 57 | + if chunks^[c].sec <> secText then continue; | |
| 58 | + for f := chunks^[c].firstFix to chunks^[c].firstFix + chunks^[c].numFix - 1 do begin | |
| 59 | + t := fixups^[f].chunk; | |
| 60 | + if not chunks^[t].live then begin | |
| 61 | + chunks^[t].live := true; | |
| 62 | + stack^[sp] := t; | |
| 63 | + inc(sp); | |
| 64 | + end; | |
| 65 | + end; | |
| 66 | + end; | |
| 67 | + FreeMem(stack); | |
| 68 | +end; | |
| 69 | + | |
| 70 | +{ Gives the live chunks of one section their places, from start; | |
| 71 | + returns the end. } | |
| 72 | +function Place(sec: TSection; start: int64): int64; | |
| 73 | +var c: longint; a: int64; | |
| 74 | +begin | |
| 75 | + a := start; | |
| 76 | + for c := 0 to numChunks - 1 do | |
| 77 | + if chunks^[c].live and (chunks^[c].sec = sec) then begin | |
| 78 | + a := (a + chunks^[c].align - 1) div chunks^[c].align * chunks^[c].align; | |
| 79 | + chunks^[c].adr := a; | |
| 80 | + a := a + chunks^[c].size; | |
| 81 | + end; | |
| 82 | + Place := a; | |
| 83 | +end; | |
| 84 | + | |
| 85 | +procedure LinkAll(entry: longint; headerSize: longint); | |
| 86 | +var c, f, at: longint; e, p, v: int64; | |
| 87 | +begin | |
| 88 | + MarkLive(entry); | |
| 89 | + textOff := (headerSize + 15) div 16 * 16; | |
| 90 | + e := Place(secText, Base + textOff); | |
| 91 | + textSize := e - (Base + textOff); | |
| 92 | + roOff := (textOff + textSize + Page - 1) div Page * Page; | |
| 93 | + e := Place(secRodata, Base + roOff); | |
| 94 | + roSize := e - (Base + roOff); | |
| 95 | + bssAdr := (Base + roOff + roSize + Page - 1) div Page * Page; | |
| 96 | + e := Place(secBss, bssAdr); | |
| 97 | + bssSize := e - bssAdr; | |
| 98 | + | |
| 99 | + textImg := GetMem(textSize + 1); | |
| 100 | + FillChar(textImg^, textSize + 1, $CC); | |
| 101 | + roImg := GetMem(roSize + 1); | |
| 102 | + FillChar(roImg^, roSize + 1, 0); | |
| 103 | + for c := 0 to numChunks - 1 do | |
| 104 | + if chunks^[c].live then | |
| 105 | + case chunks^[c].sec of | |
| 106 | + secText: begin | |
| 107 | + p := chunks^[c].adr - (Base + textOff); | |
| 108 | + Move(code^[chunks^[c].start], textImg^[p], chunks^[c].size); | |
| 109 | + for f := chunks^[c].firstFix to chunks^[c].firstFix + chunks^[c].numFix - 1 do begin | |
| 110 | + at := fixups^[f].at - chunks^[c].start; | |
| 111 | + { rel32: from the end of the field to the target } | |
| 112 | + v := chunks^[fixups^[f].chunk].adr + fixups^[f].addend | |
| 113 | + - (chunks^[c].adr + at + 4); | |
| 114 | + PLongint(@textImg^[p + at])^ := longint(v); | |
| 115 | + end; | |
| 116 | + end; | |
| 117 | + secRodata: | |
| 118 | + Move(rodata^[chunks^[c].start], roImg^[chunks^[c].adr - (Base + roOff)], | |
| 119 | + chunks^[c].size); | |
| 120 | + end; | |
| 121 | +end; | |
| 122 | + | |
| 123 | +end. |
| @@ -0,0 +1,32 @@ | ||
| 1 | +{ Emits one of each instruction form into a chunk, for objdump: a test | |
| 2 | + of the encoder in x64.pas. } | |
| 3 | +program enctest; | |
| 4 | +{$mode objfpc}{$H-} | |
| 5 | +uses scan, x64, link, elf; | |
| 6 | +var e, d: longint; L: longint; | |
| 7 | +begin | |
| 8 | + d := NewChunk(secBss, -1, Intern('data', 4)); SetBss(d, 8, 8); | |
| 9 | + e := NewChunk(secText, -1, Intern('start', 5)); | |
| 10 | + BeginChunk(e); | |
| 11 | + MovRR(RAX, RCX); MovRR(R11, RDI); MovRR(RSI, R9); | |
| 12 | + MovRI(RAX, 0); MovRI(RCX, 5); MovRI(RDX, -5); MovRI(R10, $FFFFFFFF); MovRI(RAX, $123456789A); | |
| 13 | + Load(RAX, Mem(RBP, -8), 8, true); Load(RAX, Mem(RBP, -200), 4, true); Load(RCX, Mem(RAX, 0), 4, false); | |
| 14 | + Load(RAX, Mem(RCX, 16), 2, true); Load(RAX, Mem(RAX, 3), 2, false); Load(RAX, Mem(RSP, 8), 1, true); | |
| 15 | + Load(RAX, Mem(RBP, 0), 1, false); Load(R10, Mem(12, 0), 8, true); Load(RAX, Mem(13, 0), 8, true); | |
| 16 | + Store(Mem(RBP, -16), RAX, 8); Store(Mem(RBP, -16), RSI, 1); Store(Mem(RCX, 0), RAX, 2); Store(Mem(RCX, 4), RDX, 4); | |
| 17 | + Store(MemIdx(RBP, R10, 8, 0), RAX, 8); Store(MemRip(d, 0), RAX, 8); Lea(RAX, MemRip(d, 4)); | |
| 18 | + Alu(aluAdd, RAX, RCX); Alu(aluSub, RCX, RAX); Alu(aluCmp, RAX, RCX); Alu(aluXor, R8, R9); | |
| 19 | + AluI(aluAdd, RAX, 1); AluI(aluAnd, RAX, 1000); AluI(aluCmp, RSP, 8); AluM(aluCmp, RAX, Mem(RCX, 8)); AluM(aluAdd, RAX, Mem(RCX, 0)); | |
| 20 | + Imul(RAX, RCX); ImulI(RAX, 24); Shift(shShl, RAX); Shift(shSar, RAX); ShiftI(shShr, RCX, 3); | |
| 21 | + Unary(2, RAX); Unary(3, RAX); Unary(6, RCX); Unary(7, RCX); Cqo; | |
| 22 | + Extend(RAX, 1, true); Extend(RAX, 1, false); Extend(RAX, 2, true); Extend(RAX, 2, false); Extend(RAX, 4, true); Extend(RAX, 4, false); | |
| 23 | + SetCC(ccL, RAX); SetCC(ccA, RCX); TestRR(RAX, RAX); | |
| 24 | + Push(RAX); Push(12); Pop(RCX); Pop(R11); CallR(R11); Ret; Leave; Syscall; Mfence; Ud2; RepMovsb; RepStosb; | |
| 25 | + LockCmpxchg(Mem(RCX, 0), RDX); Xchg(Mem(RCX, 0), RAX); LockXadd(Mem(RCX, 0), RAX); | |
| 26 | + MovqXR(0, RAX); MovqXR(1, RCX); MovqRX(RAX, 0); MovsdXM(2, Mem(RBP, -8)); MovsdMX(Mem(RBP, -8), 7); | |
| 27 | + Sse($58, 0, 1); Sse($5E, 0, 1); Ucomisd(0, 1); Cvtsi2sd(0, RAX); Cvttsd2si(RAX, 0); Btc(RAX, 63); | |
| 28 | + L := NoLabel; Jcc(ccE, L); Jmp(L); FixLink(L); JmpBack(chunks^[e].start); JccBack(ccNE, chunks^[e].start); | |
| 29 | + EndChunk(e); | |
| 30 | + LinkAll(e, HeaderSize); | |
| 31 | + WriteElf(ParamStr(1), e); | |
| 32 | +end. |
| @@ -0,0 +1,46 @@ | ||
| 1 | +{ Builds a program by hand, chunk by chunk, links it and writes it: | |
| 2 | + a test of x64.pas, link.pas and elf.pas. The program writes "hand" | |
| 3 | + from rodata, adds one to a bss variable through a call, and exits | |
| 4 | + with that variable plus 41: status 42. A chunk nothing reaches is | |
| 5 | + left out. } | |
| 6 | +program handtest; | |
| 7 | +{$mode objfpc}{$H-} | |
| 8 | +uses scan, x64, link, elf; | |
| 9 | +var entry, msg, count, bump, unused: longint; s: shortstring; | |
| 10 | +begin | |
| 11 | + s := 'hand'#10; | |
| 12 | + msg := NewChunk(secRodata, -1, Intern('msg', 3)); | |
| 13 | + SetData(msg, @s[1], length(s), 1); | |
| 14 | + count := NewChunk(secBss, -1, Intern('count', 5)); | |
| 15 | + SetBss(count, 8, 8); | |
| 16 | + bump := NewChunk(secText, -1, Intern('bump', 4)); | |
| 17 | + unused := NewChunk(secText, -1, Intern('unused', 6)); | |
| 18 | + entry := NewChunk(secText, -1, Intern('start', 5)); | |
| 19 | + | |
| 20 | + BeginChunk(bump); { count := count + 1 } | |
| 21 | + Load(RAX, MemRip(count, 0), 8, true); | |
| 22 | + AluI(aluAdd, RAX, 1); | |
| 23 | + Store(MemRip(count, 0), RAX, 8); | |
| 24 | + Ret; | |
| 25 | + EndChunk(bump); | |
| 26 | + | |
| 27 | + BeginChunk(unused); | |
| 28 | + Ud2; | |
| 29 | + EndChunk(unused); | |
| 30 | + | |
| 31 | + BeginChunk(entry); | |
| 32 | + MovRI(RAX, 1); { write(1, msg, 5) } | |
| 33 | + MovRI(RDI, 1); | |
| 34 | + Lea(RSI, MemRip(msg, 0)); | |
| 35 | + MovRI(RDX, length(s)); | |
| 36 | + Syscall; | |
| 37 | + CallChunk(bump); | |
| 38 | + Load(RDI, MemRip(count, 0), 8, true); | |
| 39 | + AluI(aluAdd, RDI, 41); | |
| 40 | + MovRI(RAX, 60); { exit(count + 41) } | |
| 41 | + Syscall; | |
| 42 | + EndChunk(entry); | |
| 43 | + | |
| 44 | + LinkAll(entry, HeaderSize); | |
| 45 | + WriteElf(ParamStr(1), entry); | |
| 46 | +end. |