]> git.ipfire.org Git - thirdparty/glibc.git/blob - elf/dl-runtime.c
Update copyright notices with scripts/update-copyrights.
[thirdparty/glibc.git] / elf / dl-runtime.c
1 /* On-demand PLT fixup for shared objects.
2 Copyright (C) 1995-2013 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
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.
9
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.
14
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/>. */
18
19 #define IN_DL_RUNTIME 1 /* This can be tested in dl-machine.h. */
20
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>
30
31
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
38
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
44
45 #ifndef reloc_offset
46 # define reloc_offset reloc_arg
47 # define reloc_index reloc_arg / sizeof (PLTREL)
48 #endif
49
50
51
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. */
58
59 DL_FIXUP_VALUE_TYPE
60 __attribute ((noinline)) ARCH_FIXUP_ATTRIBUTE
61 _dl_fixup (
62 # ifdef ELF_MACHINE_RUNTIME_FIXUP_ARGS
63 ELF_MACHINE_RUNTIME_FIXUP_ARGS,
64 # endif
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)
68 {
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]);
72
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);
77 lookup_t result;
78 DL_FIXUP_VALUE_TYPE value;
79
80 /* Sanity check that we're really looking at a PLT relocation. */
81 assert (ELFW(R_TYPE)(reloc->r_info) == ELF_MACHINE_JMP_SLOT);
82
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)
86 {
87 const struct r_found_version *version = NULL;
88
89 if (l->l_info[VERSYMIDX (DT_VERSYM)] != NULL)
90 {
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)
96 version = NULL;
97 }
98
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)
104 {
105 THREAD_GSCOPE_SET_FLAG ();
106 flags |= DL_LOOKUP_GSCOPE_LOCK;
107 }
108
109 #ifdef RTLD_ENABLE_FOREIGN_CALL
110 RTLD_ENABLE_FOREIGN_CALL;
111 #endif
112
113 result = _dl_lookup_symbol_x (strtab + sym->st_name, l, &sym, l->l_scope,
114 version, ELF_RTYPE_CLASS_PLT, flags, NULL);
115
116 /* We are done with the global scope. */
117 if (!RTLD_SINGLE_THREAD_P)
118 THREAD_GSCOPE_RESET_FLAG ();
119
120 #ifdef RTLD_FINALIZE_FOREIGN_CALL
121 RTLD_FINALIZE_FOREIGN_CALL;
122 #endif
123
124 /* Currently result contains the base load address (or link map)
125 of the object that defines sym. Now add in the symbol
126 offset. */
127 value = DL_FIXUP_MAKE_VALUE (result,
128 sym ? (LOOKUP_VALUE_ADDRESS (result)
129 + sym->st_value) : 0);
130 }
131 else
132 {
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);
136 result = l;
137 }
138
139 /* And now perhaps the relocation addend. */
140 value = elf_machine_plt_value (l, reloc, value);
141
142 if (sym != NULL
143 && __builtin_expect (ELFW(ST_TYPE) (sym->st_info) == STT_GNU_IFUNC, 0))
144 value = elf_ifunc_invoke (DL_FIXUP_VALUE_ADDR (value));
145
146 /* Finally, fix up the plt itself. */
147 if (__builtin_expect (GLRO(dl_bind_not), 0))
148 return value;
149
150 return elf_machine_fixup_plt (l, result, reloc, rel_addr, value);
151 }
152
153 #if !defined PROF && !__BOUNDED_POINTERS__
154 DL_FIXUP_VALUE_TYPE
155 __attribute ((noinline)) ARCH_FIXUP_ATTRIBUTE
156 _dl_profile_fixup (
157 #ifdef ELF_MACHINE_RUNTIME_FIXUP_ARGS
158 ELF_MACHINE_RUNTIME_FIXUP_ARGS,
159 #endif
160 struct link_map *l, ElfW(Word) reloc_arg,
161 ElfW(Addr) retaddr, void *regs, long int *framesizep)
162 {
163 void (*mcount_fct) (ElfW(Addr), ElfW(Addr)) = INTUSE(_dl_mcount);
164
165 if (l->l_reloc_result == NULL)
166 {
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
173 in this case. */
174 *framesizep = -1;
175 return _dl_fixup (
176 # ifdef ELF_MACHINE_RUNTIME_FIXUP_ARGS
177 # ifndef ELF_MACHINE_RUNTIME_FIXUP_PARAMS
178 # error Please define ELF_MACHINE_RUNTIME_FIXUP_PARAMS.
179 # endif
180 ELF_MACHINE_RUNTIME_FIXUP_PARAMS,
181 # endif
182 l, reloc_arg);
183 }
184
185 /* This is the address in the array where we store the result of previous
186 relocations. */
187 struct reloc_result *reloc_result = &l->l_reloc_result[reloc_index];
188 DL_FIXUP_VALUE_TYPE *resultp = &reloc_result->addr;
189
190 DL_FIXUP_VALUE_TYPE value = *resultp;
191 if (DL_FIXUP_VALUE_CODE_ADDR (value) == 0)
192 {
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]);
197
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;
202 lookup_t result;
203
204 /* Sanity check that we're really looking at a PLT relocation. */
205 assert (ELFW(R_TYPE)(reloc->r_info) == ELF_MACHINE_JMP_SLOT);
206
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)
210 {
211 const struct r_found_version *version = NULL;
212
213 if (l->l_info[VERSYMIDX (DT_VERSYM)] != NULL)
214 {
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)
220 version = NULL;
221 }
222
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)
228 {
229 THREAD_GSCOPE_SET_FLAG ();
230 flags |= DL_LOOKUP_GSCOPE_LOCK;
231 }
232
233 result = _dl_lookup_symbol_x (strtab + refsym->st_name, l,
234 &defsym, l->l_scope, version,
235 ELF_RTYPE_CLASS_PLT, flags, NULL);
236
237 /* We are done with the global scope. */
238 if (!RTLD_SINGLE_THREAD_P)
239 THREAD_GSCOPE_RESET_FLAG ();
240
241 /* Currently result contains the base load address (or link map)
242 of the object that defines sym. Now add in the symbol
243 offset. */
244 value = DL_FIXUP_MAKE_VALUE (result,
245 defsym != NULL
246 ? LOOKUP_VALUE_ADDRESS (result)
247 + defsym->st_value : 0);
248
249 if (defsym != NULL
250 && __builtin_expect (ELFW(ST_TYPE) (defsym->st_info)
251 == STT_GNU_IFUNC, 0))
252 value = elf_ifunc_invoke (DL_FIXUP_VALUE_ADDR (value));
253 }
254 else
255 {
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);
259
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));
263
264 result = l;
265 }
266 /* And now perhaps the relocation addend. */
267 value = elf_machine_plt_value (l, reloc, value);
268
269 #ifdef SHARED
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)
274 {
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,
280 l_info[DT_SYMTAB]));
281
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)
285 {
286 unsigned int flags = 0;
287 struct audit_ifaces *afct = GLRO(dl_audit);
288 /* Synthesize a symbol record where the st_value field is
289 the result. */
290 ElfW(Sym) sym = *defsym;
291 sym.st_value = DL_FIXUP_VALUE_ADDR (value);
292
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;
298
299 const char *strtab2 = (const void *) D_PTR (result,
300 l_info[DT_STRTAB]);
301
302 for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
303 {
304 /* XXX Check whether both DSOs must request action or
305 only one */
306 if ((l->l_audit[cnt].bindflags & LA_FLG_BINDFROM) != 0
307 && (result->l_audit[cnt].bindflags & LA_FLG_BINDTO) != 0)
308 {
309 if (afct->symbind != NULL)
310 {
311 uintptr_t new_value
312 = afct->symbind (&sym, reloc_result->boundndx,
313 &l->l_audit[cnt].cookie,
314 &result->l_audit[cnt].cookie,
315 &flags,
316 strtab2 + defsym->st_name);
317 if (new_value != (uintptr_t) sym.st_value)
318 {
319 flags |= LA_SYMB_ALTVALUE;
320 sym.st_value = new_value;
321 }
322 }
323
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))
330 << ((cnt + 1) * 2));
331 }
332 else
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)
337 << ((cnt + 1) * 2));
338
339 afct = afct->next;
340 }
341
342 reloc_result->flags = flags;
343 value = DL_FIXUP_ADDR_VALUE (sym.st_value);
344 }
345 else
346 /* Set all bits since this symbol binding is not interesting. */
347 reloc_result->enterexit = (1u << DL_NNS) - 1;
348 }
349 #endif
350
351 /* Store the result for later runs. */
352 if (__builtin_expect (! GLRO(dl_bind_not), 1))
353 *resultp = value;
354 }
355
356 /* By default we do not call the pltexit function. */
357 long int framesize = -1;
358
359 #ifdef SHARED
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)
365 {
366 ElfW(Sym) *defsym = ((ElfW(Sym) *) D_PTR (reloc_result->bound,
367 l_info[DT_SYMTAB])
368 + reloc_result->boundndx);
369
370 /* Set up the sym parameter. */
371 ElfW(Sym) sym = *defsym;
372 sym.st_value = DL_FIXUP_VALUE_ADDR (value);
373
374 /* Get the symbol name. */
375 const char *strtab = (const void *) D_PTR (reloc_result->bound,
376 l_info[DT_STRTAB]);
377 const char *symname = strtab + sym.st_name;
378
379 /* Keep track of overwritten addresses. */
380 unsigned int flags = reloc_result->flags;
381
382 struct audit_ifaces *afct = GLRO(dl_audit);
383 for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
384 {
385 if (afct->ARCH_LA_PLTENTER != NULL
386 && (reloc_result->enterexit
387 & (LA_SYMB_NOPLTENTER << (2 * (cnt + 1)))) == 0)
388 {
389 long int new_framesize = -1;
390 uintptr_t new_value
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,
395 &new_framesize);
396 if (new_value != (uintptr_t) sym.st_value)
397 {
398 flags |= LA_SYMB_ALTVALUE;
399 sym.st_value = new_value;
400 }
401
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))
406 << (2 * (cnt + 1)));
407
408 if ((reloc_result->enterexit & (LA_SYMB_NOPLTEXIT
409 << (2 * (cnt + 1))))
410 == 0 && new_framesize != -1 && framesize != -2)
411 {
412 /* If this is the first call providing information,
413 use it. */
414 if (framesize == -1)
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);
420 }
421 }
422
423 afct = afct->next;
424 }
425
426 value = DL_FIXUP_ADDR_VALUE (sym.st_value);
427 }
428 #endif
429
430 /* Store the frame size information. */
431 *framesizep = framesize;
432
433 (*mcount_fct) (retaddr, DL_FIXUP_VALUE_CODE_ADDR (value));
434
435 return value;
436 }
437
438 #endif /* PROF */
439
440
441 #include <stdio.h>
442 void
443 ARCH_FIXUP_ATTRIBUTE
444 _dl_call_pltexit (struct link_map *l, ElfW(Word) reloc_arg,
445 const void *inregs, void *outregs)
446 {
447 #ifdef SHARED
448 /* This is the address in the array where we store the result of previous
449 relocations. */
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,
454 l_info[DT_SYMTAB])
455 + reloc_result->boundndx);
456
457 /* Set up the sym parameter. */
458 ElfW(Sym) sym = *defsym;
459 sym.st_value = DL_FIXUP_VALUE_ADDR (reloc_result->addr);
460
461 /* Get the symbol name. */
462 const char *strtab = (const void *) D_PTR (reloc_result->bound,
463 l_info[DT_STRTAB]);
464 const char *symname = strtab + sym.st_name;
465
466 struct audit_ifaces *afct = GLRO(dl_audit);
467 for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
468 {
469 if (afct->ARCH_LA_PLTEXIT != NULL
470 && (reloc_result->enterexit
471 & (LA_SYMB_NOPLTEXIT >> (2 * cnt))) == 0)
472 {
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);
477 }
478
479 afct = afct->next;
480 }
481 #endif
482 }