1 | /** @file
|
---|
2 | * IPRT - Lazy share library linking (2nd try).
|
---|
3 | */
|
---|
4 |
|
---|
5 | /*
|
---|
6 | * Copyright (C) 2013-2024 Oracle and/or its affiliates.
|
---|
7 | *
|
---|
8 | * This file is part of VirtualBox base platform packages, as
|
---|
9 | * available from https://www.virtualbox.org.
|
---|
10 | *
|
---|
11 | * This program is free software; you can redistribute it and/or
|
---|
12 | * modify it under the terms of the GNU General Public License
|
---|
13 | * as published by the Free Software Foundation, in version 3 of the
|
---|
14 | * License.
|
---|
15 | *
|
---|
16 | * This program is distributed in the hope that it will be useful, but
|
---|
17 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
19 | * General Public License for more details.
|
---|
20 | *
|
---|
21 | * You should have received a copy of the GNU General Public License
|
---|
22 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
23 | *
|
---|
24 | * The contents of this file may alternatively be used under the terms
|
---|
25 | * of the Common Development and Distribution License Version 1.0
|
---|
26 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
27 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
28 | * CDDL are applicable instead of those of the GPL.
|
---|
29 | *
|
---|
30 | * You may elect to license modified versions of this file under the
|
---|
31 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
32 | *
|
---|
33 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
34 | */
|
---|
35 |
|
---|
36 | #ifndef IPRT_INCLUDED_ldrlazy_h
|
---|
37 | #define IPRT_INCLUDED_ldrlazy_h
|
---|
38 | #ifndef RT_WITHOUT_PRAGMA_ONCE
|
---|
39 | # pragma once
|
---|
40 | #endif
|
---|
41 |
|
---|
42 | #include <iprt/ldr.h>
|
---|
43 |
|
---|
44 | /** @defgroup grp_rt_ldrlazy RTLdrLazy - Lazy shared library linking.
|
---|
45 | * @ingroup grp_rt
|
---|
46 | *
|
---|
47 | * This is a set of macros which will produce code for dynamically loading and
|
---|
48 | * resolving symbols in shared libraries (DLLs).
|
---|
49 | *
|
---|
50 | * There is an assembly language alternative to this that only requires writing
|
---|
51 | * a list of symbols in a format similar to what the microsoft linkers take as
|
---|
52 | * input when producing DLLs and import libraries. That is probably preferable
|
---|
53 | * over this code. See src/bldprog/VBoxDef2LazyLoad.cpp.
|
---|
54 | *
|
---|
55 | * @{
|
---|
56 | */
|
---|
57 |
|
---|
58 |
|
---|
59 | /**
|
---|
60 | * Defines a module for use in lazy resolving.
|
---|
61 | *
|
---|
62 | * @param a_Mod The module name (C name).
|
---|
63 | * @param a_pszFile The file to tell RTLdrLoad to load.
|
---|
64 | */
|
---|
65 | #define RTLDRLAZY_MODULE(a_Mod, a_pszFile) \
|
---|
66 | RTLDRLAZY_MODULE_EX(a_Mod, a_pszFile, RTLdrLoad)
|
---|
67 |
|
---|
68 | /**
|
---|
69 | * Defines a module for use in lazy resolving.
|
---|
70 | *
|
---|
71 | * @param a_Mod The module name (C name).
|
---|
72 | * @param a_pszFile The file to tell RTLdrLoad to load.
|
---|
73 | * @param a_pfnLoadIt Function to call for loading the DLL, replacing
|
---|
74 | * RTLdrLoad.
|
---|
75 | */
|
---|
76 | #define RTLDRLAZY_MODULE_EX(a_Mod, a_pszFile, a_pfnLoadIt) \
|
---|
77 | static bool rtLdrLazy_##a_Mod##_Resolve(const char *pszName, void **ppvSymbol) \
|
---|
78 | { \
|
---|
79 | static RTLDRMOD volatile s_hMod = NIL_RTLDRMOD; \
|
---|
80 | static bool volatile s_fLoaded = false; \
|
---|
81 | RTLDRMOD hMod; \
|
---|
82 | int rc; \
|
---|
83 | if (!s_fLoaded) \
|
---|
84 | { \
|
---|
85 | rc = a_pfnLoadIt(a_pszFile, &hMod); \
|
---|
86 | s_hMod = RT_SUCCESS(rc) ? hMod : NIL_RTLDRMOD; \
|
---|
87 | s_fLoaded = true; \
|
---|
88 | if (RT_FAILURE(rc)) \
|
---|
89 | return false; \
|
---|
90 | } \
|
---|
91 | hMod = s_hMod; \
|
---|
92 | if (hMod == NIL_RTLDRMOD) \
|
---|
93 | return false; \
|
---|
94 | rc = RTLdrGetSymbol(hMod, pszName, ppvSymbol); \
|
---|
95 | return RT_SUCCESS(rc); \
|
---|
96 | }
|
---|
97 |
|
---|
98 |
|
---|
99 |
|
---|
100 | /** Function name mangler for preventing collision with system prototypes. */
|
---|
101 | #define RTLDRLAZY_FUNC_NAME(a_Mod, a_Name) a_Mod##__##a_Name
|
---|
102 |
|
---|
103 | /**
|
---|
104 | * Defines a function that should be lazily resolved.
|
---|
105 | */
|
---|
106 | #define RTLDRLAZY_FUNC(a_Mod, a_RetType, a_CallConv, a_Name, a_ParamDecl, a_ParamNames, a_ErrRet) \
|
---|
107 | DECLINLINE(a_RetType) RTLDRLAZY_FUNC_NAME(a_Mod, a_Name) a_ParamDecl \
|
---|
108 | { \
|
---|
109 | static a_RetType (a_CallConv * s_pfn) a_ParamDecl; \
|
---|
110 | if (!s_pfn) \
|
---|
111 | { \
|
---|
112 | if (!rtLdrLazy_##a_Mod##_Resolve(#a_Name, (void **)&s_pfn)) \
|
---|
113 | return a_ErrRet; \
|
---|
114 | } \
|
---|
115 | return s_pfn a_ParamNames; \
|
---|
116 | }
|
---|
117 |
|
---|
118 |
|
---|
119 | /** @} */
|
---|
120 |
|
---|
121 | #endif /* !IPRT_INCLUDED_ldrlazy_h */
|
---|
122 |
|
---|