Home | History | Annotate | Line # | Download | only in sparc64
mdreloc.c revision 1.68
      1  1.68     joerg /*	$NetBSD: mdreloc.c,v 1.68 2018/03/29 13:23:39 joerg Exp $	*/
      2   1.1       eeh 
      3   1.1       eeh /*-
      4   1.1       eeh  * Copyright (c) 2000 Eduardo Horvath.
      5  1.23   mycroft  * Copyright (c) 1999, 2002 The NetBSD Foundation, Inc.
      6   1.1       eeh  * All rights reserved.
      7   1.1       eeh  *
      8   1.1       eeh  * This code is derived from software contributed to The NetBSD Foundation
      9  1.27   mycroft  * by Paul Kranenburg and by Charles M. Hannum.
     10   1.1       eeh  *
     11   1.1       eeh  * Redistribution and use in source and binary forms, with or without
     12   1.1       eeh  * modification, are permitted provided that the following conditions
     13   1.1       eeh  * are met:
     14   1.1       eeh  * 1. Redistributions of source code must retain the above copyright
     15   1.1       eeh  *    notice, this list of conditions and the following disclaimer.
     16   1.1       eeh  * 2. Redistributions in binary form must reproduce the above copyright
     17   1.1       eeh  *    notice, this list of conditions and the following disclaimer in the
     18   1.1       eeh  *    documentation and/or other materials provided with the distribution.
     19   1.1       eeh  *
     20   1.1       eeh  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21   1.1       eeh  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22   1.1       eeh  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23   1.1       eeh  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24   1.1       eeh  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25   1.1       eeh  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26   1.1       eeh  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27   1.1       eeh  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28   1.1       eeh  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29   1.1       eeh  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30   1.1       eeh  * POSSIBILITY OF SUCH DAMAGE.
     31   1.1       eeh  */
     32   1.1       eeh 
     33  1.37     skrll #include <sys/cdefs.h>
     34  1.37     skrll #ifndef lint
     35  1.68     joerg __RCSID("$NetBSD: mdreloc.c,v 1.68 2018/03/29 13:23:39 joerg Exp $");
     36  1.37     skrll #endif /* not lint */
     37  1.37     skrll 
     38  1.68     joerg #include <machine/elf_support.h>
     39  1.68     joerg 
     40   1.1       eeh #include <errno.h>
     41   1.1       eeh #include <stdio.h>
     42   1.1       eeh #include <stdlib.h>
     43   1.1       eeh #include <string.h>
     44   1.1       eeh #include <unistd.h>
     45   1.1       eeh 
     46   1.1       eeh #include "rtldenv.h"
     47   1.1       eeh #include "debug.h"
     48   1.1       eeh #include "rtld.h"
     49   1.1       eeh 
     50   1.1       eeh /*
     51   1.1       eeh  * The following table holds for each relocation type:
     52   1.1       eeh  *	- the width in bits of the memory location the relocation
     53   1.1       eeh  *	  applies to (not currently used)
     54   1.1       eeh  *	- the number of bits the relocation value must be shifted to the
     55   1.1       eeh  *	  right (i.e. discard least significant bits) to fit into
     56   1.1       eeh  *	  the appropriate field in the instruction word.
     57   1.1       eeh  *	- flags indicating whether
     58   1.1       eeh  *		* the relocation involves a symbol
     59   1.1       eeh  *		* the relocation is relative to the current position
     60   1.1       eeh  *		* the relocation is for a GOT entry
     61   1.1       eeh  *		* the relocation is relative to the load address
     62   1.1       eeh  *
     63   1.1       eeh  */
     64   1.1       eeh #define _RF_S		0x80000000		/* Resolve symbol */
     65   1.1       eeh #define _RF_A		0x40000000		/* Use addend */
     66   1.1       eeh #define _RF_P		0x20000000		/* Location relative */
     67   1.1       eeh #define _RF_G		0x10000000		/* GOT offset */
     68   1.1       eeh #define _RF_B		0x08000000		/* Load address relative */
     69   1.2       eeh #define _RF_U		0x04000000		/* Unaligned */
     70   1.1       eeh #define _RF_SZ(s)	(((s) & 0xff) << 8)	/* memory target size */
     71   1.1       eeh #define _RF_RS(s)	( (s) & 0xff)		/* right shift */
     72  1.52    martin static const int reloc_target_flags[R_TYPE(TLS_TPOFF64)+1] = {
     73   1.1       eeh 	0,							/* NONE */
     74   1.1       eeh 	_RF_S|_RF_A|		_RF_SZ(8)  | _RF_RS(0),		/* RELOC_8 */
     75   1.1       eeh 	_RF_S|_RF_A|		_RF_SZ(16) | _RF_RS(0),		/* RELOC_16 */
     76   1.1       eeh 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* RELOC_32 */
     77   1.1       eeh 	_RF_S|_RF_A|_RF_P|	_RF_SZ(8)  | _RF_RS(0),		/* DISP_8 */
     78   1.1       eeh 	_RF_S|_RF_A|_RF_P|	_RF_SZ(16) | _RF_RS(0),		/* DISP_16 */
     79   1.1       eeh 	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(0),		/* DISP_32 */
     80   1.1       eeh 	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(2),		/* WDISP_30 */
     81   1.1       eeh 	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(2),		/* WDISP_22 */
     82   1.1       eeh 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(10),	/* HI22 */
     83   1.1       eeh 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* 22 */
     84   1.1       eeh 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* 13 */
     85   1.1       eeh 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* LO10 */
     86   1.1       eeh 	_RF_G|			_RF_SZ(32) | _RF_RS(0),		/* GOT10 */
     87   1.1       eeh 	_RF_G|			_RF_SZ(32) | _RF_RS(0),		/* GOT13 */
     88   1.1       eeh 	_RF_G|			_RF_SZ(32) | _RF_RS(10),	/* GOT22 */
     89   1.1       eeh 	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(0),		/* PC10 */
     90   1.1       eeh 	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(10),	/* PC22 */
     91   1.1       eeh 	      _RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(2),		/* WPLT30 */
     92   1.1       eeh 				_RF_SZ(32) | _RF_RS(0),		/* COPY */
     93   1.1       eeh 	_RF_S|_RF_A|		_RF_SZ(64) | _RF_RS(0),		/* GLOB_DAT */
     94   1.1       eeh 				_RF_SZ(32) | _RF_RS(0),		/* JMP_SLOT */
     95   1.1       eeh 	      _RF_A|	_RF_B|	_RF_SZ(64) | _RF_RS(0),		/* RELATIVE */
     96   1.2       eeh 	_RF_S|_RF_A|	_RF_U|	_RF_SZ(32) | _RF_RS(0),		/* UA_32 */
     97   1.1       eeh 
     98   1.1       eeh 	      _RF_A|		_RF_SZ(32) | _RF_RS(0),		/* PLT32 */
     99   1.1       eeh 	      _RF_A|		_RF_SZ(32) | _RF_RS(10),	/* HIPLT22 */
    100   1.1       eeh 	      _RF_A|		_RF_SZ(32) | _RF_RS(0),		/* LOPLT10 */
    101   1.1       eeh 	      _RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(0),		/* PCPLT32 */
    102   1.1       eeh 	      _RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(10),	/* PCPLT22 */
    103   1.1       eeh 	      _RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(0),		/* PCPLT10 */
    104   1.1       eeh 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* 10 */
    105   1.1       eeh 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* 11 */
    106   1.1       eeh 	_RF_S|_RF_A|		_RF_SZ(64) | _RF_RS(0),		/* 64 */
    107   1.1       eeh 	_RF_S|_RF_A|/*extra*/	_RF_SZ(32) | _RF_RS(0),		/* OLO10 */
    108   1.1       eeh 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(42),	/* HH22 */
    109   1.1       eeh 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(32),	/* HM10 */
    110   1.1       eeh 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(10),	/* LM22 */
    111   1.1       eeh 	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(42),	/* PC_HH22 */
    112   1.1       eeh 	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(32),	/* PC_HM10 */
    113   1.1       eeh 	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(10),	/* PC_LM22 */
    114   1.1       eeh 	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(2),		/* WDISP16 */
    115   1.1       eeh 	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(2),		/* WDISP19 */
    116   1.1       eeh 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* GLOB_JMP */
    117   1.1       eeh 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* 7 */
    118   1.1       eeh 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* 5 */
    119   1.1       eeh 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* 6 */
    120   1.1       eeh 	_RF_S|_RF_A|_RF_P|	_RF_SZ(64) | _RF_RS(0),		/* DISP64 */
    121   1.1       eeh 	      _RF_A|		_RF_SZ(64) | _RF_RS(0),		/* PLT64 */
    122   1.1       eeh 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(10),	/* HIX22 */
    123   1.1       eeh 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* LOX10 */
    124   1.1       eeh 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(22),	/* H44 */
    125   1.1       eeh 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(12),	/* M44 */
    126   1.1       eeh 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* L44 */
    127   1.1       eeh 	_RF_S|_RF_A|		_RF_SZ(64) | _RF_RS(0),		/* REGISTER */
    128   1.2       eeh 	_RF_S|_RF_A|	_RF_U|	_RF_SZ(64) | _RF_RS(0),		/* UA64 */
    129   1.2       eeh 	_RF_S|_RF_A|	_RF_U|	_RF_SZ(16) | _RF_RS(0),		/* UA16 */
    130  1.52    martin /* TLS relocs not represented here! */
    131   1.1       eeh };
    132   1.1       eeh 
    133   1.1       eeh #ifdef RTLD_DEBUG_RELOC
    134   1.1       eeh static const char *reloc_names[] = {
    135   1.1       eeh 	"NONE", "RELOC_8", "RELOC_16", "RELOC_32", "DISP_8",
    136   1.1       eeh 	"DISP_16", "DISP_32", "WDISP_30", "WDISP_22", "HI22",
    137   1.1       eeh 	"22", "13", "LO10", "GOT10", "GOT13",
    138   1.1       eeh 	"GOT22", "PC10", "PC22", "WPLT30", "COPY",
    139   1.1       eeh 	"GLOB_DAT", "JMP_SLOT", "RELATIVE", "UA_32", "PLT32",
    140   1.1       eeh 	"HIPLT22", "LOPLT10", "LOPLT10", "PCPLT22", "PCPLT32",
    141   1.1       eeh 	"10", "11", "64", "OLO10", "HH22",
    142   1.1       eeh 	"HM10", "LM22", "PC_HH22", "PC_HM10", "PC_LM22",
    143   1.1       eeh 	"WDISP16", "WDISP19", "GLOB_JMP", "7", "5", "6",
    144   1.1       eeh 	"DISP64", "PLT64", "HIX22", "LOX10", "H44", "M44",
    145  1.52    martin 	"L44", "REGISTER", "UA64", "UA16",
    146  1.52    martin 	"TLS_GD_HI22", "TLS_GD_LO10", "TLS_GD_ADD", "TLS_GD_CALL",
    147  1.52    martin 	"TLS_LDM_HI22", "TLS_LDM_LO10", "TLS_LDM_ADD", "TLS_LDM_CALL",
    148  1.52    martin 	"TLS_LDO_HIX22", "TLS_LDO_LOX10", "TLS_LDO_ADD", "TLS_IE_HI22",
    149  1.52    martin 	"TLS_IE_LO10", "TLS_IE_LD", "TLS_IE_LDX", "TLS_IE_ADD", "TLS_LE_HIX22",
    150  1.52    martin 	"TLS_LE_LOX10", "TLS_DTPMOD32", "TLS_DTPMOD64", "TLS_DTPOFF32",
    151  1.52    martin 	"TLS_DTPOFF64", "TLS_TPOFF32", "TLS_TPOFF64",
    152   1.1       eeh };
    153   1.1       eeh #endif
    154   1.1       eeh 
    155   1.1       eeh #define RELOC_RESOLVE_SYMBOL(t)		((reloc_target_flags[t] & _RF_S) != 0)
    156   1.1       eeh #define RELOC_PC_RELATIVE(t)		((reloc_target_flags[t] & _RF_P) != 0)
    157   1.1       eeh #define RELOC_BASE_RELATIVE(t)		((reloc_target_flags[t] & _RF_B) != 0)
    158   1.2       eeh #define RELOC_UNALIGNED(t)		((reloc_target_flags[t] & _RF_U) != 0)
    159   1.2       eeh #define RELOC_USE_ADDEND(t)		((reloc_target_flags[t] & _RF_A) != 0)
    160   1.1       eeh #define RELOC_TARGET_SIZE(t)		((reloc_target_flags[t] >> 8) & 0xff)
    161   1.1       eeh #define RELOC_VALUE_RIGHTSHIFT(t)	(reloc_target_flags[t] & 0xff)
    162  1.52    martin #define RELOC_TLS(t)			(t >= R_TYPE(TLS_GD_HI22))
    163   1.1       eeh 
    164  1.16   mycroft static const long reloc_target_bitmask[] = {
    165   1.1       eeh #define _BM(x)	(~(-(1ULL << (x))))
    166   1.1       eeh 	0,				/* NONE */
    167   1.1       eeh 	_BM(8), _BM(16), _BM(32),	/* RELOC_8, _16, _32 */
    168   1.1       eeh 	_BM(8), _BM(16), _BM(32),	/* DISP8, DISP16, DISP32 */
    169   1.1       eeh 	_BM(30), _BM(22),		/* WDISP30, WDISP22 */
    170   1.1       eeh 	_BM(22), _BM(22),		/* HI22, _22 */
    171   1.1       eeh 	_BM(13), _BM(10),		/* RELOC_13, _LO10 */
    172   1.1       eeh 	_BM(10), _BM(13), _BM(22),	/* GOT10, GOT13, GOT22 */
    173   1.1       eeh 	_BM(10), _BM(22),		/* _PC10, _PC22 */
    174   1.1       eeh 	_BM(30), 0,			/* _WPLT30, _COPY */
    175  1.56    martin 	-1, _BM(32), -1,		/* _GLOB_DAT, JMP_SLOT, _RELATIVE */
    176   1.1       eeh 	_BM(32), _BM(32),		/* _UA32, PLT32 */
    177   1.1       eeh 	_BM(22), _BM(10),		/* _HIPLT22, LOPLT10 */
    178   1.1       eeh 	_BM(32), _BM(22), _BM(10),	/* _PCPLT32, _PCPLT22, _PCPLT10 */
    179   1.1       eeh 	_BM(10), _BM(11), -1,		/* _10, _11, _64 */
    180  1.59    martin 	_BM(13), _BM(22),		/* _OLO10, _HH22 */
    181   1.1       eeh 	_BM(10), _BM(22),		/* _HM10, _LM22 */
    182   1.1       eeh 	_BM(22), _BM(10), _BM(22),	/* _PC_HH22, _PC_HM10, _PC_LM22 */
    183   1.1       eeh 	_BM(16), _BM(19),		/* _WDISP16, _WDISP19 */
    184   1.1       eeh 	-1,				/* GLOB_JMP */
    185  1.54    martin 	_BM(7), _BM(5), _BM(6),		/* _7, _5, _6 */
    186   1.1       eeh 	-1, -1,				/* DISP64, PLT64 */
    187   1.1       eeh 	_BM(22), _BM(13),		/* HIX22, LOX10 */
    188  1.55    martin 	_BM(22), _BM(10), _BM(12),	/* H44, M44, L44 */
    189   1.1       eeh 	-1, -1, _BM(16),		/* REGISTER, UA64, UA16 */
    190   1.1       eeh #undef _BM
    191   1.1       eeh };
    192   1.1       eeh #define RELOC_VALUE_BITMASK(t)	(reloc_target_bitmask[t])
    193   1.1       eeh 
    194   1.1       eeh /*
    195   1.1       eeh  * Instruction templates:
    196   1.1       eeh  */
    197   1.1       eeh 
    198   1.1       eeh 
    199  1.26   mycroft /* %hi(v)/%lo(v) with variable shift */
    200  1.26   mycroft #define	HIVAL(v, s)	(((v) >> (s)) & 0x003fffff)
    201  1.26   mycroft #define LOVAL(v, s)	(((v) >> (s)) & 0x000003ff)
    202   1.1       eeh 
    203  1.20   mycroft void _rtld_bind_start_0(long, long);
    204  1.20   mycroft void _rtld_bind_start_1(long, long);
    205  1.18   mycroft void _rtld_relocate_nonplt_self(Elf_Dyn *, Elf_Addr);
    206  1.34     skrll caddr_t _rtld_bind(const Obj_Entry *, Elf_Word);
    207   1.1       eeh 
    208   1.1       eeh /*
    209   1.1       eeh  * Install rtld function call into this PLT slot.
    210   1.1       eeh  */
    211  1.29   mycroft #define	SAVE		0x9de3bf50	/* i.e. `save %sp,-176,%sp' */
    212   1.1       eeh #define	SETHI_l0	0x21000000
    213   1.1       eeh #define	SETHI_l1	0x23000000
    214   1.1       eeh #define	OR_l0_l0	0xa0142000
    215   1.1       eeh #define	SLLX_l0_32_l0	0xa12c3020
    216   1.1       eeh #define	OR_l0_l1_l0	0xa0140011
    217  1.26   mycroft #define	JMPL_l0_o0	0x91c42000
    218  1.26   mycroft #define	MOV_g1_o1	0x92100001
    219   1.1       eeh 
    220  1.36     skrll void _rtld_install_plt(Elf_Word *, Elf_Addr);
    221  1.36     skrll static inline int _rtld_relocate_plt_object(const Obj_Entry *,
    222  1.36     skrll     const Elf_Rela *, Elf_Addr *);
    223   1.1       eeh 
    224   1.1       eeh void
    225  1.34     skrll _rtld_install_plt(Elf_Word *pltgot, Elf_Addr proc)
    226   1.1       eeh {
    227   1.1       eeh 	pltgot[0] = SAVE;
    228   1.1       eeh 	pltgot[1] = SETHI_l0  | HIVAL(proc, 42);
    229   1.1       eeh 	pltgot[2] = SETHI_l1  | HIVAL(proc, 10);
    230  1.26   mycroft 	pltgot[3] = OR_l0_l0  | LOVAL(proc, 32);
    231   1.1       eeh 	pltgot[4] = SLLX_l0_32_l0;
    232   1.1       eeh 	pltgot[5] = OR_l0_l1_l0;
    233  1.26   mycroft 	pltgot[6] = JMPL_l0_o0 | LOVAL(proc, 0);
    234  1.26   mycroft 	pltgot[7] = MOV_g1_o1;
    235   1.1       eeh }
    236   1.2       eeh 
    237   1.6   mycroft void
    238   1.6   mycroft _rtld_setup_pltgot(const Obj_Entry *obj)
    239   1.6   mycroft {
    240   1.6   mycroft 	/*
    241   1.6   mycroft 	 * On sparc64 we got troubles.
    242   1.6   mycroft 	 *
    243   1.6   mycroft 	 * Instructions are 4 bytes long.
    244   1.6   mycroft 	 * Elf[64]_Addr is 8 bytes long, so are our pltglot[]
    245   1.6   mycroft 	 * array entries.
    246   1.6   mycroft 	 * Each PLT entry jumps to PLT0 to enter the dynamic
    247   1.6   mycroft 	 * linker.
    248   1.6   mycroft 	 * Loading an arbitrary 64-bit pointer takes 6
    249   1.6   mycroft 	 * instructions and 2 registers.
    250   1.6   mycroft 	 *
    251   1.6   mycroft 	 * Somehow we need to issue a save to get a new stack
    252   1.6   mycroft 	 * frame, load the address of the dynamic linker, and
    253   1.6   mycroft 	 * jump there, in 8 instructions or less.
    254   1.6   mycroft 	 *
    255   1.6   mycroft 	 * Oh, we need to fill out both PLT0 and PLT1.
    256   1.6   mycroft 	 */
    257   1.6   mycroft 	{
    258   1.6   mycroft 		Elf_Word *entry = (Elf_Word *)obj->pltgot;
    259   1.6   mycroft 
    260   1.6   mycroft 		/* Install in entries 0 and 1 */
    261   1.6   mycroft 		_rtld_install_plt(&entry[0], (Elf_Addr) &_rtld_bind_start_0);
    262   1.6   mycroft 		_rtld_install_plt(&entry[8], (Elf_Addr) &_rtld_bind_start_1);
    263   1.6   mycroft 
    264   1.6   mycroft 		/*
    265   1.6   mycroft 		 * Install the object reference in first slot
    266   1.6   mycroft 		 * of entry 2.
    267   1.6   mycroft 		 */
    268   1.6   mycroft 		obj->pltgot[8] = (Elf_Addr) obj;
    269   1.6   mycroft 	}
    270   1.8   mycroft }
    271   1.8   mycroft 
    272  1.18   mycroft void
    273  1.34     skrll _rtld_relocate_nonplt_self(Elf_Dyn *dynp, Elf_Addr relocbase)
    274  1.18   mycroft {
    275  1.18   mycroft 	const Elf_Rela *rela = 0, *relalim;
    276  1.18   mycroft 	Elf_Addr relasz = 0;
    277  1.18   mycroft 	Elf_Addr *where;
    278  1.18   mycroft 
    279  1.18   mycroft 	for (; dynp->d_tag != DT_NULL; dynp++) {
    280  1.18   mycroft 		switch (dynp->d_tag) {
    281  1.18   mycroft 		case DT_RELA:
    282  1.18   mycroft 			rela = (const Elf_Rela *)(relocbase + dynp->d_un.d_ptr);
    283  1.18   mycroft 			break;
    284  1.18   mycroft 		case DT_RELASZ:
    285  1.18   mycroft 			relasz = dynp->d_un.d_val;
    286  1.18   mycroft 			break;
    287  1.18   mycroft 		}
    288  1.18   mycroft 	}
    289  1.44     lukem 	relalim = (const Elf_Rela *)((const uint8_t *)rela + relasz);
    290  1.18   mycroft 	for (; rela < relalim; rela++) {
    291  1.18   mycroft 		where = (Elf_Addr *)(relocbase + rela->r_offset);
    292  1.18   mycroft 		*where = (Elf_Addr)(relocbase + rela->r_addend);
    293  1.18   mycroft 	}
    294  1.18   mycroft }
    295  1.18   mycroft 
    296   1.8   mycroft int
    297  1.47     joerg _rtld_relocate_nonplt_objects(Obj_Entry *obj)
    298   1.8   mycroft {
    299   1.9   mycroft 	const Elf_Rela *rela;
    300  1.40    martin 	const Elf_Sym *def = NULL;
    301  1.40    martin 	const Obj_Entry *defobj = NULL;
    302  1.61     joerg 	unsigned long last_symnum = ULONG_MAX;
    303  1.18   mycroft 
    304   1.9   mycroft 	for (rela = obj->rela; rela < obj->relalim; rela++) {
    305   1.9   mycroft 		Elf_Addr *where;
    306   1.9   mycroft 		Elf_Word type;
    307   1.9   mycroft 		Elf_Addr value = 0, mask;
    308  1.61     joerg 		unsigned long symnum;
    309   1.9   mycroft 
    310   1.9   mycroft 		where = (Elf_Addr *) (obj->relocbase + rela->r_offset);
    311   1.9   mycroft 
    312   1.9   mycroft 		type = ELF_R_TYPE(rela->r_info);
    313   1.9   mycroft 		if (type == R_TYPE(NONE))
    314  1.12   mycroft 			continue;
    315   1.9   mycroft 
    316  1.53    martin 		/* OLO10 relocations have extra info */
    317  1.53    martin 		if ((type & 0x00ff) == R_SPARC_OLO10)
    318  1.53    martin 			type = R_SPARC_OLO10;
    319  1.53    martin 
    320  1.23   mycroft 		/* We do JMP_SLOTs in _rtld_bind() below */
    321   1.9   mycroft 		if (type == R_TYPE(JMP_SLOT))
    322  1.12   mycroft 			continue;
    323   1.9   mycroft 
    324  1.65     joerg 		/* IFUNC relocations are handled in _rtld_call_ifunc */
    325  1.65     joerg 		if (type == R_TYPE(IRELATIVE)) {
    326  1.65     joerg 			if (obj->ifunc_remaining_nonplt == 0)
    327  1.65     joerg 				obj->ifunc_remaining_nonplt = rela - obj->rela + 1;
    328  1.65     joerg 			continue;
    329  1.65     joerg 		}
    330  1.65     joerg 
    331   1.9   mycroft 		/* COPY relocs are also handled elsewhere */
    332   1.9   mycroft 		if (type == R_TYPE(COPY))
    333  1.12   mycroft 			continue;
    334   1.8   mycroft 
    335   1.9   mycroft 		/*
    336   1.9   mycroft 		 * We use the fact that relocation types are an `enum'
    337  1.52    martin 		 * Note: R_SPARC_TLS_TPOFF64 is currently numerically largest.
    338   1.9   mycroft 		 */
    339  1.53    martin 		if (type > R_TYPE(TLS_TPOFF64)) {
    340  1.53    martin 			dbg(("unknown relocation type %x at %p", type, rela));
    341  1.53    martin 			return -1;
    342  1.53    martin 		}
    343   1.8   mycroft 
    344   1.9   mycroft 		value = rela->r_addend;
    345   1.8   mycroft 
    346  1.61     joerg 		if (RELOC_RESOLVE_SYMBOL(type) || RELOC_TLS(type)) {
    347  1.61     joerg 			symnum = ELF_R_SYM(rela->r_info);
    348  1.61     joerg 			if (last_symnum != symnum) {
    349  1.61     joerg 				last_symnum = symnum;
    350  1.61     joerg 				def = _rtld_find_symdef(symnum, obj, &defobj,
    351  1.61     joerg 				    false);
    352  1.61     joerg 				if (def == NULL)
    353  1.61     joerg 					return -1;
    354  1.61     joerg 			}
    355  1.61     joerg 		}
    356  1.61     joerg 
    357   1.9   mycroft 		/*
    358  1.52    martin 		 * Handle TLS relocations here, they are different.
    359  1.52    martin 		 */
    360  1.52    martin 		if (RELOC_TLS(type)) {
    361  1.52    martin 			switch (type) {
    362  1.60     joerg 			case R_TYPE(TLS_DTPMOD64):
    363  1.60     joerg 				*where = (Elf64_Addr)defobj->tlsindex;
    364  1.60     joerg 
    365  1.60     joerg 				rdbg(("TLS_DTPMOD64 %s in %s --> %p",
    366  1.60     joerg 				    obj->strtab +
    367  1.60     joerg 				    obj->symtab[symnum].st_name,
    368  1.60     joerg 				    obj->path, (void *)*where));
    369  1.60     joerg 
    370  1.60     joerg 				break;
    371  1.60     joerg 
    372  1.60     joerg 			case R_TYPE(TLS_DTPOFF64):
    373  1.60     joerg 				*where = (Elf64_Addr)(def->st_value
    374  1.60     joerg 				    + rela->r_addend);
    375  1.60     joerg 
    376  1.60     joerg 				rdbg(("DTPOFF64 %s in %s --> %p",
    377  1.60     joerg 				    obj->strtab +
    378  1.60     joerg 				        obj->symtab[symnum].st_name,
    379  1.60     joerg 				    obj->path, (void *)*where));
    380  1.60     joerg 
    381  1.60     joerg 				break;
    382  1.60     joerg 
    383  1.60     joerg 			case R_TYPE(TLS_TPOFF64):
    384  1.60     joerg 				if (!defobj->tls_done &&
    385  1.60     joerg 					_rtld_tls_offset_allocate(obj))
    386  1.60     joerg 					     return -1;
    387  1.60     joerg 
    388  1.60     joerg 				*where = (Elf64_Addr)(def->st_value -
    389  1.60     joerg 				    defobj->tlsoffset + rela->r_addend);
    390  1.60     joerg 
    391  1.60     joerg 				rdbg(("TLS_TPOFF64 %s in %s --> %p",
    392  1.60     joerg 				    obj->strtab + obj->symtab[symnum].st_name,
    393  1.60     joerg 				    obj->path, (void *)*where));
    394  1.52    martin 
    395  1.60     joerg 				break;
    396  1.52    martin 			}
    397  1.52    martin 			continue;
    398  1.52    martin 		}
    399  1.52    martin 
    400  1.52    martin 		/*
    401  1.18   mycroft 		 * Handle relative relocs here, as an optimization.
    402   1.9   mycroft 		 */
    403  1.17   mycroft 		if (type == R_TYPE(RELATIVE)) {
    404   1.9   mycroft 			*where = (Elf_Addr)(obj->relocbase + value);
    405  1.21   mycroft 			rdbg(("RELATIVE in %s --> %p", obj->path,
    406  1.18   mycroft 			    (void *)*where));
    407  1.12   mycroft 			continue;
    408   1.9   mycroft 		}
    409   1.8   mycroft 
    410   1.9   mycroft 		if (RELOC_RESOLVE_SYMBOL(type)) {
    411   1.9   mycroft 			/* Add in the symbol's absolute address */
    412   1.9   mycroft 			value += (Elf_Addr)(defobj->relocbase + def->st_value);
    413   1.9   mycroft 		}
    414   1.8   mycroft 
    415  1.53    martin 		if (type == R_SPARC_OLO10) {
    416  1.53    martin 			value = (value & 0x3ff)
    417  1.53    martin 			    + (((Elf64_Xword)rela->r_info<<32)>>40);
    418  1.53    martin 		}
    419  1.53    martin 
    420   1.9   mycroft 		if (RELOC_PC_RELATIVE(type)) {
    421   1.9   mycroft 			value -= (Elf_Addr)where;
    422   1.9   mycroft 		}
    423   1.8   mycroft 
    424   1.9   mycroft 		if (RELOC_BASE_RELATIVE(type)) {
    425   1.9   mycroft 			/*
    426   1.9   mycroft 			 * Note that even though sparcs use `Elf_rela'
    427   1.9   mycroft 			 * exclusively we still need the implicit memory addend
    428   1.9   mycroft 			 * in relocations referring to GOT entries.
    429   1.9   mycroft 			 * Undoubtedly, someone f*cked this up in the distant
    430   1.9   mycroft 			 * past, and now we're stuck with it in the name of
    431   1.9   mycroft 			 * compatibility for all eternity..
    432   1.9   mycroft 			 *
    433   1.9   mycroft 			 * In any case, the implicit and explicit should be
    434   1.9   mycroft 			 * mutually exclusive. We provide a check for that
    435   1.9   mycroft 			 * here.
    436   1.9   mycroft 			 */
    437   1.8   mycroft #ifdef DIAGNOSTIC
    438   1.9   mycroft 			if (value != 0 && *where != 0) {
    439   1.9   mycroft 				xprintf("BASE_REL(%s): where=%p, *where 0x%lx, "
    440   1.9   mycroft 					"addend=0x%lx, base %p\n",
    441   1.9   mycroft 					obj->path, where, *where,
    442   1.9   mycroft 					rela->r_addend, obj->relocbase);
    443   1.9   mycroft 			}
    444   1.9   mycroft #endif
    445   1.9   mycroft 			/* XXXX -- apparently we ignore the preexisting value */
    446   1.9   mycroft 			value += (Elf_Addr)(obj->relocbase);
    447   1.8   mycroft 		}
    448   1.8   mycroft 
    449   1.9   mycroft 		mask = RELOC_VALUE_BITMASK(type);
    450   1.9   mycroft 		value >>= RELOC_VALUE_RIGHTSHIFT(type);
    451   1.9   mycroft 		value &= mask;
    452   1.9   mycroft 
    453   1.9   mycroft 		if (RELOC_UNALIGNED(type)) {
    454   1.9   mycroft 			/* Handle unaligned relocations. */
    455   1.9   mycroft 			Elf_Addr tmp = 0;
    456   1.9   mycroft 			char *ptr = (char *)where;
    457   1.9   mycroft 			int i, size = RELOC_TARGET_SIZE(type)/8;
    458   1.9   mycroft 
    459   1.9   mycroft 			/* Read it in one byte at a time. */
    460   1.9   mycroft 			for (i=0; i<size; i++)
    461   1.9   mycroft 				tmp = (tmp << 8) | ptr[i];
    462   1.9   mycroft 
    463   1.9   mycroft 			tmp &= ~mask;
    464   1.9   mycroft 			tmp |= value;
    465   1.9   mycroft 
    466   1.9   mycroft 			/* Write it back out. */
    467   1.9   mycroft 			for (i=0; i<size; i++)
    468   1.9   mycroft 				ptr[i] = ((tmp >> (8*i)) & 0xff);
    469   1.8   mycroft #ifdef RTLD_DEBUG_RELOC
    470   1.9   mycroft 			value = (Elf_Addr)tmp;
    471   1.8   mycroft #endif
    472   1.8   mycroft 
    473   1.9   mycroft 		} else if (RELOC_TARGET_SIZE(type) > 32) {
    474   1.9   mycroft 			*where &= ~mask;
    475   1.9   mycroft 			*where |= value;
    476   1.8   mycroft #ifdef RTLD_DEBUG_RELOC
    477   1.9   mycroft 			value = (Elf_Addr)*where;
    478   1.8   mycroft #endif
    479   1.9   mycroft 		} else {
    480   1.9   mycroft 			Elf32_Addr *where32 = (Elf32_Addr *)where;
    481   1.8   mycroft 
    482   1.9   mycroft 			*where32 &= ~mask;
    483   1.9   mycroft 			*where32 |= value;
    484   1.8   mycroft #ifdef RTLD_DEBUG_RELOC
    485   1.9   mycroft 			value = (Elf_Addr)*where32;
    486   1.8   mycroft #endif
    487   1.9   mycroft 		}
    488   1.8   mycroft 
    489   1.8   mycroft #ifdef RTLD_DEBUG_RELOC
    490   1.9   mycroft 		if (RELOC_RESOLVE_SYMBOL(type)) {
    491  1.21   mycroft 			rdbg(("%s %s in %s --> %p in %s", reloc_names[type],
    492  1.11   mycroft 			    obj->strtab + obj->symtab[symnum].st_name,
    493  1.33    petrov 			    obj->path, (void *)value, defobj->path));
    494  1.11   mycroft 		} else {
    495  1.21   mycroft 			rdbg(("%s in %s --> %p", reloc_names[type],
    496  1.33    petrov 			    obj->path, (void *)value));
    497   1.9   mycroft 		}
    498   1.9   mycroft #endif
    499   1.8   mycroft 	}
    500  1.13   mycroft 	return (0);
    501  1.13   mycroft }
    502  1.13   mycroft 
    503  1.13   mycroft int
    504  1.64     joerg _rtld_relocate_plt_lazy(Obj_Entry *obj)
    505  1.13   mycroft {
    506  1.65     joerg 	const Elf_Rela *rela;
    507  1.65     joerg 
    508  1.65     joerg 	for (rela = obj->pltrelalim; rela-- > obj->pltrela; ) {
    509  1.65     joerg 		if (ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_IREL))
    510  1.65     joerg 			obj->ifunc_remaining = obj->pltrelalim - rela + 1;
    511  1.65     joerg 	}
    512  1.65     joerg 
    513  1.65     joerg 	return 0;
    514  1.23   mycroft }
    515  1.23   mycroft 
    516  1.23   mycroft caddr_t
    517  1.34     skrll _rtld_bind(const Obj_Entry *obj, Elf_Word reloff)
    518  1.23   mycroft {
    519  1.24   mycroft 	const Elf_Rela *rela = obj->pltrela + reloff;
    520  1.35    martin 	Elf_Addr result;
    521  1.35    martin 	int err;
    522  1.23   mycroft 
    523  1.39       mrg 	result = 0;	/* XXX gcc */
    524  1.39       mrg 
    525  1.67     joerg 	if (ELF_R_TYPE(obj->pltrela->r_info) == R_TYPE(JMP_SLOT) ||
    526  1.67     joerg 	    ELF_R_TYPE(obj->pltrela->r_info) == R_TYPE(JMP_IREL)) {
    527  1.23   mycroft 		/*
    528  1.23   mycroft 		 * XXXX
    529  1.23   mycroft 		 *
    530  1.23   mycroft 		 * The first four PLT entries are reserved.  There is some
    531  1.23   mycroft 		 * disagreement whether they should have associated relocation
    532  1.23   mycroft 		 * entries.  Both the SPARC 32-bit and 64-bit ELF
    533  1.23   mycroft 		 * specifications say that they should have relocation entries,
    534  1.23   mycroft 		 * but the 32-bit SPARC binutils do not generate them, and now
    535  1.23   mycroft 		 * the 64-bit SPARC binutils have stopped generating them too.
    536  1.23   mycroft 		 *
    537  1.23   mycroft 		 * So, to provide binary compatibility, we will check the first
    538  1.23   mycroft 		 * entry, if it is reserved it should not be of the type
    539  1.67     joerg 		 * JMP_SLOT or JMP_REL.  If it is either of those, then
    540  1.67     joerg 		 * the 4 reserved entries were not generated and our index
    541  1.67     joerg 		 * is 4 entries too far.
    542  1.23   mycroft 		 */
    543  1.23   mycroft 		rela -= 4;
    544  1.23   mycroft 	}
    545  1.32   thorpej 
    546  1.51     joerg 	_rtld_shared_enter();
    547  1.35    martin 	err = _rtld_relocate_plt_object(obj, rela, &result);
    548  1.46  christos 	if (err)
    549  1.35    martin 		_rtld_die();
    550  1.51     joerg 	_rtld_shared_exit();
    551  1.35    martin 
    552  1.35    martin 	return (caddr_t)result;
    553  1.35    martin }
    554  1.35    martin 
    555  1.35    martin int
    556  1.35    martin _rtld_relocate_plt_objects(const Obj_Entry *obj)
    557  1.35    martin {
    558  1.35    martin 	const Elf_Rela *rela;
    559  1.35    martin 
    560  1.35    martin 	rela = obj->pltrela;
    561  1.35    martin 
    562  1.35    martin 	/*
    563  1.35    martin 	 * Check for first four reserved entries - and skip them.
    564  1.35    martin 	 * See above for details.
    565  1.35    martin 	 */
    566  1.67     joerg 	if (ELF_R_TYPE(obj->pltrela->r_info) != R_TYPE(JMP_SLOT) &&
    567  1.67     joerg 	    ELF_R_TYPE(obj->pltrela->r_info) != R_TYPE(JMP_IREL))
    568  1.35    martin 		rela += 4;
    569  1.35    martin 
    570  1.35    martin 	for (; rela < obj->pltrelalim; rela++)
    571  1.35    martin 		if (_rtld_relocate_plt_object(obj, rela, NULL) < 0)
    572  1.35    martin 			return -1;
    573  1.35    martin 
    574  1.35    martin 	return 0;
    575  1.35    martin }
    576  1.35    martin 
    577  1.65     joerg static inline void
    578  1.65     joerg _rtld_write_plt(Elf_Word *where, Elf_Addr value, const Elf_Rela *rela,
    579  1.65     joerg     const Obj_Entry *obj)
    580  1.35    martin {
    581  1.65     joerg 	if (rela && rela->r_addend) {
    582  1.23   mycroft 		Elf_Addr *ptr = (Elf_Addr *)where;
    583  1.23   mycroft 		/*
    584  1.48     skrll 		 * This entry is >= 32768.  The relocations points to a
    585  1.28   mycroft 		 * PC-relative pointer to the bind_0 stub at the top of the
    586  1.28   mycroft 		 * PLT section.  Update it to point to the target function.
    587  1.23   mycroft 		 */
    588  1.27   mycroft 		ptr[0] += value - (Elf_Addr)obj->pltgot;
    589  1.23   mycroft 	} else {
    590  1.68     joerg 		sparc_write_branch(where + 1, (void *)value);
    591  1.65     joerg 	}
    592  1.65     joerg }
    593  1.65     joerg 
    594  1.65     joerg /*
    595  1.65     joerg  * New inline function that is called by _rtld_relocate_plt_object and
    596  1.65     joerg  * _rtld_bind
    597  1.65     joerg  */
    598  1.65     joerg static inline int
    599  1.65     joerg _rtld_relocate_plt_object(const Obj_Entry *obj, const Elf_Rela *rela,
    600  1.65     joerg     Elf_Addr *tp)
    601  1.65     joerg {
    602  1.65     joerg 	Elf_Word *where = (Elf_Word *)(obj->relocbase + rela->r_offset);
    603  1.65     joerg 	const Elf_Sym *def;
    604  1.65     joerg 	const Obj_Entry *defobj;
    605  1.65     joerg 	Elf_Addr value;
    606  1.65     joerg 	unsigned long info = rela->r_info;
    607  1.65     joerg 
    608  1.65     joerg 	if (ELF_R_TYPE(info) == R_TYPE(JMP_IREL))
    609  1.65     joerg 		return 0;
    610  1.65     joerg 
    611  1.65     joerg 	assert(ELF_R_TYPE(info) == R_TYPE(JMP_SLOT));
    612  1.65     joerg 
    613  1.65     joerg 	def = _rtld_find_plt_symdef(ELF_R_SYM(info), obj, &defobj, tp != NULL);
    614  1.65     joerg 	if (__predict_false(def == NULL))
    615  1.65     joerg 		return -1;
    616  1.65     joerg 	if (__predict_false(def == &_rtld_sym_zero))
    617  1.65     joerg 		return 0;
    618  1.23   mycroft 
    619  1.65     joerg 	if (ELF_ST_TYPE(def->st_info) == STT_GNU_IFUNC) {
    620  1.65     joerg 		if (tp == NULL)
    621  1.65     joerg 			return 0;
    622  1.65     joerg 		value = _rtld_resolve_ifunc(defobj, def);
    623  1.65     joerg 	} else {
    624  1.65     joerg 		value = (Elf_Addr)(defobj->relocbase + def->st_value);
    625  1.23   mycroft 	}
    626  1.65     joerg 	rdbg(("bind now/fixup in %s at %p --> new=%p",
    627  1.65     joerg 	    defobj->strtab + def->st_name, (void*)where, (void *)value));
    628  1.65     joerg 
    629  1.65     joerg 	_rtld_write_plt(where, value, rela, obj);
    630  1.23   mycroft 
    631  1.35    martin 	if (tp)
    632  1.35    martin 		*tp = value;
    633  1.35    martin 
    634  1.35    martin 	return 0;
    635   1.6   mycroft }
    636  1.65     joerg 
    637  1.65     joerg void
    638  1.65     joerg _rtld_call_ifunc(Obj_Entry *obj, sigset_t *mask, u_int cur_objgen)
    639  1.65     joerg {
    640  1.65     joerg 	const Elf_Rela *rela;
    641  1.65     joerg 	Elf_Addr *where;
    642  1.65     joerg 	Elf_Word *where2;
    643  1.65     joerg 	Elf_Addr target;
    644  1.65     joerg 
    645  1.65     joerg 	while (obj->ifunc_remaining > 0 && _rtld_objgen == cur_objgen) {
    646  1.65     joerg 		rela = obj->pltrelalim - --obj->ifunc_remaining;
    647  1.65     joerg 		if (ELF_R_TYPE(rela->r_info) != R_TYPE(JMP_IREL))
    648  1.65     joerg 			continue;
    649  1.65     joerg 		where2 = (Elf_Word *)(obj->relocbase + rela->r_offset);
    650  1.65     joerg 		target = (Elf_Addr)(obj->relocbase + rela->r_addend);
    651  1.65     joerg 		_rtld_exclusive_exit(mask);
    652  1.65     joerg 		target = _rtld_resolve_ifunc2(obj, target);
    653  1.65     joerg 		_rtld_exclusive_enter(mask);
    654  1.68     joerg 		sparc_write_branch(where2 + 1, (void *)target);
    655  1.65     joerg 	}
    656  1.65     joerg 
    657  1.65     joerg 	while (obj->ifunc_remaining_nonplt > 0 && _rtld_objgen == cur_objgen) {
    658  1.65     joerg 		rela = obj->relalim - --obj->ifunc_remaining_nonplt;
    659  1.65     joerg 		if (ELF_R_TYPE(rela->r_info) != R_TYPE(IRELATIVE))
    660  1.65     joerg 			continue;
    661  1.65     joerg 		where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
    662  1.65     joerg 		target = (Elf_Addr)(obj->relocbase + rela->r_addend);
    663  1.65     joerg 		_rtld_exclusive_exit(mask);
    664  1.65     joerg 		target = _rtld_resolve_ifunc2(obj, target);
    665  1.65     joerg 		_rtld_exclusive_enter(mask);
    666  1.65     joerg 		if (*where != target)
    667  1.65     joerg 			*where = target;
    668  1.65     joerg 	}
    669  1.65     joerg }
    670