Changeset 8003 for trunk/tools/common/kFilePE.cpp
- Timestamp:
- Feb 24, 2002, 3:47:28 AM (24 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tools/common/kFilePE.cpp
r5531 r8003 1 /* 1 /* $Id: kFilePE.cpp,v 1.5 2002-02-24 02:47:26 bird Exp $ 2 * 2 3 * kFilePE - PE files. 3 4 * … … 6 7 */ 7 8 8 /*******************************************************************************9 * Defined Constants *10 *******************************************************************************/11 /* emx fixups */12 #ifdef __EMX__13 #define __stdcall14 #define max(a,b) (((a) > (b)) ? (a) : (b))15 #define min(a,b) (((a) < (b)) ? (a) : (b))16 #endif17 18 9 /****************************************************************************** 19 10 * Header Files * 20 11 ******************************************************************************/ 21 #ifdef __EMX__22 #define INT INT_23 #define PCHAR PCHAR_24 #endif25 #include <os2.h>26 #ifdef __EMX__27 #undef PCHAR28 #undef INT29 #endif30 #include <stdio.h>31 #include <stdlib.h>32 12 #include <string.h> 33 #include <malloc.h> 34 #include <assert.h> 35 #include <peexe.h> 13 14 #include "MZexe.h" 15 #include "PEexe.h" 16 17 #include "kTypes.h" 18 #include "kError.h" 36 19 #include "kFile.h" 37 20 #include "kFileFormatBase.h" 38 #include "kInterfaces.h" 39 #include "kFilePe.h" 21 #include "kFileInterfaces.h" 22 #include "kFilePE.h" 23 40 24 41 25 /******************************************************************************* … … 46 30 #endif 47 31 32 48 33 /** 49 34 * Constructs a kFilePE object for a file. 50 35 * @param pFile File to create object from. 51 * @remark throws errorcode (TODO: errorhandling.) 52 */ 53 kFilePE::kFilePE(kFile *pFile) throw(int) : pvBase(NULL), 36 * @remark throws errorcode 37 */ 38 kFilePE::kFilePE(kFile *pFile) : 39 kFileFormatBase(pFile), 40 pvBase(NULL), 54 41 pDosHdr(NULL), pFileHdr(NULL), pOptHdr(NULL), paDataDir(NULL), paSectionHdr(NULL), 55 42 pExportDir(NULL), … … 67 54 { 68 55 IMAGE_DOS_HEADER doshdr; 69 70 /* read dos-header - assumes there is one */ 71 if ( pFile->readAt(&doshdr, sizeof(doshdr), 0) 72 && doshdr.e_magic == IMAGE_DOS_SIGNATURE 73 && doshdr.e_lfanew > sizeof(doshdr) 74 ) 75 { 76 IMAGE_NT_HEADERS pehdr; 77 78 /* read pe headers */ 79 if ( pFile->readAt(&pehdr, sizeof(pehdr), doshdr.e_lfanew) 80 && pehdr.Signature == IMAGE_NT_SIGNATURE 81 && pehdr.FileHeader.SizeOfOptionalHeader == sizeof(IMAGE_OPTIONAL_HEADER) 82 && pehdr.OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR_MAGIC) 83 { 56 long offPEHdr = 0; 57 IMAGE_NT_HEADERS pehdr; 58 59 /* read dos-header (If tehre is one) */ 60 pFile->setThrowOnErrors(); 61 pFile->readAt(&doshdr, sizeof(doshdr), 0); 62 if (doshdr.e_magic == IMAGE_DOS_SIGNATURE) 63 { 64 if (doshdr.e_lfanew <= sizeof(doshdr)) 65 throw(kError(kError::INVALID_EXE_SIGNATURE)); 66 offPEHdr = doshdr.e_lfanew; 67 } 68 69 /* read pe headers and do minor verifications */ 70 pFile->readAt(&pehdr, sizeof(pehdr), offPEHdr); 71 if (pehdr.Signature != IMAGE_NT_SIGNATURE) 72 throw(kError(kError::INVALID_EXE_SIGNATURE)); 73 if ( pehdr.FileHeader.SizeOfOptionalHeader != sizeof(IMAGE_OPTIONAL_HEADER) 74 || pehdr.OptionalHeader.Magic != IMAGE_NT_OPTIONAL_HDR_MAGIC) 75 throw(kError(kError::BAD_EXE_FORMAT)); 76 84 77 /* create mapping */ 85 pvBase = calloc((size_t)pehdr.OptionalHeader.SizeOfImage, 1); 86 if (pvBase != NULL) 87 { 78 pvBase = new char [pehdr.OptionalHeader.SizeOfImage]; 79 if (pvBase == NULL) 80 throw(kError(kError::NOT_ENOUGH_MEMORY)); 81 memset(pvBase, 0, pehdr.OptionalHeader.SizeOfImage); 82 88 83 /* 89 84 printf("%ld\n", pehdr.OptionalHeader.SizeOfHeaders); 90 85 printf("%ld\n", sizeof(IMAGE_NT_HEADERS) + sizeof(IMAGE_SECTION_HEADER) * pehdr.FileHeader.NumberOfSections); 91 assert(pehdr.OptionalHeader.SizeOfHeaders ==86 kASSERT(pehdr.OptionalHeader.SizeOfHeaders == 92 87 sizeof(IMAGE_NT_HEADERS) + sizeof(IMAGE_SECTION_HEADER) * pehdr.FileHeader.NumberOfSections); 93 88 */ 94 if (pFile->readAt(pvBase, pehdr.OptionalHeader.SizeOfHeaders, 0))89 try 95 90 { 91 /* read optional header */ 92 pFile->readAt(pvBase, pehdr.OptionalHeader.SizeOfHeaders, 0); 93 96 94 /* read sections */ 97 95 for (int i = 0; i < pehdr.FileHeader.NumberOfSections; i++) 98 96 { 99 ULONGcbSection;97 unsigned long cbSection; 100 98 PIMAGE_SECTION_HEADER pSectionHdr = 101 99 #if 0 102 IMAGE_FIRST_SECTION(((ULONG)pvBase + ((PIMAGE_DOS_HEADER)pvBase)->e_lfanew));100 IMAGE_FIRST_SECTION(((unsigned long)pvBase + ((PIMAGE_DOS_HEADER)pvBase)->e_lfanew)); 103 101 #else 104 (PIMAGE_SECTION_HEADER) ( (ULONG)pvBase + doshdr.e_lfanew + sizeof(IMAGE_NT_HEADERS) );102 (PIMAGE_SECTION_HEADER) ( (unsigned long)pvBase + doshdr.e_lfanew + sizeof(IMAGE_NT_HEADERS) ); 105 103 #endif 106 104 pSectionHdr += i; 107 105 108 cbSection = min(pSectionHdr->Misc.VirtualSize, pSectionHdr->SizeOfRawData); 109 if ( cbSection 110 && !pFile->readAt((char*)pvBase + pSectionHdr->VirtualAddress, 111 cbSection, 112 pSectionHdr->PointerToRawData) 113 ) 114 { 115 /* error */ 116 free(pvBase); 117 pvBase = NULL; 118 throw(6); 119 } 120 } 106 cbSection = KMIN(pSectionHdr->Misc.VirtualSize, pSectionHdr->SizeOfRawData); 107 if (cbSection) 108 pFile->readAt((char*)pvBase + pSectionHdr->VirtualAddress, 109 cbSection, pSectionHdr->PointerToRawData); 110 } 121 111 122 112 /* set pointers */ … … 124 114 { 125 115 pDosHdr = (PIMAGE_DOS_HEADER)pvBase; 126 pFileHdr = (PIMAGE_FILE_HEADER)((DWORD)pvBase + pDosHdr->e_lfanew + 4);116 pFileHdr = (PIMAGE_FILE_HEADER)((char*)pvBase + pDosHdr->e_lfanew + 4); 127 117 } 128 118 else 129 pFileHdr = (PIMAGE_FILE_HEADER)((DWORD)pvBase + 4);119 pFileHdr = (PIMAGE_FILE_HEADER)((char*)pvBase + 4); 130 120 131 121 pOptHdr = (PIMAGE_OPTIONAL_HEADER)((int)pFileHdr + sizeof(*pFileHdr)); … … 142 132 { 143 133 if (paDataDir[i].VirtualAddress < pOptHdr->SizeOfImage) 144 ((PULONG)&this->pExportDir)[i] = (int)pvBase + paDataDir[i].VirtualAddress; 145 #ifdef DEBUG 146 else 147 fprintf(stderr, "bad directory pointer %d\n", i); 148 #endif 134 ((unsigned long*)&this->pExportDir)[i] = (int)pvBase + paDataDir[i].VirtualAddress; 149 135 } 150 136 } 151 137 } 152 else 153 throw(4); 154 } 155 else 156 throw(3); 157 } 158 else 159 throw(2); 160 } 161 else 162 throw(1); 138 catch (kError err) 139 { 140 delete(pvBase); 141 pvBase = NULL; 142 throw(err); 143 } 163 144 } 164 145 … … 170 151 { 171 152 if (pvBase) 172 delete pvBase;153 delete(pvBase); 173 154 } 174 155 … … 180 161 * @param cchBuffer Size of the buffer (defaults to 260 chars). 181 162 */ 182 BOOL kFilePE::moduleGetName(char *pszBuffer, int cchSize/* = 260*/)163 KBOOL kFilePE::moduleGetName(char *pszBuffer, int cchSize/* = 260*/) 183 164 { 184 165 if (pExportDir && pExportDir->Name) … … 203 184 * @remark 204 185 */ 205 BOOLkFilePE::exportFindFirst(kExportEntry *pExport)186 KBOOL kFilePE::exportFindFirst(kExportEntry *pExport) 206 187 { 207 188 if (pExportDir && pExportDir->NumberOfFunctions) … … 222 203 * @remark 223 204 */ 224 BOOLkFilePE::exportFindNext(kExportEntry *pExport)205 KBOOL kFilePE::exportFindNext(kExportEntry *pExport) 225 206 { 226 207 if (pExportDir && pExportDir->NumberOfFunctions) … … 283 264 * @remark stub 284 265 */ 285 BOOL kFilePE::exportLookup(unsigned long ulOrdinal, kExportEntry *pExport)286 { 287 assert(!"not implemented.");266 KBOOL kFilePE::exportLookup(unsigned long ulOrdinal, kExportEntry *pExport) 267 { 268 kASSERT(!"not implemented."); 288 269 ulOrdinal = ulOrdinal; 289 270 pExport = pExport; … … 298 279 * @remark stub 299 280 */ 300 BOOL kFilePE::exportLookup(const char * pszName, kExportEntry *pExport)301 { 302 assert(!"not implemented.");281 KBOOL kFilePE::exportLookup(const char * pszName, kExportEntry *pExport) 282 { 283 kASSERT(!"not implemented."); 303 284 pszName = pszName; 304 285 pExport = pExport; … … 310 291 * Mini dump function. 311 292 */ 312 BOOL kFilePE::dump(kFile *pOut)293 KBOOL kFilePE::dump(kFile *pOut) 313 294 { 314 295 int i,j,k; … … 428 409 if (pTLSDir[i].AddressOfCallBacks) 429 410 { 430 PULONGpaulIndex;431 PULONGpaulCallback;432 ULONGulBorlandRVAFix = 0UL;411 unsigned long * paulIndex; 412 unsigned long * paulCallback; 413 unsigned long ulBorlandRVAFix = 0UL; 433 414 434 415 /* Check if the addresses in the TLSDir is RVAs or real addresses */ … … 437 418 438 419 j = 0; 439 paulIndex = ( PULONG)((ULONG)pTLSDir[i].AddressOfIndex - ulBorlandRVAFix + (ULONG)this->pvBase);440 paulCallback = ( PULONG)((ULONG)pTLSDir[i].AddressOfCallBacks - ulBorlandRVAFix + (ULONG)this->pvBase);420 paulIndex = (unsigned long *)((unsigned long)pTLSDir[i].AddressOfIndex - ulBorlandRVAFix + (unsigned long)this->pvBase); 421 paulCallback = (unsigned long *)((unsigned long)pTLSDir[i].AddressOfCallBacks - ulBorlandRVAFix + (unsigned long)this->pvBase); 441 422 if (*paulCallback) 442 423 {
Note:
See TracChangeset
for help on using the changeset viewer.