Fix grouping and reuse other locales in various locales
[glibc.git] / elf / dl-runtime.c
blob8778e67f7cfa4146551de9a36ad83b6d7c59fd5c
1 /* On-demand PLT fixup for shared objects.
2 Copyright (C) 1995-2009, 2010, 2011 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 #define IN_DL_RUNTIME 1 /* This can be tested in dl-machine.h. */
22 #include <alloca.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25 #include <sys/param.h>
26 #include <ldsodefs.h>
27 #include <sysdep-cancel.h>
28 #include "dynamic-link.h"
29 #include <tls.h>
30 #include <dl-irel.h>
33 #if (!defined ELF_MACHINE_NO_RELA && !defined ELF_MACHINE_PLT_REL) \
34 || ELF_MACHINE_NO_REL
35 # define PLTREL ElfW(Rela)
36 #else
37 # define PLTREL ElfW(Rel)
38 #endif
40 #ifndef VERSYMIDX
41 # define VERSYMIDX(sym) (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGIDX (sym))
42 #endif
44 /* The fixup functions might have need special attributes. If none
45 are provided define the macro as empty. */
46 #ifndef ARCH_FIXUP_ATTRIBUTE
47 # define ARCH_FIXUP_ATTRIBUTE
48 #endif
50 #ifndef reloc_offset
51 # define reloc_offset reloc_arg
52 # define reloc_index reloc_arg / sizeof (PLTREL)
53 #endif
57 /* This function is called through a special trampoline from the PLT the
58 first time each PLT entry is called. We must perform the relocation
59 specified in the PLT of the given shared object, and return the resolved
60 function address to the trampoline, which will restart the original call
61 to that address. Future calls will bounce directly from the PLT to the
62 function. */
64 #ifndef ELF_MACHINE_NO_PLT
65 DL_FIXUP_VALUE_TYPE
66 __attribute ((noinline)) ARCH_FIXUP_ATTRIBUTE
67 _dl_fixup (
68 # ifdef ELF_MACHINE_RUNTIME_FIXUP_ARGS
69 ELF_MACHINE_RUNTIME_FIXUP_ARGS,
70 # endif
71 /* GKM FIXME: Fix trampoline to pass bounds so we can do
72 without the `__unbounded' qualifier. */
73 struct link_map *__unbounded l, ElfW(Word) reloc_arg)
75 const ElfW(Sym) *const symtab
76 = (const void *) D_PTR (l, l_info[DT_SYMTAB]);
77 const char *strtab = (const void *) D_PTR (l, l_info[DT_STRTAB]);
79 const PLTREL *const reloc
80 = (const void *) (D_PTR (l, l_info[DT_JMPREL]) + reloc_offset);
81 const ElfW(Sym) *sym = &symtab[ELFW(R_SYM) (reloc->r_info)];
82 void *const rel_addr = (void *)(l->l_addr + reloc->r_offset);
83 lookup_t result;
84 DL_FIXUP_VALUE_TYPE value;
86 /* Sanity check that we're really looking at a PLT relocation. */
87 assert (ELFW(R_TYPE)(reloc->r_info) == ELF_MACHINE_JMP_SLOT);
89 /* Look up the target symbol. If the normal lookup rules are not
90 used don't look in the global scope. */
91 if (__builtin_expect (ELFW(ST_VISIBILITY) (sym->st_other), 0) == 0)
93 const struct r_found_version *version = NULL;
95 if (l->l_info[VERSYMIDX (DT_VERSYM)] != NULL)
97 const ElfW(Half) *vernum =
98 (const void *) D_PTR (l, l_info[VERSYMIDX (DT_VERSYM)]);
99 ElfW(Half) ndx = vernum[ELFW(R_SYM) (reloc->r_info)] & 0x7fff;
100 version = &l->l_versions[ndx];
101 if (version->hash == 0)
102 version = NULL;
105 /* We need to keep the scope around so do some locking. This is
106 not necessary for objects which cannot be unloaded or when
107 we are not using any threads (yet). */
108 int flags = DL_LOOKUP_ADD_DEPENDENCY;
109 if (!RTLD_SINGLE_THREAD_P)
111 THREAD_GSCOPE_SET_FLAG ();
112 flags |= DL_LOOKUP_GSCOPE_LOCK;
115 #ifdef RTLD_ENABLE_FOREIGN_CALL
116 RTLD_ENABLE_FOREIGN_CALL;
117 #endif
119 result = _dl_lookup_symbol_x (strtab + sym->st_name, l, &sym, l->l_scope,
120 version, ELF_RTYPE_CLASS_PLT, flags, NULL);
122 /* We are done with the global scope. */
123 if (!RTLD_SINGLE_THREAD_P)
124 THREAD_GSCOPE_RESET_FLAG ();
126 #ifdef RTLD_FINALIZE_FOREIGN_CALL
127 RTLD_FINALIZE_FOREIGN_CALL;
128 #endif
130 /* Currently result contains the base load address (or link map)
131 of the object that defines sym. Now add in the symbol
132 offset. */
133 value = DL_FIXUP_MAKE_VALUE (result,
134 sym ? (LOOKUP_VALUE_ADDRESS (result)
135 + sym->st_value) : 0);
137 else
139 /* We already found the symbol. The module (and therefore its load
140 address) is also known. */
141 value = DL_FIXUP_MAKE_VALUE (l, l->l_addr + sym->st_value);
142 result = l;
145 /* And now perhaps the relocation addend. */
146 value = elf_machine_plt_value (l, reloc, value);
148 if (sym != NULL
149 && __builtin_expect (ELFW(ST_TYPE) (sym->st_info) == STT_GNU_IFUNC, 0))
150 value = elf_ifunc_invoke (DL_FIXUP_VALUE_ADDR (value));
152 /* Finally, fix up the plt itself. */
153 if (__builtin_expect (GLRO(dl_bind_not), 0))
154 return value;
156 return elf_machine_fixup_plt (l, result, reloc, rel_addr, value);
158 #endif
160 #if !defined PROF && !defined ELF_MACHINE_NO_PLT && !__BOUNDED_POINTERS__
161 DL_FIXUP_VALUE_TYPE
162 __attribute ((noinline)) ARCH_FIXUP_ATTRIBUTE
163 _dl_profile_fixup (
164 #ifdef ELF_MACHINE_RUNTIME_FIXUP_ARGS
165 ELF_MACHINE_RUNTIME_FIXUP_ARGS,
166 #endif
167 struct link_map *l, ElfW(Word) reloc_arg,
168 ElfW(Addr) retaddr, void *regs, long int *framesizep)
170 void (*mcount_fct) (ElfW(Addr), ElfW(Addr)) = INTUSE(_dl_mcount);
172 /* This is the address in the array where we store the result of previous
173 relocations. */
174 struct reloc_result *reloc_result = &l->l_reloc_result[reloc_index];
175 DL_FIXUP_VALUE_TYPE *resultp = &reloc_result->addr;
177 DL_FIXUP_VALUE_TYPE value = *resultp;
178 if (DL_FIXUP_VALUE_CODE_ADDR (value) == 0)
180 /* This is the first time we have to relocate this object. */
181 const ElfW(Sym) *const symtab
182 = (const void *) D_PTR (l, l_info[DT_SYMTAB]);
183 const char *strtab = (const char *) D_PTR (l, l_info[DT_STRTAB]);
185 const PLTREL *const reloc
186 = (const void *) (D_PTR (l, l_info[DT_JMPREL]) + reloc_offset);
187 const ElfW(Sym) *refsym = &symtab[ELFW(R_SYM) (reloc->r_info)];
188 const ElfW(Sym) *defsym = refsym;
189 lookup_t result;
191 /* Sanity check that we're really looking at a PLT relocation. */
192 assert (ELFW(R_TYPE)(reloc->r_info) == ELF_MACHINE_JMP_SLOT);
194 /* Look up the target symbol. If the symbol is marked STV_PROTECTED
195 don't look in the global scope. */
196 if (__builtin_expect (ELFW(ST_VISIBILITY) (refsym->st_other), 0) == 0)
198 const struct r_found_version *version = NULL;
200 if (l->l_info[VERSYMIDX (DT_VERSYM)] != NULL)
202 const ElfW(Half) *vernum =
203 (const void *) D_PTR (l, l_info[VERSYMIDX (DT_VERSYM)]);
204 ElfW(Half) ndx = vernum[ELFW(R_SYM) (reloc->r_info)] & 0x7fff;
205 version = &l->l_versions[ndx];
206 if (version->hash == 0)
207 version = NULL;
210 /* We need to keep the scope around so do some locking. This is
211 not necessary for objects which cannot be unloaded or when
212 we are not using any threads (yet). */
213 int flags = DL_LOOKUP_ADD_DEPENDENCY;
214 if (!RTLD_SINGLE_THREAD_P)
216 THREAD_GSCOPE_SET_FLAG ();
217 flags |= DL_LOOKUP_GSCOPE_LOCK;
220 result = _dl_lookup_symbol_x (strtab + refsym->st_name, l,
221 &defsym, l->l_scope, version,
222 ELF_RTYPE_CLASS_PLT, flags, NULL);
224 /* We are done with the global scope. */
225 if (!RTLD_SINGLE_THREAD_P)
226 THREAD_GSCOPE_RESET_FLAG ();
228 /* Currently result contains the base load address (or link map)
229 of the object that defines sym. Now add in the symbol
230 offset. */
231 value = DL_FIXUP_MAKE_VALUE (result,
232 defsym != NULL
233 ? LOOKUP_VALUE_ADDRESS (result)
234 + defsym->st_value : 0);
236 if (defsym != NULL
237 && __builtin_expect (ELFW(ST_TYPE) (defsym->st_info)
238 == STT_GNU_IFUNC, 0))
239 value = elf_ifunc_invoke (DL_FIXUP_VALUE_ADDR (value));
241 else
243 /* We already found the symbol. The module (and therefore its load
244 address) is also known. */
245 value = DL_FIXUP_MAKE_VALUE (l, l->l_addr + refsym->st_value);
247 if (__builtin_expect (ELFW(ST_TYPE) (refsym->st_info)
248 == STT_GNU_IFUNC, 0))
249 value = elf_ifunc_invoke (DL_FIXUP_VALUE_ADDR (value));
251 result = l;
253 /* And now perhaps the relocation addend. */
254 value = elf_machine_plt_value (l, reloc, value);
256 #ifdef SHARED
257 /* Auditing checkpoint: we have a new binding. Provide the
258 auditing libraries the possibility to change the value and
259 tell us whether further auditing is wanted. */
260 if (defsym != NULL && GLRO(dl_naudit) > 0)
262 reloc_result->bound = result;
263 /* Compute index of the symbol entry in the symbol table of
264 the DSO with the definition. */
265 reloc_result->boundndx = (defsym
266 - (ElfW(Sym) *) D_PTR (result,
267 l_info[DT_SYMTAB]));
269 /* Determine whether any of the two participating DSOs is
270 interested in auditing. */
271 if ((l->l_audit_any_plt | result->l_audit_any_plt) != 0)
273 unsigned int flags = 0;
274 struct audit_ifaces *afct = GLRO(dl_audit);
275 /* Synthesize a symbol record where the st_value field is
276 the result. */
277 ElfW(Sym) sym = *defsym;
278 sym.st_value = DL_FIXUP_VALUE_ADDR (value);
280 /* Keep track whether there is any interest in tracing
281 the call in the lower two bits. */
282 assert (DL_NNS * 2 <= sizeof (reloc_result->flags) * 8);
283 assert ((LA_SYMB_NOPLTENTER | LA_SYMB_NOPLTEXIT) == 3);
284 reloc_result->enterexit = LA_SYMB_NOPLTENTER | LA_SYMB_NOPLTEXIT;
286 const char *strtab2 = (const void *) D_PTR (result,
287 l_info[DT_STRTAB]);
289 for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
291 /* XXX Check whether both DSOs must request action or
292 only one */
293 if ((l->l_audit[cnt].bindflags & LA_FLG_BINDFROM) != 0
294 && (result->l_audit[cnt].bindflags & LA_FLG_BINDTO) != 0)
296 if (afct->symbind != NULL)
298 uintptr_t new_value
299 = afct->symbind (&sym, reloc_result->boundndx,
300 &l->l_audit[cnt].cookie,
301 &result->l_audit[cnt].cookie,
302 &flags,
303 strtab2 + defsym->st_name);
304 if (new_value != (uintptr_t) sym.st_value)
306 flags |= LA_SYMB_ALTVALUE;
307 sym.st_value = new_value;
311 /* Remember the results for every audit library and
312 store a summary in the first two bits. */
313 reloc_result->enterexit
314 &= flags & (LA_SYMB_NOPLTENTER | LA_SYMB_NOPLTEXIT);
315 reloc_result->enterexit
316 |= ((flags & (LA_SYMB_NOPLTENTER | LA_SYMB_NOPLTEXIT))
317 << ((cnt + 1) * 2));
319 else
320 /* If the bind flags say this auditor is not interested,
321 set the bits manually. */
322 reloc_result->enterexit
323 |= ((LA_SYMB_NOPLTENTER | LA_SYMB_NOPLTEXIT)
324 << ((cnt + 1) * 2));
326 afct = afct->next;
329 reloc_result->flags = flags;
330 value = DL_FIXUP_ADDR_VALUE (sym.st_value);
332 else
333 /* Set all bits since this symbol binding is not interesting. */
334 reloc_result->enterexit = (1u << DL_NNS) - 1;
336 #endif
338 /* Store the result for later runs. */
339 if (__builtin_expect (! GLRO(dl_bind_not), 1))
340 *resultp = value;
343 /* By default we do not call the pltexit function. */
344 long int framesize = -1;
346 #ifdef SHARED
347 /* Auditing checkpoint: report the PLT entering and allow the
348 auditors to change the value. */
349 if (DL_FIXUP_VALUE_CODE_ADDR (value) != 0 && GLRO(dl_naudit) > 0
350 /* Don't do anything if no auditor wants to intercept this call. */
351 && (reloc_result->enterexit & LA_SYMB_NOPLTENTER) == 0)
353 ElfW(Sym) *defsym = ((ElfW(Sym) *) D_PTR (reloc_result->bound,
354 l_info[DT_SYMTAB])
355 + reloc_result->boundndx);
357 /* Set up the sym parameter. */
358 ElfW(Sym) sym = *defsym;
359 sym.st_value = DL_FIXUP_VALUE_ADDR (value);
361 /* Get the symbol name. */
362 const char *strtab = (const void *) D_PTR (reloc_result->bound,
363 l_info[DT_STRTAB]);
364 const char *symname = strtab + sym.st_name;
366 /* Keep track of overwritten addresses. */
367 unsigned int flags = reloc_result->flags;
369 struct audit_ifaces *afct = GLRO(dl_audit);
370 for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
372 if (afct->ARCH_LA_PLTENTER != NULL
373 && (reloc_result->enterexit
374 & (LA_SYMB_NOPLTENTER << (2 * (cnt + 1)))) == 0)
376 long int new_framesize = -1;
377 uintptr_t new_value
378 = afct->ARCH_LA_PLTENTER (&sym, reloc_result->boundndx,
379 &l->l_audit[cnt].cookie,
380 &reloc_result->bound->l_audit[cnt].cookie,
381 regs, &flags, symname,
382 &new_framesize);
383 if (new_value != (uintptr_t) sym.st_value)
385 flags |= LA_SYMB_ALTVALUE;
386 sym.st_value = new_value;
389 /* Remember the results for every audit library and
390 store a summary in the first two bits. */
391 reloc_result->enterexit
392 |= ((flags & (LA_SYMB_NOPLTENTER | LA_SYMB_NOPLTEXIT))
393 << (2 * (cnt + 1)));
395 if ((reloc_result->enterexit & (LA_SYMB_NOPLTEXIT
396 << (2 * (cnt + 1))))
397 == 0 && new_framesize != -1 && framesize != -2)
399 /* If this is the first call providing information,
400 use it. */
401 if (framesize == -1)
402 framesize = new_framesize;
403 /* If two pltenter calls provide conflicting information,
404 use the larger value. */
405 else if (new_framesize != framesize)
406 framesize = MAX (new_framesize, framesize);
410 afct = afct->next;
413 value = DL_FIXUP_ADDR_VALUE (sym.st_value);
415 #endif
417 /* Store the frame size information. */
418 *framesizep = framesize;
420 (*mcount_fct) (retaddr, DL_FIXUP_VALUE_CODE_ADDR (value));
422 return value;
425 #endif /* PROF && ELF_MACHINE_NO_PLT */
428 #include <stdio.h>
429 void
430 ARCH_FIXUP_ATTRIBUTE
431 _dl_call_pltexit (struct link_map *l, ElfW(Word) reloc_arg,
432 const void *inregs, void *outregs)
434 #ifdef SHARED
435 /* This is the address in the array where we store the result of previous
436 relocations. */
437 // XXX Maybe the bound information must be stored on the stack since
438 // XXX with bind_not a new value could have been stored in the meantime.
439 struct reloc_result *reloc_result = &l->l_reloc_result[reloc_index];
440 ElfW(Sym) *defsym = ((ElfW(Sym) *) D_PTR (reloc_result->bound,
441 l_info[DT_SYMTAB])
442 + reloc_result->boundndx);
444 /* Set up the sym parameter. */
445 ElfW(Sym) sym = *defsym;
446 sym.st_value = DL_FIXUP_VALUE_ADDR (reloc_result->addr);
448 /* Get the symbol name. */
449 const char *strtab = (const void *) D_PTR (reloc_result->bound,
450 l_info[DT_STRTAB]);
451 const char *symname = strtab + sym.st_name;
453 struct audit_ifaces *afct = GLRO(dl_audit);
454 for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
456 if (afct->ARCH_LA_PLTEXIT != NULL
457 && (reloc_result->enterexit
458 & (LA_SYMB_NOPLTEXIT >> (2 * cnt))) == 0)
460 afct->ARCH_LA_PLTEXIT (&sym, reloc_result->boundndx,
461 &l->l_audit[cnt].cookie,
462 &reloc_result->bound->l_audit[cnt].cookie,
463 inregs, outregs, symname);
466 afct = afct->next;
468 #endif