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