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"
30 #if (!defined ELF_MACHINE_NO_RELA && !defined ELF_MACHINE_PLT_REL) \
32 # define PLTREL ElfW(Rela)
34 # define PLTREL ElfW(Rel)
38 # define VERSYMIDX(sym) (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGIDX (sym))
41 /* The fixup functions might have need special attributes. If none
42 are provided define the macro as empty. */
43 #ifndef ARCH_FIXUP_ATTRIBUTE
44 # define ARCH_FIXUP_ATTRIBUTE
48 /* This function is called through a special trampoline from the PLT the
49 first time each PLT entry is called. We must perform the relocation
50 specified in the PLT of the given shared object, and return the resolved
51 function address to the trampoline, which will restart the original call
52 to that address. Future calls will bounce directly from the PLT to the
55 #ifndef ELF_MACHINE_NO_PLT
57 __attribute ((noinline
)) ARCH_FIXUP_ATTRIBUTE
59 # ifdef ELF_MACHINE_RUNTIME_FIXUP_ARGS
60 ELF_MACHINE_RUNTIME_FIXUP_ARGS
,
62 /* GKM FIXME: Fix trampoline to pass bounds so we can do
63 without the `__unbounded' qualifier. */
64 struct link_map
*__unbounded l
, ElfW(Word
) reloc_offset
)
66 const ElfW(Sym
) *const symtab
67 = (const void *) D_PTR (l
, l_info
[DT_SYMTAB
]);
68 const char *strtab
= (const void *) D_PTR (l
, l_info
[DT_STRTAB
]);
70 const PLTREL
*const reloc
71 = (const void *) (D_PTR (l
, l_info
[DT_JMPREL
]) + reloc_offset
);
72 const ElfW(Sym
) *sym
= &symtab
[ELFW(R_SYM
) (reloc
->r_info
)];
73 void *const rel_addr
= (void *)(l
->l_addr
+ reloc
->r_offset
);
75 DL_FIXUP_VALUE_TYPE value
;
77 /* Sanity check that we're really looking at a PLT relocation. */
78 assert (ELFW(R_TYPE
)(reloc
->r_info
) == ELF_MACHINE_JMP_SLOT
);
80 /* Look up the target symbol. If the normal lookup rules are not
81 used don't look in the global scope. */
82 if (__builtin_expect (ELFW(ST_VISIBILITY
) (sym
->st_other
), 0) == 0)
84 const struct r_found_version
*version
= NULL
;
86 if (l
->l_info
[VERSYMIDX (DT_VERSYM
)] != NULL
)
88 const ElfW(Half
) *vernum
=
89 (const void *) D_PTR (l
, l_info
[VERSYMIDX (DT_VERSYM
)]);
90 ElfW(Half
) ndx
= vernum
[ELFW(R_SYM
) (reloc
->r_info
)] & 0x7fff;
91 version
= &l
->l_versions
[ndx
];
92 if (version
->hash
== 0)
96 /* We need to keep the scope around so do some locking. This is
97 not necessary for objects which cannot be unloaded or when
98 we are not using any threads (yet). */
99 int flags
= DL_LOOKUP_ADD_DEPENDENCY
;
100 if (l
->l_type
== lt_loaded
&& !RTLD_SINGLE_THREAD_P
)
102 __rtld_mrlock_lock (l
->l_scope_lock
);
103 flags
|= DL_LOOKUP_SCOPE_LOCK
;
106 result
= _dl_lookup_symbol_x (strtab
+ sym
->st_name
, l
, &sym
, l
->l_scope
,
107 version
, ELF_RTYPE_CLASS_PLT
, flags
, NULL
);
109 if ((flags
& DL_LOOKUP_SCOPE_LOCK
) != 0)
110 __rtld_mrlock_unlock (l
->l_scope_lock
);
112 /* Currently result contains the base load address (or link map)
113 of the object that defines sym. Now add in the symbol
115 value
= DL_FIXUP_MAKE_VALUE (result
,
116 sym
? (LOOKUP_VALUE_ADDRESS (result
)
117 + sym
->st_value
) : 0);
121 /* We already found the symbol. The module (and therefore its load
122 address) is also known. */
123 value
= DL_FIXUP_MAKE_VALUE (l
, l
->l_addr
+ sym
->st_value
);
127 /* And now perhaps the relocation addend. */
128 value
= elf_machine_plt_value (l
, reloc
, value
);
130 /* Finally, fix up the plt itself. */
131 if (__builtin_expect (GLRO(dl_bind_not
), 0))
134 return elf_machine_fixup_plt (l
, result
, reloc
, rel_addr
, value
);
138 #if !defined PROF && !defined ELF_MACHINE_NO_PLT && !__BOUNDED_POINTERS__
141 __attribute ((noinline
)) ARCH_FIXUP_ATTRIBUTE
143 #ifdef ELF_MACHINE_RUNTIME_FIXUP_ARGS
144 ELF_MACHINE_RUNTIME_FIXUP_ARGS
,
146 struct link_map
*l
, ElfW(Word
) reloc_offset
,
147 ElfW(Addr
) retaddr
, void *regs
, long int *framesizep
)
149 void (*mcount_fct
) (ElfW(Addr
), ElfW(Addr
)) = INTUSE(_dl_mcount
);
151 /* This is the address in the array where we store the result of previous
153 struct reloc_result
*reloc_result
154 = &l
->l_reloc_result
[reloc_offset
/ sizeof (PLTREL
)];
155 DL_FIXUP_VALUE_TYPE
*resultp
= &reloc_result
->addr
;
157 DL_FIXUP_VALUE_TYPE value
= *resultp
;
158 if (DL_FIXUP_VALUE_CODE_ADDR (value
) == 0)
160 /* This is the first time we have to relocate this object. */
161 const ElfW(Sym
) *const symtab
162 = (const void *) D_PTR (l
, l_info
[DT_SYMTAB
]);
163 const char *strtab
= (const char *) D_PTR (l
, l_info
[DT_STRTAB
]);
165 const PLTREL
*const reloc
166 = (const void *) (D_PTR (l
, l_info
[DT_JMPREL
]) + reloc_offset
);
167 const ElfW(Sym
) *refsym
= &symtab
[ELFW(R_SYM
) (reloc
->r_info
)];
168 const ElfW(Sym
) *defsym
= refsym
;
171 /* Sanity check that we're really looking at a PLT relocation. */
172 assert (ELFW(R_TYPE
)(reloc
->r_info
) == ELF_MACHINE_JMP_SLOT
);
174 /* Look up the target symbol. If the symbol is marked STV_PROTECTED
175 don't look in the global scope. */
176 if (__builtin_expect (ELFW(ST_VISIBILITY
) (refsym
->st_other
), 0) == 0)
178 const struct r_found_version
*version
= NULL
;
180 if (l
->l_info
[VERSYMIDX (DT_VERSYM
)] != NULL
)
182 const ElfW(Half
) *vernum
=
183 (const void *) D_PTR (l
, l_info
[VERSYMIDX (DT_VERSYM
)]);
184 ElfW(Half
) ndx
= vernum
[ELFW(R_SYM
) (reloc
->r_info
)] & 0x7fff;
185 version
= &l
->l_versions
[ndx
];
186 if (version
->hash
== 0)
190 /* We need to keep the scope around so do some locking. This is
191 not necessary for objects which cannot be unloaded or when
192 we are not using any threads (yet). */
193 int flags
= DL_LOOKUP_ADD_DEPENDENCY
;
194 if (l
->l_type
== lt_loaded
&& !RTLD_SINGLE_THREAD_P
)
196 __rtld_mrlock_lock (l
->l_scope_lock
);
197 flags
|= DL_LOOKUP_SCOPE_LOCK
;
200 result
= _dl_lookup_symbol_x (strtab
+ refsym
->st_name
, l
,
201 &defsym
, l
->l_scope
, version
,
202 ELF_RTYPE_CLASS_PLT
, flags
, NULL
);
204 if ((flags
& DL_LOOKUP_SCOPE_LOCK
) != 0)
205 __rtld_mrlock_unlock (l
->l_scope_lock
);
207 /* Currently result contains the base load address (or link map)
208 of the object that defines sym. Now add in the symbol
210 value
= DL_FIXUP_MAKE_VALUE (result
,
212 ? LOOKUP_VALUE_ADDRESS (result
)
213 + defsym
->st_value
: 0);
217 /* We already found the symbol. The module (and therefore its load
218 address) is also known. */
219 value
= DL_FIXUP_MAKE_VALUE (l
, l
->l_addr
+ refsym
->st_value
);
222 /* And now perhaps the relocation addend. */
223 value
= elf_machine_plt_value (l
, reloc
, value
);
226 /* Auditing checkpoint: we have a new binding. Provide the
227 auditing libraries the possibility to change the value and
228 tell us whether further auditing is wanted. */
229 if (defsym
!= NULL
&& GLRO(dl_naudit
) > 0)
231 reloc_result
->bound
= result
;
232 /* Compute index of the symbol entry in the symbol table of
233 the DSO with the definition. */
234 reloc_result
->boundndx
= (defsym
235 - (ElfW(Sym
) *) D_PTR (result
,
238 /* Determine whether any of the two participating DSOs is
239 interested in auditing. */
240 if ((l
->l_audit_any_plt
| result
->l_audit_any_plt
) != 0)
242 unsigned int altvalue
= 0;
243 struct audit_ifaces
*afct
= GLRO(dl_audit
);
244 /* Synthesize a symbol record where the st_value field is
246 ElfW(Sym
) sym
= *defsym
;
247 sym
.st_value
= DL_FIXUP_VALUE_ADDR (value
);
249 /* Keep track whether there is any interest in tracing
250 the call in the lower two bits. */
251 assert (DL_NNS
* 2 <= sizeof (reloc_result
->flags
) * 8);
252 assert ((LA_SYMB_NOPLTENTER
| LA_SYMB_NOPLTEXIT
) == 3);
253 reloc_result
->enterexit
= LA_SYMB_NOPLTENTER
| LA_SYMB_NOPLTEXIT
;
255 const char *strtab2
= (const void *) D_PTR (result
,
258 for (unsigned int cnt
= 0; cnt
< GLRO(dl_naudit
); ++cnt
)
260 /* XXX Check whether both DSOs must request action or
262 if ((l
->l_audit
[cnt
].bindflags
& LA_FLG_BINDFROM
) != 0
263 && (result
->l_audit
[cnt
].bindflags
& LA_FLG_BINDTO
) != 0)
265 unsigned int flags
= altvalue
;
266 if (afct
->symbind
!= NULL
)
269 = afct
->symbind (&sym
, reloc_result
->boundndx
,
270 &l
->l_audit
[cnt
].cookie
,
271 &result
->l_audit
[cnt
].cookie
,
273 strtab2
+ defsym
->st_name
);
274 if (new_value
!= (uintptr_t) sym
.st_value
)
276 altvalue
= LA_SYMB_ALTVALUE
;
277 sym
.st_value
= new_value
;
281 /* Remember the results for every audit library and
282 store a summary in the first two bits. */
283 reloc_result
->enterexit
284 &= flags
& (LA_SYMB_NOPLTENTER
| LA_SYMB_NOPLTEXIT
);
285 reloc_result
->enterexit
286 |= ((flags
& (LA_SYMB_NOPLTENTER
| LA_SYMB_NOPLTEXIT
))
290 /* If the bind flags say this auditor is not interested,
291 set the bits manually. */
292 reloc_result
->enterexit
293 |= ((LA_SYMB_NOPLTENTER
| LA_SYMB_NOPLTEXIT
)
299 reloc_result
->flags
= altvalue
;
300 value
= DL_FIXUP_ADDR_VALUE (sym
.st_value
);
303 /* Set all bits since this symbol binding is not interesting. */
304 reloc_result
->enterexit
= (1u << DL_NNS
) - 1;
308 /* Store the result for later runs. */
309 if (__builtin_expect (! GLRO(dl_bind_not
), 1))
313 /* By default we do not call the pltexit function. */
314 long int framesize
= -1;
317 /* Auditing checkpoint: report the PLT entering and allow the
318 auditors to change the value. */
319 if (DL_FIXUP_VALUE_CODE_ADDR (value
) != 0 && GLRO(dl_naudit
) > 0
320 /* Don't do anything if no auditor wants to intercept this call. */
321 && (reloc_result
->enterexit
& LA_SYMB_NOPLTENTER
) == 0)
323 ElfW(Sym
) *defsym
= ((ElfW(Sym
) *) D_PTR (reloc_result
->bound
,
325 + reloc_result
->boundndx
);
327 /* Set up the sym parameter. */
328 ElfW(Sym
) sym
= *defsym
;
329 sym
.st_value
= DL_FIXUP_VALUE_ADDR (value
);
331 /* Get the symbol name. */
332 const char *strtab
= (const void *) D_PTR (reloc_result
->bound
,
334 const char *symname
= strtab
+ sym
.st_name
;
336 /* Keep track of overwritten addresses. */
337 unsigned int altvalue
= reloc_result
->flags
;
339 struct audit_ifaces
*afct
= GLRO(dl_audit
);
340 for (unsigned int cnt
= 0; cnt
< GLRO(dl_naudit
); ++cnt
)
342 if (afct
->ARCH_LA_PLTENTER
!= NULL
343 && (reloc_result
->enterexit
344 & (LA_SYMB_NOPLTENTER
<< (2 * (cnt
+ 1)))) == 0)
346 unsigned int flags
= altvalue
;
347 long int new_framesize
= -1;
349 = afct
->ARCH_LA_PLTENTER (&sym
, reloc_result
->boundndx
,
350 &l
->l_audit
[cnt
].cookie
,
351 &reloc_result
->bound
->l_audit
[cnt
].cookie
,
352 regs
, &flags
, symname
,
354 if (new_value
!= (uintptr_t) sym
.st_value
)
356 altvalue
= LA_SYMB_ALTVALUE
;
357 sym
.st_value
= new_value
;
360 /* Remember the results for every audit library and
361 store a summary in the first two bits. */
362 reloc_result
->enterexit
363 |= ((flags
& (LA_SYMB_NOPLTENTER
| LA_SYMB_NOPLTEXIT
))
366 if ((reloc_result
->enterexit
& (LA_SYMB_NOPLTEXIT
368 == 0 && new_framesize
!= -1 && framesize
!= -2)
370 /* If this is the first call providing information,
373 framesize
= new_framesize
;
374 /* If two pltenter calls provide conflicting information,
375 use the larger value. */
376 else if (new_framesize
!= framesize
)
377 framesize
= MAX (new_framesize
, framesize
);
384 value
= DL_FIXUP_ADDR_VALUE (sym
.st_value
);
388 /* Store the frame size information. */
389 *framesizep
= framesize
;
391 (*mcount_fct
) (retaddr
, DL_FIXUP_VALUE_CODE_ADDR (value
));
396 #endif /* PROF && ELF_MACHINE_NO_PLT */
402 _dl_call_pltexit (struct link_map
*l
, ElfW(Word
) reloc_offset
,
403 const void *inregs
, void *outregs
)
406 /* This is the address in the array where we store the result of previous
408 // XXX Maybe the bound information must be stored on the stack since
409 // XXX with bind_not a new value could have been stored in the meantime.
410 struct reloc_result
*reloc_result
411 = &l
->l_reloc_result
[reloc_offset
/ sizeof (PLTREL
)];
412 ElfW(Sym
) *defsym
= ((ElfW(Sym
) *) D_PTR (reloc_result
->bound
,
414 + reloc_result
->boundndx
);
416 /* Set up the sym parameter. */
417 ElfW(Sym
) sym
= *defsym
;
419 /* Get the symbol name. */
420 const char *strtab
= (const void *) D_PTR (reloc_result
->bound
,
422 const char *symname
= strtab
+ sym
.st_name
;
424 struct audit_ifaces
*afct
= GLRO(dl_audit
);
425 for (unsigned int cnt
= 0; cnt
< GLRO(dl_naudit
); ++cnt
)
427 if (afct
->ARCH_LA_PLTEXIT
!= NULL
428 && (reloc_result
->enterexit
429 & (LA_SYMB_NOPLTEXIT
>> (2 * cnt
))) == 0)
431 afct
->ARCH_LA_PLTEXIT (&sym
, reloc_result
->boundndx
,
432 &l
->l_audit
[cnt
].cookie
,
433 &reloc_result
->bound
->l_audit
[cnt
].cookie
,
434 inregs
, outregs
, symname
);