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