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