X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;ds=sidebyside;f=Kernel%2Fbin%2Felf.c;h=3209c044a5923ea257c832a92ac8563345d400b7;hb=a79ebcb3a2e206251f44e99376ec2ed6c2bacc63;hp=dbd38d2c7d98503fab5df326f5395cac583f5ff4;hpb=8bc40333b1401d7616b225945fee53d972c2f418;p=tpg%2Facess2.git diff --git a/Kernel/bin/elf.c b/Kernel/bin/elf.c index dbd38d2c..3209c044 100644 --- a/Kernel/bin/elf.c +++ b/Kernel/bin/elf.c @@ -1,26 +1,14 @@ /* -Acess v0.1 -ELF Executable Loader Code -*/ -#include + * Acess v0.1 + * ELF Executable Loader Code + */ +#define DEBUG 0 +#include #include -#include "bin_elf.h" +#include "elf.h" -#define DEBUG 1 #define DEBUG_WARN 1 -#if DEBUG -# define DEBUGS(v...) Log(v) -#else -# define DEBUGS(v...) -# undef ENTER -# undef LOG -# undef LEAVE -# define ENTER(...) -# define LOG(...) -# define LEAVE(...) -#endif - // === PROTOTYPES === tBinary *Elf_Load(int fp); @@ -69,7 +57,12 @@ tBinary *Elf_Load(int fp) } // 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 +81,7 @@ tBinary *Elf_Load(int fp) LOG("iPageCount = %i", iPageCount); // Allocate Information Structure - ret = malloc( sizeof(tBinary) + 3*sizeof(Uint)*iPageCount ); + ret = malloc( sizeof(tBinary) + sizeof(tBinaryPage)*iPageCount ); // Fill Info Struct ret->Entry = hdr.entrypoint; ret->Base = -1; // Set Base to maximum value @@ -100,7 +93,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 ) { @@ -118,9 +121,7 @@ tBinary *Elf_Load(int fp) if(phtab[i].Type != PT_LOAD) continue; // Find Base - if(phtab[i].VAddr < ret->Base) ret->Base = phtab[i].VAddr; - - k = 0; + if(phtab[i].VAddr < ret->Base) ret->Base = phtab[i].VAddr; LOG("phtab[%i] = {VAddr:0x%x,Offset:0x%x,FileSize:0x%x}", i, phtab[i].VAddr, phtab[i].Offset, phtab[i].FileSize); @@ -135,7 +136,7 @@ tBinary *Elf_Load(int fp) // Get Pages count = ( (phtab[i].VAddr&0xFFF) + phtab[i].FileSize + 0xFFF) >> 12; - for(;kPages[j+k].Virtual = phtab[i].VAddr + (k<<12); ret->Pages[j+k].Physical = phtab[i].Offset + (k<<12); // Store the offset in the physical address @@ -277,6 +278,7 @@ int Elf_Relocate(void *Base) Elf32_Dyn *dynamicTab = NULL; // Dynamic Table Pointer char *dynstrtab = NULL; // .dynamic String Table Elf32_Sym *dynsymtab = NULL; + int bFailed = 0; ENTER("pBase", Base); @@ -295,7 +297,7 @@ int Elf_Relocate(void *Base) 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; } @@ -324,20 +326,20 @@ 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; } @@ -393,8 +395,7 @@ int Elf_Relocate(void *Base) { ptr = (void*)(iBaseDiff + rel[i].r_offset); if( !Elf_Int_DoRelocate(rel[i].r_info, ptr, *ptr, dynsymtab, (Uint)Base) ) { - LEAVE('x', 0); - return 0; + bFailed = 1; } } } @@ -406,8 +407,7 @@ int Elf_Relocate(void *Base) { ptr = (void*)(iBaseDiff + rela[i].r_offset); if( !Elf_Int_DoRelocate(rel[i].r_info, ptr, rela[i].r_addend, dynsymtab, (Uint)Base) ) { - LEAVE('x', 0); - return 0; + bFailed = 1; } } } @@ -423,8 +423,7 @@ int Elf_Relocate(void *Base) { ptr = (void*)(iBaseDiff + pltRel[i].r_offset); if( !Elf_Int_DoRelocate(pltRel[i].r_info, ptr, *ptr, dynsymtab, (Uint)Base) ) { - LEAVE('x', 0); - return 0; + bFailed = 1; } } } @@ -436,13 +435,17 @@ int Elf_Relocate(void *Base) { ptr = (void*)((Uint)Base + pltRela[i].r_offset); if( !Elf_Int_DoRelocate(pltRela[i].r_info, ptr, pltRela[i].r_addend, dynsymtab, (Uint)Base) ) { - LEAVE('x', 0); - return 0; + bFailed = 1; } } } } + if(bFailed) { + LEAVE('i', 0); + return 0; + } + LEAVE('x', hdr->entrypoint); return hdr->entrypoint; } @@ -473,7 +476,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("R_386_32 *0x%x += 0x%x('%s')", ptr, val, sSymName); *ptr = val + addend; break; @@ -482,7 +485,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("R_386_PC32 *0x%x = 0x%x + 0x%x('%s') - 0x%x", 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; @@ -493,7 +496,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("R_386_GLOB_DAT *0x%x = 0x%x (%s)", ptr, val, sSymName); *ptr = val; break; @@ -502,13 +505,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("R_386_JMP_SLOT *0x%x = 0x%x (%s)", 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("R_386_RELATIVE *0x%x = 0x%x + 0x%x", ptr, base, addend); *ptr = base + addend; break; @@ -551,7 +554,7 @@ int Elf_GetSymbol(void *Base, char *Name, Uint *ret) // Check Bucket i = pBuckets[ iNameHash ]; if(symtab[i].shndx != SHN_UNDEF && strcmp(symtab[i].name, Name) == 0) { - *ret = symtab[ i ].value; + if(ret) *ret = symtab[ i ].value; return 1; } @@ -560,7 +563,7 @@ int Elf_GetSymbol(void *Base, char *Name, Uint *ret) { i = pChains[i]; if(symtab[i].shndx != SHN_UNDEF && strcmp(symtab[ i ].name, Name) == 0) { - *ret = symtab[ i ].value; + if(ret) *ret = symtab[ i ].value; return 1; } }