0e7d1acde47c8a47ef9badec9fa4637b0e2d364e
[tpg/acess2.git] / Kernel / bin / pe.c
1 /*\r
2  * Acess v1\r
3  * Portable Executable Loader\r
4  */\r
5 #define DEBUG   1\r
6 #include <acess.h>\r
7 #include <binary.h>\r
8 #include <modules.h>\r
9 #include "pe.h"\r
10 \r
11 // === PROTOTYPES ===\r
12  int    PE_Install(char **Arguments);\r
13 tBinary *PE_Load(int fp);\r
14 tBinary *MZ_Open(int fp);\r
15  int    PE_Relocate(void *Base);\r
16  int    PE_GetSymbol(void *Base, const char *Name, Uint *Ret);\r
17 \r
18 // === GLOBALS ===\r
19 MODULE_DEFINE(0, 0x0032, BinPE, PE_Install, NULL, NULL);\r
20 const char      *gsPE_DefaultInterpreter = "/Acess/Libs/ld-acess.so";\r
21 tBinaryType     gPE_Loader = {\r
22         NULL,\r
23         ('M'|('Z'<<8)), 0xFFFF, // 'MZ'\r
24         "PE/DOS",\r
25         PE_Load, PE_Relocate, PE_GetSymbol\r
26         };\r
27 \r
28 // === CODE ===\r
29 int PE_Install(char **Arguments)\r
30 {\r
31         Binary_RegisterType(&gPE_Loader);\r
32         return MODULE_ERR_OK;\r
33 }\r
34 \r
35 /**\r
36  * \brief Loads a PE Binary\r
37  */\r
38 tBinary *PE_Load(int FP)\r
39 {\r
40          int    count, i, j;\r
41          int    iSectCount;\r
42         tBinary *ret;\r
43         tPE_DOS_HEADER          dosHdr;\r
44         tPE_IMAGE_HEADERS       peHeaders;\r
45         tPE_SECTION_HEADER      *peSections;\r
46         char    namebuf[9] = {0};\r
47         Uint    iFlags, iVA;\r
48         \r
49         ENTER("xFP", FP);\r
50         \r
51         // Read DOS header and check\r
52         VFS_Read(FP, sizeof(tPE_DOS_HEADER), &dosHdr);\r
53         if( dosHdr.Ident != ('M'|('Z'<<8)) ) {\r
54                 LEAVE('n');\r
55                 return NULL;\r
56         }\r
57         \r
58         // - Read PE Header\r
59         VFS_Seek(FP, dosHdr.PeHdrOffs, SEEK_SET);\r
60         if( VFS_Tell(FP) != dosHdr.PeHdrOffs ) {\r
61                 ret = MZ_Open(FP);\r
62                 LEAVE('p', ret);\r
63                 return ret;\r
64         }\r
65         VFS_Read(FP, sizeof(tPE_IMAGE_HEADERS), &peHeaders);\r
66         \r
67         // - Check PE Signature and pass on to the MZ Loader if invalid\r
68         if( peHeaders.Signature != (('P')|('E'<<8)) ) {\r
69                 ret = MZ_Open(FP);\r
70                 LEAVE('p', ret);\r
71                 return ret;\r
72         }\r
73         \r
74         // Read Sections (Uses `count` as a temp variable)\r
75         count = sizeof(tPE_SECTION_HEADER) * peHeaders.FileHeader.SectionCount;\r
76         peSections = malloc( count );\r
77         if(!peSections)\r
78         {\r
79                 Warning("PE_Load - Unable to allocate `peSections`, 0x%x bytes", count);\r
80                 LEAVE('n');\r
81                 return NULL;\r
82         }\r
83         VFS_Read(FP, count, peSections);\r
84         \r
85         // Count Pages\r
86         iSectCount = 1; // 1st page is headers\r
87         for( i = 0; i < peHeaders.FileHeader.SectionCount; i++ )\r
88         {\r
89                 // Check if the section is loadable\r
90                 // (VA is zero in non-loadable sections)\r
91                 if(peSections[i].RVA + peHeaders.OptHeader.ImageBase == 0)              continue;\r
92                 \r
93                 // Moar pages\r
94                 iSectCount ++;\r
95         }\r
96         \r
97         LOG("%i Sections", iSectCount);\r
98         \r
99         // Initialise Executable Information\r
100         ret = malloc(sizeof(tBinary) + sizeof(tBinarySection)*iSectCount);\r
101         \r
102         ret->Entry = peHeaders.OptHeader.EntryPoint + peHeaders.OptHeader.ImageBase;\r
103         ret->Base = peHeaders.OptHeader.ImageBase;\r
104         ret->Interpreter = gsPE_DefaultInterpreter;\r
105         ret->NumSections = iSectCount;\r
106         \r
107         LOG("Entry=%p, BaseAddress=0x%x\n", ret->Entry, ret->Base);\r
108         \r
109         ret->LoadSections[0].Virtual = peHeaders.OptHeader.ImageBase;\r
110         ret->LoadSections[0].Offset = 0;\r
111         ret->LoadSections[0].FileSize = 4096;\r
112         ret->LoadSections[0].MemSize = 4096;\r
113         ret->LoadSections[0].Flags = 0;\r
114         \r
115         // Parse Sections\r
116         j = 1;  // Page Index\r
117         for( i = 0; i < peHeaders.FileHeader.SectionCount; i++ )\r
118         {\r
119                 tBinarySection  *sect = &ret->LoadSections[j];\r
120                 iVA = peSections[i].RVA + peHeaders.OptHeader.ImageBase;\r
121                 \r
122                 // Skip non-loadable sections\r
123                 if(iVA == 0)    continue;\r
124                 \r
125                 // Create Name Buffer\r
126                 memcpy(namebuf, peSections[i].Name, 8);\r
127                 LOG("Section %i '%s', iVA = %p", i, namebuf, iVA);\r
128                 \r
129                 // Create Flags\r
130                 iFlags = 0;\r
131                 if(peSections[i].Flags & PE_SECTION_FLAG_MEM_EXECUTE)\r
132                         iFlags |= BIN_SECTFLAG_EXEC;\r
133                 if( !(peSections[i].Flags & PE_SECTION_FLAG_MEM_WRITE) )\r
134                         iFlags |= BIN_SECTFLAG_RO;\r
135                 \r
136                 sect->Virtual = iVA;\r
137                 sect->Offset = peSections[i].RawOffs;\r
138                 sect->FileSize = peSections[i].RawSize;\r
139                 sect->MemSize = peSections[i].VirtualSize;\r
140                 sect->Flags = iFlags;\r
141                 j ++;\r
142                 \r
143                 LOG("%i Name:'%s', RVA: 0x%x, Size: 0x%x (0x%x), Ofs: 0x%x, Flags: 0x%x",\r
144                         i, namebuf, \r
145                         iVA,\r
146                         peSections[i].VirtualSize, peSections[i].RawSize, peSections[i].RawOffs,\r
147                         peSections[i].Flags\r
148                         );\r
149                 \r
150         }\r
151         // Free Executable Memory\r
152         free(peSections);\r
153         \r
154         LEAVE('p', ret);\r
155         return ret;\r
156 }\r
157 \r
158 /**\r
159  */\r
160 tBinary *MZ_Open(int FP)\r
161 {\r
162         ENTER("xFP", FP);\r
163         UNIMPLEMENTED();\r
164         LEAVE('n');\r
165         return NULL;\r
166 }\r
167 \r
168 int PE_Relocate(void *Base)\r
169 {\r
170         tPE_DOS_HEADER          *dosHdr;\r
171         tPE_IMAGE_HEADERS       *peHeaders;\r
172         tPE_SECTION_HEADER      *peSections;\r
173         tPE_DATA_DIR    *directory;\r
174         tPE_IMPORT_DIR  *impDir;\r
175          int    i;\r
176         Uint    iBase = (Uint)Base;\r
177         #if 0\r
178         void    *hLibrary;\r
179         char    *libPath;\r
180         #endif\r
181         \r
182         ENTER("pBase", Base);\r
183         dosHdr = Base;\r
184         peHeaders = (void*)( iBase + dosHdr->PeHdrOffs );\r
185         LOG("Prefered Base %p", peHeaders->OptHeader.ImageBase);\r
186         peSections = (void*)( iBase + sizeof(tPE_IMAGE_HEADERS) );\r
187         \r
188         directory = (void*)(peSections[0].RVA + iBase);\r
189         \r
190         // === Load Import Tables\r
191         impDir = (void*)( directory[PE_DIR_IMPORT].RVA + iBase );\r
192         for( i = 0; impDir[i].DLLName != NULL; i++ )\r
193         {\r
194                 impDir[i].DLLName += iBase;\r
195                 impDir[i].ImportLookupTable += iBase/4;\r
196                 impDir[i].ImportAddressTable += iBase/4;\r
197                 LOG("DLL Required '%s'(0x%x)", impDir[i].DLLName, impDir[i].DLLName);\r
198                 #if 0\r
199                 libPath = FindLibrary(impDir[i].DLLName);\r
200                 if(libPath == NULL)\r
201                 {\r
202                         Warning("PE_Relocate - Unable to find library '%s'");\r
203                         LEAVE('i', -1);\r
204                         return -1;\r
205                 }\r
206                 LOG("DLL Path = '%s'", libPath);\r
207                 hLibrary = DynLib_Load(libPath, 0);\r
208                 #endif\r
209         }\r
210         \r
211         for(i=0;i<PE_DIR_LAST;i++)\r
212                 LOG("directory[%i] = {RVA=0x%x,Size=0x%x}", i, directory[i].RVA, directory[i].Size);\r
213         \r
214         LEAVE('i', 0);\r
215         return 0;\r
216 }\r
217 \r
218 int PE_GetSymbol(void *Base, const char *Name, Uint *Ret)\r
219 {\r
220         return 0;\r
221 }\r

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