Fix unwind info in x86 memcmp-ssse3.
[glibc.git] / elf / dl-runtime.c
bloba52120d1217bbe7f86eb576e1cd9896d879f0524
1 /* On-demand PLT fixup for shared objects.
2 Copyright (C) 1995-2006, 2007, 2008, 2009 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>
32 #if (!defined ELF_MACHINE_NO_RELA && !defined ELF_MACHINE_PLT_REL) \
33 || ELF_MACHINE_NO_REL
34 # define PLTREL ElfW(Rela)
35 #else
36 # define PLTREL ElfW(Rel)
37 #endif
39 #ifndef VERSYMIDX
40 # define VERSYMIDX(sym) (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGIDX (sym))
41 #endif
43 /* The fixup functions might have need special attributes. If none
44 are provided define the macro as empty. */
45 #ifndef ARCH_FIXUP_ATTRIBUTE
46 # define ARCH_FIXUP_ATTRIBUTE
47 #endif
49 #ifndef reloc_offset
50 # define reloc_offset reloc_arg
51 # define reloc_index reloc_arg / sizeof (PLTREL)
52 #endif
56 /* This function is called through a special trampoline from the PLT the
57 first time each PLT entry is called. We must perform the relocation
58 specified in the PLT of the given shared object, and return the resolved
59 function address to the trampoline, which will restart the original call
60 to that address. Future calls will bounce directly from the PLT to the
61 function. */
63 #ifndef ELF_MACHINE_NO_PLT
64 DL_FIXUP_VALUE_TYPE
65 __attribute ((noinline)) ARCH_FIXUP_ATTRIBUTE
66 _dl_fixup (
67 # ifdef ELF_MACHINE_RUNTIME_FIXUP_ARGS
68 ELF_MACHINE_RUNTIME_FIXUP_ARGS,
69 # endif
70 /* GKM FIXME: Fix trampoline to pass bounds so we can do
71 without the `__unbounded' qualifier. */
72 struct link_map *__unbounded l, ElfW(Word) reloc_arg)
74 const ElfW(Sym) *const symtab
75 = (const void *) D_PTR (l, l_info[DT_SYMTAB]);
76 const char *strtab = (const void *) D_PTR (l, l_info[DT_STRTAB]);
78 const PLTREL *const reloc
79 = (const void *) (D_PTR (l, l_info[DT_JMPREL]) + reloc_offset);
80 const ElfW(Sym) *sym = &symtab[ELFW(R_SYM) (reloc->r_info)];
81 void *const rel_addr = (void *)(l->l_addr + reloc->r_offset);
82 lookup_t result;
83 DL_FIXUP_VALUE_TYPE value;
85 /* Sanity check that we're really looking at a PLT relocation. */
86 assert (ELFW(R_TYPE)(reloc->r_info) == ELF_MACHINE_JMP_SLOT);
88 /* Look up the target symbol. If the normal lookup rules are not
89 used don't look in the global scope. */
90 if (__builtin_expect (ELFW(ST_VISIBILITY) (sym->st_other), 0) == 0)
92 const struct r_found_version *version = NULL;
94 if (l->l_info[VERSYMIDX (DT_VERSYM)] != NULL)
96 const ElfW(Half) *vernum =
97 (const void *) D_PTR (l, l_info[VERSYMIDX (DT_VERSYM)]);
98 ElfW(Half) ndx = vernum[ELFW(R_SYM) (reloc->r_info)] & 0x7fff;
99 version = &l->l_versions[ndx];
100 if (version->hash == 0)
101 version = NULL;
104 /* We need to keep the scope around so do some locking. This is
105 not necessary for objects which cannot be unloaded or when
106 we are not using any threads (yet). */
107 int flags = DL_LOOKUP_ADD_DEPENDENCY;
108 if (!RTLD_SINGLE_THREAD_P)
110 THREAD_GSCOPE_SET_FLAG ();
111 flags |= DL_LOOKUP_GSCOPE_LOCK;
114 #ifdef RTLD_ENABLE_FOREIGN_CALL
115 RTLD_ENABLE_FOREIGN_CALL;
116 #endif
118 result = _dl_lookup_symbol_x (strtab + sym->st_name, l, &sym, l->l_scope,
119 version, ELF_RTYPE_CLASS_PLT, flags, NULL);
121 /* We are done with the global scope. */
122 if (!RTLD_SINGLE_THREAD_P)
123 THREAD_GSCOPE_RESET_FLAG ();
125 #ifdef RTLD_FINALIZE_FOREIGN_CALL
126 RTLD_FINALIZE_FOREIGN_CALL;
127 #endif
129 /* Currently result contains the base load address (or link map)
130 of the object that defines sym. Now add in the symbol
131 offset. */
132 value = DL_FIXUP_MAKE_VALUE (result,
133 sym ? (LOOKUP_VALUE_ADDRESS (result)
134 + sym->st_value) : 0);
136 else
138 /* We already found the symbol. The module (and therefore its load
139 address) is also known. */
140 value = DL_FIXUP_MAKE_VALUE (l, l->l_addr + sym->st_value);
141 result = l;
144 /* And now perhaps the relocation addend. */
145 value = elf_machine_plt_value (l, reloc, value);
147 if (__builtin_expect (ELFW(ST_TYPE) (sym->st_info) == STT_GNU_IFUNC, 0))
148 value = ((DL_FIXUP_VALUE_TYPE (*) (void)) DL_FIXUP_VALUE_ADDR (value)) ();
150 /* Finally, fix up the plt itself. */
151 if (__builtin_expect (GLRO(dl_bind_not), 0))
152 return value;
154 return elf_machine_fixup_plt (l, result, reloc, rel_addr, value);
156 #endif
158 #if !defined PROF && !defined ELF_MACHINE_NO_PLT && !__BOUNDED_POINTERS__
159 DL_FIXUP_VALUE_TYPE
160 __attribute ((noinline)) ARCH_FIXUP_ATTRIBUTE
161 _dl_profile_fixup (
162 #ifdef ELF_MACHINE_RUNTIME_FIXUP_ARGS
163 ELF_MACHINE_RUNTIME_FIXUP_ARGS,
164 #endif
165 struct link_map *l, ElfW(Word) reloc_arg,
166 ElfW(Addr) retaddr, void *regs, long int *framesizep)
168 void (*mcount_fct) (ElfW(Addr), ElfW(Addr)) = INTUSE(_dl_mcount);
170 /* This is the address in the array where we store the result of previous
171 relocations. */
172 struct reloc_result *reloc_result = &l->l_reloc_result[reloc_index];
173 DL_FIXUP_VALUE_TYPE *resultp = &reloc_result->addr;
175 DL_FIXUP_VALUE_TYPE value = *resultp;
176 if (DL_FIXUP_VALUE_CODE_ADDR (value) == 0)
178 /* This is the first time we have to relocate this object. */
179 const ElfW(Sym) *const symtab
180 = (const void *) D_PTR (l, l_info[DT_SYMTAB]);
181 const char *strtab = (const char *) D_PTR (l, l_info[DT_STRTAB]);
183 const PLTREL *const reloc
184 = (const void *) (D_PTR (l, l_info[DT_JMPREL]) + reloc_offset);
185 const ElfW(Sym) *refsym = &symtab[ELFW(R_SYM) (reloc->r_info)];
186 const ElfW(Sym) *defsym = refsym;
187 lookup_t result;
189 /* Sanity check that we're really looking at a PLT relocation. */
190 assert (ELFW(R_TYPE)(reloc->r_info) == ELF_MACHINE_JMP_SLOT);
192 /* Look up the target symbol. If the symbol is marked STV_PROTECTED
193 don't look in the global scope. */
194 if (__builtin_expect (ELFW(ST_VISIBILITY) (refsym->st_other), 0) == 0)
196 const struct r_found_version *version = NULL;
198 if (l->l_info[VERSYMIDX (DT_VERSYM)] != NULL)
200 const ElfW(Half) *vernum =
201 (const void *) D_PTR (l, l_info[VERSYMIDX (DT_VERSYM)]);
202 ElfW(Half) ndx = vernum[ELFW(R_SYM) (reloc->r_info)] & 0x7fff;
203 version = &l->l_versions[ndx];
204 if (version->hash == 0)
205 version = NULL;
208 /* We need to keep the scope around so do some locking. This is
209 not necessary for objects which cannot be unloaded or when
210 we are not using any threads (yet). */
211 int flags = DL_LOOKUP_ADD_DEPENDENCY;
212 if (!RTLD_SINGLE_THREAD_P)
214 THREAD_GSCOPE_SET_FLAG ();
215 flags |= DL_LOOKUP_GSCOPE_LOCK;
218 result = _dl_lookup_symbol_x (strtab + refsym->st_name, l,
219 &defsym, l->l_scope, version,
220 ELF_RTYPE_CLASS_PLT, flags, NULL);
222 /* We are done with the global scope. */
223 if (!RTLD_SINGLE_THREAD_P)
224 THREAD_GSCOPE_RESET_FLAG ();
226 /* Currently result contains the base load address (or link map)
227 of the object that defines sym. Now add in the symbol
228 offset. */
229 value = DL_FIXUP_MAKE_VALUE (result,
230 defsym != NULL
231 ? LOOKUP_VALUE_ADDRESS (result)
232 + defsym->st_value : 0);
234 if (__builtin_expect (ELFW(ST_TYPE) (defsym->st_info)
235 == STT_GNU_IFUNC, 0))
236 value = ((DL_FIXUP_VALUE_TYPE (*) (void))
237 DL_FIXUP_VALUE_ADDR (value)) ();
239 else
241 /* We already found the symbol. The module (and therefore its load
242 address) is also known. */
243 value = DL_FIXUP_MAKE_VALUE (l, l->l_addr + refsym->st_value);
245 if (__builtin_expect (ELFW(ST_TYPE) (refsym->st_info)
246 == STT_GNU_IFUNC, 0))
247 value = ((DL_FIXUP_VALUE_TYPE (*) (void))
248 DL_FIXUP_VALUE_ADDR (value)) ();
250 result = l;
252 /* And now perhaps the relocation addend. */
253 value = elf_machine_plt_value (l, reloc, value);
255 #ifdef SHARED
256 /* Auditing checkpoint: we have a new binding. Provide the
257 auditing libraries the possibility to change the value and
258 tell us whether further auditing is wanted. */
259 if (defsym != NULL && GLRO(dl_naudit) > 0)
261 reloc_result->bound = result;
262 /* Compute index of the symbol entry in the symbol table of
263 the DSO with the definition. */
264 reloc_result->boundndx = (defsym
265 - (ElfW(Sym) *) D_PTR (result,
266 l_info[DT_SYMTAB]));
268 /* Determine whether any of the two participating DSOs is
269 interested in auditing. */
270 if ((l->l_audit_any_plt | result->l_audit_any_plt) != 0)
272 unsigned int altvalue = 0;
273 struct audit_ifaces *afct = GLRO(dl_audit);
274 /* Synthesize a symbol record where the st_value field is
275 the result. */
276 ElfW(Sym) sym = *defsym;
277 sym.st_value = DL_FIXUP_VALUE_ADDR (value);
279 /* Keep track whether there is any interest in tracing
280 the call in the lower two bits. */
281 assert (DL_NNS * 2 <= sizeof (reloc_result->flags) * 8);
282 assert ((LA_SYMB_NOPLTENTER | LA_SYMB_NOPLTEXIT) == 3);
283 reloc_result->enterexit = LA_SYMB_NOPLTENTER | LA_SYMB_NOPLTEXIT;
285 const char *strtab2 = (const void *) D_PTR (result,
286 l_info[DT_STRTAB]);
288 for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
290 /* XXX Check whether both DSOs must request action or
291 only one */
292 if ((l->l_audit[cnt].bindflags & LA_FLG_BINDFROM) != 0
293 && (result->l_audit[cnt].bindflags & LA_FLG_BINDTO) != 0)
295 unsigned int flags = altvalue;
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 altvalue = 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 = altvalue;
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 altvalue = 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 unsigned int flags = altvalue;
377 long int new_framesize = -1;
378 uintptr_t new_value
379 = afct->ARCH_LA_PLTENTER (&sym, reloc_result->boundndx,
380 &l->l_audit[cnt].cookie,
381 &reloc_result->bound->l_audit[cnt].cookie,
382 regs, &flags, symname,
383 &new_framesize);
384 if (new_value != (uintptr_t) sym.st_value)
386 altvalue = LA_SYMB_ALTVALUE;
387 sym.st_value = new_value;
390 /* Remember the results for every audit library and
391 store a summary in the first two bits. */
392 reloc_result->enterexit
393 |= ((flags & (LA_SYMB_NOPLTENTER | LA_SYMB_NOPLTEXIT))
394 << (2 * (cnt + 1)));
396 if ((reloc_result->enterexit & (LA_SYMB_NOPLTEXIT
397 << (2 * (cnt + 1))))
398 == 0 && new_framesize != -1 && framesize != -2)
400 /* If this is the first call providing information,
401 use it. */
402 if (framesize == -1)
403 framesize = new_framesize;
404 /* If two pltenter calls provide conflicting information,
405 use the larger value. */
406 else if (new_framesize != framesize)
407 framesize = MAX (new_framesize, framesize);
411 afct = afct->next;
414 value = DL_FIXUP_ADDR_VALUE (sym.st_value);
416 #endif
418 /* Store the frame size information. */
419 *framesizep = framesize;
421 (*mcount_fct) (retaddr, DL_FIXUP_VALUE_CODE_ADDR (value));
423 return value;
426 #endif /* PROF && ELF_MACHINE_NO_PLT */
429 #include <stdio.h>
430 void
431 ARCH_FIXUP_ATTRIBUTE
432 _dl_call_pltexit (struct link_map *l, ElfW(Word) reloc_arg,
433 const void *inregs, void *outregs)
435 #ifdef SHARED
436 /* This is the address in the array where we store the result of previous
437 relocations. */
438 // XXX Maybe the bound information must be stored on the stack since
439 // XXX with bind_not a new value could have been stored in the meantime.
440 struct reloc_result *reloc_result = &l->l_reloc_result[reloc_index];
441 ElfW(Sym) *defsym = ((ElfW(Sym) *) D_PTR (reloc_result->bound,
442 l_info[DT_SYMTAB])
443 + reloc_result->boundndx);
445 /* Set up the sym parameter. */
446 ElfW(Sym) sym = *defsym;
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