1 #include <dlfcn.h> 2 #include <mcheck.h> 3 #include <stdio.h> 4 #include <stdlib.h> 5 6 int main(void)7main (void) 8 { 9 void *h1; 10 void *h2; 11 int (*fp) (void); 12 13 mtrace (); 14 15 /* Open the two objects. */ 16 h1 = dlopen ("reldepmod5.so", RTLD_LAZY); 17 if (h1 == NULL) 18 { 19 printf ("cannot open reldepmod5.so: %s\n", dlerror ()); 20 exit (1); 21 } 22 h2 = dlopen ("reldepmod6.so", RTLD_LAZY); 23 if (h2 == NULL) 24 { 25 printf ("cannot open reldepmod6.so: %s\n", dlerror ()); 26 exit (1); 27 } 28 29 /* Get the address of the variable in reldepmod1.so. */ 30 fp = dlsym (h2, "bar"); 31 if (fp == NULL) 32 { 33 printf ("cannot get address of \"bar\": %s\n", dlerror ()); 34 exit (1); 35 } 36 37 /* Call the function. */ 38 puts ("calling fp for the first time"); 39 if (fp () != 0) 40 { 41 puts ("function \"call_me\" returned wrong result"); 42 exit (1); 43 } 44 45 /* Now close the first object. It must still be around since we have 46 an implicit dependency. */ 47 if (dlclose (h1) != 0) 48 { 49 printf ("closing h1 failed: %s\n", dlerror ()); 50 exit (1); 51 } 52 53 /* Calling the function must still work. */ 54 puts ("calling fp for the second time"); 55 if (fp () != 0) 56 { 57 puts ("function \"call_me\" the second time returned wrong result"); 58 exit (1); 59 } 60 puts ("second call suceeded as well"); 61 62 /* Close the second object, we are done. */ 63 if (dlclose (h2) != 0) 64 { 65 printf ("closing h2 failed: %s\n", dlerror ()); 66 exit (1); 67 } 68 69 return 0; 70 } 71