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