source: trunk/src/kernel32/winimagelx_new.cpp@ 5841

Last change on this file since 5841 was 5841, checked in by phaller, 24 years ago

.

File size: 7.9 KB
Line 
1/* $Id: winimagelx_new.cpp,v 1.1 2001-05-30 18:32:15 phaller 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 <pefile.h>
36#include <unicode.h>
37#include "oslibmisc.h"
38#include "initterm.h"
39#include <win\virtual.h>
40
41#include <ccollection.h>
42
43
44#define DBG_LOCALLOG DBG_winimagelx
45#include "dbglocal.h"
46
47static BYTE dosHeader[] = {
48 0x4D, 0x5A, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x0B, 0x00,
49 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
50 0x6A, 0x72, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
51 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0, 0x00, 0x00, 0x00,
52 0xB4, 0x30, 0xCD, 0x21, 0x86, 0xC4, 0x3D, 0x0A, 0x14, 0x72, 0x42, 0xBE, 0x80, 0x00, 0x8A, 0x1C,
53 0x32, 0xFF, 0x46, 0x88, 0x38, 0x2E, 0x8C, 0x1E, 0x6E, 0x00, 0x8E, 0x06, 0x2C, 0x00, 0x33, 0xC0,
54 0x8B, 0xF8, 0xB9, 0x00, 0x80, 0xFC, 0xF2, 0xAE, 0x75, 0x23, 0x49, 0x78, 0x20, 0xAE, 0x75, 0xF6,
55 0x47, 0x47, 0x0E, 0x1F, 0x2E, 0x89, 0x3E, 0x68, 0x00, 0x2E, 0x8C, 0x06, 0x6A, 0x00, 0xBE, 0x5C,
56 0x00, 0xB9, 0x6C, 0x63, 0xBB, 0x25, 0x00, 0xB4, 0x64, 0xCD, 0x21, 0x73, 0x0B, 0xBA, 0x7C, 0x00,
57 0x0E, 0x1F, 0xB4, 0x09, 0xCD, 0x21, 0xB0, 0x01, 0xB4, 0x4C, 0xCD, 0x21, 0x20, 0x00, 0x01, 0x00,
58 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x81, 0x00, 0x00, 0x00,
59 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x54, 0x68, 0x69, 0x73,
60 0x20, 0x70, 0x72, 0x6F, 0x67, 0x72, 0x61, 0x6D, 0x20, 0x6D, 0x75, 0x73, 0x74, 0x20, 0x62, 0x65,
61 0x20, 0x72, 0x75, 0x6E, 0x20, 0x75, 0x6E, 0x64, 0x65, 0x72, 0x20, 0x57, 0x69, 0x6E, 0x33, 0x32,
62 0x2E, 0x0D, 0x0A, 0x24, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
63};
64
65//******************************************************************************
66//******************************************************************************
67Win32LxImage::Win32LxImage(HINSTANCE hInstance, PVOID pResData)
68 : Win32ImageBase(hInstance), header(0)
69{
70 APIRET rc;
71
72 szFileName[0] = 0;
73
74 char *name = OSLibGetDllName(hinstance);
75 strcpy(szFileName, name);
76 strupr(szFileName);
77
78 setFullPath(szFileName);
79
80 //Pointer to PE resource tree generates by wrc (or NULL for system dlls)
81 pResRootDir = (PIMAGE_RESOURCE_DIRECTORY)pResData;
82
83 //ulRVAResourceSection contains the virtual address of the imagebase in the PE header
84 //for the resource section (images loaded by the pe.exe)
85 //For LX images, this is 0 as OffsetToData contains a relative offset
86 ulRVAResourceSection = 0;
87
88 // create objects for fast API lookup
89
90 // @@@PH look for way to reduce memory consumption
91 // Note: never seen a DLL export > 8192 or something
92 pLookupOrdinal = new CIndexLookupLimit(0, 8192);
93 pLookupName = new CHashtableLookup(503);
94}
95//******************************************************************************
96//******************************************************************************
97Win32LxImage::~Win32LxImage()
98{
99 if (pLookupName)
100 delete pLookupName;
101
102 if (pLookupOrdinal)
103 delete pLookupOrdinal;
104
105 if(header)
106 DosFreeMem(header);
107}
108//******************************************************************************
109//******************************************************************************
110ULONG Win32LxImage::getApi(char *name)
111{
112 APIRET rc;
113 ULONG apiaddr;
114
115 // check lookup cache first
116 apiaddr = (ULONG)pLookupName->getElement(name);
117 if (apiaddr)
118 return apiaddr;
119
120 rc = DosQueryProcAddr(hinstanceOS2, 0, name, (PFN *)&apiaddr);
121 if(rc)
122 {
123 dprintf(("Win32LxImage::getApi -> rc = %d", rc));
124 return(0);
125 }
126
127 // add to cache
128 pLookupName->addElement(name, (void*)apiaddr);
129
130 return(apiaddr);
131}
132//******************************************************************************
133//******************************************************************************
134ULONG Win32LxImage::getApi(int ordinal)
135{
136 APIRET rc;
137 ULONG apiaddr;
138
139 // check lookup cache first
140 apiaddr = (ULONG)pLookupOrdinal->getElement(ordinal);
141 if (apiaddr)
142 return apiaddr;
143
144 rc = DosQueryProcAddr(hinstanceOS2, ordinal, NULL, (PFN *)&apiaddr);
145 if(rc)
146 {
147 dprintf(("Win32LxImage::getApi -> rc = %d", rc));
148 return(0);
149 }
150
151 // add to cache
152 pLookupOrdinal->addElement(ordinal, (void*)apiaddr);
153
154 return(apiaddr);
155}
156//******************************************************************************
157//******************************************************************************
158LPVOID Win32LxImage::buildHeader(DWORD MajorImageVersion, DWORD MinorImageVersion,
159 DWORD Subsystem)
160{
161 APIRET rc;
162 IMAGE_DOS_HEADER *pdosheader;
163 PIMAGE_OPTIONAL_HEADER poh;
164 PIMAGE_FILE_HEADER pfh;
165 DWORD *ntsig;
166
167 rc = DosAllocMem(&header, 4096, PAG_READ | PAG_WRITE | PAG_COMMIT);
168 if(rc) {
169 dprintf(("ERROR: buildHeader DosAllocMem failed!! (rc=%x)", rc));
170 DebugInt3();
171 return NULL;
172 }
173 memcpy(header, dosHeader, sizeof(dosHeader));
174 ntsig = (DWORD *)((LPBYTE)header + sizeof(dosHeader));
175 *ntsig = IMAGE_NT_SIGNATURE;
176 pfh = (PIMAGE_FILE_HEADER)(ntsig+1);
177 pfh->Machine = IMAGE_FILE_MACHINE_I386;
178 pfh->NumberOfSections = 0;
179 pfh->TimeDateStamp = 0x3794f60f;
180 pfh->PointerToSymbolTable = 0;
181 pfh->NumberOfSymbols = 0;
182 pfh->SizeOfOptionalHeader = sizeof(IMAGE_OPTIONAL_HEADER);
183 pfh->Characteristics = IMAGE_FILE_DLL | IMAGE_FILE_32BIT_MACHINE |
184 IMAGE_FILE_DEBUG_STRIPPED | IMAGE_FILE_EXECUTABLE_IMAGE |
185 IMAGE_FILE_RELOCS_STRIPPED;
186 poh = (PIMAGE_OPTIONAL_HEADER)(pfh+1);
187 poh->Magic = IMAGE_NT_OPTIONAL_HDR_MAGIC;
188 poh->MajorLinkerVersion = 0x3;
189 poh->MinorLinkerVersion = 0xA;
190 poh->SizeOfCode = 0;
191 poh->SizeOfInitializedData = 0;
192 poh->SizeOfUninitializedData = 0;
193 poh->AddressOfEntryPoint = 0;
194 poh->BaseOfCode = 0;
195 poh->BaseOfData = 0;
196 poh->ImageBase = 0;
197 poh->SectionAlignment = 4096;
198 poh->FileAlignment = 512;
199 poh->MajorOperatingSystemVersion = MajorImageVersion;
200 poh->MinorOperatingSystemVersion = MinorImageVersion;
201 poh->MajorImageVersion = MajorImageVersion;
202 poh->MinorImageVersion = MinorImageVersion;
203 poh->MajorSubsystemVersion = ODINNT_MAJOR_VERSION;
204 poh->MinorSubsystemVersion = ODINNT_MINOR_VERSION;
205 poh->Reserved1 = 0;
206 poh->SizeOfImage = 0;
207 poh->SizeOfHeaders = 1024;
208 poh->CheckSum = 0;
209 poh->Subsystem = Subsystem;
210 poh->DllCharacteristics = 0;
211 poh->SizeOfStackReserve = 1*1024*1024;
212 poh->SizeOfStackCommit = 4096;
213 poh->SizeOfHeapReserve = 1*1024*1024;
214 poh->SizeOfHeapCommit = 4096;
215 poh->LoaderFlags = 0;
216 poh->NumberOfRvaAndSizes = 0;
217// poh->DataDirectory[0]
218
219 return header;
220}
221//******************************************************************************
222//******************************************************************************
Note: See TracBrowser for help on using the repository browser.