source: trunk/src/kernel32/winimagelx.cpp

Last change on this file was 21916, checked in by dmik, 14 years ago

Merge branch gcc-kmk to trunk.

File size: 9.7 KB
RevLine 
[10397]1/* $Id: winimagelx.cpp,v 1.21 2004-01-15 10:39:11 sandervl Exp $ */
[956]2
3/*
4 * Win32 LX Image base class
5 *
[3993]6 * Copyright 1999-2000 Sander van Leeuwen (sandervl@xs4all.nl)
[10397]7 * Copyright 2003 Innotek Systemberatung GmbH (sandervl@innotek.de)
[956]8 *
[10397]9 * header adjustment & fixup_rva_ptrs borrowed from Wine (Rewind)
10 * Copyright 2000 Alexandre Julliard
11 *
[3993]12 * TODO: headers not complete
[10397]13 *
[956]14 * Project Odin Software License can be found in LICENSE.TXT
15 *
16 */
17
18#define INCL_DOSFILEMGR /* File Manager values */
19#define INCL_DOSMODULEMGR
20#define INCL_DOSERRORS /* DOS Error values */
21#define INCL_DOSPROCESS /* DOS Process values */
22#define INCL_DOSMISC /* DOS Miscellanous values */
23#define INCL_WIN
24#define INCL_BASE
25#include <os2wrap.h> //Odin32 OS/2 api wrappers
26
27#include <stdio.h>
28#include <string.h>
29#include <stdlib.h>
30
31#include <assert.h>
32#include <misc.h>
33#include <win32type.h>
[21916]34#include "winimagebase.h"
35#include "winimagelx.h"
36#include "windllbase.h"
37#include "winexebase.h"
38#include "winexelx.h"
39#include "windlllx.h"
[956]40#include <pefile.h>
41#include <unicode.h>
42#include "oslibmisc.h"
43#include "initterm.h"
[21916]44#include <win/virtual.h>
[956]45
[2802]46#define DBG_LOCALLOG DBG_winimagelx
47#include "dbglocal.h"
48
[7797]49BYTE dosHeader[16*15] = {
[3993]50 0x4D, 0x5A, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x0B, 0x00,
51 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
52 0x6A, 0x72, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
53 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0, 0x00, 0x00, 0x00,
54 0xB4, 0x30, 0xCD, 0x21, 0x86, 0xC4, 0x3D, 0x0A, 0x14, 0x72, 0x42, 0xBE, 0x80, 0x00, 0x8A, 0x1C,
55 0x32, 0xFF, 0x46, 0x88, 0x38, 0x2E, 0x8C, 0x1E, 0x6E, 0x00, 0x8E, 0x06, 0x2C, 0x00, 0x33, 0xC0,
56 0x8B, 0xF8, 0xB9, 0x00, 0x80, 0xFC, 0xF2, 0xAE, 0x75, 0x23, 0x49, 0x78, 0x20, 0xAE, 0x75, 0xF6,
57 0x47, 0x47, 0x0E, 0x1F, 0x2E, 0x89, 0x3E, 0x68, 0x00, 0x2E, 0x8C, 0x06, 0x6A, 0x00, 0xBE, 0x5C,
58 0x00, 0xB9, 0x6C, 0x63, 0xBB, 0x25, 0x00, 0xB4, 0x64, 0xCD, 0x21, 0x73, 0x0B, 0xBA, 0x7C, 0x00,
59 0x0E, 0x1F, 0xB4, 0x09, 0xCD, 0x21, 0xB0, 0x01, 0xB4, 0x4C, 0xCD, 0x21, 0x20, 0x00, 0x01, 0x00,
60 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x81, 0x00, 0x00, 0x00,
61 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x54, 0x68, 0x69, 0x73,
62 0x20, 0x70, 0x72, 0x6F, 0x67, 0x72, 0x61, 0x6D, 0x20, 0x6D, 0x75, 0x73, 0x74, 0x20, 0x62, 0x65,
63 0x20, 0x72, 0x75, 0x6E, 0x20, 0x75, 0x6E, 0x64, 0x65, 0x72, 0x20, 0x57, 0x69, 0x6E, 0x33, 0x32,
64 0x2E, 0x0D, 0x0A, 0x24, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
65};
66
[956]67//******************************************************************************
[10397]68/* adjust an array of pointers to make them into RVAs */
[956]69//******************************************************************************
[10397]70static inline void fixup_rva_ptrs( void *array, void *base, int count )
71{
72 void **ptr = (void **)array;
73 while (count--)
74 {
75 if (*ptr) *ptr = (void *)((char *)*ptr - (char *)base);
76 ptr++;
77 }
78}
79//******************************************************************************
80//******************************************************************************
[1872]81Win32LxImage::Win32LxImage(HINSTANCE hInstance, PVOID pResData)
[10397]82 : Win32ImageBase(hInstance), header(0), pCustomPEHeader(0)
[956]83{
[8877]84 APIRET rc;
85 char *name;
[956]86
[8877]87 szFileName[0] = 0;
[956]88
[10397]89 if (lpszCustomDllName) {
90 name = lpszCustomDllName;
91 pCustomPEHeader = lpCustomDllPEHdr;
92
93 hinstance = (DWORD)pCustomPEHeader;
94 if (pCustomPEHeader) {
95 //Calculate address of optional header
96 poh = (PIMAGE_OPTIONAL_HEADER)OPTHEADEROFF(pCustomPEHeader);
97
98 //Update the header data to reflect the new load address
99 poh->ImageBase = hinstance;
100
101 PIMAGE_EXPORT_DIRECTORY pExpDir;
102 pExpDir = (PIMAGE_EXPORT_DIRECTORY)((char*)hinstance
103 + poh->DataDirectory[IMAGE_FILE_EXPORT_DIRECTORY].VirtualAddress);
104 fixup_rva_ptrs((char*)hinstance + (unsigned)pExpDir->AddressOfFunctions,
105 (LPVOID)hinstance,
106 pExpDir->NumberOfFunctions );
107 }
[8877]108 }
109 else {
[6471]110 name = OSLibGetDllName(hinstance);
[8877]111 }
[6401]112
[8877]113 strcpy(szFileName, name);
114 strupr(szFileName);
[978]115
[8877]116 setFullPath(szFileName);
[1131]117
[8877]118 //Pointer to PE resource tree generates by wrc (or NULL for system dlls)
119 pResRootDir = (PIMAGE_RESOURCE_DIRECTORY)pResData;
[978]120
[8877]121 //ulRVAResourceSection contains the virtual address of the imagebase in the PE header
122 //for the resource section (images loaded by the pe.exe)
123 //For LX images, this is 0 as OffsetToData contains a relative offset
124 ulRVAResourceSection = 0;
[956]125}
126//******************************************************************************
127//******************************************************************************
128Win32LxImage::~Win32LxImage()
129{
[8877]130 if(header) {
131 DosFreeMem(header);
132 }
[956]133}
134//******************************************************************************
135//******************************************************************************
[1844]136ULONG Win32LxImage::getApi(char *name)
137{
[8877]138 APIRET rc;
139 ULONG apiaddr;
[956]140
[10397]141 if(pCustomPEHeader) return Win32ImageBase::getApi(name);
142
[8877]143 rc = DosQueryProcAddr(hinstanceOS2, 0, name, (PFN *)&apiaddr);
144 if(rc)
145 {
[9411]146 dprintf(("Win32LxImage::getApi %x %s -> rc = %d", hinstanceOS2, name, rc));
147 return(0);
[8877]148 }
149 return(apiaddr);
[1844]150}
151//******************************************************************************
152//******************************************************************************
153ULONG Win32LxImage::getApi(int ordinal)
154{
[8877]155 APIRET rc;
156 ULONG apiaddr;
[1844]157
[10397]158 if(pCustomPEHeader) return Win32ImageBase::getApi(ordinal);
159
160 rc = DosQueryProcAddr(hinstanceOS2, ordinal, NULL, (PFN *)&apiaddr);
[8877]161 if(rc) {
162 dprintf(("Win32LxImage::getApi %x %d -> rc = %d", hinstanceOS2, ordinal, rc));
163 return(0);
164 }
165 return(apiaddr);
[1844]166}
167//******************************************************************************
168//******************************************************************************
[10397]169ULONG Win32LxImage::setApi(char *name, ULONG pfnNewProc)
170{
171 if(pCustomPEHeader) return Win32ImageBase::setApi(name, pfnNewProc);
172
173 return -1;
174}
175//******************************************************************************
176//******************************************************************************
177ULONG Win32LxImage::setApi(int ordinal, ULONG pfnNewProc)
178{
179 if(pCustomPEHeader) return Win32ImageBase::setApi(ordinal, pfnNewProc);
180
181 return -1;
182}
183//******************************************************************************
184//******************************************************************************
[3993]185LPVOID Win32LxImage::buildHeader(DWORD MajorImageVersion, DWORD MinorImageVersion,
[10397]186 DWORD Subsystem)
[3993]187{
[8877]188 APIRET rc;
189 IMAGE_DOS_HEADER *pdosheader;
[10397]190 PIMAGE_OPTIONAL_HEADER poh;
191 PIMAGE_FILE_HEADER pfh;
192 PIMAGE_SECTION_HEADER psh;
193 PIMAGE_EXPORT_DIRECTORY ped;
194
195 if(pCustomPEHeader)
196 {
197 return (LPVOID)pCustomPEHeader;
198 }
199
[8877]200 DWORD *ntsig;
[1844]201
[8877]202 rc = DosAllocMem(&header, 4096, PAG_READ | PAG_WRITE | PAG_COMMIT | flAllocMem);
203 if(rc) {
204 dprintf(("ERROR: buildHeader DosAllocMem failed!! (rc=%x)", rc));
[3993]205 DebugInt3();
[9411]206 return NULL;
[8877]207 }
208 memcpy(header, dosHeader, sizeof(dosHeader));
209 ntsig = (DWORD *)((LPBYTE)header + sizeof(dosHeader));
210 *ntsig = IMAGE_NT_SIGNATURE;
211 pfh = (PIMAGE_FILE_HEADER)(ntsig+1);
212 pfh->Machine = IMAGE_FILE_MACHINE_I386;
213 pfh->NumberOfSections = 0;
214 pfh->TimeDateStamp = 0x3794f60f;
215 pfh->PointerToSymbolTable = 0;
216 pfh->NumberOfSymbols = 0;
217 pfh->SizeOfOptionalHeader = sizeof(IMAGE_OPTIONAL_HEADER);
[10397]218 pfh->Characteristics = IMAGE_FILE_DLL | IMAGE_FILE_32BIT_MACHINE |
219 IMAGE_FILE_DEBUG_STRIPPED | IMAGE_FILE_EXECUTABLE_IMAGE |
[8877]220 IMAGE_FILE_RELOCS_STRIPPED;
221 poh = (PIMAGE_OPTIONAL_HEADER)(pfh+1);
222 poh->Magic = IMAGE_NT_OPTIONAL_HDR_MAGIC;
223 poh->MajorLinkerVersion = 0x3;
224 poh->MinorLinkerVersion = 0xA;
225 poh->SizeOfCode = 0;
226 poh->SizeOfInitializedData = 0;
227 poh->SizeOfUninitializedData = 0;
228 poh->AddressOfEntryPoint = 0;
229 poh->BaseOfCode = 0;
230 poh->BaseOfData = 0;
231 poh->ImageBase = 0;
232 poh->SectionAlignment = 4096;
233 poh->FileAlignment = 512;
234 poh->MajorOperatingSystemVersion = MajorImageVersion;
235 poh->MinorOperatingSystemVersion = MinorImageVersion;
236 poh->MajorImageVersion = MajorImageVersion;
237 poh->MinorImageVersion = MinorImageVersion;
238 poh->MajorSubsystemVersion = ODINNT_MAJOR_VERSION;
239 poh->MinorSubsystemVersion = ODINNT_MINOR_VERSION;
[9971]240 poh->Win32VersionValue = 0;
[8877]241 poh->SizeOfImage = 0;
242 poh->SizeOfHeaders = 1024;
243 poh->CheckSum = 0;
244 poh->Subsystem = Subsystem;
245 poh->DllCharacteristics = 0;
246 poh->SizeOfStackReserve = 1*1024*1024;
247 poh->SizeOfStackCommit = 4096;
248 poh->SizeOfHeapReserve = 1*1024*1024;
249 poh->SizeOfHeapCommit = 4096;
250 poh->LoaderFlags = 0;
251 poh->NumberOfRvaAndSizes = 0;
[3993]252
[8877]253 return header;
[3993]254}
255//******************************************************************************
256//******************************************************************************
Note: See TracBrowser for help on using the repository browser.