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