| 1 | /* $Id: pe2lx.h,v 1.6 1999-11-10 01:45:33 bird Exp $ | 
|---|
| 2 | * | 
|---|
| 3 | * Pe2Lx class declarations. Ring 0 and Ring 3 | 
|---|
| 4 | * | 
|---|
| 5 | * Copyright (c) 1998-1999 knut st. osmundsen (knut.stange.osmundsen@pmsc.no) | 
|---|
| 6 | * Copyright (c) 1998 Sander van Leeuwen (sandervl@xs4all.nl) | 
|---|
| 7 | * Copyright (c) 1998 Peter Fitzsimmons | 
|---|
| 8 | * | 
|---|
| 9 | * Project Odin Software License can be found in LICENSE.TXT | 
|---|
| 10 | * | 
|---|
| 11 | */ | 
|---|
| 12 | #ifndef _PE2LX_H_ | 
|---|
| 13 | #define _PE2LX_H_ | 
|---|
| 14 |  | 
|---|
| 15 |  | 
|---|
| 16 | /******************************************************************************* | 
|---|
| 17 | *   Defined Constants And Macros                                               * | 
|---|
| 18 | *******************************************************************************/ | 
|---|
| 19 | /* | 
|---|
| 20 | * BufferedRVAReader config | 
|---|
| 21 | */ | 
|---|
| 22 | #define BUFFEREDRVAREADER_BUFFERSIZE PAGESIZE   /* reader code assumes this size... */ | 
|---|
| 23 |  | 
|---|
| 24 |  | 
|---|
| 25 |  | 
|---|
| 26 | /******************************************************************************* | 
|---|
| 27 | *   Structures and Typedefs                                                    * | 
|---|
| 28 | *******************************************************************************/ | 
|---|
| 29 | /** | 
|---|
| 30 | * LXObject is use to store objects in the virtual LX file. | 
|---|
| 31 | */ | 
|---|
| 32 | typedef struct LXObject | 
|---|
| 33 | { | 
|---|
| 34 | ULONG ulRVA;                /* Object relative virtual address  */ | 
|---|
| 35 | ULONG cbPhysical;           /* Physical size        */ | 
|---|
| 36 | ULONG cbVirtual;            /* Virtual size         */ | 
|---|
| 37 | ULONG flFlags;              /* LX flags!            */ | 
|---|
| 38 | struct | 
|---|
| 39 | { | 
|---|
| 40 | ULONG offTIBFix : 30;   /* TIBfix offset (from the object start) */ | 
|---|
| 41 | ULONG fTIBFixObject : 1;/* Set: The object contains the tibfix. Clear: not tib fix object. */ | 
|---|
| 42 | ULONG fStackObject : 1; /* Set: The object is the stack object. Clear: not the stack object. */ | 
|---|
| 43 | } Misc; | 
|---|
| 44 | ULONG offPEFile;            /* section offset into real PE file. */ | 
|---|
| 45 | ULONG offLXFile;            /* object offset into virtual LX file. */ | 
|---|
| 46 | } LXOBJECT, *PLXOBJECT; | 
|---|
| 47 | typedef const LXOBJECT *PCLXOBJECT; | 
|---|
| 48 |  | 
|---|
| 49 |  | 
|---|
| 50 |  | 
|---|
| 51 | /** | 
|---|
| 52 | * Pe2Lx class. Reads a PE executable image and creates a virtual LX file which | 
|---|
| 53 | * can be read from and (RING3) dumped to file. | 
|---|
| 54 | * | 
|---|
| 55 | * @author      knut st. osmundsen | 
|---|
| 56 | * @approval    knut st. osmundsen | 
|---|
| 57 | */ | 
|---|
| 58 | class Pe2Lx : public ModuleBase | 
|---|
| 59 | { | 
|---|
| 60 | public: | 
|---|
| 61 | /** @cat Constructor/Destructor */ | 
|---|
| 62 | Pe2Lx(SFN hFile); | 
|---|
| 63 | ~Pe2Lx(); | 
|---|
| 64 |  | 
|---|
| 65 | /** @cat Public Main methods */ | 
|---|
| 66 | ULONG  init(PCSZ pszFilename); | 
|---|
| 67 | ULONG  read(ULONG offLXFile, PVOID pvBuffer, ULONG cbToRead, ULONG flFlags, PMTE pMTE); | 
|---|
| 68 | #ifndef RING0 | 
|---|
| 69 | ULONG  writeLxFile(PCSZ pszLXFilename); | 
|---|
| 70 | #endif | 
|---|
| 71 |  | 
|---|
| 72 | /** @cat public Helper methods */ | 
|---|
| 73 | ULONG  querySizeOfLxFile(); | 
|---|
| 74 | VOID   dumpVirtualLxFile(); | 
|---|
| 75 |  | 
|---|
| 76 | private: | 
|---|
| 77 | /** @cat conversion methods */ | 
|---|
| 78 | ULONG       makeObjectTable(); | 
|---|
| 79 | ULONG       makeObjectPageTable(); | 
|---|
| 80 | ULONG       makeFixups(); | 
|---|
| 81 | ULONG       makeExports(); | 
|---|
| 82 |  | 
|---|
| 83 | /** @cat conversion helper(s) */ | 
|---|
| 84 | ULONG       loadNtHeaders(); | 
|---|
| 85 | VOID        releaseNtHeaders(); | 
|---|
| 86 |  | 
|---|
| 87 | /** @cat init() helper methods - may only be called at init time! */ | 
|---|
| 88 | ULONG       addObject(ULONG ulRVA, ULONG cbPhysical, ULONG cbVirtual, ULONG flFlags, ULONG offPEFile); | 
|---|
| 89 | ULONG       addTIBFixObject(); | 
|---|
| 90 | ULONG       addStackObject(ULONG cbStack); | 
|---|
| 91 |  | 
|---|
| 92 | /** @cat PE helper methods */ | 
|---|
| 93 |  | 
|---|
| 94 | /** @cat Fixups Helpers - calls allowed from makeFixups only! */ | 
|---|
| 95 | ULONG       initFixups(); | 
|---|
| 96 | ULONG       addPageFixupEntry(BOOL fLast = FALSE); | 
|---|
| 97 | ULONG       add32OffsetFixup(WORD offSource, ULONG ulTarget); | 
|---|
| 98 | ULONG       add32OrdImportFixup(WORD offSource, ULONG ulModuleOrdinal, ULONG ulFunctionOrdinal); | 
|---|
| 99 | ULONG       add32NameImportFixup(WORD offSource, ULONG ulModuleOrdinal, PCSZ pszFnName); | 
|---|
| 100 | ULONG       addModule(PCSZ pszModuleName, PULONG pulModuleOrdinal); | 
|---|
| 101 | ULONG       addImportFunctionName(PCSZ pszFnName, PULONG poffFnName); | 
|---|
| 102 | VOID        finalizeFixups(); | 
|---|
| 103 | VOID        finalizeImportNames(); | 
|---|
| 104 |  | 
|---|
| 105 | /** @cat Entry table / Resident nametable helpers */ | 
|---|
| 106 | ULONG       initEntry(); | 
|---|
| 107 | ULONG       addResName(ULONG ulOrdinal, PCSZ pszName, ULONG cchName); | 
|---|
| 108 | ULONG       addEntry(ULONG ulOrdinal, ULONG ulRVA); | 
|---|
| 109 | ULONG       addForwarderEntry(ULONG ulOrdinal, PCSZ pszDllName, PCSZ pszFnNameOrOrd); | 
|---|
| 110 | ULONG       addLastEntry(); | 
|---|
| 111 | VOID        finalizeExports(); | 
|---|
| 112 |  | 
|---|
| 113 | /** @cat Misc helpers */ | 
|---|
| 114 | ULONG       getCountOfPages(); | 
|---|
| 115 | ULONG       queryObjectAndOffset(ULONG ulRVA, PULONG pulObject, PULONG poffObject); | 
|---|
| 116 |  | 
|---|
| 117 | /** @cat static helpers */ | 
|---|
| 118 | static PCSZ queryOdin32ModuleName(PCSZ pszWin32ModuleName); | 
|---|
| 119 |  | 
|---|
| 120 | /** @cat static dump methods */ | 
|---|
| 121 | static VOID dumpNtHeaders(PIMAGE_NT_HEADERS pNtHdrs); | 
|---|
| 122 | static VOID dumpSectionHeader(PIMAGE_SECTION_HEADER pSection); | 
|---|
| 123 |  | 
|---|
| 124 | private: | 
|---|
| 125 | /** @cat private data members - allways present.  */ | 
|---|
| 126 | BOOL        fAllInOneObject;        /* The All-in-object fix will be or is applied. */ | 
|---|
| 127 | PLXOBJECT   paObjects;              /* Pointer to object array. */ | 
|---|
| 128 | USHORT      cObjects;               /* Count of elements in the object array. */ | 
|---|
| 129 | USHORT      cObjectsAllocated;      /* Size of the object array.  */ | 
|---|
| 130 |  | 
|---|
| 131 | /** | 
|---|
| 132 | * @cat LX structures | 
|---|
| 133 | */ | 
|---|
| 134 | struct e32_exe     LXHdr;           /* Lxheader */ | 
|---|
| 135 |  | 
|---|
| 136 | struct o32_obj    *paObjTab;        /* Pointer to object table - if null check cObjects > 0 and generate it using makeObjectTable */ | 
|---|
| 137 | struct o32_map    *paObjPageTab;    /* Pointer to object page table - if null check cObjects > 0 and generate it using makeObjectPageTable */ | 
|---|
| 138 |  | 
|---|
| 139 | PCHAR              pachResNameTable; /* Pointer to resident name table. */ | 
|---|
| 140 | ULONG              offCurResName;    /* Offset of the next entry in the resident name table. */ | 
|---|
| 141 | ULONG              cchRNTAllocated;  /* Count of char allocated for the resident name table. */ | 
|---|
| 142 |  | 
|---|
| 143 | struct b32_bundle *pEntryBundles;       /* Pointer to entry bundles. (exports) */ | 
|---|
| 144 | ULONG              offCurEntryBundle;   /* Offset of the next bundle. */ | 
|---|
| 145 | ULONG              offLastEntryBundle;  /* Offset of the last entry bundle. */ | 
|---|
| 146 | ULONG              ulLastOrdinal;       /* Ordinal number of last entry which was added. */ | 
|---|
| 147 | ULONG              cbEBAllocated;       /* Count of bytes allocate for entry bundles. */ | 
|---|
| 148 | BOOL               fForwarders;         /* Set if forwarders are present. */ | 
|---|
| 149 |  | 
|---|
| 150 | PULONG             paulFixupPageTable;  /* Pointer to fixup pagetable. If null generate it using makeFixups. */ | 
|---|
| 151 | ULONG              cFixupPTEntries;     /* Number of entries in the fixup page table. */ | 
|---|
| 152 | ULONG              cFPTEAllocated;      /* Number of page table entries allocated. */ | 
|---|
| 153 |  | 
|---|
| 154 | PVOID              pFixupRecords;       /* Pointer to fixup records. If null generate it using makeFixups. */ | 
|---|
| 155 | ULONG              offCurFixupRec;      /* Offset of next fixup. */ | 
|---|
| 156 | ULONG              cbFRAllocated;       /* Count of bytes allocated for Fixup records. */ | 
|---|
| 157 |  | 
|---|
| 158 | PVOID              pvCrossPageFixup;    /* Pointer to cross page fixup. */ | 
|---|
| 159 | ULONG              cbCrossPageFixup;    /* Fixup size in bytes. */ | 
|---|
| 160 |  | 
|---|
| 161 | PCHAR              pachImpModuleNames;  /* Pointer to list of module names. */ | 
|---|
| 162 | ULONG              offCurImpModuleName; /* Offset of next modulename. */ | 
|---|
| 163 | ULONG              cchIMNAllocated;     /* Count of chars allocated for pachImpModuleNames. */ | 
|---|
| 164 |  | 
|---|
| 165 | PCHAR              pachImpFunctionNames;  /* Pointer to list of function names. */ | 
|---|
| 166 | ULONG              offCurImpFunctionName; /* Offset of next functionname. */ | 
|---|
| 167 | ULONG              cchIFNAllocated;       /* Count of chars allocated for pachImpFunctionNames. */ | 
|---|
| 168 |  | 
|---|
| 169 | /** | 
|---|
| 170 | * @cat PE structues | 
|---|
| 171 | */ | 
|---|
| 172 | ULONG                   offNtHeaders;   /* Fileoffset of the PE\0\0 signature. */ | 
|---|
| 173 | PIMAGE_NT_HEADERS       pNtHdrs;        /* Pointer to NT-Headers. If null load it using loadNtHeaders. */ | 
|---|
| 174 | ULONG                   ulImageBase;    /* Image base address. */ | 
|---|
| 175 |  | 
|---|
| 176 | /** | 
|---|
| 177 | * @cat static data. | 
|---|
| 178 | */ | 
|---|
| 179 | static struct LieListEntry              /* Dll Name lie table. */ | 
|---|
| 180 | { | 
|---|
| 181 | PCSZ pszWin32Name;                  /* Win32 dll name. */ | 
|---|
| 182 | PCSZ pszOdin32Name;                 /* Odin32 dll name. */ | 
|---|
| 183 | } paLieList[]; | 
|---|
| 184 |  | 
|---|
| 185 | static struct PeCharacteristicsToLxFlags/* section characteristics to object flags */ | 
|---|
| 186 | { | 
|---|
| 187 | unsigned int Characteristics;       /* set of section characteristics */ | 
|---|
| 188 | ULONG flFlags;                      /* equivalent object flags */ | 
|---|
| 189 | } paSecChars2Flags[]; | 
|---|
| 190 | }; | 
|---|
| 191 |  | 
|---|
| 192 |  | 
|---|
| 193 | /** | 
|---|
| 194 | * BufferedRVARead - read at RVA with buffering. | 
|---|
| 195 | * @author      knut st. osmundsen | 
|---|
| 196 | * @approval    knut st. osmundsen | 
|---|
| 197 | */ | 
|---|
| 198 | class BufferedRVARead | 
|---|
| 199 | { | 
|---|
| 200 | public: | 
|---|
| 201 | BufferedRVARead(SFN hFile, ULONG cObjects, PCLXOBJECT paObjects); | 
|---|
| 202 | #if 0 | 
|---|
| 203 | ULONG readAtRVA(ULONG ulRVA, PVOID pvBuffer, ULONG cbBuffer) | 
|---|
| 204 | #else | 
|---|
| 205 | /** | 
|---|
| 206 | * Reads a chunk of data at the spcified RVA. | 
|---|
| 207 | * @returns   NO_ERROR on success. | 
|---|
| 208 | *            ERROR_INVALID_PARAMETER | 
|---|
| 209 | *            <Whatever rc ReadAt returns> | 
|---|
| 210 | * @param     ulRVA     RVA to read from. Within the filesize. | 
|---|
| 211 | * @param     pvBuffer  Pointer to output buffer. pvBuffer > 64KB | 
|---|
| 212 | * @param     cbBuffer  Number of bytes to read. 0 < cbBuffer > 256MB | 
|---|
| 213 | * @status    completely | 
|---|
| 214 | * @author    knut st. osmundsen | 
|---|
| 215 | */ | 
|---|
| 216 | inline ULONG readAtRVA(ULONG ulRVA, PVOID pvBuffer, ULONG cbBuffer) | 
|---|
| 217 | { | 
|---|
| 218 | /* | 
|---|
| 219 | * five cases: | 
|---|
| 220 | *  1) entire area is within the buffer. | 
|---|
| 221 | *  2) start of area is within the buffer. | 
|---|
| 222 | *  3) end of area is within the buffer. | 
|---|
| 223 | *  4) the area is larger than the buffer, covering it. | 
|---|
| 224 | *  5) the area is outside the buffer. | 
|---|
| 225 | * | 
|---|
| 226 | * these are optimal: 1, 2, and 5. | 
|---|
| 227 | * The request is allways process from start to end. This will make case 3 and 4 less effecient. | 
|---|
| 228 | */ | 
|---|
| 229 | #ifdef DEBUG | 
|---|
| 230 | if (ulRVA == ~0UL || (ULONG)pvBuffer < 0x10000UL || cbBuffer == 0UL || cbBuffer >= 0x10000000UL) | 
|---|
| 231 | return ERROR_INVALID_PARAMETER; | 
|---|
| 232 | #endif | 
|---|
| 233 |  | 
|---|
| 234 | do | 
|---|
| 235 | { | 
|---|
| 236 | if (ulRVA >= this->ulRVA && ulRVA < this->ulRVA + sizeof(achBuffer)) | 
|---|
| 237 | {   /* in buffer */ | 
|---|
| 238 | register ULONG cbRead = sizeof(achBuffer) - (ulRVA - this->ulRVA); | 
|---|
| 239 | cbRead = min(cbRead, cbBuffer); | 
|---|
| 240 | memcpy(pvBuffer, &achBuffer[ulRVA - this->ulRVA], (size_t)cbRead); | 
|---|
| 241 | if (cbBuffer == cbRead) | 
|---|
| 242 | return NO_ERROR; | 
|---|
| 243 | cbBuffer -= cbRead; | 
|---|
| 244 | pvBuffer = (PVOID)((ULONG)pvBuffer + cbRead); | 
|---|
| 245 | ulRVA += cbRead; | 
|---|
| 246 | } | 
|---|
| 247 | else | 
|---|
| 248 | {   /* not in buffer, then read it into the buffer! */ | 
|---|
| 249 | APIRET rc = readToBuffer(ulRVA); | 
|---|
| 250 | if (rc != NO_ERROR) | 
|---|
| 251 | return rc; | 
|---|
| 252 | } | 
|---|
| 253 | } while (cbBuffer != 0UL); | 
|---|
| 254 |  | 
|---|
| 255 | return NO_ERROR; | 
|---|
| 256 | } | 
|---|
| 257 | #endif | 
|---|
| 258 |  | 
|---|
| 259 | ULONG dupString(ULONG ulRVA, PSZ *ppsz); | 
|---|
| 260 | BufferedRVARead & operator =(BufferedRVARead &SrcObj); | 
|---|
| 261 |  | 
|---|
| 262 | private: | 
|---|
| 263 | ULONG readToBuffer(ULONG ulRVA); | 
|---|
| 264 |  | 
|---|
| 265 | private: | 
|---|
| 266 | SFN   hFile;                /* Filehandle. */ | 
|---|
| 267 | ULONG cObjects;             /* Count of objects */ | 
|---|
| 268 | PCLXOBJECT paObjects;       /* Pointer to a readonly array of objects. */ | 
|---|
| 269 | ULONG ulRVA;                /* RVA for the buffer start */ | 
|---|
| 270 | CHAR  achBuffer[BUFFEREDRVAREADER_BUFFERSIZE];  /* Buffer. NOTE! Code assumes that it is a page. */ | 
|---|
| 271 | }; | 
|---|
| 272 |  | 
|---|
| 273 | #endif | 
|---|