Move all files into ports/ subdirectory in preparation for merge with glibc
[glibc.git] / ports / sysdeps / arm / dl-tlsdesc.S
blobf21555281f01504910b5133faa15a30146a2e5c2
1 /* Thread-local storage handling in the ELF dynamic linker.  ARM version.
2    Copyright (C) 2006, 2010, 2011 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library.  If not, see
17    <http://www.gnu.org/licenses/>.  */
19 #include <sysdep.h>
20 #include <tls.h>
21 #include "tlsdesc.h"
23 #ifdef __USE_BX__
24   #define BX(x) bx x
25 #else
26   #define BX(x) mov pc, x
27 #endif
29         .text
30         @ emit debug information with cfi
31         @ use arm-specific pseudos for unwinding itself
32         .cfi_sections .debug_frame
33         .hidden _dl_tlsdesc_return
34         .global _dl_tlsdesc_return
35         .type   _dl_tlsdesc_return,#function
36         cfi_startproc
37         .fnstart
38         .align 2
39 _dl_tlsdesc_return:
40         ldr     r0, [r0]
41         BX      (lr)
42         .fnend
43         cfi_endproc
44         .size   _dl_tlsdesc_return, .-_dl_tlsdesc_return
46         .hidden _dl_tlsdesc_undefweak
47         .global _dl_tlsdesc_undefweak
48         .type   _dl_tlsdesc_undefweak,#function
49         cfi_startproc
50         .fnstart
51         .align 2
52 _dl_tlsdesc_undefweak:
53         @ Are we allowed a misaligned stack pointer calling read_tp?
54         .save   {lr}
55         stmdb   sp!, {lr}
56         cfi_adjust_cfa_offset (4)
57         cfi_rel_offset (lr,0)
58         bl      __aeabi_read_tp
59         rsb     r0, r0, #0
60         ldmia   sp!, {lr}
61         cfi_adjust_cfa_offset (-4)
62         cfi_restore (lr)
63         BX      (lr)
65         cfi_endproc
66         .fnend
67         .size   _dl_tlsdesc_undefweak, .-_dl_tlsdesc_undefweak
69 #ifdef SHARED
70         .hidden _dl_tlsdesc_dynamic
71         .global _dl_tlsdesc_dynamic
72         .type   _dl_tlsdesc_dynamic,#function
76         The assembly code that follows is a rendition of the following
77         C code, hand-optimized a little bit.
79 ptrdiff_t
80 _dl_tlsdesc_dynamic(struct tlsdesc *tdp)
82        struct tlsdesc_dynamic_arg *td = tdp->argument.pointer;
83        dtv_t *dtv = (dtv_t *)THREAD_DTV();
84        if (__builtin_expect (td->gen_count <= dtv[0].counter
85                              && dtv[td->tlsinfo.ti_module].pointer.val
86                                 != TLS_DTV_UNALLOCATED,
87                              1))
88                return dtv[td->tlsinfo.ti_module].pointer.val +
89                        td->tlsinfo.ti_offset - __builtin_thread_pointer();
91        return __tls_get_addr (&td->tlsinfo) - __builtin_thread_pointer();
95         cfi_startproc
96         .fnstart
97         .align 2
98 _dl_tlsdesc_dynamic:
99         /* Our calling convention is to clobber r0, r1 and the processor
100            flags.  All others that are modified must be saved */
101         .save   {r2,r3,r4,lr}
102         stmdb   sp!, {r2,r3,r4,lr}
103         cfi_adjust_cfa_offset (16)
104         cfi_rel_offset (r2,0)
105         cfi_rel_offset (r3,4)
106         cfi_rel_offset (r4,8)
107         cfi_rel_offset (lr,12)
108         ldr     r1, [r0] /* td */
109         bl      __aeabi_read_tp
110         mov     r4, r0 /* r4 = tp */
111         ldr     r0, [r0]
112         ldr     r2, [r1, #8] /* gen_count */
113         ldr     r3, [r0]
114         cmp     r2, r3
115         bhi     1f
116         ldr     r3, [r1]
117         ldr     r2, [r0, r3, lsl #3]
118         cmn     r2, #1
119         ldrne   r3, [r1, #4]
120         addne   r3, r2, r3
121         rsbne   r0, r4, r3
122         bne     2f
123 1:      mov     r0, r1
124         bl      __tls_get_addr
125         rsb     r0, r4, r0
126 2:      ldmia   sp!, {r2,r3,r4, lr}
127         cfi_adjust_cfa_offset (-16)
128         cfi_restore (lr)
129         cfi_restore (r4)
130         cfi_restore (r3)
131         cfi_restore (r2)
132         BX      (lr)
133         .fnend
134         cfi_endproc
135         .size   _dl_tlsdesc_dynamic, .-_dl_tlsdesc_dynamic
136 #endif /* SHARED */
138 /* lazy resolved for tls descriptors.  */
139         .hidden _dl_tlsdesc_lazy_resolver
140         .global _dl_tlsdesc_lazy_resolver
141         .type   _dl_tlsdesc_lazy_resolver,#function
142         cfi_startproc
143         .fnstart
144         .align 2
145 _dl_tlsdesc_lazy_resolver:
146         /* r0 points at the tlsdesc,
147            r1 points at the GOT
148            r2 was pushed by the trampoline and used as a temp,
149               we need to pop it here.
150           We push the remaining call-clobbered registers here, and also
151           R1 -- to keep the stack correctly aligned.  */
152         /* Tell the unwinder that r2 has already been pushed.  */
153         .save   {r2}
154         cfi_adjust_cfa_offset (4)
155         cfi_rel_offset (r2, 0)
156         .save   {r0,r1,r3,ip,lr}
157         stmdb   sp!, {r0, r1, r3, ip, lr}
158         cfi_adjust_cfa_offset (20)
159         cfi_rel_offset (r0, 0)
160         cfi_rel_offset (r1, 4)
161         cfi_rel_offset (r3, 8)
162         cfi_rel_offset (ip, 12)
163         cfi_rel_offset (lr, 16)
164         bl      _dl_tlsdesc_lazy_resolver_fixup
165         ldmia   sp!, {r0, r1, r3, ip, lr}
166         cfi_adjust_cfa_offset (-20)
167         cfi_restore (lr)
168         cfi_restore (ip)
169         cfi_restore (r3)
170         cfi_restore (r1)
171         cfi_restore (r0)
172         ldmia   sp!, {r2}
173         cfi_adjust_cfa_offset (-4)
174         cfi_restore (r2)
175         ldr     r1, [r0, #4]
176         BX      (r1)
177         .fnend
178         cfi_endproc
179         .size   _dl_tlsdesc_lazy_resolver, .-_dl_tlsdesc_lazy_resolver
181 /* Holder for lazy tls descriptors being resolve in another thread.
182    Same ABI as the lazy resolver itself.  */
183         .hidden _dl_tlsdesc_resolve_hold
184         .global _dl_tlsdesc_resolve_hold
185         .type   _dl_tlsdesc_resolve_hold,#function
186         cfi_startproc
187         .fnstart
188         .align 2
189 _dl_tlsdesc_resolve_hold:
190         /* Tell the unwinder that r2 has already been pushed.  */
191         .save   {r2}
192         cfi_adjust_cfa_offset (4)
193         cfi_rel_offset (r2, 0)
194         .save   {r0,r1,r3,ip,lr}
195         stmdb   sp!, {r0, r1, r3, ip, lr}
196         cfi_adjust_cfa_offset (20)
197         cfi_rel_offset (r0, 0)
198         cfi_rel_offset (r1, 4)
199         cfi_rel_offset (r3, 8)
200         cfi_rel_offset (ip, 12)
201         cfi_rel_offset (lr, 16)
202         adr     r2, _dl_tlsdesc_resolve_hold
203         bl      _dl_tlsdesc_resolve_hold_fixup
204         ldmia   sp!, {r0, r1, r3, ip, lr}
205         cfi_adjust_cfa_offset (-20)
206         cfi_restore (lr)
207         cfi_restore (ip)
208         cfi_restore (r3)
209         cfi_restore (r1)
210         cfi_restore (r0)
211         ldmia   sp!, {r2}
212         cfi_adjust_cfa_offset (-4)
213         cfi_restore (r2)
214         ldr     r1, [r0, #4]
215         BX      (r1)
216         .fnend
217         cfi_endproc
218         .size   _dl_tlsdesc_resolve_hold, .-_dl_tlsdesc_resolve_hold