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