Blame elf/multiload.c

Packit 6c4009
#include <dlfcn.h>
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <mcheck.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
main (void)
Packit 6c4009
{
Packit 6c4009
  void *a;
Packit 6c4009
  void *b;
Packit 6c4009
  void *c;
Packit 6c4009
  void *d;
Packit 6c4009
  char *wd;
Packit 6c4009
  char *base;
Packit 6c4009
  char *buf;
Packit 6c4009
Packit 6c4009
  mtrace ();
Packit 6c4009
Packit 6c4009
  /* Change to the binary directory.  */
Packit 6c4009
  if (chdir (OBJDIR) != 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("cannot change to `%s': %m", OBJDIR);
Packit 6c4009
      exit (EXIT_FAILURE);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  wd = getcwd (NULL, 0);
Packit 6c4009
  base = basename (wd);
Packit 6c4009
  buf = alloca (strlen (wd) + strlen (base) + 5 + sizeof "testobj1.so");
Packit 6c4009
Packit 6c4009
  printf ("loading `%s'\n", "./testobj1.so");
Packit 6c4009
  a = dlopen ("./testobj1.so", RTLD_NOW);
Packit 6c4009
  if (a == NULL)
Packit 6c4009
    {
Packit 6c4009
      printf ("cannot load `./testobj1.so': %s\n", dlerror ());
Packit 6c4009
      exit (EXIT_FAILURE);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  stpcpy (stpcpy (stpcpy (buf, "../"), base), "/testobj1.so");
Packit 6c4009
  printf ("loading `%s'\n", buf);
Packit 6c4009
  b = dlopen (buf, RTLD_NOW);
Packit 6c4009
  if (b == NULL)
Packit 6c4009
    {
Packit 6c4009
      printf ("cannot load `%s': %s\n", buf, dlerror ());
Packit 6c4009
      exit (EXIT_FAILURE);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  stpcpy (stpcpy (buf, wd), "/testobj1.so");
Packit 6c4009
  printf ("loading `%s'\n", buf);
Packit 6c4009
  c = dlopen (buf, RTLD_NOW);
Packit 6c4009
  if (c == NULL)
Packit 6c4009
    {
Packit 6c4009
      printf ("cannot load `%s': %s\n", buf, dlerror ());
Packit 6c4009
      exit (EXIT_FAILURE);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  stpcpy (stpcpy (stpcpy (stpcpy (buf, wd), "/../"), base), "/testobj1.so");
Packit 6c4009
  printf ("loading `%s'\n", buf);
Packit 6c4009
  d = dlopen (buf, RTLD_NOW);
Packit 6c4009
  if (d == NULL)
Packit 6c4009
    {
Packit 6c4009
      printf ("cannot load `%s': %s\n", buf, dlerror ());
Packit 6c4009
      exit (EXIT_FAILURE);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (a != b || b != c || c != d)
Packit 6c4009
    {
Packit 6c4009
      puts ("shared object loaded more than once");
Packit 6c4009
      exit (EXIT_FAILURE);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (dlclose (a) != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("closing `a' failed");
Packit 6c4009
      exit (EXIT_FAILURE);
Packit 6c4009
    }
Packit 6c4009
  if (dlclose (b) != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("closing `a' failed");
Packit 6c4009
      exit (EXIT_FAILURE);
Packit 6c4009
    }
Packit 6c4009
  if (dlclose (c) != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("closing `a' failed");
Packit 6c4009
      exit (EXIT_FAILURE);
Packit 6c4009
    }
Packit 6c4009
  if (dlclose (d) != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("closing `a' failed");
Packit 6c4009
      exit (EXIT_FAILURE);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  free (wd);
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
extern int foo (int a);
Packit 6c4009
int
Packit 6c4009
foo (int a)
Packit 6c4009
{
Packit 6c4009
  return a;
Packit 6c4009
}