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