X-Git-Url: https://git.cworth.org/git?a=blobdiff_plain;f=glwrap.c;h=ebe7e54ca67386d1e51d648dde3ba5fcc8b39f1d;hb=2ddfc599cf1f15f2eec12ddb24754fe768b38e03;hp=a7d62c5c1ef2be57d650591302923197c0fbb05f;hpb=fac9c8c2d3bfc5e97f2fdcf90f8cbe3ac9b2c49e;p=fips diff --git a/glwrap.c b/glwrap.c index a7d62c5..ebe7e54 100644 --- a/glwrap.c +++ b/glwrap.c @@ -19,6 +19,8 @@ * THE SOFTWARE. */ +#include "dlwrap.h" + /* The prototypes for some OpenGL functions changed at one point from: * * const void* *indices @@ -45,34 +47,71 @@ #include "metrics.h" -#include "dlwrap.h" - static int inside_new_list = 0; +static void *gl_handle; + +void +glwrap_set_gl_handle (void *handle) +{ + if (gl_handle == NULL) + gl_handle = handle; +} + void * glwrap_lookup (char *name) { - 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; } } - return dlwrap_real_dlsym (libgl_handle, name); + ret = dlwrap_real_dlsym (gl_handle, name); + + if (ret == NULL) { + fprintf (stderr, "Error: glwrap_lookup failed to dlsym %s\n", + name); + exit (1); + } + + return ret; } /* Execute an OpenGL call and time it with a GPU metrics counter. */ #define TIMED_DEFER(function,...) do { \ if (! inside_new_list) { \ - unsigned counter; \ - counter = metrics_counter_new (); \ - metrics_counter_start (counter); \ + metrics_counter_start (); \ } \ GLWRAP_DEFER(function, __VA_ARGS__); \ if (! inside_new_list) { \ @@ -345,11 +384,7 @@ void glBegin (GLenum mode) { if (! inside_new_list) - { - unsigned counter; - counter = metrics_counter_new (); - metrics_counter_start (counter); - } + metrics_counter_start (); GLWRAP_DEFER (glBegin, mode); } @@ -359,9 +394,8 @@ glEnd (void) { GLWRAP_DEFER (glEnd); - if (! inside_new_list) { + if (! inside_new_list) metrics_counter_stop (); - } } /* And we need to track display lists to avoid inserting queries