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