1 /*	$OpenBSD: rtld_machine.c,v 1.68 2019/12/07 22:57:48 guenther Exp $ */
2 
3 /*
4  * Copyright (c) 1999 Dale Rahn
5  * Copyright (c) 2001 Niklas Hallqvist
6  * Copyright (c) 2001 Artur Grabowski
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
18  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 /*-
30  * Copyright (c) 2000 Eduardo Horvath.
31  * Copyright (c) 1999 The NetBSD Foundation, Inc.
32  * All rights reserved.
33  *
34  * This code is derived from software contributed to The NetBSD Foundation
35  * by Paul Kranenburg.
36  *
37  * Redistribution and use in source and binary forms, with or without
38  * modification, are permitted provided that the following conditions
39  * are met:
40  * 1. Redistributions of source code must retain the above copyright
41  *    notice, this list of conditions and the following disclaimer.
42  * 2. Redistributions in binary form must reproduce the above copyright
43  *    notice, this list of conditions and the following disclaimer in the
44  *    documentation and/or other materials provided with the distribution.
45  * 3. All advertising materials mentioning features or use of this software
46  *    must display the following acknowledgement:
47  *	This product includes software developed by the NetBSD
48  *	Foundation, Inc. and its contributors.
49  * 4. Neither the name of The NetBSD Foundation nor the names of its
50  *    contributors may be used to endorse or promote products derived
51  *    from this software without specific prior written permission.
52  *
53  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
54  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
55  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
56  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
57  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
58  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
59  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
60  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
61  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
62  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
63  * POSSIBILITY OF SUCH DAMAGE.
64  */
65 
66 #define _DYN_LOADER
67 
68 #include <sys/types.h>
69 #include <sys/mman.h>
70 #include <sys/syscall.h>
71 #include <sys/unistd.h>
72 #include <machine/trap.h>
73 
74 #include <nlist.h>
75 #include <link.h>
76 
77 #include "syscall.h"
78 #include "archdep.h"
79 #include "resolve.h"
80 
81 int64_t pcookie __attribute__((section(".openbsd.randomdata"))) __dso_hidden;
82 
83 /*
84  * The following table holds for each relocation type:
85  *	- the width in bits of the memory location the relocation
86  *	  applies to (not currently used)
87  *	- the number of bits the relocation value must be shifted to the
88  *	  right (i.e. discard least significant bits) to fit into
89  *	  the appropriate field in the instruction word.
90  *	- flags indicating whether
91  *		* the relocation involves a symbol
92  *		* the relocation is relative to the current position
93  *		* the relocation is for a GOT entry
94  *		* the relocation is relative to the load address
95  *
96  */
97 #define _RF_S		0x80000000		/* Resolve symbol */
98 #define _RF_A		0x40000000		/* Use addend */
99 #define _RF_P		0x20000000		/* Location relative */
100 #define _RF_G		0x10000000		/* GOT offset */
101 #define _RF_B		0x08000000		/* Load address relative */
102 #define _RF_U		0x04000000		/* Unaligned */
103 #define _RF_SZ(s)	(((s) & 0xff) << 8)	/* memory target size */
104 #define _RF_RS(s)	((s) & 0xff)		/* right shift */
105 static const int reloc_target_flags[] = {
106 	0,							/* NONE */
107 	_RF_S|_RF_A|		_RF_SZ(8)  | _RF_RS(0),		/* RELOC_8 */
108 	_RF_S|_RF_A|		_RF_SZ(16) | _RF_RS(0),		/* RELOC_16 */
109 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* RELOC_32 */
110 	_RF_S|_RF_A|_RF_P|	_RF_SZ(8)  | _RF_RS(0),		/* DISP_8 */
111 	_RF_S|_RF_A|_RF_P|	_RF_SZ(16) | _RF_RS(0),		/* DISP_16 */
112 	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(0),		/* DISP_32 */
113 	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(2),		/* WDISP_30 */
114 	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(2),		/* WDISP_22 */
115 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(10),	/* HI22 */
116 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* 22 */
117 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* 13 */
118 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* LO10 */
119 	_RF_G|			_RF_SZ(32) | _RF_RS(0),		/* GOT10 */
120 	_RF_G|			_RF_SZ(32) | _RF_RS(0),		/* GOT13 */
121 	_RF_G|			_RF_SZ(32) | _RF_RS(10),	/* GOT22 */
122 	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(0),		/* PC10 */
123 	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(10),	/* PC22 */
124 	      _RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(2),		/* WPLT30 */
125 	_RF_S|			_RF_SZ(32) | _RF_RS(0),		/* COPY */
126 	_RF_S|_RF_A|		_RF_SZ(64) | _RF_RS(0),		/* GLOB_DAT */
127 	_RF_S|			_RF_SZ(32) | _RF_RS(0),		/* JMP_SLOT */
128 	      _RF_A|	_RF_B|	_RF_SZ(64) | _RF_RS(0),		/* RELATIVE */
129 	_RF_S|_RF_A|	_RF_U|	_RF_SZ(32) | _RF_RS(0),		/* UA_32 */
130 
131 	      _RF_A|		_RF_SZ(32) | _RF_RS(0),		/* PLT32 */
132 	      _RF_A|		_RF_SZ(32) | _RF_RS(10),	/* HIPLT22 */
133 	      _RF_A|		_RF_SZ(32) | _RF_RS(0),		/* LOPLT10 */
134 	      _RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(0),		/* PCPLT32 */
135 	      _RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(10),	/* PCPLT22 */
136 	      _RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(0),		/* PCPLT10 */
137 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* 10 */
138 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* 11 */
139 	_RF_S|_RF_A|		_RF_SZ(64) | _RF_RS(0),		/* 64 */
140 	_RF_S|_RF_A|/*extra*/	_RF_SZ(32) | _RF_RS(0),		/* OLO10 */
141 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(42),	/* HH22 */
142 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(32),	/* HM10 */
143 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(10),	/* LM22 */
144 	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(42),	/* PC_HH22 */
145 	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(32),	/* PC_HM10 */
146 	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(10),	/* PC_LM22 */
147 	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(2),		/* WDISP16 */
148 	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(2),		/* WDISP19 */
149 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* GLOB_JMP */
150 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* 7 */
151 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* 5 */
152 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* 6 */
153 	_RF_S|_RF_A|_RF_P|	_RF_SZ(64) | _RF_RS(0),		/* DISP64 */
154 	      _RF_A|		_RF_SZ(64) | _RF_RS(0),		/* PLT64 */
155 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(10),	/* HIX22 */
156 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* LOX10 */
157 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(22),	/* H44 */
158 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(12),	/* M44 */
159 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* L44 */
160 	_RF_S|_RF_A|		_RF_SZ(64) | _RF_RS(0),		/* REGISTER */
161 	_RF_S|_RF_A|	_RF_U|	_RF_SZ(64) | _RF_RS(0),		/* UA64 */
162 	_RF_S|_RF_A|	_RF_U|	_RF_SZ(16) | _RF_RS(0),		/* UA16 */
163 };
164 
165 #define RELOC_RESOLVE_SYMBOL(t)		((reloc_target_flags[t] & _RF_S) != 0)
166 #define RELOC_PC_RELATIVE(t)		((reloc_target_flags[t] & _RF_P) != 0)
167 #define RELOC_BASE_RELATIVE(t)		((reloc_target_flags[t] & _RF_B) != 0)
168 #define RELOC_UNALIGNED(t)		((reloc_target_flags[t] & _RF_U) != 0)
169 #define RELOC_USE_ADDEND(t)		((reloc_target_flags[t] & _RF_A) != 0)
170 #define RELOC_TARGET_SIZE(t)		((reloc_target_flags[t] >> 8) & 0xff)
171 #define RELOC_VALUE_RIGHTSHIFT(t)	(reloc_target_flags[t] & 0xff)
172 
173 static const long reloc_target_bitmask[] = {
174 #define _BM(x)	(~(-(1ULL << (x))))
175 	0,				/* NONE */
176 	_BM(8), _BM(16), _BM(32),	/* RELOC_8, _16, _32 */
177 	_BM(8), _BM(16), _BM(32),	/* DISP8, DISP16, DISP32 */
178 	_BM(30), _BM(22),		/* WDISP30, WDISP22 */
179 	_BM(22), _BM(22),		/* HI22, _22 */
180 	_BM(13), _BM(10),		/* RELOC_13, _LO10 */
181 	_BM(10), _BM(13), _BM(22),	/* GOT10, GOT13, GOT22 */
182 	_BM(10), _BM(22),		/* _PC10, _PC22 */
183 	_BM(30), 0,			/* _WPLT30, _COPY */
184 	-1, _BM(32), -1,		/* _GLOB_DAT, JMP_SLOT, _RELATIVE */
185 	_BM(32), _BM(32),		/* _UA32, PLT32 */
186 	_BM(22), _BM(10),		/* _HIPLT22, LOPLT10 */
187 	_BM(32), _BM(22), _BM(10),	/* _PCPLT32, _PCPLT22, _PCPLT10 */
188 	_BM(10), _BM(11), -1,		/* _10, _11, _64 */
189 	_BM(10), _BM(22),		/* _OLO10, _HH22 */
190 	_BM(10), _BM(22),		/* _HM10, _LM22 */
191 	_BM(22), _BM(10), _BM(22),	/* _PC_HH22, _PC_HM10, _PC_LM22 */
192 	_BM(16), _BM(19),		/* _WDISP16, _WDISP19 */
193 	-1,				/* GLOB_JMP */
194 	_BM(7), _BM(5), _BM(6)		/* _7, _5, _6 */
195 	-1, -1,				/* DISP64, PLT64 */
196 	_BM(22), _BM(13),		/* HIX22, LOX10 */
197 	_BM(22), _BM(10), _BM(13),	/* H44, M44, L44 */
198 	-1, -1, _BM(16),		/* REGISTER, UA64, UA16 */
199 #undef _BM
200 };
201 #define RELOC_VALUE_BITMASK(t)	(reloc_target_bitmask[t])
202 
203 int _dl_reloc_plt(Elf_Word *where1, Elf_Word *where2, Elf_Word *pltaddr,
204 	Elf_Addr value);
205 void _dl_install_plt(Elf_Word *pltgot, Elf_Addr proc);
206 
207 int
208 _dl_md_reloc(elf_object_t *object, int rel, int relasz)
209 {
210 	long	i;
211 	long	numrela;
212 	long	relrel;
213 	int	fails = 0;
214 	Elf_Addr loff;
215 	Elf_Addr prev_value = 0;
216 	const Elf_Sym *prev_sym = NULL;
217 	Elf_RelA *relas;
218 
219 	loff = object->obj_base;
220 	numrela = object->Dyn.info[relasz] / sizeof(Elf_RelA);
221 	relrel = rel == DT_RELA ? object->relacount : 0;
222 	relas = (Elf_RelA *)(object->Dyn.info[rel]);
223 
224 	if (relas == NULL)
225 		return 0;
226 
227 	if (relrel > numrela)
228 		_dl_die("relacount > numrel: %ld > %ld", relrel, numrela);
229 
230 	/* tight loop for leading RELATIVE relocs */
231 	for (i = 0; i < relrel; i++, relas++) {
232 		Elf_Addr *where;
233 
234 		where = (Elf_Addr *)(relas->r_offset + loff);
235 		*where = relas->r_addend + loff;
236 	}
237 	for (; i < numrela; i++, relas++) {
238 		Elf_Addr *where, value, mask;
239 		Elf_Word type;
240 		const Elf_Sym *sym;
241 		const char *symn;
242 
243 		type = ELF_R_TYPE(relas->r_info);
244 
245 		if (type == R_TYPE(NONE) || type == R_TYPE(JMP_SLOT))
246 			continue;
247 
248 		where = (Elf_Addr *)(relas->r_offset + loff);
249 
250 		if (RELOC_USE_ADDEND(type))
251 			value = relas->r_addend;
252 		else
253 			value = 0;
254 
255 		sym = NULL;
256 		symn = NULL;
257 		if (RELOC_RESOLVE_SYMBOL(type)) {
258 			sym = object->dyn.symtab;
259 			sym += ELF_R_SYM(relas->r_info);
260 			symn = object->dyn.strtab + sym->st_name;
261 
262 			if (sym->st_shndx != SHN_UNDEF &&
263 			    ELF_ST_BIND(sym->st_info) == STB_LOCAL) {
264 				value += loff;
265 			} else if (sym == prev_sym) {
266 				value += prev_value;
267 			} else {
268 				struct sym_res sr;
269 
270 				sr = _dl_find_symbol(symn,
271 				    SYM_SEARCH_ALL|SYM_WARNNOTFOUND|SYM_NOTPLT,
272 				    sym, object);
273 				if (sr.sym == NULL) {
274 resolve_failed:
275 					if (ELF_ST_BIND(sym->st_info) !=
276 					    STB_WEAK)
277 						fails++;
278 					continue;
279 				}
280 				prev_sym = sym;
281 				prev_value = (Elf_Addr)(sr.obj->obj_base +
282 				    sr.sym->st_value);
283 				value += prev_value;
284 			}
285 		}
286 
287 		if (type == R_TYPE(COPY)) {
288 			void *dstaddr = where;
289 			const void *srcaddr;
290 			const Elf_Sym *dstsym = sym;
291 			struct sym_res sr;
292 
293 			sr = _dl_find_symbol(symn,
294 			    SYM_SEARCH_OTHER|SYM_WARNNOTFOUND|SYM_NOTPLT,
295 			    dstsym, object);
296 			if (sr.sym == NULL)
297 				goto resolve_failed;
298 
299 			srcaddr = (void *)(sr.obj->obj_base + sr.sym->st_value);
300 			_dl_bcopy(srcaddr, dstaddr, dstsym->st_size);
301 			continue;
302 		}
303 
304 		if (RELOC_PC_RELATIVE(type))
305 			value -= (Elf_Addr)where;
306 		if (RELOC_BASE_RELATIVE(type))
307 			value += loff;
308 
309 		mask = RELOC_VALUE_BITMASK(type);
310 		value >>= RELOC_VALUE_RIGHTSHIFT(type);
311 		value &= mask;
312 
313 		if (RELOC_UNALIGNED(type)) {
314 			/* Handle unaligned relocations. */
315 			Elf_Addr tmp = 0;
316 			char *ptr = (char *)where;
317 			int i, size = RELOC_TARGET_SIZE(type)/8;
318 
319 			/* Read it in one byte at a time. */
320 			for (i=0; i<size; i++)
321 				tmp = (tmp << 8) | ptr[i];
322 
323 			tmp &= ~mask;
324 			tmp |= value;
325 
326 			/* Write it back out. */
327 			for (i=0; i<size; i++)
328 				ptr[i] = ((tmp >> (8*i)) & 0xff);
329 		} else if (RELOC_TARGET_SIZE(type) > 32) {
330 			*where &= ~mask;
331 			*where |= value;
332 		} else {
333 			Elf32_Addr *where32 = (Elf32_Addr *)where;
334 
335 			*where32 &= ~mask;
336 			*where32 |= value;
337 		}
338 	}
339 
340 	return fails;
341 }
342 
343 /*
344  * Instruction templates:
345  */
346 
347 #define	BAA	0x30680000	/*	ba,a	%xcc, 0 */
348 #define	SETHI	0x03000000	/*	sethi	%hi(0), %g1 */
349 #define	JMP	0x81c06000	/*	jmpl	%g1+%lo(0), %g0	  <-- simm13 */
350 #define	NOP	0x01000000	/*	sethi	%hi(0), %g0 */
351 #define	OR	0x82106000	/*	or	%g1, 0, %g1 */
352 #define	ORG5	0x8a116000	/*	or	%g5, 0, %g5 */
353 #define	XOR	0x82186000	/*	xor	%g1, 0, %g1 */
354 #define	MOV71	0x8210000f	/*	or	%o7, 0, %g1 */
355 #define	MOV17	0x9e100001	/*	or	%g1, 0, %o7 */
356 #define	CALL	0x40000000	/*	call	0	  <-- disp30 */
357 #define	SLLX	0x83287000	/*	sllx	%g1, 0, %g1 */
358 #define	SLLXG5	0x8b297000	/*	sllx	%g5, 0, %g5 */
359 #define	SRAX	0x83387000	/*	srax	%g1, 0, %g1 */
360 #define	SETHIG5	0x0b000000	/*	sethi	%hi(0), %g5 */
361 #define	ORG15	0x82804005	/*	or	%g1, %g5, %g1 */
362 
363 
364 /* %hi(v) with variable shift */
365 #define	HIVAL(v, s)	(((v) >> (s)) &  0x003fffff)
366 #define LOVAL(v)	((v) & 0x000003ff)
367 
368 int
369 _dl_reloc_plt(Elf_Word *where1, Elf_Word *where2, Elf_Word *pltaddr,
370     Elf_Addr value)
371 {
372 	Elf_Addr offset;
373 
374 	/*
375 	 * At the PLT entry pointed at by `where', we now construct
376 	 * a direct transfer to the now fully resolved function
377 	 * address.
378 	 *
379 	 * A PLT entry is supposed to start by looking like this:
380 	 *
381 	 *	sethi	%hi(. - .PLT0), %g1
382 	 *	ba,a,pt	%xcc, .PLT1
383 	 *	nop
384 	 *	nop
385 	 *	nop
386 	 *	nop
387 	 *	nop
388 	 *	nop
389 	 *
390 	 * When we replace these entries we either (a) only replace
391 	 * the second word (the ba,a,pt), or (b) replace multiple
392 	 * words: one or more nops, then finally the ba,a,pt.  By
393 	 * replacing the ba,a,pt last, we guarantee that the PLT can
394 	 * be used by other threads even while it's being updated.
395 	 * This is made slightly more complicated by kbind, for which
396 	 * we need to pass them to the kernel in the order they get
397 	 * written.  To that end, we store the word to overwrite the
398 	 * ba,a,pt at *where1, and the words to overwrite the nops at
399 	 * where2[0], where2[1], ...
400 	 *
401 	 * We now need to find out how far we need to jump.  We
402 	 * have a choice of several different relocation techniques
403 	 * which are increasingly expensive.
404 	 */
405 
406 	offset = value - ((Elf_Addr)pltaddr);
407 	if ((int64_t)(offset-4) <= (1L<<20) &&
408 	    (int64_t)(offset-4) >= -(1L<<20)) {
409 		/*
410 		 * We're within 1MB -- we can use a direct branch insn.
411 		 *
412 		 * We can generate this pattern:
413 		 *
414 		 *	sethi	%hi(. - .PLT0), %g1
415 		 *	ba,a,pt	%xcc, addr
416 		 *	nop
417 		 *	nop
418 		 *	nop
419 		 *	nop
420 		 *	nop
421 		 *	nop
422 		 *
423 		 */
424 		*where1 = BAA | (((offset-4) >> 2) &0x7ffff);
425 		return 0;
426 	} else if (value < (1UL<<32)) {
427 		/*
428 		 * We're within 32-bits of address zero.
429 		 *
430 		 * The resulting code in the jump slot is:
431 		 *
432 		 *	sethi	%hi(. - .PLT0), %g1
433 		 *	sethi	%hi(addr), %g1
434 		 *	jmp	%g1+%lo(addr)
435 		 *	nop
436 		 *	nop
437 		 *	nop
438 		 *	nop
439 		 *	nop
440 		 *
441 		 */
442 		*where1 = SETHI | HIVAL(value, 10);
443 		where2[0] = JMP   | LOVAL(value);
444 		return 1;
445 	} else if (value > -(1UL<<32)) {
446 		/*
447 		 * We're within 32-bits of address -1.
448 		 *
449 		 * The resulting code in the jump slot is:
450 		 *
451 		 *	sethi	%hi(. - .PLT0), %g1
452 		 *	sethi	%hix(~addr), %g1
453 		 *	xor	%g1, %lox(~addr), %g1
454 		 *	jmp	%g1
455 		 *	nop
456 		 *	nop
457 		 *	nop
458 		 *	nop
459 		 *
460 		 */
461 		*where1 = SETHI | HIVAL(~value, 10);
462 		where2[0] = XOR | ((~value) & 0x00001fff);
463 		where2[1] = JMP;
464 		return 2;
465 	} else if ((int64_t)(offset-8) <= (1L<<31) &&
466 	    (int64_t)(offset-8) >= -((1L<<31) - 4)) {
467 		/*
468 		 * We're within 32-bits -- we can use a direct call insn
469 		 *
470 		 * The resulting code in the jump slot is:
471 		 *
472 		 *	sethi	%hi(. - .PLT0), %g1
473 		 *	mov	%o7, %g1
474 		 *	call	(.+offset)
475 		 *	 mov	%g1, %o7
476 		 *	nop
477 		 *	nop
478 		 *	nop
479 		 *	nop
480 		 *
481 		 */
482 		*where1 = MOV71;
483 		where2[0] = CALL | (((offset-8) >> 2) & 0x3fffffff);
484 		where2[1] = MOV17;
485 		return 2;
486 	} else if (value < (1L<<42)) {
487 		/*
488 		 * Target 42bits or smaller.
489 		 * We can generate this pattern:
490 		 *
491 		 * The resulting code in the jump slot is:
492 		 *
493 		 *	sethi	%hi(. - .PLT0), %g1
494 		 *	sethi	%hi(addr >> 20), %g1
495 		 *	or	%g1, %lo(addr >> 10), %g1
496 		 *	sllx	%g1, 10, %g1
497 		 *	jmp	%g1+%lo(addr)
498 		 *	nop
499 		 *	nop
500 		 *	nop
501 		 *
502 		 * this can handle addresses 0 - 0x3fffffffffc
503 		 */
504 		*where1 = SETHI | HIVAL(value, 20);
505 		where2[0] = OR    | LOVAL(value >> 10);
506 		where2[1] = SLLX  | 10;
507 		where2[2] = JMP   | LOVAL(value);
508 		return 3;
509 	} else if (value > -(1UL<<41)) {
510 		/*
511 		 * Large target >= 0xfffffe0000000000UL
512 		 * We can generate this pattern:
513 		 *
514 		 * The resulting code in the jump slot is:
515 		 *
516 		 *	sethi	%hi(. - .PLT0), %g1
517 		 *	sethi	%hi(addr >> 20), %g1
518 		 *	or	%g1, %lo(addr >> 10), %g1
519 		 *	sllx	%g1, 32, %g1
520 		 *	srax	%g1, 22, %g1
521 		 *	jmp	%g1+%lo(addr)
522 		 *	nop
523 		 *	nop
524 		 *	nop
525 		 *
526 		 */
527 		*where1 = SETHI | HIVAL(value, 20);
528 		where2[0] = OR   | LOVAL(value >> 10);
529 		where2[1] = SLLX  | 32;
530 		where2[2] = SRAX  | 22;
531 		where2[3] = JMP   | LOVAL(value);
532 		return 4;
533 	} else {
534 		/*
535 		 * We need to load all 64-bits
536 		 *
537 		 * The resulting code in the jump slot is:
538 		 *
539 		 *	sethi	%hi(. - .PLT0), %g1
540 		 *	sethi	%hi(addr >> 42), %g5
541 		 *	sethi	%hi(addr >> 10), %g1
542 		 *	or	%g1, %lo(addr >> 32), %g5
543 		 *	sllx	%g5, 32, %g5
544 		 *	or	%g1, %g5, %g1
545 		 *	jmp	%g1+%lo(addr)
546 		 *	nop
547 		 *
548 		 */
549 		*where1 = SETHIG5 | HIVAL(value, 42);
550 		where2[0] = SETHI | HIVAL(value, 10);
551 		where2[1] = ORG5 | LOVAL(value >> 32);
552 		where2[2] = SLLXG5 | 32;
553 		where2[3] = ORG15;
554 		where2[4] = JMP | LOVAL(value);
555 		return 5;
556 	}
557 }
558 
559 /*
560  * Resolve a symbol at run-time.
561  */
562 Elf_Addr
563 _dl_bind(elf_object_t *object, int index)
564 {
565 	Elf_RelA *rela;
566 	Elf_Word *addr;
567 	Elf_Addr newvalue;
568 	struct sym_res sr;
569 	const Elf_Sym *sym;
570 	const char *symn;
571 	int64_t cookie = pcookie;
572 	struct {
573 		struct __kbind param[2];
574 		Elf_Word newval[6];
575 	} buf;
576 	struct __kbind *param;
577 	size_t psize;
578 	int i;
579 
580 	rela = (Elf_RelA *)(object->Dyn.info[DT_JMPREL]);
581 	if (ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT)) {
582 		/*
583 		 * XXXX
584 		 *
585 		 * The first four PLT entries are reserved.  There
586 		 * is some disagreement whether they should have
587 		 * associated relocation entries.  Both the SPARC
588 		 * 32-bit and 64-bit ELF specifications say that
589 		 * they should have relocation entries, but the
590 		 * 32-bit SPARC binutils do not generate them,
591 		 * and now the 64-bit SPARC binutils have stopped
592 		 * generating them too.
593 		 *
594 		 * So, to provide binary compatibility, we will
595 		 * check the first entry, if it is reserved it
596 		 * should not be of the type JMP_SLOT.  If it
597 		 * is JMP_SLOT, then the 4 reserved entries were
598 		 * not generated and our index is 4 entries too far.
599 		 */
600 		rela += index - 4;
601 	} else
602 		rela += index;
603 
604 	sym = object->dyn.symtab;
605 	sym += ELF_R_SYM(rela->r_info);
606 	symn = object->dyn.strtab + sym->st_name;
607 
608 	sr = _dl_find_symbol(symn, SYM_SEARCH_ALL|SYM_WARNNOTFOUND|SYM_PLT,
609 	    sym, object);
610 	if (sr.sym == NULL)
611 		_dl_die("lazy binding failed!");
612 
613 	newvalue = sr.obj->obj_base + sr.sym->st_value;
614 
615 	if (__predict_false(sr.obj->traced) && _dl_trace_plt(sr.obj, symn))
616 		return newvalue;
617 
618 	/*
619 	 * While some relocations just need to write one word and
620 	 * can do that with kbind() with just one block, many
621 	 * require two blocks to be written: all but first word,
622 	 * then the first word.  So, if we want to write 5 words
623 	 * in total, then the layout of the buffer we pass to
624 	 * kbind() needs to be one of these:
625 	 *   +------------+
626 	 *   | kbind.addr |
627 	 *   |     """    |
628 	 *   | kbind.size |
629 	 *   |     """    |		+------------+
630 	 *   | kbind.addr |		| kbind.addr |
631 	 *   |     """    |		|     """    |
632 	 *   | kbind.size |		| kbind.size |
633 	 *   |     """    |		|     """    |
634 	 *   |   word 2   |		|    word    |
635 	 *   |   word 3   |		+------------+
636 	 *   |   word 4   |
637 	 *   |   word 5   |
638 	 *   |   word 1   |
639 	 *   +------------+
640 	 *
641 	 * We first handle the special case of relocations with a
642 	 * non-zero r_addend, which have one block to update whose
643 	 * address is the relocation address itself.  This is only
644 	 * used for PLT entries after the 2^15th, i.e., truly monstrous
645 	 * programs, thus the __predict_false().
646 	 */
647 	addr = (Elf_Word *)(object->obj_base + rela->r_offset);
648 	_dl_memset(&buf, 0, sizeof(buf));
649 	if (__predict_false(rela->r_addend)) {
650 		/*
651 		 * This entry is >32768.  The relocation points to a
652 		 * PC-relative pointer to the _dl_bind_start_0 stub at
653 		 * the top of the PLT section.  Update it to point to
654 		 * the target function.
655 		 */
656 		buf.newval[0] = rela->r_addend + newvalue
657 		    - object->Dyn.info[DT_PLTGOT];
658 		buf.param[1].kb_addr = addr;
659 		buf.param[1].kb_size = sizeof(buf.newval[0]);
660 		param = &buf.param[1];
661 		psize = sizeof(struct __kbind) + sizeof(buf.newval[0]);
662 	} else {
663 		Elf_Word first;
664 
665 		/*
666 		 * For the other relocations, the word at the relocation
667 		 * address will be left unchanged.  Assume _dl_reloc_plt()
668 		 * will tell us to update multiple words, so save the first
669 		 * word to the side.
670 		 */
671 		i = _dl_reloc_plt(&first, &buf.newval[0], addr, newvalue);
672 
673 		/*
674 		 * _dl_reloc_plt() returns the number of words that must be
675 		 * written after the first word in location, but before it
676 		 * in time.  If it returns zero, then only a single block
677 		 * with one word is needed, so we just put it in place per
678 		 * the right-hand diagram and just use param[1] and newval[0]
679 		 */
680 		if (i == 0) {
681 			/* fill in the __kbind structure */
682 			buf.param[1].kb_addr = &addr[1];
683 			buf.param[1].kb_size = sizeof(Elf_Word);
684 			buf.newval[0] = first;
685 			param = &buf.param[1];
686 			psize = sizeof(struct __kbind) + sizeof(buf.newval[0]);
687 		} else {
688 			/*
689 			 * Two blocks are necessary.  Save the first word
690 			 * after the other words.
691 			 */
692 			buf.param[0].kb_addr = &addr[2];
693 			buf.param[0].kb_size = i * sizeof(Elf_Word);
694 			buf.param[1].kb_addr = &addr[1];
695 			buf.param[1].kb_size = sizeof(Elf_Word);
696 			buf.newval[i] = first;
697 			param = &buf.param[0];
698 			psize = 2 * sizeof(struct __kbind) +
699 			    (i + 1) * sizeof(buf.newval[0]);
700 		}
701 	}
702 
703 	/* directly code the syscall, so that it's actually inline here */
704 	{
705 		register long syscall_num __asm("g1") = SYS_kbind;
706 		register void *arg1 __asm("o0") = param;
707 		register long  arg2 __asm("o1") = psize;
708 		register long  arg3 __asm("o2") = cookie;
709 
710 		__asm volatile("t %2" : "+r" (arg1), "+r" (arg2)
711 		    : "i" (ST_SYSCALL), "r" (syscall_num), "r" (arg3)
712 		    : "cc", "memory");
713 	}
714 
715 	return newvalue;
716 }
717 
718 /*
719  * Install rtld function call into this PLT slot.
720  */
721 #define SAVE		0x9de3bf50
722 #define SETHI_l0	0x21000000
723 #define SETHI_l1	0x23000000
724 #define OR_l0_l0	0xa0142000
725 #define SLLX_l0_32_l0	0xa12c3020
726 #define OR_l0_l1_l0	0xa0140011
727 #define JMPL_l0_o1	0x93c42000
728 #define MOV_g1_o0	0x90100001
729 
730 void
731 _dl_install_plt(Elf_Word *pltgot, Elf_Addr proc)
732 {
733 	pltgot[0] = SAVE;
734 	pltgot[1] = SETHI_l0  | HIVAL(proc, 42);
735 	pltgot[2] = SETHI_l1  | HIVAL(proc, 10);
736 	pltgot[3] = OR_l0_l0  | LOVAL((proc) >> 32);
737 	pltgot[4] = SLLX_l0_32_l0;
738 	pltgot[5] = OR_l0_l1_l0;
739 	pltgot[6] = JMPL_l0_o1 | LOVAL(proc);
740 	pltgot[7] = MOV_g1_o0;
741 }
742 
743 void _dl_bind_start_0(long, long);
744 void _dl_bind_start_1(long, long);
745 
746 static int
747 _dl_md_reloc_all_plt(elf_object_t *object)
748 {
749 	long	i;
750 	long	numrela;
751 	int	fails = 0;
752 	Elf_Addr loff;
753 	Elf_RelA *relas;
754 
755 	loff = object->obj_base;
756 	numrela = object->Dyn.info[DT_PLTRELSZ] / sizeof(Elf_RelA);
757 	relas = (Elf_RelA *)(object->Dyn.info[DT_JMPREL]);
758 
759 	if (relas == NULL)
760 		return 0;
761 
762 	for (i = 0; i < numrela; i++, relas++) {
763 		Elf_Addr value;
764 		Elf_Word *where;
765 		struct sym_res sr;
766 		const Elf_Sym *sym;
767 
768 		if (ELF_R_TYPE(relas->r_info) != R_TYPE(JMP_SLOT))
769 			continue;
770 
771 		sym = object->dyn.symtab + ELF_R_SYM(relas->r_info);
772 
773 		sr = _dl_find_symbol(object->dyn.strtab + sym->st_name,
774 		    SYM_SEARCH_ALL|SYM_WARNNOTFOUND|SYM_PLT,
775 		    sym, object);
776 		if (sr.sym == NULL) {
777 			if (ELF_ST_BIND(sym->st_info) != STB_WEAK)
778 				fails++;
779 			continue;
780 		}
781 
782 		where = (Elf_Word *)(relas->r_offset + loff);
783 		value = sr.obj->obj_base + sr.sym->st_value;
784 
785 		if (__predict_false(relas->r_addend)) {
786 			/*
787 			 * This entry is >32768.  The relocation points to a
788 			 * PC-relative pointer to the _dl_bind_start_0 stub at
789 			 * the top of the PLT section.  Update it to point to
790 			 * the target function.
791 			 */
792 			*(Elf_Addr *)where = relas->r_addend + value -
793 			    object->Dyn.info[DT_PLTGOT];
794 		} else
795 			_dl_reloc_plt(&where[1], &where[2], where, value);
796 	}
797 
798 	return fails;
799 }
800 
801 /*
802  *	Relocate the Global Offset Table (GOT).
803  */
804 int
805 _dl_md_reloc_got(elf_object_t *object, int lazy)
806 {
807 	int	fails = 0;
808 	Elf_Addr *pltgot = (Elf_Addr *)object->Dyn.info[DT_PLTGOT];
809 	Elf_Word *entry = (Elf_Word *)pltgot;
810 
811 	if (object->Dyn.info[DT_PLTREL] != DT_RELA)
812 		return 0;
813 
814 	if (!lazy) {
815 		fails = _dl_md_reloc_all_plt(object);
816 	} else {
817 		_dl_install_plt(&entry[0], (Elf_Addr)&_dl_bind_start_0);
818 		_dl_install_plt(&entry[8], (Elf_Addr)&_dl_bind_start_1);
819 
820 		pltgot[8] = (Elf_Addr)object;
821 	}
822 
823 	return fails;
824 }
825