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 | * @ingroup grp_rt
|
---|
35 | *
|
---|
36 | * How to use this loader generator
|
---|
37 | *
|
---|
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:
|
---|
53 | * @code
|
---|
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 | * ...
|
---|
58 | * @endcode
|
---|
59 | *
|
---|
60 | * where long_param_list is a parameter list for declaring the function of the
|
---|
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.
|
---|
69 | *
|
---|
70 | * @{
|
---|
71 | */
|
---|
72 | /** @todo this is far too complicated. A script for generating the files would
|
---|
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. */
|
---|
86 |
|
---|
87 | #ifdef RT_RUNTIME_LOADER_GENERATE_BODY_STUBS
|
---|
88 |
|
---|
89 | /* The following are the symbols which we need from the library. */
|
---|
90 | # define RT_PROXY_STUB(function, rettype, signature, shortsig) \
|
---|
91 | void (*function ## _fn)(void); \
|
---|
92 | RTR3DECL(rettype) function signature \
|
---|
93 | { return ( (rettype (*) signature) function ## _fn ) shortsig; }
|
---|
94 |
|
---|
95 | RT_RUNTIME_LOADER_INSERT_SYMBOLS
|
---|
96 |
|
---|
97 | # undef RT_PROXY_STUB
|
---|
98 |
|
---|
99 | /* Now comes a table of functions to be loaded from the library. */
|
---|
100 | typedef struct
|
---|
101 | {
|
---|
102 | const char *pszName;
|
---|
103 | void (**ppfn)(void);
|
---|
104 | } RTLDRSHAREDFUNC;
|
---|
105 |
|
---|
106 | # define RT_PROXY_STUB(s, dummy1, dummy2, dummy3 ) { #s , & s ## _fn } ,
|
---|
107 | static RTLDRSHAREDFUNC g_aSharedFuncs[] =
|
---|
108 | {
|
---|
109 | RT_RUNTIME_LOADER_INSERT_SYMBOLS
|
---|
110 | { NULL, NULL }
|
---|
111 | };
|
---|
112 | # undef RT_PROXY_STUB
|
---|
113 |
|
---|
114 | /**
|
---|
115 | * The function which does the actual work for RT_RUNTIME_LOADER_FUNCTION,
|
---|
116 | * serialised for thread safety.
|
---|
117 | */
|
---|
118 | static DECLCALLBACK(int) rtldrLoadOnce(void *, void *)
|
---|
119 | {
|
---|
120 | RTLDRMOD hLib;
|
---|
121 | int rc;
|
---|
122 |
|
---|
123 | LogFlowFunc(("\n"));
|
---|
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);
|
---|
127 | LogFlowFunc(("rc = %Rrc\n", rc));
|
---|
128 |
|
---|
129 | return rc;
|
---|
130 | }
|
---|
131 |
|
---|
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 | */
|
---|
141 | RTR3DECL(int) RT_RUNTIME_LOADER_FUNCTION(void)
|
---|
142 | {
|
---|
143 | static RTONCE s_Once = RTONCE_INITIALIZER;
|
---|
144 | int rc;
|
---|
145 |
|
---|
146 | LogFlowFunc(("\n"));
|
---|
147 | rc = RTOnce(&s_Once, rtldrLoadOnce, NULL, NULL);
|
---|
148 | LogFlowFunc(("rc = %Rrc\n", rc));
|
---|
149 |
|
---|
150 | return rc;
|
---|
151 | }
|
---|
152 |
|
---|
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) \
|
---|
158 | RTR3DECL(rettype) ( function ) signature ;
|
---|
159 |
|
---|
160 | RT_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 | */
|
---|
172 | RTR3DECL(int) RT_RUNTIME_LOADER_FUNCTION(void);
|
---|
173 |
|
---|
174 | #else
|
---|
175 | # error "One of RT_RUNTIME_LOADER_GENERATE_HEADER or RT_RUNTIME_LOADER_GENERATE_BODY_STUBS must be defined when including this file"
|
---|
176 | #endif
|
---|
177 |
|
---|
178 | /** @} */
|
---|
179 |
|
---|