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

Last change on this file since 3265 was 3059, checked in by sandervl, 25 years ago

Dll dependency changes

File size: 8.5 KB
Line 
1/* $Id: winimagebase.cpp,v 1.10 2000-03-09 19:03:22 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//Add image to dependency list of this image
101//******************************************************************************
102void Win32ImageBase::addDependency(Win32DllBase *image)
103{
104 loadedDlls.Push((ULONG)image);
105}
106//******************************************************************************
107//******************************************************************************
108BOOL Win32ImageBase::dependsOn(Win32DllBase *image)
109{
110 QueueItem *item;
111 BOOL ret = FALSE;
112
113 dlllistmutex.enter();
114 item = loadedDlls.Head();
115 while(item) {
116 if(loadedDlls.getItem(item) == (ULONG)image) {
117 ret = TRUE;
118 break;
119 }
120 item = loadedDlls.getNext(item);
121 }
122 dlllistmutex.leave();
123 return ret;
124}
125//******************************************************************************
126//Returns required OS version for this image
127//******************************************************************************
128ULONG Win32ImageBase::getVersion()
129{
130 dprintf(("Win32ImageBase::getVersion: NOT IMPLEMENTED!"));
131 return 0x40000; //NT 4
132}
133//******************************************************************************
134//******************************************************************************
135BOOL Win32ImageBase::isPEImage(char *szFileName)
136{
137 char modname[CCHMAXPATH];
138 char filename[CCHMAXPATH];
139 char *syspath;
140 HFILE dllfile;
141 IMAGE_FILE_HEADER fh;
142 HFILE win32handle;
143 ULONG ulAction = 0; /* Action taken by DosOpen */
144 ULONG ulLocal = 0; /* File pointer position after DosSetFilePtr */
145 APIRET rc = NO_ERROR; /* Return code */
146 LPVOID win32file = NULL;
147 ULONG ulRead;
148 int nSections, i;
149
150 strcpy(filename, szFileName);
151 strupr(filename);
152 if(!strchr(filename, (int)'.')) {
153 strcat(filename,".DLL");
154 }
155 dllfile = OSLibDosOpen(filename, OSLIB_ACCESS_READONLY|OSLIB_ACCESS_SHAREDENYNONE);
156 if(dllfile == NULL) {//search in libpath for dll
157 strcpy(modname, kernel32Path);
158 strcat(modname, filename);
159 dllfile = OSLibDosOpen(modname, OSLIB_ACCESS_READONLY|OSLIB_ACCESS_SHAREDENYNONE);
160 if(dllfile == NULL) {
161 OSLibDosSearchPath(OSLIB_SEARCHENV, "PATH", filename, filename, sizeof(filename));
162 }
163 else {
164 strcpy(filename, modname);
165 OSLibDosClose(dllfile);
166 }
167 }
168 else OSLibDosClose(dllfile);
169
170 rc = DosOpen(filename, /* File path name */
171 &win32handle, /* File handle */
172 &ulAction, /* Action taken */
173 0L, /* File primary allocation */
174 0L, /* File attribute */
175 OPEN_ACTION_FAIL_IF_NEW |
176 OPEN_ACTION_OPEN_IF_EXISTS, /* Open function type */
177 OPEN_FLAGS_NOINHERIT |
178 OPEN_SHARE_DENYNONE |
179 OPEN_ACCESS_READONLY, /* Open mode of the file */
180 0L); /* No extended attribute */
181
182 if (rc != NO_ERROR)
183 {
184 dprintf(("KERNEL32:Win32ImageBase::isPEImage(%s) failed with %u\n",
185 szFileName, rc));
186 return(FALSE);
187 }
188
189 /* Move the file pointer back to the beginning of the file */
190 DosSetFilePtr(win32handle, 0L, FILE_BEGIN, &ulLocal);
191
192 IMAGE_DOS_HEADER *pdoshdr = (IMAGE_DOS_HEADER *)malloc(sizeof(IMAGE_DOS_HEADER));
193 if(pdoshdr == NULL) {
194 DosClose(win32handle); /* Close the file */
195 return(FALSE);
196 }
197 rc = DosRead(win32handle, pdoshdr, sizeof(IMAGE_DOS_HEADER), &ulRead);
198 if(rc != NO_ERROR) {
199 DosClose(win32handle); /* Close the file */
200 return(FALSE);
201 }
202 ULONG hdrsize = pdoshdr->e_lfanew + SIZE_OF_NT_SIGNATURE + sizeof(IMAGE_FILE_HEADER);
203 free(pdoshdr);
204
205 /* Move the file pointer back to the beginning of the file */
206 DosSetFilePtr(win32handle, 0L, FILE_BEGIN, &ulLocal);
207
208 win32file = malloc(hdrsize);
209 if(win32file == NULL) {
210 DosClose(win32handle); /* Close the file */
211 return(FALSE);
212 }
213 rc = DosRead(win32handle, win32file, hdrsize, &ulRead);
214 if(rc != NO_ERROR) {
215 goto failure;
216 }
217
218 if(GetPEFileHeader (win32file, &fh) == FALSE) {
219 goto failure;
220 }
221
222 if(!(fh.Characteristics & IMAGE_FILE_EXECUTABLE_IMAGE)) {//not valid
223 goto failure;
224 }
225 if(fh.Machine != IMAGE_FILE_MACHINE_I386) {
226 goto failure;
227 }
228 //IMAGE_FILE_SYSTEM == only drivers (device/file system/video etc)?
229 if(fh.Characteristics & IMAGE_FILE_SYSTEM) {
230 goto failure;
231 }
232 DosClose(win32handle);
233 return(TRUE);
234
235failure:
236 free(win32file);
237 DosClose(win32handle);
238 return(FALSE);
239}
240//******************************************************************************
241//******************************************************************************
242/**
243 * Static helper which finds the Win32ImageBase object corresponding to hModule.
244 * @returns Pointer to Win32ImageBase object corresponding to hModule.
245 * @param hModule Odin32 modulehandler. 0 and -1 is aliases for the executable module
246 * @status completely implemented and tested.
247 * @author knut st. osmundsen
248 */
249Win32ImageBase * Win32ImageBase::findModule(HMODULE hModule)
250{
251 Win32ImageBase *pRet;
252
253 if (hModule == -1 || hModule == 0 || /* note: WinNt 4, SP4 don't accept -1 as the EXE handle.*/
254 (WinExe != NULL && hModule == WinExe->getInstanceHandle())
255 )
256 pRet = WinExe;
257 else
258 pRet = Win32DllBase::findModule(hModule);
259
260 if (pRet == NULL)
261 {
262 if (WinExe == NULL)
263 dprintf(("Win32ImageBase::findModule: Module not found. WinExe is NULL, hModule=%#x\n", hModule));
264 else
265 dprintf(("Win32ImageBase::findModule: Module not found, hModule=%#x\n", hModule));
266 }
267
268 return pRet;
269}
270
271
Note: See TracBrowser for help on using the repository browser.