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