]> git.ipfire.org Git - thirdparty/glibc.git/blame - elf/dl-fptr.c
Update copyright notices with scripts/update-copyrights
[thirdparty/glibc.git] / elf / dl-fptr.c
CommitLineData
b6ab06ce 1/* Manage function descriptors. Generic version.
d4697bc9 2 Copyright (C) 1999-2014 Free Software Foundation, Inc.
b6ab06ce
UD
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
59ba27a6
PE
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
b6ab06ce
UD
18
19#include <libintl.h>
20#include <unistd.h>
21#include <string.h>
22#include <sys/param.h>
23#include <sys/mman.h>
24#include <link.h>
25#include <ldsodefs.h>
26#include <elf/dynamic-link.h>
27#include <dl-fptr.h>
28#include <atomic.h>
29
30#ifndef ELF_MACHINE_BOOT_FPTR_TABLE_LEN
31/* ELF_MACHINE_BOOT_FPTR_TABLE_LEN should be greater than the number of
32 dynamic symbols in ld.so. */
33# define ELF_MACHINE_BOOT_FPTR_TABLE_LEN 256
34#endif
35
36#ifndef ELF_MACHINE_LOAD_ADDRESS
37# error "ELF_MACHINE_LOAD_ADDRESS is not defined."
38#endif
39
40#ifndef COMPARE_AND_SWAP
41# define COMPARE_AND_SWAP(ptr, old, new) \
11bf311e 42 (catomic_compare_and_exchange_bool_acq (ptr, new, old) == 0)
b6ab06ce
UD
43#endif
44
45ElfW(Addr) _dl_boot_fptr_table [ELF_MACHINE_BOOT_FPTR_TABLE_LEN];
46
47static struct local
48 {
49 struct fdesc_table *root;
50 struct fdesc *free_list;
51 unsigned int npages; /* # of pages to allocate */
52 /* the next to members MUST be consecutive! */
53 struct fdesc_table boot_table;
54 struct fdesc boot_fdescs[1024];
55 }
56local =
57 {
58 .root = &local.boot_table,
59 .npages = 2,
60 .boot_table =
61 {
62 .len = sizeof (local.boot_fdescs) / sizeof (local.boot_fdescs[0]),
63 .first_unused = 0
64 }
65 };
66
67/* Create a new fdesc table and return a pointer to the first fdesc
68 entry. The fdesc lock must have been acquired already. */
69
70static struct fdesc_table *
71new_fdesc_table (struct local *l, size_t *size)
72{
73 size_t old_npages = l->npages;
74 size_t new_npages = old_npages + old_npages;
75 struct fdesc_table *new_table;
76
77 /* If someone has just created a new table, we return NULL to tell
78 the caller to use the new table. */
79 if (! COMPARE_AND_SWAP (&l->npages, old_npages, new_npages))
80 return (struct fdesc_table *) NULL;
81
82 *size = old_npages * GLRO(dl_pagesize);
83 new_table = __mmap (NULL, *size,
84 PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0);
85 if (new_table == MAP_FAILED)
86 _dl_signal_error (errno, NULL, NULL,
87 N_("cannot map pages for fdesc table"));
88
89 new_table->len
90 = (*size - sizeof (*new_table)) / sizeof (struct fdesc);
91 new_table->first_unused = 1;
92 return new_table;
93}
94
95
96static ElfW(Addr)
97make_fdesc (ElfW(Addr) ip, ElfW(Addr) gp)
98{
99 struct fdesc *fdesc = NULL;
100 struct fdesc_table *root;
101 unsigned int old;
102 struct local *l;
103
104 ELF_MACHINE_LOAD_ADDRESS (l, local);
105
106 retry:
107 root = l->root;
108 while (1)
109 {
110 old = root->first_unused;
111 if (old >= root->len)
112 break;
113 else if (COMPARE_AND_SWAP (&root->first_unused, old, old + 1))
114 {
115 fdesc = &root->fdesc[old];
116 goto install;
117 }
118 }
119
120 if (l->free_list)
121 {
122 /* Get it from free-list. */
123 do
124 {
125 fdesc = l->free_list;
126 if (fdesc == NULL)
127 goto retry;
128 }
129 while (! COMPARE_AND_SWAP ((ElfW(Addr) *) &l->free_list,
130 (ElfW(Addr)) fdesc, fdesc->ip));
131 }
132 else
133 {
134 /* Create a new fdesc table. */
135 size_t size;
136 struct fdesc_table *new_table = new_fdesc_table (l, &size);
137
138 if (new_table == NULL)
139 goto retry;
140
141 new_table->next = root;
142 if (! COMPARE_AND_SWAP ((ElfW(Addr) *) &l->root,
143 (ElfW(Addr)) root,
144 (ElfW(Addr)) new_table))
145 {
146 /* Someone has just installed a new table. Return NULL to
147 tell the caller to use the new table. */
148 __munmap (new_table, size);
149 goto retry;
150 }
151
152 /* Note that the first entry was reserved while allocating the
153 memory for the new page. */
154 fdesc = &new_table->fdesc[0];
155 }
156
157 install:
158 fdesc->ip = ip;
159 fdesc->gp = gp;
160
161 return (ElfW(Addr)) fdesc;
162}
163
164
165static inline ElfW(Addr) * __attribute__ ((always_inline))
166make_fptr_table (struct link_map *map)
167{
168 const ElfW(Sym) *symtab
169 = (const void *) D_PTR (map, l_info[DT_SYMTAB]);
170 const char *strtab = (const void *) D_PTR (map, l_info[DT_STRTAB]);
171 ElfW(Addr) *fptr_table;
172 size_t size;
173 size_t len;
174
175 /* XXX Apparently the only way to find out the size of the dynamic
176 symbol section is to assume that the string table follows right
177 afterwards... */
178 len = ((strtab - (char *) symtab)
179 / map->l_info[DT_SYMENT]->d_un.d_val);
180 size = ((len * sizeof (fptr_table[0]) + GLRO(dl_pagesize) - 1)
181 & -GLRO(dl_pagesize));
182 /* XXX We don't support here in the moment systems without MAP_ANON.
183 There probably are none for IA-64. In case this is proven wrong
184 we will have to open /dev/null here and use the file descriptor
185 instead of the hard-coded -1. */
186 fptr_table = __mmap (NULL, size,
187 PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE,
188 -1, 0);
189 if (fptr_table == MAP_FAILED)
190 _dl_signal_error (errno, NULL, NULL,
191 N_("cannot map pages for fptr table"));
192
193 if (COMPARE_AND_SWAP ((ElfW(Addr) *) &map->l_mach.fptr_table,
194 (ElfW(Addr)) NULL, (ElfW(Addr)) fptr_table))
195 map->l_mach.fptr_table_len = len;
196 else
197 __munmap (fptr_table, len * sizeof (fptr_table[0]));
198
199 return map->l_mach.fptr_table;
200}
201
202
203ElfW(Addr)
204_dl_make_fptr (struct link_map *map, const ElfW(Sym) *sym,
205 ElfW(Addr) ip)
206{
207 ElfW(Addr) *ftab = map->l_mach.fptr_table;
208 const ElfW(Sym) *symtab;
209 Elf_Symndx symidx;
210 struct local *l;
211
212 if (__builtin_expect (ftab == NULL, 0))
213 ftab = make_fptr_table (map);
214
215 symtab = (const void *) D_PTR (map, l_info[DT_SYMTAB]);
216 symidx = sym - symtab;
217
218 if (symidx >= map->l_mach.fptr_table_len)
219 _dl_signal_error (0, NULL, NULL,
220 N_("internal error: symidx out of range of fptr table"));
221
222 while (ftab[symidx] == 0)
223 {
224 /* GOT has already been relocated in elf_get_dynamic_info -
225 don't try to relocate it again. */
226 ElfW(Addr) fdesc
227 = make_fdesc (ip, map->l_info[DT_PLTGOT]->d_un.d_ptr);
228
229 if (__builtin_expect (COMPARE_AND_SWAP (&ftab[symidx], (ElfW(Addr)) NULL,
230 fdesc), 1))
231 {
232 /* Noone has updated the entry and the new function
233 descriptor has been installed. */
234#if 0
235 const char *strtab
236 = (const void *) D_PTR (map, l_info[DT_STRTAB]);
237
238 ELF_MACHINE_LOAD_ADDRESS (l, local);
239 if (l->root != &l->boot_table
240 || l->boot_table.first_unused > 20)
241 _dl_debug_printf ("created fdesc symbol `%s' at %lx\n",
242 strtab + sym->st_name, ftab[symidx]);
243#endif
244 break;
245 }
246 else
247 {
248 /* We created a duplicated function descriptor. We put it on
249 free-list. */
250 struct fdesc *f = (struct fdesc *) fdesc;
251
252 ELF_MACHINE_LOAD_ADDRESS (l, local);
253
254 do
255 f->ip = (ElfW(Addr)) l->free_list;
256 while (! COMPARE_AND_SWAP ((ElfW(Addr) *) &l->free_list,
257 f->ip, fdesc));
258 }
259 }
260
261 return ftab[symidx];
262}
263
264
265void
266_dl_unmap (struct link_map *map)
267{
268 ElfW(Addr) *ftab = map->l_mach.fptr_table;
269 struct fdesc *head = NULL, *tail = NULL;
270 size_t i;
271
272 __munmap ((void *) map->l_map_start,
273 map->l_map_end - map->l_map_start);
274
275 if (ftab == NULL)
276 return;
277
278 /* String together the fdesc structures that are being freed. */
279 for (i = 0; i < map->l_mach.fptr_table_len; ++i)
280 {
281 if (ftab[i])
282 {
283 *(struct fdesc **) ftab[i] = head;
284 head = (struct fdesc *) ftab[i];
285 if (tail == NULL)
286 tail = head;
287 }
288 }
289
290 /* Prepend the new list to the free_list: */
291 if (tail)
292 do
293 tail->ip = (ElfW(Addr)) local.free_list;
294 while (! COMPARE_AND_SWAP ((ElfW(Addr) *) &local.free_list,
295 tail->ip, (ElfW(Addr)) head));
296
297 __munmap (ftab, (map->l_mach.fptr_table_len
298 * sizeof (map->l_mach.fptr_table[0])));
299
300 map->l_mach.fptr_table = NULL;
301}
302
303
304ElfW(Addr)
305_dl_lookup_address (const void *address)
306{
307 ElfW(Addr) addr = (ElfW(Addr)) address;
308 struct fdesc_table *t;
309 unsigned long int i;
310
311 for (t = local.root; t != NULL; t = t->next)
312 {
313 i = (struct fdesc *) addr - &t->fdesc[0];
314 if (i < t->first_unused && addr == (ElfW(Addr)) &t->fdesc[i])
315 {
316 addr = t->fdesc[i].ip;
317 break;
318 }
319 }
320
321 return addr;
322}