VirtualBox

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

Last change on this file since 1 was 1, checked in by vboxsync, 55 years ago

import

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