[23860] | 1 | /** @file
|
---|
[45928] | 2 | * IPRT - Lazy share library linking (2nd try).
|
---|
[23860] | 3 | */
|
---|
| 4 |
|
---|
| 5 | /*
|
---|
[98103] | 6 | * Copyright (C) 2013-2023 Oracle and/or its affiliates.
|
---|
[23860] | 7 | *
|
---|
[96407] | 8 | * This file is part of VirtualBox base platform packages, as
|
---|
| 9 | * available from https://www.virtualbox.org.
|
---|
[23860] | 10 | *
|
---|
[96407] | 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 | *
|
---|
[24409] | 24 | * The contents of this file may alternatively be used under the terms
|
---|
| 25 | * of the Common Development and Distribution License Version 1.0
|
---|
[96407] | 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
|
---|
[24409] | 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.
|
---|
[96407] | 32 | *
|
---|
| 33 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
[23860] | 34 | */
|
---|
| 35 |
|
---|
[76557] | 36 | #ifndef IPRT_INCLUDED_ldrlazy_h
|
---|
| 37 | #define IPRT_INCLUDED_ldrlazy_h
|
---|
[76507] | 38 | #ifndef RT_WITHOUT_PRAGMA_ONCE
|
---|
| 39 | # pragma once
|
---|
| 40 | #endif
|
---|
[28363] | 41 |
|
---|
[45928] | 42 | #include <iprt/ldr.h>
|
---|
| 43 |
|
---|
[57942] | 44 | /** @defgroup grp_rt_ldrlazy RTLdrLazy - Lazy shared library linking.
|
---|
[36511] | 45 | * @ingroup grp_rt
|
---|
[28357] | 46 | *
|
---|
[57942] | 47 | * This is a set of macros which will produce code for dynamically loading and
|
---|
| 48 | * resolving symbols in shared libraries (DLLs).
|
---|
[28363] | 49 | *
|
---|
[57942] | 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 | *
|
---|
[28363] | 55 | * @{
|
---|
[28357] | 56 | */
|
---|
[23860] | 57 |
|
---|
| 58 |
|
---|
[28363] | 59 | /**
|
---|
[45928] | 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.
|
---|
[28363] | 64 | */
|
---|
[45928] | 65 | #define RTLDRLAZY_MODULE(a_Mod, a_pszFile) \
|
---|
| 66 | RTLDRLAZY_MODULE_EX(a_Mod, a_pszFile, RTLdrLoad)
|
---|
[23860] | 67 |
|
---|
[28363] | 68 | /**
|
---|
[45928] | 69 | * Defines a module for use in lazy resolving.
|
---|
[28363] | 70 | *
|
---|
[45928] | 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.
|
---|
[28363] | 75 | */
|
---|
[45928] | 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 | }
|
---|
[23860] | 97 |
|
---|
[28363] | 98 |
|
---|
[28361] | 99 |
|
---|
[45928] | 100 | /** Function name mangler for preventing collision with system prototypes. */
|
---|
| 101 | #define RTLDRLAZY_FUNC_NAME(a_Mod, a_Name) a_Mod##__##a_Name
|
---|
[28357] | 102 |
|
---|
| 103 | /**
|
---|
[45928] | 104 | * Defines a function that should be lazily resolved.
|
---|
[28357] | 105 | */
|
---|
[45928] | 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 | }
|
---|
[28363] | 117 |
|
---|
| 118 |
|
---|
| 119 | /** @} */
|
---|
| 120 |
|
---|
[76585] | 121 | #endif /* !IPRT_INCLUDED_ldrlazy_h */
|
---|
[45928] | 122 |
|
---|