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