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