xref: /titanic_44/usr/src/cmd/sgs/libld/common/machrel.sparc.c (revision be653f2ebb94b16135c96dea55b464d5c1480bd7)
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 	 * addend field for R_SPARC_TLS_DTPMOD32 &&
680 	 * R_SPARC_TLS_DTPMOD64 mean nothing.  The addend
681 	 * is propogated in the corresponding R_SPARC_TLS_DTPOFF*
682 	 * relocations.
683 	 */
684 	if (orsp->rel_rtype == M_R_DTPMOD) {
685 		raddend = 0;
686 	}
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 			    rtype, R_SPARC_TLS_IE_HI22, arsp->rel_roffset,
759 			    sdp->sd_name));
760 			arsp->rel_rtype = R_SPARC_TLS_IE_HI22;
761 			return (FIX_RELOC);
762 
763 		case R_SPARC_TLS_GD_LO10:
764 			DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
765 			    rtype, R_SPARC_TLS_IE_LO10, arsp->rel_roffset,
766 			    sdp->sd_name));
767 			arsp->rel_rtype = R_SPARC_TLS_IE_LO10;
768 			return (FIX_RELOC);
769 
770 		case R_SPARC_TLS_GD_ADD:
771 			DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
772 			    rtype, R_SPARC_NONE, arsp->rel_roffset,
773 			    sdp->sd_name));
774 			*offset = (TLS_GD_IE_LD |
775 			    (*offset & (FM3_REG_MSK_RS1 | FM3_REG_MSK_RS2)));
776 			return (FIX_DONE);
777 
778 		case R_SPARC_TLS_GD_CALL:
779 			DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
780 			    rtype, R_SPARC_NONE, arsp->rel_roffset,
781 			    sdp->sd_name));
782 			*offset = TLS_GD_IE_ADD;
783 			return (FIX_DONE);
784 		}
785 		return (FIX_RELOC);
786 	}
787 
788 	/*
789 	 * LE reference model
790 	 */
791 	switch (rtype) {
792 	case R_SPARC_TLS_IE_HI22:
793 	case R_SPARC_TLS_GD_HI22:
794 	case R_SPARC_TLS_LDO_HIX22:
795 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH, rtype,
796 		    R_SPARC_TLS_LE_HIX22, arsp->rel_roffset, sdp->sd_name));
797 		arsp->rel_rtype = R_SPARC_TLS_LE_HIX22;
798 		return (FIX_RELOC);
799 
800 	case R_SPARC_TLS_LDO_LOX10:
801 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH, rtype,
802 		    R_SPARC_TLS_LE_LOX10, arsp->rel_roffset, sdp->sd_name));
803 		arsp->rel_rtype = R_SPARC_TLS_LE_LOX10;
804 		return (FIX_RELOC);
805 
806 	case R_SPARC_TLS_IE_LO10:
807 	case R_SPARC_TLS_GD_LO10:
808 		/*
809 		 * Current instruction is:
810 		 *
811 		 *	or r1, %lo(x), r2
812 		 *		or
813 		 *	add r1, %lo(x), r2
814 		 *
815 		 *
816 		 * Need to udpate this to:
817 		 *
818 		 *	xor r1, %lox(x), r2
819 		 */
820 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH, rtype,
821 		    R_SPARC_TLS_LE_LOX10, arsp->rel_roffset, sdp->sd_name));
822 		*offset = TLS_GD_LE_XOR |
823 		    (*offset & (FM3_REG_MSK_RS1 | FM3_REG_MSK_RD));
824 		arsp->rel_rtype = R_SPARC_TLS_LE_LOX10;
825 		return (FIX_RELOC);
826 
827 	case R_SPARC_TLS_IE_LD:
828 	case R_SPARC_TLS_IE_LDX:
829 		/*
830 		 * Current instruction:
831 		 * 	ld{x}	[r1 + r2], r3
832 		 *
833 		 * Need to update this to:
834 		 *
835 		 *	mov	r2, r3   (or  %g0, r2, r3)
836 		 */
837 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH, rtype,
838 		    R_SPARC_NONE, arsp->rel_roffset, sdp->sd_name));
839 		*offset = ((*offset) & (FM3_REG_MSK_RS2 | FM3_REG_MSK_RD)) |
840 		    TLS_IE_LE_OR;
841 		return (FIX_DONE);
842 
843 	case R_SPARC_TLS_LDO_ADD:
844 	case R_SPARC_TLS_GD_ADD:
845 		/*
846 		 * Current instruction is:
847 		 *
848 		 *	add gptr_reg, r2, r3
849 		 *
850 		 * Need to updated this to:
851 		 *
852 		 *	add %g7, r2, r3
853 		 */
854 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH, rtype,
855 		    R_SPARC_NONE, arsp->rel_roffset, sdp->sd_name));
856 		*offset = *offset & (~FM3_REG_MSK_RS1);
857 		*offset = *offset | (REG_G7 << 14);
858 		return (FIX_DONE);
859 
860 	case R_SPARC_TLS_LDM_CALL:
861 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH, rtype,
862 		    R_SPARC_NONE, arsp->rel_roffset, sdp->sd_name));
863 		*offset = TLS_LD_LE_CLRO0;
864 		return (FIX_DONE);
865 
866 	case R_SPARC_TLS_LDM_HI22:
867 	case R_SPARC_TLS_LDM_LO10:
868 	case R_SPARC_TLS_LDM_ADD:
869 	case R_SPARC_TLS_IE_ADD:
870 	case R_SPARC_TLS_GD_CALL:
871 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH, rtype,
872 		    R_SPARC_NONE, arsp->rel_roffset, sdp->sd_name));
873 		*offset = M_NOP;
874 		return (FIX_DONE);
875 	}
876 	return (FIX_RELOC);
877 }
878 
879 #define	GOTOP_ADDINST	0x80000000	/* add %g0, %g0, %g0 */
880 
881 static Fixupret
882 gotop_fixups(Ofl_desc *ofl, Rel_desc *arsp)
883 {
884 	Sym_desc	*sdp = arsp->rel_sym;
885 	Word		rtype = arsp->rel_rtype;
886 	uint_t		*offset;
887 	const char	*ifl_name;
888 
889 	switch (rtype) {
890 	case R_SPARC_GOTDATA_OP_HIX22:
891 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH, rtype,
892 		    R_SPARC_GOTDATA_HIX22, arsp->rel_roffset, sdp->sd_name));
893 		arsp->rel_rtype = R_SPARC_GOTDATA_HIX22;
894 		return (FIX_RELOC);
895 
896 	case R_SPARC_GOTDATA_OP_LOX10:
897 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH, rtype,
898 		    R_SPARC_GOTDATA_LOX10, arsp->rel_roffset, sdp->sd_name));
899 		arsp->rel_rtype = R_SPARC_GOTDATA_LOX10;
900 		return (FIX_RELOC);
901 
902 	case R_SPARC_GOTDATA_OP:
903 		/*
904 		 * Current instruction:
905 		 * 	ld{x}	[r1 + r2], r3
906 		 *
907 		 * Need to update this to:
908 		 *
909 		 *	add	r1, r2, r3
910 		 */
911 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH, rtype,
912 		    R_SPARC_NONE, arsp->rel_roffset, sdp->sd_name));
913 		offset = (uint_t *)(uintptr_t)(arsp->rel_roffset +
914 		    _elf_getxoff(arsp->rel_isdesc->is_indata) +
915 		    (uintptr_t)arsp->rel_osdesc->os_outdata->d_buf);
916 
917 		*offset = ((*offset) & (FM3_REG_MSK_RS1 |
918 		    FM3_REG_MSK_RS2 | FM3_REG_MSK_RD)) | GOTOP_ADDINST;
919 		return (FIX_DONE);
920 	}
921 	/*
922 	 * We should not get here
923 	 */
924 	if (arsp->rel_isdesc->is_file)
925 		ifl_name = arsp->rel_isdesc->is_file->ifl_name;
926 	else
927 		ifl_name = MSG_INTL(MSG_STR_NULL);
928 
929 	eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_REL_BADGOTFIX),
930 	    conv_reloc_SPARC_type(arsp->rel_rtype, 0),
931 	    ifl_name, demangle(arsp->rel_sname));
932 
933 	assert(0);
934 	return (FIX_ERROR);
935 }
936 
937 uintptr_t
938 ld_do_activerelocs(Ofl_desc *ofl)
939 {
940 	Rel_desc	*arsp;
941 	Rel_cache	*rcp;
942 	Listnode	*lnp;
943 	uintptr_t	return_code = 1;
944 	Word		flags = ofl->ofl_flags;
945 	Word		dtflags1 = ofl->ofl_dtflags_1;
946 
947 	DBG_CALL(Dbg_reloc_doact_title(ofl->ofl_lml));
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 			} else {
1047 				/*
1048 				 * else the value is the symbols value
1049 				 */
1050 				value = sdp->sd_sym->st_value;
1051 			}
1052 
1053 			/*
1054 			 * Relocation against the GLOBAL_OFFSET_TABLE.
1055 			 */
1056 			if (arsp->rel_flags & FLG_REL_GOT)
1057 				arsp->rel_osdesc = ofl->ofl_osgot;
1058 
1059 			/*
1060 			 * If loadable and not producing a relocatable object
1061 			 * add the sections virtual address to the reference
1062 			 * address.
1063 			 */
1064 			if ((arsp->rel_flags & FLG_REL_LOAD) &&
1065 			    ((flags & FLG_OF_RELOBJ) == 0))
1066 				refaddr += arsp->rel_isdesc->is_osdesc->
1067 				    os_shdr->sh_addr;
1068 
1069 			/*
1070 			 * If this entry has a PLT assigned to it, it's
1071 			 * value is actually the address of the PLT (and
1072 			 * not the address of the function).
1073 			 */
1074 			if (IS_PLT(arsp->rel_rtype)) {
1075 				if (sdp->sd_aux && sdp->sd_aux->sa_PLTndx)
1076 					value = ld_calc_plt_addr(sdp, ofl);
1077 			}
1078 
1079 			/*
1080 			 * Add relocations addend to value.  Add extra
1081 			 * relocation addend if needed.
1082 			 */
1083 			value += arsp->rel_raddend;
1084 			if (IS_EXTOFFSET(arsp->rel_rtype))
1085 				value += arsp->rel_typedata;
1086 
1087 			/*
1088 			 * Determine whether the value needs further adjustment.
1089 			 * Filter through the attributes of the relocation to
1090 			 * determine what adjustment is required.  Note, many
1091 			 * of the following cases are only applicable when a
1092 			 * .got is present.  As a .got is not generated when a
1093 			 * relocatable object is being built, any adjustments
1094 			 * that require a .got need to be skipped.
1095 			 */
1096 			if ((arsp->rel_flags & FLG_REL_GOT) &&
1097 			    ((flags & FLG_OF_RELOBJ) == 0)) {
1098 				Xword		R1addr;
1099 				uintptr_t	R2addr;
1100 				Sword		gotndx;
1101 				Gotndx		*gnp;
1102 				Gotref		gref;
1103 
1104 				/*
1105 				 * Clear the GOT table entry, on SPARC we clear
1106 				 * the entry and the 'value' if needed is stored
1107 				 * in an output relocations addend.
1108 				 *
1109 				 * Calculate offset into GOT at which to apply
1110 				 * the relocation.
1111 				 */
1112 				if (arsp->rel_flags & FLG_REL_DTLS)
1113 					gref = GOT_REF_TLSGD;
1114 				else if (arsp->rel_flags & FLG_REL_MTLS)
1115 					gref = GOT_REF_TLSLD;
1116 				else if (arsp->rel_flags & FLG_REL_STLS)
1117 					gref = GOT_REF_TLSIE;
1118 				else
1119 					gref = GOT_REF_GENERIC;
1120 
1121 				gnp = ld_find_gotndx(&(sdp->sd_GOTndxs), gref,
1122 				    ofl, arsp);
1123 				assert(gnp);
1124 
1125 				if (arsp->rel_rtype == M_R_DTPOFF)
1126 					gotndx = gnp->gn_gotndx + 1;
1127 				else
1128 					gotndx = gnp->gn_gotndx;
1129 
1130 				/* LINTED */
1131 				R1addr = (Xword)((-neggotoffset *
1132 				    M_GOT_ENTSIZE) + (gotndx * M_GOT_ENTSIZE));
1133 
1134 				/*
1135 				 * Add the GOTs data's offset.
1136 				 */
1137 				R2addr = R1addr + (uintptr_t)
1138 				    arsp->rel_osdesc->os_outdata->d_buf;
1139 
1140 				DBG_CALL(Dbg_reloc_doact(ofl->ofl_lml,
1141 				    ELF_DBG_LD, M_MACH, SHT_RELA,
1142 				    arsp->rel_rtype, R1addr, value,
1143 				    arsp->rel_sname, arsp->rel_osdesc));
1144 
1145 				/*
1146 				 * And do it.
1147 				 */
1148 				*(Xword *)R2addr = value;
1149 				continue;
1150 
1151 			} else if (IS_GOT_BASED(arsp->rel_rtype) &&
1152 			    ((flags & FLG_OF_RELOBJ) == 0)) {
1153 				value -= (ofl->ofl_osgot->os_shdr->sh_addr +
1154 					(-neggotoffset * M_GOT_ENTSIZE));
1155 
1156 			} else if (IS_PC_RELATIVE(arsp->rel_rtype)) {
1157 				value -= refaddr;
1158 
1159 			} else if (IS_TLS_INS(arsp->rel_rtype) &&
1160 			    IS_GOT_RELATIVE(arsp->rel_rtype) &&
1161 			    ((flags & FLG_OF_RELOBJ) == 0)) {
1162 				Gotndx	*gnp;
1163 				Gotref	gref;
1164 
1165 				if (arsp->rel_flags & FLG_REL_STLS)
1166 					gref = GOT_REF_TLSIE;
1167 				else if (arsp->rel_flags & FLG_REL_DTLS)
1168 					gref = GOT_REF_TLSGD;
1169 				else if (arsp->rel_flags & FLG_REL_MTLS)
1170 					gref = GOT_REF_TLSLD;
1171 
1172 				gnp = ld_find_gotndx(&(sdp->sd_GOTndxs), gref,
1173 				    ofl, arsp);
1174 				assert(gnp);
1175 
1176 				value = gnp->gn_gotndx * M_GOT_ENTSIZE;
1177 
1178 			} else if (IS_GOT_RELATIVE(arsp->rel_rtype) &&
1179 			    ((flags & FLG_OF_RELOBJ) == 0)) {
1180 				Gotndx	*gnp;
1181 
1182 				gnp = ld_find_gotndx(&(sdp->sd_GOTndxs),
1183 				    GOT_REF_GENERIC, ofl, arsp);
1184 				assert(gnp);
1185 
1186 				value = gnp->gn_gotndx * M_GOT_ENTSIZE;
1187 
1188 			} else if ((arsp->rel_flags & FLG_REL_STLS) &&
1189 			    ((flags & FLG_OF_RELOBJ) == 0)) {
1190 				Xword	tlsstatsize;
1191 
1192 				/*
1193 				 * This is the LE TLS
1194 				 * reference model.  Static offset
1195 				 * is hard-coded, and negated so that
1196 				 * it can be added to the thread pointer (%g7)
1197 				 */
1198 				tlsstatsize = S_ROUND(ofl->
1199 				    ofl_tlsphdr->p_memsz, M_TLSSTATALIGN);
1200 				value = -(tlsstatsize - value);
1201 			}
1202 
1203 			if (arsp->rel_isdesc->is_file)
1204 				ifl_name = arsp->rel_isdesc->is_file->ifl_name;
1205 			else
1206 				ifl_name = MSG_INTL(MSG_STR_NULL);
1207 
1208 			/*
1209 			 * Make sure we have data to relocate.  Compiler and
1210 			 * assembler developers have been known to generate
1211 			 * relocations against invalid sections (normally .bss),
1212 			 * so for their benefit give them sufficient information
1213 			 * to help analyze the problem.  End users should never
1214 			 * see this.
1215 			 */
1216 			if (arsp->rel_isdesc->is_indata->d_buf == 0) {
1217 				eprintf(ofl->ofl_lml, ERR_FATAL,
1218 				    MSG_INTL(MSG_REL_EMPTYSEC),
1219 				    conv_reloc_SPARC_type(arsp->rel_rtype, 0),
1220 				    ifl_name, demangle(arsp->rel_sname),
1221 				    arsp->rel_isdesc->is_name);
1222 				return (S_ERROR);
1223 			}
1224 
1225 			/*
1226 			 * Get the address of the data item we need to modify.
1227 			 */
1228 			addr = (uchar_t *)((uintptr_t)arsp->rel_roffset +
1229 			    (uintptr_t)_elf_getxoff(arsp->rel_isdesc->
1230 			    is_indata));
1231 
1232 			/*LINTED*/
1233 			DBG_CALL(Dbg_reloc_doact(ofl->ofl_lml, ELF_DBG_LD,
1234 			    M_MACH, SHT_RELA, arsp->rel_rtype, EC_NATPTR(addr),
1235 			    value, arsp->rel_sname, arsp->rel_osdesc));
1236 			addr += (uintptr_t)arsp->rel_osdesc->os_outdata->d_buf;
1237 
1238 			if ((((uintptr_t)addr - (uintptr_t)ofl->ofl_nehdr) >
1239 			    ofl->ofl_size) || (arsp->rel_roffset >
1240 			    arsp->rel_osdesc->os_shdr->sh_size)) {
1241 				int	class;
1242 
1243 				if (((uintptr_t)addr -
1244 				    (uintptr_t)ofl->ofl_nehdr) > ofl->ofl_size)
1245 					class = ERR_FATAL;
1246 				else
1247 					class = ERR_WARNING;
1248 
1249 				eprintf(ofl->ofl_lml, class,
1250 				    MSG_INTL(MSG_REL_INVALOFFSET),
1251 				    conv_reloc_SPARC_type(arsp->rel_rtype, 0),
1252 				    ifl_name, arsp->rel_isdesc->is_name,
1253 				    demangle(arsp->rel_sname),
1254 				    EC_ADDR((uintptr_t)addr -
1255 				    (uintptr_t)ofl->ofl_nehdr));
1256 
1257 				if (class == ERR_FATAL) {
1258 					return_code = S_ERROR;
1259 					continue;
1260 				}
1261 			}
1262 
1263 			/*
1264 			 * If '-z noreloc' is specified - skip the do_reloc
1265 			 * stage.
1266 			 */
1267 			if ((flags & FLG_OF_RELOBJ) ||
1268 			    !(dtflags1 & DF_1_NORELOC)) {
1269 				if (do_reloc((uchar_t)arsp->rel_rtype, addr,
1270 				    &value, arsp->rel_sname, ifl_name,
1271 				    ofl->ofl_lml) == 0)
1272 					return_code = S_ERROR;
1273 			}
1274 		}
1275 	}
1276 	return (return_code);
1277 }
1278 
1279 uintptr_t
1280 ld_add_outrel(Word flags, Rel_desc *rsp, Ofl_desc *ofl)
1281 {
1282 	Rel_desc	*orsp;
1283 	Rel_cache	*rcp;
1284 	Sym_desc	*sdp = rsp->rel_sym;
1285 
1286 	/*
1287 	 * Static executables *do not* want any relocations against them.
1288 	 * Since our engine still creates relocations against a WEAK UNDEFINED
1289 	 * symbol in a static executable, it's best to disable them here
1290 	 * instead of through out the relocation code.
1291 	 */
1292 	if ((ofl->ofl_flags & (FLG_OF_STATIC | FLG_OF_EXEC)) ==
1293 	    (FLG_OF_STATIC | FLG_OF_EXEC))
1294 		return (1);
1295 
1296 	/*
1297 	 * Certain relocations do not make sense in a 64bit shared object,
1298 	 * if building a shared object do a sanity check on the output
1299 	 * relocations being created.
1300 	 */
1301 	if (ofl->ofl_flags & FLG_OF_SHAROBJ) {
1302 		Word	rtype = rsp->rel_rtype;
1303 		/*
1304 		 * Because the R_SPARC_HIPLT22 & R_SPARC_LOPLT10 relocations
1305 		 * are not relative they make no sense to create in a shared
1306 		 * object - so emit the proper error message if that occurs.
1307 		 */
1308 		if ((rtype == R_SPARC_HIPLT22) ||
1309 		    (rtype == R_SPARC_LOPLT10)) {
1310 			eprintf(ofl->ofl_lml, ERR_FATAL,
1311 			    MSG_INTL(MSG_REL_UNRELREL),
1312 			    conv_reloc_SPARC_type(rsp->rel_rtype, 0),
1313 			    rsp->rel_isdesc->is_file->ifl_name,
1314 			    demangle(rsp->rel_sname));
1315 			return (S_ERROR);
1316 		}
1317 #if	defined(_ELF64)
1318 		/*
1319 		 * Each of the following relocations requires that the
1320 		 * object being built be loaded in either the upper 32 or
1321 		 * 44 bit range of memory.  Since shared libraries traditionally
1322 		 * are loaded in the lower range of memory - this isn't going
1323 		 * to work.
1324 		 */
1325 		if ((rtype == R_SPARC_H44) || (rtype == R_SPARC_M44) ||
1326 		    (rtype == R_SPARC_L44)) {
1327 			eprintf(ofl->ofl_lml, ERR_FATAL,
1328 			    MSG_INTL(MSG_REL_SHOBJABS44),
1329 			    conv_reloc_SPARC_type(rsp->rel_rtype, 0),
1330 			    rsp->rel_isdesc->is_file->ifl_name,
1331 			    demangle(rsp->rel_sname));
1332 			return (S_ERROR);
1333 		}
1334 #endif
1335 	}
1336 
1337 	/*
1338 	 * If no relocation cache structures are available allocate
1339 	 * a new one and link it into the cache list.
1340 	 */
1341 	if ((ofl->ofl_outrels.tail == 0) ||
1342 	    ((rcp = (Rel_cache *)ofl->ofl_outrels.tail->data) == 0) ||
1343 	    ((orsp = rcp->rc_free) == rcp->rc_end)) {
1344 		static size_t	nextsize = 0;
1345 		size_t		size;
1346 
1347 		/*
1348 		 * Output relocation numbers can vary considerably between
1349 		 * building executables or shared objects (pic vs. non-pic),
1350 		 * etc.  But, they typically aren't very large, so for these
1351 		 * objects use a standard bucket size.  For building relocatable
1352 		 * objects, typically there will be an output relocation for
1353 		 * every input relocation.
1354 		 */
1355 		if (nextsize == 0) {
1356 			if (ofl->ofl_flags & FLG_OF_RELOBJ) {
1357 				if ((size = ofl->ofl_relocincnt) == 0)
1358 					size = REL_LOIDESCNO;
1359 				if (size > REL_HOIDESCNO)
1360 					nextsize = REL_HOIDESCNO;
1361 				else
1362 					nextsize = REL_LOIDESCNO;
1363 			} else
1364 				nextsize = size = REL_HOIDESCNO;
1365 		} else
1366 			size = nextsize;
1367 
1368 		size = size * sizeof (Rel_desc);
1369 
1370 		if (((rcp = libld_malloc(sizeof (Rel_cache) + size)) == 0) ||
1371 		    (list_appendc(&ofl->ofl_outrels, rcp) == 0))
1372 			return (S_ERROR);
1373 
1374 		/* LINTED */
1375 		rcp->rc_free = orsp = (Rel_desc *)(rcp + 1);
1376 		/* LINTED */
1377 		rcp->rc_end = (Rel_desc *)((char *)rcp->rc_free + size);
1378 	}
1379 
1380 	/*
1381 	 * If we are adding a output relocation against a section
1382 	 * symbol (non-RELATIVE) then mark that section.  These sections
1383 	 * will be added to the .dynsym symbol table.
1384 	 */
1385 	if (sdp && (rsp->rel_rtype != M_R_RELATIVE) &&
1386 	    ((flags & FLG_REL_SCNNDX) ||
1387 	    (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION))) {
1388 
1389 		/*
1390 		 * If this is a COMMON symbol - no output section
1391 		 * exists yet - (it's created as part of sym_validate()).
1392 		 * So - we mark here that when it's created it should
1393 		 * be tagged with the FLG_OS_OUTREL flag.
1394 		 */
1395 		if ((sdp->sd_flags & FLG_SY_SPECSEC) &&
1396 		    (sdp->sd_sym->st_shndx == SHN_COMMON)) {
1397 			if (ELF_ST_TYPE(sdp->sd_sym->st_info) != STT_TLS)
1398 				ofl->ofl_flags1 |= FLG_OF1_BSSOREL;
1399 			else
1400 				ofl->ofl_flags1 |= FLG_OF1_TLSOREL;
1401 		} else {
1402 			Os_desc	*osp = sdp->sd_isc->is_osdesc;
1403 
1404 			if ((osp->os_flags & FLG_OS_OUTREL) == 0) {
1405 				ofl->ofl_dynshdrcnt++;
1406 				osp->os_flags |= FLG_OS_OUTREL;
1407 			}
1408 		}
1409 	}
1410 
1411 	*orsp = *rsp;
1412 	orsp->rel_flags |= flags;
1413 
1414 	rcp->rc_free++;
1415 	ofl->ofl_outrelscnt++;
1416 
1417 	if (flags & FLG_REL_GOT)
1418 		ofl->ofl_relocgotsz += (Xword)sizeof (Rela);
1419 	else if (flags & FLG_REL_PLT)
1420 		ofl->ofl_relocpltsz += (Xword)sizeof (Rela);
1421 	else if (flags & FLG_REL_BSS)
1422 		ofl->ofl_relocbsssz += (Xword)sizeof (Rela);
1423 	else if (flags & FLG_REL_NOINFO)
1424 		ofl->ofl_relocrelsz += (Xword)sizeof (Rela);
1425 	else
1426 		orsp->rel_osdesc->os_szoutrels += (Xword)sizeof (Rela);
1427 
1428 	if (orsp->rel_rtype == M_R_RELATIVE)
1429 		ofl->ofl_relocrelcnt++;
1430 
1431 #if	defined(_ELF64)
1432 	/*
1433 	 * When building a 64-bit object any R_SPARC_WDISP30 relocation is given
1434 	 * a plt padding entry, unless we're building a relocatable object
1435 	 * (ld -r) or -b is in effect.
1436 	 */
1437 	if ((orsp->rel_rtype == R_SPARC_WDISP30) &&
1438 	    ((ofl->ofl_flags & (FLG_OF_BFLAG | FLG_OF_RELOBJ)) == 0) &&
1439 	    ((orsp->rel_sym->sd_flags & FLG_SY_PLTPAD) == 0)) {
1440 		ofl->ofl_pltpad++;
1441 		orsp->rel_sym->sd_flags |= FLG_SY_PLTPAD;
1442 	}
1443 #endif
1444 	/*
1445 	 * We don't perform sorting on PLT relocations because
1446 	 * they have already been assigned a PLT index and if we
1447 	 * were to sort them we would have to re-assign the plt indexes.
1448 	 */
1449 	if (!(flags & FLG_REL_PLT))
1450 		ofl->ofl_reloccnt++;
1451 
1452 	/*
1453 	 * Insure a GLOBAL_OFFSET_TABLE is generated if required.
1454 	 */
1455 	if (IS_GOT_REQUIRED(orsp->rel_rtype))
1456 		ofl->ofl_flags |= FLG_OF_BLDGOT;
1457 
1458 	/*
1459 	 * Identify and possibly warn of a displacement relocation.
1460 	 */
1461 	if (orsp->rel_flags & FLG_REL_DISP) {
1462 		ofl->ofl_dtflags_1 |= DF_1_DISPRELPND;
1463 
1464 		if (ofl->ofl_flags & FLG_OF_VERBOSE)
1465 			ld_disp_errmsg(MSG_INTL(MSG_REL_DISPREL4), orsp, ofl);
1466 	}
1467 	DBG_CALL(Dbg_reloc_ors_entry(ofl->ofl_lml, ELF_DBG_LD, SHT_RELA,
1468 	    M_MACH, orsp));
1469 	return (1);
1470 }
1471 
1472 /*
1473  * Process relocation against a register symbol.  Note, of -z muldefs is in
1474  * effect there may have been multiple register definitions, which would have
1475  * been processed as non-fatal, with the first definition winning.  But, we
1476  * will also process multiple relocations for these multiple definitions.  In
1477  * this case we must only preserve the relocation for the definition that was
1478  * kept.  The sad part is that register relocations don't typically specify
1479  * the register symbol with which they are associated, so we might have to
1480  * search the input files global symbols to determine if this relocation is
1481  * appropriate.
1482  */
1483 uintptr_t
1484 ld_reloc_register(Rel_desc * rsp, Is_desc * isp, Ofl_desc * ofl)
1485 {
1486 	if (ofl->ofl_flags & FLG_OF_MULDEFS) {
1487 		Ifl_desc *	ifl = isp->is_file;
1488 		Sym_desc *	sdp = rsp->rel_sym;
1489 
1490 		if (sdp == 0) {
1491 			Xword		offset = rsp->rel_roffset;
1492 			Word		ndx;
1493 
1494 			for (ndx = ifl->ifl_locscnt;
1495 			    ndx < ifl->ifl_symscnt; ndx++) {
1496 				if (((sdp = ifl->ifl_oldndx[ndx]) != 0) &&
1497 				    (sdp->sd_flags & FLG_SY_REGSYM) &&
1498 				    (sdp->sd_sym->st_value == offset))
1499 					break;
1500 			}
1501 		}
1502 		if (sdp && (sdp->sd_file != ifl))
1503 			return (1);
1504 	}
1505 	return (ld_add_outrel((rsp->rel_flags | FLG_REL_REG), rsp, ofl));
1506 }
1507 
1508 /*
1509  * process relocation for a LOCAL symbol
1510  */
1511 uintptr_t
1512 ld_reloc_local(Rel_desc * rsp, Ofl_desc * ofl)
1513 {
1514 	Word		flags = ofl->ofl_flags;
1515 	Sym_desc	*sdp = rsp->rel_sym;
1516 	Word		shndx = sdp->sd_sym->st_shndx;
1517 
1518 	/*
1519 	 * if ((shared object) and (not pc relative relocation) and
1520 	 *    (not against ABS symbol))
1521 	 * then
1522 	 *	if (rtype != R_SPARC_32)
1523 	 *	then
1524 	 *		build relocation against section
1525 	 *	else
1526 	 *		build R_SPARC_RELATIVE
1527 	 *	fi
1528 	 * fi
1529 	 */
1530 	if ((flags & FLG_OF_SHAROBJ) && (rsp->rel_flags & FLG_REL_LOAD) &&
1531 	    !(IS_PC_RELATIVE(rsp->rel_rtype)) &&
1532 	    !(IS_GOT_BASED(rsp->rel_rtype)) &&
1533 	    !(rsp->rel_isdesc != NULL &&
1534 	    (rsp->rel_isdesc->is_shdr->sh_type == SHT_SUNW_dof)) &&
1535 	    (((sdp->sd_flags & FLG_SY_SPECSEC) == 0) ||
1536 	    (shndx != SHN_ABS) || (sdp->sd_aux && sdp->sd_aux->sa_symspec))) {
1537 		Word	ortype = rsp->rel_rtype;
1538 
1539 		if ((rsp->rel_rtype != R_SPARC_32) &&
1540 		    (rsp->rel_rtype != R_SPARC_PLT32) &&
1541 		    (rsp->rel_rtype != R_SPARC_64))
1542 			return (ld_add_outrel((FLG_REL_SCNNDX | FLG_REL_ADVAL),
1543 			    rsp, ofl));
1544 
1545 		rsp->rel_rtype = R_SPARC_RELATIVE;
1546 		if (ld_add_outrel(FLG_REL_ADVAL, rsp, ofl) == S_ERROR)
1547 			return (S_ERROR);
1548 		rsp->rel_rtype = ortype;
1549 		return (1);
1550 	}
1551 
1552 	/*
1553 	 * If the relocation is against a 'non-allocatable' section
1554 	 * and we can not resolve it now - then give a warning
1555 	 * message.
1556 	 *
1557 	 * We can not resolve the symbol if either:
1558 	 *	a) it's undefined
1559 	 *	b) it's defined in a shared library and a
1560 	 *	   COPY relocation hasn't moved it to the executable
1561 	 *
1562 	 * Note: because we process all of the relocations against the
1563 	 *	text segment before any others - we know whether
1564 	 *	or not a copy relocation will be generated before
1565 	 *	we get here (see reloc_init()->reloc_segments()).
1566 	 */
1567 	if (!(rsp->rel_flags & FLG_REL_LOAD) &&
1568 	    ((shndx == SHN_UNDEF) ||
1569 	    ((sdp->sd_ref == REF_DYN_NEED) &&
1570 	    ((sdp->sd_flags & FLG_SY_MVTOCOMM) == 0)))) {
1571 		/*
1572 		 * If the relocation is against a SHT_SUNW_ANNOTATE
1573 		 * section - then silently ignore that the relocation
1574 		 * can not be resolved.
1575 		 */
1576 		if (rsp->rel_osdesc &&
1577 		    (rsp->rel_osdesc->os_shdr->sh_type == SHT_SUNW_ANNOTATE))
1578 			return (0);
1579 		(void) eprintf(ofl->ofl_lml, ERR_WARNING,
1580 		    MSG_INTL(MSG_REL_EXTERNSYM),
1581 		    conv_reloc_SPARC_type(rsp->rel_rtype, 0),
1582 		    rsp->rel_isdesc->is_file->ifl_name,
1583 		    demangle(rsp->rel_sname), rsp->rel_osdesc->os_name);
1584 		return (1);
1585 	}
1586 
1587 	/*
1588 	 * Perform relocation.
1589 	 */
1590 	return (ld_add_actrel(NULL, rsp, ofl));
1591 }
1592 
1593 uintptr_t
1594 ld_reloc_GOTOP(Boolean local, Rel_desc * rsp, Ofl_desc * ofl)
1595 {
1596 	Word	rtype = rsp->rel_rtype;
1597 
1598 	if (!local) {
1599 		/*
1600 		 * When binding to a external symbol, no fixups are required
1601 		 * and the GOTDATA_OP relocation can be ignored.
1602 		 */
1603 		if (rtype == R_SPARC_GOTDATA_OP)
1604 			return (1);
1605 		return (ld_reloc_GOT_relative(local, rsp, ofl));
1606 	}
1607 
1608 	/*
1609 	 * When binding to a local symbol the relocations can be transitioned:
1610 	 *
1611 	 *	R_*_GOTDATA_OP_HIX22 -> R_*_GOTDATA_HIX22
1612 	 *	R_*_GOTDATA_OP_LOX10 -> R_*_GOTDATA_LOX10
1613 	 *	R_*_GOTDATA_OP ->	instruction fixup
1614 	 */
1615 	return (ld_add_actrel(FLG_REL_GOTFIX, rsp, ofl));
1616 }
1617 
1618 uintptr_t
1619 ld_reloc_TLS(Boolean local, Rel_desc * rsp, Ofl_desc * ofl)
1620 {
1621 	Word		rtype = rsp->rel_rtype;
1622 	Sym_desc	*sdp = rsp->rel_sym;
1623 	Word		flags = ofl->ofl_flags;
1624 	Gotndx		*gnp;
1625 
1626 	/*
1627 	 * If we're building an executable - use either the IE or LE access
1628 	 * model.  If we're building a shared object process any IE model.
1629 	 */
1630 	if ((flags & FLG_OF_EXEC) || (IS_TLS_IE(rtype))) {
1631 		/*
1632 		 * Set the DF_STATIC_TLS flag.
1633 		 */
1634 		ofl->ofl_dtflags |= DF_STATIC_TLS;
1635 
1636 		if (!local || ((flags & FLG_OF_EXEC) == 0)) {
1637 			/*
1638 			 * When processing static TLS - these relocations
1639 			 * can be ignored.
1640 			 */
1641 			if ((rtype == R_SPARC_TLS_IE_LD) ||
1642 			    (rtype == R_SPARC_TLS_IE_LDX) ||
1643 			    (rtype == R_SPARC_TLS_IE_ADD))
1644 				return (1);
1645 
1646 			/*
1647 			 * Assign a GOT entry for IE static TLS references.
1648 			 */
1649 			if (((rtype == R_SPARC_TLS_GD_HI22) ||
1650 			    (rtype == R_SPARC_TLS_GD_LO10) ||
1651 			    (rtype == R_SPARC_TLS_IE_HI22) ||
1652 			    (rtype == R_SPARC_TLS_IE_LO10)) &&
1653 			    ((gnp = ld_find_gotndx(&(sdp->sd_GOTndxs),
1654 			    GOT_REF_TLSIE, ofl, rsp)) == 0)) {
1655 
1656 				if (ld_assign_got_TLS(local, rsp, ofl, sdp,
1657 				    gnp, GOT_REF_TLSIE, FLG_REL_STLS,
1658 				    rtype, M_R_TPOFF, 0) == S_ERROR)
1659 					return (S_ERROR);
1660 			}
1661 
1662 			/*
1663 			 * IE access model.
1664 			 */
1665 			if (IS_TLS_IE(rtype))
1666 				return (ld_add_actrel(FLG_REL_STLS, rsp, ofl));
1667 
1668 			/*
1669 			 * Fixups are required for other executable models.
1670 			 */
1671 			return (ld_add_actrel((FLG_REL_TLSFIX | FLG_REL_STLS),
1672 			    rsp, ofl));
1673 		}
1674 
1675 		/*
1676 		 * LE access model.
1677 		 */
1678 		if (IS_TLS_LE(rtype))
1679 			return (ld_add_actrel(FLG_REL_STLS, rsp, ofl));
1680 
1681 		/*
1682 		 * When processing static TLS - these relocations can be
1683 		 * ignored.
1684 		 */
1685 		if (rtype == R_SPARC_TLS_IE_ADD)
1686 			return (1);
1687 
1688 		return (ld_add_actrel((FLG_REL_TLSFIX | FLG_REL_STLS),
1689 		    rsp, ofl));
1690 	}
1691 
1692 	/*
1693 	 * Building a shared object.
1694 	 *
1695 	 * For dynamic TLS references, ADD relocations are ignored.
1696 	 */
1697 	if ((rtype == R_SPARC_TLS_GD_ADD) || (rtype == R_SPARC_TLS_LDM_ADD) ||
1698 	    (rtype == R_SPARC_TLS_LDO_ADD))
1699 		return (1);
1700 
1701 	/*
1702 	 * Assign a GOT entry for a dynamic TLS reference.
1703 	 */
1704 	if (((rtype == R_SPARC_TLS_LDM_HI22) ||
1705 	    (rtype == R_SPARC_TLS_LDM_LO10)) &&
1706 	    ((gnp = ld_find_gotndx(&(sdp->sd_GOTndxs), GOT_REF_TLSLD,
1707 	    ofl, rsp)) == 0)) {
1708 
1709 		if (ld_assign_got_TLS(local, rsp, ofl, sdp, gnp, GOT_REF_TLSLD,
1710 		    FLG_REL_MTLS, rtype, M_R_DTPMOD, 0) == S_ERROR)
1711 			return (S_ERROR);
1712 
1713 	} else if (((rtype == R_SPARC_TLS_GD_HI22) ||
1714 	    (rtype == R_SPARC_TLS_GD_LO10)) &&
1715 	    ((gnp = ld_find_gotndx(&(sdp->sd_GOTndxs), GOT_REF_TLSGD,
1716 	    ofl, rsp)) == 0)) {
1717 
1718 		if (ld_assign_got_TLS(local, rsp, ofl, sdp, gnp, GOT_REF_TLSGD,
1719 		    FLG_REL_DTLS, rtype, M_R_DTPMOD, M_R_DTPOFF) == S_ERROR)
1720 			return (S_ERROR);
1721 	}
1722 
1723 	/*
1724 	 * For GD/LD TLS reference - TLS_{GD,LD}_CALL, this will eventually
1725 	 * cause a call to __tls_get_addr().  Convert this relocation to that
1726 	 * symbol now, and prepare for the PLT magic.
1727 	 */
1728 	if ((rtype == R_SPARC_TLS_GD_CALL) || (rtype == R_SPARC_TLS_LDM_CALL)) {
1729 		Sym_desc *	tlsgetsym;
1730 
1731 		if ((tlsgetsym = ld_sym_add_u(MSG_ORIG(MSG_SYM_TLSGETADDR_U),
1732 		    ofl)) == (Sym_desc *)S_ERROR)
1733 			return (S_ERROR);
1734 
1735 		rsp->rel_sym = tlsgetsym;
1736 		rsp->rel_sname = tlsgetsym->sd_name;
1737 		rsp->rel_rtype = R_SPARC_WPLT30;
1738 
1739 		if (ld_reloc_plt(rsp, ofl) == S_ERROR)
1740 			return (S_ERROR);
1741 
1742 		rsp->rel_sym = sdp;
1743 		rsp->rel_sname = sdp->sd_name;
1744 		rsp->rel_rtype = rtype;
1745 		return (1);
1746 	}
1747 
1748 	if (IS_TLS_LD(rtype))
1749 		return (ld_add_actrel(FLG_REL_MTLS, rsp, ofl));
1750 
1751 	return (ld_add_actrel(FLG_REL_DTLS, rsp, ofl));
1752 }
1753 
1754 /*
1755  * ld_allocate_got: if a GOT is to be made, after the section is built this
1756  * function is called to allocate all the GOT slots.  The allocation is
1757  * deferred until after all GOTs have been counted and sorted according
1758  * to their size, for only then will we know how to allocate them on
1759  * a processor like SPARC which has different models for addressing the
1760  * GOT.  SPARC has two: small and large, small uses a signed 13-bit offset
1761  * into the GOT, whereas large uses an unsigned 32-bit offset.
1762  */
1763 static	Sword small_index;	/* starting index for small GOT entries */
1764 static	Sword large_index;	/* starting index for large GOT entries */
1765 
1766 uintptr_t
1767 ld_assign_got(Ofl_desc *ofl, Sym_desc * sdp)
1768 {
1769 	Listnode *	lnp;
1770 	Gotndx *	gnp;
1771 
1772 	for (LIST_TRAVERSE(&sdp->sd_GOTndxs, lnp, gnp)) {
1773 		uint_t	gotents;
1774 		Gotref	gref;
1775 		gref = gnp->gn_gotref;
1776 		if ((gref == GOT_REF_TLSGD) || (gref == GOT_REF_TLSLD))
1777 			gotents = 2;
1778 		else
1779 			gotents = 1;
1780 
1781 		switch (gnp->gn_gotndx) {
1782 		case M_GOT_SMALL:
1783 			gnp->gn_gotndx = small_index;
1784 			small_index += gotents;
1785 			if (small_index == 0)
1786 				small_index = M_GOT_XNumber;
1787 			break;
1788 		case M_GOT_LARGE:
1789 			gnp->gn_gotndx = large_index;
1790 			large_index += gotents;
1791 			break;
1792 		default:
1793 			eprintf(ofl->ofl_lml, ERR_FATAL,
1794 			    MSG_INTL(MSG_REL_ASSIGNGOT),
1795 			    EC_XWORD(gnp->gn_gotndx), demangle(sdp->sd_name));
1796 			return (S_ERROR);
1797 		}
1798 	}
1799 	return (1);
1800 }
1801 
1802 /*
1803  * Search the GOT index list for a GOT entry with the proper addend.
1804  */
1805 Gotndx *
1806 ld_find_gotndx(List * lst, Gotref gref, Ofl_desc * ofl, Rel_desc * rdesc)
1807 {
1808 	Listnode *	lnp;
1809 	Gotndx *	gnp;
1810 
1811 	if ((gref == GOT_REF_TLSLD) && ofl->ofl_tlsldgotndx)
1812 		return (ofl->ofl_tlsldgotndx);
1813 
1814 	for (LIST_TRAVERSE(lst, lnp, gnp)) {
1815 		if ((rdesc->rel_raddend == gnp->gn_addend) &&
1816 		    (gref == gnp->gn_gotref))
1817 			return (gnp);
1818 	}
1819 	return ((Gotndx *)0);
1820 }
1821 
1822 Xword
1823 ld_calc_got_offset(Rel_desc * rdesc, Ofl_desc * ofl)
1824 {
1825 	Os_desc		*osp = ofl->ofl_osgot;
1826 	Sym_desc	*sdp = rdesc->rel_sym;
1827 	Xword		gotndx;
1828 	Gotref		gref;
1829 	Gotndx		*gnp;
1830 
1831 	if (rdesc->rel_flags & FLG_REL_DTLS)
1832 		gref = GOT_REF_TLSGD;
1833 	else if (rdesc->rel_flags & FLG_REL_MTLS)
1834 		gref = GOT_REF_TLSLD;
1835 	else if (rdesc->rel_flags & FLG_REL_STLS)
1836 		gref = GOT_REF_TLSIE;
1837 	else
1838 		gref = GOT_REF_GENERIC;
1839 
1840 	gnp = ld_find_gotndx(&(sdp->sd_GOTndxs), gref, ofl, rdesc);
1841 	assert(gnp);
1842 
1843 	gotndx = (Xword)gnp->gn_gotndx;
1844 
1845 	if ((rdesc->rel_flags & FLG_REL_DTLS) &&
1846 	    (rdesc->rel_rtype == M_R_DTPOFF))
1847 		gotndx++;
1848 
1849 	return ((Xword)((osp->os_shdr->sh_addr) + (gotndx * M_GOT_ENTSIZE) +
1850 	    (-neggotoffset * M_GOT_ENTSIZE)));
1851 }
1852 
1853 uintptr_t
1854 ld_assign_got_ndx(List * lst, Gotndx * pgnp, Gotref gref, Ofl_desc * ofl,
1855     Rel_desc * rsp, Sym_desc * sdp)
1856 {
1857 	Xword		raddend;
1858 	Gotndx *	gnp, * _gnp;
1859 	Listnode *	lnp, * plnp;
1860 	uint_t		gotents;
1861 
1862 	raddend = rsp->rel_raddend;
1863 	if (pgnp && (pgnp->gn_addend == raddend) && (pgnp->gn_gotref == gref)) {
1864 		/*
1865 		 * If an entry for this addend already exists, determine if it
1866 		 * should be changed to a SMALL got.
1867 		 */
1868 		if ((pgnp->gn_gotndx != M_GOT_SMALL) &&
1869 		    (rsp->rel_rtype == R_SPARC_GOT13)) {
1870 			smlgotcnt++;
1871 			pgnp->gn_gotndx = M_GOT_SMALL;
1872 			sdp->sd_flags |= FLG_SY_SMGOT;
1873 		}
1874 		return (1);
1875 	}
1876 
1877 	if ((gref == GOT_REF_TLSGD) || (gref == GOT_REF_TLSLD))
1878 		gotents = 2;
1879 	else
1880 		gotents = 1;
1881 
1882 	plnp = 0;
1883 	for (LIST_TRAVERSE(lst, lnp, _gnp)) {
1884 		if (_gnp->gn_addend > raddend)
1885 			break;
1886 		plnp = lnp;
1887 	}
1888 
1889 	/*
1890 	 * Allocate a new entry.
1891 	 */
1892 	if ((gnp = libld_calloc(sizeof (Gotndx), 1)) == 0)
1893 		return (S_ERROR);
1894 	gnp->gn_addend = raddend;
1895 	gnp->gn_gotref = gref;
1896 	ofl->ofl_gotcnt += gotents;
1897 
1898 	if (rsp->rel_rtype == R_SPARC_GOT13) {
1899 		gnp->gn_gotndx = M_GOT_SMALL;
1900 		smlgotcnt++;
1901 		sdp->sd_flags |= FLG_SY_SMGOT;
1902 	} else
1903 		gnp->gn_gotndx = M_GOT_LARGE;
1904 
1905 	if (gref == GOT_REF_TLSLD) {
1906 		ofl->ofl_tlsldgotndx = gnp;
1907 		return (1);
1908 	}
1909 
1910 	if (plnp == 0) {
1911 		/*
1912 		 * Insert at head of list
1913 		 */
1914 		if (list_prependc(lst, (void *)gnp) == 0)
1915 			return (S_ERROR);
1916 	} else if (_gnp->gn_addend > raddend) {
1917 		/*
1918 		 * Insert in middle of lest
1919 		 */
1920 		if (list_insertc(lst, (void *)gnp, plnp) == 0)
1921 			return (S_ERROR);
1922 	} else {
1923 		/*
1924 		 * Append to tail of list
1925 		 */
1926 		if (list_appendc(lst, (void *)gnp) == 0)
1927 			return (S_ERROR);
1928 	}
1929 	return (1);
1930 }
1931 
1932 void
1933 ld_assign_plt_ndx(Sym_desc * sdp, Ofl_desc *ofl)
1934 {
1935 	sdp->sd_aux->sa_PLTndx = 1 + ofl->ofl_pltcnt++;
1936 }
1937 
1938 
1939 uintptr_t
1940 ld_allocate_got(Ofl_desc * ofl)
1941 {
1942 	Sym_desc *	sdp;
1943 	Addr		addr;
1944 
1945 	/*
1946 	 * Sanity check -- is this going to fit at all?
1947 	 */
1948 	if (smlgotcnt >= M_GOT_MAXSMALL) {
1949 		eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_REL_SMALLGOT),
1950 		    EC_WORD(smlgotcnt), M_GOT_MAXSMALL);
1951 		return (S_ERROR);
1952 	}
1953 
1954 	/*
1955 	 * Set starting offset to be either 0, or a negative index into
1956 	 * the GOT based on the number of small symbols we've got.
1957 	 */
1958 	neggotoffset = ((smlgotcnt > (M_GOT_MAXSMALL / 2)) ?
1959 	    -((smlgotcnt - (M_GOT_MAXSMALL / 2))) : 0);
1960 
1961 	/*
1962 	 * Initialize the large and small got offsets (used in assign_got()).
1963 	 */
1964 	small_index = neggotoffset == 0 ? M_GOT_XNumber : neggotoffset;
1965 	large_index = neggotoffset + smlgotcnt;
1966 
1967 	/*
1968 	 * Assign bias to GOT symbols.
1969 	 */
1970 	addr = -neggotoffset * M_GOT_ENTSIZE;
1971 	if (sdp = ld_sym_find(MSG_ORIG(MSG_SYM_GOFTBL), SYM_NOHASH, 0, ofl))
1972 		sdp->sd_sym->st_value = addr;
1973 	if (sdp = ld_sym_find(MSG_ORIG(MSG_SYM_GOFTBL_U), SYM_NOHASH, 0, ofl))
1974 		sdp->sd_sym->st_value = addr;
1975 
1976 	if (ofl->ofl_tlsldgotndx) {
1977 		ofl->ofl_tlsldgotndx->gn_gotndx = large_index;
1978 		large_index += 2;
1979 	}
1980 	return (1);
1981 }
1982 
1983 /*
1984  * Initializes .got[0] with the _DYNAMIC symbol value.
1985  */
1986 uintptr_t
1987 ld_fillin_gotplt(Ofl_desc *ofl)
1988 {
1989 	if (ofl->ofl_osgot) {
1990 		Sym_desc	*sdp;
1991 
1992 		if ((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_DYNAMIC_U),
1993 		    SYM_NOHASH, 0, ofl)) != NULL) {
1994 			uchar_t	*genptr;
1995 
1996 			genptr = ((uchar_t *)ofl->ofl_osgot->os_outdata->d_buf +
1997 			    (-neggotoffset * M_GOT_ENTSIZE) +
1998 			    (M_GOT_XDYNAMIC * M_GOT_ENTSIZE));
1999 			/* LINTED */
2000 			*((Xword *)genptr) = sdp->sd_sym->st_value;
2001 		}
2002 	}
2003 	return (1);
2004 }
2005