1 | /* $Id: ldr.h 16933 2009-02-18 23:42:57Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Loader Internals.
|
---|
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 | #ifndef ___internal_ldr_h
|
---|
32 | #define ___internal_ldr_h
|
---|
33 |
|
---|
34 | #include <iprt/types.h>
|
---|
35 | #include "internal/magics.h"
|
---|
36 |
|
---|
37 | __BEGIN_DECLS
|
---|
38 |
|
---|
39 |
|
---|
40 | /*******************************************************************************
|
---|
41 | * Defined Constants And Macros *
|
---|
42 | *******************************************************************************/
|
---|
43 | #ifdef __DOXYGEN__
|
---|
44 | /** @def LDR_WITH_NATIVE
|
---|
45 | * Define this to get native support. */
|
---|
46 | # define LDR_WITH_NATIVE
|
---|
47 |
|
---|
48 | /** @def LDR_WITH_ELF32
|
---|
49 | * Define this to get 32-bit ELF support. */
|
---|
50 | # define LDR_WITH_ELF32
|
---|
51 |
|
---|
52 | /** @def LDR_WITH_ELF64
|
---|
53 | * Define this to get 64-bit ELF support. */
|
---|
54 | # define LDR_WITH_ELF64
|
---|
55 |
|
---|
56 | /** @def LDR_WITH_PE
|
---|
57 | * Define this to get 32-bit and 64-bit PE support. */
|
---|
58 | # define LDR_WITH_PE
|
---|
59 |
|
---|
60 | /** @def LDR_WITH_LX
|
---|
61 | * Define this to get LX support. */
|
---|
62 | # define LDR_WITH_LX
|
---|
63 |
|
---|
64 | /** @def LDR_WITH_MACHO
|
---|
65 | * Define this to get mach-o support (not implemented yet). */
|
---|
66 | # define LDR_WITH_MACHO
|
---|
67 | #endif /* __DOXYGEN__ */
|
---|
68 |
|
---|
69 | #if defined(LDR_WITH_ELF32) || defined(LDR_WITH_ELF64)
|
---|
70 | /** @def LDR_WITH_ELF
|
---|
71 | * This is defined if any of the ELF versions is requested.
|
---|
72 | */
|
---|
73 | # define LDR_WITH_ELF
|
---|
74 | #endif
|
---|
75 |
|
---|
76 | /* These two may clash with winnt.h. */
|
---|
77 | #undef IMAGE_DOS_SIGNATURE
|
---|
78 | #undef IMAGE_NT_SIGNATURE
|
---|
79 |
|
---|
80 |
|
---|
81 | /** Little endian uint32_t ELF signature ("\x7fELF"). */
|
---|
82 | #define IMAGE_ELF_SIGNATURE (0x7f | ('E' << 8) | ('L' << 16) | ('F' << 24))
|
---|
83 | /** Little endian uint32_t PE signature ("PE\0\0"). */
|
---|
84 | #define IMAGE_NT_SIGNATURE 0x00004550
|
---|
85 | /** Little endian uint16_t LX signature ("LX") */
|
---|
86 | #define IMAGE_LX_SIGNATURE ('L' | ('X' << 8))
|
---|
87 | /** Little endian uint16_t LE signature ("LE") */
|
---|
88 | #define IMAGE_LE_SIGNATURE ('L' | ('E' << 8))
|
---|
89 | /** Little endian uint16_t NE signature ("NE") */
|
---|
90 | #define IMAGE_NE_SIGNATURE ('N' | ('E' << 8))
|
---|
91 | /** Little endian uint16_t MZ signature ("MZ"). */
|
---|
92 | #define IMAGE_DOS_SIGNATURE ('M' | ('Z' << 8))
|
---|
93 |
|
---|
94 |
|
---|
95 | /*******************************************************************************
|
---|
96 | * Structures and Typedefs *
|
---|
97 | *******************************************************************************/
|
---|
98 | /**
|
---|
99 | * Loader state.
|
---|
100 | */
|
---|
101 | typedef enum RTLDRSTATE
|
---|
102 | {
|
---|
103 | /** Invalid. */
|
---|
104 | LDR_STATE_INVALID = 0,
|
---|
105 | /** Opened. */
|
---|
106 | LDR_STATE_OPENED,
|
---|
107 | /** The image can no longer be relocated. */
|
---|
108 | LDR_STATE_DONE,
|
---|
109 | /** The image was loaded, not opened. */
|
---|
110 | LDR_STATE_LOADED,
|
---|
111 | /** The usual 32-bit hack. */
|
---|
112 | LDR_STATE_32BIT_HACK = 0x7fffffff
|
---|
113 | } RTLDRSTATE;
|
---|
114 |
|
---|
115 |
|
---|
116 | /** Pointer to a loader item. */
|
---|
117 | typedef struct RTLDRMODINTERNAL *PRTLDRMODINTERNAL;
|
---|
118 |
|
---|
119 | /**
|
---|
120 | * Loader module operations.
|
---|
121 | */
|
---|
122 | typedef struct RTLDROPS
|
---|
123 | {
|
---|
124 | /** The name of the executable format. */
|
---|
125 | const char *pszName;
|
---|
126 |
|
---|
127 | /**
|
---|
128 | * Release any resources attached to the module.
|
---|
129 | * The caller will do RTMemFree on pMod on return.
|
---|
130 | *
|
---|
131 | * @returns iprt status code.
|
---|
132 | * @param pMod Pointer to the loader module structure.
|
---|
133 | * @remark Compulsory entry point.
|
---|
134 | */
|
---|
135 | DECLCALLBACKMEMBER(int, pfnClose)(PRTLDRMODINTERNAL pMod);
|
---|
136 |
|
---|
137 | /**
|
---|
138 | * Gets a simple symbol.
|
---|
139 | * This entrypoint can be omitted if RTLDROPS::pfnGetSymbolEx() is provided.
|
---|
140 | *
|
---|
141 | * @returns iprt status code.
|
---|
142 | * @param pMod Pointer to the loader module structure.
|
---|
143 | * @param pszSymbol The symbol name.
|
---|
144 | * @param ppvValue Where to store the symbol value.
|
---|
145 | */
|
---|
146 | DECLCALLBACKMEMBER(int, pfnGetSymbol)(PRTLDRMODINTERNAL pMod, const char *pszSymbol, void **ppvValue);
|
---|
147 |
|
---|
148 | /**
|
---|
149 | * Called when we're done with getting bits and relocating them.
|
---|
150 | * This is used to release resources used by the loader to support those actions.
|
---|
151 | *
|
---|
152 | * After this call none of the extended loader functions can be called.
|
---|
153 | *
|
---|
154 | * @returns iprt status code.
|
---|
155 | * @param pMod Pointer to the loader module structure.
|
---|
156 | * @remark This is an optional entry point.
|
---|
157 | */
|
---|
158 | DECLCALLBACKMEMBER(int, pfnDone)(PRTLDRMODINTERNAL pMod);
|
---|
159 |
|
---|
160 | /**
|
---|
161 | * Enumerates the symbols exported by the module.
|
---|
162 | *
|
---|
163 | * @returns iprt status code, which might have been returned by pfnCallback.
|
---|
164 | * @param pMod Pointer to the loader module structure.
|
---|
165 | * @param fFlags Flags indicating what to return and such.
|
---|
166 | * @param pvBits Pointer to the bits returned by RTLDROPS::pfnGetBits(), optional.
|
---|
167 | * @param BaseAddress The image base addressto use when calculating the symbol values.
|
---|
168 | * @param pfnCallback The callback function which each symbol is to be feeded to.
|
---|
169 | * @param pvUser User argument to pass to the enumerator.
|
---|
170 | * @remark This is an optional entry point.
|
---|
171 | */
|
---|
172 | DECLCALLBACKMEMBER(int, pfnEnumSymbols)(PRTLDRMODINTERNAL pMod, unsigned fFlags, const void *pvBits, RTUINTPTR BaseAddress,
|
---|
173 | PFNRTLDRENUMSYMS pfnCallback, void *pvUser);
|
---|
174 |
|
---|
175 |
|
---|
176 | /* extended functions: */
|
---|
177 |
|
---|
178 | /**
|
---|
179 | * Gets the size of the loaded image (i.e. in memory).
|
---|
180 | *
|
---|
181 | * @returns in memory size, in bytes.
|
---|
182 | * @returns ~(size_t)0 if it's not an extended image.
|
---|
183 | * @param pMod Pointer to the loader module structure.
|
---|
184 | * @remark Extended loader feature.
|
---|
185 | */
|
---|
186 | DECLCALLBACKMEMBER(size_t, pfnGetImageSize)(PRTLDRMODINTERNAL pMod);
|
---|
187 |
|
---|
188 | /**
|
---|
189 | * Gets the image bits fixed up for a specified address.
|
---|
190 | *
|
---|
191 | * @returns iprt status code.
|
---|
192 | * @param pMod Pointer to the loader module structure.
|
---|
193 | * @param pvBits Where to store the bits. The size of this buffer is equal or
|
---|
194 | * larger to the value returned by pfnGetImageSize().
|
---|
195 | * @param BaseAddress The base address which the image should be fixed up to.
|
---|
196 | * @param pfnGetImport The callback function to use to resolve imports (aka unresolved externals).
|
---|
197 | * @param pvUser User argument to pass to the callback.
|
---|
198 | * @remark Extended loader feature.
|
---|
199 | */
|
---|
200 | DECLCALLBACKMEMBER(int, pfnGetBits)(PRTLDRMODINTERNAL pMod, void *pvBits, RTUINTPTR BaseAddress, PFNRTLDRIMPORT pfnGetImport, void *pvUser);
|
---|
201 |
|
---|
202 | /**
|
---|
203 | * Relocate bits obtained using pfnGetBits to a new address.
|
---|
204 | *
|
---|
205 | * @returns iprt status code.
|
---|
206 | * @param pMod Pointer to the loader module structure.
|
---|
207 | * @param pvBits Where to store the bits. The size of this buffer is equal or
|
---|
208 | * larger to the value returned by pfnGetImageSize().
|
---|
209 | * @param NewBaseAddress The base address which the image should be fixed up to.
|
---|
210 | * @param OldBaseAddress The base address which the image is currently fixed up to.
|
---|
211 | * @param pfnGetImport The callback function to use to resolve imports (aka unresolved externals).
|
---|
212 | * @param pvUser User argument to pass to the callback.
|
---|
213 | * @remark Extended loader feature.
|
---|
214 | */
|
---|
215 | DECLCALLBACKMEMBER(int, pfnRelocate)(PRTLDRMODINTERNAL pMod, void *pvBits, RTUINTPTR NewBaseAddress, RTUINTPTR OldBaseAddress, PFNRTLDRIMPORT pfnGetImport, void *pvUser);
|
---|
216 |
|
---|
217 | /**
|
---|
218 | * Gets a symbol with special base address and stuff.
|
---|
219 | * This entrypoint can be omitted if RTLDROPS::pfnGetSymbolEx() is provided and the special BaseAddress feature isn't supported.
|
---|
220 | *
|
---|
221 | * @returns iprt status code.
|
---|
222 | * @param pMod Pointer to the loader module structure.
|
---|
223 | * @param pvBits Pointer to bits returned by RTLDROPS::pfnGetBits(), optional.
|
---|
224 | * @param BaseAddress The image base address to use when calculating the symbol value.
|
---|
225 | * @param pszSymbol The symbol name.
|
---|
226 | * @param pValue Where to store the symbol value.
|
---|
227 | * @remark Extended loader feature.
|
---|
228 | */
|
---|
229 | DECLCALLBACKMEMBER(int, pfnGetSymbolEx)(PRTLDRMODINTERNAL pMod, const void *pvBits, RTUINTPTR BaseAddress, const char *pszSymbol, RTUINTPTR *pValue);
|
---|
230 |
|
---|
231 | /** Dummy entry to make sure we've initialized it all. */
|
---|
232 | RTUINT uDummy;
|
---|
233 | } RTLDROPS;
|
---|
234 | typedef RTLDROPS *PRTLDROPS;
|
---|
235 | typedef const RTLDROPS *PCRTLDROPS;
|
---|
236 |
|
---|
237 |
|
---|
238 | /** Pointer to a loader reader instance. */
|
---|
239 | typedef struct RTLDRREADER *PRTLDRREADER;
|
---|
240 |
|
---|
241 | /**
|
---|
242 | * Loader image reader instance.
|
---|
243 | * The reader will have extra data members following this structure.
|
---|
244 | */
|
---|
245 | typedef struct RTLDRREADER
|
---|
246 | {
|
---|
247 | /** The name of the image provider. */
|
---|
248 | const char *pszName;
|
---|
249 |
|
---|
250 | /**
|
---|
251 | * Reads bytes at a give place in the raw image.
|
---|
252 | *
|
---|
253 | * @returns iprt status code.
|
---|
254 | * @param pReader Pointer to the reader instance.
|
---|
255 | * @param pvBuf Where to store the bits.
|
---|
256 | * @param cb Number of bytes to read.
|
---|
257 | * @param off Where to start reading relative to the start of the raw image.
|
---|
258 | */
|
---|
259 | DECLCALLBACKMEMBER(int, pfnRead)(PRTLDRREADER pReader, void *pvBuf, size_t cb, RTFOFF off);
|
---|
260 |
|
---|
261 | /**
|
---|
262 | * Tells end position of last read.
|
---|
263 | *
|
---|
264 | * @returns position relative to start of the raw image.
|
---|
265 | * @param pReader Pointer to the reader instance.
|
---|
266 | */
|
---|
267 | DECLCALLBACKMEMBER(RTFOFF, pfnTell)(PRTLDRREADER pReader);
|
---|
268 |
|
---|
269 | /**
|
---|
270 | * Gets the size of the raw image bits.
|
---|
271 | *
|
---|
272 | * @returns size of raw image bits in bytes.
|
---|
273 | * @param pReader Pointer to the reader instance.
|
---|
274 | */
|
---|
275 | DECLCALLBACKMEMBER(RTFOFF, pfnSize)(PRTLDRREADER pReader);
|
---|
276 |
|
---|
277 | /**
|
---|
278 | * Map the bits into memory.
|
---|
279 | *
|
---|
280 | * The mapping will be freed upon calling pfnDestroy() if not pfnUnmap()
|
---|
281 | * is called before that. The mapping is read only.
|
---|
282 | *
|
---|
283 | * @returns iprt status code.
|
---|
284 | * @param pReader Pointer to the reader instance.
|
---|
285 | * @param ppvBits Where to store the address of the memory mapping on success.
|
---|
286 | * The size of the mapping can be obtained by calling pfnSize().
|
---|
287 | */
|
---|
288 | DECLCALLBACKMEMBER(int, pfnMap)(PRTLDRREADER pReader, const void **ppvBits);
|
---|
289 |
|
---|
290 | /**
|
---|
291 | * Unmap bits.
|
---|
292 | *
|
---|
293 | * @returns iprt status code.
|
---|
294 | * @param pReader Pointer to the reader instance.
|
---|
295 | * @param pvBits Memory pointer returned by pfnMap().
|
---|
296 | */
|
---|
297 | DECLCALLBACKMEMBER(int, pfnUnmap)(PRTLDRREADER pReader, const void *pvBits);
|
---|
298 |
|
---|
299 | /**
|
---|
300 | * Gets the most appropriate log name.
|
---|
301 | *
|
---|
302 | * @returns Pointer to readonly log name.
|
---|
303 | * @param pReader Pointer to the reader instance.
|
---|
304 | */
|
---|
305 | DECLCALLBACKMEMBER(const char *, pfnLogName)(PRTLDRREADER pReader);
|
---|
306 |
|
---|
307 | /**
|
---|
308 | * Releases all resources associated with the reader instance.
|
---|
309 | * The instance is invalid after this call returns.
|
---|
310 | *
|
---|
311 | * @returns iprt status code.
|
---|
312 | * @param pReader Pointer to the reader instance.
|
---|
313 | */
|
---|
314 | DECLCALLBACKMEMBER(int, pfnDestroy)(PRTLDRREADER pReader);
|
---|
315 |
|
---|
316 | } RTLDRREADER;
|
---|
317 |
|
---|
318 |
|
---|
319 | /**
|
---|
320 | * Loader module core.
|
---|
321 | */
|
---|
322 | typedef struct RTLDRMODINTERNAL
|
---|
323 | {
|
---|
324 | /** The loader magic value (RTLDRMOD_MAGIC). */
|
---|
325 | uint32_t u32Magic;
|
---|
326 | /** State. */
|
---|
327 | RTLDRSTATE eState;
|
---|
328 | /** Loader ops. */
|
---|
329 | PCRTLDROPS pOps;
|
---|
330 | } RTLDRMODINTERNAL;
|
---|
331 |
|
---|
332 |
|
---|
333 | /**
|
---|
334 | * Validates that a loader module handle is valid.
|
---|
335 | *
|
---|
336 | * @returns true if valid.
|
---|
337 | * @returns false if invalid.
|
---|
338 | * @param hLdrMod The loader module handle.
|
---|
339 | */
|
---|
340 | DECLINLINE(bool) rtldrIsValid(RTLDRMOD hLdrMod)
|
---|
341 | {
|
---|
342 | return VALID_PTR(hLdrMod)
|
---|
343 | && ((PRTLDRMODINTERNAL)hLdrMod)->u32Magic == RTLDRMOD_MAGIC;
|
---|
344 | }
|
---|
345 |
|
---|
346 | int rtldrOpenWithReader(PRTLDRREADER pReader, uint32_t fFlags, RTLDRARCH enmArch, PRTLDRMOD phMod);
|
---|
347 |
|
---|
348 |
|
---|
349 | /**
|
---|
350 | * Native loader module.
|
---|
351 | */
|
---|
352 | typedef struct RTLDRMODNATIVE
|
---|
353 | {
|
---|
354 | /** The core structure. */
|
---|
355 | RTLDRMODINTERNAL Core;
|
---|
356 | /** The native handle. */
|
---|
357 | uintptr_t hNative;
|
---|
358 | } RTLDRMODNATIVE, *PRTLDRMODNATIVE;
|
---|
359 |
|
---|
360 | /** @copydoc RTLDROPS::pfnGetSymbol */
|
---|
361 | DECLCALLBACK(int) rtldrNativeGetSymbol(PRTLDRMODINTERNAL pMod, const char *pszSymbol, void **ppvValue);
|
---|
362 | /** @copydoc RTLDROPS::pfnClose */
|
---|
363 | DECLCALLBACK(int) rtldrNativeClose(PRTLDRMODINTERNAL pMod);
|
---|
364 |
|
---|
365 | /**
|
---|
366 | * Load a native module using the native loader.
|
---|
367 | *
|
---|
368 | * @returns iprt status code.
|
---|
369 | * @param pszFilename The image filename.
|
---|
370 | * @param phHandle Where to store the module handle on success.
|
---|
371 | */
|
---|
372 | int rtldrNativeLoad(const char *pszFilename, uintptr_t *phHandle);
|
---|
373 |
|
---|
374 | int rtldrPEOpen(PRTLDRREADER pReader, uint32_t fFlags, RTLDRARCH enmArch, RTFOFF offNtHdrs, PRTLDRMOD phLdrMod);
|
---|
375 | int rtldrELFOpen(PRTLDRREADER pReader, uint32_t fFlags, RTLDRARCH enmArch, PRTLDRMOD phLdrMod);
|
---|
376 | int rtldrkLdrOpen(PRTLDRREADER pReader, uint32_t fFlags, RTLDRARCH enmArch, PRTLDRMOD phLdrMod);
|
---|
377 | /*int rtldrLXOpen(PRTLDRREADER pReader, uint32_t fFlags, RTLDRARCH enmArch, RTFOFF offLX, PRTLDRMOD phLdrMod);
|
---|
378 | int rtldrMachoOpen(PRTLDRREADER pReader, uint32_t fFlags, RTLDRARCH enmArch, RTFOFF offSomething, PRTLDRMOD phLdrMod);*/
|
---|
379 |
|
---|
380 |
|
---|
381 | __END_DECLS
|
---|
382 |
|
---|
383 | #endif
|
---|
384 |
|
---|