source: trunk/src/kernel32/winimagebase.cpp@ 2859

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

* empty log message *

File size: 7.7 KB
Line 
1/* $Id: winimagebase.cpp,v 1.9 2000-02-22 19:12:53 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//******************************************************************************
45Win32ImageBase::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 magic = MAGIC_WINIMAGE;
52
53 if(hInstance != -1) {
54 this->hinstance = hInstance;
55
56 char *name = OSLibGetDllName(hinstance);
57 strcpy(szFileName, name);
58 strupr(szFileName);
59
60 //rename dll (os/2 -> win32) if necessary (i.e. OLE32OS2 -> OLE32)
61 Win32DllBase::renameDll(szFileName, FALSE);
62
63 name = strrchr(szFileName, '\\')+1;
64 strcpy(szModule, name);
65
66 char *dot = strrchr(szModule, '.');
67 if(dot)
68 *dot = 0;
69 }
70 else {
71 szModule[0] = 0;
72 this->hinstance = -1;
73 }
74}
75//******************************************************************************
76//******************************************************************************
77Win32ImageBase::~Win32ImageBase()
78{
79 Win32Resource *res;
80
81 while(winres)
82 {
83 res = winres->next;
84 delete(winres);
85 winres = res;
86 }
87 if(fullpath)
88 free(fullpath);
89}
90//******************************************************************************
91//******************************************************************************
92void Win32ImageBase::setFullPath(char *name)
93{
94 dassert(name, ("setFullPath, name == NULL"));
95 fullpath = (char *)malloc(strlen(name)+1);
96 dassert(fullpath, ("setFullPath, fullpath == NULL"));
97 strcpy(fullpath, name);
98}
99//******************************************************************************
100//Returns required OS version for this image
101//******************************************************************************
102ULONG Win32ImageBase::getVersion()
103{
104 dprintf(("Win32ImageBase::getVersion: NOT IMPLEMENTED!"));
105 return 0x40000; //NT 4
106}
107//******************************************************************************
108//******************************************************************************
109BOOL Win32ImageBase::isPEImage(char *szFileName)
110{
111 char modname[CCHMAXPATH];
112 char filename[CCHMAXPATH];
113 char *syspath;
114 HFILE dllfile;
115 IMAGE_FILE_HEADER fh;
116 HFILE win32handle;
117 ULONG ulAction = 0; /* Action taken by DosOpen */
118 ULONG ulLocal = 0; /* File pointer position after DosSetFilePtr */
119 APIRET rc = NO_ERROR; /* Return code */
120 LPVOID win32file = NULL;
121 ULONG ulRead;
122 int nSections, i;
123
124 strcpy(filename, szFileName);
125 strupr(filename);
126 if(!strchr(filename, (int)'.')) {
127 strcat(filename,".DLL");
128 }
129 dllfile = OSLibDosOpen(filename, OSLIB_ACCESS_READONLY|OSLIB_ACCESS_SHAREDENYNONE);
130 if(dllfile == NULL) {//search in libpath for dll
131 strcpy(modname, kernel32Path);
132 strcat(modname, filename);
133 dllfile = OSLibDosOpen(modname, OSLIB_ACCESS_READONLY|OSLIB_ACCESS_SHAREDENYNONE);
134 if(dllfile == NULL) {
135 OSLibDosSearchPath(OSLIB_SEARCHENV, "PATH", filename, filename, sizeof(filename));
136 }
137 else {
138 strcpy(filename, modname);
139 OSLibDosClose(dllfile);
140 }
141 }
142 else OSLibDosClose(dllfile);
143
144 rc = DosOpen(filename, /* File path name */
145 &win32handle, /* File handle */
146 &ulAction, /* Action taken */
147 0L, /* File primary allocation */
148 0L, /* File attribute */
149 OPEN_ACTION_FAIL_IF_NEW |
150 OPEN_ACTION_OPEN_IF_EXISTS, /* Open function type */
151 OPEN_FLAGS_NOINHERIT |
152 OPEN_SHARE_DENYNONE |
153 OPEN_ACCESS_READONLY, /* Open mode of the file */
154 0L); /* No extended attribute */
155
156 if (rc != NO_ERROR)
157 {
158 dprintf(("KERNEL32:Win32ImageBase::isPEImage(%s) failed with %u\n",
159 szFileName, rc));
160 return(FALSE);
161 }
162
163 /* Move the file pointer back to the beginning of the file */
164 DosSetFilePtr(win32handle, 0L, FILE_BEGIN, &ulLocal);
165
166 IMAGE_DOS_HEADER *pdoshdr = (IMAGE_DOS_HEADER *)malloc(sizeof(IMAGE_DOS_HEADER));
167 if(pdoshdr == NULL) {
168 DosClose(win32handle); /* Close the file */
169 return(FALSE);
170 }
171 rc = DosRead(win32handle, pdoshdr, sizeof(IMAGE_DOS_HEADER), &ulRead);
172 if(rc != NO_ERROR) {
173 DosClose(win32handle); /* Close the file */
174 return(FALSE);
175 }
176 ULONG hdrsize = pdoshdr->e_lfanew + SIZE_OF_NT_SIGNATURE + sizeof(IMAGE_FILE_HEADER);
177 free(pdoshdr);
178
179 /* Move the file pointer back to the beginning of the file */
180 DosSetFilePtr(win32handle, 0L, FILE_BEGIN, &ulLocal);
181
182 win32file = malloc(hdrsize);
183 if(win32file == NULL) {
184 DosClose(win32handle); /* Close the file */
185 return(FALSE);
186 }
187 rc = DosRead(win32handle, win32file, hdrsize, &ulRead);
188 if(rc != NO_ERROR) {
189 goto failure;
190 }
191
192 if(GetPEFileHeader (win32file, &fh) == FALSE) {
193 goto failure;
194 }
195
196 if(!(fh.Characteristics & IMAGE_FILE_EXECUTABLE_IMAGE)) {//not valid
197 goto failure;
198 }
199 if(fh.Machine != IMAGE_FILE_MACHINE_I386) {
200 goto failure;
201 }
202 //IMAGE_FILE_SYSTEM == only drivers (device/file system/video etc)?
203 if(fh.Characteristics & IMAGE_FILE_SYSTEM) {
204 goto failure;
205 }
206 DosClose(win32handle);
207 return(TRUE);
208
209failure:
210 free(win32file);
211 DosClose(win32handle);
212 return(FALSE);
213}
214//******************************************************************************
215//******************************************************************************
216/**
217 * Static helper which finds the Win32ImageBase object corresponding to hModule.
218 * @returns Pointer to Win32ImageBase object corresponding to hModule.
219 * @param hModule Odin32 modulehandler. 0 and -1 is aliases for the executable module
220 * @status completely implemented and tested.
221 * @author knut st. osmundsen
222 */
223Win32ImageBase * Win32ImageBase::findModule(HMODULE hModule)
224{
225 Win32ImageBase *pRet;
226
227 if (hModule == -1 || hModule == 0 || /* note: WinNt 4, SP4 don't accept -1 as the EXE handle.*/
228 (WinExe != NULL && hModule == WinExe->getInstanceHandle())
229 )
230 pRet = WinExe;
231 else
232 pRet = Win32DllBase::findModule(hModule);
233
234 if (pRet == NULL)
235 {
236 if (WinExe == NULL)
237 dprintf(("Win32ImageBase::findModule: Module not found. WinExe is NULL, hModule=%#x\n", hModule));
238 else
239 dprintf(("Win32ImageBase::findModule: Module not found, hModule=%#x\n", hModule));
240 }
241
242 return pRet;
243}
244
245
Note: See TracBrowser for help on using the repository browser.