Home | History | Annotate | Line # | Download | only in sparc64
mdreloc.c revision 1.52.6.1
      1  1.52.6.1       jdc /*	$NetBSD: mdreloc.c,v 1.52.6.1 2012/08/08 06:24:51 jdc 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.52.6.1       jdc __RCSID("$NetBSD: mdreloc.c,v 1.52.6.1 2012/08/08 06:24:51 jdc Exp $");
     36      1.37     skrll #endif /* not lint */
     37      1.37     skrll 
     38       1.1       eeh #include <errno.h>
     39       1.1       eeh #include <stdio.h>
     40       1.1       eeh #include <stdlib.h>
     41       1.1       eeh #include <string.h>
     42       1.1       eeh #include <unistd.h>
     43       1.1       eeh 
     44       1.1       eeh #include "rtldenv.h"
     45       1.1       eeh #include "debug.h"
     46       1.1       eeh #include "rtld.h"
     47       1.1       eeh 
     48       1.1       eeh /*
     49       1.1       eeh  * The following table holds for each relocation type:
     50       1.1       eeh  *	- the width in bits of the memory location the relocation
     51       1.1       eeh  *	  applies to (not currently used)
     52       1.1       eeh  *	- the number of bits the relocation value must be shifted to the
     53       1.1       eeh  *	  right (i.e. discard least significant bits) to fit into
     54       1.1       eeh  *	  the appropriate field in the instruction word.
     55       1.1       eeh  *	- flags indicating whether
     56       1.1       eeh  *		* the relocation involves a symbol
     57       1.1       eeh  *		* the relocation is relative to the current position
     58       1.1       eeh  *		* the relocation is for a GOT entry
     59       1.1       eeh  *		* the relocation is relative to the load address
     60       1.1       eeh  *
     61       1.1       eeh  */
     62       1.1       eeh #define _RF_S		0x80000000		/* Resolve symbol */
     63       1.1       eeh #define _RF_A		0x40000000		/* Use addend */
     64       1.1       eeh #define _RF_P		0x20000000		/* Location relative */
     65       1.1       eeh #define _RF_G		0x10000000		/* GOT offset */
     66       1.1       eeh #define _RF_B		0x08000000		/* Load address relative */
     67       1.2       eeh #define _RF_U		0x04000000		/* Unaligned */
     68       1.1       eeh #define _RF_SZ(s)	(((s) & 0xff) << 8)	/* memory target size */
     69       1.1       eeh #define _RF_RS(s)	( (s) & 0xff)		/* right shift */
     70      1.52    martin static const int reloc_target_flags[R_TYPE(TLS_TPOFF64)+1] = {
     71       1.1       eeh 	0,							/* NONE */
     72       1.1       eeh 	_RF_S|_RF_A|		_RF_SZ(8)  | _RF_RS(0),		/* RELOC_8 */
     73       1.1       eeh 	_RF_S|_RF_A|		_RF_SZ(16) | _RF_RS(0),		/* RELOC_16 */
     74       1.1       eeh 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* RELOC_32 */
     75       1.1       eeh 	_RF_S|_RF_A|_RF_P|	_RF_SZ(8)  | _RF_RS(0),		/* DISP_8 */
     76       1.1       eeh 	_RF_S|_RF_A|_RF_P|	_RF_SZ(16) | _RF_RS(0),		/* DISP_16 */
     77       1.1       eeh 	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(0),		/* DISP_32 */
     78       1.1       eeh 	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(2),		/* WDISP_30 */
     79       1.1       eeh 	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(2),		/* WDISP_22 */
     80       1.1       eeh 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(10),	/* HI22 */
     81       1.1       eeh 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* 22 */
     82       1.1       eeh 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* 13 */
     83       1.1       eeh 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* LO10 */
     84       1.1       eeh 	_RF_G|			_RF_SZ(32) | _RF_RS(0),		/* GOT10 */
     85       1.1       eeh 	_RF_G|			_RF_SZ(32) | _RF_RS(0),		/* GOT13 */
     86       1.1       eeh 	_RF_G|			_RF_SZ(32) | _RF_RS(10),	/* GOT22 */
     87       1.1       eeh 	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(0),		/* PC10 */
     88       1.1       eeh 	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(10),	/* PC22 */
     89       1.1       eeh 	      _RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(2),		/* WPLT30 */
     90       1.1       eeh 				_RF_SZ(32) | _RF_RS(0),		/* COPY */
     91       1.1       eeh 	_RF_S|_RF_A|		_RF_SZ(64) | _RF_RS(0),		/* GLOB_DAT */
     92       1.1       eeh 				_RF_SZ(32) | _RF_RS(0),		/* JMP_SLOT */
     93       1.1       eeh 	      _RF_A|	_RF_B|	_RF_SZ(64) | _RF_RS(0),		/* RELATIVE */
     94       1.2       eeh 	_RF_S|_RF_A|	_RF_U|	_RF_SZ(32) | _RF_RS(0),		/* UA_32 */
     95       1.1       eeh 
     96       1.1       eeh 	      _RF_A|		_RF_SZ(32) | _RF_RS(0),		/* PLT32 */
     97       1.1       eeh 	      _RF_A|		_RF_SZ(32) | _RF_RS(10),	/* HIPLT22 */
     98       1.1       eeh 	      _RF_A|		_RF_SZ(32) | _RF_RS(0),		/* LOPLT10 */
     99       1.1       eeh 	      _RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(0),		/* PCPLT32 */
    100       1.1       eeh 	      _RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(10),	/* PCPLT22 */
    101       1.1       eeh 	      _RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(0),		/* PCPLT10 */
    102       1.1       eeh 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* 10 */
    103       1.1       eeh 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* 11 */
    104       1.1       eeh 	_RF_S|_RF_A|		_RF_SZ(64) | _RF_RS(0),		/* 64 */
    105       1.1       eeh 	_RF_S|_RF_A|/*extra*/	_RF_SZ(32) | _RF_RS(0),		/* OLO10 */
    106       1.1       eeh 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(42),	/* HH22 */
    107       1.1       eeh 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(32),	/* HM10 */
    108       1.1       eeh 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(10),	/* LM22 */
    109       1.1       eeh 	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(42),	/* PC_HH22 */
    110       1.1       eeh 	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(32),	/* PC_HM10 */
    111       1.1       eeh 	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(10),	/* PC_LM22 */
    112       1.1       eeh 	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(2),		/* WDISP16 */
    113       1.1       eeh 	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(2),		/* WDISP19 */
    114       1.1       eeh 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* GLOB_JMP */
    115       1.1       eeh 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* 7 */
    116       1.1       eeh 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* 5 */
    117       1.1       eeh 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* 6 */
    118       1.1       eeh 	_RF_S|_RF_A|_RF_P|	_RF_SZ(64) | _RF_RS(0),		/* DISP64 */
    119       1.1       eeh 	      _RF_A|		_RF_SZ(64) | _RF_RS(0),		/* PLT64 */
    120       1.1       eeh 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(10),	/* HIX22 */
    121       1.1       eeh 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* LOX10 */
    122       1.1       eeh 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(22),	/* H44 */
    123       1.1       eeh 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(12),	/* M44 */
    124       1.1       eeh 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* L44 */
    125       1.1       eeh 	_RF_S|_RF_A|		_RF_SZ(64) | _RF_RS(0),		/* REGISTER */
    126       1.2       eeh 	_RF_S|_RF_A|	_RF_U|	_RF_SZ(64) | _RF_RS(0),		/* UA64 */
    127       1.2       eeh 	_RF_S|_RF_A|	_RF_U|	_RF_SZ(16) | _RF_RS(0),		/* UA16 */
    128      1.52    martin /* TLS relocs not represented here! */
    129       1.1       eeh };
    130       1.1       eeh 
    131       1.1       eeh #ifdef RTLD_DEBUG_RELOC
    132       1.1       eeh static const char *reloc_names[] = {
    133       1.1       eeh 	"NONE", "RELOC_8", "RELOC_16", "RELOC_32", "DISP_8",
    134       1.1       eeh 	"DISP_16", "DISP_32", "WDISP_30", "WDISP_22", "HI22",
    135       1.1       eeh 	"22", "13", "LO10", "GOT10", "GOT13",
    136       1.1       eeh 	"GOT22", "PC10", "PC22", "WPLT30", "COPY",
    137       1.1       eeh 	"GLOB_DAT", "JMP_SLOT", "RELATIVE", "UA_32", "PLT32",
    138       1.1       eeh 	"HIPLT22", "LOPLT10", "LOPLT10", "PCPLT22", "PCPLT32",
    139       1.1       eeh 	"10", "11", "64", "OLO10", "HH22",
    140       1.1       eeh 	"HM10", "LM22", "PC_HH22", "PC_HM10", "PC_LM22",
    141       1.1       eeh 	"WDISP16", "WDISP19", "GLOB_JMP", "7", "5", "6",
    142       1.1       eeh 	"DISP64", "PLT64", "HIX22", "LOX10", "H44", "M44",
    143      1.52    martin 	"L44", "REGISTER", "UA64", "UA16",
    144      1.52    martin 	"TLS_GD_HI22", "TLS_GD_LO10", "TLS_GD_ADD", "TLS_GD_CALL",
    145      1.52    martin 	"TLS_LDM_HI22", "TLS_LDM_LO10", "TLS_LDM_ADD", "TLS_LDM_CALL",
    146      1.52    martin 	"TLS_LDO_HIX22", "TLS_LDO_LOX10", "TLS_LDO_ADD", "TLS_IE_HI22",
    147      1.52    martin 	"TLS_IE_LO10", "TLS_IE_LD", "TLS_IE_LDX", "TLS_IE_ADD", "TLS_LE_HIX22",
    148      1.52    martin 	"TLS_LE_LOX10", "TLS_DTPMOD32", "TLS_DTPMOD64", "TLS_DTPOFF32",
    149      1.52    martin 	"TLS_DTPOFF64", "TLS_TPOFF32", "TLS_TPOFF64",
    150       1.1       eeh };
    151       1.1       eeh #endif
    152       1.1       eeh 
    153       1.1       eeh #define RELOC_RESOLVE_SYMBOL(t)		((reloc_target_flags[t] & _RF_S) != 0)
    154       1.1       eeh #define RELOC_PC_RELATIVE(t)		((reloc_target_flags[t] & _RF_P) != 0)
    155       1.1       eeh #define RELOC_BASE_RELATIVE(t)		((reloc_target_flags[t] & _RF_B) != 0)
    156       1.2       eeh #define RELOC_UNALIGNED(t)		((reloc_target_flags[t] & _RF_U) != 0)
    157       1.2       eeh #define RELOC_USE_ADDEND(t)		((reloc_target_flags[t] & _RF_A) != 0)
    158       1.1       eeh #define RELOC_TARGET_SIZE(t)		((reloc_target_flags[t] >> 8) & 0xff)
    159       1.1       eeh #define RELOC_VALUE_RIGHTSHIFT(t)	(reloc_target_flags[t] & 0xff)
    160      1.52    martin #define RELOC_TLS(t)			(t >= R_TYPE(TLS_GD_HI22))
    161       1.1       eeh 
    162      1.16   mycroft static const long reloc_target_bitmask[] = {
    163       1.1       eeh #define _BM(x)	(~(-(1ULL << (x))))
    164       1.1       eeh 	0,				/* NONE */
    165       1.1       eeh 	_BM(8), _BM(16), _BM(32),	/* RELOC_8, _16, _32 */
    166       1.1       eeh 	_BM(8), _BM(16), _BM(32),	/* DISP8, DISP16, DISP32 */
    167       1.1       eeh 	_BM(30), _BM(22),		/* WDISP30, WDISP22 */
    168       1.1       eeh 	_BM(22), _BM(22),		/* HI22, _22 */
    169       1.1       eeh 	_BM(13), _BM(10),		/* RELOC_13, _LO10 */
    170       1.1       eeh 	_BM(10), _BM(13), _BM(22),	/* GOT10, GOT13, GOT22 */
    171       1.1       eeh 	_BM(10), _BM(22),		/* _PC10, _PC22 */
    172       1.1       eeh 	_BM(30), 0,			/* _WPLT30, _COPY */
    173       1.1       eeh 	_BM(32), _BM(32), _BM(32),	/* _GLOB_DAT, JMP_SLOT, _RELATIVE */
    174       1.1       eeh 	_BM(32), _BM(32),		/* _UA32, PLT32 */
    175       1.1       eeh 	_BM(22), _BM(10),		/* _HIPLT22, LOPLT10 */
    176       1.1       eeh 	_BM(32), _BM(22), _BM(10),	/* _PCPLT32, _PCPLT22, _PCPLT10 */
    177       1.1       eeh 	_BM(10), _BM(11), -1,		/* _10, _11, _64 */
    178       1.1       eeh 	_BM(10), _BM(22),		/* _OLO10, _HH22 */
    179       1.1       eeh 	_BM(10), _BM(22),		/* _HM10, _LM22 */
    180       1.1       eeh 	_BM(22), _BM(10), _BM(22),	/* _PC_HH22, _PC_HM10, _PC_LM22 */
    181       1.1       eeh 	_BM(16), _BM(19),		/* _WDISP16, _WDISP19 */
    182       1.1       eeh 	-1,				/* GLOB_JMP */
    183       1.1       eeh 	_BM(7), _BM(5), _BM(6)		/* _7, _5, _6 */
    184       1.1       eeh 	-1, -1,				/* DISP64, PLT64 */
    185       1.1       eeh 	_BM(22), _BM(13),		/* HIX22, LOX10 */
    186       1.1       eeh 	_BM(22), _BM(10), _BM(13),	/* H44, M44, L44 */
    187       1.1       eeh 	-1, -1, _BM(16),		/* REGISTER, UA64, UA16 */
    188       1.1       eeh #undef _BM
    189       1.1       eeh };
    190       1.1       eeh #define RELOC_VALUE_BITMASK(t)	(reloc_target_bitmask[t])
    191       1.1       eeh 
    192       1.1       eeh /*
    193       1.1       eeh  * Instruction templates:
    194       1.1       eeh  */
    195       1.1       eeh #define	BAA	0x10400000	/*	ba,a	%xcc, 0 */
    196       1.1       eeh #define	SETHI	0x03000000	/*	sethi	%hi(0), %g1 */
    197       1.1       eeh #define	JMP	0x81c06000	/*	jmpl	%g1+%lo(0), %g0 */
    198       1.1       eeh #define	NOP	0x01000000	/*	sethi	%hi(0), %g0 */
    199       1.1       eeh #define	OR	0x82806000	/*	or	%g1, 0, %g1 */
    200       1.1       eeh #define	XOR	0x82c06000	/*	xor	%g1, 0, %g1 */
    201       1.1       eeh #define	MOV71	0x8283a000	/*	or	%o7, 0, %g1 */
    202       1.1       eeh #define	MOV17	0x9c806000	/*	or	%g1, 0, %o7 */
    203       1.1       eeh #define	CALL	0x40000000	/*	call	0 */
    204       1.1       eeh #define	SLLX	0x8b407000	/*	sllx	%g1, 0, %g1 */
    205       1.1       eeh #define	SETHIG5	0x0b000000	/*	sethi	%hi(0), %g5 */
    206       1.1       eeh #define	ORG5	0x82804005	/*	or	%g1, %g5, %g1 */
    207       1.1       eeh 
    208       1.1       eeh 
    209      1.26   mycroft /* %hi(v)/%lo(v) with variable shift */
    210      1.26   mycroft #define	HIVAL(v, s)	(((v) >> (s)) & 0x003fffff)
    211      1.26   mycroft #define LOVAL(v, s)	(((v) >> (s)) & 0x000003ff)
    212       1.1       eeh 
    213      1.20   mycroft void _rtld_bind_start_0(long, long);
    214      1.20   mycroft void _rtld_bind_start_1(long, long);
    215      1.18   mycroft void _rtld_relocate_nonplt_self(Elf_Dyn *, Elf_Addr);
    216      1.34     skrll caddr_t _rtld_bind(const Obj_Entry *, Elf_Word);
    217       1.1       eeh 
    218       1.1       eeh /*
    219       1.1       eeh  * Install rtld function call into this PLT slot.
    220       1.1       eeh  */
    221      1.29   mycroft #define	SAVE		0x9de3bf50	/* i.e. `save %sp,-176,%sp' */
    222       1.1       eeh #define	SETHI_l0	0x21000000
    223       1.1       eeh #define	SETHI_l1	0x23000000
    224       1.1       eeh #define	OR_l0_l0	0xa0142000
    225       1.1       eeh #define	SLLX_l0_32_l0	0xa12c3020
    226       1.1       eeh #define	OR_l0_l1_l0	0xa0140011
    227      1.26   mycroft #define	JMPL_l0_o0	0x91c42000
    228      1.26   mycroft #define	MOV_g1_o1	0x92100001
    229       1.1       eeh 
    230      1.36     skrll void _rtld_install_plt(Elf_Word *, Elf_Addr);
    231      1.36     skrll static inline int _rtld_relocate_plt_object(const Obj_Entry *,
    232      1.36     skrll     const Elf_Rela *, Elf_Addr *);
    233       1.1       eeh 
    234       1.1       eeh void
    235      1.34     skrll _rtld_install_plt(Elf_Word *pltgot, Elf_Addr proc)
    236       1.1       eeh {
    237       1.1       eeh 	pltgot[0] = SAVE;
    238       1.1       eeh 	pltgot[1] = SETHI_l0  | HIVAL(proc, 42);
    239       1.1       eeh 	pltgot[2] = SETHI_l1  | HIVAL(proc, 10);
    240      1.26   mycroft 	pltgot[3] = OR_l0_l0  | LOVAL(proc, 32);
    241       1.1       eeh 	pltgot[4] = SLLX_l0_32_l0;
    242       1.1       eeh 	pltgot[5] = OR_l0_l1_l0;
    243      1.26   mycroft 	pltgot[6] = JMPL_l0_o0 | LOVAL(proc, 0);
    244      1.26   mycroft 	pltgot[7] = MOV_g1_o1;
    245       1.1       eeh }
    246       1.2       eeh 
    247       1.6   mycroft void
    248       1.6   mycroft _rtld_setup_pltgot(const Obj_Entry *obj)
    249       1.6   mycroft {
    250       1.6   mycroft 	/*
    251       1.6   mycroft 	 * On sparc64 we got troubles.
    252       1.6   mycroft 	 *
    253       1.6   mycroft 	 * Instructions are 4 bytes long.
    254       1.6   mycroft 	 * Elf[64]_Addr is 8 bytes long, so are our pltglot[]
    255       1.6   mycroft 	 * array entries.
    256       1.6   mycroft 	 * Each PLT entry jumps to PLT0 to enter the dynamic
    257       1.6   mycroft 	 * linker.
    258       1.6   mycroft 	 * Loading an arbitrary 64-bit pointer takes 6
    259       1.6   mycroft 	 * instructions and 2 registers.
    260       1.6   mycroft 	 *
    261       1.6   mycroft 	 * Somehow we need to issue a save to get a new stack
    262       1.6   mycroft 	 * frame, load the address of the dynamic linker, and
    263       1.6   mycroft 	 * jump there, in 8 instructions or less.
    264       1.6   mycroft 	 *
    265       1.6   mycroft 	 * Oh, we need to fill out both PLT0 and PLT1.
    266       1.6   mycroft 	 */
    267       1.6   mycroft 	{
    268       1.6   mycroft 		Elf_Word *entry = (Elf_Word *)obj->pltgot;
    269       1.6   mycroft 
    270       1.6   mycroft 		/* Install in entries 0 and 1 */
    271       1.6   mycroft 		_rtld_install_plt(&entry[0], (Elf_Addr) &_rtld_bind_start_0);
    272       1.6   mycroft 		_rtld_install_plt(&entry[8], (Elf_Addr) &_rtld_bind_start_1);
    273       1.6   mycroft 
    274       1.6   mycroft 		/*
    275       1.6   mycroft 		 * Install the object reference in first slot
    276       1.6   mycroft 		 * of entry 2.
    277       1.6   mycroft 		 */
    278       1.6   mycroft 		obj->pltgot[8] = (Elf_Addr) obj;
    279       1.6   mycroft 	}
    280       1.8   mycroft }
    281       1.8   mycroft 
    282      1.18   mycroft void
    283      1.34     skrll _rtld_relocate_nonplt_self(Elf_Dyn *dynp, Elf_Addr relocbase)
    284      1.18   mycroft {
    285      1.18   mycroft 	const Elf_Rela *rela = 0, *relalim;
    286      1.18   mycroft 	Elf_Addr relasz = 0;
    287      1.18   mycroft 	Elf_Addr *where;
    288      1.18   mycroft 
    289      1.18   mycroft 	for (; dynp->d_tag != DT_NULL; dynp++) {
    290      1.18   mycroft 		switch (dynp->d_tag) {
    291      1.18   mycroft 		case DT_RELA:
    292      1.18   mycroft 			rela = (const Elf_Rela *)(relocbase + dynp->d_un.d_ptr);
    293      1.18   mycroft 			break;
    294      1.18   mycroft 		case DT_RELASZ:
    295      1.18   mycroft 			relasz = dynp->d_un.d_val;
    296      1.18   mycroft 			break;
    297      1.18   mycroft 		}
    298      1.18   mycroft 	}
    299      1.44     lukem 	relalim = (const Elf_Rela *)((const uint8_t *)rela + relasz);
    300      1.18   mycroft 	for (; rela < relalim; rela++) {
    301      1.18   mycroft 		where = (Elf_Addr *)(relocbase + rela->r_offset);
    302      1.18   mycroft 		*where = (Elf_Addr)(relocbase + rela->r_addend);
    303      1.18   mycroft 	}
    304      1.18   mycroft }
    305      1.18   mycroft 
    306       1.8   mycroft int
    307      1.47     joerg _rtld_relocate_nonplt_objects(Obj_Entry *obj)
    308       1.8   mycroft {
    309       1.9   mycroft 	const Elf_Rela *rela;
    310      1.40    martin 	const Elf_Sym *def = NULL;
    311      1.40    martin 	const Obj_Entry *defobj = NULL;
    312      1.18   mycroft 
    313       1.9   mycroft 	for (rela = obj->rela; rela < obj->relalim; rela++) {
    314       1.9   mycroft 		Elf_Addr *where;
    315       1.9   mycroft 		Elf_Word type;
    316       1.9   mycroft 		Elf_Addr value = 0, mask;
    317      1.10   mycroft 		unsigned long	 symnum;
    318       1.9   mycroft 
    319       1.9   mycroft 		where = (Elf_Addr *) (obj->relocbase + rela->r_offset);
    320      1.10   mycroft 		symnum = ELF_R_SYM(rela->r_info);
    321       1.9   mycroft 
    322       1.9   mycroft 		type = ELF_R_TYPE(rela->r_info);
    323       1.9   mycroft 		if (type == R_TYPE(NONE))
    324      1.12   mycroft 			continue;
    325       1.9   mycroft 
    326  1.52.6.1       jdc 		/* OLO10 relocations have extra info */
    327  1.52.6.1       jdc 		if ((type & 0x00ff) == R_SPARC_OLO10)
    328  1.52.6.1       jdc 			type = R_SPARC_OLO10;
    329  1.52.6.1       jdc 
    330      1.23   mycroft 		/* We do JMP_SLOTs in _rtld_bind() below */
    331       1.9   mycroft 		if (type == R_TYPE(JMP_SLOT))
    332      1.12   mycroft 			continue;
    333       1.9   mycroft 
    334       1.9   mycroft 		/* COPY relocs are also handled elsewhere */
    335       1.9   mycroft 		if (type == R_TYPE(COPY))
    336      1.12   mycroft 			continue;
    337       1.8   mycroft 
    338       1.9   mycroft 		/*
    339       1.9   mycroft 		 * We use the fact that relocation types are an `enum'
    340      1.52    martin 		 * Note: R_SPARC_TLS_TPOFF64 is currently numerically largest.
    341       1.9   mycroft 		 */
    342  1.52.6.1       jdc 		if (type > R_TYPE(TLS_TPOFF64)) {
    343  1.52.6.1       jdc 			dbg(("unknown relocation type %x at %p", type, rela));
    344  1.52.6.1       jdc 			return -1;
    345  1.52.6.1       jdc 		}
    346       1.8   mycroft 
    347       1.9   mycroft 		value = rela->r_addend;
    348       1.8   mycroft 
    349       1.9   mycroft 		/*
    350      1.52    martin 		 * Handle TLS relocations here, they are different.
    351      1.52    martin 		 */
    352      1.52    martin 		if (RELOC_TLS(type)) {
    353      1.52    martin 			switch (type) {
    354      1.52    martin 				case R_TYPE(TLS_DTPMOD64):
    355      1.52    martin 					def = _rtld_find_symdef(symnum, obj,
    356      1.52    martin 					    &defobj, false);
    357      1.52    martin 					if (def == NULL)
    358      1.52    martin 						return -1;
    359      1.52    martin 
    360      1.52    martin 					*where = (Elf64_Addr)defobj->tlsindex;
    361      1.52    martin 
    362      1.52    martin 					rdbg(("TLS_DTPMOD64 %s in %s --> %p",
    363      1.52    martin 					    obj->strtab +
    364      1.52    martin 					    obj->symtab[symnum].st_name,
    365      1.52    martin 					    obj->path, (void *)*where));
    366      1.52    martin 
    367      1.52    martin 					break;
    368      1.52    martin 
    369      1.52    martin 				case R_TYPE(TLS_DTPOFF64):
    370      1.52    martin 					def = _rtld_find_symdef(symnum, obj,
    371      1.52    martin 					    &defobj, false);
    372      1.52    martin 					if (def == NULL)
    373      1.52    martin 						return -1;
    374      1.52    martin 
    375      1.52    martin 					*where = (Elf64_Addr)(def->st_value
    376      1.52    martin 					    + rela->r_addend);
    377      1.52    martin 
    378      1.52    martin 					rdbg(("DTPOFF64 %s in %s --> %p",
    379      1.52    martin 					    obj->strtab +
    380      1.52    martin 					        obj->symtab[symnum].st_name,
    381      1.52    martin 					    obj->path, (void *)*where));
    382      1.52    martin 
    383      1.52    martin 					break;
    384      1.52    martin 
    385      1.52    martin 				case R_TYPE(TLS_TPOFF64):
    386      1.52    martin 					def = _rtld_find_symdef(symnum, obj,
    387      1.52    martin 					    &defobj, false);
    388      1.52    martin 					if (def == NULL)
    389      1.52    martin 						return -1;
    390      1.52    martin 
    391      1.52    martin 					if (!defobj->tls_done &&
    392      1.52    martin 						_rtld_tls_offset_allocate(obj))
    393      1.52    martin 						     return -1;
    394      1.52    martin 
    395      1.52    martin 					*where = (Elf64_Addr)(def->st_value -
    396      1.52    martin 			                            defobj->tlsoffset +
    397      1.52    martin 						    rela->r_addend);
    398      1.52    martin 
    399      1.52    martin 		                        rdbg(("TLS_TPOFF64 %s in %s --> %p",
    400      1.52    martin 		                            obj->strtab +
    401      1.52    martin 					    obj->symtab[symnum].st_name,
    402      1.52    martin 		                            obj->path, (void *)*where));
    403      1.52    martin 
    404      1.52    martin 	                		break;
    405      1.52    martin 			}
    406      1.52    martin 			continue;
    407      1.52    martin 		}
    408      1.52    martin 
    409      1.52    martin 		/*
    410      1.18   mycroft 		 * Handle relative relocs here, as an optimization.
    411       1.9   mycroft 		 */
    412      1.17   mycroft 		if (type == R_TYPE(RELATIVE)) {
    413       1.9   mycroft 			*where = (Elf_Addr)(obj->relocbase + value);
    414      1.21   mycroft 			rdbg(("RELATIVE in %s --> %p", obj->path,
    415      1.18   mycroft 			    (void *)*where));
    416      1.12   mycroft 			continue;
    417       1.9   mycroft 		}
    418       1.8   mycroft 
    419       1.9   mycroft 		if (RELOC_RESOLVE_SYMBOL(type)) {
    420       1.8   mycroft 
    421       1.9   mycroft 			/* Find the symbol */
    422      1.41      matt 			def = _rtld_find_symdef(symnum, obj, &defobj,
    423      1.41      matt 			    false);
    424      1.41      matt 			if (def == NULL)
    425      1.41      matt 				return -1;
    426       1.8   mycroft 
    427       1.9   mycroft 			/* Add in the symbol's absolute address */
    428       1.9   mycroft 			value += (Elf_Addr)(defobj->relocbase + def->st_value);
    429       1.9   mycroft 		}
    430       1.8   mycroft 
    431  1.52.6.1       jdc 		if (type == R_SPARC_OLO10) {
    432  1.52.6.1       jdc 			value = (value & 0x3ff)
    433  1.52.6.1       jdc 			    + (((Elf64_Xword)rela->r_info<<32)>>40);
    434  1.52.6.1       jdc 		}
    435  1.52.6.1       jdc 
    436       1.9   mycroft 		if (RELOC_PC_RELATIVE(type)) {
    437       1.9   mycroft 			value -= (Elf_Addr)where;
    438       1.9   mycroft 		}
    439       1.8   mycroft 
    440       1.9   mycroft 		if (RELOC_BASE_RELATIVE(type)) {
    441       1.9   mycroft 			/*
    442       1.9   mycroft 			 * Note that even though sparcs use `Elf_rela'
    443       1.9   mycroft 			 * exclusively we still need the implicit memory addend
    444       1.9   mycroft 			 * in relocations referring to GOT entries.
    445       1.9   mycroft 			 * Undoubtedly, someone f*cked this up in the distant
    446       1.9   mycroft 			 * past, and now we're stuck with it in the name of
    447       1.9   mycroft 			 * compatibility for all eternity..
    448       1.9   mycroft 			 *
    449       1.9   mycroft 			 * In any case, the implicit and explicit should be
    450       1.9   mycroft 			 * mutually exclusive. We provide a check for that
    451       1.9   mycroft 			 * here.
    452       1.9   mycroft 			 */
    453       1.8   mycroft #ifdef DIAGNOSTIC
    454       1.9   mycroft 			if (value != 0 && *where != 0) {
    455       1.9   mycroft 				xprintf("BASE_REL(%s): where=%p, *where 0x%lx, "
    456       1.9   mycroft 					"addend=0x%lx, base %p\n",
    457       1.9   mycroft 					obj->path, where, *where,
    458       1.9   mycroft 					rela->r_addend, obj->relocbase);
    459       1.9   mycroft 			}
    460       1.9   mycroft #endif
    461       1.9   mycroft 			/* XXXX -- apparently we ignore the preexisting value */
    462       1.9   mycroft 			value += (Elf_Addr)(obj->relocbase);
    463       1.8   mycroft 		}
    464       1.8   mycroft 
    465       1.9   mycroft 		mask = RELOC_VALUE_BITMASK(type);
    466       1.9   mycroft 		value >>= RELOC_VALUE_RIGHTSHIFT(type);
    467       1.9   mycroft 		value &= mask;
    468       1.9   mycroft 
    469       1.9   mycroft 		if (RELOC_UNALIGNED(type)) {
    470       1.9   mycroft 			/* Handle unaligned relocations. */
    471       1.9   mycroft 			Elf_Addr tmp = 0;
    472       1.9   mycroft 			char *ptr = (char *)where;
    473       1.9   mycroft 			int i, size = RELOC_TARGET_SIZE(type)/8;
    474       1.9   mycroft 
    475       1.9   mycroft 			/* Read it in one byte at a time. */
    476       1.9   mycroft 			for (i=0; i<size; i++)
    477       1.9   mycroft 				tmp = (tmp << 8) | ptr[i];
    478       1.9   mycroft 
    479       1.9   mycroft 			tmp &= ~mask;
    480       1.9   mycroft 			tmp |= value;
    481       1.9   mycroft 
    482       1.9   mycroft 			/* Write it back out. */
    483       1.9   mycroft 			for (i=0; i<size; i++)
    484       1.9   mycroft 				ptr[i] = ((tmp >> (8*i)) & 0xff);
    485       1.8   mycroft #ifdef RTLD_DEBUG_RELOC
    486       1.9   mycroft 			value = (Elf_Addr)tmp;
    487       1.8   mycroft #endif
    488       1.8   mycroft 
    489       1.9   mycroft 		} else if (RELOC_TARGET_SIZE(type) > 32) {
    490       1.9   mycroft 			*where &= ~mask;
    491       1.9   mycroft 			*where |= value;
    492       1.8   mycroft #ifdef RTLD_DEBUG_RELOC
    493       1.9   mycroft 			value = (Elf_Addr)*where;
    494       1.8   mycroft #endif
    495       1.9   mycroft 		} else {
    496       1.9   mycroft 			Elf32_Addr *where32 = (Elf32_Addr *)where;
    497       1.8   mycroft 
    498       1.9   mycroft 			*where32 &= ~mask;
    499       1.9   mycroft 			*where32 |= value;
    500       1.8   mycroft #ifdef RTLD_DEBUG_RELOC
    501       1.9   mycroft 			value = (Elf_Addr)*where32;
    502       1.8   mycroft #endif
    503       1.9   mycroft 		}
    504       1.8   mycroft 
    505       1.8   mycroft #ifdef RTLD_DEBUG_RELOC
    506       1.9   mycroft 		if (RELOC_RESOLVE_SYMBOL(type)) {
    507      1.21   mycroft 			rdbg(("%s %s in %s --> %p in %s", reloc_names[type],
    508      1.11   mycroft 			    obj->strtab + obj->symtab[symnum].st_name,
    509      1.33    petrov 			    obj->path, (void *)value, defobj->path));
    510      1.11   mycroft 		} else {
    511      1.21   mycroft 			rdbg(("%s in %s --> %p", reloc_names[type],
    512      1.33    petrov 			    obj->path, (void *)value));
    513       1.9   mycroft 		}
    514       1.9   mycroft #endif
    515       1.8   mycroft 	}
    516      1.13   mycroft 	return (0);
    517      1.13   mycroft }
    518      1.13   mycroft 
    519      1.13   mycroft int
    520      1.34     skrll _rtld_relocate_plt_lazy(const Obj_Entry *obj)
    521      1.13   mycroft {
    522       1.8   mycroft 	return (0);
    523      1.23   mycroft }
    524      1.23   mycroft 
    525      1.23   mycroft caddr_t
    526      1.34     skrll _rtld_bind(const Obj_Entry *obj, Elf_Word reloff)
    527      1.23   mycroft {
    528      1.24   mycroft 	const Elf_Rela *rela = obj->pltrela + reloff;
    529      1.35    martin 	Elf_Addr result;
    530      1.35    martin 	int err;
    531      1.23   mycroft 
    532      1.39       mrg 	result = 0;	/* XXX gcc */
    533      1.39       mrg 
    534      1.23   mycroft 	if (ELF_R_TYPE(obj->pltrela->r_info) == R_TYPE(JMP_SLOT)) {
    535      1.23   mycroft 		/*
    536      1.23   mycroft 		 * XXXX
    537      1.23   mycroft 		 *
    538      1.23   mycroft 		 * The first four PLT entries are reserved.  There is some
    539      1.23   mycroft 		 * disagreement whether they should have associated relocation
    540      1.23   mycroft 		 * entries.  Both the SPARC 32-bit and 64-bit ELF
    541      1.23   mycroft 		 * specifications say that they should have relocation entries,
    542      1.23   mycroft 		 * but the 32-bit SPARC binutils do not generate them, and now
    543      1.23   mycroft 		 * the 64-bit SPARC binutils have stopped generating them too.
    544      1.23   mycroft 		 *
    545      1.23   mycroft 		 * So, to provide binary compatibility, we will check the first
    546      1.23   mycroft 		 * entry, if it is reserved it should not be of the type
    547      1.23   mycroft 		 * JMP_SLOT.  If it is JMP_SLOT, then the 4 reserved entries
    548      1.23   mycroft 		 * were not generated and our index is 4 entries too far.
    549      1.23   mycroft 		 */
    550      1.23   mycroft 		rela -= 4;
    551      1.23   mycroft 	}
    552      1.32   thorpej 
    553      1.51     joerg 	_rtld_shared_enter();
    554      1.35    martin 	err = _rtld_relocate_plt_object(obj, rela, &result);
    555      1.46  christos 	if (err)
    556      1.35    martin 		_rtld_die();
    557      1.51     joerg 	_rtld_shared_exit();
    558      1.35    martin 
    559      1.35    martin 	return (caddr_t)result;
    560      1.35    martin }
    561      1.35    martin 
    562      1.35    martin int
    563      1.35    martin _rtld_relocate_plt_objects(const Obj_Entry *obj)
    564      1.35    martin {
    565      1.35    martin 	const Elf_Rela *rela;
    566      1.35    martin 
    567      1.35    martin 	rela = obj->pltrela;
    568      1.35    martin 
    569      1.35    martin 	/*
    570      1.35    martin 	 * Check for first four reserved entries - and skip them.
    571      1.35    martin 	 * See above for details.
    572      1.35    martin 	 */
    573      1.35    martin 	if (ELF_R_TYPE(obj->pltrela->r_info) != R_TYPE(JMP_SLOT))
    574      1.35    martin 		rela += 4;
    575      1.35    martin 
    576      1.35    martin 	for (; rela < obj->pltrelalim; rela++)
    577      1.35    martin 		if (_rtld_relocate_plt_object(obj, rela, NULL) < 0)
    578      1.35    martin 			return -1;
    579      1.35    martin 
    580      1.35    martin 	return 0;
    581      1.35    martin }
    582      1.35    martin 
    583      1.35    martin /*
    584      1.35    martin  * New inline function that is called by _rtld_relocate_plt_object and
    585      1.35    martin  * _rtld_bind
    586      1.35    martin  */
    587      1.35    martin static inline int
    588      1.49     skrll _rtld_relocate_plt_object(const Obj_Entry *obj, const Elf_Rela *rela,
    589      1.49     skrll     Elf_Addr *tp)
    590      1.35    martin {
    591      1.35    martin 	Elf_Word *where = (Elf_Word *)(obj->relocbase + rela->r_offset);
    592      1.35    martin 	const Elf_Sym *def;
    593      1.35    martin 	const Obj_Entry *defobj;
    594      1.35    martin 	Elf_Addr value, offset;
    595      1.46  christos 	unsigned long info = rela->r_info;
    596      1.23   mycroft 
    597      1.46  christos 	assert(ELF_R_TYPE(info) == R_TYPE(JMP_SLOT));
    598      1.23   mycroft 
    599      1.46  christos 	def = _rtld_find_plt_symdef(ELF_R_SYM(info), obj, &defobj, tp != NULL);
    600      1.46  christos 	if (__predict_false(def == NULL))
    601      1.35    martin 		return -1;
    602      1.46  christos 	if (__predict_false(def == &_rtld_sym_zero))
    603      1.46  christos 		return 0;
    604      1.23   mycroft 
    605      1.23   mycroft 	value = (Elf_Addr)(defobj->relocbase + def->st_value);
    606      1.27   mycroft 	rdbg(("bind now/fixup in %s --> new=%p",
    607      1.27   mycroft 	    defobj->strtab + def->st_name, (void *)value));
    608      1.23   mycroft 
    609      1.23   mycroft 	/*
    610      1.50     skrll 	 * At the PLT entry pointed at by `where', we now construct a direct
    611      1.50     skrll 	 * transfer to the now fully resolved function address.
    612      1.23   mycroft 	 *
    613      1.23   mycroft 	 * A PLT entry is supposed to start by looking like this:
    614      1.23   mycroft 	 *
    615      1.23   mycroft 	 *	sethi	%hi(. - .PLT0), %g1
    616      1.23   mycroft 	 *	ba,a	%xcc, .PLT1
    617      1.23   mycroft 	 *	nop
    618      1.23   mycroft 	 *	nop
    619      1.23   mycroft 	 *	nop
    620      1.23   mycroft 	 *	nop
    621      1.23   mycroft 	 *	nop
    622      1.23   mycroft 	 *	nop
    623      1.23   mycroft 	 *
    624      1.50     skrll 	 * When we replace these entries we start from the last instruction
    625      1.50     skrll 	 * and do it in reverse order so the last thing we do is replace the
    626      1.50     skrll 	 * branch.  That allows us to change this atomically.
    627      1.23   mycroft 	 *
    628      1.50     skrll 	 * We now need to find out how far we need to jump.  We have a choice
    629      1.50     skrll 	 * of several different relocation techniques which are increasingly
    630      1.50     skrll 	 * expensive.
    631      1.23   mycroft 	 */
    632      1.23   mycroft 
    633      1.23   mycroft 	offset = ((Elf_Addr)where) - value;
    634      1.23   mycroft 	if (rela->r_addend) {
    635      1.23   mycroft 		Elf_Addr *ptr = (Elf_Addr *)where;
    636      1.23   mycroft 		/*
    637      1.48     skrll 		 * This entry is >= 32768.  The relocations points to a
    638      1.28   mycroft 		 * PC-relative pointer to the bind_0 stub at the top of the
    639      1.28   mycroft 		 * PLT section.  Update it to point to the target function.
    640      1.23   mycroft 		 */
    641      1.27   mycroft 		ptr[0] += value - (Elf_Addr)obj->pltgot;
    642      1.23   mycroft 
    643      1.45    martin 	} else if (offset <= (1L<<20) && (Elf_SOff)offset >= -(1L<<20)) {
    644      1.23   mycroft 		/*
    645      1.23   mycroft 		 * We're within 1MB -- we can use a direct branch insn.
    646      1.23   mycroft 		 *
    647      1.23   mycroft 		 * We can generate this pattern:
    648      1.23   mycroft 		 *
    649      1.23   mycroft 		 *	sethi	%hi(. - .PLT0), %g1
    650      1.23   mycroft 		 *	ba,a	%xcc, addr
    651      1.23   mycroft 		 *	nop
    652      1.23   mycroft 		 *	nop
    653      1.23   mycroft 		 *	nop
    654      1.23   mycroft 		 *	nop
    655      1.23   mycroft 		 *	nop
    656      1.23   mycroft 		 *	nop
    657      1.23   mycroft 		 *
    658      1.23   mycroft 		 */
    659      1.48     skrll 		where[1] = BAA | ((offset >> 2) & 0x3fffff);
    660      1.38     perry 		__asm volatile("iflush %0+4" : : "r" (where));
    661      1.45    martin 	} else if (value < (1L<<32)) {
    662      1.23   mycroft 		/*
    663      1.26   mycroft 		 * We're within 32-bits of address zero.
    664      1.23   mycroft 		 *
    665      1.23   mycroft 		 * The resulting code in the jump slot is:
    666      1.23   mycroft 		 *
    667      1.23   mycroft 		 *	sethi	%hi(. - .PLT0), %g1
    668      1.23   mycroft 		 *	sethi	%hi(addr), %g1
    669      1.23   mycroft 		 *	jmp	%g1+%lo(addr)
    670      1.23   mycroft 		 *	nop
    671      1.23   mycroft 		 *	nop
    672      1.23   mycroft 		 *	nop
    673      1.23   mycroft 		 *	nop
    674      1.23   mycroft 		 *	nop
    675      1.23   mycroft 		 *
    676      1.23   mycroft 		 */
    677      1.26   mycroft 		where[2] = JMP   | LOVAL(value, 0);
    678      1.23   mycroft 		where[1] = SETHI | HIVAL(value, 10);
    679      1.38     perry 		__asm volatile("iflush %0+8" : : "r" (where));
    680      1.38     perry 		__asm volatile("iflush %0+4" : : "r" (where));
    681      1.23   mycroft 
    682      1.45    martin 	} else if ((Elf_SOff)value <= 0 && (Elf_SOff)value > -(1L<<32)) {
    683      1.23   mycroft 		/*
    684      1.26   mycroft 		 * We're within 32-bits of address -1.
    685      1.23   mycroft 		 *
    686      1.23   mycroft 		 * The resulting code in the jump slot is:
    687      1.23   mycroft 		 *
    688      1.23   mycroft 		 *	sethi	%hi(. - .PLT0), %g1
    689      1.23   mycroft 		 *	sethi	%hix(addr), %g1
    690      1.23   mycroft 		 *	xor	%g1, %lox(addr), %g1
    691      1.23   mycroft 		 *	jmp	%g1
    692      1.23   mycroft 		 *	nop
    693      1.23   mycroft 		 *	nop
    694      1.23   mycroft 		 *	nop
    695      1.23   mycroft 		 *	nop
    696      1.23   mycroft 		 *
    697      1.23   mycroft 		 */
    698      1.23   mycroft 		where[3] = JMP;
    699      1.23   mycroft 		where[2] = XOR | ((~value) & 0x00001fff);
    700      1.23   mycroft 		where[1] = SETHI | HIVAL(~value, 10);
    701      1.38     perry 		__asm volatile("iflush %0+12" : : "r" (where));
    702      1.38     perry 		__asm volatile("iflush %0+8" : : "r" (where));
    703      1.38     perry 		__asm volatile("iflush %0+4" : : "r" (where));
    704      1.23   mycroft 
    705      1.45    martin 	} else if (offset <= (1L<<32) && (Elf_SOff)offset >= -((1L<<32) - 4)) {
    706      1.23   mycroft 		/*
    707      1.26   mycroft 		 * We're within 32-bits -- we can use a direct call insn
    708      1.23   mycroft 		 *
    709      1.23   mycroft 		 * The resulting code in the jump slot is:
    710      1.23   mycroft 		 *
    711      1.23   mycroft 		 *	sethi	%hi(. - .PLT0), %g1
    712      1.23   mycroft 		 *	mov	%o7, %g1
    713      1.23   mycroft 		 *	call	(.+offset)
    714      1.23   mycroft 		 *	 mov	%g1, %o7
    715      1.23   mycroft 		 *	nop
    716      1.23   mycroft 		 *	nop
    717      1.23   mycroft 		 *	nop
    718      1.23   mycroft 		 *	nop
    719      1.23   mycroft 		 *
    720      1.23   mycroft 		 */
    721      1.23   mycroft 		where[3] = MOV17;
    722      1.23   mycroft 		where[2] = CALL	  | ((offset >> 4) & 0x3fffffff);
    723      1.23   mycroft 		where[1] = MOV71;
    724      1.38     perry 		__asm volatile("iflush %0+12" : : "r" (where));
    725      1.38     perry 		__asm volatile("iflush %0+8" : : "r" (where));
    726      1.38     perry 		__asm volatile("iflush %0+4" : : "r" (where));
    727      1.23   mycroft 
    728      1.45    martin 	} else if (offset < (1L<<44)) {
    729      1.23   mycroft 		/*
    730      1.26   mycroft 		 * We're within 44 bits.  We can generate this pattern:
    731      1.23   mycroft 		 *
    732      1.23   mycroft 		 * The resulting code in the jump slot is:
    733      1.23   mycroft 		 *
    734      1.23   mycroft 		 *	sethi	%hi(. - .PLT0), %g1
    735      1.23   mycroft 		 *	sethi	%h44(addr), %g1
    736      1.23   mycroft 		 *	or	%g1, %m44(addr), %g1
    737      1.23   mycroft 		 *	sllx	%g1, 12, %g1
    738      1.23   mycroft 		 *	jmp	%g1+%l44(addr)
    739      1.23   mycroft 		 *	nop
    740      1.23   mycroft 		 *	nop
    741      1.23   mycroft 		 *	nop
    742      1.23   mycroft 		 *
    743      1.23   mycroft 		 */
    744      1.26   mycroft 		where[4] = JMP   | LOVAL(offset, 0);
    745      1.23   mycroft 		where[3] = SLLX  | 12;
    746      1.23   mycroft 		where[2] = OR    | (((offset) >> 12) & 0x00001fff);
    747      1.23   mycroft 		where[1] = SETHI | HIVAL(offset, 22);
    748      1.38     perry 		__asm volatile("iflush %0+16" : : "r" (where));
    749      1.38     perry 		__asm volatile("iflush %0+12" : : "r" (where));
    750      1.38     perry 		__asm volatile("iflush %0+8" : : "r" (where));
    751      1.38     perry 		__asm volatile("iflush %0+4" : : "r" (where));
    752      1.23   mycroft 
    753      1.45    martin 	} else if ((Elf_SOff)offset < 0 && (Elf_SOff)offset > -(1L<<44)) {
    754      1.23   mycroft 		/*
    755      1.26   mycroft 		 * We're within 44 bits.  We can generate this pattern:
    756      1.23   mycroft 		 *
    757      1.23   mycroft 		 * The resulting code in the jump slot is:
    758      1.23   mycroft 		 *
    759      1.23   mycroft 		 *	sethi	%hi(. - .PLT0), %g1
    760      1.23   mycroft 		 *	sethi	%h44(-addr), %g1
    761      1.23   mycroft 		 *	xor	%g1, %m44(-addr), %g1
    762      1.23   mycroft 		 *	sllx	%g1, 12, %g1
    763      1.23   mycroft 		 *	jmp	%g1+%l44(addr)
    764      1.23   mycroft 		 *	nop
    765      1.23   mycroft 		 *	nop
    766      1.23   mycroft 		 *	nop
    767      1.23   mycroft 		 *
    768      1.23   mycroft 		 */
    769      1.26   mycroft 		where[4] = JMP   | LOVAL(offset, 0);
    770      1.23   mycroft 		where[3] = SLLX  | 12;
    771      1.23   mycroft 		where[2] = XOR   | (((~offset) >> 12) & 0x00001fff);
    772      1.23   mycroft 		where[1] = SETHI | HIVAL(~offset, 22);
    773      1.38     perry 		__asm volatile("iflush %0+16" : : "r" (where));
    774      1.38     perry 		__asm volatile("iflush %0+12" : : "r" (where));
    775      1.38     perry 		__asm volatile("iflush %0+8" : : "r" (where));
    776      1.38     perry 		__asm volatile("iflush %0+4" : : "r" (where));
    777      1.23   mycroft 
    778      1.23   mycroft 	} else {
    779      1.23   mycroft 		/*
    780      1.23   mycroft 		 * We need to load all 64-bits
    781      1.23   mycroft 		 *
    782      1.23   mycroft 		 * The resulting code in the jump slot is:
    783      1.23   mycroft 		 *
    784      1.23   mycroft 		 *	sethi	%hi(. - .PLT0), %g1
    785      1.23   mycroft 		 *	sethi	%hh(addr), %g1
    786      1.23   mycroft 		 *	sethi	%lm(addr), %g5
    787      1.23   mycroft 		 *	or	%g1, %hm(addr), %g1
    788      1.23   mycroft 		 *	sllx	%g1, 32, %g1
    789      1.23   mycroft 		 *	or	%g1, %g5, %g1
    790      1.23   mycroft 		 *	jmp	%g1+%lo(addr)
    791      1.23   mycroft 		 *	nop
    792      1.23   mycroft 		 *
    793      1.23   mycroft 		 */
    794      1.26   mycroft 		where[6] = JMP     | LOVAL(value, 0);
    795      1.23   mycroft 		where[5] = ORG5;
    796      1.26   mycroft 		where[4] = SLLX    | 32;
    797      1.26   mycroft 		where[3] = OR      | LOVAL(value, 32);
    798      1.23   mycroft 		where[2] = SETHIG5 | HIVAL(value, 10);
    799      1.23   mycroft 		where[1] = SETHI   | HIVAL(value, 42);
    800      1.38     perry 		__asm volatile("iflush %0+24" : : "r" (where));
    801      1.38     perry 		__asm volatile("iflush %0+20" : : "r" (where));
    802      1.38     perry 		__asm volatile("iflush %0+16" : : "r" (where));
    803      1.38     perry 		__asm volatile("iflush %0+12" : : "r" (where));
    804      1.38     perry 		__asm volatile("iflush %0+8" : : "r" (where));
    805      1.38     perry 		__asm volatile("iflush %0+4" : : "r" (where));
    806      1.23   mycroft 
    807      1.23   mycroft 	}
    808      1.23   mycroft 
    809      1.35    martin 	if (tp)
    810      1.35    martin 		*tp = value;
    811      1.35    martin 
    812      1.35    martin 	return 0;
    813       1.6   mycroft }
    814