1 | /* $Id: ldrEx.cpp 33540 2010-10-28 09:27:05Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Binary Image Loader, Extended Features.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 Oracle Corporation
|
---|
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 "internal/iprt.h"
|
---|
34 |
|
---|
35 | #include <iprt/alloc.h>
|
---|
36 | #include <iprt/assert.h>
|
---|
37 | #include <iprt/log.h>
|
---|
38 | #include <iprt/string.h>
|
---|
39 | #include <iprt/err.h>
|
---|
40 | #include "internal/ldr.h"
|
---|
41 | #include "internal/ldrMZ.h"
|
---|
42 |
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * Open part with reader.
|
---|
46 | *
|
---|
47 | * @returns iprt status code.
|
---|
48 | * @param pReader The loader reader instance which will provide the raw image bits.
|
---|
49 | * @param fFlags Reserved, MBZ.
|
---|
50 | * @param enmArch Architecture specifier.
|
---|
51 | * @param phMod Where to store the handle.
|
---|
52 | */
|
---|
53 | int rtldrOpenWithReader(PRTLDRREADER pReader, uint32_t fFlags, RTLDRARCH enmArch, 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 interpreter instance depending on the signature.
|
---|
104 | */
|
---|
105 | if (uSign.u32 == IMAGE_NT_SIGNATURE)
|
---|
106 | #ifdef LDR_WITH_PE
|
---|
107 | rc = rtldrPEOpen(pReader, fFlags, enmArch, 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, fFlags, enmArch, 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, fFlags, enmArch, 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, fFlags, enmArch, 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, fFlags, enmArch, 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, fFlags, enmArch, 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, fFlags, enmArch, 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, fFlags, enmArch, 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 | */
|
---|
177 | RTDECL(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 | RT_EXPORT_SYMBOL(RTLdrSize);
|
---|
196 |
|
---|
197 |
|
---|
198 | /**
|
---|
199 | * Loads the image into a buffer provided by the user and applies fixups
|
---|
200 | * for the given base address.
|
---|
201 | *
|
---|
202 | * @returns iprt status code.
|
---|
203 | * @param hLdrMod The load module handle.
|
---|
204 | * @param pvBits Where to put the bits.
|
---|
205 | * Must be as large as RTLdrSize() suggests.
|
---|
206 | * @param BaseAddress The base address.
|
---|
207 | * @param pfnGetImport Callback function for resolving imports one by one.
|
---|
208 | * @param pvUser User argument for the callback.
|
---|
209 | * @remark Not supported for RTLdrLoad() images.
|
---|
210 | */
|
---|
211 | RTDECL(int) RTLdrGetBits(RTLDRMOD hLdrMod, void *pvBits, RTUINTPTR BaseAddress, PFNRTLDRIMPORT pfnGetImport, void *pvUser)
|
---|
212 | {
|
---|
213 | LogFlow(("RTLdrGetBits: hLdrMod=%RTldrm pvBits=%p BaseAddress=%RTptr pfnGetImport=%p pvUser=%p\n",
|
---|
214 | hLdrMod, pvBits, BaseAddress, pfnGetImport, pvUser));
|
---|
215 |
|
---|
216 | /*
|
---|
217 | * Validate input.
|
---|
218 | */
|
---|
219 | AssertMsgReturn(rtldrIsValid(hLdrMod), ("hLdrMod=%p\n", hLdrMod), VERR_INVALID_HANDLE);
|
---|
220 | AssertMsgReturn(VALID_PTR(pvBits), ("pvBits=%p\n", pvBits), VERR_INVALID_PARAMETER);
|
---|
221 | AssertMsgReturn(VALID_PTR(pfnGetImport), ("pfnGetImport=%p\n", pfnGetImport), VERR_INVALID_PARAMETER);
|
---|
222 | PRTLDRMODINTERNAL pMod = (PRTLDRMODINTERNAL)hLdrMod;
|
---|
223 | AssertMsgReturn(pMod->eState == LDR_STATE_OPENED, ("eState=%d\n", pMod->eState), VERR_WRONG_ORDER);
|
---|
224 |
|
---|
225 | /*
|
---|
226 | * Do it.
|
---|
227 | */
|
---|
228 | int rc = pMod->pOps->pfnGetBits(pMod, pvBits, BaseAddress, pfnGetImport, pvUser);
|
---|
229 | LogFlow(("RTLdrGetBits: returns %Rrc\n",rc));
|
---|
230 | return rc;
|
---|
231 | }
|
---|
232 | RT_EXPORT_SYMBOL(RTLdrGetBits);
|
---|
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 have 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 | */
|
---|
249 | RTDECL(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 | RT_EXPORT_SYMBOL(RTLdrRelocate);
|
---|
272 |
|
---|
273 |
|
---|
274 | /**
|
---|
275 | * Gets the address of a named exported symbol.
|
---|
276 | *
|
---|
277 | * This function differs from the plain one in that it can deal with
|
---|
278 | * both GC and HC address sizes, and that it can calculate the symbol
|
---|
279 | * value relative to any given base address.
|
---|
280 | *
|
---|
281 | * @returns iprt status code.
|
---|
282 | * @param hLdrMod The loader module handle.
|
---|
283 | * @param pvBits Optional pointer to the loaded image.
|
---|
284 | * Set this to NULL if no RTLdrGetBits() processed image bits are available.
|
---|
285 | * Not supported for RTLdrLoad() images and must be NULL.
|
---|
286 | * @param BaseAddress Image load address.
|
---|
287 | * Not supported for RTLdrLoad() images and must be 0.
|
---|
288 | * @param pszSymbol Symbol name.
|
---|
289 | * @param pValue Where to store the symbol value.
|
---|
290 | */
|
---|
291 | RTDECL(int) RTLdrGetSymbolEx(RTLDRMOD hLdrMod, const void *pvBits, RTUINTPTR BaseAddress, const char *pszSymbol, RTUINTPTR *pValue)
|
---|
292 | {
|
---|
293 | LogFlow(("RTLdrGetSymbolEx: hLdrMod=%RTldrm pvBits=%p BaseAddress=%RTptr pszSymbol=%p:{%s} pValue\n",
|
---|
294 | hLdrMod, pvBits, BaseAddress, pszSymbol, pszSymbol, pValue));
|
---|
295 |
|
---|
296 | /*
|
---|
297 | * Validate input.
|
---|
298 | */
|
---|
299 | AssertMsgReturn(rtldrIsValid(hLdrMod), ("hLdrMod=%p\n", hLdrMod), VERR_INVALID_HANDLE);
|
---|
300 | AssertMsgReturn(!pvBits || VALID_PTR(pvBits), ("pvBits=%p\n", pvBits), VERR_INVALID_PARAMETER);
|
---|
301 | AssertMsgReturn(pszSymbol, ("pszSymbol=%p\n", pszSymbol), VERR_INVALID_PARAMETER);
|
---|
302 | AssertMsgReturn(VALID_PTR(pValue), ("pValue=%p\n", pvBits), VERR_INVALID_PARAMETER);
|
---|
303 | PRTLDRMODINTERNAL pMod = (PRTLDRMODINTERNAL)hLdrMod;
|
---|
304 | //AssertMsgReturn(pMod->eState == LDR_STATE_OPENED, ("eState=%d\n", pMod->eState), VERR_WRONG_ORDER);
|
---|
305 |
|
---|
306 | /*
|
---|
307 | * Do it.
|
---|
308 | */
|
---|
309 | int rc;
|
---|
310 | if (pMod->pOps->pfnGetSymbolEx)
|
---|
311 | rc = pMod->pOps->pfnGetSymbolEx(pMod, pvBits, BaseAddress, pszSymbol, pValue);
|
---|
312 | else if (!BaseAddress && !pvBits)
|
---|
313 | {
|
---|
314 | void *pvValue;
|
---|
315 | rc = pMod->pOps->pfnGetSymbol(pMod, pszSymbol, &pvValue);
|
---|
316 | if (RT_SUCCESS(rc))
|
---|
317 | *pValue = (uintptr_t)pvValue;
|
---|
318 | }
|
---|
319 | else
|
---|
320 | AssertMsgFailedReturn(("BaseAddress=%RTptr pvBits=%p\n", BaseAddress, pvBits), VERR_INVALID_FUNCTION);
|
---|
321 | LogFlow(("RTLdrGetSymbolEx: returns %Rrc *pValue=%p\n", rc, *pValue));
|
---|
322 | return rc;
|
---|
323 | }
|
---|
324 | RT_EXPORT_SYMBOL(RTLdrGetSymbolEx);
|
---|
325 |
|
---|
326 |
|
---|
327 | /**
|
---|
328 | * Enumerates all symbols in a module.
|
---|
329 | *
|
---|
330 | * @returns iprt status code.
|
---|
331 | * @param hLdrMod The loader module handle.
|
---|
332 | * @param fFlags Flags indicating what to return and such.
|
---|
333 | * @param pvBits Optional pointer to the loaded image.
|
---|
334 | * Set this to NULL if no RTLdrGetBits() processed image bits are available.
|
---|
335 | * @param BaseAddress Image load address.
|
---|
336 | * @param pfnCallback Callback function.
|
---|
337 | * @param pvUser User argument for the callback.
|
---|
338 | * @remark Not supported for RTLdrLoad() images.
|
---|
339 | */
|
---|
340 | RTDECL(int) RTLdrEnumSymbols(RTLDRMOD hLdrMod, unsigned fFlags, const void *pvBits, RTUINTPTR BaseAddress, PFNRTLDRENUMSYMS pfnCallback, void *pvUser)
|
---|
341 | {
|
---|
342 | LogFlow(("RTLdrEnumSymbols: hLdrMod=%RTldrm fFlags=%#x pvBit=%p BaseAddress=%RTptr pfnCallback=%p pvUser=%p\n",
|
---|
343 | hLdrMod, fFlags, pvBits, BaseAddress, pfnCallback, pvUser));
|
---|
344 |
|
---|
345 | /*
|
---|
346 | * Validate input.
|
---|
347 | */
|
---|
348 | AssertMsgReturn(rtldrIsValid(hLdrMod), ("hLdrMod=%p\n", hLdrMod), VERR_INVALID_HANDLE);
|
---|
349 | AssertMsgReturn(!pvBits || VALID_PTR(pvBits), ("pvBits=%p\n", pvBits), VERR_INVALID_PARAMETER);
|
---|
350 | AssertMsgReturn(VALID_PTR(pfnCallback), ("pfnCallback=%p\n", pfnCallback), VERR_INVALID_PARAMETER);
|
---|
351 | PRTLDRMODINTERNAL pMod = (PRTLDRMODINTERNAL)hLdrMod;
|
---|
352 | //AssertMsgReturn(pMod->eState == LDR_STATE_OPENED, ("eState=%d\n", pMod->eState), VERR_WRONG_ORDER);
|
---|
353 |
|
---|
354 | /*
|
---|
355 | * Do it.
|
---|
356 | */
|
---|
357 | int rc = pMod->pOps->pfnEnumSymbols(pMod, fFlags, pvBits, BaseAddress, pfnCallback, pvUser);
|
---|
358 | LogFlow(("RTLdrEnumSymbols: returns %Rrc\n", rc));
|
---|
359 | return rc;
|
---|
360 | }
|
---|
361 | RT_EXPORT_SYMBOL(RTLdrEnumSymbols);
|
---|
362 |
|
---|