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