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