xref: /titanic_50/usr/src/cmd/sgs/libld/common/machrel.intel.c (revision 60c807700988885656502665e0cf8afd4b4346f7)
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 2006 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 			    rtype, R_386_TLS_GOTIE, arsp->rel_roffset,
386 			    sdp->sd_name));
387 			arsp->rel_rtype = R_386_TLS_GOTIE;
388 			arsp->rel_roffset += 5;
389 
390 			/*
391 			 * Addjust 'offset' to beginning of instruction
392 			 * sequence.
393 			 */
394 			offset -= 3;
395 			r1 = (offset[2] & SIB_MSK_IND) >> 3;
396 			(void) memcpy(offset, tlsinstr_gd_ie,
397 			    sizeof (tlsinstr_gd_ie));
398 
399 			/*
400 			 * set register %r1 into the addl
401 			 * instruction.
402 			 */
403 			offset[0x7] |= r1;
404 			return (FIX_RELOC);
405 
406 		case R_386_TLS_GD_PLT:
407 			/*
408 			 * Fixup done via the TLS_GD relocation
409 			 */
410 			DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
411 			    rtype, R_386_NONE, arsp->rel_roffset,
412 			    sdp->sd_name));
413 			return (FIX_DONE);
414 		}
415 	}
416 
417 	/*
418 	 * LE reference model
419 	 */
420 	switch (rtype) {
421 	case R_386_TLS_GD:
422 		/*
423 		 * Transition:
424 		 *	0x0 leal x@tlsgd(,r1,1), %eax
425 		 *	0x7 call ___tls_get_addr
426 		 *	0xc
427 		 * To:
428 		 *	0x0 movl %gs:0, %eax
429 		 *	0x6 addl $x@ntpoff, %eax
430 		 *	0xb nop
431 		 *	0xc
432 		 */
433 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
434 		    rtype, R_386_TLS_LE, arsp->rel_roffset, sdp->sd_name));
435 
436 		arsp->rel_rtype = R_386_TLS_LE;
437 		arsp->rel_roffset += 4;
438 
439 		/*
440 		 * Addjust 'offset' to beginning of instruction
441 		 * sequence.
442 		 */
443 		offset -= 3;
444 		(void) memcpy(offset, tlsinstr_gd_le,
445 		    sizeof (tlsinstr_gd_le));
446 		return (FIX_RELOC);
447 
448 	case R_386_TLS_GD_PLT:
449 	case R_386_PLT32:
450 		/*
451 		 * Fixup done via the TLS_GD relocation
452 		 */
453 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
454 		    rtype, R_386_NONE, arsp->rel_roffset, sdp->sd_name));
455 		return (FIX_DONE);
456 
457 	case R_386_TLS_LDM_PLT:
458 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
459 		    rtype, R_386_NONE, arsp->rel_roffset, sdp->sd_name));
460 
461 		/*
462 		 * Transition:
463 		 *	call __tls_get_addr()
464 		 * to:
465 		 *	nop
466 		 *	nop
467 		 *	nop
468 		 *	nop
469 		 *	nop
470 		 */
471 		*(offset - 1) = TLS_NOP;
472 		*(offset) = TLS_NOP;
473 		*(offset + 1) = TLS_NOP;
474 		*(offset + 2) = TLS_NOP;
475 		*(offset + 3) = TLS_NOP;
476 		return (FIX_DONE);
477 
478 	case R_386_TLS_LDM:
479 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
480 		    rtype, R_386_NONE, arsp->rel_roffset, sdp->sd_name));
481 
482 		/*
483 		 * Transition:
484 		 *
485 		 *  0x00 leal x1@tlsldm(%ebx), %eax
486 		 *  0x06 call ___tls_get_addr
487 		 *
488 		 * to:
489 		 *
490 		 *  0x00 movl %gs:0, %eax
491 		 */
492 		(void) memcpy(offset - 2, tlsinstr_gd_ie_movgs,
493 		    sizeof (tlsinstr_gd_ie_movgs));
494 		return (FIX_DONE);
495 
496 	case R_386_TLS_LDO_32:
497 		/*
498 		 *  Instructions:
499 		 *
500 		 *  0x10 leal x1@dtpoff(%eax), %edx	R_386_TLS_LDO_32
501 		 *		to
502 		 *  0x10 leal x1@ntpoff(%eax), %edx	R_386_TLS_LE
503 		 *
504 		 */
505 		offset -= 2;
506 
507 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
508 		    rtype, R_386_TLS_LE, arsp->rel_roffset, sdp->sd_name));
509 		arsp->rel_rtype = R_386_TLS_LE;
510 		return (FIX_RELOC);
511 
512 	case R_386_TLS_GOTIE:
513 		/*
514 		 * These transitions are a little different than the
515 		 * others, in that we could have multiple instructions
516 		 * pointed to by a single relocation.  Depending upon the
517 		 * instruction, we perform a different code transition.
518 		 *
519 		 * Here's the known transitions:
520 		 *
521 		 *  1) movl foo@gotntpoff(%reg1), %reg2
522 		 *	0x8b, 0x80 | (reg2 << 3) | reg1, foo@gotntpoff
523 		 *
524 		 *  2) addl foo@gotntpoff(%reg1), %reg2
525 		 *	0x03, 0x80 | (reg2 << 3) | reg1, foo@gotntpoff
526 		 *
527 		 *  Transitions IE -> LE
528 		 *
529 		 *  1) movl $foo@ntpoff, %reg2
530 		 *	0xc7, 0xc0 | reg2, foo@ntpoff
531 		 *
532 		 *  2) addl $foo@ntpoff, %reg2
533 		 *	0x81, 0xc0 | reg2, foo@ntpoff
534 		 *
535 		 * Note: reg1 != 4 (%esp)
536 		 */
537 		DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
538 		    rtype, R_386_TLS_LE, arsp->rel_roffset, sdp->sd_name));
539 		arsp->rel_rtype = R_386_TLS_LE;
540 
541 		offset -= 2;
542 		r2 = (offset[1] & MODRM_MSK_RO) >> 3;
543 		if (offset[0] == 0x8b) {
544 			/* case 1 above */
545 			offset[0] = 0xc7;	/* movl */
546 			offset[1] = 0xc0 | r2;
547 			return (FIX_RELOC);
548 		}
549 
550 		if (offset[0] == 0x03) {
551 			/* case 2 above */
552 			assert(offset[0] == 0x03);
553 			offset[0] = 0x81;	/* addl */
554 			offset[1] = 0xc0 | r2;
555 			return (FIX_RELOC);
556 		}
557 
558 		/*
559 		 * Unexpected instruction sequence - fatal error.
560 		 */
561 		eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_REL_BADTLSINS),
562 		    conv_reloc_386_type(arsp->rel_rtype, 0),
563 		    arsp->rel_isdesc->is_file->ifl_name,
564 		    demangle(arsp->rel_sname), arsp->rel_isdesc->is_name,
565 		    EC_OFF(arsp->rel_roffset));
566 		return (FIX_ERROR);
567 
568 	case R_386_TLS_IE:
569 		/*
570 		 * These transitions are a little different than the
571 		 * others, in that we could have multiple instructions
572 		 * pointed to by a single relocation.  Depending upon the
573 		 * instruction, we perform a different code transition.
574 		 *
575 		 * Here's the known transitions:
576 		 *  1) movl foo@indntpoff, %eax
577 		 *	0xa1, foo@indntpoff
578 		 *
579 		 *  2) movl foo@indntpoff, %eax
580 		 *	0x8b, 0x05 | (reg << 3), foo@gotntpoff
581 		 *
582 		 *  3) addl foo@indntpoff, %eax
583 		 *	0x03, 0x05 | (reg << 3), foo@gotntpoff
584 		 *
585 		 *  Transitions IE -> LE
586 		 *
587 		 *  1) movl $foo@ntpoff, %eax
588 		 *	0xb8, foo@ntpoff
589 		 *
590 		 *  2) movl $foo@ntpoff, %reg
591 		 *	0xc7, 0xc0 | reg, foo@ntpoff
592 		 *
593 		 *  3) addl $foo@ntpoff, %reg
594 		 *	0x81, 0xc0 | reg, foo@ntpoff
595 		 */
596 		arsp->rel_rtype = R_386_TLS_LE;
597 		offset--;
598 		if (offset[0] == 0xa1) {
599 			/* case 1 above */
600 			offset[0] = 0xb8;	/*  movl */
601 			return (FIX_RELOC);
602 		}
603 
604 		offset--;
605 		if (offset[0] == 0x8b) {
606 			/* case 2 above */
607 			r2 = (offset[1] & MODRM_MSK_RO) >> 3;
608 			offset[0] = 0xc7;	/* movl */
609 			offset[1] = 0xc0 | r2;
610 			return (FIX_RELOC);
611 		}
612 		if (offset[0] == 0x03) {
613 			/* case 3 above */
614 			r2 = (offset[1] & MODRM_MSK_RO) >> 3;
615 			offset[0] = 0x81;	/* addl */
616 			offset[1] = 0xc0 | r2;
617 			return (FIX_RELOC);
618 		}
619 		/*
620 		 * Unexpected instruction sequence - fatal error.
621 		 */
622 		eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_REL_BADTLSINS),
623 		    conv_reloc_386_type(arsp->rel_rtype, 0),
624 		    arsp->rel_isdesc->is_file->ifl_name,
625 		    demangle(arsp->rel_sname), arsp->rel_isdesc->is_name,
626 		    EC_OFF(arsp->rel_roffset));
627 		return (FIX_ERROR);
628 	}
629 	return (FIX_RELOC);
630 }
631 
632 uintptr_t
633 ld_do_activerelocs(Ofl_desc *ofl)
634 {
635 	Rel_desc	*arsp;
636 	Rel_cache	*rcp;
637 	Listnode	*lnp;
638 	uintptr_t	return_code = 1;
639 	Word		flags = ofl->ofl_flags;
640 	Word		dtflags1 = ofl->ofl_dtflags_1;
641 
642 	DBG_CALL(Dbg_reloc_doact_title(ofl->ofl_lml));
643 	/*
644 	 * Process active relocations.
645 	 */
646 	for (LIST_TRAVERSE(&ofl->ofl_actrels, lnp, rcp)) {
647 		/* LINTED */
648 		for (arsp = (Rel_desc *)(rcp + 1);
649 		    arsp < rcp->rc_free; arsp++) {
650 			uchar_t		*addr;
651 			Xword 		value;
652 			Sym_desc	*sdp;
653 			const char	*ifl_name;
654 			Xword		refaddr;
655 			int		moved = 0;
656 			Gotref		gref;
657 
658 			/*
659 			 * If the section this relocation is against has been
660 			 * discarded (-zignore), then discard (skip) the
661 			 * relocation itself.
662 			 */
663 			if ((arsp->rel_isdesc->is_flags & FLG_IS_DISCARD) &&
664 			    ((arsp->rel_flags &
665 			    (FLG_REL_GOT | FLG_REL_BSS |
666 			    FLG_REL_PLT | FLG_REL_NOINFO)) == 0)) {
667 				DBG_CALL(Dbg_reloc_discard(ofl->ofl_lml,
668 				    M_MACH, arsp));
669 				continue;
670 			}
671 
672 			/*
673 			 * We deteremine what the 'got reference'
674 			 * model (if required) is at this point.  This
675 			 * needs to be done before tls_fixup() since
676 			 * it may 'transition' our instructions.
677 			 *
678 			 * The got table entries have already been assigned,
679 			 * and we bind to those initial entries.
680 			 */
681 			if (arsp->rel_flags & FLG_REL_DTLS)
682 				gref = GOT_REF_TLSGD;
683 			else if (arsp->rel_flags & FLG_REL_MTLS)
684 				gref = GOT_REF_TLSLD;
685 			else if (arsp->rel_flags & FLG_REL_STLS)
686 				gref = GOT_REF_TLSIE;
687 			else
688 				gref = GOT_REF_GENERIC;
689 
690 			/*
691 			 * Perform any required TLS fixups.
692 			 */
693 			if (arsp->rel_flags & FLG_REL_TLSFIX) {
694 				Fixupret	ret;
695 
696 				if ((ret = tls_fixups(ofl, arsp)) == FIX_ERROR)
697 					return (S_ERROR);
698 				if (ret == FIX_DONE)
699 					continue;
700 			}
701 
702 			/*
703 			 * If this is a relocation against a move table, or
704 			 * expanded move table, adjust the relocation entries.
705 			 */
706 			if (arsp->rel_move)
707 				ld_adj_movereloc(ofl, arsp);
708 
709 			sdp = arsp->rel_sym;
710 			refaddr = arsp->rel_roffset +
711 			    (Off)_elf_getxoff(arsp->rel_isdesc->is_indata);
712 
713 			if (arsp->rel_flags & FLG_REL_CLVAL)
714 				value = 0;
715 			else if (ELF_ST_TYPE(sdp->sd_sym->st_info) ==
716 			    STT_SECTION) {
717 				Sym_desc	*sym;
718 
719 				/*
720 				 * The value for a symbol pointing to a SECTION
721 				 * is based off of that sections position.
722 				 *
723 				 * The second argument of the ld_am_I_partial()
724 				 * is the value stored at the target address
725 				 * relocation is going to be applied.
726 				 */
727 				if ((sdp->sd_isc->is_flags & FLG_IS_RELUPD) &&
728 				    /* LINTED */
729 				    (sym = ld_am_I_partial(arsp, *(Xword *)
730 				    ((uchar_t *)
731 				    arsp->rel_isdesc->is_indata->d_buf +
732 				    arsp->rel_roffset)))) {
733 					/*
734 					 * If the symbol is moved,
735 					 * adjust the value
736 					 */
737 					value = sym->sd_sym->st_value;
738 					moved = 1;
739 				} else {
740 					value = _elf_getxoff(
741 					    sdp->sd_isc->is_indata);
742 					if (sdp->sd_isc->is_shdr->sh_flags &
743 					    SHF_ALLOC)
744 						value += sdp->sd_isc->
745 						    is_osdesc->os_shdr->sh_addr;
746 				}
747 				if (sdp->sd_isc->is_shdr->sh_flags & SHF_TLS)
748 					value -= ofl->ofl_tlsphdr->p_vaddr;
749 			} else {
750 				/*
751 				 * else the value is the symbols value
752 				 */
753 				value = sdp->sd_sym->st_value;
754 			}
755 
756 			/*
757 			 * Relocation against the GLOBAL_OFFSET_TABLE.
758 			 */
759 			if (arsp->rel_flags & FLG_REL_GOT)
760 				arsp->rel_osdesc = ofl->ofl_osgot;
761 
762 			/*
763 			 * If loadable and not producing a relocatable object
764 			 * add the sections virtual address to the reference
765 			 * address.
766 			 */
767 			if ((arsp->rel_flags & FLG_REL_LOAD) &&
768 			    ((flags & FLG_OF_RELOBJ) == 0))
769 				refaddr += arsp->rel_isdesc->is_osdesc->
770 				    os_shdr->sh_addr;
771 
772 			/*
773 			 * If this entry has a PLT assigned to it, it's
774 			 * value is actually the address of the PLT (and
775 			 * not the address of the function).
776 			 */
777 			if (IS_PLT(arsp->rel_rtype)) {
778 				if (sdp->sd_aux && sdp->sd_aux->sa_PLTndx)
779 					value = ld_calc_plt_addr(sdp, ofl);
780 			}
781 
782 			/*
783 			 * Determine whether the value needs further adjustment.
784 			 * Filter through the attributes of the relocation to
785 			 * determine what adjustment is required.  Note, many
786 			 * of the following cases are only applicable when a
787 			 * .got is present.  As a .got is not generated when a
788 			 * relocatable object is being built, any adjustments
789 			 * that require a .got need to be skipped.
790 			 */
791 			if ((arsp->rel_flags & FLG_REL_GOT) &&
792 			    ((flags & FLG_OF_RELOBJ) == 0)) {
793 				Xword		R1addr;
794 				uintptr_t	R2addr;
795 				Word		gotndx;
796 				Gotndx		*gnp;
797 
798 				/*
799 				 * Perform relocation against GOT table.  Since
800 				 * this doesn't fit exactly into a relocation
801 				 * we place the appropriate byte in the GOT
802 				 * directly
803 				 *
804 				 * Calculate offset into GOT at which to apply
805 				 * the relocation.
806 				 */
807 				gnp = ld_find_gotndx(&(sdp->sd_GOTndxs), gref,
808 				    ofl, 0);
809 				assert(gnp);
810 
811 				if (arsp->rel_rtype == R_386_TLS_DTPOFF32)
812 					gotndx = gnp->gn_gotndx + 1;
813 				else
814 					gotndx = gnp->gn_gotndx;
815 
816 				R1addr = (Xword)(gotndx * M_GOT_ENTSIZE);
817 
818 				/*
819 				 * Add the GOTs data's offset.
820 				 */
821 				R2addr = R1addr + (uintptr_t)
822 				    arsp->rel_osdesc->os_outdata->d_buf;
823 
824 				DBG_CALL(Dbg_reloc_doact(ofl->ofl_lml,
825 				    ELF_DBG_LD, M_MACH, SHT_REL,
826 				    arsp->rel_rtype, R1addr, value,
827 				    arsp->rel_sname, arsp->rel_osdesc));
828 
829 				/*
830 				 * And do it.
831 				 */
832 				*(Xword *)R2addr = value;
833 				continue;
834 
835 			} else if (IS_GOT_BASED(arsp->rel_rtype) &&
836 			    ((flags & FLG_OF_RELOBJ) == 0)) {
837 				value -= ofl->ofl_osgot->os_shdr->sh_addr;
838 
839 			} else if (IS_GOT_PC(arsp->rel_rtype) &&
840 			    ((flags & FLG_OF_RELOBJ) == 0)) {
841 				value =
842 				    (Xword)(ofl->ofl_osgot->os_shdr->sh_addr) -
843 				    refaddr;
844 
845 			} else if ((IS_PC_RELATIVE(arsp->rel_rtype)) &&
846 			    (((flags & FLG_OF_RELOBJ) == 0) ||
847 			    (arsp->rel_osdesc == sdp->sd_isc->is_osdesc))) {
848 				value -= refaddr;
849 
850 			} else if (IS_TLS_INS(arsp->rel_rtype) &&
851 			    IS_GOT_RELATIVE(arsp->rel_rtype) &&
852 			    ((flags & FLG_OF_RELOBJ) == 0)) {
853 				Gotndx	*gnp;
854 
855 				gnp = ld_find_gotndx(&(sdp->sd_GOTndxs), gref,
856 				    ofl, 0);
857 				assert(gnp);
858 				value = (Xword)gnp->gn_gotndx * M_GOT_ENTSIZE;
859 				if (arsp->rel_rtype == R_386_TLS_IE) {
860 					value +=
861 					    ofl->ofl_osgot->os_shdr->sh_addr;
862 				}
863 
864 			} else if (IS_GOT_RELATIVE(arsp->rel_rtype) &&
865 			    ((flags & FLG_OF_RELOBJ) == 0)) {
866 				Gotndx *gnp;
867 
868 				gnp = ld_find_gotndx(&(sdp->sd_GOTndxs),
869 				    GOT_REF_GENERIC, ofl, 0);
870 				assert(gnp);
871 				value = (Xword)gnp->gn_gotndx * M_GOT_ENTSIZE;
872 
873 			} else if ((arsp->rel_flags & FLG_REL_STLS) &&
874 			    ((flags & FLG_OF_RELOBJ) == 0)) {
875 				Xword	tlsstatsize;
876 
877 				/*
878 				 * This is the LE TLS reference model.  Static
879 				 * offset is hard-coded.
880 				 */
881 				tlsstatsize =
882 				    S_ROUND(ofl->ofl_tlsphdr->p_memsz,
883 				    M_TLSSTATALIGN);
884 				value = tlsstatsize - value;
885 
886 				/*
887 				 * Since this code is fixed up, it assumes a
888 				 * negative offset that can be added to the
889 				 * thread pointer.
890 				 */
891 				if ((arsp->rel_rtype == R_386_TLS_LDO_32) ||
892 				    (arsp->rel_rtype == R_386_TLS_LE))
893 					value = -value;
894 			}
895 
896 			if (arsp->rel_isdesc->is_file)
897 				ifl_name = arsp->rel_isdesc->is_file->ifl_name;
898 			else
899 				ifl_name = MSG_INTL(MSG_STR_NULL);
900 
901 			/*
902 			 * Make sure we have data to relocate.  Compiler and
903 			 * assembler developers have been known to generate
904 			 * relocations against invalid sections (normally .bss),
905 			 * so for their benefit give them sufficient information
906 			 * to help analyze the problem.  End users should never
907 			 * see this.
908 			 */
909 			if (arsp->rel_isdesc->is_indata->d_buf == 0) {
910 				eprintf(ofl->ofl_lml, ERR_FATAL,
911 				    MSG_INTL(MSG_REL_EMPTYSEC),
912 				    conv_reloc_386_type(arsp->rel_rtype, 0),
913 				    ifl_name, demangle(arsp->rel_sname),
914 				    arsp->rel_isdesc->is_name);
915 				return (S_ERROR);
916 			}
917 
918 			/*
919 			 * Get the address of the data item we need to modify.
920 			 */
921 			addr = (uchar_t *)((uintptr_t)arsp->rel_roffset +
922 			    (uintptr_t)_elf_getxoff(arsp->rel_isdesc->
923 			    is_indata));
924 
925 			DBG_CALL(Dbg_reloc_doact(ofl->ofl_lml, ELF_DBG_LD,
926 			    M_MACH, SHT_REL, arsp->rel_rtype, EC_NATPTR(addr),
927 			    value, arsp->rel_sname, arsp->rel_osdesc));
928 			addr += (uintptr_t)arsp->rel_osdesc->os_outdata->d_buf;
929 
930 			if ((((uintptr_t)addr - (uintptr_t)ofl->ofl_nehdr) >
931 			    ofl->ofl_size) || (arsp->rel_roffset >
932 			    arsp->rel_osdesc->os_shdr->sh_size)) {
933 				int	class;
934 
935 				if (((uintptr_t)addr -
936 				    (uintptr_t)ofl->ofl_nehdr) > ofl->ofl_size)
937 					class = ERR_FATAL;
938 				else
939 					class = ERR_WARNING;
940 
941 				eprintf(ofl->ofl_lml, class,
942 				    MSG_INTL(MSG_REL_INVALOFFSET),
943 				    conv_reloc_386_type(arsp->rel_rtype, 0),
944 				    ifl_name, arsp->rel_isdesc->is_name,
945 				    demangle(arsp->rel_sname),
946 				    EC_ADDR((uintptr_t)addr -
947 				    (uintptr_t)ofl->ofl_nehdr));
948 
949 				if (class == ERR_FATAL) {
950 					return_code = S_ERROR;
951 					continue;
952 				}
953 			}
954 
955 			/*
956 			 * The relocation is additive.  Ignore the previous
957 			 * symbol value if this local partial symbol is
958 			 * expanded.
959 			 */
960 			if (moved)
961 				value -= *addr;
962 
963 			/*
964 			 * If '-z noreloc' is specified - skip the do_reloc
965 			 * stage.
966 			 */
967 			if ((flags & FLG_OF_RELOBJ) ||
968 			    !(dtflags1 & DF_1_NORELOC)) {
969 				if (do_reloc((uchar_t)arsp->rel_rtype, addr,
970 				    &value, arsp->rel_sname, ifl_name,
971 				    ofl->ofl_lml) == 0)
972 					return_code = S_ERROR;
973 			}
974 		}
975 	}
976 	return (return_code);
977 }
978 
979 /*
980  * Add an output relocation record.
981  */
982 uintptr_t
983 ld_add_outrel(Word flags, Rel_desc *rsp, Ofl_desc *ofl)
984 {
985 	Rel_desc	*orsp;
986 	Rel_cache	*rcp;
987 	Sym_desc	*sdp = rsp->rel_sym;
988 
989 	/*
990 	 * Static executables *do not* want any relocations against them.
991 	 * Since our engine still creates relocations against a WEAK UNDEFINED
992 	 * symbol in a static executable, it's best to disable them here
993 	 * instead of through out the relocation code.
994 	 */
995 	if ((ofl->ofl_flags & (FLG_OF_STATIC | FLG_OF_EXEC)) ==
996 	    (FLG_OF_STATIC | FLG_OF_EXEC))
997 		return (1);
998 
999 	/*
1000 	 * If no relocation cache structures are available allocate
1001 	 * a new one and link it into the cache list.
1002 	 */
1003 	if ((ofl->ofl_outrels.tail == 0) ||
1004 	    ((rcp = (Rel_cache *)ofl->ofl_outrels.tail->data) == 0) ||
1005 	    ((orsp = rcp->rc_free) == rcp->rc_end)) {
1006 		static size_t	nextsize = 0;
1007 		size_t		size;
1008 
1009 		/*
1010 		 * Output relocation numbers can vary considerably between
1011 		 * building executables or shared objects (pic vs. non-pic),
1012 		 * etc.  But, they typically aren't very large, so for these
1013 		 * objects use a standard bucket size.  For building relocatable
1014 		 * objects, typically there will be an output relocation for
1015 		 * every input relocation.
1016 		 */
1017 		if (nextsize == 0) {
1018 			if (ofl->ofl_flags & FLG_OF_RELOBJ) {
1019 				if ((size = ofl->ofl_relocincnt) == 0)
1020 					size = REL_LOIDESCNO;
1021 				if (size > REL_HOIDESCNO)
1022 					nextsize = REL_HOIDESCNO;
1023 				else
1024 					nextsize = REL_LOIDESCNO;
1025 			} else
1026 				nextsize = size = REL_HOIDESCNO;
1027 		} else
1028 			size = nextsize;
1029 
1030 		size = size * sizeof (Rel_desc);
1031 
1032 		if (((rcp = libld_malloc(sizeof (Rel_cache) + size)) == 0) ||
1033 		    (list_appendc(&ofl->ofl_outrels, rcp) == 0))
1034 			return (S_ERROR);
1035 
1036 		/* LINTED */
1037 		rcp->rc_free = orsp = (Rel_desc *)(rcp + 1);
1038 		/* LINTED */
1039 		rcp->rc_end = (Rel_desc *)((char *)rcp->rc_free + size);
1040 	}
1041 
1042 	/*
1043 	 * If we are adding a output relocation against a section
1044 	 * symbol (non-RELATIVE) then mark that section.  These sections
1045 	 * will be added to the .dynsym symbol table.
1046 	 */
1047 	if (sdp && (rsp->rel_rtype != M_R_RELATIVE) &&
1048 	    ((flags & FLG_REL_SCNNDX) ||
1049 	    (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION))) {
1050 
1051 		/*
1052 		 * If this is a COMMON symbol - no output section
1053 		 * exists yet - (it's created as part of sym_validate()).
1054 		 * So - we mark here that when it's created it should
1055 		 * be tagged with the FLG_OS_OUTREL flag.
1056 		 */
1057 		if ((sdp->sd_flags & FLG_SY_SPECSEC) &&
1058 		    (sdp->sd_sym->st_shndx == SHN_COMMON)) {
1059 			if (ELF_ST_TYPE(sdp->sd_sym->st_info) != STT_TLS)
1060 				ofl->ofl_flags1 |= FLG_OF1_BSSOREL;
1061 			else
1062 				ofl->ofl_flags1 |= FLG_OF1_TLSOREL;
1063 		} else {
1064 			Os_desc	*osp = sdp->sd_isc->is_osdesc;
1065 
1066 			if ((osp->os_flags & FLG_OS_OUTREL) == 0) {
1067 				ofl->ofl_dynshdrcnt++;
1068 				osp->os_flags |= FLG_OS_OUTREL;
1069 			}
1070 		}
1071 	}
1072 
1073 	*orsp = *rsp;
1074 	orsp->rel_flags |= flags;
1075 
1076 	rcp->rc_free++;
1077 	ofl->ofl_outrelscnt++;
1078 
1079 	if (flags & FLG_REL_GOT)
1080 		ofl->ofl_relocgotsz += (Xword)sizeof (Rel);
1081 	else if (flags & FLG_REL_PLT)
1082 		ofl->ofl_relocpltsz += (Xword)sizeof (Rel);
1083 	else if (flags & FLG_REL_BSS)
1084 		ofl->ofl_relocbsssz += (Xword)sizeof (Rel);
1085 	else if (flags & FLG_REL_NOINFO)
1086 		ofl->ofl_relocrelsz += (Xword)sizeof (Rel);
1087 	else
1088 		orsp->rel_osdesc->os_szoutrels += (Xword)sizeof (Rel);
1089 
1090 	if (orsp->rel_rtype == M_R_RELATIVE)
1091 		ofl->ofl_relocrelcnt++;
1092 
1093 	/*
1094 	 * We don't perform sorting on PLT relocations because
1095 	 * they have already been assigned a PLT index and if we
1096 	 * were to sort them we would have to re-assign the plt indexes.
1097 	 */
1098 	if (!(flags & FLG_REL_PLT))
1099 		ofl->ofl_reloccnt++;
1100 
1101 	/*
1102 	 * Insure a GLOBAL_OFFSET_TABLE is generated if required.
1103 	 */
1104 	if (IS_GOT_REQUIRED(orsp->rel_rtype))
1105 		ofl->ofl_flags |= FLG_OF_BLDGOT;
1106 
1107 	/*
1108 	 * Identify and possibly warn of a displacement relocation.
1109 	 */
1110 	if (orsp->rel_flags & FLG_REL_DISP) {
1111 		ofl->ofl_dtflags_1 |= DF_1_DISPRELPND;
1112 
1113 		if (ofl->ofl_flags & FLG_OF_VERBOSE)
1114 			ld_disp_errmsg(MSG_INTL(MSG_REL_DISPREL4), orsp, ofl);
1115 	}
1116 	DBG_CALL(Dbg_reloc_ors_entry(ofl->ofl_lml, ELF_DBG_LD, SHT_REL,
1117 	    M_MACH, orsp));
1118 	return (1);
1119 }
1120 
1121 /*
1122  * Stub routine since register symbols are not supported on i386.
1123  */
1124 /* ARGSUSED */
1125 uintptr_t
1126 ld_reloc_register(Rel_desc * rsp, Is_desc * isp, Ofl_desc * ofl)
1127 {
1128 	eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_REL_NOREG));
1129 	return (S_ERROR);
1130 }
1131 
1132 /*
1133  * process relocation for a LOCAL symbol
1134  */
1135 uintptr_t
1136 ld_reloc_local(Rel_desc * rsp, Ofl_desc * ofl)
1137 {
1138 	Word		flags = ofl->ofl_flags;
1139 	Sym_desc	*sdp = rsp->rel_sym;
1140 	Word		shndx = sdp->sd_sym->st_shndx;
1141 
1142 	/*
1143 	 * if ((shared object) and (not pc relative relocation) and
1144 	 *    (not against ABS symbol))
1145 	 * then
1146 	 *	build R_386_RELATIVE
1147 	 * fi
1148 	 */
1149 	if ((flags & FLG_OF_SHAROBJ) && (rsp->rel_flags & FLG_REL_LOAD) &&
1150 	    !(IS_PC_RELATIVE(rsp->rel_rtype)) &&
1151 	    !(IS_GOT_BASED(rsp->rel_rtype)) &&
1152 	    !(rsp->rel_isdesc != NULL &&
1153 	    (rsp->rel_isdesc->is_shdr->sh_type == SHT_SUNW_dof)) &&
1154 	    (((sdp->sd_flags & FLG_SY_SPECSEC) == 0) ||
1155 	    (shndx != SHN_ABS) || (sdp->sd_aux && sdp->sd_aux->sa_symspec))) {
1156 		Word	ortype = rsp->rel_rtype;
1157 
1158 		rsp->rel_rtype = R_386_RELATIVE;
1159 		if (ld_add_outrel(NULL, rsp, ofl) == S_ERROR)
1160 			return (S_ERROR);
1161 		rsp->rel_rtype = ortype;
1162 	}
1163 
1164 	/*
1165 	 * If the relocation is against a 'non-allocatable' section
1166 	 * and we can not resolve it now - then give a warning
1167 	 * message.
1168 	 *
1169 	 * We can not resolve the symbol if either:
1170 	 *	a) it's undefined
1171 	 *	b) it's defined in a shared library and a
1172 	 *	   COPY relocation hasn't moved it to the executable
1173 	 *
1174 	 * Note: because we process all of the relocations against the
1175 	 *	text segment before any others - we know whether
1176 	 *	or not a copy relocation will be generated before
1177 	 *	we get here (see reloc_init()->reloc_segments()).
1178 	 */
1179 	if (!(rsp->rel_flags & FLG_REL_LOAD) &&
1180 	    ((shndx == SHN_UNDEF) ||
1181 	    ((sdp->sd_ref == REF_DYN_NEED) &&
1182 	    ((sdp->sd_flags & FLG_SY_MVTOCOMM) == 0)))) {
1183 		/*
1184 		 * If the relocation is against a SHT_SUNW_ANNOTATE
1185 		 * section - then silently ignore that the relocation
1186 		 * can not be resolved.
1187 		 */
1188 		if (rsp->rel_osdesc &&
1189 		    (rsp->rel_osdesc->os_shdr->sh_type == SHT_SUNW_ANNOTATE))
1190 			return (0);
1191 		eprintf(ofl->ofl_lml, ERR_WARNING, MSG_INTL(MSG_REL_EXTERNSYM),
1192 		    conv_reloc_386_type(rsp->rel_rtype, 0),
1193 		    rsp->rel_isdesc->is_file->ifl_name,
1194 		    demangle(rsp->rel_sname), rsp->rel_osdesc->os_name);
1195 		return (1);
1196 	}
1197 
1198 	/*
1199 	 * Perform relocation.
1200 	 */
1201 	return (ld_add_actrel(NULL, rsp, ofl));
1202 }
1203 
1204 uintptr_t
1205 /* ARGSUSED */
1206 ld_reloc_GOTOP(Boolean local, Rel_desc * rsp, Ofl_desc * ofl)
1207 {
1208 	/*
1209 	 * Stub routine for common code compatibility, we shouldn't
1210 	 * actually get here on x86.
1211 	 */
1212 	assert(0);
1213 	return (S_ERROR);
1214 }
1215 
1216 uintptr_t
1217 ld_reloc_TLS(Boolean local, Rel_desc * rsp, Ofl_desc * ofl)
1218 {
1219 	Word		rtype = rsp->rel_rtype;
1220 	Sym_desc	*sdp = rsp->rel_sym;
1221 	Word		flags = ofl->ofl_flags;
1222 	Word		rflags;
1223 	Gotndx		*gnp;
1224 
1225 	/*
1226 	 * all TLS relocations are illegal in a static executable.
1227 	 */
1228 	if ((ofl->ofl_flags & (FLG_OF_STATIC | FLG_OF_EXEC)) ==
1229 	    (FLG_OF_STATIC | FLG_OF_EXEC)) {
1230 		eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_REL_TLSSTAT),
1231 		    conv_reloc_386_type(rsp->rel_rtype, 0),
1232 		    rsp->rel_isdesc->is_file->ifl_name,
1233 		    demangle(rsp->rel_sname));
1234 		return (S_ERROR);
1235 	}
1236 
1237 	/*
1238 	 * Any TLS relocation must be against a STT_TLS symbol, all others
1239 	 * are illegal.
1240 	 */
1241 	if (ELF_ST_TYPE(sdp->sd_sym->st_info) != STT_TLS) {
1242 		Ifl_desc	*ifl = rsp->rel_isdesc->is_file;
1243 
1244 		eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_REL_TLSBADSYM),
1245 		    conv_reloc_386_type(rsp->rel_rtype, 0),
1246 		    ifl->ifl_name, demangle(rsp->rel_sname),
1247 		    conv_sym_info_type(ifl->ifl_ehdr->e_machine,
1248 		    ELF_ST_TYPE(sdp->sd_sym->st_info), 0));
1249 		return (S_ERROR);
1250 	}
1251 
1252 	/*
1253 	 * We're a executable - use either the IE or LE
1254 	 * access model.
1255 	 */
1256 	if (flags & FLG_OF_EXEC) {
1257 		/*
1258 		 * If we are using either IE or LE reference
1259 		 * model set the DF_STATIC_TLS flag.
1260 		 */
1261 		ofl->ofl_dtflags |= DF_STATIC_TLS;
1262 
1263 		if (!local) {
1264 			Gotref	gref;
1265 			/*
1266 			 * IE access model
1267 			 */
1268 			/*
1269 			 * It's not possible for LD or LE reference
1270 			 * models to reference a symbol external to
1271 			 * the current object.
1272 			 */
1273 			if (IS_TLS_LD(rtype) || IS_TLS_LE(rtype)) {
1274 				eprintf(ofl->ofl_lml, ERR_FATAL,
1275 				    MSG_INTL(MSG_REL_TLSBND),
1276 				    conv_reloc_386_type(rsp->rel_rtype, 0),
1277 				    rsp->rel_isdesc->is_file->ifl_name,
1278 				    demangle(rsp->rel_sname),
1279 				    sdp->sd_file->ifl_name);
1280 				return (S_ERROR);
1281 			}
1282 
1283 			gref = GOT_REF_TLSIE;
1284 
1285 			/*
1286 			 * Assign a GOT entry for static TLS references
1287 			 */
1288 			if ((gnp = ld_find_gotndx(&(sdp->sd_GOTndxs),
1289 			    gref, ofl, 0)) == 0) {
1290 				if (ld_assign_gotndx(&(sdp->sd_GOTndxs),
1291 				    gnp, gref, ofl, rsp, sdp) == S_ERROR)
1292 					return (S_ERROR);
1293 				rsp->rel_rtype = R_386_TLS_TPOFF;
1294 				if (ld_add_outrel((FLG_REL_GOT | FLG_REL_STLS),
1295 				    rsp, ofl) == S_ERROR)
1296 					return (S_ERROR);
1297 				rsp->rel_rtype = rtype;
1298 			}
1299 			if (IS_TLS_IE(rtype))
1300 				return (ld_add_actrel(FLG_REL_STLS, rsp, ofl));
1301 
1302 			/*
1303 			 * If (GD or LD) reference models - fixups
1304 			 * are required.
1305 			 */
1306 			return (ld_add_actrel((FLG_REL_TLSFIX | FLG_REL_STLS),
1307 			    rsp, ofl));
1308 		}
1309 		/*
1310 		 * LE access model
1311 		 */
1312 		if (IS_TLS_LE(rtype) || (rtype == R_386_TLS_LDO_32))
1313 			return (ld_add_actrel(FLG_REL_STLS, rsp, ofl));
1314 
1315 		return (ld_add_actrel((FLG_REL_TLSFIX | FLG_REL_STLS),
1316 		    rsp, ofl));
1317 	}
1318 
1319 	/*
1320 	 * Building a shared object
1321 	 */
1322 
1323 	/*
1324 	 * Building a shared object - only GD & LD access models
1325 	 * will work here.
1326 	 */
1327 	if (IS_TLS_IE(rtype) || IS_TLS_LE(rtype)) {
1328 		eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_REL_TLSIE),
1329 		    conv_reloc_386_type(rsp->rel_rtype, 0),
1330 		    rsp->rel_isdesc->is_file->ifl_name,
1331 		    demangle(rsp->rel_sname));
1332 		return (S_ERROR);
1333 	}
1334 
1335 	/*
1336 	 * LD access mode can only bind to local symbols.
1337 	 */
1338 	if (!local && IS_TLS_LD(rtype)) {
1339 		eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_REL_TLSBND),
1340 		    conv_reloc_386_type(rsp->rel_rtype, 0),
1341 		    rsp->rel_isdesc->is_file->ifl_name,
1342 		    demangle(rsp->rel_sname),
1343 		    sdp->sd_file->ifl_name);
1344 		return (S_ERROR);
1345 	}
1346 
1347 
1348 	if (IS_TLS_LD(rtype) && ((gnp = ld_find_gotndx(&(sdp->sd_GOTndxs),
1349 	    GOT_REF_TLSLD, ofl, 0)) == 0)) {
1350 		if (ld_assign_gotndx(&(sdp->sd_GOTndxs), gnp, GOT_REF_TLSLD,
1351 		    ofl, rsp, sdp) == S_ERROR)
1352 			return (S_ERROR);
1353 		rflags = FLG_REL_GOT | FLG_REL_MTLS;
1354 		if (local)
1355 			rflags |= FLG_REL_SCNNDX;
1356 		rsp->rel_rtype = R_386_TLS_DTPMOD32;
1357 		if (ld_add_outrel(rflags, rsp, ofl) == S_ERROR)
1358 			return (S_ERROR);
1359 		rsp->rel_rtype = rtype;
1360 	} else if (IS_TLS_GD(rtype) &&
1361 	    ((gnp = ld_find_gotndx(&(sdp->sd_GOTndxs), GOT_REF_TLSGD,
1362 	    ofl, 0)) == 0)) {
1363 		if (ld_assign_gotndx(&(sdp->sd_GOTndxs), gnp, GOT_REF_TLSGD,
1364 		    ofl, rsp, sdp) == S_ERROR)
1365 			return (S_ERROR);
1366 		rflags = FLG_REL_GOT | FLG_REL_DTLS;
1367 		if (local)
1368 			rflags |= FLG_REL_SCNNDX;
1369 		rsp->rel_rtype = R_386_TLS_DTPMOD32;
1370 		if (ld_add_outrel(rflags, rsp, ofl) == S_ERROR)
1371 			return (S_ERROR);
1372 		if (local == TRUE) {
1373 			rsp->rel_rtype = R_386_TLS_DTPOFF32;
1374 			if (ld_add_actrel((FLG_REL_GOT | FLG_REL_DTLS), rsp,
1375 			    ofl) == S_ERROR)
1376 				return (S_ERROR);
1377 		} else {
1378 			rsp->rel_rtype = R_386_TLS_DTPOFF32;
1379 			if (ld_add_outrel((FLG_REL_GOT | FLG_REL_DTLS), rsp,
1380 			    ofl) == S_ERROR)
1381 				return (S_ERROR);
1382 		}
1383 		rsp->rel_rtype = rtype;
1384 	}
1385 	/*
1386 	 * For GD/LD TLS reference - TLS_{GD,LD}_CALL, this will eventually
1387 	 * cause a call to __tls_get_addr().  Let's convert this
1388 	 * relocation to that symbol now, and prepare for the PLT magic.
1389 	 */
1390 	if ((rtype == R_386_TLS_GD_PLT) || (rtype == R_386_TLS_LDM_PLT)) {
1391 		Sym_desc *	tlsgetsym;
1392 
1393 		if ((tlsgetsym = ld_sym_add_u(MSG_ORIG(MSG_SYM_TLSGETADDR_UU),
1394 		    ofl)) == (Sym_desc *)S_ERROR)
1395 			return (S_ERROR);
1396 		rsp->rel_sym = tlsgetsym;
1397 		rsp->rel_sname = tlsgetsym->sd_name;
1398 		rsp->rel_rtype = R_386_PLT32;
1399 		if (ld_reloc_plt(rsp, ofl) == S_ERROR)
1400 			return (S_ERROR);
1401 		rsp->rel_sym = sdp;
1402 		rsp->rel_sname = sdp->sd_name;
1403 		rsp->rel_rtype = rtype;
1404 		return (1);
1405 	}
1406 
1407 	if (IS_TLS_LD(rtype))
1408 		return (ld_add_actrel(FLG_REL_MTLS, rsp, ofl));
1409 
1410 	return (ld_add_actrel(FLG_REL_DTLS, rsp, ofl));
1411 }
1412 
1413 /* ARGSUSED3 */
1414 Gotndx *
1415 ld_find_gotndx(List * lst, Gotref gref, Ofl_desc * ofl, Rel_desc * rdesc)
1416 {
1417 	Listnode *	lnp;
1418 	Gotndx *	gnp;
1419 
1420 	if ((gref == GOT_REF_TLSLD) && ofl->ofl_tlsldgotndx)
1421 		return (ofl->ofl_tlsldgotndx);
1422 
1423 	for (LIST_TRAVERSE(lst, lnp, gnp)) {
1424 		if (gnp->gn_gotref == gref)
1425 			return (gnp);
1426 	}
1427 	return ((Gotndx *)0);
1428 }
1429 
1430 Xword
1431 ld_calc_got_offset(Rel_desc * rdesc, Ofl_desc * ofl)
1432 {
1433 	Os_desc		*osp = ofl->ofl_osgot;
1434 	Sym_desc	*sdp = rdesc->rel_sym;
1435 	Xword		gotndx;
1436 	Gotref		gref;
1437 	Gotndx		*gnp;
1438 
1439 	if (rdesc->rel_flags & FLG_REL_DTLS)
1440 		gref = GOT_REF_TLSGD;
1441 	else if (rdesc->rel_flags & FLG_REL_MTLS)
1442 		gref = GOT_REF_TLSLD;
1443 	else if (rdesc->rel_flags & FLG_REL_STLS)
1444 		gref = GOT_REF_TLSIE;
1445 	else
1446 		gref = GOT_REF_GENERIC;
1447 
1448 	gnp = ld_find_gotndx(&(sdp->sd_GOTndxs), gref, ofl, 0);
1449 	assert(gnp);
1450 
1451 	gotndx = (Xword)gnp->gn_gotndx;
1452 
1453 	if ((rdesc->rel_flags & FLG_REL_DTLS) &&
1454 	    (rdesc->rel_rtype == R_386_TLS_DTPOFF32))
1455 		gotndx++;
1456 
1457 	return ((Xword)(osp->os_shdr->sh_addr + (gotndx * M_GOT_ENTSIZE)));
1458 }
1459 
1460 
1461 /* ARGSUSED4 */
1462 uintptr_t
1463 ld_assign_gotndx(List * lst, Gotndx * pgnp, Gotref gref, Ofl_desc * ofl,
1464     Rel_desc * rsp, Sym_desc * sdp)
1465 {
1466 	Gotndx	*gnp;
1467 	uint_t	gotents;
1468 
1469 	if (pgnp)
1470 		return (1);
1471 
1472 	if ((gref == GOT_REF_TLSGD) || (gref == GOT_REF_TLSLD))
1473 		gotents = 2;
1474 	else
1475 		gotents = 1;
1476 
1477 	if ((gnp = libld_calloc(sizeof (Gotndx), 1)) == 0)
1478 		return (S_ERROR);
1479 	gnp->gn_gotndx = ofl->ofl_gotcnt;
1480 	gnp->gn_gotref = gref;
1481 
1482 	ofl->ofl_gotcnt += gotents;
1483 
1484 	if (gref == GOT_REF_TLSLD) {
1485 		ofl->ofl_tlsldgotndx = gnp;
1486 		return (1);
1487 	}
1488 
1489 	if (list_appendc(lst, (void *)gnp) == 0)
1490 		return (S_ERROR);
1491 
1492 	return (1);
1493 }
1494 
1495 void
1496 ld_assign_plt_ndx(Sym_desc * sdp, Ofl_desc *ofl)
1497 {
1498 	sdp->sd_aux->sa_PLTndx = 1 + ofl->ofl_pltcnt++;
1499 	sdp->sd_aux->sa_PLTGOTndx = ofl->ofl_gotcnt++;
1500 	ofl->ofl_flags |= FLG_OF_BLDGOT;
1501 }
1502 
1503 /*
1504  * Initializes .got[0] with the _DYNAMIC symbol value.
1505  */
1506 uintptr_t
1507 ld_fillin_gotplt(Ofl_desc * ofl)
1508 {
1509 	if (ofl->ofl_osgot) {
1510 		Sym_desc *	sdp;
1511 
1512 		if ((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_DYNAMIC_U),
1513 		    SYM_NOHASH, 0, ofl)) != NULL) {
1514 			uchar_t	*genptr = ((uchar_t *)
1515 			    ofl->ofl_osgot->os_outdata->d_buf +
1516 			    (M_GOT_XDYNAMIC * M_GOT_ENTSIZE));
1517 			/* LINTED */
1518 			*(Word *)genptr = (Word)sdp->sd_sym->st_value;
1519 		}
1520 	}
1521 
1522 	/*
1523 	 * Fill in the reserved slot in the procedure linkage table the first
1524 	 * entry is:
1525 	 *  if (building a.out) {
1526 	 *	PUSHL	got[1]		    # the address of the link map entry
1527 	 *	JMP *	got[2]		    # the address of rtbinder
1528 	 *  } else {
1529 	 *	PUSHL	got[1]@GOT(%ebx)    # the address of the link map entry
1530 	 *	JMP *	got[2]@GOT(%ebx)    # the address of rtbinder
1531 	 *  }
1532 	 */
1533 	if ((ofl->ofl_flags & FLG_OF_DYNAMIC) && ofl->ofl_osplt) {
1534 		uchar_t *pltent;
1535 
1536 		pltent = (uchar_t *)ofl->ofl_osplt->os_outdata->d_buf;
1537 		if (!(ofl->ofl_flags & FLG_OF_SHAROBJ)) {
1538 			pltent[0] = M_SPECIAL_INST;
1539 			pltent[1] = M_PUSHL_DISP;
1540 			pltent += 2;
1541 			/* LINTED */
1542 			*(Word *)pltent = (Word)(ofl->ofl_osgot->os_shdr->
1543 				sh_addr + M_GOT_XLINKMAP * M_GOT_ENTSIZE);
1544 			pltent += 4;
1545 			pltent[0] = M_SPECIAL_INST;
1546 			pltent[1] = M_JMP_DISP_IND;
1547 			pltent += 2;
1548 			/* LINTED */
1549 			*(Word *)pltent = (Word)(ofl->ofl_osgot->os_shdr->
1550 				sh_addr + M_GOT_XRTLD * M_GOT_ENTSIZE);
1551 		} else {
1552 			pltent[0] = M_SPECIAL_INST;
1553 			pltent[1] = M_PUSHL_REG_DISP;
1554 			pltent += 2;
1555 			/* LINTED */
1556 			*(Word *)pltent = (Word)(M_GOT_XLINKMAP *
1557 				M_GOT_ENTSIZE);
1558 			pltent += 4;
1559 			pltent[0] = M_SPECIAL_INST;
1560 			pltent[1] = M_JMP_REG_DISP_IND;
1561 			pltent += 2;
1562 			/* LINTED */
1563 			*(Word *)pltent = (Word)(M_GOT_XRTLD *
1564 				M_GOT_ENTSIZE);
1565 		}
1566 	}
1567 	return (1);
1568 }
1569