VirtualBox

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

Last change on this file since 17019 was 16933, checked in by vboxsync, 16 years ago

IPRT/PDM,SUPLIb,REM: Extended RTLdrOpen with an architecture argument for use with FAT R0.r0 images later some day. Also added fFlags argument that's currently MBZ case.

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