Root/
Source at commit 728 created 13 years 2 months ago. By meklort, Updated Symbols.dylib generation. Module linked list rework beginning. USB Legacy off patch still needs fixing. Added a new tool to generate the symbols.dylib. It's a fairly correct dylib file, however nm complains about it (it still works fine though). Updated Symbols.dylib file is smaller (~13%) that the old method and is a bit cleaner. dylib generation needs a bit of cleaning up though) | |
---|---|
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); getc();␊ |
81 | ␉}␉␉␊ |
82 | ␉return 0;␊ |
83 | }␊ |
84 | ␊ |
85 | ␊ |
86 | /*␊ |
87 | * Load all modules in the /Extra/modules/ directory␊ |
88 | * Module depencdies will be loaded first␊ |
89 | * MOdules will only be loaded once. When loaded a module must␊ |
90 | * setup apropriete function calls and hooks as required.␊ |
91 | * NOTE: To ensure a module loads after another you may ␊ |
92 | * link one module with the other. For dyld to allow this, you must␊ |
93 | * reference at least one symbol within the module.␊ |
94 | */␊ |
95 | void load_all_modules()␊ |
96 | {␊ |
97 | ␉char* name;␊ |
98 | ␉long flags;␊ |
99 | ␉long time;␊ |
100 | ␉struct dirstuff* moduleDir = opendir("/Extra/modules/");␊ |
101 | ␉while(readdir(moduleDir, (const char**)&name, &flags, &time) >= 0)␊ |
102 | ␉{␊ |
103 | ␉␉if(strcmp(&name[strlen(name) - sizeof("dylib")], ".dylib") == 0)␊ |
104 | ␉␉{␊ |
105 | ␉␉␉char* tmp = malloc(strlen(name) + 1);␊ |
106 | ␉␉␉strcpy(tmp, name);␊ |
107 | ␉␉␉␊ |
108 | ␉␉␉if(!load_module(tmp))␊ |
109 | ␉␉␉{␊ |
110 | ␉␉␉␉// failed to load␊ |
111 | ␉␉␉␉// free(tmp);␊ |
112 | ␉␉␉}␊ |
113 | ␉␉}␊ |
114 | ␉␉else ␊ |
115 | ␉␉{␊ |
116 | ␉␉␉DBG("Ignoring %s\n", name);␊ |
117 | ␉␉}␊ |
118 | ␊ |
119 | ␉}␊ |
120 | }␊ |
121 | ␊ |
122 | ␊ |
123 | /*␊ |
124 | * Load a module file in /Extra/modules␊ |
125 | * TODO: verify version number of module␊ |
126 | */␊ |
127 | int load_module(char* module)␊ |
128 | {␊ |
129 | ␉void (*module_start)(void) = NULL;␊ |
130 | ␊ |
131 | ␉␊ |
132 | ␉// Check to see if the module has already been loaded␊ |
133 | ␉if(is_module_loaded(module))␊ |
134 | ␉{␊ |
135 | ␉␉return 1;␊ |
136 | ␉}␊ |
137 | ␉␊ |
138 | ␉char modString[128];␊ |
139 | ␉int fh = -1;␊ |
140 | ␉sprintf(modString, "/Extra/modules/%s", module);␊ |
141 | ␉fh = open(modString, 0);␊ |
142 | ␉if(fh < 0)␊ |
143 | ␉{␊ |
144 | ␉␉printf("Unable to locate module %s\n", modString); DBGPAUSE();␊ |
145 | ␉␉getc();␊ |
146 | ␉␉return 0;␊ |
147 | ␉}␊ |
148 | ␉␊ |
149 | ␉unsigned int moduleSize = file_size(fh);␊ |
150 | ␉char* module_base = (char*) malloc(moduleSize);␊ |
151 | ␉if (moduleSize && read(fh, module_base, moduleSize) == moduleSize)␊ |
152 | ␉{␊ |
153 | ␊ |
154 | ␉␉//DBG("Module %s read in.\n", modString);␊ |
155 | ␊ |
156 | ␉␉// Module loaded into memory, parse it␊ |
157 | ␉␉module_start = parse_mach(module_base, &load_module, &add_symbol);␊ |
158 | ␊ |
159 | ␉␉if(module_start && module_start != (void*)0xFFFFFFFF)␊ |
160 | ␉␉{␊ |
161 | ␉␉␉// Notify the system that it was laoded␊ |
162 | ␉␉␉module_loaded(module/*moduleName, moduleVersion, moduleCompat*/);␊ |
163 | ␊ |
164 | ␉␉␉(*module_start)();␉// Start the module␊ |
165 | ␉␉␉DBG("Module %s Loaded.\n", module); DBGPAUSE();␊ |
166 | ␉␉␉␊ |
167 | ␉␉␉//module_entry = malloc(sizeof(moduleList_t); TODO: mode to module_loaded␊ |
168 | ␉␉␉␉␉␉␉␊ |
169 | ␉␉}␊ |
170 | ␉␉else {␊ |
171 | ␉␉␉// The module does not have a valid start function␊ |
172 | ␉␉␉printf("Unable to start %s\n", module);␊ |
173 | ␉␉␉getc();␊ |
174 | ␉␉}␉␉␊ |
175 | ␉}␊ |
176 | ␉else␊ |
177 | ␉{␊ |
178 | ␉␉DBG("Unable to read in module %s\n.", module);␊ |
179 | ␉␉getc();␊ |
180 | ␉}␊ |
181 | ␉close(fh);␊ |
182 | ␊ |
183 | ␉return 1;␊ |
184 | }␊ |
185 | ␊ |
186 | /*␊ |
187 | * add_symbol␊ |
188 | * This function adds a symbol from a module to the list of known symbols ␊ |
189 | * possibly change to a pointer and add this to the Symbol module so that it can␊ |
190 | * adjust it's internal symbol list (sort) to optimize locating new symbols␊ |
191 | * NOTE: returns the address if the symbol is "start", else returns 0xFFFFFFFF␊ |
192 | */␊ |
193 | long long add_symbol(char* symbol, long long addr, char is64)␊ |
194 | {␊ |
195 | ␉if(is64) return 0xFFFFFFFF; // Fixme␊ |
196 | ␊ |
197 | ␉// This only can handle 32bit symbols ␊ |
198 | ␉symbolList_t* entry;␊ |
199 | ␉//DBG("Adding symbol %s at 0x%X\n", symbol, addr);␊ |
200 | ␉␊ |
201 | ␉if(!moduleSymbols)␊ |
202 | ␉{␊ |
203 | ␉␉moduleSymbols = entry = malloc(sizeof(symbolList_t));␊ |
204 | ␊ |
205 | ␉}␊ |
206 | ␉else␊ |
207 | ␉{␊ |
208 | ␉␉entry = moduleSymbols;␊ |
209 | ␉␉while(entry->next)␊ |
210 | ␉␉{␊ |
211 | ␉␉␉entry = entry->next;␊ |
212 | ␉␉}␊ |
213 | ␉␉␊ |
214 | ␉␉entry->next = malloc(sizeof(symbolList_t));␊ |
215 | ␉␉entry = entry->next;␊ |
216 | ␉}␊ |
217 | ␊ |
218 | ␉entry->next = NULL;␊ |
219 | ␉entry->addr = (UInt32)addr;␊ |
220 | ␉entry->symbol = symbol;␊ |
221 | ␉if(strcmp(symbol, "start") == 0)␊ |
222 | ␉{␊ |
223 | ␉␉return addr;␊ |
224 | ␉}␊ |
225 | ␉else␊ |
226 | ␉{␊ |
227 | ␉␉return 0xFFFFFFFF; // fixme␊ |
228 | ␉}␊ |
229 | }␊ |
230 | ␊ |
231 | ␊ |
232 | /*␊ |
233 | * print out the information about the loaded module␊ |
234 | */␊ |
235 | void module_loaded(const char* name/*, UInt32 version, UInt32 compat*/)␊ |
236 | {␊ |
237 | ␉// TODO: insert sorted␊ |
238 | ␉moduleList_t* new_entry = malloc(sizeof(moduleList_t));␊ |
239 | ␊ |
240 | ␉new_entry->next = loadedModules;␊ |
241 | ␉loadedModules = new_entry;␊ |
242 | ␉␊ |
243 | ␉new_entry->name = (char*)name;␊ |
244 | ␉new_entry->base_addr = NULL;␉␉// TODO␊ |
245 | ␉// todo; symbols␊ |
246 | ␉new_entry->version = 0; //version;␊ |
247 | ␉new_entry->compat = 0; //compat;␊ |
248 | }␊ |
249 | ␊ |
250 | int is_module_loaded(const char* name)␊ |
251 | {␊ |
252 | ␉// todo sorted search␊ |
253 | ␉moduleList_t* entry = loadedModules;␊ |
254 | ␉while(entry)␊ |
255 | ␉{␊ |
256 | ␉␉if(strcmp(entry->name, name) == 0)␊ |
257 | ␉␉{␊ |
258 | ␉␉␉DBG("Located module %s\n", name); DBGPAUSE();␊ |
259 | ␉␉␉return 1;␊ |
260 | ␉␉}␊ |
261 | ␉␉else␊ |
262 | ␉␉{␊ |
263 | ␉␉␉entry = entry->next;␊ |
264 | ␉␉}␊ |
265 | ␊ |
266 | ␉}␊ |
267 | ␉DBG("Module %s not found\n", name); DBGPAUSE();␊ |
268 | ␊ |
269 | ␉return 0;␊ |
270 | }␊ |
271 | ␊ |
272 | // Look for symbols using the Smbols moduel function.␊ |
273 | // If non are found, look through the list of module symbols␊ |
274 | unsigned int lookup_all_symbols(const char* name)␊ |
275 | {␊ |
276 | ␉unsigned int addr = 0xFFFFFFFF;␊ |
277 | ␉symbolList_t* entry = moduleSymbols;␊ |
278 | ␉while(entry)␊ |
279 | ␉{␊ |
280 | ␉␉if(strcmp(entry->symbol, name) == 0)␊ |
281 | ␉␉{␊ |
282 | ␉␉␉//DBG("External symbol %s located at 0x%X\n", name, entry->addr);␊ |
283 | ␉␉␉return entry->addr;␊ |
284 | ␉␉}␊ |
285 | ␉␉else␊ |
286 | ␉␉{␊ |
287 | ␉␉␉entry = entry->next;␊ |
288 | ␉␉}␊ |
289 | ␊ |
290 | ␉}␊ |
291 | ␉␊ |
292 | ␉if(lookup_symbol && (UInt32)lookup_symbol != 0xFFFFFFFF)␊ |
293 | ␉{␊ |
294 | ␉␉addr = lookup_symbol(name);␊ |
295 | ␉␉if(addr != 0xFFFFFFFF)␊ |
296 | ␉␉{␊ |
297 | ␉␉␉//DBG("Internal symbol %s located at 0x%X\n", name, addr);␊ |
298 | ␉␉␉return addr;␊ |
299 | ␉␉}␊ |
300 | ␉}␊ |
301 | ␉␊ |
302 | ␊ |
303 | #if DEBUG_MODULES␊ |
304 | ␉verbose("Unable to locate symbol %s\n", name);␊ |
305 | ␉getc();␊ |
306 | #endif␊ |
307 | ␊ |
308 | ␉return 0xFFFFFFFF;␊ |
309 | }␊ |
310 | ␊ |
311 | /********************************************************************************/␊ |
312 | /*␉Macho Parser␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉*/␊ |
313 | /********************************************************************************/␊ |
314 | ␊ |
315 | /*␊ |
316 | * Parse through a macho module. The module will be rebased and binded␊ |
317 | * as specified in the macho header. If the module is sucessfuly laoded␊ |
318 | * the module iinit address will be returned.␊ |
319 | * NOTE; all dependecies will be loaded before this module is started␊ |
320 | * NOTE: If the module is unable to load ot completeion, the modules␊ |
321 | * symbols will still be available (TODO: fix this). This should not␊ |
322 | * happen as all dependencies are verified before the sybols are read in.␊ |
323 | */␊ |
324 | void* parse_mach(void* binary, int(*dylib_loader)(char*), long long(*symbol_handler)(char*, long long, char))␉// TODO: add param to specify valid archs␊ |
325 | {␉␊ |
326 | ␉char is64 = false;␊ |
327 | ␉void (*module_start)(void) = NULL;␊ |
328 | ␉␊ |
329 | ␉// Module info␊ |
330 | ␉/*char* moduleName = NULL;␊ |
331 | ␉ UInt32 moduleVersion = 0;␊ |
332 | ␉ UInt32 moduleCompat = 0;␊ |
333 | ␉ */␊ |
334 | ␉// TODO convert all of the structs to a union␊ |
335 | ␉struct load_command *loadCommand = NULL;␊ |
336 | ␉struct dylib_command* dylibCommand = NULL;␊ |
337 | ␉struct dyld_info_command* dyldInfoCommand = NULL;␊ |
338 | ␉␊ |
339 | ␉struct symtab_command* symtabCommand = NULL;␊ |
340 | ␉struct segment_command *segCommand = NULL;␊ |
341 | ␉struct segment_command_64 *segCommand64 = NULL;␊ |
342 | ␉␊ |
343 | ␉//struct dysymtab_command* dysymtabCommand = NULL;␊ |
344 | ␉UInt32 binaryIndex = 0;␊ |
345 | ␉UInt16 cmd = 0;␊ |
346 | ␉␊ |
347 | ␉textSection = 0;␊ |
348 | ␉textAddress = 0;␉// reinitialize text location in case it doesn't exist;␊ |
349 | ␉␊ |
350 | ␉// Parse through the load commands␊ |
351 | ␉if(((struct mach_header*)binary)->magic == MH_MAGIC)␊ |
352 | ␉{␊ |
353 | ␉␉is64 = false;␊ |
354 | ␉␉binaryIndex += sizeof(struct mach_header);␊ |
355 | ␉}␊ |
356 | ␉else if(((struct mach_header_64*)binary)->magic == MH_MAGIC_64)␊ |
357 | ␉{␊ |
358 | ␉␉// NOTE: modules cannot be 64bit...␊ |
359 | ␉␉is64 = true;␊ |
360 | ␉␉binaryIndex += sizeof(struct mach_header_64);␊ |
361 | ␉}␊ |
362 | ␉else␊ |
363 | ␉{␊ |
364 | ␉␉printf("Invalid mach magic 0x%X\n", ((struct mach_header*)binary)->magic);␊ |
365 | ␉␉getc();␊ |
366 | ␉␉return NULL;␊ |
367 | ␉}␊ |
368 | ␉␊ |
369 | ␉␊ |
370 | ␉␊ |
371 | ␉/*if(((struct mach_header*)binary)->filetype != MH_DYLIB)␊ |
372 | ␉ {␊ |
373 | ␉ printf("Module is not a dylib. Unable to load.\n");␊ |
374 | ␉ getc();␊ |
375 | ␉ return NULL; // Module is in the incorrect format␊ |
376 | ␉ }*/␊ |
377 | ␉␊ |
378 | ␉while(cmd < ((struct mach_header*)binary)->ncmds)␊ |
379 | ␉{␊ |
380 | ␉␉cmd++;␊ |
381 | ␉␉␊ |
382 | ␉␉loadCommand = binary + binaryIndex;␊ |
383 | ␉␉UInt32 cmdSize = loadCommand->cmdsize;␊ |
384 | ␉␉␊ |
385 | ␉␉␊ |
386 | ␉␉switch ((loadCommand->cmd & 0x7FFFFFFF))␊ |
387 | ␉␉{␊ |
388 | ␉␉␉// TODO: sepeare function to handel appropriate sections␊ |
389 | ␉␉␉case LC_SYMTAB:␊ |
390 | ␉␉␉␉symtabCommand = binary + binaryIndex;␊ |
391 | ␉␉␉␉break;␊ |
392 | ␉␉␉␉␊ |
393 | ␉␉␉case LC_SEGMENT: // 32bit macho␊ |
394 | ␉␉␉␉segCommand = binary + binaryIndex;␊ |
395 | ␉␉␉␉␊ |
396 | ␉␉␉␉//printf("Segment name is %s\n", segCommand->segname);␊ |
397 | ␉␉␉␉␊ |
398 | ␉␉␉␉if(strcmp("__TEXT", segCommand->segname) == 0)␊ |
399 | ␉␉␉␉{␊ |
400 | ␉␉␉␉␉UInt32 sectionIndex;␊ |
401 | ␉␉␉␉␉␊ |
402 | ␉␉␉␉␉sectionIndex = sizeof(struct segment_command);␊ |
403 | ␉␉␉␉␉␊ |
404 | ␉␉␉␉␉struct section *sect;␊ |
405 | ␉␉␉␉␉␊ |
406 | ␉␉␉␉␉while(sectionIndex < segCommand->cmdsize)␊ |
407 | ␉␉␉␉␉{␊ |
408 | ␉␉␉␉␉␉sect = binary + binaryIndex + sectionIndex;␊ |
409 | ␉␉␉␉␉␉␊ |
410 | ␉␉␉␉␉␉sectionIndex += sizeof(struct section);␊ |
411 | ␉␉␉␉␉␉␊ |
412 | ␉␉␉␉␉␉␊ |
413 | ␉␉␉␉␉␉if(strcmp("__text", sect->sectname) == 0)␊ |
414 | ␉␉␉␉␉␉{␊ |
415 | ␉␉␉␉␉␉␉// __TEXT,__text found, save the offset and address for when looking for the calls.␊ |
416 | ␉␉␉␉␉␉␉textSection = sect->offset;␊ |
417 | ␉␉␉␉␉␉␉textAddress = sect->addr;␊ |
418 | ␉␉␉␉␉␉␉break;␊ |
419 | ␉␉␉␉␉␉}␉␉␉␉␉␊ |
420 | ␉␉␉␉␉}␊ |
421 | ␉␉␉␉}␊ |
422 | ␉␉␉␉␊ |
423 | ␉␉␉␉break;␊ |
424 | ␉␉␉case LC_SEGMENT_64:␉// 64bit macho's␊ |
425 | ␉␉␉␉segCommand64 = binary + binaryIndex;␊ |
426 | ␉␉␉␉␊ |
427 | ␉␉␉␉//printf("Segment name is %s\n", segCommand->segname);␊ |
428 | ␉␉␉␉␊ |
429 | ␉␉␉␉if(strcmp("__TEXT", segCommand64->segname) == 0)␊ |
430 | ␉␉␉␉{␊ |
431 | ␉␉␉␉␉UInt32 sectionIndex;␊ |
432 | ␉␉␉␉␉␊ |
433 | ␉␉␉␉␉sectionIndex = sizeof(struct segment_command_64);␊ |
434 | ␉␉␉␉␉␊ |
435 | ␉␉␉␉␉struct section_64 *sect;␊ |
436 | ␉␉␉␉␉␊ |
437 | ␉␉␉␉␉while(sectionIndex < segCommand64->cmdsize)␊ |
438 | ␉␉␉␉␉{␊ |
439 | ␉␉␉␉␉␉sect = binary + binaryIndex + sectionIndex;␊ |
440 | ␉␉␉␉␉␉␊ |
441 | ␉␉␉␉␉␉sectionIndex += sizeof(struct section_64);␊ |
442 | ␉␉␉␉␉␉␊ |
443 | ␉␉␉␉␉␉␊ |
444 | ␉␉␉␉␉␉if(strcmp("__text", sect->sectname) == 0)␊ |
445 | ␉␉␉␉␉␉{␊ |
446 | ␉␉␉␉␉␉␉// __TEXT,__text found, save the offset and address for when looking for the calls.␊ |
447 | ␉␉␉␉␉␉␉textSection = sect->offset;␊ |
448 | ␉␉␉␉␉␉␉textAddress = sect->addr;␊ |
449 | ␉␉␉␉␉␉␉␊ |
450 | ␉␉␉␉␉␉␉break;␊ |
451 | ␉␉␉␉␉␉}␉␉␉␉␉␊ |
452 | ␉␉␉␉␉}␊ |
453 | ␉␉␉␉}␉␉␉␉␊ |
454 | ␉␉␉␉␊ |
455 | ␉␉␉␉break;␊ |
456 | ␉␉␉␉␊ |
457 | ␉␉␉case LC_DYSYMTAB:␊ |
458 | ␉␉␉␉break;␊ |
459 | ␉␉␉␉␊ |
460 | ␉␉␉case LC_LOAD_DYLIB:␊ |
461 | ␉␉␉case LC_LOAD_WEAK_DYLIB ^ LC_REQ_DYLD:␊ |
462 | ␉␉␉␉dylibCommand = binary + binaryIndex;␊ |
463 | ␉␉␉␉char* module = binary + binaryIndex + ((UInt32)*((UInt32*)&dylibCommand->dylib.name));␊ |
464 | ␉␉␉␉// TODO: verify version␊ |
465 | ␉␉␉␉// =␉dylibCommand->dylib.current_version;␊ |
466 | ␉␉␉␉// =␉dylibCommand->dylib.compatibility_version;␊ |
467 | ␉␉␉␉if(dylib_loader)␊ |
468 | ␉␉␉␉{␊ |
469 | ␉␉␉␉␉char* name = malloc(strlen(module) + strlen(".dylib") + 1);␊ |
470 | ␉␉␉␉␉sprintf(name, "%s.dylib", module);␊ |
471 | ␉␉␉␉␉␊ |
472 | ␉␉␉␉␉if (!dylib_loader(name))␊ |
473 | ␉␉␉␉␉{␊ |
474 | ␉␉␉␉␉␉free(name);␊ |
475 | ␉␉␉␉␉␉// Unable to load dependancy␊ |
476 | ␉␉␉␉␉␉//return NULL;␊ |
477 | ␉␉␉␉␉}␊ |
478 | ␉␉␉␉}␊ |
479 | ␉␉␉␉␊ |
480 | ␉␉␉␉break;␊ |
481 | ␉␉␉␉␊ |
482 | ␉␉␉case LC_ID_DYLIB:␊ |
483 | ␉␉␉␉dylibCommand = binary + binaryIndex;␊ |
484 | ␉␉␉␉/*moduleName =␉binary + binaryIndex + ((UInt32)*((UInt32*)&dylibCommand->dylib.name));␊ |
485 | ␉␉␉␉ moduleVersion =␉dylibCommand->dylib.current_version;␊ |
486 | ␉␉␉␉ moduleCompat =␉dylibCommand->dylib.compatibility_version;␊ |
487 | ␉␉␉␉ */␊ |
488 | ␉␉␉␉break;␊ |
489 | ␉␉␉␉␊ |
490 | ␉␉␉case LC_DYLD_INFO:␊ |
491 | ␉␉␉//case LC_DYLD_INFO_ONLY:␉// compressed info, 10.6+ macho files, already handeled␊ |
492 | ␉␉␉␉// Bind and rebase info is stored here␊ |
493 | ␉␉␉␉dyldInfoCommand = binary + binaryIndex;␊ |
494 | ␉␉␉␉break;␊ |
495 | ␉␉␉␉␊ |
496 | ␉␉␉case LC_UUID:␊ |
497 | ␉␉␉␉break;␊ |
498 | ␉␉␉␉␊ |
499 | ␉␉␉case LC_UNIXTHREAD:␊ |
500 | ␉␉␉␉break;␊ |
501 | ␉␉␉␉␊ |
502 | ␉␉␉default:␊ |
503 | ␉␉␉␉DBG("Unhandled loadcommand 0x%X\n", loadCommand->cmd & 0x7FFFFFFF);␊ |
504 | ␉␉␉␉break;␊ |
505 | ␉␉␉␉␊ |
506 | ␉␉}␊ |
507 | ␉␉␊ |
508 | ␉␉binaryIndex += cmdSize;␊ |
509 | ␉}␊ |
510 | ␉//if(!moduleName) return NULL;␊ |
511 | ␉␊ |
512 | ␉␊ |
513 | ␉// bind_macho uses the symbols, if the textAdd does not exist (Symbols.dylib, no code), addresses are static and not relative␊ |
514 | ␉module_start = (void*)handle_symtable((UInt32)binary, symtabCommand, symbol_handler, is64);␊ |
515 | ␉␊ |
516 | ␉// Rebase the module before binding it.␊ |
517 | ␉if(dyldInfoCommand && dyldInfoCommand->rebase_off)␊ |
518 | ␉{␊ |
519 | ␉␉rebase_macho(binary, (char*)dyldInfoCommand->rebase_off, dyldInfoCommand->rebase_size);␊ |
520 | ␉}␊ |
521 | ␉␊ |
522 | ␉if(dyldInfoCommand && dyldInfoCommand->bind_off)␊ |
523 | ␉{␊ |
524 | ␉␉bind_macho(binary, (char*)dyldInfoCommand->bind_off, dyldInfoCommand->bind_size);␊ |
525 | ␉}␊ |
526 | ␉␊ |
527 | ␉if(dyldInfoCommand && dyldInfoCommand->weak_bind_off)␊ |
528 | ␉{␊ |
529 | ␉␉// NOTE: this currently should never happen.␊ |
530 | ␉␉bind_macho(binary, (char*)dyldInfoCommand->weak_bind_off, dyldInfoCommand->weak_bind_size);␊ |
531 | ␉}␊ |
532 | ␉␊ |
533 | ␉if(dyldInfoCommand && dyldInfoCommand->lazy_bind_off)␊ |
534 | ␉{␊ |
535 | ␉␉// NOTE: we are binding the lazy pointers as a module is laoded,␊ |
536 | ␉␉// This should be changed to bind when a symbol is referened at runtime instead.␊ |
537 | ␉␉bind_macho(binary, (char*)dyldInfoCommand->lazy_bind_off, dyldInfoCommand->lazy_bind_size);␊ |
538 | ␉}␊ |
539 | ␉␊ |
540 | ␉return module_start;␊ |
541 | ␉␊ |
542 | }␊ |
543 | ␊ |
544 | /*␊ |
545 | * parse the symbol table␊ |
546 | * Lookup any undefined symbols␊ |
547 | */␊ |
548 | ␊ |
549 | unsigned int handle_symtable(UInt32 base, struct symtab_command* symtabCommand, long long(*symbol_handler)(char*, long long, char), char is64)␊ |
550 | {␊ |
551 | ␉// TODO: verify that the _TEXT,_text segment starts at the same locaiton in the file. If not␊ |
552 | ␉//␉␉␉subtract the vmaddress and add the actual file address back on. (NOTE: if compiled properly, not needed)␊ |
553 | ␉␊ |
554 | ␉unsigned int module_start = 0xFFFFFFFF;␊ |
555 | ␉␊ |
556 | ␉UInt32 symbolIndex = 0;␊ |
557 | ␉char* symbolString = base + (char*)symtabCommand->stroff;␊ |
558 | ␉//char* symbolTable = base + symtabCommand->symoff;␊ |
559 | ␉if(!is64)␊ |
560 | ␉{␊ |
561 | ␉␉struct nlist* symbolEntry = (void*)base + symtabCommand->symoff;␊ |
562 | ␉␉while(symbolIndex < symtabCommand->nsyms)␊ |
563 | ␉␉{␊ |
564 | ␉␉␉// If the symbol is exported by this module␊ |
565 | ␉␉␉if(symbolEntry->n_value &&␊ |
566 | ␉␉␉ symbol_handler(symbolString + symbolEntry->n_un.n_strx, textAddress ? (long long)base + symbolEntry->n_value : symbolEntry->n_value, is64) != 0xFFFFFFFF)␊ |
567 | ␉␉␉{␊ |
568 | ␉␉␉␉␊ |
569 | ␉␉␉␉// Module start located. Start is an alias so don't register it␊ |
570 | ␉␉␉␉module_start = textAddress ? base + symbolEntry->n_value : symbolEntry->n_value;␊ |
571 | ␉␉␉}␊ |
572 | ␉␉␉␊ |
573 | ␉␉␉symbolEntry++;␊ |
574 | ␉␉␉symbolIndex++;␉// TODO remove␊ |
575 | ␉␉}␊ |
576 | ␉}␊ |
577 | ␉else␊ |
578 | ␉{␊ |
579 | ␉␉struct nlist_64* symbolEntry = (void*)base + symtabCommand->symoff;␊ |
580 | ␉␉// NOTE First entry is *not* correct, but we can ignore it (i'm getting radar:// right now)␉␊ |
581 | ␉␉while(symbolIndex < symtabCommand->nsyms)␊ |
582 | ␉␉{␊ |
583 | ␉␉␉␊ |
584 | ␉␉␉␊ |
585 | ␉␉␉// If the symbol is exported by this module␊ |
586 | ␉␉␉if(symbolEntry->n_value &&␊ |
587 | ␉␉␉ symbol_handler(symbolString + symbolEntry->n_un.n_strx, textAddress ? (long long)base + symbolEntry->n_value : symbolEntry->n_value, is64) != 0xFFFFFFFF)␊ |
588 | ␉␉␉{␊ |
589 | ␉␉␉␉␊ |
590 | ␉␉␉␉// Module start located. Start is an alias so don't register it␊ |
591 | ␉␉␉␉module_start = textAddress ? base + symbolEntry->n_value : symbolEntry->n_value;␊ |
592 | ␉␉␉}␊ |
593 | ␉␉␉␊ |
594 | ␉␉␉symbolEntry++;␊ |
595 | ␉␉␉symbolIndex++;␉// TODO remove␊ |
596 | ␉␉}␊ |
597 | ␉}␊ |
598 | ␉␊ |
599 | ␉return module_start;␊ |
600 | ␉␊ |
601 | }␊ |
602 | ␊ |
603 | // Based on code from dylibinfo.cpp and ImageLoaderMachOCompressed.cpp␊ |
604 | void rebase_macho(void* base, char* rebase_stream, UInt32 size)␊ |
605 | {␊ |
606 | ␉rebase_stream += (UInt32)base;␊ |
607 | ␉␊ |
608 | ␉UInt8 immediate = 0;␊ |
609 | ␉UInt8 opcode = 0;␊ |
610 | ␉UInt8 type = 0;␊ |
611 | ␉␊ |
612 | ␉UInt32 segmentAddress = 0;␊ |
613 | ␉␊ |
614 | ␉␊ |
615 | ␉␊ |
616 | ␉UInt32 tmp = 0;␊ |
617 | ␉UInt32 tmp2 = 0;␊ |
618 | ␉UInt8 bits = 0;␊ |
619 | ␉int index = 0;␊ |
620 | ␉␊ |
621 | ␉//int done = 0;␊ |
622 | ␉unsigned int i = 0;␊ |
623 | ␉␊ |
624 | ␉while(/*!done &&*/ i < size)␊ |
625 | ␉{␊ |
626 | ␉␉immediate = rebase_stream[i] & REBASE_IMMEDIATE_MASK;␊ |
627 | ␉␉opcode = rebase_stream[i] & REBASE_OPCODE_MASK;␊ |
628 | ␉␉␊ |
629 | ␉␉␊ |
630 | ␉␉switch(opcode)␊ |
631 | ␉␉{␊ |
632 | ␉␉␉case REBASE_OPCODE_DONE:␊ |
633 | ␉␉␉␉// Rebase complete.␊ |
634 | ␉␉␉␉//done = 1;␊ |
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 | ␉else ␊ |
1105 | ␉{␊ |
1106 | ␉␉return 0;␊ |
1107 | ␉}␊ |
1108 | ␉␊ |
1109 | }␊ |
1110 | ␊ |
1111 | ␊ |
1112 | /*␊ |
1113 | *␉execute_hook( const char* name )␊ |
1114 | *␉␉name - Name of the module hook␊ |
1115 | *␉␉␉If any callbacks have been registered for this hook␊ |
1116 | *␉␉␉they will be executed now in the same order that the␊ |
1117 | *␉␉␉hooks were added.␊ |
1118 | */␊ |
1119 | int execute_hook(const char* name, void* arg1, void* arg2, void* arg3, void* arg4)␊ |
1120 | {␊ |
1121 | ␉DBG("Attempting to execute hook '%s'\n", name); DBGPAUSE();␊ |
1122 | ␉moduleHook_t* hook = hook_exists(name);␊ |
1123 | ␉␊ |
1124 | ␉if(hook)␊ |
1125 | ␉{␊ |
1126 | ␉␉// Loop through all callbacks for this module␊ |
1127 | ␉␉callbackList_t* callbacks = hook->callbacks;␊ |
1128 | ␉␉␊ |
1129 | ␉␉while(callbacks)␊ |
1130 | ␉␉{␊ |
1131 | ␉␉␉//DBG("Executing '%s' callback at 0x%X.\n", name, callbacks->callback);␊ |
1132 | ␉␉␉// Execute callback␊ |
1133 | ␉␉␉callbacks->callback(arg1, arg2, arg3, arg4);␊ |
1134 | ␉␉␉callbacks = callbacks->next;␊ |
1135 | ␉␉␉//DBG("Hook '%s' callback executed, next is 0x%X.\n", name, callbacks);␊ |
1136 | ␉␉␉␊ |
1137 | ␉␉}␊ |
1138 | ␉␉DBG("Hook '%s' executed.\n", name); DBGPAUSE();␊ |
1139 | ␉␉return 1;␊ |
1140 | ␉}␊ |
1141 | ␉else␊ |
1142 | ␉{␊ |
1143 | ␉␉// Callback for this hook doesn't exist;␊ |
1144 | ␉␉DBG("No callbacks for '%s' hook.\n", name);␊ |
1145 | ␉␉return 0;␊ |
1146 | ␉}␊ |
1147 | }␊ |
1148 | ␊ |
1149 | ␊ |
1150 | ␊ |
1151 | /*␊ |
1152 | *␉register_hook_callback( const char* name, void(*callback)())␊ |
1153 | *␉␉name - Name of the module hook to attach to.␊ |
1154 | *␉␉callbacks - The funciton pointer that will be called when the␊ |
1155 | *␉␉␉hook is executed. When registering a new callback name, the callback is added sorted.␊ |
1156 | *␉␉␉NOTE: the hooks take four void* arguments.␊ |
1157 | */␊ |
1158 | void register_hook_callback(const char* name, void(*callback)(void*, void*, void*, void*))␊ |
1159 | {␉␊ |
1160 | ␉DBG("Adding callback for '%s' hook.\n", name); DBGPAUSE();␊ |
1161 | ␉␊ |
1162 | ␉moduleHook_t* hook = hook_exists(name);␊ |
1163 | ␉␊ |
1164 | ␉if(hook)␊ |
1165 | ␉{␊ |
1166 | ␉␉// append␊ |
1167 | ␉␉callbackList_t* newCallback = malloc(sizeof(callbackList_t));␊ |
1168 | ␉␉newCallback->next = hook->callbacks;␊ |
1169 | ␉␉hook->callbacks = newCallback;␊ |
1170 | ␉␉newCallback->callback = callback;␊ |
1171 | ␉}␊ |
1172 | ␉else␊ |
1173 | ␉{␊ |
1174 | ␉␉// create new hook␊ |
1175 | ␉␉moduleHook_t* newHook = malloc(sizeof(moduleHook_t));␉␉␊ |
1176 | ␉␉newHook->name = name;␊ |
1177 | ␉␉newHook->callbacks = malloc(sizeof(callbackList_t));␊ |
1178 | ␉␉newHook->callbacks->callback = callback;␊ |
1179 | ␉␉newHook->callbacks->next = NULL;␊ |
1180 | ␉␉␊ |
1181 | ␉␉newHook->next = moduleCallbacks;␊ |
1182 | ␉␉moduleCallbacks = newHook;␊ |
1183 | ␉␉␊ |
1184 | ␉}␊ |
1185 | ␉␊ |
1186 | #if DEBUG_MODULES␊ |
1187 | ␉//print_hook_list();␊ |
1188 | ␉//getc();␊ |
1189 | #endif␊ |
1190 | ␉␊ |
1191 | }␊ |
1192 | ␊ |
1193 | ␊ |
1194 | moduleHook_t* hook_exists(const char* name)␊ |
1195 | {␊ |
1196 | ␉moduleHook_t* hooks = moduleCallbacks;␊ |
1197 | ␉␊ |
1198 | ␉// look for a hook. If it exists, return the moduleHook_t*,␊ |
1199 | ␉// If not, return NULL.␊ |
1200 | ␉while(hooks)␊ |
1201 | ␉{␊ |
1202 | ␉␉if(strcmp(name, hooks->name) == 0)␊ |
1203 | ␉␉{␊ |
1204 | ␉␉␉//DBG("Located hook %s\n", name);␊ |
1205 | ␉␉␉return hooks;␊ |
1206 | ␉␉}␊ |
1207 | ␉␉hooks = hooks->next;␊ |
1208 | ␉}␊ |
1209 | ␉//DBG("Hook %s does not exist\n", name);␊ |
1210 | ␉return NULL;␊ |
1211 | ␉␊ |
1212 | }␊ |
1213 | ␊ |
1214 | #if DEBUG_MODULES␊ |
1215 | void print_hook_list()␊ |
1216 | {␊ |
1217 | ␉printf("---Hook Table---\n");␊ |
1218 | ␉␊ |
1219 | ␉moduleHook_t* hooks = moduleCallbacks;␊ |
1220 | ␉while(hooks)␊ |
1221 | ␉{␊ |
1222 | ␉␉printf("Hook: %s\n", hooks->name);␊ |
1223 | ␉␉hooks = hooks->next;␊ |
1224 | ␉}␊ |
1225 | }␊ |
1226 | #endif␊ |
1227 | ␊ |
1228 | /********************************************************************************/␊ |
1229 | /*␉dyld / Linker Interface␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉*/␊ |
1230 | /********************************************************************************/␊ |
1231 | ␊ |
1232 | void dyld_stub_binder()␊ |
1233 | {␊ |
1234 | ␉// TODO: actualy impliment this function (asm)␊ |
1235 | ␉printf("ERROR: dyld_stub_binder was called, should have been take care of by the linker.\n");␊ |
1236 | ␉getc();␊ |
1237 | }␊ |
1238 |