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