Home | History | Annotate | Line # | Download | only in riscv
mdreloc.c revision 1.4.4.1
      1  1.4.4.1  christos /*	$NetBSD: mdreloc.c,v 1.4.4.1 2019/06/10 22:05:30 christos Exp $	*/
      2      1.1      matt 
      3      1.1      matt /*-
      4      1.1      matt  * Copyright (c) 2014 The NetBSD Foundation, Inc.
      5      1.1      matt  * All rights reserved.
      6      1.1      matt  *
      7      1.1      matt  * This code is derived from software contributed to The NetBSD Foundation
      8      1.1      matt  * by Matt Thomas of 3am Software Foundry.
      9      1.1      matt  *
     10      1.1      matt  * Redistribution and use in source and binary forms, with or without
     11      1.1      matt  * modification, are permitted provided that the following conditions
     12      1.1      matt  * are met:
     13      1.1      matt  * 1. Redistributions of source code must retain the above copyright
     14      1.1      matt  *    notice, this list of conditions and the following disclaimer.
     15      1.1      matt  * 2. Redistributions in binary form must reproduce the above copyright
     16      1.1      matt  *    notice, this list of conditions and the following disclaimer in the
     17      1.1      matt  *    documentation and/or other materials provided with the distribution.
     18      1.1      matt  *
     19      1.1      matt  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20      1.1      matt  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21      1.1      matt  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22      1.1      matt  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23      1.1      matt  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24      1.1      matt  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25      1.1      matt  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26      1.1      matt  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27      1.1      matt  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28      1.1      matt  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29      1.1      matt  * POSSIBILITY OF SUCH DAMAGE.
     30      1.1      matt  */
     31      1.1      matt 
     32      1.1      matt #include <sys/cdefs.h>
     33      1.1      matt #ifndef lint
     34  1.4.4.1  christos __RCSID("$NetBSD: mdreloc.c,v 1.4.4.1 2019/06/10 22:05:30 christos Exp $");
     35      1.1      matt #endif /* not lint */
     36      1.1      matt 
     37      1.1      matt #include <sys/types.h>
     38      1.1      matt #include <sys/endian.h>
     39      1.1      matt #include <sys/tls.h>
     40      1.1      matt 
     41      1.1      matt #include <stdlib.h>
     42      1.1      matt #include <string.h>
     43      1.1      matt 
     44      1.1      matt #include "debug.h"
     45      1.1      matt #include "rtld.h"
     46      1.1      matt 
     47      1.1      matt void _rtld_bind_start(void);
     48      1.1      matt void _rtld_relocate_nonplt_self(Elf_Dyn *, Elf_Addr);
     49      1.1      matt void *_rtld_bind(const Obj_Entry *, Elf_Word);
     50      1.1      matt 
     51      1.1      matt void
     52      1.1      matt _rtld_setup_pltgot(const Obj_Entry *obj)
     53      1.1      matt {
     54      1.1      matt 	obj->pltgot[0] = (Elf_Addr) &_rtld_bind_start;
     55      1.1      matt 	obj->pltgot[1] = (Elf_Addr) obj;
     56      1.1      matt }
     57      1.1      matt 
     58      1.1      matt void
     59      1.1      matt _rtld_relocate_nonplt_self(Elf_Dyn *dynp, Elf_Addr relocbase)
     60      1.1      matt {
     61      1.2      matt 	const Elf_Rela *rela = NULL, *relalim;
     62      1.2      matt 	Elf_Addr relasz = 0;
     63      1.1      matt 
     64      1.1      matt 	for (; dynp->d_tag != DT_NULL; dynp++) {
     65      1.1      matt 		switch (dynp->d_tag) {
     66      1.2      matt 		case DT_RELA:
     67      1.2      matt 			rela = (const Elf_Rela *)(relocbase + dynp->d_un.d_ptr);
     68      1.1      matt 			break;
     69      1.2      matt 		case DT_RELASZ:
     70      1.2      matt 			relasz = dynp->d_un.d_val;
     71      1.1      matt 			break;
     72      1.1      matt 		}
     73      1.1      matt 	}
     74      1.1      matt 
     75      1.2      matt 	relalim = (const Elf_Rela *)((uintptr_t)rela + relasz);
     76      1.2      matt 	for (; rela < relalim; rela++) {
     77      1.2      matt 		Elf_Word r_type = ELF_R_TYPE(rela->r_info);
     78      1.2      matt 		Elf_Addr *where = (Elf_Addr *)(relocbase + rela->r_offset);
     79      1.1      matt 
     80      1.2      matt 		switch (r_type) {
     81      1.2      matt 		case R_TYPE(RELATIVE): {
     82      1.2      matt 			Elf_Addr val = relocbase + rela->r_addend;
     83      1.2      matt 			*where = val;
     84      1.2      matt 			rdbg(("RELATIVE/L(%p) -> %p in <self>",
     85      1.2      matt 			    where, (void *)val));
     86      1.1      matt 			break;
     87      1.1      matt 		}
     88      1.1      matt 
     89      1.1      matt 		case R_TYPE(NONE):
     90      1.1      matt 			break;
     91      1.1      matt 
     92      1.1      matt 		default:
     93      1.1      matt 			abort();
     94      1.1      matt 		}
     95      1.1      matt 	}
     96      1.1      matt }
     97      1.1      matt 
     98      1.1      matt int
     99      1.1      matt _rtld_relocate_nonplt_objects(Obj_Entry *obj)
    100      1.1      matt {
    101      1.2      matt 	const Elf_Rela *rela;
    102      1.3     joerg 	const Elf_Sym *def = NULL;
    103      1.3     joerg 	const Obj_Entry *defobj = NULL;
    104      1.3     joerg 	unsigned long last_symnum = ULONG_MAX;
    105      1.1      matt 
    106      1.2      matt 	for (rela = obj->rela; rela < obj->relalim; rela++) {
    107      1.1      matt 		Elf_Addr * const where =
    108      1.2      matt 		    (Elf_Addr *)(obj->relocbase + rela->r_offset);
    109      1.2      matt 		const Elf_Word r_type = ELF_R_TYPE(rela->r_info);
    110      1.3     joerg 		unsigned long symnum;
    111      1.3     joerg 
    112      1.3     joerg 		switch (r_type) {
    113      1.3     joerg 		case R_TYPESZ(ADDR):
    114      1.3     joerg 		case R_TYPESZ(TLS_DTPMOD):
    115      1.3     joerg 		case R_TYPESZ(TLS_DTPREL):
    116      1.3     joerg 			symnum = ELF_R_SYM(rela->r_info);
    117      1.3     joerg 			if (last_symnum != symnum) {
    118      1.3     joerg 				last_symnum = symnum;
    119      1.3     joerg 				def = _rtld_find_symdef(symnum, obj, &defobj,
    120      1.3     joerg 				    false);
    121      1.3     joerg 				if (def == NULL)
    122      1.3     joerg 					return -1;
    123      1.3     joerg 			}
    124      1.3     joerg 			break;
    125      1.3     joerg 		default:
    126      1.3     joerg 			break;
    127      1.3     joerg 		}
    128      1.1      matt 
    129      1.1      matt 		switch (r_type) {
    130      1.1      matt 		case R_TYPE(NONE):
    131      1.1      matt 			break;
    132      1.1      matt 
    133      1.2      matt 		case R_TYPE(RELATIVE): {
    134  1.4.4.1  christos 			symnum = ELF_R_SYM(rela->r_info);
    135  1.4.4.1  christos 			def = obj->symtab + symnum;
    136      1.1      matt 
    137      1.2      matt 			Elf_Addr val = (Elf_Addr)obj->relocbase + rela->r_addend;
    138      1.1      matt 
    139      1.2      matt 			rdbg(("RELATIVE(%p) -> %p (%s) in %s",
    140      1.2      matt 			    where, (void *)val,
    141      1.2      matt 			    obj->strtab + def->st_name, obj->path));
    142      1.1      matt 
    143      1.1      matt 			*where = val;
    144      1.1      matt 			break;
    145      1.1      matt 		}
    146      1.1      matt 
    147      1.2      matt 		case R_TYPESZ(ADDR): {
    148      1.2      matt 			Elf_Addr val = (Elf_Addr)defobj->relocbase + rela->r_addend;
    149      1.2      matt 
    150      1.2      matt 			*where = val;
    151      1.2      matt 			rdbg(("ADDR %s in %s --> %p in %s",
    152      1.2      matt 			    obj->strtab + obj->symtab[r_symndx].st_name,
    153      1.2      matt 			    obj->path, (void *)val, defobj->path));
    154      1.2      matt 			break;
    155      1.2      matt 		}
    156      1.1      matt 
    157      1.2      matt 		case R_TYPESZ(TLS_DTPMOD): {
    158      1.2      matt 			Elf_Addr val = (Elf_Addr)defobj->tlsindex + rela->r_addend;
    159      1.1      matt 
    160      1.1      matt 			*where = val;
    161      1.1      matt 			rdbg(("DTPMOD %s in %s --> %p in %s",
    162      1.1      matt 			    obj->strtab + obj->symtab[r_symndx].st_name,
    163      1.2      matt 			    obj->path, (void *)val, defobj->path));
    164      1.1      matt 			break;
    165      1.1      matt 		}
    166      1.1      matt 
    167      1.2      matt 		case R_TYPESZ(TLS_DTPREL): {
    168      1.1      matt 			Elf_Addr old = *where;
    169      1.1      matt 			Elf_Addr val = old;
    170      1.1      matt 
    171      1.1      matt 			if (!defobj->tls_done && _rtld_tls_offset_allocate(obj))
    172      1.1      matt 				return -1;
    173      1.1      matt 
    174      1.2      matt 			val = (Elf_Addr)def->st_value - TLS_DTV_OFFSET;
    175      1.2      matt 			*where = val;
    176      1.1      matt 
    177      1.1      matt 			rdbg(("DTPREL %s in %s --> %p in %s",
    178      1.1      matt 			    obj->strtab + obj->symtab[r_symndx].st_name,
    179      1.2      matt 			    obj->path, (void *)val, defobj->path));
    180      1.1      matt 			break;
    181      1.1      matt 		}
    182      1.1      matt 
    183      1.1      matt 		default:
    184      1.1      matt 			rdbg(("sym = %lu, type = %lu, offset = %p, "
    185      1.2      matt 			    "addend = %p, contents = %p, symbol = %s",
    186      1.3     joerg 			    (u_long)ELF_R_SYM(rela->r_info),
    187      1.3     joerg 			    (u_long)ELF_R_TYPE(rela->r_info),
    188      1.2      matt 			    (void *)rela->r_offset, (void *)rela->r_addend,
    189      1.2      matt 			    (void *)load_ptr(where, sizeof(Elf_Addr)),
    190      1.1      matt 			    obj->strtab + obj->symtab[r_symndx].st_name));
    191      1.1      matt 			_rtld_error("%s: Unsupported relocation type %ld "
    192      1.1      matt 			    "in non-PLT relocations",
    193      1.2      matt 			    obj->path, (u_long)r_type);
    194      1.1      matt 			return -1;
    195      1.1      matt 		}
    196      1.1      matt 	}
    197      1.1      matt 
    198      1.1      matt 	return 0;
    199      1.1      matt }
    200      1.1      matt 
    201      1.1      matt int
    202      1.4     joerg _rtld_relocate_plt_lazy(Obj_Entry *obj)
    203      1.1      matt {
    204      1.1      matt 	/* PLT fixups were done above in the GOT relocation. */
    205      1.1      matt 	return 0;
    206      1.1      matt }
    207      1.1      matt 
    208      1.1      matt static int
    209      1.1      matt _rtld_relocate_plt_object(const Obj_Entry *obj, const Elf_Rel *rel,
    210      1.1      matt     Elf_Addr *tp)
    211      1.1      matt {
    212      1.1      matt 	const Obj_Entry *defobj;
    213      1.1      matt 	Elf_Addr new_value;
    214      1.1      matt 
    215      1.1      matt         assert(ELF_R_TYPE(rel->r_info) == R_TYPE(JMP_SLOT));
    216      1.1      matt 
    217      1.1      matt 	const Elf_Sym *def = _rtld_find_plt_symdef(ELF_R_SYM(rel->r_info),
    218      1.1      matt 	    obj, &defobj, tp != NULL);
    219      1.1      matt 	if (__predict_false(def == NULL))
    220      1.1      matt 		return -1;
    221      1.1      matt 	if (__predict_false(def == &_rtld_sym_zero))
    222      1.1      matt 		return -1;
    223      1.1      matt 
    224      1.1      matt 	if (ELF_ST_TYPE(def->st_info) == STT_GNU_IFUNC) {
    225      1.1      matt 		if (tp == NULL)
    226      1.1      matt 			return 0;
    227      1.1      matt 		new_value = _rtld_resolve_ifunc(defobj, def);
    228      1.1      matt 	} else {
    229      1.1      matt 		new_value = (Elf_Addr)(defobj->relocbase + def->st_value);
    230      1.1      matt 	}
    231      1.1      matt 	rdbg(("bind now/fixup in %s --> new=%p",
    232      1.1      matt 	    defobj->strtab + def->st_name, (void *)new_value));
    233      1.1      matt 	*(Elf_Addr *)(obj->relocbase + rel->r_offset) = new_value;
    234      1.1      matt 
    235      1.1      matt 	if (tp)
    236      1.1      matt 		*tp = new_value;
    237      1.1      matt 	return 0;
    238      1.1      matt }
    239      1.1      matt 
    240      1.1      matt void *
    241      1.1      matt _rtld_bind(const Obj_Entry *obj, Elf_Word reloff)
    242      1.1      matt {
    243      1.1      matt 	const Elf_Rel *pltrel = (const Elf_Rel *)(obj->pltrel + reloff);
    244      1.1      matt 	Elf_Addr new_value;
    245      1.1      matt 	int err;
    246      1.1      matt 
    247      1.1      matt 	_rtld_shared_enter();
    248      1.1      matt 	err = _rtld_relocate_plt_object(obj, pltrel, &new_value);
    249      1.1      matt 	if (err)
    250      1.1      matt 		_rtld_die();
    251      1.1      matt 	_rtld_shared_exit();
    252      1.1      matt 
    253      1.1      matt 	return (caddr_t)new_value;
    254      1.1      matt }
    255      1.1      matt 
    256      1.1      matt int
    257      1.1      matt _rtld_relocate_plt_objects(const Obj_Entry *obj)
    258      1.1      matt {
    259      1.1      matt 
    260      1.1      matt 	for (const Elf_Rel *rel = obj->pltrel; rel < obj->pltrellim; rel++) {
    261      1.1      matt 		if (_rtld_relocate_plt_object(obj, rel, NULL) < 0)
    262      1.1      matt 			return -1;
    263      1.1      matt 	}
    264      1.1      matt 
    265      1.1      matt 	return 0;
    266      1.1      matt }
    267