1 /* On-demand PLT fixup for shared objects.
2 Copyright (C) 1995-2012 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. */
24 #include <sys/param.h>
26 #include <sysdep-cancel.h>
27 #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)
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
46 # define reloc_offset reloc_arg
47 # define reloc_index reloc_arg / sizeof (PLTREL)
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
60 __attribute ((noinline
)) ARCH_FIXUP_ATTRIBUTE
62 # ifdef ELF_MACHINE_RUNTIME_FIXUP_ARGS
63 ELF_MACHINE_RUNTIME_FIXUP_ARGS
,
65 /* GKM FIXME: Fix trampoline to pass bounds so we can do
66 without the `__unbounded' qualifier. */
67 struct link_map
*__unbounded l
, ElfW(Word
) reloc_arg
)
69 const ElfW(Sym
) *const symtab
70 = (const void *) D_PTR (l
, l_info
[DT_SYMTAB
]);
71 const char *strtab
= (const void *) D_PTR (l
, l_info
[DT_STRTAB
]);
73 const PLTREL
*const reloc
74 = (const void *) (D_PTR (l
, l_info
[DT_JMPREL
]) + reloc_offset
);
75 const ElfW(Sym
) *sym
= &symtab
[ELFW(R_SYM
) (reloc
->r_info
)];
76 void *const rel_addr
= (void *)(l
->l_addr
+ reloc
->r_offset
);
78 DL_FIXUP_VALUE_TYPE value
;
80 /* Sanity check that we're really looking at a PLT relocation. */
81 assert (ELFW(R_TYPE
)(reloc
->r_info
) == ELF_MACHINE_JMP_SLOT
);
83 /* Look up the target symbol. If the normal lookup rules are not
84 used don't look in the global scope. */
85 if (__builtin_expect (ELFW(ST_VISIBILITY
) (sym
->st_other
), 0) == 0)
87 const struct r_found_version
*version
= NULL
;
89 if (l
->l_info
[VERSYMIDX (DT_VERSYM
)] != NULL
)
91 const ElfW(Half
) *vernum
=
92 (const void *) D_PTR (l
, l_info
[VERSYMIDX (DT_VERSYM
)]);
93 ElfW(Half
) ndx
= vernum
[ELFW(R_SYM
) (reloc
->r_info
)] & 0x7fff;
94 version
= &l
->l_versions
[ndx
];
95 if (version
->hash
== 0)
99 /* We need to keep the scope around so do some locking. This is
100 not necessary for objects which cannot be unloaded or when
101 we are not using any threads (yet). */
102 int flags
= DL_LOOKUP_ADD_DEPENDENCY
;
103 if (!RTLD_SINGLE_THREAD_P
)
105 THREAD_GSCOPE_SET_FLAG ();
106 flags
|= DL_LOOKUP_GSCOPE_LOCK
;
109 #ifdef RTLD_ENABLE_FOREIGN_CALL
110 RTLD_ENABLE_FOREIGN_CALL
;
113 result
= _dl_lookup_symbol_x (strtab
+ sym
->st_name
, l
, &sym
, l
->l_scope
,
114 version
, ELF_RTYPE_CLASS_PLT
, flags
, NULL
);
116 /* We are done with the global scope. */
117 if (!RTLD_SINGLE_THREAD_P
)
118 THREAD_GSCOPE_RESET_FLAG ();
120 #ifdef RTLD_FINALIZE_FOREIGN_CALL
121 RTLD_FINALIZE_FOREIGN_CALL
;
124 /* Currently result contains the base load address (or link map)
125 of the object that defines sym. Now add in the symbol
127 value
= DL_FIXUP_MAKE_VALUE (result
,
128 sym
? (LOOKUP_VALUE_ADDRESS (result
)
129 + sym
->st_value
) : 0);
133 /* We already found the symbol. The module (and therefore its load
134 address) is also known. */
135 value
= DL_FIXUP_MAKE_VALUE (l
, l
->l_addr
+ sym
->st_value
);
139 /* And now perhaps the relocation addend. */
140 value
= elf_machine_plt_value (l
, reloc
, value
);
143 && __builtin_expect (ELFW(ST_TYPE
) (sym
->st_info
) == STT_GNU_IFUNC
, 0))
144 value
= elf_ifunc_invoke (DL_FIXUP_VALUE_ADDR (value
));
146 /* Finally, fix up the plt itself. */
147 if (__builtin_expect (GLRO(dl_bind_not
), 0))
150 return elf_machine_fixup_plt (l
, result
, reloc
, rel_addr
, value
);
153 #if !defined PROF && !__BOUNDED_POINTERS__
155 __attribute ((noinline
)) ARCH_FIXUP_ATTRIBUTE
157 #ifdef ELF_MACHINE_RUNTIME_FIXUP_ARGS
158 ELF_MACHINE_RUNTIME_FIXUP_ARGS
,
160 struct link_map
*l
, ElfW(Word
) reloc_arg
,
161 ElfW(Addr
) retaddr
, void *regs
, long int *framesizep
)
163 void (*mcount_fct
) (ElfW(Addr
), ElfW(Addr
)) = INTUSE(_dl_mcount
);
165 if (l
->l_reloc_result
== NULL
)
167 /* BZ #14843: ELF_DYNAMIC_RELOCATE is called before l_reloc_result
168 is allocated. We will get here if ELF_DYNAMIC_RELOCATE calls a
169 resolver function to resolve an IRELATIVE relocation and that
170 resolver calls a function that is not yet resolved (lazy). For
171 example, the resolver in x86-64 libm.so calls __get_cpu_features
172 defined in libc.so. Skip audit and resolve the external function
176 # ifdef ELF_MACHINE_RUNTIME_FIXUP_ARGS
177 # ifndef ELF_MACHINE_RUNTIME_FIXUP_PARAMS
178 # error Please define ELF_MACHINE_RUNTIME_FIXUP_PARAMS.
180 ELF_MACHINE_RUNTIME_FIXUP_PARAMS
,
185 /* This is the address in the array where we store the result of previous
187 struct reloc_result
*reloc_result
= &l
->l_reloc_result
[reloc_index
];
188 DL_FIXUP_VALUE_TYPE
*resultp
= &reloc_result
->addr
;
190 DL_FIXUP_VALUE_TYPE value
= *resultp
;
191 if (DL_FIXUP_VALUE_CODE_ADDR (value
) == 0)
193 /* This is the first time we have to relocate this object. */
194 const ElfW(Sym
) *const symtab
195 = (const void *) D_PTR (l
, l_info
[DT_SYMTAB
]);
196 const char *strtab
= (const char *) D_PTR (l
, l_info
[DT_STRTAB
]);
198 const PLTREL
*const reloc
199 = (const void *) (D_PTR (l
, l_info
[DT_JMPREL
]) + reloc_offset
);
200 const ElfW(Sym
) *refsym
= &symtab
[ELFW(R_SYM
) (reloc
->r_info
)];
201 const ElfW(Sym
) *defsym
= refsym
;
204 /* Sanity check that we're really looking at a PLT relocation. */
205 assert (ELFW(R_TYPE
)(reloc
->r_info
) == ELF_MACHINE_JMP_SLOT
);
207 /* Look up the target symbol. If the symbol is marked STV_PROTECTED
208 don't look in the global scope. */
209 if (__builtin_expect (ELFW(ST_VISIBILITY
) (refsym
->st_other
), 0) == 0)
211 const struct r_found_version
*version
= NULL
;
213 if (l
->l_info
[VERSYMIDX (DT_VERSYM
)] != NULL
)
215 const ElfW(Half
) *vernum
=
216 (const void *) D_PTR (l
, l_info
[VERSYMIDX (DT_VERSYM
)]);
217 ElfW(Half
) ndx
= vernum
[ELFW(R_SYM
) (reloc
->r_info
)] & 0x7fff;
218 version
= &l
->l_versions
[ndx
];
219 if (version
->hash
== 0)
223 /* We need to keep the scope around so do some locking. This is
224 not necessary for objects which cannot be unloaded or when
225 we are not using any threads (yet). */
226 int flags
= DL_LOOKUP_ADD_DEPENDENCY
;
227 if (!RTLD_SINGLE_THREAD_P
)
229 THREAD_GSCOPE_SET_FLAG ();
230 flags
|= DL_LOOKUP_GSCOPE_LOCK
;
233 result
= _dl_lookup_symbol_x (strtab
+ refsym
->st_name
, l
,
234 &defsym
, l
->l_scope
, version
,
235 ELF_RTYPE_CLASS_PLT
, flags
, NULL
);
237 /* We are done with the global scope. */
238 if (!RTLD_SINGLE_THREAD_P
)
239 THREAD_GSCOPE_RESET_FLAG ();
241 /* Currently result contains the base load address (or link map)
242 of the object that defines sym. Now add in the symbol
244 value
= DL_FIXUP_MAKE_VALUE (result
,
246 ? LOOKUP_VALUE_ADDRESS (result
)
247 + defsym
->st_value
: 0);
250 && __builtin_expect (ELFW(ST_TYPE
) (defsym
->st_info
)
251 == STT_GNU_IFUNC
, 0))
252 value
= elf_ifunc_invoke (DL_FIXUP_VALUE_ADDR (value
));
256 /* We already found the symbol. The module (and therefore its load
257 address) is also known. */
258 value
= DL_FIXUP_MAKE_VALUE (l
, l
->l_addr
+ refsym
->st_value
);
260 if (__builtin_expect (ELFW(ST_TYPE
) (refsym
->st_info
)
261 == STT_GNU_IFUNC
, 0))
262 value
= elf_ifunc_invoke (DL_FIXUP_VALUE_ADDR (value
));
266 /* And now perhaps the relocation addend. */
267 value
= elf_machine_plt_value (l
, reloc
, value
);
270 /* Auditing checkpoint: we have a new binding. Provide the
271 auditing libraries the possibility to change the value and
272 tell us whether further auditing is wanted. */
273 if (defsym
!= NULL
&& GLRO(dl_naudit
) > 0)
275 reloc_result
->bound
= result
;
276 /* Compute index of the symbol entry in the symbol table of
277 the DSO with the definition. */
278 reloc_result
->boundndx
= (defsym
279 - (ElfW(Sym
) *) D_PTR (result
,
282 /* Determine whether any of the two participating DSOs is
283 interested in auditing. */
284 if ((l
->l_audit_any_plt
| result
->l_audit_any_plt
) != 0)
286 unsigned int flags
= 0;
287 struct audit_ifaces
*afct
= GLRO(dl_audit
);
288 /* Synthesize a symbol record where the st_value field is
290 ElfW(Sym
) sym
= *defsym
;
291 sym
.st_value
= DL_FIXUP_VALUE_ADDR (value
);
293 /* Keep track whether there is any interest in tracing
294 the call in the lower two bits. */
295 assert (DL_NNS
* 2 <= sizeof (reloc_result
->flags
) * 8);
296 assert ((LA_SYMB_NOPLTENTER
| LA_SYMB_NOPLTEXIT
) == 3);
297 reloc_result
->enterexit
= LA_SYMB_NOPLTENTER
| LA_SYMB_NOPLTEXIT
;
299 const char *strtab2
= (const void *) D_PTR (result
,
302 for (unsigned int cnt
= 0; cnt
< GLRO(dl_naudit
); ++cnt
)
304 /* XXX Check whether both DSOs must request action or
306 if ((l
->l_audit
[cnt
].bindflags
& LA_FLG_BINDFROM
) != 0
307 && (result
->l_audit
[cnt
].bindflags
& LA_FLG_BINDTO
) != 0)
309 if (afct
->symbind
!= NULL
)
312 = afct
->symbind (&sym
, reloc_result
->boundndx
,
313 &l
->l_audit
[cnt
].cookie
,
314 &result
->l_audit
[cnt
].cookie
,
316 strtab2
+ defsym
->st_name
);
317 if (new_value
!= (uintptr_t) sym
.st_value
)
319 flags
|= LA_SYMB_ALTVALUE
;
320 sym
.st_value
= new_value
;
324 /* Remember the results for every audit library and
325 store a summary in the first two bits. */
326 reloc_result
->enterexit
327 &= flags
& (LA_SYMB_NOPLTENTER
| LA_SYMB_NOPLTEXIT
);
328 reloc_result
->enterexit
329 |= ((flags
& (LA_SYMB_NOPLTENTER
| LA_SYMB_NOPLTEXIT
))
333 /* If the bind flags say this auditor is not interested,
334 set the bits manually. */
335 reloc_result
->enterexit
336 |= ((LA_SYMB_NOPLTENTER
| LA_SYMB_NOPLTEXIT
)
342 reloc_result
->flags
= flags
;
343 value
= DL_FIXUP_ADDR_VALUE (sym
.st_value
);
346 /* Set all bits since this symbol binding is not interesting. */
347 reloc_result
->enterexit
= (1u << DL_NNS
) - 1;
351 /* Store the result for later runs. */
352 if (__builtin_expect (! GLRO(dl_bind_not
), 1))
356 /* By default we do not call the pltexit function. */
357 long int framesize
= -1;
360 /* Auditing checkpoint: report the PLT entering and allow the
361 auditors to change the value. */
362 if (DL_FIXUP_VALUE_CODE_ADDR (value
) != 0 && GLRO(dl_naudit
) > 0
363 /* Don't do anything if no auditor wants to intercept this call. */
364 && (reloc_result
->enterexit
& LA_SYMB_NOPLTENTER
) == 0)
366 ElfW(Sym
) *defsym
= ((ElfW(Sym
) *) D_PTR (reloc_result
->bound
,
368 + reloc_result
->boundndx
);
370 /* Set up the sym parameter. */
371 ElfW(Sym
) sym
= *defsym
;
372 sym
.st_value
= DL_FIXUP_VALUE_ADDR (value
);
374 /* Get the symbol name. */
375 const char *strtab
= (const void *) D_PTR (reloc_result
->bound
,
377 const char *symname
= strtab
+ sym
.st_name
;
379 /* Keep track of overwritten addresses. */
380 unsigned int flags
= reloc_result
->flags
;
382 struct audit_ifaces
*afct
= GLRO(dl_audit
);
383 for (unsigned int cnt
= 0; cnt
< GLRO(dl_naudit
); ++cnt
)
385 if (afct
->ARCH_LA_PLTENTER
!= NULL
386 && (reloc_result
->enterexit
387 & (LA_SYMB_NOPLTENTER
<< (2 * (cnt
+ 1)))) == 0)
389 long int new_framesize
= -1;
391 = afct
->ARCH_LA_PLTENTER (&sym
, reloc_result
->boundndx
,
392 &l
->l_audit
[cnt
].cookie
,
393 &reloc_result
->bound
->l_audit
[cnt
].cookie
,
394 regs
, &flags
, symname
,
396 if (new_value
!= (uintptr_t) sym
.st_value
)
398 flags
|= LA_SYMB_ALTVALUE
;
399 sym
.st_value
= new_value
;
402 /* Remember the results for every audit library and
403 store a summary in the first two bits. */
404 reloc_result
->enterexit
405 |= ((flags
& (LA_SYMB_NOPLTENTER
| LA_SYMB_NOPLTEXIT
))
408 if ((reloc_result
->enterexit
& (LA_SYMB_NOPLTEXIT
410 == 0 && new_framesize
!= -1 && framesize
!= -2)
412 /* If this is the first call providing information,
415 framesize
= new_framesize
;
416 /* If two pltenter calls provide conflicting information,
417 use the larger value. */
418 else if (new_framesize
!= framesize
)
419 framesize
= MAX (new_framesize
, framesize
);
426 value
= DL_FIXUP_ADDR_VALUE (sym
.st_value
);
430 /* Store the frame size information. */
431 *framesizep
= framesize
;
433 (*mcount_fct
) (retaddr
, DL_FIXUP_VALUE_CODE_ADDR (value
));
444 _dl_call_pltexit (struct link_map
*l
, ElfW(Word
) reloc_arg
,
445 const void *inregs
, void *outregs
)
448 /* This is the address in the array where we store the result of previous
450 // XXX Maybe the bound information must be stored on the stack since
451 // XXX with bind_not a new value could have been stored in the meantime.
452 struct reloc_result
*reloc_result
= &l
->l_reloc_result
[reloc_index
];
453 ElfW(Sym
) *defsym
= ((ElfW(Sym
) *) D_PTR (reloc_result
->bound
,
455 + reloc_result
->boundndx
);
457 /* Set up the sym parameter. */
458 ElfW(Sym
) sym
= *defsym
;
459 sym
.st_value
= DL_FIXUP_VALUE_ADDR (reloc_result
->addr
);
461 /* Get the symbol name. */
462 const char *strtab
= (const void *) D_PTR (reloc_result
->bound
,
464 const char *symname
= strtab
+ sym
.st_name
;
466 struct audit_ifaces
*afct
= GLRO(dl_audit
);
467 for (unsigned int cnt
= 0; cnt
< GLRO(dl_naudit
); ++cnt
)
469 if (afct
->ARCH_LA_PLTEXIT
!= NULL
470 && (reloc_result
->enterexit
471 & (LA_SYMB_NOPLTEXIT
>> (2 * cnt
))) == 0)
473 afct
->ARCH_LA_PLTEXIT (&sym
, reloc_result
->boundndx
,
474 &l
->l_audit
[cnt
].cookie
,
475 &reloc_result
->bound
->l_audit
[cnt
].cookie
,
476 inregs
, outregs
, symname
);