xref: /titanic_50/usr/src/cmd/sgs/libld/common/machrel.amd.c (revision b2b3ca14272ffe2a6fc37bab2ab65b8f6702d750)
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 2009 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /* Get the x86 version of the relocation engine */
28 #define	DO_RELOC_LIBLD_X86
29 
30 #include	<string.h>
31 #include	<stdio.h>
32 #include	<strings.h>
33 #include	<sys/elf_amd64.h>
34 #include	<debug.h>
35 #include	<reloc.h>
36 #include	<i386/machdep_x86.h>
37 #include	"msg.h"
38 #include	"_libld.h"
39 
40 /*
41  * Search the GOT index list for a GOT entry with a matching reference and the
42  * proper addend.
43  */
44 static Gotndx *
45 ld_find_got_ndx(Alist *alp, Gotref gref, Ofl_desc *ofl, Rel_desc *rdesc)
46 {
47 	Aliste	idx;
48 	Gotndx	*gnp;
49 
50 	assert(rdesc != 0);
51 
52 	if ((gref == GOT_REF_TLSLD) && ofl->ofl_tlsldgotndx)
53 		return (ofl->ofl_tlsldgotndx);
54 
55 	for (ALIST_TRAVERSE(alp, idx, gnp)) {
56 		if ((rdesc->rel_raddend == gnp->gn_addend) &&
57 		    (gnp->gn_gotref == gref)) {
58 			return (gnp);
59 		}
60 	}
61 	return (NULL);
62 }
63 
64 static Xword
65 ld_calc_got_offset(Rel_desc *rdesc, Ofl_desc *ofl)
66 {
67 	Os_desc		*osp = ofl->ofl_osgot;
68 	Sym_desc	*sdp = rdesc->rel_sym;
69 	Xword		gotndx;
70 	Gotref		gref;
71 	Gotndx		*gnp;
72 
73 	if (rdesc->rel_flags & FLG_REL_DTLS)
74 		gref = GOT_REF_TLSGD;
75 	else if (rdesc->rel_flags & FLG_REL_MTLS)
76 		gref = GOT_REF_TLSLD;
77 	else if (rdesc->rel_flags & FLG_REL_STLS)
78 		gref = GOT_REF_TLSIE;
79 	else
80 		gref = GOT_REF_GENERIC;
81 
82 	gnp = ld_find_got_ndx(sdp->sd_GOTndxs, gref, ofl, rdesc);
83 	assert(gnp);
84 
85 	gotndx = (Xword)gnp->gn_gotndx;
86 
87 	if ((rdesc->rel_flags & FLG_REL_DTLS) &&
88 	    (rdesc->rel_rtype == R_AMD64_DTPOFF64))
89 		gotndx++;
90 
91 	return ((Xword)(osp->os_shdr->sh_addr + (gotndx * M_GOT_ENTSIZE)));
92 }
93 
94 static Word
95 ld_init_rel(Rel_desc *reld, void *reloc)
96 {
97 	Rela	*rel = (Rela *)reloc;
98 
99 	/* LINTED */
100 	reld->rel_rtype = (Word)ELF_R_TYPE(rel->r_info, M_MACH);
101 	reld->rel_roffset = rel->r_offset;
102 	reld->rel_raddend = rel->r_addend;
103 	reld->rel_typedata = 0;
104 
105 	reld->rel_flags |= FLG_REL_RELA;
106 
107 	return ((Word)ELF_R_SYM(rel->r_info));
108 }
109 
110 static void
111 ld_mach_eflags(Ehdr *ehdr, Ofl_desc *ofl)
112 {
113 	ofl->ofl_dehdr->e_flags |= ehdr->e_flags;
114 }
115 
116 static void
117 ld_mach_make_dynamic(Ofl_desc *ofl, size_t *cnt)
118 {
119 	if (!(ofl->ofl_flags & FLG_OF_RELOBJ)) {
120 		/*
121 		 * Create this entry if we are going to create a PLT table.
122 		 */
123 		if (ofl->ofl_pltcnt)
124 			(*cnt)++;		/* DT_PLTGOT */
125 	}
126 }
127 
128 static void
129 ld_mach_update_odynamic(Ofl_desc *ofl, Dyn **dyn)
130 {
131 	if (((ofl->ofl_flags & FLG_OF_RELOBJ) == 0) && ofl->ofl_pltcnt) {
132 		(*dyn)->d_tag = DT_PLTGOT;
133 		if (ofl->ofl_osgot)
134 			(*dyn)->d_un.d_ptr = ofl->ofl_osgot->os_shdr->sh_addr;
135 		else
136 			(*dyn)->d_un.d_ptr = 0;
137 		(*dyn)++;
138 	}
139 }
140 
141 static Xword
142 ld_calc_plt_addr(Sym_desc *sdp, Ofl_desc *ofl)
143 {
144 	Xword	value;
145 
146 	value = (Xword)(ofl->ofl_osplt->os_shdr->sh_addr) +
147 	    M_PLT_RESERVSZ + ((sdp->sd_aux->sa_PLTndx - 1) * M_PLT_ENTSIZE);
148 	return (value);
149 }
150 
151 /*
152  *  Build a single plt entry - code is:
153  *	JMP	*name1@GOTPCREL(%rip)
154  *	PUSHL	$index
155  *	JMP	.PLT0
156  */
157 static uchar_t pltn_entry[M_PLT_ENTSIZE] = {
158 /* 0x00 jmpq *name1@GOTPCREL(%rip) */	0xff, 0x25, 0x00, 0x00, 0x00, 0x00,
159 /* 0x06 pushq $index */			0x68, 0x00, 0x00, 0x00, 0x00,
160 /* 0x0b jmpq  .plt0(%rip) */		0xe9, 0x00, 0x00, 0x00, 0x00
161 /* 0x10 */
162 };
163 
164 static uintptr_t
165 plt_entry(Ofl_desc * ofl, Sym_desc * sdp)
166 {
167 	uchar_t		*plt0, *pltent, *gotent;
168 	Sword		plt_off;
169 	Word		got_off;
170 	Xword		val1;
171 	int		bswap = (ofl->ofl_flags1 & FLG_OF1_ENCDIFF) != 0;
172 
173 	got_off = sdp->sd_aux->sa_PLTGOTndx * M_GOT_ENTSIZE;
174 	plt_off = M_PLT_RESERVSZ + ((sdp->sd_aux->sa_PLTndx - 1) *
175 	    M_PLT_ENTSIZE);
176 	plt0 = (uchar_t *)(ofl->ofl_osplt->os_outdata->d_buf);
177 	pltent = plt0 + plt_off;
178 	gotent = (uchar_t *)(ofl->ofl_osgot->os_outdata->d_buf) + got_off;
179 
180 	bcopy(pltn_entry, pltent, sizeof (pltn_entry));
181 	/*
182 	 * Fill in the got entry with the address of the next instruction.
183 	 */
184 	/* LINTED */
185 	*(Word *)gotent = ofl->ofl_osplt->os_shdr->sh_addr + plt_off +
186 	    M_PLT_INSSIZE;
187 	if (bswap)
188 		/* LINTED */
189 		*(Word *)gotent = ld_bswap_Word(*(Word *)gotent);
190 
191 	/*
192 	 * If '-z noreloc' is specified - skip the do_reloc_ld
193 	 * stage.
194 	 */
195 	if (!OFL_DO_RELOC(ofl))
196 		return (1);
197 
198 	/*
199 	 * patchup:
200 	 *	jmpq	*name1@gotpcrel(%rip)
201 	 *
202 	 * NOTE: 0x06 represents next instruction.
203 	 */
204 	val1 = (ofl->ofl_osgot->os_shdr->sh_addr + got_off) -
205 	    (ofl->ofl_osplt->os_shdr->sh_addr + plt_off) - 0x06;
206 
207 	if (do_reloc_ld(R_AMD64_GOTPCREL, &pltent[0x02],
208 	    &val1, MSG_ORIG(MSG_SYM_PLTENT),
209 	    MSG_ORIG(MSG_SPECFIL_PLTENT), bswap, ofl->ofl_lml) == 0) {
210 		eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_PLT_PLTNFAIL),
211 		    sdp->sd_aux->sa_PLTndx, demangle(sdp->sd_name));
212 		return (S_ERROR);
213 	}
214 
215 	/*
216 	 * patchup:
217 	 *	pushq	$pltndx
218 	 */
219 	val1 = (Xword)(sdp->sd_aux->sa_PLTndx - 1);
220 
221 	if (do_reloc_ld(R_AMD64_32, &pltent[0x07],
222 	    &val1, MSG_ORIG(MSG_SYM_PLTENT),
223 	    MSG_ORIG(MSG_SPECFIL_PLTENT), bswap, ofl->ofl_lml) == 0) {
224 		eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_PLT_PLTNFAIL),
225 		    sdp->sd_aux->sa_PLTndx, demangle(sdp->sd_name));
226 		return (S_ERROR);
227 	}
228 
229 	/*
230 	 * patchup:
231 	 *	jmpq	.plt0(%rip)
232 	 * NOTE: 0x10 represents next instruction. The rather complex
233 	 * series of casts is necessary to sign extend an offset into
234 	 * a 64-bit value while satisfying various compiler error
235 	 * checks.  Handle with care.
236 	 */
237 	val1 = (Xword)((intptr_t)((uintptr_t)plt0 -
238 	    (uintptr_t)(&pltent[0x10])));
239 
240 	if (do_reloc_ld(R_AMD64_PC32, &pltent[0x0c],
241 	    &val1, MSG_ORIG(MSG_SYM_PLTENT),
242 	    MSG_ORIG(MSG_SPECFIL_PLTENT), bswap, ofl->ofl_lml) == 0) {
243 		eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_PLT_PLTNFAIL),
244 		    sdp->sd_aux->sa_PLTndx, demangle(sdp->sd_name));
245 		return (S_ERROR);
246 	}
247 
248 	return (1);
249 }
250 
251 static uintptr_t
252 ld_perform_outreloc(Rel_desc * orsp, Ofl_desc * ofl)
253 {
254 	Os_desc *	relosp, * osp = 0;
255 	Word		ndx;
256 	Xword		roffset, value;
257 	Sxword		raddend;
258 	Rela		rea;
259 	char		*relbits;
260 	Sym_desc *	sdp, * psym = (Sym_desc *)0;
261 	int		sectmoved = 0;
262 
263 	raddend = orsp->rel_raddend;
264 	sdp = orsp->rel_sym;
265 
266 	/*
267 	 * If the section this relocation is against has been discarded
268 	 * (-zignore), then also discard (skip) the relocation itself.
269 	 */
270 	if (orsp->rel_isdesc && ((orsp->rel_flags &
271 	    (FLG_REL_GOT | FLG_REL_BSS | FLG_REL_PLT | FLG_REL_NOINFO)) == 0) &&
272 	    (orsp->rel_isdesc->is_flags & FLG_IS_DISCARD)) {
273 		DBG_CALL(Dbg_reloc_discard(ofl->ofl_lml, M_MACH, orsp));
274 		return (1);
275 	}
276 
277 	/*
278 	 * If this is a relocation against a move table, or expanded move
279 	 * table, adjust the relocation entries.
280 	 */
281 	if (orsp->rel_move)
282 		ld_adj_movereloc(ofl, orsp);
283 
284 	/*
285 	 * If this is a relocation against a section then we need to adjust the
286 	 * raddend field to compensate for the new position of the input section
287 	 * within the new output section.
288 	 */
289 	if (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION) {
290 		if (ofl->ofl_parsyms &&
291 		    (sdp->sd_isc->is_flags & FLG_IS_RELUPD) &&
292 		    /* LINTED */
293 		    (psym = ld_am_I_partial(orsp, orsp->rel_raddend))) {
294 			DBG_CALL(Dbg_move_outsctadj(ofl->ofl_lml, psym));
295 			sectmoved = 1;
296 			if (ofl->ofl_flags & FLG_OF_RELOBJ)
297 				raddend = psym->sd_sym->st_value;
298 			else
299 				raddend = psym->sd_sym->st_value -
300 				    psym->sd_isc->is_osdesc->os_shdr->sh_addr;
301 			/* LINTED */
302 			raddend += (Off)_elf_getxoff(psym->sd_isc->is_indata);
303 			if (psym->sd_isc->is_shdr->sh_flags & SHF_ALLOC)
304 				raddend +=
305 				    psym->sd_isc->is_osdesc->os_shdr->sh_addr;
306 		} else {
307 			/* LINTED */
308 			raddend += (Off)_elf_getxoff(sdp->sd_isc->is_indata);
309 			if (sdp->sd_isc->is_shdr->sh_flags & SHF_ALLOC)
310 				raddend +=
311 				    sdp->sd_isc->is_osdesc->os_shdr->sh_addr;
312 		}
313 	}
314 
315 	value = sdp->sd_sym->st_value;
316 
317 	if (orsp->rel_flags & FLG_REL_GOT) {
318 		/*
319 		 * Note: for GOT relative relocations on amd64
320 		 *	 we discard the addend.  It was relevant
321 		 *	 to the reference - not to the data item
322 		 *	 being referenced (ie: that -4 thing).
323 		 */
324 		raddend = 0;
325 		osp = ofl->ofl_osgot;
326 		roffset = ld_calc_got_offset(orsp, ofl);
327 
328 	} else if (orsp->rel_flags & FLG_REL_PLT) {
329 		/*
330 		 * Note that relocations for PLT's actually
331 		 * cause a relocation againt the GOT.
332 		 */
333 		osp = ofl->ofl_osplt;
334 		roffset = (ofl->ofl_osgot->os_shdr->sh_addr) +
335 		    sdp->sd_aux->sa_PLTGOTndx * M_GOT_ENTSIZE;
336 		raddend = 0;
337 		if (plt_entry(ofl, sdp) == S_ERROR)
338 			return (S_ERROR);
339 
340 	} else if (orsp->rel_flags & FLG_REL_BSS) {
341 		/*
342 		 * This must be a R_AMD64_COPY.  For these set the roffset to
343 		 * point to the new symbols location.
344 		 */
345 		osp = ofl->ofl_isbss->is_osdesc;
346 		roffset = value;
347 
348 		/*
349 		 * The raddend doesn't mean anything in a R_SPARC_COPY
350 		 * relocation.  Null it out because it can confuse people.
351 		 */
352 		raddend = 0;
353 	} else {
354 		osp = orsp->rel_osdesc;
355 
356 		/*
357 		 * Calculate virtual offset of reference point; equals offset
358 		 * into section + vaddr of section for loadable sections, or
359 		 * offset plus section displacement for nonloadable sections.
360 		 */
361 		roffset = orsp->rel_roffset +
362 		    (Off)_elf_getxoff(orsp->rel_isdesc->is_indata);
363 		if (!(ofl->ofl_flags & FLG_OF_RELOBJ))
364 			roffset += orsp->rel_isdesc->is_osdesc->
365 			    os_shdr->sh_addr;
366 	}
367 
368 	if ((osp == 0) || ((relosp = osp->os_relosdesc) == 0))
369 		relosp = ofl->ofl_osrel;
370 
371 	/*
372 	 * Assign the symbols index for the output relocation.  If the
373 	 * relocation refers to a SECTION symbol then it's index is based upon
374 	 * the output sections symbols index.  Otherwise the index can be
375 	 * derived from the symbols index itself.
376 	 */
377 	if (orsp->rel_rtype == R_AMD64_RELATIVE)
378 		ndx = STN_UNDEF;
379 	else if ((orsp->rel_flags & FLG_REL_SCNNDX) ||
380 	    (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION)) {
381 		if (sectmoved == 0) {
382 			/*
383 			 * Check for a null input section. This can
384 			 * occur if this relocation references a symbol
385 			 * generated by sym_add_sym().
386 			 */
387 			if (sdp->sd_isc && sdp->sd_isc->is_osdesc)
388 				ndx = sdp->sd_isc->is_osdesc->os_identndx;
389 			else
390 				ndx = sdp->sd_shndx;
391 		} else
392 			ndx = ofl->ofl_parexpnndx;
393 	} else
394 		ndx = sdp->sd_symndx;
395 
396 	/*
397 	 * Add the symbols 'value' to the addend field.
398 	 */
399 	if (orsp->rel_flags & FLG_REL_ADVAL)
400 		raddend += value;
401 
402 	/*
403 	 * The addend field for R_AMD64_DTPMOD64 means nothing.  The addend
404 	 * is propagated in the corresponding R_AMD64_DTPOFF64 relocation.
405 	 */
406 	if (orsp->rel_rtype == R_AMD64_DTPMOD64)
407 		raddend = 0;
408 
409 	relbits = (char *)relosp->os_outdata->d_buf;
410 
411 	rea.r_info = ELF_R_INFO(ndx, orsp->rel_rtype);
412 	rea.r_offset = roffset;
413 	rea.r_addend = raddend;
414 	DBG_CALL(Dbg_reloc_out(ofl, ELF_DBG_LD, SHT_RELA, &rea, relosp->os_name,
415 	    orsp->rel_sname));
416 
417 	/*
418 	 * Assert we haven't walked off the end of our relocation table.
419 	 */
420 	assert(relosp->os_szoutrels <= relosp->os_shdr->sh_size);
421 
422 	(void) memcpy((relbits + relosp->os_szoutrels),
423 	    (char *)&rea, sizeof (Rela));
424 	relosp->os_szoutrels += (Xword)sizeof (Rela);
425 
426 	/*
427 	 * Determine if this relocation is against a non-writable, allocatable
428 	 * section.  If so we may need to provide a text relocation diagnostic.
429 	 * Note that relocations against the .plt (R_AMD64_JUMP_SLOT) actually
430 	 * result in modifications to the .got.
431 	 */
432 	if (orsp->rel_rtype == R_AMD64_JUMP_SLOT)
433 		osp = ofl->ofl_osgot;
434 
435 	ld_reloc_remain_entry(orsp, osp, ofl);
436 	return (1);
437 }
438 
439 /*
440  * amd64 Instructions for TLS processing
441  */
442 static uchar_t tlsinstr_gd_ie[] = {
443 	/*
444 	 *	0x00 movq %fs:0, %rax
445 	 */
446 	0x64, 0x48, 0x8b, 0x04, 0x25,
447 	0x00, 0x00, 0x00, 0x00,
448 	/*
449 	 *	0x09 addq x@gottpoff(%rip), %rax
450 	 */
451 	0x48, 0x03, 0x05, 0x00, 0x00,
452 	0x00, 0x00
453 };
454 
455 static uchar_t tlsinstr_gd_le[] = {
456 	/*
457 	 *	0x00 movq %fs:0, %rax
458 	 */
459 	0x64, 0x48, 0x8b, 0x04, 0x25,
460 	0x00, 0x00, 0x00, 0x00,
461 	/*
462 	 *	0x09 leaq x@gottpoff(%rip), %rax
463 	 */
464 	0x48, 0x8d, 0x80, 0x00, 0x00,
465 	0x00, 0x00
466 };
467 
468 static uchar_t tlsinstr_ld_le[] = {
469 	/*
470 	 * .byte 0x66
471 	 */
472 	0x66,
473 	/*
474 	 * .byte 0x66
475 	 */
476 	0x66,
477 	/*
478 	 * .byte 0x66
479 	 */
480 	0x66,
481 	/*
482 	 * movq %fs:0, %rax
483 	 */
484 	0x64, 0x48, 0x8b, 0x04, 0x25,
485 	0x00, 0x00, 0x00, 0x00
486 };
487 
488 
489 static Fixupret
490 tls_fixups(Ofl_desc *ofl, Rel_desc *arsp)
491 {
492 	Sym_desc	*sdp = arsp->rel_sym;
493 	Word		rtype = arsp->rel_rtype;
494 	uchar_t		*offset;
495 
496 	offset = (uchar_t *)((uintptr_t)arsp->rel_roffset +
497 	    (uintptr_t)_elf_getxoff(arsp->rel_isdesc->is_indata) +
498 	    (uintptr_t)arsp->rel_osdesc->os_outdata->d_buf);
499 
500 	if (sdp->sd_ref == REF_DYN_NEED) {
501 		/*
502 		 * IE reference model
503 		 */
504 		switch (rtype) {
505 		case R_AMD64_TLSGD:
506 			/*
507 			 *  GD -> IE
508 			 *
509 			 * Transition:
510 			 *	0x00 .byte 0x66
511 			 *	0x01 leaq x@tlsgd(%rip), %rdi
512 			 *	0x08 .word 0x6666
513 			 *	0x0a rex64
514 			 *	0x0b call __tls_get_addr@plt
515 			 *	0x10
516 			 * To:
517 			 *	0x00 movq %fs:0, %rax
518 			 *	0x09 addq x@gottpoff(%rip), %rax
519 			 *	0x10
520 			 */
521 			DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
522 			    R_AMD64_GOTTPOFF, arsp));
523 			arsp->rel_rtype = R_AMD64_GOTTPOFF;
524 			arsp->rel_roffset += 8;
525 			arsp->rel_raddend = (Sxword)-4;
526 
527 			/*
528 			 * Adjust 'offset' to beginning of instruction
529 			 * sequence.
530 			 */
531 			offset -= 4;
532 			(void) memcpy(offset, tlsinstr_gd_ie,
533 			    sizeof (tlsinstr_gd_ie));
534 			return (FIX_RELOC);
535 
536 		case R_AMD64_PLT32:
537 			/*
538 			 * Fixup done via the TLS_GD relocation.
539 			 */
540 			DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
541 			    R_AMD64_NONE, arsp));
542 			return (FIX_DONE);
543 		}
544 	}
545 
546 	/*
547 	 * LE reference model
548 	 */
549 	switch (rtype) {
550 	case R_AMD64_TLSGD:
551 		/*
552 		 * GD -> LE
553 		 *
554 		 * Transition:
555 		 *	0x00 .byte 0x66
556 		 *	0x01 leaq x@tlsgd(%rip), %rdi
557 		 *	0x08 .word 0x6666
558 		 *	0x0a rex64
559 		 *	0x0b call __tls_get_addr@plt
560 		 *	0x10
561 		 * To:
562 		 *	0x00 movq %fs:0, %rax
563 		 *	0x09 leaq x@tpoff(%rax), %rax
564 		 *	0x10
565 		 */
566 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
567 		    R_AMD64_TPOFF32, arsp));
568 		arsp->rel_rtype = R_AMD64_TPOFF32;
569 		arsp->rel_roffset += 8;
570 		arsp->rel_raddend = 0;
571 
572 		/*
573 		 * Adjust 'offset' to beginning of instruction sequence.
574 		 */
575 		offset -= 4;
576 		(void) memcpy(offset, tlsinstr_gd_le, sizeof (tlsinstr_gd_le));
577 		return (FIX_RELOC);
578 
579 	case R_AMD64_GOTTPOFF:
580 		/*
581 		 * IE -> LE
582 		 *
583 		 * Transition:
584 		 *	0x00 movq %fs:0, %rax
585 		 *	0x09 addq x@gottopoff(%rip), %rax
586 		 *	0x10
587 		 * To:
588 		 *	0x00 movq %fs:0, %rax
589 		 *	0x09 leaq x@tpoff(%rax), %rax
590 		 *	0x10
591 		 */
592 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
593 		    R_AMD64_TPOFF32, arsp));
594 		arsp->rel_rtype = R_AMD64_TPOFF32;
595 		arsp->rel_raddend = 0;
596 
597 		/*
598 		 * Adjust 'offset' to beginning of instruction sequence.
599 		 */
600 		offset -= 12;
601 
602 		/*
603 		 * Same code sequence used in the GD -> LE transition.
604 		 */
605 		(void) memcpy(offset, tlsinstr_gd_le, sizeof (tlsinstr_gd_le));
606 		return (FIX_RELOC);
607 
608 	case R_AMD64_TLSLD:
609 		/*
610 		 * LD -> LE
611 		 *
612 		 * Transition
613 		 *	0x00 leaq x1@tlsgd(%rip), %rdi
614 		 *	0x07 call __tls_get_addr@plt
615 		 *	0x0c
616 		 * To:
617 		 *	0x00 .byte 0x66
618 		 *	0x01 .byte 0x66
619 		 *	0x02 .byte 0x66
620 		 *	0x03 movq %fs:0, %rax
621 		 */
622 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
623 		    R_AMD64_NONE, arsp));
624 		offset -= 3;
625 		(void) memcpy(offset, tlsinstr_ld_le, sizeof (tlsinstr_ld_le));
626 		return (FIX_DONE);
627 
628 	case R_AMD64_DTPOFF32:
629 		/*
630 		 * LD->LE
631 		 *
632 		 * Transition:
633 		 *	0x00 leaq x1@dtpoff(%rax), %rcx
634 		 * To:
635 		 *	0x00 leaq x1@tpoff(%rax), %rcx
636 		 */
637 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
638 		    R_AMD64_TPOFF32, arsp));
639 		arsp->rel_rtype = R_AMD64_TPOFF32;
640 		arsp->rel_raddend = 0;
641 		return (FIX_RELOC);
642 	}
643 
644 	return (FIX_RELOC);
645 }
646 
647 static uintptr_t
648 ld_do_activerelocs(Ofl_desc *ofl)
649 {
650 	Rel_desc	*arsp;
651 	Rel_cache	*rcp;
652 	Aliste		idx;
653 	uintptr_t	return_code = 1;
654 	ofl_flag_t	flags = ofl->ofl_flags;
655 
656 	if (ofl->ofl_actrels)
657 		DBG_CALL(Dbg_reloc_doact_title(ofl->ofl_lml));
658 
659 	/*
660 	 * Process active relocations.
661 	 */
662 	for (APLIST_TRAVERSE(ofl->ofl_actrels, idx, rcp)) {
663 		/* LINTED */
664 		for (arsp = (Rel_desc *)(rcp + 1);
665 		    arsp < rcp->rc_free; arsp++) {
666 			uchar_t		*addr;
667 			Xword 		value;
668 			Sym_desc	*sdp;
669 			const char	*ifl_name;
670 			Xword		refaddr;
671 			int		moved = 0;
672 			Gotref		gref;
673 
674 			/*
675 			 * If the section this relocation is against has been
676 			 * discarded (-zignore), then discard (skip) the
677 			 * relocation itself.
678 			 */
679 			if ((arsp->rel_isdesc->is_flags & FLG_IS_DISCARD) &&
680 			    ((arsp->rel_flags &
681 			    (FLG_REL_GOT | FLG_REL_BSS |
682 			    FLG_REL_PLT | FLG_REL_NOINFO)) == 0)) {
683 				DBG_CALL(Dbg_reloc_discard(ofl->ofl_lml,
684 				    M_MACH, arsp));
685 				continue;
686 			}
687 
688 			/*
689 			 * We determine what the 'got reference'
690 			 * model (if required) is at this point.  This
691 			 * needs to be done before tls_fixup() since
692 			 * it may 'transition' our instructions.
693 			 *
694 			 * The got table entries have already been assigned,
695 			 * and we bind to those initial entries.
696 			 */
697 			if (arsp->rel_flags & FLG_REL_DTLS)
698 				gref = GOT_REF_TLSGD;
699 			else if (arsp->rel_flags & FLG_REL_MTLS)
700 				gref = GOT_REF_TLSLD;
701 			else if (arsp->rel_flags & FLG_REL_STLS)
702 				gref = GOT_REF_TLSIE;
703 			else
704 				gref = GOT_REF_GENERIC;
705 
706 			/*
707 			 * Perform any required TLS fixups.
708 			 */
709 			if (arsp->rel_flags & FLG_REL_TLSFIX) {
710 				Fixupret	ret;
711 
712 				if ((ret = tls_fixups(ofl, arsp)) == FIX_ERROR)
713 					return (S_ERROR);
714 				if (ret == FIX_DONE)
715 					continue;
716 			}
717 
718 			/*
719 			 * If this is a relocation against a move table, or
720 			 * expanded move table, adjust the relocation entries.
721 			 */
722 			if (arsp->rel_move)
723 				ld_adj_movereloc(ofl, arsp);
724 
725 			sdp = arsp->rel_sym;
726 			refaddr = arsp->rel_roffset +
727 			    (Off)_elf_getxoff(arsp->rel_isdesc->is_indata);
728 
729 			if ((arsp->rel_flags & FLG_REL_CLVAL) ||
730 			    (arsp->rel_flags & FLG_REL_GOTCL))
731 				value = 0;
732 			else if (ELF_ST_TYPE(sdp->sd_sym->st_info) ==
733 			    STT_SECTION) {
734 				Sym_desc	*sym;
735 
736 				/*
737 				 * The value for a symbol pointing to a SECTION
738 				 * is based off of that sections position.
739 				 */
740 				if ((sdp->sd_isc->is_flags & FLG_IS_RELUPD) &&
741 				    /* LINTED */
742 				    (sym = ld_am_I_partial(arsp,
743 				    arsp->rel_raddend))) {
744 					/*
745 					 * The symbol was moved, so adjust
746 					 * the value relative to the new
747 					 * section.
748 					 */
749 					value = sym->sd_sym->st_value;
750 					moved = 1;
751 
752 					/*
753 					 * The original raddend covers the
754 					 * displacement from the section start
755 					 * to the desired address. The value
756 					 * computed above gets us from the
757 					 * section start to the start of the
758 					 * symbol range. Adjust the old raddend
759 					 * to remove the offset from section
760 					 * start to symbol start, leaving the
761 					 * displacement within the range of
762 					 * the symbol.
763 					 */
764 					arsp->rel_raddend -=
765 					    sym->sd_osym->st_value;
766 				} else {
767 					value = _elf_getxoff(
768 					    sdp->sd_isc->is_indata);
769 					if (sdp->sd_isc->is_shdr->sh_flags &
770 					    SHF_ALLOC)
771 						value +=
772 						    sdp->sd_isc->is_osdesc->
773 						    os_shdr->sh_addr;
774 				}
775 				if (sdp->sd_isc->is_shdr->sh_flags & SHF_TLS)
776 					value -= ofl->ofl_tlsphdr->p_vaddr;
777 
778 			} else if (IS_SIZE(arsp->rel_rtype)) {
779 				/*
780 				 * Size relocations require the symbols size.
781 				 */
782 				value = sdp->sd_sym->st_size;
783 			} else {
784 				/*
785 				 * Else the value is the symbols value.
786 				 */
787 				value = sdp->sd_sym->st_value;
788 			}
789 
790 			/*
791 			 * Relocation against the GLOBAL_OFFSET_TABLE.
792 			 */
793 			if (arsp->rel_flags & FLG_REL_GOT)
794 				arsp->rel_osdesc = ofl->ofl_osgot;
795 
796 			/*
797 			 * If loadable and not producing a relocatable object
798 			 * add the sections virtual address to the reference
799 			 * address.
800 			 */
801 			if ((arsp->rel_flags & FLG_REL_LOAD) &&
802 			    ((flags & FLG_OF_RELOBJ) == 0))
803 				refaddr += arsp->rel_isdesc->is_osdesc->
804 				    os_shdr->sh_addr;
805 
806 			/*
807 			 * If this entry has a PLT assigned to it, it's
808 			 * value is actually the address of the PLT (and
809 			 * not the address of the function).
810 			 */
811 			if (IS_PLT(arsp->rel_rtype)) {
812 				if (sdp->sd_aux && sdp->sd_aux->sa_PLTndx)
813 					value = ld_calc_plt_addr(sdp, ofl);
814 			}
815 
816 			/*
817 			 * Add relocations addend to value.  Add extra
818 			 * relocation addend if needed.
819 			 *
820 			 * Note: for GOT relative relocations on amd64
821 			 *	 we discard the addend.  It was relevant
822 			 *	 to the reference - not to the data item
823 			 *	 being referenced (ie: that -4 thing).
824 			 */
825 			if ((arsp->rel_flags & FLG_REL_GOT) == 0)
826 				value += arsp->rel_raddend;
827 
828 			/*
829 			 * Determine whether the value needs further adjustment.
830 			 * Filter through the attributes of the relocation to
831 			 * determine what adjustment is required.  Note, many
832 			 * of the following cases are only applicable when a
833 			 * .got is present.  As a .got is not generated when a
834 			 * relocatable object is being built, any adjustments
835 			 * that require a .got need to be skipped.
836 			 */
837 			if ((arsp->rel_flags & FLG_REL_GOT) &&
838 			    ((flags & FLG_OF_RELOBJ) == 0)) {
839 				Xword		R1addr;
840 				uintptr_t	R2addr;
841 				Word		gotndx;
842 				Gotndx		*gnp;
843 
844 				/*
845 				 * Perform relocation against GOT table.  Since
846 				 * this doesn't fit exactly into a relocation
847 				 * we place the appropriate byte in the GOT
848 				 * directly
849 				 *
850 				 * Calculate offset into GOT at which to apply
851 				 * the relocation.
852 				 */
853 				gnp = ld_find_got_ndx(sdp->sd_GOTndxs, gref,
854 				    ofl, arsp);
855 				assert(gnp);
856 
857 				if (arsp->rel_rtype == R_AMD64_DTPOFF64)
858 					gotndx = gnp->gn_gotndx + 1;
859 				else
860 					gotndx = gnp->gn_gotndx;
861 
862 				R1addr = (Xword)(gotndx * M_GOT_ENTSIZE);
863 
864 				/*
865 				 * Add the GOTs data's offset.
866 				 */
867 				R2addr = R1addr + (uintptr_t)
868 				    arsp->rel_osdesc->os_outdata->d_buf;
869 
870 				DBG_CALL(Dbg_reloc_doact(ofl->ofl_lml,
871 				    ELF_DBG_LD, M_MACH, SHT_RELA,
872 				    arsp->rel_rtype, R1addr, value,
873 				    arsp->rel_sname, arsp->rel_osdesc));
874 
875 				/*
876 				 * And do it.
877 				 */
878 				if (ofl->ofl_flags1 & FLG_OF1_ENCDIFF)
879 					*(Xword *)R2addr =
880 					    ld_bswap_Xword(value);
881 				else
882 					*(Xword *)R2addr = value;
883 				continue;
884 
885 			} else if (IS_GOT_BASED(arsp->rel_rtype) &&
886 			    ((flags & FLG_OF_RELOBJ) == 0)) {
887 				value -= ofl->ofl_osgot->os_shdr->sh_addr;
888 
889 			} else if (IS_GOTPCREL(arsp->rel_rtype) &&
890 			    ((flags & FLG_OF_RELOBJ) == 0)) {
891 				Gotndx *gnp;
892 
893 				/*
894 				 * Calculation:
895 				 *	G + GOT + A - P
896 				 */
897 				gnp = ld_find_got_ndx(sdp->sd_GOTndxs,
898 				    gref, ofl, arsp);
899 				assert(gnp);
900 				value = (Xword)(ofl->ofl_osgot->os_shdr->
901 				    sh_addr) + ((Xword)gnp->gn_gotndx *
902 				    M_GOT_ENTSIZE) + arsp->rel_raddend -
903 				    refaddr;
904 
905 			} else if (IS_GOT_PC(arsp->rel_rtype) &&
906 			    ((flags & FLG_OF_RELOBJ) == 0)) {
907 				value = (Xword)(ofl->ofl_osgot->os_shdr->
908 				    sh_addr) - refaddr + arsp->rel_raddend;
909 
910 			} else if ((IS_PC_RELATIVE(arsp->rel_rtype)) &&
911 			    (((flags & FLG_OF_RELOBJ) == 0) ||
912 			    (arsp->rel_osdesc == sdp->sd_isc->is_osdesc))) {
913 				value -= refaddr;
914 
915 			} else if (IS_TLS_INS(arsp->rel_rtype) &&
916 			    IS_GOT_RELATIVE(arsp->rel_rtype) &&
917 			    ((flags & FLG_OF_RELOBJ) == 0)) {
918 				Gotndx	*gnp;
919 
920 				gnp = ld_find_got_ndx(sdp->sd_GOTndxs, gref,
921 				    ofl, arsp);
922 				assert(gnp);
923 				value = (Xword)gnp->gn_gotndx * M_GOT_ENTSIZE;
924 
925 			} else if (IS_GOT_RELATIVE(arsp->rel_rtype) &&
926 			    ((flags & FLG_OF_RELOBJ) == 0)) {
927 				Gotndx *gnp;
928 
929 				gnp = ld_find_got_ndx(sdp->sd_GOTndxs,
930 				    gref, ofl, arsp);
931 				assert(gnp);
932 				value = (Xword)gnp->gn_gotndx * M_GOT_ENTSIZE;
933 
934 			} else if ((arsp->rel_flags & FLG_REL_STLS) &&
935 			    ((flags & FLG_OF_RELOBJ) == 0)) {
936 				Xword	tlsstatsize;
937 
938 				/*
939 				 * This is the LE TLS reference model.  Static
940 				 * offset is hard-coded.
941 				 */
942 				tlsstatsize =
943 				    S_ROUND(ofl->ofl_tlsphdr->p_memsz,
944 				    M_TLSSTATALIGN);
945 				value = tlsstatsize - value;
946 
947 				/*
948 				 * Since this code is fixed up, it assumes a
949 				 * negative offset that can be added to the
950 				 * thread pointer.
951 				 */
952 				if (arsp->rel_rtype == R_AMD64_TPOFF32)
953 					value = -value;
954 			}
955 
956 			if (arsp->rel_isdesc->is_file)
957 				ifl_name = arsp->rel_isdesc->is_file->ifl_name;
958 			else
959 				ifl_name = MSG_INTL(MSG_STR_NULL);
960 
961 			/*
962 			 * Make sure we have data to relocate.  Compiler and
963 			 * assembler developers have been known to generate
964 			 * relocations against invalid sections (normally .bss),
965 			 * so for their benefit give them sufficient information
966 			 * to help analyze the problem.  End users should never
967 			 * see this.
968 			 */
969 			if (arsp->rel_isdesc->is_indata->d_buf == 0) {
970 				Conv_inv_buf_t inv_buf;
971 
972 				eprintf(ofl->ofl_lml, ERR_FATAL,
973 				    MSG_INTL(MSG_REL_EMPTYSEC),
974 				    conv_reloc_amd64_type(arsp->rel_rtype,
975 				    0, &inv_buf), ifl_name,
976 				    demangle(arsp->rel_sname),
977 				    EC_WORD(arsp->rel_isdesc->is_scnndx),
978 				    arsp->rel_isdesc->is_name);
979 				return (S_ERROR);
980 			}
981 
982 			/*
983 			 * Get the address of the data item we need to modify.
984 			 */
985 			addr = (uchar_t *)((uintptr_t)arsp->rel_roffset +
986 			    (uintptr_t)_elf_getxoff(arsp->rel_isdesc->
987 			    is_indata));
988 
989 			DBG_CALL(Dbg_reloc_doact(ofl->ofl_lml, ELF_DBG_LD,
990 			    M_MACH, SHT_RELA, arsp->rel_rtype, EC_NATPTR(addr),
991 			    value, arsp->rel_sname, arsp->rel_osdesc));
992 			addr += (uintptr_t)arsp->rel_osdesc->os_outdata->d_buf;
993 
994 			if ((((uintptr_t)addr - (uintptr_t)ofl->ofl_nehdr) >
995 			    ofl->ofl_size) || (arsp->rel_roffset >
996 			    arsp->rel_osdesc->os_shdr->sh_size)) {
997 				int		class;
998 				Conv_inv_buf_t inv_buf;
999 
1000 				if (((uintptr_t)addr -
1001 				    (uintptr_t)ofl->ofl_nehdr) > ofl->ofl_size)
1002 					class = ERR_FATAL;
1003 				else
1004 					class = ERR_WARNING;
1005 
1006 				eprintf(ofl->ofl_lml, class,
1007 				    MSG_INTL(MSG_REL_INVALOFFSET),
1008 				    conv_reloc_amd64_type(arsp->rel_rtype,
1009 				    0, &inv_buf), ifl_name,
1010 				    EC_WORD(arsp->rel_isdesc->is_scnndx),
1011 				    arsp->rel_isdesc->is_name,
1012 				    demangle(arsp->rel_sname),
1013 				    EC_ADDR((uintptr_t)addr -
1014 				    (uintptr_t)ofl->ofl_nehdr));
1015 
1016 				if (class == ERR_FATAL) {
1017 					return_code = S_ERROR;
1018 					continue;
1019 				}
1020 			}
1021 
1022 			/*
1023 			 * The relocation is additive.  Ignore the previous
1024 			 * symbol value if this local partial symbol is
1025 			 * expanded.
1026 			 */
1027 			if (moved)
1028 				value -= *addr;
1029 
1030 			/*
1031 			 * If '-z noreloc' is specified - skip the do_reloc_ld
1032 			 * stage.
1033 			 */
1034 			if (OFL_DO_RELOC(ofl)) {
1035 				/*
1036 				 * If this is a PROGBITS section and the
1037 				 * running linker has a different byte order
1038 				 * than the target host, tell do_reloc_ld()
1039 				 * to swap bytes.
1040 				 */
1041 				if (do_reloc_ld((uchar_t)arsp->rel_rtype,
1042 				    addr, &value, arsp->rel_sname, ifl_name,
1043 				    OFL_SWAP_RELOC_DATA(ofl, arsp),
1044 				    ofl->ofl_lml) == 0)
1045 					return_code = S_ERROR;
1046 			}
1047 		}
1048 	}
1049 	return (return_code);
1050 }
1051 
1052 static uintptr_t
1053 ld_add_outrel(Word flags, Rel_desc *rsp, Ofl_desc *ofl)
1054 {
1055 	Rel_desc	*orsp;
1056 	Rel_cache	*rcp;
1057 	Sym_desc	*sdp = rsp->rel_sym;
1058 	static size_t	nextsize = 0;
1059 
1060 	/*
1061 	 * Static executables *do not* want any relocations against them.
1062 	 * Since our engine still creates relocations against a WEAK UNDEFINED
1063 	 * symbol in a static executable, it's best to disable them here
1064 	 * instead of through out the relocation code.
1065 	 */
1066 	if ((ofl->ofl_flags & (FLG_OF_STATIC | FLG_OF_EXEC)) ==
1067 	    (FLG_OF_STATIC | FLG_OF_EXEC))
1068 		return (1);
1069 
1070 	/*
1071 	 * Obtain the new available relocation cache entry.
1072 	 */
1073 	if ((rcp = ld_add_rel_cache(ofl, &ofl->ofl_outrels, &nextsize,
1074 	    REL_LOIDESCNO, REL_HOIDESCNO)) == (Rel_cache *)S_ERROR)
1075 		return (S_ERROR);
1076 
1077 	orsp = rcp->rc_free;
1078 
1079 	/*
1080 	 * If we are adding a output relocation against a section
1081 	 * symbol (non-RELATIVE) then mark that section.  These sections
1082 	 * will be added to the .dynsym symbol table.
1083 	 */
1084 	if (sdp && (rsp->rel_rtype != M_R_RELATIVE) &&
1085 	    ((flags & FLG_REL_SCNNDX) ||
1086 	    (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION))) {
1087 
1088 		/*
1089 		 * If this is a COMMON symbol - no output section
1090 		 * exists yet - (it's created as part of sym_validate()).
1091 		 * So - we mark here that when it's created it should
1092 		 * be tagged with the FLG_OS_OUTREL flag.
1093 		 */
1094 		if ((sdp->sd_flags & FLG_SY_SPECSEC) &&
1095 		    (sdp->sd_sym->st_shndx == SHN_COMMON)) {
1096 			if (ELF_ST_TYPE(sdp->sd_sym->st_info) != STT_TLS)
1097 				ofl->ofl_flags1 |= FLG_OF1_BSSOREL;
1098 			else
1099 				ofl->ofl_flags1 |= FLG_OF1_TLSOREL;
1100 		} else {
1101 			Os_desc	*osp = sdp->sd_isc->is_osdesc;
1102 
1103 			if (osp && ((osp->os_flags & FLG_OS_OUTREL) == 0)) {
1104 				ofl->ofl_dynshdrcnt++;
1105 				osp->os_flags |= FLG_OS_OUTREL;
1106 			}
1107 		}
1108 	}
1109 
1110 	*orsp = *rsp;
1111 	orsp->rel_flags |= flags;
1112 
1113 	rcp->rc_free++;
1114 	ofl->ofl_outrelscnt++;
1115 
1116 	if (flags & FLG_REL_GOT)
1117 		ofl->ofl_relocgotsz += (Xword)sizeof (Rela);
1118 	else if (flags & FLG_REL_PLT)
1119 		ofl->ofl_relocpltsz += (Xword)sizeof (Rela);
1120 	else if (flags & FLG_REL_BSS)
1121 		ofl->ofl_relocbsssz += (Xword)sizeof (Rela);
1122 	else if (flags & FLG_REL_NOINFO)
1123 		ofl->ofl_relocrelsz += (Xword)sizeof (Rela);
1124 	else
1125 		orsp->rel_osdesc->os_szoutrels += (Xword)sizeof (Rela);
1126 
1127 	if (orsp->rel_rtype == M_R_RELATIVE)
1128 		ofl->ofl_relocrelcnt++;
1129 
1130 	/*
1131 	 * We don't perform sorting on PLT relocations because
1132 	 * they have already been assigned a PLT index and if we
1133 	 * were to sort them we would have to re-assign the plt indexes.
1134 	 */
1135 	if (!(flags & FLG_REL_PLT))
1136 		ofl->ofl_reloccnt++;
1137 
1138 	/*
1139 	 * Insure a GLOBAL_OFFSET_TABLE is generated if required.
1140 	 */
1141 	if (IS_GOT_REQUIRED(orsp->rel_rtype))
1142 		ofl->ofl_flags |= FLG_OF_BLDGOT;
1143 
1144 	/*
1145 	 * Identify and possibly warn of a displacement relocation.
1146 	 */
1147 	if (orsp->rel_flags & FLG_REL_DISP) {
1148 		ofl->ofl_dtflags_1 |= DF_1_DISPRELPND;
1149 
1150 		if (ofl->ofl_flags & FLG_OF_VERBOSE)
1151 			ld_disp_errmsg(MSG_INTL(MSG_REL_DISPREL4), orsp, ofl);
1152 	}
1153 	DBG_CALL(Dbg_reloc_ors_entry(ofl->ofl_lml, ELF_DBG_LD, SHT_RELA,
1154 	    M_MACH, orsp));
1155 	return (1);
1156 }
1157 
1158 /*
1159  * process relocation for a LOCAL symbol
1160  */
1161 static uintptr_t
1162 ld_reloc_local(Rel_desc * rsp, Ofl_desc * ofl)
1163 {
1164 	ofl_flag_t	flags = ofl->ofl_flags;
1165 	Sym_desc	*sdp = rsp->rel_sym;
1166 	Word		shndx = sdp->sd_sym->st_shndx;
1167 	Word		ortype = rsp->rel_rtype;
1168 
1169 	/*
1170 	 * if ((shared object) and (not pc relative relocation) and
1171 	 *    (not against ABS symbol))
1172 	 * then
1173 	 *	build R_AMD64_RELATIVE
1174 	 * fi
1175 	 */
1176 	if ((flags & FLG_OF_SHAROBJ) && (rsp->rel_flags & FLG_REL_LOAD) &&
1177 	    !(IS_PC_RELATIVE(rsp->rel_rtype)) && !(IS_SIZE(rsp->rel_rtype)) &&
1178 	    !(IS_GOT_BASED(rsp->rel_rtype)) &&
1179 	    !(rsp->rel_isdesc != NULL &&
1180 	    (rsp->rel_isdesc->is_shdr->sh_type == SHT_SUNW_dof)) &&
1181 	    (((sdp->sd_flags & FLG_SY_SPECSEC) == 0) ||
1182 	    (shndx != SHN_ABS) || (sdp->sd_aux && sdp->sd_aux->sa_symspec))) {
1183 
1184 		/*
1185 		 * R_AMD64_RELATIVE updates a 64bit address, if this
1186 		 * relocation isn't a 64bit binding then we can not
1187 		 * simplify it to a RELATIVE relocation.
1188 		 */
1189 		if (reloc_table[ortype].re_fsize != sizeof (Addr)) {
1190 			return (ld_add_outrel(0, rsp, ofl));
1191 		}
1192 
1193 		rsp->rel_rtype = R_AMD64_RELATIVE;
1194 		if (ld_add_outrel(FLG_REL_ADVAL, rsp, ofl) == S_ERROR)
1195 			return (S_ERROR);
1196 		rsp->rel_rtype = ortype;
1197 		return (1);
1198 	}
1199 
1200 	/*
1201 	 * If the relocation is against a 'non-allocatable' section
1202 	 * and we can not resolve it now - then give a warning
1203 	 * message.
1204 	 *
1205 	 * We can not resolve the symbol if either:
1206 	 *	a) it's undefined
1207 	 *	b) it's defined in a shared library and a
1208 	 *	   COPY relocation hasn't moved it to the executable
1209 	 *
1210 	 * Note: because we process all of the relocations against the
1211 	 *	text segment before any others - we know whether
1212 	 *	or not a copy relocation will be generated before
1213 	 *	we get here (see reloc_init()->reloc_segments()).
1214 	 */
1215 	if (!(rsp->rel_flags & FLG_REL_LOAD) &&
1216 	    ((shndx == SHN_UNDEF) ||
1217 	    ((sdp->sd_ref == REF_DYN_NEED) &&
1218 	    ((sdp->sd_flags & FLG_SY_MVTOCOMM) == 0)))) {
1219 		Conv_inv_buf_t inv_buf;
1220 
1221 		/*
1222 		 * If the relocation is against a SHT_SUNW_ANNOTATE
1223 		 * section - then silently ignore that the relocation
1224 		 * can not be resolved.
1225 		 */
1226 		if (rsp->rel_osdesc &&
1227 		    (rsp->rel_osdesc->os_shdr->sh_type == SHT_SUNW_ANNOTATE))
1228 			return (0);
1229 		(void) eprintf(ofl->ofl_lml, ERR_WARNING,
1230 		    MSG_INTL(MSG_REL_EXTERNSYM),
1231 		    conv_reloc_amd64_type(rsp->rel_rtype, 0, &inv_buf),
1232 		    rsp->rel_isdesc->is_file->ifl_name,
1233 		    demangle(rsp->rel_sname), rsp->rel_osdesc->os_name);
1234 		return (1);
1235 	}
1236 
1237 	/*
1238 	 * Perform relocation.
1239 	 */
1240 	return (ld_add_actrel(NULL, rsp, ofl));
1241 }
1242 
1243 
1244 static uintptr_t
1245 ld_reloc_TLS(Boolean local, Rel_desc * rsp, Ofl_desc * ofl)
1246 {
1247 	Word		rtype = rsp->rel_rtype;
1248 	Sym_desc	*sdp = rsp->rel_sym;
1249 	ofl_flag_t	flags = ofl->ofl_flags;
1250 	Gotndx		*gnp;
1251 
1252 	/*
1253 	 * If we're building an executable - use either the IE or LE access
1254 	 * model.  If we're building a shared object process any IE model.
1255 	 */
1256 	if ((flags & FLG_OF_EXEC) || (IS_TLS_IE(rtype))) {
1257 		/*
1258 		 * Set the DF_STATIC_TLS flag.
1259 		 */
1260 		ofl->ofl_dtflags |= DF_STATIC_TLS;
1261 
1262 		if (!local || ((flags & FLG_OF_EXEC) == 0)) {
1263 			/*
1264 			 * Assign a GOT entry for static TLS references.
1265 			 */
1266 			if ((gnp = ld_find_got_ndx(sdp->sd_GOTndxs,
1267 			    GOT_REF_TLSIE, ofl, rsp)) == NULL) {
1268 
1269 				if (ld_assign_got_TLS(local, rsp, ofl, sdp,
1270 				    gnp, GOT_REF_TLSIE, FLG_REL_STLS,
1271 				    rtype, R_AMD64_TPOFF64, 0) == S_ERROR)
1272 					return (S_ERROR);
1273 			}
1274 
1275 			/*
1276 			 * IE access model.
1277 			 */
1278 			if (IS_TLS_IE(rtype))
1279 				return (ld_add_actrel(FLG_REL_STLS, rsp, ofl));
1280 
1281 			/*
1282 			 * Fixups are required for other executable models.
1283 			 */
1284 			return (ld_add_actrel((FLG_REL_TLSFIX | FLG_REL_STLS),
1285 			    rsp, ofl));
1286 		}
1287 
1288 		/*
1289 		 * LE access model.
1290 		 */
1291 		if (IS_TLS_LE(rtype))
1292 			return (ld_add_actrel(FLG_REL_STLS, rsp, ofl));
1293 
1294 		return (ld_add_actrel((FLG_REL_TLSFIX | FLG_REL_STLS),
1295 		    rsp, ofl));
1296 	}
1297 
1298 	/*
1299 	 * Building a shared object.
1300 	 *
1301 	 * Assign a GOT entry for a dynamic TLS reference.
1302 	 */
1303 	if (IS_TLS_LD(rtype) && ((gnp = ld_find_got_ndx(sdp->sd_GOTndxs,
1304 	    GOT_REF_TLSLD, ofl, rsp)) == NULL)) {
1305 
1306 		if (ld_assign_got_TLS(local, rsp, ofl, sdp, gnp, GOT_REF_TLSLD,
1307 		    FLG_REL_MTLS, rtype, R_AMD64_DTPMOD64, NULL) == S_ERROR)
1308 			return (S_ERROR);
1309 
1310 	} else if (IS_TLS_GD(rtype) &&
1311 	    ((gnp = ld_find_got_ndx(sdp->sd_GOTndxs, GOT_REF_TLSGD,
1312 	    ofl, rsp)) == NULL)) {
1313 
1314 		if (ld_assign_got_TLS(local, rsp, ofl, sdp, gnp, GOT_REF_TLSGD,
1315 		    FLG_REL_DTLS, rtype, R_AMD64_DTPMOD64,
1316 		    R_AMD64_DTPOFF64) == S_ERROR)
1317 			return (S_ERROR);
1318 	}
1319 
1320 	if (IS_TLS_LD(rtype))
1321 		return (ld_add_actrel(FLG_REL_MTLS, rsp, ofl));
1322 
1323 	return (ld_add_actrel(FLG_REL_DTLS, rsp, ofl));
1324 }
1325 
1326 /* ARGSUSED5 */
1327 static uintptr_t
1328 ld_assign_got_ndx(Alist **alpp, Gotndx *pgnp, Gotref gref, Ofl_desc *ofl,
1329     Rel_desc *rsp, Sym_desc *sdp)
1330 {
1331 	Xword		raddend;
1332 	Gotndx		gn, *gnp;
1333 	Aliste		idx;
1334 	uint_t		gotents;
1335 
1336 	raddend = rsp->rel_raddend;
1337 	if (pgnp && (pgnp->gn_addend == raddend) && (pgnp->gn_gotref == gref))
1338 		return (1);
1339 
1340 	if ((gref == GOT_REF_TLSGD) || (gref == GOT_REF_TLSLD))
1341 		gotents = 2;
1342 	else
1343 		gotents = 1;
1344 
1345 	gn.gn_addend = raddend;
1346 	gn.gn_gotndx = ofl->ofl_gotcnt;
1347 	gn.gn_gotref = gref;
1348 
1349 	ofl->ofl_gotcnt += gotents;
1350 
1351 	if (gref == GOT_REF_TLSLD) {
1352 		if (ofl->ofl_tlsldgotndx == NULL) {
1353 			if ((gnp = libld_malloc(sizeof (Gotndx))) == NULL)
1354 				return (S_ERROR);
1355 			(void) memcpy(gnp, &gn, sizeof (Gotndx));
1356 			ofl->ofl_tlsldgotndx = gnp;
1357 		}
1358 		return (1);
1359 	}
1360 
1361 	idx = 0;
1362 	for (ALIST_TRAVERSE(*alpp, idx, gnp)) {
1363 		if (gnp->gn_addend > raddend)
1364 			break;
1365 	}
1366 
1367 	/*
1368 	 * GOT indexes are maintained on an Alist, where there is typically
1369 	 * only one index.  The usage of this list is to scan the list to find
1370 	 * an index, and then apply that index immediately to a relocation.
1371 	 * Thus there are no external references to these GOT index structures
1372 	 * that can be compromised by the Alist being reallocated.
1373 	 */
1374 	if (alist_insert(alpp, &gn, sizeof (Gotndx),
1375 	    AL_CNT_SDP_GOT, idx) == NULL)
1376 		return (S_ERROR);
1377 
1378 	return (1);
1379 }
1380 
1381 static void
1382 ld_assign_plt_ndx(Sym_desc * sdp, Ofl_desc *ofl)
1383 {
1384 	sdp->sd_aux->sa_PLTndx = 1 + ofl->ofl_pltcnt++;
1385 	sdp->sd_aux->sa_PLTGOTndx = ofl->ofl_gotcnt++;
1386 	ofl->ofl_flags |= FLG_OF_BLDGOT;
1387 }
1388 
1389 static uchar_t plt0_template[M_PLT_ENTSIZE] = {
1390 /* 0x00 PUSHQ GOT+8(%rip) */	0xff, 0x35, 0x00, 0x00, 0x00, 0x00,
1391 /* 0x06 JMP   *GOT+16(%rip) */	0xff, 0x25, 0x00, 0x00, 0x00, 0x00,
1392 /* 0x0c NOP */			0x90,
1393 /* 0x0d NOP */			0x90,
1394 /* 0x0e NOP */			0x90,
1395 /* 0x0f NOP */			0x90
1396 };
1397 
1398 /*
1399  * Initializes .got[0] with the _DYNAMIC symbol value.
1400  */
1401 static uintptr_t
1402 ld_fillin_gotplt(Ofl_desc *ofl)
1403 {
1404 	int	bswap = (ofl->ofl_flags1 & FLG_OF1_ENCDIFF) != 0;
1405 
1406 	if (ofl->ofl_osgot) {
1407 		Sym_desc	*sdp;
1408 
1409 		if ((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_DYNAMIC_U),
1410 		    SYM_NOHASH, 0, ofl)) != NULL) {
1411 			uchar_t	*genptr;
1412 
1413 			genptr = ((uchar_t *)ofl->ofl_osgot->os_outdata->d_buf +
1414 			    (M_GOT_XDYNAMIC * M_GOT_ENTSIZE));
1415 			/* LINTED */
1416 			*(Xword *)genptr = sdp->sd_sym->st_value;
1417 			if (bswap)
1418 				/* LINTED */
1419 				*(Xword *)genptr =
1420 				    /* LINTED */
1421 				    ld_bswap_Xword(*(Xword *)genptr);
1422 		}
1423 	}
1424 
1425 	/*
1426 	 * Fill in the reserved slot in the procedure linkage table the first
1427 	 * entry is:
1428 	 *	0x00 PUSHQ	GOT+8(%rip)	    # GOT[1]
1429 	 *	0x06 JMP	*GOT+16(%rip)	    # GOT[2]
1430 	 *	0x0c NOP
1431 	 *	0x0d NOP
1432 	 *	0x0e NOP
1433 	 *	0x0f NOP
1434 	 */
1435 	if ((ofl->ofl_flags & FLG_OF_DYNAMIC) && ofl->ofl_osplt) {
1436 		uchar_t	*pltent;
1437 		Xword	val1;
1438 
1439 		pltent = (uchar_t *)ofl->ofl_osplt->os_outdata->d_buf;
1440 		bcopy(plt0_template, pltent, sizeof (plt0_template));
1441 
1442 		/*
1443 		 * If '-z noreloc' is specified - skip the do_reloc_ld
1444 		 * stage.
1445 		 */
1446 		if (!OFL_DO_RELOC(ofl))
1447 			return (1);
1448 
1449 		/*
1450 		 * filin:
1451 		 *	PUSHQ GOT + 8(%rip)
1452 		 *
1453 		 * Note: 0x06 below represents the offset to the
1454 		 *	 next instruction - which is what %rip will
1455 		 *	 be pointing at.
1456 		 */
1457 		val1 = (ofl->ofl_osgot->os_shdr->sh_addr) +
1458 		    (M_GOT_XLINKMAP * M_GOT_ENTSIZE) -
1459 		    ofl->ofl_osplt->os_shdr->sh_addr - 0x06;
1460 
1461 		if (do_reloc_ld(R_AMD64_GOTPCREL, &pltent[0x02],
1462 		    &val1, MSG_ORIG(MSG_SYM_PLTENT),
1463 		    MSG_ORIG(MSG_SPECFIL_PLTENT), bswap, ofl->ofl_lml) == 0) {
1464 			eprintf(ofl->ofl_lml, ERR_FATAL,
1465 			    MSG_INTL(MSG_PLT_PLT0FAIL));
1466 			return (S_ERROR);
1467 		}
1468 
1469 		/*
1470 		 * filin:
1471 		 *  JMP	*GOT+16(%rip)
1472 		 */
1473 		val1 = (ofl->ofl_osgot->os_shdr->sh_addr) +
1474 		    (M_GOT_XRTLD * M_GOT_ENTSIZE) -
1475 		    ofl->ofl_osplt->os_shdr->sh_addr - 0x0c;
1476 
1477 		if (do_reloc_ld(R_AMD64_GOTPCREL, &pltent[0x08],
1478 		    &val1, MSG_ORIG(MSG_SYM_PLTENT),
1479 		    MSG_ORIG(MSG_SPECFIL_PLTENT), bswap, ofl->ofl_lml) == 0) {
1480 			eprintf(ofl->ofl_lml, ERR_FATAL,
1481 			    MSG_INTL(MSG_PLT_PLT0FAIL));
1482 			return (S_ERROR);
1483 		}
1484 	}
1485 
1486 	return (1);
1487 }
1488 
1489 
1490 
1491 /*
1492  * Template for generating "void (*)(void)" function
1493  */
1494 static const uchar_t nullfunc_tmpl[] = {	/* amd64 */
1495 /* 0x00 */	0x55,				/* pushq  %rbp */
1496 /* 0x01 */	0x48, 0x8b, 0xec,		/* movq   %rsp,%rbp */
1497 /* 0x04 */	0x48, 0x8b, 0xe5,		/* movq   %rbp,%rsp */
1498 /* 0x07 */	0x5d,				/* popq   %rbp */
1499 /* 0x08 */	0xc3				/* ret */
1500 };
1501 
1502 
1503 /*
1504  * Return the ld_targ definition for this target.
1505  */
1506 const Target *
1507 ld_targ_init_x86(void)
1508 {
1509 	static const Target _ld_targ = {
1510 		{			/* Target_mach */
1511 			M_MACH,			/* m_mach */
1512 			M_MACHPLUS,		/* m_machplus */
1513 			M_FLAGSPLUS,		/* m_flagsplus */
1514 			M_CLASS,		/* m_class */
1515 			M_DATA,			/* m_data */
1516 
1517 			M_SEGM_ALIGN,		/* m_segm_align */
1518 			M_SEGM_ORIGIN,		/* m_segm_origin */
1519 			M_SEGM_AORIGIN,		/* m_segm_aorigin */
1520 			M_DATASEG_PERM,		/* m_dataseg_perm */
1521 			M_WORD_ALIGN,		/* m_word_align */
1522 			MSG_ORIG(MSG_PTH_RTLD_AMD64), /* m_def_interp */
1523 
1524 			/* Relocation type codes */
1525 			M_R_ARRAYADDR,		/* m_r_arrayaddr */
1526 			M_R_COPY,		/* m_r_copy */
1527 			M_R_GLOB_DAT,		/* m_r_glob_dat */
1528 			M_R_JMP_SLOT,		/* m_r_jmp_slot */
1529 			M_R_NUM,		/* m_r_num */
1530 			M_R_NONE,		/* m_r_none */
1531 			M_R_RELATIVE,		/* m_r_relative */
1532 			M_R_REGISTER,		/* m_r_register */
1533 
1534 			/* Relocation related constants */
1535 			M_REL_DT_COUNT,		/* m_rel_dt_count */
1536 			M_REL_DT_ENT,		/* m_rel_dt_ent */
1537 			M_REL_DT_SIZE,		/* m_rel_dt_size */
1538 			M_REL_DT_TYPE,		/* m_rel_dt_type */
1539 			M_REL_SHT_TYPE,		/* m_rel_sht_type */
1540 
1541 			/* GOT related constants */
1542 			M_GOT_ENTSIZE,		/* m_got_entsize */
1543 			M_GOT_XNumber,		/* m_got_xnumber */
1544 
1545 			/* PLT related constants */
1546 			M_PLT_ALIGN,		/* m_plt_align */
1547 			M_PLT_ENTSIZE,		/* m_plt_entsize */
1548 			M_PLT_RESERVSZ,		/* m_plt_reservsz */
1549 			M_PLT_SHF_FLAGS,	/* m_plt_shf_flags */
1550 
1551 			/* Section type of .eh_frame/.eh_frame_hdr sections */
1552 			SHT_AMD64_UNWIND,	/* m_sht_unwind */
1553 
1554 			M_DT_REGISTER,		/* m_dt_register */
1555 		},
1556 		{			/* Target_machid */
1557 			M_ID_ARRAY,		/* id_array */
1558 			M_ID_BSS,		/* id_bss */
1559 			M_ID_CAP,		/* id_cap */
1560 			M_ID_DATA,		/* id_data */
1561 			M_ID_DYNAMIC,		/* id_dynamic */
1562 			M_ID_DYNSORT,		/* id_dynsort */
1563 			M_ID_DYNSTR,		/* id_dynstr */
1564 			M_ID_DYNSYM,		/* id_dynsym */
1565 			M_ID_DYNSYM_NDX,	/* id_dynsym_ndx */
1566 			M_ID_GOT,		/* id_got */
1567 			M_ID_UNKNOWN,		/* id_gotdata (unused) */
1568 			M_ID_HASH,		/* id_hash */
1569 			M_ID_INTERP,		/* id_interp */
1570 			M_ID_LBSS,		/* id_lbss */
1571 			M_ID_LDYNSYM,		/* id_ldynsym */
1572 			M_ID_NOTE,		/* id_note */
1573 			M_ID_NULL,		/* id_null */
1574 			M_ID_PLT,		/* id_plt */
1575 			M_ID_REL,		/* id_rel */
1576 			M_ID_STRTAB,		/* id_strtab */
1577 			M_ID_SYMINFO,		/* id_syminfo */
1578 			M_ID_SYMTAB,		/* id_symtab */
1579 			M_ID_SYMTAB_NDX,	/* id_symtab_ndx */
1580 			M_ID_TEXT,		/* id_text */
1581 			M_ID_TLS,		/* id_tls */
1582 			M_ID_TLSBSS,		/* id_tlsbss */
1583 			M_ID_UNKNOWN,		/* id_unknown */
1584 			M_ID_UNWIND,		/* id_unwind */
1585 			M_ID_UNWINDHDR,		/* id_unwindhdr */
1586 			M_ID_USER,		/* id_user */
1587 			M_ID_VERSION,		/* id_version */
1588 		},
1589 		{			/* Target_nullfunc */
1590 			nullfunc_tmpl,		/* nf_template */
1591 			sizeof (nullfunc_tmpl),	/* nf_size */
1592 		},
1593 		{			/* Target_machrel */
1594 			reloc_table,
1595 
1596 			ld_init_rel,		/* mr_init_rel */
1597 			ld_mach_eflags,		/* mr_mach_eflags */
1598 			ld_mach_make_dynamic,	/* mr_mach_make_dynamic */
1599 			ld_mach_update_odynamic, /* mr_mach_update_odynamic */
1600 			ld_calc_plt_addr,	/* mr_calc_plt_addr */
1601 			ld_perform_outreloc,	/* mr_perform_outreloc */
1602 			ld_do_activerelocs,	/* mr_do_activerelocs */
1603 			ld_add_outrel,		/* mr_add_outrel */
1604 			NULL,			/* mr_reloc_register */
1605 			ld_reloc_local,		/* mr_reloc_local */
1606 			NULL,			/* mr_reloc_GOTOP */
1607 			ld_reloc_TLS,		/* mr_reloc_TLS */
1608 			NULL,			/* mr_assign_got */
1609 			ld_find_got_ndx,	/* mr_find_got_ndx */
1610 			ld_calc_got_offset,	/* mr_calc_got_offset */
1611 			ld_assign_got_ndx,	/* mr_assign_got_ndx */
1612 			ld_assign_plt_ndx,	/* mr_assign_plt_ndx */
1613 			NULL,			/* mr_allocate_got */
1614 			ld_fillin_gotplt,	/* mr_fillin_gotplt */
1615 		},
1616 		{			/* Target_machsym */
1617 			NULL,			/* ms_reg_check */
1618 			NULL,			/* ms_mach_sym_typecheck */
1619 			NULL,			/* ms_is_regsym */
1620 			NULL,			/* ms_reg_find */
1621 			NULL			/* ms_reg_enter */
1622 		}
1623 	};
1624 
1625 	return (&_ld_targ);
1626 }
1627