Home | History | Annotate | Line # | Download | only in or1k
mdreloc.c revision 1.2
      1 /*	$NetBSD: mdreloc.c,v 1.2 2017/06/19 11:57:02 joerg 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.2 2017/06/19 11:57:02 joerg 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 			if (!defobj->tls_done && _rtld_tls_offset_allocate(obj))
    175 				return -1;
    176 
    177 			*where = (Elf_Addr)(def->st_value + rela->r_addend
    178 			    - TLS_DTV_OFFSET);
    179 			rdbg(("DTPOFF %s in %s --> %p in %s",
    180 			    obj->strtab + obj->symtab[symnum].st_name,
    181 			    obj->path, (void *)*where, defobj->path));
    182 			break;
    183 
    184 		case R_TYPE(TLS_TPOFF):
    185 			if (!defobj->tls_done && _rtld_tls_offset_allocate(obj))
    186 				return -1;
    187 
    188 			*where = (Elf_Addr)(def->st_value + rela->r_addend
    189 			    + defobj->tlsoffset - TLS_TP_OFFSET);
    190 			rdbg(("TPREL %s in %s --> %p in %s",
    191 			    obj->strtab + obj->symtab[symnum].st_name,
    192 			    obj->path, (void *)*where, defobj->path));
    193 			break;
    194 
    195 		default:
    196 			rdbg(("sym = %lu, type = %lu, offset = %p, "
    197 			    "addend = %p, contents = %p, symbol = %s",
    198 			    (u_long)ELF_R_SYM(rela->r_info),
    199 			    (u_long)ELF_R_TYPE(rela->r_info),
    200 			    (void *)rela->r_offset, (void *)rela->r_addend,
    201 			    (void *)*where,
    202 			    obj->strtab + obj->symtab[symnum].st_name));
    203 			_rtld_error("%s: Unsupported relocation type %ld "
    204 			    "in non-PLT relocations",
    205 			    obj->path, (u_long) ELF_R_TYPE(rela->r_info));
    206 			return -1;
    207 		}
    208 	}
    209 	return 0;
    210 }
    211 
    212 int
    213 _rtld_relocate_plt_lazy(const Obj_Entry *obj)
    214 {
    215 	const Elf_Rela *rela;
    216 	int reloff;
    217 
    218 	for (rela = obj->pltrela, reloff = 0;
    219 	     rela < obj->pltrelalim;
    220 	     rela++, reloff++) {
    221 		Elf_Word *where = (Elf_Word *)(obj->relocbase + rela->r_offset);
    222 
    223 		assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT));
    224 
    225 		/*
    226 		 * For now, simply treat then as relative.
    227 		 */
    228 		*where += (Elf_Addr)obj->relocbase;
    229 	}
    230 
    231 	return 0;
    232 }
    233 
    234 static int
    235 _rtld_relocate_plt_object(const Obj_Entry *obj, const Elf_Rela *rela, int reloff, Elf_Addr *tp)
    236 {
    237 	Elf_Word *where = (Elf_Word *)(obj->relocbase + rela->r_offset);
    238 	Elf_Addr value;
    239 	const Elf_Sym *def;
    240 	const Obj_Entry *defobj;
    241 	unsigned long info = rela->r_info;
    242 
    243 	assert(ELF_R_TYPE(info) == R_TYPE(JMP_SLOT));
    244 
    245 	def = _rtld_find_plt_symdef(ELF_R_SYM(info), obj, &defobj, tp != NULL);
    246 	if (__predict_false(def == NULL))
    247 		return -1;
    248 	if (__predict_false(def == &_rtld_sym_zero))
    249 		return 0;
    250 
    251 	value = (Elf_Addr)(defobj->relocbase + def->st_value);
    252 	rdbg(("bind now/fixup in %s --> new=%p",
    253 	    defobj->strtab + def->st_name, (void *)value));
    254 
    255 	/*
    256 	 * Simply replace the entry in GOT with the
    257 	 * address of the routine.
    258 	 */
    259 	assert(where >= (Elf_Word *)obj->pltgot);
    260 	assert(where < (Elf_Word *)obj->pltgot + (obj->pltrelalim - obj->pltrela));
    261 	*where = value;
    262 
    263 	if (tp)
    264 		*tp = value;
    265 	return 0;
    266 }
    267 
    268 Elf_Addr
    269 _rtld_bind(const Obj_Entry *obj, Elf_Word reloff)
    270 {
    271 	const Elf_Rela *rela = obj->pltrela + reloff;
    272 	Elf_Addr new_value;
    273 	int err;
    274 
    275 	new_value = 0;	/* XXX gcc */
    276 
    277 	_rtld_shared_enter();
    278 	err = _rtld_relocate_plt_object(obj, rela, reloff, &new_value);
    279 	if (err)
    280 		_rtld_die();
    281 	_rtld_shared_exit();
    282 
    283 	return new_value;
    284 }
    285 
    286 int
    287 _rtld_relocate_plt_objects(const Obj_Entry *obj)
    288 {
    289 	const Elf_Rela *rela;
    290 	int reloff;
    291 
    292 	for (rela = obj->pltrela, reloff = 0; rela < obj->pltrelalim; rela++, reloff++) {
    293 		if (_rtld_relocate_plt_object(obj, rela, reloff, NULL) < 0)
    294 			return -1;
    295 	}
    296 	return 0;
    297 }
    298