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