+/* Many (most?) OpenGL programs dlopen libGL.so.1 rather than linking
+ * against it directly, which means they would not be seeing our
+ * wrapped GL symbols via LD_PRELOAD. So we catch the dlopen in a
+ * wrapper here and redirect it to our library.
+ */
+void *
+dlopen (const char *filename, int flag)
+{
+ void *ret;
+ unsigned index;
+
+ /* Before deciding whether to redirect this dlopen to our own
+ * library, we call the real dlopen. This assures that any
+ * expected side-effects from loading the intended library are
+ * resolved. Below, we may still return a handle pointing to
+ * our own library, and not what is opened here. */
+ ret = dlwrap_real_dlopen (filename, flag);
+
+ /* If filename is not a wrapped library, just return real dlopen */
+ if (! find_wrapped_library_index (filename, &index))
+ return ret;
+
+ /* When the application dlopens any wrapped library starting
+ * with 'libGL', (whether libGL.so.1 or libGLESv2.so.2), let's
+ * continue to use that library handle for future lookups of
+ * OpenGL functions. */
+ if (STRNCMP_LITERAL (filename, "libGL") == 0)
+ glwrap_set_gl_handle (ret);
+
+ assert (index < ARRAY_SIZE(orig_handles));
+ orig_handles[index] = ret;
+
+ if (libfips_handle == NULL)
+ libfips_handle = dlwrap_dlopen_libfips ();
+
+ /* Otherwise, we return our own handle so that we can intercept
+ * future calls to dlsym. We encode the index in the return value
+ * so that we can later map back to the originally requested
+ * dlopen-handle if necessary. */
+ return libfips_handle + index;