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