1 | /*␊ |
2 | * Copyright 2010 Evan Lojewski. All rights reserved.␊ |
3 | *␊ |
4 | */␊ |
5 | ␊ |
6 | #ifndef DEBUG_MODULES␊ |
7 | #define DEBUG_MODULES 0␊ |
8 | #endif␊ |
9 | ␊ |
10 | //#include "boot.h"␊ |
11 | //#include "libsa.h"␊ |
12 | //#include "saio_internal.h"␊ |
13 | //#include "multiboot.h"␊ |
14 | #include "bootstruct.h"␊ |
15 | #include "modules.h"␊ |
16 | #include "memory.h"␊ |
17 | ␊ |
18 | ␊ |
19 | #if DEBUG_MODULES␊ |
20 | #define DBG(x...)␉printf(x);␊ |
21 | #define DBGPAUSE()␉getc()␊ |
22 | #else␊ |
23 | #define DBG(x...)␊ |
24 | #define DBGPAUSE()␊ |
25 | #endif␊ |
26 | ␊ |
27 | // NOTE: Global so that modules can link with this␊ |
28 | unsigned long long textAddress = 0;␊ |
29 | unsigned long long textSection = 0;␊ |
30 | ␊ |
31 | void* symbols_module_start = (void*)0xFFFFFFFF;␉// Global, value is populated by the makefile with actual address␊ |
32 | ␊ |
33 | /** Internal symbols, however there are accessor methods **/␊ |
34 | moduleHook_t* moduleCallbacks = NULL;␊ |
35 | moduleList_t* loadedModules = NULL;␊ |
36 | symbolList_t* moduleSymbols = NULL;␊ |
37 | unsigned int (*lookup_symbol)(const char*) = NULL;␊ |
38 | ␊ |
39 | ␊ |
40 | /*␊ |
41 | * Initialize the module system by loading the Symbols.dylib module.␊ |
42 | * Once loaded, locate the _lookup_symbol function so that internal␊ |
43 | * symbols can be resolved.␊ |
44 | */␊ |
45 | int init_module_system()␊ |
46 | {␊ |
47 | ␉int retVal = 0;␊ |
48 | ␉void (*module_start)(void) = NULL;␊ |
49 | ␉char* module_data = symbols_module_start + BOOT2_ADDR;␊ |
50 | ␊ |
51 | ␉// Intialize module system␊ |
52 | ␉if(symbols_module_start != (void*)0xFFFFFFFF)␊ |
53 | ␉{␊ |
54 | ␉␉// Module system was compiled in (Symbols.dylib addr known)␊ |
55 | ␉␉module_start = parse_mach(module_data, &load_module, &add_symbol);␊ |
56 | ␉␉␊ |
57 | ␉␉if(module_start && module_start != (void*)0xFFFFFFFF)␊ |
58 | ␉␉{␊ |
59 | ␉␉␉// Notify the system that it was laoded␊ |
60 | ␉␉␉module_loaded(SYMBOLS_MODULE /*moduleName, moduleVersion, moduleCompat*/);␊ |
61 | ␉␉␉␊ |
62 | ␉␉␉(*module_start)();␉// Start the module. This will point to load_all_modules due to the way the dylib was constructed.␊ |
63 | ␉␉␉execute_hook("ModulesLoaded", NULL, NULL, NULL, NULL);␊ |
64 | ␉␉␉DBG("Module %s Loaded.\n", SYMBOLS_MODULE);␊ |
65 | ␉␉␉retVal = 1;␊ |
66 | ␊ |
67 | ␉␉}␊ |
68 | ␉␉else␊ |
69 | ␉␉{␊ |
70 | module_data -= 0x10; // XCODE 4 HACK␊ |
71 | module_start = parse_mach(module_data, &load_module, &add_symbol);␊ |
72 | ␊ |
73 | if(module_start && module_start != (void*)0xFFFFFFFF)␊ |
74 | {␊ |
75 | // Notify the system that it was laoded␊ |
76 | module_loaded(SYMBOLS_MODULE /*moduleName, moduleVersion, moduleCompat*/);␊ |
77 | ␊ |
78 | (*module_start)();␉// Start the module. This will point to load_all_modules due to the way the dylib was constructed.␊ |
79 | execute_hook("ModulesLoaded", NULL, NULL, NULL, NULL);␊ |
80 | DBG("Module %s Loaded.\n", SYMBOLS_MODULE);␊ |
81 | retVal = 1;␊ |
82 | ␊ |
83 | }␊ |
84 | else␊ |
85 | {␊ |
86 | // The module does not have a valid start function␊ |
87 | printf("Unable to start %s\n", SYMBOLS_MODULE); getc();␊ |
88 | }␉␉␊ |
89 | ␉␉}␉␉␊ |
90 | ␉}␊ |
91 | ␉return retVal;␊ |
92 | }␊ |
93 | ␊ |
94 | ␊ |
95 | /*␊ |
96 | * Load all modules in the /Extra/modules/ directory␊ |
97 | * Module depencdies will be loaded first␊ |
98 | * Modules will only be loaded once. When loaded a module must␊ |
99 | * setup apropriete function calls and hooks as required.␊ |
100 | * NOTE: To ensure a module loads after another you may ␊ |
101 | * link one module with the other. For dyld to allow this, you must␊ |
102 | * reference at least one symbol within the module.␊ |
103 | */␊ |
104 | void load_all_modules()␊ |
105 | {␊ |
106 | ␉char* name;␊ |
107 | ␉long flags;␊ |
108 | ␉long time;␊ |
109 | ␉struct dirstuff* moduleDir = opendir("/Extra/modules/");␊ |
110 | ␉while(readdir(moduleDir, (const char**)&name, &flags, &time) >= 0)␊ |
111 | ␉{␊ |
112 | ␉␉if(strcmp(&name[strlen(name) - sizeof("dylib")], ".dylib") == 0)␊ |
113 | ␉␉{␊ |
114 | ␉␉␉char* tmp = malloc(strlen(name) + 1);␊ |
115 | ␉␉␉strcpy(tmp, name);␊ |
116 | ␉␉␉␊ |
117 | ␉␉␉if(!load_module(tmp))␊ |
118 | ␉␉␉{␊ |
119 | ␉␉␉␉// failed to load␊ |
120 | ␉␉␉␉// free(tmp);␊ |
121 | ␉␉␉}␊ |
122 | ␉␉}␊ |
123 | ␉␉else ␊ |
124 | ␉␉{␊ |
125 | ␉␉␉DBG("Ignoring %s\n", name);␊ |
126 | ␉␉}␊ |
127 | ␊ |
128 | ␉}␊ |
129 | }␊ |
130 | ␊ |
131 | ␊ |
132 | /*␊ |
133 | * Load a module file in /Extra/modules/␊ |
134 | */␊ |
135 | int load_module(char* module)␊ |
136 | {␊ |
137 | ␉int retVal = 1;␊ |
138 | ␉void (*module_start)(void) = NULL;␊ |
139 | ␉char modString[128];␊ |
140 | ␉int fh = -1;␊ |
141 | ␊ |
142 | ␉// Check to see if the module has already been loaded␊ |
143 | ␉if(is_module_loaded(module))␊ |
144 | ␉{␊ |
145 | ␉␉return 1;␊ |
146 | ␉}␊ |
147 | ␉␊ |
148 | ␉sprintf(modString, MODULE_PATH "%s", module);␊ |
149 | ␉fh = open(modString, 0);␊ |
150 | ␉if(fh < 0)␊ |
151 | ␉{␊ |
152 | ␉␉DBG("WARNING: Unable to locate module %s\n", modString); DBGPAUSE();␊ |
153 | ␉␉return 0;␊ |
154 | ␉}␊ |
155 | ␉␊ |
156 | ␉unsigned int moduleSize = file_size(fh);␊ |
157 | ␉char* module_base = (char*) malloc(moduleSize);␊ |
158 | ␉if (moduleSize && read(fh, module_base, moduleSize) == moduleSize)␊ |
159 | ␉{␊ |
160 | ␉␉// Module loaded into memory, parse it␊ |
161 | ␉␉module_start = parse_mach(module_base, &load_module, &add_symbol);␊ |
162 | ␊ |
163 | ␉␉if(module_start && module_start != (void*)0xFFFFFFFF)␊ |
164 | ␉␉{␊ |
165 | ␉␉␉// Notify the system that it was laoded␊ |
166 | ␉␉␉module_loaded(module/*moduleName, moduleVersion, moduleCompat*/);␊ |
167 | ␉␉␉(*module_start)();␉// Start the module␊ |
168 | ␉␉␉DBG("Module %s Loaded.\n", module); DBGPAUSE();␊ |
169 | ␉␉}␊ |
170 | #if DEBUG_MODULES␊ |
171 | ␉␉else // The module does not have a valid start function. This may be a library.␊ |
172 | ␉␉{␊ |
173 | ␉␉␉printf("WARNING: Unable to start %s\n", module);␊ |
174 | ␉␉␉getc();␊ |
175 | ␉␉}␊ |
176 | #else␊ |
177 | ␉␉else msglog("WARNING: Unable to start %s\n", module);␊ |
178 | #endif␊ |
179 | ␉}␊ |
180 | ␉else␊ |
181 | ␉{␊ |
182 | ␉␉DBG("Unable to read in module %s\n.", module); DBGPAUSE();␊ |
183 | ␉␉retVal = 0;␊ |
184 | ␉}␊ |
185 | ␊ |
186 | ␉close(fh);␊ |
187 | ␉return retVal;␊ |
188 | }␊ |
189 | ␊ |
190 | /*␊ |
191 | * add_symbol␊ |
192 | * This function adds a symbol from a module to the list of known symbols ␊ |
193 | * possibly change to a pointer and add this to the Symbol module so that it can␊ |
194 | * adjust it's internal symbol list (sort) to optimize locating new symbols␊ |
195 | * NOTE: returns the address if the symbol is "start", else returns 0xFFFFFFFF␊ |
196 | */␊ |
197 | long long add_symbol(char* symbol, long long addr, char is64)␊ |
198 | {␊ |
199 | ␉if(is64) return 0xFFFFFFFF; // Fixme␊ |
200 | ␊ |
201 | ␉// This only can handle 32bit symbols ␊ |
202 | ␉symbolList_t* entry;␊ |
203 | ␉//DBG("Adding symbol %s at 0x%X\n", symbol, addr);␊ |
204 | ␉␊ |
205 | ␉entry = malloc(sizeof(symbolList_t));␊ |
206 | ␉entry->next = moduleSymbols;␊ |
207 | ␉moduleSymbols = entry;␊ |
208 | ␉␊ |
209 | ␉entry->addr = (UInt32)addr;␊ |
210 | ␉entry->symbol = symbol;␊ |
211 | ␉␊ |
212 | ␉if(strcmp(symbol, "start") == 0)␊ |
213 | ␉{␊ |
214 | ␉␉return addr;␊ |
215 | ␉}␊ |
216 | ␉else␊ |
217 | ␉{␊ |
218 | ␉␉return 0xFFFFFFFF; // fixme␊ |
219 | ␉}␊ |
220 | }␊ |
221 | ␊ |
222 | ␊ |
223 | /*␊ |
224 | * print out the information about the loaded module␊ |
225 | */␊ |
226 | void module_loaded(const char* name/*, UInt32 version, UInt32 compat*/)␊ |
227 | {␊ |
228 | ␉moduleList_t* new_entry = malloc(sizeof(moduleList_t));␊ |
229 | ␉new_entry->next = loadedModules;␊ |
230 | ␊ |
231 | ␉loadedModules = new_entry;␊ |
232 | ␉␊ |
233 | ␉new_entry->name = (char*)name;␊ |
234 | //␉new_entry->version = version;␊ |
235 | //␉new_entry->compat = compat;␊ |
236 | }␊ |
237 | ␊ |
238 | int is_module_loaded(const char* name)␊ |
239 | {␊ |
240 | ␉// todo sorted search␊ |
241 | ␉moduleList_t* entry = loadedModules;␊ |
242 | ␉while(entry)␊ |
243 | ␉{␊ |
244 | ␉␉if(strcmp(entry->name, name) == 0)␊ |
245 | ␉␉{␊ |
246 | ␉␉␉DBG("Located module %s\n", name); DBGPAUSE();␊ |
247 | ␉␉␉return 1;␊ |
248 | ␉␉}␊ |
249 | ␉␉else␊ |
250 | ␉␉{␊ |
251 | ␉␉␉entry = entry->next;␊ |
252 | ␉␉}␊ |
253 | ␊ |
254 | ␉}␊ |
255 | ␉␊ |
256 | ␉DBG("Module %s not found\n", name); DBGPAUSE();␊ |
257 | ␉return 0;␊ |
258 | }␊ |
259 | ␊ |
260 | /*␊ |
261 | *␉lookup symbols in all loaded modules. Thins inludes boot syms due to Symbols.dylib construction␊ |
262 | *␊ |
263 | */␊ |
264 | unsigned int lookup_all_symbols(const char* name)␊ |
265 | {␊ |
266 | ␉symbolList_t* entry = moduleSymbols;␊ |
267 | ␉while(entry)␊ |
268 | ␉{␊ |
269 | ␉␉if(strcmp(entry->symbol, name) == 0)␊ |
270 | ␉␉{␊ |
271 | ␉␉␉//DBG("External symbol %s located at 0x%X\n", name, entry->addr);␊ |
272 | ␉␉␉return entry->addr;␊ |
273 | ␉␉}␊ |
274 | ␉␉else␊ |
275 | ␉␉{␊ |
276 | ␉␉␉entry = entry->next;␊ |
277 | ␉␉}␊ |
278 | ␉}␊ |
279 | ␉␊ |
280 | #if DEBUG_MODULES␊ |
281 | ␉verbose("Unable to locate symbol %s\n", name);␊ |
282 | ␉getc();␊ |
283 | #endif␊ |
284 | ␉␊ |
285 | ␉if(strcmp(name, VOID_SYMBOL) == 0) return 0xFFFFFFFF;␊ |
286 | ␉// In the event that a symbol does not exist␊ |
287 | ␉// Return a pointer to a void function.␊ |
288 | ␉else return lookup_all_symbols(VOID_SYMBOL);␉␊ |
289 | }␊ |
290 | ␊ |
291 | /********************************************************************************/␊ |
292 | /*␉Macho Parser␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉*/␊ |
293 | /********************************************************************************/␊ |
294 | ␊ |
295 | /*␊ |
296 | * Parse through a macho module. The module will be rebased and binded␊ |
297 | * as specified in the macho header. If the module is sucessfuly laoded␊ |
298 | * the module iinit address will be returned.␊ |
299 | * NOTE; all dependecies will be loaded before this module is started␊ |
300 | * NOTE: If the module is unable to load ot completeion, the modules␊ |
301 | * symbols will still be available.␊ |
302 | */␊ |
303 | void* parse_mach(void* binary, int(*dylib_loader)(char*), long long(*symbol_handler)(char*, long long, char))␊ |
304 | {␉␊ |
305 | ␉char is64 = false;␊ |
306 | ␉void (*module_start)(void) = NULL;␊ |
307 | ␉␊ |
308 | ␉// Module info␊ |
309 | ␉/*char* moduleName = NULL;␊ |
310 | ␉ UInt32 moduleVersion = 0;␊ |
311 | ␉ UInt32 moduleCompat = 0;␊ |
312 | ␉ */␊ |
313 | ␉// TODO convert all of the structs to a union␊ |
314 | ␉struct load_command *loadCommand = NULL;␊ |
315 | ␉struct dylib_command* dylibCommand = NULL;␊ |
316 | ␉struct dyld_info_command* dyldInfoCommand = NULL;␊ |
317 | ␉␊ |
318 | ␉struct symtab_command* symtabCommand = NULL;␊ |
319 | ␉struct segment_command *segCommand = NULL;␊ |
320 | ␉struct segment_command_64 *segCommand64 = NULL;␊ |
321 | ␉␊ |
322 | ␉//struct dysymtab_command* dysymtabCommand = NULL;␊ |
323 | ␉UInt32 binaryIndex = 0;␊ |
324 | ␉UInt16 cmd = 0;␊ |
325 | ␉␊ |
326 | ␉textSection = 0;␊ |
327 | ␉textAddress = 0;␉// reinitialize text location in case it doesn't exist;␊ |
328 | ␉␊ |
329 | ␉// Parse through the load commands␊ |
330 | ␉if(((struct mach_header*)binary)->magic == MH_MAGIC)␊ |
331 | ␉{␊ |
332 | ␉␉is64 = false;␊ |
333 | ␉␉binaryIndex += sizeof(struct mach_header);␊ |
334 | ␉}␊ |
335 | ␉else if(((struct mach_header_64*)binary)->magic == MH_MAGIC_64)␊ |
336 | ␉{␊ |
337 | ␉␉// NOTE: modules cannot be 64bit...␊ |
338 | ␉␉is64 = true;␊ |
339 | ␉␉binaryIndex += sizeof(struct mach_header_64);␊ |
340 | ␉}␊ |
341 | ␉else␊ |
342 | ␉{␊ |
343 | ␉␉//Azi: this is double printing when using kernelcache - that's new!␊ |
344 | ␉␉verbose("Invalid mach magic 0x%X\n", ((struct mach_header*)binary)->magic);␊ |
345 | ␉␉//getc();␊ |
346 | ␉␉return NULL;␊ |
347 | ␉}␊ |
348 | ␉␊ |
349 | ␉␊ |
350 | ␉␊ |
351 | ␉/*if(((struct mach_header*)binary)->filetype != MH_DYLIB)␊ |
352 | ␉ {␊ |
353 | ␉ printf("Module is not a dylib. Unable to load.\n");␊ |
354 | ␉ getc();␊ |
355 | ␉ return NULL; // Module is in the incorrect format␊ |
356 | ␉ }*/␊ |
357 | ␉␊ |
358 | ␉while(cmd < ((struct mach_header*)binary)->ncmds)␊ |
359 | ␉{␊ |
360 | ␉␉cmd++;␊ |
361 | ␉␉␊ |
362 | ␉␉loadCommand = binary + binaryIndex;␊ |
363 | ␉␉UInt32 cmdSize = loadCommand->cmdsize;␊ |
364 | ␉␉␊ |
365 | ␉␉␊ |
366 | ␉␉switch ((loadCommand->cmd & 0x7FFFFFFF))␊ |
367 | ␉␉{␊ |
368 | ␉␉␉case LC_SYMTAB:␊ |
369 | ␉␉␉␉symtabCommand = binary + binaryIndex;␊ |
370 | ␉␉␉␉break;␊ |
371 | ␉␉␉␉␊ |
372 | ␉␉␉case LC_SEGMENT: // 32bit macho␊ |
373 | ␉␉␉␉segCommand = binary + binaryIndex;␊ |
374 | ␉␉␉␉␊ |
375 | ␉␉␉␉if(strcmp("__TEXT", segCommand->segname) == 0)␊ |
376 | ␉␉␉␉{␊ |
377 | ␉␉␉␉␉UInt32 sectionIndex;␊ |
378 | ␉␉␉␉␉␊ |
379 | ␉␉␉␉␉sectionIndex = sizeof(struct segment_command);␊ |
380 | ␉␉␉␉␉␊ |
381 | ␉␉␉␉␉struct section *sect;␊ |
382 | ␉␉␉␉␉␊ |
383 | ␉␉␉␉␉while(sectionIndex < segCommand->cmdsize)␊ |
384 | ␉␉␉␉␉{␊ |
385 | ␉␉␉␉␉␉sect = binary + binaryIndex + sectionIndex;␊ |
386 | ␉␉␉␉␉␉␊ |
387 | ␉␉␉␉␉␉sectionIndex += sizeof(struct section);␊ |
388 | ␉␉␉␉␉␉␊ |
389 | ␉␉␉␉␉␉␊ |
390 | ␉␉␉␉␉␉if(strcmp("__text", sect->sectname) == 0)␊ |
391 | ␉␉␉␉␉␉{␊ |
392 | ␉␉␉␉␉␉␉// __TEXT,__text found, save the offset and address for when looking for the calls.␊ |
393 | ␉␉␉␉␉␉␉textSection = sect->offset;␊ |
394 | ␉␉␉␉␉␉␉textAddress = sect->addr;␊ |
395 | ␉␉␉␉␉␉␉break;␊ |
396 | ␉␉␉␉␉␉}␉␉␉␉␉␊ |
397 | ␉␉␉␉␉}␊ |
398 | ␉␉␉␉}␊ |
399 | ␉␉␉␉␊ |
400 | ␉␉␉␉break;␊ |
401 | ␉␉␉case LC_SEGMENT_64:␉// 64bit macho's␊ |
402 | ␉␉␉␉segCommand64 = binary + binaryIndex;␉␉␉␉␊ |
403 | ␉␉␉␉if(strcmp("__TEXT", segCommand64->segname) == 0)␊ |
404 | ␉␉␉␉{␊ |
405 | ␉␉␉␉␉UInt32 sectionIndex;␊ |
406 | ␉␉␉␉␉␊ |
407 | ␉␉␉␉␉sectionIndex = sizeof(struct segment_command_64);␊ |
408 | ␉␉␉␉␉␊ |
409 | ␉␉␉␉␉struct section_64 *sect;␊ |
410 | ␉␉␉␉␉␊ |
411 | ␉␉␉␉␉while(sectionIndex < segCommand64->cmdsize)␊ |
412 | ␉␉␉␉␉{␊ |
413 | ␉␉␉␉␉␉sect = binary + binaryIndex + sectionIndex;␊ |
414 | ␉␉␉␉␉␉␊ |
415 | ␉␉␉␉␉␉sectionIndex += sizeof(struct section_64);␊ |
416 | ␉␉␉␉␉␉␊ |
417 | ␉␉␉␉␉␉␊ |
418 | ␉␉␉␉␉␉if(strcmp("__text", sect->sectname) == 0)␊ |
419 | ␉␉␉␉␉␉{␊ |
420 | ␉␉␉␉␉␉␉// __TEXT,__text found, save the offset and address for when looking for the calls.␊ |
421 | ␉␉␉␉␉␉␉textSection = sect->offset;␊ |
422 | ␉␉␉␉␉␉␉textAddress = sect->addr;␊ |
423 | ␉␉␉␉␉␉␉␊ |
424 | ␉␉␉␉␉␉␉break;␊ |
425 | ␉␉␉␉␉␉}␉␉␉␉␉␊ |
426 | ␉␉␉␉␉}␊ |
427 | ␉␉␉␉}␉␉␉␉␊ |
428 | ␉␉␉␉␊ |
429 | ␉␉␉␉break;␊ |
430 | ␉␉␉␉␊ |
431 | ␉␉␉␉␊ |
432 | ␉␉␉case LC_LOAD_DYLIB:␊ |
433 | ␉␉␉case LC_LOAD_WEAK_DYLIB ^ LC_REQ_DYLD:␊ |
434 | ␉␉␉␉dylibCommand = binary + binaryIndex;␊ |
435 | ␉␉␉␉char* module = binary + binaryIndex + ((UInt32)*((UInt32*)&dylibCommand->dylib.name));␊ |
436 | ␉␉␉␉// Possible enhancments: verify version␊ |
437 | ␉␉␉␉// =␉dylibCommand->dylib.current_version;␊ |
438 | ␉␉␉␉// =␉dylibCommand->dylib.compatibility_version;␊ |
439 | ␉␉␉␉if(dylib_loader)␊ |
440 | ␉␉␉␉{␊ |
441 | ␉␉␉␉␉char* name = malloc(strlen(module) + strlen(".dylib") + 1);␊ |
442 | ␉␉␉␉␉sprintf(name, "%s.dylib", module);␊ |
443 | ␉␉␉␉␉␊ |
444 | ␉␉␉␉␉if (!dylib_loader(name))␊ |
445 | ␉␉␉␉␉{␊ |
446 | ␉␉␉␉␉␉// NOTE: any symbols exported by dep will be replace with the void function␊ |
447 | ␉␉␉␉␉␉free(name);␊ |
448 | ␉␉␉␉␉}␊ |
449 | ␉␉␉␉}␊ |
450 | ␉␉␉␉␊ |
451 | ␉␉␉␉break;␊ |
452 | ␉␉␉␉␊ |
453 | ␉␉␉case LC_ID_DYLIB:␊ |
454 | ␉␉␉␉dylibCommand = binary + binaryIndex;␊ |
455 | ␉␉␉␉/*moduleName =␉binary + binaryIndex + ((UInt32)*((UInt32*)&dylibCommand->dylib.name));␊ |
456 | ␉␉␉␉ moduleVersion =␉dylibCommand->dylib.current_version;␊ |
457 | ␉␉␉␉ moduleCompat =␉dylibCommand->dylib.compatibility_version;␊ |
458 | ␉␉␉␉ */␊ |
459 | ␉␉␉␉break;␊ |
460 | ␉␉␉␉␊ |
461 | ␉␉␉case LC_DYLD_INFO:␊ |
462 | ␉␉␉//case LC_DYLD_INFO_ONLY:␉// compressed info, 10.6+ macho files, already handeled␊ |
463 | ␉␉␉␉// Bind and rebase info is stored here␊ |
464 | ␉␉␉␉dyldInfoCommand = binary + binaryIndex;␊ |
465 | ␉␉␉␉break;␊ |
466 | ␉␉␉␉␊ |
467 | ␉␉␉case LC_DYSYMTAB:␊ |
468 | ␉␉␉case LC_UUID:␊ |
469 | ␉␉␉case LC_UNIXTHREAD:␊ |
470 | ␉␉␉␉break;␊ |
471 | ␉␉␉␉␊ |
472 | ␉␉␉default:␊ |
473 | ␉␉␉␉DBG("Unhandled loadcommand 0x%X\n", loadCommand->cmd & 0x7FFFFFFF);␊ |
474 | ␉␉␉␉break;␊ |
475 | ␉␉␉␉␊ |
476 | ␉␉}␊ |
477 | ␉␉␊ |
478 | ␉␉binaryIndex += cmdSize;␊ |
479 | ␉}␊ |
480 | ␊ |
481 | ␉// bind_macho uses the symbols, if the textAdd does not exist (Symbols.dylib, no code), addresses are static and not relative␊ |
482 | ␉module_start = (void*)handle_symtable((UInt32)binary, symtabCommand, symbol_handler, is64);␊ |
483 | ␉␊ |
484 | ␉if(dyldInfoCommand)␊ |
485 | ␉{␊ |
486 | ␉␉// Rebase the module before binding it.␊ |
487 | ␉␉if(dyldInfoCommand->rebase_off)␉␉rebase_macho(binary, (char*)dyldInfoCommand->rebase_off,␉dyldInfoCommand->rebase_size);␊ |
488 | ␉␉// Bind all symbols. ␊ |
489 | ␉␉if(dyldInfoCommand->bind_off)␉␉bind_macho(binary, (char*)dyldInfoCommand->bind_off,␉␉dyldInfoCommand->bind_size);␊ |
490 | ␉␉if(dyldInfoCommand->weak_bind_off)␉bind_macho(binary, (char*)dyldInfoCommand->weak_bind_off,␉dyldInfoCommand->weak_bind_size);␊ |
491 | ␉␉if(dyldInfoCommand->lazy_bind_off)␉bind_macho(binary, (char*)dyldInfoCommand->lazy_bind_off,␉dyldInfoCommand->lazy_bind_size);␊ |
492 | ␉}␊ |
493 | ␉␊ |
494 | ␉return module_start;␊ |
495 | ␉␊ |
496 | }␊ |
497 | ␊ |
498 | /*␊ |
499 | * parse the symbol table␊ |
500 | * Lookup any undefined symbols␊ |
501 | */␊ |
502 | ␊ |
503 | unsigned int handle_symtable(UInt32 base, struct symtab_command* symtabCommand, long long(*symbol_handler)(char*, long long, char), char is64)␊ |
504 | {␊ |
505 | ␉unsigned int module_start␉= 0xFFFFFFFF;␉␊ |
506 | ␉UInt32 symbolIndex␉␉␉= 0;␊ |
507 | ␉char* symbolString␉␉␉= base + (char*)symtabCommand->stroff;␊ |
508 | ␊ |
509 | ␉if(!is64)␊ |
510 | ␉{␊ |
511 | ␉␉struct nlist* symbolEntry = (void*)base + symtabCommand->symoff;␊ |
512 | ␉␉while(symbolIndex < symtabCommand->nsyms)␊ |
513 | ␉␉{␊ |
514 | ␉␉␉// If the symbol is exported by this module␊ |
515 | ␉␉␉if(symbolEntry->n_value &&␊ |
516 | ␉␉␉ symbol_handler(symbolString + symbolEntry->n_un.n_strx, textAddress ? (long long)base + symbolEntry->n_value : symbolEntry->n_value, is64) != 0xFFFFFFFF)␊ |
517 | ␉␉␉{␊ |
518 | ␉␉␉␉␊ |
519 | ␉␉␉␉// Module start located. Start is an alias so don't register it␊ |
520 | ␉␉␉␉module_start = textAddress ? base + symbolEntry->n_value : symbolEntry->n_value;␊ |
521 | ␉␉␉}␊ |
522 | ␉␉␉␊ |
523 | ␉␉␉symbolEntry++;␊ |
524 | ␉␉␉symbolIndex++;␉// TODO remove␊ |
525 | ␉␉}␊ |
526 | ␉}␊ |
527 | ␉else␊ |
528 | ␉{␊ |
529 | ␉␉struct nlist_64* symbolEntry = (void*)base + symtabCommand->symoff;␊ |
530 | ␉␉// NOTE First entry is *not* correct, but we can ignore it (i'm getting radar:// right now, verify later)␉␊ |
531 | ␉␉while(symbolIndex < symtabCommand->nsyms)␊ |
532 | ␉␉{␊ |
533 | ␉␉␉␊ |
534 | ␉␉␉␊ |
535 | ␉␉␉// If the symbol is exported by this module␊ |
536 | ␉␉␉if(symbolEntry->n_value &&␊ |
537 | ␉␉␉ symbol_handler(symbolString + symbolEntry->n_un.n_strx, textAddress ? (long long)base + symbolEntry->n_value : symbolEntry->n_value, is64) != 0xFFFFFFFF)␊ |
538 | ␉␉␉{␊ |
539 | ␉␉␉␉␊ |
540 | ␉␉␉␉// Module start located. Start is an alias so don't register it␊ |
541 | ␉␉␉␉module_start = textAddress ? base + symbolEntry->n_value : symbolEntry->n_value;␊ |
542 | ␉␉␉}␊ |
543 | ␉␉␉␊ |
544 | ␉␉␉symbolEntry++;␊ |
545 | ␉␉␉symbolIndex++;␉// TODO remove␊ |
546 | ␉␉}␊ |
547 | ␉}␊ |
548 | ␉return module_start;␊ |
549 | }␊ |
550 | ␊ |
551 | // Based on code from dylibinfo.cpp and ImageLoaderMachOCompressed.cpp␊ |
552 | void rebase_macho(void* base, char* rebase_stream, UInt32 size)␊ |
553 | {␊ |
554 | ␉rebase_stream += (UInt32)base;␊ |
555 | ␉␊ |
556 | ␉UInt8 immediate = 0;␊ |
557 | ␉UInt8 opcode = 0;␊ |
558 | ␉UInt8 type = 0;␊ |
559 | ␉UInt32 segmentAddress = 0;␊ |
560 | ␉␊ |
561 | ␉␊ |
562 | ␉UInt32 tmp = 0;␊ |
563 | ␉UInt32 tmp2 = 0;␊ |
564 | ␉UInt8 bits = 0;␊ |
565 | ␉int index = 0;␊ |
566 | ␉unsigned int i = 0;␊ |
567 | ␉␊ |
568 | ␉while(i < size)␊ |
569 | ␉{␊ |
570 | ␉␉immediate = rebase_stream[i] & REBASE_IMMEDIATE_MASK;␊ |
571 | ␉␉opcode = rebase_stream[i] & REBASE_OPCODE_MASK;␊ |
572 | ␉␉␊ |
573 | ␉␉␊ |
574 | ␉␉switch(opcode)␊ |
575 | ␉␉{␊ |
576 | ␉␉␉case REBASE_OPCODE_DONE:␊ |
577 | ␉␉␉␉// Rebase complete, reset vars␊ |
578 | ␉␉␉␉immediate = 0;␊ |
579 | ␉␉␉␉opcode = 0;␊ |
580 | ␉␉␉␉type = 0;␊ |
581 | ␉␉␉␉segmentAddress = 0;␊ |
582 | ␉␉␉default:␊ |
583 | ␉␉␉␉break;␊ |
584 | ␉␉␉␉␊ |
585 | ␉␉␉␉␊ |
586 | ␉␉␉case REBASE_OPCODE_SET_TYPE_IMM:␊ |
587 | ␉␉␉␉type = immediate;␊ |
588 | ␉␉␉␉break;␊ |
589 | ␉␉␉␉␊ |
590 | ␉␉␉␉␊ |
591 | ␉␉␉case REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB:␊ |
592 | ␉␉␉␉// Locate address to begin rebasing␊ |
593 | ␉␉␉␉segmentAddress = 0;␊ |
594 | ␉␉␉␉struct segment_command* segCommand = NULL; // NOTE: 32bit only␊ |
595 | ␉␉␉␉␊ |
596 | ␉␉␉␉unsigned int binIndex = 0;␊ |
597 | ␉␉␉␉index = 0;␊ |
598 | ␉␉␉␉do␊ |
599 | ␉␉␉␉{␊ |
600 | ␉␉␉␉␉segCommand = base + sizeof(struct mach_header) + binIndex;␊ |
601 | ␉␉␉␉␉␊ |
602 | ␉␉␉␉␉␊ |
603 | ␉␉␉␉␉binIndex += segCommand->cmdsize;␊ |
604 | ␉␉␉␉␉index++;␊ |
605 | ␉␉␉␉}␊ |
606 | ␉␉␉␉while(index <= immediate);␊ |
607 | ␉␉␉␉␊ |
608 | ␉␉␉␉␊ |
609 | ␉␉␉␉segmentAddress = segCommand->fileoff;␊ |
610 | ␉␉␉␉␊ |
611 | ␉␉␉␉tmp = 0;␊ |
612 | ␉␉␉␉bits = 0;␊ |
613 | ␉␉␉␉do␊ |
614 | ␉␉␉␉{␊ |
615 | ␉␉␉␉␉tmp |= (rebase_stream[++i] & 0x7f) << bits;␊ |
616 | ␉␉␉␉␉bits += 7;␊ |
617 | ␉␉␉␉}␊ |
618 | ␉␉␉␉while(rebase_stream[i] & 0x80);␊ |
619 | ␉␉␉␉␊ |
620 | ␉␉␉␉segmentAddress += tmp;␊ |
621 | ␉␉␉␉break;␊ |
622 | ␉␉␉␉␊ |
623 | ␉␉␉␉␊ |
624 | ␉␉␉case REBASE_OPCODE_ADD_ADDR_ULEB:␊ |
625 | ␉␉␉␉// Add value to rebase address␊ |
626 | ␉␉␉␉tmp = 0;␊ |
627 | ␉␉␉␉bits = 0;␊ |
628 | ␉␉␉␉do␊ |
629 | ␉␉␉␉{␊ |
630 | ␉␉␉␉␉tmp <<= bits;␊ |
631 | ␉␉␉␉␉tmp |= rebase_stream[++i] & 0x7f;␊ |
632 | ␉␉␉␉␉bits += 7;␊ |
633 | ␉␉␉␉}␊ |
634 | ␉␉␉␉while(rebase_stream[i] & 0x80);␊ |
635 | ␉␉␉␉␊ |
636 | ␉␉␉␉segmentAddress +=␉tmp; ␊ |
637 | ␉␉␉␉break;␊ |
638 | ␉␉␉␉␊ |
639 | ␉␉␉case REBASE_OPCODE_ADD_ADDR_IMM_SCALED:␊ |
640 | ␉␉␉␉segmentAddress += immediate * sizeof(void*);␊ |
641 | ␉␉␉␉break;␊ |
642 | ␉␉␉␉␊ |
643 | ␉␉␉␉␊ |
644 | ␉␉␉case REBASE_OPCODE_DO_REBASE_IMM_TIMES:␊ |
645 | ␉␉␉␉index = 0;␊ |
646 | ␉␉␉␉for (index = 0; index < immediate; ++index) {␊ |
647 | ␉␉␉␉␉rebase_location(base + segmentAddress, (char*)base, type);␊ |
648 | ␉␉␉␉␉segmentAddress += sizeof(void*);␊ |
649 | ␉␉␉␉}␊ |
650 | ␉␉␉␉break;␊ |
651 | ␉␉␉␉␊ |
652 | ␉␉␉␉␊ |
653 | ␉␉␉case REBASE_OPCODE_DO_REBASE_ULEB_TIMES:␊ |
654 | ␉␉␉␉tmp = 0;␊ |
655 | ␉␉␉␉bits = 0;␊ |
656 | ␉␉␉␉do␊ |
657 | ␉␉␉␉{␊ |
658 | ␉␉␉␉␉tmp |= (rebase_stream[++i] & 0x7f) << bits;␊ |
659 | ␉␉␉␉␉bits += 7;␊ |
660 | ␉␉␉␉}␊ |
661 | ␉␉␉␉while(rebase_stream[i] & 0x80);␊ |
662 | ␉␉␉␉␊ |
663 | ␉␉␉␉index = 0;␊ |
664 | ␉␉␉␉for (index = 0; index < tmp; ++index) {␊ |
665 | ␉␉␉␉␉//DBG("\tRebasing 0x%X\n", segmentAddress);␊ |
666 | ␉␉␉␉␉rebase_location(base + segmentAddress, (char*)base, type);␉␉␉␉␉␊ |
667 | ␉␉␉␉␉segmentAddress += sizeof(void*);␊ |
668 | ␉␉␉␉}␊ |
669 | ␉␉␉␉break;␊ |
670 | ␉␉␉␉␊ |
671 | ␉␉␉case REBASE_OPCODE_DO_REBASE_ADD_ADDR_ULEB:␊ |
672 | ␉␉␉␉tmp = 0;␊ |
673 | ␉␉␉␉bits = 0;␊ |
674 | ␉␉␉␉do␊ |
675 | ␉␉␉␉{␊ |
676 | ␉␉␉␉␉tmp |= (rebase_stream[++i] & 0x7f) << bits;␊ |
677 | ␉␉␉␉␉bits += 7;␊ |
678 | ␉␉␉␉}␊ |
679 | ␉␉␉␉while(rebase_stream[i] & 0x80);␊ |
680 | ␉␉␉␉␊ |
681 | ␉␉␉␉rebase_location(base + segmentAddress, (char*)base, type);␊ |
682 | ␉␉␉␉␊ |
683 | ␉␉␉␉segmentAddress += tmp + sizeof(void*);␊ |
684 | ␉␉␉␉break;␊ |
685 | ␉␉␉␉␊ |
686 | ␉␉␉case REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_ULEB:␊ |
687 | ␉␉␉␉tmp = 0;␊ |
688 | ␉␉␉␉bits = 0;␊ |
689 | ␉␉␉␉do␊ |
690 | ␉␉␉␉{␊ |
691 | ␉␉␉␉␉tmp |= (rebase_stream[++i] & 0x7f) << bits;␊ |
692 | ␉␉␉␉␉bits += 7;␊ |
693 | ␉␉␉␉}␊ |
694 | ␉␉␉␉while(rebase_stream[i] & 0x80);␊ |
695 | ␉␉␉␉␊ |
696 | ␉␉␉␉␊ |
697 | ␉␉␉␉tmp2 = 0;␊ |
698 | ␉␉␉␉bits = 0;␊ |
699 | ␉␉␉␉do␊ |
700 | ␉␉␉␉{␊ |
701 | ␉␉␉␉␉tmp2 |= (rebase_stream[++i] & 0x7f) << bits;␊ |
702 | ␉␉␉␉␉bits += 7;␊ |
703 | ␉␉␉␉}␊ |
704 | ␉␉␉␉while(rebase_stream[i] & 0x80);␊ |
705 | ␉␉␉␉␊ |
706 | ␉␉␉␉index = 0;␊ |
707 | ␉␉␉␉for (index = 0; index < tmp; ++index) {␊ |
708 | ␉␉␉␉␉␊ |
709 | ␉␉␉␉␉rebase_location(base + segmentAddress, (char*)base, type);␊ |
710 | ␉␉␉␉␉␊ |
711 | ␉␉␉␉␉segmentAddress += tmp2 + sizeof(void*);␊ |
712 | ␉␉␉␉}␊ |
713 | ␉␉␉␉break;␊ |
714 | ␉␉}␊ |
715 | ␉␉i++;␊ |
716 | ␉}␊ |
717 | }␊ |
718 | ␊ |
719 | inline void rebase_location(UInt32* location, char* base, int type)␊ |
720 | {␉␊ |
721 | ␉switch(type)␊ |
722 | ␉{␊ |
723 | ␉␉case REBASE_TYPE_POINTER:␊ |
724 | ␉␉case REBASE_TYPE_TEXT_ABSOLUTE32:␊ |
725 | ␉␉␉*location += (UInt32)base;␊ |
726 | ␉␉␉break;␊ |
727 | ␉␉␉␊ |
728 | ␉␉default:␊ |
729 | ␉␉␉break;␊ |
730 | ␉}␊ |
731 | }␊ |
732 | ␊ |
733 | ␊ |
734 | // Based on code from dylibinfo.cpp and ImageLoaderMachOCompressed.cpp␊ |
735 | // NOTE: this uses 32bit values, and not 64bit values. ␊ |
736 | // There is a possibility that this could cause issues,␊ |
737 | // however the modules are 32 bits, so it shouldn't matter too much␊ |
738 | void bind_macho(void* base, char* bind_stream, UInt32 size)␊ |
739 | {␉␊ |
740 | ␉bind_stream += (UInt32)base;␊ |
741 | ␉␊ |
742 | ␉UInt8 immediate = 0;␊ |
743 | ␉UInt8 opcode = 0;␊ |
744 | ␉UInt8 type = BIND_TYPE_POINTER;␊ |
745 | ␉␊ |
746 | ␉UInt32 segmentAddress = 0;␊ |
747 | ␉␊ |
748 | ␉UInt32 address = 0;␊ |
749 | ␉␊ |
750 | ␉SInt32 addend = 0;␊ |
751 | ␉SInt32 libraryOrdinal = 0;␊ |
752 | ␉␊ |
753 | ␉const char* symbolName = NULL;␊ |
754 | ␉UInt8 symboFlags = 0;␊ |
755 | ␉UInt32 symbolAddr = 0xFFFFFFFF;␊ |
756 | ␉␊ |
757 | ␉// Temperary variables␊ |
758 | ␉UInt8 bits = 0;␊ |
759 | ␉UInt32 tmp = 0;␊ |
760 | ␉UInt32 tmp2 = 0;␊ |
761 | ␉UInt32 index = 0;␊ |
762 | ␉unsigned int i = 0;␊ |
763 | ␉␊ |
764 | ␉while(i < size)␊ |
765 | ␉{␊ |
766 | ␉␉immediate = bind_stream[i] & BIND_IMMEDIATE_MASK;␊ |
767 | ␉␉opcode = bind_stream[i] & BIND_OPCODE_MASK;␊ |
768 | ␉␉␊ |
769 | ␉␉␊ |
770 | ␉␉switch(opcode)␊ |
771 | ␉␉{␊ |
772 | ␉␉␉case BIND_OPCODE_DONE:␊ |
773 | ␉␉␉␉// reset vars␊ |
774 | ␉␉␉␉type = BIND_TYPE_POINTER;␊ |
775 | ␉␉␉␉segmentAddress = 0;␊ |
776 | ␉␉␉␉address = 0;␊ |
777 | ␉␉␉␉addend = 0;␊ |
778 | ␉␉␉␉libraryOrdinal = 0;␊ |
779 | ␉␉␉␉symbolAddr = 0xFFFFFFFF;␊ |
780 | ␉␉␉default:␊ |
781 | ␉␉␉␉break;␊ |
782 | ␉␉␉␉␊ |
783 | ␉␉␉case BIND_OPCODE_SET_DYLIB_ORDINAL_IMM:␊ |
784 | ␉␉␉␉libraryOrdinal = immediate;␊ |
785 | ␉␉␉␉break;␊ |
786 | ␉␉␉␉␊ |
787 | ␉␉␉case BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB:␊ |
788 | ␉␉␉␉libraryOrdinal = 0;␊ |
789 | ␉␉␉␉bits = 0;␊ |
790 | ␉␉␉␉do␊ |
791 | ␉␉␉␉{␊ |
792 | ␉␉␉␉␉libraryOrdinal |= (bind_stream[++i] & 0x7f) << bits;␊ |
793 | ␉␉␉␉␉bits += 7;␊ |
794 | ␉␉␉␉}␊ |
795 | ␉␉␉␉while(bind_stream[i] & 0x80);␊ |
796 | ␉␉␉␉break;␊ |
797 | ␉␉␉␉␊ |
798 | ␉␉␉case BIND_OPCODE_SET_DYLIB_SPECIAL_IMM:␊ |
799 | ␉␉␉␉libraryOrdinal = immediate ? (SInt8)(BIND_OPCODE_MASK | immediate) : immediate;␉␉␉␉␊ |
800 | ␉␉␉␉break;␊ |
801 | ␉␉␉␉␊ |
802 | ␉␉␉case BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM:␊ |
803 | ␉␉␉␉symboFlags = immediate;␊ |
804 | ␉␉␉␉symbolName = (char*)&bind_stream[++i];␊ |
805 | ␉␉␉␉i += strlen((char*)&bind_stream[i]);␊ |
806 | ␊ |
807 | ␉␉␉␉symbolAddr = lookup_all_symbols(symbolName);␊ |
808 | ␉␉␉␉break;␊ |
809 | ␉␉␉␉␊ |
810 | ␉␉␉case BIND_OPCODE_SET_TYPE_IMM:␊ |
811 | ␉␉␉␉type = immediate;␊ |
812 | ␉␉␉␉break;␊ |
813 | ␉␉␉␉␊ |
814 | ␉␉␉case BIND_OPCODE_SET_ADDEND_SLEB:␊ |
815 | ␉␉␉␉addend = 0;␊ |
816 | ␉␉␉␉bits = 0;␊ |
817 | ␉␉␉␉do␊ |
818 | ␉␉␉␉{␊ |
819 | ␉␉␉␉␉addend |= (bind_stream[++i] & 0x7f) << bits;␊ |
820 | ␉␉␉␉␉bits += 7;␊ |
821 | ␉␉␉␉}␊ |
822 | ␉␉␉␉while(bind_stream[i] & 0x80);␊ |
823 | ␉␉␉␉␊ |
824 | ␉␉␉␉if(!(bind_stream[i-1] & 0x40)) addend *= -1;␊ |
825 | ␉␉␉␉break;␊ |
826 | ␉␉␉␉␊ |
827 | ␉␉␉case BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB:␊ |
828 | ␉␉␉␉segmentAddress = 0;␊ |
829 | ␉␉␉␉␊ |
830 | ␉␉␉␉// Locate address␊ |
831 | ␉␉␉␉struct segment_command* segCommand = NULL;␉// NOTE: 32bit only␊ |
832 | ␉␉␉␉␊ |
833 | ␉␉␉␉unsigned int binIndex = 0;␊ |
834 | ␉␉␉␉index = 0;␊ |
835 | ␉␉␉␉do␊ |
836 | ␉␉␉␉{␊ |
837 | ␉␉␉␉␉segCommand = base + sizeof(struct mach_header) + binIndex;␊ |
838 | ␉␉␉␉␉binIndex += segCommand->cmdsize;␊ |
839 | ␉␉␉␉␉index++;␊ |
840 | ␉␉␉␉}␊ |
841 | ␉␉␉␉while(index <= immediate);␊ |
842 | ␉␉␉␉␊ |
843 | ␉␉␉␉segmentAddress = segCommand->fileoff;␊ |
844 | ␉␉␉␉␊ |
845 | ␉␉␉␉// Read in offset␊ |
846 | ␉␉␉␉tmp = 0;␊ |
847 | ␉␉␉␉bits = 0;␊ |
848 | ␉␉␉␉do␊ |
849 | ␉␉␉␉{␊ |
850 | ␉␉␉␉␉tmp |= (bind_stream[++i] & 0x7f) << bits;␊ |
851 | ␉␉␉␉␉bits += 7;␊ |
852 | ␉␉␉␉}␊ |
853 | ␉␉␉␉while(bind_stream[i] & 0x80);␊ |
854 | ␉␉␉␉␊ |
855 | ␉␉␉␉segmentAddress += tmp;␊ |
856 | ␉␉␉␉break;␊ |
857 | ␉␉␉␉␊ |
858 | ␉␉␉case BIND_OPCODE_ADD_ADDR_ULEB:␊ |
859 | ␉␉␉␉// Read in offset␊ |
860 | ␉␉␉␉tmp = 0;␊ |
861 | ␉␉␉␉bits = 0;␊ |
862 | ␉␉␉␉do␊ |
863 | ␉␉␉␉{␊ |
864 | ␉␉␉␉␉tmp |= (bind_stream[++i] & 0x7f) << bits;␊ |
865 | ␉␉␉␉␉bits += 7;␊ |
866 | ␉␉␉␉}␊ |
867 | ␉␉␉␉while(bind_stream[i] & 0x80);␊ |
868 | ␉␉␉␉␊ |
869 | ␉␉␉␉segmentAddress += tmp;␊ |
870 | ␉␉␉␉break;␊ |
871 | ␉␉␉␉␊ |
872 | ␉␉␉case BIND_OPCODE_DO_BIND:␊ |
873 | ␉␉␉␉if(symbolAddr != 0xFFFFFFFF)␊ |
874 | ␉␉␉␉{␊ |
875 | ␉␉␉␉␉address = segmentAddress + (UInt32)base;␊ |
876 | ␉␉␉␉␉␉␊ |
877 | ␉␉␉␉␉bind_location((UInt32*)address, (char*)symbolAddr, addend, type);␊ |
878 | ␉␉␉␉}␊ |
879 | ␉␉␉␉else␊ |
880 | ␉␉␉␉{␊ |
881 | ␉␉␉␉␉printf("Unable to bind symbol %s\n", symbolName);␊ |
882 | ␉␉␉␉␉getc();␊ |
883 | ␉␉␉␉}␊ |
884 | ␉␉␉␉␊ |
885 | ␉␉␉␉segmentAddress += sizeof(void*);␊ |
886 | ␉␉␉␉break;␊ |
887 | ␉␉␉␉␊ |
888 | ␉␉␉case BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB:␊ |
889 | ␉␉␉␉// Read in offset␊ |
890 | ␉␉␉␉tmp = 0;␊ |
891 | ␉␉␉␉bits = 0;␊ |
892 | ␉␉␉␉do␊ |
893 | ␉␉␉␉{␊ |
894 | ␉␉␉␉␉tmp |= (bind_stream[++i] & 0x7f) << bits;␊ |
895 | ␉␉␉␉␉bits += 7;␊ |
896 | ␉␉␉␉}␊ |
897 | ␉␉␉␉while(bind_stream[i] & 0x80);␊ |
898 | ␊ |
899 | ␉␉␉␉if(symbolAddr != 0xFFFFFFFF)␊ |
900 | ␉␉␉␉{␊ |
901 | ␉␉␉␉␉address = segmentAddress + (UInt32)base;␊ |
902 | ␉␉␉␉␉␊ |
903 | ␉␉␉␉␉bind_location((UInt32*)address, (char*)symbolAddr, addend, type);␊ |
904 | ␉␉␉␉}␊ |
905 | ␉␉␉␉else␊ |
906 | ␉␉␉␉{␊ |
907 | ␉␉␉␉␉printf("Unable to bind symbol %s\n", symbolName);␊ |
908 | ␉␉␉␉␉getc();␊ |
909 | ␉␉␉␉}␊ |
910 | ␊ |
911 | ␉␉␉␉segmentAddress += tmp + sizeof(void*);␊ |
912 | ␉␉␉␉␊ |
913 | ␉␉␉␉␊ |
914 | ␉␉␉␉break;␊ |
915 | ␉␉␉␉␊ |
916 | ␉␉␉case BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED:␊ |
917 | ␉␉␉␉if(symbolAddr != 0xFFFFFFFF)␊ |
918 | ␉␉␉␉{␊ |
919 | ␉␉␉␉␉address = segmentAddress + (UInt32)base;␊ |
920 | ␉␉␉␉␉␊ |
921 | ␉␉␉␉␉bind_location((UInt32*)address, (char*)symbolAddr, addend, type);␊ |
922 | ␉␉␉␉}␊ |
923 | ␉␉␉␉else␊ |
924 | ␉␉␉␉{␊ |
925 | ␉␉␉␉␉printf("Unable to bind symbol %s\n", symbolName);␊ |
926 | ␉␉␉␉␉getc();␊ |
927 | ␉␉␉␉}␊ |
928 | ␉␉␉␉segmentAddress += (immediate * sizeof(void*)) + sizeof(void*);␊ |
929 | ␉␉␉␉␊ |
930 | ␉␉␉␉␊ |
931 | ␉␉␉␉break;␊ |
932 | ␉␉␉␉␊ |
933 | ␉␉␉case BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB:␊ |
934 | ␉␉␉␉tmp = 0;␊ |
935 | ␉␉␉␉bits = 0;␊ |
936 | ␉␉␉␉do␊ |
937 | ␉␉␉␉{␊ |
938 | ␉␉␉␉␉tmp |= (bind_stream[++i] & 0x7f) << bits;␊ |
939 | ␉␉␉␉␉bits += 7;␊ |
940 | ␉␉␉␉}␊ |
941 | ␉␉␉␉while(bind_stream[i] & 0x80);␊ |
942 | ␉␉␉␉␊ |
943 | ␉␉␉␉␊ |
944 | ␉␉␉␉tmp2 = 0;␊ |
945 | ␉␉␉␉bits = 0;␊ |
946 | ␉␉␉␉do␊ |
947 | ␉␉␉␉{␊ |
948 | ␉␉␉␉␉tmp2 |= (bind_stream[++i] & 0x7f) << bits;␊ |
949 | ␉␉␉␉␉bits += 7;␊ |
950 | ␉␉␉␉}␊ |
951 | ␉␉␉␉while(bind_stream[i] & 0x80);␊ |
952 | ␉␉␉␉␊ |
953 | ␉␉␉␉␊ |
954 | ␉␉␉␉if(symbolAddr != 0xFFFFFFFF)␊ |
955 | ␉␉␉␉{␊ |
956 | ␉␉␉␉␉for(index = 0; index < tmp; index++)␊ |
957 | ␉␉␉␉␉{␊ |
958 | ␉␉␉␉␉␉␊ |
959 | ␉␉␉␉␉␉address = segmentAddress + (UInt32)base;␊ |
960 | ␉␉␉␉␉␉bind_location((UInt32*)address, (char*)symbolAddr, addend, type);␊ |
961 | ␉␉␉␉␉␉segmentAddress += tmp2 + sizeof(void*);␊ |
962 | ␉␉␉␉␉}␊ |
963 | ␉␉␉␉}␊ |
964 | ␉␉␉␉else␊ |
965 | ␉␉␉␉{␊ |
966 | ␉␉␉␉␉printf("Unable to bind symbol %s\n", symbolName);␊ |
967 | ␉␉␉␉␉getc();␊ |
968 | ␉␉␉␉}␊ |
969 | ␉␉␉␉break;␊ |
970 | ␉␉}␊ |
971 | ␉␉i++;␊ |
972 | ␉}␊ |
973 | }␊ |
974 | ␊ |
975 | inline void bind_location(UInt32* location, char* value, UInt32 addend, int type)␊ |
976 | {␉␊ |
977 | ␉// do actual update␊ |
978 | ␉char* newValue = value + addend;␊ |
979 | ␉␊ |
980 | ␉switch (type) {␊ |
981 | ␉␉case BIND_TYPE_POINTER:␊ |
982 | ␉␉case BIND_TYPE_TEXT_ABSOLUTE32:␊ |
983 | ␉␉␉break;␊ |
984 | ␉␉␉␊ |
985 | ␉␉case BIND_TYPE_TEXT_PCREL32:␊ |
986 | ␉␉␉newValue -= ((UInt32)location + 4);␊ |
987 | ␉␉␉␊ |
988 | ␉␉␉break;␊ |
989 | ␉␉default:␊ |
990 | ␉␉␉return;␊ |
991 | ␉}␊ |
992 | ␉//DBG("Binding 0x%X to 0x%X (was 0x%X)\n", location, newValue, *location);␊ |
993 | ␉*location = (UInt32)newValue;␊ |
994 | }␊ |
995 | ␊ |
996 | /********************************************************************************/␊ |
997 | /*␉Module Hook Interface␉␉␉␉␉␉␉␉␉␉␉␉␉␉*/␊ |
998 | /********************************************************************************/␊ |
999 | ␊ |
1000 | /*␊ |
1001 | * Locate the symbol for an already loaded function and modify the beginning of␊ |
1002 | * the function to jump directly to the new one␊ |
1003 | * example: replace_function("_HelloWorld_start", &replacement_start);␊ |
1004 | */␊ |
1005 | int replace_function(const char* symbol, void* newAddress)␊ |
1006 | {␊ |
1007 | ␉UInt32* jumpPointer = malloc(sizeof(UInt32*));␉ ␊ |
1008 | ␉UInt32 addr = lookup_all_symbols(symbol);␊ |
1009 | ␉␊ |
1010 | ␉char* binary = (char*)addr;␊ |
1011 | ␉if(addr != 0xFFFFFFFF)␊ |
1012 | ␉{␊ |
1013 | ␉␉//DBG("Replacing %s to point to 0x%x\n", symbol, newAddress);␊ |
1014 | ␉␉*binary++ = 0xFF;␉// Jump␊ |
1015 | ␉␉*binary++ = 0x25;␉// Long Jump␊ |
1016 | ␉␉*((UInt32*)binary) = (UInt32)jumpPointer;␊ |
1017 | ␉␉␊ |
1018 | ␉␉*jumpPointer = (UInt32)newAddress;␊ |
1019 | ␉␉return 1;␊ |
1020 | ␉}␊ |
1021 | ␉return 0;␊ |
1022 | }␊ |
1023 | ␊ |
1024 | ␊ |
1025 | /*␊ |
1026 | *␉execute_hook( const char* name )␊ |
1027 | *␉␉name - Name of the module hook␊ |
1028 | *␉␉␉If any callbacks have been registered for this hook␊ |
1029 | *␉␉␉they will be executed now in the same order that the␊ |
1030 | *␉␉␉hooks were added.␊ |
1031 | */␊ |
1032 | int execute_hook(const char* name, void* arg1, void* arg2, void* arg3, void* arg4)␊ |
1033 | {␊ |
1034 | ␉DBG("Attempting to execute hook '%s'\n", name); DBGPAUSE();␊ |
1035 | ␉moduleHook_t* hook = hook_exists(name);␊ |
1036 | ␉␊ |
1037 | ␉if(hook)␊ |
1038 | ␉{␊ |
1039 | ␉␉// Loop through all callbacks for this module␊ |
1040 | ␉␉callbackList_t* callbacks = hook->callbacks;␊ |
1041 | ␊ |
1042 | ␉␉while(callbacks)␊ |
1043 | ␉␉{␊ |
1044 | ␉␉␉// Execute callback␊ |
1045 | ␉␉␉callbacks->callback(arg1, arg2, arg3, arg4);␊ |
1046 | ␉␉␉callbacks = callbacks->next;␊ |
1047 | ␉␉}␊ |
1048 | ␉␉DBG("Hook '%s' executed.\n", name); DBGPAUSE();␊ |
1049 | ␉␉return 1;␊ |
1050 | ␉}␊ |
1051 | ␉else␊ |
1052 | ␉{␊ |
1053 | ␉␉// Callback for this hook doesn't exist;␊ |
1054 | ␉␉DBG("No callbacks for '%s' hook.\n", name);␊ |
1055 | ␉␉return 0;␊ |
1056 | ␉}␊ |
1057 | }␊ |
1058 | ␊ |
1059 | ␊ |
1060 | ␊ |
1061 | /*␊ |
1062 | *␉register_hook_callback( const char* name, void(*callback)())␊ |
1063 | *␉␉name - Name of the module hook to attach to.␊ |
1064 | *␉␉callbacks - The funciton pointer that will be called when the␊ |
1065 | *␉␉␉hook is executed. When registering a new callback name, the callback is added sorted.␊ |
1066 | *␉␉␉NOTE: the hooks take four void* arguments.␊ |
1067 | */␊ |
1068 | void register_hook_callback(const char* name, void(*callback)(void*, void*, void*, void*))␊ |
1069 | {␉␊ |
1070 | ␉DBG("Adding callback for '%s' hook.\n", name); DBGPAUSE();␊ |
1071 | ␉␊ |
1072 | ␉moduleHook_t* hook = hook_exists(name);␊ |
1073 | ␉␊ |
1074 | ␉if(hook)␊ |
1075 | ␉{␊ |
1076 | ␉␉// append␊ |
1077 | ␉␉callbackList_t* newCallback = malloc(sizeof(callbackList_t));␊ |
1078 | ␉␉newCallback->next = hook->callbacks;␊ |
1079 | ␉␉hook->callbacks = newCallback;␊ |
1080 | ␉␉newCallback->callback = callback;␊ |
1081 | ␉}␊ |
1082 | ␉else␊ |
1083 | ␉{␊ |
1084 | ␉␉// create new hook␊ |
1085 | ␉␉moduleHook_t* newHook = malloc(sizeof(moduleHook_t));␉␉␊ |
1086 | ␉␉newHook->name = name;␊ |
1087 | ␉␉newHook->callbacks = malloc(sizeof(callbackList_t));␊ |
1088 | ␉␉newHook->callbacks->callback = callback;␊ |
1089 | ␉␉newHook->callbacks->next = NULL;␊ |
1090 | ␉␉␊ |
1091 | ␉␉newHook->next = moduleCallbacks;␊ |
1092 | ␉␉moduleCallbacks = newHook;␊ |
1093 | ␉␉␊ |
1094 | ␉}␊ |
1095 | ␉␊ |
1096 | #if DEBUG_MODULES␊ |
1097 | ␉//print_hook_list();␊ |
1098 | ␉//getc();␊ |
1099 | #endif␊ |
1100 | ␉␊ |
1101 | }␊ |
1102 | ␊ |
1103 | ␊ |
1104 | moduleHook_t* hook_exists(const char* name)␊ |
1105 | {␊ |
1106 | ␉moduleHook_t* hooks = moduleCallbacks;␊ |
1107 | ␉␊ |
1108 | ␉// look for a hook. If it exists, return the moduleHook_t*,␊ |
1109 | ␉// If not, return NULL.␊ |
1110 | ␉while(hooks)␊ |
1111 | ␉{␊ |
1112 | ␉␉if(strcmp(name, hooks->name) == 0)␊ |
1113 | ␉␉{␊ |
1114 | ␉␉␉//DBG("Located hook %s\n", name);␊ |
1115 | ␉␉␉return hooks;␊ |
1116 | ␉␉}␊ |
1117 | ␉␉hooks = hooks->next;␊ |
1118 | ␉}␊ |
1119 | ␉//DBG("Hook %s does not exist\n", name);␊ |
1120 | ␉return NULL;␊ |
1121 | ␉␊ |
1122 | }␊ |
1123 | ␊ |
1124 | #if DEBUG_MODULES␊ |
1125 | void print_hook_list()␊ |
1126 | {␊ |
1127 | ␉printf("---Hook Table---\n");␊ |
1128 | ␉␊ |
1129 | ␉moduleHook_t* hooks = moduleCallbacks;␊ |
1130 | ␉while(hooks)␊ |
1131 | ␉{␊ |
1132 | ␉␉printf("Hook: %s\n", hooks->name);␊ |
1133 | ␉␉hooks = hooks->next;␊ |
1134 | ␉}␊ |
1135 | }␊ |
1136 | #endif␊ |
1137 | ␊ |
1138 | /********************************************************************************/␊ |
1139 | /*␉dyld / Linker Interface␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉*/␊ |
1140 | /********************************************************************************/␊ |
1141 | ␊ |
1142 | void dyld_stub_binder()␊ |
1143 | {␊ |
1144 | ␉printf("ERROR: dyld_stub_binder was called, should have been take care of by the linker.\n");␊ |
1145 | ␉getc();␊ |
1146 | }␊ |
1147 | |