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