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