VirtualBox

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

Last change on this file since 7937 was 5999, checked in by vboxsync, 17 years ago

The Giant CDDL Dual-License Header Change.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 13.2 KB
Line 
1/* $Id: ldrEx.cpp 5999 2007-12-07 15:05:06Z vboxsync $ */
2/** @file
3 * innotek Portable Runtime - Binary Image Loader, Extended Features.
4 */
5
6/*
7 * Copyright (C) 2006-2007 innotek GmbH
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 <iprt/alloc.h>
34#include <iprt/assert.h>
35#include <iprt/log.h>
36#include <iprt/string.h>
37#include <iprt/err.h>
38#include "internal/ldr.h"
39#include "internal/ldrMZ.h"
40
41
42/**
43 * Open part with reader.
44 *
45 * @returns iprt status code.
46 * @param pReader The loader reader instance which will provide the raw image bits.
47 * @param phMod Where to store the handle.
48 */
49int rtldrOpenWithReader(PRTLDRREADER pReader, PRTLDRMOD phMod)
50{
51 /*
52 * Read and verify the file signature.
53 */
54 union
55 {
56 char ach[4];
57 uint16_t au16[2];
58 uint32_t u32;
59 } uSign;
60 int rc = pReader->pfnRead(pReader, &uSign, sizeof(uSign), 0);
61 if (RT_FAILURE(rc))
62 return rc;
63#ifndef LDR_WITH_KLDR
64 if ( uSign.au16[0] != IMAGE_DOS_SIGNATURE
65 && uSign.u32 != IMAGE_NT_SIGNATURE
66 && uSign.u32 != IMAGE_ELF_SIGNATURE
67 && uSign.au16[0] != IMAGE_LX_SIGNATURE)
68 {
69 Log(("rtldrOpenWithReader: %s: unknown magic %#x / '%.4s\n", pReader->pfnLogName(pReader), uSign.u32, &uSign.ach[0]));
70 return VERR_INVALID_EXE_SIGNATURE;
71 }
72#endif
73 uint32_t offHdr = 0;
74 if (uSign.au16[0] == IMAGE_DOS_SIGNATURE)
75 {
76 rc = pReader->pfnRead(pReader, &offHdr, sizeof(offHdr), RT_OFFSETOF(IMAGE_DOS_HEADER, e_lfanew));
77 if (RT_FAILURE(rc))
78 return rc;
79
80 if (offHdr <= sizeof(IMAGE_DOS_HEADER))
81 {
82 Log(("rtldrOpenWithReader: %s: no new header / invalid offset %#RX32\n", pReader->pfnLogName(pReader), offHdr));
83 return VERR_INVALID_EXE_SIGNATURE;
84 }
85 rc = pReader->pfnRead(pReader, &uSign, sizeof(uSign), offHdr);
86 if (RT_FAILURE(rc))
87 return rc;
88 if ( uSign.u32 != IMAGE_NT_SIGNATURE
89 && uSign.au16[0] != IMAGE_LX_SIGNATURE
90 && uSign.au16[0] != IMAGE_LE_SIGNATURE
91 && uSign.au16[0] != IMAGE_NE_SIGNATURE)
92 {
93 Log(("rtldrOpenWithReader: %s: unknown new magic %#x / '%.4s\n", pReader->pfnLogName(pReader), uSign.u32, &uSign.ach[0]));
94 return VERR_INVALID_EXE_SIGNATURE;
95 }
96 }
97
98 /*
99 * Create image intepreter instance depending on the signature.
100 */
101 if (uSign.u32 == IMAGE_NT_SIGNATURE)
102#ifdef LDR_WITH_PE
103 rc = rtldrPEOpen(pReader, offHdr, phMod);
104#else
105 rc = VERR_PE_EXE_NOT_SUPPORTED;
106#endif
107 else if (uSign.u32 == IMAGE_ELF_SIGNATURE)
108#if defined(LDR_WITH_ELF)
109 rc = rtldrELFOpen(pReader, phMod);
110#else
111 rc = VERR_ELF_EXE_NOT_SUPPORTED;
112#endif
113 else if (uSign.au16[0] == IMAGE_LX_SIGNATURE)
114#ifdef LDR_WITH_LX
115 rc = rtldrLXOpen(pReader, offHdr, phMod);
116#else
117 rc = VERR_LX_EXE_NOT_SUPPORTED;
118#endif
119 else if (uSign.au16[0] == IMAGE_LE_SIGNATURE)
120#ifdef LDR_WITH_LE
121 rc = rtldrLEOpen(pReader, phMod);
122#else
123 rc = VERR_LE_EXE_NOT_SUPPORTED;
124#endif
125 else if (uSign.au16[0] == IMAGE_NE_SIGNATURE)
126#ifdef LDR_WITH_NE
127 rc = rtldrNEOpen(pReader, phMod);
128#else
129 rc = VERR_NE_EXE_NOT_SUPPORTED;
130#endif
131 else if (uSign.au16[0] == IMAGE_DOS_SIGNATURE)
132#ifdef LDR_WITH_MZ
133 rc = rtldrMZOpen(pReader, phMod);
134#else
135 rc = VERR_MZ_EXE_NOT_SUPPORTED;
136#endif
137 else if (/* uSign.u32 == IMAGE_AOUT_A_SIGNATURE
138 || uSign.u32 == IMAGE_AOUT_Z_SIGNATURE*/ /** @todo find the aout magics in emx or binutils. */
139 0)
140#ifdef LDR_WITH_AOUT
141 rc = rtldrAOUTOpen(pReader, phMod);
142#else
143 rc = VERR_AOUT_EXE_NOT_SUPPORTED;
144#endif
145 else
146 {
147#ifndef LDR_WITH_KLDR
148 Log(("rtldrOpenWithReader: %s: the format isn't implemented %#x / '%.4s\n", pReader->pfnLogName(pReader), uSign.u32, &uSign.ach[0]));
149#endif
150 rc = VERR_INVALID_EXE_SIGNATURE;
151 }
152
153#ifdef LDR_WITH_KLDR
154 /* Try kLdr if it's a format we don't recognize. */
155 if (rc <= VERR_INVALID_EXE_SIGNATURE && rc > VERR_BAD_EXE_FORMAT)
156 rc = rtldrkLdrOpen(pReader, phMod);
157#endif
158
159 LogFlow(("rtldrOpenWithReader: %s: returns %Rrc *phMod=%p\n", pReader->pfnLogName(pReader), rc, *phMod));
160 return rc;
161}
162
163
164/**
165 * Gets the size of the loaded image.
166 * This is only supported for modules which has been opened using RTLdrOpen() and RTLdrOpenBits().
167 *
168 * @returns image size (in bytes).
169 * @returns ~(size_t)0 on if not opened by RTLdrOpen().
170 * @param hLdrMod Handle to the loader module.
171 * @remark Not supported for RTLdrLoad() images.
172 */
173RTDECL(size_t) RTLdrSize(RTLDRMOD hLdrMod)
174{
175 LogFlow(("RTLdrSize: hLdrMod=%RTldrm\n", hLdrMod));
176
177 /*
178 * Validate input.
179 */
180 AssertMsgReturn(rtldrIsValid(hLdrMod), ("hLdrMod=%p\n", hLdrMod), ~(size_t)0);
181 PRTLDRMODINTERNAL pMod = (PRTLDRMODINTERNAL)hLdrMod;
182 AssertMsgReturn(pMod->eState == LDR_STATE_OPENED, ("eState=%d\n", pMod->eState), ~(size_t)0);
183
184 /*
185 * Do it.
186 */
187 size_t cb = pMod->pOps->pfnGetImageSize(pMod);
188 LogFlow(("RTLdrSize: returns %zu\n", cb));
189 return cb;
190}
191
192
193/**
194 * Loads the image into a buffer provided by the user and applies fixups
195 * for the given base address.
196 *
197 * @returns iprt status code.
198 * @param hLdrMod The load module handle.
199 * @param pvBits Where to put the bits.
200 * Must be as large as RTLdrSize() suggests.
201 * @param BaseAddress The base address.
202 * @param pfnGetImport Callback function for resolving imports one by one.
203 * @param pvUser User argument for the callback.
204 * @remark Not supported for RTLdrLoad() images.
205 */
206RTDECL(int) RTLdrGetBits(RTLDRMOD hLdrMod, void *pvBits, RTUINTPTR BaseAddress, PFNRTLDRIMPORT pfnGetImport, void *pvUser)
207{
208 LogFlow(("RTLdrGetBits: hLdrMod=%RTldrm pvBits=%p BaseAddress=%RTptr pfnGetImport=%p pvUser=%p\n",
209 hLdrMod, pvBits, BaseAddress, pfnGetImport, pvUser));
210
211 /*
212 * Validate input.
213 */
214 AssertMsgReturn(rtldrIsValid(hLdrMod), ("hLdrMod=%p\n", hLdrMod), VERR_INVALID_HANDLE);
215 AssertMsgReturn(VALID_PTR(pvBits), ("pvBits=%p\n", pvBits), VERR_INVALID_PARAMETER);
216 AssertMsgReturn(VALID_PTR(pfnGetImport), ("pfnGetImport=%p\n", pfnGetImport), VERR_INVALID_PARAMETER);
217 PRTLDRMODINTERNAL pMod = (PRTLDRMODINTERNAL)hLdrMod;
218 AssertMsgReturn(pMod->eState == LDR_STATE_OPENED, ("eState=%d\n", pMod->eState), VERR_WRONG_ORDER);
219
220 /*
221 * Do it.
222 */
223 int rc = pMod->pOps->pfnGetBits(pMod, pvBits, BaseAddress, pfnGetImport, pvUser);
224 LogFlow(("RTLdrGetBits: returns %Rrc\n",rc));
225 return rc;
226}
227
228
229/**
230 * Relocates bits after getting them.
231 * Useful for code which moves around a bit.
232 *
233 * @returns iprt status code.
234 * @param hLdrMod The loader module handle.
235 * @param pvBits Where the image bits are.
236 * Must've been passed to RTLdrGetBits().
237 * @param NewBaseAddress The new base address.
238 * @param OldBaseAddress The old base address.
239 * @param pfnGetImport Callback function for resolving imports one by one.
240 * @param pvUser User argument for the callback.
241 * @remark Not supported for RTLdrLoad() images.
242 */
243RTDECL(int) RTLdrRelocate(RTLDRMOD hLdrMod, void *pvBits, RTUINTPTR NewBaseAddress, RTUINTPTR OldBaseAddress,
244 PFNRTLDRIMPORT pfnGetImport, void *pvUser)
245{
246 LogFlow(("RTLdrRelocate: hLdrMod=%RTldrm pvBits=%p NewBaseAddress=%RTptr OldBaseAddress=%RTptr pfnGetImport=%p pvUser=%p\n",
247 hLdrMod, pvBits, NewBaseAddress, OldBaseAddress, pfnGetImport, pvUser));
248
249 /*
250 * Validate input.
251 */
252 AssertMsgReturn(rtldrIsValid(hLdrMod), ("hLdrMod=%p\n", hLdrMod), VERR_INVALID_HANDLE);
253 AssertMsgReturn(VALID_PTR(pvBits), ("pvBits=%p\n", pvBits), VERR_INVALID_PARAMETER);
254 AssertMsgReturn(VALID_PTR(pfnGetImport), ("pfnGetImport=%p\n", pfnGetImport), VERR_INVALID_PARAMETER);
255 PRTLDRMODINTERNAL pMod = (PRTLDRMODINTERNAL)hLdrMod;
256 AssertMsgReturn(pMod->eState == LDR_STATE_OPENED, ("eState=%d\n", pMod->eState), VERR_WRONG_ORDER);
257
258 /*
259 * Do it.
260 */
261 int rc = pMod->pOps->pfnRelocate(pMod, pvBits, NewBaseAddress, OldBaseAddress, pfnGetImport, pvUser);
262 LogFlow(("RTLdrRelocate: returns %Vrc\n", rc));
263 return rc;
264}
265
266
267/**
268 * Gets the address of a named exported symbol.
269 *
270 * This function differs from the plain one in that it can deal with
271 * both GC and HC address sizes, and that it can calculate the symbol
272 * value relative to any given base address.
273 *
274 * @returns iprt status code.
275 * @param hLdrMod The loader module handle.
276 * @param pvBits Optional pointer to the loaded image.
277 * Set this to NULL if no RTLdrGetBits() processed image bits are available.
278 * Not supported for RTLdrLoad() images and must be NULL.
279 * @param BaseAddress Image load address.
280 * Not supported for RTLdrLoad() images and must be 0.
281 * @param pszSymbol Symbol name.
282 * @param pValue Where to store the symbol value.
283 */
284RTDECL(int) RTLdrGetSymbolEx(RTLDRMOD hLdrMod, const void *pvBits, RTUINTPTR BaseAddress, const char *pszSymbol, RTUINTPTR *pValue)
285{
286 LogFlow(("RTLdrGetSymbolEx: hLdrMod=%RTldrm pvBits=%p BaseAddress=%RTptr pszSymbol=%p:{%s} pValue\n",
287 hLdrMod, pvBits, BaseAddress, pszSymbol, pszSymbol, pValue));
288
289 /*
290 * Validate input.
291 */
292 AssertMsgReturn(rtldrIsValid(hLdrMod), ("hLdrMod=%p\n", hLdrMod), VERR_INVALID_HANDLE);
293 AssertMsgReturn(!pvBits || VALID_PTR(pvBits), ("pvBits=%p\n", pvBits), VERR_INVALID_PARAMETER);
294 AssertMsgReturn(pszSymbol, ("pszSymbol=%p\n", pszSymbol), VERR_INVALID_PARAMETER);
295 AssertMsgReturn(VALID_PTR(pValue), ("pValue=%p\n", pvBits), VERR_INVALID_PARAMETER);
296 PRTLDRMODINTERNAL pMod = (PRTLDRMODINTERNAL)hLdrMod;
297 //AssertMsgReturn(pMod->eState == LDR_STATE_OPENED, ("eState=%d\n", pMod->eState), VERR_WRONG_ORDER);
298
299 /*
300 * Do it.
301 */
302 int rc;
303 if (pMod->pOps->pfnGetSymbolEx)
304 rc = pMod->pOps->pfnGetSymbolEx(pMod, pvBits, BaseAddress, pszSymbol, pValue);
305 else if (!BaseAddress && !pvBits)
306 {
307 void *pvValue;
308 rc = pMod->pOps->pfnGetSymbol(pMod, pszSymbol, &pvValue);
309 if (RT_SUCCESS(rc))
310 *pValue = (uintptr_t)pvValue;
311 }
312 else
313 AssertMsgFailedReturn(("BaseAddress=%RTptr pvBits=%p\n", BaseAddress, pvBits), VERR_INVALID_FUNCTION);
314 LogFlow(("RTLdrGetSymbolEx: returns %Rrc *pValue=%p\n", rc, *pValue));
315 return rc;
316}
317
318
319/**
320 * Enumerates all symbols in a module.
321 *
322 * @returns iprt status code.
323 * @param hLdrMod The loader module handle.
324 * @param fFlags Flags indicating what to return and such.
325 * @param pvBits Optional pointer to the loaded image.
326 * Set this to NULL if no RTLdrGetBits() processed image bits are available.
327 * @param BaseAddress Image load address.
328 * @param pfnCallback Callback function.
329 * @param pvUser User argument for the callback.
330 * @remark Not supported for RTLdrLoad() images.
331 */
332RTDECL(int) RTLdrEnumSymbols(RTLDRMOD hLdrMod, unsigned fFlags, const void *pvBits, RTUINTPTR BaseAddress, PFNRTLDRENUMSYMS pfnCallback, void *pvUser)
333{
334 LogFlow(("RTLdrEnumSymbols: hLdrMod=%RTldrm fFlags=%#x pvBit=%p BaseAddress=%RTptr pfnCallback=%p pvUser=%p\n",
335 hLdrMod, fFlags, pvBits, BaseAddress, pfnCallback, pvUser));
336
337 /*
338 * Validate input.
339 */
340 AssertMsgReturn(rtldrIsValid(hLdrMod), ("hLdrMod=%p\n", hLdrMod), VERR_INVALID_HANDLE);
341 AssertMsgReturn(!pvBits || VALID_PTR(pvBits), ("pvBits=%p\n", pvBits), VERR_INVALID_PARAMETER);
342 AssertMsgReturn(VALID_PTR(pfnCallback), ("pfnCallback=%p\n", pfnCallback), VERR_INVALID_PARAMETER);
343 PRTLDRMODINTERNAL pMod = (PRTLDRMODINTERNAL)hLdrMod;
344 //AssertMsgReturn(pMod->eState == LDR_STATE_OPENED, ("eState=%d\n", pMod->eState), VERR_WRONG_ORDER);
345
346 /*
347 * Do it.
348 */
349 int rc = pMod->pOps->pfnEnumSymbols(pMod, fFlags, pvBits, BaseAddress, pfnCallback, pvUser);
350 LogFlow(("RTLdrEnumSymbols: returns %Rrc\n", rc));
351 return rc;
352}
353
Note: See TracBrowser for help on using the repository browser.

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