Home | History | Annotate | Line # | Download | only in or1k
mdreloc.c revision 1.5
      1 /*	$NetBSD: mdreloc.c,v 1.5 2024/08/03 21:59:58 riastradh Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2014 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Matt Thomas of 3am Software Foundry.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 #ifndef lint
     34 __RCSID("$NetBSD: mdreloc.c,v 1.5 2024/08/03 21:59:58 riastradh Exp $");
     35 #endif /* not lint */
     36 
     37 #include <stdarg.h>
     38 #include <stdio.h>
     39 #include <stdlib.h>
     40 #include <string.h>
     41 #include <sys/types.h>
     42 #include <machine/cpu.h>
     43 
     44 #include "debug.h"
     45 #include "rtld.h"
     46 
     47 void _rtld_bind_start(void);
     48 Elf_Addr _rtld_bind(const Obj_Entry *, Elf_Word);
     49 void _rtld_relocate_nonplt_self(Elf_Dyn *, Elf_Addr);
     50 static int _rtld_relocate_plt_object(const Obj_Entry *,
     51     const Elf_Rela *, int, Elf_Addr *);
     52 
     53 /*
     54  * The OR1k PLT format is simple.
     55  */
     56 
     57 void
     58 _rtld_setup_pltgot(const Obj_Entry *obj)
     59 {
     60 	obj->pltgot[1] = (Elf_Addr) obj;
     61 	obj->pltgot[2] = (Elf_Addr) _rtld_bind_start;
     62 
     63 	dbg(("obj %s plt pltgot=%p start=%p obj=%p",
     64 	    obj->path, obj->pltgot,
     65 	    (void *) obj->pltgot[1], (void *) obj->pltgot[2]));
     66 }
     67 
     68 void
     69 _rtld_relocate_nonplt_self(Elf_Dyn *dynp, Elf_Addr relocbase)
     70 {
     71 	const Elf_Rela *rela = 0, *relalim;
     72 	Elf_Addr relasz = 0;
     73 	Elf_Addr *where;
     74 
     75 	for (; dynp->d_tag != DT_NULL; dynp++) {
     76 		switch (dynp->d_tag) {
     77 		case DT_RELA:
     78 			rela = (const Elf_Rela *)(relocbase + dynp->d_un.d_ptr);
     79 			break;
     80 		case DT_RELASZ:
     81 			relasz = dynp->d_un.d_val;
     82 			break;
     83 		}
     84 	}
     85 	relalim = (const Elf_Rela *)((const uint8_t *)rela + relasz);
     86 	for (; rela < relalim; rela++) {
     87 		where = (Elf_Addr *)(relocbase + rela->r_offset);
     88 		*where = (Elf_Addr)(relocbase + rela->r_addend);
     89 	}
     90 }
     91 
     92 int
     93 _rtld_relocate_nonplt_objects(Obj_Entry *obj)
     94 {
     95 	const Elf_Rela *rela;
     96 	const Elf_Sym *def = NULL;
     97 	const Obj_Entry *defobj = NULL;
     98 	unsigned long last_symnum = ULONG_MAX;
     99 
    100 	for (rela = obj->rela; rela < obj->relalim; rela++) {
    101 		Elf_Addr        *where;
    102 		Elf_Addr         tmp;
    103 		unsigned long	 symnum;
    104 
    105 		where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
    106 
    107 		switch (ELF_R_TYPE(rela->r_info)) {
    108 		case R_TYPE(32):	/* <address> S + A */
    109 		case R_TYPE(GLOB_DAT):	/* <address> S + A */
    110 		case R_TYPE(TLS_DTPMOD):
    111 		case R_TYPE(TLS_DTPOFF):
    112 		case R_TYPE(TLS_TPOFF):
    113 			symnum = ELF_R_SYM(rela->r_info);
    114 			if (last_symnum != symnum) {
    115 				last_symnum = symnum;
    116 				def = _rtld_find_symdef(symnum, obj, &defobj,
    117 				    false);
    118 				if (def == NULL)
    119 					return -1;
    120 			}
    121 			break;
    122 		default:
    123 			break;
    124 		}
    125 
    126 		switch (ELF_R_TYPE(rela->r_info)) {
    127 #if 1 /* XXX Should not be necessary. */
    128 		case R_TYPE(JMP_SLOT):
    129 #endif
    130 		case R_TYPE(NONE):
    131 			break;
    132 
    133 		case R_TYPE(32):	/* <address> S + A */
    134 		case R_TYPE(GLOB_DAT):	/* <address> S + A */
    135 			tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
    136 			    rela->r_addend);
    137 			if (*where != tmp)
    138 				*where = tmp;
    139 			rdbg(("32/GLOB_DAT %s in %s --> %p in %s",
    140 			    obj->strtab + obj->symtab[symnum].st_name,
    141 			    obj->path, (void *)*where, defobj->path));
    142 			break;
    143 
    144 		case R_TYPE(RELATIVE):	/* <address> B + A */
    145 			*where = (Elf_Addr)(obj->relocbase + rela->r_addend);
    146 			rdbg(("RELATIVE in %s --> %p", obj->path,
    147 			    (void *)*where));
    148 			break;
    149 
    150 		case R_TYPE(COPY):
    151 			/*
    152 			 * These are deferred until all other relocations have
    153 			 * been done.  All we do here is make sure that the
    154 			 * COPY relocation is not in a shared library.  They
    155 			 * are allowed only in executable files.
    156 			 */
    157 			if (obj->isdynamic) {
    158 				_rtld_error(
    159 			"%s: Unexpected R_COPY relocation in shared library",
    160 				    obj->path);
    161 				return -1;
    162 			}
    163 			rdbg(("COPY (avoid in main)"));
    164 			break;
    165 
    166 		case R_TYPE(TLS_DTPMOD):
    167 			*where = (Elf_Addr)defobj->tlsindex;
    168 			rdbg(("DTPMOD32 %s in %s --> %p in %s",
    169 			    obj->strtab + obj->symtab[symnum].st_name,
    170 			    obj->path, (void *)*where, defobj->path));
    171 			break;
    172 
    173 		case R_TYPE(TLS_DTPOFF):
    174 			*where = (Elf_Addr)(def->st_value + rela->r_addend
    175 			    - TLS_DTV_OFFSET);
    176 			rdbg(("DTPOFF %s in %s --> %p in %s",
    177 			    obj->strtab + obj->symtab[symnum].st_name,
    178 			    obj->path, (void *)*where, defobj->path));
    179 			break;
    180 
    181 		case R_TYPE(TLS_TPOFF):
    182 			if (!defobj->tls_static &&
    183 			    _rtld_tls_offset_allocate(__UNCONST(defobj)))
    184 				return -1;
    185 
    186 			*where = (Elf_Addr)(def->st_value + rela->r_addend
    187 			    + defobj->tlsoffset - TLS_TP_OFFSET);
    188 			rdbg(("TPREL %s in %s --> %p in %s",
    189 			    obj->strtab + obj->symtab[symnum].st_name,
    190 			    obj->path, (void *)*where, defobj->path));
    191 			break;
    192 
    193 		default:
    194 			rdbg(("sym = %lu, type = %lu, offset = %p, "
    195 			    "addend = %p, contents = %p, symbol = %s",
    196 			    (u_long)ELF_R_SYM(rela->r_info),
    197 			    (u_long)ELF_R_TYPE(rela->r_info),
    198 			    (void *)rela->r_offset, (void *)rela->r_addend,
    199 			    (void *)*where,
    200 			    obj->strtab + obj->symtab[symnum].st_name));
    201 			_rtld_error("%s: Unsupported relocation type %ld "
    202 			    "in non-PLT relocations",
    203 			    obj->path, (u_long) ELF_R_TYPE(rela->r_info));
    204 			return -1;
    205 		}
    206 	}
    207 	return 0;
    208 }
    209 
    210 int
    211 _rtld_relocate_plt_lazy(Obj_Entry *obj)
    212 {
    213 	const Elf_Rela *rela;
    214 	int reloff;
    215 
    216 	for (rela = obj->pltrela, reloff = 0;
    217 	     rela < obj->pltrelalim;
    218 	     rela++, reloff++) {
    219 		Elf_Word *where = (Elf_Word *)(obj->relocbase + rela->r_offset);
    220 
    221 		assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT));
    222 
    223 		/*
    224 		 * For now, simply treat then as relative.
    225 		 */
    226 		*where += (Elf_Addr)obj->relocbase;
    227 	}
    228 
    229 	return 0;
    230 }
    231 
    232 static int
    233 _rtld_relocate_plt_object(const Obj_Entry *obj, const Elf_Rela *rela, int reloff, Elf_Addr *tp)
    234 {
    235 	Elf_Word *where = (Elf_Word *)(obj->relocbase + rela->r_offset);
    236 	Elf_Addr value;
    237 	const Elf_Sym *def;
    238 	const Obj_Entry *defobj;
    239 	unsigned long info = rela->r_info;
    240 
    241 	assert(ELF_R_TYPE(info) == R_TYPE(JMP_SLOT));
    242 
    243 	def = _rtld_find_plt_symdef(ELF_R_SYM(info), obj, &defobj, tp != NULL);
    244 	if (__predict_false(def == NULL))
    245 		return -1;
    246 	if (__predict_false(def == &_rtld_sym_zero))
    247 		return 0;
    248 
    249 	value = (Elf_Addr)(defobj->relocbase + def->st_value);
    250 	rdbg(("bind now/fixup in %s --> new=%p",
    251 	    defobj->strtab + def->st_name, (void *)value));
    252 
    253 	/*
    254 	 * Simply replace the entry in GOT with the
    255 	 * address of the routine.
    256 	 */
    257 	assert(where >= (Elf_Word *)obj->pltgot);
    258 	assert(where < (Elf_Word *)obj->pltgot + (obj->pltrelalim - obj->pltrela));
    259 	*where = value;
    260 
    261 	if (tp)
    262 		*tp = value;
    263 	return 0;
    264 }
    265 
    266 Elf_Addr
    267 _rtld_bind(const Obj_Entry *obj, Elf_Word reloff)
    268 {
    269 	const Elf_Rela *rela = obj->pltrela + reloff;
    270 	Elf_Addr new_value;
    271 	int err;
    272 
    273 	new_value = 0;	/* XXX gcc */
    274 
    275 	_rtld_shared_enter();
    276 	err = _rtld_relocate_plt_object(obj, rela, reloff, &new_value);
    277 	if (err)
    278 		_rtld_die();
    279 	_rtld_shared_exit();
    280 
    281 	return new_value;
    282 }
    283 
    284 int
    285 _rtld_relocate_plt_objects(const Obj_Entry *obj)
    286 {
    287 	const Elf_Rela *rela;
    288 	int reloff;
    289 
    290 	for (rela = obj->pltrela, reloff = 0; rela < obj->pltrelalim; rela++, reloff++) {
    291 		if (_rtld_relocate_plt_object(obj, rela, reloff, NULL) < 0)
    292 			return -1;
    293 	}
    294 	return 0;
    295 }
    296