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