| 1 | /* $Id: myldrOpen.cpp,v 1.6 2000-01-22 18:21:02 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 | */
|
|---|
| 47 | ULONG LDRCALL myldrOpen(PSFN phFile, char *pszFilename, ULONG param3)
|
|---|
| 48 | {
|
|---|
| 49 | ULONG rc;
|
|---|
| 50 |
|
|---|
| 51 | rc = ldrOpen(phFile, pszFilename, param3);
|
|---|
| 52 |
|
|---|
| 53 | if (rc == NO_ERROR)
|
|---|
| 54 | kprintf(("ldrOpen: phFile=%#.4x, flags=%#.8x, pszFn=%s\n", *phFile, param3, pszFilename));
|
|---|
| 55 |
|
|---|
| 56 | if (rc == NO_ERROR && (options.fElf || options.fPE != FLAGS_PE_NOT || options.fScript))
|
|---|
| 57 | {
|
|---|
| 58 | static char achBuffer[sizeof(IMAGE_DOS_HEADER)];
|
|---|
| 59 | PIMAGE_DOS_HEADER pMzHdr = (PIMAGE_DOS_HEADER)&achBuffer[0];
|
|---|
| 60 | PIMAGE_NT_HEADERS pNtHdrs = (PIMAGE_NT_HEADERS)&achBuffer[0]; /* oops. Last accessible field is OptionalHeader.FileAlignment */
|
|---|
| 61 | char *pach = &achBuffer[0];
|
|---|
| 62 |
|
|---|
| 63 | /**
|
|---|
| 64 | * See if this is an recognizable module format.
|
|---|
| 65 | * This costs up to two disk reads!
|
|---|
| 66 | */
|
|---|
| 67 | rc = ldrRead(*phFile, 0UL, pMzHdr, 0UL, sizeof(IMAGE_DOS_HEADER), NULL);
|
|---|
| 68 | if (rc == NO_ERROR)
|
|---|
| 69 | {
|
|---|
| 70 | if ((pMzHdr->e_magic == IMAGE_DOS_SIGNATURE &&
|
|---|
| 71 | pMzHdr->e_lfanew > sizeof(IMAGE_DOS_HEADER) && pMzHdr->e_lfanew < 0x04000000UL) /* Larger than 64 bytes and less that 64MB. */
|
|---|
| 72 | || *(PULONG)pach == IMAGE_NT_SIGNATURE)
|
|---|
| 73 | { /* MZ or PE header found */
|
|---|
| 74 | if (options.fPE == FLAGS_PE_NOT)
|
|---|
| 75 | return NO_ERROR;
|
|---|
| 76 |
|
|---|
| 77 | if (*(PULONG)pach != IMAGE_NT_SIGNATURE)
|
|---|
| 78 | rc = ldrRead(*phFile, pMzHdr->e_lfanew, pach, 0UL, sizeof(achBuffer), NULL);
|
|---|
| 79 |
|
|---|
| 80 | if (rc == NO_ERROR && *(PULONG)pach == IMAGE_NT_SIGNATURE)
|
|---|
| 81 | { /* PE signature found */
|
|---|
| 82 | kprintf(("ldrOpen: PE executable...\n"));
|
|---|
| 83 | if (options.fPE == FLAGS_PE_PE2LX
|
|---|
| 84 | || (options.fPE == FLAGS_PE_MIXED
|
|---|
| 85 | && !((pNtHdrs->FileHeader.Characteristics & IMAGE_FILE_DLL == 0UL)
|
|---|
| 86 | && pNtHdrs->OptionalHeader.ImageBase >= 0x04000000UL /* 64MB */
|
|---|
| 87 | )
|
|---|
| 88 | )
|
|---|
| 89 | )
|
|---|
| 90 | { /* pe2lx */
|
|---|
| 91 | Pe2Lx * pPe2Lx = new Pe2Lx(*phFile);
|
|---|
| 92 | if (pPe2Lx != NULL)
|
|---|
| 93 | {
|
|---|
| 94 | rc = pPe2Lx->init(pszFilename);
|
|---|
| 95 | if (rc == NO_ERROR)
|
|---|
| 96 | {
|
|---|
| 97 | kprintf(("ldrOpen: Successfully init of Pe2Lx object.\n"));
|
|---|
| 98 | rc = addModule(*phFile, NULL, MOD_TYPE_PE2LX, pPe2Lx);
|
|---|
| 99 | if (rc == NO_ERROR)
|
|---|
| 100 | #pragma info(notrd)
|
|---|
| 101 | SetState(*phFile, HSTATE_OUR);
|
|---|
| 102 | #pragma info(restore)
|
|---|
| 103 | else
|
|---|
| 104 | kprintf(("ldrOpen: Failed to add the module. rc=%d\n"));
|
|---|
| 105 | }
|
|---|
| 106 | else
|
|---|
| 107 | kprintf(("ldrOpen: Failed to init Pe2Lx object. rc=%d\n"));
|
|---|
| 108 | if (rc != NO_ERROR)
|
|---|
| 109 | delete pPe2Lx;
|
|---|
| 110 | }
|
|---|
| 111 | else
|
|---|
| 112 | kprintf(("ldrOpen: Failed to allocate Pe2Lx object.\n"));
|
|---|
| 113 | }
|
|---|
| 114 | else
|
|---|
| 115 | if (options.fPE == FLAGS_PE_PE || options.fPE == FLAGS_PE_MIXED)
|
|---|
| 116 | { /* pe.exe */
|
|---|
| 117 | kprintf(("ldrOpen: pe.exe - opening\n"));
|
|---|
| 118 | ldrClose(*phFile);
|
|---|
| 119 | rc = ldrOpen(phFile, "pe.exe", param3); /* path....! problems! */
|
|---|
| 120 | kprintf(("ldrOpen: pe.exe - open returned with rc = %d\n", rc));
|
|---|
| 121 | return rc;
|
|---|
| 122 | }
|
|---|
| 123 | }
|
|---|
| 124 | rc = NO_ERROR;
|
|---|
| 125 | }
|
|---|
| 126 | else
|
|---|
| 127 | {
|
|---|
| 128 | if (pach[0] == ELFMAG0 && pach[1] == ELFMAG1 && pach[2] == ELFMAG2 && pach[3] == ELFMAG3)
|
|---|
| 129 | {
|
|---|
| 130 | /* ELF signature found */
|
|---|
| 131 | kprintf(("ldrOpen: ELF executable! - not implemented yet!\n"));
|
|---|
| 132 | }
|
|---|
| 133 | else
|
|---|
| 134 | if (*pach == '#' && pach[1] == '!')
|
|---|
| 135 | {
|
|---|
| 136 | /* unix styled script...? Must be more than 64 bytes long? No options. firstline < 64 bytes. */
|
|---|
| 137 | char *pszStart = pach+2;
|
|---|
| 138 | char *pszEnd;
|
|---|
| 139 | kprintf(("ldrOpen: unix script?\n"));
|
|---|
| 140 |
|
|---|
| 141 | achBuffer[sizeof(achBuffer)-1] = '\0'; /* just to make sure we don't read to much... */
|
|---|
| 142 |
|
|---|
| 143 | /* skip blanks */
|
|---|
| 144 | while (*pszStart != '\0' && (*pszStart == ' ' || *pszStart == '\t'))
|
|---|
| 145 | pszStart++;
|
|---|
| 146 | if (*pszStart != '\0' && *pszStart != '\r' && *pszStart != '\n')
|
|---|
| 147 | { /* find end-of-word */
|
|---|
| 148 | pszEnd = pszStart + 1;
|
|---|
| 149 | while (*pszEnd != '\0' && *pszEnd != '\n' && *pszEnd != '\r'
|
|---|
| 150 | && *pszEnd != '\t' && *pszEnd != ' ')
|
|---|
| 151 | pszEnd++;
|
|---|
| 152 | if (*pszEnd != '\0')
|
|---|
| 153 | {
|
|---|
| 154 | *pszEnd = '\0';
|
|---|
| 155 | kprintf(("ldrOpen: unix script - opening %s\n", pszStart));
|
|---|
| 156 | ldrClose(*phFile);
|
|---|
| 157 | rc = ldrOpen(phFile, pszStart, param3);
|
|---|
| 158 | kprintf(("ldrOpen: unix script - open returned with rc = %d\n", rc));
|
|---|
| 159 | }
|
|---|
| 160 | }
|
|---|
| 161 | else
|
|---|
| 162 | kprintf(("ldrOpen: unix script - unexpected end of line/file. (line: %.10s\n", pach));
|
|---|
| 163 | }
|
|---|
| 164 | }
|
|---|
| 165 | }
|
|---|
| 166 | else
|
|---|
| 167 | {
|
|---|
| 168 | kprintf(("ldrOpen: ldrRead failed with rc=%d when reading DosHdr.\n", rc));
|
|---|
| 169 | rc = NO_ERROR;
|
|---|
| 170 | }
|
|---|
| 171 | }
|
|---|
| 172 | return rc;
|
|---|
| 173 | }
|
|---|