| 1 | /* $Id: winimagelx.cpp,v 1.13 2001-10-16 11:39:39 sandervl Exp $ */ | 
|---|
| 2 |  | 
|---|
| 3 | /* | 
|---|
| 4 | * Win32 LX Image base class | 
|---|
| 5 | * | 
|---|
| 6 | * Copyright 1999-2000 Sander van Leeuwen (sandervl@xs4all.nl) | 
|---|
| 7 | * | 
|---|
| 8 | * TODO: headers not complete | 
|---|
| 9 | * | 
|---|
| 10 | * Project Odin Software License can be found in LICENSE.TXT | 
|---|
| 11 | * | 
|---|
| 12 | */ | 
|---|
| 13 |  | 
|---|
| 14 | #define INCL_DOSFILEMGR          /* File Manager values      */ | 
|---|
| 15 | #define INCL_DOSMODULEMGR | 
|---|
| 16 | #define INCL_DOSERRORS           /* DOS Error values         */ | 
|---|
| 17 | #define INCL_DOSPROCESS          /* DOS Process values       */ | 
|---|
| 18 | #define INCL_DOSMISC             /* DOS Miscellanous values  */ | 
|---|
| 19 | #define INCL_WIN | 
|---|
| 20 | #define INCL_BASE | 
|---|
| 21 | #include <os2wrap.h>             //Odin32 OS/2 api wrappers | 
|---|
| 22 |  | 
|---|
| 23 | #include <stdio.h> | 
|---|
| 24 | #include <string.h> | 
|---|
| 25 | #include <stdlib.h> | 
|---|
| 26 |  | 
|---|
| 27 | #include <assert.h> | 
|---|
| 28 | #include <misc.h> | 
|---|
| 29 | #include <win32type.h> | 
|---|
| 30 | #include <winimagebase.h> | 
|---|
| 31 | #include <winimagelx.h> | 
|---|
| 32 | #include <windllbase.h> | 
|---|
| 33 | #include <winexebase.h> | 
|---|
| 34 | #include <winexelx.h> | 
|---|
| 35 | #include <windlllx.h> | 
|---|
| 36 | #include <pefile.h> | 
|---|
| 37 | #include <unicode.h> | 
|---|
| 38 | #include "oslibmisc.h" | 
|---|
| 39 | #include "initterm.h" | 
|---|
| 40 | #include <win\virtual.h> | 
|---|
| 41 |  | 
|---|
| 42 | #define DBG_LOCALLOG    DBG_winimagelx | 
|---|
| 43 | #include "dbglocal.h" | 
|---|
| 44 |  | 
|---|
| 45 | static BYTE dosHeader[] = { | 
|---|
| 46 | 0x4D, 0x5A, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x0B, 0x00, | 
|---|
| 47 | 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, | 
|---|
| 48 | 0x6A, 0x72, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | 
|---|
| 49 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0, 0x00, 0x00, 0x00, | 
|---|
| 50 | 0xB4, 0x30, 0xCD, 0x21, 0x86, 0xC4, 0x3D, 0x0A, 0x14, 0x72, 0x42, 0xBE, 0x80, 0x00, 0x8A, 0x1C, | 
|---|
| 51 | 0x32, 0xFF, 0x46, 0x88, 0x38, 0x2E, 0x8C, 0x1E, 0x6E, 0x00, 0x8E, 0x06, 0x2C, 0x00, 0x33, 0xC0, | 
|---|
| 52 | 0x8B, 0xF8, 0xB9, 0x00, 0x80, 0xFC, 0xF2, 0xAE, 0x75, 0x23, 0x49, 0x78, 0x20, 0xAE, 0x75, 0xF6, | 
|---|
| 53 | 0x47, 0x47, 0x0E, 0x1F, 0x2E, 0x89, 0x3E, 0x68, 0x00, 0x2E, 0x8C, 0x06, 0x6A, 0x00, 0xBE, 0x5C, | 
|---|
| 54 | 0x00, 0xB9, 0x6C, 0x63, 0xBB, 0x25, 0x00, 0xB4, 0x64, 0xCD, 0x21, 0x73, 0x0B, 0xBA, 0x7C, 0x00, | 
|---|
| 55 | 0x0E, 0x1F, 0xB4, 0x09, 0xCD, 0x21, 0xB0, 0x01, 0xB4, 0x4C, 0xCD, 0x21, 0x20, 0x00, 0x01, 0x00, | 
|---|
| 56 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x81, 0x00, 0x00, 0x00, | 
|---|
| 57 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x54, 0x68, 0x69, 0x73, | 
|---|
| 58 | 0x20, 0x70, 0x72, 0x6F, 0x67, 0x72, 0x61, 0x6D, 0x20, 0x6D, 0x75, 0x73, 0x74, 0x20, 0x62, 0x65, | 
|---|
| 59 | 0x20, 0x72, 0x75, 0x6E, 0x20, 0x75, 0x6E, 0x64, 0x65, 0x72, 0x20, 0x57, 0x69, 0x6E, 0x33, 0x32, | 
|---|
| 60 | 0x2E, 0x0D, 0x0A, 0x24, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 | 
|---|
| 61 | }; | 
|---|
| 62 |  | 
|---|
| 63 | //****************************************************************************** | 
|---|
| 64 | //****************************************************************************** | 
|---|
| 65 | Win32LxImage::Win32LxImage(HINSTANCE hInstance, PVOID pResData) | 
|---|
| 66 | : Win32ImageBase(hInstance), header(0) | 
|---|
| 67 | { | 
|---|
| 68 | APIRET rc; | 
|---|
| 69 | char  *name; | 
|---|
| 70 |  | 
|---|
| 71 | szFileName[0] = 0; | 
|---|
| 72 |  | 
|---|
| 73 | if(lpszCustomDllName) { | 
|---|
| 74 | name = lpszCustomDllName; | 
|---|
| 75 | this->dwOrdinalBase = ::dwOrdinalBase; | 
|---|
| 76 | } | 
|---|
| 77 | else { | 
|---|
| 78 | name = OSLibGetDllName(hinstance); | 
|---|
| 79 | this->dwOrdinalBase = 0; | 
|---|
| 80 | } | 
|---|
| 81 |  | 
|---|
| 82 | strcpy(szFileName, name); | 
|---|
| 83 | strupr(szFileName); | 
|---|
| 84 |  | 
|---|
| 85 | setFullPath(szFileName); | 
|---|
| 86 |  | 
|---|
| 87 | //Pointer to PE resource tree generates by wrc (or NULL for system dlls) | 
|---|
| 88 | pResRootDir = (PIMAGE_RESOURCE_DIRECTORY)pResData; | 
|---|
| 89 |  | 
|---|
| 90 | //ulRVAResourceSection contains the virtual address of the imagebase in the PE header | 
|---|
| 91 | //for the resource section (images loaded by the pe.exe) | 
|---|
| 92 | //For LX images, this is 0 as OffsetToData contains a relative offset | 
|---|
| 93 | ulRVAResourceSection = 0; | 
|---|
| 94 | } | 
|---|
| 95 | //****************************************************************************** | 
|---|
| 96 | //****************************************************************************** | 
|---|
| 97 | Win32LxImage::~Win32LxImage() | 
|---|
| 98 | { | 
|---|
| 99 | if(header) { | 
|---|
| 100 | DosFreeMem(header); | 
|---|
| 101 | } | 
|---|
| 102 | } | 
|---|
| 103 | //****************************************************************************** | 
|---|
| 104 | //****************************************************************************** | 
|---|
| 105 | ULONG Win32LxImage::getApi(char *name) | 
|---|
| 106 | { | 
|---|
| 107 | APIRET      rc; | 
|---|
| 108 | ULONG       apiaddr; | 
|---|
| 109 |  | 
|---|
| 110 | rc = DosQueryProcAddr(hinstanceOS2, 0, name, (PFN *)&apiaddr); | 
|---|
| 111 | if(rc) | 
|---|
| 112 | { | 
|---|
| 113 | dprintf(("Win32LxImage::getApi %x %s -> rc = %d", hinstanceOS2, name, rc)); | 
|---|
| 114 | return(0); | 
|---|
| 115 | } | 
|---|
| 116 | return(apiaddr); | 
|---|
| 117 | } | 
|---|
| 118 | //****************************************************************************** | 
|---|
| 119 | //****************************************************************************** | 
|---|
| 120 | ULONG Win32LxImage::getApi(int ordinal) | 
|---|
| 121 | { | 
|---|
| 122 | APIRET      rc; | 
|---|
| 123 | ULONG       apiaddr; | 
|---|
| 124 |  | 
|---|
| 125 | rc = DosQueryProcAddr(hinstanceOS2, dwOrdinalBase+ordinal, NULL, (PFN *)&apiaddr); | 
|---|
| 126 | if(rc) { | 
|---|
| 127 | dprintf(("Win32LxImage::getApi %x %d -> rc = %d", hinstanceOS2, ordinal, rc)); | 
|---|
| 128 | return(0); | 
|---|
| 129 | } | 
|---|
| 130 | return(apiaddr); | 
|---|
| 131 | } | 
|---|
| 132 | //****************************************************************************** | 
|---|
| 133 | //****************************************************************************** | 
|---|
| 134 | LPVOID Win32LxImage::buildHeader(DWORD MajorImageVersion, DWORD MinorImageVersion, | 
|---|
| 135 | DWORD Subsystem) | 
|---|
| 136 | { | 
|---|
| 137 | APIRET rc; | 
|---|
| 138 | IMAGE_DOS_HEADER *pdosheader; | 
|---|
| 139 | PIMAGE_OPTIONAL_HEADER poh; | 
|---|
| 140 | PIMAGE_FILE_HEADER     pfh; | 
|---|
| 141 | DWORD *ntsig; | 
|---|
| 142 |  | 
|---|
| 143 | rc = DosAllocMem(&header, 4096, PAG_READ | PAG_WRITE | PAG_COMMIT); | 
|---|
| 144 | if(rc) { | 
|---|
| 145 | dprintf(("ERROR: buildHeader DosAllocMem failed!! (rc=%x)", rc)); | 
|---|
| 146 | DebugInt3(); | 
|---|
| 147 | return NULL; | 
|---|
| 148 | } | 
|---|
| 149 | memcpy(header, dosHeader, sizeof(dosHeader)); | 
|---|
| 150 | ntsig  = (DWORD *)((LPBYTE)header + sizeof(dosHeader)); | 
|---|
| 151 | *ntsig = IMAGE_NT_SIGNATURE; | 
|---|
| 152 | pfh    = (PIMAGE_FILE_HEADER)(ntsig+1); | 
|---|
| 153 | pfh->Machine              = IMAGE_FILE_MACHINE_I386; | 
|---|
| 154 | pfh->NumberOfSections     = 0; | 
|---|
| 155 | pfh->TimeDateStamp        = 0x3794f60f; | 
|---|
| 156 | pfh->PointerToSymbolTable = 0; | 
|---|
| 157 | pfh->NumberOfSymbols      = 0; | 
|---|
| 158 | pfh->SizeOfOptionalHeader = sizeof(IMAGE_OPTIONAL_HEADER); | 
|---|
| 159 | pfh->Characteristics      = IMAGE_FILE_DLL | IMAGE_FILE_32BIT_MACHINE | | 
|---|
| 160 | IMAGE_FILE_DEBUG_STRIPPED | IMAGE_FILE_EXECUTABLE_IMAGE | | 
|---|
| 161 | IMAGE_FILE_RELOCS_STRIPPED; | 
|---|
| 162 | poh    = (PIMAGE_OPTIONAL_HEADER)(pfh+1); | 
|---|
| 163 | poh->Magic                       = IMAGE_NT_OPTIONAL_HDR_MAGIC; | 
|---|
| 164 | poh->MajorLinkerVersion          = 0x3; | 
|---|
| 165 | poh->MinorLinkerVersion          = 0xA; | 
|---|
| 166 | poh->SizeOfCode                  = 0; | 
|---|
| 167 | poh->SizeOfInitializedData       = 0; | 
|---|
| 168 | poh->SizeOfUninitializedData     = 0; | 
|---|
| 169 | poh->AddressOfEntryPoint         = 0; | 
|---|
| 170 | poh->BaseOfCode                  = 0; | 
|---|
| 171 | poh->BaseOfData                  = 0; | 
|---|
| 172 | poh->ImageBase                   = 0; | 
|---|
| 173 | poh->SectionAlignment            = 4096; | 
|---|
| 174 | poh->FileAlignment               = 512; | 
|---|
| 175 | poh->MajorOperatingSystemVersion = MajorImageVersion; | 
|---|
| 176 | poh->MinorOperatingSystemVersion = MinorImageVersion; | 
|---|
| 177 | poh->MajorImageVersion           = MajorImageVersion; | 
|---|
| 178 | poh->MinorImageVersion           = MinorImageVersion; | 
|---|
| 179 | poh->MajorSubsystemVersion       = ODINNT_MAJOR_VERSION; | 
|---|
| 180 | poh->MinorSubsystemVersion       = ODINNT_MINOR_VERSION; | 
|---|
| 181 | poh->Reserved1                   = 0; | 
|---|
| 182 | poh->SizeOfImage                 = 0; | 
|---|
| 183 | poh->SizeOfHeaders               = 1024; | 
|---|
| 184 | poh->CheckSum                    = 0; | 
|---|
| 185 | poh->Subsystem                   = Subsystem; | 
|---|
| 186 | poh->DllCharacteristics          = 0; | 
|---|
| 187 | poh->SizeOfStackReserve          = 1*1024*1024; | 
|---|
| 188 | poh->SizeOfStackCommit           = 4096; | 
|---|
| 189 | poh->SizeOfHeapReserve           = 1*1024*1024; | 
|---|
| 190 | poh->SizeOfHeapCommit            = 4096; | 
|---|
| 191 | poh->LoaderFlags                 = 0; | 
|---|
| 192 | poh->NumberOfRvaAndSizes         = 0; | 
|---|
| 193 | //  poh->DataDirectory[0] | 
|---|
| 194 |  | 
|---|
| 195 | return header; | 
|---|
| 196 | } | 
|---|
| 197 | //****************************************************************************** | 
|---|
| 198 | //****************************************************************************** | 
|---|