Blame elf/reldep.c

Packit 6c4009
#include <dlfcn.h>
Packit 6c4009
#include <mcheck.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
main (void)
Packit 6c4009
{
Packit 6c4009
  void *h1;
Packit 6c4009
  void *h2;
Packit 6c4009
  int (*fp) (void);
Packit 6c4009
  int *vp;
Packit 6c4009
Packit 6c4009
  mtrace ();
Packit 6c4009
Packit 6c4009
  /* Open the two objects.  */
Packit 6c4009
  h1 = dlopen ("reldepmod1.so", RTLD_LAZY | RTLD_GLOBAL);
Packit 6c4009
  if (h1 == NULL)
Packit 6c4009
    {
Packit 6c4009
      printf ("cannot open reldepmod1.so: %s\n", dlerror ());
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
  h2 = dlopen ("reldepmod2.so", RTLD_LAZY);
Packit 6c4009
  if (h2 == NULL)
Packit 6c4009
    {
Packit 6c4009
      printf ("cannot open reldepmod2.so: %s\n", dlerror ());
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Get the address of the variable in reldepmod1.so.  */
Packit 6c4009
  vp = dlsym (h1, "some_var");
Packit 6c4009
  if (vp == NULL)
Packit 6c4009
    {
Packit 6c4009
      printf ("cannot get address of \"some_var\": %s\n", dlerror ());
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  *vp = 42;
Packit 6c4009
Packit 6c4009
  /* Get the function `call_me' in the second object.  This has a
Packit 6c4009
     dependency which is resolved by a definition in reldepmod1.so.  */
Packit 6c4009
  fp = dlsym (h2, "call_me");
Packit 6c4009
  if (fp == NULL)
Packit 6c4009
    {
Packit 6c4009
      printf ("cannot get address of \"call_me\": %s\n", dlerror ());
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Call the function.  */
Packit 6c4009
  if (fp () != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("function \"call_me\" returned wrong result");
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Now close the first object.  If must still be around since we have
Packit 6c4009
     an implicit dependency.  */
Packit 6c4009
  if (dlclose (h1) != 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("closing h1 failed: %s\n", dlerror ());
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Try calling the function again.  This will fail if the first object
Packit 6c4009
     got unloaded.  */
Packit 6c4009
  if (fp () != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("second call of function \"call_me\" returned wrong result");
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Now close the second file as well.  */
Packit 6c4009
  if (dlclose (h2) != 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("closing h2 failed: %s\n", dlerror ());
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Finally, open the first object again.   */
Packit 6c4009
  h1 = dlopen ("reldepmod1.so", RTLD_LAZY | RTLD_GLOBAL);
Packit 6c4009
  if (h1 == NULL)
Packit 6c4009
    {
Packit 6c4009
      printf ("cannot open reldepmod1.so the second time: %s\n", dlerror ());
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* And get the variable address again.  */
Packit 6c4009
  vp = dlsym (h1, "some_var");
Packit 6c4009
  if (vp == NULL)
Packit 6c4009
    {
Packit 6c4009
      printf ("cannot get address of \"some_var\" the second time: %s\n",
Packit 6c4009
	      dlerror ());
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* The variable now must have its originial value.  */
Packit 6c4009
  if (*vp != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("variable \"some_var\" not reset");
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Close the first object again, we are done.  */
Packit 6c4009
  if (dlclose (h1) != 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("closing h1 failed: %s\n", dlerror ());
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
}