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