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