Home | History | Annotate | Line # | Download | only in x86_64
mdreloc.c revision 1.49
      1 /*	$NetBSD: mdreloc.c,v 1.49 2024/08/03 21:59:59 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 #include <sys/cdefs.h>
     70 #ifndef lint
     71 __RCSID("$NetBSD: mdreloc.c,v 1.49 2024/08/03 21:59:59 riastradh Exp $");
     72 #endif /* not lint */
     73 
     74 #include <sys/types.h>
     75 #include <sys/mman.h>
     76 #include <errno.h>
     77 #include <fcntl.h>
     78 #include <stdarg.h>
     79 #include <stdio.h>
     80 #include <stdlib.h>
     81 #include <string.h>
     82 #include <unistd.h>
     83 #include <elf.h>
     84 
     85 #include "debug.h"
     86 #include "rtld.h"
     87 
     88 void _rtld_bind_start(void);
     89 void _rtld_relocate_nonplt_self(Elf_Dyn *, Elf_Addr);
     90 caddr_t _rtld_bind(const Obj_Entry *, Elf_Word);
     91 static inline int _rtld_relocate_plt_object(const Obj_Entry *,
     92     const Elf_Rela *, Elf_Addr *);
     93 
     94 #define rdbg_symname(obj, rela) \
     95     ((obj)->strtab + (obj)->symtab[ELF_R_SYM((rela)->r_info)].st_name)
     96 
     97 void
     98 _rtld_setup_pltgot(const Obj_Entry *obj)
     99 {
    100 	obj->pltgot[1] = (Elf_Addr) obj;
    101 	obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start;
    102 }
    103 
    104 void
    105 _rtld_relocate_nonplt_self(Elf_Dyn *dynp, Elf_Addr relocbase)
    106 {
    107 	const Elf_Rela *rela = 0, *relalim;
    108 	Elf_Addr relasz = 0;
    109 	Elf_Addr *where;
    110 
    111 	for (; dynp->d_tag != DT_NULL; dynp++) {
    112 		switch (dynp->d_tag) {
    113 		case DT_RELA:
    114 			rela = (const Elf_Rela *)(relocbase + dynp->d_un.d_ptr);
    115 			break;
    116 		case DT_RELASZ:
    117 			relasz = dynp->d_un.d_val;
    118 			break;
    119 		}
    120 	}
    121 	/*
    122 	 * Assume only 64-bit relocations here, which should always
    123 	 * be true for the dynamic linker.
    124 	 */
    125 	relalim = (const Elf_Rela *)((const uint8_t *)rela + relasz);
    126 	for (; rela < relalim; rela++) {
    127 		where = (Elf_Addr *)(relocbase + rela->r_offset);
    128 		*where = (Elf_Addr)(relocbase + rela->r_addend);
    129 	}
    130 }
    131 
    132 int
    133 _rtld_relocate_nonplt_objects(Obj_Entry *obj)
    134 {
    135 	const Elf_Rela *rela;
    136 	const Elf_Sym *def = NULL;
    137 	const Obj_Entry *defobj = NULL;
    138 	unsigned long last_symnum = ULONG_MAX;
    139 
    140 	for (rela = obj->rela; rela < obj->relalim; rela++) {
    141 		Elf64_Addr *where64;
    142 		Elf32_Addr *where32;
    143 		Elf64_Addr tmp64;
    144 		Elf32_Addr tmp32;
    145 		unsigned long symnum;
    146 
    147 		where64 = (Elf64_Addr *)(obj->relocbase + rela->r_offset);
    148 		where32 = (Elf32_Addr *)where64;
    149 
    150 		switch (ELF_R_TYPE(rela->r_info)) {
    151 		case R_TYPE(32):	/* word32 S + A, truncate */
    152 		case R_TYPE(32S):	/* word32 S + A, signed truncate */
    153 		case R_TYPE(GOT32):	/* word32 G + A (XXX can we see these?) */
    154 		case R_TYPE(64):	/* word64 S + A */
    155 		case R_TYPE(PC32):	/* word32 S + A - P */
    156 		case R_TYPE(GLOB_DAT):	/* word64 S */
    157 		case R_TYPE(TPOFF64):
    158 		case R_TYPE(DTPMOD64):
    159 		case R_TYPE(DTPOFF64):
    160 			symnum = ELF_R_SYM(rela->r_info);
    161 			if (last_symnum != symnum) {
    162 				last_symnum = symnum;
    163 				def = _rtld_find_symdef(symnum, obj, &defobj,
    164 				    false);
    165 				if (def == NULL)
    166 					return -1;
    167 			}
    168 			break;
    169 		default:
    170 			break;
    171 		}
    172 
    173 		switch (ELF_R_TYPE(rela->r_info)) {
    174 		case R_TYPE(NONE):
    175 			break;
    176 
    177 		case R_TYPE(32):	/* word32 S + A, truncate */
    178 		case R_TYPE(32S):	/* word32 S + A, signed truncate */
    179 		case R_TYPE(GOT32):	/* word32 G + A (XXX can we see these?) */
    180 			tmp32 = (Elf32_Addr)(u_long)(defobj->relocbase +
    181 			    def->st_value + rela->r_addend);
    182 
    183 			if (*where32 != tmp32)
    184 				*where32 = tmp32;
    185 			rdbg(("32/32S %s in %s --> %p in %s",
    186 			    rdbg_symname(obj, rela),
    187 			    obj->path, (void *)(uintptr_t)*where32,
    188 			    defobj->path));
    189 			break;
    190 		case R_TYPE(64):	/* word64 S + A */
    191 			tmp64 = (Elf64_Addr)(defobj->relocbase + def->st_value +
    192 			    rela->r_addend);
    193 
    194 			if (*where64 != tmp64)
    195 				*where64 = tmp64;
    196 			rdbg(("64 %s in %s --> %p in %s",
    197 			    rdbg_symname(obj, rela),
    198 			    obj->path, (void *)*where64, defobj->path));
    199 			break;
    200 		case R_TYPE(PC32):	/* word32 S + A - P */
    201 			tmp32 = (Elf32_Addr)(u_long)(defobj->relocbase +
    202 			    def->st_value + rela->r_addend -
    203 			    (Elf64_Addr)where64);
    204 			if (*where32 != tmp32)
    205 				*where32 = tmp32;
    206 			rdbg(("PC32 %s in %s --> %p in %s",
    207 			    rdbg_symname(obj, rela),
    208 			    obj->path, (void *)(unsigned long)*where32,
    209 			    defobj->path));
    210 			break;
    211 		case R_TYPE(GLOB_DAT):	/* word64 S */
    212 			tmp64 = (Elf64_Addr)(defobj->relocbase + def->st_value);
    213 
    214 			if (*where64 != tmp64)
    215 				*where64 = tmp64;
    216 			rdbg(("64 %s in %s --> %p in %s",
    217 			    rdbg_symname(obj, rela),
    218 			    obj->path, (void *)*where64, defobj->path));
    219 			break;
    220 		case R_TYPE(RELATIVE):  /* word64 B + A */
    221 			tmp64 = (Elf64_Addr)(obj->relocbase + rela->r_addend);
    222 			if (*where64 != tmp64)
    223 				*where64 = tmp64;
    224 			rdbg(("RELATIVE in %s --> %p", obj->path,
    225 			    (void *)*where64));
    226        			break;
    227 
    228 		case R_TYPE(TPOFF64):
    229 			if (!defobj->tls_static &&
    230 			    _rtld_tls_offset_allocate(__UNCONST(defobj)))
    231 				return -1;
    232 
    233 			*where64 = (Elf64_Addr)(def->st_value -
    234 			    defobj->tlsoffset + rela->r_addend);
    235 
    236 			rdbg(("TPOFF64 %s in %s --> %p",
    237 			    rdbg_symname(obj, rela),
    238 			    obj->path, (void *)*where64));
    239 
    240 			break;
    241 
    242 		case R_TYPE(DTPMOD64):
    243 			*where64 = (Elf64_Addr)defobj->tlsindex;
    244 
    245 			rdbg(("DTPMOD64 %s in %s --> %p",
    246 			    rdbg_symname(obj, rela),
    247 			    obj->path, (void *)*where64));
    248 
    249 			break;
    250 
    251 		case R_TYPE(DTPOFF64):
    252 			*where64 = (Elf64_Addr)(def->st_value + rela->r_addend);
    253 
    254 			rdbg(("DTPOFF64 %s in %s --> %p",
    255 			    rdbg_symname(obj, rela),
    256 			    obj->path, (void *)*where64));
    257 
    258 			break;
    259 
    260 		case R_TYPE(COPY):
    261 			rdbg(("COPY"));
    262 			break;
    263 
    264 		case R_TYPE(IRELATIVE):
    265 			/* IFUNC relocations are handled in _rtld_call_ifunc */
    266 			if (obj->ifunc_remaining_nonplt == 0) {
    267 				obj->ifunc_remaining_nonplt =
    268 				    obj->relalim - rela;
    269 			}
    270 			break;
    271 
    272 		default:
    273 			rdbg(("sym = %lu, type = %lu, offset = %p, "
    274 			    "addend = %p, contents = %p, symbol = %s",
    275 			    (u_long)ELF_R_SYM(rela->r_info),
    276 			    (u_long)ELF_R_TYPE(rela->r_info),
    277 			    (void *)rela->r_offset, (void *)rela->r_addend,
    278 			    (void *)*where64, rdbg_symname(obj, rela)));
    279 			_rtld_error("%s: Unsupported relocation type %ld "
    280 			    "in non-PLT relocations",
    281 			    obj->path, (u_long) ELF_R_TYPE(rela->r_info));
    282 			return -1;
    283 		}
    284 	}
    285 	return 0;
    286 }
    287 
    288 int
    289 _rtld_relocate_plt_lazy(Obj_Entry *obj)
    290 {
    291 	const Elf_Rela *rela;
    292 
    293 	for (rela = obj->pltrelalim; rela-- > obj->pltrela; ) {
    294 		Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
    295 
    296 		assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JUMP_SLOT) ||
    297 		       ELF_R_TYPE(rela->r_info) == R_TYPE(IRELATIVE));
    298 
    299 		if (ELF_R_TYPE(rela->r_info) == R_TYPE(IRELATIVE))
    300 			obj->ifunc_remaining = obj->pltrelalim - rela;
    301 
    302 		/* Just relocate the GOT slots pointing into the PLT */
    303 		*where += (Elf_Addr)obj->relocbase;
    304 		rdbg(("fixup !main in %s --> %p", obj->path, (void *)*where));
    305 	}
    306 
    307 	return 0;
    308 }
    309 
    310 static inline int
    311 _rtld_relocate_plt_object(const Obj_Entry *obj, const Elf_Rela *rela, Elf_Addr *tp)
    312 {
    313 	Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
    314 	Elf_Addr new_value;
    315 	const Elf_Sym  *def;
    316 	const Obj_Entry *defobj;
    317 	unsigned long info = rela->r_info;
    318 
    319 	if (ELF_R_TYPE(info) == R_TYPE(IRELATIVE))
    320 		return 0;
    321 
    322 	assert(ELF_R_TYPE(info) == R_TYPE(JUMP_SLOT));
    323 
    324 	def = _rtld_find_plt_symdef(ELF_R_SYM(info), obj, &defobj, tp != NULL);
    325 	if (__predict_false(def == NULL))
    326 		return -1;
    327 	if (__predict_false(def == &_rtld_sym_zero))
    328 		return 0;
    329 
    330 	if (ELF_ST_TYPE(def->st_info) == STT_GNU_IFUNC) {
    331 		if (tp == NULL)
    332 			return 0;
    333 		new_value = _rtld_resolve_ifunc(defobj, def);
    334 	} else {
    335 		new_value = (Elf_Addr)(defobj->relocbase + def->st_value +
    336 		    rela->r_addend);
    337 	}
    338 
    339 	rdbg(("bind now/fixup in %s --> old=%p new=%p",
    340 	    defobj->strtab + def->st_name, (void *)*where, (void *)new_value));
    341 	if (*where != new_value)
    342 		*where = new_value;
    343 
    344 	if (tp)
    345 		*tp = new_value - rela->r_addend;
    346 
    347 	return 0;
    348 }
    349 
    350 caddr_t
    351 _rtld_bind(const Obj_Entry *obj, Elf_Word reloff)
    352 {
    353 	const Elf_Rela *rela = obj->pltrela + reloff;
    354 	Elf_Addr new_value;
    355 	int error;
    356 
    357 	new_value = 0; /* XXX GCC4 */
    358 
    359 	_rtld_shared_enter();
    360 	error = _rtld_relocate_plt_object(obj, rela, &new_value);
    361 	if (error)
    362 		_rtld_die();
    363 	_rtld_shared_exit();
    364 
    365 	return (caddr_t)new_value;
    366 }
    367 
    368 int
    369 _rtld_relocate_plt_objects(const Obj_Entry *obj)
    370 {
    371 	const Elf_Rela *rela;
    372 
    373 	for (rela = obj->pltrela; rela < obj->pltrelalim; rela++)
    374 		if (_rtld_relocate_plt_object(obj, rela, NULL) < 0)
    375 			return -1;
    376 
    377 	return 0;
    378 }
    379