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