Home | History | Annotate | Line # | Download | only in sparc
mdreloc.c revision 1.33
      1 /*	$NetBSD: mdreloc.c,v 1.33 2003/07/24 10:12:29 skrll 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  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 #include <errno.h>
     40 #include <stdio.h>
     41 #include <stdlib.h>
     42 #include <string.h>
     43 #include <unistd.h>
     44 #include <sys/stat.h>
     45 
     46 #include "rtldenv.h"
     47 #include "debug.h"
     48 #include "rtld.h"
     49 
     50 /*
     51  * The following table holds for each relocation type:
     52  *	- the width in bits of the memory location the relocation
     53  *	  applies to (not currently used)
     54  *	- the number of bits the relocation value must be shifted to the
     55  *	  right (i.e. discard least significant bits) to fit into
     56  *	  the appropriate field in the instruction word.
     57  *	- flags indicating whether
     58  *		* the relocation involves a symbol
     59  *		* the relocation is relative to the current position
     60  *		* the relocation is for a GOT entry
     61  *		* the relocation is relative to the load address
     62  *
     63  */
     64 #define _RF_S		0x80000000		/* Resolve symbol */
     65 #define _RF_A		0x40000000		/* Use addend */
     66 #define _RF_P		0x20000000		/* Location relative */
     67 #define _RF_G		0x10000000		/* GOT offset */
     68 #define _RF_B		0x08000000		/* Load address relative */
     69 #define _RF_SZ(s)	(((s) & 0xff) << 8)	/* memory target size */
     70 #define _RF_RS(s)	( (s) & 0xff)		/* right shift */
     71 static const int reloc_target_flags[] = {
     72 	0,							/* NONE */
     73 	_RF_S|_RF_A|		_RF_SZ(8)  | _RF_RS(0),		/* RELOC_8 */
     74 	_RF_S|_RF_A|		_RF_SZ(16) | _RF_RS(0),		/* RELOC_16 */
     75 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* RELOC_32 */
     76 	_RF_S|_RF_A|_RF_P|	_RF_SZ(8)  | _RF_RS(0),		/* DISP_8 */
     77 	_RF_S|_RF_A|_RF_P|	_RF_SZ(16) | _RF_RS(0),		/* DISP_16 */
     78 	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(0),		/* DISP_32 */
     79 	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(2),		/* WDISP_30 */
     80 	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(2),		/* WDISP_22 */
     81 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(10),	/* HI22 */
     82 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* 22 */
     83 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* 13 */
     84 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* LO10 */
     85 	_RF_G|			_RF_SZ(32) | _RF_RS(0),		/* GOT10 */
     86 	_RF_G|			_RF_SZ(32) | _RF_RS(0),		/* GOT13 */
     87 	_RF_G|			_RF_SZ(32) | _RF_RS(10),	/* GOT22 */
     88 	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(0),		/* PC10 */
     89 	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(10),	/* PC22 */
     90 	      _RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(2),		/* WPLT30 */
     91 				_RF_SZ(32) | _RF_RS(0),		/* COPY */
     92 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* GLOB_DAT */
     93 				_RF_SZ(32) | _RF_RS(0),		/* JMP_SLOT */
     94 	      _RF_A|	_RF_B|	_RF_SZ(32) | _RF_RS(0),		/* RELATIVE */
     95 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* UA_32 */
     96 
     97 	/*unknown*/		_RF_SZ(32) | _RF_RS(0),		/* PLT32 */
     98 	/*unknown*/		_RF_SZ(32) | _RF_RS(0),		/* HIPLT22 */
     99 	/*unknown*/		_RF_SZ(32) | _RF_RS(0),		/* LOPLT10 */
    100 	/*unknown*/		_RF_SZ(32) | _RF_RS(0),		/* LOPLT10 */
    101 	/*unknown*/		_RF_SZ(32) | _RF_RS(0),		/* PCPLT22 */
    102 	/*unknown*/		_RF_SZ(32) | _RF_RS(0),		/* PCPLT32 */
    103 	_RF_S|_RF_A|/*unknown*/	_RF_SZ(32) | _RF_RS(0),		/* 10 */
    104 	_RF_S|_RF_A|/*unknown*/	_RF_SZ(32) | _RF_RS(0),		/* 11 */
    105 	_RF_S|_RF_A|/*unknown*/	_RF_SZ(32) | _RF_RS(0),		/* 64 */
    106 	_RF_S|_RF_A|/*unknown*/	_RF_SZ(32) | _RF_RS(0),		/* OLO10 */
    107 	_RF_S|_RF_A|/*unknown*/	_RF_SZ(32) | _RF_RS(0),		/* HH22 */
    108 	_RF_S|_RF_A|/*unknown*/	_RF_SZ(32) | _RF_RS(0),		/* HM10 */
    109 	_RF_S|_RF_A|/*unknown*/	_RF_SZ(32) | _RF_RS(0),		/* LM22 */
    110 	_RF_S|_RF_A|_RF_P|/*unknown*/	_RF_SZ(32) | _RF_RS(0),	/* WDISP16 */
    111 	_RF_S|_RF_A|_RF_P|/*unknown*/	_RF_SZ(32) | _RF_RS(0),	/* WDISP19 */
    112 	/*unknown*/		_RF_SZ(32) | _RF_RS(0),		/* GLOB_JMP */
    113 	/*unknown*/		_RF_SZ(32) | _RF_RS(0),		/* 7 */
    114 	/*unknown*/		_RF_SZ(32) | _RF_RS(0),		/* 5 */
    115 	/*unknown*/		_RF_SZ(32) | _RF_RS(0),		/* 6 */
    116 };
    117 
    118 #ifdef RTLD_DEBUG_RELOC
    119 static const char *reloc_names[] = {
    120 	"NONE", "RELOC_8", "RELOC_16", "RELOC_32", "DISP_8",
    121 	"DISP_16", "DISP_32", "WDISP_30", "WDISP_22", "HI22",
    122 	"22", "13", "LO10", "GOT10", "GOT13",
    123 	"GOT22", "PC10", "PC22", "WPLT30", "COPY",
    124 	"GLOB_DAT", "JMP_SLOT", "RELATIVE", "UA_32", "PLT32",
    125 	"HIPLT22", "LOPLT10", "LOPLT10", "PCPLT22", "PCPLT32",
    126 	"10", "11", "64", "OLO10", "HH22",
    127 	"HM10", "LM22", "WDISP16", "WDISP19", "GLOB_JMP",
    128 	"7", "5", "6"
    129 };
    130 #endif
    131 
    132 #define RELOC_RESOLVE_SYMBOL(t)		((reloc_target_flags[t] & _RF_S) != 0)
    133 #define RELOC_PC_RELATIVE(t)		((reloc_target_flags[t] & _RF_P) != 0)
    134 #define RELOC_BASE_RELATIVE(t)		((reloc_target_flags[t] & _RF_B) != 0)
    135 #define RELOC_TARGET_SIZE(t)		((reloc_target_flags[t] >> 8) & 0xff)
    136 #define RELOC_VALUE_RIGHTSHIFT(t)	(reloc_target_flags[t] & 0xff)
    137 
    138 static const int reloc_target_bitmask[] = {
    139 #define _BM(x)	(~(-(1ULL << (x))))
    140 	0,				/* NONE */
    141 	_BM(8), _BM(16), _BM(32),	/* RELOC_8, _16, _32 */
    142 	_BM(8), _BM(16), _BM(32),	/* DISP8, DISP16, DISP32 */
    143 	_BM(30), _BM(22),		/* WDISP30, WDISP22 */
    144 	_BM(22), _BM(22),		/* HI22, _22 */
    145 	_BM(13), _BM(10),		/* RELOC_13, _LO10 */
    146 	_BM(10), _BM(13), _BM(22),	/* GOT10, GOT13, GOT22 */
    147 	_BM(10), _BM(22),		/* _PC10, _PC22 */
    148 	_BM(30), 0,			/* _WPLT30, _COPY */
    149 	-1, -1, -1,			/* _GLOB_DAT, JMP_SLOT, _RELATIVE */
    150 	_BM(32), _BM(32),		/* _UA32, PLT32 */
    151 	_BM(22), _BM(10),		/* _HIPLT22, LOPLT10 */
    152 	_BM(32), _BM(22), _BM(10),	/* _PCPLT32, _PCPLT22, _PCPLT10 */
    153 	_BM(10), _BM(11), -1,		/* _10, _11, _64 */
    154 	_BM(10), _BM(22),		/* _OLO10, _HH22 */
    155 	_BM(10), _BM(22),		/* _HM10, _LM22 */
    156 	_BM(16), _BM(19),		/* _WDISP16, _WDISP19 */
    157 	-1,				/* GLOB_JMP */
    158 	_BM(7), _BM(5), _BM(6)		/* _7, _5, _6 */
    159 #undef _BM
    160 };
    161 #define RELOC_VALUE_BITMASK(t)	(reloc_target_bitmask[t])
    162 
    163 void _rtld_bind_start(void);
    164 void _rtld_relocate_nonplt_self(Elf_Dyn *, Elf_Addr);
    165 caddr_t _rtld_bind(const Obj_Entry *, Elf_Word);
    166 
    167 void
    168 _rtld_setup_pltgot(const Obj_Entry *obj)
    169 {
    170 	/*
    171 	 * PLTGOT is the PLT on the sparc.
    172 	 * The first entry holds the call the dynamic linker.
    173 	 * We construct a `call' sequence that transfers
    174 	 * to `_rtld_bind_start()'.
    175 	 * The second entry holds the object identification.
    176 	 * Note: each PLT entry is three words long.
    177 	 */
    178 #define SAVE	0x9de3bfa0	/* i.e. `save %sp,-96,%sp' */
    179 #define CALL	0x40000000
    180 #define NOP	0x01000000
    181 	obj->pltgot[0] = SAVE;
    182 	obj->pltgot[1] = CALL |
    183 	    ((Elf_Addr) &_rtld_bind_start - (Elf_Addr) &obj->pltgot[1]) >> 2;
    184 	obj->pltgot[2] = NOP;
    185 	obj->pltgot[3] = (Elf_Addr) obj;
    186 }
    187 
    188 void
    189 _rtld_relocate_nonplt_self(Elf_Dyn *dynp, Elf_Addr relocbase)
    190 {
    191 	const Elf_Rela *rela = 0, *relalim;
    192 	Elf_Addr relasz = 0;
    193 	Elf_Addr *where;
    194 
    195 	for (; dynp->d_tag != DT_NULL; dynp++) {
    196 		switch (dynp->d_tag) {
    197 		case DT_RELA:
    198 			rela = (const Elf_Rela *)(relocbase + dynp->d_un.d_ptr);
    199 			break;
    200 		case DT_RELASZ:
    201 			relasz = dynp->d_un.d_val;
    202 			break;
    203 		}
    204 	}
    205 	relalim = (const Elf_Rela *)((caddr_t)rela + relasz);
    206 	for (; rela < relalim; rela++) {
    207 		where = (Elf_Addr *)(relocbase + rela->r_offset);
    208 		*where += (Elf_Addr)(relocbase + rela->r_addend);
    209 	}
    210 }
    211 
    212 int
    213 _rtld_relocate_nonplt_objects(const Obj_Entry *obj)
    214 {
    215 	const Elf_Rela *rela;
    216 
    217 	for (rela = obj->rela; rela < obj->relalim; rela++) {
    218 		Elf_Addr *where;
    219 		Elf_Word type, value, mask;
    220 		const Elf_Sym *def = NULL;
    221 		const Obj_Entry *defobj = NULL;
    222 		unsigned long	 symnum;
    223 
    224 		where = (Elf_Addr *) (obj->relocbase + rela->r_offset);
    225 		symnum = ELF_R_SYM(rela->r_info);
    226 
    227 		type = ELF_R_TYPE(rela->r_info);
    228 		if (type == R_TYPE(NONE))
    229 			continue;
    230 
    231 		/* We do JMP_SLOTs in _rtld_bind() below */
    232 		if (type == R_TYPE(JMP_SLOT))
    233 			continue;
    234 
    235 		/* COPY relocs are also handled elsewhere */
    236 		if (type == R_TYPE(COPY))
    237 			continue;
    238 
    239 		/*
    240 		 * We use the fact that relocation types are an `enum'
    241 		 * Note: R_SPARC_6 is currently numerically largest.
    242 		 */
    243 		if (type > R_TYPE(6))
    244 			return (-1);
    245 
    246 		value = rela->r_addend;
    247 
    248 		/*
    249 		 * Handle relative relocs here, as an optimization.
    250 		 */
    251 		if (type == R_TYPE(RELATIVE)) {
    252 			*where += (Elf_Addr)(obj->relocbase + value);
    253 			rdbg(("RELATIVE in %s --> %p", obj->path,
    254 			    (void *)*where));
    255 			continue;
    256 		}
    257 
    258 		if (RELOC_RESOLVE_SYMBOL(type)) {
    259 
    260 			/* Find the symbol */
    261 			def = _rtld_find_symdef(symnum, obj, &defobj, false);
    262 			if (def == NULL)
    263 				return (-1);
    264 
    265 			/* Add in the symbol's absolute address */
    266 			value += (Elf_Word)(defobj->relocbase + def->st_value);
    267 		}
    268 
    269 		if (RELOC_PC_RELATIVE(type)) {
    270 			value -= (Elf_Word)where;
    271 		}
    272 
    273 		if (RELOC_BASE_RELATIVE(type)) {
    274 			/*
    275 			 * Note that even though sparcs use `Elf_rela'
    276 			 * exclusively we still need the implicit memory addend
    277 			 * in relocations referring to GOT entries.
    278 			 * Undoubtedly, someone f*cked this up in the distant
    279 			 * past, and now we're stuck with it in the name of
    280 			 * compatibility for all eternity..
    281 			 *
    282 			 * In any case, the implicit and explicit should be
    283 			 * mutually exclusive. We provide a check for that
    284 			 * here.
    285 			 */
    286 #define DIAGNOSTIC
    287 #ifdef DIAGNOSTIC
    288 			if (value != 0 && *where != 0) {
    289 				xprintf("BASE_REL(%s): where=%p, *where 0x%x, "
    290 					"addend=0x%x, base %p\n",
    291 					obj->path, where, *where,
    292 					rela->r_addend, obj->relocbase);
    293 			}
    294 #endif
    295 			value += (Elf_Word)(obj->relocbase + *where);
    296 		}
    297 
    298 		mask = RELOC_VALUE_BITMASK(type);
    299 		value >>= RELOC_VALUE_RIGHTSHIFT(type);
    300 		value &= mask;
    301 
    302 		/* We ignore alignment restrictions here */
    303 		*where &= ~mask;
    304 		*where |= value;
    305 #ifdef RTLD_DEBUG_RELOC
    306 		if (RELOC_RESOLVE_SYMBOL(type)) {
    307 			rdbg(("%s %s in %s --> %p in %s", reloc_names[type],
    308 			    obj->strtab + obj->symtab[symnum].st_name,
    309 			    obj->path, (void *)*where, defobj->path));
    310 		} else {
    311 			rdbg(("%s in %s --> %p", reloc_names[type],
    312 			    obj->path, (void *)*where));
    313 		}
    314 #endif
    315 	}
    316 	return (0);
    317 }
    318 
    319 int
    320 _rtld_relocate_plt_lazy(const Obj_Entry *obj)
    321 {
    322 	return (0);
    323 }
    324 
    325 caddr_t
    326 _rtld_bind(const Obj_Entry *obj, Elf_Word reloff)
    327 {
    328 	const Elf_Rela *rela = (const Elf_Rela *)((caddr_t)obj->pltrela + reloff);
    329 	const Elf_Sym *def;
    330 	const Obj_Entry *defobj;
    331 	Elf_Word *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
    332 	Elf_Addr value;
    333 
    334 	/* Fully resolve procedure addresses now */
    335 
    336 	assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT));
    337 
    338 	def = _rtld_find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj, true);
    339 	if (def == NULL)
    340 		_rtld_die();
    341 
    342 	value = (Elf_Addr)(defobj->relocbase + def->st_value);
    343 	rdbg(("bind now/fixup in %s --> new=%p",
    344 	    defobj->strtab + def->st_name, (void *)value));
    345 
    346 	/*
    347 	 * At the PLT entry pointed at by `where', we now construct
    348 	 * a direct transfer to the now fully resolved function
    349 	 * address.  The resulting code in the jump slot is:
    350 	 *
    351 	 *	sethi	%hi(roffset), %g1
    352 	 *	sethi	%hi(addr), %g1
    353 	 *	jmp	%g1+%lo(addr)
    354 	 *
    355 	 * We write the third instruction first, since that leaves the
    356 	 * previous `b,a' at the second word in place. Hence the whole
    357 	 * PLT slot can be atomically change to the new sequence by
    358 	 * writing the `sethi' instruction at word 2.
    359 	 */
    360 #define SETHI	0x03000000
    361 #define JMP	0x81c06000
    362 #define NOP	0x01000000
    363 	where[2] = JMP   | (value & 0x000003ff);
    364 	where[1] = SETHI | ((value >> 10) & 0x003fffff);
    365 	__asm __volatile("iflush %0+8" : : "r" (where));
    366 	__asm __volatile("iflush %0+4" : : "r" (where));
    367 
    368 	return (caddr_t)value;
    369 }
    370