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 <elf.h>
16 #if defined(__UCLIBC_HAS_THREADS__)
17 #include <bits/libc-lock.h>
18 #endif
19 #if defined(USE_TLS) && USE_TLS
20 #include <assert.h>
21 #include <tls.h>
22 #include <stdbool.h>
23 #include <ldsodefs.h>
24 #include <string.h>
25 #endif
26 #include <bits/uClibc_page.h>
27 
28 #if defined(USE_TLS) && USE_TLS
29 
30 void (*_dl_init_static_tls) (struct link_map *) = &_dl_nothread_init_static_tls;
31 
32 #endif
33 
34 ElfW(Phdr) *_dl_phdr;
35 size_t _dl_phnum;
36 size_t _dl_pagesize;
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    /* Get the program headers base address from the aux vect */
42    _dl_phdr = (ElfW(Phdr) *) av[AT_PHDR].a_un.a_val;
43 
44    /* Get the number of program headers from the aux vect */
45    _dl_phnum = (size_t) av[AT_PHNUM].a_un.a_val;
46 
47    /* Get the pagesize from the aux vect */
48    _dl_pagesize = (av[AT_PAGESZ].a_un.a_val) ? (size_t) av[AT_PAGESZ].a_un.a_val : PAGE_SIZE;
49 }
50 
51 #if defined(USE_TLS) && USE_TLS
52 /* Initialize static TLS area and DTV for current (only) thread.
53    libpthread implementations should provide their own hook
54    to handle all threads.  */
55 void
56 attribute_hidden
_dl_nothread_init_static_tls(struct link_map * map)57 _dl_nothread_init_static_tls (struct link_map *map)
58 {
59 # if defined(TLS_TCB_AT_TP)
60   void *dest = (char *) THREAD_SELF - map->l_tls_offset;
61 # elif defined(TLS_DTV_AT_TP)
62   void *dest = (char *) THREAD_SELF + map->l_tls_offset + TLS_PRE_TCB_SIZE;
63 # else
64 #  error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
65 # endif
66 
67   /* Fill in the DTV slot so that a later LD/GD access will find it.  */
68   dtv_t *dtv = THREAD_DTV ();
69   assert (map->l_tls_modid <= dtv[-1].counter);
70   dtv[map->l_tls_modid].pointer.val = dest;
71   dtv[map->l_tls_modid].pointer.is_static = true;
72 
73   /* Initialize the memory.  */
74   memset (mempcpy (dest, map->l_tls_initimage, map->l_tls_initimage_size),
75 	  '\0', map->l_tls_blocksize - map->l_tls_initimage_size);
76 }
77 
78 #endif
79 
80 #if defined(__UCLIBC_HAS_THREADS__)
81 __rtld_lock_define_initialized_recursive (, _dl_load_lock)
82 __rtld_lock_define_initialized_recursive (, _dl_load_write_lock)
83 #endif
84