xref: /titanic_44/usr/src/cmd/sgs/libld/common/machrel.intel.c (revision 726fad2a65f16c200a03969c29cb5c86c2d427db)
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)
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);
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 			eprintf(ofl->ofl_lml, ERR_FATAL,
645 			    MSG_INTL(MSG_REL_BADTLSINS),
646 			    conv_reloc_386_type(arsp->rel_rtype, 0, &inv_buf),
647 			    arsp->rel_isdesc->is_file->ifl_name,
648 			    ld_reloc_sym_name(arsp),
649 			    arsp->rel_isdesc->is_name,
650 			    EC_OFF(arsp->rel_roffset));
651 		}
652 		return (FIX_ERROR);
653 
654 	case R_386_TLS_IE:
655 		/*
656 		 * These transitions are a little different than the
657 		 * others, in that we could have multiple instructions
658 		 * pointed to by a single relocation.  Depending upon the
659 		 * instruction, we perform a different code transition.
660 		 *
661 		 * Here's the known transitions:
662 		 *  1) movl foo@indntpoff, %eax
663 		 *	0xa1, foo@indntpoff
664 		 *
665 		 *  2) movl foo@indntpoff, %eax
666 		 *	0x8b, 0x05 | (reg << 3), foo@gotntpoff
667 		 *
668 		 *  3) addl foo@indntpoff, %eax
669 		 *	0x03, 0x05 | (reg << 3), foo@gotntpoff
670 		 *
671 		 *  Transitions IE -> LE
672 		 *
673 		 *  1) movl $foo@ntpoff, %eax
674 		 *	0xb8, foo@ntpoff
675 		 *
676 		 *  2) movl $foo@ntpoff, %reg
677 		 *	0xc7, 0xc0 | reg, foo@ntpoff
678 		 *
679 		 *  3) addl $foo@ntpoff, %reg
680 		 *	0x81, 0xc0 | reg, foo@ntpoff
681 		 */
682 		arsp->rel_rtype = R_386_TLS_LE;
683 		offset--;
684 		if (offset[0] == 0xa1) {
685 			/* case 1 above */
686 			offset[0] = 0xb8;	/*  movl */
687 			return (FIX_RELOC);
688 		}
689 
690 		offset--;
691 		if (offset[0] == 0x8b) {
692 			/* case 2 above */
693 			r2 = (offset[1] & MODRM_MSK_RO) >> 3;
694 			offset[0] = 0xc7;	/* movl */
695 			offset[1] = 0xc0 | r2;
696 			return (FIX_RELOC);
697 		}
698 		if (offset[0] == 0x03) {
699 			/* case 3 above */
700 			r2 = (offset[1] & MODRM_MSK_RO) >> 3;
701 			offset[0] = 0x81;	/* addl */
702 			offset[1] = 0xc0 | r2;
703 			return (FIX_RELOC);
704 		}
705 		/*
706 		 * Unexpected instruction sequence - fatal error.
707 		 */
708 		{
709 			Conv_inv_buf_t	inv_buf;
710 
711 			eprintf(ofl->ofl_lml, ERR_FATAL,
712 			    MSG_INTL(MSG_REL_BADTLSINS),
713 			    conv_reloc_386_type(arsp->rel_rtype, 0, &inv_buf),
714 			    arsp->rel_isdesc->is_file->ifl_name,
715 			    ld_reloc_sym_name(arsp),
716 			    arsp->rel_isdesc->is_name,
717 			    EC_OFF(arsp->rel_roffset));
718 		}
719 		return (FIX_ERROR);
720 	}
721 	return (FIX_RELOC);
722 }
723 
724 static uintptr_t
725 ld_do_activerelocs(Ofl_desc *ofl)
726 {
727 	Rel_desc	*arsp;
728 	Rel_cachebuf	*rcbp;
729 	Aliste		idx;
730 	uintptr_t	return_code = 1;
731 	ofl_flag_t	flags = ofl->ofl_flags;
732 
733 	if (aplist_nitems(ofl->ofl_actrels.rc_list) != 0)
734 		DBG_CALL(Dbg_reloc_doact_title(ofl->ofl_lml));
735 
736 	/*
737 	 * Process active relocations.
738 	 */
739 	REL_CACHE_TRAVERSE(&ofl->ofl_actrels, idx, rcbp, arsp) {
740 		uchar_t		*addr;
741 		Xword 		value;
742 		Sym_desc	*sdp;
743 		const char	*ifl_name;
744 		Xword		refaddr;
745 		int		moved = 0;
746 		Gotref		gref;
747 		Os_desc		*osp;
748 
749 		/*
750 		 * If the section this relocation is against has been discarded
751 		 * (-zignore), then discard (skip) the relocation itself.
752 		 */
753 		if ((arsp->rel_isdesc->is_flags & FLG_IS_DISCARD) &&
754 		    ((arsp->rel_flags & (FLG_REL_GOT | FLG_REL_BSS |
755 		    FLG_REL_PLT | FLG_REL_NOINFO)) == 0)) {
756 			DBG_CALL(Dbg_reloc_discard(ofl->ofl_lml, M_MACH, arsp));
757 			continue;
758 		}
759 
760 		/*
761 		 * We determine what the 'got reference' model (if required)
762 		 * is at this point.  This needs to be done before tls_fixup()
763 		 * since it may 'transition' our instructions.
764 		 *
765 		 * The got table entries have already been assigned,
766 		 * and we bind to those initial entries.
767 		 */
768 		if (arsp->rel_flags & FLG_REL_DTLS)
769 			gref = GOT_REF_TLSGD;
770 		else if (arsp->rel_flags & FLG_REL_MTLS)
771 			gref = GOT_REF_TLSLD;
772 		else if (arsp->rel_flags & FLG_REL_STLS)
773 			gref = GOT_REF_TLSIE;
774 		else
775 			gref = GOT_REF_GENERIC;
776 
777 		/*
778 		 * Perform any required TLS fixups.
779 		 */
780 		if (arsp->rel_flags & FLG_REL_TLSFIX) {
781 			Fixupret	ret;
782 
783 			if ((ret = tls_fixups(ofl, arsp)) == FIX_ERROR)
784 				return (S_ERROR);
785 			if (ret == FIX_DONE)
786 				continue;
787 		}
788 
789 		/*
790 		 * If this is a relocation against a move table, or
791 		 * expanded move table, adjust the relocation entries.
792 		 */
793 		if (RELAUX_GET_MOVE(arsp))
794 			ld_adj_movereloc(ofl, arsp);
795 
796 		sdp = arsp->rel_sym;
797 		refaddr = arsp->rel_roffset +
798 		    (Off)_elf_getxoff(arsp->rel_isdesc->is_indata);
799 
800 		if (arsp->rel_flags & FLG_REL_CLVAL)
801 			value = 0;
802 		else if (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION) {
803 			/*
804 			 * The value for a symbol pointing to a SECTION
805 			 * is based off of that sections position.
806 			 */
807 			if (sdp->sd_isc->is_flags & FLG_IS_RELUPD) {
808 				Sym_desc	*sym;
809 				Xword		radd;
810 				uchar_t		*raddr = (uchar_t *)
811 				    arsp->rel_isdesc->is_indata->d_buf +
812 				    arsp->rel_roffset;
813 
814 				/*
815 				 * This is a REL platform. Hence, the second
816 				 * argument of ld_am_I_partial() is the value
817 				 * stored at the target address where the
818 				 * relocation is going to be applied.
819 				 */
820 				if (ld_reloc_targval_get(ofl, arsp, raddr,
821 				    &radd) == 0)
822 					return (S_ERROR);
823 				sym = ld_am_I_partial(arsp, radd);
824 				if (sym) {
825 					Sym	*osym = sym->sd_osym;
826 
827 					/*
828 					 * The symbol was moved, so adjust the
829 					 * value relative to the new section.
830 					 */
831 					value = sym->sd_sym->st_value;
832 					moved = 1;
833 
834 					/*
835 					 * The original raddend covers the
836 					 * displacement from the section start
837 					 * to the desired address. The value
838 					 * computed above gets us from the
839 					 * section start to the start of the
840 					 * symbol range. Adjust the old raddend
841 					 * to remove the offset from section
842 					 * start to symbol start, leaving the
843 					 * displacement within the range of
844 					 * the symbol.
845 					 */
846 					if (osym->st_value != 0) {
847 						radd -= osym->st_value;
848 						if (ld_reloc_targval_set(ofl,
849 						    arsp, raddr, radd) == 0)
850 							return (S_ERROR);
851 					}
852 				}
853 			}
854 			if (!moved) {
855 				value = _elf_getxoff(sdp->sd_isc->is_indata);
856 				if (sdp->sd_isc->is_shdr->sh_flags & SHF_ALLOC)
857 					value += sdp->sd_isc->
858 					    is_osdesc->os_shdr->sh_addr;
859 			}
860 			if (sdp->sd_isc->is_shdr->sh_flags & SHF_TLS)
861 				value -= ofl->ofl_tlsphdr->p_vaddr;
862 
863 		} else if (IS_SIZE(arsp->rel_rtype)) {
864 			/*
865 			 * Size relocations require the symbols size.
866 			 */
867 			value = sdp->sd_sym->st_size;
868 
869 		} else if ((sdp->sd_flags & FLG_SY_CAP) &&
870 		    sdp->sd_aux && sdp->sd_aux->sa_PLTndx) {
871 			/*
872 			 * If relocation is against a capabilities symbol, we
873 			 * need to jump to an associated PLT, so that at runtime
874 			 * ld.so.1 is involved to determine the best binding
875 			 * choice. Otherwise, the value is the symbols value.
876 			 */
877 			value = ld_calc_plt_addr(sdp, ofl);
878 
879 		} else
880 			value = sdp->sd_sym->st_value;
881 
882 		/*
883 		 * Relocation against the GLOBAL_OFFSET_TABLE.
884 		 */
885 		if ((arsp->rel_flags & FLG_REL_GOT) &&
886 		    !ld_reloc_set_aux_osdesc(ofl, arsp, ofl->ofl_osgot))
887 			return (S_ERROR);
888 		osp = RELAUX_GET_OSDESC(arsp);
889 
890 		/*
891 		 * If loadable and not producing a relocatable object add the
892 		 * sections virtual address to the reference address.
893 		 */
894 		if ((arsp->rel_flags & FLG_REL_LOAD) &&
895 		    ((flags & FLG_OF_RELOBJ) == 0))
896 			refaddr +=
897 			    arsp->rel_isdesc->is_osdesc->os_shdr->sh_addr;
898 
899 		/*
900 		 * If this entry has a PLT assigned to it, its value is actually
901 		 * the address of the PLT (and not the address of the function).
902 		 */
903 		if (IS_PLT(arsp->rel_rtype)) {
904 			if (sdp->sd_aux && sdp->sd_aux->sa_PLTndx)
905 				value = ld_calc_plt_addr(sdp, ofl);
906 		}
907 
908 		/*
909 		 * Determine whether the value needs further adjustment. Filter
910 		 * through the attributes of the relocation to determine what
911 		 * adjustment is required.  Note, many of the following cases
912 		 * are only applicable when a .got is present.  As a .got is
913 		 * not generated when a relocatable object is being built,
914 		 * any adjustments that require a .got need to be skipped.
915 		 */
916 		if ((arsp->rel_flags & FLG_REL_GOT) &&
917 		    ((flags & FLG_OF_RELOBJ) == 0)) {
918 			Xword		R1addr;
919 			uintptr_t	R2addr;
920 			Word		gotndx;
921 			Gotndx		*gnp;
922 
923 			/*
924 			 * Perform relocation against GOT table.  Since this
925 			 * doesn't fit exactly into a relocation we place the
926 			 * appropriate byte in the GOT directly
927 			 *
928 			 * Calculate offset into GOT at which to apply
929 			 * the relocation.
930 			 */
931 			gnp = ld_find_got_ndx(sdp->sd_GOTndxs, gref, ofl, NULL);
932 			assert(gnp);
933 
934 			if (arsp->rel_rtype == R_386_TLS_DTPOFF32)
935 				gotndx = gnp->gn_gotndx + 1;
936 			else
937 				gotndx = gnp->gn_gotndx;
938 
939 			R1addr = (Xword)(gotndx * M_GOT_ENTSIZE);
940 
941 			/*
942 			 * Add the GOTs data's offset.
943 			 */
944 			R2addr = R1addr + (uintptr_t)osp->os_outdata->d_buf;
945 
946 			DBG_CALL(Dbg_reloc_doact(ofl->ofl_lml, ELF_DBG_LD_ACT,
947 			    M_MACH, SHT_REL, arsp, R1addr, value,
948 			    ld_reloc_sym_name));
949 
950 			/*
951 			 * And do it.
952 			 */
953 			if (ofl->ofl_flags1 & FLG_OF1_ENCDIFF)
954 				*(Xword *)R2addr = ld_bswap_Xword(value);
955 			else
956 				*(Xword *)R2addr = value;
957 			continue;
958 
959 		} else if (IS_GOT_BASED(arsp->rel_rtype) &&
960 		    ((flags & FLG_OF_RELOBJ) == 0)) {
961 			value -= ofl->ofl_osgot->os_shdr->sh_addr;
962 
963 		} else if (IS_GOT_PC(arsp->rel_rtype) &&
964 		    ((flags & FLG_OF_RELOBJ) == 0)) {
965 			value = (Xword)(ofl->ofl_osgot->os_shdr->sh_addr) -
966 			    refaddr;
967 
968 		} else if ((IS_PC_RELATIVE(arsp->rel_rtype)) &&
969 		    (((flags & FLG_OF_RELOBJ) == 0) ||
970 		    (osp == sdp->sd_isc->is_osdesc))) {
971 			value -= refaddr;
972 
973 		} else if (IS_TLS_INS(arsp->rel_rtype) &&
974 		    IS_GOT_RELATIVE(arsp->rel_rtype) &&
975 		    ((flags & FLG_OF_RELOBJ) == 0)) {
976 			Gotndx	*gnp;
977 
978 			gnp = ld_find_got_ndx(sdp->sd_GOTndxs, gref, ofl, NULL);
979 			assert(gnp);
980 			value = (Xword)gnp->gn_gotndx * M_GOT_ENTSIZE;
981 			if (arsp->rel_rtype == R_386_TLS_IE) {
982 				value += ofl->ofl_osgot->os_shdr->sh_addr;
983 			}
984 
985 		} else if (IS_GOT_RELATIVE(arsp->rel_rtype) &&
986 		    ((flags & FLG_OF_RELOBJ) == 0)) {
987 			Gotndx *gnp;
988 
989 			gnp = ld_find_got_ndx(sdp->sd_GOTndxs,
990 			    GOT_REF_GENERIC, ofl, NULL);
991 			assert(gnp);
992 			value = (Xword)gnp->gn_gotndx * M_GOT_ENTSIZE;
993 
994 		} else if ((arsp->rel_flags & FLG_REL_STLS) &&
995 		    ((flags & FLG_OF_RELOBJ) == 0)) {
996 			Xword	tlsstatsize;
997 
998 			/*
999 			 * This is the LE TLS reference model.  Static
1000 			 * offset is hard-coded.
1001 			 */
1002 			tlsstatsize = S_ROUND(ofl->ofl_tlsphdr->p_memsz,
1003 			    M_TLSSTATALIGN);
1004 			value = tlsstatsize - value;
1005 
1006 			/*
1007 			 * Since this code is fixed up, it assumes a
1008 			 * negative offset that can be added to the
1009 			 * thread pointer.
1010 			 */
1011 			if ((arsp->rel_rtype == R_386_TLS_LDO_32) ||
1012 			    (arsp->rel_rtype == R_386_TLS_LE))
1013 				value = -value;
1014 		}
1015 
1016 		if (arsp->rel_isdesc->is_file)
1017 			ifl_name = arsp->rel_isdesc->is_file->ifl_name;
1018 		else
1019 			ifl_name = MSG_INTL(MSG_STR_NULL);
1020 
1021 		/*
1022 		 * Make sure we have data to relocate.  Compiler and assembler
1023 		 * developers have been known to generate relocations against
1024 		 * invalid sections (normally .bss), so for their benefit give
1025 		 * them sufficient information to help analyze the problem.
1026 		 * End users should never see this.
1027 		 */
1028 		if (arsp->rel_isdesc->is_indata->d_buf == 0) {
1029 			Conv_inv_buf_t	inv_buf;
1030 
1031 			eprintf(ofl->ofl_lml, ERR_FATAL,
1032 			    MSG_INTL(MSG_REL_EMPTYSEC),
1033 			    conv_reloc_386_type(arsp->rel_rtype, 0, &inv_buf),
1034 			    ifl_name, ld_reloc_sym_name(arsp),
1035 			    EC_WORD(arsp->rel_isdesc->is_scnndx),
1036 			    arsp->rel_isdesc->is_name);
1037 			return (S_ERROR);
1038 		}
1039 
1040 		/*
1041 		 * Get the address of the data item we need to modify.
1042 		 */
1043 		addr = (uchar_t *)((uintptr_t)arsp->rel_roffset +
1044 		    (uintptr_t)_elf_getxoff(arsp->rel_isdesc->is_indata));
1045 
1046 		DBG_CALL(Dbg_reloc_doact(ofl->ofl_lml, ELF_DBG_LD_ACT,
1047 		    M_MACH, SHT_REL, arsp, EC_NATPTR(addr), value,
1048 		    ld_reloc_sym_name));
1049 		addr += (uintptr_t)osp->os_outdata->d_buf;
1050 
1051 		if ((((uintptr_t)addr - (uintptr_t)ofl->ofl_nehdr) >
1052 		    ofl->ofl_size) || (arsp->rel_roffset >
1053 		    osp->os_shdr->sh_size)) {
1054 			Conv_inv_buf_t	inv_buf;
1055 			int		class;
1056 
1057 			if (((uintptr_t)addr - (uintptr_t)ofl->ofl_nehdr) >
1058 			    ofl->ofl_size)
1059 				class = ERR_FATAL;
1060 			else
1061 				class = ERR_WARNING;
1062 
1063 			eprintf(ofl->ofl_lml, class,
1064 			    MSG_INTL(MSG_REL_INVALOFFSET),
1065 			    conv_reloc_386_type(arsp->rel_rtype, 0, &inv_buf),
1066 			    ifl_name, EC_WORD(arsp->rel_isdesc->is_scnndx),
1067 			    arsp->rel_isdesc->is_name, ld_reloc_sym_name(arsp),
1068 			    EC_ADDR((uintptr_t)addr -
1069 			    (uintptr_t)ofl->ofl_nehdr));
1070 
1071 			if (class == ERR_FATAL) {
1072 				return_code = S_ERROR;
1073 				continue;
1074 			}
1075 		}
1076 
1077 		/*
1078 		 * The relocation is additive.  Ignore the previous symbol
1079 		 * value if this local partial symbol is expanded.
1080 		 */
1081 		if (moved)
1082 			value -= *addr;
1083 
1084 		/*
1085 		 * If we have a replacement value for the relocation
1086 		 * target, put it in place now.
1087 		 */
1088 		if (arsp->rel_flags & FLG_REL_NADDEND) {
1089 			Xword addend = arsp->rel_raddend;
1090 
1091 			if (ld_reloc_targval_set(ofl, arsp, addr, addend) == 0)
1092 				return (S_ERROR);
1093 		}
1094 
1095 		/*
1096 		 * If '-z noreloc' is specified - skip the do_reloc_ld stage.
1097 		 */
1098 		if (OFL_DO_RELOC(ofl)) {
1099 			if (do_reloc_ld(arsp, addr, &value, ld_reloc_sym_name,
1100 			    ifl_name, OFL_SWAP_RELOC_DATA(ofl, arsp),
1101 			    ofl->ofl_lml) == 0)
1102 				return_code = S_ERROR;
1103 		}
1104 	}
1105 	return (return_code);
1106 }
1107 
1108 /*
1109  * Add an output relocation record.
1110  */
1111 static uintptr_t
1112 ld_add_outrel(Word flags, Rel_desc *rsp, Ofl_desc *ofl)
1113 {
1114 	Rel_desc	*orsp;
1115 	Sym_desc	*sdp = rsp->rel_sym;
1116 
1117 	/*
1118 	 * Static executables *do not* want any relocations against them.
1119 	 * Since our engine still creates relocations against a WEAK UNDEFINED
1120 	 * symbol in a static executable, it's best to disable them here
1121 	 * instead of through out the relocation code.
1122 	 */
1123 	if (OFL_IS_STATIC_EXEC(ofl))
1124 		return (1);
1125 
1126 	/*
1127 	 * If we are adding a output relocation against a section
1128 	 * symbol (non-RELATIVE) then mark that section.  These sections
1129 	 * will be added to the .dynsym symbol table.
1130 	 */
1131 	if (sdp && (rsp->rel_rtype != M_R_RELATIVE) &&
1132 	    ((flags & FLG_REL_SCNNDX) ||
1133 	    (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION))) {
1134 
1135 		/*
1136 		 * If this is a COMMON symbol - no output section
1137 		 * exists yet - (it's created as part of sym_validate()).
1138 		 * So - we mark here that when it's created it should
1139 		 * be tagged with the FLG_OS_OUTREL flag.
1140 		 */
1141 		if ((sdp->sd_flags & FLG_SY_SPECSEC) &&
1142 		    (sdp->sd_sym->st_shndx == SHN_COMMON)) {
1143 			if (ELF_ST_TYPE(sdp->sd_sym->st_info) != STT_TLS)
1144 				ofl->ofl_flags1 |= FLG_OF1_BSSOREL;
1145 			else
1146 				ofl->ofl_flags1 |= FLG_OF1_TLSOREL;
1147 		} else {
1148 			Os_desc *osp;
1149 			Is_desc *isp = sdp->sd_isc;
1150 
1151 			if (isp && ((osp = isp->is_osdesc) != NULL) &&
1152 			    ((osp->os_flags & FLG_OS_OUTREL) == 0)) {
1153 				ofl->ofl_dynshdrcnt++;
1154 				osp->os_flags |= FLG_OS_OUTREL;
1155 			}
1156 		}
1157 	}
1158 
1159 	/* Enter it into the output relocation cache */
1160 	if ((orsp = ld_reloc_enter(ofl, &ofl->ofl_outrels, rsp, flags)) == NULL)
1161 		return (S_ERROR);
1162 
1163 	if (flags & FLG_REL_GOT)
1164 		ofl->ofl_relocgotsz += (Xword)sizeof (Rel);
1165 	else if (flags & FLG_REL_PLT)
1166 		ofl->ofl_relocpltsz += (Xword)sizeof (Rel);
1167 	else if (flags & FLG_REL_BSS)
1168 		ofl->ofl_relocbsssz += (Xword)sizeof (Rel);
1169 	else if (flags & FLG_REL_NOINFO)
1170 		ofl->ofl_relocrelsz += (Xword)sizeof (Rel);
1171 	else
1172 		RELAUX_GET_OSDESC(orsp)->os_szoutrels += (Xword)sizeof (Rel);
1173 
1174 	if (orsp->rel_rtype == M_R_RELATIVE)
1175 		ofl->ofl_relocrelcnt++;
1176 
1177 	/*
1178 	 * We don't perform sorting on PLT relocations because
1179 	 * they have already been assigned a PLT index and if we
1180 	 * were to sort them we would have to re-assign the plt indexes.
1181 	 */
1182 	if (!(flags & FLG_REL_PLT))
1183 		ofl->ofl_reloccnt++;
1184 
1185 	/*
1186 	 * Insure a GLOBAL_OFFSET_TABLE is generated if required.
1187 	 */
1188 	if (IS_GOT_REQUIRED(orsp->rel_rtype))
1189 		ofl->ofl_flags |= FLG_OF_BLDGOT;
1190 
1191 	/*
1192 	 * Identify and possibly warn of a displacement relocation.
1193 	 */
1194 	if (orsp->rel_flags & FLG_REL_DISP) {
1195 		ofl->ofl_dtflags_1 |= DF_1_DISPRELPND;
1196 
1197 		if (ofl->ofl_flags & FLG_OF_VERBOSE)
1198 			ld_disp_errmsg(MSG_INTL(MSG_REL_DISPREL4), orsp, ofl);
1199 	}
1200 	DBG_CALL(Dbg_reloc_ors_entry(ofl->ofl_lml, ELF_DBG_LD, SHT_REL,
1201 	    M_MACH, orsp));
1202 	return (1);
1203 }
1204 
1205 /*
1206  * process relocation for a LOCAL symbol
1207  */
1208 static uintptr_t
1209 ld_reloc_local(Rel_desc * rsp, Ofl_desc * ofl)
1210 {
1211 	ofl_flag_t	flags = ofl->ofl_flags;
1212 	Sym_desc	*sdp = rsp->rel_sym;
1213 	Word		shndx = sdp->sd_sym->st_shndx;
1214 
1215 	/*
1216 	 * if ((shared object) and (not pc relative relocation) and
1217 	 *    (not against ABS symbol))
1218 	 * then
1219 	 *	build R_386_RELATIVE
1220 	 * fi
1221 	 */
1222 	if ((flags & FLG_OF_SHAROBJ) && (rsp->rel_flags & FLG_REL_LOAD) &&
1223 	    !(IS_PC_RELATIVE(rsp->rel_rtype)) && !(IS_SIZE(rsp->rel_rtype)) &&
1224 	    !(IS_GOT_BASED(rsp->rel_rtype)) &&
1225 	    !(rsp->rel_isdesc != NULL &&
1226 	    (rsp->rel_isdesc->is_shdr->sh_type == SHT_SUNW_dof)) &&
1227 	    (((sdp->sd_flags & FLG_SY_SPECSEC) == 0) ||
1228 	    (shndx != SHN_ABS) || (sdp->sd_aux && sdp->sd_aux->sa_symspec))) {
1229 		Word	ortype = rsp->rel_rtype;
1230 
1231 		rsp->rel_rtype = R_386_RELATIVE;
1232 		if (ld_add_outrel(NULL, rsp, ofl) == S_ERROR)
1233 			return (S_ERROR);
1234 		rsp->rel_rtype = ortype;
1235 	}
1236 
1237 	/*
1238 	 * If the relocation is against a 'non-allocatable' section
1239 	 * and we can not resolve it now - then give a warning
1240 	 * message.
1241 	 *
1242 	 * We can not resolve the symbol if either:
1243 	 *	a) it's undefined
1244 	 *	b) it's defined in a shared library and a
1245 	 *	   COPY relocation hasn't moved it to the executable
1246 	 *
1247 	 * Note: because we process all of the relocations against the
1248 	 *	text segment before any others - we know whether
1249 	 *	or not a copy relocation will be generated before
1250 	 *	we get here (see reloc_init()->reloc_segments()).
1251 	 */
1252 	if (!(rsp->rel_flags & FLG_REL_LOAD) &&
1253 	    ((shndx == SHN_UNDEF) ||
1254 	    ((sdp->sd_ref == REF_DYN_NEED) &&
1255 	    ((sdp->sd_flags & FLG_SY_MVTOCOMM) == 0)))) {
1256 		Conv_inv_buf_t	inv_buf;
1257 		Os_desc		*osp = RELAUX_GET_OSDESC(rsp);
1258 
1259 		/*
1260 		 * If the relocation is against a SHT_SUNW_ANNOTATE
1261 		 * section - then silently ignore that the relocation
1262 		 * can not be resolved.
1263 		 */
1264 		if (osp && (osp->os_shdr->sh_type == SHT_SUNW_ANNOTATE))
1265 			return (0);
1266 		eprintf(ofl->ofl_lml, ERR_WARNING, MSG_INTL(MSG_REL_EXTERNSYM),
1267 		    conv_reloc_386_type(rsp->rel_rtype, 0, &inv_buf),
1268 		    rsp->rel_isdesc->is_file->ifl_name,
1269 		    ld_reloc_sym_name(rsp), osp->os_name);
1270 		return (1);
1271 	}
1272 
1273 	/*
1274 	 * Perform relocation.
1275 	 */
1276 	return (ld_add_actrel(NULL, rsp, ofl));
1277 }
1278 
1279 static uintptr_t
1280 ld_reloc_TLS(Boolean local, Rel_desc * rsp, Ofl_desc * ofl)
1281 {
1282 	Word		rtype = rsp->rel_rtype;
1283 	Sym_desc	*sdp = rsp->rel_sym;
1284 	ofl_flag_t	flags = ofl->ofl_flags;
1285 	Gotndx		*gnp;
1286 
1287 	/*
1288 	 * If we're building an executable - use either the IE or LE access
1289 	 * model.  If we're building a shared object process any IE model.
1290 	 */
1291 	if ((flags & FLG_OF_EXEC) || (IS_TLS_IE(rtype))) {
1292 		/*
1293 		 * Set the DF_STATIC_TLS flag.
1294 		 */
1295 		ofl->ofl_dtflags |= DF_STATIC_TLS;
1296 
1297 		if (!local || ((flags & FLG_OF_EXEC) == 0)) {
1298 			/*
1299 			 * Assign a GOT entry for static TLS references.
1300 			 */
1301 			if ((gnp = ld_find_got_ndx(sdp->sd_GOTndxs,
1302 			    GOT_REF_TLSIE, ofl, NULL)) == NULL) {
1303 
1304 				if (ld_assign_got_TLS(local, rsp, ofl, sdp,
1305 				    gnp, GOT_REF_TLSIE, FLG_REL_STLS,
1306 				    rtype, R_386_TLS_TPOFF, NULL) == S_ERROR)
1307 					return (S_ERROR);
1308 			}
1309 
1310 			/*
1311 			 * IE access model.
1312 			 */
1313 			if (IS_TLS_IE(rtype)) {
1314 				if (ld_add_actrel(FLG_REL_STLS,
1315 				    rsp, ofl) == S_ERROR)
1316 					return (S_ERROR);
1317 
1318 				/*
1319 				 * A non-pic shared object needs to adjust the
1320 				 * active relocation (indntpoff).
1321 				 */
1322 				if (((flags & FLG_OF_EXEC) == 0) &&
1323 				    (rtype == R_386_TLS_IE)) {
1324 					rsp->rel_rtype = R_386_RELATIVE;
1325 					return (ld_add_outrel(NULL, rsp, ofl));
1326 				}
1327 				return (1);
1328 			}
1329 
1330 			/*
1331 			 * Fixups are required for other executable models.
1332 			 */
1333 			return (ld_add_actrel((FLG_REL_TLSFIX | FLG_REL_STLS),
1334 			    rsp, ofl));
1335 		}
1336 
1337 		/*
1338 		 * LE access model.
1339 		 */
1340 		if (IS_TLS_LE(rtype) || (rtype == R_386_TLS_LDO_32))
1341 			return (ld_add_actrel(FLG_REL_STLS, rsp, ofl));
1342 
1343 		return (ld_add_actrel((FLG_REL_TLSFIX | FLG_REL_STLS),
1344 		    rsp, ofl));
1345 	}
1346 
1347 	/*
1348 	 * Building a shared object.
1349 	 *
1350 	 * Assign a GOT entry for a dynamic TLS reference.
1351 	 */
1352 	if (IS_TLS_LD(rtype) && ((gnp = ld_find_got_ndx(sdp->sd_GOTndxs,
1353 	    GOT_REF_TLSLD, ofl, NULL)) == NULL)) {
1354 
1355 		if (ld_assign_got_TLS(local, rsp, ofl, sdp, gnp, GOT_REF_TLSLD,
1356 		    FLG_REL_MTLS, rtype, R_386_TLS_DTPMOD32, NULL) == S_ERROR)
1357 			return (S_ERROR);
1358 
1359 	} else if (IS_TLS_GD(rtype) && ((gnp = ld_find_got_ndx(sdp->sd_GOTndxs,
1360 	    GOT_REF_TLSGD, ofl, NULL)) == NULL)) {
1361 
1362 		if (ld_assign_got_TLS(local, rsp, ofl, sdp, gnp, GOT_REF_TLSGD,
1363 		    FLG_REL_DTLS, rtype, R_386_TLS_DTPMOD32,
1364 		    R_386_TLS_DTPOFF32) == S_ERROR)
1365 			return (S_ERROR);
1366 	}
1367 
1368 	/*
1369 	 * For GD/LD TLS reference - TLS_{GD,LD}_CALL, this will eventually
1370 	 * cause a call to __tls_get_addr().  Convert this relocation to that
1371 	 * symbol now, and prepare for the PLT magic.
1372 	 */
1373 	if ((rtype == R_386_TLS_GD_PLT) || (rtype == R_386_TLS_LDM_PLT)) {
1374 		Sym_desc	*tlsgetsym;
1375 
1376 		if ((tlsgetsym = ld_sym_add_u(MSG_ORIG(MSG_SYM_TLSGETADDR_UU),
1377 		    ofl, MSG_STR_TLSREL)) == (Sym_desc *)S_ERROR)
1378 			return (S_ERROR);
1379 
1380 		rsp->rel_sym = tlsgetsym;
1381 		rsp->rel_rtype = R_386_PLT32;
1382 
1383 		if (ld_reloc_plt(rsp, ofl) == S_ERROR)
1384 			return (S_ERROR);
1385 
1386 		rsp->rel_sym = sdp;
1387 		rsp->rel_rtype = rtype;
1388 		return (1);
1389 	}
1390 
1391 	if (IS_TLS_LD(rtype))
1392 		return (ld_add_actrel(FLG_REL_MTLS, rsp, ofl));
1393 
1394 	return (ld_add_actrel(FLG_REL_DTLS, rsp, ofl));
1395 }
1396 
1397 /* ARGSUSED4 */
1398 static uintptr_t
1399 ld_assign_got_ndx(Alist **alpp, Gotndx *pgnp, Gotref gref, Ofl_desc *ofl,
1400     Rel_desc *rsp, Sym_desc *sdp)
1401 {
1402 	Gotndx	gn, *gnp;
1403 	uint_t	gotents;
1404 
1405 	if (pgnp)
1406 		return (1);
1407 
1408 	if ((gref == GOT_REF_TLSGD) || (gref == GOT_REF_TLSLD))
1409 		gotents = 2;
1410 	else
1411 		gotents = 1;
1412 
1413 	gn.gn_addend = 0;
1414 	gn.gn_gotndx = ofl->ofl_gotcnt;
1415 	gn.gn_gotref = gref;
1416 
1417 	ofl->ofl_gotcnt += gotents;
1418 
1419 	if (gref == GOT_REF_TLSLD) {
1420 		if (ofl->ofl_tlsldgotndx == NULL) {
1421 			if ((gnp = libld_malloc(sizeof (Gotndx))) == NULL)
1422 				return (S_ERROR);
1423 			(void) memcpy(gnp, &gn, sizeof (Gotndx));
1424 			ofl->ofl_tlsldgotndx = gnp;
1425 		}
1426 		return (1);
1427 	}
1428 
1429 	/*
1430 	 * GOT indexes are maintained on an Alist, where there is typically
1431 	 * only one index.  The usage of this list is to scan the list to find
1432 	 * an index, and then apply that index immediately to a relocation.
1433 	 * Thus there are no external references to these GOT index structures
1434 	 * that can be compromised by the Alist being reallocated.
1435 	 */
1436 	if (alist_append(alpp, &gn, sizeof (Gotndx), AL_CNT_SDP_GOT) == NULL)
1437 		return (S_ERROR);
1438 
1439 	return (1);
1440 }
1441 
1442 static void
1443 ld_assign_plt_ndx(Sym_desc * sdp, Ofl_desc *ofl)
1444 {
1445 	sdp->sd_aux->sa_PLTndx = 1 + ofl->ofl_pltcnt++;
1446 	sdp->sd_aux->sa_PLTGOTndx = ofl->ofl_gotcnt++;
1447 	ofl->ofl_flags |= FLG_OF_BLDGOT;
1448 }
1449 
1450 /*
1451  * Initializes .got[0] with the _DYNAMIC symbol value.
1452  */
1453 static uintptr_t
1454 ld_fillin_gotplt(Ofl_desc *ofl)
1455 {
1456 	ofl_flag_t	flags = ofl->ofl_flags;
1457 	int		bswap = (ofl->ofl_flags1 & FLG_OF1_ENCDIFF) != 0;
1458 
1459 	if (ofl->ofl_osgot) {
1460 		Sym_desc	*sdp;
1461 
1462 		if ((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_DYNAMIC_U),
1463 		    SYM_NOHASH, NULL, ofl)) != NULL) {
1464 			uchar_t	*genptr;
1465 
1466 			genptr = ((uchar_t *)ofl->ofl_osgot->os_outdata->d_buf +
1467 			    (M_GOT_XDYNAMIC * M_GOT_ENTSIZE));
1468 			/* LINTED */
1469 			*(Word *)genptr = (Word)sdp->sd_sym->st_value;
1470 			if (bswap)
1471 				/* LINTED */
1472 				*(Word *)genptr =
1473 				    /* LINTED */
1474 				    ld_bswap_Word(*(Word *)genptr);
1475 		}
1476 	}
1477 
1478 	/*
1479 	 * Fill in the reserved slot in the procedure linkage table the first
1480 	 * entry is:
1481 	 *  if (building a.out) {
1482 	 *	PUSHL	got[1]		    # the address of the link map entry
1483 	 *	JMP *	got[2]		    # the address of rtbinder
1484 	 *  } else {
1485 	 *	PUSHL	got[1]@GOT(%ebx)    # the address of the link map entry
1486 	 *	JMP *	got[2]@GOT(%ebx)    # the address of rtbinder
1487 	 *  }
1488 	 */
1489 	if ((flags & FLG_OF_DYNAMIC) && ofl->ofl_osplt) {
1490 		uchar_t *pltent;
1491 
1492 		pltent = (uchar_t *)ofl->ofl_osplt->os_outdata->d_buf;
1493 		if (!(flags & FLG_OF_SHAROBJ)) {
1494 			pltent[0] = M_SPECIAL_INST;
1495 			pltent[1] = M_PUSHL_DISP;
1496 			pltent += 2;
1497 			/* LINTED */
1498 			*(Word *)pltent = (Word)(ofl->ofl_osgot->os_shdr->
1499 			    sh_addr + M_GOT_XLINKMAP * M_GOT_ENTSIZE);
1500 			if (bswap)
1501 				/* LINTED */
1502 				*(Word *)pltent =
1503 				    /* LINTED */
1504 				    ld_bswap_Word(*(Word *)pltent);
1505 			pltent += 4;
1506 			pltent[0] = M_SPECIAL_INST;
1507 			pltent[1] = M_JMP_DISP_IND;
1508 			pltent += 2;
1509 			/* LINTED */
1510 			*(Word *)pltent = (Word)(ofl->ofl_osgot->os_shdr->
1511 			    sh_addr + M_GOT_XRTLD * M_GOT_ENTSIZE);
1512 			if (bswap)
1513 				/* LINTED */
1514 				*(Word *)pltent =
1515 				    /* LINTED */
1516 				    ld_bswap_Word(*(Word *)pltent);
1517 		} else {
1518 			pltent[0] = M_SPECIAL_INST;
1519 			pltent[1] = M_PUSHL_REG_DISP;
1520 			pltent += 2;
1521 			/* LINTED */
1522 			*(Word *)pltent = (Word)(M_GOT_XLINKMAP *
1523 			    M_GOT_ENTSIZE);
1524 			if (bswap)
1525 				/* LINTED */
1526 				*(Word *)pltent =
1527 				    /* LINTED */
1528 				    ld_bswap_Word(*(Word *)pltent);
1529 			pltent += 4;
1530 			pltent[0] = M_SPECIAL_INST;
1531 			pltent[1] = M_JMP_REG_DISP_IND;
1532 			pltent += 2;
1533 			/* LINTED */
1534 			*(Word *)pltent = (Word)(M_GOT_XRTLD *
1535 			    M_GOT_ENTSIZE);
1536 			if (bswap)
1537 				/* LINTED */
1538 				*(Word *)pltent =
1539 				    /* LINTED */
1540 				    ld_bswap_Word(*(Word *)pltent);
1541 		}
1542 	}
1543 	return (1);
1544 }
1545 
1546 
1547 
1548 /*
1549  * Template for generating "void (*)(void)" function
1550  */
1551 static const uchar_t nullfunc_tmpl[] = {	/* IA32 */
1552 /* 0x00 */	0xc3				/* ret */
1553 };
1554 
1555 
1556 
1557 /*
1558  * Function used to provide fill padding in SHF_EXECINSTR sections
1559  *
1560  * entry:
1561  *
1562  *	base - base address of section being filled
1563  *	offset - starting offset for fill within memory referenced by base
1564  *	cnt - # bytes to be filled
1565  *
1566  * exit:
1567  *	The fill has been completed.
1568  */
1569 static void
1570 execfill(void *base, off_t off, size_t cnt)
1571 {
1572 	/*
1573 	 * 0x90 is an X86 NOP instruction in both 32 and 64-bit worlds.
1574 	 * There are no alignment constraints.
1575 	 */
1576 	(void) memset(off + (char *)base, 0x90, cnt);
1577 }
1578 
1579 
1580 /*
1581  * Return the ld_targ definition for this target.
1582  */
1583 const Target *
1584 ld_targ_init_x86(void)
1585 {
1586 	static const Target _ld_targ = {
1587 		{			/* Target_mach */
1588 			M_MACH,			/* m_mach */
1589 			M_MACHPLUS,		/* m_machplus */
1590 			M_FLAGSPLUS,		/* m_flagsplus */
1591 			M_CLASS,		/* m_class */
1592 			M_DATA,			/* m_data */
1593 
1594 			M_SEGM_ALIGN,		/* m_segm_align */
1595 			M_SEGM_ORIGIN,		/* m_segm_origin */
1596 			M_SEGM_AORIGIN,		/* m_segm_aorigin */
1597 			M_DATASEG_PERM,		/* m_dataseg_perm */
1598 			M_STACK_PERM,		/* m_stack_perm */
1599 			M_WORD_ALIGN,		/* m_word_align */
1600 			MSG_ORIG(MSG_PTH_RTLD),	/* m_def_interp */
1601 
1602 			/* Relocation type codes */
1603 			M_R_ARRAYADDR,		/* m_r_arrayaddr */
1604 			M_R_COPY,		/* m_r_copy */
1605 			M_R_GLOB_DAT,		/* m_r_glob_dat */
1606 			M_R_JMP_SLOT,		/* m_r_jmp_slot */
1607 			M_R_NUM,		/* m_r_num */
1608 			M_R_NONE,		/* m_r_none */
1609 			M_R_RELATIVE,		/* m_r_relative */
1610 			M_R_REGISTER,		/* m_r_register */
1611 
1612 			/* Relocation related constants */
1613 			M_REL_DT_COUNT,		/* m_rel_dt_count */
1614 			M_REL_DT_ENT,		/* m_rel_dt_ent */
1615 			M_REL_DT_SIZE,		/* m_rel_dt_size */
1616 			M_REL_DT_TYPE,		/* m_rel_dt_type */
1617 			M_REL_SHT_TYPE,		/* m_rel_sht_type */
1618 
1619 			/* GOT related constants */
1620 			M_GOT_ENTSIZE,		/* m_got_entsize */
1621 			M_GOT_XNumber,		/* m_got_xnumber */
1622 
1623 			/* PLT related constants */
1624 			M_PLT_ALIGN,		/* m_plt_align */
1625 			M_PLT_ENTSIZE,		/* m_plt_entsize */
1626 			M_PLT_RESERVSZ,		/* m_plt_reservsz */
1627 			M_PLT_SHF_FLAGS,	/* m_plt_shf_flags */
1628 
1629 			/* Section type of .eh_frame/.eh_frame_hdr sections */
1630 			SHT_PROGBITS,		/* m_sht_unwind */
1631 
1632 			M_DT_REGISTER,		/* m_dt_register */
1633 		},
1634 		{			/* Target_machid */
1635 			M_ID_ARRAY,		/* id_array */
1636 			M_ID_BSS,		/* id_bss */
1637 			M_ID_CAP,		/* id_cap */
1638 			M_ID_CAPINFO,		/* id_capinfo */
1639 			M_ID_CAPCHAIN,		/* id_capchain */
1640 			M_ID_DATA,		/* id_data */
1641 			M_ID_DYNAMIC,		/* id_dynamic */
1642 			M_ID_DYNSORT,		/* id_dynsort */
1643 			M_ID_DYNSTR,		/* id_dynstr */
1644 			M_ID_DYNSYM,		/* id_dynsym */
1645 			M_ID_DYNSYM_NDX,	/* id_dynsym_ndx */
1646 			M_ID_GOT,		/* id_got */
1647 			M_ID_UNKNOWN,		/* id_gotdata (unused) */
1648 			M_ID_HASH,		/* id_hash */
1649 			M_ID_INTERP,		/* id_interp */
1650 			M_ID_LBSS,		/* id_lbss */
1651 			M_ID_LDYNSYM,		/* id_ldynsym */
1652 			M_ID_NOTE,		/* id_note */
1653 			M_ID_NULL,		/* id_null */
1654 			M_ID_PLT,		/* id_plt */
1655 			M_ID_REL,		/* id_rel */
1656 			M_ID_STRTAB,		/* id_strtab */
1657 			M_ID_SYMINFO,		/* id_syminfo */
1658 			M_ID_SYMTAB,		/* id_symtab */
1659 			M_ID_SYMTAB_NDX,	/* id_symtab_ndx */
1660 			M_ID_TEXT,		/* id_text */
1661 			M_ID_TLS,		/* id_tls */
1662 			M_ID_TLSBSS,		/* id_tlsbss */
1663 			M_ID_UNKNOWN,		/* id_unknown */
1664 			M_ID_UNWIND,		/* id_unwind */
1665 			M_ID_UNWINDHDR,		/* id_unwindhdr */
1666 			M_ID_USER,		/* id_user */
1667 			M_ID_VERSION,		/* id_version */
1668 		},
1669 		{			/* Target_nullfunc */
1670 			nullfunc_tmpl,		/* nf_template */
1671 			sizeof (nullfunc_tmpl),	/* nf_size */
1672 		},
1673 		{			/* Target_fillfunc */
1674 			execfill		/* ff_execfill */
1675 		},
1676 		{			/* Target_machrel */
1677 			reloc_table,
1678 
1679 			ld_init_rel,		/* mr_init_rel */
1680 			ld_mach_eflags,		/* mr_mach_eflags */
1681 			ld_mach_make_dynamic,	/* mr_mach_make_dynamic */
1682 			ld_mach_update_odynamic, /* mr_mach_update_odynamic */
1683 			ld_calc_plt_addr,	/* mr_calc_plt_addr */
1684 			ld_perform_outreloc,	/* mr_perform_outreloc */
1685 			ld_do_activerelocs,	/* mr_do_activerelocs */
1686 			ld_add_outrel,		/* mr_add_outrel */
1687 			NULL,			/* mr_reloc_register */
1688 			ld_reloc_local,		/* mr_reloc_local */
1689 			NULL,			/* mr_reloc_GOTOP */
1690 			ld_reloc_TLS,		/* mr_reloc_TLS */
1691 			NULL,			/* mr_assign_got */
1692 			ld_find_got_ndx,	/* mr_find_got_ndx */
1693 			ld_calc_got_offset,	/* mr_calc_got_offset */
1694 			ld_assign_got_ndx,	/* mr_assign_got_ndx */
1695 			ld_assign_plt_ndx,	/* mr_assign_plt_ndx */
1696 			NULL,			/* mr_allocate_got */
1697 			ld_fillin_gotplt,	/* mr_fillin_gotplt */
1698 		},
1699 		{			/* Target_machsym */
1700 			NULL,			/* ms_reg_check */
1701 			NULL,			/* ms_mach_sym_typecheck */
1702 			NULL,			/* ms_is_regsym */
1703 			NULL,			/* ms_reg_find */
1704 			NULL			/* ms_reg_enter */
1705 		}
1706 	};
1707 
1708 	return (&_ld_targ);
1709 }
1710