-
Notifications
You must be signed in to change notification settings - Fork 28
/
CDFatFile.m
166 lines (131 loc) · 4.09 KB
/
CDFatFile.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
// -*- 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 "CDFatFile.h"
#include <mach-o/arch.h>
#include <mach-o/fat.h>
#import "CDDataCursor.h"
#import "CDFatArch.h"
#import "CDMachOFile.h"
@implementation CDFatFile
{
NSMutableArray *_arches;
}
- (id)init;
{
if ((self = [super init])) {
_arches = [[NSMutableArray alloc] init];
}
return self;
}
- (id)initWithData:(NSData *)data filename:(NSString *)filename searchPathState:(CDSearchPathState *)searchPathState;
{
if ((self = [super initWithData:data filename:filename searchPathState:searchPathState])) {
CDDataCursor *cursor = [[CDDataCursor alloc] initWithData:data];
struct fat_header header;
header.magic = [cursor readBigInt32];
//NSLog(@"(testing fat) magic: 0x%x", header.magic);
if (header.magic != FAT_MAGIC) {
return nil;
}
_arches = [[NSMutableArray alloc] init];
header.nfat_arch = [cursor readBigInt32];
//NSLog(@"nfat_arch: %u", header.nfat_arch);
for (NSUInteger index = 0; index < header.nfat_arch; index++) {
CDFatArch *arch = [[CDFatArch alloc] initWithDataCursor:cursor];
arch.fatFile = self;
[_arches addObject:arch];
}
}
return self;
}
#pragma mark - Debugging
- (NSString *)description;
{
return [NSString stringWithFormat:@"<%@:%p> %lu arches", NSStringFromClass([self class]), self, [self.arches count]];
}
#pragma mark -
// Case 1: no arch specified
// - check main file for these, then lock down on that arch:
// - local arch, 64 bit
// - local arch, 32 bit
// - any arch, 64 bit
// - any arch, 32 bit
//
// Case 2: you specified a specific arch (i386, x86_64, ppc, ppc7400, ppc64, etc.)
// - only that arch
//
// In either case, we can ignore the cpu subtype
// Returns YES on success, NO on failure.
- (BOOL)bestMatchForArch:(CDArch *)ioArchPtr;
{
cpu_type_t targetType = ioArchPtr->cputype & ~CPU_ARCH_MASK;
// Target architecture, 64 bit
for (CDFatArch *fatArch in self.arches) {
if (fatArch.maskedCPUType == targetType && fatArch.uses64BitABI) {
if (ioArchPtr != NULL) *ioArchPtr = fatArch.arch;
return YES;
}
}
// Target architecture, 32 bit
for (CDFatArch *fatArch in self.arches) {
if (fatArch.maskedCPUType == targetType && fatArch.uses64BitABI == NO) {
if (ioArchPtr != NULL) *ioArchPtr = fatArch.arch;
return YES;
}
}
// Any architecture, 64 bit
for (CDFatArch *fatArch in self.arches) {
if (fatArch.uses64BitABI) {
if (ioArchPtr != NULL) *ioArchPtr = fatArch.arch;
return YES;
}
}
// Any architecture, 32 bit
for (CDFatArch *fatArch in self.arches) {
if (fatArch.uses64BitABI == NO) {
if (ioArchPtr != NULL) *ioArchPtr = fatArch.arch;
return YES;
}
}
// Any architecture
if ([self.arches count] > 0) {
if (ioArchPtr != NULL) *ioArchPtr = [self.arches[0] arch];
return YES;
}
return NO;
}
- (CDFatArch *)fatArchWithArch:(CDArch)cdarch;
{
for (CDFatArch *arch in self.arches) {
if (arch.cputype == cdarch.cputype && arch.maskedCPUSubtype == (cdarch.cpusubtype & ~CPU_SUBTYPE_MASK))
return arch;
}
return nil;
}
- (CDMachOFile *)machOFileWithArch:(CDArch)cdarch;
{
return [[self fatArchWithArch:cdarch] machOFile];
}
- (NSArray *)archNames;
{
NSMutableArray *archNames = [NSMutableArray array];
for (CDFatArch *arch in self.arches)
[archNames addObject:arch.archName];
return archNames;
}
- (NSString *)architectureNameDescription;
{
return [self.archNames componentsJoinedByString:@", "];
}
#pragma mark -
- (void)addArchitecture:(CDFatArch *)fatArch;
{
fatArch.fatFile = self;
[self.arches addObject:fatArch];
}
- (BOOL)containsArchitecture:(CDArch)arch;
{
return [self fatArchWithArch:arch] != nil;
}
@end