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 load_command *loadCommand = NULL;␊ |
368 | ␉␉UInt32 binaryIndex = 0;␊ |
369 | ␉␉UInt16 cmd = 0;␊ |
370 | ␉␉␊ |
371 | ␉␉// Parse through the load commands␊ |
372 | ␉␉if(((struct mach_header*)binary)->magic == MH_MAGIC)␊ |
373 | ␉␉{␊ |
374 | ␉␉␉is64 = false;␊ |
375 | ␉␉␉binaryIndex += sizeof(struct mach_header);␊ |
376 | ␉␉}␊ |
377 | ␉␉else if(((struct mach_header_64*)binary)->magic == MH_MAGIC_64)␊ |
378 | ␉␉{␊ |
379 | ␉␉␉// NOTE: modules cannot be 64bit...␊ |
380 | ␉␉␉is64 = true;␊ |
381 | ␉␉␉binaryIndex += sizeof(struct mach_header_64);␊ |
382 | ␉␉}␊ |
383 | ␉␉else␊ |
384 | ␉␉{␊ |
385 | ␉␉␉printf("Modules: Invalid mach magic\n");␊ |
386 | ␉␉␉getc();␊ |
387 | ␉␉␉return NULL;␊ |
388 | ␉␉}␊ |
389 | ␉␉␊ |
390 | ␉␉␊ |
391 | ␉␉␊ |
392 | ␉␉/*if(((struct mach_header*)binary)->filetype != MH_DYLIB)␊ |
393 | ␉␉ {␊ |
394 | ␉␉ printf("Module is not a dylib. Unable to load.\n");␊ |
395 | ␉␉ getc();␊ |
396 | ␉␉ return NULL; // Module is in the incorrect format␊ |
397 | ␉␉ }*/␊ |
398 | ␉␉␊ |
399 | ␉␉while(cmd < ((struct mach_header*)binary)->ncmds)␊ |
400 | ␉␉{␊ |
401 | ␉␉␉cmd++;␊ |
402 | ␉␉␉␊ |
403 | ␉␉␉loadCommand = binary + binaryIndex;␊ |
404 | ␉␉␉UInt32 cmdSize = loadCommand->cmdsize;␊ |
405 | ␉␉␉␊ |
406 | ␉␉␉␊ |
407 | ␉␉␉switch ((loadCommand->cmd & 0x7FFFFFFF))␊ |
408 | ␉␉␉{␊ |
409 | ␉␉␉␉case LC_SYMTAB:␊ |
410 | ␉␉␉␉␉symtabCommand = binary + binaryIndex;␊ |
411 | ␉␉␉␉␉break;␊ |
412 | ␉␉␉␉␉␊ |
413 | ␉␉␉␉case LC_SEGMENT: // 32bit macho␊ |
414 | ␉␉␉␉{␊ |
415 | ␉␉␉␉␉segCommand = binary + binaryIndex;␊ |
416 | ␉␉␉␉␉␊ |
417 | ␉␉␉␉␉//printf("Segment name is %s\n", segCommand->segname);␊ |
418 | ␉␉␉␉␉␊ |
419 | ␉␉␉␉␉if(strcmp("__TEXT", segCommand->segname) == 0)␊ |
420 | ␉␉␉␉␉{␊ |
421 | ␉␉␉␉␉␉UInt32 sectionIndex;␊ |
422 | ␉␉␉␉␉␉␊ |
423 | #if DEBUG_MODULES␊ |
424 | ␉␉␉␉␉␉unsigned long fileaddr;␊ |
425 | ␉␉␉␉␉␉long filesize;␉␉␉␉␉␊ |
426 | ␉␉␉␉␉␉vmaddr = (segCommand->vmaddr & 0x3fffffff);␊ |
427 | ␉␉␉␉␉␉vmsize = segCommand->vmsize;␉ ␊ |
428 | ␉␉␉␉␉␉fileaddr = ((unsigned long)(binary + binaryIndex) + segCommand->fileoff);␊ |
429 | ␉␉␉␉␉␉filesize = segCommand->filesize;␊ |
430 | ␉␉␉␉␉␉␊ |
431 | ␉␉␉␉␉␉printf("segname: %s, vmaddr: %x, vmsize: %x, fileoff: %x, filesize: %x, nsects: %d, flags: %x.\n",␊ |
432 | ␉␉␉␉␉␉␉ segCommand->segname, (unsigned)vmaddr, (unsigned)vmsize, (unsigned)fileaddr, (unsigned)filesize,␊ |
433 | ␉␉␉␉␉␉␉ (unsigned) segCommand->nsects, (unsigned)segCommand->flags);␊ |
434 | #if DEBUG_MODULES==2␊ |
435 | ␉␉␉␉␉␉␊ |
436 | ␉␉␉␉␉␉getc();␊ |
437 | #endif␊ |
438 | #endif␊ |
439 | ␉␉␉␉␉␉␊ |
440 | ␉␉␉␉␉␉sectionIndex = sizeof(struct segment_command);␊ |
441 | ␉␉␉␉␉␉␊ |
442 | ␉␉␉␉␉␉struct section *sect;␊ |
443 | ␉␉␉␉␉␉␊ |
444 | ␉␉␉␉␉␉while(sectionIndex < segCommand->cmdsize)␊ |
445 | ␉␉␉␉␉␉{␊ |
446 | ␉␉␉␉␉␉␉sect = binary + binaryIndex + sectionIndex;␊ |
447 | ␉␉␉␉␉␉␉␊ |
448 | ␉␉␉␉␉␉␉sectionIndex += sizeof(struct section);␊ |
449 | ␉␉␉␉␉␉␉␊ |
450 | ␉␉␉␉␉␉␉␊ |
451 | ␉␉␉␉␉␉␉if(strcmp("__text", sect->sectname) == 0)␊ |
452 | ␉␉␉␉␉␉␉{␊ |
453 | ␉␉␉␉␉␉␉␉// __TEXT,__text found, save the offset and address for when looking for the calls.␊ |
454 | ␉␉␉␉␉␉␉␉textSection = sect->offset;␊ |
455 | ␉␉␉␉␉␉␉␉textAddress = sect->addr;␊ |
456 | ␉␉␉␉␉␉␉␉break;␊ |
457 | ␉␉␉␉␉␉␉}␉␉␉␉␉␊ |
458 | ␉␉␉␉␉␉}␊ |
459 | ␉␉␉␉␉}␊ |
460 | ␉␉␉␉␉break;␊ |
461 | ␉␉␉␉}␉␉␉␉␊ |
462 | ␉␉␉␉case LC_SEGMENT_64:␉// 64bit macho's␊ |
463 | ␉␉␉␉{␊ |
464 | ␉␉␉␉␉segCommand64 = binary + binaryIndex;␊ |
465 | ␉␉␉␉␉␊ |
466 | ␉␉␉␉␉//printf("Segment name is %s\n", segCommand->segname);␊ |
467 | ␉␉␉␉␉␊ |
468 | ␉␉␉␉␉if(strcmp("__TEXT", segCommand64->segname) == 0)␊ |
469 | ␉␉␉␉␉{␊ |
470 | ␉␉␉␉␉␉UInt32 sectionIndex;␊ |
471 | ␉␉␉␉␉␉␊ |
472 | #if DEBUG_MODULES␉␉␉␉␉␉␊ |
473 | ␉␉␉␉␉␉unsigned long fileaddr;␊ |
474 | ␉␉␉␉␉␉long filesize;␉␉␉␉␉␊ |
475 | ␉␉␉␉␉␉vmaddr = (segCommand64->vmaddr & 0x3fffffff);␊ |
476 | ␉␉␉␉␉␉vmsize = segCommand64->vmsize;␉ ␊ |
477 | ␉␉␉␉␉␉fileaddr = ((unsigned long)(binary + binaryIndex) + segCommand64->fileoff);␊ |
478 | ␉␉␉␉␉␉filesize = segCommand64->filesize;␊ |
479 | ␉␉␉␉␉␉␊ |
480 | ␉␉␉␉␉␉printf("segname: %s, vmaddr: %x, vmsize: %x, fileoff: %x, filesize: %x, nsects: %d, flags: %x.\n",␊ |
481 | ␉␉␉␉␉␉␉ segCommand64->segname, (unsigned)vmaddr, (unsigned)vmsize, (unsigned)fileaddr, (unsigned)filesize,␊ |
482 | ␉␉␉␉␉␉␉ (unsigned) segCommand64->nsects, (unsigned)segCommand64->flags);␊ |
483 | #if DEBUG_MODULES==2␊ |
484 | ␉␉␉␉␉␉␊ |
485 | ␉␉␉␉␉␉getc();␊ |
486 | #endif␊ |
487 | #endif␊ |
488 | ␉␉␉␉␉␉␊ |
489 | ␉␉␉␉␉␉sectionIndex = sizeof(struct segment_command_64);␊ |
490 | ␉␉␉␉␉␉␊ |
491 | ␉␉␉␉␉␉struct section_64 *sect;␊ |
492 | ␉␉␉␉␉␉␊ |
493 | ␉␉␉␉␉␉while(sectionIndex < segCommand64->cmdsize)␊ |
494 | ␉␉␉␉␉␉{␊ |
495 | ␉␉␉␉␉␉␉sect = binary + binaryIndex + sectionIndex;␊ |
496 | ␉␉␉␉␉␉␉␊ |
497 | ␉␉␉␉␉␉␉sectionIndex += sizeof(struct section_64);␊ |
498 | ␉␉␉␉␉␉␉␊ |
499 | ␉␉␉␉␉␉␉␊ |
500 | ␉␉␉␉␉␉␉if(strcmp("__text", sect->sectname) == 0)␊ |
501 | ␉␉␉␉␉␉␉{␊ |
502 | ␉␉␉␉␉␉␉␉// __TEXT,__text found, save the offset and address for when looking for the calls.␊ |
503 | ␉␉␉␉␉␉␉␉textSection = sect->offset;␊ |
504 | ␉␉␉␉␉␉␉␉textAddress = sect->addr;␊ |
505 | ␉␉␉␉␉␉␉␉␊ |
506 | ␉␉␉␉␉␉␉␉break;␊ |
507 | ␉␉␉␉␉␉␉}␉␉␉␉␉␊ |
508 | ␉␉␉␉␉␉}␊ |
509 | ␉␉␉␉␉}␊ |
510 | ␉␉␉␉␉␊ |
511 | ␉␉␉␉␉break;␊ |
512 | ␉␉␉␉}␉␉␉␉␊ |
513 | ␉␉␉␉case LC_DYSYMTAB:␊ |
514 | ␉␉␉␉␉break;␊ |
515 | ␉␉␉␉␉␊ |
516 | ␉␉␉␉case LC_LOAD_DYLIB:␊ |
517 | ␉␉␉␉case LC_LOAD_WEAK_DYLIB ^ LC_REQ_DYLD:␊ |
518 | ␉␉␉␉{␊ |
519 | ␉␉␉␉␉struct dylib_command* dylibCommand = binary + binaryIndex;␊ |
520 | ␉␉␉␉␉char* weak_module = binary + binaryIndex + ((UInt32)*((UInt32*)&dylibCommand->dylib.name));␊ |
521 | ␉␉␉␉␉␉␉␉␉␉␊ |
522 | ␉␉␉␉␉char *name=NULL;␊ |
523 | ␉␉␉␉␉name = newStringWithFormat( "%s.dylib", weak_module);␊ |
524 | ␉␉␉␉␉if (!name) {␊ |
525 | ␉␉␉␉␉␉printf("Unable to allocate module name : %s\n", weak_module);␊ |
526 | ␉␉␉␉␉␉return NULL;␊ |
527 | ␉␉␉␉␉}␊ |
528 | ␉␉␉␉␉if(dylib_loader)␊ |
529 | ␉␉␉␉␉{␊ |
530 | ␉␉␉␉␉␉EFI_STATUS statue = dylib_loader(weak_module);␊ |
531 | ␉␉␉␉␉␉␊ |
532 | ␉␉␉␉␉␉if( statue != EFI_SUCCESS)␊ |
533 | ␉␉␉␉␉␉{␊ |
534 | ␉␉␉␉␉␉␉if (statue != EFI_ALREADY_STARTED)␊ |
535 | ␉␉␉␉␉␉␉{␊ |
536 | ␉␉␉␉␉␉␉␉// Unable to load dependancy␊ |
537 | ␉␉␉␉␉␉␉␉//free(weak_module);␊ |
538 | ␉␉␉␉␉␉␉␉return NULL;␊ |
539 | ␉␉␉␉␉␉␉}␊ |
540 | ␉␉␉␉␉␉␉␊ |
541 | ␉␉␉␉␉␉} ␊ |
542 | ␉␉␉␉␉␉␊ |
543 | ␉␉␉␉␉}␊ |
544 | ␉␉␉␉␉//free(weak_module);␊ |
545 | ␉␉␉␉␉␊ |
546 | ␉␉␉␉␉break;␊ |
547 | ␉␉␉␉}␉␉␉␉␊ |
548 | ␉␉␉␉case LC_ID_DYLIB:␊ |
549 | {␊ |
550 | /*struct dylib_command* dylibCommand = binary + binaryIndex;␊ |
551 | ␉␉␉␉␉ moduleName =␉binary + binaryIndex + ((UInt32)*((UInt32*)&dylibCommand->dylib.name));␊ |
552 | ␉␉␉␉␉ moduleVersion =␉dylibCommand->dylib.current_version;␊ |
553 | ␉␉␉␉␉ moduleCompat =␉dylibCommand->dylib.compatibility_version;␊ |
554 | ␉␉␉␉␉ */␊ |
555 | }␊ |
556 | ␉␉␉␉␉break;␊ |
557 | ␉␉␉␉␉␊ |
558 | ␉␉␉␉case LC_DYLD_INFO:␊ |
559 | ␉␉␉␉␉// Bind and rebase info is stored here␊ |
560 | ␉␉␉␉␉dyldInfoCommand = binary + binaryIndex;␊ |
561 | ␉␉␉␉␉break;␊ |
562 | ␉␉␉␉␉␊ |
563 | ␉␉␉␉case LC_UUID:␊ |
564 | ␉␉␉␉␉break;␊ |
565 | ␉␉␉␉␉␊ |
566 | ␉␉␉␉case LC_UNIXTHREAD:␊ |
567 | ␉␉␉␉␉break;␊ |
568 | ␉␉␉␉␉␊ |
569 | ␉␉␉␉default:␊ |
570 | ␉␉␉␉␉DBG("Unhandled loadcommand 0x%X\n", loadCommand->cmd & 0x7FFFFFFF);␊ |
571 | ␉␉␉␉␉break;␊ |
572 | ␉␉␉␉␉␊ |
573 | ␉␉␉}␊ |
574 | ␉␉␉␊ |
575 | ␉␉␉binaryIndex += cmdSize;␊ |
576 | ␉␉}␊ |
577 | ␉␉//if(!moduleName) return NULL;␊ |
578 | ␉}␉␉␊ |
579 | ␉␊ |
580 | ␉// bind_macho uses the symbols.␊ |
581 | ␉module_start = (void*)handle_symtable(module, (UInt32)binary, symtabCommand, symbol_handler, is64);␊ |
582 | ␊ |
583 | ␉// Rebase the module before binding it.␊ |
584 | ␉if(dyldInfoCommand && dyldInfoCommand->rebase_off)␊ |
585 | ␉{␊ |
586 | ␉␉rebase_macho(binary, (char*)dyldInfoCommand->rebase_off, dyldInfoCommand->rebase_size);␊ |
587 | ␉}␊ |
588 | ␉␊ |
589 | ␉if(dyldInfoCommand && dyldInfoCommand->bind_off)␊ |
590 | ␉{␊ |
591 | ␉␉bind_status = bind_macho(module, binary, (char*)dyldInfoCommand->bind_off, dyldInfoCommand->bind_size);␊ |
592 | ␉}␊ |
593 | ␉␊ |
594 | ␉if(dyldInfoCommand && dyldInfoCommand->weak_bind_off && (bind_status == EFI_SUCCESS))␊ |
595 | ␉{␊ |
596 | ␉␉// NOTE: this currently should never happen.␊ |
597 | ␉␉bind_status = bind_macho(module, binary, (char*)dyldInfoCommand->weak_bind_off, dyldInfoCommand->weak_bind_size);␊ |
598 | ␉}␊ |
599 | ␉␊ |
600 | ␉if(dyldInfoCommand && dyldInfoCommand->lazy_bind_off && (bind_status == EFI_SUCCESS))␊ |
601 | ␉{␊ |
602 | ␉␉// NOTE: we are binding the lazy pointers as a module is laoded,␊ |
603 | ␉␉// This should be changed to bind when a symbol is referened at runtime instead.␊ |
604 | ␉␉bind_status = bind_macho(module, binary, (char*)dyldInfoCommand->lazy_bind_off, dyldInfoCommand->lazy_bind_size);␊ |
605 | ␉}␊ |
606 | ␉␊ |
607 | if (bind_status != EFI_SUCCESS) {␊ |
608 | module_start = (void*)0xFFFFFFFF;␊ |
609 | }␊ |
610 | ␊ |
611 | ␉return module_start;␊ |
612 | ␉␊ |
613 | }␊ |
614 | ␊ |
615 | // Based on code from dylibinfo.cpp and ImageLoaderMachOCompressed.cpp␊ |
616 | void rebase_macho(void* base, char* rebase_stream, UInt32 size)␊ |
617 | {␊ |
618 | ␉rebase_stream += (UInt32)base;␊ |
619 | ␉␊ |
620 | ␉UInt8 immediate = 0;␊ |
621 | ␉UInt8 opcode = 0;␊ |
622 | ␉UInt8 type = 0;␊ |
623 | ␉␊ |
624 | ␉UInt32 segmentAddress = 0;␊ |
625 | ␉␊ |
626 | ␉␊ |
627 | ␉␊ |
628 | ␉UInt32 tmp = 0;␊ |
629 | ␉UInt32 tmp2 = 0;␊ |
630 | ␉UInt8 bits = 0;␊ |
631 | ␉UInt32 index = 0;␊ |
632 | ␉␊ |
633 | ␉//int done = 0;␊ |
634 | ␉unsigned int i = 0;␊ |
635 | ␉␊ |
636 | ␉while(/*!done &&*/ i < size)␊ |
637 | ␉{␊ |
638 | ␉␉immediate = rebase_stream[i] & REBASE_IMMEDIATE_MASK;␊ |
639 | ␉␉opcode = rebase_stream[i] & REBASE_OPCODE_MASK;␊ |
640 | ␉␉␊ |
641 | ␉␉␊ |
642 | ␉␉switch(opcode)␊ |
643 | ␉␉{␊ |
644 | ␉␉␉case REBASE_OPCODE_DONE:␊ |
645 | ␉␉␉␉// Rebase complete.␊ |
646 | ␉␉␉␉//done = 1;␊ |
647 | ␉␉␉␉break;␊ |
648 | ␉␉␉␉␊ |
649 | ␉␉␉␉␊ |
650 | ␉␉␉case REBASE_OPCODE_SET_TYPE_IMM:␊ |
651 | ␉␉␉␉// Set rebase type (pointer, absolute32, pcrel32)␊ |
652 | ␉␉␉␉//DBG("Rebase type = 0x%X\n", immediate);␊ |
653 | ␉␉␉␉type = immediate;␊ |
654 | ␉␉␉␉break;␊ |
655 | ␉␉␉␉␊ |
656 | ␉␉␉␉␊ |
657 | ␉␉␉case REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB:␊ |
658 | ␉␉␉{␊ |
659 | ␉␉␉␉// Locate address to begin rebasing␊ |
660 | ␉␉␉␉//segmentAddress = 0;␊ |
661 | ␉␉␉␉␊ |
662 | ␉␉␉␉struct segment_command* segCommand = NULL; // NOTE: 32bit only␊ |
663 | ␉␉␉␉␊ |
664 | ␉␉␉␉{␊ |
665 | ␉␉␉␉␉unsigned int binIndex = 0;␊ |
666 | ␉␉␉␉␉index = 0;␊ |
667 | ␉␉␉␉␉do␊ |
668 | ␉␉␉␉␉{␊ |
669 | ␉␉␉␉␉␉segCommand = base + sizeof(struct mach_header) + binIndex;␊ |
670 | ␉␉␉␉␉␉␊ |
671 | ␉␉␉␉␉␉␊ |
672 | ␉␉␉␉␉␉binIndex += segCommand->cmdsize;␊ |
673 | ␉␉␉␉␉␉index++;␊ |
674 | ␉␉␉␉␉}␊ |
675 | ␉␉␉␉␉while(index <= immediate);␊ |
676 | ␉␉␉␉}␉␉␉␉␊ |
677 | ␉␉␉␉␊ |
678 | ␉␉␉␉segmentAddress = segCommand->fileoff;␊ |
679 | ␉␉␉␉␊ |
680 | ␉␉␉␉tmp = 0;␊ |
681 | ␉␉␉␉bits = 0;␊ |
682 | ␉␉␉␉do␊ |
683 | ␉␉␉␉{␊ |
684 | ␉␉␉␉␉tmp |= (rebase_stream[++i] & 0x7f) << bits;␊ |
685 | ␉␉␉␉␉bits += 7;␊ |
686 | ␉␉␉␉}␊ |
687 | ␉␉␉␉while(rebase_stream[i] & 0x80);␊ |
688 | ␉␉␉␉␊ |
689 | ␉␉␉␉segmentAddress += tmp;␊ |
690 | ␉␉␉␉break;␊ |
691 | ␉␉␉}␉␉␉␉␊ |
692 | ␉␉␉case REBASE_OPCODE_ADD_ADDR_ULEB:␊ |
693 | ␉␉␉␉// Add value to rebase address␊ |
694 | ␉␉␉␉tmp = 0;␊ |
695 | ␉␉␉␉bits = 0;␊ |
696 | ␉␉␉␉do␊ |
697 | ␉␉␉␉{␊ |
698 | ␉␉␉␉␉tmp <<= bits;␊ |
699 | ␉␉␉␉␉tmp |= rebase_stream[++i] & 0x7f;␊ |
700 | ␉␉␉␉␉bits += 7;␊ |
701 | ␉␉␉␉}␊ |
702 | ␉␉␉␉while(rebase_stream[i] & 0x80);␊ |
703 | ␉␉␉␉␊ |
704 | ␉␉␉␉segmentAddress +=␉tmp; ␊ |
705 | ␉␉␉␉break;␊ |
706 | ␉␉␉␉␊ |
707 | ␉␉␉case REBASE_OPCODE_ADD_ADDR_IMM_SCALED:␊ |
708 | ␉␉␉␉segmentAddress += immediate * sizeof(void*);␊ |
709 | ␉␉␉␉break;␊ |
710 | ␉␉␉␉␊ |
711 | ␉␉␉␉␊ |
712 | ␉␉␉case REBASE_OPCODE_DO_REBASE_IMM_TIMES:␊ |
713 | ␉␉␉␉for (index = 0; index < immediate; ++index)␊ |
714 | ␉␉␉␉{␊ |
715 | ␉␉␉␉␉rebase_location(base + segmentAddress, (char*)base, type);␊ |
716 | ␉␉␉␉␉segmentAddress += sizeof(void*);␊ |
717 | ␉␉␉␉}␊ |
718 | ␉␉␉␉break;␉␉␉␉␊ |
719 | ␉␉␉␉␊ |
720 | ␉␉␉case REBASE_OPCODE_DO_REBASE_ULEB_TIMES:␊ |
721 | ␉␉␉␉tmp = 0;␊ |
722 | ␉␉␉␉bits = 0;␊ |
723 | ␉␉␉␉do␊ |
724 | ␉␉␉␉{␊ |
725 | ␉␉␉␉␉tmp |= (rebase_stream[++i] & 0x7f) << bits;␊ |
726 | ␉␉␉␉␉bits += 7;␊ |
727 | ␉␉␉␉}␊ |
728 | ␉␉␉␉while(rebase_stream[i] & 0x80);␊ |
729 | ␉␉␉␉␊ |
730 | ␉␉␉␉for (index = 0; index < tmp; ++index)␊ |
731 | ␉␉␉␉{␊ |
732 | ␉␉␉␉␉//DBG("\tRebasing 0x%X\n", segmentAddress);␊ |
733 | ␉␉␉␉␉rebase_location(base + segmentAddress, (char*)base, type);␉␉␉␉␉␊ |
734 | ␉␉␉␉␉segmentAddress += sizeof(void*);␊ |
735 | ␉␉␉␉}␊ |
736 | ␉␉␉␉break;␊ |
737 | ␉␉␉␉␊ |
738 | ␉␉␉case REBASE_OPCODE_DO_REBASE_ADD_ADDR_ULEB:␊ |
739 | ␉␉␉␉tmp = 0;␊ |
740 | ␉␉␉␉bits = 0;␊ |
741 | ␉␉␉␉do␊ |
742 | ␉␉␉␉{␊ |
743 | ␉␉␉␉␉tmp |= (rebase_stream[++i] & 0x7f) << bits;␊ |
744 | ␉␉␉␉␉bits += 7;␊ |
745 | ␉␉␉␉}␊ |
746 | ␉␉␉␉while(rebase_stream[i] & 0x80);␊ |
747 | ␉␉␉␉␊ |
748 | ␉␉␉␉rebase_location(base + segmentAddress, (char*)base, type);␊ |
749 | ␉␉␉␉␊ |
750 | ␉␉␉␉segmentAddress += tmp + sizeof(void*);␊ |
751 | ␉␉␉␉break;␊ |
752 | ␉␉␉␉␊ |
753 | ␉␉␉case REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_ULEB:␊ |
754 | ␉␉␉␉tmp = 0;␊ |
755 | ␉␉␉␉bits = 0;␊ |
756 | ␉␉␉␉do␊ |
757 | ␉␉␉␉{␊ |
758 | ␉␉␉␉␉tmp |= (rebase_stream[++i] & 0x7f) << bits;␊ |
759 | ␉␉␉␉␉bits += 7;␊ |
760 | ␉␉␉␉}␊ |
761 | ␉␉␉␉while(rebase_stream[i] & 0x80);␊ |
762 | ␉␉␉␉␊ |
763 | ␉␉␉␉␊ |
764 | ␉␉␉␉tmp2 = 0;␊ |
765 | ␉␉␉␉bits = 0;␊ |
766 | ␉␉␉␉do␊ |
767 | ␉␉␉␉{␊ |
768 | ␉␉␉␉␉tmp2 |= (rebase_stream[++i] & 0x7f) << bits;␊ |
769 | ␉␉␉␉␉bits += 7;␊ |
770 | ␉␉␉␉}␊ |
771 | ␉␉␉␉while(rebase_stream[i] & 0x80);␊ |
772 | ␉␉␉␉␊ |
773 | ␉␉␉␉for (index = 0; index < tmp; ++index)␊ |
774 | ␉␉␉␉{␊ |
775 | ␉␉␉␉␉␊ |
776 | ␉␉␉␉␉rebase_location(base + segmentAddress, (char*)base, type);␊ |
777 | ␉␉␉␉␉␊ |
778 | ␉␉␉␉␉segmentAddress += tmp2 + sizeof(void*);␊ |
779 | ␉␉␉␉}␊ |
780 | ␉␉␉␉break;␊ |
781 | ␉␉␉default:␊ |
782 | ␉␉␉␉break;␊ |
783 | ␉␉}␊ |
784 | ␉␉i++;␊ |
785 | ␉}␊ |
786 | }␊ |
787 | ␊ |
788 | // Based on code from dylibinfo.cpp and ImageLoaderMachOCompressed.cpp␊ |
789 | // NOTE: this uses 32bit values, and not 64bit values. ␊ |
790 | // There is apossibility that this could cause issues,␊ |
791 | // however the macho file is 32 bit, so it shouldn't matter too much␊ |
792 | EFI_STATUS bind_macho(char* module, void* base, char* bind_stream, UInt32 size)␊ |
793 | {␉␊ |
794 | ␉bind_stream += (UInt32)base;␊ |
795 | ␉␊ |
796 | ␉UInt8 immediate = 0;␊ |
797 | ␉UInt8 opcode = 0;␊ |
798 | ␉␊ |
799 | ␉UInt32 segmentAddress = 0;␊ |
800 | ␉␊ |
801 | ␉UInt32 address = 0;␊ |
802 | ␉␊ |
803 | ␉SInt32 addend = 0;␉␉␉// TODO: handle this␊ |
804 | ␉␊ |
805 | ␉const char* symbolName = NULL;␊ |
806 | ␉UInt32 symbolAddr = 0xFFFFFFFF;␊ |
807 | ␉␊ |
808 | ␉// Temperary variables␊ |
809 | ␉UInt8 bits = 0;␊ |
810 | ␉UInt32 tmp = 0;␊ |
811 | ␉UInt32 tmp2 = 0;␊ |
812 | ␉␊ |
813 | ␉UInt32 index = 0;␊ |
814 | ␉unsigned int i = 0;␊ |
815 | ␉␊ |
816 | ␉while(i < size)␊ |
817 | ␉{␊ |
818 | ␉␉immediate = bind_stream[i] & BIND_IMMEDIATE_MASK;␊ |
819 | ␉␉opcode = bind_stream[i] & BIND_OPCODE_MASK;␊ |
820 | ␉␉␊ |
821 | ␉␉␊ |
822 | ␉␉switch(opcode)␊ |
823 | ␉␉{␊ |
824 | ␉␉␉case BIND_OPCODE_DONE:␊ |
825 | ␉␉␉␉break;␊ |
826 | ␉␉␉␉␊ |
827 | ␉␉␉case BIND_OPCODE_SET_DYLIB_ORDINAL_IMM:␊ |
828 | {␊ |
829 | #if DEBUG_MODULES==2␊ |
830 | ␉␉␉␉SInt32 libraryOrdinal = immediate;␊ |
831 | ␉␉␉␉DBG("BIND_OPCODE_SET_DYLIB_ORDINAL_IMM: %d\n", libraryOrdinal);␊ |
832 | #endif␊ |
833 | ␉␉␉␉break;␊ |
834 | }␊ |
835 | ␉␉␉case BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB:␊ |
836 | {␊ |
837 | #if DEBUG_MODULES==2␊ |
838 | ␉␉␉␉SInt32 libraryOrdinal = 0;␊ |
839 | ␉␉␉␉UInt8 bits = 0;␊ |
840 | ␉␉␉␉do␊ |
841 | ␉␉␉␉{␊ |
842 | ␉␉␉␉␉libraryOrdinal |= (bind_stream[++i] & 0x7f) << bits;␊ |
843 | ␉␉␉␉␉bits += 7;␊ |
844 | ␉␉␉␉}␊ |
845 | ␉␉␉␉while(bind_stream[i] & 0x80);␊ |
846 | ␉␉␉␉␊ |
847 | ␉␉␉␉DBG("BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB: %d\n", libraryOrdinal);␊ |
848 | #else␊ |
849 | do␊ |
850 | ␉␉␉␉{␊ |
851 | ␉␉␉␉␉i++;␊ |
852 | ␉␉␉␉}␊ |
853 | ␉␉␉␉while(bind_stream[i] & 0x80);␊ |
854 | #endif␊ |
855 | ␉␉␉␉break;␊ |
856 | ␉␉␉}␉␊ |
857 | ␉␉␉case BIND_OPCODE_SET_DYLIB_SPECIAL_IMM:␊ |
858 | {␊ |
859 | #if DEBUG_MODULES==2␊ |
860 | // NOTE: this is wrong, fortunately we don't use it␊ |
861 | ␉␉␉␉SInt32 libraryOrdinal = -immediate;␊ |
862 | ␉␉␉␉DBG("BIND_OPCODE_SET_DYLIB_SPECIAL_IMM: %d\n", libraryOrdinal);␊ |
863 | #endif␉␉␊ |
864 | ␉␉␉␉break;␊ |
865 | ␉␉␉}␉␊ |
866 | ␉␉␉case BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM:␊ |
867 | {␊ |
868 | ␉␉␉␉symbolName = (char*)&bind_stream[++i];␊ |
869 | ␉␉␉␉i += strlen((char*)&bind_stream[i]);␊ |
870 | #if DEBUG_MODULES==2␊ |
871 | ␉␉␉␉UInt8 symbolFlags = immediate;␊ |
872 | DBG("BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM: %s, 0x%X\n", symbolName, symbolFlags);␊ |
873 | #endif␉␉␉␉␊ |
874 | ␉␉␉␉symbolAddr = lookup_all_symbols(NULL ,symbolName);␊ |
875 | ␉␉␉␉␊ |
876 | ␉␉␉␉break;␊ |
877 | }␊ |
878 | ␉␉␉case BIND_OPCODE_SET_TYPE_IMM:␊ |
879 | {␊ |
880 | ␉␉␉␉// Set bind type (pointer, absolute32, pcrel32)␊ |
881 | #if DEBUG_MODULES==2␊ |
882 | UInt8 type = immediate;␊ |
883 | ␉␉␉␉DBG("BIND_OPCODE_SET_TYPE_IMM: %d\n", type);␊ |
884 | #endif␊ |
885 | ␉␉␉␉break;␊ |
886 | }␊ |
887 | ␉␉␉case BIND_OPCODE_SET_ADDEND_SLEB:␊ |
888 | ␉␉␉␉addend = 0;␊ |
889 | ␉␉␉␉bits = 0;␊ |
890 | ␉␉␉␉do␊ |
891 | ␉␉␉␉{␊ |
892 | ␉␉␉␉␉addend |= (bind_stream[++i] & 0x7f) << bits;␊ |
893 | ␉␉␉␉␉bits += 7;␊ |
894 | ␉␉␉␉}␊ |
895 | ␉␉␉␉while(bind_stream[i] & 0x80);␊ |
896 | ␉␉␉␉␊ |
897 | ␉␉␉␉if(!(bind_stream[i-1] & 0x40)) addend *= -1;␊ |
898 | #if DEBUG_MODULES==2␊ |
899 | ␉␉␉␉DBG("BIND_OPCODE_SET_ADDEND_SLEB: %d\n", addend);␊ |
900 | #endif␊ |
901 | ␊ |
902 | ␉␉␉␉break;␊ |
903 | ␉␉␉␉␊ |
904 | ␉␉␉case BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB:␊ |
905 | ␉␉␉{␉␉␉␉␊ |
906 | ␉␉␉␉// Locate address␊ |
907 | ␉␉␉␉struct segment_command* segCommand = NULL;␉// NOTE: 32bit only␊ |
908 | ␉␉␉␉␊ |
909 | ␉␉␉␉{␊ |
910 | ␉␉␉␉␉unsigned int binIndex = 0;␊ |
911 | ␉␉␉␉␉index = 0;␊ |
912 | ␉␉␉␉␉do␊ |
913 | ␉␉␉␉␉{␊ |
914 | ␉␉␉␉␉␉segCommand = base + sizeof(struct mach_header) + binIndex;␊ |
915 | ␉␉␉␉␉␉binIndex += segCommand->cmdsize;␊ |
916 | ␉␉␉␉␉␉index++;␊ |
917 | ␉␉␉␉␉}while(index <= immediate);␊ |
918 | ␉␉␉␉}␊ |
919 | ␉␉␉␉␊ |
920 | ␉␉␉␉␊ |
921 | ␉␉␉␉segmentAddress = segCommand->fileoff;␊ |
922 | ␉␉␉␉␊ |
923 | ␉␉␉␉// Read in offset␊ |
924 | ␉␉␉␉tmp = 0;␊ |
925 | ␉␉␉␉bits = 0;␊ |
926 | ␉␉␉␉do␊ |
927 | ␉␉␉␉{␊ |
928 | ␉␉␉␉␉tmp |= (bind_stream[++i] & 0x7f) << bits;␊ |
929 | ␉␉␉␉␉bits += 7;␊ |
930 | ␉␉␉␉}while(bind_stream[i] & 0x80);␊ |
931 | ␉␉␉␉␊ |
932 | ␉␉␉␉segmentAddress += tmp;␊ |
933 | #if DEBUG_MODULES==2␊ |
934 | ␉␉␉␉DBG("BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB: 0x%X\n", segmentAddress);␊ |
935 | #endif␊ |
936 | ␉␉␉␉break;␊ |
937 | ␉␉␉}␉␉␉␉␊ |
938 | ␉␉␉case BIND_OPCODE_ADD_ADDR_ULEB:␊ |
939 | ␉␉␉␉// Read in offset␊ |
940 | ␉␉␉␉tmp = 0;␊ |
941 | ␉␉␉␉bits = 0;␊ |
942 | ␉␉␉␉do␊ |
943 | ␉␉␉␉{␊ |
944 | ␉␉␉␉␉tmp |= (bind_stream[++i] & 0x7f) << bits;␊ |
945 | ␉␉␉␉␉bits += 7;␊ |
946 | ␉␉␉␉}␊ |
947 | ␉␉␉␉while(bind_stream[i] & 0x80);␊ |
948 | ␉␉␉␉␊ |
949 | ␉␉␉␉segmentAddress += tmp;␊ |
950 | #if DEBUG_MODULES==2␊ |
951 | ␉␉␉␉DBG("BIND_OPCODE_ADD_ADDR_ULEB: 0x%X\n", segmentAddress);␊ |
952 | #endif␊ |
953 | ␊ |
954 | ␉␉␉␉break;␊ |
955 | ␉␉␉␉␊ |
956 | ␉␉␉case BIND_OPCODE_DO_BIND:␊ |
957 | #if DEBUG_MODULES==2␊ |
958 | ␉␉␉␉DBG("BIND_OPCODE_DO_BIND\n");␊ |
959 | #endif ␊ |
960 | ␊ |
961 | if(symbolAddr != 0xFFFFFFFF)␊ |
962 | ␉␉␉␉{␊ |
963 | ␉␉␉␉␉address = segmentAddress + (UInt32)base;␊ |
964 | ␉␉␉␉␉␊ |
965 | ␉␉␉␉␉bind_location((UInt32*)address, (char*)symbolAddr, addend, BIND_TYPE_POINTER);␊ |
966 | ␉␉␉␉}␊ |
967 | ␉␉␉␉else if(symbolName && (strcmp(symbolName, SYMBOL_DYLD_STUB_BINDER) != 0))␊ |
968 | ␉␉␉␉{␊ |
969 | ␉␉␉␉␉printf("Unable to bind symbol %s needed by %s\n", symbolName, module);␊ |
970 | getc();␊ |
971 | goto error;␊ |
972 | ␊ |
973 | ␉␉␉␉}␊ |
974 | ␉␉␉␉␊ |
975 | ␉␉␉␉segmentAddress += sizeof(void*);␊ |
976 | ␉␉␉␉break;␊ |
977 | ␉␉␉␉␊ |
978 | ␉␉␉case BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB:␊ |
979 | #if DEBUG_MODULES==2␊ |
980 | ␉␉␉␉DBG("BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB\n");␊ |
981 | #endif␉␉␊ |
982 | ␉␉␉␉␊ |
983 | ␉␉␉␉// Read in offset␊ |
984 | ␉␉␉␉tmp = 0;␊ |
985 | ␉␉␉␉bits = 0;␊ |
986 | ␉␉␉␉do␊ |
987 | ␉␉␉␉{␊ |
988 | ␉␉␉␉␉tmp |= (bind_stream[++i] & 0x7f) << bits;␊ |
989 | ␉␉␉␉␉bits += 7;␊ |
990 | ␉␉␉␉}␊ |
991 | ␉␉␉␉while(bind_stream[i] & 0x80);␊ |
992 | ␉␉␉␉␊ |
993 | ␉␉␉␉ ␊ |
994 | if(symbolAddr != 0xFFFFFFFF)␊ |
995 | ␉␉␉␉{␊ |
996 | ␉␉␉␉␉address = segmentAddress + (UInt32)base;␊ |
997 | ␉␉␉␉␉␊ |
998 | ␉␉␉␉␉bind_location((UInt32*)address, (char*)symbolAddr, addend, BIND_TYPE_POINTER);␊ |
999 | ␉␉␉␉}␊ |
1000 | ␉␉␉␉else if(symbolName && (strcmp(symbolName, SYMBOL_DYLD_STUB_BINDER) != 0))␊ |
1001 | ␉␉␉␉{␊ |
1002 | ␉␉␉␉␉printf("Unable to bind symbol %s needed by %s\n", symbolName, module);␊ |
1003 | getc();␊ |
1004 | goto error; ␊ |
1005 | ␊ |
1006 | ␉␉␉␉}␊ |
1007 | ␊ |
1008 | ␉␉␉␉segmentAddress += tmp + sizeof(void*);␊ |
1009 | ␉␉␉␉␊ |
1010 | ␉␉␉␉␊ |
1011 | ␉␉␉␉break;␊ |
1012 | ␉␉␉␉␊ |
1013 | ␉␉␉case BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED:␊ |
1014 | #if DEBUG_MODULES==2␊ |
1015 | ␉␉␉␉DBG("BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED\n");␊ |
1016 | #endif␉␉␉␉␉␉␊ |
1017 | ␊ |
1018 | if(symbolAddr != 0xFFFFFFFF)␊ |
1019 | ␉␉␉␉{␊ |
1020 | ␉␉␉␉␉address = segmentAddress + (UInt32)base;␊ |
1021 | ␉␉␉␉␉␊ |
1022 | ␉␉␉␉␉bind_location((UInt32*)address, (char*)symbolAddr, addend, BIND_TYPE_POINTER);␊ |
1023 | ␉␉␉␉}␊ |
1024 | ␉␉␉␉else if(symbolName && (strcmp(symbolName, SYMBOL_DYLD_STUB_BINDER) != 0))␊ |
1025 | ␉␉␉␉{␊ |
1026 | ␉␉␉␉␉printf("Unable to bind symbol %s needed by %s\n", symbolName, module);␊ |
1027 | getc();␊ |
1028 | goto error; ␊ |
1029 | ␊ |
1030 | ␉␉␉␉}␊ |
1031 | ␉␉␉␉segmentAddress += (immediate * sizeof(void*)) + sizeof(void*);␉␉␉␉␊ |
1032 | ␉␉␉␉␊ |
1033 | ␉␉␉␉break;␊ |
1034 | ␉␉␉␉␊ |
1035 | ␉␉␉case BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB:␊ |
1036 | ␉␉␉␉␊ |
1037 | ␉␉␉␉tmp = 0;␊ |
1038 | ␉␉␉␉bits = 0;␊ |
1039 | ␉␉␉␉do␊ |
1040 | ␉␉␉␉{␊ |
1041 | ␉␉␉␉␉tmp |= (bind_stream[++i] & 0x7f) << bits;␊ |
1042 | ␉␉␉␉␉bits += 7;␊ |
1043 | ␉␉␉␉}␊ |
1044 | ␉␉␉␉while(bind_stream[i] & 0x80);␊ |
1045 | ␉␉␉␉␊ |
1046 | ␉␉␉␉␊ |
1047 | ␉␉␉␉tmp2 = 0;␊ |
1048 | ␉␉␉␉bits = 0;␊ |
1049 | ␉␉␉␉do␊ |
1050 | ␉␉␉␉{␊ |
1051 | ␉␉␉␉␉tmp2 |= (bind_stream[++i] & 0x7f) << bits;␊ |
1052 | ␉␉␉␉␉bits += 7;␊ |
1053 | ␉␉␉␉}␊ |
1054 | ␉␉␉␉while(bind_stream[i] & 0x80);␊ |
1055 | ␉␉␉␉␊ |
1056 | #if DEBUG_MODULES==2␊ |
1057 | ␉␉␉␉DBG("BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB 0x%X 0x%X\n", tmp, tmp2);␊ |
1058 | #endif␉␉␉␉␊ |
1059 | ␊ |
1060 | if(symbolAddr != 0xFFFFFFFF)␊ |
1061 | ␉␉␉␉{␊ |
1062 | ␉␉␉␉␉for(index = 0; index < tmp; index++)␊ |
1063 | ␉␉␉␉␉{␊ |
1064 | ␉␉␉␉␉␉␊ |
1065 | ␉␉␉␉␉␉address = segmentAddress + (UInt32)base;␊ |
1066 | ␉␉␉␉␉␉␊ |
1067 | ␉␉␉␉␉␉bind_location((UInt32*)address, (char*)symbolAddr, addend, BIND_TYPE_POINTER);␊ |
1068 | ␉␉␉␉␉␉␊ |
1069 | ␉␉␉␉␉␉segmentAddress += tmp2 + sizeof(void*);␊ |
1070 | ␉␉␉␉␉}␊ |
1071 | ␉␉␉␉}␊ |
1072 | ␉␉␉␉else if(symbolName && (strcmp(symbolName, SYMBOL_DYLD_STUB_BINDER) != 0))␊ |
1073 | ␉␉␉␉{␊ |
1074 | ␉␉␉␉␉printf("Unable to bind symbol %s needed by %s\n", symbolName, module);␊ |
1075 | getc();␊ |
1076 | goto error; ␊ |
1077 | ␊ |
1078 | ␉␉␉␉}␉␉␉␉␊ |
1079 | ␉␉␉␉␊ |
1080 | ␉␉␉␉break;␊ |
1081 | ␉␉␉default:␊ |
1082 | ␉␉␉␉break;␊ |
1083 | ␉␉␉␉␊ |
1084 | ␉␉}␊ |
1085 | ␉␉i++;␊ |
1086 | ␉}␊ |
1087 | return EFI_SUCCESS;␊ |
1088 | error:␊ |
1089 | return EFI_NOT_FOUND;␊ |
1090 | }␊ |
1091 | ␊ |
1092 | void rebase_location(UInt32* location, char* base, int type)␊ |
1093 | {␉␊ |
1094 | ␉switch(type)␊ |
1095 | ␉{␊ |
1096 | ␉␉case REBASE_TYPE_POINTER:␊ |
1097 | ␉␉case REBASE_TYPE_TEXT_ABSOLUTE32:␊ |
1098 | ␉␉␉*location += (UInt32)base;␊ |
1099 | ␉␉␉break;␊ |
1100 | ␉␉␉␊ |
1101 | ␉␉default:␊ |
1102 | ␉␉␉break;␊ |
1103 | ␉}␊ |
1104 | }␊ |
1105 | ␊ |
1106 | void bind_location(UInt32* location, char* value, UInt32 addend, int type)␊ |
1107 | {␉␊ |
1108 | ␉// do actual update␊ |
1109 | ␉char* newValue = value + addend;␊ |
1110 | ␉␊ |
1111 | ␉switch (type) {␊ |
1112 | ␉␉case BIND_TYPE_POINTER:␊ |
1113 | ␉␉case BIND_TYPE_TEXT_ABSOLUTE32:␊ |
1114 | ␉␉␉break;␊ |
1115 | ␉␉␉␊ |
1116 | ␉␉case BIND_TYPE_TEXT_PCREL32:␊ |
1117 | ␉␉␉newValue -= ((UInt32)location + 4);␊ |
1118 | ␉␉␉␊ |
1119 | ␉␉␉break;␊ |
1120 | ␉␉default:␊ |
1121 | ␉␉␉return;␊ |
1122 | ␉}␊ |
1123 | ␉*location = (UInt32)newValue;␊ |
1124 | ␉␊ |
1125 | ␉␊ |
1126 | }␊ |
1127 | ␊ |
1128 | /*␊ |
1129 | * add_symbol␊ |
1130 | * This function adds a symbol from a module to the list of known symbols ␊ |
1131 | * possibly change to a pointer and add this to the Symbol module so that it can␊ |
1132 | * adjust it's internal symbol list (sort) to optimize locating new symbols␊ |
1133 | * NOTE: returns the address if the symbol is "start", else returns 0xFFFFFFFF␊ |
1134 | */␊ |
1135 | long long add_symbol(char* module,char* symbol, long long addr, char is64)␊ |
1136 | {␊ |
1137 | ␉if(is64) return 0xFFFFFFFF; // Fixme␊ |
1138 | ␉␊ |
1139 | ␉// This only can handle 32bit symbols ␊ |
1140 | ␉symbolList_t* new_entry= malloc(sizeof(symbolList_t));␊ |
1141 | ␉DBG("Adding symbol %s at 0x%X\n", symbol, addr);␊ |
1142 | ␉if (new_entry)␊ |
1143 | ␉{␉␊ |
1144 | ␉␉new_entry->next = moduleSymbols;␊ |
1145 | ␉␉␊ |
1146 | ␉␉moduleSymbols = new_entry;␊ |
1147 | ␉␉␊ |
1148 | ␉␉new_entry->addr = (UInt32)addr;␊ |
1149 | new_entry->module = module;␊ |
1150 | ␉␉new_entry->symbol = symbol;␊ |
1151 | ␉␉return addr;␊ |
1152 | ␉}␉␊ |
1153 | ␉␊ |
1154 | ␉return 0xFFFFFFFF;␊ |
1155 | ␉␊ |
1156 | }␊ |
1157 | ␊ |
1158 | /*␊ |
1159 | * print out the information about the loaded module␊ |
1160 | */␊ |
1161 | VOID module_loaded(const char* name)␊ |
1162 | {␊ |
1163 | ␉moduleList_t* new_entry = malloc(sizeof(moduleList_t));␊ |
1164 | ␉if (new_entry)␊ |
1165 | ␉{␉␊ |
1166 | ␉␉new_entry->next = loadedModules;␊ |
1167 | ␉␉␊ |
1168 | ␉␉loadedModules = new_entry;␊ |
1169 | ␉␉␊ |
1170 | ␉␉new_entry->module = (char*)name;␉␉␊ |
1171 | ␉}␊ |
1172 | }␊ |
1173 | ␊ |
1174 | EFI_STATUS is_module_loaded(const char* name)␊ |
1175 | {␊ |
1176 | ␉moduleList_t* entry = loadedModules;␊ |
1177 | ␉while(entry)␊ |
1178 | ␉{␊ |
1179 | ␉␉DBG("Comparing %s with %s\n", name, entry->module);␊ |
1180 | ␉␉char *fullname = newStringWithFormat("%s.dylib",name);␊ |
1181 | ␉␉if(fullname && ((strcmp(entry->module, name) == 0) || (strcmp(entry->module, fullname) == 0)))␊ |
1182 | ␉␉{␊ |
1183 | ␉␉␉free(fullname);␊ |
1184 | ␉␉␉DBG("Located module %s\n", name);␊ |
1185 | ␉␉␉return EFI_SUCCESS;␊ |
1186 | ␉␉}␊ |
1187 | ␉␉else␊ |
1188 | ␉␉{␊ |
1189 | ␉␉␉entry = entry->next;␊ |
1190 | ␉␉}␊ |
1191 | ␉␉␊ |
1192 | ␉}␊ |
1193 | ␉DBG("Module %s not found\n", name);␊ |
1194 | ␉␊ |
1195 | ␉return EFI_NOT_FOUND;␊ |
1196 | }␊ |
1197 | ␊ |
1198 | // Look for symbols using the Smbols moduel function.␊ |
1199 | // If non are found, look through the list of module symbols␊ |
1200 | unsigned int lookup_all_symbols(const char* module, const char* name)␊ |
1201 | {␊ |
1202 | ␊ |
1203 | unsigned int addr = 0xFFFFFFFF;␊ |
1204 | ␊ |
1205 | do {␊ |
1206 | ␊ |
1207 | if ((module != NULL) && (strcmp(module,SYMBOLS_MODULE) != 0))␊ |
1208 | break; ␊ |
1209 | ␊ |
1210 | if(lookup_symbol && (UInt32)lookup_symbol != 0xFFFFFFFF)␊ |
1211 | {␊ |
1212 | addr = lookup_symbol(name, &strcmp);␊ |
1213 | if(addr != 0xFFFFFFFF)␊ |
1214 | {␊ |
1215 | DBG("Internal symbol %s located at 0x%X\n", name, addr);␊ |
1216 | goto out;␊ |
1217 | }␊ |
1218 | } ␊ |
1219 | ␊ |
1220 | } while (0);␊ |
1221 | ␊ |
1222 | ␉␉␊ |
1223 | ␉{␊ |
1224 | ␉␉symbolList_t* entry = moduleSymbols;␊ |
1225 | ␉␉while(entry)␊ |
1226 | ␉␉{␊ |
1227 | if ((module != NULL) && (strcmp(entry->module,module) != 0))␊ |
1228 | {␊ |
1229 | entry = entry->next;␊ |
1230 | continue; ␊ |
1231 | }␊ |
1232 | ␊ |
1233 | if(strcmp(entry->symbol, name) == 0)␊ |
1234 | {␊ |
1235 | DBG("External symbol %s located at 0x%X\n", name, entry->addr);␊ |
1236 | addr = entry->addr;␊ |
1237 | goto out;␊ |
1238 | }␊ |
1239 | else␊ |
1240 | {␊ |
1241 | entry = entry->next;␊ |
1242 | } ␊ |
1243 | ␉␉␉␊ |
1244 | ␉␉}␊ |
1245 | ␉}␊ |
1246 | ␉␊ |
1247 | #if DEBUG_MODULES␊ |
1248 | ␉if(strcmp(name, SYMBOL_DYLD_STUB_BINDER) != 0)␊ |
1249 | ␉{␊ |
1250 | ␉␉verbose("Unable to locate symbol %s\n", name);␊ |
1251 | ␉␉getc();␊ |
1252 | ␉}␊ |
1253 | #endif␊ |
1254 | out:␊ |
1255 | return addr;␊ |
1256 | ␊ |
1257 | }␊ |
1258 | ␊ |
1259 | ␊ |
1260 | /*␊ |
1261 | * parse the symbol table␊ |
1262 | * Lookup any undefined symbols␊ |
1263 | */␊ |
1264 | ␊ |
1265 | unsigned int handle_symtable(char *module, UInt32 base, struct symtab_command* symtabCommand, long long(*symbol_handler)(char*, char*, long long, char), char is64)␊ |
1266 | {␉␉␊ |
1267 | ␉unsigned int module_start = 0xFFFFFFFF;␊ |
1268 | ␉␊ |
1269 | ␉UInt32 symbolIndex = 0;␊ |
1270 | if (!symtabCommand) {␊ |
1271 | return 0xFFFFFFFF;␊ |
1272 | }␊ |
1273 | ␉char* symbolString = base + (char*)symtabCommand->stroff;␊ |
1274 | ␉//char* symbolTable = base + symtabCommand->symoff;␊ |
1275 | ␉if(!is64)␊ |
1276 | ␉{␊ |
1277 | ␉␉struct nlist* symbolEntry = (void*)base + symtabCommand->symoff;␊ |
1278 | ␉␉while(symbolIndex < symtabCommand->nsyms)␊ |
1279 | ␉␉{␉␉␉␉␉␉␊ |
1280 | ␉␉␉if(symbolEntry->n_value)␊ |
1281 | ␉␉␉{␉␉␉␉␊ |
1282 | ␉␉␉␉if(strstr(symbolString + symbolEntry->n_un.n_strx, "module_start") || (strcmp(symbolString + symbolEntry->n_un.n_strx, "start") == 0))␊ |
1283 | ␉␉␉␉{␊ |
1284 | ␉␉␉␉␉module_start = base + symbolEntry->n_value;␊ |
1285 | ␉␉␉␉␉DBG("n_value %x module_start %x\n", (unsigned)symbolEntry->n_value, (unsigned)module_start);␊ |
1286 | ␉␉␉␉}␊ |
1287 | ␉␉␉␉else␊ |
1288 | ␉␉␉␉{␊ |
1289 | ␉␉␉␉␉symbol_handler(module, symbolString + symbolEntry->n_un.n_strx, (long long)base + symbolEntry->n_value, is64);␊ |
1290 | ␉␉␉␉}␊ |
1291 | #if DEBUG_MODULES␊ |
1292 | ␉␉␉␉bool isTexT = (((unsigned)symbolEntry->n_value > (unsigned)vmaddr) && ((unsigned)(vmaddr + vmsize) > (unsigned)symbolEntry->n_value ));␊ |
1293 | ␉␉␉␉printf("%s %s\n", isTexT ? "__TEXT :" : "__DATA(OR ANY) :", symbolString + symbolEntry->n_un.n_strx);␊ |
1294 | #if DEBUG_MODULES==2␉␊ |
1295 | ␉␉␉␉␊ |
1296 | ␉␉␉␉if(strcmp(symbolString + symbolEntry->n_un.n_strx, "_BootHelp_txt") == 0)␊ |
1297 | ␉␉␉␉{␊ |
1298 | ␉␉␉␉␉long long addr = (long long)base + symbolEntry->n_value;␊ |
1299 | ␉␉␉␉␉unsigned char *BootHelp = NULL;␊ |
1300 | ␉␉␉␉␉BootHelp = (unsigned char*)(UInt32)addr;␉␉␉␉␉␊ |
1301 | ␉␉␉␉␉printf("method 1: __DATA : BootHelp_txt[0] %x\n", BootHelp[0]);␊ |
1302 | ␉␉␉␉␉␊ |
1303 | ␉␉␉␉␉long long addr2 = symbolEntry->n_value;␊ |
1304 | ␉␉␉␉␉unsigned char *BootHelp2 = NULL;␊ |
1305 | ␉␉␉␉␉BootHelp2 = (unsigned char*)(UInt32)addr2;␉␉␉␉␉␊ |
1306 | ␉␉␉␉␉printf("method 2: __DATA : BootHelp_txt[0] %x\n", BootHelp2[0]);␊ |
1307 | ␉␉␉␉}␊ |
1308 | #endif␊ |
1309 | #endif␊ |
1310 | ␉␉␉␉␊ |
1311 | ␉␉␉}␊ |
1312 | ␉␉␉␊ |
1313 | ␉␉␉symbolEntry++;␊ |
1314 | ␉␉␉symbolIndex++;␉// TODO remove␊ |
1315 | ␉␉}␊ |
1316 | ␉}␊ |
1317 | ␉else␊ |
1318 | ␉{␊ |
1319 | ␉␉struct nlist_64* symbolEntry = (void*)base + symtabCommand->symoff;␊ |
1320 | ␉␉// NOTE First entry is *not* correct, but we can ignore it (i'm getting radar:// right now)␉␊ |
1321 | ␉␉while(symbolIndex < symtabCommand->nsyms)␊ |
1322 | ␉␉{␉␊ |
1323 | ␉␉␉␊ |
1324 | ␉␉␉if(strstr(symbolString + symbolEntry->n_un.n_strx, "module_start") || (strcmp(symbolString + symbolEntry->n_un.n_strx, "start") == 0))␊ |
1325 | ␉␉␉{␊ |
1326 | ␉␉␉␉module_start = (unsigned int)(base + symbolEntry->n_value);␊ |
1327 | ␉␉␉}␊ |
1328 | ␉␉␉else␊ |
1329 | ␉␉␉{␊ |
1330 | ␉␉␉␉symbol_handler(module, symbolString + symbolEntry->n_un.n_strx, (long long)base + symbolEntry->n_value, is64);␊ |
1331 | ␉␉␉}␊ |
1332 | ␉␉␉␊ |
1333 | ␉␉␉symbolEntry++;␊ |
1334 | ␉␉␉symbolIndex++;␉// TODO remove␊ |
1335 | ␉␉}␊ |
1336 | ␉}␊ |
1337 | ␉␊ |
1338 | ␉return module_start;␊ |
1339 | ␉␊ |
1340 | }␊ |
1341 | ␊ |
1342 | ␊ |
1343 | /*␊ |
1344 | * Locate the symbol for an already loaded function and modify the beginning of␊ |
1345 | * the function to jump directly to the new one␊ |
1346 | * example: replace_system_function("_getc", &replacement);␊ |
1347 | * replace_function(module_name,"_getc", &replacement);␊ |
1348 | * replace_function_any("_getc", &replacement);␊ |
1349 | */␊ |
1350 | EFI_STATUS replace_function(const char* module, const char* symbol, void* newAddress)␊ |
1351 | {␉␉ ␊ |
1352 | ␉// TODO: look into using the next four bytes of the function instead␊ |
1353 | ␉// Most functions should support this, as they probably will be at ␊ |
1354 | ␉// least 10 bytes long, but you never know, this is sligtly safer as␊ |
1355 | ␉// function can be as small as 6 bytes.␊ |
1356 | ␉UInt32 addr = lookup_all_symbols(module, symbol);␊ |
1357 | ␉␊ |
1358 | ␉char* binary = (char*)addr;␊ |
1359 | ␉if(addr != 0xFFFFFFFF)␊ |
1360 | ␉{␊ |
1361 | ␉␉UInt32* jumpPointer = malloc(sizeof(UInt32*));␊ |
1362 | ␉␉if (!jumpPointer) {␊ |
1363 | ␉␉␉return EFI_OUT_OF_RESOURCES;␊ |
1364 | ␉␉}␊ |
1365 | ␉␉␊ |
1366 | ␉␉*binary++ = 0xFF;␉// Jump␊ |
1367 | ␉␉*binary++ = 0x25;␉// Long Jump␊ |
1368 | ␉␉*((UInt32*)binary) = (UInt32)jumpPointer;␊ |
1369 | ␉␉␊ |
1370 | ␉␉*jumpPointer = (UInt32)newAddress;␊ |
1371 | ␉␉␊ |
1372 | ␉␉return EFI_SUCCESS;␊ |
1373 | ␉}␊ |
1374 | ␉␊ |
1375 | ␉return EFI_NOT_FOUND;␊ |
1376 | ␉␊ |
1377 | }␊ |
1378 | ␊ |
1379 | EFI_STATUS replace_system_function(const char* symbol, void* newAddress)␊ |
1380 | {␉␉ ␊ |
1381 | ␉␊ |
1382 | ␉return replace_function(SYMBOLS_MODULE,symbol,newAddress);␊ |
1383 | ␉␊ |
1384 | }␊ |
1385 | ␊ |
1386 | EFI_STATUS replace_function_any(const char* symbol, void* newAddress)␊ |
1387 | {␉␉ ␊ |
1388 | ␉␊ |
1389 | ␉return replace_function(NULL,symbol,newAddress);␊ |
1390 | ␉␊ |
1391 | }␊ |
1392 | |