-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLinker.Mod
More file actions
668 lines (605 loc) · 13.3 KB
/
Linker.Mod
File metadata and controls
668 lines (605 loc) · 13.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
MODULE Linker;
IMPORT Out, Strings, Files, Machine, Errors := MocErrors, E := Emitter,
Generator, Table, Tree, SYSTEM;
CONST
codeSpace = Generator.maxCodeLen; (** Maximum code length *)
dataSpace = Generator.maxDataLen; (** Maximum data length *)
TYPE
HUGEINT = SYSTEM.INT64;
VAR
fname: ARRAY 256 OF CHAR;
F: Files.File;
r: Files.Rider;
codeLen: INTEGER;
dataLen: INTEGER;
(** Outputs 1 byte to the file. *)
PROCEDURE Byte(x: BYTE);
BEGIN
Files.Write(r, x)
END Byte;
(** Outputs 2 bytes to the file. Little-endian.*)
PROCEDURE Word(x: INTEGER);
BEGIN
Byte(x MOD 100H);
Byte(x DIV 100H MOD 100H)
END Word;
(** Outputs a double word = 4 bytes to the file. Little-endian.*)
PROCEDURE DWord(x: INTEGER);
BEGIN
Byte(x MOD 100H);
Byte(x DIV 100H MOD 100H);
Byte(x DIV 10000H MOD 100H);
Byte(x DIV 1000000H MOD 100H)
END DWord;
(** Outputs a quadruple word = 8 bytes to the file. Little-endian.*)
PROCEDURE QWord(x: HUGEINT);
BEGIN
Byte(x MOD 100H);
Byte(x DIV 100H MOD 100H);
Byte(x DIV 10000H MOD 100H);
Byte(x DIV 1000000H MOD 100H);
Byte(x DIV 1000000H DIV 100H MOD 100H);
Byte(x DIV 1000000H DIV 10000H MOD 100H);
Byte(x DIV 1000000H DIV 1000000H MOD 100H);
Byte(x DIV 1000000H DIV 1000000H DIV 100H MOD 100H)
END QWord;
(** Outputs a set as 4 bytes to the file. Little-endian.*)
PROCEDURE Set(x: SET);
BEGIN
DWord(ORD(x))
END Set;
(** Outputs string to the file (one byte per character) and then
zero-bytes until total of n bytes is reached.
If n is smaller than the string, the string is not truncated. *)
PROCEDURE String(s: ARRAY OF CHAR; n: INTEGER);
VAR i: INTEGER;
BEGIN
i := 0;
WHILE s[i] # 0X DO
Byte(ORD(s[i]));
INC(i)
END;
WHILE i < n DO
Byte(0);
INC(i)
END
END String;
(** Outputs n zero bytes to the file. *)
PROCEDURE Zeros(n: INTEGER);
BEGIN
WHILE n > 0 DO
Byte(0);
DEC(n)
END
END Zeros;
(** Outputs first 32 bytes - the Mach-O header to the file. *)
PROCEDURE Header;
BEGIN
(* FEED-FACF Magic Number, little-endian *)
Byte(0CFH); Byte(0FAH); Byte(0EDH); Byte(0FEH);
(* CPU Type = ARM64 *)
DWord(100000CH);
(* CPU Subtype = ARM64_ALL *)
DWord(0);
(* File Type = MH_EXECUTE *)
DWord(2);
(* Number of Load Commands *)
DWord(16);
(* Size of Load Commands in bytes *)
DWord(744-16+152); (* 152 is data segment command *)
(* Flags = {NOUNDEFS, DYLDLINK, TWOLEVEL, PIE} *)
Set({0, 2, 7, 21});
(* Reserved = 0 *)
DWord(0)
END Header;
PROCEDURE Command0;
BEGIN
(* Command LC_SEGMENT_64 *)
DWord(19H);
(* Command Size in bytes *)
DWord(72);
(* Segment Name *)
String('__PAGEZERO', 16);
(* VM Address *)
QWord(0);
(* VM Size *)
QWord(1000000H * 100H); (* 4 GB *)
(* File Offset *)
QWord(0);
(* File Size *)
QWord(0);
(* Maximum VM Protection *)
DWord(0);
(* Initial VM Protection *)
DWord(0);
(* Number of Sections *)
DWord(0);
(* Flags *)
Set({})
END Command0;
PROCEDURE Command1;
PROCEDURE Section0;
BEGIN
(* Section Name *)
String('__text', 16);
(* Segment Name *)
String('__TEXT', 16);
(* Address *)
QWord(1000000H * 100H + 4000H - codeSpace);
(* Size in bytes *)
QWord(codeLen);
(* Offset *)
DWord(4000H - codeSpace);
(* Alignment = 2^2 = 4 *)
DWord(2);
(* Relocations Offset *)
DWord(0);
(* Number of Relocations *)
DWord(0);
(* Flags = S_ATTR_SOME_INSTRUCTIONS + S_ATTR_PURE_INSTRUCTIONS *)
Set({10, 31});
(* Reserved *)
DWord(0);
DWord(0);
DWord(0)
END Section0;
PROCEDURE Section1;
BEGIN
(* Section Name *)
String('__unwind_info', 16);
(* Segment Name *)
String('__TEXT', 16);
(* Address *)
QWord(1000000H * 100H + 4000H - codeSpace + codeLen);
(* Size in bytes *)
QWord(codeSpace - codeLen);
(* Offset *)
DWord(4000H - codeSpace + codeLen);
(* Alignment = 2^2 = 4*)
DWord(2);
(* Relocations Offset *)
DWord(0);
(* Number of Relocations *)
DWord(0);
(* Flags = S_REGULAR (empty set) *)
Set({});
(* Reserved *)
DWord(0);
DWord(0);
DWord(0)
END Section1;
BEGIN (* Command1 *)
(* Command LC_SEGMENT_64 *)
DWord(19H);
(* Command Size in bytes *)
DWord(232);
(* Segment Name *)
String('__TEXT', 16);
(* VM Address *)
QWord(1000000H * 100H); (* 4 GB *)
(* VM Size *)
QWord(4000H);
(* File Offset *)
QWord(0);
(* File Size *)
QWord(4000H);
(* Maximum VM Protection = VM_PROT_READ, VM_PROT_EXECUTE *)
Set({0, 2});
(* Initial VM Protection = VM_PROT_READ, VM_PROT_EXECUTE *)
Set({0, 2});
(* Number of Sections *)
DWord(2);
(* Flags *)
Set({});
(* Sections *)
Section0;
Section1
END Command1;
PROCEDURE Command2;
PROCEDURE Section0;
BEGIN
(* Section Name *)
String('__data', 16);
(* Segment Name *)
String('__DATA', 16);
(* Address *)
QWord(100004H * 1000H);
(* Size in bytes *)
QWord(dataLen);
(* Offset *)
DWord(4000H);
(* Alignment = 2^0 = 1 *)
DWord(0);
(* Relocations Offset *)
DWord(0);
(* Number of Relocations *)
DWord(0);
(* Flags = S_REGULAR *)
Set({});
(* Reserved *)
DWord(0);
DWord(0);
DWord(0)
END Section0;
BEGIN (* Command2 *)
(* Command LC_SEGMENT_64 *)
DWord(19H);
(* Command Size in bytes *)
DWord(152);
(* Segment Name *)
String('__DATA', 16);
(* VM Address *)
QWord(100004H * 1000H); (* 4 GB + D, where D = 16 KB *)
(* VM Size *)
QWord(4000H); (* D = 16 KB *)
(* File Offset *)
QWord(4000H); (* 16 KB *)
(* File Size *)
QWord(4000H); (* D = 16 KB *)
(* Maximum VM Protection = VM_PROT_READ, VM_PROT_WRITE *)
Set({0, 1});
(* Initial VM Protection = VM_PROT_READ, VM_PROT_WRITE *)
Set({0, 1});
(* Number of Sections *)
DWord(1);
(* Flags *)
Set({});
(* Sections *)
Section0
END Command2;
PROCEDURE Command3;
BEGIN
(* Command LC_SEGMENT_64 *)
DWord(19H);
(* Command Size in bytes *)
DWord(72);
(* Segment Name *)
String('__LINKEDIT', 16);
(* VM Address *)
QWord(100000H * 1000H + 8000H);
(* VM Size *)
QWord(4000H); (* 16 KB *)
(* File Offset *)
QWord(8000H); (* 32 KB *)
(* File Size *)
QWord(176);
(* Maximum VM Protection *)
DWord(1);
(* Initial VM Protection *)
DWord(1);
(* Number of Sections *)
DWord(0);
(* Flags *)
Set({})
END Command3;
PROCEDURE Command4;
BEGIN
(* Command LC_DYLD_CHAINED_FIXUPS *)
(*DWord(34H);*) (* FIXME 34 00 00 80, what is 8 for? *)
Byte(34H); Byte(0); Byte(0); Byte(80H);
(* Command Size in bytes *)
DWord(16);
(* Data Offset *)
DWord(8000H);
(* Data Size *)
DWord(56)
END Command4;
PROCEDURE Command5;
BEGIN
(* Command LC_DYLD_EXPORTS_TRIE *)
(*DWord(33H);*) (* FIXME what is 8 for? *)
Byte(33H); Byte(0); Byte(0); Byte(80H);
(* Command Size in bytes *)
DWord(16);
(* Data Offset *)
DWord(8000H + 56);
(* Data Size *)
DWord(48)
END Command5;
PROCEDURE Command6;
BEGIN
(* Command LC_SYMTAB *)
DWord(2);
(* Command Size in bytes *)
DWord(24);
(* Symbol Table Offset, 8 bytes contain the procedure starts table, Seg. 13 *)
DWord(8000H + 56 + 48 + 8); (* 8000H + 112 *)
(* Number of Symbols *)
DWord(2);
(* String Table Offset *)
DWord(8000H + 56 + 48 + 8 + 32); (* 8000H + 144 *)
(* String Table Size *)
DWord(32)
END Command6;
PROCEDURE Command7;
BEGIN
(* Command LC_DYSYMTAB *)
DWord(0BH);
(* Command Size in bytes *)
DWord(80);
(* LocSymbol Index *)
DWord(0);
(* LocSymbol Number *)
DWord(0);
(* Defined ExtSymbol Index *)
DWord(0);
(* Defined ExtSymbol Number *)
DWord(2);
(* Undefined ExtSymbol Index *)
DWord(2);
(* Undefined ExtSymbol Number *)
DWord(0);
(* TOC (Table of Contents) Offset *)
DWord(0);
(* TOC Entries *)
DWord(0);
(* Module Table Offset *)
DWord(0);
(* Module Table Entries *)
DWord(0);
(* ExtRef Table Offset *)
DWord(0);
(* ExtRef Table Entries *)
DWord(0);
(* IndSym Table Offset *)
DWord(0);
(* IndSym Table Entries *)
DWord(0);
(* ExtReloc Table Offset *)
DWord(0);
(* ExtReloc Table Entries *)
DWord(0);
(* LocReloc Table Offset *)
DWord(0);
(* LocReloc Table Entries *)
DWord(0)
END Command7;
PROCEDURE Command8;
BEGIN
(* Command LC_LOAD_DYLINKER *)
DWord(0EH);
(* Command Size in bytes *)
DWord(32);
(* String Offset *)
DWord(12);
(* Name *)
String('/usr/lib/dyld', 20)
END Command8;
PROCEDURE Command9;
BEGIN
(* Command LC_UUID *)
DWord(1BH);
(* Command Size in bytes *)
DWord(24);
(* UUID, 16 bytes, TODO generate random *)
Word(09A45H); Word(0ADB9H); Word(02E6CH); Word(09B32H);
Word(0E48AH); Word(06EECH); Word(07BCDH); Word(0EC6DH)
END Command9;
PROCEDURE Command10;
BEGIN
(* Command LC_BUILD_VERSION *)
DWord(32H);
(* Command Size in bytes *)
DWord(32);
(* Platform = MacOS *)
DWord(1);
(* Minimum OS Version = 15.0.0 *)
DWord(0F0000H);
(* SDK Version, 0 = undefined *)
DWord(0);
(* Tool Count - number of elements in the array below *)
DWord(1);
(* Array of Tools, 1 element *)
(* Tool Type = linker ld *)
DWord(3);
(* Tool Version = 1115.7.3 *)
DWord(45B0703H)
END Command10;
PROCEDURE Command11;
BEGIN
(* Command LC_SOURCE_VERSION *)
DWord(2AH);
(* Command Size in bytes *)
DWord(16);
(* Version = 0.0 *)
QWord(0)
END Command11;
PROCEDURE Command12;
BEGIN
(* Command LC_MAIN *)
Byte(28H); Byte(0); Byte(0); Byte(80H);
(* Command Size in bytes *)
DWord(24);
(* Entry Offset *)
QWord(4000H - codeSpace);
(* Initial Stack Size, may be 0 *)
QWord(0)
END Command12;
PROCEDURE Command13;
BEGIN
(* Command LC_LOAD_DYLIB *)
DWord(0CH);
(* Command Size in bytes *)
DWord(56);
(* String Offset *)
DWord(24);
(* Time Stamp = 1970-01-01 00:00:02 *)
DWord(2);
(* Current Version = 1351.0.0 *)
DWord(5470000H);
(* Compatibility Version *)
DWord(10000H);
(* Name *)
String('/usr/lib/libSystem.B.dylib', 32)
END Command13;
PROCEDURE Command14;
BEGIN
(* Command LC_FUNCTION_STARTS *)
DWord(26H);
(* Command Size in bytes *)
DWord(16);
(* Data Offset *)
DWord(8000H + 104);
(* Data Size *)
DWord(8)
END Command14;
PROCEDURE Command15;
BEGIN
(* Command LC_DATA_IN_CODE *)
DWord(29H);
(* Command Size in bytes *)
DWord(16);
(* Data Offset *)
DWord(8000H + 112);
(* Data Size *)
DWord(0)
END Command15;
(** Outputs Mach-O commands to the file. *)
PROCEDURE Commands;
BEGIN
Command0;
Command1;
Command2;
Command3;
Command4;
Command5;
Command6;
Command7;
Command8;
Command9;
Command10;
Command11;
Command12;
Command13;
Command14;
Command15
END Commands;
(** Outputs 56 bytes to the file. *)
PROCEDURE ChainedFixups;
BEGIN
(* Fixups Version *)
DWord(0);
(* Starts Offset *)
DWord(20H);
(* Imports Offset *)
DWord(30H);
(* Symbols Offset *)
DWord(30H);
(* Imports Count *)
DWord(0);
(* Imports Format *)
DWord(1);
(* Symbols Format *)
DWord(0);
(* Starts in Image *)
(* Segment Count *)
DWord(0);
(* Segment Info Offset - DWord array (empty) *)
(* Starts in Segment *)
(* Size *)
DWord(3);
(* Page Size *)
DWord(0);
(* Pointer Format *)
DWord(0);
(* Segment Offset *)
DWord(0);
(* Maximum Valid Pointer *)
DWord(0);
(* Page Count *)
Word(0);
(* Page Start - DWord array *)
Word(0)
END ChainedFixups;
(** Outputs 48 bytes to the file. *)
PROCEDURE ExportsTrie;
BEGIN
Byte(0); Byte(1); Byte(5FH); Byte(0);
Byte(12H); Byte(0); Byte(0); Byte(0);
Byte(0); Byte(2); Byte(0); Byte(0);
Byte(0); Byte(3); Byte(0); Byte(9CH);
Byte(7FH); Byte(0); Byte(0); Byte(2);
String('_mh_execute_header', 0);
Byte(0); Byte(9);
String('start', 0);
Byte(0); Byte(0DH); Byte(0)
END ExportsTrie;
PROCEDURE FunctionStarts;
BEGIN
Byte(9CH); Byte(7FH); Word(0); DWord(0)
END FunctionStarts;
PROCEDURE SymbolTable;
BEGIN
(* Proper Symbol Table *)
DWord(2); DWord(10010FH); DWord(0); DWord(1);
DWord(16H); DWord(10FH); DWord(3F9CH); DWord(1);
(* Name Strings *)
Byte(20H); Byte(0);
String('__mh_execute_header', 0); Byte(0);
String('_start', 0); DWord(0)
END SymbolTable;
PROCEDURE Output;
BEGIN
Header;
Commands;
(* Zero-out until position 4000H - codeSpace *)
Zeros(4000H - codeSpace - Files.Pos(r));
(* Machine code *)
Generator.OutputCode(r);
(* Unwind Info *)
Zeros(codeSpace - codeLen);
(* Data (global variables) *)
Generator.OutputData(r);
Zeros(4000H - dataLen - 1);
Byte(0AH); (* For Out.Ln *)
ChainedFixups;
ExportsTrie;
FunctionStarts;
SymbolTable
END Output;
PROCEDURE OpenOutputFile;
BEGIN
F := Files.New(fname);
IF F = NIL THEN
Machine.Error(Errors.outputFile)
ELSE
Files.Set(r, F, 0)
END
END OpenOutputFile;
PROCEDURE ChmodAndSign;
VAR s: ARRAY 1024 OF CHAR;
err: INTEGER;
BEGIN
(* Chmod *)
s := 'chmod +x ';
Strings.Append(fname, s);
err := Machine.Exec(s);
IF err # 0 THEN
Machine.Error(Errors.chmodFailed)
ELSE
(* Sign *)
s := 'codesign -s - -f --timestamp=none ';
Strings.Append(fname, s);
err := Machine.Exec(s);
IF err # 0 THEN
Machine.Error(Errors.signFailed)
END
END
END ChmodAndSign;
PROCEDURE Link*(exeFname: ARRAY OF CHAR);
BEGIN
Strings.Copy(exeFname, fname);
codeLen := Generator.CodeLength();
dataLen := Generator.DataLength();
OpenOutputFile;
IF ~Machine.hadErrors THEN
Output;
IF Machine.hadErrors THEN
Files.Close(F)
ELSE
Files.Register(F);
ChmodAndSign
END
END
END Link;
END Linker.