-
Notifications
You must be signed in to change notification settings - Fork 28
/
CDFile.m
186 lines (149 loc) · 4.96 KB
/
CDFile.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
// -*- mode: ObjC -*-
// This file is part of class-dump, a utility for examining the Objective-C segment of Mach-O files.
// Copyright (C) 1997-2019 Steve Nygard.
#import "CDFile.h"
#include <mach-o/arch.h>
#import "CDFatFile.h"
#import "CDMachOFile.h"
#import "CDSearchPathState.h"
NSString *CDImportNameForPath(NSString *path)
{
NSString *name = [path lastPathComponent];
// Remove all extensions (make sure extensions like .a.dylib are covered)
NSString *nameWithExtensions = name;
for (NSInteger i = ([nameWithExtensions length] - 1); i >= 0; i--) {
if ([nameWithExtensions characterAtIndex:i] == '.')
name = [name substringToIndex:i];
}
NSString *libPrefix = @"lib";
if ([name hasPrefix:libPrefix])
name = [name substringFromIndex:[libPrefix length]];
return name;
}
NSString *CDNameForCPUType(cpu_type_t cputype, cpu_subtype_t cpusubtype)
{
const NXArchInfo *archInfo = NXGetArchInfoFromCpuType(cputype, cpusubtype);
if (archInfo != NULL)
return [NSString stringWithUTF8String:archInfo->name];
// Special cases until the built-in function recognizes these.
switch (cputype) {
case CPU_TYPE_ARM: {
switch (cpusubtype) {
case 11: return @"armv7s"; // Not recognized in 10.8.4
}
break;
}
case CPU_TYPE_ARM | CPU_ARCH_ABI64: {
switch (cpusubtype) {
case CPU_SUBTYPE_ARM_ALL: return @"arm64"; // Not recognized in 10.8.4
}
break;
}
default: break;
}
return [NSString stringWithFormat:@"0x%x:0x%x", cputype, cpusubtype];
}
CDArch CDArchFromName(NSString *name)
{
CDArch arch;
arch.cputype = CPU_TYPE_ANY;
arch.cpusubtype = 0;
if (name == nil)
return arch;
const NXArchInfo *archInfo = NXGetArchInfoFromName([name UTF8String]);
if (archInfo == NULL) {
if ([name isEqualToString:@"armv7s"]) { // Not recognized in 10.8.4
arch.cputype = CPU_TYPE_ARM;
arch.cpusubtype = 11;
} else if ([name isEqualToString:@"arm64"]) { // Not recognized in 10.8.4
arch.cputype = CPU_TYPE_ARM | CPU_ARCH_ABI64;
arch.cpusubtype = CPU_SUBTYPE_ARM_ALL;
} else {
NSString *ignore;
NSScanner *scanner = [[NSScanner alloc] initWithString:name];
if ([scanner scanHexInt:(uint32_t *)&arch.cputype]
&& [scanner scanString:@":" intoString:&ignore]
&& [scanner scanHexInt:(uint32_t *)&arch.cpusubtype]) {
// Great!
//NSLog(@"scanned 0x%08x : 0x%08x from '%@'", arch.cputype, arch.cpusubtype, name);
} else {
arch.cputype = CPU_TYPE_ANY;
arch.cpusubtype = 0;
}
}
} else {
arch.cputype = archInfo->cputype;
arch.cpusubtype = archInfo->cpusubtype;
}
return arch;
}
BOOL CDArchUses64BitABI(CDArch arch)
{
return (arch.cputype & CPU_ARCH_ABI64) == CPU_ARCH_ABI64;
}
BOOL CDArchUses64BitLibraries(CDArch arch)
{
return (arch.cpusubtype & CPU_SUBTYPE_LIB64) == CPU_SUBTYPE_LIB64;
}
#pragma mark -
@interface CDFile ()
@end
#pragma mark -
@implementation CDFile
{
NSString *_filename;
NSData *_data;
CDSearchPathState *_searchPathState;
}
// Returns CDFatFile or CDMachOFile
+ (id)fileWithContentsOfFile:(NSString *)filename searchPathState:(CDSearchPathState *)searchPathState;
{
NSData *data = [NSData dataWithContentsOfMappedFile:filename];
CDFatFile *fatFile = [[CDFatFile alloc] initWithData:data filename:filename searchPathState:searchPathState];
if (fatFile != nil)
return fatFile;
CDMachOFile *machOFile = [[CDMachOFile alloc] initWithData:data filename:filename searchPathState:searchPathState];
return machOFile;
}
- (id)initWithData:(NSData *)data filename:(NSString *)filename searchPathState:(CDSearchPathState *)searchPathState;
{
if ((self = [super init])) {
// Otherwise reading the magic number fails.
if ([data length] < 4) {
return nil;
}
_filename = filename;
_data = data;
_searchPathState = searchPathState;
}
return self;
}
#pragma mark -
// Return YES on success. If oArchPtr is not NULL, return the best match.
// Return NO on failure, oArchPtr is untouched.
- (BOOL)bestMatchForLocalArch:(CDArch *)oArchPtr;
{
const NXArchInfo *archInfo = NXGetLocalArchInfo();
if (archInfo == NULL)
return NO;
CDArch arch = { archInfo->cputype, archInfo->cpusubtype };
if ([self bestMatchForArch:&arch]) {
if (oArchPtr != NULL)
*oArchPtr = arch;
return YES;
}
return NO;
}
- (BOOL)bestMatchForArch:(CDArch *)ioArchPtr;
{
return NO;
}
- (CDMachOFile *)machOFileWithArch:(CDArch)arch;
{
return nil;
}
- (NSString *)architectureNameDescription;
{
return nil;
}
@end