VirtualBox

source: vbox/trunk/include/iprt/runtime-loader.h@ 77807

Last change on this file since 77807 was 76553, checked in by vboxsync, 6 years ago

scm --update-copyright-year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.2 KB
RevLine 
[23860]1/** @file
[28363]2 * IPRT - Runtime Loader Generation.
[23860]3 */
4
5/*
[76553]6 * Copyright (C) 2008-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
[28363]26#include <iprt/types.h>
27#ifdef RT_RUNTIME_LOADER_GENERATE_BODY_STUBS
28# include <iprt/ldr.h>
29# include <iprt/log.h>
30# include <iprt/once.h>
31#endif
32
33/** @defgroup grp_rt_runtime_loader Runtime Loader Generation
[36511]34 * @ingroup grp_rt
[28357]35 *
[28363]36 * How to use this loader generator
37 *
[28357]38 * This loader generator can be used to generate stub code for loading a shared
39 * library and its functions at runtime, or for generating a header file with
40 * the declaration of the loader function and optionally declarations for the
41 * functions loaded. It should be included in a header file or a C source
42 * file, after defining certain macros which it makes use of.
43 *
44 * To generate the C source code for function proxy stubs and the library
45 * loader function, you should define the following macros in your source file
46 * before including this header:
47 *
48 * RT_RUNTIME_LOADER_LIB_NAME - the file name of the library to load
49 * RT_RUNTIME_LOADER_FUNCTION - the name of the loader function
50 * RT_RUNTIME_LOADER_INSERT_SYMBOLS - a macro containing the names of the
51 * functions to be loaded, defined in the
52 * following pattern:
[28363]53 * @code
[28357]54 * #define RT_RUNTIME_LOADER_INSERT_SYMBOLS \
55 * RT_PROXY_STUB(func_name, ret_type, (long_param_list), (short_param_list)) \
56 * RT_PROXY_STUB(func_name2, ret_type2, (long_param_list2), (short_param_list2)) \
57 * ...
[28363]58 * @endcode
[28357]59 *
[33540]60 * where long_param_list is a parameter list for declaring the function of the
[28357]61 * form (type1 arg1, type2 arg2, ...) and short_param_list for calling it, of
62 * the form (arg1, arg2, ...).
63 *
64 * To generate the header file, you should define RT_RUNTIME_LOADER_FUNCTION
65 * and if you wish to generate declarations for the functions you should
66 * additionally define RT_RUNTIME_LOADER_INSERT_SYMBOLS as above and
67 * RT_RUNTIME_LOADER_GENERATE_DECLS (without a value) before including this
68 * file.
[28363]69 *
70 * @{
[28357]71 */
72/** @todo this is far too complicated. A script for generating the files would
[28363]73 * probably be preferable.
74 *
75 * bird> An alternative is to generate assembly jump wrappers, this only
76 * requires the symbol names and prefix. I've done this ages ago when we forked
77 * the EMX/GCC toolchain on OS/2... It's a wee bit more annoying in x86 PIC/PIE
78 * mode, but nothing that cannot be dealt with.
79 */
80/** @todo r=bird: The use of RTR3DECL here is an unresolved issue. */
81/** @todo r=bird: The lack of RT_C_DECLS_BEGIN/END is an unresolved issue. Here
82 * we'll get into trouble if we use the same symbol names as the
83 * original! */
84/** @todo r=bird: The prefix usage here is very confused: RT_RUNTIME_LOADER_XXX,
85 * RT_PROXY_STUB, etc. */
[23860]86
[28363]87#ifdef RT_RUNTIME_LOADER_GENERATE_BODY_STUBS
[23860]88
[28363]89/* The following are the symbols which we need from the library. */
[28357]90# define RT_PROXY_STUB(function, rettype, signature, shortsig) \
[28363]91 void (*function ## _fn)(void); \
92 RTR3DECL(rettype) function signature \
93 { return ( (rettype (*) signature) function ## _fn ) shortsig; }
[23860]94
[28357]95RT_RUNTIME_LOADER_INSERT_SYMBOLS
[23860]96
[28357]97# undef RT_PROXY_STUB
[23860]98
[28363]99/* Now comes a table of functions to be loaded from the library. */
[23860]100typedef struct
101{
[28363]102 const char *pszName;
103 void (**ppfn)(void);
104} RTLDRSHAREDFUNC;
[23860]105
[28357]106# define RT_PROXY_STUB(s, dummy1, dummy2, dummy3 ) { #s , & s ## _fn } ,
[28363]107static RTLDRSHAREDFUNC g_aSharedFuncs[] =
[23860]108{
[28363]109 RT_RUNTIME_LOADER_INSERT_SYMBOLS
[23860]110 { NULL, NULL }
111};
[28357]112# undef RT_PROXY_STUB
[23860]113
[28363]114/**
115 * The function which does the actual work for RT_RUNTIME_LOADER_FUNCTION,
116 * serialised for thread safety.
117 */
[43879]118static DECLCALLBACK(int) rtldrLoadOnce(void *)
[23860]119{
[28363]120 RTLDRMOD hLib;
121 int rc;
[23860]122
[28361]123 LogFlowFunc(("\n"));
[28363]124 rc = RTLdrLoad(RT_RUNTIME_LOADER_LIB_NAME, &hLib);
125 for (unsigned i = 0; RT_SUCCESS(rc) && g_aSharedFuncs[i].pszName != NULL; ++i)
126 rc = RTLdrGetSymbol(hLib, g_aSharedFuncs[i].pszName, (void **)g_aSharedFuncs[i].ppfn);
[28361]127 LogFlowFunc(("rc = %Rrc\n", rc));
[28363]128
[28361]129 return rc;
130}
[23860]131
[28363]132/**
133 * Load the shared library RT_RUNTIME_LOADER_LIB_NAME and resolve the symbols
134 * pointed to by RT_RUNTIME_LOADER_INSERT_SYMBOLS.
135 *
136 * May safely be called from multiple threads and will not return until the
137 * library is loaded or has failed to load.
138 *
139 * @returns IPRT status code.
140 */
[28357]141RTR3DECL(int) RT_RUNTIME_LOADER_FUNCTION(void)
[23860]142{
[28363]143 static RTONCE s_Once = RTONCE_INITIALIZER;
144 int rc;
[23860]145
146 LogFlowFunc(("\n"));
[43879]147 rc = RTOnce(&s_Once, rtldrLoadOnce, NULL);
[23860]148 LogFlowFunc(("rc = %Rrc\n", rc));
[28363]149
[23860]150 return rc;
151}
[28361]152
[28357]153#elif defined(RT_RUNTIME_LOADER_GENERATE_HEADER)
154# ifdef RT_RUNTIME_LOADER_GENERATE_DECLS
155/* Declarations of the functions that we need from
156 * RT_RUNTIME_LOADER_LIB_NAME */
157# define RT_PROXY_STUB(function, rettype, signature, shortsig) \
[68675]158 RTR3DECL(rettype) function signature ;
[28357]159
160RT_RUNTIME_LOADER_INSERT_SYMBOLS
161
162# undef RT_PROXY_STUB
163# endif /* RT_RUNTIME_LOADER_GENERATE_DECLS */
164
165/**
166 * Try to dynamically load the library. This function should be called before
167 * attempting to use any of the library functions. It is safe to call this
168 * function multiple times.
169 *
170 * @returns iprt status code
171 */
172RTR3DECL(int) RT_RUNTIME_LOADER_FUNCTION(void);
[28363]173
[28357]174#else
[28363]175# error "One of RT_RUNTIME_LOADER_GENERATE_HEADER or RT_RUNTIME_LOADER_GENERATE_BODY_STUBS must be defined when including this file"
[28357]176#endif
[28363]177
178/** @} */
179
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette