Chameleon

Chameleon Commit Details

Date:2010-08-12 08:11:58 (13 years 8 months ago)
Author:Evan Lojewski
Commit:355
Parents: 354
Message:Fixed the rebase + bind code, now works properly. Removed a few ugly hacks. KernelPatcher now loads properly (no more reboots).
Changes:
M/branches/meklort/i386/modules/KernelPatcher/kernel_patcher.c
M/branches/meklort/i386/modules/KernelPatcher/Makefile
M/branches/meklort/i386/modules/Symbols/Makefile
M/branches/meklort/i386/boot2/modules.c
M/branches/meklort/i386/modules/HelloWorld/Makefile
M/branches/meklort/i386/boot2/modules.h

File differences

branches/meklort/i386/boot2/modules.c
142142
143143
144144
145
146
145147
148
146149
147150
148151
149
152
153
150154
151155
152156
153157
154
155
158
159
156160
157161
158162
......
165169
166170
167171
172
173
174
175
168176
177
169178
170179
171180
......
229238
230239
231240
241
242
243
244
232245
233
234
246
247
248
249
250
235251
236252
237253
238254
239255
240
256
241257
242
258
243259
244260
245
261
262
263
264
265
246266
247267
268
269
248270
249271
250272
......
360382
361383
362384
385
363386
364
365387
366388
367389
......
384406
385407
386408
409
410
387411
388412
389413
......
404428
405429
406430
431
432
407433
408434
409435
......
435461
436462
437463
464
465
466
438467
439468
440469
......
599628
600629
601630
602
631
603632
604633
605634
......
832861
833862
834863
864
835865
836866
837867
838
839
840
841
842
843868
844
845869
846870
847871
848872
849873
850874
875
851876
852877
853878
......
856881
857882
858883
859
884
885
886
887
860888
861889
862890
......
866894
867895
868896
869
897
870898
871899
872900
......
874902
875903
876904
877
878905
879906
880907
......
893920
894921
895922
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948923
949924
950925
switch(section->flags)
{
case S_NON_LAZY_SYMBOL_POINTERS:
//printf("%s S_NON_LAZY_SYMBOL_POINTERS section\n", SECT_NON_LAZY_SYMBOL_PTR);
//nonlazy_variables = binary + section->offset;
nonlazy = binary + section->offset;
break;
case S_LAZY_SYMBOL_POINTERS:
nonlazy = binary + section->offset;
//nonlazy = binary + section->offset;
//printf("%s S_LAZY_SYMBOL_POINTERS section, 0x%X\n", SECT_NON_LAZY_SYMBOL_PTR, nonlazy);
// Fucntions
break;
default:
printf("Unhandled %s section\n", SECT_NON_LAZY_SYMBOL_PTR);
getc();
//printf("Unhandled %s section\n", SECT_NON_LAZY_SYMBOL_PTR);
//getc();
break;
}
//getc();
symbolStub = binary + section->offset;
//getc();
}
else
{
//printf("Unhandled section %s\n", section->sectname);
}
sections--;
section++;
}
if(!moduleName) return NULL;
// bind_macho uses the symbols added in for some reason...
module_start = (void*)handle_symtable((UInt32)binary, symtabCommand, symbolStub, (UInt32)nonlazy);
// Module is loaded and all module dependencies have been loaded, bind the module
// NOTE: circular dependencies are not handled yet
if(dyldInfoCommand && dyldInfoCommand->rebase_off)
{
rebase_macho(binary, (char*)dyldInfoCommand->rebase_off, dyldInfoCommand->rebase_size);
}
if(dyldInfoCommand && dyldInfoCommand->bind_off)
{
bind_macho(binary, (char*)dyldInfoCommand->bind_off, dyldInfoCommand->bind_size);
}
if(dyldInfoCommand && dyldInfoCommand->rebase_off)
if(dyldInfoCommand && dyldInfoCommand->weak_bind_off)
{
rebase_macho(binary, (char*)dyldInfoCommand->rebase_off, dyldInfoCommand->rebase_size);
bind_macho(binary, (char*)dyldInfoCommand->weak_bind_off, dyldInfoCommand->weak_bind_size);
}
module_start = (void*)handle_symtable((UInt32)binary, symtabCommand, symbolStub, nonlazy);
if(dyldInfoCommand && dyldInfoCommand->lazy_bind_off)
{
bind_macho(binary, (char*)dyldInfoCommand->lazy_bind_off, dyldInfoCommand->lazy_bind_size);
}
// To satisfy cicular deps, the module_loaded command shoudl be run before the module init();
module_loaded(moduleName, moduleVersion, moduleCompat);
UInt32* addr = base + segmentAddress;
addr[0] += (UInt32)base;
//if(type != REBASE_TYPE_POINTER) addr[0] -= 8;
segmentAddress += sizeof(void*);
}
break;
UInt32* addr = base + segmentAddress;
addr[0] += (UInt32)base;
//if(type != REBASE_TYPE_POINTER) addr[0] -= 8;
segmentAddress += sizeof(void*);
}
//printf("\tRebasing 0x%X\n", segmentAddress);
UInt32* addr = base + segmentAddress;
addr[0] += (UInt32)base;
//if(type != REBASE_TYPE_POINTER) addr[0] -= 8;
segmentAddress += tmp + sizeof(void*);
break;
UInt32* addr = base + segmentAddress;
addr[0] += (UInt32)base;
//if(type != REBASE_TYPE_POINTER) addr[0] -= 8;
segmentAddress += tmp2 + sizeof(void*);
}
if(symbolAddr != 0xFFFFFFFF)
{
address = segmentAddress + (UInt32)base;
((char*)address)[0] = (symbolAddr & 0x000000FF) >> 0;
((char*)address)[1] = (symbolAddr & 0x0000FF00) >> 8;
((char*)address)[2] = (symbolAddr & 0x00FF0000) >> 16;
addr = lookup_symbol(name);
if(addr != 0xFFFFFFFF)
{
//printf("Internal symbol %s located at 0x%X\n", name, addr);
return addr;
}
}
/*else
{
printf("Symbol.dylib not loaded. Module loader not setup.\n");
return 0xFFFFFFFF;
}*/
symbolList_t* entry = moduleSymbols;
while(entry)
{
if(strcmp(entry->symbol, name) == 0)
{
//printf("External symbol %s located at 0x%X\n", name, entry->addr);
return entry->addr;
}
else
}
}
if(strcmp(name, SYMBOL_DYLD_STUB_BINDER) != 0)
{
printf("Unable to locate symbol %s\n", name);
}
return 0xFFFFFFFF;
}
* Lookup any undefined symbols
*/
unsigned int handle_symtable(UInt32 base, struct symtab_command* symtabCommand, char* symbolStub, char* nonlazy)
unsigned int handle_symtable(UInt32 base, struct symtab_command* symtabCommand, char* symbolStub, UInt32 nonlazy)
{
unsigned int module_start = 0xFFFFFFFF;
char* symbolString = base + (char*)symtabCommand->stroff;
//char* symbolTable = base + symtabCommand->symoff;
int externalIndex = 0;
while(symbolIndex < symtabCommand->nsyms)
{
add_symbol(symbolString + symbolEntry->n_un.n_strx, (void*)base + symbolEntry->n_value);
}
}
// External symbol
else if(symbolEntry->n_type & 0x01 && symbolStub)
{
printf("Located external symbol %s", symbolString + symbolEntry->n_un.n_strx);
printf(" stub at 0x%X, (0x%X)\n", symbolStub + STUB_ENTRY_SIZE * externalIndex - base, symbolStub + STUB_ENTRY_SIZE * externalIndex);
getc();
// Patch stub
void* symbolAddress = (void*)lookup_all_symbols(symbolString + symbolEntry->n_un.n_strx);
if((0xFFFFFFFF == (UInt32)symbolAddress) &&
strcmp(symbolString + symbolEntry->n_un.n_strx, SYMBOL_DYLD_STUB_BINDER) != 0)
{
printf("Unable to locate symbol %s\n", symbolString + symbolEntry->n_un.n_strx);
}
else
{
char* patchLocation = symbolStub + STUB_ENTRY_SIZE * externalIndex;
//patch with far jump ;
/*
printf("0x%X 0x%X 0x%X 0x%X 0x%X 0x%X\n",
patchLocation[0],
patchLocation[1],
patchLocation[2],
patchLocation[3],
patchLocation[4],
patchLocation[5]);
*/
// Point the symbol stub to the nonlazy pointers
// TODO: do this *after* each module dep has been laoded.
// At the moment, module deps won't work
patchLocation[0] = 0xFF;// Should already be this
patchLocation[1] = 0x25;// Should already be this
patchLocation[2] = ((UInt32)(nonlazy + externalIndex * 4) & 0x000000FF) >> 0;
patchLocation[3] = ((UInt32)(nonlazy + externalIndex * 4) & 0x0000FF00) >> 8;
patchLocation[4] = ((UInt32)(nonlazy + externalIndex * 4) & 0x00FF0000) >> 16;
patchLocation[5] = ((UInt32)(nonlazy + externalIndex * 4) & 0xFF000000) >> 24;
// Set the nonlazy pointer to the correct address
patchLocation = (nonlazy + externalIndex*4);
patchLocation[0] =((UInt32)symbolAddress & 0x000000FF) >> 0;
patchLocation[1] =((UInt32)symbolAddress & 0x0000FF00) >> 8;
patchLocation[2] =((UInt32)symbolAddress & 0x00FF0000) >> 16;
patchLocation[3] =((UInt32)symbolAddress & 0xFF000000) >> 24;
externalIndex++;
}
}
symbolEntry+= sizeof(struct nlist);
symbolIndex++;// TODO remove
branches/meklort/i386/boot2/modules.h
4343
4444
4545
46
46
4747
4848
4949
void add_symbol(char* symbol, void* addr);
void* parse_mach(void* binary);
unsigned int handle_symtable(UInt32 base, struct symtab_command* symtabCommand, char* symbolStub, char* nonlazy);
unsigned int handle_symtable(UInt32 base, struct symtab_command* symtabCommand, char* symbolStub, UInt32 nonlazy);
unsigned int lookup_all_symbols(const char* name);
branches/meklort/i386/modules/KernelPatcher/kernel_patcher.c
77
88
99
10
10
1111
1212
1313
......
4949
5050
5151
52
53
52
53
5454
5555
5656
#include "kernel_patcher.h"
#include "platform.h"
//extern PlatformInfo_t Platform;
extern PlatformInfo_t Platform;
#define SYMBOL_CPUID_SET_INFO0
void KernelPatcher_start()
{
printf("KernelPatcher(), about to call HelloWorld_start()\n");
//getc();
//HelloWorld_start();
getc();
HelloWorld_start();
}
branches/meklort/i386/modules/KernelPatcher/Makefile
2929
3030
3131
32
32
3333
3434
3535
-DRCZ_COMPRESSED_FILE_SUPPORT $(DEBUG) \
-fno-builtin $(OMIT_FRAME_POINTER_CFLAG) \
-mpreferred-stack-boundary=2 -fno-align-functions -fno-stack-protector \
-march=pentium4 -msse2 -mfpmath=sse -msoft-float -fno-common
-march=pentium4 -msse2 -mfpmath=sse -msoft-float -fno-common -mdynamic-no-pic
DEFINES=
CONFIG = hd
branches/meklort/i386/modules/Symbols/Makefile
2222
2323
2424
25
25
2626
2727
2828
-DRCZ_COMPRESSED_FILE_SUPPORT $(DEBUG) \
-fno-builtin $(OMIT_FRAME_POINTER_CFLAG) \
-mpreferred-stack-boundary=2 -fno-align-functions -fno-stack-protector \
-march=pentium4 -msse2 -mfpmath=sse -msoft-float -fno-common
-march=pentium4 -msse2 -mfpmath=sse -msoft-float -fno-common -mdynamic-no-pic
DEFINES=
CONFIG = hd
branches/meklort/i386/modules/HelloWorld/Makefile
2929
3030
3131
32
32
3333
3434
3535
......
6767
6868
6969
70
70
7171
72
7372
7473
75
7674
7775
7876
77
7978
8079
8180
-DRCZ_COMPRESSED_FILE_SUPPORT $(DEBUG) \
-fno-builtin $(OMIT_FRAME_POINTER_CFLAG) \
-mpreferred-stack-boundary=2 -fno-align-functions -fno-stack-protector \
-march=pentium4 -msse2 -mfpmath=sse -msoft-float -fno-common
-march=pentium4 -msse2 -mfpmath=sse -msoft-float -fno-common -mdynamic-no-pic
DEFINES=
CONFIG = hd
-undefined suppress \
-alias $(MODULE_START) start \
-dylib -read_only_relocs suppress \
-S -x -dead_strip_dylibs \
-S -x -Z -dead_strip_dylibs \
-no_uuid \
-bind_at_load \
-current_version $(MODULE_VERSION) -compatibility_version $(MODULE_COMPAT_VERSION) \
-final_output $(MODULE_NAME) \
-weak_library $(SYMROOT)/Symbols.dylib \
$(OBJROOT)/HelloWorld.o -o $(SYMROOT)/$(MODULE_NAME).dylib
HelloWorld.o:
$(CC) $(CPPFLAGS) $(CFLAGS) $(DEFINES) -c "HelloWorld.c" $(INC) -o "$(OBJROOT)/HelloWorld.o"

Archive Download the corresponding diff file

Revision: 355