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