1 | /*␊ |
2 | * Copyright 2010 Evan Lojewski. All rights reserved.␊ |
3 | *␊ |
4 | */␊ |
5 | #include "boot.h"␊ |
6 | #include "bootstruct.h"␊ |
7 | #include "modules.h"␊ |
8 | #include "boot_modules.h"␊ |
9 | #include <vers.h>␊ |
10 | ␊ |
11 | #ifdef CONFIG_MODULES␊ |
12 | #ifndef CONFIG_MODULE_DEBUG␊ |
13 | #define CONFIG_MODULE_DEBUG 0␊ |
14 | #endif␊ |
15 | ␊ |
16 | ␊ |
17 | #if CONFIG_MODULE_DEBUG␊ |
18 | #define DBG(x...)␉printf(x)␊ |
19 | #define DBGPAUSE()␉getchar()␊ |
20 | #else␊ |
21 | #define DBG(x...)␊ |
22 | #define DBGPAUSE()␊ |
23 | #endif␊ |
24 | ␊ |
25 | // NOTE: Global so that modules can link with this␊ |
26 | static UInt64 textAddress = 0;␊ |
27 | static UInt64 textSection = 0;␊ |
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 | // Start any modules that were compiled in first.␊ |
44 | start_built_in_modules();␊ |
45 | ␊ |
46 | ␊ |
47 | ␉int retVal = 0;␊ |
48 | ␉void (*module_start)(void) = NULL;␊ |
49 | ␊ |
50 | ␉extern char symbols_start __asm("section$start$__DATA$__Symbols");␊ |
51 | ␉char* module_data = &symbols_start;␊ |
52 | ␊ |
53 | ␉// Intialize module system␊ |
54 | ␉if(module_data)␊ |
55 | ␉{␊ |
56 | ␉␉// Module system was compiled in (Symbols.dylib addr known)␊ |
57 | ␉␉module_start = parse_mach(module_data, &load_module, &add_symbol, NULL);␊ |
58 | ␉␉␊ |
59 | ␉␉if(module_start && module_start != (void*)0xFFFFFFFF)␊ |
60 | ␉␉{␊ |
61 | ␉␉␉// Notify the system that it was laoded␊ |
62 | ␉␉␉module_loaded(SYMBOLS_MODULE, SYMBOLS_AUTHOR, SYMBOLS_DESCRIPTION, SYMBOLS_VERSION, SYMBOLS_COMPAT);␊ |
63 | ␉␉␉(*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 | ␉␉␉// The module does not have a valid start function␊ |
72 | ␉␉␉printf("Unable to start %s at 0x%x\n", SYMBOLS_MODULE, module_data); pause();␊ |
73 | ␉␉}␉␉␊ |
74 | ␉}␊ |
75 | ␉return retVal;␊ |
76 | }␊ |
77 | ␊ |
78 | void start_built_in_module(const char* name, ␊ |
79 | const char* author, ␊ |
80 | const char* description,␊ |
81 | UInt32 version,␊ |
82 | UInt32 compat,␊ |
83 | void(*start_function)(void))␊ |
84 | {␊ |
85 | start_function();␊ |
86 | // Notify the module system that this module really exists, specificaly, let other module link with it␊ |
87 | module_loaded(name, author, description, version, compat);␊ |
88 | }␊ |
89 | ␊ |
90 | /*␊ |
91 | * Load all modules in the /Extra/modules/ directory␊ |
92 | * Module depencdies will be loaded first␊ |
93 | * Modules will only be loaded once. When loaded a module must␊ |
94 | * setup apropriete function calls and hooks as required.␊ |
95 | * NOTE: To ensure a module loads after another you may ␊ |
96 | * link one module with the other. For dyld to allow this, you must␊ |
97 | * reference at least one symbol within the module.␊ |
98 | */␊ |
99 | void load_all_modules()␊ |
100 | {␊ |
101 | ␉char* name;␊ |
102 | ␉long flags;␊ |
103 | ␉long time;␊ |
104 | ␉struct dirstuff* moduleDir = opendir("/Extra/modules/");␊ |
105 | ␉while(readdir(moduleDir, (const char**)&name, &flags, &time) >= 0)␊ |
106 | ␉{␊ |
107 | ␉␉if(strcmp(&name[strlen(name) - sizeof("dylib")], ".dylib") == 0)␊ |
108 | ␉␉{␊ |
109 | ␉␉␉char* tmp = malloc(strlen(name) + 1);␊ |
110 | ␉␉␉strcpy(tmp, name);␊ |
111 | ␉␉␉␊ |
112 | ␉␉␉if(!load_module(tmp))␊ |
113 | ␉␉␉{␊ |
114 | ␉␉␉␉// failed to load␊ |
115 | ␉␉␉␉// free(tmp);␊ |
116 | ␉␉␉}␊ |
117 | ␉␉}␊ |
118 | ␉␉else ␊ |
119 | ␉␉{␊ |
120 | ␉␉␉DBG("Ignoring %s\n", name);␊ |
121 | ␉␉}␊ |
122 | ␊ |
123 | ␉}␊ |
124 | }␊ |
125 | ␊ |
126 | ␊ |
127 | /*␊ |
128 | * Load a module file in /Extra/modules/␊ |
129 | */␊ |
130 | int load_module(char* module)␊ |
131 | {␊ |
132 | ␉int retVal = 1;␊ |
133 | ␉void (*module_start)(void) = NULL;␊ |
134 | ␉char modString[128];␊ |
135 | ␉int fh = -1;␊ |
136 | ␊ |
137 | ␉// Check to see if the module has already been loaded␊ |
138 | ␉if(is_module_loaded(module))␊ |
139 | ␉{␊ |
140 | ␉␉return 1;␊ |
141 | ␉}␊ |
142 | ␉␊ |
143 | ␉sprintf(modString, MODULE_PATH "%s", module);␊ |
144 | ␉fh = open(modString, 0);␊ |
145 | ␉if(fh < 0)␊ |
146 | ␉{␊ |
147 | ␉␉DBG("WARNING: Unable to locate module %s\n", modString); DBGPAUSE();␊ |
148 | ␉␉return 0;␊ |
149 | ␉}␊ |
150 | ␉unsigned int moduleSize = file_size(fh);␊ |
151 | if(moduleSize == 0)␊ |
152 | {␊ |
153 | DBG("WARNING: The module %s has a file size of %d, the module will not be loaded.\n", modString, moduleSize);␊ |
154 | return 0;␊ |
155 | }␊ |
156 | ␊ |
157 | ␉char* module_base = (char*) malloc(moduleSize);␊ |
158 | ␉if (moduleSize && read(fh, module_base, moduleSize) == moduleSize)␊ |
159 | ␉{␊ |
160 | ␉␉// Module loaded into memory, parse it␊ |
161 | ␉␉module_start = parse_mach(module_base, &load_module, &add_symbol, NULL);␊ |
162 | ␊ |
163 | ␉␉if(module_start && module_start != (void*)0xFFFFFFFF)␊ |
164 | ␉␉{␊ |
165 | ␉␉␉// Notify the system that it was laoded␊ |
166 | ␉␉␉module_loaded(module, NULL, NULL, 0, 0 /*moduleName, NULL, moduleVersion, moduleCompat*/);␊ |
167 | ␉␉␉(*module_start)();␉// Start the module␊ |
168 | ␉␉␉DBG("Module %s Loaded.\n", module); DBGPAUSE();␊ |
169 | ␉␉}␊ |
170 | #if CONFIG_MODULE_DEBUG␊ |
171 | ␉␉else // The module does not have a valid start function. This may be a library.␊ |
172 | ␉␉{␊ |
173 | ␉␉␉printf("WARNING: Unable to start %s\n", module);␊ |
174 | ␉␉␉getchar();␊ |
175 | ␉␉}␊ |
176 | #else␊ |
177 | ␉␉else msglog("WARNING: Unable to start %s\n", module);␊ |
178 | #endif␊ |
179 | ␉}␊ |
180 | ␉else␊ |
181 | ␉{␊ |
182 | ␉␉DBG("Unable to read in module %s\n.", module); DBGPAUSE();␊ |
183 | ␉␉retVal = 0;␊ |
184 | ␉}␊ |
185 | ␊ |
186 | ␉close(fh);␊ |
187 | ␉return retVal;␊ |
188 | }␊ |
189 | ␊ |
190 | /*␊ |
191 | * add_symbol␊ |
192 | * This function adds a symbol from a module to the list of known symbols ␊ |
193 | * possibly change to a pointer and add this to the Symbol module so that it can␊ |
194 | * adjust it's internal symbol list (sort) to optimize locating new symbols␊ |
195 | * NOTE: returns the address if the symbol is "start", else returns 0xFFFFFFFF␊ |
196 | */␊ |
197 | long long add_symbol(char* symbol, long long addr, char is64)␊ |
198 | {␊ |
199 | ␉// This only can handle 32bit symbols ␊ |
200 | ␉symbolList_t* entry;␊ |
201 | ␉//DBG("Adding symbol %s at 0x%X\n", symbol, addr);␊ |
202 | ␉␊ |
203 | ␉entry = malloc(sizeof(symbolList_t));␊ |
204 | ␉entry->next = moduleSymbols;␊ |
205 | ␉moduleSymbols = entry;␊ |
206 | ␉␊ |
207 | ␉entry->addr = (UInt32)addr;␊ |
208 | ␉entry->symbol = symbol;␊ |
209 | ␉␊ |
210 | ␉if(!is64 && strcmp(symbol, "start") == 0)␊ |
211 | ␉{␊ |
212 | ␉␉return addr;␊ |
213 | ␉}␊ |
214 | ␉else␊ |
215 | ␉{␊ |
216 | ␉␉return 0xFFFFFFFF; // fixme␊ |
217 | ␉}␊ |
218 | }␊ |
219 | ␊ |
220 | ␊ |
221 | /*␊ |
222 | * print out the information about the loaded module␊ |
223 | */␊ |
224 | void module_loaded(const char* name, const char* author, const char* description, UInt32 version, UInt32 compat)␊ |
225 | {␊ |
226 | ␉moduleList_t* new_entry = malloc(sizeof(moduleList_t));␊ |
227 | ␉new_entry->next = loadedModules;␊ |
228 | ␊ |
229 | ␉loadedModules = new_entry;␊ |
230 | ␉␊ |
231 | if(!name) name = "Unknown";␊ |
232 | if(!author) author = "Unknown";␊ |
233 | if(!description) description = "";␊ |
234 | ␊ |
235 | ␉new_entry->name = name;␊ |
236 | new_entry->author = author;␊ |
237 | new_entry->description = description;␊ |
238 | ␉new_entry->version = version;␊ |
239 | new_entry->compat = compat;␊ |
240 | ␊ |
241 | msglog("Module '%s' by '%s' Loaded.\n", name, author);␊ |
242 | msglog("\tDescription: %s\n", description);␊ |
243 | msglog("\tVersion: %d\n", version); // todo: sperate to major.minor.bugfix␊ |
244 | msglog("\tCompat: %d\n", compat); // todo: ^^^ major.minor.bugfix␊ |
245 | }␊ |
246 | ␊ |
247 | int is_module_loaded(const char* name)␊ |
248 | {␊ |
249 | ␉// todo sorted search␊ |
250 | ␉moduleList_t* entry = loadedModules;␊ |
251 | ␉while(entry)␊ |
252 | ␉{␊ |
253 | ␉␉if(strcmp(entry->name, name) == 0)␊ |
254 | ␉␉{␊ |
255 | ␉␉␉DBG("Located module %s\n", name); DBGPAUSE();␊ |
256 | ␉␉␉return 1;␊ |
257 | ␉␉}␊ |
258 | ␉␉else␊ |
259 | ␉␉{␊ |
260 | ␉␉␉entry = entry->next;␊ |
261 | ␉␉}␊ |
262 | ␊ |
263 | ␉}␊ |
264 | ␉␊ |
265 | ␉DBG("Module %s not loaded\n", name); DBGPAUSE();␊ |
266 | ␉return 0;␊ |
267 | }␊ |
268 | ␊ |
269 | /*␊ |
270 | *␉lookup symbols in all loaded modules. Thins inludes boot syms due to Symbols.dylib construction␊ |
271 | *␊ |
272 | */␊ |
273 | unsigned int lookup_all_symbols(const char* name)␊ |
274 | {␊ |
275 | ␉symbolList_t* entry = moduleSymbols;␊ |
276 | ␉while(entry)␊ |
277 | ␉{␊ |
278 | ␉␉if(strcmp(entry->symbol, name) == 0)␊ |
279 | ␉␉{␊ |
280 | ␉␉␉//DBG("External symbol %s located at 0x%X\n", name, entry->addr);␊ |
281 | ␉␉␉return entry->addr;␊ |
282 | ␉␉}␊ |
283 | ␉␉else␊ |
284 | ␉␉{␊ |
285 | ␉␉␉entry = entry->next;␊ |
286 | ␉␉}␊ |
287 | ␉}␊ |
288 | ␉␊ |
289 | #if CONFIG_MODULE_DEBUG␊ |
290 | ␉printf("Unable to locate symbol %s\n", name);␊ |
291 | ␉getchar();␊ |
292 | #endif␊ |
293 | ␉␊ |
294 | ␉if(strcmp(name, VOID_SYMBOL) == 0) return 0xFFFFFFFF;␊ |
295 | ␉// In the event that a symbol does not exist␊ |
296 | ␉// Return a pointer to a void function.␊ |
297 | ␉else return lookup_all_symbols(VOID_SYMBOL);␉␊ |
298 | }␊ |
299 | ␊ |
300 | /********************************************************************************/␊ |
301 | /*␉Macho Parser␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉*/␊ |
302 | /********************************************************************************/␊ |
303 | ␊ |
304 | /*␊ |
305 | * Parse through a macho module. The module will be rebased and binded␊ |
306 | * as specified in the macho header. If the module is sucessfuly laoded␊ |
307 | * the module iinit address will be returned.␊ |
308 | * NOTE; all dependecies will be loaded before this module is started␊ |
309 | * NOTE: If the module is unable to load ot completeion, the modules␊ |
310 | * symbols will still be available.␊ |
311 | */␊ |
312 | void* parse_mach(void* binary, ␊ |
313 | int(*dylib_loader)(char*), ␊ |
314 | long long(*symbol_handler)(char*, long long, char),␊ |
315 | void (*section_handler)(char* section, char* segment, void* cmd, UInt64 offset, UInt64 address)␊ |
316 | )␊ |
317 | {␉␊ |
318 | ␉char is64 = false;␊ |
319 | ␉void (*module_start)(void) = NULL;␊ |
320 | ␉␊ |
321 | ␉// Module info␊ |
322 | ␉/*char* moduleName = NULL;␊ |
323 | ␉ UInt32 moduleVersion = 0;␊ |
324 | ␉ UInt32 moduleCompat = 0;␊ |
325 | ␉ */␊ |
326 | ␉// TODO convert all of the structs to a union␊ |
327 | ␉struct load_command *loadCommand = NULL;␊ |
328 | ␉struct dylib_command* dylibCommand = NULL;␊ |
329 | ␉struct dyld_info_command* dyldInfoCommand = NULL;␊ |
330 | ␉␊ |
331 | ␉struct symtab_command* symtabCommand = NULL;␊ |
332 | ␉struct segment_command *segCommand = NULL;␊ |
333 | ␉struct segment_command_64 *segCommand64 = NULL;␊ |
334 | ␉␊ |
335 | ␉//struct dysymtab_command* dysymtabCommand = NULL;␊ |
336 | ␉UInt32 binaryIndex = 0;␊ |
337 | ␉UInt16 cmd = 0;␊ |
338 | ␉␊ |
339 | ␉textSection = 0;␊ |
340 | ␉textAddress = 0;␉// reinitialize text location in case it doesn't exist;␊ |
341 | ␉␊ |
342 | ␉// Parse through the load commands␊ |
343 | ␉if(((struct mach_header*)binary)->magic == MH_MAGIC)␊ |
344 | ␉{␊ |
345 | ␉␉is64 = false;␊ |
346 | ␉␉binaryIndex += sizeof(struct mach_header);␊ |
347 | ␉}␊ |
348 | ␉else if(((struct mach_header_64*)binary)->magic == MH_MAGIC_64)␊ |
349 | ␉{␊ |
350 | ␉␉// NOTE: modules cannot be 64bit. This is used to parse the kernel and kexts␊ |
351 | ␉␉is64 = true;␊ |
352 | ␉␉binaryIndex += sizeof(struct mach_header_64);␊ |
353 | ␉}␊ |
354 | ␉else␊ |
355 | ␉{␊ |
356 | ␉␉verbose("Invalid mach magic 0x%X\n", ((struct mach_header*)binary)->magic);␊ |
357 | ␉␉return NULL;␊ |
358 | ␉}␊ |
359 | ␉␊ |
360 | ␉␊ |
361 | ␉␊ |
362 | ␉/*if(((struct mach_header*)binary)->filetype != MH_DYLIB)␊ |
363 | ␉ {␊ |
364 | ␉ printf("Module is not a dylib. Unable to load.\n");␊ |
365 | ␉ getchar();␊ |
366 | ␉ return NULL; // Module is in the incorrect format␊ |
367 | ␉ }*/␊ |
368 | ␉␊ |
369 | ␉while(cmd < ((struct mach_header*)binary)->ncmds)␊ |
370 | ␉{␊ |
371 | ␉␉cmd++;␊ |
372 | ␉␉␊ |
373 | ␉␉loadCommand = binary + binaryIndex;␊ |
374 | ␉␉UInt32 cmdSize = loadCommand->cmdsize;␊ |
375 | ␉␉␊ |
376 | ␉␉␊ |
377 | ␉␉switch ((loadCommand->cmd & 0x7FFFFFFF))␊ |
378 | ␉␉{␊ |
379 | ␉␉␉case LC_SYMTAB:␊ |
380 | ␉␉␉␉symtabCommand = binary + binaryIndex;␊ |
381 | ␉␉␉␉break;␊ |
382 | ␉␉␉␉␊ |
383 | ␉␉␉case LC_SEGMENT: // 32bit macho␊ |
384 | {␊ |
385 | segCommand = binary + binaryIndex;␊ |
386 | ␊ |
387 | UInt32 sectionIndex;␊ |
388 | ␊ |
389 | sectionIndex = sizeof(struct segment_command);␊ |
390 | ␊ |
391 | struct section *sect;␊ |
392 | ␊ |
393 | while(sectionIndex < segCommand->cmdsize)␊ |
394 | {␊ |
395 | sect = binary + binaryIndex + sectionIndex;␊ |
396 | ␊ |
397 | sectionIndex += sizeof(struct section);␊ |
398 | ␊ |
399 | if(section_handler) section_handler(sect->sectname, segCommand->segname, (void*)sect, sect->offset, sect->addr);␊ |
400 | ␊ |
401 | if((strcmp("__TEXT", segCommand->segname) == 0) && (strcmp("__text", sect->sectname) == 0))␊ |
402 | {␊ |
403 | // __TEXT,__text found, save the offset and address for when looking for the calls.␊ |
404 | textSection = sect->offset;␊ |
405 | textAddress = sect->addr;␊ |
406 | }␉␉␉␉␉␊ |
407 | }␊ |
408 | }␊ |
409 | ␉␉␉␉break;␊ |
410 | ␉␉␉case LC_SEGMENT_64:␉// 64bit macho's␊ |
411 | {␊ |
412 | segCommand64 = binary + binaryIndex;␉␉␉␉␊ |
413 | UInt32 sectionIndex;␊ |
414 | ␊ |
415 | sectionIndex = sizeof(struct segment_command_64);␊ |
416 | ␊ |
417 | struct section_64 *sect;␊ |
418 | ␊ |
419 | while(sectionIndex < segCommand64->cmdsize)␊ |
420 | {␊ |
421 | sect = binary + binaryIndex + sectionIndex;␊ |
422 | ␊ |
423 | sectionIndex += sizeof(struct section_64);␊ |
424 | ␊ |
425 | if(section_handler) section_handler(sect->sectname, segCommand64->segname, (void*)sect, sect->offset, sect->addr);␊ |
426 | ␊ |
427 | if((strcmp("__TEXT", segCommand64->segname) == 0) && (strcmp("__text", sect->sectname) == 0))␊ |
428 | {␊ |
429 | // __TEXT,__text found, save the offset and address for when looking for the calls.␊ |
430 | textSection = sect->offset;␊ |
431 | textAddress = sect->addr;␊ |
432 | }␉␉␉␉␉␊ |
433 | }␉␊ |
434 | ␉␉␉␉}␉␉␉␊ |
435 | ␉␉␉␉break;␊ |
436 | ␉␉␉␉␊ |
437 | ␉␉␉␉␊ |
438 | ␉␉␉case LC_LOAD_DYLIB:␊ |
439 | ␉␉␉case LC_LOAD_WEAK_DYLIB ^ LC_REQ_DYLD:␊ |
440 | // Required modules␊ |
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, (UInt8*)dyldInfoCommand->bind_off,␉␉dyldInfoCommand->bind_size);␊ |
497 | ␉␉if(dyldInfoCommand->weak_bind_off)␉bind_macho(binary, (UInt8*)dyldInfoCommand->weak_bind_off,␉dyldInfoCommand->weak_bind_size);␊ |
498 | ␉␉if(dyldInfoCommand->lazy_bind_off)␉bind_macho(binary, (UInt8*)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 | UInt32 read_uleb(UInt8* bind_stream, unsigned int* i)␊ |
742 | {␊ |
743 | // Read in offset␊ |
744 | UInt32 tmp = 0;␊ |
745 | UInt8 bits = 0;␊ |
746 | do␊ |
747 | {␊ |
748 | if(bits < sizeof(UInt32)*8) // hack␊ |
749 | {␊ |
750 | tmp |= (bind_stream[++(*i)] & 0x7f) << bits;␊ |
751 | bits += 7;␊ |
752 | }␊ |
753 | else␊ |
754 | {␊ |
755 | ++(*i);␊ |
756 | }␊ |
757 | }␊ |
758 | while(bind_stream[*i] & 0x80);␊ |
759 | return tmp;␊ |
760 | }␊ |
761 | ␊ |
762 | ␊ |
763 | // Based on code from dylibinfo.cpp and ImageLoaderMachOCompressed.cpp␊ |
764 | // NOTE: this uses 32bit values, and not 64bit values. ␊ |
765 | // There is a possibility that this could cause issues,␊ |
766 | // however the modules are 32 bits, so it shouldn't matter too much␊ |
767 | void bind_macho(void* base, UInt8* bind_stream, UInt32 size)␊ |
768 | {␉␊ |
769 | ␉bind_stream += (UInt32)base;␊ |
770 | ␉␊ |
771 | ␉UInt8 immediate = 0;␊ |
772 | ␉UInt8 opcode = 0;␊ |
773 | ␉UInt8 type = BIND_TYPE_POINTER;␊ |
774 | ␉␊ |
775 | ␉UInt32 segmentAddress = 0;␊ |
776 | ␉␊ |
777 | ␉UInt32 address = 0;␊ |
778 | ␉␊ |
779 | ␉SInt32 addend = 0;␊ |
780 | ␉SInt32 libraryOrdinal = 0;␊ |
781 | ␉␊ |
782 | ␉const char* symbolName = NULL;␊ |
783 | ␉UInt8 symboFlags = 0;␊ |
784 | ␉UInt32 symbolAddr = 0xFFFFFFFF;␊ |
785 | ␉␊ |
786 | ␉// Temperary variables␊ |
787 | ␉UInt32 tmp = 0;␊ |
788 | ␉UInt32 tmp2 = 0;␊ |
789 | ␉UInt32 index = 0;␊ |
790 | ␉unsigned int i = 0;␊ |
791 | ␉␊ |
792 | ␉while(i < size)␊ |
793 | ␉{␊ |
794 | ␉␉immediate = bind_stream[i] & BIND_IMMEDIATE_MASK;␊ |
795 | ␉␉opcode = bind_stream[i] & BIND_OPCODE_MASK;␊ |
796 | ␉␉␊ |
797 | ␉␉␊ |
798 | ␉␉switch(opcode)␊ |
799 | ␉␉{␊ |
800 | ␉␉␉case BIND_OPCODE_DONE:␊ |
801 | ␉␉␉␉// reset vars␊ |
802 | ␉␉␉␉type = BIND_TYPE_POINTER;␊ |
803 | ␉␉␉␉segmentAddress = 0;␊ |
804 | ␉␉␉␉address = 0;␊ |
805 | ␉␉␉␉addend = 0;␊ |
806 | ␉␉␉␉libraryOrdinal = 0;␊ |
807 | ␉␉␉␉symbolAddr = 0xFFFFFFFF;␊ |
808 | ␉␉␉default:␊ |
809 | ␉␉␉␉break;␊ |
810 | ␉␉␉␉␊ |
811 | ␉␉␉case BIND_OPCODE_SET_DYLIB_ORDINAL_IMM:␊ |
812 | ␉␉␉␉libraryOrdinal = immediate;␊ |
813 | ␉␉␉␉break;␊ |
814 | ␉␉␉␉␊ |
815 | ␉␉␉case BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB:␊ |
816 | ␉␉␉␉libraryOrdinal = read_uleb(bind_stream, &i);␊ |
817 | ␉␉␉␉break;␊ |
818 | ␉␉␉␉␊ |
819 | ␉␉␉case BIND_OPCODE_SET_DYLIB_SPECIAL_IMM:␊ |
820 | ␉␉␉␉libraryOrdinal = immediate ? (SInt8)(BIND_OPCODE_MASK | immediate) : immediate;␉␉␉␉␊ |
821 | ␉␉␉␉break;␊ |
822 | ␉␉␉␉␊ |
823 | ␉␉␉case BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM:␊ |
824 | ␉␉␉␉symboFlags = immediate;␊ |
825 | ␉␉␉␉symbolName = (char*)&bind_stream[++i];␊ |
826 | ␉␉␉␉i += strlen((char*)&bind_stream[i]);␊ |
827 | ␊ |
828 | ␉␉␉␉symbolAddr = lookup_all_symbols(symbolName);␊ |
829 | ␉␉␉␉break;␊ |
830 | ␉␉␉␉␊ |
831 | ␉␉␉case BIND_OPCODE_SET_TYPE_IMM:␊ |
832 | ␉␉␉␉type = immediate;␊ |
833 | ␉␉␉␉break;␊ |
834 | ␉␉␉␉␊ |
835 | ␉␉␉case BIND_OPCODE_SET_ADDEND_SLEB:␊ |
836 | ␉␉␉␉addend = read_uleb(bind_stream, &i);␉␉␉␉␊ |
837 | ␉␉␉␉if(!(bind_stream[i-1] & 0x40)) addend *= -1;␊ |
838 | ␉␉␉␉break;␊ |
839 | ␉␉␉␉␊ |
840 | ␉␉␉case BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB:␊ |
841 | ␉␉␉␉segmentAddress = 0;␊ |
842 | ␉␉␉␉␊ |
843 | ␉␉␉␉// Locate address␊ |
844 | ␉␉␉␉struct segment_command* segCommand = NULL;␉// NOTE: 32bit only␊ |
845 | ␉␉␉␉␊ |
846 | ␉␉␉␉unsigned int binIndex = 0;␊ |
847 | ␉␉␉␉index = 0;␊ |
848 | ␉␉␉␉do␊ |
849 | ␉␉␉␉{␊ |
850 | ␉␉␉␉␉segCommand = base + sizeof(struct mach_header) + binIndex;␊ |
851 | ␉␉␉␉␉binIndex += segCommand->cmdsize;␊ |
852 | ␉␉␉␉␉index++;␊ |
853 | ␉␉␉␉}␊ |
854 | ␉␉␉␉while(index <= immediate);␊ |
855 | ␉␉␉␉␊ |
856 | ␉␉␉␉segmentAddress = segCommand->fileoff;␊ |
857 | ␉␉␉␉␉␉␉␉␊ |
858 | ␉␉␉␉segmentAddress += read_uleb(bind_stream, &i);␊ |
859 | ␉␉␉␉break;␊ |
860 | ␉␉␉␉␊ |
861 | ␉␉␉case BIND_OPCODE_ADD_ADDR_ULEB:␊ |
862 | ␉␉␉␉segmentAddress += read_uleb(bind_stream, &i);␊ |
863 | ␉␉␉␉break;␊ |
864 | ␉␉␉␉␊ |
865 | ␉␉␉case BIND_OPCODE_DO_BIND:␊ |
866 | ␉␉␉␉if(symbolAddr != 0xFFFFFFFF)␊ |
867 | ␉␉␉␉{␊ |
868 | ␉␉␉␉␉address = segmentAddress + (UInt32)base;␊ |
869 | ␉␉␉␉␉␉␊ |
870 | ␉␉␉␉␉bind_location((UInt32*)address, (char*)symbolAddr, addend, type);␊ |
871 | ␉␉␉␉}␊ |
872 | ␉␉␉␉else␊ |
873 | ␉␉␉␉{␊ |
874 | ␉␉␉␉␉printf("Unable to bind symbol %s\n", symbolName);␊ |
875 | ␉␉␉␉␉getchar();␊ |
876 | ␉␉␉␉}␊ |
877 | ␉␉␉␉␊ |
878 | ␉␉␉␉segmentAddress += sizeof(void*);␊ |
879 | ␉␉␉␉break;␊ |
880 | ␉␉␉␉␊ |
881 | ␉␉␉case BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB:␊ |
882 | ␉␉␉␉// Read in offset␊ |
883 | ␉␉␉␉tmp = read_uleb(bind_stream, &i);␊ |
884 | ␊ |
885 | ␉␉␉␉if(symbolAddr != 0xFFFFFFFF)␊ |
886 | ␉␉␉␉{␊ |
887 | ␉␉␉␉␉address = segmentAddress + (UInt32)base;␊ |
888 | ␉␉␉␉␉␊ |
889 | ␉␉␉␉␉bind_location((UInt32*)address, (char*)symbolAddr, addend, type);␊ |
890 | ␉␉␉␉}␊ |
891 | ␉␉␉␉else␊ |
892 | ␉␉␉␉{␊ |
893 | ␉␉␉␉␉printf("Unable to bind symbol %s\n", symbolName);␊ |
894 | ␉␉␉␉␉getchar();␊ |
895 | ␉␉␉␉}␊ |
896 | ␊ |
897 | ␉␉␉␉segmentAddress += tmp + sizeof(void*);␊ |
898 | ␉␉␉␉␊ |
899 | ␉␉␉␉␊ |
900 | ␉␉␉␉break;␊ |
901 | ␉␉␉␉␊ |
902 | ␉␉␉case BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED:␊ |
903 | ␉␉␉␉if(symbolAddr != 0xFFFFFFFF)␊ |
904 | ␉␉␉␉{␊ |
905 | ␉␉␉␉␉address = segmentAddress + (UInt32)base;␊ |
906 | ␉␉␉␉␉␊ |
907 | ␉␉␉␉␉bind_location((UInt32*)address, (char*)symbolAddr, addend, type);␊ |
908 | ␉␉␉␉}␊ |
909 | ␉␉␉␉else␊ |
910 | ␉␉␉␉{␊ |
911 | ␉␉␉␉␉printf("Unable to bind symbol %s\n", symbolName);␊ |
912 | ␉␉␉␉␉getchar();␊ |
913 | ␉␉␉␉}␊ |
914 | ␉␉␉␉segmentAddress += (immediate * sizeof(void*)) + sizeof(void*);␊ |
915 | ␉␉␉␉␊ |
916 | ␉␉␉␉␊ |
917 | ␉␉␉␉break;␊ |
918 | ␉␉␉␉␊ |
919 | ␉␉␉case BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB:␊ |
920 | ␉␉␉␉tmp = read_uleb(bind_stream, &i);␉␉␉␉␊ |
921 | ␉␉␉␉␊ |
922 | ␉␉␉␉tmp2 = read_uleb(bind_stream, &i);␉␉␉␉␊ |
923 | ␉␉␉␉␊ |
924 | ␉␉␉␉if(symbolAddr != 0xFFFFFFFF)␊ |
925 | ␉␉␉␉{␊ |
926 | ␉␉␉␉␉for(index = 0; index < tmp; index++)␊ |
927 | ␉␉␉␉␉{␊ |
928 | ␉␉␉␉␉␉␊ |
929 | ␉␉␉␉␉␉address = segmentAddress + (UInt32)base;␊ |
930 | ␉␉␉␉␉␉bind_location((UInt32*)address, (char*)symbolAddr, addend, type);␊ |
931 | ␉␉␉␉␉␉segmentAddress += tmp2 + sizeof(void*);␊ |
932 | ␉␉␉␉␉}␊ |
933 | ␉␉␉␉}␊ |
934 | ␉␉␉␉else␊ |
935 | ␉␉␉␉{␊ |
936 | ␉␉␉␉␉printf("Unable to bind symbol %s\n", symbolName);␊ |
937 | ␉␉␉␉␉getchar();␊ |
938 | ␉␉␉␉}␊ |
939 | ␉␉␉␉break;␊ |
940 | ␉␉}␊ |
941 | ␉␉i++;␊ |
942 | ␉}␊ |
943 | }␊ |
944 | ␊ |
945 | ␊ |
946 | inline void bind_location(UInt32* location, char* value, UInt32 addend, int type)␊ |
947 | {␉␊ |
948 | ␉// do actual update␊ |
949 | ␉char* newValue = value + addend;␊ |
950 | ␉␊ |
951 | ␉switch (type) {␊ |
952 | ␉␉case BIND_TYPE_POINTER:␊ |
953 | ␉␉case BIND_TYPE_TEXT_ABSOLUTE32:␊ |
954 | ␉␉␉break;␊ |
955 | ␉␉␉␊ |
956 | ␉␉case BIND_TYPE_TEXT_PCREL32:␊ |
957 | ␉␉␉newValue -= ((UInt32)location + 4);␊ |
958 | ␉␉␉␊ |
959 | ␉␉␉break;␊ |
960 | ␉␉default:␊ |
961 | ␉␉␉return;␊ |
962 | ␉}␊ |
963 | ␉//DBG("Binding 0x%X to 0x%X (was 0x%X)\n", location, newValue, *location);␊ |
964 | ␉*location = (UInt32)newValue;␊ |
965 | }␊ |
966 | ␊ |
967 | /********************************************************************************/␊ |
968 | /*␉Module Hook Interface␉␉␉␉␉␉␉␉␉␉␉␉␉␉*/␊ |
969 | /********************************************************************************/␊ |
970 | ␊ |
971 | /*␊ |
972 | * Locate the symbol for an already loaded function and modify the beginning of␊ |
973 | * the function to jump directly to the new one␊ |
974 | * example: replace_function("_HelloWorld_start", &replacement_start);␊ |
975 | */␊ |
976 | int replace_function(const char* symbol, void* newAddress)␊ |
977 | {␊ |
978 | ␉UInt32* jumpPointer = malloc(sizeof(UInt32*));␉ ␊ |
979 | ␉UInt32 addr = lookup_all_symbols(symbol);␊ |
980 | ␉␊ |
981 | ␉char* binary = (char*)addr;␊ |
982 | ␉if(addr != 0xFFFFFFFF)␊ |
983 | ␉{␊ |
984 | ␉␉//DBG("Replacing %s to point to 0x%x\n", symbol, newAddress);␊ |
985 | ␉␉*binary++ = 0xFF;␉// Jump␊ |
986 | ␉␉*binary++ = 0x25;␉// Long Jump␊ |
987 | ␉␉*((UInt32*)binary) = (UInt32)jumpPointer;␊ |
988 | ␉␉␊ |
989 | ␉␉*jumpPointer = (UInt32)newAddress;␊ |
990 | ␉␉return 1;␊ |
991 | ␉}␊ |
992 | ␉return 0;␊ |
993 | }␊ |
994 | ␊ |
995 | ␊ |
996 | /*␊ |
997 | *␉execute_hook( const char* name )␊ |
998 | *␉␉name - Name of the module hook␊ |
999 | *␉␉␉If any callbacks have been registered for this hook␊ |
1000 | *␉␉␉they will be executed now in the same order that the␊ |
1001 | *␉␉␉hooks were added.␊ |
1002 | */␊ |
1003 | int execute_hook(const char* name, void* arg1, void* arg2, void* arg3, void* arg4)␊ |
1004 | {␊ |
1005 | ␉DBG("Attempting to execute hook '%s'\n", name); DBGPAUSE();␊ |
1006 | ␉moduleHook_t* hook = hook_exists(name);␊ |
1007 | ␉␊ |
1008 | ␉if(hook)␊ |
1009 | ␉{␊ |
1010 | ␉␉// Loop through all callbacks for this module␊ |
1011 | ␉␉callbackList_t* callbacks = hook->callbacks;␊ |
1012 | ␊ |
1013 | ␉␉while(callbacks)␊ |
1014 | ␉␉{␊ |
1015 | ␉␉␉// Execute callback␊ |
1016 | ␉␉␉callbacks->callback(arg1, arg2, arg3, arg4);␊ |
1017 | ␉␉␉callbacks = callbacks->next;␊ |
1018 | ␉␉}␊ |
1019 | ␉␉DBG("Hook '%s' executed.\n", name); DBGPAUSE();␊ |
1020 | ␉␉return 1;␊ |
1021 | ␉}␊ |
1022 | ␉else␊ |
1023 | ␉{␊ |
1024 | ␉␉// Callback for this hook doesn't exist;␊ |
1025 | ␉␉DBG("No callbacks for '%s' hook.\n", name);␊ |
1026 | ␉␉return 0;␊ |
1027 | ␉}␊ |
1028 | }␊ |
1029 | ␊ |
1030 | ␊ |
1031 | ␊ |
1032 | /*␊ |
1033 | *␉register_hook_callback( const char* name, void(*callback)())␊ |
1034 | *␉␉name - Name of the module hook to attach to.␊ |
1035 | *␉␉callbacks - The funciton pointer that will be called when the␊ |
1036 | *␉␉␉hook is executed. When registering a new callback name, the callback is added sorted.␊ |
1037 | *␉␉␉NOTE: the hooks take four void* arguments.␊ |
1038 | */␊ |
1039 | void register_hook_callback(const char* name, void(*callback)(void*, void*, void*, void*))␊ |
1040 | {␉␊ |
1041 | ␉DBG("Adding callback for '%s' hook.\n", name); DBGPAUSE();␊ |
1042 | ␉␊ |
1043 | ␉moduleHook_t* hook = hook_exists(name);␊ |
1044 | ␉␊ |
1045 | ␉if(hook)␊ |
1046 | ␉{␊ |
1047 | ␉␉// append␊ |
1048 | ␉␉callbackList_t* newCallback = malloc(sizeof(callbackList_t));␊ |
1049 | ␉␉newCallback->next = hook->callbacks;␊ |
1050 | ␉␉hook->callbacks = newCallback;␊ |
1051 | ␉␉newCallback->callback = callback;␊ |
1052 | ␉}␊ |
1053 | ␉else␊ |
1054 | ␉{␊ |
1055 | ␉␉// create new hook␊ |
1056 | ␉␉moduleHook_t* newHook = malloc(sizeof(moduleHook_t));␉␉␊ |
1057 | ␉␉newHook->name = name;␊ |
1058 | ␉␉newHook->callbacks = malloc(sizeof(callbackList_t));␊ |
1059 | ␉␉newHook->callbacks->callback = callback;␊ |
1060 | ␉␉newHook->callbacks->next = NULL;␊ |
1061 | ␉␉␊ |
1062 | ␉␉newHook->next = moduleCallbacks;␊ |
1063 | ␉␉moduleCallbacks = newHook;␊ |
1064 | ␉␉␊ |
1065 | ␉}␊ |
1066 | ␉␊ |
1067 | #if CONFIG_MODULE_DEBUG␊ |
1068 | ␉//print_hook_list();␊ |
1069 | ␉//getchar();␊ |
1070 | #endif␊ |
1071 | ␉␊ |
1072 | }␊ |
1073 | ␊ |
1074 | ␊ |
1075 | moduleHook_t* hook_exists(const char* name)␊ |
1076 | {␊ |
1077 | ␉moduleHook_t* hooks = moduleCallbacks;␊ |
1078 | ␉␊ |
1079 | ␉// look for a hook. If it exists, return the moduleHook_t*,␊ |
1080 | ␉// If not, return NULL.␊ |
1081 | ␉while(hooks)␊ |
1082 | ␉{␊ |
1083 | ␉␉if(strcmp(name, hooks->name) == 0)␊ |
1084 | ␉␉{␊ |
1085 | ␉␉␉//DBG("Located hook %s\n", name);␊ |
1086 | ␉␉␉return hooks;␊ |
1087 | ␉␉}␊ |
1088 | ␉␉hooks = hooks->next;␊ |
1089 | ␉}␊ |
1090 | ␉//DBG("Hook %s does not exist\n", name);␊ |
1091 | ␉return NULL;␊ |
1092 | ␉␊ |
1093 | }␊ |
1094 | ␊ |
1095 | #if CONFIG_MODULE_DEBUG␊ |
1096 | void print_hook_list()␊ |
1097 | {␊ |
1098 | ␉printf("---Hook Table---\n");␊ |
1099 | ␉␊ |
1100 | ␉moduleHook_t* hooks = moduleCallbacks;␊ |
1101 | ␉while(hooks)␊ |
1102 | ␉{␊ |
1103 | ␉␉printf("Hook: %s\n", hooks->name);␊ |
1104 | ␉␉hooks = hooks->next;␊ |
1105 | ␉}␊ |
1106 | }␊ |
1107 | ␊ |
1108 | #endif␊ |
1109 | ␊ |
1110 | /********************************************************************************/␊ |
1111 | /*␉dyld / Linker Interface␉␉␉␉␉␉␉␉␉␉␉␉␉␉*/␊ |
1112 | /********************************************************************************/␊ |
1113 | ␊ |
1114 | void dyld_stub_binder()␊ |
1115 | {␊ |
1116 | ␉printf("ERROR: dyld_stub_binder was called, should have been take care of by the linker.\n");␊ |
1117 | ␉getchar();␊ |
1118 | }␊ |
1119 | ␊ |
1120 | #else /* CONFIG_MODULES */␊ |
1121 | ␊ |
1122 | int init_module_system()␊ |
1123 | {␊ |
1124 | return 0;␊ |
1125 | }␊ |
1126 | ␊ |
1127 | void load_all_modules()␊ |
1128 | {␊ |
1129 | ␊ |
1130 | }␊ |
1131 | ␊ |
1132 | int execute_hook(const char* name, void* arg1, void* arg2, void* arg3, void* arg4)␊ |
1133 | {␊ |
1134 | return 0;␊ |
1135 | }␊ |
1136 | ␊ |
1137 | void register_hook_callback(const char* name, void(*callback)(void*, void*, void*, void*))␊ |
1138 | {␊ |
1139 | ␉printf("WARNING: register_hook_callback is not supported when compiled in.\n");␊ |
1140 | ␉pause();␊ |
1141 | }␊ |
1142 | ␊ |
1143 | int replace_function(const char* symbol, void* newAddress)␊ |
1144 | {␊ |
1145 | ␉printf("WARNING: replace_functions is not supported when compiled in.\n");␊ |
1146 | ␉pause();␊ |
1147 | ␉return 0;␊ |
1148 | }␊ |
1149 | ␊ |
1150 | void start_built_in_module(const char* name, ␊ |
1151 | const char* author, ␊ |
1152 | const char* description,␊ |
1153 | UInt32 version,␊ |
1154 | UInt32 compat,␊ |
1155 | void(*start_function)(void))␊ |
1156 | {␊ |
1157 | start_function();␊ |
1158 | }␊ |
1159 | ␊ |
1160 | #endif␊ |
1161 | |