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