Back to index

glibc  2.9
reldep.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 ("reldepmod2.so", RTLD_LAZY);
00024   if (h2 == NULL)
00025     {
00026       printf ("cannot open reldepmod2.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   /* Try calling the function again.  This will fail if the first object
00065      got unloaded.  */
00066   if (fp () != 0)
00067     {
00068       puts ("second call of function \"call_me\" returned wrong result");
00069       exit (1);
00070     }
00071 
00072   /* Now close the second file as well.  */
00073   if (dlclose (h2) != 0)
00074     {
00075       printf ("closing h2 failed: %s\n", dlerror ());
00076       exit (1);
00077     }
00078 
00079   /* Finally, open the first object again.   */
00080   h1 = dlopen ("reldepmod1.so", RTLD_LAZY | RTLD_GLOBAL);
00081   if (h1 == NULL)
00082     {
00083       printf ("cannot open reldepmod1.so the second time: %s\n", dlerror ());
00084       exit (1);
00085     }
00086 
00087   /* And get the variable address again.  */
00088   vp = dlsym (h1, "some_var");
00089   if (vp == NULL)
00090     {
00091       printf ("cannot get address of \"some_var\" the second time: %s\n",
00092              dlerror ());
00093       exit (1);
00094     }
00095 
00096   /* The variable now must have its originial value.  */
00097   if (*vp != 0)
00098     {
00099       puts ("variable \"some_var\" not reset");
00100       exit (1);
00101     }
00102 
00103   /* Close the first object again, we are done.  */
00104   if (dlclose (h1) != 0)
00105     {
00106       printf ("closing h1 failed: %s\n", dlerror ());
00107       exit (1);
00108     }
00109 
00110   return 0;
00111 }