Back to index

glibc  2.9
libc-tls.c
Go to the documentation of this file.
00001 /* Initialization code for TLS in statically linked application.
00002    Copyright (C) 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <errno.h>
00021 #include <ldsodefs.h>
00022 #include <tls.h>
00023 #include <unistd.h>
00024 #include <stdio.h>
00025 #include <sys/param.h>
00026 
00027 
00028 #ifdef SHARED
00029  #error makefile bug, this file is for static only
00030 #endif
00031 
00032 extern ElfW(Phdr) *_dl_phdr;
00033 extern size_t _dl_phnum;
00034 
00035 
00036 static dtv_t static_dtv[2 + TLS_SLOTINFO_SURPLUS];
00037 
00038 
00039 static struct
00040 {
00041   struct dtv_slotinfo_list si;
00042   /* The dtv_slotinfo_list data structure does not include the actual
00043      information since it is defined as an array of size zero.  We define
00044      here the necessary entries.  Note that it is not important whether
00045      there is padding or not since we will always access the information
00046      through the 'si' element.  */
00047   struct dtv_slotinfo info[2 + TLS_SLOTINFO_SURPLUS];
00048 } static_slotinfo;
00049 
00050 /* Fake link map for the application.  */
00051 static struct link_map static_map;
00052 
00053 
00054 /* Highest dtv index currently needed.  */
00055 size_t _dl_tls_max_dtv_idx;
00056 /* Flag signalling whether there are gaps in the module ID allocation.  */
00057 bool _dl_tls_dtv_gaps;
00058 /* Information about the dtv slots.  */
00059 struct dtv_slotinfo_list *_dl_tls_dtv_slotinfo_list;
00060 /* Number of modules in the static TLS block.  */
00061 size_t _dl_tls_static_nelem;
00062 /* Size of the static TLS block.  Giving this initialized value
00063    preallocates some surplus bytes in the static TLS area.  */
00064 size_t _dl_tls_static_size = 2048;
00065 /* Size actually allocated in the static TLS block.  */
00066 size_t _dl_tls_static_used;
00067 /* Alignment requirement of the static TLS block.  */
00068 size_t _dl_tls_static_align;
00069 
00070 /* Generation counter for the dtv.  */
00071 size_t _dl_tls_generation;
00072 
00073 
00074 /* Additional definitions needed by TLS initialization.  */
00075 #ifdef TLS_INIT_HELPER
00076 TLS_INIT_HELPER
00077 #endif
00078 
00079 static inline void
00080 init_slotinfo (void)
00081 {
00082   /* Create the slotinfo list.  */
00083   static_slotinfo.si.len = (((char *) (&static_slotinfo + 1)
00084                           - (char *) &static_slotinfo.si.slotinfo[0])
00085                          / sizeof static_slotinfo.si.slotinfo[0]);
00086   // static_slotinfo.si.next = NULL;      already zero
00087 
00088   /* The slotinfo list.  Will be extended by the code doing dynamic
00089      linking.  */
00090   GL(dl_tls_max_dtv_idx) = 1;
00091   GL(dl_tls_dtv_slotinfo_list) = &static_slotinfo.si;
00092 }
00093 
00094 static inline void
00095 init_static_tls (size_t memsz, size_t align)
00096 {
00097   /* That is the size of the TLS memory for this object.  The initialized
00098      value of _dl_tls_static_size is provided by dl-open.c to request some
00099      surplus that permits dynamic loading of modules with IE-model TLS.  */
00100   GL(dl_tls_static_size) = roundup (memsz + GL(dl_tls_static_size),
00101                                 TLS_TCB_ALIGN);
00102   GL(dl_tls_static_used) = memsz;
00103   /* The alignment requirement for the static TLS block.  */
00104   GL(dl_tls_static_align) = align;
00105   /* Number of elements in the static TLS block.  */
00106   GL(dl_tls_static_nelem) = GL(dl_tls_max_dtv_idx);
00107 }
00108 
00109 void
00110 __libc_setup_tls (size_t tcbsize, size_t tcbalign)
00111 {
00112   void *tlsblock;
00113   size_t memsz = 0;
00114   size_t filesz = 0;
00115   void *initimage = NULL;
00116   size_t align = 0;
00117   size_t max_align = tcbalign;
00118   size_t tcb_offset;
00119   ElfW(Phdr) *phdr;
00120 
00121   /* Look through the TLS segment if there is any.  */
00122   if (_dl_phdr != NULL)
00123     for (phdr = _dl_phdr; phdr < &_dl_phdr[_dl_phnum]; ++phdr)
00124       if (phdr->p_type == PT_TLS)
00125        {
00126          /* Remember the values we need.  */
00127          memsz = phdr->p_memsz;
00128          filesz = phdr->p_filesz;
00129          initimage = (void *) phdr->p_vaddr;
00130          align = phdr->p_align;
00131          if (phdr->p_align > max_align)
00132            max_align = phdr->p_align;
00133          break;
00134        }
00135 
00136   /* We have to set up the TCB block which also (possibly) contains
00137      'errno'.  Therefore we avoid 'malloc' which might touch 'errno'.
00138      Instead we use 'sbrk' which would only uses 'errno' if it fails.
00139      In this case we are right away out of memory and the user gets
00140      what she/he deserves.
00141 
00142      The initialized value of _dl_tls_static_size is provided by dl-open.c
00143      to request some surplus that permits dynamic loading of modules with
00144      IE-model TLS.  */
00145 #if TLS_TCB_AT_TP
00146   tcb_offset = roundup (memsz + GL(dl_tls_static_size), tcbalign);
00147   tlsblock = __sbrk (tcb_offset + tcbsize + max_align);
00148 #elif TLS_DTV_AT_TP
00149   tcb_offset = roundup (tcbsize, align ?: 1);
00150   tlsblock = __sbrk (tcb_offset + memsz + max_align
00151                    + TLS_PRE_TCB_SIZE + GL(dl_tls_static_size));
00152   tlsblock += TLS_PRE_TCB_SIZE;
00153 #else
00154   /* In case a model with a different layout for the TCB and DTV
00155      is defined add another #elif here and in the following #ifs.  */
00156 # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
00157 #endif
00158 
00159   /* Align the TLS block.  */
00160   tlsblock = (void *) (((uintptr_t) tlsblock + max_align - 1)
00161                      & ~(max_align - 1));
00162 
00163   /* Initialize the dtv.  [0] is the length, [1] the generation counter.  */
00164   static_dtv[0].counter = (sizeof (static_dtv) / sizeof (static_dtv[0])) - 2;
00165   // static_dtv[1].counter = 0;           would be needed if not already done
00166 
00167   /* Initialize the TLS block.  */
00168 #if TLS_TCB_AT_TP
00169   static_dtv[2].pointer.val = ((char *) tlsblock + tcb_offset
00170                             - roundup (memsz, align ?: 1));
00171   static_map.l_tls_offset = roundup (memsz, align ?: 1);
00172 #elif TLS_DTV_AT_TP
00173   static_dtv[2].pointer.val = (char *) tlsblock + tcb_offset;
00174   static_map.l_tls_offset = tcb_offset;
00175 #else
00176 # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
00177 #endif
00178   static_dtv[2].pointer.is_static = true;
00179   /* sbrk gives us zero'd memory, so we don't need to clear the remainder.  */
00180   memcpy (static_dtv[2].pointer.val, initimage, filesz);
00181 
00182   /* Install the pointer to the dtv.  */
00183 
00184   /* Initialize the thread pointer.  */
00185 #if TLS_TCB_AT_TP
00186   INSTALL_DTV ((char *) tlsblock + tcb_offset, static_dtv);
00187 
00188   const char *lossage = TLS_INIT_TP ((char *) tlsblock + tcb_offset, 0);
00189 #elif TLS_DTV_AT_TP
00190   INSTALL_DTV (tlsblock, static_dtv);
00191   const char *lossage = TLS_INIT_TP (tlsblock, 0);
00192 #else
00193 # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
00194 #endif
00195   if (__builtin_expect (lossage != NULL, 0))
00196     __libc_fatal (lossage);
00197 
00198   /* We have to create a fake link map which normally would be created
00199      by the dynamic linker.  It just has to have enough information to
00200      make the TLS routines happy.  */
00201   static_map.l_tls_align = align;
00202   static_map.l_tls_blocksize = memsz;
00203   static_map.l_tls_initimage = initimage;
00204   static_map.l_tls_initimage_size = filesz;
00205   static_map.l_type = lt_executable;
00206   static_map.l_tls_modid = 1;
00207 
00208   init_slotinfo ();
00209   // static_slotinfo.si.slotinfo[1].gen = 0; already zero
00210   static_slotinfo.si.slotinfo[1].map = &static_map;
00211 
00212   memsz = roundup (memsz, align ?: 1);
00213 
00214 #if TLS_TCB_AT_TP
00215   memsz += tcbsize;
00216 #elif TLS_DTV_AT_TP
00217   memsz += tcb_offset;
00218 #endif
00219 
00220   init_static_tls (memsz, MAX (TLS_TCB_ALIGN, max_align));
00221 }
00222 
00223 /* This is called only when the data structure setup was skipped at startup,
00224    when there was no need for it then.  Now we have dynamically loaded
00225    something needing TLS, or libpthread needs it.  */
00226 int
00227 internal_function
00228 _dl_tls_setup (void)
00229 {
00230   init_slotinfo ();
00231   init_static_tls (
00232 #if TLS_TCB_AT_TP
00233                  TLS_TCB_SIZE,
00234 #else
00235                  0,
00236 #endif
00237                  TLS_TCB_ALIGN);
00238   return 0;
00239 }
00240 
00241 
00242 /* This is the minimal initialization function used when libpthread is
00243    not used.  */
00244 void
00245 __attribute__ ((weak))
00246 __pthread_initialize_minimal (void)
00247 {
00248   __libc_setup_tls (TLS_INIT_TCB_SIZE, TLS_INIT_TCB_ALIGN);
00249 }