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