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