Add Changelog ...
[glibc.git] / sysdeps / hppa / dl-fptr.c
blob82b25b5422c5bae6e34f12ebe71f4e86137ea3b6
1 /* Manage function descriptors. Generic version.
2 Copyright (C) 1999-2012 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <libintl.h>
21 #include <unistd.h>
22 #include <string.h>
23 #include <sys/param.h>
24 #include <sys/mman.h>
25 #include <link.h>
26 #include <ldsodefs.h>
27 #include <elf/dynamic-link.h>
28 #include <dl-fptr.h>
29 #include <atomic.h>
31 #ifndef ELF_MACHINE_BOOT_FPTR_TABLE_LEN
32 /* ELF_MACHINE_BOOT_FPTR_TABLE_LEN should be greater than the number of
33 dynamic symbols in ld.so. */
34 # define ELF_MACHINE_BOOT_FPTR_TABLE_LEN 256
35 #endif
37 #ifndef ELF_MACHINE_LOAD_ADDRESS
38 # error "ELF_MACHINE_LOAD_ADDRESS is not defined."
39 #endif
41 #ifndef COMPARE_AND_SWAP
42 # define COMPARE_AND_SWAP(ptr, old, new) \
43 (catomic_compare_and_exchange_bool_acq (ptr, new, old) == 0)
44 #endif
46 ElfW(Addr) _dl_boot_fptr_table [ELF_MACHINE_BOOT_FPTR_TABLE_LEN];
48 static struct local
50 struct fdesc_table *root;
51 struct fdesc *free_list;
52 unsigned int npages; /* # of pages to allocate */
53 /* the next to members MUST be consecutive! */
54 struct fdesc_table boot_table;
55 struct fdesc boot_fdescs[1024];
57 local =
59 #ifdef SHARED
60 /* Address of .boot_table is not known until runtime. */
61 .root = 0,
62 #else
63 .root = &local.boot_table,
64 #endif
65 .npages = 2,
66 .boot_table =
68 .len = sizeof (local.boot_fdescs) / sizeof (local.boot_fdescs[0]),
69 .first_unused = 0
73 /* Create a new fdesc table and return a pointer to the first fdesc
74 entry. The fdesc lock must have been acquired already. */
76 static struct fdesc_table *
77 new_fdesc_table (struct local *l, size_t *size)
79 size_t old_npages = l->npages;
80 size_t new_npages = old_npages + old_npages;
81 struct fdesc_table *new_table;
83 /* If someone has just created a new table, we return NULL to tell
84 the caller to use the new table. */
85 if (! COMPARE_AND_SWAP (&l->npages, old_npages, new_npages))
86 return (struct fdesc_table *) NULL;
88 *size = old_npages * GLRO(dl_pagesize);
89 new_table = __mmap (NULL, *size,
90 PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0);
91 if (new_table == MAP_FAILED)
92 _dl_signal_error (errno, NULL, NULL,
93 N_("cannot map pages for fdesc table"));
95 new_table->len
96 = (*size - sizeof (*new_table)) / sizeof (struct fdesc);
97 new_table->first_unused = 1;
98 return new_table;
101 /* Must call _dl_fptr_init before using any other function. */
102 void
103 _dl_fptr_init (void)
105 struct local *l;
107 ELF_MACHINE_LOAD_ADDRESS (l, local);
108 l->root = &l->boot_table;
111 static ElfW(Addr)
112 make_fdesc (ElfW(Addr) ip, ElfW(Addr) gp)
114 struct fdesc *fdesc = NULL;
115 struct fdesc_table *root;
116 unsigned int old;
117 struct local *l;
119 ELF_MACHINE_LOAD_ADDRESS (l, local);
121 retry:
122 root = l->root;
123 while (1)
125 old = root->first_unused;
126 if (old >= root->len)
127 break;
128 else if (COMPARE_AND_SWAP (&root->first_unused, old, old + 1))
130 fdesc = &root->fdesc[old];
131 goto install;
135 if (l->free_list)
137 /* Get it from free-list. */
140 fdesc = l->free_list;
141 if (fdesc == NULL)
142 goto retry;
144 while (! COMPARE_AND_SWAP ((ElfW(Addr) *) &l->free_list,
145 (ElfW(Addr)) fdesc, fdesc->ip));
147 else
149 /* Create a new fdesc table. */
150 size_t size;
151 struct fdesc_table *new_table = new_fdesc_table (l, &size);
153 if (new_table == NULL)
154 goto retry;
156 new_table->next = root;
157 if (! COMPARE_AND_SWAP ((ElfW(Addr) *) &l->root,
158 (ElfW(Addr)) root,
159 (ElfW(Addr)) new_table))
161 /* Someone has just installed a new table. Return NULL to
162 tell the caller to use the new table. */
163 __munmap (new_table, size);
164 goto retry;
167 /* Note that the first entry was reserved while allocating the
168 memory for the new page. */
169 fdesc = &new_table->fdesc[0];
172 install:
173 fdesc->ip = ip;
174 fdesc->gp = gp;
176 return (ElfW(Addr)) fdesc;
180 static inline ElfW(Addr) * __attribute__ ((always_inline))
181 make_fptr_table (struct link_map *map)
183 const ElfW(Sym) *symtab
184 = (const void *) D_PTR (map, l_info[DT_SYMTAB]);
185 const char *strtab = (const void *) D_PTR (map, l_info[DT_STRTAB]);
186 ElfW(Addr) *fptr_table;
187 size_t size;
188 size_t len;
190 /* XXX Apparently the only way to find out the size of the dynamic
191 symbol section is to assume that the string table follows right
192 afterwards... */
193 len = ((strtab - (char *) symtab)
194 / map->l_info[DT_SYMENT]->d_un.d_val);
195 size = ((len * sizeof (fptr_table[0]) + GLRO(dl_pagesize) - 1)
196 & -GLRO(dl_pagesize));
197 /* XXX We don't support here in the moment systems without MAP_ANON.
198 There probably are none for IA-64. In case this is proven wrong
199 we will have to open /dev/null here and use the file descriptor
200 instead of the hard-coded -1. */
201 fptr_table = __mmap (NULL, size,
202 PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE,
203 -1, 0);
204 if (fptr_table == MAP_FAILED)
205 _dl_signal_error (errno, NULL, NULL,
206 N_("cannot map pages for fptr table"));
208 if (COMPARE_AND_SWAP ((ElfW(Addr) *) &map->l_mach.fptr_table,
209 (ElfW(Addr)) NULL, (ElfW(Addr)) fptr_table))
210 map->l_mach.fptr_table_len = len;
211 else
212 __munmap (fptr_table, len * sizeof (fptr_table[0]));
214 return map->l_mach.fptr_table;
218 ElfW(Addr)
219 _dl_make_fptr (struct link_map *map, const ElfW(Sym) *sym,
220 ElfW(Addr) ip)
222 ElfW(Addr) *ftab = map->l_mach.fptr_table;
223 const ElfW(Sym) *symtab;
224 Elf_Symndx symidx;
225 struct local *l;
227 if (__builtin_expect (ftab == NULL, 0))
228 ftab = make_fptr_table (map);
230 symtab = (const void *) D_PTR (map, l_info[DT_SYMTAB]);
231 symidx = sym - symtab;
233 if (symidx >= map->l_mach.fptr_table_len)
234 _dl_signal_error (0, NULL, NULL,
235 N_("internal error: symidx out of range of fptr table"));
237 while (ftab[symidx] == 0)
239 /* GOT has already been relocated in elf_get_dynamic_info -
240 don't try to relocate it again. */
241 ElfW(Addr) fdesc
242 = make_fdesc (ip, map->l_info[DT_PLTGOT]->d_un.d_ptr);
244 if (__builtin_expect (COMPARE_AND_SWAP (&ftab[symidx], (ElfW(Addr)) NULL,
245 fdesc), 1))
247 /* Noone has updated the entry and the new function
248 descriptor has been installed. */
249 #if 0
250 const char *strtab
251 = (const void *) D_PTR (map, l_info[DT_STRTAB]);
253 ELF_MACHINE_LOAD_ADDRESS (l, local);
254 if (l->root != &l->boot_table
255 || l->boot_table.first_unused > 20)
256 _dl_debug_printf ("created fdesc symbol `%s' at %lx\n",
257 strtab + sym->st_name, ftab[symidx]);
258 #endif
259 break;
261 else
263 /* We created a duplicated function descriptor. We put it on
264 free-list. */
265 struct fdesc *f = (struct fdesc *) fdesc;
267 ELF_MACHINE_LOAD_ADDRESS (l, local);
270 f->ip = (ElfW(Addr)) l->free_list;
271 while (! COMPARE_AND_SWAP ((ElfW(Addr) *) &l->free_list,
272 f->ip, fdesc));
276 return ftab[symidx];
280 void
281 _dl_unmap (struct link_map *map)
283 ElfW(Addr) *ftab = map->l_mach.fptr_table;
284 struct fdesc *head = NULL, *tail = NULL;
285 size_t i;
287 __munmap ((void *) map->l_map_start,
288 map->l_map_end - map->l_map_start);
290 if (ftab == NULL)
291 return;
293 /* String together the fdesc structures that are being freed. */
294 for (i = 0; i < map->l_mach.fptr_table_len; ++i)
296 if (ftab[i])
298 *(struct fdesc **) ftab[i] = head;
299 head = (struct fdesc *) ftab[i];
300 if (tail == NULL)
301 tail = head;
305 /* Prepend the new list to the free_list: */
306 if (tail)
308 tail->ip = (ElfW(Addr)) local.free_list;
309 while (! COMPARE_AND_SWAP ((ElfW(Addr) *) &local.free_list,
310 tail->ip, (ElfW(Addr)) head));
312 __munmap (ftab, (map->l_mach.fptr_table_len
313 * sizeof (map->l_mach.fptr_table[0])));
315 map->l_mach.fptr_table = NULL;
319 ElfW(Addr)
320 _dl_lookup_address (const void *address)
322 ElfW(Addr) addr = (ElfW(Addr)) address;
323 struct fdesc_table *t;
324 unsigned long int i;
326 for (t = local.root; t != NULL; t = t->next)
328 i = (struct fdesc *) addr - &t->fdesc[0];
329 if (i < t->first_unused && addr == (ElfW(Addr)) &t->fdesc[i])
331 addr = t->fdesc[i].ip;
332 break;
336 return addr;