1 /* Thread-local storage handling in the ELF dynamic linker. Generic version.
2 Copyright (C) 2002 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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
23 /* We don't need any of this if TLS is not supported. */
30 /* Value used for dtv entries for which the allocation is delayed. */
31 # define TLS_DTV_UNALLOCATE ((void *) -1l)
36 _dl_next_tls_modid (void)
40 if (__builtin_expect (GL(dl_tls_dtv_gaps), false))
42 /* XXX If this method proves too costly we can optimize
43 it to use a constant time method. But I don't think
45 struct link_map *runp = GL(dl_initimage_list);
46 bool used[GL(dl_tls_max_dtv_idx)];
48 assert (runp != NULL);
51 assert (runp->l_tls_modid > 0
52 && runp->l_tls_modid <= GL(dl_tls_max_dtv_idx));
53 used[runp->l_tls_modid - 1] = true;
55 while ((runp = runp->l_tls_nextimage) != GL(dl_initimage_list));
59 /* The information about the gaps is pessimistic. It might be
60 there are actually none. */
61 if (result >= GL(dl_tls_max_dtv_idx))
63 /* Now we know there is actually no gap. Bump the maximum
64 ID number and remember that there are no gaps. */
65 result = ++GL(dl_tls_max_dtv_idx);
66 GL(dl_tls_dtv_gaps) = false;
69 while (used[result++]);
72 /* No gaps, allocate a new entry. */
73 result = ++GL(dl_tls_max_dtv_idx);
81 _dl_determine_tlsoffset (struct link_map *firstp)
83 struct link_map *runp = firstp;
88 /* We simply start with zero. */
93 max_align = MAX (max_align, runp->l_tls_align);
95 /* Compute the offset of the next TLS block. */
96 offset = roundup (offset + runp->l_tls_blocksize, runp->l_tls_align);
98 /* XXX For some architectures we perhaps should store the
100 runp->l_tls_offset = offset;
102 while ((runp = runp->l_tls_nextimage) != firstp);
104 /* The thread descriptor (pointed to by the thread pointer) has its
105 own alignment requirement. Adjust the static TLS size
106 and TLS offsets appropriately. */
107 if (offset % TLS_TCB_ALIGN != 0)
109 size_t add = TLS_TCB_ALIGN - offset % TLS_TCB_ALIGN;
111 /* XXX If the offset stored is negative we must subtract here. */
116 runp->l_tls_offset += add;
117 while ((runp = runp->l_tls_nextimage) != firstp);
120 GL(dl_tls_static_size) = offset + TLS_TCB_SIZE;
122 struct link_map *lastp;
124 /* The first block starts right after the TCB. */
125 offset = TLS_TCB_SIZE;
126 max_align = runp->l_tls_align;
127 runp->l_tls_offset = offset;
130 while ((runp = runp->l_tls_nextimage) != firstp)
132 max_align = MAX (max_align, runp->l_tls_align);
134 /* Compute the offset of the next TLS block. */
135 offset = roundup (offset + lastp->l_tls_blocksize, runp->l_tls_align);
137 runp->l_tls_offset = offset;
142 GL(dl_tls_static_size) = offset + lastp->l_tls_blocksize;
144 # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
147 /* The alignment requirement for the static TLS block. */
148 GL(dl_tls_static_align) = MAX (TLS_TCB_ALIGN, max_align);
152 /* The __tls_get_addr function has two basic forms which differ in the
153 arguments. The IA-64 form takes two parameters, the module ID and
154 offset. The form used, among others, on IA-32 takes a reference to
155 a special structure which contain the same information. The second
156 form seems to be more often used (in the moment) so we default to
157 it. Users of the IA-64 form have to provide adequate definitions
158 of the following macros. */
159 # ifndef GET_ADDR_ARGS
160 # define GET_ADDR_ARGS tls_index *ti
162 # ifndef GET_ADDR_MODULE
163 # define GET_ADDR_MODULE ti->ti_module
165 # ifndef GET_ADDR_OFFSET
166 # define GET_ADDR_OFFSET ti->ti_offset
171 __tls_get_addr (GET_ADDR_ARGS)
173 dtv_t *dtv = THREAD_DTV ();
175 if (dtv[GET_ADDR_MODULE].pointer == TLS_DTV_UNALLOCATE)
178 return (char *) dtv[GET_ADDR_MODULE].pointer + GET_ADDR_OFFSET;