VirtualBox

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

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