about summary refs log tree commit diff
path: root/elf/tst-tls1.c
blob: 4d0913890c1a982dde50843764319e4c5246b9ae (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/* glibc test for TLS in ld.so.  */
#include <stdio.h>

#include <tls.h>


/* XXX Until gcc gets told how to define and use thread-local
   variables we will have to resort to use asms.  */
//asm (".tls_common foo,4,4");
asm (".section \".tdata\", \"awT\", @progbits\n\t"
     ".align 4\n\t"
     ".globl foo\n"
     "foo:\t.long 0\n\t"
     ".globl bar\n"
     "bar:\t.long 0\n\t"
     ".previous");


int
main (void)
{
#ifdef USE_TLS
  int result = 0;
  int a, b;

  /* XXX Each architecture must have its own asm for now.  */
# ifdef __i386__
  /* Set the variable using the local exec model.  */
  puts ("set bar to 1 (LE)");
  asm ("movl %gs:0,%eax\n\t"
     "subl $bar@tpoff,%eax\n\t"
     "movl $1,(%eax)");

#if 0
  // XXX Doesn't work yet; no runtime relocations.
  fputs ("get sum of foo and bar (IE)", stdout);
  asm ("call 1f\n\t"
       ".subsection 1\n"
       "1:\tmovl (%%esp), %%ebx\n\t"
       "ret\n\t"
       ".previous\n\t"
       "addl $_GLOBAL_OFFSET_TABLE_, %%ebx\n\t"
       "movl %%gs:0,%%eax\n\t"
       "movl %%eax,%%edx\n\t"
       "addl foo@gottpoff(%%ebx),%%eax\n\t"
       "addl bar@gottpoff(%%ebx),%%eax\n\t"
       "movl (%%eax), %0\n\t"
       "addl (%%edx), %0"
       : "=a" (a), "=&b" (b));
  printf (" = %d\n", a);
  result |= a != 1;
#endif

  fputs ("get sum of foo and bar (GD)", stdout);
  asm ("call 1f\n\t"
       ".subsection 1\n"
       "1:\tmovl (%%esp), %%ebx\n\t"
       "ret\n\t"
       ".previous\n\t"
       "addl $_GLOBAL_OFFSET_TABLE_, %%ebx\n\t"
       "leal foo@tlsgd(%%ebx),%%eax\n\t"
       "call ___tls_get_addr@plt\n\t"
       "nop\n\t"
       "movl (%%eax), %%edx\n\t"
       "leal bar@tlsgd(%%ebx),%%eax\n\t"
       "call ___tls_get_addr@plt\n\t"
       "nop\n\t"
       "addl (%%eax), %%edx\n\t"
       : "=&a" (a), "=d" (b));
  printf (" = %d\n", b);
  result |= b != 1;

# else
#  error "No support for this architecture so far."
# endif

  return result;
#else
  return 0;
#endif
}