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