This repository has been archived by the owner on Jun 17, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMachOReader.m
708 lines (590 loc) · 16.4 KB
/
MachOReader.m
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
//
// MachOReader.m
// MachoViewer
//
// Created by Alexander Stavonin on 28.01.10.
// Copyright 2010 __MyCompanyName__. All rights reserved.
//
#include "sglib.h"
#import <mach-o/loader.h>
#import <mach-o/nlist.h>
#import "MachOReader.h"
////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////// Data representation //////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////
@implementation MachODyLib
@synthesize name;
@synthesize pathName;
@synthesize timestamp;
@synthesize current_version;
@synthesize compatibility_version;
-(id)initWithDylib:(macho_dylib_t*)data
{
if (self = [super init]) {
NSString *tmpPath = [NSString stringWithCString:data->name encoding:NSUTF8StringEncoding];
NSArray *pathComp = [tmpPath componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"/"]];
name = [pathComp objectAtIndex:([pathComp count] - 1)];
[name retain];
pathName = [[NSURL alloc] initFileURLWithPath:[NSString stringWithCString:data->name encoding:NSUTF8StringEncoding] isDirectory:NO];
timestamp = [[NSNumber alloc] initWithUnsignedInt:data->timestamp];
current_version = [[NSNumber alloc] initWithUnsignedInt:data->current_version];
compatibility_version = [[NSNumber alloc] initWithUnsignedInt:data->compatibility_version];
}
return self;
}
-(void)dealloc
{
[name release];
[pathName release];
[timestamp release];
[current_version release];
[compatibility_version release];
[super dealloc];
}
@end
@implementation MachOSymbol
@synthesize name;
@synthesize type;
@synthesize sect;
@synthesize desc;
-(id)initWithSymbol:(macho_symbol_t*)data;
{
if (self = [super init]) {
name = [[NSString alloc] initWithCString:data->name];
type = [[NSNumber alloc] initWithUnsignedChar:data->type];
sect = data->sect;
desc = data->desc;
}
return self;
}
-(void)dealloc
{
[name release];
[type release];
[super dealloc];
}
@end
@implementation MachOSection
@synthesize sectname;
@synthesize segname;
@synthesize addr;
@synthesize size;
@synthesize offset;
@synthesize align;
@synthesize reloff;
@synthesize nreloc;
@synthesize flags;
@synthesize sections;
@synthesize asString;
-(id)initWithSection:(macho_section_t*)data
{
if (self = [super init]) {
sectname = [[NSString alloc] initWithCString:data->sectname];
segname = [[NSString alloc] initWithCString:data->segname];
addr = data->addr;
size = data->size;
offset = data->offset;
align = data->align;
reloff = data->reloff;
flags = data->flags;
sections = [[NSArray alloc] init];
[self buildInfoString];
}
return self;
}
-(id)initWithSection64:(macho_section_64_t*)data
{
if (self = [super init]) {
sectname = [[NSString alloc] initWithCString:data->sectname];
segname = [[NSString alloc] initWithCString:data->segname];
addr = data->addr;
size = data->size;
offset = data->offset;
align = data->align;
reloff = data->reloff;
flags = data->flags;
sections = [[NSArray alloc] init];
[self buildInfoString];
}
return self;
}
-(void)buildInfoString
{
StringFormatter *formatter = [[StringFormatter alloc] init];
[formatter addString:sectname withName:@"Section name"];
[formatter addUInt:addr withName:@"Memory address"];
[formatter addUInt:size withName:@"Section size"];
[formatter addUInt:offset withName:@"File offset"];
[formatter addUInt:align withName:@"Section alignment"];
[formatter addUInt:reloff withName:@"Relocation entries offset"];
[formatter addUInt:flags withName:@"Flags"];
NSAttributedString *info = [formatter resultString];
asString = [[NSAttributedString alloc] initWithAttributedString:info];
[formatter release];
}
-(void)dealloc
{
[sectname release];
[segname release];
[sections release];
[super dealloc];
}
@end
@implementation MachOSegment
@synthesize segname;
@synthesize vmaddr;
@synthesize vmsize;
@synthesize fileoff;
@synthesize filesize;
@synthesize flags;
@synthesize nsects;
@synthesize sections;
@synthesize asString;
-(id)initWithSegment:(macho_segment_t*)data
{
if (self = [super init]) {
segname = [[NSString alloc] initWithCString:data->segname];
vmaddr = data->vmaddr;
vmsize = data->vmsize;
fileoff = data->fileoff;
filesize = data->filesize;
flags = data->flags;
sections = [[NSMutableArray alloc] init];
SGLIB_LIST_MAP_ON_ELEMENTS(section_list_t, data->sections, ll, next_ptr, {
MachOSection *section = [[MachOSection alloc] initWithSection:ll->data];
[sections addObject:section];
});
nsects = [sections count];
[self buildInfoString];
}
return self;
}
-(id)initWithSegment64:(macho_segment_64_t*)data
{
if (self = [super init]) {
segname = [[NSString alloc] initWithCString:data->segname];
vmaddr = data->vmaddr;
vmsize = data->vmsize;
fileoff = data->fileoff;
filesize = data->filesize;
flags = data->flags;
sections = [[NSMutableArray alloc] init];
SGLIB_LIST_MAP_ON_ELEMENTS(section_64_list_t, data->sections, ll, next_ptr, {
MachOSection *section = [[MachOSection alloc] initWithSection64:ll->data];
[sections addObject:section];
});
nsects = [sections count];
[self buildInfoString];
}
return self;
}
-(void)buildInfoString
{
StringFormatter *formatter = [[StringFormatter alloc] init];
[formatter addString:segname withName:@"Segment name"];
[formatter addUInt:vmaddr withName:@"Memory address"];
[formatter addUInt:vmsize withName:@"Memory size"];
[formatter addUInt:fileoff withName:@"File offset"];
[formatter addUInt:filesize withName:@"File size"];
[formatter addUInt:nsects withName:@"Number of sections"];
[formatter addUInt:flags withName:@"Flags"];
NSAttributedString *info = [formatter resultString];
asString = [[NSAttributedString alloc] initWithAttributedString:info];
[formatter release];
}
-(void)dealloc
{
[segname release];
[sections release];
[super dealloc];
}
@end
@implementation MachOArch
@synthesize magic;
@synthesize cputype;
@synthesize cpusubtype;
@synthesize offset;
@synthesize filetype;
@synthesize ncmds;
@synthesize flags;
@synthesize dylinker;
@synthesize segments;
@synthesize dylibs;
@synthesize symbols;
@synthesize nseg;
-(id)initWithArch:(macho_arch_t*)data
{
if (self = [super init]) {
magic = [[NSNumber alloc] initWithUnsignedInt:data->magic];
cputype = [[NSNumber alloc] initWithInt:data->cputype];
cpusubtype = [[NSNumber alloc] initWithInt:data->cpusubtype];
offset = [[NSNumber alloc] initWithUnsignedInt:data->offset];
filetype = [[NSNumber alloc] initWithUnsignedInt:data->filetype];
ncmds = [[NSNumber alloc] initWithUnsignedInt:data->ncmds];
flags = [[NSNumber alloc] initWithUnsignedInt:data->flags];
if (data->dylinker) {
dylinker = [[NSURL alloc] initFileURLWithPath:
[NSString stringWithCString:data->dylinker encoding:NSUTF8StringEncoding]
isDirectory:NO];
}
segments = [[NSMutableArray alloc] init];
dylibs = [[NSMutableArray alloc] init];
symbols = [[NSMutableArray alloc] init];
SGLIB_LIST_MAP_ON_ELEMENTS(segment_list_t, data->segments, lseg, next_ptr, {
MachOSegment *segment = [[MachOSegment alloc] initWithSegment:lseg->data];
[segments addObject:segment];
});
SGLIB_LIST_MAP_ON_ELEMENTS(segment_64_list_t, data->segments64, lseg, next_ptr, {
MachOSegment *segment = [[MachOSegment alloc] initWithSegment64:lseg->data];
[segments addObject:segment];
});
SGLIB_LIST_MAP_ON_ELEMENTS(dylib_list_t, data->dylibs, ldlib, next_ptr, {
MachODyLib *dylib = [[MachODyLib alloc] initWithDylib:ldlib->data];
[dylibs addObject:dylib];
});
SGLIB_LIST_MAP_ON_ELEMENTS(symbol_list_t, data->symbols, lsym, next_ptr, {
MachOSymbol *symbol = [[MachOSymbol alloc] initWithSymbol:lsym->data];
[symbols addObject:symbol];
});
nseg = [[NSNumber alloc] initWithUnsignedInt:[segments count]];
}
return self;
}
-(void)dealloc
{
[magic release];
[cputype release];
[cpusubtype release];
[offset release];
[filetype release];
[ncmds release];
[flags release];
[dylinker release];
[segments release];
[dylibs release];
[symbols release];
[nseg release];
[super dealloc];
}
@end
////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////// Reader /////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////
@implementation MachOReader
@synthesize archs;
@synthesize binaryPath;
@synthesize binaryName;
-(id)init
{
if (self = [super init]) {
archs = [[NSMutableArray alloc] init];
}
return self;
}
-(BOOL)openMachOFile:(NSString*)path
{
machoInfo = open_macho_file([path UTF8String]);
if (machoInfo == NULL) {
return NO;
}
NSArray *pathComp;
[self willChangeValueForKey:@"archs"];
binaryPath = [[NSURL alloc] initFileURLWithPath:path isDirectory:NO];
pathComp = [path componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"/"]];
binaryName = [pathComp objectAtIndex:([pathComp count] - 1)];
[archs removeAllObjects];
SGLIB_LIST_MAP_ON_ELEMENTS(arch_list_t, machoInfo->arch, larch, next_ptr, {
MachOArch *arch = [[MachOArch alloc] initWithArch:larch->data];
[archs addObject:arch];
});
[self didChangeValueForKey:@"archs"];
return YES;
}
-(void)closeMachOFile
{
if (machoInfo != NULL) {
close_macho_file(machoInfo);
machoInfo = NULL;
}
}
-(void)dealloc
{
[archs release];
[super dealloc];
}
@end
////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////// Transformers /////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////
static NSString *kArchI386 = @"Intel-based";
static NSString *kArchPPC = @"PowerPC-based";
static NSString *kArchX86_64 = @"64-bit Intel-based";
static NSString *kArchPPC_64 = @"64-bit PowerPC–based";
static NSString *kArchUnknown = @"Unknown";
static NSString* arch_to_string(cpu_type_t cpu_type)
{
NSString *result;
switch (cpu_type) {
case CPU_TYPE_I386:
result = kArchI386;
break;
case CPU_TYPE_POWERPC:
result = kArchPPC;
break;
case CPU_TYPE_X86_64:
result = kArchX86_64;
break;
case CPU_TYPE_POWERPC64:
result = kArchPPC_64;
break;
default:
result = kArchUnknown;
break;
}
return result;
}
@implementation ArchListToNamesTransformer
+ (Class)transformedValueClass;
{
return [NSString class];
}
+ (BOOL)allowsReverseTransformation;
{
return NO;
}
-(id)transformedValue:(id)value;
{
MachOArch *arch;
cpu_type_t cpu_type;
NSMutableArray *result = [[[NSMutableArray alloc] init] autorelease];
int i, count = [value count];
for (i = 0; i < count; i++) {
arch = [value objectAtIndex:i];
cpu_type = [arch.cputype unsignedIntValue];
[result addObject:arch_to_string(cpu_type)];
}
return result;
}
@end
@implementation ArchToNameTransformer
+ (Class)transformedValueClass;
{
return [NSString class];
}
+ (BOOL)allowsReverseTransformation;
{
return NO;
}
-(id)transformedValue:(id)value;
{
cpu_type_t cpu_type;
NSString *result;
cpu_type = [value unsignedIntValue];
result = arch_to_string(cpu_type);
return result;
}
@end
@implementation DecToHexTransformer
+ (Class)transformedValueClass;
{
return [NSString class];
}
+ (BOOL)allowsReverseTransformation;
{
return NO;
}
-(id)transformedValue:(id)value;
{
NSString *result;
result = [[[NSString alloc] initWithFormat:@"0x%X (%u)", [value unsignedIntValue], [value unsignedIntValue]] autorelease];
return result;
}
@end
@implementation FileTypeTransformer
static NSString *kFTObject = @"MH_OBJECT";
static NSString *kFTExecute = @"MH_EXECUTE";
static NSString *kFTBundle = @"MH_BUNDLE";
static NSString *kFTDylib = @"MH_DYLIB";
static NSString *kFTPreload = @"MH_PRELOAD";
static NSString *kFTCore = @"MH_CORE";
static NSString *kFTDylinker = @"MH_DYLINKER";
static NSString *kFTDsym = @"MH_DSYM";
static NSString *kFTInvalid = @"(Invalid)";
+ (Class)transformedValueClass;
{
return [NSString class];
}
+ (BOOL)allowsReverseTransformation;
{
return NO;
}
-(id)transformedValue:(id)value;
{
NSString *result;
switch ([value unsignedIntValue]) {
case MH_OBJECT:
result = kFTObject;
break;
case MH_EXECUTE:
result = kFTExecute;
break;
case MH_BUNDLE:
result = kFTBundle;
break;
case MH_DYLIB:
result = kFTDylib;
break;
case MH_PRELOAD:
result = kFTPreload;
break;
case MH_CORE:
result = kFTCore;
break;
case MH_DYLINKER:
result = kFTDylinker;
break;
case MH_DSYM:
result = kFTDsym;
break;
default:
result = kFTInvalid;
break;
}
return result;
}
@end
@implementation ArchFlagsTransformer
struct arch_flag_strings {
uint32 flag;
NSString *name;
};
typedef struct arch_flag_strings arch_flag_strings_t;
static arch_flag_strings_t arch_flags[] = {
{MH_NOUNDEFS, @"MH_NOUNDEFS"},
{MH_INCRLINK, @"MH_INCRLINK"},
{MH_DYLDLINK, @"MH_DYLDLINK"},
{MH_TWOLEVEL, @"MH_TWOLEVEL"},
{MH_BINDATLOAD, @"MH_BINDATLOAD"},
{MH_PREBOUND, @"MH_PREBOUND"},
{MH_PREBINDABLE, @"MH_PREBINDABLE"},
{MH_NOFIXPREBINDING, @"MH_NOFIXPREBINDING"},
{MH_ALLMODSBOUND, @"MH_ALLMODSBOUND"},
{MH_CANONICAL, @"MH_CANONICAL"},
{MH_SPLIT_SEGS, @"MH_SPLIT_SEGS"},
{MH_FORCE_FLAT, @"MH_FORCE_FLAT"},
{MH_SUBSECTIONS_VIA_SYMBOLS, @"MH_SUBSECTIONS_VIA_SYMBOLS"},
{MH_NOMULTIDEFS, @"MH_NOMULTIDEFS"}
};
+ (Class)transformedValueClass;
{
return [NSString class];
}
+ (BOOL)allowsReverseTransformation;
{
return NO;
}
-(id)transformedValue:(id)value;
{
static NSString *emptyFormatString = @"%@";
static NSString *nonEmptyFormatString = @" | %@";
NSString *formatter;
NSMutableString *result;
int i, count = sizeof(arch_flags) / sizeof(arch_flag_strings_t);
uint32 flags = [value unsignedIntValue];
result = [[[NSMutableString alloc] init] autorelease];
for (i = 0; i < count; i++) {
if ((flags & arch_flags[i].flag) != 0 ) {
if ([result isEqualToString:@""] == YES) {
formatter = emptyFormatString;
}
else {
formatter = nonEmptyFormatString;
}
[result appendString:[NSString stringWithFormat:formatter, arch_flags[i].name]];
}
}
return result;
}
@end
@implementation SymbFlagsTransformer
struct symb_flag_strings {
uint8_t flag;
NSString *name;
};
typedef struct symb_flag_strings symb_flag_strings_t;
static symb_flag_strings_t symb_flags_type[] = {
{N_STAB, @"S"},
{N_PEXT, @"P"},
{N_TYPE, @"T"},
{N_EXT, @"E"}
};
static symb_flag_strings_t symb_flags_ext[] = {
{N_UNDF, @"u"},
{N_ABS, @"a"},
{N_SECT, @"s"},
{N_PBUD, @"p"},
{N_INDR, @"i"}
};
+ (Class)transformedValueClass;
{
return [NSString class];
}
+ (BOOL)allowsReverseTransformation;
{
return NO;
}
-(id)transformedValue:(id)value;
{
NSMutableString *result;
// NSString *asNumber;
int i, count = sizeof(symb_flags_type) / sizeof(symb_flag_strings_t),
j, count_ext = sizeof(symb_flags_ext) / sizeof(symb_flag_strings_t);
uint8 flags = [value unsignedCharValue];
result = [[[NSMutableString alloc] init] autorelease];
for (i = 0; i < count; i++) {
if ((flags & symb_flags_type[i].flag) != 0 ) {
[result appendString:symb_flags_type[i].name];
if (flags & symb_flags_type[i].flag == N_TYPE) {
for (j = 0; j < count_ext; j++) {
if ((flags & symb_flags_ext[j].flag) != 0 ) {
[result appendString:symb_flags_ext[j].name];
}
}
}
}
}
// asNumber = [[NSString alloc] initWithFormat:@" (0x%X)", flags];
// [result appendString:asNumber];
// [asNumber release];
return result;
}
@end
@implementation StringFormatter
@synthesize resultString;
-(id)init
{
if (self = [super init]) {
boldFont = [NSDictionary dictionaryWithObject:[NSFont boldSystemFontOfSize:10.0] forKey:NSFontAttributeName];
normalFont = [NSDictionary dictionaryWithObject:[NSFont systemFontOfSize:10.0] forKey:NSFontAttributeName];
resultString = [[NSMutableAttributedString alloc] init];
}
return self;
}
-(void)dealloc
{
[resultString release];
[super dealloc];
}
-(void)addUInt:(uint)data withName:(NSString*)name
{
NSString *formatter = [NSString stringWithFormat:@"0x%X (%u)", data, data];
[self addString:formatter withName:name];
}
-(void)addString:(NSString*)data withName:(NSString*)name
{
[resultString appendAttributedString:[[NSAttributedString alloc] initWithString:name attributes:boldFont]];
[resultString appendAttributedString:[[NSAttributedString alloc] initWithString:@": " attributes:normalFont]];
[resultString appendAttributedString:[[NSAttributedString alloc] initWithString:data attributes:normalFont]];
[resultString appendAttributedString:[[NSAttributedString alloc] initWithString:@"\n"]];
}
@end