Chameleon

Chameleon Svn Source Tree

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

Archive Download this file

Revision: 2093