Blame elf/neededtest2.c

Packit 6c4009
#include <dlfcn.h>
Packit 6c4009
#include <libintl.h>
Packit 6c4009
#include <link.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
Packit 6c4009
#define MAPS ((struct link_map *) _r_debug.r_map)
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
check_loaded_objects (const char **loaded)
Packit 6c4009
{
Packit 6c4009
  struct link_map *lm;
Packit 6c4009
  int n;
Packit 6c4009
  int *found = NULL;
Packit 6c4009
  int errors = 0;
Packit 6c4009
Packit 6c4009
  for (n = 0; loaded[n]; n++)
Packit 6c4009
    /* NOTHING */;
Packit 6c4009
Packit 6c4009
  if (n)
Packit 6c4009
    {
Packit 6c4009
      found = (int *) alloca (sizeof (int) * n);
Packit 6c4009
      memset (found, 0, sizeof (int) * n);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  printf("   Name\n");
Packit 6c4009
  printf(" --------------------------------------------------------\n");
Packit 6c4009
  for (lm = MAPS; lm; lm = lm->l_next)
Packit 6c4009
    {
Packit 6c4009
      if (lm->l_name && lm->l_name[0])
Packit 6c4009
	printf(" %s, count = %d\n", lm->l_name, (int) lm->l_direct_opencount);
Packit 6c4009
      if (lm->l_type == lt_loaded && lm->l_name)
Packit 6c4009
	{
Packit 6c4009
	  int match = 0;
Packit 6c4009
	  for (n = 0; loaded[n] != NULL; n++)
Packit 6c4009
	    {
Packit 6c4009
	      if (strcmp (basename (loaded[n]), basename (lm->l_name)) == 0)
Packit 6c4009
	        {
Packit 6c4009
		  found[n] = 1;
Packit 6c4009
		  match = 1;
Packit 6c4009
		  break;
Packit 6c4009
		}
Packit 6c4009
	    }
Packit 6c4009
Packit 6c4009
	  if (match == 0)
Packit 6c4009
	    {
Packit 6c4009
	      ++errors;
Packit 6c4009
	      printf ("ERRORS: %s is not unloaded\n", lm->l_name);
Packit 6c4009
	    }
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  for (n = 0; loaded[n] != NULL; n++)
Packit 6c4009
    {
Packit 6c4009
      if (found[n] == 0)
Packit 6c4009
        {
Packit 6c4009
	  ++errors;
Packit 6c4009
	  printf ("ERRORS: %s is not loaded\n", loaded[n]);
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return errors;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
main (void)
Packit 6c4009
{
Packit 6c4009
  void *obj2;
Packit 6c4009
  void *obj3[2];
Packit 6c4009
  const char *loaded[] = { NULL, NULL, NULL, NULL };
Packit 6c4009
  int errors = 0;
Packit 6c4009
Packit 6c4009
  printf ("\nThis is what is in memory now:\n");
Packit 6c4009
  errors += check_loaded_objects (loaded);
Packit 6c4009
  printf ("\nLoading shared object neededobj2.so\n");
Packit 6c4009
  obj2 = dlopen ("neededobj2.so", RTLD_LAZY);
Packit 6c4009
  if (obj2 == NULL)
Packit 6c4009
    {
Packit 6c4009
      printf ("%s\n", dlerror ());
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
  loaded[0] = "neededobj1.so";
Packit 6c4009
  loaded[1] = "neededobj2.so";
Packit 6c4009
  errors += check_loaded_objects (loaded);
Packit 6c4009
  printf ("\nLoading shared object neededobj3.so\n");
Packit 6c4009
  obj3[0] = dlopen( "neededobj3.so", RTLD_LAZY);
Packit 6c4009
  if (obj3[0] == NULL)
Packit 6c4009
    {
Packit 6c4009
      printf ("%s\n", dlerror ());
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
  loaded[2] = "neededobj3.so";
Packit 6c4009
  errors += check_loaded_objects (loaded);
Packit 6c4009
  printf ("\nNow loading shared object neededobj3.so again\n");
Packit 6c4009
  obj3[1] = dlopen ("neededobj3.so", RTLD_LAZY);
Packit 6c4009
  if (obj3[1] == NULL)
Packit 6c4009
    {
Packit 6c4009
      printf ("%s\n", dlerror ());
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
  errors += check_loaded_objects (loaded);
Packit 6c4009
  printf ("\nClosing neededobj3.so once\n");
Packit 6c4009
  dlclose (obj3[0]);
Packit 6c4009
  errors += check_loaded_objects (loaded);
Packit 6c4009
  printf ("\nClosing neededobj2.so\n");
Packit 6c4009
  dlclose (obj2);
Packit 6c4009
  errors += check_loaded_objects (loaded);
Packit 6c4009
  printf ("\nClosing neededobj3.so for the second time\n");
Packit 6c4009
  dlclose (obj3[1]);
Packit 6c4009
  loaded[0] = NULL;
Packit 6c4009
  loaded[1] = NULL;
Packit 6c4009
  loaded[2] = NULL;
Packit 6c4009
  errors += check_loaded_objects (loaded);
Packit 6c4009
  if (errors != 0)
Packit 6c4009
    printf ("%d errors found\n", errors);
Packit 6c4009
  return errors;
Packit 6c4009
}