Chameleon

Chameleon Svn Source Tree

Root/branches/Chimera/i386/boot2/modules.c

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

Archive Download this file

Revision: 2391