1 | /* $Id: kLdrModPE.c 58 2013-10-12 20:18:21Z bird $ */
|
---|
2 | /** @file
|
---|
3 | * kLdr - The Module Interpreter for the Portable Executable (PE) Format.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (c) 2006-2007 Knut St. Osmundsen <bird-kStuff-spamix@anduin.net>
|
---|
8 | *
|
---|
9 | * Permission is hereby granted, free of charge, to any person
|
---|
10 | * obtaining a copy of this software and associated documentation
|
---|
11 | * files (the "Software"), to deal in the Software without
|
---|
12 | * restriction, including without limitation the rights to use,
|
---|
13 | * copy, modify, merge, publish, distribute, sublicense, and/or sell
|
---|
14 | * copies of the Software, and to permit persons to whom the
|
---|
15 | * Software is furnished to do so, subject to the following
|
---|
16 | * conditions:
|
---|
17 | *
|
---|
18 | * The above copyright notice and this permission notice shall be
|
---|
19 | * included in all copies or substantial portions of the Software.
|
---|
20 | *
|
---|
21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
---|
22 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
---|
23 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
---|
24 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
---|
25 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
---|
26 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
---|
27 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
---|
28 | * OTHER DEALINGS IN THE SOFTWARE.
|
---|
29 | */
|
---|
30 |
|
---|
31 | /*******************************************************************************
|
---|
32 | * Header Files *
|
---|
33 | *******************************************************************************/
|
---|
34 | #include <k/kLdr.h>
|
---|
35 | #include "kLdrInternal.h"
|
---|
36 | #include <k/kLdrFmts/pe.h>
|
---|
37 |
|
---|
38 |
|
---|
39 | /*******************************************************************************
|
---|
40 | * Defined Constants And Macros *
|
---|
41 | *******************************************************************************/
|
---|
42 | /** @def KLDRMODPE_STRICT
|
---|
43 | * Define KLDRMODPE_STRICT to enabled strict checks in KLDRMODPE. */
|
---|
44 | #define KLDRMODPE_STRICT 1
|
---|
45 |
|
---|
46 | /** @def KLDRMODPE_ASSERT
|
---|
47 | * Assert that an expression is true when KLDR_STRICT is defined.
|
---|
48 | */
|
---|
49 | #ifdef KLDRMODPE_STRICT
|
---|
50 | # define KLDRMODPE_ASSERT(expr) kHlpAssert(expr)
|
---|
51 | #else
|
---|
52 | # define KLDRMODPE_ASSERT(expr) do {} while (0)
|
---|
53 | #endif
|
---|
54 |
|
---|
55 | /** @def KLDRMODPE_RVA2TYPE
|
---|
56 | * Converts a RVA to a pointer of the specified type.
|
---|
57 | * @param pvBits The bits (image base).
|
---|
58 | * @param uRVA The image relative virtual address.
|
---|
59 | * @param type The type to cast to.
|
---|
60 | */
|
---|
61 | #define KLDRMODPE_RVA2TYPE(pvBits, uRVA, type) \
|
---|
62 | ( (type) ((KUPTR)(pvBits) + (KUPTR)(uRVA)) )
|
---|
63 |
|
---|
64 | /** @def KLDRMODPE_VALID_RVA
|
---|
65 | * Checks that the specified RVA value is non-zero and within the bounds of the image.
|
---|
66 | * @returns true/false.
|
---|
67 | * @param pModPE The PE module interpreter instance.
|
---|
68 | * @param uRVA The RVA to validate.
|
---|
69 | */
|
---|
70 | #define KLDRMODPE_VALID_RVA(pModPE, uRVA) \
|
---|
71 | ( (uRVA) && (uRVA) < (pModPE)->Hdrs.OptionalHeader.SizeOfImage )
|
---|
72 |
|
---|
73 |
|
---|
74 |
|
---|
75 | /*******************************************************************************
|
---|
76 | * Structures and Typedefs *
|
---|
77 | *******************************************************************************/
|
---|
78 | /**
|
---|
79 | * Instance data for the PE module interpreter.
|
---|
80 | */
|
---|
81 | typedef struct KLDRMODPE
|
---|
82 | {
|
---|
83 | /** Pointer to the module. (Follows the section table.) */
|
---|
84 | PKLDRMOD pMod;
|
---|
85 | /** Pointer to the RDR mapping of the raw file bits. NULL if not mapped. */
|
---|
86 | const void *pvBits;
|
---|
87 | /** Pointer to the user mapping. */
|
---|
88 | const void *pvMapping;
|
---|
89 | /** Reserved flags. */
|
---|
90 | KU32 f32Reserved;
|
---|
91 | /** The number of imported modules.
|
---|
92 | * If ~(KU32)0 this hasn't been determined yet. */
|
---|
93 | KU32 cImportModules;
|
---|
94 | /** The offset of the NT headers. */
|
---|
95 | KLDRFOFF offHdrs;
|
---|
96 | /** Copy of the NT headers. */
|
---|
97 | IMAGE_NT_HEADERS64 Hdrs;
|
---|
98 | /** The section header table . */
|
---|
99 | IMAGE_SECTION_HEADER aShdrs[1];
|
---|
100 | } KLDRMODPE, *PKLDRMODPE;
|
---|
101 |
|
---|
102 |
|
---|
103 | /*******************************************************************************
|
---|
104 | * Internal Functions *
|
---|
105 | *******************************************************************************/
|
---|
106 | static KI32 kldrModPENumberOfImports(PKLDRMOD pMod, const void *pvBits);
|
---|
107 | static int kldrModPERelocateBits(PKLDRMOD pMod, void *pvBits, KLDRADDR NewBaseAddress, KLDRADDR OldBaseAddress,
|
---|
108 | PFNKLDRMODGETIMPORT pfnGetImport, void *pvUser);
|
---|
109 |
|
---|
110 | static int kldrModPEDoCreate(PKRDR pRdr, KLDRFOFF offNewHdr, PKLDRMODPE *ppMod);
|
---|
111 | /*static void kldrModPEDoLoadConfigConversion(PIMAGE_LOAD_CONFIG_DIRECTORY64 pLoadCfg); */
|
---|
112 | static int kLdrModPEDoOptionalHeaderValidation(PKLDRMODPE pModPE);
|
---|
113 | static int kLdrModPEDoSectionHeadersValidation(PKLDRMODPE pModPE);
|
---|
114 | static void kldrModPEDoOptionalHeaderConversion(PIMAGE_OPTIONAL_HEADER64 pOptionalHeader);
|
---|
115 | static int kldrModPEDoForwarderQuery(PKLDRMODPE pModPE, const void *pvBits, const char *pszForwarder,
|
---|
116 | PFNKLDRMODGETIMPORT pfnGetImport, void *pvUser, PKLDRADDR puValue, KU32 *pfKind);
|
---|
117 | static int kldrModPEDoFixups(PKLDRMODPE pModPE, void *pvMapping, KLDRADDR NewBaseAddress, KLDRADDR OldBaseAddress);
|
---|
118 | static int kldrModPEDoImports32Bit(PKLDRMODPE pModPE, void *pvMapping, const IMAGE_IMPORT_DESCRIPTOR *pImpDesc,
|
---|
119 | PFNKLDRMODGETIMPORT pfnGetImport, void *pvUser);
|
---|
120 | static int kldrModPEDoImports64Bit(PKLDRMODPE pModPE, void *pvMapping, const IMAGE_IMPORT_DESCRIPTOR *pImpDesc,
|
---|
121 | PFNKLDRMODGETIMPORT pfnGetImport, void *pvUser);
|
---|
122 | static int kldrModPEDoImports(PKLDRMODPE pModPE, void *pvMapping, PFNKLDRMODGETIMPORT pfnGetImport, void *pvUser);
|
---|
123 | static int kldrModPEDoCallDLL(PKLDRMODPE pModPE, unsigned uOp, KUPTR uHandle);
|
---|
124 | static int kldrModPEDoCallTLS(PKLDRMODPE pModPE, unsigned uOp, KUPTR uHandle);
|
---|
125 | static KI32 kldrModPEDoCall(KUPTR uEntrypoint, KUPTR uHandle, KU32 uOp, void *pvReserved);
|
---|
126 |
|
---|
127 |
|
---|
128 | /**
|
---|
129 | * Create a loader module instance interpreting the executable image found
|
---|
130 | * in the specified file provider instance.
|
---|
131 | *
|
---|
132 | * @returns 0 on success and *ppMod pointing to a module instance.
|
---|
133 | * On failure, a non-zero OS specific error code is returned.
|
---|
134 | * @param pOps Pointer to the registered method table.
|
---|
135 | * @param pRdr The file provider instance to use.
|
---|
136 | * @param fFlags Flags, MBZ.
|
---|
137 | * @param enmCpuArch The desired CPU architecture. KCPUARCH_UNKNOWN means
|
---|
138 | * anything goes, but with a preference for the current
|
---|
139 | * host architecture.
|
---|
140 | * @param offNewHdr The offset of the new header in MZ files. -1 if not found.
|
---|
141 | * @param ppMod Where to store the module instance pointer.
|
---|
142 | */
|
---|
143 | static int kldrModPECreate(PCKLDRMODOPS pOps, PKRDR pRdr, KU32 fFlags, KCPUARCH enmCpuArch, KLDRFOFF offNewHdr, PPKLDRMOD ppMod)
|
---|
144 | {
|
---|
145 | PKLDRMODPE pModPE;
|
---|
146 | int rc;
|
---|
147 |
|
---|
148 | /*
|
---|
149 | * Create the instance data and do a minimal header validation.
|
---|
150 | */
|
---|
151 | rc = kldrModPEDoCreate(pRdr, offNewHdr, &pModPE);
|
---|
152 | if (!rc)
|
---|
153 | {
|
---|
154 | /*
|
---|
155 | * Match up against the requested CPU architecture.
|
---|
156 | */
|
---|
157 | if ( enmCpuArch == KCPUARCH_UNKNOWN
|
---|
158 | || pModPE->pMod->enmArch == enmCpuArch)
|
---|
159 | {
|
---|
160 | pModPE->pMod->pOps = pOps;
|
---|
161 | pModPE->pMod->u32Magic = KLDRMOD_MAGIC;
|
---|
162 | *ppMod = pModPE->pMod;
|
---|
163 | return 0;
|
---|
164 | }
|
---|
165 | rc = KLDR_ERR_CPU_ARCH_MISMATCH;
|
---|
166 | }
|
---|
167 | kHlpFree(pModPE);
|
---|
168 | return rc;
|
---|
169 | }
|
---|
170 |
|
---|
171 |
|
---|
172 | /**
|
---|
173 | * Separate function for reading creating the PE module instance to
|
---|
174 | * simplify cleanup on failure.
|
---|
175 | */
|
---|
176 | static int kldrModPEDoCreate(PKRDR pRdr, KLDRFOFF offNewHdr, PKLDRMODPE *ppModPE)
|
---|
177 | {
|
---|
178 | struct
|
---|
179 | {
|
---|
180 | KU32 Signature;
|
---|
181 | IMAGE_FILE_HEADER FileHdr;
|
---|
182 | } s;
|
---|
183 | PKLDRMODPE pModPE;
|
---|
184 | PKLDRMOD pMod;
|
---|
185 | KSIZE cb;
|
---|
186 | KSIZE cchFilename;
|
---|
187 | KLDRFOFF off;
|
---|
188 | KU32 i;
|
---|
189 | int rc;
|
---|
190 | *ppModPE = NULL;
|
---|
191 |
|
---|
192 | /*
|
---|
193 | * Read the signature and file header.
|
---|
194 | */
|
---|
195 | rc = kRdrRead(pRdr, &s, sizeof(s), offNewHdr > 0 ? offNewHdr : 0);
|
---|
196 | if (rc)
|
---|
197 | return rc;
|
---|
198 | if (s.Signature != IMAGE_NT_SIGNATURE)
|
---|
199 | return KLDR_ERR_UNKNOWN_FORMAT;
|
---|
200 |
|
---|
201 | /* sanity checks. */
|
---|
202 | if ( s.FileHdr.NumberOfSections > 4096
|
---|
203 | || ( s.FileHdr.SizeOfOptionalHeader != sizeof(IMAGE_OPTIONAL_HEADER32)
|
---|
204 | && s.FileHdr.SizeOfOptionalHeader != sizeof(IMAGE_OPTIONAL_HEADER64))
|
---|
205 | || !(s.FileHdr.Characteristics & IMAGE_FILE_EXECUTABLE_IMAGE)
|
---|
206 | )
|
---|
207 | return KLDR_ERR_PE_BAD_FILE_HEADER;
|
---|
208 | if ( s.FileHdr.Machine != IMAGE_FILE_MACHINE_I386
|
---|
209 | && s.FileHdr.Machine != IMAGE_FILE_MACHINE_AMD64
|
---|
210 | )
|
---|
211 | return KLDR_ERR_PE_UNSUPPORTED_MACHINE;
|
---|
212 |
|
---|
213 | /*
|
---|
214 | * Calc the instance size, allocate and initialize it.
|
---|
215 | */
|
---|
216 | cchFilename = kHlpStrLen(kRdrName(pRdr));
|
---|
217 | cb = K_ALIGN_Z(K_OFFSETOF(KLDRMODPE, aShdrs[s.FileHdr.NumberOfSections]), 16)
|
---|
218 | + K_OFFSETOF(KLDRMOD, aSegments[s.FileHdr.NumberOfSections + 1])
|
---|
219 | + cchFilename + 1;
|
---|
220 | pModPE = (PKLDRMODPE)kHlpAlloc(cb);
|
---|
221 | if (!pModPE)
|
---|
222 | return KERR_NO_MEMORY;
|
---|
223 | *ppModPE = pModPE;
|
---|
224 |
|
---|
225 | /* KLDRMOD */
|
---|
226 | pMod = (PKLDRMOD)((KU8 *)pModPE + K_ALIGN_Z(K_OFFSETOF(KLDRMODPE, aShdrs[s.FileHdr.NumberOfSections]), 16));
|
---|
227 | pMod->pvData = pModPE;
|
---|
228 | pMod->pRdr = pRdr;
|
---|
229 | pMod->pOps = NULL; /* set upon success. */
|
---|
230 | pMod->cSegments = s.FileHdr.NumberOfSections + 1;
|
---|
231 | pMod->cchFilename = cchFilename;
|
---|
232 | pMod->pszFilename = (char *)&pMod->aSegments[pMod->cSegments];
|
---|
233 | kHlpMemCopy((char *)pMod->pszFilename, kRdrName(pRdr), cchFilename + 1);
|
---|
234 | pMod->pszName = kHlpGetFilename(pMod->pszFilename);
|
---|
235 | pMod->cchName = cchFilename - (pMod->pszName - pMod->pszFilename);
|
---|
236 | pMod->fFlags = 0;
|
---|
237 | switch (s.FileHdr.Machine)
|
---|
238 | {
|
---|
239 | case IMAGE_FILE_MACHINE_I386:
|
---|
240 | pMod->enmCpu = KCPU_I386;
|
---|
241 | pMod->enmArch = KCPUARCH_X86_32;
|
---|
242 | pMod->enmEndian = KLDRENDIAN_LITTLE;
|
---|
243 | break;
|
---|
244 |
|
---|
245 | case IMAGE_FILE_MACHINE_AMD64:
|
---|
246 | pMod->enmCpu = KCPU_K8;
|
---|
247 | pMod->enmArch = KCPUARCH_AMD64;
|
---|
248 | pMod->enmEndian = KLDRENDIAN_LITTLE;
|
---|
249 | break;
|
---|
250 | default:
|
---|
251 | kHlpAssert(0);
|
---|
252 | break;
|
---|
253 | }
|
---|
254 | pMod->enmFmt = KLDRFMT_PE;
|
---|
255 | if (s.FileHdr.Characteristics & IMAGE_FILE_DLL)
|
---|
256 | pMod->enmType = !(s.FileHdr.Characteristics & IMAGE_FILE_RELOCS_STRIPPED)
|
---|
257 | ? KLDRTYPE_SHARED_LIBRARY_RELOCATABLE
|
---|
258 | : KLDRTYPE_SHARED_LIBRARY_FIXED;
|
---|
259 | else
|
---|
260 | pMod->enmType = !(s.FileHdr.Characteristics & IMAGE_FILE_RELOCS_STRIPPED)
|
---|
261 | ? KLDRTYPE_EXECUTABLE_RELOCATABLE
|
---|
262 | : KLDRTYPE_EXECUTABLE_FIXED;
|
---|
263 | pMod->u32Magic = 0; /* set upon success. */
|
---|
264 |
|
---|
265 | /* KLDRMODPE */
|
---|
266 | pModPE->pMod = pMod;
|
---|
267 | pModPE->pvBits = NULL;
|
---|
268 | pModPE->pvMapping = NULL;
|
---|
269 | pModPE->f32Reserved = 0;
|
---|
270 | pModPE->cImportModules = ~(KU32)0;
|
---|
271 | pModPE->offHdrs = offNewHdr >= 0 ? offNewHdr : 0;
|
---|
272 | pModPE->Hdrs.Signature = s.Signature;
|
---|
273 | pModPE->Hdrs.FileHeader = s.FileHdr;
|
---|
274 |
|
---|
275 | /*
|
---|
276 | * Read the optional header and the section table.
|
---|
277 | */
|
---|
278 | off = pModPE->offHdrs + sizeof(pModPE->Hdrs.Signature) + sizeof(pModPE->Hdrs.FileHeader);
|
---|
279 | rc = kRdrRead(pRdr, &pModPE->Hdrs.OptionalHeader, pModPE->Hdrs.FileHeader.SizeOfOptionalHeader, off);
|
---|
280 | if (rc)
|
---|
281 | return rc;
|
---|
282 | if (pModPE->Hdrs.FileHeader.SizeOfOptionalHeader != sizeof(pModPE->Hdrs.OptionalHeader))
|
---|
283 | kldrModPEDoOptionalHeaderConversion(&pModPE->Hdrs.OptionalHeader);
|
---|
284 | off += pModPE->Hdrs.FileHeader.SizeOfOptionalHeader;
|
---|
285 | rc = kRdrRead(pRdr, &pModPE->aShdrs[0], sizeof(IMAGE_SECTION_HEADER) * pModPE->Hdrs.FileHeader.NumberOfSections, off);
|
---|
286 | if (rc)
|
---|
287 | return rc;
|
---|
288 |
|
---|
289 | /*
|
---|
290 | * Validate the two.
|
---|
291 | */
|
---|
292 | rc = kLdrModPEDoOptionalHeaderValidation(pModPE);
|
---|
293 | if (rc)
|
---|
294 | return rc;
|
---|
295 | for (i = 0; i < pModPE->Hdrs.FileHeader.NumberOfSections; i++)
|
---|
296 | {
|
---|
297 | rc = kLdrModPEDoSectionHeadersValidation(pModPE);
|
---|
298 | if (rc)
|
---|
299 | return rc;
|
---|
300 | }
|
---|
301 |
|
---|
302 | /*
|
---|
303 | * Setup the KLDRMOD segment array.
|
---|
304 | */
|
---|
305 | /* The implied headers section. */
|
---|
306 | pMod->aSegments[0].pvUser = NULL;
|
---|
307 | pMod->aSegments[0].pchName = "TheHeaders";
|
---|
308 | pMod->aSegments[0].cchName = sizeof("TheHeaders") - 1;
|
---|
309 | pMod->aSegments[0].enmProt = KPROT_READONLY;
|
---|
310 | pMod->aSegments[0].cb = pModPE->Hdrs.OptionalHeader.SizeOfHeaders;
|
---|
311 | pMod->aSegments[0].Alignment = pModPE->Hdrs.OptionalHeader.SectionAlignment;
|
---|
312 | pMod->aSegments[0].LinkAddress = pModPE->Hdrs.OptionalHeader.ImageBase;
|
---|
313 | pMod->aSegments[0].offFile = 0;
|
---|
314 | pMod->aSegments[0].cbFile = pModPE->Hdrs.OptionalHeader.SizeOfHeaders;
|
---|
315 | pMod->aSegments[0].RVA = 0;
|
---|
316 | if (pMod->cSegments > 1)
|
---|
317 | pMod->aSegments[0].cbMapped = pModPE->aShdrs[0].VirtualAddress;
|
---|
318 | else
|
---|
319 | pMod->aSegments[0].cbMapped = pModPE->Hdrs.OptionalHeader.SizeOfHeaders;
|
---|
320 | pMod->aSegments[0].MapAddress = 0;
|
---|
321 |
|
---|
322 | /* The section headers. */
|
---|
323 | for (i = 0; i < pModPE->Hdrs.FileHeader.NumberOfSections; i++)
|
---|
324 | {
|
---|
325 | const char *pch;
|
---|
326 |
|
---|
327 | /* unused */
|
---|
328 | pMod->aSegments[i + 1].pvUser = NULL;
|
---|
329 | pMod->aSegments[i + 1].MapAddress = 0;
|
---|
330 | pMod->aSegments[i + 1].SelFlat = 0;
|
---|
331 | pMod->aSegments[i + 1].Sel16bit = 0;
|
---|
332 | pMod->aSegments[i + 1].fFlags = 0;
|
---|
333 |
|
---|
334 | /* name */
|
---|
335 | pMod->aSegments[i + 1].pchName = pch = (const char *)&pModPE->aShdrs[i].Name[0];
|
---|
336 | cb = IMAGE_SIZEOF_SHORT_NAME;
|
---|
337 | while ( cb > 0
|
---|
338 | && (pch[cb - 1] == ' ' || pch[cb - 1] == '\0'))
|
---|
339 | cb--;
|
---|
340 | pMod->aSegments[i + 1].cchName = cb;
|
---|
341 |
|
---|
342 | /* size and addresses */
|
---|
343 | if (!(pModPE->aShdrs[i].Characteristics & IMAGE_SCN_TYPE_NOLOAD))
|
---|
344 | {
|
---|
345 | pMod->aSegments[i + 1].cb = pModPE->aShdrs[i].Misc.VirtualSize;
|
---|
346 | pMod->aSegments[i + 1].LinkAddress = pModPE->aShdrs[i].VirtualAddress
|
---|
347 | + pModPE->Hdrs.OptionalHeader.ImageBase;
|
---|
348 | pMod->aSegments[i + 1].RVA = pModPE->aShdrs[i].VirtualAddress;
|
---|
349 | pMod->aSegments[i + 1].cbMapped = pModPE->aShdrs[i].Misc.VirtualSize;
|
---|
350 | if (i + 2 < pMod->cSegments)
|
---|
351 | pMod->aSegments[i + 1].cbMapped= pModPE->aShdrs[i + 1].VirtualAddress
|
---|
352 | - pModPE->aShdrs[i].VirtualAddress;
|
---|
353 | }
|
---|
354 | else
|
---|
355 | {
|
---|
356 | pMod->aSegments[i + 1].cb = 0;
|
---|
357 | pMod->aSegments[i + 1].cbMapped = 0;
|
---|
358 | pMod->aSegments[i + 1].LinkAddress = NIL_KLDRADDR;
|
---|
359 | pMod->aSegments[i + 1].RVA = 0;
|
---|
360 | }
|
---|
361 |
|
---|
362 | /* file location */
|
---|
363 | pMod->aSegments[i + 1].offFile = pModPE->aShdrs[i].PointerToRawData;
|
---|
364 | pMod->aSegments[i + 1].cbFile = pModPE->aShdrs[i].SizeOfRawData;
|
---|
365 | if ( pMod->aSegments[i + 1].cbMapped > 0 /* if mapped */
|
---|
366 | && (KLDRSIZE)pMod->aSegments[i + 1].cbFile > pMod->aSegments[i + 1].cbMapped)
|
---|
367 | pMod->aSegments[i + 1].cbFile = pMod->aSegments[i + 1].cbMapped;
|
---|
368 |
|
---|
369 | /* protection */
|
---|
370 | switch ( pModPE->aShdrs[i].Characteristics
|
---|
371 | & (IMAGE_SCN_MEM_SHARED | IMAGE_SCN_MEM_EXECUTE | IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_WRITE))
|
---|
372 | {
|
---|
373 | case 0:
|
---|
374 | case IMAGE_SCN_MEM_SHARED:
|
---|
375 | pMod->aSegments[i + 1].enmProt = KPROT_NOACCESS;
|
---|
376 | break;
|
---|
377 | case IMAGE_SCN_MEM_READ:
|
---|
378 | case IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_SHARED:
|
---|
379 | pMod->aSegments[i + 1].enmProt = KPROT_READONLY;
|
---|
380 | break;
|
---|
381 | case IMAGE_SCN_MEM_WRITE:
|
---|
382 | case IMAGE_SCN_MEM_WRITE | IMAGE_SCN_MEM_READ:
|
---|
383 | pMod->aSegments[i + 1].enmProt = KPROT_WRITECOPY;
|
---|
384 | break;
|
---|
385 | case IMAGE_SCN_MEM_WRITE | IMAGE_SCN_MEM_SHARED:
|
---|
386 | case IMAGE_SCN_MEM_WRITE | IMAGE_SCN_MEM_SHARED | IMAGE_SCN_MEM_READ:
|
---|
387 | pMod->aSegments[i + 1].enmProt = KPROT_READWRITE;
|
---|
388 | break;
|
---|
389 | case IMAGE_SCN_MEM_EXECUTE:
|
---|
390 | case IMAGE_SCN_MEM_EXECUTE | IMAGE_SCN_MEM_SHARED:
|
---|
391 | pMod->aSegments[i + 1].enmProt = KPROT_EXECUTE;
|
---|
392 | break;
|
---|
393 | case IMAGE_SCN_MEM_EXECUTE | IMAGE_SCN_MEM_READ:
|
---|
394 | case IMAGE_SCN_MEM_EXECUTE | IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_SHARED:
|
---|
395 | pMod->aSegments[i + 1].enmProt = KPROT_EXECUTE_READ;
|
---|
396 | break;
|
---|
397 | case IMAGE_SCN_MEM_EXECUTE | IMAGE_SCN_MEM_WRITE:
|
---|
398 | case IMAGE_SCN_MEM_EXECUTE | IMAGE_SCN_MEM_WRITE | IMAGE_SCN_MEM_READ:
|
---|
399 | pMod->aSegments[i + 1].enmProt = KPROT_EXECUTE_WRITECOPY;
|
---|
400 | break;
|
---|
401 | case IMAGE_SCN_MEM_EXECUTE | IMAGE_SCN_MEM_WRITE | IMAGE_SCN_MEM_SHARED:
|
---|
402 | case IMAGE_SCN_MEM_EXECUTE | IMAGE_SCN_MEM_WRITE | IMAGE_SCN_MEM_SHARED | IMAGE_SCN_MEM_READ:
|
---|
403 | pMod->aSegments[i + 1].enmProt = KPROT_EXECUTE_READWRITE;
|
---|
404 | break;
|
---|
405 | }
|
---|
406 |
|
---|
407 | /* alignment. */
|
---|
408 | switch (pModPE->aShdrs[i].Characteristics & IMAGE_SCN_ALIGN_MASK)
|
---|
409 | {
|
---|
410 | case 0: /* hope this is right... */
|
---|
411 | pMod->aSegments[i + 1].Alignment = pModPE->Hdrs.OptionalHeader.SectionAlignment;
|
---|
412 | break;
|
---|
413 | case IMAGE_SCN_ALIGN_1BYTES: pMod->aSegments[i + 1].Alignment = 1; break;
|
---|
414 | case IMAGE_SCN_ALIGN_2BYTES: pMod->aSegments[i + 1].Alignment = 2; break;
|
---|
415 | case IMAGE_SCN_ALIGN_4BYTES: pMod->aSegments[i + 1].Alignment = 4; break;
|
---|
416 | case IMAGE_SCN_ALIGN_8BYTES: pMod->aSegments[i + 1].Alignment = 8; break;
|
---|
417 | case IMAGE_SCN_ALIGN_16BYTES: pMod->aSegments[i + 1].Alignment = 16; break;
|
---|
418 | case IMAGE_SCN_ALIGN_32BYTES: pMod->aSegments[i + 1].Alignment = 32; break;
|
---|
419 | case IMAGE_SCN_ALIGN_64BYTES: pMod->aSegments[i + 1].Alignment = 64; break;
|
---|
420 | case IMAGE_SCN_ALIGN_128BYTES: pMod->aSegments[i + 1].Alignment = 128; break;
|
---|
421 | case IMAGE_SCN_ALIGN_256BYTES: pMod->aSegments[i + 1].Alignment = 256; break;
|
---|
422 | case IMAGE_SCN_ALIGN_512BYTES: pMod->aSegments[i + 1].Alignment = 512; break;
|
---|
423 | case IMAGE_SCN_ALIGN_1024BYTES: pMod->aSegments[i + 1].Alignment = 1024; break;
|
---|
424 | case IMAGE_SCN_ALIGN_2048BYTES: pMod->aSegments[i + 1].Alignment = 2048; break;
|
---|
425 | case IMAGE_SCN_ALIGN_4096BYTES: pMod->aSegments[i + 1].Alignment = 4096; break;
|
---|
426 | case IMAGE_SCN_ALIGN_8192BYTES: pMod->aSegments[i + 1].Alignment = 8192; break;
|
---|
427 | default: kHlpAssert(0); pMod->aSegments[i + 1].Alignment = 0; break;
|
---|
428 | }
|
---|
429 | }
|
---|
430 |
|
---|
431 | /*
|
---|
432 | * We're done.
|
---|
433 | */
|
---|
434 | *ppModPE = pModPE;
|
---|
435 | return 0;
|
---|
436 | }
|
---|
437 |
|
---|
438 |
|
---|
439 | /**
|
---|
440 | * Converts a 32-bit optional header to a 64-bit one
|
---|
441 | *
|
---|
442 | * @param pOptHdr The optional header to convert.
|
---|
443 | */
|
---|
444 | static void kldrModPEDoOptionalHeaderConversion(PIMAGE_OPTIONAL_HEADER64 pOptHdr)
|
---|
445 | {
|
---|
446 | /* volatile everywhere! */
|
---|
447 | IMAGE_OPTIONAL_HEADER32 volatile *pOptHdr32 = (IMAGE_OPTIONAL_HEADER32 volatile *)pOptHdr;
|
---|
448 | IMAGE_OPTIONAL_HEADER64 volatile *pOptHdr64 = pOptHdr;
|
---|
449 | KU32 volatile *pu32Dst;
|
---|
450 | KU32 volatile *pu32Src;
|
---|
451 | KU32 volatile *pu32SrcLast;
|
---|
452 | KU32 u32;
|
---|
453 |
|
---|
454 | /* From LoaderFlags and out the difference is 4 * 32-bits. */
|
---|
455 | pu32Dst = (KU32 *)&pOptHdr64->DataDirectory[IMAGE_NUMBEROF_DIRECTORY_ENTRIES] - 1;
|
---|
456 | pu32Src = (KU32 *)&pOptHdr32->DataDirectory[IMAGE_NUMBEROF_DIRECTORY_ENTRIES] - 1;
|
---|
457 | pu32SrcLast = (KU32 *)&pOptHdr32->LoaderFlags;
|
---|
458 | while (pu32Src >= pu32SrcLast)
|
---|
459 | *pu32Dst-- = *pu32Src--;
|
---|
460 |
|
---|
461 | /* The previous 4 fields are 32/64 and needs special attention. */
|
---|
462 | pOptHdr64->SizeOfHeapCommit = pOptHdr32->SizeOfHeapCommit;
|
---|
463 | pOptHdr64->SizeOfHeapReserve = pOptHdr32->SizeOfHeapReserve;
|
---|
464 | pOptHdr64->SizeOfStackCommit = pOptHdr32->SizeOfStackCommit;
|
---|
465 | u32 = pOptHdr32->SizeOfStackReserve;
|
---|
466 | pOptHdr64->SizeOfStackReserve = u32;
|
---|
467 |
|
---|
468 | /*
|
---|
469 | * The rest matches except for BaseOfData which has been merged into ImageBase in the 64-bit version.
|
---|
470 | * Thus, ImageBase needs some special treatement. It will probably work fine assigning one to the
|
---|
471 | * other since this is all declared volatile, but taking now chances, we'll use a temp variable.
|
---|
472 | */
|
---|
473 | u32 = pOptHdr32->ImageBase;
|
---|
474 | pOptHdr64->ImageBase = u32;
|
---|
475 | }
|
---|
476 |
|
---|
477 |
|
---|
478 | #if 0
|
---|
479 | /**
|
---|
480 | * Converts a 32-bit load config directory to a 64 bit one.
|
---|
481 | *
|
---|
482 | * @param pOptHdr The load config to convert.
|
---|
483 | */
|
---|
484 | static void kldrModPEDoLoadConfigConversion(PIMAGE_LOAD_CONFIG_DIRECTORY64 pLoadCfg)
|
---|
485 | {
|
---|
486 | /* volatile everywhere! */
|
---|
487 | IMAGE_LOAD_CONFIG_DIRECTORY32 volatile *pLoadCfg32 = (IMAGE_LOAD_CONFIG_DIRECTORY32 volatile *)pLoadCfg;
|
---|
488 | IMAGE_LOAD_CONFIG_DIRECTORY64 volatile *pLoadCfg64 = pLoadCfg;
|
---|
489 | KU32 u32;
|
---|
490 |
|
---|
491 | pLoadCfg64->SEHandlerCount = pLoadCfg32->SEHandlerCount;
|
---|
492 | pLoadCfg64->SEHandlerTable = pLoadCfg32->SEHandlerTable;
|
---|
493 | pLoadCfg64->SecurityCookie = pLoadCfg32->SecurityCookie;
|
---|
494 | pLoadCfg64->EditList = pLoadCfg32->EditList;
|
---|
495 | pLoadCfg64->Reserved1 = pLoadCfg32->Reserved1;
|
---|
496 | pLoadCfg64->CSDVersion = pLoadCfg32->CSDVersion;
|
---|
497 | /* (ProcessHeapFlags switched place with ProcessAffinityMask, but we're
|
---|
498 | * more than 16 byte off by now so it doesn't matter.) */
|
---|
499 | pLoadCfg64->ProcessHeapFlags = pLoadCfg32->ProcessHeapFlags;
|
---|
500 | pLoadCfg64->ProcessAffinityMask = pLoadCfg32->ProcessAffinityMask;
|
---|
501 | pLoadCfg64->VirtualMemoryThreshold = pLoadCfg32->VirtualMemoryThreshold;
|
---|
502 | pLoadCfg64->MaximumAllocationSize = pLoadCfg32->MaximumAllocationSize;
|
---|
503 | pLoadCfg64->LockPrefixTable = pLoadCfg32->LockPrefixTable;
|
---|
504 | pLoadCfg64->DeCommitTotalFreeThreshold = pLoadCfg32->DeCommitTotalFreeThreshold;
|
---|
505 | u32 = pLoadCfg32->DeCommitFreeBlockThreshold;
|
---|
506 | pLoadCfg64->DeCommitFreeBlockThreshold = u32;
|
---|
507 | /* the remainder matches. */
|
---|
508 | }
|
---|
509 | #endif
|
---|
510 |
|
---|
511 |
|
---|
512 | /**
|
---|
513 | * Internal worker which validates the section headers.
|
---|
514 | */
|
---|
515 | static int kLdrModPEDoOptionalHeaderValidation(PKLDRMODPE pModPE)
|
---|
516 | {
|
---|
517 | const unsigned fIs32Bit = pModPE->Hdrs.FileHeader.SizeOfOptionalHeader == sizeof(IMAGE_OPTIONAL_HEADER32);
|
---|
518 |
|
---|
519 | /* the magic */
|
---|
520 | if ( pModPE->Hdrs.OptionalHeader.Magic
|
---|
521 | != (fIs32Bit ? IMAGE_NT_OPTIONAL_HDR32_MAGIC : IMAGE_NT_OPTIONAL_HDR64_MAGIC))
|
---|
522 | return KLDR_ERR_PE_BAD_OPTIONAL_HEADER;
|
---|
523 |
|
---|
524 | /** @todo validate more */
|
---|
525 | return 0;
|
---|
526 | }
|
---|
527 |
|
---|
528 |
|
---|
529 | /**
|
---|
530 | * Internal worker which validates the section headers.
|
---|
531 | */
|
---|
532 | static int kLdrModPEDoSectionHeadersValidation(PKLDRMODPE pModPE)
|
---|
533 | {
|
---|
534 | /** @todo validate shdrs */
|
---|
535 | return 0;
|
---|
536 | }
|
---|
537 |
|
---|
538 |
|
---|
539 | /** @copydoc KLDRMODOPS::pfnDestroy */
|
---|
540 | static int kldrModPEDestroy(PKLDRMOD pMod)
|
---|
541 | {
|
---|
542 | PKLDRMODPE pModPE = (PKLDRMODPE)pMod->pvData;
|
---|
543 | int rc = 0;
|
---|
544 | KLDRMODPE_ASSERT(!pModPE->pvMapping);
|
---|
545 |
|
---|
546 | if (pMod->pRdr)
|
---|
547 | {
|
---|
548 | rc = kRdrClose(pMod->pRdr);
|
---|
549 | pMod->pRdr = NULL;
|
---|
550 | }
|
---|
551 | pMod->u32Magic = 0;
|
---|
552 | pMod->pOps = NULL;
|
---|
553 | kHlpFree(pModPE);
|
---|
554 | return rc;
|
---|
555 | }
|
---|
556 |
|
---|
557 |
|
---|
558 | /**
|
---|
559 | * Performs the mapping of the image.
|
---|
560 | *
|
---|
561 | * This can be used to do the internal mapping as well as the
|
---|
562 | * user requested mapping. fForReal indicates which is desired.
|
---|
563 | *
|
---|
564 | * @returns 0 on success, non-zero OS or kLdr status code on failure.
|
---|
565 | * @param pModPE The interpreter module instance
|
---|
566 | * @param fForReal If set, do the user mapping. if clear, do the internal mapping.
|
---|
567 | */
|
---|
568 | static int kldrModPEDoMap(PKLDRMODPE pModPE, unsigned fForReal)
|
---|
569 | {
|
---|
570 | PKLDRMOD pMod = pModPE->pMod;
|
---|
571 | KBOOL fFixed;
|
---|
572 | void *pvBase;
|
---|
573 | int rc;
|
---|
574 | KU32 i;
|
---|
575 |
|
---|
576 | /*
|
---|
577 | * Map it.
|
---|
578 | */
|
---|
579 | /* fixed image? */
|
---|
580 | fFixed = fForReal
|
---|
581 | && ( pMod->enmType == KLDRTYPE_EXECUTABLE_FIXED
|
---|
582 | || pMod->enmType == KLDRTYPE_SHARED_LIBRARY_FIXED);
|
---|
583 | if (!fFixed)
|
---|
584 | pvBase = NULL;
|
---|
585 | else
|
---|
586 | {
|
---|
587 | pvBase = (void *)(KUPTR)pMod->aSegments[0].LinkAddress;
|
---|
588 | if ((KUPTR)pvBase != pMod->aSegments[0].LinkAddress)
|
---|
589 | return KLDR_ERR_ADDRESS_OVERFLOW;
|
---|
590 | }
|
---|
591 |
|
---|
592 | /* try do the prepare */
|
---|
593 | rc = kRdrMap(pMod->pRdr, &pvBase, pMod->cSegments, pMod->aSegments, fFixed);
|
---|
594 | if (rc)
|
---|
595 | return rc;
|
---|
596 |
|
---|
597 | /*
|
---|
598 | * Update the segments with their map addresses.
|
---|
599 | */
|
---|
600 | if (fForReal)
|
---|
601 | {
|
---|
602 | for (i = 0; i < pMod->cSegments; i++)
|
---|
603 | {
|
---|
604 | if (pMod->aSegments[i].RVA != NIL_KLDRADDR)
|
---|
605 | pMod->aSegments[i].MapAddress = (KUPTR)pvBase + (KUPTR)pMod->aSegments[i].RVA;
|
---|
606 | }
|
---|
607 | pModPE->pvMapping = pvBase;
|
---|
608 | }
|
---|
609 | else
|
---|
610 | pModPE->pvBits = pvBase;
|
---|
611 | return 0;
|
---|
612 | }
|
---|
613 |
|
---|
614 |
|
---|
615 | /**
|
---|
616 | * Unmaps a image mapping.
|
---|
617 | *
|
---|
618 | * This can be used to do the internal mapping as well as the
|
---|
619 | * user requested mapping. fForReal indicates which is desired.
|
---|
620 | *
|
---|
621 | * @returns 0 on success, non-zero OS or kLdr status code on failure.
|
---|
622 | * @param pModPE The interpreter module instance
|
---|
623 | * @param pvMapping The mapping to unmap.
|
---|
624 | */
|
---|
625 | static int kldrModPEDoUnmap(PKLDRMODPE pModPE, const void *pvMapping)
|
---|
626 | {
|
---|
627 | PKLDRMOD pMod = pModPE->pMod;
|
---|
628 | int rc;
|
---|
629 | KU32 i;
|
---|
630 |
|
---|
631 | /*
|
---|
632 | * Try unmap the image.
|
---|
633 | */
|
---|
634 | rc = kRdrUnmap(pMod->pRdr, (void *)pvMapping, pMod->cSegments, pMod->aSegments);
|
---|
635 | if (rc)
|
---|
636 | return rc;
|
---|
637 |
|
---|
638 | /*
|
---|
639 | * Update the segments to reflect that they aren't mapped any longer.
|
---|
640 | */
|
---|
641 | if (pModPE->pvMapping == pvMapping)
|
---|
642 | {
|
---|
643 | pModPE->pvMapping = NULL;
|
---|
644 | for (i = 0; i < pMod->cSegments; i++)
|
---|
645 | pMod->aSegments[i].MapAddress = 0;
|
---|
646 | }
|
---|
647 | if (pModPE->pvBits == pvMapping)
|
---|
648 | pModPE->pvBits = NULL;
|
---|
649 |
|
---|
650 | return 0;
|
---|
651 | }
|
---|
652 |
|
---|
653 |
|
---|
654 | /**
|
---|
655 | * Gets usable bits and the right base address.
|
---|
656 | *
|
---|
657 | * @returns 0 on success.
|
---|
658 | * @returns A non-zero status code if the BaseAddress isn't right or some problem is encountered
|
---|
659 | * featch in a temp mapping the bits.
|
---|
660 | * @param pModPE The interpreter module instance
|
---|
661 | * @param ppvBits The bits address, IN & OUT.
|
---|
662 | * @param pBaseAddress The base address, IN & OUT. Optional.
|
---|
663 | */
|
---|
664 | static int kldrModPEBitsAndBaseAddress(PKLDRMODPE pModPE, const void **ppvBits, PKLDRADDR pBaseAddress)
|
---|
665 | {
|
---|
666 | int rc = 0;
|
---|
667 |
|
---|
668 | /*
|
---|
669 | * Correct the base address.
|
---|
670 | *
|
---|
671 | * We don't use the base address for interpreting the bits in this
|
---|
672 | * interpreter, which makes things relativly simple.
|
---|
673 | */
|
---|
674 | if (pBaseAddress)
|
---|
675 | {
|
---|
676 | if (*pBaseAddress == KLDRMOD_BASEADDRESS_MAP)
|
---|
677 | *pBaseAddress = pModPE->pMod->aSegments[0].MapAddress;
|
---|
678 | else if (*pBaseAddress == KLDRMOD_BASEADDRESS_LINK)
|
---|
679 | *pBaseAddress = pModPE->Hdrs.OptionalHeader.ImageBase;
|
---|
680 | }
|
---|
681 |
|
---|
682 | /*
|
---|
683 | * Get bits.
|
---|
684 | */
|
---|
685 | if (ppvBits && !*ppvBits)
|
---|
686 | {
|
---|
687 | if (pModPE->pvMapping)
|
---|
688 | *ppvBits = pModPE->pvMapping;
|
---|
689 | else if (pModPE->pvBits)
|
---|
690 | *ppvBits = pModPE->pvBits;
|
---|
691 | else
|
---|
692 | {
|
---|
693 | /* create an internal mapping. */
|
---|
694 | rc = kldrModPEDoMap(pModPE, 0 /* not for real */);
|
---|
695 | if (rc)
|
---|
696 | return rc;
|
---|
697 | KLDRMODPE_ASSERT(pModPE->pvBits);
|
---|
698 | *ppvBits = pModPE->pvBits;
|
---|
699 | }
|
---|
700 | }
|
---|
701 |
|
---|
702 | return 0;
|
---|
703 | }
|
---|
704 |
|
---|
705 |
|
---|
706 | /** @copydoc kLdrModQuerySymbol */
|
---|
707 | static int kldrModPEQuerySymbol(PKLDRMOD pMod, const void *pvBits, KLDRADDR BaseAddress, KU32 iSymbol,
|
---|
708 | const char *pchSymbol, KSIZE cchSymbol, const char *pszVersion,
|
---|
709 | PFNKLDRMODGETIMPORT pfnGetForwarder, void *pvUser, PKLDRADDR puValue, KU32 *pfKind)
|
---|
710 |
|
---|
711 | {
|
---|
712 | PKLDRMODPE pModPE = (PKLDRMODPE)pMod->pvData;
|
---|
713 | const KU32 *paExportRVAs;
|
---|
714 | const IMAGE_EXPORT_DIRECTORY *pExpDir;
|
---|
715 | KU32 iExpOrd;
|
---|
716 | KU32 uRVA;
|
---|
717 | int rc;
|
---|
718 |
|
---|
719 | /*
|
---|
720 | * Make sure we've got mapped bits and resolve any base address aliases.
|
---|
721 | */
|
---|
722 | rc = kldrModPEBitsAndBaseAddress(pModPE, &pvBits, &BaseAddress);
|
---|
723 | if (rc)
|
---|
724 | return rc;
|
---|
725 | if ( pModPE->Hdrs.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].Size
|
---|
726 | < sizeof(IMAGE_EXPORT_DIRECTORY))
|
---|
727 | return KLDR_ERR_SYMBOL_NOT_FOUND;
|
---|
728 | if (pszVersion && *pszVersion)
|
---|
729 | return KLDR_ERR_SYMBOL_NOT_FOUND;
|
---|
730 |
|
---|
731 | pExpDir = KLDRMODPE_RVA2TYPE(pvBits,
|
---|
732 | pModPE->Hdrs.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress,
|
---|
733 | PIMAGE_EXPORT_DIRECTORY);
|
---|
734 | if (!pchSymbol)
|
---|
735 | {
|
---|
736 | /*
|
---|
737 | * Simple, calculate the unbased ordinal and bounds check it.
|
---|
738 | */
|
---|
739 | iExpOrd = iSymbol - pExpDir->Base;
|
---|
740 | if (iExpOrd >= K_MAX(pExpDir->NumberOfNames, pExpDir->NumberOfFunctions))
|
---|
741 | return KLDR_ERR_SYMBOL_NOT_FOUND;
|
---|
742 | }
|
---|
743 | else
|
---|
744 | {
|
---|
745 | /*
|
---|
746 | * Do a binary search for the name.
|
---|
747 | * (The name table is sorted in ascending ordered by the linker.)
|
---|
748 | */
|
---|
749 | const KU32 *paRVANames = KLDRMODPE_RVA2TYPE(pvBits, pExpDir->AddressOfNames, const KU32 *);
|
---|
750 | const KU16 *paOrdinals = KLDRMODPE_RVA2TYPE(pvBits, pExpDir->AddressOfNameOrdinals, const KU16 *);
|
---|
751 | KI32 iStart = 1; /* one based binary searching is simpler. */
|
---|
752 | KI32 iEnd = pExpDir->NumberOfNames;
|
---|
753 |
|
---|
754 | for (;;)
|
---|
755 | {
|
---|
756 | KI32 i;
|
---|
757 | int diff;
|
---|
758 | const char *pszName;
|
---|
759 |
|
---|
760 | /* done? */
|
---|
761 | if (iStart > iEnd)
|
---|
762 | {
|
---|
763 | #ifdef KLDRMODPE_STRICT /* Make sure the linker and we both did our job right. */
|
---|
764 | for (i = 0; i < (KI32)pExpDir->NumberOfNames; i++)
|
---|
765 |
|
---|
766 | {
|
---|
767 | pszName = KLDRMODPE_RVA2TYPE(pvBits, paRVANames[i], const char *);
|
---|
768 | KLDRMODPE_ASSERT(kHlpStrNComp(pszName, pchSymbol, cchSymbol) || pszName[cchSymbol]);
|
---|
769 | KLDRMODPE_ASSERT(i == 0 || kHlpStrComp(pszName, KLDRMODPE_RVA2TYPE(pvBits, paRVANames[i - 1], const char *)));
|
---|
770 | }
|
---|
771 | #endif
|
---|
772 | return KLDR_ERR_SYMBOL_NOT_FOUND;
|
---|
773 | }
|
---|
774 |
|
---|
775 | i = (iEnd - iStart) / 2 + iStart;
|
---|
776 | pszName = KLDRMODPE_RVA2TYPE(pvBits, paRVANames[i - 1], const char *);
|
---|
777 | diff = kHlpStrNComp(pszName, pchSymbol, cchSymbol);
|
---|
778 | if (!diff)
|
---|
779 | diff = pszName[cchSymbol] - 0;
|
---|
780 | if (diff < 0)
|
---|
781 | iStart = i + 1; /* The symbol must be after the current name. */
|
---|
782 | else if (diff)
|
---|
783 | iEnd = i - 1; /* The symbol must be before the current name. */
|
---|
784 | else
|
---|
785 | {
|
---|
786 | iExpOrd = paOrdinals[i - 1]; /* match! */
|
---|
787 | break;
|
---|
788 | }
|
---|
789 | }
|
---|
790 | }
|
---|
791 |
|
---|
792 | /*
|
---|
793 | * Lookup the address in the 'symbol' table.
|
---|
794 | */
|
---|
795 | paExportRVAs = KLDRMODPE_RVA2TYPE(pvBits, pExpDir->AddressOfFunctions, const KU32 *);
|
---|
796 | uRVA = paExportRVAs[iExpOrd];
|
---|
797 | if ( uRVA - pModPE->Hdrs.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress
|
---|
798 | < pModPE->Hdrs.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].Size)
|
---|
799 | return kldrModPEDoForwarderQuery(pModPE, pvBits, KLDRMODPE_RVA2TYPE(pvBits, uRVA, const char *),
|
---|
800 | pfnGetForwarder, pvUser, puValue, pfKind);
|
---|
801 |
|
---|
802 | /*
|
---|
803 | * Set the return value.
|
---|
804 | */
|
---|
805 | if (puValue)
|
---|
806 | *puValue = BaseAddress + uRVA;
|
---|
807 | if (pfKind)
|
---|
808 | *pfKind = (pModPE->Hdrs.FileHeader.SizeOfOptionalHeader == sizeof(IMAGE_OPTIONAL_HEADER32)
|
---|
809 | ? KLDRSYMKIND_32BIT : KLDRSYMKIND_64BIT)
|
---|
810 | | KLDRSYMKIND_NO_TYPE;
|
---|
811 | return 0;
|
---|
812 | }
|
---|
813 |
|
---|
814 |
|
---|
815 | /**
|
---|
816 | * Deal with a forwarder entry.
|
---|
817 | *
|
---|
818 | * We do this seprately from kldrModPEQuerySymbol because the code is clumsy (as is all PE code
|
---|
819 | * thanks to the descriptive field names), and because it uses quite a bit more stack and we're
|
---|
820 | * trying to avoid allocating stack unless we have to.
|
---|
821 | *
|
---|
822 | * @returns See kLdrModQuerySymbol.
|
---|
823 | * @param pModPE The PE module interpreter instance.
|
---|
824 | * @param pvBits Where to read the image from.
|
---|
825 | * @param pszForwarder The forwarder entry name.
|
---|
826 | * @param pfnGetForwarder The callback for resolving forwarder symbols. (optional)
|
---|
827 | * @param pvUser The user argument for the callback.
|
---|
828 | * @param puValue Where to put the value. (optional)
|
---|
829 | * @param pfKind Where to put the symbol kind. (optional)
|
---|
830 | */
|
---|
831 | static int kldrModPEDoForwarderQuery(PKLDRMODPE pModPE, const void *pvBits, const char *pszForwarder,
|
---|
832 | PFNKLDRMODGETIMPORT pfnGetForwarder, void *pvUser, PKLDRADDR puValue, KU32 *pfKind)
|
---|
833 | {
|
---|
834 | const IMAGE_IMPORT_DESCRIPTOR *paImpDir;
|
---|
835 | KU32 iImpModule;
|
---|
836 | KU32 cchImpModule;
|
---|
837 | const char *pszSymbol;
|
---|
838 | KU32 iSymbol;
|
---|
839 | int rc;
|
---|
840 |
|
---|
841 | if (!pfnGetForwarder)
|
---|
842 | return KLDR_ERR_FORWARDER_SYMBOL;
|
---|
843 |
|
---|
844 | /*
|
---|
845 | * Separate the name into a module name and a symbol name or ordinal.
|
---|
846 | *
|
---|
847 | * The module name ends at the first dot ('.').
|
---|
848 | * After the dot follows either a symbol name or a hash ('#') + ordinal.
|
---|
849 | */
|
---|
850 | pszSymbol = pszForwarder;
|
---|
851 | while (*pszSymbol != '.')
|
---|
852 | pszSymbol++;
|
---|
853 | if (!*pszSymbol)
|
---|
854 | return KLDR_ERR_PE_BAD_FORWARDER;
|
---|
855 | cchImpModule = pszSymbol - pszForwarder;
|
---|
856 |
|
---|
857 | pszSymbol++; /* skip the dot */
|
---|
858 | if (!*pszSymbol)
|
---|
859 | return KLDR_ERR_PE_BAD_FORWARDER;
|
---|
860 | if (*pszSymbol == '#')
|
---|
861 | {
|
---|
862 | unsigned uBase;
|
---|
863 | pszSymbol++; /* skip the hash */
|
---|
864 |
|
---|
865 | /* base detection */
|
---|
866 | uBase = 10;
|
---|
867 | if (pszSymbol[0] == '0' && (pszSymbol[1] == 'x' || pszSymbol[1] == 'X'))
|
---|
868 | {
|
---|
869 | uBase = 16;
|
---|
870 | pszSymbol += 2;
|
---|
871 | }
|
---|
872 |
|
---|
873 | /* ascii to integer */
|
---|
874 | iSymbol = 0;
|
---|
875 | for (;;)
|
---|
876 | {
|
---|
877 | /* convert char to digit. */
|
---|
878 | unsigned uDigit = *pszSymbol++;
|
---|
879 | if (uDigit >= '0' && uDigit <= '9')
|
---|
880 | uDigit -= '0';
|
---|
881 | else if (uDigit >= 'a' && uDigit <= 'z')
|
---|
882 | uDigit -= 'a' + 10;
|
---|
883 | else if (uDigit >= 'A' && uDigit <= 'Z')
|
---|
884 | uDigit -= 'A' + 10;
|
---|
885 | else if (!uDigit)
|
---|
886 | break;
|
---|
887 | else
|
---|
888 | return KLDR_ERR_PE_BAD_FORWARDER;
|
---|
889 | if (uDigit >= uBase)
|
---|
890 | return KLDR_ERR_PE_BAD_FORWARDER;
|
---|
891 |
|
---|
892 | /* insert the digit */
|
---|
893 | iSymbol *= uBase;
|
---|
894 | iSymbol += uDigit;
|
---|
895 | }
|
---|
896 |
|
---|
897 | pszSymbol = NULL; /* no symbol name. */
|
---|
898 | }
|
---|
899 | else
|
---|
900 | iSymbol = NIL_KLDRMOD_SYM_ORDINAL; /* no ordinal number. */
|
---|
901 |
|
---|
902 |
|
---|
903 | /*
|
---|
904 | * Find the import module name.
|
---|
905 | *
|
---|
906 | * We ASSUME the linker will make sure there is an import
|
---|
907 | * entry for the module... not sure if this is right though.
|
---|
908 | */
|
---|
909 | if ( !pModPE->Hdrs.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].Size
|
---|
910 | || !pModPE->Hdrs.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress)
|
---|
911 | return KLDR_ERR_PE_FORWARDER_IMPORT_NOT_FOUND;
|
---|
912 | paImpDir = KLDRMODPE_RVA2TYPE(pvBits,
|
---|
913 | pModPE->Hdrs.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress,
|
---|
914 | const IMAGE_IMPORT_DESCRIPTOR *);
|
---|
915 |
|
---|
916 | kldrModPENumberOfImports(pModPE->pMod, pvBits);
|
---|
917 | for (iImpModule = 0; iImpModule < pModPE->cImportModules; iImpModule++)
|
---|
918 | {
|
---|
919 | const char *pszName = KLDRMODPE_RVA2TYPE(pvBits, paImpDir[iImpModule].Name, const char *);
|
---|
920 | KSIZE cchName = kHlpStrLen(pszName);
|
---|
921 | if ( ( cchName == cchImpModule
|
---|
922 | || ( cchName > cchImpModule
|
---|
923 | && pszName[cchImpModule] == '.'
|
---|
924 | && (pszName[cchImpModule + 1] == 'd' || pszName[cchImpModule + 1] == 'D')
|
---|
925 | && (pszName[cchImpModule + 2] == 'l' || pszName[cchImpModule + 2] == 'L')
|
---|
926 | && (pszName[cchImpModule + 3] == 'l' || pszName[cchImpModule + 3] == 'L'))
|
---|
927 | )
|
---|
928 | && kHlpMemICompAscii(pszName, pszForwarder, cchImpModule)
|
---|
929 | )
|
---|
930 | {
|
---|
931 | /*
|
---|
932 | * Now the rest is up to the callback (almost).
|
---|
933 | */
|
---|
934 | rc = pfnGetForwarder(pModPE->pMod, iImpModule, iSymbol, pszSymbol,
|
---|
935 | pszSymbol ? kHlpStrLen(pszSymbol) : 0, NULL, puValue, pfKind, pvUser);
|
---|
936 | if (!rc && pfKind)
|
---|
937 | *pfKind |= KLDRSYMKIND_FORWARDER;
|
---|
938 | return rc;
|
---|
939 | }
|
---|
940 | }
|
---|
941 | return KLDR_ERR_PE_FORWARDER_IMPORT_NOT_FOUND;
|
---|
942 | }
|
---|
943 |
|
---|
944 |
|
---|
945 | /** @copydoc kLdrModEnumSymbols */
|
---|
946 | static int kldrModPEEnumSymbols(PKLDRMOD pMod, const void *pvBits, KLDRADDR BaseAddress,
|
---|
947 | KU32 fFlags, PFNKLDRMODENUMSYMS pfnCallback, void *pvUser)
|
---|
948 | {
|
---|
949 | PKLDRMODPE pModPE = (PKLDRMODPE)pMod->pvData;
|
---|
950 | const KU32 *paFunctions;
|
---|
951 | const IMAGE_EXPORT_DIRECTORY *pExpDir;
|
---|
952 | const KU32 *paRVANames;
|
---|
953 | const KU16 *paOrdinals;
|
---|
954 | KU32 iFunction;
|
---|
955 | KU32 cFunctions;
|
---|
956 | KU32 cNames;
|
---|
957 | int rc;
|
---|
958 |
|
---|
959 | /*
|
---|
960 | * Make sure we've got mapped bits and resolve any base address aliases.
|
---|
961 | */
|
---|
962 | rc = kldrModPEBitsAndBaseAddress(pModPE, &pvBits, &BaseAddress);
|
---|
963 | if (rc)
|
---|
964 | return rc;
|
---|
965 |
|
---|
966 | if ( pModPE->Hdrs.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].Size
|
---|
967 | < sizeof(IMAGE_EXPORT_DIRECTORY))
|
---|
968 | return 0; /* no exports to enumerate, return success. */
|
---|
969 |
|
---|
970 | pExpDir = KLDRMODPE_RVA2TYPE(pvBits,
|
---|
971 | pModPE->Hdrs.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress,
|
---|
972 | PIMAGE_EXPORT_DIRECTORY);
|
---|
973 |
|
---|
974 | /*
|
---|
975 | * Enumerate the ordinal exports.
|
---|
976 | */
|
---|
977 | paRVANames = KLDRMODPE_RVA2TYPE(pvBits, pExpDir->AddressOfNames, const KU32 *);
|
---|
978 | paOrdinals = KLDRMODPE_RVA2TYPE(pvBits, pExpDir->AddressOfNameOrdinals, const KU16 *);
|
---|
979 | paFunctions = KLDRMODPE_RVA2TYPE(pvBits, pExpDir->AddressOfFunctions, const KU32 *);
|
---|
980 | cFunctions = pExpDir->NumberOfFunctions;
|
---|
981 | cNames = pExpDir->NumberOfNames;
|
---|
982 | for (iFunction = 0; iFunction < cFunctions; iFunction++)
|
---|
983 | {
|
---|
984 | unsigned fFoundName;
|
---|
985 | KU32 iName;
|
---|
986 | const KU32 uRVA = paFunctions[iFunction];
|
---|
987 | const KLDRADDR uValue = BaseAddress + uRVA;
|
---|
988 | KU32 fKind = (pModPE->Hdrs.FileHeader.SizeOfOptionalHeader == sizeof(IMAGE_OPTIONAL_HEADER32)
|
---|
989 | ? KLDRSYMKIND_32BIT : KLDRSYMKIND_64BIT)
|
---|
990 | | KLDRSYMKIND_NO_TYPE;
|
---|
991 | if ( uRVA - pModPE->Hdrs.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress
|
---|
992 | < pModPE->Hdrs.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].Size)
|
---|
993 | fKind |= KLDRSYMKIND_FORWARDER;
|
---|
994 |
|
---|
995 | /*
|
---|
996 | * Any symbol names?
|
---|
997 | */
|
---|
998 | fFoundName = 0;
|
---|
999 | for (iName = 0; iName < cNames; iName++)
|
---|
1000 | {
|
---|
1001 | const char *pszName;
|
---|
1002 | if (paOrdinals[iName] != iFunction)
|
---|
1003 | continue;
|
---|
1004 | fFoundName = 1;
|
---|
1005 | pszName = KLDRMODPE_RVA2TYPE(pvBits, paRVANames[iName], const char *);
|
---|
1006 | rc = pfnCallback(pMod, iFunction + pExpDir->Base, pszName, kHlpStrLen(pszName), NULL,
|
---|
1007 | uValue, fKind, pvUser);
|
---|
1008 | if (rc)
|
---|
1009 | return rc;
|
---|
1010 | }
|
---|
1011 |
|
---|
1012 | /*
|
---|
1013 | * If no names, call once with the ordinal only.
|
---|
1014 | */
|
---|
1015 | if (!fFoundName)
|
---|
1016 | {
|
---|
1017 | rc = pfnCallback(pMod, iFunction + pExpDir->Base, NULL, 0, NULL, uValue, fKind, pvUser);
|
---|
1018 | if (rc)
|
---|
1019 | return rc;
|
---|
1020 | }
|
---|
1021 | }
|
---|
1022 |
|
---|
1023 | return 0;
|
---|
1024 | }
|
---|
1025 |
|
---|
1026 |
|
---|
1027 | /** @copydoc kLdrModGetImport */
|
---|
1028 | static int kldrModPEGetImport(PKLDRMOD pMod, const void *pvBits, KU32 iImport, char *pszName, KSIZE cchName)
|
---|
1029 | {
|
---|
1030 | PKLDRMODPE pModPE = (PKLDRMODPE)pMod->pvData;
|
---|
1031 | const IMAGE_IMPORT_DESCRIPTOR *pImpDesc;
|
---|
1032 | const char *pszImportName;
|
---|
1033 | KSIZE cchImportName;
|
---|
1034 | int rc;
|
---|
1035 |
|
---|
1036 | /*
|
---|
1037 | * Make sure we've got mapped bits and resolve any base address aliases.
|
---|
1038 | */
|
---|
1039 | rc = kldrModPEBitsAndBaseAddress(pModPE, &pvBits, NULL);
|
---|
1040 | if (rc)
|
---|
1041 | return rc;
|
---|
1042 |
|
---|
1043 | /*
|
---|
1044 | * Simple bounds check.
|
---|
1045 | */
|
---|
1046 | if (iImport >= (KU32)kldrModPENumberOfImports(pMod, pvBits))
|
---|
1047 | return KLDR_ERR_IMPORT_ORDINAL_OUT_OF_BOUNDS;
|
---|
1048 |
|
---|
1049 | /*
|
---|
1050 | * Get the name.
|
---|
1051 | */
|
---|
1052 | pImpDesc = KLDRMODPE_RVA2TYPE(pvBits,
|
---|
1053 | pModPE->Hdrs.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress
|
---|
1054 | + sizeof(IMAGE_IMPORT_DESCRIPTOR) * iImport,
|
---|
1055 | const IMAGE_IMPORT_DESCRIPTOR *);
|
---|
1056 | pszImportName = KLDRMODPE_RVA2TYPE(pvBits, pImpDesc->Name, const char *);
|
---|
1057 | cchImportName = kHlpStrLen(pszImportName);
|
---|
1058 | if (cchImportName < cchName)
|
---|
1059 | {
|
---|
1060 | kHlpMemCopy(pszName, pszImportName, cchImportName + 1);
|
---|
1061 | rc = 0;
|
---|
1062 | }
|
---|
1063 | else
|
---|
1064 | {
|
---|
1065 | kHlpMemCopy(pszName, pszImportName, cchName);
|
---|
1066 | if (cchName)
|
---|
1067 | pszName[cchName - 1] = '\0';
|
---|
1068 | rc = KERR_BUFFER_OVERFLOW;
|
---|
1069 | }
|
---|
1070 |
|
---|
1071 | return rc;
|
---|
1072 | }
|
---|
1073 |
|
---|
1074 |
|
---|
1075 | /** @copydoc kLdrModNumberOfImports */
|
---|
1076 | static KI32 kldrModPENumberOfImports(PKLDRMOD pMod, const void *pvBits)
|
---|
1077 | {
|
---|
1078 | PKLDRMODPE pModPE = (PKLDRMODPE)pMod->pvData;
|
---|
1079 | if (pModPE->cImportModules == ~(KU32)0)
|
---|
1080 | {
|
---|
1081 | /*
|
---|
1082 | * We'll have to walk the import descriptors to figure out their number.
|
---|
1083 | * First, make sure we've got mapped bits.
|
---|
1084 | */
|
---|
1085 | if (kldrModPEBitsAndBaseAddress(pModPE, &pvBits, NULL))
|
---|
1086 | return -1;
|
---|
1087 | pModPE->cImportModules = 0;
|
---|
1088 | if ( pModPE->Hdrs.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].Size
|
---|
1089 | && pModPE->Hdrs.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress)
|
---|
1090 | {
|
---|
1091 | const IMAGE_IMPORT_DESCRIPTOR *pImpDesc;
|
---|
1092 |
|
---|
1093 | pImpDesc = KLDRMODPE_RVA2TYPE(pvBits,
|
---|
1094 | pModPE->Hdrs.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress,
|
---|
1095 | const IMAGE_IMPORT_DESCRIPTOR *);
|
---|
1096 | while (pImpDesc->Name && pImpDesc->FirstThunk)
|
---|
1097 | {
|
---|
1098 | pModPE->cImportModules++;
|
---|
1099 | pImpDesc++;
|
---|
1100 | }
|
---|
1101 | }
|
---|
1102 | }
|
---|
1103 | return pModPE->cImportModules;
|
---|
1104 | }
|
---|
1105 |
|
---|
1106 |
|
---|
1107 | /** @copydoc kLdrModGetStackInfo */
|
---|
1108 | static int kldrModPEGetStackInfo(PKLDRMOD pMod, const void *pvBits, KLDRADDR BaseAddress, PKLDRSTACKINFO pStackInfo)
|
---|
1109 | {
|
---|
1110 | PKLDRMODPE pModPE = (PKLDRMODPE)pMod->pvData;
|
---|
1111 |
|
---|
1112 | pStackInfo->Address = NIL_KLDRADDR;
|
---|
1113 | pStackInfo->LinkAddress = NIL_KLDRADDR;
|
---|
1114 | pStackInfo->cbStack = pStackInfo->cbStackThread = pModPE->Hdrs.OptionalHeader.SizeOfStackReserve;
|
---|
1115 |
|
---|
1116 | return 0;
|
---|
1117 | }
|
---|
1118 |
|
---|
1119 |
|
---|
1120 | /** @copydoc kLdrModQueryMainEntrypoint */
|
---|
1121 | static int kldrModPEQueryMainEntrypoint(PKLDRMOD pMod, const void *pvBits, KLDRADDR BaseAddress, PKLDRADDR pMainEPAddress)
|
---|
1122 | {
|
---|
1123 | PKLDRMODPE pModPE = (PKLDRMODPE)pMod->pvData;
|
---|
1124 | int rc;
|
---|
1125 |
|
---|
1126 | /*
|
---|
1127 | * Resolve base address alias if any.
|
---|
1128 | */
|
---|
1129 | rc = kldrModPEBitsAndBaseAddress(pModPE, NULL, &BaseAddress);
|
---|
1130 | if (rc)
|
---|
1131 | return rc;
|
---|
1132 |
|
---|
1133 | /*
|
---|
1134 | * Convert the address from the header.
|
---|
1135 | */
|
---|
1136 | *pMainEPAddress = pModPE->Hdrs.OptionalHeader.AddressOfEntryPoint
|
---|
1137 | ? BaseAddress + pModPE->Hdrs.OptionalHeader.AddressOfEntryPoint
|
---|
1138 | : NIL_KLDRADDR;
|
---|
1139 | return 0;
|
---|
1140 | }
|
---|
1141 |
|
---|
1142 |
|
---|
1143 | /** @copydoc kLdrModEnumDbgInfo */
|
---|
1144 | static int kldrModPEEnumDbgInfo(PKLDRMOD pMod, const void *pvBits, PFNKLDRENUMDBG pfnCallback, void *pvUser)
|
---|
1145 | {
|
---|
1146 | PKLDRMODPE pModPE = (PKLDRMODPE)pMod->pvData;
|
---|
1147 | const IMAGE_DEBUG_DIRECTORY *pDbgDir;
|
---|
1148 | KU32 iDbgInfo;
|
---|
1149 | KU32 cb;
|
---|
1150 | int rc;
|
---|
1151 |
|
---|
1152 | /*
|
---|
1153 | * Check that there is a debug directory first.
|
---|
1154 | */
|
---|
1155 | cb = pModPE->Hdrs.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_DEBUG].Size;
|
---|
1156 | if ( cb < sizeof(IMAGE_DEBUG_DIRECTORY) /* screw borland linkers */
|
---|
1157 | || !pModPE->Hdrs.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_DEBUG].VirtualAddress)
|
---|
1158 | return 0;
|
---|
1159 |
|
---|
1160 | /*
|
---|
1161 | * Make sure we've got mapped bits.
|
---|
1162 | */
|
---|
1163 | rc = kldrModPEBitsAndBaseAddress(pModPE, &pvBits, NULL);
|
---|
1164 | if (rc)
|
---|
1165 | return rc;
|
---|
1166 |
|
---|
1167 | /*
|
---|
1168 | * Enumerate the debug directory.
|
---|
1169 | */
|
---|
1170 | pDbgDir = KLDRMODPE_RVA2TYPE(pvBits,
|
---|
1171 | pModPE->Hdrs.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_DEBUG].VirtualAddress,
|
---|
1172 | const IMAGE_DEBUG_DIRECTORY *);
|
---|
1173 | for (iDbgInfo = 0;; iDbgInfo++, pDbgDir++, cb -= sizeof(IMAGE_DEBUG_DIRECTORY))
|
---|
1174 | {
|
---|
1175 | KLDRDBGINFOTYPE enmDbgInfoType;
|
---|
1176 |
|
---|
1177 | /* convert the type. */
|
---|
1178 | switch (pDbgDir->Type)
|
---|
1179 | {
|
---|
1180 | case IMAGE_DEBUG_TYPE_UNKNOWN:
|
---|
1181 | case IMAGE_DEBUG_TYPE_FPO:
|
---|
1182 | case IMAGE_DEBUG_TYPE_COFF: /*stabs dialect??*/
|
---|
1183 | case IMAGE_DEBUG_TYPE_MISC:
|
---|
1184 | case IMAGE_DEBUG_TYPE_EXCEPTION:
|
---|
1185 | case IMAGE_DEBUG_TYPE_FIXUP:
|
---|
1186 | case IMAGE_DEBUG_TYPE_BORLAND:
|
---|
1187 | default:
|
---|
1188 | enmDbgInfoType = KLDRDBGINFOTYPE_UNKNOWN;
|
---|
1189 | break;
|
---|
1190 | case IMAGE_DEBUG_TYPE_CODEVIEW:
|
---|
1191 | enmDbgInfoType = KLDRDBGINFOTYPE_CODEVIEW;
|
---|
1192 | break;
|
---|
1193 | }
|
---|
1194 |
|
---|
1195 | rc = pfnCallback(pMod, iDbgInfo,
|
---|
1196 | enmDbgInfoType, pDbgDir->MajorVersion, pDbgDir->MinorVersion, NULL,
|
---|
1197 | pDbgDir->PointerToRawData ? (KLDRFOFF)pDbgDir->PointerToRawData : -1,
|
---|
1198 | pDbgDir->AddressOfRawData ? pDbgDir->AddressOfRawData : NIL_KLDRADDR,
|
---|
1199 | pDbgDir->SizeOfData,
|
---|
1200 | NULL,
|
---|
1201 | pvUser);
|
---|
1202 | if (rc)
|
---|
1203 | break;
|
---|
1204 |
|
---|
1205 | /* next */
|
---|
1206 | if (cb <= sizeof(IMAGE_DEBUG_DIRECTORY))
|
---|
1207 | break;
|
---|
1208 | }
|
---|
1209 |
|
---|
1210 | return rc;
|
---|
1211 | }
|
---|
1212 |
|
---|
1213 |
|
---|
1214 | /** @copydoc kLdrModHasDbgInfo */
|
---|
1215 | static int kldrModPEHasDbgInfo(PKLDRMOD pMod, const void *pvBits)
|
---|
1216 | {
|
---|
1217 | PKLDRMODPE pModPE = (PKLDRMODPE)pMod->pvData;
|
---|
1218 |
|
---|
1219 | /*
|
---|
1220 | * Base this entirely on the presence of a debug directory.
|
---|
1221 | */
|
---|
1222 | if ( pModPE->Hdrs.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_DEBUG].Size
|
---|
1223 | < sizeof(IMAGE_DEBUG_DIRECTORY) /* screw borland linkers */
|
---|
1224 | || !pModPE->Hdrs.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_DEBUG].VirtualAddress)
|
---|
1225 | return KLDR_ERR_NO_DEBUG_INFO;
|
---|
1226 | return 0;
|
---|
1227 | }
|
---|
1228 |
|
---|
1229 |
|
---|
1230 | /** @copydoc kLdrModMap */
|
---|
1231 | static int kldrModPEMap(PKLDRMOD pMod)
|
---|
1232 | {
|
---|
1233 | PKLDRMODPE pModPE = (PKLDRMODPE)pMod->pvData;
|
---|
1234 | int rc;
|
---|
1235 |
|
---|
1236 | /*
|
---|
1237 | * Already mapped?
|
---|
1238 | */
|
---|
1239 | if (pModPE->pvMapping)
|
---|
1240 | return KLDR_ERR_ALREADY_MAPPED;
|
---|
1241 |
|
---|
1242 | /*
|
---|
1243 | * We've got a common worker which does this.
|
---|
1244 | */
|
---|
1245 | rc = kldrModPEDoMap(pModPE, 1 /* the real thing */);
|
---|
1246 | if (rc)
|
---|
1247 | return rc;
|
---|
1248 | KLDRMODPE_ASSERT(pModPE->pvMapping);
|
---|
1249 | return 0;
|
---|
1250 | }
|
---|
1251 |
|
---|
1252 |
|
---|
1253 | /** @copydoc kLdrModUnmap */
|
---|
1254 | static int kldrModPEUnmap(PKLDRMOD pMod)
|
---|
1255 | {
|
---|
1256 | PKLDRMODPE pModPE = (PKLDRMODPE)pMod->pvData;
|
---|
1257 | int rc;
|
---|
1258 |
|
---|
1259 | /*
|
---|
1260 | * Mapped?
|
---|
1261 | */
|
---|
1262 | if (!pModPE->pvMapping)
|
---|
1263 | return KLDR_ERR_NOT_MAPPED;
|
---|
1264 |
|
---|
1265 | /*
|
---|
1266 | * We've got a common worker which does this.
|
---|
1267 | */
|
---|
1268 | rc = kldrModPEDoUnmap(pModPE, pModPE->pvMapping);
|
---|
1269 | if (rc)
|
---|
1270 | return rc;
|
---|
1271 | KLDRMODPE_ASSERT(!pModPE->pvMapping);
|
---|
1272 | return 0;
|
---|
1273 |
|
---|
1274 | }
|
---|
1275 |
|
---|
1276 |
|
---|
1277 | /** @copydoc kLdrModAllocTLS */
|
---|
1278 | static int kldrModPEAllocTLS(PKLDRMOD pMod)
|
---|
1279 | {
|
---|
1280 | PKLDRMODPE pModPE = (PKLDRMODPE)pMod->pvData;
|
---|
1281 |
|
---|
1282 | /*
|
---|
1283 | * Mapped?
|
---|
1284 | */
|
---|
1285 | if (!pModPE->pvMapping)
|
---|
1286 | return KLDR_ERR_NOT_MAPPED;
|
---|
1287 |
|
---|
1288 | /*
|
---|
1289 | * If no TLS directory then there is nothing to do.
|
---|
1290 | */
|
---|
1291 | if ( !pModPE->Hdrs.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_TLS].Size
|
---|
1292 | || !pModPE->Hdrs.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_TLS].VirtualAddress)
|
---|
1293 | return 0;
|
---|
1294 | /** @todo implement TLS. */
|
---|
1295 | return -1;
|
---|
1296 | }
|
---|
1297 |
|
---|
1298 |
|
---|
1299 | /** @copydoc kLdrModFreeTLS */
|
---|
1300 | static void kldrModPEFreeTLS(PKLDRMOD pMod)
|
---|
1301 | {
|
---|
1302 | PKLDRMODPE pModPE = (PKLDRMODPE)pMod->pvData;
|
---|
1303 |
|
---|
1304 | /*
|
---|
1305 | * Mapped?
|
---|
1306 | */
|
---|
1307 | if (!pModPE->pvMapping)
|
---|
1308 | return;
|
---|
1309 |
|
---|
1310 | /*
|
---|
1311 | * If no TLS directory then there is nothing to do.
|
---|
1312 | */
|
---|
1313 | if ( !pModPE->Hdrs.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_TLS].Size
|
---|
1314 | || !pModPE->Hdrs.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_TLS].VirtualAddress)
|
---|
1315 | return;
|
---|
1316 | /** @todo implement TLS. */
|
---|
1317 | return;
|
---|
1318 | }
|
---|
1319 |
|
---|
1320 |
|
---|
1321 | /** @copydoc kLdrModReload */
|
---|
1322 | static int kldrModPEReload(PKLDRMOD pMod)
|
---|
1323 | {
|
---|
1324 | PKLDRMODPE pModPE = (PKLDRMODPE)pMod->pvData;
|
---|
1325 |
|
---|
1326 | /*
|
---|
1327 | * Mapped?
|
---|
1328 | */
|
---|
1329 | if (!pModPE->pvMapping)
|
---|
1330 | return KLDR_ERR_NOT_MAPPED;
|
---|
1331 |
|
---|
1332 | /* the file provider does it all */
|
---|
1333 | return kRdrRefresh(pMod->pRdr, (void *)pModPE->pvMapping, pMod->cSegments, pMod->aSegments);
|
---|
1334 | }
|
---|
1335 |
|
---|
1336 |
|
---|
1337 | /** @copydoc kLdrModFixupMapping */
|
---|
1338 | static int kldrModPEFixupMapping(PKLDRMOD pMod, PFNKLDRMODGETIMPORT pfnGetImport, void *pvUser)
|
---|
1339 | {
|
---|
1340 | PKLDRMODPE pModPE = (PKLDRMODPE)pMod->pvData;
|
---|
1341 | int rc, rc2;
|
---|
1342 |
|
---|
1343 | /*
|
---|
1344 | * Mapped?
|
---|
1345 | */
|
---|
1346 | if (!pModPE->pvMapping)
|
---|
1347 | return KLDR_ERR_NOT_MAPPED;
|
---|
1348 |
|
---|
1349 | /*
|
---|
1350 | * Before doing anything we'll have to make all pages writable.
|
---|
1351 | */
|
---|
1352 | rc = kRdrProtect(pMod->pRdr, (void *)pModPE->pvMapping, pMod->cSegments, pMod->aSegments, 1 /* unprotect */);
|
---|
1353 | if (rc)
|
---|
1354 | return rc;
|
---|
1355 |
|
---|
1356 | /*
|
---|
1357 | * Apply base relocations.
|
---|
1358 | */
|
---|
1359 | rc = kldrModPEDoFixups(pModPE, (void *)pModPE->pvMapping, (KUPTR)pModPE->pvMapping,
|
---|
1360 | pModPE->Hdrs.OptionalHeader.ImageBase);
|
---|
1361 |
|
---|
1362 | /*
|
---|
1363 | * Resolve imports.
|
---|
1364 | */
|
---|
1365 | if (!rc)
|
---|
1366 | rc = kldrModPEDoImports(pModPE, (void *)pModPE->pvMapping, pfnGetImport, pvUser);
|
---|
1367 |
|
---|
1368 | /*
|
---|
1369 | * Restore protection.
|
---|
1370 | */
|
---|
1371 | rc2 = kRdrProtect(pMod->pRdr, (void *)pModPE->pvMapping, pMod->cSegments, pMod->aSegments, 0 /* protect */);
|
---|
1372 | if (!rc && rc2)
|
---|
1373 | rc = rc2;
|
---|
1374 | return rc;
|
---|
1375 | }
|
---|
1376 |
|
---|
1377 |
|
---|
1378 | /**
|
---|
1379 | * Applies base relocations to a (unprotected) image mapping.
|
---|
1380 | *
|
---|
1381 | * @returns 0 on success, non-zero kLdr status code on failure.
|
---|
1382 | * @param pModPE The PE module interpreter instance.
|
---|
1383 | * @param pvMapping The mapping to fixup.
|
---|
1384 | * @param NewBaseAddress The address to fixup the mapping to.
|
---|
1385 | * @param OldBaseAddress The address the mapping is currently fixed up to.
|
---|
1386 | */
|
---|
1387 | static int kldrModPEDoFixups(PKLDRMODPE pModPE, void *pvMapping, KLDRADDR NewBaseAddress, KLDRADDR OldBaseAddress)
|
---|
1388 | {
|
---|
1389 | const KLDRADDR Delta = NewBaseAddress - OldBaseAddress;
|
---|
1390 | KU32 cbLeft = pModPE->Hdrs.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC].Size;
|
---|
1391 | const IMAGE_BASE_RELOCATION *pBR, *pFirstBR;
|
---|
1392 |
|
---|
1393 | /*
|
---|
1394 | * Don't don anything if the delta is 0 or there aren't any relocations.
|
---|
1395 | */
|
---|
1396 | if ( !Delta
|
---|
1397 | || !cbLeft
|
---|
1398 | || !pModPE->Hdrs.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC].VirtualAddress)
|
---|
1399 | return 0;
|
---|
1400 |
|
---|
1401 | /*
|
---|
1402 | * Process the fixups block by block.
|
---|
1403 | * (These blocks appears to be 4KB on all archs despite the native page size.)
|
---|
1404 | */
|
---|
1405 | pBR = pFirstBR = KLDRMODPE_RVA2TYPE(pvMapping,
|
---|
1406 | pModPE->Hdrs.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC].VirtualAddress,
|
---|
1407 | const IMAGE_BASE_RELOCATION *);
|
---|
1408 | while ( cbLeft > sizeof(IMAGE_BASE_RELOCATION)
|
---|
1409 | && pBR->SizeOfBlock >= sizeof(IMAGE_BASE_RELOCATION) /* paranoia */)
|
---|
1410 | {
|
---|
1411 | union
|
---|
1412 | {
|
---|
1413 | KU8 *pu8;
|
---|
1414 | KU16 *pu16;
|
---|
1415 | KU32 *pu32;
|
---|
1416 | KU64 *pu64;
|
---|
1417 | } uChunk,
|
---|
1418 | u;
|
---|
1419 | const KU16 *poffFixup = (const KU16 *)(pBR + 1);
|
---|
1420 | const KU32 cbBlock = K_MIN(cbLeft, pBR->SizeOfBlock) - sizeof(IMAGE_BASE_RELOCATION); /* more caution... */
|
---|
1421 | KU32 cFixups = cbBlock / sizeof(poffFixup[0]);
|
---|
1422 | uChunk.pu8 = KLDRMODPE_RVA2TYPE(pvMapping, pBR->VirtualAddress, KU8 *);
|
---|
1423 |
|
---|
1424 | /*
|
---|
1425 | * Loop thru the fixups in this chunk.
|
---|
1426 | */
|
---|
1427 | while (cFixups > 0)
|
---|
1428 | {
|
---|
1429 | u.pu8 = uChunk.pu8 + (*poffFixup & 0xfff);
|
---|
1430 | switch (*poffFixup >> 12) /* ordered by value. */
|
---|
1431 | {
|
---|
1432 | /* 0 - Alignment placeholder. */
|
---|
1433 | case IMAGE_REL_BASED_ABSOLUTE:
|
---|
1434 | break;
|
---|
1435 |
|
---|
1436 | /* 1 - 16-bit, add 2nd 16-bit part of the delta. (rare) */
|
---|
1437 | case IMAGE_REL_BASED_HIGH:
|
---|
1438 | *u.pu16 += (KU16)(Delta >> 16);
|
---|
1439 | break;
|
---|
1440 |
|
---|
1441 | /* 2 - 16-bit, add 1st 16-bit part of the delta. (rare) */
|
---|
1442 | case IMAGE_REL_BASED_LOW:
|
---|
1443 | *u.pu16 += (KU16)Delta;
|
---|
1444 | break;
|
---|
1445 |
|
---|
1446 | /* 3 - 32-bit, add delta. (frequent in 32-bit images) */
|
---|
1447 | case IMAGE_REL_BASED_HIGHLOW:
|
---|
1448 | *u.pu32 += (KU32)Delta;
|
---|
1449 | break;
|
---|
1450 |
|
---|
1451 | /* 4 - 16-bit, add 2nd 16-bit of the delta, sign adjust for the lower 16-bit. one arg. (rare) */
|
---|
1452 | case IMAGE_REL_BASED_HIGHADJ:
|
---|
1453 | {
|
---|
1454 | KI32 i32;
|
---|
1455 | if (cFixups <= 1)
|
---|
1456 | return KLDR_ERR_PE_BAD_FIXUP;
|
---|
1457 |
|
---|
1458 | i32 = (KU32)*u.pu16 << 16;
|
---|
1459 | i32 |= *++poffFixup; cFixups--; /* the addend argument */
|
---|
1460 | i32 += (KU32)Delta;
|
---|
1461 | i32 += 0x8000;
|
---|
1462 | *u.pu16 = (KU16)(i32 >> 16);
|
---|
1463 | break;
|
---|
1464 | }
|
---|
1465 |
|
---|
1466 | /* 5 - 32-bit MIPS JMPADDR, no implemented. */
|
---|
1467 | case IMAGE_REL_BASED_MIPS_JMPADDR:
|
---|
1468 | *u.pu32 = (*u.pu32 & 0xc0000000)
|
---|
1469 | | ((KU32)((*u.pu32 << 2) + (KU32)Delta) >> 2);
|
---|
1470 | break;
|
---|
1471 |
|
---|
1472 | /* 6 - Intra section? Reserved value in later specs. Not implemented. */
|
---|
1473 | case IMAGE_REL_BASED_SECTION:
|
---|
1474 | KLDRMODPE_ASSERT(!"SECTION");
|
---|
1475 | return KLDR_ERR_PE_BAD_FIXUP;
|
---|
1476 |
|
---|
1477 | /* 7 - Relative intra section? Reserved value in later specs. Not implemented. */
|
---|
1478 | case IMAGE_REL_BASED_REL32:
|
---|
1479 | KLDRMODPE_ASSERT(!"SECTION");
|
---|
1480 | return KLDR_ERR_PE_BAD_FIXUP;
|
---|
1481 |
|
---|
1482 | /* 8 - reserved according to binutils... */
|
---|
1483 | case 8:
|
---|
1484 | KLDRMODPE_ASSERT(!"RESERVERED8");
|
---|
1485 | return KLDR_ERR_PE_BAD_FIXUP;
|
---|
1486 |
|
---|
1487 | /* 9 - IA64_IMM64 (/ MIPS_JMPADDR16), no specs nor need to support the platform yet.
|
---|
1488 | * Bet this requires more code than all the other fixups put together in good IA64 spirit :-) */
|
---|
1489 | case IMAGE_REL_BASED_IA64_IMM64:
|
---|
1490 | KLDRMODPE_ASSERT(!"IA64_IMM64 / MIPS_JMPADDR16");
|
---|
1491 | return KLDR_ERR_PE_BAD_FIXUP;
|
---|
1492 |
|
---|
1493 | /* 10 - 64-bit, add delta. (frequently in 64-bit images) */
|
---|
1494 | case IMAGE_REL_BASED_DIR64:
|
---|
1495 | *u.pu64 += (KU64)Delta;
|
---|
1496 | break;
|
---|
1497 |
|
---|
1498 | /* 11 - 16-bit, add 3rd 16-bit of the delta, sign adjust for the lower 32-bit. two args. (rare) */
|
---|
1499 | case IMAGE_REL_BASED_HIGH3ADJ:
|
---|
1500 | {
|
---|
1501 | KI64 i64;
|
---|
1502 | if (cFixups <= 2)
|
---|
1503 | return KLDR_ERR_PE_BAD_FIXUP;
|
---|
1504 |
|
---|
1505 | i64 = (KU64)*u.pu16 << 32
|
---|
1506 | | ((KU32)poffFixup[2] << 16)
|
---|
1507 | | poffFixup[1];
|
---|
1508 | i64 += Delta;
|
---|
1509 | i64 += 0x80008000UL;
|
---|
1510 | *u.pu16 = (KU16)(i64 >> 32);
|
---|
1511 | /* skip the addends arguments */
|
---|
1512 | poffFixup += 2;
|
---|
1513 | cFixups -= 2;
|
---|
1514 | break;
|
---|
1515 | }
|
---|
1516 |
|
---|
1517 | /* the rest are yet to be defined.*/
|
---|
1518 | default:
|
---|
1519 | return KLDR_ERR_PE_BAD_FIXUP;
|
---|
1520 | }
|
---|
1521 |
|
---|
1522 | /*
|
---|
1523 | * Next relocation.
|
---|
1524 | */
|
---|
1525 | poffFixup++;
|
---|
1526 | cFixups--;
|
---|
1527 | }
|
---|
1528 |
|
---|
1529 |
|
---|
1530 | /*
|
---|
1531 | * Next block.
|
---|
1532 | */
|
---|
1533 | cbLeft -= pBR->SizeOfBlock;
|
---|
1534 | pBR = (PIMAGE_BASE_RELOCATION)((KUPTR)pBR + pBR->SizeOfBlock);
|
---|
1535 | }
|
---|
1536 |
|
---|
1537 | return 0;
|
---|
1538 | }
|
---|
1539 |
|
---|
1540 |
|
---|
1541 |
|
---|
1542 | /**
|
---|
1543 | * Resolves imports.
|
---|
1544 | *
|
---|
1545 | * @returns 0 on success, non-zero kLdr status code on failure.
|
---|
1546 | * @param pModPE The PE module interpreter instance.
|
---|
1547 | * @param pvMapping The mapping which imports should be resolved.
|
---|
1548 | * @param pfnGetImport The callback for resolving an imported symbol.
|
---|
1549 | * @param pvUser User argument to the callback.
|
---|
1550 | */
|
---|
1551 | static int kldrModPEDoImports(PKLDRMODPE pModPE, void *pvMapping, PFNKLDRMODGETIMPORT pfnGetImport, void *pvUser)
|
---|
1552 | {
|
---|
1553 | const IMAGE_IMPORT_DESCRIPTOR *pImpDesc;
|
---|
1554 |
|
---|
1555 | /*
|
---|
1556 | * If no imports, there is nothing to do.
|
---|
1557 | */
|
---|
1558 | kldrModPENumberOfImports(pModPE->pMod, pvMapping);
|
---|
1559 | if (!pModPE->cImportModules)
|
---|
1560 | return 0;
|
---|
1561 |
|
---|
1562 | pImpDesc = KLDRMODPE_RVA2TYPE(pvMapping,
|
---|
1563 | pModPE->Hdrs.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress,
|
---|
1564 | const IMAGE_IMPORT_DESCRIPTOR *);
|
---|
1565 | if (pModPE->Hdrs.FileHeader.SizeOfOptionalHeader == sizeof(IMAGE_OPTIONAL_HEADER32))
|
---|
1566 | return kldrModPEDoImports32Bit(pModPE, pvMapping, pImpDesc, pfnGetImport, pvUser);
|
---|
1567 | return kldrModPEDoImports64Bit(pModPE, pvMapping, pImpDesc, pfnGetImport, pvUser);
|
---|
1568 | }
|
---|
1569 |
|
---|
1570 |
|
---|
1571 | /**
|
---|
1572 | * Resolves imports, 32-bit image.
|
---|
1573 | *
|
---|
1574 | * @returns 0 on success, non-zero kLdr status code on failure.
|
---|
1575 | * @param pModPE The PE module interpreter instance.
|
---|
1576 | * @param pvMapping The mapping which imports should be resolved.
|
---|
1577 | * @param pImpDesc Pointer to the first import descriptor.
|
---|
1578 | * @param pfnGetImport The callback for resolving an imported symbol.
|
---|
1579 | * @param pvUser User argument to the callback.
|
---|
1580 | */
|
---|
1581 | static int kldrModPEDoImports32Bit(PKLDRMODPE pModPE, void *pvMapping, const IMAGE_IMPORT_DESCRIPTOR *pImpDesc,
|
---|
1582 | PFNKLDRMODGETIMPORT pfnGetImport, void *pvUser)
|
---|
1583 | {
|
---|
1584 | PKLDRMOD pMod = pModPE->pMod;
|
---|
1585 | KU32 iImp;
|
---|
1586 |
|
---|
1587 | /*
|
---|
1588 | * Iterate the import descriptors.
|
---|
1589 | */
|
---|
1590 | for (iImp = 0; iImp < pModPE->cImportModules; iImp++, pImpDesc++)
|
---|
1591 | {
|
---|
1592 | PIMAGE_THUNK_DATA32 pFirstThunk = KLDRMODPE_RVA2TYPE(pvMapping, pImpDesc->FirstThunk, PIMAGE_THUNK_DATA32);
|
---|
1593 | const IMAGE_THUNK_DATA32 *pThunk = pImpDesc->u.OriginalFirstThunk
|
---|
1594 | ? KLDRMODPE_RVA2TYPE(pvMapping, pImpDesc->u.OriginalFirstThunk, const IMAGE_THUNK_DATA32 *)
|
---|
1595 | : KLDRMODPE_RVA2TYPE(pvMapping, pImpDesc->FirstThunk, const IMAGE_THUNK_DATA32 *);
|
---|
1596 |
|
---|
1597 | /* Iterate the thunks. */
|
---|
1598 | while (pThunk->u1.Ordinal != 0)
|
---|
1599 | {
|
---|
1600 | KLDRADDR Value;
|
---|
1601 | KU32 fKind = KLDRSYMKIND_REQ_FLAT;
|
---|
1602 | int rc;
|
---|
1603 |
|
---|
1604 | /* Ordinal or name import? */
|
---|
1605 | if (IMAGE_SNAP_BY_ORDINAL32(pThunk->u1.Ordinal))
|
---|
1606 | rc = pfnGetImport(pMod, iImp, IMAGE_ORDINAL32(pThunk->u1.Ordinal), NULL, 0, NULL, &Value, &fKind, pvUser);
|
---|
1607 | else if (KLDRMODPE_VALID_RVA(pModPE, pThunk->u1.Ordinal))
|
---|
1608 | {
|
---|
1609 | const IMAGE_IMPORT_BY_NAME *pName = KLDRMODPE_RVA2TYPE(pvMapping, pThunk->u1.Ordinal, const IMAGE_IMPORT_BY_NAME *);
|
---|
1610 | rc = pfnGetImport(pMod, iImp, NIL_KLDRMOD_SYM_ORDINAL, (const char *)pName->Name,
|
---|
1611 | kHlpStrLen((const char *)pName->Name), NULL, &Value, &fKind, pvUser);
|
---|
1612 | }
|
---|
1613 | else
|
---|
1614 | {
|
---|
1615 | KLDRMODPE_ASSERT(!"bad 32-bit import");
|
---|
1616 | return KLDR_ERR_PE_BAD_IMPORT;
|
---|
1617 | }
|
---|
1618 | if (rc)
|
---|
1619 | return rc;
|
---|
1620 |
|
---|
1621 | /* Apply it. */
|
---|
1622 | pFirstThunk->u1.Function = (KU32)Value;
|
---|
1623 | if (pFirstThunk->u1.Function != Value)
|
---|
1624 | {
|
---|
1625 | KLDRMODPE_ASSERT(!"overflow");
|
---|
1626 | return KLDR_ERR_ADDRESS_OVERFLOW;
|
---|
1627 | }
|
---|
1628 |
|
---|
1629 | /* next */
|
---|
1630 | pThunk++;
|
---|
1631 | pFirstThunk++;
|
---|
1632 | }
|
---|
1633 | }
|
---|
1634 |
|
---|
1635 | return 0;
|
---|
1636 | }
|
---|
1637 |
|
---|
1638 |
|
---|
1639 | /**
|
---|
1640 | * Resolves imports, 64-bit image.
|
---|
1641 | *
|
---|
1642 | * @returns 0 on success, non-zero kLdr status code on failure.
|
---|
1643 | * @param pModPE The PE module interpreter instance.
|
---|
1644 | * @param pvMapping The mapping which imports should be resolved.
|
---|
1645 | * @param pImpDesc Pointer to the first import descriptor.
|
---|
1646 | * @param pfnGetImport The callback for resolving an imported symbol.
|
---|
1647 | * @param pvUser User argument to the callback.
|
---|
1648 | */
|
---|
1649 | static int kldrModPEDoImports64Bit(PKLDRMODPE pModPE, void *pvMapping, const IMAGE_IMPORT_DESCRIPTOR *pImpDesc,
|
---|
1650 | PFNKLDRMODGETIMPORT pfnGetImport, void *pvUser)
|
---|
1651 | {
|
---|
1652 | PKLDRMOD pMod = pModPE->pMod;
|
---|
1653 | KU32 iImp;
|
---|
1654 |
|
---|
1655 | /*
|
---|
1656 | * Iterate the import descriptors.
|
---|
1657 | */
|
---|
1658 | for (iImp = 0; iImp < pModPE->cImportModules; iImp++, pImpDesc++)
|
---|
1659 | {
|
---|
1660 | PIMAGE_THUNK_DATA64 pFirstThunk = KLDRMODPE_RVA2TYPE(pvMapping, pImpDesc->FirstThunk, PIMAGE_THUNK_DATA64);
|
---|
1661 | const IMAGE_THUNK_DATA64 *pThunk = pImpDesc->u.OriginalFirstThunk
|
---|
1662 | ? KLDRMODPE_RVA2TYPE(pvMapping, pImpDesc->u.OriginalFirstThunk, const IMAGE_THUNK_DATA64 *)
|
---|
1663 | : KLDRMODPE_RVA2TYPE(pvMapping, pImpDesc->FirstThunk, const IMAGE_THUNK_DATA64 *);
|
---|
1664 |
|
---|
1665 | /* Iterate the thunks. */
|
---|
1666 | while (pThunk->u1.Ordinal != 0)
|
---|
1667 | {
|
---|
1668 | KLDRADDR Value;
|
---|
1669 | KU32 fKind = KLDRSYMKIND_REQ_FLAT;
|
---|
1670 | int rc;
|
---|
1671 |
|
---|
1672 | /* Ordinal or name import? */
|
---|
1673 | if (IMAGE_SNAP_BY_ORDINAL64(pThunk->u1.Ordinal))
|
---|
1674 | rc = pfnGetImport(pMod, iImp, (KU32)IMAGE_ORDINAL64(pThunk->u1.Ordinal), NULL, 0, NULL, &Value, &fKind, pvUser);
|
---|
1675 | else if (KLDRMODPE_VALID_RVA(pModPE, pThunk->u1.Ordinal))
|
---|
1676 | {
|
---|
1677 | const IMAGE_IMPORT_BY_NAME *pName = KLDRMODPE_RVA2TYPE(pvMapping, pThunk->u1.Ordinal, const IMAGE_IMPORT_BY_NAME *);
|
---|
1678 | rc = pfnGetImport(pMod, iImp, NIL_KLDRMOD_SYM_ORDINAL, (const char *)pName->Name,
|
---|
1679 | kHlpStrLen((const char *)pName->Name), NULL, &Value, &fKind, pvUser);
|
---|
1680 | }
|
---|
1681 | else
|
---|
1682 | {
|
---|
1683 | KLDRMODPE_ASSERT(!"bad 64-bit import");
|
---|
1684 | return KLDR_ERR_PE_BAD_IMPORT;
|
---|
1685 | }
|
---|
1686 | if (rc)
|
---|
1687 | return rc;
|
---|
1688 |
|
---|
1689 | /* Apply it. */
|
---|
1690 | pFirstThunk->u1.Function = Value;
|
---|
1691 |
|
---|
1692 | /* next */
|
---|
1693 | pThunk++;
|
---|
1694 | pFirstThunk++;
|
---|
1695 | }
|
---|
1696 | }
|
---|
1697 |
|
---|
1698 | return 0;
|
---|
1699 | }
|
---|
1700 |
|
---|
1701 |
|
---|
1702 |
|
---|
1703 | /** @copydoc kLdrModCallInit */
|
---|
1704 | static int kldrModPECallInit(PKLDRMOD pMod, KUPTR uHandle)
|
---|
1705 | {
|
---|
1706 | PKLDRMODPE pModPE = (PKLDRMODPE)pMod->pvData;
|
---|
1707 | int rc;
|
---|
1708 |
|
---|
1709 | /*
|
---|
1710 | * Mapped?
|
---|
1711 | */
|
---|
1712 | if (!pModPE->pvMapping)
|
---|
1713 | return KLDR_ERR_NOT_MAPPED;
|
---|
1714 |
|
---|
1715 | /*
|
---|
1716 | * Do TLS callbacks first and then call the init/term function if it's a DLL.
|
---|
1717 | */
|
---|
1718 | rc = kldrModPEDoCallTLS(pModPE, DLL_PROCESS_ATTACH, uHandle);
|
---|
1719 | if ( !rc
|
---|
1720 | && (pModPE->Hdrs.FileHeader.Characteristics & IMAGE_FILE_DLL))
|
---|
1721 | {
|
---|
1722 | rc = kldrModPEDoCallDLL(pModPE, DLL_PROCESS_ATTACH, uHandle);
|
---|
1723 | if (rc)
|
---|
1724 | kldrModPEDoCallTLS(pModPE, DLL_PROCESS_DETACH, uHandle);
|
---|
1725 | }
|
---|
1726 |
|
---|
1727 | return rc;
|
---|
1728 | }
|
---|
1729 |
|
---|
1730 |
|
---|
1731 | /**
|
---|
1732 | * Call the DLL entrypoint.
|
---|
1733 | *
|
---|
1734 | * @returns 0 on success.
|
---|
1735 | * @returns KLDR_ERR_MODULE_INIT_FAILED or KLDR_ERR_THREAD_ATTACH_FAILED on failure.
|
---|
1736 | * @param pModPE The PE module interpreter instance.
|
---|
1737 | * @param uOp The operation (DLL_*).
|
---|
1738 | * @param uHandle The module handle to present.
|
---|
1739 | */
|
---|
1740 | static int kldrModPEDoCallDLL(PKLDRMODPE pModPE, unsigned uOp, KUPTR uHandle)
|
---|
1741 | {
|
---|
1742 | int rc;
|
---|
1743 |
|
---|
1744 | /*
|
---|
1745 | * If no entrypoint there isn't anything to be done.
|
---|
1746 | */
|
---|
1747 | if (!pModPE->Hdrs.OptionalHeader.AddressOfEntryPoint)
|
---|
1748 | return 0;
|
---|
1749 |
|
---|
1750 | /*
|
---|
1751 | * Invoke the entrypoint and convert the boolean result to a kLdr status code.
|
---|
1752 | */
|
---|
1753 | rc = kldrModPEDoCall((KUPTR)pModPE->pvMapping + pModPE->Hdrs.OptionalHeader.AddressOfEntryPoint,
|
---|
1754 | uHandle, uOp, NULL);
|
---|
1755 | if (rc)
|
---|
1756 | rc = 0;
|
---|
1757 | else if (uOp == DLL_PROCESS_ATTACH)
|
---|
1758 | rc = KLDR_ERR_MODULE_INIT_FAILED;
|
---|
1759 | else if (uOp == DLL_THREAD_ATTACH)
|
---|
1760 | rc = KLDR_ERR_THREAD_ATTACH_FAILED;
|
---|
1761 | else /* detach: ignore failures */
|
---|
1762 | rc = 0;
|
---|
1763 | return rc;
|
---|
1764 | }
|
---|
1765 |
|
---|
1766 |
|
---|
1767 | /**
|
---|
1768 | * Call the TLS entrypoints.
|
---|
1769 | *
|
---|
1770 | * @returns 0 on success.
|
---|
1771 | * @returns KLDR_ERR_THREAD_ATTACH_FAILED on failure.
|
---|
1772 | * @param pModPE The PE module interpreter instance.
|
---|
1773 | * @param uOp The operation (DLL_*).
|
---|
1774 | * @param uHandle The module handle to present.
|
---|
1775 | */
|
---|
1776 | static int kldrModPEDoCallTLS(PKLDRMODPE pModPE, unsigned uOp, KUPTR uHandle)
|
---|
1777 | {
|
---|
1778 | /** @todo implement TLS support. */
|
---|
1779 | return 0;
|
---|
1780 | }
|
---|
1781 |
|
---|
1782 |
|
---|
1783 | /**
|
---|
1784 | * Do a 3 parameter callback.
|
---|
1785 | *
|
---|
1786 | * @returns 32-bit callback return.
|
---|
1787 | * @param uEntrypoint The address of the function to be called.
|
---|
1788 | * @param uHandle The first argument, the module handle.
|
---|
1789 | * @param uOp The second argumnet, the reason we're calling.
|
---|
1790 | * @param pvReserved The third argument, reserved argument. (figure this one out)
|
---|
1791 | */
|
---|
1792 | static KI32 kldrModPEDoCall(KUPTR uEntrypoint, KUPTR uHandle, KU32 uOp, void *pvReserved)
|
---|
1793 | {
|
---|
1794 | KI32 rc;
|
---|
1795 |
|
---|
1796 | /** @todo try/except */
|
---|
1797 | #if defined(__X86__) || defined(__i386__) || defined(_M_IX86)
|
---|
1798 | /*
|
---|
1799 | * Be very careful.
|
---|
1800 | * Not everyone will have got the calling convention right.
|
---|
1801 | */
|
---|
1802 | # ifdef __GNUC__
|
---|
1803 | __asm__ __volatile__(
|
---|
1804 | "pushl %2\n\t"
|
---|
1805 | "pushl %1\n\t"
|
---|
1806 | "pushl %0\n\t"
|
---|
1807 | "lea 12(%%esp), %2\n\t"
|
---|
1808 | "call *%3\n\t"
|
---|
1809 | "movl %2, %%esp\n\t"
|
---|
1810 | : "=a" (rc)
|
---|
1811 | : "d" (uOp),
|
---|
1812 | "S" (0),
|
---|
1813 | "c" (uEntrypoint),
|
---|
1814 | "0" (uHandle));
|
---|
1815 | # elif defined(_MSC_VER)
|
---|
1816 | __asm {
|
---|
1817 | mov eax, [uHandle]
|
---|
1818 | mov edx, [uOp]
|
---|
1819 | mov ecx, 0
|
---|
1820 | mov ebx, [uEntrypoint]
|
---|
1821 | push edi
|
---|
1822 | mov edi, esp
|
---|
1823 | push ecx
|
---|
1824 | push edx
|
---|
1825 | push eax
|
---|
1826 | call ebx
|
---|
1827 | mov esp, edi
|
---|
1828 | pop edi
|
---|
1829 | mov [rc], eax
|
---|
1830 | }
|
---|
1831 | # else
|
---|
1832 | # error "port me!"
|
---|
1833 | # endif
|
---|
1834 |
|
---|
1835 | #elif defined(__AMD64__) || defined(__x86_64__) || defined(_M_IX86)
|
---|
1836 | /*
|
---|
1837 | * For now, let's just get the work done...
|
---|
1838 | */
|
---|
1839 | /** @todo Deal with GCC / MSC differences in some sensible way. */
|
---|
1840 | int (*pfn)(KUPTR uHandle, KU32 uOp, void *pvReserved);
|
---|
1841 | pfn = (int (*)(KUPTR uHandle, KU32 uOp, void *pvReserved))uEntrypoint;
|
---|
1842 | rc = pfn(uHandle, uOp, NULL);
|
---|
1843 |
|
---|
1844 | #else
|
---|
1845 | # error "port me"
|
---|
1846 | #endif
|
---|
1847 |
|
---|
1848 | return rc;
|
---|
1849 | }
|
---|
1850 |
|
---|
1851 |
|
---|
1852 | /** @copydoc kLdrModCallTerm */
|
---|
1853 | static int kldrModPECallTerm(PKLDRMOD pMod, KUPTR uHandle)
|
---|
1854 | {
|
---|
1855 | PKLDRMODPE pModPE = (PKLDRMODPE)pMod->pvData;
|
---|
1856 |
|
---|
1857 | /*
|
---|
1858 | * Mapped?
|
---|
1859 | */
|
---|
1860 | if (!pModPE->pvMapping)
|
---|
1861 | return KLDR_ERR_NOT_MAPPED;
|
---|
1862 |
|
---|
1863 | /*
|
---|
1864 | * Do TLS callbacks first.
|
---|
1865 | */
|
---|
1866 | kldrModPEDoCallTLS(pModPE, DLL_PROCESS_DETACH, uHandle);
|
---|
1867 | if (pModPE->Hdrs.FileHeader.Characteristics & IMAGE_FILE_DLL)
|
---|
1868 | kldrModPEDoCallDLL(pModPE, DLL_PROCESS_DETACH, uHandle);
|
---|
1869 |
|
---|
1870 | return 0;
|
---|
1871 | }
|
---|
1872 |
|
---|
1873 |
|
---|
1874 | /** @copydoc kLdrModCallThread */
|
---|
1875 | static int kldrModPECallThread(PKLDRMOD pMod, KUPTR uHandle, unsigned fAttachingOrDetaching)
|
---|
1876 | {
|
---|
1877 | PKLDRMODPE pModPE = (PKLDRMODPE)pMod->pvData;
|
---|
1878 | unsigned uOp = fAttachingOrDetaching ? DLL_THREAD_ATTACH : DLL_THREAD_DETACH;
|
---|
1879 | int rc;
|
---|
1880 |
|
---|
1881 | /*
|
---|
1882 | * Do TLS callbacks first and then call the init/term function if it's a DLL.
|
---|
1883 | */
|
---|
1884 | rc = kldrModPEDoCallTLS(pModPE, uOp, uHandle);
|
---|
1885 | if (!fAttachingOrDetaching)
|
---|
1886 | rc = 0;
|
---|
1887 | if ( !rc
|
---|
1888 | && (pModPE->Hdrs.FileHeader.Characteristics & IMAGE_FILE_DLL))
|
---|
1889 | {
|
---|
1890 | rc = kldrModPEDoCallDLL(pModPE, uOp, uHandle);
|
---|
1891 | if (!fAttachingOrDetaching)
|
---|
1892 | rc = 0;
|
---|
1893 | if (rc)
|
---|
1894 | kldrModPEDoCallTLS(pModPE, uOp, uHandle);
|
---|
1895 | }
|
---|
1896 |
|
---|
1897 | return rc;
|
---|
1898 | }
|
---|
1899 |
|
---|
1900 |
|
---|
1901 | /** @copydoc kLdrModSize */
|
---|
1902 | static KLDRADDR kldrModPESize(PKLDRMOD pMod)
|
---|
1903 | {
|
---|
1904 | PKLDRMODPE pModPE = (PKLDRMODPE)pMod->pvData;
|
---|
1905 | return pModPE->Hdrs.OptionalHeader.SizeOfImage;
|
---|
1906 | }
|
---|
1907 |
|
---|
1908 |
|
---|
1909 | /** @copydoc kLdrModGetBits */
|
---|
1910 | static int kldrModPEGetBits(PKLDRMOD pMod, void *pvBits, KLDRADDR BaseAddress, PFNKLDRMODGETIMPORT pfnGetImport, void *pvUser)
|
---|
1911 | {
|
---|
1912 | PKLDRMODPE pModPE = (PKLDRMODPE)pMod->pvData;
|
---|
1913 | KU32 i;
|
---|
1914 | int rc;
|
---|
1915 |
|
---|
1916 | /*
|
---|
1917 | * Zero the entire buffer first to simplify things.
|
---|
1918 | */
|
---|
1919 | kHlpMemSet(pvBits, 0, pModPE->Hdrs.OptionalHeader.SizeOfImage);
|
---|
1920 |
|
---|
1921 | /*
|
---|
1922 | * Iterate the segments and read the data within them.
|
---|
1923 | */
|
---|
1924 | for (i = 0; i < pMod->cSegments; i++)
|
---|
1925 | {
|
---|
1926 | /* skip it? */
|
---|
1927 | if ( pMod->aSegments[i].cbFile == -1
|
---|
1928 | || pMod->aSegments[i].offFile == -1
|
---|
1929 | || pMod->aSegments[i].LinkAddress == NIL_KLDRADDR
|
---|
1930 | || !pMod->aSegments[i].Alignment)
|
---|
1931 | continue;
|
---|
1932 | rc = kRdrRead(pMod->pRdr,
|
---|
1933 | (KU8 *)pvBits + (pMod->aSegments[i].LinkAddress - pModPE->Hdrs.OptionalHeader.ImageBase),
|
---|
1934 | pMod->aSegments[i].cbFile,
|
---|
1935 | pMod->aSegments[i].offFile);
|
---|
1936 | if (rc)
|
---|
1937 | return rc;
|
---|
1938 | }
|
---|
1939 |
|
---|
1940 | /*
|
---|
1941 | * Perform relocations.
|
---|
1942 | */
|
---|
1943 | return kldrModPERelocateBits(pMod, pvBits, BaseAddress, pModPE->Hdrs.OptionalHeader.ImageBase, pfnGetImport, pvUser);
|
---|
1944 |
|
---|
1945 | }
|
---|
1946 |
|
---|
1947 |
|
---|
1948 | /** @copydoc kLdrModRelocateBits */
|
---|
1949 | static int kldrModPERelocateBits(PKLDRMOD pMod, void *pvBits, KLDRADDR NewBaseAddress, KLDRADDR OldBaseAddress,
|
---|
1950 | PFNKLDRMODGETIMPORT pfnGetImport, void *pvUser)
|
---|
1951 | {
|
---|
1952 | PKLDRMODPE pModPE = (PKLDRMODPE)pMod->pvData;
|
---|
1953 | int rc;
|
---|
1954 |
|
---|
1955 | /*
|
---|
1956 | * Call workers to do the jobs.
|
---|
1957 | */
|
---|
1958 | rc = kldrModPEDoFixups(pModPE, pvBits, NewBaseAddress, OldBaseAddress);
|
---|
1959 | if (!rc)
|
---|
1960 | rc = kldrModPEDoImports(pModPE, pvBits, pfnGetImport, pvUser);
|
---|
1961 |
|
---|
1962 | return rc;
|
---|
1963 | }
|
---|
1964 |
|
---|
1965 |
|
---|
1966 | /**
|
---|
1967 | * The PE module interpreter method table.
|
---|
1968 | */
|
---|
1969 | KLDRMODOPS g_kLdrModPEOps =
|
---|
1970 | {
|
---|
1971 | "PE",
|
---|
1972 | NULL,
|
---|
1973 | kldrModPECreate,
|
---|
1974 | kldrModPEDestroy,
|
---|
1975 | kldrModPEQuerySymbol,
|
---|
1976 | kldrModPEEnumSymbols,
|
---|
1977 | kldrModPEGetImport,
|
---|
1978 | kldrModPENumberOfImports,
|
---|
1979 | NULL /* can execute one is optional */,
|
---|
1980 | kldrModPEGetStackInfo,
|
---|
1981 | kldrModPEQueryMainEntrypoint,
|
---|
1982 | NULL /* pfnQueryImageUuid */,
|
---|
1983 | NULL, /** @todo resources */
|
---|
1984 | NULL, /** @todo resources */
|
---|
1985 | kldrModPEEnumDbgInfo,
|
---|
1986 | kldrModPEHasDbgInfo,
|
---|
1987 | kldrModPEMap,
|
---|
1988 | kldrModPEUnmap,
|
---|
1989 | kldrModPEAllocTLS,
|
---|
1990 | kldrModPEFreeTLS,
|
---|
1991 | kldrModPEReload,
|
---|
1992 | kldrModPEFixupMapping,
|
---|
1993 | kldrModPECallInit,
|
---|
1994 | kldrModPECallTerm,
|
---|
1995 | kldrModPECallThread,
|
---|
1996 | kldrModPESize,
|
---|
1997 | kldrModPEGetBits,
|
---|
1998 | kldrModPERelocateBits,
|
---|
1999 | NULL, /** @todo mostly done */
|
---|
2000 | 42 /* the end */
|
---|
2001 | };
|
---|