xref: /titanic_44/usr/src/cmd/sgs/libld/common/relocate.c (revision 646cf3c6c5f9a3403fe9d15809bbbb2441deb1e2)
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) 1988 AT&T
24  *	  All Rights Reserved
25  *
26  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
27  * Use is subject to license terms.
28  */
29 #pragma ident	"%Z%%M%	%I%	%E% SMI"
30 
31 /*
32  * set-up for relocations
33  */
34 #include	<string.h>
35 #include	<stdio.h>
36 #include	<alloca.h>
37 #include	<reloc.h>
38 #include	<debug.h>
39 #include	"msg.h"
40 #include	"_libld.h"
41 
42 /*
43  * Structure to hold copy relocation items.
44  */
45 typedef struct copy_rel {
46 	Sym_desc *	copyrel_symd;	/* symbol descriptor to be copied */
47 	Addr 		copyrel_stval;	/* Original symbol value */
48 } Copy_rel;
49 
50 /*
51  * For each copy relocation symbol, determine if the symbol is:
52  * 	1) to be *disp* relocated at runtime
53  *	2) a reference symbol for *disp* relocation
54  *	3) possibly *disp* relocated at ld time.
55  *
56  * The first and the second are serious errors.
57  */
58 static void
59 is_disp_copied(Ofl_desc *ofl, Copy_rel *cpy)
60 {
61 	Ifl_desc	*ifl = cpy->copyrel_symd->sd_file;
62 	Sym_desc	*sdp = cpy->copyrel_symd;
63 	Is_desc		*irel;
64 	Addr		symaddr = cpy->copyrel_stval;
65 	Listnode	*lnp1;
66 	Conv_inv_buf_t	inv_buf;
67 
68 	/*
69 	 * This symbol may not be *disp* relocated at run time, but could
70 	 * already have been *disp* relocated when the shared object was
71 	 * created.  Warn the user.
72 	 */
73 	if ((ifl->ifl_flags & FLG_IF_DISPDONE) &&
74 	    (ofl->ofl_flags & FLG_OF_VERBOSE))
75 		eprintf(ofl->ofl_lml, ERR_WARNING, MSG_INTL(MSG_REL_DISPREL2),
76 		    conv_reloc_type(ifl->ifl_ehdr->e_machine, M_R_COPY,
77 		    0, &inv_buf), ifl->ifl_name, demangle(sdp->sd_name));
78 
79 	if ((ifl->ifl_flags & FLG_IF_DISPPEND) == 0)
80 		return;
81 
82 	/*
83 	 * Traverse the input relocation sections.
84 	 */
85 	for (LIST_TRAVERSE(&ifl->ifl_relsect, lnp1, irel)) {
86 		Sym_desc	*rsdp;
87 		Is_desc		*trel;
88 		Rel		*rend, *reloc;
89 		Xword		rsize, entsize;
90 
91 		trel = ifl->ifl_isdesc[irel->is_shdr->sh_info];
92 		rsize = irel->is_shdr->sh_size;
93 		entsize = irel->is_shdr->sh_entsize;
94 		reloc = (Rel *)irel->is_indata->d_buf;
95 
96 		/*
97 		 * Decide entry size
98 		 */
99 		if ((entsize == 0) || (entsize > rsize)) {
100 			if (irel->is_shdr->sh_type == SHT_RELA)
101 				entsize = sizeof (Rela);
102 			else
103 				entsize = sizeof (Rel);
104 		}
105 
106 		/*
107 		 * Traverse the relocation entries.
108 		 */
109 		for (rend = (Rel *)((uintptr_t)reloc + (uintptr_t)rsize);
110 		    reloc < rend;
111 		    reloc = (Rel *)((uintptr_t)reloc + (uintptr_t)entsize)) {
112 			const char	*str;
113 			Word		rstndx;
114 
115 			if (IS_PC_RELATIVE(ELF_R_TYPE(reloc->r_info)) == 0)
116 				continue;
117 
118 			/*
119 			 * First, check if this symbol is reference symbol
120 			 * for this relocation entry.
121 			 */
122 			rstndx = (Word) ELF_R_SYM(reloc->r_info);
123 			rsdp = ifl->ifl_oldndx[rstndx];
124 			if (rsdp == sdp) {
125 				if ((str = demangle(rsdp->sd_name)) !=
126 				    rsdp->sd_name) {
127 					char	*_str = alloca(strlen(str) + 1);
128 					(void) strcpy(_str, str);
129 					str = (const char *)_str;
130 				}
131 				eprintf(ofl->ofl_lml,
132 				    ERR_WARNING, MSG_INTL(MSG_REL_DISPREL1),
133 				    conv_reloc_type(ifl->ifl_ehdr->e_machine,
134 				    (uint_t)ELF_R_TYPE(reloc->r_info),
135 				    0, &inv_buf), ifl->ifl_name, str,
136 				    MSG_INTL(MSG_STR_UNKNOWN),
137 				    EC_XWORD(reloc->r_offset),
138 				    demangle(sdp->sd_name));
139 			}
140 
141 			/*
142 			 * Then check if this relocation entry is relocating
143 			 * this symbol.
144 			 */
145 			if ((sdp->sd_isc != trel) ||
146 			    (reloc->r_offset < symaddr) ||
147 			    (reloc->r_offset >=
148 			    (symaddr + sdp->sd_sym->st_size)))
149 				continue;
150 
151 			/*
152 			 * This symbol is truely *disp* relocated, so should
153 			 * really be fixed by user.
154 			 */
155 			if ((str = demangle(sdp->sd_name)) != sdp->sd_name) {
156 				char	*_str = alloca(strlen(str) + 1);
157 				(void) strcpy(_str, str);
158 				str = (const char *)_str;
159 			}
160 			eprintf(ofl->ofl_lml, ERR_WARNING,
161 			    MSG_INTL(MSG_REL_DISPREL1),
162 			    conv_reloc_type(ifl->ifl_ehdr->e_machine,
163 			    (uint_t)ELF_R_TYPE(reloc->r_info), 0, &inv_buf),
164 			    ifl->ifl_name, demangle(rsdp->sd_name), str,
165 			    EC_XWORD(reloc->r_offset), str);
166 		}
167 	}
168 }
169 
170 /*
171  * The number of symbols provided by some objects can be very large.  Use a
172  * binary search to match the associated value to a symbol table entry.
173  */
174 static int
175 disp_bsearch(const void *key, const void *array)
176 {
177 	Addr		kvalue, avalue;
178 	Ssv_desc	*ssvp = (Ssv_desc *)array;
179 
180 	kvalue = *((Addr *)key);
181 	avalue = ssvp->ssv_value;
182 
183 	if (avalue > kvalue)
184 		return (-1);
185 	if ((avalue < kvalue) &&
186 	    ((avalue + ssvp->ssv_sdp->sd_sym->st_size) <= kvalue))
187 		return (1);
188 	return (0);
189 }
190 
191 /*
192  * Given a sorted list of symbols, look for a symbol in which the relocation
193  * offset falls between the [sym.st_value - sym.st_value + sym.st_size].  Since
194  * the symbol list is maintained in sorted order,  we can bail once the
195  * relocation offset becomes less than the symbol values.  The symbol is
196  * returned for use in error diagnostics.
197  */
198 static Sym_desc *
199 disp_scansyms(Ifl_desc * ifl, Rel_desc *rld, Boolean rlocal, int inspect,
200     Ofl_desc *ofl)
201 {
202 	Sym_desc	*tsdp, *rsdp;
203 	Sym		*rsym, *tsym;
204 	Ssv_desc	*ssvp;
205 	uchar_t		rtype, ttype;
206 	Addr		value;
207 
208 	/*
209 	 * Sorted symbol values have been uniquified by adding their associated
210 	 * section offset.  Uniquify the relocation offset by adding its
211 	 * associated section offset, and search for the symbol.
212 	 */
213 	value = rld->rel_roffset;
214 	if (rld->rel_isdesc->is_shdr)
215 		value += rld->rel_isdesc->is_shdr->sh_offset;
216 
217 	if ((ssvp = bsearch((void *)&value, (void *)ifl->ifl_sortsyms,
218 	    ifl->ifl_sortcnt, sizeof (Ssv_desc), &disp_bsearch)) != 0)
219 		tsdp = ssvp->ssv_sdp;
220 	else
221 		tsdp = 0;
222 
223 	if (inspect)
224 		return (tsdp);
225 
226 	/*
227 	 * Determine the relocation reference symbol and its type.
228 	 */
229 	rsdp = rld->rel_sym;
230 	rsym = rsdp->sd_sym;
231 	rtype = ELF_ST_TYPE(rsym->st_info);
232 
233 	/*
234 	 * If there is no target symbol to match the relocation offset, then the
235 	 * offset is effectively local data.  If the relocation symbol is global
236 	 * data we have a potential for this displacement relocation to be
237 	 * invalidated should the global symbol be copied.
238 	 */
239 	if (tsdp == 0) {
240 		if ((rlocal == TRUE) ||
241 		    ((rtype != STT_OBJECT) && (rtype != STT_SECTION)))
242 		return (tsdp);
243 	} else {
244 		/*
245 		 * If both symbols are local, no copy relocations can occur to
246 		 * either symbol.  Note, this test is very similar to the test
247 		 * used in ld_sym_adjust_vis().
248 		 */
249 		if ((rlocal == TRUE) &&
250 		    ((tsdp->sd_flags1 & FLG_SY1_HIDDEN) ||
251 		    (ELF_ST_BIND(tsdp->sd_sym->st_info) != STB_GLOBAL) ||
252 		    (((ofl->ofl_flags & FLG_OF_AUTOLCL) ||
253 		    (ofl->ofl_flags1 & FLG_OF1_AUTOELM)) &&
254 		    ((tsdp->sd_flags1 & MSK_SY1_NOAUTO) == 0))))
255 			return (tsdp);
256 
257 		/*
258 		 * Determine the relocation target symbols type.
259 		 */
260 		tsym = tsdp->sd_sym;
261 		ttype = ELF_ST_TYPE(tsym->st_info);
262 
263 		/*
264 		 * If the reference symbol is local, and the target isn't a
265 		 * data element, then no copy relocations can occur to either
266 		 * symbol.  Note, this catches pc-relative relocations against
267 		 * the _GLOBAL_OFFSET_TABLE_, which is effectively treated as
268 		 * a local symbol.
269 		 */
270 		if ((rlocal == TRUE) && (ttype != STT_OBJECT) &&
271 		    (ttype != STT_SECTION))
272 			return (tsdp);
273 
274 		/*
275 		 * Finally, one of the symbols must reference a data element.
276 		 */
277 		if ((rtype != STT_OBJECT) && (rtype != STT_SECTION) &&
278 		    (ttype != STT_OBJECT) && (ttype != STT_SECTION))
279 			return (tsdp);
280 	}
281 
282 	/*
283 	 * We have two global symbols, at least one of which is a data item.
284 	 * The last case where a displacement relocation can be ignored, is
285 	 * if the reference symbol is included in the target symbol.
286 	 */
287 	value = rsym->st_value;
288 	value += rld->rel_raddend;
289 
290 	if ((rld->rel_roffset >= value) &&
291 	    (rld->rel_roffset < (value + rsym->st_size)))
292 		return (tsdp);
293 
294 	/*
295 	 * We have a displacement relocation that could be compromised by a
296 	 * copy relocation of one of the associated data items.
297 	 */
298 	rld->rel_flags |= FLG_REL_DISP;
299 	return (tsdp);
300 }
301 
302 void
303 ld_disp_errmsg(const char *msg, Rel_desc *rsp, Ofl_desc *ofl)
304 {
305 	Sym_desc	*sdp;
306 	const char	*str;
307 	Ifl_desc	*ifl = rsp->rel_isdesc->is_file;
308 	Conv_inv_buf_t	inv_buf;
309 
310 	if ((sdp = disp_scansyms(ifl, rsp, 0, 1, ofl)) != 0)
311 		str = demangle(sdp->sd_name);
312 	else
313 		str = MSG_INTL(MSG_STR_UNKNOWN);
314 
315 	eprintf(ofl->ofl_lml, ERR_WARNING, msg,
316 	    conv_reloc_type(ifl->ifl_ehdr->e_machine, rsp->rel_rtype,
317 	    0, &inv_buf), ifl->ifl_name, rsp->rel_sname, str,
318 	    EC_OFF(rsp->rel_roffset));
319 }
320 
321 /*
322  * qsort(3C) comparison routine used for the disp_sortsyms().
323  */
324 static int
325 disp_qsort(const void * s1, const void * s2)
326 {
327 	Ssv_desc	*ssvp1 = ((Ssv_desc *)s1);
328 	Ssv_desc	*ssvp2 = ((Ssv_desc *)s2);
329 	Addr		val1 = ssvp1->ssv_value;
330 	Addr		val2 = ssvp2->ssv_value;
331 
332 	if (val1 > val2)
333 		return (1);
334 	if (val1 < val2)
335 		return (-1);
336 	return (0);
337 }
338 
339 /*
340  * Determine whether a displacement relocation is between a local and global
341  * symbol pair.  One symbol is used to perform the relocation, and the other
342  * is the destination offset of the relocation.
343  */
344 static uintptr_t
345 disp_inspect(Ofl_desc *ofl, Rel_desc *rld, Boolean rlocal)
346 {
347 	Is_desc		*isp = rld->rel_isdesc;
348 	Ifl_desc	*ifl = rld->rel_isdesc->is_file;
349 
350 	/*
351 	 * If the input files symbols haven't been sorted yet, do so.
352 	 */
353 	if (ifl->ifl_sortsyms == 0) {
354 		Word	ondx, nndx;
355 
356 		if ((ifl->ifl_sortsyms = libld_malloc((ifl->ifl_symscnt + 1) *
357 		    sizeof (Ssv_desc))) == 0)
358 			return (S_ERROR);
359 
360 		for (ondx = 0, nndx = 0; ondx < ifl->ifl_symscnt; ondx++) {
361 			Sym_desc	*sdp;
362 			Addr		value;
363 
364 			/*
365 			 * As symbol resolution has already occurred, various
366 			 * symbols from this object may have been satisfied
367 			 * from other objects.  Only select symbols from this
368 			 * object.  For the displacement test, we only really
369 			 * need to observe data definitions, however, later as
370 			 * part of providing warning disgnostics, relating the
371 			 * relocation offset to a symbol is desirable.  Thus,
372 			 * collect all symbols that define a memory area.
373 			 */
374 			if (((sdp = ifl->ifl_oldndx[ondx]) == 0) ||
375 			    (sdp->sd_sym->st_shndx == SHN_UNDEF) ||
376 			    (sdp->sd_sym->st_shndx >= SHN_LORESERVE) ||
377 			    (sdp->sd_ref != REF_REL_NEED) ||
378 			    (sdp->sd_file != ifl) ||
379 			    (sdp->sd_sym->st_size == 0))
380 				continue;
381 
382 			/*
383 			 * As a further optimization for later checking, mark
384 			 * this section if this a global data definition.
385 			 */
386 			if (sdp->sd_isc && (ondx >= ifl->ifl_locscnt))
387 				sdp->sd_isc->is_flags |= FLG_IS_GDATADEF;
388 
389 			/*
390 			 * Capture the symbol.  Within relocatable objects, a
391 			 * symbols value is its offset within its associated
392 			 * section.  Add the section offset to this value to
393 			 * uniquify the symbol.
394 			 */
395 			value = sdp->sd_sym->st_value;
396 			if (sdp->sd_isc && sdp->sd_isc->is_shdr)
397 				value += sdp->sd_isc->is_shdr->sh_offset;
398 
399 			ifl->ifl_sortsyms[nndx].ssv_value = value;
400 			ifl->ifl_sortsyms[nndx].ssv_sdp = sdp;
401 			nndx++;
402 		}
403 
404 		/*
405 		 * Sort the list based on the symbols value (address).
406 		 */
407 		if ((ifl->ifl_sortcnt = nndx) != 0)
408 			qsort(ifl->ifl_sortsyms, nndx, sizeof (Ssv_desc),
409 			    &disp_qsort);
410 	}
411 
412 	/*
413 	 * If the reference symbol is local, and the section being relocated
414 	 * contains no global definitions, neither can be the target of a copy
415 	 * relocation.
416 	 */
417 	if ((rlocal == FALSE) && ((isp->is_flags & FLG_IS_GDATADEF) == 0))
418 		return (1);
419 
420 	/*
421 	 * Otherwise determine whether this relocation symbol and its offset
422 	 * could be candidates for a copy relocation.
423 	 */
424 	if (ifl->ifl_sortcnt)
425 		(void) disp_scansyms(ifl, rld, rlocal, 0, ofl);
426 	return (1);
427 }
428 
429 /*
430  * Add an active relocation record.
431  */
432 uintptr_t
433 ld_add_actrel(Word flags, Rel_desc *rsp, Ofl_desc *ofl)
434 {
435 	Rel_desc	*arsp;
436 	Rel_cache	*rcp;
437 
438 	/*
439 	 * If no relocation cache structures are available, allocate a new
440 	 * one and link it into the bucket list.
441 	 */
442 	if ((ofl->ofl_actrels.tail == 0) ||
443 	    ((rcp = (Rel_cache *)ofl->ofl_actrels.tail->data) == 0) ||
444 	    ((arsp = rcp->rc_free) == rcp->rc_end)) {
445 		static size_t	nextsize = 0;
446 		size_t		size;
447 
448 		/*
449 		 * Typically, when generating an executable or shared object
450 		 * there will be an active relocation for every input
451 		 * relocation.
452 		 */
453 		if (nextsize == 0) {
454 			if ((ofl->ofl_flags & FLG_OF_RELOBJ) == 0) {
455 				if ((size = ofl->ofl_relocincnt) == 0)
456 					size = REL_LAIDESCNO;
457 				if (size > REL_HAIDESCNO)
458 					nextsize = REL_HAIDESCNO;
459 				else
460 					nextsize = REL_LAIDESCNO;
461 			} else
462 				nextsize = size = REL_HAIDESCNO;
463 		} else
464 			size = nextsize;
465 
466 		size = size * sizeof (Rel_desc);
467 
468 		if (((rcp = libld_malloc(sizeof (Rel_cache) + size)) == 0) ||
469 		    (list_appendc(&ofl->ofl_actrels, rcp) == 0))
470 			return (S_ERROR);
471 
472 		/* LINTED */
473 		rcp->rc_free = arsp = (Rel_desc *)(rcp + 1);
474 		/* LINTED */
475 		rcp->rc_end = (Rel_desc *)((char *)rcp->rc_free + size);
476 	}
477 
478 	*arsp = *rsp;
479 	arsp->rel_flags |= flags;
480 
481 	rcp->rc_free++;
482 	ofl->ofl_actrelscnt++;
483 
484 	/*
485 	 * Any GOT relocation reference requires the creation of a .got table.
486 	 * Most references to a .got require a .got entry,  which is accounted
487 	 * for with the ofl_gotcnt counter.  However, some references are
488 	 * relative to the .got table, but require no .got entry.  This test
489 	 * insures a .got is created regardless of the type of reference.
490 	 */
491 	if (IS_GOT_REQUIRED(arsp->rel_rtype))
492 		ofl->ofl_flags |= FLG_OF_BLDGOT;
493 
494 	/*
495 	 * If this is a displacement relocation generate a warning.
496 	 */
497 	if (arsp->rel_flags & FLG_REL_DISP) {
498 		ofl->ofl_dtflags_1 |= DF_1_DISPRELDNE;
499 
500 		if (ofl->ofl_flags & FLG_OF_VERBOSE)
501 			ld_disp_errmsg(MSG_INTL(MSG_REL_DISPREL3), arsp, ofl);
502 	}
503 
504 	DBG_CALL(Dbg_reloc_ars_entry(ofl->ofl_lml, ELF_DBG_LD,
505 	    arsp->rel_isdesc->is_shdr->sh_type, M_MACH, arsp));
506 	return (1);
507 }
508 
509 /*
510  * In the platform specific machrel.XXX.c files, we sometimes write
511  * a value directly into the GOT. This function can be used when
512  * the running linker has the opposite byte order of the object being
513  * produced.
514  */
515 Xword
516 ld_byteswap_Xword(Xword v)
517 {
518 #ifdef _ELF64
519 	return ((v << 56) |
520 	    ((v & 0x0000ff00) << 40) |
521 	    ((v & 0x00ff0000) << 24) |
522 	    ((v & 0xff000000) << 8) |
523 	    ((v >> 8)  & 0xff000000) |
524 	    ((v >> 24) & 0x00ff0000) |
525 	    ((v >> 40) & 0x0000ff00) |
526 	    (v >> 56));		/* Xword is unsigned - 0 bits enter from left */
527 #else
528 	return (((v << 24) | ((v & 0xff00) << 8) |
529 	    ((v >> 8) & 0xff00) | (v >> 24)));
530 #endif
531 }
532 
533 
534 uintptr_t
535 ld_reloc_GOT_relative(Boolean local, Rel_desc *rsp, Ofl_desc *ofl)
536 {
537 	Sym_desc	*sdp;
538 	Word		flags = ofl->ofl_flags;
539 	Gotndx		*gnp;
540 
541 	sdp = rsp->rel_sym;
542 
543 	/*
544 	 * If this is the first time we've seen this symbol in a GOT
545 	 * relocation we need to assign it a GOT token.  Once we've got
546 	 * all of the GOT's assigned we can assign the actual indexes.
547 	 */
548 	if ((gnp = ld_find_gotndx(&(sdp->sd_GOTndxs), GOT_REF_GENERIC,
549 	    ofl, rsp)) == 0) {
550 		Word	rtype = rsp->rel_rtype;
551 
552 		if (ld_assign_got_ndx(&(sdp->sd_GOTndxs), 0, GOT_REF_GENERIC,
553 		    ofl, rsp, sdp) == S_ERROR)
554 			return (S_ERROR);
555 
556 		/*
557 		 * Now we initialize the GOT table entry.
558 		 *
559 		 * Pseudo code to describe the the decisions below:
560 		 *
561 		 * If (local)
562 		 * then
563 		 *	enter symbol value in GOT table entry
564 		 *	if (Shared Object)
565 		 *	then
566 		 *		create Relative relocation against symbol
567 		 *	fi
568 		 * else
569 		 *	clear GOT table entry
570 		 *	create a GLOB_DAT relocation against symbol
571 		 * fi
572 		 */
573 		if (local == TRUE) {
574 			if (flags & FLG_OF_SHAROBJ) {
575 				if (ld_add_actrel((FLG_REL_GOT | FLG_REL_GOTCL),
576 				    rsp, ofl) == S_ERROR)
577 					return (S_ERROR);
578 
579 				/*
580 				 * Add a RELATIVE relocation if this is
581 				 * anything but a ABS symbol.
582 				 */
583 				if ((((sdp->sd_flags & FLG_SY_SPECSEC) == 0) ||
584 				    (sdp->sd_sym->st_shndx != SHN_ABS)) ||
585 				    (sdp->sd_aux && sdp->sd_aux->sa_symspec)) {
586 					rsp->rel_rtype = M_R_RELATIVE;
587 					if (ld_add_outrel((FLG_REL_GOT |
588 					    FLG_REL_ADVAL), rsp,
589 					    ofl) == S_ERROR)
590 						return (S_ERROR);
591 					rsp->rel_rtype = rtype;
592 				}
593 			} else {
594 				if (ld_add_actrel(FLG_REL_GOT, rsp,
595 				    ofl) == S_ERROR)
596 					return (S_ERROR);
597 			}
598 		} else {
599 			rsp->rel_rtype = M_R_GLOB_DAT;
600 			if (ld_add_outrel(FLG_REL_GOT, rsp, ofl) == S_ERROR)
601 				return (S_ERROR);
602 			rsp->rel_rtype = rtype;
603 		}
604 	} else {
605 		if (ld_assign_got_ndx(&(sdp->sd_GOTndxs), gnp, GOT_REF_GENERIC,
606 		    ofl, rsp, sdp) == S_ERROR)
607 			return (S_ERROR);
608 	}
609 
610 	/*
611 	 * Perform relocation to GOT table entry.
612 	 */
613 	return (ld_add_actrel(NULL, rsp, ofl));
614 }
615 
616 
617 /*
618  * Perform relocations for PLT's
619  */
620 uintptr_t
621 ld_reloc_plt(Rel_desc *rsp, Ofl_desc *ofl)
622 {
623 	Sym_desc	*sdp = rsp->rel_sym;
624 
625 #if	defined(__x86)
626 #if	defined(_ELF64)
627 	/*
628 	 * AMD64 TLS code sequences do not use a unique TLS relocation to
629 	 * reference the __tls_get_addr() function call.
630 	 */
631 	if ((ofl->ofl_flags & FLG_OF_EXEC) &&
632 	    (strcmp(sdp->sd_name, MSG_ORIG(MSG_SYM_TLSGETADDR_U)) == 0)) {
633 		return (ld_add_actrel(FLG_REL_TLSFIX, rsp, ofl));
634 	}
635 #else
636 	/*
637 	 * GNUC IA32 TLS code sequences do not use a unique TLS relocation to
638 	 * reference the ___tls_get_addr() function call.
639 	 */
640 	if ((ofl->ofl_flags & FLG_OF_EXEC) &&
641 	    (strcmp(sdp->sd_name, MSG_ORIG(MSG_SYM_TLSGETADDR_UU)) == 0)) {
642 		return (ld_add_actrel(FLG_REL_TLSFIX, rsp, ofl));
643 	}
644 #endif
645 #endif	/* __x86 */
646 
647 	/*
648 	 * if (not PLT yet assigned)
649 	 * then
650 	 *	assign PLT index to symbol
651 	 *	build output JMP_SLOT relocation
652 	 * fi
653 	 */
654 	if (sdp->sd_aux->sa_PLTndx == 0) {
655 		Word	ortype = rsp->rel_rtype;
656 
657 		ld_assign_plt_ndx(sdp, ofl);
658 
659 		/*
660 		 * If this symbol is binding to a LAZYLOADED object then
661 		 * set the LAZYLD symbol flag.
662 		 */
663 		if ((sdp->sd_aux->sa_bindto &&
664 		    (sdp->sd_aux->sa_bindto->ifl_flags & FLG_IF_LAZYLD)) ||
665 		    (sdp->sd_file &&
666 		    (sdp->sd_file->ifl_flags & FLG_IF_LAZYLD)))
667 			sdp->sd_flags |= FLG_SY_LAZYLD;
668 
669 		rsp->rel_rtype = M_R_JMP_SLOT;
670 		if (ld_add_outrel(FLG_REL_PLT, rsp, ofl) == S_ERROR)
671 			return (S_ERROR);
672 		rsp->rel_rtype = ortype;
673 	}
674 
675 	/*
676 	 * Perform relocation to PLT table entry.
677 	 */
678 	if ((ofl->ofl_flags & FLG_OF_SHAROBJ) &&
679 	    IS_ADD_RELATIVE(rsp->rel_rtype)) {
680 		Word	ortype	= rsp->rel_rtype;
681 
682 		rsp->rel_rtype = M_R_RELATIVE;
683 		if (ld_add_outrel(FLG_REL_ADVAL, rsp, ofl) == S_ERROR)
684 			return (S_ERROR);
685 		rsp->rel_rtype = ortype;
686 		return (1);
687 	} else
688 		return (ld_add_actrel(NULL, rsp, ofl));
689 }
690 
691 /*
692  * process GLOBAL undefined and ref_dyn_need symbols.
693  */
694 static uintptr_t
695 reloc_exec(Rel_desc *rsp, Ofl_desc *ofl)
696 {
697 	Sym_desc	*_sdp, *sdp = rsp->rel_sym;
698 	Sym_aux		*sap = sdp->sd_aux;
699 	Sym		*sym = sdp->sd_sym;
700 	Addr		stval;
701 
702 	/*
703 	 * Reference is to a function so simply create a plt entry for it.
704 	 */
705 	if (ELF_ST_TYPE(sym->st_info) == STT_FUNC)
706 		return (ld_reloc_plt(rsp, ofl));
707 
708 	/*
709 	 * Catch absolutes - these may cause a text relocation.
710 	 */
711 	if ((sdp->sd_flags & FLG_SY_SPECSEC) && (sym->st_shndx == SHN_ABS)) {
712 		if ((ofl->ofl_flags1 & FLG_OF1_ABSEXEC) == 0)
713 			return (ld_add_outrel(NULL, rsp, ofl));
714 
715 		/*
716 		 * If -zabsexec is set then promote the ABSOLUTE symbol to
717 		 * current the current object and perform the relocation now.
718 		 */
719 		sdp->sd_ref = REF_REL_NEED;
720 		return (ld_add_actrel(NULL, rsp, ofl));
721 	}
722 
723 	/*
724 	 * If the relocation is against a writable section simply compute the
725 	 * necessary output relocation.  As an optimization, if the symbol has
726 	 * already been transformed into a copy relocation then we can perform
727 	 * the relocation directly (copy relocations should only be generated
728 	 * for references from the text segment and these relocations are
729 	 * normally carried out before we get to the data segment relocations).
730 	 */
731 	if ((ELF_ST_TYPE(sym->st_info) == STT_OBJECT) &&
732 	    (rsp->rel_osdesc->os_shdr->sh_flags & SHF_WRITE)) {
733 		if (sdp->sd_flags & FLG_SY_MVTOCOMM)
734 			return (ld_add_actrel(NULL, rsp, ofl));
735 		else
736 			return (ld_add_outrel(NULL, rsp, ofl));
737 	}
738 
739 	/*
740 	 * If the reference isn't to an object (normally because a .type
741 	 * directive hasn't defined in some assembler source), then simply apply
742 	 * a generic relocation (this has a tendency to result in text
743 	 * relocations).
744 	 */
745 	if (ELF_ST_TYPE(sym->st_info) != STT_OBJECT) {
746 		Conv_inv_buf_t inv_buf;
747 
748 		eprintf(ofl->ofl_lml, ERR_WARNING, MSG_INTL(MSG_REL_UNEXPSYM),
749 		    conv_sym_info_type(sdp->sd_file->ifl_ehdr->e_machine,
750 		    ELF_ST_TYPE(sym->st_info), 0, &inv_buf),
751 		    rsp->rel_isdesc->is_file->ifl_name,
752 		    demangle(rsp->rel_sname), sdp->sd_file->ifl_name);
753 		return (ld_add_outrel(NULL, rsp, ofl));
754 	}
755 
756 	/*
757 	 * Prepare for generating a copy relocation.
758 	 *
759 	 * If this symbol is one of an alias pair, we need to insure both
760 	 * symbols become part of the output (the strong symbol will be used to
761 	 * maintain the symbols state).  And, if we did raise the precedence of
762 	 * a symbol we need to check and see if this is a weak symbol.  If it is
763 	 * we want to use it's strong counter part.
764 	 *
765 	 * The results of this logic should be:
766 	 *	rel_usym: assigned to strong
767 	 *	 rel_sym: assigned to symbol to perform
768 	 *		  copy_reloc against (weak or strong).
769 	 */
770 	if (sap->sa_linkndx) {
771 		_sdp = sdp->sd_file->ifl_oldndx[sap->sa_linkndx];
772 
773 		if (_sdp->sd_ref < sdp->sd_ref) {
774 			_sdp->sd_ref = sdp->sd_ref;
775 			_sdp->sd_flags |= FLG_SY_REFRSD;
776 
777 			/*
778 			 * As we're going to replicate a symbol from a shared
779 			 * object, retain its correct binding status.
780 			 */
781 			if (ELF_ST_BIND(_sdp->sd_sym->st_info) == STB_GLOBAL)
782 				_sdp->sd_flags |= FLG_SY_GLOBREF;
783 
784 		} else if (_sdp->sd_ref > sdp->sd_ref) {
785 			sdp->sd_ref = _sdp->sd_ref;
786 			sdp->sd_flags |= FLG_SY_REFRSD;
787 
788 			/*
789 			 * As we're going to replicate a symbol from a shared
790 			 * object, retain its correct binding status.
791 			 */
792 			if (ELF_ST_BIND(sym->st_info) == STB_GLOBAL)
793 				sdp->sd_flags |= FLG_SY_GLOBREF;
794 		}
795 
796 		/*
797 		 * If this is a weak symbol then we want to move the strong
798 		 * symbol into local .bss.  If there is a copy_reloc to be
799 		 * performed, that should still occur against the WEAK symbol.
800 		 */
801 		if ((ELF_ST_BIND(sdp->sd_sym->st_info) == STB_WEAK) ||
802 		    (sdp->sd_flags & FLG_SY_WEAKDEF))
803 			rsp->rel_usym = _sdp;
804 	} else
805 		_sdp = 0;
806 
807 	/*
808 	 * If the reference is to an object then allocate space for the object
809 	 * within the executables .bss.  Relocations will now be performed from
810 	 * this new location.  If the original shared objects data is
811 	 * initialized, then generate a copy relocation that will copy the data
812 	 * to the executables .bss at runtime.
813 	 */
814 	if (!(rsp->rel_usym->sd_flags & FLG_SY_MVTOCOMM)) {
815 		Word	rtype = rsp->rel_rtype;
816 		Copy_rel *cpy_rel;
817 
818 		/*
819 		 * Indicate that the symbol(s) against which we're relocating
820 		 * have been moved to the executables common.  Also, insure that
821 		 * the symbol(s) remain marked as global, as the shared object
822 		 * from which they are copied must be able to relocate to the
823 		 * new common location within the executable.
824 		 *
825 		 * Note that even though a new symbol has been generated in the
826 		 * output files' .bss, the symbol must remain REF_DYN_NEED and
827 		 * not be promoted to REF_REL_NEED.  sym_validate() still needs
828 		 * to carry out a number of checks against the symbols binding
829 		 * that are triggered by the REF_DYN_NEED state.
830 		 */
831 		sdp->sd_flags |= FLG_SY_MVTOCOMM;
832 		sdp->sd_flags1 |= (FLG_SY1_DEFAULT | FLG_SY1_EXPDEF);
833 		sdp->sd_flags1 &= ~MSK_SY1_LOCAL;
834 		sdp->sd_sym->st_other &= ~MSK_SYM_VISIBILITY;
835 		if (_sdp) {
836 			_sdp->sd_flags |= FLG_SY_MVTOCOMM;
837 			_sdp->sd_flags1 |= (FLG_SY1_DEFAULT | FLG_SY1_EXPDEF);
838 			_sdp->sd_flags1 &= ~MSK_SY1_LOCAL;
839 			_sdp->sd_sym->st_other &= ~MSK_SYM_VISIBILITY;
840 
841 			/*
842 			 * Make sure the symbol has a reference in case of any
843 			 * error diagnostics against it (perhaps this belongs
844 			 * to a version that isn't allowable for this build).
845 			 * The resulting diagnostic (see sym_undef_entry())
846 			 * might seem a little bogus, as the symbol hasn't
847 			 * really been referenced by this file, but has been
848 			 * promoted as a consequence of its alias reference.
849 			 */
850 			if (!(_sdp->sd_aux->sa_rfile))
851 				_sdp->sd_aux->sa_rfile = sdp->sd_aux->sa_rfile;
852 		}
853 
854 		/*
855 		 * Assign the symbol to the bss and insure sufficient alignment
856 		 * (we don't know the real alignment so we have to make the
857 		 * worst case guess).
858 		 */
859 		_sdp = rsp->rel_usym;
860 		stval = _sdp->sd_sym->st_value;
861 		if (ld_sym_copy(_sdp) == S_ERROR)
862 			return (S_ERROR);
863 		_sdp->sd_shndx = _sdp->sd_sym->st_shndx = SHN_COMMON;
864 		_sdp->sd_flags |= FLG_SY_SPECSEC;
865 		_sdp->sd_sym->st_value =
866 		    (_sdp->sd_sym->st_size < (M_WORD_ALIGN * 2)) ?
867 		    M_WORD_ALIGN : M_WORD_ALIGN * 2;
868 
869 		/*
870 		 * Whether or not the symbol references initialized
871 		 * data we generate a copy relocation - this differs
872 		 * from the past where we would not create the COPY_RELOC
873 		 * if we were binding against .bss.  This is done
874 		 * for *two* reasons.
875 		 *
876 		 *  o If the symbol in the shared object changes to
877 		 *    a initialized data - we need the COPY to pick it
878 		 *    up.
879 		 *  o without the COPY RELOC we can't tell that the
880 		 *    symbol from the COPY'd object has been moved
881 		 *    and all bindings to it should bind here.
882 		 */
883 
884 		/*
885 		 * Keep this symbol in the copy relocation list
886 		 * to check the validity later.
887 		 */
888 		if ((cpy_rel = libld_malloc(sizeof (Copy_rel))) == 0)
889 			return (S_ERROR);
890 		cpy_rel->copyrel_symd = _sdp;
891 		cpy_rel->copyrel_stval = stval;
892 		if (list_appendc(&ofl->ofl_copyrels, cpy_rel) == 0)
893 			return (S_ERROR);
894 
895 		rsp->rel_rtype = M_R_COPY;
896 		if (ld_add_outrel(FLG_REL_BSS, rsp, ofl) == S_ERROR)
897 			return (S_ERROR);
898 		rsp->rel_rtype = rtype;
899 
900 		/*
901 		 * If this symbol is a protected symbol, warn it.
902 		 */
903 		if (_sdp->sd_flags & FLG_SY_PROT) {
904 			Conv_inv_buf_t inv_buf;
905 
906 			eprintf(ofl->ofl_lml, ERR_WARNING,
907 			    MSG_INTL(MSG_REL_COPY),
908 			    conv_reloc_type(_sdp->sd_file->ifl_ehdr->e_machine,
909 			    M_R_COPY, 0, &inv_buf), _sdp->sd_file->ifl_name,
910 			    _sdp->sd_name);
911 		}
912 		DBG_CALL(Dbg_syms_reloc(ofl, sdp));
913 	}
914 	return (ld_add_actrel(NULL, rsp, ofl));
915 }
916 
917 /*
918  * All relocations should have been handled by the other routines.  This
919  * routine is here as a catch all, if we do enter it we've goofed - but
920  * we'll try and to the best we can.
921  */
922 static uintptr_t
923 reloc_generic(Rel_desc *rsp, Ofl_desc *ofl)
924 {
925 	Ifl_desc	*ifl = rsp->rel_isdesc->is_file;
926 	Conv_inv_buf_t	inv_buf;
927 
928 	eprintf(ofl->ofl_lml, ERR_WARNING, MSG_INTL(MSG_REL_UNEXPREL),
929 	    conv_reloc_type(ifl->ifl_ehdr->e_machine, rsp->rel_rtype,
930 	    0, &inv_buf), ifl->ifl_name, demangle(rsp->rel_sname));
931 
932 	/*
933 	 * If building a shared object then put the relocation off
934 	 * until runtime.
935 	 */
936 	if (ofl->ofl_flags & FLG_OF_SHAROBJ)
937 		return (ld_add_outrel(NULL, rsp, ofl));
938 
939 	/*
940 	 * Otherwise process relocation now.
941 	 */
942 	return (ld_add_actrel(NULL, rsp, ofl));
943 }
944 
945 /*
946  * Process relocations when building a relocatable object.  Typically, there
947  * aren't many relocations that can be caught at this point, most are simply
948  * passed through to the output relocatable object.
949  */
950 static uintptr_t
951 reloc_relobj(Boolean local, Rel_desc *rsp, Ofl_desc *ofl)
952 {
953 	Word		rtype = rsp->rel_rtype;
954 	Sym_desc	*sdp = rsp->rel_sym;
955 	Is_desc		*isp = rsp->rel_isdesc;
956 	Word		oflags = NULL;
957 
958 	/*
959 	 * Determine if we can do any relocations at this point.  We can if:
960 	 *
961 	 *	this is local_symbol and a non-GOT relocation, and
962 	 *	the relocation is pc-relative, and
963 	 *	the relocation is against a symbol in same section
964 	 */
965 	if (local && !IS_GOT_RELATIVE(rtype) && !IS_GOT_BASED(rtype) &&
966 	    !IS_GOT_PC(rtype) && IS_PC_RELATIVE(rtype) &&
967 	    ((sdp->sd_isc) && (sdp->sd_isc->is_osdesc == isp->is_osdesc)))
968 		return (ld_add_actrel(NULL, rsp, ofl));
969 
970 	/*
971 	 * If -zredlocsym is in effect, translate all local symbol relocations
972 	 * to be against section symbols, since section symbols are the only
973 	 * local symbols which will be added to the .symtab.
974 	 */
975 	if (local && (((ofl->ofl_flags1 & FLG_OF1_REDLSYM) &&
976 	    (ELF_ST_BIND(sdp->sd_sym->st_info) == STB_LOCAL)) ||
977 	    ((sdp->sd_flags1 & FLG_SY1_ELIM) &&
978 	    (ofl->ofl_flags & FLG_OF_PROCRED)))) {
979 		/*
980 		 * But if this is PIC code, don't allow it for now.
981 		 */
982 		if (IS_GOT_RELATIVE(rsp->rel_rtype)) {
983 			Ifl_desc	*ifl = rsp->rel_isdesc->is_file;
984 			Conv_inv_buf_t inv_buf;
985 
986 			eprintf(ofl->ofl_lml, ERR_FATAL,
987 			    MSG_INTL(MSG_REL_PICREDLOC),
988 			    demangle(rsp->rel_sname), ifl->ifl_name,
989 			    conv_reloc_type(ifl->ifl_ehdr->e_machine,
990 			    rsp->rel_rtype, 0, &inv_buf));
991 			return (S_ERROR);
992 		}
993 
994 		/*
995 		 * Indicate that this relocation should be processed the same
996 		 * as a section symbol.  For SPARC and AMD64 (Rela), indicate
997 		 * that the addend also needs to be applied to this relocation.
998 		 */
999 #if	(defined(__i386) || defined(__amd64)) && !defined(_ELF64)
1000 		oflags = FLG_REL_SCNNDX;
1001 #else
1002 		oflags = FLG_REL_SCNNDX | FLG_REL_ADVAL;
1003 #endif
1004 	}
1005 
1006 #if	(defined(__i386) || defined(__amd64)) && !defined(_ELF64)
1007 	/*
1008 	 * Intel (Rel) relocations do not contain an addend.  Any addend is
1009 	 * contained within the file at the location identified by the
1010 	 * relocation offset.  Therefore, if we're processing a section symbol,
1011 	 * or a -zredlocsym relocation (that basically transforms a local symbol
1012 	 * reference into a section reference), perform an active relocation to
1013 	 * propagate any addend.
1014 	 */
1015 	if ((ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION) ||
1016 	    (oflags == FLG_REL_SCNNDX))
1017 		if (ld_add_actrel(NULL, rsp, ofl) == S_ERROR)
1018 			return (S_ERROR);
1019 #endif
1020 	return (ld_add_outrel(oflags, rsp, ofl));
1021 }
1022 
1023 /*
1024  * Perform any generic TLS validations before passing control to machine
1025  * specific routines.  At this point we know we are dealing with an executable
1026  * or shared object - relocatable objects have already been processed.
1027  */
1028 static uintptr_t
1029 reloc_TLS(Boolean local, Rel_desc *rsp, Ofl_desc *ofl)
1030 {
1031 	Word		rtype = rsp->rel_rtype, flags = ofl->ofl_flags;
1032 	Ifl_desc	*ifl = rsp->rel_isdesc->is_file;
1033 	Half		mach = ifl->ifl_ehdr->e_machine;
1034 	Sym_desc	*sdp = rsp->rel_sym;
1035 	unsigned char	type;
1036 	Conv_inv_buf_t	inv_buf1, inv_buf2;
1037 
1038 	/*
1039 	 * All TLS relocations are illegal in a static executable.
1040 	 */
1041 	if ((flags & (FLG_OF_STATIC | FLG_OF_EXEC)) ==
1042 	    (FLG_OF_STATIC | FLG_OF_EXEC)) {
1043 		eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_REL_TLSSTAT),
1044 		    conv_reloc_type(mach, rtype, 0, &inv_buf1), ifl->ifl_name,
1045 		    demangle(rsp->rel_sname));
1046 		return (S_ERROR);
1047 	}
1048 
1049 	/*
1050 	 * Any TLS relocation must be against a STT_TLS symbol, all others
1051 	 * are illegal.
1052 	 */
1053 	if ((type = ELF_ST_TYPE(sdp->sd_sym->st_info)) != STT_TLS) {
1054 		eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_REL_TLSBADSYM),
1055 		    conv_reloc_type(mach, rtype, 0, &inv_buf1), ifl->ifl_name,
1056 		    demangle(rsp->rel_sname),
1057 		    conv_sym_info_type(mach, type, 0, &inv_buf2));
1058 		return (S_ERROR);
1059 	}
1060 
1061 	/*
1062 	 * A dynamic executable can not use the LD or LE reference models to
1063 	 * reference an external symbol.  A shared object can not use the LD
1064 	 * reference model to reference an external symbol.
1065 	 */
1066 	if (!local && (IS_TLS_LD(rtype) ||
1067 	    ((flags & FLG_OF_EXEC) && IS_TLS_LE(rtype)))) {
1068 		eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_REL_TLSBND),
1069 		    conv_reloc_type(mach, rtype, 0, &inv_buf1), ifl->ifl_name,
1070 		    demangle(rsp->rel_sname), sdp->sd_file->ifl_name);
1071 		return (S_ERROR);
1072 	}
1073 
1074 	/*
1075 	 * The TLS LE model is only allowed for dynamic executables.  The TLS IE
1076 	 * model is allowed for shared objects, but this model has restrictions.
1077 	 * This model can only be used freely in dependencies that are loaded
1078 	 * immediately as part of process initialization.  However, during the
1079 	 * initial runtime handshake with libc that establishes the thread
1080 	 * pointer, a small backup TLS reservation is created.  This area can
1081 	 * be used by objects that are loaded after threads are initialized.
1082 	 * However, this area is limited in size and may have already been
1083 	 * used.  This area is intended for specialized applications, and does
1084 	 * not provide the degree of flexibility dynamic TLS can offer.  Under
1085 	 * -z verbose indicate this restriction to the user.
1086 	 */
1087 	if ((flags & FLG_OF_EXEC) == 0) {
1088 		if (IS_TLS_LE(rtype)) {
1089 			eprintf(ofl->ofl_lml, ERR_FATAL,
1090 			    MSG_INTL(MSG_REL_TLSLE),
1091 			    conv_reloc_type(mach, rtype, 0, &inv_buf1),
1092 			    ifl->ifl_name, demangle(rsp->rel_sname));
1093 			return (S_ERROR);
1094 
1095 		} else if ((IS_TLS_IE(rtype)) && (flags & FLG_OF_VERBOSE)) {
1096 			eprintf(ofl->ofl_lml, ERR_WARNING,
1097 			    MSG_INTL(MSG_REL_TLSIE),
1098 			    conv_reloc_type(mach, rtype, 0, &inv_buf1),
1099 			    ifl->ifl_name, demangle(rsp->rel_sname));
1100 		}
1101 	}
1102 
1103 	return (ld_reloc_TLS(local, rsp, ofl));
1104 }
1105 
1106 uintptr_t
1107 ld_process_sym_reloc(Ofl_desc *ofl, Rel_desc *reld, Rel *reloc, Is_desc *isp,
1108     const char *isname)
1109 {
1110 	Word		rtype = reld->rel_rtype;
1111 	Word		flags = ofl->ofl_flags;
1112 	Sym_desc	*sdp = reld->rel_sym;
1113 	Sym_aux		*sap;
1114 	Boolean		local;
1115 	Conv_inv_buf_t	inv_buf;
1116 
1117 	DBG_CALL(Dbg_reloc_in(ofl->ofl_lml, ELF_DBG_LD, M_MACH, M_REL_SHT_TYPE,
1118 	    (void *)reloc, isname, reld->rel_sname));
1119 
1120 	/*
1121 	 * Indicate this symbol is being used for relocation and therefore must
1122 	 * have its output address updated accordingly (refer to update_osym()).
1123 	 */
1124 	sdp->sd_flags |= FLG_SY_UPREQD;
1125 
1126 	/*
1127 	 * Indicate the section this symbol is defined in has been referenced,
1128 	 * therefor it *is not* a candidate for elimination.
1129 	 */
1130 	if (sdp->sd_isc) {
1131 		sdp->sd_isc->is_flags |= FLG_IS_SECTREF;
1132 		sdp->sd_isc->is_file->ifl_flags |= FLG_IF_FILEREF;
1133 	}
1134 
1135 	reld->rel_usym = sdp;
1136 
1137 	/*
1138 	 * Determine if this symbol is actually an alias to another symbol.  If
1139 	 * so, and the alias is not REF_DYN_SEEN, set rel_usym to point to the
1140 	 * weak symbols strong counter-part.  The one exception is if the
1141 	 * FLG_SY_MVTOCOMM flag is set on the weak symbol.  If this is the case,
1142 	 * the strong is only here because of its promotion, and the weak symbol
1143 	 * should still be used for the relocation reference (see reloc_exec()).
1144 	 */
1145 	sap = sdp->sd_aux;
1146 	if (sap && sap->sa_linkndx &&
1147 	    ((ELF_ST_BIND(sdp->sd_sym->st_info) == STB_WEAK) ||
1148 	    (sdp->sd_flags & FLG_SY_WEAKDEF)) &&
1149 	    (!(sdp->sd_flags & FLG_SY_MVTOCOMM))) {
1150 		Sym_desc *	_sdp;
1151 
1152 		_sdp = sdp->sd_file->ifl_oldndx[sap->sa_linkndx];
1153 		if (_sdp->sd_ref != REF_DYN_SEEN)
1154 			reld->rel_usym = _sdp;
1155 	}
1156 
1157 	/*
1158 	 * Determine whether this symbol should be bound locally or not.
1159 	 * Symbols are bound locally if one of the following is true:
1160 	 *
1161 	 *  o	the symbol is of type STB_LOCAL.
1162 	 *
1163 	 *  o	the output image is not a relocatable object and the relocation
1164 	 *	is relative to the .got.
1165 	 *
1166 	 *  o	the section being relocated is of type SHT_SUNW_dof.  These
1167 	 *	sections must be bound to the functions in the containing
1168 	 *	object and can not be interposed upon.
1169 	 *
1170 	 *  o	the symbol has been reduced (scoped to a local or symbolic) and
1171 	 *	reductions are being processed.
1172 	 *
1173 	 *  o	the -Bsymbolic flag is in use when building a shared object,
1174 	 *	and the symbol hasn't explicitly been defined as nodirect.
1175 	 *
1176 	 *  o	an executable (fixed address) is being created, and the symbol
1177 	 *	is defined in the executable.
1178 	 *
1179 	 *  o	the relocation is against a segment which will not be loaded
1180 	 *	into memory.  In this case, the relocation must be resolved
1181 	 *	now, as ld.so.1 can not process relocations against unmapped
1182 	 *	segments.
1183 	 */
1184 	local = FALSE;
1185 	if (ELF_ST_BIND(sdp->sd_sym->st_info) == STB_LOCAL) {
1186 		local = TRUE;
1187 	} else if (!(reld->rel_flags & FLG_REL_LOAD)) {
1188 		local = TRUE;
1189 	} else if (sdp->sd_sym->st_shndx != SHN_UNDEF) {
1190 		if (reld->rel_isdesc &&
1191 		    reld->rel_isdesc->is_shdr->sh_type == SHT_SUNW_dof) {
1192 			local = TRUE;
1193 		} else if (!(flags & FLG_OF_RELOBJ) &&
1194 		    (IS_LOCALBND(rtype) || IS_SEG_RELATIVE(rtype))) {
1195 			local = TRUE;
1196 		} else if (sdp->sd_ref == REF_REL_NEED) {
1197 			/*
1198 			 * Global symbols may have been individually reduced in
1199 			 * scope.  If the whole object is to be self contained,
1200 			 * such as when generating an executable or a symbolic
1201 			 * shared object, make sure all relocation symbol
1202 			 * references (sections too) are treated locally.  Note,
1203 			 * explicit no-direct symbols should not be bound to
1204 			 * locally.
1205 			 */
1206 			if ((sdp->sd_flags1 &
1207 			    (FLG_SY1_HIDDEN | FLG_SY1_PROTECT)))
1208 				local = TRUE;
1209 			else if ((flags & FLG_OF_EXEC) ||
1210 			    ((flags & FLG_OF_SYMBOLIC) &&
1211 			    ((sdp->sd_flags1 & FLG_SY1_NDIR) == 0)))
1212 				local = TRUE;
1213 		}
1214 	}
1215 
1216 	/*
1217 	 * If this is a PC_RELATIVE relocation, the relocation could be
1218 	 * compromised if the relocated address is later used as a copy
1219 	 * relocated symbol (PSARC 1999/636, bugid 4187211).  Scan the input
1220 	 * files symbol table to cross reference this relocation offset.
1221 	 */
1222 	if ((ofl->ofl_flags & FLG_OF_SHAROBJ) && IS_PC_RELATIVE(rtype) &&
1223 	    (IS_GOT_PC(rtype) == 0) && (IS_PLT(rtype) == 0)) {
1224 		if (disp_inspect(ofl, reld, local) == S_ERROR)
1225 			return (S_ERROR);
1226 	}
1227 
1228 	/*
1229 	 * GOT based relocations must bind to the object being built - since
1230 	 * they are relevant to the current GOT.  If not building a relocatable
1231 	 * object - give a appropriate error message.
1232 	 */
1233 	if (!local && !(flags & FLG_OF_RELOBJ) && IS_GOT_BASED(rtype)) {
1234 		Ifl_desc	*ifl = reld->rel_isdesc->is_file;
1235 
1236 		eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_REL_BADGOTBASED),
1237 		    conv_reloc_type(ifl->ifl_ehdr->e_machine, rtype,
1238 		    0, &inv_buf), ifl->ifl_name, demangle(sdp->sd_name));
1239 		return (S_ERROR);
1240 	}
1241 
1242 	/*
1243 	 * TLS symbols can only have TLS relocations.
1244 	 */
1245 	if ((ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_TLS) &&
1246 	    (IS_TLS_INS(rtype) == 0)) {
1247 		/*
1248 		 * The above test is relaxed if the target section is
1249 		 * non-allocable.
1250 		 */
1251 		if (reld->rel_osdesc->os_shdr->sh_flags & SHF_ALLOC) {
1252 			Ifl_desc	*ifl = reld->rel_isdesc->is_file;
1253 
1254 			eprintf(ofl->ofl_lml, ERR_FATAL,
1255 			    MSG_INTL(MSG_REL_BADTLS),
1256 			    conv_reloc_type(ifl->ifl_ehdr->e_machine,
1257 			    rtype, 0, &inv_buf), ifl->ifl_name,
1258 			    demangle(sdp->sd_name));
1259 			return (S_ERROR);
1260 		}
1261 	}
1262 
1263 	/*
1264 	 * Select the relocation to perform.
1265 	 */
1266 	if (IS_REGISTER(rtype))
1267 		return (ld_reloc_register(reld, isp, ofl));
1268 
1269 	if (flags & FLG_OF_RELOBJ)
1270 		return (reloc_relobj(local, reld, ofl));
1271 
1272 	if (IS_TLS_INS(rtype))
1273 		return (reloc_TLS(local, reld, ofl));
1274 
1275 	if (IS_GOT_OPINS(rtype))
1276 		return (ld_reloc_GOTOP(local, reld, ofl));
1277 
1278 	if (IS_GOT_RELATIVE(rtype))
1279 		return (ld_reloc_GOT_relative(local, reld, ofl));
1280 
1281 	if (local)
1282 		return (ld_reloc_local(reld, ofl));
1283 
1284 	if ((IS_PLT(rtype)) && ((flags & FLG_OF_BFLAG) == 0))
1285 		return (ld_reloc_plt(reld, ofl));
1286 
1287 	if ((sdp->sd_ref == REF_REL_NEED) ||
1288 	    (flags & FLG_OF_BFLAG) || (flags & FLG_OF_SHAROBJ) ||
1289 	    (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_NOTYPE))
1290 		return (ld_add_outrel(NULL, reld, ofl));
1291 
1292 	if (sdp->sd_ref == REF_DYN_NEED)
1293 		return (reloc_exec(reld, ofl));
1294 
1295 	/*
1296 	 * IS_NOT_REL(rtype)
1297 	 */
1298 	return (reloc_generic(reld, ofl));
1299 }
1300 
1301 /*
1302  * Given a relocation that references a local symbol from a discarded
1303  * COMDAT (or SHF_GROUP) section, replace the symbol with the
1304  * corresponding symbol from the section that was kept.
1305  *
1306  * entry:
1307  *	reld - Relocation
1308  *	orig_sdp - Symbol to be replaced. Must be a local symbol (STB_LOCAL).
1309  *
1310  * exit:
1311  *	Returns address of replacement symbol descriptor if one was
1312  *	found, and NULL otherwise.
1313  *
1314  * note:
1315  *	[Note that I'm using the word "COMDAT" here loosely, to refer
1316  *	to actual COMDAT sections as well as to groups tied together
1317  *	with an SHF_GROUP section. SHF_GROUP is simply a more advanced
1318  *	version of the same idea: That multiple versions of the same thing
1319  *	can come in, but only one of them is used for the output.]
1320  *
1321  *	In principle, this sort of sloppy relocation remapping is
1322  *	a questionable practice. All self-referential sections should
1323  *	be in a common SHF_GROUP so that they are all kept or removed
1324  *	together. The problem is that there is no way to ensure that the
1325  *	two sections are similar enough that the replacement section will
1326  *	really supply the correct information. However, we see a couple of
1327  *	situations where it is useful to do this: (1) Older Sun C compilers
1328  *	generated DWARF sections that would refer to one of the COMDAT
1329  *	sections, and (2) gcc, when its COMDAT feature is enabled.
1330  *	It turns out that the GNU ld does these sloppy remappings.
1331  *
1332  *	The GNU ld takes an approach that hard wires special section
1333  *	names and treats them specially. We avoid that practice and
1334  *	try to get the necessary work done relying only on the ELF
1335  *	attributes of the sections and symbols involved. This means
1336  *	that our heuristic is somewhat different than theirs, but the
1337  *	end result is close enough to solve the same problem.
1338  *
1339  *	It is our hope that gcc will eventually phase out the need
1340  *	for sloppy relocations, and this won't be needed. In the
1341  *	meantime, the option allows us to interoperate.
1342  *
1343  *	Here is how we do it: The symbol points at the input section,
1344  *	and the input section points at the output section to which it
1345  *	is assigned. The output section contains a list of all of the
1346  *	input sections that have been mapped to it, including the
1347  *	corresponding COMDAT section that was kept. The kept COMDAT
1348  *	section contains a reference to its input file, where we can find
1349  *	the array of all the symbols in that file. From the input file,
1350  *	we then locate the corresponding symbol that references the kept
1351  *	COMDAT section.
1352  *
1353  */
1354 static Sym_desc *
1355 sloppy_comdat_reloc(Ofl_desc *ofl, Rel_desc *reld, Sym_desc *sdp)
1356 {
1357 	Listnode	*lnp;
1358 	Is_desc		*rep_isp;
1359 	const char	*is_name;
1360 	Sym		*sym = sdp->sd_sym;
1361 	Is_desc		*isp = sdp->sd_isc;
1362 	Conv_inv_buf_t	inv_buf;
1363 
1364 	/*
1365 	 * ld_place_section() can alter the section name if it contains
1366 	 * a '%' character. We need to use the original name in this
1367 	 * case.
1368 	 */
1369 	is_name = isp->is_basename ? isp->is_basename : isp->is_name;
1370 
1371 	/*
1372 	 * 1) The caller is required to ensure that the input symbol is
1373 	 *	local. We don't check for that here.
1374 	 * 2) If the discarded section has not been assigned to an
1375 	 *	output section, we won't be able to continue.
1376 	 * 3) This operation only applies to SHT_SUNW_COMDAT sections
1377 	 *	or sections contained within a COMDAT group (SHF_GROUP).
1378 	 */
1379 	if ((isp->is_osdesc == NULL) ||
1380 	    ((isp->is_shdr->sh_type != SHT_SUNW_COMDAT) &&
1381 	    ((isp->is_shdr->sh_flags & SHF_GROUP) == 0)))
1382 		return (NULL);
1383 
1384 	/*
1385 	 * Examine each input section assigned to this output section.
1386 	 * The replacement section must:
1387 	 *	- Have the same name as the original
1388 	 *	- Not have been discarded
1389 	 *	- Have the same size
1390 	 *	- Have the same section type (and note that this type may
1391 	 *		be SHT_SUNW_COMDAT).
1392 	 *	- Have the same SHF_GROUP flag setting (either on or off)
1393 	 *	- Must be a COMDAT section of one form or the other.
1394 	 */
1395 	/* BEGIN CSTYLED */
1396 	for (LIST_TRAVERSE(&isp->is_osdesc->os_isdescs, lnp, rep_isp)) {
1397 	    const char *rep_is_name = rep_isp->is_basename ?
1398 		rep_isp->is_basename : rep_isp->is_name;
1399 
1400 	    if (!(rep_isp->is_flags & FLG_IS_DISCARD) &&
1401 		(isp->is_indata->d_size == rep_isp->is_indata->d_size) &&
1402 		(isp->is_shdr->sh_type == rep_isp->is_shdr->sh_type) &&
1403 		((isp->is_shdr->sh_flags & SHF_GROUP) ==
1404 		(rep_isp->is_shdr->sh_flags & SHF_GROUP)) &&
1405 		(strcmp(rep_is_name, is_name) == 0)) {
1406 		/*
1407 		 * We found the kept COMDAT section. Now, look at all of the
1408 		 * symbols from the input file that contains it to find the
1409 		 * symbol that corresponds to the one we started with:
1410 		 *	- Hasn't been discarded
1411 		 *	- Has section index of kept section
1412 		 *	- If one symbol has a name, the other must have
1413 		 *		the same name. The st_name field of a symbol
1414 		 *		is 0 if there is no name, and is a string
1415 		 *		table offset otherwise. The string table
1416 		 *		offsets may well not agree --- it is the
1417 		 *		actual string that matters.
1418 		 *	- Type and binding attributes match (st_info)
1419 		 *	- Values match (st_value)
1420 		 *	- Sizes match (st_size)
1421 		 *	- Visibility matches (st_other)
1422 		 */
1423 		Word scnndx = rep_isp->is_scnndx;
1424 		Sym_desc **oldndx = rep_isp->is_file->ifl_oldndx;
1425 		Word symscnt = rep_isp->is_file->ifl_symscnt;
1426 		Sym_desc *rep_sdp;
1427 		Sym *rep_sym;
1428 
1429 		while (symscnt--) {
1430 		    rep_sdp = *oldndx++;
1431 		    if (rep_sdp && !(rep_sdp->sd_flags & FLG_SY_ISDISC) &&
1432 			((rep_sym = rep_sdp->sd_sym)->st_shndx == scnndx) &&
1433 			((sym->st_name == 0) == (rep_sym->st_name == 0)) &&
1434 			!((sym->st_name != 0) &&
1435 			    (strcmp(sdp->sd_name, rep_sdp->sd_name) != 0)) &&
1436 			(sym->st_info == rep_sym->st_info) &&
1437 			(sym->st_value == rep_sym->st_value) &&
1438 			(sym->st_size == rep_sym->st_size) &&
1439 			(sym->st_other == rep_sym->st_other)) {
1440 
1441 			if (ofl->ofl_flags & FLG_OF_VERBOSE) {
1442 			    Ifl_desc *ifl = sdp->sd_file;
1443 
1444 			    if (sym->st_name != 0) {
1445 				eprintf(ofl->ofl_lml, ERR_WARNING,
1446 				    MSG_INTL(MSG_REL_SLOPCDATNAM),
1447 				    conv_reloc_type(ifl->ifl_ehdr->e_machine,
1448 					reld->rel_rtype, 0, &inv_buf),
1449 				    ifl->ifl_name, reld->rel_isdesc->is_name,
1450 				    rep_sdp->sd_name, is_name,
1451 				    rep_sdp->sd_file->ifl_name);
1452 			    } else {
1453 				eprintf(ofl->ofl_lml, ERR_WARNING,
1454 				    MSG_INTL(MSG_REL_SLOPCDATNONAM),
1455 				    conv_reloc_type(
1456 					ifl->ifl_ehdr->e_machine,
1457 					reld->rel_rtype, 0, &inv_buf),
1458 				    ifl->ifl_name, reld->rel_isdesc->is_name,
1459 				    is_name, rep_sdp->sd_file->ifl_name);
1460 			    }
1461 			}
1462 			DBG_CALL(Dbg_reloc_sloppycomdat(ofl->ofl_lml,
1463 			    is_name, rep_sdp));
1464 			return (rep_sdp);
1465 		    }
1466 		}
1467 	    }
1468 	}
1469 	/* END CSTYLED */
1470 
1471 	/* If didn't return above, we didn't find it */
1472 	return (NULL);
1473 }
1474 
1475 /*
1476  * Generate relocation descriptor and dispatch
1477  */
1478 static uintptr_t
1479 process_reld(Ofl_desc *ofl, Is_desc *isp, Rel_desc *reld, Word rsndx,
1480     Rel *reloc)
1481 {
1482 	Ifl_desc	*ifl = isp->is_file;
1483 	Word		rtype = reld->rel_rtype;
1484 	Sym_desc	*sdp;
1485 	Conv_inv_buf_t	inv_buf;
1486 
1487 	/*
1488 	 * Make sure the relocation is in the valid range.
1489 	 */
1490 	if (rtype >= M_R_NUM) {
1491 		eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_REL_INVALRELT),
1492 		    ifl->ifl_name, isp->is_name, rtype);
1493 		return (S_ERROR);
1494 	}
1495 
1496 	ofl->ofl_entrelscnt++;
1497 
1498 	/*
1499 	 * Special case: a register symbol associated with symbol index 0 is
1500 	 * initialized (i.e., relocated) to a constant from the r_addend field
1501 	 * rather than from a symbol value.
1502 	 */
1503 	if (IS_REGISTER(rtype) && (rsndx == 0)) {
1504 		reld->rel_sym = 0;
1505 		reld->rel_sname = MSG_ORIG(MSG_STR_EMPTY);
1506 
1507 		DBG_CALL(Dbg_reloc_in(ofl->ofl_lml, ELF_DBG_LD, M_MACH,
1508 		    isp->is_shdr->sh_type, (void *)reloc, isp->is_name,
1509 		    reld->rel_sname));
1510 		return (ld_reloc_register(reld, isp, ofl));
1511 	}
1512 
1513 	/*
1514 	 * Determine whether we're dealing with a named symbol.  Note, bogus
1515 	 * relocations can result in a null symbol descriptor (sdp), the error
1516 	 * condition should be caught below after determining whether a valid
1517 	 * symbol name exists.
1518 	 */
1519 	sdp = ifl->ifl_oldndx[rsndx];
1520 	if (sdp != NULL && sdp->sd_name && *sdp->sd_name)
1521 		reld->rel_sname = sdp->sd_name;
1522 	else {
1523 		static char *strunknown;
1524 
1525 		if (strunknown == 0)
1526 			strunknown = (char *)MSG_INTL(MSG_STR_UNKNOWN);
1527 		reld->rel_sname = strunknown;
1528 	}
1529 
1530 	/*
1531 	 * If for some reason we have a null relocation record issue a
1532 	 * warning and continue (the compiler folks can get into this
1533 	 * state some time).  Normal users should never see this error.
1534 	 */
1535 	if (rtype == M_R_NONE) {
1536 		DBG_CALL(Dbg_reloc_in(ofl->ofl_lml, ELF_DBG_LD, M_MACH,
1537 		    M_REL_SHT_TYPE, (void *)reloc, isp->is_name,
1538 		    reld->rel_sname));
1539 		eprintf(ofl->ofl_lml, ERR_WARNING, MSG_INTL(MSG_REL_NULL),
1540 		    ifl->ifl_name, isp->is_name);
1541 		return (1);
1542 	}
1543 
1544 	if (((ofl->ofl_flags & FLG_OF_RELOBJ) == 0) && IS_NOTSUP(rtype)) {
1545 		eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_REL_NOTSUP),
1546 		    conv_reloc_type(ifl->ifl_ehdr->e_machine, rtype,
1547 		    0, &inv_buf), ifl->ifl_name, isp->is_name);
1548 		return (S_ERROR);
1549 	}
1550 
1551 	/*
1552 	 * If we are here, we know that the relocation requires reference
1553 	 * symbol. If no symbol is assigned, this is a fatal error.
1554 	 */
1555 	if (sdp == NULL) {
1556 		eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_REL_NOSYMBOL),
1557 		    conv_reloc_type(ifl->ifl_ehdr->e_machine, rtype,
1558 		    0, &inv_buf), isp->is_name, ifl->ifl_name,
1559 		    EC_XWORD(reloc->r_offset));
1560 		return (S_ERROR);
1561 	}
1562 
1563 	if (sdp->sd_flags1 & FLG_SY1_IGNORE)
1564 		return (1);
1565 
1566 	/*
1567 	 * If this symbol is part of a DISCARDED section attempt to find another
1568 	 * definition.
1569 	 */
1570 	if (sdp->sd_flags & FLG_SY_ISDISC) {
1571 		Sym_desc *nsdp = NULL;
1572 
1573 		if (ELF_ST_BIND(sdp->sd_sym->st_info) == STB_LOCAL) {
1574 			/*
1575 			 * If "-z relaxreloc", then check to see if
1576 			 * this is a reference to a discarded COMDAT
1577 			 * section that can be replaced with the
1578 			 * one that was kept.
1579 			 */
1580 			if (ofl->ofl_flags1 & FLG_OF1_RLXREL)
1581 				nsdp = sloppy_comdat_reloc(ofl, reld, sdp);
1582 		} else if (reld->rel_sname == sdp->sd_name) {
1583 			nsdp = ld_sym_find(sdp->sd_name, SYM_NOHASH, 0, ofl);
1584 		}
1585 		if (nsdp == 0) {
1586 			eprintf(ofl->ofl_lml, ERR_FATAL,
1587 			    MSG_INTL(MSG_REL_SYMDISC),
1588 			    ifl->ifl_name, isp->is_name,
1589 			    demangle(sdp->sd_name), sdp->sd_isc->is_name);
1590 			return (S_ERROR);
1591 		}
1592 		ifl->ifl_oldndx[rsndx] = sdp = nsdp;
1593 	}
1594 
1595 	/*
1596 	 * If this is a global symbol, determine whether its visibility needs
1597 	 * adjusting.
1598 	 */
1599 	if (sdp->sd_aux && ((sdp->sd_flags & FLG_SY_VISIBLE) == 0))
1600 		ld_sym_adjust_vis(sdp, ofl);
1601 
1602 	/*
1603 	 * Ignore any relocation against a section that will not be in the
1604 	 * output file (has been stripped).
1605 	 */
1606 	if ((sdp->sd_isc == 0) &&
1607 	    (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION))
1608 		return (1);
1609 
1610 	/*
1611 	 * If the input section exists, but the section has not been associated
1612 	 * to an output section, then this is a little suspicious.
1613 	 */
1614 	if (sdp->sd_isc && (sdp->sd_isc->is_osdesc == 0) &&
1615 	    (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION)) {
1616 		eprintf(ofl->ofl_lml, ERR_WARNING, MSG_INTL(MSG_RELINVSEC),
1617 		    conv_reloc_type(ifl->ifl_ehdr->e_machine, rtype,
1618 		    0, &inv_buf), ifl->ifl_name, isp->is_name,
1619 		    sdp->sd_isc->is_name);
1620 		return (1);
1621 	}
1622 
1623 	/*
1624 	 * If the symbol for this relocation is invalid (which should have
1625 	 * generated a message during symbol processing), or the relocation
1626 	 * record's symbol reference is in any other way invalid, then it's
1627 	 * about time we gave up.
1628 	 */
1629 	if ((sdp->sd_flags & FLG_SY_INVALID) || (rsndx == 0) ||
1630 	    (rsndx >= ifl->ifl_symscnt)) {
1631 		eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_REL_UNKNWSYM),
1632 		    conv_reloc_type(ifl->ifl_ehdr->e_machine, rtype,
1633 		    0, &inv_buf), ifl->ifl_name, isp->is_name,
1634 		    demangle(reld->rel_sname), EC_XWORD(reloc->r_offset),
1635 		    EC_WORD(rsndx));
1636 		return (S_ERROR);
1637 	}
1638 
1639 	/*
1640 	 * Size relocations against section symbols are presently unsupported.
1641 	 * There is a question as to whether the input section size, or output
1642 	 * section size would be used.  Until an explicit requirement is
1643 	 * established for either case, we'll punt.
1644 	 */
1645 	if (IS_SIZE(rtype) &&
1646 	    (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION)) {
1647 		eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_REL_UNSUPSIZE),
1648 		    conv_reloc_type(ifl->ifl_ehdr->e_machine, rtype,
1649 		    0, &inv_buf), ifl->ifl_name, isp->is_name);
1650 		return (S_ERROR);
1651 	}
1652 
1653 	reld->rel_sym = sdp;
1654 	return (ld_process_sym_reloc(ofl, reld, reloc, isp, isp->is_name));
1655 }
1656 
1657 static uintptr_t
1658 reloc_section(Ofl_desc *ofl, Is_desc *isect, Is_desc *rsect, Os_desc *osect)
1659 {
1660 	Rel		*rend;		/* end of relocation section data */
1661 	Rel		*reloc;		/* current relocation entry */
1662 	Xword		rsize;		/* size of relocation section data */
1663 	Xword		entsize;	/* size of relocation entry */
1664 	Rel_desc	reld;		/* relocation descriptor */
1665 	Shdr *		shdr;
1666 	Word		flags = 0;
1667 	uintptr_t	ret = 1;
1668 
1669 	shdr = rsect->is_shdr;
1670 	rsize = shdr->sh_size;
1671 	reloc = (Rel *)rsect->is_indata->d_buf;
1672 
1673 	/*
1674 	 * Decide entry size.
1675 	 */
1676 	if (((entsize = shdr->sh_entsize) == 0) || (entsize > rsize)) {
1677 		if (shdr->sh_type == SHT_RELA)
1678 			entsize = sizeof (Rela);
1679 		else
1680 			entsize = sizeof (Rel);
1681 	}
1682 
1683 	/*
1684 	 * Build up the basic information in for the Rel_desc structure.
1685 	 */
1686 	reld.rel_osdesc = osect;
1687 	reld.rel_isdesc = isect;
1688 	reld.rel_move = 0;
1689 
1690 	if ((ofl->ofl_flags & FLG_OF_RELOBJ) ||
1691 	    (osect && (osect->os_sgdesc->sg_phdr.p_type == PT_LOAD)))
1692 		flags |= FLG_REL_LOAD;
1693 
1694 	if (shdr->sh_info == 0)
1695 		flags |= FLG_REL_NOINFO;
1696 
1697 	DBG_CALL(Dbg_reloc_proc(ofl->ofl_lml, osect, isect, rsect));
1698 
1699 	for (rend = (Rel *)((uintptr_t)reloc + (uintptr_t)rsize);
1700 	    reloc < rend;
1701 	    reloc = (Rel *)((uintptr_t)reloc + (uintptr_t)entsize)) {
1702 		Word	rsndx;
1703 
1704 		/*
1705 		 * Initialize the relocation record information and process
1706 		 * the individual relocation.  Reinitialize the flags to
1707 		 * insure we don't carry any state over from the previous
1708 		 * relocation records processing.
1709 		 */
1710 		reld.rel_flags = flags;
1711 		rsndx = ld_init_rel(&reld, (void *)reloc);
1712 
1713 		if (process_reld(ofl, rsect, &reld, rsndx, reloc) == S_ERROR)
1714 			ret = S_ERROR;
1715 	}
1716 	return (ret);
1717 }
1718 
1719 static uintptr_t
1720 reloc_segments(int wr_flag, Ofl_desc *ofl)
1721 {
1722 	Listnode	*lnp1;
1723 	Sg_desc		*sgp;
1724 	Is_desc		*isp;
1725 
1726 	for (LIST_TRAVERSE(&ofl->ofl_segs, lnp1, sgp)) {
1727 		Os_desc	**ospp;
1728 		Aliste	off;
1729 
1730 		if ((sgp->sg_phdr.p_flags & PF_W) != wr_flag)
1731 			continue;
1732 
1733 		for (ALIST_TRAVERSE(sgp->sg_osdescs, off, ospp)) {
1734 			Is_desc		*risp;
1735 			Listnode	*lnp3;
1736 			Os_desc		*osp = *ospp;
1737 
1738 			osp->os_szoutrels = 0;
1739 			for (LIST_TRAVERSE(&(osp->os_relisdescs), lnp3, risp)) {
1740 				Word	indx;
1741 
1742 				/*
1743 				 * Determine the input section that this
1744 				 * relocation information refers to.
1745 				 */
1746 				indx = risp->is_shdr->sh_info;
1747 				isp = risp->is_file->ifl_isdesc[indx];
1748 
1749 				/*
1750 				 * Do not process relocations against sections
1751 				 * which are being discarded (COMDAT)
1752 				 */
1753 				if (isp->is_flags & FLG_IS_DISCARD)
1754 					continue;
1755 
1756 				if (reloc_section(ofl, isp, risp, osp) ==
1757 				    S_ERROR)
1758 					return (S_ERROR);
1759 			}
1760 
1761 			/*
1762 			 * Check for relocations against non-writable
1763 			 * allocatable sections.
1764 			 */
1765 			if ((osp->os_szoutrels) &&
1766 			    (sgp->sg_phdr.p_type == PT_LOAD) &&
1767 			    ((sgp->sg_phdr.p_flags & PF_W) == 0)) {
1768 				ofl->ofl_flags |= FLG_OF_TEXTREL;
1769 				ofl->ofl_dtflags |= DF_TEXTREL;
1770 			}
1771 		}
1772 	}
1773 
1774 	return (1);
1775 }
1776 
1777 /*
1778  * Move Section related function
1779  * Get move entry
1780  */
1781 static Move *
1782 get_move_entry(Is_desc *rsect, Xword roffset)
1783 {
1784 	Ifl_desc	*ifile = rsect->is_file;
1785 	Shdr		*rshdr = rsect->is_shdr;
1786 	Is_desc		*misp;
1787 	Shdr		*mshdr;
1788 	Xword 		midx;
1789 	Move		*ret;
1790 
1791 	/*
1792 	 * Set info for the target move section
1793 	 */
1794 	misp = ifile->ifl_isdesc[rshdr->sh_info];
1795 	mshdr = (ifile->ifl_isdesc[rshdr->sh_info])->is_shdr;
1796 
1797 	if (mshdr->sh_entsize == 0)
1798 		return ((Move *)0);
1799 	midx = roffset / mshdr->sh_entsize;
1800 
1801 	ret = (Move *)misp->is_indata->d_buf;
1802 	ret += midx;
1803 
1804 	/*
1805 	 * If this is an illgal entry, retun NULL.
1806 	 */
1807 	if ((midx * mshdr->sh_entsize) >= mshdr->sh_size)
1808 		return ((Move *)0);
1809 	return (ret);
1810 }
1811 
1812 /*
1813  * Relocation against Move Table.
1814  */
1815 static uintptr_t
1816 process_movereloc(Ofl_desc *ofl, Is_desc *rsect)
1817 {
1818 	Ifl_desc	*file = rsect->is_file;
1819 	Rel		*rend, *reloc;
1820 	Xword 		rsize, entsize;
1821 	static Rel_desc reld_zero;
1822 	Rel_desc 	reld;
1823 
1824 	rsize = rsect->is_shdr->sh_size;
1825 	reloc = (Rel *)rsect->is_indata->d_buf;
1826 
1827 	reld = reld_zero;
1828 
1829 	/*
1830 	 * Decide entry size
1831 	 */
1832 	entsize = rsect->is_shdr->sh_entsize;
1833 	if ((entsize == 0) ||
1834 	    (entsize > rsect->is_shdr->sh_size)) {
1835 		if (rsect->is_shdr->sh_type == SHT_RELA)
1836 			entsize = sizeof (Rela);
1837 		else
1838 			entsize = sizeof (Rel);
1839 	}
1840 
1841 	/*
1842 	 * Go through the relocation entries.
1843 	 */
1844 	for (rend = (Rel *)((uintptr_t)reloc + (uintptr_t)rsize);
1845 	    reloc < rend;
1846 	    reloc = (Rel *)((uintptr_t)reloc + (uintptr_t)entsize)) {
1847 		Sym_desc *	psdp;
1848 		Move *		mp;
1849 		Word		rsndx;
1850 
1851 		/*
1852 		 * Initialize the relocation record information.
1853 		 */
1854 		reld.rel_flags = FLG_REL_LOAD;
1855 		rsndx = ld_init_rel(&reld, (void *)reloc);
1856 
1857 		if (((mp = get_move_entry(rsect, reloc->r_offset)) == 0) ||
1858 		    ((reld.rel_move = libld_malloc(sizeof (Mv_desc))) == 0))
1859 			return (S_ERROR);
1860 
1861 		psdp = file->ifl_oldndx[ELF_M_SYM(mp->m_info)];
1862 		reld.rel_move->mvd_move = mp;
1863 		reld.rel_move->mvd_sym = psdp;
1864 
1865 		if (psdp->sd_flags & FLG_SY_PAREXPN) {
1866 			int	_num, num;
1867 
1868 			reld.rel_osdesc = ofl->ofl_issunwdata1->is_osdesc;
1869 			reld.rel_isdesc = ofl->ofl_issunwdata1;
1870 			reld.rel_roffset = mp->m_poffset;
1871 
1872 			for (num = mp->m_repeat, _num = 0; _num < num; _num++) {
1873 				reld.rel_roffset +=
1874 				    /* LINTED */
1875 				    (_num * ELF_M_SIZE(mp->m_info));
1876 
1877 				/*
1878 				 * Generate Reld
1879 				 */
1880 				if (process_reld(ofl,
1881 				    rsect, &reld, rsndx, reloc) == S_ERROR)
1882 					return (S_ERROR);
1883 			}
1884 		} else {
1885 			/*
1886 			 * Generate Reld
1887 			 */
1888 			reld.rel_flags |= FLG_REL_MOVETAB;
1889 			reld.rel_osdesc = ofl->ofl_osmove;
1890 			reld.rel_isdesc =
1891 			    ofl->ofl_osmove->os_isdescs.head->data;
1892 
1893 			if (process_reld(ofl,
1894 			    rsect, &reld, rsndx, reloc) == S_ERROR)
1895 				return (S_ERROR);
1896 		}
1897 	}
1898 	return (1);
1899 }
1900 
1901 /*
1902  * This function is similar to reloc_init().
1903  *
1904  * This function is called when the SHT_SUNW_move table is expanded
1905  * and there were relocation against the SHT_SUNW_move section.
1906  */
1907 static uintptr_t
1908 reloc_movesections(Ofl_desc *ofl)
1909 {
1910 	Listnode	*lnp1;
1911 	Is_desc		*risp;
1912 	uintptr_t	ret = 1;
1913 
1914 	/*
1915 	 * Generate/Expand relocation entries
1916 	 */
1917 	for (LIST_TRAVERSE(&ofl->ofl_mvrelisdescs, lnp1, risp)) {
1918 		if (process_movereloc(ofl, risp) == S_ERROR)
1919 			ret = S_ERROR;
1920 	}
1921 
1922 	return (ret);
1923 }
1924 
1925 /*
1926  * Count the number of output relocation entries, global offset table entries,
1927  * and procedure linkage table entries.  This function searches the segment and
1928  * outsect lists and passes each input reloc section to process_reloc().
1929  * It allocates space for any output relocations needed.  And builds up
1930  * the relocation structures for later processing.
1931  */
1932 uintptr_t
1933 ld_reloc_init(Ofl_desc *ofl)
1934 {
1935 	Listnode	*lnp;
1936 	Is_desc		*isp;
1937 	Sym_desc	*sdp;
1938 
1939 	/*
1940 	 * At this point we have finished processing all input symbols.  Make
1941 	 * sure we add any absolute (internal) symbols before continuing with
1942 	 * any relocation processing.
1943 	 */
1944 	if (ld_sym_spec(ofl) == S_ERROR)
1945 		return (S_ERROR);
1946 
1947 	ofl->ofl_gotcnt = M_GOT_XNumber;
1948 
1949 	/*
1950 	 * First process all of the relocations against NON-writable
1951 	 * segments followed by relocations against the writeable segments.
1952 	 *
1953 	 * This separation is so that when the writable segments are processed
1954 	 * we know whether or not a COPYRELOC will be produced for any symbols.
1955 	 * If relocations aren't processed in this order, a COPYRELOC and a
1956 	 * regular relocation can be produced against the same symbol.  The
1957 	 * regular relocation would be redundant.
1958 	 */
1959 	if (reloc_segments(0, ofl) == S_ERROR)
1960 		return (S_ERROR);
1961 
1962 	if (reloc_segments(PF_W, ofl) == S_ERROR)
1963 		return (S_ERROR);
1964 
1965 	/*
1966 	 * Process any extra relocations.  These are relocation sections that
1967 	 * have a NULL sh_info.
1968 	 */
1969 	for (LIST_TRAVERSE(&ofl->ofl_extrarels, lnp, isp)) {
1970 		if (reloc_section(ofl, NULL, isp, NULL) == S_ERROR)
1971 			return (S_ERROR);
1972 	}
1973 
1974 	/*
1975 	 * If there were relocation against move table,
1976 	 * process the relocation sections.
1977 	 */
1978 	if (reloc_movesections(ofl) == S_ERROR)
1979 		return (S_ERROR);
1980 
1981 	/*
1982 	 * Now all the relocations are pre-processed,
1983 	 * check the validity of copy relocations.
1984 	 */
1985 	if (ofl->ofl_copyrels.head != 0) {
1986 		Copy_rel	*cpyrel;
1987 
1988 		for (LIST_TRAVERSE(&ofl->ofl_copyrels, lnp, cpyrel)) {
1989 			sdp = cpyrel->copyrel_symd;
1990 
1991 			/*
1992 			 * If there were no displacement relocation
1993 			 * in this file, don't worry about it.
1994 			 */
1995 			if (sdp->sd_file->ifl_flags &
1996 			    (FLG_IF_DISPPEND | FLG_IF_DISPDONE))
1997 				is_disp_copied(ofl, cpyrel);
1998 		}
1999 	}
2000 
2001 	/*
2002 	 * GOT sections are created for dynamic executables and shared objects
2003 	 * if the FLG_OF_BLDGOT is set, or explicit reference has been made to
2004 	 * a GOT symbol.
2005 	 */
2006 	if (((ofl->ofl_flags & FLG_OF_RELOBJ) == 0) &&
2007 	    ((ofl->ofl_flags & FLG_OF_BLDGOT) ||
2008 	    ((((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_GOFTBL),
2009 	    SYM_NOHASH, 0, ofl)) != 0) ||
2010 	    ((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_GOFTBL_U),
2011 	    SYM_NOHASH, 0, ofl)) != 0)) && (sdp->sd_ref != REF_DYN_SEEN)))) {
2012 		if (ld_make_got(ofl) == S_ERROR)
2013 			return (S_ERROR);
2014 
2015 #if	defined(__sparc)
2016 		if (ld_allocate_got(ofl) == S_ERROR)
2017 			return (S_ERROR);
2018 #elif	defined(__x86)
2019 /* nothing to do */
2020 #else
2021 #error Unknown architecture!
2022 #endif
2023 	}
2024 
2025 	return (1);
2026 }
2027 
2028 /*
2029  * Simple comparison routine to be used by qsort() for
2030  * the sorting of the output relocation list.
2031  *
2032  * The reloc_compare() routine results in a relocation
2033  * table which is located on:
2034  *
2035  *	file referenced (NEEDED NDX)
2036  *	referenced symbol
2037  *	relocation offset
2038  *
2039  * This provides the most efficient traversal of the relocation
2040  * table at run-time.
2041  */
2042 static int
2043 reloc_compare(Reloc_list *i, Reloc_list *j)
2044 {
2045 
2046 	/*
2047 	 * first - sort on neededndx
2048 	 */
2049 	if (i->rl_key1 > j->rl_key1)
2050 		return (1);
2051 	if (i->rl_key1 < j->rl_key1)
2052 		return (-1);
2053 
2054 	/*
2055 	 * Then sort on symbol
2056 	 */
2057 	if ((uintptr_t)i->rl_key2 > (uintptr_t)j->rl_key2)
2058 		return (1);
2059 	if ((uintptr_t)i->rl_key2 < (uintptr_t)j->rl_key2)
2060 		return (-1);
2061 
2062 	/*
2063 	 * i->key2 == j->key2
2064 	 *
2065 	 * At this point we fall back to key2 (offsets) to
2066 	 * sort the output relocations.  Ideally this will
2067 	 * make for the most efficient processing of these
2068 	 * relocations at run-time.
2069 	 */
2070 	if (i->rl_key3 > j->rl_key3)
2071 		return (1);
2072 	if (i->rl_key3 < j->rl_key3)
2073 		return (-1);
2074 	return (0);
2075 }
2076 
2077 static uintptr_t
2078 do_sorted_outrelocs(Ofl_desc *ofl)
2079 {
2080 	Rel_desc	*orsp;
2081 	Rel_cache	*rcp;
2082 	Listnode	*lnp;
2083 	Reloc_list	*sorted_list;
2084 	Word		index = 0;
2085 	int		debug = 0;
2086 	uintptr_t	error = 1;
2087 
2088 	if ((sorted_list = libld_malloc((size_t)(sizeof (Reloc_list) *
2089 	    ofl->ofl_reloccnt))) == NULL)
2090 		return (S_ERROR);
2091 
2092 	/*
2093 	 * All but the PLT output relocations are sorted in the output file
2094 	 * based upon their sym_desc.  By doing this multiple relocations
2095 	 * against the same symbol are grouped together, thus when the object
2096 	 * is later relocated by ld.so.1 it will take advantage of the symbol
2097 	 * cache that ld.so.1 has.  This can significantly reduce the runtime
2098 	 * relocation cost of a dynamic object.
2099 	 *
2100 	 * PLT relocations are not sorted because the order of the PLT
2101 	 * relocations is used by ld.so.1 to determine what symbol a PLT
2102 	 * relocation is against.
2103 	 */
2104 	for (LIST_TRAVERSE(&ofl->ofl_outrels, lnp, rcp)) {
2105 		/*LINTED*/
2106 		for (orsp = (Rel_desc *)(rcp + 1);
2107 		    orsp < rcp->rc_free; orsp++) {
2108 			if (debug == 0) {
2109 				DBG_CALL(Dbg_reloc_dooutrel(ofl->ofl_lml,
2110 				    M_REL_SHT_TYPE));
2111 				debug = 1;
2112 			}
2113 
2114 			/*
2115 			 * If it's a PLT relocation we output it now in the
2116 			 * order that it was originally processed.
2117 			 */
2118 			if (orsp->rel_flags & FLG_REL_PLT) {
2119 				if (ld_perform_outreloc(orsp, ofl) == S_ERROR)
2120 					error = S_ERROR;
2121 				continue;
2122 			}
2123 
2124 			if ((orsp->rel_rtype == M_R_RELATIVE) ||
2125 			    (orsp->rel_rtype == M_R_REGISTER)) {
2126 				sorted_list[index].rl_key1 = 0;
2127 				sorted_list[index].rl_key2 =
2128 				    /* LINTED */
2129 				    (Sym_desc *)(uintptr_t)orsp->rel_rtype;
2130 			} else {
2131 				sorted_list[index].rl_key1 =
2132 				    orsp->rel_sym->sd_file->ifl_neededndx;
2133 				sorted_list[index].rl_key2 =
2134 				    orsp->rel_sym;
2135 			}
2136 
2137 			if (orsp->rel_flags & FLG_REL_GOT)
2138 				sorted_list[index].rl_key3 =
2139 				    ld_calc_got_offset(orsp, ofl);
2140 			else {
2141 				if (orsp->rel_rtype == M_R_REGISTER)
2142 					sorted_list[index].rl_key3 = 0;
2143 				else {
2144 					sorted_list[index].rl_key3 =
2145 					    orsp->rel_roffset +
2146 					    (Xword)_elf_getxoff(orsp->
2147 					    rel_isdesc->is_indata) +
2148 					    orsp->rel_isdesc->is_osdesc->
2149 					    os_shdr->sh_addr;
2150 				}
2151 			}
2152 
2153 			sorted_list[index++].rl_rsp = orsp;
2154 		}
2155 	}
2156 
2157 	qsort(sorted_list, (size_t)ofl->ofl_reloccnt, sizeof (Reloc_list),
2158 	    (int (*)(const void *, const void *))reloc_compare);
2159 
2160 	/*
2161 	 * All output relocations have now been sorted, go through
2162 	 * and process each relocation.
2163 	 */
2164 	for (index = 0; index < ofl->ofl_reloccnt; index++) {
2165 		if (ld_perform_outreloc(sorted_list[index].rl_rsp, ofl) ==
2166 		    S_ERROR)
2167 			error = S_ERROR;
2168 	}
2169 
2170 	return (error);
2171 }
2172 
2173 /*
2174  * Process relocations.  Finds every input relocation section for each output
2175  * section and invokes reloc_sec() to relocate that section.
2176  */
2177 uintptr_t
2178 ld_reloc_process(Ofl_desc *ofl)
2179 {
2180 	Listnode	*lnp1;
2181 	Sg_desc		*sgp;
2182 	Os_desc		*osp;
2183 	Word		ndx = 0, flags = ofl->ofl_flags;
2184 	Shdr		*shdr;
2185 
2186 	/*
2187 	 * Determine the index of the symbol table that will be referenced by
2188 	 * the relocation entries.
2189 	 */
2190 	if ((flags & (FLG_OF_DYNAMIC|FLG_OF_RELOBJ)) == FLG_OF_DYNAMIC)
2191 		/* LINTED */
2192 		ndx = (Word)elf_ndxscn(ofl->ofl_osdynsym->os_scn);
2193 	else if (!(flags & FLG_OF_STRIP) || (flags & FLG_OF_RELOBJ))
2194 		/* LINTED */
2195 		ndx = (Word)elf_ndxscn(ofl->ofl_ossymtab->os_scn);
2196 
2197 	/*
2198 	 * Re-initialize counters. These are used to provide relocation
2199 	 * offsets within the output buffers.
2200 	 */
2201 	ofl->ofl_relocpltsz = 0;
2202 	ofl->ofl_relocgotsz = 0;
2203 	ofl->ofl_relocbsssz = 0;
2204 
2205 	/*
2206 	 * Now that the output file is created and symbol update has occurred,
2207 	 * process the relocations collected in process_reloc().
2208 	 */
2209 	if (do_sorted_outrelocs(ofl) == S_ERROR)
2210 		return (S_ERROR);
2211 
2212 	if (ld_do_activerelocs(ofl) == S_ERROR)
2213 		return (S_ERROR);
2214 
2215 	if ((ofl->ofl_flags & FLG_OF_COMREL) == 0) {
2216 		/*
2217 		 * Process the relocation sections:
2218 		 *
2219 		 *  o	for each relocation section generated for the output
2220 		 *	image update its shdr information to reflect the
2221 		 *	symbol table it needs (sh_link) and the section to
2222 		 *	which the relocation must be applied (sh_info).
2223 		 */
2224 		for (LIST_TRAVERSE(&ofl->ofl_segs, lnp1, sgp)) {
2225 			Os_desc **ospp;
2226 			Aliste	off;
2227 
2228 			for (ALIST_TRAVERSE(sgp->sg_osdescs, off, ospp)) {
2229 				osp = *ospp;
2230 
2231 				if (osp->os_relosdesc == 0)
2232 					continue;
2233 
2234 				shdr = osp->os_relosdesc->os_shdr;
2235 				shdr->sh_link = ndx;
2236 				/* LINTED */
2237 				shdr->sh_info = (Word)elf_ndxscn(osp->os_scn);
2238 			}
2239 		}
2240 
2241 		/*
2242 		 * Since the .rel[a] section is not tied to any specific
2243 		 * section, we'd not have found it above.
2244 		 */
2245 		if ((osp = ofl->ofl_osrel) != NULL) {
2246 			shdr = osp->os_shdr;
2247 			shdr->sh_link = ndx;
2248 			shdr->sh_info = 0;
2249 		}
2250 	} else {
2251 		/*
2252 		 * We only have two relocation sections here, (PLT's,
2253 		 * coalesced) so just hit them directly instead of stepping
2254 		 * over the output sections.
2255 		 */
2256 		if ((osp = ofl->ofl_osrelhead) != NULL) {
2257 			shdr = osp->os_shdr;
2258 			shdr->sh_link = ndx;
2259 			shdr->sh_info = 0;
2260 		}
2261 		if (((osp = ofl->ofl_osplt) != NULL) && osp->os_relosdesc) {
2262 			shdr = osp->os_relosdesc->os_shdr;
2263 			shdr->sh_link = ndx;
2264 			/* LINTED */
2265 			shdr->sh_info = (Word)elf_ndxscn(osp->os_scn);
2266 		}
2267 	}
2268 
2269 	/*
2270 	 * If the -z text option was given, and we have output relocations
2271 	 * against a non-writable, allocatable section, issue a diagnostic and
2272 	 * return (the actual entries that caused this error would have been
2273 	 * output during the relocating section phase).
2274 	 */
2275 	if ((flags & (FLG_OF_PURETXT | FLG_OF_TEXTREL)) ==
2276 	    (FLG_OF_PURETXT | FLG_OF_TEXTREL)) {
2277 		eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_REL_REMAIN_3));
2278 		return (S_ERROR);
2279 	}
2280 
2281 	/*
2282 	 * Finally, initialize the first got entry with the address of the
2283 	 * .dynamic section (_DYNAMIC).
2284 	 */
2285 	if (flags & FLG_OF_DYNAMIC) {
2286 		if (ld_fillin_gotplt(ofl) == S_ERROR)
2287 			return (S_ERROR);
2288 	}
2289 
2290 	/*
2291 	 * Now that any GOT information has been written, display the debugging
2292 	 * information if required.
2293 	 */
2294 	if ((osp = ofl->ofl_osgot) != NULL)
2295 		DBG_CALL(Dbg_got_display(ofl, osp->os_shdr->sh_addr, 1));
2296 
2297 	return (1);
2298 }
2299 
2300 /*
2301  * If the -z text option was given, and we have output relocations against a
2302  * non-writable, allocatable section, issue a diagnostic. Print offending
2303  * symbols in tabular form similar to the way undefined symbols are presented.
2304  * Called from reloc_count().  The actual fatal error condition is triggered on
2305  * in reloc_process() above.
2306  *
2307  * Note.  For historic reasons -ztext is not a default option (however all OS
2308  * shared object builds use this option).  It can be argued that this option
2309  * should also be default when generating an a.out (see 1163979).  However, if
2310  * an a.out contains text relocations it is either because the user is creating
2311  * something pretty weird (they've used the -b or -znodefs options), or because
2312  * the library against which they're building wasn't constructed correctly (ie.
2313  * a function has a NOTYPE type, in which case the a.out won't generate an
2314  * associated plt).  In the latter case the builder of the a.out can't do
2315  * anything to fix the error - thus we've chosen not to give the user an error,
2316  * or warning, for this case.
2317  */
2318 static void
2319 reloc_remain_title(Ofl_desc *ofl, int warning)
2320 {
2321 	const char	*str1;
2322 
2323 	if (warning)
2324 		str1 = MSG_INTL(MSG_REL_RMN_ITM_13);
2325 	else
2326 		str1 = MSG_INTL(MSG_REL_RMN_ITM_11);
2327 
2328 	eprintf(ofl->ofl_lml, ERR_NONE, MSG_INTL(MSG_REL_REMAIN_FMT_1), str1,
2329 	    MSG_INTL(MSG_REL_RMN_ITM_31), MSG_INTL(MSG_REL_RMN_ITM_12),
2330 	    MSG_INTL(MSG_REL_RMN_ITM_2), MSG_INTL(MSG_REL_RMN_ITM_32));
2331 }
2332 
2333 void
2334 ld_reloc_remain_entry(Rel_desc *orsp, Os_desc *osp, Ofl_desc *ofl)
2335 {
2336 	static Boolean	reloc_title = TRUE;
2337 
2338 	/*
2339 	 * -ztextoff
2340 	 */
2341 	if (ofl->ofl_flags1 & FLG_OF1_TEXTOFF)
2342 		return;
2343 
2344 	/*
2345 	 * Only give relocation errors against loadable read-only segments.
2346 	 */
2347 	if ((orsp->rel_rtype == M_R_REGISTER) || (!osp) ||
2348 	    (osp->os_sgdesc->sg_phdr.p_type != PT_LOAD) ||
2349 	    (osp->os_sgdesc->sg_phdr.p_flags & PF_W))
2350 		return;
2351 
2352 	/*
2353 	 * If we are in -ztextwarn mode, it's a silent error if a relocation is
2354 	 * due to a 'WEAK REFERENCE'.  This is because if the symbol is not
2355 	 * provided at run-time we will not perform a text-relocation.
2356 	 */
2357 	if (((ofl->ofl_flags & FLG_OF_PURETXT) == 0) &&
2358 	    (ELF_ST_BIND(orsp->rel_sym->sd_sym->st_info) == STB_WEAK) &&
2359 	    (orsp->rel_sym->sd_sym->st_shndx == SHN_UNDEF))
2360 		return;
2361 
2362 	if (reloc_title) {
2363 		/*
2364 		 * If building with '-ztext' then emit a fatal error.  If
2365 		 * building a executable then only emit a 'warning'.
2366 		 */
2367 		if (ofl->ofl_flags & FLG_OF_PURETXT)
2368 			reloc_remain_title(ofl, 0);
2369 		else
2370 			reloc_remain_title(ofl, 1);
2371 		reloc_title = FALSE;
2372 	}
2373 
2374 	eprintf(ofl->ofl_lml, ERR_NONE, MSG_INTL(MSG_REL_REMAIN_2),
2375 	    demangle(orsp->rel_sname), EC_OFF(orsp->rel_roffset),
2376 	    orsp->rel_isdesc->is_file->ifl_name);
2377 }
2378 
2379 /*
2380  * Generic encapsulation for generating a TLS got index.
2381  */
2382 uintptr_t
2383 ld_assign_got_TLS(Boolean local, Rel_desc *rsp, Ofl_desc *ofl, Sym_desc *sdp,
2384     Gotndx *gnp, Gotref gref, Word rflag, Word ortype, Word rtype1, Word rtype2)
2385 {
2386 	Word	rflags;
2387 
2388 	if (ld_assign_got_ndx(&(sdp->sd_GOTndxs), gnp, gref, ofl,
2389 	    rsp, sdp) == S_ERROR)
2390 		return (S_ERROR);
2391 
2392 	rflags = FLG_REL_GOT | rflag;
2393 	if (local)
2394 		rflags |= FLG_REL_SCNNDX;
2395 	rsp->rel_rtype = rtype1;
2396 
2397 	if (ld_add_outrel(rflags, rsp, ofl) == S_ERROR)
2398 		return (S_ERROR);
2399 
2400 	if (local && (gref == GOT_REF_TLSIE)) {
2401 		/*
2402 		 * If this is a local LE TLS symbol, then the symbol won't be
2403 		 * available at runtime.  The value of the local symbol will
2404 		 * be placed in the associated got entry, and the got
2405 		 * relocation is reassigned to a section symbol.
2406 		 */
2407 		if (ld_add_actrel(rflags, rsp, ofl) == S_ERROR)
2408 			return (S_ERROR);
2409 	}
2410 
2411 	if (rtype2) {
2412 		rflags = FLG_REL_GOT | rflag;
2413 		rsp->rel_rtype = rtype2;
2414 
2415 		if (local) {
2416 			if (ld_add_actrel(rflags, rsp, ofl) == S_ERROR)
2417 				return (S_ERROR);
2418 		} else {
2419 			if (ld_add_outrel(rflags, rsp, ofl) == S_ERROR)
2420 				return (S_ERROR);
2421 		}
2422 	}
2423 
2424 	rsp->rel_rtype = ortype;
2425 
2426 	return (1);
2427 }
2428 
2429 /*
2430  * Move Section related function
2431  */
2432 static uintptr_t
2433 newroffset_for_move(Sym_desc *symd,
2434 	Move *mventry, Xword offset1, Xword *offset2)
2435 {
2436 	Psym_info	*psym = symd->sd_psyminfo;
2437 	Mv_itm		*itm;
2438 	Listnode	*lnp1;
2439 	int 		found = 0;
2440 
2441 	/*
2442 	 * Search for matching move entry
2443 	 */
2444 	found = 0;
2445 	for (LIST_TRAVERSE(&psym->psym_mvs, lnp1, itm)) {
2446 		if (itm->mv_ientry == mventry) {
2447 			found = 1;
2448 			break;
2449 		}
2450 	}
2451 	if (found == 0) {
2452 		/*
2453 		 * This should never happen.
2454 		 */
2455 		return (S_ERROR);
2456 	}
2457 
2458 	/*
2459 	 * Update r_offset
2460 	 */
2461 	*offset2 = (Xword)((itm->mv_oidx - 1)*sizeof (Move) +
2462 	    offset1 % sizeof (Move));
2463 	return (1);
2464 }
2465 
2466 void
2467 ld_adj_movereloc(Ofl_desc *ofl, Rel_desc *arsp)
2468 {
2469 	Move		*move = arsp->rel_move->mvd_move;
2470 	Sym_desc	*psdp = arsp->rel_move->mvd_sym;
2471 	Xword		newoffset;
2472 
2473 	if (arsp->rel_flags & FLG_REL_MOVETAB) {
2474 		/*
2475 		 * We are relocating the move table itself.
2476 		 */
2477 		(void) newroffset_for_move(psdp, move, arsp->rel_roffset,
2478 		    &newoffset);
2479 		DBG_CALL(Dbg_move_adjmovereloc(ofl->ofl_lml, arsp->rel_roffset,
2480 		    newoffset, psdp->sd_name));
2481 		arsp->rel_roffset = newoffset;
2482 	} else {
2483 		/*
2484 		 * We are expanding the partial symbol.  So we are generating
2485 		 * the relocation entry relocating the expanded partial symbol.
2486 		 */
2487 		arsp->rel_roffset += psdp->sd_sym->st_value -
2488 		    ofl->ofl_issunwdata1->is_osdesc->os_shdr->sh_addr;
2489 		DBG_CALL(Dbg_move_adjexpandreloc(ofl->ofl_lml,
2490 		    arsp->rel_roffset, psdp->sd_name));
2491 	}
2492 }
2493 
2494 /*
2495  * Partially Initialized Symbol Handling routines
2496  * For sparc architecture, the second argument is reld->rel_raddend.
2497  * For i386  acrchitecure, the second argument is the value stored
2498  *	at the relocation target address.
2499  */
2500 Sym_desc *
2501 ld_am_I_partial(Rel_desc *reld, Xword val)
2502 {
2503 	Ifl_desc *	ifile = reld->rel_sym->sd_isc->is_file;
2504 	int 		nlocs = ifile->ifl_locscnt, i;
2505 
2506 	for (i = 1; i < nlocs; i++) {
2507 		Sym *		osym;
2508 		Sym_desc *	symd = ifile->ifl_oldndx[i];
2509 
2510 		if ((osym = symd->sd_osym) == 0)
2511 			continue;
2512 		if ((symd->sd_flags & FLG_SY_PAREXPN) == 0)
2513 			continue;
2514 		if ((osym->st_value <= val) &&
2515 		    (osym->st_value + osym->st_size  > val))
2516 			return (symd);
2517 	}
2518 	return ((Sym_desc *) 0);
2519 }
2520 
2521 /*
2522  * Because of the combinations of 32-bit lib providing 64-bit support, and
2523  * visa-versa, the use of krtld's dorelocs can result in differing message
2524  * requirements that make msg.c/msg.h creation and chkmsg "interesting".
2525  * Thus the actual message files contain a couple of entries to satisfy
2526  * each architectures build.  Here we add dummy calls to quieten chkmsg.
2527  *
2528  * chkmsg: MSG_INTL(MSG_REL_NOFIT)
2529  * chkmsg: MSG_INTL(MSG_REL_NONALIGN)
2530  */
2531