-
Notifications
You must be signed in to change notification settings - Fork 5
/
CacheSet.m
180 lines (142 loc) · 3.1 KB
/
CacheSet.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
@implementation CacheSet
-(instancetype)initWithPathPrefix:(NSString*)prefix
{
trace(@"read %@",prefix);
NSMutableArray<CacheFile*>* files=NSMutableArray.alloc.init.autorelease;
CacheFile* file=[CacheFile.alloc initWithPath:prefix].autorelease;
if(!file)
{
return nil;
}
[files addObject:file];
// TODO: silly, can probably use dyld_subcache_entry
for(NSString* format in @[@"%@.%d",@"%@.%02d"])
{
for(int index=1;;index++)
{
NSString* path=[NSString stringWithFormat:format,prefix,index];
CacheFile* file=[CacheFile.alloc initWithPath:path].autorelease;
if(!file)
{
break;
}
[files addObject:file];
}
}
if(files.count==0)
{
return nil;
}
self.files=files;
trace(@"os version %d.%d.%d, subcache count %x",self.majorVersion,self.minorVersion,self.subMinorVersion,files.count);
self.findMagicSel;
return self;
}
-(int)majorVersion
{
return self.files[0].header->osVersion/0x10000;
}
-(int)minorVersion
{
return (self.files[0].header->osVersion/0x100)%0x100;
}
-(int)subMinorVersion
{
return self.files[0].header->osVersion%0x100;
}
-(long)addressWithOffset:(long)offset
{
// lacks context of which cache file
abort();
}
-(long)addressWithPointer:(char*)pointer
{
for(CacheFile* file in self.files)
{
Location* location=wrapPointerUnsafe(file,pointer);
if(location)
{
return location.address;
}
}
return -1;
}
-(long)offsetWithAddress:(long)address
{
for(CacheFile* file in self.files)
{
Location* location=wrapAddressUnsafe(file,address);
if(location)
{
return location.offset;
}
}
return -1;
}
-(char*)pointerWithAddress:(long)address
{
for(CacheFile* file in self.files)
{
Location* location=wrapAddressUnsafe(file,address);
if(location)
{
return location.pointer;
}
}
return NULL;
}
-(CacheImage*)imageWithPath:(NSString*)path
{
for(CacheFile* file in self.files)
{
CacheImage* image=[file imageWithPath:path];
if(image)
{
return image;
}
}
return nil;
}
-(NSArray<CacheImage*>*)imagesWithPathPrefix:(NSString*)path
{
NSMutableArray<CacheImage*>* result=NSMutableArray.alloc.init.autorelease;
for(CacheFile* file in self.files)
{
NSArray<CacheImage*>* images=[file imagesWithPathPrefix:path];
[result addObjectsFromArray:images];
}
return result;
}
-(CacheImage*)imageWithAddress:(long)address
{
for(CacheFile* file in self.files)
{
CacheImage* image=[file imageWithAddress:address];
if(image)
{
return image;
}
}
return nil;
}
-(void)findMagicSel
{
CacheImage* image=[self imagesWithPathPrefix:@"/usr/lib/libobjc.A.dylib"].firstObject;
assert(image);
struct section_64* section=[image.header sectionCommandWithName:(char*)"__objc_selrefs"];
assert(section);
long* refs=(long*)wrapOffset(image.file,section->offset).pointer;
int count=section->size/sizeof(long*);
for(int index=0;index<count;index++)
{
char* name=wrapAddress(self,refs[index]).pointer;
if(name&&!strcmp(name,"\xf0\x9f\xa4\xaf"))
{
self.magicSelAddress=refs[index];
trace(@"found magic selector at %lx",self.magicSelAddress);
break;
}
}
assert(self.magicSelAddress);
}
@end