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