Kernel - Preparing for ELF64 support
[tpg/acess2.git] / Kernel / bin / elf.c
index 90a092f..4feb6bc 100644 (file)
@@ -9,13 +9,14 @@
 \r
 #define DEBUG_WARN     1\r
 \r
-\r
 // === PROTOTYPES ===\r
 tBinary        *Elf_Load(int fp);\r
+tBinary *Elf_Load32(Elf32_Ehdr *hdr, int fp);\r
+//tBinary *Elf_Load64(int fp);\r
  int   Elf_Relocate(void *Base);\r
- int   Elf_GetSymbol(void *Base, char *Name, Uint *ret);\r
+ int   Elf_GetSymbol(void *Base, const char *Name, Uint *ret);\r
  int   Elf_Int_DoRelocate(Uint r_info, Uint32 *ptr, Uint32 addend, Elf32_Sym *symtab, Uint base);\r
-Uint   Elf_Int_HashString(char *str);\r
+Uint   Elf_Int_HashString(const char *str);\r
 \r
 // === GLOBALS ===\r
 tBinaryType    gELF_Info = {\r
@@ -28,13 +29,8 @@ tBinaryType  gELF_Info = {
 // === CODE ===\r
 tBinary *Elf_Load(int fp)\r
 {\r
-       tBinary *ret;\r
        Elf32_Ehdr      hdr;\r
-       Elf32_Phdr      *phtab;\r
-        int    i, j, k;\r
-        int    iPageCount;\r
-        int    count;\r
-       \r
+\r
        ENTER("xfp", fp);\r
        \r
        // Read ELF Header\r
@@ -42,34 +38,52 @@ tBinary *Elf_Load(int fp)
        \r
        // Check the file type\r
        if(hdr.ident[0] != 0x7F || hdr.ident[1] != 'E' || hdr.ident[2] != 'L' || hdr.ident[3] != 'F') {\r
-               Warning("Non-ELF File was passed to the ELF loader\n");\r
+               Log_Warning("ELF", "Non-ELF File was passed to the ELF loader");\r
                LEAVE('n');\r
                return NULL;\r
        }\r
-       \r
+\r
+       switch( hdr.ident[4] )\r
+       {\r
+       case 1: // ELF32\r
+               return Elf_Load32(&hdr, fp);\r
+//     case 2: // ELF64\r
+//             return ELf_Load64(fp);\r
+       }\r
+       return NULL;\r
+}\r
+\r
+tBinary *Elf_Load32(Elf32_Ehdr *hdr, int fp)\r
+{      \r
+       tBinary *ret;\r
+       Elf32_Phdr      *phtab;\r
+        int    i, j, k;\r
+        int    iPageCount;\r
+        int    count;\r
+\r
        // Check for a program header\r
-       if(hdr.phoff == 0) {\r
+       if(hdr->phoff == 0) {\r
                #if DEBUG_WARN\r
-               Warning("ELF File does not contain a program header\n");\r
+               Log_Warning("ELF", "File does not contain a program header (phoff == 0)");\r
                #endif\r
                LEAVE('n');\r
                return NULL;\r
        }\r
        \r
        // Read Program Header Table\r
-       phtab = malloc( sizeof(Elf32_Phdr) * hdr.phentcount );\r
+       phtab = malloc( sizeof(Elf32_Phdr) * hdr->phentcount );\r
        if( !phtab ) {\r
                LEAVE('n');\r
                return NULL;\r
        }\r
-       LOG("hdr.phoff = 0x%08x", hdr.phoff);\r
-       VFS_Seek(fp, hdr.phoff, SEEK_SET);\r
-       VFS_Read(fp, sizeof(Elf32_Phdr)*hdr.phentcount, phtab);\r
+       LOG("hdr->phoff = 0x%08x", hdr->phoff);\r
+       VFS_Seek(fp, hdr->phoff, SEEK_SET);\r
+       VFS_Read(fp, sizeof(Elf32_Phdr)*hdr->phentcount, phtab);\r
        \r
        // Count Pages\r
        iPageCount = 0;\r
-       LOG("hdr.phentcount = %i", hdr.phentcount);\r
-       for( i = 0; i < hdr.phentcount; i++ )\r
+       LOG("hdr->phentcount = %i", hdr->phentcount);\r
+       for( i = 0; i < hdr->phentcount; i++ )\r
        {\r
                // Ignore Non-LOAD types\r
                if(phtab[i].Type != PT_LOAD)\r
@@ -83,14 +97,14 @@ tBinary *Elf_Load(int fp)
        // Allocate Information Structure\r
        ret = malloc( sizeof(tBinary) + sizeof(tBinaryPage)*iPageCount );\r
        // Fill Info Struct\r
-       ret->Entry = hdr.entrypoint;\r
+       ret->Entry = hdr->entrypoint;\r
        ret->Base = -1;         // Set Base to maximum value\r
        ret->NumPages = iPageCount;\r
        ret->Interpreter = NULL;\r
        \r
        // Load Pages\r
        j = 0;\r
-       for( i = 0; i < hdr.phentcount; i++ )\r
+       for( i = 0; i < hdr->phentcount; i++ )\r
        {\r
                 int    lastSize;\r
                //LOG("phtab[%i].Type = 0x%x", i, phtab[i].Type);\r
@@ -223,7 +237,7 @@ tBinary *Elf_Load(int fp)
                // Reallocate\r
                ret = realloc( ret, sizeof(tBinary) + 3*sizeof(Uint)*j );\r
                if(!ret) {\r
-                       Warning("BIN", "ElfLoad: Unable to reallocate return structure");\r
+                       Log_Warning("BIN", "ElfLoad: Unable to reallocate return structure");\r
                        return NULL;\r
                }\r
                ret->NumPages = j;\r
@@ -286,7 +300,7 @@ int Elf_Relocate(void *Base)
        ENTER("pBase", Base);\r
        \r
        // Parse Program Header to get Dynamic Table\r
-       phtab = Base + hdr->phoff;\r
+       phtab = (void *)( (tVAddr)Base + hdr->phoff );\r
        iSegmentCount = hdr->phentcount;\r
        for(i = 0; i < iSegmentCount; i ++ )\r
        {\r
@@ -348,6 +362,10 @@ int Elf_Relocate(void *Base)
                }\r
        }\r
 \r
+       if( !dynsymtab && iSymCount > 0 ) {\r
+               Log_Warning("ELF", "Elf_Relocate: No Dynamic symbol table, but count >0");\r
+               return 0;\r
+       }\r
 \r
        // Alter Symbols to true base\r
        for(i = 0; i < iSymCount; i ++)\r
@@ -410,7 +428,7 @@ int Elf_Relocate(void *Base)
                for( i = 0; i < j; i++ )\r
                {\r
                        ptr = (void*)(iBaseDiff + rela[i].r_offset);\r
-                       if( !Elf_Int_DoRelocate(rel[i].r_info, ptr, rela[i].r_addend, dynsymtab, (Uint)Base) ) {\r
+                       if( !Elf_Int_DoRelocate(rela[i].r_info, ptr, rela[i].r_addend, dynsymtab, (Uint)Base) ) {\r
                                bFailed = 1;\r
                        }\r
                }\r
@@ -452,8 +470,8 @@ int Elf_Relocate(void *Base)
                return 0;\r
        }\r
        \r
-       LEAVE('x', hdr->entrypoint);\r
-       return hdr->entrypoint;\r
+       LEAVE('x', 1);\r
+       return 1;\r
 }\r
 \r
 /**\r
@@ -529,10 +547,10 @@ int Elf_Int_DoRelocate(Uint r_info, Uint32 *ptr, Uint32 addend, Elf32_Sym *symta
 }\r
 \r
 /**\r
- * \fn int Elf_GetSymbol(void *Base, char *name, Uint *ret)\r
+ * \fn int Elf_GetSymbol(void *Base, const char *name, Uint *ret)\r
  * \brief Get a symbol from the loaded binary\r
  */\r
-int Elf_GetSymbol(void *Base, char *Name, Uint *ret)\r
+int Elf_GetSymbol(void *Base, const char *Name, Uint *ret)\r
 {\r
        Elf32_Ehdr      *hdr = (void*)Base;\r
        Elf32_Sym       *symtab;\r
@@ -582,7 +600,7 @@ int Elf_GetSymbol(void *Base, char *Name, Uint *ret)
  * \param str  String to hash\r
  * \return Hash value\r
  */\r
-Uint Elf_Int_HashString(char *str)\r
+Uint Elf_Int_HashString(const char *str)\r
 {\r
        Uint    h = 0, g;\r
        while(*str)\r

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