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