- const char *libgl_filename = "libGL.so.1";
- static void *libgl_handle = NULL;
-
- if (! libgl_handle) {
- libgl_handle = dlwrap_real_dlopen (libgl_filename, RTLD_NOW | RTLD_DEEPBIND);
- if (! libgl_handle) {
- fprintf (stderr, "Error: Failed to dlopen %s\n",
- libgl_filename);
- exit (1);
+ void *ret;
+
+ /* We don't call dlopen here to find the library in which to
+ * perform a dlsym lookup. That's because the application may
+ * be loading libGL.so or libGLESv2.so for its OpenGL symbols.
+ *
+ * So we instead watch for one of those filenames to go by in
+ * our dlopen wrapper, which will then call
+ * glwrap_set_gl_handle to give us the handle to use here.
+ *
+ * If the application hasn't called dlopen on a "libGL"
+ * library, then presumably the application is linked directly
+ * to an OpenGL implementation. In this case, we can use
+ * RTLD_NEXT to find the symbol.
+ *
+ * But just in case, we also let the user override that by
+ * specifying the FIPS_LIBGL environment variable to the path
+ * of the real libGL.so library that fips should dlopen here.
+ */
+ if (gl_handle == NULL) {
+ const char *path;
+
+ path = getenv ("FIPS_LIBGL");
+ if (path) {
+ gl_handle = dlopen (path, RTLD_LAZY);
+
+ if (gl_handle == NULL) {
+ fprintf (stderr, "Failed to dlopen FIPS_LIBGL: "
+ "%s\n", path);
+ exit (1);
+ }
+ } else {
+ gl_handle = RTLD_NEXT;