xref: /titanic_44/usr/src/cmd/sgs/libld/common/machrel.sparc.c (revision ca35ed50b0189df73b8ea3eb509c9d582312df7c)
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 
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_roffset))) {
1086 					/*
1087 					 * If the symbol is moved,
1088 					 * adjust the value
1089 					 */
1090 					value = _elf_getxoff(
1091 					    sym->sd_isc->is_indata);
1092 					if (sym->sd_isc->is_shdr->sh_flags &
1093 					    SHF_ALLOC)
1094 						value += sym->sd_isc->
1095 						    is_osdesc->os_shdr->sh_addr;
1096 				} else {
1097 					value = _elf_getxoff(
1098 					    sdp->sd_isc->is_indata);
1099 					if (sdp->sd_isc->is_shdr->sh_flags &
1100 					    SHF_ALLOC)
1101 						value += sdp->sd_isc->
1102 						    is_osdesc->os_shdr->sh_addr;
1103 				}
1104 
1105 				if (sdp->sd_isc->is_shdr->sh_flags & SHF_TLS)
1106 					value -= ofl->ofl_tlsphdr->p_vaddr;
1107 
1108 			} else if (IS_SIZE(arsp->rel_rtype)) {
1109 				/*
1110 				 * Size relocations require the symbols size.
1111 				 */
1112 				value = sdp->sd_sym->st_size;
1113 			} else {
1114 				/*
1115 				 * Else the value is the symbols value.
1116 				 */
1117 				value = sdp->sd_sym->st_value;
1118 			}
1119 
1120 			/*
1121 			 * Relocation against the GLOBAL_OFFSET_TABLE.
1122 			 */
1123 			if (arsp->rel_flags & FLG_REL_GOT)
1124 				arsp->rel_osdesc = ofl->ofl_osgot;
1125 
1126 			/*
1127 			 * If loadable and not producing a relocatable object
1128 			 * add the sections virtual address to the reference
1129 			 * address.
1130 			 */
1131 			if ((arsp->rel_flags & FLG_REL_LOAD) &&
1132 			    ((flags & FLG_OF_RELOBJ) == 0))
1133 				refaddr += arsp->rel_isdesc->is_osdesc->
1134 				    os_shdr->sh_addr;
1135 
1136 			/*
1137 			 * If this entry has a PLT assigned to it, it's
1138 			 * value is actually the address of the PLT (and
1139 			 * not the address of the function).
1140 			 */
1141 			if (IS_PLT(arsp->rel_rtype)) {
1142 				if (sdp->sd_aux && sdp->sd_aux->sa_PLTndx)
1143 					value = ld_calc_plt_addr(sdp, ofl);
1144 			}
1145 
1146 			/*
1147 			 * Add relocations addend to value.  Add extra
1148 			 * relocation addend if needed.
1149 			 */
1150 			value += arsp->rel_raddend;
1151 			if (IS_EXTOFFSET(arsp->rel_rtype))
1152 				value += arsp->rel_typedata;
1153 
1154 			/*
1155 			 * Determine whether the value needs further adjustment.
1156 			 * Filter through the attributes of the relocation to
1157 			 * determine what adjustment is required.  Note, many
1158 			 * of the following cases are only applicable when a
1159 			 * .got is present.  As a .got is not generated when a
1160 			 * relocatable object is being built, any adjustments
1161 			 * that require a .got need to be skipped.
1162 			 */
1163 			if ((arsp->rel_flags & FLG_REL_GOT) &&
1164 			    ((flags & FLG_OF_RELOBJ) == 0)) {
1165 				Xword		R1addr;
1166 				uintptr_t	R2addr;
1167 				Sword		gotndx;
1168 				Gotndx		*gnp;
1169 				Gotref		gref;
1170 
1171 				/*
1172 				 * Clear the GOT table entry, on SPARC we clear
1173 				 * the entry and the 'value' if needed is stored
1174 				 * in an output relocations addend.
1175 				 *
1176 				 * Calculate offset into GOT at which to apply
1177 				 * the relocation.
1178 				 */
1179 				if (arsp->rel_flags & FLG_REL_DTLS)
1180 					gref = GOT_REF_TLSGD;
1181 				else if (arsp->rel_flags & FLG_REL_MTLS)
1182 					gref = GOT_REF_TLSLD;
1183 				else if (arsp->rel_flags & FLG_REL_STLS)
1184 					gref = GOT_REF_TLSIE;
1185 				else
1186 					gref = GOT_REF_GENERIC;
1187 
1188 				gnp = ld_find_gotndx(&(sdp->sd_GOTndxs), gref,
1189 				    ofl, arsp);
1190 				assert(gnp);
1191 
1192 				if (arsp->rel_rtype == M_R_DTPOFF)
1193 					gotndx = gnp->gn_gotndx + 1;
1194 				else
1195 					gotndx = gnp->gn_gotndx;
1196 
1197 				/* LINTED */
1198 				R1addr = (Xword)((-neggotoffset *
1199 				    M_GOT_ENTSIZE) + (gotndx * M_GOT_ENTSIZE));
1200 
1201 				/*
1202 				 * Add the GOTs data's offset.
1203 				 */
1204 				R2addr = R1addr + (uintptr_t)
1205 				    arsp->rel_osdesc->os_outdata->d_buf;
1206 
1207 				DBG_CALL(Dbg_reloc_doact(ofl->ofl_lml,
1208 				    ELF_DBG_LD, M_MACH, SHT_RELA,
1209 				    arsp->rel_rtype, R1addr, value,
1210 				    arsp->rel_sname, arsp->rel_osdesc));
1211 
1212 				/*
1213 				 * And do it.
1214 				 */
1215 				if (ofl->ofl_flags1 & FLG_OF1_ENCDIFF)
1216 					*(Xword *)R2addr =
1217 					    ld_bswap_Xword(value);
1218 				else
1219 					*(Xword *)R2addr = value;
1220 				continue;
1221 
1222 			} else if (IS_GOT_BASED(arsp->rel_rtype) &&
1223 			    ((flags & FLG_OF_RELOBJ) == 0)) {
1224 				value -= (ofl->ofl_osgot->os_shdr->sh_addr +
1225 				    (-neggotoffset * M_GOT_ENTSIZE));
1226 
1227 			} else if (IS_PC_RELATIVE(arsp->rel_rtype)) {
1228 				value -= refaddr;
1229 
1230 			} else if (IS_TLS_INS(arsp->rel_rtype) &&
1231 			    IS_GOT_RELATIVE(arsp->rel_rtype) &&
1232 			    ((flags & FLG_OF_RELOBJ) == 0)) {
1233 				Gotndx	*gnp;
1234 				Gotref	gref;
1235 
1236 				if (arsp->rel_flags & FLG_REL_STLS)
1237 					gref = GOT_REF_TLSIE;
1238 				else if (arsp->rel_flags & FLG_REL_DTLS)
1239 					gref = GOT_REF_TLSGD;
1240 				else if (arsp->rel_flags & FLG_REL_MTLS)
1241 					gref = GOT_REF_TLSLD;
1242 
1243 				gnp = ld_find_gotndx(&(sdp->sd_GOTndxs), gref,
1244 				    ofl, arsp);
1245 				assert(gnp);
1246 
1247 				value = gnp->gn_gotndx * M_GOT_ENTSIZE;
1248 
1249 			} else if (IS_GOT_RELATIVE(arsp->rel_rtype) &&
1250 			    ((flags & FLG_OF_RELOBJ) == 0)) {
1251 				Gotndx	*gnp;
1252 
1253 				gnp = ld_find_gotndx(&(sdp->sd_GOTndxs),
1254 				    GOT_REF_GENERIC, ofl, arsp);
1255 				assert(gnp);
1256 
1257 				value = gnp->gn_gotndx * M_GOT_ENTSIZE;
1258 
1259 			} else if ((arsp->rel_flags & FLG_REL_STLS) &&
1260 			    ((flags & FLG_OF_RELOBJ) == 0)) {
1261 				Xword	tlsstatsize;
1262 
1263 				/*
1264 				 * This is the LE TLS
1265 				 * reference model.  Static offset
1266 				 * is hard-coded, and negated so that
1267 				 * it can be added to the thread pointer (%g7)
1268 				 */
1269 				tlsstatsize = S_ROUND(ofl->
1270 				    ofl_tlsphdr->p_memsz, M_TLSSTATALIGN);
1271 				value = -(tlsstatsize - value);
1272 			}
1273 
1274 			if (arsp->rel_isdesc->is_file)
1275 				ifl_name = arsp->rel_isdesc->is_file->ifl_name;
1276 			else
1277 				ifl_name = MSG_INTL(MSG_STR_NULL);
1278 
1279 			/*
1280 			 * Make sure we have data to relocate.  Compiler and
1281 			 * assembler developers have been known to generate
1282 			 * relocations against invalid sections (normally .bss),
1283 			 * so for their benefit give them sufficient information
1284 			 * to help analyze the problem.  End users should never
1285 			 * see this.
1286 			 */
1287 			if (arsp->rel_isdesc->is_indata->d_buf == 0) {
1288 				Conv_inv_buf_t	inv_buf;
1289 
1290 				eprintf(ofl->ofl_lml, ERR_FATAL,
1291 				    MSG_INTL(MSG_REL_EMPTYSEC),
1292 				    conv_reloc_SPARC_type(arsp->rel_rtype,
1293 				    0, &inv_buf), ifl_name,
1294 				    demangle(arsp->rel_sname),
1295 				    arsp->rel_isdesc->is_name);
1296 				return (S_ERROR);
1297 			}
1298 
1299 			/*
1300 			 * Get the address of the data item we need to modify.
1301 			 */
1302 			addr = (uchar_t *)((uintptr_t)arsp->rel_roffset +
1303 			    (uintptr_t)_elf_getxoff(arsp->rel_isdesc->
1304 			    is_indata));
1305 
1306 			/*LINTED*/
1307 			DBG_CALL(Dbg_reloc_doact(ofl->ofl_lml, ELF_DBG_LD,
1308 			    M_MACH, SHT_RELA, arsp->rel_rtype, EC_NATPTR(addr),
1309 			    value, arsp->rel_sname, arsp->rel_osdesc));
1310 			addr += (uintptr_t)arsp->rel_osdesc->os_outdata->d_buf;
1311 
1312 			if ((((uintptr_t)addr - (uintptr_t)ofl->ofl_nehdr) >
1313 			    ofl->ofl_size) || (arsp->rel_roffset >
1314 			    arsp->rel_osdesc->os_shdr->sh_size)) {
1315 				Conv_inv_buf_t	inv_buf;
1316 				int		class;
1317 
1318 				if (((uintptr_t)addr -
1319 				    (uintptr_t)ofl->ofl_nehdr) > ofl->ofl_size)
1320 					class = ERR_FATAL;
1321 				else
1322 					class = ERR_WARNING;
1323 
1324 				eprintf(ofl->ofl_lml, class,
1325 				    MSG_INTL(MSG_REL_INVALOFFSET),
1326 				    conv_reloc_SPARC_type(arsp->rel_rtype,
1327 				    0, &inv_buf), ifl_name,
1328 				    arsp->rel_isdesc->is_name,
1329 				    demangle(arsp->rel_sname),
1330 				    EC_ADDR((uintptr_t)addr -
1331 				    (uintptr_t)ofl->ofl_nehdr));
1332 
1333 				if (class == ERR_FATAL) {
1334 					return_code = S_ERROR;
1335 					continue;
1336 				}
1337 			}
1338 
1339 			/*
1340 			 * If '-z noreloc' is specified - skip the do_reloc
1341 			 * stage.
1342 			 */
1343 			if (OFL_DO_RELOC(ofl)) {
1344 				if (do_reloc_ld((uchar_t)arsp->rel_rtype, addr,
1345 				    &value, arsp->rel_sname, ifl_name,
1346 				    OFL_SWAP_RELOC_DATA(ofl, arsp),
1347 				    ofl->ofl_lml) == 0)
1348 					return_code = S_ERROR;
1349 			}
1350 		}
1351 	}
1352 	return (return_code);
1353 }
1354 
1355 static uintptr_t
1356 ld_add_outrel(Word flags, Rel_desc *rsp, Ofl_desc *ofl)
1357 {
1358 	Rel_desc	*orsp;
1359 	Rel_cache	*rcp;
1360 	Sym_desc	*sdp = rsp->rel_sym;
1361 	Conv_inv_buf_t	inv_buf;
1362 
1363 	/*
1364 	 * Static executables *do not* want any relocations against them.
1365 	 * Since our engine still creates relocations against a WEAK UNDEFINED
1366 	 * symbol in a static executable, it's best to disable them here
1367 	 * instead of through out the relocation code.
1368 	 */
1369 	if ((ofl->ofl_flags & (FLG_OF_STATIC | FLG_OF_EXEC)) ==
1370 	    (FLG_OF_STATIC | FLG_OF_EXEC))
1371 		return (1);
1372 
1373 	/*
1374 	 * Certain relocations do not make sense in a 64bit shared object,
1375 	 * if building a shared object do a sanity check on the output
1376 	 * relocations being created.
1377 	 */
1378 	if (ofl->ofl_flags & FLG_OF_SHAROBJ) {
1379 		Word	rtype = rsp->rel_rtype;
1380 		/*
1381 		 * Because the R_SPARC_HIPLT22 & R_SPARC_LOPLT10 relocations
1382 		 * are not relative they make no sense to create in a shared
1383 		 * object - so emit the proper error message if that occurs.
1384 		 */
1385 		if ((rtype == R_SPARC_HIPLT22) || (rtype == R_SPARC_LOPLT10)) {
1386 			eprintf(ofl->ofl_lml, ERR_FATAL,
1387 			    MSG_INTL(MSG_REL_UNRELREL),
1388 			    conv_reloc_SPARC_type(rsp->rel_rtype, 0, &inv_buf),
1389 			    rsp->rel_isdesc->is_file->ifl_name,
1390 			    demangle(rsp->rel_sname));
1391 			return (S_ERROR);
1392 		}
1393 #if	defined(_ELF64)
1394 		/*
1395 		 * Each of the following relocations requires that the
1396 		 * object being built be loaded in either the upper 32 or
1397 		 * 44 bit range of memory.  Since shared libraries traditionally
1398 		 * are loaded in the lower range of memory - this isn't going
1399 		 * to work.
1400 		 */
1401 		if ((rtype == R_SPARC_H44) || (rtype == R_SPARC_M44) ||
1402 		    (rtype == R_SPARC_L44)) {
1403 			eprintf(ofl->ofl_lml, ERR_FATAL,
1404 			    MSG_INTL(MSG_REL_SHOBJABS44),
1405 			    conv_reloc_SPARC_type(rsp->rel_rtype, 0, &inv_buf),
1406 			    rsp->rel_isdesc->is_file->ifl_name,
1407 			    demangle(rsp->rel_sname));
1408 			return (S_ERROR);
1409 		}
1410 #endif
1411 	}
1412 
1413 	/*
1414 	 * If no relocation cache structures are available allocate
1415 	 * a new one and link it into the cache list.
1416 	 */
1417 	if ((ofl->ofl_outrels.tail == 0) ||
1418 	    ((rcp = (Rel_cache *)ofl->ofl_outrels.tail->data) == 0) ||
1419 	    ((orsp = rcp->rc_free) == rcp->rc_end)) {
1420 		static size_t	nextsize = 0;
1421 		size_t		size;
1422 
1423 		/*
1424 		 * Output relocation numbers can vary considerably between
1425 		 * building executables or shared objects (pic vs. non-pic),
1426 		 * etc.  But, they typically aren't very large, so for these
1427 		 * objects use a standard bucket size.  For building relocatable
1428 		 * objects, typically there will be an output relocation for
1429 		 * every input relocation.
1430 		 */
1431 		if (nextsize == 0) {
1432 			if (ofl->ofl_flags & FLG_OF_RELOBJ) {
1433 				if ((size = ofl->ofl_relocincnt) == 0)
1434 					size = REL_LOIDESCNO;
1435 				if (size > REL_HOIDESCNO)
1436 					nextsize = REL_HOIDESCNO;
1437 				else
1438 					nextsize = REL_LOIDESCNO;
1439 			} else
1440 				nextsize = size = REL_HOIDESCNO;
1441 		} else
1442 			size = nextsize;
1443 
1444 		size = size * sizeof (Rel_desc);
1445 
1446 		if (((rcp = libld_malloc(sizeof (Rel_cache) + size)) == 0) ||
1447 		    (list_appendc(&ofl->ofl_outrels, rcp) == 0))
1448 			return (S_ERROR);
1449 
1450 		/* LINTED */
1451 		rcp->rc_free = orsp = (Rel_desc *)(rcp + 1);
1452 		/* LINTED */
1453 		rcp->rc_end = (Rel_desc *)((char *)rcp->rc_free + size);
1454 	}
1455 
1456 
1457 	/*
1458 	 * If we are adding a output relocation against a section
1459 	 * symbol (non-RELATIVE) then mark that section.  These sections
1460 	 * will be added to the .dynsym symbol table.
1461 	 */
1462 	if (sdp && (rsp->rel_rtype != M_R_RELATIVE) &&
1463 	    ((flags & FLG_REL_SCNNDX) ||
1464 	    (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION))) {
1465 
1466 		/*
1467 		 * If this is a COMMON symbol - no output section
1468 		 * exists yet - (it's created as part of sym_validate()).
1469 		 * So - we mark here that when it's created it should
1470 		 * be tagged with the FLG_OS_OUTREL flag.
1471 		 */
1472 		if ((sdp->sd_flags & FLG_SY_SPECSEC) &&
1473 		    (sdp->sd_sym->st_shndx == SHN_COMMON)) {
1474 			if (ELF_ST_TYPE(sdp->sd_sym->st_info) != STT_TLS)
1475 				ofl->ofl_flags1 |= FLG_OF1_BSSOREL;
1476 			else
1477 				ofl->ofl_flags1 |= FLG_OF1_TLSOREL;
1478 		} else {
1479 			Os_desc	*osp = sdp->sd_isc->is_osdesc;
1480 
1481 			if (osp && ((osp->os_flags & FLG_OS_OUTREL) == 0)) {
1482 				ofl->ofl_dynshdrcnt++;
1483 				osp->os_flags |= FLG_OS_OUTREL;
1484 			}
1485 		}
1486 	}
1487 
1488 	*orsp = *rsp;
1489 	orsp->rel_flags |= flags;
1490 
1491 	rcp->rc_free++;
1492 	ofl->ofl_outrelscnt++;
1493 
1494 	if (flags & FLG_REL_GOT)
1495 		ofl->ofl_relocgotsz += (Xword)sizeof (Rela);
1496 	else if (flags & FLG_REL_PLT)
1497 		ofl->ofl_relocpltsz += (Xword)sizeof (Rela);
1498 	else if (flags & FLG_REL_BSS)
1499 		ofl->ofl_relocbsssz += (Xword)sizeof (Rela);
1500 	else if (flags & FLG_REL_NOINFO)
1501 		ofl->ofl_relocrelsz += (Xword)sizeof (Rela);
1502 	else
1503 		orsp->rel_osdesc->os_szoutrels += (Xword)sizeof (Rela);
1504 
1505 	if (orsp->rel_rtype == M_R_RELATIVE)
1506 		ofl->ofl_relocrelcnt++;
1507 
1508 #if	defined(_ELF64)
1509 	/*
1510 	 * When building a 64-bit object any R_SPARC_WDISP30 relocation is given
1511 	 * a plt padding entry, unless we're building a relocatable object
1512 	 * (ld -r) or -b is in effect.
1513 	 */
1514 	if ((orsp->rel_rtype == R_SPARC_WDISP30) &&
1515 	    ((ofl->ofl_flags & (FLG_OF_BFLAG | FLG_OF_RELOBJ)) == 0) &&
1516 	    ((orsp->rel_sym->sd_flags & FLG_SY_PLTPAD) == 0)) {
1517 		ofl->ofl_pltpad++;
1518 		orsp->rel_sym->sd_flags |= FLG_SY_PLTPAD;
1519 	}
1520 #endif
1521 	/*
1522 	 * We don't perform sorting on PLT relocations because
1523 	 * they have already been assigned a PLT index and if we
1524 	 * were to sort them we would have to re-assign the plt indexes.
1525 	 */
1526 	if (!(flags & FLG_REL_PLT))
1527 		ofl->ofl_reloccnt++;
1528 
1529 	/*
1530 	 * Insure a GLOBAL_OFFSET_TABLE is generated if required.
1531 	 */
1532 	if (IS_GOT_REQUIRED(orsp->rel_rtype))
1533 		ofl->ofl_flags |= FLG_OF_BLDGOT;
1534 
1535 	/*
1536 	 * Identify and possibly warn of a displacement relocation.
1537 	 */
1538 	if (orsp->rel_flags & FLG_REL_DISP) {
1539 		ofl->ofl_dtflags_1 |= DF_1_DISPRELPND;
1540 
1541 		if (ofl->ofl_flags & FLG_OF_VERBOSE)
1542 			ld_disp_errmsg(MSG_INTL(MSG_REL_DISPREL4), orsp, ofl);
1543 	}
1544 	DBG_CALL(Dbg_reloc_ors_entry(ofl->ofl_lml, ELF_DBG_LD, SHT_RELA,
1545 	    M_MACH, orsp));
1546 	return (1);
1547 }
1548 
1549 /*
1550  * Process relocation against a register symbol.  Note, of -z muldefs is in
1551  * effect there may have been multiple register definitions, which would have
1552  * been processed as non-fatal, with the first definition winning.  But, we
1553  * will also process multiple relocations for these multiple definitions.  In
1554  * this case we must only preserve the relocation for the definition that was
1555  * kept.  The sad part is that register relocations don't typically specify
1556  * the register symbol with which they are associated, so we might have to
1557  * search the input files global symbols to determine if this relocation is
1558  * appropriate.
1559  */
1560 static uintptr_t
1561 ld_reloc_register(Rel_desc * rsp, Is_desc * isp, Ofl_desc * ofl)
1562 {
1563 	if (ofl->ofl_flags & FLG_OF_MULDEFS) {
1564 		Ifl_desc *	ifl = isp->is_file;
1565 		Sym_desc *	sdp = rsp->rel_sym;
1566 
1567 		if (sdp == 0) {
1568 			Xword		offset = rsp->rel_roffset;
1569 			Word		ndx;
1570 
1571 			for (ndx = ifl->ifl_locscnt;
1572 			    ndx < ifl->ifl_symscnt; ndx++) {
1573 				if (((sdp = ifl->ifl_oldndx[ndx]) != 0) &&
1574 				    (sdp->sd_flags & FLG_SY_REGSYM) &&
1575 				    (sdp->sd_sym->st_value == offset))
1576 					break;
1577 			}
1578 		}
1579 		if (sdp && (sdp->sd_file != ifl))
1580 			return (1);
1581 	}
1582 	return (ld_add_outrel((rsp->rel_flags | FLG_REL_REG), rsp, ofl));
1583 }
1584 
1585 /*
1586  * process relocation for a LOCAL symbol
1587  */
1588 static uintptr_t
1589 ld_reloc_local(Rel_desc * rsp, Ofl_desc * ofl)
1590 {
1591 	ofl_flag_t	flags = ofl->ofl_flags;
1592 	Sym_desc	*sdp = rsp->rel_sym;
1593 	Word		shndx = sdp->sd_sym->st_shndx;
1594 
1595 	/*
1596 	 * if ((shared object) and (not pc relative relocation) and
1597 	 *    (not against ABS symbol))
1598 	 * then
1599 	 *	if (rtype != R_SPARC_32)
1600 	 *	then
1601 	 *		build relocation against section
1602 	 *	else
1603 	 *		build R_SPARC_RELATIVE
1604 	 *	fi
1605 	 * fi
1606 	 */
1607 	if ((flags & FLG_OF_SHAROBJ) && (rsp->rel_flags & FLG_REL_LOAD) &&
1608 	    !(IS_PC_RELATIVE(rsp->rel_rtype)) && !(IS_SIZE(rsp->rel_rtype)) &&
1609 	    !(IS_GOT_BASED(rsp->rel_rtype)) &&
1610 	    !(rsp->rel_isdesc != NULL &&
1611 	    (rsp->rel_isdesc->is_shdr->sh_type == SHT_SUNW_dof)) &&
1612 	    (((sdp->sd_flags & FLG_SY_SPECSEC) == 0) ||
1613 	    (shndx != SHN_ABS) || (sdp->sd_aux && sdp->sd_aux->sa_symspec))) {
1614 		Word	ortype = rsp->rel_rtype;
1615 
1616 		if ((rsp->rel_rtype != R_SPARC_32) &&
1617 		    (rsp->rel_rtype != R_SPARC_PLT32) &&
1618 		    (rsp->rel_rtype != R_SPARC_64))
1619 			return (ld_add_outrel((FLG_REL_SCNNDX | FLG_REL_ADVAL),
1620 			    rsp, ofl));
1621 
1622 		rsp->rel_rtype = R_SPARC_RELATIVE;
1623 		if (ld_add_outrel(FLG_REL_ADVAL, rsp, ofl) == S_ERROR)
1624 			return (S_ERROR);
1625 		rsp->rel_rtype = ortype;
1626 		return (1);
1627 	}
1628 
1629 	/*
1630 	 * If the relocation is against a 'non-allocatable' section
1631 	 * and we can not resolve it now - then give a warning
1632 	 * message.
1633 	 *
1634 	 * We can not resolve the symbol if either:
1635 	 *	a) it's undefined
1636 	 *	b) it's defined in a shared library and a
1637 	 *	   COPY relocation hasn't moved it to the executable
1638 	 *
1639 	 * Note: because we process all of the relocations against the
1640 	 *	text segment before any others - we know whether
1641 	 *	or not a copy relocation will be generated before
1642 	 *	we get here (see reloc_init()->reloc_segments()).
1643 	 */
1644 	if (!(rsp->rel_flags & FLG_REL_LOAD) &&
1645 	    ((shndx == SHN_UNDEF) ||
1646 	    ((sdp->sd_ref == REF_DYN_NEED) &&
1647 	    ((sdp->sd_flags & FLG_SY_MVTOCOMM) == 0)))) {
1648 		Conv_inv_buf_t	inv_buf;
1649 
1650 		/*
1651 		 * If the relocation is against a SHT_SUNW_ANNOTATE
1652 		 * section - then silently ignore that the relocation
1653 		 * can not be resolved.
1654 		 */
1655 		if (rsp->rel_osdesc &&
1656 		    (rsp->rel_osdesc->os_shdr->sh_type == SHT_SUNW_ANNOTATE))
1657 			return (0);
1658 		(void) eprintf(ofl->ofl_lml, ERR_WARNING,
1659 		    MSG_INTL(MSG_REL_EXTERNSYM),
1660 		    conv_reloc_SPARC_type(rsp->rel_rtype, 0, &inv_buf),
1661 		    rsp->rel_isdesc->is_file->ifl_name,
1662 		    demangle(rsp->rel_sname), rsp->rel_osdesc->os_name);
1663 		return (1);
1664 	}
1665 
1666 	/*
1667 	 * Perform relocation.
1668 	 */
1669 	return (ld_add_actrel(NULL, rsp, ofl));
1670 }
1671 
1672 /*
1673  * Establish a relocation transition.  Note, at this point of input relocation
1674  * processing, we have no idea of the relocation value that will be used in
1675  * the eventual relocation calculation.  This value is only known after the
1676  * initial image has been constructed.  Therefore, there is a small chance
1677  * that a value can exceed the capabilities of the transitioned relocation.
1678  * One example might be the offset from the GOT to a symbol.
1679  *
1680  * The only instance of this failure discovered so far has been via the use of
1681  * ABS symbols to represent an external memory location.  This situation is
1682  * rare, since ABS symbols aren't typically generated by the compilers.
1683  * Therefore, our solution is to excluded ABS symbols from the transition
1684  * relocation possibilities.  As an additional safeguard, if an inappropriate
1685  * value is passed to the final relocation engine, a verification ("V")
1686  * relocation should trigger a fatal error condition.
1687  */
1688 static uintptr_t
1689 ld_reloc_GOTOP(Boolean local, Rel_desc *rsp, Ofl_desc *ofl)
1690 {
1691 	Word	rtype = rsp->rel_rtype;
1692 
1693 	if (!local || (rsp->rel_sym->sd_sym->st_shndx == SHN_ABS)) {
1694 		/*
1695 		 * When binding to a external symbol, no fixups are required
1696 		 * and the GOTDATA_OP relocation can be ignored.
1697 		 */
1698 		if (rtype == R_SPARC_GOTDATA_OP)
1699 			return (1);
1700 		return (ld_reloc_GOT_relative(local, rsp, ofl));
1701 	}
1702 
1703 	/*
1704 	 * When binding to a local symbol the relocations can be transitioned:
1705 	 *
1706 	 *	R_*_GOTDATA_OP_HIX22 -> R_*_GOTDATA_HIX22
1707 	 *	R_*_GOTDATA_OP_LOX10 -> R_*_GOTDATA_LOX10
1708 	 *	R_*_GOTDATA_OP ->	instruction fixup
1709 	 */
1710 	return (ld_add_actrel(FLG_REL_GOTFIX, rsp, ofl));
1711 }
1712 
1713 static uintptr_t
1714 ld_reloc_TLS(Boolean local, Rel_desc *rsp, Ofl_desc *ofl)
1715 {
1716 	Word		rtype = rsp->rel_rtype;
1717 	Sym_desc	*sdp = rsp->rel_sym;
1718 	ofl_flag_t	flags = ofl->ofl_flags;
1719 	Gotndx		*gnp;
1720 
1721 	/*
1722 	 * If we're building an executable - use either the IE or LE access
1723 	 * model.  If we're building a shared object process any IE model.
1724 	 */
1725 	if ((flags & FLG_OF_EXEC) || (IS_TLS_IE(rtype))) {
1726 		/*
1727 		 * Set the DF_STATIC_TLS flag.
1728 		 */
1729 		ofl->ofl_dtflags |= DF_STATIC_TLS;
1730 
1731 		if (!local || ((flags & FLG_OF_EXEC) == 0)) {
1732 			/*
1733 			 * When processing static TLS - these relocations
1734 			 * can be ignored.
1735 			 */
1736 			if ((rtype == R_SPARC_TLS_IE_LD) ||
1737 			    (rtype == R_SPARC_TLS_IE_LDX) ||
1738 			    (rtype == R_SPARC_TLS_IE_ADD))
1739 				return (1);
1740 
1741 			/*
1742 			 * Assign a GOT entry for IE static TLS references.
1743 			 */
1744 			if (((rtype == R_SPARC_TLS_GD_HI22) ||
1745 			    (rtype == R_SPARC_TLS_GD_LO10) ||
1746 			    (rtype == R_SPARC_TLS_IE_HI22) ||
1747 			    (rtype == R_SPARC_TLS_IE_LO10)) &&
1748 			    ((gnp = ld_find_gotndx(&(sdp->sd_GOTndxs),
1749 			    GOT_REF_TLSIE, ofl, rsp)) == 0)) {
1750 
1751 				if (ld_assign_got_TLS(local, rsp, ofl, sdp,
1752 				    gnp, GOT_REF_TLSIE, FLG_REL_STLS,
1753 				    rtype, M_R_TPOFF, 0) == S_ERROR)
1754 					return (S_ERROR);
1755 			}
1756 
1757 			/*
1758 			 * IE access model.
1759 			 */
1760 			if (IS_TLS_IE(rtype))
1761 				return (ld_add_actrel(FLG_REL_STLS, rsp, ofl));
1762 
1763 			/*
1764 			 * Fixups are required for other executable models.
1765 			 */
1766 			return (ld_add_actrel((FLG_REL_TLSFIX | FLG_REL_STLS),
1767 			    rsp, ofl));
1768 		}
1769 
1770 		/*
1771 		 * LE access model.
1772 		 */
1773 		if (IS_TLS_LE(rtype))
1774 			return (ld_add_actrel(FLG_REL_STLS, rsp, ofl));
1775 
1776 		/*
1777 		 * When processing static TLS - these relocations can be
1778 		 * ignored.
1779 		 */
1780 		if (rtype == R_SPARC_TLS_IE_ADD)
1781 			return (1);
1782 
1783 		return (ld_add_actrel((FLG_REL_TLSFIX | FLG_REL_STLS),
1784 		    rsp, ofl));
1785 	}
1786 
1787 	/*
1788 	 * Building a shared object.
1789 	 *
1790 	 * For dynamic TLS references, ADD relocations are ignored.
1791 	 */
1792 	if ((rtype == R_SPARC_TLS_GD_ADD) || (rtype == R_SPARC_TLS_LDM_ADD) ||
1793 	    (rtype == R_SPARC_TLS_LDO_ADD))
1794 		return (1);
1795 
1796 	/*
1797 	 * Assign a GOT entry for a dynamic TLS reference.
1798 	 */
1799 	if (((rtype == R_SPARC_TLS_LDM_HI22) ||
1800 	    (rtype == R_SPARC_TLS_LDM_LO10)) &&
1801 	    ((gnp = ld_find_gotndx(&(sdp->sd_GOTndxs), GOT_REF_TLSLD,
1802 	    ofl, rsp)) == 0)) {
1803 
1804 		if (ld_assign_got_TLS(local, rsp, ofl, sdp, gnp, GOT_REF_TLSLD,
1805 		    FLG_REL_MTLS, rtype, M_R_DTPMOD, 0) == S_ERROR)
1806 			return (S_ERROR);
1807 
1808 	} else if (((rtype == R_SPARC_TLS_GD_HI22) ||
1809 	    (rtype == R_SPARC_TLS_GD_LO10)) &&
1810 	    ((gnp = ld_find_gotndx(&(sdp->sd_GOTndxs), GOT_REF_TLSGD,
1811 	    ofl, rsp)) == 0)) {
1812 
1813 		if (ld_assign_got_TLS(local, rsp, ofl, sdp, gnp, GOT_REF_TLSGD,
1814 		    FLG_REL_DTLS, rtype, M_R_DTPMOD, M_R_DTPOFF) == S_ERROR)
1815 			return (S_ERROR);
1816 	}
1817 
1818 	/*
1819 	 * For GD/LD TLS reference - TLS_{GD,LD}_CALL, this will eventually
1820 	 * cause a call to __tls_get_addr().  Convert this relocation to that
1821 	 * symbol now, and prepare for the PLT magic.
1822 	 */
1823 	if ((rtype == R_SPARC_TLS_GD_CALL) || (rtype == R_SPARC_TLS_LDM_CALL)) {
1824 		Sym_desc	*tlsgetsym;
1825 
1826 		if ((tlsgetsym = ld_sym_add_u(MSG_ORIG(MSG_SYM_TLSGETADDR_U),
1827 		    ofl, MSG_STR_TLSREL)) == (Sym_desc *)S_ERROR)
1828 			return (S_ERROR);
1829 
1830 		rsp->rel_sym = tlsgetsym;
1831 		rsp->rel_sname = tlsgetsym->sd_name;
1832 		rsp->rel_rtype = R_SPARC_WPLT30;
1833 
1834 		if (ld_reloc_plt(rsp, ofl) == S_ERROR)
1835 			return (S_ERROR);
1836 
1837 		rsp->rel_sym = sdp;
1838 		rsp->rel_sname = sdp->sd_name;
1839 		rsp->rel_rtype = rtype;
1840 		return (1);
1841 	}
1842 
1843 	if (IS_TLS_LD(rtype))
1844 		return (ld_add_actrel(FLG_REL_MTLS, rsp, ofl));
1845 
1846 	return (ld_add_actrel(FLG_REL_DTLS, rsp, ofl));
1847 }
1848 
1849 /*
1850  * ld_allocate_got: if a GOT is to be made, after the section is built this
1851  * function is called to allocate all the GOT slots.  The allocation is
1852  * deferred until after all GOTs have been counted and sorted according
1853  * to their size, for only then will we know how to allocate them on
1854  * a processor like SPARC which has different models for addressing the
1855  * GOT.  SPARC has two: small and large, small uses a signed 13-bit offset
1856  * into the GOT, whereas large uses an unsigned 32-bit offset.
1857  */
1858 static	Sword small_index;	/* starting index for small GOT entries */
1859 static	Sword mixed_index;	/* starting index for mixed GOT entries */
1860 static	Sword large_index;	/* starting index for large GOT entries */
1861 
1862 static uintptr_t
1863 ld_assign_got(Ofl_desc *ofl, Sym_desc * sdp)
1864 {
1865 	Listnode *	lnp;
1866 	Gotndx *	gnp;
1867 
1868 	for (LIST_TRAVERSE(&sdp->sd_GOTndxs, lnp, gnp)) {
1869 		uint_t	gotents;
1870 		Gotref	gref;
1871 		gref = gnp->gn_gotref;
1872 		if ((gref == GOT_REF_TLSGD) || (gref == GOT_REF_TLSLD))
1873 			gotents = 2;
1874 		else
1875 			gotents = 1;
1876 
1877 		switch (gnp->gn_gotndx) {
1878 		case M_GOT_SMALL:
1879 			gnp->gn_gotndx = small_index;
1880 			small_index += gotents;
1881 			if (small_index == 0)
1882 				small_index = M_GOT_XNumber;
1883 			break;
1884 		case M_GOT_MIXED:
1885 			gnp->gn_gotndx = mixed_index;
1886 			mixed_index += gotents;
1887 			break;
1888 		case M_GOT_LARGE:
1889 			gnp->gn_gotndx = large_index;
1890 			large_index += gotents;
1891 			break;
1892 		default:
1893 			eprintf(ofl->ofl_lml, ERR_FATAL,
1894 			    MSG_INTL(MSG_REL_ASSIGNGOT),
1895 			    EC_XWORD(gnp->gn_gotndx), demangle(sdp->sd_name));
1896 			return (S_ERROR);
1897 		}
1898 	}
1899 	return (1);
1900 }
1901 
1902 /*
1903  * Search the GOT index list for a GOT entry with the proper addend.
1904  */
1905 static Gotndx *
1906 ld_find_gotndx(List * lst, Gotref gref, Ofl_desc * ofl, Rel_desc * rdesc)
1907 {
1908 	Listnode *	lnp;
1909 	Gotndx *	gnp;
1910 
1911 	if ((gref == GOT_REF_TLSLD) && ofl->ofl_tlsldgotndx)
1912 		return (ofl->ofl_tlsldgotndx);
1913 
1914 	for (LIST_TRAVERSE(lst, lnp, gnp)) {
1915 		if ((rdesc->rel_raddend == gnp->gn_addend) &&
1916 		    (gref == gnp->gn_gotref))
1917 			return (gnp);
1918 	}
1919 	return ((Gotndx *)0);
1920 }
1921 
1922 static Xword
1923 ld_calc_got_offset(Rel_desc * rdesc, Ofl_desc * ofl)
1924 {
1925 	Os_desc		*osp = ofl->ofl_osgot;
1926 	Sym_desc	*sdp = rdesc->rel_sym;
1927 	Xword		gotndx;
1928 	Gotref		gref;
1929 	Gotndx		*gnp;
1930 
1931 	if (rdesc->rel_flags & FLG_REL_DTLS)
1932 		gref = GOT_REF_TLSGD;
1933 	else if (rdesc->rel_flags & FLG_REL_MTLS)
1934 		gref = GOT_REF_TLSLD;
1935 	else if (rdesc->rel_flags & FLG_REL_STLS)
1936 		gref = GOT_REF_TLSIE;
1937 	else
1938 		gref = GOT_REF_GENERIC;
1939 
1940 	gnp = ld_find_gotndx(&(sdp->sd_GOTndxs), gref, ofl, rdesc);
1941 	assert(gnp);
1942 
1943 	gotndx = (Xword)gnp->gn_gotndx;
1944 
1945 	if ((rdesc->rel_flags & FLG_REL_DTLS) &&
1946 	    (rdesc->rel_rtype == M_R_DTPOFF))
1947 		gotndx++;
1948 
1949 	return ((Xword)((osp->os_shdr->sh_addr) + (gotndx * M_GOT_ENTSIZE) +
1950 	    (-neggotoffset * M_GOT_ENTSIZE)));
1951 }
1952 
1953 static uintptr_t
1954 ld_assign_got_ndx(List * lst, Gotndx * pgnp, Gotref gref, Ofl_desc * ofl,
1955     Rel_desc * rsp, Sym_desc * sdp)
1956 {
1957 	Xword		raddend;
1958 	Gotndx *	gnp, * _gnp;
1959 	Listnode *	lnp, * plnp;
1960 	uint_t		gotents;
1961 
1962 	/* Some TLS requires two relocations with two GOT entries */
1963 	if ((gref == GOT_REF_TLSGD) || (gref == GOT_REF_TLSLD))
1964 		gotents = 2;
1965 	else
1966 		gotents = 1;
1967 
1968 	raddend = rsp->rel_raddend;
1969 	if (pgnp && (pgnp->gn_addend == raddend) && (pgnp->gn_gotref == gref)) {
1970 
1971 		/*
1972 		 * If an entry for this addend already exists, determine if it
1973 		 * has mixed mode GOT access (both PIC and pic).
1974 		 *
1975 		 * In order to be accessible by both large and small pic,
1976 		 * a mixed mode GOT must be located in the positive index
1977 		 * range above _GLOBAL_OFFSET_TABLE_, and in the range
1978 		 * reachable small pic. This is necessary because the large
1979 		 * PIC mode cannot use a negative offset. This implies that
1980 		 * there can be no more than (M_GOT_MAXSMALL/2 - M_GOT_XNumber)
1981 		 * such entries.
1982 		 */
1983 		switch (pgnp->gn_gotndx) {
1984 		case M_GOT_SMALL:
1985 			/*
1986 			 * This one was previously identified as a small
1987 			 * GOT. If this access is large, then convert
1988 			 * it to mixed.
1989 			 */
1990 			if (rsp->rel_rtype != R_SPARC_GOT13) {
1991 				pgnp->gn_gotndx = M_GOT_MIXED;
1992 				mixgotcnt += gotents;
1993 			}
1994 			break;
1995 
1996 		case M_GOT_LARGE:
1997 			/*
1998 			 * This one was previously identified as a large
1999 			 * GOT. If this access is small, convert it to mixed.
2000 			 */
2001 			if (rsp->rel_rtype == R_SPARC_GOT13) {
2002 				smlgotcnt += gotents;
2003 				mixgotcnt += gotents;
2004 				pgnp->gn_gotndx = M_GOT_MIXED;
2005 				sdp->sd_flags |= FLG_SY_SMGOT;
2006 			}
2007 			break;
2008 		}
2009 		return (1);
2010 	}
2011 
2012 	plnp = 0;
2013 	for (LIST_TRAVERSE(lst, lnp, _gnp)) {
2014 		if (_gnp->gn_addend > raddend)
2015 			break;
2016 		plnp = lnp;
2017 	}
2018 
2019 	/*
2020 	 * Allocate a new entry.
2021 	 */
2022 	if ((gnp = libld_calloc(sizeof (Gotndx), 1)) == 0)
2023 		return (S_ERROR);
2024 	gnp->gn_addend = raddend;
2025 	gnp->gn_gotref = gref;
2026 	ofl->ofl_gotcnt += gotents;
2027 
2028 	if (rsp->rel_rtype == R_SPARC_GOT13) {
2029 		gnp->gn_gotndx = M_GOT_SMALL;
2030 		smlgotcnt += gotents;
2031 		sdp->sd_flags |= FLG_SY_SMGOT;
2032 	} else {
2033 		gnp->gn_gotndx = M_GOT_LARGE;
2034 	}
2035 
2036 	if (gref == GOT_REF_TLSLD) {
2037 		ofl->ofl_tlsldgotndx = gnp;
2038 		return (1);
2039 	}
2040 
2041 	if (plnp == 0) {
2042 		/*
2043 		 * Insert at head of list
2044 		 */
2045 		if (list_prependc(lst, (void *)gnp) == 0)
2046 			return (S_ERROR);
2047 	} else if (_gnp->gn_addend > raddend) {
2048 		/*
2049 		 * Insert in middle of lest
2050 		 */
2051 		if (list_insertc(lst, (void *)gnp, plnp) == 0)
2052 			return (S_ERROR);
2053 	} else {
2054 		/*
2055 		 * Append to tail of list
2056 		 */
2057 		if (list_appendc(lst, (void *)gnp) == 0)
2058 			return (S_ERROR);
2059 	}
2060 	return (1);
2061 }
2062 
2063 static void
2064 ld_assign_plt_ndx(Sym_desc * sdp, Ofl_desc *ofl)
2065 {
2066 	sdp->sd_aux->sa_PLTndx = 1 + ofl->ofl_pltcnt++;
2067 }
2068 
2069 
2070 static uintptr_t
2071 ld_allocate_got(Ofl_desc * ofl)
2072 {
2073 	const Sword	first_large_ndx = M_GOT_MAXSMALL / 2;
2074 	Sym_desc *	sdp;
2075 	Addr		addr;
2076 
2077 	/*
2078 	 * Sanity check -- is this going to fit at all? There are two
2079 	 * limits to be concerned about:
2080 	 *	1) There is a limit on the number of small pic GOT indices,
2081 	 *		given by M_GOT_MAXSMALL.
2082 	 *	2) If there are more than (M_GOT_MAXSMALL/2 - M_GOT_XNumber)
2083 	 *		small GOT indices, there will be items at negative
2084 	 *		offsets from _GLOBAL_OFFSET_TABLE_. Items that are
2085 	 *		accessed via large (PIC) code cannot reach these
2086 	 *		negative slots, so mixed mode items must be in the
2087 	 *		non-negative range. This implies a limit of
2088 	 *		(M_GOT_MAXSMALL/2 - M_GOT_XNumber) mixed mode indices.
2089 	 */
2090 	if (smlgotcnt > M_GOT_MAXSMALL) {
2091 		eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_REL_SMALLGOT),
2092 		    EC_WORD(smlgotcnt), M_GOT_MAXSMALL);
2093 		return (S_ERROR);
2094 	}
2095 	if (mixgotcnt > (first_large_ndx - M_GOT_XNumber)) {
2096 		eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_REL_MIXEDGOT),
2097 		    EC_WORD(mixgotcnt), first_large_ndx - M_GOT_XNumber);
2098 		return (S_ERROR);
2099 	}
2100 
2101 	/*
2102 	 * Set starting offset to be either 0, or a negative index into
2103 	 * the GOT based on the number of small symbols we've got.
2104 	 */
2105 	neggotoffset = ((smlgotcnt >= first_large_ndx) ?
2106 	    (first_large_ndx - smlgotcnt) : 0);
2107 
2108 	/*
2109 	 * Initialize the got offsets used by assign_got() to
2110 	 * locate GOT items:
2111 	 *	small - Starting index of items referenced only
2112 	 *		by small offsets (-Kpic).
2113 	 *	mixed - Starting index of items referenced
2114 	 *		by both large (-KPIC) and small (-Kpic).
2115 	 *	large - Indexes referenced only by large (-KPIC)
2116 	 *
2117 	 *  Small items can have negative indexes (i.e. lie below
2118 	 *	_GLOBAL_OFFSET_TABLE_). Mixed and large items must have
2119 	 *	non-negative offsets.
2120 	 */
2121 	small_index = (neggotoffset == 0) ? M_GOT_XNumber : neggotoffset;
2122 	large_index = neggotoffset + smlgotcnt;
2123 	mixed_index = large_index - mixgotcnt;
2124 
2125 	/*
2126 	 * Assign bias to GOT symbols.
2127 	 */
2128 	addr = -neggotoffset * M_GOT_ENTSIZE;
2129 	if (sdp = ld_sym_find(MSG_ORIG(MSG_SYM_GOFTBL), SYM_NOHASH, 0, ofl))
2130 		sdp->sd_sym->st_value = addr;
2131 	if (sdp = ld_sym_find(MSG_ORIG(MSG_SYM_GOFTBL_U), SYM_NOHASH, 0, ofl))
2132 		sdp->sd_sym->st_value = addr;
2133 
2134 	if (ofl->ofl_tlsldgotndx) {
2135 		ofl->ofl_tlsldgotndx->gn_gotndx = large_index;
2136 		large_index += 2;
2137 	}
2138 	return (1);
2139 }
2140 
2141 /*
2142  * Initializes .got[0] with the _DYNAMIC symbol value.
2143  */
2144 static uintptr_t
2145 ld_fillin_gotplt(Ofl_desc *ofl)
2146 {
2147 	if (ofl->ofl_osgot) {
2148 		Sym_desc	*sdp;
2149 
2150 		if ((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_DYNAMIC_U),
2151 		    SYM_NOHASH, 0, ofl)) != NULL) {
2152 			uchar_t	*genptr;
2153 
2154 			genptr = ((uchar_t *)ofl->ofl_osgot->os_outdata->d_buf +
2155 			    (-neggotoffset * M_GOT_ENTSIZE) +
2156 			    (M_GOT_XDYNAMIC * M_GOT_ENTSIZE));
2157 			/* LINTED */
2158 			*((Xword *)genptr) = sdp->sd_sym->st_value;
2159 			if (ofl->ofl_flags1 & FLG_OF1_ENCDIFF)
2160 				/* LINTED */
2161 				*((Xword *)genptr) =
2162 				    /* LINTED */
2163 				    ld_bswap_Xword(*((Xword *)genptr));
2164 		}
2165 	}
2166 	return (1);
2167 }
2168 
2169 
2170 
2171 /*
2172  * Template for generating "void (*)(void)" function
2173  */
2174 static const uchar_t nullfunc_tmpl[] = {
2175 /* 0x00 */	0x81, 0xc3, 0xe0, 0x08,		/* retl */
2176 /* 0x04 */	0x01, 0x00, 0x00, 0x00		/* nop */
2177 };
2178 
2179 
2180 
2181 /*
2182  * Return the ld_targ definition for this target.
2183  */
2184 const Target *
2185 ld_targ_init_sparc(void)
2186 {
2187 	static const Target _ld_targ = {
2188 		{			/* Target_mach */
2189 			M_MACH,			/* m_mach */
2190 			M_MACHPLUS,		/* m_machplus */
2191 			M_FLAGSPLUS,		/* m_flagsplus */
2192 			M_CLASS,		/* m_class */
2193 			M_DATA,			/* m_data */
2194 
2195 			M_SEGM_ALIGN,		/* m_segm_align */
2196 			M_SEGM_ORIGIN,		/* m_segm_origin */
2197 			M_DATASEG_PERM,		/* m_dataseg_perm */
2198 			M_WORD_ALIGN,		/* m_word_align */
2199 						/* m_def_interp */
2200 #if	defined(_ELF64)
2201 			MSG_ORIG(MSG_PTH_RTLD_SPARCV9),
2202 #else
2203 			MSG_ORIG(MSG_PTH_RTLD),
2204 #endif
2205 
2206 			/* Relocation type codes */
2207 			M_R_ARRAYADDR,		/* m_r_arrayaddr */
2208 			M_R_COPY,		/* m_r_copy */
2209 			M_R_GLOB_DAT,		/* m_r_glob_dat */
2210 			M_R_JMP_SLOT,		/* m_r_jmp_slot */
2211 			M_R_NUM,		/* m_r_num */
2212 			M_R_NONE,		/* m_r_none */
2213 			M_R_RELATIVE,		/* m_r_relative */
2214 			M_R_REGISTER,		/* m_r_register */
2215 
2216 			/* Relocation related constants */
2217 			M_REL_DT_COUNT,		/* m_rel_dt_count */
2218 			M_REL_DT_ENT,		/* m_rel_dt_ent */
2219 			M_REL_DT_SIZE,		/* m_rel_dt_size */
2220 			M_REL_DT_TYPE,		/* m_rel_dt_type */
2221 			M_REL_SHT_TYPE,		/* m_rel_sht_type */
2222 
2223 			/* GOT related constants */
2224 			M_GOT_ENTSIZE,		/* m_got_entsize */
2225 			M_GOT_XNumber,		/* m_got_xnumber */
2226 
2227 			/* PLT related constants */
2228 			M_PLT_ALIGN,		/* m_plt_align */
2229 			M_PLT_ENTSIZE,		/* m_plt_entsize */
2230 			M_PLT_RESERVSZ,		/* m_plt_reservsz */
2231 			M_PLT_SHF_FLAGS,	/* m_plt_shf_flags */
2232 
2233 			M_DT_REGISTER,		/* m_dt_register */
2234 		},
2235 		{			/* Target_machid */
2236 			M_ID_ARRAY,		/* id_array */
2237 			M_ID_BSS,		/* id_bss */
2238 			M_ID_CAP,		/* id_cap */
2239 			M_ID_DATA,		/* id_data */
2240 			M_ID_DYNAMIC,		/* id_dynamic */
2241 			M_ID_DYNSORT,		/* id_dynsort */
2242 			M_ID_DYNSTR,		/* id_dynstr */
2243 			M_ID_DYNSYM,		/* id_dynsym */
2244 			M_ID_DYNSYM_NDX,	/* id_dynsym_ndx */
2245 			M_ID_GOT,		/* id_got */
2246 			M_ID_GOTDATA,		/* id_gotdata */
2247 			M_ID_HASH,		/* id_hash */
2248 			M_ID_INTERP,		/* id_interp */
2249 			M_ID_UNKNOWN,		/* id_lbss (unused) */
2250 			M_ID_LDYNSYM,		/* id_ldynsym */
2251 			M_ID_NOTE,		/* id_note */
2252 			M_ID_NULL,		/* id_null */
2253 			M_ID_PLT,		/* id_plt */
2254 			M_ID_REL,		/* id_rel */
2255 			M_ID_STRTAB,		/* id_strtab */
2256 			M_ID_SYMINFO,		/* id_syminfo */
2257 			M_ID_SYMTAB,		/* id_symtab */
2258 			M_ID_SYMTAB_NDX,	/* id_symtab_ndx */
2259 			M_ID_TEXT,		/* id_text */
2260 			M_ID_TLS,		/* id_tls */
2261 			M_ID_TLSBSS,		/* id_tlsbss */
2262 			M_ID_UNKNOWN,		/* id_unknown */
2263 			M_ID_UNKNOWN,		/* id_unwind (unused) */
2264 			M_ID_USER,		/* id_user */
2265 			M_ID_VERSION,		/* id_version */
2266 		},
2267 		{			/* Target_nullfunc */
2268 			nullfunc_tmpl,		/* nf_template */
2269 			sizeof (nullfunc_tmpl),	/* nf_size */
2270 		},
2271 		{			/* Target_machrel */
2272 			reloc_table,
2273 
2274 			ld_init_rel,		/* mr_init_rel */
2275 			ld_mach_eflags,		/* mr_mach_eflags */
2276 			ld_mach_make_dynamic,	/* mr_mach_make_dynamic */
2277 			ld_mach_update_odynamic, /* mr_mach_update_odynamic */
2278 			ld_calc_plt_addr,	/* mr_calc_plt_addr */
2279 			ld_perform_outreloc,	/* mr_perform_outreloc */
2280 			ld_do_activerelocs,	/* mr_do_activerelocs */
2281 			ld_add_outrel,		/* mr_add_outrel */
2282 			ld_reloc_register,	/* mr_reloc_register */
2283 			ld_reloc_local,		/* mr_reloc_local */
2284 			ld_reloc_GOTOP,		/* mr_reloc_GOTOP */
2285 			ld_reloc_TLS,		/* mr_reloc_TLS */
2286 			ld_assign_got,		/* mr_assign_got */
2287 			ld_find_gotndx,		/* mr_find_gotndx */
2288 			ld_calc_got_offset,	/* mr_calc_got_offset */
2289 			ld_assign_got_ndx,	/* mr_assign_got_ndx */
2290 			ld_assign_plt_ndx,	/* mr_assign_plt_ndx */
2291 			ld_allocate_got,	/* mr_allocate_got */
2292 			ld_fillin_gotplt,	/* mr_fillin_gotplt */
2293 		},
2294 		{			/* Target_machsym */
2295 			ld_reg_check_sparc,	/* ms_reg_check */
2296 			ld_mach_sym_typecheck_sparc, /* ms_mach_sym_typecheck */
2297 			ld_is_regsym_sparc,	/* ms_is_regsym */
2298 			ld_reg_find_sparc,	/* ms_reg_find */
2299 			ld_reg_enter_sparc	/* ms_reg_enter */
2300 		},
2301 		{			/* Target_unwind */
2302 			NULL,		/* uw_make_unwindhdr */
2303 			NULL,		/* uw_populate_unwindhdr */
2304 			NULL,		/* uw_append_unwind */
2305 		}
2306 	};
2307 
2308 	return (&_ld_targ);
2309 }
2310