source: mainline/uspace/lib/c/generic/thread/tls.c@ e43acd3

Last change on this file since e43acd3 was 32254d6, checked in by GitHub <noreply@…>, 5 months ago

init RTLD runtime at load time even for statically linked binaries (#242)

init RTLD runtime at load time even for statically linked binaries

  • Property mode set to 100644
File size: 7.1 KB
Line 
1/*
2 * Copyright (c) 2006 Jakub Jermar
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup libc
30 * @{
31 */
32/** @file
33 *
34 * Support for thread-local storage, as described in:
35 * Drepper U.: ELF Handling For Thread-Local Storage, 2005
36 */
37
38#include <assert.h>
39#include <stdalign.h>
40#include <stddef.h>
41#include <align.h>
42#include <tls.h>
43#include <stdlib.h>
44#include <str.h>
45#include <macros.h>
46#include <elf/elf.h>
47#include <as.h>
48
49#include <libarch/config.h>
50
51#ifdef CONFIG_RTLD
52#include <rtld/rtld.h>
53#endif
54
55#include "../private/libc.h"
56
57#if !defined(CONFIG_TLS_VARIANT_1) && !defined(CONFIG_TLS_VARIANT_2)
58#error Unknown TLS variant.
59#endif
60
61static ptrdiff_t _tcb_data_offset(const void *elf)
62{
63 const elf_segment_header_t *tls =
64 elf_get_phdr(elf, PT_TLS);
65
66 size_t tls_align = tls ? tls->p_align : 1;
67
68#ifdef CONFIG_TLS_VARIANT_1
69 return ALIGN_UP((ptrdiff_t) sizeof(tcb_t), tls_align);
70#else
71 size_t tls_size = tls ? tls->p_memsz : 0;
72 return -ALIGN_UP((ptrdiff_t) tls_size, max(tls_align, alignof(tcb_t)));
73#endif
74}
75
76/** Get address of static TLS block - only when RTLD is not initialized */
77void *tls_get(void)
78{
79#ifdef CONFIG_RTLD
80 assert(runtime_env == NULL);
81#endif
82 return (uint8_t *)__tcb_get() + _tcb_data_offset(__progsymbols.elfstart);
83}
84
85static tcb_t *tls_make_generic(const void *elf, void *(*alloc)(size_t, size_t))
86{
87 /*
88 * See also rtld/module.c -> modules_process_tls(), where we have less
89 * messy code for the dynamic-linking version of this.
90 */
91 assert(!elf_get_phdr(elf, PT_DYNAMIC));
92#ifdef CONFIG_RTLD
93 assert(runtime_env == NULL);
94#endif
95
96 const elf_segment_header_t *tls = elf_get_phdr(elf, PT_TLS);
97 size_t tls_size = tls ? tls->p_memsz : 0;
98 size_t tls_align = tls ? tls->p_align : 1;
99
100 /*
101 * We don't currently support alignment this big,
102 * and neither should we need to.
103 */
104 assert(tls_align <= PAGE_SIZE);
105
106 /*
107 * FIXME: the calculation of alloc_size shouldn't include the alignment
108 * of tcb_t (at least in Variant II)
109 * See https://github.com/HelenOS/helenos/pull/240/files/4ef27ebf98a0656e09889b7d00efdec03343f1aa#r1929592924
110 * (you will also need to fix _tcb_data_offset)
111 */
112
113#ifdef CONFIG_TLS_VARIANT_1
114 size_t alloc_size =
115 ALIGN_UP(sizeof(tcb_t), tls_align) + tls_size;
116#else
117 size_t alloc_size =
118 ALIGN_UP(tls_size, max(tls_align, alignof(tcb_t))) + sizeof(tcb_t);
119#endif
120
121 void *area = alloc(max(tls_align, alignof(tcb_t)), alloc_size);
122 if (!area)
123 return NULL;
124
125#ifdef CONFIG_TLS_VARIANT_1
126 tcb_t *tcb = area;
127 uint8_t *data = (uint8_t *)tcb + _tcb_data_offset(elf);
128 memset(tcb, 0, sizeof(*tcb));
129#else
130 uint8_t *data = area;
131 tcb_t *tcb = (tcb_t *) (data - _tcb_data_offset(elf));
132 memset(tcb, 0, sizeof(tcb_t));
133 tcb->self = tcb;
134#endif
135
136 if (!tls)
137 return tcb;
138
139 uintptr_t bias = elf_get_bias(elf);
140
141 /* Copy thread local data from the initialization image. */
142 memcpy(data, (void *)(tls->p_vaddr + bias), tls->p_filesz);
143 /* Zero out the thread local uninitialized data. */
144 memset(data + tls->p_filesz, 0, tls->p_memsz - tls->p_filesz);
145
146 return tcb;
147}
148
149static void *early_alloc(size_t align, size_t alloc_size)
150{
151 assert(align <= PAGE_SIZE);
152 alloc_size = ALIGN_UP(alloc_size, PAGE_SIZE);
153
154 void *area = as_area_create(AS_AREA_ANY, alloc_size,
155 AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE, AS_AREA_UNPAGED);
156 if (area == AS_MAP_FAILED)
157 return NULL;
158 return area;
159}
160
161/** Same as tls_make(), but uses as_area_create() instead of memalign().
162 * Only used in __libc_main() if the program was created by the kernel.
163 */
164tcb_t *tls_make_initial(const void *elf)
165{
166 return tls_make_generic(elf, early_alloc);
167}
168
169/** Create TLS (Thread Local Storage) data structures.
170 *
171 * @return Pointer to TCB.
172 */
173tcb_t *tls_make(const void *elf)
174{
175 // TODO: Always use rtld.
176
177#ifdef CONFIG_RTLD
178 if (runtime_env != NULL)
179 return rtld_tls_make(runtime_env);
180#endif
181
182 return tls_make_generic(elf, memalign);
183}
184
185void tls_free(tcb_t *tcb)
186{
187#ifdef CONFIG_RTLD
188 free(tcb->dtv);
189
190 if (runtime_env != NULL) {
191 tls_free_arch(tcb, runtime_env->tls_size, runtime_env->tls_align);
192 return;
193 }
194#endif
195 const elf_segment_header_t *tls =
196 elf_get_phdr(__progsymbols.elfstart, PT_TLS);
197
198 assert(tls != NULL);
199 tls_free_arch(tcb,
200 ALIGN_UP(tls->p_memsz, tls->p_align) + sizeof(tcb_t),
201 max(tls->p_align, alignof(tcb_t)));
202}
203
204#ifdef CONFIG_TLS_VARIANT_1
205/** Allocate TLS variant 1 data structures.
206 *
207 * @param data Start of TLS section. This is an output argument.
208 * @param size Size of tdata + tbss section.
209 * @return Pointer to tcb_t structure.
210 */
211tcb_t *tls_alloc_variant_1(size_t size, size_t align)
212{
213 tcb_t *tcb = memalign(align, size);
214 if (!tcb)
215 return NULL;
216 memset(tcb, 0, sizeof(tcb_t));
217 return tcb;
218}
219
220/** Free TLS variant I data structures.
221 *
222 * @param tcb Pointer to TCB structure.
223 * @param size This argument is ignored.
224 */
225void tls_free_variant_1(tcb_t *tcb, size_t size, size_t align)
226{
227 free(tcb);
228}
229#endif
230
231#ifdef CONFIG_TLS_VARIANT_2
232/** Allocate TLS variant II data structures.
233 *
234 * @param data Pointer to pointer to thread local data. This is
235 * actually an output argument.
236 * @param size Size of thread local data.
237 * @param align Alignment of thread local data.
238 * @return Pointer to TCB structure.
239 */
240tcb_t *tls_alloc_variant_2(size_t size, size_t align)
241{
242 void *data = memalign(align, size);
243 if (data == NULL)
244 return NULL;
245
246 tcb_t *tcb = (tcb_t *) (data + size - sizeof(tcb_t));
247 memset(tcb, 0, sizeof(tcb_t));
248 tcb->self = tcb;
249 return tcb;
250}
251
252/** Free TLS variant II data structures.
253 *
254 * @param tcb Pointer to TCB structure.
255 * @param size Size of thread local data.
256 * @param align Alignment of thread local data.
257 */
258void tls_free_variant_2(tcb_t *tcb, size_t size, size_t align)
259{
260 if (tcb != NULL) {
261 void *start = ((void *) tcb) + sizeof(tcb_t) - size;
262 free(start);
263 }
264}
265#endif
266
267/** @}
268 */
Note: See TracBrowser for help on using the repository browser.