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