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