VirtualBox

source: vbox/trunk/include/iprt/ldr.h@ 55747

Last change on this file since 55747 was 53818, checked in by vboxsync, 10 years ago

RTLdr: Added RTLDR_O_IGNORE_ARCH_IF_NO_CODE for PE resource dlls and RTLDR_O_WHATEVER_ARCH for future mach-O hacking.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 41.6 KB
Line 
1/** @file
2 * IPRT - Loader.
3 */
4
5/*
6 * Copyright (C) 2006-2014 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
39RT_C_DECLS_BEGIN
40
41/** Loader address (unsigned integer). */
42typedef RTUINTPTR RTLDRADDR;
43/** Pointer to a loader address. */
44typedef RTLDRADDR *PRTLDRADDR;
45/** Pointer to a const loader address. */
46typedef RTLDRADDR const *PCRTLDRADDR;
47/** The max loader address value. */
48#define RTLDRADDR_MAX RTUINTPTR_MAX
49/** NIL loader address value. */
50#define NIL_RTLDRADDR RTLDRADDR_MAX
51
52
53/**
54 * Loader module format.
55 */
56typedef enum RTLDRFMT
57{
58 /** The usual invalid 0 format. */
59 RTLDRFMT_INVALID = 0,
60 /** The native OS loader. */
61 RTLDRFMT_NATIVE,
62 /** The AOUT loader. */
63 RTLDRFMT_AOUT,
64 /** The ELF loader. */
65 RTLDRFMT_ELF,
66 /** The LX loader. */
67 RTLDRFMT_LX,
68 /** The Mach-O loader. */
69 RTLDRFMT_MACHO,
70 /** The PE loader. */
71 RTLDRFMT_PE,
72 /** The end of the valid format values (exclusive). */
73 RTLDRFMT_END,
74 /** Hack to blow the type up to 32-bit. */
75 RTLDRFMT_32BIT_HACK = 0x7fffffff
76} RTLDRFMT;
77
78
79/**
80 * Loader module type.
81 */
82typedef enum RTLDRTYPE
83{
84 /** The usual invalid 0 type. */
85 RTLDRTYPE_INVALID = 0,
86 /** Object file. */
87 RTLDRTYPE_OBJECT,
88 /** Executable module, fixed load address. */
89 RTLDRTYPE_EXECUTABLE_FIXED,
90 /** Executable module, relocatable, non-fixed load address. */
91 RTLDRTYPE_EXECUTABLE_RELOCATABLE,
92 /** Executable module, position independent code, non-fixed load address. */
93 RTLDRTYPE_EXECUTABLE_PIC,
94 /** Shared library, fixed load address.
95 * Typically a system library. */
96 RTLDRTYPE_SHARED_LIBRARY_FIXED,
97 /** Shared library, relocatable, non-fixed load address. */
98 RTLDRTYPE_SHARED_LIBRARY_RELOCATABLE,
99 /** Shared library, position independent code, non-fixed load address. */
100 RTLDRTYPE_SHARED_LIBRARY_PIC,
101 /** DLL that contains no code or data only imports and exports. (Chiefly OS/2.) */
102 RTLDRTYPE_FORWARDER_DLL,
103 /** Core or dump. */
104 RTLDRTYPE_CORE,
105 /** Debug module (debug info with empty code & data segments). */
106 RTLDRTYPE_DEBUG_INFO,
107 /** The end of the valid types values (exclusive). */
108 RTLDRTYPE_END,
109 /** Hack to blow the type up to 32-bit. */
110 RTLDRTYPE_32BIT_HACK = 0x7fffffff
111} RTLDRTYPE;
112
113
114/**
115 * Loader endian indicator.
116 */
117typedef enum RTLDRENDIAN
118{
119 /** The usual invalid endian. */
120 RTLDRENDIAN_INVALID,
121 /** Little endian. */
122 RTLDRENDIAN_LITTLE,
123 /** Bit endian. */
124 RTLDRENDIAN_BIG,
125 /** Endianness doesn't have a meaning in the context. */
126 RTLDRENDIAN_NA,
127 /** The end of the valid endian values (exclusive). */
128 RTLDRENDIAN_END,
129 /** Hack to blow the type up to 32-bit. */
130 RTLDRENDIAN_32BIT_HACK = 0x7fffffff
131} RTLDRENDIAN;
132
133
134/** Pointer to a loader reader instance. */
135typedef struct RTLDRREADER *PRTLDRREADER;
136/**
137 * Loader image reader instance.
138 *
139 * @remarks The reader will typically have a larger structure wrapping this one
140 * for storing necessary instance variables.
141 *
142 * The loader ASSUMES the caller serializes all access to the
143 * individual loader module handlers, thus no serialization is required
144 * when implementing this interface.
145 */
146typedef struct RTLDRREADER
147{
148 /** Magic value (RTLDRREADER_MAGIC). */
149 uintptr_t uMagic;
150
151 /**
152 * Reads bytes at a give place in the raw image.
153 *
154 * @returns iprt status code.
155 * @param pReader Pointer to the reader instance.
156 * @param pvBuf Where to store the bits.
157 * @param cb Number of bytes to read.
158 * @param off Where to start reading relative to the start of the raw image.
159 */
160 DECLCALLBACKMEMBER(int, pfnRead)(PRTLDRREADER pReader, void *pvBuf, size_t cb, RTFOFF off);
161
162 /**
163 * Tells end position of last read.
164 *
165 * @returns position relative to start of the raw image.
166 * @param pReader Pointer to the reader instance.
167 */
168 DECLCALLBACKMEMBER(RTFOFF, pfnTell)(PRTLDRREADER pReader);
169
170 /**
171 * Gets the size of the raw image bits.
172 *
173 * @returns size of raw image bits in bytes.
174 * @param pReader Pointer to the reader instance.
175 */
176 DECLCALLBACKMEMBER(RTFOFF, pfnSize)(PRTLDRREADER pReader);
177
178 /**
179 * Map the bits into memory.
180 *
181 * The mapping will be freed upon calling pfnDestroy() if not pfnUnmap()
182 * is called before that. The mapping is read only.
183 *
184 * @returns iprt status code.
185 * @param pReader Pointer to the reader instance.
186 * @param ppvBits Where to store the address of the memory mapping on success.
187 * The size of the mapping can be obtained by calling pfnSize().
188 */
189 DECLCALLBACKMEMBER(int, pfnMap)(PRTLDRREADER pReader, const void **ppvBits);
190
191 /**
192 * Unmap bits.
193 *
194 * @returns iprt status code.
195 * @param pReader Pointer to the reader instance.
196 * @param pvBits Memory pointer returned by pfnMap().
197 */
198 DECLCALLBACKMEMBER(int, pfnUnmap)(PRTLDRREADER pReader, const void *pvBits);
199
200 /**
201 * Gets the most appropriate log name.
202 *
203 * @returns Pointer to readonly log name.
204 * @param pReader Pointer to the reader instance.
205 */
206 DECLCALLBACKMEMBER(const char *, pfnLogName)(PRTLDRREADER pReader);
207
208 /**
209 * Releases all resources associated with the reader instance.
210 * The instance is invalid after this call returns.
211 *
212 * @returns iprt status code.
213 * @param pReader Pointer to the reader instance.
214 */
215 DECLCALLBACKMEMBER(int, pfnDestroy)(PRTLDRREADER pReader);
216} RTLDRREADER;
217
218/** Magic value for RTLDRREADER (Gordon Matthew Thomas Sumner / Sting). */
219#define RTLDRREADER_MAGIC UINT32_C(0x19511002)
220
221
222/**
223 * Gets the default file suffix for DLL/SO/DYLIB/whatever.
224 *
225 * @returns The stuff (readonly).
226 */
227RTDECL(const char *) RTLdrGetSuff(void);
228
229/**
230 * Checks if a library is loadable or not.
231 *
232 * This may attempt load and unload the library.
233 *
234 * @returns true/false accordingly.
235 * @param pszFilename Image filename.
236 */
237RTDECL(bool) RTLdrIsLoadable(const char *pszFilename);
238
239/**
240 * Loads a dynamic load library (/shared object) image file using native
241 * OS facilities.
242 *
243 * The filename will be appended the default DLL/SO extension of
244 * the platform if it have been omitted. This means that it's not
245 * possible to load DLLs/SOs with no extension using this interface,
246 * but that's not a bad tradeoff.
247 *
248 * If no path is specified in the filename, the OS will usually search it's library
249 * path to find the image file.
250 *
251 * @returns iprt status code.
252 * @param pszFilename Image filename.
253 * @param phLdrMod Where to store the handle to the loader module.
254 */
255RTDECL(int) RTLdrLoad(const char *pszFilename, PRTLDRMOD phLdrMod);
256
257/**
258 * Loads a dynamic load library (/shared object) image file using native
259 * OS facilities.
260 *
261 * The filename will be appended the default DLL/SO extension of
262 * the platform if it have been omitted. This means that it's not
263 * possible to load DLLs/SOs with no extension using this interface,
264 * but that's not a bad tradeoff.
265 *
266 * If no path is specified in the filename, the OS will usually search it's library
267 * path to find the image file.
268 *
269 * @returns iprt status code.
270 * @param pszFilename Image filename.
271 * @param phLdrMod Where to store the handle to the loader module.
272 * @param fFlags See RTLDRLOAD_FLAGS_XXX.
273 * @param pErrInfo Where to return extended error information. Optional.
274 */
275RTDECL(int) RTLdrLoadEx(const char *pszFilename, PRTLDRMOD phLdrMod, uint32_t fFlags, PRTERRINFO pErrInfo);
276
277/** @defgroup RTLDRLOAD_FLAGS_XXX RTLdrLoadEx flags.
278 * @{ */
279/** Symbols defined in this library are not made available to resolve
280 * references in subsequently loaded libraries (default). */
281#define RTLDRLOAD_FLAGS_LOCAL UINT32_C(0)
282/** Symbols defined in this library will be made available for symbol
283 * resolution of subsequently loaded libraries. */
284#define RTLDRLOAD_FLAGS_GLOBAL RT_BIT_32(0)
285/** Do not unload the library upon RTLdrClose. (For system libs.) */
286#define RTLDRLOAD_FLAGS_NO_UNLOAD RT_BIT_32(1)
287/** The mask of valid flag bits. */
288#define RTLDRLOAD_FLAGS_VALID_MASK UINT32_C(0x00000003)
289/** @} */
290
291/**
292 * Loads a dynamic load library (/shared object) image file residing in one of
293 * the default system library locations.
294 *
295 * Only the system library locations are searched. No suffix is required.
296 *
297 * @returns iprt status code.
298 * @param pszFilename Image filename. No path.
299 * @param fNoUnload Do not unload the library when RTLdrClose is called.
300 * @param phLdrMod Where to store the handle to the loaded module.
301 */
302RTDECL(int) RTLdrLoadSystem(const char *pszFilename, bool fNoUnload, PRTLDRMOD phLdrMod);
303
304/**
305 * Combines RTLdrLoadSystem and RTLdrGetSymbol, with fNoUnload set to true.
306 *
307 * @returns The symbol value, NULL on failure. (If you care for a less boolean
308 * status, go thru the necessary API calls yourself.)
309 * @param pszFilename Image filename. No path.
310 * @param pszSymbol Symbol name.
311 */
312RTDECL(void *) RTLdrGetSystemSymbol(const char *pszFilename, const char *pszSymbol);
313
314/**
315 * Loads a dynamic load library (/shared object) image file residing in the
316 * RTPathAppPrivateArch() directory.
317 *
318 * Suffix is not required.
319 *
320 * @returns iprt status code.
321 * @param pszFilename Image filename. No path.
322 * @param phLdrMod Where to store the handle to the loaded module.
323 */
324RTDECL(int) RTLdrLoadAppPriv(const char *pszFilename, PRTLDRMOD phLdrMod);
325
326/**
327 * Gets the native module handle for a module loaded by RTLdrLoad, RTLdrLoadEx,
328 * RTLdrLoadSystem, or RTLdrLoadAppPriv.
329 *
330 * @returns Native handle on success, ~(uintptr_t)0 on failure.
331 * @param hLdrMod The loader module handle.
332 */
333RTDECL(uintptr_t) RTLdrGetNativeHandle(RTLDRMOD hLdrMod);
334
335
336/**
337 * Image architecuture specifier for RTLdrOpenEx.
338 */
339typedef enum RTLDRARCH
340{
341 RTLDRARCH_INVALID = 0,
342 /** Whatever. */
343 RTLDRARCH_WHATEVER,
344 /** The host architecture. */
345 RTLDRARCH_HOST,
346 /** 32-bit x86. */
347 RTLDRARCH_X86_32,
348 /** AMD64 (64-bit x86 if you like). */
349 RTLDRARCH_AMD64,
350 /** End of the valid values. */
351 RTLDRARCH_END,
352 /** Make sure the type is a full 32-bit. */
353 RTLDRARCH_32BIT_HACK = 0x7fffffff
354} RTLDRARCH;
355/** Pointer to a RTLDRARCH. */
356typedef RTLDRARCH *PRTLDRARCH;
357
358/** @name RTLDR_O_XXX - RTLdrOpen flags.
359 * @{ */
360/** Open for debugging or introspection reasons.
361 * This will skip a few of the stricter validations when loading images. */
362#define RTLDR_O_FOR_DEBUG RT_BIT_32(0)
363/** Open for signature validation. */
364#define RTLDR_O_FOR_VALIDATION RT_BIT_32(1)
365/** The arch specification is just a guideline for FAT binaries. */
366#define RTLDR_O_WHATEVER_ARCH RT_BIT_32(2)
367/** Ignore the architecture specification if there is no code. */
368#define RTLDR_O_IGNORE_ARCH_IF_NO_CODE RT_BIT_32(3)
369/** Mask of valid flags. */
370#define RTLDR_O_VALID_MASK UINT32_C(0x0000000f)
371/** @} */
372
373/**
374 * Open a binary image file, extended version.
375 *
376 * @returns iprt status code.
377 * @param pszFilename Image filename.
378 * @param fFlags Valid RTLDR_O_XXX combination.
379 * @param enmArch CPU architecture specifier for the image to be loaded.
380 * @param phLdrMod Where to store the handle to the loader module.
381 */
382RTDECL(int) RTLdrOpen(const char *pszFilename, uint32_t fFlags, RTLDRARCH enmArch, PRTLDRMOD phLdrMod);
383
384/**
385 * Opens a binary image file using kLdr.
386 *
387 * @returns iprt status code.
388 * @param pszFilename Image filename.
389 * @param phLdrMod Where to store the handle to the loaded module.
390 * @param fFlags Valid RTLDR_O_XXX combination.
391 * @param enmArch CPU architecture specifier for the image to be loaded.
392 * @remark Primarily for testing the loader.
393 */
394RTDECL(int) RTLdrOpenkLdr(const char *pszFilename, uint32_t fFlags, RTLDRARCH enmArch, PRTLDRMOD phLdrMod);
395
396/**
397 * Open part with reader.
398 *
399 * @returns iprt status code.
400 * @param pReader The loader reader instance which will provide the raw
401 * image bits. The reader instance will be consumed on
402 * success. On failure, the caller has to do the cleaning
403 * up.
404 * @param fFlags Valid RTLDR_O_XXX combination.
405 * @param enmArch Architecture specifier.
406 * @param phMod Where to store the handle.
407 * @param pErrInfo Where to return extended error information. Optional.
408 */
409RTDECL(int) RTLdrOpenWithReader(PRTLDRREADER pReader, uint32_t fFlags, RTLDRARCH enmArch, PRTLDRMOD phMod, PRTERRINFO pErrInfo);
410
411/**
412 * Called to read @a cb bytes at @a off into @a pvBuf.
413 *
414 * @returns IPRT status code
415 * @param pvBuf The output buffer.
416 * @param cb The number of bytes to read.
417 * @param off Where to start reading.
418 * @param pvUser The user parameter.
419 */
420typedef DECLCALLBACK(int) FNRTLDRRDRMEMREAD(void *pvBuf, size_t cb, size_t off, void *pvUser);
421/** Pointer to a RTLdrOpenInMemory reader callback. */
422typedef FNRTLDRRDRMEMREAD *PFNRTLDRRDRMEMREAD;
423
424/**
425 * Called to when the module is unloaded (or done loading) to release resources
426 * associated with it (@a pvUser).
427 *
428 * @returns IPRT status code
429 * @param pvUser The user parameter.
430 */
431typedef DECLCALLBACK(void) FNRTLDRRDRMEMDTOR(void *pvUser);
432/** Pointer to a RTLdrOpenInMemory destructor callback. */
433typedef FNRTLDRRDRMEMDTOR *PFNRTLDRRDRMEMDTOR;
434
435/**
436 * Open a in-memory image or an image with a custom reader callback.
437 *
438 * @returns IPRT status code.
439 * @param pszName The image name.
440 * @param fFlags Valid RTLDR_O_XXX combination.
441 * @param enmArch CPU architecture specifier for the image to be loaded.
442 * @param cbImage The size of the image (fake file).
443 * @param pfnRead The read function. If NULL is passed in, a default
444 * reader function is provided that assumes @a pvUser
445 * points to the raw image bits, at least @a cbImage of
446 * valid memory.
447 * @param pfnDtor The destructor function. If NULL is passed, a default
448 * destructor will be provided that passes @a pvUser to
449 * RTMemFree.
450 * @param pvUser The user argument or, if any of the callbacks are NULL,
451 * a pointer to a memory block.
452 * @param phLdrMod Where to return the module handle.
453 *
454 * @remarks With the exception of invalid @a pfnDtor and/or @a pvUser
455 * parameters, the pfnDtor methods (or the default one if NULL) will
456 * always be invoked. The destruction of pvUser is entirely in the
457 * hands of this method once it's called.
458 */
459RTDECL(int) RTLdrOpenInMemory(const char *pszName, uint32_t fFlags, RTLDRARCH enmArch, size_t cbImage,
460 PFNRTLDRRDRMEMREAD pfnRead, PFNRTLDRRDRMEMDTOR pfnDtor, void *pvUser,
461 PRTLDRMOD phLdrMod);
462
463/**
464 * Closes a loader module handle.
465 *
466 * The handle can be obtained using any of the RTLdrLoad(), RTLdrOpen()
467 * and RTLdrOpenInMemory() functions.
468 *
469 * @returns iprt status code.
470 * @param hLdrMod The loader module handle.
471 */
472RTDECL(int) RTLdrClose(RTLDRMOD hLdrMod);
473
474/**
475 * Gets the address of a named exported symbol.
476 *
477 * @returns iprt status code.
478 * @retval VERR_LDR_FORWARDER forwarder, use pfnQueryForwarderInfo. Buffer size
479 * hint in @a ppvValue.
480 * @param hLdrMod The loader module handle.
481 * @param pszSymbol Symbol name.
482 * @param ppvValue Where to store the symbol value. Note that this is restricted to the
483 * pointer size used on the host!
484 */
485RTDECL(int) RTLdrGetSymbol(RTLDRMOD hLdrMod, const char *pszSymbol, void **ppvValue);
486
487/**
488 * Gets the address of a named exported symbol.
489 *
490 * This function differs from the plain one in that it can deal with
491 * both GC and HC address sizes, and that it can calculate the symbol
492 * value relative to any given base address.
493 *
494 * @returns iprt status code.
495 * @retval VERR_LDR_FORWARDER forwarder, use pfnQueryForwarderInfo. Buffer size
496 * hint in @a pValue.
497 * @param hLdrMod The loader module handle.
498 * @param pvBits Optional pointer to the loaded image.
499 * Set this to NULL if no RTLdrGetBits() processed image bits are available.
500 * Not supported for RTLdrLoad() images.
501 * @param BaseAddress Image load address.
502 * Not supported for RTLdrLoad() images.
503 * @param iOrdinal Symbol ordinal number, pass UINT32_MAX if pszSymbol
504 * should be used instead.
505 * @param pszSymbol Symbol name.
506 * @param pValue Where to store the symbol value.
507 */
508RTDECL(int) RTLdrGetSymbolEx(RTLDRMOD hLdrMod, const void *pvBits, RTLDRADDR BaseAddress,
509 uint32_t iOrdinal, const char *pszSymbol, PRTLDRADDR pValue);
510
511/**
512 * Gets the address of a named exported function.
513 *
514 * Same as RTLdrGetSymbol, but skips the status code and pointer to return
515 * variable stuff.
516 *
517 * @returns Pointer to the function if found, NULL if not.
518 * @param hLdrMod The loader module handle.
519 * @param pszSymbol Function name.
520 */
521RTDECL(PFNRT) RTLdrGetFunction(RTLDRMOD hLdrMod, const char *pszSymbol);
522
523/**
524 * Information about an imported symbol.
525 */
526typedef struct RTLDRIMPORTINFO
527{
528 /** Symbol table entry number, UINT32_MAX if not available. */
529 uint32_t iSelfOrdinal;
530 /** The ordinal of the imported symbol in szModule, UINT32_MAX if not used. */
531 uint32_t iOrdinal;
532 /** The symbol name, NULL if not used. This points to the char immediately
533 * following szModule when returned by RTLdrQueryForwarderInfo. */
534 const char *pszSymbol;
535 /** The name of the module being imported from. */
536 char szModule[1];
537} RTLDRIMPORTINFO;
538/** Pointer to information about an imported symbol. */
539typedef RTLDRIMPORTINFO *PRTLDRIMPORTINFO;
540/** Pointer to const information about an imported symbol. */
541typedef RTLDRIMPORTINFO const *PCRTLDRIMPORTINFO;
542
543/**
544 * Query information about a forwarded symbol.
545 *
546 * @returns IPRT status code.
547 * @param hLdrMod The loader module handle.
548 * @param pvBits Optional pointer to the loaded image.
549 * Set this to NULL if no RTLdrGetBits() processed image bits are available.
550 * Not supported for RTLdrLoad() images.
551 * @param iOrdinal Symbol ordinal number, pass UINT32_MAX if pszSymbol
552 * should be used instead.
553 * @param pszSymbol Symbol name.
554 * @param pInfo Where to return the forwarder info.
555 * @param cbInfo Size of the buffer @a pInfo points to. For a size
556 * hint, see @a pValue when RTLdrGetSymbolEx returns
557 * VERR_LDR_FORWARDER.
558 */
559RTDECL(int) RTLdrQueryForwarderInfo(RTLDRMOD hLdrMod, const void *pvBits, uint32_t iOrdinal, const char *pszSymbol,
560 PRTLDRIMPORTINFO pInfo, size_t cbInfo);
561
562
563/**
564 * Gets the size of the loaded image.
565 *
566 * This is not necessarily available for images that has been loaded using
567 * RTLdrLoad().
568 *
569 * @returns image size (in bytes).
570 * @returns ~(size_t)0 on if not available.
571 * @param hLdrMod Handle to the loader module.
572 */
573RTDECL(size_t) RTLdrSize(RTLDRMOD hLdrMod);
574
575/**
576 * Resolve an external symbol during RTLdrGetBits().
577 *
578 * @returns iprt status code.
579 * @param hLdrMod The loader module handle.
580 * @param pszModule Module name.
581 * @param pszSymbol Symbol name, NULL if uSymbol should be used.
582 * @param uSymbol Symbol ordinal, ~0 if pszSymbol should be used.
583 * @param pValue Where to store the symbol value (address).
584 * @param pvUser User argument.
585 */
586typedef DECLCALLBACK(int) RTLDRIMPORT(RTLDRMOD hLdrMod, const char *pszModule, const char *pszSymbol, unsigned uSymbol,
587 PRTLDRADDR pValue, void *pvUser);
588/** Pointer to a FNRTLDRIMPORT() callback function. */
589typedef RTLDRIMPORT *PFNRTLDRIMPORT;
590
591/**
592 * Loads the image into a buffer provided by the user and applies fixups
593 * for the given base address.
594 *
595 * @returns iprt status code.
596 * @param hLdrMod The load module handle.
597 * @param pvBits Where to put the bits.
598 * Must be as large as RTLdrSize() suggests.
599 * @param BaseAddress The base address.
600 * @param pfnGetImport Callback function for resolving imports one by one.
601 * @param pvUser User argument for the callback.
602 * @remark Not supported for RTLdrLoad() images.
603 */
604RTDECL(int) RTLdrGetBits(RTLDRMOD hLdrMod, void *pvBits, RTLDRADDR BaseAddress, PFNRTLDRIMPORT pfnGetImport, void *pvUser);
605
606/**
607 * Relocates bits after getting them.
608 * Useful for code which moves around a bit.
609 *
610 * @returns iprt status code.
611 * @param hLdrMod The loader module handle.
612 * @param pvBits Where the image bits are.
613 * Must have been passed to RTLdrGetBits().
614 * @param NewBaseAddress The new base address.
615 * @param OldBaseAddress The old base address.
616 * @param pfnGetImport Callback function for resolving imports one by one.
617 * @param pvUser User argument for the callback.
618 * @remark Not supported for RTLdrLoad() images.
619 */
620RTDECL(int) RTLdrRelocate(RTLDRMOD hLdrMod, void *pvBits, RTLDRADDR NewBaseAddress, RTLDRADDR OldBaseAddress,
621 PFNRTLDRIMPORT pfnGetImport, void *pvUser);
622
623/**
624 * Enumeration callback function used by RTLdrEnumSymbols().
625 *
626 * @returns iprt status code. Failure will stop the enumeration.
627 * @param hLdrMod The loader module handle.
628 * @param pszSymbol Symbol name. NULL if ordinal only.
629 * @param uSymbol Symbol ordinal, ~0 if not used.
630 * @param Value Symbol value.
631 * @param pvUser The user argument specified to RTLdrEnumSymbols().
632 */
633typedef DECLCALLBACK(int) RTLDRENUMSYMS(RTLDRMOD hLdrMod, const char *pszSymbol, unsigned uSymbol, RTLDRADDR Value, void *pvUser);
634/** Pointer to a RTLDRENUMSYMS() callback function. */
635typedef RTLDRENUMSYMS *PFNRTLDRENUMSYMS;
636
637/**
638 * Enumerates all symbols in a module.
639 *
640 * @returns iprt status code.
641 * @param hLdrMod The loader module handle.
642 * @param fFlags Flags indicating what to return and such.
643 * @param pvBits Optional pointer to the loaded image. (RTLDR_ENUM_SYMBOL_FLAGS_*)
644 * Set this to NULL if no RTLdrGetBits() processed image bits are available.
645 * @param BaseAddress Image load address.
646 * @param pfnCallback Callback function.
647 * @param pvUser User argument for the callback.
648 * @remark Not supported for RTLdrLoad() images.
649 */
650RTDECL(int) RTLdrEnumSymbols(RTLDRMOD hLdrMod, unsigned fFlags, const void *pvBits, RTLDRADDR BaseAddress, PFNRTLDRENUMSYMS pfnCallback, void *pvUser);
651
652/** @name RTLdrEnumSymbols flags.
653 * @{ */
654/** Returns ALL kinds of symbols. The default is to only return public/exported symbols. */
655#define RTLDR_ENUM_SYMBOL_FLAGS_ALL RT_BIT(1)
656/** Ignore forwarders (for use with RTLDR_ENUM_SYMBOL_FLAGS_ALL). */
657#define RTLDR_ENUM_SYMBOL_FLAGS_NO_FWD RT_BIT(2)
658/** @} */
659
660
661/**
662 * Debug info type (as far the loader can tell).
663 */
664typedef enum RTLDRDBGINFOTYPE
665{
666 /** The invalid 0 value. */
667 RTLDRDBGINFOTYPE_INVALID = 0,
668 /** Unknown debug info format. */
669 RTLDRDBGINFOTYPE_UNKNOWN,
670 /** Stabs. */
671 RTLDRDBGINFOTYPE_STABS,
672 /** Debug With Arbitrary Record Format (DWARF). */
673 RTLDRDBGINFOTYPE_DWARF,
674 /** Debug With Arbitrary Record Format (DWARF), in external file (DWO). */
675 RTLDRDBGINFOTYPE_DWARF_DWO,
676 /** Microsoft Codeview debug info. */
677 RTLDRDBGINFOTYPE_CODEVIEW,
678 /** Microsoft Codeview debug info, in external v2.0+ program database (PDB). */
679 RTLDRDBGINFOTYPE_CODEVIEW_PDB20,
680 /** Microsoft Codeview debug info, in external v7.0+ program database (PDB). */
681 RTLDRDBGINFOTYPE_CODEVIEW_PDB70,
682 /** Microsoft Codeview debug info, in external file (DBG). */
683 RTLDRDBGINFOTYPE_CODEVIEW_DBG,
684 /** Microsoft COFF debug info. */
685 RTLDRDBGINFOTYPE_COFF,
686 /** Watcom debug info. */
687 RTLDRDBGINFOTYPE_WATCOM,
688 /** IBM High Level Language debug info.. */
689 RTLDRDBGINFOTYPE_HLL,
690 /** The end of the valid debug info values (exclusive). */
691 RTLDRDBGINFOTYPE_END,
692 /** Blow the type up to 32-bits. */
693 RTLDRDBGINFOTYPE_32BIT_HACK = 0x7fffffff
694} RTLDRDBGINFOTYPE;
695
696
697/**
698 * Debug info details for the enumeration callback.
699 */
700typedef struct RTLDRDBGINFO
701{
702 /** The kind of debug info. */
703 RTLDRDBGINFOTYPE enmType;
704 /** The debug info ordinal number / id. */
705 uint32_t iDbgInfo;
706 /** The file offset *if* this type has one specific location in the executable
707 * image file. This is -1 if there isn't any specific file location. */
708 RTFOFF offFile;
709 /** The link address of the debug info if it's loadable. NIL_RTLDRADDR if not
710 * loadable*/
711 RTLDRADDR LinkAddress;
712 /** The size of the debug information. -1 is used if this isn't applicable.*/
713 RTLDRADDR cb;
714 /** This is set if the debug information is found in an external file. NULL
715 * if no external file involved.
716 * @note Putting it outside the union to allow lazy callback implementation. */
717 const char *pszExtFile;
718 /** Type (enmType) specific information. */
719 union
720 {
721 /** RTLDRDBGINFOTYPE_DWARF */
722 struct
723 {
724 /** The section name. */
725 const char *pszSection;
726 } Dwarf;
727
728 /** RTLDRDBGINFOTYPE_DWARF_DWO */
729 struct
730 {
731 /** The CRC32 of the external file. */
732 uint32_t uCrc32;
733 } Dwo;
734
735 /** RTLDRDBGINFOTYPE_CODEVIEW, RTLDRDBGINFOTYPE_COFF */
736 struct
737 {
738 /** The PE image size. */
739 uint32_t cbImage;
740 /** The timestamp. */
741 uint32_t uTimestamp;
742 /** The major version from the entry. */
743 uint32_t uMajorVer;
744 /** The minor version from the entry. */
745 uint32_t uMinorVer;
746 } Cv, Coff;
747
748 /** RTLDRDBGINFOTYPE_CODEVIEW_DBG */
749 struct
750 {
751 /** The PE image size. */
752 uint32_t cbImage;
753 /** The timestamp. */
754 uint32_t uTimestamp;
755 } Dbg;
756
757 /** RTLDRDBGINFOTYPE_CODEVIEW_PDB20*/
758 struct
759 {
760 /** The PE image size. */
761 uint32_t cbImage;
762 /** The timestamp. */
763 uint32_t uTimestamp;
764 /** The PDB age. */
765 uint32_t uAge;
766 } Pdb20;
767
768 /** RTLDRDBGINFOTYPE_CODEVIEW_PDB70 */
769 struct
770 {
771 /** The PE image size. */
772 uint32_t cbImage;
773 /** The PDB age. */
774 uint32_t uAge;
775 /** The UUID. */
776 RTUUID Uuid;
777 } Pdb70;
778 } u;
779} RTLDRDBGINFO;
780/** Pointer to debug info details. */
781typedef RTLDRDBGINFO *PRTLDRDBGINFO;
782/** Pointer to read only debug info details. */
783typedef RTLDRDBGINFO const *PCRTLDRDBGINFO;
784
785
786/**
787 * Debug info enumerator callback.
788 *
789 * @returns VINF_SUCCESS to continue the enumeration. Any other status code
790 * will cause RTLdrEnumDbgInfo to immediately return with that status.
791 *
792 * @param hLdrMod The module handle.
793 * @param pDbgInfo Pointer to a read only structure with the details.
794 * @param pvUser The user parameter specified to RTLdrEnumDbgInfo.
795 */
796typedef DECLCALLBACK(int) FNRTLDRENUMDBG(RTLDRMOD hLdrMod, PCRTLDRDBGINFO pDbgInfo, void *pvUser);
797/** Pointer to a debug info enumerator callback. */
798typedef FNRTLDRENUMDBG *PFNRTLDRENUMDBG;
799
800/**
801 * Enumerate the debug info contained in the executable image.
802 *
803 * @returns IPRT status code or whatever pfnCallback returns.
804 *
805 * @param hLdrMod The module handle.
806 * @param pvBits Optional pointer to bits returned by
807 * RTLdrGetBits(). This can be used by some module
808 * interpreters to reduce memory consumption.
809 * @param pfnCallback The callback function.
810 * @param pvUser The user argument.
811 */
812RTDECL(int) RTLdrEnumDbgInfo(RTLDRMOD hLdrMod, const void *pvBits, PFNRTLDRENUMDBG pfnCallback, void *pvUser);
813
814
815/**
816 * Loader segment.
817 */
818typedef struct RTLDRSEG
819{
820 /** The segment name. Always set to something. */
821 const char *pszName;
822 /** The length of the segment name. */
823 uint32_t cchName;
824 /** The flat selector to use for the segment (i.e. data/code).
825 * Primarily a way for the user to specify selectors for the LX/LE and NE interpreters. */
826 uint16_t SelFlat;
827 /** The 16-bit selector to use for the segment.
828 * Primarily a way for the user to specify selectors for the LX/LE and NE interpreters. */
829 uint16_t Sel16bit;
830 /** Segment flags. */
831 uint32_t fFlags;
832 /** The segment protection (RTMEM_PROT_XXX). */
833 uint32_t fProt;
834 /** The size of the segment. */
835 RTLDRADDR cb;
836 /** The required segment alignment.
837 * The to 0 if the segment isn't supposed to be mapped. */
838 RTLDRADDR Alignment;
839 /** The link address.
840 * Set to NIL_RTLDRADDR if the segment isn't supposed to be mapped or if
841 * the image doesn't have link addresses. */
842 RTLDRADDR LinkAddress;
843 /** File offset of the segment.
844 * Set to -1 if no file backing (like BSS). */
845 RTFOFF offFile;
846 /** Size of the file bits of the segment.
847 * Set to -1 if no file backing (like BSS). */
848 RTFOFF cbFile;
849 /** The relative virtual address when mapped.
850 * Set to NIL_RTLDRADDR if the segment isn't supposed to be mapped. */
851 RTLDRADDR RVA;
852 /** The size of the segment including the alignment gap up to the next segment when mapped.
853 * This is set to NIL_RTLDRADDR if not implemented. */
854 RTLDRADDR cbMapped;
855} RTLDRSEG;
856/** Pointer to a loader segment. */
857typedef RTLDRSEG *PRTLDRSEG;
858/** Pointer to a read only loader segment. */
859typedef RTLDRSEG const *PCRTLDRSEG;
860
861
862/** @name Segment flags
863 * @{ */
864/** The segment is 16-bit. When not set the default of the target architecture is assumed. */
865#define RTLDRSEG_FLAG_16BIT UINT32_C(1)
866/** The segment requires a 16-bit selector alias. (OS/2) */
867#define RTLDRSEG_FLAG_OS2_ALIAS16 UINT32_C(2)
868/** Conforming segment (x86 weirdness). (OS/2) */
869#define RTLDRSEG_FLAG_OS2_CONFORM UINT32_C(4)
870/** IOPL (ring-2) segment. (OS/2) */
871#define RTLDRSEG_FLAG_OS2_IOPL UINT32_C(8)
872/** @} */
873
874/**
875 * Segment enumerator callback.
876 *
877 * @returns VINF_SUCCESS to continue the enumeration. Any other status code
878 * will cause RTLdrEnumSegments to immediately return with that
879 * status.
880 *
881 * @param hLdrMod The module handle.
882 * @param pSeg The segment information.
883 * @param pvUser The user parameter specified to RTLdrEnumSegments.
884 */
885typedef DECLCALLBACK(int) FNRTLDRENUMSEGS(RTLDRMOD hLdrMod, PCRTLDRSEG pSeg, void *pvUser);
886/** Pointer to a segment enumerator callback. */
887typedef FNRTLDRENUMSEGS *PFNRTLDRENUMSEGS;
888
889/**
890 * Enumerate the debug info contained in the executable image.
891 *
892 * @returns IPRT status code or whatever pfnCallback returns.
893 *
894 * @param hLdrMod The module handle.
895 * @param pfnCallback The callback function.
896 * @param pvUser The user argument.
897 */
898RTDECL(int) RTLdrEnumSegments(RTLDRMOD hLdrMod, PFNRTLDRENUMSEGS pfnCallback, void *pvUser);
899
900/**
901 * Converts a link address to a segment:offset address.
902 *
903 * @returns IPRT status code.
904 *
905 * @param hLdrMod The module handle.
906 * @param LinkAddress The link address to convert.
907 * @param piSeg Where to return the segment index.
908 * @param poffSeg Where to return the segment offset.
909 */
910RTDECL(int) RTLdrLinkAddressToSegOffset(RTLDRMOD hLdrMod, RTLDRADDR LinkAddress, uint32_t *piSeg, PRTLDRADDR poffSeg);
911
912/**
913 * Converts a link address to an image relative virtual address (RVA).
914 *
915 * @returns IPRT status code.
916 *
917 * @param hLdrMod The module handle.
918 * @param LinkAddress The link address to convert.
919 * @param pRva Where to return the RVA.
920 */
921RTDECL(int) RTLdrLinkAddressToRva(RTLDRMOD hLdrMod, RTLDRADDR LinkAddress, PRTLDRADDR pRva);
922
923/**
924 * Converts an image relative virtual address (RVA) to a segment:offset.
925 *
926 * @returns IPRT status code.
927 *
928 * @param hLdrMod The module handle.
929 * @param Rva The link address to convert.
930 * @param piSeg Where to return the segment index.
931 * @param poffSeg Where to return the segment offset.
932 */
933RTDECL(int) RTLdrSegOffsetToRva(RTLDRMOD hLdrMod, uint32_t iSeg, RTLDRADDR offSeg, PRTLDRADDR pRva);
934
935/**
936 * Converts a segment:offset into an image relative virtual address (RVA).
937 *
938 * @returns IPRT status code.
939 *
940 * @param hLdrMod The module handle.
941 * @param iSeg The segment index.
942 * @param offSeg The segment offset.
943 * @param pRva Where to return the RVA.
944 */
945RTDECL(int) RTLdrRvaToSegOffset(RTLDRMOD hLdrMod, RTLDRADDR Rva, uint32_t *piSeg, PRTLDRADDR poffSeg);
946
947/**
948 * Gets the image format.
949 *
950 * @returns Valid image format on success. RTLDRFMT_INVALID on invalid handle or
951 * other errors.
952 * @param hLdrMod The module handle.
953 */
954RTDECL(RTLDRFMT) RTLdrGetFormat(RTLDRMOD hLdrMod);
955
956/**
957 * Gets the image type.
958 *
959 * @returns Valid image type value on success. RTLDRTYPE_INVALID on
960 * invalid handle or other errors.
961 * @param hLdrMod The module handle.
962 */
963RTDECL(RTLDRTYPE) RTLdrGetType(RTLDRMOD hLdrMod);
964
965/**
966 * Gets the image endian-ness.
967 *
968 * @returns Valid image endian value on success. RTLDRENDIAN_INVALID on invalid
969 * handle or other errors.
970 * @param hLdrMod The module handle.
971 */
972RTDECL(RTLDRENDIAN) RTLdrGetEndian(RTLDRMOD hLdrMod);
973
974/**
975 * Gets the image endian-ness.
976 *
977 * @returns Valid image architecture value on success.
978 * RTLDRARCH_INVALID on invalid handle or other errors.
979 * @param hLdrMod The module handle.
980 */
981RTDECL(RTLDRARCH) RTLdrGetArch(RTLDRMOD hLdrMod);
982
983/**
984 * Loader properties that can be queried thru RTLdrQueryProp.
985 */
986typedef enum RTLDRPROP
987{
988 RTLDRPROP_INVALID = 0,
989 /** The image UUID (Mach-O).
990 * Returns a RTUUID in the buffer. */
991 RTLDRPROP_UUID,
992 /** The image timestamp in seconds, genrally since unix epoc.
993 * Returns a 32-bit or 64-bit signed integer value in the buffer. */
994 RTLDRPROP_TIMESTAMP_SECONDS,
995 /** Checks if the image is signed.
996 * Returns a bool. */
997 RTLDRPROP_IS_SIGNED,
998 /** Retrives the PKCS \#7 SignedData blob that signs the image.
999 * Returns variable sized buffer containing the ASN.1 BER encoding.
1000 *
1001 * @remarks This generally starts with a PKCS \#7 Content structure, the
1002 * SignedData bit is found a few levels down into this as per RFC. */
1003 RTLDRPROP_PKCS7_SIGNED_DATA,
1004
1005 /** Query whether code signature checks are enabled. */
1006 RTLDRPROP_SIGNATURE_CHECKS_ENFORCED,
1007
1008 /** Number of import or needed modules. */
1009 RTLDRPROP_IMPORT_COUNT,
1010 /** Import module by index (32-bit) stored in the buffer. */
1011 RTLDRPROP_IMPORT_MODULE,
1012
1013 /** End of valid properties. */
1014 RTLDRPROP_END,
1015 /** Blow the type up to 32 bits. */
1016 RTLDRPROP_32BIT_HACK = 0x7fffffff
1017} RTLDRPROP;
1018
1019/**
1020 * Generic method for querying image properties.
1021 *
1022 * @returns IPRT status code.
1023 * @retval VERR_NOT_SUPPORTED if the property query isn't supported (either all
1024 * or that specific property). The caller must handle this result.
1025 * @retval VERR_NOT_FOUND the property was not found in the module. The caller
1026 * must also normally deal with this.
1027 * @retval VERR_INVALID_FUNCTION if the function value is wrong.
1028 * @retval VERR_INVALID_PARAMETER if the buffer size is wrong.
1029 * @retval VERR_BUFFER_OVERFLOW if the function doesn't have a fixed size
1030 * buffer and the buffer isn't big enough. Use RTLdrQueryPropEx.
1031 * @retval VERR_INVALID_HANDLE if the handle is invalid.
1032 *
1033 * @param hLdrMod The module handle.
1034 * @param enmLdrProp The property to query.
1035 * @param pvBuf Pointer to the input / output buffer. In most cases
1036 * it's only used for returning data.
1037 * @param cbBuf The size of the buffer.
1038 */
1039RTDECL(int) RTLdrQueryProp(RTLDRMOD hLdrMod, RTLDRPROP enmProp, void *pvBuf, size_t cbBuf);
1040
1041/**
1042 * Generic method for querying image properties, extended version.
1043 *
1044 * @returns IPRT status code.
1045 * @retval VERR_NOT_SUPPORTED if the property query isn't supported (either all
1046 * or that specific property). The caller must handle this result.
1047 * @retval VERR_NOT_FOUND the property was not found in the module. The caller
1048 * must also normally deal with this.
1049 * @retval VERR_INVALID_FUNCTION if the function value is wrong.
1050 * @retval VERR_INVALID_PARAMETER if the fixed buffer size is wrong. Correct
1051 * size in @a *pcbRet.
1052 * @retval VERR_BUFFER_OVERFLOW if the function doesn't have a fixed size
1053 * buffer and the buffer isn't big enough. Correct size in @a *pcbRet.
1054 * @retval VERR_INVALID_HANDLE if the handle is invalid.
1055 *
1056 * @param hLdrMod The module handle.
1057 * @param enmLdrProp The property to query.
1058 * @param pvBits Optional pointer to bits returned by
1059 * RTLdrGetBits(). This can be utilized by some module
1060 * interpreters to reduce memory consumption and file
1061 * access.
1062 * @param pvBuf Pointer to the input / output buffer. In most cases
1063 * it's only used for returning data.
1064 * @param cbBuf The size of the buffer.
1065 * @param pcbRet Where to return the amount of data returned. On
1066 * buffer size errors, this is set to the correct size.
1067 * Optional.
1068 */
1069RTDECL(int) RTLdrQueryPropEx(RTLDRMOD hLdrMod, RTLDRPROP enmProp, void *pvBits, void *pvBuf, size_t cbBuf, size_t *pcbBuf);
1070
1071
1072/**
1073 * Signature type, see FNRTLDRVALIDATESIGNEDDATA.
1074 */
1075typedef enum RTLDRSIGNATURETYPE
1076{
1077 /** Invalid value. */
1078 RTLDRSIGNATURETYPE_INVALID = 0,
1079 /** A RTPKCS7CONTENTINFO structure w/ RTPKCS7SIGNEDDATA inside.
1080 * It's parsed, so the whole binary ASN.1 representation can be found by
1081 * using RTASN1CORE_GET_RAW_ASN1_PTR() and RTASN1CORE_GET_RAW_ASN1_SIZE(). */
1082 RTLDRSIGNATURETYPE_PKCS7_SIGNED_DATA,
1083 /** End of valid values. */
1084 RTLDRSIGNATURETYPE_END,
1085 /** Make sure the size is 32-bit. */
1086 RTLDRSIGNATURETYPE_32BIT_HACK = 0x7fffffff
1087} RTLDRSIGNATURETYPE;
1088
1089/**
1090 * Callback used by RTLdrVerifySignature to verify the signature and associated
1091 * certificates.
1092 *
1093 * @returns IPRT status code.
1094 * @param hLdrMod The module handle.
1095 * @param enmSignature The signature format.
1096 * @param pvSignature The signature data. Format given by @a enmSignature.
1097 * @param cbSignature The size of the buffer @a pvSignature points to.
1098 * @param pErrInfo Pointer to an error info buffer, optional.
1099 * @param pvUser User argument.
1100 *
1101 */
1102typedef DECLCALLBACK(int) FNRTLDRVALIDATESIGNEDDATA(RTLDRMOD hLdrMod, RTLDRSIGNATURETYPE enmSignature, void const *pvSignature, size_t cbSignature,
1103 PRTERRINFO pErrInfo, void *pvUser);
1104/** Pointer to a signature verification callback. */
1105typedef FNRTLDRVALIDATESIGNEDDATA *PFNRTLDRVALIDATESIGNEDDATA;
1106
1107/**
1108 * Verify the image signature.
1109 *
1110 * This may permform additional integrity checks on the image structures that
1111 * was not done when opening the image.
1112 *
1113 * @returns IPRT status code.
1114 * @retval VERR_LDRVI_NOT_SIGNED if not signed.
1115 *
1116 * @param hLdrMod The module handle.
1117 * @param pfnCallback Callback that does the signature and certificate
1118 * verficiation.
1119 * @param pvUser User argument for the callback.
1120 * @param pErrInfo Pointer to an error info buffer. Optional.
1121 */
1122RTDECL(int) RTLdrVerifySignature(RTLDRMOD hLdrMod, PFNRTLDRVALIDATESIGNEDDATA pfnCallback, void *pvUser, PRTERRINFO pErrInfo);
1123
1124/**
1125 * Calculate the image hash according the image signing rules.
1126 *
1127 * @returns IPRT status code.
1128 * @param hLdrMod The module handle.
1129 * @param enmDigest Which kind of digest.
1130 * @param pszDigest Where to store the image digest.
1131 * @param cbDigest Size of the buffer @a pszDigest points at.
1132 */
1133RTDECL(int) RTLdrHashImage(RTLDRMOD hLdrMod, RTDIGESTTYPE enmDigest, char *pszDigest, size_t cbDigest);
1134
1135RT_C_DECLS_END
1136
1137/** @} */
1138
1139#endif
1140
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