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