Root/
Source at commit 359 created 13 years 7 months ago. By meklort, Used the wrong variable in the rebase function. created a rebase_location function to make the code a little cleaner. | |
---|---|
1 | /*␊ |
2 | * Copyright 2010 Evan Lojewski. All rights reserved.␊ |
3 | *␊ |
4 | */␊ |
5 | ␊ |
6 | #include "boot.h"␊ |
7 | #include "bootstruct.h"␊ |
8 | #include "multiboot.h"␊ |
9 | #include "modules.h"␊ |
10 | ␊ |
11 | ␊ |
12 | moduleList_t* loadedModules = NULL;␊ |
13 | symbolList_t* moduleSymbols = NULL;␊ |
14 | unsigned int (*lookup_symbol)(const char*) = NULL;␊ |
15 | ␊ |
16 | ␊ |
17 | void rebase_macho(void* base, char* rebase_stream, UInt32 size);␊ |
18 | void bind_macho(void* base, char* bind_stream, UInt32 size);␊ |
19 | ␊ |
20 | /*␊ |
21 | * Load a module file in /Extra/modules␊ |
22 | * TODO: verify version number of module␊ |
23 | */␊ |
24 | int load_module(const char* module)␊ |
25 | {␊ |
26 | ␉// Check to see if the module has already been loaded␊ |
27 | ␉if(is_module_laoded(module))␊ |
28 | ␉{␊ |
29 | ␉␉return 1;␊ |
30 | ␉}␊ |
31 | ␉␊ |
32 | ␉char modString[128];␊ |
33 | ␉int fh = -1;␊ |
34 | ␉sprintf(modString, "/Extra/modules/%s.dylib", module);␊ |
35 | ␉fh = open(modString, 0);␊ |
36 | ␉if(fh < 0)␊ |
37 | ␉{␊ |
38 | ␉␉printf("Unable to locate module %s\n", modString);␊ |
39 | ␉␉getc();␊ |
40 | ␉␉return 0;␊ |
41 | ␉}␊ |
42 | ␉␊ |
43 | ␉unsigned int moduleSize = file_size(fh);␊ |
44 | ␉char* module_base = (char*) malloc(moduleSize);␊ |
45 | ␉if (read(fh, module_base, moduleSize) == moduleSize)␊ |
46 | ␉{␊ |
47 | ␉␉void (*module_start)(void) = NULL;␊ |
48 | ␊ |
49 | ␉␉printf("Module %s read in.\n", modString);␊ |
50 | ␊ |
51 | ␉␉// Module loaded into memory, parse it␊ |
52 | ␉␉module_start = parse_mach(module_base);␊ |
53 | ␊ |
54 | ␉␉if(module_start)␊ |
55 | ␉␉{␊ |
56 | ␉␉␉(*module_start)();␉// Start the module␊ |
57 | ␉␉␉printf("Module started\n");␊ |
58 | ␉␉}␊ |
59 | ␉␉else {␊ |
60 | ␉␉␉printf("Unabel to locate module start\n");␊ |
61 | ␉␉}␊ |
62 | ␊ |
63 | ␉␉␊ |
64 | ␉␉getc();␊ |
65 | ␉␉␊ |
66 | ␉␉// TODO: Add module to loaded list if loaded successfuly␊ |
67 | ␉␉␊ |
68 | ␉}␊ |
69 | ␉else␊ |
70 | ␉{␊ |
71 | ␉␉printf("Unable to read in module %s.dylib\n.", module);␊ |
72 | ␉␉getc();␊ |
73 | ␉}␊ |
74 | ␉return 1;␊ |
75 | }␊ |
76 | ␊ |
77 | ␊ |
78 | void* parse_mach(void* binary)␊ |
79 | {␉␊ |
80 | ␉void (*module_start)(void) = NULL;␊ |
81 | ␊ |
82 | ␉// Module info␊ |
83 | ␉char* moduleName = NULL;␊ |
84 | ␉UInt32 moduleVersion = 0;␊ |
85 | ␉UInt32 moduleCompat = 0;␊ |
86 | ␉␊ |
87 | ␉char* symbolStub = NULL;␊ |
88 | ␉char* nonlazy = NULL;␊ |
89 | ␉//char* nonlazy_variables = NULL;␊ |
90 | ␊ |
91 | ␉// TODO convert all of the structs to a union␊ |
92 | ␉struct load_command *loadCommand = NULL;␊ |
93 | ␉struct dylib_command* dylibCommand = NULL;␊ |
94 | ␉struct dyld_info_command* dyldInfoCommand = NULL;␊ |
95 | ␉struct symtab_command* symtabCommand = NULL;␊ |
96 | ␉struct segment_command* segmentCommand = NULL;␊ |
97 | ␉//struct dysymtab_command* dysymtabCommand = NULL;␊ |
98 | ␉struct section* section = NULL;␊ |
99 | ␉UInt32 binaryIndex = sizeof(struct mach_header);␊ |
100 | ␉UInt16 cmd = 0;␊ |
101 | ␊ |
102 | ␉// Parse through the load commands␊ |
103 | ␉if(((struct mach_header*)binary)->magic != MH_MAGIC)␊ |
104 | ␉{␊ |
105 | ␉␉printf("Module is not 32bit\n");␊ |
106 | ␉␉return NULL;␉// 32bit only␊ |
107 | ␉}␊ |
108 | ␉␊ |
109 | ␉if(((struct mach_header*)binary)->filetype != MH_DYLIB)␊ |
110 | ␉{␊ |
111 | ␉␉printf("Module is not a dylib. Unable to load.\n");␊ |
112 | ␉␉return NULL; // Module is in the incorrect format␊ |
113 | ␉}␊ |
114 | ␉␊ |
115 | ␉while(cmd < ((struct mach_header*)binary)->ncmds)␉// TODO: for loop instead␊ |
116 | ␉{␊ |
117 | ␉␉cmd++;␊ |
118 | ␉␉␊ |
119 | ␉␉loadCommand = binary + binaryIndex;␊ |
120 | ␉␉UInt32 cmdSize = loadCommand->cmdsize;␊ |
121 | ␊ |
122 | ␉␉␊ |
123 | ␉␉switch ((loadCommand->cmd & 0x7FFFFFFF))␊ |
124 | ␉␉{␊ |
125 | ␉␉␉case LC_SYMTAB:␊ |
126 | ␉␉␉␉symtabCommand = binary + binaryIndex;␊ |
127 | ␉␉␉␉break;␊ |
128 | ␉␉␉case LC_SEGMENT:␊ |
129 | ␉␉␉␉segmentCommand = binary + binaryIndex;␊ |
130 | ␊ |
131 | ␉␉␉␉UInt32 sections = segmentCommand->nsects;␊ |
132 | ␉␉␉␉section = binary + binaryIndex + sizeof(struct segment_command);␊ |
133 | ␉␉␉␉␊ |
134 | ␉␉␉␉// Loop untill needed variables are found␊ |
135 | ␉␉␉␉while(!(nonlazy && symbolStub) && sections)␉␊ |
136 | ␉␉␉␉{␊ |
137 | ␉␉␉␉␉// Look for some sections and save the addresses␊ |
138 | ␉␉␉␉␉if(strcmp(section->sectname, SECT_NON_LAZY_SYMBOL_PTR) == 0)␊ |
139 | ␉␉␉␉␉{␊ |
140 | ␉␉␉␉␉␉/*printf("\tSection non lazy pointers at 0x%X, %d symbols\n",␊ |
141 | ␉␉␉␉␉␉␉ section->offset, ␊ |
142 | ␉␉␉␉␉␉␉ section->size / 4);␊ |
143 | ␉␉␉␉␉␉*/␊ |
144 | ␉␉␉␉␉␉switch(section->flags)␊ |
145 | ␉␉␉␉␉␉{␊ |
146 | ␉␉␉␉␉␉␉case S_NON_LAZY_SYMBOL_POINTERS:␊ |
147 | ␉␉␉␉␉␉␉␉//printf("%s S_NON_LAZY_SYMBOL_POINTERS section\n", SECT_NON_LAZY_SYMBOL_PTR);␊ |
148 | ␊ |
149 | ␉␉␉␉␉␉␉␉//nonlazy_variables = binary + section->offset;␊ |
150 | ␉␉␉␉␉␉␉␉nonlazy = binary + section->offset;␊ |
151 | ␉␉␉␉␉␉␉␉break;␊ |
152 | ␉␉␉␉␉␉␉␉␊ |
153 | ␉␉␉␉␉␉␉case S_LAZY_SYMBOL_POINTERS:␊ |
154 | ␉␉␉␉␉␉␉␉//nonlazy = binary + section->offset;␊ |
155 | ␉␉␉␉␉␉␉␉//printf("%s S_LAZY_SYMBOL_POINTERS section, 0x%X\n", SECT_NON_LAZY_SYMBOL_PTR, nonlazy);␊ |
156 | ␉␉␉␉␉␉␉␉// Fucntions␊ |
157 | ␉␉␉␉␉␉␉␉break;␊ |
158 | ␉␉␉␉␉␉␉␉␊ |
159 | ␉␉␉␉␉␉␉default:␊ |
160 | ␉␉␉␉␉␉␉␉//printf("Unhandled %s section\n", SECT_NON_LAZY_SYMBOL_PTR);␊ |
161 | ␉␉␉␉␉␉␉␉//getc();␊ |
162 | ␉␉␉␉␉␉␉␉break;␊ |
163 | ␉␉␉␉␉␉}␊ |
164 | ␉␉␉␉␉␉//getc();␊ |
165 | ␉␉␉␉␉}␊ |
166 | ␉␉␉␉␉else if(strcmp(section->sectname, SECT_SYMBOL_STUBS) == 0)␊ |
167 | ␉␉␉␉␉{␊ |
168 | ␉␉␉␉␉␉symbolStub = binary + section->offset;␊ |
169 | ␉␉␉␉␉}␊ |
170 | ␉␉␉␉␉/*␊ |
171 | ␉␉␉␉␉else ␊ |
172 | ␉␉␉␉␉{␊ |
173 | ␉␉␉␉␉␉printf("Unhandled section %s\n", section->sectname);␊ |
174 | ␉␉␉␉␉}␊ |
175 | ␉␉␉␉␉*/␊ |
176 | ␉␉␉␉␉sections--;␊ |
177 | ␉␉␉␉␉section++;␊ |
178 | ␉␉␉␉}␊ |
179 | ␉␉␉␉␉␉ ␊ |
180 | ␉␉␉␉␊ |
181 | ␉␉␉␉break;␊ |
182 | ␉␉␉␉␊ |
183 | ␉␉␉case LC_DYSYMTAB:␊ |
184 | ␉␉␉␉//dysymtabCommand = binary + binaryIndex;␊ |
185 | ␉␉␉␉//printf("Unhandled loadcommand LC_DYSYMTAB\n");␊ |
186 | ␉␉␉␉break;␊ |
187 | ␉␉␉␉␊ |
188 | ␉␉␉case LC_LOAD_DYLIB:␊ |
189 | ␉␉␉case LC_LOAD_WEAK_DYLIB ^ LC_REQ_DYLD:␊ |
190 | ␉␉␉␉dylibCommand = binary + binaryIndex;␊ |
191 | ␉␉␉␉char* module = binary + binaryIndex + ((UInt32)*((UInt32*)&dylibCommand->dylib.name));␊ |
192 | ␉␉␉␉// TODO: verify version␊ |
193 | ␉␉␉␉// =␉dylibCommand->dylib.current_version;␊ |
194 | ␉␉␉␉// =␉dylibCommand->dylib.compatibility_version;␊ |
195 | ␊ |
196 | ␉␉␉␉if(!load_module(module))␊ |
197 | ␉␉␉␉{␊ |
198 | ␉␉␉␉␉// Unable to load dependancy␊ |
199 | ␉␉␉␉␉return NULL;␊ |
200 | ␉␉␉␉}␊ |
201 | ␉␉␉␉break;␊ |
202 | ␉␉␉␉␊ |
203 | ␉␉␉case LC_ID_DYLIB:␊ |
204 | ␉␉␉␉dylibCommand = binary + binaryIndex;␊ |
205 | ␉␉␉␉moduleName =␉binary + binaryIndex + ((UInt32)*((UInt32*)&dylibCommand->dylib.name));␊ |
206 | ␉␉␉␉moduleVersion =␉dylibCommand->dylib.current_version;␊ |
207 | ␉␉␉␉moduleCompat =␉dylibCommand->dylib.compatibility_version;␊ |
208 | ␉␉␉␉break;␊ |
209 | ␊ |
210 | ␉␉␉case LC_DYLD_INFO:␊ |
211 | ␉␉␉␉// Bind and rebase info is stored here␊ |
212 | ␉␉␉␉dyldInfoCommand = binary + binaryIndex;␊ |
213 | ␉␉␉␉break;␊ |
214 | ␉␉␉␉␊ |
215 | ␉␉␉case LC_UUID:␊ |
216 | ␉␉␉␉break;␊ |
217 | ␉␉␉␉␊ |
218 | ␉␉␉default:␊ |
219 | ␉␉␉␉printf("Unhandled loadcommand 0x%X\n", loadCommand->cmd & 0x7FFFFFFF);␊ |
220 | ␉␉␉␉break;␊ |
221 | ␉␉␊ |
222 | ␉␉}␊ |
223 | ␊ |
224 | ␉␉binaryIndex += cmdSize;␊ |
225 | ␉}␊ |
226 | ␉if(!moduleName) return NULL;␊ |
227 | ␉␉␊ |
228 | ␊ |
229 | ␉// bind_macho uses the symbols added in for some reason...␊ |
230 | ␉module_start = (void*)handle_symtable((UInt32)binary, symtabCommand, symbolStub, (UInt32)nonlazy);␊ |
231 | ␊ |
232 | ␉// REbase the module before binding it.␊ |
233 | ␉if(dyldInfoCommand && dyldInfoCommand->rebase_off)␊ |
234 | ␉{␊ |
235 | ␉␉rebase_macho(binary, (char*)dyldInfoCommand->rebase_off, dyldInfoCommand->rebase_size);␊ |
236 | ␉}␊ |
237 | ␉␊ |
238 | ␉if(dyldInfoCommand && dyldInfoCommand->bind_off)␊ |
239 | ␉{␊ |
240 | ␉␉bind_macho(binary, (char*)dyldInfoCommand->bind_off, dyldInfoCommand->bind_size);␊ |
241 | ␉}␊ |
242 | ␉␊ |
243 | ␉if(dyldInfoCommand && dyldInfoCommand->weak_bind_off)␊ |
244 | ␉{␊ |
245 | ␉␉bind_macho(binary, (char*)dyldInfoCommand->weak_bind_off, dyldInfoCommand->weak_bind_size);␊ |
246 | ␉}␊ |
247 | ␉␊ |
248 | ␉if(dyldInfoCommand && dyldInfoCommand->lazy_bind_off)␊ |
249 | ␉{␊ |
250 | ␉␉bind_macho(binary, (char*)dyldInfoCommand->lazy_bind_off, dyldInfoCommand->lazy_bind_size);␊ |
251 | ␉}␊ |
252 | ␉␊ |
253 | ␊ |
254 | ␉␊ |
255 | ␊ |
256 | ␉// Notify the system that it was laoded␊ |
257 | ␉module_loaded(moduleName, moduleVersion, moduleCompat);␊ |
258 | ␉␊ |
259 | ␉return module_start;␊ |
260 | ␉␊ |
261 | }␊ |
262 | ␊ |
263 | ␊ |
264 | // Based on code from dylibinfo.cpp and ImageLoaderMachOCompressed.cpp␊ |
265 | void rebase_macho(void* base, char* rebase_stream, UInt32 size)␊ |
266 | {␊ |
267 | ␉rebase_stream += (UInt32)base;␊ |
268 | ␉␊ |
269 | ␉UInt8 immediate = 0;␊ |
270 | ␉UInt8 opcode = 0;␊ |
271 | ␉UInt8 type = 0;␊ |
272 | ␉␊ |
273 | ␉UInt32 segmentAddress = 0;␊ |
274 | ␉␊ |
275 | ␉␊ |
276 | ␉␊ |
277 | ␉UInt32 tmp = 0;␊ |
278 | ␉UInt32 tmp2 = 0;␊ |
279 | ␉UInt8 bits = 0;␊ |
280 | ␉int index = 0;␊ |
281 | ␉␊ |
282 | ␉int done = 0;␊ |
283 | ␉unsigned int i = 0;␊ |
284 | ␉␊ |
285 | ␉while(/*!done &&*/ i < size)␊ |
286 | ␉{␊ |
287 | ␉␉immediate = rebase_stream[i] & REBASE_IMMEDIATE_MASK;␊ |
288 | ␉␉opcode = rebase_stream[i] & REBASE_OPCODE_MASK;␊ |
289 | ␊ |
290 | ␉␉␊ |
291 | ␉␉switch(opcode)␊ |
292 | ␉␉{␊ |
293 | ␉␉␉case REBASE_OPCODE_DONE:␊ |
294 | ␉␉␉␉// Rebase complete.␊ |
295 | ␉␉␉␉done = 1;␊ |
296 | ␉␉␉␉break;␊ |
297 | ␉␉␉␉␊ |
298 | ␉␉␉␉␊ |
299 | ␉␉␉case REBASE_OPCODE_SET_TYPE_IMM:␊ |
300 | ␉␉␉␉// Set rebase type (pointer, absolute32, pcrel32)␊ |
301 | ␉␉␉␉//printf("Rebase type = 0x%X\n", immediate);␊ |
302 | ␉␉␉␉// NOTE: This is currently NOT used␊ |
303 | ␉␉␉␉type = immediate;␊ |
304 | ␉␉␉␉break;␊ |
305 | ␉␉␉␉␊ |
306 | ␉␉␉␉␊ |
307 | ␉␉␉case REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB:␊ |
308 | ␉␉␉␉// Locate address to begin rebasing␊ |
309 | ␉␉␉␉segmentAddress = 0;␊ |
310 | ␊ |
311 | ␉␉␉␉struct segment_command* segCommand = NULL;␊ |
312 | ␉␉␉␉␊ |
313 | ␉␉␉␉unsigned int binIndex = 0;␊ |
314 | ␉␉␉␉index = 0;␊ |
315 | ␉␉␉␉do␊ |
316 | ␉␉␉␉{␊ |
317 | ␉␉␉␉␉segCommand = base + sizeof(struct mach_header) + binIndex;␊ |
318 | ␉␉␉␉␉␊ |
319 | ␉␉␉␉␉␊ |
320 | ␉␉␉␉␉binIndex += segCommand->cmdsize;␊ |
321 | ␉␉␉␉␉index++;␊ |
322 | ␉␉␉␉}␊ |
323 | ␉␉␉␉while(index <= immediate);␊ |
324 | ␊ |
325 | ␊ |
326 | ␉␉␉␉segmentAddress = segCommand->fileoff;␊ |
327 | ␉␉␉␉␊ |
328 | ␉␉␉␉tmp = 0;␊ |
329 | ␉␉␉␉bits = 0;␊ |
330 | ␉␉␉␉do␊ |
331 | ␉␉␉␉{␊ |
332 | ␉␉␉␉␉tmp |= (rebase_stream[++i] & 0x7f) << bits;␊ |
333 | ␉␉␉␉␉bits += 7;␊ |
334 | ␉␉␉␉}␊ |
335 | ␉␉␉␉while(rebase_stream[i] & 0x80);␊ |
336 | ␉␉␉␉␊ |
337 | ␉␉␉␉segmentAddress += tmp;␊ |
338 | ␉␉␉␉break;␊ |
339 | ␉␉␉␉␊ |
340 | ␉␉␉␉␊ |
341 | ␉␉␉case REBASE_OPCODE_ADD_ADDR_ULEB:␊ |
342 | ␉␉␉␉// Add value to rebase address␊ |
343 | ␉␉␉␉tmp = 0;␊ |
344 | ␉␉␉␉bits = 0;␊ |
345 | ␉␉␉␉do␊ |
346 | ␉␉␉␉{␊ |
347 | ␉␉␉␉␉tmp <<= bits;␊ |
348 | ␉␉␉␉␉tmp |= rebase_stream[++i] & 0x7f;␊ |
349 | ␉␉␉␉␉bits += 7;␊ |
350 | ␉␉␉␉}␊ |
351 | ␉␉␉␉while(rebase_stream[i] & 0x80);␊ |
352 | ␉␉␉␉␊ |
353 | ␉␉␉␉segmentAddress +=␉tmp; ␊ |
354 | ␉␉␉␉break;␊ |
355 | ␉␉␉␉␊ |
356 | ␉␉␉case REBASE_OPCODE_ADD_ADDR_IMM_SCALED:␊ |
357 | ␉␉␉␉segmentAddress += immediate * sizeof(void*);␊ |
358 | ␉␉␉␉break;␊ |
359 | ␉␉␉␉␊ |
360 | ␉␉␉␉␊ |
361 | ␉␉␉case REBASE_OPCODE_DO_REBASE_IMM_TIMES:␊ |
362 | ␉␉␉␉index = 0;␊ |
363 | ␉␉␉␉for (index = 0; index < immediate; ++index) {␊ |
364 | ␉␉␉␉␉rebase_location(base + segmentAddress, (char*)base);␊ |
365 | ␉␉␉␉␉segmentAddress += sizeof(void*);␊ |
366 | ␉␉␉␉}␊ |
367 | ␉␉␉␉break;␊ |
368 | ␉␉␉␊ |
369 | ␉␉␉␉␊ |
370 | ␉␉␉case REBASE_OPCODE_DO_REBASE_ULEB_TIMES:␊ |
371 | ␉␉␉␉tmp = 0;␊ |
372 | ␉␉␉␉bits = 0;␊ |
373 | ␉␉␉␉do␊ |
374 | ␉␉␉␉{␊ |
375 | ␉␉␉␉␉tmp |= (rebase_stream[++i] & 0x7f) << bits;␊ |
376 | ␉␉␉␉␉bits += 7;␊ |
377 | ␉␉␉␉}␊ |
378 | ␉␉␉␉while(rebase_stream[i] & 0x80);␊ |
379 | ␉␉␉␉␊ |
380 | ␉␉␉␉index = 0;␊ |
381 | ␉␉␉␉for (index = 0; index < tmp; ++index) {␊ |
382 | ␉␉␉␉␉//printf("\tRebasing 0x%X\n", segmentAddress);␊ |
383 | ␉␉␉␉␉rebase_location(base + segmentAddress, (char*)base);␉␉␉␉␉␊ |
384 | ␉␉␉␉␉segmentAddress += sizeof(void*);␊ |
385 | ␉␉␉␉}␊ |
386 | ␉␉␉␉break;␊ |
387 | ␉␉␉␉␊ |
388 | ␉␉␉case REBASE_OPCODE_DO_REBASE_ADD_ADDR_ULEB:␊ |
389 | ␉␉␉␉tmp = 0;␊ |
390 | ␉␉␉␉bits = 0;␊ |
391 | ␉␉␉␉do␊ |
392 | ␉␉␉␉{␊ |
393 | ␉␉␉␉␉tmp |= (rebase_stream[++i] & 0x7f) << bits;␊ |
394 | ␉␉␉␉␉bits += 7;␊ |
395 | ␉␉␉␉}␊ |
396 | ␉␉␉␉while(rebase_stream[i] & 0x80);␊ |
397 | ␉␉␉␉␊ |
398 | ␉␉␉␉rebase_location(base + segmentAddress, (char*)base);␊ |
399 | ␉␉␉␉␊ |
400 | ␉␉␉␉segmentAddress += tmp + sizeof(void*);␊ |
401 | ␉␉␉␉break;␊ |
402 | ␉␉␉␉␊ |
403 | ␉␉␉case REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_ULEB:␊ |
404 | ␉␉␉␉tmp = 0;␊ |
405 | ␉␉␉␉bits = 0;␊ |
406 | ␉␉␉␉do␊ |
407 | ␉␉␉␉{␊ |
408 | ␉␉␉␉␉tmp |= (rebase_stream[++i] & 0x7f) << bits;␊ |
409 | ␉␉␉␉␉bits += 7;␊ |
410 | ␉␉␉␉}␊ |
411 | ␉␉␉␉while(rebase_stream[i] & 0x80);␊ |
412 | ␉␉␉␉␊ |
413 | ␉␉␉␉␊ |
414 | ␉␉␉␉tmp2 = 0;␊ |
415 | ␉␉␉␉bits = 0;␊ |
416 | ␉␉␉␉do␊ |
417 | ␉␉␉␉{␊ |
418 | ␉␉␉␉␉tmp2 |= (rebase_stream[++i] & 0x7f) << bits;␊ |
419 | ␉␉␉␉␉bits += 7;␊ |
420 | ␉␉␉␉}␊ |
421 | ␉␉␉␉while(rebase_stream[i] & 0x80);␊ |
422 | ␉␉␉␉␊ |
423 | ␉␉␉␉index = 0;␊ |
424 | ␉␉␉␉for (index = 0; index < tmp; ++index) {␊ |
425 | ␉␉␉␉␉rebase_location(base + segmentAddress, (char*)base);␊ |
426 | ␉␉␉␉␉␊ |
427 | ␉␉␉␉␉segmentAddress += tmp2 + sizeof(void*);␊ |
428 | ␉␉␉␉}␊ |
429 | ␉␉␉␉break;␊ |
430 | ␉␉}␊ |
431 | ␉␉i++;␊ |
432 | ␉}␊ |
433 | }␊ |
434 | ␊ |
435 | // Based on code from dylibinfo.cpp and ImageLoaderMachOCompressed.cpp␊ |
436 | // NOTE: this uses 32bit values, and not 64bit values. ␊ |
437 | // There is apossibility that this could cause issues,␊ |
438 | // however the macho file is 32 bit, so it shouldn't matter too much␊ |
439 | void bind_macho(void* base, char* bind_stream, UInt32 size)␊ |
440 | {␉␊ |
441 | ␉bind_stream += (UInt32)base;␊ |
442 | ␉␊ |
443 | ␉UInt8 immediate = 0;␊ |
444 | ␉UInt8 opcode = 0;␊ |
445 | ␉UInt8 type = 0;␊ |
446 | ␉␊ |
447 | ␉UInt32 segmentAddress = 0;␊ |
448 | ␉␊ |
449 | ␉UInt32 address = 0;␊ |
450 | ␉␊ |
451 | ␉SInt32 addend = 0;␉␉␉// TODO: handle this␊ |
452 | ␉SInt32 libraryOrdinal = 0;␊ |
453 | ␊ |
454 | ␉const char* symbolName = NULL;␊ |
455 | ␉UInt8 symboFlags = 0;␊ |
456 | ␉UInt32 symbolAddr = 0xFFFFFFFF;␊ |
457 | ␉␊ |
458 | ␉// Temperary variables␊ |
459 | ␉UInt8 bits = 0;␊ |
460 | ␉UInt32 tmp = 0;␊ |
461 | ␉UInt32 tmp2 = 0;␊ |
462 | ␉␊ |
463 | ␉UInt32 index = 0;␊ |
464 | ␉int done = 0;␊ |
465 | ␉unsigned int i = 0;␊ |
466 | ␉␊ |
467 | ␉while(/*!done &&*/ i < size)␊ |
468 | ␉{␊ |
469 | ␉␉immediate = bind_stream[i] & BIND_IMMEDIATE_MASK;␊ |
470 | ␉␉opcode = bind_stream[i] & BIND_OPCODE_MASK;␊ |
471 | ␉␉␊ |
472 | ␉␉␊ |
473 | ␉␉switch(opcode)␊ |
474 | ␉␉{␊ |
475 | ␉␉␉case BIND_OPCODE_DONE:␊ |
476 | ␉␉␉␉done = 1; ␊ |
477 | ␉␉␉␉break;␊ |
478 | ␉␉␉␉␊ |
479 | ␉␉␉case BIND_OPCODE_SET_DYLIB_ORDINAL_IMM:␊ |
480 | ␉␉␉␉libraryOrdinal = immediate;␊ |
481 | ␉␉␉␉//printf("BIND_OPCODE_SET_DYLIB_ORDINAL_IMM: %d\n", libraryOrdinal);␊ |
482 | ␉␉␉␉break;␊ |
483 | ␉␉␉␉␊ |
484 | ␉␉␉case BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB:␊ |
485 | ␉␉␉␉libraryOrdinal = 0;␊ |
486 | ␉␉␉␉bits = 0;␊ |
487 | ␉␉␉␉do␊ |
488 | ␉␉␉␉{␊ |
489 | ␉␉␉␉␉libraryOrdinal |= (bind_stream[++i] & 0x7f) << bits;␊ |
490 | ␉␉␉␉␉bits += 7;␊ |
491 | ␉␉␉␉}␊ |
492 | ␉␉␉␉while(bind_stream[i] & 0x80);␊ |
493 | ␉␉␉␉␊ |
494 | ␉␉␉␉//printf("BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB: %d\n", libraryOrdinal);␊ |
495 | ␊ |
496 | ␉␉␉␉break;␊ |
497 | ␉␉␉␉␊ |
498 | ␉␉␉case BIND_OPCODE_SET_DYLIB_SPECIAL_IMM:␊ |
499 | ␉␉␉␉// NOTE: this is wrong, fortunately we don't use it␊ |
500 | ␉␉␉␉libraryOrdinal = -immediate;␊ |
501 | ␉␉␉␉//printf("BIND_OPCODE_SET_DYLIB_SPECIAL_IMM: %d\n", libraryOrdinal);␊ |
502 | ␊ |
503 | ␉␉␉␉break;␊ |
504 | ␉␉␉␉␊ |
505 | ␉␉␉case BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM:␊ |
506 | ␉␉␉␉symboFlags = immediate;␊ |
507 | ␉␉␉␉symbolName = (char*)&bind_stream[++i];␊ |
508 | ␉␉␉␉i += strlen((char*)&bind_stream[i]);␊ |
509 | ␉␉␉␉//printf("BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM: %s, 0x%X\n", symbolName, symboFlags);␊ |
510 | ␊ |
511 | ␉␉␉␉symbolAddr = lookup_all_symbols(symbolName);␊ |
512 | ␊ |
513 | ␉␉␉␉break;␊ |
514 | ␉␉␉␉␊ |
515 | ␉␉␉case BIND_OPCODE_SET_TYPE_IMM:␊ |
516 | ␉␉␉␉// Set bind type (pointer, absolute32, pcrel32)␊ |
517 | ␉␉␉␉type = immediate;␊ |
518 | ␉␉␉␉//printf("BIND_OPCODE_SET_TYPE_IMM: %d\n", type);␊ |
519 | ␊ |
520 | ␉␉␉␉break;␊ |
521 | ␉␉␉␉␊ |
522 | ␉␉␉case BIND_OPCODE_SET_ADDEND_SLEB:␊ |
523 | ␉␉␉␉addend = 0;␊ |
524 | ␉␉␉␉bits = 0;␊ |
525 | ␉␉␉␉do␊ |
526 | ␉␉␉␉{␊ |
527 | ␉␉␉␉␉addend |= (bind_stream[++i] & 0x7f) << bits;␊ |
528 | ␉␉␉␉␉bits += 7;␊ |
529 | ␉␉␉␉}␊ |
530 | ␉␉␉␉while(bind_stream[i] & 0x80);␊ |
531 | ␉␉␉␉␊ |
532 | ␉␉␉␉if(!(bind_stream[i-1] & 0x40)) addend *= -1;␊ |
533 | ␉␉␉␉␊ |
534 | ␉␉␉␉//printf("BIND_OPCODE_SET_ADDEND_SLEB: %d\n", addend);␊ |
535 | ␉␉␉␉break;␊ |
536 | ␉␉␉␉␊ |
537 | ␉␉␉case BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB:␊ |
538 | ␉␉␉␉segmentAddress = 0;␊ |
539 | ␊ |
540 | ␉␉␉␉// Locate address␊ |
541 | ␉␉␉␉struct segment_command* segCommand = NULL;␊ |
542 | ␉␉␉␉␊ |
543 | ␉␉␉␉unsigned int binIndex = 0;␊ |
544 | ␉␉␉␉index = 0;␊ |
545 | ␉␉␉␉do␊ |
546 | ␉␉␉␉{␊ |
547 | ␉␉␉␉␉segCommand = base + sizeof(struct mach_header) + binIndex;␊ |
548 | ␉␉␉␉␉binIndex += segCommand->cmdsize;␊ |
549 | ␉␉␉␉␉index++;␊ |
550 | ␉␉␉␉}␊ |
551 | ␉␉␉␉while(index <= immediate);␊ |
552 | ␉␉␉␉␊ |
553 | ␉␉␉␉segmentAddress = segCommand->fileoff;␊ |
554 | ␉␉␉␉␊ |
555 | ␉␉␉␉// Read in offset␊ |
556 | ␉␉␉␉tmp = 0;␊ |
557 | ␉␉␉␉bits = 0;␊ |
558 | ␉␉␉␉do␊ |
559 | ␉␉␉␉{␊ |
560 | ␉␉␉␉␉tmp |= (bind_stream[++i] & 0x7f) << bits;␊ |
561 | ␉␉␉␉␉bits += 7;␊ |
562 | ␉␉␉␉}␊ |
563 | ␉␉␉␉while(bind_stream[i] & 0x80);␊ |
564 | ␉␉␉␉␊ |
565 | ␉␉␉␉segmentAddress += tmp;␊ |
566 | ␉␉␉␉␊ |
567 | ␉␉␉␉//printf("BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB: 0x%X\n", segmentAddress);␊ |
568 | ␉␉␉␉break;␊ |
569 | ␉␉␉␉␊ |
570 | ␉␉␉case BIND_OPCODE_ADD_ADDR_ULEB:␊ |
571 | ␉␉␉␉// Read in offset␊ |
572 | ␉␉␉␉tmp = 0;␊ |
573 | ␉␉␉␉bits = 0;␊ |
574 | ␉␉␉␉do␊ |
575 | ␉␉␉␉{␊ |
576 | ␉␉␉␉␉tmp |= (bind_stream[++i] & 0x7f) << bits;␊ |
577 | ␉␉␉␉␉bits += 7;␊ |
578 | ␉␉␉␉}␊ |
579 | ␉␉␉␉while(bind_stream[i] & 0x80);␊ |
580 | ␉␉␉␉␊ |
581 | ␉␉␉␉segmentAddress += tmp;␊ |
582 | ␉␉␉␉//printf("BIND_OPCODE_ADD_ADDR_ULEB: 0x%X\n", segmentAddress);␊ |
583 | ␉␉␉␉break;␊ |
584 | ␉␉␉␉␊ |
585 | ␉␉␉case BIND_OPCODE_DO_BIND:␊ |
586 | ␉␉␉␉//printf("BIND_OPCODE_DO_BIND\n");␊ |
587 | ␉␉␉␉if(symbolAddr != 0xFFFFFFFF)␊ |
588 | ␉␉␉␉{␊ |
589 | ␉␉␉␉␉address = segmentAddress + (UInt32)base;␊ |
590 | ␊ |
591 | ␉␉␉␉␉((char*)address)[0] = (symbolAddr & 0x000000FF) >> 0;␊ |
592 | ␉␉␉␉␉((char*)address)[1] = (symbolAddr & 0x0000FF00) >> 8;␊ |
593 | ␉␉␉␉␉((char*)address)[2] = (symbolAddr & 0x00FF0000) >> 16;␊ |
594 | ␉␉␉␉␉((char*)address)[3] = (symbolAddr & 0xFF000000) >> 24;␊ |
595 | ␉␉␉␉}␊ |
596 | ␉␉␉␉else if(strcmp(symbolName, SYMBOL_DYLD_STUB_BINDER) != 0)␊ |
597 | ␉␉␉␉{␊ |
598 | ␉␉␉␉␉printf("Unable to bind symbol %s\n", symbolName);␊ |
599 | ␉␉␉␉}␊ |
600 | ␉␉␉␉␊ |
601 | ␉␉␉␉segmentAddress += sizeof(void*);␊ |
602 | ␉␉␉␉break;␊ |
603 | ␉␉␉␉␊ |
604 | ␉␉␉case BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB:␊ |
605 | ␉␉␉␉//printf("BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB\n");␊ |
606 | ␉␉␉␉␊ |
607 | ␉␉␉␉␊ |
608 | ␉␉␉␉// Read in offset␊ |
609 | ␉␉␉␉tmp = 0;␊ |
610 | ␉␉␉␉bits = 0;␊ |
611 | ␉␉␉␉do␊ |
612 | ␉␉␉␉{␊ |
613 | ␉␉␉␉␉tmp |= (bind_stream[++i] & 0x7f) << bits;␊ |
614 | ␉␉␉␉␉bits += 7;␊ |
615 | ␉␉␉␉}␊ |
616 | ␉␉␉␉while(bind_stream[i] & 0x80);␊ |
617 | ␉␉␉␉␊ |
618 | ␉␉␉␉␊ |
619 | ␉␉␉␉␊ |
620 | ␉␉␉␉if(symbolAddr != 0xFFFFFFFF)␊ |
621 | ␉␉␉␉{␊ |
622 | ␉␉␉␉␉address = segmentAddress + (UInt32)base;␊ |
623 | ␉␉␉␉␉␊ |
624 | ␉␉␉␉␉((char*)address)[0] = (symbolAddr & 0x000000FF) >> 0;␊ |
625 | ␉␉␉␉␉((char*)address)[1] = (symbolAddr & 0x0000FF00) >> 8;␊ |
626 | ␉␉␉␉␉((char*)address)[2] = (symbolAddr & 0x00FF0000) >> 16;␊ |
627 | ␉␉␉␉␉((char*)address)[3] = (symbolAddr & 0xFF000000) >> 24;␊ |
628 | ␉␉␉␉}␊ |
629 | ␉␉␉␉else if(strcmp(symbolName, SYMBOL_DYLD_STUB_BINDER) != 0)␊ |
630 | ␉␉␉␉{␊ |
631 | ␉␉␉␉␉printf("Unable to bind symbol %s\n", symbolName);␊ |
632 | ␉␉␉␉}␊ |
633 | ␉␉␉␉segmentAddress += tmp + sizeof(void*);␊ |
634 | ␊ |
635 | ␉␉␉␉␊ |
636 | ␉␉␉␉break;␊ |
637 | ␉␉␉␉␊ |
638 | ␉␉␉case BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED:␊ |
639 | ␉␉␉␉//printf("BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED\n");␊ |
640 | ␉␉␉␉␊ |
641 | ␉␉␉␉if(symbolAddr != 0xFFFFFFFF)␊ |
642 | ␉␉␉␉{␊ |
643 | ␉␉␉␉␉address = segmentAddress + (UInt32)base;␊ |
644 | ␉␉␉␉␉␊ |
645 | ␉␉␉␉␉((char*)address)[0] = (symbolAddr & 0x000000FF) >> 0;␊ |
646 | ␉␉␉␉␉((char*)address)[1] = (symbolAddr & 0x0000FF00) >> 8;␊ |
647 | ␉␉␉␉␉((char*)address)[2] = (symbolAddr & 0x00FF0000) >> 16;␊ |
648 | ␉␉␉␉␉((char*)address)[3] = (symbolAddr & 0xFF000000) >> 24;␊ |
649 | ␉␉␉␉}␊ |
650 | ␉␉␉␉else if(strcmp(symbolName, SYMBOL_DYLD_STUB_BINDER) != 0)␊ |
651 | ␉␉␉␉{␊ |
652 | ␉␉␉␉␉printf("Unable to bind symbol %s\n", symbolName);␊ |
653 | ␉␉␉␉}␊ |
654 | ␉␉␉␉segmentAddress += (immediate * sizeof(void*)) + sizeof(void*);␊ |
655 | ␊ |
656 | ␉␉␉␉␊ |
657 | ␉␉␉␉break;␊ |
658 | ␉␉␉␉␊ |
659 | ␉␉␉case BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB:␊ |
660 | ␊ |
661 | ␉␉␉␉tmp = 0;␊ |
662 | ␉␉␉␉bits = 0;␊ |
663 | ␉␉␉␉do␊ |
664 | ␉␉␉␉{␊ |
665 | ␉␉␉␉␉tmp |= (bind_stream[++i] & 0x7f) << bits;␊ |
666 | ␉␉␉␉␉bits += 7;␊ |
667 | ␉␉␉␉}␊ |
668 | ␉␉␉␉while(bind_stream[i] & 0x80);␊ |
669 | ␊ |
670 | ␉␉␉␉␊ |
671 | ␉␉␉␉tmp2 = 0;␊ |
672 | ␉␉␉␉bits = 0;␊ |
673 | ␉␉␉␉do␊ |
674 | ␉␉␉␉{␊ |
675 | ␉␉␉␉␉tmp2 |= (bind_stream[++i] & 0x7f) << bits;␊ |
676 | ␉␉␉␉␉bits += 7;␊ |
677 | ␉␉␉␉}␊ |
678 | ␉␉␉␉while(bind_stream[i] & 0x80);␊ |
679 | ␉␉␉␉␊ |
680 | ␉␉␉␉␊ |
681 | ␉␉␉␉//printf("BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB 0x%X 0x%X\n", tmp, tmp2);␊ |
682 | ␊ |
683 | ␉␉␉␉␊ |
684 | ␉␉␉␉if(symbolAddr != 0xFFFFFFFF)␊ |
685 | ␉␉␉␉{␊ |
686 | ␉␉␉␉␉for(index = 0; index < tmp; index++)␊ |
687 | ␉␉␉␉␉{␊ |
688 | ␉␉␉␉␉␉␊ |
689 | ␉␉␉␉␉␉address = segmentAddress + (UInt32)base;␊ |
690 | ␉␉␉␉␉␉␊ |
691 | ␉␉␉␉␉␉((char*)address)[0] = (symbolAddr & 0x000000FF) >> 0;␊ |
692 | ␉␉␉␉␉␉((char*)address)[1] = (symbolAddr & 0x0000FF00) >> 8;␊ |
693 | ␉␉␉␉␉␉((char*)address)[2] = (symbolAddr & 0x00FF0000) >> 16;␊ |
694 | ␉␉␉␉␉␉((char*)address)[3] = (symbolAddr & 0xFF000000) >> 24;␊ |
695 | ␉␉␉␉␉␉␊ |
696 | ␉␉␉␉␉␉segmentAddress += tmp2 + sizeof(void*);␊ |
697 | ␉␉␉␉␉}␊ |
698 | ␉␉␉␉}␊ |
699 | ␉␉␉␉else if(strcmp(symbolName, SYMBOL_DYLD_STUB_BINDER) != 0)␊ |
700 | ␉␉␉␉{␊ |
701 | ␉␉␉␉␉printf("Unable to bind symbol %s\n", symbolName);␊ |
702 | ␉␉␉␉}␊ |
703 | ␉␉␉␉␊ |
704 | ␉␉␉␉␊ |
705 | ␉␉␉␉break;␊ |
706 | ␉␉␉␉␊ |
707 | ␉␉}␊ |
708 | ␉␉i++;␊ |
709 | ␉}␊ |
710 | }␊ |
711 | ␊ |
712 | inline void rebase_location(UInt32* location, char* base)␊ |
713 | {␊ |
714 | ␉*location += (UInt32)base;␊ |
715 | }␊ |
716 | ␊ |
717 | /*␊ |
718 | * add_symbol␊ |
719 | * This function adds a symbol from a module to the list of known symbols ␊ |
720 | * TODO: actualy do something... ␊ |
721 | * possibly change to a pointer and add this to the Symbol module␊ |
722 | */␊ |
723 | void add_symbol(char* symbol, void* addr)␊ |
724 | {␊ |
725 | ␉//printf("Adding symbol %s at 0x%X\n", symbol, addr);␊ |
726 | ␉␊ |
727 | ␉if(!moduleSymbols)␊ |
728 | ␉{␊ |
729 | ␉␉moduleSymbols = malloc(sizeof(symbolList_t));␊ |
730 | ␉␉moduleSymbols->next = NULL;␊ |
731 | ␉␉moduleSymbols->addr = (unsigned int)addr;␊ |
732 | ␉␉moduleSymbols->symbol = symbol;␊ |
733 | ␉}␊ |
734 | ␉else␊ |
735 | ␉{␊ |
736 | ␉␉symbolList_t* entry = moduleSymbols;␊ |
737 | ␉␉while(entry->next)␊ |
738 | ␉␉{␊ |
739 | ␉␉␉entry = entry->next;␊ |
740 | ␉␉}␊ |
741 | ␉␉␊ |
742 | ␉␉entry->next = malloc(sizeof(symbolList_t));␊ |
743 | ␉␉entry = entry->next;␊ |
744 | ␉␉␊ |
745 | ␉␉entry->next = NULL;␊ |
746 | ␉␉entry->addr = (unsigned int)addr;␊ |
747 | ␉␉entry->symbol = symbol;␊ |
748 | ␉␉␊ |
749 | ␉}␊ |
750 | ␉␉␉␉␉␉␉ ␊ |
751 | ␊ |
752 | }␊ |
753 | ␊ |
754 | ␊ |
755 | /*␊ |
756 | * print out the information about the loaded module␊ |
757 | ␊ |
758 | */␊ |
759 | void module_loaded(char* name, UInt32 version, UInt32 compat)␊ |
760 | {␊ |
761 | ␉/*␊ |
762 | ␉printf("\%s.dylib Version %d.%d.%d loaded\n"␊ |
763 | ␉␉ "\tCompatibility Version: %d.%d.%d\n",␊ |
764 | ␉␉ name,␊ |
765 | ␉␉ (version >> 16) & 0xFFFF,␊ |
766 | ␉␉ (version >> 8) & 0x00FF,␊ |
767 | ␉␉ (version >> 0) & 0x00FF,␊ |
768 | ␉␉ (compat >> 16) & 0xFFFF,␊ |
769 | ␉␉ (compat >> 8) & 0x00FF,␊ |
770 | ␉␉ (compat >> 0) & 0x00FF);␉␊ |
771 | ␉*/␊ |
772 | ␉if(loadedModules == NULL)␊ |
773 | ␉{␊ |
774 | ␉␉loadedModules = malloc(sizeof(moduleList_t));␊ |
775 | ␉␉loadedModules->next = NULL;␊ |
776 | ␉␉loadedModules->module = name;␊ |
777 | ␉␉loadedModules->version = version;␊ |
778 | ␉␉loadedModules->compat = compat;␊ |
779 | ␉}␊ |
780 | ␉else␊ |
781 | ␉{␊ |
782 | ␉␉moduleList_t* entry = loadedModules;␊ |
783 | ␉␉while(entry->next)␊ |
784 | ␉␉{␊ |
785 | ␉␉␉entry = entry->next;␊ |
786 | ␉␉}␊ |
787 | ␉␉␊ |
788 | ␉␉entry->next = malloc(sizeof(moduleList_t));␊ |
789 | ␉␉entry = entry->next;␊ |
790 | ␉␉␊ |
791 | ␉␉entry->next = NULL;␊ |
792 | ␉␉entry->module = name;␊ |
793 | ␉␉entry->version = version;␊ |
794 | ␉␉entry->compat = compat;␊ |
795 | ␉}␊ |
796 | ␊ |
797 | ␉␊ |
798 | }␊ |
799 | ␊ |
800 | int is_module_laoded(const char* name)␊ |
801 | {␊ |
802 | ␉moduleList_t* entry = loadedModules;␊ |
803 | ␉while(entry)␊ |
804 | ␉{␊ |
805 | ␉␉if(strcmp(entry->module, name) == 0)␊ |
806 | ␉␉{␊ |
807 | ␉␉␉return 1;␊ |
808 | ␉␉}␊ |
809 | ␉␉else␊ |
810 | ␉␉{␊ |
811 | ␉␉␉entry = entry->next;␊ |
812 | ␉␉}␊ |
813 | ␊ |
814 | ␉}␊ |
815 | ␉return 0;␊ |
816 | }␊ |
817 | ␊ |
818 | // Look for symbols using the Smbols moduel function.␊ |
819 | // If non are found, look through the list of module symbols␊ |
820 | unsigned int lookup_all_symbols(const char* name)␊ |
821 | {␊ |
822 | ␉unsigned int addr = 0xFFFFFFFF;␊ |
823 | ␉if(lookup_symbol && (UInt32)lookup_symbol != 0xFFFFFFFF)␊ |
824 | ␉{␊ |
825 | ␉␉addr = lookup_symbol(name);␊ |
826 | ␉␉if(addr != 0xFFFFFFFF)␊ |
827 | ␉␉{␊ |
828 | ␉␉␉//printf("Internal symbol %s located at 0x%X\n", name, addr);␊ |
829 | ␉␉␉return addr;␊ |
830 | ␉␉}␊ |
831 | ␉}␊ |
832 | ␊ |
833 | ␉␊ |
834 | ␉symbolList_t* entry = moduleSymbols;␊ |
835 | ␉while(entry)␊ |
836 | ␉{␊ |
837 | ␉␉if(strcmp(entry->symbol, name) == 0)␊ |
838 | ␉␉{␊ |
839 | ␉␉␉//printf("External symbol %s located at 0x%X\n", name, entry->addr);␊ |
840 | ␉␉␉return entry->addr;␊ |
841 | ␉␉}␊ |
842 | ␉␉else␊ |
843 | ␉␉{␊ |
844 | ␉␉␉entry = entry->next;␊ |
845 | ␉␉}␊ |
846 | ␊ |
847 | ␉}␊ |
848 | ␉if(strcmp(name, SYMBOL_DYLD_STUB_BINDER) != 0)␊ |
849 | ␉{␊ |
850 | ␉␉printf("Unable to locate symbol %s\n", name);␊ |
851 | ␉}␊ |
852 | ␉return 0xFFFFFFFF;␊ |
853 | }␊ |
854 | ␊ |
855 | ␊ |
856 | /*␊ |
857 | * parse the symbol table␊ |
858 | * Lookup any undefined symbols␊ |
859 | */␊ |
860 | ␊ |
861 | unsigned int handle_symtable(UInt32 base, struct symtab_command* symtabCommand, char* symbolStub, UInt32 nonlazy)␊ |
862 | {␊ |
863 | ␉unsigned int module_start = 0xFFFFFFFF;␊ |
864 | ␉␊ |
865 | ␉UInt32 symbolIndex = 0;␊ |
866 | ␉char* symbolString = base + (char*)symtabCommand->stroff;␊ |
867 | ␉//char* symbolTable = base + symtabCommand->symoff;␊ |
868 | ␉␊ |
869 | ␉while(symbolIndex < symtabCommand->nsyms)␊ |
870 | ␉{␊ |
871 | ␉␉␊ |
872 | ␉␉struct nlist* symbolEntry = (void*)base + symtabCommand->symoff + (symbolIndex * sizeof(struct nlist));␊ |
873 | ␉␉␊ |
874 | ␉␉// If the symbol is exported by this module␊ |
875 | ␉␉if(symbolEntry->n_value)␊ |
876 | ␉␉{␊ |
877 | ␉␉␉if(strcmp(symbolString + symbolEntry->n_un.n_strx, "start") == 0)␊ |
878 | ␉␉␉{␊ |
879 | ␉␉␉␉// Module start located. Start is an alias so don't register it␊ |
880 | ␉␉␉␉module_start = base + symbolEntry->n_value;␊ |
881 | ␉␉␉}␊ |
882 | ␉␉␉else␊ |
883 | ␉␉␉{␊ |
884 | ␉␉␉␉add_symbol(symbolString + symbolEntry->n_un.n_strx, (void*)base + symbolEntry->n_value); ␊ |
885 | ␉␉␉}␊ |
886 | ␉␉}␊ |
887 | ␉␉␊ |
888 | ␉␉symbolEntry+= sizeof(struct nlist);␊ |
889 | ␉␉symbolIndex++;␉// TODO remove␊ |
890 | ␉}␊ |
891 | ␉␊ |
892 | ␉return module_start;␊ |
893 | ␉␊ |
894 | }␊ |
895 | ␊ |
896 | /*␊ |
897 | * Modify a function to call this one, then return once finished.␊ |
898 | */␊ |
899 | int hook_function(const char* symbol)␊ |
900 | {␊ |
901 | ␉return 0;␊ |
902 | }␊ |
903 | ␊ |
904 | /*␊ |
905 | * Locate the symbol for an already loaded function and modify the beginning of␊ |
906 | * the function to jump directly to the new one␊ |
907 | * example: replace_function("_HelloWorld_start", &replacement_start);␊ |
908 | */␊ |
909 | int replace_function(const char* symbol, void* newAddress)␊ |
910 | {␊ |
911 | ␉UInt32* jumpPointer = malloc(sizeof(UInt32*));␉ ␊ |
912 | ␉// TODO: look into using the next four bytes of the function instead␊ |
913 | ␊ |
914 | ␉UInt32 addr = lookup_all_symbols(symbol);␊ |
915 | ␉␊ |
916 | ␉char* binary = (char*)addr;␊ |
917 | ␉if(addr != 0xFFFFFFFF)␊ |
918 | ␉{␊ |
919 | ␉␉*binary++ = 0xFF;␉// Jump␊ |
920 | ␉␉*binary++ = 0x25;␉// Long Jump␊ |
921 | ␉␉*((UInt32*)binary) = (UInt32)jumpPointer;␊ |
922 | ␉␉␊ |
923 | ␉␉*jumpPointer = (UInt32)newAddress;␊ |
924 | ␉␉␊ |
925 | ␉␉return 1;␊ |
926 | ␉}␊ |
927 | ␉else ␊ |
928 | ␉{␊ |
929 | ␉␉return 0;␊ |
930 | ␉}␊ |
931 | ␊ |
932 | } |