X-Git-Url: https://git.cworth.org/git?a=blobdiff_plain;f=dlwrap.c;h=036d981d909feb56e857726b7a0ff0bf165d9763;hb=dfb96c9b64def8674a38dda2bc2276d4e2cdd58e;hp=aff9e625eda298680cc890f88061ea1845d97e65;hpb=fd9a68595eecba23d48729bda3f470934a0048ed;p=fips diff --git a/dlwrap.c b/dlwrap.c index aff9e62..036d981 100644 --- a/dlwrap.c +++ b/dlwrap.c @@ -29,6 +29,8 @@ #include "dlwrap.h" +#include "glwrap.h" + void *libfips_handle; typedef void * (* fips_dlopen_t)(const char * filename, int flag); @@ -36,7 +38,8 @@ typedef void * (* fips_dlsym_t)(void *handle, const char *symbol); static const char *wrapped_libs[] = { "libGL.so", - "libEGL.so" + "libEGL.so", + "libGLESv2.so" }; static void *orig_handles[ARRAY_SIZE(wrapped_libs)]; @@ -63,6 +66,31 @@ find_wrapped_library_index (const char *filename, unsigned *index_ret) return false; } +/* Perform a dlopen on the libfips library itself. + * + * Many places in fips need to lookup symbols within the libfips + * library itself, (and not in any other library). This function + * provides a reliable way to get a handle for performing such + * lookups. + * + * The returned handle can be passed to dlwrap_real_dlsym for the + * lookups. */ +void * +dlwrap_dlopen_libfips (void) +{ + Dl_info info; + + /* We first find our own filename by looking up a function + * known to exist only in libfips. This function itself + * (dlwrap_dlopen_libfips) is a good one for that purpose. */ + if (dladdr (dlwrap_dlopen_libfips, &info) == 0) { + fprintf (stderr, "Internal error: Failed to lookup filename of libfips library with dladdr\n"); + exit (1); + } + + return dlwrap_real_dlopen (info.dli_fname, RTLD_NOW); +} + /* 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 @@ -71,7 +99,6 @@ find_wrapped_library_index (const char *filename, unsigned *index_ret) void * dlopen (const char *filename, int flag) { - Dl_info info; void *ret; unsigned index; @@ -86,26 +113,23 @@ dlopen (const char *filename, int flag) 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. */ - if (libfips_handle) - return libfips_handle + index; - - /* We find our own filename by looking up this very function - * (that is, this "dlopen"), with dladdr).*/ - if (dladdr (dlopen, &info) == 0) { - fprintf (stderr, "Error: Failed to redirect dlopen of %s:\n", - filename); - exit (1); - } - - libfips_handle = dlwrap_real_dlopen (info.dli_fname, flag); - return libfips_handle + index; }