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