Back to index

glibc  2.9
unload2.c
Go to the documentation of this file.
00001 #include <dlfcn.h>
00002 #include <elf.h>
00003 #include <errno.h>
00004 #include <error.h>
00005 #include <link.h>
00006 #include <stdio.h>
00007 #include <stdlib.h>
00008 
00009 #define MAPS ((struct link_map *) _r_debug.r_map)
00010 
00011 #define OUT \
00012   for (map = MAPS; map != NULL; map = map->l_next)                          \
00013     if (map->l_type == lt_loaded)                                    \
00014       printf ("name = \"%s\", direct_opencount = %d\n",                     \
00015              map->l_name, (int) map->l_direct_opencount);                   \
00016   fflush (stdout)
00017 
00018 int
00019 main (void)
00020 {
00021   void *h[3];
00022   struct link_map *map;
00023   void (*fp) (void);
00024 
00025   h[0] = dlopen ("unload2mod.so", RTLD_LAZY);
00026   h[1] = dlopen ("unload2mod.so", RTLD_LAZY);
00027   if (h[0] == NULL || h[1] == NULL)
00028     error (EXIT_FAILURE, errno, "cannot load \"unload2mod.so\"");
00029   h[2] = dlopen ("unload2dep.so", RTLD_LAZY);
00030   if (h[2] == NULL)
00031     error (EXIT_FAILURE, errno, "cannot load \"unload2dep.so\"");
00032 
00033   puts ("\nAfter loading everything:");
00034   OUT;
00035 
00036   dlclose (h[0]);
00037 
00038   puts ("\nAfter unloading \"unload2mod.so\" once:");
00039   OUT;
00040 
00041   dlclose (h[1]);
00042 
00043   puts ("\nAfter unloading \"unload2mod.so\" twice:");
00044   OUT;
00045 
00046   fp = dlsym (h[2], "foo");
00047   puts ("\nnow calling `foo'");
00048   fflush (stdout);
00049   fp ();
00050   puts ("managed to call `foo'");
00051   fflush (stdout);
00052 
00053   dlclose (h[2]);
00054 
00055   puts ("\nAfter unloading \"unload2dep.so\":");
00056   OUT;
00057 
00058   return 0;
00059 }