948e859a0f72976ea9b302a6b507dfb9409dc7e1vboxsync/** @file
948e859a0f72976ea9b302a6b507dfb9409dc7e1vboxsync * IPRT - Lazy share library linking (2nd try).
948e859a0f72976ea9b302a6b507dfb9409dc7e1vboxsync */
948e859a0f72976ea9b302a6b507dfb9409dc7e1vboxsync
948e859a0f72976ea9b302a6b507dfb9409dc7e1vboxsync/*
948e859a0f72976ea9b302a6b507dfb9409dc7e1vboxsync * Copyright (C) 2013 Oracle Corporation
948e859a0f72976ea9b302a6b507dfb9409dc7e1vboxsync *
948e859a0f72976ea9b302a6b507dfb9409dc7e1vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
948e859a0f72976ea9b302a6b507dfb9409dc7e1vboxsync * available from http://www.virtualbox.org. This file is free software;
948e859a0f72976ea9b302a6b507dfb9409dc7e1vboxsync * you can redistribute it and/or modify it under the terms of the GNU
948e859a0f72976ea9b302a6b507dfb9409dc7e1vboxsync * General Public License (GPL) as published by the Free Software
948e859a0f72976ea9b302a6b507dfb9409dc7e1vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
948e859a0f72976ea9b302a6b507dfb9409dc7e1vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
948e859a0f72976ea9b302a6b507dfb9409dc7e1vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
948e859a0f72976ea9b302a6b507dfb9409dc7e1vboxsync *
948e859a0f72976ea9b302a6b507dfb9409dc7e1vboxsync * The contents of this file may alternatively be used under the terms
948e859a0f72976ea9b302a6b507dfb9409dc7e1vboxsync * of the Common Development and Distribution License Version 1.0
948e859a0f72976ea9b302a6b507dfb9409dc7e1vboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
948e859a0f72976ea9b302a6b507dfb9409dc7e1vboxsync * VirtualBox OSE distribution, in which case the provisions of the
948e859a0f72976ea9b302a6b507dfb9409dc7e1vboxsync * CDDL are applicable instead of those of the GPL.
948e859a0f72976ea9b302a6b507dfb9409dc7e1vboxsync *
948e859a0f72976ea9b302a6b507dfb9409dc7e1vboxsync * You may elect to license modified versions of this file under the
948e859a0f72976ea9b302a6b507dfb9409dc7e1vboxsync * terms and conditions of either the GPL or the CDDL or both.
948e859a0f72976ea9b302a6b507dfb9409dc7e1vboxsync */
948e859a0f72976ea9b302a6b507dfb9409dc7e1vboxsync
948e859a0f72976ea9b302a6b507dfb9409dc7e1vboxsync#ifndef ___iprt_ldrlazy_h
948e859a0f72976ea9b302a6b507dfb9409dc7e1vboxsync#define ___iprt_ldrlazy_h
948e859a0f72976ea9b302a6b507dfb9409dc7e1vboxsync
948e859a0f72976ea9b302a6b507dfb9409dc7e1vboxsync#include <iprt/ldr.h>
948e859a0f72976ea9b302a6b507dfb9409dc7e1vboxsync
948e859a0f72976ea9b302a6b507dfb9409dc7e1vboxsync/** @defgroup grp_rt_ldrlazy Lazy shared library linking.
948e859a0f72976ea9b302a6b507dfb9409dc7e1vboxsync * @ingroup grp_rt
948e859a0f72976ea9b302a6b507dfb9409dc7e1vboxsync *
948e859a0f72976ea9b302a6b507dfb9409dc7e1vboxsync *
948e859a0f72976ea9b302a6b507dfb9409dc7e1vboxsync * @{
948e859a0f72976ea9b302a6b507dfb9409dc7e1vboxsync */
948e859a0f72976ea9b302a6b507dfb9409dc7e1vboxsync
948e859a0f72976ea9b302a6b507dfb9409dc7e1vboxsync
948e859a0f72976ea9b302a6b507dfb9409dc7e1vboxsync/**
948e859a0f72976ea9b302a6b507dfb9409dc7e1vboxsync * Defines a module for use in lazy resolving.
948e859a0f72976ea9b302a6b507dfb9409dc7e1vboxsync *
948e859a0f72976ea9b302a6b507dfb9409dc7e1vboxsync * @param a_Mod The module name (C name).
948e859a0f72976ea9b302a6b507dfb9409dc7e1vboxsync * @param a_pszFile The file to tell RTLdrLoad to load.
948e859a0f72976ea9b302a6b507dfb9409dc7e1vboxsync */
948e859a0f72976ea9b302a6b507dfb9409dc7e1vboxsync#define RTLDRLAZY_MODULE(a_Mod, a_pszFile) \
948e859a0f72976ea9b302a6b507dfb9409dc7e1vboxsync RTLDRLAZY_MODULE_EX(a_Mod, a_pszFile, RTLdrLoad)
948e859a0f72976ea9b302a6b507dfb9409dc7e1vboxsync
948e859a0f72976ea9b302a6b507dfb9409dc7e1vboxsync/**
948e859a0f72976ea9b302a6b507dfb9409dc7e1vboxsync * Defines a module for use in lazy resolving.
948e859a0f72976ea9b302a6b507dfb9409dc7e1vboxsync *
948e859a0f72976ea9b302a6b507dfb9409dc7e1vboxsync * @param a_Mod The module name (C name).
948e859a0f72976ea9b302a6b507dfb9409dc7e1vboxsync * @param a_pszFile The file to tell RTLdrLoad to load.
948e859a0f72976ea9b302a6b507dfb9409dc7e1vboxsync * @param a_pfnLoadIt Function to call for loading the DLL, replacing
948e859a0f72976ea9b302a6b507dfb9409dc7e1vboxsync * RTLdrLoad.
948e859a0f72976ea9b302a6b507dfb9409dc7e1vboxsync */
948e859a0f72976ea9b302a6b507dfb9409dc7e1vboxsync#define RTLDRLAZY_MODULE_EX(a_Mod, a_pszFile, a_pfnLoadIt) \
948e859a0f72976ea9b302a6b507dfb9409dc7e1vboxsync static bool rtLdrLazy_##a_Mod##_Resolve(const char *pszName, void **ppvSymbol) \
948e859a0f72976ea9b302a6b507dfb9409dc7e1vboxsync { \
948e859a0f72976ea9b302a6b507dfb9409dc7e1vboxsync static RTLDRMOD volatile s_hMod = NIL_RTLDRMOD; \
948e859a0f72976ea9b302a6b507dfb9409dc7e1vboxsync static bool volatile s_fLoaded = false; \
948e859a0f72976ea9b302a6b507dfb9409dc7e1vboxsync RTLDRMOD hMod; \
948e859a0f72976ea9b302a6b507dfb9409dc7e1vboxsync int rc; \
948e859a0f72976ea9b302a6b507dfb9409dc7e1vboxsync if (!s_fLoaded) \
948e859a0f72976ea9b302a6b507dfb9409dc7e1vboxsync { \
948e859a0f72976ea9b302a6b507dfb9409dc7e1vboxsync rc = a_pfnLoadIt(a_pszFile, &hMod); \
948e859a0f72976ea9b302a6b507dfb9409dc7e1vboxsync s_hMod = RT_SUCCESS(rc) ? hMod : NIL_RTLDRMOD; \
948e859a0f72976ea9b302a6b507dfb9409dc7e1vboxsync s_fLoaded = true; \
948e859a0f72976ea9b302a6b507dfb9409dc7e1vboxsync if (RT_FAILURE(rc)) \
948e859a0f72976ea9b302a6b507dfb9409dc7e1vboxsync return false; \
948e859a0f72976ea9b302a6b507dfb9409dc7e1vboxsync } \
948e859a0f72976ea9b302a6b507dfb9409dc7e1vboxsync hMod = s_hMod; \
948e859a0f72976ea9b302a6b507dfb9409dc7e1vboxsync if (hMod == NIL_RTLDRMOD) \
948e859a0f72976ea9b302a6b507dfb9409dc7e1vboxsync return false; \
948e859a0f72976ea9b302a6b507dfb9409dc7e1vboxsync rc = RTLdrGetSymbol(hMod, pszName, ppvSymbol); \
948e859a0f72976ea9b302a6b507dfb9409dc7e1vboxsync return RT_SUCCESS(rc); \
948e859a0f72976ea9b302a6b507dfb9409dc7e1vboxsync }
948e859a0f72976ea9b302a6b507dfb9409dc7e1vboxsync
948e859a0f72976ea9b302a6b507dfb9409dc7e1vboxsync
948e859a0f72976ea9b302a6b507dfb9409dc7e1vboxsync
948e859a0f72976ea9b302a6b507dfb9409dc7e1vboxsync/** Function name mangler for preventing collision with system prototypes. */
948e859a0f72976ea9b302a6b507dfb9409dc7e1vboxsync#define RTLDRLAZY_FUNC_NAME(a_Mod, a_Name) a_Mod##__##a_Name
948e859a0f72976ea9b302a6b507dfb9409dc7e1vboxsync
948e859a0f72976ea9b302a6b507dfb9409dc7e1vboxsync/**
948e859a0f72976ea9b302a6b507dfb9409dc7e1vboxsync * Defines a function that should be lazily resolved.
948e859a0f72976ea9b302a6b507dfb9409dc7e1vboxsync */
948e859a0f72976ea9b302a6b507dfb9409dc7e1vboxsync#define RTLDRLAZY_FUNC(a_Mod, a_RetType, a_CallConv, a_Name, a_ParamDecl, a_ParamNames, a_ErrRet) \
948e859a0f72976ea9b302a6b507dfb9409dc7e1vboxsync DECLINLINE(a_RetType) RTLDRLAZY_FUNC_NAME(a_Mod, a_Name) a_ParamDecl \
948e859a0f72976ea9b302a6b507dfb9409dc7e1vboxsync { \
948e859a0f72976ea9b302a6b507dfb9409dc7e1vboxsync static a_RetType (a_CallConv * s_pfn) a_ParamDecl; \
948e859a0f72976ea9b302a6b507dfb9409dc7e1vboxsync if (!s_pfn) \
948e859a0f72976ea9b302a6b507dfb9409dc7e1vboxsync { \
948e859a0f72976ea9b302a6b507dfb9409dc7e1vboxsync if (!rtLdrLazy_##a_Mod##_Resolve(#a_Name, (void **)&s_pfn)) \
948e859a0f72976ea9b302a6b507dfb9409dc7e1vboxsync return a_ErrRet; \
948e859a0f72976ea9b302a6b507dfb9409dc7e1vboxsync } \
948e859a0f72976ea9b302a6b507dfb9409dc7e1vboxsync return s_pfn a_ParamNames; \
948e859a0f72976ea9b302a6b507dfb9409dc7e1vboxsync }
948e859a0f72976ea9b302a6b507dfb9409dc7e1vboxsync
948e859a0f72976ea9b302a6b507dfb9409dc7e1vboxsync
948e859a0f72976ea9b302a6b507dfb9409dc7e1vboxsync/** @} */
948e859a0f72976ea9b302a6b507dfb9409dc7e1vboxsync
948e859a0f72976ea9b302a6b507dfb9409dc7e1vboxsync#endif
948e859a0f72976ea9b302a6b507dfb9409dc7e1vboxsync