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