runtime-loader.h revision 8fa59d6e8a7241b88e10a611d883318d157317cf
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe/** @file
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe * IPRT - runtime loader generation
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe */
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe/*
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe * Copyright (C) 2008 Sun Microsystems, Inc.
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe *
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe * This file is part of VirtualBox Open Source Edition (OSE), as
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe * available from http://www.virtualbox.org. This file is free software;
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe * you can redistribute it and/or modify it under the terms of the GNU
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe * General Public License (GPL) as published by the Free Software
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe * Foundation, in version 2 as it comes in the "COPYING" file of the
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe *
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe * The contents of this file may alternatively be used under the terms
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe * of the Common Development and Distribution License Version 1.0
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe * VirtualBox OSE distribution, in which case the provisions of the
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe * CDDL are applicable instead of those of the GPL.
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe *
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe * You may elect to license modified versions of this file under the
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe * terms and conditions of either the GPL or the CDDL or both.
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe *
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe * Clara, CA 95054 USA or visit http://www.sun.com if you need
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe * additional information or have any questions.
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe */
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe/* How to use this loader generator
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe *
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe * This loader generator can be used to generate stub code for loading a shared
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe * library and its functions at runtime, or for generating a header file with
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe * the declaration of the loader function and optionally declarations for the
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe * functions loaded. It should be included in a header file or a C source
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe * file, after defining certain macros which it makes use of.
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe *
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe * To generate the C source code for function proxy stubs and the library
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe * loader function, you should define the following macros in your source file
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe * before including this header:
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe *
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe * RT_RUNTIME_LOADER_LIB_NAME - the file name of the library to load
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe * RT_RUNTIME_LOADER_FUNCTION - the name of the loader function
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe * RT_RUNTIME_LOADER_INSERT_SYMBOLS - a macro containing the names of the
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe * functions to be loaded, defined in the
56e85d89d42a6980f31b800266649efbed338da3wrowe * following pattern:
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe *
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe * #define RT_RUNTIME_LOADER_INSERT_SYMBOLS \
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe * RT_PROXY_STUB(func_name, ret_type, (long_param_list), (short_param_list)) \
37ad54b8fd2611b7a4f2b269eec3d27ed784a25dwrowe * RT_PROXY_STUB(func_name2, ret_type2, (long_param_list2), (short_param_list2)) \
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe * ...
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe *
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe * where long_param_list is a paramter list for declaring the function of the
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe * form (type1 arg1, type2 arg2, ...) and short_param_list for calling it, of
94b262d3639149df0b02642a9daa6db8bff58577wrowe * the form (arg1, arg2, ...).
94b262d3639149df0b02642a9daa6db8bff58577wrowe *
94b262d3639149df0b02642a9daa6db8bff58577wrowe * To generate the header file, you should define RT_RUNTIME_LOADER_FUNCTION
94b262d3639149df0b02642a9daa6db8bff58577wrowe * and if you wish to generate declarations for the functions you should
94b262d3639149df0b02642a9daa6db8bff58577wrowe * additionally define RT_RUNTIME_LOADER_INSERT_SYMBOLS as above and
94b262d3639149df0b02642a9daa6db8bff58577wrowe * RT_RUNTIME_LOADER_GENERATE_DECLS (without a value) before including this
94b262d3639149df0b02642a9daa6db8bff58577wrowe * file.
94b262d3639149df0b02642a9daa6db8bff58577wrowe */
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe/** @todo this is far too complicated. A script for generating the files would
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe * probably be preferable. */
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe#include <iprt/ldr.h>
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe#include <iprt/log.h>
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe#include <iprt/once.h>
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe#ifdef RT_RUNTIME_LOADER_GENERATE_BODY_STUBS
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe/** The following are the symbols which we need from the library. */
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe# define RT_PROXY_STUB(function, rettype, signature, shortsig) \
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe void (*function ## _fn)(void); \
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe RTR3DECL(rettype) function signature \
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe { return ( (rettype (*) signature) function ## _fn ) shortsig; }
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe
5ac28f3fe2417368757f29cf381338357605fd52wroweRT_RUNTIME_LOADER_INSERT_SYMBOLS
e1ad80c048e29e968221817698529d73098f07a4wrowe
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe# undef RT_PROXY_STUB
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe/* Now comes a table of functions to be loaded from the library */
37ad54b8fd2611b7a4f2b269eec3d27ed784a25dwrowetypedef struct
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe{
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe const char *name;
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe void (**fn)(void);
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe} SHARED_FUNC;
94b262d3639149df0b02642a9daa6db8bff58577wrowe
94b262d3639149df0b02642a9daa6db8bff58577wrowe# define RT_PROXY_STUB(s, dummy1, dummy2, dummy3 ) { #s , & s ## _fn } ,
94b262d3639149df0b02642a9daa6db8bff58577wrowestatic SHARED_FUNC SharedFuncs[] =
94b262d3639149df0b02642a9daa6db8bff58577wrowe{
94b262d3639149df0b02642a9daa6db8bff58577wroweRT_RUNTIME_LOADER_INSERT_SYMBOLS
94b262d3639149df0b02642a9daa6db8bff58577wrowe { NULL, NULL }
94b262d3639149df0b02642a9daa6db8bff58577wrowe};
94b262d3639149df0b02642a9daa6db8bff58577wrowe# undef RT_PROXY_STUB
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe/* The function which does the actual work for RT_RUNTIME_LOADER_FUNCTION,
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe * serialised for thread safety. */
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowestatic int rtldrLoadOnce(void *, void *)
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe{
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe RTLDRMOD hLib;
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe LogFlowFunc(("\n"));
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe int rc = RTLdrLoad(RT_RUNTIME_LOADER_LIB_NAME, &hLib);
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe for (unsigned i = 0; RT_SUCCESS(rc) && SharedFuncs[i].name != NULL; ++i)
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe rc = RTLdrGetSymbol(hLib, SharedFuncs[i].name, (void**)SharedFuncs[i].fn);
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe LogFlowFunc(("rc = %Rrc\n", rc));
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe return rc;
8c8173f49dd7122e10636b3d20ae841551bd0b43wrowe}
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe/** Load the shared library RT_RUNTIME_LOADER_LIB_NAME and resolve the symbols
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe * pointed to by RT_RUNTIME_LOADER_INSERT_SYMBOLS. May safely be called from
* multiple threads and will not return until the library is loaded or has
* failed to load. */
RTR3DECL(int) RT_RUNTIME_LOADER_FUNCTION(void)
{
static RTONCE sOnce = RTONCE_INITIALIZER;
LogFlowFunc(("\n"));
int rc = RTOnce (&sOnce, rtldrLoadOnce, NULL, NULL);
LogFlowFunc(("rc = %Rrc\n", rc));
return rc;
}
#elif defined(RT_RUNTIME_LOADER_GENERATE_HEADER)
# ifdef RT_RUNTIME_LOADER_GENERATE_DECLS
/* Declarations of the functions that we need from
* RT_RUNTIME_LOADER_LIB_NAME */
# define RT_PROXY_STUB(function, rettype, signature, shortsig) \
RTR3DECL(rettype) ( function ) signature ;
RT_RUNTIME_LOADER_INSERT_SYMBOLS
# undef RT_PROXY_STUB
# endif /* RT_RUNTIME_LOADER_GENERATE_DECLS */
/**
* Try to dynamically load the library. This function should be called before
* attempting to use any of the library functions. It is safe to call this
* function multiple times.
*
* @returns iprt status code
*/
RTR3DECL(int) RT_RUNTIME_LOADER_FUNCTION(void);
#else
# error One of RT_RUNTIME_LOADER_GENERATE_HEADER or \
RT_RUNTIME_LOADER_GENERATE_BODY_STUBS must be defined when including this file
#endif