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