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