José Fonseca | 1cfd89b | 2011-12-01 21:09:57 +0000 | [diff] [blame] | 1 | /************************************************************************** |
| 2 | * |
| 3 | * Copyright 2011 Jose Fonseca |
| 4 | * All Rights Reserved. |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to deal |
| 8 | * in the Software without restriction, including without limitation the rights |
| 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | * copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in |
| 14 | * all copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | * THE SOFTWARE. |
| 23 | * |
| 24 | **************************************************************************/ |
| 25 | |
| 26 | |
| 27 | #include "glproc.hpp" |
| 28 | |
| 29 | |
| 30 | #if !defined(_WIN32) |
José Fonseca | 2dc8ebe | 2011-12-03 11:00:32 +0000 | [diff] [blame] | 31 | #include <unistd.h> // for symlink |
Alexander Monakov | d6aa74c | 2013-05-03 21:03:27 +0400 | [diff] [blame] | 32 | #include "dlopen.hpp" |
José Fonseca | 1cfd89b | 2011-12-01 21:09:57 +0000 | [diff] [blame] | 33 | #endif |
| 34 | |
| 35 | |
| 36 | /* |
| 37 | * Handle to the true OpenGL library. |
| 38 | */ |
| 39 | #if defined(_WIN32) |
José Fonseca | 3947e36 | 2012-04-20 21:04:43 +0100 | [diff] [blame] | 40 | HMODULE _libGlHandle = NULL; |
José Fonseca | 1cfd89b | 2011-12-01 21:09:57 +0000 | [diff] [blame] | 41 | #else |
José Fonseca | 632a78d | 2012-04-19 07:18:59 +0100 | [diff] [blame] | 42 | void *_libGlHandle = NULL; |
José Fonseca | 1cfd89b | 2011-12-01 21:09:57 +0000 | [diff] [blame] | 43 | #endif |
| 44 | |
| 45 | |
| 46 | |
| 47 | #if defined(_WIN32) |
| 48 | |
| 49 | void * |
José Fonseca | 632a78d | 2012-04-19 07:18:59 +0100 | [diff] [blame] | 50 | _getPublicProcAddress(const char *procName) |
José Fonseca | 1cfd89b | 2011-12-01 21:09:57 +0000 | [diff] [blame] | 51 | { |
José Fonseca | 632a78d | 2012-04-19 07:18:59 +0100 | [diff] [blame] | 52 | if (!_libGlHandle) { |
José Fonseca | 1cfd89b | 2011-12-01 21:09:57 +0000 | [diff] [blame] | 53 | char szDll[MAX_PATH] = {0}; |
| 54 | |
| 55 | if (!GetSystemDirectoryA(szDll, MAX_PATH)) { |
| 56 | return NULL; |
| 57 | } |
| 58 | |
| 59 | strcat(szDll, "\\\\opengl32.dll"); |
| 60 | |
José Fonseca | 632a78d | 2012-04-19 07:18:59 +0100 | [diff] [blame] | 61 | _libGlHandle = LoadLibraryA(szDll); |
| 62 | if (!_libGlHandle) { |
José Fonseca | 1cfd89b | 2011-12-01 21:09:57 +0000 | [diff] [blame] | 63 | os::log("apitrace: error: couldn't load %s\n", szDll); |
| 64 | return NULL; |
| 65 | } |
| 66 | } |
| 67 | |
José Fonseca | 632a78d | 2012-04-19 07:18:59 +0100 | [diff] [blame] | 68 | return (void *)GetProcAddress(_libGlHandle, procName); |
José Fonseca | 1cfd89b | 2011-12-01 21:09:57 +0000 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | |
| 72 | void * |
José Fonseca | 632a78d | 2012-04-19 07:18:59 +0100 | [diff] [blame] | 73 | _getPrivateProcAddress(const char *procName) { |
| 74 | return (void *)_wglGetProcAddress(procName); |
José Fonseca | 1cfd89b | 2011-12-01 21:09:57 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | |
| 78 | #elif defined(__APPLE__) |
| 79 | |
| 80 | |
| 81 | /* |
| 82 | * Path to the true OpenGL framework |
| 83 | */ |
| 84 | static const char *libgl_filename = "/System/Library/Frameworks/OpenGL.framework/OpenGL"; |
| 85 | |
| 86 | |
| 87 | /* |
| 88 | * Lookup a libGL symbol |
| 89 | */ |
José Fonseca | 632a78d | 2012-04-19 07:18:59 +0100 | [diff] [blame] | 90 | void * _libgl_sym(const char *symbol) |
José Fonseca | 1cfd89b | 2011-12-01 21:09:57 +0000 | [diff] [blame] | 91 | { |
| 92 | void *result; |
| 93 | |
José Fonseca | 632a78d | 2012-04-19 07:18:59 +0100 | [diff] [blame] | 94 | if (!_libGlHandle) { |
José Fonseca | 1cfd89b | 2011-12-01 21:09:57 +0000 | [diff] [blame] | 95 | /* |
| 96 | * Unfortunately we can't just dlopen the true dynamic library because |
| 97 | * DYLD_LIBRARY_PATH/DYLD_FRAMEWORK_PATH take precedence, even for |
| 98 | * absolute paths. So we create a temporary symlink, and dlopen that |
| 99 | * instead. |
| 100 | */ |
| 101 | |
| 102 | char temp_filename[] = "/tmp/tmp.XXXXXX"; |
| 103 | |
| 104 | if (mktemp(temp_filename) != NULL) { |
| 105 | if (symlink(libgl_filename, temp_filename) == 0) { |
José Fonseca | 632a78d | 2012-04-19 07:18:59 +0100 | [diff] [blame] | 106 | _libGlHandle = dlopen(temp_filename, RTLD_LOCAL | RTLD_NOW | RTLD_FIRST); |
José Fonseca | 1cfd89b | 2011-12-01 21:09:57 +0000 | [diff] [blame] | 107 | remove(temp_filename); |
| 108 | } |
| 109 | } |
| 110 | |
José Fonseca | 632a78d | 2012-04-19 07:18:59 +0100 | [diff] [blame] | 111 | if (!_libGlHandle) { |
José Fonseca | 1cfd89b | 2011-12-01 21:09:57 +0000 | [diff] [blame] | 112 | os::log("apitrace: error: couldn't load %s\n", libgl_filename); |
| 113 | os::abort(); |
| 114 | return NULL; |
| 115 | } |
| 116 | } |
| 117 | |
José Fonseca | 632a78d | 2012-04-19 07:18:59 +0100 | [diff] [blame] | 118 | result = dlsym(_libGlHandle, symbol); |
José Fonseca | 1cfd89b | 2011-12-01 21:09:57 +0000 | [diff] [blame] | 119 | |
José Fonseca | 3987963 | 2012-04-14 22:26:09 +0100 | [diff] [blame] | 120 | #if 0 |
| 121 | if (result && result == dlsym(RTLD_SELF, symbol)) { |
José Fonseca | 1cfd89b | 2011-12-01 21:09:57 +0000 | [diff] [blame] | 122 | os::log("apitrace: error: symbol lookup recursion\n"); |
| 123 | os::abort(); |
| 124 | return NULL; |
| 125 | } |
José Fonseca | 3987963 | 2012-04-14 22:26:09 +0100 | [diff] [blame] | 126 | #endif |
José Fonseca | 1cfd89b | 2011-12-01 21:09:57 +0000 | [diff] [blame] | 127 | |
| 128 | return result; |
| 129 | } |
| 130 | |
| 131 | |
| 132 | void * |
José Fonseca | 632a78d | 2012-04-19 07:18:59 +0100 | [diff] [blame] | 133 | _getPublicProcAddress(const char *procName) |
José Fonseca | 1cfd89b | 2011-12-01 21:09:57 +0000 | [diff] [blame] | 134 | { |
José Fonseca | 632a78d | 2012-04-19 07:18:59 +0100 | [diff] [blame] | 135 | return _libgl_sym(procName); |
José Fonseca | 1cfd89b | 2011-12-01 21:09:57 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | void * |
José Fonseca | 632a78d | 2012-04-19 07:18:59 +0100 | [diff] [blame] | 139 | _getPrivateProcAddress(const char *procName) |
José Fonseca | 1cfd89b | 2011-12-01 21:09:57 +0000 | [diff] [blame] | 140 | { |
José Fonseca | 632a78d | 2012-04-19 07:18:59 +0100 | [diff] [blame] | 141 | return _libgl_sym(procName); |
José Fonseca | 1cfd89b | 2011-12-01 21:09:57 +0000 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | |
| 145 | #else |
| 146 | |
| 147 | |
José Fonseca | 7700f74 | 2013-06-15 11:31:53 +0100 | [diff] [blame] | 148 | static inline void |
| 149 | logSymbol(const char *name, void *ptr) { |
| 150 | if (0) { |
| 151 | if (ptr) { |
| 152 | Dl_info info; |
| 153 | if (ptr && dladdr(ptr, &info)) { |
| 154 | os::log("apitrace: %s -> \"%s\"\n", name, info.dli_fname); |
| 155 | } |
| 156 | } else { |
| 157 | os::log("apitrace: %s -> NULL\n", name); |
| 158 | } |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | |
| 163 | #ifdef __GLIBC__ |
| 164 | extern "C" void * __libc_dlsym(void *, const char *); |
| 165 | #endif |
| 166 | |
| 167 | |
| 168 | /* |
| 169 | * Protect against dlsym interception. |
| 170 | * |
| 171 | * We implement the whole API, so we don't need to intercept dlsym -- dlopen is |
| 172 | * enough. However we need to protect against other dynamic libraries |
| 173 | * intercepting dlsym, to prevent infinite recursion, |
| 174 | * |
| 175 | * In particular the Steam Community Overlay advertises dlsym. See also |
| 176 | * http://lists.freedesktop.org/archives/apitrace/2013-March/000573.html |
| 177 | */ |
| 178 | static inline void * |
| 179 | _dlsym(void *handle, const char *symbol) |
| 180 | { |
| 181 | void *result; |
| 182 | |
| 183 | #ifdef __GLIBC__ |
| 184 | /* |
| 185 | * We rely on glibc's internal __libc_dlsym. See also |
| 186 | * http://www.linuxforu.com/2011/08/lets-hook-a-library-function/ |
| 187 | * |
| 188 | * Use use it to obtain the true dlsym. We don't use __libc_dlsym directly |
| 189 | * because it does not support things such as RTLD_NEXT. |
| 190 | */ |
| 191 | typedef void * (*PFN_DLSYM)(void *, const char *); |
| 192 | static PFN_DLSYM dlsym_ptr = NULL; |
| 193 | if (!dlsym_ptr) { |
| 194 | void *libdl_handle = _dlopen("libdl.so.2", RTLD_LOCAL | RTLD_NOW); |
| 195 | if (libdl_handle) { |
| 196 | dlsym_ptr = (PFN_DLSYM)__libc_dlsym(libdl_handle, "dlsym"); |
| 197 | } |
| 198 | if (!dlsym_ptr) { |
| 199 | os::log("apitrace: error: failed to look up real dlsym\n"); |
| 200 | return NULL; |
| 201 | } |
| 202 | |
| 203 | logSymbol("dlsym", (void*)dlsym_ptr); |
| 204 | } |
| 205 | |
| 206 | result = dlsym_ptr(handle, symbol); |
| 207 | #else |
| 208 | result = dlsym(handle, symbol); |
| 209 | #endif |
| 210 | |
| 211 | return result; |
| 212 | } |
| 213 | |
| 214 | |
José Fonseca | 1cfd89b | 2011-12-01 21:09:57 +0000 | [diff] [blame] | 215 | /* |
José Fonseca | 1cfd89b | 2011-12-01 21:09:57 +0000 | [diff] [blame] | 216 | * Lookup a libGL symbol |
| 217 | */ |
José Fonseca | 632a78d | 2012-04-19 07:18:59 +0100 | [diff] [blame] | 218 | void * _libgl_sym(const char *symbol) |
José Fonseca | 1cfd89b | 2011-12-01 21:09:57 +0000 | [diff] [blame] | 219 | { |
| 220 | void *result; |
| 221 | |
José Fonseca | 632a78d | 2012-04-19 07:18:59 +0100 | [diff] [blame] | 222 | if (!_libGlHandle) { |
José Fonseca | 1cfd89b | 2011-12-01 21:09:57 +0000 | [diff] [blame] | 223 | /* |
| 224 | * The app doesn't directly link against libGL.so, nor does it directly |
| 225 | * dlopen it. So we have to load it ourselves. |
| 226 | */ |
| 227 | |
| 228 | const char * libgl_filename = getenv("TRACE_LIBGL"); |
| 229 | |
| 230 | if (!libgl_filename) { |
| 231 | /* |
| 232 | * Try to use whatever libGL.so the library is linked against. |
| 233 | */ |
| 234 | |
| 235 | result = dlsym(RTLD_NEXT, symbol); |
| 236 | if (result) { |
José Fonseca | 632a78d | 2012-04-19 07:18:59 +0100 | [diff] [blame] | 237 | _libGlHandle = RTLD_NEXT; |
José Fonseca | 1cfd89b | 2011-12-01 21:09:57 +0000 | [diff] [blame] | 238 | return result; |
| 239 | } |
| 240 | |
| 241 | libgl_filename = "libGL.so.1"; |
| 242 | } |
| 243 | |
| 244 | /* |
| 245 | * It would have been preferable to use RTLD_LOCAL to ensure that the |
| 246 | * application can never access libGL.so symbols directly, but this |
| 247 | * won't work, given libGL.so often loads a driver specific SO and |
| 248 | * exposes symbols to it. |
| 249 | */ |
| 250 | |
José Fonseca | 632a78d | 2012-04-19 07:18:59 +0100 | [diff] [blame] | 251 | _libGlHandle = _dlopen(libgl_filename, RTLD_GLOBAL | RTLD_LAZY); |
| 252 | if (!_libGlHandle) { |
José Fonseca | 1cfd89b | 2011-12-01 21:09:57 +0000 | [diff] [blame] | 253 | os::log("apitrace: error: couldn't find libGL.so\n"); |
| 254 | return NULL; |
| 255 | } |
| 256 | } |
| 257 | |
José Fonseca | 7700f74 | 2013-06-15 11:31:53 +0100 | [diff] [blame] | 258 | result = _dlsym(_libGlHandle, symbol); |
| 259 | |
| 260 | logSymbol(symbol, result); |
| 261 | |
| 262 | return result; |
José Fonseca | 1cfd89b | 2011-12-01 21:09:57 +0000 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | |
| 266 | void * |
José Fonseca | 632a78d | 2012-04-19 07:18:59 +0100 | [diff] [blame] | 267 | _getPublicProcAddress(const char *procName) |
José Fonseca | 1cfd89b | 2011-12-01 21:09:57 +0000 | [diff] [blame] | 268 | { |
José Fonseca | 632a78d | 2012-04-19 07:18:59 +0100 | [diff] [blame] | 269 | return _libgl_sym(procName); |
José Fonseca | 1cfd89b | 2011-12-01 21:09:57 +0000 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | void * |
José Fonseca | 632a78d | 2012-04-19 07:18:59 +0100 | [diff] [blame] | 273 | _getPrivateProcAddress(const char *procName) |
José Fonseca | 1cfd89b | 2011-12-01 21:09:57 +0000 | [diff] [blame] | 274 | { |
José Fonseca | 632a78d | 2012-04-19 07:18:59 +0100 | [diff] [blame] | 275 | return (void *)_glXGetProcAddressARB((const GLubyte *)procName); |
José Fonseca | 1cfd89b | 2011-12-01 21:09:57 +0000 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | |
| 279 | #endif |
| 280 | |