Back to index

glibc  2.9
reldep3.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   int *vp;
00013 
00014   mtrace ();
00015 
00016   /* Open the two objects.  */
00017   h1 = dlopen ("reldepmod1.so", RTLD_LAZY | RTLD_GLOBAL);
00018   if (h1 == NULL)
00019     {
00020       printf ("cannot open reldepmod1.so: %s\n", dlerror ());
00021       exit (1);
00022     }
00023   h2 = dlopen ("reldepmod4.so", RTLD_LAZY);
00024   if (h2 == NULL)
00025     {
00026       printf ("cannot open reldepmod4.so: %s\n", dlerror ());
00027       exit (1);
00028     }
00029 
00030   /* Get the address of the variable in reldepmod1.so.  */
00031   vp = dlsym (h1, "some_var");
00032   if (vp == NULL)
00033     {
00034       printf ("cannot get address of \"some_var\": %s\n", dlerror ());
00035       exit (1);
00036     }
00037 
00038   *vp = 42;
00039 
00040   /* Get the function `call_me' in the second object.  This has a
00041      dependency which is resolved by a definition in reldepmod1.so.  */
00042   fp = dlsym (h2, "call_me");
00043   if (fp == NULL)
00044     {
00045       printf ("cannot get address of \"call_me\": %s\n", dlerror ());
00046       exit (1);
00047     }
00048 
00049   /* Call the function.  */
00050   if (fp () != 0)
00051     {
00052       puts ("function \"call_me\" returned wrong result");
00053       exit (1);
00054     }
00055 
00056   /* Now close the first object.  If must still be around since we have
00057      a implicit dependency.  */
00058   if (dlclose (h1) != 0)
00059     {
00060       printf ("closing h1 failed: %s\n", dlerror ());
00061       exit (1);
00062     }
00063 
00064   /* Open the first object again.   */
00065   h1 = dlopen ("reldepmod1.so", RTLD_LAZY | RTLD_GLOBAL);
00066   if (h1 == NULL)
00067     {
00068       printf ("cannot open reldepmod1.so the second time: %s\n", dlerror ());
00069       exit (1);
00070     }
00071 
00072   /* Get the variable address again.  */
00073   vp = dlsym (h1, "some_var");
00074   if (vp == NULL)
00075     {
00076       printf ("cannot get address of \"some_var\" the second time: %s\n",
00077              dlerror ());
00078       exit (1);
00079     }
00080 
00081   /* The variable now must have its originial value.  */
00082   if (*vp != 0)
00083     {
00084       puts ("variable \"some_var\" not reset");
00085       exit (1);
00086     }
00087 
00088   /* Close the first object again, we are done.  */
00089   if (dlclose (h1) != 0)
00090     {
00091       printf ("closing h1 failed: %s\n", dlerror ());
00092       exit (1);
00093     }
00094   if (dlclose (h2) != 0)
00095     {
00096       printf ("closing h2 failed: %s\n", dlerror ());
00097       exit (1);
00098     }
00099 
00100   return 0;
00101 }