1 /*
2  * Support for dynamic linking code in static libc.
3  * Copyright (C) 1996-2002, 2003, 2004, 2005 Free Software Foundation, Inc.
4  *
5  * Partially based on GNU C Library (file: libc/elf/dl-support.c)
6  *
7  * Copyright (C) 2008 STMicroelectronics Ltd.
8  * Author: Carmelo Amoroso <carmelo.amoroso@st.com>
9  *
10  * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
11  *
12  */
13 
14 #include <link.h>
15 #include <ldso.h>
16 #include <elf.h>
17 #if defined(USE_TLS) && USE_TLS
18 #include <assert.h>
19 #include <tls.h>
20 #include <ldsodefs.h>
21 #include <string.h>
22 #endif
23 #include <bits/uClibc_page.h>
24 
25 #if defined(USE_TLS) && USE_TLS
26 
27 void (*_dl_init_static_tls) (struct link_map *) = &_dl_nothread_init_static_tls;
28 
29 #endif
30 
31 ElfW(Phdr) *_dl_phdr;
32 size_t _dl_phnum;
33 size_t _dl_pagesize;
34 
35 ElfW(auxv_t) _dl_auxvt[AUX_MAX_AT_ID];
36 ElfW(auxv_t) *_dl_auxv_start;
37 
38 void internal_function _dl_aux_init (ElfW(auxv_t) *av);
_dl_aux_init(ElfW (auxv_t)* av)39 void internal_function _dl_aux_init (ElfW(auxv_t) *av)
40 {
41    _dl_auxv_start = av;
42 
43    memset(_dl_auxvt, 0x00, sizeof(_dl_auxvt));
44    for (; av->a_type != AT_NULL; av++)
45      {
46        if (av->a_type < AUX_MAX_AT_ID)
47          _dl_auxvt[av->a_type] = *av;
48      }
49 
50    /* Get the program headers base address from the aux vect */
51    _dl_phdr = (ElfW(Phdr) *) _dl_auxvt[AT_PHDR].a_un.a_val;
52 
53    /* Get the number of program headers from the aux vect */
54    _dl_phnum = (size_t) _dl_auxvt[AT_PHNUM].a_un.a_val;
55 
56    /* Get the pagesize from the aux vect */
57    _dl_pagesize = (_dl_auxvt[AT_PAGESZ].a_un.a_val) ? (size_t) _dl_auxvt[AT_PAGESZ].a_un.a_val : PAGE_SIZE;
58 }
59 
60 #if defined(USE_TLS) && USE_TLS
61 /* Initialize static TLS area and DTV for current (only) thread.
62    libpthread implementations should provide their own hook
63    to handle all threads.  */
64 void
65 attribute_hidden
_dl_nothread_init_static_tls(struct link_map * map)66 _dl_nothread_init_static_tls (struct link_map *map)
67 {
68 # if defined(TLS_TCB_AT_TP)
69   void *dest = (char *) THREAD_SELF - map->l_tls_offset;
70 # elif defined(TLS_DTV_AT_TP)
71   void *dest = (char *) THREAD_SELF + map->l_tls_offset + TLS_PRE_TCB_SIZE;
72 # else
73 #  error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
74 # endif
75 
76   /* Fill in the DTV slot so that a later LD/GD access will find it.  */
77   dtv_t *dtv = THREAD_DTV ();
78   assert (map->l_tls_modid <= dtv[-1].counter);
79   dtv[map->l_tls_modid].pointer.val = dest;
80   dtv[map->l_tls_modid].pointer.is_static = true;
81 
82   /* Initialize the memory.  */
83   memset (mempcpy (dest, map->l_tls_initimage, map->l_tls_initimage_size),
84 	  '\0', map->l_tls_blocksize - map->l_tls_initimage_size);
85 }
86 
87 #endif
88 
89