Changeset 483 for Whitix/branches/hybrid

Show
Ignore:
Timestamp:
05/12/08 12:20:25 (4 months ago)
Author:
mwhitworth
Message:

Finish module code, add support for module symbolic crashes, fix functions.

Location:
Whitix/branches/hybrid/kernel
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • Whitix/branches/hybrid/kernel/load.c

    r479 r483  
    5353 * FUNCTION: ExecOpen 
    5454 * 
    55  * DESCRIPTION: Opens an executable file, and does basic checks for it's 
     55 * DESCRIPTION: Opens an executable file, and does basic checks for its 
    5656 *                              suitability. 
    5757 * 
     
    5959 *                         file - file structure. 
    6060 * 
    61  ***********************************************************************/ 
    62  
    63 static int ExecOpen(char* pathName,struct File* file) 
     61 * RETURNS:             0 if successfully opened, the return value from DoOpenFile 
     62 *                              if it failed to open, or -EISDIR if the path name is a 
     63 *                              directory. 
     64 ***********************************************************************/ 
     65 
     66static int ExecOpen(char* pathName, struct File* file) 
    6467{ 
    6568        int err; 
  • Whitix/branches/hybrid/kernel/module.c

    r481 r483  
    1717 */ 
    1818 
     19#include <config.h> 
    1920#include <console.h> 
    2021#include <elf.h> 
     
    2728#include <slab.h> 
    2829#include <sys.h> 
     30#include <task.h> 
    2931 
    3032LIST_HEAD(moduleList); 
     
    5254 
    5355                /* Handle data? */ 
    54                 return (module->textAddr+symbol->symValue); 
     56                return symbol->symValue; 
    5557        } 
    5658 
    5759        return NULL; 
    5860} 
     61 
     62#ifdef CONFIG_ALL_SYMBOLS 
     63 
     64void ModuleSymbolPrint(DWORD address) 
     65{ 
     66        struct Module* curr, *module=NULL; 
     67 
     68        ListForEachEntry(curr, &moduleList, next) 
     69        { 
     70                if (address >= curr->textAddr && address < curr->textAddr+curr->textLength) 
     71                { 
     72                        module=curr; 
     73                        break; 
     74                } 
     75        } 
     76 
     77        if (module) 
     78        { 
     79                DWORD size, offset; 
     80                const char* SymbolLookup(char*, struct ElfSymbol*, int, DWORD, DWORD*, DWORD*); 
     81 
     82                const char* name=SymbolLookup(module->strTable, module->symTable, module->symTableSize, address, &size, &offset); 
     83 
     84                if (name) 
     85                        printf("%s+%#X/%#X\n", name, offset, size); 
     86                else 
     87                        printf("%#X\n", address); 
     88        }else 
     89                printf("%#X\n", address); 
     90} 
     91 
     92void ModuleSymbolAdd(struct Module* module) 
     93{ 
     94        int length=module->symTableSize+module->strTableSize; 
     95        char* dest=(void*)VirtMapPhysRange(MODULE_START, MODULE_END, PAGE_ALIGN_UP(length) >> PAGE_SHIFT, 3); 
     96 
     97        memcpy(dest, module->symTable, module->symTableSize); 
     98        module->symTable=(struct ElfSymbol*)dest; 
     99 
     100        dest+=module->symTableSize; 
     101        memcpy(dest, module->strTable, module->strTableSize); 
     102        module->strTable=dest;   
     103} 
     104 
     105#else 
     106void ModuleSymbolPrint(DWORD address) 
     107{ 
     108        printf("%#X\n", address); 
     109} 
     110 
     111void ModuleSymbolAdd(struct Module* module) 
     112{ 
     113} 
     114#endif 
    59115 
    60116DWORD ModuleResolveKernel(char* name) 
     
    97153} 
    98154 
    99 int ModuleSymbolResolve(struct Module* module, char* file, int sec) 
     155int ModuleSymbolPrepare(struct Module* module) 
     156{ 
     157        int i; 
     158        DWORD symSize=module->symTableSize/sizeof(struct ElfSymbol); 
     159 
     160        for (i=1; i<symSize; i++) 
     161        { 
     162                struct ElfSymbol* symbol=&module->symTable[i]; 
     163                char* symName=module->strTable+symbol->symName; 
     164 
     165                switch (symbol->symIndex) 
     166                { 
     167                        case STN_UNDEF: 
     168                                symbol->symValue=ModuleResolveKernel(symName); 
     169 
     170                                if (symbol->symValue) 
     171                                        break; 
     172 
     173                                printf("Could not resolve %s\n", symName); 
     174                                return -ENOENT; 
     175 
     176                        /* Absolute symbol. No relocation needed. */ 
     177                        case STN_ABS: 
     178                                break; 
     179 
     180                        case STN_COMMON: 
     181                                printf("Please recompile the module with -fno-common.\n"); 
     182                                return -ENOENT; 
     183 
     184                        default: 
     185                                symbol->symValue+=module->sectionHeaders[symbol->symIndex].shAddr; 
     186                } 
     187        } 
     188 
     189        return 0; 
     190} 
     191 
     192/* TODO: Create a prepare symbols function, then remove symValue manipulation code here. */ 
     193 
     194void ModuleSymbolResolve(struct Module* module, char* file, int sec) 
    100195{ 
    101196        int i; 
     
    107202        { 
    108203                reloc=&relTable[i]; 
    109  
    110204                unsigned long* relAddr=(unsigned long*)(module->sectionHeaders[ module->sectionHeaders[sec].shInfo ].shAddr+reloc->addr); 
    111205                int symTabIdx=ELF_R_SYM(reloc->info); 
    112206                struct ElfSymbol* symbol=&module->symTable[symTabIdx]; 
    113                 char* symName=module->strTable+symbol->symName; 
    114                 DWORD symbolAddr=0; 
    115  
    116                 if (symTabIdx && symName[0] != '.' && strlen(symName) && symbol->symIndex == STN_UNDEF) 
    117                 { 
    118                         symbolAddr=ModuleResolveKernel(symName); 
    119  
    120                         /* Could not resolve symbol address. */ 
    121                         if (!symbolAddr) 
    122                                 return -EINVAL; 
    123                 } 
    124207 
    125208                switch (ELF_R_TYPE(reloc->info)) 
     
    129212 
    130213                        case R_386_32: 
    131                         { 
    132                                 if (symbol->symIndex == STN_UNDEF) 
    133                                         *relAddr+=symbolAddr; 
    134  
    135                                 if (symbol->symIndex > 0 && symbol->symIndex < 0xF000) 
    136                                         *relAddr+=module->sectionHeaders[symbol->symIndex].shAddr; 
    137                         } 
    138                         break; 
     214                                *relAddr+=symbol->symValue; 
     215                                break; 
    139216 
    140217                        case R_386_PC32: 
    141                                 *relAddr+=symbolAddr-(DWORD)relAddr; 
     218                                *relAddr+=symbol->symValue-(DWORD)relAddr; 
    142219                                break; 
    143220 
    144221                        default: 
    145                                 printf("ModuleSymbolResolve: type = %u\n", ELF_R_TYPE(reloc->info)); 
    146                 } 
    147         } 
    148  
    149         return 0; 
     222                                printf("ModuleSymbolResolve: type = %u, %u\n", ELF_R_TYPE(reloc->info), symbol->symIndex); 
     223                } 
     224        } 
    150225} 
    151226 
     
    186261        if (!ELF_HEAD_CHECK(elfHeader) || (elfHeader->fileType != ELF_REL) || (elfHeader->shEntrySize != sizeof(struct ElfSectionHeader))) 
    187262                return -EINVAL; 
    188          
     263 
    189264        /* Iterate through section headers. */ 
    190265        sectionHeaders=(struct ElfSectionHeader*)((char*)file+elfHeader->shOffset); 
     
    207282 
    208283                if (sectionHeaders[i].shType == SEC_TYPE_STRTAB) 
     284                { 
    209285                        module->strTable=(char*)file+sectionHeaders[i].shOffset; 
     286                        module->strTableSize=sectionHeaders[i].shSize; 
     287                } 
    210288 
    211289                /* Align up sizes. */ 
     
    213291        } 
    214292 
     293        /* Copy over the module data into a new image. */ 
    215294        char* dest; 
    216  
    217295        dest=module->loadAddr; 
    218296 
     
    223301 
    224302                if (sectionHeaders[i].shFlags & SEC_FLAGS_EXEC) 
     303                { 
    225304                        module->textAddr=dest; 
     305                        module->textLength=sectionHeaders[i].shSize; 
     306                } 
    226307 
    227308                sectionHeaders[i].shAddr=dest; 
     
    234315        } 
    235316 
     317        if (ModuleSymbolPrepare(module)) 
     318                return -ENOENT; 
     319 
    236320        for (i=0; i<elfHeader->shEntries; i++) 
    237321        { 
    238322                if (sectionHeaders[i].shType == SEC_TYPE_REL) 
    239                 { 
    240                         /* Resolve entries in the module file. */ 
    241                         if (ModuleSymbolResolve(module, file, i) < 0) 
    242                                 return -EINVAL; 
    243                 } 
     323                        /* Resolve entries in the module. */ 
     324                        ModuleSymbolResolve(module, file, i); 
    244325        } 
    245326 
  • Whitix/branches/hybrid/kernel/symbols.c

    r474 r483  
    3535static int strTableSize DATA; 
    3636 
    37 const char* SymbolLookup(DWORD address, DWORD* size, DWORD* offset) 
     37const char* SymbolLookup(char* stringTable, struct ElfSymbol* symbolTable, int symSize, DWORD address, DWORD* size, DWORD* offset) 
    3838{ 
    3939        int i=0; 
     
    4242        DWORD currOffset=~0; 
    4343 
    44         while (i < (symTableSize/sizeof(struct ElfSymbol))) 
     44        while (i < (symSize/sizeof(struct ElfSymbol))) 
    4545        { 
    46                 symbol=&symTable[i++]; 
     46                symbol=&symbolTable[i++]; 
    4747 
    4848                if (ELF_ST_TYPE(symbol->symInfo) != STT_FUNC) 
     
    6060                return NULL; 
    6161 
    62         *offset=address-currSym->symValue; 
     62        *offset=currOffset; 
    6363        *size=currSym->symSize; 
    6464 
    65         return strTable+currSym->symName; 
     65        if (*offset > *size) 
     66        { 
     67                printf("%#X-%#X = %#X %#X\n", address, symbol->symValue, *offset, *size); 
     68                while (1); 
     69        } 
     70 
     71        return stringTable+currSym->symName; 
    6672} 
    6773 
     
    7076        DWORD size, offset; 
    7177 
    72         const char* name=SymbolLookup(address, &size, &offset); 
     78        const char* name=SymbolLookup(strTable, symTable, symTableSize, address, &size, &offset); 
    7379 
    74         if (name) 
     80        if (name) 
    7581                printf("%s+%#X/%#X\n", name, offset, size); 
    7682        else