]> git.ipfire.org Git - thirdparty/gcc.git/blame - libgcc/emutls.c
Update copyright years.
[thirdparty/gcc.git] / libgcc / emutls.c
CommitLineData
8893239d 1/* TLS emulation.
a945c346 2 Copyright (C) 2006-2024 Free Software Foundation, Inc.
8893239d
RH
3 Contributed by Jakub Jelinek <jakub@redhat.com>.
4
5This file is part of GCC.
6
7GCC is free software; you can redistribute it and/or modify it under
8the terms of the GNU General Public License as published by the Free
748086b7 9Software Foundation; either version 3, or (at your option) any later
8893239d
RH
10version.
11
8893239d
RH
12GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or
14FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15for more details.
16
748086b7
JJ
17Under Section 7 of GPL version 3, you are granted additional
18permissions described in the GCC Runtime Library Exception, version
193.1, as published by the Free Software Foundation.
20
21You should have received a copy of the GNU General Public License and
22a copy of the GCC Runtime Library Exception along with this program;
23see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24<http://www.gnu.org/licenses/>. */
8893239d
RH
25
26#include "tconfig.h"
27#include "tsystem.h"
28#include "coretypes.h"
29#include "tm.h"
852b75ed 30#include "libgcc_tm.h"
8893239d
RH
31#include "gthr.h"
32
33typedef unsigned int word __attribute__((mode(word)));
34typedef unsigned int pointer __attribute__((mode(pointer)));
35
36struct __emutls_object
37{
38 word size;
39 word align;
40 union {
41 pointer offset;
42 void *ptr;
43 } loc;
44 void *templ;
45};
46
5b77de89
RH
47struct __emutls_array
48{
49 pointer size;
50 void **data[];
51};
52
b9873b4e
IS
53/* EMUTLS_ATTR is provided to allow targets to build the emulated tls
54 routines as weak definitions, for example.
55 If there is no definition, fall back to the default. */
56#ifndef EMUTLS_ATTR
57# define EMUTLS_ATTR
58#endif
59
d7ceffab
JJ
60/* __emutls_get_address and __emutls_register_common are registered as
61 builtins, but the compiler struct __emutls_object doesn't have
62 a union in there and is only created when actually needed for
63 the calls to the builtins, so the builtins are created with void *
64 arguments rather than struct __emutls_object *. Avoid
65 -Wbuiltin-declaration-mismatch warnings. */
66#pragma GCC diagnostic ignored "-Wbuiltin-declaration-mismatch"
67
b9873b4e 68EMUTLS_ATTR
c6c2b18d 69void *__emutls_get_address (struct __emutls_object *);
b9873b4e 70EMUTLS_ATTR
c6c2b18d
BE
71void __emutls_register_common (struct __emutls_object *, word, word, void *);
72
8893239d
RH
73#ifdef __GTHREADS
74#ifdef __GTHREAD_MUTEX_INIT
75static __gthread_mutex_t emutls_mutex = __GTHREAD_MUTEX_INIT;
76#else
77static __gthread_mutex_t emutls_mutex;
78#endif
79static __gthread_key_t emutls_key;
80static pointer emutls_size;
81
82static void
83emutls_destroy (void *ptr)
84{
5b77de89
RH
85 struct __emutls_array *arr = ptr;
86 pointer size = arr->size;
87 pointer i;
88
89 for (i = 0; i < size; ++i)
8893239d 90 {
5b77de89
RH
91 if (arr->data[i])
92 free (arr->data[i][-1]);
8893239d 93 }
5b77de89 94
8893239d
RH
95 free (ptr);
96}
97
98static void
99emutls_init (void)
100{
101#ifndef __GTHREAD_MUTEX_INIT
102 __GTHREAD_MUTEX_INIT_FUNCTION (&emutls_mutex);
103#endif
104 if (__gthread_key_create (&emutls_key, emutls_destroy) != 0)
105 abort ();
106}
107#endif
108
109static void *
110emutls_alloc (struct __emutls_object *obj)
111{
112 void *ptr;
113 void *ret;
114
115 /* We could use here posix_memalign if available and adjust
116 emutls_destroy accordingly. */
117 if (obj->align <= sizeof (void *))
118 {
119 ptr = malloc (obj->size + sizeof (void *));
120 if (ptr == NULL)
121 abort ();
122 ((void **) ptr)[0] = ptr;
123 ret = ptr + sizeof (void *);
124 }
125 else
126 {
127 ptr = malloc (obj->size + sizeof (void *) + obj->align - 1);
128 if (ptr == NULL)
129 abort ();
130 ret = (void *) (((pointer) (ptr + sizeof (void *) + obj->align - 1))
131 & ~(pointer)(obj->align - 1));
132 ((void **) ret)[-1] = ptr;
133 }
134
135 if (obj->templ)
136 memcpy (ret, obj->templ, obj->size);
137 else
138 memset (ret, 0, obj->size);
139
140 return ret;
141}
142
b9873b4e
IS
143/* Despite applying the attribute to the declaration, in this case the mis-
144 match between the builtin's declaration [void * (*)(void *)] and the
145 implementation here, causes the decl. attributes to be discarded. */
146
147EMUTLS_ATTR void *
8893239d
RH
148__emutls_get_address (struct __emutls_object *obj)
149{
150 if (! __gthread_active_p ())
151 {
152 if (__builtin_expect (obj->loc.ptr == NULL, 0))
153 obj->loc.ptr = emutls_alloc (obj);
154 return obj->loc.ptr;
155 }
156
157#ifndef __GTHREADS
158 abort ();
159#else
f4919e4a 160 pointer offset = __atomic_load_n (&obj->loc.offset, __ATOMIC_ACQUIRE);
8893239d 161
5b77de89 162 if (__builtin_expect (offset == 0, 0))
8893239d
RH
163 {
164 static __gthread_once_t once = __GTHREAD_ONCE_INIT;
165 __gthread_once (&once, emutls_init);
166 __gthread_mutex_lock (&emutls_mutex);
b6b561e4
JDA
167 offset = obj->loc.offset;
168 if (offset == 0)
169 {
170 offset = ++emutls_size;
f4919e4a 171 __atomic_store_n (&obj->loc.offset, offset, __ATOMIC_RELEASE);
b6b561e4 172 }
8893239d
RH
173 __gthread_mutex_unlock (&emutls_mutex);
174 }
8893239d 175
5b77de89 176 struct __emutls_array *arr = __gthread_getspecific (emutls_key);
8893239d
RH
177 if (__builtin_expect (arr == NULL, 0))
178 {
179 pointer size = offset + 32;
df0026a7 180 arr = calloc (size + 1, sizeof (void *));
8893239d
RH
181 if (arr == NULL)
182 abort ();
5b77de89 183 arr->size = size;
8893239d
RH
184 __gthread_setspecific (emutls_key, (void *) arr);
185 }
df0026a7 186 else if (__builtin_expect (offset > arr->size, 0))
8893239d 187 {
5b77de89 188 pointer orig_size = arr->size;
8893239d 189 pointer size = orig_size * 2;
df0026a7 190 if (offset > size)
8893239d 191 size = offset + 32;
df0026a7 192 arr = realloc (arr, (size + 1) * sizeof (void *));
8893239d
RH
193 if (arr == NULL)
194 abort ();
5b77de89 195 arr->size = size;
df0026a7 196 memset (arr->data + orig_size, 0,
5b77de89 197 (size - orig_size) * sizeof (void *));
8893239d
RH
198 __gthread_setspecific (emutls_key, (void *) arr);
199 }
200
5b77de89 201 void *ret = arr->data[offset - 1];
8893239d
RH
202 if (__builtin_expect (ret == NULL, 0))
203 {
204 ret = emutls_alloc (obj);
5b77de89 205 arr->data[offset - 1] = ret;
8893239d
RH
206 }
207 return ret;
208#endif
209}
210
b9873b4e 211EMUTLS_ATTR void
8893239d
RH
212__emutls_register_common (struct __emutls_object *obj,
213 word size, word align, void *templ)
214{
215 if (obj->size < size)
216 {
217 obj->size = size;
218 obj->templ = NULL;
219 }
220 if (obj->align < align)
221 obj->align = align;
222 if (templ && size == obj->size)
223 obj->templ = templ;
224}