]> git.ipfire.org Git - thirdparty/glibc.git/blame - elf/tst-audit2.c
test-container: Fix "unused code" warnings on HURD
[thirdparty/glibc.git] / elf / tst-audit2.c
CommitLineData
cafdfdb6
RM
1/* Test case for early TLS initialization in dynamic linker. */
2
3#include <stdio.h>
4#include <stdlib.h>
5#include <string.h>
58007e9e 6#include <dlfcn.h>
cafdfdb6 7
3ce1f295
UD
8#define MAGIC1 0xabcdef72
9#define MAGIC2 0xd8675309
cafdfdb6 10static __thread unsigned int magic[] = { MAGIC1, MAGIC2 };
58007e9e 11static __thread int calloc_called;
cafdfdb6
RM
12
13#undef calloc
14
15/* This calloc definition will be called by the dynamic linker itself.
58007e9e
L
16 We test that interposed calloc is called by the dynamic loader, and
17 that TLS is fully initialized by then. */
cafdfdb6
RM
18
19void *
20calloc (size_t n, size_t m)
21{
58007e9e 22 if (!calloc_called)
cafdfdb6 23 {
58007e9e
L
24 /* Allow our calloc to be called more than once. */
25 calloc_called = 1;
26 if (magic[0] != MAGIC1 || magic[1] != MAGIC2)
27 {
28 printf ("{%x, %x} != {%x, %x}\n",
29 magic[0], magic[1], MAGIC1, MAGIC2);
30 abort ();
31 }
32 magic[0] = MAGIC2;
33 magic[1] = MAGIC1;
cafdfdb6 34 }
cafdfdb6
RM
35
36 n *= m;
37 void *ptr = malloc (n);
38 if (ptr != NULL)
39 memset (ptr, '\0', n);
40 return ptr;
41}
42
29955b5d
AS
43static int
44do_test (void)
cafdfdb6 45{
58007e9e
L
46 /* Make sure that our calloc is called from the dynamic linker at least
47 once. */
48 void *h = dlopen("$ORIGIN/tst-auditmod9b.so", RTLD_LAZY);
49 if (h != NULL)
50 dlclose (h);
cafdfdb6
RM
51 if (magic[1] != MAGIC1 || magic[0] != MAGIC2)
52 {
53 printf ("{%x, %x} != {%x, %x}\n", magic[0], magic[1], MAGIC2, MAGIC1);
54 return 1;
55 }
cafdfdb6
RM
56
57 return 0;
58}
29955b5d 59
36fe25fd 60#include <support/test-driver.c>