1 /* On-demand PLT fixup for shared objects.
2 Copyright (C) 1995-2006, 2007 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
20 #define IN_DL_RUNTIME 1 /* This can be tested in dl-machine.h. */
25 #include <sys/param.h>
27 #include <sysdep-cancel.h>
28 #include "dynamic-link.h"
32 #if (!defined ELF_MACHINE_NO_RELA && !defined ELF_MACHINE_PLT_REL) \
34 # define PLTREL ElfW(Rela)
36 # define PLTREL ElfW(Rel)
40 # define VERSYMIDX(sym) (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGIDX (sym))
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
50 /* This function is called through a special trampoline from the PLT the
51 first time each PLT entry is called. We must perform the relocation
52 specified in the PLT of the given shared object, and return the resolved
53 function address to the trampoline, which will restart the original call
54 to that address. Future calls will bounce directly from the PLT to the
57 #ifndef ELF_MACHINE_NO_PLT
59 __attribute ((noinline)) ARCH_FIXUP_ATTRIBUTE
61 # ifdef ELF_MACHINE_RUNTIME_FIXUP_ARGS
62 ELF_MACHINE_RUNTIME_FIXUP_ARGS,
64 /* GKM FIXME: Fix trampoline to pass bounds so we can do
65 without the `__unbounded' qualifier. */
66 struct link_map *__unbounded l, ElfW(Word) reloc_offset)
68 const ElfW(Sym) *const symtab
69 = (const void *) D_PTR (l, l_info[DT_SYMTAB]);
70 const char *strtab = (const void *) D_PTR (l, l_info[DT_STRTAB]);
72 const PLTREL *const reloc
73 = (const void *) (D_PTR (l, l_info[DT_JMPREL]) + reloc_offset);
74 const ElfW(Sym) *sym = &symtab[ELFW(R_SYM) (reloc->r_info)];
75 void *const rel_addr = (void *)(l->l_addr + reloc->r_offset);
77 DL_FIXUP_VALUE_TYPE value;
79 /* Sanity check that we're really looking at a PLT relocation. */
80 assert (ELFW(R_TYPE)(reloc->r_info) == ELF_MACHINE_JMP_SLOT);
82 /* Look up the target symbol. If the normal lookup rules are not
83 used don't look in the global scope. */
84 if (__builtin_expect (ELFW(ST_VISIBILITY) (sym->st_other), 0) == 0)
86 const struct r_found_version *version = NULL;
88 if (l->l_info[VERSYMIDX (DT_VERSYM)] != NULL)
90 const ElfW(Half) *vernum =
91 (const void *) D_PTR (l, l_info[VERSYMIDX (DT_VERSYM)]);
92 ElfW(Half) ndx = vernum[ELFW(R_SYM) (reloc->r_info)] & 0x7fff;
93 version = &l->l_versions[ndx];
94 if (version->hash == 0)
98 /* We need to keep the scope around so do some locking. This is
99 not necessary for objects which cannot be unloaded or when
100 we are not using any threads (yet). */
101 int flags = DL_LOOKUP_ADD_DEPENDENCY;
102 if (!RTLD_SINGLE_THREAD_P)
104 THREAD_GSCOPE_SET_FLAG ();
106 if (l->l_type == lt_loaded)
108 __rtld_mrlock_lock (l->l_scope_lock);
109 flags |= DL_LOOKUP_SCOPE_LOCK;
113 result = _dl_lookup_symbol_x (strtab + sym->st_name, l, &sym, l->l_scope,
114 version, ELF_RTYPE_CLASS_PLT, flags, NULL);
116 if ((flags & DL_LOOKUP_SCOPE_LOCK) != 0)
117 __rtld_mrlock_unlock (l->l_scope_lock);
119 /* We are done with the global scope. */
120 if (!RTLD_SINGLE_THREAD_P)
121 THREAD_GSCOPE_RESET_FLAG ();
123 /* Currently result contains the base load address (or link map)
124 of the object that defines sym. Now add in the symbol
126 value = DL_FIXUP_MAKE_VALUE (result,
127 sym ? (LOOKUP_VALUE_ADDRESS (result)
128 + sym->st_value) : 0);
132 /* We already found the symbol. The module (and therefore its load
133 address) is also known. */
134 value = DL_FIXUP_MAKE_VALUE (l, l->l_addr + sym->st_value);
138 /* And now perhaps the relocation addend. */
139 value = elf_machine_plt_value (l, reloc, value);
141 /* Finally, fix up the plt itself. */
142 if (__builtin_expect (GLRO(dl_bind_not), 0))
145 return elf_machine_fixup_plt (l, result, reloc, rel_addr, value);
149 #if !defined PROF && !defined ELF_MACHINE_NO_PLT && !__BOUNDED_POINTERS__
152 __attribute ((noinline)) ARCH_FIXUP_ATTRIBUTE
154 #ifdef ELF_MACHINE_RUNTIME_FIXUP_ARGS
155 ELF_MACHINE_RUNTIME_FIXUP_ARGS,
157 struct link_map *l, ElfW(Word) reloc_offset,
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
164 struct reloc_result *reloc_result
165 = &l->l_reloc_result[reloc_offset / sizeof (PLTREL)];
166 DL_FIXUP_VALUE_TYPE *resultp = &reloc_result->addr;
168 DL_FIXUP_VALUE_TYPE value = *resultp;
169 if (DL_FIXUP_VALUE_CODE_ADDR (value) == 0)
171 /* This is the first time we have to relocate this object. */
172 const ElfW(Sym) *const symtab
173 = (const void *) D_PTR (l, l_info[DT_SYMTAB]);
174 const char *strtab = (const char *) D_PTR (l, l_info[DT_STRTAB]);
176 const PLTREL *const reloc
177 = (const void *) (D_PTR (l, l_info[DT_JMPREL]) + reloc_offset);
178 const ElfW(Sym) *refsym = &symtab[ELFW(R_SYM) (reloc->r_info)];
179 const ElfW(Sym) *defsym = refsym;
182 /* Sanity check that we're really looking at a PLT relocation. */
183 assert (ELFW(R_TYPE)(reloc->r_info) == ELF_MACHINE_JMP_SLOT);
185 /* Look up the target symbol. If the symbol is marked STV_PROTECTED
186 don't look in the global scope. */
187 if (__builtin_expect (ELFW(ST_VISIBILITY) (refsym->st_other), 0) == 0)
189 const struct r_found_version *version = NULL;
191 if (l->l_info[VERSYMIDX (DT_VERSYM)] != NULL)
193 const ElfW(Half) *vernum =
194 (const void *) D_PTR (l, l_info[VERSYMIDX (DT_VERSYM)]);
195 ElfW(Half) ndx = vernum[ELFW(R_SYM) (reloc->r_info)] & 0x7fff;
196 version = &l->l_versions[ndx];
197 if (version->hash == 0)
201 /* We need to keep the scope around so do some locking. This is
202 not necessary for objects which cannot be unloaded or when
203 we are not using any threads (yet). */
204 int flags = DL_LOOKUP_ADD_DEPENDENCY;
205 if (!RTLD_SINGLE_THREAD_P)
207 THREAD_GSCOPE_SET_FLAG ();
209 if (l->l_type == lt_loaded)
211 __rtld_mrlock_lock (l->l_scope_lock);
212 flags |= DL_LOOKUP_SCOPE_LOCK;
216 result = _dl_lookup_symbol_x (strtab + refsym->st_name, l,
217 &defsym, l->l_scope, version,
218 ELF_RTYPE_CLASS_PLT, flags, NULL);
220 if ((flags & DL_LOOKUP_SCOPE_LOCK) != 0)
221 __rtld_mrlock_unlock (l->l_scope_lock);
223 /* We are done with the global scope. */
224 if (!RTLD_SINGLE_THREAD_P)
225 THREAD_GSCOPE_RESET_FLAG ();
227 /* Currently result contains the base load address (or link map)
228 of the object that defines sym. Now add in the symbol
230 value = DL_FIXUP_MAKE_VALUE (result,
232 ? LOOKUP_VALUE_ADDRESS (result)
233 + defsym->st_value : 0);
237 /* We already found the symbol. The module (and therefore its load
238 address) is also known. */
239 value = DL_FIXUP_MAKE_VALUE (l, l->l_addr + refsym->st_value);
242 /* And now perhaps the relocation addend. */
243 value = elf_machine_plt_value (l, reloc, value);
246 /* Auditing checkpoint: we have a new binding. Provide the
247 auditing libraries the possibility to change the value and
248 tell us whether further auditing is wanted. */
249 if (defsym != NULL && GLRO(dl_naudit) > 0)
251 reloc_result->bound = result;
252 /* Compute index of the symbol entry in the symbol table of
253 the DSO with the definition. */
254 reloc_result->boundndx = (defsym
255 - (ElfW(Sym) *) D_PTR (result,
258 /* Determine whether any of the two participating DSOs is
259 interested in auditing. */
260 if ((l->l_audit_any_plt | result->l_audit_any_plt) != 0)
262 unsigned int altvalue = 0;
263 struct audit_ifaces *afct = GLRO(dl_audit);
264 /* Synthesize a symbol record where the st_value field is
266 ElfW(Sym) sym = *defsym;
267 sym.st_value = DL_FIXUP_VALUE_ADDR (value);
269 /* Keep track whether there is any interest in tracing
270 the call in the lower two bits. */
271 assert (DL_NNS * 2 <= sizeof (reloc_result->flags) * 8);
272 assert ((LA_SYMB_NOPLTENTER | LA_SYMB_NOPLTEXIT) == 3);
273 reloc_result->enterexit = LA_SYMB_NOPLTENTER | LA_SYMB_NOPLTEXIT;
275 const char *strtab2 = (const void *) D_PTR (result,
278 for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
280 /* XXX Check whether both DSOs must request action or
282 if ((l->l_audit[cnt].bindflags & LA_FLG_BINDFROM) != 0
283 && (result->l_audit[cnt].bindflags & LA_FLG_BINDTO) != 0)
285 unsigned int flags = altvalue;
286 if (afct->symbind != NULL)
289 = afct->symbind (&sym, reloc_result->boundndx,
290 &l->l_audit[cnt].cookie,
291 &result->l_audit[cnt].cookie,
293 strtab2 + defsym->st_name);
294 if (new_value != (uintptr_t) sym.st_value)
296 altvalue = LA_SYMB_ALTVALUE;
297 sym.st_value = new_value;
301 /* Remember the results for every audit library and
302 store a summary in the first two bits. */
303 reloc_result->enterexit
304 &= flags & (LA_SYMB_NOPLTENTER | LA_SYMB_NOPLTEXIT);
305 reloc_result->enterexit
306 |= ((flags & (LA_SYMB_NOPLTENTER | LA_SYMB_NOPLTEXIT))
310 /* If the bind flags say this auditor is not interested,
311 set the bits manually. */
312 reloc_result->enterexit
313 |= ((LA_SYMB_NOPLTENTER | LA_SYMB_NOPLTEXIT)
319 reloc_result->flags = altvalue;
320 value = DL_FIXUP_ADDR_VALUE (sym.st_value);
323 /* Set all bits since this symbol binding is not interesting. */
324 reloc_result->enterexit = (1u << DL_NNS) - 1;
328 /* Store the result for later runs. */
329 if (__builtin_expect (! GLRO(dl_bind_not), 1))
333 /* By default we do not call the pltexit function. */
334 long int framesize = -1;
337 /* Auditing checkpoint: report the PLT entering and allow the
338 auditors to change the value. */
339 if (DL_FIXUP_VALUE_CODE_ADDR (value) != 0 && GLRO(dl_naudit) > 0
340 /* Don't do anything if no auditor wants to intercept this call. */
341 && (reloc_result->enterexit & LA_SYMB_NOPLTENTER) == 0)
343 ElfW(Sym) *defsym = ((ElfW(Sym) *) D_PTR (reloc_result->bound,
345 + reloc_result->boundndx);
347 /* Set up the sym parameter. */
348 ElfW(Sym) sym = *defsym;
349 sym.st_value = DL_FIXUP_VALUE_ADDR (value);
351 /* Get the symbol name. */
352 const char *strtab = (const void *) D_PTR (reloc_result->bound,
354 const char *symname = strtab + sym.st_name;
356 /* Keep track of overwritten addresses. */
357 unsigned int altvalue = reloc_result->flags;
359 struct audit_ifaces *afct = GLRO(dl_audit);
360 for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
362 if (afct->ARCH_LA_PLTENTER != NULL
363 && (reloc_result->enterexit
364 & (LA_SYMB_NOPLTENTER << (2 * (cnt + 1)))) == 0)
366 unsigned int flags = altvalue;
367 long int new_framesize = -1;
369 = afct->ARCH_LA_PLTENTER (&sym, reloc_result->boundndx,
370 &l->l_audit[cnt].cookie,
371 &reloc_result->bound->l_audit[cnt].cookie,
372 regs, &flags, symname,
374 if (new_value != (uintptr_t) sym.st_value)
376 altvalue = LA_SYMB_ALTVALUE;
377 sym.st_value = new_value;
380 /* Remember the results for every audit library and
381 store a summary in the first two bits. */
382 reloc_result->enterexit
383 |= ((flags & (LA_SYMB_NOPLTENTER | LA_SYMB_NOPLTEXIT))
386 if ((reloc_result->enterexit & (LA_SYMB_NOPLTEXIT
388 == 0 && new_framesize != -1 && framesize != -2)
390 /* If this is the first call providing information,
393 framesize = new_framesize;
394 /* If two pltenter calls provide conflicting information,
395 use the larger value. */
396 else if (new_framesize != framesize)
397 framesize = MAX (new_framesize, framesize);
404 value = DL_FIXUP_ADDR_VALUE (sym.st_value);
408 /* Store the frame size information. */
409 *framesizep = framesize;
411 (*mcount_fct) (retaddr, DL_FIXUP_VALUE_CODE_ADDR (value));
416 #endif /* PROF && ELF_MACHINE_NO_PLT */
422 _dl_call_pltexit (struct link_map *l, ElfW(Word) reloc_offset,
423 const void *inregs, void *outregs)
426 /* This is the address in the array where we store the result of previous
428 // XXX Maybe the bound information must be stored on the stack since
429 // XXX with bind_not a new value could have been stored in the meantime.
430 struct reloc_result *reloc_result
431 = &l->l_reloc_result[reloc_offset / sizeof (PLTREL)];
432 ElfW(Sym) *defsym = ((ElfW(Sym) *) D_PTR (reloc_result->bound,
434 + reloc_result->boundndx);
436 /* Set up the sym parameter. */
437 ElfW(Sym) sym = *defsym;
439 /* Get the symbol name. */
440 const char *strtab = (const void *) D_PTR (reloc_result->bound,
442 const char *symname = strtab + sym.st_name;
444 struct audit_ifaces *afct = GLRO(dl_audit);
445 for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
447 if (afct->ARCH_LA_PLTEXIT != NULL
448 && (reloc_result->enterexit
449 & (LA_SYMB_NOPLTEXIT >> (2 * cnt))) == 0)
451 afct->ARCH_LA_PLTEXIT (&sym, reloc_result->boundndx,
452 &l->l_audit[cnt].cookie,
453 &reloc_result->bound->l_audit[cnt].cookie,
454 inregs, outregs, symname);