VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/ldr/ldrEx.cpp@ 73150

Last change on this file since 73150 was 73150, checked in by vboxsync, 6 years ago

VMM,DBGC,IPRT: In memory

  • VMM: Morphed part of the NT kernel digger into DBGFR3ModInMem.
  • DBGC: Added 'loadinmem' command for accessing the DBGFR3ModInMem functionality.
  • IPRT: Modified RTDbgModCreateFromPeImage to clearly indicate to caller whether the loader module was consumed or not (missing direct ref counting).
  • IPRT: Added RTLdrGetHostArch for resolving RTLDRARCH_HOST.
  • IPRT: Added RTLdrArchName for naming a RTLDRARCH value.
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 26.7 KB
Line 
1/* $Id: ldrEx.cpp 73150 2018-07-16 10:03:41Z vboxsync $ */
2/** @file
3 * IPRT - Binary Image Loader, Extended Features.
4 */
5
6/*
7 * Copyright (C) 2006-2017 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*********************************************************************************************************************************
29* Header Files *
30*********************************************************************************************************************************/
31#define LOG_GROUP RTLOGGROUP_LDR
32#include <iprt/ldr.h>
33#include "internal/iprt.h"
34
35#include <iprt/assert.h>
36#include <iprt/err.h>
37#include <iprt/log.h>
38#include <iprt/md5.h>
39#include <iprt/mem.h>
40#include <iprt/sha.h>
41#include <iprt/string.h>
42#include <iprt/formats/mz.h>
43#include "internal/ldr.h"
44
45#ifdef LDR_ONLY_PE
46# undef LDR_WITH_PE
47# undef LDR_WITH_KLDR
48# undef LDR_WITH_ELF
49# undef LDR_WITH_LX
50# undef LDR_WITH_LE
51# undef LDR_WITH_NE
52# undef LDR_WITH_MZ
53# undef LDR_WITH_AOUT
54# define LDR_WITH_PE
55#endif
56
57
58RTDECL(int) RTLdrOpenWithReader(PRTLDRREADER pReader, uint32_t fFlags, RTLDRARCH enmArch, PRTLDRMOD phMod, PRTERRINFO pErrInfo)
59{
60 /*
61 * Resolve RTLDRARCH_HOST.
62 */
63 if (enmArch == RTLDRARCH_HOST)
64 enmArch = RTLdrGetHostArch();
65
66 /*
67 * Read and verify the file signature.
68 */
69 union
70 {
71 char ach[4];
72 uint16_t au16[2];
73 uint32_t u32;
74 } uSign;
75 int rc = pReader->pfnRead(pReader, &uSign, sizeof(uSign), 0);
76 if (RT_FAILURE(rc))
77 return rc;
78#ifndef LDR_WITH_KLDR
79 if ( uSign.au16[0] != IMAGE_DOS_SIGNATURE
80 && uSign.u32 != IMAGE_NT_SIGNATURE
81 && uSign.u32 != IMAGE_ELF_SIGNATURE
82 && uSign.au16[0] != IMAGE_LX_SIGNATURE)
83 {
84 Log(("rtldrOpenWithReader: %s: unknown magic %#x / '%.4s\n", pReader->pfnLogName(pReader), uSign.u32, &uSign.ach[0]));
85 return VERR_INVALID_EXE_SIGNATURE;
86 }
87#endif
88 uint32_t offHdr = 0;
89 if (uSign.au16[0] == IMAGE_DOS_SIGNATURE)
90 {
91 rc = pReader->pfnRead(pReader, &offHdr, sizeof(offHdr), RT_UOFFSETOF(IMAGE_DOS_HEADER, e_lfanew));
92 if (RT_FAILURE(rc))
93 return rc;
94
95 if (offHdr <= sizeof(IMAGE_DOS_HEADER))
96 {
97 Log(("rtldrOpenWithReader: %s: no new header / invalid offset %#RX32\n", pReader->pfnLogName(pReader), offHdr));
98 return VERR_INVALID_EXE_SIGNATURE;
99 }
100 rc = pReader->pfnRead(pReader, &uSign, sizeof(uSign), offHdr);
101 if (RT_FAILURE(rc))
102 return rc;
103 if ( uSign.u32 != IMAGE_NT_SIGNATURE
104 && uSign.au16[0] != IMAGE_LX_SIGNATURE
105 && uSign.au16[0] != IMAGE_LE_SIGNATURE
106 && uSign.au16[0] != IMAGE_NE_SIGNATURE)
107 {
108 Log(("rtldrOpenWithReader: %s: unknown new magic %#x / '%.4s\n", pReader->pfnLogName(pReader), uSign.u32, &uSign.ach[0]));
109 return VERR_INVALID_EXE_SIGNATURE;
110 }
111 }
112
113 /*
114 * Create image interpreter instance depending on the signature.
115 */
116 if (uSign.u32 == IMAGE_NT_SIGNATURE)
117#ifdef LDR_WITH_PE
118 rc = rtldrPEOpen(pReader, fFlags, enmArch, offHdr, phMod, pErrInfo);
119#else
120 rc = VERR_PE_EXE_NOT_SUPPORTED;
121#endif
122 else if (uSign.u32 == IMAGE_ELF_SIGNATURE)
123#if defined(LDR_WITH_ELF)
124 rc = rtldrELFOpen(pReader, fFlags, enmArch, phMod, pErrInfo);
125#else
126 rc = VERR_ELF_EXE_NOT_SUPPORTED;
127#endif
128 else if (uSign.au16[0] == IMAGE_LX_SIGNATURE)
129#ifdef LDR_WITH_LX
130 rc = rtldrLXOpen(pReader, fFlags, enmArch, offHdr, phMod, pErrInfo);
131#else
132 rc = VERR_LX_EXE_NOT_SUPPORTED;
133#endif
134 else if (uSign.au16[0] == IMAGE_LE_SIGNATURE)
135#ifdef LDR_WITH_LE
136 rc = rtldrLEOpen(pReader, fFlags, enmArch, phMod, pErrInfo);
137#else
138 rc = VERR_LE_EXE_NOT_SUPPORTED;
139#endif
140 else if (uSign.au16[0] == IMAGE_NE_SIGNATURE)
141#ifdef LDR_WITH_NE
142 rc = rtldrNEOpen(pReader, fFlags, enmArch, phMod, pErrInfo);
143#else
144 rc = VERR_NE_EXE_NOT_SUPPORTED;
145#endif
146 else if (uSign.au16[0] == IMAGE_DOS_SIGNATURE)
147#ifdef LDR_WITH_MZ
148 rc = rtldrMZOpen(pReader, fFlags, enmArch, phMod, pErrInfo);
149#else
150 rc = VERR_MZ_EXE_NOT_SUPPORTED;
151#endif
152 else if (/* uSign.u32 == IMAGE_AOUT_A_SIGNATURE
153 || uSign.u32 == IMAGE_AOUT_Z_SIGNATURE*/ /** @todo find the aout magics in emx or binutils. */
154 0)
155#ifdef LDR_WITH_AOUT
156 rc = rtldrAOUTOpen(pReader, fFlags, enmArch, phMod, pErrInfo);
157#else
158 rc = VERR_AOUT_EXE_NOT_SUPPORTED;
159#endif
160 else
161 {
162#ifndef LDR_WITH_KLDR
163 Log(("rtldrOpenWithReader: %s: the format isn't implemented %#x / '%.4s\n", pReader->pfnLogName(pReader), uSign.u32, &uSign.ach[0]));
164#endif
165 rc = VERR_INVALID_EXE_SIGNATURE;
166 }
167
168#ifdef LDR_WITH_KLDR
169 /* Try kLdr if it's a format we don't recognize. */
170 if (rc <= VERR_INVALID_EXE_SIGNATURE && rc > VERR_BAD_EXE_FORMAT)
171 {
172 int rc2 = rtldrkLdrOpen(pReader, fFlags, enmArch, phMod, pErrInfo);
173 if ( RT_SUCCESS(rc2)
174 || (rc == VERR_INVALID_EXE_SIGNATURE && rc2 != VERR_MZ_EXE_NOT_SUPPORTED /* Quick fix for bad return code. */)
175 || rc2 > VERR_INVALID_EXE_SIGNATURE
176 || rc2 <= VERR_BAD_EXE_FORMAT)
177 rc = rc2;
178 }
179#endif
180
181 LogFlow(("rtldrOpenWithReader: %s: returns %Rrc *phMod=%p\n", pReader->pfnLogName(pReader), rc, *phMod));
182 return rc;
183}
184
185
186RTDECL(size_t) RTLdrSize(RTLDRMOD hLdrMod)
187{
188 LogFlow(("RTLdrSize: hLdrMod=%RTldrm\n", hLdrMod));
189
190 /*
191 * Validate input.
192 */
193 AssertMsgReturn(rtldrIsValid(hLdrMod), ("hLdrMod=%p\n", hLdrMod), ~(size_t)0);
194 PRTLDRMODINTERNAL pMod = (PRTLDRMODINTERNAL)hLdrMod;
195 AssertMsgReturn(pMod->eState == LDR_STATE_OPENED, ("eState=%d\n", pMod->eState), ~(size_t)0);
196
197 /*
198 * Do it.
199 */
200 size_t cb = pMod->pOps->pfnGetImageSize(pMod);
201 LogFlow(("RTLdrSize: returns %zu\n", cb));
202 return cb;
203}
204RT_EXPORT_SYMBOL(RTLdrSize);
205
206
207/**
208 * Loads the image into a buffer provided by the user and applies fixups
209 * for the given base address.
210 *
211 * @returns iprt status code.
212 * @param hLdrMod The load module handle.
213 * @param pvBits Where to put the bits.
214 * Must be as large as RTLdrSize() suggests.
215 * @param BaseAddress The base address.
216 * @param pfnGetImport Callback function for resolving imports one by one.
217 * If this is NULL, imports will not be resolved.
218 * @param pvUser User argument for the callback.
219 * @remark Not supported for RTLdrLoad() images.
220 */
221RTDECL(int) RTLdrGetBits(RTLDRMOD hLdrMod, void *pvBits, RTLDRADDR BaseAddress, PFNRTLDRIMPORT pfnGetImport, void *pvUser)
222{
223 LogFlow(("RTLdrGetBits: hLdrMod=%RTldrm pvBits=%p BaseAddress=%RTptr pfnGetImport=%p pvUser=%p\n",
224 hLdrMod, pvBits, BaseAddress, pfnGetImport, pvUser));
225
226 /*
227 * Validate input.
228 */
229 AssertMsgReturn(rtldrIsValid(hLdrMod), ("hLdrMod=%p\n", hLdrMod), VERR_INVALID_HANDLE);
230 AssertPtrReturn(pvBits, VERR_INVALID_POINTER);
231 AssertPtrNullReturn(pfnGetImport, VERR_INVALID_POINTER);
232 PRTLDRMODINTERNAL pMod = (PRTLDRMODINTERNAL)hLdrMod;
233 AssertMsgReturn(pMod->eState == LDR_STATE_OPENED, ("eState=%d\n", pMod->eState), VERR_WRONG_ORDER);
234
235 /*
236 * Do it.
237 */
238 int rc = pMod->pOps->pfnGetBits(pMod, pvBits, BaseAddress, pfnGetImport, pvUser);
239 LogFlow(("RTLdrGetBits: returns %Rrc\n",rc));
240 return rc;
241}
242RT_EXPORT_SYMBOL(RTLdrGetBits);
243
244
245/**
246 * Relocates bits after getting them.
247 * Useful for code which moves around a bit.
248 *
249 * @returns iprt status code.
250 * @param hLdrMod The loader module handle.
251 * @param pvBits Where the image bits are.
252 * Must have been passed to RTLdrGetBits().
253 * @param NewBaseAddress The new base address.
254 * @param OldBaseAddress The old base address.
255 * @param pfnGetImport Callback function for resolving imports one by one.
256 * @param pvUser User argument for the callback.
257 * @remark Not supported for RTLdrLoad() images.
258 */
259RTDECL(int) RTLdrRelocate(RTLDRMOD hLdrMod, void *pvBits, RTLDRADDR NewBaseAddress, RTLDRADDR OldBaseAddress,
260 PFNRTLDRIMPORT pfnGetImport, void *pvUser)
261{
262 LogFlow(("RTLdrRelocate: hLdrMod=%RTldrm pvBits=%p NewBaseAddress=%RTptr OldBaseAddress=%RTptr pfnGetImport=%p pvUser=%p\n",
263 hLdrMod, pvBits, NewBaseAddress, OldBaseAddress, pfnGetImport, pvUser));
264
265 /*
266 * Validate input.
267 */
268 AssertMsgReturn(rtldrIsValid(hLdrMod), ("hLdrMod=%p\n", hLdrMod), VERR_INVALID_HANDLE);
269 AssertMsgReturn(VALID_PTR(pvBits), ("pvBits=%p\n", pvBits), VERR_INVALID_PARAMETER);
270 AssertMsgReturn(VALID_PTR(pfnGetImport), ("pfnGetImport=%p\n", pfnGetImport), VERR_INVALID_PARAMETER);
271 PRTLDRMODINTERNAL pMod = (PRTLDRMODINTERNAL)hLdrMod;
272 AssertMsgReturn(pMod->eState == LDR_STATE_OPENED, ("eState=%d\n", pMod->eState), VERR_WRONG_ORDER);
273
274 /*
275 * Do it.
276 */
277 int rc = pMod->pOps->pfnRelocate(pMod, pvBits, NewBaseAddress, OldBaseAddress, pfnGetImport, pvUser);
278 LogFlow(("RTLdrRelocate: returns %Rrc\n", rc));
279 return rc;
280}
281RT_EXPORT_SYMBOL(RTLdrRelocate);
282
283
284RTDECL(int) RTLdrGetSymbolEx(RTLDRMOD hLdrMod, const void *pvBits, RTLDRADDR BaseAddress,
285 uint32_t iOrdinal, const char *pszSymbol, PRTLDRADDR pValue)
286{
287 LogFlow(("RTLdrGetSymbolEx: hLdrMod=%RTldrm pvBits=%p BaseAddress=%RTptr iOrdinal=%#x pszSymbol=%p:{%s} pValue=%p\n",
288 hLdrMod, pvBits, BaseAddress, iOrdinal, pszSymbol, pszSymbol, pValue));
289
290 /*
291 * Validate input.
292 */
293 AssertMsgReturn(rtldrIsValid(hLdrMod), ("hLdrMod=%p\n", hLdrMod), VERR_INVALID_HANDLE);
294 AssertPtrNullReturn(pvBits, VERR_INVALID_POINTER);
295 AssertPtrNullReturn(pszSymbol, VERR_INVALID_POINTER);
296 AssertReturn(pszSymbol || iOrdinal != UINT32_MAX, VERR_INVALID_PARAMETER);
297 AssertPtrReturn(pValue, VERR_INVALID_POINTER);
298 PRTLDRMODINTERNAL pMod = (PRTLDRMODINTERNAL)hLdrMod;
299
300 /*
301 * Do it.
302 */
303 int rc;
304 if (pMod->pOps->pfnGetSymbolEx)
305 rc = pMod->pOps->pfnGetSymbolEx(pMod, pvBits, BaseAddress, iOrdinal, pszSymbol, pValue);
306 else if (!BaseAddress && !pvBits && iOrdinal == UINT32_MAX)
307 {
308 void *pvValue;
309 rc = pMod->pOps->pfnGetSymbol(pMod, pszSymbol, &pvValue);
310 if (RT_SUCCESS(rc))
311 *pValue = (uintptr_t)pvValue;
312 }
313 else
314 AssertMsgFailedReturn(("BaseAddress=%RTptr pvBits=%p\n", BaseAddress, pvBits), VERR_INVALID_FUNCTION);
315 LogFlow(("RTLdrGetSymbolEx: returns %Rrc *pValue=%p\n", rc, *pValue));
316 return rc;
317}
318RT_EXPORT_SYMBOL(RTLdrGetSymbolEx);
319
320
321RTDECL(int) RTLdrQueryForwarderInfo(RTLDRMOD hLdrMod, const void *pvBits, uint32_t iOrdinal, const char *pszSymbol,
322 PRTLDRIMPORTINFO pInfo, size_t cbInfo)
323{
324 LogFlow(("RTLdrQueryForwarderInfo: hLdrMod=%RTldrm pvBits=%p iOrdinal=%#x pszSymbol=%p:{%s} pInfo=%p cbInfo=%zu\n",
325 hLdrMod, pvBits, iOrdinal, pszSymbol, pszSymbol, pInfo, cbInfo));
326
327 /*
328 * Validate input.
329 */
330 AssertMsgReturn(rtldrIsValid(hLdrMod), ("hLdrMod=%p\n", hLdrMod), VERR_INVALID_HANDLE);
331 AssertPtrNullReturn(pvBits, VERR_INVALID_POINTER);
332 AssertMsgReturn(pszSymbol, ("pszSymbol=%p\n", pszSymbol), VERR_INVALID_PARAMETER);
333 AssertPtrReturn(pInfo, VERR_INVALID_PARAMETER);
334 AssertReturn(cbInfo >= sizeof(*pInfo), VERR_INVALID_PARAMETER);
335 PRTLDRMODINTERNAL pMod = (PRTLDRMODINTERNAL)hLdrMod;
336
337 /*
338 * Do it.
339 */
340 int rc;
341 if (pMod->pOps->pfnQueryForwarderInfo)
342 {
343 rc = pMod->pOps->pfnQueryForwarderInfo(pMod, pvBits, iOrdinal, pszSymbol, pInfo, cbInfo);
344 if (RT_SUCCESS(rc))
345 LogFlow(("RTLdrQueryForwarderInfo: returns %Rrc pInfo={%#x,%#x,%s,%s}\n", rc,
346 pInfo->iSelfOrdinal, pInfo->iOrdinal, pInfo->pszSymbol, pInfo->szModule));
347 else
348 LogFlow(("RTLdrQueryForwarderInfo: returns %Rrc\n", rc));
349 }
350 else
351 {
352 LogFlow(("RTLdrQueryForwarderInfo: returns VERR_NOT_SUPPORTED\n"));
353 rc = VERR_NOT_SUPPORTED;
354 }
355 return rc;
356
357}
358RT_EXPORT_SYMBOL(RTLdrQueryForwarderInfo);
359
360
361/**
362 * Enumerates all symbols in a module.
363 *
364 * @returns iprt status code.
365 * @param hLdrMod The loader module handle.
366 * @param fFlags Flags indicating what to return and such.
367 * @param pvBits Optional pointer to the loaded image.
368 * Set this to NULL if no RTLdrGetBits() processed image bits are available.
369 * @param BaseAddress Image load address.
370 * @param pfnCallback Callback function.
371 * @param pvUser User argument for the callback.
372 * @remark Not supported for RTLdrLoad() images.
373 */
374RTDECL(int) RTLdrEnumSymbols(RTLDRMOD hLdrMod, unsigned fFlags, const void *pvBits, RTLDRADDR BaseAddress,
375 PFNRTLDRENUMSYMS pfnCallback, void *pvUser)
376{
377 LogFlow(("RTLdrEnumSymbols: hLdrMod=%RTldrm fFlags=%#x pvBits=%p BaseAddress=%RTptr pfnCallback=%p pvUser=%p\n",
378 hLdrMod, fFlags, pvBits, BaseAddress, pfnCallback, pvUser));
379
380 /*
381 * Validate input.
382 */
383 AssertMsgReturn(rtldrIsValid(hLdrMod), ("hLdrMod=%p\n", hLdrMod), VERR_INVALID_HANDLE);
384 AssertMsgReturn(!pvBits || VALID_PTR(pvBits), ("pvBits=%p\n", pvBits), VERR_INVALID_PARAMETER);
385 AssertMsgReturn(VALID_PTR(pfnCallback), ("pfnCallback=%p\n", pfnCallback), VERR_INVALID_PARAMETER);
386 PRTLDRMODINTERNAL pMod = (PRTLDRMODINTERNAL)hLdrMod;
387 //AssertMsgReturn(pMod->eState == LDR_STATE_OPENED, ("eState=%d\n", pMod->eState), VERR_WRONG_ORDER);
388
389 /*
390 * Do it.
391 */
392 int rc = pMod->pOps->pfnEnumSymbols(pMod, fFlags, pvBits, BaseAddress, pfnCallback, pvUser);
393 LogFlow(("RTLdrEnumSymbols: returns %Rrc\n", rc));
394 return rc;
395}
396RT_EXPORT_SYMBOL(RTLdrEnumSymbols);
397
398
399RTDECL(int) RTLdrEnumDbgInfo(RTLDRMOD hLdrMod, const void *pvBits, PFNRTLDRENUMDBG pfnCallback, void *pvUser)
400{
401 LogFlow(("RTLdrEnumDbgInfo: hLdrMod=%RTldrm pvBits=%p pfnCallback=%p pvUser=%p\n",
402 hLdrMod, pvBits, pfnCallback, pvUser));
403
404 /*
405 * Validate input.
406 */
407 AssertMsgReturn(rtldrIsValid(hLdrMod), ("hLdrMod=%p\n", hLdrMod), VERR_INVALID_HANDLE);
408 AssertMsgReturn(!pvBits || RT_VALID_PTR(pvBits), ("pvBits=%p\n", pvBits), VERR_INVALID_PARAMETER);
409 AssertMsgReturn(RT_VALID_PTR(pfnCallback), ("pfnCallback=%p\n", pfnCallback), VERR_INVALID_PARAMETER);
410 PRTLDRMODINTERNAL pMod = (PRTLDRMODINTERNAL)hLdrMod;
411 //AssertMsgReturn(pMod->eState == LDR_STATE_OPENED, ("eState=%d\n", pMod->eState), VERR_WRONG_ORDER);
412
413 /*
414 * Do it.
415 */
416 int rc;
417 if (pMod->pOps->pfnEnumDbgInfo)
418 rc = pMod->pOps->pfnEnumDbgInfo(pMod, pvBits, pfnCallback, pvUser);
419 else
420 rc = VERR_NOT_SUPPORTED;
421
422 LogFlow(("RTLdrEnumDbgInfo: returns %Rrc\n", rc));
423 return rc;
424}
425RT_EXPORT_SYMBOL(RTLdrEnumDbgInfo);
426
427
428RTDECL(int) RTLdrEnumSegments(RTLDRMOD hLdrMod, PFNRTLDRENUMSEGS pfnCallback, void *pvUser)
429{
430 LogFlow(("RTLdrEnumSegments: hLdrMod=%RTldrm pfnCallback=%p pvUser=%p\n",
431 hLdrMod, pfnCallback, pvUser));
432
433 /*
434 * Validate input.
435 */
436 AssertMsgReturn(rtldrIsValid(hLdrMod), ("hLdrMod=%p\n", hLdrMod), VERR_INVALID_HANDLE);
437 AssertMsgReturn(RT_VALID_PTR(pfnCallback), ("pfnCallback=%p\n", pfnCallback), VERR_INVALID_PARAMETER);
438 PRTLDRMODINTERNAL pMod = (PRTLDRMODINTERNAL)hLdrMod;
439 //AssertMsgReturn(pMod->eState == LDR_STATE_OPENED, ("eState=%d\n", pMod->eState), VERR_WRONG_ORDER);
440
441 /*
442 * Do it.
443 */
444 int rc;
445 if (pMod->pOps->pfnEnumSegments)
446 rc = pMod->pOps->pfnEnumSegments(pMod, pfnCallback, pvUser);
447 else
448 rc = VERR_NOT_SUPPORTED;
449
450 LogFlow(("RTLdrEnumSegments: returns %Rrc\n", rc));
451 return rc;
452
453}
454RT_EXPORT_SYMBOL(RTLdrEnumSegments);
455
456
457RTDECL(int) RTLdrLinkAddressToSegOffset(RTLDRMOD hLdrMod, RTLDRADDR LinkAddress, uint32_t *piSeg, PRTLDRADDR poffSeg)
458{
459 LogFlow(("RTLdrLinkAddressToSegOffset: hLdrMod=%RTldrm LinkAddress=%RTptr piSeg=%p poffSeg=%p\n",
460 hLdrMod, LinkAddress, piSeg, poffSeg));
461
462 /*
463 * Validate input.
464 */
465 AssertMsgReturn(rtldrIsValid(hLdrMod), ("hLdrMod=%p\n", hLdrMod), VERR_INVALID_HANDLE);
466 AssertPtrReturn(piSeg, VERR_INVALID_POINTER);
467 AssertPtrReturn(poffSeg, VERR_INVALID_POINTER);
468
469 PRTLDRMODINTERNAL pMod = (PRTLDRMODINTERNAL)hLdrMod;
470 //AssertMsgReturn(pMod->eState == LDR_STATE_OPENED, ("eState=%d\n", pMod->eState), VERR_WRONG_ORDER);
471
472 *piSeg = UINT32_MAX;
473 *poffSeg = ~(RTLDRADDR)0;
474
475 /*
476 * Do it.
477 */
478 int rc;
479 if (pMod->pOps->pfnLinkAddressToSegOffset)
480 rc = pMod->pOps->pfnLinkAddressToSegOffset(pMod, LinkAddress, piSeg, poffSeg);
481 else
482 rc = VERR_NOT_SUPPORTED;
483
484 LogFlow(("RTLdrLinkAddressToSegOffset: returns %Rrc %#x:%RTptr\n", rc, *piSeg, *poffSeg));
485 return rc;
486}
487RT_EXPORT_SYMBOL(RTLdrLinkAddressToSegOffset);
488
489
490RTDECL(int) RTLdrLinkAddressToRva(RTLDRMOD hLdrMod, RTLDRADDR LinkAddress, PRTLDRADDR pRva)
491{
492 LogFlow(("RTLdrLinkAddressToRva: hLdrMod=%RTldrm LinkAddress=%RTptr pRva=%p\n",
493 hLdrMod, LinkAddress, pRva));
494
495 /*
496 * Validate input.
497 */
498 AssertMsgReturn(rtldrIsValid(hLdrMod), ("hLdrMod=%p\n", hLdrMod), VERR_INVALID_HANDLE);
499 AssertPtrReturn(pRva, VERR_INVALID_POINTER);
500
501 PRTLDRMODINTERNAL pMod = (PRTLDRMODINTERNAL)hLdrMod;
502 //AssertMsgReturn(pMod->eState == LDR_STATE_OPENED, ("eState=%d\n", pMod->eState), VERR_WRONG_ORDER);
503
504 *pRva = ~(RTLDRADDR)0;
505
506 /*
507 * Do it.
508 */
509 int rc;
510 if (pMod->pOps->pfnLinkAddressToRva)
511 rc = pMod->pOps->pfnLinkAddressToRva(pMod, LinkAddress, pRva);
512 else
513 rc = VERR_NOT_SUPPORTED;
514
515 LogFlow(("RTLdrLinkAddressToRva: returns %Rrc %RTptr\n", rc, *pRva));
516 return rc;
517}
518RT_EXPORT_SYMBOL(RTLdrLinkAddressToRva);
519
520
521RTDECL(int) RTLdrSegOffsetToRva(RTLDRMOD hLdrMod, uint32_t iSeg, RTLDRADDR offSeg, PRTLDRADDR pRva)
522{
523 LogFlow(("RTLdrSegOffsetToRva: hLdrMod=%RTldrm iSeg=%#x offSeg=%RTptr pRva=%p\n", hLdrMod, iSeg, offSeg, pRva));
524
525 /*
526 * Validate input.
527 */
528 AssertMsgReturn(rtldrIsValid(hLdrMod), ("hLdrMod=%p\n", hLdrMod), VERR_INVALID_HANDLE);
529 AssertPtrReturn(pRva, VERR_INVALID_POINTER);
530
531 PRTLDRMODINTERNAL pMod = (PRTLDRMODINTERNAL)hLdrMod;
532 //AssertMsgReturn(pMod->eState == LDR_STATE_OPENED, ("eState=%d\n", pMod->eState), VERR_WRONG_ORDER);
533
534 *pRva = ~(RTLDRADDR)0;
535
536 /*
537 * Do it.
538 */
539 int rc;
540 if (pMod->pOps->pfnSegOffsetToRva)
541 rc = pMod->pOps->pfnSegOffsetToRva(pMod, iSeg, offSeg, pRva);
542 else
543 rc = VERR_NOT_SUPPORTED;
544
545 LogFlow(("RTLdrSegOffsetToRva: returns %Rrc %RTptr\n", rc, *pRva));
546 return rc;
547}
548RT_EXPORT_SYMBOL(RTLdrSegOffsetToRva);
549
550RTDECL(int) RTLdrRvaToSegOffset(RTLDRMOD hLdrMod, RTLDRADDR Rva, uint32_t *piSeg, PRTLDRADDR poffSeg)
551{
552 LogFlow(("RTLdrRvaToSegOffset: hLdrMod=%RTldrm Rva=%RTptr piSeg=%p poffSeg=%p\n",
553 hLdrMod, Rva, piSeg, poffSeg));
554
555 /*
556 * Validate input.
557 */
558 AssertMsgReturn(rtldrIsValid(hLdrMod), ("hLdrMod=%p\n", hLdrMod), VERR_INVALID_HANDLE);
559 AssertPtrReturn(piSeg, VERR_INVALID_POINTER);
560 AssertPtrReturn(poffSeg, VERR_INVALID_POINTER);
561
562 PRTLDRMODINTERNAL pMod = (PRTLDRMODINTERNAL)hLdrMod;
563 //AssertMsgReturn(pMod->eState == LDR_STATE_OPENED, ("eState=%d\n", pMod->eState), VERR_WRONG_ORDER);
564
565 *piSeg = UINT32_MAX;
566 *poffSeg = ~(RTLDRADDR)0;
567
568 /*
569 * Do it.
570 */
571 int rc;
572 if (pMod->pOps->pfnRvaToSegOffset)
573 rc = pMod->pOps->pfnRvaToSegOffset(pMod, Rva, piSeg, poffSeg);
574 else
575 rc = VERR_NOT_SUPPORTED;
576
577 LogFlow(("RTLdrRvaToSegOffset: returns %Rrc %#x:%RTptr\n", rc, *piSeg, *poffSeg));
578 return rc;
579}
580RT_EXPORT_SYMBOL(RTLdrRvaToSegOffset);
581
582
583RTDECL(int) RTLdrQueryProp(RTLDRMOD hLdrMod, RTLDRPROP enmProp, void *pvBuf, size_t cbBuf)
584{
585 return RTLdrQueryPropEx(hLdrMod, enmProp, NULL /*pvBits*/, pvBuf, cbBuf, NULL);
586}
587RT_EXPORT_SYMBOL(RTLdrQueryProp);
588
589
590RTDECL(int) RTLdrQueryPropEx(RTLDRMOD hLdrMod, RTLDRPROP enmProp, void *pvBits, void *pvBuf, size_t cbBuf, size_t *pcbRet)
591{
592 AssertMsgReturn(rtldrIsValid(hLdrMod), ("hLdrMod=%p\n", hLdrMod), RTLDRENDIAN_INVALID);
593 PRTLDRMODINTERNAL pMod = (PRTLDRMODINTERNAL)hLdrMod;
594
595 AssertPtrNullReturn(pcbRet, VERR_INVALID_POINTER);
596 size_t cbRet;
597 if (!pcbRet)
598 pcbRet = &cbRet;
599
600 /*
601 * Do some pre screening of the input
602 */
603 switch (enmProp)
604 {
605 case RTLDRPROP_UUID:
606 *pcbRet = sizeof(RTUUID);
607 AssertReturn(cbBuf == sizeof(RTUUID), VERR_INVALID_PARAMETER);
608 break;
609 case RTLDRPROP_TIMESTAMP_SECONDS:
610 *pcbRet = sizeof(int64_t);
611 AssertReturn(cbBuf == sizeof(int32_t) || cbBuf == sizeof(int64_t), VERR_INVALID_PARAMETER);
612 break;
613 case RTLDRPROP_IS_SIGNED:
614 *pcbRet = sizeof(bool);
615 AssertReturn(cbBuf == sizeof(bool), VERR_INVALID_PARAMETER);
616 break;
617 case RTLDRPROP_PKCS7_SIGNED_DATA:
618 *pcbRet = 0;
619 break;
620 case RTLDRPROP_SIGNATURE_CHECKS_ENFORCED:
621 *pcbRet = sizeof(bool);
622 AssertReturn(cbBuf == sizeof(bool), VERR_INVALID_PARAMETER);
623 break;
624 case RTLDRPROP_IMPORT_COUNT:
625 *pcbRet = sizeof(uint32_t);
626 AssertReturn(cbBuf == sizeof(uint32_t), VERR_INVALID_PARAMETER);
627 break;
628 case RTLDRPROP_IMPORT_MODULE:
629 *pcbRet = sizeof(uint32_t);
630 AssertReturn(cbBuf >= sizeof(uint32_t), VERR_INVALID_PARAMETER);
631 break;
632 case RTLDRPROP_FILE_OFF_HEADER:
633 *pcbRet = sizeof(uint64_t);
634 AssertReturn(cbBuf == sizeof(uint32_t) || cbBuf == sizeof(uint64_t), VERR_INVALID_PARAMETER);
635 break;
636 case RTLDRPROP_INTERNAL_NAME:
637 *pcbRet = 0;
638 break;
639
640 default:
641 AssertFailedReturn(VERR_INVALID_FUNCTION);
642 }
643 AssertPtrReturn(pvBuf, VERR_INVALID_POINTER);
644
645 /*
646 * Call the image specific worker, if there is one.
647 */
648 if (!pMod->pOps->pfnQueryProp)
649 return VERR_NOT_SUPPORTED;
650 return pMod->pOps->pfnQueryProp(pMod, enmProp, pvBits, pvBuf, cbBuf, pcbRet);
651}
652RT_EXPORT_SYMBOL(RTLdrQueryPropEx);
653
654
655RTDECL(int) RTLdrVerifySignature(RTLDRMOD hLdrMod, PFNRTLDRVALIDATESIGNEDDATA pfnCallback, void *pvUser, PRTERRINFO pErrInfo)
656{
657 AssertMsgReturn(rtldrIsValid(hLdrMod), ("hLdrMod=%p\n", hLdrMod), VERR_INVALID_HANDLE);
658 PRTLDRMODINTERNAL pMod = (PRTLDRMODINTERNAL)hLdrMod;
659 AssertPtrReturn(pfnCallback, VERR_INVALID_POINTER);
660
661 /*
662 * Call the image specific worker, if there is one.
663 */
664 if (!pMod->pOps->pfnVerifySignature)
665 return VERR_NOT_SUPPORTED;
666 return pMod->pOps->pfnVerifySignature(pMod, pfnCallback, pvUser, pErrInfo);
667}
668RT_EXPORT_SYMBOL(RTLdrVerifySignature);
669
670
671RTDECL(int) RTLdrHashImage(RTLDRMOD hLdrMod, RTDIGESTTYPE enmDigest, char *pszDigest, size_t cbDigest)
672{
673 AssertMsgReturn(rtldrIsValid(hLdrMod), ("hLdrMod=%p\n", hLdrMod), VERR_INVALID_HANDLE);
674 PRTLDRMODINTERNAL pMod = (PRTLDRMODINTERNAL)hLdrMod;
675
676 /*
677 * Make sure there is sufficient space for the wanted digest and that
678 * it's supported.
679 */
680 switch (enmDigest)
681 {
682 case RTDIGESTTYPE_MD5: AssertReturn(cbDigest >= RTMD5_DIGEST_LEN + 1, VERR_BUFFER_OVERFLOW); break;
683 case RTDIGESTTYPE_SHA1: AssertReturn(cbDigest >= RTSHA1_DIGEST_LEN + 1, VERR_BUFFER_OVERFLOW); break;
684 case RTDIGESTTYPE_SHA256: AssertReturn(cbDigest >= RTSHA256_DIGEST_LEN + 1, VERR_BUFFER_OVERFLOW); break;
685 case RTDIGESTTYPE_SHA512: AssertReturn(cbDigest >= RTSHA512_DIGEST_LEN + 1, VERR_BUFFER_OVERFLOW); break;
686 default:
687 if (enmDigest > RTDIGESTTYPE_INVALID && enmDigest < RTDIGESTTYPE_END)
688 return VERR_NOT_SUPPORTED;
689 AssertFailedReturn(VERR_INVALID_PARAMETER);
690 }
691 AssertPtrReturn(pszDigest, VERR_INVALID_POINTER);
692
693 /*
694 * Call the image specific worker, if there is one.
695 */
696 if (!pMod->pOps->pfnHashImage)
697 return VERR_NOT_SUPPORTED;
698 return pMod->pOps->pfnHashImage(pMod, enmDigest, pszDigest, cbDigest);
699}
700RT_EXPORT_SYMBOL(RTLdrHashImage);
701
702
703/**
704 * Internal method used by the IPRT debug bits.
705 *
706 * @returns IPRT status code.
707 * @param hLdrMod The loader handle which executable we wish to
708 * read from.
709 * @param pvBuf The output buffer.
710 * @param iDbgInfo The debug info ordinal number if the request
711 * corresponds exactly to a debug info part from
712 * pfnEnumDbgInfo. Otherwise, pass UINT32_MAX.
713 * @param off Where in the executable file to start reading.
714 * @param cb The number of bytes to read.
715 *
716 * @remarks Fixups will only be applied if @a iDbgInfo is specified.
717 */
718DECLHIDDEN(int) rtLdrReadAt(RTLDRMOD hLdrMod, void *pvBuf, uint32_t iDbgInfo, RTFOFF off, size_t cb)
719{
720 AssertMsgReturn(rtldrIsValid(hLdrMod), ("hLdrMod=%p\n", hLdrMod), VERR_INVALID_HANDLE);
721 PRTLDRMODINTERNAL pMod = (PRTLDRMODINTERNAL)hLdrMod;
722
723 if (iDbgInfo != UINT32_MAX)
724 {
725 AssertReturn(pMod->pOps->pfnReadDbgInfo, VERR_NOT_SUPPORTED);
726 return pMod->pOps->pfnReadDbgInfo(pMod, iDbgInfo, off, cb, pvBuf);
727 }
728
729 AssertReturn(pMod->pReader, VERR_NOT_SUPPORTED);
730 return pMod->pReader->pfnRead(pMod->pReader, pvBuf, cb, off);
731}
732
733
734/**
735 * Translates a RTLDRARCH value to a string.
736 *
737 * @returns Name corresponding to @a enmArch
738 * @param enmArch The value to name.
739 */
740RTDECL(const char *) RTLdrArchName(RTLDRARCH enmArch)
741{
742 switch (enmArch)
743 {
744 case RTLDRARCH_INVALID: return "INVALID";
745 case RTLDRARCH_WHATEVER: return "WHATEVER";
746 case RTLDRARCH_HOST: return "HOST";
747 case RTLDRARCH_AMD64: return "AMD64";
748 case RTLDRARCH_X86_32: return "X86_32";
749 case RTLDRARCH_ARM32: return "ARM32";
750 case RTLDRARCH_ARM64: return "ARM64";
751
752 case RTLDRARCH_END:
753 case RTLDRARCH_32BIT_HACK:
754 break;
755 }
756 return "UNKNOWN";
757}
758RT_EXPORT_SYMBOL(RTLdrArchName);
759
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette