-
Notifications
You must be signed in to change notification settings - Fork 701
/
SectionContents.mm
655 lines (550 loc) · 24.5 KB
/
SectionContents.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
/*
* SectionContents.mm
* MachOView
*
* Created by psaghelyi on 15/09/2010.
*
*/
#include <string>
#include <vector>
#include <set>
#include <map>
#import "Common.h"
#import "SectionContents.h"
#import "CRTFootPrints.h"
#import "ReadWrite.h"
#import "DataController.h"
#import "capstone/include/capstone/capstone.h"
#import <mach-o/loader.h>
#define TAB_WIDTH 10
using namespace std;
//============================================================================
@implementation MachOLayout (SectionContents)
//-----------------------------------------------------------------------------
- (MVNode *)createPointersNode:(MVNode *)parent
caption:(NSString *)caption
location:(uint64_t)location
length:(uint64_t)length
{
MVNodeSaver nodeSaver;
MVNode * node = [parent insertChildWithDetails:caption location:location length:length saver:nodeSaver];
NSRange range = NSMakeRange(location,0);
NSString * lastReadHex;
while (NSMaxRange(range) < location + length)
{
uint32_t ptr = [dataController read_uint32:range lastReadHex:&lastReadHex];
NSString * symbolName = [NSString stringWithFormat:@"%@->%@",
[self findSymbolAtRVA:[self fileOffsetToRVA:range.location]],
[self findSymbolAtRVA:ptr]];
[node.details appendRow:[NSString stringWithFormat:@"%.8lX", range.location]
:lastReadHex
:@"Pointer"
:symbolName];
[node.details setAttributes:MVMetaDataAttributeName,symbolName,nil];
[symbolNames setObject:symbolName
forKey:[NSNumber numberWithUnsignedLong:[self fileOffsetToRVA:range.location]]];
}
return node;
}
//-----------------------------------------------------------------------------
- (MVNode *)createPointers64Node:(MVNode *)parent
caption:(NSString *)caption
location:(uint64_t)location
length:(uint64_t)length
{
MVNodeSaver nodeSaver;
MVNode * node = [parent insertChildWithDetails:caption location:location length:length saver:nodeSaver];
NSRange range = NSMakeRange(location,0);
NSString * lastReadHex;
while (NSMaxRange(range) < location + length)
{
uint64_t ptr = [dataController read_uint64:range lastReadHex:&lastReadHex];
NSString * symbolName = [NSString stringWithFormat:@"%@->%@",
[self findSymbolAtRVA:[self fileOffsetToRVA:range.location]],
[self findSymbolAtRVA:ptr]];
[node.details appendRow:[NSString stringWithFormat:@"%.8lX", range.location]
:lastReadHex
:@"Pointer"
:symbolName];
[node.details setAttributes:MVMetaDataAttributeName,symbolName,nil];
[symbolNames setObject:symbolName
forKey:[NSNumber numberWithUnsignedLongLong:[self fileOffsetToRVA:range.location]]];
}
return node;
}
//-----------------------------------------------------------------------------
-(MVNode *)createCStringsNode:(MVNode *)parent
caption:(NSString *)caption
location:(uint64_t)location
length:(uint64_t)length
{
MVNodeSaver nodeSaver;
MVNode * node = [parent insertChildWithDetails:caption location:location length:length saver:nodeSaver];
NSRange range = NSMakeRange(location,0);
NSString * lastReadHex;
while (NSMaxRange(range) < location + length)
{
NSString * symbolName = [dataController read_string:range lastReadHex:&lastReadHex];
[node.details appendRow:[NSString stringWithFormat:@"%.8lX", range.location]
:lastReadHex
:[NSString stringWithFormat:@"CString (length:%lu)", [symbolName length]]
:symbolName];
[node.details setAttributes:MVMetaDataAttributeName,symbolName,nil];
// fill in lookup table with C Strings
uint64_t rva = [self fileOffsetToRVA:range.location];
[symbolNames setObject:[NSString stringWithFormat:@"0x%qX:\"%@\"", rva, symbolName]
forKey:[NSNumber numberWithUnsignedLongLong:rva]];
}
return node;
}
//-----------------------------------------------------------------------------
-(MVNode *)createLiteralsNode:(MVNode *)parent
caption:(NSString *)caption
location:(uint64_t)location
length:(uint64_t)length
stride:(uint32_t)stride
{
MVNodeSaver nodeSaver;
MVNode * node = [parent insertChildWithDetails:caption location:location length:length saver:nodeSaver];
NSRange range = NSMakeRange(location,0);
NSString * lastReadHex;
while (NSMaxRange(range) < location + length)
{
NSData * data = [dataController read_bytes:range length:stride lastReadHex:&lastReadHex];
NSString * literalStr;
switch (stride)
{
case sizeof(float):
{
double num = *(float *)[data bytes];
literalStr = [NSString stringWithFormat:@"%.16g", num];
} break;
case sizeof(double):
{
double num = *(double *)[data bytes];
literalStr = [NSString stringWithFormat:@"%.16g", num];
} break;
default:
#if !defined (__arm64__)
// it's 8 for both cases in arm64
case sizeof(long double):
#endif
{
long double num = *(long double *)[data bytes];
literalStr = [NSString stringWithFormat:@"%.16Lg", num];
break;
}
}
[node.details appendRow:[NSString stringWithFormat:@"%.8lX", range.location]
:lastReadHex
:@"Floating Point Number"
:literalStr];
// fill in lookup table with string literals
uint64_t rva = [self fileOffsetToRVA:range.location];
[symbolNames setObject:[NSString stringWithFormat:@"0x%qX:%@f", rva, literalStr]
forKey:[NSNumber numberWithUnsignedLongLong:rva]];
}
return node;
}
//-----------------------------------------------------------------------------
- (MVNode *)createIndPointersNode:(MVNode *)parent
caption:(NSString *)caption
location:(uint64_t)location
length:(uint64_t)length
{
MVNodeSaver nodeSaver;
MVNode * node = [parent insertChildWithDetails:caption location:location length:length saver:nodeSaver];
NSRange range = NSMakeRange(location,0);
NSString * lastReadHex;
while (NSMaxRange(range) < location + length)
{
[dataController read_uint32:range lastReadHex:&lastReadHex];
NSString * symbolName = [self findSymbolAtRVA:[self fileOffsetToRVA:range.location]];
[node.details appendRow:[NSString stringWithFormat:@"%.8lX", range.location]
:lastReadHex
:@"Indirect Pointer"
:symbolName];
[node.details setAttributes:MVMetaDataAttributeName,symbolName,nil];
}
return node;
}
//-----------------------------------------------------------------------------
- (MVNode *)createIndPointers64Node:(MVNode *)parent
caption:(NSString *)caption
location:(uint64_t)location
length:(uint64_t)length
{
MVNodeSaver nodeSaver;
MVNode * node = [parent insertChildWithDetails:caption location:location length:length saver:nodeSaver];
NSRange range = NSMakeRange(location,0);
NSString * lastReadHex;
while (NSMaxRange(range) < location + length)
{
[dataController read_uint64:range lastReadHex:&lastReadHex];
NSString * symbolName = [self findSymbolAtRVA:[self fileOffsetToRVA:range.location]];
[node.details appendRow:[NSString stringWithFormat:@"%.8lX", range.location]
:lastReadHex
:@"Indirect Pointer"
:symbolName];
[node.details setAttributes:MVMetaDataAttributeName,symbolName,nil];
}
return node;
}
//-----------------------------------------------------------------------------
- (MVNode *)createIndStubsNode:(MVNode *)parent
caption:(NSString *)caption
location:(uint64_t)location
length:(uint64_t)length
stride:(uint32_t)stride
{
MVNodeSaver nodeSaver;
MVNode * node = [parent insertChildWithDetails:caption location:location length:length saver:nodeSaver];
NSRange range = NSMakeRange(location,0);
NSString * lastReadHex;
while (NSMaxRange(range) < location + length)
{
[dataController read_bytes:range length:stride lastReadHex:&lastReadHex];
NSString * symbolName = [self findSymbolAtRVA:[self fileOffsetToRVA:range.location]];
[node.details appendRow:[NSString stringWithFormat:@"%.8lX", range.location]
:lastReadHex
:@"Indirect Stub"
:symbolName];
[node.details setAttributes:MVMetaDataAttributeName,symbolName,nil];
}
return node;
}
//-----------------------------------------------------------------------------
- (MVNode *)createIndStubs64Node:(MVNode *)parent
caption:(NSString *)caption
location:(uint64_t)location
length:(uint64_t)length
stride:(uint32_t)stride
{
MVNodeSaver nodeSaver;
MVNode * node = [parent insertChildWithDetails:caption location:location length:length saver:nodeSaver];
NSRange range = NSMakeRange(location,0);
NSString * lastReadHex;
while (NSMaxRange(range) < location + length)
{
[dataController read_bytes:range length:stride lastReadHex:&lastReadHex];
NSString * symbolName = [self findSymbolAtRVA:[self fileOffsetToRVA:range.location]];
[node.details appendRow:[NSString stringWithFormat:@"%.8lX", range.location]
:lastReadHex
:@"Indirect Stub"
:symbolName];
[node.details setAttributes:MVMetaDataAttributeName,symbolName,nil];
}
return node;
}
//============================= X86 =========================
//===========================================================
static AsmFootPrint const classicStubHelperX86 =
{
{1, 0x68}, GAP(4), // pushl $foo$lazy_ptr
{1, 0xE9}, GAP(4), // jmp helperhelper
};
static AsmFootPrint const hybridStubHelperX86 =
{
{1, 0x68}, GAP(4), // pushl $lazy-info-offset
{1, 0x68}, GAP(4), // pushl $foo$lazy_ptr
{1, 0xE9}, GAP(4), // jmp dyld_hybrid_stub_binding_helper
{1, 0x90}, // nop
};
static AsmFootPrint const hybridStubHelperHelperX86 =
{
{2, 0x83, 0x3D}, GAP(4), {1, 0x00}, // cmpl $0x00,_fast_lazy_bind
{2, 0x75, 0x0D}, // jne $0x0D
{4, 0x89, 0x44, 0x24, 0x04}, // movl %eax,4(%esp)
{1, 0x58}, // popl %eax
{3, 0x87, 0x04, 0x24}, // xchgl (%esp),%eax
{1, 0xE9}, GAP(4), // jmpl dyld_stub_binding_helper
{3, 0x83, 0xC4, 0x04}, // addl $0x04,%esp
{1, 0x68}, GAP(4), // pushl imageloadercache
{2, 0xFF, 0x25}, GAP(4), // jmp *_fast_lazy_bind(%rip)
};
static AsmFootPrint const fastStubHelperX86 =
{
{1, 0x68}, GAP(4), // pushl $lazy-info-offset
{1, 0xE9}, GAP(4), // jmp helperhelper
};
static AsmFootPrint const fastStubHelperHelperX86 =
{
{1, 0x68}, GAP(4), // pushl imageloadercache
{2, 0xFF, 0x25}, GAP(4), // jmp *_fast_lazy_bind
{1, 0x90}, // nop
};
//=========================== X86_64 ========================
//===========================================================
static AsmFootPrint const classicStubHelperX86_64 =
{
{3, 0x4C, 0x8D, 0x1D}, GAP(4), // lea foo$lazy_ptr(%rip),%r11
{1, 0xE9}, GAP(4), // jmp dyld_stub_binding_helper
};
static AsmFootPrint const hybridStubHelperX86_64 =
{
{1, 0x68}, GAP(4), // pushq $lazy-info-offset
{3, 0x4C, 0x8D, 0x1D}, GAP(4), // lea foo$lazy_ptr(%rip),%r11
{1, 0xE9}, GAP(4), // jmp helper-helper
{1, 0x90}, // nop
};
static AsmFootPrint const hybridStubHelperHelperX86_64 =
{
{3, 0x48, 0x83, 0x3D}, GAP(4), {1, 0x00}, // cmpq $0x00,_fast_lazy_bind
{2, 0x74, 0x0F}, // je $0x0F
{3, 0x4C, 0x8D, 0x1D}, GAP(4), // leaq imageCache(%rip),%r11
{2, 0x41, 0x53}, // pushq %r11
{2, 0xFF, 0x25}, GAP(4), // jmp *_fast_lazy_bind(%rip)
{4, 0x48, 0x83, 0xC4, 0x08}, // addq $8,%rsp
{1, 0xE9}, GAP(4), // jmp dyld_stub_binding_helper
};
static AsmFootPrint const fastStubHelperX86_64 =
{
{1, 0x68}, GAP(4), // pushq $lazy-info-offset
{1, 0xE9}, GAP(4), // jmp helperhelper
};
static AsmFootPrint const fastStubHelperHelperX86_64 =
{
{3, 0x4C, 0x8D, 0x1D}, GAP(4), // leaq dyld_mageLoaderCache(%rip),%r11
{2, 0x41, 0x53}, // pushq %r11
{2, 0xFF, 0x25}, GAP(4), // jmp *_fast_lazy_bind(%rip)
{1, 0x90}, // nop
};
//=========================== ARM ===========================
//===========================================================
static AsmFootPrint const fastStubHelperARM =
{
{4, 0xe5, 0x9f, 0xc0, 0x00}, // ldr ip, [pc, #0]
{4, 0xea, 0x00, 0x00, 0x00}, // b _helperhelper
GAP(4), // lazy binding info
};
static AsmFootPrint const fastStubHelperHelperARM =
{
// push lazy-info-offset
{4, 0xe5, 0x2d, 0xc0, 0x04}, // str ip, [sp, #-4]!
// push address of dyld_mageLoaderCache
{4, 0xe5, 0x9f, 0xc0, 0x10}, // ldr ip, L1
{4, 0xe0, 0x8f, 0xc0, 0x0c}, // add ip, pc, ip
{4, 0xe5, 0x2d, 0xc0, 0x04}, // str ip, [sp, #-4]!
// jump through _fast_lazy_bind
{4, 0xe5, 0x9f, 0xc0, 0x08}, // ldr ip, L2
{4, 0xe0, 0x8f, 0xc0, 0x0c}, // add ip, pc, ip
{4, 0xe5, 0x9c, 0xf0, 0x00}, // ldr pc, [ip]
GAP(4), // L1: .long fFastStubGOTAtom - (helperhelper+16)
GAP(4), // L2: .long _fast_lazy_bind - (helperhelper+28)
};
//-----------------------------------------------------------------------------
// __stub_helper:
//
// StubHelperHelper
// StubHelper
// StubHelper
// ...
// __symbol_stub1:
// FF 25 <relative to indirect>
// ...
//-----------------------------------------------------------------------------
- (MVNode *)createStubHelperNode:(MVNode *)parent
caption:(NSString *)caption
location:(uint64_t)location
length:(uint64_t)length
{
MVNodeSaver nodeSaver;
MVNode * node = [parent insertChildWithDetails:caption location:location length:length saver:nodeSaver];
NSRange range = NSMakeRange(location,0);
NSString * lastReadHex;
NSData * data;
uint64_t address;
if ([self matchAsmAtOffset:range.location
asmFootPrint:hybridStubHelperHelperX86
lineCount:sizeof(hybridStubHelperHelperX86)/FOOTPRINT_STRIDE])
{
data = [dataController read_bytes:range length:7 lastReadHex:&lastReadHex];
address = *(uint32_t *)((uint8_t *)data.bytes + 2);
[node.details appendRow:[NSString stringWithFormat:@"%.8lX", range.location]
:lastReadHex
:@"cmpl $0x00,_fast_lazy_bind"
:[self findSymbolAtRVA:address]];
[dataController read_bytes:range length:2 lastReadHex:&lastReadHex];
[node.details appendRow:[NSString stringWithFormat:@"%.8lX", range.location]
:lastReadHex
:@"jne $0x0D"
:@""];
[dataController read_bytes:range length:4 lastReadHex:&lastReadHex];
[node.details appendRow:[NSString stringWithFormat:@"%.8lX", range.location]
:lastReadHex
:@"movl %eax,4(%esp)"
:@""];
[dataController read_bytes:range length:1 lastReadHex:&lastReadHex];
[node.details appendRow:[NSString stringWithFormat:@"%.8lX", range.location]
:lastReadHex
:@"popl %eax"
:@""];
[dataController read_bytes:range length:3 lastReadHex:&lastReadHex];
[node.details appendRow:[NSString stringWithFormat:@"%.8lX", range.location]
:lastReadHex
:@"xchgl (%esp),%eax"
:@""];
data = [dataController read_bytes:range length:5 lastReadHex:&lastReadHex];
address = [self fileOffsetToRVA:NSMaxRange(range) + *(uint32_t *)((uint8_t *)data.bytes + 1)];
[node.details appendRow:[NSString stringWithFormat:@"%.8lX", range.location]
:lastReadHex
:@"jmpl dyld_stub_binding_helper"
:[self findSymbolAtRVA:address]];
[dataController read_bytes:range length:3 lastReadHex:&lastReadHex];
[node.details appendRow:[NSString stringWithFormat:@"%.8lX", range.location]
:lastReadHex
:@"addl $0x04,%esp"
:@""];
data = [dataController read_bytes:range length:5 lastReadHex:&lastReadHex];
address = *(uint32_t *)((uint8_t *)data.bytes + 1);
[node.details appendRow:[NSString stringWithFormat:@"%.8lX", range.location]
:lastReadHex
:@"pushl imageloadercache"
:[self findSymbolAtRVA:address]];
data = [dataController read_bytes:range length:6 lastReadHex:&lastReadHex];
address = *(uint32_t *)((uint8_t *)data.bytes + 2);
[node.details appendRow:[NSString stringWithFormat:@"%.8lX", range.location]
:lastReadHex
:@"jmp *_fast_lazy_bind(%rip)"
:[self findSymbolAtRVA:address]];
[node.details setAttributes:MVUnderlineAttributeName,@"YES",nil];
}
else if ([self matchAsmAtOffset:range.location
asmFootPrint:fastStubHelperHelperX86
lineCount:sizeof(fastStubHelperHelperX86)/FOOTPRINT_STRIDE])
{
data = [dataController read_bytes:range length:5 lastReadHex:&lastReadHex];
address = *(uint32_t *)((uint8_t *)data.bytes + 1);
[node.details appendRow:[NSString stringWithFormat:@"%.8lX", range.location]
:lastReadHex
:@"pushl imageloadercache"
:[self findSymbolAtRVA:address]];
data = [dataController read_bytes:range length:6 lastReadHex:&lastReadHex];
address = *(uint32_t *)((uint8_t *)data.bytes + 2);
[node.details appendRow:[NSString stringWithFormat:@"%.8lX", range.location]
:lastReadHex
:@"jmp *_fast_lazy_bind"
:[self findSymbolAtRVA:address]];
[dataController read_bytes:range length:1 lastReadHex:&lastReadHex];
[node.details appendRow:[NSString stringWithFormat:@"%.8lX", range.location]
:lastReadHex
:@"nop"
:@""];
[node.details setAttributes:MVUnderlineAttributeName,@"YES",nil];
}
return node;
}
//-----------------------------------------------------------------------------
- (MVNode *)createTextNode:(MVNode *)parent
caption:(NSString *)caption
location:(uint64_t)location
length:(uint64_t)length
reloff:(uint64_t)reloff
nreloc:(uint32_t)nreloc
extreloff:(uint64_t)extreloff
nextrel:(uint32_t)nextrel
locreloff:(uint64_t)locreloff
nlocrel:(uint32_t)nlocrel
{
MVNodeSaver nodeSaver;
MVNode * node = [parent insertChildWithDetails:caption location:location length:length saver:nodeSaver];
// accumulate search info
NSUInteger bookmark = node.details.rowCount;
NSString * symbolName = nil;
if (length == 0) // prevent from attempting to parse zero length sections
{
return node;
}
// prepare disassembler params
//===========================================================================
MATCH_STRUCT(mach_header,imageOffset);
char * ot_sect = (char*)[dataController.fileData bytes] + location;
uint64_t ot_left = length;
uint64_t ot_addr = [self fileOffsetToRVA:location];
csh cs_handle = 0;
cs_insn *cs_insn = NULL;
size_t disasm_count = 0;
cs_err cserr;
/* open capstone */
cs_arch target_arch = CS_ARCH_ALL;
cs_mode target_mode = CS_MODE_LITTLE_ENDIAN;
switch (mach_header->cputype)
{
case CPU_TYPE_I386:
target_arch = CS_ARCH_X86;
target_mode = CS_MODE_32;
break;
case CPU_TYPE_X86_64:
target_arch = CS_ARCH_X86;
target_mode = CS_MODE_64;
break;
case CPU_TYPE_ARM:
target_arch = CS_ARCH_ARM;
target_mode = CS_MODE_ARM;
break;
case CPU_TYPE_ARM64:
target_arch = CS_ARCH_ARM64;
target_mode = CS_MODE_LITTLE_ENDIAN;
break;
default:
NSLog(@"No CPU found to disassemble!");
return node;
}
if ( (cserr = cs_open(target_arch, target_mode, &cs_handle)) != CS_ERR_OK )
{
NSLog(@"Failed to initialize Capstone: %d, %s.", cserr, cs_strerror(cs_errno(cs_handle)));
return node;
}
/* set or not thumb mode for 32 bits ARM targets */
if (mach_header->cputype == CPU_TYPE_ARM)
{
switch (mach_header->cpusubtype)
{
case CPU_SUBTYPE_ARM_V7:
case CPU_SUBTYPE_ARM_V7F:
case CPU_SUBTYPE_ARM_V7S:
case CPU_SUBTYPE_ARM_V7K:
case CPU_SUBTYPE_ARM_V8:
cs_option(cs_handle, CS_OPT_MODE, CS_MODE_THUMB);
break;
default:
cs_option(cs_handle, CS_OPT_MODE, CS_MODE_ARM);
break;
}
}
/* enable detail - we need fields available in detail field */
cs_option(cs_handle, CS_OPT_DETAIL, CS_OPT_ON);
cs_option(cs_handle, CS_OPT_SKIPDATA, CS_OPT_ON);
/* disassemble the whole section */
/* this will fail if we have data in code or jump tables because Capstone stops when it can't disassemble */
/* a bit of a problem with most binaries :( */
/* XXX: parse data in code section to partially solve this */
disasm_count = cs_disasm(cs_handle, (const uint8_t *)ot_sect, ot_left, ot_addr, 0, &cs_insn);
NSLog(@"Disassembled %lu instructions.", disasm_count);
uint64_t fileOffset = [self RVAToFileOffset:ot_addr];
for (size_t i = 0; i < disasm_count; i++)
{
/* XXX: replace this bytes retrieval with Capstone internal data since it already contains this info */
NSRange range = NSMakeRange(fileOffset,0);
NSString * lastReadHex;
[dataController read_bytes:range length:cs_insn[i].size lastReadHex:&lastReadHex];
/* format the disassembly output using Capstone strings */
NSString *asm_string = [NSString stringWithFormat:@"%-10s\t%s", cs_insn[i].mnemonic, cs_insn[i].op_str];
[node.details appendRow:[NSString stringWithFormat:@"%.8qX", fileOffset]
:lastReadHex
:asm_string
:@""];
/* advance to next instruction */
fileOffset += cs_insn[i].size;
}
cs_free(cs_insn, disasm_count);
cs_close(&cs_handle);
// close last block
if (symbolName)
{
[node.details setAttributesFromRowIndex:bookmark:MVMetaDataAttributeName,symbolName,nil];
[node.details setAttributes:MVUnderlineAttributeName,@"YES",nil];
}
return node;
}
@end