xref: /titanic_41/usr/src/cmd/sgs/libld/common/machrel.amd.c (revision de6f998e6d02b8c2b9fd7169f88e293848d5760f)
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 /* Forward declarations */
42 static Gotndx	*ld_find_gotndx(List *, Gotref, Ofl_desc *, Rel_desc *);
43 static Xword	ld_calc_got_offset(Rel_desc *, Ofl_desc *);
44 
45 
46 static Word
47 ld_init_rel(Rel_desc *reld, void *reloc)
48 {
49 	Rela *	rel = (Rela *)reloc;
50 
51 	/* LINTED */
52 	reld->rel_rtype = (Word)ELF_R_TYPE(rel->r_info, M_MACH);
53 	reld->rel_roffset = rel->r_offset;
54 	reld->rel_raddend = rel->r_addend;
55 	reld->rel_typedata = 0;
56 
57 	reld->rel_flags |= FLG_REL_RELA;
58 
59 	return ((Word)ELF_R_SYM(rel->r_info));
60 }
61 
62 static void
63 ld_mach_eflags(Ehdr *ehdr, Ofl_desc *ofl)
64 {
65 	ofl->ofl_dehdr->e_flags |= ehdr->e_flags;
66 }
67 
68 static void
69 ld_mach_make_dynamic(Ofl_desc *ofl, size_t *cnt)
70 {
71 	if (!(ofl->ofl_flags & FLG_OF_RELOBJ)) {
72 		/*
73 		 * Create this entry if we are going to create a PLT table.
74 		 */
75 		if (ofl->ofl_pltcnt)
76 			(*cnt)++;		/* DT_PLTGOT */
77 	}
78 }
79 
80 static void
81 ld_mach_update_odynamic(Ofl_desc *ofl, Dyn **dyn)
82 {
83 	if (((ofl->ofl_flags & FLG_OF_RELOBJ) == 0) && ofl->ofl_pltcnt) {
84 		(*dyn)->d_tag = DT_PLTGOT;
85 		if (ofl->ofl_osgot)
86 			(*dyn)->d_un.d_ptr = ofl->ofl_osgot->os_shdr->sh_addr;
87 		else
88 			(*dyn)->d_un.d_ptr = 0;
89 		(*dyn)++;
90 	}
91 }
92 
93 static Xword
94 ld_calc_plt_addr(Sym_desc *sdp, Ofl_desc *ofl)
95 {
96 	Xword	value;
97 
98 	value = (Xword)(ofl->ofl_osplt->os_shdr->sh_addr) +
99 	    M_PLT_RESERVSZ + ((sdp->sd_aux->sa_PLTndx - 1) * M_PLT_ENTSIZE);
100 	return (value);
101 }
102 
103 /*
104  *  Build a single plt entry - code is:
105  *	JMP	*name1@GOTPCREL(%rip)
106  *	PUSHL	$index
107  *	JMP	.PLT0
108  */
109 static uchar_t pltn_entry[M_PLT_ENTSIZE] = {
110 /* 0x00 jmpq *name1@GOTPCREL(%rip) */	0xff, 0x25, 0x00, 0x00, 0x00, 0x00,
111 /* 0x06 pushq $index */			0x68, 0x00, 0x00, 0x00, 0x00,
112 /* 0x0b jmpq  .plt0(%rip) */		0xe9, 0x00, 0x00, 0x00, 0x00
113 /* 0x10 */
114 };
115 
116 static uintptr_t
117 plt_entry(Ofl_desc * ofl, Sym_desc * sdp)
118 {
119 	uchar_t		*plt0, *pltent, *gotent;
120 	Sword		plt_off;
121 	Word		got_off;
122 	Xword		val1;
123 	int		bswap = (ofl->ofl_flags1 & FLG_OF1_ENCDIFF) != 0;
124 
125 	got_off = sdp->sd_aux->sa_PLTGOTndx * M_GOT_ENTSIZE;
126 	plt_off = M_PLT_RESERVSZ + ((sdp->sd_aux->sa_PLTndx - 1) *
127 	    M_PLT_ENTSIZE);
128 	plt0 = (uchar_t *)(ofl->ofl_osplt->os_outdata->d_buf);
129 	pltent = plt0 + plt_off;
130 	gotent = (uchar_t *)(ofl->ofl_osgot->os_outdata->d_buf) + got_off;
131 
132 	bcopy(pltn_entry, pltent, sizeof (pltn_entry));
133 	/*
134 	 * Fill in the got entry with the address of the next instruction.
135 	 */
136 	/* LINTED */
137 	*(Word *)gotent = ofl->ofl_osplt->os_shdr->sh_addr + plt_off +
138 	    M_PLT_INSSIZE;
139 	if (bswap)
140 		/* LINTED */
141 		*(Word *)gotent = ld_bswap_Word(*(Word *)gotent);
142 
143 	/*
144 	 * If '-z noreloc' is specified - skip the do_reloc_ld
145 	 * stage.
146 	 */
147 	if (!OFL_DO_RELOC(ofl))
148 		return (1);
149 
150 	/*
151 	 * patchup:
152 	 *	jmpq	*name1@gotpcrel(%rip)
153 	 *
154 	 * NOTE: 0x06 represents next instruction.
155 	 */
156 	val1 = (ofl->ofl_osgot->os_shdr->sh_addr + got_off) -
157 	    (ofl->ofl_osplt->os_shdr->sh_addr + plt_off) - 0x06;
158 
159 	if (do_reloc_ld(R_AMD64_GOTPCREL, &pltent[0x02],
160 	    &val1, MSG_ORIG(MSG_SYM_PLTENT),
161 	    MSG_ORIG(MSG_SPECFIL_PLTENT), bswap, ofl->ofl_lml) == 0) {
162 		eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_PLT_PLTNFAIL),
163 		    sdp->sd_aux->sa_PLTndx, demangle(sdp->sd_name));
164 		return (S_ERROR);
165 	}
166 
167 	/*
168 	 * patchup:
169 	 *	pushq	$pltndx
170 	 */
171 	val1 = (Xword)(sdp->sd_aux->sa_PLTndx - 1);
172 
173 	if (do_reloc_ld(R_AMD64_32, &pltent[0x07],
174 	    &val1, MSG_ORIG(MSG_SYM_PLTENT),
175 	    MSG_ORIG(MSG_SPECFIL_PLTENT), bswap, ofl->ofl_lml) == 0) {
176 		eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_PLT_PLTNFAIL),
177 		    sdp->sd_aux->sa_PLTndx, demangle(sdp->sd_name));
178 		return (S_ERROR);
179 	}
180 
181 	/*
182 	 * patchup:
183 	 *	jmpq	.plt0(%rip)
184 	 * NOTE: 0x10 represents next instruction. The rather complex
185 	 * series of casts is necessary to sign extend an offset into
186 	 * a 64-bit value while satisfying various compiler error
187 	 * checks.  Handle with care.
188 	 */
189 	val1 = (Xword)((intptr_t)((uintptr_t)plt0 -
190 	    (uintptr_t)(&pltent[0x10])));
191 
192 	if (do_reloc_ld(R_AMD64_PC32, &pltent[0x0c],
193 	    &val1, MSG_ORIG(MSG_SYM_PLTENT),
194 	    MSG_ORIG(MSG_SPECFIL_PLTENT), bswap, ofl->ofl_lml) == 0) {
195 		eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_PLT_PLTNFAIL),
196 		    sdp->sd_aux->sa_PLTndx, demangle(sdp->sd_name));
197 		return (S_ERROR);
198 	}
199 
200 	return (1);
201 }
202 
203 static uintptr_t
204 ld_perform_outreloc(Rel_desc * orsp, Ofl_desc * ofl)
205 {
206 	Os_desc *	relosp, * osp = 0;
207 	Word		ndx;
208 	Xword		roffset, value;
209 	Sxword		raddend;
210 	Rela		rea;
211 	char		*relbits;
212 	Sym_desc *	sdp, * psym = (Sym_desc *)0;
213 	int		sectmoved = 0;
214 
215 	raddend = orsp->rel_raddend;
216 	sdp = orsp->rel_sym;
217 
218 	/*
219 	 * If the section this relocation is against has been discarded
220 	 * (-zignore), then also discard (skip) the relocation itself.
221 	 */
222 	if (orsp->rel_isdesc && ((orsp->rel_flags &
223 	    (FLG_REL_GOT | FLG_REL_BSS | FLG_REL_PLT | FLG_REL_NOINFO)) == 0) &&
224 	    (orsp->rel_isdesc->is_flags & FLG_IS_DISCARD)) {
225 		DBG_CALL(Dbg_reloc_discard(ofl->ofl_lml, M_MACH, orsp));
226 		return (1);
227 	}
228 
229 	/*
230 	 * If this is a relocation against a move table, or expanded move
231 	 * table, adjust the relocation entries.
232 	 */
233 	if (orsp->rel_move)
234 		ld_adj_movereloc(ofl, orsp);
235 
236 	/*
237 	 * If this is a relocation against a section then we need to adjust the
238 	 * raddend field to compensate for the new position of the input section
239 	 * within the new output section.
240 	 */
241 	if (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION) {
242 		if (ofl->ofl_parsym.head &&
243 		    (sdp->sd_isc->is_flags & FLG_IS_RELUPD) &&
244 		    /* LINTED */
245 		    (psym = ld_am_I_partial(orsp, orsp->rel_raddend))) {
246 			DBG_CALL(Dbg_move_outsctadj(ofl->ofl_lml, psym));
247 			sectmoved = 1;
248 			if (ofl->ofl_flags & FLG_OF_RELOBJ)
249 				raddend = psym->sd_sym->st_value;
250 			else
251 				raddend = psym->sd_sym->st_value -
252 				    psym->sd_isc->is_osdesc->os_shdr->sh_addr;
253 			/* LINTED */
254 			raddend += (Off)_elf_getxoff(psym->sd_isc->is_indata);
255 			if (psym->sd_isc->is_shdr->sh_flags & SHF_ALLOC)
256 				raddend +=
257 				    psym->sd_isc->is_osdesc->os_shdr->sh_addr;
258 		} else {
259 			/* LINTED */
260 			raddend += (Off)_elf_getxoff(sdp->sd_isc->is_indata);
261 			if (sdp->sd_isc->is_shdr->sh_flags & SHF_ALLOC)
262 				raddend +=
263 				    sdp->sd_isc->is_osdesc->os_shdr->sh_addr;
264 		}
265 	}
266 
267 	value = sdp->sd_sym->st_value;
268 
269 	if (orsp->rel_flags & FLG_REL_GOT) {
270 		/*
271 		 * Note: for GOT relative relocations on amd64
272 		 *	 we discard the addend.  It was relevant
273 		 *	 to the reference - not to the data item
274 		 *	 being referenced (ie: that -4 thing).
275 		 */
276 		raddend = 0;
277 		osp = ofl->ofl_osgot;
278 		roffset = ld_calc_got_offset(orsp, ofl);
279 
280 	} else if (orsp->rel_flags & FLG_REL_PLT) {
281 		/*
282 		 * Note that relocations for PLT's actually
283 		 * cause a relocation againt the GOT.
284 		 */
285 		osp = ofl->ofl_osplt;
286 		roffset = (ofl->ofl_osgot->os_shdr->sh_addr) +
287 		    sdp->sd_aux->sa_PLTGOTndx * M_GOT_ENTSIZE;
288 		raddend = 0;
289 		if (plt_entry(ofl, sdp) == S_ERROR)
290 			return (S_ERROR);
291 
292 	} else if (orsp->rel_flags & FLG_REL_BSS) {
293 		/*
294 		 * This must be a R_AMD64_COPY.  For these set the roffset to
295 		 * point to the new symbols location.
296 		 */
297 		osp = ofl->ofl_isbss->is_osdesc;
298 		roffset = value;
299 
300 		/*
301 		 * The raddend doesn't mean anything in a R_SPARC_COPY
302 		 * relocation.  Null it out because it can confuse people.
303 		 */
304 		raddend = 0;
305 	} else {
306 		osp = orsp->rel_osdesc;
307 
308 		/*
309 		 * Calculate virtual offset of reference point; equals offset
310 		 * into section + vaddr of section for loadable sections, or
311 		 * offset plus section displacement for nonloadable sections.
312 		 */
313 		roffset = orsp->rel_roffset +
314 		    (Off)_elf_getxoff(orsp->rel_isdesc->is_indata);
315 		if (!(ofl->ofl_flags & FLG_OF_RELOBJ))
316 			roffset += orsp->rel_isdesc->is_osdesc->
317 			    os_shdr->sh_addr;
318 	}
319 
320 	if ((osp == 0) || ((relosp = osp->os_relosdesc) == 0))
321 		relosp = ofl->ofl_osrel;
322 
323 	/*
324 	 * Assign the symbols index for the output relocation.  If the
325 	 * relocation refers to a SECTION symbol then it's index is based upon
326 	 * the output sections symbols index.  Otherwise the index can be
327 	 * derived from the symbols index itself.
328 	 */
329 	if (orsp->rel_rtype == R_AMD64_RELATIVE)
330 		ndx = STN_UNDEF;
331 	else if ((orsp->rel_flags & FLG_REL_SCNNDX) ||
332 	    (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION)) {
333 		if (sectmoved == 0) {
334 			/*
335 			 * Check for a null input section. This can
336 			 * occur if this relocation references a symbol
337 			 * generated by sym_add_sym().
338 			 */
339 			if ((sdp->sd_isc != 0) &&
340 			    (sdp->sd_isc->is_osdesc != 0))
341 				ndx = sdp->sd_isc->is_osdesc->os_scnsymndx;
342 			else
343 				ndx = sdp->sd_shndx;
344 		} else
345 			ndx = ofl->ofl_parexpnndx;
346 	} else
347 		ndx = sdp->sd_symndx;
348 
349 	/*
350 	 * Add the symbols 'value' to the addend field.
351 	 */
352 	if (orsp->rel_flags & FLG_REL_ADVAL)
353 		raddend += value;
354 
355 	/*
356 	 * The addend field for R_AMD64_DTPMOD64 means nothing.  The addend
357 	 * is propagated in the corresponding R_AMD64_DTPOFF64 relocation.
358 	 */
359 	if (orsp->rel_rtype == R_AMD64_DTPMOD64)
360 		raddend = 0;
361 
362 	relbits = (char *)relosp->os_outdata->d_buf;
363 
364 	rea.r_info = ELF_R_INFO(ndx, orsp->rel_rtype);
365 	rea.r_offset = roffset;
366 	rea.r_addend = raddend;
367 	DBG_CALL(Dbg_reloc_out(ofl, ELF_DBG_LD, SHT_RELA, &rea, relosp->os_name,
368 	    orsp->rel_sname));
369 
370 	/*
371 	 * Assert we haven't walked off the end of our relocation table.
372 	 */
373 	assert(relosp->os_szoutrels <= relosp->os_shdr->sh_size);
374 
375 	(void) memcpy((relbits + relosp->os_szoutrels),
376 	    (char *)&rea, sizeof (Rela));
377 	relosp->os_szoutrels += (Xword)sizeof (Rela);
378 
379 	/*
380 	 * Determine if this relocation is against a non-writable, allocatable
381 	 * section.  If so we may need to provide a text relocation diagnostic.
382 	 * Note that relocations against the .plt (R_AMD64_JUMP_SLOT) actually
383 	 * result in modifications to the .got.
384 	 */
385 	if (orsp->rel_rtype == R_AMD64_JUMP_SLOT)
386 		osp = ofl->ofl_osgot;
387 
388 	ld_reloc_remain_entry(orsp, osp, ofl);
389 	return (1);
390 }
391 
392 /*
393  * amd64 Instructions for TLS processing
394  */
395 static uchar_t tlsinstr_gd_ie[] = {
396 	/*
397 	 *	0x00 movq %fs:0, %rax
398 	 */
399 	0x64, 0x48, 0x8b, 0x04, 0x25,
400 	0x00, 0x00, 0x00, 0x00,
401 	/*
402 	 *	0x09 addq x@gottpoff(%rip), %rax
403 	 */
404 	0x48, 0x03, 0x05, 0x00, 0x00,
405 	0x00, 0x00
406 };
407 
408 static uchar_t tlsinstr_gd_le[] = {
409 	/*
410 	 *	0x00 movq %fs:0, %rax
411 	 */
412 	0x64, 0x48, 0x8b, 0x04, 0x25,
413 	0x00, 0x00, 0x00, 0x00,
414 	/*
415 	 *	0x09 leaq x@gottpoff(%rip), %rax
416 	 */
417 	0x48, 0x8d, 0x80, 0x00, 0x00,
418 	0x00, 0x00
419 };
420 
421 static uchar_t tlsinstr_ld_le[] = {
422 	/*
423 	 * .byte 0x66
424 	 */
425 	0x66,
426 	/*
427 	 * .byte 0x66
428 	 */
429 	0x66,
430 	/*
431 	 * .byte 0x66
432 	 */
433 	0x66,
434 	/*
435 	 * movq %fs:0, %rax
436 	 */
437 	0x64, 0x48, 0x8b, 0x04, 0x25,
438 	0x00, 0x00, 0x00, 0x00
439 };
440 
441 
442 static Fixupret
443 tls_fixups(Ofl_desc *ofl, Rel_desc *arsp)
444 {
445 	Sym_desc	*sdp = arsp->rel_sym;
446 	Word		rtype = arsp->rel_rtype;
447 	uchar_t		*offset;
448 
449 	offset = (uchar_t *)((uintptr_t)arsp->rel_roffset +
450 	    (uintptr_t)_elf_getxoff(arsp->rel_isdesc->is_indata) +
451 	    (uintptr_t)arsp->rel_osdesc->os_outdata->d_buf);
452 
453 	if (sdp->sd_ref == REF_DYN_NEED) {
454 		/*
455 		 * IE reference model
456 		 */
457 		switch (rtype) {
458 		case R_AMD64_TLSGD:
459 			/*
460 			 *  GD -> IE
461 			 *
462 			 * Transition:
463 			 *	0x00 .byte 0x66
464 			 *	0x01 leaq x@tlsgd(%rip), %rdi
465 			 *	0x08 .word 0x6666
466 			 *	0x0a rex64
467 			 *	0x0b call __tls_get_addr@plt
468 			 *	0x10
469 			 * To:
470 			 *	0x00 movq %fs:0, %rax
471 			 *	0x09 addq x@gottpoff(%rip), %rax
472 			 *	0x10
473 			 */
474 			DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
475 			    R_AMD64_GOTTPOFF, arsp));
476 			arsp->rel_rtype = R_AMD64_GOTTPOFF;
477 			arsp->rel_roffset += 8;
478 			arsp->rel_raddend = (Sxword)-4;
479 
480 			/*
481 			 * Adjust 'offset' to beginning of instruction
482 			 * sequence.
483 			 */
484 			offset -= 4;
485 			(void) memcpy(offset, tlsinstr_gd_ie,
486 			    sizeof (tlsinstr_gd_ie));
487 			return (FIX_RELOC);
488 
489 		case R_AMD64_PLT32:
490 			/*
491 			 * Fixup done via the TLS_GD relocation.
492 			 */
493 			DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
494 			    R_AMD64_NONE, arsp));
495 			return (FIX_DONE);
496 		}
497 	}
498 
499 	/*
500 	 * LE reference model
501 	 */
502 	switch (rtype) {
503 	case R_AMD64_TLSGD:
504 		/*
505 		 * GD -> LE
506 		 *
507 		 * Transition:
508 		 *	0x00 .byte 0x66
509 		 *	0x01 leaq x@tlsgd(%rip), %rdi
510 		 *	0x08 .word 0x6666
511 		 *	0x0a rex64
512 		 *	0x0b call __tls_get_addr@plt
513 		 *	0x10
514 		 * To:
515 		 *	0x00 movq %fs:0, %rax
516 		 *	0x09 leaq x@tpoff(%rax), %rax
517 		 *	0x10
518 		 */
519 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
520 		    R_AMD64_TPOFF32, arsp));
521 		arsp->rel_rtype = R_AMD64_TPOFF32;
522 		arsp->rel_roffset += 8;
523 		arsp->rel_raddend = 0;
524 
525 		/*
526 		 * Adjust 'offset' to beginning of instruction sequence.
527 		 */
528 		offset -= 4;
529 		(void) memcpy(offset, tlsinstr_gd_le, sizeof (tlsinstr_gd_le));
530 		return (FIX_RELOC);
531 
532 	case R_AMD64_GOTTPOFF:
533 		/*
534 		 * IE -> LE
535 		 *
536 		 * Transition:
537 		 *	0x00 movq %fs:0, %rax
538 		 *	0x09 addq x@gottopoff(%rip), %rax
539 		 *	0x10
540 		 * To:
541 		 *	0x00 movq %fs:0, %rax
542 		 *	0x09 leaq x@tpoff(%rax), %rax
543 		 *	0x10
544 		 */
545 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
546 		    R_AMD64_TPOFF32, arsp));
547 		arsp->rel_rtype = R_AMD64_TPOFF32;
548 		arsp->rel_raddend = 0;
549 
550 		/*
551 		 * Adjust 'offset' to beginning of instruction sequence.
552 		 */
553 		offset -= 12;
554 
555 		/*
556 		 * Same code sequence used in the GD -> LE transition.
557 		 */
558 		(void) memcpy(offset, tlsinstr_gd_le, sizeof (tlsinstr_gd_le));
559 		return (FIX_RELOC);
560 
561 	case R_AMD64_TLSLD:
562 		/*
563 		 * LD -> LE
564 		 *
565 		 * Transition
566 		 *	0x00 leaq x1@tlsgd(%rip), %rdi
567 		 *	0x07 call __tls_get_addr@plt
568 		 *	0x0c
569 		 * To:
570 		 *	0x00 .byte 0x66
571 		 *	0x01 .byte 0x66
572 		 *	0x02 .byte 0x66
573 		 *	0x03 movq %fs:0, %rax
574 		 */
575 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
576 		    R_AMD64_NONE, arsp));
577 		offset -= 3;
578 		(void) memcpy(offset, tlsinstr_ld_le, sizeof (tlsinstr_ld_le));
579 		return (FIX_DONE);
580 
581 	case R_AMD64_DTPOFF32:
582 		/*
583 		 * LD->LE
584 		 *
585 		 * Transition:
586 		 *	0x00 leaq x1@dtpoff(%rax), %rcx
587 		 * To:
588 		 *	0x00 leaq x1@tpoff(%rax), %rcx
589 		 */
590 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
591 		    R_AMD64_TPOFF32, arsp));
592 		arsp->rel_rtype = R_AMD64_TPOFF32;
593 		arsp->rel_raddend = 0;
594 		return (FIX_RELOC);
595 	}
596 
597 	return (FIX_RELOC);
598 }
599 
600 static uintptr_t
601 ld_do_activerelocs(Ofl_desc *ofl)
602 {
603 	Rel_desc	*arsp;
604 	Rel_cache	*rcp;
605 	Listnode	*lnp;
606 	uintptr_t	return_code = 1;
607 	ofl_flag_t	flags = ofl->ofl_flags;
608 
609 	if (ofl->ofl_actrels.head)
610 		DBG_CALL(Dbg_reloc_doact_title(ofl->ofl_lml));
611 
612 	/*
613 	 * Process active relocations.
614 	 */
615 	for (LIST_TRAVERSE(&ofl->ofl_actrels, lnp, rcp)) {
616 		/* LINTED */
617 		for (arsp = (Rel_desc *)(rcp + 1);
618 		    arsp < rcp->rc_free; arsp++) {
619 			uchar_t		*addr;
620 			Xword 		value;
621 			Sym_desc	*sdp;
622 			const char	*ifl_name;
623 			Xword		refaddr;
624 			int		moved = 0;
625 			Gotref		gref;
626 
627 			/*
628 			 * If the section this relocation is against has been
629 			 * discarded (-zignore), then discard (skip) the
630 			 * relocation itself.
631 			 */
632 			if ((arsp->rel_isdesc->is_flags & FLG_IS_DISCARD) &&
633 			    ((arsp->rel_flags &
634 			    (FLG_REL_GOT | FLG_REL_BSS |
635 			    FLG_REL_PLT | FLG_REL_NOINFO)) == 0)) {
636 				DBG_CALL(Dbg_reloc_discard(ofl->ofl_lml,
637 				    M_MACH, arsp));
638 				continue;
639 			}
640 
641 			/*
642 			 * We determine what the 'got reference'
643 			 * model (if required) is at this point.  This
644 			 * needs to be done before tls_fixup() since
645 			 * it may 'transition' our instructions.
646 			 *
647 			 * The got table entries have already been assigned,
648 			 * and we bind to those initial entries.
649 			 */
650 			if (arsp->rel_flags & FLG_REL_DTLS)
651 				gref = GOT_REF_TLSGD;
652 			else if (arsp->rel_flags & FLG_REL_MTLS)
653 				gref = GOT_REF_TLSLD;
654 			else if (arsp->rel_flags & FLG_REL_STLS)
655 				gref = GOT_REF_TLSIE;
656 			else
657 				gref = GOT_REF_GENERIC;
658 
659 			/*
660 			 * Perform any required TLS fixups.
661 			 */
662 			if (arsp->rel_flags & FLG_REL_TLSFIX) {
663 				Fixupret	ret;
664 
665 				if ((ret = tls_fixups(ofl, arsp)) == FIX_ERROR)
666 					return (S_ERROR);
667 				if (ret == FIX_DONE)
668 					continue;
669 			}
670 
671 			/*
672 			 * If this is a relocation against a move table, or
673 			 * expanded move table, adjust the relocation entries.
674 			 */
675 			if (arsp->rel_move)
676 				ld_adj_movereloc(ofl, arsp);
677 
678 			sdp = arsp->rel_sym;
679 			refaddr = arsp->rel_roffset +
680 			    (Off)_elf_getxoff(arsp->rel_isdesc->is_indata);
681 
682 			if ((arsp->rel_flags & FLG_REL_CLVAL) ||
683 			    (arsp->rel_flags & FLG_REL_GOTCL))
684 				value = 0;
685 			else if (ELF_ST_TYPE(sdp->sd_sym->st_info) ==
686 			    STT_SECTION) {
687 				Sym_desc	*sym;
688 
689 				/*
690 				 * The value for a symbol pointing to a SECTION
691 				 * is based off of that sections position.
692 				 */
693 				if ((sdp->sd_isc->is_flags & FLG_IS_RELUPD) &&
694 				    /* LINTED */
695 				    (sym = ld_am_I_partial(arsp,
696 				    arsp->rel_raddend))) {
697 					/*
698 					 * The symbol was moved, so adjust
699 					 * the value relative to the new
700 					 * section.
701 					 */
702 					value = sym->sd_sym->st_value;
703 					moved = 1;
704 
705 					/*
706 					 * The original raddend covers the
707 					 * displacement from the section start
708 					 * to the desired address. The value
709 					 * computed above gets us from the
710 					 * section start to the start of the
711 					 * symbol range. Adjust the old raddend
712 					 * to remove the offset from section
713 					 * start to symbol start, leaving the
714 					 * displacement within the range of
715 					 * the symbol.
716 					 */
717 					arsp->rel_raddend -=
718 					    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 += arsp->rel_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 			/* Section type of .eh_frame/.eh_frame_hdr sections */
1598 			SHT_AMD64_UNWIND,	/* m_sht_unwind */
1599 
1600 			M_DT_REGISTER,		/* m_dt_register */
1601 		},
1602 		{			/* Target_machid */
1603 			M_ID_ARRAY,		/* id_array */
1604 			M_ID_BSS,		/* id_bss */
1605 			M_ID_CAP,		/* id_cap */
1606 			M_ID_DATA,		/* id_data */
1607 			M_ID_DYNAMIC,		/* id_dynamic */
1608 			M_ID_DYNSORT,		/* id_dynsort */
1609 			M_ID_DYNSTR,		/* id_dynstr */
1610 			M_ID_DYNSYM,		/* id_dynsym */
1611 			M_ID_DYNSYM_NDX,	/* id_dynsym_ndx */
1612 			M_ID_GOT,		/* id_got */
1613 			M_ID_UNKNOWN,		/* id_gotdata (unused) */
1614 			M_ID_HASH,		/* id_hash */
1615 			M_ID_INTERP,		/* id_interp */
1616 			M_ID_LBSS,		/* id_lbss */
1617 			M_ID_LDYNSYM,		/* id_ldynsym */
1618 			M_ID_NOTE,		/* id_note */
1619 			M_ID_NULL,		/* id_null */
1620 			M_ID_PLT,		/* id_plt */
1621 			M_ID_REL,		/* id_rel */
1622 			M_ID_STRTAB,		/* id_strtab */
1623 			M_ID_SYMINFO,		/* id_syminfo */
1624 			M_ID_SYMTAB,		/* id_symtab */
1625 			M_ID_SYMTAB_NDX,	/* id_symtab_ndx */
1626 			M_ID_TEXT,		/* id_text */
1627 			M_ID_TLS,		/* id_tls */
1628 			M_ID_TLSBSS,		/* id_tlsbss */
1629 			M_ID_UNKNOWN,		/* id_unknown */
1630 			M_ID_UNWIND,		/* id_unwind */
1631 			M_ID_UNWINDHDR,		/* id_unwindhdr */
1632 			M_ID_USER,		/* id_user */
1633 			M_ID_VERSION,		/* id_version */
1634 		},
1635 		{			/* Target_nullfunc */
1636 			nullfunc_tmpl,		/* nf_template */
1637 			sizeof (nullfunc_tmpl),	/* nf_size */
1638 		},
1639 		{			/* Target_machrel */
1640 			reloc_table,
1641 
1642 			ld_init_rel,		/* mr_init_rel */
1643 			ld_mach_eflags,		/* mr_mach_eflags */
1644 			ld_mach_make_dynamic,	/* mr_mach_make_dynamic */
1645 			ld_mach_update_odynamic, /* mr_mach_update_odynamic */
1646 			ld_calc_plt_addr,	/* mr_calc_plt_addr */
1647 			ld_perform_outreloc,	/* mr_perform_outreloc */
1648 			ld_do_activerelocs,	/* mr_do_activerelocs */
1649 			ld_add_outrel,		/* mr_add_outrel */
1650 			NULL,			/* mr_reloc_register */
1651 			ld_reloc_local,		/* mr_reloc_local */
1652 			NULL,			/* mr_reloc_GOTOP */
1653 			ld_reloc_TLS,		/* mr_reloc_TLS */
1654 			NULL,			/* mr_assign_got */
1655 			ld_find_gotndx,		/* mr_find_gotndx */
1656 			ld_calc_got_offset,	/* mr_calc_got_offset */
1657 			ld_assign_got_ndx,	/* mr_assign_got_ndx */
1658 			ld_assign_plt_ndx,	/* mr_assign_plt_ndx */
1659 			NULL,			/* mr_allocate_got */
1660 			ld_fillin_gotplt,	/* mr_fillin_gotplt */
1661 		},
1662 		{			/* Target_machsym */
1663 			NULL,			/* ms_reg_check */
1664 			NULL,			/* ms_mach_sym_typecheck */
1665 			NULL,			/* ms_is_regsym */
1666 			NULL,			/* ms_reg_find */
1667 			NULL			/* ms_reg_enter */
1668 		}
1669 	};
1670 
1671 	return (&_ld_targ);
1672 }
1673