forked from difcareer/010templates
-
Notifications
You must be signed in to change notification settings - Fork 3
/
MachOTemplate_1.bt
1685 lines (1549 loc) · 53.9 KB
/
MachOTemplate_1.bt
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
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//--------------------------------------
//--- 010 Editor v3.2.2 Binary Template
//
// File: MachOTemplate.bt
// Author: Tim "diff" Strazzere
// <[email protected]>
// <[email protected]>
// Revision: 1.5.7
// Purpose: Quick template for parsing Macho-o binaries.
//--------------------------------------
//
//
// Version 1.5.7 (2018-03-17)
// - Add DynamicSymbolTable
// - Add FunctionStarts
// - Update StringTable
// - Update SymbolTable
// - Support UPX 3.9 packed macho.
//
//
// Version 1.5.4 (2016-04-26)
// - Add LoadCommands of :
// - LC_VERSION_MIN_TVOS
// - LC_VERSION_MIN_WATCHOS
//
//
// Version 1.5.6 (2016-10-25
// - Fix Section64 offset type
// Version 1.5.4 (2016-08-10):
// - Fix a parsing bug for single-architecture mach-o files.
// - Add ARM64 CpuType
//
// Version 1.5.3 (2015-09-02)
// - WIP, adding lots of stuff :D
//
// Version 1.5.2 (2015-08-28)
// - Add CpuSubType structs
//
// Version 1.5.1 (2015-08-26)
// - Add extra LoadCommands which where missing;
// - LC_ENCRYPTION_INFO_64
// - LC_LINKER_OPTION
// - LC_OPTIMIZATION_HUNT
//
// Version 1.5 (2015-08-26)
// - Add enum for cpu type
// - Remove constants and constant hack
//
// Version 1.4 (2015-03-31)
// - Added parsing for data of CODE_SIGNATURE (by Claud Xiao <[email protected]>)
//
// Version 1.3.1 (2014-05-20)
// - Spot fix for PowerPC arch issues, the endianness is not being set
// properly. Now the template can "complete" but that section is currently
// a bit useless as there appears to be a bug in 010Editor Templating engine.
// The endianness is not being propagated to the rest of the structures - need
// to find a work around for this. Thank you to Joxean Koret for reporting this
// issue.
//
// Version 1.3 (2013-10-07)
// - Fixed MAIN load command.
// - Fixed support/parsing for DULIB_CODE_SIGN_DRS.
// - Added SOURCE_VERSION parsing.
// - Fixed MIN_(MAC/IPHONE) version commands
// - Fixed some typos and spacing issues.
//
// Version 1.2 (2013-02-08)
// - Fixed LoadCommand for ARM thread handling
// - Added LoadCommand handling for DATA_IN_CODE
// - Finished optimization true|false categorization for structs, also did size optimzations
// so the template should run faster
// - Removed some TODO's related to pretty-printing stuff
// - Grouped the LoadCommands together in lists for easier use
// - Added enum for known CryptId in the EncryptionInfo LoadCommand
//
// Version 1.1 (2013-02-07)
// - Minimum version load command now properly outputs the format for better readability
// - Added a readvalue function for the header, helps understand headers at a glance
//
// Version 1.0 (2013-02-06)
// - Correctly parses FAT headers and will continue to parse the rest of the combined
// binary
// - Added many todo's to make the output more pretty
// - Fixed some broken LoadCommands (64bit ones mainly), will gracefully fail if unknown
// LoadCommand is hit
// - Found some bugs in 010Editor and added fixes to try to avoid those
//
// Version 0.1 (2013-02-05)
// - First stab it this, lots of issues - FAT binaries don't work at all
//
//
typedef struct {
ubyte val <comment="uleb128 element">;
if(val > 0x7f) {
ubyte val <comment="uleb128 element">;
if (val > 0x7f) {
ubyte val <comment="uleb128 element">;
if(val > 0x7f) {
ubyte val <comment="uleb128 element">;
if(val > 0x7f) {
ubyte val <comment="uleb128 element">;
}
}
}
}
} uleb128 <read=ULeb128Read, comment="Unsigned little-endian base 128 value">;
// get the actual uint value of the uleb128
uint uleb128_value(uleb128 &u) {
local uint result;
local ubyte cur;
result = u.val[0];
if(result > 0x7f) {
cur = u.val[1];
result = (result & 0x7f) | (uint)((cur & 0x7f) << 7);
if(cur > 0x7f) {
cur = u.val[2];
result |= (uint)(cur & 0x7f) << 14;
if(cur > 0x7f) {
cur = u.val[3];
result |= (uint)(cur & 0x7f) << 21;
if(cur > 0x7f) {
cur = u.val[4];
result |= (uint)cur << 28;
}
}
}
}
return result;
}
typedef struct uleb128 uleb128p1;
int uleb128p1_value(uleb128 &u) {
return (int)uleb128_value(u) - 1;
}
string ULeb128Read(uleb128 &u) {
local string s;
s = SPrintf(s, "0x%X", uleb128_value(u));
return s;
}
// Mach-o's should be Little Endian only -- except for the fat_header/fat_arch
LittleEndian();
typedef enum <uint> {
MACHO_32 = 0xFEEDFACE, // 32-bit mach object file
MACHO_64 = 0xFEEDFACF, // 64-bit mach object file
MACHO_FAT = 0xCAFEBABE, // Universal object file / FAT_MAGIC
MACHO_FAT_CIGAM = 0xBEBAFECA
} Magic <format=hex>;
// http://www.opensource.apple.com/source/xnu/xnu-792.13.8/osfmk/mach/machine.h
typedef enum <uint32> {
VAX = 0x1,
/* SKIP 0x2 */
/* SKIP 0x3 */
/* SKIP 0x4 */
/* SKIP 0x5 */
MC680x0 = 0x6,
X86 = 0x7,
I386 = 0x7,
X86_64 = (0x7 | 0x01000000),
MIPS = 0x8,
/* SKIP 0x9 */
MC98000 = 0xA,
HPPA = 0xB,
ARM = 0xC,
ARM64 = (0xC | 0x01000000),
MC88000 = 0xD,
SPARC = 0xE,
I860 = 0xF,
ALPHA = 0x10,
/* SKIP 0x11 */
POWERPC = 0x12,
POWERPC64 = (0x12 | 0x01000000)
} CpuType;
/* Enums can't start with a number, prefix with _ */
typedef enum <uint32> {
I386_ALL = 0x3,
_386 = 0x3,
_486 = 0x4,
_486SX = 0x84,
_586 = 0x5,
PENT = 0x5,
PENTPRO = 0x16,
PENTII_M3 = 0x36,
PENTII_M5 = 0x56,
CELERON = 0x67,
CELERON_MOBILE = 0x77,
PENTIUM_3 = 0x08,
PENTIUM_3_M = 0x18,
PENTIUM_3_XEON = 0x28,
PENTIUM_M = 0x09,
PENTIUM_4 = 0x0A,
PENTIUM_4_M = 0x1A,
ITANIUM = 0x0B,
ITANIUM_2 = 0x1B,
XEON = 0x0C,
XEON_MP = 0x1C,
X86_ALL = 0x3,
X86_64_ALL = 0x3,
X86_ARCH1 = 0x4,
X86_64_H = 0x8
} CpuSubTypeX86 <format=hex>;
typedef enum <uint32> {
INTEL_MODEL_ALL = 0x0,
INTEL_FAMILY_MAX = 0xF
} CpuSubTypeIntel <format=hex>;
typedef enum <uint32> {
ARM_ALL = 0x0,
/* SKIP 0x1 */
/* SKIP 0x2 */
/* SKIP 0x3 */
/* SKIP 0x4 */
ARM_V4T = 0x5,
ARM_V6 = 0x6,
ARM_V5 = 0x7,
ARM_V5TEJ = 0x7,
ARM_XSCALE = 0x8,
ARM_V7 = 0x9,
/* ARM_V7F (unused) 0xA */
ARM_V7S = 0xB,
ARM_V7K = 0xC,
/* SKIP 0xD */
ARM_V6M = 0xE,
ARM_V7M = 0xF,
ARM_V7EM = 0x10
} CpuSubTypeARM <format=hex>;
typedef enum <uint32> {
ARM64_ALL = 0x0,
ARM64_V8 = 0x1
} CpuSubTypeARM64 <format=hex>;
typedef enum <uint32> {
SPARC_ALL = 0x0
} CpuSubTypeSPARC <format=hex>;
typedef enum <uint32> {
POWERPC_ALL = 0x0,
/* MC980000_ALL = POWERPC_ALL */
POWERPC_601 = 0x1,
/* MC98601 = POWERPC_601 */
POWERPC_602 = 0x2,
POWERPC_603 = 0x3,
POWERPC_603e = 0x4,
POWERPC_603ev = 0x5,
POWERPC_604 = 0x6,
POWERPC_604e = 0x7,
POWERPC_620 = 0x8,
POWERPC_750 = 0x9,
POWERPC_7400 = 0xA,
POWERPC_7450 = 0xB,
POWERPC_970 = 0x64
} CpuSubTypePowerPC <format=hex>;
typedef enum <uint> {
MACH_OBJECT = 0x1,
MACH_EXECUTE = 0x2,
MACH_FVMLIB = 0x3,
MACH_CORE = 0x4,
MACH_PRELOAD = 0x5,
MACH_DYLIB = 0x6,
MACH_DYLINKER = 0x7,
MACH_BUNDLE = 0x8,
MACH_DYLIB_STUB = 0x9,
MACH_DSYM = 0xA,
MACH_KEXT_BUNDLE = 0xB
} FileType;
typedef enum <uint> {
i386_THREAD_STATE = 0x1,
i386_FLOAT_STATE = 0x2,
i386_EXCEPTION_STATE = 0x3
} i386ThreadFlavor <format=hex>;
typedef struct {
uint32 eax <format=hex>;
uint32 ebx <format=hex>;
uint32 ecx <format=hex>;
uint32 edx <format=hex>;
uint32 edi <format=hex>;
uint32 esi <format=hex>;
uint32 ebp <format=hex>;
uint32 esp <format=hex>;
uint32 ss <format=hex>;
uint32 eflags <format=hex>;
uint32 eip <format=hex>;
uint32 cs <format=hex>;
uint32 ds <format=hex>;
uint32 es <format=hex>;
uint32 fs <format=hex>;
uint32 gs <format=hex>;
} i386ThreadState <optimize=true, size=64>;
typedef enum <uint> {
x86_THREAD_STATE32 = 0x1,
x86_FLOAT_STATE32 = 0x2,
x86_EXCEPTION_STATE32 = 0x3,
x86_THREAD_STATE64 = 0x4,
x86_FLOAT_STATE64 = 0x5,
x86_EXCEPTION_STATE64 = 0x6,
x86_THREAD_STATE = 0x7,
x86_FLOAT_STATE = 0x8,
x86_EXCEPTION_STATE = 0x9,
x86_DEBUG_STATE32 = 0xA,
x86_DEBUG_STATE64 = 0xB,
x86_DEBUG_STATE = 0xC,
THREAD_STATE_NONE = 0xD
} x86ThreadFlavor <format=hex>;
typedef struct {
uint64 rax <format=hex>;
uint64 rbx <format=hex>;
uint64 rcx <format=hex>;
uint64 rdx <format=hex>;
uint64 rdi <format=hex>;
uint64 rsi <format=hex>;
uint64 rbp <format=hex>;
uint64 rsp <format=hex>;
uint64 r8 <format=hex>;
uint64 r9 <format=hex>;
uint64 r10 <format=hex>;
uint64 r11 <format=hex>;
uint64 r12 <format=hex>;
uint64 r13 <format=hex>;
uint64 r14 <format=hex>;
uint64 r15 <format=hex>;
uint64 rip <format=hex>;
uint64 rflags <format=hex>;
uint64 cs <format=hex>;
uint64 fs <format=hex>;
uint64 gs <format=hex>;
} x86ThreadState <optimize=true, size=168>;
typedef enum <uint> {
PPC_THREAD_STATE = 0x1,
PPC_FLOAT_STATE = 0x2,
PPC_EXCEPTION_STATE = 0x3,
PPC_VECTOR_STATE = 0x4,
PPC_THREAD_STATE64 = 0x5,
PPC_EXCEPTION_STATE64 = 0x6
} PPCThreadFlavor <format=hex>;
typedef struct {
uint32 r0 <format=hex>;
uint32 r1 <format=hex>;
uint32 r2 <format=hex>;
uint32 r3 <format=hex>;
uint32 r4 <format=hex>;
uint32 r5 <format=hex>;
uint32 r6 <format=hex>;
uint32 r7 <format=hex>;
uint32 r8 <format=hex>;
uint32 r9 <format=hex>;
uint32 r10 <format=hex>;
uint32 r11 <format=hex>;
uint32 r12 <format=hex>;
uint32 sp <format=hex>;
uint32 lr <format=hex>;
uint32 pc <format=hex>;
uint32 cpsr <format=hex>;
} ARMThreadState <optimize=true, size=68>;
typedef enum {
ARM_THREAD_STATE = 0x1
} ARMThreadFlavor <format=hex>;
typedef enum {
NOT_ENCRYPTED = 0x0,
COM_APPLE_UNFREE = 0x1,
COM_APPLE_NULL = 0x10
} CryptId <format=hex>;
typedef enum <uint16> {
DICE_KIND_DATA = 0x1,
DICE_KIND_JUMP_TABLE8 = 0x2,
DICE_KIND_JUMP_TABLE16 = 0x3,
DICE_KIND_JUMP_TABLE32 = 0x4,
DICE_KIND_ABS_JUMP_TABLE32 = 0x5
} DataRegionType <format=hex>;
// TODO : Add a printer for this
typedef struct {
uint32 offset;
uint16 length;
DataRegionType kind;
} DataInCodeTableEntry <optimize=true, size=8>;
typedef struct (uint32 size) {
DataInCodeTableEntry entries[size];
} DataInCodeTable;
// This is required as a "concept" inside 010Editor
// http://www.sweetscape.com/support/kb/kb1024.html
typedef struct {
string data;
} StringTableEntry <optimize=false>;
string StringTableEntryRead(StringTableEntry &entry) {
return entry.data;
}
typedef struct (uint32 string_offset, uint32 string_size) {
local uint32 pos = string_offset;
while(FTell() < string_offset + string_size) {
StringTableEntry stringTableEntry <read=StringTableEntryRead>;
}
} StringTable <optimize=false>;
typedef enum <ubyte> {
N_STAB = 0xE0,
N_PEXT = 0x10,
N_TYPE = 0x0E,
N_EXT = 0x01
} SymbolType <format=hex>;
typedef enum <ubyte> {
N_UNDF = 0x0,
N_ABS = 0x2,
N_SECT = 0xE,
N_PBUD = 0xC,
N_INDR = 0xA
} NListType <format=hex>;
typedef enum <uint16> {
// Constant masks for the "n_desc" field in llvm::MachO::nlist and
// llvm::MachO::nlist_64
// The low 3 bits are the for the REFERENCE_TYPE.
REFERENCE_TYPE = 0x7,
REFERENCE_FLAG_UNDEFINED_NON_LAZY = 0,
REFERENCE_FLAG_UNDEFINED_LAZY = 1,
REFERENCE_FLAG_DEFINED = 2,
REFERENCE_FLAG_PRIVATE_DEFINED = 3,
REFERENCE_FLAG_PRIVATE_UNDEFINED_NON_LAZY = 4,
REFERENCE_FLAG_PRIVATE_UNDEFINED_LAZY = 5,
// Flag bits (some overlap with the library ordinal bits).
N_ARM_THUMB_DEF = 0x0008u,
REFERENCED_DYNAMICALLY = 0x0010u,
N_NO_DEAD_STRIP = 0x0020u,
N_WEAK_REF = 0x0040u,
N_WEAK_DEF = 0x0080u,
N_SYMBOL_RESOLVER = 0x0100u,
N_ALT_ENTRY = 0x0200u,
// For undefined symbols coming from libraries, see GET_LIBRARY_ORDINAL()
// as these are in the top 8 bits.
SELF_LIBRARY_ORDINAL = 0x0,
MAX_LIBRARY_ORDINAL = 0xfd,
DYNAMIC_LOOKUP_ORDINAL = 0xfe,
EXECUTABLE_ORDINAL = 0xff
} SymbolDescription <format=hex>;
typedef struct {
// Index into StringTable
uint32 stringIndex;
SymbolType type;
// Index into sections
ubyte sectionIndex;
// Flags
SymbolDescription description;
uint32 value;
} SymbolEntry <optimize=false, read=SymbolEntryRead>;
string SymbolEntryRead(SymbolEntry &entry) {
local string str = ReadString(stringTable.pos + entry.stringIndex);
return str;
}
typedef struct {
// Index into StringTable
uint32 stringIndex;
SymbolType type;
// Index into sections
ubyte sectionIndex;
// Flags
SymbolDescription description;
uint64 value <comment="Symbol Address">;
} SymbolEntry64 <optimize=false, read=SymbolEntry64Read>;
string SymbolEntry64Read(SymbolEntry64 &entry) {
local string str = ReadString(stringTable.pos + entry.stringIndex);
return str;
}
typedef struct (Magic magic,
uint32 symbol_offset, uint32 number_of_symbols) {
local uint32 symbol_count = number_of_symbols;
// Needs to know if this is 64bit or not
FSeek(symbol_offset);
if(magic == MACHO_64) {
SymbolEntry64 symbolEntry[number_of_symbols];
} else {
SymbolEntry symbolEntry[number_of_symbols];
}
} SymbolTable <optimize=false>;
typedef struct (uint32 symbol_offset, uint32 number_of_symbols) {
// Needs to know if this is 64bit or not
FSeek(symbol_offset);
int IndirectSymbolEntry[number_of_symbols] <format=hex, read=IndirectSymbolEntryRead>;
} DynamicSymbolTable <optimize=false>;
string IndirectSymbolEntryRead(int &IndirectSymbolEntry) {
if (header.magic == MACHO_64) {
if (IndirectSymbolEntry < symbolTable.symbol_count)
return SymbolEntry64Read(symbolTable.symbolEntry[IndirectSymbolEntry]);
} else {
if (IndirectSymbolEntry < symbolTable.symbol_count) {
return SymbolEntryRead(symbolTable.symbolEntry[IndirectSymbolEntry]);
}
}
return "";
}
typedef struct (uint32 offset, uint32 sz) {
FSeek(offset);
while (FTell() < offset + sz) {
uleb128 functions <format=hex>;
//Printf("pos:%x\n", FTell());
}
} FunctionStarts <optimize=false>;
typedef struct {
uint32 __srr0 <comment="Instruction address register (PC)">;
uint32 __srr1 <comment="Machine state register (supervisor)">;
uint32 __r0;
uint32 __r1;
uint32 __r2;
uint32 __r3;
uint32 __r4;
uint32 __r5;
uint32 __r6;
uint32 __r7;
uint32 __r8;
uint32 __r9;
uint32 __r10;
uint32 __r11;
uint32 __r12;
uint32 __r13;
uint32 __r14;
uint32 __r15;
uint32 __r16;
uint32 __r17;
uint32 __r18;
uint32 __r19;
uint32 __r20;
uint32 __r21;
uint32 __r22;
uint32 __r23;
uint32 __r24;
uint32 __r25;
uint32 __r26;
uint32 __r27;
uint32 __r28;
uint32 __r29;
uint32 __r30;
uint32 __r31;
uint32 __cr <comment="Condition register">;
uint32 __xer <comment="User's integer exception register">;
uint32 __lr <comment="Link register">;
uint32 __ctr <comment="Count register">;
uint32 __mq <comment="MQ Register (601 only)">;
uint32 __vrsave <comment="Vector save register">;
} PPCThreadState <optimize=true, size=160>;
typedef enum <uint> {
MACH_NOUNDEFS = 0x1,
MACH_INCRLINK = 0x2,
MACH_DYLDLINK = 0x4,
MACH_BINDATLOAD = 0x8,
MACH_PREBOUND = 0x10,
MACH_SPLIT_SEGS = 0x20,
MACH_LAZY_INIT = 0x40,
MACH_TWOLEVEL = 0x80,
MACH_FORCE_FLAT = 0x100,
MACH_NOMULTIDEFS = 0x200,
MACH_NOFIXPREBINDING = 0x400,
MACH_PREBINDABLE = 0x800,
MACH_ALLMODSBOUND = 0x1000,
MACH_SUBSECTIONS_VIA_SYMBOLS = 0x2000,
MACH_CANONICAL = 0x4000,
MACH_WEAK_DEFINES = 0x8000,
MACH_BINDS_TO_WEAK = 0x10000,
MACH_ALLOW_STACK_EXECUTION = 0x20000,
MACH_ROOT_SAFE = 0x40000,
MACH_SETUID_SAFE = 0x80000,
MACH_NO_REEXPORTED_DYLIBS = 0x100000,
MACH_PIE = 0x200000,
MACH_DEAD_STRIPPABLE_DYLIB = 0x400000,
MACH_HAS_TLV_DESCRIPTORS = 0x800000,
MACH_NO_HEAP_EXECUTION = 0x1000000
} Flags;
typedef struct {
CpuType cpu_type <comment="CPU specifier", format=hex>;
switch(cpu_type) {
case X86 :
case I386 :
CpuSubTypeX86 cpu_sub_type <comment="Machine specifier", format=hex>;
break;
case ARM :
CpuSubTypeARM cpu_sub_type <comment="Machine specifier", format=hex>;
break;
default :
uint32 cpu_sub_type <comment="Machine specifier", format=hex>;
}
uint32 file_offset <comment="Offset of header in file">;
uint32 size <comment="Size of object file">;
uint32 align <comment="alignment as a power of two">;
} Fat_Arch <optimize=true>;
typedef struct {
Magic magic <comment="Magic bytes for the file">;
if(magic == MACHO_FAT || magic == MACHO_FAT_CIGAM) {
// Need to switch to BigEndian!
BigEndian();
uint32 fat_arch_size <comment="Number of fat_arch structs">;
Fat_Arch fat_arch[fat_arch_size];
// Switch back to LittleEndian for rest of parsing
LittleEndian();
} else {
CpuType cpu_type <comment="CPU specifier", format=hex>;
switch(cpu_type) {
case X86 :
case I386 :
CpuSubTypeX86 cpu_sub_type <comment="Machine specifier", format=hex>;
break;
case ARM :
CpuSubTypeARM cpu_sub_type <comment="Machine specifier", format=hex>;
break;
case ARM64 :
CpuSubTypeARM64 cpu_sub_type <comment="Machine specifier", format=hex>;
break;
default :
uint32 cpu_sub_type <comment="Machine specifier", format=hex>;
}
FileType file_type;
uint32 num_load_commands;
uint32 size_of_load_commands;
Flags flags;
}
if(magic == MACHO_64) {
uint32 reserved;
}
} Header <read=HeaderRead, optimize=false>;
// TODO : Add instruction set to this (ex: ARM/etc)
string HeaderRead(Header &header) {
local string header_string;
switch(header.magic) {
case MACHO_FAT :
case MACHO_FAT_CIGAM :
header_string = "FAT header";
break;
case MACHO_32 :
header_string = "32bit Mach-O header";
break;
case MACHO_64 :
header_string = "64bit Mach-O header";
break;
default :
header_string = "Unknown header!";
}
return header_string;
}
#define REQ_DYLD (0x80000000)
typedef enum <uint> {
SEGMENT = 0x1,
SYM_TAB = 0x2,
SYM_SEG = 0x3,
THREAD = 0x4,
UNIX_THREAD = 0x5,
LOAD_FVM_LIB = 0x6,
ID_FVM_LIB = 0x7,
IDENT = 0x8,
FVM_FILE = 0x9,
PREPAGE = 0xA,
DY_SYM_TAB = 0xB,
LOAD_DYLIB = 0xC,
ID_DYLIB = 0xD,
LOAD_DYLINKER = 0xE,
ID_DYLINKER = 0xF,
PREBOUND_DYLIB = 0x10,
ROUTINES = 0x11,
SUB_FRAMEWORK = 0x12,
SUB_UMBRELLA = 0x13,
SUB_CLIENT = 0x14,
SUB_LIBRARY = 0x15,
TWOLEVEL_HINTS = 0x16,
PREBIND_CKSUM = 0x17,
LOAD_WEAK_DYLIB = 0x18 | REQ_DYLD,
SEGMENT_64 = 0x19,
ROUTINES_64 = 0x1A,
UUID = 0x1B,
RPATH = 0x1C | REQ_DYLD,
CODE_SIGNATURE = 0x1D, // LinkeditDataCommand
SEGMENT_SPLIT_INFO = 0x1E, // LinkeditDataCommand
REEXPORT_DYLIB = 0x1F | REQ_DYLD,
LAZY_LOAD_DYLIB = 0x20,
ENCRYPTION_INFO = 0x21,
DYLD_INFO = 0x22,
DYLD_INFO_ONLY = 0x22 | REQ_DYLD,
LOAD_UPWARD_DYLIB = 0x23 | REQ_DYLD,
VERSION_MIN_MAC_OSX = 0x24,
VERSION_MIN_IPHONE_OS = 0x25,
FUNCTION_STARTS = 0x26, // LinkeditDataCommand
DYLD_ENVIRONMENT = 0x27,
MAIN = 0x28 | REQ_DYLD,
DATA_IN_CODE = 0x29,
SOURCE_VERSION = 0x2A,
DYLIB_CODE_SIGN_DRS = 0x2B,
ENCRYPTION_INFO_64 = 0x2C,
LINKER_OPTION = 0x2D,
LINKER_OPTIMIZATION_HINT = 0x2E,
VERSION_MIN_TVOS = 0x2F,
VERSION_MIN_WATCHOS = 0x30,
} LoadCommandType <read=LoadCommandTypeRead>;
string LoadCommandTypeRead(LoadCommandType &loadCommandType) {
switch(loadCommandType) {
case SEGMENT :
return "SEGMENT";
case SYM_TAB :
return "SYM_TAB";
case SYM_SEG :
return "SYM_SEG";
case THREAD :
return "THREAD";
case UNIX_THREAD :
return "UNIX_THREAD";
case LOAD_FVM_LIB :
return "LOAD_FVM_LIB";
case ID_FVM_LIB :
return "ID_FVM_LIB";
case IDENT :
return "IDENT";
case FVM_FILE :
return "FVM_FILE";
case PREPAGE :
return "PREPAGE";
case DY_SYM_TAB :
return "DY_SYM_TAB";
case LOAD_DYLIB :
return "LOAD_DYLIB";
case ID_DYLIB :
return "ID_DYLIB";
case LOAD_DYLINKER :
return "LOAD_DYLINKER";
case ID_DYLINKER :
return "ID_DYLINKER";
case PREBOUND_DYLIB :
return "PREBOUND_DYLIB";
case ROUTINES :
return "ROUTINES";
case SUB_FRAMEWORK :
return "SUB_FRAMEWORK";
case SUB_UMBRELLA :
return "SUB_UMBRELLA";
case SUB_CLIENT :
return "SUB_CLIENT";
case SUB_LIBRARY :
return "SUB_LIBRARY";
case TWOLEVEL_HINTS :
return "TWOLEVEL_HINTS";
case PREBIND_CKSUM :
return "PREBIND_CKSUM";
case LOAD_WEAK_DYLIB :
return "LOAD_WEAK_DYLIB";
case SEGMENT_64 :
return "SEGMENT_64";
case ROUTINES_64 :
return "ROUTINES_64";
case UUID :
return "UUID";
case RPATH :
return "RPATH";
case CODE_SIGNATURE :
return "CODE_SIGNATURE";
case SEGMENT_SPLIT_INFO :
return "SEGMENT_SPLIT_INFO";
case REEXPORT_DYLIB :
return "REEXPORT_DYLIB";
case LAZY_LOAD_DYLIB :
return "LAZY_LOAD_DYLIB";
case ENCRYPTION_INFO :
return "ENCRYPTION_INFO";
case DYLD_INFO :
return "DYLD_INFO";
case DYLD_INFO_ONLY :
return "DYLD_INFO_ONLY";
case LOAD_UPWARD_DYLIB :
return "LOAD_UPWARD_DYLIB";
case VERSION_MIN_MAC_OSX :
return "VERSION_MIN_MAC_OSX";
case VERSION_MIN_IPHONE_OS :
return "VERSION_MIN_IPHONE_OS";
case FUNCTION_STARTS :
return "FUNCTION_STARTS";
case DYLD_ENVIRONMENT :
return "DYLD_ENVIRONMENT";
case MAIN :
return "MAIN";
case DATA_IN_CODE :
return "DATA_IN_CODE";
case SOURCE_VERSION :
return "SOURCE_VERSION";
case DYLIB_CODE_SIGN_DRS :
return "DYLIB_CODE_SIGN_DRS";
case ENCRYPTION_INFO_64 :
return "ENCRYPTION_INFO_64";
case LINKER_OPTION :
return "LINKER_OPTION";
case LINKER_OPTIMIZATION_HINT :
return "LINKER_OPTIMIZATION_HINT";
case VERSION_MIN_TVOS :
return "VERSION_MIN_TVOS";
case VERSION_MIN_WATCHOS :
return "VERSION_MIN_WATCHOS";
default :
return "Error";
}
}
typedef struct {
char section_name[16];
char segment_name[16];
uint32 address <format=hex>;
uint32 size <format=hex>;
uint32 offset;
uint32 section_alignment;
uint32 relocation_entry_offset;
uint32 number_of_relocation_entries;
uint32 flags <format=hex>;
uint32 reserved1;
uint32 reserved2;
} Section <optimize=false, size=68>;
typedef struct {
char section_name[16];
char segment_name[16];
uint64 address <format=hex>;
uint64 size <format=hex>;
uint32 offset;
uint32 section_alignment;
uint32 relocation_entry_offset;
uint32 number_of_relocation_entries;
uint32 flags <format=hex>;
uint32 reserved1;
uint32 reserved2;
uint32 reserved3;
} Section64 <optimize=false, size=80>;
typedef uint32 vm_proc;
typedef enum <uint> {
HIGH_VM = 0x1,
FVM_LIB = 0x2,
NO_RELOC = 0x4,
PROTECTION_VERSION_1 = 0x8
} SegmentFlags <format=hex>;
typedef struct {
uint32 load_command_string_offset <comment="Offset in respect to the start of load command to string data">;
local int64 pos = FTell();
// We need to goto beginning of LoadCommand, then goto the offset
FSeek(FTell() - (sizeof(uint32) * 3) + load_command_string_offset);
string string_data <comment="Load command string">;
FSeek(pos);
} LoadCommandString <read=LoadCommandStringRead, optimize=false>;
string LoadCommandStringRead(LoadCommandString &loadCommandString) {
return loadCommandString.string_data;
};
typedef ubyte Uuid[16] <read=UuidRead, format=hex>;
// TODO : Clean this ugly thing up
string UuidRead(Uuid uuid) {
local string ret, tmp;
local int i;
for(i = 0; i<4; i++) {
SPrintf(tmp, "%.2X", uuid[i]);
ret += tmp;
}
ret += "-";
for(i = 0; i<2; i++) {
SPrintf(tmp, "%.2X", uuid[i+4]);
ret += tmp;
}
ret += "-";
for(i = 0; i<2; i++) {
SPrintf(tmp, "%.2X", uuid[i+6]);
ret += tmp;
}
ret += "-";
for(i = 0; i<2; i++) {
SPrintf(tmp, "%.2X", uuid[i+8]);
ret += tmp;
}
ret += "-";
for(i = 0; i<6; i++) {
SPrintf(tmp, "%.2X", uuid[i+10]);
ret += tmp;
}
return ret;
}
typedef struct {
uint32 version;
} Version <read=VersionRead, optimize=true, size=4>;
string VersionRead(Version &version) {
local string version_string;
if(version.version & 0xFF == 0) {
SPrintf(version_string, "%u.%u", version.version >> 16, (version.version >> 8) & 0xFF);
} else {
SPrintf(version_string, "%u.%u.%u", version.version >> 16, (version.version >> 8) & 0xFF, version.version & 0xFF);
}
return version_string;
}
typedef struct {
uint64 version;
} SourceVersion <read=SourceVersionRead, optimize=true, size=8>;
string SourceVersionRead(SourceVersion &version) {
local string version_string;
SPrintf(version_string, "%u.%u.%u.%u.%u", (version.version >> 64) & 0xFF,
(version.version >> 32) & 0xFF,
(version.version >> 16) & 0xFF,
(version.version >> 8) & 0xFF,
version.version & 0xFF);
return version_string;
}
// Structures for CodeSignature
typedef enum <uint> {
REQUIREMENT = 0xfade0c00,
REQUIREMENTS = 0xfade0c01,
CODEDIRECTORY = 0xfade0c02,
EMBEDDED_SIGNATURE = 0xfade0cc0,
DETACHED_SIGNATURE = 0xfade0cc1,
BLOB_WRAPPER = 0xfade0b01,
ENTITLEMENT = 0xfade7171,
} BlobMagic <format=hex, comment="blob magic">;
typedef struct (uint begin){
local uint begin = begin;
uint32 type <comment="Type of entry">;
uint32 offset <format=hex, comment="Offset of entry">;
} BlobIndex <optimize=false, read=BlobIndexReader, comment="Blob index">;
string BlobIndexReader(BlobIndex & blobIndex) {
local string s;
s = SPrintf(s, "offset=0x%x", blobIndex.begin + blobIndex.offset);
return s;
}
typedef struct {
local uint begin = FTell();
BlobMagic magic;
uint32 length;
uint32 count <comment="Number of index entries">;
BlobIndex index(begin)[count];
} SuperBlob <optimize=false, read=SuperBlobReader, comment="Super Blob">;
string SuperBlobReader(SuperBlob &superBlob) {
local string s;
s = SPrintf(s, "%d blobs", superBlob.count);
return s;
}