Blame elf/neededtest4.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
extern void c_function (void);
Packit 6c4009
extern char *dirname (const char *__filename);
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
main (int argc, char **argv)
Packit 6c4009
{
Packit 6c4009
  void *obj;
Packit 6c4009
  const char *loaded[] = { NULL, NULL, NULL};
Packit 6c4009
  int errors = 0;
Packit 6c4009
  void (*f) (void);
Packit 6c4009
  const char *dir = dirname (argv [0]);
Packit 6c4009
  char *oldfilename;
Packit 6c4009
  char *newfilename;
Packit 6c4009
Packit 6c4009
  c_function ();
Packit 6c4009
Packit 6c4009
  printf ("\nThis is what is in memory now:\n");
Packit 6c4009
  errors += check_loaded_objects (loaded);
Packit 6c4009
Packit 6c4009
  printf( "Loading shared object neededobj6.so\n");
Packit 6c4009
  obj = dlopen( "neededobj6.so", RTLD_LAZY);
Packit 6c4009
  if (obj == NULL)
Packit 6c4009
    {
Packit 6c4009
      printf ("%s\n", dlerror ());
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
  f = dlsym (obj, "a2_function");
Packit 6c4009
  if (f == NULL)
Packit 6c4009
    {
Packit 6c4009
      printf ("%s\n", dlerror ());
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
  f ();
Packit 6c4009
  loaded[0] = "neededobj5.so";
Packit 6c4009
  loaded[1] = "neededobj6.so";
Packit 6c4009
  errors += check_loaded_objects (loaded);
Packit 6c4009
Packit 6c4009
  printf ("Closing neededobj6.so\n");
Packit 6c4009
  dlclose (obj);
Packit 6c4009
  loaded[0] = NULL;
Packit 6c4009
  errors += check_loaded_objects (loaded);
Packit 6c4009
Packit 6c4009
  printf ("Rename neededobj5.so\n");
Packit 6c4009
  oldfilename = alloca (strlen (dir) + 1 + sizeof ("neededobj5.so"));
Packit 6c4009
  strcpy (oldfilename, dir);
Packit 6c4009
  strcat (oldfilename, "/");
Packit 6c4009
  strcat (oldfilename, "neededobj5.so");
Packit 6c4009
  newfilename = alloca (strlen (oldfilename) + sizeof (".renamed"));
Packit 6c4009
  strcpy (newfilename, oldfilename);
Packit 6c4009
  strcat (newfilename, ".renamed");
Packit 6c4009
  if (rename (oldfilename, newfilename))
Packit 6c4009
    {
Packit 6c4009
      perror ("rename");
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  printf( "Loading shared object neededobj6.so\n");
Packit 6c4009
  obj = dlopen( "neededobj6.so", RTLD_LAZY);
Packit 6c4009
  if (obj == NULL)
Packit 6c4009
    printf ("%s\n", dlerror ());
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      printf ("neededobj6.so should fail to load\n");
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  printf( "Loading shared object neededobj1.so\n");
Packit 6c4009
  obj = dlopen( "neededobj1.so", RTLD_LAZY);
Packit 6c4009
  if (obj == NULL)
Packit 6c4009
    {
Packit 6c4009
      printf ("%s\n", dlerror ());
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
  errors += check_loaded_objects (loaded);
Packit 6c4009
  f = dlsym (obj, "c_function");
Packit 6c4009
  if (f == NULL)
Packit 6c4009
    {
Packit 6c4009
      printf ("%s\n", dlerror ());
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
  f ();
Packit 6c4009
Packit 6c4009
  printf ("Restore neededobj5.so\n");
Packit 6c4009
  if (rename (newfilename, oldfilename))
Packit 6c4009
    {
Packit 6c4009
      perror ("rename");
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (errors != 0)
Packit 6c4009
    printf ("%d errors found\n", errors);
Packit 6c4009
  return errors;
Packit 6c4009
}