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