-
Notifications
You must be signed in to change notification settings - Fork 5
/
ImageHeader.m
317 lines (252 loc) · 7.16 KB
/
ImageHeader.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
@implementation ImageHeader
-(instancetype)init;
{
// TODO: some Output operations use addCommand: without reloading pointers
// which causes crashes when the NSMutableData backing gets enlarged
// preallocating should prevent this, but it's ugly and not explicitly documented
self.data=[NSMutableData dataWithCapacity:0x10000];
return self;
}
-(instancetype)initWithPointer:(char*)pointer
{
self=self.init;
int size=sizeof(struct mach_header_64)+((struct mach_header_64*)pointer)->sizeofcmds;
[self.data appendBytes:pointer length:size];
return self;
}
-(instancetype)initEmpty
{
self=self.init;
[self.data increaseLengthBy:sizeof(struct mach_header_64)];
// TODO: check
self.header->magic=MH_MAGIC_64;
self.header->cputype=CPU_TYPE_X86_64;
self.header->cpusubtype=CPU_SUBTYPE_X86_64_ALL;
self.header->filetype=MH_DYLIB;
return self;
}
-(struct mach_header_64*)header
{
return (struct mach_header_64*)self.data.mutableBytes;
}
-(void)forEachCommand:(void (^)(struct load_command*))block
{
struct load_command* command=(struct load_command*)(self.header+1);
for(int commandIndex=0;commandIndex<self.header->ncmds;commandIndex++)
{
block(command);
command=(struct load_command*)(((char*)command)+command->cmdsize);
}
}
-(struct load_command*)commandWithType:(int)type
{
__block struct load_command* result=NULL;
[self forEachCommand:^(struct load_command* command)
{
if(command->cmd==type)
{
assert(!result);
result=command;
}
}];
return result;
}
-(void)forEachSegmentCommand:(void (^)(struct segment_command_64*))block
{
[self forEachCommand:^(struct load_command* command)
{
if(command->cmd==LC_SEGMENT_64)
{
struct segment_command_64* segment=(struct segment_command_64*)command;
block(segment);
}
}];
}
-(struct segment_command_64*)segmentCommandWithName:(char*)name
{
__block struct segment_command_64* output=NULL;
[self forEachSegmentCommand:^(struct segment_command_64* command)
{
int length=MIN(16,MAX(strlen(command->segname),strlen(name)));
if(!strncmp(command->segname,name,length))
{
assert(!output);
output=command;
}
}];
return output;
}
// TODO: slow and ugly, may be worth abstracting Segment and creating a map
-(struct segment_command_64*)segmentCommandWithAddress:(long)address indexOut:(int*)indexOut
{
__block struct segment_command_64* result=NULL;
__block int index=0;
[self forEachSegmentCommand:^(struct segment_command_64* command)
{
if(address>=command->vmaddr&&address<command->vmaddr+command->vmsize)
{
if(result)
{
trace(@"multiple segments cover address %lx",address);
self.dumpSegments;
abort();
}
result=command;
if(indexOut)
{
*indexOut=index;
}
}
index++;
}];
return result;
}
-(struct segment_command_64*)segmentCommandWithOffset:(long)offset indexOut:(int*)indexOut
{
__block struct segment_command_64* result=NULL;
__block int index=0;
[self forEachSegmentCommand:^(struct segment_command_64* command)
{
if(offset>=command->fileoff&&offset<command->fileoff+command->filesize)
{
if(result)
{
trace(@"multiple segments cover offset %lx",offset);
self.dumpSegments;
abort();
}
result=command;
if(indexOut)
{
*indexOut=index;
}
}
index++;
}];
return result;
}
-(void)forEachSectionCommand:(void (^)(struct segment_command_64*,struct section_64*))block
{
[self forEachSegmentCommand:^(struct segment_command_64* command)
{
struct section_64* sections=(struct section_64*)(command+1);
for(int index=0;index<command->nsects;index++)
{
block(command,§ions[index]);
}
}];
}
-(struct section_64*)sectionCommandWithName:(char*)name
{
__block struct section_64* output=NULL;
[self forEachSectionCommand:^(struct segment_command_64* segment,struct section_64* command)
{
// hack to avoid matching subsets or overrunning into segname
int length=MIN(16,MAX(strlen(command->sectname),strlen(name)));
if(!strncmp(command->sectname,name,length))
{
assert(!output);
output=command;
}
}];
return output;
}
-(void)addCommand:(struct load_command*)command
{
// TODO: confirm Ventura requires this, and pad instead of crashing
assert(command->cmdsize%8==0);
[self.data appendBytes:(char*)command length:command->cmdsize];
self.header->ncmds++;
self.header->sizeofcmds+=command->cmdsize;
}
// TODO: everything below here is a bit weird, refactor? move to other classes?
-(NSArray<NSString*>*)dylibPathsReexportOnly:(BOOL)reexportOnly
{
NSMutableArray* result=NSMutableArray.alloc.init.autorelease;
[self forEachCommand:^(struct load_command* command)
{
if(command->cmd==LC_LOAD_DYLIB||command->cmd==LC_LOAD_WEAK_DYLIB||command->cmd==LC_LOAD_UPWARD_DYLIB||command->cmd==LC_REEXPORT_DYLIB)
{
if(!reexportOnly||command->cmd==LC_REEXPORT_DYLIB)
{
int nameOffset=((struct dylib_command*)command)->dylib.name.offset;
NSString* name=[NSString stringWithUTF8String:(char*)command+nameOffset];
[result addObject:name];
}
}
}];
return result;
}
-(NSArray<NSString*>*)dylibPaths
{
// order matters here (bind ordinal)
if(!self.fastShallowPaths)
{
self.fastShallowPaths=[self dylibPathsReexportOnly:false];
}
return self.fastShallowPaths;
}
-(NSSet<NSString*>*)reexportedDylibPathsRecursiveWithCache:(CacheSet*)cache
{
if(!self.fastRecursivePaths)
{
// order doesn't matter here, since we're getting children of one import
NSArray<NSString*>* reexports=[self dylibPathsReexportOnly:true];
NSMutableSet* result=NSMutableSet.alloc.init.autorelease;
[result addObjectsFromArray:reexports];
for(NSString* path in reexports)
{
CacheImage* image=[cache imageWithPath:path];
assert(image);
[result unionSet:[image.header reexportedDylibPathsRecursiveWithCache:cache]];
}
self.fastRecursivePaths=result;
}
return self.fastRecursivePaths;
}
-(int)ordinalWithDylibPath:(NSString*)target cache:(CacheSet*)cache symbol:(NSString*)symbol newSymbolOut:(NSString**)newSymbolOut
{
NSArray<NSString*>* shallowPaths=self.dylibPaths;
long shallowFound=[shallowPaths indexOfObject:target];
if(shallowFound!=NSNotFound)
{
return shallowFound+1;
}
for(int index=0;index<shallowPaths.count;index++)
{
CacheImage* shallowImage=[cache imageWithPath:shallowPaths[index]];
assert(shallowImage);
NSSet<NSString*>* deepPaths=[shallowImage.header reexportedDylibPathsRecursiveWithCache:cache];
if([deepPaths containsObject:target])
{
return index+1;
}
// TODO: check if symbol actually comes from the dylib we originally matched
// name collisions are theoretically possible...
Address* reexport=[shallowImage reexportWithName:symbol];
if(reexport)
{
*newSymbolOut=reexport.name;
return index+1;
}
for(NSString* deepPath in deepPaths)
{
CacheImage* deepImage=[cache imageWithPath:deepPath];
Address* reexport=[deepImage reexportWithName:symbol];
if(reexport)
{
*newSymbolOut=reexport.name;
return index+1;
}
}
}
return -1;
}
-(void)dumpSegments
{
[self forEachSegmentCommand:^(struct segment_command_64* seg)
{
trace(@"segment %s address %lx offset %lx memory size %lx file size %lx",seg->segname,seg->vmaddr,seg->fileoff,seg->vmsize,seg->filesize);
}];
}
@end