VirtualBox

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

Last change on this file since 14049 was 13837, checked in by vboxsync, 16 years ago

s/%Vr\([acfs]\)/%Rr\1/g - since I'm upsetting everyone anyway, better make the most of it...

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