source: trunk/src/win32k/ldr/myldrOpen.cpp@ 1678

Last change on this file since 1678 was 1678, checked in by bird, 26 years ago

Some bugsfixes - Yield is disabled.
Added parameters.
Correcte moduleheaders.
Introduced a new base class for virtual lx modules + some elf sketches.

File size: 7.1 KB
Line 
1/* $Id: myldrOpen.cpp,v 1.5 1999-11-10 01:45:36 bird Exp $
2 *
3 * myldrOpen - _ldrOpen.
4 *
5 * Copyright (c) 1998-1999 knut st. osmundsen
6 *
7 * Project Odin Software License can be found in LICENSE.TXT
8 *
9 */
10
11
12/*******************************************************************************
13* Defined Constants And Macros *
14*******************************************************************************/
15#define INCL_DOSERRORS
16#define INCL_NOPMAPI
17
18/*******************************************************************************
19* Header Files *
20*******************************************************************************/
21#include <os2.h>
22
23#include "malloc.h"
24#include <memory.h>
25#include <stdlib.h>
26
27#include "log.h"
28#include <peexe.h>
29#include <exe386.h>
30#include "OS2Krnl.h"
31#include "ModuleBase.h"
32#include "pe2lx.h"
33#include "elf.h"
34#include "avl.h"
35#include "ldr.h"
36#include "ldrCalls.h"
37#include "options.h"
38
39
40/**
41 * _ldrOpen override.
42 * @returns Return code.
43 * @param phFile Pointer to file handler. Holds filehandle on output.
44 * @param pszFilename Pointer to filename.
45 * @parma param3 Probably some flags.
46 */
47ULONG LDRCALL myldrOpen(PSFN phFile, char *pszFilename, ULONG param3)
48{
49 ULONG rc;
50 int i;
51
52 rc = _ldrOpen(phFile, pszFilename, param3);
53
54 if (rc == NO_ERROR)
55 kprintf(("_ldrOpen: phFile=%#.4x, flags=%#.8x, pszFn=%s\n", *phFile, param3, pszFilename));
56
57 if (rc == NO_ERROR && (options.fElf || options.fPE != FLAGS_PE_NOT || options.fScript))
58 {
59 static char achBuffer[sizeof(IMAGE_DOS_HEADER)];
60 PIMAGE_DOS_HEADER pMzHdr = (PIMAGE_DOS_HEADER)&achBuffer[0];
61 PIMAGE_NT_HEADERS pNtHdrs = (PIMAGE_NT_HEADERS)&achBuffer[0]; /* oops. Last accessible field is OptionalHeader.FileAlignment */
62 char *pach = &achBuffer[0];
63
64 /**
65 * See if this is an recognizable module format.
66 * This costs up to two disk reads!
67 */
68 rc = _ldrRead(*phFile, 0UL, pMzHdr, 0UL, sizeof(IMAGE_DOS_HEADER), NULL);
69 if (rc == NO_ERROR)
70 {
71 if ((pMzHdr->e_magic == IMAGE_DOS_SIGNATURE &&
72 pMzHdr->e_lfanew > sizeof(IMAGE_DOS_HEADER) && pMzHdr->e_lfanew < 0x04000000UL) /* Larger than 64 bytes and less that 64MB. */
73 || *(PULONG)pach == IMAGE_NT_SIGNATURE)
74 { /* MZ or PE header found */
75 if (options.fPE == FLAGS_PE_NOT)
76 return NO_ERROR;
77
78 if (*(PULONG)pach != IMAGE_NT_SIGNATURE)
79 rc = _ldrRead(*phFile, pMzHdr->e_lfanew, pach, 0UL, sizeof(achBuffer), NULL);
80
81 if (rc == NO_ERROR && *(PULONG)pach == IMAGE_NT_SIGNATURE)
82 { /* PE signature found */
83 kprintf(("_ldrOpen: PE executable...\n"));
84 if (options.fPE == FLAGS_PE_PE2LX
85 || (options.fPE == FLAGS_PE_MIXED
86 && !((pNtHdrs->FileHeader.Characteristics & IMAGE_FILE_DLL == 0UL)
87 && pNtHdrs->OptionalHeader.ImageBase >= 0x04000000UL /* 64MB */
88 )
89 )
90 )
91 { /* pe2lx */
92 Pe2Lx * pPe2Lx = new Pe2Lx(*phFile);
93 if (pPe2Lx != NULL)
94 {
95 rc = pPe2Lx->init(pszFilename);
96 if (rc == NO_ERROR)
97 {
98 kprintf(("_ldrOpen: Successfully init of Pe2Lx object.\n"));
99 rc = addModule(*phFile, NULL, MOD_TYPE_PE2LX, pPe2Lx);
100 if (rc == NO_ERROR)
101 SetState(*phFile, HSTATE_OUR);
102 else
103 kprintf(("_ldrOpen: Failed to add the module. rc=%d\n"));
104 }
105 else
106 kprintf(("_ldrOpen: Failed to init Pe2Lx object. rc=%d\n"));
107 if (rc != NO_ERROR)
108 delete pPe2Lx;
109 }
110 else
111 kprintf(("_ldrOpen: Failed to allocate Pe2Lx object.\n"));
112 }
113 else
114 if (options.fPE == FLAGS_PE_PE || options.fPE == FLAGS_PE_MIXED)
115 { /* pe.exe */
116 kprintf(("_ldrOpen: pe.exe - opening\n"));
117 _ldrClose(*phFile);
118 rc = _ldrOpen(phFile, "pe.exe", param3); /* path....! problems! */
119 kprintf(("_ldrOpen: pe.exe - open returned with rc = %d\n", rc));
120 return rc;
121 }
122 }
123 rc = NO_ERROR;
124 }
125 else
126 {
127 if (pach[0] == ELFMAG0 && pach[1] == ELFMAG1 && pach[2] == ELFMAG2 && pach[3] == ELFMAG3)
128 {
129 /* ELF signature found */
130 kprintf(("_ldrOpen: ELF executable! - not implemented yet!\n"));
131 }
132 else
133 if (*pach == '#' && pach[1] == '!')
134 {
135 /* unix styled script...? Must be more than 64 bytes long? No options. firstline < 64 bytes. */
136 char *pszStart = pach+2;
137 char *pszEnd;
138 kprintf(("_ldrOpen: unix script?\n"));
139
140 achBuffer[sizeof(achBuffer)-1] = '\0'; /* just to make sure we don't read to much... */
141
142 /* skip blanks */
143 while (*pszStart != '\0' && (*pszStart == ' ' || *pszStart == '\t'))
144 pszStart++;
145 if (*pszStart != '\0' && *pszStart != '\r' && *pszStart != '\n')
146 { /* find end-of-word */
147 pszEnd = pszStart + 1;
148 while (*pszEnd != '\0' && *pszEnd != '\n' && *pszEnd != '\r'
149 && *pszEnd != '\t' && *pszEnd != ' ')
150 pszEnd++;
151 if (*pszEnd != '\0')
152 {
153 *pszEnd = '\0';
154 kprintf(("_ldrOpen: unix script - opening %s\n", pszStart));
155 _ldrClose(*phFile);
156 rc = _ldrOpen(phFile, pszStart, param3);
157 kprintf(("_ldrOpen: unix script - open returned with rc = %d\n", rc));
158 }
159 }
160 else
161 kprintf(("_ldrOpen: unix script - unexpected end of line/file. (line: %.10s\n", pach));
162 }
163 }
164 }
165 else
166 {
167 kprintf(("_ldrOpen: _ldrRead failed with rc=%d when reading DosHdr.\n", rc));
168 rc = NO_ERROR;
169 }
170 }
171 return rc;
172}
Note: See TracBrowser for help on using the repository browser.