1 | /** @file
|
---|
2 | * IPRT - runtime loader generation
|
---|
3 | */
|
---|
4 |
|
---|
5 | /*
|
---|
6 | * Copyright (C) 2008 Sun Microsystems, Inc.
|
---|
7 | *
|
---|
8 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
9 | * available from http://www.215389.xyz. 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 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
26 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
27 | * additional information or have any questions.
|
---|
28 | */
|
---|
29 |
|
---|
30 | /* How to use this loader generator
|
---|
31 | *
|
---|
32 | * This loader generator can be used to generate stub code for loading a shared
|
---|
33 | * library and its functions at runtime, or for generating a header file with
|
---|
34 | * the declaration of the loader function and optionally declarations for the
|
---|
35 | * functions loaded. It should be included in a header file or a C source
|
---|
36 | * file, after defining certain macros which it makes use of.
|
---|
37 | *
|
---|
38 | * To generate the C source code for function proxy stubs and the library
|
---|
39 | * loader function, you should define the following macros in your source file
|
---|
40 | * before including this header:
|
---|
41 | *
|
---|
42 | * RT_RUNTIME_LOADER_LIB_NAME - the file name of the library to load
|
---|
43 | * RT_RUNTIME_LOADER_FUNCTION - the name of the loader function
|
---|
44 | * RT_RUNTIME_LOADER_INSERT_SYMBOLS - a macro containing the names of the
|
---|
45 | * functions to be loaded, defined in the
|
---|
46 | * following pattern:
|
---|
47 | *
|
---|
48 | * #define RT_RUNTIME_LOADER_INSERT_SYMBOLS \
|
---|
49 | * RT_PROXY_STUB(func_name, ret_type, (long_param_list), (short_param_list)) \
|
---|
50 | * RT_PROXY_STUB(func_name2, ret_type2, (long_param_list2), (short_param_list2)) \
|
---|
51 | * ...
|
---|
52 | *
|
---|
53 | * where long_param_list is a paramter list for declaring the function of the
|
---|
54 | * form (type1 arg1, type2 arg2, ...) and short_param_list for calling it, of
|
---|
55 | * the form (arg1, arg2, ...).
|
---|
56 | *
|
---|
57 | * To generate the header file, you should define RT_RUNTIME_LOADER_FUNCTION
|
---|
58 | * and if you wish to generate declarations for the functions you should
|
---|
59 | * additionally define RT_RUNTIME_LOADER_INSERT_SYMBOLS as above and
|
---|
60 | * RT_RUNTIME_LOADER_GENERATE_DECLS (without a value) before including this
|
---|
61 | * file.
|
---|
62 | */
|
---|
63 | /** @todo this is far too complicated. A script for generating the files would
|
---|
64 | * probably be preferable. */
|
---|
65 |
|
---|
66 | #include <iprt/ldr.h>
|
---|
67 | #include <iprt/log.h>
|
---|
68 | #include <iprt/once.h>
|
---|
69 |
|
---|
70 | #ifdef RT_RUNTIME_LOADER_GENERATE_BODY_STUBS
|
---|
71 | /** The following are the symbols which we need from the library. */
|
---|
72 | # define RT_PROXY_STUB(function, rettype, signature, shortsig) \
|
---|
73 | void (*function ## _fn)(void); \
|
---|
74 | RTR3DECL(rettype) function signature \
|
---|
75 | { return ( (rettype (*) signature) function ## _fn ) shortsig; }
|
---|
76 |
|
---|
77 | RT_RUNTIME_LOADER_INSERT_SYMBOLS
|
---|
78 |
|
---|
79 | # undef RT_PROXY_STUB
|
---|
80 |
|
---|
81 | /* Now comes a table of functions to be loaded from the library */
|
---|
82 | typedef struct
|
---|
83 | {
|
---|
84 | const char *name;
|
---|
85 | void (**fn)(void);
|
---|
86 | } SHARED_FUNC;
|
---|
87 |
|
---|
88 | # define RT_PROXY_STUB(s, dummy1, dummy2, dummy3 ) { #s , & s ## _fn } ,
|
---|
89 | static SHARED_FUNC SharedFuncs[] =
|
---|
90 | {
|
---|
91 | RT_RUNTIME_LOADER_INSERT_SYMBOLS
|
---|
92 | { NULL, NULL }
|
---|
93 | };
|
---|
94 | # undef RT_PROXY_STUB
|
---|
95 |
|
---|
96 | /* The function which does the actual work for RT_RUNTIME_LOADER_FUNCTION,
|
---|
97 | * serialised for thread safety. */
|
---|
98 | static int rtldrLoadOnce(void *, void *)
|
---|
99 | {
|
---|
100 | RTLDRMOD hLib;
|
---|
101 |
|
---|
102 | LogFlowFunc(("\n"));
|
---|
103 | int rc = RTLdrLoad(RT_RUNTIME_LOADER_LIB_NAME, &hLib);
|
---|
104 | for (unsigned i = 0; RT_SUCCESS(rc) && SharedFuncs[i].name != NULL; ++i)
|
---|
105 | rc = RTLdrGetSymbol(hLib, SharedFuncs[i].name, (void**)SharedFuncs[i].fn);
|
---|
106 | LogFlowFunc(("rc = %Rrc\n", rc));
|
---|
107 | return rc;
|
---|
108 | }
|
---|
109 |
|
---|
110 | /** Load the shared library RT_RUNTIME_LOADER_LIB_NAME and resolve the symbols
|
---|
111 | * pointed to by RT_RUNTIME_LOADER_INSERT_SYMBOLS. May safely be called from
|
---|
112 | * multiple threads and will not return until the library is loaded or has
|
---|
113 | * failed to load. */
|
---|
114 | RTR3DECL(int) RT_RUNTIME_LOADER_FUNCTION(void)
|
---|
115 | {
|
---|
116 | static RTONCE sOnce = RTONCE_INITIALIZER;
|
---|
117 |
|
---|
118 | LogFlowFunc(("\n"));
|
---|
119 | int rc = RTOnce (&sOnce, rtldrLoadOnce, NULL, NULL);
|
---|
120 | LogFlowFunc(("rc = %Rrc\n", rc));
|
---|
121 | return rc;
|
---|
122 | }
|
---|
123 |
|
---|
124 | #elif defined(RT_RUNTIME_LOADER_GENERATE_HEADER)
|
---|
125 | # ifdef RT_RUNTIME_LOADER_GENERATE_DECLS
|
---|
126 | /* Declarations of the functions that we need from
|
---|
127 | * RT_RUNTIME_LOADER_LIB_NAME */
|
---|
128 | # define RT_PROXY_STUB(function, rettype, signature, shortsig) \
|
---|
129 | RTR3DECL(rettype) ( function ) signature ;
|
---|
130 |
|
---|
131 | RT_RUNTIME_LOADER_INSERT_SYMBOLS
|
---|
132 |
|
---|
133 | # undef RT_PROXY_STUB
|
---|
134 | # endif /* RT_RUNTIME_LOADER_GENERATE_DECLS */
|
---|
135 |
|
---|
136 | /**
|
---|
137 | * Try to dynamically load the library. This function should be called before
|
---|
138 | * attempting to use any of the library functions. It is safe to call this
|
---|
139 | * function multiple times.
|
---|
140 | *
|
---|
141 | * @returns iprt status code
|
---|
142 | */
|
---|
143 | RTR3DECL(int) RT_RUNTIME_LOADER_FUNCTION(void);
|
---|
144 | #else
|
---|
145 | # error One of RT_RUNTIME_LOADER_GENERATE_HEADER or \
|
---|
146 | RT_RUNTIME_LOADER_GENERATE_BODY_STUBS must be defined when including this file
|
---|
147 | #endif
|
---|