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