1 | /** @file
|
---|
2 | * IPRT - Lazy share library linking (2nd try).
|
---|
3 | */
|
---|
4 |
|
---|
5 | /*
|
---|
6 | * Copyright (C) 2013-2016 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_ldrlazy_h
|
---|
27 | #define ___iprt_ldrlazy_h
|
---|
28 |
|
---|
29 | #include <iprt/ldr.h>
|
---|
30 |
|
---|
31 | /** @defgroup grp_rt_ldrlazy RTLdrLazy - Lazy shared library linking.
|
---|
32 | * @ingroup grp_rt
|
---|
33 | *
|
---|
34 | * This is a set of macros which will produce code for dynamically loading and
|
---|
35 | * resolving symbols in shared libraries (DLLs).
|
---|
36 | *
|
---|
37 | * There is an assembly language alternative to this that only requires writing
|
---|
38 | * a list of symbols in a format similar to what the microsoft linkers take as
|
---|
39 | * input when producing DLLs and import libraries. That is probably preferable
|
---|
40 | * over this code. See src/bldprog/VBoxDef2LazyLoad.cpp.
|
---|
41 | *
|
---|
42 | * @{
|
---|
43 | */
|
---|
44 |
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * Defines a module for use in lazy resolving.
|
---|
48 | *
|
---|
49 | * @param a_Mod The module name (C name).
|
---|
50 | * @param a_pszFile The file to tell RTLdrLoad to load.
|
---|
51 | */
|
---|
52 | #define RTLDRLAZY_MODULE(a_Mod, a_pszFile) \
|
---|
53 | RTLDRLAZY_MODULE_EX(a_Mod, a_pszFile, RTLdrLoad)
|
---|
54 |
|
---|
55 | /**
|
---|
56 | * Defines a module for use in lazy resolving.
|
---|
57 | *
|
---|
58 | * @param a_Mod The module name (C name).
|
---|
59 | * @param a_pszFile The file to tell RTLdrLoad to load.
|
---|
60 | * @param a_pfnLoadIt Function to call for loading the DLL, replacing
|
---|
61 | * RTLdrLoad.
|
---|
62 | */
|
---|
63 | #define RTLDRLAZY_MODULE_EX(a_Mod, a_pszFile, a_pfnLoadIt) \
|
---|
64 | static bool rtLdrLazy_##a_Mod##_Resolve(const char *pszName, void **ppvSymbol) \
|
---|
65 | { \
|
---|
66 | static RTLDRMOD volatile s_hMod = NIL_RTLDRMOD; \
|
---|
67 | static bool volatile s_fLoaded = false; \
|
---|
68 | RTLDRMOD hMod; \
|
---|
69 | int rc; \
|
---|
70 | if (!s_fLoaded) \
|
---|
71 | { \
|
---|
72 | rc = a_pfnLoadIt(a_pszFile, &hMod); \
|
---|
73 | s_hMod = RT_SUCCESS(rc) ? hMod : NIL_RTLDRMOD; \
|
---|
74 | s_fLoaded = true; \
|
---|
75 | if (RT_FAILURE(rc)) \
|
---|
76 | return false; \
|
---|
77 | } \
|
---|
78 | hMod = s_hMod; \
|
---|
79 | if (hMod == NIL_RTLDRMOD) \
|
---|
80 | return false; \
|
---|
81 | rc = RTLdrGetSymbol(hMod, pszName, ppvSymbol); \
|
---|
82 | return RT_SUCCESS(rc); \
|
---|
83 | }
|
---|
84 |
|
---|
85 |
|
---|
86 |
|
---|
87 | /** Function name mangler for preventing collision with system prototypes. */
|
---|
88 | #define RTLDRLAZY_FUNC_NAME(a_Mod, a_Name) a_Mod##__##a_Name
|
---|
89 |
|
---|
90 | /**
|
---|
91 | * Defines a function that should be lazily resolved.
|
---|
92 | */
|
---|
93 | #define RTLDRLAZY_FUNC(a_Mod, a_RetType, a_CallConv, a_Name, a_ParamDecl, a_ParamNames, a_ErrRet) \
|
---|
94 | DECLINLINE(a_RetType) RTLDRLAZY_FUNC_NAME(a_Mod, a_Name) a_ParamDecl \
|
---|
95 | { \
|
---|
96 | static a_RetType (a_CallConv * s_pfn) a_ParamDecl; \
|
---|
97 | if (!s_pfn) \
|
---|
98 | { \
|
---|
99 | if (!rtLdrLazy_##a_Mod##_Resolve(#a_Name, (void **)&s_pfn)) \
|
---|
100 | return a_ErrRet; \
|
---|
101 | } \
|
---|
102 | return s_pfn a_ParamNames; \
|
---|
103 | }
|
---|
104 |
|
---|
105 |
|
---|
106 | /** @} */
|
---|
107 |
|
---|
108 | #endif
|
---|
109 |
|
---|