xref: /freebsd/libexec/rtld-elf/aarch64/reloc.c (revision aa1a8ff2d6dbc51ef058f46f3db5a8bb77967145)
1 /*-
2  * Copyright (c) 2014-2015 The FreeBSD Foundation
3  * All rights reserved.
4  *
5  * Portions of this software were developed by Andrew Turner
6  * under sponsorship from the FreeBSD Foundation.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/types.h>
31 
32 #include <machine/sysarch.h>
33 
34 #include <stdlib.h>
35 
36 #include "debug.h"
37 #include "rtld.h"
38 #include "rtld_printf.h"
39 
40 /*
41  * It is possible for the compiler to emit relocations for unaligned data.
42  * We handle this situation with these inlines.
43  */
44 #define	RELOC_ALIGNED_P(x) \
45 	(((uintptr_t)(x) & (sizeof(void *) - 1)) == 0)
46 
47 /*
48  * This is not the correct prototype, but we only need it for
49  * a function pointer to a simple asm function.
50  */
51 void *_rtld_tlsdesc_static(void *);
52 void *_rtld_tlsdesc_undef(void *);
53 void *_rtld_tlsdesc_dynamic(void *);
54 
55 void _exit(int);
56 
57 bool
58 arch_digest_note(struct Struct_Obj_Entry *obj __unused, const Elf_Note *note)
59 {
60 	const char *note_name;
61 	const uint32_t *note_data;
62 
63 	note_name = (const char *)(note + 1);
64 	/* Only handle GNU notes */
65 	if (note->n_namesz != sizeof(ELF_NOTE_GNU) ||
66 	    strncmp(note_name, ELF_NOTE_GNU, sizeof(ELF_NOTE_GNU)) != 0)
67 		return (false);
68 
69 	/* Only handle GNU property notes */
70 	if (note->n_type != NT_GNU_PROPERTY_TYPE_0)
71 		return (false);
72 
73 	/*
74 	 * note_data[0] - Type
75 	 * note_data[1] - Length
76 	 * note_data[2] - Data
77 	 * note_data[3] - Padding?
78 	 */
79 	note_data = (const uint32_t *)(note_name + note->n_namesz);
80 
81 	/* Only handle AArch64 feature notes */
82 	if (note_data[0] != GNU_PROPERTY_AARCH64_FEATURE_1_AND)
83 		return (false);
84 
85 	/* We expect at least 4 bytes of data */
86 	if (note_data[1] < 4)
87 		return (false);
88 
89 	/* TODO: Only guard if HWCAP2_BTI is set */
90 	if ((note_data[2] & GNU_PROPERTY_AARCH64_FEATURE_1_BTI) != 0) {
91 		struct arm64_guard_page_args guard;
92 
93 		guard.addr = (uintptr_t)obj->mapbase;
94 		guard.len = obj->mapsize;
95 
96 		sysarch(ARM64_GUARD_PAGE, &guard);
97 	}
98 
99 	return (true);
100 }
101 
102 void
103 init_pltgot(Obj_Entry *obj)
104 {
105 
106 	if (obj->pltgot != NULL) {
107 		obj->pltgot[1] = (Elf_Addr) obj;
108 		obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start;
109 	}
110 }
111 
112 int
113 do_copy_relocations(Obj_Entry *dstobj)
114 {
115 	const Obj_Entry *srcobj, *defobj;
116 	const Elf_Rela *relalim;
117 	const Elf_Rela *rela;
118 	const Elf_Sym *srcsym;
119 	const Elf_Sym *dstsym;
120 	const void *srcaddr;
121 	const char *name;
122 	void *dstaddr;
123 	SymLook req;
124 	size_t size;
125 	int res;
126 
127 	/*
128 	 * COPY relocs are invalid outside of the main program
129 	 */
130 	assert(dstobj->mainprog);
131 
132 	relalim = (const Elf_Rela *)((const char *)dstobj->rela +
133 	    dstobj->relasize);
134 	for (rela = dstobj->rela; rela < relalim; rela++) {
135 		if (ELF_R_TYPE(rela->r_info) != R_AARCH64_COPY)
136 			continue;
137 
138 		dstaddr = (void *)(dstobj->relocbase + rela->r_offset);
139 		dstsym = dstobj->symtab + ELF_R_SYM(rela->r_info);
140 		name = dstobj->strtab + dstsym->st_name;
141 		size = dstsym->st_size;
142 
143 		symlook_init(&req, name);
144 		req.ventry = fetch_ventry(dstobj, ELF_R_SYM(rela->r_info));
145 		req.flags = SYMLOOK_EARLY;
146 
147 		for (srcobj = globallist_next(dstobj); srcobj != NULL;
148 		     srcobj = globallist_next(srcobj)) {
149 			res = symlook_obj(&req, srcobj);
150 			if (res == 0) {
151 				srcsym = req.sym_out;
152 				defobj = req.defobj_out;
153 				break;
154 			}
155 		}
156 		if (srcobj == NULL) {
157 			_rtld_error("Undefined symbol \"%s\" referenced from "
158 			    "COPY relocation in %s", name, dstobj->path);
159 			return (-1);
160 		}
161 
162 		srcaddr = (const void *)(defobj->relocbase + srcsym->st_value);
163 		memcpy(dstaddr, srcaddr, size);
164 	}
165 
166 	return (0);
167 }
168 
169 struct tls_data {
170 	Elf_Addr	dtv_gen;
171 	int		tls_index;
172 	Elf_Addr	tls_offs;
173 };
174 
175 static Elf_Addr
176 reloc_tlsdesc_alloc(int tlsindex, Elf_Addr tlsoffs)
177 {
178 	struct tls_data *tlsdesc;
179 
180 	tlsdesc = xmalloc(sizeof(struct tls_data));
181 	tlsdesc->dtv_gen = tls_dtv_generation;
182 	tlsdesc->tls_index = tlsindex;
183 	tlsdesc->tls_offs = tlsoffs;
184 
185 	return ((Elf_Addr)tlsdesc);
186 }
187 
188 static void
189 reloc_tlsdesc(const Obj_Entry *obj, const Elf_Rela *rela, Elf_Addr *where,
190     int flags, RtldLockState *lockstate)
191 {
192 	const Elf_Sym *def;
193 	const Obj_Entry *defobj;
194 	Elf_Addr offs;
195 
196 
197 	offs = 0;
198 	if (ELF_R_SYM(rela->r_info) != 0) {
199 		def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj, flags,
200 			    NULL, lockstate);
201 		if (def == NULL)
202 			rtld_die();
203 		offs = def->st_value;
204 		obj = defobj;
205 		if (def->st_shndx == SHN_UNDEF) {
206 			/* Weak undefined thread variable */
207 			where[0] = (Elf_Addr)_rtld_tlsdesc_undef;
208 			where[1] = rela->r_addend;
209 			return;
210 		}
211 	}
212 	offs += rela->r_addend;
213 
214 	if (obj->tlsoffset != 0) {
215 		/* Variable is in initialy allocated TLS segment */
216 		where[0] = (Elf_Addr)_rtld_tlsdesc_static;
217 		where[1] = obj->tlsoffset + offs;
218 	} else {
219 		/* TLS offest is unknown at load time, use dynamic resolving */
220 		where[0] = (Elf_Addr)_rtld_tlsdesc_dynamic;
221 		where[1] = reloc_tlsdesc_alloc(obj->tlsindex, offs);
222 	}
223 }
224 
225 /*
226  * Process the PLT relocations.
227  */
228 int
229 reloc_plt(Obj_Entry *obj, int flags, RtldLockState *lockstate)
230 {
231 	const Elf_Rela *relalim;
232 	const Elf_Rela *rela;
233 
234 	relalim = (const Elf_Rela *)((const char *)obj->pltrela +
235 	    obj->pltrelasize);
236 	for (rela = obj->pltrela; rela < relalim; rela++) {
237 		Elf_Addr *where;
238 
239 		where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
240 
241 		switch(ELF_R_TYPE(rela->r_info)) {
242 		case R_AARCH64_JUMP_SLOT:
243 			*where += (Elf_Addr)obj->relocbase;
244 			break;
245 		case R_AARCH64_TLSDESC:
246 			reloc_tlsdesc(obj, rela, where, SYMLOOK_IN_PLT | flags,
247 			    lockstate);
248 			break;
249 		case R_AARCH64_IRELATIVE:
250 			obj->irelative = true;
251 			break;
252 		case R_AARCH64_NONE:
253 			break;
254 		default:
255 			_rtld_error("Unknown relocation type %u in PLT",
256 			    (unsigned int)ELF_R_TYPE(rela->r_info));
257 			return (-1);
258 		}
259 	}
260 
261 	return (0);
262 }
263 
264 /*
265  * LD_BIND_NOW was set - force relocation for all jump slots
266  */
267 int
268 reloc_jmpslots(Obj_Entry *obj, int flags, RtldLockState *lockstate)
269 {
270 	const Obj_Entry *defobj;
271 	const Elf_Rela *relalim;
272 	const Elf_Rela *rela;
273 	const Elf_Sym *def;
274 
275 	if (obj->jmpslots_done)
276 		return (0);
277 
278 	relalim = (const Elf_Rela *)((const char *)obj->pltrela +
279 	    obj->pltrelasize);
280 	for (rela = obj->pltrela; rela < relalim; rela++) {
281 		Elf_Addr *where, target;
282 
283 		where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
284 		switch(ELF_R_TYPE(rela->r_info)) {
285 		case R_AARCH64_JUMP_SLOT:
286 			def = find_symdef(ELF_R_SYM(rela->r_info), obj,
287 			    &defobj, SYMLOOK_IN_PLT | flags, NULL, lockstate);
288 			if (def == NULL)
289 				return (-1);
290 			if (ELF_ST_TYPE(def->st_info) == STT_GNU_IFUNC) {
291 				obj->gnu_ifunc = true;
292 				continue;
293 			}
294 			target = (Elf_Addr)(defobj->relocbase + def->st_value);
295 			reloc_jmpslot(where, target, defobj, obj,
296 			    (const Elf_Rel *)rela);
297 			break;
298 		}
299 	}
300 	obj->jmpslots_done = true;
301 
302 	return (0);
303 }
304 
305 static void
306 reloc_iresolve_one(Obj_Entry *obj, const Elf_Rela *rela,
307     RtldLockState *lockstate)
308 {
309 	Elf_Addr *where, target, *ptr;
310 
311 	ptr = (Elf_Addr *)(obj->relocbase + rela->r_addend);
312 	where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
313 	lock_release(rtld_bind_lock, lockstate);
314 	target = call_ifunc_resolver(ptr);
315 	wlock_acquire(rtld_bind_lock, lockstate);
316 	*where = target;
317 }
318 
319 int
320 reloc_iresolve(Obj_Entry *obj, struct Struct_RtldLockState *lockstate)
321 {
322 	const Elf_Rela *relalim;
323 	const Elf_Rela *rela;
324 
325 	if (!obj->irelative)
326 		return (0);
327 	obj->irelative = false;
328 	relalim = (const Elf_Rela *)((const char *)obj->pltrela +
329 	    obj->pltrelasize);
330 	for (rela = obj->pltrela;  rela < relalim;  rela++) {
331 		if (ELF_R_TYPE(rela->r_info) == R_AARCH64_IRELATIVE)
332 			reloc_iresolve_one(obj, rela, lockstate);
333 	}
334 	return (0);
335 }
336 
337 int
338 reloc_iresolve_nonplt(Obj_Entry *obj, struct Struct_RtldLockState *lockstate)
339 {
340 	const Elf_Rela *relalim;
341 	const Elf_Rela *rela;
342 
343 	if (!obj->irelative_nonplt)
344 		return (0);
345 	obj->irelative_nonplt = false;
346 	relalim = (const Elf_Rela *)((const char *)obj->rela + obj->relasize);
347 	for (rela = obj->rela;  rela < relalim;  rela++) {
348 		if (ELF_R_TYPE(rela->r_info) == R_AARCH64_IRELATIVE)
349 			reloc_iresolve_one(obj, rela, lockstate);
350 	}
351 	return (0);
352 }
353 
354 int
355 reloc_gnu_ifunc(Obj_Entry *obj, int flags,
356    struct Struct_RtldLockState *lockstate)
357 {
358 	const Elf_Rela *relalim;
359 	const Elf_Rela *rela;
360 	Elf_Addr *where, target;
361 	const Elf_Sym *def;
362 	const Obj_Entry *defobj;
363 
364 	if (!obj->gnu_ifunc)
365 		return (0);
366 	relalim = (const Elf_Rela *)((const char *)obj->pltrela + obj->pltrelasize);
367 	for (rela = obj->pltrela;  rela < relalim;  rela++) {
368 		if (ELF_R_TYPE(rela->r_info) == R_AARCH64_JUMP_SLOT) {
369 			where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
370 			def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
371 			    SYMLOOK_IN_PLT | flags, NULL, lockstate);
372 			if (def == NULL)
373 				return (-1);
374 			if (ELF_ST_TYPE(def->st_info) != STT_GNU_IFUNC)
375 				continue;
376 			lock_release(rtld_bind_lock, lockstate);
377 			target = (Elf_Addr)rtld_resolve_ifunc(defobj, def);
378 			wlock_acquire(rtld_bind_lock, lockstate);
379 			reloc_jmpslot(where, target, defobj, obj,
380 			    (const Elf_Rel *)rela);
381 		}
382 	}
383 	obj->gnu_ifunc = false;
384 	return (0);
385 }
386 
387 Elf_Addr
388 reloc_jmpslot(Elf_Addr *where, Elf_Addr target,
389     const Obj_Entry *defobj __unused, const Obj_Entry *obj __unused,
390     const Elf_Rel *rel)
391 {
392 
393 	assert(ELF_R_TYPE(rel->r_info) == R_AARCH64_JUMP_SLOT ||
394 	    ELF_R_TYPE(rel->r_info) == R_AARCH64_IRELATIVE);
395 
396 	if (*where != target && !ld_bind_not)
397 		*where = target;
398 	return (target);
399 }
400 
401 void
402 ifunc_init(Elf_Auxinfo aux_info[__min_size(AT_COUNT)] __unused)
403 {
404 
405 }
406 
407 /*
408  * Process non-PLT relocations
409  */
410 int
411 reloc_non_plt(Obj_Entry *obj, Obj_Entry *obj_rtld, int flags,
412     RtldLockState *lockstate)
413 {
414 	const Obj_Entry *defobj;
415 	const Elf_Rela *relalim;
416 	const Elf_Rela *rela;
417 	const Elf_Sym *def;
418 	SymCache *cache;
419 	Elf_Addr *where, symval;
420 
421 	/*
422 	 * The dynamic loader may be called from a thread, we have
423 	 * limited amounts of stack available so we cannot use alloca().
424 	 */
425 	if (obj == obj_rtld)
426 		cache = NULL;
427 	else
428 		cache = calloc(obj->dynsymcount, sizeof(SymCache));
429 		/* No need to check for NULL here */
430 
431 	relalim = (const Elf_Rela *)((const char *)obj->rela + obj->relasize);
432 	for (rela = obj->rela; rela < relalim; rela++) {
433 		/*
434 		 * First, resolve symbol for relocations which
435 		 * reference symbols.
436 		 */
437 		switch (ELF_R_TYPE(rela->r_info)) {
438 		case R_AARCH64_ABS64:
439 		case R_AARCH64_GLOB_DAT:
440 		case R_AARCH64_TLS_TPREL64:
441 		case R_AARCH64_TLS_DTPREL64:
442 		case R_AARCH64_TLS_DTPMOD64:
443 			def = find_symdef(ELF_R_SYM(rela->r_info), obj,
444 			    &defobj, flags, cache, lockstate);
445 			if (def == NULL)
446 				return (-1);
447 			/*
448 			 * If symbol is IFUNC, only perform relocation
449 			 * when caller allowed it by passing
450 			 * SYMLOOK_IFUNC flag.  Skip the relocations
451 			 * otherwise.
452 			 *
453 			 * Also error out in case IFUNC relocations
454 			 * are specified for TLS, which cannot be
455 			 * usefully interpreted.
456 			 */
457 			if (ELF_ST_TYPE(def->st_info) == STT_GNU_IFUNC) {
458 				switch (ELF_R_TYPE(rela->r_info)) {
459 				case R_AARCH64_ABS64:
460 				case R_AARCH64_GLOB_DAT:
461 					if ((flags & SYMLOOK_IFUNC) == 0) {
462 						obj->non_plt_gnu_ifunc = true;
463 						continue;
464 					}
465 					symval = (Elf_Addr)rtld_resolve_ifunc(
466 					    defobj, def);
467 					break;
468 				default:
469 					_rtld_error("%s: IFUNC for TLS reloc",
470 					    obj->path);
471 					return (-1);
472 				}
473 			} else {
474 				if ((flags & SYMLOOK_IFUNC) != 0)
475 					continue;
476 				symval = (Elf_Addr)defobj->relocbase +
477 				    def->st_value;
478 			}
479 			break;
480 		default:
481 			if ((flags & SYMLOOK_IFUNC) != 0)
482 				continue;
483 		}
484 
485 		where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
486 
487 		switch (ELF_R_TYPE(rela->r_info)) {
488 		case R_AARCH64_ABS64:
489 		case R_AARCH64_GLOB_DAT:
490 			*where = symval + rela->r_addend;
491 			break;
492 		case R_AARCH64_COPY:
493 			/*
494 			 * These are deferred until all other relocations have
495 			 * been done. All we do here is make sure that the
496 			 * COPY relocation is not in a shared library. They
497 			 * are allowed only in executable files.
498 			 */
499 			if (!obj->mainprog) {
500 				_rtld_error("%s: Unexpected R_AARCH64_COPY "
501 				    "relocation in shared library", obj->path);
502 				return (-1);
503 			}
504 			break;
505 		case R_AARCH64_TLSDESC:
506 			reloc_tlsdesc(obj, rela, where, flags, lockstate);
507 			break;
508 		case R_AARCH64_TLS_TPREL64:
509 			/*
510 			 * We lazily allocate offsets for static TLS as we
511 			 * see the first relocation that references the
512 			 * TLS block. This allows us to support (small
513 			 * amounts of) static TLS in dynamically loaded
514 			 * modules. If we run out of space, we generate an
515 			 * error.
516 			 */
517 			if (!defobj->tls_static) {
518 				if (!allocate_tls_offset(
519 				    __DECONST(Obj_Entry *, defobj))) {
520 					_rtld_error(
521 					    "%s: No space available for static "
522 					    "Thread Local Storage", obj->path);
523 					return (-1);
524 				}
525 			}
526 			*where = def->st_value + rela->r_addend +
527 			    defobj->tlsoffset;
528 			break;
529 
530 		/*
531 		 * !!! BEWARE !!!
532 		 * ARM ELF ABI defines TLS_DTPMOD64 as 1029, and TLS_DTPREL64
533 		 * as 1028. But actual bfd linker and the glibc RTLD linker
534 		 * treats TLS_DTPMOD64 as 1028 and TLS_DTPREL64 1029.
535 		 */
536 		case R_AARCH64_TLS_DTPREL64: /* efectively is TLS_DTPMOD64 */
537 			*where += (Elf_Addr)defobj->tlsindex;
538 			break;
539 		case R_AARCH64_TLS_DTPMOD64: /* efectively is TLS_DTPREL64 */
540 			*where += (Elf_Addr)(def->st_value + rela->r_addend);
541 			break;
542 		case R_AARCH64_RELATIVE:
543 			*where = (Elf_Addr)(obj->relocbase + rela->r_addend);
544 			break;
545 		case R_AARCH64_NONE:
546 			break;
547 		case R_AARCH64_IRELATIVE:
548 			obj->irelative_nonplt = true;
549 			break;
550 		default:
551 			rtld_printf("%s: Unhandled relocation %lu\n",
552 			    obj->path, ELF_R_TYPE(rela->r_info));
553 			return (-1);
554 		}
555 	}
556 
557 	return (0);
558 }
559 
560 void
561 allocate_initial_tls(Obj_Entry *objs)
562 {
563 
564 	/*
565 	* Fix the size of the static TLS block by using the maximum
566 	* offset allocated so far and adding a bit for dynamic modules to
567 	* use.
568 	*/
569 	tls_static_space = tls_last_offset + tls_last_size +
570 	    ld_static_tls_extra;
571 
572 	_tcb_set(allocate_tls(objs, NULL, TLS_TCB_SIZE, TLS_TCB_ALIGN));
573 }
574 
575 void *
576 __tls_get_addr(tls_index* ti)
577 {
578 	uintptr_t **dtvp;
579 
580 	dtvp = &_tcb_get()->tcb_dtv;
581 	return (tls_get_addr_common(dtvp, ti->ti_module, ti->ti_offset));
582 }
583