xref: /freebsd/libexec/rtld-elf/amd64/reloc.c (revision e6bfd18d21b225af6a0ed67ceeaf1293b7b9eba5)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright 1996, 1997, 1998, 1999 John D. Polstra.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  *
27  * $FreeBSD$
28  */
29 
30 /*
31  * Dynamic linker for ELF.
32  *
33  * John Polstra <jdp@polstra.com>.
34  */
35 
36 #define _WANT_P_OSREL
37 #include <sys/param.h>
38 #include <sys/mman.h>
39 #include <machine/cpufunc.h>
40 #include <machine/specialreg.h>
41 #include <machine/sysarch.h>
42 
43 #include <dlfcn.h>
44 #include <err.h>
45 #include <errno.h>
46 #include <fcntl.h>
47 #include <stdarg.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include <unistd.h>
52 
53 #include "debug.h"
54 #include "rtld.h"
55 #include "rtld_tls.h"
56 
57 /*
58  * Process the special R_X86_64_COPY relocations in the main program.  These
59  * copy data from a shared object into a region in the main program's BSS
60  * segment.
61  *
62  * Returns 0 on success, -1 on failure.
63  */
64 int
65 do_copy_relocations(Obj_Entry *dstobj)
66 {
67     const Elf_Rela *relalim;
68     const Elf_Rela *rela;
69 
70     assert(dstobj->mainprog);	/* COPY relocations are invalid elsewhere */
71 
72     relalim = (const Elf_Rela *)((const char *) dstobj->rela + dstobj->relasize);
73     for (rela = dstobj->rela;  rela < relalim;  rela++) {
74 	if (ELF_R_TYPE(rela->r_info) == R_X86_64_COPY) {
75 	    void *dstaddr;
76 	    const Elf_Sym *dstsym;
77 	    const char *name;
78 	    size_t size;
79 	    const void *srcaddr;
80 	    const Elf_Sym *srcsym;
81 	    const Obj_Entry *srcobj, *defobj;
82 	    SymLook req;
83 	    int res;
84 
85 	    dstaddr = (void *)(dstobj->relocbase + rela->r_offset);
86 	    dstsym = dstobj->symtab + ELF_R_SYM(rela->r_info);
87 	    name = dstobj->strtab + dstsym->st_name;
88 	    size = dstsym->st_size;
89 	    symlook_init(&req, name);
90 	    req.ventry = fetch_ventry(dstobj, ELF_R_SYM(rela->r_info));
91 	    req.flags = SYMLOOK_EARLY;
92 
93 	    for (srcobj = globallist_next(dstobj); srcobj != NULL;
94 	      srcobj = globallist_next(srcobj)) {
95 		res = symlook_obj(&req, srcobj);
96 		if (res == 0) {
97 		    srcsym = req.sym_out;
98 		    defobj = req.defobj_out;
99 		    break;
100 		}
101 	    }
102 
103 	    if (srcobj == NULL) {
104 		_rtld_error("Undefined symbol \"%s\" referenced from COPY"
105 		  " relocation in %s", name, dstobj->path);
106 		return -1;
107 	    }
108 
109 	    srcaddr = (const void *)(defobj->relocbase + srcsym->st_value);
110 	    memcpy(dstaddr, srcaddr, size);
111 	}
112     }
113 
114     return 0;
115 }
116 
117 /* Initialize the special GOT entries. */
118 void
119 init_pltgot(Obj_Entry *obj)
120 {
121     if (obj->pltgot != NULL) {
122 	obj->pltgot[1] = (Elf_Addr) obj;
123 	obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start;
124     }
125 }
126 
127 /* Process the non-PLT relocations. */
128 int
129 reloc_non_plt(Obj_Entry *obj, Obj_Entry *obj_rtld, int flags,
130     RtldLockState *lockstate)
131 {
132 	const Elf_Rela *relalim;
133 	const Elf_Rela *rela;
134 	SymCache *cache;
135 	const Elf_Sym *def;
136 	const Obj_Entry *defobj;
137 	Elf_Addr *where, symval;
138 	Elf32_Addr *where32;
139 	int r;
140 
141 	r = -1;
142 	symval = 0;
143 	def = NULL;
144 
145 	/*
146 	 * The dynamic loader may be called from a thread, we have
147 	 * limited amounts of stack available so we cannot use alloca().
148 	 */
149 	if (obj != obj_rtld) {
150 		cache = calloc(obj->dynsymcount, sizeof(SymCache));
151 		/* No need to check for NULL here */
152 	} else
153 		cache = NULL;
154 
155 	relalim = (const Elf_Rela *)((const char*)obj->rela + obj->relasize);
156 	for (rela = obj->rela;  rela < relalim;  rela++) {
157 		/*
158 		 * First, resolve symbol for relocations which
159 		 * reference symbols.
160 		 */
161 		switch (ELF_R_TYPE(rela->r_info)) {
162 		case R_X86_64_64:
163 		case R_X86_64_PC32:
164 		case R_X86_64_GLOB_DAT:
165 		case R_X86_64_TPOFF64:
166 		case R_X86_64_TPOFF32:
167 		case R_X86_64_DTPMOD64:
168 		case R_X86_64_DTPOFF64:
169 		case R_X86_64_DTPOFF32:
170 			def = find_symdef(ELF_R_SYM(rela->r_info), obj,
171 			    &defobj, flags, cache, lockstate);
172 			if (def == NULL)
173 				goto done;
174 			/*
175 			 * If symbol is IFUNC, only perform relocation
176 			 * when caller allowed it by passing
177 			 * SYMLOOK_IFUNC flag.  Skip the relocations
178 			 * otherwise.
179 			 *
180 			 * Also error out in case IFUNC relocations
181 			 * are specified for TLS, which cannot be
182 			 * usefully interpreted.
183 			 */
184 			if (ELF_ST_TYPE(def->st_info) == STT_GNU_IFUNC) {
185 				switch (ELF_R_TYPE(rela->r_info)) {
186 				case R_X86_64_64:
187 				case R_X86_64_PC32:
188 				case R_X86_64_GLOB_DAT:
189 					if ((flags & SYMLOOK_IFUNC) == 0) {
190 						obj->non_plt_gnu_ifunc = true;
191 						continue;
192 					}
193 					symval = (Elf_Addr)rtld_resolve_ifunc(
194 					    defobj, def);
195 					break;
196 				case R_X86_64_TPOFF64:
197 				case R_X86_64_TPOFF32:
198 				case R_X86_64_DTPMOD64:
199 				case R_X86_64_DTPOFF64:
200 				case R_X86_64_DTPOFF32:
201 					_rtld_error("%s: IFUNC for TLS reloc",
202 					    obj->path);
203 					goto done;
204 				}
205 			} else {
206 				if ((flags & SYMLOOK_IFUNC) != 0)
207 					continue;
208 				symval = (Elf_Addr)defobj->relocbase +
209 				    def->st_value;
210 			}
211 			break;
212 		default:
213 			if ((flags & SYMLOOK_IFUNC) != 0)
214 				continue;
215 			break;
216 		}
217 		where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
218 		where32 = (Elf32_Addr *)where;
219 
220 		switch (ELF_R_TYPE(rela->r_info)) {
221 		case R_X86_64_NONE:
222 			break;
223 		case R_X86_64_64:
224 			*where = symval + rela->r_addend;
225 			break;
226 		case R_X86_64_PC32:
227 			/*
228 			 * I don't think the dynamic linker should
229 			 * ever see this type of relocation.  But the
230 			 * binutils-2.6 tools sometimes generate it.
231 			 */
232 			*where32 = (Elf32_Addr)(unsigned long)(symval +
233 		            rela->r_addend - (Elf_Addr)where);
234 			break;
235 		/* missing: R_X86_64_GOT32 R_X86_64_PLT32 */
236 		case R_X86_64_COPY:
237 			/*
238 			 * These are deferred until all other relocations have
239 			 * been done.  All we do here is make sure that the COPY
240 			 * relocation is not in a shared library.  They are
241 			 * allowed only in executable files.
242 			 */
243 			if (!obj->mainprog) {
244 				_rtld_error("%s: Unexpected R_X86_64_COPY "
245 				    "relocation in shared library", obj->path);
246 				goto done;
247 			}
248 			break;
249 		case R_X86_64_GLOB_DAT:
250 			*where = symval;
251 			break;
252 		case R_X86_64_TPOFF64:
253 			/*
254 			 * We lazily allocate offsets for static TLS
255 			 * as we see the first relocation that
256 			 * references the TLS block. This allows us to
257 			 * support (small amounts of) static TLS in
258 			 * dynamically loaded modules. If we run out
259 			 * of space, we generate an error.
260 			 */
261 			if (!defobj->tls_static) {
262 				if (!allocate_tls_offset(
263 				    __DECONST(Obj_Entry *, defobj))) {
264 					_rtld_error("%s: No space available "
265 					    "for static Thread Local Storage",
266 					    obj->path);
267 					goto done;
268 				}
269 			}
270 			*where = (Elf_Addr)(def->st_value - defobj->tlsoffset +
271 			    rela->r_addend);
272 			break;
273 		case R_X86_64_TPOFF32:
274 			/*
275 			 * We lazily allocate offsets for static TLS
276 			 * as we see the first relocation that
277 			 * references the TLS block. This allows us to
278 			 * support (small amounts of) static TLS in
279 			 * dynamically loaded modules. If we run out
280 			 * of space, we generate an error.
281 			 */
282 			if (!defobj->tls_static) {
283 				if (!allocate_tls_offset(
284 				    __DECONST(Obj_Entry *, defobj))) {
285 					_rtld_error("%s: No space available "
286 					    "for static Thread Local Storage",
287 					    obj->path);
288 					goto done;
289 				}
290 			}
291 			*where32 = (Elf32_Addr)(def->st_value -
292 			    defobj->tlsoffset + rela->r_addend);
293 			break;
294 		case R_X86_64_DTPMOD64:
295 			*where += (Elf_Addr)defobj->tlsindex;
296 			break;
297 		case R_X86_64_DTPOFF64:
298 			*where += (Elf_Addr)(def->st_value + rela->r_addend);
299 			break;
300 		case R_X86_64_DTPOFF32:
301 			*where32 += (Elf32_Addr)(def->st_value +
302 			    rela->r_addend);
303 			break;
304 		case R_X86_64_RELATIVE:
305 			*where = (Elf_Addr)(obj->relocbase + rela->r_addend);
306 			break;
307 		case R_X86_64_IRELATIVE:
308 			obj->irelative_nonplt = true;
309 			break;
310 
311 		/*
312 		 * missing:
313 		 * R_X86_64_GOTPCREL, R_X86_64_32, R_X86_64_32S, R_X86_64_16,
314 		 * R_X86_64_PC16, R_X86_64_8, R_X86_64_PC8
315 		 */
316 		default:
317 			_rtld_error("%s: Unsupported relocation type %u"
318 			    " in non-PLT relocations\n", obj->path,
319 			    (unsigned int)ELF_R_TYPE(rela->r_info));
320 			goto done;
321 		}
322 	}
323 	r = 0;
324 done:
325 	free(cache);
326 	return (r);
327 }
328 
329 /* Process the PLT relocations. */
330 int
331 reloc_plt(Obj_Entry *obj, int flags __unused, RtldLockState *lockstate __unused)
332 {
333     const Elf_Rela *relalim;
334     const Elf_Rela *rela;
335 
336     relalim = (const Elf_Rela *)((const char *)obj->pltrela + obj->pltrelasize);
337     for (rela = obj->pltrela;  rela < relalim;  rela++) {
338 	Elf_Addr *where;
339 
340 	switch(ELF_R_TYPE(rela->r_info)) {
341 	case R_X86_64_JMP_SLOT:
342 	  /* Relocate the GOT slot pointing into the PLT. */
343 	  where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
344 	  *where += (Elf_Addr)obj->relocbase;
345 	  break;
346 
347 	case R_X86_64_IRELATIVE:
348 	  obj->irelative = true;
349 	  break;
350 
351 	default:
352 	  _rtld_error("Unknown relocation type %x in PLT",
353 	    (unsigned int)ELF_R_TYPE(rela->r_info));
354 	  return (-1);
355 	}
356     }
357     return 0;
358 }
359 
360 /* Relocate the jump slots in an object. */
361 int
362 reloc_jmpslots(Obj_Entry *obj, int flags, RtldLockState *lockstate)
363 {
364     const Elf_Rela *relalim;
365     const Elf_Rela *rela;
366 
367     if (obj->jmpslots_done)
368 	return 0;
369     relalim = (const Elf_Rela *)((const char *)obj->pltrela + obj->pltrelasize);
370     for (rela = obj->pltrela;  rela < relalim;  rela++) {
371 	Elf_Addr *where, target;
372 	const Elf_Sym *def;
373 	const Obj_Entry *defobj;
374 
375 	switch (ELF_R_TYPE(rela->r_info)) {
376 	case R_X86_64_JMP_SLOT:
377 	  where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
378 	  def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
379 		SYMLOOK_IN_PLT | flags, NULL, lockstate);
380 	  if (def == NULL)
381 	      return (-1);
382 	  if (ELF_ST_TYPE(def->st_info) == STT_GNU_IFUNC) {
383 	      obj->gnu_ifunc = true;
384 	      continue;
385 	  }
386 	  target = (Elf_Addr)(defobj->relocbase + def->st_value + rela->r_addend);
387 	  reloc_jmpslot(where, target, defobj, obj, (const Elf_Rel *)rela);
388 	  break;
389 
390 	case R_X86_64_IRELATIVE:
391 	  break;
392 
393 	default:
394 	  _rtld_error("Unknown relocation type %x in PLT",
395 	    (unsigned int)ELF_R_TYPE(rela->r_info));
396 	  return (-1);
397 	}
398     }
399     obj->jmpslots_done = true;
400     return 0;
401 }
402 
403 /* Fixup the jump slot at "where" to transfer control to "target". */
404 Elf_Addr
405 reloc_jmpslot(Elf_Addr *where, Elf_Addr target,
406     const struct Struct_Obj_Entry *obj  __unused,
407     const struct Struct_Obj_Entry *refobj  __unused,
408     const Elf_Rel *rel  __unused)
409 {
410 #ifdef dbg
411 	dbg("reloc_jmpslot: *%p = %p", where, (void *)target);
412 #endif
413 	if (!ld_bind_not)
414 		*where = target;
415 	return (target);
416 }
417 
418 static void
419 reloc_iresolve_one(Obj_Entry *obj, const Elf_Rela *rela,
420     RtldLockState *lockstate)
421 {
422 	Elf_Addr *where, target, *ptr;
423 
424 	ptr = (Elf_Addr *)(obj->relocbase + rela->r_addend);
425 	where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
426 	lock_release(rtld_bind_lock, lockstate);
427 	target = call_ifunc_resolver(ptr);
428 	wlock_acquire(rtld_bind_lock, lockstate);
429 	*where = target;
430 }
431 
432 int
433 reloc_iresolve(Obj_Entry *obj, RtldLockState *lockstate)
434 {
435 	const Elf_Rela *relalim;
436 	const Elf_Rela *rela;
437 
438 	if (!obj->irelative)
439 		return (0);
440 	obj->irelative = false;
441 	relalim = (const Elf_Rela *)((const char *)obj->pltrela +
442 	    obj->pltrelasize);
443 	for (rela = obj->pltrela;  rela < relalim;  rela++) {
444 		if (ELF_R_TYPE(rela->r_info) == R_X86_64_IRELATIVE)
445 			reloc_iresolve_one(obj, rela, lockstate);
446 	}
447 	return (0);
448 }
449 
450 int
451 reloc_iresolve_nonplt(Obj_Entry *obj, RtldLockState *lockstate)
452 {
453 	const Elf_Rela *relalim;
454 	const Elf_Rela *rela;
455 
456 	if (!obj->irelative_nonplt)
457 		return (0);
458 	obj->irelative_nonplt = false;
459 	relalim = (const Elf_Rela *)((const char *)obj->rela + obj->relasize);
460 	for (rela = obj->rela;  rela < relalim;  rela++) {
461 		if (ELF_R_TYPE(rela->r_info) == R_X86_64_IRELATIVE)
462 			reloc_iresolve_one(obj, rela, lockstate);
463 	}
464 	return (0);
465 }
466 
467 int
468 reloc_gnu_ifunc(Obj_Entry *obj, int flags, RtldLockState *lockstate)
469 {
470     const Elf_Rela *relalim;
471     const Elf_Rela *rela;
472 
473     if (!obj->gnu_ifunc)
474 	return (0);
475     relalim = (const Elf_Rela *)((const char *)obj->pltrela + obj->pltrelasize);
476     for (rela = obj->pltrela;  rela < relalim;  rela++) {
477 	Elf_Addr *where, target;
478 	const Elf_Sym *def;
479 	const Obj_Entry *defobj;
480 
481 	switch (ELF_R_TYPE(rela->r_info)) {
482 	case R_X86_64_JMP_SLOT:
483 	  where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
484 	  def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
485 		SYMLOOK_IN_PLT | flags, NULL, lockstate);
486 	  if (def == NULL)
487 	      return (-1);
488 	  if (ELF_ST_TYPE(def->st_info) != STT_GNU_IFUNC)
489 	      continue;
490 	  lock_release(rtld_bind_lock, lockstate);
491 	  target = (Elf_Addr)rtld_resolve_ifunc(defobj, def);
492 	  wlock_acquire(rtld_bind_lock, lockstate);
493 	  reloc_jmpslot(where, target, defobj, obj, (const Elf_Rel *)rela);
494 	  break;
495 	}
496     }
497     obj->gnu_ifunc = false;
498     return (0);
499 }
500 
501 uint32_t cpu_feature, cpu_feature2, cpu_stdext_feature, cpu_stdext_feature2;
502 
503 void
504 ifunc_init(Elf_Auxinfo aux_info[__min_size(AT_COUNT)] __unused)
505 {
506 	u_int p[4], cpu_high;
507 
508 	do_cpuid(1, p);
509 	cpu_feature = p[3];
510 	cpu_feature2 = p[2];
511 	do_cpuid(0, p);
512 	cpu_high = p[0];
513 	if (cpu_high >= 7) {
514 		cpuid_count(7, 0, p);
515 		cpu_stdext_feature = p[1];
516 		cpu_stdext_feature2 = p[2];
517 	}
518 }
519 
520 int __getosreldate(void);
521 
522 void
523 allocate_initial_tls(Obj_Entry *objs)
524 {
525 	void *addr;
526 
527 	/*
528 	 * Fix the size of the static TLS block by using the maximum
529 	 * offset allocated so far and adding a bit for dynamic
530 	 * modules to use.
531 	 */
532 	tls_static_space = tls_last_offset + RTLD_STATIC_TLS_EXTRA;
533 
534 	addr = allocate_tls(objs, 0, TLS_TCB_SIZE, TLS_TCB_ALIGN);
535 
536 	/*
537 	 * This does not use _tcb_set() as it calls amd64_set_fsbase()
538 	 * which is an ifunc and rtld must not use ifuncs.
539 	 */
540 	if (__getosreldate() >= P_OSREL_WRFSBASE &&
541 	    (cpu_stdext_feature & CPUID_STDEXT_FSGSBASE) != 0)
542 		wrfsbase((uintptr_t)addr);
543 	else
544 		sysarch(AMD64_SET_FSBASE, &addr);
545 }
546 
547 void *
548 __tls_get_addr(tls_index *ti)
549 {
550 	uintptr_t **dtvp;
551 
552 	dtvp = &_tcb_get()->tcb_dtv;
553 	return (tls_get_addr_common(dtvp, ti->ti_module, ti->ti_offset));
554 }
555 
556 size_t
557 calculate_tls_offset(size_t prev_offset, size_t prev_size __unused,
558     size_t size, size_t align, size_t offset)
559 {
560 	size_t res;
561 
562         /*
563 	 * res is the smallest integer satisfying res - prev_offset >= size
564          * and (-res) % p_align = p_vaddr % p_align (= p_offset % p_align).
565 	 */
566         res = prev_offset + size + align - 1;
567         res -= (res + offset) & (align - 1);
568         return (res);
569 }
570 
571 size_t
572 calculate_first_tls_offset(size_t size, size_t align, size_t offset)
573 {
574 	return (calculate_tls_offset(0, 0, size, align, offset));
575 }
576