xref: /titanic_44/usr/src/cmd/sgs/libld/common/machrel.sparc.c (revision 416369f6ab2ab327f0e71cbdb854f9e5bdc6f5ec)
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 
942 	if (ofl->ofl_actrels.head)
943 		DBG_CALL(Dbg_reloc_doact_title(ofl->ofl_lml));
944 
945 	/*
946 	 * Process active relocations.
947 	 */
948 	for (LIST_TRAVERSE(&ofl->ofl_actrels, lnp, rcp)) {
949 		/* LINTED */
950 		for (arsp = (Rel_desc *)(rcp + 1);
951 		    arsp < rcp->rc_free; arsp++) {
952 			uchar_t		*addr;
953 			Xword		value;
954 			Sym_desc	*sdp;
955 			const char	*ifl_name;
956 			Xword		refaddr;
957 
958 			/*
959 			 * If the section this relocation is against has been
960 			 * discarded (-zignore), then discard (skip) the
961 			 * relocation itself.
962 			 */
963 			if ((arsp->rel_isdesc->is_flags & FLG_IS_DISCARD) &&
964 			    ((arsp->rel_flags &
965 			    (FLG_REL_GOT | FLG_REL_BSS |
966 			    FLG_REL_PLT | FLG_REL_NOINFO)) == 0)) {
967 				DBG_CALL(Dbg_reloc_discard(ofl->ofl_lml,
968 				    M_MACH, arsp));
969 				continue;
970 			}
971 
972 			/*
973 			 * Perform any required TLS fixups.
974 			 */
975 			if (arsp->rel_flags & FLG_REL_TLSFIX) {
976 				Fixupret	ret;
977 
978 				if ((ret = tls_fixups(ofl, arsp)) == FIX_ERROR)
979 					return (S_ERROR);
980 				if (ret == FIX_DONE)
981 					continue;
982 			}
983 
984 			/*
985 			 * Perform any required GOTOP fixups.
986 			 */
987 			if (arsp->rel_flags & FLG_REL_GOTFIX) {
988 				Fixupret	ret;
989 
990 				if ((ret =
991 				    gotop_fixups(ofl, arsp)) == FIX_ERROR)
992 					return (S_ERROR);
993 				if (ret == FIX_DONE)
994 					continue;
995 			}
996 
997 			/*
998 			 * If this is a relocation against the move table, or
999 			 * expanded move table, adjust the relocation entries.
1000 			 */
1001 			if (arsp->rel_move)
1002 				ld_adj_movereloc(ofl, arsp);
1003 
1004 			sdp = arsp->rel_sym;
1005 			refaddr = arsp->rel_roffset +
1006 			    (Off)_elf_getxoff(arsp->rel_isdesc->is_indata);
1007 
1008 			if ((arsp->rel_flags & FLG_REL_CLVAL) ||
1009 			    (arsp->rel_flags & FLG_REL_GOTCL))
1010 				value = 0;
1011 			else if (ELF_ST_TYPE(sdp->sd_sym->st_info) ==
1012 			    STT_SECTION) {
1013 				Sym_desc	*sym;
1014 
1015 				/*
1016 				 * The value for a symbol pointing to a SECTION
1017 				 * is based off of that sections position.
1018 				 */
1019 				if ((sdp->sd_isc->is_flags & FLG_IS_RELUPD) &&
1020 				    (sym = ld_am_I_partial(arsp,
1021 				    arsp->rel_roffset))) {
1022 					/*
1023 					 * If the symbol is moved,
1024 					 * adjust the value
1025 					 */
1026 					value = _elf_getxoff(
1027 					    sym->sd_isc->is_indata);
1028 					if (sym->sd_isc->is_shdr->sh_flags &
1029 					    SHF_ALLOC)
1030 						value += sym->sd_isc->
1031 						    is_osdesc->os_shdr->sh_addr;
1032 				} else {
1033 					value = _elf_getxoff(
1034 					    sdp->sd_isc->is_indata);
1035 					if (sdp->sd_isc->is_shdr->sh_flags &
1036 					    SHF_ALLOC)
1037 						value += sdp->sd_isc->
1038 						    is_osdesc->os_shdr->sh_addr;
1039 				}
1040 
1041 				if (sdp->sd_isc->is_shdr->sh_flags & SHF_TLS)
1042 					value -= ofl->ofl_tlsphdr->p_vaddr;
1043 
1044 			} else if (IS_SIZE(arsp->rel_rtype)) {
1045 				/*
1046 				 * Size relocations require the symbols size.
1047 				 */
1048 				value = sdp->sd_sym->st_size;
1049 			} else {
1050 				/*
1051 				 * Else the value is the symbols value.
1052 				 */
1053 				value = sdp->sd_sym->st_value;
1054 			}
1055 
1056 			/*
1057 			 * Relocation against the GLOBAL_OFFSET_TABLE.
1058 			 */
1059 			if (arsp->rel_flags & FLG_REL_GOT)
1060 				arsp->rel_osdesc = ofl->ofl_osgot;
1061 
1062 			/*
1063 			 * If loadable and not producing a relocatable object
1064 			 * add the sections virtual address to the reference
1065 			 * address.
1066 			 */
1067 			if ((arsp->rel_flags & FLG_REL_LOAD) &&
1068 			    ((flags & FLG_OF_RELOBJ) == 0))
1069 				refaddr += arsp->rel_isdesc->is_osdesc->
1070 				    os_shdr->sh_addr;
1071 
1072 			/*
1073 			 * If this entry has a PLT assigned to it, it's
1074 			 * value is actually the address of the PLT (and
1075 			 * not the address of the function).
1076 			 */
1077 			if (IS_PLT(arsp->rel_rtype)) {
1078 				if (sdp->sd_aux && sdp->sd_aux->sa_PLTndx)
1079 					value = ld_calc_plt_addr(sdp, ofl);
1080 			}
1081 
1082 			/*
1083 			 * Add relocations addend to value.  Add extra
1084 			 * relocation addend if needed.
1085 			 */
1086 			value += arsp->rel_raddend;
1087 			if (IS_EXTOFFSET(arsp->rel_rtype))
1088 				value += arsp->rel_typedata;
1089 
1090 			/*
1091 			 * Determine whether the value needs further adjustment.
1092 			 * Filter through the attributes of the relocation to
1093 			 * determine what adjustment is required.  Note, many
1094 			 * of the following cases are only applicable when a
1095 			 * .got is present.  As a .got is not generated when a
1096 			 * relocatable object is being built, any adjustments
1097 			 * that require a .got need to be skipped.
1098 			 */
1099 			if ((arsp->rel_flags & FLG_REL_GOT) &&
1100 			    ((flags & FLG_OF_RELOBJ) == 0)) {
1101 				Xword		R1addr;
1102 				uintptr_t	R2addr;
1103 				Sword		gotndx;
1104 				Gotndx		*gnp;
1105 				Gotref		gref;
1106 
1107 				/*
1108 				 * Clear the GOT table entry, on SPARC we clear
1109 				 * the entry and the 'value' if needed is stored
1110 				 * in an output relocations addend.
1111 				 *
1112 				 * Calculate offset into GOT at which to apply
1113 				 * the relocation.
1114 				 */
1115 				if (arsp->rel_flags & FLG_REL_DTLS)
1116 					gref = GOT_REF_TLSGD;
1117 				else if (arsp->rel_flags & FLG_REL_MTLS)
1118 					gref = GOT_REF_TLSLD;
1119 				else if (arsp->rel_flags & FLG_REL_STLS)
1120 					gref = GOT_REF_TLSIE;
1121 				else
1122 					gref = GOT_REF_GENERIC;
1123 
1124 				gnp = ld_find_gotndx(&(sdp->sd_GOTndxs), gref,
1125 				    ofl, arsp);
1126 				assert(gnp);
1127 
1128 				if (arsp->rel_rtype == M_R_DTPOFF)
1129 					gotndx = gnp->gn_gotndx + 1;
1130 				else
1131 					gotndx = gnp->gn_gotndx;
1132 
1133 				/* LINTED */
1134 				R1addr = (Xword)((-neggotoffset *
1135 				    M_GOT_ENTSIZE) + (gotndx * M_GOT_ENTSIZE));
1136 
1137 				/*
1138 				 * Add the GOTs data's offset.
1139 				 */
1140 				R2addr = R1addr + (uintptr_t)
1141 				    arsp->rel_osdesc->os_outdata->d_buf;
1142 
1143 				DBG_CALL(Dbg_reloc_doact(ofl->ofl_lml,
1144 				    ELF_DBG_LD, M_MACH, SHT_RELA,
1145 				    arsp->rel_rtype, R1addr, value,
1146 				    arsp->rel_sname, arsp->rel_osdesc));
1147 
1148 				/*
1149 				 * And do it.
1150 				 */
1151 				if (ofl->ofl_flags1 & FLG_OF1_ENCDIFF)
1152 					*(Xword *)R2addr =
1153 					    ld_byteswap_Xword(value);
1154 				else
1155 					*(Xword *)R2addr = value;
1156 				continue;
1157 
1158 			} else if (IS_GOT_BASED(arsp->rel_rtype) &&
1159 			    ((flags & FLG_OF_RELOBJ) == 0)) {
1160 				value -= (ofl->ofl_osgot->os_shdr->sh_addr +
1161 				    (-neggotoffset * M_GOT_ENTSIZE));
1162 
1163 			} else if (IS_PC_RELATIVE(arsp->rel_rtype)) {
1164 				value -= refaddr;
1165 
1166 			} else if (IS_TLS_INS(arsp->rel_rtype) &&
1167 			    IS_GOT_RELATIVE(arsp->rel_rtype) &&
1168 			    ((flags & FLG_OF_RELOBJ) == 0)) {
1169 				Gotndx	*gnp;
1170 				Gotref	gref;
1171 
1172 				if (arsp->rel_flags & FLG_REL_STLS)
1173 					gref = GOT_REF_TLSIE;
1174 				else if (arsp->rel_flags & FLG_REL_DTLS)
1175 					gref = GOT_REF_TLSGD;
1176 				else if (arsp->rel_flags & FLG_REL_MTLS)
1177 					gref = GOT_REF_TLSLD;
1178 
1179 				gnp = ld_find_gotndx(&(sdp->sd_GOTndxs), gref,
1180 				    ofl, arsp);
1181 				assert(gnp);
1182 
1183 				value = gnp->gn_gotndx * M_GOT_ENTSIZE;
1184 
1185 			} else if (IS_GOT_RELATIVE(arsp->rel_rtype) &&
1186 			    ((flags & FLG_OF_RELOBJ) == 0)) {
1187 				Gotndx	*gnp;
1188 
1189 				gnp = ld_find_gotndx(&(sdp->sd_GOTndxs),
1190 				    GOT_REF_GENERIC, ofl, arsp);
1191 				assert(gnp);
1192 
1193 				value = gnp->gn_gotndx * M_GOT_ENTSIZE;
1194 
1195 			} else if ((arsp->rel_flags & FLG_REL_STLS) &&
1196 			    ((flags & FLG_OF_RELOBJ) == 0)) {
1197 				Xword	tlsstatsize;
1198 
1199 				/*
1200 				 * This is the LE TLS
1201 				 * reference model.  Static offset
1202 				 * is hard-coded, and negated so that
1203 				 * it can be added to the thread pointer (%g7)
1204 				 */
1205 				tlsstatsize = S_ROUND(ofl->
1206 				    ofl_tlsphdr->p_memsz, M_TLSSTATALIGN);
1207 				value = -(tlsstatsize - value);
1208 			}
1209 
1210 			if (arsp->rel_isdesc->is_file)
1211 				ifl_name = arsp->rel_isdesc->is_file->ifl_name;
1212 			else
1213 				ifl_name = MSG_INTL(MSG_STR_NULL);
1214 
1215 			/*
1216 			 * Make sure we have data to relocate.  Compiler and
1217 			 * assembler developers have been known to generate
1218 			 * relocations against invalid sections (normally .bss),
1219 			 * so for their benefit give them sufficient information
1220 			 * to help analyze the problem.  End users should never
1221 			 * see this.
1222 			 */
1223 			if (arsp->rel_isdesc->is_indata->d_buf == 0) {
1224 				Conv_inv_buf_t	inv_buf;
1225 
1226 				eprintf(ofl->ofl_lml, ERR_FATAL,
1227 				    MSG_INTL(MSG_REL_EMPTYSEC),
1228 				    conv_reloc_SPARC_type(arsp->rel_rtype,
1229 				    0, &inv_buf), ifl_name,
1230 				    demangle(arsp->rel_sname),
1231 				    arsp->rel_isdesc->is_name);
1232 				return (S_ERROR);
1233 			}
1234 
1235 			/*
1236 			 * Get the address of the data item we need to modify.
1237 			 */
1238 			addr = (uchar_t *)((uintptr_t)arsp->rel_roffset +
1239 			    (uintptr_t)_elf_getxoff(arsp->rel_isdesc->
1240 			    is_indata));
1241 
1242 			/*LINTED*/
1243 			DBG_CALL(Dbg_reloc_doact(ofl->ofl_lml, ELF_DBG_LD,
1244 			    M_MACH, SHT_RELA, arsp->rel_rtype, EC_NATPTR(addr),
1245 			    value, arsp->rel_sname, arsp->rel_osdesc));
1246 			addr += (uintptr_t)arsp->rel_osdesc->os_outdata->d_buf;
1247 
1248 			if ((((uintptr_t)addr - (uintptr_t)ofl->ofl_nehdr) >
1249 			    ofl->ofl_size) || (arsp->rel_roffset >
1250 			    arsp->rel_osdesc->os_shdr->sh_size)) {
1251 				Conv_inv_buf_t	inv_buf;
1252 				int		class;
1253 
1254 				if (((uintptr_t)addr -
1255 				    (uintptr_t)ofl->ofl_nehdr) > ofl->ofl_size)
1256 					class = ERR_FATAL;
1257 				else
1258 					class = ERR_WARNING;
1259 
1260 				eprintf(ofl->ofl_lml, class,
1261 				    MSG_INTL(MSG_REL_INVALOFFSET),
1262 				    conv_reloc_SPARC_type(arsp->rel_rtype,
1263 				    0, &inv_buf), ifl_name,
1264 				    arsp->rel_isdesc->is_name,
1265 				    demangle(arsp->rel_sname),
1266 				    EC_ADDR((uintptr_t)addr -
1267 				    (uintptr_t)ofl->ofl_nehdr));
1268 
1269 				if (class == ERR_FATAL) {
1270 					return_code = S_ERROR;
1271 					continue;
1272 				}
1273 			}
1274 
1275 			/*
1276 			 * If '-z noreloc' is specified - skip the do_reloc
1277 			 * stage.
1278 			 */
1279 			if (OFL_DO_RELOC(ofl)) {
1280 				if (do_reloc_ld((uchar_t)arsp->rel_rtype, addr,
1281 				    &value, arsp->rel_sname, ifl_name,
1282 				    OFL_SWAP_RELOC_DATA(ofl, arsp),
1283 				    ofl->ofl_lml) == 0)
1284 					return_code = S_ERROR;
1285 			}
1286 		}
1287 	}
1288 	return (return_code);
1289 }
1290 
1291 uintptr_t
1292 ld_add_outrel(Word flags, Rel_desc *rsp, Ofl_desc *ofl)
1293 {
1294 	Rel_desc	*orsp;
1295 	Rel_cache	*rcp;
1296 	Sym_desc	*sdp = rsp->rel_sym;
1297 	Conv_inv_buf_t	inv_buf;
1298 
1299 	/*
1300 	 * Static executables *do not* want any relocations against them.
1301 	 * Since our engine still creates relocations against a WEAK UNDEFINED
1302 	 * symbol in a static executable, it's best to disable them here
1303 	 * instead of through out the relocation code.
1304 	 */
1305 	if ((ofl->ofl_flags & (FLG_OF_STATIC | FLG_OF_EXEC)) ==
1306 	    (FLG_OF_STATIC | FLG_OF_EXEC))
1307 		return (1);
1308 
1309 	/*
1310 	 * Certain relocations do not make sense in a 64bit shared object,
1311 	 * if building a shared object do a sanity check on the output
1312 	 * relocations being created.
1313 	 */
1314 	if (ofl->ofl_flags & FLG_OF_SHAROBJ) {
1315 		Word	rtype = rsp->rel_rtype;
1316 		/*
1317 		 * Because the R_SPARC_HIPLT22 & R_SPARC_LOPLT10 relocations
1318 		 * are not relative they make no sense to create in a shared
1319 		 * object - so emit the proper error message if that occurs.
1320 		 */
1321 		if ((rtype == R_SPARC_HIPLT22) || (rtype == R_SPARC_LOPLT10)) {
1322 			eprintf(ofl->ofl_lml, ERR_FATAL,
1323 			    MSG_INTL(MSG_REL_UNRELREL),
1324 			    conv_reloc_SPARC_type(rsp->rel_rtype, 0, &inv_buf),
1325 			    rsp->rel_isdesc->is_file->ifl_name,
1326 			    demangle(rsp->rel_sname));
1327 			return (S_ERROR);
1328 		}
1329 #if	defined(_ELF64)
1330 		/*
1331 		 * Each of the following relocations requires that the
1332 		 * object being built be loaded in either the upper 32 or
1333 		 * 44 bit range of memory.  Since shared libraries traditionally
1334 		 * are loaded in the lower range of memory - this isn't going
1335 		 * to work.
1336 		 */
1337 		if ((rtype == R_SPARC_H44) || (rtype == R_SPARC_M44) ||
1338 		    (rtype == R_SPARC_L44)) {
1339 			eprintf(ofl->ofl_lml, ERR_FATAL,
1340 			    MSG_INTL(MSG_REL_SHOBJABS44),
1341 			    conv_reloc_SPARC_type(rsp->rel_rtype, 0, &inv_buf),
1342 			    rsp->rel_isdesc->is_file->ifl_name,
1343 			    demangle(rsp->rel_sname));
1344 			return (S_ERROR);
1345 		}
1346 #endif
1347 	}
1348 
1349 	/*
1350 	 * If no relocation cache structures are available allocate
1351 	 * a new one and link it into the cache list.
1352 	 */
1353 	if ((ofl->ofl_outrels.tail == 0) ||
1354 	    ((rcp = (Rel_cache *)ofl->ofl_outrels.tail->data) == 0) ||
1355 	    ((orsp = rcp->rc_free) == rcp->rc_end)) {
1356 		static size_t	nextsize = 0;
1357 		size_t		size;
1358 
1359 		/*
1360 		 * Output relocation numbers can vary considerably between
1361 		 * building executables or shared objects (pic vs. non-pic),
1362 		 * etc.  But, they typically aren't very large, so for these
1363 		 * objects use a standard bucket size.  For building relocatable
1364 		 * objects, typically there will be an output relocation for
1365 		 * every input relocation.
1366 		 */
1367 		if (nextsize == 0) {
1368 			if (ofl->ofl_flags & FLG_OF_RELOBJ) {
1369 				if ((size = ofl->ofl_relocincnt) == 0)
1370 					size = REL_LOIDESCNO;
1371 				if (size > REL_HOIDESCNO)
1372 					nextsize = REL_HOIDESCNO;
1373 				else
1374 					nextsize = REL_LOIDESCNO;
1375 			} else
1376 				nextsize = size = REL_HOIDESCNO;
1377 		} else
1378 			size = nextsize;
1379 
1380 		size = size * sizeof (Rel_desc);
1381 
1382 		if (((rcp = libld_malloc(sizeof (Rel_cache) + size)) == 0) ||
1383 		    (list_appendc(&ofl->ofl_outrels, rcp) == 0))
1384 			return (S_ERROR);
1385 
1386 		/* LINTED */
1387 		rcp->rc_free = orsp = (Rel_desc *)(rcp + 1);
1388 		/* LINTED */
1389 		rcp->rc_end = (Rel_desc *)((char *)rcp->rc_free + size);
1390 	}
1391 
1392 
1393 	/*
1394 	 * If we are adding a output relocation against a section
1395 	 * symbol (non-RELATIVE) then mark that section.  These sections
1396 	 * will be added to the .dynsym symbol table.
1397 	 */
1398 	if (sdp && (rsp->rel_rtype != M_R_RELATIVE) &&
1399 	    ((flags & FLG_REL_SCNNDX) ||
1400 	    (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION))) {
1401 
1402 		/*
1403 		 * If this is a COMMON symbol - no output section
1404 		 * exists yet - (it's created as part of sym_validate()).
1405 		 * So - we mark here that when it's created it should
1406 		 * be tagged with the FLG_OS_OUTREL flag.
1407 		 */
1408 		if ((sdp->sd_flags & FLG_SY_SPECSEC) &&
1409 		    (sdp->sd_sym->st_shndx == SHN_COMMON)) {
1410 			if (ELF_ST_TYPE(sdp->sd_sym->st_info) != STT_TLS)
1411 				ofl->ofl_flags1 |= FLG_OF1_BSSOREL;
1412 			else
1413 				ofl->ofl_flags1 |= FLG_OF1_TLSOREL;
1414 		} else {
1415 			Os_desc	*osp = sdp->sd_isc->is_osdesc;
1416 
1417 			if (osp && ((osp->os_flags & FLG_OS_OUTREL) == 0)) {
1418 				ofl->ofl_dynshdrcnt++;
1419 				osp->os_flags |= FLG_OS_OUTREL;
1420 			}
1421 		}
1422 	}
1423 
1424 	*orsp = *rsp;
1425 	orsp->rel_flags |= flags;
1426 
1427 	rcp->rc_free++;
1428 	ofl->ofl_outrelscnt++;
1429 
1430 	if (flags & FLG_REL_GOT)
1431 		ofl->ofl_relocgotsz += (Xword)sizeof (Rela);
1432 	else if (flags & FLG_REL_PLT)
1433 		ofl->ofl_relocpltsz += (Xword)sizeof (Rela);
1434 	else if (flags & FLG_REL_BSS)
1435 		ofl->ofl_relocbsssz += (Xword)sizeof (Rela);
1436 	else if (flags & FLG_REL_NOINFO)
1437 		ofl->ofl_relocrelsz += (Xword)sizeof (Rela);
1438 	else
1439 		orsp->rel_osdesc->os_szoutrels += (Xword)sizeof (Rela);
1440 
1441 	if (orsp->rel_rtype == M_R_RELATIVE)
1442 		ofl->ofl_relocrelcnt++;
1443 
1444 #if	defined(_ELF64)
1445 	/*
1446 	 * When building a 64-bit object any R_SPARC_WDISP30 relocation is given
1447 	 * a plt padding entry, unless we're building a relocatable object
1448 	 * (ld -r) or -b is in effect.
1449 	 */
1450 	if ((orsp->rel_rtype == R_SPARC_WDISP30) &&
1451 	    ((ofl->ofl_flags & (FLG_OF_BFLAG | FLG_OF_RELOBJ)) == 0) &&
1452 	    ((orsp->rel_sym->sd_flags & FLG_SY_PLTPAD) == 0)) {
1453 		ofl->ofl_pltpad++;
1454 		orsp->rel_sym->sd_flags |= FLG_SY_PLTPAD;
1455 	}
1456 #endif
1457 	/*
1458 	 * We don't perform sorting on PLT relocations because
1459 	 * they have already been assigned a PLT index and if we
1460 	 * were to sort them we would have to re-assign the plt indexes.
1461 	 */
1462 	if (!(flags & FLG_REL_PLT))
1463 		ofl->ofl_reloccnt++;
1464 
1465 	/*
1466 	 * Insure a GLOBAL_OFFSET_TABLE is generated if required.
1467 	 */
1468 	if (IS_GOT_REQUIRED(orsp->rel_rtype))
1469 		ofl->ofl_flags |= FLG_OF_BLDGOT;
1470 
1471 	/*
1472 	 * Identify and possibly warn of a displacement relocation.
1473 	 */
1474 	if (orsp->rel_flags & FLG_REL_DISP) {
1475 		ofl->ofl_dtflags_1 |= DF_1_DISPRELPND;
1476 
1477 		if (ofl->ofl_flags & FLG_OF_VERBOSE)
1478 			ld_disp_errmsg(MSG_INTL(MSG_REL_DISPREL4), orsp, ofl);
1479 	}
1480 	DBG_CALL(Dbg_reloc_ors_entry(ofl->ofl_lml, ELF_DBG_LD, SHT_RELA,
1481 	    M_MACH, orsp));
1482 	return (1);
1483 }
1484 
1485 /*
1486  * Process relocation against a register symbol.  Note, of -z muldefs is in
1487  * effect there may have been multiple register definitions, which would have
1488  * been processed as non-fatal, with the first definition winning.  But, we
1489  * will also process multiple relocations for these multiple definitions.  In
1490  * this case we must only preserve the relocation for the definition that was
1491  * kept.  The sad part is that register relocations don't typically specify
1492  * the register symbol with which they are associated, so we might have to
1493  * search the input files global symbols to determine if this relocation is
1494  * appropriate.
1495  */
1496 uintptr_t
1497 ld_reloc_register(Rel_desc * rsp, Is_desc * isp, Ofl_desc * ofl)
1498 {
1499 	if (ofl->ofl_flags & FLG_OF_MULDEFS) {
1500 		Ifl_desc *	ifl = isp->is_file;
1501 		Sym_desc *	sdp = rsp->rel_sym;
1502 
1503 		if (sdp == 0) {
1504 			Xword		offset = rsp->rel_roffset;
1505 			Word		ndx;
1506 
1507 			for (ndx = ifl->ifl_locscnt;
1508 			    ndx < ifl->ifl_symscnt; ndx++) {
1509 				if (((sdp = ifl->ifl_oldndx[ndx]) != 0) &&
1510 				    (sdp->sd_flags & FLG_SY_REGSYM) &&
1511 				    (sdp->sd_sym->st_value == offset))
1512 					break;
1513 			}
1514 		}
1515 		if (sdp && (sdp->sd_file != ifl))
1516 			return (1);
1517 	}
1518 	return (ld_add_outrel((rsp->rel_flags | FLG_REL_REG), rsp, ofl));
1519 }
1520 
1521 /*
1522  * process relocation for a LOCAL symbol
1523  */
1524 uintptr_t
1525 ld_reloc_local(Rel_desc * rsp, Ofl_desc * ofl)
1526 {
1527 	Word		flags = ofl->ofl_flags;
1528 	Sym_desc	*sdp = rsp->rel_sym;
1529 	Word		shndx = sdp->sd_sym->st_shndx;
1530 
1531 	/*
1532 	 * if ((shared object) and (not pc relative relocation) and
1533 	 *    (not against ABS symbol))
1534 	 * then
1535 	 *	if (rtype != R_SPARC_32)
1536 	 *	then
1537 	 *		build relocation against section
1538 	 *	else
1539 	 *		build R_SPARC_RELATIVE
1540 	 *	fi
1541 	 * fi
1542 	 */
1543 	if ((flags & FLG_OF_SHAROBJ) && (rsp->rel_flags & FLG_REL_LOAD) &&
1544 	    !(IS_PC_RELATIVE(rsp->rel_rtype)) && !(IS_SIZE(rsp->rel_rtype)) &&
1545 	    !(IS_GOT_BASED(rsp->rel_rtype)) &&
1546 	    !(rsp->rel_isdesc != NULL &&
1547 	    (rsp->rel_isdesc->is_shdr->sh_type == SHT_SUNW_dof)) &&
1548 	    (((sdp->sd_flags & FLG_SY_SPECSEC) == 0) ||
1549 	    (shndx != SHN_ABS) || (sdp->sd_aux && sdp->sd_aux->sa_symspec))) {
1550 		Word	ortype = rsp->rel_rtype;
1551 
1552 		if ((rsp->rel_rtype != R_SPARC_32) &&
1553 		    (rsp->rel_rtype != R_SPARC_PLT32) &&
1554 		    (rsp->rel_rtype != R_SPARC_64))
1555 			return (ld_add_outrel((FLG_REL_SCNNDX | FLG_REL_ADVAL),
1556 			    rsp, ofl));
1557 
1558 		rsp->rel_rtype = R_SPARC_RELATIVE;
1559 		if (ld_add_outrel(FLG_REL_ADVAL, rsp, ofl) == S_ERROR)
1560 			return (S_ERROR);
1561 		rsp->rel_rtype = ortype;
1562 		return (1);
1563 	}
1564 
1565 	/*
1566 	 * If the relocation is against a 'non-allocatable' section
1567 	 * and we can not resolve it now - then give a warning
1568 	 * message.
1569 	 *
1570 	 * We can not resolve the symbol if either:
1571 	 *	a) it's undefined
1572 	 *	b) it's defined in a shared library and a
1573 	 *	   COPY relocation hasn't moved it to the executable
1574 	 *
1575 	 * Note: because we process all of the relocations against the
1576 	 *	text segment before any others - we know whether
1577 	 *	or not a copy relocation will be generated before
1578 	 *	we get here (see reloc_init()->reloc_segments()).
1579 	 */
1580 	if (!(rsp->rel_flags & FLG_REL_LOAD) &&
1581 	    ((shndx == SHN_UNDEF) ||
1582 	    ((sdp->sd_ref == REF_DYN_NEED) &&
1583 	    ((sdp->sd_flags & FLG_SY_MVTOCOMM) == 0)))) {
1584 		Conv_inv_buf_t	inv_buf;
1585 
1586 		/*
1587 		 * If the relocation is against a SHT_SUNW_ANNOTATE
1588 		 * section - then silently ignore that the relocation
1589 		 * can not be resolved.
1590 		 */
1591 		if (rsp->rel_osdesc &&
1592 		    (rsp->rel_osdesc->os_shdr->sh_type == SHT_SUNW_ANNOTATE))
1593 			return (0);
1594 		(void) eprintf(ofl->ofl_lml, ERR_WARNING,
1595 		    MSG_INTL(MSG_REL_EXTERNSYM),
1596 		    conv_reloc_SPARC_type(rsp->rel_rtype, 0, &inv_buf),
1597 		    rsp->rel_isdesc->is_file->ifl_name,
1598 		    demangle(rsp->rel_sname), rsp->rel_osdesc->os_name);
1599 		return (1);
1600 	}
1601 
1602 	/*
1603 	 * Perform relocation.
1604 	 */
1605 	return (ld_add_actrel(NULL, rsp, ofl));
1606 }
1607 
1608 /*
1609  * Establish a relocation transition.  Note, at this point of input relocation
1610  * processing, we have no idea of the relocation value that will be used in
1611  * the eventual relocation calculation.  This value is only known after the
1612  * initial image has been constructed.  Therefore, there is a small chance
1613  * that a value can exceed the capabilities of the transitioned relocation.
1614  * One example might be the offset from the GOT to a symbol.
1615  *
1616  * The only instance of this failure discovered so far has been via the use of
1617  * ABS symbols to represent an external memory location.  This situation is
1618  * rare, since ABS symbols aren't typically generated by the compilers.
1619  * Therefore, our solution is to excluded ABS symbols from the transition
1620  * relocation possibilities.  As an additional safeguard, if an inappropriate
1621  * value is passed to the final relocation engine, a verification ("V")
1622  * relocation should trigger a fatal error condition.
1623  */
1624 uintptr_t
1625 ld_reloc_GOTOP(Boolean local, Rel_desc *rsp, Ofl_desc *ofl)
1626 {
1627 	Word	rtype = rsp->rel_rtype;
1628 
1629 	if (!local || (rsp->rel_sym->sd_sym->st_shndx == SHN_ABS)) {
1630 		/*
1631 		 * When binding to a external symbol, no fixups are required
1632 		 * and the GOTDATA_OP relocation can be ignored.
1633 		 */
1634 		if (rtype == R_SPARC_GOTDATA_OP)
1635 			return (1);
1636 		return (ld_reloc_GOT_relative(local, rsp, ofl));
1637 	}
1638 
1639 	/*
1640 	 * When binding to a local symbol the relocations can be transitioned:
1641 	 *
1642 	 *	R_*_GOTDATA_OP_HIX22 -> R_*_GOTDATA_HIX22
1643 	 *	R_*_GOTDATA_OP_LOX10 -> R_*_GOTDATA_LOX10
1644 	 *	R_*_GOTDATA_OP ->	instruction fixup
1645 	 */
1646 	return (ld_add_actrel(FLG_REL_GOTFIX, rsp, ofl));
1647 }
1648 
1649 uintptr_t
1650 ld_reloc_TLS(Boolean local, Rel_desc *rsp, Ofl_desc *ofl)
1651 {
1652 	Word		rtype = rsp->rel_rtype;
1653 	Sym_desc	*sdp = rsp->rel_sym;
1654 	Word		flags = ofl->ofl_flags;
1655 	Gotndx		*gnp;
1656 
1657 	/*
1658 	 * If we're building an executable - use either the IE or LE access
1659 	 * model.  If we're building a shared object process any IE model.
1660 	 */
1661 	if ((flags & FLG_OF_EXEC) || (IS_TLS_IE(rtype))) {
1662 		/*
1663 		 * Set the DF_STATIC_TLS flag.
1664 		 */
1665 		ofl->ofl_dtflags |= DF_STATIC_TLS;
1666 
1667 		if (!local || ((flags & FLG_OF_EXEC) == 0)) {
1668 			/*
1669 			 * When processing static TLS - these relocations
1670 			 * can be ignored.
1671 			 */
1672 			if ((rtype == R_SPARC_TLS_IE_LD) ||
1673 			    (rtype == R_SPARC_TLS_IE_LDX) ||
1674 			    (rtype == R_SPARC_TLS_IE_ADD))
1675 				return (1);
1676 
1677 			/*
1678 			 * Assign a GOT entry for IE static TLS references.
1679 			 */
1680 			if (((rtype == R_SPARC_TLS_GD_HI22) ||
1681 			    (rtype == R_SPARC_TLS_GD_LO10) ||
1682 			    (rtype == R_SPARC_TLS_IE_HI22) ||
1683 			    (rtype == R_SPARC_TLS_IE_LO10)) &&
1684 			    ((gnp = ld_find_gotndx(&(sdp->sd_GOTndxs),
1685 			    GOT_REF_TLSIE, ofl, rsp)) == 0)) {
1686 
1687 				if (ld_assign_got_TLS(local, rsp, ofl, sdp,
1688 				    gnp, GOT_REF_TLSIE, FLG_REL_STLS,
1689 				    rtype, M_R_TPOFF, 0) == S_ERROR)
1690 					return (S_ERROR);
1691 			}
1692 
1693 			/*
1694 			 * IE access model.
1695 			 */
1696 			if (IS_TLS_IE(rtype))
1697 				return (ld_add_actrel(FLG_REL_STLS, rsp, ofl));
1698 
1699 			/*
1700 			 * Fixups are required for other executable models.
1701 			 */
1702 			return (ld_add_actrel((FLG_REL_TLSFIX | FLG_REL_STLS),
1703 			    rsp, ofl));
1704 		}
1705 
1706 		/*
1707 		 * LE access model.
1708 		 */
1709 		if (IS_TLS_LE(rtype))
1710 			return (ld_add_actrel(FLG_REL_STLS, rsp, ofl));
1711 
1712 		/*
1713 		 * When processing static TLS - these relocations can be
1714 		 * ignored.
1715 		 */
1716 		if (rtype == R_SPARC_TLS_IE_ADD)
1717 			return (1);
1718 
1719 		return (ld_add_actrel((FLG_REL_TLSFIX | FLG_REL_STLS),
1720 		    rsp, ofl));
1721 	}
1722 
1723 	/*
1724 	 * Building a shared object.
1725 	 *
1726 	 * For dynamic TLS references, ADD relocations are ignored.
1727 	 */
1728 	if ((rtype == R_SPARC_TLS_GD_ADD) || (rtype == R_SPARC_TLS_LDM_ADD) ||
1729 	    (rtype == R_SPARC_TLS_LDO_ADD))
1730 		return (1);
1731 
1732 	/*
1733 	 * Assign a GOT entry for a dynamic TLS reference.
1734 	 */
1735 	if (((rtype == R_SPARC_TLS_LDM_HI22) ||
1736 	    (rtype == R_SPARC_TLS_LDM_LO10)) &&
1737 	    ((gnp = ld_find_gotndx(&(sdp->sd_GOTndxs), GOT_REF_TLSLD,
1738 	    ofl, rsp)) == 0)) {
1739 
1740 		if (ld_assign_got_TLS(local, rsp, ofl, sdp, gnp, GOT_REF_TLSLD,
1741 		    FLG_REL_MTLS, rtype, M_R_DTPMOD, 0) == S_ERROR)
1742 			return (S_ERROR);
1743 
1744 	} else if (((rtype == R_SPARC_TLS_GD_HI22) ||
1745 	    (rtype == R_SPARC_TLS_GD_LO10)) &&
1746 	    ((gnp = ld_find_gotndx(&(sdp->sd_GOTndxs), GOT_REF_TLSGD,
1747 	    ofl, rsp)) == 0)) {
1748 
1749 		if (ld_assign_got_TLS(local, rsp, ofl, sdp, gnp, GOT_REF_TLSGD,
1750 		    FLG_REL_DTLS, rtype, M_R_DTPMOD, M_R_DTPOFF) == S_ERROR)
1751 			return (S_ERROR);
1752 	}
1753 
1754 	/*
1755 	 * For GD/LD TLS reference - TLS_{GD,LD}_CALL, this will eventually
1756 	 * cause a call to __tls_get_addr().  Convert this relocation to that
1757 	 * symbol now, and prepare for the PLT magic.
1758 	 */
1759 	if ((rtype == R_SPARC_TLS_GD_CALL) || (rtype == R_SPARC_TLS_LDM_CALL)) {
1760 		Sym_desc	*tlsgetsym;
1761 
1762 		if ((tlsgetsym = ld_sym_add_u(MSG_ORIG(MSG_SYM_TLSGETADDR_U),
1763 		    ofl, MSG_STR_TLSREL)) == (Sym_desc *)S_ERROR)
1764 			return (S_ERROR);
1765 
1766 		rsp->rel_sym = tlsgetsym;
1767 		rsp->rel_sname = tlsgetsym->sd_name;
1768 		rsp->rel_rtype = R_SPARC_WPLT30;
1769 
1770 		if (ld_reloc_plt(rsp, ofl) == S_ERROR)
1771 			return (S_ERROR);
1772 
1773 		rsp->rel_sym = sdp;
1774 		rsp->rel_sname = sdp->sd_name;
1775 		rsp->rel_rtype = rtype;
1776 		return (1);
1777 	}
1778 
1779 	if (IS_TLS_LD(rtype))
1780 		return (ld_add_actrel(FLG_REL_MTLS, rsp, ofl));
1781 
1782 	return (ld_add_actrel(FLG_REL_DTLS, rsp, ofl));
1783 }
1784 
1785 /*
1786  * ld_allocate_got: if a GOT is to be made, after the section is built this
1787  * function is called to allocate all the GOT slots.  The allocation is
1788  * deferred until after all GOTs have been counted and sorted according
1789  * to their size, for only then will we know how to allocate them on
1790  * a processor like SPARC which has different models for addressing the
1791  * GOT.  SPARC has two: small and large, small uses a signed 13-bit offset
1792  * into the GOT, whereas large uses an unsigned 32-bit offset.
1793  */
1794 static	Sword small_index;	/* starting index for small GOT entries */
1795 static	Sword mixed_index;	/* starting index for mixed GOT entries */
1796 static	Sword large_index;	/* starting index for large GOT entries */
1797 
1798 uintptr_t
1799 ld_assign_got(Ofl_desc *ofl, Sym_desc * sdp)
1800 {
1801 	Listnode *	lnp;
1802 	Gotndx *	gnp;
1803 
1804 	for (LIST_TRAVERSE(&sdp->sd_GOTndxs, lnp, gnp)) {
1805 		uint_t	gotents;
1806 		Gotref	gref;
1807 		gref = gnp->gn_gotref;
1808 		if ((gref == GOT_REF_TLSGD) || (gref == GOT_REF_TLSLD))
1809 			gotents = 2;
1810 		else
1811 			gotents = 1;
1812 
1813 		switch (gnp->gn_gotndx) {
1814 		case M_GOT_SMALL:
1815 			gnp->gn_gotndx = small_index;
1816 			small_index += gotents;
1817 			if (small_index == 0)
1818 				small_index = M_GOT_XNumber;
1819 			break;
1820 		case M_GOT_MIXED:
1821 			gnp->gn_gotndx = mixed_index;
1822 			mixed_index += gotents;
1823 			break;
1824 		case M_GOT_LARGE:
1825 			gnp->gn_gotndx = large_index;
1826 			large_index += gotents;
1827 			break;
1828 		default:
1829 			eprintf(ofl->ofl_lml, ERR_FATAL,
1830 			    MSG_INTL(MSG_REL_ASSIGNGOT),
1831 			    EC_XWORD(gnp->gn_gotndx), demangle(sdp->sd_name));
1832 			return (S_ERROR);
1833 		}
1834 	}
1835 	return (1);
1836 }
1837 
1838 /*
1839  * Search the GOT index list for a GOT entry with the proper addend.
1840  */
1841 Gotndx *
1842 ld_find_gotndx(List * lst, Gotref gref, Ofl_desc * ofl, Rel_desc * rdesc)
1843 {
1844 	Listnode *	lnp;
1845 	Gotndx *	gnp;
1846 
1847 	if ((gref == GOT_REF_TLSLD) && ofl->ofl_tlsldgotndx)
1848 		return (ofl->ofl_tlsldgotndx);
1849 
1850 	for (LIST_TRAVERSE(lst, lnp, gnp)) {
1851 		if ((rdesc->rel_raddend == gnp->gn_addend) &&
1852 		    (gref == gnp->gn_gotref))
1853 			return (gnp);
1854 	}
1855 	return ((Gotndx *)0);
1856 }
1857 
1858 Xword
1859 ld_calc_got_offset(Rel_desc * rdesc, Ofl_desc * ofl)
1860 {
1861 	Os_desc		*osp = ofl->ofl_osgot;
1862 	Sym_desc	*sdp = rdesc->rel_sym;
1863 	Xword		gotndx;
1864 	Gotref		gref;
1865 	Gotndx		*gnp;
1866 
1867 	if (rdesc->rel_flags & FLG_REL_DTLS)
1868 		gref = GOT_REF_TLSGD;
1869 	else if (rdesc->rel_flags & FLG_REL_MTLS)
1870 		gref = GOT_REF_TLSLD;
1871 	else if (rdesc->rel_flags & FLG_REL_STLS)
1872 		gref = GOT_REF_TLSIE;
1873 	else
1874 		gref = GOT_REF_GENERIC;
1875 
1876 	gnp = ld_find_gotndx(&(sdp->sd_GOTndxs), gref, ofl, rdesc);
1877 	assert(gnp);
1878 
1879 	gotndx = (Xword)gnp->gn_gotndx;
1880 
1881 	if ((rdesc->rel_flags & FLG_REL_DTLS) &&
1882 	    (rdesc->rel_rtype == M_R_DTPOFF))
1883 		gotndx++;
1884 
1885 	return ((Xword)((osp->os_shdr->sh_addr) + (gotndx * M_GOT_ENTSIZE) +
1886 	    (-neggotoffset * M_GOT_ENTSIZE)));
1887 }
1888 
1889 uintptr_t
1890 ld_assign_got_ndx(List * lst, Gotndx * pgnp, Gotref gref, Ofl_desc * ofl,
1891     Rel_desc * rsp, Sym_desc * sdp)
1892 {
1893 	Xword		raddend;
1894 	Gotndx *	gnp, * _gnp;
1895 	Listnode *	lnp, * plnp;
1896 	uint_t		gotents;
1897 
1898 	/* Some TLS requires two relocations with two GOT entries */
1899 	if ((gref == GOT_REF_TLSGD) || (gref == GOT_REF_TLSLD))
1900 		gotents = 2;
1901 	else
1902 		gotents = 1;
1903 
1904 	raddend = rsp->rel_raddend;
1905 	if (pgnp && (pgnp->gn_addend == raddend) && (pgnp->gn_gotref == gref)) {
1906 
1907 		/*
1908 		 * If an entry for this addend already exists, determine if it
1909 		 * has mixed mode GOT access (both PIC and pic).
1910 		 *
1911 		 * In order to be accessible by both large and small pic,
1912 		 * a mixed mode GOT must be located in the positive index
1913 		 * range above _GLOBAL_OFFSET_TABLE_, and in the range
1914 		 * reachable small pic. This is necessary because the large
1915 		 * PIC mode cannot use a negative offset. This implies that
1916 		 * there can be no more than (M_GOT_MAXSMALL/2 - M_GOT_XNumber)
1917 		 * such entries.
1918 		 */
1919 		switch (pgnp->gn_gotndx) {
1920 		case M_GOT_SMALL:
1921 			/*
1922 			 * This one was previously identified as a small
1923 			 * GOT. If this access is large, then convert
1924 			 * it to mixed.
1925 			 */
1926 			if (rsp->rel_rtype != R_SPARC_GOT13) {
1927 				pgnp->gn_gotndx = M_GOT_MIXED;
1928 				mixgotcnt += gotents;
1929 			}
1930 			break;
1931 
1932 		case M_GOT_LARGE:
1933 			/*
1934 			 * This one was previously identified as a large
1935 			 * GOT. If this access is small, convert it to mixed.
1936 			 */
1937 			if (rsp->rel_rtype == R_SPARC_GOT13) {
1938 				smlgotcnt += gotents;
1939 				mixgotcnt += gotents;
1940 				pgnp->gn_gotndx = M_GOT_MIXED;
1941 				sdp->sd_flags |= FLG_SY_SMGOT;
1942 			}
1943 			break;
1944 		}
1945 		return (1);
1946 	}
1947 
1948 	plnp = 0;
1949 	for (LIST_TRAVERSE(lst, lnp, _gnp)) {
1950 		if (_gnp->gn_addend > raddend)
1951 			break;
1952 		plnp = lnp;
1953 	}
1954 
1955 	/*
1956 	 * Allocate a new entry.
1957 	 */
1958 	if ((gnp = libld_calloc(sizeof (Gotndx), 1)) == 0)
1959 		return (S_ERROR);
1960 	gnp->gn_addend = raddend;
1961 	gnp->gn_gotref = gref;
1962 	ofl->ofl_gotcnt += gotents;
1963 
1964 	if (rsp->rel_rtype == R_SPARC_GOT13) {
1965 		gnp->gn_gotndx = M_GOT_SMALL;
1966 		smlgotcnt += gotents;
1967 		sdp->sd_flags |= FLG_SY_SMGOT;
1968 	} else {
1969 		gnp->gn_gotndx = M_GOT_LARGE;
1970 	}
1971 
1972 	if (gref == GOT_REF_TLSLD) {
1973 		ofl->ofl_tlsldgotndx = gnp;
1974 		return (1);
1975 	}
1976 
1977 	if (plnp == 0) {
1978 		/*
1979 		 * Insert at head of list
1980 		 */
1981 		if (list_prependc(lst, (void *)gnp) == 0)
1982 			return (S_ERROR);
1983 	} else if (_gnp->gn_addend > raddend) {
1984 		/*
1985 		 * Insert in middle of lest
1986 		 */
1987 		if (list_insertc(lst, (void *)gnp, plnp) == 0)
1988 			return (S_ERROR);
1989 	} else {
1990 		/*
1991 		 * Append to tail of list
1992 		 */
1993 		if (list_appendc(lst, (void *)gnp) == 0)
1994 			return (S_ERROR);
1995 	}
1996 	return (1);
1997 }
1998 
1999 void
2000 ld_assign_plt_ndx(Sym_desc * sdp, Ofl_desc *ofl)
2001 {
2002 	sdp->sd_aux->sa_PLTndx = 1 + ofl->ofl_pltcnt++;
2003 }
2004 
2005 
2006 uintptr_t
2007 ld_allocate_got(Ofl_desc * ofl)
2008 {
2009 	const Sword	first_large_ndx = M_GOT_MAXSMALL / 2;
2010 	Sym_desc *	sdp;
2011 	Addr		addr;
2012 
2013 	/*
2014 	 * Sanity check -- is this going to fit at all? There are two
2015 	 * limits to be concerned about:
2016 	 *	1) There is a limit on the number of small pic GOT indices,
2017 	 *		given by M_GOT_MAXSMALL.
2018 	 *	2) If there are more than (M_GOT_MAXSMALL/2 - M_GOT_XNumber)
2019 	 *		small GOT indices, there will be items at negative
2020 	 *		offsets from _GLOBAL_OFFSET_TABLE_. Items that are
2021 	 *		accessed via large (PIC) code cannot reach these
2022 	 *		negative slots, so mixed mode items must be in the
2023 	 *		non-negative range. This implies a limit of
2024 	 *		(M_GOT_MAXSMALL/2 - M_GOT_XNumber) mixed mode indices.
2025 	 */
2026 	if (smlgotcnt > M_GOT_MAXSMALL) {
2027 		eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_REL_SMALLGOT),
2028 		    EC_WORD(smlgotcnt), M_GOT_MAXSMALL);
2029 		return (S_ERROR);
2030 	}
2031 	if (mixgotcnt > (first_large_ndx - M_GOT_XNumber)) {
2032 		eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_REL_MIXEDGOT),
2033 		    EC_WORD(mixgotcnt), first_large_ndx - M_GOT_XNumber);
2034 		return (S_ERROR);
2035 	}
2036 
2037 	/*
2038 	 * Set starting offset to be either 0, or a negative index into
2039 	 * the GOT based on the number of small symbols we've got.
2040 	 */
2041 	neggotoffset = ((smlgotcnt >= first_large_ndx) ?
2042 	    (first_large_ndx - smlgotcnt) : 0);
2043 
2044 	/*
2045 	 * Initialize the got offsets used by assign_got() to
2046 	 * locate GOT items:
2047 	 *	small - Starting index of items referenced only
2048 	 *		by small offsets (-Kpic).
2049 	 *	mixed - Starting index of items referenced
2050 	 *		by both large (-KPIC) and small (-Kpic).
2051 	 *	large - Indexes referenced only by large (-KPIC)
2052 	 *
2053 	 *  Small items can have negative indexes (i.e. lie below
2054 	 *	_GLOBAL_OFFSET_TABLE_). Mixed and large items must have
2055 	 *	non-negative offsets.
2056 	 */
2057 	small_index = (neggotoffset == 0) ? M_GOT_XNumber : neggotoffset;
2058 	large_index = neggotoffset + smlgotcnt;
2059 	mixed_index = large_index - mixgotcnt;
2060 
2061 	/*
2062 	 * Assign bias to GOT symbols.
2063 	 */
2064 	addr = -neggotoffset * M_GOT_ENTSIZE;
2065 	if (sdp = ld_sym_find(MSG_ORIG(MSG_SYM_GOFTBL), SYM_NOHASH, 0, ofl))
2066 		sdp->sd_sym->st_value = addr;
2067 	if (sdp = ld_sym_find(MSG_ORIG(MSG_SYM_GOFTBL_U), SYM_NOHASH, 0, ofl))
2068 		sdp->sd_sym->st_value = addr;
2069 
2070 	if (ofl->ofl_tlsldgotndx) {
2071 		ofl->ofl_tlsldgotndx->gn_gotndx = large_index;
2072 		large_index += 2;
2073 	}
2074 	return (1);
2075 }
2076 
2077 /*
2078  * Initializes .got[0] with the _DYNAMIC symbol value.
2079  */
2080 uintptr_t
2081 ld_fillin_gotplt(Ofl_desc *ofl)
2082 {
2083 	if (ofl->ofl_osgot) {
2084 		Sym_desc	*sdp;
2085 
2086 		if ((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_DYNAMIC_U),
2087 		    SYM_NOHASH, 0, ofl)) != NULL) {
2088 			uchar_t	*genptr;
2089 
2090 			genptr = ((uchar_t *)ofl->ofl_osgot->os_outdata->d_buf +
2091 			    (-neggotoffset * M_GOT_ENTSIZE) +
2092 			    (M_GOT_XDYNAMIC * M_GOT_ENTSIZE));
2093 			/* LINTED */
2094 			*((Xword *)genptr) = sdp->sd_sym->st_value;
2095 		}
2096 	}
2097 	return (1);
2098 }
2099