Back to index

glibc  2.9
reldep6.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 typedef int (*fn)(void);
00007 #define CHUNKS 1024
00008 #define REPEAT 64
00009 
00010 int
00011 main (void)
00012 {
00013   void *h1;
00014   void *h2;
00015   fn **foopp;
00016   fn bar, baz;
00017   int i, j;
00018   int n;
00019   void *allocs[REPEAT][CHUNKS];
00020 
00021   mtrace ();
00022 
00023   /* Open the two objects.  */
00024   h1 = dlopen ("reldep6mod3.so", RTLD_LAZY);
00025   if (h1 == NULL)
00026     {
00027       printf ("cannot open reldep6mod3.so: %s\n", dlerror ());
00028       exit (1);
00029     }
00030 
00031   foopp = dlsym (h1, "foopp");
00032   if (foopp == NULL)
00033     {
00034       printf ("cannot get address of \"foopp\": %s\n", dlerror ());
00035       exit (1);
00036     }
00037   n = (**foopp) ();
00038   if (n != 20)
00039     {
00040       printf ("(**foopp)() return %d, not return 20\n", n);
00041       exit (1);
00042     }
00043 
00044   h2 = dlopen ("reldep6mod4.so", RTLD_LAZY);
00045   if (h2 == NULL)
00046     {
00047       printf ("cannot open reldep6mod4.so: %s\n", dlerror ());
00048       exit (1);
00049     }
00050 
00051   baz = dlsym (h2, "baz");
00052   if (baz == NULL)
00053     {
00054       printf ("cannot get address of \"baz\": %s\n", dlerror ());
00055       exit (1);
00056     }
00057   if (baz () != 31)
00058     {
00059       printf ("baz() did not return 31\n");
00060       exit (1);
00061     }
00062 
00063   if (dlclose (h1) != 0)
00064     {
00065       printf ("closing h1 failed: %s\n", dlerror ());
00066       exit (1);
00067     }
00068 
00069   /* Clobber memory.  */
00070   for (i = 0; i < REPEAT; ++i)
00071     for (j = 0; j < CHUNKS; ++j)
00072       allocs[i][j] = calloc (1, j + 1);
00073 
00074   bar = dlsym (h2, "bar");
00075   if (bar == NULL)
00076     {
00077       printf ("cannot get address of \"bar\": %s\n", dlerror ());
00078       exit (1);
00079     }
00080   if (bar () != 40)
00081     {
00082       printf ("bar() did not return 40\n");
00083       exit (1);
00084     }
00085 
00086   baz = dlsym (h2, "baz");
00087   if (baz == NULL)
00088     {
00089       printf ("cannot get address of \"baz\": %s\n", dlerror ());
00090       exit (1);
00091     }
00092   if (baz () != 31)
00093     {
00094       printf ("baz() did not return 31\n");
00095       exit (1);
00096     }
00097 
00098   for (i = 0; i < REPEAT; ++i)
00099     for (j = 0; j < CHUNKS; ++j)
00100       free (allocs[i][j]);
00101 
00102   if (dlclose (h2) != 0)
00103     {
00104       printf ("closing h2 failed: %s\n", dlerror ());
00105       exit (1);
00106     }
00107 
00108   return 0;
00109 }