[23860] | 1 | /** @file
|
---|
[45928] | 2 | * IPRT - Lazy share library linking (2nd try).
|
---|
[23860] | 3 | */
|
---|
| 4 |
|
---|
| 5 | /*
|
---|
[76553] | 6 | * Copyright (C) 2013-2019 Oracle Corporation
|
---|
[23860] | 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 | *
|
---|
[24409] | 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.
|
---|
[23860] | 24 | */
|
---|
| 25 |
|
---|
[76557] | 26 | #ifndef IPRT_INCLUDED_ldrlazy_h
|
---|
| 27 | #define IPRT_INCLUDED_ldrlazy_h
|
---|
[76507] | 28 | #ifndef RT_WITHOUT_PRAGMA_ONCE
|
---|
| 29 | # pragma once
|
---|
| 30 | #endif
|
---|
[28363] | 31 |
|
---|
[45928] | 32 | #include <iprt/ldr.h>
|
---|
| 33 |
|
---|
[57942] | 34 | /** @defgroup grp_rt_ldrlazy RTLdrLazy - Lazy shared library linking.
|
---|
[36511] | 35 | * @ingroup grp_rt
|
---|
[28357] | 36 | *
|
---|
[57942] | 37 | * This is a set of macros which will produce code for dynamically loading and
|
---|
| 38 | * resolving symbols in shared libraries (DLLs).
|
---|
[28363] | 39 | *
|
---|
[57942] | 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 | *
|
---|
[28363] | 45 | * @{
|
---|
[28357] | 46 | */
|
---|
[23860] | 47 |
|
---|
| 48 |
|
---|
[28363] | 49 | /**
|
---|
[45928] | 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.
|
---|
[28363] | 54 | */
|
---|
[45928] | 55 | #define RTLDRLAZY_MODULE(a_Mod, a_pszFile) \
|
---|
| 56 | RTLDRLAZY_MODULE_EX(a_Mod, a_pszFile, RTLdrLoad)
|
---|
[23860] | 57 |
|
---|
[28363] | 58 | /**
|
---|
[45928] | 59 | * Defines a module for use in lazy resolving.
|
---|
[28363] | 60 | *
|
---|
[45928] | 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.
|
---|
[28363] | 65 | */
|
---|
[45928] | 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 | }
|
---|
[23860] | 87 |
|
---|
[28363] | 88 |
|
---|
[28361] | 89 |
|
---|
[45928] | 90 | /** Function name mangler for preventing collision with system prototypes. */
|
---|
| 91 | #define RTLDRLAZY_FUNC_NAME(a_Mod, a_Name) a_Mod##__##a_Name
|
---|
[28357] | 92 |
|
---|
| 93 | /**
|
---|
[45928] | 94 | * Defines a function that should be lazily resolved.
|
---|
[28357] | 95 | */
|
---|
[45928] | 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 | }
|
---|
[28363] | 107 |
|
---|
| 108 |
|
---|
| 109 | /** @} */
|
---|
| 110 |
|
---|
[76585] | 111 | #endif /* !IPRT_INCLUDED_ldrlazy_h */
|
---|
[45928] | 112 |
|
---|