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