VirtualBox

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

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

IPRT: More kStuff cleanup. bugref:9232

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