1 | /** @file
|
---|
2 | * IPRT - Loader.
|
---|
3 | */
|
---|
4 |
|
---|
5 | /*
|
---|
6 | * Copyright (C) 2006-2007 Oracle Corporation
|
---|
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 | /** Symbols defined in this library are not made available to resolve
|
---|
39 | * references in subsequently loaded libraries (default). */
|
---|
40 | #define RTLDRFLAGS_LOCAL 0
|
---|
41 | /** Symbols defined in this library will be made available for symbol
|
---|
42 | * resolution of subsequently loaded libraries. */
|
---|
43 | #define RTLDRFLAGS_GLOBAL RT_BIT(0)
|
---|
44 |
|
---|
45 |
|
---|
46 | RT_C_DECLS_BEGIN
|
---|
47 |
|
---|
48 |
|
---|
49 | /**
|
---|
50 | * Gets the default file suffix for DLL/SO/DYLIB/whatever.
|
---|
51 | *
|
---|
52 | * @returns The stuff (readonly).
|
---|
53 | */
|
---|
54 | RTDECL(const char *) RTLdrGetSuff(void);
|
---|
55 |
|
---|
56 | /**
|
---|
57 | * Checks if a library is loadable or not.
|
---|
58 | *
|
---|
59 | * This may attempt load and unload the library.
|
---|
60 | *
|
---|
61 | * @returns true/false accordingly.
|
---|
62 | * @param pszFilename Image filename.
|
---|
63 | */
|
---|
64 | RTDECL(bool) RTLdrIsLoadable(const char *pszFilename);
|
---|
65 |
|
---|
66 | /**
|
---|
67 | * Loads a dynamic load library (/shared object) image file using native
|
---|
68 | * OS facilities.
|
---|
69 | *
|
---|
70 | * The filename will be appended the default DLL/SO extension of
|
---|
71 | * the platform if it have been omitted. This means that it's not
|
---|
72 | * possible to load DLLs/SOs with no extension using this interface,
|
---|
73 | * but that's not a bad tradeoff.
|
---|
74 | *
|
---|
75 | * If no path is specified in the filename, the OS will usually search it's library
|
---|
76 | * path to find the image file.
|
---|
77 | *
|
---|
78 | * @returns iprt status code.
|
---|
79 | * @param pszFilename Image filename.
|
---|
80 | * @param phLdrMod Where to store the handle to the loader module.
|
---|
81 | */
|
---|
82 | RTDECL(int) RTLdrLoad(const char *pszFilename, PRTLDRMOD phLdrMod);
|
---|
83 |
|
---|
84 | /**
|
---|
85 | * Loads a dynamic load library (/shared object) image file using native
|
---|
86 | * OS facilities.
|
---|
87 | *
|
---|
88 | * The filename will be appended the default DLL/SO extension of
|
---|
89 | * the platform if it have been omitted. This means that it's not
|
---|
90 | * possible to load DLLs/SOs with no extension using this interface,
|
---|
91 | * but that's not a bad tradeoff.
|
---|
92 | *
|
---|
93 | * If no path is specified in the filename, the OS will usually search it's library
|
---|
94 | * path to find the image file.
|
---|
95 | *
|
---|
96 | * @returns iprt status code.
|
---|
97 | * @param pszFilename Image filename.
|
---|
98 | * @param phLdrMod Where to store the handle to the loader module.
|
---|
99 | * @param fFlags See RTLDFLAGS_.
|
---|
100 | * @param pErrInfo Where to return extended error information. Optional.
|
---|
101 | */
|
---|
102 | RTDECL(int) RTLdrLoadEx(const char *pszFilename, PRTLDRMOD phLdrMod, uint32_t fFlags, PRTERRINFO pErrInfo);
|
---|
103 |
|
---|
104 | /**
|
---|
105 | * Loads a dynamic load library (/shared object) image file residing in the
|
---|
106 | * RTPathAppPrivateArch() directory.
|
---|
107 | *
|
---|
108 | * Suffix is not required.
|
---|
109 | *
|
---|
110 | * @returns iprt status code.
|
---|
111 | * @param pszFilename Image filename. No path.
|
---|
112 | * @param phLdrMod Where to store the handle to the loaded module.
|
---|
113 | */
|
---|
114 | RTDECL(int) RTLdrLoadAppPriv(const char *pszFilename, PRTLDRMOD phLdrMod);
|
---|
115 |
|
---|
116 | /**
|
---|
117 | * Image architecuture specifier for RTLdrOpenEx.
|
---|
118 | */
|
---|
119 | typedef enum RTLDRARCH
|
---|
120 | {
|
---|
121 | RTLDRARCH_INVALID = 0,
|
---|
122 | /** Whatever. */
|
---|
123 | RTLDRARCH_WHATEVER,
|
---|
124 | /** The host architecture. */
|
---|
125 | RTLDRARCH_HOST,
|
---|
126 | /** 32-bit x86. */
|
---|
127 | RTLDRARCH_X86_32,
|
---|
128 | /** AMD64 (64-bit x86 if you like). */
|
---|
129 | RTLDRARCH_AMD64,
|
---|
130 | /** End of the valid values. */
|
---|
131 | RTLDRARCH_END,
|
---|
132 | /** Make sure the type is a full 32-bit. */
|
---|
133 | RTLDRARCH_32BIT_HACK = 0x7fffffff
|
---|
134 | } RTLDRARCH;
|
---|
135 | /** Pointer to a RTLDRARCH. */
|
---|
136 | typedef RTLDRARCH *PRTLDRARCH;
|
---|
137 |
|
---|
138 | /**
|
---|
139 | * Open a binary image file, extended version.
|
---|
140 | *
|
---|
141 | * @returns iprt status code.
|
---|
142 | * @param pszFilename Image filename.
|
---|
143 | * @param fFlags Reserved, MBZ.
|
---|
144 | * @param enmArch CPU architecture specifier for the image to be loaded.
|
---|
145 | * @param phLdrMod Where to store the handle to the loader module.
|
---|
146 | */
|
---|
147 | RTDECL(int) RTLdrOpen(const char *pszFilename, uint32_t fFlags, RTLDRARCH enmArch, PRTLDRMOD phLdrMod);
|
---|
148 |
|
---|
149 | /**
|
---|
150 | * Opens a binary image file using kLdr.
|
---|
151 | *
|
---|
152 | * @returns iprt status code.
|
---|
153 | * @param pszFilename Image filename.
|
---|
154 | * @param phLdrMod Where to store the handle to the loaded module.
|
---|
155 | * @param fFlags Reserved, MBZ.
|
---|
156 | * @param enmArch CPU architecture specifier for the image to be loaded.
|
---|
157 | * @remark Primarily for testing the loader.
|
---|
158 | */
|
---|
159 | RTDECL(int) RTLdrOpenkLdr(const char *pszFilename, uint32_t fFlags, RTLDRARCH enmArch, PRTLDRMOD phLdrMod);
|
---|
160 |
|
---|
161 | /**
|
---|
162 | * What to expect and do with the bits passed to RTLdrOpenBits().
|
---|
163 | */
|
---|
164 | typedef enum RTLDROPENBITS
|
---|
165 | {
|
---|
166 | /** The usual invalid 0 entry. */
|
---|
167 | RTLDROPENBITS_INVALID = 0,
|
---|
168 | /** The bits are readonly and will never be changed. */
|
---|
169 | RTLDROPENBITS_READONLY,
|
---|
170 | /** The bits are going to be changed and the loader will have to duplicate them
|
---|
171 | * when opening the image. */
|
---|
172 | RTLDROPENBITS_WRITABLE,
|
---|
173 | /** The bits are both the source and destination for the loader operation.
|
---|
174 | * This means that the loader may have to duplicate them prior to changing them. */
|
---|
175 | RTLDROPENBITS_SRC_AND_DST,
|
---|
176 | /** The end of the valid enums. This entry marks the
|
---|
177 | * first invalid entry.. */
|
---|
178 | RTLDROPENBITS_END,
|
---|
179 | RTLDROPENBITS_32BIT_HACK = 0x7fffffff
|
---|
180 | } RTLDROPENBITS;
|
---|
181 |
|
---|
182 | /**
|
---|
183 | * Open a binary image from in-memory bits.
|
---|
184 | *
|
---|
185 | * @returns iprt status code.
|
---|
186 | * @param pvBits The start of the raw-image.
|
---|
187 | * @param cbBits The size of the raw-image.
|
---|
188 | * @param enmBits What to expect from the pvBits.
|
---|
189 | * @param pszLogName What to call the raw-image when logging.
|
---|
190 | * For RTLdrLoad and RTLdrOpen the filename is used for this.
|
---|
191 | * @param phLdrMod Where to store the handle to the loader module.
|
---|
192 | */
|
---|
193 | RTDECL(int) RTLdrOpenBits(const void *pvBits, size_t cbBits, RTLDROPENBITS enmBits, const char *pszLogName, PRTLDRMOD phLdrMod);
|
---|
194 |
|
---|
195 | /**
|
---|
196 | * Closes a loader module handle.
|
---|
197 | *
|
---|
198 | * The handle can be obtained using any of the RTLdrLoad(), RTLdrOpen()
|
---|
199 | * and RTLdrOpenBits() functions.
|
---|
200 | *
|
---|
201 | * @returns iprt status code.
|
---|
202 | * @param hLdrMod The loader module handle.
|
---|
203 | */
|
---|
204 | RTDECL(int) RTLdrClose(RTLDRMOD hLdrMod);
|
---|
205 |
|
---|
206 | /**
|
---|
207 | * Gets the address of a named exported symbol.
|
---|
208 | *
|
---|
209 | * @returns iprt status code.
|
---|
210 | * @param hLdrMod The loader module handle.
|
---|
211 | * @param pszSymbol Symbol name.
|
---|
212 | * @param ppvValue Where to store the symbol value. Note that this is restricted to the
|
---|
213 | * pointer size used on the host!
|
---|
214 | */
|
---|
215 | RTDECL(int) RTLdrGetSymbol(RTLDRMOD hLdrMod, const char *pszSymbol, void **ppvValue);
|
---|
216 |
|
---|
217 | /**
|
---|
218 | * Gets the address of a named exported symbol.
|
---|
219 | *
|
---|
220 | * This function differs from the plain one in that it can deal with
|
---|
221 | * both GC and HC address sizes, and that it can calculate the symbol
|
---|
222 | * value relative to any given base address.
|
---|
223 | *
|
---|
224 | * @returns iprt status code.
|
---|
225 | * @param hLdrMod The loader module handle.
|
---|
226 | * @param pvBits Optional pointer to the loaded image.
|
---|
227 | * Set this to NULL if no RTLdrGetBits() processed image bits are available.
|
---|
228 | * Not supported for RTLdrLoad() images.
|
---|
229 | * @param BaseAddress Image load address.
|
---|
230 | * Not supported for RTLdrLoad() images.
|
---|
231 | * @param pszSymbol Symbol name.
|
---|
232 | * @param pValue Where to store the symbol value.
|
---|
233 | */
|
---|
234 | RTDECL(int) RTLdrGetSymbolEx(RTLDRMOD hLdrMod, const void *pvBits, RTUINTPTR BaseAddress, const char *pszSymbol, RTUINTPTR *pValue);
|
---|
235 |
|
---|
236 | /**
|
---|
237 | * Gets the size of the loaded image.
|
---|
238 | * This is only supported for modules which has been opened using RTLdrOpen() and RTLdrOpenBits().
|
---|
239 | *
|
---|
240 | * @returns image size (in bytes).
|
---|
241 | * @returns ~(size_t)0 on if not opened by RTLdrOpen().
|
---|
242 | * @param hLdrMod Handle to the loader module.
|
---|
243 | * @remark Not supported for RTLdrLoad() images.
|
---|
244 | */
|
---|
245 | RTDECL(size_t) RTLdrSize(RTLDRMOD hLdrMod);
|
---|
246 |
|
---|
247 | /**
|
---|
248 | * Resolve an external symbol during RTLdrGetBits().
|
---|
249 | *
|
---|
250 | * @returns iprt status code.
|
---|
251 | * @param hLdrMod The loader module handle.
|
---|
252 | * @param pszModule Module name.
|
---|
253 | * @param pszSymbol Symbol name, NULL if uSymbol should be used.
|
---|
254 | * @param uSymbol Symbol ordinal, ~0 if pszSymbol should be used.
|
---|
255 | * @param pValue Where to store the symbol value (address).
|
---|
256 | * @param pvUser User argument.
|
---|
257 | */
|
---|
258 | typedef DECLCALLBACK(int) RTLDRIMPORT(RTLDRMOD hLdrMod, const char *pszModule, const char *pszSymbol, unsigned uSymbol, RTUINTPTR *pValue, void *pvUser);
|
---|
259 | /** Pointer to a FNRTLDRIMPORT() callback function. */
|
---|
260 | typedef RTLDRIMPORT *PFNRTLDRIMPORT;
|
---|
261 |
|
---|
262 | /**
|
---|
263 | * Loads the image into a buffer provided by the user and applies fixups
|
---|
264 | * for the given base address.
|
---|
265 | *
|
---|
266 | * @returns iprt status code.
|
---|
267 | * @param hLdrMod The load module handle.
|
---|
268 | * @param pvBits Where to put the bits.
|
---|
269 | * Must be as large as RTLdrSize() suggests.
|
---|
270 | * @param BaseAddress The base address.
|
---|
271 | * @param pfnGetImport Callback function for resolving imports one by one.
|
---|
272 | * @param pvUser User argument for the callback.
|
---|
273 | * @remark Not supported for RTLdrLoad() images.
|
---|
274 | */
|
---|
275 | RTDECL(int) RTLdrGetBits(RTLDRMOD hLdrMod, void *pvBits, RTUINTPTR BaseAddress, PFNRTLDRIMPORT pfnGetImport, void *pvUser);
|
---|
276 |
|
---|
277 | /**
|
---|
278 | * Relocates bits after getting them.
|
---|
279 | * Useful for code which moves around a bit.
|
---|
280 | *
|
---|
281 | * @returns iprt status code.
|
---|
282 | * @param hLdrMod The loader module handle.
|
---|
283 | * @param pvBits Where the image bits are.
|
---|
284 | * Must have been passed to RTLdrGetBits().
|
---|
285 | * @param NewBaseAddress The new base address.
|
---|
286 | * @param OldBaseAddress The old base address.
|
---|
287 | * @param pfnGetImport Callback function for resolving imports one by one.
|
---|
288 | * @param pvUser User argument for the callback.
|
---|
289 | * @remark Not supported for RTLdrLoad() images.
|
---|
290 | */
|
---|
291 | RTDECL(int) RTLdrRelocate(RTLDRMOD hLdrMod, void *pvBits, RTUINTPTR NewBaseAddress, RTUINTPTR OldBaseAddress,
|
---|
292 | PFNRTLDRIMPORT pfnGetImport, void *pvUser);
|
---|
293 |
|
---|
294 | /**
|
---|
295 | * Enumeration callback function used by RTLdrEnumSymbols().
|
---|
296 | *
|
---|
297 | * @returns iprt status code. Failure will stop the enumeration.
|
---|
298 | * @param hLdrMod The loader module handle.
|
---|
299 | * @param pszSymbol Symbol name. NULL if ordinal only.
|
---|
300 | * @param uSymbol Symbol ordinal, ~0 if not used.
|
---|
301 | * @param Value Symbol value.
|
---|
302 | * @param pvUser The user argument specified to RTLdrEnumSymbols().
|
---|
303 | */
|
---|
304 | typedef DECLCALLBACK(int) RTLDRENUMSYMS(RTLDRMOD hLdrMod, const char *pszSymbol, unsigned uSymbol, RTUINTPTR Value, void *pvUser);
|
---|
305 | /** Pointer to a RTLDRENUMSYMS() callback function. */
|
---|
306 | typedef RTLDRENUMSYMS *PFNRTLDRENUMSYMS;
|
---|
307 |
|
---|
308 | /**
|
---|
309 | * Enumerates all symbols in a module.
|
---|
310 | *
|
---|
311 | * @returns iprt status code.
|
---|
312 | * @param hLdrMod The loader module handle.
|
---|
313 | * @param fFlags Flags indicating what to return and such.
|
---|
314 | * @param pvBits Optional pointer to the loaded image. (RTLDR_ENUM_SYMBOL_FLAGS_*)
|
---|
315 | * Set this to NULL if no RTLdrGetBits() processed image bits are available.
|
---|
316 | * @param BaseAddress Image load address.
|
---|
317 | * @param pfnCallback Callback function.
|
---|
318 | * @param pvUser User argument for the callback.
|
---|
319 | * @remark Not supported for RTLdrLoad() images.
|
---|
320 | */
|
---|
321 | RTDECL(int) RTLdrEnumSymbols(RTLDRMOD hLdrMod, unsigned fFlags, const void *pvBits, RTUINTPTR BaseAddress, PFNRTLDRENUMSYMS pfnCallback, void *pvUser);
|
---|
322 |
|
---|
323 | /** @name RTLdrEnumSymbols flags.
|
---|
324 | * @{ */
|
---|
325 | /** Returns ALL kinds of symbols. The default is to only return public/exported symbols. */
|
---|
326 | #define RTLDR_ENUM_SYMBOL_FLAGS_ALL RT_BIT(1)
|
---|
327 | /** @} */
|
---|
328 |
|
---|
329 | RT_C_DECLS_END
|
---|
330 |
|
---|
331 | /** @} */
|
---|
332 |
|
---|
333 | #endif
|
---|
334 |
|
---|