X-Git-Url: https://git.cworth.org/git?a=blobdiff_plain;f=dispatch%2Fglproc_gl.cpp;h=2516dab81c441e685a5797096a53c032056567b7;hb=5220b4974a1b93df3f916cdac74b65fae6a9177b;hp=7f604d20ddb43782ae38e5f4f8e40d497b90eb58;hpb=632a78d5c90941c896fa3c7156131f27c5a58b24;p=apitrace diff --git a/dispatch/glproc_gl.cpp b/dispatch/glproc_gl.cpp index 7f604d2..2516dab 100644 --- a/dispatch/glproc_gl.cpp +++ b/dispatch/glproc_gl.cpp @@ -29,7 +29,7 @@ #if !defined(_WIN32) #include // for symlink -#include +#include "dlopen.hpp" #endif @@ -37,7 +37,7 @@ * Handle to the true OpenGL library. */ #if defined(_WIN32) -HINSTANCE _libGlHandle = NULL; +HMODULE _libGlHandle = NULL; #else void *_libGlHandle = NULL; #endif @@ -145,24 +145,70 @@ _getPrivateProcAddress(const char *procName) #else +static inline void +logSymbol(const char *name, void *ptr) { + if (0) { + if (ptr) { + Dl_info info; + if (ptr && dladdr(ptr, &info)) { + os::log("apitrace: %s -> \"%s\"\n", name, info.dli_fname); + } + } else { + os::log("apitrace: %s -> NULL\n", name); + } + } +} + + +#ifdef __GLIBC__ +extern "C" void * __libc_dlsym(void *, const char *); +#endif + + /* - * Invoke the true dlopen() function. + * Protect against dlsym interception. + * + * We implement the whole API, so we don't need to intercept dlsym -- dlopen is + * enough. However we need to protect against other dynamic libraries + * intercepting dlsym, to prevent infinite recursion, + * + * In particular the Steam Community Overlay advertises dlsym. See also + * http://lists.freedesktop.org/archives/apitrace/2013-March/000573.html */ -static void * -_dlopen(const char *filename, int flag) +static inline void * +_dlsym(void *handle, const char *symbol) { - typedef void * (*PFNDLOPEN)(const char *, int); - static PFNDLOPEN dlopen_ptr = NULL; + void *result; - if (!dlopen_ptr) { - dlopen_ptr = (PFNDLOPEN)dlsym(RTLD_NEXT, "dlopen"); - if (!dlopen_ptr) { - os::log("apitrace: error: dlsym(RTLD_NEXT, \"dlopen\") failed\n"); +#ifdef __GLIBC__ + /* + * We rely on glibc's internal __libc_dlsym. See also + * http://www.linuxforu.com/2011/08/lets-hook-a-library-function/ + * + * Use use it to obtain the true dlsym. We don't use __libc_dlsym directly + * because it does not support things such as RTLD_NEXT. + */ + typedef void * (*PFN_DLSYM)(void *, const char *); + static PFN_DLSYM dlsym_ptr = NULL; + if (!dlsym_ptr) { + void *libdl_handle = _dlopen("libdl.so.2", RTLD_LOCAL | RTLD_NOW); + if (libdl_handle) { + dlsym_ptr = (PFN_DLSYM)__libc_dlsym(libdl_handle, "dlsym"); + } + if (!dlsym_ptr) { + os::log("apitrace: error: failed to look up real dlsym\n"); return NULL; } + + logSymbol("dlsym", (void*)dlsym_ptr); } - return dlopen_ptr(filename, flag); + result = dlsym_ptr(handle, symbol); +#else + result = dlsym(handle, symbol); +#endif + + return result; } @@ -209,7 +255,11 @@ void * _libgl_sym(const char *symbol) } } - return dlsym(_libGlHandle, symbol); + result = _dlsym(_libGlHandle, symbol); + + logSymbol(symbol, result); + + return result; }