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