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