-
Notifications
You must be signed in to change notification settings - Fork 5
/
CacheFile.m
273 lines (221 loc) · 6.28 KB
/
CacheFile.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
@implementation CacheFile
-(instancetype)initWithPath:(NSString*)path
{
self=super.init;
self.data=[NSMutableData dataWithContentsOfFile:path];
if(!self.data)
{
return nil;
}
NSMutableArray<CacheImage*>* images=NSMutableArray.alloc.init.autorelease;
struct dyld_cache_image_info* infos=(struct dyld_cache_image_info*)wrapOffset(self,self.header->imagesOffset).pointer;
for(int index=0;index<self.header->imagesCount;index++)
{
CacheImage* image=[CacheImage.alloc initWithCacheFile:self info:&infos[index]].autorelease;
if(image)
{
[images addObject:image];
}
}
self.images=images;
NSMutableDictionary<NSNumber*,NSMutableArray<NSNumber*>*>* rebasesByChunk=NSMutableDictionary.alloc.init.autorelease;
[self forEachMapping:^(struct dyld_cache_mapping_and_slide_info* mapping)
{
if(mapping->slideInfoFileOffset)
{
dyld_cache_slide_info2* slide=(struct dyld_cache_slide_info2*)wrapOffset(self,mapping->slideInfoFileOffset).pointer;
assert(slide->version==2);
// dyld_cache_format.h
unsigned long valueMask=~(slide->delta_mask);
int deltaShift=__builtin_ctzll(slide->delta_mask)-2;
short* starts=(short*)((char*)slide+slide->page_starts_offset);
for(long pageIndex=0;pageIndex<slide->page_starts_count;pageIndex++)
{
if(starts[pageIndex]==DYLD_CACHE_SLIDE_PAGE_ATTR_NO_REBASE)
{
continue;
}
assert((starts[pageIndex]&DYLD_CACHE_SLIDE_PAGE_ATTR_EXTRA)==0);
unsigned long startAddress=mapping->address+pageIndex*slide->page_size+starts[pageIndex]*4;
char* pointer=wrapAddress(self,startAddress).pointer;
int delta=1;
while(delta)
{
unsigned long* valuePointer=(unsigned long*)pointer;
delta=(*valuePointer&slide->delta_mask)>>deltaShift;
unsigned long value=(*valuePointer&valueMask)+slide->value_add;
*valuePointer=value;
unsigned long address=wrapPointer(self,pointer).address;
NSNumber* chunk=[NSNumber numberWithLong:address/FAST_CHUNK_SIZE];
if(!rebasesByChunk[chunk])
{
rebasesByChunk[chunk]=NSMutableArray.alloc.init.autorelease;
}
[rebasesByChunk[chunk] addObject:[NSNumber numberWithLong:address]];
pointer+=delta;
}
}
}
}];
self.fastRebasesByChunk=rebasesByChunk;
NSMutableDictionary<NSNumber*,NSMutableArray<CacheImage*>*>* imagesByChunk=NSMutableDictionary.alloc.init.autorelease;
for(CacheImage* image in self.images)
{
NSArray<NSNumber*>* chunks=[image enclosingChunksWithSize:FAST_CHUNK_SIZE];
for(NSNumber* chunk in chunks)
{
if(!imagesByChunk[chunk])
{
imagesByChunk[chunk]=NSMutableArray.alloc.init.autorelease;
}
[imagesByChunk[chunk] addObject:image];
}
}
self.fastImagesByChunk=imagesByChunk;
NSMutableDictionary<NSString*,CacheImage*>* imagesByPath=NSMutableDictionary.alloc.init.autorelease;
for(CacheImage* image in self.images)
{
imagesByPath[image.path]=image;
}
self.fastImagesByPath=imagesByPath;
return self;
}
-(long)maxConstDataMappingAddress
{
__block long max=0;
[self forEachMapping:^(struct dyld_cache_mapping_and_slide_info* info)
{
if(info->flags==DYLD_CACHE_MAPPING_CONST_DATA)
{
assert(max==0);
max=info->address+info->size;
}
}];
assert(max!=0);
return max;
}
-(long)maxConstDataSegmentAddress
{
long mappingEnd=self.maxConstDataMappingAddress;
__block long max=0;
for(CacheImage* image in self.images)
{
[image.header forEachSegmentCommand:^(struct segment_command_64* command)
{
long end=command->vmaddr+command->vmsize;
if(end<mappingEnd)
{
max=MAX(max,end);
}
}];
}
assert(max!=0);
return max;
}
-(struct dyld_cache_header*)header
{
return (struct dyld_cache_header*)self.data.bytes;
}
-(void)forEachMapping:(void (^)(struct dyld_cache_mapping_and_slide_info*))block
{
// using Location here would create a circular dependency
struct dyld_cache_mapping_and_slide_info* infos=(struct dyld_cache_mapping_and_slide_info*)((char*)self.data.mutableBytes+self.header->mappingWithSlideOffset);
for(int index=0;index<self.header->mappingWithSlideCount;index++)
{
block(&infos[index]);
}
}
-(long)addressWithOffset:(long)offset
{
__block dyld_cache_mapping_and_slide_info* info=NULL;
[self forEachMapping:^(struct dyld_cache_mapping_and_slide_info* mapping)
{
if(offset>=mapping->fileOffset&&offset<mapping->fileOffset+mapping->size)
{
assert(!info);
info=mapping;
}
}];
if(!info)
{
return -1;
}
return info->address+offset-info->fileOffset;
}
-(long)addressWithPointer:(char*)pointer
{
return [self addressWithOffset:pointer-(char*)self.data.mutableBytes];
}
-(long)offsetWithAddress:(long)address
{
__block dyld_cache_mapping_and_slide_info* info=NULL;
[self forEachMapping:^(struct dyld_cache_mapping_and_slide_info* mapping)
{
if(address>=mapping->address&&address<mapping->address+mapping->size)
{
assert(!info);
info=mapping;
}
}];
if(!info)
{
return -1;
}
return info->fileOffset+address-info->address;
}
-(char*)pointerWithAddress:(long)address
{
long offset=[self offsetWithAddress:address];
if(offset==-1)
{
return NULL;
}
return (char*)self.data.mutableBytes+offset;
}
-(CacheImage*)imageWithPath:(NSString*)path
{
return self.fastImagesByPath[path];
}
-(NSArray<CacheImage*>*)imagesWithPathPrefix:(NSString*)path
{
NSMutableArray<CacheImage*>* result=NSMutableArray.alloc.init.autorelease;
for(CacheImage* image in self.images)
{
if([image.path hasPrefix:path])
{
[result addObject:image];
}
}
return result;
}
-(CacheImage*)imageWithAddress:(long)address
{
NSNumber* chunk=[NSNumber numberWithLong:address/FAST_CHUNK_SIZE];
NSArray<CacheImage*>* candidates=self.fastImagesByChunk[chunk];
for(CacheImage* image in candidates)
{
if([image.header segmentCommandWithAddress:address indexOut:NULL])
{
return image;
}
}
return nil;
}
-(NSArray<NSNumber*>*)rebasesWithStartAddress:(long)start endAddress:(long)end
{
NSMutableArray<NSNumber*>* result=NSMutableArray.alloc.init.autorelease;
long startChunk=start/FAST_CHUNK_SIZE;
long endChunk=(end-1)/FAST_CHUNK_SIZE;
for(long chunk=startChunk;chunk<=endChunk;chunk++)
{
for(NSNumber* rebase in self.fastRebasesByChunk[[NSNumber numberWithLong:chunk]])
{
if(rebase.longValue>=start&&rebase.longValue<end)
{
[result addObject:rebase];
}
}
}
return result;
}
@end