xref: /titanic_50/usr/src/cmd/sgs/libld/common/machrel.intel.c (revision 753a6d457b330b1b29b2d3eefcd0831116ce950d)
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 				    arsp->rel_isdesc->is_name);
1057 				return (S_ERROR);
1058 			}
1059 
1060 			/*
1061 			 * Get the address of the data item we need to modify.
1062 			 */
1063 			addr = (uchar_t *)((uintptr_t)arsp->rel_roffset +
1064 			    (uintptr_t)_elf_getxoff(arsp->rel_isdesc->
1065 			    is_indata));
1066 
1067 			DBG_CALL(Dbg_reloc_doact(ofl->ofl_lml, ELF_DBG_LD,
1068 			    M_MACH, SHT_REL, arsp->rel_rtype, EC_NATPTR(addr),
1069 			    value, arsp->rel_sname, arsp->rel_osdesc));
1070 			addr += (uintptr_t)arsp->rel_osdesc->os_outdata->d_buf;
1071 
1072 			if ((((uintptr_t)addr - (uintptr_t)ofl->ofl_nehdr) >
1073 			    ofl->ofl_size) || (arsp->rel_roffset >
1074 			    arsp->rel_osdesc->os_shdr->sh_size)) {
1075 				Conv_inv_buf_t	inv_buf;
1076 				int		class;
1077 
1078 				if (((uintptr_t)addr -
1079 				    (uintptr_t)ofl->ofl_nehdr) > ofl->ofl_size)
1080 					class = ERR_FATAL;
1081 				else
1082 					class = ERR_WARNING;
1083 
1084 				eprintf(ofl->ofl_lml, class,
1085 				    MSG_INTL(MSG_REL_INVALOFFSET),
1086 				    conv_reloc_386_type(arsp->rel_rtype,
1087 				    0, &inv_buf),
1088 				    ifl_name, arsp->rel_isdesc->is_name,
1089 				    demangle(arsp->rel_sname),
1090 				    EC_ADDR((uintptr_t)addr -
1091 				    (uintptr_t)ofl->ofl_nehdr));
1092 
1093 				if (class == ERR_FATAL) {
1094 					return_code = S_ERROR;
1095 					continue;
1096 				}
1097 			}
1098 
1099 			/*
1100 			 * The relocation is additive.  Ignore the previous
1101 			 * symbol value if this local partial symbol is
1102 			 * expanded.
1103 			 */
1104 			if (moved)
1105 				value -= *addr;
1106 
1107 			/*
1108 			 * If we have a replacement value for the relocation
1109 			 * target, put it in place now.
1110 			 */
1111 			if (arsp->rel_flags & FLG_REL_NADDEND) {
1112 				Xword addend = arsp->rel_raddend;
1113 
1114 				if (ld_reloc_targval_set(ofl, arsp,
1115 				    addr, addend) == 0)
1116 					return (S_ERROR);
1117 			}
1118 
1119 			/*
1120 			 * If '-z noreloc' is specified - skip the do_reloc_ld
1121 			 * stage.
1122 			 */
1123 			if (OFL_DO_RELOC(ofl)) {
1124 				if (do_reloc_ld((uchar_t)arsp->rel_rtype, addr,
1125 				    &value, arsp->rel_sname, ifl_name,
1126 				    OFL_SWAP_RELOC_DATA(ofl, arsp),
1127 				    ofl->ofl_lml) == 0)
1128 					return_code = S_ERROR;
1129 			}
1130 		}
1131 	}
1132 	return (return_code);
1133 }
1134 
1135 /*
1136  * Add an output relocation record.
1137  */
1138 static uintptr_t
1139 ld_add_outrel(Word flags, Rel_desc *rsp, Ofl_desc *ofl)
1140 {
1141 	Rel_desc	*orsp;
1142 	Rel_cache	*rcp;
1143 	Sym_desc	*sdp = rsp->rel_sym;
1144 	static size_t	nextsize = 0;
1145 
1146 	/*
1147 	 * Static executables *do not* want any relocations against them.
1148 	 * Since our engine still creates relocations against a WEAK UNDEFINED
1149 	 * symbol in a static executable, it's best to disable them here
1150 	 * instead of through out the relocation code.
1151 	 */
1152 	if ((ofl->ofl_flags & (FLG_OF_STATIC | FLG_OF_EXEC)) ==
1153 	    (FLG_OF_STATIC | FLG_OF_EXEC))
1154 		return (1);
1155 
1156 	/*
1157 	 * Obtain the new available relocation cache entry.
1158 	 */
1159 	if ((rcp = ld_add_rel_cache(ofl, &ofl->ofl_outrels, &nextsize,
1160 	    REL_LOIDESCNO, REL_HOIDESCNO)) == (Rel_cache *)S_ERROR)
1161 		return (S_ERROR);
1162 
1163 	orsp = rcp->rc_free;
1164 
1165 	/*
1166 	 * If we are adding a output relocation against a section
1167 	 * symbol (non-RELATIVE) then mark that section.  These sections
1168 	 * will be added to the .dynsym symbol table.
1169 	 */
1170 	if (sdp && (rsp->rel_rtype != M_R_RELATIVE) &&
1171 	    ((flags & FLG_REL_SCNNDX) ||
1172 	    (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION))) {
1173 
1174 		/*
1175 		 * If this is a COMMON symbol - no output section
1176 		 * exists yet - (it's created as part of sym_validate()).
1177 		 * So - we mark here that when it's created it should
1178 		 * be tagged with the FLG_OS_OUTREL flag.
1179 		 */
1180 		if ((sdp->sd_flags & FLG_SY_SPECSEC) &&
1181 		    (sdp->sd_sym->st_shndx == SHN_COMMON)) {
1182 			if (ELF_ST_TYPE(sdp->sd_sym->st_info) != STT_TLS)
1183 				ofl->ofl_flags1 |= FLG_OF1_BSSOREL;
1184 			else
1185 				ofl->ofl_flags1 |= FLG_OF1_TLSOREL;
1186 		} else {
1187 			Os_desc	*osp = sdp->sd_isc->is_osdesc;
1188 
1189 			if (osp && ((osp->os_flags & FLG_OS_OUTREL) == 0)) {
1190 				ofl->ofl_dynshdrcnt++;
1191 				osp->os_flags |= FLG_OS_OUTREL;
1192 			}
1193 		}
1194 	}
1195 
1196 	*orsp = *rsp;
1197 	orsp->rel_flags |= flags;
1198 
1199 	rcp->rc_free++;
1200 	ofl->ofl_outrelscnt++;
1201 
1202 	if (flags & FLG_REL_GOT)
1203 		ofl->ofl_relocgotsz += (Xword)sizeof (Rel);
1204 	else if (flags & FLG_REL_PLT)
1205 		ofl->ofl_relocpltsz += (Xword)sizeof (Rel);
1206 	else if (flags & FLG_REL_BSS)
1207 		ofl->ofl_relocbsssz += (Xword)sizeof (Rel);
1208 	else if (flags & FLG_REL_NOINFO)
1209 		ofl->ofl_relocrelsz += (Xword)sizeof (Rel);
1210 	else
1211 		orsp->rel_osdesc->os_szoutrels += (Xword)sizeof (Rel);
1212 
1213 	if (orsp->rel_rtype == M_R_RELATIVE)
1214 		ofl->ofl_relocrelcnt++;
1215 
1216 	/*
1217 	 * We don't perform sorting on PLT relocations because
1218 	 * they have already been assigned a PLT index and if we
1219 	 * were to sort them we would have to re-assign the plt indexes.
1220 	 */
1221 	if (!(flags & FLG_REL_PLT))
1222 		ofl->ofl_reloccnt++;
1223 
1224 	/*
1225 	 * Insure a GLOBAL_OFFSET_TABLE is generated if required.
1226 	 */
1227 	if (IS_GOT_REQUIRED(orsp->rel_rtype))
1228 		ofl->ofl_flags |= FLG_OF_BLDGOT;
1229 
1230 	/*
1231 	 * Identify and possibly warn of a displacement relocation.
1232 	 */
1233 	if (orsp->rel_flags & FLG_REL_DISP) {
1234 		ofl->ofl_dtflags_1 |= DF_1_DISPRELPND;
1235 
1236 		if (ofl->ofl_flags & FLG_OF_VERBOSE)
1237 			ld_disp_errmsg(MSG_INTL(MSG_REL_DISPREL4), orsp, ofl);
1238 	}
1239 	DBG_CALL(Dbg_reloc_ors_entry(ofl->ofl_lml, ELF_DBG_LD, SHT_REL,
1240 	    M_MACH, orsp));
1241 	return (1);
1242 }
1243 
1244 /*
1245  * process relocation for a LOCAL symbol
1246  */
1247 static uintptr_t
1248 ld_reloc_local(Rel_desc * rsp, Ofl_desc * ofl)
1249 {
1250 	ofl_flag_t	flags = ofl->ofl_flags;
1251 	Sym_desc	*sdp = rsp->rel_sym;
1252 	Word		shndx = sdp->sd_sym->st_shndx;
1253 
1254 	/*
1255 	 * if ((shared object) and (not pc relative relocation) and
1256 	 *    (not against ABS symbol))
1257 	 * then
1258 	 *	build R_386_RELATIVE
1259 	 * fi
1260 	 */
1261 	if ((flags & FLG_OF_SHAROBJ) && (rsp->rel_flags & FLG_REL_LOAD) &&
1262 	    !(IS_PC_RELATIVE(rsp->rel_rtype)) && !(IS_SIZE(rsp->rel_rtype)) &&
1263 	    !(IS_GOT_BASED(rsp->rel_rtype)) &&
1264 	    !(rsp->rel_isdesc != NULL &&
1265 	    (rsp->rel_isdesc->is_shdr->sh_type == SHT_SUNW_dof)) &&
1266 	    (((sdp->sd_flags & FLG_SY_SPECSEC) == 0) ||
1267 	    (shndx != SHN_ABS) || (sdp->sd_aux && sdp->sd_aux->sa_symspec))) {
1268 		Word	ortype = rsp->rel_rtype;
1269 
1270 		rsp->rel_rtype = R_386_RELATIVE;
1271 		if (ld_add_outrel(NULL, rsp, ofl) == S_ERROR)
1272 			return (S_ERROR);
1273 		rsp->rel_rtype = ortype;
1274 	}
1275 
1276 	/*
1277 	 * If the relocation is against a 'non-allocatable' section
1278 	 * and we can not resolve it now - then give a warning
1279 	 * message.
1280 	 *
1281 	 * We can not resolve the symbol if either:
1282 	 *	a) it's undefined
1283 	 *	b) it's defined in a shared library and a
1284 	 *	   COPY relocation hasn't moved it to the executable
1285 	 *
1286 	 * Note: because we process all of the relocations against the
1287 	 *	text segment before any others - we know whether
1288 	 *	or not a copy relocation will be generated before
1289 	 *	we get here (see reloc_init()->reloc_segments()).
1290 	 */
1291 	if (!(rsp->rel_flags & FLG_REL_LOAD) &&
1292 	    ((shndx == SHN_UNDEF) ||
1293 	    ((sdp->sd_ref == REF_DYN_NEED) &&
1294 	    ((sdp->sd_flags & FLG_SY_MVTOCOMM) == 0)))) {
1295 		Conv_inv_buf_t inv_buf;
1296 
1297 		/*
1298 		 * If the relocation is against a SHT_SUNW_ANNOTATE
1299 		 * section - then silently ignore that the relocation
1300 		 * can not be resolved.
1301 		 */
1302 		if (rsp->rel_osdesc &&
1303 		    (rsp->rel_osdesc->os_shdr->sh_type == SHT_SUNW_ANNOTATE))
1304 			return (0);
1305 		eprintf(ofl->ofl_lml, ERR_WARNING, MSG_INTL(MSG_REL_EXTERNSYM),
1306 		    conv_reloc_386_type(rsp->rel_rtype, 0, &inv_buf),
1307 		    rsp->rel_isdesc->is_file->ifl_name,
1308 		    demangle(rsp->rel_sname), rsp->rel_osdesc->os_name);
1309 		return (1);
1310 	}
1311 
1312 	/*
1313 	 * Perform relocation.
1314 	 */
1315 	return (ld_add_actrel(NULL, rsp, ofl));
1316 }
1317 
1318 static uintptr_t
1319 ld_reloc_TLS(Boolean local, Rel_desc * rsp, Ofl_desc * ofl)
1320 {
1321 	Word		rtype = rsp->rel_rtype;
1322 	Sym_desc	*sdp = rsp->rel_sym;
1323 	ofl_flag_t	flags = ofl->ofl_flags;
1324 	Gotndx		*gnp;
1325 
1326 	/*
1327 	 * If we're building an executable - use either the IE or LE access
1328 	 * model.  If we're building a shared object process any IE model.
1329 	 */
1330 	if ((flags & FLG_OF_EXEC) || (IS_TLS_IE(rtype))) {
1331 		/*
1332 		 * Set the DF_STATIC_TLS flag.
1333 		 */
1334 		ofl->ofl_dtflags |= DF_STATIC_TLS;
1335 
1336 		if (!local || ((flags & FLG_OF_EXEC) == 0)) {
1337 			/*
1338 			 * Assign a GOT entry for static TLS references.
1339 			 */
1340 			if ((gnp = ld_find_got_ndx(sdp->sd_GOTndxs,
1341 			    GOT_REF_TLSIE, ofl, NULL)) == NULL) {
1342 
1343 				if (ld_assign_got_TLS(local, rsp, ofl, sdp,
1344 				    gnp, GOT_REF_TLSIE, FLG_REL_STLS,
1345 				    rtype, R_386_TLS_TPOFF, NULL) == S_ERROR)
1346 					return (S_ERROR);
1347 			}
1348 
1349 			/*
1350 			 * IE access model.
1351 			 */
1352 			if (IS_TLS_IE(rtype)) {
1353 				if (ld_add_actrel(FLG_REL_STLS,
1354 				    rsp, ofl) == S_ERROR)
1355 					return (S_ERROR);
1356 
1357 				/*
1358 				 * A non-pic shared object needs to adjust the
1359 				 * active relocation (indntpoff).
1360 				 */
1361 				if (((flags & FLG_OF_EXEC) == 0) &&
1362 				    (rtype == R_386_TLS_IE)) {
1363 					rsp->rel_rtype = R_386_RELATIVE;
1364 					return (ld_add_outrel(NULL, rsp, ofl));
1365 				}
1366 				return (1);
1367 			}
1368 
1369 			/*
1370 			 * Fixups are required for other executable models.
1371 			 */
1372 			return (ld_add_actrel((FLG_REL_TLSFIX | FLG_REL_STLS),
1373 			    rsp, ofl));
1374 		}
1375 
1376 		/*
1377 		 * LE access model.
1378 		 */
1379 		if (IS_TLS_LE(rtype) || (rtype == R_386_TLS_LDO_32))
1380 			return (ld_add_actrel(FLG_REL_STLS, rsp, ofl));
1381 
1382 		return (ld_add_actrel((FLG_REL_TLSFIX | FLG_REL_STLS),
1383 		    rsp, ofl));
1384 	}
1385 
1386 	/*
1387 	 * Building a shared object.
1388 	 *
1389 	 * Assign a GOT entry for a dynamic TLS reference.
1390 	 */
1391 	if (IS_TLS_LD(rtype) && ((gnp = ld_find_got_ndx(sdp->sd_GOTndxs,
1392 	    GOT_REF_TLSLD, ofl, NULL)) == NULL)) {
1393 
1394 		if (ld_assign_got_TLS(local, rsp, ofl, sdp, gnp, GOT_REF_TLSLD,
1395 		    FLG_REL_MTLS, rtype, R_386_TLS_DTPMOD32, NULL) == S_ERROR)
1396 			return (S_ERROR);
1397 
1398 	} else if (IS_TLS_GD(rtype) && ((gnp = ld_find_got_ndx(sdp->sd_GOTndxs,
1399 	    GOT_REF_TLSGD, ofl, NULL)) == NULL)) {
1400 
1401 		if (ld_assign_got_TLS(local, rsp, ofl, sdp, gnp, GOT_REF_TLSGD,
1402 		    FLG_REL_DTLS, rtype, R_386_TLS_DTPMOD32,
1403 		    R_386_TLS_DTPOFF32) == S_ERROR)
1404 			return (S_ERROR);
1405 	}
1406 
1407 	/*
1408 	 * For GD/LD TLS reference - TLS_{GD,LD}_CALL, this will eventually
1409 	 * cause a call to __tls_get_addr().  Convert this relocation to that
1410 	 * symbol now, and prepare for the PLT magic.
1411 	 */
1412 	if ((rtype == R_386_TLS_GD_PLT) || (rtype == R_386_TLS_LDM_PLT)) {
1413 		Sym_desc	*tlsgetsym;
1414 
1415 		if ((tlsgetsym = ld_sym_add_u(MSG_ORIG(MSG_SYM_TLSGETADDR_UU),
1416 		    ofl, MSG_STR_TLSREL)) == (Sym_desc *)S_ERROR)
1417 			return (S_ERROR);
1418 
1419 		rsp->rel_sym = tlsgetsym;
1420 		rsp->rel_sname = tlsgetsym->sd_name;
1421 		rsp->rel_rtype = R_386_PLT32;
1422 
1423 		if (ld_reloc_plt(rsp, ofl) == S_ERROR)
1424 			return (S_ERROR);
1425 
1426 		rsp->rel_sym = sdp;
1427 		rsp->rel_sname = sdp->sd_name;
1428 		rsp->rel_rtype = rtype;
1429 		return (1);
1430 	}
1431 
1432 	if (IS_TLS_LD(rtype))
1433 		return (ld_add_actrel(FLG_REL_MTLS, rsp, ofl));
1434 
1435 	return (ld_add_actrel(FLG_REL_DTLS, rsp, ofl));
1436 }
1437 
1438 /* ARGSUSED4 */
1439 static uintptr_t
1440 ld_assign_got_ndx(Alist **alpp, Gotndx *pgnp, Gotref gref, Ofl_desc *ofl,
1441     Rel_desc *rsp, Sym_desc *sdp)
1442 {
1443 	Gotndx	gn, *gnp;
1444 	uint_t	gotents;
1445 
1446 	if (pgnp)
1447 		return (1);
1448 
1449 	if ((gref == GOT_REF_TLSGD) || (gref == GOT_REF_TLSLD))
1450 		gotents = 2;
1451 	else
1452 		gotents = 1;
1453 
1454 	gn.gn_addend = 0;
1455 	gn.gn_gotndx = ofl->ofl_gotcnt;
1456 	gn.gn_gotref = gref;
1457 
1458 	ofl->ofl_gotcnt += gotents;
1459 
1460 	if (gref == GOT_REF_TLSLD) {
1461 		if (ofl->ofl_tlsldgotndx == NULL) {
1462 			if ((gnp = libld_malloc(sizeof (Gotndx))) == NULL)
1463 				return (S_ERROR);
1464 			(void) memcpy(gnp, &gn, sizeof (Gotndx));
1465 			ofl->ofl_tlsldgotndx = gnp;
1466 		}
1467 		return (1);
1468 	}
1469 
1470 	/*
1471 	 * GOT indexes are maintained on an Alist, where there is typically
1472 	 * only one index.  The usage of this list is to scan the list to find
1473 	 * an index, and then apply that index immediately to a relocation.
1474 	 * Thus there are no external references to these GOT index structures
1475 	 * that can be compromised by the Alist being reallocated.
1476 	 */
1477 	if (alist_append(alpp, &gn, sizeof (Gotndx), AL_CNT_SDP_GOT) == NULL)
1478 		return (S_ERROR);
1479 
1480 	return (1);
1481 }
1482 
1483 static void
1484 ld_assign_plt_ndx(Sym_desc * sdp, Ofl_desc *ofl)
1485 {
1486 	sdp->sd_aux->sa_PLTndx = 1 + ofl->ofl_pltcnt++;
1487 	sdp->sd_aux->sa_PLTGOTndx = ofl->ofl_gotcnt++;
1488 	ofl->ofl_flags |= FLG_OF_BLDGOT;
1489 }
1490 
1491 /*
1492  * Initializes .got[0] with the _DYNAMIC symbol value.
1493  */
1494 static uintptr_t
1495 ld_fillin_gotplt(Ofl_desc *ofl)
1496 {
1497 	ofl_flag_t	flags = ofl->ofl_flags;
1498 	int		bswap = (ofl->ofl_flags1 & FLG_OF1_ENCDIFF) != 0;
1499 
1500 	if (ofl->ofl_osgot) {
1501 		Sym_desc	*sdp;
1502 
1503 		if ((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_DYNAMIC_U),
1504 		    SYM_NOHASH, 0, ofl)) != NULL) {
1505 			uchar_t	*genptr;
1506 
1507 			genptr = ((uchar_t *)ofl->ofl_osgot->os_outdata->d_buf +
1508 			    (M_GOT_XDYNAMIC * M_GOT_ENTSIZE));
1509 			/* LINTED */
1510 			*(Word *)genptr = (Word)sdp->sd_sym->st_value;
1511 			if (bswap)
1512 				/* LINTED */
1513 				*(Word *)genptr =
1514 				    /* LINTED */
1515 				    ld_bswap_Word(*(Word *)genptr);
1516 		}
1517 	}
1518 
1519 	/*
1520 	 * Fill in the reserved slot in the procedure linkage table the first
1521 	 * entry is:
1522 	 *  if (building a.out) {
1523 	 *	PUSHL	got[1]		    # the address of the link map entry
1524 	 *	JMP *	got[2]		    # the address of rtbinder
1525 	 *  } else {
1526 	 *	PUSHL	got[1]@GOT(%ebx)    # the address of the link map entry
1527 	 *	JMP *	got[2]@GOT(%ebx)    # the address of rtbinder
1528 	 *  }
1529 	 */
1530 	if ((flags & FLG_OF_DYNAMIC) && ofl->ofl_osplt) {
1531 		uchar_t *pltent;
1532 
1533 		pltent = (uchar_t *)ofl->ofl_osplt->os_outdata->d_buf;
1534 		if (!(flags & FLG_OF_SHAROBJ)) {
1535 			pltent[0] = M_SPECIAL_INST;
1536 			pltent[1] = M_PUSHL_DISP;
1537 			pltent += 2;
1538 			/* LINTED */
1539 			*(Word *)pltent = (Word)(ofl->ofl_osgot->os_shdr->
1540 			    sh_addr + M_GOT_XLINKMAP * M_GOT_ENTSIZE);
1541 			if (bswap)
1542 				/* LINTED */
1543 				*(Word *)pltent =
1544 				    /* LINTED */
1545 				    ld_bswap_Word(*(Word *)pltent);
1546 			pltent += 4;
1547 			pltent[0] = M_SPECIAL_INST;
1548 			pltent[1] = M_JMP_DISP_IND;
1549 			pltent += 2;
1550 			/* LINTED */
1551 			*(Word *)pltent = (Word)(ofl->ofl_osgot->os_shdr->
1552 			    sh_addr + M_GOT_XRTLD * M_GOT_ENTSIZE);
1553 			if (bswap)
1554 				/* LINTED */
1555 				*(Word *)pltent =
1556 				    /* LINTED */
1557 				    ld_bswap_Word(*(Word *)pltent);
1558 		} else {
1559 			pltent[0] = M_SPECIAL_INST;
1560 			pltent[1] = M_PUSHL_REG_DISP;
1561 			pltent += 2;
1562 			/* LINTED */
1563 			*(Word *)pltent = (Word)(M_GOT_XLINKMAP *
1564 			    M_GOT_ENTSIZE);
1565 			if (bswap)
1566 				/* LINTED */
1567 				*(Word *)pltent =
1568 				    /* LINTED */
1569 				    ld_bswap_Word(*(Word *)pltent);
1570 			pltent += 4;
1571 			pltent[0] = M_SPECIAL_INST;
1572 			pltent[1] = M_JMP_REG_DISP_IND;
1573 			pltent += 2;
1574 			/* LINTED */
1575 			*(Word *)pltent = (Word)(M_GOT_XRTLD *
1576 			    M_GOT_ENTSIZE);
1577 			if (bswap)
1578 				/* LINTED */
1579 				*(Word *)pltent =
1580 				    /* LINTED */
1581 				    ld_bswap_Word(*(Word *)pltent);
1582 		}
1583 	}
1584 	return (1);
1585 }
1586 
1587 
1588 
1589 /*
1590  * Template for generating "void (*)(void)" function
1591  */
1592 static const uchar_t nullfunc_tmpl[] = {	/* IA32 */
1593 /* 0x00 */	0xc3				/* ret */
1594 };
1595 
1596 
1597 
1598 /*
1599  * Return the ld_targ definition for this target.
1600  */
1601 const Target *
1602 ld_targ_init_x86(void)
1603 {
1604 	static const Target _ld_targ = {
1605 		{			/* Target_mach */
1606 			M_MACH,			/* m_mach */
1607 			M_MACHPLUS,		/* m_machplus */
1608 			M_FLAGSPLUS,		/* m_flagsplus */
1609 			M_CLASS,		/* m_class */
1610 			M_DATA,			/* m_data */
1611 
1612 			M_SEGM_ALIGN,		/* m_segm_align */
1613 			M_SEGM_ORIGIN,		/* m_segm_origin */
1614 			M_SEGM_AORIGIN,		/* m_segm_aorigin */
1615 			M_DATASEG_PERM,		/* m_dataseg_perm */
1616 			M_WORD_ALIGN,		/* m_word_align */
1617 			MSG_ORIG(MSG_PTH_RTLD),	/* m_def_interp */
1618 
1619 			/* Relocation type codes */
1620 			M_R_ARRAYADDR,		/* m_r_arrayaddr */
1621 			M_R_COPY,		/* m_r_copy */
1622 			M_R_GLOB_DAT,		/* m_r_glob_dat */
1623 			M_R_JMP_SLOT,		/* m_r_jmp_slot */
1624 			M_R_NUM,		/* m_r_num */
1625 			M_R_NONE,		/* m_r_none */
1626 			M_R_RELATIVE,		/* m_r_relative */
1627 			M_R_REGISTER,		/* m_r_register */
1628 
1629 			/* Relocation related constants */
1630 			M_REL_DT_COUNT,		/* m_rel_dt_count */
1631 			M_REL_DT_ENT,		/* m_rel_dt_ent */
1632 			M_REL_DT_SIZE,		/* m_rel_dt_size */
1633 			M_REL_DT_TYPE,		/* m_rel_dt_type */
1634 			M_REL_SHT_TYPE,		/* m_rel_sht_type */
1635 
1636 			/* GOT related constants */
1637 			M_GOT_ENTSIZE,		/* m_got_entsize */
1638 			M_GOT_XNumber,		/* m_got_xnumber */
1639 
1640 			/* PLT related constants */
1641 			M_PLT_ALIGN,		/* m_plt_align */
1642 			M_PLT_ENTSIZE,		/* m_plt_entsize */
1643 			M_PLT_RESERVSZ,		/* m_plt_reservsz */
1644 			M_PLT_SHF_FLAGS,	/* m_plt_shf_flags */
1645 
1646 			/* Section type of .eh_frame/.eh_frame_hdr sections */
1647 			SHT_PROGBITS,		/* m_sht_unwind */
1648 
1649 			M_DT_REGISTER,		/* m_dt_register */
1650 		},
1651 		{			/* Target_machid */
1652 			M_ID_ARRAY,		/* id_array */
1653 			M_ID_BSS,		/* id_bss */
1654 			M_ID_CAP,		/* id_cap */
1655 			M_ID_DATA,		/* id_data */
1656 			M_ID_DYNAMIC,		/* id_dynamic */
1657 			M_ID_DYNSORT,		/* id_dynsort */
1658 			M_ID_DYNSTR,		/* id_dynstr */
1659 			M_ID_DYNSYM,		/* id_dynsym */
1660 			M_ID_DYNSYM_NDX,	/* id_dynsym_ndx */
1661 			M_ID_GOT,		/* id_got */
1662 			M_ID_UNKNOWN,		/* id_gotdata (unused) */
1663 			M_ID_HASH,		/* id_hash */
1664 			M_ID_INTERP,		/* id_interp */
1665 			M_ID_LBSS,		/* id_lbss */
1666 			M_ID_LDYNSYM,		/* id_ldynsym */
1667 			M_ID_NOTE,		/* id_note */
1668 			M_ID_NULL,		/* id_null */
1669 			M_ID_PLT,		/* id_plt */
1670 			M_ID_REL,		/* id_rel */
1671 			M_ID_STRTAB,		/* id_strtab */
1672 			M_ID_SYMINFO,		/* id_syminfo */
1673 			M_ID_SYMTAB,		/* id_symtab */
1674 			M_ID_SYMTAB_NDX,	/* id_symtab_ndx */
1675 			M_ID_TEXT,		/* id_text */
1676 			M_ID_TLS,		/* id_tls */
1677 			M_ID_TLSBSS,		/* id_tlsbss */
1678 			M_ID_UNKNOWN,		/* id_unknown */
1679 			M_ID_UNWIND,		/* id_unwind */
1680 			M_ID_UNWINDHDR,		/* id_unwindhdr */
1681 			M_ID_USER,		/* id_user */
1682 			M_ID_VERSION,		/* id_version */
1683 		},
1684 		{			/* Target_nullfunc */
1685 			nullfunc_tmpl,		/* nf_template */
1686 			sizeof (nullfunc_tmpl),	/* nf_size */
1687 		},
1688 		{			/* Target_machrel */
1689 			reloc_table,
1690 
1691 			ld_init_rel,		/* mr_init_rel */
1692 			ld_mach_eflags,		/* mr_mach_eflags */
1693 			ld_mach_make_dynamic,	/* mr_mach_make_dynamic */
1694 			ld_mach_update_odynamic, /* mr_mach_update_odynamic */
1695 			ld_calc_plt_addr,	/* mr_calc_plt_addr */
1696 			ld_perform_outreloc,	/* mr_perform_outreloc */
1697 			ld_do_activerelocs,	/* mr_do_activerelocs */
1698 			ld_add_outrel,		/* mr_add_outrel */
1699 			NULL,			/* mr_reloc_register */
1700 			ld_reloc_local,		/* mr_reloc_local */
1701 			NULL,			/* mr_reloc_GOTOP */
1702 			ld_reloc_TLS,		/* mr_reloc_TLS */
1703 			NULL,			/* mr_assign_got */
1704 			ld_find_got_ndx,	/* mr_find_got_ndx */
1705 			ld_calc_got_offset,	/* mr_calc_got_offset */
1706 			ld_assign_got_ndx,	/* mr_assign_got_ndx */
1707 			ld_assign_plt_ndx,	/* mr_assign_plt_ndx */
1708 			NULL,			/* mr_allocate_got */
1709 			ld_fillin_gotplt,	/* mr_fillin_gotplt */
1710 		},
1711 		{			/* Target_machsym */
1712 			NULL,			/* ms_reg_check */
1713 			NULL,			/* ms_mach_sym_typecheck */
1714 			NULL,			/* ms_is_regsym */
1715 			NULL,			/* ms_reg_find */
1716 			NULL			/* ms_reg_enter */
1717 		}
1718 	};
1719 
1720 	return (&_ld_targ);
1721 }
1722