| 1 | (* Exe: the executable file, for macOS on arm64: Mach-O. |
| 2 | |
| 3 | Exe.linux.raz, for Linux, is ELF; a target has one Exe, chosen by the |
| 4 | suffix of its file (doc/raz.md, 3), and no other unit knows the |
| 5 | format. This one writes a Mach-O executable as dyld, macOS's dynamic |
| 6 | loader, takes one, and the kernel runs one on arm64: |
| 7 | |
| 8 | the header and load commands |
| 9 | __PAGEZERO 4 GB at address 0, no access: arm64 macOS runs no |
| 10 | program without it, so a nil pointer faults |
| 11 | __TEXT the header again, and the code, read and run |
| 12 | __DATA_CONST rodata, made read-only once dyld has moved the |
| 13 | addresses in it (rebases) |
| 14 | __DATA the variables, zeros, and C's symbols' quadwords, which |
| 15 | dyld fills in (binds) |
| 16 | __DWARF the line numbers lldb reads (below), in the file only |
| 17 | __LINKEDIT what dyld reads, unmapped by the program: the rebases |
| 18 | and binds, the symbol table, and the code signature |
| 19 | |
| 20 | Each segment starts on a page of 16 KB, in the file as in memory, at |
| 21 | its offset in the file from __TEXT's address (Link.setPaged). macOS |
| 22 | loads a program at an address of its own choosing (MH_PIE), so every |
| 23 | address held in data is listed for dyld to move with it: those in |
| 24 | rodata, which Link lists (Link.rebases); code makes its addresses by |
| 25 | adrp from its own. Every program is linked to libSystem, through |
| 26 | which a Mac's program reaches the kernel; C's symbols are bound from |
| 27 | it, as Linux's GLOB_DAT does it, into quadwords the code reads. The |
| 28 | rebases and binds are dyld's opcodes (LC_DYLD_INFO_ONLY), the |
| 29 | plainest form dyld still takes: a few bytes for each address, where |
| 30 | chained fixups would thread them through the data with tables to say |
| 31 | where each chain starts. arm64 macOS runs nothing unsigned, so the |
| 32 | file ends with an ad-hoc code signature: a SHA-256 hash of each of |
| 33 | its pages of 4 KB, in a CodeDirectory, which the kernel checks as it |
| 34 | maps them. |
| 35 | |
| 36 | Line numbers are DWARF 4, the same as Linux's: Link makes the line |
| 37 | table (in the places, in rodata, where Rt reads it too) and the one |
| 38 | compile unit; here they are copied into __DWARF, a segment of three |
| 39 | sections, __debug_line, __debug_info and __debug_abbrev, which lldb |
| 40 | finds there as it does Go's. dyld refuses a segment bigger in the file |
| 41 | than in memory, so it is mapped, read only, and never touched: its |
| 42 | pages are read only if the program is stopped in lldb. It lies before __LINKEDIT, which must stay last, for the signature. |
| 43 | -s leaves it out, and the symbols. |
| 44 | |
| 45 | Libraries. Every program loads libSystem, first. A program that says |
| 46 | `link "name";` loads that library too, by the path libPath makes of |
| 47 | the name (below), and every C symbol is bound by flat lookup, as the |
| 48 | ordinal -2 (BIND_SPECIAL_DYLIB_FLAT_LOOKUP): dyld looks for it in all |
| 49 | the libraries the program loaded, in order, as Linux's loader does. |
| 50 | The compiler doesn't know which library defines a symbol, as `extern` |
| 51 | doesn't say, and the two-level namespace wants it to say; flat lookup |
| 52 | is how a Mac's program says "wherever it is". |
| 53 | |
| 54 | Depends on: Link for the images and addresses, Chunk for the chunks, |
| 55 | Scan for names and memory, Diag for errors, Host to write the file. |
| 56 | Leaves out: an export trie (the program exports nothing). |
| 57 | |
| 58 | Exported variables can't be set from outside, so needLib, setMusl, |
| 59 | setMainPath and setStripped do it. *) |
| 60 | module Exe; |
| 61 | import Host, Fmt, Diag, Scan, Chunk, Link; |
| 62 | import SYSTEM; |
| 63 | |
| 64 | const |
| 65 | Os* = "darwin"; (* the target's operating system, as a file's suffix says it *) |
| 66 | Base = 100000000H; (* __TEXT's address, past __PAGEZERO; dyld moves it *) |
| 67 | Page = 4000H; (* arm64 macOS's page, 16 KB, on which each segment starts *) |
| 68 | HashPage = 1000H; (* the signature hashes pages of 4 KB *) |
| 69 | Dyld = "/usr/lib/dyld"; |
| 70 | Rpaths: [4][]const U8 = ["@executable_path", "/usr/local/lib", "/opt/homebrew/lib", "/usr/lib"]; |
| 71 | (* room for the header and the load commands but the libraries': see |
| 72 | write. The most segments: __PAGEZERO, __TEXT, __DATA_CONST, __DATA, |
| 73 | __DWARF (three sections), __LINKEDIT; then dyld info, symbols and |
| 74 | the rest; the four rpaths of libPath. *) |
| 75 | HeaderRoom = 32 + 72 + 3 * 152 + (72 + 3 * 80) + 72 + 48 + 24 + 80 + 32 + 24 + 24 + 24 + 16 |
| 76 | + 32 + 32 + 32 + 24; |
| 77 | |
| 78 | var |
| 79 | needed: [32]INT; (* the libraries to load, names interned *) |
| 80 | numNeeded*: INT; |
| 81 | stripped*: BOOL; (* -s: no symbols *) |
| 82 | libc*: []const U8; (* the C library, libSystem *) |
| 83 | mainPath: []const U8; (* the main module's file, which lldb will name *) |
| 84 | |
| 85 | buf: ^U8; |
| 86 | len, cap: INT; |
| 87 | |
| 88 | (* The library name, to be loaded, unless it is already: false if there |
| 89 | are too many *) |
| 90 | proc needLib*(name: INT): BOOL; |
| 91 | var i: INT; |
| 92 | begin |
| 93 | for i := 0 to numNeeded - 1 do |
| 94 | if needed[i] = name then return true; end; |
| 95 | end; |
| 96 | if numNeeded = 32 then return false; end; |
| 97 | needed[numNeeded] := name; |
| 98 | INC(numNeeded); |
| 99 | return true; |
| 100 | end; |
| 101 | |
| 102 | proc noLibs*; |
| 103 | begin |
| 104 | numNeeded := 0; |
| 105 | end; |
| 106 | |
| 107 | (* -libc chooses Linux's C library; a Mac has one, libSystem *) |
| 108 | proc setMusl*(yes: BOOL); |
| 109 | begin |
| 110 | if yes then Diag.fatal("-libc musl: a program for macOS is linked to libSystem"); end; |
| 111 | end; |
| 112 | |
| 113 | (* The main module's file, which line numbers will name *) |
| 114 | proc setMainPath*(path: []const U8); |
| 115 | begin |
| 116 | mainPath := path; |
| 117 | end; |
| 118 | |
| 119 | (* The path a `link` name loads from. A name with a / is a path, used as |
| 120 | it is (it may start with @executable_path/). Otherwise the name is a |
| 121 | library's, written as on Linux or without its decoration: "X11", |
| 122 | "libX11" and "libX11.so.6" all mean libX11.dylib, the name cut at its |
| 123 | first dot, "lib" put before it if it isn't there; a name that ends in |
| 124 | .dylib is kept whole. The file is then looked for, at run time, by |
| 125 | @rpath, which the program gives four places: its own directory, so |
| 126 | that a program can ship its libraries, /usr/local/lib and |
| 127 | /opt/homebrew/lib, where Homebrew keeps them, and /usr/lib. Nothing |
| 128 | is looked for when compiling, so a program can be compiled where its |
| 129 | libraries are not, as for another machine. *) |
| 130 | proc libPath(name: []const U8): []const U8; |
| 131 | var i, stem: INT; |
| 132 | begin |
| 133 | stem := 0; |
| 134 | for i := LEN(name) - 1 to 0 by -1 do |
| 135 | if name[i] = '/' then return name; end; |
| 136 | if name[i] = '.' then stem := i; end; |
| 137 | end; |
| 138 | if (LEN(name) > 6) and (name[LEN(name) - 6..LEN(name)] = ".dylib") then |
| 139 | return Diag.say("@rpath/{}", [Fmt.str(name)]); |
| 140 | end; |
| 141 | if stem = 0 then stem := LEN(name); end; |
| 142 | if (stem > 3) and (name[0..3] = "lib") then return Diag.say("@rpath/{}.dylib", [Fmt.str(name[0..stem])]); end; |
| 143 | return Diag.say("@rpath/lib{}.dylib", [Fmt.str(name[0..stem])]); |
| 144 | end; |
| 145 | |
| 146 | proc setStripped*(yes: BOOL); |
| 147 | begin |
| 148 | stripped := yes; |
| 149 | end; |
| 150 | |
| 151 | proc room(n: INT); |
| 152 | begin |
| 153 | if len + n > cap then |
| 154 | buf := Scan.regrab(buf, len, cap * 2 + n); |
| 155 | cap := cap * 2 + n; |
| 156 | end; |
| 157 | end; |
| 158 | |
| 159 | (* The n low bytes of v, least significant first, as Mach-O is *) |
| 160 | proc putN(v: INT; n: INT); |
| 161 | var i: INT; |
| 162 | begin |
| 163 | room(n); |
| 164 | for i := 0 to n - 1 do |
| 165 | buf[len] := U8(v & 0FFH); |
| 166 | v := v shr 8; |
| 167 | INC(len); |
| 168 | end; |
| 169 | end; |
| 170 | |
| 171 | (* The n low bytes of v, most significant first, as the signature is *) |
| 172 | proc putBE(v: INT; n: INT); |
| 173 | var i: INT; |
| 174 | begin |
| 175 | for i := n - 1 to 0 by -1 do putN(v shr (8 * i), 1); end; |
| 176 | end; |
| 177 | |
| 178 | proc putBytes(p: ^U8; n: INT); |
| 179 | begin |
| 180 | room(n); |
| 181 | if n > 0 then COPY(buf[len..len + n], p[0..n]); end; |
| 182 | len := len + n; |
| 183 | end; |
| 184 | |
| 185 | proc padTo(off: INT); |
| 186 | begin |
| 187 | while len < off do putN(0, 1); end; |
| 188 | end; |
| 189 | |
| 190 | (* s and a zero byte *) |
| 191 | proc putStr(s: []const U8); |
| 192 | begin |
| 193 | putBytes(SYSTEM.PTR(s), LEN(s)); |
| 194 | putN(0, 1); |
| 195 | end; |
| 196 | |
| 197 | (* s in a field of n bytes, the rest zeros, as a segment's name *) |
| 198 | proc putName(s: []const U8; n: INT); |
| 199 | var at: INT; |
| 200 | begin |
| 201 | at := len; |
| 202 | putBytes(SYSTEM.PTR(s), LEN(s)); |
| 203 | padTo(at + n); |
| 204 | end; |
| 205 | |
| 206 | (* LEB128, unsigned, as dyld's opcodes take their operands *) |
| 207 | proc putU(v: INT); |
| 208 | begin |
| 209 | while v >= 80H do |
| 210 | putN(v & 7FH | 80H, 1); |
| 211 | v := v shr 7; |
| 212 | end; |
| 213 | putN(v, 1); |
| 214 | end; |
| 215 | |
| 216 | (* n, up to a multiple of m *) |
| 217 | proc roundUp(n, m: INT): INT; |
| 218 | begin |
| 219 | return (n + m - 1) div m * m; |
| 220 | end; |
| 221 | |
| 222 | (* SHA-256 (FIPS 180-4), for the signature's hashes: the message in |
| 223 | blocks of 64 bytes, the last ones padded with a one bit, zeros and |
| 224 | the message's length in bits *) |
| 225 | |
| 226 | const |
| 227 | (* the first 32 bits of the cube roots of the first 64 primes *) |
| 228 | K: [64]U32 = [ |
| 229 | 0428A2F98H, 071374491H, 0B5C0FBCFH, 0E9B5DBA5H, 03956C25BH, 059F111F1H, 0923F82A4H, 0AB1C5ED5H, |
| 230 | 0D807AA98H, 012835B01H, 0243185BEH, 0550C7DC3H, 072BE5D74H, 080DEB1FEH, 09BDC06A7H, 0C19BF174H, |
| 231 | 0E49B69C1H, 0EFBE4786H, 00FC19DC6H, 0240CA1CCH, 02DE92C6FH, 04A7484AAH, 05CB0A9DCH, 076F988DAH, |
| 232 | 0983E5152H, 0A831C66DH, 0B00327C8H, 0BF597FC7H, 0C6E00BF3H, 0D5A79147H, 006CA6351H, 014292967H, |
| 233 | 027B70A85H, 02E1B2138H, 04D2C6DFCH, 053380D13H, 0650A7354H, 0766A0ABBH, 081C2C92EH, 092722C85H, |
| 234 | 0A2BFE8A1H, 0A81A664BH, 0C24B8B70H, 0C76C51A3H, 0D192E819H, 0D6990624H, 0F40E3585H, 0106AA070H, |
| 235 | 019A4C116H, 01E376C08H, 02748774CH, 034B0BCB5H, 0391C0CB3H, 04ED8AA4AH, 05B9CCA4FH, 0682E6FF3H, |
| 236 | 0748F82EEH, 078A5636FH, 084C87814H, 08CC70208H, 090BEFFFAH, 0A4506CEBH, 0BEF9A3F7H, 0C67178F2H]; |
| 237 | |
| 238 | type Hash = [8]U32; |
| 239 | |
| 240 | (* Mixes the 64 bytes p into the running hash h *) |
| 241 | proc block(var h: Hash; p: []const U8); |
| 242 | var w: [64]U32; v: Hash; t1, t2: U32; i: INT; |
| 243 | begin |
| 244 | for i := 0 to 15 do |
| 245 | w[i] := (U32(p[4 * i]) shl 24) | (U32(p[4 * i + 1]) shl 16) | (U32(p[4 * i + 2]) shl 8) | U32(p[4 * i + 3]); |
| 246 | end; |
| 247 | for i := 16 to 63 do |
| 248 | w[i] := w[i - 16] + (ROR(w[i - 15], 7) xor ROR(w[i - 15], 18) xor (w[i - 15] shr 3)) |
| 249 | + w[i - 7] + (ROR(w[i - 2], 17) xor ROR(w[i - 2], 19) xor (w[i - 2] shr 10)); |
| 250 | end; |
| 251 | v := h; (* a to h, as the standard names them, are v[0] to v[7] *) |
| 252 | for i := 0 to 63 do |
| 253 | t1 := v[7] + (ROR(v[4], 6) xor ROR(v[4], 11) xor ROR(v[4], 25)) + ((v[4] & v[5]) xor (~v[4] & v[6])) + K[i] + w[i]; |
| 254 | t2 := (ROR(v[0], 2) xor ROR(v[0], 13) xor ROR(v[0], 22)) + ((v[0] & v[1]) xor (v[0] & v[2]) xor (v[1] & v[2])); |
| 255 | v[7] := v[6]; v[6] := v[5]; v[5] := v[4]; v[4] := v[3] + t1; |
| 256 | v[3] := v[2]; v[2] := v[1]; v[1] := v[0]; v[0] := t1 + t2; |
| 257 | end; |
| 258 | for i := 0 to 7 do h[i] := h[i] + v[i]; end; |
| 259 | end; |
| 260 | |
| 261 | (* The SHA-256 of data, its 32 bytes put at the end of buf *) |
| 262 | proc putHash(data: []const U8); |
| 263 | var h: Hash; tail: [128]U8; whole, rest, n, i: INT; |
| 264 | begin |
| 265 | h := [06A09E667H, 0BB67AE85H, 03C6EF372H, 0A54FF53AH, 0510E527FH, 09B05688CH, 01F83D9ABH, 05BE0CD19H]; |
| 266 | whole := LEN(data) div 64 * 64; |
| 267 | for i := 0 to whole - 64 by 64 do block(h, data[i..i + 64]); end; |
| 268 | rest := LEN(data) - whole; |
| 269 | FILL(tail, 0); |
| 270 | COPY(tail[0..rest], data[whole..LEN(data)]); |
| 271 | tail[rest] := 80H; |
| 272 | n := 64; |
| 273 | if rest >= 56 then n := 128; end; |
| 274 | for i := 0 to 7 do tail[n - 1 - i] := U8(((LEN(data) * 8) shr (8 * i)) & 0FFH); end; |
| 275 | for i := 0 to n - 64 by 64 do block(h, tail[i..i + 64]); end; |
| 276 | for i := 0 to 7 do putBE(INT(h[i]), 4); end; |
| 277 | end; |
| 278 | |
| 279 | (* Load commands *) |
| 280 | |
| 281 | const |
| 282 | LcSegment = 19H; LcSymtab = 2H; LcDysymtab = 0BH; LcLoadDylinker = 0EH; LcLoadDylib = 0CH; |
| 283 | LcUuid = 1BH; LcBuildVersion = 32H; LcMain = 80000028H; LcDyldInfoOnly = 80000022H; |
| 284 | LcCodeSignature = 1DH; LcRpath = 8000001CH; |
| 285 | |
| 286 | (* A segment of 64 bits, followed by its nsects sections; prot is the |
| 287 | access, 1 read, 2 write, 4 run *) |
| 288 | proc segment(name: []const U8; adr, vmSize, off, fileSize, prot, nsects, flags: INT); |
| 289 | begin |
| 290 | putN(LcSegment, 4); putN(72 + 80 * nsects, 4); |
| 291 | putName(name, 16); |
| 292 | putN(adr, 8); putN(vmSize, 8); (* where it is loaded, and its size there *) |
| 293 | putN(off, 8); putN(fileSize, 8); (* where it is in the file, and its size there *) |
| 294 | putN(prot, 4); putN(prot, 4); (* the most access it may have, and what it has *) |
| 295 | putN(nsects, 4); putN(flags, 4); |
| 296 | end; |
| 297 | |
| 298 | (* A section, what otool and lldb see of a segment: its name, its |
| 299 | segment's, address, size, file offset, alignment as a power of two, |
| 300 | no relocations, flags *) |
| 301 | proc section(name, seg: []const U8; adr, size, off, align, flags: INT); |
| 302 | begin |
| 303 | putName(name, 16); putName(seg, 16); |
| 304 | putN(adr, 8); putN(size, 8); |
| 305 | putN(off, 4); putN(align, 4); |
| 306 | putN(0, 4); putN(0, 4); (* relocations: none *) |
| 307 | putN(flags, 4); |
| 308 | putN(0, 12); (* reserved *) |
| 309 | end; |
| 310 | |
| 311 | (* Whether chunk c is a live C symbol, and a live chunk with a name the |
| 312 | symbol table gives *) |
| 313 | proc isExt(c: INT): BOOL; |
| 314 | begin |
| 315 | return Chunk.chunks[c].live and Chunk.chunks[c].ext; |
| 316 | end; |
| 317 | |
| 318 | proc isNamed(c: INT): BOOL; |
| 319 | begin |
| 320 | return not stripped and Chunk.chunks[c].live and (Chunk.chunks[c].name >= 0) and not Chunk.chunks[c].ext; |
| 321 | end; |
| 322 | |
| 323 | proc write*(path: []const U8; entry: INT); |
| 324 | var |
| 325 | c, i, n, textSize, leOff, leAdr, ncmds, segRo, segData, sectData: INT; |
| 326 | rebaseOff, rebaseSize, bindOff, bindSize, symOff, nsyms, strOff, strSize, sigOff, sigSize, slots: INT; |
| 327 | ident: []const U8; |
| 328 | uuidAt, end0, sigRoom: INT; |
| 329 | dwarfOff, dwarfSize, dwarfAdr, libsRoom: INT; |
| 330 | dbgOff, dbgSize: [3]INT; |
| 331 | libs: [33][]const U8; (* the paths to load, libSystem first *) |
| 332 | nlibs: INT; |
| 333 | debug: BOOL; |
| 334 | begin |
| 335 | if Link.base # 400000H then Diag.fatal("-base: macOS loads a program where it chooses"); end; |
| 336 | libs[0] := libc; |
| 337 | nlibs := 1; |
| 338 | libsRoom := roundUp(24 + LEN(libc) + 1, 8); |
| 339 | for i := 0 to numNeeded - 1 do |
| 340 | libs[nlibs] := libPath(Scan.nameOf(needed[i])); |
| 341 | n := 0; (* several names may mean one library: "m" and "libm.so.6" *) |
| 342 | while libs[n] # libs[nlibs] do INC(n); end; |
| 343 | if n = nlibs then (* new *) |
| 344 | libsRoom := libsRoom + roundUp(24 + LEN(libs[nlibs]) + 1, 8); |
| 345 | INC(nlibs); |
| 346 | end; |
| 347 | end; |
| 348 | debug := not stripped and (Link.placesChunk >= 0); |
| 349 | cap := 1 shl 16; |
| 350 | buf := Scan.grab(cap); |
| 351 | len := 0; |
| 352 | Link.setBase(Base); |
| 353 | Link.setPaged(Page); |
| 354 | Link.linkAll(HeaderRoom + libsRoom, 0, 0); |
| 355 | textSize := roundUp(Link.textOff + Link.textSize, Page); (* __TEXT's, the header in it *) |
| 356 | dwarfOff := Link.bssOff; (* the next page in the file: __DATA has no bytes there *) |
| 357 | leAdr := Link.bssAdr + roundUp(Link.bssSize, Page); |
| 358 | dwarfAdr := leAdr; |
| 359 | (* the segments' numbers, as the opcodes name them, and the |
| 360 | sections', as the symbols do: __PAGEZERO 0, __TEXT 1 (__text, 1) *) |
| 361 | segRo := 2; |
| 362 | segData := 2; |
| 363 | sectData := 2; |
| 364 | if Link.roSize > 0 then INC(segData); INC(sectData); end; |
| 365 | |
| 366 | (* the code and rodata, after the room for the header *) |
| 367 | padTo(Link.textOff); |
| 368 | putBytes(Link.textImg, Link.textSize); |
| 369 | padTo(Link.roOff); |
| 370 | putBytes(Link.roImg, Link.roSize); |
| 371 | padTo(dwarfOff); |
| 372 | |
| 373 | (* the line numbers: the line table, copied from the places, then the |
| 374 | compile unit and its abbreviation; each section's file offset and size *) |
| 375 | if debug then |
| 376 | dbgOff[0] := len; |
| 377 | dbgSize[0] := Link.linesSize; |
| 378 | putBytes(Link.roImg + (Chunk.chunks[Link.placesChunk].adr - Link.roAdr + Link.linesOff), Link.linesSize); |
| 379 | ident := Link.debugInfo(mainPath); |
| 380 | dbgOff[1] := len; dbgSize[1] := LEN(ident); |
| 381 | putBytes(SYSTEM.PTR(ident), LEN(ident)); |
| 382 | ident := Link.debugAbbrev(); |
| 383 | dbgOff[2] := len; dbgSize[2] := LEN(ident); |
| 384 | putBytes(SYSTEM.PTR(ident), LEN(ident)); |
| 385 | end; |
| 386 | dwarfSize := len - dwarfOff; |
| 387 | leOff := roundUp(len, Page); (* __LINKEDIT, a page of its own *) |
| 388 | leAdr := leAdr + roundUp(dwarfSize, Page); (* in memory, after __DWARF *) |
| 389 | padTo(leOff); |
| 390 | |
| 391 | (* rebases: each word in rodata that holds an address, a pointer |
| 392 | (type 1), at its segment and offset; then done *) |
| 393 | rebaseOff := len; |
| 394 | putN(11H, 1); (* REBASE_OPCODE_SET_TYPE_IMM, pointer *) |
| 395 | for i := 0 to Link.numRebases - 1 do |
| 396 | putN(20H | segRo, 1); putU(Link.rebases[i] - Link.roAdr); (* SET_SEGMENT_AND_OFFSET_ULEB *) |
| 397 | putN(51H, 1); (* DO_REBASE_IMM_TIMES, once *) |
| 398 | end; |
| 399 | putN(0, 1); (* DONE *) |
| 400 | padTo(roundUp(len, 8)); |
| 401 | rebaseSize := len - rebaseOff; |
| 402 | |
| 403 | (* binds: each C symbol's quadword, by flat lookup (above), by its |
| 404 | name, which in Mach-O begins with _ *) |
| 405 | bindOff := len; |
| 406 | putN(3EH, 1); (* BIND_OPCODE_SET_DYLIB_SPECIAL_IMM, -2 *) |
| 407 | putN(51H, 1); (* SET_TYPE_IMM, pointer *) |
| 408 | for c := 0 to Chunk.numChunks - 1 do |
| 409 | if isExt(c) then |
| 410 | putN(40H, 1); (* SET_SYMBOL_TRAILING_FLAGS_IMM, none *) |
| 411 | putN(INT('_'), 1); putStr(Scan.nameOf(Chunk.chunks[c].name)); |
| 412 | putN(70H | segData, 1); putU(Chunk.chunks[c].adr - Link.bssAdr); (* SET_SEGMENT_AND_OFFSET_ULEB *) |
| 413 | putN(90H, 1); (* DO_BIND *) |
| 414 | end; |
| 415 | end; |
| 416 | putN(0, 1); (* DONE *) |
| 417 | padTo(roundUp(len, 8)); |
| 418 | bindSize := len - bindOff; |
| 419 | |
| 420 | (* the symbol table: a local symbol (N_SECT, 0EH) for each named |
| 421 | live chunk, nlist_64: its name's offset, type, section, no |
| 422 | description, its address; the names follow, after a 0 *) |
| 423 | symOff := len; |
| 424 | nsyms := 0; |
| 425 | strSize := 1; |
| 426 | for c := 0 to Chunk.numChunks - 1 do |
| 427 | if isNamed(c) then |
| 428 | putN(strSize, 4); |
| 429 | putN(0EH, 1); |
| 430 | match Chunk.chunks[c].sec of |
| 431 | | secText: putN(1, 1); |
| 432 | | secRodata: putN(2, 1); |
| 433 | | secBss: putN(sectData, 1); |
| 434 | end; |
| 435 | putN(0, 2); |
| 436 | putN(Chunk.chunks[c].adr, 8); |
| 437 | strSize := strSize + LEN(Chunk.chunkName(c)) + 1; |
| 438 | INC(nsyms); |
| 439 | end; |
| 440 | end; |
| 441 | strOff := len; |
| 442 | putN(0, 1); |
| 443 | for c := 0 to Chunk.numChunks - 1 do |
| 444 | if isNamed(c) then putStr(Chunk.chunkName(c)); end; |
| 445 | end; |
| 446 | padTo(roundUp(len, 16)); |
| 447 | strSize := len - strOff; |
| 448 | |
| 449 | (* the signature's size: a SuperBlob of one blob, the CodeDirectory, |
| 450 | of 88 bytes, the program's name, and a hash of each page up to |
| 451 | the signature *) |
| 452 | sigOff := len; |
| 453 | i := LEN(path); |
| 454 | while (i > 0) and (path[i - 1] # '/') do DEC(i); end; |
| 455 | ident := path[i..LEN(path)]; |
| 456 | slots := roundUp(sigOff, HashPage) div HashPage; |
| 457 | sigSize := 12 + 8 + 88 + LEN(ident) + 1 + 32 * slots; |
| 458 | sigRoom := roundUp(sigSize, 16); |
| 459 | room(sigRoom); |
| 460 | |
| 461 | (* The header, in the room left for it, and the load commands. The |
| 462 | UUID, which names this build for lldb, is the first 16 bytes of |
| 463 | the hash of what follows the header. *) |
| 464 | end0 := len; |
| 465 | len := 0; |
| 466 | putN(0FEEDFACFH, 4); (* MH_MAGIC_64 *) |
| 467 | putN(0100000CH, 4); putN(0, 4); (* CPU_TYPE_ARM64, any of them *) |
| 468 | putN(2, 4); (* MH_EXECUTE *) |
| 469 | putN(0, 4); putN(0, 4); (* the commands' count and size, patched below *) |
| 470 | putN(200085H, 4); (* MH_PIE, TWOLEVEL, DYLDLINK, NOUNDEFS *) |
| 471 | putN(0, 4); |
| 472 | ncmds := 0; |
| 473 | segment("__PAGEZERO", 0, Base, 0, 0, 0, 0, 0); INC(ncmds); |
| 474 | segment("__TEXT", Base, textSize, 0, textSize, 5, 1, 0); INC(ncmds); |
| 475 | section("__text", "__TEXT", Base + Link.textOff, Link.textSize, Link.textOff, 5, 80000400H); (* instructions only *) |
| 476 | if Link.roSize > 0 then |
| 477 | (* SG_READ_ONLY, 10H: dyld takes write away once it has rebased *) |
| 478 | segment("__DATA_CONST", Link.roAdr, roundUp(Link.roSize, Page), Link.roOff, roundUp(Link.roSize, Page), 3, 1, 10H); INC(ncmds); |
| 479 | section("__const", "__DATA_CONST", Link.roAdr, Link.roSize, Link.roOff, 3, 0); |
| 480 | end; |
| 481 | if Link.bssSize > 0 then |
| 482 | segment("__DATA", Link.bssAdr, roundUp(Link.bssSize, Page), 0, 0, 3, 1, 0); INC(ncmds); |
| 483 | section("__bss", "__DATA", Link.bssAdr, Link.bssSize, 0, 3, 1); (* S_ZEROFILL *) |
| 484 | end; |
| 485 | if debug then |
| 486 | (* read only, flags 0 *) |
| 487 | segment("__DWARF", dwarfAdr, roundUp(dwarfSize, Page), dwarfOff, dwarfSize, 1, 3, 0); INC(ncmds); |
| 488 | section("__debug_line", "__DWARF", dwarfAdr + dbgOff[0] - dwarfOff, dbgSize[0], dbgOff[0], 0, 02000000H); (* S_ATTR_DEBUG *) |
| 489 | section("__debug_info", "__DWARF", dwarfAdr + dbgOff[1] - dwarfOff, dbgSize[1], dbgOff[1], 0, 02000000H); |
| 490 | section("__debug_abbrev", "__DWARF", dwarfAdr + dbgOff[2] - dwarfOff, dbgSize[2], dbgOff[2], 0, 02000000H); |
| 491 | end; |
| 492 | segment("__LINKEDIT", leAdr, roundUp(sigOff + sigRoom - leOff, Page), leOff, sigOff + sigRoom - leOff, 1, 0, 0); INC(ncmds); |
| 493 | putN(LcDyldInfoOnly, 4); putN(48, 4); |
| 494 | putN(rebaseOff, 4); putN(rebaseSize, 4); putN(bindOff, 4); putN(bindSize, 4); |
| 495 | putN(0, 24); (* weak and lazy binds, exports: none *) |
| 496 | INC(ncmds); |
| 497 | putN(LcSymtab, 4); putN(24, 4); putN(symOff, 4); putN(nsyms, 4); putN(strOff, 4); putN(strSize, 4); INC(ncmds); |
| 498 | (* every symbol is local; none is defined for others or wanted *) |
| 499 | putN(LcDysymtab, 4); putN(80, 4); putN(0, 4); putN(nsyms, 4); putN(nsyms, 4); putN(0, 4); |
| 500 | putN(nsyms, 4); putN(0, 4); putN(0, 48); INC(ncmds); |
| 501 | putN(LcLoadDylinker, 4); putN(32, 4); putN(12, 4); putName(Dyld, 20); INC(ncmds); |
| 502 | putN(LcUuid, 4); putN(24, 4); uuidAt := len; putN(0, 16); INC(ncmds); |
| 503 | putN(LcBuildVersion, 4); putN(24, 4); |
| 504 | putN(1, 4); putN(0B0000H, 4); putN(0B0000H, 4); putN(0, 4); (* macOS, 11.0 and up, SDK 11.0, no tools *) |
| 505 | INC(ncmds); |
| 506 | putN(LcMain, 4); putN(24, 4); putN(Chunk.chunks[entry].adr - Base, 8); putN(0, 8); INC(ncmds); |
| 507 | (* each library, version 1.0.0 and compatible with 1.0.0, which any is |
| 508 | (dyld checks those of the library's own; the timestamp is 2) *) |
| 509 | for i := 0 to nlibs - 1 do |
| 510 | n := roundUp(24 + LEN(libs[i]) + 1, 8); |
| 511 | putN(LcLoadDylib, 4); putN(n, 4); putN(24, 4); putN(2, 4); putN(10000H, 4); putN(10000H, 4); |
| 512 | putName(libs[i], n - 24); INC(ncmds); |
| 513 | end; |
| 514 | (* where @rpath looks, in order (libPath) *) |
| 515 | for i := 0 to 3 do |
| 516 | n := roundUp(12 + LEN(Rpaths[i]) + 1, 8); |
| 517 | putN(LcRpath, 4); putN(n, 4); putN(12, 4); putName(Rpaths[i], n - 12); INC(ncmds); |
| 518 | end; |
| 519 | putN(LcCodeSignature, 4); putN(16, 4); putN(sigOff, 4); putN(sigRoom, 4); INC(ncmds); |
| 520 | if len > HeaderRoom + libsRoom then Diag.fatal("internal error: the Mach-O header outgrew its room"); end; |
| 521 | i := len; |
| 522 | len := 16; putN(ncmds, 4); putN(i - 32, 4); |
| 523 | len := end0; |
| 524 | putHash(buf[Link.textOff..sigOff]); |
| 525 | COPY(buf[uuidAt..uuidAt + 16], buf[len - 32..len - 16]); |
| 526 | len := sigOff; |
| 527 | |
| 528 | (* the signature, big-endian: the SuperBlob (CSMAGIC_EMBEDDED_SIGNATURE), |
| 529 | its length and count, the index of its one blob, the |
| 530 | CodeDirectory, at 20 *) |
| 531 | putBE(0FADE0CC0H, 4); putBE(sigSize, 4); putBE(1, 4); |
| 532 | putBE(0, 4); putBE(20, 4); (* CSSLOT_CODEDIRECTORY *) |
| 533 | putBE(0FADE0C02H, 4); putBE(sigSize - 20, 4); (* CSMAGIC_CODEDIRECTORY *) |
| 534 | putBE(20400H, 4); (* the version with the executable segment *) |
| 535 | putBE(20002H, 4); (* ad hoc, and the linker's: no identity behind it *) |
| 536 | putBE(88 + LEN(ident) + 1, 4); putBE(88, 4); (* where the hashes and the name are *) |
| 537 | putBE(0, 4); putBE(slots, 4); (* no special slots; a slot a page *) |
| 538 | putBE(sigOff, 4); (* what is hashed: all before the signature *) |
| 539 | putN(32, 1); putN(2, 1); putN(0, 1); putN(12, 1); (* SHA-256's 32 bytes; no platform; pages of 2^12 *) |
| 540 | putN(0, 4 + 4 + 4 + 4 + 8); (* no scatter, no team, a 32-bit limit *) |
| 541 | putBE(0, 8); putBE(textSize, 8); (* the executable segment: __TEXT, from 0 *) |
| 542 | putBE(1, 8); (* CS_EXECSEG_MAIN_BINARY *) |
| 543 | putStr(ident); |
| 544 | for i := 0 to slots - 1 do |
| 545 | n := (i + 1) * HashPage; |
| 546 | if n > sigOff then n := sigOff; end; |
| 547 | putHash(buf[i * HashPage..n]); |
| 548 | end; |
| 549 | padTo(sigOff + sigRoom); |
| 550 | |
| 551 | if not Host.writeExecutable(path, buf[0..len]) then |
| 552 | Diag.fatal(Diag.say("can't write {}", [Fmt.str(path)])); |
| 553 | end; |
| 554 | end; |
| 555 | |
| 556 | begin |
| 557 | mainPath := ""; |
| 558 | libc := "/usr/lib/libSystem.B.dylib"; |
| 559 | end Exe. |