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