Changeset 28363 in vbox
- Timestamp:
- Apr 15, 2010 2:08:05 PM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/include/iprt/runtime-loader.h
r28361 r28363 1 1 /** @file 2 * IPRT - runtime loader generation2 * IPRT - Runtime Loader Generation. 3 3 */ 4 4 5 5 /* 6 * Copyright (C) 2008 Sun Microsystems, Inc.6 * Copyright (C) 2008-2010 Sun Microsystems, Inc. 7 7 * 8 8 * This file is part of VirtualBox Open Source Edition (OSE), as … … 28 28 */ 29 29 30 /* How to use this loader generator 30 #include <iprt/types.h> 31 #ifdef RT_RUNTIME_LOADER_GENERATE_BODY_STUBS 32 # include <iprt/ldr.h> 33 # include <iprt/log.h> 34 # include <iprt/once.h> 35 #endif 36 37 /** @defgroup grp_rt_runtime_loader Runtime Loader Generation 38 * 39 * How to use this loader generator 31 40 * 32 41 * This loader generator can be used to generate stub code for loading a shared … … 45 54 * functions to be loaded, defined in the 46 55 * following pattern: 47 * 56 * @code 48 57 * #define RT_RUNTIME_LOADER_INSERT_SYMBOLS \ 49 58 * RT_PROXY_STUB(func_name, ret_type, (long_param_list), (short_param_list)) \ 50 59 * RT_PROXY_STUB(func_name2, ret_type2, (long_param_list2), (short_param_list2)) \ 51 60 * ... 61 * @endcode 52 62 * 53 63 * where long_param_list is a paramter list for declaring the function of the … … 60 70 * RT_RUNTIME_LOADER_GENERATE_DECLS (without a value) before including this 61 71 * file. 72 * 73 * @{ 62 74 */ 63 75 /** @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> 76 * probably be preferable. 77 * 78 * bird> An alternative is to generate assembly jump wrappers, this only 79 * requires the symbol names and prefix. I've done this ages ago when we forked 80 * the EMX/GCC toolchain on OS/2... It's a wee bit more annoying in x86 PIC/PIE 81 * mode, but nothing that cannot be dealt with. 82 */ 83 /** @todo r=bird: The use of RTR3DECL here is an unresolved issue. */ 84 /** @todo r=bird: The lack of RT_C_DECLS_BEGIN/END is an unresolved issue. Here 85 * we'll get into trouble if we use the same symbol names as the 86 * original! */ 87 /** @todo r=bird: The prefix usage here is very confused: RT_RUNTIME_LOADER_XXX, 88 * RT_PROXY_STUB, etc. */ 69 89 70 90 #ifdef RT_RUNTIME_LOADER_GENERATE_BODY_STUBS 71 /** The following are the symbols which we need from the library. */ 91 92 /* The following are the symbols which we need from the library. */ 72 93 # 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; }94 void (*function ## _fn)(void); \ 95 RTR3DECL(rettype) function signature \ 96 { return ( (rettype (*) signature) function ## _fn ) shortsig; } 76 97 77 98 RT_RUNTIME_LOADER_INSERT_SYMBOLS … … 79 100 # undef RT_PROXY_STUB 80 101 81 /* Now comes a table of functions to be loaded from the library */102 /* Now comes a table of functions to be loaded from the library. */ 82 103 typedef struct 83 104 { 84 const char * name;85 void (** fn)(void);86 } SHARED_FUNC;105 const char *pszName; 106 void (**ppfn)(void); 107 } RTLDRSHAREDFUNC; 87 108 88 109 # define RT_PROXY_STUB(s, dummy1, dummy2, dummy3 ) { #s , & s ## _fn } , 89 static SHARED_FUNCSharedFuncs[] =110 static RTLDRSHAREDFUNC g_aSharedFuncs[] = 90 111 { 91 RT_RUNTIME_LOADER_INSERT_SYMBOLS112 RT_RUNTIME_LOADER_INSERT_SYMBOLS 92 113 { NULL, NULL } 93 114 }; 94 115 # undef RT_PROXY_STUB 95 116 96 /* The function which does the actual work for RT_RUNTIME_LOADER_FUNCTION, 97 * serialised for thread safety. */ 98 static int rtldrLoadOnce(void *, void *) 117 /** 118 * The function which does the actual work for RT_RUNTIME_LOADER_FUNCTION, 119 * serialised for thread safety. 120 */ 121 static DECLCALLBACK(int) rtldrLoadOnce(void *, void *) 99 122 { 100 RTLDRMOD hLib; 123 RTLDRMOD hLib; 124 int rc; 101 125 102 126 LogFlowFunc(("\n")); 103 intrc = 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);127 rc = RTLdrLoad(RT_RUNTIME_LOADER_LIB_NAME, &hLib); 128 for (unsigned i = 0; RT_SUCCESS(rc) && g_aSharedFuncs[i].pszName != NULL; ++i) 129 rc = RTLdrGetSymbol(hLib, g_aSharedFuncs[i].pszName, (void **)g_aSharedFuncs[i].ppfn); 106 130 LogFlowFunc(("rc = %Rrc\n", rc)); 131 107 132 return rc; 108 133 } 109 134 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. */ 135 /** 136 * Load the shared library RT_RUNTIME_LOADER_LIB_NAME and resolve the symbols 137 * pointed to by RT_RUNTIME_LOADER_INSERT_SYMBOLS. 138 * 139 * May safely be called from multiple threads and will not return until the 140 * library is loaded or has failed to load. 141 * 142 * @returns IPRT status code. 143 */ 114 144 RTR3DECL(int) RT_RUNTIME_LOADER_FUNCTION(void) 115 145 { 116 static RTONCE sOnce = RTONCE_INITIALIZER; 146 static RTONCE s_Once = RTONCE_INITIALIZER; 147 int rc; 117 148 118 149 LogFlowFunc(("\n")); 119 int rc = RTOnce (&sOnce, rtldrLoadOnce, NULL, NULL);150 rc = RTOnce(&s_Once, rtldrLoadOnce, NULL, NULL); 120 151 LogFlowFunc(("rc = %Rrc\n", rc)); 152 121 153 return rc; 122 154 } … … 142 174 */ 143 175 RTR3DECL(int) RT_RUNTIME_LOADER_FUNCTION(void); 176 144 177 #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 178 # error "One of RT_RUNTIME_LOADER_GENERATE_HEADER or RT_RUNTIME_LOADER_GENERATE_BODY_STUBS must be defined when including this file" 147 179 #endif 180 181 /** @} */ 182
Note:
See TracChangeset
for help on using the changeset viewer.