Home | History | Annotate | Line # | Download | only in sparc
mdreloc.c revision 1.49
      1 /*	$NetBSD: mdreloc.c,v 1.49 2017/06/15 23:08:46 joerg Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1999, 2002 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Paul Kranenburg and by Charles M. Hannum.
      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.49 2017/06/15 23:08:46 joerg Exp $");
     35 #endif /* not lint */
     36 
     37 #include <errno.h>
     38 #include <stdio.h>
     39 #include <stdlib.h>
     40 #include <string.h>
     41 #include <unistd.h>
     42 
     43 #include "rtldenv.h"
     44 #include "debug.h"
     45 #include "rtld.h"
     46 
     47 /*
     48  * The following table holds for each relocation type:
     49  *	- the width in bits of the memory location the relocation
     50  *	  applies to (not currently used)
     51  *	- the number of bits the relocation value must be shifted to the
     52  *	  right (i.e. discard least significant bits) to fit into
     53  *	  the appropriate field in the instruction word.
     54  *	- flags indicating whether
     55  *		* the relocation involves a symbol
     56  *		* the relocation is relative to the current position
     57  *		* the relocation is for a GOT entry
     58  *		* the relocation is relative to the load address
     59  *
     60  */
     61 #define _RF_S		0x80000000		/* Resolve symbol */
     62 #define _RF_A		0x40000000		/* Use addend */
     63 #define _RF_P		0x20000000		/* Location relative */
     64 #define _RF_G		0x10000000		/* GOT offset */
     65 #define _RF_B		0x08000000		/* Load address relative */
     66 #define _RF_U		0x04000000		/* Unaligned */
     67 #define _RF_SZ(s)	(((s) & 0xff) << 8)	/* memory target size */
     68 #define _RF_RS(s)	( (s) & 0xff)		/* right shift */
     69 static const int reloc_target_flags[R_TYPE(TLS_TPOFF64)+1] = {
     70 	0,							/* NONE */
     71 	_RF_S|_RF_A|		_RF_SZ(8)  | _RF_RS(0),		/* RELOC_8 */
     72 	_RF_S|_RF_A|		_RF_SZ(16) | _RF_RS(0),		/* RELOC_16 */
     73 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* RELOC_32 */
     74 	_RF_S|_RF_A|_RF_P|	_RF_SZ(8)  | _RF_RS(0),		/* DISP_8 */
     75 	_RF_S|_RF_A|_RF_P|	_RF_SZ(16) | _RF_RS(0),		/* DISP_16 */
     76 	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(0),		/* DISP_32 */
     77 	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(2),		/* WDISP_30 */
     78 	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(2),		/* WDISP_22 */
     79 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(10),	/* HI22 */
     80 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* 22 */
     81 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* 13 */
     82 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* LO10 */
     83 	_RF_G|			_RF_SZ(32) | _RF_RS(0),		/* GOT10 */
     84 	_RF_G|			_RF_SZ(32) | _RF_RS(0),		/* GOT13 */
     85 	_RF_G|			_RF_SZ(32) | _RF_RS(10),	/* GOT22 */
     86 	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(0),		/* PC10 */
     87 	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(10),	/* PC22 */
     88 	      _RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(2),		/* WPLT30 */
     89 				_RF_SZ(32) | _RF_RS(0),		/* COPY */
     90 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* GLOB_DAT */
     91 				_RF_SZ(32) | _RF_RS(0),		/* JMP_SLOT */
     92 	      _RF_A|	_RF_B|	_RF_SZ(32) | _RF_RS(0),		/* RELATIVE */
     93 	_RF_S|_RF_A|	_RF_U|	_RF_SZ(32) | _RF_RS(0),		/* UA_32 */
     94 
     95 	/* TLS and 64 bit relocs not listed here... */
     96 };
     97 
     98 #ifdef RTLD_DEBUG_RELOC
     99 static const char *reloc_names[] = {
    100 	"NONE", "RELOC_8", "RELOC_16", "RELOC_32", "DISP_8",
    101 	"DISP_16", "DISP_32", "WDISP_30", "WDISP_22", "HI22",
    102 	"22", "13", "LO10", "GOT10", "GOT13",
    103 	"GOT22", "PC10", "PC22", "WPLT30", "COPY",
    104 	"GLOB_DAT", "JMP_SLOT", "RELATIVE", "UA_32",
    105 
    106 	/* not used with 32bit userland, besides a few of the TLS ones */
    107 	"PLT32",
    108 	"HIPLT22", "LOPLT10", "LOPLT10", "PCPLT22", "PCPLT32",
    109 	"10", "11", "64", "OLO10", "HH22",
    110 	"HM10", "LM22", "PC_HH22", "PC_HM10", "PC_LM22",
    111 	"WDISP16", "WDISP19", "GLOB_JMP", "7", "5", "6",
    112 	"DISP64", "PLT64", "HIX22", "LOX10", "H44", "M44",
    113 	"L44", "REGISTER", "UA64", "UA16",
    114 	"TLS_GD_HI22", "TLS_GD_LO10", "TLS_GD_ADD", "TLS_GD_CALL",
    115 	"TLS_LDM_HI22", "TLS_LDM_LO10", "TLS_LDM_ADD", "TLS_LDM_CALL",
    116 	"TLS_LDO_HIX22", "TLS_LDO_LOX10", "TLS_LDO_ADD", "TLS_IE_HI22",
    117 	"TLS_IE_LO10", "TLS_IE_LD", "TLS_IE_LDX", "TLS_IE_ADD", "TLS_LE_HIX22",
    118 	"TLS_LE_LOX10", "TLS_DTPMOD32", "TLS_DTPMOD64", "TLS_DTPOFF32",
    119 	"TLS_DTPOFF64", "TLS_TPOFF32", "TLS_TPOFF64",
    120 };
    121 #endif
    122 
    123 #define RELOC_RESOLVE_SYMBOL(t)		((reloc_target_flags[t] & _RF_S) != 0)
    124 #define RELOC_PC_RELATIVE(t)		((reloc_target_flags[t] & _RF_P) != 0)
    125 #define RELOC_BASE_RELATIVE(t)		((reloc_target_flags[t] & _RF_B) != 0)
    126 #define RELOC_UNALIGNED(t)		((reloc_target_flags[t] & _RF_U) != 0)
    127 #define RELOC_USE_ADDEND(t)		((reloc_target_flags[t] & _RF_A) != 0)
    128 #define RELOC_TARGET_SIZE(t)		((reloc_target_flags[t] >> 8) & 0xff)
    129 #define RELOC_VALUE_RIGHTSHIFT(t)	(reloc_target_flags[t] & 0xff)
    130 #define RELOC_TLS(t)			(t >= R_TYPE(TLS_GD_HI22))
    131 
    132 static const int reloc_target_bitmask[] = {
    133 #define _BM(x)	(~(-(1ULL << (x))))
    134 	0,				/* NONE */
    135 	_BM(8), _BM(16), _BM(32),	/* RELOC_8, _16, _32 */
    136 	_BM(8), _BM(16), _BM(32),	/* DISP8, DISP16, DISP32 */
    137 	_BM(30), _BM(22),		/* WDISP30, WDISP22 */
    138 	_BM(22), _BM(22),		/* HI22, _22 */
    139 	_BM(13), _BM(10),		/* RELOC_13, _LO10 */
    140 	_BM(10), _BM(13), _BM(22),	/* GOT10, GOT13, GOT22 */
    141 	_BM(10), _BM(22),		/* _PC10, _PC22 */
    142 	_BM(30), 0,			/* _WPLT30, _COPY */
    143 	-1, -1, -1,			/* _GLOB_DAT, JMP_SLOT, _RELATIVE */
    144 	_BM(32)				/* _UA32 */
    145 #undef _BM
    146 };
    147 #define RELOC_VALUE_BITMASK(t)	(reloc_target_bitmask[t])
    148 
    149 void _rtld_bind_start(void);
    150 void _rtld_relocate_nonplt_self(Elf_Dyn *, Elf_Addr);
    151 caddr_t _rtld_bind(const Obj_Entry *, Elf_Word);
    152 static inline int _rtld_relocate_plt_object(const Obj_Entry *,
    153     const Elf_Rela *, Elf_Addr *);
    154 
    155 void
    156 _rtld_setup_pltgot(const Obj_Entry *obj)
    157 {
    158 	/*
    159 	 * PLTGOT is the PLT on the sparc.
    160 	 * The first entry holds the call the dynamic linker.
    161 	 * We construct a `call' sequence that transfers
    162 	 * to `_rtld_bind_start()'.
    163 	 * The second entry holds the object identification.
    164 	 * Note: each PLT entry is three words long.
    165 	 */
    166 #define SAVE	0x9de3bfa0	/* i.e. `save %sp,-96,%sp' */
    167 #define CALL	0x40000000
    168 #define NOP	0x01000000
    169 	obj->pltgot[0] = SAVE;
    170 	obj->pltgot[1] = CALL |
    171 	    ((Elf_Addr) &_rtld_bind_start - (Elf_Addr) &obj->pltgot[1]) >> 2;
    172 	obj->pltgot[2] = NOP;
    173 	obj->pltgot[3] = (Elf_Addr) obj;
    174 }
    175 
    176 void
    177 _rtld_relocate_nonplt_self(Elf_Dyn *dynp, Elf_Addr relocbase)
    178 {
    179 	const Elf_Rela *rela = 0, *relalim;
    180 	Elf_Addr relasz = 0;
    181 	Elf_Addr *where;
    182 
    183 	for (; dynp->d_tag != DT_NULL; dynp++) {
    184 		switch (dynp->d_tag) {
    185 		case DT_RELA:
    186 			rela = (const Elf_Rela *)(relocbase + dynp->d_un.d_ptr);
    187 			break;
    188 		case DT_RELASZ:
    189 			relasz = dynp->d_un.d_val;
    190 			break;
    191 		}
    192 	}
    193 	relalim = (const Elf_Rela *)((const uint8_t *)rela + relasz);
    194 	for (; rela < relalim; rela++) {
    195 		where = (Elf_Addr *)(relocbase + rela->r_offset);
    196 		*where += (Elf_Addr)(relocbase + rela->r_addend);
    197 	}
    198 }
    199 
    200 int
    201 _rtld_relocate_nonplt_objects(Obj_Entry *obj)
    202 {
    203 	const Elf_Rela *rela;
    204 
    205 	for (rela = obj->rela; rela < obj->relalim; rela++) {
    206 		Elf_Addr *where;
    207 		Elf_Word type, value, mask;
    208 		const Elf_Sym *def = NULL;
    209 		const Obj_Entry *defobj = NULL;
    210 		unsigned long	 symnum;
    211 
    212 		where = (Elf_Addr *) (obj->relocbase + rela->r_offset);
    213 		symnum = ELF_R_SYM(rela->r_info);
    214 
    215 		type = ELF_R_TYPE(rela->r_info);
    216 		if (type == R_TYPE(NONE))
    217 			continue;
    218 
    219 		/* We do JMP_SLOTs in _rtld_bind() below */
    220 		if (type == R_TYPE(JMP_SLOT))
    221 			continue;
    222 
    223 		/* COPY relocs are also handled elsewhere */
    224 		if (type == R_TYPE(COPY))
    225 			continue;
    226 
    227 		/*
    228 		 * We use the fact that relocation types are an `enum'
    229 		 * Note: R_SPARC_TLS_TPOFF64 is currently numerically largest.
    230 		 */
    231 		if (type > R_TYPE(TLS_TPOFF64))
    232 			return (-1);
    233 
    234 		value = rela->r_addend;
    235 
    236 		/*
    237 		 * Handle TLS relocations here, they are different.
    238 		 */
    239 		if (RELOC_TLS(type)) {
    240 			switch (type) {
    241 			case R_TYPE(TLS_DTPMOD32):
    242 				def = _rtld_find_symdef(symnum, obj,
    243 				    &defobj, false);
    244 				if (def == NULL)
    245 					return -1;
    246 
    247 				*where = (Elf_Addr)defobj->tlsindex;
    248 
    249 				rdbg(("TLS_DTPMOD32 %s in %s --> %p",
    250 				    obj->strtab +
    251 				    obj->symtab[symnum].st_name,
    252 				    obj->path, (void *)*where));
    253 
    254 				break;
    255 
    256 			case R_TYPE(TLS_DTPOFF32):
    257 				def = _rtld_find_symdef(symnum, obj,
    258 				    &defobj, false);
    259 				if (def == NULL)
    260 					return -1;
    261 
    262 				*where = (Elf_Addr)(def->st_value
    263 				    + rela->r_addend);
    264 
    265 				rdbg(("TLS_DTPOFF32 %s in %s --> %p",
    266 				    obj->strtab +
    267 				        obj->symtab[symnum].st_name,
    268 				    obj->path, (void *)*where));
    269 
    270 				break;
    271 
    272 			case R_TYPE(TLS_TPOFF32):
    273 				def = _rtld_find_symdef(symnum, obj,
    274 				    &defobj, false);
    275 				if (def == NULL)
    276 					return -1;
    277 
    278 				if (!defobj->tls_done &&
    279 					_rtld_tls_offset_allocate(obj))
    280 					     return -1;
    281 
    282 				*where = (Elf_Addr)(def->st_value -
    283 				    defobj->tlsoffset + rela->r_addend);
    284 
    285 				rdbg(("TLS_TPOFF32 %s in %s --> %p",
    286 				    obj->strtab +
    287 				    obj->symtab[symnum].st_name,
    288 				    obj->path, (void *)*where));
    289 
    290 				break;
    291 			}
    292 			continue;
    293 		}
    294 
    295 		/*
    296 		 * If it is no TLS relocation (handled above), we can not
    297 		 * deal with it if it is beyound R_SPARC_6.
    298 		 */
    299 		if (type > R_TYPE(6))
    300 			return (-1);
    301 
    302 		/*
    303 		 * Handle relative relocs here, as an optimization.
    304 		 */
    305 		if (type == R_TYPE(RELATIVE)) {
    306 			*where += (Elf_Addr)(obj->relocbase + value);
    307 			rdbg(("RELATIVE in %s --> %p", obj->path,
    308 			    (void *)*where));
    309 			continue;
    310 		}
    311 
    312 		if (RELOC_RESOLVE_SYMBOL(type)) {
    313 
    314 			/* Find the symbol */
    315 			def = _rtld_find_symdef(symnum, obj, &defobj, false);
    316 			if (def == NULL)
    317 				return (-1);
    318 
    319 			/* Add in the symbol's absolute address */
    320 			value += (Elf_Word)(defobj->relocbase + def->st_value);
    321 		}
    322 
    323 		if (RELOC_PC_RELATIVE(type)) {
    324 			value -= (Elf_Word)where;
    325 		}
    326 
    327 		if (RELOC_BASE_RELATIVE(type)) {
    328 			/*
    329 			 * Note that even though sparcs use `Elf_rela'
    330 			 * exclusively we still need the implicit memory addend
    331 			 * in relocations referring to GOT entries.
    332 			 * Undoubtedly, someone f*cked this up in the distant
    333 			 * past, and now we're stuck with it in the name of
    334 			 * compatibility for all eternity..
    335 			 *
    336 			 * In any case, the implicit and explicit should be
    337 			 * mutually exclusive. We provide a check for that
    338 			 * here.
    339 			 */
    340 #define DIAGNOSTIC
    341 #ifdef DIAGNOSTIC
    342 			if (value != 0 && *where != 0) {
    343 				xprintf("BASE_REL(%s): where=%p, *where 0x%x, "
    344 					"addend=0x%x, base %p\n",
    345 					obj->path, where, *where,
    346 					rela->r_addend, obj->relocbase);
    347 			}
    348 #endif
    349 			value += (Elf_Word)(obj->relocbase + *where);
    350 		}
    351 
    352 		mask = RELOC_VALUE_BITMASK(type);
    353 		value >>= RELOC_VALUE_RIGHTSHIFT(type);
    354 		value &= mask;
    355 
    356 		if (RELOC_UNALIGNED(type)) {
    357 			/* Handle unaligned relocations. */
    358 			Elf_Addr tmp = 0;
    359 			char *ptr = (char *)where;
    360 			int i, size = RELOC_TARGET_SIZE(type)/8;
    361 
    362 			/* Read it in one byte at a time. */
    363 			for (i=0; i<size; i++)
    364 				tmp = (tmp << 8) | ptr[i];
    365 
    366 			tmp &= ~mask;
    367 			tmp |= value;
    368 
    369 			/* Write it back out. */
    370 			for (i=0; i<size; i++)
    371 				ptr[i] = ((tmp >> (8*i)) & 0xff);
    372 #ifdef RTLD_DEBUG_RELOC
    373 			value = (Elf_Word)tmp;
    374 #endif
    375 
    376 		} else {
    377 			*where &= ~mask;
    378 			*where |= value;
    379 #ifdef RTLD_DEBUG_RELOC
    380 			value = (Elf_Word)*where;
    381 #endif
    382 		}
    383 #ifdef RTLD_DEBUG_RELOC
    384 		if (RELOC_RESOLVE_SYMBOL(type)) {
    385 			rdbg(("%s %s in %s --> %p in %s", reloc_names[type],
    386 			    obj->strtab + obj->symtab[symnum].st_name,
    387 			    obj->path, (void *)value, defobj->path));
    388 		} else {
    389 			rdbg(("%s in %s --> %p", reloc_names[type],
    390 			    obj->path, (void *)value));
    391 		}
    392 #endif
    393 	}
    394 	return (0);
    395 }
    396 
    397 int
    398 _rtld_relocate_plt_lazy(const Obj_Entry *obj)
    399 {
    400 	return (0);
    401 }
    402 
    403 caddr_t
    404 _rtld_bind(const Obj_Entry *obj, Elf_Word reloff)
    405 {
    406 	const Elf_Rela *rela = (const Elf_Rela *)((const uint8_t *)obj->pltrela + reloff);
    407 	Elf_Addr value;
    408 	int err;
    409 
    410 	value = 0;	/* XXX gcc */
    411 
    412 	_rtld_shared_enter();
    413 	err = _rtld_relocate_plt_object(obj, rela, &value);
    414 	if (err)
    415 		_rtld_die();
    416 	_rtld_shared_exit();
    417 
    418 	return (caddr_t)value;
    419 }
    420 
    421 int
    422 _rtld_relocate_plt_objects(const Obj_Entry *obj)
    423 {
    424 	const Elf_Rela *rela = obj->pltrela;
    425 
    426 	for (; rela < obj->pltrelalim; rela++)
    427 		if (_rtld_relocate_plt_object(obj, rela, NULL) < 0)
    428 			return -1;
    429 
    430 	return 0;
    431 }
    432 
    433 static inline int
    434 _rtld_relocate_plt_object(const Obj_Entry *obj, const Elf_Rela *rela, Elf_Addr *tp)
    435 {
    436 	const Elf_Sym *def;
    437 	const Obj_Entry *defobj;
    438 	Elf_Word *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
    439 	Elf_Addr value;
    440 	unsigned long info = rela->r_info;
    441 
    442 	assert(ELF_R_TYPE(info) == R_TYPE(JMP_SLOT));
    443 
    444 	def = _rtld_find_plt_symdef(ELF_R_SYM(info), obj, &defobj, tp != NULL);
    445 	if (__predict_false(def == NULL))
    446 		return -1;
    447 	if (__predict_false(def == &_rtld_sym_zero))
    448 		return 0;
    449 
    450 	if (ELF_ST_TYPE(def->st_info) == STT_GNU_IFUNC) {
    451 		if (tp == NULL)
    452 			return 0;
    453 		value = _rtld_resolve_ifunc(defobj, def);
    454 	} else {
    455 		value = (Elf_Addr)(defobj->relocbase + def->st_value);
    456 	}
    457 	rdbg(("bind now/fixup in %s --> new=%p",
    458 	    defobj->strtab + def->st_name, (void *)value));
    459 
    460 	/*
    461 	 * At the PLT entry pointed at by `where', we now construct
    462 	 * a direct transfer to the now fully resolved function
    463 	 * address.  The resulting code in the jump slot is:
    464 	 *
    465 	 *	sethi	%hi(roffset), %g1
    466 	 *	sethi	%hi(addr), %g1
    467 	 *	jmp	%g1+%lo(addr)
    468 	 *
    469 	 * We write the third instruction first, since that leaves the
    470 	 * previous `b,a' at the second word in place. Hence the whole
    471 	 * PLT slot can be atomically change to the new sequence by
    472 	 * writing the `sethi' instruction at word 2.
    473 	 */
    474 #define SETHI	0x03000000
    475 #define JMP	0x81c06000
    476 #define NOP	0x01000000
    477 	where[2] = JMP   | (value & 0x000003ff);
    478 	where[1] = SETHI | ((value >> 10) & 0x003fffff);
    479 	__asm volatile("iflush %0+8" : : "r" (where));
    480 	__asm volatile("iflush %0+4" : : "r" (where));
    481 
    482 	if (tp)
    483 		*tp = value;
    484 
    485 	return 0;
    486 }
    487