Blame elf/tst-audit2.c

Packit 6c4009
/* Test case for early TLS initialization in dynamic linker.  */
Packit 6c4009
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <dlfcn.h>
Packit 6c4009
Packit 6c4009
#define MAGIC1 0xabcdef72
Packit 6c4009
#define MAGIC2 0xd8675309
Packit 6c4009
static __thread unsigned int magic[] = { MAGIC1, MAGIC2 };
Packit 6c4009
static __thread int calloc_called;
Packit 6c4009
Packit 6c4009
#undef calloc
Packit 6c4009
Packit 6c4009
/* This calloc definition will be called by the dynamic linker itself.
Packit 6c4009
   We test that interposed calloc is called by the dynamic loader, and
Packit 6c4009
   that TLS is fully initialized by then.  */
Packit 6c4009
Packit 6c4009
void *
Packit 6c4009
calloc (size_t n, size_t m)
Packit 6c4009
{
Packit 6c4009
  if (!calloc_called)
Packit 6c4009
    {
Packit 6c4009
      /* Allow our calloc to be called more than once.  */
Packit 6c4009
      calloc_called = 1;
Packit 6c4009
      if (magic[0] != MAGIC1 || magic[1] != MAGIC2)
Packit 6c4009
	{
Packit 6c4009
	  printf ("{%x, %x} != {%x, %x}\n",
Packit 6c4009
		  magic[0], magic[1], MAGIC1, MAGIC2);
Packit 6c4009
	  abort ();
Packit 6c4009
	}
Packit 6c4009
      magic[0] = MAGIC2;
Packit 6c4009
      magic[1] = MAGIC1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  n *= m;
Packit 6c4009
  void *ptr = malloc (n);
Packit 6c4009
  if (ptr != NULL)
Packit 6c4009
    memset (ptr, '\0', n);
Packit 6c4009
  return ptr;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit 6c4009
  /* Make sure that our calloc is called from the dynamic linker at least
Packit 6c4009
     once.  */
Packit 6c4009
  void *h = dlopen("$ORIGIN/tst-auditmod9b.so", RTLD_LAZY);
Packit 6c4009
  if (h != NULL)
Packit 6c4009
    dlclose (h);
Packit 6c4009
  if (magic[1] != MAGIC1 || magic[0] != MAGIC2)
Packit 6c4009
    {
Packit 6c4009
      printf ("{%x, %x} != {%x, %x}\n", magic[0], magic[1], MAGIC2, MAGIC1);
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#include <support/test-driver.c>