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