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