xref: /onnv-gate/usr/src/cmd/sgs/libld/common/update.c (revision 13074:787bf65954d0)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51618Srie  * Common Development and Distribution License (the "License").
61618Srie  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
211618Srie 
220Sstevel@tonic-gate /*
230Sstevel@tonic-gate  *	Copyright (c) 1988 AT&T
240Sstevel@tonic-gate  *	  All Rights Reserved
250Sstevel@tonic-gate  *
2612449SRod.Evans@Sun.COM  * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
270Sstevel@tonic-gate  */
280Sstevel@tonic-gate 
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate  * Update the new output file image, perform virtual address, offset and
310Sstevel@tonic-gate  * displacement calculations on the program headers and sections headers,
320Sstevel@tonic-gate  * and generate any new output section information.
330Sstevel@tonic-gate  */
346206Sab196087 
356206Sab196087 #define	ELF_TARGET_AMD64
366206Sab196087 
370Sstevel@tonic-gate #include	<stdio.h>
380Sstevel@tonic-gate #include	<string.h>
391722Sseizo #include	<unistd.h>
401618Srie #include	<debug.h>
410Sstevel@tonic-gate #include	"msg.h"
420Sstevel@tonic-gate #include	"_libld.h"
430Sstevel@tonic-gate 
440Sstevel@tonic-gate /*
452766Sab196087  * Comparison routine used by qsort() for sorting of the global symbol list
460Sstevel@tonic-gate  * based off of the hashbuckets the symbol will eventually be deposited in.
470Sstevel@tonic-gate  */
480Sstevel@tonic-gate static int
sym_hash_compare(Sym_s_list * s1,Sym_s_list * s2)490Sstevel@tonic-gate sym_hash_compare(Sym_s_list * s1, Sym_s_list * s2)
500Sstevel@tonic-gate {
510Sstevel@tonic-gate 	return (s1->sl_hval - s2->sl_hval);
520Sstevel@tonic-gate }
530Sstevel@tonic-gate 
540Sstevel@tonic-gate /*
553492Sab196087  * Comparison routine used by qsort() for sorting of dyn[sym|tls]sort section
563492Sab196087  * indices based on the address of the symbols they reference. The
573492Sab196087  * use of the global dynsort_compare_syms variable is needed because
583492Sab196087  * we need to examine the symbols the indices reference. It is safe, because
593492Sab196087  * the linker is single threaded.
603492Sab196087  */
613492Sab196087 Sym *dynsort_compare_syms;
623492Sab196087 
633492Sab196087 static int
dynsort_compare(const void * idx1,const void * idx2)643492Sab196087 dynsort_compare(const void *idx1, const void *idx2)
653492Sab196087 {
663492Sab196087 	Sym *s1 = dynsort_compare_syms + *((const Word *) idx1);
673492Sab196087 	Sym *s2 = dynsort_compare_syms + *((const Word *) idx2);
683492Sab196087 
693492Sab196087 	/*
703492Sab196087 	 * Note: the logical computation for this is
713492Sab196087 	 *	(st_value1 - st_value2)
723492Sab196087 	 * However, that is only correct if the address type is smaller
733492Sab196087 	 * than a pointer. Writing it this way makes it immune to the
743492Sab196087 	 * class (32 or 64-bit) of the linker.
753492Sab196087 	 */
763492Sab196087 	return ((s1->st_value < s2->st_value) ? -1 :
773492Sab196087 	    (s1->st_value > s2->st_value));
783492Sab196087 }
793492Sab196087 
803492Sab196087 /*
813492Sab196087  * Scan the sorted symbols, and issue warnings if there are any duplicate
823492Sab196087  * values in the list. We only do this if -zverbose is set, or we are
833492Sab196087  * running with LD_DEBUG defined
843492Sab196087  *
853492Sab196087  * entry:
863492Sab196087  *	ofl - Output file descriptor
873492Sab196087  *	ldynsym - Pointer to start of .SUNW_ldynsym section that the
883492Sab196087  *		sort section indexes reference.
893492Sab196087  *	symsort - Pointer to start of .SUNW_dynsymsort or .SUNW_dyntlssort
903492Sab196087  *		section.
913492Sab196087  *	n - # of indices in symsort array
923492Sab196087  *	secname - Name of the symsort section.
933492Sab196087  *
943492Sab196087  * exit:
953492Sab196087  *	If the symsort section contains indexes to more than one
963492Sab196087  *	symbol with the same address value, a warning is issued.
973492Sab196087  */
983492Sab196087 static void
dynsort_dupwarn(Ofl_desc * ofl,Sym * ldynsym,const char * str,Word * symsort,Word n,const char * secname)993492Sab196087 dynsort_dupwarn(Ofl_desc *ofl, Sym *ldynsym, const char *str,
1003492Sab196087     Word *symsort, Word n, const char *secname)
1013492Sab196087 {
1023492Sab196087 	int zverbose = (ofl->ofl_flags & FLG_OF_VERBOSE) != 0;
1033492Sab196087 	Word ndx, cmp_ndx;
1043492Sab196087 	Addr addr, cmp_addr;
1053492Sab196087 
1063492Sab196087 	/* Nothing to do if -zverbose or LD_DEBUG are not active */
1073492Sab196087 	if (!(zverbose || DBG_ENABLED))
1083492Sab196087 		return;
1093492Sab196087 
1103492Sab196087 	cmp_ndx = 0;
1113492Sab196087 	cmp_addr = ldynsym[symsort[cmp_ndx]].st_value;
1123492Sab196087 	for (ndx = 1; ndx < n; ndx++) {
1133492Sab196087 		addr = ldynsym[symsort[ndx]].st_value;
1143492Sab196087 		if (cmp_addr == addr) {
1153492Sab196087 			if (zverbose)
116*13074SAli.Bahrami@Oracle.COM 				ld_eprintf(ofl, ERR_WARNING,
1173492Sab196087 				    MSG_INTL(MSG_SYM_DUPSORTADDR), secname,
1183492Sab196087 				    str + ldynsym[symsort[cmp_ndx]].st_name,
1193492Sab196087 				    str + ldynsym[symsort[ndx]].st_name,
1203492Sab196087 				    EC_ADDR(addr));
1213492Sab196087 			DBG_CALL(Dbg_syms_dup_sort_addr(ofl->ofl_lml, secname,
1223492Sab196087 			    str + ldynsym[symsort[cmp_ndx]].st_name,
1233492Sab196087 			    str + ldynsym[symsort[ndx]].st_name,
1243492Sab196087 			    EC_ADDR(addr)));
1253492Sab196087 		} else {	/* Not a dup. Move reference up */
1263492Sab196087 			cmp_ndx = ndx;
1273492Sab196087 			cmp_addr = addr;
1283492Sab196087 		}
1293492Sab196087 	}
1303492Sab196087 }
1313492Sab196087 
1323492Sab196087 /*
1330Sstevel@tonic-gate  * Build and update any output symbol tables.  Here we work on all the symbol
1340Sstevel@tonic-gate  * tables at once to reduce the duplication of symbol and string manipulation.
1350Sstevel@tonic-gate  * Symbols and their associated strings are copied from the read-only input
1360Sstevel@tonic-gate  * file images to the output image and their values and index's updated in the
1370Sstevel@tonic-gate  * output image.
1380Sstevel@tonic-gate  */
1391618Srie static Addr
update_osym(Ofl_desc * ofl)1400Sstevel@tonic-gate update_osym(Ofl_desc *ofl)
1410Sstevel@tonic-gate {
1423492Sab196087 	/*
1433492Sab196087 	 * There are several places in this function where we wish
1443492Sab196087 	 * to insert a symbol index to the combined .SUNW_ldynsym/.dynsym
1453492Sab196087 	 * symbol table into one of the two sort sections (.SUNW_dynsymsort
1463492Sab196087 	 * or .SUNW_dyntlssort), if that symbol has the right attributes.
1473492Sab196087 	 * This macro is used to generate the necessary code from a single
1483492Sab196087 	 * specification.
1493492Sab196087 	 *
1503492Sab196087 	 * entry:
1513492Sab196087 	 *	_sdp, _sym, _type - As per DYNSORT_COUNT. See _libld.h
1523492Sab196087 	 *	_sym_ndx - Index that _sym will have in the combined
1533492Sab196087 	 *		.SUNW_ldynsym/.dynsym symbol table.
1543492Sab196087 	 */
1553492Sab196087 #define	ADD_TO_DYNSORT(_sdp, _sym, _type, _sym_ndx) \
1563492Sab196087 	{ \
1573492Sab196087 		Word *_dynsort_arr, *_dynsort_ndx; \
1583492Sab196087 		\
1593492Sab196087 		if (dynsymsort_symtype[_type]) { \
1603492Sab196087 			_dynsort_arr = dynsymsort; \
1613492Sab196087 			_dynsort_ndx = &dynsymsort_ndx; \
1623492Sab196087 		} else if (_type == STT_TLS) { \
1633492Sab196087 			_dynsort_arr = dyntlssort; \
1643492Sab196087 			_dynsort_ndx = &dyntlssort_ndx; \
1653492Sab196087 		} else { \
1663492Sab196087 			_dynsort_arr = NULL; \
1673492Sab196087 		} \
1683492Sab196087 		if ((_dynsort_arr != NULL) && DYNSORT_TEST_ATTR(_sdp, _sym)) \
16911827SRod.Evans@Sun.COM 		    _dynsort_arr[(*_dynsort_ndx)++] = _sym_ndx; \
1703492Sab196087 	}
1713492Sab196087 
1720Sstevel@tonic-gate 	Sym_desc	*sdp;
1730Sstevel@tonic-gate 	Sym_avlnode	*sav;
1749131SRod.Evans@Sun.COM 	Sg_desc		*sgp, *tsgp = NULL, *dsgp = NULL, *esgp = NULL;
1759131SRod.Evans@Sun.COM 	Os_desc		*osp, *iosp = NULL, *fosp = NULL;
1769131SRod.Evans@Sun.COM 	Is_desc		*isc;
1770Sstevel@tonic-gate 	Ifl_desc	*ifl;
1780Sstevel@tonic-gate 	Word		bssndx, etext_ndx, edata_ndx = 0, end_ndx, start_ndx;
1790Sstevel@tonic-gate 	Word		end_abs = 0, etext_abs = 0, edata_abs;
1808159SAli.Bahrami@Sun.COM 	Word		tlsbssndx = 0, parexpnndx;
1816206Sab196087 #if	defined(_ELF64)
182574Sseizo 	Word		lbssndx = 0;
183574Sseizo 	Addr		lbssaddr = 0;
184574Sseizo #endif
1850Sstevel@tonic-gate 	Addr		bssaddr, etext = 0, edata = 0, end = 0, start = 0;
1860Sstevel@tonic-gate 	Addr		tlsbssaddr = 0;
1878369SAli.Bahrami@Sun.COM 	Addr 		parexpnbase, parexpnaddr;
1880Sstevel@tonic-gate 	int		start_set = 0;
1899131SRod.Evans@Sun.COM 	Sym		_sym = {0}, *sym, *symtab = NULL;
1909131SRod.Evans@Sun.COM 	Sym		*dynsym = NULL, *ldynsym = NULL;
19110792SRod.Evans@Sun.COM 	Word		symtab_ndx = 0;		/* index into .symtab */
1928140SAli.Bahrami@Sun.COM 	Word		symtab_gbl_bndx;	/* .symtab ndx 1st global */
1932766Sab196087 	Word		ldynsym_ndx = 0;	/* index into .SUNW_ldynsym */
1942766Sab196087 	Word		dynsym_ndx = 0;		/* index into .dynsym */
19510792SRod.Evans@Sun.COM 	Word		scopesym_ndx = 0;	/* index into scoped symbols */
1968140SAli.Bahrami@Sun.COM 	Word		scopesym_bndx = 0;	/* .symtab ndx 1st scoped sym */
19710792SRod.Evans@Sun.COM 	Word		ldynscopesym_ndx = 0;	/* index to ldynsym scoped */
19810792SRod.Evans@Sun.COM 						/*	symbols */
19910792SRod.Evans@Sun.COM 	Word		*dynsymsort = NULL;	/* SUNW_dynsymsort index */
20010792SRod.Evans@Sun.COM 						/*	vector */
20110792SRod.Evans@Sun.COM 	Word		*dyntlssort = NULL;	/* SUNW_dyntlssort index */
20210792SRod.Evans@Sun.COM 						/*	vector */
2033492Sab196087 	Word		dynsymsort_ndx;		/* index dynsymsort array */
2043492Sab196087 	Word		dyntlssort_ndx;		/* index dyntlssort array */
20510792SRod.Evans@Sun.COM 	Word		*symndx;		/* symbol index (for */
20610792SRod.Evans@Sun.COM 						/*	relocation use) */
2079131SRod.Evans@Sun.COM 	Word		*symshndx = NULL;	/* .symtab_shndx table */
2089131SRod.Evans@Sun.COM 	Word		*dynshndx = NULL;	/* .dynsym_shndx table */
2099131SRod.Evans@Sun.COM 	Word		*ldynshndx = NULL;	/* .SUNW_ldynsym_shndx table */
21010792SRod.Evans@Sun.COM 	Word		ldynsym_cnt = NULL;	/* number of items in */
21110792SRod.Evans@Sun.COM 						/*	.SUNW_ldynsym */
2120Sstevel@tonic-gate 	Str_tbl		*shstrtab;
2130Sstevel@tonic-gate 	Str_tbl		*strtab;
2140Sstevel@tonic-gate 	Str_tbl		*dynstr;
2150Sstevel@tonic-gate 	Word		*hashtab;	/* hash table pointer */
2160Sstevel@tonic-gate 	Word		*hashbkt;	/* hash table bucket pointer */
2170Sstevel@tonic-gate 	Word		*hashchain;	/* hash table chain pointer */
2180Sstevel@tonic-gate 	Wk_desc		*wkp;
2199131SRod.Evans@Sun.COM 	Alist		*weak = NULL;
2206299Sab196087 	ofl_flag_t	flags = ofl->ofl_flags;
2210Sstevel@tonic-gate 	Versym		*versym;
2220Sstevel@tonic-gate 	Gottable	*gottable;	/* used for display got debugging */
2230Sstevel@tonic-gate 					/*	information */
2240Sstevel@tonic-gate 	Syminfo		*syminfo;
2250Sstevel@tonic-gate 	Sym_s_list	*sorted_syms;	/* table to hold sorted symbols */
2260Sstevel@tonic-gate 	Word		ssndx;		/* global index into sorted_syms */
2270Sstevel@tonic-gate 	Word		scndx;		/* scoped index into sorted_syms */
2285892Sab196087 	size_t		stoff;		/* string offset */
2299131SRod.Evans@Sun.COM 	Aliste		idx1;
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate 	/*
2320Sstevel@tonic-gate 	 * Initialize pointers to the symbol table entries and the symbol
2330Sstevel@tonic-gate 	 * table strings.  Skip the first symbol entry and the first string
2340Sstevel@tonic-gate 	 * table byte.  Note that if we are not generating any output symbol
23511227SAli.Bahrami@Sun.COM 	 * tables we must still generate and update internal copies so
2360Sstevel@tonic-gate 	 * that the relocation phase has the correct information.
2370Sstevel@tonic-gate 	 */
2380Sstevel@tonic-gate 	if (!(flags & FLG_OF_STRIP) || (flags & FLG_OF_RELOBJ) ||
2390Sstevel@tonic-gate 	    ((flags & FLG_OF_STATIC) && ofl->ofl_osversym)) {
2400Sstevel@tonic-gate 		symtab = (Sym *)ofl->ofl_ossymtab->os_outdata->d_buf;
2410Sstevel@tonic-gate 		symtab[symtab_ndx++] = _sym;
2420Sstevel@tonic-gate 		if (ofl->ofl_ossymshndx)
2434716Sab196087 			symshndx =
2444716Sab196087 			    (Word *)ofl->ofl_ossymshndx->os_outdata->d_buf;
2450Sstevel@tonic-gate 	}
2462766Sab196087 	if (OFL_ALLOW_DYNSYM(ofl)) {
2470Sstevel@tonic-gate 		dynsym = (Sym *)ofl->ofl_osdynsym->os_outdata->d_buf;
2480Sstevel@tonic-gate 		dynsym[dynsym_ndx++] = _sym;
2490Sstevel@tonic-gate 		/*
2502766Sab196087 		 * If we are also constructing a .SUNW_ldynsym section
2512766Sab196087 		 * to contain local function symbols, then set it up too.
2522766Sab196087 		 */
2532766Sab196087 		if (ofl->ofl_osldynsym) {
2542766Sab196087 			ldynsym = (Sym *)ofl->ofl_osldynsym->os_outdata->d_buf;
2552766Sab196087 			ldynsym[ldynsym_ndx++] = _sym;
2563492Sab196087 			ldynsym_cnt = 1 + ofl->ofl_dynlocscnt +
2573492Sab196087 			    ofl->ofl_dynscopecnt;
2583492Sab196087 
2593492Sab196087 			/*
2603492Sab196087 			 * If there is a SUNW_ldynsym, then there may also
2613492Sab196087 			 * be a .SUNW_dynsymsort and/or .SUNW_dyntlssort
2623492Sab196087 			 * sections, used to collect indices of function
2633492Sab196087 			 * and data symbols sorted by address order.
2643492Sab196087 			 */
2653492Sab196087 			if (ofl->ofl_osdynsymsort) {	/* .SUNW_dynsymsort */
2663492Sab196087 				dynsymsort = (Word *)
2673492Sab196087 				    ofl->ofl_osdynsymsort->os_outdata->d_buf;
2683492Sab196087 				dynsymsort_ndx = 0;
2693492Sab196087 			}
2703492Sab196087 			if (ofl->ofl_osdyntlssort) {	/* .SUNW_dyntlssort */
2713492Sab196087 				dyntlssort = (Word *)
2723492Sab196087 				    ofl->ofl_osdyntlssort->os_outdata->d_buf;
2733492Sab196087 				dyntlssort_ndx = 0;
2743492Sab196087 			}
2752766Sab196087 		}
2762766Sab196087 
2772766Sab196087 		/*
2780Sstevel@tonic-gate 		 * Initialize the hash table.
2790Sstevel@tonic-gate 		 */
2800Sstevel@tonic-gate 		hashtab = (Word *)(ofl->ofl_oshash->os_outdata->d_buf);
2810Sstevel@tonic-gate 		hashbkt = &hashtab[2];
2820Sstevel@tonic-gate 		hashchain = &hashtab[2 + ofl->ofl_hashbkts];
2830Sstevel@tonic-gate 		hashtab[0] = ofl->ofl_hashbkts;
28410792SRod.Evans@Sun.COM 		hashtab[1] = DYNSYM_ALL_CNT(ofl);
2850Sstevel@tonic-gate 		if (ofl->ofl_osdynshndx)
2864716Sab196087 			dynshndx =
2874716Sab196087 			    (Word *)ofl->ofl_osdynshndx->os_outdata->d_buf;
2882766Sab196087 		if (ofl->ofl_osldynshndx)
2894716Sab196087 			ldynshndx =
2904716Sab196087 			    (Word *)ofl->ofl_osldynshndx->os_outdata->d_buf;
2910Sstevel@tonic-gate 	}
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate 	/*
2940Sstevel@tonic-gate 	 * symndx is the symbol index to be used for relocation processing.  It
2950Sstevel@tonic-gate 	 * points to the relevant symtab's (.dynsym or .symtab) symbol ndx.
2960Sstevel@tonic-gate 	 */
2970Sstevel@tonic-gate 	if (dynsym)
2980Sstevel@tonic-gate 		symndx = &dynsym_ndx;
2990Sstevel@tonic-gate 	else
3000Sstevel@tonic-gate 		symndx = &symtab_ndx;
3010Sstevel@tonic-gate 
3020Sstevel@tonic-gate 	/*
3030Sstevel@tonic-gate 	 * If we have version definitions initialize the version symbol index
3040Sstevel@tonic-gate 	 * table.  There is one entry for each symbol which contains the symbols
3050Sstevel@tonic-gate 	 * version index.
3060Sstevel@tonic-gate 	 */
3077682SAli.Bahrami@Sun.COM 	if (!(flags & FLG_OF_NOVERSEC) &&
3087682SAli.Bahrami@Sun.COM 	    (flags & (FLG_OF_VERNEED | FLG_OF_VERDEF))) {
3090Sstevel@tonic-gate 		versym = (Versym *)ofl->ofl_osversym->os_outdata->d_buf;
31010167SRod.Evans@Sun.COM 		versym[0] = NULL;
3110Sstevel@tonic-gate 	} else
31210167SRod.Evans@Sun.COM 		versym = NULL;
3130Sstevel@tonic-gate 
3140Sstevel@tonic-gate 	/*
3150Sstevel@tonic-gate 	 * If syminfo section exists be prepared to fill it in.
3160Sstevel@tonic-gate 	 */
3170Sstevel@tonic-gate 	if (ofl->ofl_ossyminfo) {
3180Sstevel@tonic-gate 		syminfo = ofl->ofl_ossyminfo->os_outdata->d_buf;
3190Sstevel@tonic-gate 		syminfo[0].si_flags = SYMINFO_CURRENT;
3200Sstevel@tonic-gate 	} else
32110167SRod.Evans@Sun.COM 		syminfo = NULL;
3220Sstevel@tonic-gate 
3230Sstevel@tonic-gate 	/*
3240Sstevel@tonic-gate 	 * Setup our string tables.
3250Sstevel@tonic-gate 	 */
3260Sstevel@tonic-gate 	shstrtab = ofl->ofl_shdrsttab;
3270Sstevel@tonic-gate 	strtab = ofl->ofl_strtab;
3280Sstevel@tonic-gate 	dynstr = ofl->ofl_dynstrtab;
3290Sstevel@tonic-gate 
3301618Srie 	DBG_CALL(Dbg_syms_sec_title(ofl->ofl_lml));
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate 	/*
3332766Sab196087 	 * Put output file name to the first .symtab and .SUNW_ldynsym symbol.
3340Sstevel@tonic-gate 	 */
3350Sstevel@tonic-gate 	if (symtab) {
3360Sstevel@tonic-gate 		(void) st_setstring(strtab, ofl->ofl_name, &stoff);
3370Sstevel@tonic-gate 		sym = &symtab[symtab_ndx++];
3380Sstevel@tonic-gate 		/* LINTED */
3390Sstevel@tonic-gate 		sym->st_name = stoff;
3400Sstevel@tonic-gate 		sym->st_value = 0;
3410Sstevel@tonic-gate 		sym->st_size = 0;
3420Sstevel@tonic-gate 		sym->st_info = ELF_ST_INFO(STB_LOCAL, STT_FILE);
3430Sstevel@tonic-gate 		sym->st_other = 0;
3440Sstevel@tonic-gate 		sym->st_shndx = SHN_ABS;
3450Sstevel@tonic-gate 
3460Sstevel@tonic-gate 		if (versym && !dynsym)
3470Sstevel@tonic-gate 			versym[1] = 0;
3480Sstevel@tonic-gate 	}
3493492Sab196087 	if (ldynsym) {
3502766Sab196087 		(void) st_setstring(dynstr, ofl->ofl_name, &stoff);
3512766Sab196087 		sym = &ldynsym[ldynsym_ndx];
3522766Sab196087 		/* LINTED */
3532766Sab196087 		sym->st_name = stoff;
3542766Sab196087 		sym->st_value = 0;
3552766Sab196087 		sym->st_size = 0;
3562766Sab196087 		sym->st_info = ELF_ST_INFO(STB_LOCAL, STT_FILE);
3572766Sab196087 		sym->st_other = 0;
3582766Sab196087 		sym->st_shndx = SHN_ABS;
3592766Sab196087 
3602766Sab196087 		/* Scoped symbols get filled in global loop below */
3612766Sab196087 		ldynscopesym_ndx = ldynsym_ndx + 1;
3622766Sab196087 		ldynsym_ndx += ofl->ofl_dynscopecnt;
3632766Sab196087 	}
3640Sstevel@tonic-gate 
3650Sstevel@tonic-gate 	/*
3660Sstevel@tonic-gate 	 * If we are to display GOT summary information, then allocate
3670Sstevel@tonic-gate 	 * the buffer to 'cache' the GOT symbols into now.
3680Sstevel@tonic-gate 	 */
3691618Srie 	if (DBG_ENABLED) {
3702145Srie 		if ((ofl->ofl_gottable = gottable =
3719131SRod.Evans@Sun.COM 		    libld_calloc(ofl->ofl_gotcnt, sizeof (Gottable))) == NULL)
37211827SRod.Evans@Sun.COM 			return ((Addr)S_ERROR);
3730Sstevel@tonic-gate 	}
3740Sstevel@tonic-gate 
3750Sstevel@tonic-gate 	/*
3760Sstevel@tonic-gate 	 * Traverse the program headers.  Determine the last executable segment
3770Sstevel@tonic-gate 	 * and the last data segment so that we can update etext and edata. If
3780Sstevel@tonic-gate 	 * we have empty segments (reservations) record them for setting _end.
3790Sstevel@tonic-gate 	 */
3809131SRod.Evans@Sun.COM 	for (APLIST_TRAVERSE(ofl->ofl_segs, idx1, sgp)) {
3811682Srie 		Phdr	*phd = &(sgp->sg_phdr);
3825892Sab196087 		Os_desc	*osp;
3839131SRod.Evans@Sun.COM 		Aliste	idx2;
3840Sstevel@tonic-gate 
3850Sstevel@tonic-gate 		if (phd->p_type == PT_LOAD) {
3861682Srie 			if (sgp->sg_osdescs != NULL) {
3874716Sab196087 				Word	_flags = phd->p_flags & (PF_W | PF_R);
3884716Sab196087 
3894716Sab196087 				if (_flags == PF_R)
3904716Sab196087 					tsgp = sgp;
3914716Sab196087 				else if (_flags == (PF_W | PF_R))
3924716Sab196087 					dsgp = sgp;
3930Sstevel@tonic-gate 			} else if (sgp->sg_flags & FLG_SG_EMPTY)
3944716Sab196087 				esgp = sgp;
3950Sstevel@tonic-gate 		}
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate 		/*
3980Sstevel@tonic-gate 		 * Generate a section symbol for each output section.
3990Sstevel@tonic-gate 		 */
4009131SRod.Evans@Sun.COM 		for (APLIST_TRAVERSE(sgp->sg_osdescs, idx2, osp)) {
4010Sstevel@tonic-gate 			Word	sectndx;
4020Sstevel@tonic-gate 
4030Sstevel@tonic-gate 			sym = &_sym;
4040Sstevel@tonic-gate 			sym->st_value = osp->os_shdr->sh_addr;
4050Sstevel@tonic-gate 			sym->st_info = ELF_ST_INFO(STB_LOCAL, STT_SECTION);
4060Sstevel@tonic-gate 			/* LINTED */
4070Sstevel@tonic-gate 			sectndx = elf_ndxscn(osp->os_scn);
4080Sstevel@tonic-gate 
4090Sstevel@tonic-gate 			if (symtab) {
4100Sstevel@tonic-gate 				if (sectndx >= SHN_LORESERVE) {
4110Sstevel@tonic-gate 					symshndx[symtab_ndx] = sectndx;
4120Sstevel@tonic-gate 					sym->st_shndx = SHN_XINDEX;
4130Sstevel@tonic-gate 				} else {
4140Sstevel@tonic-gate 					/* LINTED */
4150Sstevel@tonic-gate 					sym->st_shndx = (Half)sectndx;
4160Sstevel@tonic-gate 				}
4170Sstevel@tonic-gate 				symtab[symtab_ndx++] = *sym;
4180Sstevel@tonic-gate 			}
4190Sstevel@tonic-gate 
4200Sstevel@tonic-gate 			if (dynsym && (osp->os_flags & FLG_OS_OUTREL))
4210Sstevel@tonic-gate 				dynsym[dynsym_ndx++] = *sym;
4220Sstevel@tonic-gate 
4239131SRod.Evans@Sun.COM 			if ((dynsym == NULL) ||
4249131SRod.Evans@Sun.COM 			    (osp->os_flags & FLG_OS_OUTREL)) {
4250Sstevel@tonic-gate 				if (versym)
4260Sstevel@tonic-gate 					versym[*symndx - 1] = 0;
4279131SRod.Evans@Sun.COM 				osp->os_identndx = *symndx - 1;
4281618Srie 				DBG_CALL(Dbg_syms_sec_entry(ofl->ofl_lml,
4299131SRod.Evans@Sun.COM 				    osp->os_identndx, sgp, osp));
4300Sstevel@tonic-gate 			}
4310Sstevel@tonic-gate 
4320Sstevel@tonic-gate 			/*
4330Sstevel@tonic-gate 			 * Generate the .shstrtab for this section.
4340Sstevel@tonic-gate 			 */
4350Sstevel@tonic-gate 			(void) st_setstring(shstrtab, osp->os_name, &stoff);
4360Sstevel@tonic-gate 			osp->os_shdr->sh_name = (Word)stoff;
4370Sstevel@tonic-gate 
4380Sstevel@tonic-gate 			/*
4390Sstevel@tonic-gate 			 * Find the section index for our special symbols.
4400Sstevel@tonic-gate 			 */
4410Sstevel@tonic-gate 			if (sgp == tsgp) {
4420Sstevel@tonic-gate 				/* LINTED */
4430Sstevel@tonic-gate 				etext_ndx = elf_ndxscn(osp->os_scn);
4440Sstevel@tonic-gate 			} else if (dsgp == sgp) {
4450Sstevel@tonic-gate 				if (osp->os_shdr->sh_type != SHT_NOBITS) {
4460Sstevel@tonic-gate 					/* LINTED */
4470Sstevel@tonic-gate 					edata_ndx = elf_ndxscn(osp->os_scn);
4480Sstevel@tonic-gate 				}
4490Sstevel@tonic-gate 			}
4500Sstevel@tonic-gate 
4510Sstevel@tonic-gate 			if (start_set == 0) {
4520Sstevel@tonic-gate 				start = sgp->sg_phdr.p_vaddr;
4530Sstevel@tonic-gate 				/* LINTED */
4540Sstevel@tonic-gate 				start_ndx = elf_ndxscn(osp->os_scn);
4550Sstevel@tonic-gate 				start_set++;
4560Sstevel@tonic-gate 			}
4572347Srie 
4582347Srie 			/*
4592347Srie 			 * While we're here, determine whether a .init or .fini
4602347Srie 			 * section exist.
4612347Srie 			 */
4629131SRod.Evans@Sun.COM 			if ((iosp == NULL) && (strcmp(osp->os_name,
4632347Srie 			    MSG_ORIG(MSG_SCN_INIT)) == 0))
4642347Srie 				iosp = osp;
4659131SRod.Evans@Sun.COM 			if ((fosp == NULL) && (strcmp(osp->os_name,
4662347Srie 			    MSG_ORIG(MSG_SCN_FINI)) == 0))
4672347Srie 				fosp = osp;
4680Sstevel@tonic-gate 		}
4690Sstevel@tonic-gate 	}
4700Sstevel@tonic-gate 
4710Sstevel@tonic-gate 	/*
4720Sstevel@tonic-gate 	 * Add local register symbols to the .dynsym.  These are required as
4730Sstevel@tonic-gate 	 * DT_REGISTER .dynamic entries must have a symbol to reference.
4740Sstevel@tonic-gate 	 */
4750Sstevel@tonic-gate 	if (ofl->ofl_regsyms && dynsym) {
4760Sstevel@tonic-gate 		int	ndx;
4770Sstevel@tonic-gate 
4780Sstevel@tonic-gate 		for (ndx = 0; ndx < ofl->ofl_regsymsno; ndx++) {
4799131SRod.Evans@Sun.COM 			Sym_desc	*rsdp;
4809131SRod.Evans@Sun.COM 
4819131SRod.Evans@Sun.COM 			if ((rsdp = ofl->ofl_regsyms[ndx]) == NULL)
4820Sstevel@tonic-gate 				continue;
4830Sstevel@tonic-gate 
48411827SRod.Evans@Sun.COM 			if (!SYM_IS_HIDDEN(rsdp) &&
4850Sstevel@tonic-gate 			    (ELF_ST_BIND(rsdp->sd_sym->st_info) != STB_LOCAL))
4860Sstevel@tonic-gate 				continue;
4870Sstevel@tonic-gate 
4880Sstevel@tonic-gate 			dynsym[dynsym_ndx] = *(rsdp->sd_sym);
4890Sstevel@tonic-gate 			rsdp->sd_symndx = *symndx;
4900Sstevel@tonic-gate 
4910Sstevel@tonic-gate 			if (dynsym[dynsym_ndx].st_name) {
4920Sstevel@tonic-gate 				(void) st_setstring(dynstr, rsdp->sd_name,
4930Sstevel@tonic-gate 				    &stoff);
4940Sstevel@tonic-gate 				dynsym[dynsym_ndx].st_name = stoff;
4950Sstevel@tonic-gate 			}
4960Sstevel@tonic-gate 			dynsym_ndx++;
4970Sstevel@tonic-gate 		}
4980Sstevel@tonic-gate 	}
4990Sstevel@tonic-gate 
5000Sstevel@tonic-gate 	/*
5010Sstevel@tonic-gate 	 * Having traversed all the output segments, warn the user if the
5020Sstevel@tonic-gate 	 * traditional text or data segments don't exist.  Otherwise from these
5030Sstevel@tonic-gate 	 * segments establish the values for `etext', `edata', `end', `END',
5040Sstevel@tonic-gate 	 * and `START'.
5050Sstevel@tonic-gate 	 */
5060Sstevel@tonic-gate 	if (!(flags & FLG_OF_RELOBJ)) {
5079131SRod.Evans@Sun.COM 		Sg_desc	*sgp;
5080Sstevel@tonic-gate 
5090Sstevel@tonic-gate 		if (tsgp)
5100Sstevel@tonic-gate 			etext = tsgp->sg_phdr.p_vaddr + tsgp->sg_phdr.p_filesz;
5110Sstevel@tonic-gate 		else {
5120Sstevel@tonic-gate 			etext = (Addr)0;
5130Sstevel@tonic-gate 			etext_ndx = SHN_ABS;
5140Sstevel@tonic-gate 			etext_abs = 1;
5156299Sab196087 			if (flags & FLG_OF_VERBOSE)
516*13074SAli.Bahrami@Oracle.COM 				ld_eprintf(ofl, ERR_WARNING,
5170Sstevel@tonic-gate 				    MSG_INTL(MSG_UPD_NOREADSEG));
5180Sstevel@tonic-gate 		}
5190Sstevel@tonic-gate 		if (dsgp) {
5200Sstevel@tonic-gate 			edata = dsgp->sg_phdr.p_vaddr + dsgp->sg_phdr.p_filesz;
5210Sstevel@tonic-gate 		} else {
5220Sstevel@tonic-gate 			edata = (Addr)0;
5230Sstevel@tonic-gate 			edata_ndx = SHN_ABS;
5240Sstevel@tonic-gate 			edata_abs = 1;
5256299Sab196087 			if (flags & FLG_OF_VERBOSE)
526*13074SAli.Bahrami@Oracle.COM 				ld_eprintf(ofl, ERR_WARNING,
5270Sstevel@tonic-gate 				    MSG_INTL(MSG_UPD_NORDWRSEG));
5280Sstevel@tonic-gate 		}
5290Sstevel@tonic-gate 
5309131SRod.Evans@Sun.COM 		if (dsgp == NULL) {
5310Sstevel@tonic-gate 			if (tsgp)
5320Sstevel@tonic-gate 				sgp = tsgp;
5330Sstevel@tonic-gate 			else
5340Sstevel@tonic-gate 				sgp = 0;
5359131SRod.Evans@Sun.COM 		} else if (tsgp == NULL)
5360Sstevel@tonic-gate 			sgp = dsgp;
5370Sstevel@tonic-gate 		else if (dsgp->sg_phdr.p_vaddr > tsgp->sg_phdr.p_vaddr)
5380Sstevel@tonic-gate 			sgp = dsgp;
5390Sstevel@tonic-gate 		else if (dsgp->sg_phdr.p_vaddr < tsgp->sg_phdr.p_vaddr)
5400Sstevel@tonic-gate 			sgp = tsgp;
5410Sstevel@tonic-gate 		else {
5420Sstevel@tonic-gate 			/*
5430Sstevel@tonic-gate 			 * One of the segments must be of zero size.
5440Sstevel@tonic-gate 			 */
5450Sstevel@tonic-gate 			if (tsgp->sg_phdr.p_memsz)
5460Sstevel@tonic-gate 				sgp = tsgp;
5470Sstevel@tonic-gate 			else
5480Sstevel@tonic-gate 				sgp = dsgp;
5490Sstevel@tonic-gate 		}
5500Sstevel@tonic-gate 
5510Sstevel@tonic-gate 		if (esgp && (esgp->sg_phdr.p_vaddr > sgp->sg_phdr.p_vaddr))
5520Sstevel@tonic-gate 			sgp = esgp;
5530Sstevel@tonic-gate 
5540Sstevel@tonic-gate 		if (sgp) {
5550Sstevel@tonic-gate 			end = sgp->sg_phdr.p_vaddr + sgp->sg_phdr.p_memsz;
5560Sstevel@tonic-gate 
5570Sstevel@tonic-gate 			/*
5581722Sseizo 			 * If the last loadable segment is a read-only segment,
5591722Sseizo 			 * then the application which uses the symbol _end to
5601722Sseizo 			 * find the beginning of writable heap area may cause
5611722Sseizo 			 * segmentation violation. We adjust the value of the
5621722Sseizo 			 * _end to skip to the next page boundary.
5631722Sseizo 			 *
5641722Sseizo 			 * 6401812 System interface which returs beginning
5651722Sseizo 			 *	   heap would be nice.
5661722Sseizo 			 * When the above RFE is implemented, the changes below
5671722Sseizo 			 * could be changed in a better way.
5681722Sseizo 			 */
5691722Sseizo 			if ((sgp->sg_phdr.p_flags & PF_W) == 0)
5705220Srie 				end = (Addr)S_ROUND(end, sysconf(_SC_PAGESIZE));
5711722Sseizo 
5721722Sseizo 			/*
5730Sstevel@tonic-gate 			 * If we're dealing with a memory reservation there are
5740Sstevel@tonic-gate 			 * no sections to establish an index for _end, so assign
5750Sstevel@tonic-gate 			 * it as an absolute.
5760Sstevel@tonic-gate 			 */
5771682Srie 			if (sgp->sg_osdescs != NULL) {
5781682Srie 				/*
5791682Srie 				 * Determine the last section for this segment.
5801682Srie 				 */
5815892Sab196087 				Os_desc	*osp = sgp->sg_osdescs->apl_data
5825892Sab196087 				    [sgp->sg_osdescs->apl_nitems - 1];
5835892Sab196087 
5840Sstevel@tonic-gate 				/* LINTED */
5855892Sab196087 				end_ndx = elf_ndxscn(osp->os_scn);
5860Sstevel@tonic-gate 			} else {
5870Sstevel@tonic-gate 				end_ndx = SHN_ABS;
5880Sstevel@tonic-gate 				end_abs = 1;
5890Sstevel@tonic-gate 			}
5900Sstevel@tonic-gate 		} else {
5910Sstevel@tonic-gate 			end = (Addr) 0;
5920Sstevel@tonic-gate 			end_ndx = SHN_ABS;
5930Sstevel@tonic-gate 			end_abs = 1;
594*13074SAli.Bahrami@Oracle.COM 			ld_eprintf(ofl, ERR_WARNING, MSG_INTL(MSG_UPD_NOSEG));
5950Sstevel@tonic-gate 		}
5960Sstevel@tonic-gate 	}
5970Sstevel@tonic-gate 
5980Sstevel@tonic-gate 	/*
5990Sstevel@tonic-gate 	 * Initialize the scoped symbol table entry point.  This is for all
6000Sstevel@tonic-gate 	 * the global symbols that have been scoped to locals and will be
6010Sstevel@tonic-gate 	 * filled in during global symbol processing so that we don't have
6020Sstevel@tonic-gate 	 * to traverse the globals symbol hash array more than once.
6030Sstevel@tonic-gate 	 */
6040Sstevel@tonic-gate 	if (symtab) {
6058140SAli.Bahrami@Sun.COM 		scopesym_bndx = symtab_ndx;
6068140SAli.Bahrami@Sun.COM 		scopesym_ndx = scopesym_bndx;
6070Sstevel@tonic-gate 		symtab_ndx += ofl->ofl_scopecnt;
6080Sstevel@tonic-gate 	}
6090Sstevel@tonic-gate 
6100Sstevel@tonic-gate 	/*
6118159SAli.Bahrami@Sun.COM 	 * If expanding partially expanded symbols under '-z nopartial',
6128159SAli.Bahrami@Sun.COM 	 * prepare to do that.
6130Sstevel@tonic-gate 	 */
6148159SAli.Bahrami@Sun.COM 	if (ofl->ofl_isparexpn) {
6158159SAli.Bahrami@Sun.COM 		osp = ofl->ofl_isparexpn->is_osdesc;
6168369SAli.Bahrami@Sun.COM 		parexpnbase = parexpnaddr = (Addr)(osp->os_shdr->sh_addr +
6178159SAli.Bahrami@Sun.COM 		    ofl->ofl_isparexpn->is_indata->d_off);
6180Sstevel@tonic-gate 		/* LINTED */
6198159SAli.Bahrami@Sun.COM 		parexpnndx = elf_ndxscn(osp->os_scn);
6209131SRod.Evans@Sun.COM 		ofl->ofl_parexpnndx = osp->os_identndx;
6210Sstevel@tonic-gate 	}
6220Sstevel@tonic-gate 
6230Sstevel@tonic-gate 	/*
6240Sstevel@tonic-gate 	 * If we are generating a .symtab collect all the local symbols,
6250Sstevel@tonic-gate 	 * assigning a new virtual address or displacement (value).
6260Sstevel@tonic-gate 	 */
6279131SRod.Evans@Sun.COM 	for (APLIST_TRAVERSE(ofl->ofl_objs, idx1, ifl)) {
62811827SRod.Evans@Sun.COM 		Xword		lndx, local = ifl->ifl_locscnt;
62911827SRod.Evans@Sun.COM 		Cap_desc	*cdp = ifl->ifl_caps;
6300Sstevel@tonic-gate 
6310Sstevel@tonic-gate 		for (lndx = 1; lndx < local; lndx++) {
6320Sstevel@tonic-gate 			Gotndx		*gnp;
6333492Sab196087 			uchar_t		type;
6340Sstevel@tonic-gate 			Word		*_symshndx;
6352766Sab196087 			int		enter_in_symtab, enter_in_ldynsym;
6362766Sab196087 			int		update_done;
6370Sstevel@tonic-gate 
6380Sstevel@tonic-gate 			sdp = ifl->ifl_oldndx[lndx];
6390Sstevel@tonic-gate 			sym = sdp->sd_sym;
6400Sstevel@tonic-gate 
6410Sstevel@tonic-gate 			/*
6420Sstevel@tonic-gate 			 * Assign a got offset if necessary.
6430Sstevel@tonic-gate 			 */
6446206Sab196087 			if ((ld_targ.t_mr.mr_assign_got != NULL) &&
6456206Sab196087 			    (*ld_targ.t_mr.mr_assign_got)(ofl, sdp) == S_ERROR)
6460Sstevel@tonic-gate 				return ((Addr)S_ERROR);
6476206Sab196087 
6481618Srie 			if (DBG_ENABLED) {
6499131SRod.Evans@Sun.COM 				Aliste	idx2;
6509131SRod.Evans@Sun.COM 
6519131SRod.Evans@Sun.COM 				for (ALIST_TRAVERSE(sdp->sd_GOTndxs,
6529131SRod.Evans@Sun.COM 				    idx2, gnp)) {
6534716Sab196087 					gottable->gt_sym = sdp;
6544716Sab196087 					gottable->gt_gndx.gn_gotndx =
6554716Sab196087 					    gnp->gn_gotndx;
6564716Sab196087 					gottable->gt_gndx.gn_addend =
6574716Sab196087 					    gnp->gn_addend;
6584716Sab196087 					gottable++;
6594716Sab196087 				}
6600Sstevel@tonic-gate 			}
6610Sstevel@tonic-gate 
6620Sstevel@tonic-gate 			if ((type = ELF_ST_TYPE(sym->st_info)) == STT_SECTION)
6630Sstevel@tonic-gate 				continue;
6640Sstevel@tonic-gate 
6650Sstevel@tonic-gate 			/*
6660Sstevel@tonic-gate 			 * Ignore any symbols that have been marked as invalid
6670Sstevel@tonic-gate 			 * during input processing.  Providing these aren't used
6680Sstevel@tonic-gate 			 * for relocation they'll just be dropped from the
6690Sstevel@tonic-gate 			 * output image.
6700Sstevel@tonic-gate 			 */
6710Sstevel@tonic-gate 			if (sdp->sd_flags & FLG_SY_INVALID)
6720Sstevel@tonic-gate 				continue;
6730Sstevel@tonic-gate 
6740Sstevel@tonic-gate 			/*
6750Sstevel@tonic-gate 			 * If the section that this symbol was associated
6760Sstevel@tonic-gate 			 * with has been discarded - then we discard
6770Sstevel@tonic-gate 			 * the local symbol along with it.
6780Sstevel@tonic-gate 			 */
6790Sstevel@tonic-gate 			if (sdp->sd_flags & FLG_SY_ISDISC)
6800Sstevel@tonic-gate 				continue;
6810Sstevel@tonic-gate 
6820Sstevel@tonic-gate 			/*
6838140SAli.Bahrami@Sun.COM 			 * If this symbol is from a different file
6848140SAli.Bahrami@Sun.COM 			 * than the input descriptor we are processing,
6858140SAli.Bahrami@Sun.COM 			 * treat it as if it has FLG_SY_ISDISC set.
6868140SAli.Bahrami@Sun.COM 			 * This happens when sloppy_comdat_reloc()
6878140SAli.Bahrami@Sun.COM 			 * replaces a symbol to a discarded comdat section
6888140SAli.Bahrami@Sun.COM 			 * with an equivalent symbol from a different
6898140SAli.Bahrami@Sun.COM 			 * file. We only want to enter such a symbol
6908140SAli.Bahrami@Sun.COM 			 * once --- as part of the file that actually
6918140SAli.Bahrami@Sun.COM 			 * supplies it.
6928140SAli.Bahrami@Sun.COM 			 */
6938140SAli.Bahrami@Sun.COM 			if (ifl != sdp->sd_file)
6948140SAli.Bahrami@Sun.COM 				continue;
6958140SAli.Bahrami@Sun.COM 
6968140SAli.Bahrami@Sun.COM 			/*
6970Sstevel@tonic-gate 			 * Generate an output symbol to represent this input
6980Sstevel@tonic-gate 			 * symbol.  Even if the symbol table is to be stripped
6990Sstevel@tonic-gate 			 * we still need to update any local symbols that are
7000Sstevel@tonic-gate 			 * used during relocation.
7010Sstevel@tonic-gate 			 */
7022766Sab196087 			enter_in_symtab = symtab &&
7036614Srie 			    (!(ofl->ofl_flags & FLG_OF_REDLSYM) ||
7049131SRod.Evans@Sun.COM 			    sdp->sd_move);
7052766Sab196087 			enter_in_ldynsym = ldynsym && sdp->sd_name &&
7063492Sab196087 			    ldynsym_symtype[type] &&
7076614Srie 			    !(ofl->ofl_flags & FLG_OF_REDLSYM);
7089131SRod.Evans@Sun.COM 			_symshndx = NULL;
7099131SRod.Evans@Sun.COM 
7102766Sab196087 			if (enter_in_symtab) {
7110Sstevel@tonic-gate 				if (!dynsym)
7120Sstevel@tonic-gate 					sdp->sd_symndx = *symndx;
7130Sstevel@tonic-gate 				symtab[symtab_ndx] = *sym;
71411827SRod.Evans@Sun.COM 
7150Sstevel@tonic-gate 				/*
7160Sstevel@tonic-gate 				 * Provided this isn't an unnamed register
7170Sstevel@tonic-gate 				 * symbol, update its name.
7180Sstevel@tonic-gate 				 */
7190Sstevel@tonic-gate 				if (((sdp->sd_flags & FLG_SY_REGSYM) == 0) ||
7200Sstevel@tonic-gate 				    symtab[symtab_ndx].st_name) {
7210Sstevel@tonic-gate 					(void) st_setstring(strtab,
7220Sstevel@tonic-gate 					    sdp->sd_name, &stoff);
7230Sstevel@tonic-gate 					symtab[symtab_ndx].st_name = stoff;
7240Sstevel@tonic-gate 				}
7250Sstevel@tonic-gate 				sdp->sd_flags &= ~FLG_SY_CLEAN;
7260Sstevel@tonic-gate 				if (symshndx)
7270Sstevel@tonic-gate 					_symshndx = &symshndx[symtab_ndx];
7280Sstevel@tonic-gate 				sdp->sd_sym = sym = &symtab[symtab_ndx++];
7290Sstevel@tonic-gate 
7300Sstevel@tonic-gate 				if ((sdp->sd_flags & FLG_SY_SPECSEC) &&
7312766Sab196087 				    (sym->st_shndx == SHN_ABS) &&
7322766Sab196087 				    !enter_in_ldynsym)
7330Sstevel@tonic-gate 					continue;
7342766Sab196087 			} else if (enter_in_ldynsym) {
7352766Sab196087 				/*
7362766Sab196087 				 * Not using symtab, but we do have ldynsym
7372766Sab196087 				 * available.
7382766Sab196087 				 */
7392766Sab196087 				ldynsym[ldynsym_ndx] = *sym;
7402766Sab196087 				(void) st_setstring(dynstr, sdp->sd_name,
7414716Sab196087 				    &stoff);
7422766Sab196087 				ldynsym[ldynsym_ndx].st_name = stoff;
7432766Sab196087 
7442766Sab196087 				sdp->sd_flags &= ~FLG_SY_CLEAN;
7452766Sab196087 				if (ldynshndx)
7462766Sab196087 					_symshndx = &ldynshndx[ldynsym_ndx];
7473492Sab196087 				sdp->sd_sym = sym = &ldynsym[ldynsym_ndx];
7483492Sab196087 				/* Add it to sort section if it qualifies */
7493492Sab196087 				ADD_TO_DYNSORT(sdp, sym, type, ldynsym_ndx);
7503492Sab196087 				ldynsym_ndx++;
7512766Sab196087 			} else {	/* Not using symtab or ldynsym */
7520Sstevel@tonic-gate 				/*
7530Sstevel@tonic-gate 				 * If this symbol requires modifying to provide
7540Sstevel@tonic-gate 				 * for a relocation or move table update, make
7550Sstevel@tonic-gate 				 * a copy of it.
7560Sstevel@tonic-gate 				 */
7570Sstevel@tonic-gate 				if (!(sdp->sd_flags & FLG_SY_UPREQD) &&
7589131SRod.Evans@Sun.COM 				    !(sdp->sd_move))
7590Sstevel@tonic-gate 					continue;
7600Sstevel@tonic-gate 				if ((sdp->sd_flags & FLG_SY_SPECSEC) &&
7610Sstevel@tonic-gate 				    (sym->st_shndx == SHN_ABS))
7620Sstevel@tonic-gate 					continue;
7630Sstevel@tonic-gate 
7641618Srie 				if (ld_sym_copy(sdp) == S_ERROR)
7650Sstevel@tonic-gate 					return ((Addr)S_ERROR);
7660Sstevel@tonic-gate 				sym = sdp->sd_sym;
7670Sstevel@tonic-gate 			}
7680Sstevel@tonic-gate 
7690Sstevel@tonic-gate 			/*
7700Sstevel@tonic-gate 			 * Update the symbols contents if necessary.
7710Sstevel@tonic-gate 			 */
7722766Sab196087 			update_done = 0;
7730Sstevel@tonic-gate 			if (type == STT_FILE) {
7740Sstevel@tonic-gate 				sdp->sd_shndx = sym->st_shndx = SHN_ABS;
7750Sstevel@tonic-gate 				sdp->sd_flags |= FLG_SY_SPECSEC;
7762766Sab196087 				update_done = 1;
7770Sstevel@tonic-gate 			}
7780Sstevel@tonic-gate 
7790Sstevel@tonic-gate 			/*
7800Sstevel@tonic-gate 			 * If we are expanding the locally bound partially
7810Sstevel@tonic-gate 			 * initialized symbols, then update the address here.
7820Sstevel@tonic-gate 			 */
7838159SAli.Bahrami@Sun.COM 			if (ofl->ofl_isparexpn &&
7842766Sab196087 			    (sdp->sd_flags & FLG_SY_PAREXPN) && !update_done) {
7858159SAli.Bahrami@Sun.COM 				sym->st_shndx = parexpnndx;
7868159SAli.Bahrami@Sun.COM 				sdp->sd_isc = ofl->ofl_isparexpn;
7878369SAli.Bahrami@Sun.COM 				sym->st_value = parexpnaddr;
7888159SAli.Bahrami@Sun.COM 				parexpnaddr += sym->st_size;
7898369SAli.Bahrami@Sun.COM 				if ((flags & FLG_OF_RELOBJ) == 0)
7908369SAli.Bahrami@Sun.COM 					sym->st_value -= parexpnbase;
7910Sstevel@tonic-gate 			}
7920Sstevel@tonic-gate 
7930Sstevel@tonic-gate 			/*
7940Sstevel@tonic-gate 			 * If this isn't an UNDEF symbol (ie. an input section
7950Sstevel@tonic-gate 			 * is associated), update the symbols value and index.
7960Sstevel@tonic-gate 			 */
79711827SRod.Evans@Sun.COM 			if (((isc = sdp->sd_isc) != NULL) && !update_done) {
7980Sstevel@tonic-gate 				Word	sectndx;
7990Sstevel@tonic-gate 
8000Sstevel@tonic-gate 				osp = isc->is_osdesc;
8010Sstevel@tonic-gate 				/* LINTED */
8020Sstevel@tonic-gate 				sym->st_value +=
8030Sstevel@tonic-gate 				    (Off)_elf_getxoff(isc->is_indata);
80410792SRod.Evans@Sun.COM 				if ((flags & FLG_OF_RELOBJ) == 0) {
8050Sstevel@tonic-gate 					sym->st_value += osp->os_shdr->sh_addr;
8060Sstevel@tonic-gate 					/*
8070Sstevel@tonic-gate 					 * TLS symbols are relative to
8080Sstevel@tonic-gate 					 * the TLS segment.
8090Sstevel@tonic-gate 					 */
8103492Sab196087 					if ((type == STT_TLS) &&
8113492Sab196087 					    (ofl->ofl_tlsphdr)) {
8120Sstevel@tonic-gate 						sym->st_value -=
8130Sstevel@tonic-gate 						    ofl->ofl_tlsphdr->p_vaddr;
8142766Sab196087 					}
8150Sstevel@tonic-gate 				}
8160Sstevel@tonic-gate 				/* LINTED */
8170Sstevel@tonic-gate 				if ((sdp->sd_shndx = sectndx =
8180Sstevel@tonic-gate 				    elf_ndxscn(osp->os_scn)) >= SHN_LORESERVE) {
8190Sstevel@tonic-gate 					if (_symshndx) {
8200Sstevel@tonic-gate 						*_symshndx = sectndx;
8210Sstevel@tonic-gate 					}
8220Sstevel@tonic-gate 					sym->st_shndx = SHN_XINDEX;
8230Sstevel@tonic-gate 				} else {
8240Sstevel@tonic-gate 					/* LINTED */
8250Sstevel@tonic-gate 					sym->st_shndx = sectndx;
8260Sstevel@tonic-gate 				}
8270Sstevel@tonic-gate 			}
8282766Sab196087 
8292766Sab196087 			/*
8302766Sab196087 			 * If entering the symbol in both the symtab and the
8312766Sab196087 			 * ldynsym, then the one in symtab needs to be
8322766Sab196087 			 * copied to ldynsym. If it is only in the ldynsym,
8332766Sab196087 			 * then the code above already set it up and we have
8342766Sab196087 			 * nothing more to do here.
8352766Sab196087 			 */
8362766Sab196087 			if (enter_in_symtab && enter_in_ldynsym) {
8372766Sab196087 				ldynsym[ldynsym_ndx] = *sym;
8382766Sab196087 				(void) st_setstring(dynstr, sdp->sd_name,
8394716Sab196087 				    &stoff);
8402766Sab196087 				ldynsym[ldynsym_ndx].st_name = stoff;
8412766Sab196087 
8422766Sab196087 				if (_symshndx && ldynshndx)
8432766Sab196087 					ldynshndx[ldynsym_ndx] = *_symshndx;
8442766Sab196087 
8453492Sab196087 				/* Add it to sort section if it qualifies */
8463492Sab196087 				ADD_TO_DYNSORT(sdp, sym, type, ldynsym_ndx);
8473492Sab196087 
8482766Sab196087 				ldynsym_ndx++;
8492766Sab196087 			}
8500Sstevel@tonic-gate 		}
85111827SRod.Evans@Sun.COM 
85211827SRod.Evans@Sun.COM 		/*
85311827SRod.Evans@Sun.COM 		 * If this input file has undergone object to symbol
85411827SRod.Evans@Sun.COM 		 * capabilities conversion, supply any new capabilities symbols.
85511827SRod.Evans@Sun.COM 		 * These symbols are copies of the original global symbols, and
85611827SRod.Evans@Sun.COM 		 * follow the existing local symbols that are supplied from this
85711827SRod.Evans@Sun.COM 		 * input file (which are identified with a preceding STT_FILE).
85811827SRod.Evans@Sun.COM 		 */
85911827SRod.Evans@Sun.COM 		if (symtab && cdp && cdp->ca_syms) {
86011827SRod.Evans@Sun.COM 			Aliste		idx2;
86111827SRod.Evans@Sun.COM 			Cap_sym		*csp;
86211827SRod.Evans@Sun.COM 
86311827SRod.Evans@Sun.COM 			for (APLIST_TRAVERSE(cdp->ca_syms, idx2, csp)) {
86411827SRod.Evans@Sun.COM 				Is_desc	*isp;
86511827SRod.Evans@Sun.COM 
86611827SRod.Evans@Sun.COM 				sdp = csp->cs_sdp;
86711827SRod.Evans@Sun.COM 				sym = sdp->sd_sym;
86811827SRod.Evans@Sun.COM 
86911827SRod.Evans@Sun.COM 				if ((isp = sdp->sd_isc) != NULL) {
87011827SRod.Evans@Sun.COM 					Os_desc	*osp = isp->is_osdesc;
87111827SRod.Evans@Sun.COM 
87211827SRod.Evans@Sun.COM 					/*
87311827SRod.Evans@Sun.COM 					 * Update the symbols value.
87411827SRod.Evans@Sun.COM 					 */
87511827SRod.Evans@Sun.COM 					/* LINTED */
87611827SRod.Evans@Sun.COM 					sym->st_value +=
87711827SRod.Evans@Sun.COM 					    (Off)_elf_getxoff(isp->is_indata);
87811827SRod.Evans@Sun.COM 					if ((flags & FLG_OF_RELOBJ) == 0)
87911827SRod.Evans@Sun.COM 						sym->st_value +=
88011827SRod.Evans@Sun.COM 						    osp->os_shdr->sh_addr;
88111827SRod.Evans@Sun.COM 
88211827SRod.Evans@Sun.COM 					/*
88311827SRod.Evans@Sun.COM 					 * Update the symbols section index.
88411827SRod.Evans@Sun.COM 					 */
88511827SRod.Evans@Sun.COM 					sdp->sd_shndx = sym->st_shndx =
88611827SRod.Evans@Sun.COM 					    elf_ndxscn(osp->os_scn);
88711827SRod.Evans@Sun.COM 				}
88811827SRod.Evans@Sun.COM 
88911827SRod.Evans@Sun.COM 				symtab[symtab_ndx] = *sym;
89011827SRod.Evans@Sun.COM 				(void) st_setstring(strtab, sdp->sd_name,
89111827SRod.Evans@Sun.COM 				    &stoff);
89211827SRod.Evans@Sun.COM 				symtab[symtab_ndx].st_name = stoff;
89311827SRod.Evans@Sun.COM 				sdp->sd_symndx = symtab_ndx++;
89411827SRod.Evans@Sun.COM 			}
89511827SRod.Evans@Sun.COM 		}
8960Sstevel@tonic-gate 	}
89711827SRod.Evans@Sun.COM 
8988140SAli.Bahrami@Sun.COM 	symtab_gbl_bndx = symtab_ndx;	/* .symtab index of 1st global entry */
8990Sstevel@tonic-gate 
9000Sstevel@tonic-gate 	/*
9010Sstevel@tonic-gate 	 * Two special symbols are `_init' and `_fini'.  If these are supplied
9020Sstevel@tonic-gate 	 * by crti.o then they are used to represent the total concatenation of
9032347Srie 	 * the `.init' and `.fini' sections.
9042347Srie 	 *
90510792SRod.Evans@Sun.COM 	 * Determine whether any .init or .fini sections exist.  If these
90610792SRod.Evans@Sun.COM 	 * sections exist and a dynamic object is being built, but no `_init'
90710792SRod.Evans@Sun.COM 	 * or `_fini' symbols are found, then the user is probably building
90810792SRod.Evans@Sun.COM 	 * this object directly from ld(1) rather than using a compiler driver
90910792SRod.Evans@Sun.COM 	 * that provides the symbols via crt's.
9102347Srie 	 *
9112347Srie 	 * If the .init or .fini section exist, and their associated symbols,
9122347Srie 	 * determine the size of the sections and updated the symbols value
9132347Srie 	 * accordingly.
9140Sstevel@tonic-gate 	 */
9151618Srie 	if (((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_INIT_U), SYM_NOHASH, 0,
9160Sstevel@tonic-gate 	    ofl)) != NULL) && (sdp->sd_ref == REF_REL_NEED) && sdp->sd_isc &&
9172347Srie 	    (sdp->sd_isc->is_osdesc == iosp)) {
9181618Srie 		if (ld_sym_copy(sdp) == S_ERROR)
9190Sstevel@tonic-gate 			return ((Addr)S_ERROR);
9205220Srie 		sdp->sd_sym->st_size = sdp->sd_isc->is_osdesc->os_shdr->sh_size;
9215220Srie 
9222347Srie 	} else if (iosp && !(flags & FLG_OF_RELOBJ)) {
923*13074SAli.Bahrami@Oracle.COM 		ld_eprintf(ofl, ERR_WARNING, MSG_INTL(MSG_SYM_NOCRT),
9242347Srie 		    MSG_ORIG(MSG_SYM_INIT_U), MSG_ORIG(MSG_SCN_INIT));
9250Sstevel@tonic-gate 	}
9262347Srie 
9271618Srie 	if (((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_FINI_U), SYM_NOHASH, 0,
9280Sstevel@tonic-gate 	    ofl)) != NULL) && (sdp->sd_ref == REF_REL_NEED) && sdp->sd_isc &&
9292347Srie 	    (sdp->sd_isc->is_osdesc == fosp)) {
9301618Srie 		if (ld_sym_copy(sdp) == S_ERROR)
9310Sstevel@tonic-gate 			return ((Addr)S_ERROR);
9325220Srie 		sdp->sd_sym->st_size = sdp->sd_isc->is_osdesc->os_shdr->sh_size;
9335220Srie 
9342347Srie 	} else if (fosp && !(flags & FLG_OF_RELOBJ)) {
935*13074SAli.Bahrami@Oracle.COM 		ld_eprintf(ofl, ERR_WARNING, MSG_INTL(MSG_SYM_NOCRT),
9362347Srie 		    MSG_ORIG(MSG_SYM_FINI_U), MSG_ORIG(MSG_SCN_FINI));
9370Sstevel@tonic-gate 	}
9380Sstevel@tonic-gate 
9390Sstevel@tonic-gate 	/*
9400Sstevel@tonic-gate 	 * Assign .bss information for use with updating COMMON symbols.
9410Sstevel@tonic-gate 	 */
9420Sstevel@tonic-gate 	if (ofl->ofl_isbss) {
9439131SRod.Evans@Sun.COM 		isc = ofl->ofl_isbss;
9449131SRod.Evans@Sun.COM 		osp = isc->is_osdesc;
9450Sstevel@tonic-gate 
9460Sstevel@tonic-gate 		bssaddr = osp->os_shdr->sh_addr +
9479131SRod.Evans@Sun.COM 		    (Off)_elf_getxoff(isc->is_indata);
9480Sstevel@tonic-gate 		/* LINTED */
9490Sstevel@tonic-gate 		bssndx = elf_ndxscn(osp->os_scn);
9500Sstevel@tonic-gate 	}
9510Sstevel@tonic-gate 
9526206Sab196087 #if	defined(_ELF64)
953574Sseizo 	/*
9546206Sab196087 	 * For amd64 target, assign .lbss information for use
9556206Sab196087 	 * with updating LCOMMON symbols.
956574Sseizo 	 */
9576206Sab196087 	if ((ld_targ.t_m.m_mach == EM_AMD64) && ofl->ofl_islbss) {
958574Sseizo 		osp = ofl->ofl_islbss->is_osdesc;
959574Sseizo 
960574Sseizo 		lbssaddr = osp->os_shdr->sh_addr +
9614716Sab196087 		    (Off)_elf_getxoff(ofl->ofl_islbss->is_indata);
962574Sseizo 		/* LINTED */
963574Sseizo 		lbssndx = elf_ndxscn(osp->os_scn);
964574Sseizo 	}
965574Sseizo #endif
9660Sstevel@tonic-gate 	/*
9670Sstevel@tonic-gate 	 * Assign .tlsbss information for use with updating COMMON symbols.
9680Sstevel@tonic-gate 	 */
9690Sstevel@tonic-gate 	if (ofl->ofl_istlsbss) {
9700Sstevel@tonic-gate 		osp = ofl->ofl_istlsbss->is_osdesc;
9710Sstevel@tonic-gate 		tlsbssaddr = osp->os_shdr->sh_addr +
9724716Sab196087 		    (Off)_elf_getxoff(ofl->ofl_istlsbss->is_indata);
9730Sstevel@tonic-gate 		/* LINTED */
9740Sstevel@tonic-gate 		tlsbssndx = elf_ndxscn(osp->os_scn);
9750Sstevel@tonic-gate 	}
9760Sstevel@tonic-gate 
9770Sstevel@tonic-gate 	if ((sorted_syms = libld_calloc(ofl->ofl_globcnt +
9789131SRod.Evans@Sun.COM 	    ofl->ofl_elimcnt + ofl->ofl_scopecnt,
9799131SRod.Evans@Sun.COM 	    sizeof (*sorted_syms))) == NULL)
9800Sstevel@tonic-gate 		return ((Addr)S_ERROR);
9810Sstevel@tonic-gate 
9820Sstevel@tonic-gate 	scndx = 0;
9830Sstevel@tonic-gate 	ssndx = ofl->ofl_scopecnt + ofl->ofl_elimcnt;
9840Sstevel@tonic-gate 
98510792SRod.Evans@Sun.COM 	DBG_CALL(Dbg_syms_up_title(ofl->ofl_lml));
98610792SRod.Evans@Sun.COM 
9870Sstevel@tonic-gate 	/*
98810167SRod.Evans@Sun.COM 	 * Traverse the internal symbol table updating global symbol information
98910167SRod.Evans@Sun.COM 	 * and allocating common.
9900Sstevel@tonic-gate 	 */
9910Sstevel@tonic-gate 	for (sav = avl_first(&ofl->ofl_symavl); sav;
9920Sstevel@tonic-gate 	    sav = AVL_NEXT(&ofl->ofl_symavl, sav)) {
9939131SRod.Evans@Sun.COM 		Sym	*symptr;
9940Sstevel@tonic-gate 		int	local;
995574Sseizo 		int	restore;
9960Sstevel@tonic-gate 
99710792SRod.Evans@Sun.COM 		sdp = sav->sav_sdp;
9980Sstevel@tonic-gate 
9990Sstevel@tonic-gate 		/*
10005220Srie 		 * Ignore any symbols that have been marked as invalid during
10015220Srie 		 * input processing.  Providing these aren't used for
10025220Srie 		 * relocation, they will be dropped from the output image.
10030Sstevel@tonic-gate 		 */
10040Sstevel@tonic-gate 		if (sdp->sd_flags & FLG_SY_INVALID) {
10051618Srie 			DBG_CALL(Dbg_syms_old(ofl, sdp));
10061618Srie 			DBG_CALL(Dbg_syms_ignore(ofl, sdp));
10070Sstevel@tonic-gate 			continue;
10080Sstevel@tonic-gate 		}
10090Sstevel@tonic-gate 
10100Sstevel@tonic-gate 		/*
10115220Srie 		 * Only needed symbols are copied to the output symbol table.
10120Sstevel@tonic-gate 		 */
10130Sstevel@tonic-gate 		if (sdp->sd_ref == REF_DYN_SEEN)
10140Sstevel@tonic-gate 			continue;
10150Sstevel@tonic-gate 
101611827SRod.Evans@Sun.COM 		if (SYM_IS_HIDDEN(sdp) && (flags & FLG_OF_PROCRED))
10170Sstevel@tonic-gate 			local = 1;
10180Sstevel@tonic-gate 		else
10190Sstevel@tonic-gate 			local = 0;
10200Sstevel@tonic-gate 
10210Sstevel@tonic-gate 		if (local || (ofl->ofl_hashbkts == 0)) {
10220Sstevel@tonic-gate 			sorted_syms[scndx++].sl_sdp = sdp;
10230Sstevel@tonic-gate 		} else {
10240Sstevel@tonic-gate 			sorted_syms[ssndx].sl_hval = sdp->sd_aux->sa_hash %
10250Sstevel@tonic-gate 			    ofl->ofl_hashbkts;
10260Sstevel@tonic-gate 			sorted_syms[ssndx].sl_sdp = sdp;
10270Sstevel@tonic-gate 			ssndx++;
10280Sstevel@tonic-gate 		}
10290Sstevel@tonic-gate 
10300Sstevel@tonic-gate 		/*
10312347Srie 		 * Note - expand the COMMON symbols here because an address
10322347Srie 		 * must be assigned to them in the same order that space was
10332347Srie 		 * calculated in sym_validate().  If this ordering isn't
10342347Srie 		 * followed differing alignment requirements can throw us all
10352347Srie 		 * out of whack.
10360Sstevel@tonic-gate 		 *
10372347Srie 		 * The expanded .bss global symbol is handled here as well.
10380Sstevel@tonic-gate 		 *
10392347Srie 		 * The actual adding entries into the symbol table still occurs
10402347Srie 		 * below in hashbucket order.
10410Sstevel@tonic-gate 		 */
10420Sstevel@tonic-gate 		symptr = sdp->sd_sym;
1043574Sseizo 		restore = 0;
10440Sstevel@tonic-gate 		if ((sdp->sd_flags & FLG_SY_PAREXPN) ||
10450Sstevel@tonic-gate 		    ((sdp->sd_flags & FLG_SY_SPECSEC) &&
10460Sstevel@tonic-gate 		    (sdp->sd_shndx = symptr->st_shndx) == SHN_COMMON)) {
10470Sstevel@tonic-gate 
10480Sstevel@tonic-gate 			/*
10498159SAli.Bahrami@Sun.COM 			 * An expanded symbol goes to a special .data section
10508159SAli.Bahrami@Sun.COM 			 * prepared for that purpose (ofl->ofl_isparexpn).
10512347Srie 			 * Assign COMMON allocations to .bss.
10520Sstevel@tonic-gate 			 * Otherwise leave it as is.
10530Sstevel@tonic-gate 			 */
10540Sstevel@tonic-gate 			if (sdp->sd_flags & FLG_SY_PAREXPN) {
10550Sstevel@tonic-gate 				restore = 1;
10568159SAli.Bahrami@Sun.COM 				sdp->sd_shndx = parexpnndx;
10570Sstevel@tonic-gate 				sdp->sd_flags &= ~FLG_SY_SPECSEC;
10580Sstevel@tonic-gate 				symptr->st_value = (Xword) S_ROUND(
10598159SAli.Bahrami@Sun.COM 				    parexpnaddr, symptr->st_value);
10608159SAli.Bahrami@Sun.COM 				parexpnaddr = symptr->st_value +
10614716Sab196087 				    symptr->st_size;
10628159SAli.Bahrami@Sun.COM 				sdp->sd_isc = ofl->ofl_isparexpn;
10630Sstevel@tonic-gate 				sdp->sd_flags |= FLG_SY_COMMEXP;
10642347Srie 
10650Sstevel@tonic-gate 			} else if (ELF_ST_TYPE(symptr->st_info) != STT_TLS &&
10660Sstevel@tonic-gate 			    (local || !(flags & FLG_OF_RELOBJ))) {
10670Sstevel@tonic-gate 				restore = 1;
10680Sstevel@tonic-gate 				sdp->sd_shndx = bssndx;
10690Sstevel@tonic-gate 				sdp->sd_flags &= ~FLG_SY_SPECSEC;
10705220Srie 				symptr->st_value = (Xword)S_ROUND(bssaddr,
10714716Sab196087 				    symptr->st_value);
10720Sstevel@tonic-gate 				bssaddr = symptr->st_value + symptr->st_size;
10730Sstevel@tonic-gate 				sdp->sd_isc = ofl->ofl_isbss;
10740Sstevel@tonic-gate 				sdp->sd_flags |= FLG_SY_COMMEXP;
10752347Srie 
10760Sstevel@tonic-gate 			} else if (ELF_ST_TYPE(symptr->st_info) == STT_TLS &&
10770Sstevel@tonic-gate 			    (local || !(flags & FLG_OF_RELOBJ))) {
10780Sstevel@tonic-gate 				restore = 1;
10790Sstevel@tonic-gate 				sdp->sd_shndx = tlsbssndx;
10800Sstevel@tonic-gate 				sdp->sd_flags &= ~FLG_SY_SPECSEC;
10810Sstevel@tonic-gate 				symptr->st_value = (Xword)S_ROUND(tlsbssaddr,
10824716Sab196087 				    symptr->st_value);
10830Sstevel@tonic-gate 				tlsbssaddr = symptr->st_value + symptr->st_size;
10840Sstevel@tonic-gate 				sdp->sd_isc = ofl->ofl_istlsbss;
10850Sstevel@tonic-gate 				sdp->sd_flags |= FLG_SY_COMMEXP;
10860Sstevel@tonic-gate 				/*
1087542Srie 				 * TLS symbols are relative to the TLS segment.
10880Sstevel@tonic-gate 				 */
10890Sstevel@tonic-gate 				symptr->st_value -= ofl->ofl_tlsphdr->p_vaddr;
10900Sstevel@tonic-gate 			}
10916206Sab196087 #if	defined(_ELF64)
10926206Sab196087 		} else if ((ld_targ.t_m.m_mach == EM_AMD64) &&
10936206Sab196087 		    (sdp->sd_flags & FLG_SY_SPECSEC) &&
1094574Sseizo 		    ((sdp->sd_shndx = symptr->st_shndx) ==
1095574Sseizo 		    SHN_X86_64_LCOMMON) &&
1096574Sseizo 		    ((local || !(flags & FLG_OF_RELOBJ)))) {
1097574Sseizo 			restore = 1;
1098574Sseizo 			sdp->sd_shndx = lbssndx;
1099574Sseizo 			sdp->sd_flags &= ~FLG_SY_SPECSEC;
11005220Srie 			symptr->st_value = (Xword)S_ROUND(lbssaddr,
11014716Sab196087 			    symptr->st_value);
1102574Sseizo 			lbssaddr = symptr->st_value + symptr->st_size;
1103574Sseizo 			sdp->sd_isc = ofl->ofl_islbss;
1104574Sseizo 			sdp->sd_flags |= FLG_SY_COMMEXP;
1105574Sseizo #endif
1106574Sseizo 		}
1107574Sseizo 
1108574Sseizo 		if (restore != 0) {
11093492Sab196087 			uchar_t		type, bind;
11102347Srie 
1111574Sseizo 			/*
11122347Srie 			 * Make sure this COMMON symbol is returned to the same
11132347Srie 			 * binding as was defined in the original relocatable
1114574Sseizo 			 * object reference.
1115574Sseizo 			 */
1116574Sseizo 			type = ELF_ST_TYPE(symptr->st_info);
1117574Sseizo 			if (sdp->sd_flags & FLG_SY_GLOBREF)
1118574Sseizo 				bind = STB_GLOBAL;
1119574Sseizo 			else
1120574Sseizo 				bind = STB_WEAK;
1121574Sseizo 
1122574Sseizo 			symptr->st_info = ELF_ST_INFO(bind, type);
11230Sstevel@tonic-gate 		}
11240Sstevel@tonic-gate 	}
11250Sstevel@tonic-gate 
112611827SRod.Evans@Sun.COM 	/*
112711827SRod.Evans@Sun.COM 	 * If this is a dynamic object then add any local capabilities symbols.
112811827SRod.Evans@Sun.COM 	 */
112911827SRod.Evans@Sun.COM 	if (dynsym && ofl->ofl_capfamilies) {
113011827SRod.Evans@Sun.COM 		Cap_avlnode	*cav;
113111827SRod.Evans@Sun.COM 
113211827SRod.Evans@Sun.COM 		for (cav = avl_first(ofl->ofl_capfamilies); cav;
113311827SRod.Evans@Sun.COM 		    cav = AVL_NEXT(ofl->ofl_capfamilies, cav)) {
113411827SRod.Evans@Sun.COM 			Cap_sym		*csp;
113511827SRod.Evans@Sun.COM 			Aliste		idx;
113611827SRod.Evans@Sun.COM 
113711827SRod.Evans@Sun.COM 			for (APLIST_TRAVERSE(cav->cn_members, idx, csp)) {
113811827SRod.Evans@Sun.COM 				sdp = csp->cs_sdp;
113911827SRod.Evans@Sun.COM 
114011827SRod.Evans@Sun.COM 				DBG_CALL(Dbg_syms_created(ofl->ofl_lml,
114111827SRod.Evans@Sun.COM 				    sdp->sd_name));
114211827SRod.Evans@Sun.COM 				DBG_CALL(Dbg_syms_entered(ofl, sdp->sd_sym,
114311827SRod.Evans@Sun.COM 				    sdp));
114411827SRod.Evans@Sun.COM 
114511827SRod.Evans@Sun.COM 				dynsym[dynsym_ndx] = *sdp->sd_sym;
114611827SRod.Evans@Sun.COM 
114711827SRod.Evans@Sun.COM 				(void) st_setstring(dynstr, sdp->sd_name,
114811827SRod.Evans@Sun.COM 				    &stoff);
114911827SRod.Evans@Sun.COM 				dynsym[dynsym_ndx].st_name = stoff;
115011827SRod.Evans@Sun.COM 
115111827SRod.Evans@Sun.COM 				sdp->sd_sym = &dynsym[dynsym_ndx];
115211827SRod.Evans@Sun.COM 				sdp->sd_symndx = dynsym_ndx;
115311827SRod.Evans@Sun.COM 
115411827SRod.Evans@Sun.COM 				/*
115511827SRod.Evans@Sun.COM 				 * Indicate that this is a capabilities symbol.
115611827SRod.Evans@Sun.COM 				 * Note, that this identification only provides
115711827SRod.Evans@Sun.COM 				 * information regarding the symbol that is
115811827SRod.Evans@Sun.COM 				 * visible from elfdump(1) -y.  The association
115911827SRod.Evans@Sun.COM 				 * of a symbol to its capabilities is derived
116011827SRod.Evans@Sun.COM 				 * from a .SUNW_capinfo entry.
116111827SRod.Evans@Sun.COM 				 */
116211827SRod.Evans@Sun.COM 				if (syminfo) {
116311827SRod.Evans@Sun.COM 					syminfo[dynsym_ndx].si_flags |=
116411827SRod.Evans@Sun.COM 					    SYMINFO_FLG_CAP;
116511827SRod.Evans@Sun.COM 				}
116611827SRod.Evans@Sun.COM 
116711827SRod.Evans@Sun.COM 				dynsym_ndx++;
116811827SRod.Evans@Sun.COM 			}
116911827SRod.Evans@Sun.COM 		}
117011827SRod.Evans@Sun.COM 	}
117111827SRod.Evans@Sun.COM 
11720Sstevel@tonic-gate 	if (ofl->ofl_hashbkts) {
11730Sstevel@tonic-gate 		qsort(sorted_syms + ofl->ofl_scopecnt + ofl->ofl_elimcnt,
11740Sstevel@tonic-gate 		    ofl->ofl_globcnt, sizeof (Sym_s_list),
11750Sstevel@tonic-gate 		    (int (*)(const void *, const void *))sym_hash_compare);
11760Sstevel@tonic-gate 	}
11770Sstevel@tonic-gate 
11780Sstevel@tonic-gate 	for (ssndx = 0; ssndx < (ofl->ofl_elimcnt + ofl->ofl_scopecnt +
11790Sstevel@tonic-gate 	    ofl->ofl_globcnt); ssndx++) {
11800Sstevel@tonic-gate 		const char	*name;
11810Sstevel@tonic-gate 		Sym		*sym;
11820Sstevel@tonic-gate 		Sym_aux		*sap;
11830Sstevel@tonic-gate 		Half		spec;
11842766Sab196087 		int		local = 0, dynlocal = 0, enter_in_symtab;
11850Sstevel@tonic-gate 		Gotndx		*gnp;
11860Sstevel@tonic-gate 		Word		sectndx;
11870Sstevel@tonic-gate 
11880Sstevel@tonic-gate 		sdp = sorted_syms[ssndx].sl_sdp;
11890Sstevel@tonic-gate 		sectndx = 0;
11900Sstevel@tonic-gate 
11910Sstevel@tonic-gate 		if (symtab)
11920Sstevel@tonic-gate 			enter_in_symtab = 1;
11930Sstevel@tonic-gate 		else
11940Sstevel@tonic-gate 			enter_in_symtab = 0;
11950Sstevel@tonic-gate 
11960Sstevel@tonic-gate 		/*
11970Sstevel@tonic-gate 		 * Assign a got offset if necessary.
11980Sstevel@tonic-gate 		 */
11996206Sab196087 		if ((ld_targ.t_mr.mr_assign_got != NULL) &&
12006206Sab196087 		    (*ld_targ.t_mr.mr_assign_got)(ofl, sdp) == S_ERROR)
12010Sstevel@tonic-gate 			return ((Addr)S_ERROR);
12022347Srie 
12031618Srie 		if (DBG_ENABLED) {
12049131SRod.Evans@Sun.COM 			Aliste	idx2;
12059131SRod.Evans@Sun.COM 
12069131SRod.Evans@Sun.COM 			for (ALIST_TRAVERSE(sdp->sd_GOTndxs, idx2, gnp)) {
12072145Srie 				gottable->gt_sym = sdp;
12082145Srie 				gottable->gt_gndx.gn_gotndx = gnp->gn_gotndx;
12092145Srie 				gottable->gt_gndx.gn_addend = gnp->gn_addend;
12102145Srie 				gottable++;
12110Sstevel@tonic-gate 			}
12120Sstevel@tonic-gate 
12130Sstevel@tonic-gate 			if (sdp->sd_aux && sdp->sd_aux->sa_PLTGOTndx) {
12142145Srie 				gottable->gt_sym = sdp;
12152145Srie 				gottable->gt_gndx.gn_gotndx =
12160Sstevel@tonic-gate 				    sdp->sd_aux->sa_PLTGOTndx;
12172145Srie 				gottable++;
12180Sstevel@tonic-gate 			}
12190Sstevel@tonic-gate 		}
12200Sstevel@tonic-gate 
12210Sstevel@tonic-gate 		/*
12220Sstevel@tonic-gate 		 * If this symbol has been marked as being reduced to local
12230Sstevel@tonic-gate 		 * scope then it will have to be placed in the scoped portion
12240Sstevel@tonic-gate 		 * of the .symtab.  Retain the appropriate index for use in
12250Sstevel@tonic-gate 		 * version symbol indexing and relocation.
12260Sstevel@tonic-gate 		 */
122711827SRod.Evans@Sun.COM 		if (SYM_IS_HIDDEN(sdp) && (flags & FLG_OF_PROCRED)) {
12280Sstevel@tonic-gate 			local = 1;
122910792SRod.Evans@Sun.COM 			if (!(sdp->sd_flags & FLG_SY_ELIM) && !dynsym)
12300Sstevel@tonic-gate 				sdp->sd_symndx = scopesym_ndx;
12310Sstevel@tonic-gate 			else
12320Sstevel@tonic-gate 				sdp->sd_symndx = 0;
12330Sstevel@tonic-gate 
123410792SRod.Evans@Sun.COM 			if (sdp->sd_flags & FLG_SY_ELIM) {
12350Sstevel@tonic-gate 				enter_in_symtab = 0;
12362766Sab196087 			} else if (ldynsym && sdp->sd_sym->st_name &&
12373492Sab196087 			    ldynsym_symtype[
12383492Sab196087 			    ELF_ST_TYPE(sdp->sd_sym->st_info)]) {
12392766Sab196087 				dynlocal = 1;
12402766Sab196087 			}
12412766Sab196087 		} else {
12420Sstevel@tonic-gate 			sdp->sd_symndx = *symndx;
12432766Sab196087 		}
12440Sstevel@tonic-gate 
12450Sstevel@tonic-gate 		/*
12460Sstevel@tonic-gate 		 * Copy basic symbol and string information.
12470Sstevel@tonic-gate 		 */
12480Sstevel@tonic-gate 		name = sdp->sd_name;
12490Sstevel@tonic-gate 		sap = sdp->sd_aux;
12500Sstevel@tonic-gate 
12510Sstevel@tonic-gate 		/*
12520Sstevel@tonic-gate 		 * If we require to record version symbol indexes, update the
12530Sstevel@tonic-gate 		 * associated version symbol information for all defined
12540Sstevel@tonic-gate 		 * symbols.  If a version definition is required any zero value
12550Sstevel@tonic-gate 		 * symbol indexes would have been flagged as undefined symbol
12560Sstevel@tonic-gate 		 * errors, however if we're just scoping these need to fall into
12570Sstevel@tonic-gate 		 * the base of global symbols.
12580Sstevel@tonic-gate 		 */
12590Sstevel@tonic-gate 		if (sdp->sd_symndx && versym) {
12600Sstevel@tonic-gate 			Half	vndx = 0;
12610Sstevel@tonic-gate 
12627682SAli.Bahrami@Sun.COM 			if (sdp->sd_flags & FLG_SY_MVTOCOMM) {
12630Sstevel@tonic-gate 				vndx = VER_NDX_GLOBAL;
12647682SAli.Bahrami@Sun.COM 			} else if (sdp->sd_ref == REF_REL_NEED) {
12650Sstevel@tonic-gate 				vndx = sap->sa_overndx;
126611827SRod.Evans@Sun.COM 
12670Sstevel@tonic-gate 				if ((vndx == 0) &&
12680Sstevel@tonic-gate 				    (sdp->sd_sym->st_shndx != SHN_UNDEF)) {
126911827SRod.Evans@Sun.COM 					if (SYM_IS_HIDDEN(sdp))
12700Sstevel@tonic-gate 						vndx = VER_NDX_LOCAL;
12710Sstevel@tonic-gate 					else
12720Sstevel@tonic-gate 						vndx = VER_NDX_GLOBAL;
12730Sstevel@tonic-gate 				}
12747682SAli.Bahrami@Sun.COM 			} else if ((sdp->sd_ref == REF_DYN_NEED) &&
12757682SAli.Bahrami@Sun.COM 			    (sap->sa_dverndx > 0) &&
12767682SAli.Bahrami@Sun.COM 			    (sap->sa_dverndx <= sdp->sd_file->ifl_vercnt) &&
12777682SAli.Bahrami@Sun.COM 			    (sdp->sd_file->ifl_verndx != NULL)) {
12787682SAli.Bahrami@Sun.COM 				/* Use index of verneed record */
12797682SAli.Bahrami@Sun.COM 				vndx = sdp->sd_file->ifl_verndx
12807682SAli.Bahrami@Sun.COM 				    [sap->sa_dverndx].vi_overndx;
12810Sstevel@tonic-gate 			}
12820Sstevel@tonic-gate 			versym[sdp->sd_symndx] = vndx;
12830Sstevel@tonic-gate 		}
12840Sstevel@tonic-gate 
12850Sstevel@tonic-gate 		/*
12860Sstevel@tonic-gate 		 * If we are creating the .syminfo section then set per symbol
12870Sstevel@tonic-gate 		 * flags here.
12880Sstevel@tonic-gate 		 */
12890Sstevel@tonic-gate 		if (sdp->sd_symndx && syminfo &&
12900Sstevel@tonic-gate 		    !(sdp->sd_flags & FLG_SY_NOTAVAIL)) {
12910Sstevel@tonic-gate 			int	ndx = sdp->sd_symndx;
129210792SRod.Evans@Sun.COM 			APlist	**alpp = &(ofl->ofl_symdtent);
12930Sstevel@tonic-gate 
12940Sstevel@tonic-gate 			if (sdp->sd_flags & FLG_SY_MVTOCOMM)
12950Sstevel@tonic-gate 				/*
12960Sstevel@tonic-gate 				 * Identify a copy relocation symbol.
12970Sstevel@tonic-gate 				 */
12980Sstevel@tonic-gate 				syminfo[ndx].si_flags |= SYMINFO_FLG_COPY;
12990Sstevel@tonic-gate 
13000Sstevel@tonic-gate 			if (sdp->sd_ref == REF_DYN_NEED) {
13010Sstevel@tonic-gate 				/*
13020Sstevel@tonic-gate 				 * A reference is bound to a needed dependency.
130310167SRod.Evans@Sun.COM 				 * Save the syminfo entry, so that when the
130410167SRod.Evans@Sun.COM 				 * .dynamic section has been updated, a
130510167SRod.Evans@Sun.COM 				 * DT_NEEDED entry can be associated
130610167SRod.Evans@Sun.COM 				 * (see update_osyminfo()).
13070Sstevel@tonic-gate 				 */
13089131SRod.Evans@Sun.COM 				if (aplist_append(alpp, sdp,
13099131SRod.Evans@Sun.COM 				    AL_CNT_OFL_SYMINFOSYMS) == NULL)
13100Sstevel@tonic-gate 					return (0);
13110Sstevel@tonic-gate 
131210167SRod.Evans@Sun.COM 				/*
131310167SRod.Evans@Sun.COM 				 * Flag that the symbol has a direct association
131410167SRod.Evans@Sun.COM 				 * with the external reference (this is an old
131510167SRod.Evans@Sun.COM 				 * tagging, that has no real effect by itself).
131610167SRod.Evans@Sun.COM 				 */
13170Sstevel@tonic-gate 				syminfo[ndx].si_flags |= SYMINFO_FLG_DIRECT;
131812449SRod.Evans@Sun.COM 
131912449SRod.Evans@Sun.COM 				/*
132012449SRod.Evans@Sun.COM 				 * Flag any lazy or deferred reference.
132112449SRod.Evans@Sun.COM 				 */
13220Sstevel@tonic-gate 				if (sdp->sd_flags & FLG_SY_LAZYLD)
13230Sstevel@tonic-gate 					syminfo[ndx].si_flags |=
13240Sstevel@tonic-gate 					    SYMINFO_FLG_LAZYLOAD;
132512449SRod.Evans@Sun.COM 				if (sdp->sd_flags & FLG_SY_DEFERRED)
132612449SRod.Evans@Sun.COM 					syminfo[ndx].si_flags |=
132712449SRod.Evans@Sun.COM 					    SYMINFO_FLG_DEFERRED;
13280Sstevel@tonic-gate 
13290Sstevel@tonic-gate 				/*
13300Sstevel@tonic-gate 				 * Enable direct symbol bindings if:
13310Sstevel@tonic-gate 				 *
13329131SRod.Evans@Sun.COM 				 *  -	Symbol was identified with the DIRECT
13330Sstevel@tonic-gate 				 *	keyword in a mapfile.
13340Sstevel@tonic-gate 				 *
13359131SRod.Evans@Sun.COM 				 *  -	Symbol reference has been bound to a
13360Sstevel@tonic-gate 				 * 	dependency which was specified as
13370Sstevel@tonic-gate 				 *	requiring direct bindings with -zdirect.
13380Sstevel@tonic-gate 				 *
13399131SRod.Evans@Sun.COM 				 *  -	All symbol references are required to
13400Sstevel@tonic-gate 				 *	use direct bindings via -Bdirect.
13410Sstevel@tonic-gate 				 */
134210792SRod.Evans@Sun.COM 				if (sdp->sd_flags & FLG_SY_DIR)
13430Sstevel@tonic-gate 					syminfo[ndx].si_flags |=
13440Sstevel@tonic-gate 					    SYMINFO_FLG_DIRECTBIND;
13450Sstevel@tonic-gate 
13460Sstevel@tonic-gate 			} else if ((sdp->sd_flags & FLG_SY_EXTERN) &&
13470Sstevel@tonic-gate 			    (sdp->sd_sym->st_shndx == SHN_UNDEF)) {
13480Sstevel@tonic-gate 				/*
13490Sstevel@tonic-gate 				 * If this symbol has been explicitly defined
13500Sstevel@tonic-gate 				 * as external, and remains unresolved, mark
13510Sstevel@tonic-gate 				 * it as external.
13520Sstevel@tonic-gate 				 */
13530Sstevel@tonic-gate 				syminfo[ndx].si_boundto = SYMINFO_BT_EXTERN;
13540Sstevel@tonic-gate 
13553466Srie 			} else if ((sdp->sd_flags & FLG_SY_PARENT) &&
13563466Srie 			    (sdp->sd_sym->st_shndx == SHN_UNDEF)) {
13570Sstevel@tonic-gate 				/*
13583466Srie 				 * If this symbol has been explicitly defined
13593466Srie 				 * to be a reference to a parent object,
13603466Srie 				 * indicate whether a direct binding should be
13610Sstevel@tonic-gate 				 * established.
13620Sstevel@tonic-gate 				 */
13630Sstevel@tonic-gate 				syminfo[ndx].si_flags |= SYMINFO_FLG_DIRECT;
13640Sstevel@tonic-gate 				syminfo[ndx].si_boundto = SYMINFO_BT_PARENT;
136510792SRod.Evans@Sun.COM 				if (sdp->sd_flags & FLG_SY_DIR)
13660Sstevel@tonic-gate 					syminfo[ndx].si_flags |=
13670Sstevel@tonic-gate 					    SYMINFO_FLG_DIRECTBIND;
13680Sstevel@tonic-gate 
13690Sstevel@tonic-gate 			} else if (sdp->sd_flags & FLG_SY_STDFLTR) {
13700Sstevel@tonic-gate 				/*
13710Sstevel@tonic-gate 				 * A filter definition.  Although this symbol
13720Sstevel@tonic-gate 				 * can only be a stub, it might be necessary to
13730Sstevel@tonic-gate 				 * prevent external direct bindings.
13740Sstevel@tonic-gate 				 */
13750Sstevel@tonic-gate 				syminfo[ndx].si_flags |= SYMINFO_FLG_FILTER;
137610792SRod.Evans@Sun.COM 				if (sdp->sd_flags & FLG_SY_NDIR)
13770Sstevel@tonic-gate 					syminfo[ndx].si_flags |=
13780Sstevel@tonic-gate 					    SYMINFO_FLG_NOEXTDIRECT;
13790Sstevel@tonic-gate 
13800Sstevel@tonic-gate 			} else if (sdp->sd_flags & FLG_SY_AUXFLTR) {
13810Sstevel@tonic-gate 				/*
13820Sstevel@tonic-gate 				 * An auxiliary filter definition.  By nature,
13830Sstevel@tonic-gate 				 * this definition is direct, in that should the
13840Sstevel@tonic-gate 				 * filtee lookup fail, we'll fall back to this
138511827SRod.Evans@Sun.COM 				 * object.  It may still be necessary to
13860Sstevel@tonic-gate 				 * prevent external direct bindings.
13870Sstevel@tonic-gate 				 */
13880Sstevel@tonic-gate 				syminfo[ndx].si_flags |= SYMINFO_FLG_AUXILIARY;
138910792SRod.Evans@Sun.COM 				if (sdp->sd_flags & FLG_SY_NDIR)
13900Sstevel@tonic-gate 					syminfo[ndx].si_flags |=
13910Sstevel@tonic-gate 					    SYMINFO_FLG_NOEXTDIRECT;
13920Sstevel@tonic-gate 
13930Sstevel@tonic-gate 			} else if ((sdp->sd_ref == REF_REL_NEED) &&
13940Sstevel@tonic-gate 			    (sdp->sd_sym->st_shndx != SHN_UNDEF)) {
13950Sstevel@tonic-gate 				/*
13960Sstevel@tonic-gate 				 * This definition exists within the object
139710792SRod.Evans@Sun.COM 				 * being created.  Provide a default boundto
139810792SRod.Evans@Sun.COM 				 * definition, which may be overridden later.
13990Sstevel@tonic-gate 				 */
140010792SRod.Evans@Sun.COM 				syminfo[ndx].si_boundto = SYMINFO_BT_NONE;
140110792SRod.Evans@Sun.COM 
140210792SRod.Evans@Sun.COM 				/*
140310792SRod.Evans@Sun.COM 				 * Indicate whether it is necessary to prevent
140410792SRod.Evans@Sun.COM 				 * external direct bindings.
140510792SRod.Evans@Sun.COM 				 */
140610792SRod.Evans@Sun.COM 				if (sdp->sd_flags & FLG_SY_NDIR) {
14070Sstevel@tonic-gate 					syminfo[ndx].si_flags |=
14080Sstevel@tonic-gate 					    SYMINFO_FLG_NOEXTDIRECT;
14090Sstevel@tonic-gate 				}
14100Sstevel@tonic-gate 
14110Sstevel@tonic-gate 				/*
14123466Srie 				 * Indicate that this symbol is acting as an
14133466Srie 				 * individual interposer.
14143466Srie 				 */
14153466Srie 				if (sdp->sd_flags & FLG_SY_INTPOSE) {
14163466Srie 					syminfo[ndx].si_flags |=
14173466Srie 					    SYMINFO_FLG_INTERPOSE;
14183466Srie 				}
14193466Srie 
14203466Srie 				/*
142112449SRod.Evans@Sun.COM 				 * Indicate that this symbol is deferred, and
142212449SRod.Evans@Sun.COM 				 * hence should not be bound to during BIND_NOW
142312449SRod.Evans@Sun.COM 				 * relocations.
142412449SRod.Evans@Sun.COM 				 */
142512449SRod.Evans@Sun.COM 				if (sdp->sd_flags & FLG_SY_DEFERRED) {
142612449SRod.Evans@Sun.COM 					syminfo[ndx].si_flags |=
142712449SRod.Evans@Sun.COM 					    SYMINFO_FLG_DEFERRED;
142812449SRod.Evans@Sun.COM 				}
142912449SRod.Evans@Sun.COM 
143012449SRod.Evans@Sun.COM 				/*
143110792SRod.Evans@Sun.COM 				 * If external bindings are allowed, indicate
143210792SRod.Evans@Sun.COM 				 * the binding, and a direct binding if
143310792SRod.Evans@Sun.COM 				 * necessary.
14340Sstevel@tonic-gate 				 */
143510792SRod.Evans@Sun.COM 				if ((sdp->sd_flags & FLG_SY_NDIR) == 0) {
14360Sstevel@tonic-gate 					syminfo[ndx].si_flags |=
14370Sstevel@tonic-gate 					    SYMINFO_FLG_DIRECT;
14380Sstevel@tonic-gate 
143910792SRod.Evans@Sun.COM 					if (sdp->sd_flags & FLG_SY_DIR)
14400Sstevel@tonic-gate 						syminfo[ndx].si_flags |=
14410Sstevel@tonic-gate 						    SYMINFO_FLG_DIRECTBIND;
14420Sstevel@tonic-gate 
14430Sstevel@tonic-gate 					/*
144410792SRod.Evans@Sun.COM 					 * Provide a default boundto definition,
144510792SRod.Evans@Sun.COM 					 * which may be overridden later.
14460Sstevel@tonic-gate 					 */
144710792SRod.Evans@Sun.COM 					syminfo[ndx].si_boundto =
144810792SRod.Evans@Sun.COM 					    SYMINFO_BT_SELF;
14490Sstevel@tonic-gate 				}
145011827SRod.Evans@Sun.COM 
145111827SRod.Evans@Sun.COM 				/*
145211827SRod.Evans@Sun.COM 				 * Indicate that this is a capabilities symbol.
145311827SRod.Evans@Sun.COM 				 * Note, that this identification only provides
145411827SRod.Evans@Sun.COM 				 * information regarding the symbol that is
145511827SRod.Evans@Sun.COM 				 * visible from elfdump(1) -y.  The association
145611827SRod.Evans@Sun.COM 				 * of a symbol to its capabilities is derived
145711827SRod.Evans@Sun.COM 				 * from a .SUNW_capinfo entry.
145811827SRod.Evans@Sun.COM 				 */
145911827SRod.Evans@Sun.COM 				if ((sdp->sd_flags & FLG_SY_CAP) &&
146011827SRod.Evans@Sun.COM 				    ofl->ofl_oscapinfo) {
146111827SRod.Evans@Sun.COM 					syminfo[ndx].si_flags |=
146211827SRod.Evans@Sun.COM 					    SYMINFO_FLG_CAP;
146311827SRod.Evans@Sun.COM 				}
14640Sstevel@tonic-gate 			}
14650Sstevel@tonic-gate 		}
14660Sstevel@tonic-gate 
14670Sstevel@tonic-gate 		/*
14680Sstevel@tonic-gate 		 * Note that the `sym' value is reset to be one of the new
14690Sstevel@tonic-gate 		 * symbol table entries.  This symbol will be updated further
14700Sstevel@tonic-gate 		 * depending on the type of the symbol.  Process the .symtab
14710Sstevel@tonic-gate 		 * first, followed by the .dynsym, thus the `sym' value will
14720Sstevel@tonic-gate 		 * remain as the .dynsym value when the .dynsym is present.
14732347Srie 		 * This ensures that any versioning symbols st_name value will
14742766Sab196087 		 * be appropriate for the string table used by version
14750Sstevel@tonic-gate 		 * entries.
14760Sstevel@tonic-gate 		 */
14770Sstevel@tonic-gate 		if (enter_in_symtab) {
14780Sstevel@tonic-gate 			Word	_symndx;
14790Sstevel@tonic-gate 
14800Sstevel@tonic-gate 			if (local)
14810Sstevel@tonic-gate 				_symndx = scopesym_ndx;
14820Sstevel@tonic-gate 			else
14830Sstevel@tonic-gate 				_symndx = symtab_ndx;
14842766Sab196087 
14850Sstevel@tonic-gate 			symtab[_symndx] = *sdp->sd_sym;
14860Sstevel@tonic-gate 			sdp->sd_sym = sym = &symtab[_symndx];
14870Sstevel@tonic-gate 			(void) st_setstring(strtab, name, &stoff);
14880Sstevel@tonic-gate 			sym->st_name = stoff;
14890Sstevel@tonic-gate 		}
14902766Sab196087 		if (dynlocal) {
14912766Sab196087 			ldynsym[ldynscopesym_ndx] = *sdp->sd_sym;
14922766Sab196087 			sdp->sd_sym = sym = &ldynsym[ldynscopesym_ndx];
14932766Sab196087 			(void) st_setstring(dynstr, name, &stoff);
14942766Sab196087 			ldynsym[ldynscopesym_ndx].st_name = stoff;
14953492Sab196087 			/* Add it to sort section if it qualifies */
14963492Sab196087 			ADD_TO_DYNSORT(sdp, sym, ELF_ST_TYPE(sym->st_info),
14973492Sab196087 			    ldynscopesym_ndx);
14982766Sab196087 		}
14990Sstevel@tonic-gate 
15000Sstevel@tonic-gate 		if (dynsym && !local) {
15010Sstevel@tonic-gate 			dynsym[dynsym_ndx] = *sdp->sd_sym;
15025220Srie 
15030Sstevel@tonic-gate 			/*
15040Sstevel@tonic-gate 			 * Provided this isn't an unnamed register symbol,
15055220Srie 			 * update the symbols name and hash value.
15060Sstevel@tonic-gate 			 */
15070Sstevel@tonic-gate 			if (((sdp->sd_flags & FLG_SY_REGSYM) == 0) ||
15080Sstevel@tonic-gate 			    dynsym[dynsym_ndx].st_name) {
15094716Sab196087 				(void) st_setstring(dynstr, name, &stoff);
15104716Sab196087 				dynsym[dynsym_ndx].st_name = stoff;
15115220Srie 
15124716Sab196087 				if (stoff) {
151311827SRod.Evans@Sun.COM 					Word	hashval, _hashndx;
15145220Srie 
15155220Srie 					hashval =
15165220Srie 					    sap->sa_hash % ofl->ofl_hashbkts;
15175220Srie 
15184716Sab196087 					/* LINTED */
15194716Sab196087 					if (_hashndx = hashbkt[hashval]) {
15205220Srie 						while (hashchain[_hashndx]) {
15214716Sab196087 							_hashndx =
15224716Sab196087 							    hashchain[_hashndx];
15235220Srie 						}
15244716Sab196087 						hashchain[_hashndx] =
15254716Sab196087 						    sdp->sd_symndx;
15264716Sab196087 					} else {
15274716Sab196087 						hashbkt[hashval] =
15284716Sab196087 						    sdp->sd_symndx;
15294716Sab196087 					}
15300Sstevel@tonic-gate 				}
15310Sstevel@tonic-gate 			}
15320Sstevel@tonic-gate 			sdp->sd_sym = sym = &dynsym[dynsym_ndx];
15335220Srie 
15343492Sab196087 			/*
15353492Sab196087 			 * Add it to sort section if it qualifies.
15363492Sab196087 			 * The indexes in that section are relative to the
15373492Sab196087 			 * the adjacent SUNW_ldynsym/dymsym pair, so we
15383492Sab196087 			 * add the number of items in SUNW_ldynsym to the
15393492Sab196087 			 * dynsym index.
15403492Sab196087 			 */
15413492Sab196087 			ADD_TO_DYNSORT(sdp, sym, ELF_ST_TYPE(sym->st_info),
15423492Sab196087 			    ldynsym_cnt + dynsym_ndx);
15430Sstevel@tonic-gate 		}
154411827SRod.Evans@Sun.COM 
15452766Sab196087 		if (!enter_in_symtab && (!dynsym || (local && !dynlocal))) {
15460Sstevel@tonic-gate 			if (!(sdp->sd_flags & FLG_SY_UPREQD))
15470Sstevel@tonic-gate 				continue;
15480Sstevel@tonic-gate 			sym = sdp->sd_sym;
15490Sstevel@tonic-gate 		} else
15500Sstevel@tonic-gate 			sdp->sd_flags &= ~FLG_SY_CLEAN;
15510Sstevel@tonic-gate 
15520Sstevel@tonic-gate 		/*
15530Sstevel@tonic-gate 		 * If we have a weak data symbol for which we need the real
15540Sstevel@tonic-gate 		 * symbol also, save this processing until later.
15550Sstevel@tonic-gate 		 *
15560Sstevel@tonic-gate 		 * The exception to this is if the weak/strong have PLT's
15570Sstevel@tonic-gate 		 * assigned to them.  In that case we don't do the post-weak
15580Sstevel@tonic-gate 		 * processing because the PLT's must be maintained so that we
15590Sstevel@tonic-gate 		 * can do 'interpositioning' on both of the symbols.
15600Sstevel@tonic-gate 		 */
15610Sstevel@tonic-gate 		if ((sap->sa_linkndx) &&
15620Sstevel@tonic-gate 		    (ELF_ST_BIND(sym->st_info) == STB_WEAK) &&
15630Sstevel@tonic-gate 		    (!sap->sa_PLTndx)) {
15649131SRod.Evans@Sun.COM 			Sym_desc	*_sdp;
15659131SRod.Evans@Sun.COM 
15669131SRod.Evans@Sun.COM 			_sdp = sdp->sd_file->ifl_oldndx[sap->sa_linkndx];
15670Sstevel@tonic-gate 
15680Sstevel@tonic-gate 			if (_sdp->sd_ref != REF_DYN_SEEN) {
15699131SRod.Evans@Sun.COM 				Wk_desc	wk;
15700Sstevel@tonic-gate 
15712766Sab196087 				if (enter_in_symtab) {
15729131SRod.Evans@Sun.COM 					if (local) {
15739131SRod.Evans@Sun.COM 						wk.wk_symtab =
15740Sstevel@tonic-gate 						    &symtab[scopesym_ndx];
15759131SRod.Evans@Sun.COM 						scopesym_ndx++;
15769131SRod.Evans@Sun.COM 					} else {
15779131SRod.Evans@Sun.COM 						wk.wk_symtab =
15780Sstevel@tonic-gate 						    &symtab[symtab_ndx];
15799131SRod.Evans@Sun.COM 						symtab_ndx++;
15809131SRod.Evans@Sun.COM 					}
15819313SAli.Bahrami@Sun.COM 				} else {
15829313SAli.Bahrami@Sun.COM 					wk.wk_symtab = NULL;
15832766Sab196087 				}
15842766Sab196087 				if (dynsym) {
15852766Sab196087 					if (!local) {
15869131SRod.Evans@Sun.COM 						wk.wk_dynsym =
15872766Sab196087 						    &dynsym[dynsym_ndx];
15882766Sab196087 						dynsym_ndx++;
15892766Sab196087 					} else if (dynlocal) {
15909131SRod.Evans@Sun.COM 						wk.wk_dynsym =
15919131SRod.Evans@Sun.COM 						    &ldynsym[ldynscopesym_ndx];
15922766Sab196087 						ldynscopesym_ndx++;
15932766Sab196087 					}
15949313SAli.Bahrami@Sun.COM 				} else {
15959313SAli.Bahrami@Sun.COM 					wk.wk_dynsym = NULL;
15962766Sab196087 				}
15979131SRod.Evans@Sun.COM 				wk.wk_weak = sdp;
15989131SRod.Evans@Sun.COM 				wk.wk_alias = _sdp;
15999131SRod.Evans@Sun.COM 
16009131SRod.Evans@Sun.COM 				if (alist_append(&weak, &wk,
16019131SRod.Evans@Sun.COM 				    sizeof (Wk_desc), AL_CNT_WEAK) == NULL)
16029131SRod.Evans@Sun.COM 					return ((Addr)S_ERROR);
16039131SRod.Evans@Sun.COM 
16040Sstevel@tonic-gate 				continue;
16050Sstevel@tonic-gate 			}
16060Sstevel@tonic-gate 		}
16070Sstevel@tonic-gate 
16081618Srie 		DBG_CALL(Dbg_syms_old(ofl, sdp));
16090Sstevel@tonic-gate 
16100Sstevel@tonic-gate 		spec = NULL;
16110Sstevel@tonic-gate 		/*
16120Sstevel@tonic-gate 		 * assign new symbol value.
16130Sstevel@tonic-gate 		 */
16140Sstevel@tonic-gate 		sectndx = sdp->sd_shndx;
16150Sstevel@tonic-gate 		if (sectndx == SHN_UNDEF) {
16160Sstevel@tonic-gate 			if (((sdp->sd_flags & FLG_SY_REGSYM) == 0) &&
16170Sstevel@tonic-gate 			    (sym->st_value != 0)) {
1618*13074SAli.Bahrami@Oracle.COM 				ld_eprintf(ofl, ERR_WARNING,
16191618Srie 				    MSG_INTL(MSG_SYM_NOTNULL),
16200Sstevel@tonic-gate 				    demangle(name), sdp->sd_file->ifl_name);
16210Sstevel@tonic-gate 			}
16220Sstevel@tonic-gate 
16230Sstevel@tonic-gate 			/*
16240Sstevel@tonic-gate 			 * Undefined weak global, if we are generating a static
16250Sstevel@tonic-gate 			 * executable, output as an absolute zero.  Otherwise
16260Sstevel@tonic-gate 			 * leave it as is, ld.so.1 will skip symbols of this
16270Sstevel@tonic-gate 			 * type (this technique allows applications and
16280Sstevel@tonic-gate 			 * libraries to test for the existence of a symbol as an
16290Sstevel@tonic-gate 			 * indication of the presence or absence of certain
16300Sstevel@tonic-gate 			 * functionality).
16310Sstevel@tonic-gate 			 */
163210792SRod.Evans@Sun.COM 			if (OFL_IS_STATIC_EXEC(ofl) &&
16330Sstevel@tonic-gate 			    (ELF_ST_BIND(sym->st_info) == STB_WEAK)) {
16340Sstevel@tonic-gate 				sdp->sd_flags |= FLG_SY_SPECSEC;
16350Sstevel@tonic-gate 				sdp->sd_shndx = sectndx = SHN_ABS;
16360Sstevel@tonic-gate 			}
16370Sstevel@tonic-gate 		} else if ((sdp->sd_flags & FLG_SY_SPECSEC) &&
16380Sstevel@tonic-gate 		    (sectndx == SHN_COMMON)) {
16390Sstevel@tonic-gate 			/* COMMONs have already been processed */
16400Sstevel@tonic-gate 			/* EMPTY */
16410Sstevel@tonic-gate 			;
16420Sstevel@tonic-gate 		} else {
16430Sstevel@tonic-gate 			if ((sdp->sd_flags & FLG_SY_SPECSEC) &&
16440Sstevel@tonic-gate 			    (sectndx == SHN_ABS))
16450Sstevel@tonic-gate 				spec = sdp->sd_aux->sa_symspec;
16460Sstevel@tonic-gate 
16470Sstevel@tonic-gate 			/* LINTED */
16480Sstevel@tonic-gate 			if (sdp->sd_flags & FLG_SY_COMMEXP) {
16490Sstevel@tonic-gate 				/*
16500Sstevel@tonic-gate 				 * This is (or was) a COMMON symbol which was
16510Sstevel@tonic-gate 				 * processed above - no processing
16520Sstevel@tonic-gate 				 * required here.
16530Sstevel@tonic-gate 				 */
16540Sstevel@tonic-gate 				;
16550Sstevel@tonic-gate 			} else if (sdp->sd_ref == REF_DYN_NEED) {
16563492Sab196087 				uchar_t	type, bind;
16570Sstevel@tonic-gate 
16580Sstevel@tonic-gate 				sectndx = SHN_UNDEF;
16590Sstevel@tonic-gate 				sym->st_value = 0;
16600Sstevel@tonic-gate 				sym->st_size = 0;
16610Sstevel@tonic-gate 
16620Sstevel@tonic-gate 				/*
16630Sstevel@tonic-gate 				 * Make sure this undefined symbol is returned
16640Sstevel@tonic-gate 				 * to the same binding as was defined in the
16650Sstevel@tonic-gate 				 * original relocatable object reference.
16660Sstevel@tonic-gate 				 */
16670Sstevel@tonic-gate 				type = ELF_ST_TYPE(sym-> st_info);
16680Sstevel@tonic-gate 				if (sdp->sd_flags & FLG_SY_GLOBREF)
16690Sstevel@tonic-gate 					bind = STB_GLOBAL;
16700Sstevel@tonic-gate 				else
16710Sstevel@tonic-gate 					bind = STB_WEAK;
16720Sstevel@tonic-gate 
16730Sstevel@tonic-gate 				sym->st_info = ELF_ST_INFO(bind, type);
16740Sstevel@tonic-gate 
16750Sstevel@tonic-gate 			} else if (((sdp->sd_flags & FLG_SY_SPECSEC) == 0) &&
16760Sstevel@tonic-gate 			    (sdp->sd_ref == REF_REL_NEED)) {
16770Sstevel@tonic-gate 				osp = sdp->sd_isc->is_osdesc;
16780Sstevel@tonic-gate 				/* LINTED */
16790Sstevel@tonic-gate 				sectndx = elf_ndxscn(osp->os_scn);
16800Sstevel@tonic-gate 
16810Sstevel@tonic-gate 				/*
16820Sstevel@tonic-gate 				 * In an executable, the new symbol value is the
16830Sstevel@tonic-gate 				 * old value (offset into defining section) plus
16840Sstevel@tonic-gate 				 * virtual address of defining section.  In a
16850Sstevel@tonic-gate 				 * relocatable, the new value is the old value
16860Sstevel@tonic-gate 				 * plus the displacement of the section within
16870Sstevel@tonic-gate 				 * the file.
16880Sstevel@tonic-gate 				 */
16890Sstevel@tonic-gate 				/* LINTED */
16900Sstevel@tonic-gate 				sym->st_value +=
16910Sstevel@tonic-gate 				    (Off)_elf_getxoff(sdp->sd_isc->is_indata);
16920Sstevel@tonic-gate 
16930Sstevel@tonic-gate 				if (!(flags & FLG_OF_RELOBJ)) {
16940Sstevel@tonic-gate 					sym->st_value += osp->os_shdr->sh_addr;
16950Sstevel@tonic-gate 					/*
16960Sstevel@tonic-gate 					 * TLS symbols are relative to
16970Sstevel@tonic-gate 					 * the TLS segment.
16980Sstevel@tonic-gate 					 */
16990Sstevel@tonic-gate 					if ((ELF_ST_TYPE(sym->st_info) ==
17000Sstevel@tonic-gate 					    STT_TLS) && (ofl->ofl_tlsphdr))
17010Sstevel@tonic-gate 						sym->st_value -=
17020Sstevel@tonic-gate 						    ofl->ofl_tlsphdr->p_vaddr;
17030Sstevel@tonic-gate 				}
17040Sstevel@tonic-gate 			}
17050Sstevel@tonic-gate 		}
17060Sstevel@tonic-gate 
17070Sstevel@tonic-gate 		if (spec) {
17080Sstevel@tonic-gate 			switch (spec) {
17090Sstevel@tonic-gate 			case SDAUX_ID_ETEXT:
17100Sstevel@tonic-gate 				sym->st_value = etext;
17110Sstevel@tonic-gate 				sectndx = etext_ndx;
17120Sstevel@tonic-gate 				if (etext_abs)
17130Sstevel@tonic-gate 					sdp->sd_flags |= FLG_SY_SPECSEC;
17140Sstevel@tonic-gate 				else
17150Sstevel@tonic-gate 					sdp->sd_flags &= ~FLG_SY_SPECSEC;
17160Sstevel@tonic-gate 				break;
17170Sstevel@tonic-gate 			case SDAUX_ID_EDATA:
17180Sstevel@tonic-gate 				sym->st_value = edata;
17190Sstevel@tonic-gate 				sectndx = edata_ndx;
17200Sstevel@tonic-gate 				if (edata_abs)
17210Sstevel@tonic-gate 					sdp->sd_flags |= FLG_SY_SPECSEC;
17220Sstevel@tonic-gate 				else
17230Sstevel@tonic-gate 					sdp->sd_flags &= ~FLG_SY_SPECSEC;
17240Sstevel@tonic-gate 				break;
17250Sstevel@tonic-gate 			case SDAUX_ID_END:
17260Sstevel@tonic-gate 				sym->st_value = end;
17270Sstevel@tonic-gate 				sectndx = end_ndx;
17280Sstevel@tonic-gate 				if (end_abs)
17290Sstevel@tonic-gate 					sdp->sd_flags |= FLG_SY_SPECSEC;
17300Sstevel@tonic-gate 				else
17310Sstevel@tonic-gate 					sdp->sd_flags &= ~FLG_SY_SPECSEC;
17320Sstevel@tonic-gate 				break;
17330Sstevel@tonic-gate 			case SDAUX_ID_START:
17340Sstevel@tonic-gate 				sym->st_value = start;
17350Sstevel@tonic-gate 				sectndx = start_ndx;
17360Sstevel@tonic-gate 				sdp->sd_flags &= ~FLG_SY_SPECSEC;
17370Sstevel@tonic-gate 				break;
17380Sstevel@tonic-gate 			case SDAUX_ID_DYN:
17390Sstevel@tonic-gate 				if (flags & FLG_OF_DYNAMIC) {
17400Sstevel@tonic-gate 					sym->st_value = ofl->
17410Sstevel@tonic-gate 					    ofl_osdynamic->os_shdr->sh_addr;
17420Sstevel@tonic-gate 					/* LINTED */
17430Sstevel@tonic-gate 					sectndx = elf_ndxscn(
17440Sstevel@tonic-gate 					    ofl->ofl_osdynamic->os_scn);
17450Sstevel@tonic-gate 					sdp->sd_flags &= ~FLG_SY_SPECSEC;
17460Sstevel@tonic-gate 				}
17470Sstevel@tonic-gate 				break;
17480Sstevel@tonic-gate 			case SDAUX_ID_PLT:
17490Sstevel@tonic-gate 				if (ofl->ofl_osplt) {
17500Sstevel@tonic-gate 					sym->st_value = ofl->
17510Sstevel@tonic-gate 					    ofl_osplt->os_shdr->sh_addr;
17520Sstevel@tonic-gate 					/* LINTED */
17530Sstevel@tonic-gate 					sectndx = elf_ndxscn(
17540Sstevel@tonic-gate 					    ofl->ofl_osplt->os_scn);
17550Sstevel@tonic-gate 					sdp->sd_flags &= ~FLG_SY_SPECSEC;
17560Sstevel@tonic-gate 				}
17570Sstevel@tonic-gate 				break;
17580Sstevel@tonic-gate 			case SDAUX_ID_GOT:
17590Sstevel@tonic-gate 				/*
17600Sstevel@tonic-gate 				 * Symbol bias for negative growing tables is
17610Sstevel@tonic-gate 				 * stored in symbol's value during
17620Sstevel@tonic-gate 				 * allocate_got().
17630Sstevel@tonic-gate 				 */
17640Sstevel@tonic-gate 				sym->st_value += ofl->
17650Sstevel@tonic-gate 				    ofl_osgot->os_shdr->sh_addr;
17660Sstevel@tonic-gate 				/* LINTED */
17670Sstevel@tonic-gate 				sectndx = elf_ndxscn(ofl->
17680Sstevel@tonic-gate 				    ofl_osgot->os_scn);
17690Sstevel@tonic-gate 				sdp->sd_flags &= ~FLG_SY_SPECSEC;
17700Sstevel@tonic-gate 				break;
17710Sstevel@tonic-gate 			default:
17720Sstevel@tonic-gate 				/* NOTHING */
17730Sstevel@tonic-gate 				;
17740Sstevel@tonic-gate 			}
17750Sstevel@tonic-gate 		}
17760Sstevel@tonic-gate 
17770Sstevel@tonic-gate 		/*
17780Sstevel@tonic-gate 		 * If a plt index has been assigned to an undefined function,
17790Sstevel@tonic-gate 		 * update the symbols value to the appropriate .plt address.
17800Sstevel@tonic-gate 		 */
17810Sstevel@tonic-gate 		if ((flags & FLG_OF_DYNAMIC) && (flags & FLG_OF_EXEC) &&
17820Sstevel@tonic-gate 		    (sdp->sd_file) &&
17830Sstevel@tonic-gate 		    (sdp->sd_file->ifl_ehdr->e_type == ET_DYN) &&
17840Sstevel@tonic-gate 		    (ELF_ST_TYPE(sym->st_info) == STT_FUNC) &&
17850Sstevel@tonic-gate 		    !(flags & FLG_OF_BFLAG)) {
17860Sstevel@tonic-gate 			if (sap->sa_PLTndx)
17876206Sab196087 				sym->st_value =
17886206Sab196087 				    (*ld_targ.t_mr.mr_calc_plt_addr)(sdp, ofl);
17890Sstevel@tonic-gate 		}
17900Sstevel@tonic-gate 
17910Sstevel@tonic-gate 		/*
17920Sstevel@tonic-gate 		 * Finish updating the symbols.
17930Sstevel@tonic-gate 		 */
17940Sstevel@tonic-gate 
17950Sstevel@tonic-gate 		/*
17960Sstevel@tonic-gate 		 * Sym Update: if scoped local - set local binding
17970Sstevel@tonic-gate 		 */
17980Sstevel@tonic-gate 		if (local)
17990Sstevel@tonic-gate 			sym->st_info = ELF_ST_INFO(STB_LOCAL,
18000Sstevel@tonic-gate 			    ELF_ST_TYPE(sym->st_info));
18010Sstevel@tonic-gate 
18020Sstevel@tonic-gate 		/*
18030Sstevel@tonic-gate 		 * Sym Updated: If both the .symtab and .dynsym
18040Sstevel@tonic-gate 		 * are present then we've actually updated the information in
18050Sstevel@tonic-gate 		 * the .dynsym, therefore copy this same information to the
18060Sstevel@tonic-gate 		 * .symtab entry.
18070Sstevel@tonic-gate 		 */
18080Sstevel@tonic-gate 		sdp->sd_shndx = sectndx;
18092766Sab196087 		if (enter_in_symtab && dynsym && (!local || dynlocal)) {
18102766Sab196087 			Word _symndx = dynlocal ? scopesym_ndx : symtab_ndx;
18112766Sab196087 
18122766Sab196087 			symtab[_symndx].st_value = sym->st_value;
18132766Sab196087 			symtab[_symndx].st_size = sym->st_size;
18142766Sab196087 			symtab[_symndx].st_info = sym->st_info;
18152766Sab196087 			symtab[_symndx].st_other = sym->st_other;
18160Sstevel@tonic-gate 		}
18170Sstevel@tonic-gate 
18180Sstevel@tonic-gate 		if (enter_in_symtab) {
18190Sstevel@tonic-gate 			Word	_symndx;
18200Sstevel@tonic-gate 
18210Sstevel@tonic-gate 			if (local)
18220Sstevel@tonic-gate 				_symndx = scopesym_ndx++;
18230Sstevel@tonic-gate 			else
18240Sstevel@tonic-gate 				_symndx = symtab_ndx++;
18250Sstevel@tonic-gate 			if (((sdp->sd_flags & FLG_SY_SPECSEC) == 0) &&
18260Sstevel@tonic-gate 			    (sectndx >= SHN_LORESERVE)) {
18279131SRod.Evans@Sun.COM 				assert(symshndx != NULL);
18280Sstevel@tonic-gate 				symshndx[_symndx] = sectndx;
18290Sstevel@tonic-gate 				symtab[_symndx].st_shndx = SHN_XINDEX;
18300Sstevel@tonic-gate 			} else {
18310Sstevel@tonic-gate 				/* LINTED */
18320Sstevel@tonic-gate 				symtab[_symndx].st_shndx = (Half)sectndx;
18330Sstevel@tonic-gate 			}
18340Sstevel@tonic-gate 		}
18350Sstevel@tonic-gate 
18362766Sab196087 		if (dynsym && (!local || dynlocal)) {
18372766Sab196087 			/*
18382766Sab196087 			 * dynsym and ldynsym are distinct tables, so
18392766Sab196087 			 * we use indirection to access the right one
18402766Sab196087 			 * and the related extended section index array.
18412766Sab196087 			 */
18422766Sab196087 			Word	_symndx;
18432766Sab196087 			Sym	*_dynsym;
18442766Sab196087 			Word	*_dynshndx;
18452766Sab196087 
18462766Sab196087 			if (!local) {
18472766Sab196087 				_symndx = dynsym_ndx++;
18482766Sab196087 				_dynsym = dynsym;
18492766Sab196087 				_dynshndx = dynshndx;
18502766Sab196087 			} else {
18512766Sab196087 				_symndx = ldynscopesym_ndx++;
18522766Sab196087 				_dynsym = ldynsym;
18532766Sab196087 				_dynshndx = ldynshndx;
18542766Sab196087 			}
18550Sstevel@tonic-gate 			if (((sdp->sd_flags & FLG_SY_SPECSEC) == 0) &&
18560Sstevel@tonic-gate 			    (sectndx >= SHN_LORESERVE)) {
18579131SRod.Evans@Sun.COM 				assert(_dynshndx != NULL);
18582766Sab196087 				_dynshndx[_symndx] = sectndx;
18592766Sab196087 				_dynsym[_symndx].st_shndx = SHN_XINDEX;
18600Sstevel@tonic-gate 			} else {
18610Sstevel@tonic-gate 				/* LINTED */
18622766Sab196087 				_dynsym[_symndx].st_shndx = (Half)sectndx;
18630Sstevel@tonic-gate 			}
18640Sstevel@tonic-gate 		}
18650Sstevel@tonic-gate 
18661618Srie 		DBG_CALL(Dbg_syms_new(ofl, sym, sdp));
18670Sstevel@tonic-gate 	}
18680Sstevel@tonic-gate 
18690Sstevel@tonic-gate 	/*
18700Sstevel@tonic-gate 	 * Now that all the symbols have been processed update any weak symbols
18710Sstevel@tonic-gate 	 * information (ie. copy all information except `st_name').  As both
18720Sstevel@tonic-gate 	 * symbols will be represented in the output, return the weak symbol to
18730Sstevel@tonic-gate 	 * its correct type.
18740Sstevel@tonic-gate 	 */
18759131SRod.Evans@Sun.COM 	for (ALIST_TRAVERSE(weak, idx1, wkp)) {
18769131SRod.Evans@Sun.COM 		Sym_desc	*sdp, *_sdp;
18779131SRod.Evans@Sun.COM 		Sym		*sym, *_sym, *__sym;
18783492Sab196087 		uchar_t		bind;
18790Sstevel@tonic-gate 
18800Sstevel@tonic-gate 		sdp = wkp->wk_weak;
18810Sstevel@tonic-gate 		_sdp = wkp->wk_alias;
18829313SAli.Bahrami@Sun.COM 		_sym = __sym = _sdp->sd_sym;
18830Sstevel@tonic-gate 
18840Sstevel@tonic-gate 		sdp->sd_flags |= FLG_SY_WEAKDEF;
18850Sstevel@tonic-gate 
18860Sstevel@tonic-gate 		/*
18870Sstevel@tonic-gate 		 * If the symbol definition has been scoped then assign it to
18880Sstevel@tonic-gate 		 * be local, otherwise if it's from a shared object then we need
18890Sstevel@tonic-gate 		 * to maintain the binding of the original reference.
18900Sstevel@tonic-gate 		 */
189111827SRod.Evans@Sun.COM 		if (SYM_IS_HIDDEN(sdp)) {
18920Sstevel@tonic-gate 			if (flags & FLG_OF_PROCRED)
18930Sstevel@tonic-gate 				bind = STB_LOCAL;
18940Sstevel@tonic-gate 			else
18950Sstevel@tonic-gate 				bind = STB_WEAK;
18960Sstevel@tonic-gate 		} else if ((sdp->sd_ref == REF_DYN_NEED) &&
18970Sstevel@tonic-gate 		    (sdp->sd_flags & FLG_SY_GLOBREF))
18980Sstevel@tonic-gate 			bind = STB_GLOBAL;
18990Sstevel@tonic-gate 		else
19000Sstevel@tonic-gate 			bind = STB_WEAK;
19010Sstevel@tonic-gate 
19021618Srie 		DBG_CALL(Dbg_syms_old(ofl, sdp));
19039313SAli.Bahrami@Sun.COM 		if ((sym = wkp->wk_symtab) != NULL) {
19040Sstevel@tonic-gate 			sym->st_value = _sym->st_value;
19050Sstevel@tonic-gate 			sym->st_size = _sym->st_size;
19060Sstevel@tonic-gate 			sym->st_other = _sym->st_other;
19070Sstevel@tonic-gate 			sym->st_shndx = _sym->st_shndx;
19080Sstevel@tonic-gate 			sym->st_info = ELF_ST_INFO(bind,
19090Sstevel@tonic-gate 			    ELF_ST_TYPE(sym->st_info));
19100Sstevel@tonic-gate 			__sym = sym;
19110Sstevel@tonic-gate 		}
19129313SAli.Bahrami@Sun.COM 		if ((sym = wkp->wk_dynsym) != NULL) {
19130Sstevel@tonic-gate 			sym->st_value = _sym->st_value;
19140Sstevel@tonic-gate 			sym->st_size = _sym->st_size;
19150Sstevel@tonic-gate 			sym->st_other = _sym->st_other;
19160Sstevel@tonic-gate 			sym->st_shndx = _sym->st_shndx;
19170Sstevel@tonic-gate 			sym->st_info = ELF_ST_INFO(bind,
19180Sstevel@tonic-gate 			    ELF_ST_TYPE(sym->st_info));
19190Sstevel@tonic-gate 			__sym = sym;
19200Sstevel@tonic-gate 		}
19211618Srie 		DBG_CALL(Dbg_syms_new(ofl, __sym, sdp));
19220Sstevel@tonic-gate 	}
19230Sstevel@tonic-gate 
19240Sstevel@tonic-gate 	/*
19251618Srie 	 * Now display GOT debugging information if required.
19260Sstevel@tonic-gate 	 */
19276206Sab196087 	DBG_CALL(Dbg_got_display(ofl, 0, 0,
19286206Sab196087 	    ld_targ.t_m.m_got_xnumber, ld_targ.t_m.m_got_entsize));
19290Sstevel@tonic-gate 
19300Sstevel@tonic-gate 	/*
19312766Sab196087 	 * Update the section headers information. sh_info is
19322766Sab196087 	 * supposed to contain the offset at which the first
19332766Sab196087 	 * global symbol resides in the symbol table, while
19342766Sab196087 	 * sh_link contains the section index of the associated
19352766Sab196087 	 * string table.
19360Sstevel@tonic-gate 	 */
19370Sstevel@tonic-gate 	if (symtab) {
19385220Srie 		Shdr	*shdr = ofl->ofl_ossymtab->os_shdr;
19390Sstevel@tonic-gate 
19408140SAli.Bahrami@Sun.COM 		shdr->sh_info = symtab_gbl_bndx;
19410Sstevel@tonic-gate 		/* LINTED */
19420Sstevel@tonic-gate 		shdr->sh_link = (Word)elf_ndxscn(ofl->ofl_osstrtab->os_scn);
194311227SAli.Bahrami@Sun.COM 		if (symshndx)
194411227SAli.Bahrami@Sun.COM 			ofl->ofl_ossymshndx->os_shdr->sh_link =
19454716Sab196087 			    (Word)elf_ndxscn(ofl->ofl_ossymtab->os_scn);
19468140SAli.Bahrami@Sun.COM 
19478140SAli.Bahrami@Sun.COM 		/*
19488140SAli.Bahrami@Sun.COM 		 * Ensure that the expected number of symbols
19498140SAli.Bahrami@Sun.COM 		 * were entered into the right spots:
19508140SAli.Bahrami@Sun.COM 		 *	- Scoped symbols in the right range
19518140SAli.Bahrami@Sun.COM 		 *	- Globals start at the right spot
19528140SAli.Bahrami@Sun.COM 		 *		(correct number of locals entered)
19538140SAli.Bahrami@Sun.COM 		 *	- The table is exactly filled
19548140SAli.Bahrami@Sun.COM 		 *		(correct number of globals entered)
19558140SAli.Bahrami@Sun.COM 		 */
19568140SAli.Bahrami@Sun.COM 		assert((scopesym_bndx + ofl->ofl_scopecnt) == scopesym_ndx);
195710792SRod.Evans@Sun.COM 		assert(shdr->sh_info == SYMTAB_LOC_CNT(ofl));
19588140SAli.Bahrami@Sun.COM 		assert((shdr->sh_info + ofl->ofl_globcnt) == symtab_ndx);
19590Sstevel@tonic-gate 	}
19600Sstevel@tonic-gate 	if (dynsym) {
19615220Srie 		Shdr	*shdr = ofl->ofl_osdynsym->os_shdr;
19620Sstevel@tonic-gate 
196310792SRod.Evans@Sun.COM 		shdr->sh_info = DYNSYM_LOC_CNT(ofl);
19640Sstevel@tonic-gate 		/* LINTED */
19650Sstevel@tonic-gate 		shdr->sh_link = (Word)elf_ndxscn(ofl->ofl_osdynstr->os_scn);
19660Sstevel@tonic-gate 
19670Sstevel@tonic-gate 		ofl->ofl_oshash->os_shdr->sh_link =
19680Sstevel@tonic-gate 		    /* LINTED */
19690Sstevel@tonic-gate 		    (Word)elf_ndxscn(ofl->ofl_osdynsym->os_scn);
19700Sstevel@tonic-gate 		if (dynshndx) {
19710Sstevel@tonic-gate 			shdr = ofl->ofl_osdynshndx->os_shdr;
19720Sstevel@tonic-gate 			shdr->sh_link =
19734716Sab196087 			    (Word)elf_ndxscn(ofl->ofl_osdynsym->os_scn);
19740Sstevel@tonic-gate 		}
19750Sstevel@tonic-gate 	}
19762766Sab196087 	if (ldynsym) {
19775220Srie 		Shdr	*shdr = ofl->ofl_osldynsym->os_shdr;
19782766Sab196087 
19792766Sab196087 		/* ldynsym has no globals, so give index one past the end */
19802766Sab196087 		shdr->sh_info = ldynsym_ndx;
19812766Sab196087 
19822766Sab196087 		/*
19832766Sab196087 		 * The ldynsym and dynsym must be adjacent. The
19842766Sab196087 		 * idea is that rtld should be able to start with
19852766Sab196087 		 * the ldynsym and march straight through the end
19862766Sab196087 		 * of dynsym, seeing them as a single symbol table,
19872766Sab196087 		 * despite the fact that they are in distinct sections.
19882766Sab196087 		 * Ensure that this happened correctly.
19892766Sab196087 		 *
19902766Sab196087 		 * Note that I use ldynsym_ndx here instead of the
19912766Sab196087 		 * computation I used to set the section size
19923492Sab196087 		 * (found in ldynsym_cnt). The two will agree, unless
19933492Sab196087 		 * we somehow miscounted symbols or failed to insert them
19943492Sab196087 		 * all. Using ldynsym_ndx here catches that error in
19953492Sab196087 		 * addition to checking for adjacency.
19962766Sab196087 		 */
19972766Sab196087 		assert(dynsym == (ldynsym + ldynsym_ndx));
19982766Sab196087 
19992766Sab196087 
20002766Sab196087 		/* LINTED */
20012766Sab196087 		shdr->sh_link = (Word)elf_ndxscn(ofl->ofl_osdynstr->os_scn);
20022766Sab196087 
20032766Sab196087 		if (ldynshndx) {
20042766Sab196087 			shdr = ofl->ofl_osldynshndx->os_shdr;
20052766Sab196087 			shdr->sh_link =
20064716Sab196087 			    (Word)elf_ndxscn(ofl->ofl_osldynsym->os_scn);
20072766Sab196087 		}
20083492Sab196087 
20093492Sab196087 		/*
20103492Sab196087 		 * The presence of .SUNW_ldynsym means that there may be
20113492Sab196087 		 * associated sort sections, one for regular symbols
20123492Sab196087 		 * and the other for TLS. Each sort section needs the
20133492Sab196087 		 * following done:
20143492Sab196087 		 *	- Section header link references .SUNW_ldynsym
20153492Sab196087 		 *	- Should have received the expected # of items
20163492Sab196087 		 *	- Sorted by increasing address
20173492Sab196087 		 */
20183492Sab196087 		if (ofl->ofl_osdynsymsort) {	/* .SUNW_dynsymsort */
20193492Sab196087 			ofl->ofl_osdynsymsort->os_shdr->sh_link =
20203492Sab196087 			    (Word)elf_ndxscn(ofl->ofl_osldynsym->os_scn);
20213492Sab196087 			assert(ofl->ofl_dynsymsortcnt == dynsymsort_ndx);
20225220Srie 
20233492Sab196087 			if (dynsymsort_ndx > 1) {
20243492Sab196087 				dynsort_compare_syms = ldynsym;
20253492Sab196087 				qsort(dynsymsort, dynsymsort_ndx,
20263492Sab196087 				    sizeof (*dynsymsort), dynsort_compare);
20275549Srie 				dynsort_dupwarn(ofl, ldynsym,
20285549Srie 				    st_getstrbuf(dynstr),
20293492Sab196087 				    dynsymsort, dynsymsort_ndx,
20303492Sab196087 				    MSG_ORIG(MSG_SCN_DYNSYMSORT));
20313492Sab196087 			}
20323492Sab196087 		}
20333492Sab196087 		if (ofl->ofl_osdyntlssort) {	/* .SUNW_dyntlssort */
20343492Sab196087 			ofl->ofl_osdyntlssort->os_shdr->sh_link =
20353492Sab196087 			    (Word)elf_ndxscn(ofl->ofl_osldynsym->os_scn);
20363492Sab196087 			assert(ofl->ofl_dyntlssortcnt == dyntlssort_ndx);
20375220Srie 
20383492Sab196087 			if (dyntlssort_ndx > 1) {
20393492Sab196087 				dynsort_compare_syms = ldynsym;
20403492Sab196087 				qsort(dyntlssort, dyntlssort_ndx,
20413492Sab196087 				    sizeof (*dyntlssort), dynsort_compare);
20425549Srie 				dynsort_dupwarn(ofl, ldynsym,
20435549Srie 				    st_getstrbuf(dynstr),
20443492Sab196087 				    dyntlssort, dyntlssort_ndx,
20453492Sab196087 				    MSG_ORIG(MSG_SCN_DYNTLSSORT));
20463492Sab196087 			}
20473492Sab196087 		}
20482766Sab196087 	}
20490Sstevel@tonic-gate 
20500Sstevel@tonic-gate 	/*
20510Sstevel@tonic-gate 	 * Used by ld.so.1 only.
20520Sstevel@tonic-gate 	 */
20530Sstevel@tonic-gate 	return (etext);
20543492Sab196087 
20553492Sab196087 #undef ADD_TO_DYNSORT
20560Sstevel@tonic-gate }
20570Sstevel@tonic-gate 
20580Sstevel@tonic-gate /*
20590Sstevel@tonic-gate  * Build the dynamic section.
20606299Sab196087  *
20616299Sab196087  * This routine must be maintained in parallel with make_dynamic()
20626299Sab196087  * in sections.c
20630Sstevel@tonic-gate  */
20641618Srie static int
update_odynamic(Ofl_desc * ofl)20650Sstevel@tonic-gate update_odynamic(Ofl_desc *ofl)
20660Sstevel@tonic-gate {
20679131SRod.Evans@Sun.COM 	Aliste		idx;
20680Sstevel@tonic-gate 	Ifl_desc	*ifl;
20690Sstevel@tonic-gate 	Sym_desc	*sdp;
20700Sstevel@tonic-gate 	Shdr		*shdr;
20710Sstevel@tonic-gate 	Dyn		*_dyn = (Dyn *)ofl->ofl_osdynamic->os_outdata->d_buf;
20720Sstevel@tonic-gate 	Dyn		*dyn;
207310792SRod.Evans@Sun.COM 	Os_desc		*symosp, *strosp;
207410792SRod.Evans@Sun.COM 	Str_tbl		*strtbl;
20755892Sab196087 	size_t		stoff;
20766299Sab196087 	ofl_flag_t	flags = ofl->ofl_flags;
20776299Sab196087 	int		not_relobj = !(flags & FLG_OF_RELOBJ);
20783850Sab196087 	Word		cnt;
20790Sstevel@tonic-gate 
20806299Sab196087 	/*
208110792SRod.Evans@Sun.COM 	 * Relocatable objects can be built with -r and -dy to trigger the
208210792SRod.Evans@Sun.COM 	 * creation of a .dynamic section.  This model is used to create kernel
208310792SRod.Evans@Sun.COM 	 * device drivers.  The .dynamic section provides a subset of userland
208410792SRod.Evans@Sun.COM 	 * .dynamic entries, typically entries such as DT_NEEDED and DT_RUNPATH.
20856299Sab196087 	 *
208610792SRod.Evans@Sun.COM 	 * Within a dynamic object, any .dynamic string references are to the
208710792SRod.Evans@Sun.COM 	 * .dynstr table.  Within a relocatable object, these strings can reside
208810792SRod.Evans@Sun.COM 	 * within the .strtab.
20896299Sab196087 	 */
209010792SRod.Evans@Sun.COM 	if (OFL_IS_STATIC_OBJ(ofl)) {
209110792SRod.Evans@Sun.COM 		symosp = ofl->ofl_ossymtab;
209210792SRod.Evans@Sun.COM 		strosp = ofl->ofl_osstrtab;
209310792SRod.Evans@Sun.COM 		strtbl = ofl->ofl_strtab;
209410792SRod.Evans@Sun.COM 	} else {
209510792SRod.Evans@Sun.COM 		symosp = ofl->ofl_osdynsym;
209610792SRod.Evans@Sun.COM 		strosp = ofl->ofl_osdynstr;
209710792SRod.Evans@Sun.COM 		strtbl = ofl->ofl_dynstrtab;
209810792SRod.Evans@Sun.COM 	}
209910792SRod.Evans@Sun.COM 
210010792SRod.Evans@Sun.COM 	/* LINTED */
210110792SRod.Evans@Sun.COM 	ofl->ofl_osdynamic->os_shdr->sh_link = (Word)elf_ndxscn(strosp->os_scn);
21020Sstevel@tonic-gate 
21030Sstevel@tonic-gate 	dyn = _dyn;
21040Sstevel@tonic-gate 
21059131SRod.Evans@Sun.COM 	for (APLIST_TRAVERSE(ofl->ofl_sos, idx, ifl)) {
21060Sstevel@tonic-gate 		if ((ifl->ifl_flags &
21070Sstevel@tonic-gate 		    (FLG_IF_IGNORE | FLG_IF_DEPREQD)) == FLG_IF_IGNORE)
21080Sstevel@tonic-gate 			continue;
21090Sstevel@tonic-gate 
21100Sstevel@tonic-gate 		/*
21110Sstevel@tonic-gate 		 * Create and set up the DT_POSFLAG_1 entry here if required.
21120Sstevel@tonic-gate 		 */
211312449SRod.Evans@Sun.COM 		if ((ifl->ifl_flags & MSK_IF_POSFLAG1) &&
211412449SRod.Evans@Sun.COM 		    (ifl->ifl_flags & FLG_IF_NEEDED) && not_relobj) {
21150Sstevel@tonic-gate 			dyn->d_tag = DT_POSFLAG_1;
21160Sstevel@tonic-gate 			if (ifl->ifl_flags & FLG_IF_LAZYLD)
21170Sstevel@tonic-gate 				dyn->d_un.d_val = DF_P1_LAZYLOAD;
21180Sstevel@tonic-gate 			if (ifl->ifl_flags & FLG_IF_GRPPRM)
21190Sstevel@tonic-gate 				dyn->d_un.d_val |= DF_P1_GROUPPERM;
212012449SRod.Evans@Sun.COM 			if (ifl->ifl_flags & FLG_IF_DEFERRED)
212112449SRod.Evans@Sun.COM 				dyn->d_un.d_val |= DF_P1_DEFERRED;
21220Sstevel@tonic-gate 			dyn++;
21230Sstevel@tonic-gate 		}
21240Sstevel@tonic-gate 
21250Sstevel@tonic-gate 		if (ifl->ifl_flags & (FLG_IF_NEEDED | FLG_IF_NEEDSTR))
21260Sstevel@tonic-gate 			dyn->d_tag = DT_NEEDED;
21270Sstevel@tonic-gate 		else
21280Sstevel@tonic-gate 			continue;
21290Sstevel@tonic-gate 
213010792SRod.Evans@Sun.COM 		(void) st_setstring(strtbl, ifl->ifl_soname, &stoff);
21310Sstevel@tonic-gate 		dyn->d_un.d_val = stoff;
21320Sstevel@tonic-gate 		/* LINTED */
21330Sstevel@tonic-gate 		ifl->ifl_neededndx = (Half)(((uintptr_t)dyn - (uintptr_t)_dyn) /
21340Sstevel@tonic-gate 		    sizeof (Dyn));
21350Sstevel@tonic-gate 		dyn++;
21360Sstevel@tonic-gate 	}
21370Sstevel@tonic-gate 
21386299Sab196087 	if (not_relobj) {
21396299Sab196087 		if (ofl->ofl_dtsfltrs != NULL) {
21406299Sab196087 			Dfltr_desc	*dftp;
21416299Sab196087 
21426299Sab196087 			for (ALIST_TRAVERSE(ofl->ofl_dtsfltrs, idx, dftp)) {
21436299Sab196087 				if (dftp->dft_flag == FLG_SY_AUXFLTR)
21446299Sab196087 					dyn->d_tag = DT_SUNW_AUXILIARY;
21456299Sab196087 				else
21466299Sab196087 					dyn->d_tag = DT_SUNW_FILTER;
21476299Sab196087 
214810792SRod.Evans@Sun.COM 				(void) st_setstring(strtbl, dftp->dft_str,
21496299Sab196087 				    &stoff);
21506299Sab196087 				dyn->d_un.d_val = stoff;
21516299Sab196087 				dftp->dft_ndx = (Half)(((uintptr_t)dyn -
21526299Sab196087 				    (uintptr_t)_dyn) / sizeof (Dyn));
21536299Sab196087 				dyn++;
21546299Sab196087 			}
21556299Sab196087 		}
21566299Sab196087 		if (((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_INIT_U),
21576299Sab196087 		    SYM_NOHASH, 0, ofl)) != NULL) &&
21586299Sab196087 		    (sdp->sd_ref == REF_REL_NEED) &&
21596299Sab196087 		    (sdp->sd_sym->st_shndx != SHN_UNDEF)) {
21606299Sab196087 			dyn->d_tag = DT_INIT;
21616299Sab196087 			dyn->d_un.d_ptr = sdp->sd_sym->st_value;
21626299Sab196087 			dyn++;
21636299Sab196087 		}
21646299Sab196087 		if (((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_FINI_U),
21656299Sab196087 		    SYM_NOHASH, 0, ofl)) != NULL) &&
21666299Sab196087 		    (sdp->sd_ref == REF_REL_NEED) &&
21676299Sab196087 		    (sdp->sd_sym->st_shndx != SHN_UNDEF)) {
21686299Sab196087 			dyn->d_tag = DT_FINI;
21696299Sab196087 			dyn->d_un.d_ptr = sdp->sd_sym->st_value;
21706299Sab196087 			dyn++;
21716299Sab196087 		}
21726299Sab196087 		if (ofl->ofl_soname) {
21736299Sab196087 			dyn->d_tag = DT_SONAME;
217410792SRod.Evans@Sun.COM 			(void) st_setstring(strtbl, ofl->ofl_soname, &stoff);
21750Sstevel@tonic-gate 			dyn->d_un.d_val = stoff;
21766299Sab196087 			dyn++;
21776299Sab196087 		}
21786299Sab196087 		if (ofl->ofl_filtees) {
21796299Sab196087 			if (flags & FLG_OF_AUX) {
21806299Sab196087 				dyn->d_tag = DT_AUXILIARY;
21816299Sab196087 			} else {
21826299Sab196087 				dyn->d_tag = DT_FILTER;
21836299Sab196087 			}
218410792SRod.Evans@Sun.COM 			(void) st_setstring(strtbl, ofl->ofl_filtees, &stoff);
21856299Sab196087 			dyn->d_un.d_val = stoff;
21860Sstevel@tonic-gate 			dyn++;
21870Sstevel@tonic-gate 		}
21880Sstevel@tonic-gate 	}
21896299Sab196087 
21900Sstevel@tonic-gate 	if (ofl->ofl_rpath) {
219110792SRod.Evans@Sun.COM 		(void) st_setstring(strtbl, ofl->ofl_rpath, &stoff);
21920Sstevel@tonic-gate 		dyn->d_tag = DT_RUNPATH;
21930Sstevel@tonic-gate 		dyn->d_un.d_val = stoff;
21940Sstevel@tonic-gate 		dyn++;
21950Sstevel@tonic-gate 		dyn->d_tag = DT_RPATH;
21960Sstevel@tonic-gate 		dyn->d_un.d_val = stoff;
21970Sstevel@tonic-gate 		dyn++;
21980Sstevel@tonic-gate 	}
21996299Sab196087 
22006299Sab196087 	if (not_relobj) {
22019131SRod.Evans@Sun.COM 		Aliste	idx;
220212992SRod.Evans@Oracle.COM 		Sg_desc	*sgp;
22039131SRod.Evans@Sun.COM 
22046299Sab196087 		if (ofl->ofl_config) {
22056299Sab196087 			dyn->d_tag = DT_CONFIG;
220610792SRod.Evans@Sun.COM 			(void) st_setstring(strtbl, ofl->ofl_config, &stoff);
22076299Sab196087 			dyn->d_un.d_val = stoff;
22086299Sab196087 			dyn++;
22096299Sab196087 		}
22106299Sab196087 		if (ofl->ofl_depaudit) {
22116299Sab196087 			dyn->d_tag = DT_DEPAUDIT;
221210792SRod.Evans@Sun.COM 			(void) st_setstring(strtbl, ofl->ofl_depaudit, &stoff);
22136299Sab196087 			dyn->d_un.d_val = stoff;
22146299Sab196087 			dyn++;
22156299Sab196087 		}
22166299Sab196087 		if (ofl->ofl_audit) {
22176299Sab196087 			dyn->d_tag = DT_AUDIT;
221810792SRod.Evans@Sun.COM 			(void) st_setstring(strtbl, ofl->ofl_audit, &stoff);
22196299Sab196087 			dyn->d_un.d_val = stoff;
22206299Sab196087 			dyn++;
22216299Sab196087 		}
22220Sstevel@tonic-gate 
22230Sstevel@tonic-gate 		dyn->d_tag = DT_HASH;
22240Sstevel@tonic-gate 		dyn->d_un.d_ptr = ofl->ofl_oshash->os_shdr->sh_addr;
22250Sstevel@tonic-gate 		dyn++;
22260Sstevel@tonic-gate 
222710792SRod.Evans@Sun.COM 		shdr = strosp->os_shdr;
22280Sstevel@tonic-gate 		dyn->d_tag = DT_STRTAB;
22290Sstevel@tonic-gate 		dyn->d_un.d_ptr = shdr->sh_addr;
22300Sstevel@tonic-gate 		dyn++;
22310Sstevel@tonic-gate 
22320Sstevel@tonic-gate 		dyn->d_tag = DT_STRSZ;
22330Sstevel@tonic-gate 		dyn->d_un.d_ptr = shdr->sh_size;
22340Sstevel@tonic-gate 		dyn++;
22350Sstevel@tonic-gate 
223610792SRod.Evans@Sun.COM 		/*
223710792SRod.Evans@Sun.COM 		 * Note, the shdr is set and used in the ofl->ofl_osldynsym case
223810792SRod.Evans@Sun.COM 		 * that follows.
223910792SRod.Evans@Sun.COM 		 */
224010792SRod.Evans@Sun.COM 		shdr = symosp->os_shdr;
22410Sstevel@tonic-gate 		dyn->d_tag = DT_SYMTAB;
22420Sstevel@tonic-gate 		dyn->d_un.d_ptr = shdr->sh_addr;
22430Sstevel@tonic-gate 		dyn++;
22440Sstevel@tonic-gate 
22450Sstevel@tonic-gate 		dyn->d_tag = DT_SYMENT;
22460Sstevel@tonic-gate 		dyn->d_un.d_ptr = shdr->sh_entsize;
22470Sstevel@tonic-gate 		dyn++;
22480Sstevel@tonic-gate 
22492766Sab196087 		if (ofl->ofl_osldynsym) {
225010792SRod.Evans@Sun.COM 			Shdr	*lshdr = ofl->ofl_osldynsym->os_shdr;
225110792SRod.Evans@Sun.COM 
22522766Sab196087 			/*
22532766Sab196087 			 * We have arranged for the .SUNW_ldynsym data to be
22542766Sab196087 			 * immediately in front of the .dynsym data.
22552766Sab196087 			 * This means that you could start at the top
22562766Sab196087 			 * of .SUNW_ldynsym and see the data for both tables
22572766Sab196087 			 * without a break. This is the view we want to
22582766Sab196087 			 * provide for DT_SUNW_SYMTAB, which is why we
22592766Sab196087 			 * add the lengths together.
22602766Sab196087 			 */
22612766Sab196087 			dyn->d_tag = DT_SUNW_SYMTAB;
22622766Sab196087 			dyn->d_un.d_ptr = lshdr->sh_addr;
22632766Sab196087 			dyn++;
22642766Sab196087 
22652766Sab196087 			dyn->d_tag = DT_SUNW_SYMSZ;
22662766Sab196087 			dyn->d_un.d_val = lshdr->sh_size + shdr->sh_size;
22672766Sab196087 			dyn++;
22682766Sab196087 		}
22692766Sab196087 
22703492Sab196087 		if (ofl->ofl_osdynsymsort || ofl->ofl_osdyntlssort) {
22713492Sab196087 			dyn->d_tag = DT_SUNW_SORTENT;
22723492Sab196087 			dyn->d_un.d_val = sizeof (Word);
22733492Sab196087 			dyn++;
22743492Sab196087 		}
22753492Sab196087 
22763492Sab196087 		if (ofl->ofl_osdynsymsort) {
227710792SRod.Evans@Sun.COM 			shdr = ofl->ofl_osdynsymsort->os_shdr;
227810792SRod.Evans@Sun.COM 
22793492Sab196087 			dyn->d_tag = DT_SUNW_SYMSORT;
228010792SRod.Evans@Sun.COM 			dyn->d_un.d_ptr = shdr->sh_addr;
22813492Sab196087 			dyn++;
22823492Sab196087 
22833492Sab196087 			dyn->d_tag = DT_SUNW_SYMSORTSZ;
228410792SRod.Evans@Sun.COM 			dyn->d_un.d_val = shdr->sh_size;
22853492Sab196087 			dyn++;
22863492Sab196087 		}
22873492Sab196087 
22883492Sab196087 		if (ofl->ofl_osdyntlssort) {
228910792SRod.Evans@Sun.COM 			shdr = ofl->ofl_osdyntlssort->os_shdr;
229010792SRod.Evans@Sun.COM 
22913492Sab196087 			dyn->d_tag = DT_SUNW_TLSSORT;
229210792SRod.Evans@Sun.COM 			dyn->d_un.d_ptr = shdr->sh_addr;
22933492Sab196087 			dyn++;
22943492Sab196087 
22953492Sab196087 			dyn->d_tag = DT_SUNW_TLSSORTSZ;
229610792SRod.Evans@Sun.COM 			dyn->d_un.d_val = shdr->sh_size;
22973492Sab196087 			dyn++;
22983492Sab196087 		}
22993492Sab196087 
23000Sstevel@tonic-gate 		/*
23010Sstevel@tonic-gate 		 * Reserve the DT_CHECKSUM entry.  Its value will be filled in
23020Sstevel@tonic-gate 		 * after the complete image is built.
23030Sstevel@tonic-gate 		 */
23040Sstevel@tonic-gate 		dyn->d_tag = DT_CHECKSUM;
23050Sstevel@tonic-gate 		ofl->ofl_checksum = &dyn->d_un.d_val;
23060Sstevel@tonic-gate 		dyn++;
23070Sstevel@tonic-gate 
23084716Sab196087 		/*
23094716Sab196087 		 * Versioning sections: DT_VERDEF and DT_VERNEED.
23104716Sab196087 		 *
23114716Sab196087 		 * The Solaris ld does not produce DT_VERSYM, but the GNU ld
23124716Sab196087 		 * does, in order to support their style of versioning, which
23134716Sab196087 		 * differs from ours:
23144716Sab196087 		 *
23154716Sab196087 		 *	- The top bit of the 16-bit Versym index is
23164716Sab196087 		 *		not part of the version, but is interpreted
23174716Sab196087 		 *		as a "hidden bit".
23184716Sab196087 		 *
23194716Sab196087 		 *	- External (SHN_UNDEF) symbols can have non-zero
23204716Sab196087 		 *		Versym values, which specify versions in
23214716Sab196087 		 *		referenced objects, via the Verneed section.
23224716Sab196087 		 *
23234716Sab196087 		 *	- The vna_other field of the Vernaux structures
23244716Sab196087 		 *		found in the Verneed section are not zero as
23254716Sab196087 		 *		with Solaris, but instead contain the version
23264716Sab196087 		 *		index to be used by Versym indices to reference
23274716Sab196087 		 *		the given external version.
23284716Sab196087 		 *
23294716Sab196087 		 * The Solaris ld, rtld, and elfdump programs all interpret the
23304716Sab196087 		 * presence of DT_VERSYM as meaning that GNU versioning rules
23314716Sab196087 		 * apply to the given file. If DT_VERSYM is not present,
23324716Sab196087 		 * then Solaris versioning rules apply. If we should ever need
23334716Sab196087 		 * to change our ld so that it does issue DT_VERSYM, then
23344716Sab196087 		 * this rule for detecting GNU versioning will no longer work.
23354716Sab196087 		 * In that case, we will have to invent a way to explicitly
23364716Sab196087 		 * specify the style of versioning in use, perhaps via a
23374716Sab196087 		 * new dynamic entry named something like DT_SUNW_VERSIONSTYLE,
23384716Sab196087 		 * where the d_un.d_val value specifies which style is to be
23394716Sab196087 		 * used.
23404716Sab196087 		 */
23410Sstevel@tonic-gate 		if ((flags & (FLG_OF_VERDEF | FLG_OF_NOVERSEC)) ==
23420Sstevel@tonic-gate 		    FLG_OF_VERDEF) {
23430Sstevel@tonic-gate 			shdr = ofl->ofl_osverdef->os_shdr;
234410792SRod.Evans@Sun.COM 
23450Sstevel@tonic-gate 			dyn->d_tag = DT_VERDEF;
23460Sstevel@tonic-gate 			dyn->d_un.d_ptr = shdr->sh_addr;
23470Sstevel@tonic-gate 			dyn++;
23480Sstevel@tonic-gate 			dyn->d_tag = DT_VERDEFNUM;
23490Sstevel@tonic-gate 			dyn->d_un.d_ptr = shdr->sh_info;
23500Sstevel@tonic-gate 			dyn++;
23510Sstevel@tonic-gate 		}
23520Sstevel@tonic-gate 		if ((flags & (FLG_OF_VERNEED | FLG_OF_NOVERSEC)) ==
23530Sstevel@tonic-gate 		    FLG_OF_VERNEED) {
23540Sstevel@tonic-gate 			shdr = ofl->ofl_osverneed->os_shdr;
235510792SRod.Evans@Sun.COM 
23560Sstevel@tonic-gate 			dyn->d_tag = DT_VERNEED;
23570Sstevel@tonic-gate 			dyn->d_un.d_ptr = shdr->sh_addr;
23580Sstevel@tonic-gate 			dyn++;
23590Sstevel@tonic-gate 			dyn->d_tag = DT_VERNEEDNUM;
23600Sstevel@tonic-gate 			dyn->d_un.d_ptr = shdr->sh_info;
23610Sstevel@tonic-gate 			dyn++;
23620Sstevel@tonic-gate 		}
23634716Sab196087 
23646299Sab196087 		if ((flags & FLG_OF_COMREL) && ofl->ofl_relocrelcnt) {
23656206Sab196087 			dyn->d_tag = ld_targ.t_m.m_rel_dt_count;
23660Sstevel@tonic-gate 			dyn->d_un.d_val = ofl->ofl_relocrelcnt;
23670Sstevel@tonic-gate 			dyn++;
23680Sstevel@tonic-gate 		}
23690Sstevel@tonic-gate 		if (flags & FLG_OF_TEXTREL) {
23700Sstevel@tonic-gate 			/*
23710Sstevel@tonic-gate 			 * Only the presence of this entry is used in this
23720Sstevel@tonic-gate 			 * implementation, not the value stored.
23730Sstevel@tonic-gate 			 */
23740Sstevel@tonic-gate 			dyn->d_tag = DT_TEXTREL;
23750Sstevel@tonic-gate 			dyn->d_un.d_val = 0;
23760Sstevel@tonic-gate 			dyn++;
23770Sstevel@tonic-gate 		}
23780Sstevel@tonic-gate 
23790Sstevel@tonic-gate 		if (ofl->ofl_osfiniarray) {
23800Sstevel@tonic-gate 			shdr = ofl->ofl_osfiniarray->os_shdr;
23810Sstevel@tonic-gate 
23820Sstevel@tonic-gate 			dyn->d_tag = DT_FINI_ARRAY;
23830Sstevel@tonic-gate 			dyn->d_un.d_ptr = shdr->sh_addr;
23840Sstevel@tonic-gate 			dyn++;
23850Sstevel@tonic-gate 
23860Sstevel@tonic-gate 			dyn->d_tag = DT_FINI_ARRAYSZ;
23870Sstevel@tonic-gate 			dyn->d_un.d_val = shdr->sh_size;
23880Sstevel@tonic-gate 			dyn++;
23890Sstevel@tonic-gate 		}
23900Sstevel@tonic-gate 
23910Sstevel@tonic-gate 		if (ofl->ofl_osinitarray) {
23920Sstevel@tonic-gate 			shdr = ofl->ofl_osinitarray->os_shdr;
23930Sstevel@tonic-gate 
23940Sstevel@tonic-gate 			dyn->d_tag = DT_INIT_ARRAY;
23950Sstevel@tonic-gate 			dyn->d_un.d_ptr = shdr->sh_addr;
23960Sstevel@tonic-gate 			dyn++;
23970Sstevel@tonic-gate 
23980Sstevel@tonic-gate 			dyn->d_tag = DT_INIT_ARRAYSZ;
23990Sstevel@tonic-gate 			dyn->d_un.d_val = shdr->sh_size;
24000Sstevel@tonic-gate 			dyn++;
24010Sstevel@tonic-gate 		}
24020Sstevel@tonic-gate 
24030Sstevel@tonic-gate 		if (ofl->ofl_ospreinitarray) {
24040Sstevel@tonic-gate 			shdr = ofl->ofl_ospreinitarray->os_shdr;
24050Sstevel@tonic-gate 
24060Sstevel@tonic-gate 			dyn->d_tag = DT_PREINIT_ARRAY;
24070Sstevel@tonic-gate 			dyn->d_un.d_ptr = shdr->sh_addr;
24080Sstevel@tonic-gate 			dyn++;
24090Sstevel@tonic-gate 
24100Sstevel@tonic-gate 			dyn->d_tag = DT_PREINIT_ARRAYSZ;
24110Sstevel@tonic-gate 			dyn->d_un.d_val = shdr->sh_size;
24120Sstevel@tonic-gate 			dyn++;
24130Sstevel@tonic-gate 		}
24140Sstevel@tonic-gate 
24150Sstevel@tonic-gate 		if (ofl->ofl_pltcnt) {
241610792SRod.Evans@Sun.COM 			shdr = ofl->ofl_osplt->os_relosdesc->os_shdr;
24170Sstevel@tonic-gate 
24180Sstevel@tonic-gate 			dyn->d_tag = DT_PLTRELSZ;
24190Sstevel@tonic-gate 			dyn->d_un.d_ptr = shdr->sh_size;
24200Sstevel@tonic-gate 			dyn++;
24210Sstevel@tonic-gate 			dyn->d_tag = DT_PLTREL;
24226206Sab196087 			dyn->d_un.d_ptr = ld_targ.t_m.m_rel_dt_type;
24230Sstevel@tonic-gate 			dyn++;
24240Sstevel@tonic-gate 			dyn->d_tag = DT_JMPREL;
24250Sstevel@tonic-gate 			dyn->d_un.d_ptr = shdr->sh_addr;
24260Sstevel@tonic-gate 			dyn++;
24270Sstevel@tonic-gate 		}
24280Sstevel@tonic-gate 		if (ofl->ofl_pltpad) {
242910792SRod.Evans@Sun.COM 			shdr = ofl->ofl_osplt->os_shdr;
24300Sstevel@tonic-gate 
24310Sstevel@tonic-gate 			dyn->d_tag = DT_PLTPAD;
24325220Srie 			if (ofl->ofl_pltcnt) {
24330Sstevel@tonic-gate 				dyn->d_un.d_ptr = shdr->sh_addr +
24346206Sab196087 				    ld_targ.t_m.m_plt_reservsz +
24356206Sab196087 				    ofl->ofl_pltcnt * ld_targ.t_m.m_plt_entsize;
24365220Srie 			} else
24370Sstevel@tonic-gate 				dyn->d_un.d_ptr = shdr->sh_addr;
24380Sstevel@tonic-gate 			dyn++;
24390Sstevel@tonic-gate 			dyn->d_tag = DT_PLTPADSZ;
24406206Sab196087 			dyn->d_un.d_val = ofl->ofl_pltpad *
24416206Sab196087 			    ld_targ.t_m.m_plt_entsize;
24420Sstevel@tonic-gate 			dyn++;
24430Sstevel@tonic-gate 		}
24440Sstevel@tonic-gate 		if (ofl->ofl_relocsz) {
244510792SRod.Evans@Sun.COM 			shdr = ofl->ofl_osrelhead->os_shdr;
244610792SRod.Evans@Sun.COM 
24476206Sab196087 			dyn->d_tag = ld_targ.t_m.m_rel_dt_type;
244810792SRod.Evans@Sun.COM 			dyn->d_un.d_ptr = shdr->sh_addr;
24490Sstevel@tonic-gate 			dyn++;
24506206Sab196087 			dyn->d_tag = ld_targ.t_m.m_rel_dt_size;
24510Sstevel@tonic-gate 			dyn->d_un.d_ptr = ofl->ofl_relocsz;
24520Sstevel@tonic-gate 			dyn++;
24536206Sab196087 			dyn->d_tag = ld_targ.t_m.m_rel_dt_ent;
245410792SRod.Evans@Sun.COM 			if (shdr->sh_type == SHT_REL)
24550Sstevel@tonic-gate 				dyn->d_un.d_ptr = sizeof (Rel);
24560Sstevel@tonic-gate 			else
24570Sstevel@tonic-gate 				dyn->d_un.d_ptr = sizeof (Rela);
24580Sstevel@tonic-gate 			dyn++;
24590Sstevel@tonic-gate 		}
24600Sstevel@tonic-gate 		if (ofl->ofl_ossyminfo) {
24610Sstevel@tonic-gate 			shdr = ofl->ofl_ossyminfo->os_shdr;
246210792SRod.Evans@Sun.COM 
24630Sstevel@tonic-gate 			dyn->d_tag = DT_SYMINFO;
24640Sstevel@tonic-gate 			dyn->d_un.d_ptr = shdr->sh_addr;
24650Sstevel@tonic-gate 			dyn++;
24660Sstevel@tonic-gate 			dyn->d_tag = DT_SYMINSZ;
24670Sstevel@tonic-gate 			dyn->d_un.d_val = shdr->sh_size;
24680Sstevel@tonic-gate 			dyn++;
24690Sstevel@tonic-gate 			dyn->d_tag = DT_SYMINENT;
24700Sstevel@tonic-gate 			dyn->d_un.d_val = sizeof (Syminfo);
24710Sstevel@tonic-gate 			dyn++;
24720Sstevel@tonic-gate 		}
24730Sstevel@tonic-gate 		if (ofl->ofl_osmove) {
247410792SRod.Evans@Sun.COM 			shdr = ofl->ofl_osmove->os_shdr;
24750Sstevel@tonic-gate 
247611827SRod.Evans@Sun.COM 			dyn->d_tag = DT_MOVETAB;
247711827SRod.Evans@Sun.COM 			dyn->d_un.d_val = shdr->sh_addr;
24780Sstevel@tonic-gate 			dyn++;
24790Sstevel@tonic-gate 			dyn->d_tag = DT_MOVESZ;
248010792SRod.Evans@Sun.COM 			dyn->d_un.d_val = shdr->sh_size;
24810Sstevel@tonic-gate 			dyn++;
248211827SRod.Evans@Sun.COM 			dyn->d_tag = DT_MOVEENT;
248311827SRod.Evans@Sun.COM 			dyn->d_un.d_val = shdr->sh_entsize;
24840Sstevel@tonic-gate 			dyn++;
24850Sstevel@tonic-gate 		}
24860Sstevel@tonic-gate 		if (ofl->ofl_regsymcnt) {
24870Sstevel@tonic-gate 			int	ndx;
24880Sstevel@tonic-gate 
24890Sstevel@tonic-gate 			for (ndx = 0; ndx < ofl->ofl_regsymsno; ndx++) {
24909131SRod.Evans@Sun.COM 				if ((sdp = ofl->ofl_regsyms[ndx]) == NULL)
24910Sstevel@tonic-gate 					continue;
24920Sstevel@tonic-gate 
24936206Sab196087 				dyn->d_tag = ld_targ.t_m.m_dt_register;
24940Sstevel@tonic-gate 				dyn->d_un.d_val = sdp->sd_symndx;
24950Sstevel@tonic-gate 				dyn++;
24960Sstevel@tonic-gate 			}
24970Sstevel@tonic-gate 		}
24980Sstevel@tonic-gate 
24999131SRod.Evans@Sun.COM 		for (APLIST_TRAVERSE(ofl->ofl_rtldinfo, idx, sdp)) {
25000Sstevel@tonic-gate 			dyn->d_tag = DT_SUNW_RTLDINF;
25010Sstevel@tonic-gate 			dyn->d_un.d_ptr = sdp->sd_sym->st_value;
25020Sstevel@tonic-gate 			dyn++;
25030Sstevel@tonic-gate 		}
25040Sstevel@tonic-gate 
250512992SRod.Evans@Oracle.COM 		if (((sgp = ofl->ofl_osdynamic->os_sgdesc) != NULL) &&
250612992SRod.Evans@Oracle.COM 		    (sgp->sg_phdr.p_flags & PF_W) && ofl->ofl_osinterp) {
250712992SRod.Evans@Oracle.COM 			dyn->d_tag = DT_DEBUG;
250812992SRod.Evans@Oracle.COM 			dyn->d_un.d_ptr = 0;
25090Sstevel@tonic-gate 			dyn++;
25100Sstevel@tonic-gate 		}
25110Sstevel@tonic-gate 
25120Sstevel@tonic-gate 		if (ofl->ofl_oscap) {
25130Sstevel@tonic-gate 			dyn->d_tag = DT_SUNW_CAP;
25140Sstevel@tonic-gate 			dyn->d_un.d_val = ofl->ofl_oscap->os_shdr->sh_addr;
25150Sstevel@tonic-gate 			dyn++;
25160Sstevel@tonic-gate 		}
251711827SRod.Evans@Sun.COM 		if (ofl->ofl_oscapinfo) {
251811827SRod.Evans@Sun.COM 			dyn->d_tag = DT_SUNW_CAPINFO;
251911827SRod.Evans@Sun.COM 			dyn->d_un.d_val = ofl->ofl_oscapinfo->os_shdr->sh_addr;
252011827SRod.Evans@Sun.COM 			dyn++;
252111827SRod.Evans@Sun.COM 		}
252211827SRod.Evans@Sun.COM 		if (ofl->ofl_oscapchain) {
252311827SRod.Evans@Sun.COM 			shdr = ofl->ofl_oscapchain->os_shdr;
252411827SRod.Evans@Sun.COM 
252511827SRod.Evans@Sun.COM 			dyn->d_tag = DT_SUNW_CAPCHAIN;
252611827SRod.Evans@Sun.COM 			dyn->d_un.d_val = shdr->sh_addr;
252711827SRod.Evans@Sun.COM 			dyn++;
252811827SRod.Evans@Sun.COM 			dyn->d_tag = DT_SUNW_CAPCHAINSZ;
252911827SRod.Evans@Sun.COM 			dyn->d_un.d_val = shdr->sh_size;
253011827SRod.Evans@Sun.COM 			dyn++;
253111827SRod.Evans@Sun.COM 			dyn->d_tag = DT_SUNW_CAPCHAINENT;
253211827SRod.Evans@Sun.COM 			dyn->d_un.d_val = shdr->sh_entsize;
253311827SRod.Evans@Sun.COM 			dyn++;
253411827SRod.Evans@Sun.COM 		}
25356299Sab196087 		if (flags & FLG_OF_SYMBOLIC) {
25366299Sab196087 			dyn->d_tag = DT_SYMBOLIC;
25376299Sab196087 			dyn->d_un.d_val = 0;
25386299Sab196087 			dyn++;
25396299Sab196087 		}
25400Sstevel@tonic-gate 	}
25410Sstevel@tonic-gate 
25420Sstevel@tonic-gate 	dyn->d_tag = DT_FLAGS;
25430Sstevel@tonic-gate 	dyn->d_un.d_val = ofl->ofl_dtflags;
25440Sstevel@tonic-gate 	dyn++;
25450Sstevel@tonic-gate 
25460Sstevel@tonic-gate 	/*
25470Sstevel@tonic-gate 	 * If -Bdirect was specified, but some NODIRECT symbols were specified
25480Sstevel@tonic-gate 	 * via a mapfile, or -znodirect was used on the command line, then
25490Sstevel@tonic-gate 	 * clear the DF_1_DIRECT flag.  The resultant object will use per-symbol
25500Sstevel@tonic-gate 	 * direct bindings rather than be enabled for global direct bindings.
255110167SRod.Evans@Sun.COM 	 *
255210167SRod.Evans@Sun.COM 	 * If any no-direct bindings exist within this object, set the
255310167SRod.Evans@Sun.COM 	 * DF_1_NODIRECT flag.  ld(1) recognizes this flag when processing
255410167SRod.Evans@Sun.COM 	 * dependencies, and performs extra work to ensure that no direct
255510167SRod.Evans@Sun.COM 	 * bindings are established to the no-direct symbols that exist
255610167SRod.Evans@Sun.COM 	 * within these dependencies.
25570Sstevel@tonic-gate 	 */
255810167SRod.Evans@Sun.COM 	if (ofl->ofl_flags1 & FLG_OF1_NGLBDIR)
25590Sstevel@tonic-gate 		ofl->ofl_dtflags_1 &= ~DF_1_DIRECT;
256010167SRod.Evans@Sun.COM 	if (ofl->ofl_flags1 & FLG_OF1_NDIRECT)
25615220Srie 		ofl->ofl_dtflags_1 |= DF_1_NODIRECT;
25620Sstevel@tonic-gate 
25630Sstevel@tonic-gate 	dyn->d_tag = DT_FLAGS_1;
25640Sstevel@tonic-gate 	dyn->d_un.d_val = ofl->ofl_dtflags_1;
25650Sstevel@tonic-gate 	dyn++;
25660Sstevel@tonic-gate 
25673850Sab196087 	dyn->d_tag = DT_SUNW_STRPAD;
25683850Sab196087 	dyn->d_un.d_val = DYNSTR_EXTRA_PAD;
25693850Sab196087 	dyn++;
25703850Sab196087 
25716206Sab196087 	dyn->d_tag = DT_SUNW_LDMACH;
25726206Sab196087 	dyn->d_un.d_val = ld_sunw_ldmach();
25736206Sab196087 	dyn++;
25746206Sab196087 
25756206Sab196087 	(*ld_targ.t_mr.mr_mach_update_odynamic)(ofl, &dyn);
25760Sstevel@tonic-gate 
25773850Sab196087 	for (cnt = 1 + DYNAMIC_EXTRA_ELTS; cnt--; dyn++) {
25783850Sab196087 		dyn->d_tag = DT_NULL;
25793850Sab196087 		dyn->d_un.d_val = 0;
25803850Sab196087 	}
25810Sstevel@tonic-gate 
25823788Sab196087 	/*
258310792SRod.Evans@Sun.COM 	 * Ensure that we wrote the right number of entries. If not, we either
258410792SRod.Evans@Sun.COM 	 * miscounted in make_dynamic(), or we did something wrong in this
258510792SRod.Evans@Sun.COM 	 * function.
25863788Sab196087 	 */
25873788Sab196087 	assert((ofl->ofl_osdynamic->os_shdr->sh_size /
25883788Sab196087 	    ofl->ofl_osdynamic->os_shdr->sh_entsize) ==
25893850Sab196087 	    ((uintptr_t)dyn - (uintptr_t)_dyn) / sizeof (*dyn));
25903788Sab196087 
25910Sstevel@tonic-gate 	return (1);
25920Sstevel@tonic-gate }
25930Sstevel@tonic-gate 
25940Sstevel@tonic-gate /*
25950Sstevel@tonic-gate  * Build the version definition section
25960Sstevel@tonic-gate  */
25971618Srie static int
update_overdef(Ofl_desc * ofl)25980Sstevel@tonic-gate update_overdef(Ofl_desc *ofl)
25990Sstevel@tonic-gate {
26009131SRod.Evans@Sun.COM 	Aliste		idx1;
26010Sstevel@tonic-gate 	Ver_desc	*vdp, *_vdp;
26020Sstevel@tonic-gate 	Verdef		*vdf, *_vdf;
26030Sstevel@tonic-gate 	int		num = 0;
26047682SAli.Bahrami@Sun.COM 	Os_desc		*strosp;
260510792SRod.Evans@Sun.COM 	Str_tbl		*strtbl;
260610792SRod.Evans@Sun.COM 
260710792SRod.Evans@Sun.COM 	/*
260810792SRod.Evans@Sun.COM 	 * Determine which string table to use.
260910792SRod.Evans@Sun.COM 	 */
261010792SRod.Evans@Sun.COM 	if (OFL_IS_STATIC_OBJ(ofl)) {
261110792SRod.Evans@Sun.COM 		strtbl = ofl->ofl_strtab;
261210792SRod.Evans@Sun.COM 		strosp = ofl->ofl_osstrtab;
261310792SRod.Evans@Sun.COM 	} else {
261410792SRod.Evans@Sun.COM 		strtbl = ofl->ofl_dynstrtab;
261510792SRod.Evans@Sun.COM 		strosp = ofl->ofl_osdynstr;
261610792SRod.Evans@Sun.COM 	}
26170Sstevel@tonic-gate 
26180Sstevel@tonic-gate 	/*
26190Sstevel@tonic-gate 	 * Traverse the version descriptors and update the version structures
26200Sstevel@tonic-gate 	 * to point to the dynstr name in preparation for building the version
26210Sstevel@tonic-gate 	 * section structure.
26220Sstevel@tonic-gate 	 */
26239131SRod.Evans@Sun.COM 	for (APLIST_TRAVERSE(ofl->ofl_verdesc, idx1, vdp)) {
26249131SRod.Evans@Sun.COM 		Sym_desc	*sdp;
26250Sstevel@tonic-gate 
26260Sstevel@tonic-gate 		if (vdp->vd_flags & VER_FLG_BASE) {
26270Sstevel@tonic-gate 			const char	*name = vdp->vd_name;
26285892Sab196087 			size_t		stoff;
26290Sstevel@tonic-gate 
26300Sstevel@tonic-gate 			/*
26310Sstevel@tonic-gate 			 * Create a new string table entry to represent the base
26320Sstevel@tonic-gate 			 * version name (there is no corresponding symbol for
26330Sstevel@tonic-gate 			 * this).
26340Sstevel@tonic-gate 			 */
263510792SRod.Evans@Sun.COM 			(void) st_setstring(strtbl, name, &stoff);
263610792SRod.Evans@Sun.COM 			/* LINTED */
263710792SRod.Evans@Sun.COM 			vdp->vd_name = (const char *)stoff;
26380Sstevel@tonic-gate 		} else {
26391618Srie 			sdp = ld_sym_find(vdp->vd_name, vdp->vd_hash, 0, ofl);
26400Sstevel@tonic-gate 			/* LINTED */
26410Sstevel@tonic-gate 			vdp->vd_name = (const char *)
26424716Sab196087 			    (uintptr_t)sdp->sd_sym->st_name;
26430Sstevel@tonic-gate 		}
26440Sstevel@tonic-gate 	}
26450Sstevel@tonic-gate 
26460Sstevel@tonic-gate 	_vdf = vdf = (Verdef *)ofl->ofl_osverdef->os_outdata->d_buf;
26470Sstevel@tonic-gate 
26480Sstevel@tonic-gate 	/*
26490Sstevel@tonic-gate 	 * Traverse the version descriptors and update the version section to
26500Sstevel@tonic-gate 	 * reflect each version and its associated dependencies.
26510Sstevel@tonic-gate 	 */
26529131SRod.Evans@Sun.COM 	for (APLIST_TRAVERSE(ofl->ofl_verdesc, idx1, vdp)) {
26539131SRod.Evans@Sun.COM 		Aliste		idx2;
26540Sstevel@tonic-gate 		Half		cnt = 1;
26559131SRod.Evans@Sun.COM 		Verdaux		*vdap, *_vdap;
26560Sstevel@tonic-gate 
26570Sstevel@tonic-gate 		_vdap = vdap = (Verdaux *)(vdf + 1);
26580Sstevel@tonic-gate 
26590Sstevel@tonic-gate 		vdf->vd_version = VER_DEF_CURRENT;
26600Sstevel@tonic-gate 		vdf->vd_flags	= vdp->vd_flags & MSK_VER_USER;
26610Sstevel@tonic-gate 		vdf->vd_ndx	= vdp->vd_ndx;
26620Sstevel@tonic-gate 		vdf->vd_hash	= vdp->vd_hash;
26630Sstevel@tonic-gate 
26640Sstevel@tonic-gate 		/* LINTED */
26650Sstevel@tonic-gate 		vdap->vda_name = (uintptr_t)vdp->vd_name;
26660Sstevel@tonic-gate 		vdap++;
26670Sstevel@tonic-gate 		/* LINTED */
26680Sstevel@tonic-gate 		_vdap->vda_next = (Word)((uintptr_t)vdap - (uintptr_t)_vdap);
26690Sstevel@tonic-gate 
26700Sstevel@tonic-gate 		/*
26710Sstevel@tonic-gate 		 * Traverse this versions dependency list generating the
26720Sstevel@tonic-gate 		 * appropriate version dependency entries.
26730Sstevel@tonic-gate 		 */
26749131SRod.Evans@Sun.COM 		for (APLIST_TRAVERSE(vdp->vd_deps, idx2, _vdp)) {
26750Sstevel@tonic-gate 			/* LINTED */
26760Sstevel@tonic-gate 			vdap->vda_name = (uintptr_t)_vdp->vd_name;
26770Sstevel@tonic-gate 			_vdap = vdap;
26780Sstevel@tonic-gate 			vdap++, cnt++;
26790Sstevel@tonic-gate 			/* LINTED */
26800Sstevel@tonic-gate 			_vdap->vda_next = (Word)((uintptr_t)vdap -
26810Sstevel@tonic-gate 			    (uintptr_t)_vdap);
26820Sstevel@tonic-gate 		}
26830Sstevel@tonic-gate 		_vdap->vda_next = 0;
26840Sstevel@tonic-gate 
26850Sstevel@tonic-gate 		/*
26860Sstevel@tonic-gate 		 * Record the versions auxiliary array offset and the associated
26870Sstevel@tonic-gate 		 * dependency count.
26880Sstevel@tonic-gate 		 */
26890Sstevel@tonic-gate 		/* LINTED */
26900Sstevel@tonic-gate 		vdf->vd_aux = (Word)((uintptr_t)(vdf + 1) - (uintptr_t)vdf);
26910Sstevel@tonic-gate 		vdf->vd_cnt = cnt;
26920Sstevel@tonic-gate 
26930Sstevel@tonic-gate 		/*
26940Sstevel@tonic-gate 		 * Record the next versions offset and update the version
26950Sstevel@tonic-gate 		 * pointer.  Remember the previous version offset as the very
26960Sstevel@tonic-gate 		 * last structures next pointer should be null.
26970Sstevel@tonic-gate 		 */
26980Sstevel@tonic-gate 		_vdf = vdf;
26990Sstevel@tonic-gate 		vdf = (Verdef *)vdap, num++;
27000Sstevel@tonic-gate 		/* LINTED */
27010Sstevel@tonic-gate 		_vdf->vd_next = (Word)((uintptr_t)vdf - (uintptr_t)_vdf);
27020Sstevel@tonic-gate 	}
27030Sstevel@tonic-gate 	_vdf->vd_next = 0;
27040Sstevel@tonic-gate 
27050Sstevel@tonic-gate 	/*
27060Sstevel@tonic-gate 	 * Record the string table association with the version definition
27070Sstevel@tonic-gate 	 * section, and the symbol table associated with the version symbol
27080Sstevel@tonic-gate 	 * table (the actual contents of the version symbol table are filled
27090Sstevel@tonic-gate 	 * in during symbol update).
27100Sstevel@tonic-gate 	 */
27110Sstevel@tonic-gate 	/* LINTED */
27120Sstevel@tonic-gate 	ofl->ofl_osverdef->os_shdr->sh_link = (Word)elf_ndxscn(strosp->os_scn);
27130Sstevel@tonic-gate 
27140Sstevel@tonic-gate 	/*
27150Sstevel@tonic-gate 	 * The version definition sections `info' field is used to indicate the
27160Sstevel@tonic-gate 	 * number of entries in this section.
27170Sstevel@tonic-gate 	 */
27180Sstevel@tonic-gate 	ofl->ofl_osverdef->os_shdr->sh_info = num;
27190Sstevel@tonic-gate 
27200Sstevel@tonic-gate 	return (1);
27210Sstevel@tonic-gate }
27220Sstevel@tonic-gate 
27230Sstevel@tonic-gate /*
27247682SAli.Bahrami@Sun.COM  * Finish the version symbol index section
27257682SAli.Bahrami@Sun.COM  */
272610792SRod.Evans@Sun.COM static void
update_oversym(Ofl_desc * ofl)27277682SAli.Bahrami@Sun.COM update_oversym(Ofl_desc *ofl)
27287682SAli.Bahrami@Sun.COM {
272910792SRod.Evans@Sun.COM 	Os_desc		*osp;
27307682SAli.Bahrami@Sun.COM 
27317682SAli.Bahrami@Sun.COM 	/*
273210792SRod.Evans@Sun.COM 	 * Record the symbol table associated with the version symbol table.
273310792SRod.Evans@Sun.COM 	 * The contents of the version symbol table are filled in during
273410792SRod.Evans@Sun.COM 	 * symbol update.
27357682SAli.Bahrami@Sun.COM 	 */
273610792SRod.Evans@Sun.COM 	if (OFL_IS_STATIC_OBJ(ofl))
273710792SRod.Evans@Sun.COM 		osp = ofl->ofl_ossymtab;
273810792SRod.Evans@Sun.COM 	else
273910792SRod.Evans@Sun.COM 		osp = ofl->ofl_osdynsym;
27407682SAli.Bahrami@Sun.COM 
27417682SAli.Bahrami@Sun.COM 	/* LINTED */
274210792SRod.Evans@Sun.COM 	ofl->ofl_osversym->os_shdr->sh_link = (Word)elf_ndxscn(osp->os_scn);
27437682SAli.Bahrami@Sun.COM }
27447682SAli.Bahrami@Sun.COM 
27457682SAli.Bahrami@Sun.COM /*
27460Sstevel@tonic-gate  * Build the version needed section
27470Sstevel@tonic-gate  */
27481618Srie static int
update_overneed(Ofl_desc * ofl)27490Sstevel@tonic-gate update_overneed(Ofl_desc *ofl)
27500Sstevel@tonic-gate {
27519131SRod.Evans@Sun.COM 	Aliste		idx1;
27520Sstevel@tonic-gate 	Ifl_desc	*ifl;
27530Sstevel@tonic-gate 	Verneed		*vnd, *_vnd;
275410792SRod.Evans@Sun.COM 	Os_desc		*strosp;
275510792SRod.Evans@Sun.COM 	Str_tbl		*strtbl;
27567682SAli.Bahrami@Sun.COM 	Word		num = 0;
27570Sstevel@tonic-gate 
27580Sstevel@tonic-gate 	_vnd = vnd = (Verneed *)ofl->ofl_osverneed->os_outdata->d_buf;
27590Sstevel@tonic-gate 
27600Sstevel@tonic-gate 	/*
276110792SRod.Evans@Sun.COM 	 * Determine which string table is appropriate.
276210792SRod.Evans@Sun.COM 	 */
276310792SRod.Evans@Sun.COM 	if (OFL_IS_STATIC_OBJ(ofl)) {
276410792SRod.Evans@Sun.COM 		strosp = ofl->ofl_osstrtab;
276510792SRod.Evans@Sun.COM 		strtbl = ofl->ofl_strtab;
276610792SRod.Evans@Sun.COM 	} else {
276710792SRod.Evans@Sun.COM 		strosp = ofl->ofl_osdynstr;
276810792SRod.Evans@Sun.COM 		strtbl = ofl->ofl_dynstrtab;
276910792SRod.Evans@Sun.COM 	}
277010792SRod.Evans@Sun.COM 
277110792SRod.Evans@Sun.COM 	/*
27720Sstevel@tonic-gate 	 * Traverse the shared object list looking for dependencies that have
27730Sstevel@tonic-gate 	 * versions defined within them.
27740Sstevel@tonic-gate 	 */
27759131SRod.Evans@Sun.COM 	for (APLIST_TRAVERSE(ofl->ofl_sos, idx1, ifl)) {
27760Sstevel@tonic-gate 		Half		_cnt;
27777682SAli.Bahrami@Sun.COM 		Word		cnt = 0;
27780Sstevel@tonic-gate 		Vernaux		*_vnap, *vnap;
27795892Sab196087 		size_t		stoff;
27800Sstevel@tonic-gate 
27810Sstevel@tonic-gate 		if (!(ifl->ifl_flags & FLG_IF_VERNEED))
27820Sstevel@tonic-gate 			continue;
27830Sstevel@tonic-gate 
27840Sstevel@tonic-gate 		vnd->vn_version = VER_NEED_CURRENT;
27850Sstevel@tonic-gate 
278610792SRod.Evans@Sun.COM 		(void) st_setstring(strtbl, ifl->ifl_soname, &stoff);
27870Sstevel@tonic-gate 		vnd->vn_file = stoff;
27880Sstevel@tonic-gate 
27890Sstevel@tonic-gate 		_vnap = vnap = (Vernaux *)(vnd + 1);
27900Sstevel@tonic-gate 
27917682SAli.Bahrami@Sun.COM 		/*
27927682SAli.Bahrami@Sun.COM 		 * Traverse the version index list recording
27937682SAli.Bahrami@Sun.COM 		 * each version as a needed dependency.
27947682SAli.Bahrami@Sun.COM 		 */
27957682SAli.Bahrami@Sun.COM 		for (_cnt = 0; _cnt <= ifl->ifl_vercnt; _cnt++) {
27967682SAli.Bahrami@Sun.COM 			Ver_index	*vip = &ifl->ifl_verndx[_cnt];
27977682SAli.Bahrami@Sun.COM 
27987682SAli.Bahrami@Sun.COM 			if (vip->vi_flags & FLG_VER_REFER) {
279910792SRod.Evans@Sun.COM 				(void) st_setstring(strtbl, vip->vi_name,
28007682SAli.Bahrami@Sun.COM 				    &stoff);
28017682SAli.Bahrami@Sun.COM 				vnap->vna_name = stoff;
28027682SAli.Bahrami@Sun.COM 
28037682SAli.Bahrami@Sun.COM 				if (vip->vi_desc) {
28047682SAli.Bahrami@Sun.COM 					vnap->vna_hash = vip->vi_desc->vd_hash;
28057682SAli.Bahrami@Sun.COM 					vnap->vna_flags =
28067682SAli.Bahrami@Sun.COM 					    vip->vi_desc->vd_flags;
28077682SAli.Bahrami@Sun.COM 				} else {
28087682SAli.Bahrami@Sun.COM 					vnap->vna_hash = 0;
28097682SAli.Bahrami@Sun.COM 					vnap->vna_flags = 0;
28100Sstevel@tonic-gate 				}
28117682SAli.Bahrami@Sun.COM 				vnap->vna_other = vip->vi_overndx;
28127682SAli.Bahrami@Sun.COM 
28137682SAli.Bahrami@Sun.COM 				/*
28147682SAli.Bahrami@Sun.COM 				 * If version A inherits version B, then
28157682SAli.Bahrami@Sun.COM 				 * B is implicit in A. It suffices for ld.so.1
28167682SAli.Bahrami@Sun.COM 				 * to verify A at runtime and skip B. The
28177682SAli.Bahrami@Sun.COM 				 * version normalization process sets the INFO
28187682SAli.Bahrami@Sun.COM 				 * flag for the versions we want ld.so.1 to
28199878SAli.Bahrami@Sun.COM 				 * skip.
28207682SAli.Bahrami@Sun.COM 				 */
28219878SAli.Bahrami@Sun.COM 				if (vip->vi_flags & VER_FLG_INFO)
28227682SAli.Bahrami@Sun.COM 					vnap->vna_flags |= VER_FLG_INFO;
28237682SAli.Bahrami@Sun.COM 
28247682SAli.Bahrami@Sun.COM 				_vnap = vnap;
28257682SAli.Bahrami@Sun.COM 				vnap++, cnt++;
28267682SAli.Bahrami@Sun.COM 				_vnap->vna_next =
28277682SAli.Bahrami@Sun.COM 				    /* LINTED */
28287682SAli.Bahrami@Sun.COM 				    (Word)((uintptr_t)vnap - (uintptr_t)_vnap);
28290Sstevel@tonic-gate 			}
28300Sstevel@tonic-gate 		}
28317682SAli.Bahrami@Sun.COM 
28320Sstevel@tonic-gate 		_vnap->vna_next = 0;
28330Sstevel@tonic-gate 
28340Sstevel@tonic-gate 		/*
28350Sstevel@tonic-gate 		 * Record the versions auxiliary array offset and
28360Sstevel@tonic-gate 		 * the associated dependency count.
28370Sstevel@tonic-gate 		 */
28380Sstevel@tonic-gate 		/* LINTED */
28390Sstevel@tonic-gate 		vnd->vn_aux = (Word)((uintptr_t)(vnd + 1) - (uintptr_t)vnd);
28400Sstevel@tonic-gate 		/* LINTED */
28410Sstevel@tonic-gate 		vnd->vn_cnt = (Half)cnt;
28420Sstevel@tonic-gate 
28430Sstevel@tonic-gate 		/*
28440Sstevel@tonic-gate 		 * Record the next versions offset and update the version
28450Sstevel@tonic-gate 		 * pointer.  Remember the previous version offset as the very
28460Sstevel@tonic-gate 		 * last structures next pointer should be null.
28470Sstevel@tonic-gate 		 */
28480Sstevel@tonic-gate 		_vnd = vnd;
28490Sstevel@tonic-gate 		vnd = (Verneed *)vnap, num++;
28500Sstevel@tonic-gate 		/* LINTED */
28510Sstevel@tonic-gate 		_vnd->vn_next = (Word)((uintptr_t)vnd - (uintptr_t)_vnd);
28520Sstevel@tonic-gate 	}
28530Sstevel@tonic-gate 	_vnd->vn_next = 0;
28540Sstevel@tonic-gate 
28550Sstevel@tonic-gate 	/*
285610792SRod.Evans@Sun.COM 	 * Use sh_link to record the associated string table section, and
285710792SRod.Evans@Sun.COM 	 * sh_info to indicate the number of entries contained in the section.
28580Sstevel@tonic-gate 	 */
285910792SRod.Evans@Sun.COM 	/* LINTED */
286010792SRod.Evans@Sun.COM 	ofl->ofl_osverneed->os_shdr->sh_link = (Word)elf_ndxscn(strosp->os_scn);
28610Sstevel@tonic-gate 	ofl->ofl_osverneed->os_shdr->sh_info = num;
28620Sstevel@tonic-gate 
28630Sstevel@tonic-gate 	return (1);
28640Sstevel@tonic-gate }
28650Sstevel@tonic-gate 
28660Sstevel@tonic-gate /*
28670Sstevel@tonic-gate  * Update syminfo section.
28680Sstevel@tonic-gate  */
28691618Srie static uintptr_t
update_osyminfo(Ofl_desc * ofl)28709131SRod.Evans@Sun.COM update_osyminfo(Ofl_desc *ofl)
28710Sstevel@tonic-gate {
28729131SRod.Evans@Sun.COM 	Os_desc		*symosp, *infosp = ofl->ofl_ossyminfo;
28739131SRod.Evans@Sun.COM 	Syminfo		*sip = infosp->os_outdata->d_buf;
28749131SRod.Evans@Sun.COM 	Shdr		*shdr = infosp->os_shdr;
28750Sstevel@tonic-gate 	char		*strtab;
28765892Sab196087 	Aliste		idx;
28779131SRod.Evans@Sun.COM 	Sym_desc	*sdp;
28789131SRod.Evans@Sun.COM 	Sfltr_desc	*sftp;
28790Sstevel@tonic-gate 
28800Sstevel@tonic-gate 	if (ofl->ofl_flags & FLG_OF_RELOBJ) {
28810Sstevel@tonic-gate 		symosp = ofl->ofl_ossymtab;
28820Sstevel@tonic-gate 		strtab = ofl->ofl_osstrtab->os_outdata->d_buf;
28830Sstevel@tonic-gate 	} else {
28840Sstevel@tonic-gate 		symosp = ofl->ofl_osdynsym;
28850Sstevel@tonic-gate 		strtab = ofl->ofl_osdynstr->os_outdata->d_buf;
28860Sstevel@tonic-gate 	}
28870Sstevel@tonic-gate 
28880Sstevel@tonic-gate 	/* LINTED */
28890Sstevel@tonic-gate 	infosp->os_shdr->sh_link = (Word)elf_ndxscn(symosp->os_scn);
28900Sstevel@tonic-gate 	if (ofl->ofl_osdynamic)
28910Sstevel@tonic-gate 		infosp->os_shdr->sh_info =
28920Sstevel@tonic-gate 		    /* LINTED */
28930Sstevel@tonic-gate 		    (Word)elf_ndxscn(ofl->ofl_osdynamic->os_scn);
28940Sstevel@tonic-gate 
28950Sstevel@tonic-gate 	/*
28960Sstevel@tonic-gate 	 * Update any references with the index into the dynamic table.
28970Sstevel@tonic-gate 	 */
289810792SRod.Evans@Sun.COM 	for (APLIST_TRAVERSE(ofl->ofl_symdtent, idx, sdp))
289910792SRod.Evans@Sun.COM 		sip[sdp->sd_symndx].si_boundto = sdp->sd_file->ifl_neededndx;
29000Sstevel@tonic-gate 
29010Sstevel@tonic-gate 	/*
29020Sstevel@tonic-gate 	 * Update any filtee references with the index into the dynamic table.
29030Sstevel@tonic-gate 	 */
29045892Sab196087 	for (ALIST_TRAVERSE(ofl->ofl_symfltrs, idx, sftp)) {
29055892Sab196087 		Dfltr_desc	*dftp;
29065892Sab196087 
29075892Sab196087 		dftp = alist_item(ofl->ofl_dtsfltrs, sftp->sft_idx);
29080Sstevel@tonic-gate 		sip[sftp->sft_sdp->sd_symndx].si_boundto = dftp->dft_ndx;
29090Sstevel@tonic-gate 	}
29100Sstevel@tonic-gate 
29110Sstevel@tonic-gate 	/*
29120Sstevel@tonic-gate 	 * Display debugging information about section.
29130Sstevel@tonic-gate 	 */
29141618Srie 	DBG_CALL(Dbg_syminfo_title(ofl->ofl_lml));
29151618Srie 	if (DBG_ENABLED) {
29161618Srie 		Word	_cnt, cnt = shdr->sh_size / shdr->sh_entsize;
29179131SRod.Evans@Sun.COM 		Sym	*symtab = symosp->os_outdata->d_buf;
29189131SRod.Evans@Sun.COM 		Dyn	*dyn;
29190Sstevel@tonic-gate 
29200Sstevel@tonic-gate 		if (ofl->ofl_osdynamic)
29210Sstevel@tonic-gate 			dyn = ofl->ofl_osdynamic->os_outdata->d_buf;
29220Sstevel@tonic-gate 		else
29239131SRod.Evans@Sun.COM 			dyn = NULL;
29240Sstevel@tonic-gate 
29250Sstevel@tonic-gate 		for (_cnt = 1; _cnt < cnt; _cnt++) {
29260Sstevel@tonic-gate 			if (sip[_cnt].si_flags || sip[_cnt].si_boundto)
29270Sstevel@tonic-gate 				/* LINTED */
29281618Srie 				DBG_CALL(Dbg_syminfo_entry(ofl->ofl_lml, _cnt,
29290Sstevel@tonic-gate 				    &sip[_cnt], &symtab[_cnt], strtab, dyn));
29300Sstevel@tonic-gate 		}
29310Sstevel@tonic-gate 	}
29320Sstevel@tonic-gate 	return (1);
29330Sstevel@tonic-gate }
29340Sstevel@tonic-gate 
29350Sstevel@tonic-gate /*
29360Sstevel@tonic-gate  * Build the output elf header.
29370Sstevel@tonic-gate  */
29381618Srie static uintptr_t
update_oehdr(Ofl_desc * ofl)29390Sstevel@tonic-gate update_oehdr(Ofl_desc * ofl)
29400Sstevel@tonic-gate {
29411618Srie 	Ehdr	*ehdr = ofl->ofl_nehdr;
29420Sstevel@tonic-gate 
29430Sstevel@tonic-gate 	/*
29440Sstevel@tonic-gate 	 * If an entry point symbol has already been established (refer
29450Sstevel@tonic-gate 	 * sym_validate()) simply update the elf header entry point with the
29460Sstevel@tonic-gate 	 * symbols value.  If no entry point is defined it will have been filled
29470Sstevel@tonic-gate 	 * with the start address of the first section within the text segment
29480Sstevel@tonic-gate 	 * (refer update_outfile()).
29490Sstevel@tonic-gate 	 */
29500Sstevel@tonic-gate 	if (ofl->ofl_entry)
29510Sstevel@tonic-gate 		ehdr->e_entry =
29524716Sab196087 		    ((Sym_desc *)(ofl->ofl_entry))->sd_sym->st_value;
29530Sstevel@tonic-gate 
295412029SRod.Evans@Sun.COM 	ehdr->e_ident[EI_DATA] = ld_targ.t_m.m_data;
295512029SRod.Evans@Sun.COM 	ehdr->e_version = ofl->ofl_dehdr->e_version;
295612029SRod.Evans@Sun.COM 
29570Sstevel@tonic-gate 	/*
295812029SRod.Evans@Sun.COM 	 * When generating a relocatable object under -z symbolcap, set the
295912029SRod.Evans@Sun.COM 	 * e_machine to be generic, and remove any e_flags.  Input relocatable
296012029SRod.Evans@Sun.COM 	 * objects may identify alternative e_machine (m.machplus) and e_flags
296112029SRod.Evans@Sun.COM 	 * values.  However, the functions within the created output object
296212029SRod.Evans@Sun.COM 	 * are selected at runtime using the capabilities mechanism, which
296312029SRod.Evans@Sun.COM 	 * supersedes the e-machine and e_flags information.  Therefore,
296412029SRod.Evans@Sun.COM 	 * e_machine and e_flag values are not propagated to the output object,
296512029SRod.Evans@Sun.COM 	 * as these values might prevent the kernel from loading the object
296612029SRod.Evans@Sun.COM 	 * before the runtime linker gets control.
29670Sstevel@tonic-gate 	 */
296812029SRod.Evans@Sun.COM 	if (ofl->ofl_flags & FLG_OF_OTOSCAP) {
296912029SRod.Evans@Sun.COM 		ehdr->e_machine = ld_targ.t_m.m_mach;
297012029SRod.Evans@Sun.COM 		ehdr->e_flags = 0;
297112029SRod.Evans@Sun.COM 	} else {
297212029SRod.Evans@Sun.COM 		/*
297312029SRod.Evans@Sun.COM 		 * Note. it may be necessary to update the e_flags field in the
297412029SRod.Evans@Sun.COM 		 * machine dependent section.
297512029SRod.Evans@Sun.COM 		 */
297612029SRod.Evans@Sun.COM 		ehdr->e_machine = ofl->ofl_dehdr->e_machine;
297712029SRod.Evans@Sun.COM 		ehdr->e_flags = ofl->ofl_dehdr->e_flags;
297812029SRod.Evans@Sun.COM 
297912029SRod.Evans@Sun.COM 		if (ehdr->e_machine != ld_targ.t_m.m_mach) {
298012029SRod.Evans@Sun.COM 			if (ehdr->e_machine != ld_targ.t_m.m_machplus)
298112029SRod.Evans@Sun.COM 				return (S_ERROR);
298212029SRod.Evans@Sun.COM 			if ((ehdr->e_flags & ld_targ.t_m.m_flagsplus) == 0)
298312029SRod.Evans@Sun.COM 				return (S_ERROR);
298412029SRod.Evans@Sun.COM 		}
29850Sstevel@tonic-gate 	}
29860Sstevel@tonic-gate 
29870Sstevel@tonic-gate 	if (ofl->ofl_flags & FLG_OF_SHAROBJ)
29880Sstevel@tonic-gate 		ehdr->e_type = ET_DYN;
29890Sstevel@tonic-gate 	else if (ofl->ofl_flags & FLG_OF_RELOBJ)
29900Sstevel@tonic-gate 		ehdr->e_type = ET_REL;
29910Sstevel@tonic-gate 	else
29920Sstevel@tonic-gate 		ehdr->e_type = ET_EXEC;
29930Sstevel@tonic-gate 
29940Sstevel@tonic-gate 	return (1);
29950Sstevel@tonic-gate }
29960Sstevel@tonic-gate 
29970Sstevel@tonic-gate /*
29980Sstevel@tonic-gate  * Perform move table expansion.
29990Sstevel@tonic-gate  */
30009131SRod.Evans@Sun.COM static void
expand_move(Ofl_desc * ofl,Sym_desc * sdp,Move * mvp)30019131SRod.Evans@Sun.COM expand_move(Ofl_desc *ofl, Sym_desc *sdp, Move *mvp)
30020Sstevel@tonic-gate {
30030Sstevel@tonic-gate 	Os_desc		*osp;
30049131SRod.Evans@Sun.COM 	uchar_t		*taddr, *taddr0;
30050Sstevel@tonic-gate 	Sxword		offset;
30069131SRod.Evans@Sun.COM 	Half		cnt;
30079131SRod.Evans@Sun.COM 	uint_t		stride;
30080Sstevel@tonic-gate 
30098159SAli.Bahrami@Sun.COM 	osp = ofl->ofl_isparexpn->is_osdesc;
30109131SRod.Evans@Sun.COM 	offset = sdp->sd_sym->st_value - osp->os_shdr->sh_addr;
30119131SRod.Evans@Sun.COM 
30120Sstevel@tonic-gate 	taddr0 = taddr = osp->os_outdata->d_buf;
30130Sstevel@tonic-gate 	taddr += offset;
30149131SRod.Evans@Sun.COM 	taddr = taddr + mvp->m_poffset;
30159131SRod.Evans@Sun.COM 
30169131SRod.Evans@Sun.COM 	for (cnt = 0; cnt < mvp->m_repeat; cnt++) {
30170Sstevel@tonic-gate 		/* LINTED */
30189131SRod.Evans@Sun.COM 		DBG_CALL(Dbg_move_expand(ofl->ofl_lml, mvp,
30191618Srie 		    (Addr)(taddr - taddr0)));
30209131SRod.Evans@Sun.COM 		stride = (uint_t)mvp->m_stride + 1;
30219131SRod.Evans@Sun.COM 
30229131SRod.Evans@Sun.COM 		/*
30239131SRod.Evans@Sun.COM 		 * Update the target address based upon the move entry size.
30249131SRod.Evans@Sun.COM 		 * This size was validated in ld_process_move().
30259131SRod.Evans@Sun.COM 		 */
30260Sstevel@tonic-gate 		/* LINTED */
30279131SRod.Evans@Sun.COM 		switch (ELF_M_SIZE(mvp->m_info)) {
30280Sstevel@tonic-gate 		case 1:
30290Sstevel@tonic-gate 			/* LINTED */
30309131SRod.Evans@Sun.COM 			*taddr = (uchar_t)mvp->m_value;
30310Sstevel@tonic-gate 			taddr += stride;
30320Sstevel@tonic-gate 			break;
30330Sstevel@tonic-gate 		case 2:
30340Sstevel@tonic-gate 			/* LINTED */
30359131SRod.Evans@Sun.COM 			*((Half *)taddr) = (Half)mvp->m_value;
30369131SRod.Evans@Sun.COM 			taddr += 2 * stride;
30370Sstevel@tonic-gate 			break;
30380Sstevel@tonic-gate 		case 4:
30390Sstevel@tonic-gate 			/* LINTED */
30409131SRod.Evans@Sun.COM 			*((Word *)taddr) = (Word)mvp->m_value;
30419131SRod.Evans@Sun.COM 			taddr += 4 * stride;
30420Sstevel@tonic-gate 			break;
30430Sstevel@tonic-gate 		case 8:
30440Sstevel@tonic-gate 			/* LINTED */
30459131SRod.Evans@Sun.COM 			*((u_longlong_t *)taddr) = mvp->m_value;
30469131SRod.Evans@Sun.COM 			taddr += 8 * stride;
30470Sstevel@tonic-gate 			break;
30480Sstevel@tonic-gate 		}
30490Sstevel@tonic-gate 	}
30500Sstevel@tonic-gate }
30510Sstevel@tonic-gate 
30520Sstevel@tonic-gate /*
30530Sstevel@tonic-gate  * Update Move sections.
30540Sstevel@tonic-gate  */
30559131SRod.Evans@Sun.COM static void
update_move(Ofl_desc * ofl)30560Sstevel@tonic-gate update_move(Ofl_desc *ofl)
30570Sstevel@tonic-gate {
30580Sstevel@tonic-gate 	Word		ndx = 0;
30596299Sab196087 	ofl_flag_t	flags = ofl->ofl_flags;
30609131SRod.Evans@Sun.COM 	Move		*omvp;
30619131SRod.Evans@Sun.COM 	Aliste		idx1;
30629131SRod.Evans@Sun.COM 	Sym_desc	*sdp;
30630Sstevel@tonic-gate 
30640Sstevel@tonic-gate 	/*
30650Sstevel@tonic-gate 	 * Determine the index of the symbol table that will be referenced by
30669131SRod.Evans@Sun.COM 	 * the Move section.
30670Sstevel@tonic-gate 	 */
30682766Sab196087 	if (OFL_ALLOW_DYNSYM(ofl))
30690Sstevel@tonic-gate 		/* LINTED */
30700Sstevel@tonic-gate 		ndx = (Word) elf_ndxscn(ofl->ofl_osdynsym->os_scn);
30710Sstevel@tonic-gate 	else if (!(flags & FLG_OF_STRIP) || (flags & FLG_OF_RELOBJ))
30720Sstevel@tonic-gate 		/* LINTED */
30730Sstevel@tonic-gate 		ndx = (Word) elf_ndxscn(ofl->ofl_ossymtab->os_scn);
30740Sstevel@tonic-gate 
30750Sstevel@tonic-gate 	/*
30769131SRod.Evans@Sun.COM 	 * Update sh_link of the Move section, and point to the new Move data.
30770Sstevel@tonic-gate 	 */
30780Sstevel@tonic-gate 	if (ofl->ofl_osmove) {
30790Sstevel@tonic-gate 		ofl->ofl_osmove->os_shdr->sh_link = ndx;
30809131SRod.Evans@Sun.COM 		omvp = (Move *)ofl->ofl_osmove->os_outdata->d_buf;
30810Sstevel@tonic-gate 	}
30820Sstevel@tonic-gate 
30830Sstevel@tonic-gate 	/*
30840Sstevel@tonic-gate 	 * Update symbol entry index
30850Sstevel@tonic-gate 	 */
30869131SRod.Evans@Sun.COM 	for (APLIST_TRAVERSE(ofl->ofl_parsyms, idx1, sdp)) {
30879131SRod.Evans@Sun.COM 		Aliste		idx2;
30889131SRod.Evans@Sun.COM 		Mv_desc		*mdp;
30890Sstevel@tonic-gate 
30900Sstevel@tonic-gate 		/*
30910Sstevel@tonic-gate 		 * Expand move table
30920Sstevel@tonic-gate 		 */
30939131SRod.Evans@Sun.COM 		if (sdp->sd_flags & FLG_SY_PAREXPN) {
30949131SRod.Evans@Sun.COM 			const char	*str;
30950Sstevel@tonic-gate 
30966299Sab196087 			if (flags & FLG_OF_STATIC)
30979131SRod.Evans@Sun.COM 				str = MSG_INTL(MSG_PSYM_EXPREASON1);
30980Sstevel@tonic-gate 			else if (ofl->ofl_flags1 & FLG_OF1_NOPARTI)
30999131SRod.Evans@Sun.COM 				str = MSG_INTL(MSG_PSYM_EXPREASON2);
31000Sstevel@tonic-gate 			else
31019131SRod.Evans@Sun.COM 				str = MSG_INTL(MSG_PSYM_EXPREASON3);
31029131SRod.Evans@Sun.COM 
31031618Srie 			DBG_CALL(Dbg_move_parexpn(ofl->ofl_lml,
31049131SRod.Evans@Sun.COM 			    sdp->sd_name, str));
31059131SRod.Evans@Sun.COM 
31069131SRod.Evans@Sun.COM 			for (ALIST_TRAVERSE(sdp->sd_move, idx2, mdp)) {
31071618Srie 				DBG_CALL(Dbg_move_entry1(ofl->ofl_lml, 0,
31089131SRod.Evans@Sun.COM 				    mdp->md_move, sdp));
31099131SRod.Evans@Sun.COM 				expand_move(ofl, sdp, mdp->md_move);
31100Sstevel@tonic-gate 			}
31110Sstevel@tonic-gate 			continue;
31120Sstevel@tonic-gate 		}
31130Sstevel@tonic-gate 
31140Sstevel@tonic-gate 		/*
31150Sstevel@tonic-gate 		 * Process move table
31160Sstevel@tonic-gate 		 */
31179131SRod.Evans@Sun.COM 		DBG_CALL(Dbg_move_outmove(ofl->ofl_lml, sdp->sd_name));
31189131SRod.Evans@Sun.COM 
31199131SRod.Evans@Sun.COM 		for (ALIST_TRAVERSE(sdp->sd_move, idx2, mdp)) {
31209131SRod.Evans@Sun.COM 			Move	*imvp;
31210Sstevel@tonic-gate 			int	idx = 1;
31225220Srie 			Sym	*sym;
31235220Srie 
31249131SRod.Evans@Sun.COM 			imvp = mdp->md_move;
31255220Srie 			sym = sdp->sd_sym;
31260Sstevel@tonic-gate 
31279131SRod.Evans@Sun.COM 			DBG_CALL(Dbg_move_entry1(ofl->ofl_lml, 1, imvp, sdp));
31289131SRod.Evans@Sun.COM 
31299131SRod.Evans@Sun.COM 			*omvp = *imvp;
31306299Sab196087 			if ((flags & FLG_OF_RELOBJ) == 0) {
31315220Srie 				if (ELF_ST_BIND(sym->st_info) == STB_LOCAL) {
31329131SRod.Evans@Sun.COM 					Os_desc	*osp = sdp->sd_isc->is_osdesc;
31339131SRod.Evans@Sun.COM 					Word	ndx = osp->os_identndx;
31349131SRod.Evans@Sun.COM 
31359131SRod.Evans@Sun.COM 					omvp->m_info =
31364716Sab196087 					    /* LINTED */
31379131SRod.Evans@Sun.COM 					    ELF_M_INFO(ndx, imvp->m_info);
31385220Srie 
31395220Srie 					if (ELF_ST_TYPE(sym->st_info) !=
31404716Sab196087 					    STT_SECTION) {
31419131SRod.Evans@Sun.COM 						omvp->m_poffset =
31429131SRod.Evans@Sun.COM 						    sym->st_value -
31439131SRod.Evans@Sun.COM 						    osp->os_shdr->sh_addr +
31449131SRod.Evans@Sun.COM 						    imvp->m_poffset;
31454716Sab196087 					}
31460Sstevel@tonic-gate 				} else {
31479131SRod.Evans@Sun.COM 					omvp->m_info =
31484716Sab196087 					    /* LINTED */
31494716Sab196087 					    ELF_M_INFO(sdp->sd_symndx,
31509131SRod.Evans@Sun.COM 					    imvp->m_info);
31510Sstevel@tonic-gate 				}
31520Sstevel@tonic-gate 			} else {
31530Sstevel@tonic-gate 				Boolean 	isredloc = FALSE;
31540Sstevel@tonic-gate 
31555220Srie 				if ((ELF_ST_BIND(sym->st_info) == STB_LOCAL) &&
31566614Srie 				    (ofl->ofl_flags & FLG_OF_REDLSYM))
31570Sstevel@tonic-gate 					isredloc = TRUE;
31580Sstevel@tonic-gate 
31599131SRod.Evans@Sun.COM 				if (isredloc && !(sdp->sd_move)) {
31609131SRod.Evans@Sun.COM 					Os_desc	*osp = sdp->sd_isc->is_osdesc;
31619131SRod.Evans@Sun.COM 					Word	ndx = osp->os_identndx;
31629131SRod.Evans@Sun.COM 
31639131SRod.Evans@Sun.COM 					omvp->m_info =
31644716Sab196087 					    /* LINTED */
31659131SRod.Evans@Sun.COM 					    ELF_M_INFO(ndx, imvp->m_info);
31669131SRod.Evans@Sun.COM 
31679131SRod.Evans@Sun.COM 					omvp->m_poffset += sym->st_value;
31680Sstevel@tonic-gate 				} else {
31690Sstevel@tonic-gate 					if (isredloc)
31704716Sab196087 						DBG_CALL(Dbg_syms_reduce(ofl,
31719131SRod.Evans@Sun.COM 						    DBG_SYM_REDUCE_RETAIN,
31729131SRod.Evans@Sun.COM 						    sdp, idx,
31734716Sab196087 						    ofl->ofl_osmove->os_name));
31740Sstevel@tonic-gate 
31759131SRod.Evans@Sun.COM 					omvp->m_info =
31764716Sab196087 					    /* LINTED */
31774716Sab196087 					    ELF_M_INFO(sdp->sd_symndx,
31789131SRod.Evans@Sun.COM 					    imvp->m_info);
31790Sstevel@tonic-gate 				}
31800Sstevel@tonic-gate 			}
31819131SRod.Evans@Sun.COM 
31829131SRod.Evans@Sun.COM 			DBG_CALL(Dbg_move_entry1(ofl->ofl_lml, 0, omvp, sdp));
31839131SRod.Evans@Sun.COM 			omvp++;
31840Sstevel@tonic-gate 			idx++;
31850Sstevel@tonic-gate 		}
31860Sstevel@tonic-gate 	}
31870Sstevel@tonic-gate }
31880Sstevel@tonic-gate 
31890Sstevel@tonic-gate /*
31909131SRod.Evans@Sun.COM  * Scan through the SHT_GROUP output sections.  Update their sh_link/sh_info
31919131SRod.Evans@Sun.COM  * fields as well as the section contents.
31920Sstevel@tonic-gate  */
31931618Srie static uintptr_t
update_ogroup(Ofl_desc * ofl)31947463SRod.Evans@Sun.COM update_ogroup(Ofl_desc *ofl)
31950Sstevel@tonic-gate {
31969131SRod.Evans@Sun.COM 	Aliste		idx;
31970Sstevel@tonic-gate 	Os_desc		*osp;
31980Sstevel@tonic-gate 	uintptr_t	error = 0;
31990Sstevel@tonic-gate 
32009131SRod.Evans@Sun.COM 	for (APLIST_TRAVERSE(ofl->ofl_osgroups, idx, osp)) {
32010Sstevel@tonic-gate 		Is_desc		*isp;
32020Sstevel@tonic-gate 		Ifl_desc	*ifl;
32030Sstevel@tonic-gate 		Shdr		*shdr = osp->os_shdr;
32040Sstevel@tonic-gate 		Sym_desc	*sdp;
32050Sstevel@tonic-gate 		Xword		i, grpcnt;
32060Sstevel@tonic-gate 		Word		*gdata;
32070Sstevel@tonic-gate 
32080Sstevel@tonic-gate 		/*
32090Sstevel@tonic-gate 		 * Since input GROUP sections always create unique
32100Sstevel@tonic-gate 		 * output GROUP sections - we know there is only one
32110Sstevel@tonic-gate 		 * item on the list.
32120Sstevel@tonic-gate 		 */
32139615SAli.Bahrami@Sun.COM 		isp = ld_os_first_isdesc(osp);
32140Sstevel@tonic-gate 
32150Sstevel@tonic-gate 		ifl = isp->is_file;
32160Sstevel@tonic-gate 		sdp = ifl->ifl_oldndx[isp->is_shdr->sh_info];
32170Sstevel@tonic-gate 		shdr->sh_link = (Word)elf_ndxscn(ofl->ofl_ossymtab->os_scn);
32180Sstevel@tonic-gate 		shdr->sh_info = sdp->sd_symndx;
32190Sstevel@tonic-gate 
32200Sstevel@tonic-gate 		/*
32210Sstevel@tonic-gate 		 * Scan through the group data section and update
32220Sstevel@tonic-gate 		 * all of the links to new values.
32230Sstevel@tonic-gate 		 */
32240Sstevel@tonic-gate 		grpcnt = shdr->sh_size / shdr->sh_entsize;
32250Sstevel@tonic-gate 		gdata = (Word *)osp->os_outdata->d_buf;
32267463SRod.Evans@Sun.COM 
32270Sstevel@tonic-gate 		for (i = 1; i < grpcnt; i++) {
32287463SRod.Evans@Sun.COM 			Os_desc	*_osp;
32297463SRod.Evans@Sun.COM 			Is_desc	*_isp = ifl->ifl_isdesc[gdata[i]];
32300Sstevel@tonic-gate 
32310Sstevel@tonic-gate 			/*
32320Sstevel@tonic-gate 			 * If the referenced section didn't make it to the
32330Sstevel@tonic-gate 			 * output file - just zero out the entry.
32340Sstevel@tonic-gate 			 */
32357463SRod.Evans@Sun.COM 			if ((_osp = _isp->is_osdesc) == NULL)
32360Sstevel@tonic-gate 				gdata[i] = 0;
32370Sstevel@tonic-gate 			else
32380Sstevel@tonic-gate 				gdata[i] = (Word)elf_ndxscn(_osp->os_scn);
32390Sstevel@tonic-gate 		}
32400Sstevel@tonic-gate 	}
32410Sstevel@tonic-gate 	return (error);
32420Sstevel@tonic-gate }
32430Sstevel@tonic-gate 
32441618Srie static void
update_ostrtab(Os_desc * osp,Str_tbl * stp,uint_t extra)32453850Sab196087 update_ostrtab(Os_desc *osp, Str_tbl *stp, uint_t extra)
32460Sstevel@tonic-gate {
32470Sstevel@tonic-gate 	Elf_Data	*data;
32483850Sab196087 
32499131SRod.Evans@Sun.COM 	if (osp == NULL)
32500Sstevel@tonic-gate 		return;
32510Sstevel@tonic-gate 
32520Sstevel@tonic-gate 	data = osp->os_outdata;
32533850Sab196087 	assert(data->d_size == (st_getstrtab_sz(stp) + extra));
32545892Sab196087 	(void) st_setstrbuf(stp, data->d_buf, data->d_size - extra);
32553850Sab196087 	/* If leaving an extra hole at the end, zero it */
32563850Sab196087 	if (extra > 0)
32573850Sab196087 		(void) memset((char *)data->d_buf + data->d_size - extra,
32583850Sab196087 		    0x0, extra);
32590Sstevel@tonic-gate }
32600Sstevel@tonic-gate 
32610Sstevel@tonic-gate /*
326211827SRod.Evans@Sun.COM  * Update capabilities information.
326311827SRod.Evans@Sun.COM  *
326411827SRod.Evans@Sun.COM  * If string table capabilities exist, then the associated string must be
326511827SRod.Evans@Sun.COM  * translated into an offset into the string table.
326611827SRod.Evans@Sun.COM  */
326711827SRod.Evans@Sun.COM static void
update_oscap(Ofl_desc * ofl)326811827SRod.Evans@Sun.COM update_oscap(Ofl_desc *ofl)
326911827SRod.Evans@Sun.COM {
327011827SRod.Evans@Sun.COM 	Os_desc		*strosp, *cosp;
327111827SRod.Evans@Sun.COM 	Cap		*cap;
327211827SRod.Evans@Sun.COM 	Str_tbl		*strtbl;
327311827SRod.Evans@Sun.COM 	Capstr		*capstr;
327411827SRod.Evans@Sun.COM 	size_t		stoff;
327511827SRod.Evans@Sun.COM 	Aliste		idx1;
327611827SRod.Evans@Sun.COM 
327711827SRod.Evans@Sun.COM 	/*
327811827SRod.Evans@Sun.COM 	 * Determine which symbol table or string table is appropriate.
327911827SRod.Evans@Sun.COM 	 */
328011827SRod.Evans@Sun.COM 	if (OFL_IS_STATIC_OBJ(ofl)) {
328111827SRod.Evans@Sun.COM 		strosp = ofl->ofl_osstrtab;
328211827SRod.Evans@Sun.COM 		strtbl = ofl->ofl_strtab;
328311827SRod.Evans@Sun.COM 	} else {
328411827SRod.Evans@Sun.COM 		strosp = ofl->ofl_osdynstr;
328511827SRod.Evans@Sun.COM 		strtbl = ofl->ofl_dynstrtab;
328611827SRod.Evans@Sun.COM 	}
328711827SRod.Evans@Sun.COM 
328811827SRod.Evans@Sun.COM 	/*
328911827SRod.Evans@Sun.COM 	 * If symbol capabilities exist, set the sh_link field of the .SUNW_cap
329011827SRod.Evans@Sun.COM 	 * section to the .SUNW_capinfo section.
329111827SRod.Evans@Sun.COM 	 */
329211827SRod.Evans@Sun.COM 	if (ofl->ofl_oscapinfo) {
329311827SRod.Evans@Sun.COM 		cosp = ofl->ofl_oscap;
329411827SRod.Evans@Sun.COM 		cosp->os_shdr->sh_link =
329511827SRod.Evans@Sun.COM 		    (Word)elf_ndxscn(ofl->ofl_oscapinfo->os_scn);
329611827SRod.Evans@Sun.COM 	}
329711827SRod.Evans@Sun.COM 
329811827SRod.Evans@Sun.COM 	/*
329911827SRod.Evans@Sun.COM 	 * If there are capability strings to process, set the sh_info
330011827SRod.Evans@Sun.COM 	 * field of the .SUNW_cap section to the associated string table, and
330111827SRod.Evans@Sun.COM 	 * proceed to process any CA_SUNW_PLAT entries.
330211827SRod.Evans@Sun.COM 	 */
330311827SRod.Evans@Sun.COM 	if ((ofl->ofl_flags & FLG_OF_CAPSTRS) == 0)
330411827SRod.Evans@Sun.COM 		return;
330511827SRod.Evans@Sun.COM 
330611827SRod.Evans@Sun.COM 	cosp = ofl->ofl_oscap;
330711827SRod.Evans@Sun.COM 	cosp->os_shdr->sh_info = (Word)elf_ndxscn(strosp->os_scn);
330811827SRod.Evans@Sun.COM 
330911827SRod.Evans@Sun.COM 	cap = ofl->ofl_oscap->os_outdata->d_buf;
331011827SRod.Evans@Sun.COM 
331111827SRod.Evans@Sun.COM 	/*
331211827SRod.Evans@Sun.COM 	 * Determine whether an object capability identifier, or object
331311827SRod.Evans@Sun.COM 	 * machine/platform capabilities exists.
331411827SRod.Evans@Sun.COM 	 */
331511827SRod.Evans@Sun.COM 	capstr = &ofl->ofl_ocapset.oc_id;
331611827SRod.Evans@Sun.COM 	if (capstr->cs_str) {
331711827SRod.Evans@Sun.COM 		(void) st_setstring(strtbl, capstr->cs_str, &stoff);
331811827SRod.Evans@Sun.COM 		cap[capstr->cs_ndx].c_un.c_ptr = stoff;
331911827SRod.Evans@Sun.COM 	}
332011827SRod.Evans@Sun.COM 	for (ALIST_TRAVERSE(ofl->ofl_ocapset.oc_plat.cl_val, idx1, capstr)) {
332111827SRod.Evans@Sun.COM 		(void) st_setstring(strtbl, capstr->cs_str, &stoff);
332211827SRod.Evans@Sun.COM 		cap[capstr->cs_ndx].c_un.c_ptr = stoff;
332311827SRod.Evans@Sun.COM 	}
332411827SRod.Evans@Sun.COM 	for (ALIST_TRAVERSE(ofl->ofl_ocapset.oc_mach.cl_val, idx1, capstr)) {
332511827SRod.Evans@Sun.COM 		(void) st_setstring(strtbl, capstr->cs_str, &stoff);
332611827SRod.Evans@Sun.COM 		cap[capstr->cs_ndx].c_un.c_ptr = stoff;
332711827SRod.Evans@Sun.COM 	}
332811827SRod.Evans@Sun.COM 
332911827SRod.Evans@Sun.COM 	/*
333011827SRod.Evans@Sun.COM 	 * Determine any symbol capability identifiers, or machine/platform
333111827SRod.Evans@Sun.COM 	 * capabilities.
333211827SRod.Evans@Sun.COM 	 */
333311827SRod.Evans@Sun.COM 	if (ofl->ofl_capgroups) {
333411827SRod.Evans@Sun.COM 		Cap_group	*cgp;
333511827SRod.Evans@Sun.COM 
333611827SRod.Evans@Sun.COM 		for (APLIST_TRAVERSE(ofl->ofl_capgroups, idx1, cgp)) {
333711827SRod.Evans@Sun.COM 			Objcapset	*ocapset = &cgp->cg_set;
333811827SRod.Evans@Sun.COM 			Aliste		idx2;
333911827SRod.Evans@Sun.COM 
334011827SRod.Evans@Sun.COM 			capstr = &ocapset->oc_id;
334111827SRod.Evans@Sun.COM 			if (capstr->cs_str) {
334211827SRod.Evans@Sun.COM 				(void) st_setstring(strtbl, capstr->cs_str,
334311827SRod.Evans@Sun.COM 				    &stoff);
334411827SRod.Evans@Sun.COM 				cap[capstr->cs_ndx].c_un.c_ptr = stoff;
334511827SRod.Evans@Sun.COM 			}
334611827SRod.Evans@Sun.COM 			for (ALIST_TRAVERSE(ocapset->oc_plat.cl_val, idx2,
334711827SRod.Evans@Sun.COM 			    capstr)) {
334811827SRod.Evans@Sun.COM 				(void) st_setstring(strtbl, capstr->cs_str,
334911827SRod.Evans@Sun.COM 				    &stoff);
335011827SRod.Evans@Sun.COM 				cap[capstr->cs_ndx].c_un.c_ptr = stoff;
335111827SRod.Evans@Sun.COM 			}
335211827SRod.Evans@Sun.COM 			for (ALIST_TRAVERSE(ocapset->oc_mach.cl_val, idx2,
335311827SRod.Evans@Sun.COM 			    capstr)) {
335411827SRod.Evans@Sun.COM 				(void) st_setstring(strtbl, capstr->cs_str,
335511827SRod.Evans@Sun.COM 				    &stoff);
335611827SRod.Evans@Sun.COM 				cap[capstr->cs_ndx].c_un.c_ptr = stoff;
335711827SRod.Evans@Sun.COM 			}
335811827SRod.Evans@Sun.COM 		}
335911827SRod.Evans@Sun.COM 	}
336011827SRod.Evans@Sun.COM }
336111827SRod.Evans@Sun.COM 
336211827SRod.Evans@Sun.COM /*
336311827SRod.Evans@Sun.COM  * Update the .SUNW_capinfo, and possibly the .SUNW_capchain sections.
336411827SRod.Evans@Sun.COM  */
336511827SRod.Evans@Sun.COM static void
update_oscapinfo(Ofl_desc * ofl)336611827SRod.Evans@Sun.COM update_oscapinfo(Ofl_desc *ofl)
336711827SRod.Evans@Sun.COM {
336811827SRod.Evans@Sun.COM 	Os_desc		*symosp, *ciosp, *ccosp = NULL;
336911827SRod.Evans@Sun.COM 	Capinfo		*ocapinfo;
337011827SRod.Evans@Sun.COM 	Capchain	*ocapchain;
337111827SRod.Evans@Sun.COM 	Cap_avlnode	*cav;
337211827SRod.Evans@Sun.COM 	Word		chainndx = 0;
337311827SRod.Evans@Sun.COM 
337411827SRod.Evans@Sun.COM 	/*
337511827SRod.Evans@Sun.COM 	 * Determine which symbol table is appropriate.
337611827SRod.Evans@Sun.COM 	 */
337711827SRod.Evans@Sun.COM 	if (OFL_IS_STATIC_OBJ(ofl))
337811827SRod.Evans@Sun.COM 		symosp = ofl->ofl_ossymtab;
337911827SRod.Evans@Sun.COM 	else
338011827SRod.Evans@Sun.COM 		symosp = ofl->ofl_osdynsym;
338111827SRod.Evans@Sun.COM 
338211827SRod.Evans@Sun.COM 	/*
338311827SRod.Evans@Sun.COM 	 * Update the .SUNW_capinfo sh_link to point to the appropriate symbol
338411827SRod.Evans@Sun.COM 	 * table section.  If we're creating a dynamic object, the
338511827SRod.Evans@Sun.COM 	 * .SUNW_capinfo sh_info is updated to point to the .SUNW_capchain
338611827SRod.Evans@Sun.COM 	 * section.
338711827SRod.Evans@Sun.COM 	 */
338811827SRod.Evans@Sun.COM 	ciosp = ofl->ofl_oscapinfo;
338911827SRod.Evans@Sun.COM 	ciosp->os_shdr->sh_link = (Word)elf_ndxscn(symosp->os_scn);
339011827SRod.Evans@Sun.COM 
339111827SRod.Evans@Sun.COM 	if (OFL_IS_STATIC_OBJ(ofl) == 0) {
339211827SRod.Evans@Sun.COM 		ccosp = ofl->ofl_oscapchain;
339311827SRod.Evans@Sun.COM 		ciosp->os_shdr->sh_info = (Word)elf_ndxscn(ccosp->os_scn);
339411827SRod.Evans@Sun.COM 	}
339511827SRod.Evans@Sun.COM 
339611827SRod.Evans@Sun.COM 	/*
339711827SRod.Evans@Sun.COM 	 * Establish the data for each section.  The first element of each
339811827SRod.Evans@Sun.COM 	 * section defines the section's version number.
339911827SRod.Evans@Sun.COM 	 */
340011827SRod.Evans@Sun.COM 	ocapinfo = ciosp->os_outdata->d_buf;
340111827SRod.Evans@Sun.COM 	ocapinfo[0] = CAPINFO_CURRENT;
340211827SRod.Evans@Sun.COM 	if (ccosp) {
340311827SRod.Evans@Sun.COM 		ocapchain = ccosp->os_outdata->d_buf;
340411827SRod.Evans@Sun.COM 		ocapchain[chainndx++] = CAPCHAIN_CURRENT;
340511827SRod.Evans@Sun.COM 	}
340611827SRod.Evans@Sun.COM 
340711827SRod.Evans@Sun.COM 	/*
340811827SRod.Evans@Sun.COM 	 * Traverse all capabilities families.  Each member has a .SUNW_capinfo
340911827SRod.Evans@Sun.COM 	 * assignment.  The .SUNW_capinfo entry differs for relocatable objects
341011827SRod.Evans@Sun.COM 	 * and dynamic objects.
341111827SRod.Evans@Sun.COM 	 *
341211827SRod.Evans@Sun.COM 	 * Relocatable objects:
341311827SRod.Evans@Sun.COM 	 *			ELF_C_GROUP		ELF_C_SYM
341411827SRod.Evans@Sun.COM 	 *
341511827SRod.Evans@Sun.COM 	 * Family lead:		CAPINFO_SUNW_GLOB	lead symbol index
341611827SRod.Evans@Sun.COM 	 * Family lead alias:	CAPINFO_SUNW_GLOB	lead symbol index
341711827SRod.Evans@Sun.COM 	 * Family member:	.SUNW_cap index		lead symbol index
341811827SRod.Evans@Sun.COM 	 *
341911827SRod.Evans@Sun.COM 	 * Dynamic objects:
342011827SRod.Evans@Sun.COM 	 *			ELF_C_GROUP		ELF_C_SYM
342111827SRod.Evans@Sun.COM 	 *
342211827SRod.Evans@Sun.COM 	 * Family lead:		CAPINFO_SUNW_GLOB	.SUNW_capchain index
342311827SRod.Evans@Sun.COM 	 * Family lead alias:	CAPINFO_SUNW_GLOB	.SUNW_capchain index
342411827SRod.Evans@Sun.COM 	 * Family member:	.SUNW_cap index		lead symbol index
342511827SRod.Evans@Sun.COM 	 *
342611827SRod.Evans@Sun.COM 	 * The ELF_C_GROUP field identifies a capabilities symbol.  Lead
342711827SRod.Evans@Sun.COM 	 * capability symbols, and lead capability aliases are identified by
342811827SRod.Evans@Sun.COM 	 * a CAPINFO_SUNW_GLOB group identifier.  For family members, the
342911827SRod.Evans@Sun.COM 	 * ELF_C_GROUP provides an index to the associate capabilities group
343011827SRod.Evans@Sun.COM 	 * (i.e, an index into the SUNW_cap section that defines a group).
343111827SRod.Evans@Sun.COM 	 *
343211827SRod.Evans@Sun.COM 	 * For relocatable objects, the ELF_C_SYM field identifies the lead
343311827SRod.Evans@Sun.COM 	 * capability symbol.  For the lead symbol itself, the .SUNW_capinfo
343411827SRod.Evans@Sun.COM 	 * index is the same as the ELF_C_SYM value.  For lead alias symbols,
343511827SRod.Evans@Sun.COM 	 * the .SUNW_capinfo index differs from the ELF_C_SYM value.  This
343611827SRod.Evans@Sun.COM 	 * differentiation of CAPINFO_SUNW_GLOB symbols allows ld(1) to
343711827SRod.Evans@Sun.COM 	 * identify, and propagate lead alias symbols.  For example, the lead
343811827SRod.Evans@Sun.COM 	 * capability symbol memcpy() would have the ELF_C_SYM for memcpy(),
343911827SRod.Evans@Sun.COM 	 * and the lead alias _memcpy() would also have the ELF_C_SYM for
344011827SRod.Evans@Sun.COM 	 * memcpy().
344111827SRod.Evans@Sun.COM 	 *
344211827SRod.Evans@Sun.COM 	 * For dynamic objects, both a lead capability symbol, and alias symbol
344311827SRod.Evans@Sun.COM 	 * would have a ELF_C_SYM value that represents the same capability
344411827SRod.Evans@Sun.COM 	 * chain index.  The capability chain allows ld.so.1 to traverse a
344511827SRod.Evans@Sun.COM 	 * family chain for a given lead symbol, and select the most appropriate
344611827SRod.Evans@Sun.COM 	 * family member.  The .SUNW_capchain array contains a series of symbol
344711827SRod.Evans@Sun.COM 	 * indexes for each family member:
344811827SRod.Evans@Sun.COM 	 *
344911827SRod.Evans@Sun.COM 	 *    chaincap[n]  chaincap[n + 1]  chaincap[n + 2]  chaincap[n + x]
345011827SRod.Evans@Sun.COM 	 *	foo() ndx    foo%x() ndx	foo%y() ndx	0
345111827SRod.Evans@Sun.COM 	 *
345211827SRod.Evans@Sun.COM 	 * For family members, the ELF_C_SYM value associates the capability
345311827SRod.Evans@Sun.COM 	 * members with their family lead symbol.  This association, although
345411827SRod.Evans@Sun.COM 	 * unused within a dynamic object, allows ld(1) to identify, and
345511827SRod.Evans@Sun.COM 	 * propagate family members when processing relocatable objects.
345611827SRod.Evans@Sun.COM 	 */
345711827SRod.Evans@Sun.COM 	for (cav = avl_first(ofl->ofl_capfamilies); cav;
345811827SRod.Evans@Sun.COM 	    cav = AVL_NEXT(ofl->ofl_capfamilies, cav)) {
345911827SRod.Evans@Sun.COM 		Cap_sym		*csp;
346011827SRod.Evans@Sun.COM 		Aliste		idx;
346111827SRod.Evans@Sun.COM 		Sym_desc	*asdp, *lsdp = cav->cn_symavlnode.sav_sdp;
346211827SRod.Evans@Sun.COM 
346311827SRod.Evans@Sun.COM 		if (ccosp) {
346411827SRod.Evans@Sun.COM 			/*
346511827SRod.Evans@Sun.COM 			 * For a dynamic object, identify this lead symbol, and
346611827SRod.Evans@Sun.COM 			 * point it to the head of a capability chain.  Set the
346711827SRod.Evans@Sun.COM 			 * head of the capability chain to the same lead symbol.
346811827SRod.Evans@Sun.COM 			 */
346911827SRod.Evans@Sun.COM 			ocapinfo[lsdp->sd_symndx] =
347011827SRod.Evans@Sun.COM 			    ELF_C_INFO(chainndx, CAPINFO_SUNW_GLOB);
347111827SRod.Evans@Sun.COM 			ocapchain[chainndx] = lsdp->sd_symndx;
347211827SRod.Evans@Sun.COM 		} else {
347311827SRod.Evans@Sun.COM 			/*
347411827SRod.Evans@Sun.COM 			 * For a relocatable object, identify this lead symbol,
347511827SRod.Evans@Sun.COM 			 * and set the lead symbol index to itself.
347611827SRod.Evans@Sun.COM 			 */
347711827SRod.Evans@Sun.COM 			ocapinfo[lsdp->sd_symndx] =
347811827SRod.Evans@Sun.COM 			    ELF_C_INFO(lsdp->sd_symndx, CAPINFO_SUNW_GLOB);
347911827SRod.Evans@Sun.COM 		}
348011827SRod.Evans@Sun.COM 
348111827SRod.Evans@Sun.COM 		/*
348211827SRod.Evans@Sun.COM 		 * Gather any lead symbol aliases.
348311827SRod.Evans@Sun.COM 		 */
348411827SRod.Evans@Sun.COM 		for (APLIST_TRAVERSE(cav->cn_aliases, idx, asdp)) {
348511827SRod.Evans@Sun.COM 			if (ccosp) {
348611827SRod.Evans@Sun.COM 				/*
348711827SRod.Evans@Sun.COM 				 * For a dynamic object, identify this lead
348811827SRod.Evans@Sun.COM 				 * alias symbol, and point it to the same
348911827SRod.Evans@Sun.COM 				 * capability chain index as the lead symbol.
349011827SRod.Evans@Sun.COM 				 */
349111827SRod.Evans@Sun.COM 				ocapinfo[asdp->sd_symndx] =
349211827SRod.Evans@Sun.COM 				    ELF_C_INFO(chainndx, CAPINFO_SUNW_GLOB);
349311827SRod.Evans@Sun.COM 			} else {
349411827SRod.Evans@Sun.COM 				/*
349511827SRod.Evans@Sun.COM 				 * For a relocatable object, identify this lead
349611827SRod.Evans@Sun.COM 				 * alias symbol, and set the lead symbol index
349711827SRod.Evans@Sun.COM 				 * to the lead symbol.
349811827SRod.Evans@Sun.COM 				 */
349911827SRod.Evans@Sun.COM 				ocapinfo[asdp->sd_symndx] =
350011827SRod.Evans@Sun.COM 				    ELF_C_INFO(lsdp->sd_symndx,
350111827SRod.Evans@Sun.COM 				    CAPINFO_SUNW_GLOB);
350211827SRod.Evans@Sun.COM 			}
350311827SRod.Evans@Sun.COM 		}
350411827SRod.Evans@Sun.COM 
350511827SRod.Evans@Sun.COM 		chainndx++;
350611827SRod.Evans@Sun.COM 
350711827SRod.Evans@Sun.COM 		/*
350811827SRod.Evans@Sun.COM 		 * Gather the family members.
350911827SRod.Evans@Sun.COM 		 */
351011827SRod.Evans@Sun.COM 		for (APLIST_TRAVERSE(cav->cn_members, idx, csp)) {
351111827SRod.Evans@Sun.COM 			Sym_desc	*msdp = csp->cs_sdp;
351211827SRod.Evans@Sun.COM 
351311827SRod.Evans@Sun.COM 			/*
351411827SRod.Evans@Sun.COM 			 * Identify the members capability group, and the lead
351511827SRod.Evans@Sun.COM 			 * symbol of the family this symbol is a member of.
351611827SRod.Evans@Sun.COM 			 */
351711827SRod.Evans@Sun.COM 			ocapinfo[msdp->sd_symndx] =
351811827SRod.Evans@Sun.COM 			    ELF_C_INFO(lsdp->sd_symndx, csp->cs_group->cg_ndx);
351911827SRod.Evans@Sun.COM 			if (ccosp) {
352011827SRod.Evans@Sun.COM 				/*
352111827SRod.Evans@Sun.COM 				 * For a dynamic object, set the next capability
352211827SRod.Evans@Sun.COM 				 * chain to point to this family member.
352311827SRod.Evans@Sun.COM 				 */
352411827SRod.Evans@Sun.COM 				ocapchain[chainndx++] = msdp->sd_symndx;
352511827SRod.Evans@Sun.COM 			}
352611827SRod.Evans@Sun.COM 		}
352711827SRod.Evans@Sun.COM 
352811827SRod.Evans@Sun.COM 		/*
352911827SRod.Evans@Sun.COM 		 * Any chain of family members is terminated with a 0 element.
353011827SRod.Evans@Sun.COM 		 */
353111827SRod.Evans@Sun.COM 		if (ccosp)
353211827SRod.Evans@Sun.COM 			ocapchain[chainndx++] = 0;
353311827SRod.Evans@Sun.COM 	}
353411827SRod.Evans@Sun.COM }
353511827SRod.Evans@Sun.COM 
353611827SRod.Evans@Sun.COM /*
35370Sstevel@tonic-gate  * Translate the shdr->sh_{link, info} from its input section value to that
35380Sstevel@tonic-gate  * of the corresponding shdr->sh_{link, info} output section value.
35390Sstevel@tonic-gate  */
35401618Srie static Word
translate_link(Ofl_desc * ofl,Os_desc * osp,Word link,const char * msg)35411618Srie translate_link(Ofl_desc *ofl, Os_desc *osp, Word link, const char *msg)
35420Sstevel@tonic-gate {
35439131SRod.Evans@Sun.COM 	Is_desc		*isp;
35449131SRod.Evans@Sun.COM 	Ifl_desc	*ifl;
35450Sstevel@tonic-gate 
35460Sstevel@tonic-gate 	/*
35470Sstevel@tonic-gate 	 * Don't translate the special section numbers.
35480Sstevel@tonic-gate 	 */
35490Sstevel@tonic-gate 	if (link >= SHN_LORESERVE)
35500Sstevel@tonic-gate 		return (link);
35510Sstevel@tonic-gate 
35520Sstevel@tonic-gate 	/*
35530Sstevel@tonic-gate 	 * Does this output section translate back to an input file.  If not
35540Sstevel@tonic-gate 	 * then there is no translation to do.  In this case we will assume that
35550Sstevel@tonic-gate 	 * if sh_link has a value, it's the right value.
35560Sstevel@tonic-gate 	 */
35579615SAli.Bahrami@Sun.COM 	isp = ld_os_first_isdesc(osp);
35580Sstevel@tonic-gate 	if ((ifl = isp->is_file) == NULL)
35590Sstevel@tonic-gate 		return (link);
35600Sstevel@tonic-gate 
35610Sstevel@tonic-gate 	/*
35620Sstevel@tonic-gate 	 * Sanity check to make sure that the sh_{link, info} value
35630Sstevel@tonic-gate 	 * is within range for the input file.
35640Sstevel@tonic-gate 	 */
35650Sstevel@tonic-gate 	if (link >= ifl->ifl_shnum) {
3566*13074SAli.Bahrami@Oracle.COM 		ld_eprintf(ofl, ERR_WARNING, msg, ifl->ifl_name,
35679878SAli.Bahrami@Sun.COM 		    EC_WORD(isp->is_scnndx), isp->is_name, EC_XWORD(link));
35680Sstevel@tonic-gate 		return (link);
35690Sstevel@tonic-gate 	}
35700Sstevel@tonic-gate 
35710Sstevel@tonic-gate 	/*
35720Sstevel@tonic-gate 	 * Follow the link to the input section.
35730Sstevel@tonic-gate 	 */
35749131SRod.Evans@Sun.COM 	if ((isp = ifl->ifl_isdesc[link]) == NULL)
35750Sstevel@tonic-gate 		return (0);
35769131SRod.Evans@Sun.COM 	if ((osp = isp->is_osdesc) == NULL)
35770Sstevel@tonic-gate 		return (0);
35780Sstevel@tonic-gate 
35790Sstevel@tonic-gate 	/* LINTED */
35800Sstevel@tonic-gate 	return ((Word)elf_ndxscn(osp->os_scn));
35810Sstevel@tonic-gate }
35820Sstevel@tonic-gate 
35830Sstevel@tonic-gate /*
35840Sstevel@tonic-gate  * Having created all of the necessary sections, segments, and associated
35850Sstevel@tonic-gate  * headers, fill in the program headers and update any other data in the
35860Sstevel@tonic-gate  * output image.  Some general rules:
35870Sstevel@tonic-gate  *
35889131SRod.Evans@Sun.COM  *  -	If an interpreter is required always generate a PT_PHDR entry as
35890Sstevel@tonic-gate  *	well.  It is this entry that triggers the kernel into passing the
35902648Srie  *	interpreter an aux vector instead of just a file descriptor.
35910Sstevel@tonic-gate  *
35929131SRod.Evans@Sun.COM  *  -	When generating an image that will be interpreted (ie. a dynamic
35930Sstevel@tonic-gate  *	executable, a shared object, or a static executable that has been
35942648Srie  *	provided with an interpreter - weird, but possible), make the initial
35950Sstevel@tonic-gate  *	loadable segment include both the ehdr and phdr[].  Both of these
35962648Srie  *	tables are used by the interpreter therefore it seems more intuitive
35970Sstevel@tonic-gate  *	to explicitly defined them as part of the mapped image rather than
35982648Srie  *	relying on page rounding by the interpreter to allow their access.
35990Sstevel@tonic-gate  *
36009131SRod.Evans@Sun.COM  *  -	When generating a static image that does not require an interpreter
36010Sstevel@tonic-gate  *	have the first loadable segment indicate the address of the first
36020Sstevel@tonic-gate  *	.section as the start address (things like /kernel/unix and ufsboot
36030Sstevel@tonic-gate  *	expect this behavior).
36040Sstevel@tonic-gate  */
36050Sstevel@tonic-gate uintptr_t
ld_update_outfile(Ofl_desc * ofl)36061618Srie ld_update_outfile(Ofl_desc *ofl)
36070Sstevel@tonic-gate {
36088501SRod.Evans@Sun.COM 	Addr		size, etext, vaddr;
36099131SRod.Evans@Sun.COM 	Sg_desc		*sgp;
36109131SRod.Evans@Sun.COM 	Sg_desc		*dtracesgp = NULL, *capsgp = NULL, *intpsgp = NULL;
36115892Sab196087 	Os_desc		*osp;
36129131SRod.Evans@Sun.COM 	int		phdrndx = 0, segndx = -1, secndx, intppndx, intpsndx;
36134284Srie 	int		dtracepndx, dtracesndx, cappndx, capsndx;
36141618Srie 	Ehdr		*ehdr = ofl->ofl_nehdr;
36150Sstevel@tonic-gate 	Shdr		*hshdr;
36169131SRod.Evans@Sun.COM 	Phdr		*_phdr = NULL;
36174284Srie 	Word		phdrsz = (ehdr->e_phnum * ehdr->e_phentsize), shscnndx;
36186299Sab196087 	ofl_flag_t	flags = ofl->ofl_flags;
36196299Sab196087 	Word		ehdrsz = ehdr->e_ehsize;
36200Sstevel@tonic-gate 	Boolean		nobits;
36210Sstevel@tonic-gate 	Off		offset;
36229131SRod.Evans@Sun.COM 	Aliste		idx1;
36230Sstevel@tonic-gate 
36240Sstevel@tonic-gate 	/*
36258501SRod.Evans@Sun.COM 	 * Initialize the starting address for the first segment.  Executables
36268501SRod.Evans@Sun.COM 	 * have different starting addresses depending upon the target ABI,
36278501SRod.Evans@Sun.COM 	 * where as shared objects have a starting address of 0.  If this is
36288501SRod.Evans@Sun.COM 	 * a 64-bit executable that is being constructed to run in a restricted
36298501SRod.Evans@Sun.COM 	 * address space, use an alternative origin that will provide more free
36308501SRod.Evans@Sun.COM 	 * address space for the the eventual process.
36318501SRod.Evans@Sun.COM 	 */
36328501SRod.Evans@Sun.COM 	if (ofl->ofl_flags & FLG_OF_EXEC) {
36338501SRod.Evans@Sun.COM #if	defined(_ELF64)
363411827SRod.Evans@Sun.COM 		if (ofl->ofl_ocapset.oc_sf_1.cm_val & SF1_SUNW_ADDR32)
36358501SRod.Evans@Sun.COM 			vaddr = ld_targ.t_m.m_segm_aorigin;
36368501SRod.Evans@Sun.COM 		else
36378501SRod.Evans@Sun.COM #endif
36388501SRod.Evans@Sun.COM 			vaddr = ld_targ.t_m.m_segm_origin;
36398501SRod.Evans@Sun.COM 	} else
36408501SRod.Evans@Sun.COM 		vaddr = 0;
36418501SRod.Evans@Sun.COM 
36428501SRod.Evans@Sun.COM 	/*
36430Sstevel@tonic-gate 	 * Loop through the segment descriptors and pick out what we need.
36440Sstevel@tonic-gate 	 */
36451618Srie 	DBG_CALL(Dbg_seg_title(ofl->ofl_lml));
36469131SRod.Evans@Sun.COM 	for (APLIST_TRAVERSE(ofl->ofl_segs, idx1, sgp)) {
364711734SAli.Bahrami@Sun.COM 		Phdr		*phdr = &(sgp->sg_phdr);
364811734SAli.Bahrami@Sun.COM 		Xword 		p_align;
364911734SAli.Bahrami@Sun.COM 		Aliste		idx2;
365011734SAli.Bahrami@Sun.COM 		Sym_desc	*sdp;
36510Sstevel@tonic-gate 
36520Sstevel@tonic-gate 		segndx++;
36530Sstevel@tonic-gate 
36540Sstevel@tonic-gate 		/*
36550Sstevel@tonic-gate 		 * If an interpreter is required generate a PT_INTERP and
36560Sstevel@tonic-gate 		 * PT_PHDR program header entry.  The PT_PHDR entry describes
36570Sstevel@tonic-gate 		 * the program header table itself.  This information will be
36580Sstevel@tonic-gate 		 * passed via the aux vector to the interpreter (ld.so.1).
36590Sstevel@tonic-gate 		 * The program header array is actually part of the first
36600Sstevel@tonic-gate 		 * loadable segment (and the PT_PHDR entry is the first entry),
36610Sstevel@tonic-gate 		 * therefore its virtual address isn't known until the first
36620Sstevel@tonic-gate 		 * loadable segment is processed.
36630Sstevel@tonic-gate 		 */
36640Sstevel@tonic-gate 		if (phdr->p_type == PT_PHDR) {
36650Sstevel@tonic-gate 			if (ofl->ofl_osinterp) {
36660Sstevel@tonic-gate 				phdr->p_offset = ehdr->e_phoff;
36670Sstevel@tonic-gate 				phdr->p_filesz = phdr->p_memsz = phdrsz;
36684284Srie 
36691618Srie 				DBG_CALL(Dbg_seg_entry(ofl, segndx, sgp));
36700Sstevel@tonic-gate 				ofl->ofl_phdr[phdrndx++] = *phdr;
36710Sstevel@tonic-gate 			}
36720Sstevel@tonic-gate 			continue;
36730Sstevel@tonic-gate 		}
36740Sstevel@tonic-gate 		if (phdr->p_type == PT_INTERP) {
36750Sstevel@tonic-gate 			if (ofl->ofl_osinterp) {
36769131SRod.Evans@Sun.COM 				intpsgp = sgp;
36779131SRod.Evans@Sun.COM 				intpsndx = segndx;
36789131SRod.Evans@Sun.COM 				intppndx = phdrndx++;
36790Sstevel@tonic-gate 			}
36800Sstevel@tonic-gate 			continue;
36810Sstevel@tonic-gate 		}
36820Sstevel@tonic-gate 
36830Sstevel@tonic-gate 		/*
36844284Srie 		 * If we are creating a PT_SUNWDTRACE segment, remember where
36854284Srie 		 * the program header is.  The header values are assigned after
36864284Srie 		 * update_osym() has completed and the symbol table addresses
368710167SRod.Evans@Sun.COM 		 * have been updated.
36880Sstevel@tonic-gate 		 */
36890Sstevel@tonic-gate 		if (phdr->p_type == PT_SUNWDTRACE) {
369010167SRod.Evans@Sun.COM 			if (ofl->ofl_dtracesym &&
36910Sstevel@tonic-gate 			    ((flags & FLG_OF_RELOBJ) == 0)) {
36924284Srie 				dtracesgp = sgp;
36934284Srie 				dtracesndx = segndx;
36944284Srie 				dtracepndx = phdrndx++;
36950Sstevel@tonic-gate 			}
36960Sstevel@tonic-gate 			continue;
36970Sstevel@tonic-gate 		}
36980Sstevel@tonic-gate 
36990Sstevel@tonic-gate 		/*
37000Sstevel@tonic-gate 		 * If a hardware/software capabilities section is required,
37010Sstevel@tonic-gate 		 * generate the PT_SUNWCAP header.  Note, as this comes before
37020Sstevel@tonic-gate 		 * the first loadable segment, we don't yet know its real
37030Sstevel@tonic-gate 		 * virtual address.  This is updated later.
37040Sstevel@tonic-gate 		 */
37050Sstevel@tonic-gate 		if (phdr->p_type == PT_SUNWCAP) {
370611827SRod.Evans@Sun.COM 			if (ofl->ofl_oscap && (ofl->ofl_flags & FLG_OF_PTCAP) &&
370711827SRod.Evans@Sun.COM 			    ((flags & FLG_OF_RELOBJ) == 0)) {
37084284Srie 				capsgp = sgp;
37094284Srie 				capsndx = segndx;
37104284Srie 				cappndx = phdrndx++;
37110Sstevel@tonic-gate 			}
37120Sstevel@tonic-gate 			continue;
37130Sstevel@tonic-gate 		}
37140Sstevel@tonic-gate 
37150Sstevel@tonic-gate 		/*
37160Sstevel@tonic-gate 		 * As the dynamic program header occurs after the loadable
37170Sstevel@tonic-gate 		 * headers in the segment descriptor table, all the address
37180Sstevel@tonic-gate 		 * information for the .dynamic output section will have been
37190Sstevel@tonic-gate 		 * figured out by now.
37200Sstevel@tonic-gate 		 */
37210Sstevel@tonic-gate 		if (phdr->p_type == PT_DYNAMIC) {
37222766Sab196087 			if (OFL_ALLOW_DYNSYM(ofl)) {
37234284Srie 				Shdr	*shdr = ofl->ofl_osdynamic->os_shdr;
37240Sstevel@tonic-gate 
37250Sstevel@tonic-gate 				phdr->p_vaddr = shdr->sh_addr;
37260Sstevel@tonic-gate 				phdr->p_offset = shdr->sh_offset;
37270Sstevel@tonic-gate 				phdr->p_filesz = shdr->sh_size;
37286206Sab196087 				phdr->p_flags = ld_targ.t_m.m_dataseg_perm;
37294284Srie 
37301618Srie 				DBG_CALL(Dbg_seg_entry(ofl, segndx, sgp));
37310Sstevel@tonic-gate 				ofl->ofl_phdr[phdrndx++] = *phdr;
37320Sstevel@tonic-gate 			}
37330Sstevel@tonic-gate 			continue;
37340Sstevel@tonic-gate 		}
37354284Srie 
37364284Srie 		/*
37379085SAli.Bahrami@Sun.COM 		 * As the unwind (.eh_frame_hdr) program header occurs after
37389085SAli.Bahrami@Sun.COM 		 * the loadable headers in the segment descriptor table, all
37399085SAli.Bahrami@Sun.COM 		 * the address information for the .eh_frame output section
37409085SAli.Bahrami@Sun.COM 		 * will have been figured out by now.
37414284Srie 		 */
37429085SAli.Bahrami@Sun.COM 		if (phdr->p_type == PT_SUNW_UNWIND) {
37430Sstevel@tonic-gate 			Shdr	    *shdr;
37444284Srie 
37459085SAli.Bahrami@Sun.COM 			if (ofl->ofl_unwindhdr == NULL)
37460Sstevel@tonic-gate 				continue;
37474284Srie 
37480Sstevel@tonic-gate 			shdr = ofl->ofl_unwindhdr->os_shdr;
37490Sstevel@tonic-gate 
37500Sstevel@tonic-gate 			phdr->p_flags = PF_R;
37510Sstevel@tonic-gate 			phdr->p_vaddr = shdr->sh_addr;
37520Sstevel@tonic-gate 			phdr->p_memsz = shdr->sh_size;
37530Sstevel@tonic-gate 			phdr->p_filesz = shdr->sh_size;
37540Sstevel@tonic-gate 			phdr->p_offset = shdr->sh_offset;
37550Sstevel@tonic-gate 			phdr->p_align = shdr->sh_addralign;
37560Sstevel@tonic-gate 			phdr->p_paddr = 0;
37570Sstevel@tonic-gate 			ofl->ofl_phdr[phdrndx++] = *phdr;
37580Sstevel@tonic-gate 			continue;
37590Sstevel@tonic-gate 		}
37609085SAli.Bahrami@Sun.COM 
37614284Srie 		/*
376211734SAli.Bahrami@Sun.COM 		 * The sunwstack program is used to convey non-default
376311734SAli.Bahrami@Sun.COM 		 * flags for the process stack. Only emit it if it would
376411734SAli.Bahrami@Sun.COM 		 * change the default.
376511734SAli.Bahrami@Sun.COM 		 */
376611734SAli.Bahrami@Sun.COM 		if (phdr->p_type == PT_SUNWSTACK) {
376711828SAli.Bahrami@Sun.COM 			if (((flags & FLG_OF_RELOBJ) == 0) &&
376811828SAli.Bahrami@Sun.COM 			    ((sgp->sg_flags & FLG_SG_DISABLED) == 0))
376911734SAli.Bahrami@Sun.COM 				ofl->ofl_phdr[phdrndx++] = *phdr;
377011734SAli.Bahrami@Sun.COM 			continue;
377111734SAli.Bahrami@Sun.COM 		}
377211734SAli.Bahrami@Sun.COM 
377311734SAli.Bahrami@Sun.COM 		/*
37744284Srie 		 * As the TLS program header occurs after the loadable
37754284Srie 		 * headers in the segment descriptor table, all the address
37764284Srie 		 * information for the .tls output section will have been
37774284Srie 		 * figured out by now.
37784284Srie 		 */
37790Sstevel@tonic-gate 		if (phdr->p_type == PT_TLS) {
37809131SRod.Evans@Sun.COM 			Os_desc		*tlsosp;
378111827SRod.Evans@Sun.COM 			Shdr		*lastfileshdr = NULL;
378211827SRod.Evans@Sun.COM 			Shdr		*firstshdr = NULL, *lastshdr;
37839131SRod.Evans@Sun.COM 			Aliste		idx;
37849131SRod.Evans@Sun.COM 
37859131SRod.Evans@Sun.COM 			if (ofl->ofl_ostlsseg == NULL)
37860Sstevel@tonic-gate 				continue;
3787542Srie 
37884234Srie 			/*
378911827SRod.Evans@Sun.COM 			 * Scan the output sections that have contributed TLS.
37904234Srie 			 * Remember the first and last so as to determine the
37914234Srie 			 * TLS memory size requirement.  Remember the last
379211827SRod.Evans@Sun.COM 			 * progbits section to determine the TLS data
379311827SRod.Evans@Sun.COM 			 * contribution, which determines the TLS program
379411827SRod.Evans@Sun.COM 			 * header filesz.
37954234Srie 			 */
37969131SRod.Evans@Sun.COM 			for (APLIST_TRAVERSE(ofl->ofl_ostlsseg, idx, tlsosp)) {
3797542Srie 				Shdr	*tlsshdr = tlsosp->os_shdr;
3798542Srie 
37999131SRod.Evans@Sun.COM 				if (firstshdr == NULL)
38004234Srie 					firstshdr = tlsshdr;
38014234Srie 				if (tlsshdr->sh_type != SHT_NOBITS)
38024234Srie 					lastfileshdr = tlsshdr;
38034234Srie 				lastshdr = tlsshdr;
3804542Srie 			}
3805542Srie 
38060Sstevel@tonic-gate 			phdr->p_flags = PF_R | PF_W;
38070Sstevel@tonic-gate 			phdr->p_vaddr = firstshdr->sh_addr;
38080Sstevel@tonic-gate 			phdr->p_offset = firstshdr->sh_offset;
3809542Srie 			phdr->p_align = firstshdr->sh_addralign;
38104234Srie 
381111827SRod.Evans@Sun.COM 			/*
381211827SRod.Evans@Sun.COM 			 * Determine the initialized TLS data size.  This
381311827SRod.Evans@Sun.COM 			 * address range is from the start of the TLS segment
381411827SRod.Evans@Sun.COM 			 * to the end of the last piece of initialized data.
381511827SRod.Evans@Sun.COM 			 */
38164234Srie 			if (lastfileshdr)
38174234Srie 				phdr->p_filesz = lastfileshdr->sh_offset +
38184234Srie 				    lastfileshdr->sh_size - phdr->p_offset;
38194234Srie 			else
38204234Srie 				phdr->p_filesz = 0;
38214234Srie 
382211827SRod.Evans@Sun.COM 			/*
382311827SRod.Evans@Sun.COM 			 * Determine the total TLS memory size.  This includes
382411827SRod.Evans@Sun.COM 			 * all TLS data and TLS uninitialized data.  This
382511827SRod.Evans@Sun.COM 			 * address range is from the start of the TLS segment
382611827SRod.Evans@Sun.COM 			 * to the memory address of the last piece of
382711827SRod.Evans@Sun.COM 			 * uninitialized data.
382811827SRod.Evans@Sun.COM 			 */
382911827SRod.Evans@Sun.COM 			phdr->p_memsz = lastshdr->sh_addr +
383011827SRod.Evans@Sun.COM 			    lastshdr->sh_size - phdr->p_vaddr;
3831542Srie 
38321618Srie 			DBG_CALL(Dbg_seg_entry(ofl, segndx, sgp));
38334284Srie 			ofl->ofl_phdr[phdrndx] = *phdr;
38344284Srie 			ofl->ofl_tlsphdr = &ofl->ofl_phdr[phdrndx++];
38350Sstevel@tonic-gate 			continue;
38360Sstevel@tonic-gate 		}
38370Sstevel@tonic-gate 
38380Sstevel@tonic-gate 		/*
38390Sstevel@tonic-gate 		 * If this is an empty segment declaration, it will occur after
38404284Srie 		 * all other loadable segments.  As empty segments can be
384111827SRod.Evans@Sun.COM 		 * defined with fixed addresses, make sure that no loadable
38424284Srie 		 * segments overlap.  This might occur as the object evolves
38434284Srie 		 * and the loadable segments grow, thus encroaching upon an
38444284Srie 		 * existing segment reservation.
38454284Srie 		 *
38464284Srie 		 * Segments are only created for dynamic objects, thus this
38474284Srie 		 * checking can be skipped when building a relocatable object.
38480Sstevel@tonic-gate 		 */
38496299Sab196087 		if (!(flags & FLG_OF_RELOBJ) &&
38500Sstevel@tonic-gate 		    (sgp->sg_flags & FLG_SG_EMPTY)) {
38514284Srie 			int	i;
38520Sstevel@tonic-gate 			Addr	v_e;
38530Sstevel@tonic-gate 
38540Sstevel@tonic-gate 			vaddr = phdr->p_vaddr;
38550Sstevel@tonic-gate 			phdr->p_memsz = sgp->sg_length;
38561618Srie 			DBG_CALL(Dbg_seg_entry(ofl, segndx, sgp));
38570Sstevel@tonic-gate 			ofl->ofl_phdr[phdrndx++] = *phdr;
38580Sstevel@tonic-gate 
38590Sstevel@tonic-gate 			if (phdr->p_type != PT_LOAD)
38600Sstevel@tonic-gate 				continue;
38610Sstevel@tonic-gate 
38620Sstevel@tonic-gate 			v_e = vaddr + phdr->p_memsz;
38634284Srie 
38640Sstevel@tonic-gate 			/*
38650Sstevel@tonic-gate 			 * Check overlaps
38660Sstevel@tonic-gate 			 */
38670Sstevel@tonic-gate 			for (i = 0; i < phdrndx - 1; i++) {
38680Sstevel@tonic-gate 				Addr 	p_s = (ofl->ofl_phdr[i]).p_vaddr;
38690Sstevel@tonic-gate 				Addr 	p_e;
38700Sstevel@tonic-gate 
38710Sstevel@tonic-gate 				if ((ofl->ofl_phdr[i]).p_type != PT_LOAD)
38720Sstevel@tonic-gate 					continue;
38730Sstevel@tonic-gate 
38740Sstevel@tonic-gate 				p_e = p_s + (ofl->ofl_phdr[i]).p_memsz;
38750Sstevel@tonic-gate 				if (((p_s <= vaddr) && (p_e > vaddr)) ||
38760Sstevel@tonic-gate 				    ((vaddr <= p_s) && (v_e > p_s)))
3877*13074SAli.Bahrami@Oracle.COM 					ld_eprintf(ofl, ERR_WARNING,
38780Sstevel@tonic-gate 					    MSG_INTL(MSG_UPD_SEGOVERLAP),
38791618Srie 					    ofl->ofl_name, EC_ADDR(p_e),
38801618Srie 					    sgp->sg_name, EC_ADDR(vaddr));
38810Sstevel@tonic-gate 			}
38820Sstevel@tonic-gate 			continue;
38830Sstevel@tonic-gate 		}
38840Sstevel@tonic-gate 
38850Sstevel@tonic-gate 		/*
38860Sstevel@tonic-gate 		 * Having processed any of the special program headers any
38870Sstevel@tonic-gate 		 * remaining headers will be built to express individual
38880Sstevel@tonic-gate 		 * segments.  Segments are only built if they have output
38890Sstevel@tonic-gate 		 * section descriptors associated with them (ie. some form of
38900Sstevel@tonic-gate 		 * input section has been matched to this segment).
38910Sstevel@tonic-gate 		 */
38921682Srie 		if (sgp->sg_osdescs == NULL)
38930Sstevel@tonic-gate 			continue;
38940Sstevel@tonic-gate 
38950Sstevel@tonic-gate 		/*
38960Sstevel@tonic-gate 		 * Determine the segments offset and size from the section
38970Sstevel@tonic-gate 		 * information provided from elf_update().
38980Sstevel@tonic-gate 		 * Allow for multiple NOBITS sections.
38990Sstevel@tonic-gate 		 */
39005892Sab196087 		osp = sgp->sg_osdescs->apl_data[0];
3901574Sseizo 		hshdr = osp->os_shdr;
39020Sstevel@tonic-gate 
39030Sstevel@tonic-gate 		phdr->p_filesz = 0;
39040Sstevel@tonic-gate 		phdr->p_memsz = 0;
39050Sstevel@tonic-gate 		phdr->p_offset = offset = hshdr->sh_offset;
39061682Srie 
3907574Sseizo 		nobits = ((hshdr->sh_type == SHT_NOBITS) &&
39081682Srie 		    ((sgp->sg_flags & FLG_SG_PHREQ) == 0));
39091682Srie 
39109131SRod.Evans@Sun.COM 		for (APLIST_TRAVERSE(sgp->sg_osdescs, idx2, osp)) {
39115892Sab196087 			Shdr	*shdr = osp->os_shdr;
39120Sstevel@tonic-gate 
39130Sstevel@tonic-gate 			p_align = 0;
39140Sstevel@tonic-gate 			if (shdr->sh_addralign > p_align)
39150Sstevel@tonic-gate 				p_align = shdr->sh_addralign;
39161682Srie 
39170Sstevel@tonic-gate 			offset = (Off)S_ROUND(offset, shdr->sh_addralign);
39180Sstevel@tonic-gate 			offset += shdr->sh_size;
39191682Srie 
39200Sstevel@tonic-gate 			if (shdr->sh_type != SHT_NOBITS) {
39210Sstevel@tonic-gate 				if (nobits) {
3922*13074SAli.Bahrami@Oracle.COM 					ld_eprintf(ofl, ERR_FATAL,
39230Sstevel@tonic-gate 					    MSG_INTL(MSG_UPD_NOBITS));
39240Sstevel@tonic-gate 					return (S_ERROR);
39250Sstevel@tonic-gate 				}
39260Sstevel@tonic-gate 				phdr->p_filesz = offset - phdr->p_offset;
3927574Sseizo 			} else if ((sgp->sg_flags & FLG_SG_PHREQ) == 0)
39280Sstevel@tonic-gate 				nobits = TRUE;
39290Sstevel@tonic-gate 		}
39300Sstevel@tonic-gate 		phdr->p_memsz = offset - hshdr->sh_offset;
39310Sstevel@tonic-gate 
39320Sstevel@tonic-gate 		/*
39330Sstevel@tonic-gate 		 * If this is the first loadable segment of a dynamic object,
39342648Srie 		 * or an interpreter has been specified (a static object built
39352648Srie 		 * with an interpreter will still be given a PT_HDR entry), then
39360Sstevel@tonic-gate 		 * compensate for the elf header and program header array.  Both
39370Sstevel@tonic-gate 		 * of these are actually part of the loadable segment as they
39382648Srie 		 * may be inspected by the interpreter.  Adjust the segments
39390Sstevel@tonic-gate 		 * size and offset accordingly.
39400Sstevel@tonic-gate 		 */
39419131SRod.Evans@Sun.COM 		if ((_phdr == NULL) && (phdr->p_type == PT_LOAD) &&
39420Sstevel@tonic-gate 		    ((ofl->ofl_osinterp) || (flags & FLG_OF_DYNAMIC)) &&
39431698Sab196087 		    (!(ofl->ofl_dtflags_1 & DF_1_NOHDR))) {
39440Sstevel@tonic-gate 			size = (Addr)S_ROUND((phdrsz + ehdrsz),
39450Sstevel@tonic-gate 			    hshdr->sh_addralign);
39460Sstevel@tonic-gate 			phdr->p_offset -= size;
39470Sstevel@tonic-gate 			phdr->p_filesz += size;
39480Sstevel@tonic-gate 			phdr->p_memsz += size;
39490Sstevel@tonic-gate 		}
39500Sstevel@tonic-gate 
39510Sstevel@tonic-gate 		/*
395211734SAli.Bahrami@Sun.COM 		 * If segment size symbols are required (specified via a
395311734SAli.Bahrami@Sun.COM 		 * mapfile) update their value.
39540Sstevel@tonic-gate 		 */
395511734SAli.Bahrami@Sun.COM 		for (APLIST_TRAVERSE(sgp->sg_sizesym, idx2, sdp))
395611734SAli.Bahrami@Sun.COM 			sdp->sd_sym->st_value = phdr->p_memsz;
39570Sstevel@tonic-gate 
39580Sstevel@tonic-gate 		/*
39590Sstevel@tonic-gate 		 * If no file content has been assigned to this segment (it
39600Sstevel@tonic-gate 		 * only contains no-bits sections), then reset the offset for
39610Sstevel@tonic-gate 		 * consistency.
39620Sstevel@tonic-gate 		 */
39630Sstevel@tonic-gate 		if (phdr->p_filesz == 0)
39640Sstevel@tonic-gate 			phdr->p_offset = 0;
39650Sstevel@tonic-gate 
39660Sstevel@tonic-gate 		/*
39670Sstevel@tonic-gate 		 * If a virtual address has been specified for this segment
396811734SAli.Bahrami@Sun.COM 		 * from a mapfile use it and make sure the previous segment
396911734SAli.Bahrami@Sun.COM 		 * does not run into this segment.
39700Sstevel@tonic-gate 		 */
39718159SAli.Bahrami@Sun.COM 		if (phdr->p_type == PT_LOAD) {
397211734SAli.Bahrami@Sun.COM 			if ((sgp->sg_flags & FLG_SG_P_VADDR)) {
39730Sstevel@tonic-gate 				if (_phdr && (vaddr > phdr->p_vaddr) &&
39740Sstevel@tonic-gate 				    (phdr->p_type == PT_LOAD))
3975*13074SAli.Bahrami@Oracle.COM 					ld_eprintf(ofl, ERR_WARNING,
39760Sstevel@tonic-gate 					    MSG_INTL(MSG_UPD_SEGOVERLAP),
39770Sstevel@tonic-gate 					    ofl->ofl_name, EC_ADDR(vaddr),
39780Sstevel@tonic-gate 					    sgp->sg_name,
39790Sstevel@tonic-gate 					    EC_ADDR(phdr->p_vaddr));
39800Sstevel@tonic-gate 				vaddr = phdr->p_vaddr;
39810Sstevel@tonic-gate 				phdr->p_align = 0;
39820Sstevel@tonic-gate 			} else {
39830Sstevel@tonic-gate 				vaddr = phdr->p_vaddr =
39840Sstevel@tonic-gate 				    (Addr)S_ROUND(vaddr, phdr->p_align);
39850Sstevel@tonic-gate 			}
39860Sstevel@tonic-gate 		}
39870Sstevel@tonic-gate 
39880Sstevel@tonic-gate 		/*
39890Sstevel@tonic-gate 		 * Adjust the address offset and p_align if needed.
39900Sstevel@tonic-gate 		 */
399111734SAli.Bahrami@Sun.COM 		if (((sgp->sg_flags & FLG_SG_P_VADDR) == 0) &&
39922978Srie 		    ((ofl->ofl_dtflags_1 & DF_1_NOHDR) == 0)) {
39930Sstevel@tonic-gate 			if (phdr->p_align != 0)
39940Sstevel@tonic-gate 				vaddr += phdr->p_offset % phdr->p_align;
39950Sstevel@tonic-gate 			else
39960Sstevel@tonic-gate 				vaddr += phdr->p_offset;
39970Sstevel@tonic-gate 			phdr->p_vaddr = vaddr;
39980Sstevel@tonic-gate 		}
39990Sstevel@tonic-gate 
40000Sstevel@tonic-gate 		/*
40010Sstevel@tonic-gate 		 * If an interpreter is required set the virtual address of the
40020Sstevel@tonic-gate 		 * PT_PHDR program header now that we know the virtual address
40030Sstevel@tonic-gate 		 * of the loadable segment that contains it.  Update the
40040Sstevel@tonic-gate 		 * PT_SUNWCAP header similarly.
40050Sstevel@tonic-gate 		 */
40069131SRod.Evans@Sun.COM 		if ((_phdr == NULL) && (phdr->p_type == PT_LOAD)) {
40070Sstevel@tonic-gate 			_phdr = phdr;
40080Sstevel@tonic-gate 
40094284Srie 			if ((ofl->ofl_dtflags_1 & DF_1_NOHDR) == 0) {
40100Sstevel@tonic-gate 				if (ofl->ofl_osinterp)
40110Sstevel@tonic-gate 					ofl->ofl_phdr[0].p_vaddr =
40120Sstevel@tonic-gate 					    vaddr + ehdrsz;
40130Sstevel@tonic-gate 
40140Sstevel@tonic-gate 				/*
40150Sstevel@tonic-gate 				 * Finally, if we're creating a dynamic object
40162648Srie 				 * (or a static object in which an interpreter
40170Sstevel@tonic-gate 				 * is specified) update the vaddr to reflect
40180Sstevel@tonic-gate 				 * the address of the first section within this
40190Sstevel@tonic-gate 				 * segment.
40200Sstevel@tonic-gate 				 */
40210Sstevel@tonic-gate 				if ((ofl->ofl_osinterp) ||
40220Sstevel@tonic-gate 				    (flags & FLG_OF_DYNAMIC))
40230Sstevel@tonic-gate 					vaddr += size;
40240Sstevel@tonic-gate 			} else {
40250Sstevel@tonic-gate 				/*
40264284Srie 				 * If the DF_1_NOHDR flag was set, and an
40274284Srie 				 * interpreter is being generated, the PT_PHDR
40280Sstevel@tonic-gate 				 * will not be part of any loadable segment.
40290Sstevel@tonic-gate 				 */
40304284Srie 				if (ofl->ofl_osinterp) {
40314284Srie 					ofl->ofl_phdr[0].p_vaddr = 0;
40324284Srie 					ofl->ofl_phdr[0].p_memsz = 0;
40334284Srie 					ofl->ofl_phdr[0].p_flags = 0;
40344284Srie 				}
40350Sstevel@tonic-gate 			}
40360Sstevel@tonic-gate 		}
40370Sstevel@tonic-gate 
40380Sstevel@tonic-gate 		/*
40392347Srie 		 * Ensure the ELF entry point defaults to zero.  Typically, this
40402347Srie 		 * value is overridden in update_oehdr() to one of the standard
40412347Srie 		 * entry points.  Historically, this default was set to the
40422347Srie 		 * address of first executable section, but this has since been
40432347Srie 		 * found to be more confusing than it is helpful.
40440Sstevel@tonic-gate 		 */
40452347Srie 		ehdr->e_entry = 0;
40460Sstevel@tonic-gate 
40471618Srie 		DBG_CALL(Dbg_seg_entry(ofl, segndx, sgp));
40480Sstevel@tonic-gate 
40490Sstevel@tonic-gate 		/*
40500Sstevel@tonic-gate 		 * Traverse the output section descriptors for this segment so
40510Sstevel@tonic-gate 		 * that we can update the section headers addresses.  We've
40520Sstevel@tonic-gate 		 * calculated the virtual address of the initial section within
40530Sstevel@tonic-gate 		 * this segment, so each successive section can be calculated
40540Sstevel@tonic-gate 		 * based on their offsets from each other.
40550Sstevel@tonic-gate 		 */
40560Sstevel@tonic-gate 		secndx = 0;
40570Sstevel@tonic-gate 		hshdr = 0;
40589131SRod.Evans@Sun.COM 		for (APLIST_TRAVERSE(sgp->sg_osdescs, idx2, osp)) {
40595892Sab196087 			Shdr	*shdr = osp->os_shdr;
40600Sstevel@tonic-gate 
40610Sstevel@tonic-gate 			if (shdr->sh_link)
40625220Srie 				shdr->sh_link = translate_link(ofl, osp,
40635220Srie 				    shdr->sh_link, MSG_INTL(MSG_FIL_INVSHLINK));
40640Sstevel@tonic-gate 
40650Sstevel@tonic-gate 			if (shdr->sh_info && (shdr->sh_flags & SHF_INFO_LINK))
40665220Srie 				shdr->sh_info = translate_link(ofl, osp,
40675220Srie 				    shdr->sh_info, MSG_INTL(MSG_FIL_INVSHINFO));
40680Sstevel@tonic-gate 
40690Sstevel@tonic-gate 			if (!(flags & FLG_OF_RELOBJ) &&
40708159SAli.Bahrami@Sun.COM 			    (phdr->p_type == PT_LOAD)) {
40710Sstevel@tonic-gate 				if (hshdr)
40720Sstevel@tonic-gate 					vaddr += (shdr->sh_offset -
40730Sstevel@tonic-gate 					    hshdr->sh_offset);
40740Sstevel@tonic-gate 
40750Sstevel@tonic-gate 				shdr->sh_addr = vaddr;
40760Sstevel@tonic-gate 				hshdr = shdr;
40770Sstevel@tonic-gate 			}
40780Sstevel@tonic-gate 
40790Sstevel@tonic-gate 			DBG_CALL(Dbg_seg_os(ofl, osp, secndx));
40800Sstevel@tonic-gate 			secndx++;
40810Sstevel@tonic-gate 		}
40820Sstevel@tonic-gate 
40830Sstevel@tonic-gate 		/*
40840Sstevel@tonic-gate 		 * Establish the virtual address of the end of the last section
40850Sstevel@tonic-gate 		 * in this segment so that the next segments offset can be
40860Sstevel@tonic-gate 		 * calculated from this.
40870Sstevel@tonic-gate 		 */
40880Sstevel@tonic-gate 		if (hshdr)
40890Sstevel@tonic-gate 			vaddr += hshdr->sh_size;
40900Sstevel@tonic-gate 
40910Sstevel@tonic-gate 		/*
40920Sstevel@tonic-gate 		 * Output sections for this segment complete.  Adjust the
40930Sstevel@tonic-gate 		 * virtual offset for the last sections size, and make sure we
40940Sstevel@tonic-gate 		 * haven't exceeded any maximum segment length specification.
40950Sstevel@tonic-gate 		 */
40960Sstevel@tonic-gate 		if ((sgp->sg_length != 0) && (sgp->sg_length < phdr->p_memsz)) {
4097*13074SAli.Bahrami@Oracle.COM 			ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_UPD_LARGSIZE),
4098*13074SAli.Bahrami@Oracle.COM 			    ofl->ofl_name, sgp->sg_name,
4099*13074SAli.Bahrami@Oracle.COM 			    EC_XWORD(phdr->p_memsz), EC_XWORD(sgp->sg_length));
41000Sstevel@tonic-gate 			return (S_ERROR);
41010Sstevel@tonic-gate 		}
41020Sstevel@tonic-gate 
41030Sstevel@tonic-gate 		if (phdr->p_type == PT_NOTE) {
41040Sstevel@tonic-gate 			phdr->p_vaddr = 0;
41050Sstevel@tonic-gate 			phdr->p_paddr = 0;
41060Sstevel@tonic-gate 			phdr->p_align = 0;
41070Sstevel@tonic-gate 			phdr->p_memsz = 0;
41080Sstevel@tonic-gate 		}
41094284Srie 
41100Sstevel@tonic-gate 		if ((phdr->p_type != PT_NULL) && !(flags & FLG_OF_RELOBJ))
41110Sstevel@tonic-gate 			ofl->ofl_phdr[phdrndx++] = *phdr;
41120Sstevel@tonic-gate 	}
41130Sstevel@tonic-gate 
41140Sstevel@tonic-gate 	/*
41150Sstevel@tonic-gate 	 * Update any new output sections.  When building the initial output
41160Sstevel@tonic-gate 	 * image, a number of sections were created but left uninitialized (eg.
41170Sstevel@tonic-gate 	 * .dynsym, .dynstr, .symtab, .symtab, etc.).  Here we update these
41180Sstevel@tonic-gate 	 * sections with the appropriate data.  Other sections may still be
41190Sstevel@tonic-gate 	 * modified via reloc_process().
41200Sstevel@tonic-gate 	 *
41212648Srie 	 * Copy the interpreter name into the .interp section.
41220Sstevel@tonic-gate 	 */
41230Sstevel@tonic-gate 	if (ofl->ofl_interp)
41240Sstevel@tonic-gate 		(void) strcpy((char *)ofl->ofl_osinterp->os_outdata->d_buf,
41250Sstevel@tonic-gate 		    ofl->ofl_interp);
41260Sstevel@tonic-gate 
41270Sstevel@tonic-gate 	/*
41280Sstevel@tonic-gate 	 * Update the .shstrtab, .strtab and .dynstr sections.
41290Sstevel@tonic-gate 	 */
41303850Sab196087 	update_ostrtab(ofl->ofl_osshstrtab, ofl->ofl_shdrsttab, 0);
41313850Sab196087 	update_ostrtab(ofl->ofl_osstrtab, ofl->ofl_strtab, 0);
41323850Sab196087 	update_ostrtab(ofl->ofl_osdynstr, ofl->ofl_dynstrtab, DYNSTR_EXTRA_PAD);
41330Sstevel@tonic-gate 
41340Sstevel@tonic-gate 	/*
41350Sstevel@tonic-gate 	 * Build any output symbol tables, the symbols information is copied
41360Sstevel@tonic-gate 	 * and updated into the new output image.
41370Sstevel@tonic-gate 	 */
41380Sstevel@tonic-gate 	if ((etext = update_osym(ofl)) == (Addr)S_ERROR)
41390Sstevel@tonic-gate 		return (S_ERROR);
41400Sstevel@tonic-gate 
41410Sstevel@tonic-gate 	/*
41429131SRod.Evans@Sun.COM 	 * If we have an PT_INTERP phdr, update it now from the associated
41439131SRod.Evans@Sun.COM 	 * section information.
41449131SRod.Evans@Sun.COM 	 */
41459131SRod.Evans@Sun.COM 	if (intpsgp) {
41469131SRod.Evans@Sun.COM 		Phdr	*phdr = &(intpsgp->sg_phdr);
41479131SRod.Evans@Sun.COM 		Shdr	*shdr = ofl->ofl_osinterp->os_shdr;
41489131SRod.Evans@Sun.COM 
41499131SRod.Evans@Sun.COM 		phdr->p_vaddr = shdr->sh_addr;
41509131SRod.Evans@Sun.COM 		phdr->p_offset = shdr->sh_offset;
41519131SRod.Evans@Sun.COM 		phdr->p_memsz = phdr->p_filesz = shdr->sh_size;
41529131SRod.Evans@Sun.COM 		phdr->p_flags = PF_R;
41539131SRod.Evans@Sun.COM 
41549131SRod.Evans@Sun.COM 		DBG_CALL(Dbg_seg_entry(ofl, intpsndx, intpsgp));
41559131SRod.Evans@Sun.COM 		ofl->ofl_phdr[intppndx] = *phdr;
41569131SRod.Evans@Sun.COM 	}
41579131SRod.Evans@Sun.COM 
41589131SRod.Evans@Sun.COM 	/*
41590Sstevel@tonic-gate 	 * If we have a PT_SUNWDTRACE phdr, update it now with the address of
41600Sstevel@tonic-gate 	 * the symbol.  It's only now been updated via update_sym().
41610Sstevel@tonic-gate 	 */
416210792SRod.Evans@Sun.COM 	if (dtracesgp) {
41634284Srie 		Phdr		*aphdr, *phdr = &(dtracesgp->sg_phdr);
41640Sstevel@tonic-gate 		Sym_desc	*sdp = ofl->ofl_dtracesym;
41650Sstevel@tonic-gate 
41664284Srie 		phdr->p_vaddr = sdp->sd_sym->st_value;
41674284Srie 		phdr->p_memsz = sdp->sd_sym->st_size;
41680Sstevel@tonic-gate 
41690Sstevel@tonic-gate 		/*
417011827SRod.Evans@Sun.COM 		 * Take permissions from the segment that the symbol is
417111827SRod.Evans@Sun.COM 		 * associated with.
41720Sstevel@tonic-gate 		 */
41734284Srie 		aphdr = &sdp->sd_isc->is_osdesc->os_sgdesc->sg_phdr;
41744284Srie 		assert(aphdr);
41754284Srie 		phdr->p_flags = aphdr->p_flags;
41764284Srie 
41774284Srie 		DBG_CALL(Dbg_seg_entry(ofl, dtracesndx, dtracesgp));
41784284Srie 		ofl->ofl_phdr[dtracepndx] = *phdr;
41794284Srie 	}
41804284Srie 
41814284Srie 	/*
41824284Srie 	 * If we have a PT_SUNWCAP phdr, update it now from the associated
41834284Srie 	 * section information.
41844284Srie 	 */
418510792SRod.Evans@Sun.COM 	if (capsgp) {
41864284Srie 		Phdr	*phdr = &(capsgp->sg_phdr);
41874284Srie 		Shdr	*shdr = ofl->ofl_oscap->os_shdr;
41884284Srie 
41894284Srie 		phdr->p_vaddr = shdr->sh_addr;
41904284Srie 		phdr->p_offset = shdr->sh_offset;
41919131SRod.Evans@Sun.COM 		phdr->p_memsz = phdr->p_filesz = shdr->sh_size;
41924284Srie 		phdr->p_flags = PF_R;
41934284Srie 
41944284Srie 		DBG_CALL(Dbg_seg_entry(ofl, capsndx, capsgp));
41954284Srie 		ofl->ofl_phdr[cappndx] = *phdr;
41960Sstevel@tonic-gate 	}
41970Sstevel@tonic-gate 
41980Sstevel@tonic-gate 	/*
41990Sstevel@tonic-gate 	 * Update the GROUP sections.
42000Sstevel@tonic-gate 	 */
42010Sstevel@tonic-gate 	if (update_ogroup(ofl) == S_ERROR)
42020Sstevel@tonic-gate 		return (S_ERROR);
42030Sstevel@tonic-gate 
42040Sstevel@tonic-gate 	/*
42050Sstevel@tonic-gate 	 * Update Move Table.
42060Sstevel@tonic-gate 	 */
42079131SRod.Evans@Sun.COM 	if (ofl->ofl_osmove || ofl->ofl_isparexpn)
42089131SRod.Evans@Sun.COM 		update_move(ofl);
42090Sstevel@tonic-gate 
42100Sstevel@tonic-gate 	/*
42110Sstevel@tonic-gate 	 * Build any output headers, version information, dynamic structure and
42120Sstevel@tonic-gate 	 * syminfo structure.
42130Sstevel@tonic-gate 	 */
42140Sstevel@tonic-gate 	if (update_oehdr(ofl) == S_ERROR)
42150Sstevel@tonic-gate 		return (S_ERROR);
42167682SAli.Bahrami@Sun.COM 	if (!(flags & FLG_OF_NOVERSEC)) {
42177682SAli.Bahrami@Sun.COM 		if ((flags & FLG_OF_VERDEF) &&
42187682SAli.Bahrami@Sun.COM 		    (update_overdef(ofl) == S_ERROR))
42190Sstevel@tonic-gate 			return (S_ERROR);
42207682SAli.Bahrami@Sun.COM 		if ((flags & FLG_OF_VERNEED) &&
42217682SAli.Bahrami@Sun.COM 		    (update_overneed(ofl) == S_ERROR))
42220Sstevel@tonic-gate 			return (S_ERROR);
422310792SRod.Evans@Sun.COM 		if (flags & (FLG_OF_VERNEED | FLG_OF_VERDEF))
422410792SRod.Evans@Sun.COM 			update_oversym(ofl);
42257682SAli.Bahrami@Sun.COM 	}
42260Sstevel@tonic-gate 	if (flags & FLG_OF_DYNAMIC) {
42270Sstevel@tonic-gate 		if (update_odynamic(ofl) == S_ERROR)
42280Sstevel@tonic-gate 			return (S_ERROR);
422910792SRod.Evans@Sun.COM 	}
423010792SRod.Evans@Sun.COM 	if (ofl->ofl_ossyminfo) {
423110792SRod.Evans@Sun.COM 		if (update_osyminfo(ofl) == S_ERROR)
423210792SRod.Evans@Sun.COM 			return (S_ERROR);
42330Sstevel@tonic-gate 	}
42340Sstevel@tonic-gate 
42350Sstevel@tonic-gate 	/*
423611827SRod.Evans@Sun.COM 	 * Update capabilities information if required.
423711827SRod.Evans@Sun.COM 	 */
423811827SRod.Evans@Sun.COM 	if (ofl->ofl_oscap)
423911827SRod.Evans@Sun.COM 		update_oscap(ofl);
424011827SRod.Evans@Sun.COM 	if (ofl->ofl_oscapinfo)
424111827SRod.Evans@Sun.COM 		update_oscapinfo(ofl);
424211827SRod.Evans@Sun.COM 
424311827SRod.Evans@Sun.COM 	/*
424410792SRod.Evans@Sun.COM 	 * Sanity test: the first and last data byte of a string table
42458747SAli.Bahrami@Sun.COM 	 * must be NULL.
42468747SAli.Bahrami@Sun.COM 	 */
42478747SAli.Bahrami@Sun.COM 	assert((ofl->ofl_osshstrtab == NULL) ||
42488747SAli.Bahrami@Sun.COM 	    (*((char *)ofl->ofl_osshstrtab->os_outdata->d_buf) == '\0'));
42498747SAli.Bahrami@Sun.COM 	assert((ofl->ofl_osshstrtab == NULL) ||
42508747SAli.Bahrami@Sun.COM 	    (*(((char *)ofl->ofl_osshstrtab->os_outdata->d_buf) +
42518747SAli.Bahrami@Sun.COM 	    ofl->ofl_osshstrtab->os_outdata->d_size - 1) == '\0'));
42528747SAli.Bahrami@Sun.COM 
42538747SAli.Bahrami@Sun.COM 	assert((ofl->ofl_osstrtab == NULL) ||
42548747SAli.Bahrami@Sun.COM 	    (*((char *)ofl->ofl_osstrtab->os_outdata->d_buf) == '\0'));
42558747SAli.Bahrami@Sun.COM 	assert((ofl->ofl_osstrtab == NULL) ||
42568747SAli.Bahrami@Sun.COM 	    (*(((char *)ofl->ofl_osstrtab->os_outdata->d_buf) +
42578747SAli.Bahrami@Sun.COM 	    ofl->ofl_osstrtab->os_outdata->d_size - 1) == '\0'));
42588747SAli.Bahrami@Sun.COM 
42598747SAli.Bahrami@Sun.COM 	assert((ofl->ofl_osdynstr == NULL) ||
42608747SAli.Bahrami@Sun.COM 	    (*((char *)ofl->ofl_osdynstr->os_outdata->d_buf) == '\0'));
42618747SAli.Bahrami@Sun.COM 	assert((ofl->ofl_osdynstr == NULL) ||
42628747SAli.Bahrami@Sun.COM 	    (*(((char *)ofl->ofl_osdynstr->os_outdata->d_buf) +
42638747SAli.Bahrami@Sun.COM 	    ofl->ofl_osdynstr->os_outdata->d_size - DYNSTR_EXTRA_PAD - 1) ==
42648747SAli.Bahrami@Sun.COM 	    '\0'));
42658747SAli.Bahrami@Sun.COM 
42668747SAli.Bahrami@Sun.COM 	/*
42670Sstevel@tonic-gate 	 * Emit Strtab diagnostics.
42680Sstevel@tonic-gate 	 */
42691618Srie 	DBG_CALL(Dbg_sec_strtab(ofl->ofl_lml, ofl->ofl_osshstrtab,
42701618Srie 	    ofl->ofl_shdrsttab));
42711618Srie 	DBG_CALL(Dbg_sec_strtab(ofl->ofl_lml, ofl->ofl_osstrtab,
42721618Srie 	    ofl->ofl_strtab));
42731618Srie 	DBG_CALL(Dbg_sec_strtab(ofl->ofl_lml, ofl->ofl_osdynstr,
42741618Srie 	    ofl->ofl_dynstrtab));
42750Sstevel@tonic-gate 
42760Sstevel@tonic-gate 	/*
42770Sstevel@tonic-gate 	 * Initialize the section headers string table index within the elf
42780Sstevel@tonic-gate 	 * header.
42790Sstevel@tonic-gate 	 */
42800Sstevel@tonic-gate 	/* LINTED */
42810Sstevel@tonic-gate 	if ((shscnndx = elf_ndxscn(ofl->ofl_osshstrtab->os_scn)) <
42820Sstevel@tonic-gate 	    SHN_LORESERVE) {
42831618Srie 		ofl->ofl_nehdr->e_shstrndx =
42840Sstevel@tonic-gate 		    /* LINTED */
42850Sstevel@tonic-gate 		    (Half)shscnndx;
42860Sstevel@tonic-gate 	} else {
42870Sstevel@tonic-gate 		/*
42880Sstevel@tonic-gate 		 * If the STRTAB section index doesn't fit into
42890Sstevel@tonic-gate 		 * e_shstrndx, then we store it in 'shdr[0].st_link'.
42900Sstevel@tonic-gate 		 */
42910Sstevel@tonic-gate 		Elf_Scn	*scn;
42920Sstevel@tonic-gate 		Shdr	*shdr0;
42931618Srie 
42940Sstevel@tonic-gate 		if ((scn = elf_getscn(ofl->ofl_elf, 0)) == NULL) {
4295*13074SAli.Bahrami@Oracle.COM 			ld_eprintf(ofl, ERR_ELF, MSG_INTL(MSG_ELF_GETSCN),
4296*13074SAli.Bahrami@Oracle.COM 			    ofl->ofl_name);
42970Sstevel@tonic-gate 			return (S_ERROR);
42980Sstevel@tonic-gate 		}
42990Sstevel@tonic-gate 		if ((shdr0 = elf_getshdr(scn)) == NULL) {
4300*13074SAli.Bahrami@Oracle.COM 			ld_eprintf(ofl, ERR_ELF, MSG_INTL(MSG_ELF_GETSHDR),
4301*13074SAli.Bahrami@Oracle.COM 			    ofl->ofl_name);
43020Sstevel@tonic-gate 			return (S_ERROR);
43030Sstevel@tonic-gate 		}
43041618Srie 		ofl->ofl_nehdr->e_shstrndx = SHN_XINDEX;
43050Sstevel@tonic-gate 		shdr0->sh_link = shscnndx;
43060Sstevel@tonic-gate 	}
43070Sstevel@tonic-gate 
43080Sstevel@tonic-gate 	return ((uintptr_t)etext);
43090Sstevel@tonic-gate }
4310