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