Updated to fedora-glibc-20090427T1419
[glibc/history.git] / elf / dl-runtime.c
blob0eb7d4e3b9759e29f053cfa2f4f91231bd73f738
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 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 /* Currently result contains the base load address (or link map)
122 of the object that defines sym. Now add in the symbol
123 offset. */
124 value = DL_FIXUP_MAKE_VALUE (result,
125 sym ? (LOOKUP_VALUE_ADDRESS (result)
126 + sym->st_value) : 0);
128 else
130 /* We already found the symbol. The module (and therefore its load
131 address) is also known. */
132 value = DL_FIXUP_MAKE_VALUE (l, l->l_addr + sym->st_value);
133 result = l;
136 /* And now perhaps the relocation addend. */
137 value = elf_machine_plt_value (l, reloc, value);
139 if (__builtin_expect (ELFW(ST_TYPE) (sym->st_info) == STT_GNU_IFUNC, 0))
140 value = ((DL_FIXUP_VALUE_TYPE (*) (void)) DL_FIXUP_VALUE_ADDR (value)) ();
142 /* Finally, fix up the plt itself. */
143 if (__builtin_expect (GLRO(dl_bind_not), 0))
144 return value;
146 return elf_machine_fixup_plt (l, result, reloc, rel_addr, value);
148 #endif
150 #if !defined PROF && !defined ELF_MACHINE_NO_PLT && !__BOUNDED_POINTERS__
151 DL_FIXUP_VALUE_TYPE
152 __attribute ((noinline)) ARCH_FIXUP_ATTRIBUTE
153 _dl_profile_fixup (
154 #ifdef ELF_MACHINE_RUNTIME_FIXUP_ARGS
155 ELF_MACHINE_RUNTIME_FIXUP_ARGS,
156 #endif
157 struct link_map *l, ElfW(Word) reloc_arg,
158 ElfW(Addr) retaddr, void *regs, long int *framesizep)
160 void (*mcount_fct) (ElfW(Addr), ElfW(Addr)) = INTUSE(_dl_mcount);
162 /* This is the address in the array where we store the result of previous
163 relocations. */
164 struct reloc_result *reloc_result = &l->l_reloc_result[reloc_index];
165 DL_FIXUP_VALUE_TYPE *resultp = &reloc_result->addr;
167 DL_FIXUP_VALUE_TYPE value = *resultp;
168 if (DL_FIXUP_VALUE_CODE_ADDR (value) == 0)
170 /* This is the first time we have to relocate this object. */
171 const ElfW(Sym) *const symtab
172 = (const void *) D_PTR (l, l_info[DT_SYMTAB]);
173 const char *strtab = (const char *) D_PTR (l, l_info[DT_STRTAB]);
175 const PLTREL *const reloc
176 = (const void *) (D_PTR (l, l_info[DT_JMPREL]) + reloc_offset);
177 const ElfW(Sym) *refsym = &symtab[ELFW(R_SYM) (reloc->r_info)];
178 const ElfW(Sym) *defsym = refsym;
179 lookup_t result;
181 /* Sanity check that we're really looking at a PLT relocation. */
182 assert (ELFW(R_TYPE)(reloc->r_info) == ELF_MACHINE_JMP_SLOT);
184 /* Look up the target symbol. If the symbol is marked STV_PROTECTED
185 don't look in the global scope. */
186 if (__builtin_expect (ELFW(ST_VISIBILITY) (refsym->st_other), 0) == 0)
188 const struct r_found_version *version = NULL;
190 if (l->l_info[VERSYMIDX (DT_VERSYM)] != NULL)
192 const ElfW(Half) *vernum =
193 (const void *) D_PTR (l, l_info[VERSYMIDX (DT_VERSYM)]);
194 ElfW(Half) ndx = vernum[ELFW(R_SYM) (reloc->r_info)] & 0x7fff;
195 version = &l->l_versions[ndx];
196 if (version->hash == 0)
197 version = NULL;
200 /* We need to keep the scope around so do some locking. This is
201 not necessary for objects which cannot be unloaded or when
202 we are not using any threads (yet). */
203 int flags = DL_LOOKUP_ADD_DEPENDENCY;
204 if (!RTLD_SINGLE_THREAD_P)
206 THREAD_GSCOPE_SET_FLAG ();
207 flags |= DL_LOOKUP_GSCOPE_LOCK;
210 result = _dl_lookup_symbol_x (strtab + refsym->st_name, l,
211 &defsym, l->l_scope, version,
212 ELF_RTYPE_CLASS_PLT, flags, NULL);
214 /* We are done with the global scope. */
215 if (!RTLD_SINGLE_THREAD_P)
216 THREAD_GSCOPE_RESET_FLAG ();
218 /* Currently result contains the base load address (or link map)
219 of the object that defines sym. Now add in the symbol
220 offset. */
221 value = DL_FIXUP_MAKE_VALUE (result,
222 defsym != NULL
223 ? LOOKUP_VALUE_ADDRESS (result)
224 + defsym->st_value : 0);
226 if (__builtin_expect (ELFW(ST_TYPE) (defsym->st_info)
227 == STT_GNU_IFUNC, 0))
228 value = ((DL_FIXUP_VALUE_TYPE (*) (void))
229 DL_FIXUP_VALUE_ADDR (value)) ();
231 else
233 /* We already found the symbol. The module (and therefore its load
234 address) is also known. */
235 value = DL_FIXUP_MAKE_VALUE (l, l->l_addr + refsym->st_value);
237 if (__builtin_expect (ELFW(ST_TYPE) (refsym->st_info)
238 == STT_GNU_IFUNC, 0))
239 value = ((DL_FIXUP_VALUE_TYPE (*) (void))
240 DL_FIXUP_VALUE_ADDR (value)) ();
242 result = l;
244 /* And now perhaps the relocation addend. */
245 value = elf_machine_plt_value (l, reloc, value);
247 #ifdef SHARED
248 /* Auditing checkpoint: we have a new binding. Provide the
249 auditing libraries the possibility to change the value and
250 tell us whether further auditing is wanted. */
251 if (defsym != NULL && GLRO(dl_naudit) > 0)
253 reloc_result->bound = result;
254 /* Compute index of the symbol entry in the symbol table of
255 the DSO with the definition. */
256 reloc_result->boundndx = (defsym
257 - (ElfW(Sym) *) D_PTR (result,
258 l_info[DT_SYMTAB]));
260 /* Determine whether any of the two participating DSOs is
261 interested in auditing. */
262 if ((l->l_audit_any_plt | result->l_audit_any_plt) != 0)
264 unsigned int altvalue = 0;
265 struct audit_ifaces *afct = GLRO(dl_audit);
266 /* Synthesize a symbol record where the st_value field is
267 the result. */
268 ElfW(Sym) sym = *defsym;
269 sym.st_value = DL_FIXUP_VALUE_ADDR (value);
271 /* Keep track whether there is any interest in tracing
272 the call in the lower two bits. */
273 assert (DL_NNS * 2 <= sizeof (reloc_result->flags) * 8);
274 assert ((LA_SYMB_NOPLTENTER | LA_SYMB_NOPLTEXIT) == 3);
275 reloc_result->enterexit = LA_SYMB_NOPLTENTER | LA_SYMB_NOPLTEXIT;
277 const char *strtab2 = (const void *) D_PTR (result,
278 l_info[DT_STRTAB]);
280 for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
282 /* XXX Check whether both DSOs must request action or
283 only one */
284 if ((l->l_audit[cnt].bindflags & LA_FLG_BINDFROM) != 0
285 && (result->l_audit[cnt].bindflags & LA_FLG_BINDTO) != 0)
287 unsigned int flags = altvalue;
288 if (afct->symbind != NULL)
290 uintptr_t new_value
291 = afct->symbind (&sym, reloc_result->boundndx,
292 &l->l_audit[cnt].cookie,
293 &result->l_audit[cnt].cookie,
294 &flags,
295 strtab2 + defsym->st_name);
296 if (new_value != (uintptr_t) sym.st_value)
298 altvalue = LA_SYMB_ALTVALUE;
299 sym.st_value = new_value;
303 /* Remember the results for every audit library and
304 store a summary in the first two bits. */
305 reloc_result->enterexit
306 &= flags & (LA_SYMB_NOPLTENTER | LA_SYMB_NOPLTEXIT);
307 reloc_result->enterexit
308 |= ((flags & (LA_SYMB_NOPLTENTER | LA_SYMB_NOPLTEXIT))
309 << ((cnt + 1) * 2));
311 else
312 /* If the bind flags say this auditor is not interested,
313 set the bits manually. */
314 reloc_result->enterexit
315 |= ((LA_SYMB_NOPLTENTER | LA_SYMB_NOPLTEXIT)
316 << ((cnt + 1) * 2));
318 afct = afct->next;
321 reloc_result->flags = altvalue;
322 value = DL_FIXUP_ADDR_VALUE (sym.st_value);
324 else
325 /* Set all bits since this symbol binding is not interesting. */
326 reloc_result->enterexit = (1u << DL_NNS) - 1;
328 #endif
330 /* Store the result for later runs. */
331 if (__builtin_expect (! GLRO(dl_bind_not), 1))
332 *resultp = value;
335 /* By default we do not call the pltexit function. */
336 long int framesize = -1;
338 #ifdef SHARED
339 /* Auditing checkpoint: report the PLT entering and allow the
340 auditors to change the value. */
341 if (DL_FIXUP_VALUE_CODE_ADDR (value) != 0 && GLRO(dl_naudit) > 0
342 /* Don't do anything if no auditor wants to intercept this call. */
343 && (reloc_result->enterexit & LA_SYMB_NOPLTENTER) == 0)
345 ElfW(Sym) *defsym = ((ElfW(Sym) *) D_PTR (reloc_result->bound,
346 l_info[DT_SYMTAB])
347 + reloc_result->boundndx);
349 /* Set up the sym parameter. */
350 ElfW(Sym) sym = *defsym;
351 sym.st_value = DL_FIXUP_VALUE_ADDR (value);
353 /* Get the symbol name. */
354 const char *strtab = (const void *) D_PTR (reloc_result->bound,
355 l_info[DT_STRTAB]);
356 const char *symname = strtab + sym.st_name;
358 /* Keep track of overwritten addresses. */
359 unsigned int altvalue = reloc_result->flags;
361 struct audit_ifaces *afct = GLRO(dl_audit);
362 for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
364 if (afct->ARCH_LA_PLTENTER != NULL
365 && (reloc_result->enterexit
366 & (LA_SYMB_NOPLTENTER << (2 * (cnt + 1)))) == 0)
368 unsigned int flags = altvalue;
369 long int new_framesize = -1;
370 uintptr_t new_value
371 = afct->ARCH_LA_PLTENTER (&sym, reloc_result->boundndx,
372 &l->l_audit[cnt].cookie,
373 &reloc_result->bound->l_audit[cnt].cookie,
374 regs, &flags, symname,
375 &new_framesize);
376 if (new_value != (uintptr_t) sym.st_value)
378 altvalue = LA_SYMB_ALTVALUE;
379 sym.st_value = new_value;
382 /* Remember the results for every audit library and
383 store a summary in the first two bits. */
384 reloc_result->enterexit
385 |= ((flags & (LA_SYMB_NOPLTENTER | LA_SYMB_NOPLTEXIT))
386 << (2 * (cnt + 1)));
388 if ((reloc_result->enterexit & (LA_SYMB_NOPLTEXIT
389 << (2 * (cnt + 1))))
390 == 0 && new_framesize != -1 && framesize != -2)
392 /* If this is the first call providing information,
393 use it. */
394 if (framesize == -1)
395 framesize = new_framesize;
396 /* If two pltenter calls provide conflicting information,
397 use the larger value. */
398 else if (new_framesize != framesize)
399 framesize = MAX (new_framesize, framesize);
403 afct = afct->next;
406 value = DL_FIXUP_ADDR_VALUE (sym.st_value);
408 #endif
410 /* Store the frame size information. */
411 *framesizep = framesize;
413 (*mcount_fct) (retaddr, DL_FIXUP_VALUE_CODE_ADDR (value));
415 return value;
418 #endif /* PROF && ELF_MACHINE_NO_PLT */
421 #include <stdio.h>
422 void
423 ARCH_FIXUP_ATTRIBUTE
424 _dl_call_pltexit (struct link_map *l, ElfW(Word) reloc_arg,
425 const void *inregs, void *outregs)
427 #ifdef SHARED
428 /* This is the address in the array where we store the result of previous
429 relocations. */
430 // XXX Maybe the bound information must be stored on the stack since
431 // XXX with bind_not a new value could have been stored in the meantime.
432 struct reloc_result *reloc_result = &l->l_reloc_result[reloc_index];
433 ElfW(Sym) *defsym = ((ElfW(Sym) *) D_PTR (reloc_result->bound,
434 l_info[DT_SYMTAB])
435 + reloc_result->boundndx);
437 /* Set up the sym parameter. */
438 ElfW(Sym) sym = *defsym;
440 /* Get the symbol name. */
441 const char *strtab = (const void *) D_PTR (reloc_result->bound,
442 l_info[DT_STRTAB]);
443 const char *symname = strtab + sym.st_name;
445 struct audit_ifaces *afct = GLRO(dl_audit);
446 for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
448 if (afct->ARCH_LA_PLTEXIT != NULL
449 && (reloc_result->enterexit
450 & (LA_SYMB_NOPLTEXIT >> (2 * cnt))) == 0)
452 afct->ARCH_LA_PLTEXIT (&sym, reloc_result->boundndx,
453 &l->l_audit[cnt].cookie,
454 &reloc_result->bound->l_audit[cnt].cookie,
455 inregs, outregs, symname);
458 afct = afct->next;
460 #endif