Merge branch 'master' of git://git.ucc.asn.au/tpg/acess2
[tpg/acess2.git] / KernelLand / Kernel / bin / elf.c
index 7f65d89..37172cf 100644 (file)
@@ -15,8 +15,8 @@
 #if BITS <= 32\r
 # define DISABLE_ELF64\r
 #endif\r
-static int     GetSymbol(const char *Name, void **Value, size_t *Size);\r
-static int     GetSymbol(const char *Name, void **Value, size_t *Size) {\r
+static int     GetSymbol(const char *Name, void **Value, size_t *Size, void *IgnoreBase);\r
+static int     GetSymbol(const char *Name, void **Value, size_t *Size, void *IgnoreBase) {\r
        Uint val;\r
        if(!Binary_GetSymbol(Name, &val)) {\r
                Log_Notice("ELF", "Lookup of '%s' failed", Name);\r
@@ -205,15 +205,14 @@ tBinary *Elf_Load32(int FD, Elf32_Ehdr *Header)
 {\r
        tBinary *ret;\r
        Elf32_Phdr      *phtab;\r
-        int    i, j;\r
-        int    iLoadCount;\r
+        int    j;\r
 \r
        ENTER("xFD", FD);\r
 \r
        // Check architecture with current CPU\r
        // - TODO: Support kernel level emulation\r
        #if ARCH_IS_x86\r
-       if( Header->machine != EM_386 )\r
+       if( Header->e_machine != EM_386 )\r
        {\r
                Log_Warning("ELF", "Unknown architecure on ELF-32");\r
                LEAVE_RET('n');\r
@@ -222,7 +221,7 @@ tBinary *Elf_Load32(int FD, Elf32_Ehdr *Header)
        #endif\r
 \r
        // Check for a program header\r
-       if(Header->phoff == 0) {\r
+       if(Header->e_phoff == 0) {\r
                #if DEBUG_WARN\r
                Log_Warning("ELF", "File does not contain a program header (phoff == 0)");\r
                #endif\r
@@ -231,25 +230,25 @@ tBinary *Elf_Load32(int FD, Elf32_Ehdr *Header)
        }\r
        \r
        // Read Program Header Table\r
-       phtab = malloc( sizeof(Elf32_Phdr) * Header->phentcount );\r
+       phtab = malloc( sizeof(Elf32_Phdr) * Header->e_phnum );\r
        if( !phtab ) {\r
                LEAVE('n');\r
                return NULL;\r
        }\r
-       LOG("hdr.phoff = 0x%08x", Header->phoff);\r
-       VFS_Seek(FD, Header->phoff, SEEK_SET);\r
-       VFS_Read(FD, sizeof(Elf32_Phdr)*Header->phentcount, phtab);\r
+       LOG("hdr.phoff = 0x%08x", Header->e_phoff);\r
+       VFS_Seek(FD, Header->e_phoff, SEEK_SET);\r
+       VFS_Read(FD, sizeof(Elf32_Phdr)*Header->e_phnum, phtab);\r
        \r
        // Count Pages\r
-       iLoadCount = 0;\r
-       LOG("Header->phentcount = %i", Header->phentcount);\r
-       for( i = 0; i < Header->phentcount; i++ )\r
+       unsigned iLoadCount = 0;\r
+       LOG("Header->phentcount = %i", Header->e_phnum);\r
+       for( int i = 0; i < Header->e_phnum; i++ )\r
        {\r
                // Ignore Non-LOAD types\r
-               if(phtab[i].Type != PT_LOAD)\r
+               if(phtab[i].p_type != PT_LOAD)\r
                        continue;\r
                iLoadCount ++;\r
-               LOG("phtab[%i] = {VAddr:0x%x, MemSize:0x%x}", i, phtab[i].VAddr, phtab[i].MemSize);\r
+               LOG("phtab[%i] = {VAddr:0x%x, MemSize:0x%x}", i, phtab[i].p_vaddr, phtab[i].p_memsz);\r
        }\r
        \r
        LOG("iLoadCount = %i", iLoadCount);\r
@@ -257,56 +256,55 @@ tBinary *Elf_Load32(int FD, Elf32_Ehdr *Header)
        // Allocate Information Structure\r
        ret = malloc( sizeof(tBinary) + sizeof(tBinarySection)*iLoadCount );\r
        // Fill Info Struct\r
-       ret->Entry = Header->entrypoint;\r
+       ret->Entry = Header->e_entry;\r
        ret->Base = -1;         // Set Base to maximum value\r
        ret->NumSections = iLoadCount;\r
        ret->Interpreter = NULL;\r
        \r
        // Load Pages\r
        j = 0;\r
-       for( i = 0; i < Header->phentcount; i++ )\r
+       for( int i = 0; i < Header->e_phnum; i++ )\r
        {\r
                //LOG("phtab[%i].Type = 0x%x", i, phtab[i].Type);\r
                LOG("phtab[%i] = {", i);\r
-               LOG(" .Type = 0x%08x", phtab[i].Type);\r
-               LOG(" .Offset = 0x%08x", phtab[i].Offset);\r
-               LOG(" .VAddr = 0x%08x", phtab[i].VAddr);\r
-               LOG(" .PAddr = 0x%08x", phtab[i].PAddr);\r
-               LOG(" .FileSize = 0x%08x", phtab[i].FileSize);\r
-               LOG(" .MemSize = 0x%08x", phtab[i].MemSize);\r
-               LOG(" .Flags = 0x%08x", phtab[i].Flags);\r
-               LOG(" .Align = 0x%08x", phtab[i].Align);\r
+               LOG(" .Type = 0x%08x",   phtab[i].p_type);\r
+               LOG(" .Offset = 0x%08x", phtab[i].p_offset);\r
+               LOG(" .VAddr = 0x%08x",  phtab[i].p_vaddr);\r
+               LOG(" .PAddr = 0x%08x",  phtab[i].p_paddr);\r
+               LOG(" .FileSize = 0x%08x", phtab[i].p_filesz);\r
+               LOG(" .MemSize = 0x%08x", phtab[i].p_memsz);\r
+               LOG(" .Flags = 0x%08x",  phtab[i].p_flags);\r
+               LOG(" .Align = 0x%08x",  phtab[i].p_align);\r
                LOG(" }");\r
                // Get Interpreter Name\r
-               if( phtab[i].Type == PT_INTERP )\r
+               if( phtab[i].p_type == PT_INTERP )\r
                {\r
-                       char *tmp;\r
                        if(ret->Interpreter)    continue;\r
-                       tmp = malloc(phtab[i].FileSize);\r
-                       VFS_Seek(FD, phtab[i].Offset, 1);\r
-                       VFS_Read(FD, phtab[i].FileSize, tmp);\r
+                       char* tmp = malloc(phtab[i].p_filesz);\r
+                       VFS_Seek(FD, phtab[i].p_offset, 1);\r
+                       VFS_Read(FD, phtab[i].p_filesz, tmp);\r
                        ret->Interpreter = Binary_RegInterp(tmp);\r
                        LOG("Interpreter '%s'", tmp);\r
                        free(tmp);\r
                        continue;\r
                }\r
                // Ignore non-LOAD types\r
-               if(phtab[i].Type != PT_LOAD)    continue;\r
+               if(phtab[i].p_type != PT_LOAD)  continue;\r
                \r
                // Find Base\r
-               if(phtab[i].VAddr < ret->Base)  ret->Base = phtab[i].VAddr;\r
+               if(phtab[i].p_vaddr < ret->Base)        ret->Base = phtab[i].p_vaddr;\r
                \r
                LOG("phtab[%i] = {VAddr:0x%x,Offset:0x%x,FileSize:0x%x}",\r
-                       i, phtab[i].VAddr, phtab[i].Offset, phtab[i].FileSize);\r
+                       i, phtab[i].p_vaddr, phtab[i].p_offset, phtab[i].p_filesz);\r
                \r
-               ret->LoadSections[j].Offset = phtab[i].Offset;\r
-               ret->LoadSections[j].FileSize = phtab[i].FileSize;\r
-               ret->LoadSections[j].Virtual = phtab[i].VAddr;\r
-               ret->LoadSections[j].MemSize = phtab[i].MemSize;\r
+               ret->LoadSections[j].Offset   = phtab[i].p_offset;\r
+               ret->LoadSections[j].FileSize = phtab[i].p_filesz;\r
+               ret->LoadSections[j].Virtual  = phtab[i].p_vaddr;\r
+               ret->LoadSections[j].MemSize  = phtab[i].p_memsz;\r
                ret->LoadSections[j].Flags = 0;\r
-               if( !(phtab[i].Flags & PF_W) )\r
+               if( !(phtab[i].p_flags & PF_W) )\r
                        ret->LoadSections[j].Flags |= BIN_SECTFLAG_RO;\r
-               if( phtab[i].Flags & PF_X )\r
+               if( phtab[i].p_flags & PF_X )\r
                        ret->LoadSections[j].Flags |= BIN_SECTFLAG_EXEC;\r
                j ++;\r
        }\r

UCC git Repository :: git.ucc.asn.au