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