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