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