| 1 | /* $Id: winimage.h,v 1.3 1999-07-07 08:11:09 sandervl Exp $ */ | 
|---|
| 2 |  | 
|---|
| 3 | /* | 
|---|
| 4 | * | 
|---|
| 5 | * Project Odin Software License can be found in LICENSE.TXT | 
|---|
| 6 | * | 
|---|
| 7 | */ | 
|---|
| 8 | /* | 
|---|
| 9 | * Win32 PE Image class | 
|---|
| 10 | * | 
|---|
| 11 | * Copyright 1998 Sander van Leeuwen (sandervl@xs4all.nl) | 
|---|
| 12 | * | 
|---|
| 13 | */ | 
|---|
| 14 | #ifndef __WINIMAGE_H__ | 
|---|
| 15 | #define __WINIMAGE_H__ | 
|---|
| 16 |  | 
|---|
| 17 | #include <peexe.h> | 
|---|
| 18 |  | 
|---|
| 19 | #define MAX_RES 17 | 
|---|
| 20 | extern char *ResTypes[MAX_RES]; | 
|---|
| 21 |  | 
|---|
| 22 |  | 
|---|
| 23 | #ifndef __PE2LX__ | 
|---|
| 24 |  | 
|---|
| 25 | #ifdef DEBUG | 
|---|
| 26 | #define MAGIC_WINIMAGE          0x11223344 | 
|---|
| 27 | #endif | 
|---|
| 28 |  | 
|---|
| 29 | #pragma pack(1) | 
|---|
| 30 | typedef struct { | 
|---|
| 31 | int    id; | 
|---|
| 32 | char   name[1]; | 
|---|
| 33 | } NameId; | 
|---|
| 34 | #pragma pack() | 
|---|
| 35 |  | 
|---|
| 36 | #define ERROR_INTERNAL          1 | 
|---|
| 37 |  | 
|---|
| 38 | #define SECTION_CODE            1 | 
|---|
| 39 | #define SECTION_INITDATA        2 | 
|---|
| 40 | #define SECTION_UNINITDATA      4 | 
|---|
| 41 | #define SECTION_READONLYDATA    8 | 
|---|
| 42 | #define SECTION_IMPORT          16 | 
|---|
| 43 | #define SECTION_RESOURCE        32 | 
|---|
| 44 | #define SECTION_RELOC           64 | 
|---|
| 45 | #define SECTION_EXPORT          128 | 
|---|
| 46 | #define SECTION_DEBUG           256 | 
|---|
| 47 |  | 
|---|
| 48 | #define PAGE_SIZE               4096 | 
|---|
| 49 |  | 
|---|
| 50 | #define MAX_SECTION             64  /*PLF Mon  98-02-09 23:47:16*/ | 
|---|
| 51 |  | 
|---|
| 52 | #define LANG_GETFIRST           0x80000000 | 
|---|
| 53 |  | 
|---|
| 54 | #define NO_NAMETABLE            0x77777777 | 
|---|
| 55 | #define NO_LOOKUPTABLE          0x888888 | 
|---|
| 56 | #define GET_CONSOLE(a)          (a >> 24) | 
|---|
| 57 | #define SET_CONSOLE(a)          (a << 24) | 
|---|
| 58 |  | 
|---|
| 59 | typedef struct { | 
|---|
| 60 | char  *rawdata; | 
|---|
| 61 | ULONG  rawsize; | 
|---|
| 62 | ULONG  virtaddr; | 
|---|
| 63 | ULONG  realvirtaddr;  //as allocated in OS/2 | 
|---|
| 64 | ULONG  virtualsize; | 
|---|
| 65 | ULONG  type; | 
|---|
| 66 | } Section; | 
|---|
| 67 |  | 
|---|
| 68 | typedef struct { | 
|---|
| 69 | ULONG  virtaddr; | 
|---|
| 70 | ULONG  ordinal; | 
|---|
| 71 | ULONG  nlength; | 
|---|
| 72 | char   name[4]; | 
|---|
| 73 | } NameExport; | 
|---|
| 74 |  | 
|---|
| 75 | typedef struct { | 
|---|
| 76 | ULONG  virtaddr; | 
|---|
| 77 | ULONG  ordinal; | 
|---|
| 78 | } OrdExport; | 
|---|
| 79 |  | 
|---|
| 80 | class Win32Dll; | 
|---|
| 81 | class Win32Resource; | 
|---|
| 82 |  | 
|---|
| 83 | class Win32Image | 
|---|
| 84 | { | 
|---|
| 85 | #ifdef DEBUG | 
|---|
| 86 | protected: | 
|---|
| 87 | DWORD magic; | 
|---|
| 88 | public: | 
|---|
| 89 | void checkObject() | 
|---|
| 90 | { | 
|---|
| 91 | if(magic != MAGIC_WINIMAGE) { | 
|---|
| 92 | eprintf(("Corrupt this pointer %X %X!!", this, magic)); | 
|---|
| 93 | DebugInt3(); | 
|---|
| 94 | } | 
|---|
| 95 | }; | 
|---|
| 96 | #endif | 
|---|
| 97 |  | 
|---|
| 98 | public: | 
|---|
| 99 | // Constructors and destructors | 
|---|
| 100 | Win32Image(HINSTANCE hinstance, int NameTableId, int Win32TableId); | 
|---|
| 101 | Win32Image(char *szFileName); | 
|---|
| 102 | virtual ~Win32Image(); | 
|---|
| 103 |  | 
|---|
| 104 | //called to reset object to native OS/2 or converted win32 dll | 
|---|
| 105 | void OS2ImageInit(HINSTANCE hinstance, int NameTableId, int Win32TableId); | 
|---|
| 106 |  | 
|---|
| 107 | virtual BOOL  init(); | 
|---|
| 108 |  | 
|---|
| 109 | ULONG getError()              { return errorState; }; | 
|---|
| 110 | HINSTANCE getInstanceHandle() { return hinstance; }; | 
|---|
| 111 |  | 
|---|
| 112 | virtual void setFullPath(char *name); | 
|---|
| 113 | char *getFullPath()           { return fullpath; }; | 
|---|
| 114 |  | 
|---|
| 115 | HRSRC   findResourceA(LPCSTR lpszName, LPSTR lpszType); | 
|---|
| 116 | HRSRC   findResourceW(LPWSTR lpszName, LPWSTR lpszType); | 
|---|
| 117 |  | 
|---|
| 118 | int   getWin32ResourceId(int id); | 
|---|
| 119 | int   convertNameId(char *lpszName); | 
|---|
| 120 |  | 
|---|
| 121 | static  BOOL  isPEImage(char *szFileName); | 
|---|
| 122 |  | 
|---|
| 123 | void  setVersionId(int id) { VersionId = id;   }; | 
|---|
| 124 | int   getVersionId()       { return VersionId; }; | 
|---|
| 125 |  | 
|---|
| 126 | void  setEntryPoint(ULONG startAddress) { entryPoint = startAddress; }; | 
|---|
| 127 |  | 
|---|
| 128 | void  setTLSAddress(LPVOID dwTlsAddress)        { tlsAddress = dwTlsAddress; }; | 
|---|
| 129 | void  setTLSIndexAddr(LPDWORD dwTlsIndexAddr)   { tlsIndexAddr = dwTlsIndexAddr; }; | 
|---|
| 130 | void  setTLSInitSize(ULONG dwTlsSize)           { tlsInitSize = dwTlsSize; }; | 
|---|
| 131 | void  setTLSTotalSize(ULONG dwTlsSize)          { tlsTotalSize = dwTlsSize; }; | 
|---|
| 132 | void  setTLSCallBackAddr(PIMAGE_TLS_CALLBACK *dwTlsCallBackAddr) | 
|---|
| 133 | { | 
|---|
| 134 | tlsCallBackAddr = dwTlsCallBackAddr; | 
|---|
| 135 | }; | 
|---|
| 136 |  | 
|---|
| 137 | void  tlsAttachThread();        //setup TLS structures for new thread | 
|---|
| 138 | void  tlsDetachThread();        //destroy TLS structures | 
|---|
| 139 |  | 
|---|
| 140 | protected: | 
|---|
| 141 | void tlsAlloc();                //Allocate TLS index for this module | 
|---|
| 142 | void tlsDelete();               //Destroy TLS index for this module | 
|---|
| 143 |  | 
|---|
| 144 | void StoreImportByOrd(Win32Dll *WinDll, ULONG ordinal, ULONG impaddr); | 
|---|
| 145 | void StoreImportByName(Win32Dll *WinDll, char *impname, ULONG impaddr); | 
|---|
| 146 |  | 
|---|
| 147 | void  addSection(ULONG type, char *rawdata, ULONG rawsize, ULONG virtaddress, ULONG virtsize); | 
|---|
| 148 | BOOL  allocSections(); | 
|---|
| 149 | BOOL  allocFixedMem(); | 
|---|
| 150 |  | 
|---|
| 151 | BOOL  storeSections(); | 
|---|
| 152 | BOOL  setMemFlags(); | 
|---|
| 153 | BOOL  setFixups(PIMAGE_BASE_RELOCATION prel); | 
|---|
| 154 | void  AddOff32Fixup(ULONG fixupaddr); | 
|---|
| 155 | void  AddOff16Fixup(ULONG fixupaddr, BOOL fHighFixup); | 
|---|
| 156 |  | 
|---|
| 157 | BOOL  processImports(char *win32file); | 
|---|
| 158 |  | 
|---|
| 159 | BOOL  processExports(char *win32file); | 
|---|
| 160 | void  AddNameExport(ULONG virtaddr, char *apiname, ULONG ordinal); | 
|---|
| 161 | void  AddOrdExport(ULONG virtaddr, ULONG ordinal); | 
|---|
| 162 |  | 
|---|
| 163 | Win32Resource *getPEResource(ULONG id, ULONG type, ULONG lang = LANG_GETFIRST); | 
|---|
| 164 | PIMAGE_RESOURCE_DATA_ENTRY ProcessResSubDir(PIMAGE_RESOURCE_DIRECTORY prdType, | 
|---|
| 165 | ULONG *nodeData); | 
|---|
| 166 | PIMAGE_RESOURCE_DIRECTORY pResDir; | 
|---|
| 167 | Section                  *pResSection; | 
|---|
| 168 | Win32Resource            *winres; | 
|---|
| 169 |  | 
|---|
| 170 | IMAGE_OPTIONAL_HEADER oh; | 
|---|
| 171 | IMAGE_FILE_HEADER     fh; | 
|---|
| 172 |  | 
|---|
| 173 | ULONG                 errorState, entryPoint; | 
|---|
| 174 | ULONG                 nrNameExports, nameExportSize; | 
|---|
| 175 | ULONG                 nrOrdExports; | 
|---|
| 176 | NameExport           *nameexports, *curnameexport; | 
|---|
| 177 | OrdExport            *ordexports, *curordexport; | 
|---|
| 178 |  | 
|---|
| 179 | ULONG                 nrsections, imageSize, imageVirtBase, imageVirtEnd; | 
|---|
| 180 | //OS/2 virtual base address | 
|---|
| 181 | ULONG                 baseAddress, realBaseAddress; | 
|---|
| 182 | Section               section[MAX_SECTION]; | 
|---|
| 183 |  | 
|---|
| 184 | char                 *szFileName, *fullpath; | 
|---|
| 185 |  | 
|---|
| 186 | HINSTANCE             hinstance; | 
|---|
| 187 |  | 
|---|
| 188 | int                   NameTableId; | 
|---|
| 189 | int                   Win32TableId; | 
|---|
| 190 | int                   VersionId; | 
|---|
| 191 |  | 
|---|
| 192 | ULONG                *Win32Table; | 
|---|
| 193 | NameId               *NameTable; | 
|---|
| 194 |  | 
|---|
| 195 | BOOL                  fNativePEImage; | 
|---|
| 196 |  | 
|---|
| 197 | LPVOID                  tlsAddress;             //address of TLS data | 
|---|
| 198 | LPDWORD                 tlsIndexAddr;           //address of DWORD that receives the TLS index | 
|---|
| 199 | ULONG                   tlsInitSize;            //size of initialized TLS memory block | 
|---|
| 200 | ULONG                   tlsTotalSize;           //size of TLS memory block | 
|---|
| 201 | PIMAGE_TLS_CALLBACK    *tlsCallBackAddr;        //ptr to TLS callback array | 
|---|
| 202 | ULONG                   tlsIndex;               //module TLS index | 
|---|
| 203 |  | 
|---|
| 204 | private: | 
|---|
| 205 |  | 
|---|
| 206 | friend class Win32Resource; | 
|---|
| 207 | friend ULONG SYSTEM GetVersionSize(char *modname); | 
|---|
| 208 | }; | 
|---|
| 209 |  | 
|---|
| 210 | #include <iostream.h> | 
|---|
| 211 | #include <fstream.h> | 
|---|
| 212 | extern ofstream fout; | 
|---|
| 213 |  | 
|---|
| 214 | #endif //__PE2LX__ | 
|---|
| 215 |  | 
|---|
| 216 | #endif | 
|---|