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