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