Home | History | Annotate | Line # | Download | only in riscv
mdreloc.c revision 1.1
      1  1.1  matt /*	$NetBSD: mdreloc.c,v 1.1 2014/09/19 17:36:25 matt 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.1  matt __RCSID("$NetBSD: mdreloc.c,v 1.1 2014/09/19 17:36:25 matt 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 #if ELFSIZE == 64
     52  1.1  matt #define	Elf_Sxword			Elf64_Sxword
     53  1.1  matt #else
     54  1.1  matt #define	Elf_Sxword			Elf32_Sword
     55  1.1  matt #endif
     56  1.1  matt 
     57  1.1  matt void
     58  1.1  matt _rtld_setup_pltgot(const Obj_Entry *obj)
     59  1.1  matt {
     60  1.1  matt 	obj->pltgot[0] = (Elf_Addr) &_rtld_bind_start;
     61  1.1  matt 	obj->pltgot[1] = (Elf_Addr) obj;
     62  1.1  matt }
     63  1.1  matt 
     64  1.1  matt void
     65  1.1  matt _rtld_relocate_nonplt_self(Elf_Dyn *dynp, Elf_Addr relocbase)
     66  1.1  matt {
     67  1.1  matt 	const Elf_Rel *rel = 0, *rellim;
     68  1.1  matt 	Elf_Addr relsz = 0;
     69  1.1  matt 	Elf_Sxword *where;
     70  1.1  matt 	const Elf_Sym *symtab = NULL, *sym;
     71  1.1  matt 	Elf_Addr *got = NULL;
     72  1.1  matt 	Elf_Word local_gotno = 0, symtabno = 0, gotsym = 0;
     73  1.1  matt 	size_t i;
     74  1.1  matt 
     75  1.1  matt 	for (; dynp->d_tag != DT_NULL; dynp++) {
     76  1.1  matt 		switch (dynp->d_tag) {
     77  1.1  matt 		case DT_REL:
     78  1.1  matt 			rel = (const Elf_Rel *)(relocbase + dynp->d_un.d_ptr);
     79  1.1  matt 			break;
     80  1.1  matt 		case DT_RELSZ:
     81  1.1  matt 			relsz = dynp->d_un.d_val;
     82  1.1  matt 			break;
     83  1.1  matt 		case DT_SYMTAB:
     84  1.1  matt 			symtab = (const Elf_Sym *)(relocbase + dynp->d_un.d_ptr);
     85  1.1  matt 			break;
     86  1.1  matt 		case DT_PLTGOT:
     87  1.1  matt 			got = (Elf_Addr *)(relocbase + dynp->d_un.d_ptr);
     88  1.1  matt 			break;
     89  1.1  matt 		case DT_RISCV_LOCAL_GOTNO:
     90  1.1  matt 			local_gotno = dynp->d_un.d_val;
     91  1.1  matt 			break;
     92  1.1  matt 		case DT_RISCV_SYMTABNO:
     93  1.1  matt 			symtabno = dynp->d_un.d_val;
     94  1.1  matt 			break;
     95  1.1  matt 		case DT_RISCV_GOTSYM:
     96  1.1  matt 			gotsym = dynp->d_un.d_val;
     97  1.1  matt 			break;
     98  1.1  matt 		}
     99  1.1  matt 	}
    100  1.1  matt 
    101  1.1  matt 	i = (got[1] & 0x80000000) ? 2 : 1;
    102  1.1  matt 	/* Relocate the local GOT entries */
    103  1.1  matt 	got += i;
    104  1.1  matt 	for (; i < local_gotno; i++)
    105  1.1  matt 		*got++ += relocbase;
    106  1.1  matt 	sym = symtab + gotsym;
    107  1.1  matt 	/* Now do the global GOT entries */
    108  1.1  matt 	for (i = gotsym; i < symtabno; i++) {
    109  1.1  matt 		*got = sym->st_value + relocbase;
    110  1.1  matt 		++sym;
    111  1.1  matt 		++got;
    112  1.1  matt 	}
    113  1.1  matt 
    114  1.1  matt 	rellim = (const Elf_Rel *)((uintptr_t)rel + relsz);
    115  1.1  matt 	for (; rel < rellim; rel++) {
    116  1.1  matt 		Elf_Word r_symndx, r_type;
    117  1.1  matt 
    118  1.1  matt 		where = (Elf_Sxword *)(relocbase + rel->r_offset);
    119  1.1  matt 
    120  1.1  matt 		r_symndx = ELF_R_SYM(rel->r_info);
    121  1.1  matt 		r_type = ELF_R_TYPE(rel->r_info);
    122  1.1  matt 
    123  1.1  matt 		switch (r_type & 0xff) {
    124  1.1  matt 		case R_TYPE(REL32): {
    125  1.1  matt 			Elf_Sxword old = *where;
    126  1.1  matt 			Elf_Sxword val = old;
    127  1.1  matt #if ELFSIZE == 64
    128  1.1  matt 			assert(r_type == R_TYPE(REL32)
    129  1.1  matt 			    || r_type == (R_TYPE(REL32)|(R_TYPE(64) << 8)));
    130  1.1  matt #endif
    131  1.1  matt 			assert(r_symndx < gotsym);
    132  1.1  matt 			sym = symtab + r_symndx;
    133  1.1  matt 			assert(ELF_ST_BIND(sym->st_info) == STB_LOCAL);
    134  1.1  matt 			val += relocbase;
    135  1.1  matt 			*(Elf_Sword *)where = val;
    136  1.1  matt 			rdbg(("REL32/L(%p) %p -> %p in <self>",
    137  1.1  matt 			    where, (void *)old, (void *)val));
    138  1.1  matt 			break;
    139  1.1  matt 		}
    140  1.1  matt 
    141  1.1  matt 		case R_TYPE(NONE):
    142  1.1  matt 			break;
    143  1.1  matt 
    144  1.1  matt 		default:
    145  1.1  matt 			abort();
    146  1.1  matt 		}
    147  1.1  matt 	}
    148  1.1  matt }
    149  1.1  matt 
    150  1.1  matt int
    151  1.1  matt _rtld_relocate_nonplt_objects(Obj_Entry *obj)
    152  1.1  matt {
    153  1.1  matt 	const Elf_Rel *rel;
    154  1.1  matt 	Elf_Addr *got = obj->pltgot;
    155  1.1  matt 	const Elf_Sym *sym, *def;
    156  1.1  matt 	const Obj_Entry *defobj;
    157  1.1  matt 	Elf_Word i;
    158  1.1  matt 
    159  1.1  matt 	i = 2;
    160  1.1  matt 	/* Relocate the local GOT entries */
    161  1.1  matt 	got += i;
    162  1.1  matt 	for (; i < obj->local_gotno; i++)
    163  1.1  matt 		*got++ += (Elf_Addr)obj->relocbase;
    164  1.1  matt 
    165  1.1  matt 	sym = obj->symtab + obj->gotsym;
    166  1.1  matt 	/* Now do the global GOT entries */
    167  1.1  matt 	for (i = obj->gotsym; i < obj->symtabno; i++) {
    168  1.1  matt 		rdbg((" doing got %d sym %p (%s, %lx)", i - obj->gotsym, sym,
    169  1.1  matt 		    sym->st_name + obj->strtab, (u_long) *got));
    170  1.1  matt 
    171  1.1  matt 		if (ELF_ST_TYPE(sym->st_info) == STT_FUNC &&
    172  1.1  matt 		    sym->st_value != 0 && sym->st_shndx == SHN_UNDEF) {
    173  1.1  matt 			/*
    174  1.1  matt 			 * If there are non-PLT references to the function,
    175  1.1  matt 			 * st_value should be 0, forcing us to resolve the
    176  1.1  matt 			 * address immediately.
    177  1.1  matt 			 *
    178  1.1  matt 			 * XXX DANGER WILL ROBINSON!
    179  1.1  matt 			 * The linker is not outputting PLT slots for calls to
    180  1.1  matt 			 * functions that are defined in the same shared
    181  1.1  matt 			 * library.  This is a bug, because it can screw up
    182  1.1  matt 			 * link ordering rules if the symbol is defined in
    183  1.1  matt 			 * more than one module.  For now, if there is a
    184  1.1  matt 			 * definition, we fail the test above and force a full
    185  1.1  matt 			 * symbol lookup.  This means that all intra-module
    186  1.1  matt 			 * calls are bound immediately.  - mycroft, 2003/09/24
    187  1.1  matt 			 */
    188  1.1  matt 			*got = sym->st_value + (Elf_Addr)obj->relocbase;
    189  1.1  matt 		} else if (sym->st_info == ELF_ST_INFO(STB_GLOBAL, STT_SECTION)) {
    190  1.1  matt 			/* Symbols with index SHN_ABS are not relocated. */
    191  1.1  matt 			if (sym->st_shndx != SHN_ABS)
    192  1.1  matt 				*got = sym->st_value +
    193  1.1  matt 				    (Elf_Addr)obj->relocbase;
    194  1.1  matt 		} else {
    195  1.1  matt 			def = _rtld_find_symdef(i, obj, &defobj, false);
    196  1.1  matt 			if (def == NULL)
    197  1.1  matt 				return -1;
    198  1.1  matt 			*got = def->st_value + (Elf_Addr)defobj->relocbase;
    199  1.1  matt 		}
    200  1.1  matt 
    201  1.1  matt 		rdbg(("  --> now %lx", (u_long) *got));
    202  1.1  matt 		++sym;
    203  1.1  matt 		++got;
    204  1.1  matt 	}
    205  1.1  matt 
    206  1.1  matt 	got = obj->pltgot;
    207  1.1  matt 	for (rel = obj->rel; rel < obj->rellim; rel++) {
    208  1.1  matt 		Elf_Addr * const where =
    209  1.1  matt 		    (Elf_Addr *)(obj->relocbase + rel->r_offset);
    210  1.1  matt 		const Elf_Word r_symndx = ELF_R_SYM(rel->r_info);
    211  1.1  matt 		const Elf_Word r_type = ELF_R_TYPE(rel->r_info);
    212  1.1  matt 
    213  1.1  matt 		switch (r_type) {
    214  1.1  matt 		case R_TYPE(NONE):
    215  1.1  matt 			break;
    216  1.1  matt 
    217  1.1  matt 		case R_TYPE(REL32): {
    218  1.1  matt 			/* 32-bit PC-relative reference */
    219  1.1  matt 			Elf_Sxword old = *where;
    220  1.1  matt 			Elf_Sxword val = old;
    221  1.1  matt 
    222  1.1  matt 			def = obj->symtab + r_symndx;
    223  1.1  matt 
    224  1.1  matt 			if (r_symndx < obj->gotsym) {
    225  1.1  matt 				val += (Elf_Addr)obj->relocbase;
    226  1.1  matt 
    227  1.1  matt 				rdbg(("REL32/L(%p) %p -> %p (%s) in %s",
    228  1.1  matt 				    where, (void *)old, (void *)val,
    229  1.1  matt 				    obj->strtab + def->st_name, obj->path));
    230  1.1  matt 			} else {
    231  1.1  matt 				val += got[obj->local_gotno + r_symndx - obj->gotsym];
    232  1.1  matt 				rdbg(("REL32/G(%p) %p --> %p (%s) in %s",
    233  1.1  matt 				    where, (void *)old, (void *)val,
    234  1.1  matt 				    obj->strtab + def->st_name,
    235  1.1  matt 				    obj->path));
    236  1.1  matt 			}
    237  1.1  matt 			*where = val;
    238  1.1  matt 			break;
    239  1.1  matt 		}
    240  1.1  matt 
    241  1.1  matt #if ELFSIZE == 64
    242  1.1  matt 		case R_TYPE(TLS_DTPMOD64):
    243  1.1  matt #else
    244  1.1  matt 		case R_TYPE(TLS_DTPMOD32):
    245  1.1  matt #endif
    246  1.1  matt 		{
    247  1.1  matt 			Elf_Addr old = *where;
    248  1.1  matt 			Elf_Addr val = old;
    249  1.1  matt 
    250  1.1  matt 			def = _rtld_find_symdef(r_symndx, obj, &defobj, false);
    251  1.1  matt 			if (def == NULL)
    252  1.1  matt 				return -1;
    253  1.1  matt 
    254  1.1  matt 			val += (Elf_Addr)defobj->tlsindex;
    255  1.1  matt 
    256  1.1  matt 			*where = val;
    257  1.1  matt 			rdbg(("DTPMOD %s in %s --> %p in %s",
    258  1.1  matt 			    obj->strtab + obj->symtab[r_symndx].st_name,
    259  1.1  matt 			    obj->path, (void *)old, defobj->path));
    260  1.1  matt 			break;
    261  1.1  matt 		}
    262  1.1  matt 
    263  1.1  matt #if ELFSIZE == 64
    264  1.1  matt 		case R_TYPE(TLS_DTPREL64):
    265  1.1  matt #else
    266  1.1  matt 		case R_TYPE(TLS_DTPREL32):
    267  1.1  matt #endif
    268  1.1  matt 		{
    269  1.1  matt 			Elf_Addr old = *where;
    270  1.1  matt 			Elf_Addr val = old;
    271  1.1  matt 
    272  1.1  matt 			def = _rtld_find_symdef(r_symndx, obj, &defobj, false);
    273  1.1  matt 			if (def == NULL)
    274  1.1  matt 				return -1;
    275  1.1  matt 
    276  1.1  matt 			if (!defobj->tls_done && _rtld_tls_offset_allocate(obj))
    277  1.1  matt 				return -1;
    278  1.1  matt 
    279  1.1  matt 			val += (Elf_Addr)def->st_value - TLS_DTV_OFFSET;
    280  1.1  matt 			*(Elf_Word *) where = val;
    281  1.1  matt 
    282  1.1  matt 			rdbg(("DTPREL %s in %s --> %p in %s",
    283  1.1  matt 			    obj->strtab + obj->symtab[r_symndx].st_name,
    284  1.1  matt 			    obj->path, (void *)old, defobj->path));
    285  1.1  matt 			break;
    286  1.1  matt 		}
    287  1.1  matt 
    288  1.1  matt 		default:
    289  1.1  matt 			rdbg(("sym = %lu, type = %lu, offset = %p, "
    290  1.1  matt 			    "contents = %p, symbol = %s",
    291  1.1  matt 			    (u_long)r_symndx, (u_long)ELF_R_TYPE(rel->r_info),
    292  1.1  matt 			    (void *)rel->r_offset,
    293  1.1  matt 			    (void *)load_ptr(where, sizeof(Elf_Sword)),
    294  1.1  matt 			    obj->strtab + obj->symtab[r_symndx].st_name));
    295  1.1  matt 			_rtld_error("%s: Unsupported relocation type %ld "
    296  1.1  matt 			    "in non-PLT relocations",
    297  1.1  matt 			    obj->path, (u_long) ELF_R_TYPE(rel->r_info));
    298  1.1  matt 			return -1;
    299  1.1  matt 		}
    300  1.1  matt 	}
    301  1.1  matt 
    302  1.1  matt 	return 0;
    303  1.1  matt }
    304  1.1  matt 
    305  1.1  matt int
    306  1.1  matt _rtld_relocate_plt_lazy(const Obj_Entry *obj)
    307  1.1  matt {
    308  1.1  matt 	/* PLT fixups were done above in the GOT relocation. */
    309  1.1  matt 	return 0;
    310  1.1  matt }
    311  1.1  matt 
    312  1.1  matt static int
    313  1.1  matt _rtld_relocate_plt_object(const Obj_Entry *obj, const Elf_Rel *rel,
    314  1.1  matt     Elf_Addr *tp)
    315  1.1  matt {
    316  1.1  matt 	const Obj_Entry *defobj;
    317  1.1  matt 	Elf_Addr new_value;
    318  1.1  matt 
    319  1.1  matt         assert(ELF_R_TYPE(rel->r_info) == R_TYPE(JMP_SLOT));
    320  1.1  matt 
    321  1.1  matt 	const Elf_Sym *def = _rtld_find_plt_symdef(ELF_R_SYM(rel->r_info),
    322  1.1  matt 	    obj, &defobj, tp != NULL);
    323  1.1  matt 	if (__predict_false(def == NULL))
    324  1.1  matt 		return -1;
    325  1.1  matt 	if (__predict_false(def == &_rtld_sym_zero))
    326  1.1  matt 		return -1;
    327  1.1  matt 
    328  1.1  matt 	if (ELF_ST_TYPE(def->st_info) == STT_GNU_IFUNC) {
    329  1.1  matt 		if (tp == NULL)
    330  1.1  matt 			return 0;
    331  1.1  matt 		new_value = _rtld_resolve_ifunc(defobj, def);
    332  1.1  matt 	} else {
    333  1.1  matt 		new_value = (Elf_Addr)(defobj->relocbase + def->st_value);
    334  1.1  matt 	}
    335  1.1  matt 	rdbg(("bind now/fixup in %s --> new=%p",
    336  1.1  matt 	    defobj->strtab + def->st_name, (void *)new_value));
    337  1.1  matt 	*(Elf_Addr *)(obj->relocbase + rel->r_offset) = new_value;
    338  1.1  matt 
    339  1.1  matt 	if (tp)
    340  1.1  matt 		*tp = new_value;
    341  1.1  matt 	return 0;
    342  1.1  matt }
    343  1.1  matt 
    344  1.1  matt void *
    345  1.1  matt _rtld_bind(const Obj_Entry *obj, Elf_Word reloff)
    346  1.1  matt {
    347  1.1  matt 	const Elf_Rel *pltrel = (const Elf_Rel *)(obj->pltrel + reloff);
    348  1.1  matt 	Elf_Addr new_value;
    349  1.1  matt 	int err;
    350  1.1  matt 
    351  1.1  matt 	_rtld_shared_enter();
    352  1.1  matt 	err = _rtld_relocate_plt_object(obj, pltrel, &new_value);
    353  1.1  matt 	if (err)
    354  1.1  matt 		_rtld_die();
    355  1.1  matt 	_rtld_shared_exit();
    356  1.1  matt 
    357  1.1  matt 	return (caddr_t)new_value;
    358  1.1  matt }
    359  1.1  matt 
    360  1.1  matt int
    361  1.1  matt _rtld_relocate_plt_objects(const Obj_Entry *obj)
    362  1.1  matt {
    363  1.1  matt 
    364  1.1  matt 	for (const Elf_Rel *rel = obj->pltrel; rel < obj->pltrellim; rel++) {
    365  1.1  matt 		if (_rtld_relocate_plt_object(obj, rel, NULL) < 0)
    366  1.1  matt 			return -1;
    367  1.1  matt 	}
    368  1.1  matt 
    369  1.1  matt 	return 0;
    370  1.1  matt }
    371