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