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