1 /* Copyright © 2013, Intel Corporation
3 * Permission is hereby granted, free of charge, to any person obtaining a copy
4 * of this software and associated documentation files (the "Software"), to deal
5 * in the Software without restriction, including without limitation the rights
6 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 * copies of the Software, and to permit persons to whom the Software is
8 * furnished to do so, subject to the following conditions:
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 /* dladdr is a glibc extension */
36 typedef void * (* fips_dlopen_t)(const char * filename, int flag);
37 typedef void * (* fips_dlsym_t)(void *handle, const char *symbol);
39 static const char *wrapped_libs[] = {
44 static void *orig_handles[ARRAY_SIZE(wrapped_libs)];
46 /* Match 'filename' against an internal list of libraries for which
47 * libfips has wrappers.
49 * Returns true and sets *index_ret if a match is found.
50 * Returns false if no match is found. */
52 find_wrapped_library_index (const char *filename, unsigned *index_ret)
56 for (i = 0; i < ARRAY_SIZE(wrapped_libs); i++) {
57 if (strncmp(wrapped_libs[i], filename,
58 strlen (wrapped_libs[i])) == 0)
68 /* Many (most?) OpenGL programs dlopen libGL.so.1 rather than linking
69 * against it directly, which means they would not be seeing our
70 * wrapped GL symbols via LD_PRELOAD. So we catch the dlopen in a
71 * wrapper here and redirect it to our library.
74 dlopen (const char *filename, int flag)
80 /* Before deciding whether to redirect this dlopen to our own
81 * library, we call the real dlopen. This assures that any
82 * expected side-effects from loading the intended library are
83 * resolved. Below, we may still return a handle pointing to
84 * our own library, and not what is opened here. */
85 ret = dlwrap_real_dlopen (filename, flag);
87 /* If filename is not a wrapped library, just return real dlopen */
88 if (! find_wrapped_library_index (filename, &index))
91 /* When the application dlopens any wrapped library starting
92 * with 'libGL', (whether libGL.so.1 or libGLESv2.so.2), let's
93 * continue to use that library handle for future lookups of
94 * OpenGL functions. */
95 if (STRNCMP_LITERAL (filename, "libGL") == 0)
96 glwrap_set_gl_handle (ret);
98 assert (index < ARRAY_SIZE(orig_handles));
99 orig_handles[index] = ret;
101 /* Otherwise, we return our own handle so that we can intercept
102 * future calls to dlsym. We encode the index in the return value
103 * so that we can later map back to the originally requested
104 * dlopen-handle if necessary. */
106 return libfips_handle + index;
108 /* We find our own filename by looking up this very function
109 * (that is, this "dlopen"), with dladdr).*/
110 if (dladdr (dlopen, &info) == 0) {
111 fprintf (stderr, "Error: Failed to redirect dlopen of %s:\n",
116 libfips_handle = dlwrap_real_dlopen (info.dli_fname, flag);
118 return libfips_handle + index;
122 dlwrap_real_dlopen (const char *filename, int flag)
124 static fips_dlopen_t real_dlopen = NULL;
127 real_dlopen = (fips_dlopen_t) dlwrap_real_dlsym (RTLD_NEXT, "dlopen");
129 fprintf (stderr, "Error: Failed to find symbol for dlopen.\n");
134 return real_dlopen (filename, flag);
137 /* Since we redirect dlopens of libGL.so and libEGL.so to libfips we
138 * need to ensure that dlysm succeeds for all functions that might be
139 * defined in the real, underlying libGL library. But we're far too
140 * lazy to implement wrappers for function that would simply
141 * pass-through, so instead we also wrap dlysm and arrange for it to
142 * pass things through with RTLD_next if libfips does not have the
143 * function desired. */
145 dlsym (void *handle, const char *name)
150 /* All gl* and egl* symbols are preferentially looked up in libfips. */
151 if (STRNCMP_LITERAL (name, "gl") == 0 ||
152 STRNCMP_LITERAL (name, "egl") == 0)
154 symbol = dlwrap_real_dlsym (libfips_handle, name);
159 /* Failing that, anything specifically requested from the
160 * libfips library should be redirected to a real GL
163 /* We subtract the index back out of the handle (see the addition
164 * of the index in our wrapper for dlopen above) to then use the
165 * correct, original dlopen'ed handle for the library of
167 index = handle - libfips_handle;
168 if (index < ARRAY_SIZE(orig_handles)) {
169 return dlwrap_real_dlsym (orig_handles[index], name);
172 /* And anything else is some unrelated dlsym. Just pass it
173 * through. (This also covers the cases of lookups with
174 * special handles such as RTLD_DEFAULT or RTLD_NEXT.)
176 return dlwrap_real_dlsym (handle, name);
180 dlwrap_real_dlsym (void *handle, const char *name)
182 static fips_dlsym_t real_dlsym = NULL;
185 /* FIXME: This brute-force, hard-coded searching for a versioned
186 * symbol is really ugly. The only reason I'm doing this is because
187 * I need some way to lookup the "dlsym" function in libdl, but
188 * I can't use 'dlsym' to do it. So dlvsym works, but forces me
189 * to guess what the right version is.
191 * Potential fixes here:
193 * 1. Use libelf to actually inspect libdl.so and
194 * find the right version, (finding the right
195 * libdl.so can be made easier with
198 * 2. Use libelf to find the offset of the 'dlsym'
199 * symbol within libdl.so, (and then add this to
200 * the base address at which libdl.so is loaded
201 * as reported by dl_iterate_phdr).
203 * In the meantime, I'll just keep augmenting this
204 * hard-coded version list as people report bugs. */
205 const char *version[] = {
209 int num_versions = sizeof(version) / sizeof(version[0]);
211 for (i = 0; i < num_versions; i++) {
212 real_dlsym = (fips_dlsym_t) dlvsym (RTLD_NEXT, "dlsym",
217 if (i == num_versions) {
218 fprintf (stderr, "Internal error: Failed to find real dlsym\n");
220 "This may be a simple matter of fips not knowing about the version of GLIBC that\n"
221 "your program is using. Current known versions are:\n\n\t");
222 for (i = 0; i < num_versions; i++)
223 fprintf (stderr, "%s ", version[i]);
225 "\n\nYou can inspect your version by first finding libdl.so.2:\n"
227 "\tldd <your-program> | grep libdl.so\n"
229 "And then inspecting the version attached to the dlsym symbol:\n"
231 "\treadelf -s /path/to/libdl.so.2 | grep dlsym\n"
233 "And finally, adding the version to dlwrap.c:dlwrap_real_dlsym.\n");
239 return real_dlsym (handle, name);