forked from gdbinit/MachOView
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DyldInfo.mm
928 lines (751 loc) · 36 KB
/
DyldInfo.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
/*
* DyldInfo.mm
* MachOView
*
* Created by psaghelyi on 21/09/2010.
*
*/
#include <string>
#include <vector>
#include <set>
#include <map>
#import "Common.h"
#import "DyldInfo.h"
#import "ReadWrite.h"
#import "DataController.h"
using namespace std;
//============================================================================
@implementation DyldHelper
//-----------------------------------------------------------------------------
-(id) initWithSymbols:(NSDictionary *)symbolNames is64Bit:(bool)is64Bit
{
if (self = [super init])
{
externalMap = [[NSMutableDictionary alloc] initWithCapacity:[symbolNames count]];
NSEnumerator * enumerator = [symbolNames keyEnumerator];
id key;
while ((key = [enumerator nextObject]) != nil)
{
NSNumber * symbolIndex = (NSNumber *)key;
// negative index indicates that it is external
if ((is64Bit == NO && (int32_t)[symbolIndex unsignedLongValue] < 0) ||
(int64_t)[symbolIndex unsignedLongLongValue] < 0)
{
[externalMap setObject:key forKey:[symbolNames objectForKey:key]];
}
}
}
return self;
}
//-----------------------------------------------------------------------------
+(DyldHelper *) dyldHelperWithSymbols:(NSDictionary *)symbolNames is64Bit:(bool)is64Bit
{
return [[DyldHelper alloc] initWithSymbols:symbolNames is64Bit:is64Bit];
}
//-----------------------------------------------------------------------------
-(NSNumber *) indexForSymbol:(NSString *)symbolName
{
return [externalMap objectForKey:symbolName];
}
@end
//============================================================================
@implementation MachOLayout (DyldInfo)
//-----------------------------------------------------------------------------
- (void)rebaseAddress:(uint64_t)address
type:(uint32_t)type
node:(MVNode *)node
location:(uint32_t)location
{
NSString * descStr = [NSString stringWithFormat:@"%@ 0x%qX %@",
[self is64bit] == NO ? [self findSectionContainsRVA:address] : [self findSectionContainsRVA64:address],
address,
type == REBASE_TYPE_POINTER ? @"Pointer" :
type == REBASE_TYPE_TEXT_ABSOLUTE32 ? @"Abs32 " :
type == REBASE_TYPE_TEXT_PCREL32 ? @"PCrel32" : @"???"];
[node.details appendRow:[NSString stringWithFormat:@"%.8X", location]
:@""
:descStr
:@""];
}
//-----------------------------------------------------------------------------
- (MVNode *)createRebaseNode:(MVNode *)parent
caption:(NSString *)caption
location:(uint32_t)location
length:(uint32_t)length
baseAddress:(uint64_t)baseAddress
{
MVNode * dataNode = [self createDataNode:parent
caption:caption
location:location
length:length];
MVNodeSaver nodeSaver;
MVNode * node = [dataNode insertChildWithDetails:@"Opcodes" location:location length:length saver:nodeSaver];
MVNodeSaver actionNodeSaver;
MVNode * actionNode = [dataNode insertChildWithDetails:@"Actions" location:location length:length saver:actionNodeSaver];
NSRange range = NSMakeRange(location,0);
NSString * lastReadHex;
BOOL isDone = NO;
uint64_t ptrSize = ([self is64bit] == NO ? sizeof(uint32_t) : sizeof(uint64_t));
uint64_t address = baseAddress;
uint32_t type = 0;
uint32_t doRebaseLocation = location;
while (NSMaxRange(range) < location + length && isDone == NO)
{
uint8_t byte = [dataController read_int8:range lastReadHex:&lastReadHex];
uint8_t opcode = byte & REBASE_OPCODE_MASK;
uint8_t immediate = byte & REBASE_IMMEDIATE_MASK;
switch (opcode)
{
case REBASE_OPCODE_DONE:
isDone = YES;
[node.details appendRow:[NSString stringWithFormat:@"%.8lX", range.location]
:lastReadHex
:@"REBASE_OPCODE_DONE"
:@""];
break;
case REBASE_OPCODE_SET_TYPE_IMM:
{
type = immediate;
NSString *typeString = [NSString alloc];
switch (type)
{
case REBASE_TYPE_POINTER:
typeString = [typeString initWithString:@"REBASE_TYPE_POINTER"];
break;
case REBASE_TYPE_TEXT_ABSOLUTE32:
typeString = [typeString initWithString:@"REBASE_TYPE_TEXT_ABSOLUTE32"];
break;
case REBASE_TYPE_TEXT_PCREL32:
typeString = [typeString initWithString:@"REBASE_TYPE_TEXT_PCREL32"];
break;
default:
typeString = [typeString initWithString:@"Unknown"];
}
[node.details appendRow:[NSString stringWithFormat:@"%.8lX", range.location]
:lastReadHex
:@"REBASE_OPCODE_SET_TYPE_IMM"
:[NSString stringWithFormat:@"type (%i, %@)", type, typeString]];
break;
}
case REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB:
{
uint32_t segmentIndex = immediate;
[node.details appendRow:[NSString stringWithFormat:@"%.8lX", range.location]
:lastReadHex
:@"REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB"
:[NSString stringWithFormat:@"segment (%u)", segmentIndex]];
uint64_t offset = [dataController read_uleb128:range lastReadHex:&lastReadHex];
[node.details appendRow:[NSString stringWithFormat:@"%.8lX", range.location]
:lastReadHex
:@"uleb128"
:[NSString stringWithFormat:@"offset (%qi)",offset]];
if (([self is64bit] == NO && segmentIndex >= segments.size()) ||
([self is64bit] == YES && segmentIndex >= segments_64.size()))
{
[NSException raise:@"Segment"
format:@"index is out of range %u", segmentIndex];
}
address = ([self is64bit] == NO ? segments.at(segmentIndex)->vmaddr
: segments_64.at(segmentIndex)->vmaddr) + offset;
} break;
case REBASE_OPCODE_ADD_ADDR_ULEB:
{
[node.details appendRow:[NSString stringWithFormat:@"%.8lX", range.location]
:lastReadHex
:@"REBASE_OPCODE_ADD_ADDR_ULEB"
:@""];
uint64_t offset = [dataController read_uleb128:range lastReadHex:&lastReadHex];
[node.details appendRow:[NSString stringWithFormat:@"%.8lX", range.location]
:lastReadHex
:@"uleb128"
:[NSString stringWithFormat:@"offset (%qi)",offset]];
address += offset;
} break;
case REBASE_OPCODE_ADD_ADDR_IMM_SCALED:
{
uint32_t scale = immediate;
[node.details appendRow:[NSString stringWithFormat:@"%.8lX", range.location]
:lastReadHex
:@"REBASE_OPCODE_ADD_ADDR_IMM_SCALED"
:[NSString stringWithFormat:@"scale (%u)",scale]];
address += scale * ptrSize;
} break;
case REBASE_OPCODE_DO_REBASE_IMM_TIMES:
{
uint32_t count = immediate;
[node.details appendRow:[NSString stringWithFormat:@"%.8lX", range.location]
:lastReadHex
:@"REBASE_OPCODE_DO_REBASE_IMM_TIMES"
:[NSString stringWithFormat:@"count (%u)",count]];
[node.details setAttributes:MVUnderlineAttributeName,@"YES",nil];
for (uint32_t index = 0; index < count; index++)
{
[self rebaseAddress:address type:type node:actionNode location:doRebaseLocation];
address += ptrSize;
}
doRebaseLocation = NSMaxRange(range);
} break;
case REBASE_OPCODE_DO_REBASE_ULEB_TIMES:
{
[node.details appendRow:[NSString stringWithFormat:@"%.8lX", range.location]
:lastReadHex
:@"REBASE_OPCODE_DO_REBASE_ULEB_TIMES"
:@""];
uint32_t startNextRebase = NSMaxRange(range);
uint64_t count = [dataController read_uleb128:range lastReadHex:&lastReadHex];
[node.details appendRow:[NSString stringWithFormat:@"%.8lX", range.location]
:lastReadHex
:@"uleb128"
:[NSString stringWithFormat:@"count (%qu)",count]];
[node.details setAttributes:MVUnderlineAttributeName,@"YES",nil];
for (uint64_t index = 0; index < count; index++)
{
[self rebaseAddress:address type:type node:actionNode location:doRebaseLocation];
address += ptrSize;
}
doRebaseLocation = startNextRebase;
} break;
case REBASE_OPCODE_DO_REBASE_ADD_ADDR_ULEB:
{
[node.details appendRow:[NSString stringWithFormat:@"%.8lX", range.location]
:lastReadHex
:@"REBASE_OPCODE_DO_REBASE_ADD_ADDR_ULEB"
:@""];
uint32_t startNextRebase = NSMaxRange(range);
uint64_t offset = [dataController read_uleb128:range lastReadHex:&lastReadHex];
[node.details appendRow:[NSString stringWithFormat:@"%.8lX", range.location]
:lastReadHex
:@"uleb128"
:[NSString stringWithFormat:@"offset (%qi)",offset]];
[node.details setAttributes:MVUnderlineAttributeName,@"YES",nil];
[self rebaseAddress:address type:type node:actionNode location:doRebaseLocation];
address += ptrSize + offset;
doRebaseLocation = startNextRebase;
} break;
case REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_ULEB:
{
[node.details appendRow:[NSString stringWithFormat:@"%.8lX", range.location]
:lastReadHex
:@"REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_ULEB"
:@""];
uint32_t startNextRebase = NSMaxRange(range);
uint64_t count = [dataController read_uleb128:range lastReadHex:&lastReadHex];
[node.details appendRow:[NSString stringWithFormat:@"%.8lX", range.location]
:lastReadHex
:@"uleb128"
:[NSString stringWithFormat:@"count (%qu)",count]];
uint64_t skip = [dataController read_uleb128:range lastReadHex:&lastReadHex];
[node.details appendRow:[NSString stringWithFormat:@"%.8lX", range.location]
:lastReadHex
:@"uleb128"
:[NSString stringWithFormat:@"skip (%qu)",skip]];
[node.details setAttributes:MVUnderlineAttributeName,@"YES",nil];
for (uint64_t index = 0; index < count; index++)
{
[self rebaseAddress:address type:type node:actionNode location:doRebaseLocation];
address += ptrSize + skip;
}
doRebaseLocation = startNextRebase;
} break;
default:
[NSException raise:@"Rebase info" format:@"Unknown opcode (%u %u)",
((uint32_t)-1 & opcode), ((uint32_t)-1 & immediate)];
}
}
return node;
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
- (void)bindAddress:(uint64_t)address
type:(uint32_t)type
symbolName:(NSString *)symbolName
flags:(uint32_t)flags
addend:(int64_t)addend
libraryOrdinal:(int32_t)libOrdinal
node:(MVNode *)node
nodeType:(BindNodeType)nodeType
location:(uint32_t)location
dyldHelper:(DyldHelper *)helper
ptrSize:(uint32_t)ptrSize
{
NSString * descStr = [NSString stringWithFormat:@"%@ 0x%qX",
[self is64bit] == NO ? [self findSectionContainsRVA:address] : [self findSectionContainsRVA64:address],
address];
if (nodeType != NodeTypeLazyBind)
{
descStr = [descStr stringByAppendingFormat:@" %@ addend:%qi",
type == BIND_TYPE_POINTER ? @"Pointer" :
type == BIND_TYPE_TEXT_ABSOLUTE32 ? @"Abs32 " :
type == BIND_TYPE_TEXT_PCREL32 ? @"PCrel32" : @"type:???",
addend];
}
if ((flags & BIND_SYMBOL_FLAGS_WEAK_IMPORT) != 0)
{
descStr = [descStr stringByAppendingString:@"[weak-ref]"];
}
if ((flags & BIND_SYMBOL_FLAGS_NON_WEAK_DEFINITION) != 0)
{
descStr = [descStr stringByAppendingString:@"[strong-def]"];
}
if (nodeType != NodeTypeWeakBind)
{
struct dylib const * dylib = [self getDylibByIndex:libOrdinal];
descStr = [descStr stringByAppendingFormat:@" (%@)",
libOrdinal == BIND_SPECIAL_DYLIB_SELF ? @"BIND_SPECIAL_DYLIB_SELF" :
libOrdinal == BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE ? @"BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE" :
libOrdinal == BIND_SPECIAL_DYLIB_FLAT_LOOKUP ? @"BIND_SPECIAL_DYLIB_FLAT_LOOKUP" :
(uint32_t)libOrdinal >= dylibs.size() ? @"???" :
[NSSTRING((uint8_t *)dylib + dylib->name.offset - sizeof(struct load_command)) lastPathComponent]];
}
[node.details appendRow:[NSString stringWithFormat:@"%.8X", location]
:@""
:descStr
:symbolName];
[node.details setAttributes:MVMetaDataAttributeName,symbolName,nil];
// preserve binding info for reloc pathcing
if ([self isDylibStub] == NO && nodeType == NodeTypeBind) // weak and lazy does not count
{
NSParameterAssert(type == BIND_TYPE_POINTER); // only this one is supported so far
NSNumber * symbolIndex = [helper indexForSymbol:symbolName];
if (symbolIndex != nil)
{
uint32_t relocLocation;
uint64_t relocValue;
if ([self is64bit] == NO)
{
relocLocation = [self RVAToFileOffset:(uint32_t)address];
relocValue = [symbolIndex longValue];
}
else
{
relocLocation = [self RVA64ToFileOffset:address];
relocValue = [symbolIndex longLongValue];
}
// update real data
relocValue += addend;
[dataController.realData replaceBytesInRange:NSMakeRange(relocLocation, ptrSize) withBytes:&relocValue];
/*
NSLog(@"%0xqX --> %0xqX",
([self is64bit] == NO ? [self fileOffsetToRVA:relocLocation] : [self fileOffsetToRVA64:relocLocation]),
relocValue);
*/
}
}
}
//-----------------------------------------------------------------------------
- (MVNode *)createBindingNode:(MVNode *)parent
caption:(NSString *)caption
location:(uint32_t)location
length:(uint32_t)length
baseAddress:(uint64_t)baseAddress
nodeType:(BindNodeType)nodeType
dyldHelper:(DyldHelper *)helper
{
MVNode * dataNode = [self createDataNode:parent
caption:caption
location:location
length:length];
MVNodeSaver nodeSaver;
MVNode * node = [dataNode insertChildWithDetails:@"Opcodes" location:location length:length saver:nodeSaver];
MVNodeSaver actionNodeSaver;
MVNode * actionNode = [dataNode insertChildWithDetails:@"Actions" location:location length:length saver:actionNodeSaver];
NSRange range = NSMakeRange(location,0);
NSString * lastReadHex;
//----------------------------
BOOL isDone = NO;
int32_t libOrdinal = 0;
uint32_t type = 0;
int64_t addend = 0;
NSString * symbolName = nil;
uint32_t symbolFlags = 0;
uint32_t doBindLocation = location;
uint64_t ptrSize = ([self is64bit] == NO ? sizeof(uint32_t) : sizeof(uint64_t));
uint64_t address = baseAddress;
while (NSMaxRange(range) < location + length && isDone == NO)
{
uint8_t byte = [dataController read_int8:range lastReadHex:&lastReadHex];
uint8_t opcode = byte & BIND_OPCODE_MASK;
uint8_t immediate = byte & BIND_IMMEDIATE_MASK;
switch (opcode)
{
case BIND_OPCODE_DONE:
[node.details appendRow:[NSString stringWithFormat:@"%.8lX", range.location]
:lastReadHex
:@"BIND_OPCODE_DONE"
:@""];
// The lazy bindings have one of these at the end of each bind.
if (nodeType != NodeTypeLazyBind)
{
isDone = YES;
}
doBindLocation = NSMaxRange(range);
break;
case BIND_OPCODE_SET_DYLIB_ORDINAL_IMM:
libOrdinal = immediate;
[node.details appendRow:[NSString stringWithFormat:@"%.8lX", range.location]
:lastReadHex
:@"BIND_OPCODE_SET_DYLIB_ORDINAL_IMM"
:[NSString stringWithFormat:@"dylib (%d)",libOrdinal]];
break;
case BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB:
[node.details appendRow:[NSString stringWithFormat:@"%.8lX", range.location]
:lastReadHex
:@"BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB"
:@""];
libOrdinal = [dataController read_uleb128:range lastReadHex:&lastReadHex];
[node.details appendRow:[NSString stringWithFormat:@"%.8lX", range.location]
:lastReadHex
:@"uleb128"
:[NSString stringWithFormat:@"dylib (%d)",libOrdinal]];
break;
case BIND_OPCODE_SET_DYLIB_SPECIAL_IMM:
{
// Special means negative
if (immediate == 0)
{
libOrdinal = 0;
}
else
{
int8_t signExtended = immediate | BIND_OPCODE_MASK; // This sign extends the value
libOrdinal = signExtended;
}
[node.details appendRow:[NSString stringWithFormat:@"%.8lX", range.location]
:lastReadHex
:@"BIND_OPCODE_SET_DYLIB_SPECIAL_IMM"
:[NSString stringWithFormat:@"dylib (%d)",libOrdinal]];
} break;
case BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM:
symbolFlags = immediate;
[node.details appendRow:[NSString stringWithFormat:@"%.8lX", range.location]
:lastReadHex
:@"BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM"
:[NSString stringWithFormat:@"flags (%u)",((uint32_t)-1 & symbolFlags)]];
symbolName = [dataController read_string:range lastReadHex:&lastReadHex];
[node.details appendRow:[NSString stringWithFormat:@"%.8lX", range.location]
:lastReadHex
:@"string"
:[NSString stringWithFormat:@"name (%@)",symbolName]];
break;
case BIND_OPCODE_SET_TYPE_IMM:
type = immediate;
[node.details appendRow:[NSString stringWithFormat:@"%.8lX", range.location]
:lastReadHex
:@"BIND_OPCODE_SET_TYPE_IMM"
:[NSString stringWithFormat:@"type (%@)",
type == BIND_TYPE_POINTER ? @"BIND_TYPE_POINTER" :
type == BIND_TYPE_TEXT_ABSOLUTE32 ? @"BIND_TYPE_TEXT_ABSOLUTE32" :
type == BIND_TYPE_TEXT_PCREL32 ? @"BIND_TYPE_TEXT_PCREL32" : @"???"]];
break;
case BIND_OPCODE_SET_ADDEND_SLEB:
[node.details appendRow:[NSString stringWithFormat:@"%.8lX", range.location]
:lastReadHex
:@"BIND_OPCODE_SET_ADDEND_SLEB"
:@""];
addend = [dataController read_sleb128:range lastReadHex:&lastReadHex];
[node.details appendRow:[NSString stringWithFormat:@"%.8lX", range.location]
:lastReadHex
:@"sleb128"
:[NSString stringWithFormat:@"addend (%qi)",addend]];
break;
case BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB:
{
uint32_t segmentIndex = immediate;
[node.details appendRow:[NSString stringWithFormat:@"%.8lX", range.location]
:lastReadHex
:@"BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB"
:[NSString stringWithFormat:@"segment (%u)",segmentIndex]];
uint64_t val = [dataController read_uleb128:range lastReadHex:&lastReadHex];
[node.details appendRow:[NSString stringWithFormat:@"%.8lX", range.location]
:lastReadHex
:@"uleb128"
:[NSString stringWithFormat:@"offset (%qi)",val]];
if (([self is64bit] == NO && segmentIndex >= segments.size()) ||
([self is64bit] == YES && segmentIndex >= segments_64.size()))
{
[NSException raise:@"Segment"
format:@"index is out of range %u", segmentIndex];
}
address = ([self is64bit] == NO ? segments.at(segmentIndex)->vmaddr
: segments_64.at(segmentIndex)->vmaddr) + val;
} break;
case BIND_OPCODE_ADD_ADDR_ULEB:
{
[node.details appendRow:[NSString stringWithFormat:@"%.8lX", range.location]
:lastReadHex
:@"BIND_OPCODE_ADD_ADDR_ULEB"
:@""];
uint64_t val = [dataController read_uleb128:range lastReadHex:&lastReadHex];
[node.details appendRow:[NSString stringWithFormat:@"%.8lX", range.location]
:lastReadHex
:@"uleb128"
:[NSString stringWithFormat:@"offset (%qi)",val]];
address += val;
} break;
case BIND_OPCODE_DO_BIND:
{
[node.details appendRow:[NSString stringWithFormat:@"%.8lX", range.location]
:lastReadHex
:@"BIND_OPCODE_DO_BIND"
:@""];
[node.details setAttributes:MVUnderlineAttributeName,@"YES",nil];
[self bindAddress:address
type:type
symbolName:symbolName
flags:symbolFlags
addend:addend
libraryOrdinal:libOrdinal
node:actionNode
nodeType:nodeType
location:doBindLocation
dyldHelper:helper
ptrSize:ptrSize];
doBindLocation = NSMaxRange(range);
address += ptrSize;
} break;
case BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB:
{
[node.details appendRow:[NSString stringWithFormat:@"%.8lX", range.location]
:lastReadHex
:@"BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB"
:@""];
uint32_t startNextBind = NSMaxRange(range);
uint64_t val = [dataController read_uleb128:range lastReadHex:&lastReadHex];
[node.details appendRow:[NSString stringWithFormat:@"%.8lX", range.location]
:lastReadHex
:@"uleb128"
:[NSString stringWithFormat:@"offset (%qi)",val]];
[node.details setAttributes:MVUnderlineAttributeName,@"YES",nil];
[self bindAddress:address
type:type
symbolName:symbolName
flags:symbolFlags
addend:addend
libraryOrdinal:libOrdinal
node:actionNode
nodeType:nodeType
location:doBindLocation
dyldHelper:helper
ptrSize:ptrSize];
doBindLocation = startNextBind;
address += ptrSize + val;
} break;
case BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED:
{
uint32_t scale = immediate;
[node.details appendRow:[NSString stringWithFormat:@"%.8lX", range.location]
:lastReadHex
:@"BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED"
:[NSString stringWithFormat:@"scale (%u)",scale]];
[node.details setAttributes:MVUnderlineAttributeName,@"YES",nil];
[self bindAddress:address
type:type
symbolName:symbolName
flags:symbolFlags
addend:addend
libraryOrdinal:libOrdinal
node:actionNode
nodeType:nodeType
location:doBindLocation
dyldHelper:helper
ptrSize:ptrSize];
doBindLocation = NSMaxRange(range);
address += ptrSize + scale * ptrSize;
} break;
case BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB:
{
[node.details appendRow:[NSString stringWithFormat:@"%.8lX", range.location]
:lastReadHex
:@"BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB"
:@""];
uint32_t startNextBind = NSMaxRange(range);
uint64_t count = [dataController read_uleb128:range lastReadHex:&lastReadHex];
[node.details appendRow:[NSString stringWithFormat:@"%.8lX", range.location]
:lastReadHex
:@"uleb128"
:[NSString stringWithFormat:@"count (%qu)",count]];
uint64_t skip = [dataController read_uleb128:range lastReadHex:&lastReadHex];
[node.details appendRow:[NSString stringWithFormat:@"%.8lX", range.location]
:lastReadHex
:@"uleb128"
:[NSString stringWithFormat:@"skip (%qu)",skip]];
[node.details setAttributes:MVUnderlineAttributeName,@"YES",nil];
for (uint64_t index = 0; index < count; index++)
{
[self bindAddress:address
type:type
symbolName:symbolName
flags:symbolFlags
addend:addend
libraryOrdinal:libOrdinal
node:actionNode
nodeType:nodeType
location:doBindLocation
dyldHelper:helper
ptrSize:ptrSize];
doBindLocation = startNextBind;
address += ptrSize + skip;
}
} break;
default:
[NSException raise:@"Bind info" format:@"Unknown opcode (%u %u)",
((uint32_t)-1 & opcode), ((uint32_t)-1 & immediate)];
}
}
return node;
}
//-----------------------------------------------------------------------------
- (void)exportSymbol:(uint64_t)address
symbolName:(NSString *)symbolName
flags:(uint64_t)flags
node:(MVNode *)node
location:(uint32_t)location
{
//uint64_t address = [self is64bit] == NO ? [self fileOffsetToRVA:offset] : [self fileOffsetToRVA64:offset];
NSString * descStr = [NSString stringWithFormat:@"%@ 0x%qX",
[self is64bit] == NO ? [self findSectionContainsRVA:address] : [self findSectionContainsRVA64:address],
address];
if ((flags & EXPORT_SYMBOL_FLAGS_KIND_MASK) == EXPORT_SYMBOL_FLAGS_KIND_THREAD_LOCAL)
{
descStr = [descStr stringByAppendingString:@" [thread-local]"];
}
if (flags & EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION)
{
descStr = [descStr stringByAppendingString:@" [weak-def]"];
}
if (flags & EXPORT_SYMBOL_FLAGS_REEXPORT)
{
descStr = [descStr stringByAppendingString:@" [reexport]"];
}
if (flags & EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER)
{
descStr = [descStr stringByAppendingString:@" [stub & resolver]"];
}
[node.details insertRowWithOffset:location
:[NSString stringWithFormat:@"%.8X", location]
:@""
:descStr
:symbolName];
[node.details setAttributes:MVMetaDataAttributeName,symbolName,nil];
}
//-----------------------------------------------------------------------------
- (void)printSymbols:(NSString *)prefix
location:(uint32_t)location
skipBytes:(uint32_t)skip
node:(MVNode *)node
actionNode:(MVNode *)actionNode
baseAddress:(uint64_t)baseAddress
exportLocation:(uint32_t &)exportLocation
{
NSRange range = NSMakeRange(location + skip,0);
NSString * lastReadHex;
uint8_t terminalSize = [dataController read_uint8:range lastReadHex:&lastReadHex];
[node.details insertRowWithOffset:range.location
:[NSString stringWithFormat:@"%.8lX", range.location]
:lastReadHex
:@"Terminal Size"
:[NSString stringWithFormat:@"%u",((uint32_t)-1 & terminalSize)]];
if (terminalSize != 0)
{
uint32_t terminalLocation = NSMaxRange(range);
uint64_t flags = [dataController read_uleb128:range lastReadHex:&lastReadHex];
[node.details insertRowWithOffset:range.location
:[NSString stringWithFormat:@"%.8lX", range.location]
:lastReadHex
:@"Flags"
:@""];
if ((flags & EXPORT_SYMBOL_FLAGS_KIND_MASK) == EXPORT_SYMBOL_FLAGS_KIND_REGULAR) [node.details insertRowWithOffset:range.location:@"":@"":@"00":@"EXPORT_SYMBOL_FLAGS_KIND_REGULAR"];
if ((flags & EXPORT_SYMBOL_FLAGS_KIND_MASK) == EXPORT_SYMBOL_FLAGS_KIND_THREAD_LOCAL) [node.details insertRowWithOffset:range.location:@"":@"":@"01":@"EXPORT_SYMBOL_FLAGS_KIND_THREAD_LOCAL"];
if (flags & EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION) [node.details insertRowWithOffset:range.location:@"":@"":@"04":@"EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION"];
if (flags & EXPORT_SYMBOL_FLAGS_REEXPORT) [node.details insertRowWithOffset:range.location:@"":@"":@"08":@"EXPORT_SYMBOL_FLAGS_REEXPORT"];
if (flags & EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER) [node.details insertRowWithOffset:range.location:@"":@"":@"10":@"EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER"];
uint64_t offset = [dataController read_uleb128:range lastReadHex:&lastReadHex];
[node.details insertRowWithOffset:range.location
:[NSString stringWithFormat:@"%.8lX", range.location]
:lastReadHex
:@"Symbol Offset"
:[NSString stringWithFormat:@"0x%qX",offset]];
//=================================================================
[self exportSymbol:baseAddress + offset
symbolName:prefix
flags:flags
node:actionNode
location:exportLocation];
//=================================================================
range = NSMakeRange(terminalLocation, terminalSize);
}
uint8_t childCount = [dataController read_uint8:range lastReadHex:&lastReadHex];
[node.details insertRowWithOffset:range.location
:[NSString stringWithFormat:@"%.8lX", range.location]
:lastReadHex
:@"Child Count"
:[NSString stringWithFormat:@"%u",((uint32_t)-1 & childCount)]];
if (childCount == 0)
{
// separate export nodes
[node.details setAttributes:MVUnderlineAttributeName,@"YES",nil];
}
while (childCount-- > 0)
{
exportLocation = NSMaxRange(range);
NSString * label = [dataController read_string:range lastReadHex:&lastReadHex];
[node.details insertRowWithOffset:range.location
:[NSString stringWithFormat:@"%.8lX", range.location]
:lastReadHex
:@"Node Label"
:[NSString stringWithFormat:@"\"%@\"",label]];
uint64_t skip = [dataController read_uleb128:range lastReadHex:&lastReadHex];
[node.details insertRowWithOffset:range.location
:[NSString stringWithFormat:@"%.8lX", range.location]
:lastReadHex
:@"Next Node"
:[self is64bit] == NO
? [NSString stringWithFormat:@"0x%X",[self fileOffsetToRVA:location + skip]]
: [NSString stringWithFormat:@"0x%qX",[self fileOffsetToRVA64:location + skip]]];
if (childCount == 0)
{
// separate export nodes
[node.details setAttributes:MVUnderlineAttributeName,@"YES",nil];
}
[self printSymbols:[NSString stringWithFormat:@"%@%@", prefix, label]
location:location
skipBytes:skip
node:node
actionNode:actionNode
baseAddress:baseAddress
exportLocation:exportLocation];
}
}
//-----------------------------------------------------------------------------
- (MVNode *)createExportNode:(MVNode *)parent
caption:(NSString *)caption
location:(uint32_t)location
length:(uint32_t)length
baseAddress:(uint64_t)baseAddress
{
MVNode * dataNode = [self createDataNode:parent
caption:caption
location:location
length:length];
MVNodeSaver nodeSaver;
MVNode * node = [dataNode insertChildWithDetails:@"Opcodes" location:location length:length saver:nodeSaver];
MVNodeSaver actionNodeSaver;
MVNode * actionNode = [dataNode insertChildWithDetails:@"Actions" location:location length:length saver:actionNodeSaver];
uint32_t exportLocation = location;
// start to traverse with initial values
[self printSymbols:@""
location:location
skipBytes:0
node:node
actionNode:actionNode
baseAddress:baseAddress
exportLocation:exportLocation];
// line up the details of traversal
[node sortDetails];
[actionNode sortDetails];
return node;
}
//-----------------------------------------------------------------------------
@end