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