VirtualBox

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

Last change on this file since 34575 was 33540, checked in by vboxsync, 14 years ago

*: spelling fixes, thanks Timeless!

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