X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=Kernel%2Fbin%2Felf.c;h=d04dbda45b2c3a669d24bb4bae80d01ef72a4aeb;hb=d7d0479311c4dba418ee880f27f01418fd536344;hp=4bc13b16a29f007190712c9a5c84442ba8391665;hpb=fbb51904de075386178cc6bb14717132d3b2153d;p=tpg%2Facess2.git diff --git a/Kernel/bin/elf.c b/Kernel/bin/elf.c index 4bc13b16..d04dbda4 100644 --- a/Kernel/bin/elf.c +++ b/Kernel/bin/elf.c @@ -9,13 +9,12 @@ #define DEBUG_WARN 1 - // === PROTOTYPES === tBinary *Elf_Load(int fp); int Elf_Relocate(void *Base); - int Elf_GetSymbol(void *Base, char *Name, Uint *ret); + int Elf_GetSymbol(void *Base, const char *Name, Uint *ret); int Elf_Int_DoRelocate(Uint r_info, Uint32 *ptr, Uint32 addend, Elf32_Sym *symtab, Uint base); -Uint Elf_Int_HashString(char *str); +Uint Elf_Int_HashString(const char *str); // === GLOBALS === tBinaryType gELF_Info = { @@ -35,14 +34,14 @@ tBinary *Elf_Load(int fp) int iPageCount; int count; - ENTER("ifp", fp); + ENTER("xfp", fp); // Read ELF Header VFS_Read(fp, sizeof(hdr), &hdr); // Check the file type if(hdr.ident[0] != 0x7F || hdr.ident[1] != 'E' || hdr.ident[2] != 'L' || hdr.ident[3] != 'F') { - Warning("Non-ELF File was passed to the ELF loader\n"); + Log_Warning("ELF", "Non-ELF File was passed to the ELF loader"); LEAVE('n'); return NULL; } @@ -50,14 +49,19 @@ tBinary *Elf_Load(int fp) // Check for a program header if(hdr.phoff == 0) { #if DEBUG_WARN - Warning("ELF File does not contain a program header\n"); + Log_Warning("ELF", "File does not contain a program header (phoff == 0)"); #endif LEAVE('n'); return NULL; } // Read Program Header Table - phtab = malloc(sizeof(Elf32_Phdr)*hdr.phentcount); + phtab = malloc( sizeof(Elf32_Phdr) * hdr.phentcount ); + if( !phtab ) { + LEAVE('n'); + return NULL; + } + LOG("hdr.phoff = 0x%08x", hdr.phoff); VFS_Seek(fp, hdr.phoff, SEEK_SET); VFS_Read(fp, sizeof(Elf32_Phdr)*hdr.phentcount, phtab); @@ -88,7 +92,17 @@ tBinary *Elf_Load(int fp) for( i = 0; i < hdr.phentcount; i++ ) { int lastSize; - LOG("phtab[%i].Type = 0x%x", i, phtab[i].Type); + //LOG("phtab[%i].Type = 0x%x", i, phtab[i].Type); + LOG("phtab[%i] = {", i); + LOG(" .Type = 0x%08x", phtab[i].Type); + LOG(" .Offset = 0x%08x", phtab[i].Offset); + LOG(" .VAddr = 0x%08x", phtab[i].VAddr); + LOG(" .PAddr = 0x%08x", phtab[i].PAddr); + LOG(" .FileSize = 0x%08x", phtab[i].FileSize); + LOG(" .MemSize = 0x%08x", phtab[i].MemSize); + LOG(" .Flags = 0x%08x", phtab[i].Flags); + LOG(" .Align = 0x%08x", phtab[i].Align); + LOG(" }"); // Get Interpreter Name if( phtab[i].Type == PT_INTERP ) { @@ -111,13 +125,15 @@ tBinary *Elf_Load(int fp) LOG("phtab[%i] = {VAddr:0x%x,Offset:0x%x,FileSize:0x%x}", i, phtab[i].VAddr, phtab[i].Offset, phtab[i].FileSize); - if( (phtab[i].FileSize & 0xFFF) < 0x1000 - (phtab[i].VAddr & 0xFFF) ) - lastSize = phtab[i].FileSize; - else + //if( (phtab[i].FileSize & 0xFFF) < 0x1000 - (phtab[i].VAddr & 0xFFF) ) + // lastSize = phtab[i].FileSize; + //else lastSize = (phtab[i].FileSize & 0xFFF) + (phtab[i].VAddr & 0xFFF); - lastSize &= 0xFFF; + //lastSize &= 0xFFF; - LOG("lastSize = 0x%x", lastSize); + //LOG("lastSize = 0x%x", lastSize); + + lastSize = phtab[i].FileSize; // Get Pages count = ( (phtab[i].VAddr&0xFFF) + phtab[i].FileSize + 0xFFF) >> 12; @@ -137,6 +153,7 @@ tBinary *Elf_Load(int fp) ret->Pages[j+k].Size = 4096; LOG("ret->Pages[%i].Size = 0x%x", j+k, ret->Pages[j+k].Size); ret->Pages[j+k].Flags = 0; + lastSize -= ret->Pages[j+k].Size; } count = (phtab[i].MemSize + 0xFFF) >> 12; for(;kNumPages = j; @@ -268,9 +285,9 @@ int Elf_Relocate(void *Base) ENTER("pBase", Base); // Parse Program Header to get Dynamic Table - phtab = Base + hdr->phoff; + phtab = (void *)( (tVAddr)Base + hdr->phoff ); iSegmentCount = hdr->phentcount; - for(i=0;i phtab[i].VAddr) @@ -279,10 +296,10 @@ int Elf_Relocate(void *Base) // Find Dynamic Section if(phtab[i].Type == PT_DYNAMIC) { if(dynamicTab) { - Warning("ELF", "Elf_Relocate - Multiple PT_DYNAMIC segments\n"); + Log_Warning("ELF", "Elf_Relocate - Multiple PT_DYNAMIC segments\n"); continue; } - dynamicTab = (void *) phtab[i].VAddr; + dynamicTab = (void *) (tVAddr) phtab[i].VAddr; j = i; // Save Dynamic Table ID break; } @@ -290,7 +307,7 @@ int Elf_Relocate(void *Base) // Check if a PT_DYNAMIC segement was found if(!dynamicTab) { - Warning("ELF", "Elf_Relocate: No PT_DYNAMIC segment in image, returning\n"); + Log_Warning("ELF", "Elf_Relocate: No PT_DYNAMIC segment in image, returning\n"); LEAVE('x', hdr->entrypoint); return hdr->entrypoint; } @@ -311,28 +328,32 @@ int Elf_Relocate(void *Base) // --- Symbol Table --- case DT_SYMTAB: dynamicTab[j].d_val += iBaseDiff; - dynsymtab = (void*)(dynamicTab[j].d_val); + dynsymtab = (void*) (tVAddr) dynamicTab[j].d_val; hdr->misc.SymTable = dynamicTab[j].d_val; // Saved in unused bytes of ident break; // --- String Table --- case DT_STRTAB: dynamicTab[j].d_val += iBaseDiff; - dynstrtab = (void*)(dynamicTab[j].d_val); + dynstrtab = (void*) (tVAddr) dynamicTab[j].d_val; break; // --- Hash Table -- case DT_HASH: dynamicTab[j].d_val += iBaseDiff; - iSymCount = ((Uint*)(dynamicTab[j].d_val))[1]; + iSymCount = ((Uint*)((tVAddr)dynamicTab[j].d_val))[1]; hdr->misc.HashTable = dynamicTab[j].d_val; // Saved in unused bytes of ident break; } } + if( !dynsymtab && iSymCount > 0 ) { + Log_Warning("ELF", "Elf_Relocate: No Dynamic symbol table, but count >0"); + return 0; + } // Alter Symbols to true base - for(i=0;ientrypoint); - return hdr->entrypoint; + LEAVE('x', 1); + return 1; } /** @@ -461,7 +485,7 @@ int Elf_Int_DoRelocate(Uint r_info, Uint32 *ptr, Uint32 addend, Elf32_Sym *symta if( !Elf_GetSymbol((void*)base, sSymName, &val) ) // Search this binary first if( !Binary_GetSymbol( sSymName, &val ) ) return 0; - LOG("R_386_32 *0x%x += 0x%x('%s')", ptr, val, sSymName); + LOG("%08x R_386_32 *0x%x += 0x%x('%s')", r_info, ptr, val, sSymName); *ptr = val + addend; break; @@ -470,7 +494,7 @@ int Elf_Int_DoRelocate(Uint r_info, Uint32 *ptr, Uint32 addend, Elf32_Sym *symta if( !Elf_GetSymbol( (void*)base, sSymName, &val ) ) if( !Binary_GetSymbol( sSymName, &val ) ) return 0; - LOG("R_386_PC32 *0x%x = 0x%x + 0x%x('%s') - 0x%x", ptr, *ptr, val, sSymName, (Uint)ptr ); + LOG("%08x R_386_PC32 *0x%x = 0x%x + 0x%x('%s') - 0x%x", r_info, ptr, *ptr, val, sSymName, (Uint)ptr ); // TODO: Check if it needs the true value of ptr or the compiled value // NOTE: Testing using true value *ptr = val + addend - (Uint)ptr; @@ -481,7 +505,7 @@ int Elf_Int_DoRelocate(Uint r_info, Uint32 *ptr, Uint32 addend, Elf32_Sym *symta if( !Elf_GetSymbol( (void*)base, sSymName, &val ) ) if( !Binary_GetSymbol( sSymName, &val ) ) return 0; - LOG("R_386_GLOB_DAT *0x%x = 0x%x (%s)", ptr, val, sSymName); + LOG("%08x R_386_GLOB_DAT *0x%x = 0x%x (%s)", r_info, ptr, val, sSymName); *ptr = val; break; @@ -490,13 +514,13 @@ int Elf_Int_DoRelocate(Uint r_info, Uint32 *ptr, Uint32 addend, Elf32_Sym *symta if( !Elf_GetSymbol( (void*)base, sSymName, &val ) ) if( !Binary_GetSymbol( sSymName, &val ) ) return 0; - LOG("R_386_JMP_SLOT *0x%x = 0x%x (%s)", ptr, val, sSymName); + LOG("%08x R_386_JMP_SLOT *0x%x = 0x%x (%s)", r_info, ptr, val, sSymName); *ptr = val; break; // Base Address (B+A) case R_386_RELATIVE: - LOG("R_386_RELATIVE *0x%x = 0x%x + 0x%x", ptr, base, addend); + LOG("%08x R_386_RELATIVE *0x%x = 0x%x + 0x%x", r_info, ptr, base, addend); *ptr = base + addend; break; @@ -508,10 +532,10 @@ int Elf_Int_DoRelocate(Uint r_info, Uint32 *ptr, Uint32 addend, Elf32_Sym *symta } /** - * \fn int Elf_GetSymbol(void *Base, char *name, Uint *ret) + * \fn int Elf_GetSymbol(void *Base, const char *name, Uint *ret) * \brief Get a symbol from the loaded binary */ -int Elf_GetSymbol(void *Base, char *Name, Uint *ret) +int Elf_GetSymbol(void *Base, const char *Name, Uint *ret) { Elf32_Ehdr *hdr = (void*)Base; Elf32_Sym *symtab; @@ -561,7 +585,7 @@ int Elf_GetSymbol(void *Base, char *Name, Uint *ret) * \param str String to hash * \return Hash value */ -Uint Elf_Int_HashString(char *str) +Uint Elf_Int_HashString(const char *str) { Uint h = 0, g; while(*str)