VirtualBox

source: vbox/trunk/src/VBox/Runtime/include/internal/ldr.h@ 2413

Last change on this file since 2413 was 1816, checked in by vboxsync, 18 years ago

moved magics to a common header to avoid duplicating the same defines all over the place.

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