-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmacho.py
More file actions
executable file
·4609 lines (4104 loc) · 176 KB
/
Copy pathmacho.py
File metadata and controls
executable file
·4609 lines (4104 loc) · 176 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
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
#!/usr/bin/env python3
import binascii
import cmd
from collections import defaultdict
import copy
import dict_utils
import dwarf.context
import dwarf.options
from dwarf.ranges import AddressRange, AddressRangeList
from enum import IntEnum
import file_extract
import filecmp
import io
import optparse
import os
import re
import shlex
import struct
import string
import sys
import term_colors
import uuid
# import Tkinter
# from Tkinter import *
# from ttk import *
UINT32_MAX = 4294967295
# Mach header "magic" constants
MH_MAGIC = 0xfeedface
MH_CIGAM = 0xcefaedfe
MH_MAGIC_64 = 0xfeedfacf
MH_CIGAM_64 = 0xcffaedfe
FAT_MAGIC = 0xcafebabe
FAT_CIGAM = 0xbebafeca
FAT_MAGIC_64 = 0xcafebabf
FAT_CIGAM_64 = 0xbfbafeca
# Mach haeder "filetype" constants
MH_OBJECT = 0x00000001
MH_EXECUTE = 0x00000002
MH_FVMLIB = 0x00000003
MH_CORE = 0x00000004
MH_PRELOAD = 0x00000005
MH_DYLIB = 0x00000006
MH_DYLINKER = 0x00000007
MH_BUNDLE = 0x00000008
MH_DYLIB_STUB = 0x00000009
MH_DSYM = 0x0000000a
MH_KEXT_BUNDLE = 0x0000000b
# Mach haeder "flag" constant bits
MH_NOUNDEFS = 0x00000001
MH_INCRLINK = 0x00000002
MH_DYLDLINK = 0x00000004
MH_BINDATLOAD = 0x00000008
MH_PREBOUND = 0x00000010
MH_SPLIT_SEGS = 0x00000020
MH_LAZY_INIT = 0x00000040
MH_TWOLEVEL = 0x00000080
MH_FORCE_FLAT = 0x00000100
MH_NOMULTIDEFS = 0x00000200
MH_NOFIXPREBINDING = 0x00000400
MH_PREBINDABLE = 0x00000800
MH_ALLMODSBOUND = 0x00001000
MH_SUBSECTIONS_VIA_SYMBOLS = 0x00002000
MH_CANONICAL = 0x00004000
MH_WEAK_DEFINES = 0x00008000
MH_BINDS_TO_WEAK = 0x00010000
MH_ALLOW_STACK_EXECUTION = 0x00020000
MH_ROOT_SAFE = 0x00040000
MH_SETUID_SAFE = 0x00080000
MH_NO_REEXPORTED_DYLIBS = 0x00100000
MH_PIE = 0x00200000
MH_DEAD_STRIPPABLE_DYLIB = 0x00400000
MH_HAS_TLV_DESCRIPTORS = 0x00800000
MH_NO_HEAP_EXECUTION = 0x01000000
# Mach load command constants
LC_REQ_DYLD = 0x80000000
LC_SEGMENT = 0x00000001
LC_SYMTAB = 0x00000002
LC_SYMSEG = 0x00000003
LC_THREAD = 0x00000004
LC_UNIXTHREAD = 0x00000005
LC_LOADFVMLIB = 0x00000006
LC_IDFVMLIB = 0x00000007
LC_IDENT = 0x00000008
LC_FVMFILE = 0x00000009
LC_PREPAGE = 0x0000000a
LC_DYSYMTAB = 0x0000000b
LC_LOAD_DYLIB = 0x0000000c
LC_ID_DYLIB = 0x0000000d
LC_LOAD_DYLINKER = 0x0000000e
LC_ID_DYLINKER = 0x0000000f
LC_PREBOUND_DYLIB = 0x00000010
LC_ROUTINES = 0x00000011
LC_SUB_FRAMEWORK = 0x00000012
LC_SUB_UMBRELLA = 0x00000013
LC_SUB_CLIENT = 0x00000014
LC_SUB_LIBRARY = 0x00000015
LC_TWOLEVEL_HINTS = 0x00000016
LC_PREBIND_CKSUM = 0x00000017
LC_LOAD_WEAK_DYLIB = 0x00000018 | LC_REQ_DYLD
LC_SEGMENT_64 = 0x00000019
LC_ROUTINES_64 = 0x0000001a
LC_UUID = 0x0000001b
LC_RPATH = 0x0000001c | LC_REQ_DYLD
LC_CODE_SIGNATURE = 0x0000001d
LC_SEGMENT_SPLIT_INFO = 0x0000001e
LC_REEXPORT_DYLIB = 0x0000001f | LC_REQ_DYLD
LC_LAZY_LOAD_DYLIB = 0x00000020
LC_ENCRYPTION_INFO = 0x00000021
LC_DYLD_INFO = 0x00000022
LC_DYLD_INFO_ONLY = 0x00000022 | LC_REQ_DYLD
LC_LOAD_UPWARD_DYLIB = 0x00000023 | LC_REQ_DYLD
LC_VERSION_MIN_MACOSX = 0x00000024
LC_VERSION_MIN_IPHONEOS = 0x00000025
LC_FUNCTION_STARTS = 0x00000026
LC_DYLD_ENVIRONMENT = 0x00000027
LC_MAIN = 0x00000028 | LC_REQ_DYLD
LC_DATA_IN_CODE = 0x00000029
LC_SOURCE_VERSION = 0x0000002A
LC_DYLIB_CODE_SIGN_DRS = 0x0000002B
LC_ENCRYPTION_INFO_64 = 0x0000002C
LC_LINKER_OPTION = 0x0000002D
LC_LINKER_OPTIMIZATION_HINT = 0x0000002E
LC_VERSION_MIN_TVOS = 0x0000002F
LC_VERSION_MIN_WATCHOS = 0x00000030
LC_NOTE = 0x31
LC_BUILD_VERSION = 0x32
LC_DYLD_EXPORTS_TRIE = (0x33 | LC_REQ_DYLD)
LC_DYLD_CHAINED_FIXUPS = (0x34 | LC_REQ_DYLD)
# Segment flags
SG_HIGHVM = 0x00000001
SG_FVMLIB = 0x00000002
SG_NORELOC = 0x00000004
SG_PROTECTED_VERSION_1 = 0x00000008
# Section flags
SECTION_TYPE = 0x000000ff
SECTION_ATTRIBUTES = 0xffffff00
# Section type constants
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
# Section attribute constants
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
# Mach CPU constants
CPU_ARCH_MASK = 0xff000000
CPU_ARCH_ABI64 = 0x01000000
CPU_TYPE_ANY = 0xffffffff
CPU_TYPE_VAX = 1
CPU_TYPE_MC680x0 = 6
CPU_TYPE_I386 = 7
CPU_TYPE_X86_64 = CPU_TYPE_I386 | CPU_ARCH_ABI64
CPU_TYPE_MIPS = 8
CPU_TYPE_MC98000 = 10
CPU_TYPE_HPPA = 11
CPU_TYPE_ARM = 12
CPU_TYPE_MC88000 = 13
CPU_TYPE_SPARC = 14
CPU_TYPE_I860 = 15
CPU_TYPE_ALPHA = 16
CPU_TYPE_POWERPC = 18
CPU_TYPE_POWERPC64 = CPU_TYPE_POWERPC | CPU_ARCH_ABI64
CPU_TYPE_ARM64 = CPU_TYPE_ARM | CPU_ARCH_ABI64
# VM protection constants
VM_PROT_READ = 1
VM_PROT_WRITE = 2
VM_PROT_EXECUTE = 4
# VM protection constants
N_STAB = 0xe0
N_PEXT = 0x10
N_TYPE = 0x0e
N_EXT = 0x01
# Values for nlist N_TYPE bits of the "Mach.NList.type" field.
N_UNDF = 0x0
N_ABS = 0x2
N_SECT = 0xe
N_PBUD = 0xc
N_INDR = 0xa
# Section indexes for the "Mach.NList.sect_idx" fields
NO_SECT = 0
MAX_SECT = 255
# Stab defines
class Stab(IntEnum):
N_GSYM = 0x20
N_FNAME = 0x22
N_FUN = 0x24
N_STSYM = 0x26
N_LCSYM = 0x28
N_BNSYM = 0x2e
N_OPT = 0x3c
N_RSYM = 0x40
N_SLINE = 0x44
N_ENSYM = 0x4e
N_SSYM = 0x60
N_SO = 0x64
N_OSO = 0x66
N_LSYM = 0x80
N_BINCL = 0x82
N_SOL = 0x84
N_PARAMS = 0x86
N_VERSION = 0x88
N_OLEVEL = 0x8A
N_PSYM = 0xa0
N_EINCL = 0xa2
N_ENTRY = 0xa4
N_LBRAC = 0xc0
N_EXCL = 0xc2
N_RBRAC = 0xe0
N_BCOMM = 0xe2
N_ECOMM = 0xe4
N_ECOML = 0xe8
N_LENG = 0xfe
def __str__(self):
return self.name
# Platform definitions for LC_BUILD_VERSION
PLATFORM_INVALID = 0
PLATFORM_MACOS = 1
PLATFORM_IOS = 2
PLATFORM_TVOS = 3
PLATFORM_WATCHOS = 4
PLATFORM_BRIDGEOS = 5
PLATFORM_MACCATALYST = 6
PLATFORM_IOSSIMULATOR = 7
PLATFORM_TVOSSIMULATOR = 8
PLATFORM_WATCHOSSIMULATOR = 9
PLATFORM_DRIVERKIT = 10
# Tool definitions for LC_BUILD_VERSION
TOOL_CLANG = 1
TOOL_SWIFT = 2
TOOL_LD = 3
vm_prot_names = ['---', 'r--', '-w-', 'rw-', '--x', 'r-x', '-wx', 'rwx']
def get_version32_as_string(v):
return "%u.%u.%u" % (v >> 16, (v >> 8) & 0xff, v & 0xff)
def int_to_hex16(i):
return '0x%4.4x' % (i)
def int_to_hex32(i):
return '0x%8.8x' % (i)
def int_to_hex64(i):
return '0x%16.16x' % (i)
def address_to_str(addr, is_64):
if is_64:
return int_to_hex64(addr)
else:
return int_to_hex32(addr)
def address_range_to_str(i, j, is_64):
if is_64:
return '[%s - %s)' % (int_to_hex64(i), int_to_hex64(j))
else:
return '[%s - %s)' % (int_to_hex32(i), int_to_hex32(j))
def sizeof_fmt(num):
for unit in ['B', 'K', 'M', 'G', 'T', 'P', 'E', 'Z']:
if abs(num) < 1024.0:
return "%3.1f%s" % (num, unit)
num /= 1024.0
return "%.1f%s" % (num, 'Y')
def swap_unpack_char():
"""Returns the unpack prefix that will for non-native endian-ness."""
if struct.pack('H', 1).startswith("\x00"):
return '<'
return '>'
def dump_hex_bytes(addr, s, bytes_per_line=16):
i = 0
line = ''
for ch in s:
if (i % bytes_per_line) == 0:
if line:
print(line)
line = '%#8.8x: ' % (addr + i)
line += "%02X " % ord(ch)
i += 1
print(line)
def dump_hex_byte_string_diff(addr, a, b, bytes_per_line=16):
i = 0
line = ''
a_len = len(a)
b_len = len(b)
if a_len < b_len:
max_len = b_len
else:
max_len = a_len
tty_colors = term_colors.TerminalColors(True)
for i in range(max_len):
ch = None
if i < a_len:
ch_a = a[i]
ch = ch_a
else:
ch_a = None
if i < b_len:
ch_b = b[i]
if not ch:
ch = ch_b
else:
ch_b = None
mismatch = ch_a != ch_b
if (i % bytes_per_line) == 0:
if line:
print(line)
line = '%#8.8x: ' % (addr + i)
if mismatch:
line += tty_colors.red()
line += "%02X " % ord(ch)
if mismatch:
line += tty_colors.default()
i += 1
print(line)
def get_mach_files_in_directory(path, options, mach_files, depth=0):
for dirpath, dirs, files in os.walk(path):
for file in files:
fullpath = os.path.join(dirpath, file)
if options.verbose:
print('parsing: %s' % (fullpath))
mach = Mach.load(fullpath)
if mach:
mach_files.append(mach)
elif options.verbose:
print('not a mach file')
def compare_size_in_directories(a_dir, b_dir):
a_dir = os.path.normpath(a_dir)
b_dir = os.path.normpath(b_dir)
a_machs = []
b_machs = []
get_mach_files_in_directory(a_dir, options, a_machs)
get_mach_files_in_directory(b_dir, options, b_machs)
handled_files = {}
for (i, a_mach) in enumerate(a_machs):
a_relative_path = a_mach.path[len(a_dir)+1:]
b_mach = None
if i < len(b_machs) and b_machs[i].path.endswith(a_relative_path):
b_mach = b_machs[i]
else:
for mach in b_machs:
if mach.path.endswith(a_relative_path):
b_mach = mach
break
handled_files[a_relative_path] = True
if b_mach:
if filecmp.cmp(a_mach.path, b_mach.path):
print('"%s" files are the same' % (a_relative_path))
else:
a_mach.compare_size(b_mach)
else:
print('error: "%s" missing from "%s"' % (a_relative_path, b_dir))
for mach in b_machs:
b_relative_path = mach.path[len(b_dir)+1:]
if b_relative_path in handled_files:
continue
else:
print('error: "%s" missing from "%s"' % (b_relative_path, a_dir))
class OutputHelper:
def __init__(self, depth=0, file=sys.stdout):
self.depth = depth
self.file = file
def indent_more(self):
self.depth += 2
def indent_less(self):
self.depth -= 2
if self.depth < 0:
self.depth = 0
def indent(self, s=None, newline=True):
if self.depth:
self.file.write(' ' * self.depth)
if s:
self.file.write(s)
if newline:
self.file.write('\n')
class method_t:
def __init__(self, objc_class, data):
self.objc_class = objc_class
self.offset = data.tell()
self.SEL = data.get_address()
self.types = data.get_address()
self.imp = data.get_address()
self.SEL_str = None
self.types_str = None
def get_selector(self):
if self.SEL_str is None:
self.SEL_str = self.objc_class.mach.read_c_string_from_addr(
self.SEL)
return self.SEL_str
def get_types(self):
if self.types_str is None:
self.types_str = self.objc_class.mach.read_c_string_from_addr(
self.types)
return self.types_str
def dump(self, out, verbose=False):
if verbose:
out.indent("method_t @ %#x:" % (self.offset))
out.indent_more()
out.indent("SEL: %#x (%s)" % (self.SEL, self.get_selector()))
out.indent("types: %#x" % (self.types))
out.indent("imp: %#x" % (self.imp))
out.indent_less()
else:
out.indent("%#16.16x: %s %s" % (self.imp, self.get_selector(),
self.get_types()))
class method_list_t:
'''Reads a method_list_t type from the mach file at address'''
def __init__(self, objc_class, addr, name):
self.objc_class = objc_class
self.name = name
data = objc_class.mach.seek_to_addr(addr)
self.offset = data.tell()
self.entsize_NEVER_USE = data.get_uint32()
self.count = data.get_uint32()
self.methods = []
for i in range(self.count):
self.methods.append(method_t(self.objc_class, data))
def dump(self, out, verbose=False):
out.indent("%s:" % (self.name))
out.indent_more()
for method in self.methods:
method.dump(out, verbose)
out.indent_less()
class property_t:
def __init__(self, objc_class, data):
self.objc_class = objc_class
self.name_ptr = data.get_address()
self.attributes_ptr = data.get_address()
self.name = None
self.attributes = None
def get_name(self):
if self.name is None:
self.name = self.objc_class.mach.read_c_string_from_addr(
self.name_ptr)
return self.name
def get_attributes(self):
if self.attributes is None:
self.attributes = self.objc_class.mach.read_c_string_from_addr(
self.attributes_ptr)
return self.attributes
def dump(self, out, verbose=False):
out.indent("%s %s" % (self.get_name(), self.get_attributes()))
class property_list_t:
'''Reads a property_list_t type from the mach file at address'''
def __init__(self, objc_class, addr, name):
self.objc_class = objc_class
self.name = name
data = objc_class.mach.seek_to_addr(addr)
self.offset = data.tell()
self.entsize = data.get_uint32()
self.count = data.get_uint32()
self.properties = []
for i in range(self.count):
self.properties.append(property_t(self.objc_class, data))
def dump(self, out, verbose=False):
out.indent("%s:" % (self.name))
out.indent_more()
for property in self.properties:
property.dump(out, verbose)
out.indent_less()
class ivar_t:
def __init__(self, objc_class, data):
self.objc_class = objc_class
self.offset_ptr = data.get_address()
self.name_ptr = data.get_address()
self.type_ptr = data.get_address()
self.alignment = data.get_uint32()
self.size = data.get_uint32()
self.name = None
self.type = None
self.offset = None
def get_offset(self):
if self.offset is None:
self.offset = self.objc_class.mach.seek_to_addr(
self.offset_ptr).get_address()
return self.offset
def get_name(self):
if self.name is None:
self.name = self.objc_class.mach.read_c_string_from_addr(
self.name_ptr)
return self.name
def get_type(self):
if self.type is None:
self.type = self.objc_class.mach.read_c_string_from_addr(
self.type_ptr)
return self.type
def dump(self, out, verbose=False):
out.indent("%s: offset=%i, type=%s, align=%i, size=%i" % (
self.get_name(), self.get_offset(), self.get_type(),
self.alignment, self.size))
class ivar_list_t:
'''Reads a ivar_list_t type from the mach file at address'''
def __init__(self, objc_class, addr, name):
self.objc_class = objc_class
self.name = name
data = objc_class.mach.seek_to_addr(addr)
self.offset = data.tell()
self.entsize = data.get_uint32()
self.count = data.get_uint32()
self.ivars = []
for i in range(self.count):
self.ivars.append(ivar_t(self.objc_class, data))
def dump(self, out, verbose=False):
out.indent("%s:" % (self.name))
out.indent_more()
for ivar in self.ivars:
ivar.dump(out, verbose)
out.indent_less()
class protocol_t:
def __init__(self, objc_class, addr):
data = objc_class.mach.seek_to_addr(addr)
self.objc_class = objc_class
self.offset = data.tell()
self.isa = data.get_address()
self.name_ptr = data.get_address()
self.protocols_ptr = data.get_address()
self.instanceMethods_ptr = data.get_address()
self.classMethods_ptr = data.get_address()
self.optionalInstanceMethods_ptr = data.get_address()
self.optionalClassMethods_ptr = data.get_address()
self.instanceProperties_ptr = data.get_address()
self.size = data.get_uint32() # sizeof(protocol_t)
self.flags = data.get_uint32()
self.extendedMethodTypes = data.get_address()
self.name = None
self.protocols = None
self.instanceMethods = None
self.classMethods = None
self.optionalInstanceMethods = None
self.optionalClassMethods = None
self.instanceProperties = None
def get_protocols(self):
if self.protocols is None and self.protocols_ptr != 0:
self.protocols = protocol_list_t(self.objc_class,
self.protocols_ptr,
"protocols")
return self.protocols
def get_instanceMethods(self):
if self.instanceMethods is None and self.instanceMethods_ptr != 0:
self.instanceMethods = method_list_t(self.objc_class,
self.instanceMethods_ptr,
"instanceMethods")
return self.instanceMethods
def get_classMethods(self):
if self.classMethods is None and self.classMethods_ptr != 0:
self.classMethods = method_list_t(self.objc_class,
self.classMethods_ptr,
"classMethods")
return self.classMethods
def get_optionalInstanceMethods(self):
if (self.optionalInstanceMethods is None and
self.optionalInstanceMethods_ptr != 0):
self.optionalInstanceMethods = method_list_t(
self.objc_class, self.optionalInstanceMethods_ptr,
"optionalInstanceMethods")
return self.optionalInstanceMethods
def get_optionalClassMethods(self):
if (self.optionalClassMethods is None and
self.optionalClassMethods_ptr != 0):
self.optionalClassMethods = method_list_t(
self.objc_class, self.optionalClassMethods_ptr,
"optionalClassMethods")
return self.optionalClassMethods
def get_instanceProperties(self):
if (self.instanceProperties is None and
self.instanceProperties_ptr != 0):
self.instanceProperties = property_list_t(
self.objc_class, self.instanceProperties_ptr,
"instanceProperties")
return self.instanceProperties
def get_name(self):
if self.name is None:
self.name = self.objc_class.mach.read_c_string_from_addr(
self.name_ptr)
return self.name
def dump(self, out, verbose=False, brief=False):
if verbose:
out.indent("protocol_t @ %#x:" % (self.offset))
out.indent("isa: %#x" % (self.isa))
out.indent("name: %#x (%s)" % (self.name_ptr, self.get_name()))
out.indent("protocols: %#x" % (self.protocols_ptr))
out.indent("instanceMethods: %#x" % (self.instanceMethods_ptr))
out.indent("classMethods: %#x" % (self.classMethods_ptr))
out.indent("optionalInstanceMethods: %#x" % (
self.optionalInstanceMethods_ptr))
out.indent("optionalClassMethods: %#x" % (
self.optionalClassMethods_ptr))
out.indent("instanceProperties: %#x" % (
self.instanceProperties_ptr))
out.indent("size: %#x" % (self.size))
out.indent("flags: %#x" % (self.flags))
out.indent("extendedMethodTypes: %#x" % (self.extendedMethodTypes))
else:
out.indent("@protocol %s" % (self.get_name()))
if brief:
return
out.indent_more()
protocols = self.get_protocols()
if protocols:
protocols.dump(out, verbose, True)
methods = self.get_instanceMethods()
if methods:
methods.dump(out, verbose)
methods = self.get_classMethods()
if methods:
methods.dump(out, verbose)
methods = self.get_optionalInstanceMethods()
if methods:
methods.dump(out, verbose)
methods = self.get_optionalClassMethods()
if methods:
methods.dump(out, verbose)
properties = self.get_instanceProperties()
if properties:
properties.dump(out, verbose)
out.indent_less()
class protocol_list_t:
'''Reads a protocol_list_t type from the mach file at address'''
def __init__(self, objc_class, addr, name):
self.objc_class = objc_class
self.name = name
data = objc_class.mach.seek_to_addr(addr)
self.offset = data.tell()
self.count = data.get_address()
self.protocol_refs = []
for i in range(self.count):
self.protocol_refs.append(data.get_address())
self.protocols = None
def get_protocols(self):
if self.protocols is None:
self.protocols = []
for protocol_ref in self.protocol_refs:
self.protocols.append(protocol_t(self.objc_class,
protocol_ref))
return self.protocols
def dump(self, out, verbose=False, brief=False):
out.indent("%s:" % (self.name))
out.indent_more()
for protocol in self.get_protocols():
protocol.dump(out, verbose, brief)
out.indent_less()
class class_ro_t:
'''Reads a class_ro_t type from the mach file'''
def __init__(self, objc_class, addr):
self.objc_class = objc_class
data = objc_class.mach.seek_to_addr(addr)
self.offset = data.tell()
self.flags = data.get_uint32()
self.instanceStart = data.get_uint32()
self.instanceSize = data.get_uint32()
if data.get_addr_size() == 8:
data.get_uint32()
self.ivarLayout = data.get_address()
self.name_ptr = data.get_address()
self.baseMethods_ptr = data.get_address()
self.baseProtocols_ptr = data.get_address()
self.ivars_ptr = data.get_address()
self.weakIvarLayout = data.get_address()
self.baseProperties_ptr = data.get_address()
data = objc_class.mach.seek_to_addr(self.name_ptr)
self.name = data.get_c_string()
self.baseMethods = None
self.baseProtocols = None
self.ivars = None
self.baseProperties = None
def get_class_name(self):
return self.name
def get_ivars(self):
if self.ivars is None and self.ivars_ptr != 0:
self.ivars = ivar_list_t(self.objc_class, self.ivars_ptr, "ivars")
return self.ivars
def get_baseMethods(self):
if self.baseMethods is None and self.baseMethods_ptr != 0:
self.baseMethods = method_list_t(self.objc_class,
self.baseMethods_ptr,
"baseMethods")
return self.baseMethods
def get_baseProtocols(self):
if self.baseProtocols is None and self.baseProtocols_ptr != 0:
self.baseProtocols = protocol_list_t(self.objc_class,
self.baseProtocols_ptr,
"baseProtocols")
return self.baseProtocols
def get_baseProperties(self):
if self.baseProperties is None and self.baseProperties_ptr != 0:
self.baseProperties = property_list_t(self.objc_class,
self.baseProperties_ptr,
"baseProperties")
return self.baseProperties
def gather_stats(self, objc_stats):
method_list = self.get_baseMethods()
if method_list:
objc_stats.num_methods += method_list.count
protocols = self.get_baseProtocols()
if protocols:
objc_stats.num_protocols += protocols.count
ivars = self.get_ivars()
if ivars:
objc_stats.num_ivars += ivars.count
properties = self.get_baseProperties()
if properties:
objc_stats.num_properties += properties.count
def dump(self, out, verbose=False):
if verbose:
out.indent("class_ro_t @ %#x:" % (self.offset))
out.indent(" flags: %#x" % (self.flags))
out.indent(" instanceStart: %#x" % (self.instanceStart))
out.indent(" instanceSize: %#x" % (self.instanceSize))
out.indent(" ivarLayout: %#x" % (self.ivarLayout))
out.indent(" name: %#x (%s)" % (self.name_ptr, self.name))
out.indent(" baseMethods: %#x" % (self.baseMethods_ptr))
out.indent(" baseProtocols: %#x" % (self.baseProtocols_ptr))
out.indent(" ivars: %#x" % (self.ivars_ptr))
out.indent(" weakIvarLayout: %#x" % (self.weakIvarLayout))
out.indent(" baseProperties: %#x" % (self.baseProperties_ptr))
method_list = self.get_baseMethods()
if method_list:
method_list.dump(out, verbose)
protocols = self.get_baseProtocols()
if protocols:
protocols.dump(out, verbose)
ivars = self.get_ivars()
if ivars:
ivars.dump(out, verbose)
properties = self.get_baseProperties()
if properties:
properties.dump(out, verbose)
class class_t:
def __init__(self, isa, mach):
self.isa = isa
self.mach = mach
data = mach.seek_to_addr(isa)
self.super_isa = data.get_address()
self.cache = data.get_address()
self.vtable = data.get_address()
self.class_rw = data.get_address()
self.class_ro = data.get_address()
self.objc_class_ro = None
def dump(self, out, verbose=False):
out.indent()
if verbose:
out.indent(" isa: %#x" % (self.isa))
out.indent(" super: %#x" % (self.super_isa))
out.indent(" cache: %#x" % (self.cache))
out.indent(" vtable: %#x" % (self.vtable))
out.indent("class_rw: %#x" % (self.class_rw))
out.indent("class_ro: %#x" % (self.class_ro))
else:
out.indent("@class %s isa=%#x" % (self.get_name(), self.isa))
out.indent_more()
class_ro = self.get_class_ro()
class_ro.dump(out, verbose)
out.indent_less()
def gather_stats(self, objc_stats):
objc_stats.num_classes += 1
self.get_class_ro().gather_stats(objc_stats)
def get_name(self):
return self.get_class_ro().get_class_name()
def get_class_ro(self):
if self.objc_class_ro is not None:
return self.objc_class_ro
self.objc_class_ro = class_ro_t(self, self.class_ro)
return self.objc_class_ro
class ObjcStats:
def __init__(self):
self.num_classes = 0
self.num_methods = 0
self.num_protocols = 0
self.num_properties = 0
self.num_ivars = 0
def aggregate(self, objc_stats):
self.num_classes += objc_stats.num_classes
self.num_methods += objc_stats.num_methods
self.num_protocols += objc_stats.num_protocols
self.num_properties += objc_stats.num_properties
self.num_ivars += objc_stats.num_ivars
def dump(self):
print(' Classes: %i' % (self.num_classes))
print(' Methods: %i' % (self.num_methods))
print(' Protocols: %i' % (self.num_protocols))
print('Properties: %i' % (self.num_properties))
print(' IVars: %i' % (self.num_ivars))
UNWIND_IS_NOT_FUNCTION_START = 0x80000000
UNWIND_HAS_LSDA = 0x40000000
UNWIND_PERSONALITY_MASK = 0x30000000
UNWIND_SECOND_LEVEL_REGULAR = 2
UNWIND_SECOND_LEVEL_COMPRESSED = 3
def UNWIND_INFO_COMPRESSED_ENTRY_FUNC_OFFSET(entry):
return entry & 0x00FFFFFF
def UNWIND_INFO_COMPRESSED_ENTRY_ENCODING_INDEX(entry):
return ((entry >> 24) & 0xFF)
class unwind_info_regular_second_level_entry:
'''
struct unwind_info_regular_second_level_entry {
uint32_t functionOffset;
uint32_t encoding;
};
'''
def __init__(self, data):
self.functionOffset = data.get_uint32()
self.encoding = data.get_uint32()
self.in_common = False
@classmethod
def dump_header(self, f):
f.write('INDEX FUNC OFF ENCODING COMMON\n')
f.write('===== ---------- ---------- ======\n')
def dump(self, f, i):
f.write('%5u %#8.8x %#8.8x %s\n' % (i, self.functionOffset, self.encoding, self.in_common))
class unwind_info_regular_second_level_page_header:
'''
struct unwind_info_regular_second_level_page_header {
uint32_t kind; // UNWIND_SECOND_LEVEL_REGULAR
uint16_t entryPageOffset;
uint16_t entryCount;
// entry array
};
'''
def __init__(self, data, common_encodings):
# We assume "kind" has already been decoded as is 2
self.entryPageOffset = data.get_uint16()
self.entryCount = data.get_uint16()
self.entries = []
self.uncommon_encodings = 0
for _i in range(self.entryCount):
entry = unwind_info_regular_second_level_entry(data)
entry.in_common = entry.encoding in common_encodings
if not entry.in_common:
self.uncommon_encodings += 1
self.entries.append(entry)
def dump(self, f):
f.write('unwind_info_regular_second_level_page_header:\n')
f.write(' kind = 0x00000002 (UNWIND_SECOND_LEVEL_REGULAR)\n')
f.write(' entryPageOffset = %#4.4x\n' % (self.entryPageOffset))
f.write(' entryCount = %#4.4x\n' % (self.entryCount))
f.write(' uncommon_encodings = %u\n' % (self.uncommon_encodings))
if self.uncommon_encodings == 0:
f.write(' warning: this could have been encoded as '
'UNWIND_SECOND_LEVEL_COMPRESSED\n')
unwind_info_regular_second_level_entry.dump_header(f)
for (i, entry) in enumerate(self.entries):
entry.dump(f, i)
def get_address_and_encodings(self, base_addr):
addr_and_encodings = []
for entry in self.entries:
addr_and_encodings.append((base_addr + entry.functionOffset,
entry.encoding))
return addr_and_encodings
class unwind_info_compressed_second_level_page_header:
'''
struct unwind_info_compressed_second_level_page_header {
uint32_t kind; // UNWIND_SECOND_LEVEL_COMPRESSED
uint16_t entryPageOffset;
uint16_t entryCount;