]> git.ipfire.org Git - thirdparty/glibc.git/blob - elf/dl-runtime.c
Skip audit if l_reloc_result is NULL
[thirdparty/glibc.git] / elf / dl-runtime.c
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.
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 #ifndef ELF_MACHINE_NO_PLT
60 DL_FIXUP_VALUE_TYPE
61 __attribute ((noinline)) ARCH_FIXUP_ATTRIBUTE
62 _dl_fixup (
63 # ifdef ELF_MACHINE_RUNTIME_FIXUP_ARGS
64 ELF_MACHINE_RUNTIME_FIXUP_ARGS,
65 # endif
66 /* GKM FIXME: Fix trampoline to pass bounds so we can do
67 without the `__unbounded' qualifier. */
68 struct link_map *__unbounded l, ElfW(Word) reloc_arg)
69 {
70 const ElfW(Sym) *const symtab
71 = (const void *) D_PTR (l, l_info[DT_SYMTAB]);
72 const char *strtab = (const void *) D_PTR (l, l_info[DT_STRTAB]);
73
74 const PLTREL *const reloc
75 = (const void *) (D_PTR (l, l_info[DT_JMPREL]) + reloc_offset);
76 const ElfW(Sym) *sym = &symtab[ELFW(R_SYM) (reloc->r_info)];
77 void *const rel_addr = (void *)(l->l_addr + reloc->r_offset);
78 lookup_t result;
79 DL_FIXUP_VALUE_TYPE value;
80
81 /* Sanity check that we're really looking at a PLT relocation. */
82 assert (ELFW(R_TYPE)(reloc->r_info) == ELF_MACHINE_JMP_SLOT);
83
84 /* Look up the target symbol. If the normal lookup rules are not
85 used don't look in the global scope. */
86 if (__builtin_expect (ELFW(ST_VISIBILITY) (sym->st_other), 0) == 0)
87 {
88 const struct r_found_version *version = NULL;
89
90 if (l->l_info[VERSYMIDX (DT_VERSYM)] != NULL)
91 {
92 const ElfW(Half) *vernum =
93 (const void *) D_PTR (l, l_info[VERSYMIDX (DT_VERSYM)]);
94 ElfW(Half) ndx = vernum[ELFW(R_SYM) (reloc->r_info)] & 0x7fff;
95 version = &l->l_versions[ndx];
96 if (version->hash == 0)
97 version = NULL;
98 }
99
100 /* We need to keep the scope around so do some locking. This is
101 not necessary for objects which cannot be unloaded or when
102 we are not using any threads (yet). */
103 int flags = DL_LOOKUP_ADD_DEPENDENCY;
104 if (!RTLD_SINGLE_THREAD_P)
105 {
106 THREAD_GSCOPE_SET_FLAG ();
107 flags |= DL_LOOKUP_GSCOPE_LOCK;
108 }
109
110 #ifdef RTLD_ENABLE_FOREIGN_CALL
111 RTLD_ENABLE_FOREIGN_CALL;
112 #endif
113
114 result = _dl_lookup_symbol_x (strtab + sym->st_name, l, &sym, l->l_scope,
115 version, ELF_RTYPE_CLASS_PLT, flags, NULL);
116
117 /* We are done with the global scope. */
118 if (!RTLD_SINGLE_THREAD_P)
119 THREAD_GSCOPE_RESET_FLAG ();
120
121 #ifdef RTLD_FINALIZE_FOREIGN_CALL
122 RTLD_FINALIZE_FOREIGN_CALL;
123 #endif
124
125 /* Currently result contains the base load address (or link map)
126 of the object that defines sym. Now add in the symbol
127 offset. */
128 value = DL_FIXUP_MAKE_VALUE (result,
129 sym ? (LOOKUP_VALUE_ADDRESS (result)
130 + sym->st_value) : 0);
131 }
132 else
133 {
134 /* We already found the symbol. The module (and therefore its load
135 address) is also known. */
136 value = DL_FIXUP_MAKE_VALUE (l, l->l_addr + sym->st_value);
137 result = l;
138 }
139
140 /* And now perhaps the relocation addend. */
141 value = elf_machine_plt_value (l, reloc, value);
142
143 if (sym != NULL
144 && __builtin_expect (ELFW(ST_TYPE) (sym->st_info) == STT_GNU_IFUNC, 0))
145 value = elf_ifunc_invoke (DL_FIXUP_VALUE_ADDR (value));
146
147 /* Finally, fix up the plt itself. */
148 if (__builtin_expect (GLRO(dl_bind_not), 0))
149 return value;
150
151 return elf_machine_fixup_plt (l, result, reloc, rel_addr, value);
152 }
153 #endif
154
155 #if !defined PROF && !defined ELF_MACHINE_NO_PLT && !__BOUNDED_POINTERS__
156 DL_FIXUP_VALUE_TYPE
157 __attribute ((noinline)) ARCH_FIXUP_ATTRIBUTE
158 _dl_profile_fixup (
159 #ifdef ELF_MACHINE_RUNTIME_FIXUP_ARGS
160 ELF_MACHINE_RUNTIME_FIXUP_ARGS,
161 #endif
162 struct link_map *l, ElfW(Word) reloc_arg,
163 ElfW(Addr) retaddr, void *regs, long int *framesizep)
164 {
165 void (*mcount_fct) (ElfW(Addr), ElfW(Addr)) = INTUSE(_dl_mcount);
166
167 if (l->l_reloc_result == NULL)
168 {
169 /* BZ #14843: ELF_DYNAMIC_RELOCATE is called before l_reloc_result
170 is allocated. We will get here if ELF_DYNAMIC_RELOCATE calls a
171 resolver function to resolve an IRELATIVE relocation and that
172 resolver calls a function that is not yet resolved (lazy). For
173 example, the resolver in x86-64 libm.so calls __get_cpu_features
174 defined in libc.so. Skip audit and resolve the external function
175 in this case. */
176 *framesizep = -1;
177 return _dl_fixup (
178 # ifdef ELF_MACHINE_RUNTIME_FIXUP_ARGS
179 # ifndef ELF_MACHINE_RUNTIME_FIXUP_PARAMS
180 # error Please define ELF_MACHINE_RUNTIME_FIXUP_PARAMS.
181 # endif
182 ELF_MACHINE_RUNTIME_FIXUP_PARAMS,
183 # endif
184 l, reloc_arg);
185 }
186
187 /* This is the address in the array where we store the result of previous
188 relocations. */
189 struct reloc_result *reloc_result = &l->l_reloc_result[reloc_index];
190 DL_FIXUP_VALUE_TYPE *resultp = &reloc_result->addr;
191
192 DL_FIXUP_VALUE_TYPE value = *resultp;
193 if (DL_FIXUP_VALUE_CODE_ADDR (value) == 0)
194 {
195 /* This is the first time we have to relocate this object. */
196 const ElfW(Sym) *const symtab
197 = (const void *) D_PTR (l, l_info[DT_SYMTAB]);
198 const char *strtab = (const char *) D_PTR (l, l_info[DT_STRTAB]);
199
200 const PLTREL *const reloc
201 = (const void *) (D_PTR (l, l_info[DT_JMPREL]) + reloc_offset);
202 const ElfW(Sym) *refsym = &symtab[ELFW(R_SYM) (reloc->r_info)];
203 const ElfW(Sym) *defsym = refsym;
204 lookup_t result;
205
206 /* Sanity check that we're really looking at a PLT relocation. */
207 assert (ELFW(R_TYPE)(reloc->r_info) == ELF_MACHINE_JMP_SLOT);
208
209 /* Look up the target symbol. If the symbol is marked STV_PROTECTED
210 don't look in the global scope. */
211 if (__builtin_expect (ELFW(ST_VISIBILITY) (refsym->st_other), 0) == 0)
212 {
213 const struct r_found_version *version = NULL;
214
215 if (l->l_info[VERSYMIDX (DT_VERSYM)] != NULL)
216 {
217 const ElfW(Half) *vernum =
218 (const void *) D_PTR (l, l_info[VERSYMIDX (DT_VERSYM)]);
219 ElfW(Half) ndx = vernum[ELFW(R_SYM) (reloc->r_info)] & 0x7fff;
220 version = &l->l_versions[ndx];
221 if (version->hash == 0)
222 version = NULL;
223 }
224
225 /* We need to keep the scope around so do some locking. This is
226 not necessary for objects which cannot be unloaded or when
227 we are not using any threads (yet). */
228 int flags = DL_LOOKUP_ADD_DEPENDENCY;
229 if (!RTLD_SINGLE_THREAD_P)
230 {
231 THREAD_GSCOPE_SET_FLAG ();
232 flags |= DL_LOOKUP_GSCOPE_LOCK;
233 }
234
235 result = _dl_lookup_symbol_x (strtab + refsym->st_name, l,
236 &defsym, l->l_scope, version,
237 ELF_RTYPE_CLASS_PLT, flags, NULL);
238
239 /* We are done with the global scope. */
240 if (!RTLD_SINGLE_THREAD_P)
241 THREAD_GSCOPE_RESET_FLAG ();
242
243 /* Currently result contains the base load address (or link map)
244 of the object that defines sym. Now add in the symbol
245 offset. */
246 value = DL_FIXUP_MAKE_VALUE (result,
247 defsym != NULL
248 ? LOOKUP_VALUE_ADDRESS (result)
249 + defsym->st_value : 0);
250
251 if (defsym != NULL
252 && __builtin_expect (ELFW(ST_TYPE) (defsym->st_info)
253 == STT_GNU_IFUNC, 0))
254 value = elf_ifunc_invoke (DL_FIXUP_VALUE_ADDR (value));
255 }
256 else
257 {
258 /* We already found the symbol. The module (and therefore its load
259 address) is also known. */
260 value = DL_FIXUP_MAKE_VALUE (l, l->l_addr + refsym->st_value);
261
262 if (__builtin_expect (ELFW(ST_TYPE) (refsym->st_info)
263 == STT_GNU_IFUNC, 0))
264 value = elf_ifunc_invoke (DL_FIXUP_VALUE_ADDR (value));
265
266 result = l;
267 }
268 /* And now perhaps the relocation addend. */
269 value = elf_machine_plt_value (l, reloc, value);
270
271 #ifdef SHARED
272 /* Auditing checkpoint: we have a new binding. Provide the
273 auditing libraries the possibility to change the value and
274 tell us whether further auditing is wanted. */
275 if (defsym != NULL && GLRO(dl_naudit) > 0)
276 {
277 reloc_result->bound = result;
278 /* Compute index of the symbol entry in the symbol table of
279 the DSO with the definition. */
280 reloc_result->boundndx = (defsym
281 - (ElfW(Sym) *) D_PTR (result,
282 l_info[DT_SYMTAB]));
283
284 /* Determine whether any of the two participating DSOs is
285 interested in auditing. */
286 if ((l->l_audit_any_plt | result->l_audit_any_plt) != 0)
287 {
288 unsigned int flags = 0;
289 struct audit_ifaces *afct = GLRO(dl_audit);
290 /* Synthesize a symbol record where the st_value field is
291 the result. */
292 ElfW(Sym) sym = *defsym;
293 sym.st_value = DL_FIXUP_VALUE_ADDR (value);
294
295 /* Keep track whether there is any interest in tracing
296 the call in the lower two bits. */
297 assert (DL_NNS * 2 <= sizeof (reloc_result->flags) * 8);
298 assert ((LA_SYMB_NOPLTENTER | LA_SYMB_NOPLTEXIT) == 3);
299 reloc_result->enterexit = LA_SYMB_NOPLTENTER | LA_SYMB_NOPLTEXIT;
300
301 const char *strtab2 = (const void *) D_PTR (result,
302 l_info[DT_STRTAB]);
303
304 for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
305 {
306 /* XXX Check whether both DSOs must request action or
307 only one */
308 if ((l->l_audit[cnt].bindflags & LA_FLG_BINDFROM) != 0
309 && (result->l_audit[cnt].bindflags & LA_FLG_BINDTO) != 0)
310 {
311 if (afct->symbind != NULL)
312 {
313 uintptr_t new_value
314 = afct->symbind (&sym, reloc_result->boundndx,
315 &l->l_audit[cnt].cookie,
316 &result->l_audit[cnt].cookie,
317 &flags,
318 strtab2 + defsym->st_name);
319 if (new_value != (uintptr_t) sym.st_value)
320 {
321 flags |= LA_SYMB_ALTVALUE;
322 sym.st_value = new_value;
323 }
324 }
325
326 /* Remember the results for every audit library and
327 store a summary in the first two bits. */
328 reloc_result->enterexit
329 &= flags & (LA_SYMB_NOPLTENTER | LA_SYMB_NOPLTEXIT);
330 reloc_result->enterexit
331 |= ((flags & (LA_SYMB_NOPLTENTER | LA_SYMB_NOPLTEXIT))
332 << ((cnt + 1) * 2));
333 }
334 else
335 /* If the bind flags say this auditor is not interested,
336 set the bits manually. */
337 reloc_result->enterexit
338 |= ((LA_SYMB_NOPLTENTER | LA_SYMB_NOPLTEXIT)
339 << ((cnt + 1) * 2));
340
341 afct = afct->next;
342 }
343
344 reloc_result->flags = flags;
345 value = DL_FIXUP_ADDR_VALUE (sym.st_value);
346 }
347 else
348 /* Set all bits since this symbol binding is not interesting. */
349 reloc_result->enterexit = (1u << DL_NNS) - 1;
350 }
351 #endif
352
353 /* Store the result for later runs. */
354 if (__builtin_expect (! GLRO(dl_bind_not), 1))
355 *resultp = value;
356 }
357
358 /* By default we do not call the pltexit function. */
359 long int framesize = -1;
360
361 #ifdef SHARED
362 /* Auditing checkpoint: report the PLT entering and allow the
363 auditors to change the value. */
364 if (DL_FIXUP_VALUE_CODE_ADDR (value) != 0 && GLRO(dl_naudit) > 0
365 /* Don't do anything if no auditor wants to intercept this call. */
366 && (reloc_result->enterexit & LA_SYMB_NOPLTENTER) == 0)
367 {
368 ElfW(Sym) *defsym = ((ElfW(Sym) *) D_PTR (reloc_result->bound,
369 l_info[DT_SYMTAB])
370 + reloc_result->boundndx);
371
372 /* Set up the sym parameter. */
373 ElfW(Sym) sym = *defsym;
374 sym.st_value = DL_FIXUP_VALUE_ADDR (value);
375
376 /* Get the symbol name. */
377 const char *strtab = (const void *) D_PTR (reloc_result->bound,
378 l_info[DT_STRTAB]);
379 const char *symname = strtab + sym.st_name;
380
381 /* Keep track of overwritten addresses. */
382 unsigned int flags = reloc_result->flags;
383
384 struct audit_ifaces *afct = GLRO(dl_audit);
385 for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
386 {
387 if (afct->ARCH_LA_PLTENTER != NULL
388 && (reloc_result->enterexit
389 & (LA_SYMB_NOPLTENTER << (2 * (cnt + 1)))) == 0)
390 {
391 long int new_framesize = -1;
392 uintptr_t new_value
393 = afct->ARCH_LA_PLTENTER (&sym, reloc_result->boundndx,
394 &l->l_audit[cnt].cookie,
395 &reloc_result->bound->l_audit[cnt].cookie,
396 regs, &flags, symname,
397 &new_framesize);
398 if (new_value != (uintptr_t) sym.st_value)
399 {
400 flags |= LA_SYMB_ALTVALUE;
401 sym.st_value = new_value;
402 }
403
404 /* Remember the results for every audit library and
405 store a summary in the first two bits. */
406 reloc_result->enterexit
407 |= ((flags & (LA_SYMB_NOPLTENTER | LA_SYMB_NOPLTEXIT))
408 << (2 * (cnt + 1)));
409
410 if ((reloc_result->enterexit & (LA_SYMB_NOPLTEXIT
411 << (2 * (cnt + 1))))
412 == 0 && new_framesize != -1 && framesize != -2)
413 {
414 /* If this is the first call providing information,
415 use it. */
416 if (framesize == -1)
417 framesize = new_framesize;
418 /* If two pltenter calls provide conflicting information,
419 use the larger value. */
420 else if (new_framesize != framesize)
421 framesize = MAX (new_framesize, framesize);
422 }
423 }
424
425 afct = afct->next;
426 }
427
428 value = DL_FIXUP_ADDR_VALUE (sym.st_value);
429 }
430 #endif
431
432 /* Store the frame size information. */
433 *framesizep = framesize;
434
435 (*mcount_fct) (retaddr, DL_FIXUP_VALUE_CODE_ADDR (value));
436
437 return value;
438 }
439
440 #endif /* PROF && ELF_MACHINE_NO_PLT */
441
442
443 #include <stdio.h>
444 void
445 ARCH_FIXUP_ATTRIBUTE
446 _dl_call_pltexit (struct link_map *l, ElfW(Word) reloc_arg,
447 const void *inregs, void *outregs)
448 {
449 #ifdef SHARED
450 /* This is the address in the array where we store the result of previous
451 relocations. */
452 // XXX Maybe the bound information must be stored on the stack since
453 // XXX with bind_not a new value could have been stored in the meantime.
454 struct reloc_result *reloc_result = &l->l_reloc_result[reloc_index];
455 ElfW(Sym) *defsym = ((ElfW(Sym) *) D_PTR (reloc_result->bound,
456 l_info[DT_SYMTAB])
457 + reloc_result->boundndx);
458
459 /* Set up the sym parameter. */
460 ElfW(Sym) sym = *defsym;
461 sym.st_value = DL_FIXUP_VALUE_ADDR (reloc_result->addr);
462
463 /* Get the symbol name. */
464 const char *strtab = (const void *) D_PTR (reloc_result->bound,
465 l_info[DT_STRTAB]);
466 const char *symname = strtab + sym.st_name;
467
468 struct audit_ifaces *afct = GLRO(dl_audit);
469 for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
470 {
471 if (afct->ARCH_LA_PLTEXIT != NULL
472 && (reloc_result->enterexit
473 & (LA_SYMB_NOPLTEXIT >> (2 * cnt))) == 0)
474 {
475 afct->ARCH_LA_PLTEXIT (&sym, reloc_result->boundndx,
476 &l->l_audit[cnt].cookie,
477 &reloc_result->bound->l_audit[cnt].cookie,
478 inregs, outregs, symname);
479 }
480
481 afct = afct->next;
482 }
483 #endif
484 }