Home | History | Annotate | Line # | Download | only in x86_64
      1 /*	$NetBSD: mdreloc.c,v 1.50 2025/04/16 18:10:46 riastradh Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2001 Wasabi Systems, Inc.
      5  * All rights reserved.
      6  *
      7  * Written by Frank van der Linden for Wasabi Systems, Inc.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. All advertising materials mentioning features or use of this software
     18  *    must display the following acknowledgement:
     19  *      This product includes software developed for the NetBSD Project by
     20  *      Wasabi Systems, Inc.
     21  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     22  *    or promote products derived from this software without specific prior
     23  *    written permission.
     24  *
     25  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35  * POSSIBILITY OF SUCH DAMAGE.
     36  */
     37 
     38 /*
     39  * Copyright 1996 John D. Polstra.
     40  * Copyright 1996 Matt Thomas <matt (at) 3am-software.com>
     41  * All rights reserved.
     42  *
     43  * Redistribution and use in source and binary forms, with or without
     44  * modification, are permitted provided that the following conditions
     45  * are met:
     46  * 1. Redistributions of source code must retain the above copyright
     47  *    notice, this list of conditions and the following disclaimer.
     48  * 2. Redistributions in binary form must reproduce the above copyright
     49  *    notice, this list of conditions and the following disclaimer in the
     50  *    documentation and/or other materials provided with the distribution.
     51  * 3. All advertising materials mentioning features or use of this software
     52  *    must display the following acknowledgement:
     53  *      This product includes software developed by John Polstra.
     54  * 4. The name of the author may not be used to endorse or promote products
     55  *    derived from this software without specific prior written permission.
     56  *
     57  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     58  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     59  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     60  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     61  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     62  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     63  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     64  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     65  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     66  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     67  */
     68 
     69 /*
     70  * amd64 ELF relocations.
     71  *
     72  * References:
     73  *
     74  *	[AMD64psABI] System V Application Binary Interface: AMD64
     75  *	Architecture Processor Supplement, Draft Version 0.99.6,
     76  *	2013-10-07.
     77  *	https://web.archive.org/web/20160801075146/http://www.x86-64.org/documentation/abi.pdf
     78  *	Source code: https://gitlab.com/x86-psABIs/x86-64-ABI
     79  */
     80 
     81 #include <sys/cdefs.h>
     82 #ifndef lint
     83 __RCSID("$NetBSD: mdreloc.c,v 1.50 2025/04/16 18:10:46 riastradh Exp $");
     84 #endif /* not lint */
     85 
     86 #include <sys/types.h>
     87 #include <sys/mman.h>
     88 #include <errno.h>
     89 #include <fcntl.h>
     90 #include <stdarg.h>
     91 #include <stdio.h>
     92 #include <stdlib.h>
     93 #include <string.h>
     94 #include <unistd.h>
     95 #include <elf.h>
     96 
     97 #include "debug.h"
     98 #include "rtld.h"
     99 
    100 void _rtld_bind_start(void);
    101 void _rtld_relocate_nonplt_self(Elf_Dyn *, Elf_Addr);
    102 caddr_t _rtld_bind(const Obj_Entry *, Elf_Word);
    103 static inline int _rtld_relocate_plt_object(const Obj_Entry *,
    104     const Elf_Rela *, Elf_Addr *);
    105 
    106 #define rdbg_symname(obj, rela) \
    107     ((obj)->strtab + (obj)->symtab[ELF_R_SYM((rela)->r_info)].st_name)
    108 
    109 void
    110 _rtld_setup_pltgot(const Obj_Entry *obj)
    111 {
    112 	obj->pltgot[1] = (Elf_Addr) obj;
    113 	obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start;
    114 }
    115 
    116 void
    117 _rtld_relocate_nonplt_self(Elf_Dyn *dynp, Elf_Addr relocbase)
    118 {
    119 	const Elf_Rela *rela = 0, *relalim;
    120 	Elf_Addr relasz = 0;
    121 	Elf_Addr *where;
    122 
    123 	for (; dynp->d_tag != DT_NULL; dynp++) {
    124 		switch (dynp->d_tag) {
    125 		case DT_RELA:
    126 			rela = (const Elf_Rela *)(relocbase + dynp->d_un.d_ptr);
    127 			break;
    128 		case DT_RELASZ:
    129 			relasz = dynp->d_un.d_val;
    130 			break;
    131 		}
    132 	}
    133 	/*
    134 	 * Assume only 64-bit relocations here, which should always
    135 	 * be true for the dynamic linker.
    136 	 */
    137 	relalim = (const Elf_Rela *)((const uint8_t *)rela + relasz);
    138 	for (; rela < relalim; rela++) {
    139 		where = (Elf_Addr *)(relocbase + rela->r_offset);
    140 		*where = (Elf_Addr)(relocbase + rela->r_addend);
    141 	}
    142 }
    143 
    144 int
    145 _rtld_relocate_nonplt_objects(Obj_Entry *obj)
    146 {
    147 	const Elf_Rela *rela;
    148 	const Elf_Sym *def = NULL;
    149 	const Obj_Entry *defobj = NULL;
    150 	unsigned long last_symnum = ULONG_MAX;
    151 
    152 	for (rela = obj->rela; rela < obj->relalim; rela++) {
    153 		Elf64_Addr *where64;
    154 		Elf32_Addr *where32;
    155 		Elf64_Addr tmp64;
    156 		Elf32_Addr tmp32;
    157 		unsigned long symnum;
    158 
    159 		where64 = (Elf64_Addr *)(obj->relocbase + rela->r_offset);
    160 		where32 = (Elf32_Addr *)where64;
    161 
    162 		switch (ELF_R_TYPE(rela->r_info)) {
    163 		case R_TYPE(32):	/* word32 S + A, truncate */
    164 		case R_TYPE(32S):	/* word32 S + A, signed truncate */
    165 		case R_TYPE(GOT32):	/* word32 G + A (XXX can we see these?) */
    166 		case R_TYPE(64):	/* word64 S + A */
    167 		case R_TYPE(PC32):	/* word32 S + A - P */
    168 		case R_TYPE(GLOB_DAT):	/* word64 S */
    169 		case R_TYPE(TPOFF64):
    170 		case R_TYPE(DTPMOD64):
    171 		case R_TYPE(DTPOFF64):
    172 			symnum = ELF_R_SYM(rela->r_info);
    173 			if (last_symnum != symnum) {
    174 				last_symnum = symnum;
    175 				def = _rtld_find_symdef(symnum, obj, &defobj,
    176 				    false);
    177 				if (def == NULL)
    178 					return -1;
    179 			}
    180 			break;
    181 		default:
    182 			break;
    183 		}
    184 
    185 		switch (ELF_R_TYPE(rela->r_info)) {
    186 		case R_TYPE(NONE):
    187 			break;
    188 
    189 		case R_TYPE(32):	/* word32 S + A, truncate */
    190 		case R_TYPE(32S):	/* word32 S + A, signed truncate */
    191 		case R_TYPE(GOT32):	/* word32 G + A (XXX can we see these?) */
    192 			tmp32 = (Elf32_Addr)(u_long)(defobj->relocbase +
    193 			    def->st_value + rela->r_addend);
    194 
    195 			if (*where32 != tmp32)
    196 				*where32 = tmp32;
    197 			rdbg(("32/32S %s in %s --> %p in %s",
    198 			    rdbg_symname(obj, rela),
    199 			    obj->path, (void *)(uintptr_t)*where32,
    200 			    defobj->path));
    201 			break;
    202 		case R_TYPE(64):	/* word64 S + A */
    203 			tmp64 = (Elf64_Addr)(defobj->relocbase + def->st_value +
    204 			    rela->r_addend);
    205 
    206 			if (*where64 != tmp64)
    207 				*where64 = tmp64;
    208 			rdbg(("64 %s in %s --> %p in %s",
    209 			    rdbg_symname(obj, rela),
    210 			    obj->path, (void *)*where64, defobj->path));
    211 			break;
    212 		case R_TYPE(PC32):	/* word32 S + A - P */
    213 			tmp32 = (Elf32_Addr)(u_long)(defobj->relocbase +
    214 			    def->st_value + rela->r_addend -
    215 			    (Elf64_Addr)where64);
    216 			if (*where32 != tmp32)
    217 				*where32 = tmp32;
    218 			rdbg(("PC32 %s in %s --> %p in %s",
    219 			    rdbg_symname(obj, rela),
    220 			    obj->path, (void *)(unsigned long)*where32,
    221 			    defobj->path));
    222 			break;
    223 		case R_TYPE(GLOB_DAT):	/* word64 S */
    224 			tmp64 = (Elf64_Addr)(defobj->relocbase + def->st_value);
    225 
    226 			if (*where64 != tmp64)
    227 				*where64 = tmp64;
    228 			rdbg(("64 %s in %s --> %p in %s",
    229 			    rdbg_symname(obj, rela),
    230 			    obj->path, (void *)*where64, defobj->path));
    231 			break;
    232 		case R_TYPE(RELATIVE):  /* word64 B + A */
    233 			tmp64 = (Elf64_Addr)(obj->relocbase + rela->r_addend);
    234 			if (*where64 != tmp64)
    235 				*where64 = tmp64;
    236 			rdbg(("RELATIVE in %s --> %p", obj->path,
    237 			    (void *)*where64));
    238        			break;
    239 
    240 		case R_TYPE(TPOFF64):
    241 			if (!defobj->tls_static &&
    242 			    _rtld_tls_offset_allocate(__UNCONST(defobj)))
    243 				return -1;
    244 
    245 			*where64 = (Elf64_Addr)(def->st_value -
    246 			    defobj->tlsoffset + rela->r_addend);
    247 
    248 			rdbg(("TPOFF64 %s in %s --> %p",
    249 			    rdbg_symname(obj, rela),
    250 			    obj->path, (void *)*where64));
    251 
    252 			break;
    253 
    254 		case R_TYPE(DTPMOD64):
    255 			*where64 = (Elf64_Addr)defobj->tlsindex;
    256 
    257 			rdbg(("DTPMOD64 %s in %s --> %p",
    258 			    rdbg_symname(obj, rela),
    259 			    obj->path, (void *)*where64));
    260 
    261 			break;
    262 
    263 		case R_TYPE(DTPOFF64):
    264 			*where64 = (Elf64_Addr)(def->st_value + rela->r_addend);
    265 
    266 			rdbg(("DTPOFF64 %s in %s --> %p",
    267 			    rdbg_symname(obj, rela),
    268 			    obj->path, (void *)*where64));
    269 
    270 			break;
    271 
    272 		case R_TYPE(COPY):
    273 			rdbg(("COPY"));
    274 			break;
    275 
    276 		case R_TYPE(IRELATIVE):
    277 			/* IFUNC relocations are handled in _rtld_call_ifunc */
    278 			if (obj->ifunc_remaining_nonplt == 0) {
    279 				obj->ifunc_remaining_nonplt =
    280 				    obj->relalim - rela;
    281 			}
    282 			break;
    283 
    284 		default:
    285 			rdbg(("sym = %lu, type = %lu, offset = %p, "
    286 			    "addend = %p, contents = %p, symbol = %s",
    287 			    (u_long)ELF_R_SYM(rela->r_info),
    288 			    (u_long)ELF_R_TYPE(rela->r_info),
    289 			    (void *)rela->r_offset, (void *)rela->r_addend,
    290 			    (void *)*where64, rdbg_symname(obj, rela)));
    291 			_rtld_error("%s: Unsupported relocation type %ld "
    292 			    "in non-PLT relocations",
    293 			    obj->path, (u_long) ELF_R_TYPE(rela->r_info));
    294 			return -1;
    295 		}
    296 	}
    297 	return 0;
    298 }
    299 
    300 int
    301 _rtld_relocate_plt_lazy(Obj_Entry *obj)
    302 {
    303 	const Elf_Rela *rela;
    304 
    305 	for (rela = obj->pltrelalim; rela-- > obj->pltrela; ) {
    306 		Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
    307 
    308 		assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JUMP_SLOT) ||
    309 		       ELF_R_TYPE(rela->r_info) == R_TYPE(IRELATIVE));
    310 
    311 		if (ELF_R_TYPE(rela->r_info) == R_TYPE(IRELATIVE))
    312 			obj->ifunc_remaining = obj->pltrelalim - rela;
    313 
    314 		/* Just relocate the GOT slots pointing into the PLT */
    315 		*where += (Elf_Addr)obj->relocbase;
    316 		rdbg(("fixup !main in %s --> %p", obj->path, (void *)*where));
    317 	}
    318 
    319 	return 0;
    320 }
    321 
    322 static inline int
    323 _rtld_relocate_plt_object(const Obj_Entry *obj, const Elf_Rela *rela, Elf_Addr *tp)
    324 {
    325 	Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
    326 	Elf_Addr new_value;
    327 	const Elf_Sym  *def;
    328 	const Obj_Entry *defobj;
    329 	unsigned long info = rela->r_info;
    330 
    331 	if (ELF_R_TYPE(info) == R_TYPE(IRELATIVE))
    332 		return 0;
    333 
    334 	assert(ELF_R_TYPE(info) == R_TYPE(JUMP_SLOT));
    335 
    336 	def = _rtld_find_plt_symdef(ELF_R_SYM(info), obj, &defobj, tp != NULL);
    337 	if (__predict_false(def == NULL))
    338 		return -1;
    339 	if (__predict_false(def == &_rtld_sym_zero))
    340 		return 0;
    341 
    342 	if (ELF_ST_TYPE(def->st_info) == STT_GNU_IFUNC) {
    343 		if (tp == NULL)
    344 			return 0;
    345 		new_value = _rtld_resolve_ifunc(defobj, def);
    346 	} else {
    347 		new_value = (Elf_Addr)(defobj->relocbase + def->st_value +
    348 		    rela->r_addend);
    349 	}
    350 
    351 	rdbg(("bind now/fixup in %s --> old=%p new=%p",
    352 	    defobj->strtab + def->st_name, (void *)*where, (void *)new_value));
    353 	if (*where != new_value)
    354 		*where = new_value;
    355 
    356 	if (tp)
    357 		*tp = new_value - rela->r_addend;
    358 
    359 	return 0;
    360 }
    361 
    362 caddr_t
    363 _rtld_bind(const Obj_Entry *obj, Elf_Word reloff)
    364 {
    365 	const Elf_Rela *rela = obj->pltrela + reloff;
    366 	Elf_Addr new_value;
    367 	int error;
    368 
    369 	new_value = 0; /* XXX GCC4 */
    370 
    371 	_rtld_shared_enter();
    372 	error = _rtld_relocate_plt_object(obj, rela, &new_value);
    373 	if (error)
    374 		_rtld_die();
    375 	_rtld_shared_exit();
    376 
    377 	return (caddr_t)new_value;
    378 }
    379 
    380 int
    381 _rtld_relocate_plt_objects(const Obj_Entry *obj)
    382 {
    383 	const Elf_Rela *rela;
    384 
    385 	for (rela = obj->pltrela; rela < obj->pltrelalim; rela++)
    386 		if (_rtld_relocate_plt_object(obj, rela, NULL) < 0)
    387 			return -1;
    388 
    389 	return 0;
    390 }
    391