X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=Kernel%2Fbin%2Felf.c;h=4bc13b16a29f007190712c9a5c84442ba8391665;hb=07173b260d76a7e6482838c02d5deb2ead2afbb2;hp=dbd38d2c7d98503fab5df326f5395cac583f5ff4;hpb=8bc40333b1401d7616b225945fee53d972c2f418;p=tpg%2Facess2.git diff --git a/Kernel/bin/elf.c b/Kernel/bin/elf.c index dbd38d2c..4bc13b16 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); @@ -88,7 +76,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 @@ -118,9 +106,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 +121,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 +263,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); @@ -393,8 +380,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 +392,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 +408,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 +420,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 +461,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 +470,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 +481,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 +490,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 +539,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 +548,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; } }