VirtualBox

source: vbox/trunk/src/VBox/Runtime/ldrEx.cpp@ 977

Last change on this file since 977 was 894, checked in by vboxsync, 18 years ago

kLdr integration. tstLdrObj.gc now works on darwin.

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