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