Chameleon

Chameleon Commit Details

Date:2010-08-24 15:25:55 (13 years 8 months ago)
Author:Evan Lojewski
Commit:430
Parents: 429
Message:Removed unused code left over from before rebinding. Modified then handel_symtab function so that it can be used within the kernel patcher module. This is untested, other than to make sure it boots.
Changes:
M/branches/meklort/i386/boot2/modules.c
M/branches/meklort/i386/boot2/modules.h

File differences

branches/meklort/i386/boot2/modules.c
296296
297297
298298
299
300
301
302
303299
304300
305301
306302
307303
308
309304
310
311305
312306
313307
......
340334
341335
342336
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396337
397338
398339
399
400
401340
402341
403342
......
441380
442381
443382
444
445
383
384
446385
447
386
448387
449388
450389
......
464403
465404
466405
467
406
468407
469408
470409
......
937876
938877
939878
879
940880
941
881
942882
943883
944884
......
963903
964904
965905
906
907
908
909
910
911
912
913
914
966915
967916
968917
......
10681017
10691018
10701019
1071
1020
10721021
10731022
10741023
......
10821031
10831032
10841033
1085
1034
1035
10861036
1087
1088
1089
1090
1091
1092
1093
1094
1095
1037
1038
1039
10961040
10971041
10981042
UInt32 moduleVersion = 0;
UInt32 moduleCompat = 0;
char* symbolStub = NULL;
char* nonlazy = NULL;
//char* nonlazy_variables = NULL;
// TODO convert all of the structs to a union
struct load_command *loadCommand = NULL;
struct dylib_command* dylibCommand = NULL;
struct dyld_info_command* dyldInfoCommand = NULL;
struct symtab_command* symtabCommand = NULL;
struct segment_command* segmentCommand = NULL;
//struct dysymtab_command* dysymtabCommand = NULL;
struct section* section = NULL;
UInt32 binaryIndex = sizeof(struct mach_header);
UInt16 cmd = 0;
symtabCommand = binary + binaryIndex;
break;
case LC_SEGMENT:
segmentCommand = binary + binaryIndex;
UInt32 sections = segmentCommand->nsects;
section = binary + binaryIndex + sizeof(struct segment_command);
// Loop untill needed variables are found
while(!(nonlazy && symbolStub) && sections)
{
// Look for some sections and save the addresses
if(strcmp(section->sectname, SECT_NON_LAZY_SYMBOL_PTR) == 0)
{
/*printf("\tSection non lazy pointers at 0x%X, %d symbols\n",
section->offset,
section->size / 4);
*/
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:
// NOTE: Theses currently are not handled.
//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();
break;
}
//getc();
}
else if(strcmp(section->sectname, SECT_SYMBOL_STUBS) == 0)
{
symbolStub = binary + section->offset;
}
/*
else
{
printf("Unhandled section %s\n", section->sectname);
}
*/
sections--;
section++;
}
break;
case LC_DYSYMTAB:
//dysymtabCommand = binary + binaryIndex;
//printf("Unhandled loadcommand LC_DYSYMTAB\n");
break;
case LC_LOAD_DYLIB:
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);
// bind_macho uses the symbols.
module_start = (void*)handle_symtable((UInt32)binary, symtabCommand, &add_symbol);
// REbase the module before binding it.
// Rebase the module before binding it.
if(dyldInfoCommand && dyldInfoCommand->rebase_off)
{
rebase_macho(binary, (char*)dyldInfoCommand->rebase_off, dyldInfoCommand->rebase_size);
if(dyldInfoCommand && dyldInfoCommand->lazy_bind_off)
{
// NOTE: we are binding the lazy pointers as a module is laoded,
// This should be cahnged to bund when a symbol is referened at runtime.
// This should be changed to bind when a symbol is referened at runtime instead.
bind_macho(binary, (char*)dyldInfoCommand->lazy_bind_off, dyldInfoCommand->lazy_bind_size);
}
* This function adds a symbol from a module to the list of known symbols
* possibly change to a pointer and add this to the Symbol module so that it can
* adjust it's internal symbol list (sort) to optimize locating new symbols
* NOTE: returns the address if the symbol is "start", else returns 0xFFFFFFFF
*/
void add_symbol(char* symbol, void* addr)
void* add_symbol(char* symbol, void* addr)
{
symbolList_t* entry;
//DBG("Adding symbol %s at 0x%X\n", symbol, addr);
entry->next = NULL;
entry->addr = (unsigned int)addr;
entry->symbol = symbol;
if(strcmp(symbol, "start") == 0)
{
return addr;
}
else
{
return (void*)0xFFFFFFFF;
}
}
* Lookup any undefined symbols
*/
unsigned int handle_symtable(UInt32 base, struct symtab_command* symtabCommand, char* symbolStub, UInt32 nonlazy)
unsigned int handle_symtable(UInt32 base, struct symtab_command* symtabCommand, void*(*symbol_handler)(char*, void*))
{
unsigned int module_start = 0xFFFFFFFF;
struct nlist* symbolEntry = (void*)base + symtabCommand->symoff + (symbolIndex * sizeof(struct nlist));
// If the symbol is exported by this module
if(symbolEntry->n_value)
if(symbolEntry->n_value &&
symbol_handler(symbolString + symbolEntry->n_un.n_strx, (void*)base + symbolEntry->n_value) != (void*)0xFFFFFFFF)
{
if(strcmp(symbolString + symbolEntry->n_un.n_strx, "start") == 0)
{
// Module start located. Start is an alias so don't register it
module_start = base + symbolEntry->n_value;
}
else
{
add_symbol(symbolString + symbolEntry->n_un.n_strx, (void*)base + symbolEntry->n_value);
}
// Module start located. Start is an alias so don't register it
module_start = base + symbolEntry->n_value;
}
symbolEntry+= sizeof(struct nlist);
branches/meklort/i386/boot2/modules.h
7777
7878
7979
80
80
81
8182
8283
83
84
84
85
86
87
8588
8689
8790
int is_module_laoded(const char* name);
void module_loaded(char* name, UInt32 version, UInt32 compat);
void add_symbol(char* symbol, void* addr);
void* add_symbol(char* symbol, void* addr);
void* parse_mach(void* binary);
unsigned int handle_symtable(UInt32 base, struct symtab_command* symtabCommand, char* symbolStub, UInt32 nonlazy);
unsigned int handle_symtable(UInt32 base,
struct symtab_command* symtabCommand,
void*(*symbol_handler)(char*, void*));
unsigned int lookup_all_symbols(const char* name);
extern unsigned int (*lookup_symbol)(const char*);

Archive Download the corresponding diff file

Revision: 430