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