forked from nygard/class-dump
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CDFatFile.m
143 lines (113 loc) · 3.7 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
// -*- 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-2012 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
{
NSArray *_arches;
}
- (id)initWithData:(NSData *)data archOffset:(NSUInteger)offset archSize:(NSUInteger)size filename:(NSString *)filename searchPathState:(CDSearchPathState *)searchPathState;
{
if ((self = [super initWithData:data archOffset:offset archSize:size filename:filename searchPathState:searchPathState])) {
CDDataCursor *cursor = [[CDDataCursor alloc] initWithData:data offset:self.archOffset];
struct fat_header header;
header.magic = [cursor readBigInt32];
//NSLog(@"(testing fat) magic: 0x%x", header.magic);
if (header.magic != FAT_MAGIC) {
return nil;
}
NSMutableArray *arches = [[NSMutableArray alloc] init];
header.nfat_arch = [cursor readBigInt32];
//NSLog(@"nfat_arch: %u", header.nfat_arch);
for (unsigned int index = 0; index < header.nfat_arch; index++) {
CDFatArch *arch = [[CDFatArch alloc] initWithDataCursor:cursor];
arch.fatFile = self;
[arches addObject:arch];
}
_arches = [arches copy];
//NSLog(@"arches: %@", _arches);
}
return self;
}
#pragma mark - Debugging
- (NSString *)description;
{
return [NSString stringWithFormat:@"[%p] CDFatFile with %lu arches", 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)bestMatchForLocalArch:(CDArch *)archPtr;
{
const NXArchInfo *archInfo = NXGetLocalArchInfo();
if (archInfo == NULL)
return NO;
if (archPtr == NULL)
return [self.arches count] > 0;
cpu_type_t targetType = archInfo->cputype & ~CPU_ARCH_MASK;
// This architecture, 64 bit
for (CDFatArch *fatArch in self.arches) {
if (fatArch.maskedCPUType == targetType && fatArch.uses64BitABI) {
*archPtr = fatArch.arch;
return YES;
}
}
// This architecture, 32 bit
for (CDFatArch *fatArch in self.arches) {
if (fatArch.maskedCPUType == targetType && fatArch.uses64BitABI == NO) {
*archPtr = fatArch.arch;
return YES;
}
}
// Any architecture, 64 bit
for (CDFatArch *fatArch in self.arches) {
if (fatArch.uses64BitABI) {
*archPtr = fatArch.arch;
return YES;
}
}
// Any architecture, 32 bit
for (CDFatArch *fatArch in self.arches) {
if (fatArch.uses64BitABI == NO) {
*archPtr = fatArch.arch;
return YES;
}
}
// Any architecture
if ([self.arches count] > 0) {
*archPtr = [self.arches[0] arch];
return YES;
}
return NO;
}
- (CDMachOFile *)machOFileWithArch:(CDArch)cdarch;
{
for (CDFatArch *arch in self.arches) {
if (arch.cpuType == cdarch.cputype)
return arch.machOFile;
}
return nil;
}
- (NSArray *)archNames;
{
NSMutableArray *archNames = [NSMutableArray array];
for (CDFatArch *arch in self.arches)
[archNames addObject:arch.archName];
return archNames;
}
@end