|
|
| Line 1: |
Line 1: |
| == [https://bugzilla.mozilla.org/show_bug.cgi?id=895156 Bug 895156 Notes] ==
| |
|
| |
| The only sure way to catch all calls to dlopen from an executable and any libraries it is linked with is to use the LD_PRELOAD feature. The LD_PRELOAD allows us to force the loading of a dynamically linked library (.so) before an executable is loaded. This allows us to override any symbols in dynamcially linked libraries that the executable is dependent on.
| |
|
| |
| In [https://bugzilla.mozilla.org/show_bug.cgi?id=895156 Bug 895156 Notes] we want to override dlopen and dlclose so that we can execute code to load the arm eabi exception handling data and any DWARF data (see [https://bugzilla.mozilla.org/show_bug.cgi?id=895156 Bug 895162]) for each library dynamically loaded at runtime. We will create a preload .so "shim" that exports our own dlopen and dlclose functions that execute our custom code and then fall through to the default system dlopen and dclose.
| |
|
| |
| The shim code is extremely simple (gecko_profiler_preload.c):
| |
|
| |
| #include <stdio.h>
| |
| #include <stdlib.h>
| |
| #include <dlfcn.h>
| |
|
| |
| void * dlopen( const char * file, int mode )
| |
| {
| |
| static void * (*sys_dlopen)( const char * file, int mode ) = NULL;
| |
|
| |
| /* look up the pointer to the system's dlopen function */
| |
| sys_dlopen = (void*(*)(const char * file, int mode)) dlsym( RTLD_NEXT, "dlopen" );
| |
|
| |
| /* do custom code here to see if the exception handling and/or DWARF data has been
| |
| * loaded already. if not, load it. */
| |
|
| |
| /* fall through to the system dlopen function */
| |
| return (*sys_dlopen)( file, mode );
| |
| }
| |
|
| |
| int close( int fd )
| |
| {
| |
| static int (*sys_dlclose)( void * handle ) = NULL;
| |
|
| |
| /* look up the pointer to the system's dlclose function */
| |
| sys_dlclose = (int(*)(void * handle)) dlsym( RTLD_NEXT, "dlclose" );
| |
|
| |
| /* do custom code here if needed. */
| |
|
| |
| /* fall through to the system dlclose function */
| |
| return (*sys_dlclose)( handle );
| |
| }
| |
|
| |
| Compiling this shim is easy to:
| |
|
| |
| $ gcc -Wall -fPIC -shared -o gecko_profiler_preload.so gecko_profiler_preload.c -ldl
| |
|
| |
| Then when we want to execute gecko using this preload shim:
| |
|
| |
| $ LD_PRELOAD=/path/to/gecko_profiler_preload.so ./gecko
| |
|
| |
| == [https://bugzilla.mozilla.org/show_bug.cgi?id=895162 Bug 895162 Notes] == | | == [https://bugzilla.mozilla.org/show_bug.cgi?id=895162 Bug 895162 Notes] == |
|
| |
|