VirtualBox

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

Last change on this file since 106579 was 106061, checked in by vboxsync, 3 months ago

Copyright year updates by scm.

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