Add a testase for BZ #14602
[glibc.git] / elf / dl-runtime.c
blob2e02a218e6b972a15a9c3f068b2bb3ae20d48351
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, see
17 <http://www.gnu.org/licenses/>. */
19 #define IN_DL_RUNTIME 1 /* This can be tested in dl-machine.h. */
21 #include <alloca.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <sys/param.h>
25 #include <ldsodefs.h>
26 #include <sysdep-cancel.h>
27 #include "dynamic-link.h"
28 #include <tls.h>
29 #include <dl-irel.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 /* The fixup functions might have need special attributes. If none
40 are provided define the macro as empty. */
41 #ifndef ARCH_FIXUP_ATTRIBUTE
42 # define ARCH_FIXUP_ATTRIBUTE
43 #endif
45 #ifndef reloc_offset
46 # define reloc_offset reloc_arg
47 # define reloc_index reloc_arg / sizeof (PLTREL)
48 #endif
52 /* This function is called through a special trampoline from the PLT the
53 first time each PLT entry is called. We must perform the relocation
54 specified in the PLT of the given shared object, and return the resolved
55 function address to the trampoline, which will restart the original call
56 to that address. Future calls will bounce directly from the PLT to the
57 function. */
59 #ifndef ELF_MACHINE_NO_PLT
60 DL_FIXUP_VALUE_TYPE
61 __attribute ((noinline)) ARCH_FIXUP_ATTRIBUTE
62 _dl_fixup (
63 # ifdef ELF_MACHINE_RUNTIME_FIXUP_ARGS
64 ELF_MACHINE_RUNTIME_FIXUP_ARGS,
65 # endif
66 /* GKM FIXME: Fix trampoline to pass bounds so we can do
67 without the `__unbounded' qualifier. */
68 struct link_map *__unbounded l, ElfW(Word) reloc_arg)
70 const ElfW(Sym) *const symtab
71 = (const void *) D_PTR (l, l_info[DT_SYMTAB]);
72 const char *strtab = (const void *) D_PTR (l, l_info[DT_STRTAB]);
74 const PLTREL *const reloc
75 = (const void *) (D_PTR (l, l_info[DT_JMPREL]) + reloc_offset);
76 const ElfW(Sym) *sym = &symtab[ELFW(R_SYM) (reloc->r_info)];
77 void *const rel_addr = (void *)(l->l_addr + reloc->r_offset);
78 lookup_t result;
79 DL_FIXUP_VALUE_TYPE value;
81 /* Sanity check that we're really looking at a PLT relocation. */
82 assert (ELFW(R_TYPE)(reloc->r_info) == ELF_MACHINE_JMP_SLOT);
84 /* Look up the target symbol. If the normal lookup rules are not
85 used don't look in the global scope. */
86 if (__builtin_expect (ELFW(ST_VISIBILITY) (sym->st_other), 0) == 0)
88 const struct r_found_version *version = NULL;
90 if (l->l_info[VERSYMIDX (DT_VERSYM)] != NULL)
92 const ElfW(Half) *vernum =
93 (const void *) D_PTR (l, l_info[VERSYMIDX (DT_VERSYM)]);
94 ElfW(Half) ndx = vernum[ELFW(R_SYM) (reloc->r_info)] & 0x7fff;
95 version = &l->l_versions[ndx];
96 if (version->hash == 0)
97 version = NULL;
100 /* We need to keep the scope around so do some locking. This is
101 not necessary for objects which cannot be unloaded or when
102 we are not using any threads (yet). */
103 int flags = DL_LOOKUP_ADD_DEPENDENCY;
104 if (!RTLD_SINGLE_THREAD_P)
106 THREAD_GSCOPE_SET_FLAG ();
107 flags |= DL_LOOKUP_GSCOPE_LOCK;
110 #ifdef RTLD_ENABLE_FOREIGN_CALL
111 RTLD_ENABLE_FOREIGN_CALL;
112 #endif
114 result = _dl_lookup_symbol_x (strtab + sym->st_name, l, &sym, l->l_scope,
115 version, ELF_RTYPE_CLASS_PLT, flags, NULL);
117 /* We are done with the global scope. */
118 if (!RTLD_SINGLE_THREAD_P)
119 THREAD_GSCOPE_RESET_FLAG ();
121 #ifdef RTLD_FINALIZE_FOREIGN_CALL
122 RTLD_FINALIZE_FOREIGN_CALL;
123 #endif
125 /* Currently result contains the base load address (or link map)
126 of the object that defines sym. Now add in the symbol
127 offset. */
128 value = DL_FIXUP_MAKE_VALUE (result,
129 sym ? (LOOKUP_VALUE_ADDRESS (result)
130 + sym->st_value) : 0);
132 else
134 /* We already found the symbol. The module (and therefore its load
135 address) is also known. */
136 value = DL_FIXUP_MAKE_VALUE (l, l->l_addr + sym->st_value);
137 result = l;
140 /* And now perhaps the relocation addend. */
141 value = elf_machine_plt_value (l, reloc, value);
143 if (sym != NULL
144 && __builtin_expect (ELFW(ST_TYPE) (sym->st_info) == STT_GNU_IFUNC, 0))
145 value = elf_ifunc_invoke (DL_FIXUP_VALUE_ADDR (value));
147 /* Finally, fix up the plt itself. */
148 if (__builtin_expect (GLRO(dl_bind_not), 0))
149 return value;
151 return elf_machine_fixup_plt (l, result, reloc, rel_addr, value);
153 #endif
155 #if !defined PROF && !defined ELF_MACHINE_NO_PLT && !__BOUNDED_POINTERS__
156 DL_FIXUP_VALUE_TYPE
157 __attribute ((noinline)) ARCH_FIXUP_ATTRIBUTE
158 _dl_profile_fixup (
159 #ifdef ELF_MACHINE_RUNTIME_FIXUP_ARGS
160 ELF_MACHINE_RUNTIME_FIXUP_ARGS,
161 #endif
162 struct link_map *l, ElfW(Word) reloc_arg,
163 ElfW(Addr) retaddr, void *regs, long int *framesizep)
165 void (*mcount_fct) (ElfW(Addr), ElfW(Addr)) = INTUSE(_dl_mcount);
167 /* This is the address in the array where we store the result of previous
168 relocations. */
169 struct reloc_result *reloc_result = &l->l_reloc_result[reloc_index];
170 DL_FIXUP_VALUE_TYPE *resultp = &reloc_result->addr;
172 DL_FIXUP_VALUE_TYPE value = *resultp;
173 if (DL_FIXUP_VALUE_CODE_ADDR (value) == 0)
175 /* This is the first time we have to relocate this object. */
176 const ElfW(Sym) *const symtab
177 = (const void *) D_PTR (l, l_info[DT_SYMTAB]);
178 const char *strtab = (const char *) D_PTR (l, l_info[DT_STRTAB]);
180 const PLTREL *const reloc
181 = (const void *) (D_PTR (l, l_info[DT_JMPREL]) + reloc_offset);
182 const ElfW(Sym) *refsym = &symtab[ELFW(R_SYM) (reloc->r_info)];
183 const ElfW(Sym) *defsym = refsym;
184 lookup_t result;
186 /* Sanity check that we're really looking at a PLT relocation. */
187 assert (ELFW(R_TYPE)(reloc->r_info) == ELF_MACHINE_JMP_SLOT);
189 /* Look up the target symbol. If the symbol is marked STV_PROTECTED
190 don't look in the global scope. */
191 if (__builtin_expect (ELFW(ST_VISIBILITY) (refsym->st_other), 0) == 0)
193 const struct r_found_version *version = NULL;
195 if (l->l_info[VERSYMIDX (DT_VERSYM)] != NULL)
197 const ElfW(Half) *vernum =
198 (const void *) D_PTR (l, l_info[VERSYMIDX (DT_VERSYM)]);
199 ElfW(Half) ndx = vernum[ELFW(R_SYM) (reloc->r_info)] & 0x7fff;
200 version = &l->l_versions[ndx];
201 if (version->hash == 0)
202 version = NULL;
205 /* We need to keep the scope around so do some locking. This is
206 not necessary for objects which cannot be unloaded or when
207 we are not using any threads (yet). */
208 int flags = DL_LOOKUP_ADD_DEPENDENCY;
209 if (!RTLD_SINGLE_THREAD_P)
211 THREAD_GSCOPE_SET_FLAG ();
212 flags |= DL_LOOKUP_GSCOPE_LOCK;
215 result = _dl_lookup_symbol_x (strtab + refsym->st_name, l,
216 &defsym, l->l_scope, version,
217 ELF_RTYPE_CLASS_PLT, flags, NULL);
219 /* We are done with the global scope. */
220 if (!RTLD_SINGLE_THREAD_P)
221 THREAD_GSCOPE_RESET_FLAG ();
223 /* Currently result contains the base load address (or link map)
224 of the object that defines sym. Now add in the symbol
225 offset. */
226 value = DL_FIXUP_MAKE_VALUE (result,
227 defsym != NULL
228 ? LOOKUP_VALUE_ADDRESS (result)
229 + defsym->st_value : 0);
231 if (defsym != NULL
232 && __builtin_expect (ELFW(ST_TYPE) (defsym->st_info)
233 == STT_GNU_IFUNC, 0))
234 value = elf_ifunc_invoke (DL_FIXUP_VALUE_ADDR (value));
236 else
238 /* We already found the symbol. The module (and therefore its load
239 address) is also known. */
240 value = DL_FIXUP_MAKE_VALUE (l, l->l_addr + refsym->st_value);
242 if (__builtin_expect (ELFW(ST_TYPE) (refsym->st_info)
243 == STT_GNU_IFUNC, 0))
244 value = elf_ifunc_invoke (DL_FIXUP_VALUE_ADDR (value));
246 result = l;
248 /* And now perhaps the relocation addend. */
249 value = elf_machine_plt_value (l, reloc, value);
251 #ifdef SHARED
252 /* Auditing checkpoint: we have a new binding. Provide the
253 auditing libraries the possibility to change the value and
254 tell us whether further auditing is wanted. */
255 if (defsym != NULL && GLRO(dl_naudit) > 0)
257 reloc_result->bound = result;
258 /* Compute index of the symbol entry in the symbol table of
259 the DSO with the definition. */
260 reloc_result->boundndx = (defsym
261 - (ElfW(Sym) *) D_PTR (result,
262 l_info[DT_SYMTAB]));
264 /* Determine whether any of the two participating DSOs is
265 interested in auditing. */
266 if ((l->l_audit_any_plt | result->l_audit_any_plt) != 0)
268 unsigned int flags = 0;
269 struct audit_ifaces *afct = GLRO(dl_audit);
270 /* Synthesize a symbol record where the st_value field is
271 the result. */
272 ElfW(Sym) sym = *defsym;
273 sym.st_value = DL_FIXUP_VALUE_ADDR (value);
275 /* Keep track whether there is any interest in tracing
276 the call in the lower two bits. */
277 assert (DL_NNS * 2 <= sizeof (reloc_result->flags) * 8);
278 assert ((LA_SYMB_NOPLTENTER | LA_SYMB_NOPLTEXIT) == 3);
279 reloc_result->enterexit = LA_SYMB_NOPLTENTER | LA_SYMB_NOPLTEXIT;
281 const char *strtab2 = (const void *) D_PTR (result,
282 l_info[DT_STRTAB]);
284 for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
286 /* XXX Check whether both DSOs must request action or
287 only one */
288 if ((l->l_audit[cnt].bindflags & LA_FLG_BINDFROM) != 0
289 && (result->l_audit[cnt].bindflags & LA_FLG_BINDTO) != 0)
291 if (afct->symbind != NULL)
293 uintptr_t new_value
294 = afct->symbind (&sym, reloc_result->boundndx,
295 &l->l_audit[cnt].cookie,
296 &result->l_audit[cnt].cookie,
297 &flags,
298 strtab2 + defsym->st_name);
299 if (new_value != (uintptr_t) sym.st_value)
301 flags |= LA_SYMB_ALTVALUE;
302 sym.st_value = new_value;
306 /* Remember the results for every audit library and
307 store a summary in the first two bits. */
308 reloc_result->enterexit
309 &= flags & (LA_SYMB_NOPLTENTER | LA_SYMB_NOPLTEXIT);
310 reloc_result->enterexit
311 |= ((flags & (LA_SYMB_NOPLTENTER | LA_SYMB_NOPLTEXIT))
312 << ((cnt + 1) * 2));
314 else
315 /* If the bind flags say this auditor is not interested,
316 set the bits manually. */
317 reloc_result->enterexit
318 |= ((LA_SYMB_NOPLTENTER | LA_SYMB_NOPLTEXIT)
319 << ((cnt + 1) * 2));
321 afct = afct->next;
324 reloc_result->flags = flags;
325 value = DL_FIXUP_ADDR_VALUE (sym.st_value);
327 else
328 /* Set all bits since this symbol binding is not interesting. */
329 reloc_result->enterexit = (1u << DL_NNS) - 1;
331 #endif
333 /* Store the result for later runs. */
334 if (__builtin_expect (! GLRO(dl_bind_not), 1))
335 *resultp = value;
338 /* By default we do not call the pltexit function. */
339 long int framesize = -1;
341 #ifdef SHARED
342 /* Auditing checkpoint: report the PLT entering and allow the
343 auditors to change the value. */
344 if (DL_FIXUP_VALUE_CODE_ADDR (value) != 0 && GLRO(dl_naudit) > 0
345 /* Don't do anything if no auditor wants to intercept this call. */
346 && (reloc_result->enterexit & LA_SYMB_NOPLTENTER) == 0)
348 ElfW(Sym) *defsym = ((ElfW(Sym) *) D_PTR (reloc_result->bound,
349 l_info[DT_SYMTAB])
350 + reloc_result->boundndx);
352 /* Set up the sym parameter. */
353 ElfW(Sym) sym = *defsym;
354 sym.st_value = DL_FIXUP_VALUE_ADDR (value);
356 /* Get the symbol name. */
357 const char *strtab = (const void *) D_PTR (reloc_result->bound,
358 l_info[DT_STRTAB]);
359 const char *symname = strtab + sym.st_name;
361 /* Keep track of overwritten addresses. */
362 unsigned int flags = reloc_result->flags;
364 struct audit_ifaces *afct = GLRO(dl_audit);
365 for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
367 if (afct->ARCH_LA_PLTENTER != NULL
368 && (reloc_result->enterexit
369 & (LA_SYMB_NOPLTENTER << (2 * (cnt + 1)))) == 0)
371 long int new_framesize = -1;
372 uintptr_t new_value
373 = afct->ARCH_LA_PLTENTER (&sym, reloc_result->boundndx,
374 &l->l_audit[cnt].cookie,
375 &reloc_result->bound->l_audit[cnt].cookie,
376 regs, &flags, symname,
377 &new_framesize);
378 if (new_value != (uintptr_t) sym.st_value)
380 flags |= LA_SYMB_ALTVALUE;
381 sym.st_value = new_value;
384 /* Remember the results for every audit library and
385 store a summary in the first two bits. */
386 reloc_result->enterexit
387 |= ((flags & (LA_SYMB_NOPLTENTER | LA_SYMB_NOPLTEXIT))
388 << (2 * (cnt + 1)));
390 if ((reloc_result->enterexit & (LA_SYMB_NOPLTEXIT
391 << (2 * (cnt + 1))))
392 == 0 && new_framesize != -1 && framesize != -2)
394 /* If this is the first call providing information,
395 use it. */
396 if (framesize == -1)
397 framesize = new_framesize;
398 /* If two pltenter calls provide conflicting information,
399 use the larger value. */
400 else if (new_framesize != framesize)
401 framesize = MAX (new_framesize, framesize);
405 afct = afct->next;
408 value = DL_FIXUP_ADDR_VALUE (sym.st_value);
410 #endif
412 /* Store the frame size information. */
413 *framesizep = framesize;
415 (*mcount_fct) (retaddr, DL_FIXUP_VALUE_CODE_ADDR (value));
417 return value;
420 #endif /* PROF && ELF_MACHINE_NO_PLT */
423 #include <stdio.h>
424 void
425 ARCH_FIXUP_ATTRIBUTE
426 _dl_call_pltexit (struct link_map *l, ElfW(Word) reloc_arg,
427 const void *inregs, void *outregs)
429 #ifdef SHARED
430 /* This is the address in the array where we store the result of previous
431 relocations. */
432 // XXX Maybe the bound information must be stored on the stack since
433 // XXX with bind_not a new value could have been stored in the meantime.
434 struct reloc_result *reloc_result = &l->l_reloc_result[reloc_index];
435 ElfW(Sym) *defsym = ((ElfW(Sym) *) D_PTR (reloc_result->bound,
436 l_info[DT_SYMTAB])
437 + reloc_result->boundndx);
439 /* Set up the sym parameter. */
440 ElfW(Sym) sym = *defsym;
441 sym.st_value = DL_FIXUP_VALUE_ADDR (reloc_result->addr);
443 /* Get the symbol name. */
444 const char *strtab = (const void *) D_PTR (reloc_result->bound,
445 l_info[DT_STRTAB]);
446 const char *symname = strtab + sym.st_name;
448 struct audit_ifaces *afct = GLRO(dl_audit);
449 for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
451 if (afct->ARCH_LA_PLTEXIT != NULL
452 && (reloc_result->enterexit
453 & (LA_SYMB_NOPLTEXIT >> (2 * cnt))) == 0)
455 afct->ARCH_LA_PLTEXIT (&sym, reloc_result->boundndx,
456 &l->l_audit[cnt].cookie,
457 &reloc_result->bound->l_audit[cnt].cookie,
458 inregs, outregs, symname);
461 afct = afct->next;
463 #endif