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