| 1 | /* $Id: winimage.h,v 1.4 1999-08-09 22:46:46 phaller 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 | #ifdef DEBUG
|
|---|
| 92 | if(magic != MAGIC_WINIMAGE) {
|
|---|
| 93 | eprintf(("Corrupt this pointer %X %X!!", this, magic));
|
|---|
| 94 | DebugInt3();
|
|---|
| 95 | }
|
|---|
| 96 | #endif
|
|---|
| 97 | };
|
|---|
| 98 | #endif
|
|---|
| 99 |
|
|---|
| 100 | public:
|
|---|
| 101 | // Constructors and destructors
|
|---|
| 102 | Win32Image(HINSTANCE hinstance, int NameTableId, int Win32TableId);
|
|---|
| 103 | Win32Image(char *szFileName);
|
|---|
| 104 | virtual ~Win32Image();
|
|---|
| 105 |
|
|---|
| 106 | //called to reset object to native OS/2 or converted win32 dll
|
|---|
| 107 | void OS2ImageInit(HINSTANCE hinstance, int NameTableId, int Win32TableId);
|
|---|
| 108 |
|
|---|
| 109 | virtual BOOL init();
|
|---|
| 110 |
|
|---|
| 111 | ULONG getError() { return errorState; };
|
|---|
| 112 | HINSTANCE getInstanceHandle() { return hinstance; };
|
|---|
| 113 |
|
|---|
| 114 | virtual void setFullPath(char *name);
|
|---|
| 115 | char *getFullPath() { return fullpath; };
|
|---|
| 116 |
|
|---|
| 117 | HRSRC findResourceA(LPCSTR lpszName, LPSTR lpszType);
|
|---|
| 118 | HRSRC findResourceW(LPWSTR lpszName, LPWSTR lpszType);
|
|---|
| 119 |
|
|---|
| 120 | int getWin32ResourceId(int id);
|
|---|
| 121 | int convertNameId(char *lpszName);
|
|---|
| 122 |
|
|---|
| 123 | static BOOL isPEImage(char *szFileName);
|
|---|
| 124 |
|
|---|
| 125 | void setVersionId(int id) { VersionId = id; };
|
|---|
| 126 | int getVersionId() { return VersionId; };
|
|---|
| 127 |
|
|---|
| 128 | void setEntryPoint(ULONG startAddress) { entryPoint = startAddress; };
|
|---|
| 129 |
|
|---|
| 130 | void setTLSAddress(LPVOID dwTlsAddress) { tlsAddress = dwTlsAddress; };
|
|---|
| 131 | void setTLSIndexAddr(LPDWORD dwTlsIndexAddr) { tlsIndexAddr = dwTlsIndexAddr; };
|
|---|
| 132 | void setTLSInitSize(ULONG dwTlsSize) { tlsInitSize = dwTlsSize; };
|
|---|
| 133 | void setTLSTotalSize(ULONG dwTlsSize) { tlsTotalSize = dwTlsSize; };
|
|---|
| 134 | void setTLSCallBackAddr(PIMAGE_TLS_CALLBACK *dwTlsCallBackAddr)
|
|---|
| 135 | {
|
|---|
| 136 | tlsCallBackAddr = dwTlsCallBackAddr;
|
|---|
| 137 | };
|
|---|
| 138 |
|
|---|
| 139 | void tlsAttachThread(); //setup TLS structures for new thread
|
|---|
| 140 | void tlsDetachThread(); //destroy TLS structures
|
|---|
| 141 |
|
|---|
| 142 | protected:
|
|---|
| 143 | void tlsAlloc(); //Allocate TLS index for this module
|
|---|
| 144 | void tlsDelete(); //Destroy TLS index for this module
|
|---|
| 145 |
|
|---|
| 146 | void StoreImportByOrd(Win32Dll *WinDll, ULONG ordinal, ULONG impaddr);
|
|---|
| 147 | void StoreImportByName(Win32Dll *WinDll, char *impname, ULONG impaddr);
|
|---|
| 148 |
|
|---|
| 149 | void addSection(ULONG type, char *rawdata, ULONG rawsize, ULONG virtaddress, ULONG virtsize);
|
|---|
| 150 | BOOL allocSections();
|
|---|
| 151 | BOOL allocFixedMem();
|
|---|
| 152 |
|
|---|
| 153 | BOOL storeSections();
|
|---|
| 154 | BOOL setMemFlags();
|
|---|
| 155 | BOOL setFixups(PIMAGE_BASE_RELOCATION prel);
|
|---|
| 156 | void AddOff32Fixup(ULONG fixupaddr);
|
|---|
| 157 | void AddOff16Fixup(ULONG fixupaddr, BOOL fHighFixup);
|
|---|
| 158 |
|
|---|
| 159 | BOOL processImports(char *win32file);
|
|---|
| 160 |
|
|---|
| 161 | BOOL processExports(char *win32file);
|
|---|
| 162 | void AddNameExport(ULONG virtaddr, char *apiname, ULONG ordinal);
|
|---|
| 163 | void AddOrdExport(ULONG virtaddr, ULONG ordinal);
|
|---|
| 164 |
|
|---|
| 165 | Win32Resource *getPEResource(ULONG id, ULONG type, ULONG lang = LANG_GETFIRST);
|
|---|
| 166 | PIMAGE_RESOURCE_DATA_ENTRY ProcessResSubDir(PIMAGE_RESOURCE_DIRECTORY prdType,
|
|---|
| 167 | ULONG *nodeData);
|
|---|
| 168 | PIMAGE_RESOURCE_DIRECTORY pResDir;
|
|---|
| 169 | Section *pResSection;
|
|---|
| 170 | Win32Resource *winres;
|
|---|
| 171 |
|
|---|
| 172 | IMAGE_OPTIONAL_HEADER oh;
|
|---|
| 173 | IMAGE_FILE_HEADER fh;
|
|---|
| 174 |
|
|---|
| 175 | ULONG errorState, entryPoint;
|
|---|
| 176 | ULONG nrNameExports, nameExportSize;
|
|---|
| 177 | ULONG nrOrdExports;
|
|---|
| 178 | NameExport *nameexports, *curnameexport;
|
|---|
| 179 | OrdExport *ordexports, *curordexport;
|
|---|
| 180 |
|
|---|
| 181 | ULONG nrsections, imageSize, imageVirtBase, imageVirtEnd;
|
|---|
| 182 | //OS/2 virtual base address
|
|---|
| 183 | ULONG baseAddress, realBaseAddress;
|
|---|
| 184 | Section section[MAX_SECTION];
|
|---|
| 185 |
|
|---|
| 186 | char *szFileName, *fullpath;
|
|---|
| 187 |
|
|---|
| 188 | HINSTANCE hinstance;
|
|---|
| 189 |
|
|---|
| 190 | int NameTableId;
|
|---|
| 191 | int Win32TableId;
|
|---|
| 192 | int VersionId;
|
|---|
| 193 |
|
|---|
| 194 | ULONG *Win32Table;
|
|---|
| 195 | NameId *NameTable;
|
|---|
| 196 |
|
|---|
| 197 | BOOL fNativePEImage;
|
|---|
| 198 |
|
|---|
| 199 | LPVOID tlsAddress; //address of TLS data
|
|---|
| 200 | LPDWORD tlsIndexAddr; //address of DWORD that receives the TLS index
|
|---|
| 201 | ULONG tlsInitSize; //size of initialized TLS memory block
|
|---|
| 202 | ULONG tlsTotalSize; //size of TLS memory block
|
|---|
| 203 | PIMAGE_TLS_CALLBACK *tlsCallBackAddr; //ptr to TLS callback array
|
|---|
| 204 | ULONG tlsIndex; //module TLS index
|
|---|
| 205 |
|
|---|
| 206 | private:
|
|---|
| 207 |
|
|---|
| 208 | friend class Win32Resource;
|
|---|
| 209 | friend ULONG SYSTEM GetVersionSize(char *modname);
|
|---|
| 210 | };
|
|---|
| 211 |
|
|---|
| 212 | #include <iostream.h>
|
|---|
| 213 | #include <fstream.h>
|
|---|
| 214 | extern ofstream fout;
|
|---|
| 215 |
|
|---|
| 216 | #endif //__PE2LX__
|
|---|
| 217 |
|
|---|
| 218 | #endif
|
|---|