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