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