+
+/* We rely on an internal symbol within glibc in order to be able to
+ * get a handle on the real dlsym function, (we can't call dlsym to
+ * find the address of dlsym itself of course). */
+void * __libc_dlsym(void *, const char *);
+
+static void *
+do_real_dlsym (void *handle, const char *symbol)
+{
+ static typeof(&dlsym) real_dlsym = NULL;
+
+ if (real_dlsym == NULL) {
+ void *libdl_handle = dlopen ("libdl.so.2", RTLD_LAZY);
+ real_dlsym = __libc_dlsym(libdl_handle, "dlsym");
+ }
+
+ return real_dlsym (handle, symbol);
+}
+
+void *
+dlsym (void *handle, const char *symbol)
+{
+ void *ret;
+
+ ret = do_real_dlsym (handle, symbol);
+
+ if (strcmp (symbol, "glXSwapBuffers") == 0) {
+ real_glXSwapBuffers = ret;
+ return &glXSwapBuffers;
+ }
+
+ if (strcmp (symbol, "glXGetProcAddressARB") == 0) {
+ real_glXGetProcAddressARB = ret;
+ return &glXGetProcAddressARB;
+ }
+
+ if (strcmp (symbol, "glXGetProcAddress") == 0) {
+ real_glXGetProcAddress = ret;
+ return &glXGetProcAddress;
+ }
+
+ return ret;
+}