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