Home | History | Annotate | Line # | Download | only in riscv
mdreloc.c revision 1.2
      1 /*	$NetBSD: mdreloc.c,v 1.2 2015/03/27 23:14:53 matt 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 2015/03/27 23:14:53 matt Exp $");
     35 #endif /* not lint */
     36 
     37 #include <sys/types.h>
     38 #include <sys/endian.h>
     39 #include <sys/tls.h>
     40 
     41 #include <stdlib.h>
     42 #include <string.h>
     43 
     44 #include "debug.h"
     45 #include "rtld.h"
     46 
     47 void _rtld_bind_start(void);
     48 void _rtld_relocate_nonplt_self(Elf_Dyn *, Elf_Addr);
     49 void *_rtld_bind(const Obj_Entry *, Elf_Word);
     50 
     51 void
     52 _rtld_setup_pltgot(const Obj_Entry *obj)
     53 {
     54 	obj->pltgot[0] = (Elf_Addr) &_rtld_bind_start;
     55 	obj->pltgot[1] = (Elf_Addr) obj;
     56 }
     57 
     58 void
     59 _rtld_relocate_nonplt_self(Elf_Dyn *dynp, Elf_Addr relocbase)
     60 {
     61 	const Elf_Rela *rela = NULL, *relalim;
     62 	Elf_Addr relasz = 0;
     63 
     64 	for (; dynp->d_tag != DT_NULL; dynp++) {
     65 		switch (dynp->d_tag) {
     66 		case DT_RELA:
     67 			rela = (const Elf_Rela *)(relocbase + dynp->d_un.d_ptr);
     68 			break;
     69 		case DT_RELASZ:
     70 			relasz = dynp->d_un.d_val;
     71 			break;
     72 		}
     73 	}
     74 
     75 	relalim = (const Elf_Rela *)((uintptr_t)rela + relasz);
     76 	for (; rela < relalim; rela++) {
     77 		Elf_Word r_type = ELF_R_TYPE(rela->r_info);
     78 		Elf_Addr *where = (Elf_Addr *)(relocbase + rela->r_offset);
     79 
     80 		switch (r_type) {
     81 		case R_TYPE(RELATIVE): {
     82 			Elf_Addr val = relocbase + rela->r_addend;
     83 			*where = val;
     84 			rdbg(("RELATIVE/L(%p) -> %p in <self>",
     85 			    where, (void *)val));
     86 			break;
     87 		}
     88 
     89 		case R_TYPE(NONE):
     90 			break;
     91 
     92 		default:
     93 			abort();
     94 		}
     95 	}
     96 }
     97 
     98 int
     99 _rtld_relocate_nonplt_objects(Obj_Entry *obj)
    100 {
    101 	const Elf_Rela *rela;
    102 	const Elf_Sym *def;
    103 	const Obj_Entry *defobj;
    104 
    105 	for (rela = obj->rela; rela < obj->relalim; rela++) {
    106 		Elf_Addr * const where =
    107 		    (Elf_Addr *)(obj->relocbase + rela->r_offset);
    108 		const Elf_Word r_symndx = ELF_R_SYM(rela->r_info);
    109 		const Elf_Word r_type = ELF_R_TYPE(rela->r_info);
    110 
    111 		switch (r_type) {
    112 		case R_TYPE(NONE):
    113 			break;
    114 
    115 		case R_TYPE(RELATIVE): {
    116 			def = obj->symtab + r_symndx;
    117 
    118 			Elf_Addr val = (Elf_Addr)obj->relocbase + rela->r_addend;
    119 
    120 			rdbg(("RELATIVE(%p) -> %p (%s) in %s",
    121 			    where, (void *)val,
    122 			    obj->strtab + def->st_name, obj->path));
    123 
    124 			*where = val;
    125 			break;
    126 		}
    127 
    128 		case R_TYPESZ(ADDR): {
    129 			def = _rtld_find_symdef(r_symndx, obj, &defobj, false);
    130 			if (def == NULL)
    131 				return -1;
    132 
    133 			Elf_Addr val = (Elf_Addr)defobj->relocbase + rela->r_addend;
    134 
    135 			*where = val;
    136 			rdbg(("ADDR %s in %s --> %p in %s",
    137 			    obj->strtab + obj->symtab[r_symndx].st_name,
    138 			    obj->path, (void *)val, defobj->path));
    139 			break;
    140 		}
    141 
    142 		case R_TYPESZ(TLS_DTPMOD): {
    143 			def = _rtld_find_symdef(r_symndx, obj, &defobj, false);
    144 			if (def == NULL)
    145 				return -1;
    146 
    147 			Elf_Addr val = (Elf_Addr)defobj->tlsindex + rela->r_addend;
    148 
    149 			*where = val;
    150 			rdbg(("DTPMOD %s in %s --> %p in %s",
    151 			    obj->strtab + obj->symtab[r_symndx].st_name,
    152 			    obj->path, (void *)val, defobj->path));
    153 			break;
    154 		}
    155 
    156 		case R_TYPESZ(TLS_DTPREL): {
    157 			Elf_Addr old = *where;
    158 			Elf_Addr val = old;
    159 
    160 			def = _rtld_find_symdef(r_symndx, obj, &defobj, false);
    161 			if (def == NULL)
    162 				return -1;
    163 
    164 			if (!defobj->tls_done && _rtld_tls_offset_allocate(obj))
    165 				return -1;
    166 
    167 			val = (Elf_Addr)def->st_value - TLS_DTV_OFFSET;
    168 			*where = val;
    169 
    170 			rdbg(("DTPREL %s in %s --> %p in %s",
    171 			    obj->strtab + obj->symtab[r_symndx].st_name,
    172 			    obj->path, (void *)val, defobj->path));
    173 			break;
    174 		}
    175 
    176 		default:
    177 			rdbg(("sym = %lu, type = %lu, offset = %p, "
    178 			    "addend = %p, contents = %p, symbol = %s",
    179 			    (u_long)r_symndx, (u_long)r_type,
    180 			    (void *)rela->r_offset, (void *)rela->r_addend,
    181 			    (void *)load_ptr(where, sizeof(Elf_Addr)),
    182 			    obj->strtab + obj->symtab[r_symndx].st_name));
    183 			_rtld_error("%s: Unsupported relocation type %ld "
    184 			    "in non-PLT relocations",
    185 			    obj->path, (u_long)r_type);
    186 			return -1;
    187 		}
    188 	}
    189 
    190 	return 0;
    191 }
    192 
    193 int
    194 _rtld_relocate_plt_lazy(const Obj_Entry *obj)
    195 {
    196 	/* PLT fixups were done above in the GOT relocation. */
    197 	return 0;
    198 }
    199 
    200 static int
    201 _rtld_relocate_plt_object(const Obj_Entry *obj, const Elf_Rel *rel,
    202     Elf_Addr *tp)
    203 {
    204 	const Obj_Entry *defobj;
    205 	Elf_Addr new_value;
    206 
    207         assert(ELF_R_TYPE(rel->r_info) == R_TYPE(JMP_SLOT));
    208 
    209 	const Elf_Sym *def = _rtld_find_plt_symdef(ELF_R_SYM(rel->r_info),
    210 	    obj, &defobj, tp != NULL);
    211 	if (__predict_false(def == NULL))
    212 		return -1;
    213 	if (__predict_false(def == &_rtld_sym_zero))
    214 		return -1;
    215 
    216 	if (ELF_ST_TYPE(def->st_info) == STT_GNU_IFUNC) {
    217 		if (tp == NULL)
    218 			return 0;
    219 		new_value = _rtld_resolve_ifunc(defobj, def);
    220 	} else {
    221 		new_value = (Elf_Addr)(defobj->relocbase + def->st_value);
    222 	}
    223 	rdbg(("bind now/fixup in %s --> new=%p",
    224 	    defobj->strtab + def->st_name, (void *)new_value));
    225 	*(Elf_Addr *)(obj->relocbase + rel->r_offset) = new_value;
    226 
    227 	if (tp)
    228 		*tp = new_value;
    229 	return 0;
    230 }
    231 
    232 void *
    233 _rtld_bind(const Obj_Entry *obj, Elf_Word reloff)
    234 {
    235 	const Elf_Rel *pltrel = (const Elf_Rel *)(obj->pltrel + reloff);
    236 	Elf_Addr new_value;
    237 	int err;
    238 
    239 	_rtld_shared_enter();
    240 	err = _rtld_relocate_plt_object(obj, pltrel, &new_value);
    241 	if (err)
    242 		_rtld_die();
    243 	_rtld_shared_exit();
    244 
    245 	return (caddr_t)new_value;
    246 }
    247 
    248 int
    249 _rtld_relocate_plt_objects(const Obj_Entry *obj)
    250 {
    251 
    252 	for (const Elf_Rel *rel = obj->pltrel; rel < obj->pltrellim; rel++) {
    253 		if (_rtld_relocate_plt_object(obj, rel, NULL) < 0)
    254 			return -1;
    255 	}
    256 
    257 	return 0;
    258 }
    259