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