Kernel/ELF - Fixed ELF segment permissions
[tpg/acess2.git] / Kernel / bin / pe.c
index 0b74529..0e7d1ac 100644 (file)
 tBinary        *PE_Load(int fp);\r
 tBinary        *MZ_Open(int fp);\r
  int   PE_Relocate(void *Base);\r
- int   PE_GetSymbol(void *Base, char *Name, Uint *Ret);\r
+ int   PE_GetSymbol(void *Base, const char *Name, Uint *Ret);\r
 \r
 // === GLOBALS ===\r
 MODULE_DEFINE(0, 0x0032, BinPE, PE_Install, NULL, NULL);\r
-char   *gsPE_DefaultInterpreter = "/Acess/Libs/ld-acess.so";\r
+const char     *gsPE_DefaultInterpreter = "/Acess/Libs/ld-acess.so";\r
 tBinaryType    gPE_Loader = {\r
        NULL,\r
        ('M'|('Z'<<8)), 0xFFFF, // 'MZ'\r
@@ -37,8 +37,8 @@ int PE_Install(char **Arguments)
  */\r
 tBinary *PE_Load(int FP)\r
 {\r
-        int    count, i, j, k;\r
-        int    iPageCount;\r
+        int    count, i, j;\r
+        int    iSectCount;\r
        tBinary *ret;\r
        tPE_DOS_HEADER          dosHdr;\r
        tPE_IMAGE_HEADERS       peHeaders;\r
@@ -83,7 +83,7 @@ tBinary *PE_Load(int FP)
        VFS_Read(FP, count, peSections);\r
        \r
        // Count Pages\r
-       iPageCount = 1; // 1st page is headers\r
+       iSectCount = 1; // 1st page is headers\r
        for( i = 0; i < peHeaders.FileHeader.SectionCount; i++ )\r
        {\r
                // Check if the section is loadable\r
@@ -91,30 +91,32 @@ tBinary *PE_Load(int FP)
                if(peSections[i].RVA + peHeaders.OptHeader.ImageBase == 0)              continue;\r
                \r
                // Moar pages\r
-               iPageCount += (peSections[i].VirtualSize + 0xFFF) >> 12;\r
+               iSectCount ++;\r
        }\r
        \r
-       LOG("%i Pages", iPageCount);\r
+       LOG("%i Sections", iSectCount);\r
        \r
        // Initialise Executable Information\r
-       ret = malloc(sizeof(tBinary) + sizeof(tBinaryPage)*iPageCount);\r
+       ret = malloc(sizeof(tBinary) + sizeof(tBinarySection)*iSectCount);\r
        \r
        ret->Entry = peHeaders.OptHeader.EntryPoint + peHeaders.OptHeader.ImageBase;\r
        ret->Base = peHeaders.OptHeader.ImageBase;\r
        ret->Interpreter = gsPE_DefaultInterpreter;\r
-       ret->NumPages = iPageCount;\r
+       ret->NumSections = iSectCount;\r
        \r
        LOG("Entry=%p, BaseAddress=0x%x\n", ret->Entry, ret->Base);\r
        \r
-       ret->Pages[0].Virtual = peHeaders.OptHeader.ImageBase;\r
-       ret->Pages[0].Physical = 0;\r
-       ret->Pages[0].Size = 4096;\r
-       ret->Pages[0].Flags = 0;\r
+       ret->LoadSections[0].Virtual = peHeaders.OptHeader.ImageBase;\r
+       ret->LoadSections[0].Offset = 0;\r
+       ret->LoadSections[0].FileSize = 4096;\r
+       ret->LoadSections[0].MemSize = 4096;\r
+       ret->LoadSections[0].Flags = 0;\r
        \r
        // Parse Sections\r
        j = 1;  // Page Index\r
        for( i = 0; i < peHeaders.FileHeader.SectionCount; i++ )\r
        {\r
+               tBinarySection  *sect = &ret->LoadSections[j];\r
                iVA = peSections[i].RVA + peHeaders.OptHeader.ImageBase;\r
                \r
                // Skip non-loadable sections\r
@@ -127,34 +129,16 @@ tBinary *PE_Load(int FP)
                // Create Flags\r
                iFlags = 0;\r
                if(peSections[i].Flags & PE_SECTION_FLAG_MEM_EXECUTE)\r
-                       iFlags |= BIN_PAGEFLAG_EXEC;\r
+                       iFlags |= BIN_SECTFLAG_EXEC;\r
                if( !(peSections[i].Flags & PE_SECTION_FLAG_MEM_WRITE) )\r
-                       iFlags |= BIN_PAGEFLAG_RO;\r
+                       iFlags |= BIN_SECTFLAG_RO;\r
                \r
-               // Create Page Listing\r
-               count = (peSections[i].RawSize + 0xFFF) >> 12;\r
-               for(k=0;k<count;k++)\r
-               {\r
-                       ret->Pages[j+k].Virtual = iVA + (k<<12);\r
-                       ret->Pages[j+k].Physical = peSections[i].RawOffs + (k<<12);     // Store the offset in the physical address\r
-                       if(k == count-1 && (peSections[i].RawSize & 0xFFF))\r
-                               ret->Pages[j+k].Size = peSections[i].RawSize & 0xFFF;   // Byte count in page\r
-                       else\r
-                               ret->Pages[j+k].Size = 4096;\r
-                       ret->Pages[j+k].Flags = iFlags;\r
-               }\r
-               count = (peSections[i].VirtualSize + 0xFFF) >> 12;\r
-               for(;k<count;k++)\r
-               {\r
-                       ret->Pages[j+k].Virtual = iVA + (k<<12);\r
-                       ret->Pages[j+k].Physical = -1;  // -1 = Fill with zeros\r
-                       if(k == count-1 && (peSections[i].VirtualSize & 0xFFF))\r
-                               ret->Pages[j+k].Size = peSections[i].VirtualSize & 0xFFF;       // Byte count in page\r
-                       else\r
-                               ret->Pages[j+k].Size = 4096;\r
-                       ret->Pages[j+k].Flags = iFlags;\r
-               }\r
-               j += count;\r
+               sect->Virtual = iVA;\r
+               sect->Offset = peSections[i].RawOffs;\r
+               sect->FileSize = peSections[i].RawSize;\r
+               sect->MemSize = peSections[i].VirtualSize;\r
+               sect->Flags = iFlags;\r
+               j ++;\r
                \r
                LOG("%i Name:'%s', RVA: 0x%x, Size: 0x%x (0x%x), Ofs: 0x%x, Flags: 0x%x",\r
                        i, namebuf, \r
@@ -231,7 +215,7 @@ int PE_Relocate(void *Base)
        return 0;\r
 }\r
 \r
-int PE_GetSymbol(void *Base, char *Name, Uint *Ret)\r
+int PE_GetSymbol(void *Base, const char *Name, Uint *Ret)\r
 {\r
        return 0;\r
 }\r

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