Home | History | Annotate | Line # | Download | only in sparc
mdreloc.c revision 1.2
      1 /*	$NetBSD: mdreloc.c,v 1.2 1999/02/26 22:13:49 pk Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1999 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.
      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 
     45 #include "rtldenv.h"
     46 #include "debug.h"
     47 #include "rtld.h"
     48 
     49 /*
     50  * The following table holds for each relocation type:
     51  *	- the width in bits of the memory location the relocation
     52  *	  applies to (not currently used)
     53  *	- the number of bits the relocation value must be shifted to the
     54  *	  right (i.e. discard least significant bits) to fit into
     55  *	  the appropriate field in the instruction word.
     56  *	- flags indicating whether
     57  *		* the relocation involves a symbol
     58  *		* the relocation is relative to the current position
     59  *		* the relocation is for a GOT entry
     60  *		* the relocation is relative to the load address
     61  *
     62  */
     63 #define _RF_S		0x80000000		/* Resolve symbol */
     64 #define _RF_A		0x40000000		/* Use addend */
     65 #define _RF_P		0x20000000		/* Location relative */
     66 #define _RF_G		0x10000000		/* GOT offset */
     67 #define _RF_B		0x08000000		/* Load address relative */
     68 #define _RF_SZ(s)	(((s) & 0xff) << 8)	/* memory target size */
     69 #define _RF_RS(s)	( (s) & 0xff)		/* right shift */
     70 static int reloc_target_flags[] = {
     71 	0,							/* NONE */
     72 	_RF_S|_RF_A|		_RF_SZ(8)  | _RF_RS(0),		/* RELOC_8 */
     73 	_RF_S|_RF_A|		_RF_SZ(16) | _RF_RS(0),		/* RELOC_16 */
     74 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* RELOC_32 */
     75 	_RF_S|_RF_A|_RF_P|	_RF_SZ(8)  | _RF_RS(0),		/* DISP_8 */
     76 	_RF_S|_RF_A|_RF_P|	_RF_SZ(16) | _RF_RS(0),		/* DISP_16 */
     77 	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(0),		/* DISP_32 */
     78 	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(2),		/* WDISP_30 */
     79 	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(2),		/* WDISP_22 */
     80 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(10),	/* HI22 */
     81 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* 22 */
     82 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* 13 */
     83 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* LO10 */
     84 	_RF_G|			_RF_SZ(32) | _RF_RS(0),		/* GOT10 */
     85 	_RF_G|			_RF_SZ(32) | _RF_RS(0),		/* GOT13 */
     86 	_RF_G|			_RF_SZ(32) | _RF_RS(10),	/* GOT22 */
     87 	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(0),		/* PC10 */
     88 	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(10),	/* PC22 */
     89 	      _RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(2),		/* WPLT30 */
     90 				_RF_SZ(32) | _RF_RS(0),		/* COPY */
     91 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* GLOB_DAT */
     92 				_RF_SZ(32) | _RF_RS(0),		/* JMP_SLOT */
     93 	      _RF_A|	_RF_B|	_RF_SZ(32) | _RF_RS(0),		/* RELATIVE */
     94 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* UA_32 */
     95 
     96 	/*unknown*/		_RF_SZ(32) | _RF_RS(0),		/* PLT32 */
     97 	/*unknown*/		_RF_SZ(32) | _RF_RS(0),		/* HIPLT22 */
     98 	/*unknown*/		_RF_SZ(32) | _RF_RS(0),		/* LOPLT10 */
     99 	/*unknown*/		_RF_SZ(32) | _RF_RS(0),		/* LOPLT10 */
    100 	/*unknown*/		_RF_SZ(32) | _RF_RS(0),		/* PCPLT22 */
    101 	/*unknown*/		_RF_SZ(32) | _RF_RS(0),		/* PCPLT32 */
    102 	_RF_S|_RF_A|/*unknown*/	_RF_SZ(32) | _RF_RS(0),		/* 10 */
    103 	_RF_S|_RF_A|/*unknown*/	_RF_SZ(32) | _RF_RS(0),		/* 11 */
    104 	_RF_S|_RF_A|/*unknown*/	_RF_SZ(32) | _RF_RS(0),		/* 64 */
    105 	_RF_S|_RF_A|/*unknown*/	_RF_SZ(32) | _RF_RS(0),		/* OLO10 */
    106 	_RF_S|_RF_A|/*unknown*/	_RF_SZ(32) | _RF_RS(0),		/* HH22 */
    107 	_RF_S|_RF_A|/*unknown*/	_RF_SZ(32) | _RF_RS(0),		/* HM10 */
    108 	_RF_S|_RF_A|/*unknown*/	_RF_SZ(32) | _RF_RS(0),		/* LM22 */
    109 	_RF_S|_RF_A|_RF_P|/*unknown*/	_RF_SZ(32) | _RF_RS(0),	/* WDISP16 */
    110 	_RF_S|_RF_A|_RF_P|/*unknown*/	_RF_SZ(32) | _RF_RS(0),	/* WDISP19 */
    111 	/*unknown*/		_RF_SZ(32) | _RF_RS(0),		/* GLOB_JMP */
    112 	/*unknown*/		_RF_SZ(32) | _RF_RS(0),		/* 7 */
    113 	/*unknown*/		_RF_SZ(32) | _RF_RS(0),		/* 5 */
    114 	/*unknown*/		_RF_SZ(32) | _RF_RS(0),		/* 6 */
    115 };
    116 
    117 #ifdef RTLD_DEBUG_RELOC
    118 static const char *reloc_names[] = {
    119 	"NONE", "RELOC_8", "RELOC_16", "RELOC_32", "DISP_8",
    120 	"DISP_16", "DISP_32", "WDISP_30", "WDISP_22", "HI22",
    121 	"22", "13", "LO10", "GOT10", "GOT13",
    122 	"GOT22", "PC10", "PC22", "WPLT30", "COPY",
    123 	"GLOB_DAT", "JMP_SLOT", "RELATIVE", "UA_32", "PLT32",
    124 	"HIPLT22", "LOPLT10", "LOPLT10", "PCPLT22", "PCPLT32",
    125 	"10", "11", "64", "OLO10", "HH22",
    126 	"HM10", "LM22", "WDISP16", "WDISP19", "GLOB_JMP",
    127 	"7", "5", "6"
    128 };
    129 #endif
    130 
    131 #define RELOC_RESOLVE_SYMBOL(t)		((reloc_target_flags[t] & _RF_S) != 0)
    132 #define RELOC_PC_RELATIVE(t)		((reloc_target_flags[t] & _RF_P) != 0)
    133 #define RELOC_BASE_RELATIVE(t)		((reloc_target_flags[t] & _RF_B) != 0)
    134 #define RELOC_TARGET_SIZE(t)		((reloc_target_flags[t] >> 8) & 0xff)
    135 #define RELOC_VALUE_RIGHTSHIFT(t)	(reloc_target_flags[t] & 0xff)
    136 
    137 static int reloc_target_bitmask[] = {
    138 #define _BM(x)	(~(-(1ULL << (x))))
    139 	0,				/* NONE */
    140 	_BM(8), _BM(16), _BM(32),	/* RELOC_8, _16, _32 */
    141 	_BM(8), _BM(16), _BM(32),	/* DISP8, DISP16, DISP32 */
    142 	_BM(30), _BM(22),		/* WDISP30, WDISP22 */
    143 	_BM(22), _BM(22),		/* HI22, _22 */
    144 	_BM(13), _BM(10),		/* RELOC_13, _LO10 */
    145 	_BM(10), _BM(13), _BM(22),	/* GOT10, GOT13, GOT22 */
    146 	_BM(10), _BM(22),		/* _PC10, _PC22 */
    147 	_BM(30), 0,			/* _WPLT30, _COPY */
    148 	-1, -1, _BM(22),		/* _GLOB_DAT, JMP_SLOT, _RELATIVE */
    149 	_BM(32), _BM(32),		/* _UA32, PLT32 */
    150 	_BM(22), _BM(10),		/* _HIPLT22, LOPLT10 */
    151 	_BM(32), _BM(22), _BM(10),	/* _PCPLT32, _PCPLT22, _PCPLT10 */
    152 	_BM(10), _BM(11), -1,		/* _10, _11, _64 */
    153 	_BM(10), _BM(22),		/* _OLO10, _HH22 */
    154 	_BM(10), _BM(22),		/* _HM10, _LM22 */
    155 	_BM(16), _BM(19),		/* _WDISP16, _WDISP19 */
    156 	-1,				/* GLOB_JMP */
    157 	_BM(7), _BM(5), _BM(6)		/* _7, _5, _6 */
    158 #undef _BM
    159 };
    160 #define RELOC_VALUE_BITMASK(t)	(reloc_target_bitmask[t])
    161 
    162 int
    163 _rtld_relocate_nonplt_object(
    164 	const Obj_Entry *obj,
    165 	const Elf_RelA *rela,
    166 	bool dodebug)
    167 {
    168 	Elf_Addr *where = (Elf_Addr *) (obj->relocbase + rela->r_offset);
    169 	Elf_Word type, value, mask;
    170 	const Elf_Sym *def = NULL;
    171 	const Obj_Entry *defobj = NULL;
    172 
    173 	type = ELF_R_TYPE(rela->r_info);
    174 	if (type == R_TYPE(NONE))
    175 		return (0);
    176 
    177 	/* We do JMP_SLOTs in relocate_plt_objject() below */
    178 	if (type == R_TYPE(JMP_SLOT))
    179 		return (0);
    180 
    181 	/*
    182 	 * We use the fact that relocation types are an `enum'
    183 	 * Note: R_SPARC_6 is currently numerically largest.
    184 	 */
    185 	if (type > R_TYPE(6))
    186 		return (-1);
    187 
    188 	/*
    189 	 * Handle relative relocs here, because we might not
    190 	 * be able to access globals yet
    191 	 */
    192 	if (!dodebug && type == R_TYPE(RELATIVE)) {
    193 		*where += (Elf_Addr) obj->relocbase;
    194 		return (0);
    195 	}
    196 
    197 	value = rela->r_addend;
    198 	if (RELOC_RESOLVE_SYMBOL(type)) {
    199 
    200 		/* Find the symbol */
    201 		def = _rtld_find_symdef(_rtld_objlist, rela->r_info,
    202 					NULL, obj, &defobj, false);
    203 		if (def == NULL)
    204 			return (-1);
    205 
    206 		/* Add in the symbol's absolute address */
    207 		value += (Elf_Word)(defobj->relocbase + def->st_value);
    208 	}
    209 
    210 	if (RELOC_PC_RELATIVE(type)) {
    211 		value -= (Elf_Word)where;
    212 	}
    213 
    214 	if (RELOC_BASE_RELATIVE(type)) {
    215 		value += (Elf_Word)obj->relocbase;
    216 	}
    217 
    218 	mask = RELOC_VALUE_BITMASK(type);
    219 	value >>= RELOC_VALUE_RIGHTSHIFT(type);
    220 	value &= mask;
    221 
    222 	/* We ignore alignment restrictions here */
    223 	*where &= ~mask;
    224 	*where |= value;
    225 #ifdef RTLD_DEBUG_RELOC
    226 	if (RELOC_RESOLVE_SYMBOL(type)) {
    227 		rdbg(dodebug, "%s %s in %s --> %p %s",
    228 		    reloc_names[type],
    229 		    defobj->strtab + def->st_name, obj->path,
    230 		    (void *)*where, defobj->path);
    231 	}
    232 	else {
    233 		rdbg(dodebug, "%s --> %p", reloc_names[type],
    234 		    (void *)*where);
    235 	}
    236 #endif
    237 	return (0);
    238 }
    239 
    240 int
    241 _rtld_relocate_plt_object(
    242 	const Obj_Entry *obj,
    243 	const Elf_RelA *rela,
    244 	caddr_t *addrp,
    245 	bool bind_now,
    246 	bool dodebug)
    247 {
    248 	const Elf_Sym *def;
    249 	const Obj_Entry *defobj;
    250 	Elf_Addr *where = (Elf_Addr *) (obj->relocbase + rela->r_offset);
    251 	Elf_Addr value;
    252 
    253 	if (bind_now == 0 && obj->pltgot != NULL)
    254 		return (0);
    255 
    256 	/* Fully resolve procedure addresses now */
    257 
    258 	assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT));
    259 
    260 	def = _rtld_find_symdef(_rtld_objlist, rela->r_info,
    261 				NULL, obj, &defobj, true);
    262 	if (def == NULL)
    263 		return (-1);
    264 
    265 	value = (Elf_Addr) (defobj->relocbase + def->st_value);
    266 
    267 	rdbg(dodebug, "bind now %d/fixup in %s --> old=%p new=%p",
    268 	    (int)bind_now, defobj->strtab + def->st_name,
    269 	    (void *)*where, (void *)value);
    270 
    271 	/*
    272 	 * At the PLT entry pointed at by `where', we now construct
    273 	 * a direct transfer to the now fully resolved function
    274 	 * address.  The resulting code in the jump slot is:
    275 	 *
    276 	 *	sethi	%hi(addr), %g1
    277 	 *	jmp	%g1+%lo(addr)
    278 	 *	nop	! delay slot
    279 	 */
    280 #define SETHI	0x03000000
    281 #define JMP	0x81c06000
    282 #define NOP	0x01000000
    283 	where[0] = SETHI | ((value >> 10) & 0x003fffff);
    284 	where[1] = JMP   | (value & 0x000003ff);
    285 	where[2] = NOP;
    286 
    287 	if (addrp != NULL)
    288 		*addrp = (caddr_t)value;
    289 
    290 	return (0);
    291 }
    292