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