| 1 | /* $Id: ldr.cpp,v 1.7 2000-01-22 18:21:01 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 | #if 0 | 
|---|
| 86 | /* Not 100% sure that this will work correctly! */ | 
|---|
| 87 | PMODULE pMod = (PMODULE)AVLGet(&pMTERoot, (AVLKEY)pMTE); | 
|---|
| 88 | if (pMod == NULL) | 
|---|
| 89 | { | 
|---|
| 90 | #ifdef DEBUG | 
|---|
| 91 | if (pMTE <= (PMTE)0x10000) | 
|---|
| 92 | { | 
|---|
| 93 | kprintf(("getModuleByMTE: invalid pMTE pointer - %#8x\n", pMTE)); | 
|---|
| 94 | return NULL; | 
|---|
| 95 | } | 
|---|
| 96 | #endif | 
|---|
| 97 | pMod = (PMODULE)AVLGet(&pSFNRoot, (AVLKEY)pMTE->mte_sfn); | 
|---|
| 98 | if (pMod != NULL) | 
|---|
| 99 | { | 
|---|
| 100 | pMod->coreMTE.Key = (AVLKEY)pMTE; | 
|---|
| 101 | pMod->fFlags |= MOD_FLAGS_IN_MTETREE; | 
|---|
| 102 | AVLInsert(&pMTERoot, (PAVLNODECORE)((unsigned)pMod + offsetof(MODULE, coreMTE))); | 
|---|
| 103 | } | 
|---|
| 104 | } | 
|---|
| 105 | else | 
|---|
| 106 | pMod = (PMODULE)((unsigned)pMod - offsetof(MODULE, coreMTE)); | 
|---|
| 107 | return pMod; | 
|---|
| 108 | #else | 
|---|
| 109 | /* Use this for the time being. */ | 
|---|
| 110 | #ifdef DEBUG | 
|---|
| 111 | if (pMTE <= (PMTE)0x10000) | 
|---|
| 112 | { | 
|---|
| 113 | kprintf(("getModuleByMTE: invalid pMTE pointer - %#8x\n", pMTE)); | 
|---|
| 114 | return NULL; | 
|---|
| 115 | } | 
|---|
| 116 | #endif | 
|---|
| 117 | if (GetState(pMTE->mte_sfn) == HSTATE_OUR) | 
|---|
| 118 | return (PMODULE)AVLGet(&pSFNRoot, (AVLKEY)pMTE->mte_sfn); | 
|---|
| 119 |  | 
|---|
| 120 | return NULL; | 
|---|
| 121 | #endif | 
|---|
| 122 | } | 
|---|
| 123 |  | 
|---|
| 124 |  | 
|---|
| 125 | /** | 
|---|
| 126 | * Get a module by filename. | 
|---|
| 127 | * @returns   Pointer to module node. If not found NULL. | 
|---|
| 128 | * @param     pszFilename  Pointer to the filename which we are search by. | 
|---|
| 129 | * @sketch    Not implemented. | 
|---|
| 130 | * @status    Stub. | 
|---|
| 131 | * @author    knut st. osmundsen | 
|---|
| 132 | */ | 
|---|
| 133 | PMODULE     getModuleByFilename(PCSZ pszFilename) | 
|---|
| 134 | { | 
|---|
| 135 | pszFilename = pszFilename; | 
|---|
| 136 | return NULL; | 
|---|
| 137 | } | 
|---|
| 138 |  | 
|---|
| 139 |  | 
|---|
| 140 | /** | 
|---|
| 141 | * Adds a module to the SFN-tree, if pMTE is not NULL it is added to the MTE-tree too. | 
|---|
| 142 | * @returns   NO_ERROR on success. Appropriate errorcode on failiure. | 
|---|
| 143 | * @param     hFile   System file number for the module. | 
|---|
| 144 | * @param     pMTE    Pointer to MTE. NULL is valid. | 
|---|
| 145 | * @param     fFlags  Type flags for the fFlags field in the node. | 
|---|
| 146 | * @param     pModObj Pointer to module object. | 
|---|
| 147 | * @sketch    DEBUG: check that the module doesn't exists and parameter check. | 
|---|
| 148 | *            (try) Allocate a new node. (return errorcode on failure) | 
|---|
| 149 | *            Fill in the node. | 
|---|
| 150 | *            Add the node to the SFN-tree. | 
|---|
| 151 | *            IF valid MTE pointer THEN add it to the MTE tree and set the in MTE-tree flag. | 
|---|
| 152 | *            return successfully. | 
|---|
| 153 | * @status    completely implemented. | 
|---|
| 154 | * @author    knut st. osmundsen | 
|---|
| 155 | */ | 
|---|
| 156 | ULONG       addModule(SFN hFile, PMTE pMTE, ULONG fFlags, ModuleBase *pModObj) | 
|---|
| 157 | { | 
|---|
| 158 | PMODULE pMod; | 
|---|
| 159 | #ifdef DEBUG | 
|---|
| 160 | if (AVLGet(&pSFNRoot, (AVLKEY)hFile) != NULL) | 
|---|
| 161 | kprintf(("addModule: Module allready present in the SFN-tree!\n")); | 
|---|
| 162 | if (hFile == 0) | 
|---|
| 163 | { | 
|---|
| 164 | kprintf(("addModule: invalid parameter: hFile = 0\n")); | 
|---|
| 165 | return ERROR_INVALID_PARAMETER; | 
|---|
| 166 | } | 
|---|
| 167 | if ((fFlags & MOD_TYPE_MASK) == 0 || (fFlags  & ~MOD_TYPE_MASK) != 0UL) | 
|---|
| 168 | { | 
|---|
| 169 | kprintf(("addModule: invalid parameter: fFlags = 0x%#8x\n", fFlags)); | 
|---|
| 170 | return ERROR_INVALID_PARAMETER; | 
|---|
| 171 | } | 
|---|
| 172 | #endif | 
|---|
| 173 |  | 
|---|
| 174 | /* try allocate memory for the node. */ | 
|---|
| 175 | pMod = (PMODULE)malloc(sizeof(MODULE)); | 
|---|
| 176 | if (pMod == NULL) | 
|---|
| 177 | { | 
|---|
| 178 | kprintf(("addModule: out of memory!\n")); | 
|---|
| 179 | return ERROR_NOT_ENOUGH_MEMORY; | 
|---|
| 180 | } | 
|---|
| 181 |  | 
|---|
| 182 | /* fill in the module node. */ | 
|---|
| 183 | pMod->coreKey.Key = (AVLKEY)hFile; | 
|---|
| 184 | pMod->hFile = hFile; | 
|---|
| 185 | pMod->pMTE = pMTE; | 
|---|
| 186 | pMod->fFlags = fFlags; | 
|---|
| 187 | pMod->Data.pModule = pModObj; | 
|---|
| 188 |  | 
|---|
| 189 | /* insert the module node into the tree(s) */ | 
|---|
| 190 | AVLInsert(&pSFNRoot, (PAVLNODECORE)pMod); | 
|---|
| 191 | if (pMTE != NULL) | 
|---|
| 192 | { | 
|---|
| 193 | pMod->coreMTE.Key = (AVLKEY)pMTE; | 
|---|
| 194 | pMod->fFlags |= MOD_FLAGS_IN_MTETREE; | 
|---|
| 195 | AVLInsert(&pMTERoot, (PAVLNODECORE)((unsigned)pMod + offsetof(MODULE, coreMTE))); | 
|---|
| 196 | } | 
|---|
| 197 |  | 
|---|
| 198 | return NO_ERROR; | 
|---|
| 199 | } | 
|---|
| 200 |  | 
|---|
| 201 |  | 
|---|
| 202 | /** | 
|---|
| 203 | * Removes and frees a module node (including the data pointer). | 
|---|
| 204 | * @returns   NO_ERROR on success. Appropriate error code on failure. | 
|---|
| 205 | * @param     hFile  System filehandle of the module. | 
|---|
| 206 | * @sketch    Remove the node from the SFN-tree. | 
|---|
| 207 | *            IF present in the MTE-tree THEN remove it from the tree. | 
|---|
| 208 | *            Delete the datapointer. | 
|---|
| 209 | *            Free the module node. | 
|---|
| 210 | *            return successfully. | 
|---|
| 211 | * @status    completely implemented. | 
|---|
| 212 | * @author    knut st. osmundsen | 
|---|
| 213 | */ | 
|---|
| 214 | ULONG      removeModule(SFN hFile) | 
|---|
| 215 | { | 
|---|
| 216 | PMODULE pMod = (PMODULE)AVLRemove(&pSFNRoot, (AVLKEY)hFile); | 
|---|
| 217 | if (pMod == NULL) | 
|---|
| 218 | { | 
|---|
| 219 | kprintf(("removeModule: Module not found! hFile=%#4x\n", hFile)); | 
|---|
| 220 | return ERROR_INVALID_PARAMETER; | 
|---|
| 221 | } | 
|---|
| 222 |  | 
|---|
| 223 | /* In MTE-tree too? */ | 
|---|
| 224 | if (pMod->fFlags & MOD_FLAGS_IN_MTETREE) | 
|---|
| 225 | { | 
|---|
| 226 | if (AVLRemove(&pMTERoot, (AVLKEY)pMod->pMTE) == NULL) | 
|---|
| 227 | { | 
|---|
| 228 | kprintf(("removeModule: MOD_FLAGS_IN_MTETREE set but AVLRemove returns NULL\n")); | 
|---|
| 229 | } | 
|---|
| 230 | } | 
|---|
| 231 |  | 
|---|
| 232 | /* Delete the datapointer. */ | 
|---|
| 233 | switch (pMod->fFlags & MOD_TYPE_MASK) | 
|---|
| 234 | { | 
|---|
| 235 | case MOD_TYPE_PE2LX: | 
|---|
| 236 | delete pMod->Data.pPe2Lx; | 
|---|
| 237 | break; | 
|---|
| 238 |  | 
|---|
| 239 | case MOD_TYPE_ELF2LX: | 
|---|
| 240 | case MOD_TYPE_SCRIPT: | 
|---|
| 241 | case MOD_TYPE_PE: | 
|---|
| 242 | default: | 
|---|
| 243 | kprintf(("removeModule: Unknown type, %#x\n", pMod->fFlags & MOD_TYPE_MASK)); | 
|---|
| 244 | } | 
|---|
| 245 |  | 
|---|
| 246 | /* Free the module node. */ | 
|---|
| 247 | free(pMod); | 
|---|
| 248 |  | 
|---|
| 249 | return NO_ERROR; | 
|---|
| 250 | } | 
|---|
| 251 |  | 
|---|
| 252 |  | 
|---|
| 253 | /** | 
|---|
| 254 | * Initiate the loader "sub-system". | 
|---|
| 255 | * @returns   NO_ERROR on success. !0 on error. | 
|---|
| 256 | */ | 
|---|
| 257 | ULONG ldrInit(void) | 
|---|
| 258 | { | 
|---|
| 259 | int rc = NO_ERROR; | 
|---|
| 260 |  | 
|---|
| 261 | /* init state table */ | 
|---|
| 262 | memset(&achHandleStates[0], 0, sizeof(achHandleStates)); | 
|---|
| 263 |  | 
|---|
| 264 | /* init the tree roots */ | 
|---|
| 265 | pSFNRoot = NULL; | 
|---|
| 266 | pMTERoot = NULL; | 
|---|
| 267 |  | 
|---|
| 268 | /* ModuleBase logging. */ | 
|---|
| 269 | ModuleBase::ulInfoLevel = options.ulInfoLevel; | 
|---|
| 270 |  | 
|---|
| 271 | return rc; | 
|---|
| 272 | } | 
|---|
| 273 |  | 
|---|