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