b57e4ac1d3068a207c44f701d9c6b404c2002d54
[kopensolaris-gnu/glibc.git] / elf / tls-macros.h
1 /* Macros to support TLS testing in times of missing compiler support.  */
2
3 #define COMMON_INT_DEF(x) \
4   asm (".tls_common " #x ",4,4")
5 /* XXX Until we get compiler support we don't need declarations.  */
6 #define COMMON_INT_DECL(x)
7
8 /* XXX This definition will probably be machine specific, too.  */
9 #define VAR_INT_DEF(x) \
10   asm (".section .tdata\n\t"                                                  \
11        ".globl " #x "\n"                                                      \
12        #x ":\t.long 0\n\t"                                                    \
13        ".size " #x ",4\n\t"                                                   \
14        ".previous")
15 /* XXX Until we get compiler support we don't need declarations.  */
16 #define VAR_INT_DECL(x)
17
18
19   /* XXX Each architecture must have its own asm for now.  */
20 #ifdef __i386__
21 # define TLS_LE(x) \
22   ({ int *__l;                                                                \
23      asm ("movl %%gs:0,%0\n\t"                                                \
24           "subl $" #x "@tpoff,%0"                                             \
25           : "=r" (__l));                                                      \
26      __l; })
27
28 # ifdef PIC
29 #  define TLS_IE(x) \
30   ({ int *__l;                                                                \
31      asm ("movl %%gs:0,%0\n\t"                                                \
32           "subl " #x "@gottpoff(%%ebx),%0"                                    \
33           : "=r" (__l));                                                      \
34      __l; })
35 # else
36 #  define TLS_IE(x) \
37   ({ int *__l, __b;                                                           \
38      asm ("call 1f\n\t"                                                       \
39           ".subsection 1\n"                                                   \
40           "1:\tmovl (%%esp), %%ebx\n\t"                                       \
41           "ret\n\t"                                                           \
42           ".previous\n\t"                                                     \
43           "addl $_GLOBAL_OFFSET_TABLE_, %%ebx\n\t"                            \
44           "movl %%gs:0,%0\n\t"                                                \
45           "subl " #x "@gottpoff(%%ebx),%0"                                    \
46           : "=r" (__l), "=&b" (__b));                                         \
47      __l; })
48 # endif
49
50 # ifdef PIC
51 #  define TLS_LD(x) \
52   ({ int *__l, __c, __d;                                                      \
53      asm ("leal " #x "@tlsldm(%%ebx),%%eax\n\t"                               \
54           "call ___tls_get_addr@plt\n\t"                                      \
55           "leal " #x "@dtpoff(%%eax), %%eax"                                  \
56           : "=a" (__l), "=&c" (__c), "=&d" (__d));                            \
57      __l; })
58 # else
59 #  define TLS_LD(x) \
60   ({ int *__l, __b, __c, __d;                                                 \
61      asm ("call 1f\n\t"                                                       \
62           ".subsection 1\n"                                                   \
63           "1:\tmovl (%%esp), %%ebx\n\t"                                       \
64           "ret\n\t"                                                           \
65           ".previous\n\t"                                                     \
66           "addl $_GLOBAL_OFFSET_TABLE_, %%ebx\n\t"                            \
67           "leal " #x "@tlsldm(%%ebx),%%eax\n\t"                               \
68           "call ___tls_get_addr@plt\n\t"                                      \
69           "leal " #x "@dtpoff(%%eax), %%eax"                                  \
70           : "=a" (__l), "=&b" (__b), "=&c" (__c), "=&d" (__d));               \
71      __l; })
72 # endif
73
74 # ifdef PIC
75 #  define TLS_GD(x) \
76   ({ int *__l, __c, __d;                                                      \
77      asm ("leal " #x "@tlsgd(%%ebx),%%eax\n\t"                                \
78           "call ___tls_get_addr@plt\n\t"                                      \
79           "nop"                                                               \
80           : "=a" (__l), "=&c" (__c), "=&d" (__d));                            \
81      __l; })
82 # else
83 #  define TLS_GD(x) \
84   ({ int *__l, __b, __c, __d;                                                 \
85      asm ("call 1f\n\t"                                                       \
86           ".subsection 1\n"                                                   \
87           "1:\tmovl (%%esp), %%ebx\n\t"                                       \
88           "ret\n\t"                                                           \
89           ".previous\n\t"                                                     \
90           "addl $_GLOBAL_OFFSET_TABLE_, %%ebx\n\t"                            \
91           "leal " #x "@tlsgd(%%ebx),%%eax\n\t"                                \
92           "call ___tls_get_addr@plt\n\t"                                      \
93           "nop"                                                               \
94           : "=a" (__l), "=&b" (__b), "=&c" (__c), "=&d" (__d));               \
95      __l; })
96 # endif
97
98 #elif defined __x86_64__
99
100 # define TLS_LE(x) \
101   ({ int *__l;                                                                \
102      asm ("movq %%fs:0,%0\n\t"                                                \
103           "leaq " #x "@tpoff(%0), %0"                                         \
104           : "=r" (__l));                                                      \
105      __l; })
106
107 # define TLS_IE(x) \
108   ({ int *__l;                                                                \
109      asm ("movq %%fs:0,%0\n\t"                                                \
110           "addq " #x "@gottpoff(%%rip),%0"                                    \
111           : "=r" (__l));                                                      \
112      __l; })
113
114 # define TLS_LD(x) \
115   ({ int *__l, __c, __d;                                                      \
116      asm ("leaq " #x "@tlsld(%%rip),%%rdi\n\t"                                \
117           "call __tls_get_addr@plt\n\t"                                       \
118           "leaq " #x "@dtpoff(%%rax), %%rax"                                  \
119           : "=a" (__l), "=&c" (__c), "=&d" (__d)                              \
120           : : "rdi", "rsi", "r8", "r9", "r10", "r11");                        \
121      __l; })
122
123 # define TLS_GD(x) \
124   ({ int *__l, __c, __d;                                                      \
125      asm (".byte 0x66\n\t"                                                    \
126           "leaq " #x "@tlsgd(%%rip),%%rdi\n\t"                                \
127           ".word 0x6666\n\t"                                                  \
128           "rex64\n\t"                                                         \
129           "call __tls_get_addr@plt"                                           \
130           : "=a" (__l), "=&c" (__c), "=&d" (__d)                              \
131           : : "rdi", "rsi", "r8", "r9", "r10", "r11");                        \
132      __l; })
133
134 #elif defined __sh__
135
136 # define TLS_LE(x) \
137   ({ int *__l; void *__tp;                                                    \
138      asm ("stc gbr,%1\n\t"                                                    \
139           "mov.l 1f,%0\n\t"                                                   \
140           "bra 2f\n\t"                                                        \
141           " add %1,%0\n\t"                                                    \
142           ".align 2\n\t"                                                      \
143           "1: .long " #x "@tpoff\n\t"                                         \
144           "2:"                                                                \
145           : "=r" (__l), "=r" (__tp));                                         \
146      __l; })
147
148 # define TLS_IE(x) \
149   ({ int *__l; void *__tp;                                                    \
150      asm ("mova 0f,r0\n\t"                                                    \
151           "mov.l 0f,r12\n\t"                                                  \
152           "add r0,r12\n\t"                                                    \
153           "mov.l 1f,r0\n\t"                                                   \
154           "stc gbr,%1\n\t"                                                    \
155           "mov.l @(r0,r12),%0\n\t"                                            \
156           "bra 2f\n\t"                                                        \
157           " add %1,%0\n\t"                                                    \
158           ".align 2\n\t"                                                      \
159           "1: .long " #x "@gottpoff\n\t"                                      \
160           "0: .long _GLOBAL_OFFSET_TABLE_\n\t"                                \
161           "2:"                                                                \
162           : "=r" (__l), "=r" (__tp) : : "r0", "r12");                         \
163      __l; })
164
165 # define TLS_LD(x) \
166   ({ int *__l;                                                                \
167      asm ("mova 0f,r0\n\t"                                                    \
168           "mov.l 0f,r12\n\t"                                                  \
169           "add r0,r12\n\t"                                                    \
170           "mov.l 1f,r4\n\t"                                                   \
171           "mova 2f,r0\n\t"                                                    \
172           "mov.l 2f,r1\n\t"                                                   \
173           "add r0,r1\n\t"                                                     \
174           "jsr @r1\n\t"                                                       \
175           " add r12,r4\n\t"                                                   \
176           "bra 4f\n\t"                                                        \
177           " nop\n\t"                                                          \
178           ".align 2\n\t"                                                      \
179           "1: .long " #x "@tlsldm\n\t"                                        \
180           "2: .long __tls_get_addr@plt\n\t"                                   \
181           "0: .long _GLOBAL_OFFSET_TABLE_\n\t"                                \
182           "4: mov.l 3f,%0\n\t"                                                \
183           "bra 5f\n\t"                                                        \
184           " add r0,%0\n\t"                                                    \
185           ".align 2\n\t"                                                      \
186           "3: .long " #x "@dtpoff\n\t"                                        \
187           "5:"                                                                \
188           : "=r" (__l) : : "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",    \
189                            "r12", "pr", "t");                                 \
190      __l; })
191
192 # define TLS_GD(x) \
193   ({ int *__l;                                                                \
194      asm ("mova 0f,r0\n\t"                                                    \
195           "mov.l 0f,r12\n\t"                                                  \
196           "add r0,r12\n\t"                                                    \
197           "mov.l 1f,r4\n\t"                                                   \
198           "mova 2f,r0\n\t"                                                    \
199           "mov.l 2f,r1\n\t"                                                   \
200           "add r0,r1\n\t"                                                     \
201           "jsr @r1\n\t"                                                       \
202           " add r12,r4\n\t"                                                   \
203           "bra 3f\n\t"                                                        \
204           " mov r0,%0\n\t"                                                    \
205           ".align 2\n\t"                                                      \
206           "1: .long " #x "@tlsgd\n\t"                                         \
207           "2: .long __tls_get_addr@plt\n\t"                                   \
208           "0: .long _GLOBAL_OFFSET_TABLE_\n\t"                                \
209           "3:"                                                                \
210           : "=r" (__l) : : "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",    \
211                            "r12", "pr", "t");                                 \
212      __l; })
213
214 #elif defined __alpha__
215
216 register void *__gp __asm__("$29");
217
218 # define TLS_LE(x)                                                      \
219   ({ int *__l;                                                          \
220      asm ("call_pal 158\n\tlda $0," #x "($0)\t\t!tprel" : "=v"(__l));   \
221      __l; })
222
223 # define TLS_IE(x)                                                      \
224   ({ char *__tp; unsigned long __o;                                     \
225      asm ("call_pal 158\n\tldq %1," #x "($gp)\t\t!gottprel"             \
226           : "=v"(__tp), "=r"(__o) : "r"(__gp));                         \
227      (int *)(__tp + __o); })
228
229 # define TLS_LD(x)                                                      \
230   ({ extern void *__tls_get_addr(void *); int *__l; void *__i;          \
231      asm ("lda %0," #x "($gp)\t\t!tlsldm" : "=r" (__i) : "r"(__gp));    \
232      __i = __tls_get_addr(__i);                                         \
233      asm ("lda %0, " #x "(%1)\t\t!dtprel" : "=r"(__l) : "r"(__i));      \
234      __l; })
235           
236 # define TLS_GD(x)                                                      \
237   ({ extern void *__tls_get_addr(void *); void *__i;                    \
238      asm ("lda %0," #x "($gp)\t\t!tlsgd" : "=r" (__i) : "r"(__gp));     \
239      (int *) __tls_get_addr(__i); })
240
241 #else
242 # error "No support for this architecture so far."
243 #endif