-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathmach_o.py
1641 lines (1381 loc) · 45.7 KB
/
mach_o.py
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
"""
Other than changing the load commands in such a way that they do not
contain the load command itself, this is largely a by-hand conversion
of the C headers. Hopefully everything in here should be at least as
obvious as the C headers, and you should be using the C headers as a real
reference because the documentation didn't come along for the ride.
Doing much of anything with the symbol tables or segments is really
not covered at this point.
See /usr/include/mach-o and friends.
"""
import time
from macholib.ptypes import (
Structure,
p_int32,
p_int64,
p_long,
p_short,
p_uint8,
p_uint32,
p_uint64,
p_ulong,
pypackable,
)
_CPU_ARCH_ABI64 = 0x01000000
CPU_TYPE_NAMES = {
-1: "ANY",
1: "VAX",
6: "MC680x0",
7: "i386",
_CPU_ARCH_ABI64 | 7: "x86_64",
8: "MIPS",
10: "MC98000",
11: "HPPA",
12: "ARM",
_CPU_ARCH_ABI64 | 12: "ARM64",
13: "MC88000",
14: "SPARC",
15: "i860",
16: "Alpha",
18: "PowerPC",
_CPU_ARCH_ABI64 | 18: "PowerPC64",
}
INTEL64_SUBTYPE = {
3: "CPU_SUBTYPE_X86_64_ALL",
4: "CPU_SUBTYPE_X86_ARCH1",
8: "CPU_SUBTYPE_X86_64_H",
}
# define CPU_SUBTYPE_INTEL(f, m) ((cpu_subtype_t) (f) + ((m) << 4))
INTEL_SUBTYPE = {
0: "CPU_SUBTYPE_INTEL_MODEL_ALL",
1: "CPU_THREADTYPE_INTEL_HTT",
3: "CPU_SUBTYPE_I386_ALL",
4: "CPU_SUBTYPE_486",
5: "CPU_SUBTYPE_586",
8: "CPU_SUBTYPE_PENTIUM_3",
9: "CPU_SUBTYPE_PENTIUM_M",
10: "CPU_SUBTYPE_PENTIUM_4",
11: "CPU_SUBTYPE_ITANIUM",
12: "CPU_SUBTYPE_XEON",
34: "CPU_SUBTYPE_XEON_MP",
42: "CPU_SUBTYPE_PENTIUM_4_M",
43: "CPU_SUBTYPE_ITANIUM_2",
38: "CPU_SUBTYPE_PENTPRO",
40: "CPU_SUBTYPE_PENTIUM_3_M",
52: "CPU_SUBTYPE_PENTIUM_3_XEON",
102: "CPU_SUBTYPE_PENTII_M3",
132: "CPU_SUBTYPE_486SX",
166: "CPU_SUBTYPE_PENTII_M5",
199: "CPU_SUBTYPE_CELERON",
231: "CPU_SUBTYPE_CELERON_MOBILE",
}
MC680_SUBTYPE = {
1: "CPU_SUBTYPE_MC680x0_ALL",
2: "CPU_SUBTYPE_MC68040",
3: "CPU_SUBTYPE_MC68030_ONLY",
}
MIPS_SUBTYPE = {
0: "CPU_SUBTYPE_MIPS_ALL",
1: "CPU_SUBTYPE_MIPS_R2300",
2: "CPU_SUBTYPE_MIPS_R2600",
3: "CPU_SUBTYPE_MIPS_R2800",
4: "CPU_SUBTYPE_MIPS_R2000a",
5: "CPU_SUBTYPE_MIPS_R2000",
6: "CPU_SUBTYPE_MIPS_R3000a",
7: "CPU_SUBTYPE_MIPS_R3000",
}
MC98000_SUBTYPE = {0: "CPU_SUBTYPE_MC98000_ALL", 1: "CPU_SUBTYPE_MC98601"}
HPPA_SUBTYPE = {0: "CPU_SUBTYPE_HPPA_7100", 1: "CPU_SUBTYPE_HPPA_7100LC"}
MC88_SUBTYPE = {
0: "CPU_SUBTYPE_MC88000_ALL",
1: "CPU_SUBTYPE_MC88100",
2: "CPU_SUBTYPE_MC88110",
}
SPARC_SUBTYPE = {0: "CPU_SUBTYPE_SPARC_ALL"}
I860_SUBTYPE = {0: "CPU_SUBTYPE_I860_ALL", 1: "CPU_SUBTYPE_I860_860"}
POWERPC_SUBTYPE = {
0: "CPU_SUBTYPE_POWERPC_ALL",
1: "CPU_SUBTYPE_POWERPC_601",
2: "CPU_SUBTYPE_POWERPC_602",
3: "CPU_SUBTYPE_POWERPC_603",
4: "CPU_SUBTYPE_POWERPC_603e",
5: "CPU_SUBTYPE_POWERPC_603ev",
6: "CPU_SUBTYPE_POWERPC_604",
7: "CPU_SUBTYPE_POWERPC_604e",
8: "CPU_SUBTYPE_POWERPC_620",
9: "CPU_SUBTYPE_POWERPC_750",
10: "CPU_SUBTYPE_POWERPC_7400",
11: "CPU_SUBTYPE_POWERPC_7450",
100: "CPU_SUBTYPE_POWERPC_970",
}
ARM_SUBTYPE = {
0: "CPU_SUBTYPE_ARM_ALL12",
5: "CPU_SUBTYPE_ARM_V4T",
6: "CPU_SUBTYPE_ARM_V6",
7: "CPU_SUBTYPE_ARM_V5TEJ",
8: "CPU_SUBTYPE_ARM_XSCALE",
9: "CPU_SUBTYPE_ARM_V7",
10: "CPU_SUBTYPE_ARM_V7F",
11: "CPU_SUBTYPE_ARM_V7S",
12: "CPU_SUBTYPE_ARM_V7K",
13: "CPU_SUBTYPE_ARM_V8",
14: "CPU_SUBTYPE_ARM_V6M",
15: "CPU_SUBTYPE_ARM_V7M",
16: "CPU_SUBTYPE_ARM_V7EM",
17: "CPU_SUBTYPE_ARM_V8M",
}
ARM64_SUBTYPE = {
0: "CPU_SUBTYPE_ARM64_ALL",
1: "CPU_SUBTYPE_ARM64_V8",
2: "CPU_SUBTYPE_ARM64E",
}
VAX_SUBTYPE = {
0: "CPU_SUBTYPE_VAX_ALL",
1: "CPU_SUBTYPE_VAX780",
2: "CPU_SUBTYPE_VAX785",
3: "CPU_SUBTYPE_VAX750",
4: "CPU_SUBTYPE_VAX730",
5: "CPU_SUBTYPE_UVAXI",
6: "CPU_SUBTYPE_UVAXII",
7: "CPU_SUBTYPE_VAX8200",
8: "CPU_SUBTYPE_VAX8500",
9: "CPU_SUBTYPE_VAX8600",
10: "CPU_SUBTYPE_VAX8650",
11: "CPU_SUBTYPE_VAX8800",
12: "CPU_SUBTYPE_UVAXIII",
}
def get_cpu_subtype(cpu_type, cpu_subtype):
st = cpu_subtype & 0x0FFFFFFF
if cpu_type == 1:
subtype = VAX_SUBTYPE.get(st, st)
elif cpu_type == 6:
subtype = MC680_SUBTYPE.get(st, st)
elif cpu_type == 7:
subtype = INTEL_SUBTYPE.get(st, st)
elif cpu_type == 7 | _CPU_ARCH_ABI64:
subtype = INTEL64_SUBTYPE.get(st, st)
elif cpu_type == 8:
subtype = MIPS_SUBTYPE.get(st, st)
elif cpu_type == 10:
subtype = MC98000_SUBTYPE.get(st, st)
elif cpu_type == 11:
subtype = HPPA_SUBTYPE.get(st, st)
elif cpu_type == 12:
subtype = ARM_SUBTYPE.get(st, st)
elif cpu_type == 12 | _CPU_ARCH_ABI64:
subtype = ARM64_SUBTYPE.get(st, st)
elif cpu_type == 13:
subtype = MC88_SUBTYPE.get(st, st)
elif cpu_type == 14:
subtype = SPARC_SUBTYPE.get(st, st)
elif cpu_type == 15:
subtype = I860_SUBTYPE.get(st, st)
elif cpu_type == 16:
subtype = MIPS_SUBTYPE.get(st, st)
elif cpu_type == 18:
subtype = POWERPC_SUBTYPE.get(st, st)
elif cpu_type == 18 | _CPU_ARCH_ABI64:
subtype = POWERPC_SUBTYPE.get(st, st)
else:
subtype = str(st)
return subtype
_MH_EXECUTE_SYM = "__mh_execute_header"
MH_EXECUTE_SYM = "_mh_execute_header"
_MH_BUNDLE_SYM = "__mh_bundle_header"
MH_BUNDLE_SYM = "_mh_bundle_header"
_MH_DYLIB_SYM = "__mh_dylib_header"
MH_DYLIB_SYM = "_mh_dylib_header"
_MH_DYLINKER_SYM = "__mh_dylinker_header"
MH_DYLINKER_SYM = "_mh_dylinker_header"
(
MH_OBJECT,
MH_EXECUTE,
MH_FVMLIB,
MH_CORE,
MH_PRELOAD,
MH_DYLIB,
MH_DYLINKER,
MH_BUNDLE,
MH_DYLIB_STUB,
MH_DSYM,
) = range(0x1, 0xB)
MH_FILESET = 0xC
(
MH_NOUNDEFS,
MH_INCRLINK,
MH_DYLDLINK,
MH_BINDATLOAD,
MH_PREBOUND,
MH_SPLIT_SEGS,
MH_LAZY_INIT,
MH_TWOLEVEL,
MH_FORCE_FLAT,
MH_NOMULTIDEFS,
MH_NOFIXPREBINDING,
MH_PREBINDABLE,
MH_ALLMODSBOUND,
MH_SUBSECTIONS_VIA_SYMBOLS,
MH_CANONICAL,
MH_WEAK_DEFINES,
MH_BINDS_TO_WEAK,
MH_ALLOW_STACK_EXECUTION,
MH_ROOT_SAFE,
MH_SETUID_SAFE,
MH_NO_REEXPORTED_DYLIBS,
MH_PIE,
MH_DEAD_STRIPPABLE_DYLIB,
MH_HAS_TLV_DESCRIPTORS,
MH_NO_HEAP_EXECUTION,
MH_APP_EXTENSION_SAFE,
) = map((1).__lshift__, range(26))
MH_MAGIC = 0xFEEDFACE
MH_CIGAM = 0xCEFAEDFE
MH_MAGIC_64 = 0xFEEDFACF
MH_CIGAM_64 = 0xCFFAEDFE
integer_t = p_int32
cpu_type_t = integer_t
cpu_subtype_t = p_uint32
MH_FILETYPE_NAMES = {
MH_OBJECT: "relocatable object",
MH_EXECUTE: "demand paged executable",
MH_FVMLIB: "fixed vm shared library",
MH_CORE: "core",
MH_PRELOAD: "preloaded executable",
MH_DYLIB: "dynamically bound shared library",
MH_DYLINKER: "dynamic link editor",
MH_BUNDLE: "dynamically bound bundle",
MH_DYLIB_STUB: "shared library stub for static linking",
MH_DSYM: "symbol information",
MH_FILESET: "fileset object",
}
MH_FILETYPE_SHORTNAMES = {
MH_OBJECT: "object",
MH_EXECUTE: "execute",
MH_FVMLIB: "fvmlib",
MH_CORE: "core",
MH_PRELOAD: "preload",
MH_DYLIB: "dylib",
MH_DYLINKER: "dylinker",
MH_BUNDLE: "bundle",
MH_DYLIB_STUB: "dylib_stub",
MH_DSYM: "dsym",
}
MH_FLAGS_NAMES = {
MH_NOUNDEFS: "MH_NOUNDEFS",
MH_INCRLINK: "MH_INCRLINK",
MH_DYLDLINK: "MH_DYLDLINK",
MH_BINDATLOAD: "MH_BINDATLOAD",
MH_PREBOUND: "MH_PREBOUND",
MH_SPLIT_SEGS: "MH_SPLIT_SEGS",
MH_LAZY_INIT: "MH_LAZY_INIT",
MH_TWOLEVEL: "MH_TWOLEVEL",
MH_FORCE_FLAT: "MH_FORCE_FLAT",
MH_NOMULTIDEFS: "MH_NOMULTIDEFS",
MH_NOFIXPREBINDING: "MH_NOFIXPREBINDING",
MH_PREBINDABLE: "MH_PREBINDABLE",
MH_ALLMODSBOUND: "MH_ALLMODSBOUND",
MH_SUBSECTIONS_VIA_SYMBOLS: "MH_SUBSECTIONS_VIA_SYMBOLS",
MH_CANONICAL: "MH_CANONICAL",
MH_WEAK_DEFINES: "MH_WEAK_DEFINES",
MH_BINDS_TO_WEAK: "MH_BINDS_TO_WEAK",
MH_ALLOW_STACK_EXECUTION: "MH_ALLOW_STACK_EXECUTION",
MH_ROOT_SAFE: "MH_ROOT_SAFE",
MH_SETUID_SAFE: "MH_SETUID_SAFE",
MH_NO_REEXPORTED_DYLIBS: "MH_NO_REEXPORTED_DYLIBS",
MH_PIE: "MH_PIE",
MH_DEAD_STRIPPABLE_DYLIB: "MH_DEAD_STRIPPABLE_DYLIB",
MH_HAS_TLV_DESCRIPTORS: "MH_HAS_TLV_DESCRIPTORS",
MH_NO_HEAP_EXECUTION: "MH_NO_HEAP_EXECUTION",
MH_APP_EXTENSION_SAFE: "MH_APP_EXTENSION_SAFE",
}
MH_FLAGS_DESCRIPTIONS = {
MH_NOUNDEFS: "no undefined references",
MH_INCRLINK: "output of an incremental link",
MH_DYLDLINK: "input for the dynamic linker",
MH_BINDATLOAD: "undefined references bound dynamically when loaded",
MH_PREBOUND: "dynamic undefined references prebound",
MH_SPLIT_SEGS: "split read-only and read-write segments",
MH_LAZY_INIT: "(obsolete)",
MH_TWOLEVEL: "using two-level name space bindings",
MH_FORCE_FLAT: "forcing all imagges to use flat name space bindings",
MH_NOMULTIDEFS: "umbrella guarantees no multiple definitions",
MH_NOFIXPREBINDING: "do not notify prebinding agent about this executable",
MH_PREBINDABLE: "the binary is not prebound but can have its prebinding redone",
MH_ALLMODSBOUND: "indicates that this binary binds to all "
"two-level namespace modules of its dependent libraries",
MH_SUBSECTIONS_VIA_SYMBOLS: "safe to divide up the sections into "
"sub-sections via symbols for dead code stripping",
MH_CANONICAL: "the binary has been canonicalized via the unprebind operation",
MH_WEAK_DEFINES: "the final linked image contains external weak symbols",
MH_BINDS_TO_WEAK: "the final linked image uses weak symbols",
MH_ALLOW_STACK_EXECUTION: "all stacks in the task will be given "
"stack execution privilege",
MH_ROOT_SAFE: "the binary declares it is safe for use in processes with uid zero",
MH_SETUID_SAFE: "the binary declares it is safe for use in processes "
"when issetugid() is true",
MH_NO_REEXPORTED_DYLIBS: "the static linker does not need to examine dependent "
"dylibs to see if any are re-exported",
MH_PIE: "the OS will load the main executable at a random address",
MH_DEAD_STRIPPABLE_DYLIB: "the static linker will automatically not create a "
"LC_LOAD_DYLIB load command to the dylib if no symbols are being "
"referenced from the dylib",
MH_HAS_TLV_DESCRIPTORS: "contains a section of type S_THREAD_LOCAL_VARIABLES",
MH_NO_HEAP_EXECUTION: "the OS will run the main executable with a "
"non-executable heap even on platforms that don't require it",
MH_APP_EXTENSION_SAFE: "the code was linked for use in an application extension.",
}
class mach_version_helper(Structure):
_fields_ = (("_version", p_uint32),)
@property
def major(self):
return self._version >> 16 & 0xFFFF
@major.setter
def major(self, v):
self._version = (self._version & 0xFFFF) | (v << 16)
@property
def minor(self):
return self._version >> 8 & 0xFF
@minor.setter
def minor(self, v):
self._version = (self._version & 0xFFFF00FF) | (v << 8)
@property
def rev(self):
return self._version & 0xFF
@rev.setter
def rev(self, v):
return (self._version & 0xFFFFFF00) | v
def __str__(self):
return "%s.%s.%s" % (self.major, self.minor, self.rev)
class mach_timestamp_helper(p_uint32):
def __str__(self):
return time.ctime(self)
def read_struct(f, s, **kw):
return s.from_fileobj(f, **kw)
class mach_header(Structure):
_fields_ = (
("magic", p_uint32),
("cputype", cpu_type_t),
("cpusubtype", cpu_subtype_t),
("filetype", p_uint32),
("ncmds", p_uint32),
("sizeofcmds", p_uint32),
("flags", p_uint32),
)
def _describe(self):
bit = 1
flags = self.flags
dflags = []
while flags and bit < (1 << 32):
if flags & bit:
dflags.append(
{
"name": MH_FLAGS_NAMES.get(bit, str(bit)),
"description": MH_FLAGS_DESCRIPTIONS.get(bit, str(bit)),
}
)
flags = flags ^ bit
bit <<= 1
return (
("magic", int(self.magic)),
("cputype_string", CPU_TYPE_NAMES.get(self.cputype, self.cputype)),
("cputype", int(self.cputype)),
("cpusubtype_string", get_cpu_subtype(self.cputype, self.cpusubtype)),
("cpusubtype", int(self.cpusubtype)),
("filetype_string", MH_FILETYPE_NAMES.get(self.filetype, self.filetype)),
("filetype", int(self.filetype)),
("ncmds", self.ncmds),
("sizeofcmds", self.sizeofcmds),
("flags", dflags),
("raw_flags", int(self.flags)),
)
class mach_header_64(mach_header):
_fields_ = mach_header._fields_ + (("reserved", p_uint32),)
class load_command(Structure):
_fields_ = (("cmd", p_uint32), ("cmdsize", p_uint32))
def get_cmd_name(self):
return LC_NAMES.get(self.cmd, self.cmd)
LC_REQ_DYLD = 0x80000000
(
LC_SEGMENT,
LC_SYMTAB,
LC_SYMSEG,
LC_THREAD,
LC_UNIXTHREAD,
LC_LOADFVMLIB,
LC_IDFVMLIB,
LC_IDENT,
LC_FVMFILE,
LC_PREPAGE,
LC_DYSYMTAB,
LC_LOAD_DYLIB,
LC_ID_DYLIB,
LC_LOAD_DYLINKER,
LC_ID_DYLINKER,
LC_PREBOUND_DYLIB,
LC_ROUTINES,
LC_SUB_FRAMEWORK,
LC_SUB_UMBRELLA,
LC_SUB_CLIENT,
LC_SUB_LIBRARY,
LC_TWOLEVEL_HINTS,
LC_PREBIND_CKSUM,
) = range(0x1, 0x18)
LC_LOAD_WEAK_DYLIB = LC_REQ_DYLD | 0x18
LC_SEGMENT_64 = 0x19
LC_ROUTINES_64 = 0x1A
LC_UUID = 0x1B
LC_RPATH = 0x1C | LC_REQ_DYLD
LC_CODE_SIGNATURE = 0x1D
LC_CODE_SEGMENT_SPLIT_INFO = 0x1E
LC_REEXPORT_DYLIB = 0x1F | LC_REQ_DYLD
LC_LAZY_LOAD_DYLIB = 0x20
LC_ENCRYPTION_INFO = 0x21
LC_DYLD_INFO = 0x22
LC_DYLD_INFO_ONLY = 0x22 | LC_REQ_DYLD
LC_LOAD_UPWARD_DYLIB = 0x23 | LC_REQ_DYLD
LC_VERSION_MIN_MACOSX = 0x24
LC_VERSION_MIN_IPHONEOS = 0x25
LC_FUNCTION_STARTS = 0x26
LC_DYLD_ENVIRONMENT = 0x27
LC_MAIN = 0x28 | LC_REQ_DYLD
LC_DATA_IN_CODE = 0x29
LC_SOURCE_VERSION = 0x2A
LC_DYLIB_CODE_SIGN_DRS = 0x2B
LC_ENCRYPTION_INFO_64 = 0x2C
LC_LINKER_OPTION = 0x2D
LC_LINKER_OPTIMIZATION_HINT = 0x2E
LC_VERSION_MIN_TVOS = 0x2F
LC_VERSION_MIN_WATCHOS = 0x30
LC_NOTE = 0x31
LC_BUILD_VERSION = 0x32
LC_DYLD_EXPORTS_TRIE = 0x33 | LC_REQ_DYLD
LC_DYLD_CHAINED_FIXUPS = 0x34 | LC_REQ_DYLD
LC_FILESET_ENTRY = 0x35 | LC_REQ_DYLD
# this is really a union.. but whatever
class lc_str(p_uint32):
pass
p_str16 = pypackable("p_str16", bytes, "16s")
vm_prot_t = p_int32
class segment_command(Structure):
_fields_ = (
("segname", p_str16),
("vmaddr", p_uint32),
("vmsize", p_uint32),
("fileoff", p_uint32),
("filesize", p_uint32),
("maxprot", vm_prot_t),
("initprot", vm_prot_t),
("nsects", p_uint32), # read the section structures ?
("flags", p_uint32),
)
def describe(self):
s = {}
s["segname"] = self.segname.rstrip("\x00")
s["vmaddr"] = int(self.vmaddr)
s["vmsize"] = int(self.vmsize)
s["fileoff"] = int(self.fileoff)
s["filesize"] = int(self.filesize)
s["initprot"] = self.get_initial_virtual_memory_protections()
s["initprot_raw"] = int(self.initprot)
s["maxprot"] = self.get_max_virtual_memory_protections()
s["maxprot_raw"] = int(self.maxprot)
s["nsects"] = int(self.nsects)
s["flags"] = self.flags
return s
def get_initial_virtual_memory_protections(self):
vm = []
if self.initprot == 0:
vm.append("VM_PROT_NONE")
if self.initprot & 1:
vm.append("VM_PROT_READ")
if self.initprot & 2:
vm.append("VM_PROT_WRITE")
if self.initprot & 4:
vm.append("VM_PROT_EXECUTE")
return vm
def get_max_virtual_memory_protections(self):
vm = []
if self.maxprot == 0:
vm.append("VM_PROT_NONE")
if self.maxprot & 1:
vm.append("VM_PROT_READ")
if self.maxprot & 2:
vm.append("VM_PROT_WRITE")
if self.maxprot & 4:
vm.append("VM_PROT_EXECUTE")
return vm
class segment_command_64(Structure):
_fields_ = (
("segname", p_str16),
("vmaddr", p_uint64),
("vmsize", p_uint64),
("fileoff", p_uint64),
("filesize", p_uint64),
("maxprot", vm_prot_t),
("initprot", vm_prot_t),
("nsects", p_uint32), # read the section structures ?
("flags", p_uint32),
)
def describe(self):
s = {}
s["segname"] = self.segname.rstrip("\x00")
s["vmaddr"] = int(self.vmaddr)
s["vmsize"] = int(self.vmsize)
s["fileoff"] = int(self.fileoff)
s["filesize"] = int(self.filesize)
s["initprot"] = self.get_initial_virtual_memory_protections()
s["initprot_raw"] = int(self.initprot)
s["maxprot"] = self.get_max_virtual_memory_protections()
s["maxprot_raw"] = int(self.maxprot)
s["nsects"] = int(self.nsects)
s["flags"] = self.flags
return s
def get_initial_virtual_memory_protections(self):
vm = []
if self.initprot == 0:
vm.append("VM_PROT_NONE")
if self.initprot & 1:
vm.append("VM_PROT_READ")
if self.initprot & 2:
vm.append("VM_PROT_WRITE")
if self.initprot & 4:
vm.append("VM_PROT_EXECUTE")
return vm
def get_max_virtual_memory_protections(self):
vm = []
if self.maxprot == 0:
vm.append("VM_PROT_NONE")
if self.maxprot & 1:
vm.append("VM_PROT_READ")
if self.maxprot & 2:
vm.append("VM_PROT_WRITE")
if self.maxprot & 4:
vm.append("VM_PROT_EXECUTE")
return vm
SG_HIGHVM = 0x1
SG_FVMLIB = 0x2
SG_NORELOC = 0x4
SG_PROTECTED_VERSION_1 = 0x8
class section(Structure):
_fields_ = (
("sectname", p_str16),
("segname", p_str16),
("addr", p_uint32),
("size", p_uint32),
("offset", p_uint32),
("align", p_uint32),
("reloff", p_uint32),
("nreloc", p_uint32),
("flags", p_uint32),
("reserved1", p_uint32),
("reserved2", p_uint32),
)
def describe(self):
s = {}
s["sectname"] = self.sectname.rstrip("\x00")
s["segname"] = self.segname.rstrip("\x00")
s["addr"] = int(self.addr)
s["size"] = int(self.size)
s["offset"] = int(self.offset)
s["align"] = int(self.align)
s["reloff"] = int(self.reloff)
s["nreloc"] = int(self.nreloc)
f = {}
f["type"] = FLAG_SECTION_TYPES[int(self.flags) & 0xFF]
f["attributes"] = []
for k in FLAG_SECTION_ATTRIBUTES:
if k & self.flags:
f["attributes"].append(FLAG_SECTION_ATTRIBUTES[k])
if not f["attributes"]:
del f["attributes"]
s["flags"] = f
s["reserved1"] = int(self.reserved1)
s["reserved2"] = int(self.reserved2)
return s
def add_section_data(self, data):
self.section_data = data
class section_64(Structure):
_fields_ = (
("sectname", p_str16),
("segname", p_str16),
("addr", p_uint64),
("size", p_uint64),
("offset", p_uint32),
("align", p_uint32),
("reloff", p_uint32),
("nreloc", p_uint32),
("flags", p_uint32),
("reserved1", p_uint32),
("reserved2", p_uint32),
("reserved3", p_uint32),
)
def describe(self):
s = {}
s["sectname"] = self.sectname.rstrip("\x00")
s["segname"] = self.segname.rstrip("\x00")
s["addr"] = int(self.addr)
s["size"] = int(self.size)
s["offset"] = int(self.offset)
s["align"] = int(self.align)
s["reloff"] = int(self.reloff)
s["nreloc"] = int(self.nreloc)
f = {}
f["type"] = FLAG_SECTION_TYPES[int(self.flags) & 0xFF]
f["attributes"] = []
for k in FLAG_SECTION_ATTRIBUTES:
if k & self.flags:
f["attributes"].append(FLAG_SECTION_ATTRIBUTES[k])
if not f["attributes"]:
del f["attributes"]
s["flags"] = f
s["reserved1"] = int(self.reserved1)
s["reserved2"] = int(self.reserved2)
s["reserved3"] = int(self.reserved3)
return s
def add_section_data(self, data):
self.section_data = data
SECTION_TYPE = 0xFF
SECTION_ATTRIBUTES = 0xFFFFFF00
S_REGULAR = 0x0
S_ZEROFILL = 0x1
S_CSTRING_LITERALS = 0x2
S_4BYTE_LITERALS = 0x3
S_8BYTE_LITERALS = 0x4
S_LITERAL_POINTERS = 0x5
S_NON_LAZY_SYMBOL_POINTERS = 0x6
S_LAZY_SYMBOL_POINTERS = 0x7
S_SYMBOL_STUBS = 0x8
S_MOD_INIT_FUNC_POINTERS = 0x9
S_MOD_TERM_FUNC_POINTERS = 0xA
S_COALESCED = 0xB
S_GB_ZEROFILL = 0xC
S_INTERPOSING = 0xD
S_16BYTE_LITERALS = 0xE
S_DTRACE_DOF = 0xF
S_LAZY_DYLIB_SYMBOL_POINTERS = 0x10
S_THREAD_LOCAL_REGULAR = 0x11
S_THREAD_LOCAL_ZEROFILL = 0x12
S_THREAD_LOCAL_VARIABLES = 0x13
S_THREAD_LOCAL_VARIABLE_POINTERS = 0x14
S_THREAD_LOCAL_INIT_FUNCTION_POINTERS = 0x15
FLAG_SECTION_TYPES = {
S_REGULAR: "S_REGULAR",
S_ZEROFILL: "S_ZEROFILL",
S_CSTRING_LITERALS: "S_CSTRING_LITERALS",
S_4BYTE_LITERALS: "S_4BYTE_LITERALS",
S_8BYTE_LITERALS: "S_8BYTE_LITERALS",
S_LITERAL_POINTERS: "S_LITERAL_POINTERS",
S_NON_LAZY_SYMBOL_POINTERS: "S_NON_LAZY_SYMBOL_POINTERS",
S_LAZY_SYMBOL_POINTERS: "S_LAZY_SYMBOL_POINTERS",
S_SYMBOL_STUBS: "S_SYMBOL_STUBS",
S_MOD_INIT_FUNC_POINTERS: "S_MOD_INIT_FUNC_POINTERS",
S_MOD_TERM_FUNC_POINTERS: "S_MOD_TERM_FUNC_POINTERS",
S_COALESCED: "S_COALESCED",
S_GB_ZEROFILL: "S_GB_ZEROFILL",
S_INTERPOSING: "S_INTERPOSING",
S_16BYTE_LITERALS: "S_16BYTE_LITERALS",
S_DTRACE_DOF: "S_DTRACE_DOF",
S_LAZY_DYLIB_SYMBOL_POINTERS: "S_LAZY_DYLIB_SYMBOL_POINTERS",
S_THREAD_LOCAL_REGULAR: "S_THREAD_LOCAL_REGULAR",
S_THREAD_LOCAL_ZEROFILL: "S_THREAD_LOCAL_ZEROFILL",
S_THREAD_LOCAL_VARIABLES: "S_THREAD_LOCAL_VARIABLES",
S_THREAD_LOCAL_VARIABLE_POINTERS: "S_THREAD_LOCAL_VARIABLE_POINTERS",
S_THREAD_LOCAL_INIT_FUNCTION_POINTERS: "S_THREAD_LOCAL_INIT_FUNCTION_POINTERS",
}
SECTION_ATTRIBUTES_USR = 0xFF000000
S_ATTR_PURE_INSTRUCTIONS = 0x80000000
S_ATTR_NO_TOC = 0x40000000
S_ATTR_STRIP_STATIC_SYMS = 0x20000000
S_ATTR_NO_DEAD_STRIP = 0x10000000
S_ATTR_LIVE_SUPPORT = 0x08000000
S_ATTR_SELF_MODIFYING_CODE = 0x04000000
S_ATTR_DEBUG = 0x02000000
SECTION_ATTRIBUTES_SYS = 0x00FFFF00
S_ATTR_SOME_INSTRUCTIONS = 0x00000400
S_ATTR_EXT_RELOC = 0x00000200
S_ATTR_LOC_RELOC = 0x00000100
FLAG_SECTION_ATTRIBUTES = {
S_ATTR_PURE_INSTRUCTIONS: "S_ATTR_PURE_INSTRUCTIONS",
S_ATTR_NO_TOC: "S_ATTR_NO_TOC",
S_ATTR_STRIP_STATIC_SYMS: "S_ATTR_STRIP_STATIC_SYMS",
S_ATTR_NO_DEAD_STRIP: "S_ATTR_NO_DEAD_STRIP",
S_ATTR_LIVE_SUPPORT: "S_ATTR_LIVE_SUPPORT",
S_ATTR_SELF_MODIFYING_CODE: "S_ATTR_SELF_MODIFYING_CODE",
S_ATTR_DEBUG: "S_ATTR_DEBUG",
S_ATTR_SOME_INSTRUCTIONS: "S_ATTR_SOME_INSTRUCTIONS",
S_ATTR_EXT_RELOC: "S_ATTR_EXT_RELOC",
S_ATTR_LOC_RELOC: "S_ATTR_LOC_RELOC",
}
SEG_PAGEZERO = "__PAGEZERO"
SEG_TEXT = "__TEXT"
SECT_TEXT = "__text"
SECT_FVMLIB_INIT0 = "__fvmlib_init0"
SECT_FVMLIB_INIT1 = "__fvmlib_init1"
SEG_DATA = "__DATA"
SECT_DATA = "__data"
SECT_BSS = "__bss"
SECT_COMMON = "__common"
SEG_OBJC = "__OBJC"
SECT_OBJC_SYMBOLS = "__symbol_table"
SECT_OBJC_MODULES = "__module_info"
SECT_OBJC_STRINGS = "__selector_strs"
SECT_OBJC_REFS = "__selector_refs"
SEG_ICON = "__ICON"
SECT_ICON_HEADER = "__header"
SECT_ICON_TIFF = "__tiff"
SEG_LINKEDIT = "__LINKEDIT"
SEG_UNIXSTACK = "__UNIXSTACK"
SEG_IMPORT = "__IMPORT"
#
# I really should remove all these _command classes because they
# are no different. I decided to keep the load commands separate,
# so classes like fvmlib and fvmlib_command are equivalent.
#
class fvmlib(Structure):
_fields_ = (
("name", lc_str),
("minor_version", mach_version_helper),
("header_addr", p_uint32),
)
class fvmlib_command(Structure):
_fields_ = fvmlib._fields_
def describe(self):
s = {}
s["header_addr"] = int(self.header_addr)
return s
class dylib(Structure):
_fields_ = (
("name", lc_str),
("timestamp", mach_timestamp_helper),
("current_version", mach_version_helper),
("compatibility_version", mach_version_helper),
)
# merged dylib structure
class dylib_command(Structure):
_fields_ = dylib._fields_
def describe(self):
s = {}
s["timestamp"] = str(self.timestamp)
s["current_version"] = str(self.current_version)
s["compatibility_version"] = str(self.compatibility_version)
return s
class sub_framework_command(Structure):
_fields_ = (("umbrella", lc_str),)
def describe(self):
return {}
class sub_client_command(Structure):
_fields_ = (("client", lc_str),)
def describe(self):
return {}
class sub_umbrella_command(Structure):
_fields_ = (("sub_umbrella", lc_str),)
def describe(self):
return {}
class sub_library_command(Structure):
_fields_ = (("sub_library", lc_str),)
def describe(self):
return {}
class prebound_dylib_command(Structure):
_fields_ = (("name", lc_str), ("nmodules", p_uint32), ("linked_modules", lc_str))
def describe(self):
return {"nmodules": int(self.nmodules)}
class dylinker_command(Structure):
_fields_ = (("name", lc_str),)
def describe(self):
return {}
class thread_command(Structure):
_fields_ = (("flavor", p_uint32), ("count", p_uint32))
def describe(self):
s = {}
s["flavor"] = int(self.flavor)
s["count"] = int(self.count)
return s
class entry_point_command(Structure):
_fields_ = (("entryoff", p_uint64), ("stacksize", p_uint64))
def describe(self):
s = {}
s["entryoff"] = int(self.entryoff)
s["stacksize"] = int(self.stacksize)
return s
class routines_command(Structure):
_fields_ = (
("init_address", p_uint32),
("init_module", p_uint32),
("reserved1", p_uint32),
("reserved2", p_uint32),
("reserved3", p_uint32),
("reserved4", p_uint32),
("reserved5", p_uint32),
("reserved6", p_uint32),
)
def describe(self):
s = {}
s["init_address"] = int(self.init_address)
s["init_module"] = int(self.init_module)
s["reserved1"] = int(self.reserved1)
s["reserved2"] = int(self.reserved2)
s["reserved3"] = int(self.reserved3)
s["reserved4"] = int(self.reserved4)
s["reserved5"] = int(self.reserved5)
s["reserved6"] = int(self.reserved6)
return s
class routines_command_64(Structure):
_fields_ = (
("init_address", p_uint64),
("init_module", p_uint64),
("reserved1", p_uint64),
("reserved2", p_uint64),
("reserved3", p_uint64),
("reserved4", p_uint64),
("reserved5", p_uint64),
("reserved6", p_uint64),
)
def describe(self):
s = {}
s["init_address"] = int(self.init_address)
s["init_module"] = int(self.init_module)
s["reserved1"] = int(self.reserved1)
s["reserved2"] = int(self.reserved2)
s["reserved3"] = int(self.reserved3)
s["reserved4"] = int(self.reserved4)
s["reserved5"] = int(self.reserved5)
s["reserved6"] = int(self.reserved6)
return s
class symtab_command(Structure):
_fields_ = (
("symoff", p_uint32),
("nsyms", p_uint32),
("stroff", p_uint32),
("strsize", p_uint32),
)
def describe(self):
s = {}
s["symoff"] = int(self.symoff)
s["nsyms"] = int(self.nsyms)
s["stroff"] = int(self.stroff)
s["strsize"] = int(self.strsize)
return s
class dysymtab_command(Structure):
_fields_ = (
("ilocalsym", p_uint32),
("nlocalsym", p_uint32),
("iextdefsym", p_uint32),