| 1 | /* $Id: winimagepeldr.h,v 1.3 1999-10-23 12:36:09 sandervl Exp $ */ | 
|---|
| 2 |  | 
|---|
| 3 | /* | 
|---|
| 4 | * Win32 PE loader Image base class | 
|---|
| 5 | * | 
|---|
| 6 | * Copyright 1998-1999 Sander van Leeuwen (sandervl@xs4all.nl) | 
|---|
| 7 | * | 
|---|
| 8 | * | 
|---|
| 9 | * Project Odin Software License can be found in LICENSE.TXT | 
|---|
| 10 | * | 
|---|
| 11 | */ | 
|---|
| 12 | #ifndef __WINIMAGEPELDR_H__ | 
|---|
| 13 | #define __WINIMAGEPELDR_H__ | 
|---|
| 14 |  | 
|---|
| 15 | #include <winimagebase.h> | 
|---|
| 16 |  | 
|---|
| 17 | //SvL: To load a dll/exe for i.e. getting a single resource (GetVersionSize/Resource) | 
|---|
| 18 | #define REAL_LOAD               0 | 
|---|
| 19 | #define RSRC_LOAD               1 | 
|---|
| 20 |  | 
|---|
| 21 | //SvL: Amount of memory the peldr dll reserves for win32 exes without fixups | 
|---|
| 22 | //(most of them need to be loaded at 4 MB; except MS Office apps of course) | 
|---|
| 23 | #define PELDR_RESERVEDMEMSIZE   16*1024*1024 | 
|---|
| 24 |  | 
|---|
| 25 | #define ERROR_INTERNAL          1 | 
|---|
| 26 |  | 
|---|
| 27 | #define SECTION_CODE            1 | 
|---|
| 28 | #define SECTION_INITDATA        2 | 
|---|
| 29 | #define SECTION_UNINITDATA      4 | 
|---|
| 30 | #define SECTION_READONLYDATA    8 | 
|---|
| 31 | #define SECTION_IMPORT          16 | 
|---|
| 32 | #define SECTION_RESOURCE        32 | 
|---|
| 33 | #define SECTION_RELOC           64 | 
|---|
| 34 | #define SECTION_EXPORT          128 | 
|---|
| 35 | #define SECTION_DEBUG           256 | 
|---|
| 36 | #define SECTION_TLS             512 | 
|---|
| 37 |  | 
|---|
| 38 | #define PAGE_SIZE               4096 | 
|---|
| 39 |  | 
|---|
| 40 | #define MAX_SECTION             64  /*PLF Mon  98-02-09 23:47:16*/ | 
|---|
| 41 |  | 
|---|
| 42 | typedef struct { | 
|---|
| 43 | char  *rawdata; | 
|---|
| 44 | ULONG  rawsize; | 
|---|
| 45 | ULONG  virtaddr; | 
|---|
| 46 | ULONG  realvirtaddr;  //as allocated in OS/2 | 
|---|
| 47 | ULONG  virtualsize; | 
|---|
| 48 | ULONG  type; | 
|---|
| 49 | } Section; | 
|---|
| 50 |  | 
|---|
| 51 | typedef struct { | 
|---|
| 52 | ULONG  virtaddr; | 
|---|
| 53 | ULONG  ordinal; | 
|---|
| 54 | ULONG  nlength; | 
|---|
| 55 | char   name[4]; | 
|---|
| 56 | } NameExport; | 
|---|
| 57 |  | 
|---|
| 58 | typedef struct { | 
|---|
| 59 | ULONG  virtaddr; | 
|---|
| 60 | ULONG  ordinal; | 
|---|
| 61 | } OrdExport; | 
|---|
| 62 |  | 
|---|
| 63 | class Win32DllBase; | 
|---|
| 64 |  | 
|---|
| 65 | class Win32PeLdrImage : public virtual Win32ImageBase | 
|---|
| 66 | { | 
|---|
| 67 | public: | 
|---|
| 68 | Win32PeLdrImage(char *szFileName, int loadtype = REAL_LOAD); | 
|---|
| 69 | virtual ~Win32PeLdrImage(); | 
|---|
| 70 |  | 
|---|
| 71 | //reservedMem is address of memory reserved in peldr.dll (allocated before | 
|---|
| 72 | //any dlls are loaded, so that exes without fixups can be loaded at a low | 
|---|
| 73 | //address) | 
|---|
| 74 | virtual BOOL  init(ULONG reservedMem); | 
|---|
| 75 |  | 
|---|
| 76 | protected: | 
|---|
| 77 | void StoreImportByOrd(Win32DllBase *WinDll, ULONG ordinal, ULONG impaddr); | 
|---|
| 78 | void StoreImportByName(Win32DllBase *WinDll, char *impname, ULONG impaddr); | 
|---|
| 79 |  | 
|---|
| 80 | void  addSection(ULONG type, char *rawdata, ULONG rawsize, ULONG virtaddress, ULONG virtsize); | 
|---|
| 81 | BOOL  allocSections(ULONG reservedMem); | 
|---|
| 82 | BOOL  allocFixedMem(ULONG reservedMem); | 
|---|
| 83 | Section *findSection(ULONG type); | 
|---|
| 84 | Section *findSectionByAddr(ULONG addr); | 
|---|
| 85 |  | 
|---|
| 86 | BOOL  storeSections(char *win32file); | 
|---|
| 87 | BOOL  setMemFlags(); | 
|---|
| 88 | BOOL  setFixups(PIMAGE_BASE_RELOCATION prel); | 
|---|
| 89 | void  AddOff32Fixup(ULONG fixupaddr); | 
|---|
| 90 | void  AddOff16Fixup(ULONG fixupaddr, BOOL fHighFixup); | 
|---|
| 91 |  | 
|---|
| 92 | BOOL  processImports(char *win32file); | 
|---|
| 93 |  | 
|---|
| 94 | BOOL  processExports(char *win32file); | 
|---|
| 95 | void  AddNameExport(ULONG virtaddr, char *apiname, ULONG ordinal); | 
|---|
| 96 | void  AddOrdExport(ULONG virtaddr, ULONG ordinal); | 
|---|
| 97 |  | 
|---|
| 98 | Section                  *pResSection; | 
|---|
| 99 |  | 
|---|
| 100 | IMAGE_OPTIONAL_HEADER oh; | 
|---|
| 101 | IMAGE_FILE_HEADER     fh; | 
|---|
| 102 |  | 
|---|
| 103 | ULONG                 nrNameExports, nameExportSize; | 
|---|
| 104 | ULONG                 nrOrdExports; | 
|---|
| 105 | NameExport           *nameexports, *curnameexport; | 
|---|
| 106 | OrdExport            *ordexports, *curordexport; | 
|---|
| 107 |  | 
|---|
| 108 | ULONG                 nrsections, imageSize, imageVirtBase, imageVirtEnd; | 
|---|
| 109 | //OS/2 virtual base address | 
|---|
| 110 | ULONG                 realBaseAddress; | 
|---|
| 111 | Section               section[MAX_SECTION]; | 
|---|
| 112 |  | 
|---|
| 113 | ULONG                 loadType; | 
|---|
| 114 | HANDLE                fImgMapping; | 
|---|
| 115 |  | 
|---|
| 116 | private: | 
|---|
| 117 | }; | 
|---|
| 118 |  | 
|---|
| 119 | #include <iostream.h> | 
|---|
| 120 | #include <fstream.h> | 
|---|
| 121 | extern ofstream fout; | 
|---|
| 122 |  | 
|---|
| 123 | #endif //__WINIMAGEPELDR_H__ | 
|---|
| 124 |  | 
|---|