1 | /* $Id: myldrOpen.cpp,v 1.10 2000-02-21 09:24:01 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 | #define INCL_OS2KRNL_IO
|
---|
19 |
|
---|
20 | /*******************************************************************************
|
---|
21 | * Header Files *
|
---|
22 | *******************************************************************************/
|
---|
23 | #include <os2.h>
|
---|
24 |
|
---|
25 | #include "rmalloc.h"
|
---|
26 | #include "malloc.h"
|
---|
27 | #include <memory.h>
|
---|
28 | #include <stdlib.h>
|
---|
29 | #include <string.h>
|
---|
30 |
|
---|
31 | #include "log.h"
|
---|
32 | #include <peexe.h>
|
---|
33 | #include <exe386.h>
|
---|
34 | #include "OS2Krnl.h"
|
---|
35 | #include "dev32.h"
|
---|
36 | #include "ModuleBase.h"
|
---|
37 | #include "pe2lx.h"
|
---|
38 | #include "elf.h"
|
---|
39 | #include "avl.h"
|
---|
40 | #include "ldr.h"
|
---|
41 | #include "ldrCalls.h"
|
---|
42 | #include "options.h"
|
---|
43 | #include "myExecPgm.h"
|
---|
44 |
|
---|
45 | /*******************************************************************************
|
---|
46 | * Global Variables *
|
---|
47 | *******************************************************************************/
|
---|
48 | extern BOOL fQAppType; /* From LDRQAppType */
|
---|
49 |
|
---|
50 | /*******************************************************************************
|
---|
51 | * Internal Functions *
|
---|
52 | *******************************************************************************/
|
---|
53 | static unsigned getArgsLength(const char *pachArgs);
|
---|
54 |
|
---|
55 |
|
---|
56 | /**
|
---|
57 | * ldrOpen override.
|
---|
58 | * @returns Return code.
|
---|
59 | * @param phFile Pointer to file handler. Holds filehandle on output.
|
---|
60 | * @param pszFilename Pointer to filename.
|
---|
61 | * @parma param3 Probably some flags.
|
---|
62 | */
|
---|
63 | ULONG LDRCALL myldrOpen(PSFN phFile, char *pszFilename, ULONG param3)
|
---|
64 | {
|
---|
65 | ULONG rc;
|
---|
66 |
|
---|
67 | /*
|
---|
68 | * Try open the file (thats why this function is called anyway)
|
---|
69 | */
|
---|
70 | rc = ldrOpen(phFile, pszFilename, param3);
|
---|
71 |
|
---|
72 | /* log sucesses */
|
---|
73 | if (rc == NO_ERROR)
|
---|
74 | kprintf(("ldrOpen: phFile=%#.4x, flags=%#.8x, pszFn=%s\n", *phFile, param3, pszFilename));
|
---|
75 |
|
---|
76 | /*
|
---|
77 | * Are we to intercept the loading?
|
---|
78 | * - Only if open were succesful and one of the loaders are enabled.
|
---|
79 | */
|
---|
80 | if (rc == NO_ERROR && (options.fElf || options.fPE != FLAGS_PE_NOT || options.fScript))
|
---|
81 | {
|
---|
82 | char *pszBuffer = (char*)rmalloc(640); /* Read buffer. */
|
---|
83 | PIMAGE_DOS_HEADER pMzHdr = (PIMAGE_DOS_HEADER)pszBuffer; /* Pointer to the buffer as it were a dosheader. */
|
---|
84 | PIMAGE_NT_HEADERS pNtHdrs = (PIMAGE_NT_HEADERS)pszBuffer; /* Pointer to the buffer as if it were an NT header. */
|
---|
85 | char *pach = pszBuffer; /* Finally an pointer to the buffer as if it were chars.. (which it is!) */
|
---|
86 | PEXECPGMBUFFER pBuffer; /* Pointer to a buffer containing the programname and arguments. */
|
---|
87 | /* For scripts and PE.EXE this has to be changed to have correct */
|
---|
88 | /* parameters sendt in to the program. */
|
---|
89 | unsigned cchRead = sizeof(IMAGE_DOS_HEADER); /* Amount of the buffer which contains valid data. */
|
---|
90 | unsigned cbFile; /* Filesize (0xffffffff if call to SftFileSize failed - should _never_ happen though) */
|
---|
91 |
|
---|
92 | /*
|
---|
93 | * Verify that rmalloc completed successfully.
|
---|
94 | */
|
---|
95 | if (pszBuffer == NULL)
|
---|
96 | {
|
---|
97 | kprintf(("ldrOpen: rmalloc(1024) failed\n"));
|
---|
98 | return NO_ERROR;
|
---|
99 | }
|
---|
100 |
|
---|
101 | /*
|
---|
102 | * Try get the filesize
|
---|
103 | */
|
---|
104 | rc = SftFileSize(*phFile, (PULONG)SSToDS(&cbFile));
|
---|
105 | if (rc != NO_ERROR)
|
---|
106 | {
|
---|
107 | kprintf(("ldrOpen: SftFileSize failed with rc=%d\n", rc));
|
---|
108 | cbFile = (unsigned)~0;
|
---|
109 | }
|
---|
110 |
|
---|
111 | /*
|
---|
112 | * See if this is an recognizable module format.
|
---|
113 | * This costs up to two disk reads!
|
---|
114 | */
|
---|
115 | rc = ldrRead(*phFile, 0UL, pMzHdr, 0UL, cchRead, NULL);
|
---|
116 | if (rc == NO_ERROR)
|
---|
117 | {
|
---|
118 | /*
|
---|
119 | * PE header?
|
---|
120 | * - If DOS Magic is found AND a valid e_lfanew (offset of NE/LX/LE/PE header) is found
|
---|
121 | * - OR if PE siganture is found.
|
---|
122 | */
|
---|
123 | if ((pMzHdr->e_magic == IMAGE_DOS_SIGNATURE &&
|
---|
124 | pMzHdr->e_lfanew > sizeof(IMAGE_DOS_HEADER) && pMzHdr->e_lfanew < 0x04000000UL) /* Larger than 64 bytes and less that 64MB. */
|
---|
125 | || *(PULONG)pach == IMAGE_NT_SIGNATURE)
|
---|
126 | { /*
|
---|
127 | * MZ or PE header found
|
---|
128 | */
|
---|
129 |
|
---|
130 | /* if PE loading is diable return to the caller */
|
---|
131 | if (options.fPE == FLAGS_PE_NOT)
|
---|
132 | {
|
---|
133 | rfree(pszBuffer);
|
---|
134 | return NO_ERROR;
|
---|
135 | }
|
---|
136 |
|
---|
137 | /*
|
---|
138 | * Read the PE header if it isn't what we allready have!
|
---|
139 | */
|
---|
140 | cchRead = sizeof(IMAGE_NT_HEADERS);
|
---|
141 | if (*(PULONG)pach != IMAGE_NT_SIGNATURE)
|
---|
142 | rc = ldrRead(*phFile, pMzHdr->e_lfanew, pach, 0UL, cchRead, NULL);
|
---|
143 | else
|
---|
144 | rc = ldrRead(*phFile, 0UL, pach, 0UL, cchRead, NULL);
|
---|
145 |
|
---|
146 | /*
|
---|
147 | * If successfully read, and a PE signature is present the continue and try load it!
|
---|
148 | * Else don't do anything, simply return NO_ERROR to the caller. (probably NE or LX exec)
|
---|
149 | */
|
---|
150 | if (rc == NO_ERROR && *(PULONG)pach == IMAGE_NT_SIGNATURE)
|
---|
151 | { /*
|
---|
152 | * PE signature found.
|
---|
153 | */
|
---|
154 | kprintf(("ldrOpen: PE executable...\n"));
|
---|
155 |
|
---|
156 | /*
|
---|
157 | * PE2LX?
|
---|
158 | * - When PE2LX flag is set
|
---|
159 | * - OR when the MIXED flag is set and the image is with the first 64MB of memory.
|
---|
160 | */
|
---|
161 | if (options.fPE == FLAGS_PE_PE2LX
|
---|
162 | || (options.fPE == FLAGS_PE_MIXED
|
---|
163 | && !((pNtHdrs->FileHeader.Characteristics & IMAGE_FILE_DLL == 0UL)
|
---|
164 | && pNtHdrs->OptionalHeader.ImageBase >= 0x04000000UL /* 64MB */
|
---|
165 | )
|
---|
166 | )
|
---|
167 | )
|
---|
168 | { /*
|
---|
169 | * Pe2Lx (Ring0 of course)
|
---|
170 | * - Create a Pe2Lx class,
|
---|
171 | * - initiate it
|
---|
172 | * - Add the module to the module tree so we may find it later...
|
---|
173 | * - Set the handle state to 'our'.
|
---|
174 | */
|
---|
175 | Pe2Lx * pPe2Lx = new Pe2Lx(*phFile);
|
---|
176 | if (pPe2Lx != NULL)
|
---|
177 | {
|
---|
178 | rc = pPe2Lx->init(pszFilename);
|
---|
179 | if (rc == NO_ERROR)
|
---|
180 | {
|
---|
181 | kprintf(("ldrOpen: Successfully init of Pe2Lx object.\n"));
|
---|
182 | rc = addModule(*phFile, NULL, MOD_TYPE_PE2LX, pPe2Lx);
|
---|
183 | if (rc == NO_ERROR)
|
---|
184 | #pragma info(notrd)
|
---|
185 | SetState(*phFile, HSTATE_OUR);
|
---|
186 | #pragma info(restore)
|
---|
187 | else
|
---|
188 | kprintf(("ldrOpen: Failed to add the module. rc=%d\n"));
|
---|
189 | }
|
---|
190 | else
|
---|
191 | kprintf(("ldrOpen: Failed to init Pe2Lx object. rc=%d\n"));
|
---|
192 | if (rc != NO_ERROR)
|
---|
193 | delete pPe2Lx;
|
---|
194 | }
|
---|
195 | else
|
---|
196 | kprintf(("ldrOpen: Failed to allocate Pe2Lx object.\n"));
|
---|
197 | }
|
---|
198 | else
|
---|
199 | {
|
---|
200 | /*
|
---|
201 | * Starting of PE.EXE enable?
|
---|
202 | */
|
---|
203 | if (options.fPE == FLAGS_PE_PE || options.fPE == FLAGS_PE_MIXED)
|
---|
204 | { /*
|
---|
205 | * pe.exe - need the path!
|
---|
206 | */
|
---|
207 | kprintf(("ldrOpen: pe.exe - opening\n"));
|
---|
208 | ldrClose(*phFile);
|
---|
209 | rc = ldrOpen(phFile, "pe.exe", param3); /* path....! problems! */
|
---|
210 | kprintf(("ldrOpen: pe.exe - open returned with rc = %d\n", rc));
|
---|
211 | rfree(pszBuffer);
|
---|
212 | return rc;
|
---|
213 | }
|
---|
214 | }
|
---|
215 | }
|
---|
216 | rfree(pszBuffer);
|
---|
217 | return NO_ERROR;
|
---|
218 | }
|
---|
219 | else
|
---|
220 | {
|
---|
221 | /*
|
---|
222 | * ELF image?
|
---|
223 | */
|
---|
224 | if (pach[0] == ELFMAG0 && pach[1] == ELFMAG1 && pach[2] == ELFMAG2 && pach[3] == ELFMAG3)
|
---|
225 | {
|
---|
226 | /*
|
---|
227 | * ELF signature found.
|
---|
228 | */
|
---|
229 | kprintf(("ldrOpen: ELF executable! - not implemented yet!\n"));
|
---|
230 |
|
---|
231 | /*
|
---|
232 | * Do nothing more yet. NEED AN ELF LOADER!!!
|
---|
233 | */
|
---|
234 | rfree(pszBuffer);
|
---|
235 | return NO_ERROR;
|
---|
236 | }
|
---|
237 | }
|
---|
238 | }
|
---|
239 |
|
---|
240 | /*
|
---|
241 | * Only unreconized files and readerror passes this point!
|
---|
242 | *
|
---|
243 | * * Fileformats with lower priority should reside here. *
|
---|
244 | *
|
---|
245 | */
|
---|
246 |
|
---|
247 | /*
|
---|
248 | * If the initial readoperation failed try to read a smaller amount, in case it is a small script...
|
---|
249 | * 4 bytes is a small amount isn't it?
|
---|
250 | */
|
---|
251 | if (rc != NO_ERROR)
|
---|
252 | {
|
---|
253 | kprintf(("ldrOpen: first ldrread failed with rc=%d. tries to read 4 byte.\n", rc));
|
---|
254 | cchRead = 4;
|
---|
255 | if ((rc = ldrRead(*phFile, 0UL, pach, 0UL, cchRead, NULL)) != NO_ERROR)
|
---|
256 | kprintf(("ldrOpen: second ldrread failed with rc=%d.\n ", rc));
|
---|
257 | }
|
---|
258 |
|
---|
259 | /*
|
---|
260 | * Now we'll try again, UNIX styled script?
|
---|
261 | */
|
---|
262 | if (rc == NO_ERROR && *pach == '#' && pach[1] == '!')
|
---|
263 | {
|
---|
264 | /*
|
---|
265 | * UNIX styled script?
|
---|
266 | * FIXME! Must be more than 64 bytes long?
|
---|
267 | * No options!
|
---|
268 | * Firstline < 64 bytes!
|
---|
269 | */
|
---|
270 | kprintf(("ldrOpen: unix script?\n"));
|
---|
271 | cchRead = min(cbFile, 256);
|
---|
272 | rc = ldrRead(*phFile, 0UL, pach, 0UL, cchRead, NULL);
|
---|
273 | if (rc == NO_ERROR)
|
---|
274 | {
|
---|
275 | char *pszStart = pach+2;
|
---|
276 | kprintf(("ldrOpen: script debug 1\n"));
|
---|
277 |
|
---|
278 | /* Make sure we don't read to much... */
|
---|
279 | pszBuffer[cchRead] = '\0';
|
---|
280 |
|
---|
281 | /*
|
---|
282 | * Skip blanks
|
---|
283 | */
|
---|
284 | pszStart = pszBuffer + 2; /* skips the "#!" stuff. */
|
---|
285 | while (*pszStart != '\0' && (*pszStart == ' ' || *pszStart == '\t'))
|
---|
286 | pszStart++;
|
---|
287 | kprintf(("ldrOpen: script debug 2\n"));
|
---|
288 |
|
---|
289 | /* anything left on the line? */
|
---|
290 | if (*pszStart != '\0' && *pszStart != '\r' && *pszStart != '\n')
|
---|
291 | {
|
---|
292 | char * pszEnd; /* Pointer to the end of the string(s) when the next step is finished. */
|
---|
293 | unsigned cchToAdd = 1; /* Chars to add */
|
---|
294 | BOOL fFirst = TRUE; /* Set if a '\0' has not been set yet.
|
---|
295 | * If this is clear, there are one or more parameters after the interpreter name. */
|
---|
296 |
|
---|
297 | /*
|
---|
298 | * find linesize and make parameters ready for copying
|
---|
299 | */
|
---|
300 | pszEnd = pszStart;
|
---|
301 | kprintf(("ldrOpen: script debug 3\n"));
|
---|
302 | while (*pszEnd != '\0' && *pszEnd != '\r' && *pszEnd != '\n')
|
---|
303 | {
|
---|
304 | if (fFirst && (*pszEnd == ' ' || *pszEnd == '\t'))
|
---|
305 | {
|
---|
306 | *pszEnd = '\0';
|
---|
307 | fFirst = FALSE;
|
---|
308 | if (pszEnd[1] == '\0' || pszEnd[1] == '\r' || pszEnd[1] == '\n')
|
---|
309 | fFirst = TRUE;
|
---|
310 | }
|
---|
311 |
|
---|
312 | /* next */
|
---|
313 | pszEnd++;
|
---|
314 | cchToAdd++;
|
---|
315 | }
|
---|
316 | *pszEnd = '\0';
|
---|
317 | kprintf(("ldrOpen: script debug 4\n"));
|
---|
318 |
|
---|
319 | /*
|
---|
320 | * If ldrQueryApp type we don't have any ExecPgm buffer we need to mess with.
|
---|
321 | * We'll simply try open the the interpreter.
|
---|
322 | */
|
---|
323 | if (fQAppType)
|
---|
324 | {
|
---|
325 | rc = ldrClose(*phFile);
|
---|
326 | rc = ldrOpen(phFile, pszStart, param3); /* FIXME, recusion! check that name not equal! Use flags to prevent race? */
|
---|
327 | }
|
---|
328 | else
|
---|
329 | {
|
---|
330 | /*
|
---|
331 | * Find the ExecPgm buffer.
|
---|
332 | */
|
---|
333 | pBuffer = QueryBufferPointerFromFilename(pszFilename);
|
---|
334 | kprintf(("ldrOpen: script debug 5\n"));
|
---|
335 | if (pBuffer != NULL)
|
---|
336 | {
|
---|
337 | unsigned cchArguments = getArgsLength(pBuffer->achArgs); /* minus the first argument. */
|
---|
338 | unsigned cchScriptnameDelta = strlen(pBuffer->szFilename) - strlen(pBuffer->achArgs); /* scriptname size difference. */
|
---|
339 |
|
---|
340 | kprintf(("ldrOpen: script debug 6\n"));
|
---|
341 |
|
---|
342 | /*
|
---|
343 | * Is there enough space in the struct?
|
---|
344 | */
|
---|
345 | if (cchArguments + cchToAdd + cchScriptnameDelta < sizeof(pBuffer->achArgs))
|
---|
346 | {
|
---|
347 | kprintf(("ldrOpen: script debug 7\n"));
|
---|
348 | /*
|
---|
349 | * Open the interpreter.
|
---|
350 | */
|
---|
351 | rc = ldrClose(*phFile);
|
---|
352 | rc = ldrOpen(phFile, pszStart, param3); /* FIXME, recusion! check that name not equal! Use flags to prevent race? */
|
---|
353 | if (rc == NO_ERROR)
|
---|
354 | {
|
---|
355 | kprintf(("ldrOpen: script debug 8\n"));
|
---|
356 | /* Make space for the addition arguments. */
|
---|
357 | #ifdef DEBUG
|
---|
358 | char *psz = &pBuffer->achArgs[0];
|
---|
359 | int i = 0;
|
---|
360 | while (*psz != '\0')
|
---|
361 | {
|
---|
362 | kprintf(("Arg %d: %s\n", i++, psz));
|
---|
363 | psz += 1 + strlen(psz);
|
---|
364 | }
|
---|
365 | #endif
|
---|
366 | memmove(&pBuffer->achArgs[cchToAdd + cchScriptnameDelta],
|
---|
367 | &pBuffer->achArgs[0], cchArguments);
|
---|
368 |
|
---|
369 | /*
|
---|
370 | * Copy the arguments.
|
---|
371 | */
|
---|
372 | kprintf(("ldrOpen: script debug 8\n"));
|
---|
373 | memcpy(&pBuffer->achArgs[0], pszStart, cchToAdd); /* Interpreter with arguments */
|
---|
374 | if (!fFirst)
|
---|
375 | pBuffer->achArgs[cchToAdd - 1] = ' ';
|
---|
376 | strcpy(&pBuffer->achArgs[cchToAdd], pszFilename); /* Scriptname */
|
---|
377 | kprintf(("ldrOpen: script debug a\n"));
|
---|
378 |
|
---|
379 | #ifdef DEBUG
|
---|
380 | psz = &pBuffer->achArgs[0];
|
---|
381 | i = 0;
|
---|
382 | while (*psz != '\0')
|
---|
383 | {
|
---|
384 | kprintf(("Arg %d: %s\n", i++, psz));
|
---|
385 | psz += 1 + strlen(psz);
|
---|
386 | }
|
---|
387 | #endif
|
---|
388 | }
|
---|
389 | else
|
---|
390 | kprintf(("ldrOpen: failed to open interpreter (%s), rc=%d\n", pszStart, rc));
|
---|
391 | }
|
---|
392 | else
|
---|
393 | {
|
---|
394 | kprintf(("ldrOpen: Argument buffer too small, %d\n", cchArguments + cchToAdd));
|
---|
395 | rc = ERROR_BAD_EXE_FORMAT;
|
---|
396 | }
|
---|
397 | }
|
---|
398 | else
|
---|
399 | {
|
---|
400 | kprintf(("ldrOpen: QueryBufferPointerFromFilename failed.\n"));
|
---|
401 | rc = ERROR_BAD_EXE_FORMAT; /*?*/
|
---|
402 | }
|
---|
403 | }
|
---|
404 | }
|
---|
405 | else
|
---|
406 | {
|
---|
407 | kprintf(("ldrOpen: no interpereter on the first line.\n"));
|
---|
408 | rc = ERROR_BAD_EXE_FORMAT; /*?*/
|
---|
409 | }
|
---|
410 | }
|
---|
411 | else
|
---|
412 | {
|
---|
413 | kprintf(("ldrOpen: read of min(cbFile, 256) = %d failed, rc = %d\n", cchRead, rc));
|
---|
414 | }
|
---|
415 | } /* else inn other formats here. */
|
---|
416 | rfree(pszBuffer);
|
---|
417 | }
|
---|
418 | return rc;
|
---|
419 | }
|
---|
420 |
|
---|
421 |
|
---|
422 | /**
|
---|
423 | * Get the lenght of the arguments.
|
---|
424 | * @returns Lenght in char, includes the two '\0's.
|
---|
425 | * @param pachArgs Pointer to the ASCIIZs which makes up the arguments.
|
---|
426 | * @status completely implemented.
|
---|
427 | * @author knut st. osmundsen (knut.stange.osmundsen@pmsc.no)
|
---|
428 | */
|
---|
429 | static unsigned getArgsLength(const char *pachArgs)
|
---|
430 | {
|
---|
431 | unsigned cch = 1;
|
---|
432 | const char *psz = pachArgs;
|
---|
433 |
|
---|
434 | while (*psz != '\0')
|
---|
435 | {
|
---|
436 | register unsigned cch2 = strlen(psz);
|
---|
437 | cch += cch2;
|
---|
438 | psz += cch2 + 1;
|
---|
439 | }
|
---|
440 |
|
---|
441 | return cch;
|
---|
442 | }
|
---|