Chameleon

Chameleon Svn Source Tree

Root/branches/azimutz/trunkGraphicsEnablerModules/i386/boot2/modules.c

1/*
2 * Copyright 2010 Evan Lojewski. All rights reserved.
3 *
4 */
5#ifdef CONFIG_MODULES
6#ifndef CONFIG_MODULE_DEBUG
7#define CONFIG_MODULE_DEBUG 0
8#endif
9
10#include "boot.h"
11#include "bootstruct.h"
12#include "modules.h"
13#include "boot_modules.h"
14#include <vers.h>
15
16
17#if CONFIG_MODULE_DEBUG
18#define DBG(x...)printf(x);
19#define DBGPAUSE()getchar()
20#else
21#define DBG(x...)
22#define DBGPAUSE()
23#endif
24
25// NOTE: Global so that modules can link with this
26UInt64 textAddress = 0;
27UInt64 textSection = 0;
28
29void* symbols_module_start = (void*)0xFFFFFFFF;// Global, value is populated by the makefile with actual address
30
31/** Internal symbols, however there are accessor methods **/
32moduleHook_t* moduleCallbacks = NULL;
33moduleList_t* loadedModules = NULL;
34symbolList_t* moduleSymbols = NULL;
35unsigned int (*lookup_symbol)(const char*) = NULL;
36
37
38/*
39 * Initialize the module system by loading the Symbols.dylib module.
40 * Once loaded, locate the _lookup_symbol function so that internal
41 * symbols can be resolved.
42 */
43int init_module_system()
44{
45 // Start any modules that were compiled in first.
46 start_built_in_modules();
47
48
49int retVal = 0;
50void (*module_start)(void) = NULL;
51char* module_data = symbols_module_start + BOOT2_ADDR;
52
53// Intialize module system
54if(symbols_module_start != (void*)0xFFFFFFFF)
55{
56// Module system was compiled in (Symbols.dylib addr known)
57module_start = parse_mach(module_data, &load_module, &add_symbol, NULL);
58
59if(module_start && module_start != (void*)0xFFFFFFFF)
60{
61// Notify the system that it was laoded
62module_loaded(SYMBOLS_MODULE, SYMBOLS_AUTHOR, SYMBOLS_DESCRIPTION, SYMBOLS_VERSION, SYMBOLS_COMPAT);
63
64(*module_start)();// Start the module. This will point to load_all_modules due to the way the dylib was constructed.
65execute_hook("ModulesLoaded", NULL, NULL, NULL, NULL);
66DBG("Module %s Loaded.\n", SYMBOLS_MODULE);
67retVal = 1;
68
69}
70else
71{
72 module_data -= 0x10; // XCODE 4 HACK
73 module_start = parse_mach(module_data, &load_module, &add_symbol, NULL);
74
75 if(module_start && module_start != (void*)0xFFFFFFFF)
76 {
77 // Notify the system that it was laoded
78 module_loaded(SYMBOLS_MODULE, SYMBOLS_AUTHOR, SYMBOLS_DESCRIPTION, SYMBOLS_VERSION, SYMBOLS_COMPAT);
79
80 (*module_start)();// Start the module. This will point to load_all_modules due to the way the dylib was constructed.
81 execute_hook("ModulesLoaded", NULL, NULL, NULL, NULL);
82 DBG("Module %s Loaded.\n", SYMBOLS_MODULE);
83 retVal = 1;
84
85 }
86 else
87 {
88 // The module does not have a valid start function
89 printf("Unable to start %s\n", SYMBOLS_MODULE); getchar();
90 }
91}
92}
93return retVal;
94}
95
96void start_built_in_module(const char* name,
97 const char* author,
98 const char* description,
99 UInt32 version,
100 UInt32 compat,
101 void(*start_function)(void))
102{
103 start_function();
104 // Notify the module system that this module really exists, specificaly, let other module link with it
105 module_loaded(name, author, description, version, compat);
106}
107
108
109/*
110 * Load all modules in the /Extra/modules/ directory
111 * Module depencdies will be loaded first
112 * Modules will only be loaded once. When loaded a module must
113 * setup apropriete function calls and hooks as required.
114 * NOTE: To ensure a module loads after another you may
115 * link one module with the other. For dyld to allow this, you must
116 * reference at least one symbol within the module.
117 */
118void load_all_modules()
119{
120char* name;
121long flags;
122long time;
123struct dirstuff* moduleDir = opendir("/Extra/modules/");
124while(readdir(moduleDir, (const char**)&name, &flags, &time) >= 0)
125{
126if(strcmp(&name[strlen(name) - sizeof("dylib")], ".dylib") == 0)
127{
128char* tmp = malloc(strlen(name) + 1);
129strcpy(tmp, name);
130
131if(!load_module(tmp))
132{
133// failed to load
134// free(tmp);
135}
136}
137else
138{
139DBG("Ignoring %s\n", name);
140}
141
142}
143}
144
145
146/*
147 * Load a module file in /Extra/modules/
148 */
149int load_module(char* module)
150{
151int retVal = 1;
152void (*module_start)(void) = NULL;
153char modString[128];
154int fh = -1;
155
156// Check to see if the module has already been loaded
157if(is_module_loaded(module))
158{
159return 1;
160}
161
162sprintf(modString, MODULE_PATH "%s", module);
163fh = open(modString, 0);
164if(fh < 0)
165{
166DBG("WARNING: Unable to locate module %s\n", modString); DBGPAUSE();
167return 0;
168}
169
170unsigned int moduleSize = file_size(fh);
171char* module_base = (char*) malloc(moduleSize);
172if (moduleSize && read(fh, module_base, moduleSize) == moduleSize)
173{
174// Module loaded into memory, parse it
175module_start = parse_mach(module_base, &load_module, &add_symbol, NULL);
176
177if(module_start && module_start != (void*)0xFFFFFFFF)
178{
179// Notify the system that it was laoded
180module_loaded(module, NULL, NULL, 0, 0 /*moduleName, moduleVersion, moduleCompat*/);
181(*module_start)();// Start the module
182DBG("Module %s Loaded.\n", module); DBGPAUSE();
183}
184#if CONFIG_MODULE_DEBUG
185else // The module does not have a valid start function. This may be a library.
186{
187printf("WARNING: Unable to start %s\n", module);
188getchar();
189}
190#else
191else msglog("WARNING: Unable to start %s\n", module);
192#endif
193}
194else
195{
196DBG("Unable to read in module %s\n.", module); DBGPAUSE();
197retVal = 0;
198}
199
200close(fh);
201return retVal;
202}
203
204/*
205 * add_symbol
206 * This function adds a symbol from a module to the list of known symbols
207 * possibly change to a pointer and add this to the Symbol module so that it can
208 * adjust it's internal symbol list (sort) to optimize locating new symbols
209 * NOTE: returns the address if the symbol is "start", else returns 0xFFFFFFFF
210 */
211long long add_symbol(char* symbol, long long addr, char is64)
212{
213// This only can handle 32bit symbols
214symbolList_t* entry;
215//DBG("Adding symbol %s at 0x%X\n", symbol, addr);
216
217entry = malloc(sizeof(symbolList_t));
218entry->next = moduleSymbols;
219moduleSymbols = entry;
220
221entry->addr = (UInt32)addr;
222entry->symbol = symbol;
223
224if(!is64 && strcmp(symbol, "start") == 0)
225{
226return addr;
227}
228else
229{
230return 0xFFFFFFFF; // fixme
231}
232}
233
234
235/*
236 * print out the information about the loaded module
237 */
238void module_loaded(const char* name, const char* author, const char* description, UInt32 version, UInt32 compat)
239{
240moduleList_t* new_entry = malloc(sizeof(moduleList_t));
241new_entry->next = loadedModules;
242
243loadedModules = new_entry;
244
245 if(!name) name = "Unknown";
246 if(!author) author = "Unknown";
247 if(!description) description = "";
248
249new_entry->name = name;
250 new_entry->author = author;
251 new_entry->description = description;
252new_entry->version = version;
253 new_entry->compat = compat;
254
255 msglog("Module '%s' by '%s' Loaded.\n", name, author);
256 msglog("\tDescription: %s\n", description);
257 msglog("\tVersion: %d\n", version); // todo: sperate to major.minor.bugfix
258 msglog("\tCompat: %d\n", compat); // todo: ^^^ major.minor.bugfix
259}
260
261int is_module_loaded(const char* name)
262{
263// todo sorted search
264moduleList_t* entry = loadedModules;
265while(entry)
266{
267if(strcmp(entry->name, name) == 0)
268{
269DBG("Located module %s\n", name); DBGPAUSE();
270return 1;
271}
272else
273{
274entry = entry->next;
275}
276
277}
278
279DBG("Module %s not found\n", name); DBGPAUSE();
280return 0;
281}
282
283/*
284 *lookup symbols in all loaded modules. Thins inludes boot syms due to Symbols.dylib construction
285 *
286 */
287unsigned int lookup_all_symbols(const char* name)
288{
289symbolList_t* entry = moduleSymbols;
290while(entry)
291{
292if(strcmp(entry->symbol, name) == 0)
293{
294//DBG("External symbol %s located at 0x%X\n", name, entry->addr);
295return entry->addr;
296}
297else
298{
299entry = entry->next;
300}
301}
302
303#if CONFIG_MODULE_DEBUG
304printf("Unable to locate symbol %s\n", name);
305getchar();
306#endif
307
308if(strcmp(name, VOID_SYMBOL) == 0) return 0xFFFFFFFF;
309// In the event that a symbol does not exist
310// Return a pointer to a void function.
311else return lookup_all_symbols(VOID_SYMBOL);
312}
313
314/********************************************************************************/
315/*Macho Parser*/
316/********************************************************************************/
317
318/*
319 * Parse through a macho module. The module will be rebased and binded
320 * as specified in the macho header. If the module is sucessfuly laoded
321 * the module iinit address will be returned.
322 * NOTE; all dependecies will be loaded before this module is started
323 * NOTE: If the module is unable to load ot completeion, the modules
324 * symbols will still be available.
325 */
326void* parse_mach(void* binary,
327 int(*dylib_loader)(char*),
328 long long(*symbol_handler)(char*, long long, char),
329 void (*section_handler)(char* section, char* segment, long long offset, long long address)
330)
331{
332char is64 = false;
333void (*module_start)(void) = NULL;
334
335// Module info
336/*char* moduleName = NULL;
337 UInt32 moduleVersion = 0;
338 UInt32 moduleCompat = 0;
339 */
340// TODO convert all of the structs to a union
341struct load_command *loadCommand = NULL;
342struct dylib_command* dylibCommand = NULL;
343struct dyld_info_command* dyldInfoCommand = NULL;
344
345struct symtab_command* symtabCommand = NULL;
346struct segment_command *segCommand = NULL;
347struct segment_command_64 *segCommand64 = NULL;
348
349//struct dysymtab_command* dysymtabCommand = NULL;
350UInt32 binaryIndex = 0;
351UInt16 cmd = 0;
352
353textSection = 0;
354textAddress = 0;// reinitialize text location in case it doesn't exist;
355
356// Parse through the load commands
357if(((struct mach_header*)binary)->magic == MH_MAGIC)
358{
359is64 = false;
360binaryIndex += sizeof(struct mach_header);
361}
362else if(((struct mach_header_64*)binary)->magic == MH_MAGIC_64)
363{
364// NOTE: modules cannot be 64bit...
365is64 = true;
366binaryIndex += sizeof(struct mach_header_64);
367}
368else
369{
370verbose("Invalid mach magic 0x%X\n", ((struct mach_header*)binary)->magic);
371//getchar();
372return NULL;
373}
374
375
376
377/*if(((struct mach_header*)binary)->filetype != MH_DYLIB)
378 {
379 printf("Module is not a dylib. Unable to load.\n");
380 getchar();
381 return NULL; // Module is in the incorrect format
382 }*/
383
384while(cmd < ((struct mach_header*)binary)->ncmds)
385{
386cmd++;
387
388loadCommand = binary + binaryIndex;
389UInt32 cmdSize = loadCommand->cmdsize;
390
391
392switch ((loadCommand->cmd & 0x7FFFFFFF))
393{
394case LC_SYMTAB:
395symtabCommand = binary + binaryIndex;
396break;
397
398case LC_SEGMENT: // 32bit macho
399 {
400 segCommand = binary + binaryIndex;
401
402 UInt32 sectionIndex;
403
404 sectionIndex = sizeof(struct segment_command);
405
406 struct section *sect;
407
408 while(sectionIndex < segCommand->cmdsize)
409 {
410 sect = binary + binaryIndex + sectionIndex;
411
412 sectionIndex += sizeof(struct section);
413
414 if(section_handler) section_handler(sect->sectname, segCommand->segname, sect->offset, sect->addr);
415
416
417
418 if((strcmp("__TEXT", segCommand->segname) == 0) && (strcmp("__text", sect->sectname) == 0))
419 {
420 // __TEXT,__text found, save the offset and address for when looking for the calls.
421 textSection = sect->offset;
422 textAddress = sect->addr;
423 }
424 }
425 }
426break;
427case LC_SEGMENT_64:// 64bit macho's
428 {
429 segCommand64 = binary + binaryIndex;
430 UInt32 sectionIndex;
431
432 sectionIndex = sizeof(struct segment_command_64);
433
434 struct section_64 *sect;
435
436 while(sectionIndex < segCommand64->cmdsize)
437 {
438 sect = binary + binaryIndex + sectionIndex;
439
440 sectionIndex += sizeof(struct section_64);
441
442 if(section_handler) section_handler(sect->sectname, segCommand->segname, sect->offset, sect->addr);
443
444
445 if((strcmp("__TEXT", segCommand->segname) == 0) && (strcmp("__text", sect->sectname) == 0))
446 {
447 // __TEXT,__text found, save the offset and address for when looking for the calls.
448 textSection = sect->offset;
449 textAddress = sect->addr;
450 }
451 }
452}
453break;
454
455
456case LC_LOAD_DYLIB:
457case LC_LOAD_WEAK_DYLIB ^ LC_REQ_DYLD:
458dylibCommand = binary + binaryIndex;
459char* module = binary + binaryIndex + ((UInt32)*((UInt32*)&dylibCommand->dylib.name));
460// Possible enhancments: verify version
461// =dylibCommand->dylib.current_version;
462// =dylibCommand->dylib.compatibility_version;
463if(dylib_loader)
464{
465char* name = malloc(strlen(module) + strlen(".dylib") + 1);
466sprintf(name, "%s.dylib", module);
467
468if (!dylib_loader(name))
469{
470// NOTE: any symbols exported by dep will be replace with the void function
471free(name);
472}
473}
474
475break;
476
477case LC_ID_DYLIB:
478dylibCommand = binary + binaryIndex;
479/*moduleName =binary + binaryIndex + ((UInt32)*((UInt32*)&dylibCommand->dylib.name));
480 moduleVersion =dylibCommand->dylib.current_version;
481 moduleCompat =dylibCommand->dylib.compatibility_version;
482 */
483break;
484
485case LC_DYLD_INFO:
486//case LC_DYLD_INFO_ONLY:// compressed info, 10.6+ macho files, already handeled
487// Bind and rebase info is stored here
488dyldInfoCommand = binary + binaryIndex;
489break;
490
491case LC_DYSYMTAB:
492case LC_UUID:
493case LC_UNIXTHREAD:
494break;
495
496default:
497DBG("Unhandled loadcommand 0x%X\n", loadCommand->cmd & 0x7FFFFFFF);
498break;
499
500}
501
502binaryIndex += cmdSize;
503}
504
505// bind_macho uses the symbols, if the textAdd does not exist (Symbols.dylib, no code), addresses are static and not relative
506module_start = (void*)handle_symtable((UInt32)binary, symtabCommand, symbol_handler, is64);
507
508if(dyldInfoCommand)
509{
510// Rebase the module before binding it.
511if(dyldInfoCommand->rebase_off)rebase_macho(binary, (char*)dyldInfoCommand->rebase_off,dyldInfoCommand->rebase_size);
512// Bind all symbols.
513if(dyldInfoCommand->bind_off)bind_macho(binary, (UInt8*)dyldInfoCommand->bind_off,dyldInfoCommand->bind_size);
514if(dyldInfoCommand->weak_bind_off)bind_macho(binary, (UInt8*)dyldInfoCommand->weak_bind_off,dyldInfoCommand->weak_bind_size);
515if(dyldInfoCommand->lazy_bind_off)bind_macho(binary, (UInt8*)dyldInfoCommand->lazy_bind_off,dyldInfoCommand->lazy_bind_size);
516}
517
518return module_start;
519
520}
521
522/*
523 * parse the symbol table
524 * Lookup any undefined symbols
525 */
526
527unsigned int handle_symtable(UInt32 base, struct symtab_command* symtabCommand, long long(*symbol_handler)(char*, long long, char), char is64)
528{
529unsigned int module_start= 0xFFFFFFFF;
530UInt32 symbolIndex= 0;
531char* symbolString= base + (char*)symtabCommand->stroff;
532
533if(!is64)
534{
535struct nlist* symbolEntry = (void*)base + symtabCommand->symoff;
536while(symbolIndex < symtabCommand->nsyms)
537{
538// If the symbol is exported by this module
539if(symbolEntry->n_value &&
540 symbol_handler(symbolString + symbolEntry->n_un.n_strx, textAddress ? (long long)base + symbolEntry->n_value : symbolEntry->n_value, is64) != 0xFFFFFFFF)
541{
542
543// Module start located. Start is an alias so don't register it
544module_start = textAddress ? base + symbolEntry->n_value : symbolEntry->n_value;
545}
546
547symbolEntry++;
548symbolIndex++;// TODO remove
549}
550}
551else
552{
553struct nlist_64* symbolEntry = (void*)base + symtabCommand->symoff;
554// NOTE First entry is *not* correct, but we can ignore it (i'm getting radar:// right now, verify later)
555while(symbolIndex < symtabCommand->nsyms)
556{
557
558
559// If the symbol is exported by this module
560if(symbolEntry->n_value &&
561 symbol_handler(symbolString + symbolEntry->n_un.n_strx, textAddress ? (long long)base + symbolEntry->n_value : symbolEntry->n_value, is64) != 0xFFFFFFFF)
562{
563
564// Module start located. Start is an alias so don't register it
565module_start = textAddress ? base + symbolEntry->n_value : symbolEntry->n_value;
566}
567
568symbolEntry++;
569symbolIndex++;// TODO remove
570}
571}
572return module_start;
573}
574
575// Based on code from dylibinfo.cpp and ImageLoaderMachOCompressed.cpp
576void rebase_macho(void* base, char* rebase_stream, UInt32 size)
577{
578rebase_stream += (UInt32)base;
579
580UInt8 immediate = 0;
581UInt8 opcode = 0;
582UInt8 type = 0;
583UInt32 segmentAddress = 0;
584
585
586UInt32 tmp = 0;
587UInt32 tmp2 = 0;
588UInt8 bits = 0;
589int index = 0;
590unsigned int i = 0;
591
592while(i < size)
593{
594immediate = rebase_stream[i] & REBASE_IMMEDIATE_MASK;
595opcode = rebase_stream[i] & REBASE_OPCODE_MASK;
596
597
598switch(opcode)
599{
600case REBASE_OPCODE_DONE:
601// Rebase complete, reset vars
602immediate = 0;
603opcode = 0;
604type = 0;
605segmentAddress = 0;
606default:
607break;
608
609
610case REBASE_OPCODE_SET_TYPE_IMM:
611type = immediate;
612break;
613
614
615case REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB:
616// Locate address to begin rebasing
617segmentAddress = 0;
618struct segment_command* segCommand = NULL; // NOTE: 32bit only
619
620unsigned int binIndex = 0;
621index = 0;
622do
623{
624segCommand = base + sizeof(struct mach_header) + binIndex;
625
626
627binIndex += segCommand->cmdsize;
628index++;
629}
630while(index <= immediate);
631
632
633segmentAddress = segCommand->fileoff;
634
635tmp = 0;
636bits = 0;
637do
638{
639tmp |= (rebase_stream[++i] & 0x7f) << bits;
640bits += 7;
641}
642while(rebase_stream[i] & 0x80);
643
644segmentAddress += tmp;
645break;
646
647
648case REBASE_OPCODE_ADD_ADDR_ULEB:
649// Add value to rebase address
650tmp = 0;
651bits = 0;
652do
653{
654tmp <<= bits;
655tmp |= rebase_stream[++i] & 0x7f;
656bits += 7;
657}
658while(rebase_stream[i] & 0x80);
659
660segmentAddress +=tmp;
661break;
662
663case REBASE_OPCODE_ADD_ADDR_IMM_SCALED:
664segmentAddress += immediate * sizeof(void*);
665break;
666
667
668case REBASE_OPCODE_DO_REBASE_IMM_TIMES:
669index = 0;
670for (index = 0; index < immediate; ++index) {
671rebase_location(base + segmentAddress, (char*)base, type);
672segmentAddress += sizeof(void*);
673}
674break;
675
676
677case REBASE_OPCODE_DO_REBASE_ULEB_TIMES:
678tmp = 0;
679bits = 0;
680do
681{
682tmp |= (rebase_stream[++i] & 0x7f) << bits;
683bits += 7;
684}
685while(rebase_stream[i] & 0x80);
686
687index = 0;
688for (index = 0; index < tmp; ++index) {
689//DBG("\tRebasing 0x%X\n", segmentAddress);
690rebase_location(base + segmentAddress, (char*)base, type);
691segmentAddress += sizeof(void*);
692}
693break;
694
695case REBASE_OPCODE_DO_REBASE_ADD_ADDR_ULEB:
696tmp = 0;
697bits = 0;
698do
699{
700tmp |= (rebase_stream[++i] & 0x7f) << bits;
701bits += 7;
702}
703while(rebase_stream[i] & 0x80);
704
705rebase_location(base + segmentAddress, (char*)base, type);
706
707segmentAddress += tmp + sizeof(void*);
708break;
709
710case REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_ULEB:
711tmp = 0;
712bits = 0;
713do
714{
715tmp |= (rebase_stream[++i] & 0x7f) << bits;
716bits += 7;
717}
718while(rebase_stream[i] & 0x80);
719
720
721tmp2 = 0;
722bits = 0;
723do
724{
725tmp2 |= (rebase_stream[++i] & 0x7f) << bits;
726bits += 7;
727}
728while(rebase_stream[i] & 0x80);
729
730index = 0;
731for (index = 0; index < tmp; ++index) {
732
733rebase_location(base + segmentAddress, (char*)base, type);
734
735segmentAddress += tmp2 + sizeof(void*);
736}
737break;
738}
739i++;
740}
741}
742
743inline void rebase_location(UInt32* location, char* base, int type)
744{
745switch(type)
746{
747case REBASE_TYPE_POINTER:
748case REBASE_TYPE_TEXT_ABSOLUTE32:
749*location += (UInt32)base;
750break;
751
752default:
753break;
754}
755}
756
757
758UInt32 read_uleb(UInt8* bind_stream, unsigned int* i)
759{
760 // Read in offset
761 UInt32 tmp = 0;
762 UInt8 bits = 0;
763 do
764 {
765 if(bits < sizeof(UInt32)*8) // hack
766 {
767 tmp |= (bind_stream[++(*i)] & 0x7f) << bits;
768 bits += 7;
769 }
770 else
771 {
772 ++(*i);
773 }
774 }
775 while(bind_stream[*i] & 0x80);
776 return tmp;
777}
778
779
780// Based on code from dylibinfo.cpp and ImageLoaderMachOCompressed.cpp
781// NOTE: this uses 32bit values, and not 64bit values.
782// There is a possibility that this could cause issues,
783// however the modules are 32 bits, so it shouldn't matter too much
784void bind_macho(void* base, UInt8* bind_stream, UInt32 size)
785{
786bind_stream += (UInt32)base;
787
788UInt8 immediate = 0;
789UInt8 opcode = 0;
790UInt8 type = BIND_TYPE_POINTER;
791
792UInt32 segmentAddress = 0;
793
794UInt32 address = 0;
795
796SInt32 addend = 0;
797SInt32 libraryOrdinal = 0;
798
799const char* symbolName = NULL;
800UInt8 symboFlags = 0;
801UInt32 symbolAddr = 0xFFFFFFFF;
802
803// Temperary variables
804UInt32 tmp = 0;
805UInt32 tmp2 = 0;
806UInt32 index = 0;
807unsigned int i = 0;
808
809while(i < size)
810{
811immediate = bind_stream[i] & BIND_IMMEDIATE_MASK;
812opcode = bind_stream[i] & BIND_OPCODE_MASK;
813
814
815switch(opcode)
816{
817case BIND_OPCODE_DONE:
818// reset vars
819type = BIND_TYPE_POINTER;
820segmentAddress = 0;
821address = 0;
822addend = 0;
823libraryOrdinal = 0;
824symbolAddr = 0xFFFFFFFF;
825default:
826break;
827
828case BIND_OPCODE_SET_DYLIB_ORDINAL_IMM:
829libraryOrdinal = immediate;
830break;
831
832case BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB:
833libraryOrdinal = read_uleb(bind_stream, &i);
834break;
835
836case BIND_OPCODE_SET_DYLIB_SPECIAL_IMM:
837libraryOrdinal = immediate ? (SInt8)(BIND_OPCODE_MASK | immediate) : immediate;
838break;
839
840case BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM:
841symboFlags = immediate;
842symbolName = (char*)&bind_stream[++i];
843i += strlen((char*)&bind_stream[i]);
844
845symbolAddr = lookup_all_symbols(symbolName);
846break;
847
848case BIND_OPCODE_SET_TYPE_IMM:
849type = immediate;
850break;
851
852case BIND_OPCODE_SET_ADDEND_SLEB:
853addend = read_uleb(bind_stream, &i);
854if(!(bind_stream[i-1] & 0x40)) addend *= -1;
855break;
856
857case BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB:
858segmentAddress = 0;
859
860// Locate address
861struct segment_command* segCommand = NULL;// NOTE: 32bit only
862
863unsigned int binIndex = 0;
864index = 0;
865do
866{
867segCommand = base + sizeof(struct mach_header) + binIndex;
868binIndex += segCommand->cmdsize;
869index++;
870}
871while(index <= immediate);
872
873segmentAddress = segCommand->fileoff;
874
875segmentAddress += read_uleb(bind_stream, &i);
876break;
877
878case BIND_OPCODE_ADD_ADDR_ULEB:
879segmentAddress += read_uleb(bind_stream, &i);
880break;
881
882case BIND_OPCODE_DO_BIND:
883if(symbolAddr != 0xFFFFFFFF)
884{
885address = segmentAddress + (UInt32)base;
886
887bind_location((UInt32*)address, (char*)symbolAddr, addend, type);
888}
889else
890{
891printf("Unable to bind symbol %s\n", symbolName);
892getchar();
893}
894
895segmentAddress += sizeof(void*);
896break;
897
898case BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB:
899// Read in offset
900tmp = read_uleb(bind_stream, &i);
901
902if(symbolAddr != 0xFFFFFFFF)
903{
904address = segmentAddress + (UInt32)base;
905
906bind_location((UInt32*)address, (char*)symbolAddr, addend, type);
907}
908else
909{
910printf("Unable to bind symbol %s\n", symbolName);
911getchar();
912}
913
914segmentAddress += tmp + sizeof(void*);
915
916
917break;
918
919case BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED:
920if(symbolAddr != 0xFFFFFFFF)
921{
922address = segmentAddress + (UInt32)base;
923
924bind_location((UInt32*)address, (char*)symbolAddr, addend, type);
925}
926else
927{
928printf("Unable to bind symbol %s\n", symbolName);
929getchar();
930}
931segmentAddress += (immediate * sizeof(void*)) + sizeof(void*);
932
933
934break;
935
936case BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB:
937tmp = read_uleb(bind_stream, &i);
938
939tmp2 = read_uleb(bind_stream, &i);
940
941if(symbolAddr != 0xFFFFFFFF)
942{
943for(index = 0; index < tmp; index++)
944{
945
946address = segmentAddress + (UInt32)base;
947bind_location((UInt32*)address, (char*)symbolAddr, addend, type);
948segmentAddress += tmp2 + sizeof(void*);
949}
950}
951else
952{
953printf("Unable to bind symbol %s\n", symbolName);
954getchar();
955}
956break;
957}
958i++;
959}
960}
961
962
963inline void bind_location(UInt32* location, char* value, UInt32 addend, int type)
964{
965// do actual update
966char* newValue = value + addend;
967
968switch (type) {
969case BIND_TYPE_POINTER:
970case BIND_TYPE_TEXT_ABSOLUTE32:
971break;
972
973case BIND_TYPE_TEXT_PCREL32:
974newValue -= ((UInt32)location + 4);
975
976break;
977default:
978return;
979}
980//DBG("Binding 0x%X to 0x%X (was 0x%X)\n", location, newValue, *location);
981*location = (UInt32)newValue;
982}
983
984/********************************************************************************/
985/*Module Hook Interface*/
986/********************************************************************************/
987
988/*
989* Locate the symbol for an already loaded function and modify the beginning of
990* the function to jump directly to the new one
991* example: replace_function("_HelloWorld_start", &replacement_start);
992*/
993int replace_function(const char* symbol, void* newAddress)
994{
995UInt32* jumpPointer = malloc(sizeof(UInt32*));
996UInt32 addr = lookup_all_symbols(symbol);
997
998char* binary = (char*)addr;
999if(addr != 0xFFFFFFFF)
1000{
1001//DBG("Replacing %s to point to 0x%x\n", symbol, newAddress);
1002*binary++ = 0xFF;// Jump
1003*binary++ = 0x25;// Long Jump
1004*((UInt32*)binary) = (UInt32)jumpPointer;
1005
1006*jumpPointer = (UInt32)newAddress;
1007return 1;
1008}
1009return 0;
1010}
1011
1012
1013/*
1014 *execute_hook( const char* name )
1015 *name - Name of the module hook
1016 *If any callbacks have been registered for this hook
1017 *they will be executed now in the same order that the
1018 *hooks were added.
1019*/
1020int execute_hook(const char* name, void* arg1, void* arg2, void* arg3, void* arg4)
1021{
1022DBG("Attempting to execute hook '%s'\n", name); DBGPAUSE();
1023moduleHook_t* hook = hook_exists(name);
1024
1025if(hook)
1026{
1027// Loop through all callbacks for this module
1028callbackList_t* callbacks = hook->callbacks;
1029
1030while(callbacks)
1031{
1032// Execute callback
1033callbacks->callback(arg1, arg2, arg3, arg4);
1034callbacks = callbacks->next;
1035}
1036DBG("Hook '%s' executed.\n", name); DBGPAUSE();
1037return 1;
1038}
1039else
1040{
1041// Callback for this hook doesn't exist;
1042DBG("No callbacks for '%s' hook.\n", name);
1043return 0;
1044}
1045}
1046
1047
1048
1049/*
1050 *register_hook_callback( const char* name, void(*callback)())
1051 *name - Name of the module hook to attach to.
1052 *callbacks - The funciton pointer that will be called when the
1053 *hook is executed. When registering a new callback name, the callback is added sorted.
1054 *NOTE: the hooks take four void* arguments.
1055 */
1056void register_hook_callback(const char* name, void(*callback)(void*, void*, void*, void*))
1057{
1058DBG("Adding callback for '%s' hook.\n", name); DBGPAUSE();
1059
1060moduleHook_t* hook = hook_exists(name);
1061
1062if(hook)
1063{
1064// append
1065callbackList_t* newCallback = malloc(sizeof(callbackList_t));
1066newCallback->next = hook->callbacks;
1067hook->callbacks = newCallback;
1068newCallback->callback = callback;
1069}
1070else
1071{
1072// create new hook
1073moduleHook_t* newHook = malloc(sizeof(moduleHook_t));
1074newHook->name = name;
1075newHook->callbacks = malloc(sizeof(callbackList_t));
1076newHook->callbacks->callback = callback;
1077newHook->callbacks->next = NULL;
1078
1079newHook->next = moduleCallbacks;
1080moduleCallbacks = newHook;
1081
1082}
1083
1084#if CONFIG_MODULE_DEBUG
1085//print_hook_list();
1086//getchar();
1087#endif
1088
1089}
1090
1091
1092moduleHook_t* hook_exists(const char* name)
1093{
1094moduleHook_t* hooks = moduleCallbacks;
1095
1096// look for a hook. If it exists, return the moduleHook_t*,
1097// If not, return NULL.
1098while(hooks)
1099{
1100if(strcmp(name, hooks->name) == 0)
1101{
1102//DBG("Located hook %s\n", name);
1103return hooks;
1104}
1105hooks = hooks->next;
1106}
1107//DBG("Hook %s does not exist\n", name);
1108return NULL;
1109
1110}
1111
1112#if CONFIG_MODULE_DEBUG
1113void print_hook_list()
1114{
1115printf("---Hook Table---\n");
1116
1117moduleHook_t* hooks = moduleCallbacks;
1118while(hooks)
1119{
1120printf("Hook: %s\n", hooks->name);
1121hooks = hooks->next;
1122}
1123}
1124
1125#endif
1126
1127/********************************************************************************/
1128/*dyld / Linker Interface*/
1129/********************************************************************************/
1130
1131void dyld_stub_binder()
1132{
1133printf("ERROR: dyld_stub_binder was called, should have been take care of by the linker.\n");
1134getchar();
1135}
1136
1137#else /* CONFIG_MODULES */
1138
1139int init_module_system()
1140{
1141 return 0;
1142}
1143
1144void load_all_modules()
1145{
1146
1147}
1148
1149int execute_hook(const char* name, void* arg1, void* arg2, void* arg3, void* arg4)
1150{
1151 return 0;
1152}
1153#endif

Archive Download this file

Revision: 1114