Back to index

glibc  2.9
reldep5.c
Go to the documentation of this file.
00001 #include <dlfcn.h>
00002 #include <mcheck.h>
00003 #include <stdio.h>
00004 #include <stdlib.h>
00005 
00006 int
00007 main (void)
00008 {
00009   void *h1;
00010   void *h2;
00011   int (*fp) (void);
00012 
00013   mtrace ();
00014 
00015   /* Open the two objects.  */
00016   h1 = dlopen ("reldepmod5.so", RTLD_LAZY);
00017   if (h1 == NULL)
00018     {
00019       printf ("cannot open reldepmod5.so: %s\n", dlerror ());
00020       exit (1);
00021     }
00022   h2 = dlopen ("reldepmod6.so", RTLD_LAZY);
00023   if (h2 == NULL)
00024     {
00025       printf ("cannot open reldepmod6.so: %s\n", dlerror ());
00026       exit (1);
00027     }
00028 
00029   /* Get the address of the variable in reldepmod1.so.  */
00030   fp = dlsym (h2, "bar");
00031   if (fp == NULL)
00032     {
00033       printf ("cannot get address of \"bar\": %s\n", dlerror ());
00034       exit (1);
00035     }
00036 
00037   /* Call the function.  */
00038   puts ("calling fp for the first time");
00039   if (fp () != 0)
00040     {
00041       puts ("function \"call_me\" returned wrong result");
00042       exit (1);
00043     }
00044 
00045   /* Now close the first object.  It must still be around since we have
00046      an implicit dependency.  */
00047   if (dlclose (h1) != 0)
00048     {
00049       printf ("closing h1 failed: %s\n", dlerror ());
00050       exit (1);
00051     }
00052 
00053   /* Calling the function must still work.  */
00054   puts ("calling fp for the second time");
00055   if (fp () != 0)
00056     {
00057       puts ("function \"call_me\" the second time returned wrong result");
00058       exit (1);
00059     }
00060   puts ("second call suceeded as well");
00061 
00062   /* Close the second object, we are done.  */
00063   if (dlclose (h2) != 0)
00064     {
00065       printf ("closing h2 failed: %s\n", dlerror ());
00066       exit (1);
00067     }
00068 
00069   return 0;
00070 }