source: trunk/include/winimage.h@ 570

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

PE loader resource fixes

File size: 7.2 KB
Line 
1/* $Id: winimage.h,v 1.7 1999-08-19 10:24:53 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
135 int getWin32ResourceId(int id);
136 int convertNameId(char *lpszName);
137
138static BOOL isPEImage(char *szFileName);
139
140 void setVersionId(int id) { VersionId = id; };
141 int getVersionId() { return VersionId; };
142
143 void setEntryPoint(ULONG startAddress) { entryPoint = startAddress; };
144
145 void setTLSAddress(LPVOID dwTlsAddress) { tlsAddress = dwTlsAddress; };
146 void setTLSIndexAddr(LPDWORD dwTlsIndexAddr) { tlsIndexAddr = dwTlsIndexAddr; };
147 void setTLSInitSize(ULONG dwTlsSize) { tlsInitSize = dwTlsSize; };
148 void setTLSTotalSize(ULONG dwTlsSize) { tlsTotalSize = dwTlsSize; };
149 void setTLSCallBackAddr(PIMAGE_TLS_CALLBACK *dwTlsCallBackAddr)
150 {
151 tlsCallBackAddr = dwTlsCallBackAddr;
152 };
153
154 void tlsAttachThread(); //setup TLS structures for new thread
155 void tlsDetachThread(); //destroy TLS structures
156
157protected:
158 void tlsAlloc(); //Allocate TLS index for this module
159 void tlsDelete(); //Destroy TLS index for this module
160
161 void StoreImportByOrd(Win32Dll *WinDll, ULONG ordinal, ULONG impaddr);
162 void StoreImportByName(Win32Dll *WinDll, char *impname, ULONG impaddr);
163
164 void addSection(ULONG type, char *rawdata, ULONG rawsize, ULONG virtaddress, ULONG virtsize);
165 BOOL allocSections(ULONG reservedMem);
166 BOOL allocFixedMem(ULONG reservedMem);
167 Section *findSection(ULONG type);
168 Section *findSectionByAddr(ULONG addr);
169
170 BOOL storeSections(char *win32file);
171 BOOL setMemFlags();
172 BOOL setFixups(PIMAGE_BASE_RELOCATION prel);
173 void AddOff32Fixup(ULONG fixupaddr);
174 void AddOff16Fixup(ULONG fixupaddr, BOOL fHighFixup);
175
176 BOOL processImports(char *win32file);
177
178 BOOL processExports(char *win32file);
179 void AddNameExport(ULONG virtaddr, char *apiname, ULONG ordinal);
180 void AddOrdExport(ULONG virtaddr, ULONG ordinal);
181
182 Win32Resource *getPEResource(ULONG id, ULONG type, ULONG lang = LANG_GETFIRST);
183 PIMAGE_RESOURCE_DATA_ENTRY ProcessResSubDir(PIMAGE_RESOURCE_DIRECTORY prdType,
184 ULONG *nodeData, int level);
185 PIMAGE_RESOURCE_DIRECTORY pResDir;
186 Section *pResSection;
187 Win32Resource *winres;
188
189 IMAGE_OPTIONAL_HEADER oh;
190 IMAGE_FILE_HEADER fh;
191
192 ULONG errorState, entryPoint;
193 ULONG nrNameExports, nameExportSize;
194 ULONG nrOrdExports;
195 NameExport *nameexports, *curnameexport;
196 OrdExport *ordexports, *curordexport;
197
198 ULONG nrsections, imageSize, imageVirtBase, imageVirtEnd;
199 //OS/2 virtual base address
200 ULONG realBaseAddress;
201 Section section[MAX_SECTION];
202
203 char *szFileName, *fullpath;
204 char szModule[CCHMAXPATH];
205
206 HINSTANCE hinstance;
207
208 int NameTableId;
209 int Win32TableId;
210 int VersionId;
211
212 ULONG *Win32Table;
213 NameId *NameTable;
214
215 BOOL fNativePEImage;
216
217 LPVOID tlsAddress; //address of TLS data
218 LPDWORD tlsIndexAddr; //address of DWORD that receives the TLS index
219 ULONG tlsInitSize; //size of initialized TLS memory block
220 ULONG tlsTotalSize; //size of TLS memory block
221 PIMAGE_TLS_CALLBACK *tlsCallBackAddr; //ptr to TLS callback array
222 ULONG tlsIndex; //module TLS index
223
224private:
225
226 friend class Win32Resource;
227 friend ULONG SYSTEM GetVersionSize(char *modname);
228};
229
230#include <iostream.h>
231#include <fstream.h>
232extern ofstream fout;
233
234//SvL: This structure is placed at the end of the first page of the image (header
235// page), so we can determine the Win32Image pointer from a HINSTANCE variable
236// (which is actually the address of the win32 module)
237typedef struct
238{
239 Win32Image *image;
240 ULONG magic;
241} WINIMAGE_LOOKUP;
242
243#define WINIMAGE_LOOKUPADDR(a) (WINIMAGE_LOOKUP *)((ULONG)a + PAGE_SIZE - sizeof(WINIMAGE_LOOKUP))
244
245#endif //__PE2LX__
246
247#endif
Note: See TracBrowser for help on using the repository browser.