-
Notifications
You must be signed in to change notification settings - Fork 708
/
MachOLayout.mm
2558 lines (2280 loc) · 108 KB
/
MachOLayout.mm
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
/*
* MachOLayout.mm
* MachOView
*
* Created by psaghelyi on 15/06/2010.
*
*/
#import "Common.h"
#import "MachOLayout.h"
#import "DataController.h"
#import "ReadWrite.h"
#import "LoadCommands.h"
#import "LinkEdit.h"
#import "DyldInfo.h"
#import "Exceptions.h"
#import "SectionContents.h"
#import "ObjC.h"
#import "CRTFootPrints.h"
#import <mach-o/loader.h>
#import <mach-o/nlist.h>
#import <mach-o/reloc.h>
using namespace std;
//============================================================================
@implementation MachOLayout
//-----------------------------------------------------------------------------
- (instancetype)initWithDataController:(MVDataController *)dc rootNode:(MVNode *)node
{
if (self = [super initWithDataController:dc rootNode:node])
{
symbolNames = [[NSMutableDictionary alloc] init];
}
return self;
}
//-----------------------------------------------------------------------------
+ (MachOLayout *)layoutWithDataController:(MVDataController *)dc rootNode:(MVNode *)node
{
return [[MachOLayout alloc] initWithDataController:dc rootNode:node];
}
//-----------------------------------------------------------------------------
- (BOOL)is64bit
{
MATCH_STRUCT(mach_header,imageOffset);
return ((mach_header->cputype & CPU_ARCH_ABI64) == CPU_ARCH_ABI64);
}
//-----------------------------------------------------------------------------
- (BOOL)isDylibStub
{
MATCH_STRUCT(mach_header,imageOffset);
return (mach_header->filetype == MH_DYLIB_STUB);
}
//-----------------------------------------------------------------------------
- (struct section const *)getSectionByIndex:(uint32_t)index
{
static const struct section notfound = { "???", "?????", 0, 0, 0, 0, 0, 0, 0, 0, 0 };
return (index < sections.size() ? sections.at(index) : ¬found);
}
//-----------------------------------------------------------------------------
- (struct section_64 const *)getSection64ByIndex:(uint32_t)index
{
static const struct section_64 notfound = { "???", "?????", 0, 0, 0, 0, 0, 0, 0, 0, 0 };
return (index < sections_64.size() ? sections_64.at(index) : ¬found);
}
//-----------------------------------------------------------------------------
- (struct nlist const *)getSymbolByIndex:(uint32_t)index
{
static const struct nlist notfound = { 0, 0, 0, 0, 0 };
return (index < symbols.size() ? symbols.at(index) : ¬found);
}
//-----------------------------------------------------------------------------
- (struct nlist_64 const *)getSymbol64ByIndex:(uint32_t)index
{
static const struct nlist_64 notfound = { 0, 0, 0, 0, 0 };
return (index < symbols_64.size() ? symbols_64.at(index) : ¬found);
}
//-----------------------------------------------------------------------------
- (struct dylib const *)getDylibByIndex:(uint32_t)index
{
static const struct dylib notfound = { 0, 0, 0, 0 };
return (index < dylibs.size() ? dylibs.at(index) : ¬found);
}
//-----------------------------------------------------------------------------
- (NSString *)findSymbolAtRVA:(uint64_t)rva
{
NSString * symbolName = [symbolNames objectForKey:[NSNumber numberWithUnsignedLongLong:rva]];
return (symbolName != nil ? symbolName : [NSString stringWithFormat:@"0x%qX",rva]);
}
//-----------------------------------------------------------------------------
-(struct section const *)findSectionByName:(char const *)sectname
andSegment:(char const *)segname
{
for (SectionVector::const_iterator sectIter = ++sections.begin();
sectIter != sections.end(); ++sectIter)
{
struct section const * section = *sectIter;
if ((segname == NULL || strncmp(section->segname,segname,16) == 0) &&
strncmp(section->sectname,sectname,16) == 0)
{
return section;
}
}
return NULL;
}
//-----------------------------------------------------------------------------
-(struct section_64 const *)findSection64ByName:(char const *)sectname
andSegment:(char const *)segname
{
for (Section64Vector::const_iterator sectIter = ++sections_64.begin();
sectIter != sections_64.end(); ++sectIter)
{
struct section_64 const * section_64 = *sectIter;
if ((segname == NULL || strncmp(section_64->segname,segname,16) == 0) &&
strncmp(section_64->sectname,sectname,16) == 0)
{
return section_64;
}
}
return NULL;
}
//-----------------------------------------------------------------------------
// convert a file offset to the virtual address
- (uint64_t)fileOffsetToRVA: (uint64_t)offset
{
SegmentInfoMap::const_iterator segIter = segmentInfo.upper_bound(offset);
if (segIter == segmentInfo.begin()) {
[NSException raise:@"fileOffsetToRVA"
format:@"no segment found at offset 0x%llX", offset];
}
--segIter;
uint64_t segOffset = segIter->first;
uint64_t segAddr = segIter->second.first;
// XXX: missing overflow checks
return offset - segOffset + segAddr;
}
// ----------------------------------------------------------------------------
- (uint64_t)RVAToFileOffset: (uint64_t)rva
{
SectionInfoMap::const_iterator sectIter = sectionInfo.upper_bound(rva);
if (sectIter == sectionInfo.begin()) {
[NSException raise:@"RVAToFileOffset"
format:@"no section found at address 0x%llX", rva];
}
--sectIter;
uint64_t sectOffset = sectIter->second.first;
uint64_t fileOffset = sectOffset + (rva - [self fileOffsetToRVA:sectOffset]);
NSAssert1(fileOffset < [dataController.fileData length], @"rva is out of range (0x%llX)", rva);
return fileOffset;
}
// ----------------------------------------------------------------------------
- (void)addRelocAtFileOffset:(uint64_t)offset withLength:(uint64_t)length andValue:(uint64_t)value
{
[dataController.realData replaceBytesInRange:NSMakeRange(offset,length) withBytes:&value];
}
// ----------------------------------------------------------------------------
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Winitializer-overrides"
static const long hextable[] =
{
[0 ... 255] = -1, // bit aligned access into this table is considerably
['0'] = 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, // faster for most modern processors,
['A'] = 10, 11, 12, 13, 14, 15, // for the space conscious, reduce to
['a'] = 10, 11, 12, 13, 14, 15 // signed char.
};
#pragma clang diagnostic pop
/**
* @brief convert a hexidecimal string to a signed long
* will not produce or process negative numbers except
* to signal error.
*
* @param hex without decoration, case insensitive.
*
* @return -1 on error, or result (max (sizeof(long)*8)-1 bits)
*/
static inline
long hexdec(const char *hex) {
long ret = 0;
while (*hex && ret >= 0) {
ret = (ret << 4) | hextable[*hex++];
}
return ret;
}
// ----------------------------------------------------------------------------
// RAW string to RVA string converter for data source
- (NSString *)convertToRVA: (NSString *)offsetStr
{
uint64_t fileOffset = hexdec(CSTRING(offsetStr));
NSParameterAssert((long)fileOffset != -1);
if (segmentInfo.empty() ||
fileOffset < segmentInfo.begin()->first ||
fileOffset + 1 >= (--segmentInfo.end())->first + (--segmentInfo.end())->second.second)
{
return @"";
}
return [NSString stringWithFormat:@"%.8qX",[self fileOffsetToRVA:fileOffset]];
}
// ----------------------------------------------------------------------------
- (NSDictionary *)userInfoForSection:(struct section const *)section
{
if (section == NULL) return nil;
typeof(self) __weak weakSelf = self;
return [NSDictionary dictionaryWithObjectsAndKeys:
weakSelf,MVLayoutUserInfoKey,
NSSTRING(string(section->segname,16).c_str()), @"segname",
NSSTRING(string(section->sectname,16).c_str()), @"sectname",
[NSNumber numberWithUnsignedLong:section->addr], @"address",
nil];
}
//-----------------------------------------------------------------------------
- (NSDictionary *)userInfoForSection64:(struct section_64 const *)section_64
{
if (section_64 == NULL) return nil;
typeof(self) __weak weakSelf = self;
return [NSDictionary dictionaryWithObjectsAndKeys:
weakSelf,MVLayoutUserInfoKey,
NSSTRING(string(section_64->segname,16).c_str()), @"segname",
NSSTRING(string(section_64->sectname,16).c_str()), @"sectname",
[NSNumber numberWithUnsignedLongLong:section_64->addr], @"address",
nil];
}
//-----------------------------------------------------------------------------
- (NSDictionary *)userInfoForRelocs
{
typeof(self) __weak weakSelf = self;
return [NSDictionary dictionaryWithObjectsAndKeys:
weakSelf,MVLayoutUserInfoKey,
@"Relocations", MVNodeUserInfoKey,
nil];
}
//-----------------------------------------------------------------------------
- (NSDictionary *)sectionInfoForRVA:(uint64_t)rva
{
SectionInfoMap::iterator iter = sectionInfo.upper_bound(rva);
if (iter == sectionInfo.begin()) {
NSLog(@"warning: no section info found for address 0x%.8qX",rva);
return nil;
}
return (--iter)->second.second;
}
//-----------------------------------------------------------------------------
- (NSString *)findSectionContainsRVA:(uint64_t)rva
{
NSDictionary * userInfo = [self sectionInfoForRVA:rva];
return (userInfo ? [NSString stringWithFormat:@"%8s %-16s",
CSTRING([userInfo objectForKey:@"segname"]),
CSTRING([userInfo objectForKey:@"sectname"])] : @"NO SECTION ");
}
//------------------------------------------------------------------------------
- (MVNode *)sectionNodeContainsRVA:(uint64_t)rva
{
NSDictionary * userInfo = [self sectionInfoForRVA:rva];
return (userInfo ? [self findNodeByUserInfo:userInfo] : nil);
}
//-----------------------------------------------------------------------------
-(void) processLinkEdit
{
// find related load commands
struct symtab_command const * symtab_command = NULL;
struct dysymtab_command const * dysymtab_command = NULL;
struct twolevel_hints_command const * twolevel_hints_command = NULL;
struct linkedit_data_command const * segment_split_info = NULL;
struct linkedit_data_command const * code_signature = NULL;
struct linkedit_data_command const * function_starts = NULL;
struct linkedit_data_command const * data_in_code_entries = NULL;
MATCH_STRUCT(mach_header,imageOffset);
uint32_t base_addr;
uint32_t seg1addr = (uint32_t)-1;
uint32_t segs_read_write_addr = (uint32_t)-1;
for (CommandVector::const_iterator cmdIter = commands.begin(); cmdIter != commands.end(); ++cmdIter)
{
struct load_command const * load_command = *cmdIter;
switch (load_command->cmd)
{
case LC_SEGMENT:
{
struct segment_command const * segment_command = (struct segment_command const *)load_command;
if (segment_command->fileoff == 0 && segment_command->filesize != 0)
{
base_addr = segment_command->vmaddr;
}
if(segment_command->vmaddr < seg1addr)
{
seg1addr = segment_command->vmaddr;
}
// Pickup the address of the first read-write segment for MH_SPLIT_SEGS images.
if((segment_command->initprot & VM_PROT_WRITE) == VM_PROT_WRITE &&
segment_command->vmaddr < segs_read_write_addr)
{
segs_read_write_addr = segment_command->vmaddr;
}
} break;
case LC_SYMTAB: symtab_command = (struct symtab_command const *)load_command; break;
case LC_DYSYMTAB: dysymtab_command = (struct dysymtab_command const *)load_command; break;
case LC_TWOLEVEL_HINTS: twolevel_hints_command = (struct twolevel_hints_command const *)load_command; break;
case LC_SEGMENT_SPLIT_INFO: segment_split_info = (struct linkedit_data_command const *)load_command; break;
case LC_CODE_SIGNATURE: code_signature = (struct linkedit_data_command const *)load_command; break;
case LC_FUNCTION_STARTS: function_starts = (struct linkedit_data_command const *)load_command; break;
case LC_DATA_IN_CODE: data_in_code_entries = (struct linkedit_data_command const *)load_command; break;
default: ; // not interested
}
}
MVNode * symtabNode = nil;
MVNode * dysymtabNode = nil;
MVNode * twoLevelHintsNode = nil;
MVNode * segmentSplitInfoNode = nil;
MVNode * functionStartsNode = nil;
MVNode * dataInCodeEntriesNode = nil;
NSString * lastNodeCaption;
if (symtab_command)
{
symtabNode = [self createDataNode:rootNode
caption:@"Symbol Table"
location:symtab_command->symoff + imageOffset
length:symtab_command->nsyms * sizeof(struct nlist)];
[self createDataNode:rootNode
caption:@"String Table"
location:symtab_command->stroff + imageOffset
length:symtab_command->strsize];
}
if (dysymtab_command)
{
NSRange dysymtabRange = NSMakeRange(0,0);
if (dysymtab_command->tocoff > 0)
{
NSRange range = NSMakeRange(dysymtab_command->tocoff + imageOffset, dysymtab_command->ntoc * sizeof(struct dylib_table_of_contents));
dysymtabRange = NSMaxRange(dysymtabRange) > 0 ? NSUnionRange(dysymtabRange, range) : range;
}
if (dysymtab_command->modtaboff > 0)
{
NSRange range = NSMakeRange(dysymtab_command->modtaboff + imageOffset, dysymtab_command->nmodtab * sizeof(struct dylib_module));
dysymtabRange = NSMaxRange(dysymtabRange) > 0 ? NSUnionRange(dysymtabRange, range) : range;
}
if (dysymtab_command->extrefsymoff > 0)
{
NSRange range = NSMakeRange(dysymtab_command->extrefsymoff + imageOffset, dysymtab_command->nextrefsyms * sizeof(struct dylib_reference));
dysymtabRange = NSMaxRange(dysymtabRange) > 0 ? NSUnionRange(dysymtabRange, range) : range;
}
if (dysymtab_command->indirectsymoff > 0)
{
NSRange range = NSMakeRange(dysymtab_command->indirectsymoff + imageOffset, dysymtab_command->nindirectsyms * sizeof(uint32_t));
dysymtabRange = NSMaxRange(dysymtabRange) > 0 ? NSUnionRange(dysymtabRange, range) : range;
}
if (dysymtab_command->extreloff > 0)
{
NSRange range = NSMakeRange(dysymtab_command->extreloff + imageOffset, dysymtab_command->nextrel * sizeof(struct relocation_info));
dysymtabRange = NSMaxRange(dysymtabRange) > 0 ? NSUnionRange(dysymtabRange, range) : range;
}
if (dysymtab_command->locreloff > 0)
{
NSRange range = NSMakeRange(dysymtab_command->locreloff + imageOffset, dysymtab_command->nlocrel * sizeof(struct relocation_info));
dysymtabRange = NSMaxRange(dysymtabRange) > 0 ? NSUnionRange(dysymtabRange, range) : range;
}
if (dysymtabRange.length > 0)
{
dysymtabNode = [self createDataNode:rootNode
caption:@"Dynamic Symbol Table"
location:dysymtabRange.location
length:dysymtabRange.length];
}
}
if (twolevel_hints_command)
{
twoLevelHintsNode = [self createDataNode:rootNode
caption:@"Two Level Hints Table"
location:twolevel_hints_command->offset + imageOffset
length:twolevel_hints_command->nhints * sizeof(struct twolevel_hint)];
}
if (segment_split_info)
{
segmentSplitInfoNode = [self createDataNode:rootNode
caption:@"Segment Split Info"
location:segment_split_info->dataoff + imageOffset
length:segment_split_info->datasize];
}
if (code_signature)
{
[self createDataNode:rootNode
caption:@"Code Signature"
location:code_signature->dataoff + imageOffset
length:code_signature->datasize];
}
if (function_starts)
{
functionStartsNode = [self createDataNode:rootNode
caption:@"Function Starts"
location:function_starts->dataoff + imageOffset
length:function_starts->datasize];
}
if (data_in_code_entries)
{
dataInCodeEntriesNode = [self createDataNode:rootNode
caption:@"Data in Code Entries"
location:data_in_code_entries->dataoff + imageOffset
length:data_in_code_entries->datasize];
}
//============ Symbol Table ====================
//==============================================
if (symtabNode)
{
@try
{
[self createSymbolsNode:symtabNode
caption:(lastNodeCaption = @"Symbols")
location:symtabNode.dataRange.location
length:symtabNode.dataRange.length];
}
@catch(NSException * exception)
{
[self printException:exception caption:lastNodeCaption];
}
}
//=========== Dynamic Symbol Table =============
//==============================================
if (dysymtabNode)
{
@try
{
//=============== Module Table =================
//==============================================
if (dysymtab_command->modtaboff * dysymtab_command->nmodtab > 0)
{
[self createModulesNode:dysymtabNode
caption:(lastNodeCaption = @"Modules")
location:dysymtab_command->modtaboff + imageOffset
length:dysymtab_command->nmodtab * sizeof(struct dylib_module)];
}
//========== Table of Contents =================
//==============================================
if (dysymtab_command->tocoff * dysymtab_command->ntoc > 0)
{
[self createTOCNode:dysymtabNode
caption:(lastNodeCaption = @"Table of Contents")
location:dysymtab_command->tocoff + imageOffset
length:dysymtab_command->ntoc * sizeof(struct dylib_table_of_contents)];
}
//======= External Reference Table =============
//==============================================
if (dysymtab_command->extrefsymoff * dysymtab_command->nextrefsyms > 0)
{
[self createReferencesNode:dysymtabNode
caption:(lastNodeCaption = @"External References")
location:dysymtab_command->extrefsymoff + imageOffset
length:dysymtab_command->nextrefsyms * sizeof(struct dylib_reference)];
}
//========== Indirect Symbol Table =============
//==============================================
if (dysymtab_command->indirectsymoff * dysymtab_command->nindirectsyms > 0)
{
[self createISymbolsNode:dysymtabNode
caption:(lastNodeCaption = @"Indirect Symbols")
location:dysymtab_command->indirectsymoff + imageOffset
length:dysymtab_command->nindirectsyms * sizeof(uint32_t)];
}
//========== External Reloc Table ==============
//==============================================
if (dysymtab_command->extreloff * dysymtab_command->nextrel > 0)
{
[self createRelocNode:dysymtabNode
caption:(lastNodeCaption = @"External Relocations")
location:dysymtab_command->extreloff + imageOffset
length:dysymtab_command->nextrel * sizeof(struct relocation_info)
baseAddress:(mach_header->flags & MH_SPLIT_SEGS) == MH_SPLIT_SEGS ? segs_read_write_addr : seg1addr];
}
//=========== Local Reloc Table ================
//==============================================
if (dysymtab_command->locreloff * dysymtab_command->nlocrel > 0)
{
[self createRelocNode:dysymtabNode
caption:(lastNodeCaption = @"Local Relocations")
location:dysymtab_command->locreloff + imageOffset
length:dysymtab_command->nlocrel * sizeof(struct relocation_info)
baseAddress:(mach_header->flags & MH_SPLIT_SEGS) == MH_SPLIT_SEGS ? segs_read_write_addr : seg1addr];
}
}
@catch(NSException * exception)
{
[self printException:exception caption:lastNodeCaption];
}
}
if (twoLevelHintsNode && twoLevelHintsNode.dataRange.length > 0)
{
@try
{
[self createTwoLevelHintsNode:twoLevelHintsNode
caption:(lastNodeCaption = @"Hints")
location:twoLevelHintsNode.dataRange.location
length:twoLevelHintsNode.dataRange.length
index:dysymtab_command->iundefsym];
}
@catch(NSException * exception)
{
[self printException:exception caption:lastNodeCaption];
}
}
if (segmentSplitInfoNode && segmentSplitInfoNode.dataRange.length > 0)
{
@try
{
[self createSplitSegmentNode:segmentSplitInfoNode
caption:(lastNodeCaption = @"Shared Region Info")
location:segmentSplitInfoNode.dataRange.location
length:segmentSplitInfoNode.dataRange.length
baseAddress:base_addr];
}
@catch(NSException * exception)
{
[self printException:exception caption:lastNodeCaption];
}
}
if (functionStartsNode && functionStartsNode.dataRange.length > 0)
{
@try
{
[self createFunctionStartsNode:functionStartsNode
caption:(lastNodeCaption = @"Functions")
location:functionStartsNode.dataRange.location
length:functionStartsNode.dataRange.length
baseAddress:base_addr];
}
@catch(NSException * exception)
{
[self printException:exception caption:lastNodeCaption];
}
}
if (dataInCodeEntriesNode && dataInCodeEntriesNode.dataRange.length > 0)
{
@try
{
[self createDataInCodeEntriesNode:dataInCodeEntriesNode
caption:(lastNodeCaption = @"Dices")
location:dataInCodeEntriesNode.dataRange.location
length:dataInCodeEntriesNode.dataRange.length];
}
@catch(NSException * exception)
{
[self printException:exception caption:lastNodeCaption];
}
}
}
//-----------------------------------------------------------------------------
-(void) processLinkEdit64
{
// find related load commands
struct symtab_command const * symtab_command = NULL;
struct dysymtab_command const * dysymtab_command = NULL;
struct twolevel_hints_command const * twolevel_hints_command = NULL;
struct linkedit_data_command const * segment_split_info = NULL;
struct linkedit_data_command const * code_signature = NULL;
struct linkedit_data_command const * function_starts = NULL;
struct linkedit_data_command const * data_in_code_entries = NULL;
MATCH_STRUCT(mach_header_64,imageOffset);
uint64_t base_addr;
uint64_t seg1addr = (uint64_t)-1LL;
uint64_t segs_read_write_addr = (uint64_t)-1LL;
for (CommandVector::const_iterator cmdIter = commands.begin(); cmdIter != commands.end(); ++cmdIter)
{
struct load_command const * load_command = *cmdIter;
switch (load_command->cmd)
{
case LC_SEGMENT_64:
{
struct segment_command_64 const * segment_command_64 = (struct segment_command_64 const *)load_command;
if (segment_command_64->fileoff == 0 && segment_command_64->filesize != 0)
{
base_addr = segment_command_64->vmaddr;
}
if(segment_command_64->vmaddr < seg1addr)
{
seg1addr = segment_command_64->vmaddr;
}
// Pickup the address of the first read-write segment for MH_SPLIT_SEGS images.
if((segment_command_64->initprot & VM_PROT_WRITE) == VM_PROT_WRITE &&
segment_command_64->vmaddr < segs_read_write_addr)
{
segs_read_write_addr = segment_command_64->vmaddr;
}
} break;
case LC_SYMTAB: symtab_command = (struct symtab_command const *)load_command; break;
case LC_DYSYMTAB: dysymtab_command = (struct dysymtab_command const *)load_command; break;
case LC_TWOLEVEL_HINTS: twolevel_hints_command = (struct twolevel_hints_command const *)load_command; break;
case LC_SEGMENT_SPLIT_INFO: segment_split_info = (struct linkedit_data_command const *)load_command; break;
case LC_CODE_SIGNATURE: code_signature = (struct linkedit_data_command const *)load_command; break;
case LC_FUNCTION_STARTS: function_starts = (struct linkedit_data_command const *)load_command; break;
case LC_DATA_IN_CODE: data_in_code_entries = (struct linkedit_data_command const *)load_command; break;
default: ; // not interested
}
}
MVNode * symtabNode = nil;
MVNode * dysymtabNode = nil;
MVNode * twoLevelHintsNode = nil;
MVNode * segmentSplitInfoNode = nil;
MVNode * functionStartsNode = nil;
MVNode * dataInCodeEntriesNode = nil;
NSString * lastNodeCaption;
if (symtab_command)
{
symtabNode = [self createDataNode:rootNode
caption:@"Symbol Table"
location:symtab_command->symoff + imageOffset
length:symtab_command->nsyms * sizeof(struct nlist_64)];
[self createDataNode:rootNode
caption:@"String Table"
location:symtab_command->stroff + imageOffset
length:symtab_command->strsize];
}
if (dysymtab_command)
{
NSRange dysymtabRange = NSMakeRange(0,0);
if (dysymtab_command->tocoff > 0)
{
NSRange range = NSMakeRange(dysymtab_command->tocoff + imageOffset, dysymtab_command->ntoc * sizeof(struct dylib_table_of_contents));
dysymtabRange = NSMaxRange(dysymtabRange) > 0 ? NSUnionRange(dysymtabRange, range) : range;
}
if (dysymtab_command->modtaboff > 0)
{
NSRange range = NSMakeRange(dysymtab_command->modtaboff + imageOffset, dysymtab_command->nmodtab * sizeof(struct dylib_module_64));
dysymtabRange = NSMaxRange(dysymtabRange) > 0 ? NSUnionRange(dysymtabRange, range) : range;
}
if (dysymtab_command->extrefsymoff > 0)
{
NSRange range = NSMakeRange(dysymtab_command->extrefsymoff + imageOffset, dysymtab_command->nextrefsyms * sizeof(struct dylib_reference));
dysymtabRange = NSMaxRange(dysymtabRange) > 0 ? NSUnionRange(dysymtabRange, range) : range;
}
if (dysymtab_command->indirectsymoff > 0)
{
NSRange range = NSMakeRange(dysymtab_command->indirectsymoff + imageOffset, dysymtab_command->nindirectsyms * sizeof(uint32_t));
dysymtabRange = NSMaxRange(dysymtabRange) > 0 ? NSUnionRange(dysymtabRange, range) : range;
}
if (dysymtab_command->extreloff > 0)
{
NSRange range = NSMakeRange(dysymtab_command->extreloff + imageOffset, dysymtab_command->nextrel * sizeof(struct relocation_info));
dysymtabRange = NSMaxRange(dysymtabRange) > 0 ? NSUnionRange(dysymtabRange, range) : range;
}
if (dysymtab_command->locreloff > 0)
{
NSRange range = NSMakeRange(dysymtab_command->locreloff + imageOffset, dysymtab_command->nlocrel * sizeof(struct relocation_info));
dysymtabRange = NSMaxRange(dysymtabRange) > 0 ? NSUnionRange(dysymtabRange, range) : range;
}
if (dysymtabRange.length > 0)
{
dysymtabNode = [self createDataNode:rootNode
caption:@"Dynamic Symbol Table"
location:dysymtabRange.location
length:dysymtabRange.length];
}
}
if (twolevel_hints_command)
{
twoLevelHintsNode = [self createDataNode:rootNode
caption:@"Two Level Hints Table"
location:twolevel_hints_command->offset + imageOffset
length:twolevel_hints_command->nhints * sizeof(struct twolevel_hint)];
}
if (segment_split_info)
{
segmentSplitInfoNode = [self createDataNode:rootNode
caption:@"Segment Split Info"
location:segment_split_info->dataoff + imageOffset
length:segment_split_info->datasize];
}
if (code_signature)
{
[self createDataNode:rootNode
caption:@"Code Signature"
location:code_signature->dataoff + imageOffset
length:code_signature->datasize];
}
if (function_starts)
{
functionStartsNode = [self createDataNode:rootNode
caption:@"Function Starts"
location:function_starts->dataoff + imageOffset
length:function_starts->datasize];
}
if (data_in_code_entries)
{
dataInCodeEntriesNode = [self createDataNode:rootNode
caption:@"Data in Code Entries"
location:data_in_code_entries->dataoff + imageOffset
length:data_in_code_entries->datasize];
}
//============ Symbol Table ====================
//==============================================
if (symtabNode)
{
@try
{
[self createSymbols64Node:symtabNode
caption:(lastNodeCaption = @"Symbols")
location:symtabNode.dataRange.location
length:symtabNode.dataRange.length];
}
@catch(NSException * exception)
{
[self printException:exception caption:lastNodeCaption];
}
}
//=========== Dynamic Symbol Table =============
//==============================================
if (dysymtabNode)
{
@try
{
//=============== Module Table =================
//==============================================
if (dysymtab_command->modtaboff * dysymtab_command->nmodtab > 0)
{
[self createModules64Node:dysymtabNode
caption:(lastNodeCaption = @"Modules64")
location:dysymtab_command->modtaboff + imageOffset
length:dysymtab_command->nmodtab * sizeof(struct dylib_module_64)];
}
//========== Table of Contents =================
//==============================================
if (dysymtab_command->tocoff * dysymtab_command->ntoc > 0)
{
[self createTOC64Node:dysymtabNode
caption:(lastNodeCaption = @"Table of Contents")
location:dysymtab_command->tocoff + imageOffset
length:dysymtab_command->ntoc * sizeof(struct dylib_table_of_contents)];
}
//======= External Reference Table =============
//==============================================
if (dysymtab_command->extrefsymoff * dysymtab_command->nextrefsyms > 0)
{
[self createReferencesNode:dysymtabNode
caption:(lastNodeCaption = @"External References")
location:dysymtab_command->extrefsymoff + imageOffset
length:dysymtab_command->nextrefsyms * sizeof(struct dylib_reference)];
}
//========== Indirect Symbol Table =============
//==============================================
if (dysymtab_command->indirectsymoff * dysymtab_command->nindirectsyms > 0)
{
[self createISymbols64Node:dysymtabNode
caption:(lastNodeCaption = @"Indirect Symbols")
location:dysymtab_command->indirectsymoff + imageOffset
length:dysymtab_command->nindirectsyms * sizeof(uint32_t)];
}
//========== External Reloc Table ==============
//==============================================
if (dysymtab_command->extreloff * dysymtab_command->nextrel > 0)
{
[self createReloc64Node:dysymtabNode
caption:(lastNodeCaption = @"External Relocations")
location:dysymtab_command->extreloff + imageOffset
length:dysymtab_command->nextrel * sizeof(struct relocation_info)
baseAddress:(mach_header_64->flags & MH_SPLIT_SEGS) == MH_SPLIT_SEGS ? segs_read_write_addr : seg1addr];
}
//=========== Local Reloc Table ================
//==============================================
if (dysymtab_command->locreloff * dysymtab_command->nlocrel > 0)
{
[self createReloc64Node:dysymtabNode
caption:(lastNodeCaption = @"Local Reloc Table")
location:dysymtab_command->locreloff + imageOffset
length:dysymtab_command->nlocrel * sizeof(struct relocation_info)
baseAddress:(mach_header_64->flags & MH_SPLIT_SEGS) == MH_SPLIT_SEGS ? segs_read_write_addr : seg1addr];
}
}
@catch(NSException * exception)
{
[self printException:exception caption:lastNodeCaption];
}
}
if (twoLevelHintsNode && twoLevelHintsNode.dataRange.length > 0)
{
@try
{
[self createTwoLevelHintsNode:twoLevelHintsNode
caption:(lastNodeCaption = @"Hints")
location:twoLevelHintsNode.dataRange.location
length:twoLevelHintsNode.dataRange.length
index:dysymtab_command->iundefsym];
}
@catch(NSException * exception)
{
[self printException:exception caption:lastNodeCaption];
}
}
if (segmentSplitInfoNode && segmentSplitInfoNode.dataRange.length > 0)
{
@try
{
[self createSplitSegmentNode:segmentSplitInfoNode
caption:(lastNodeCaption = @"Shared Region Info")
location:segmentSplitInfoNode.dataRange.location
length:segmentSplitInfoNode.dataRange.length
baseAddress:base_addr];
}
@catch(NSException * exception)
{
[self printException:exception caption:lastNodeCaption];
}
}
if (functionStartsNode && functionStartsNode.dataRange.length > 0)
{
@try
{
[self createFunctionStartsNode:functionStartsNode
caption:(lastNodeCaption = @"Functions")
location:functionStartsNode.dataRange.location
length:functionStartsNode.dataRange.length
baseAddress:base_addr];
}
@catch(NSException * exception)
{
[self printException:exception caption:lastNodeCaption];
}
}
if (dataInCodeEntriesNode && dataInCodeEntriesNode.dataRange.length > 0)
{
@try
{
[self createDataInCodeEntriesNode:dataInCodeEntriesNode
caption:(lastNodeCaption = @"Dices")
location:dataInCodeEntriesNode.dataRange.location
length:dataInCodeEntriesNode.dataRange.length];
}
@catch(NSException * exception)
{
[self printException:exception caption:lastNodeCaption];
}
}
}
//-----------------------------------------------------------------------------
-(void)processDyldInfo
{
uint64_t base_addr = 0;
// find related load commands
struct dyld_info_command const * dyld_info_command = NULL;
for (CommandVector::const_iterator cmdIter = commands.begin(); cmdIter != commands.end(); ++cmdIter)
{
struct load_command const * load_command = *cmdIter;
switch (load_command->cmd)
{
case LC_SEGMENT:
{
struct segment_command const * segment_command = (struct segment_command const *)load_command;
if (segment_command->fileoff == 0 && segment_command->filesize != 0)
{
base_addr = segment_command->vmaddr;
}
} break;
case LC_SEGMENT_64:
{
struct segment_command_64 const * segment_command_64 = (struct segment_command_64 const *)load_command;
if (segment_command_64->fileoff == 0 && segment_command_64->filesize != 0)
{
base_addr = segment_command_64->vmaddr;
}
} break;
case LC_DYLD_INFO:
case LC_DYLD_INFO_ONLY: dyld_info_command = (struct dyld_info_command const *)load_command; break;
default: ; // not interested
}
}
if (dyld_info_command == NULL)
{
return;
}
NSRange dyldInfoRange = NSMakeRange(0,0);
if (dyld_info_command->rebase_off > 0)
{
NSRange range = NSMakeRange(dyld_info_command->rebase_off + imageOffset, dyld_info_command->rebase_size);
dyldInfoRange = NSMaxRange(dyldInfoRange) > 0 ? NSUnionRange(dyldInfoRange, range) : range;
}
if (dyld_info_command->bind_off > 0)
{
NSRange range = NSMakeRange(dyld_info_command->bind_off + imageOffset, dyld_info_command->bind_size);
dyldInfoRange = NSMaxRange(dyldInfoRange) > 0 ? NSUnionRange(dyldInfoRange, range) : range;
}
if (dyld_info_command->weak_bind_off > 0)
{
NSRange range = NSMakeRange(dyld_info_command->weak_bind_off + imageOffset, dyld_info_command->weak_bind_size);
dyldInfoRange = NSMaxRange(dyldInfoRange) > 0 ? NSUnionRange(dyldInfoRange, range) : range;
}
if (dyld_info_command->lazy_bind_off > 0)
{
NSRange range = NSMakeRange(dyld_info_command->lazy_bind_off + imageOffset, dyld_info_command->lazy_bind_size);
dyldInfoRange = NSMaxRange(dyldInfoRange) > 0 ? NSUnionRange(dyldInfoRange, range) : range;
}
if (dyld_info_command->export_off > 0)
{
NSRange range = NSMakeRange(dyld_info_command->export_off + imageOffset, dyld_info_command->export_size);
dyldInfoRange = NSMaxRange(dyldInfoRange) > 0 ? NSUnionRange(dyldInfoRange, range) : range;
}
MVNode * dyldInfoNode = [self createDataNode:rootNode
caption:@"Dynamic Loader Info"
location:dyldInfoRange.location
length:dyldInfoRange.length];
DyldHelper * dyldHelper = [DyldHelper dyldHelperWithSymbols:symbolNames is64Bit:[self is64bit]];
NSString * lastNodeCaption;
@try
{
if (dyld_info_command->rebase_off * dyld_info_command->rebase_size > 0)
{
[self createRebaseNode:dyldInfoNode
caption:(lastNodeCaption = @"Rebase Info")
location:dyld_info_command->rebase_off + imageOffset
length:dyld_info_command->rebase_size
baseAddress:base_addr];
}