source: trunk/include/winimage.h@ 602

Last change on this file since 602 was 589, checked in by sandervl, 26 years ago

More PE resource changes

File size: 7.6 KB
Line 
1/* $Id: winimage.h,v 1.8 1999-08-19 19:51:18 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
20extern 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)
39typedef 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
69typedef 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
78typedef struct {
79 ULONG virtaddr;
80 ULONG ordinal;
81 ULONG nlength;
82 char name[4];
83} NameExport;
84
85typedef struct {
86 ULONG virtaddr;
87 ULONG ordinal;
88} OrdExport;
89
90class Win32Dll;
91class Win32Resource;
92
93class Win32Image
94{
95#ifdef DEBUG
96protected:
97 DWORD magic;
98public:
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
110public:
111 // Constructors and destructors
112 Win32Image(HINSTANCE hinstance, int NameTableId, int Win32TableId);
113 Win32Image(char *szFileName);
114virtual ~Win32Image();
115
116 //called to reset object to native OS/2 or converted win32 dll
117 void OS2ImageInit(HINSTANCE hinstance, int NameTableId, int Win32TableId);
118
119 //reservedMem is address of memory reserved in peldr.dll (allocated before
120 //any dlls are loaded, so that exes without fixups can be loaded at a low
121 //address)
122virtual BOOL init(ULONG reservedMem);
123
124 ULONG getError() { return errorState; };
125 HINSTANCE getInstanceHandle() { return hinstance; };
126
127virtual void setFullPath(char *name);
128 char *getFullPath() { return fullpath; };
129
130 char *getModuleName() { return szModule; };
131
132 HRSRC findResourceA(LPCSTR lpszName, LPSTR lpszType);
133 HRSRC findResourceW(LPWSTR lpszName, LPWSTR lpszType);
134 ULONG getResourceSizeA(LPCSTR lpszName, LPSTR lpszType);
135 ULONG getResourceSizeW(LPCWSTR lpszName, LPWSTR lpszType);
136
137 int getWin32ResourceId(int id);
138 int convertNameId(char *lpszName);
139
140static BOOL isPEImage(char *szFileName);
141
142 void setVersionId(int id) { VersionId = id; };
143 int getVersionId() { return VersionId; };
144
145 void setEntryPoint(ULONG startAddress) { entryPoint = startAddress; };
146
147 void setTLSAddress(LPVOID dwTlsAddress) { tlsAddress = dwTlsAddress; };
148 void setTLSIndexAddr(LPDWORD dwTlsIndexAddr) { tlsIndexAddr = dwTlsIndexAddr; };
149 void setTLSInitSize(ULONG dwTlsSize) { tlsInitSize = dwTlsSize; };
150 void setTLSTotalSize(ULONG dwTlsSize) { tlsTotalSize = dwTlsSize; };
151 void setTLSCallBackAddr(PIMAGE_TLS_CALLBACK *dwTlsCallBackAddr)
152 {
153 tlsCallBackAddr = dwTlsCallBackAddr;
154 };
155
156 void tlsAttachThread(); //setup TLS structures for new thread
157 void tlsDetachThread(); //destroy TLS structures
158
159protected:
160 void tlsAlloc(); //Allocate TLS index for this module
161 void tlsDelete(); //Destroy TLS index for this module
162
163 void StoreImportByOrd(Win32Dll *WinDll, ULONG ordinal, ULONG impaddr);
164 void StoreImportByName(Win32Dll *WinDll, char *impname, ULONG impaddr);
165
166 void addSection(ULONG type, char *rawdata, ULONG rawsize, ULONG virtaddress, ULONG virtsize);
167 BOOL allocSections(ULONG reservedMem);
168 BOOL allocFixedMem(ULONG reservedMem);
169 Section *findSection(ULONG type);
170 Section *findSectionByAddr(ULONG addr);
171
172 BOOL storeSections(char *win32file);
173 BOOL setMemFlags();
174 BOOL setFixups(PIMAGE_BASE_RELOCATION prel);
175 void AddOff32Fixup(ULONG fixupaddr);
176 void AddOff16Fixup(ULONG fixupaddr, BOOL fHighFixup);
177
178 BOOL processImports(char *win32file);
179
180 BOOL processExports(char *win32file);
181 void AddNameExport(ULONG virtaddr, char *apiname, ULONG ordinal);
182 void AddOrdExport(ULONG virtaddr, ULONG ordinal);
183
184 ULONG getPEResourceSize(ULONG id, ULONG type, ULONG lang = LANG_GETFIRST);
185 Win32Resource *getPEResource(ULONG id, ULONG type, ULONG lang = LANG_GETFIRST);
186
187 PIMAGE_RESOURCE_DATA_ENTRY getPEResourceEntry(ULONG id, ULONG type, ULONG lang = LANG_GETFIRST);
188 PIMAGE_RESOURCE_DATA_ENTRY ProcessResSubDir(PIMAGE_RESOURCE_DIRECTORY prdType,
189 ULONG *nodeData, int level);
190 PIMAGE_RESOURCE_DIRECTORY pResDir;
191 Section *pResSection;
192 Win32Resource *winres;
193
194 IMAGE_OPTIONAL_HEADER oh;
195 IMAGE_FILE_HEADER fh;
196
197 ULONG errorState, entryPoint;
198 ULONG nrNameExports, nameExportSize;
199 ULONG nrOrdExports;
200 NameExport *nameexports, *curnameexport;
201 OrdExport *ordexports, *curordexport;
202
203 ULONG nrsections, imageSize, imageVirtBase, imageVirtEnd;
204 //OS/2 virtual base address
205 ULONG realBaseAddress;
206 Section section[MAX_SECTION];
207
208 char *szFileName, *fullpath;
209 char szModule[CCHMAXPATH];
210
211 HINSTANCE hinstance;
212
213 int NameTableId;
214 int Win32TableId;
215 int VersionId;
216
217 ULONG *Win32Table;
218 NameId *NameTable;
219
220 BOOL fNativePEImage;
221
222 LPVOID tlsAddress; //address of TLS data
223 LPDWORD tlsIndexAddr; //address of DWORD that receives the TLS index
224 ULONG tlsInitSize; //size of initialized TLS memory block
225 ULONG tlsTotalSize; //size of TLS memory block
226 PIMAGE_TLS_CALLBACK *tlsCallBackAddr; //ptr to TLS callback array
227 ULONG tlsIndex; //module TLS index
228
229private:
230
231 friend class Win32Resource;
232 friend ULONG SYSTEM GetVersionSize(char *modname);
233};
234
235#include <iostream.h>
236#include <fstream.h>
237extern ofstream fout;
238
239//SvL: This structure is placed at the end of the first page of the image (header
240// page), so we can determine the Win32Image pointer from a HINSTANCE variable
241// (which is actually the address of the win32 module)
242typedef struct
243{
244 Win32Image *image;
245 ULONG magic;
246} WINIMAGE_LOOKUP;
247
248#define WINIMAGE_LOOKUPADDR(a) (WINIMAGE_LOOKUP *)((ULONG)a + PAGE_SIZE - sizeof(WINIMAGE_LOOKUP))
249
250#endif //__PE2LX__
251
252#endif
Note: See TracBrowser for help on using the repository browser.