1 | /** @file
|
---|
2 | * innotek Portable Runtime - Loader.
|
---|
3 | */
|
---|
4 |
|
---|
5 | /*
|
---|
6 | * Copyright (C) 2006-2007 innotek GmbH
|
---|
7 | *
|
---|
8 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
9 | * available from http://www.virtualbox.org. This file is free software;
|
---|
10 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
11 | * General Public License (GPL) as published by the Free Software
|
---|
12 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
13 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
14 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
15 | *
|
---|
16 | * The contents of this file may alternatively be used under the terms
|
---|
17 | * of the Common Development and Distribution License Version 1.0
|
---|
18 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
19 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
20 | * CDDL are applicable instead of those of the GPL.
|
---|
21 | *
|
---|
22 | * You may elect to license modified versions of this file under the
|
---|
23 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
24 | */
|
---|
25 |
|
---|
26 | #ifndef ___iprt_ldr_h
|
---|
27 | #define ___iprt_ldr_h
|
---|
28 |
|
---|
29 | #include <iprt/cdefs.h>
|
---|
30 | #include <iprt/types.h>
|
---|
31 |
|
---|
32 |
|
---|
33 | /** @defgroup grp_ldr RTLdr - Loader
|
---|
34 | * @ingroup grp_rt
|
---|
35 | * @{
|
---|
36 | */
|
---|
37 |
|
---|
38 | __BEGIN_DECLS
|
---|
39 |
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * Loads a dynamic load library (/shared object) image file using native
|
---|
43 | * OS facilities.
|
---|
44 | *
|
---|
45 | * The filename will be appended the default DLL/SO extension of
|
---|
46 | * the platform if it have been omitted. This means that it's not
|
---|
47 | * possible to load DLLs/SOs with no extension using this interface,
|
---|
48 | * but that's not a bad tradeoff.
|
---|
49 | *
|
---|
50 | * If no path is specified in the filename, the OS will usually search it's library
|
---|
51 | * path to find the image file.
|
---|
52 | *
|
---|
53 | * @returns iprt status code.
|
---|
54 | * @param pszFilename Image filename.
|
---|
55 | * @param phLdrMod Where to store the handle to the loader module.
|
---|
56 | */
|
---|
57 | RTDECL(int) RTLdrLoad(const char *pszFilename, PRTLDRMOD phLdrMod);
|
---|
58 |
|
---|
59 | /**
|
---|
60 | * Open a binary image file.
|
---|
61 | *
|
---|
62 | * @returns iprt status code.
|
---|
63 | * @param pszFilename Image filename.
|
---|
64 | * @param phLdrMod Where to store the handle to the loader module.
|
---|
65 | */
|
---|
66 | RTDECL(int) RTLdrOpen(const char *pszFilename, PRTLDRMOD phLdrMod);
|
---|
67 |
|
---|
68 | /**
|
---|
69 | * Opens a binary image file using kLdr.
|
---|
70 | *
|
---|
71 | * @returns iprt status code.
|
---|
72 | * @param pszFilename Image filename.
|
---|
73 | * @param phLdrMod Where to store the handle to the loaded module.
|
---|
74 | * @remark Primarily for testing the loader.
|
---|
75 | */
|
---|
76 | RTDECL(int) RTLdrOpenkLdr(const char *pszFilename, PRTLDRMOD phLdrMod);
|
---|
77 |
|
---|
78 | /**
|
---|
79 | * What to expect and do with the bits passed to RTLdrOpenBits().
|
---|
80 | */
|
---|
81 | typedef enum RTLDROPENBITS
|
---|
82 | {
|
---|
83 | /** The usual invalid 0 entry. */
|
---|
84 | RTLDROPENBITS_INVALID = 0,
|
---|
85 | /** The bits are readonly and will never be changed. */
|
---|
86 | RTLDROPENBITS_READONLY,
|
---|
87 | /** The bits are going to be changed and the loader will have to duplicate them
|
---|
88 | * when opening the image. */
|
---|
89 | RTLDROPENBITS_WRITABLE,
|
---|
90 | /** The bits are both the source and destination for the loader operation.
|
---|
91 | * This means that the loader may have to duplicate them prior to changing them. */
|
---|
92 | RTLDROPENBITS_SRC_AND_DST,
|
---|
93 | /** The end of the valid enums. This entry marks the
|
---|
94 | * first invalid entry.. */
|
---|
95 | RTLDROPENBITS_END,
|
---|
96 | RTLDROPENBITS_32BIT_HACK = 0x7fffffff
|
---|
97 | } RTLDROPENBITS;
|
---|
98 |
|
---|
99 | /**
|
---|
100 | * Open a binary image from in-memory bits.
|
---|
101 | *
|
---|
102 | * @returns iprt status code.
|
---|
103 | * @param pvBits The start of the raw-image.
|
---|
104 | * @param cbBits The size of the raw-image.
|
---|
105 | * @param enmBits What to expect from the pvBits.
|
---|
106 | * @param pszLogName What to call the raw-image when logging.
|
---|
107 | * For RTLdrLoad and RTLdrOpen the filename is used for this.
|
---|
108 | * @param phLdrMod Where to store the handle to the loader module.
|
---|
109 | */
|
---|
110 | RTDECL(int) RTLdrOpenBits(const void *pvBits, size_t cbBits, RTLDROPENBITS enmBits, const char *pszLogName, PRTLDRMOD phLdrMod);
|
---|
111 |
|
---|
112 | /**
|
---|
113 | * Closes a loader module handle.
|
---|
114 | *
|
---|
115 | * The handle can be obtained using any of the RTLdrLoad(), RTLdrOpen()
|
---|
116 | * and RTLdrOpenBits() functions.
|
---|
117 | *
|
---|
118 | * @returns iprt status code.
|
---|
119 | * @param hLdrMod The loader module handle.
|
---|
120 | */
|
---|
121 | RTDECL(int) RTLdrClose(RTLDRMOD hLdrMod);
|
---|
122 |
|
---|
123 | /**
|
---|
124 | * Gets the address of a named exported symbol.
|
---|
125 | *
|
---|
126 | * @returns iprt status code.
|
---|
127 | * @param hLdrMod The loader module handle.
|
---|
128 | * @param pszSymbol Symbol name.
|
---|
129 | * @param ppvValue Where to store the symbol value. Note that this is restricted to the
|
---|
130 | * pointer size used on the host!
|
---|
131 | */
|
---|
132 | RTDECL(int) RTLdrGetSymbol(RTLDRMOD hLdrMod, const char *pszSymbol, void **ppvValue);
|
---|
133 |
|
---|
134 | /**
|
---|
135 | * Gets the address of a named exported symbol.
|
---|
136 | *
|
---|
137 | * This function differs from the plain one in that it can deal with
|
---|
138 | * both GC and HC address sizes, and that it can calculate the symbol
|
---|
139 | * value relative to any given base address.
|
---|
140 | *
|
---|
141 | * @returns iprt status code.
|
---|
142 | * @param hLdrMod The loader module handle.
|
---|
143 | * @param pvBits Optional pointer to the loaded image.
|
---|
144 | * Set this to NULL if no RTLdrGetBits() processed image bits are available.
|
---|
145 | * Not supported for RTLdrLoad() images.
|
---|
146 | * @param BaseAddress Image load address.
|
---|
147 | * Not supported for RTLdrLoad() images.
|
---|
148 | * @param pszSymbol Symbol name.
|
---|
149 | * @param pValue Where to store the symbol value.
|
---|
150 | */
|
---|
151 | RTDECL(int) RTLdrGetSymbolEx(RTLDRMOD hLdrMod, const void *pvBits, RTUINTPTR BaseAddress, const char *pszSymbol, RTUINTPTR *pValue);
|
---|
152 |
|
---|
153 | /**
|
---|
154 | * Gets the size of the loaded image.
|
---|
155 | * This is only supported for modules which has been opened using RTLdrOpen() and RTLdrOpenBits().
|
---|
156 | *
|
---|
157 | * @returns image size (in bytes).
|
---|
158 | * @returns ~(size_t)0 on if not opened by RTLdrOpen().
|
---|
159 | * @param hLdrMod Handle to the loader module.
|
---|
160 | * @remark Not supported for RTLdrLoad() images.
|
---|
161 | */
|
---|
162 | RTDECL(size_t) RTLdrSize(RTLDRMOD hLdrMod);
|
---|
163 |
|
---|
164 | /**
|
---|
165 | * Resolve an external symbol during RTLdrGetBits().
|
---|
166 | *
|
---|
167 | * @returns iprt status code.
|
---|
168 | * @param hLdrMod The loader module handle.
|
---|
169 | * @param pszModule Module name.
|
---|
170 | * @param pszSymbol Symbol name, NULL if uSymbol should be used.
|
---|
171 | * @param uSymbol Symbol ordinal, ~0 if pszSymbol should be used.
|
---|
172 | * @param pValue Where to store the symbol value (address).
|
---|
173 | * @param pvUser User argument.
|
---|
174 | */
|
---|
175 | typedef DECLCALLBACK(int) RTLDRIMPORT(RTLDRMOD hLdrMod, const char *pszModule, const char *pszSymbol, unsigned uSymbol, RTUINTPTR *pValue, void *pvUser);
|
---|
176 | /** Pointer to a FNRTLDRIMPORT() callback function. */
|
---|
177 | typedef RTLDRIMPORT *PFNRTLDRIMPORT;
|
---|
178 |
|
---|
179 | /**
|
---|
180 | * Loads the image into a buffer provided by the user and applies fixups
|
---|
181 | * for the given base address.
|
---|
182 | *
|
---|
183 | * @returns iprt status code.
|
---|
184 | * @param hLdrMod The load module handle.
|
---|
185 | * @param pvBits Where to put the bits.
|
---|
186 | * Must be as large as RTLdrSize() suggests.
|
---|
187 | * @param BaseAddress The base address.
|
---|
188 | * @param pfnGetImport Callback function for resolving imports one by one.
|
---|
189 | * @param pvUser User argument for the callback.
|
---|
190 | * @remark Not supported for RTLdrLoad() images.
|
---|
191 | */
|
---|
192 | RTDECL(int) RTLdrGetBits(RTLDRMOD hLdrMod, void *pvBits, RTUINTPTR BaseAddress, PFNRTLDRIMPORT pfnGetImport, void *pvUser);
|
---|
193 |
|
---|
194 | /**
|
---|
195 | * Relocates bits after getting them.
|
---|
196 | * Useful for code which moves around a bit.
|
---|
197 | *
|
---|
198 | * @returns iprt status code.
|
---|
199 | * @param hLdrMod The loader module handle.
|
---|
200 | * @param pvBits Where the image bits are.
|
---|
201 | * Must've been passed to RTLdrGetBits().
|
---|
202 | * @param NewBaseAddress The new base address.
|
---|
203 | * @param OldBaseAddress The old base address.
|
---|
204 | * @param pfnGetImport Callback function for resolving imports one by one.
|
---|
205 | * @param pvUser User argument for the callback.
|
---|
206 | * @remark Not supported for RTLdrLoad() images.
|
---|
207 | */
|
---|
208 | RTDECL(int) RTLdrRelocate(RTLDRMOD hLdrMod, void *pvBits, RTUINTPTR NewBaseAddress, RTUINTPTR OldBaseAddress,
|
---|
209 | PFNRTLDRIMPORT pfnGetImport, void *pvUser);
|
---|
210 |
|
---|
211 | /**
|
---|
212 | * Enumeration callback function used by RTLdrEnumSymbols().
|
---|
213 | *
|
---|
214 | * @returns iprt status code. Failure will stop the enumeration.
|
---|
215 | * @param hLdrMod The loader module handle.
|
---|
216 | * @param pszSymbol Symbol name. NULL if ordinal only.
|
---|
217 | * @param uSymbol Symbol ordinal, ~0 if not used.
|
---|
218 | * @param Value Symbol value.
|
---|
219 | * @param pvUser The user argument specified to RTLdrEnumSymbols().
|
---|
220 | */
|
---|
221 | typedef DECLCALLBACK(int) RTLDRENUMSYMS(RTLDRMOD hLdrMod, const char *pszSymbol, unsigned uSymbol, RTUINTPTR Value, void *pvUser);
|
---|
222 | /** Pointer to a RTLDRENUMSYMS() callback function. */
|
---|
223 | typedef RTLDRENUMSYMS *PFNRTLDRENUMSYMS;
|
---|
224 |
|
---|
225 | /**
|
---|
226 | * Enumerates all symbols in a module.
|
---|
227 | *
|
---|
228 | * @returns iprt status code.
|
---|
229 | * @param hLdrMod The loader module handle.
|
---|
230 | * @param fFlags Flags indicating what to return and such.
|
---|
231 | * @param pvBits Optional pointer to the loaded image. (RTLDR_ENUM_SYMBOL_FLAGS_*)
|
---|
232 | * Set this to NULL if no RTLdrGetBits() processed image bits are available.
|
---|
233 | * @param BaseAddress Image load address.
|
---|
234 | * @param pfnCallback Callback function.
|
---|
235 | * @param pvUser User argument for the callback.
|
---|
236 | * @remark Not supported for RTLdrLoad() images.
|
---|
237 | */
|
---|
238 | RTDECL(int) RTLdrEnumSymbols(RTLDRMOD hLdrMod, unsigned fFlags, const void *pvBits, RTUINTPTR BaseAddress, PFNRTLDRENUMSYMS pfnCallback, void *pvUser);
|
---|
239 |
|
---|
240 | /** @name RTLdrEnumSymbols flags.
|
---|
241 | * @{ */
|
---|
242 | /** Returns ALL kinds of symbols. The default is to only return public/exported symbols. */
|
---|
243 | #define RTLDR_ENUM_SYMBOL_FLAGS_ALL RT_BIT(1)
|
---|
244 | /** @} */
|
---|
245 |
|
---|
246 | __END_DECLS
|
---|
247 |
|
---|
248 | /** @} */
|
---|
249 |
|
---|
250 | #endif
|
---|
251 |
|
---|