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