forked from Naville/class-dump-utf8
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CDClassDump.m
545 lines (432 loc) · 13.8 KB
/
CDClassDump.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
// -*- mode: ObjC -*-
// This file is part of class-dump, a utility for examining the Objective-C segment of Mach-O files.
// Copyright (C) 1997-1998, 2000-2001, 2004-2009 Steve Nygard.
#import "CDClassDump.h"
#import <Foundation/Foundation.h>
#import "NSArray-Extensions.h"
#import "NSString-Extensions.h"
#import "CDFatArch.h"
#import "CDFatFile.h"
#import "CDLCDylib.h"
#import "CDMachOFile.h"
#import "CDObjectiveC1Processor.h"
#import "CDStructureTable.h"
#import "CDSymbolReferences.h"
#import "CDType.h"
#import "CDTypeFormatter.h"
#import "CDTypeParser.h"
#import "CDVisitor.h"
#import "CDLCSegment.h"
#import "CDObjectiveC2Processor64.h"
@implementation CDClassDump
- (id)init;
{
if ([super init] == nil)
return nil;
executablePath = nil;
machOFiles = [[NSMutableArray alloc] init];
machOFilesByID = [[NSMutableDictionary alloc] init];
objcProcessors = [[NSMutableArray alloc] init];
structureTable = [[CDStructureTable alloc] init];
[structureTable setAnonymousBaseName:@"CDAnonymousStruct"];
[structureTable setName:@"Structs"];
unionTable = [[CDStructureTable alloc] init];
[unionTable setAnonymousBaseName:@"CDAnonymousUnion"];
[unionTable setName:@"Unions"];
ivarTypeFormatter = [[CDTypeFormatter alloc] init];
[ivarTypeFormatter setShouldExpand:NO];
[ivarTypeFormatter setShouldAutoExpand:YES];
[ivarTypeFormatter setBaseLevel:1];
[ivarTypeFormatter setDelegate:self];
methodTypeFormatter = [[CDTypeFormatter alloc] init];
[methodTypeFormatter setShouldExpand:NO];
[methodTypeFormatter setShouldAutoExpand:NO];
[methodTypeFormatter setBaseLevel:0];
[methodTypeFormatter setDelegate:self];
propertyTypeFormatter = [[CDTypeFormatter alloc] init];
[propertyTypeFormatter setShouldExpand:NO];
[propertyTypeFormatter setShouldAutoExpand:NO];
[propertyTypeFormatter setBaseLevel:0];
[propertyTypeFormatter setDelegate:self];
structDeclarationTypeFormatter = [[CDTypeFormatter alloc] init];
[structDeclarationTypeFormatter setShouldExpand:YES]; // But don't expand named struct members...
[structDeclarationTypeFormatter setShouldAutoExpand:YES];
[structDeclarationTypeFormatter setBaseLevel:0];
[structDeclarationTypeFormatter setDelegate:self]; // But need to ignore some things?
// These can be ppc, ppc7400, ppc64, i386, x86_64
targetArch.cputype = CPU_TYPE_ANY;
targetArch.cpusubtype = 0;
flags.shouldShowHeader = YES;
return self;
}
- (void)dealloc;
{
[executablePath release];
[machOFiles release];
[machOFilesByID release];
[objcProcessors release];
[structureTable release];
[unionTable release];
[ivarTypeFormatter release];
[methodTypeFormatter release];
[propertyTypeFormatter release];
[structDeclarationTypeFormatter release];
if (flags.shouldMatchRegex)
regfree(&compiledRegex);
[super dealloc];
}
- (NSString *)executablePath;
{
return executablePath;
}
- (void)setExecutablePath:(NSString *)newPath;
{
if (newPath == executablePath)
return;
[executablePath release];
executablePath = [newPath retain];
}
- (BOOL)shouldProcessRecursively;
{
return flags.shouldProcessRecursively;
}
- (void)setShouldProcessRecursively:(BOOL)newFlag;
{
flags.shouldProcessRecursively = newFlag;
}
- (BOOL)shouldSortClasses;
{
return flags.shouldSortClasses;
}
- (void)setShouldSortClasses:(BOOL)newFlag;
{
flags.shouldSortClasses = newFlag;
}
- (BOOL)shouldSortClassesByInheritance;
{
return flags.shouldSortClassesByInheritance;
}
- (void)setShouldSortClassesByInheritance:(BOOL)newFlag;
{
flags.shouldSortClassesByInheritance = newFlag;
}
- (BOOL)shouldSortMethods;
{
return flags.shouldSortMethods;
}
- (void)setShouldSortMethods:(BOOL)newFlag;
{
flags.shouldSortMethods = newFlag;
}
- (BOOL)shouldShowIvarOffsets;
{
return flags.shouldShowIvarOffsets;
}
- (void)setShouldShowIvarOffsets:(BOOL)newFlag;
{
flags.shouldShowIvarOffsets = newFlag;
}
- (BOOL)shouldShowMethodAddresses;
{
return flags.shouldShowMethodAddresses;
}
- (void)setShouldShowMethodAddresses:(BOOL)newFlag;
{
flags.shouldShowMethodAddresses = newFlag;
}
- (BOOL)shouldMatchRegex;
{
return flags.shouldMatchRegex;
}
- (void)setShouldMatchRegex:(BOOL)newFlag;
{
if (flags.shouldMatchRegex && newFlag == NO)
regfree(&compiledRegex);
flags.shouldMatchRegex = newFlag;
}
- (BOOL)shouldShowHeader;
{
return flags.shouldShowHeader;
}
- (void)setShouldShowHeader:(BOOL)newFlag;
{
flags.shouldShowHeader = newFlag;
}
- (BOOL)setRegex:(char *)regexCString errorMessage:(NSString **)errorMessagePointer;
{
int result;
if (flags.shouldMatchRegex)
regfree(&compiledRegex);
result = regcomp(&compiledRegex, regexCString, REG_EXTENDED);
if (result != 0) {
char regex_error_buffer[256];
if (regerror(result, &compiledRegex, regex_error_buffer, 256) > 0) {
if (errorMessagePointer != NULL) {
*errorMessagePointer = [NSString stringWithUTF8String:regex_error_buffer];
}
} else {
if (errorMessagePointer != NULL)
*errorMessagePointer = nil;
}
return NO;
}
[self setShouldMatchRegex:YES];
return YES;
}
- (BOOL)regexMatchesString:(NSString *)aString;
{
int result;
result = regexec(&compiledRegex, [aString UTF8String], 0, NULL, 0);
if (result != 0) {
if (result != REG_NOMATCH) {
char regex_error_buffer[256];
if (regerror(result, &compiledRegex, regex_error_buffer, 256) > 0)
NSLog(@"Error with regex matching string, %@", [NSString stringWithUTF8String:regex_error_buffer]);
}
return NO;
}
return YES;
}
- (NSArray *)machOFiles;
{
return machOFiles;
}
- (NSArray *)objcProcessors;
{
return objcProcessors;
}
- (CDArch)targetArch;
{
return targetArch;
}
- (void)setTargetArch:(CDArch)newArch;
{
targetArch = newArch;
}
- (BOOL)containsObjectiveCData;
{
for (CDObjectiveC1Processor *processor in objcProcessors) {
if ([processor hasObjectiveCData])
return YES;
}
return NO;
}
- (CDStructureTable *)structureTable;
{
return structureTable;
}
- (CDStructureTable *)unionTable;
{
return unionTable;
}
- (CDTypeFormatter *)ivarTypeFormatter;
{
return ivarTypeFormatter;
}
- (CDTypeFormatter *)methodTypeFormatter;
{
return methodTypeFormatter;
}
- (CDTypeFormatter *)propertyTypeFormatter;
{
return propertyTypeFormatter;
}
- (CDTypeFormatter *)structDeclarationTypeFormatter;
{
return structDeclarationTypeFormatter;
}
// Return YES if successful, NO if there was an error.
- (BOOL)_loadFilename:(NSString *)aFilename;
{
NSData *data;
CDFile *aFile;
data = [[NSData alloc] initWithContentsOfMappedFile:aFilename];
aFile = [CDFile fileWithData:data];
[aFile setFilename:aFilename];
[data release];
if (aFile == nil)
return NO;
return [self loadFile:aFile];
}
- (BOOL)loadFile:(CDFile *)aFile;
{
CDMachOFile *aMachOFile;
//NSLog(@"targetArch: (%08x, %08x)", targetArch.cputype, targetArch.cpusubtype);
aMachOFile = [aFile machOFileWithArch:targetArch];
//NSLog(@"aMachOFile: %@", aMachOFile);
if (aMachOFile == nil) {
fprintf(stderr, "Error: file doesn't contain the specified arch.\n\n");
return NO;
}
if ([self shouldProcessRecursively]) {
@try {
for (CDLoadCommand *loadCommand in [aMachOFile loadCommands]) {
if ([loadCommand isKindOfClass:[CDLCDylib class]]) {
CDLCDylib *aDylibCommand;
aDylibCommand = (CDLCDylib *)loadCommand;
if ([aDylibCommand cmd] == LC_LOAD_DYLIB)
[self machOFileWithID:[aDylibCommand name]]; // Loads as a side effect
}
}
}
@catch (NSException *exception) {
[aMachOFile release];
return NO;
}
}
assert([aMachOFile filename] != nil);
[machOFiles addObject:aMachOFile];
[machOFilesByID setObject:aMachOFile forKey:[aMachOFile filename]];
return YES;
}
- (void)processObjectiveCData;
{
for (CDMachOFile *machOFile in machOFiles) {
CDObjectiveC1Processor *aProcessor;
aProcessor = [[[machOFile processorClass] alloc] initWithMachOFile:machOFile];
[aProcessor process];
[objcProcessors addObject:aProcessor];
[aProcessor release];
}
}
// This visits everything segment processors, classes, categories. It skips over modules. Need something to visit modules so we can generate separate headers.
- (void)recursivelyVisit:(CDVisitor *)aVisitor;
{
[aVisitor willBeginVisiting];
if ([self containsObjectiveCData]) {
for (CDObjectiveCProcessor *processor in objcProcessors) {
[processor recursivelyVisit:aVisitor];
}
}
[aVisitor didEndVisiting];
}
- (void)registerStuff;
{
[self registerPhase:1];
[self registerPhase:2];
[self generateMemberNames];
}
- (void)logInfo;
{
[structureTable logInfo];
[unionTable logInfo];
}
- (void)appendStructuresToString:(NSMutableString *)resultString symbolReferences:(CDSymbolReferences *)symbolReferences;
{
[structureTable appendNamedStructuresToString:resultString classDump:self formatter:structDeclarationTypeFormatter symbolReferences:symbolReferences];
[structureTable appendTypedefsToString:resultString classDump:self formatter:structDeclarationTypeFormatter symbolReferences:symbolReferences];
[unionTable appendNamedStructuresToString:resultString classDump:self formatter:structDeclarationTypeFormatter symbolReferences:symbolReferences];
[unionTable appendTypedefsToString:resultString classDump:self formatter:structDeclarationTypeFormatter symbolReferences:symbolReferences];
}
- (CDMachOFile *)machOFileWithID:(NSString *)anID;
{
NSString *adjustedID;
CDMachOFile *aMachOFile;
NSString *replacementString = @"@executable_path";
if ([anID hasPrefix:replacementString]) {
adjustedID = [executablePath stringByAppendingString:[anID substringFromIndex:[replacementString length]]];
} else {
adjustedID = anID;
}
aMachOFile = [machOFilesByID objectForKey:adjustedID];
if (aMachOFile == nil) {
[self _loadFilename:adjustedID];
aMachOFile = [machOFilesByID objectForKey:adjustedID];
}
return aMachOFile;
}
- (void)appendHeaderToString:(NSMutableString *)resultString;
{
// Since this changes each version, for regression testing it'll be better to be able to not show it.
if (flags.shouldShowHeader == NO)
return;
[resultString appendString:@"/*\n"];
[resultString appendFormat:@" * Generated by class-dump %s.\n", CLASS_DUMP_VERSION];
[resultString appendString:@" *\n"];
[resultString appendString:@" * class-dump is Copyright (C) 1997-1998, 2000-2001, 2004-2009 by Steve Nygard.\n"];
[resultString appendString:@" */\n\n"];
}
- (CDType *)typeFormatter:(CDTypeFormatter *)aFormatter replacementForType:(CDType *)aType;
{
if ([aType type] == '{')
return [structureTable replacementForType:aType];
if ([aType type] == '(')
return [unionTable replacementForType:aType];
return nil;
}
- (NSString *)typeFormatter:(CDTypeFormatter *)aFormatter typedefNameForStruct:(CDType *)structType level:(int)level;
{
CDType *searchType;
CDStructureTable *targetTable;
if (level == 0 && aFormatter == structDeclarationTypeFormatter)
return nil;
if ([structType type] == '{') {
targetTable = structureTable;
} else {
targetTable = unionTable;
}
// We need to catch top level replacements, not just replacements for struct members.
searchType = [targetTable replacementForType:structType];
if (searchType == nil)
searchType = structType;
return [targetTable typedefNameForStructureType:searchType];
}
- (void)registerPhase:(int)phase;
{
NSAutoreleasePool *pool;
pool = [[NSAutoreleasePool alloc] init];
for (CDObjectiveC1Processor *processor in objcProcessors) {
[processor registerStructuresWithObject:self phase:phase];
}
[self endPhase:phase];
[pool release];
}
- (void)endPhase:(int)phase;
{
if (phase == 1) {
[structureTable finishPhase1];
[unionTable finishPhase1];
} else if (phase == 2) {
[structureTable generateNamesForAnonymousStructures];
[unionTable generateNamesForAnonymousStructures];
}
}
- (void)phase1RegisterStructure:(CDType *)aStructure;
{
if ([aStructure type] == '{') {
[structureTable phase1RegisterStructure:aStructure];
} else if ([aStructure type] == '(') {
[unionTable phase1RegisterStructure:aStructure];
} else {
NSLog(@"%s, unknown structure type: %d", _cmd, [aStructure type]);
}
}
- (BOOL)phase2RegisterStructure:(CDType *)aStructure usedInMethod:(BOOL)isUsedInMethod countReferences:(BOOL)shouldCountReferences;
{
if ([aStructure type] == '{') {
return [structureTable phase2RegisterStructure:aStructure withObject:self usedInMethod:isUsedInMethod countReferences:shouldCountReferences];
} else if ([aStructure type] == '(') {
return [unionTable phase2RegisterStructure:aStructure withObject:self usedInMethod:isUsedInMethod countReferences:shouldCountReferences];
} else {
NSLog(@"%s, unknown structure type: %d", _cmd, [aStructure type]);
}
return NO;
}
- (void)generateMemberNames;
{
[structureTable generateMemberNames];
[unionTable generateMemberNames];
}
- (void)showHeader;
{
if ([machOFiles count] > 0) {
[[[machOFiles lastObject] headerString:YES] print];
}
}
- (void)showLoadCommands;
{
if ([machOFiles count] > 0) {
[[[machOFiles lastObject] loadCommandString:YES] print];
}
}
@end