1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20: #define IN_DL_RUNTIME 1
21:
22: #include <alloca.h>
23: #include <stdlib.h>
24: #include <unistd.h>
25: #include <sys/param.h>
26: #include <ldsodefs.h>
27: #include <sysdep-cancel.h>
28: #include "dynamic-link.h"
29: #include <tls.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: #ifndef VERSYMIDX
40: # define VERSYMIDX(sym) (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGIDX (sym))
41: #endif
42:
43:
44:
45: #ifndef ARCH_FIXUP_ATTRIBUTE
46: # define ARCH_FIXUP_ATTRIBUTE
47: #endif
48:
49:
50:
51:
52:
53:
54:
55:
56:
57: #ifndef ELF_MACHINE_NO_PLT
58: DL_FIXUP_VALUE_TYPE
59: __attribute ((noinline)) ARCH_FIXUP_ATTRIBUTE
60: _dl_fixup (
61: # ifdef ELF_MACHINE_RUNTIME_FIXUP_ARGS
62: ELF_MACHINE_RUNTIME_FIXUP_ARGS,
63: # endif
64:
65:
66: struct link_map *__unbounded l, ElfW(Word) reloc_offset)
67: {
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]);
71:
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);
76: lookup_t result;
77: DL_FIXUP_VALUE_TYPE value;
78:
79:
80: assert (ELFW(R_TYPE)(reloc->r_info) == ELF_MACHINE_JMP_SLOT);
81:
82:
83:
84: if (__builtin_expect (ELFW(ST_VISIBILITY) (sym->st_other), 0) == 0)
85: {
86: const struct r_found_version *version = NULL;
87:
88: if (l->l_info[VERSYMIDX (DT_VERSYM)] != NULL)
89: {
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)
95: version = NULL;
96: }
97:
98:
99:
100:
101: int flags = DL_LOOKUP_ADD_DEPENDENCY;
102: if (!RTLD_SINGLE_THREAD_P)
103: {
104: THREAD_GSCOPE_SET_FLAG ();
105: flags |= DL_LOOKUP_GSCOPE_LOCK;
106: }
107:
108: result = _dl_lookup_symbol_x (strtab + sym->st_name, l, &sym, l->l_scope,
109: version, ELF_RTYPE_CLASS_PLT, flags, NULL);
110:
111:
112: if (!RTLD_SINGLE_THREAD_P)
113: THREAD_GSCOPE_RESET_FLAG ();
114:
115:
116:
117:
118: value = DL_FIXUP_MAKE_VALUE (result,
119: sym ? (LOOKUP_VALUE_ADDRESS (result)
120: + sym->st_value) : 0);
121: }
122: else
123: {
124:
125:
126: value = DL_FIXUP_MAKE_VALUE (l, l->l_addr + sym->st_value);
127: result = l;
128: }
129:
130:
131: value = elf_machine_plt_value (l, reloc, value);
132:
133:
134: if (__builtin_expect (GLRO(dl_bind_not), 0))
135: return value;
136:
137: return elf_machine_fixup_plt (l, result, reloc, rel_addr, value);
138: }
139: #endif
140:
141: #if !defined PROF && !defined ELF_MACHINE_NO_PLT && !__BOUNDED_POINTERS__
142:
143: DL_FIXUP_VALUE_TYPE
144: __attribute ((noinline)) ARCH_FIXUP_ATTRIBUTE
145: _dl_profile_fixup (
146: #ifdef ELF_MACHINE_RUNTIME_FIXUP_ARGS
147: ELF_MACHINE_RUNTIME_FIXUP_ARGS,
148: #endif
149: struct link_map *l, ElfW(Word) reloc_offset,
150: ElfW(Addr) retaddr, void *regs, long int *framesizep)
151: {
152: void (*mcount_fct) (ElfW(Addr), ElfW(Addr)) = INTUSE(_dl_mcount);
153:
154:
155:
156: struct reloc_result *reloc_result
157: = &l->l_reloc_result[reloc_offset / sizeof (PLTREL)];
158: DL_FIXUP_VALUE_TYPE *resultp = &reloc_result->addr;
159:
160: DL_FIXUP_VALUE_TYPE value = *resultp;
161: if (DL_FIXUP_VALUE_CODE_ADDR (value) == 0)
162: {
163:
164: const ElfW(Sym) *const symtab
165: = (const void *) D_PTR (l, l_info[DT_SYMTAB]);
166: const char *strtab = (const char *) D_PTR (l, l_info[DT_STRTAB]);
167:
168: const PLTREL *const reloc
169: = (const void *) (D_PTR (l, l_info[DT_JMPREL]) + reloc_offset);
170: const ElfW(Sym) *refsym = &symtab[ELFW(R_SYM) (reloc->r_info)];
171: const ElfW(Sym) *defsym = refsym;
172: lookup_t result;
173:
174:
175: assert (ELFW(R_TYPE)(reloc->r_info) == ELF_MACHINE_JMP_SLOT);
176:
177:
178:
179: if (__builtin_expect (ELFW(ST_VISIBILITY) (refsym->st_other), 0) == 0)
180: {
181: const struct r_found_version *version = NULL;
182:
183: if (l->l_info[VERSYMIDX (DT_VERSYM)] != NULL)
184: {
185: const ElfW(Half) *vernum =
186: (const void *) D_PTR (l, l_info[VERSYMIDX (DT_VERSYM)]);
187: ElfW(Half) ndx = vernum[ELFW(R_SYM) (reloc->r_info)] & 0x7fff;
188: version = &l->l_versions[ndx];
189: if (version->hash == 0)
190: version = NULL;
191: }
192:
193:
194:
195:
196: int flags = DL_LOOKUP_ADD_DEPENDENCY;
197: if (!RTLD_SINGLE_THREAD_P)
198: {
199: THREAD_GSCOPE_SET_FLAG ();
200: flags |= DL_LOOKUP_GSCOPE_LOCK;
201: }
202:
203: result = _dl_lookup_symbol_x (strtab + refsym->st_name, l,
204: &defsym, l->l_scope, version,
205: ELF_RTYPE_CLASS_PLT, flags, NULL);
206:
207:
208: if (!RTLD_SINGLE_THREAD_P)
209: THREAD_GSCOPE_RESET_FLAG ();
210:
211:
212:
213:
214: value = DL_FIXUP_MAKE_VALUE (result,
215: defsym != NULL
216: ? LOOKUP_VALUE_ADDRESS (result)
217: + defsym->st_value : 0);
218: }
219: else
220: {
221:
222:
223: value = DL_FIXUP_MAKE_VALUE (l, l->l_addr + refsym->st_value);
224: result = l;
225: }
226:
227: value = elf_machine_plt_value (l, reloc, value);
228:
229: #ifdef SHARED
230:
231:
232:
233: if (defsym != NULL && GLRO(dl_naudit) > 0)
234: {
235: reloc_result->bound = result;
236:
237:
238: reloc_result->boundndx = (defsym
239: - (ElfW(Sym) *) D_PTR (result,
240: l_info[DT_SYMTAB]));
241:
242:
243:
244: if ((l->l_audit_any_plt | result->l_audit_any_plt) != 0)
245: {
246: unsigned int altvalue = 0;
247: struct audit_ifaces *afct = GLRO(dl_audit);
248:
249:
250: ElfW(Sym) sym = *defsym;
251: sym.st_value = DL_FIXUP_VALUE_ADDR (value);
252:
253:
254:
255: assert (DL_NNS * 2 <= sizeof (reloc_result->flags) * 8);
256: assert ((LA_SYMB_NOPLTENTER | LA_SYMB_NOPLTEXIT) == 3);
257: reloc_result->enterexit = LA_SYMB_NOPLTENTER | LA_SYMB_NOPLTEXIT;
258:
259: const char *strtab2 = (const void *) D_PTR (result,
260: l_info[DT_STRTAB]);
261:
262: for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
263: {
264:
265:
266: if ((l->l_audit[cnt].bindflags & LA_FLG_BINDFROM) != 0
267: && (result->l_audit[cnt].bindflags & LA_FLG_BINDTO) != 0)
268: {
269: unsigned int flags = altvalue;
270: if (afct->symbind != NULL)
271: {
272: uintptr_t new_value
273: = afct->symbind (&sym, reloc_result->boundndx,
274: &l->l_audit[cnt].cookie,
275: &result->l_audit[cnt].cookie,
276: &flags,
277: strtab2 + defsym->st_name);
278: if (new_value != (uintptr_t) sym.st_value)
279: {
280: altvalue = LA_SYMB_ALTVALUE;
281: sym.st_value = new_value;
282: }
283: }
284:
285:
286:
287: reloc_result->enterexit
288: &= flags & (LA_SYMB_NOPLTENTER | LA_SYMB_NOPLTEXIT);
289: reloc_result->enterexit
290: |= ((flags & (LA_SYMB_NOPLTENTER | LA_SYMB_NOPLTEXIT))
291: << ((cnt + 1) * 2));
292: }
293: else
294:
295:
296: reloc_result->enterexit
297: |= ((LA_SYMB_NOPLTENTER | LA_SYMB_NOPLTEXIT)
298: << ((cnt + 1) * 2));
299:
300: afct = afct->next;
301: }
302:
303: reloc_result->flags = altvalue;
304: value = DL_FIXUP_ADDR_VALUE (sym.st_value);
305: }
306: else
307:
308: reloc_result->enterexit = (1u << DL_NNS) - 1;
309: }
310: #endif
311:
312:
313: if (__builtin_expect (! GLRO(dl_bind_not), 1))
314: *resultp = value;
315: }
316:
317:
318: long int framesize = -1;
319:
320: #ifdef SHARED
321:
322:
323: if (DL_FIXUP_VALUE_CODE_ADDR (value) != 0 && GLRO(dl_naudit) > 0
324:
325: && (reloc_result->enterexit & LA_SYMB_NOPLTENTER) == 0)
326: {
327: ElfW(Sym) *defsym = ((ElfW(Sym) *) D_PTR (reloc_result->bound,
328: l_info[DT_SYMTAB])
329: + reloc_result->boundndx);
330:
331:
332: ElfW(Sym) sym = *defsym;
333: sym.st_value = DL_FIXUP_VALUE_ADDR (value);
334:
335:
336: const char *strtab = (const void *) D_PTR (reloc_result->bound,
337: l_info[DT_STRTAB]);
338: const char *symname = strtab + sym.st_name;
339:
340:
341: unsigned int altvalue = reloc_result->flags;
342:
343: struct audit_ifaces *afct = GLRO(dl_audit);
344: for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
345: {
346: if (afct->ARCH_LA_PLTENTER != NULL
347: && (reloc_result->enterexit
348: & (LA_SYMB_NOPLTENTER << (2 * (cnt + 1)))) == 0)
349: {
350: unsigned int flags = altvalue;
351: long int new_framesize = -1;
352: uintptr_t new_value
353: = afct->ARCH_LA_PLTENTER (&sym, reloc_result->boundndx,
354: &l->l_audit[cnt].cookie,
355: &reloc_result->bound->l_audit[cnt].cookie,
356: regs, &flags, symname,
357: &new_framesize);
358: if (new_value != (uintptr_t) sym.st_value)
359: {
360: altvalue = LA_SYMB_ALTVALUE;
361: sym.st_value = new_value;
362: }
363:
364:
365:
366: reloc_result->enterexit
367: |= ((flags & (LA_SYMB_NOPLTENTER | LA_SYMB_NOPLTEXIT))
368: << (2 * (cnt + 1)));
369:
370: if ((reloc_result->enterexit & (LA_SYMB_NOPLTEXIT
371: << (2 * (cnt + 1))))
372: == 0 && new_framesize != -1 && framesize != -2)
373: {
374:
375:
376: if (framesize == -1)
377: framesize = new_framesize;
378:
379:
380: else if (new_framesize != framesize)
381: framesize = MAX (new_framesize, framesize);
382: }
383: }
384:
385: afct = afct->next;
386: }
387:
388: value = DL_FIXUP_ADDR_VALUE (sym.st_value);
389: }
390: #endif
391:
392:
393: *framesizep = framesize;
394:
395: (*mcount_fct) (retaddr, DL_FIXUP_VALUE_CODE_ADDR (value));
396:
397: return value;
398: }
399:
400: #endif
401:
402:
403: #include <stdio.h>
404: void
405: ARCH_FIXUP_ATTRIBUTE
406: _dl_call_pltexit (struct link_map *l, ElfW(Word) reloc_offset,
407: const void *inregs, void *outregs)
408: {
409: #ifdef SHARED
410:
411:
412:
413:
414: struct reloc_result *reloc_result
415: = &l->l_reloc_result[reloc_offset / sizeof (PLTREL)];
416: ElfW(Sym) *defsym = ((ElfW(Sym) *) D_PTR (reloc_result->bound,
417: l_info[DT_SYMTAB])
418: + reloc_result->boundndx);
419:
420:
421: ElfW(Sym) sym = *defsym;
422:
423:
424: const char *strtab = (const void *) D_PTR (reloc_result->bound,
425: l_info[DT_STRTAB]);
426: const char *symname = strtab + sym.st_name;
427:
428: struct audit_ifaces *afct = GLRO(dl_audit);
429: for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
430: {
431: if (afct->ARCH_LA_PLTEXIT != NULL
432: && (reloc_result->enterexit
433: & (LA_SYMB_NOPLTEXIT >> (2 * cnt))) == 0)
434: {
435: afct->ARCH_LA_PLTEXIT (&sym, reloc_result->boundndx,
436: &l->l_audit[cnt].cookie,
437: &reloc_result->bound->l_audit[cnt].cookie,
438: inregs, outregs, symname);
439: }
440:
441: afct = afct->next;
442: }
443: #endif
444: }