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