-
Notifications
You must be signed in to change notification settings - Fork 701
/
FatLayout.mm
188 lines (162 loc) · 10.2 KB
/
FatLayout.mm
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
/*
* FatLayout.mm
* MachOView
*
* Created by psaghelyi on 02/12/2011.
*
*/
#include <string>
#include <vector>
#include <set>
#include <map>
#include <cxxabi.h>
#import "Common.h"
#import "FatLayout.h"
#import "DataController.h"
#import "MachOLayout.h"
#import "ReadWrite.h"
#import <mach-o/fat.h>
#import <mach-o/swap.h>
using namespace std;
//============================================================================
@implementation FatLayout
//-----------------------------------------------------------------------------
- (instancetype)initWithDataController:(MVDataController *)dc rootNode:(MVNode *)node
{
if (self = [super initWithDataController:dc rootNode:node])
{
//further initialisations
}
return self;
}
//-----------------------------------------------------------------------------
+ (FatLayout *)layoutWithDataController:(MVDataController *)dc rootNode:(MVNode *)node
{
return [[FatLayout alloc] initWithDataController:dc rootNode:node];
}
//----------------------------------------------------------------------------
- (MVNode *)createHeaderNode:(MVNode *)parent
caption:(NSString *)caption
location:(uint64_t)location
fat_header:(struct fat_header const *)fat_header
{
MVNodeSaver nodeSaver;
MVNode * node = [parent insertChildWithDetails:caption
location:location
length:sizeof(struct fat_header) + fat_header->nfat_arch * sizeof(struct fat_arch)
saver:nodeSaver];
NSRange range = NSMakeRange(location,0);
NSString * lastReadHex;
uint32_t magic = [dataController read_uint32:range lastReadHex:&lastReadHex];
[node.details appendRow:[NSString stringWithFormat:@"%.8lX", range.location]
:lastReadHex
:@"Magic Number"
:magic == FAT_MAGIC ? @"FAT_MAGIC" :
magic == FAT_CIGAM ? @"FAT_CIGAM" : @"???"];
[node.details setAttributes:MVCellColorAttributeName,[NSColor greenColor],nil];
[dataController read_uint32:range lastReadHex:&lastReadHex];
[node.details appendRow:[NSString stringWithFormat:@"%.8lX", range.location]
:lastReadHex
:@"Number of Architecture"
:[NSString stringWithFormat:@"%u",fat_header->nfat_arch]];
[node.details setAttributes:MVCellColorAttributeName,[NSColor greenColor],
MVUnderlineAttributeName,@"YES",nil];
for (uint32_t nimg = 0; nimg < fat_header->nfat_arch; ++nimg)
{
// need to make copy for byte swapping
struct fat_arch fat_arch;
[dataController.fileData getBytes:&fat_arch range:NSMakeRange(NSMaxRange(range), sizeof(struct fat_arch))];
swap_fat_arch(&fat_arch, 1, NX_LittleEndian);
[dataController read_uint32:range lastReadHex:&lastReadHex];
[node.details appendRow:[NSString stringWithFormat:@"%.8lX", range.location]
:lastReadHex
:@"CPU Type"
:fat_arch.cputype == CPU_TYPE_ANY ? @"CPU_TYPE_ANY" :
fat_arch.cputype == CPU_TYPE_I386 ? @"CPU_TYPE_I386" :
fat_arch.cputype == CPU_TYPE_X86_64 ? @"CPU_TYPE_X86_64" :
fat_arch.cputype == CPU_TYPE_ARM ? @"CPU_TYPE_ARM" :
fat_arch.cputype == CPU_TYPE_ARM64 ? @"CPU_TYPE_ARM64" :
fat_arch.cputype == CPU_TYPE_ARM64_32 ? @"CPU_TYPE_ARM64_32" :
fat_arch.cputype == CPU_TYPE_POWERPC ? @"CPU_TYPE_POWERPC" :
fat_arch.cputype == CPU_TYPE_POWERPC64 ? @"CPU_TYPE_POWERPC64" :
@"???"];
[dataController read_uint32:range lastReadHex:&lastReadHex];
[node.details appendRow:[NSString stringWithFormat:@"%.8lX", range.location]
:lastReadHex
:@"CPU SubType"
:fat_arch.cputype == CPU_TYPE_POWERPC ?
((fat_arch.cpusubtype & ~CPU_SUBTYPE_MASK) == CPU_SUBTYPE_POWERPC_ALL ? @"CPU_SUBTYPE_POWERPC_ALL" :
(fat_arch.cpusubtype & ~CPU_SUBTYPE_MASK) == CPU_SUBTYPE_POWERPC_601 ? @"CPU_SUBTYPE_POWERPC_601" :
(fat_arch.cpusubtype & ~CPU_SUBTYPE_MASK) == CPU_SUBTYPE_POWERPC_602 ? @"CPU_SUBTYPE_POWERPC_602" :
(fat_arch.cpusubtype & ~CPU_SUBTYPE_MASK) == CPU_SUBTYPE_POWERPC_603 ? @"CPU_SUBTYPE_POWERPC_603" :
(fat_arch.cpusubtype & ~CPU_SUBTYPE_MASK) == CPU_SUBTYPE_POWERPC_603e ? @"CPU_SUBTYPE_POWERPC_603e" :
(fat_arch.cpusubtype & ~CPU_SUBTYPE_MASK) == CPU_SUBTYPE_POWERPC_603ev ? @"CPU_SUBTYPE_POWERPC_603ev" :
(fat_arch.cpusubtype & ~CPU_SUBTYPE_MASK) == CPU_SUBTYPE_POWERPC_604 ? @"CPU_SUBTYPE_POWERPC_604" :
(fat_arch.cpusubtype & ~CPU_SUBTYPE_MASK) == CPU_SUBTYPE_POWERPC_604e ? @"CPU_SUBTYPE_POWERPC_604e" :
(fat_arch.cpusubtype & ~CPU_SUBTYPE_MASK) == CPU_SUBTYPE_POWERPC_620 ? @"CPU_SUBTYPE_POWERPC_620" :
(fat_arch.cpusubtype & ~CPU_SUBTYPE_MASK) == CPU_SUBTYPE_POWERPC_750 ? @"CPU_SUBTYPE_POWERPC_750" :
(fat_arch.cpusubtype & ~CPU_SUBTYPE_MASK) == CPU_SUBTYPE_POWERPC_7400 ? @"CPU_SUBTYPE_POWERPC_7400" :
(fat_arch.cpusubtype & ~CPU_SUBTYPE_MASK) == CPU_SUBTYPE_POWERPC_7450 ? @"CPU_SUBTYPE_POWERPC_7450" :
(fat_arch.cpusubtype & ~CPU_SUBTYPE_MASK) == CPU_SUBTYPE_POWERPC_970 ? @"CPU_SUBTYPE_POWERPC_970" : @"???") :
fat_arch.cputype == CPU_TYPE_ARM ?
((fat_arch.cpusubtype & ~CPU_SUBTYPE_MASK) == CPU_SUBTYPE_ARM_ALL ? @"CPU_SUBTYPE_ARM_ALL" :
(fat_arch.cpusubtype & ~CPU_SUBTYPE_MASK) == CPU_SUBTYPE_ARM_V4T ? @"CPU_SUBTYPE_ARM_V4T" :
(fat_arch.cpusubtype & ~CPU_SUBTYPE_MASK) == CPU_SUBTYPE_ARM_V6 ? @"CPU_SUBTYPE_ARM_V6" :
(fat_arch.cpusubtype & ~CPU_SUBTYPE_MASK) == CPU_SUBTYPE_ARM_V5TEJ ? @"CPU_SUBTYPE_ARM_V5TEJ" :
(fat_arch.cpusubtype & ~CPU_SUBTYPE_MASK) == CPU_SUBTYPE_ARM_XSCALE ? @"CPU_SUBTYPE_ARM_XSCALE" :
(fat_arch.cpusubtype & ~CPU_SUBTYPE_MASK) == CPU_SUBTYPE_ARM_V7 ? @"CPU_SUBTYPE_ARM_V7" :
(fat_arch.cpusubtype & ~CPU_SUBTYPE_MASK) == CPU_SUBTYPE_ARM_V7F ? @"CPU_SUBTYPE_ARM_V7F" :
(fat_arch.cpusubtype & ~CPU_SUBTYPE_MASK) == CPU_SUBTYPE_ARM_V7S ? @"CPU_SUBTYPE_ARM_V7S" :
(fat_arch.cpusubtype & ~CPU_SUBTYPE_MASK) == CPU_SUBTYPE_ARM_V7K ? @"CPU_SUBTYPE_ARM_V7K" :
(fat_arch.cpusubtype & ~CPU_SUBTYPE_MASK) == CPU_SUBTYPE_ARM_V6M ? @"CPU_SUBTYPE_ARM_V6M" :
(fat_arch.cpusubtype & ~CPU_SUBTYPE_MASK) == CPU_SUBTYPE_ARM_V7M ? @"CPU_SUBTYPE_ARM_V7M" :
(fat_arch.cpusubtype & ~CPU_SUBTYPE_MASK) == CPU_SUBTYPE_ARM_V7EM ? @"CPU_SUBTYPE_ARM_V7EM" :
(fat_arch.cpusubtype & ~CPU_SUBTYPE_MASK) == CPU_SUBTYPE_ARM_V8 ? @"CPU_SUBTYPE_ARM_V8" :
(fat_arch.cpusubtype & ~CPU_SUBTYPE_MASK) == CPU_SUBTYPE_ARM_V8M ? @"CPU_SUBTYPE_ARM_V8M" : @"???") :
fat_arch.cputype == CPU_TYPE_ARM64 ?
((fat_arch.cpusubtype & ~CPU_SUBTYPE_MASK) == CPU_SUBTYPE_ARM64_ALL ? @"CPU_SUBTYPE_ARM64_ALL" :
(fat_arch.cpusubtype & ~CPU_SUBTYPE_MASK) == CPU_SUBTYPE_ARM64_V8 ? @"CPU_SUBTYPE_ARM64_V8" :
(fat_arch.cpusubtype & ~CPU_SUBTYPE_MASK) == CPU_SUBTYPE_ARM64E ? @"CPU_SUBTYPE_ARM64E" : @"???") :
fat_arch.cputype == CPU_TYPE_ARM64_32 ?
((fat_arch.cpusubtype & ~CPU_SUBTYPE_MASK) == CPU_SUBTYPE_ARM64_32_ALL ? @"CPU_SUBTYPE_ARM64_32_ALL" :
(fat_arch.cpusubtype & ~CPU_SUBTYPE_MASK) == CPU_SUBTYPE_ARM64_32_V8 ? @"CPU_SUBTYPE_ARM64_32_V8" : @"???") :
fat_arch.cputype == CPU_TYPE_I386 ?
((fat_arch.cpusubtype & ~CPU_SUBTYPE_MASK) == CPU_SUBTYPE_I386_ALL ? @"CPU_SUBTYPE_I386_ALL" : @"???") :
fat_arch.cputype == CPU_TYPE_X86_64 ?
((fat_arch.cpusubtype & ~CPU_SUBTYPE_MASK) == CPU_SUBTYPE_X86_64_ALL ? @"CPU_SUBTYPE_X86_64_ALL" : @"???") :
@"???"];
[dataController read_uint32:range lastReadHex:&lastReadHex];
[node.details appendRow:[NSString stringWithFormat:@"%.8lX", range.location]
:lastReadHex
:@"Offset"
:[NSString stringWithFormat:@"%u",fat_arch.offset]];
[dataController read_uint32:range lastReadHex:&lastReadHex];
[node.details appendRow:[NSString stringWithFormat:@"%.8lX", range.location]
:lastReadHex
:@"Size"
:[NSString stringWithFormat:@"%u",fat_arch.size]];
[dataController read_uint32:range lastReadHex:&lastReadHex];
[node.details appendRow:[NSString stringWithFormat:@"%.8lX", range.location]
:lastReadHex
:@"Align"
:[NSString stringWithFormat:@"%u",(1 << fat_arch.align)]];
[node.details setAttributes:MVUnderlineAttributeName,@"YES",nil];
}
return node;
}
//----------------------------------------------------------------------------
- (void)doMainTasks
{
struct fat_header fat_header;
[dataController.fileData getBytes:&fat_header length:sizeof(struct fat_header)];
if (fat_header.magic == FAT_CIGAM)
{
swap_fat_header(&fat_header, NX_LittleEndian);
}
[self createHeaderNode:rootNode
caption:@"Fat Header"
location:imageOffset
fat_header:&fat_header];
[super doMainTasks];
}
@end