xref: /titanic_41/usr/src/cmd/sgs/libld/common/machrel.sparc.c (revision 6a074c93c5dee390d8ca2377f42e55418f0a9eb3)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  *	Copyright (c) 1988 AT&T
24  *	  All Rights Reserved
25  *
26  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
27  * Use is subject to license terms.
28  */
29 #pragma ident	"%Z%%M%	%I%	%E% SMI"
30 
31 #include	<string.h>
32 #include	<stdio.h>
33 #include	<sys/elf_SPARC.h>
34 #include	<debug.h>
35 #include	<reloc.h>
36 #include	"msg.h"
37 #include	"_libld.h"
38 
39 /*
40  * Local Variable Definitions
41  */
42 static Sword neggotoffset = 0;		/* off. of GOT table from GOT symbol */
43 static Sword smlgotcnt = M_GOT_XNumber;	/* no. of small GOT symbols */
44 static Sword mixgotcnt = 0;		/* # syms with both large/small GOT */
45 
46 Word
47 ld_init_rel(Rel_desc *reld, void *reloc)
48 {
49 	Rela *	rela = (Rela *)reloc;
50 
51 	/* LINTED */
52 	reld->rel_rtype = (Word)ELF_R_TYPE(rela->r_info);
53 	reld->rel_roffset = rela->r_offset;
54 	reld->rel_raddend = rela->r_addend;
55 	reld->rel_typedata = (Word)ELF_R_TYPE_DATA(rela->r_info);
56 
57 	reld->rel_flags |= FLG_REL_RELA;
58 
59 	return ((Word)ELF_R_SYM(rela->r_info));
60 }
61 
62 void
63 ld_mach_eflags(Ehdr *ehdr, Ofl_desc *ofl)
64 {
65 	Word		eflags = ofl->ofl_dehdr->e_flags;
66 	Word		memopt1, memopt2;
67 	static int	firstpass;
68 
69 	/*
70 	 * If a *PLUS relocatable is included, the output object is type *PLUS.
71 	 */
72 	if ((ehdr->e_machine == EM_SPARC32PLUS) &&
73 	    (ehdr->e_flags & EF_SPARC_32PLUS))
74 		ofl->ofl_dehdr->e_machine = EM_SPARC32PLUS;
75 
76 	/*
77 	 * On the first pass, we don't yet have a memory model to compare
78 	 * against, therefore the initial file becomes our baseline.  Subsequent
79 	 * passes will do the comparison described below.
80 	 */
81 	if (firstpass == 0) {
82 		ofl->ofl_dehdr->e_flags |= ehdr->e_flags;
83 		firstpass++;
84 		return;
85 	}
86 
87 	/*
88 	 * Determine which memory model to mark the binary with.  The options
89 	 * are (most restrictive to least):
90 	 *
91 	 *	EF_SPARCV9_TSO		0x0 	Total Store Order
92 	 *	EF_SPARCV9_PSO		0x1	Partial Store Order
93 	 *	EF_SPARCV9_RMO		0x2	Relaxed Memory Order
94 	 *
95 	 * Mark the binary with the most restrictive option encountered from a
96 	 * relocatable object included in the link.
97 	 */
98 	eflags |= (ehdr->e_flags & ~EF_SPARCV9_MM);
99 	memopt1 = eflags & EF_SPARCV9_MM;
100 	memopt2 = ehdr->e_flags & EF_SPARCV9_MM;
101 	eflags &= ~EF_SPARCV9_MM;
102 
103 	if ((memopt1 == EF_SPARCV9_TSO) || (memopt2 == EF_SPARCV9_TSO))
104 		/* EMPTY */
105 		;
106 	else if ((memopt1 == EF_SPARCV9_PSO) || (memopt2 == EF_SPARCV9_PSO))
107 		eflags |= EF_SPARCV9_PSO;
108 	else
109 		eflags |= EF_SPARCV9_RMO;
110 
111 	ofl->ofl_dehdr->e_flags = eflags;
112 }
113 
114 void
115 ld_mach_make_dynamic(Ofl_desc *ofl, size_t *cnt)
116 {
117 	if (!(ofl->ofl_flags & FLG_OF_RELOBJ)) {
118 		/*
119 		 * Create this entry if we are going to create a PLT table.
120 		 */
121 		if (ofl->ofl_pltcnt)
122 			(*cnt)++;		/* DT_PLTGOT */
123 	}
124 }
125 
126 void
127 ld_mach_update_odynamic(Ofl_desc *ofl, Dyn **dyn)
128 {
129 	if (((ofl->ofl_flags & FLG_OF_RELOBJ) == 0) && ofl->ofl_pltcnt) {
130 		(*dyn)->d_tag = DT_PLTGOT;
131 		if (ofl->ofl_osplt)
132 			(*dyn)->d_un.d_ptr = ofl->ofl_osplt->os_shdr->sh_addr;
133 		else
134 			(*dyn)->d_un.d_ptr = 0;
135 		(*dyn)++;
136 	}
137 }
138 
139 #if	defined(_ELF64)
140 
141 Xword
142 ld_calc_plt_addr(Sym_desc *sdp, Ofl_desc *ofl)
143 {
144 	Xword	value, pltndx, farpltndx;
145 
146 	pltndx = sdp->sd_aux->sa_PLTndx + M_PLT_XNumber - 1;
147 
148 	if ((pltndx) < M64_PLT_NEARPLTS) {
149 		value = (Xword)(ofl->ofl_osplt->os_shdr->sh_addr) +
150 		    (pltndx * M_PLT_ENTSIZE);
151 		return (value);
152 	}
153 
154 	farpltndx = pltndx - M64_PLT_NEARPLTS;
155 
156 	/*
157 	 * pltoffset of a far plt is calculated by:
158 	 *
159 	 *	<size of near plt table> +
160 	 *	<size of preceding far plt blocks> +
161 	 *	<blockndx * sizeof (far plt entsize)>
162 	 */
163 	value =
164 	    /* size of near plt table */
165 	    (M64_PLT_NEARPLTS * M_PLT_ENTSIZE) +
166 	    /* size of preceding far plt blocks */
167 	    ((farpltndx / M64_PLT_FBLKCNTS) *
168 	    ((M64_PLT_FENTSIZE + sizeof (Addr)) *
169 	    M64_PLT_FBLKCNTS)) +
170 	    /* pltblockendx * fentsize */
171 	    ((farpltndx % M64_PLT_FBLKCNTS) * M64_PLT_FENTSIZE);
172 
173 	value += (Xword)(ofl->ofl_osplt->os_shdr->sh_addr);
174 	return (value);
175 }
176 
177 /*
178  * Instructions required for Far PLT's
179  */
180 static uint32_t farplt_instrs[6] = {
181 	0x8a10000f,			/* mov   %o7, %g5	*/
182 	0x40000002,			/* call  . + 0x8	*/
183 	0x01000000,			/* nop			*/
184 	0xc25be000,			/* ldx   [%o7 + 0], %g1	*/
185 	0x83c3c001,			/* jmpl  %o7 + %g1, %g1	*/
186 	0x9e100005			/* mov   %g5, %o7	*/
187 };
188 
189 /*
190  * Far PLT'S:
191  *
192  * Far PLT's are established in blocks of '160' at a time.  These
193  * PLT's consist of 6 instructions (24 bytes) and 1 pointer (8 bytes).
194  * The instructions are collected together in blocks of 160 entries
195  * followed by 160 pointers.  The last group of entries and pointers
196  * may contain less then 160 items.  No padding is required.
197  *
198  *	.PLT32768:
199  *		mov	%o7, %g5
200  *		call	. + 8
201  *		nop
202  *		ldx	[%o7 + .PLTP32768 - (.PLT32768 + 4)], %g1
203  *		jmpl	%o7 + %g1, %g1
204  *		mov	%g5, %o7
205  *	................................
206  *	.PLT32927:
207  *		mov	%o7, %g5
208  *		call	. + 8
209  *		nop
210  *		ldx	[%o7 + .PLTP32927 - (.PLT32927 + 4)], %g1
211  *		jmpl	%o7 + %g1, %g1
212  *		mov	%g5, %o7
213  *	.PLTP32768:
214  *		.xword .PLT0-(.PLT32768+4)
215  *	................................
216  *	.PLTP32927:
217  *		.xword .PLT0-(.PLT32927+4)
218  *
219  */
220 void
221 plt_far_entry(Ofl_desc *ofl, Xword pltndx, Xword *roffset, Sxword *raddend)
222 {
223 	uint_t		blockndx;	/* # of far PLT blocks */
224 	uint_t		farblkcnt;	/* Index to far PLT block */
225 	Xword		farpltndx;	/* index of Far Plt */
226 	Xword		farpltblkndx;	/* index of PLT in BLOCK */
227 	uint32_t	*pltent;	/* ptr to plt instr. sequence */
228 	uint64_t	*pltentptr;	/* ptr to plt addr ptr */
229 	Sxword		pltblockoff;	/* offset to Far plt block */
230 	Sxword		pltoff;		/* offset to PLT instr. sequence */
231 	Sxword		pltptroff;	/* offset to PLT addr ptr */
232 	uchar_t		*pltbuf;	/* ptr to PLT's in file */
233 
234 
235 	farblkcnt = ((ofl->ofl_pltcnt - 1 +
236 	    M_PLT_XNumber - M64_PLT_NEARPLTS) / M64_PLT_FBLKCNTS);
237 
238 	/*
239 	 * Determine the 'Far' PLT index.
240 	 */
241 	farpltndx = pltndx - 1 + M_PLT_XNumber - M64_PLT_NEARPLTS;
242 	farpltblkndx = farpltndx % M64_PLT_FBLKCNTS;
243 
244 	/*
245 	 * Determine what FPLT block this plt falls into.
246 	 */
247 	blockndx = (uint_t)(farpltndx / M64_PLT_FBLKCNTS);
248 
249 	/*
250 	 * Calculate the starting offset of the Far PLT block
251 	 * that this PLT is a member of.
252 	 */
253 	pltblockoff = (M64_PLT_NEARPLTS * M_PLT_ENTSIZE) +
254 	    (blockndx * M64_PLT_FBLOCKSZ);
255 
256 	pltoff = pltblockoff +
257 	    (farpltblkndx * M64_PLT_FENTSIZE);
258 
259 	pltptroff = pltblockoff;
260 
261 
262 	if (farblkcnt > blockndx) {
263 		/*
264 		 * If this is a full block - the 'pltptroffs' start
265 		 * after 160 fplts.
266 		 */
267 		pltptroff += (M64_PLT_FBLKCNTS * M64_PLT_FENTSIZE) +
268 		    (farpltblkndx * M64_PLT_PSIZE);
269 	} else {
270 		Xword	lastblkpltndx;
271 		/*
272 		 * If this is the last block - the the pltptr's start
273 		 * after the last FPLT instruction sequence.
274 		 */
275 		lastblkpltndx = (ofl->ofl_pltcnt - 1 + M_PLT_XNumber -
276 		    M64_PLT_NEARPLTS) % M64_PLT_FBLKCNTS;
277 		pltptroff += ((lastblkpltndx + 1) * M64_PLT_FENTSIZE) +
278 		    (farpltblkndx * M64_PLT_PSIZE);
279 	}
280 	pltbuf = (uchar_t *)ofl->ofl_osplt->os_outdata->d_buf;
281 
282 	/*
283 	 * For far-plts, the Raddend and Roffset fields are defined
284 	 * to be:
285 	 *
286 	 *	roffset:	address of .PLTP#
287 	 *	raddend:	-(.PLT#+4)
288 	 */
289 	*roffset = pltptroff + (Xword)(ofl->ofl_osplt->os_shdr->sh_addr);
290 	*raddend = -(pltoff + 4 + (Xword)(ofl->ofl_osplt->os_shdr->sh_addr));
291 
292 	/* LINTED */
293 	pltent = (uint32_t *)(pltbuf + pltoff);
294 	/* LINTED */
295 	pltentptr = (uint64_t *)(pltbuf + pltptroff);
296 	(void) memcpy(pltent, farplt_instrs, sizeof (farplt_instrs));
297 
298 	/*
299 	 *  update
300 	 *	ldx   [%o7 + 0], %g1
301 	 * to
302 	 *	ldx   [%o7 + .PLTP# - (.PLT# + 4)], %g1
303 	 */
304 	/* LINTED */
305 	pltent[3] |= (uint32_t)(pltptroff - (pltoff + 4));
306 
307 	/*
308 	 * Store:
309 	 *	.PLTP#
310 	 *		.xword	.PLT0 - .PLT# + 4
311 	 */
312 	*pltentptr = -(pltoff + 4);
313 }
314 
315 /*
316  *	Build a single V9 P.L.T. entry - code is:
317  *
318  *	For Target Addresses +/- 4GB of the entry
319  *	-----------------------------------------
320  *	sethi	(. - .PLT0), %g1
321  *	ba,a	%xcc, .PLT1
322  *	nop
323  *	nop
324  *	nop
325  *	nop
326  *	nop
327  *	nop
328  *
329  *	For Target Addresses +/- 2GB of the entry
330  *	-----------------------------------------
331  *
332  *	.PLT0 is the address of the first entry in the P.L.T.
333  *	This one is filled in by the run-time link editor. We just
334  *	have to leave space for it.
335  */
336 static void
337 plt_entry(Ofl_desc *ofl, Xword pltndx, Xword *roffset, Sxword *raddend)
338 {
339 	uchar_t		*pltent;	/* PLT entry being created. */
340 	Sxword		pltoff;		/* Offset of this entry from PLT top */
341 
342 	/*
343 	 *  The second part of the V9 ABI (sec. 5.2.4)
344 	 *  applies to plt entries greater than 0x8000 (32,768).
345 	 *  This is handled in 'plt_far_entry()'
346 	 */
347 	if ((pltndx - 1 + M_PLT_XNumber) >= M64_PLT_NEARPLTS) {
348 		plt_far_entry(ofl, pltndx, roffset, raddend);
349 		return;
350 	}
351 
352 	pltoff = M_PLT_RESERVSZ + (pltndx - 1) * M_PLT_ENTSIZE;
353 	pltent = (uchar_t *)ofl->ofl_osplt->os_outdata->d_buf + pltoff;
354 
355 	*roffset = pltoff + (Xword)(ofl->ofl_osplt->os_shdr->sh_addr);
356 	*raddend = 0;
357 
358 	/*
359 	 * PLT[0]: sethi %hi(. - .L0), %g1
360 	 */
361 	/* LINTED */
362 	*(Word *)pltent = M_SETHIG1 | pltoff;
363 
364 	/*
365 	 * PLT[1]: ba,a %xcc, .PLT1 (.PLT1 accessed as a
366 	 * PC-relative index of longwords).
367 	 */
368 	pltent += M_PLT_INSSIZE;
369 	pltoff += M_PLT_INSSIZE;
370 	pltoff = -pltoff;
371 	/* LINTED */
372 	*(Word *)pltent = M_BA_A_XCC |
373 	    (((pltoff + M_PLT_ENTSIZE) >> 2) & S_MASK(19));
374 
375 	/*
376 	 * PLT[2]: sethi 0, %g0 (NOP for delay slot of eventual CTI).
377 	 */
378 	pltent += M_PLT_INSSIZE;
379 	/* LINTED */
380 	*(Word *)pltent = M_NOP;
381 
382 	/*
383 	 * PLT[3]: sethi 0, %g0 (NOP for PLT padding).
384 	 */
385 	pltent += M_PLT_INSSIZE;
386 	/* LINTED */
387 	*(Word *)pltent = M_NOP;
388 
389 	/*
390 	 * PLT[4]: sethi 0, %g0 (NOP for PLT padding).
391 	 */
392 	pltent += M_PLT_INSSIZE;
393 	/* LINTED */
394 	*(Word *)pltent = M_NOP;
395 
396 	/*
397 	 * PLT[5]: sethi 0, %g0 (NOP for PLT padding).
398 	 */
399 	pltent += M_PLT_INSSIZE;
400 	/* LINTED */
401 	*(Word *)pltent = M_NOP;
402 
403 	/*
404 	 * PLT[6]: sethi 0, %g0 (NOP for PLT padding).
405 	 */
406 	pltent += M_PLT_INSSIZE;
407 	/* LINTED */
408 	*(Word *)pltent = M_NOP;
409 
410 	/*
411 	 * PLT[7]: sethi 0, %g0 (NOP for PLT padding).
412 	 */
413 	pltent += M_PLT_INSSIZE;
414 	/* LINTED */
415 	*(Word *)pltent = M_NOP;
416 }
417 
418 
419 #else  /* Elf 32 */
420 
421 Xword
422 ld_calc_plt_addr(Sym_desc *sdp, Ofl_desc *ofl)
423 {
424 	Xword	value, pltndx;
425 
426 	pltndx = sdp->sd_aux->sa_PLTndx + M_PLT_XNumber - 1;
427 	value = (Xword)(ofl->ofl_osplt->os_shdr->sh_addr) +
428 	    (pltndx * M_PLT_ENTSIZE);
429 	return (value);
430 }
431 
432 
433 /*
434  *	Build a single P.L.T. entry - code is:
435  *
436  *	sethi	(. - .L0), %g1
437  *	ba,a	.L0
438  *	sethi	0, %g0		(nop)
439  *
440  *	.L0 is the address of the first entry in the P.L.T.
441  *	This one is filled in by the run-time link editor. We just
442  *	have to leave space for it.
443  */
444 static void
445 plt_entry(Ofl_desc * ofl, Xword pltndx, Xword *roffset, Sxword *raddend)
446 {
447 	Byte *	pltent;	/* PLT entry being created. */
448 	Sxword	pltoff;	/* Offset of this entry from PLT top */
449 
450 	pltoff = M_PLT_RESERVSZ + (pltndx - 1) * M_PLT_ENTSIZE;
451 	pltent = (Byte *)ofl->ofl_osplt->os_outdata->d_buf + pltoff;
452 
453 	*roffset = pltoff + (Xword)(ofl->ofl_osplt->os_shdr->sh_addr);
454 	*raddend = 0;
455 
456 	/*
457 	 * PLT[0]: sethi %hi(. - .L0), %g1
458 	 */
459 	/* LINTED */
460 	*(Word *)pltent = M_SETHIG1 | pltoff;
461 
462 	/*
463 	 * PLT[1]: ba,a .L0 (.L0 accessed as a PC-relative index of longwords)
464 	 */
465 	pltent += M_PLT_INSSIZE;
466 	pltoff += M_PLT_INSSIZE;
467 	pltoff = -pltoff;
468 	/* LINTED */
469 	*(Word *)pltent = M_BA_A | ((pltoff >> 2) & S_MASK(22));
470 
471 	/*
472 	 * PLT[2]: sethi 0, %g0 (NOP for delay slot of eventual CTI).
473 	 */
474 	pltent += M_PLT_INSSIZE;
475 	/* LINTED */
476 	*(Word *)pltent = M_SETHIG0;
477 
478 	/*
479 	 * PLT[3]: sethi 0, %g0 (NOP for PLT padding).
480 	 */
481 	pltent += M_PLT_INSSIZE;
482 	/* LINTED */
483 	*(Word *)pltent = M_SETHIG0;
484 }
485 
486 #endif /* _ELF64 */
487 
488 uintptr_t
489 ld_perform_outreloc(Rel_desc * orsp, Ofl_desc * ofl)
490 {
491 	Os_desc *		relosp, * osp = 0;
492 	Xword			ndx, roffset, value;
493 	Sxword			raddend;
494 	const Rel_entry *	rep;
495 	Rela			rea;
496 	char			*relbits;
497 	Sym_desc *		sdp, * psym = (Sym_desc *)0;
498 	int			sectmoved = 0;
499 	Word			dtflags1 = ofl->ofl_dtflags_1;
500 	Word			flags = ofl->ofl_flags;
501 
502 	raddend = orsp->rel_raddend;
503 	sdp = orsp->rel_sym;
504 
505 	/*
506 	 * Special case, a regsiter symbol associated with symbol
507 	 * index 0 is initialized (i.e. relocated) to a constant
508 	 * in the r_addend field rather than to a symbol value.
509 	 */
510 	if ((orsp->rel_rtype == M_R_REGISTER) && !sdp) {
511 		relosp = ofl->ofl_osrel;
512 		relbits = (char *)relosp->os_outdata->d_buf;
513 
514 		rea.r_info = ELF_R_INFO(0,
515 		    ELF_R_TYPE_INFO(orsp->rel_typedata, orsp->rel_rtype));
516 		rea.r_offset = orsp->rel_roffset;
517 		rea.r_addend = raddend;
518 		DBG_CALL(Dbg_reloc_out(ofl, ELF_DBG_LD, SHT_RELA, &rea,
519 		    relosp->os_name, orsp->rel_sname));
520 
521 		assert(relosp->os_szoutrels <= relosp->os_shdr->sh_size);
522 		(void) memcpy((relbits + relosp->os_szoutrels),
523 		    (char *)&rea, sizeof (Rela));
524 		relosp->os_szoutrels += (Xword)sizeof (Rela);
525 
526 		return (1);
527 	}
528 
529 	/*
530 	 * If the section this relocation is against has been discarded
531 	 * (-zignore), then also discard (skip) the relocation itself.
532 	 */
533 	if (orsp->rel_isdesc && ((orsp->rel_flags &
534 	    (FLG_REL_GOT | FLG_REL_BSS | FLG_REL_PLT | FLG_REL_NOINFO)) == 0) &&
535 	    (orsp->rel_isdesc->is_flags & FLG_IS_DISCARD)) {
536 		DBG_CALL(Dbg_reloc_discard(ofl->ofl_lml, M_MACH, orsp));
537 		return (1);
538 	}
539 
540 	/*
541 	 * If this is a relocation against a move table, or expanded move
542 	 * table, adjust the relocation entries.
543 	 */
544 	if (orsp->rel_move)
545 		ld_adj_movereloc(ofl, orsp);
546 
547 	/*
548 	 * If this is a relocation against a section then we need to adjust the
549 	 * raddend field to compensate for the new position of the input section
550 	 * within the new output section.
551 	 */
552 	if (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION) {
553 		if (ofl->ofl_parsym.head &&
554 		    (sdp->sd_isc->is_flags & FLG_IS_RELUPD) &&
555 		    (psym = ld_am_I_partial(orsp, orsp->rel_raddend))) {
556 			/*
557 			 * If the symbol is moved, adjust the value
558 			 */
559 			DBG_CALL(Dbg_move_outsctadj(ofl->ofl_lml, psym));
560 			sectmoved = 1;
561 			if (ofl->ofl_flags & FLG_OF_RELOBJ)
562 				raddend = psym->sd_sym->st_value;
563 			else
564 				raddend = psym->sd_sym->st_value -
565 				    psym->sd_isc->is_osdesc->os_shdr->sh_addr;
566 			/* LINTED */
567 			raddend += (Off)_elf_getxoff(psym->sd_isc->is_indata);
568 			if (psym->sd_isc->is_shdr->sh_flags & SHF_ALLOC)
569 				raddend +=
570 				    psym->sd_isc->is_osdesc->os_shdr->sh_addr;
571 		} else {
572 			/* LINTED */
573 			raddend += (Off)_elf_getxoff(sdp->sd_isc->is_indata);
574 			if (sdp->sd_isc->is_shdr->sh_flags & SHF_ALLOC)
575 				raddend +=
576 				    sdp->sd_isc->is_osdesc->os_shdr->sh_addr;
577 		}
578 	}
579 
580 	value = sdp->sd_sym->st_value;
581 
582 	if (orsp->rel_flags & FLG_REL_GOT) {
583 		osp = ofl->ofl_osgot;
584 		roffset = ld_calc_got_offset(orsp, ofl);
585 
586 	} else if (orsp->rel_flags & FLG_REL_PLT) {
587 		osp = ofl->ofl_osplt;
588 		plt_entry(ofl, sdp->sd_aux->sa_PLTndx, &roffset, &raddend);
589 	} else if (orsp->rel_flags & FLG_REL_BSS) {
590 		/*
591 		 * This must be a R_SPARC_COPY.  For these set the roffset to
592 		 * point to the new symbols location.
593 		 */
594 		osp = ofl->ofl_isbss->is_osdesc;
595 		roffset = (Xword)value;
596 
597 		/*
598 		 * The raddend doesn't mean anything in an R_SPARC_COPY
599 		 * relocation.  Null it out because it can confuse people.
600 		 */
601 		raddend = 0;
602 	} else if (orsp->rel_flags & FLG_REL_REG) {
603 		/*
604 		 * The offsets of relocations against register symbols
605 		 * identifiy the register directly - so the offset
606 		 * does not need to be adjusted.
607 		 */
608 		roffset = orsp->rel_roffset;
609 	} else {
610 		osp = orsp->rel_osdesc;
611 
612 		/*
613 		 * Calculate virtual offset of reference point; equals offset
614 		 * into section + vaddr of section for loadable sections, or
615 		 * offset plus section displacement for nonloadable sections.
616 		 */
617 		roffset = orsp->rel_roffset +
618 		    (Off)_elf_getxoff(orsp->rel_isdesc->is_indata);
619 		if (!(ofl->ofl_flags & FLG_OF_RELOBJ))
620 			roffset += orsp->rel_isdesc->is_osdesc->
621 			    os_shdr->sh_addr;
622 	}
623 
624 	if ((osp == 0) || ((relosp = osp->os_relosdesc) == 0))
625 		relosp = ofl->ofl_osrel;
626 
627 	/*
628 	 * Verify that the output relocations offset meets the
629 	 * alignment requirements of the relocation being processed.
630 	 */
631 	rep = &reloc_table[orsp->rel_rtype];
632 	if (((flags & FLG_OF_RELOBJ) || !(dtflags1 & DF_1_NORELOC)) &&
633 	    !(rep->re_flags & FLG_RE_UNALIGN)) {
634 		if (((rep->re_fsize == 2) && (roffset & 0x1)) ||
635 		    ((rep->re_fsize == 4) && (roffset & 0x3)) ||
636 		    ((rep->re_fsize == 8) && (roffset & 0x7))) {
637 			Conv_inv_buf_t inv_buf;
638 
639 			eprintf(ofl->ofl_lml, ERR_FATAL,
640 			    MSG_INTL(MSG_REL_NONALIGN),
641 			    conv_reloc_SPARC_type(orsp->rel_rtype, 0, &inv_buf),
642 			    orsp->rel_isdesc->is_file->ifl_name,
643 			    demangle(orsp->rel_sname), EC_XWORD(roffset));
644 			return (S_ERROR);
645 		}
646 	}
647 
648 	/*
649 	 * Assign the symbols index for the output relocation.  If the
650 	 * relocation refers to a SECTION symbol then it's index is based upon
651 	 * the output sections symbols index.  Otherwise the index can be
652 	 * derived from the symbols index itself.
653 	 */
654 	if (orsp->rel_rtype == R_SPARC_RELATIVE)
655 		ndx = STN_UNDEF;
656 	else if ((orsp->rel_flags & FLG_REL_SCNNDX) ||
657 	    (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION)) {
658 		if (sectmoved == 0) {
659 			/*
660 			 * Check for a null input section. This can
661 			 * occur if this relocation references a symbol
662 			 * generated by sym_add_sym().
663 			 */
664 			if ((sdp->sd_isc != 0) &&
665 			    (sdp->sd_isc->is_osdesc != 0))
666 				ndx = sdp->sd_isc->is_osdesc->os_scnsymndx;
667 			else
668 				ndx = sdp->sd_shndx;
669 		} else
670 			ndx = ofl->ofl_sunwdata1ndx;
671 	} else
672 		ndx = sdp->sd_symndx;
673 
674 	/*
675 	 * Add the symbols 'value' to the addend field.
676 	 */
677 	if (orsp->rel_flags & FLG_REL_ADVAL)
678 		raddend += value;
679 
680 	/*
681 	 * The addend field for R_SPARC_TLS_DTPMOD32 and R_SPARC_TLS_DTPMOD64
682 	 * mean nothing.  The addend is propagated in the corresponding
683 	 * R_SPARC_TLS_DTPOFF* relocations.
684 	 */
685 	if (orsp->rel_rtype == M_R_DTPMOD)
686 		raddend = 0;
687 
688 	relbits = (char *)relosp->os_outdata->d_buf;
689 
690 	rea.r_info = ELF_R_INFO(ndx, ELF_R_TYPE_INFO(orsp->rel_typedata,
691 	    orsp->rel_rtype));
692 	rea.r_offset = roffset;
693 	rea.r_addend = raddend;
694 	DBG_CALL(Dbg_reloc_out(ofl, ELF_DBG_LD, SHT_RELA, &rea, relosp->os_name,
695 	    orsp->rel_sname));
696 
697 	/*
698 	 * Assert we haven't walked off the end of our relocation table.
699 	 */
700 	assert(relosp->os_szoutrels <= relosp->os_shdr->sh_size);
701 
702 	(void) memcpy((relbits + relosp->os_szoutrels),
703 	    (char *)&rea, sizeof (Rela));
704 	relosp->os_szoutrels += (Xword)sizeof (Rela);
705 
706 	/*
707 	 * Determine if this relocation is against a non-writable, allocatable
708 	 * section.  If so we may need to provide a text relocation diagnostic.
709 	 */
710 	ld_reloc_remain_entry(orsp, osp, ofl);
711 	return (1);
712 }
713 
714 
715 /*
716  * Sparc Instructions for TLS processing
717  */
718 #if	defined(_ELF64)
719 #define	TLS_GD_IE_LD	0xd0580000	/* ldx [%g0 + %g0], %o0 */
720 #else
721 #define	TLS_GD_IE_LD	0xd0000000	/* ld [%g0 + %g0], %o0 */
722 #endif
723 #define	TLS_GD_IE_ADD	0x9001c008	/* add %g7, %o0, %o0 */
724 
725 #define	TLS_GD_LE_XOR	0x80182000	/* xor %g0, 0, %g0 */
726 #define	TLS_IE_LE_OR	0x80100000	/* or %g0, %o0, %o1 */
727 					/*  synthetic: mov %g0, %g0 */
728 
729 #define	TLS_LD_LE_CLRO0	0x90100000	/* clr	%o0 */
730 
731 #define	FM3_REG_MSK_RD	(0x1f << 25)	/* Formate (3) rd register mask */
732 					/*	bits 25->29 */
733 #define	FM3_REG_MSK_RS1	(0x1f << 14)	/* Formate (3) rs1 register mask */
734 					/*	bits 14->18 */
735 #define	FM3_REG_MSK_RS2	0x1f		/* Formate (3) rs2 register mask */
736 					/*	bits 0->4 */
737 
738 #define	REG_G7		7		/* %g7 register */
739 
740 static Fixupret
741 tls_fixups(Ofl_desc *ofl, Rel_desc *arsp)
742 {
743 	Sym_desc	*sdp = arsp->rel_sym;
744 	Word		rtype = arsp->rel_rtype;
745 	uint_t		*offset;
746 
747 	offset = (uint_t *)((uintptr_t)arsp->rel_roffset +
748 	    (uintptr_t)_elf_getxoff(arsp->rel_isdesc->is_indata) +
749 	    (uintptr_t)arsp->rel_osdesc->os_outdata->d_buf);
750 
751 	if (sdp->sd_ref == REF_DYN_NEED) {
752 		/*
753 		 * IE reference model
754 		 */
755 		switch (rtype) {
756 		case R_SPARC_TLS_GD_HI22:
757 			DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
758 			    R_SPARC_TLS_IE_HI22, arsp));
759 			arsp->rel_rtype = R_SPARC_TLS_IE_HI22;
760 			return (FIX_RELOC);
761 
762 		case R_SPARC_TLS_GD_LO10:
763 			DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
764 			    R_SPARC_TLS_IE_LO10, arsp));
765 			arsp->rel_rtype = R_SPARC_TLS_IE_LO10;
766 			return (FIX_RELOC);
767 
768 		case R_SPARC_TLS_GD_ADD:
769 			DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
770 			    R_SPARC_NONE, arsp));
771 			*offset = (TLS_GD_IE_LD |
772 			    (*offset & (FM3_REG_MSK_RS1 | FM3_REG_MSK_RS2)));
773 			return (FIX_DONE);
774 
775 		case R_SPARC_TLS_GD_CALL:
776 			DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
777 			    R_SPARC_NONE, arsp));
778 			*offset = TLS_GD_IE_ADD;
779 			return (FIX_DONE);
780 		}
781 		return (FIX_RELOC);
782 	}
783 
784 	/*
785 	 * LE reference model
786 	 */
787 	switch (rtype) {
788 	case R_SPARC_TLS_IE_HI22:
789 	case R_SPARC_TLS_GD_HI22:
790 	case R_SPARC_TLS_LDO_HIX22:
791 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
792 		    R_SPARC_TLS_LE_HIX22, arsp));
793 		arsp->rel_rtype = R_SPARC_TLS_LE_HIX22;
794 		return (FIX_RELOC);
795 
796 	case R_SPARC_TLS_LDO_LOX10:
797 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
798 		    R_SPARC_TLS_LE_LOX10, arsp));
799 		arsp->rel_rtype = R_SPARC_TLS_LE_LOX10;
800 		return (FIX_RELOC);
801 
802 	case R_SPARC_TLS_IE_LO10:
803 	case R_SPARC_TLS_GD_LO10:
804 		/*
805 		 * Current instruction is:
806 		 *
807 		 *	or r1, %lo(x), r2
808 		 *		or
809 		 *	add r1, %lo(x), r2
810 		 *
811 		 *
812 		 * Need to udpate this to:
813 		 *
814 		 *	xor r1, %lox(x), r2
815 		 */
816 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
817 		    R_SPARC_TLS_LE_LOX10, arsp));
818 		*offset = TLS_GD_LE_XOR |
819 		    (*offset & (FM3_REG_MSK_RS1 | FM3_REG_MSK_RD));
820 		arsp->rel_rtype = R_SPARC_TLS_LE_LOX10;
821 		return (FIX_RELOC);
822 
823 	case R_SPARC_TLS_IE_LD:
824 	case R_SPARC_TLS_IE_LDX:
825 		/*
826 		 * Current instruction:
827 		 * 	ld{x}	[r1 + r2], r3
828 		 *
829 		 * Need to update this to:
830 		 *
831 		 *	mov	r2, r3   (or  %g0, r2, r3)
832 		 */
833 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
834 		    R_SPARC_NONE, arsp));
835 		*offset = ((*offset) & (FM3_REG_MSK_RS2 | FM3_REG_MSK_RD)) |
836 		    TLS_IE_LE_OR;
837 		return (FIX_DONE);
838 
839 	case R_SPARC_TLS_LDO_ADD:
840 	case R_SPARC_TLS_GD_ADD:
841 		/*
842 		 * Current instruction is:
843 		 *
844 		 *	add gptr_reg, r2, r3
845 		 *
846 		 * Need to updated this to:
847 		 *
848 		 *	add %g7, r2, r3
849 		 */
850 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
851 		    R_SPARC_NONE, arsp));
852 		*offset = *offset & (~FM3_REG_MSK_RS1);
853 		*offset = *offset | (REG_G7 << 14);
854 		return (FIX_DONE);
855 
856 	case R_SPARC_TLS_LDM_CALL:
857 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
858 		    R_SPARC_NONE, arsp));
859 		*offset = TLS_LD_LE_CLRO0;
860 		return (FIX_DONE);
861 
862 	case R_SPARC_TLS_LDM_HI22:
863 	case R_SPARC_TLS_LDM_LO10:
864 	case R_SPARC_TLS_LDM_ADD:
865 	case R_SPARC_TLS_IE_ADD:
866 	case R_SPARC_TLS_GD_CALL:
867 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
868 		    R_SPARC_NONE, arsp));
869 		*offset = M_NOP;
870 		return (FIX_DONE);
871 	}
872 	return (FIX_RELOC);
873 }
874 
875 #define	GOTOP_ADDINST	0x80000000	/* add %g0, %g0, %g0 */
876 
877 static Fixupret
878 gotop_fixups(Ofl_desc *ofl, Rel_desc *arsp)
879 {
880 	Word		rtype = arsp->rel_rtype;
881 	uint_t		*offset;
882 	const char	*ifl_name;
883 	Conv_inv_buf_t	inv_buf;
884 
885 	switch (rtype) {
886 	case R_SPARC_GOTDATA_OP_HIX22:
887 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
888 		    R_SPARC_GOTDATA_HIX22, arsp));
889 		arsp->rel_rtype = R_SPARC_GOTDATA_HIX22;
890 		return (FIX_RELOC);
891 
892 	case R_SPARC_GOTDATA_OP_LOX10:
893 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
894 		    R_SPARC_GOTDATA_LOX10, arsp));
895 		arsp->rel_rtype = R_SPARC_GOTDATA_LOX10;
896 		return (FIX_RELOC);
897 
898 	case R_SPARC_GOTDATA_OP:
899 		/*
900 		 * Current instruction:
901 		 * 	ld{x}	[r1 + r2], r3
902 		 *
903 		 * Need to update this to:
904 		 *
905 		 *	add	r1, r2, r3
906 		 */
907 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
908 		    R_SPARC_NONE, arsp));
909 		offset = (uint_t *)(uintptr_t)(arsp->rel_roffset +
910 		    _elf_getxoff(arsp->rel_isdesc->is_indata) +
911 		    (uintptr_t)arsp->rel_osdesc->os_outdata->d_buf);
912 
913 		*offset = ((*offset) & (FM3_REG_MSK_RS1 |
914 		    FM3_REG_MSK_RS2 | FM3_REG_MSK_RD)) | GOTOP_ADDINST;
915 		return (FIX_DONE);
916 	}
917 	/*
918 	 * We should not get here
919 	 */
920 	if (arsp->rel_isdesc->is_file)
921 		ifl_name = arsp->rel_isdesc->is_file->ifl_name;
922 	else
923 		ifl_name = MSG_INTL(MSG_STR_NULL);
924 
925 	eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_REL_BADGOTFIX),
926 	    conv_reloc_SPARC_type(arsp->rel_rtype, 0, &inv_buf),
927 	    ifl_name, demangle(arsp->rel_sname));
928 
929 	assert(0);
930 	return (FIX_ERROR);
931 }
932 
933 uintptr_t
934 ld_do_activerelocs(Ofl_desc *ofl)
935 {
936 	Rel_desc	*arsp;
937 	Rel_cache	*rcp;
938 	Listnode	*lnp;
939 	uintptr_t	return_code = 1;
940 	Word		flags = ofl->ofl_flags;
941 	Word		dtflags1 = ofl->ofl_dtflags_1;
942 
943 	if (ofl->ofl_actrels.head)
944 		DBG_CALL(Dbg_reloc_doact_title(ofl->ofl_lml));
945 
946 	/*
947 	 * Process active relocations.
948 	 */
949 	for (LIST_TRAVERSE(&ofl->ofl_actrels, lnp, rcp)) {
950 		/* LINTED */
951 		for (arsp = (Rel_desc *)(rcp + 1);
952 		    arsp < rcp->rc_free; arsp++) {
953 			uchar_t		*addr;
954 			Xword		value;
955 			Sym_desc	*sdp;
956 			const char	*ifl_name;
957 			Xword		refaddr;
958 
959 			/*
960 			 * If the section this relocation is against has been
961 			 * discarded (-zignore), then discard (skip) the
962 			 * relocation itself.
963 			 */
964 			if ((arsp->rel_isdesc->is_flags & FLG_IS_DISCARD) &&
965 			    ((arsp->rel_flags &
966 			    (FLG_REL_GOT | FLG_REL_BSS |
967 			    FLG_REL_PLT | FLG_REL_NOINFO)) == 0)) {
968 				DBG_CALL(Dbg_reloc_discard(ofl->ofl_lml,
969 				    M_MACH, arsp));
970 				continue;
971 			}
972 
973 			/*
974 			 * Perform any required TLS fixups.
975 			 */
976 			if (arsp->rel_flags & FLG_REL_TLSFIX) {
977 				Fixupret	ret;
978 
979 				if ((ret = tls_fixups(ofl, arsp)) == FIX_ERROR)
980 					return (S_ERROR);
981 				if (ret == FIX_DONE)
982 					continue;
983 			}
984 
985 			/*
986 			 * Perform any required GOTOP fixups.
987 			 */
988 			if (arsp->rel_flags & FLG_REL_GOTFIX) {
989 				Fixupret	ret;
990 
991 				if ((ret =
992 				    gotop_fixups(ofl, arsp)) == FIX_ERROR)
993 					return (S_ERROR);
994 				if (ret == FIX_DONE)
995 					continue;
996 			}
997 
998 			/*
999 			 * If this is a relocation against the move table, or
1000 			 * expanded move table, adjust the relocation entries.
1001 			 */
1002 			if (arsp->rel_move)
1003 				ld_adj_movereloc(ofl, arsp);
1004 
1005 			sdp = arsp->rel_sym;
1006 			refaddr = arsp->rel_roffset +
1007 			    (Off)_elf_getxoff(arsp->rel_isdesc->is_indata);
1008 
1009 			if ((arsp->rel_flags & FLG_REL_CLVAL) ||
1010 			    (arsp->rel_flags & FLG_REL_GOTCL))
1011 				value = 0;
1012 			else if (ELF_ST_TYPE(sdp->sd_sym->st_info) ==
1013 			    STT_SECTION) {
1014 				Sym_desc	*sym;
1015 
1016 				/*
1017 				 * The value for a symbol pointing to a SECTION
1018 				 * is based off of that sections position.
1019 				 */
1020 				if ((sdp->sd_isc->is_flags & FLG_IS_RELUPD) &&
1021 				    (sym = ld_am_I_partial(arsp,
1022 				    arsp->rel_roffset))) {
1023 					/*
1024 					 * If the symbol is moved,
1025 					 * adjust the value
1026 					 */
1027 					value = _elf_getxoff(
1028 					    sym->sd_isc->is_indata);
1029 					if (sym->sd_isc->is_shdr->sh_flags &
1030 					    SHF_ALLOC)
1031 						value += sym->sd_isc->
1032 						    is_osdesc->os_shdr->sh_addr;
1033 				} else {
1034 					value = _elf_getxoff(
1035 					    sdp->sd_isc->is_indata);
1036 					if (sdp->sd_isc->is_shdr->sh_flags &
1037 					    SHF_ALLOC)
1038 						value += sdp->sd_isc->
1039 						    is_osdesc->os_shdr->sh_addr;
1040 				}
1041 
1042 				if (sdp->sd_isc->is_shdr->sh_flags & SHF_TLS)
1043 					value -= ofl->ofl_tlsphdr->p_vaddr;
1044 
1045 			} else if (IS_SIZE(arsp->rel_rtype)) {
1046 				/*
1047 				 * Size relocations require the symbols size.
1048 				 */
1049 				value = sdp->sd_sym->st_size;
1050 			} else {
1051 				/*
1052 				 * Else the value is the symbols value.
1053 				 */
1054 				value = sdp->sd_sym->st_value;
1055 			}
1056 
1057 			/*
1058 			 * Relocation against the GLOBAL_OFFSET_TABLE.
1059 			 */
1060 			if (arsp->rel_flags & FLG_REL_GOT)
1061 				arsp->rel_osdesc = ofl->ofl_osgot;
1062 
1063 			/*
1064 			 * If loadable and not producing a relocatable object
1065 			 * add the sections virtual address to the reference
1066 			 * address.
1067 			 */
1068 			if ((arsp->rel_flags & FLG_REL_LOAD) &&
1069 			    ((flags & FLG_OF_RELOBJ) == 0))
1070 				refaddr += arsp->rel_isdesc->is_osdesc->
1071 				    os_shdr->sh_addr;
1072 
1073 			/*
1074 			 * If this entry has a PLT assigned to it, it's
1075 			 * value is actually the address of the PLT (and
1076 			 * not the address of the function).
1077 			 */
1078 			if (IS_PLT(arsp->rel_rtype)) {
1079 				if (sdp->sd_aux && sdp->sd_aux->sa_PLTndx)
1080 					value = ld_calc_plt_addr(sdp, ofl);
1081 			}
1082 
1083 			/*
1084 			 * Add relocations addend to value.  Add extra
1085 			 * relocation addend if needed.
1086 			 */
1087 			value += arsp->rel_raddend;
1088 			if (IS_EXTOFFSET(arsp->rel_rtype))
1089 				value += arsp->rel_typedata;
1090 
1091 			/*
1092 			 * Determine whether the value needs further adjustment.
1093 			 * Filter through the attributes of the relocation to
1094 			 * determine what adjustment is required.  Note, many
1095 			 * of the following cases are only applicable when a
1096 			 * .got is present.  As a .got is not generated when a
1097 			 * relocatable object is being built, any adjustments
1098 			 * that require a .got need to be skipped.
1099 			 */
1100 			if ((arsp->rel_flags & FLG_REL_GOT) &&
1101 			    ((flags & FLG_OF_RELOBJ) == 0)) {
1102 				Xword		R1addr;
1103 				uintptr_t	R2addr;
1104 				Sword		gotndx;
1105 				Gotndx		*gnp;
1106 				Gotref		gref;
1107 
1108 				/*
1109 				 * Clear the GOT table entry, on SPARC we clear
1110 				 * the entry and the 'value' if needed is stored
1111 				 * in an output relocations addend.
1112 				 *
1113 				 * Calculate offset into GOT at which to apply
1114 				 * the relocation.
1115 				 */
1116 				if (arsp->rel_flags & FLG_REL_DTLS)
1117 					gref = GOT_REF_TLSGD;
1118 				else if (arsp->rel_flags & FLG_REL_MTLS)
1119 					gref = GOT_REF_TLSLD;
1120 				else if (arsp->rel_flags & FLG_REL_STLS)
1121 					gref = GOT_REF_TLSIE;
1122 				else
1123 					gref = GOT_REF_GENERIC;
1124 
1125 				gnp = ld_find_gotndx(&(sdp->sd_GOTndxs), gref,
1126 				    ofl, arsp);
1127 				assert(gnp);
1128 
1129 				if (arsp->rel_rtype == M_R_DTPOFF)
1130 					gotndx = gnp->gn_gotndx + 1;
1131 				else
1132 					gotndx = gnp->gn_gotndx;
1133 
1134 				/* LINTED */
1135 				R1addr = (Xword)((-neggotoffset *
1136 				    M_GOT_ENTSIZE) + (gotndx * M_GOT_ENTSIZE));
1137 
1138 				/*
1139 				 * Add the GOTs data's offset.
1140 				 */
1141 				R2addr = R1addr + (uintptr_t)
1142 				    arsp->rel_osdesc->os_outdata->d_buf;
1143 
1144 				DBG_CALL(Dbg_reloc_doact(ofl->ofl_lml,
1145 				    ELF_DBG_LD, M_MACH, SHT_RELA,
1146 				    arsp->rel_rtype, R1addr, value,
1147 				    arsp->rel_sname, arsp->rel_osdesc));
1148 
1149 				/*
1150 				 * And do it.
1151 				 */
1152 				*(Xword *)R2addr = value;
1153 				continue;
1154 
1155 			} else if (IS_GOT_BASED(arsp->rel_rtype) &&
1156 			    ((flags & FLG_OF_RELOBJ) == 0)) {
1157 				value -= (ofl->ofl_osgot->os_shdr->sh_addr +
1158 				    (-neggotoffset * M_GOT_ENTSIZE));
1159 
1160 			} else if (IS_PC_RELATIVE(arsp->rel_rtype)) {
1161 				value -= refaddr;
1162 
1163 			} else if (IS_TLS_INS(arsp->rel_rtype) &&
1164 			    IS_GOT_RELATIVE(arsp->rel_rtype) &&
1165 			    ((flags & FLG_OF_RELOBJ) == 0)) {
1166 				Gotndx	*gnp;
1167 				Gotref	gref;
1168 
1169 				if (arsp->rel_flags & FLG_REL_STLS)
1170 					gref = GOT_REF_TLSIE;
1171 				else if (arsp->rel_flags & FLG_REL_DTLS)
1172 					gref = GOT_REF_TLSGD;
1173 				else if (arsp->rel_flags & FLG_REL_MTLS)
1174 					gref = GOT_REF_TLSLD;
1175 
1176 				gnp = ld_find_gotndx(&(sdp->sd_GOTndxs), gref,
1177 				    ofl, arsp);
1178 				assert(gnp);
1179 
1180 				value = gnp->gn_gotndx * M_GOT_ENTSIZE;
1181 
1182 			} else if (IS_GOT_RELATIVE(arsp->rel_rtype) &&
1183 			    ((flags & FLG_OF_RELOBJ) == 0)) {
1184 				Gotndx	*gnp;
1185 
1186 				gnp = ld_find_gotndx(&(sdp->sd_GOTndxs),
1187 				    GOT_REF_GENERIC, ofl, arsp);
1188 				assert(gnp);
1189 
1190 				value = gnp->gn_gotndx * M_GOT_ENTSIZE;
1191 
1192 			} else if ((arsp->rel_flags & FLG_REL_STLS) &&
1193 			    ((flags & FLG_OF_RELOBJ) == 0)) {
1194 				Xword	tlsstatsize;
1195 
1196 				/*
1197 				 * This is the LE TLS
1198 				 * reference model.  Static offset
1199 				 * is hard-coded, and negated so that
1200 				 * it can be added to the thread pointer (%g7)
1201 				 */
1202 				tlsstatsize = S_ROUND(ofl->
1203 				    ofl_tlsphdr->p_memsz, M_TLSSTATALIGN);
1204 				value = -(tlsstatsize - value);
1205 			}
1206 
1207 			if (arsp->rel_isdesc->is_file)
1208 				ifl_name = arsp->rel_isdesc->is_file->ifl_name;
1209 			else
1210 				ifl_name = MSG_INTL(MSG_STR_NULL);
1211 
1212 			/*
1213 			 * Make sure we have data to relocate.  Compiler and
1214 			 * assembler developers have been known to generate
1215 			 * relocations against invalid sections (normally .bss),
1216 			 * so for their benefit give them sufficient information
1217 			 * to help analyze the problem.  End users should never
1218 			 * see this.
1219 			 */
1220 			if (arsp->rel_isdesc->is_indata->d_buf == 0) {
1221 				Conv_inv_buf_t	inv_buf;
1222 
1223 				eprintf(ofl->ofl_lml, ERR_FATAL,
1224 				    MSG_INTL(MSG_REL_EMPTYSEC),
1225 				    conv_reloc_SPARC_type(arsp->rel_rtype,
1226 				    0, &inv_buf), ifl_name,
1227 				    demangle(arsp->rel_sname),
1228 				    arsp->rel_isdesc->is_name);
1229 				return (S_ERROR);
1230 			}
1231 
1232 			/*
1233 			 * Get the address of the data item we need to modify.
1234 			 */
1235 			addr = (uchar_t *)((uintptr_t)arsp->rel_roffset +
1236 			    (uintptr_t)_elf_getxoff(arsp->rel_isdesc->
1237 			    is_indata));
1238 
1239 			/*LINTED*/
1240 			DBG_CALL(Dbg_reloc_doact(ofl->ofl_lml, ELF_DBG_LD,
1241 			    M_MACH, SHT_RELA, arsp->rel_rtype, EC_NATPTR(addr),
1242 			    value, arsp->rel_sname, arsp->rel_osdesc));
1243 			addr += (uintptr_t)arsp->rel_osdesc->os_outdata->d_buf;
1244 
1245 			if ((((uintptr_t)addr - (uintptr_t)ofl->ofl_nehdr) >
1246 			    ofl->ofl_size) || (arsp->rel_roffset >
1247 			    arsp->rel_osdesc->os_shdr->sh_size)) {
1248 				Conv_inv_buf_t	inv_buf;
1249 				int		class;
1250 
1251 				if (((uintptr_t)addr -
1252 				    (uintptr_t)ofl->ofl_nehdr) > ofl->ofl_size)
1253 					class = ERR_FATAL;
1254 				else
1255 					class = ERR_WARNING;
1256 
1257 				eprintf(ofl->ofl_lml, class,
1258 				    MSG_INTL(MSG_REL_INVALOFFSET),
1259 				    conv_reloc_SPARC_type(arsp->rel_rtype,
1260 				    0, &inv_buf), ifl_name,
1261 				    arsp->rel_isdesc->is_name,
1262 				    demangle(arsp->rel_sname),
1263 				    EC_ADDR((uintptr_t)addr -
1264 				    (uintptr_t)ofl->ofl_nehdr));
1265 
1266 				if (class == ERR_FATAL) {
1267 					return_code = S_ERROR;
1268 					continue;
1269 				}
1270 			}
1271 
1272 			/*
1273 			 * If '-z noreloc' is specified - skip the do_reloc
1274 			 * stage.
1275 			 */
1276 			if ((flags & FLG_OF_RELOBJ) ||
1277 			    !(dtflags1 & DF_1_NORELOC)) {
1278 				if (do_reloc((uchar_t)arsp->rel_rtype, addr,
1279 				    &value, arsp->rel_sname, ifl_name,
1280 				    ofl->ofl_lml) == 0)
1281 					return_code = S_ERROR;
1282 			}
1283 		}
1284 	}
1285 	return (return_code);
1286 }
1287 
1288 uintptr_t
1289 ld_add_outrel(Word flags, Rel_desc *rsp, Ofl_desc *ofl)
1290 {
1291 	Rel_desc	*orsp;
1292 	Rel_cache	*rcp;
1293 	Sym_desc	*sdp = rsp->rel_sym;
1294 	Conv_inv_buf_t	inv_buf;
1295 
1296 	/*
1297 	 * Static executables *do not* want any relocations against them.
1298 	 * Since our engine still creates relocations against a WEAK UNDEFINED
1299 	 * symbol in a static executable, it's best to disable them here
1300 	 * instead of through out the relocation code.
1301 	 */
1302 	if ((ofl->ofl_flags & (FLG_OF_STATIC | FLG_OF_EXEC)) ==
1303 	    (FLG_OF_STATIC | FLG_OF_EXEC))
1304 		return (1);
1305 
1306 	/*
1307 	 * Certain relocations do not make sense in a 64bit shared object,
1308 	 * if building a shared object do a sanity check on the output
1309 	 * relocations being created.
1310 	 */
1311 	if (ofl->ofl_flags & FLG_OF_SHAROBJ) {
1312 		Word	rtype = rsp->rel_rtype;
1313 		/*
1314 		 * Because the R_SPARC_HIPLT22 & R_SPARC_LOPLT10 relocations
1315 		 * are not relative they make no sense to create in a shared
1316 		 * object - so emit the proper error message if that occurs.
1317 		 */
1318 		if ((rtype == R_SPARC_HIPLT22) || (rtype == R_SPARC_LOPLT10)) {
1319 			eprintf(ofl->ofl_lml, ERR_FATAL,
1320 			    MSG_INTL(MSG_REL_UNRELREL),
1321 			    conv_reloc_SPARC_type(rsp->rel_rtype, 0, &inv_buf),
1322 			    rsp->rel_isdesc->is_file->ifl_name,
1323 			    demangle(rsp->rel_sname));
1324 			return (S_ERROR);
1325 		}
1326 #if	defined(_ELF64)
1327 		/*
1328 		 * Each of the following relocations requires that the
1329 		 * object being built be loaded in either the upper 32 or
1330 		 * 44 bit range of memory.  Since shared libraries traditionally
1331 		 * are loaded in the lower range of memory - this isn't going
1332 		 * to work.
1333 		 */
1334 		if ((rtype == R_SPARC_H44) || (rtype == R_SPARC_M44) ||
1335 		    (rtype == R_SPARC_L44)) {
1336 			eprintf(ofl->ofl_lml, ERR_FATAL,
1337 			    MSG_INTL(MSG_REL_SHOBJABS44),
1338 			    conv_reloc_SPARC_type(rsp->rel_rtype, 0, &inv_buf),
1339 			    rsp->rel_isdesc->is_file->ifl_name,
1340 			    demangle(rsp->rel_sname));
1341 			return (S_ERROR);
1342 		}
1343 #endif
1344 	}
1345 
1346 	/*
1347 	 * If no relocation cache structures are available allocate
1348 	 * a new one and link it into the cache list.
1349 	 */
1350 	if ((ofl->ofl_outrels.tail == 0) ||
1351 	    ((rcp = (Rel_cache *)ofl->ofl_outrels.tail->data) == 0) ||
1352 	    ((orsp = rcp->rc_free) == rcp->rc_end)) {
1353 		static size_t	nextsize = 0;
1354 		size_t		size;
1355 
1356 		/*
1357 		 * Output relocation numbers can vary considerably between
1358 		 * building executables or shared objects (pic vs. non-pic),
1359 		 * etc.  But, they typically aren't very large, so for these
1360 		 * objects use a standard bucket size.  For building relocatable
1361 		 * objects, typically there will be an output relocation for
1362 		 * every input relocation.
1363 		 */
1364 		if (nextsize == 0) {
1365 			if (ofl->ofl_flags & FLG_OF_RELOBJ) {
1366 				if ((size = ofl->ofl_relocincnt) == 0)
1367 					size = REL_LOIDESCNO;
1368 				if (size > REL_HOIDESCNO)
1369 					nextsize = REL_HOIDESCNO;
1370 				else
1371 					nextsize = REL_LOIDESCNO;
1372 			} else
1373 				nextsize = size = REL_HOIDESCNO;
1374 		} else
1375 			size = nextsize;
1376 
1377 		size = size * sizeof (Rel_desc);
1378 
1379 		if (((rcp = libld_malloc(sizeof (Rel_cache) + size)) == 0) ||
1380 		    (list_appendc(&ofl->ofl_outrels, rcp) == 0))
1381 			return (S_ERROR);
1382 
1383 		/* LINTED */
1384 		rcp->rc_free = orsp = (Rel_desc *)(rcp + 1);
1385 		/* LINTED */
1386 		rcp->rc_end = (Rel_desc *)((char *)rcp->rc_free + size);
1387 	}
1388 
1389 
1390 	/*
1391 	 * If we are adding a output relocation against a section
1392 	 * symbol (non-RELATIVE) then mark that section.  These sections
1393 	 * will be added to the .dynsym symbol table.
1394 	 */
1395 	if (sdp && (rsp->rel_rtype != M_R_RELATIVE) &&
1396 	    ((flags & FLG_REL_SCNNDX) ||
1397 	    (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION))) {
1398 
1399 		/*
1400 		 * If this is a COMMON symbol - no output section
1401 		 * exists yet - (it's created as part of sym_validate()).
1402 		 * So - we mark here that when it's created it should
1403 		 * be tagged with the FLG_OS_OUTREL flag.
1404 		 */
1405 		if ((sdp->sd_flags & FLG_SY_SPECSEC) &&
1406 		    (sdp->sd_sym->st_shndx == SHN_COMMON)) {
1407 			if (ELF_ST_TYPE(sdp->sd_sym->st_info) != STT_TLS)
1408 				ofl->ofl_flags1 |= FLG_OF1_BSSOREL;
1409 			else
1410 				ofl->ofl_flags1 |= FLG_OF1_TLSOREL;
1411 		} else {
1412 			Os_desc	*osp = sdp->sd_isc->is_osdesc;
1413 
1414 			if (osp && ((osp->os_flags & FLG_OS_OUTREL) == 0)) {
1415 				ofl->ofl_dynshdrcnt++;
1416 				osp->os_flags |= FLG_OS_OUTREL;
1417 			}
1418 		}
1419 	}
1420 
1421 	*orsp = *rsp;
1422 	orsp->rel_flags |= flags;
1423 
1424 	rcp->rc_free++;
1425 	ofl->ofl_outrelscnt++;
1426 
1427 	if (flags & FLG_REL_GOT)
1428 		ofl->ofl_relocgotsz += (Xword)sizeof (Rela);
1429 	else if (flags & FLG_REL_PLT)
1430 		ofl->ofl_relocpltsz += (Xword)sizeof (Rela);
1431 	else if (flags & FLG_REL_BSS)
1432 		ofl->ofl_relocbsssz += (Xword)sizeof (Rela);
1433 	else if (flags & FLG_REL_NOINFO)
1434 		ofl->ofl_relocrelsz += (Xword)sizeof (Rela);
1435 	else
1436 		orsp->rel_osdesc->os_szoutrels += (Xword)sizeof (Rela);
1437 
1438 	if (orsp->rel_rtype == M_R_RELATIVE)
1439 		ofl->ofl_relocrelcnt++;
1440 
1441 #if	defined(_ELF64)
1442 	/*
1443 	 * When building a 64-bit object any R_SPARC_WDISP30 relocation is given
1444 	 * a plt padding entry, unless we're building a relocatable object
1445 	 * (ld -r) or -b is in effect.
1446 	 */
1447 	if ((orsp->rel_rtype == R_SPARC_WDISP30) &&
1448 	    ((ofl->ofl_flags & (FLG_OF_BFLAG | FLG_OF_RELOBJ)) == 0) &&
1449 	    ((orsp->rel_sym->sd_flags & FLG_SY_PLTPAD) == 0)) {
1450 		ofl->ofl_pltpad++;
1451 		orsp->rel_sym->sd_flags |= FLG_SY_PLTPAD;
1452 	}
1453 #endif
1454 	/*
1455 	 * We don't perform sorting on PLT relocations because
1456 	 * they have already been assigned a PLT index and if we
1457 	 * were to sort them we would have to re-assign the plt indexes.
1458 	 */
1459 	if (!(flags & FLG_REL_PLT))
1460 		ofl->ofl_reloccnt++;
1461 
1462 	/*
1463 	 * Insure a GLOBAL_OFFSET_TABLE is generated if required.
1464 	 */
1465 	if (IS_GOT_REQUIRED(orsp->rel_rtype))
1466 		ofl->ofl_flags |= FLG_OF_BLDGOT;
1467 
1468 	/*
1469 	 * Identify and possibly warn of a displacement relocation.
1470 	 */
1471 	if (orsp->rel_flags & FLG_REL_DISP) {
1472 		ofl->ofl_dtflags_1 |= DF_1_DISPRELPND;
1473 
1474 		if (ofl->ofl_flags & FLG_OF_VERBOSE)
1475 			ld_disp_errmsg(MSG_INTL(MSG_REL_DISPREL4), orsp, ofl);
1476 	}
1477 	DBG_CALL(Dbg_reloc_ors_entry(ofl->ofl_lml, ELF_DBG_LD, SHT_RELA,
1478 	    M_MACH, orsp));
1479 	return (1);
1480 }
1481 
1482 /*
1483  * Process relocation against a register symbol.  Note, of -z muldefs is in
1484  * effect there may have been multiple register definitions, which would have
1485  * been processed as non-fatal, with the first definition winning.  But, we
1486  * will also process multiple relocations for these multiple definitions.  In
1487  * this case we must only preserve the relocation for the definition that was
1488  * kept.  The sad part is that register relocations don't typically specify
1489  * the register symbol with which they are associated, so we might have to
1490  * search the input files global symbols to determine if this relocation is
1491  * appropriate.
1492  */
1493 uintptr_t
1494 ld_reloc_register(Rel_desc * rsp, Is_desc * isp, Ofl_desc * ofl)
1495 {
1496 	if (ofl->ofl_flags & FLG_OF_MULDEFS) {
1497 		Ifl_desc *	ifl = isp->is_file;
1498 		Sym_desc *	sdp = rsp->rel_sym;
1499 
1500 		if (sdp == 0) {
1501 			Xword		offset = rsp->rel_roffset;
1502 			Word		ndx;
1503 
1504 			for (ndx = ifl->ifl_locscnt;
1505 			    ndx < ifl->ifl_symscnt; ndx++) {
1506 				if (((sdp = ifl->ifl_oldndx[ndx]) != 0) &&
1507 				    (sdp->sd_flags & FLG_SY_REGSYM) &&
1508 				    (sdp->sd_sym->st_value == offset))
1509 					break;
1510 			}
1511 		}
1512 		if (sdp && (sdp->sd_file != ifl))
1513 			return (1);
1514 	}
1515 	return (ld_add_outrel((rsp->rel_flags | FLG_REL_REG), rsp, ofl));
1516 }
1517 
1518 /*
1519  * process relocation for a LOCAL symbol
1520  */
1521 uintptr_t
1522 ld_reloc_local(Rel_desc * rsp, Ofl_desc * ofl)
1523 {
1524 	Word		flags = ofl->ofl_flags;
1525 	Sym_desc	*sdp = rsp->rel_sym;
1526 	Word		shndx = sdp->sd_sym->st_shndx;
1527 
1528 	/*
1529 	 * if ((shared object) and (not pc relative relocation) and
1530 	 *    (not against ABS symbol))
1531 	 * then
1532 	 *	if (rtype != R_SPARC_32)
1533 	 *	then
1534 	 *		build relocation against section
1535 	 *	else
1536 	 *		build R_SPARC_RELATIVE
1537 	 *	fi
1538 	 * fi
1539 	 */
1540 	if ((flags & FLG_OF_SHAROBJ) && (rsp->rel_flags & FLG_REL_LOAD) &&
1541 	    !(IS_PC_RELATIVE(rsp->rel_rtype)) && !(IS_SIZE(rsp->rel_rtype)) &&
1542 	    !(IS_GOT_BASED(rsp->rel_rtype)) &&
1543 	    !(rsp->rel_isdesc != NULL &&
1544 	    (rsp->rel_isdesc->is_shdr->sh_type == SHT_SUNW_dof)) &&
1545 	    (((sdp->sd_flags & FLG_SY_SPECSEC) == 0) ||
1546 	    (shndx != SHN_ABS) || (sdp->sd_aux && sdp->sd_aux->sa_symspec))) {
1547 		Word	ortype = rsp->rel_rtype;
1548 
1549 		if ((rsp->rel_rtype != R_SPARC_32) &&
1550 		    (rsp->rel_rtype != R_SPARC_PLT32) &&
1551 		    (rsp->rel_rtype != R_SPARC_64))
1552 			return (ld_add_outrel((FLG_REL_SCNNDX | FLG_REL_ADVAL),
1553 			    rsp, ofl));
1554 
1555 		rsp->rel_rtype = R_SPARC_RELATIVE;
1556 		if (ld_add_outrel(FLG_REL_ADVAL, rsp, ofl) == S_ERROR)
1557 			return (S_ERROR);
1558 		rsp->rel_rtype = ortype;
1559 		return (1);
1560 	}
1561 
1562 	/*
1563 	 * If the relocation is against a 'non-allocatable' section
1564 	 * and we can not resolve it now - then give a warning
1565 	 * message.
1566 	 *
1567 	 * We can not resolve the symbol if either:
1568 	 *	a) it's undefined
1569 	 *	b) it's defined in a shared library and a
1570 	 *	   COPY relocation hasn't moved it to the executable
1571 	 *
1572 	 * Note: because we process all of the relocations against the
1573 	 *	text segment before any others - we know whether
1574 	 *	or not a copy relocation will be generated before
1575 	 *	we get here (see reloc_init()->reloc_segments()).
1576 	 */
1577 	if (!(rsp->rel_flags & FLG_REL_LOAD) &&
1578 	    ((shndx == SHN_UNDEF) ||
1579 	    ((sdp->sd_ref == REF_DYN_NEED) &&
1580 	    ((sdp->sd_flags & FLG_SY_MVTOCOMM) == 0)))) {
1581 		Conv_inv_buf_t	inv_buf;
1582 
1583 		/*
1584 		 * If the relocation is against a SHT_SUNW_ANNOTATE
1585 		 * section - then silently ignore that the relocation
1586 		 * can not be resolved.
1587 		 */
1588 		if (rsp->rel_osdesc &&
1589 		    (rsp->rel_osdesc->os_shdr->sh_type == SHT_SUNW_ANNOTATE))
1590 			return (0);
1591 		(void) eprintf(ofl->ofl_lml, ERR_WARNING,
1592 		    MSG_INTL(MSG_REL_EXTERNSYM),
1593 		    conv_reloc_SPARC_type(rsp->rel_rtype, 0, &inv_buf),
1594 		    rsp->rel_isdesc->is_file->ifl_name,
1595 		    demangle(rsp->rel_sname), rsp->rel_osdesc->os_name);
1596 		return (1);
1597 	}
1598 
1599 	/*
1600 	 * Perform relocation.
1601 	 */
1602 	return (ld_add_actrel(NULL, rsp, ofl));
1603 }
1604 
1605 /*
1606  * Establish a relocation transition.  Note, at this point of input relocation
1607  * processing, we have no idea of the relocation value that will be used in
1608  * the eventual relocation calculation.  This value is only known after the
1609  * initial image has been constructed.  Therefore, there is a small chance
1610  * that a value can exceed the capabilities of the transitioned relocation.
1611  * One example might be the offset from the GOT to a symbol.
1612  *
1613  * The only instance of this failure discovered so far has been via the use of
1614  * ABS symbols to represent an external memory location.  This situation is
1615  * rare, since ABS symbols aren't typically generated by the compilers.
1616  * Therefore, our solution is to excluded ABS symbols from the transition
1617  * relocation possibilities.  As an additional safeguard, if an inappropriate
1618  * value is passed to the final relocation engine, a verification ("V")
1619  * relocation should trigger a fatal error condition.
1620  */
1621 uintptr_t
1622 ld_reloc_GOTOP(Boolean local, Rel_desc *rsp, Ofl_desc *ofl)
1623 {
1624 	Word	rtype = rsp->rel_rtype;
1625 
1626 	if (!local || (rsp->rel_sym->sd_sym->st_shndx == SHN_ABS)) {
1627 		/*
1628 		 * When binding to a external symbol, no fixups are required
1629 		 * and the GOTDATA_OP relocation can be ignored.
1630 		 */
1631 		if (rtype == R_SPARC_GOTDATA_OP)
1632 			return (1);
1633 		return (ld_reloc_GOT_relative(local, rsp, ofl));
1634 	}
1635 
1636 	/*
1637 	 * When binding to a local symbol the relocations can be transitioned:
1638 	 *
1639 	 *	R_*_GOTDATA_OP_HIX22 -> R_*_GOTDATA_HIX22
1640 	 *	R_*_GOTDATA_OP_LOX10 -> R_*_GOTDATA_LOX10
1641 	 *	R_*_GOTDATA_OP ->	instruction fixup
1642 	 */
1643 	return (ld_add_actrel(FLG_REL_GOTFIX, rsp, ofl));
1644 }
1645 
1646 uintptr_t
1647 ld_reloc_TLS(Boolean local, Rel_desc *rsp, Ofl_desc *ofl)
1648 {
1649 	Word		rtype = rsp->rel_rtype;
1650 	Sym_desc	*sdp = rsp->rel_sym;
1651 	Word		flags = ofl->ofl_flags;
1652 	Gotndx		*gnp;
1653 
1654 	/*
1655 	 * If we're building an executable - use either the IE or LE access
1656 	 * model.  If we're building a shared object process any IE model.
1657 	 */
1658 	if ((flags & FLG_OF_EXEC) || (IS_TLS_IE(rtype))) {
1659 		/*
1660 		 * Set the DF_STATIC_TLS flag.
1661 		 */
1662 		ofl->ofl_dtflags |= DF_STATIC_TLS;
1663 
1664 		if (!local || ((flags & FLG_OF_EXEC) == 0)) {
1665 			/*
1666 			 * When processing static TLS - these relocations
1667 			 * can be ignored.
1668 			 */
1669 			if ((rtype == R_SPARC_TLS_IE_LD) ||
1670 			    (rtype == R_SPARC_TLS_IE_LDX) ||
1671 			    (rtype == R_SPARC_TLS_IE_ADD))
1672 				return (1);
1673 
1674 			/*
1675 			 * Assign a GOT entry for IE static TLS references.
1676 			 */
1677 			if (((rtype == R_SPARC_TLS_GD_HI22) ||
1678 			    (rtype == R_SPARC_TLS_GD_LO10) ||
1679 			    (rtype == R_SPARC_TLS_IE_HI22) ||
1680 			    (rtype == R_SPARC_TLS_IE_LO10)) &&
1681 			    ((gnp = ld_find_gotndx(&(sdp->sd_GOTndxs),
1682 			    GOT_REF_TLSIE, ofl, rsp)) == 0)) {
1683 
1684 				if (ld_assign_got_TLS(local, rsp, ofl, sdp,
1685 				    gnp, GOT_REF_TLSIE, FLG_REL_STLS,
1686 				    rtype, M_R_TPOFF, 0) == S_ERROR)
1687 					return (S_ERROR);
1688 			}
1689 
1690 			/*
1691 			 * IE access model.
1692 			 */
1693 			if (IS_TLS_IE(rtype))
1694 				return (ld_add_actrel(FLG_REL_STLS, rsp, ofl));
1695 
1696 			/*
1697 			 * Fixups are required for other executable models.
1698 			 */
1699 			return (ld_add_actrel((FLG_REL_TLSFIX | FLG_REL_STLS),
1700 			    rsp, ofl));
1701 		}
1702 
1703 		/*
1704 		 * LE access model.
1705 		 */
1706 		if (IS_TLS_LE(rtype))
1707 			return (ld_add_actrel(FLG_REL_STLS, rsp, ofl));
1708 
1709 		/*
1710 		 * When processing static TLS - these relocations can be
1711 		 * ignored.
1712 		 */
1713 		if (rtype == R_SPARC_TLS_IE_ADD)
1714 			return (1);
1715 
1716 		return (ld_add_actrel((FLG_REL_TLSFIX | FLG_REL_STLS),
1717 		    rsp, ofl));
1718 	}
1719 
1720 	/*
1721 	 * Building a shared object.
1722 	 *
1723 	 * For dynamic TLS references, ADD relocations are ignored.
1724 	 */
1725 	if ((rtype == R_SPARC_TLS_GD_ADD) || (rtype == R_SPARC_TLS_LDM_ADD) ||
1726 	    (rtype == R_SPARC_TLS_LDO_ADD))
1727 		return (1);
1728 
1729 	/*
1730 	 * Assign a GOT entry for a dynamic TLS reference.
1731 	 */
1732 	if (((rtype == R_SPARC_TLS_LDM_HI22) ||
1733 	    (rtype == R_SPARC_TLS_LDM_LO10)) &&
1734 	    ((gnp = ld_find_gotndx(&(sdp->sd_GOTndxs), GOT_REF_TLSLD,
1735 	    ofl, rsp)) == 0)) {
1736 
1737 		if (ld_assign_got_TLS(local, rsp, ofl, sdp, gnp, GOT_REF_TLSLD,
1738 		    FLG_REL_MTLS, rtype, M_R_DTPMOD, 0) == S_ERROR)
1739 			return (S_ERROR);
1740 
1741 	} else if (((rtype == R_SPARC_TLS_GD_HI22) ||
1742 	    (rtype == R_SPARC_TLS_GD_LO10)) &&
1743 	    ((gnp = ld_find_gotndx(&(sdp->sd_GOTndxs), GOT_REF_TLSGD,
1744 	    ofl, rsp)) == 0)) {
1745 
1746 		if (ld_assign_got_TLS(local, rsp, ofl, sdp, gnp, GOT_REF_TLSGD,
1747 		    FLG_REL_DTLS, rtype, M_R_DTPMOD, M_R_DTPOFF) == S_ERROR)
1748 			return (S_ERROR);
1749 	}
1750 
1751 	/*
1752 	 * For GD/LD TLS reference - TLS_{GD,LD}_CALL, this will eventually
1753 	 * cause a call to __tls_get_addr().  Convert this relocation to that
1754 	 * symbol now, and prepare for the PLT magic.
1755 	 */
1756 	if ((rtype == R_SPARC_TLS_GD_CALL) || (rtype == R_SPARC_TLS_LDM_CALL)) {
1757 		Sym_desc	*tlsgetsym;
1758 
1759 		if ((tlsgetsym = ld_sym_add_u(MSG_ORIG(MSG_SYM_TLSGETADDR_U),
1760 		    ofl, MSG_STR_TLSREL)) == (Sym_desc *)S_ERROR)
1761 			return (S_ERROR);
1762 
1763 		rsp->rel_sym = tlsgetsym;
1764 		rsp->rel_sname = tlsgetsym->sd_name;
1765 		rsp->rel_rtype = R_SPARC_WPLT30;
1766 
1767 		if (ld_reloc_plt(rsp, ofl) == S_ERROR)
1768 			return (S_ERROR);
1769 
1770 		rsp->rel_sym = sdp;
1771 		rsp->rel_sname = sdp->sd_name;
1772 		rsp->rel_rtype = rtype;
1773 		return (1);
1774 	}
1775 
1776 	if (IS_TLS_LD(rtype))
1777 		return (ld_add_actrel(FLG_REL_MTLS, rsp, ofl));
1778 
1779 	return (ld_add_actrel(FLG_REL_DTLS, rsp, ofl));
1780 }
1781 
1782 /*
1783  * ld_allocate_got: if a GOT is to be made, after the section is built this
1784  * function is called to allocate all the GOT slots.  The allocation is
1785  * deferred until after all GOTs have been counted and sorted according
1786  * to their size, for only then will we know how to allocate them on
1787  * a processor like SPARC which has different models for addressing the
1788  * GOT.  SPARC has two: small and large, small uses a signed 13-bit offset
1789  * into the GOT, whereas large uses an unsigned 32-bit offset.
1790  */
1791 static	Sword small_index;	/* starting index for small GOT entries */
1792 static	Sword mixed_index;	/* starting index for mixed GOT entries */
1793 static	Sword large_index;	/* starting index for large GOT entries */
1794 
1795 uintptr_t
1796 ld_assign_got(Ofl_desc *ofl, Sym_desc * sdp)
1797 {
1798 	Listnode *	lnp;
1799 	Gotndx *	gnp;
1800 
1801 	for (LIST_TRAVERSE(&sdp->sd_GOTndxs, lnp, gnp)) {
1802 		uint_t	gotents;
1803 		Gotref	gref;
1804 		gref = gnp->gn_gotref;
1805 		if ((gref == GOT_REF_TLSGD) || (gref == GOT_REF_TLSLD))
1806 			gotents = 2;
1807 		else
1808 			gotents = 1;
1809 
1810 		switch (gnp->gn_gotndx) {
1811 		case M_GOT_SMALL:
1812 			gnp->gn_gotndx = small_index;
1813 			small_index += gotents;
1814 			if (small_index == 0)
1815 				small_index = M_GOT_XNumber;
1816 			break;
1817 		case M_GOT_MIXED:
1818 			gnp->gn_gotndx = mixed_index;
1819 			mixed_index += gotents;
1820 			break;
1821 		case M_GOT_LARGE:
1822 			gnp->gn_gotndx = large_index;
1823 			large_index += gotents;
1824 			break;
1825 		default:
1826 			eprintf(ofl->ofl_lml, ERR_FATAL,
1827 			    MSG_INTL(MSG_REL_ASSIGNGOT),
1828 			    EC_XWORD(gnp->gn_gotndx), demangle(sdp->sd_name));
1829 			return (S_ERROR);
1830 		}
1831 	}
1832 	return (1);
1833 }
1834 
1835 /*
1836  * Search the GOT index list for a GOT entry with the proper addend.
1837  */
1838 Gotndx *
1839 ld_find_gotndx(List * lst, Gotref gref, Ofl_desc * ofl, Rel_desc * rdesc)
1840 {
1841 	Listnode *	lnp;
1842 	Gotndx *	gnp;
1843 
1844 	if ((gref == GOT_REF_TLSLD) && ofl->ofl_tlsldgotndx)
1845 		return (ofl->ofl_tlsldgotndx);
1846 
1847 	for (LIST_TRAVERSE(lst, lnp, gnp)) {
1848 		if ((rdesc->rel_raddend == gnp->gn_addend) &&
1849 		    (gref == gnp->gn_gotref))
1850 			return (gnp);
1851 	}
1852 	return ((Gotndx *)0);
1853 }
1854 
1855 Xword
1856 ld_calc_got_offset(Rel_desc * rdesc, Ofl_desc * ofl)
1857 {
1858 	Os_desc		*osp = ofl->ofl_osgot;
1859 	Sym_desc	*sdp = rdesc->rel_sym;
1860 	Xword		gotndx;
1861 	Gotref		gref;
1862 	Gotndx		*gnp;
1863 
1864 	if (rdesc->rel_flags & FLG_REL_DTLS)
1865 		gref = GOT_REF_TLSGD;
1866 	else if (rdesc->rel_flags & FLG_REL_MTLS)
1867 		gref = GOT_REF_TLSLD;
1868 	else if (rdesc->rel_flags & FLG_REL_STLS)
1869 		gref = GOT_REF_TLSIE;
1870 	else
1871 		gref = GOT_REF_GENERIC;
1872 
1873 	gnp = ld_find_gotndx(&(sdp->sd_GOTndxs), gref, ofl, rdesc);
1874 	assert(gnp);
1875 
1876 	gotndx = (Xword)gnp->gn_gotndx;
1877 
1878 	if ((rdesc->rel_flags & FLG_REL_DTLS) &&
1879 	    (rdesc->rel_rtype == M_R_DTPOFF))
1880 		gotndx++;
1881 
1882 	return ((Xword)((osp->os_shdr->sh_addr) + (gotndx * M_GOT_ENTSIZE) +
1883 	    (-neggotoffset * M_GOT_ENTSIZE)));
1884 }
1885 
1886 uintptr_t
1887 ld_assign_got_ndx(List * lst, Gotndx * pgnp, Gotref gref, Ofl_desc * ofl,
1888     Rel_desc * rsp, Sym_desc * sdp)
1889 {
1890 	Xword		raddend;
1891 	Gotndx *	gnp, * _gnp;
1892 	Listnode *	lnp, * plnp;
1893 	uint_t		gotents;
1894 
1895 	/* Some TLS requires two relocations with two GOT entries */
1896 	if ((gref == GOT_REF_TLSGD) || (gref == GOT_REF_TLSLD))
1897 		gotents = 2;
1898 	else
1899 		gotents = 1;
1900 
1901 	raddend = rsp->rel_raddend;
1902 	if (pgnp && (pgnp->gn_addend == raddend) && (pgnp->gn_gotref == gref)) {
1903 
1904 		/*
1905 		 * If an entry for this addend already exists, determine if it
1906 		 * has mixed mode GOT access (both PIC and pic).
1907 		 *
1908 		 * In order to be accessible by both large and small pic,
1909 		 * a mixed mode GOT must be located in the positive index
1910 		 * range above _GLOBAL_OFFSET_TABLE_, and in the range
1911 		 * reachable small pic. This is necessary because the large
1912 		 * PIC mode cannot use a negative offset. This implies that
1913 		 * there can be no more than (M_GOT_MAXSMALL/2 - M_GOT_XNumber)
1914 		 * such entries.
1915 		 */
1916 		switch (pgnp->gn_gotndx) {
1917 		case M_GOT_SMALL:
1918 			/*
1919 			 * This one was previously identified as a small
1920 			 * GOT. If this access is large, then convert
1921 			 * it to mixed.
1922 			 */
1923 			if (rsp->rel_rtype != R_SPARC_GOT13) {
1924 				pgnp->gn_gotndx = M_GOT_MIXED;
1925 				mixgotcnt += gotents;
1926 			}
1927 			break;
1928 
1929 		case M_GOT_LARGE:
1930 			/*
1931 			 * This one was previously identified as a large
1932 			 * GOT. If this access is small, convert it to mixed.
1933 			 */
1934 			if (rsp->rel_rtype == R_SPARC_GOT13) {
1935 				smlgotcnt += gotents;
1936 				mixgotcnt += gotents;
1937 				pgnp->gn_gotndx = M_GOT_MIXED;
1938 				sdp->sd_flags |= FLG_SY_SMGOT;
1939 			}
1940 			break;
1941 		}
1942 		return (1);
1943 	}
1944 
1945 	plnp = 0;
1946 	for (LIST_TRAVERSE(lst, lnp, _gnp)) {
1947 		if (_gnp->gn_addend > raddend)
1948 			break;
1949 		plnp = lnp;
1950 	}
1951 
1952 	/*
1953 	 * Allocate a new entry.
1954 	 */
1955 	if ((gnp = libld_calloc(sizeof (Gotndx), 1)) == 0)
1956 		return (S_ERROR);
1957 	gnp->gn_addend = raddend;
1958 	gnp->gn_gotref = gref;
1959 	ofl->ofl_gotcnt += gotents;
1960 
1961 	if (rsp->rel_rtype == R_SPARC_GOT13) {
1962 		gnp->gn_gotndx = M_GOT_SMALL;
1963 		smlgotcnt += gotents;
1964 		sdp->sd_flags |= FLG_SY_SMGOT;
1965 	} else {
1966 		gnp->gn_gotndx = M_GOT_LARGE;
1967 	}
1968 
1969 	if (gref == GOT_REF_TLSLD) {
1970 		ofl->ofl_tlsldgotndx = gnp;
1971 		return (1);
1972 	}
1973 
1974 	if (plnp == 0) {
1975 		/*
1976 		 * Insert at head of list
1977 		 */
1978 		if (list_prependc(lst, (void *)gnp) == 0)
1979 			return (S_ERROR);
1980 	} else if (_gnp->gn_addend > raddend) {
1981 		/*
1982 		 * Insert in middle of lest
1983 		 */
1984 		if (list_insertc(lst, (void *)gnp, plnp) == 0)
1985 			return (S_ERROR);
1986 	} else {
1987 		/*
1988 		 * Append to tail of list
1989 		 */
1990 		if (list_appendc(lst, (void *)gnp) == 0)
1991 			return (S_ERROR);
1992 	}
1993 	return (1);
1994 }
1995 
1996 void
1997 ld_assign_plt_ndx(Sym_desc * sdp, Ofl_desc *ofl)
1998 {
1999 	sdp->sd_aux->sa_PLTndx = 1 + ofl->ofl_pltcnt++;
2000 }
2001 
2002 
2003 uintptr_t
2004 ld_allocate_got(Ofl_desc * ofl)
2005 {
2006 	const Sword	first_large_ndx = M_GOT_MAXSMALL / 2;
2007 	Sym_desc *	sdp;
2008 	Addr		addr;
2009 
2010 	/*
2011 	 * Sanity check -- is this going to fit at all? There are two
2012 	 * limits to be concerned about:
2013 	 *	1) There is a limit on the number of small pic GOT indices,
2014 	 *		given by M_GOT_MAXSMALL.
2015 	 *	2) If there are more than (M_GOT_MAXSMALL/2 - M_GOT_XNumber)
2016 	 *		small GOT indices, there will be items at negative
2017 	 *		offsets from _GLOBAL_OFFSET_TABLE_. Items that are
2018 	 *		accessed via large (PIC) code cannot reach these
2019 	 *		negative slots, so mixed mode items must be in the
2020 	 *		non-negative range. This implies a limit of
2021 	 *		(M_GOT_MAXSMALL/2 - M_GOT_XNumber) mixed mode indices.
2022 	 */
2023 	if (smlgotcnt > M_GOT_MAXSMALL) {
2024 		eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_REL_SMALLGOT),
2025 		    EC_WORD(smlgotcnt), M_GOT_MAXSMALL);
2026 		return (S_ERROR);
2027 	}
2028 	if (mixgotcnt > (first_large_ndx - M_GOT_XNumber)) {
2029 		eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_REL_MIXEDGOT),
2030 		    EC_WORD(mixgotcnt), first_large_ndx - M_GOT_XNumber);
2031 		return (S_ERROR);
2032 	}
2033 
2034 	/*
2035 	 * Set starting offset to be either 0, or a negative index into
2036 	 * the GOT based on the number of small symbols we've got.
2037 	 */
2038 	neggotoffset = ((smlgotcnt >= first_large_ndx) ?
2039 	    (first_large_ndx - smlgotcnt) : 0);
2040 
2041 	/*
2042 	 * Initialize the got offsets used by assign_got() to
2043 	 * locate GOT items:
2044 	 *	small - Starting index of items referenced only
2045 	 *		by small offsets (-Kpic).
2046 	 *	mixed - Starting index of items referenced
2047 	 *		by both large (-KPIC) and small (-Kpic).
2048 	 *	large - Indexes referenced only by large (-KPIC)
2049 	 *
2050 	 *  Small items can have negative indexes (i.e. lie below
2051 	 *	_GLOBAL_OFFSET_TABLE_). Mixed and large items must have
2052 	 *	non-negative offsets.
2053 	 */
2054 	small_index = (neggotoffset == 0) ? M_GOT_XNumber : neggotoffset;
2055 	large_index = neggotoffset + smlgotcnt;
2056 	mixed_index = large_index - mixgotcnt;
2057 
2058 	/*
2059 	 * Assign bias to GOT symbols.
2060 	 */
2061 	addr = -neggotoffset * M_GOT_ENTSIZE;
2062 	if (sdp = ld_sym_find(MSG_ORIG(MSG_SYM_GOFTBL), SYM_NOHASH, 0, ofl))
2063 		sdp->sd_sym->st_value = addr;
2064 	if (sdp = ld_sym_find(MSG_ORIG(MSG_SYM_GOFTBL_U), SYM_NOHASH, 0, ofl))
2065 		sdp->sd_sym->st_value = addr;
2066 
2067 	if (ofl->ofl_tlsldgotndx) {
2068 		ofl->ofl_tlsldgotndx->gn_gotndx = large_index;
2069 		large_index += 2;
2070 	}
2071 	return (1);
2072 }
2073 
2074 /*
2075  * Initializes .got[0] with the _DYNAMIC symbol value.
2076  */
2077 uintptr_t
2078 ld_fillin_gotplt(Ofl_desc *ofl)
2079 {
2080 	if (ofl->ofl_osgot) {
2081 		Sym_desc	*sdp;
2082 
2083 		if ((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_DYNAMIC_U),
2084 		    SYM_NOHASH, 0, ofl)) != NULL) {
2085 			uchar_t	*genptr;
2086 
2087 			genptr = ((uchar_t *)ofl->ofl_osgot->os_outdata->d_buf +
2088 			    (-neggotoffset * M_GOT_ENTSIZE) +
2089 			    (M_GOT_XDYNAMIC * M_GOT_ENTSIZE));
2090 			/* LINTED */
2091 			*((Xword *)genptr) = sdp->sd_sym->st_value;
2092 		}
2093 	}
2094 	return (1);
2095 }
2096