xref: /freebsd/libexec/rtld-elf/aarch64/reloc.c (revision edca4938f74db18d091868237592abbf7e718669)
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/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
33 #include <sys/types.h>
34 
35 #include <stdlib.h>
36 
37 #include "debug.h"
38 #include "rtld.h"
39 #include "rtld_printf.h"
40 
41 /*
42  * It is possible for the compiler to emit relocations for unaligned data.
43  * We handle this situation with these inlines.
44  */
45 #define	RELOC_ALIGNED_P(x) \
46 	(((uintptr_t)(x) & (sizeof(void *) - 1)) == 0)
47 
48 /*
49  * This is not the correct prototype, but we only need it for
50  * a function pointer to a simple asm function.
51  */
52 void *_rtld_tlsdesc_static(void *);
53 void *_rtld_tlsdesc_undef(void *);
54 void *_rtld_tlsdesc_dynamic(void *);
55 
56 void _exit(int);
57 
58 void
59 init_pltgot(Obj_Entry *obj)
60 {
61 
62 	if (obj->pltgot != NULL) {
63 		obj->pltgot[1] = (Elf_Addr) obj;
64 		obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start;
65 	}
66 }
67 
68 int
69 do_copy_relocations(Obj_Entry *dstobj)
70 {
71 	const Obj_Entry *srcobj, *defobj;
72 	const Elf_Rela *relalim;
73 	const Elf_Rela *rela;
74 	const Elf_Sym *srcsym;
75 	const Elf_Sym *dstsym;
76 	const void *srcaddr;
77 	const char *name;
78 	void *dstaddr;
79 	SymLook req;
80 	size_t size;
81 	int res;
82 
83 	/*
84 	 * COPY relocs are invalid outside of the main program
85 	 */
86 	assert(dstobj->mainprog);
87 
88 	relalim = (const Elf_Rela *)((const char *)dstobj->rela +
89 	    dstobj->relasize);
90 	for (rela = dstobj->rela; rela < relalim; rela++) {
91 		if (ELF_R_TYPE(rela->r_info) != R_AARCH64_COPY)
92 			continue;
93 
94 		dstaddr = (void *)(dstobj->relocbase + rela->r_offset);
95 		dstsym = dstobj->symtab + ELF_R_SYM(rela->r_info);
96 		name = dstobj->strtab + dstsym->st_name;
97 		size = dstsym->st_size;
98 
99 		symlook_init(&req, name);
100 		req.ventry = fetch_ventry(dstobj, ELF_R_SYM(rela->r_info));
101 		req.flags = SYMLOOK_EARLY;
102 
103 		for (srcobj = globallist_next(dstobj); srcobj != NULL;
104 		     srcobj = globallist_next(srcobj)) {
105 			res = symlook_obj(&req, srcobj);
106 			if (res == 0) {
107 				srcsym = req.sym_out;
108 				defobj = req.defobj_out;
109 				break;
110 			}
111 		}
112 		if (srcobj == NULL) {
113 			_rtld_error("Undefined symbol \"%s\" referenced from "
114 			    "COPY relocation in %s", name, dstobj->path);
115 			return (-1);
116 		}
117 
118 		srcaddr = (const void *)(defobj->relocbase + srcsym->st_value);
119 		memcpy(dstaddr, srcaddr, size);
120 	}
121 
122 	return (0);
123 }
124 
125 struct tls_data {
126 	Elf_Addr	dtv_gen;
127 	int		tls_index;
128 	Elf_Addr	tls_offs;
129 };
130 
131 static Elf_Addr
132 reloc_tlsdesc_alloc(int tlsindex, Elf_Addr tlsoffs)
133 {
134 	struct tls_data *tlsdesc;
135 
136 	tlsdesc = xmalloc(sizeof(struct tls_data));
137 	tlsdesc->dtv_gen = tls_dtv_generation;
138 	tlsdesc->tls_index = tlsindex;
139 	tlsdesc->tls_offs = tlsoffs;
140 
141 	return ((Elf_Addr)tlsdesc);
142 }
143 
144 static void
145 reloc_tlsdesc(const Obj_Entry *obj, const Elf_Rela *rela, Elf_Addr *where,
146     int flags, RtldLockState *lockstate)
147 {
148 	const Elf_Sym *def;
149 	const Obj_Entry *defobj;
150 	Elf_Addr offs;
151 
152 
153 	offs = 0;
154 	if (ELF_R_SYM(rela->r_info) != 0) {
155 		def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj, flags,
156 			    NULL, lockstate);
157 		if (def == NULL)
158 			rtld_die();
159 		offs = def->st_value;
160 		obj = defobj;
161 		if (def->st_shndx == SHN_UNDEF) {
162 			/* Weak undefined thread variable */
163 			where[0] = (Elf_Addr)_rtld_tlsdesc_undef;
164 			where[1] = rela->r_addend;
165 			return;
166 		}
167 	}
168 	offs += rela->r_addend;
169 
170 	if (obj->tlsoffset != 0) {
171 		/* Variable is in initialy allocated TLS segment */
172 		where[0] = (Elf_Addr)_rtld_tlsdesc_static;
173 		where[1] = obj->tlsoffset + offs;
174 	} else {
175 		/* TLS offest is unknown at load time, use dynamic resolving */
176 		where[0] = (Elf_Addr)_rtld_tlsdesc_dynamic;
177 		where[1] = reloc_tlsdesc_alloc(obj->tlsindex, offs);
178 	}
179 }
180 
181 /*
182  * Process the PLT relocations.
183  */
184 int
185 reloc_plt(Obj_Entry *obj, int flags, RtldLockState *lockstate)
186 {
187 	const Elf_Rela *relalim;
188 	const Elf_Rela *rela;
189 
190 	relalim = (const Elf_Rela *)((const char *)obj->pltrela +
191 	    obj->pltrelasize);
192 	for (rela = obj->pltrela; rela < relalim; rela++) {
193 		Elf_Addr *where;
194 
195 		where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
196 
197 		switch(ELF_R_TYPE(rela->r_info)) {
198 		case R_AARCH64_JUMP_SLOT:
199 			*where += (Elf_Addr)obj->relocbase;
200 			break;
201 		case R_AARCH64_TLSDESC:
202 			reloc_tlsdesc(obj, rela, where, SYMLOOK_IN_PLT | flags,
203 			    lockstate);
204 			break;
205 		case R_AARCH64_IRELATIVE:
206 			obj->irelative = true;
207 			break;
208 		case R_AARCH64_NONE:
209 			break;
210 		default:
211 			_rtld_error("Unknown relocation type %u in PLT",
212 			    (unsigned int)ELF_R_TYPE(rela->r_info));
213 			return (-1);
214 		}
215 	}
216 
217 	return (0);
218 }
219 
220 /*
221  * LD_BIND_NOW was set - force relocation for all jump slots
222  */
223 int
224 reloc_jmpslots(Obj_Entry *obj, int flags, RtldLockState *lockstate)
225 {
226 	const Obj_Entry *defobj;
227 	const Elf_Rela *relalim;
228 	const Elf_Rela *rela;
229 	const Elf_Sym *def;
230 
231 	if (obj->jmpslots_done)
232 		return (0);
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, target;
238 
239 		where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
240 		switch(ELF_R_TYPE(rela->r_info)) {
241 		case R_AARCH64_JUMP_SLOT:
242 			def = find_symdef(ELF_R_SYM(rela->r_info), obj,
243 			    &defobj, SYMLOOK_IN_PLT | flags, NULL, lockstate);
244 			if (def == NULL)
245 				return (-1);
246 			if (ELF_ST_TYPE(def->st_info) == STT_GNU_IFUNC) {
247 				obj->gnu_ifunc = true;
248 				continue;
249 			}
250 			target = (Elf_Addr)(defobj->relocbase + def->st_value);
251 			reloc_jmpslot(where, target, defobj, obj,
252 			    (const Elf_Rel *)rela);
253 			break;
254 		}
255 	}
256 	obj->jmpslots_done = true;
257 
258 	return (0);
259 }
260 
261 int
262 reloc_iresolve(Obj_Entry *obj, struct Struct_RtldLockState *lockstate)
263 {
264 	const Elf_Rela *relalim;
265 	const Elf_Rela *rela;
266 	Elf_Addr *where, target, *ptr;
267 
268 	if (!obj->irelative)
269 		return (0);
270 	relalim = (const Elf_Rela *)((const char *)obj->pltrela + obj->pltrelasize);
271 	for (rela = obj->pltrela;  rela < relalim;  rela++) {
272 		if (ELF_R_TYPE(rela->r_info) == R_AARCH64_IRELATIVE) {
273 			ptr = (Elf_Addr *)(obj->relocbase + rela->r_addend);
274 			where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
275 			lock_release(rtld_bind_lock, lockstate);
276 			target = call_ifunc_resolver(ptr);
277 			wlock_acquire(rtld_bind_lock, lockstate);
278 			*where = target;
279 		}
280 	}
281 	obj->irelative = false;
282 	return (0);
283 }
284 
285 int
286 reloc_gnu_ifunc(Obj_Entry *obj, int flags,
287    struct Struct_RtldLockState *lockstate)
288 {
289 	const Elf_Rela *relalim;
290 	const Elf_Rela *rela;
291 	Elf_Addr *where, target;
292 	const Elf_Sym *def;
293 	const Obj_Entry *defobj;
294 
295 	if (!obj->gnu_ifunc)
296 		return (0);
297 	relalim = (const Elf_Rela *)((const char *)obj->pltrela + obj->pltrelasize);
298 	for (rela = obj->pltrela;  rela < relalim;  rela++) {
299 		if (ELF_R_TYPE(rela->r_info) == R_AARCH64_JUMP_SLOT) {
300 			where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
301 			def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
302 			    SYMLOOK_IN_PLT | flags, NULL, lockstate);
303 			if (def == NULL)
304 				return (-1);
305 			if (ELF_ST_TYPE(def->st_info) != STT_GNU_IFUNC)
306 				continue;
307 			lock_release(rtld_bind_lock, lockstate);
308 			target = (Elf_Addr)rtld_resolve_ifunc(defobj, def);
309 			wlock_acquire(rtld_bind_lock, lockstate);
310 			reloc_jmpslot(where, target, defobj, obj,
311 			    (const Elf_Rel *)rela);
312 		}
313 	}
314 	obj->gnu_ifunc = false;
315 	return (0);
316 }
317 
318 Elf_Addr
319 reloc_jmpslot(Elf_Addr *where, Elf_Addr target,
320     const Obj_Entry *defobj __unused, const Obj_Entry *obj __unused,
321     const Elf_Rel *rel)
322 {
323 
324 	assert(ELF_R_TYPE(rel->r_info) == R_AARCH64_JUMP_SLOT ||
325 	    ELF_R_TYPE(rel->r_info) == R_AARCH64_IRELATIVE);
326 
327 	if (*where != target && !ld_bind_not)
328 		*where = target;
329 	return (target);
330 }
331 
332 void
333 ifunc_init(Elf_Auxinfo aux_info[__min_size(AT_COUNT)] __unused)
334 {
335 
336 }
337 
338 void
339 pre_init(void)
340 {
341 
342 }
343 
344 /*
345  * Process non-PLT relocations
346  */
347 int
348 reloc_non_plt(Obj_Entry *obj, Obj_Entry *obj_rtld, int flags,
349     RtldLockState *lockstate)
350 {
351 	const Obj_Entry *defobj;
352 	const Elf_Rela *relalim;
353 	const Elf_Rela *rela;
354 	const Elf_Sym *def;
355 	SymCache *cache;
356 	Elf_Addr *where, symval;
357 
358 	/*
359 	 * The dynamic loader may be called from a thread, we have
360 	 * limited amounts of stack available so we cannot use alloca().
361 	 */
362 	if (obj == obj_rtld)
363 		cache = NULL;
364 	else
365 		cache = calloc(obj->dynsymcount, sizeof(SymCache));
366 		/* No need to check for NULL here */
367 
368 	relalim = (const Elf_Rela *)((const char *)obj->rela + obj->relasize);
369 	for (rela = obj->rela; rela < relalim; rela++) {
370 		/*
371 		 * First, resolve symbol for relocations which
372 		 * reference symbols.
373 		 */
374 		switch (ELF_R_TYPE(rela->r_info)) {
375 		case R_AARCH64_ABS64:
376 		case R_AARCH64_GLOB_DAT:
377 		case R_AARCH64_TLS_TPREL64:
378 		case R_AARCH64_TLS_DTPREL64:
379 		case R_AARCH64_TLS_DTPMOD64:
380 			def = find_symdef(ELF_R_SYM(rela->r_info), obj,
381 			    &defobj, flags, cache, lockstate);
382 			if (def == NULL)
383 				return (-1);
384 			/*
385 			 * If symbol is IFUNC, only perform relocation
386 			 * when caller allowed it by passing
387 			 * SYMLOOK_IFUNC flag.  Skip the relocations
388 			 * otherwise.
389 			 *
390 			 * Also error out in case IFUNC relocations
391 			 * are specified for TLS, which cannot be
392 			 * usefully interpreted.
393 			 */
394 			if (ELF_ST_TYPE(def->st_info) == STT_GNU_IFUNC) {
395 				switch (ELF_R_TYPE(rela->r_info)) {
396 				case R_AARCH64_ABS64:
397 				case R_AARCH64_GLOB_DAT:
398 					if ((flags & SYMLOOK_IFUNC) == 0) {
399 						obj->non_plt_gnu_ifunc = true;
400 						continue;
401 					}
402 					symval = (Elf_Addr)rtld_resolve_ifunc(
403 					    defobj, def);
404 					break;
405 				default:
406 					_rtld_error("%s: IFUNC for TLS reloc",
407 					    obj->path);
408 					return (-1);
409 				}
410 			} else {
411 				if ((flags & SYMLOOK_IFUNC) != 0)
412 					continue;
413 				symval = (Elf_Addr)defobj->relocbase +
414 				    def->st_value;
415 			}
416 			break;
417 		default:
418 			if ((flags & SYMLOOK_IFUNC) != 0)
419 				continue;
420 		}
421 
422 		where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
423 
424 		switch (ELF_R_TYPE(rela->r_info)) {
425 		case R_AARCH64_ABS64:
426 		case R_AARCH64_GLOB_DAT:
427 			*where = symval + rela->r_addend;
428 			break;
429 		case R_AARCH64_COPY:
430 			/*
431 			 * These are deferred until all other relocations have
432 			 * been done. All we do here is make sure that the
433 			 * COPY relocation is not in a shared library. They
434 			 * are allowed only in executable files.
435 			 */
436 			if (!obj->mainprog) {
437 				_rtld_error("%s: Unexpected R_AARCH64_COPY "
438 				    "relocation in shared library", obj->path);
439 				return (-1);
440 			}
441 			break;
442 		case R_AARCH64_TLSDESC:
443 			reloc_tlsdesc(obj, rela, where, flags, lockstate);
444 			break;
445 		case R_AARCH64_TLS_TPREL64:
446 			/*
447 			 * We lazily allocate offsets for static TLS as we
448 			 * see the first relocation that references the
449 			 * TLS block. This allows us to support (small
450 			 * amounts of) static TLS in dynamically loaded
451 			 * modules. If we run out of space, we generate an
452 			 * error.
453 			 */
454 			if (!defobj->tls_done) {
455 				if (!allocate_tls_offset(
456 				    __DECONST(Obj_Entry *, defobj))) {
457 					_rtld_error(
458 					    "%s: No space available for static "
459 					    "Thread Local Storage", obj->path);
460 					return (-1);
461 				}
462 			}
463 			/* Test weak undefined thread variable */
464 			if (def->st_shndx != SHN_UNDEF) {
465 				*where = def->st_value + rela->r_addend +
466 				    defobj->tlsoffset;
467 			} else {
468 				/*
469 				 * XXX We should relocate undefined thread
470 				 * weak variable address to NULL, but how?
471 				 * Can we return error in this situation?
472 				 */
473 				rtld_printf("%s: Unable to relocate undefined "
474 				"weak TLS variable\n", obj->path);
475 #if 0
476 				return (-1);
477 #else
478 				*where = def->st_value + rela->r_addend +
479 				    defobj->tlsoffset;
480 #endif
481 			}
482 			break;
483 
484 		/*
485 		 * !!! BEWARE !!!
486 		 * ARM ELF ABI defines TLS_DTPMOD64 as 1029, and TLS_DTPREL64
487 		 * as 1028. But actual bfd linker and the glibc RTLD linker
488 		 * treats TLS_DTPMOD64 as 1028 and TLS_DTPREL64 1029.
489 		 */
490 		case R_AARCH64_TLS_DTPREL64: /* efectively is TLS_DTPMOD64 */
491 			*where += (Elf_Addr)defobj->tlsindex;
492 			break;
493 		case R_AARCH64_TLS_DTPMOD64: /* efectively is TLS_DTPREL64 */
494 			*where += (Elf_Addr)(def->st_value + rela->r_addend);
495 			break;
496 		case R_AARCH64_RELATIVE:
497 			*where = (Elf_Addr)(obj->relocbase + rela->r_addend);
498 			break;
499 		case R_AARCH64_NONE:
500 			break;
501 		default:
502 			rtld_printf("%s: Unhandled relocation %lu\n",
503 			    obj->path, ELF_R_TYPE(rela->r_info));
504 			return (-1);
505 		}
506 	}
507 
508 	return (0);
509 }
510 
511 void
512 allocate_initial_tls(Obj_Entry *objs)
513 {
514 	Elf_Addr **tp;
515 
516 	/*
517 	* Fix the size of the static TLS block by using the maximum
518 	* offset allocated so far and adding a bit for dynamic modules to
519 	* use.
520 	*/
521 	tls_static_space = tls_last_offset + tls_last_size +
522 	    RTLD_STATIC_TLS_EXTRA;
523 
524 	tp = (Elf_Addr **) allocate_tls(objs, NULL, TLS_TCB_SIZE, 16);
525 
526 	asm volatile("msr	tpidr_el0, %0" : : "r"(tp));
527 }
528 
529 void *
530 __tls_get_addr(tls_index* ti)
531 {
532       char *p;
533       void *_tp;
534 
535       __asm __volatile("mrs	%0, tpidr_el0"  : "=r" (_tp));
536       p = tls_get_addr_common((Elf_Addr **)(_tp), ti->ti_module, ti->ti_offset);
537 
538       return (p);
539 }
540