| 1 | /* $Id: ldr.cpp,v 1.6 1999-11-10 01:45:36 bird Exp $
|
|---|
| 2 | *
|
|---|
| 3 | * ldr.cpp - Loader helpers.
|
|---|
| 4 | *
|
|---|
| 5 | * Copyright (c) 1999 knut St. osmundsen
|
|---|
| 6 | *
|
|---|
| 7 | * Project Odin Software License can be found in LICENSE.TXT
|
|---|
| 8 | *
|
|---|
| 9 | */
|
|---|
| 10 |
|
|---|
| 11 | /*******************************************************************************
|
|---|
| 12 | * Defined Constants And Macros *
|
|---|
| 13 | *******************************************************************************/
|
|---|
| 14 | #define INCL_DOSERRORS
|
|---|
| 15 | #define INCL_NOPMAPI
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 | /*******************************************************************************
|
|---|
| 19 | * Header Files *
|
|---|
| 20 | *******************************************************************************/
|
|---|
| 21 | #include <os2.h>
|
|---|
| 22 |
|
|---|
| 23 | #include "malloc.h"
|
|---|
| 24 | #include "new.h"
|
|---|
| 25 | #include <memory.h>
|
|---|
| 26 | #include <stdlib.h>
|
|---|
| 27 | #include <stddef.h>
|
|---|
| 28 |
|
|---|
| 29 | #include "log.h"
|
|---|
| 30 | #include <peexe.h>
|
|---|
| 31 | #include <exe386.h>
|
|---|
| 32 | #include "OS2Krnl.h"
|
|---|
| 33 | #include "ModuleBase.h"
|
|---|
| 34 | #include "pe2lx.h"
|
|---|
| 35 | #include "avl.h"
|
|---|
| 36 | #include "ldr.h"
|
|---|
| 37 | #include "options.h"
|
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 | /*******************************************************************************
|
|---|
| 41 | * Global Variables *
|
|---|
| 42 | *******************************************************************************/
|
|---|
| 43 | PAVLNODECORE pSFNRoot = NULL;
|
|---|
| 44 | PAVLNODECORE pMTERoot = NULL;
|
|---|
| 45 |
|
|---|
| 46 | unsigned char achHandleStates[MAX_FILE_HANDLES/8];
|
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 | /**
|
|---|
| 50 | * Gets a module by the give hFile.
|
|---|
| 51 | * @returns Pointer to module node. If not found NULL.
|
|---|
| 52 | * @param hFile File handle of the module to be found.
|
|---|
| 53 | * @sketch return a AVLGet on the pSFNRoot-tree.
|
|---|
| 54 | * @status completely implemented.
|
|---|
| 55 | * @author knut st. osmundsen
|
|---|
| 56 | */
|
|---|
| 57 | PMODULE getModuleBySFN(SFN hFile)
|
|---|
| 58 | {
|
|---|
| 59 | return (PMODULE)AVLGet(&pSFNRoot, (AVLKEY)hFile);
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 |
|
|---|
| 63 | /**
|
|---|
| 64 | * Gets a module by the MTE.
|
|---|
| 65 | * @returns Pointer to module node. If not found NULL.
|
|---|
| 66 | * @param pMTE Pointer an Module Table Entry.
|
|---|
| 67 | * @sketch Try find it in the MTE tree.
|
|---|
| 68 | * IF not found THEN
|
|---|
| 69 | * BEGIN
|
|---|
| 70 | * DEBUG: validate pMTE pointer.
|
|---|
| 71 | * Get the SFN from the MTE.
|
|---|
| 72 | * IF found in the SFN-tree THEN
|
|---|
| 73 | * BEGIN
|
|---|
| 74 | * Update the pMTE in the node.
|
|---|
| 75 | * Add the node to the MTE-tree.
|
|---|
| 76 | * END
|
|---|
| 77 | * ELSE return NULL
|
|---|
| 78 | * END
|
|---|
| 79 | * return pointer to module node.
|
|---|
| 80 | * @status completely implemented.
|
|---|
| 81 | * @author knut st. osmundsen
|
|---|
| 82 | */
|
|---|
| 83 | PMODULE getModuleByMTE(PMTE pMTE)
|
|---|
| 84 | {
|
|---|
| 85 | PMODULE pMod = (PMODULE)AVLGet(&pMTERoot, (AVLKEY)pMTE);
|
|---|
| 86 | if (pMod == NULL)
|
|---|
| 87 | {
|
|---|
| 88 | #ifdef DEBUG
|
|---|
| 89 | if (pMTE <= (PMTE)0x10000)
|
|---|
| 90 | {
|
|---|
| 91 | kprintf(("getModuleByMTE: invalid pMTE pointer - %#8x\n", pMTE));
|
|---|
| 92 | return NULL;
|
|---|
| 93 | }
|
|---|
| 94 | #endif
|
|---|
| 95 | pMod = (PMODULE)AVLGet(&pSFNRoot, (AVLKEY)pMTE->mte_sfn);
|
|---|
| 96 | if (pMod != NULL)
|
|---|
| 97 | {
|
|---|
| 98 | pMod->coreMTE.Key = (AVLKEY)pMTE;
|
|---|
| 99 | pMod->fFlags |= MOD_FLAGS_IN_MTETREE;
|
|---|
| 100 | AVLInsert(&pMTERoot, (PAVLNODECORE)((unsigned)pMod + offsetof(MODULE, coreMTE)));
|
|---|
| 101 | }
|
|---|
| 102 | }
|
|---|
| 103 | else
|
|---|
| 104 | pMod = (PMODULE)((unsigned)pMod - offsetof(MODULE, coreMTE));
|
|---|
| 105 | return pMod;
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 |
|
|---|
| 109 | /**
|
|---|
| 110 | * Get a module by filename.
|
|---|
| 111 | * @returns Pointer to module node. If not found NULL.
|
|---|
| 112 | * @param pszFilename Pointer to the filename which we are search by.
|
|---|
| 113 | * @sketch Not implemented.
|
|---|
| 114 | * @status Stub.
|
|---|
| 115 | * @author knut st. osmundsen
|
|---|
| 116 | */
|
|---|
| 117 | PMODULE getModuleByFilename(PCSZ pszFilename)
|
|---|
| 118 | {
|
|---|
| 119 | pszFilename = pszFilename;
|
|---|
| 120 | return NULL;
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 |
|
|---|
| 124 | /**
|
|---|
| 125 | * Adds a module to the SFN-tree, if pMTE is not NULL it is added to the MTE-tree too.
|
|---|
| 126 | * @returns NO_ERROR on success. Appropriate errorcode on failiure.
|
|---|
| 127 | * @param hFile System file number for the module.
|
|---|
| 128 | * @param pMTE Pointer to MTE. NULL is valid.
|
|---|
| 129 | * @param fFlags Type flags for the fFlags field in the node.
|
|---|
| 130 | * @param pModObj Pointer to module object.
|
|---|
| 131 | * @sketch DEBUG: check that the module doesn't exists and parameter check.
|
|---|
| 132 | * (try) Allocate a new node. (return errorcode on failure)
|
|---|
| 133 | * Fill in the node.
|
|---|
| 134 | * Add the node to the SFN-tree.
|
|---|
| 135 | * IF valid MTE pointer THEN add it to the MTE tree and set the in MTE-tree flag.
|
|---|
| 136 | * return successfully.
|
|---|
| 137 | * @status completely implemented.
|
|---|
| 138 | * @author knut st. osmundsen
|
|---|
| 139 | */
|
|---|
| 140 | ULONG addModule(SFN hFile, PMTE pMTE, ULONG fFlags, ModuleBase *pModObj)
|
|---|
| 141 | {
|
|---|
| 142 | PMODULE pMod;
|
|---|
| 143 | #ifdef DEBUG
|
|---|
| 144 | if (AVLGet(&pSFNRoot, (AVLKEY)hFile) != NULL)
|
|---|
| 145 | kprintf(("addModule: Module allready present in the SFN-tree!\n"));
|
|---|
| 146 | if (hFile == 0)
|
|---|
| 147 | {
|
|---|
| 148 | kprintf(("addModule: invalid parameter: hFile = 0\n"));
|
|---|
| 149 | return ERROR_INVALID_PARAMETER;
|
|---|
| 150 | }
|
|---|
| 151 | if ((fFlags & MOD_TYPE_MASK) == 0 || (fFlags & ~MOD_TYPE_MASK) != 0)
|
|---|
| 152 | {
|
|---|
| 153 | kprintf(("addModule: invalid parameter: fFlags = 0x%#8x\n", fFlags));
|
|---|
| 154 | return ERROR_INVALID_PARAMETER;
|
|---|
| 155 | }
|
|---|
| 156 | #endif
|
|---|
| 157 |
|
|---|
| 158 | /* try allocate memory for the node. */
|
|---|
| 159 | pMod = (PMODULE)malloc(sizeof(MODULE));
|
|---|
| 160 | if (pMod == NULL)
|
|---|
| 161 | {
|
|---|
| 162 | kprintf(("addModule: out of memory!\n"));
|
|---|
| 163 | return ERROR_NOT_ENOUGH_MEMORY;
|
|---|
| 164 | }
|
|---|
| 165 |
|
|---|
| 166 | /* fill in the module node. */
|
|---|
| 167 | pMod->coreKey.Key = (AVLKEY)hFile;
|
|---|
| 168 | pMod->hFile = hFile;
|
|---|
| 169 | pMod->pMTE = pMTE;
|
|---|
| 170 | pMod->fFlags = fFlags;
|
|---|
| 171 | pMod->Data.pModule = pModObj;
|
|---|
| 172 |
|
|---|
| 173 | /* insert the module node into the tree(s) */
|
|---|
| 174 | AVLInsert(&pSFNRoot, (PAVLNODECORE)pMod);
|
|---|
| 175 | if (pMTE != NULL)
|
|---|
| 176 | {
|
|---|
| 177 | pMod->coreMTE.Key = (AVLKEY)pMTE;
|
|---|
| 178 | pMod->fFlags |= MOD_FLAGS_IN_MTETREE;
|
|---|
| 179 | AVLInsert(&pMTERoot, (PAVLNODECORE)((unsigned)pMod + offsetof(MODULE, coreMTE)));
|
|---|
| 180 | }
|
|---|
| 181 |
|
|---|
| 182 | return NO_ERROR;
|
|---|
| 183 | }
|
|---|
| 184 |
|
|---|
| 185 |
|
|---|
| 186 | /**
|
|---|
| 187 | * Removes and frees a module node (including the data pointer).
|
|---|
| 188 | * @returns NO_ERROR on success. Appropriate error code on failure.
|
|---|
| 189 | * @param hFile System filehandle of the module.
|
|---|
| 190 | * @sketch Remove the node from the SFN-tree.
|
|---|
| 191 | * IF present in the MTE-tree THEN remove it from the tree.
|
|---|
| 192 | * Delete the datapointer.
|
|---|
| 193 | * Free the module node.
|
|---|
| 194 | * return successfully.
|
|---|
| 195 | * @status completely implemented.
|
|---|
| 196 | * @author knut st. osmundsen
|
|---|
| 197 | */
|
|---|
| 198 | ULONG removeModule(SFN hFile)
|
|---|
| 199 | {
|
|---|
| 200 | PMODULE pMod = (PMODULE)AVLRemove(&pSFNRoot, (AVLKEY)hFile);
|
|---|
| 201 | if (pMod == NULL)
|
|---|
| 202 | {
|
|---|
| 203 | kprintf(("removeModule: Module not found! hFile=%#4x\n", hFile));
|
|---|
| 204 | return ERROR_INVALID_PARAMETER;
|
|---|
| 205 | }
|
|---|
| 206 |
|
|---|
| 207 | /* In MTE-tree too? */
|
|---|
| 208 | if (pMod->fFlags & MOD_FLAGS_IN_MTETREE)
|
|---|
| 209 | {
|
|---|
| 210 | if (AVLRemove(&pMTERoot, (AVLKEY)pMod->pMTE) == NULL)
|
|---|
| 211 | {
|
|---|
| 212 | kprintf(("removeModule: MOD_FLAGS_IN_MTETREE set but AVLRemove returns NULL\n"));
|
|---|
| 213 | }
|
|---|
| 214 | }
|
|---|
| 215 |
|
|---|
| 216 | /* Delete the datapointer. */
|
|---|
| 217 | switch (pMod->fFlags & MOD_TYPE_MASK)
|
|---|
| 218 | {
|
|---|
| 219 | case MOD_TYPE_PE2LX:
|
|---|
| 220 | delete pMod->Data.pPe2Lx;
|
|---|
| 221 | break;
|
|---|
| 222 |
|
|---|
| 223 | case MOD_TYPE_ELF2LX:
|
|---|
| 224 | case MOD_TYPE_SCRIPT:
|
|---|
| 225 | case MOD_TYPE_PE:
|
|---|
| 226 | default:
|
|---|
| 227 | kprintf(("removeModule: Unknown type, %#x\n", pMod->fFlags & MOD_TYPE_MASK));
|
|---|
| 228 | }
|
|---|
| 229 |
|
|---|
| 230 | /* Free the module node. */
|
|---|
| 231 | free(pMod);
|
|---|
| 232 |
|
|---|
| 233 | return NO_ERROR;
|
|---|
| 234 | }
|
|---|
| 235 |
|
|---|
| 236 |
|
|---|
| 237 | /**
|
|---|
| 238 | * Initiate the loader "sub-system".
|
|---|
| 239 | * @returns NO_ERROR on success. !0 on error.
|
|---|
| 240 | */
|
|---|
| 241 | ULONG ldrInit(void)
|
|---|
| 242 | {
|
|---|
| 243 | int rc = NO_ERROR;
|
|---|
| 244 | int i;
|
|---|
| 245 |
|
|---|
| 246 | /* init state table */
|
|---|
| 247 | memset(&achHandleStates[0], 0, sizeof(achHandleStates));
|
|---|
| 248 |
|
|---|
| 249 | /* init the tree roots */
|
|---|
| 250 | pSFNRoot = NULL;
|
|---|
| 251 | pMTERoot = NULL;
|
|---|
| 252 |
|
|---|
| 253 | /* ModuleBase logging. */
|
|---|
| 254 | ModuleBase::ulInfoLevel = options.ulInfoLevel;
|
|---|
| 255 |
|
|---|
| 256 | return rc;
|
|---|
| 257 | }
|
|---|
| 258 |
|
|---|