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