This repository was archived by the owner on Oct 9, 2024. It is now read-only.
forked from Polidea/ios-class-guard
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSDKSymbolExtractor.m
More file actions
161 lines (141 loc) · 7.16 KB
/
Copy pathSDKSymbolExtractor.m
File metadata and controls
161 lines (141 loc) · 7.16 KB
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
#import "SDKSymbolExtractor.h"
#import "CDMachOFile.h"
#import "CDLoadCommand.h"
#import "CDLCDylib.h"
@interface SDKSymbolExtractor ()
@property (strong, nonatomic, readwrite) CDSearchPathState *searchPathState;
@property (strong, nonatomic, readwrite) NSMutableDictionary *machOFilesByName;
@end
@implementation SDKSymbolExtractor
- (instancetype)init {
if (self = [super init]) {
_searchPathState = [[CDSearchPathState alloc] init];
_machOFilesByName = [[NSMutableDictionary alloc] init];
_symbols = [[NSMutableSet alloc] init];
}
return self;
}
- (void)collectSymbols {
for (CDFile *file in self.machOFilesByName.allValues) {
if (![file.filename containsString:@"UserTesting"] && ![file.filename containsString:@"AppCapture"]) {
[self collectSymbolsFromExecutableAtPath:file.filename];
}
}
}
- (void)collectSymbolsFromExecutableAtPath:(NSString *)path {
NSTask *otool = [[NSTask alloc] init];
[otool setLaunchPath:@"/usr/bin/otool"];
[otool setArguments:@[@"-o", path]];
NSPipe *fromOtoolToSed = [NSPipe pipe];
[otool setStandardOutput:fromOtoolToSed];
[otool launch];
NSFileHandle *sedOutput = [fromOtoolToSed fileHandleForReading];
NSData *data = [sedOutput readDataToEndOfFile];
if (data.length) {
NSString *output = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSError *error;
NSRegularExpression *regex = [[NSRegularExpression alloc] initWithPattern:@"\\s*name\\s\\dx\\S*\\s(.*)" options:0 error:&error];
[regex enumerateMatchesInString:output
options:0
range:NSMakeRange(0, [output length])
usingBlock:^(NSTextCheckingResult * _Nullable result, NSMatchingFlags flags, BOOL * _Nonnull stop) {
[self.symbols addObjectsFromArray:[[output substringWithRange:[result rangeAtIndex:1]] componentsSeparatedByString:@":"]];
}];
}
}
- (BOOL)loadFile:(CDFile *)file error:(NSError **)error depth:(int)depth {
CDMachOFile *machOFile = [file machOFileWithArch:_targetArch];
// if (machOFile == nil) {
// if (error != NULL) {
// NSString *failureReason;
// NSString *targetArchName = CDNameForCPUType(_targetArch.cputype, _targetArch.cpusubtype);
// if ([file isKindOfClass:[CDFatFile class]] && [(CDFatFile *)file containsArchitecture:_targetArch]) {
// failureReason = [NSString stringWithFormat:@"Fat file doesn't contain a valid Mach-O file for the specified architecture (%@). "
// "It probably means that class-dump was run on a static library, which is not supported.", targetArchName];
// } else {
// failureReason = [NSString stringWithFormat:@"File doesn't contain the specified architecture (%@). Available architectures are %@.", targetArchName, file.architectureNameDescription];
// }
// NSDictionary *userInfo = @{ NSLocalizedFailureReasonErrorKey : failureReason };
// *error = [NSError errorWithDomain:CDErrorDomain_ClassDump code:0 userInfo:userInfo];
// }
// return NO;
// }
// Set before processing recursively. This was getting caught on CoreUI on 10.6
assert([machOFile filename] != nil);
// [_machOFiles addObject:machOFile];
_machOFilesByName[machOFile.filename] = machOFile;
// BOOL shouldProcessRecursively = [self shouldProcessRecursively] && depth < _maxRecursiveDepth;
// if(!shouldProcessRecursively && [self.forceRecursiveAnalyze containsObject:machOFile.importBaseName]) {
// shouldProcessRecursively = YES;
// NSLog(@"Forced recursively processing of %@", machOFile.importBaseName);
// }
// if (shouldProcessRecursively) {
@try {
for (CDLoadCommand *loadCommand in [machOFile loadCommands]) {
if ([loadCommand isKindOfClass:[CDLCDylib class]]) {
CDLCDylib *dylibCommand = (CDLCDylib *)loadCommand;
if ([dylibCommand cmd] == LC_LOAD_DYLIB) {
[self.searchPathState pushSearchPaths:[machOFile runPaths]];
{
NSString *loaderPathPrefix = @"@loader_path";
NSString *path = [dylibCommand path];
if ([path hasPrefix:loaderPathPrefix]) {
NSString *loaderPath = [machOFile.filename stringByDeletingLastPathComponent];
path = [[path stringByReplacingOccurrencesOfString:loaderPathPrefix withString:loaderPath] stringByStandardizingPath];
}
[self machOFileWithName:path andDepth:depth+1]; // Loads as a side effect
}
[self.searchPathState popSearchPaths];
}
}
}
}
@catch (NSException *exception) {
NSLog(@"Caught exception: %@", exception);
if (error != NULL) {
// NSDictionary *userInfo = @{
// NSLocalizedFailureReasonErrorKey : @"Caught exception",
// CDErrorKey_Exception : exception,
// };
// *error = [NSError errorWithDomain:CDErrorDomain_ClassDump code:0 userInfo:userInfo];
}
return NO;
}
// }
return YES;
}
- (CDMachOFile *)machOFileWithName:(NSString *)name andDepth:(int)depth {
NSString *adjustedName = nil;
NSString *executablePathPrefix = @"@executable_path";
NSString *rpathPrefix = @"@rpath";
if ([name hasPrefix:executablePathPrefix]) {
adjustedName = [name stringByReplacingOccurrencesOfString:executablePathPrefix withString:self.searchPathState.executablePath];
} else if ([name hasPrefix:rpathPrefix]) {
for (NSString *searchPath in [self.searchPathState searchPaths]) {
NSString *str = [name stringByReplacingOccurrencesOfString:rpathPrefix withString:searchPath];
if ([[NSFileManager defaultManager] fileExistsAtPath:str]) {
adjustedName = str;
break;
}
}
if (adjustedName == nil) {
adjustedName = name;
}
} else if (self.sdkRoot != nil) {
adjustedName = [self.sdkRoot stringByAppendingPathComponent:name];
} else {
adjustedName = name;
}
CDMachOFile *machOFile = _machOFilesByName[adjustedName];
if (machOFile == nil) {
CDFile *file = [CDFile fileWithContentsOfFile:adjustedName searchPathState:self.searchPathState];
if (file == nil || [self loadFile:file error:NULL depth:depth] == NO)
NSLog(@"Warning: Failed to load: %@", adjustedName);
machOFile = _machOFilesByName[adjustedName];
if (machOFile == nil) {
NSLog(@"Warning: Couldn't load MachOFile with ID: %@, adjustedID: %@", name, adjustedName);
}
}
return machOFile;
}
@end