1 | /*␊ |
2 | * Copyright 2010 Evan Lojewski. All rights reserved.␊ |
3 | *␊ |
4 | */␊ |
5 | ␊ |
6 | /*␊ |
7 | * Copyright 2012 Cadet-petit Armel <armelcadetpetit@gmail.com>. All rights reserved.␊ |
8 | *␊ |
9 | * Cleaned, Added (runtime) bundles module support, based on a dylib environement.␊ |
10 | *␊ |
11 | */␊ |
12 | #include "libsaio.h"␊ |
13 | #include "bootstruct.h"␊ |
14 | #include "modules.h"␊ |
15 | ␊ |
16 | #ifndef DEBUG_MODULES␊ |
17 | #define DEBUG_MODULES 0␊ |
18 | #endif␊ |
19 | ␊ |
20 | #if DEBUG_MODULES␊ |
21 | #define DBG(x...)␉printf(x)␊ |
22 | #else␊ |
23 | #define DBG(x...)␊ |
24 | #endif␊ |
25 | ␊ |
26 | // NOTE: Global so that modules can link with this␊ |
27 | unsigned long long textAddress = 0;␊ |
28 | unsigned long long textSection = 0;␊ |
29 | ␊ |
30 | moduleHook_t* moduleCallbacks = NULL;␊ |
31 | symbolList_t* moduleSymbols = NULL;␊ |
32 | unsigned int (*lookup_symbol)(const char*, int(*strcmp_callback)(const char*, const char*)) = NULL;␊ |
33 | moduleHook_t* get_callback(const char* name);␊ |
34 | static unsigned int load_module(char * name, char* module);␊ |
35 | ␊ |
36 | #if DEBUG_MODULES␊ |
37 | VOID print_hook_list()␊ |
38 | {␊ |
39 | ␉moduleHook_t* hooks = moduleCallbacks;␊ |
40 | ␉printf("Hook list: \n");␊ |
41 | ␉while(hooks)␊ |
42 | ␉{␊ |
43 | ␉␉printf("* %s\n", hooks->name);␊ |
44 | ␉␉hooks = hooks->next;␊ |
45 | ␉}␊ |
46 | ␉printf("\n");␊ |
47 | }␊ |
48 | ␊ |
49 | VOID print_symbol_list()␊ |
50 | {␊ |
51 | ␉symbolList_t* symbol = moduleSymbols;␊ |
52 | ␉printf("Symbol list: \n");␊ |
53 | ␉while(symbol)␊ |
54 | ␉{␊ |
55 | ␉␉printf("* %s : %s\n", symbol->module, symbol->symbol);␊ |
56 | ␉␉symbol = symbol->next;␊ |
57 | ␉}␊ |
58 | ␉printf("\n");␊ |
59 | }␊ |
60 | #endif␊ |
61 | ␊ |
62 | #if DYLIB_SUPPORT␊ |
63 | ␊ |
64 | typedef struct dylbList_t␊ |
65 | {␊ |
66 | ␉char* dylib_name;␊ |
67 | ␉struct dylbList_t* next;␊ |
68 | } dylbList_t;␊ |
69 | ␊ |
70 | dylbList_t* loadedDylib = NULL;␊ |
71 | ␊ |
72 | ␊ |
73 | static EFI_STATUS is_system_loaded(void)␊ |
74 | {␊ |
75 | ␉msglog("* Attempting to load system module\n");␊ |
76 | ␊ |
77 | ␊ |
78 | if((UInt32)lookup_symbol != 0xFFFFFFFF)␊ |
79 | {␊ |
80 | #if␉DEBUG_MODULES == 2␊ |
81 | printf("System successfully Loaded.\n");␊ |
82 | getc(); ␊ |
83 | #endif␊ |
84 | return EFI_SUCCESS;␊ |
85 | }␊ |
86 | ␊ |
87 | ␊ |
88 | #if␉DEBUG_MODULES == 2␊ |
89 | printf("Failed to load system module\n");␊ |
90 | getc(); ␊ |
91 | #endif␊ |
92 | ␉return EFI_LOAD_ERROR;␊ |
93 | }␊ |
94 | ␊ |
95 | /*␊ |
96 | * print out the information about the loaded module␊ |
97 | */␊ |
98 | static VOID add_dylib(const char* name)␊ |
99 | {␊ |
100 | ␉dylbList_t* new_entry = malloc(sizeof(dylbList_t));␊ |
101 | ␉if (new_entry)␊ |
102 | ␉{␉␊ |
103 | ␉␉new_entry->next = loadedDylib;␊ |
104 | ␉␉␊ |
105 | ␉␉loadedDylib = new_entry;␊ |
106 | ␉␉␊ |
107 | ␉␉new_entry->dylib_name = (char*)name;␉␉␊ |
108 | ␉}␊ |
109 | }␊ |
110 | ␊ |
111 | static EFI_STATUS is_dylib_loaded(const char* name)␊ |
112 | {␊ |
113 | ␉dylbList_t* entry = loadedDylib;␊ |
114 | ␉while(entry)␊ |
115 | ␉{␊ |
116 | ␉␉DBG("Comparing %s with %s\n", name, entry->dylib_name);␊ |
117 | ␉␉char *fullname = newStringWithFormat("%s.dylib",name);␊ |
118 | ␉␉if(fullname && ((strcmp(entry->dylib_name, name) == 0) || (strcmp(entry->dylib_name, fullname) == 0)))␊ |
119 | ␉␉{␊ |
120 | ␉␉␉free(fullname);␊ |
121 | ␉␉␉DBG("Located module %s\n", name);␊ |
122 | ␉␉␉return EFI_SUCCESS;␊ |
123 | ␉␉}␊ |
124 | ␉␉else␊ |
125 | ␉␉{␊ |
126 | ␉␉␉entry = entry->next;␊ |
127 | ␉␉}␊ |
128 | ␉␉␊ |
129 | ␉}␊ |
130 | ␉DBG("Module %s not found\n", name);␊ |
131 | ␉␊ |
132 | ␉return EFI_NOT_FOUND;␊ |
133 | }␊ |
134 | ␊ |
135 | /*␊ |
136 | * Load a dylib ␊ |
137 | *␊ |
138 | * ex: load_dylib("/Extra/modules/", "AcpiCodec.dylib");␊ |
139 | */␊ |
140 | VOID load_dylib(const char * dylib_path, const char * name)␊ |
141 | {␊ |
142 | void (*module_start)(void);␊ |
143 | ␊ |
144 | char *tmp = newStringWithFormat("%s%s",dylib_path, name);␉␉␉␉␉␉␉␉␊ |
145 | if (!tmp) {␊ |
146 | return;␊ |
147 | }␊ |
148 | char *dylib_name = newString(name);␉␉␉␉␉␉␉␉␊ |
149 | if (!dylib_name) {␊ |
150 | free(tmp);␊ |
151 | return;␊ |
152 | }␊ |
153 | msglog("* Attempting to load module: %s\n", tmp);␊ |
154 | module_start = (void*)load_module(dylib_name,tmp);␊ |
155 | ␊ |
156 | if(module_start && ( module_start != (void*)0xFFFFFFFF))␊ |
157 | {␊ |
158 | add_dylib(name);␊ |
159 | module_start();␊ |
160 | }␊ |
161 | else␊ |
162 | {␊ |
163 | // failed to load or already loaded␊ |
164 | free(tmp);␊ |
165 | free(dylib_name);␊ |
166 | ␊ |
167 | }␊ |
168 | }␊ |
169 | ␊ |
170 | /*␊ |
171 | * Load all dylib in the /Extra/modules/ directory␊ |
172 | */␊ |
173 | ␊ |
174 | VOID load_all_dylib(void)␊ |
175 | {␊ |
176 | ␉char* name;␊ |
177 | ␉long flags;␊ |
178 | ␉long time;␊ |
179 | ␊ |
180 | if (is_system_loaded() != EFI_SUCCESS) return;␊ |
181 | ␊ |
182 | ␉struct dirstuff* moduleDir = opendir("/Extra/modules/");␊ |
183 | void (*module_start)(void);␊ |
184 | ␉while(readdir(moduleDir, (const char**)&name, &flags, &time) >= 0)␊ |
185 | ␉{␊ |
186 | ␉␉if ((strcmp("Symbols.dylib",name)) == 0) continue; // if we found Symbols.dylib, just skip it␊ |
187 | ␉␉␊ |
188 | if (is_dylib_loaded(name) == EFI_SUCCESS) continue;␊ |
189 | ␉␉␊ |
190 | int len = strlen(name);␊ |
191 | ␉␉int ext_size = sizeof("dylib");␊ |
192 | ␉␉␊ |
193 | ␉␉if (len >= ext_size)␊ |
194 | ␉␉{␊ |
195 | ␉␉␉if(strcmp(&name[len - ext_size], ".dylib") == 0)␊ |
196 | ␉␉␉{␉␉␉␉␊ |
197 | ␉␉␉␉char *tmp = newStringWithFormat("/Extra/modules/%s",name);␉␉␉␉␉␉␉␉␊ |
198 | ␉␉␉␉if (!tmp) {␊ |
199 | ␉␉␉␉␉continue;␊ |
200 | ␉␉␉␉}␊ |
201 | char *dylib_name = newString(name);␉␉␉␉␉␉␉␉␊ |
202 | ␉␉␉␉if (!dylib_name) {␊ |
203 | free(tmp);␊ |
204 | ␉␉␉␉␉continue;␊ |
205 | ␉␉␉␉}␊ |
206 | ␉␉␉␉msglog("* Attempting to load module: %s\n", tmp);␊ |
207 | module_start = (void*)load_module(dylib_name,tmp);␊ |
208 | ␊ |
209 | ␉␉␉␉if(module_start && ( module_start != (void*)0xFFFFFFFF))␊ |
210 | ␉␉␉␉{␊ |
211 | add_dylib(name);␊ |
212 | ␉␉␉␉␉module_start();␊ |
213 | ␉␉␉␉}␊ |
214 | else␊ |
215 | {␊ |
216 | // failed to load or already loaded␊ |
217 | ␉␉␉␉␉free(tmp);␊ |
218 | free(dylib_name);␊ |
219 | ␊ |
220 | }␊ |
221 | ␉␉␉}␊ |
222 | #if DEBUG_MODULES␊ |
223 | ␉␉␉else ␊ |
224 | ␉␉␉{␊ |
225 | ␉␉␉␉DBG("Ignoring %s\n", name);␊ |
226 | ␉␉␉}␊ |
227 | #endif␊ |
228 | ␉␉}␉␉␊ |
229 | #if DEBUG_MODULES␊ |
230 | ␉␉else ␊ |
231 | ␉␉{␊ |
232 | ␉␉␉DBG("Ignoring %s\n", name);␊ |
233 | ␉␉}␊ |
234 | #endif␊ |
235 | ␉␉␊ |
236 | ␉}␊ |
237 | ␉␊ |
238 | ␉if (moduleDir)␊ |
239 | ␉{␊ |
240 | ␉␉closedir(moduleDir);␊ |
241 | ␉␉␊ |
242 | ␉}␊ |
243 | ␉␊ |
244 | #if DEBUG_MODULES␊ |
245 | ␉print_symbol_list();␊ |
246 | #endif␊ |
247 | }␊ |
248 | ␊ |
249 | #endif␊ |
250 | ␊ |
251 | /*␊ |
252 | * Load a module file ␊ |
253 | */␊ |
254 | static unsigned int load_module(char * name, char* module)␊ |
255 | {␊ |
256 | ␉unsigned int module_start = 0xFFFFFFFF;␊ |
257 | ␊ |
258 | ␉␊ |
259 | ␉int fh = -1;␊ |
260 | ␉␊ |
261 | ␉fh = open(module);␊ |
262 | ␉if(fh < 0)␊ |
263 | ␉{␉␉␊ |
264 | #if DEBUG_MODULES␊ |
265 | ␉␉DBG("Unable to locate module %s\n", module);␊ |
266 | ␉␉getc();␊ |
267 | #else␊ |
268 | ␉␉msglog("Unable to locate module %s\n", module);␊ |
269 | #endif␊ |
270 | ␉␉return 0xFFFFFFFF;␉␉␊ |
271 | ␉}␊ |
272 | ␉␊ |
273 | ␉{␊ |
274 | ␉␉int moduleSize = file_size(fh);␊ |
275 | ␉␉␊ |
276 | ␉␉char* module_base = NULL;␊ |
277 | ␉␉␊ |
278 | ␉␉if (moduleSize > 0)␊ |
279 | ␉␉{␊ |
280 | ␉␉␉module_base = (char*) malloc(moduleSize);␊ |
281 | ␉␉}␉␉␊ |
282 | ␉␉␊ |
283 | ␉␉if (module_base && read(fh, module_base, moduleSize) == moduleSize)␊ |
284 | ␉␉{␊ |
285 | ␉␉␉␊ |
286 | ␉␉␉DBG("Module %s read in.\n", module);␊ |
287 | ␉␉␉␊ |
288 | ␉␉␉// Module loaded into memory, parse it␊ |
289 | ␉␉␉module_start = parse_mach(name, module_base, &add_symbol);␉␉␉␊ |
290 | ␊ |
291 | ␉␉}␊ |
292 | ␉␉else␊ |
293 | ␉␉{␊ |
294 | ␉␉␉printf("Unable to read in module %s\n.", module);␊ |
295 | #if DEBUG_MODULES␉␉␊ |
296 | ␉␉␉getc();␊ |
297 | #endif␊ |
298 | ␉␉␉module_start = 0xFFFFFFFF;␊ |
299 | ␉␉}␊ |
300 | ␉}␊ |
301 | ␉close(fh);␊ |
302 | ␉return module_start;␊ |
303 | }␊ |
304 | ␊ |
305 | moduleHook_t* get_callback(const char* name)␊ |
306 | {␊ |
307 | ␉moduleHook_t* hooks = moduleCallbacks;␊ |
308 | ␉␊ |
309 | ␉// look for a hook. If it exists, return the moduleHook_t*,␊ |
310 | ␉// If not, return NULL.␊ |
311 | ␉while(hooks)␊ |
312 | ␉{␊ |
313 | ␉␉if(strcmp(name, hooks->name) == 0)␊ |
314 | ␉␉{␊ |
315 | ␉␉␉//DBG("Located hook %s\n", name);␊ |
316 | ␉␉␉return hooks;␊ |
317 | ␉␉}␊ |
318 | ␉␉hooks = hooks->next;␊ |
319 | ␉}␊ |
320 | ␉return NULL;␊ |
321 | ␉␊ |
322 | }␊ |
323 | ␊ |
324 | /*␊ |
325 | *␉execute_hook( const char* name )␊ |
326 | *␉␉name - Name of the module hook␊ |
327 | *␉␉␉If any callbacks have been registered for this hook␊ |
328 | *␉␉␉they will be executed now in the same order that the␊ |
329 | *␉␉␉hooks were added.␊ |
330 | */␊ |
331 | EFI_STATUS execute_hook(const char* name, void* arg1, void* arg2, void* arg3, void* arg4, void* arg5, void* arg6)␊ |
332 | {␊ |
333 | ␉DBG("Attempting to execute hook '%s'\n", name);␊ |
334 | ␉moduleHook_t* hook = get_callback(name);␊ |
335 | ␉␊ |
336 | ␉if(hook)␊ |
337 | ␉{␊ |
338 | ␉␉// Loop through all callbacks for this module␊ |
339 | ␉␉callbackList_t* callbacks = hook->callbacks;␊ |
340 | ␉␉␊ |
341 | ␉␉while(callbacks)␊ |
342 | ␉␉{␊ |
343 | ␉␉␉// Execute callback␊ |
344 | ␉␉␉callbacks->callback(arg1, arg2, arg3, arg4, arg5, arg6);␊ |
345 | ␉␉␉callbacks = callbacks->next;␊ |
346 | ␉␉}␊ |
347 | ␉␉DBG("Hook '%s' executed.\n", name); ␊ |
348 | ␉␉return EFI_SUCCESS;␊ |
349 | ␉}␊ |
350 | ␉␊ |
351 | ␉// Callback for this hook doesn't exist;␊ |
352 | ␉DBG("No callbacks for '%s' hook.\n", name);␊ |
353 | ␉return EFI_NOT_FOUND;␊ |
354 | ␉␊ |
355 | }␊ |
356 | ␊ |
357 | /*␊ |
358 | *␉register_hook_callback( const char* name, void(*callback)())␊ |
359 | *␉␉name - Name of the module hook to attach to.␊ |
360 | *␉␉callbacks - The funciton pointer that will be called when the␊ |
361 | *␉␉␉hook is executed. When registering a new callback name, the callback is added sorted.␊ |
362 | *␉␉␉NOTE: the hooks take four void* arguments.␊ |
363 | */␊ |
364 | VOID register_hook_callback(const char* name, void(*callback)(void*, void*, void*, void*, void*, void*))␊ |
365 | {␉␊ |
366 | ␉DBG("Adding callback for hook '%s'.\n", name);␊ |
367 | ␉␊ |
368 | ␉moduleHook_t* hook = get_callback(name);␊ |
369 | ␉␊ |
370 | ␉if(hook)␊ |
371 | ␉{␊ |
372 | ␉␉// append␊ |
373 | ␉␉callbackList_t* newCallback = malloc(sizeof(callbackList_t));␊ |
374 | ␉␉if (!newCallback) {␊ |
375 | ␉␉␉DBG("Unable to allocate memory for callback \n");␊ |
376 | ␉␉␉return;␊ |
377 | ␉␉}␊ |
378 | ␉␉newCallback->next = hook->callbacks;␊ |
379 | ␉␉hook->callbacks = newCallback;␊ |
380 | ␉␉newCallback->callback = callback;␊ |
381 | ␉}␊ |
382 | ␉else␊ |
383 | ␉{␊ |
384 | ␉␉// create new hook␊ |
385 | ␉␉moduleHook_t* newHook = malloc(sizeof(moduleHook_t));␊ |
386 | ␉␉if (!newHook) {␊ |
387 | ␉␉␉DBG("Unable to allocate memory for hook '%s'.\n", name);␊ |
388 | ␉␉␉return;␊ |
389 | ␉␉}␊ |
390 | ␉␉newHook->name = name;␊ |
391 | ␉␉newHook->callbacks = malloc(sizeof(callbackList_t));␊ |
392 | ␉␉if (!newHook->callbacks) {␊ |
393 | ␉␉␉DBG("Unable to allocate memory for callback \n");␊ |
394 | ␉␉␉free(newHook);␊ |
395 | ␉␉␉return;␊ |
396 | ␉␉}␊ |
397 | ␉␉newHook->callbacks->callback = callback;␊ |
398 | ␉␉newHook->callbacks->next = NULL;␊ |
399 | ␉␉␊ |
400 | ␉␉newHook->next = moduleCallbacks;␊ |
401 | ␉␉moduleCallbacks = newHook;␊ |
402 | ␉␉␊ |
403 | ␉}␊ |
404 | ␉␊ |
405 | #if DEBUG_MODULES␊ |
406 | ␉print_hook_list();␊ |
407 | ␉getc();␊ |
408 | #endif␊ |
409 | ␉␊ |
410 | }␊ |
411 | ␊ |
412 | #if DEBUG_MODULES␊ |
413 | unsigned long vmaddr;␊ |
414 | long vmsize;␊ |
415 | #endif␊ |
416 | ␊ |
417 | /*␊ |
418 | * Parse through a macho module. The module will be rebased and binded␊ |
419 | * as specified in the macho header. If the module is successfully loaded␊ |
420 | * the module iinit address will be returned.␊ |
421 | * NOTE; all dependecies will be loaded before this module is started␊ |
422 | * NOTE: If the module is unable to load ot completeion, the modules␊ |
423 | * symbols will still be available (TODO: fix this). This should not␊ |
424 | * happen as all dependencies are verified before the sybols are read in.␊ |
425 | */␊ |
426 | unsigned int parse_mach(char *module, void* binary, long long(*symbol_handler)(char*, char*, long long, char))␉// TODO: add param to specify valid archs␊ |
427 | {␉␊ |
428 | ␉char is64 = false;␊ |
429 | ␉unsigned int module_start = 0xFFFFFFFF;␊ |
430 | ␉EFI_STATUS bind_status = EFI_SUCCESS;␊ |
431 | ␊ |
432 | ␉// TODO convert all of the structs to a union␉␊ |
433 | ␉struct dyld_info_command* dyldInfoCommand = NULL;␉␊ |
434 | ␉struct symtab_command* symtabCommand = NULL;␉␊ |
435 | ␉␊ |
436 | ␉{␊ |
437 | ␉␉struct segment_command *segCommand = NULL;␊ |
438 | ␉␉struct segment_command_64 *segCommand64 = NULL;␊ |
439 | ␉␉struct load_command *loadCommand = NULL;␊ |
440 | ␉␉UInt32 binaryIndex = 0;␊ |
441 | ␉␉UInt16 cmd = 0;␊ |
442 | ␉␉␊ |
443 | ␉␉// Parse through the load commands␊ |
444 | ␉␉if(((struct mach_header*)binary)->magic == MH_MAGIC)␊ |
445 | ␉␉{␊ |
446 | ␉␉␉is64 = false;␊ |
447 | ␉␉␉binaryIndex += sizeof(struct mach_header);␊ |
448 | ␉␉}␊ |
449 | ␉␉else if(((struct mach_header_64*)binary)->magic == MH_MAGIC_64)␊ |
450 | ␉␉{␊ |
451 | ␉␉␉// NOTE: modules cannot be 64bit...␊ |
452 | ␉␉␉is64 = true;␊ |
453 | ␉␉␉binaryIndex += sizeof(struct mach_header_64);␊ |
454 | ␉␉}␊ |
455 | ␉␉else␊ |
456 | ␉␉{␊ |
457 | ␉␉␉printf("Modules: Invalid mach magic\n");␊ |
458 | ␉␉␉getc();␊ |
459 | ␉␉␉return 0xFFFFFFFF;␊ |
460 | ␉␉}␊ |
461 | ␉␉␊ |
462 | ␉␉␊ |
463 | ␉␉␊ |
464 | ␉␉/*if(((struct mach_header*)binary)->filetype != MH_DYLIB)␊ |
465 | ␉␉ {␊ |
466 | ␉␉ printf("Module is not a dylib. Unable to load.\n");␊ |
467 | ␉␉ getc();␊ |
468 | ␉␉ return NULL; // Module is in the incorrect format␊ |
469 | ␉␉ }*/␊ |
470 | ␉␉␊ |
471 | ␉␉while(cmd < ((struct mach_header*)binary)->ncmds)␊ |
472 | ␉␉{␊ |
473 | ␉␉␉cmd++;␊ |
474 | ␉␉␉␊ |
475 | ␉␉␉loadCommand = binary + binaryIndex;␊ |
476 | ␉␉␉UInt32 cmdSize = loadCommand->cmdsize;␊ |
477 | ␉␉␉␊ |
478 | ␉␉␉␊ |
479 | ␉␉␉switch ((loadCommand->cmd & 0x7FFFFFFF))␊ |
480 | ␉␉␉{␊ |
481 | ␉␉␉␉case LC_SYMTAB:␊ |
482 | ␉␉␉␉␉symtabCommand = binary + binaryIndex;␊ |
483 | ␉␉␉␉␉break;␊ |
484 | ␉␉␉␉␉␊ |
485 | ␉␉␉␉case LC_SEGMENT: // 32bit macho␊ |
486 | ␉␉␉␉{␊ |
487 | ␉␉␉␉␉segCommand = binary + binaryIndex;␊ |
488 | ␉␉␉␉␉␊ |
489 | ␉␉␉␉␉//printf("Segment name is %s\n", segCommand->segname);␊ |
490 | ␉␉␉␉␉␊ |
491 | ␉␉␉␉␉if(strcmp("__TEXT", segCommand->segname) == 0)␊ |
492 | ␉␉␉␉␉{␊ |
493 | ␉␉␉␉␉␉UInt32 sectionIndex;␊ |
494 | ␉␉␉␉␉␉␊ |
495 | #if DEBUG_MODULES␊ |
496 | ␉␉␉␉␉␉unsigned long fileaddr;␊ |
497 | ␉␉␉␉␉␉long filesize;␉␉␉␉␉␊ |
498 | ␉␉␉␉␉␉vmaddr = (segCommand->vmaddr & 0x3fffffff);␊ |
499 | ␉␉␉␉␉␉vmsize = segCommand->vmsize;␉ ␊ |
500 | ␉␉␉␉␉␉fileaddr = ((unsigned long)(binary + binaryIndex) + segCommand->fileoff);␊ |
501 | ␉␉␉␉␉␉filesize = segCommand->filesize;␊ |
502 | ␉␉␉␉␉␉␊ |
503 | ␉␉␉␉␉␉printf("segname: %s, vmaddr: %x, vmsize: %x, fileoff: %x, filesize: %x, nsects: %d, flags: %x.\n",␊ |
504 | ␉␉␉␉␉␉␉ segCommand->segname, (unsigned)vmaddr, (unsigned)vmsize, (unsigned)fileaddr, (unsigned)filesize,␊ |
505 | ␉␉␉␉␉␉␉ (unsigned) segCommand->nsects, (unsigned)segCommand->flags);␊ |
506 | #if DEBUG_MODULES==2␊ |
507 | ␉␉␉␉␉␉␊ |
508 | ␉␉␉␉␉␉getc();␊ |
509 | #endif␊ |
510 | #endif␊ |
511 | ␉␉␉␉␉␉␊ |
512 | ␉␉␉␉␉␉sectionIndex = sizeof(struct segment_command);␊ |
513 | ␉␉␉␉␉␉␊ |
514 | ␉␉␉␉␉␉struct section *sect;␊ |
515 | ␉␉␉␉␉␉␊ |
516 | ␉␉␉␉␉␉while(sectionIndex < segCommand->cmdsize)␊ |
517 | ␉␉␉␉␉␉{␊ |
518 | ␉␉␉␉␉␉␉sect = binary + binaryIndex + sectionIndex;␊ |
519 | ␉␉␉␉␉␉␉␊ |
520 | ␉␉␉␉␉␉␉sectionIndex += sizeof(struct section);␊ |
521 | ␉␉␉␉␉␉␉␊ |
522 | ␉␉␉␉␉␉␉␊ |
523 | ␉␉␉␉␉␉␉if(strcmp("__text", sect->sectname) == 0)␊ |
524 | ␉␉␉␉␉␉␉{␊ |
525 | ␉␉␉␉␉␉␉␉// __TEXT,__text found, save the offset and address for when looking for the calls.␊ |
526 | ␉␉␉␉␉␉␉␉textSection = sect->offset;␊ |
527 | ␉␉␉␉␉␉␉␉textAddress = sect->addr;␊ |
528 | ␉␉␉␉␉␉␉␉break;␊ |
529 | ␉␉␉␉␉␉␉}␉␉␉␉␉␊ |
530 | ␉␉␉␉␉␉}␊ |
531 | ␉␉␉␉␉}␊ |
532 | ␉␉␉␉␉break;␊ |
533 | ␉␉␉␉}␉␉␉␉␊ |
534 | ␉␉␉␉case LC_SEGMENT_64:␉// 64bit macho's␊ |
535 | ␉␉␉␉{␊ |
536 | ␉␉␉␉␉segCommand64 = binary + binaryIndex;␊ |
537 | ␉␉␉␉␉␊ |
538 | ␉␉␉␉␉//printf("Segment name is %s\n", segCommand->segname);␊ |
539 | ␉␉␉␉␉␊ |
540 | ␉␉␉␉␉if(strcmp("__TEXT", segCommand64->segname) == 0)␊ |
541 | ␉␉␉␉␉{␊ |
542 | ␉␉␉␉␉␉UInt32 sectionIndex;␊ |
543 | ␉␉␉␉␉␉␊ |
544 | #if DEBUG_MODULES␉␉␉␉␉␉␊ |
545 | ␉␉␉␉␉␉unsigned long fileaddr;␊ |
546 | ␉␉␉␉␉␉long filesize;␉␉␉␉␉␊ |
547 | ␉␉␉␉␉␉vmaddr = (segCommand64->vmaddr & 0x3fffffff);␊ |
548 | ␉␉␉␉␉␉vmsize = segCommand64->vmsize;␉ ␊ |
549 | ␉␉␉␉␉␉fileaddr = ((unsigned long)(binary + binaryIndex) + segCommand64->fileoff);␊ |
550 | ␉␉␉␉␉␉filesize = segCommand64->filesize;␊ |
551 | ␉␉␉␉␉␉␊ |
552 | ␉␉␉␉␉␉printf("segname: %s, vmaddr: %x, vmsize: %x, fileoff: %x, filesize: %x, nsects: %d, flags: %x.\n",␊ |
553 | ␉␉␉␉␉␉␉ segCommand64->segname, (unsigned)vmaddr, (unsigned)vmsize, (unsigned)fileaddr, (unsigned)filesize,␊ |
554 | ␉␉␉␉␉␉␉ (unsigned) segCommand64->nsects, (unsigned)segCommand64->flags);␊ |
555 | #if DEBUG_MODULES==2␊ |
556 | ␉␉␉␉␉␉␊ |
557 | ␉␉␉␉␉␉getc();␊ |
558 | #endif␊ |
559 | #endif␊ |
560 | ␉␉␉␉␉␉␊ |
561 | ␉␉␉␉␉␉sectionIndex = sizeof(struct segment_command_64);␊ |
562 | ␉␉␉␉␉␉␊ |
563 | ␉␉␉␉␉␉struct section_64 *sect;␊ |
564 | ␉␉␉␉␉␉␊ |
565 | ␉␉␉␉␉␉while(sectionIndex < segCommand64->cmdsize)␊ |
566 | ␉␉␉␉␉␉{␊ |
567 | ␉␉␉␉␉␉␉sect = binary + binaryIndex + sectionIndex;␊ |
568 | ␉␉␉␉␉␉␉␊ |
569 | ␉␉␉␉␉␉␉sectionIndex += sizeof(struct section_64);␊ |
570 | ␉␉␉␉␉␉␉␊ |
571 | ␉␉␉␉␉␉␉␊ |
572 | ␉␉␉␉␉␉␉if(strcmp("__text", sect->sectname) == 0)␊ |
573 | ␉␉␉␉␉␉␉{␊ |
574 | ␉␉␉␉␉␉␉␉// __TEXT,__text found, save the offset and address for when looking for the calls.␊ |
575 | ␉␉␉␉␉␉␉␉textSection = sect->offset;␊ |
576 | ␉␉␉␉␉␉␉␉textAddress = sect->addr;␊ |
577 | ␉␉␉␉␉␉␉␉␊ |
578 | ␉␉␉␉␉␉␉␉break;␊ |
579 | ␉␉␉␉␉␉␉}␉␉␉␉␉␊ |
580 | ␉␉␉␉␉␉}␊ |
581 | ␉␉␉␉␉}␊ |
582 | ␉␉␉␉␉␊ |
583 | ␉␉␉␉␉break;␊ |
584 | ␉␉␉␉}␉␉␉␉␊ |
585 | ␉␉␉␉case LC_DYSYMTAB:␊ |
586 | ␉␉␉␉␉break;␊ |
587 | ␉␉␉␉␉␊ |
588 | ␉␉␉␉case LC_LOAD_DYLIB:␊ |
589 | ␉␉␉␉case LC_LOAD_WEAK_DYLIB ^ LC_REQ_DYLD:␊ |
590 | break;␊ |
591 | ␊ |
592 | ␉␉␉␉case LC_ID_DYLIB:␊ |
593 | break;␊ |
594 | ␊ |
595 | ␉␉␉␉␉␊ |
596 | ␉␉␉␉case LC_DYLD_INFO:␊ |
597 | ␉␉␉␉␉// Bind and rebase info is stored here␊ |
598 | ␉␉␉␉␉dyldInfoCommand = binary + binaryIndex;␊ |
599 | ␉␉␉␉␉break;␊ |
600 | ␉␉␉␉␉␊ |
601 | ␉␉␉␉case LC_UUID:␊ |
602 | ␉␉␉␉␉break;␊ |
603 | ␉␉␉␉␉␊ |
604 | ␉␉␉␉case LC_UNIXTHREAD:␊ |
605 | ␉␉␉␉␉break;␊ |
606 | ␉␉␉␉␉␊ |
607 | ␉␉␉␉default:␊ |
608 | ␉␉␉␉␉DBG("Unhandled loadcommand 0x%X\n", loadCommand->cmd & 0x7FFFFFFF);␊ |
609 | ␉␉␉␉␉break;␊ |
610 | ␉␉␉␉␉␊ |
611 | ␉␉␉}␊ |
612 | ␉␉␉␊ |
613 | ␉␉␉binaryIndex += cmdSize;␊ |
614 | ␉␉}␊ |
615 | ␉␉//if(!moduleName) return NULL;␊ |
616 | ␉}␉␉␊ |
617 | ␉␊ |
618 | ␉// bind_macho uses the symbols.␊ |
619 | ␉module_start = handle_symtable(module, (UInt32)binary, symtabCommand, symbol_handler, is64);␊ |
620 | ␊ |
621 | ␉// Rebase the module before binding it.␊ |
622 | ␉if(dyldInfoCommand && dyldInfoCommand->rebase_off)␊ |
623 | ␉{␊ |
624 | ␉␉rebase_macho(binary, (char*)dyldInfoCommand->rebase_off, dyldInfoCommand->rebase_size);␊ |
625 | ␉}␊ |
626 | ␉␊ |
627 | ␉if(dyldInfoCommand && dyldInfoCommand->bind_off)␊ |
628 | ␉{␊ |
629 | ␉␉bind_status = bind_macho(module, binary, (char*)dyldInfoCommand->bind_off, dyldInfoCommand->bind_size);␊ |
630 | ␉}␊ |
631 | ␉␊ |
632 | ␉if(dyldInfoCommand && dyldInfoCommand->weak_bind_off && (bind_status == EFI_SUCCESS))␊ |
633 | ␉{␊ |
634 | ␉␉// NOTE: this currently should never happen.␊ |
635 | ␉␉bind_status = bind_macho(module, binary, (char*)dyldInfoCommand->weak_bind_off, dyldInfoCommand->weak_bind_size);␊ |
636 | ␉}␊ |
637 | ␉␊ |
638 | ␉if(dyldInfoCommand && dyldInfoCommand->lazy_bind_off && (bind_status == EFI_SUCCESS))␊ |
639 | ␉{␊ |
640 | ␉␉// NOTE: we are binding the lazy pointers as a module is laoded,␊ |
641 | ␉␉// This should be changed to bind when a symbol is referened at runtime instead.␊ |
642 | ␉␉bind_status = bind_macho(module, binary, (char*)dyldInfoCommand->lazy_bind_off, dyldInfoCommand->lazy_bind_size);␊ |
643 | ␉}␊ |
644 | ␉␊ |
645 | if (bind_status != EFI_SUCCESS) {␊ |
646 | module_start = 0xFFFFFFFF;␊ |
647 | }␊ |
648 | ␊ |
649 | ␉return module_start;␊ |
650 | ␉␊ |
651 | }␊ |
652 | ␊ |
653 | // Based on code from dylibinfo.cpp and ImageLoaderMachOCompressed.cpp␊ |
654 | void rebase_macho(void* base, char* rebase_stream, UInt32 size)␊ |
655 | {␊ |
656 | ␉rebase_stream += (UInt32)base;␊ |
657 | ␉␊ |
658 | ␉UInt8 immediate = 0;␊ |
659 | ␉UInt8 opcode = 0;␊ |
660 | ␉UInt8 type = 0;␊ |
661 | ␉␊ |
662 | ␉UInt32 segmentAddress = 0;␊ |
663 | ␉␊ |
664 | ␉␊ |
665 | ␉␊ |
666 | ␉UInt32 tmp = 0;␊ |
667 | ␉UInt32 tmp2 = 0;␊ |
668 | ␉UInt8 bits = 0;␊ |
669 | ␉UInt32 index = 0;␊ |
670 | ␉␊ |
671 | ␉//int done = 0;␊ |
672 | ␉unsigned int i = 0;␊ |
673 | ␉␊ |
674 | ␉while(/*!done &&*/ i < size)␊ |
675 | ␉{␊ |
676 | ␉␉immediate = rebase_stream[i] & REBASE_IMMEDIATE_MASK;␊ |
677 | ␉␉opcode = rebase_stream[i] & REBASE_OPCODE_MASK;␊ |
678 | ␉␉␊ |
679 | ␉␉␊ |
680 | ␉␉switch(opcode)␊ |
681 | ␉␉{␊ |
682 | ␉␉␉case REBASE_OPCODE_DONE:␊ |
683 | ␉␉␉␉// Rebase complete.␊ |
684 | ␉␉␉␉//done = 1;␊ |
685 | ␉␉␉␉break;␊ |
686 | ␉␉␉␉␊ |
687 | ␉␉␉␉␊ |
688 | ␉␉␉case REBASE_OPCODE_SET_TYPE_IMM:␊ |
689 | ␉␉␉␉// Set rebase type (pointer, absolute32, pcrel32)␊ |
690 | ␉␉␉␉//DBG("Rebase type = 0x%X\n", immediate);␊ |
691 | ␉␉␉␉type = immediate;␊ |
692 | ␉␉␉␉break;␊ |
693 | ␉␉␉␉␊ |
694 | ␉␉␉␉␊ |
695 | ␉␉␉case REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB:␊ |
696 | ␉␉␉{␊ |
697 | ␉␉␉␉// Locate address to begin rebasing␊ |
698 | ␉␉␉␉//segmentAddress = 0;␊ |
699 | ␉␉␉␉␊ |
700 | ␉␉␉␉struct segment_command* segCommand = NULL; // NOTE: 32bit only␊ |
701 | ␉␉␉␉␊ |
702 | ␉␉␉␉{␊ |
703 | ␉␉␉␉␉unsigned int binIndex = 0;␊ |
704 | ␉␉␉␉␉index = 0;␊ |
705 | ␉␉␉␉␉do␊ |
706 | ␉␉␉␉␉{␊ |
707 | ␉␉␉␉␉␉segCommand = base + sizeof(struct mach_header) + binIndex;␊ |
708 | ␉␉␉␉␉␉␊ |
709 | ␉␉␉␉␉␉␊ |
710 | ␉␉␉␉␉␉binIndex += segCommand->cmdsize;␊ |
711 | ␉␉␉␉␉␉index++;␊ |
712 | ␉␉␉␉␉}␊ |
713 | ␉␉␉␉␉while(index <= immediate);␊ |
714 | ␉␉␉␉}␉␉␉␉␊ |
715 | ␉␉␉␉␊ |
716 | ␉␉␉␉segmentAddress = segCommand->fileoff;␊ |
717 | ␉␉␉␉␊ |
718 | ␉␉␉␉tmp = 0;␊ |
719 | ␉␉␉␉bits = 0;␊ |
720 | ␉␉␉␉do␊ |
721 | ␉␉␉␉{␊ |
722 | ␉␉␉␉␉tmp |= (rebase_stream[++i] & 0x7f) << bits;␊ |
723 | ␉␉␉␉␉bits += 7;␊ |
724 | ␉␉␉␉}␊ |
725 | ␉␉␉␉while(rebase_stream[i] & 0x80);␊ |
726 | ␉␉␉␉␊ |
727 | ␉␉␉␉segmentAddress += tmp;␊ |
728 | ␉␉␉␉break;␊ |
729 | ␉␉␉}␉␉␉␉␊ |
730 | ␉␉␉case REBASE_OPCODE_ADD_ADDR_ULEB:␊ |
731 | ␉␉␉␉// Add value to rebase address␊ |
732 | ␉␉␉␉tmp = 0;␊ |
733 | ␉␉␉␉bits = 0;␊ |
734 | ␉␉␉␉do␊ |
735 | ␉␉␉␉{␊ |
736 | ␉␉␉␉␉tmp <<= bits;␊ |
737 | ␉␉␉␉␉tmp |= rebase_stream[++i] & 0x7f;␊ |
738 | ␉␉␉␉␉bits += 7;␊ |
739 | ␉␉␉␉}␊ |
740 | ␉␉␉␉while(rebase_stream[i] & 0x80);␊ |
741 | ␉␉␉␉␊ |
742 | ␉␉␉␉segmentAddress +=␉tmp; ␊ |
743 | ␉␉␉␉break;␊ |
744 | ␉␉␉␉␊ |
745 | ␉␉␉case REBASE_OPCODE_ADD_ADDR_IMM_SCALED:␊ |
746 | ␉␉␉␉segmentAddress += immediate * sizeof(void*);␊ |
747 | ␉␉␉␉break;␊ |
748 | ␉␉␉␉␊ |
749 | ␉␉␉␉␊ |
750 | ␉␉␉case REBASE_OPCODE_DO_REBASE_IMM_TIMES:␊ |
751 | ␉␉␉␉for (index = 0; index < immediate; ++index)␊ |
752 | ␉␉␉␉{␊ |
753 | ␉␉␉␉␉rebase_location(base + segmentAddress, (char*)base, type);␊ |
754 | ␉␉␉␉␉segmentAddress += sizeof(void*);␊ |
755 | ␉␉␉␉}␊ |
756 | ␉␉␉␉break;␉␉␉␉␊ |
757 | ␉␉␉␉␊ |
758 | ␉␉␉case REBASE_OPCODE_DO_REBASE_ULEB_TIMES:␊ |
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 | ␉␉␉␉for (index = 0; index < tmp; ++index)␊ |
769 | ␉␉␉␉{␊ |
770 | ␉␉␉␉␉//DBG("\tRebasing 0x%X\n", segmentAddress);␊ |
771 | ␉␉␉␉␉rebase_location(base + segmentAddress, (char*)base, type);␉␉␉␉␉␊ |
772 | ␉␉␉␉␉segmentAddress += sizeof(void*);␊ |
773 | ␉␉␉␉}␊ |
774 | ␉␉␉␉break;␊ |
775 | ␉␉␉␉␊ |
776 | ␉␉␉case REBASE_OPCODE_DO_REBASE_ADD_ADDR_ULEB:␊ |
777 | ␉␉␉␉tmp = 0;␊ |
778 | ␉␉␉␉bits = 0;␊ |
779 | ␉␉␉␉do␊ |
780 | ␉␉␉␉{␊ |
781 | ␉␉␉␉␉tmp |= (rebase_stream[++i] & 0x7f) << bits;␊ |
782 | ␉␉␉␉␉bits += 7;␊ |
783 | ␉␉␉␉}␊ |
784 | ␉␉␉␉while(rebase_stream[i] & 0x80);␊ |
785 | ␉␉␉␉␊ |
786 | ␉␉␉␉rebase_location(base + segmentAddress, (char*)base, type);␊ |
787 | ␉␉␉␉␊ |
788 | ␉␉␉␉segmentAddress += tmp + sizeof(void*);␊ |
789 | ␉␉␉␉break;␊ |
790 | ␉␉␉␉␊ |
791 | ␉␉␉case REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_ULEB:␊ |
792 | ␉␉␉␉tmp = 0;␊ |
793 | ␉␉␉␉bits = 0;␊ |
794 | ␉␉␉␉do␊ |
795 | ␉␉␉␉{␊ |
796 | ␉␉␉␉␉tmp |= (rebase_stream[++i] & 0x7f) << bits;␊ |
797 | ␉␉␉␉␉bits += 7;␊ |
798 | ␉␉␉␉}␊ |
799 | ␉␉␉␉while(rebase_stream[i] & 0x80);␊ |
800 | ␉␉␉␉␊ |
801 | ␉␉␉␉␊ |
802 | ␉␉␉␉tmp2 = 0;␊ |
803 | ␉␉␉␉bits = 0;␊ |
804 | ␉␉␉␉do␊ |
805 | ␉␉␉␉{␊ |
806 | ␉␉␉␉␉tmp2 |= (rebase_stream[++i] & 0x7f) << bits;␊ |
807 | ␉␉␉␉␉bits += 7;␊ |
808 | ␉␉␉␉}␊ |
809 | ␉␉␉␉while(rebase_stream[i] & 0x80);␊ |
810 | ␉␉␉␉␊ |
811 | ␉␉␉␉for (index = 0; index < tmp; ++index)␊ |
812 | ␉␉␉␉{␊ |
813 | ␉␉␉␉␉␊ |
814 | ␉␉␉␉␉rebase_location(base + segmentAddress, (char*)base, type);␊ |
815 | ␉␉␉␉␉␊ |
816 | ␉␉␉␉␉segmentAddress += tmp2 + sizeof(void*);␊ |
817 | ␉␉␉␉}␊ |
818 | ␉␉␉␉break;␊ |
819 | ␉␉␉default:␊ |
820 | ␉␉␉␉break;␊ |
821 | ␉␉}␊ |
822 | ␉␉i++;␊ |
823 | ␉}␊ |
824 | }␊ |
825 | ␊ |
826 | // Based on code from dylibinfo.cpp and ImageLoaderMachOCompressed.cpp␊ |
827 | // NOTE: this uses 32bit values, and not 64bit values. ␊ |
828 | // There is apossibility that this could cause issues,␊ |
829 | // however the macho file is 32 bit, so it shouldn't matter too much␊ |
830 | EFI_STATUS bind_macho(char* module, void* base, char* bind_stream, UInt32 size)␊ |
831 | {␉␊ |
832 | ␉bind_stream += (UInt32)base;␊ |
833 | ␉␊ |
834 | ␉UInt8 immediate = 0;␊ |
835 | ␉UInt8 opcode = 0;␊ |
836 | ␉␊ |
837 | ␉UInt32 segmentAddress = 0;␊ |
838 | ␉␊ |
839 | ␉UInt32 address = 0;␊ |
840 | ␉␊ |
841 | ␉SInt32 addend = 0;␉␉␉// TODO: handle this␊ |
842 | ␉␊ |
843 | ␉const char* symbolName = NULL;␊ |
844 | ␉UInt32 symbolAddr = 0xFFFFFFFF;␊ |
845 | ␉␊ |
846 | ␉// Temperary variables␊ |
847 | ␉UInt8 bits = 0;␊ |
848 | ␉UInt32 tmp = 0;␊ |
849 | ␉UInt32 tmp2 = 0;␊ |
850 | ␉␊ |
851 | ␉UInt32 index = 0;␊ |
852 | ␉unsigned int i = 0;␊ |
853 | ␉␊ |
854 | ␉while(i < size)␊ |
855 | ␉{␊ |
856 | ␉␉immediate = bind_stream[i] & BIND_IMMEDIATE_MASK;␊ |
857 | ␉␉opcode = bind_stream[i] & BIND_OPCODE_MASK;␊ |
858 | ␉␉␊ |
859 | ␉␉␊ |
860 | ␉␉switch(opcode)␊ |
861 | ␉␉{␊ |
862 | ␉␉␉case BIND_OPCODE_DONE:␊ |
863 | ␉␉␉␉break;␊ |
864 | ␉␉␉␉␊ |
865 | ␉␉␉case BIND_OPCODE_SET_DYLIB_ORDINAL_IMM:␊ |
866 | {␊ |
867 | #if DEBUG_MODULES==2␊ |
868 | ␉␉␉␉SInt32 libraryOrdinal = immediate;␊ |
869 | ␉␉␉␉DBG("BIND_OPCODE_SET_DYLIB_ORDINAL_IMM: %d\n", libraryOrdinal);␊ |
870 | #endif␊ |
871 | ␉␉␉␉break;␊ |
872 | }␊ |
873 | ␉␉␉case BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB:␊ |
874 | {␊ |
875 | #if DEBUG_MODULES==2␊ |
876 | ␉␉␉␉SInt32 libraryOrdinal = 0;␊ |
877 | ␉␉␉␉UInt8 bits = 0;␊ |
878 | ␉␉␉␉do␊ |
879 | ␉␉␉␉{␊ |
880 | ␉␉␉␉␉libraryOrdinal |= (bind_stream[++i] & 0x7f) << bits;␊ |
881 | ␉␉␉␉␉bits += 7;␊ |
882 | ␉␉␉␉}␊ |
883 | ␉␉␉␉while(bind_stream[i] & 0x80);␊ |
884 | ␉␉␉␉␊ |
885 | ␉␉␉␉DBG("BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB: %d\n", libraryOrdinal);␊ |
886 | #else␊ |
887 | do␊ |
888 | ␉␉␉␉{␊ |
889 | ␉␉␉␉␉i++;␊ |
890 | ␉␉␉␉}␊ |
891 | ␉␉␉␉while(bind_stream[i] & 0x80);␊ |
892 | #endif␊ |
893 | ␉␉␉␉break;␊ |
894 | ␉␉␉}␉␊ |
895 | ␉␉␉case BIND_OPCODE_SET_DYLIB_SPECIAL_IMM:␊ |
896 | {␊ |
897 | #if DEBUG_MODULES==2␊ |
898 | // NOTE: this is wrong, fortunately we don't use it␊ |
899 | ␉␉␉␉SInt32 libraryOrdinal = -immediate;␊ |
900 | ␉␉␉␉DBG("BIND_OPCODE_SET_DYLIB_SPECIAL_IMM: %d\n", libraryOrdinal);␊ |
901 | #endif␉␉␊ |
902 | ␉␉␉␉break;␊ |
903 | ␉␉␉}␉␊ |
904 | ␉␉␉case BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM:␊ |
905 | {␊ |
906 | ␉␉␉␉symbolName = (char*)&bind_stream[++i];␊ |
907 | ␉␉␉␉i += strlen((char*)&bind_stream[i]);␊ |
908 | #if DEBUG_MODULES==2␊ |
909 | ␉␉␉␉UInt8 symbolFlags = immediate;␊ |
910 | DBG("BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM: %s, 0x%X\n", symbolName, symbolFlags);␊ |
911 | #endif␉␉␉␉␊ |
912 | ␉␉␉␉symbolAddr = lookup_all_symbols(NULL ,symbolName);␊ |
913 | ␉␉␉␉␊ |
914 | ␉␉␉␉break;␊ |
915 | }␊ |
916 | ␉␉␉case BIND_OPCODE_SET_TYPE_IMM:␊ |
917 | {␊ |
918 | ␉␉␉␉// Set bind type (pointer, absolute32, pcrel32)␊ |
919 | #if DEBUG_MODULES==2␊ |
920 | UInt8 type = immediate;␊ |
921 | ␉␉␉␉DBG("BIND_OPCODE_SET_TYPE_IMM: %d\n", type);␊ |
922 | #endif␊ |
923 | ␉␉␉␉break;␊ |
924 | }␊ |
925 | ␉␉␉case BIND_OPCODE_SET_ADDEND_SLEB:␊ |
926 | ␉␉␉␉addend = 0;␊ |
927 | ␉␉␉␉bits = 0;␊ |
928 | ␉␉␉␉do␊ |
929 | ␉␉␉␉{␊ |
930 | ␉␉␉␉␉addend |= (bind_stream[++i] & 0x7f) << bits;␊ |
931 | ␉␉␉␉␉bits += 7;␊ |
932 | ␉␉␉␉}␊ |
933 | ␉␉␉␉while(bind_stream[i] & 0x80);␊ |
934 | ␉␉␉␉␊ |
935 | ␉␉␉␉if(!(bind_stream[i-1] & 0x40)) addend *= -1;␊ |
936 | #if DEBUG_MODULES==2␊ |
937 | ␉␉␉␉DBG("BIND_OPCODE_SET_ADDEND_SLEB: %d\n", addend);␊ |
938 | #endif␊ |
939 | ␊ |
940 | ␉␉␉␉break;␊ |
941 | ␉␉␉␉␊ |
942 | ␉␉␉case BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB:␊ |
943 | ␉␉␉{␉␉␉␉␊ |
944 | ␉␉␉␉// Locate address␊ |
945 | ␉␉␉␉struct segment_command* segCommand = NULL;␉// NOTE: 32bit only␊ |
946 | ␉␉␉␉␊ |
947 | ␉␉␉␉{␊ |
948 | ␉␉␉␉␉unsigned int binIndex = 0;␊ |
949 | ␉␉␉␉␉index = 0;␊ |
950 | ␉␉␉␉␉do␊ |
951 | ␉␉␉␉␉{␊ |
952 | ␉␉␉␉␉␉segCommand = base + sizeof(struct mach_header) + binIndex;␊ |
953 | ␉␉␉␉␉␉binIndex += segCommand->cmdsize;␊ |
954 | ␉␉␉␉␉␉index++;␊ |
955 | ␉␉␉␉␉}while(index <= immediate);␊ |
956 | ␉␉␉␉}␊ |
957 | ␉␉␉␉␊ |
958 | ␉␉␉␉␊ |
959 | ␉␉␉␉segmentAddress = segCommand->fileoff;␊ |
960 | ␉␉␉␉␊ |
961 | ␉␉␉␉// Read in offset␊ |
962 | ␉␉␉␉tmp = 0;␊ |
963 | ␉␉␉␉bits = 0;␊ |
964 | ␉␉␉␉do␊ |
965 | ␉␉␉␉{␊ |
966 | ␉␉␉␉␉tmp |= (bind_stream[++i] & 0x7f) << bits;␊ |
967 | ␉␉␉␉␉bits += 7;␊ |
968 | ␉␉␉␉}while(bind_stream[i] & 0x80);␊ |
969 | ␉␉␉␉␊ |
970 | ␉␉␉␉segmentAddress += tmp;␊ |
971 | #if DEBUG_MODULES==2␊ |
972 | ␉␉␉␉DBG("BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB: 0x%X\n", segmentAddress);␊ |
973 | #endif␊ |
974 | ␉␉␉␉break;␊ |
975 | ␉␉␉}␉␉␉␉␊ |
976 | ␉␉␉case BIND_OPCODE_ADD_ADDR_ULEB:␊ |
977 | ␉␉␉␉// Read in offset␊ |
978 | ␉␉␉␉tmp = 0;␊ |
979 | ␉␉␉␉bits = 0;␊ |
980 | ␉␉␉␉do␊ |
981 | ␉␉␉␉{␊ |
982 | ␉␉␉␉␉tmp |= (bind_stream[++i] & 0x7f) << bits;␊ |
983 | ␉␉␉␉␉bits += 7;␊ |
984 | ␉␉␉␉}␊ |
985 | ␉␉␉␉while(bind_stream[i] & 0x80);␊ |
986 | ␉␉␉␉␊ |
987 | ␉␉␉␉segmentAddress += tmp;␊ |
988 | #if DEBUG_MODULES==2␊ |
989 | ␉␉␉␉DBG("BIND_OPCODE_ADD_ADDR_ULEB: 0x%X\n", segmentAddress);␊ |
990 | #endif␊ |
991 | ␊ |
992 | ␉␉␉␉break;␊ |
993 | ␉␉␉␉␊ |
994 | ␉␉␉case BIND_OPCODE_DO_BIND:␊ |
995 | #if DEBUG_MODULES==2␊ |
996 | ␉␉␉␉DBG("BIND_OPCODE_DO_BIND\n");␊ |
997 | #endif ␊ |
998 | ␊ |
999 | if(symbolAddr != 0xFFFFFFFF)␊ |
1000 | ␉␉␉␉{␊ |
1001 | ␉␉␉␉␉address = segmentAddress + (UInt32)base;␊ |
1002 | ␉␉␉␉␉␊ |
1003 | ␉␉␉␉␉bind_location((UInt32*)address, (char*)symbolAddr, addend, BIND_TYPE_POINTER);␊ |
1004 | ␉␉␉␉}␊ |
1005 | ␉␉␉␉else if(symbolName && (strcmp(symbolName, SYMBOL_DYLD_STUB_BINDER) != 0))␊ |
1006 | ␉␉␉␉{␊ |
1007 | ␉␉␉␉␉printf("Unable to bind symbol %s needed by %s\n", symbolName, module);␊ |
1008 | getc();␊ |
1009 | goto error;␊ |
1010 | ␊ |
1011 | ␉␉␉␉}␊ |
1012 | ␉␉␉␉␊ |
1013 | ␉␉␉␉segmentAddress += sizeof(void*);␊ |
1014 | ␉␉␉␉break;␊ |
1015 | ␉␉␉␉␊ |
1016 | ␉␉␉case BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB:␊ |
1017 | #if DEBUG_MODULES==2␊ |
1018 | ␉␉␉␉DBG("BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB\n");␊ |
1019 | #endif␉␉␊ |
1020 | ␉␉␉␉␊ |
1021 | ␉␉␉␉// Read in offset␊ |
1022 | ␉␉␉␉tmp = 0;␊ |
1023 | ␉␉␉␉bits = 0;␊ |
1024 | ␉␉␉␉do␊ |
1025 | ␉␉␉␉{␊ |
1026 | ␉␉␉␉␉tmp |= (bind_stream[++i] & 0x7f) << bits;␊ |
1027 | ␉␉␉␉␉bits += 7;␊ |
1028 | ␉␉␉␉}␊ |
1029 | ␉␉␉␉while(bind_stream[i] & 0x80);␊ |
1030 | ␉␉␉␉␊ |
1031 | ␊ |
1032 | if(symbolAddr != 0xFFFFFFFF)␊ |
1033 | ␉␉␉␉{␊ |
1034 | ␉␉␉␉␉address = segmentAddress + (UInt32)base;␊ |
1035 | ␉␉␉␉␉␊ |
1036 | ␉␉␉␉␉bind_location((UInt32*)address, (char*)symbolAddr, addend, BIND_TYPE_POINTER);␊ |
1037 | ␉␉␉␉}␊ |
1038 | ␉␉␉␉else if(symbolName && (strcmp(symbolName, SYMBOL_DYLD_STUB_BINDER) != 0))␊ |
1039 | ␉␉␉␉{␊ |
1040 | ␉␉␉␉␉printf("Unable to bind symbol %s needed by %s\n", symbolName, module);␊ |
1041 | getc();␊ |
1042 | goto error; ␊ |
1043 | ␊ |
1044 | ␉␉␉␉}␊ |
1045 | ␊ |
1046 | ␉␉␉␉segmentAddress += tmp + sizeof(void*);␊ |
1047 | ␉␉␉␉␊ |
1048 | ␉␉␉␉␊ |
1049 | ␉␉␉␉break;␊ |
1050 | ␉␉␉␉␊ |
1051 | ␉␉␉case BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED:␊ |
1052 | #if DEBUG_MODULES==2␊ |
1053 | ␉␉␉␉DBG("BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED\n");␊ |
1054 | #endif␉␉␉␉␉␉␊ |
1055 | ␊ |
1056 | if(symbolAddr != 0xFFFFFFFF)␊ |
1057 | ␉␉␉␉{␊ |
1058 | ␉␉␉␉␉address = segmentAddress + (UInt32)base;␊ |
1059 | ␉␉␉␉␉␊ |
1060 | ␉␉␉␉␉bind_location((UInt32*)address, (char*)symbolAddr, addend, BIND_TYPE_POINTER);␊ |
1061 | ␉␉␉␉}␊ |
1062 | ␉␉␉␉else if(symbolName && (strcmp(symbolName, SYMBOL_DYLD_STUB_BINDER) != 0))␊ |
1063 | ␉␉␉␉{␊ |
1064 | ␉␉␉␉␉printf("Unable to bind symbol %s needed by %s\n", symbolName, module);␊ |
1065 | getc();␊ |
1066 | goto error; ␊ |
1067 | ␊ |
1068 | ␉␉␉␉}␊ |
1069 | ␉␉␉␉segmentAddress += (immediate * sizeof(void*)) + sizeof(void*);␉␉␉␉␊ |
1070 | ␉␉␉␉␊ |
1071 | ␉␉␉␉break;␊ |
1072 | ␉␉␉␉␊ |
1073 | ␉␉␉case BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB:␊ |
1074 | ␉␉␉␉␊ |
1075 | ␉␉␉␉tmp = 0;␊ |
1076 | ␉␉␉␉bits = 0;␊ |
1077 | ␉␉␉␉do␊ |
1078 | ␉␉␉␉{␊ |
1079 | ␉␉␉␉␉tmp |= (bind_stream[++i] & 0x7f) << bits;␊ |
1080 | ␉␉␉␉␉bits += 7;␊ |
1081 | ␉␉␉␉}␊ |
1082 | ␉␉␉␉while(bind_stream[i] & 0x80);␊ |
1083 | ␉␉␉␉␊ |
1084 | ␉␉␉␉␊ |
1085 | ␉␉␉␉tmp2 = 0;␊ |
1086 | ␉␉␉␉bits = 0;␊ |
1087 | ␉␉␉␉do␊ |
1088 | ␉␉␉␉{␊ |
1089 | ␉␉␉␉␉tmp2 |= (bind_stream[++i] & 0x7f) << bits;␊ |
1090 | ␉␉␉␉␉bits += 7;␊ |
1091 | ␉␉␉␉}␊ |
1092 | ␉␉␉␉while(bind_stream[i] & 0x80);␊ |
1093 | ␉␉␉␉␊ |
1094 | #if DEBUG_MODULES==2␊ |
1095 | ␉␉␉␉DBG("BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB 0x%X 0x%X\n", tmp, tmp2);␊ |
1096 | #endif␉␉␉␉␊ |
1097 | ␊ |
1098 | if(symbolAddr != 0xFFFFFFFF)␊ |
1099 | ␉␉␉␉{␊ |
1100 | ␉␉␉␉␉for(index = 0; index < tmp; index++)␊ |
1101 | ␉␉␉␉␉{␊ |
1102 | ␉␉␉␉␉␉␊ |
1103 | ␉␉␉␉␉␉address = segmentAddress + (UInt32)base;␊ |
1104 | ␉␉␉␉␉␉␊ |
1105 | ␉␉␉␉␉␉bind_location((UInt32*)address, (char*)symbolAddr, addend, BIND_TYPE_POINTER);␊ |
1106 | ␉␉␉␉␉␉␊ |
1107 | ␉␉␉␉␉␉segmentAddress += tmp2 + sizeof(void*);␊ |
1108 | ␉␉␉␉␉}␊ |
1109 | ␉␉␉␉}␊ |
1110 | ␉␉␉␉else if(symbolName && (strcmp(symbolName, SYMBOL_DYLD_STUB_BINDER) != 0))␊ |
1111 | ␉␉␉␉{␊ |
1112 | ␉␉␉␉␉printf("Unable to bind symbol %s needed by %s\n", symbolName, module);␊ |
1113 | getc();␊ |
1114 | goto error; ␊ |
1115 | ␊ |
1116 | ␉␉␉␉}␉␉␉␉␊ |
1117 | ␉␉␉␉␊ |
1118 | ␉␉␉␉break;␊ |
1119 | ␉␉␉default:␊ |
1120 | ␉␉␉␉break;␊ |
1121 | ␉␉␉␉␊ |
1122 | ␉␉}␊ |
1123 | ␉␉i++;␊ |
1124 | ␉}␊ |
1125 | return EFI_SUCCESS;␊ |
1126 | error:␊ |
1127 | return EFI_NOT_FOUND;␊ |
1128 | }␊ |
1129 | ␊ |
1130 | void rebase_location(UInt32* location, char* base, int type)␊ |
1131 | {␉␊ |
1132 | ␉switch(type)␊ |
1133 | ␉{␊ |
1134 | ␉␉case REBASE_TYPE_POINTER:␊ |
1135 | ␉␉case REBASE_TYPE_TEXT_ABSOLUTE32:␊ |
1136 | ␉␉␉*location += (UInt32)base;␊ |
1137 | ␉␉␉break;␊ |
1138 | ␉␉␉␊ |
1139 | ␉␉default:␊ |
1140 | ␉␉␉break;␊ |
1141 | ␉}␊ |
1142 | }␊ |
1143 | ␊ |
1144 | void bind_location(UInt32* location, char* value, UInt32 addend, int type)␊ |
1145 | {␉␊ |
1146 | ␉// do actual update␊ |
1147 | ␉char* newValue = value + addend;␊ |
1148 | ␉␊ |
1149 | ␉switch (type) {␊ |
1150 | ␉␉case BIND_TYPE_POINTER:␊ |
1151 | ␉␉case BIND_TYPE_TEXT_ABSOLUTE32:␊ |
1152 | ␉␉␉break;␊ |
1153 | ␉␉␉␊ |
1154 | ␉␉case BIND_TYPE_TEXT_PCREL32:␊ |
1155 | ␉␉␉newValue -= ((UInt32)location + 4);␊ |
1156 | ␉␉␉␊ |
1157 | ␉␉␉break;␊ |
1158 | ␉␉default:␊ |
1159 | ␉␉␉return;␊ |
1160 | ␉}␊ |
1161 | ␉*location = (UInt32)newValue;␊ |
1162 | ␉␊ |
1163 | ␉␊ |
1164 | }␊ |
1165 | ␊ |
1166 | /*␊ |
1167 | * add_symbol␊ |
1168 | * This function adds a symbol from a module to the list of known symbols ␊ |
1169 | * possibly change to a pointer and add this to the Symbol module so that it can␊ |
1170 | * adjust it's internal symbol list (sort) to optimize locating new symbols␊ |
1171 | * NOTE: returns the address if the symbol is "start", else returns 0xFFFFFFFF␊ |
1172 | */␊ |
1173 | long long add_symbol(char* module,char* symbol, long long addr, char is64)␊ |
1174 | {␊ |
1175 | ␉if(is64) return 0xFFFFFFFF; // Fixme␊ |
1176 | ␉␊ |
1177 | ␉// This only can handle 32bit symbols ␊ |
1178 | ␉symbolList_t* new_entry= malloc(sizeof(symbolList_t));␊ |
1179 | ␉DBG("Adding symbol %s at 0x%X\n", symbol, addr);␊ |
1180 | ␉if (new_entry)␊ |
1181 | ␉{␉␊ |
1182 | ␉␉new_entry->next = moduleSymbols;␊ |
1183 | ␉␉␊ |
1184 | ␉␉moduleSymbols = new_entry;␊ |
1185 | ␉␉␊ |
1186 | ␉␉new_entry->addr = (UInt32)addr;␊ |
1187 | new_entry->module = module;␊ |
1188 | ␉␉new_entry->symbol = symbol;␊ |
1189 | ␉␉return addr;␊ |
1190 | ␉}␉␊ |
1191 | ␉␊ |
1192 | ␉return 0xFFFFFFFF;␊ |
1193 | ␉␊ |
1194 | }␊ |
1195 | ␊ |
1196 | // Look for symbols using the Smbols moduel function.␊ |
1197 | // If non are found, look through the list of module symbols␊ |
1198 | unsigned int lookup_all_symbols(const char* module, const char* name)␊ |
1199 | {␊ |
1200 | ␊ |
1201 | unsigned int addr = 0xFFFFFFFF;␊ |
1202 | ␊ |
1203 | do {␊ |
1204 | ␊ |
1205 | if ((module != NULL) && (strcmp(module,SYMBOLS_BUNDLE) != 0))␊ |
1206 | break; ␊ |
1207 | ␊ |
1208 | if(lookup_symbol && (UInt32)lookup_symbol != 0xFFFFFFFF)␊ |
1209 | {␊ |
1210 | addr = lookup_symbol(name, &strcmp);␊ |
1211 | if(addr != 0xFFFFFFFF)␊ |
1212 | {␊ |
1213 | DBG("Internal symbol %s located at 0x%X\n", name, addr);␊ |
1214 | goto out;␊ |
1215 | }␊ |
1216 | } ␊ |
1217 | ␊ |
1218 | } while (0);␊ |
1219 | ␊ |
1220 | ␊ |
1221 | ␉{␊ |
1222 | ␉␉symbolList_t* entry = moduleSymbols;␊ |
1223 | ␉␉while(entry)␊ |
1224 | ␉␉{␊ |
1225 | if ((module != NULL) && (strcmp(entry->module,module) != 0))␊ |
1226 | {␊ |
1227 | entry = entry->next;␊ |
1228 | continue; ␊ |
1229 | }␊ |
1230 | ␊ |
1231 | if(strcmp(entry->symbol, name) == 0)␊ |
1232 | {␊ |
1233 | DBG("External symbol %s located at 0x%X\n", name, entry->addr);␊ |
1234 | addr = entry->addr;␊ |
1235 | goto out;␊ |
1236 | }␊ |
1237 | else␊ |
1238 | {␊ |
1239 | entry = entry->next;␊ |
1240 | } ␊ |
1241 | ␊ |
1242 | ␉␉}␊ |
1243 | ␉}␊ |
1244 | ␉␊ |
1245 | #if DEBUG_MODULES␊ |
1246 | ␉if(strcmp(name, SYMBOL_DYLD_STUB_BINDER) != 0)␊ |
1247 | ␉{␊ |
1248 | ␉␉verbose("Unable to locate symbol %s\n", name);␊ |
1249 | ␉␉getc();␊ |
1250 | ␉}␊ |
1251 | #endif␊ |
1252 | out:␊ |
1253 | return addr;␊ |
1254 | ␊ |
1255 | }␊ |
1256 | ␊ |
1257 | ␊ |
1258 | /*␊ |
1259 | * parse the symbol table␊ |
1260 | * Lookup any undefined symbols␊ |
1261 | */␊ |
1262 | ␊ |
1263 | unsigned int handle_symtable(char *module, UInt32 base, struct symtab_command* symtabCommand, long long(*symbol_handler)(char*, char*, long long, char), char is64)␊ |
1264 | {␉␉␊ |
1265 | ␉unsigned int module_start = 0xFFFFFFFF;␊ |
1266 | ␉␊ |
1267 | ␉UInt32 symbolIndex = 0;␊ |
1268 | if (!symtabCommand) {␊ |
1269 | return 0xFFFFFFFF;␊ |
1270 | }␊ |
1271 | ␉char* symbolString = base + (char*)symtabCommand->stroff;␊ |
1272 | ␉//char* symbolTable = base + symtabCommand->symoff;␊ |
1273 | ␉if(!is64)␊ |
1274 | ␉{␊ |
1275 | ␉␉struct nlist* symbolEntry = (void*)base + symtabCommand->symoff;␊ |
1276 | ␉␉while(symbolIndex < symtabCommand->nsyms)␊ |
1277 | ␉␉{␉␉␉␉␉␉␊ |
1278 | ␉␉␉if(symbolEntry->n_value)␊ |
1279 | ␉␉␉{␉␉␉␉␊ |
1280 | ␉␉␉␉if(strstr(symbolString + symbolEntry->n_un.n_strx, "module_start") || (strcmp(symbolString + symbolEntry->n_un.n_strx, "start") == 0))␊ |
1281 | ␉␉␉␉{␊ |
1282 | ␉␉␉␉␉module_start = base + symbolEntry->n_value;␊ |
1283 | ␉␉␉␉␉DBG("n_value %x module_start %x\n", (unsigned)symbolEntry->n_value, (unsigned)module_start);␊ |
1284 | ␉␉␉␉}␊ |
1285 | ␉␉␉␉else␊ |
1286 | ␉␉␉␉{␊ |
1287 | ␉␉␉␉␉symbol_handler(module, symbolString + symbolEntry->n_un.n_strx, (long long)base + symbolEntry->n_value, is64);␊ |
1288 | ␉␉␉␉}␊ |
1289 | #if DEBUG_MODULES␊ |
1290 | ␉␉␉␉bool isTexT = (((unsigned)symbolEntry->n_value > (unsigned)vmaddr) && ((unsigned)(vmaddr + vmsize) > (unsigned)symbolEntry->n_value ));␊ |
1291 | ␉␉␉␉printf("%s %s\n", isTexT ? "__TEXT :" : "__DATA(OR ANY) :", symbolString + symbolEntry->n_un.n_strx);␊ |
1292 | #if DEBUG_MODULES==2␉␊ |
1293 | ␉␉␉␉␊ |
1294 | ␉␉␉␉if(strcmp(symbolString + symbolEntry->n_un.n_strx, "_BootHelp_txt") == 0)␊ |
1295 | ␉␉␉␉{␊ |
1296 | ␉␉␉␉␉long long addr = (long long)base + symbolEntry->n_value;␊ |
1297 | ␉␉␉␉␉unsigned char *BootHelp = NULL;␊ |
1298 | ␉␉␉␉␉BootHelp = (unsigned char*)(UInt32)addr;␉␉␉␉␉␊ |
1299 | ␉␉␉␉␉printf("method 1: __DATA : BootHelp_txt[0] %x\n", BootHelp[0]);␊ |
1300 | ␉␉␉␉␉␊ |
1301 | ␉␉␉␉␉long long addr2 = symbolEntry->n_value;␊ |
1302 | ␉␉␉␉␉unsigned char *BootHelp2 = NULL;␊ |
1303 | ␉␉␉␉␉BootHelp2 = (unsigned char*)(UInt32)addr2;␉␉␉␉␉␊ |
1304 | ␉␉␉␉␉printf("method 2: __DATA : BootHelp_txt[0] %x\n", BootHelp2[0]);␊ |
1305 | ␉␉␉␉}␊ |
1306 | #endif␊ |
1307 | #endif␊ |
1308 | ␉␉␉␉␊ |
1309 | ␉␉␉}␊ |
1310 | ␉␉␉␊ |
1311 | ␉␉␉symbolEntry++;␊ |
1312 | ␉␉␉symbolIndex++;␉// TODO remove␊ |
1313 | ␉␉}␊ |
1314 | ␉}␊ |
1315 | ␉else␊ |
1316 | ␉{␊ |
1317 | ␉␉struct nlist_64* symbolEntry = (void*)base + symtabCommand->symoff;␊ |
1318 | ␉␉// NOTE First entry is *not* correct, but we can ignore it (i'm getting radar:// right now)␉␊ |
1319 | ␉␉while(symbolIndex < symtabCommand->nsyms)␊ |
1320 | ␉␉{␉␊ |
1321 | ␉␉␉␊ |
1322 | ␉␉␉if(strstr(symbolString + symbolEntry->n_un.n_strx, "module_start") || (strcmp(symbolString + symbolEntry->n_un.n_strx, "start") == 0))␊ |
1323 | ␉␉␉{␊ |
1324 | ␉␉␉␉module_start = (unsigned int)(base + symbolEntry->n_value);␊ |
1325 | ␉␉␉}␊ |
1326 | ␉␉␉else␊ |
1327 | ␉␉␉{␊ |
1328 | ␉␉␉␉symbol_handler(module, symbolString + symbolEntry->n_un.n_strx, (long long)base + symbolEntry->n_value, is64);␊ |
1329 | ␉␉␉}␊ |
1330 | ␉␉␉␊ |
1331 | ␉␉␉symbolEntry++;␊ |
1332 | ␉␉␉symbolIndex++;␉// TODO remove␊ |
1333 | ␉␉}␊ |
1334 | ␉}␊ |
1335 | ␉␊ |
1336 | ␉return module_start;␊ |
1337 | ␉␊ |
1338 | }␊ |
1339 | ␊ |
1340 | ␊ |
1341 | /*␊ |
1342 | * Locate the symbol for an already loaded function and modify the beginning of␊ |
1343 | * the function to jump directly to the new one␊ |
1344 | * example: replace_system_function("_getc", &replacement);␊ |
1345 | * replace_function(module_name,"_getc", &replacement);␊ |
1346 | * replace_function_any("_getc", &replacement);␊ |
1347 | */␊ |
1348 | EFI_STATUS replace_function(const char* module, const char* symbol, void* newAddress)␊ |
1349 | {␉␉ ␊ |
1350 | ␉// TODO: look into using the next four bytes of the function instead␊ |
1351 | ␉// Most functions should support this, as they probably will be at ␊ |
1352 | ␉// least 10 bytes long, but you never know, this is sligtly safer as␊ |
1353 | ␉// function can be as small as 6 bytes.␊ |
1354 | ␉UInt32 addr = lookup_all_symbols(module, symbol);␊ |
1355 | ␉␊ |
1356 | ␉char* binary = (char*)addr;␊ |
1357 | ␉if(addr != 0xFFFFFFFF)␊ |
1358 | ␉{␊ |
1359 | ␉␉UInt32* jumpPointer = malloc(sizeof(UInt32*));␊ |
1360 | ␉␉if (!jumpPointer) {␊ |
1361 | ␉␉␉return EFI_OUT_OF_RESOURCES;␊ |
1362 | ␉␉}␊ |
1363 | ␉␉␊ |
1364 | ␉␉*binary++ = 0xFF;␉// Jump␊ |
1365 | ␉␉*binary++ = 0x25;␉// Long Jump␊ |
1366 | ␉␉*((UInt32*)binary) = (UInt32)jumpPointer;␊ |
1367 | ␉␉␊ |
1368 | ␉␉*jumpPointer = (UInt32)newAddress;␊ |
1369 | ␉␉␊ |
1370 | ␉␉return EFI_SUCCESS;␊ |
1371 | ␉}␊ |
1372 | ␉␊ |
1373 | ␉return EFI_NOT_FOUND;␊ |
1374 | ␉␊ |
1375 | }␊ |
1376 | ␊ |
1377 | EFI_STATUS replace_system_function(const char* symbol, void* newAddress)␊ |
1378 | {␉␉ ␊ |
1379 | ␉␊ |
1380 | ␉return replace_function(SYMBOLS_BUNDLE,symbol,newAddress);␊ |
1381 | ␉␊ |
1382 | }␊ |
1383 | ␊ |
1384 | EFI_STATUS replace_function_any(const char* symbol, void* newAddress)␊ |
1385 | {␉␉ ␊ |
1386 | ␉␊ |
1387 | ␉return replace_function(NULL,symbol,newAddress);␊ |
1388 | ␉␊ |
1389 | }␊ |
1390 | /*␊ |
1391 | * Copyright (c) 1999-2003 Apple Computer, Inc. All rights reserved.␊ |
1392 | *␊ |
1393 | * @APPLE_LICENSE_HEADER_START@␊ |
1394 | * ␊ |
1395 | * Portions Copyright (c) 1999-2003 Apple Computer, Inc. All Rights␊ |
1396 | * Reserved. This file contains Original Code and/or Modifications of␊ |
1397 | * Original Code as defined in and that are subject to the Apple Public␊ |
1398 | * Source License Version 2.0 (the 'License'). You may not use this file␊ |
1399 | * except in compliance with the License. Please obtain a copy of the␊ |
1400 | * License at http://www.apple.com/publicsource and read it before using␊ |
1401 | * this file.␊ |
1402 | * ␊ |
1403 | * The Original Code and all software distributed under the License are␊ |
1404 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER␊ |
1405 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,␊ |
1406 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,␊ |
1407 | * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the␊ |
1408 | * License for the specific language governing rights and limitations␊ |
1409 | * under the License.␊ |
1410 | * ␊ |
1411 | * @APPLE_LICENSE_HEADER_END@␊ |
1412 | */␊ |
1413 | /*␊ |
1414 | * drivers.c - Driver Loading Functions.␊ |
1415 | *␊ |
1416 | * Copyright (c) 2000 Apple Computer, Inc.␊ |
1417 | *␊ |
1418 | * DRI: Josh de Cesare␊ |
1419 | */␊ |
1420 | ␊ |
1421 | /*␊ |
1422 | * Copyright 2012 Cadet-petit Armel <armelcadetpetit@gmail.com>. All rights reserved.␊ |
1423 | *␊ |
1424 | * Cleaned, Added (runtime) bundles support.␊ |
1425 | *␊ |
1426 | */␊ |
1427 | #include <mach-o/fat.h>␊ |
1428 | #include <libkern/OSByteOrder.h>␊ |
1429 | #include <mach/machine.h>␊ |
1430 | ␊ |
1431 | #include "sl.h"␊ |
1432 | #include "xml.h"␊ |
1433 | ␊ |
1434 | struct Module { ␊ |
1435 | ␉struct Module *nextModule;␊ |
1436 | ␉long willLoad;␊ |
1437 | ␉TagPtr dict;␊ |
1438 | TagPtr personalities;␊ |
1439 | ␉char *plistAddr;␊ |
1440 | ␉long plistLength;␊ |
1441 | ␉char *executablePath;␊ |
1442 | ␉char *bundlePath;␊ |
1443 | ␉long bundlePathLength;␊ |
1444 | };␊ |
1445 | typedef struct Module Module, *ModulePtr;␊ |
1446 | ␊ |
1447 | ␊ |
1448 | enum {␊ |
1449 | ␉kCFBundleType2,␊ |
1450 | ␉kCFBundleType3␊ |
1451 | };␊ |
1452 | ␊ |
1453 | enum {␊ |
1454 | ␉BundlePriorityNull = 0,␊ |
1455 | ␉BundlePriorityInit = 1,␊ |
1456 | ␉BundlePrioritySystem = 2,␊ |
1457 | ␉BundlePrioritySystemLib = 3,␊ |
1458 | BundlePriorityNormalPriority = 4,␊ |
1459 | BundlePriorityLowestPriority = 99,␊ |
1460 | ␉BundlePriorityEnd = 100 // can not be assigned␊ |
1461 | ␊ |
1462 | };␊ |
1463 | ␊ |
1464 | static ModulePtr gModuleHead;␊ |
1465 | static char * gModulesSpec;␊ |
1466 | static char * gDriverSpec;␊ |
1467 | static char * gFileSpec;␊ |
1468 | static char * gTempSpec;␊ |
1469 | static char * gFileName;␊ |
1470 | static int gLowestLoadPriority;␊ |
1471 | ␊ |
1472 | static long ParseXML(char *buffer, ModulePtr *module);␊ |
1473 | static ModulePtr FindBundle( char * bundle_id );␊ |
1474 | ␊ |
1475 | static void␊ |
1476 | FreeBundleSupport( void )␊ |
1477 | {␊ |
1478 | ␊ |
1479 | if ( gModulesSpec ) free(gModulesSpec);␊ |
1480 | if ( gDriverSpec) free(gDriverSpec);␊ |
1481 | if ( gFileSpec ) free(gFileSpec);␊ |
1482 | if ( gTempSpec ) free(gTempSpec);␊ |
1483 | if ( gFileName ) free(gFileName);␊ |
1484 | }␊ |
1485 | ␊ |
1486 | //==========================================================================␊ |
1487 | // InitBundleSupport␊ |
1488 | ␊ |
1489 | long␊ |
1490 | InitBundleSupport( void )␊ |
1491 | {␊ |
1492 | ␉DBG("InitBundleSupport\n");␊ |
1493 | ␊ |
1494 | static bool BundleSet = false;␊ |
1495 | ␊ |
1496 | if (BundleSet == true) return 0; ␊ |
1497 | ␊ |
1498 | gModulesSpec = malloc( 4096 );␊ |
1499 | gDriverSpec = malloc( 4096 );␊ |
1500 | gFileSpec = malloc( 4096 );␊ |
1501 | gTempSpec = malloc( 4096 );␊ |
1502 | gFileName = malloc( 4096 );␉␊ |
1503 | ␊ |
1504 | if ( !gModulesSpec || !gDriverSpec || !gFileSpec || !gTempSpec || !gFileName )␊ |
1505 | goto error;␊ |
1506 | ␊ |
1507 | BundleSet = true;␊ |
1508 | ␊ |
1509 | return 0;␊ |
1510 | error:␊ |
1511 | FreeBundleSupport();␊ |
1512 | return 1;␊ |
1513 | }␊ |
1514 | ␊ |
1515 | //==========================================================================␊ |
1516 | // LoadBundles␊ |
1517 | ␊ |
1518 | long LoadBundles( char * dirSpec )␊ |
1519 | {␉␊ |
1520 | ␉DBG("LoadBundles\n");␊ |
1521 | ␊ |
1522 | if ( InitBundleSupport() != 0 )␊ |
1523 | return 1;␊ |
1524 | ␉␊ |
1525 | ␉␊ |
1526 | ␉strlcpy(gModulesSpec, dirSpec, 4096);␊ |
1527 | strlcat(gModulesSpec, "Modules", 4096 - 1);␊ |
1528 | FileLoadBundles(gModulesSpec, 0);␉␉␊ |
1529 | ␊ |
1530 | ␉␊ |
1531 | MatchBundlesLibraries();␊ |
1532 | ␉␊ |
1533 | LoadMatchedBundles();␊ |
1534 | ␊ |
1535 | ␉DBG("LoadBundles Finished\n");␊ |
1536 | ␊ |
1537 | return 0;␊ |
1538 | }␊ |
1539 | ␊ |
1540 | //==========================================================================␊ |
1541 | // FileLoadBundles␊ |
1542 | long␊ |
1543 | FileLoadBundles( char * dirSpec, long plugin )␊ |
1544 | {␊ |
1545 | long ret, length, flags, time, bundleType;␊ |
1546 | long long␉ index;␊ |
1547 | long result = -1;␊ |
1548 | const char * name;␊ |
1549 | ␊ |
1550 | ␉DBG("FileLoadBundles in %s\n",dirSpec);␊ |
1551 | ␊ |
1552 | index = 0;␊ |
1553 | while (1) {␊ |
1554 | ret = GetDirEntry(dirSpec, &index, &name, &flags, &time);␊ |
1555 | if (ret == -1) break;␊ |
1556 | ␉␉␊ |
1557 | // Make sure this is a directory.␊ |
1558 | if ((flags & kFileTypeMask) != kFileTypeDirectory) continue;␊ |
1559 | ␊ |
1560 | // Make sure this is a kext.␊ |
1561 | length = strlen(name);␊ |
1562 | if (strcmp(name + length - 7, ".bundle")) continue;␊ |
1563 | ␉␉␊ |
1564 | // Save the file name.␊ |
1565 | strlcpy(gFileName, name, 4096);␊ |
1566 | ␉␉DBG("Load Bundles %s\n",gFileName);␊ |
1567 | ␊ |
1568 | // Determine the bundle type.␊ |
1569 | sprintf(gTempSpec, "%s/%s", dirSpec, gFileName);␊ |
1570 | ret = GetFileInfo(gTempSpec, "Contents", &flags, &time);␊ |
1571 | if (ret == 0) bundleType = kCFBundleType2;␊ |
1572 | else bundleType = kCFBundleType3;␊ |
1573 | ␉␉␊ |
1574 | ␉␉DBG("Bundles type = %d\n",bundleType);␊ |
1575 | ␊ |
1576 | if (!plugin)␊ |
1577 | sprintf(gDriverSpec, "%s/%s/%sPlugIns", dirSpec, gFileName,␊ |
1578 | (bundleType == kCFBundleType2) ? "Contents/" : "");␊ |
1579 | ␉␉␊ |
1580 | ret = LoadBundlePList( dirSpec, gFileName, bundleType);␊ |
1581 | ␉␉␊ |
1582 | if (result != 0)␊ |
1583 | ␉␉␉result = ret;␊ |
1584 | ␉␉␊ |
1585 | if (!plugin) ␊ |
1586 | ␉␉␉FileLoadBundles(gDriverSpec, 1);␊ |
1587 | }␊ |
1588 | ␉␊ |
1589 | return result;␊ |
1590 | }␊ |
1591 | ␊ |
1592 | ␊ |
1593 | static void add_bundle(ModulePtr module,char* name)␊ |
1594 | {␊ |
1595 | ␉ModulePtr new_entry= malloc(sizeof(Module));␊ |
1596 | ␉DBG("Adding bundle %s \n", name );␊ |
1597 | ␉if (new_entry)␊ |
1598 | ␉{␉␊ |
1599 | ␉␉new_entry->nextModule = gModuleHead;␊ |
1600 | ␉␉␊ |
1601 | ␉␉gModuleHead = new_entry;␊ |
1602 | ␉␉␊ |
1603 | ␉␉new_entry->executablePath = module->executablePath;␊ |
1604 | new_entry->bundlePath = module->bundlePath;␊ |
1605 | new_entry->bundlePathLength = module->bundlePathLength;␊ |
1606 | new_entry->plistAddr = module->plistAddr;␊ |
1607 | new_entry->willLoad = module->willLoad;␊ |
1608 | new_entry->dict = module->dict;␊ |
1609 | new_entry->plistLength = module->plistLength;␊ |
1610 | new_entry->personalities = module->personalities;␊ |
1611 | ␊ |
1612 | ␉}␉␊ |
1613 | ␉␊ |
1614 | }␊ |
1615 | ␊ |
1616 | //==========================================================================␊ |
1617 | // LoadBundlePList␊ |
1618 | ␊ |
1619 | long␊ |
1620 | LoadBundlePList( char * dirSpec, char * name, long bundleType )␊ |
1621 | {␊ |
1622 | long length, executablePathLength, bundlePathLength;␊ |
1623 | ModulePtr module = 0;␊ |
1624 | char * buffer = 0;␊ |
1625 | char * tmpExecutablePath = 0;␊ |
1626 | char * tmpBundlePath = 0;␊ |
1627 | long ret = -1;␊ |
1628 | ␉DBG("LoadBundlePList\n");␊ |
1629 | ␊ |
1630 | do {␊ |
1631 | // Save the driver path.␊ |
1632 | ␊ |
1633 | sprintf(gFileSpec, "%s/%s/%s", dirSpec, name,␊ |
1634 | (bundleType == kCFBundleType2) ? "Contents/MacOS/" : "");␊ |
1635 | executablePathLength = strlen(gFileSpec) + 1;␊ |
1636 | ␉␉␊ |
1637 | tmpExecutablePath = malloc(executablePathLength);␊ |
1638 | if (tmpExecutablePath == 0) break;␊ |
1639 | ␊ |
1640 | strlcpy(tmpExecutablePath, gFileSpec, executablePathLength);␊ |
1641 | ␊ |
1642 | sprintf(gFileSpec, "%s/%s", dirSpec, name);␊ |
1643 | bundlePathLength = strlen(gFileSpec) + 1;␊ |
1644 | ␉␉␊ |
1645 | tmpBundlePath = malloc(bundlePathLength);␊ |
1646 | if (tmpBundlePath == 0) break;␊ |
1647 | ␉␉␊ |
1648 | strlcpy(tmpBundlePath, gFileSpec, bundlePathLength);␊ |
1649 | ␊ |
1650 | ␊ |
1651 | // Construct the file spec to the plist, then load it.␊ |
1652 | ␉␉␊ |
1653 | sprintf(gFileSpec, "%s/%s/%sInfo.plist", dirSpec, name,␊ |
1654 | (bundleType == kCFBundleType2) ? "Contents/" : "");␊ |
1655 | ␉␉␊ |
1656 | ␉␉DBG("Loading Bundle PList %s\n",gFileSpec);␊ |
1657 | ␊ |
1658 | length = LoadFile(gFileSpec);␊ |
1659 | if (length == -1) break;␊ |
1660 | ␉␉␊ |
1661 | length = length + 1;␊ |
1662 | buffer = malloc(length);␊ |
1663 | if (buffer == 0) break;␊ |
1664 | ␉␉␊ |
1665 | strlcpy(buffer, (char *)kLoadAddr, length);␊ |
1666 | ␉␉␊ |
1667 | // Parse the plist.␊ |
1668 | ␉␉␊ |
1669 | ret = ParseXML(buffer, &module);␊ |
1670 | if (ret != 0 ) { printf("Unable to read plist of %s",name); break; }␊ |
1671 | ␉␉␊ |
1672 | ␉␉if (!module) break; // Should never happen but it will make the compiler happy␊ |
1673 | ␊ |
1674 | // Allocate memory for the driver path and the plist.␊ |
1675 | ␊ |
1676 | module->executablePath = tmpExecutablePath;␊ |
1677 | module->bundlePath = tmpBundlePath;␊ |
1678 | module->bundlePathLength = bundlePathLength;␊ |
1679 | module->plistAddr = malloc(length);␊ |
1680 | ␉␉␊ |
1681 | if ((module->executablePath == 0) || (module->bundlePath == 0) || (module->plistAddr == 0))␊ |
1682 | {␊ |
1683 | if ( module->plistAddr ) free(module->plistAddr);␊ |
1684 | ␉␉␉ret = -1;␊ |
1685 | break;␊ |
1686 | } ␊ |
1687 | ␉␉␊ |
1688 | // Add the plist to the module.␊ |
1689 | ␉␉␊ |
1690 | strlcpy(module->plistAddr, (char *)kLoadAddr, length);␊ |
1691 | module->plistLength = length;␊ |
1692 | ␉␉␊ |
1693 | // Add the module to the module list.␊ |
1694 | ␊ |
1695 | add_bundle(module, name); ␊ |
1696 | ␊ |
1697 | ␊ |
1698 | ret = 0;␊ |
1699 | }␊ |
1700 | while (0);␊ |
1701 | ␊ |
1702 | if ( buffer ) free( buffer );␊ |
1703 | if ( module ) free( module );␊ |
1704 | ␊ |
1705 | if (ret != 0) {␊ |
1706 | if ( tmpExecutablePath ) free( tmpExecutablePath );␊ |
1707 | if ( tmpBundlePath ) free( tmpBundlePath );␊ |
1708 | }␉␊ |
1709 | return ret;␊ |
1710 | }␊ |
1711 | ␊ |
1712 | #define WillLoadBundles \␊ |
1713 | module = gModuleHead; \␊ |
1714 | while (module != NULL) \␊ |
1715 | { \␊ |
1716 | if (module->willLoad == willLoad) \␊ |
1717 | { \␊ |
1718 | prop = XMLGetProperty(module->dict, kPropCFBundleExecutable); \␊ |
1719 | \␊ |
1720 | if (prop != 0) \␊ |
1721 | { \␊ |
1722 | fileName = prop->string; \␊ |
1723 | sprintf(gFileSpec, "%s%s", module->executablePath, fileName); \␊ |
1724 | \␊ |
1725 | module_start = (void*)load_module((char*)fileName,gFileSpec); \␊ |
1726 | if(!module_start || (*module_start == (void*)0xFFFFFFFF)) \␊ |
1727 | { \␊ |
1728 | if (module->willLoad > BundlePrioritySystemLib) \␊ |
1729 | { \␊ |
1730 | module->willLoad = BundlePriorityNull ; \␊ |
1731 | printf("Unable to start %s\n", gFileSpec); \␊ |
1732 | } \␊ |
1733 | } else module_start(); \␊ |
1734 | if (module->willLoad == BundlePrioritySystem) \␊ |
1735 | { \␊ |
1736 | lookup_symbol = (void*)lookup_all_symbols(SYMBOLS_BUNDLE,SYMBOL_LOOKUP_SYMBOL); \␊ |
1737 | if((UInt32)lookup_symbol != 0xFFFFFFFF) \␊ |
1738 | { \␊ |
1739 | msglog("%s successfully Loaded.\n", gFileSpec); \␊ |
1740 | } else return -1; \␊ |
1741 | } \␊ |
1742 | } \␊ |
1743 | } \␊ |
1744 | module = module->nextModule; \␊ |
1745 | } ␊ |
1746 | ␊ |
1747 | //==========================================================================␊ |
1748 | // LoadMatchedBundles␊ |
1749 | ␊ |
1750 | long LoadMatchedBundles( void )␊ |
1751 | {␊ |
1752 | TagPtr prop;␊ |
1753 | ModulePtr module;␊ |
1754 | char *fileName;␊ |
1755 | void (*module_start)(void);␊ |
1756 | long willLoad;␊ |
1757 | ␊ |
1758 | ␉DBG("LoadMatchedBundles\n");␊ |
1759 | ␊ |
1760 | ␉int priority_end = MIN(gLowestLoadPriority+1, BundlePriorityEnd);␊ |
1761 | ␊ |
1762 | for (willLoad = BundlePrioritySystem; willLoad < priority_end ; willLoad++)␊ |
1763 | {␊ |
1764 | ␊ |
1765 | WillLoadBundles ;␊ |
1766 | ␊ |
1767 | }␊ |
1768 | ␊ |
1769 | return 0;␊ |
1770 | }␊ |
1771 | ␊ |
1772 | //==========================================================================␊ |
1773 | // MatchBundlesLibraries␊ |
1774 | ␊ |
1775 | long MatchBundlesLibraries( void )␊ |
1776 | {␊ |
1777 | ␊ |
1778 | TagPtr prop, prop2;␊ |
1779 | ModulePtr module, module2,dummy_module;␊ |
1780 | ␉␊ |
1781 | // Check for active modules with the same Bundle IDs or same principal class, only one must remain (except for type 3 aka system libs)␊ |
1782 | {␊ |
1783 | module = gModuleHead;␊ |
1784 | ␊ |
1785 | while (module != 0)␊ |
1786 | {␊ |
1787 | if (!(module->willLoad > BundlePriorityInit)) // if the module load priority is not higher than initialized, continue␊ |
1788 | {␊ |
1789 | module = module->nextModule;␊ |
1790 | continue;␊ |
1791 | }␊ |
1792 | ␊ |
1793 | prop = XMLGetProperty(module->dict, kPropNSPrincipalClass);␊ |
1794 | prop2 = XMLGetProperty(module->dict, kPropCFBundleIdentifier); ␊ |
1795 | ␊ |
1796 | if (prop != 0 && prop2 != 0)␊ |
1797 | { ␊ |
1798 | module2 = gModuleHead;␊ |
1799 | ␊ |
1800 | TagPtr prop3,prop4;␊ |
1801 | ␊ |
1802 | while (module2 != 0)␊ |
1803 | {␊ |
1804 | prop3 = XMLGetProperty(module2->dict, kPropNSPrincipalClass);␊ |
1805 | prop4 = XMLGetProperty(module2->dict, kPropCFBundleIdentifier); ␊ |
1806 | ␊ |
1807 | if ((prop3 != 0) && (prop4 != 0) && (module != module2))␊ |
1808 | {␊ |
1809 | ␊ |
1810 | if ((module2->willLoad == BundlePrioritySystemLib) && ((strcmp(prop2->string, prop4->string)!= 0) /*&& (!strcmp(prop->string, prop3->string))*/)) {␊ |
1811 | continue;␊ |
1812 | }␊ |
1813 | ␊ |
1814 | if ((!strcmp(prop2->string, prop4->string)) || (!strcmp(prop->string, prop3->string))) {␊ |
1815 | if (module2->willLoad > BundlePriorityNull) module2->willLoad = BundlePriorityNull;␊ |
1816 | }␊ |
1817 | ␊ |
1818 | }␊ |
1819 | module2 = module2->nextModule;␊ |
1820 | } ␊ |
1821 | ␊ |
1822 | }␊ |
1823 | ␊ |
1824 | module = module->nextModule;␊ |
1825 | }␊ |
1826 | }␊ |
1827 | ␊ |
1828 | // Check for dependencies (it works in most cases, still a little buggy but should be sufficient for what we have to do, ␊ |
1829 | ␉// clearly the Achilles' heel of this implementation, please use dependencies with caution !!!)␊ |
1830 | dummy_module = gModuleHead;␊ |
1831 | while (dummy_module != 0)␊ |
1832 | {␊ |
1833 | module = gModuleHead;␊ |
1834 | ␊ |
1835 | while (module != 0)␊ |
1836 | {␊ |
1837 | if (module->willLoad > BundlePrioritySystemLib)␊ |
1838 | { ␊ |
1839 | prop = XMLGetProperty(module->dict, kPropOSBundleLibraries);␊ |
1840 | if (prop != 0)␊ |
1841 | {␊ |
1842 | prop = prop->tag;␊ |
1843 | while (prop != 0)␊ |
1844 | {␊ |
1845 | module2 = gModuleHead;␊ |
1846 | while (module2 != 0)␊ |
1847 | {␊ |
1848 | prop2 = XMLGetProperty(module2->dict, kPropCFBundleIdentifier);␊ |
1849 | if ((prop2 != 0) && (!strncmp(prop->string, prop2->string, strlen( prop->string))))␊ |
1850 | {␊ |
1851 | // found a parent␊ |
1852 | ␊ |
1853 | if (module2->willLoad > BundlePriorityInit)␊ |
1854 | { ␊ |
1855 | // parent is active␊ |
1856 | if (module->willLoad == BundlePriorityNull) {␊ |
1857 | module->willLoad = BundlePriorityNormalPriority;␊ |
1858 | } ␊ |
1859 | ␊ |
1860 | // Check if the version of the parent >= version of the child␊ |
1861 | if (strtol(XMLCastString ( XMLGetProperty(module2->dict,"CFBundleShortVersionString") ), NULL, 10) >= strtol(XMLCastString( prop->tag ), NULL, 10)) {␊ |
1862 | ␊ |
1863 | if ((module2->willLoad >= module->willLoad) && (module->willLoad > BundlePrioritySystemLib))␊ |
1864 | ␉␉␉␉␉␉␉␉␉␉␉module->willLoad = MIN(MAX(module->willLoad, module2->willLoad+1), BundlePriorityLowestPriority); // child must be loaded after the parent, this allow the to find symbols of the parent while we bind the child.␊ |
1865 | ␊ |
1866 | } else {␊ |
1867 | module->willLoad = BundlePriorityNull;␊ |
1868 | goto nextmodule;␊ |
1869 | }␊ |
1870 | break;␊ |
1871 | }␊ |
1872 | ␊ |
1873 | }␊ |
1874 | ␉␉␉␉␉␉␉if (module->willLoad != BundlePriorityNull) module->willLoad = BundlePriorityNull;␊ |
1875 | module2 = module2->nextModule;␊ |
1876 | }␊ |
1877 | if (module->willLoad == BundlePriorityNull) goto nextmodule;␊ |
1878 | prop = prop->tagNext;␊ |
1879 | }␊ |
1880 | }␊ |
1881 | }␊ |
1882 | nextmodule:␊ |
1883 | module = module->nextModule;␊ |
1884 | }␊ |
1885 | ␊ |
1886 | ␉␉dummy_module = dummy_module->nextModule;␊ |
1887 | }␉␊ |
1888 | ␉␊ |
1889 | // Get the lowest load priority␊ |
1890 | {␊ |
1891 | gLowestLoadPriority = BundlePriorityNormalPriority;␊ |
1892 | module = gModuleHead;␊ |
1893 | ␊ |
1894 | while (module != 0)␊ |
1895 | {␊ |
1896 | if (module->willLoad > BundlePriorityInit)␊ |
1897 | { ␊ |
1898 | gLowestLoadPriority = MIN(module->willLoad, BundlePriorityLowestPriority); ␊ |
1899 | }␊ |
1900 | module = module->nextModule;␊ |
1901 | }␊ |
1902 | }␊ |
1903 | ␊ |
1904 | return 0;␊ |
1905 | }␊ |
1906 | ␊ |
1907 | ␊ |
1908 | //==========================================================================␊ |
1909 | // FindBundle␊ |
1910 | ␊ |
1911 | static ModulePtr␊ |
1912 | FindBundle( char * bundle_id )␊ |
1913 | {␊ |
1914 | ModulePtr module;␊ |
1915 | TagPtr prop;␊ |
1916 | ␉DBG("FindBundle %s\n",bundle_id);␊ |
1917 | ␊ |
1918 | module = gModuleHead;␊ |
1919 | ␊ |
1920 | while (module != 0)␊ |
1921 | {␊ |
1922 | prop = XMLGetProperty(module->dict, kPropCFBundleIdentifier);␊ |
1923 | if ((prop != 0) && !strcmp(bundle_id, prop->string)) break;␊ |
1924 | module = module->nextModule;␊ |
1925 | }␊ |
1926 | ␊ |
1927 | return module;␊ |
1928 | }␊ |
1929 | ␊ |
1930 | //==========================================================================␊ |
1931 | // GetBundleDict␊ |
1932 | ␊ |
1933 | void *␊ |
1934 | GetBundleDict( char * bundle_id )␊ |
1935 | {␊ |
1936 | ModulePtr module;␊ |
1937 | ␉DBG("GetBundleDict %s\n",bundle_id);␊ |
1938 | ␊ |
1939 | module = FindBundle( bundle_id );␊ |
1940 | ␊ |
1941 | if (module != 0)␊ |
1942 | {␊ |
1943 | return (void *)module->dict;␊ |
1944 | }␊ |
1945 | ␊ |
1946 | return 0;␊ |
1947 | }␊ |
1948 | ␊ |
1949 | //==========================================================================␊ |
1950 | // GetBundlePersonality␊ |
1951 | ␊ |
1952 | void *␊ |
1953 | GetBundlePersonality( char * bundle_id )␊ |
1954 | {␊ |
1955 | ModulePtr module;␊ |
1956 | ␉DBG("GetBundlePersonalities %s\n",bundle_id);␊ |
1957 | ␊ |
1958 | module = FindBundle( bundle_id );␊ |
1959 | ␊ |
1960 | if (module != 0)␊ |
1961 | {␊ |
1962 | return (void *)module->personalities;␊ |
1963 | }␊ |
1964 | ␊ |
1965 | return 0;␊ |
1966 | }␊ |
1967 | ␊ |
1968 | //==========================================================================␊ |
1969 | // GetBundlePath␊ |
1970 | ␊ |
1971 | char *␊ |
1972 | GetBundlePath( char * bundle_id )␊ |
1973 | {␊ |
1974 | ModulePtr module;␊ |
1975 | ␉DBG("GetBundlePath %s\n",bundle_id);␊ |
1976 | ␊ |
1977 | module = FindBundle( bundle_id );␊ |
1978 | ␊ |
1979 | if (module != 0)␊ |
1980 | {␊ |
1981 | return module->bundlePath;␊ |
1982 | }␊ |
1983 | ␊ |
1984 | return 0;␊ |
1985 | }␊ |
1986 | ␊ |
1987 | //==========================================================================␊ |
1988 | // ParseXML␊ |
1989 | ␊ |
1990 | static long␊ |
1991 | ParseXML( char * buffer, ModulePtr * module )␊ |
1992 | {␊ |
1993 | ␉long length, pos;␊ |
1994 | ␉TagPtr moduleDict, prop;␊ |
1995 | ␉ModulePtr tmpModule;␊ |
1996 | ␉␊ |
1997 | pos = 0;␊ |
1998 | ␉DBG("ParseXML\n");␊ |
1999 | ␊ |
2000 | ␉if (!module) {␊ |
2001 | return -1;␊ |
2002 | }␊ |
2003 | ␉␊ |
2004 | while (1)␊ |
2005 | {␊ |
2006 | length = XMLParseNextTag(buffer + pos, &moduleDict);␊ |
2007 | if (length == -1) break;␊ |
2008 | ␉␉␊ |
2009 | pos += length;␊ |
2010 | ␉␉␊ |
2011 | if (moduleDict == 0) continue;␊ |
2012 | if (moduleDict->type == kTagTypeDict) break;␊ |
2013 | ␉␉␊ |
2014 | XMLFreeTag(moduleDict);␊ |
2015 | }␊ |
2016 | ␉␊ |
2017 | if (length == -1) ␊ |
2018 | {␊ |
2019 | return -1;␊ |
2020 | }␊ |
2021 | ␉␊ |
2022 | ␊ |
2023 | tmpModule = malloc(sizeof(Module));␊ |
2024 | if (tmpModule == 0)␊ |
2025 | {␊ |
2026 | XMLFreeTag(moduleDict);␊ |
2027 | return -1;␊ |
2028 | }␊ |
2029 | tmpModule->dict = moduleDict;␊ |
2030 | ␊ |
2031 | do {␊ |
2032 | prop = XMLGetProperty(moduleDict, kPropOSBundleEnabled);␊ |
2033 | if ((prop != 0) && prop->string)␊ |
2034 | {␊ |
2035 | if ( (strlen(prop->string) >= 1) && (prop->string[0] == 'N' || prop->string[0] == 'n') )␊ |
2036 | {␊ |
2037 | tmpModule->willLoad = 0;␊ |
2038 | break;␊ |
2039 | }␊ |
2040 | }␊ |
2041 | prop = XMLGetProperty(moduleDict, kPropNSPrincipalClass);␊ |
2042 | if ((prop != 0) && prop->string)␊ |
2043 | {␊ |
2044 | if (!strcmp(prop->string,SYSLIB_CLASS)) ␊ |
2045 | {␊ |
2046 | tmpModule->willLoad = BundlePrioritySystemLib;␊ |
2047 | break;␊ |
2048 | }␊ |
2049 | if (!strcmp(prop->string,SYS_CLASS)) ␊ |
2050 | {␊ |
2051 | tmpModule->willLoad = BundlePrioritySystem;␊ |
2052 | break;␊ |
2053 | }␊ |
2054 | } ␊ |
2055 | ␊ |
2056 | prop = XMLGetProperty(moduleDict, kPropOSBundlePriority);␊ |
2057 | if ((prop != 0) && prop->string)␊ |
2058 | {␊ |
2059 | int tmpwillLoad;␊ |
2060 | if ((tmpwillLoad = strtoul(prop->string, NULL, 10)) > BundlePrioritySystemLib )␊ |
2061 | { ␊ |
2062 | tmpModule->willLoad = MIN(tmpwillLoad, BundlePriorityLowestPriority);␊ |
2063 | break;␊ |
2064 | ␊ |
2065 | }␊ |
2066 | ␊ |
2067 | } ␊ |
2068 | ␊ |
2069 | tmpModule->willLoad = BundlePriorityNormalPriority;␊ |
2070 | ␊ |
2071 | } while (0);␊ |
2072 | ␊ |
2073 | // Get the personalities.␊ |
2074 | tmpModule->personalities = XMLGetProperty(moduleDict, kPropIOKitPersonalities);␊ |
2075 | ␊ |
2076 | *module = tmpModule;␊ |
2077 | ␉␊ |
2078 | ␉␊ |
2079 | ␉␊ |
2080 | return 0;␊ |
2081 | }␊ |
2082 | |