hjl / source-git / glibc

Forked from source-git/glibc 3 years ago
Clone

Blame elf/unload2.c

Packit 6c4009
#include <dlfcn.h>
Packit 6c4009
#include <elf.h>
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <error.h>
Packit 6c4009
#include <link.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
Packit 6c4009
#define MAPS ((struct link_map *) _r_debug.r_map)
Packit 6c4009
Packit 6c4009
#define OUT \
Packit 6c4009
  for (map = MAPS; map != NULL; map = map->l_next)			      \
Packit 6c4009
    if (map->l_type == lt_loaded)					      \
Packit 6c4009
      printf ("name = \"%s\", direct_opencount = %d\n",			      \
Packit 6c4009
	      map->l_name, (int) map->l_direct_opencount);		      \
Packit 6c4009
  fflush (stdout)
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
main (void)
Packit 6c4009
{
Packit 6c4009
  void *h[3];
Packit 6c4009
  struct link_map *map;
Packit 6c4009
  void (*fp) (void);
Packit 6c4009
Packit 6c4009
  h[0] = dlopen ("unload2mod.so", RTLD_LAZY);
Packit 6c4009
  h[1] = dlopen ("unload2mod.so", RTLD_LAZY);
Packit 6c4009
  if (h[0] == NULL || h[1] == NULL)
Packit 6c4009
    error (EXIT_FAILURE, errno, "cannot load \"unload2mod.so\"");
Packit 6c4009
  h[2] = dlopen ("unload2dep.so", RTLD_LAZY);
Packit 6c4009
  if (h[2] == NULL)
Packit 6c4009
    error (EXIT_FAILURE, errno, "cannot load \"unload2dep.so\"");
Packit 6c4009
Packit 6c4009
  puts ("\nAfter loading everything:");
Packit 6c4009
  OUT;
Packit 6c4009
Packit 6c4009
  dlclose (h[0]);
Packit 6c4009
Packit 6c4009
  puts ("\nAfter unloading \"unload2mod.so\" once:");
Packit 6c4009
  OUT;
Packit 6c4009
Packit 6c4009
  dlclose (h[1]);
Packit 6c4009
Packit 6c4009
  puts ("\nAfter unloading \"unload2mod.so\" twice:");
Packit 6c4009
  OUT;
Packit 6c4009
Packit 6c4009
  fp = dlsym (h[2], "foo");
Packit 6c4009
  puts ("\nnow calling `foo'");
Packit 6c4009
  fflush (stdout);
Packit 6c4009
  fp ();
Packit 6c4009
  puts ("managed to call `foo'");
Packit 6c4009
  fflush (stdout);
Packit 6c4009
Packit 6c4009
  dlclose (h[2]);
Packit 6c4009
Packit 6c4009
  puts ("\nAfter unloading \"unload2dep.so\":");
Packit 6c4009
  OUT;
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
}