hjl / source-git / glibc

Forked from source-git/glibc 3 years ago
Clone

Blame elf/circleload1.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
static int
Packit 6c4009
load_dso (const char **loading, int undef, int flag)
Packit 6c4009
{
Packit 6c4009
  void *obj;
Packit 6c4009
  const char *loaded[] = { NULL, NULL, NULL, NULL };
Packit 6c4009
  int errors = 0;
Packit 6c4009
  const char *errstring;
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 %s: %s\n", loading[0],
Packit 6c4009
	 flag == RTLD_LAZY ? "RTLD_LAZY" : "RTLD_NOW");
Packit 6c4009
  obj = dlopen (loading[0], flag);
Packit 6c4009
  if (obj == NULL)
Packit 6c4009
    {
Packit 6c4009
      if (flag == RTLD_LAZY)
Packit 6c4009
	{
Packit 6c4009
	  ++errors;
Packit 6c4009
	  printf ("ERRORS: dlopen shouldn't fail for RTLD_LAZY\n");
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      errstring = dlerror ();
Packit 6c4009
      if (strstr (errstring, "undefined symbol") == 0
Packit 6c4009
	  || strstr (errstring, "circlemod2_undefined") == 0)
Packit 6c4009
	{
Packit 6c4009
	  ++errors;
Packit 6c4009
	  printf ("ERRORS: dlopen: `%s': Invalid error string\n",
Packit 6c4009
		  errstring);
Packit 6c4009
	}
Packit 6c4009
      else
Packit 6c4009
	printf ("dlopen: %s\n", errstring);
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      if (undef && flag == RTLD_NOW)
Packit 6c4009
	{
Packit 6c4009
	  ++errors;
Packit 6c4009
	  printf ("ERRORS: dlopen shouldn't work for RTLD_NOW\n");
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      if (!undef)
Packit 6c4009
	{
Packit 6c4009
	  int (*func) (void);
Packit 6c4009
Packit 6c4009
	  func = dlsym (obj, "circlemod1");
Packit 6c4009
	  if (func == NULL)
Packit 6c4009
	    {
Packit 6c4009
	      ++errors;
Packit 6c4009
	      printf ("ERRORS: cannot get address of \"circlemod1\": %s\n",
Packit 6c4009
		      dlerror ());
Packit 6c4009
	    }
Packit 6c4009
	  else if (func () != 3)
Packit 6c4009
	    {
Packit 6c4009
	      ++errors;
Packit 6c4009
	      printf ("ERRORS: function \"circlemod1\" returned wrong result\n");
Packit 6c4009
	    }
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      loaded[0] = loading[0];
Packit 6c4009
      loaded[1] = loading[1];
Packit 6c4009
      loaded[2] = loading[2];
Packit 6c4009
    }
Packit 6c4009
  errors += check_loaded_objects (loaded);
Packit 6c4009
Packit 6c4009
  if (obj)
Packit 6c4009
    {
Packit 6c4009
      printf ("UnLoading shared object %s\n", loading[0]);
Packit 6c4009
      dlclose (obj);
Packit 6c4009
      loaded[0] = NULL;
Packit 6c4009
      loaded[1] = NULL;
Packit 6c4009
      loaded[2] = NULL;
Packit 6c4009
      errors += check_loaded_objects (loaded);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return errors;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
main (void)
Packit 6c4009
{
Packit 6c4009
  int errors = 0;
Packit 6c4009
  const char *loading[3];
Packit 6c4009
Packit 6c4009
  loading[0] = "circlemod1a.so";
Packit 6c4009
  loading[1] = "circlemod2a.so";
Packit 6c4009
  loading[2] = "circlemod3a.so";
Packit 6c4009
  errors += load_dso (loading, 0, RTLD_LAZY);
Packit 6c4009
  errors += load_dso (loading, 0, RTLD_NOW);
Packit 6c4009
Packit 6c4009
  loading[0] = "circlemod1.so";
Packit 6c4009
  loading[1] = "circlemod2.so";
Packit 6c4009
  loading[2] = "circlemod3.so";
Packit 6c4009
  errors += load_dso (loading, 1, RTLD_LAZY);
Packit 6c4009
  errors += load_dso (loading, 1, RTLD_NOW);
Packit 6c4009
Packit 6c4009
  if (errors != 0)
Packit 6c4009
    printf ("%d errors found\n", errors);
Packit 6c4009
Packit 6c4009
  return errors;
Packit 6c4009
}