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