1 | /* $Id: winimagebase.cpp,v 1.8 2000-02-16 14:22:12 sandervl Exp $ */
|
---|
2 |
|
---|
3 | /*
|
---|
4 | * Win32 PE Image base class
|
---|
5 | *
|
---|
6 | * Copyright 1998-1999 Sander van Leeuwen (sandervl@xs4all.nl)
|
---|
7 | * Copyright 1998 Knut St. Osmundsen
|
---|
8 | *
|
---|
9 | * Project Odin Software License can be found in LICENSE.TXT
|
---|
10 | *
|
---|
11 | */
|
---|
12 |
|
---|
13 | #define INCL_DOSFILEMGR /* File Manager values */
|
---|
14 | #define INCL_DOSMODULEMGR
|
---|
15 | #define INCL_DOSERRORS /* DOS Error values */
|
---|
16 | #define INCL_DOSPROCESS /* DOS Process values */
|
---|
17 | #define INCL_DOSMISC /* DOS Miscellanous values */
|
---|
18 | #define INCL_WIN
|
---|
19 | #define INCL_BASE
|
---|
20 | #include <os2wrap.h> //Odin32 OS/2 api wrappers
|
---|
21 |
|
---|
22 | #include <stdio.h>
|
---|
23 | #include <string.h>
|
---|
24 | #include <stdlib.h>
|
---|
25 |
|
---|
26 | #include <assert.h>
|
---|
27 | #include <misc.h>
|
---|
28 | #include <win32type.h>
|
---|
29 | #include <winimagebase.h>
|
---|
30 | #include <windllbase.h>
|
---|
31 | #include <winexebase.h>
|
---|
32 | #include <pefile.h>
|
---|
33 | #include <unicode.h>
|
---|
34 | #include <winres.h>
|
---|
35 | #include "oslibmisc.h"
|
---|
36 | #include "oslibdos.h"
|
---|
37 | #include "initterm.h"
|
---|
38 | #include <win\virtual.h>
|
---|
39 |
|
---|
40 | #define DBG_LOCALLOG DBG_winimagebase
|
---|
41 | #include "dbglocal.h"
|
---|
42 |
|
---|
43 | //******************************************************************************
|
---|
44 | //******************************************************************************
|
---|
45 | Win32ImageBase::Win32ImageBase(HINSTANCE hInstance) :
|
---|
46 | errorState(NO_ERROR), entryPoint(0), fullpath(NULL),
|
---|
47 | tlsAddress(0), tlsIndexAddr(0), tlsInitSize(0), tlsTotalSize(0),
|
---|
48 | tlsCallBackAddr(0), tlsIndex(-1), winres(NULL), pResDir(NULL),
|
---|
49 | ulRVAResourceSection(0)
|
---|
50 | {
|
---|
51 | #ifdef DEBUG
|
---|
52 | magic = MAGIC_WINIMAGE;
|
---|
53 | #endif
|
---|
54 |
|
---|
55 | if(hInstance != -1) {
|
---|
56 | this->hinstance = hInstance;
|
---|
57 |
|
---|
58 | char *name = OSLibGetDllName(hinstance);
|
---|
59 | strcpy(szFileName, name);
|
---|
60 | strupr(szFileName);
|
---|
61 |
|
---|
62 | //rename dll (os/2 -> win32) if necessary (i.e. OLE32OS2 -> OLE32)
|
---|
63 | Win32DllBase::renameDll(szFileName, FALSE);
|
---|
64 |
|
---|
65 | name = strrchr(szFileName, '\\')+1;
|
---|
66 | strcpy(szModule, name);
|
---|
67 |
|
---|
68 | char *dot = strrchr(szModule, '.');
|
---|
69 | if(dot)
|
---|
70 | *dot = 0;
|
---|
71 | }
|
---|
72 | else {
|
---|
73 | szModule[0] = 0;
|
---|
74 | this->hinstance = -1;
|
---|
75 | }
|
---|
76 | }
|
---|
77 | //******************************************************************************
|
---|
78 | //******************************************************************************
|
---|
79 | Win32ImageBase::~Win32ImageBase()
|
---|
80 | {
|
---|
81 | Win32Resource *res;
|
---|
82 |
|
---|
83 | while(winres)
|
---|
84 | {
|
---|
85 | res = winres->next;
|
---|
86 | delete(winres);
|
---|
87 | winres = res;
|
---|
88 | }
|
---|
89 | if(fullpath)
|
---|
90 | free(fullpath);
|
---|
91 | }
|
---|
92 | //******************************************************************************
|
---|
93 | //******************************************************************************
|
---|
94 | void Win32ImageBase::setFullPath(char *name)
|
---|
95 | {
|
---|
96 | dassert(name, ("setFullPath, name == NULL"));
|
---|
97 | fullpath = (char *)malloc(strlen(name)+1);
|
---|
98 | dassert(fullpath, ("setFullPath, fullpath == NULL"));
|
---|
99 | strcpy(fullpath, name);
|
---|
100 | }
|
---|
101 | //******************************************************************************
|
---|
102 | //Returns required OS version for this image
|
---|
103 | //******************************************************************************
|
---|
104 | ULONG Win32ImageBase::getVersion()
|
---|
105 | {
|
---|
106 | dprintf(("Win32ImageBase::getVersion: NOT IMPLEMENTED!"));
|
---|
107 | return 0x40000; //NT 4
|
---|
108 | }
|
---|
109 | //******************************************************************************
|
---|
110 | //******************************************************************************
|
---|
111 | BOOL Win32ImageBase::isPEImage(char *szFileName)
|
---|
112 | {
|
---|
113 | char modname[CCHMAXPATH];
|
---|
114 | char filename[CCHMAXPATH];
|
---|
115 | char *syspath;
|
---|
116 | HFILE dllfile;
|
---|
117 | IMAGE_FILE_HEADER fh;
|
---|
118 | HFILE win32handle;
|
---|
119 | ULONG ulAction = 0; /* Action taken by DosOpen */
|
---|
120 | ULONG ulLocal = 0; /* File pointer position after DosSetFilePtr */
|
---|
121 | APIRET rc = NO_ERROR; /* Return code */
|
---|
122 | LPVOID win32file = NULL;
|
---|
123 | ULONG ulRead;
|
---|
124 | int nSections, i;
|
---|
125 |
|
---|
126 | strcpy(filename, szFileName);
|
---|
127 | strupr(filename);
|
---|
128 | if(!strchr(filename, (int)'.')) {
|
---|
129 | strcat(filename,".DLL");
|
---|
130 | }
|
---|
131 | dllfile = OSLibDosOpen(filename, OSLIB_ACCESS_READONLY|OSLIB_ACCESS_SHAREDENYNONE);
|
---|
132 | if(dllfile == NULL) {//search in libpath for dll
|
---|
133 | strcpy(modname, kernel32Path);
|
---|
134 | strcat(modname, filename);
|
---|
135 | dllfile = OSLibDosOpen(modname, OSLIB_ACCESS_READONLY|OSLIB_ACCESS_SHAREDENYNONE);
|
---|
136 | if(dllfile == NULL) {
|
---|
137 | OSLibDosSearchPath(OSLIB_SEARCHENV, "PATH", filename, filename, sizeof(filename));
|
---|
138 | }
|
---|
139 | else {
|
---|
140 | strcpy(filename, modname);
|
---|
141 | OSLibDosClose(dllfile);
|
---|
142 | }
|
---|
143 | }
|
---|
144 | else OSLibDosClose(dllfile);
|
---|
145 |
|
---|
146 | rc = DosOpen(filename, /* File path name */
|
---|
147 | &win32handle, /* File handle */
|
---|
148 | &ulAction, /* Action taken */
|
---|
149 | 0L, /* File primary allocation */
|
---|
150 | 0L, /* File attribute */
|
---|
151 | OPEN_ACTION_FAIL_IF_NEW |
|
---|
152 | OPEN_ACTION_OPEN_IF_EXISTS, /* Open function type */
|
---|
153 | OPEN_FLAGS_NOINHERIT |
|
---|
154 | OPEN_SHARE_DENYNONE |
|
---|
155 | OPEN_ACCESS_READONLY, /* Open mode of the file */
|
---|
156 | 0L); /* No extended attribute */
|
---|
157 |
|
---|
158 | if (rc != NO_ERROR)
|
---|
159 | {
|
---|
160 | dprintf(("KERNEL32:Win32ImageBase::isPEImage(%s) failed with %u\n",
|
---|
161 | szFileName, rc));
|
---|
162 | return(FALSE);
|
---|
163 | }
|
---|
164 |
|
---|
165 | /* Move the file pointer back to the beginning of the file */
|
---|
166 | DosSetFilePtr(win32handle, 0L, FILE_BEGIN, &ulLocal);
|
---|
167 |
|
---|
168 | IMAGE_DOS_HEADER *pdoshdr = (IMAGE_DOS_HEADER *)malloc(sizeof(IMAGE_DOS_HEADER));
|
---|
169 | if(pdoshdr == NULL) {
|
---|
170 | DosClose(win32handle); /* Close the file */
|
---|
171 | return(FALSE);
|
---|
172 | }
|
---|
173 | rc = DosRead(win32handle, pdoshdr, sizeof(IMAGE_DOS_HEADER), &ulRead);
|
---|
174 | if(rc != NO_ERROR) {
|
---|
175 | DosClose(win32handle); /* Close the file */
|
---|
176 | return(FALSE);
|
---|
177 | }
|
---|
178 | ULONG hdrsize = pdoshdr->e_lfanew + SIZE_OF_NT_SIGNATURE + sizeof(IMAGE_FILE_HEADER);
|
---|
179 | free(pdoshdr);
|
---|
180 |
|
---|
181 | /* Move the file pointer back to the beginning of the file */
|
---|
182 | DosSetFilePtr(win32handle, 0L, FILE_BEGIN, &ulLocal);
|
---|
183 |
|
---|
184 | win32file = malloc(hdrsize);
|
---|
185 | if(win32file == NULL) {
|
---|
186 | DosClose(win32handle); /* Close the file */
|
---|
187 | return(FALSE);
|
---|
188 | }
|
---|
189 | rc = DosRead(win32handle, win32file, hdrsize, &ulRead);
|
---|
190 | if(rc != NO_ERROR) {
|
---|
191 | goto failure;
|
---|
192 | }
|
---|
193 |
|
---|
194 | if(GetPEFileHeader (win32file, &fh) == FALSE) {
|
---|
195 | goto failure;
|
---|
196 | }
|
---|
197 |
|
---|
198 | if(!(fh.Characteristics & IMAGE_FILE_EXECUTABLE_IMAGE)) {//not valid
|
---|
199 | goto failure;
|
---|
200 | }
|
---|
201 | if(fh.Machine != IMAGE_FILE_MACHINE_I386) {
|
---|
202 | goto failure;
|
---|
203 | }
|
---|
204 | //IMAGE_FILE_SYSTEM == only drivers (device/file system/video etc)?
|
---|
205 | if(fh.Characteristics & IMAGE_FILE_SYSTEM) {
|
---|
206 | goto failure;
|
---|
207 | }
|
---|
208 | DosClose(win32handle);
|
---|
209 | return(TRUE);
|
---|
210 |
|
---|
211 | failure:
|
---|
212 | free(win32file);
|
---|
213 | DosClose(win32handle);
|
---|
214 | return(FALSE);
|
---|
215 | }
|
---|
216 | //******************************************************************************
|
---|
217 | //******************************************************************************
|
---|
218 | /**
|
---|
219 | * Static helper which finds the Win32ImageBase object corresponding to hModule.
|
---|
220 | * @returns Pointer to Win32ImageBase object corresponding to hModule.
|
---|
221 | * @param hModule Odin32 modulehandler. 0 and -1 is aliases for the executable module
|
---|
222 | * @status completely implemented and tested.
|
---|
223 | * @author knut st. osmundsen
|
---|
224 | */
|
---|
225 | Win32ImageBase * Win32ImageBase::findModule(HMODULE hModule)
|
---|
226 | {
|
---|
227 | Win32ImageBase *pRet;
|
---|
228 |
|
---|
229 | if (hModule == -1 || hModule == 0 || /* note: WinNt 4, SP4 don't accept -1 as the EXE handle.*/
|
---|
230 | (WinExe != NULL && hModule == WinExe->getInstanceHandle())
|
---|
231 | )
|
---|
232 | pRet = WinExe;
|
---|
233 | else
|
---|
234 | pRet = Win32DllBase::findModule(hModule);
|
---|
235 |
|
---|
236 | if (pRet == NULL)
|
---|
237 | {
|
---|
238 | if (WinExe == NULL)
|
---|
239 | dprintf(("Win32ImageBase::findModule: Module not found. WinExe is NULL, hModule=%#x\n", hModule));
|
---|
240 | else
|
---|
241 | dprintf(("Win32ImageBase::findModule: Module not found, hModule=%#x\n", hModule));
|
---|
242 | }
|
---|
243 |
|
---|
244 | return pRet;
|
---|
245 | }
|
---|
246 |
|
---|
247 |
|
---|