Update.
[platform/upstream/glibc.git] / sysdeps / generic / libc-tls.c
1 /* Initialization code for TLS in statically linked application.
2    Copyright (C) 2002 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4
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.
9
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.
14
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
18    02111-1307 USA.  */
19
20 #include <errno.h>
21 #include <ldsodefs.h>
22 #include <tls.h>
23 #include <unistd.h>
24
25
26 #ifdef USE_TLS
27 extern ElfW(Phdr) *_dl_phdr;
28 extern size_t _dl_phnum;
29
30
31 /* DTV with just one element plus overhead.  */
32 static dtv_t static_dtv[3];
33
34
35 static struct
36 {
37   struct dtv_slotinfo_list si;
38   /* The dtv_slotinfo_list data structure does not include the actual
39      informatin since it is defined as an array of size zero.  We
40      define here the necessary entries.  Not that it is not important
41      whether there is padding or not since we will always access the
42      informatin through the 'si' element.  */
43   struct dtv_slotinfo info[2 + TLS_SLOTINFO_SURPLUS];
44 } static_slotinfo;
45
46 /* Fake link map for the application.  */
47 static struct link_map static_map;
48
49
50 /* Additional definitions needed by TLS initialization.  */
51 #ifdef TLS_INIT_HELPER
52 TLS_INIT_HELPER
53 #endif
54
55
56 void
57 __libc_setup_tls (size_t tcbsize, size_t tcbalign)
58 {
59   void *tlsblock;
60   size_t memsz = 0;
61   size_t filesz = 0;
62   size_t initimage = 0;
63   size_t align = 0;
64   size_t max_align = tcbalign;
65   size_t loadaddr = ~0ul;
66   size_t tcb_offset;
67   ElfW(Phdr) *phdr;
68
69   /* Look through the TLS segment if there is any.  */
70   if (_dl_phdr != NULL)
71     for (phdr = _dl_phdr; phdr < &_dl_phdr[_dl_phnum]; ++phdr)
72       {
73         if (phdr->p_type == PT_TLS)
74           {
75             /* Remember the values we need.  */
76             memsz = phdr->p_memsz;
77             filesz = phdr->p_filesz;
78             initimage = phdr->p_vaddr;
79             align = phdr->p_align;
80             if (phdr->p_align > max_align)
81               max_align = phdr->p_align;
82           }
83         else if (phdr->p_type == PT_LOAD)
84           {
85             /* We have to find the load address which is not easy.
86                Look for the load segment with the lowest address.  */
87             if (phdr->p_vaddr < loadaddr)
88               loadaddr = phdr->p_vaddr;
89           }
90     }
91
92   if (memsz == 0 && tcbsize == 0)
93     /* We do not need a TLS block and no thread descriptor.  */
94     return;
95
96   /* We have to set up the TCB block which also (possibly) contains
97      'errno'.  Therefore we avoid 'malloc' which might touch 'errno'.
98      Instead we use 'sbrk' which would only uses 'errno' if it fails.
99      In this case we are right away out of memory and the user gets
100      what she/he deserves.  */
101 # if TLS_TCB_AT_TP
102   tlsblock = __sbrk (roundup (memsz, tcbalign) + tcbsize + max_align);
103 # elif TLS_DTV_AT_TP
104   tlsblock = __sbrk (roundup (tcbsize, align) + memsz + max_align);
105 # else
106   /* In case a model with a different layout for the TCB and DTV
107      is defined add another #elif here and in the following #ifs.  */
108 #  error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
109 # endif
110
111   /* Align the TLS block.  */
112   tlsblock = (void *) (((uintptr_t) tlsblock + max_align - 1)
113                        & ~(max_align - 1));
114
115   /* Initialize the dtv.  [0] is the length, [1] the generation counter.  */
116   static_dtv[0].counter = 1;
117   // static_dtv[1].counter = 0;         would be needed if not already done
118
119   /* Initialize the TLS block.  */
120 # if TLS_TCB_AT_TP
121   static_dtv[2].pointer = tlsblock;
122 # elif TLS_DTV_AT_TP
123   tcb_offset = roundup (tcbsize, align);
124   static_dtv[2].pointer = (char *) tlsblock + tcb_offset;
125 # else
126 #  error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
127 # endif
128   memset (__mempcpy (static_dtv[2].pointer, (char *) loadaddr + initimage,
129                      filesz),
130           '\0', memsz - filesz);
131
132   /* Install the pointer to the dtv.  */
133
134   /* Initialize the thread pointer.  */
135 # if TLS_TCB_AT_TP
136   tcb_offset = roundup (memsz, tcbalign);
137
138   INSTALL_DTV ((char *) tlsblock + tcb_offset, static_dtv);
139
140   TLS_INIT_TP ((char *) tlsblock + tcb_offset);
141 # elif TLS_DTV_AT_TP
142   INSTALL_DTV (tlsblock, static_dtv);
143   TLS_INIT_TP (tlsblock);
144 # else
145 #  error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
146 # endif
147
148   /* We have to create a fake link map which normally would be created
149      by the dynamic linker.  It just has to have enough information to
150      make the TLS routines happy.  */
151   static_map.l_tls_align = align;
152   static_map.l_tls_blocksize = memsz;
153   static_map.l_tls_initimage_size = filesz;
154   static_map.l_tls_offset = tcb_offset;
155   static_map.l_type = lt_executable;
156   static_map.l_tls_modid = 1;
157
158   /* Create the slotinfo list.  */
159   static_slotinfo.si.len = 1;   /* Only one element.  */
160   // static_slotinfo.si.next = NULL;    already zero
161
162   static_slotinfo.si.slotinfo[1].gen = 0;
163   static_slotinfo.si.slotinfo[1].map = &static_map;
164
165   /* The slotinfo list.  Will be extended by the code doing dynamic
166      linking.  */
167   GL(dl_tls_max_dtv_idx) = 1;
168   GL(dl_tls_dtv_slotinfo_list) = &static_slotinfo.si;
169
170   /* That is the size of the TLS memory for this object.  */
171   GL(dl_tls_static_size) = (roundup (memsz, align ?: 1)
172 # if TLS_TCB_AT_TP
173                             + tcbsize
174 # endif
175                             );
176   /* The alignment requirement for the static TLS block.  */
177   GL(dl_tls_static_align) = MAX (TLS_TCB_ALIGN, max_align);
178   /* Number of elements in the static TLS block.  */
179   GL(dl_tls_static_nelem) = GL(dl_tls_max_dtv_idx);
180 }
181
182
183 /* This is the minimal initialization function used when libpthread is
184    not used.  */
185 void
186 __attribute__ ((weak))
187 __pthread_initialize_minimal (void)
188 {
189   __libc_setup_tls (0, 1);
190 }
191 #endif