dl.c
changeset 1 9ccc1574a367
parent 0 7d3c07f8acfb
equal deleted inserted replaced
0:7d3c07f8acfb 1:9ccc1574a367
     1 #include <stdio.h>
     1 #include <stdio.h>
     2 #include <dlfcn.h>
     2 #include <dlfcn.h>
     3 #include <stdlib.h>
     3 #include <stdlib.h>
     4 #include "dl.h"
     4 #include "dl.h"
     5 
     5 
     6 void *get_original_function(const char *soname, const char *name)
     6 #define DEFAULT_LIB "libc.so.6"
       
     7 
       
     8 void *lib_function(const char *name, const char *soname)
     7 {
     9 {
       
    10     void *rc;
     8     const char* error;
    11     const char* error;
     9     void *handle = dlopen(soname, RTLD_LAZY);
    12     void *handle;
    10     void *rc;
       
    11 
    13 
    12     if (!handle) {
    14 
       
    15 
       
    16     if (!(handle = dlopen(soname ? soname : DEFAULT_LIB, RTLD_LAZY))) {
    13 	fprintf(stderr, "dlopen on %s: %s\n", soname, dlerror());
    17 	fprintf(stderr, "dlopen on %s: %s\n", soname, dlerror());
    14 	exit(1);
    18 	exit(1);
    15     }
    19     }
    16 
    20 
    17     dlerror();
    21     dlerror();			    /* clear the error flag */
    18     rc = dlsym(handle, name);
    22     rc = dlsym(handle, name);
    19     if ((error = dlerror())) {
    23     if ((error = dlerror())) {
    20 	fprintf(stderr, "dlsym for %s: %s\n", name, error);
    24 	fprintf(stderr, "dlsym for %s: %s\n", name, error);
    21 	exit(1);
    25 	exit(1);
    22     }
    26     }