| 1 | (* Exe: the executable file, for Linux: ELF. |
| 2 | |
| 3 | Exe.darwin.raz, for macOS, is Mach-O; a target has one Exe, chosen |
| 4 | by the suffix of its file (doc/raz.md, 3), and no other unit knows the |
| 5 | format. This one writes an ELF64 executable: the ELF header, the |
| 6 | program headers, the linked text and rodata, and section headers |
| 7 | with a symbol table, so that objdump and gdb can name each procedure |
| 8 | and variable. For gdb's line numbers there is DWARF 4: a line table |
| 9 | (.debug_line), and the one compile unit that points to it |
| 10 | (.debug_info, .debug_abbrev). There are no types or variables. |
| 11 | |
| 12 | A program that uses C is linked dynamically, in the least way the |
| 13 | loader accepts: it names its loader and libraries, and each C symbol |
| 14 | it uses is a quadword the loader fills in with the symbol's address |
| 15 | (a GLOB_DAT relocation), which the code calls or reads through. |
| 16 | There is no PLT and no lazy binding, and the program exports nothing. |
| 17 | |
| 18 | Depends on: Link for the images and addresses, Chunk for the chunks, |
| 19 | Scan for names and memory, Diag for errors, and Host to write the |
| 20 | file; Link also makes the DWARF's compile unit. |
| 21 | Leaves out: position independent executables, symbol versions, the |
| 22 | rest of DWARF. |
| 23 | |
| 24 | The C library is glibc's or musl's, as setMusl says: the loader the |
| 25 | program names, and the library it needs first. |
| 26 | |
| 27 | Exported variables can't be set from outside, so needLib, setMusl, |
| 28 | setMainPath and setStripped do it. *) |
| 29 | module Exe; |
| 30 | import Host, Fmt, Diag, Scan, Chunk, Link; |
| 31 | import SYSTEM; |
| 32 | |
| 33 | const |
| 34 | Os* = "linux"; (* the target's operating system, as a file's suffix says it *) |
| 35 | |
| 36 | var |
| 37 | needed: [32]INT; (* the libraries to load, names interned *) |
| 38 | numNeeded*: INT; (* none: a static program *) |
| 39 | mainPath: []const U8; (* the main module's file, for gdb *) |
| 40 | stripped*: BOOL; (* -s: no symbols and no line numbers; the sections are there, empty *) |
| 41 | interp: []const U8; (* the loader a program that uses C names *) |
| 42 | libc*: []const U8; (* and the C library it needs *) |
| 43 | |
| 44 | buf: ^U8; |
| 45 | len, cap: INT; |
| 46 | |
| 47 | (* The library name, to be loaded, unless it is already: false if there |
| 48 | are too many *) |
| 49 | proc needLib*(name: INT): BOOL; |
| 50 | var i: INT; |
| 51 | begin |
| 52 | for i := 0 to numNeeded - 1 do |
| 53 | if needed[i] = name then return true; end; |
| 54 | end; |
| 55 | if numNeeded = 32 then return false; end; |
| 56 | needed[numNeeded] := name; |
| 57 | INC(numNeeded); |
| 58 | return true; |
| 59 | end; |
| 60 | |
| 61 | (* A static program again *) |
| 62 | proc noLibs*; |
| 63 | begin |
| 64 | numNeeded := 0; |
| 65 | end; |
| 66 | |
| 67 | (* A program that uses C is linked against musl, if yes, or glibc. |
| 68 | musl's library is its own loader, which takes libc.so to mean |
| 69 | itself, whatever file the system keeps it in. *) |
| 70 | proc setMusl*(yes: BOOL); |
| 71 | begin |
| 72 | if yes then |
| 73 | interp := "/lib/ld-musl-x86_64.so.1"; |
| 74 | libc := "libc.so"; |
| 75 | else |
| 76 | interp := "/lib64/ld-linux-x86-64.so.2"; |
| 77 | libc := "libc.so.6"; |
| 78 | end; |
| 79 | end; |
| 80 | |
| 81 | proc setMainPath*(path: []const U8); |
| 82 | begin |
| 83 | mainPath := path; |
| 84 | end; |
| 85 | |
| 86 | proc setStripped*(yes: BOOL); |
| 87 | begin |
| 88 | stripped := yes; |
| 89 | end; |
| 90 | |
| 91 | proc room(n: INT); |
| 92 | begin |
| 93 | if len + n > cap then |
| 94 | buf := Scan.regrab(buf, len, cap * 2 + n); |
| 95 | cap := cap * 2 + n; |
| 96 | end; |
| 97 | end; |
| 98 | |
| 99 | (* The n low bytes of v, least significant first *) |
| 100 | proc putN(v: INT; n: INT); |
| 101 | var i: INT; |
| 102 | begin |
| 103 | room(n); |
| 104 | for i := 0 to n - 1 do |
| 105 | buf[len] := U8(v & 0FFH); |
| 106 | v := v shr 8; |
| 107 | INC(len); |
| 108 | end; |
| 109 | end; |
| 110 | |
| 111 | proc putBytes(p: ^U8; n: INT); |
| 112 | begin |
| 113 | room(n); |
| 114 | if n > 0 then COPY(buf[len..len + n], p[0..n]); end; |
| 115 | len := len + n; |
| 116 | end; |
| 117 | |
| 118 | proc padTo(off: INT); |
| 119 | begin |
| 120 | while len < off do putN(0, 1); end; |
| 121 | end; |
| 122 | |
| 123 | proc patch(at: INT; v: INT; n: INT); |
| 124 | var i: INT; |
| 125 | begin |
| 126 | for i := 0 to n - 1 do |
| 127 | buf[at + i] := U8(v & 0FFH); |
| 128 | v := v shr 8; |
| 129 | end; |
| 130 | end; |
| 131 | |
| 132 | proc phdr(typ, flags: INT; off, adr, filesz, memsz, align: INT); |
| 133 | begin |
| 134 | putN(typ, 4); |
| 135 | putN(flags, 4); |
| 136 | putN(off, 8); |
| 137 | putN(adr, 8); |
| 138 | putN(adr, 8); |
| 139 | putN(filesz, 8); |
| 140 | putN(memsz, 8); |
| 141 | putN(align, 8); |
| 142 | end; |
| 143 | |
| 144 | const |
| 145 | (* the section names, in .shstrtab; a section header names its own *) |
| 146 | ShNames = "\0.text\0.rodata\0.bss\0.symtab\0.strtab\0.shstrtab\0.dynamic\0.dynstr\0.dynsym\0.debug_line\0.debug_info\0.debug_abbrev\0"; |
| 147 | |
| 148 | (* Where in ShNames the name is: the byte after a zero that starts it and |
| 149 | is followed by one; the empty name, found nowhere, is 0 *) |
| 150 | proc nameAt(name: []const U8): INT; |
| 151 | var i, k: INT; same: BOOL; |
| 152 | begin |
| 153 | for i := 1 to LEN(ShNames) - LEN(name) - 1 do |
| 154 | if ShNames[i - 1] = 0 then |
| 155 | same := ShNames[i + LEN(name)] = 0; |
| 156 | for k := 0 to LEN(name) - 1 do |
| 157 | if ShNames[i + k] # name[k] then same := false; end; |
| 158 | end; |
| 159 | if same and (LEN(name) > 0) then return i; end; |
| 160 | end; |
| 161 | end; |
| 162 | return 0; |
| 163 | end; |
| 164 | |
| 165 | (* A section header. Its name is stored as its offset in ShNames. *) |
| 166 | proc shdr(name: []const U8; typ: INT; flags, adr, off, size: INT; lnk, info: INT; align, entsize: INT); |
| 167 | begin |
| 168 | putN(nameAt(name), 4); putN(typ, 4); putN(flags, 8); putN(adr, 8); |
| 169 | putN(off, 8); putN(size, 8); putN(lnk, 4); putN(info, 4); |
| 170 | putN(align, 8); putN(entsize, 8); |
| 171 | end; |
| 172 | |
| 173 | proc putStr(s: []const U8); |
| 174 | begin |
| 175 | putBytes(SYSTEM.PTR(s), LEN(s)); |
| 176 | putN(0, 1); |
| 177 | end; |
| 178 | |
| 179 | (* The compile unit, which names the line table, and its abbreviation |
| 180 | (Link makes them); the table itself is in the places, in rodata. |
| 181 | Section offsets go in off, sizes in size: line, info, abbrev. *) |
| 182 | proc putDebug(off, size: []INT); |
| 183 | var info, abbrev: []const U8; |
| 184 | begin |
| 185 | off[0] := Chunk.chunks[Link.placesChunk].adr - Link.roAdr + Link.roOff + Link.linesOff; |
| 186 | size[0] := Link.linesSize; |
| 187 | info := Link.debugInfo(mainPath); |
| 188 | off[1] := len; |
| 189 | putBytes(SYSTEM.PTR(info), LEN(info)); |
| 190 | size[1] := LEN(info); |
| 191 | abbrev := Link.debugAbbrev(); |
| 192 | off[2] := len; |
| 193 | putBytes(SYSTEM.PTR(abbrev), LEN(abbrev)); |
| 194 | size[2] := LEN(abbrev); |
| 195 | end; |
| 196 | |
| 197 | const |
| 198 | (* program header types *) |
| 199 | PtLoad = 1; PtDynamic = 2; PtInterp = 3; PtGnuStack = 6474E551H; |
| 200 | |
| 201 | var |
| 202 | (* in the room at the end of rodata: where each of the loader's |
| 203 | tables starts, from extraAdr *) |
| 204 | symAt, relaAt, hashAt, strAt: INT; |
| 205 | nsym, strLen: INT; |
| 206 | |
| 207 | (* Whether chunk c is a C symbol the program uses *) |
| 208 | proc isExt(c: INT): BOOL; |
| 209 | begin |
| 210 | return Chunk.chunks[c].live and Chunk.chunks[c].ext; |
| 211 | end; |
| 212 | |
| 213 | (* The sizes of the loader's tables: what linkAll must leave room for. *) |
| 214 | proc sizeTables(var roExtra, dynSize: INT); |
| 215 | var c, i: INT; |
| 216 | begin |
| 217 | nsym := 1; |
| 218 | strLen := 1; |
| 219 | for i := 0 to numNeeded - 1 do strLen := strLen + LEN(Scan.nameOf(needed[i])) + 1; end; |
| 220 | for c := 0 to Chunk.numChunks - 1 do |
| 221 | if isExt(c) then |
| 222 | INC(nsym); |
| 223 | strLen := strLen + LEN(Scan.nameOf(Chunk.chunks[c].name)) + 1; |
| 224 | end; |
| 225 | end; |
| 226 | symAt := (LEN(interp) + 1 + 7) div 8 * 8; |
| 227 | relaAt := symAt + nsym * 24; |
| 228 | hashAt := relaAt + (nsym - 1) * 24; |
| 229 | strAt := hashAt + 4 * (3 + nsym); |
| 230 | roExtra := strAt + strLen; |
| 231 | dynSize := (numNeeded + 10) * 16; |
| 232 | end; |
| 233 | |
| 234 | (* The loader's tables in the room at the end of rodata, which the |
| 235 | buffer already holds as zeros: the loader's name; the symbols, one |
| 236 | for each C symbol used; a GLOB_DAT relocation for each, at its |
| 237 | quadword; a hash table of one bucket; and their names. *) |
| 238 | proc putTables; |
| 239 | var c, k, i, at, name: INT; |
| 240 | begin |
| 241 | len := Link.extraAdr - Link.roAdr + Link.roOff; |
| 242 | putStr(interp); |
| 243 | padTo(Link.extraAdr - Link.roAdr + Link.roOff + symAt); |
| 244 | putN(0, 24); |
| 245 | name := strLen; |
| 246 | for c := Chunk.numChunks - 1 to 0 by -1 do (* names go last in the string table *) |
| 247 | if isExt(c) then name := name - LEN(Scan.nameOf(Chunk.chunks[c].name)) - 1; end; |
| 248 | end; |
| 249 | for c := 0 to Chunk.numChunks - 1 do |
| 250 | if isExt(c) then |
| 251 | putN(name, 4); putN(10H, 1); putN(0, 1); putN(0, 2); (* global, undefined *) |
| 252 | putN(0, 8); putN(0, 8); |
| 253 | name := name + LEN(Scan.nameOf(Chunk.chunks[c].name)) + 1; |
| 254 | end; |
| 255 | end; |
| 256 | k := 1; |
| 257 | for c := 0 to Chunk.numChunks - 1 do |
| 258 | if isExt(c) then |
| 259 | putN(Chunk.chunks[c].adr, 8); |
| 260 | putN((k shl 32) | 6, 8); (* R_X86_64_GLOB_DAT *) |
| 261 | putN(0, 8); |
| 262 | INC(k); |
| 263 | end; |
| 264 | end; |
| 265 | putN(1, 4); putN(nsym, 4); putN(nsym - 1, 4); (* the bucket chains every symbol *) |
| 266 | putN(0, 4); |
| 267 | for i := 1 to nsym - 1 do putN(i - 1, 4); end; |
| 268 | putN(0, 1); |
| 269 | for i := 0 to numNeeded - 1 do putStr(Scan.nameOf(needed[i])); end; |
| 270 | for c := 0 to Chunk.numChunks - 1 do |
| 271 | if isExt(c) then putStr(Scan.nameOf(Chunk.chunks[c].name)); end; |
| 272 | end; |
| 273 | len := Link.roOff + Link.roSize; |
| 274 | |
| 275 | (* the dynamic table, at the start of the writable segment *) |
| 276 | padTo(Link.bssOff); |
| 277 | at := 1; |
| 278 | for i := 0 to numNeeded - 1 do |
| 279 | putN(1, 8); putN(at, 8); (* DT_NEEDED *) |
| 280 | at := at + LEN(Scan.nameOf(needed[i])) + 1; |
| 281 | end; |
| 282 | putN(4, 8); putN(Link.extraAdr + hashAt, 8); (* DT_HASH *) |
| 283 | putN(5, 8); putN(Link.extraAdr + strAt, 8); (* DT_STRTAB *) |
| 284 | putN(6, 8); putN(Link.extraAdr + symAt, 8); (* DT_SYMTAB *) |
| 285 | putN(10, 8); putN(strLen, 8); (* DT_STRSZ *) |
| 286 | putN(11, 8); putN(24, 8); (* DT_SYMENT *) |
| 287 | putN(7, 8); putN(Link.extraAdr + relaAt, 8); (* DT_RELA *) |
| 288 | putN(8, 8); putN((nsym - 1) * 24, 8); (* DT_RELASZ *) |
| 289 | putN(9, 8); putN(24, 8); (* DT_RELAENT *) |
| 290 | putN(21, 8); putN(0, 8); (* DT_DEBUG, for gdb *) |
| 291 | putN(0, 16); (* DT_NULL *) |
| 292 | end; |
| 293 | |
| 294 | proc write*(path: []const U8; entry: INT); |
| 295 | var |
| 296 | nph, nsh, c, nsyms, sec, strsCap: INT; |
| 297 | roExtra, dynSize, symOff, strOff, shstrOff, shOff, strsLen: INT; |
| 298 | dbgOff, dbgSize: [3]INT; |
| 299 | strs: ^U8; |
| 300 | s: []const U8; |
| 301 | dyn: BOOL; |
| 302 | begin |
| 303 | cap := 1 shl 16; |
| 304 | buf := Scan.grab(cap); |
| 305 | len := 0; |
| 306 | dyn := numNeeded > 0; |
| 307 | roExtra := 0; dynSize := 0; |
| 308 | if dyn then sizeTables(roExtra, dynSize); end; |
| 309 | (* room for the most program headers either kind of program has *) |
| 310 | Link.linkAll(64 + 6 * 56, roExtra, dynSize); |
| 311 | if dyn then |
| 312 | nph := 6; |
| 313 | else |
| 314 | nph := 2; |
| 315 | if Link.roSize > 0 then INC(nph); end; |
| 316 | if Link.bssSize > 0 then INC(nph); end; |
| 317 | end; |
| 318 | |
| 319 | (* ELF header *) |
| 320 | putN(464C457FH, 4); (* #127 'ELF' *) |
| 321 | putN(2, 1); putN(1, 1); putN(1, 1); putN(0, 9); (* 64-bit, little-endian, version 1 *) |
| 322 | putN(2, 2); (* ET_EXEC *) |
| 323 | putN(62, 2); (* EM_X86_64 *) |
| 324 | putN(1, 4); |
| 325 | putN(Chunk.chunks[entry].adr, 8); |
| 326 | putN(64, 8); (* program headers follow *) |
| 327 | putN(0, 8); (* section headers: patched below *) |
| 328 | putN(0, 4); |
| 329 | nsh := 10; |
| 330 | if dyn then nsh := 13; end; |
| 331 | putN(64, 2); putN(56, 2); putN(nph, 2); |
| 332 | putN(64, 2); putN(nsh, 2); putN(6, 2); (* .shstrtab is section 6 *) |
| 333 | |
| 334 | (* flags: 4 read, 5 read and run, 6 read and write *) |
| 335 | if dyn then |
| 336 | phdr(PtInterp, 4, Link.extraAdr - Link.roAdr + Link.roOff, Link.extraAdr, LEN(interp) + 1, LEN(interp) + 1, 1); |
| 337 | end; |
| 338 | (* the first segment maps the headers too, so text can follow them *) |
| 339 | phdr(PtLoad, 5, 0, Link.base, Link.textOff + Link.textSize, Link.textOff + Link.textSize, Link.Page); |
| 340 | if Link.roSize > 0 then phdr(PtLoad, 4, Link.roOff, Link.roAdr, Link.roSize, Link.roSize, Link.Page); end; |
| 341 | if dyn then |
| 342 | phdr(PtLoad, 6, Link.bssOff, Link.bssAdr, dynSize, Link.bssSize, Link.Page); |
| 343 | phdr(PtDynamic, 6, Link.bssOff, Link.bssAdr, dynSize, dynSize, 8); |
| 344 | elsif Link.bssSize > 0 then |
| 345 | phdr(PtLoad, 6, 0, Link.bssAdr, 0, Link.bssSize, Link.Page); |
| 346 | end; |
| 347 | phdr(PtGnuStack, 6, 0, 0, 0, 0, 16); (* a stack that can't run code *) |
| 348 | |
| 349 | padTo(Link.textOff); |
| 350 | putBytes(Link.textImg, Link.textSize); |
| 351 | padTo(Link.roOff); (* even when empty, so that every section lies in the file *) |
| 352 | putBytes(Link.roImg, Link.roSize); |
| 353 | if dyn then putTables; end; |
| 354 | |
| 355 | (* the symbol table: one local symbol for each named live chunk *) |
| 356 | padTo((len + 7) div 8 * 8); |
| 357 | symOff := len; |
| 358 | strsCap := 1 shl 16; |
| 359 | strs := Scan.grab(strsCap); |
| 360 | strsLen := 1; |
| 361 | strs[0] := 0; |
| 362 | putN(0, 24); |
| 363 | nsyms := 1; |
| 364 | for c := 0 to Chunk.numChunks - 1 do |
| 365 | if not stripped and Chunk.chunks[c].live and (Chunk.chunks[c].name >= 0) and not Chunk.chunks[c].ext then |
| 366 | s := Chunk.chunkName(c); |
| 367 | if strsLen + LEN(s) + 1 > strsCap then |
| 368 | strs := Scan.regrab(strs, strsLen, strsLen * 2 + 256); |
| 369 | strsCap := strsLen * 2 + 256; |
| 370 | end; |
| 371 | COPY(strs[strsLen..strsLen + LEN(s)], s); |
| 372 | strs[strsLen + LEN(s)] := 0; |
| 373 | if Chunk.chunks[c].sec = Chunk.secText then (* the section's index in the headers below *) |
| 374 | sec := 1; |
| 375 | elsif Chunk.chunks[c].sec = Chunk.secRodata then |
| 376 | sec := 2; |
| 377 | else |
| 378 | sec := 3; |
| 379 | end; |
| 380 | putN(strsLen, 4); |
| 381 | if sec = 1 then putN(2, 1); else putN(1, 1); end; (* STT_FUNC, STT_OBJECT *) |
| 382 | putN(0, 1); |
| 383 | putN(sec, 2); |
| 384 | putN(Chunk.chunks[c].adr, 8); |
| 385 | putN(Chunk.chunks[c].size, 8); |
| 386 | strsLen := strsLen + LEN(s) + 1; |
| 387 | INC(nsyms); |
| 388 | end; |
| 389 | end; |
| 390 | strOff := len; |
| 391 | putBytes(strs, strsLen); |
| 392 | shstrOff := len; |
| 393 | putBytes(SYSTEM.PTR(ShNames), LEN(ShNames)); |
| 394 | if stripped or (Link.placesChunk < 0) then |
| 395 | for c := 0 to 2 do dbgOff[c] := len; dbgSize[c] := 0; end; |
| 396 | else |
| 397 | putDebug(dbgOff, dbgSize); |
| 398 | end; |
| 399 | |
| 400 | padTo((len + 7) div 8 * 8); |
| 401 | shOff := len; |
| 402 | patch(40, shOff, 8); |
| 403 | (* types: 1 PROGBITS, 2 SYMTAB, 3 STRTAB, 6 DYNAMIC, 8 NOBITS, 11 DYNSYM; |
| 404 | flags: 2 ALLOC, 3 WRITE+ALLOC, 6 ALLOC+EXECINSTR. A symbol table's |
| 405 | link is its string table's index: 5 .strtab, 8 .dynstr. The order |
| 406 | here is the index the code above relies on. *) |
| 407 | shdr("", 0, 0, 0, 0, 0, 0, 0, 0, 0); |
| 408 | shdr(".text", 1, 6, Link.base + Link.textOff, Link.textOff, Link.textSize, 0, 0, 16, 0); |
| 409 | shdr(".rodata", 1, 2, Link.roAdr, Link.roOff, Link.roSize, 0, 0, 16, 0); |
| 410 | shdr(".bss", 8, 3, Link.bssAdr + dynSize, Link.roOff + Link.roSize, Link.bssSize - dynSize, 0, 0, 16, 0); |
| 411 | shdr(".symtab", 2, 0, 0, symOff, nsyms * 24, 5, nsyms, 8, 24); |
| 412 | shdr(".strtab", 3, 0, 0, strOff, strsLen, 0, 0, 1, 0); |
| 413 | shdr(".shstrtab", 3, 0, 0, shstrOff, LEN(ShNames), 0, 0, 1, 0); |
| 414 | if dyn then (* the loader's tables, for readelf and objdump *) |
| 415 | shdr(".dynamic", 6, 3, Link.bssAdr, Link.bssOff, dynSize, 8, 0, 8, 16); |
| 416 | shdr(".dynstr", 3, 2, Link.extraAdr + strAt, Link.extraAdr + strAt - Link.roAdr + Link.roOff, strLen, 0, 0, 1, 0); |
| 417 | shdr(".dynsym", 11, 2, Link.extraAdr + symAt, Link.extraAdr + symAt - Link.roAdr + Link.roOff, nsym * 24, 8, 1, 8, 24); |
| 418 | end; |
| 419 | shdr(".debug_line", 1, 0, 0, dbgOff[0], dbgSize[0], 0, 0, 1, 0); |
| 420 | shdr(".debug_info", 1, 0, 0, dbgOff[1], dbgSize[1], 0, 0, 1, 0); |
| 421 | shdr(".debug_abbrev", 1, 0, 0, dbgOff[2], dbgSize[2], 0, 0, 1, 0); |
| 422 | |
| 423 | if not Host.writeExecutable(path, buf[0..len]) then |
| 424 | Diag.fatal(Diag.say("can't write {}", [Fmt.str(path)])); |
| 425 | end; |
| 426 | end; |
| 427 | |
| 428 | begin |
| 429 | mainPath := ""; |
| 430 | end Exe. |