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