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