xref: /titanic_44/usr/src/cmd/sgs/libld/common/syms.c (revision 8878595fcb30298d8ee1db0aee6e69e4dc15b318)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
55aefb655Srie  * Common Development and Distribution License (the "License").
65aefb655Srie  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
215aefb655Srie 
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  *	Copyright (c) 1988 AT&T
247c478bd9Sstevel@tonic-gate  *	  All Rights Reserved
257c478bd9Sstevel@tonic-gate  *
267c478bd9Sstevel@tonic-gate  *
27bf994817SAli Bahrami  * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
287c478bd9Sstevel@tonic-gate  */
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  * Symbol table management routines
327c478bd9Sstevel@tonic-gate  */
33ba2be530Sab196087 
34ba2be530Sab196087 #define	ELF_TARGET_AMD64
35ba2be530Sab196087 
367c478bd9Sstevel@tonic-gate #include	<stdio.h>
377c478bd9Sstevel@tonic-gate #include	<string.h>
385aefb655Srie #include	<debug.h>
397c478bd9Sstevel@tonic-gate #include	"msg.h"
407c478bd9Sstevel@tonic-gate #include	"_libld.h"
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate /*
437c478bd9Sstevel@tonic-gate  * AVL tree comparator function:
447c478bd9Sstevel@tonic-gate  *
456b3ba5bdSAli Bahrami  * The primary key is the symbol name hash with a secondary key of the symbol
466b3ba5bdSAli Bahrami  * name itself.
477c478bd9Sstevel@tonic-gate  */
487c478bd9Sstevel@tonic-gate int
ld_sym_avl_comp(const void * elem1,const void * elem2)495aefb655Srie ld_sym_avl_comp(const void *elem1, const void *elem2)
507c478bd9Sstevel@tonic-gate {
517c478bd9Sstevel@tonic-gate 	Sym_avlnode	*sav1 = (Sym_avlnode *)elem1;
527c478bd9Sstevel@tonic-gate 	Sym_avlnode	*sav2 = (Sym_avlnode *)elem2;
536b3ba5bdSAli Bahrami 	int		res;
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate 	res = sav1->sav_hash - sav2->sav_hash;
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate 	if (res < 0)
587c478bd9Sstevel@tonic-gate 		return (-1);
597c478bd9Sstevel@tonic-gate 	if (res > 0)
607c478bd9Sstevel@tonic-gate 		return (1);
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate 	/*
637c478bd9Sstevel@tonic-gate 	 * Hash is equal - now compare name
647c478bd9Sstevel@tonic-gate 	 */
657c478bd9Sstevel@tonic-gate 	res = strcmp(sav1->sav_name, sav2->sav_name);
667c478bd9Sstevel@tonic-gate 	if (res == 0)
677c478bd9Sstevel@tonic-gate 		return (0);
687c478bd9Sstevel@tonic-gate 	if (res > 0)
697c478bd9Sstevel@tonic-gate 		return (1);
707c478bd9Sstevel@tonic-gate 	return (-1);
717c478bd9Sstevel@tonic-gate }
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate /*
747c478bd9Sstevel@tonic-gate  * Focal point for verifying symbol names.
757c478bd9Sstevel@tonic-gate  */
76a194faf8Srie inline static const char *
string(Ofl_desc * ofl,Ifl_desc * ifl,Sym * sym,const char * strs,size_t strsize,int symndx,Word shndx,Word symsecndx,const char * symsecname,const char * strsecname,sd_flag_t * flags)775aefb655Srie string(Ofl_desc *ofl, Ifl_desc *ifl, Sym *sym, const char *strs, size_t strsize,
784a8d0ea7SAli Bahrami     int symndx, Word shndx, Word symsecndx, const char *symsecname,
79635216b6SRod Evans     const char *strsecname, sd_flag_t *flags)
807c478bd9Sstevel@tonic-gate {
817c478bd9Sstevel@tonic-gate 	Word	name = sym->st_name;
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate 	if (name) {
847c478bd9Sstevel@tonic-gate 		if ((ifl->ifl_flags & FLG_IF_HSTRTAB) == 0) {
851007fd6fSAli Bahrami 			ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_FIL_NOSTRTABLE),
861007fd6fSAli Bahrami 			    ifl->ifl_name, EC_WORD(symsecndx), symsecname,
871007fd6fSAli Bahrami 			    symndx, EC_XWORD(name));
886b3ba5bdSAli Bahrami 			return (NULL);
897c478bd9Sstevel@tonic-gate 		}
907c478bd9Sstevel@tonic-gate 		if (name >= (Word)strsize) {
911007fd6fSAli Bahrami 			ld_eprintf(ofl, ERR_FATAL,
925aefb655Srie 			    MSG_INTL(MSG_FIL_EXCSTRTABLE), ifl->ifl_name,
934a8d0ea7SAli Bahrami 			    EC_WORD(symsecndx), symsecname, symndx,
944a8d0ea7SAli Bahrami 			    EC_XWORD(name), strsecname, EC_XWORD(strsize));
956b3ba5bdSAli Bahrami 			return (NULL);
967c478bd9Sstevel@tonic-gate 		}
977c478bd9Sstevel@tonic-gate 	}
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate 	/*
1007c478bd9Sstevel@tonic-gate 	 * Determine if we're dealing with a register and if so validate it.
1017c478bd9Sstevel@tonic-gate 	 * If it's a scratch register, a fabricated name will be returned.
1027c478bd9Sstevel@tonic-gate 	 */
103ba2be530Sab196087 	if (ld_targ.t_ms.ms_is_regsym != NULL) {
104ba2be530Sab196087 		const char *regname = (*ld_targ.t_ms.ms_is_regsym)(ofl, ifl,
105ba2be530Sab196087 		    sym, strs, symndx, shndx, symsecname, flags);
106ba2be530Sab196087 
107ba2be530Sab196087 		if (regname == (const char *)S_ERROR) {
1086b3ba5bdSAli Bahrami 			return (NULL);
1097c478bd9Sstevel@tonic-gate 		}
1107c478bd9Sstevel@tonic-gate 		if (regname)
1117c478bd9Sstevel@tonic-gate 			return (regname);
112ba2be530Sab196087 	}
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate 	/*
1157c478bd9Sstevel@tonic-gate 	 * If this isn't a register, but we have a global symbol with a null
1167c478bd9Sstevel@tonic-gate 	 * name, we're not going to be able to hash this, search for it, or
1177c478bd9Sstevel@tonic-gate 	 * do anything interesting.  However, we've been accepting a symbol of
1187c478bd9Sstevel@tonic-gate 	 * this kind for ages now, so give the user a warning (rather than a
1197c478bd9Sstevel@tonic-gate 	 * fatal error), just in case this instance exists somewhere in the
1207c478bd9Sstevel@tonic-gate 	 * world and hasn't, as yet, been a problem.
1217c478bd9Sstevel@tonic-gate 	 */
1227c478bd9Sstevel@tonic-gate 	if ((name == 0) && (ELF_ST_BIND(sym->st_info) != STB_LOCAL)) {
1231007fd6fSAli Bahrami 		ld_eprintf(ofl, ERR_WARNING, MSG_INTL(MSG_FIL_NONAMESYM),
1244a8d0ea7SAli Bahrami 		    ifl->ifl_name, EC_WORD(symsecndx), symsecname, symndx,
1254a8d0ea7SAli Bahrami 		    EC_XWORD(name));
1267c478bd9Sstevel@tonic-gate 	}
1277c478bd9Sstevel@tonic-gate 	return (strs + name);
1287c478bd9Sstevel@tonic-gate }
1297c478bd9Sstevel@tonic-gate 
130ba2be530Sab196087 /*
131ba2be530Sab196087  * For producing symbol names strings to use in error messages.
132ba2be530Sab196087  * If the symbol has a non-null name, then the string returned by
133ba2be530Sab196087  * this function is the output from demangle(), surrounded by
134ba2be530Sab196087  * single quotes. For null names, a descriptive string giving
135ba2be530Sab196087  * the symbol section and index is generated.
136ba2be530Sab196087  *
137ba2be530Sab196087  * This function uses an internal static buffer to hold the resulting
138ba2be530Sab196087  * string. The value returned is usable by the caller until the next
139ba2be530Sab196087  * call, at which point it is overwritten.
140ba2be530Sab196087  */
141ba2be530Sab196087 static const char *
demangle_symname(const char * name,const char * symtab_name,Word symndx)142ba2be530Sab196087 demangle_symname(const char *name, const char *symtab_name, Word symndx)
143ba2be530Sab196087 {
144ba2be530Sab196087 #define	INIT_BUFSIZE 256
145ba2be530Sab196087 
146ba2be530Sab196087 	static char	*buf;
147ba2be530Sab196087 	static size_t	bufsize = 0;
148ba2be530Sab196087 	size_t		len;
149ba2be530Sab196087 	int		use_name;
150ba2be530Sab196087 
151ba2be530Sab196087 	use_name = (name != NULL) && (*name != '\0');
152ba2be530Sab196087 
153ba2be530Sab196087 	if (use_name) {
154ba2be530Sab196087 		name = demangle(name);
155ba2be530Sab196087 		len = strlen(name) + 2;   /* Include room for quotes */
156ba2be530Sab196087 	} else {
157ba2be530Sab196087 		name = MSG_ORIG(MSG_STR_EMPTY);
1584f680cc6SAli Bahrami 		len = strlen(symtab_name) + 2 + CONV_INV_BUFSIZE;
159ba2be530Sab196087 	}
160ba2be530Sab196087 	len++;			/* Null termination */
161ba2be530Sab196087 
162ba2be530Sab196087 	/* If our buffer is too small, double it until it is big enough */
163ba2be530Sab196087 	if (len > bufsize) {
164ba2be530Sab196087 		size_t	new_bufsize = bufsize;
165ba2be530Sab196087 		char	*new_buf;
166ba2be530Sab196087 
167ba2be530Sab196087 		if (new_bufsize == 0)
168ba2be530Sab196087 			new_bufsize = INIT_BUFSIZE;
169ba2be530Sab196087 		while (len > new_bufsize)
170ba2be530Sab196087 			new_bufsize *= 2;
1716b3ba5bdSAli Bahrami 		if ((new_buf = libld_malloc(new_bufsize)) == NULL)
172ba2be530Sab196087 			return (name);
173ba2be530Sab196087 		buf = new_buf;
174ba2be530Sab196087 		bufsize = new_bufsize;
175ba2be530Sab196087 	}
176ba2be530Sab196087 
177ba2be530Sab196087 	if (use_name) {
178ba2be530Sab196087 		(void) snprintf(buf, bufsize, MSG_ORIG(MSG_FMT_SYMNAM), name);
179ba2be530Sab196087 	} else {
180ba2be530Sab196087 		(void) snprintf(buf, bufsize, MSG_ORIG(MSG_FMT_NULLSYMNAM),
181ba2be530Sab196087 		    symtab_name, EC_WORD(symndx));
182ba2be530Sab196087 	}
183ba2be530Sab196087 
184ba2be530Sab196087 	return (buf);
185ba2be530Sab196087 
186ba2be530Sab196087 #undef INIT_BUFSIZE
187ba2be530Sab196087 }
188ba2be530Sab196087 
1897c478bd9Sstevel@tonic-gate /*
1907c478bd9Sstevel@tonic-gate  * Shared objects can be built that define specific symbols that can not be
1917c478bd9Sstevel@tonic-gate  * directly bound to.  These objects have a syminfo section (and an associated
1927c478bd9Sstevel@tonic-gate  * DF_1_NODIRECT dynamic flags entry).  Scan this table looking for symbols
1937c478bd9Sstevel@tonic-gate  * that can't be bound to directly, and if this files symbol is presently
1947c478bd9Sstevel@tonic-gate  * referenced, mark it so that we don't directly bind to it.
1957c478bd9Sstevel@tonic-gate  */
1967c478bd9Sstevel@tonic-gate uintptr_t
ld_sym_nodirect(Is_desc * isp,Ifl_desc * ifl,Ofl_desc * ofl)1975aefb655Srie ld_sym_nodirect(Is_desc *isp, Ifl_desc *ifl, Ofl_desc *ofl)
1987c478bd9Sstevel@tonic-gate {
1997c478bd9Sstevel@tonic-gate 	Shdr		*sifshdr, *symshdr;
2007c478bd9Sstevel@tonic-gate 	Syminfo		*sifdata;
2017c478bd9Sstevel@tonic-gate 	Sym		*symdata;
2027c478bd9Sstevel@tonic-gate 	char		*strdata;
2037c478bd9Sstevel@tonic-gate 	ulong_t		cnt, _cnt;
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 	/*
2067c478bd9Sstevel@tonic-gate 	 * Get the syminfo data, and determine the number of entries.
2077c478bd9Sstevel@tonic-gate 	 */
2087c478bd9Sstevel@tonic-gate 	sifshdr = isp->is_shdr;
2097c478bd9Sstevel@tonic-gate 	sifdata = (Syminfo *)isp->is_indata->d_buf;
2107c478bd9Sstevel@tonic-gate 	cnt =  sifshdr->sh_size / sifshdr->sh_entsize;
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 	/*
2137c478bd9Sstevel@tonic-gate 	 * Get the associated symbol table.
2147c478bd9Sstevel@tonic-gate 	 */
21528bda19cSRod Evans 	if ((sifshdr->sh_link == 0) || (sifshdr->sh_link >= ifl->ifl_shnum)) {
21628bda19cSRod Evans 		/*
21728bda19cSRod Evans 		 * Broken input file
21828bda19cSRod Evans 		 */
2191007fd6fSAli Bahrami 		ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_FIL_INVSHINFO),
22028bda19cSRod Evans 		    ifl->ifl_name, isp->is_name, EC_XWORD(sifshdr->sh_link));
22128bda19cSRod Evans 		return (0);
22228bda19cSRod Evans 	}
2237c478bd9Sstevel@tonic-gate 	symshdr = ifl->ifl_isdesc[sifshdr->sh_link]->is_shdr;
2247c478bd9Sstevel@tonic-gate 	symdata = ifl->ifl_isdesc[sifshdr->sh_link]->is_indata->d_buf;
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 	/*
2277c478bd9Sstevel@tonic-gate 	 * Get the string table associated with the symbol table.
2287c478bd9Sstevel@tonic-gate 	 */
2297c478bd9Sstevel@tonic-gate 	strdata = ifl->ifl_isdesc[symshdr->sh_link]->is_indata->d_buf;
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 	/*
2327c478bd9Sstevel@tonic-gate 	 * Traverse the syminfo data for symbols that can't be directly
2337c478bd9Sstevel@tonic-gate 	 * bound to.
2347c478bd9Sstevel@tonic-gate 	 */
2357c478bd9Sstevel@tonic-gate 	for (_cnt = 1, sifdata++; _cnt < cnt; _cnt++, sifdata++) {
2367c478bd9Sstevel@tonic-gate 		Sym		*sym;
2377c478bd9Sstevel@tonic-gate 		char		*str;
2387c478bd9Sstevel@tonic-gate 		Sym_desc	*sdp;
2397c478bd9Sstevel@tonic-gate 
2403c4993fbSrie 		if ((sifdata->si_flags & SYMINFO_FLG_NOEXTDIRECT) == 0)
2417c478bd9Sstevel@tonic-gate 			continue;
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate 		sym = (Sym *)(symdata + _cnt);
2447c478bd9Sstevel@tonic-gate 		str = (char *)(strdata + sym->st_name);
2457c478bd9Sstevel@tonic-gate 
24628bda19cSRod Evans 		if ((sdp = ld_sym_find(str, SYM_NOHASH, NULL, ofl)) != NULL) {
2477c478bd9Sstevel@tonic-gate 			if (ifl != sdp->sd_file)
2487c478bd9Sstevel@tonic-gate 				continue;
2497c478bd9Sstevel@tonic-gate 
250635216b6SRod Evans 			sdp->sd_flags &= ~FLG_SY_DIR;
251635216b6SRod Evans 			sdp->sd_flags |= FLG_SY_NDIR;
2527c478bd9Sstevel@tonic-gate 		}
2537c478bd9Sstevel@tonic-gate 	}
2547c478bd9Sstevel@tonic-gate 	return (0);
2557c478bd9Sstevel@tonic-gate }
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate /*
2587c478bd9Sstevel@tonic-gate  * If, during symbol processing, it is necessary to update a local symbols
2597c478bd9Sstevel@tonic-gate  * contents before we have generated the symbol tables in the output image,
2607c478bd9Sstevel@tonic-gate  * create a new symbol structure and copy the original symbol contents.  While
2617c478bd9Sstevel@tonic-gate  * we are processing the input files, their local symbols are part of the
2627c478bd9Sstevel@tonic-gate  * read-only mapped image.  Commonly, these symbols are copied to the new output
2637c478bd9Sstevel@tonic-gate  * file image and then updated to reflect their new address and any change in
2647c478bd9Sstevel@tonic-gate  * attributes.  However, sometimes during relocation counting, it is necessary
2657c478bd9Sstevel@tonic-gate  * to adjust the symbols information.  This routine provides for the generation
2667c478bd9Sstevel@tonic-gate  * of a new symbol image so that this update can be performed.
2677c478bd9Sstevel@tonic-gate  * All global symbols are copied to an internal symbol table to improve locality
2687c478bd9Sstevel@tonic-gate  * of reference and hence performance, and thus this copying is not necessary.
2697c478bd9Sstevel@tonic-gate  */
2707c478bd9Sstevel@tonic-gate uintptr_t
ld_sym_copy(Sym_desc * sdp)2715aefb655Srie ld_sym_copy(Sym_desc *sdp)
2727c478bd9Sstevel@tonic-gate {
2737c478bd9Sstevel@tonic-gate 	Sym	*nsym;
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate 	if (sdp->sd_flags & FLG_SY_CLEAN) {
2766b3ba5bdSAli Bahrami 		if ((nsym = libld_malloc(sizeof (Sym))) == NULL)
2777c478bd9Sstevel@tonic-gate 			return (S_ERROR);
2787c478bd9Sstevel@tonic-gate 		*nsym = *(sdp->sd_sym);
2797c478bd9Sstevel@tonic-gate 		sdp->sd_sym = nsym;
2807c478bd9Sstevel@tonic-gate 		sdp->sd_flags &= ~FLG_SY_CLEAN;
2817c478bd9Sstevel@tonic-gate 	}
2827c478bd9Sstevel@tonic-gate 	return (1);
2837c478bd9Sstevel@tonic-gate }
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate /*
2867c478bd9Sstevel@tonic-gate  * Finds a given name in the link editors internal symbol table.  If no
2877c478bd9Sstevel@tonic-gate  * hash value is specified it is calculated.  A pointer to the located
2887c478bd9Sstevel@tonic-gate  * Sym_desc entry is returned, or NULL if the symbol is not found.
2897c478bd9Sstevel@tonic-gate  */
2907c478bd9Sstevel@tonic-gate Sym_desc *
ld_sym_find(const char * name,Word hash,avl_index_t * where,Ofl_desc * ofl)2915aefb655Srie ld_sym_find(const char *name, Word hash, avl_index_t *where, Ofl_desc *ofl)
2927c478bd9Sstevel@tonic-gate {
2936b3ba5bdSAli Bahrami 	Sym_avlnode	qsav, *sav;
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate 	if (hash == SYM_NOHASH)
2967c478bd9Sstevel@tonic-gate 		/* LINTED */
2977c478bd9Sstevel@tonic-gate 		hash = (Word)elf_hash((const char *)name);
2987c478bd9Sstevel@tonic-gate 	qsav.sav_hash = hash;
2997c478bd9Sstevel@tonic-gate 	qsav.sav_name = name;
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 	/*
3027c478bd9Sstevel@tonic-gate 	 * Perform search for symbol in AVL tree.  Note that the 'where' field
3037c478bd9Sstevel@tonic-gate 	 * is passed in from the caller.  If a 'where' is present, it can be
304a194faf8Srie 	 * used in subsequent 'ld_sym_enter()' calls if required.
3057c478bd9Sstevel@tonic-gate 	 */
3067c478bd9Sstevel@tonic-gate 	sav = avl_find(&ofl->ofl_symavl, &qsav, where);
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate 	/*
3097c478bd9Sstevel@tonic-gate 	 * If symbol was not found in the avl tree, return null to show that.
3107c478bd9Sstevel@tonic-gate 	 */
3116b3ba5bdSAli Bahrami 	if (sav == NULL)
3126b3ba5bdSAli Bahrami 		return (NULL);
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 	/*
3157c478bd9Sstevel@tonic-gate 	 * Return symbol found.
3167c478bd9Sstevel@tonic-gate 	 */
317635216b6SRod Evans 	return (sav->sav_sdp);
3187c478bd9Sstevel@tonic-gate }
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate /*
3217c478bd9Sstevel@tonic-gate  * Enter a new symbol into the link editors internal symbol table.
3227c478bd9Sstevel@tonic-gate  * If the symbol is from an input file, information regarding the input file
3237c478bd9Sstevel@tonic-gate  * and input section is also recorded.  Otherwise (file == NULL) the symbol
3247c478bd9Sstevel@tonic-gate  * has been internally generated (ie. _etext, _edata, etc.).
3257c478bd9Sstevel@tonic-gate  */
3267c478bd9Sstevel@tonic-gate Sym_desc *
ld_sym_enter(const char * name,Sym * osym,Word hash,Ifl_desc * ifl,Ofl_desc * ofl,Word ndx,Word shndx,sd_flag_t sdflags,avl_index_t * where)3275aefb655Srie ld_sym_enter(const char *name, Sym *osym, Word hash, Ifl_desc *ifl,
328635216b6SRod Evans     Ofl_desc *ofl, Word ndx, Word shndx, sd_flag_t sdflags, avl_index_t *where)
3297c478bd9Sstevel@tonic-gate {
3307c478bd9Sstevel@tonic-gate 	Sym_desc	*sdp;
3317c478bd9Sstevel@tonic-gate 	Sym_aux		*sap;
3327c478bd9Sstevel@tonic-gate 	Sym_avlnode	*savl;
3337c478bd9Sstevel@tonic-gate 	char		*_name;
3347c478bd9Sstevel@tonic-gate 	Sym		*nsym;
3357c478bd9Sstevel@tonic-gate 	Half		etype;
33660758829Srie 	uchar_t		vis;
3377c478bd9Sstevel@tonic-gate 	avl_index_t	_where;
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate 	/*
3407c478bd9Sstevel@tonic-gate 	 * Establish the file type.
3417c478bd9Sstevel@tonic-gate 	 */
3427c478bd9Sstevel@tonic-gate 	if (ifl)
3437c478bd9Sstevel@tonic-gate 		etype = ifl->ifl_ehdr->e_type;
3447c478bd9Sstevel@tonic-gate 	else
3457c478bd9Sstevel@tonic-gate 		etype = ET_NONE;
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate 	ofl->ofl_entercnt++;
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate 	/*
3507c478bd9Sstevel@tonic-gate 	 * Allocate a Sym Descriptor, Auxiliary Descriptor, and a Sym AVLNode -
3517c478bd9Sstevel@tonic-gate 	 * contiguously.
3527c478bd9Sstevel@tonic-gate 	 */
353635216b6SRod Evans 	if ((savl = libld_calloc(S_DROUND(sizeof (Sym_avlnode)) +
354635216b6SRod Evans 	    S_DROUND(sizeof (Sym_desc)) +
355635216b6SRod Evans 	    S_DROUND(sizeof (Sym_aux)), 1)) == NULL)
3567c478bd9Sstevel@tonic-gate 		return ((Sym_desc *)S_ERROR);
357635216b6SRod Evans 	sdp = (Sym_desc *)((uintptr_t)savl +
358635216b6SRod Evans 	    S_DROUND(sizeof (Sym_avlnode)));
359635216b6SRod Evans 	sap = (Sym_aux *)((uintptr_t)sdp +
360635216b6SRod Evans 	    S_DROUND(sizeof (Sym_desc)));
3617c478bd9Sstevel@tonic-gate 
362635216b6SRod Evans 	savl->sav_sdp = sdp;
3637c478bd9Sstevel@tonic-gate 	sdp->sd_file = ifl;
3647c478bd9Sstevel@tonic-gate 	sdp->sd_aux = sap;
3657c478bd9Sstevel@tonic-gate 	savl->sav_hash = sap->sa_hash = hash;
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate 	/*
3687c478bd9Sstevel@tonic-gate 	 * Copy the symbol table entry from the input file into the internal
3697c478bd9Sstevel@tonic-gate 	 * entry and have the symbol descriptor use it.
3707c478bd9Sstevel@tonic-gate 	 */
3717c478bd9Sstevel@tonic-gate 	sdp->sd_sym = nsym = &sap->sa_sym;
3727c478bd9Sstevel@tonic-gate 	*nsym = *osym;
3737c478bd9Sstevel@tonic-gate 	sdp->sd_shndx = shndx;
3747c478bd9Sstevel@tonic-gate 	sdp->sd_flags |= sdflags;
3757c478bd9Sstevel@tonic-gate 
3766b3ba5bdSAli Bahrami 	if ((_name = libld_malloc(strlen(name) + 1)) == NULL)
3777c478bd9Sstevel@tonic-gate 		return ((Sym_desc *)S_ERROR);
3787c478bd9Sstevel@tonic-gate 	savl->sav_name = sdp->sd_name = (const char *)strcpy(_name, name);
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate 	/*
3817c478bd9Sstevel@tonic-gate 	 * Enter Symbol in AVL tree.
3827c478bd9Sstevel@tonic-gate 	 */
3837c478bd9Sstevel@tonic-gate 	if (where == 0) {
3847c478bd9Sstevel@tonic-gate 		/* LINTED */
3857c478bd9Sstevel@tonic-gate 		Sym_avlnode	*_savl;
3867c478bd9Sstevel@tonic-gate 		/*
3875aefb655Srie 		 * If a previous ld_sym_find() hasn't initialized 'where' do it
3887c478bd9Sstevel@tonic-gate 		 * now.
3897c478bd9Sstevel@tonic-gate 		 */
3907c478bd9Sstevel@tonic-gate 		where = &_where;
3917c478bd9Sstevel@tonic-gate 		_savl = avl_find(&ofl->ofl_symavl, savl, where);
3926b3ba5bdSAli Bahrami 		assert(_savl == NULL);
3937c478bd9Sstevel@tonic-gate 	}
3947c478bd9Sstevel@tonic-gate 	avl_insert(&ofl->ofl_symavl, savl, *where);
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate 	/*
3977c478bd9Sstevel@tonic-gate 	 * Record the section index.  This is possible because the
3987c478bd9Sstevel@tonic-gate 	 * `ifl_isdesc' table is filled before we start symbol processing.
3997c478bd9Sstevel@tonic-gate 	 */
4000bc07c75Srie 	if ((sdflags & FLG_SY_SPECSEC) || (nsym->st_shndx == SHN_UNDEF))
4017c478bd9Sstevel@tonic-gate 		sdp->sd_isc = NULL;
4027c478bd9Sstevel@tonic-gate 	else {
4037c478bd9Sstevel@tonic-gate 		sdp->sd_isc = ifl->ifl_isdesc[shndx];
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate 		/*
4067c478bd9Sstevel@tonic-gate 		 * If this symbol is from a relocatable object, make sure that
4077c478bd9Sstevel@tonic-gate 		 * it is still associated with a section.  For example, an
4087c478bd9Sstevel@tonic-gate 		 * unknown section type (SHT_NULL) would have been rejected on
4097c478bd9Sstevel@tonic-gate 		 * input with a warning.  Here, we make the use of the symbol
4107c478bd9Sstevel@tonic-gate 		 * fatal.  A symbol descriptor is still returned, so that the
4117c478bd9Sstevel@tonic-gate 		 * caller can continue processing all symbols, and hence flush
4127c478bd9Sstevel@tonic-gate 		 * out as many error conditions as possible.
4137c478bd9Sstevel@tonic-gate 		 */
4146b3ba5bdSAli Bahrami 		if ((etype == ET_REL) && (sdp->sd_isc == NULL)) {
4151007fd6fSAli Bahrami 			ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_SYM_INVSEC),
4161007fd6fSAli Bahrami 			    name, ifl->ifl_name, EC_XWORD(shndx));
4177c478bd9Sstevel@tonic-gate 			return (sdp);
4187c478bd9Sstevel@tonic-gate 		}
4197c478bd9Sstevel@tonic-gate 	}
4207c478bd9Sstevel@tonic-gate 
4217c478bd9Sstevel@tonic-gate 	/*
4227c478bd9Sstevel@tonic-gate 	 * Mark any COMMON symbols as 'tentative'.
4237c478bd9Sstevel@tonic-gate 	 */
42454d82594Sseizo 	if (sdflags & FLG_SY_SPECSEC) {
4250bc07c75Srie 		if (nsym->st_shndx == SHN_COMMON)
4267c478bd9Sstevel@tonic-gate 			sdp->sd_flags |= FLG_SY_TENTSYM;
427ba2be530Sab196087 #if	defined(_ELF64)
428ba2be530Sab196087 		else if ((ld_targ.t_m.m_mach == EM_AMD64) &&
429ba2be530Sab196087 		    (nsym->st_shndx == SHN_X86_64_LCOMMON))
43054d82594Sseizo 			sdp->sd_flags |= FLG_SY_TENTSYM;
43154d82594Sseizo #endif
43254d82594Sseizo 	}
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate 	/*
43560758829Srie 	 * Establish the symbols visibility and reference.
4367c478bd9Sstevel@tonic-gate 	 */
43760758829Srie 	vis = ELF_ST_VISIBILITY(nsym->st_other);
43860758829Srie 
4397c478bd9Sstevel@tonic-gate 	if ((etype == ET_NONE) || (etype == ET_REL)) {
44060758829Srie 		switch (vis) {
44160758829Srie 		case STV_DEFAULT:
442635216b6SRod Evans 			sdp->sd_flags |= FLG_SY_DEFAULT;
44360758829Srie 			break;
44460758829Srie 		case STV_INTERNAL:
44560758829Srie 		case STV_HIDDEN:
446635216b6SRod Evans 			sdp->sd_flags |= FLG_SY_HIDDEN;
44760758829Srie 			break;
44860758829Srie 		case STV_PROTECTED:
449635216b6SRod Evans 			sdp->sd_flags |= FLG_SY_PROTECT;
45060758829Srie 			break;
45160758829Srie 		case STV_EXPORTED:
452635216b6SRod Evans 			sdp->sd_flags |= FLG_SY_EXPORT;
45360758829Srie 			break;
45460758829Srie 		case STV_SINGLETON:
455635216b6SRod Evans 			sdp->sd_flags |= (FLG_SY_SINGLE | FLG_SY_NDIR);
45628bda19cSRod Evans 			ofl->ofl_flags1 |= (FLG_OF1_NDIRECT | FLG_OF1_NGLBDIR);
45760758829Srie 			break;
45860758829Srie 		case STV_ELIMINATE:
459635216b6SRod Evans 			sdp->sd_flags |= (FLG_SY_HIDDEN | FLG_SY_ELIM);
46060758829Srie 			break;
46160758829Srie 		default:
46260758829Srie 			assert(vis <= STV_ELIMINATE);
46360758829Srie 		}
46460758829Srie 
4657c478bd9Sstevel@tonic-gate 		sdp->sd_ref = REF_REL_NEED;
4667c478bd9Sstevel@tonic-gate 
4677c478bd9Sstevel@tonic-gate 		/*
4689a411307Srie 		 * Under -Bnodirect, all exported interfaces that have not
4699a411307Srie 		 * explicitly been defined protected or directly bound to, are
4709a411307Srie 		 * tagged to prevent direct binding.
4717c478bd9Sstevel@tonic-gate 		 */
4720bc07c75Srie 		if ((ofl->ofl_flags1 & FLG_OF1_ALNODIR) &&
473635216b6SRod Evans 		    ((sdp->sd_flags & (FLG_SY_PROTECT | FLG_SY_DIR)) == 0) &&
4749a411307Srie 		    (nsym->st_shndx != SHN_UNDEF)) {
475635216b6SRod Evans 			sdp->sd_flags |= FLG_SY_NDIR;
4769a411307Srie 		}
4777c478bd9Sstevel@tonic-gate 	} else {
4787c478bd9Sstevel@tonic-gate 		sdp->sd_ref = REF_DYN_SEEN;
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate 		/*
48160758829Srie 		 * If this is a protected symbol, remember this.  Note, this
482635216b6SRod Evans 		 * state is different from the FLG_SY_PROTECT used to establish
48360758829Srie 		 * a symbol definitions visibility.  This state is used to warn
48460758829Srie 		 * against possible copy relocations against this referenced
48560758829Srie 		 * symbol.
4867c478bd9Sstevel@tonic-gate 		 */
48760758829Srie 		if (vis == STV_PROTECTED)
4887c478bd9Sstevel@tonic-gate 			sdp->sd_flags |= FLG_SY_PROT;
4897c478bd9Sstevel@tonic-gate 
4907c478bd9Sstevel@tonic-gate 		/*
49160758829Srie 		 * If this is a SINGLETON definition, then indicate the symbol
49260758829Srie 		 * can not be directly bound to, and retain the visibility.
49360758829Srie 		 * This visibility will be inherited by any references made to
49460758829Srie 		 * this symbol.
4957c478bd9Sstevel@tonic-gate 		 */
49660758829Srie 		if ((vis == STV_SINGLETON) && (nsym->st_shndx != SHN_UNDEF))
497635216b6SRod Evans 			sdp->sd_flags |= (FLG_SY_SINGLE | FLG_SY_NDIR);
4987c478bd9Sstevel@tonic-gate 
4997c478bd9Sstevel@tonic-gate 		/*
50060758829Srie 		 * If the new symbol is from a shared library and is associated
50160758829Srie 		 * with a SHT_NOBITS section then this symbol originated from a
50260758829Srie 		 * tentative symbol.
5037c478bd9Sstevel@tonic-gate 		 */
5047c478bd9Sstevel@tonic-gate 		if (sdp->sd_isc &&
5057c478bd9Sstevel@tonic-gate 		    (sdp->sd_isc->is_shdr->sh_type == SHT_NOBITS))
5067c478bd9Sstevel@tonic-gate 			sdp->sd_flags |= FLG_SY_TENTSYM;
5077c478bd9Sstevel@tonic-gate 	}
5087c478bd9Sstevel@tonic-gate 
5097c478bd9Sstevel@tonic-gate 	/*
5107c478bd9Sstevel@tonic-gate 	 * Reclassify any SHN_SUNW_IGNORE symbols to SHN_UNDEF so as to
5110bc07c75Srie 	 * simplify future processing.
5127c478bd9Sstevel@tonic-gate 	 */
5130bc07c75Srie 	if (nsym->st_shndx == SHN_SUNW_IGNORE) {
5147c478bd9Sstevel@tonic-gate 		sdp->sd_shndx = shndx = SHN_UNDEF;
515635216b6SRod Evans 		sdp->sd_flags |= (FLG_SY_REDUCED |
516635216b6SRod Evans 		    FLG_SY_HIDDEN | FLG_SY_IGNORE | FLG_SY_ELIM);
5177c478bd9Sstevel@tonic-gate 	}
5187c478bd9Sstevel@tonic-gate 
5197c478bd9Sstevel@tonic-gate 	/*
5207c478bd9Sstevel@tonic-gate 	 * If this is an undefined, or common symbol from a relocatable object
5217c478bd9Sstevel@tonic-gate 	 * determine whether it is a global or weak reference (see build_osym(),
5227c478bd9Sstevel@tonic-gate 	 * where REF_DYN_NEED definitions are returned back to undefines).
5237c478bd9Sstevel@tonic-gate 	 */
52454d82594Sseizo 	if ((etype == ET_REL) &&
52554d82594Sseizo 	    (ELF_ST_BIND(nsym->st_info) == STB_GLOBAL) &&
5260bc07c75Srie 	    ((nsym->st_shndx == SHN_UNDEF) || ((sdflags & FLG_SY_SPECSEC) &&
527ba2be530Sab196087 #if	defined(_ELF64)
5280bc07c75Srie 	    ((nsym->st_shndx == SHN_COMMON) ||
529ba2be530Sab196087 	    ((ld_targ.t_m.m_mach == EM_AMD64) &&
530ba2be530Sab196087 	    (nsym->st_shndx == SHN_X86_64_LCOMMON))))))
53154d82594Sseizo #else
53233eb6ee1Sab196087 	/* BEGIN CSTYLED */
5330bc07c75Srie 	    (nsym->st_shndx == SHN_COMMON))))
53433eb6ee1Sab196087 	/* END CSTYLED */
53554d82594Sseizo #endif
5367c478bd9Sstevel@tonic-gate 		sdp->sd_flags |= FLG_SY_GLOBREF;
5377c478bd9Sstevel@tonic-gate 
5387c478bd9Sstevel@tonic-gate 	/*
5397c478bd9Sstevel@tonic-gate 	 * Record the input filename on the referenced or defined files list
5407c478bd9Sstevel@tonic-gate 	 * for possible later diagnostics.  The `sa_rfile' pointer contains the
5417c478bd9Sstevel@tonic-gate 	 * name of the file that first referenced this symbol and is used to
5427c478bd9Sstevel@tonic-gate 	 * generate undefined symbol diagnostics (refer to sym_undef_entry()).
5437c478bd9Sstevel@tonic-gate 	 * Note that this entry can be overridden if a reference from a
5447c478bd9Sstevel@tonic-gate 	 * relocatable object is found after a reference from a shared object
5457c478bd9Sstevel@tonic-gate 	 * (refer to sym_override()).
5467c478bd9Sstevel@tonic-gate 	 * The `sa_dfiles' list is used to maintain the list of files that
5477c478bd9Sstevel@tonic-gate 	 * define the same symbol.  This list can be used for two reasons:
5487c478bd9Sstevel@tonic-gate 	 *
5496b3ba5bdSAli Bahrami 	 *  -	To save the first definition of a symbol that is not available
5507c478bd9Sstevel@tonic-gate 	 *	for this link-edit.
5517c478bd9Sstevel@tonic-gate 	 *
5526b3ba5bdSAli Bahrami 	 *  -	To save all definitions of a symbol when the -m option is in
5537c478bd9Sstevel@tonic-gate 	 *	effect.  This is optional as it is used to list multiple
5547c478bd9Sstevel@tonic-gate 	 *	(interposed) definitions of a symbol (refer to ldmap_out()),
5557c478bd9Sstevel@tonic-gate 	 *	and can be quite expensive.
5567c478bd9Sstevel@tonic-gate 	 */
5570bc07c75Srie 	if (nsym->st_shndx == SHN_UNDEF) {
5587c478bd9Sstevel@tonic-gate 		sap->sa_rfile = ifl->ifl_name;
5597c478bd9Sstevel@tonic-gate 	} else {
5607c478bd9Sstevel@tonic-gate 		if (sdp->sd_ref == REF_DYN_SEEN) {
5617c478bd9Sstevel@tonic-gate 			/*
5627c478bd9Sstevel@tonic-gate 			 * A symbol is determined to be unavailable if it
5637c478bd9Sstevel@tonic-gate 			 * belongs to a version of a shared object that this
5647c478bd9Sstevel@tonic-gate 			 * user does not wish to use, or if it belongs to an
5657c478bd9Sstevel@tonic-gate 			 * implicit shared object.
5667c478bd9Sstevel@tonic-gate 			 */
5677c478bd9Sstevel@tonic-gate 			if (ifl->ifl_vercnt) {
5687c478bd9Sstevel@tonic-gate 				Ver_index	*vip;
5697c478bd9Sstevel@tonic-gate 				Half		vndx = ifl->ifl_versym[ndx];
5707c478bd9Sstevel@tonic-gate 
5717c478bd9Sstevel@tonic-gate 				sap->sa_dverndx = vndx;
5727c478bd9Sstevel@tonic-gate 				vip = &ifl->ifl_verndx[vndx];
5737c478bd9Sstevel@tonic-gate 				if (!(vip->vi_flags & FLG_VER_AVAIL)) {
5747c478bd9Sstevel@tonic-gate 					sdp->sd_flags |= FLG_SY_NOTAVAIL;
5757c478bd9Sstevel@tonic-gate 					sap->sa_vfile = ifl->ifl_name;
5767c478bd9Sstevel@tonic-gate 				}
5777c478bd9Sstevel@tonic-gate 			}
5787c478bd9Sstevel@tonic-gate 			if (!(ifl->ifl_flags & FLG_IF_NEEDED))
5797c478bd9Sstevel@tonic-gate 				sdp->sd_flags |= FLG_SY_NOTAVAIL;
5807c478bd9Sstevel@tonic-gate 
5817c478bd9Sstevel@tonic-gate 		} else if (etype == ET_REL) {
5827c478bd9Sstevel@tonic-gate 			/*
5837c478bd9Sstevel@tonic-gate 			 * If this symbol has been obtained from a versioned
5847c478bd9Sstevel@tonic-gate 			 * input relocatable object then the new symbol must be
5857c478bd9Sstevel@tonic-gate 			 * promoted to the versioning of the output file.
5867c478bd9Sstevel@tonic-gate 			 */
5877c478bd9Sstevel@tonic-gate 			if (ifl->ifl_versym)
5885aefb655Srie 				ld_vers_promote(sdp, ndx, ifl, ofl);
5897c478bd9Sstevel@tonic-gate 		}
5907c478bd9Sstevel@tonic-gate 
5917c478bd9Sstevel@tonic-gate 		if ((ofl->ofl_flags & FLG_OF_GENMAP) &&
5927c478bd9Sstevel@tonic-gate 		    ((sdflags & FLG_SY_SPECSEC) == 0))
59357ef7aa9SRod Evans 			if (aplist_append(&sap->sa_dfiles, ifl->ifl_name,
59457ef7aa9SRod Evans 			    AL_CNT_SDP_DFILES) == NULL)
5957c478bd9Sstevel@tonic-gate 				return ((Sym_desc *)S_ERROR);
5967c478bd9Sstevel@tonic-gate 	}
5977c478bd9Sstevel@tonic-gate 
59860758829Srie 	/*
59960758829Srie 	 * Provided we're not processing a mapfile, diagnose the entered symbol.
60060758829Srie 	 * Mapfile processing requires the symbol to be updated with additional
60160758829Srie 	 * information, therefore the diagnosing of the symbol is deferred until
60260758829Srie 	 * later (see Dbg_map_symbol()).
60360758829Srie 	 */
6046b3ba5bdSAli Bahrami 	if ((ifl == NULL) || ((ifl->ifl_flags & FLG_IF_MAPFILE) == 0))
6055aefb655Srie 		DBG_CALL(Dbg_syms_entered(ofl, nsym, sdp));
606635216b6SRod Evans 
6077c478bd9Sstevel@tonic-gate 	return (sdp);
6087c478bd9Sstevel@tonic-gate }
6097c478bd9Sstevel@tonic-gate 
6107c478bd9Sstevel@tonic-gate /*
6117c478bd9Sstevel@tonic-gate  * Add a special symbol to the symbol table.  Takes special symbol name with
6127c478bd9Sstevel@tonic-gate  * and without underscores.  This routine is called, after all other symbol
6137c478bd9Sstevel@tonic-gate  * resolution has completed, to generate a reserved absolute symbol (the
6147c478bd9Sstevel@tonic-gate  * underscore version).  Special symbols are updated with the appropriate
615dd94ecefSrie  * values in update_osym().  If the user has already defined this symbol
6167c478bd9Sstevel@tonic-gate  * issue a warning and leave the symbol as is.  If the non-underscore symbol
6177c478bd9Sstevel@tonic-gate  * is referenced then turn it into a weak alias of the underscored symbol.
6187c478bd9Sstevel@tonic-gate  *
619635216b6SRod Evans  * The bits in sdflags_u are OR'd into the flags field of the symbol for the
620635216b6SRod Evans  * underscored symbol.
621d579eb63Sab196087  *
6227c478bd9Sstevel@tonic-gate  * If this is a global symbol, and it hasn't explicitly been defined as being
6237c478bd9Sstevel@tonic-gate  * directly bound to, indicate that it can't be directly bound to.
6247c478bd9Sstevel@tonic-gate  * Historically, most special symbols only have meaning to the object in which
625c1c6f601Srie  * they exist, however, they've always been global.  To ensure compatibility
626c1c6f601Srie  * with any unexpected use presently in effect, ensure these symbols don't get
6277c478bd9Sstevel@tonic-gate  * directly bound to.  Note, that establishing this state here isn't sufficient
6287c478bd9Sstevel@tonic-gate  * to create a syminfo table, only if a syminfo table is being created by some
629c1c6f601Srie  * other symbol directives will the nodirect binding be recorded.  This ensures
6307c478bd9Sstevel@tonic-gate  * we don't create syminfo sections for all objects we create, as this might add
6317c478bd9Sstevel@tonic-gate  * unnecessary bloat to users who haven't explicitly requested extra symbol
6327c478bd9Sstevel@tonic-gate  * information.
6337c478bd9Sstevel@tonic-gate  */
6347c478bd9Sstevel@tonic-gate static uintptr_t
sym_add_spec(const char * name,const char * uname,Word sdaux_id,sd_flag_t sdflags_u,sd_flag_t sdflags,Ofl_desc * ofl)6357c478bd9Sstevel@tonic-gate sym_add_spec(const char *name, const char *uname, Word sdaux_id,
636635216b6SRod Evans     sd_flag_t sdflags_u, sd_flag_t sdflags, Ofl_desc *ofl)
6377c478bd9Sstevel@tonic-gate {
6387c478bd9Sstevel@tonic-gate 	Sym_desc	*sdp;
6397c478bd9Sstevel@tonic-gate 	Sym_desc 	*usdp;
6407c478bd9Sstevel@tonic-gate 	Sym		*sym;
6417c478bd9Sstevel@tonic-gate 	Word		hash;
6427c478bd9Sstevel@tonic-gate 	avl_index_t	where;
6437c478bd9Sstevel@tonic-gate 
6447c478bd9Sstevel@tonic-gate 	/* LINTED */
6457c478bd9Sstevel@tonic-gate 	hash = (Word)elf_hash(uname);
6465aefb655Srie 	if (usdp = ld_sym_find(uname, hash, &where, ofl)) {
6477c478bd9Sstevel@tonic-gate 		/*
6487c478bd9Sstevel@tonic-gate 		 * If the underscore symbol exists and is undefined, or was
6497c478bd9Sstevel@tonic-gate 		 * defined in a shared library, convert it to a local symbol.
6507c478bd9Sstevel@tonic-gate 		 * Otherwise leave it as is and warn the user.
6517c478bd9Sstevel@tonic-gate 		 */
6527c478bd9Sstevel@tonic-gate 		if ((usdp->sd_shndx == SHN_UNDEF) ||
6537c478bd9Sstevel@tonic-gate 		    (usdp->sd_ref != REF_REL_NEED)) {
6547c478bd9Sstevel@tonic-gate 			usdp->sd_ref = REF_REL_NEED;
6557c478bd9Sstevel@tonic-gate 			usdp->sd_shndx = usdp->sd_sym->st_shndx = SHN_ABS;
656635216b6SRod Evans 			usdp->sd_flags |= FLG_SY_SPECSEC | sdflags_u;
6577c478bd9Sstevel@tonic-gate 			usdp->sd_sym->st_info =
6587c478bd9Sstevel@tonic-gate 			    ELF_ST_INFO(STB_GLOBAL, STT_OBJECT);
6597c478bd9Sstevel@tonic-gate 			usdp->sd_isc = NULL;
6607c478bd9Sstevel@tonic-gate 			usdp->sd_sym->st_size = 0;
6617c478bd9Sstevel@tonic-gate 			usdp->sd_sym->st_value = 0;
6627c478bd9Sstevel@tonic-gate 			/* LINTED */
6637c478bd9Sstevel@tonic-gate 			usdp->sd_aux->sa_symspec = (Half)sdaux_id;
6647c478bd9Sstevel@tonic-gate 
6657c478bd9Sstevel@tonic-gate 			/*
6669a411307Srie 			 * If a user hasn't specifically indicated that the
6679a411307Srie 			 * scope of this symbol be made local, then leave it
6689a411307Srie 			 * as global (ie. prevent automatic scoping).  The GOT
6699a411307Srie 			 * should be defined protected, whereas all other
6709a411307Srie 			 * special symbols are tagged as no-direct.
6717c478bd9Sstevel@tonic-gate 			 */
67208278a5eSRod Evans 			if (!SYM_IS_HIDDEN(usdp) &&
673635216b6SRod Evans 			    (sdflags & FLG_SY_DEFAULT)) {
6747c478bd9Sstevel@tonic-gate 				usdp->sd_aux->sa_overndx = VER_NDX_GLOBAL;
6759a411307Srie 				if (sdaux_id == SDAUX_ID_GOT) {
676635216b6SRod Evans 					usdp->sd_flags &= ~FLG_SY_NDIR;
677635216b6SRod Evans 					usdp->sd_flags |= FLG_SY_PROTECT;
6789a411307Srie 					usdp->sd_sym->st_other = STV_PROTECTED;
67933eb6ee1Sab196087 				} else if (
680635216b6SRod Evans 				    ((usdp->sd_flags & FLG_SY_DIR) == 0) &&
6819a411307Srie 				    ((ofl->ofl_flags & FLG_OF_SYMBOLIC) == 0)) {
682635216b6SRod Evans 					usdp->sd_flags |= FLG_SY_NDIR;
6837c478bd9Sstevel@tonic-gate 				}
6849a411307Srie 			}
685635216b6SRod Evans 			usdp->sd_flags |= sdflags;
6867c478bd9Sstevel@tonic-gate 
6877c478bd9Sstevel@tonic-gate 			/*
688c1c6f601Srie 			 * If the reference originated from a mapfile ensure
6897c478bd9Sstevel@tonic-gate 			 * we mark the symbol as used.
6907c478bd9Sstevel@tonic-gate 			 */
6917c478bd9Sstevel@tonic-gate 			if (usdp->sd_flags & FLG_SY_MAPREF)
6927c478bd9Sstevel@tonic-gate 				usdp->sd_flags |= FLG_SY_MAPUSED;
6937c478bd9Sstevel@tonic-gate 
6945aefb655Srie 			DBG_CALL(Dbg_syms_updated(ofl, usdp, uname));
6957c478bd9Sstevel@tonic-gate 		} else
6961007fd6fSAli Bahrami 			ld_eprintf(ofl, ERR_WARNING, MSG_INTL(MSG_SYM_RESERVE),
6971007fd6fSAli Bahrami 			    uname, usdp->sd_file->ifl_name);
6987c478bd9Sstevel@tonic-gate 	} else {
6997c478bd9Sstevel@tonic-gate 		/*
7007c478bd9Sstevel@tonic-gate 		 * If the symbol does not exist create it.
7017c478bd9Sstevel@tonic-gate 		 */
7026b3ba5bdSAli Bahrami 		if ((sym = libld_calloc(sizeof (Sym), 1)) == NULL)
7037c478bd9Sstevel@tonic-gate 			return (S_ERROR);
7047c478bd9Sstevel@tonic-gate 		sym->st_shndx = SHN_ABS;
7057c478bd9Sstevel@tonic-gate 		sym->st_info = ELF_ST_INFO(STB_GLOBAL, STT_OBJECT);
7067c478bd9Sstevel@tonic-gate 		sym->st_size = 0;
7077c478bd9Sstevel@tonic-gate 		sym->st_value = 0;
7085aefb655Srie 		DBG_CALL(Dbg_syms_created(ofl->ofl_lml, uname));
7095aefb655Srie 		if ((usdp = ld_sym_enter(uname, sym, hash, (Ifl_desc *)NULL,
710635216b6SRod Evans 		    ofl, 0, SHN_ABS, (FLG_SY_SPECSEC | sdflags_u), &where)) ==
7117c478bd9Sstevel@tonic-gate 		    (Sym_desc *)S_ERROR)
7127c478bd9Sstevel@tonic-gate 			return (S_ERROR);
7137c478bd9Sstevel@tonic-gate 		usdp->sd_ref = REF_REL_NEED;
7147c478bd9Sstevel@tonic-gate 		/* LINTED */
7157c478bd9Sstevel@tonic-gate 		usdp->sd_aux->sa_symspec = (Half)sdaux_id;
7167c478bd9Sstevel@tonic-gate 
7177c478bd9Sstevel@tonic-gate 		usdp->sd_aux->sa_overndx = VER_NDX_GLOBAL;
7189a411307Srie 
7199a411307Srie 		if (sdaux_id == SDAUX_ID_GOT) {
720635216b6SRod Evans 			usdp->sd_flags |= FLG_SY_PROTECT;
7219a411307Srie 			usdp->sd_sym->st_other = STV_PROTECTED;
722635216b6SRod Evans 		} else if ((sdflags & FLG_SY_DEFAULT) &&
7239a411307Srie 		    ((ofl->ofl_flags & FLG_OF_SYMBOLIC) == 0)) {
724635216b6SRod Evans 			usdp->sd_flags |= FLG_SY_NDIR;
7259a411307Srie 		}
726635216b6SRod Evans 		usdp->sd_flags |= sdflags;
7277c478bd9Sstevel@tonic-gate 	}
7287c478bd9Sstevel@tonic-gate 
72928bda19cSRod Evans 	if (name && (sdp = ld_sym_find(name, SYM_NOHASH, NULL, ofl)) &&
7300bc07c75Srie 	    (sdp->sd_sym->st_shndx == SHN_UNDEF)) {
7317c478bd9Sstevel@tonic-gate 		uchar_t	bind;
7327c478bd9Sstevel@tonic-gate 
7337c478bd9Sstevel@tonic-gate 		/*
7347c478bd9Sstevel@tonic-gate 		 * If the non-underscore symbol exists and is undefined
7357c478bd9Sstevel@tonic-gate 		 * convert it to be a local.  If the underscore has
7367c478bd9Sstevel@tonic-gate 		 * sa_symspec set (ie. it was created above) then simulate this
7377c478bd9Sstevel@tonic-gate 		 * as a weak alias.
7387c478bd9Sstevel@tonic-gate 		 */
7397c478bd9Sstevel@tonic-gate 		sdp->sd_ref = REF_REL_NEED;
7407c478bd9Sstevel@tonic-gate 		sdp->sd_shndx = sdp->sd_sym->st_shndx = SHN_ABS;
7417c478bd9Sstevel@tonic-gate 		sdp->sd_flags |= FLG_SY_SPECSEC;
7427c478bd9Sstevel@tonic-gate 		sdp->sd_isc = NULL;
7437c478bd9Sstevel@tonic-gate 		sdp->sd_sym->st_size = 0;
7447c478bd9Sstevel@tonic-gate 		sdp->sd_sym->st_value = 0;
7457c478bd9Sstevel@tonic-gate 		/* LINTED */
7467c478bd9Sstevel@tonic-gate 		sdp->sd_aux->sa_symspec = (Half)sdaux_id;
7477c478bd9Sstevel@tonic-gate 		if (usdp->sd_aux->sa_symspec) {
7487c478bd9Sstevel@tonic-gate 			usdp->sd_aux->sa_linkndx = 0;
7497c478bd9Sstevel@tonic-gate 			sdp->sd_aux->sa_linkndx = 0;
7507c478bd9Sstevel@tonic-gate 			bind = STB_WEAK;
7517c478bd9Sstevel@tonic-gate 		} else
7527c478bd9Sstevel@tonic-gate 			bind = STB_GLOBAL;
7537c478bd9Sstevel@tonic-gate 		sdp->sd_sym->st_info = ELF_ST_INFO(bind, STT_OBJECT);
7547c478bd9Sstevel@tonic-gate 
7557c478bd9Sstevel@tonic-gate 		/*
7569a411307Srie 		 * If a user hasn't specifically indicated the scope of this
7579a411307Srie 		 * symbol be made local then leave it as global (ie. prevent
7589a411307Srie 		 * automatic scoping).  The GOT should be defined protected,
7599a411307Srie 		 * whereas all other special symbols are tagged as no-direct.
7607c478bd9Sstevel@tonic-gate 		 */
76108278a5eSRod Evans 		if (!SYM_IS_HIDDEN(sdp) &&
762635216b6SRod Evans 		    (sdflags & FLG_SY_DEFAULT)) {
7637c478bd9Sstevel@tonic-gate 			sdp->sd_aux->sa_overndx = VER_NDX_GLOBAL;
7649a411307Srie 			if (sdaux_id == SDAUX_ID_GOT) {
765635216b6SRod Evans 				sdp->sd_flags &= ~FLG_SY_NDIR;
766635216b6SRod Evans 				sdp->sd_flags |= FLG_SY_PROTECT;
7679a411307Srie 				sdp->sd_sym->st_other = STV_PROTECTED;
768635216b6SRod Evans 			} else if (((sdp->sd_flags & FLG_SY_DIR) == 0) &&
7699a411307Srie 			    ((ofl->ofl_flags & FLG_OF_SYMBOLIC) == 0)) {
770635216b6SRod Evans 				sdp->sd_flags |= FLG_SY_NDIR;
7717c478bd9Sstevel@tonic-gate 			}
7729a411307Srie 		}
773635216b6SRod Evans 		sdp->sd_flags |= sdflags;
7747c478bd9Sstevel@tonic-gate 
7757c478bd9Sstevel@tonic-gate 		/*
776c1c6f601Srie 		 * If the reference originated from a mapfile ensure
7777c478bd9Sstevel@tonic-gate 		 * we mark the symbol as used.
7787c478bd9Sstevel@tonic-gate 		 */
7797c478bd9Sstevel@tonic-gate 		if (sdp->sd_flags & FLG_SY_MAPREF)
7807c478bd9Sstevel@tonic-gate 			sdp->sd_flags |= FLG_SY_MAPUSED;
7817c478bd9Sstevel@tonic-gate 
7825aefb655Srie 		DBG_CALL(Dbg_syms_updated(ofl, sdp, name));
7837c478bd9Sstevel@tonic-gate 	}
7847c478bd9Sstevel@tonic-gate 	return (1);
7857c478bd9Sstevel@tonic-gate }
7867c478bd9Sstevel@tonic-gate 
7877c478bd9Sstevel@tonic-gate 
7887c478bd9Sstevel@tonic-gate /*
7897c478bd9Sstevel@tonic-gate  * Undefined symbols can fall into one of four types:
7907c478bd9Sstevel@tonic-gate  *
7916b3ba5bdSAli Bahrami  *  -	the symbol is really undefined (SHN_UNDEF).
7927c478bd9Sstevel@tonic-gate  *
7936b3ba5bdSAli Bahrami  *  -	versioning has been enabled, however this symbol has not been assigned
7947c478bd9Sstevel@tonic-gate  *	to one of the defined versions.
7957c478bd9Sstevel@tonic-gate  *
7966b3ba5bdSAli Bahrami  *  -	the symbol has been defined by an implicitly supplied library, ie. one
7977c478bd9Sstevel@tonic-gate  *	which was encounted because it was NEEDED by another library, rather
7987c478bd9Sstevel@tonic-gate  * 	than from a command line supplied library which would become the only
7997c478bd9Sstevel@tonic-gate  *	dependency of the output file being produced.
8007c478bd9Sstevel@tonic-gate  *
8016b3ba5bdSAli Bahrami  *  -	the symbol has been defined by a version of a shared object that is
8027c478bd9Sstevel@tonic-gate  *	not permitted for this link-edit.
8037c478bd9Sstevel@tonic-gate  *
8047c478bd9Sstevel@tonic-gate  * In all cases the file who made the first reference to this symbol will have
8057c478bd9Sstevel@tonic-gate  * been recorded via the `sa_rfile' pointer.
8067c478bd9Sstevel@tonic-gate  */
8077c478bd9Sstevel@tonic-gate typedef enum {
8087c478bd9Sstevel@tonic-gate 	UNDEF,		NOVERSION,	IMPLICIT,	NOTAVAIL,
8097c478bd9Sstevel@tonic-gate 	BNDLOCAL
8107c478bd9Sstevel@tonic-gate } Type;
8117c478bd9Sstevel@tonic-gate 
8127c478bd9Sstevel@tonic-gate static const Msg format[] = {
8137c478bd9Sstevel@tonic-gate 	MSG_SYM_UND_UNDEF,		/* MSG_INTL(MSG_SYM_UND_UNDEF) */
8147c478bd9Sstevel@tonic-gate 	MSG_SYM_UND_NOVER,		/* MSG_INTL(MSG_SYM_UND_NOVER) */
8157c478bd9Sstevel@tonic-gate 	MSG_SYM_UND_IMPL,		/* MSG_INTL(MSG_SYM_UND_IMPL) */
8167c478bd9Sstevel@tonic-gate 	MSG_SYM_UND_NOTA,		/* MSG_INTL(MSG_SYM_UND_NOTA) */
8177c478bd9Sstevel@tonic-gate 	MSG_SYM_UND_BNDLOCAL		/* MSG_INTL(MSG_SYM_UND_BNDLOCAL) */
8187c478bd9Sstevel@tonic-gate };
8197c478bd9Sstevel@tonic-gate 
8201007fd6fSAli Bahrami /*
8211007fd6fSAli Bahrami  * Issue an undefined symbol message for the given symbol.
8221007fd6fSAli Bahrami  *
8231007fd6fSAli Bahrami  * entry:
8241007fd6fSAli Bahrami  *	ofl - Output descriptor
8251007fd6fSAli Bahrami  *	sdp - Undefined symbol to report
8261007fd6fSAli Bahrami  *	type - Type of undefined symbol
8271007fd6fSAli Bahrami  *	ofl_flag - One of 0, FLG_OF_FATAL, or FLG_OF_WARN.
8281007fd6fSAli Bahrami  *	undef_state - Address of variable to be initialized to 0
8291007fd6fSAli Bahrami  *		before the first call to sym_undef_entry, and passed
8301007fd6fSAli Bahrami  *		to each subsequent call. A non-zero value for *undef_state
8311007fd6fSAli Bahrami  *		indicates that this is not the first call in the series.
8321007fd6fSAli Bahrami  *
8331007fd6fSAli Bahrami  * exit:
8341007fd6fSAli Bahrami  *	If *undef_state is 0, a title is issued.
8351007fd6fSAli Bahrami  *
8361007fd6fSAli Bahrami  *	A message for the undefined symbol is issued.
8371007fd6fSAli Bahrami  *
8381007fd6fSAli Bahrami  *	If ofl_flag is non-zero, its value is OR'd into *undef_state. Otherwise,
8391007fd6fSAli Bahrami  *	all bits other than FLG_OF_FATAL and FLG_OF_WARN are set, in order to
8401007fd6fSAli Bahrami  *	provide *undef_state with a non-zero value. These other bits have
8411007fd6fSAli Bahrami  *	no meaning beyond that, and serve to ensure that *undef_state is
8421007fd6fSAli Bahrami  *	non-zero if sym_undef_entry() has been called.
8431007fd6fSAli Bahrami  */
8445aefb655Srie static void
sym_undef_entry(Ofl_desc * ofl,Sym_desc * sdp,Type type,ofl_flag_t ofl_flag,ofl_flag_t * undef_state)8451007fd6fSAli Bahrami sym_undef_entry(Ofl_desc *ofl, Sym_desc *sdp, Type type, ofl_flag_t ofl_flag,
8461007fd6fSAli Bahrami     ofl_flag_t *undef_state)
8477c478bd9Sstevel@tonic-gate {
8487c478bd9Sstevel@tonic-gate 	const char	*name1, *name2, *name3;
8497c478bd9Sstevel@tonic-gate 	Ifl_desc	*ifl = sdp->sd_file;
8507c478bd9Sstevel@tonic-gate 	Sym_aux		*sap = sdp->sd_aux;
8517c478bd9Sstevel@tonic-gate 
8521007fd6fSAli Bahrami 	if (*undef_state == 0)
8531007fd6fSAli Bahrami 		ld_eprintf(ofl, ERR_NONE, MSG_INTL(MSG_SYM_FMT_UNDEF),
8541007fd6fSAli Bahrami 		    MSG_INTL(MSG_SYM_UNDEF_ITM_11),
8551007fd6fSAli Bahrami 		    MSG_INTL(MSG_SYM_UNDEF_ITM_21),
8561007fd6fSAli Bahrami 		    MSG_INTL(MSG_SYM_UNDEF_ITM_12),
8571007fd6fSAli Bahrami 		    MSG_INTL(MSG_SYM_UNDEF_ITM_22));
8581007fd6fSAli Bahrami 
8591007fd6fSAli Bahrami 	ofl->ofl_flags |= ofl_flag;
8601007fd6fSAli Bahrami 	*undef_state |= ofl_flag ? ofl_flag : ~(FLG_OF_FATAL | FLG_OF_WARN);
8617c478bd9Sstevel@tonic-gate 
8627c478bd9Sstevel@tonic-gate 	switch (type) {
8637c478bd9Sstevel@tonic-gate 	case UNDEF:
8647c478bd9Sstevel@tonic-gate 	case BNDLOCAL:
8657c478bd9Sstevel@tonic-gate 		name1 = sap->sa_rfile;
8667c478bd9Sstevel@tonic-gate 		break;
8677c478bd9Sstevel@tonic-gate 	case NOVERSION:
8687c478bd9Sstevel@tonic-gate 		name1 = ifl->ifl_name;
8697c478bd9Sstevel@tonic-gate 		break;
8707c478bd9Sstevel@tonic-gate 	case IMPLICIT:
8717c478bd9Sstevel@tonic-gate 		name1 = sap->sa_rfile;
8727c478bd9Sstevel@tonic-gate 		name2 = ifl->ifl_name;
8737c478bd9Sstevel@tonic-gate 		break;
8747c478bd9Sstevel@tonic-gate 	case NOTAVAIL:
8757c478bd9Sstevel@tonic-gate 		name1 = sap->sa_rfile;
8767c478bd9Sstevel@tonic-gate 		name2 = sap->sa_vfile;
8777c478bd9Sstevel@tonic-gate 		name3 = ifl->ifl_verndx[sap->sa_dverndx].vi_name;
8787c478bd9Sstevel@tonic-gate 		break;
8797c478bd9Sstevel@tonic-gate 	default:
8807c478bd9Sstevel@tonic-gate 		return;
8817c478bd9Sstevel@tonic-gate 	}
8827c478bd9Sstevel@tonic-gate 
8831007fd6fSAli Bahrami 	ld_eprintf(ofl, ERR_NONE, MSG_INTL(format[type]),
8845aefb655Srie 	    demangle(sdp->sd_name), name1, name2, name3);
8857c478bd9Sstevel@tonic-gate }
8867c478bd9Sstevel@tonic-gate 
8877c478bd9Sstevel@tonic-gate /*
8887c478bd9Sstevel@tonic-gate  * At this point all symbol input processing has been completed, therefore
8897c478bd9Sstevel@tonic-gate  * complete the symbol table entries by generating any necessary internal
8907c478bd9Sstevel@tonic-gate  * symbols.
8917c478bd9Sstevel@tonic-gate  */
8927c478bd9Sstevel@tonic-gate uintptr_t
ld_sym_spec(Ofl_desc * ofl)8935aefb655Srie ld_sym_spec(Ofl_desc *ofl)
8947c478bd9Sstevel@tonic-gate {
8959a411307Srie 	Sym_desc	*sdp;
8969a411307Srie 
8979a411307Srie 	if (ofl->ofl_flags & FLG_OF_RELOBJ)
8989a411307Srie 		return (1);
8997c478bd9Sstevel@tonic-gate 
9005aefb655Srie 	DBG_CALL(Dbg_syms_spec_title(ofl->ofl_lml));
9017c478bd9Sstevel@tonic-gate 
9029a411307Srie 	if (sym_add_spec(MSG_ORIG(MSG_SYM_ETEXT), MSG_ORIG(MSG_SYM_ETEXT_U),
903635216b6SRod Evans 	    SDAUX_ID_ETEXT, 0, (FLG_SY_DEFAULT | FLG_SY_EXPDEF),
90460758829Srie 	    ofl) == S_ERROR)
9057c478bd9Sstevel@tonic-gate 		return (S_ERROR);
9069a411307Srie 	if (sym_add_spec(MSG_ORIG(MSG_SYM_EDATA), MSG_ORIG(MSG_SYM_EDATA_U),
907635216b6SRod Evans 	    SDAUX_ID_EDATA, 0, (FLG_SY_DEFAULT | FLG_SY_EXPDEF),
90860758829Srie 	    ofl) == S_ERROR)
9097c478bd9Sstevel@tonic-gate 		return (S_ERROR);
9109a411307Srie 	if (sym_add_spec(MSG_ORIG(MSG_SYM_END), MSG_ORIG(MSG_SYM_END_U),
911635216b6SRod Evans 	    SDAUX_ID_END, FLG_SY_DYNSORT, (FLG_SY_DEFAULT | FLG_SY_EXPDEF),
91260758829Srie 	    ofl) == S_ERROR)
9137c478bd9Sstevel@tonic-gate 		return (S_ERROR);
9149a411307Srie 	if (sym_add_spec(MSG_ORIG(MSG_SYM_L_END), MSG_ORIG(MSG_SYM_L_END_U),
915635216b6SRod Evans 	    SDAUX_ID_END, 0, FLG_SY_HIDDEN, ofl) == S_ERROR)
9167c478bd9Sstevel@tonic-gate 		return (S_ERROR);
9179a411307Srie 	if (sym_add_spec(MSG_ORIG(MSG_SYM_L_START), MSG_ORIG(MSG_SYM_L_START_U),
918635216b6SRod Evans 	    SDAUX_ID_START, 0, FLG_SY_HIDDEN, ofl) == S_ERROR)
9197c478bd9Sstevel@tonic-gate 		return (S_ERROR);
9207c478bd9Sstevel@tonic-gate 
9217c478bd9Sstevel@tonic-gate 	/*
9229a411307Srie 	 * Historically we've always produced a _DYNAMIC symbol, even for
9239a411307Srie 	 * static executables (in which case its value will be 0).
9247c478bd9Sstevel@tonic-gate 	 */
9259a411307Srie 	if (sym_add_spec(MSG_ORIG(MSG_SYM_DYNAMIC), MSG_ORIG(MSG_SYM_DYNAMIC_U),
926635216b6SRod Evans 	    SDAUX_ID_DYN, FLG_SY_DYNSORT, (FLG_SY_DEFAULT | FLG_SY_EXPDEF),
92760758829Srie 	    ofl) == S_ERROR)
9287c478bd9Sstevel@tonic-gate 		return (S_ERROR);
9297c478bd9Sstevel@tonic-gate 
930d579eb63Sab196087 	if (OFL_ALLOW_DYNSYM(ofl))
9317c478bd9Sstevel@tonic-gate 		if (sym_add_spec(MSG_ORIG(MSG_SYM_PLKTBL),
9327c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_SYM_PLKTBL_U), SDAUX_ID_PLT,
933635216b6SRod Evans 		    FLG_SY_DYNSORT, (FLG_SY_DEFAULT | FLG_SY_EXPDEF),
93460758829Srie 		    ofl) == S_ERROR)
9357c478bd9Sstevel@tonic-gate 			return (S_ERROR);
9367c478bd9Sstevel@tonic-gate 
9379a411307Srie 	/*
9389a411307Srie 	 * A GOT reference will be accompanied by the associated GOT symbol.
9399a411307Srie 	 * Make sure it gets assigned the appropriate special attributes.
9409a411307Srie 	 */
9419a411307Srie 	if (((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_GOFTBL_U),
942635216b6SRod Evans 	    SYM_NOHASH, NULL, ofl)) != NULL) && (sdp->sd_ref != REF_DYN_SEEN)) {
9437c478bd9Sstevel@tonic-gate 		if (sym_add_spec(MSG_ORIG(MSG_SYM_GOFTBL),
944d579eb63Sab196087 		    MSG_ORIG(MSG_SYM_GOFTBL_U), SDAUX_ID_GOT, FLG_SY_DYNSORT,
945635216b6SRod Evans 		    (FLG_SY_DEFAULT | FLG_SY_EXPDEF), ofl) == S_ERROR)
9467c478bd9Sstevel@tonic-gate 			return (S_ERROR);
9477c478bd9Sstevel@tonic-gate 	}
9489a411307Srie 
9497c478bd9Sstevel@tonic-gate 	return (1);
9507c478bd9Sstevel@tonic-gate }
9517c478bd9Sstevel@tonic-gate 
9527c478bd9Sstevel@tonic-gate /*
95308278a5eSRod Evans  * Determine a potential capability symbol's visibility.
95408278a5eSRod Evans  *
95508278a5eSRod Evans  * The -z symbolcap option transforms an object capabilities relocatable object
95608278a5eSRod Evans  * into a symbol capabilities relocatable object.  Any global function symbols,
95708278a5eSRod Evans  * or initialized global data symbols are candidates for transforming into local
95808278a5eSRod Evans  * symbol capabilities definitions.  However, if a user indicates that a symbol
95908278a5eSRod Evans  * should be demoted to local using a mapfile, then there is no need to
96008278a5eSRod Evans  * transform the associated global symbol.
96108278a5eSRod Evans  *
96208278a5eSRod Evans  * Normally, a symbol's visibility is determined after the symbol resolution
96308278a5eSRod Evans  * process, after all symbol state has been gathered and resolved.  However,
96408278a5eSRod Evans  * for -z symbolcap, this determination is too late.  When a global symbol is
96508278a5eSRod Evans  * read from an input file we need to determine it's visibility so as to decide
96608278a5eSRod Evans  * whether to create a local or not.
96708278a5eSRod Evans  *
96808278a5eSRod Evans  * If a user has explicitly defined this symbol as having local scope within a
96908278a5eSRod Evans  * mapfile, then a symbol of the same name already exists.  However, explicit
97008278a5eSRod Evans  * local definitions are uncommon, as most mapfiles define the global symbol
97108278a5eSRod Evans  * requirements together with an auto-reduction directive '*'.  If this state
97208278a5eSRod Evans  * has been defined, then we must make sure that the new symbol isn't a type
97308278a5eSRod Evans  * that can not be demoted to local.
97408278a5eSRod Evans  */
97508278a5eSRod Evans static int
sym_cap_vis(const char * name,Word hash,Sym * sym,Ofl_desc * ofl)97608278a5eSRod Evans sym_cap_vis(const char *name, Word hash, Sym *sym, Ofl_desc *ofl)
97708278a5eSRod Evans {
97808278a5eSRod Evans 	Sym_desc	*sdp;
97908278a5eSRod Evans 	uchar_t		vis;
98008278a5eSRod Evans 	avl_index_t	where;
98108278a5eSRod Evans 	sd_flag_t	sdflags = 0;
98208278a5eSRod Evans 
98308278a5eSRod Evans 	/*
98408278a5eSRod Evans 	 * Determine the visibility of the new symbol.
98508278a5eSRod Evans 	 */
98608278a5eSRod Evans 	vis = ELF_ST_VISIBILITY(sym->st_other);
98708278a5eSRod Evans 	switch (vis) {
98808278a5eSRod Evans 	case STV_EXPORTED:
98908278a5eSRod Evans 		sdflags |= FLG_SY_EXPORT;
99008278a5eSRod Evans 		break;
99108278a5eSRod Evans 	case STV_SINGLETON:
99208278a5eSRod Evans 		sdflags |= FLG_SY_SINGLE;
99308278a5eSRod Evans 		break;
99408278a5eSRod Evans 	}
99508278a5eSRod Evans 
99608278a5eSRod Evans 	/*
99708278a5eSRod Evans 	 * Determine whether a symbol definition already exists, and if so
99808278a5eSRod Evans 	 * obtain the visibility.
99908278a5eSRod Evans 	 */
100008278a5eSRod Evans 	if ((sdp = ld_sym_find(name, hash, &where, ofl)) != NULL)
100108278a5eSRod Evans 		sdflags |= sdp->sd_flags;
100208278a5eSRod Evans 
100308278a5eSRod Evans 	/*
100408278a5eSRod Evans 	 * Determine whether the symbol flags indicate this symbol should be
100508278a5eSRod Evans 	 * hidden.
100608278a5eSRod Evans 	 */
100708278a5eSRod Evans 	if ((ofl->ofl_flags & (FLG_OF_AUTOLCL | FLG_OF_AUTOELM)) &&
100808278a5eSRod Evans 	    ((sdflags & MSK_SY_NOAUTO) == 0))
100908278a5eSRod Evans 		sdflags |= FLG_SY_HIDDEN;
101008278a5eSRod Evans 
101108278a5eSRod Evans 	return ((sdflags & FLG_SY_HIDDEN) == 0);
101208278a5eSRod Evans }
101308278a5eSRod Evans 
101408278a5eSRod Evans /*
10157c478bd9Sstevel@tonic-gate  * This routine checks to see if a symbols visibility needs to be reduced to
10167c478bd9Sstevel@tonic-gate  * either SYMBOLIC or LOCAL.  This routine can be called from either
10177c478bd9Sstevel@tonic-gate  * reloc_init() or sym_validate().
10187c478bd9Sstevel@tonic-gate  */
10197c478bd9Sstevel@tonic-gate void
ld_sym_adjust_vis(Sym_desc * sdp,Ofl_desc * ofl)10205aefb655Srie ld_sym_adjust_vis(Sym_desc *sdp, Ofl_desc *ofl)
10217c478bd9Sstevel@tonic-gate {
102244bac77bSrie 	ofl_flag_t	oflags = ofl->ofl_flags;
10237c478bd9Sstevel@tonic-gate 	Sym		*sym = sdp->sd_sym;
10247c478bd9Sstevel@tonic-gate 
10250bc07c75Srie 	if ((sdp->sd_ref == REF_REL_NEED) &&
10260bc07c75Srie 	    (sdp->sd_sym->st_shndx != SHN_UNDEF)) {
10277c478bd9Sstevel@tonic-gate 		/*
102860758829Srie 		 * If auto-reduction/elimination is enabled, reduce any
102908278a5eSRod Evans 		 * non-versioned, and non-local capabilities global symbols.
103060758829Srie 		 * A symbol is a candidate for auto-reduction/elimination if:
103160758829Srie 		 *
103228bda19cSRod Evans 		 *  -	the symbol wasn't explicitly defined within a mapfile
103360758829Srie 		 *	(in which case all the necessary state has been applied
103460758829Srie 		 *	to the symbol), or
103528bda19cSRod Evans 		 *  -	the symbol isn't one of the family of reserved
103660758829Srie 		 *	special symbols (ie. _end, _etext, etc.), or
103728bda19cSRod Evans 		 *  -	the symbol isn't a SINGLETON, or
103828bda19cSRod Evans 		 *  -	the symbol wasn't explicitly defined within a version
103960758829Srie 		 *	definition associated with an input relocatable object.
104060758829Srie 		 *
10417c478bd9Sstevel@tonic-gate 		 * Indicate that the symbol has been reduced as it may be
10427c478bd9Sstevel@tonic-gate 		 * necessary to print these symbols later.
10437c478bd9Sstevel@tonic-gate 		 */
104444bac77bSrie 		if ((oflags & (FLG_OF_AUTOLCL | FLG_OF_AUTOELM)) &&
1045635216b6SRod Evans 		    ((sdp->sd_flags & MSK_SY_NOAUTO) == 0)) {
1046635216b6SRod Evans 			if ((sdp->sd_flags & FLG_SY_HIDDEN) == 0) {
1047635216b6SRod Evans 				sdp->sd_flags |=
1048635216b6SRod Evans 				    (FLG_SY_REDUCED | FLG_SY_HIDDEN);
104960758829Srie 			}
10507c478bd9Sstevel@tonic-gate 
105144bac77bSrie 			if (oflags & (FLG_OF_REDLSYM | FLG_OF_AUTOELM)) {
1052635216b6SRod Evans 				sdp->sd_flags |= FLG_SY_ELIM;
105360758829Srie 				sym->st_other = STV_ELIMINATE |
105460758829Srie 				    (sym->st_other & ~MSK_SYM_VISIBILITY);
105560758829Srie 			} else if (ELF_ST_VISIBILITY(sym->st_other) !=
105660758829Srie 			    STV_INTERNAL)
105760758829Srie 				sym->st_other = STV_HIDDEN |
105860758829Srie 				    (sym->st_other & ~MSK_SYM_VISIBILITY);
10597c478bd9Sstevel@tonic-gate 		}
10607c478bd9Sstevel@tonic-gate 
10617c478bd9Sstevel@tonic-gate 		/*
10629a411307Srie 		 * If -Bsymbolic is in effect, and the symbol hasn't explicitly
10639a411307Srie 		 * been defined nodirect (via a mapfile), then bind the global
10649a411307Srie 		 * symbol symbolically and assign the STV_PROTECTED visibility
10657c478bd9Sstevel@tonic-gate 		 * attribute.
10667c478bd9Sstevel@tonic-gate 		 */
10677c478bd9Sstevel@tonic-gate 		if ((oflags & FLG_OF_SYMBOLIC) &&
1068635216b6SRod Evans 		    ((sdp->sd_flags & (FLG_SY_HIDDEN | FLG_SY_NDIR)) == 0)) {
1069635216b6SRod Evans 			sdp->sd_flags |= FLG_SY_PROTECT;
10707c478bd9Sstevel@tonic-gate 			if (ELF_ST_VISIBILITY(sym->st_other) == STV_DEFAULT)
10717c478bd9Sstevel@tonic-gate 				sym->st_other = STV_PROTECTED |
10727c478bd9Sstevel@tonic-gate 				    (sym->st_other & ~MSK_SYM_VISIBILITY);
10737c478bd9Sstevel@tonic-gate 		}
10747c478bd9Sstevel@tonic-gate 	}
10757c478bd9Sstevel@tonic-gate 
10767c478bd9Sstevel@tonic-gate 	/*
10777c478bd9Sstevel@tonic-gate 	 * Indicate that this symbol has had it's visibility checked so that
10787c478bd9Sstevel@tonic-gate 	 * we don't need to do this investigation again.
10797c478bd9Sstevel@tonic-gate 	 */
10807c478bd9Sstevel@tonic-gate 	sdp->sd_flags |= FLG_SY_VISIBLE;
10817c478bd9Sstevel@tonic-gate }
10827c478bd9Sstevel@tonic-gate 
10837c478bd9Sstevel@tonic-gate /*
1084c1c6f601Srie  * Make sure a symbol definition is local to the object being built.
1085c1c6f601Srie  */
108657ef7aa9SRod Evans inline static int
ensure_sym_local(Ofl_desc * ofl,Sym_desc * sdp,const char * str)1087c1c6f601Srie ensure_sym_local(Ofl_desc *ofl, Sym_desc *sdp, const char *str)
1088c1c6f601Srie {
1089c1c6f601Srie 	if (sdp->sd_sym->st_shndx == SHN_UNDEF) {
1090c1c6f601Srie 		if (str) {
10911007fd6fSAli Bahrami 			ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_SYM_UNDEF),
10921007fd6fSAli Bahrami 			    str, demangle((char *)sdp->sd_name));
1093c1c6f601Srie 		}
1094c1c6f601Srie 		return (1);
1095c1c6f601Srie 	}
1096c1c6f601Srie 	if (sdp->sd_ref != REF_REL_NEED) {
1097c1c6f601Srie 		if (str) {
10981007fd6fSAli Bahrami 			ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_SYM_EXTERN),
10991007fd6fSAli Bahrami 			    str, demangle((char *)sdp->sd_name),
1100c1c6f601Srie 			    sdp->sd_file->ifl_name);
1101c1c6f601Srie 		}
1102c1c6f601Srie 		return (1);
1103c1c6f601Srie 	}
1104c1c6f601Srie 
1105c1c6f601Srie 	sdp->sd_flags |= FLG_SY_UPREQD;
1106c1c6f601Srie 	if (sdp->sd_isc) {
1107c1c6f601Srie 		sdp->sd_isc->is_flags |= FLG_IS_SECTREF;
1108c1c6f601Srie 		sdp->sd_isc->is_file->ifl_flags |= FLG_IF_FILEREF;
1109c1c6f601Srie 	}
1110c1c6f601Srie 	return (0);
1111c1c6f601Srie }
1112c1c6f601Srie 
1113c1c6f601Srie /*
1114c1c6f601Srie  * Make sure all the symbol definitions required for initarray, finiarray, or
1115c1c6f601Srie  * preinitarray's are local to the object being built.
1116c1c6f601Srie  */
1117c1c6f601Srie static int
ensure_array_local(Ofl_desc * ofl,APlist * apl,const char * str)111857ef7aa9SRod Evans ensure_array_local(Ofl_desc *ofl, APlist *apl, const char *str)
1119c1c6f601Srie {
112057ef7aa9SRod Evans 	Aliste		idx;
1121c1c6f601Srie 	Sym_desc	*sdp;
1122c1c6f601Srie 	int		ret = 0;
1123c1c6f601Srie 
112457ef7aa9SRod Evans 	for (APLIST_TRAVERSE(apl, idx, sdp))
1125c1c6f601Srie 		ret += ensure_sym_local(ofl, sdp, str);
1126c1c6f601Srie 
1127c1c6f601Srie 	return (ret);
1128c1c6f601Srie }
1129c1c6f601Srie 
1130c1c6f601Srie /*
11317c478bd9Sstevel@tonic-gate  * After all symbol table input processing has been finished, and all relocation
11327c478bd9Sstevel@tonic-gate  * counting has been carried out (ie. no more symbols will be read, generated,
11337c478bd9Sstevel@tonic-gate  * or modified), validate and count the relevant entries:
11347c478bd9Sstevel@tonic-gate  *
113508278a5eSRod Evans  *  -	check and print any undefined symbols remaining.  Note that if a symbol
113608278a5eSRod Evans  *	has been defined by virtue of the inclusion of 	an implicit shared
113708278a5eSRod Evans  *	library, it is still classed as undefined.
11387c478bd9Sstevel@tonic-gate  *
113908278a5eSRod Evans  *  -	count the number of global needed symbols together with the size of
114008278a5eSRod Evans  *	their associated name strings (if scoping has been indicated these
114108278a5eSRod Evans  *	symbols may be reduced to locals).
11427c478bd9Sstevel@tonic-gate  *
114308278a5eSRod Evans  *  -	establish the size and alignment requirements for the global .bss
114408278a5eSRod Evans  *	section (the alignment of this section is based on the 	first symbol
114508278a5eSRod Evans  *	that it will contain).
11467c478bd9Sstevel@tonic-gate  */
11477c478bd9Sstevel@tonic-gate uintptr_t
ld_sym_validate(Ofl_desc * ofl)11485aefb655Srie ld_sym_validate(Ofl_desc *ofl)
11497c478bd9Sstevel@tonic-gate {
11507c478bd9Sstevel@tonic-gate 	Sym_avlnode	*sav;
11517c478bd9Sstevel@tonic-gate 	Sym_desc	*sdp;
11527c478bd9Sstevel@tonic-gate 	Sym		*sym;
11531d9df23bSab196087 	ofl_flag_t	oflags = ofl->ofl_flags;
11541d9df23bSab196087 	ofl_flag_t	undef = 0, needed = 0, verdesc = 0;
11557c478bd9Sstevel@tonic-gate 	Xword		bssalign = 0, tlsalign = 0;
1156e64d0ff9SAli Bahrami 	Boolean		need_bss, need_tlsbss;
11577c478bd9Sstevel@tonic-gate 	Xword		bsssize = 0, tlssize = 0;
1158ba2be530Sab196087 #if	defined(_ELF64)
115954d82594Sseizo 	Xword		lbssalign = 0, lbsssize = 0;
1160e64d0ff9SAli Bahrami 	Boolean		need_lbss;
116154d82594Sseizo #endif
116208278a5eSRod Evans 	int		ret, allow_ldynsym;
1163d579eb63Sab196087 	uchar_t		type;
11641007fd6fSAli Bahrami 	ofl_flag_t	undef_state = 0;
11657c478bd9Sstevel@tonic-gate 
11662017c965SRod Evans 	DBG_CALL(Dbg_basic_validate(ofl->ofl_lml));
11672017c965SRod Evans 
11687c478bd9Sstevel@tonic-gate 	/*
1169e64d0ff9SAli Bahrami 	 * The need_XXX booleans are used to determine whether we need to
1170e64d0ff9SAli Bahrami 	 * create each type of bss section. We used to create these sections
1171e64d0ff9SAli Bahrami 	 * if the sum of the required sizes for each type were non-zero.
1172e64d0ff9SAli Bahrami 	 * However, it is possible for a compiler to generate COMMON variables
1173e64d0ff9SAli Bahrami 	 * of zero-length and this tricks that logic --- even zero-length
1174e64d0ff9SAli Bahrami 	 * symbols need an output section.
1175e64d0ff9SAli Bahrami 	 */
1176e64d0ff9SAli Bahrami 	need_bss = need_tlsbss = FALSE;
1177e64d0ff9SAli Bahrami #if	defined(_ELF64)
1178e64d0ff9SAli Bahrami 	need_lbss = FALSE;
1179e64d0ff9SAli Bahrami #endif
1180e64d0ff9SAli Bahrami 
1181e64d0ff9SAli Bahrami 	/*
11821007fd6fSAli Bahrami 	 * Determine how undefined symbols are handled:
11837c478bd9Sstevel@tonic-gate 	 *
11841007fd6fSAli Bahrami 	 * fatal:
11851007fd6fSAli Bahrami 	 *	If this link-edit calls for no undefined symbols to remain
11861007fd6fSAli Bahrami 	 *	(this is the default case when generating an executable but
11871007fd6fSAli Bahrami 	 *	can be enforced for any object using -z defs), a fatal error
11881007fd6fSAli Bahrami 	 *	condition will be indicated.
11891007fd6fSAli Bahrami 	 *
11901007fd6fSAli Bahrami 	 * warning:
11911007fd6fSAli Bahrami 	 *	If we're creating a shared object, and either the -Bsymbolic
11921007fd6fSAli Bahrami 	 *	flag is set, or the user has turned on the -z guidance feature,
11931007fd6fSAli Bahrami 	 *	then a non-fatal warning is issued for each symbol.
11941007fd6fSAli Bahrami 	 *
11951007fd6fSAli Bahrami 	 * ignore:
11961007fd6fSAli Bahrami 	 *	In all other cases, undefined symbols are quietly allowed.
11977c478bd9Sstevel@tonic-gate 	 */
11981007fd6fSAli Bahrami 	if (oflags & FLG_OF_NOUNDEF) {
11997c478bd9Sstevel@tonic-gate 		undef = FLG_OF_FATAL;
12001007fd6fSAli Bahrami 	} else if (oflags & FLG_OF_SHAROBJ) {
12011007fd6fSAli Bahrami 		if ((oflags & FLG_OF_SYMBOLIC) ||
12021007fd6fSAli Bahrami 		    OFL_GUIDANCE(ofl, FLG_OFG_NO_DEFS))
12031007fd6fSAli Bahrami 			undef = FLG_OF_WARN;
12041007fd6fSAli Bahrami 	}
12057c478bd9Sstevel@tonic-gate 
12067c478bd9Sstevel@tonic-gate 	/*
12077c478bd9Sstevel@tonic-gate 	 * If the symbol is referenced from an implicitly included shared object
12087c478bd9Sstevel@tonic-gate 	 * (ie. it's not on the NEEDED list) then the symbol is also classified
12097c478bd9Sstevel@tonic-gate 	 * as undefined and a fatal error condition will be indicated.
12107c478bd9Sstevel@tonic-gate 	 */
12117c478bd9Sstevel@tonic-gate 	if ((oflags & FLG_OF_NOUNDEF) || !(oflags & FLG_OF_SHAROBJ))
12127c478bd9Sstevel@tonic-gate 		needed = FLG_OF_FATAL;
12131007fd6fSAli Bahrami 	else if ((oflags & FLG_OF_SHAROBJ) &&
12141007fd6fSAli Bahrami 	    OFL_GUIDANCE(ofl, FLG_OFG_NO_DEFS))
12151007fd6fSAli Bahrami 		needed = FLG_OF_WARN;
12167c478bd9Sstevel@tonic-gate 
12177c478bd9Sstevel@tonic-gate 	/*
121828bda19cSRod Evans 	 * If the output image is being versioned, then all symbol definitions
121928bda19cSRod Evans 	 * must be associated with a version.  Any symbol that isn't associated
122028bda19cSRod Evans 	 * with a version is classified as undefined, and a fatal error
122128bda19cSRod Evans 	 * condition is indicated.
12227c478bd9Sstevel@tonic-gate 	 */
12237c478bd9Sstevel@tonic-gate 	if ((oflags & FLG_OF_VERDEF) && (ofl->ofl_vercnt > VER_NDX_GLOBAL))
12247c478bd9Sstevel@tonic-gate 		verdesc = FLG_OF_FATAL;
12257c478bd9Sstevel@tonic-gate 
12269039eeafSab196087 	allow_ldynsym = OFL_ALLOW_LDYNSYM(ofl);
1227d579eb63Sab196087 
1228d579eb63Sab196087 	if (allow_ldynsym) {
1229d579eb63Sab196087 		/*
1230d579eb63Sab196087 		 * Normally, we disallow symbols with 0 size from appearing
1231d579eb63Sab196087 		 * in a dyn[sym|tls]sort section. However, there are some
1232d579eb63Sab196087 		 * symbols that serve special purposes that we want to exempt
1233d579eb63Sab196087 		 * from this rule. Look them up, and set their
1234d579eb63Sab196087 		 * FLG_SY_DYNSORT flag.
1235d579eb63Sab196087 		 */
1236d579eb63Sab196087 		static const char *special[] = {
1237d579eb63Sab196087 			MSG_ORIG(MSG_SYM_INIT_U),	/* _init */
1238d579eb63Sab196087 			MSG_ORIG(MSG_SYM_FINI_U),	/* _fini */
1239d579eb63Sab196087 			MSG_ORIG(MSG_SYM_START),	/* _start */
1240d579eb63Sab196087 			NULL
1241d579eb63Sab196087 		};
1242d579eb63Sab196087 		int i;
1243d579eb63Sab196087 
1244d579eb63Sab196087 		for (i = 0; special[i] != NULL; i++) {
1245d579eb63Sab196087 			if (((sdp = ld_sym_find(special[i],
124628bda19cSRod Evans 			    SYM_NOHASH, NULL, ofl)) != NULL) &&
1247d579eb63Sab196087 			    (sdp->sd_sym->st_size == 0)) {
1248d579eb63Sab196087 				if (ld_sym_copy(sdp) == S_ERROR)
1249d579eb63Sab196087 					return (S_ERROR);
1250d579eb63Sab196087 				sdp->sd_flags |= FLG_SY_DYNSORT;
1251d579eb63Sab196087 			}
1252d579eb63Sab196087 		}
1253d579eb63Sab196087 	}
1254d579eb63Sab196087 
12557c478bd9Sstevel@tonic-gate 	/*
12567c478bd9Sstevel@tonic-gate 	 * Collect and validate the globals from the internal symbol table.
12577c478bd9Sstevel@tonic-gate 	 */
12587c478bd9Sstevel@tonic-gate 	for (sav = avl_first(&ofl->ofl_symavl); sav;
12597c478bd9Sstevel@tonic-gate 	    sav = AVL_NEXT(&ofl->ofl_symavl, sav)) {
12607c478bd9Sstevel@tonic-gate 		Is_desc		*isp;
12610bc07c75Srie 		int		undeferr = 0;
126260758829Srie 		uchar_t		vis;
12637c478bd9Sstevel@tonic-gate 
1264635216b6SRod Evans 		sdp = sav->sav_sdp;
12657c478bd9Sstevel@tonic-gate 
12667c478bd9Sstevel@tonic-gate 		/*
12671007fd6fSAli Bahrami 		 * If undefined symbols are allowed, and we're not being
12681007fd6fSAli Bahrami 		 * asked to supply guidance, ignore any symbols that are
12697c478bd9Sstevel@tonic-gate 		 * not needed.
12707c478bd9Sstevel@tonic-gate 		 */
12717c478bd9Sstevel@tonic-gate 		if (!(oflags & FLG_OF_NOUNDEF) &&
12721007fd6fSAli Bahrami 		    !OFL_GUIDANCE(ofl, FLG_OFG_NO_DEFS) &&
12737c478bd9Sstevel@tonic-gate 		    (sdp->sd_ref == REF_DYN_SEEN))
12747c478bd9Sstevel@tonic-gate 			continue;
12757c478bd9Sstevel@tonic-gate 
12767c478bd9Sstevel@tonic-gate 		/*
12777c478bd9Sstevel@tonic-gate 		 * If the symbol originates from an external or parent mapfile
12787c478bd9Sstevel@tonic-gate 		 * reference and hasn't been matched to a reference from a
12797c478bd9Sstevel@tonic-gate 		 * relocatable object, ignore it.
12807c478bd9Sstevel@tonic-gate 		 */
12817c478bd9Sstevel@tonic-gate 		if ((sdp->sd_flags & (FLG_SY_EXTERN | FLG_SY_PARENT)) &&
12827c478bd9Sstevel@tonic-gate 		    ((sdp->sd_flags & FLG_SY_MAPUSED) == 0)) {
12837c478bd9Sstevel@tonic-gate 			sdp->sd_flags |= FLG_SY_INVALID;
12847c478bd9Sstevel@tonic-gate 			continue;
12857c478bd9Sstevel@tonic-gate 		}
12867c478bd9Sstevel@tonic-gate 
12877c478bd9Sstevel@tonic-gate 		sym = sdp->sd_sym;
1288d579eb63Sab196087 		type = ELF_ST_TYPE(sym->st_info);
12897c478bd9Sstevel@tonic-gate 
12907c478bd9Sstevel@tonic-gate 		/*
12917c478bd9Sstevel@tonic-gate 		 * Sanity check TLS.
12927c478bd9Sstevel@tonic-gate 		 */
1293d579eb63Sab196087 		if ((type == STT_TLS) && (sym->st_size != 0) &&
1294d579eb63Sab196087 		    (sym->st_shndx != SHN_UNDEF) &&
12950bc07c75Srie 		    (sym->st_shndx != SHN_COMMON)) {
12967c478bd9Sstevel@tonic-gate 			Is_desc		*isp = sdp->sd_isc;
12977c478bd9Sstevel@tonic-gate 			Ifl_desc	*ifl = sdp->sd_file;
12987c478bd9Sstevel@tonic-gate 
12996b3ba5bdSAli Bahrami 			if ((isp == NULL) || (isp->is_shdr == NULL) ||
13007c478bd9Sstevel@tonic-gate 			    ((isp->is_shdr->sh_flags & SHF_TLS) == 0)) {
13011007fd6fSAli Bahrami 				ld_eprintf(ofl, ERR_FATAL,
13025aefb655Srie 				    MSG_INTL(MSG_SYM_TLS),
13037c478bd9Sstevel@tonic-gate 				    demangle(sdp->sd_name), ifl->ifl_name);
13047c478bd9Sstevel@tonic-gate 				continue;
13057c478bd9Sstevel@tonic-gate 			}
13067c478bd9Sstevel@tonic-gate 		}
13077c478bd9Sstevel@tonic-gate 
13087c478bd9Sstevel@tonic-gate 		if ((sdp->sd_flags & FLG_SY_VISIBLE) == 0)
13095aefb655Srie 			ld_sym_adjust_vis(sdp, ofl);
13107c478bd9Sstevel@tonic-gate 
13117c478bd9Sstevel@tonic-gate 		if ((sdp->sd_flags & FLG_SY_REDUCED) &&
13127c478bd9Sstevel@tonic-gate 		    (oflags & FLG_OF_PROCRED)) {
13135aefb655Srie 			DBG_CALL(Dbg_syms_reduce(ofl, DBG_SYM_REDUCE_GLOBAL,
13145aefb655Srie 			    sdp, 0, 0));
13157c478bd9Sstevel@tonic-gate 		}
13167c478bd9Sstevel@tonic-gate 
13177c478bd9Sstevel@tonic-gate 		/*
131860758829Srie 		 * Record any STV_SINGLETON existence.
131960758829Srie 		 */
132060758829Srie 		if ((vis = ELF_ST_VISIBILITY(sym->st_other)) == STV_SINGLETON)
132160758829Srie 			ofl->ofl_dtflags_1 |= DF_1_SINGLETON;
132260758829Srie 
132360758829Srie 		/*
13247c478bd9Sstevel@tonic-gate 		 * If building a shared object or executable, and this is a
13257c478bd9Sstevel@tonic-gate 		 * non-weak UNDEF symbol with reduced visibility (STV_*), then
13267c478bd9Sstevel@tonic-gate 		 * give a fatal error.
13277c478bd9Sstevel@tonic-gate 		 */
132860758829Srie 		if (((oflags & FLG_OF_RELOBJ) == 0) &&
13290bc07c75Srie 		    (sym->st_shndx == SHN_UNDEF) &&
13307c478bd9Sstevel@tonic-gate 		    (ELF_ST_BIND(sym->st_info) != STB_WEAK)) {
133160758829Srie 			if (vis && (vis != STV_SINGLETON)) {
13321007fd6fSAli Bahrami 				sym_undef_entry(ofl, sdp, BNDLOCAL,
13331007fd6fSAli Bahrami 				    FLG_OF_FATAL, &undef_state);
13347c478bd9Sstevel@tonic-gate 				continue;
13357c478bd9Sstevel@tonic-gate 			}
133660758829Srie 		}
13377c478bd9Sstevel@tonic-gate 
13387c478bd9Sstevel@tonic-gate 		/*
13397c478bd9Sstevel@tonic-gate 		 * If this symbol is defined in a non-allocatable section,
13407c478bd9Sstevel@tonic-gate 		 * reduce it to local symbol.
13417c478bd9Sstevel@tonic-gate 		 */
13427c478bd9Sstevel@tonic-gate 		if (((isp = sdp->sd_isc) != 0) && isp->is_shdr &&
13437c478bd9Sstevel@tonic-gate 		    ((isp->is_shdr->sh_flags & SHF_ALLOC) == 0)) {
1344635216b6SRod Evans 			sdp->sd_flags |= (FLG_SY_REDUCED | FLG_SY_HIDDEN);
13457c478bd9Sstevel@tonic-gate 		}
13467c478bd9Sstevel@tonic-gate 
13477c478bd9Sstevel@tonic-gate 		/*
13487c478bd9Sstevel@tonic-gate 		 * If this symbol originated as a SHN_SUNW_IGNORE, it will have
13497c478bd9Sstevel@tonic-gate 		 * been processed as an SHN_UNDEF.  Return the symbol to its
13507c478bd9Sstevel@tonic-gate 		 * original index for validation, and propagation to the output
13517c478bd9Sstevel@tonic-gate 		 * file.
13527c478bd9Sstevel@tonic-gate 		 */
1353635216b6SRod Evans 		if (sdp->sd_flags & FLG_SY_IGNORE)
13540bc07c75Srie 			sdp->sd_shndx = SHN_SUNW_IGNORE;
13557c478bd9Sstevel@tonic-gate 
13567c478bd9Sstevel@tonic-gate 		if (undef) {
13577c478bd9Sstevel@tonic-gate 			/*
13583b41b08bSab196087 			 * If a non-weak reference remains undefined, or if a
13597c478bd9Sstevel@tonic-gate 			 * mapfile reference is not bound to the relocatable
13607c478bd9Sstevel@tonic-gate 			 * objects that make up the object being built, we have
13617c478bd9Sstevel@tonic-gate 			 * a fatal error.
13627c478bd9Sstevel@tonic-gate 			 *
13637c478bd9Sstevel@tonic-gate 			 * The exceptions are symbols which are defined to be
13647c478bd9Sstevel@tonic-gate 			 * found in the parent (FLG_SY_PARENT), which is really
13657c478bd9Sstevel@tonic-gate 			 * only meaningful for direct binding, or are defined
13667c478bd9Sstevel@tonic-gate 			 * external (FLG_SY_EXTERN) so as to suppress -zdefs
13677c478bd9Sstevel@tonic-gate 			 * errors.
13687c478bd9Sstevel@tonic-gate 			 *
13697c478bd9Sstevel@tonic-gate 			 * Register symbols are always allowed to be UNDEF.
13707c478bd9Sstevel@tonic-gate 			 *
13717c478bd9Sstevel@tonic-gate 			 * Note that we don't include references created via -u
13727c478bd9Sstevel@tonic-gate 			 * in the same shared object binding test.  This is for
13737c478bd9Sstevel@tonic-gate 			 * backward compatibility, in that a number of archive
13747c478bd9Sstevel@tonic-gate 			 * makefile rules used -u to cause archive extraction.
13757c478bd9Sstevel@tonic-gate 			 * These same rules have been cut and pasted to apply
13767c478bd9Sstevel@tonic-gate 			 * to shared objects, and thus although the -u reference
13777c478bd9Sstevel@tonic-gate 			 * is redundant, flagging it as fatal could cause some
13787c478bd9Sstevel@tonic-gate 			 * build to fail.  Also we have documented the use of
13797c478bd9Sstevel@tonic-gate 			 * -u as a mechanism to cause binding to weak version
13807c478bd9Sstevel@tonic-gate 			 * definitions, thus giving users an error condition
13817c478bd9Sstevel@tonic-gate 			 * would be incorrect.
13827c478bd9Sstevel@tonic-gate 			 */
13837c478bd9Sstevel@tonic-gate 			if (!(sdp->sd_flags & FLG_SY_REGSYM) &&
13840bc07c75Srie 			    ((sym->st_shndx == SHN_UNDEF) &&
13857c478bd9Sstevel@tonic-gate 			    ((ELF_ST_BIND(sym->st_info) != STB_WEAK) &&
13867c478bd9Sstevel@tonic-gate 			    ((sdp->sd_flags &
13877c478bd9Sstevel@tonic-gate 			    (FLG_SY_PARENT | FLG_SY_EXTERN)) == 0)) ||
1388635216b6SRod Evans 			    ((sdp->sd_flags &
1389635216b6SRod Evans 			    (FLG_SY_MAPREF | FLG_SY_MAPUSED | FLG_SY_HIDDEN |
1390635216b6SRod Evans 			    FLG_SY_PROTECT)) == FLG_SY_MAPREF))) {
13911007fd6fSAli Bahrami 				sym_undef_entry(ofl, sdp, UNDEF, undef,
13921007fd6fSAli Bahrami 				    &undef_state);
13937c478bd9Sstevel@tonic-gate 				undeferr = 1;
13947c478bd9Sstevel@tonic-gate 			}
13957c478bd9Sstevel@tonic-gate 
13967c478bd9Sstevel@tonic-gate 		} else {
13977c478bd9Sstevel@tonic-gate 			/*
13987c478bd9Sstevel@tonic-gate 			 * For building things like shared objects (or anything
13997c478bd9Sstevel@tonic-gate 			 * -znodefs), undefined symbols are allowed.
14007c478bd9Sstevel@tonic-gate 			 *
14017c478bd9Sstevel@tonic-gate 			 * If a mapfile reference remains undefined the user
14027c478bd9Sstevel@tonic-gate 			 * would probably like a warning at least (they've
14037c478bd9Sstevel@tonic-gate 			 * usually mis-spelt the reference).  Refer to the above
14047c478bd9Sstevel@tonic-gate 			 * comments for discussion on -u references, which
14057c478bd9Sstevel@tonic-gate 			 * are not tested for in the same manner.
14067c478bd9Sstevel@tonic-gate 			 */
14077c478bd9Sstevel@tonic-gate 			if ((sdp->sd_flags &
14087c478bd9Sstevel@tonic-gate 			    (FLG_SY_MAPREF | FLG_SY_MAPUSED)) ==
14097c478bd9Sstevel@tonic-gate 			    FLG_SY_MAPREF) {
14101007fd6fSAli Bahrami 				sym_undef_entry(ofl, sdp, UNDEF, FLG_OF_WARN,
14111007fd6fSAli Bahrami 				    &undef_state);
14127c478bd9Sstevel@tonic-gate 				undeferr = 1;
14137c478bd9Sstevel@tonic-gate 			}
14147c478bd9Sstevel@tonic-gate 		}
14157c478bd9Sstevel@tonic-gate 
14167c478bd9Sstevel@tonic-gate 		/*
14177c478bd9Sstevel@tonic-gate 		 * If this symbol comes from a dependency mark the dependency
14187c478bd9Sstevel@tonic-gate 		 * as required (-z ignore can result in unused dependencies
14197c478bd9Sstevel@tonic-gate 		 * being dropped).  If we need to record dependency versioning
14207c478bd9Sstevel@tonic-gate 		 * information indicate what version of the needed shared object
14217c478bd9Sstevel@tonic-gate 		 * this symbol is part of.  Flag the symbol as undefined if it
14227c478bd9Sstevel@tonic-gate 		 * has not been made available to us.
14237c478bd9Sstevel@tonic-gate 		 */
14247c478bd9Sstevel@tonic-gate 		if ((sdp->sd_ref == REF_DYN_NEED) &&
14257c478bd9Sstevel@tonic-gate 		    (!(sdp->sd_flags & FLG_SY_REFRSD))) {
14267c478bd9Sstevel@tonic-gate 			sdp->sd_file->ifl_flags |= FLG_IF_DEPREQD;
14277c478bd9Sstevel@tonic-gate 
14287c478bd9Sstevel@tonic-gate 			/*
14297c478bd9Sstevel@tonic-gate 			 * Capture that we've bound to a symbol that doesn't
14307c478bd9Sstevel@tonic-gate 			 * allow being directly bound to.
14317c478bd9Sstevel@tonic-gate 			 */
1432635216b6SRod Evans 			if (sdp->sd_flags & FLG_SY_NDIR)
143328bda19cSRod Evans 				ofl->ofl_flags1 |= FLG_OF1_NGLBDIR;
14347c478bd9Sstevel@tonic-gate 
14357c478bd9Sstevel@tonic-gate 			if (sdp->sd_file->ifl_vercnt) {
14367c478bd9Sstevel@tonic-gate 				int		vndx;
14377c478bd9Sstevel@tonic-gate 				Ver_index	*vip;
14387c478bd9Sstevel@tonic-gate 
14397c478bd9Sstevel@tonic-gate 				vndx = sdp->sd_aux->sa_dverndx;
14407c478bd9Sstevel@tonic-gate 				vip = &sdp->sd_file->ifl_verndx[vndx];
14417c478bd9Sstevel@tonic-gate 				if (vip->vi_flags & FLG_VER_AVAIL) {
14427c478bd9Sstevel@tonic-gate 					vip->vi_flags |= FLG_VER_REFER;
14437c478bd9Sstevel@tonic-gate 				} else {
14441007fd6fSAli Bahrami 					sym_undef_entry(ofl, sdp, NOTAVAIL,
14451007fd6fSAli Bahrami 					    FLG_OF_FATAL, &undef_state);
14467c478bd9Sstevel@tonic-gate 					continue;
14477c478bd9Sstevel@tonic-gate 				}
14487c478bd9Sstevel@tonic-gate 			}
14497c478bd9Sstevel@tonic-gate 		}
14507c478bd9Sstevel@tonic-gate 
14517c478bd9Sstevel@tonic-gate 		/*
14527c478bd9Sstevel@tonic-gate 		 * Test that we do not bind to symbol supplied from an implicit
14537c478bd9Sstevel@tonic-gate 		 * shared object.  If a binding is from a weak reference it can
14547c478bd9Sstevel@tonic-gate 		 * be ignored.
14557c478bd9Sstevel@tonic-gate 		 */
14567c478bd9Sstevel@tonic-gate 		if (needed && !undeferr && (sdp->sd_flags & FLG_SY_GLOBREF) &&
14577c478bd9Sstevel@tonic-gate 		    (sdp->sd_ref == REF_DYN_NEED) &&
14587c478bd9Sstevel@tonic-gate 		    (sdp->sd_flags & FLG_SY_NOTAVAIL)) {
14591007fd6fSAli Bahrami 			sym_undef_entry(ofl, sdp, IMPLICIT, needed,
14601007fd6fSAli Bahrami 			    &undef_state);
14611007fd6fSAli Bahrami 			if (needed == FLG_OF_FATAL)
14627c478bd9Sstevel@tonic-gate 				continue;
14637c478bd9Sstevel@tonic-gate 		}
14647c478bd9Sstevel@tonic-gate 
14657c478bd9Sstevel@tonic-gate 		/*
14667c478bd9Sstevel@tonic-gate 		 * Test that a symbol isn't going to be reduced to local scope
14677c478bd9Sstevel@tonic-gate 		 * which actually wants to bind to a shared object - if so it's
14687c478bd9Sstevel@tonic-gate 		 * a fatal error.
14697c478bd9Sstevel@tonic-gate 		 */
14707c478bd9Sstevel@tonic-gate 		if ((sdp->sd_ref == REF_DYN_NEED) &&
1471635216b6SRod Evans 		    (sdp->sd_flags & (FLG_SY_HIDDEN | FLG_SY_PROTECT))) {
14721007fd6fSAli Bahrami 			sym_undef_entry(ofl, sdp, BNDLOCAL, FLG_OF_FATAL,
14731007fd6fSAli Bahrami 			    &undef_state);
14747c478bd9Sstevel@tonic-gate 			continue;
14757c478bd9Sstevel@tonic-gate 		}
14767c478bd9Sstevel@tonic-gate 
14777c478bd9Sstevel@tonic-gate 		/*
14787c478bd9Sstevel@tonic-gate 		 * If the output image is to be versioned then all symbol
147928bda19cSRod Evans 		 * definitions must be associated with a version.  Remove any
148028bda19cSRod Evans 		 * versioning that might be left associated with an undefined
148128bda19cSRod Evans 		 * symbol.
14827c478bd9Sstevel@tonic-gate 		 */
148328bda19cSRod Evans 		if (verdesc && (sdp->sd_ref == REF_REL_NEED)) {
148428bda19cSRod Evans 			if (sym->st_shndx == SHN_UNDEF) {
148528bda19cSRod Evans 				if (sdp->sd_aux && sdp->sd_aux->sa_overndx)
148628bda19cSRod Evans 					sdp->sd_aux->sa_overndx = 0;
148728bda19cSRod Evans 			} else {
148808278a5eSRod Evans 				if (!SYM_IS_HIDDEN(sdp) && sdp->sd_aux &&
14897c478bd9Sstevel@tonic-gate 				    (sdp->sd_aux->sa_overndx == 0)) {
14901007fd6fSAli Bahrami 					sym_undef_entry(ofl, sdp, NOVERSION,
14911007fd6fSAli Bahrami 					    verdesc, &undef_state);
14927c478bd9Sstevel@tonic-gate 					continue;
14937c478bd9Sstevel@tonic-gate 				}
149428bda19cSRod Evans 			}
149528bda19cSRod Evans 		}
14967c478bd9Sstevel@tonic-gate 
14977c478bd9Sstevel@tonic-gate 		/*
14987c478bd9Sstevel@tonic-gate 		 * If we don't need the symbol there's no need to process it
14997c478bd9Sstevel@tonic-gate 		 * any further.
15007c478bd9Sstevel@tonic-gate 		 */
15017c478bd9Sstevel@tonic-gate 		if (sdp->sd_ref == REF_DYN_SEEN)
15027c478bd9Sstevel@tonic-gate 			continue;
15037c478bd9Sstevel@tonic-gate 
15047c478bd9Sstevel@tonic-gate 		/*
15057c478bd9Sstevel@tonic-gate 		 * Calculate the size and alignment requirements for the global
15067c478bd9Sstevel@tonic-gate 		 * .bss and .tls sections.  If we're building a relocatable
15077c478bd9Sstevel@tonic-gate 		 * object only account for scoped COMMON symbols (these will
15087c478bd9Sstevel@tonic-gate 		 * be converted to .bss references).
15097c478bd9Sstevel@tonic-gate 		 *
151035450702SAli Bahrami 		 * When -z nopartial is in effect, partially initialized
151135450702SAli Bahrami 		 * symbols are directed to the special .data section
151235450702SAli Bahrami 		 * created for that purpose (ofl->ofl_isparexpn).
151335450702SAli Bahrami 		 * Otherwise, partially initialized symbols go to .bss.
15147c478bd9Sstevel@tonic-gate 		 *
15157c478bd9Sstevel@tonic-gate 		 * Also refer to make_mvsections() in sunwmove.c
15167c478bd9Sstevel@tonic-gate 		 */
15170bc07c75Srie 		if ((sym->st_shndx == SHN_COMMON) &&
15180bc07c75Srie 		    (((oflags & FLG_OF_RELOBJ) == 0) ||
151908278a5eSRod Evans 		    (SYM_IS_HIDDEN(sdp) && (oflags & FLG_OF_PROCRED)))) {
152057ef7aa9SRod Evans 			if ((sdp->sd_move == NULL) ||
152135450702SAli Bahrami 			    ((sdp->sd_flags & FLG_SY_PAREXPN) == 0)) {
1522d579eb63Sab196087 				if (type != STT_TLS) {
1523e64d0ff9SAli Bahrami 					need_bss = TRUE;
1524e64d0ff9SAli Bahrami 					bsssize = (Xword)S_ROUND(bsssize,
1525e64d0ff9SAli Bahrami 					    sym->st_value) + sym->st_size;
1526e64d0ff9SAli Bahrami 					if (sym->st_value > bssalign)
1527e64d0ff9SAli Bahrami 						bssalign = sym->st_value;
15287c478bd9Sstevel@tonic-gate 				} else {
1529e64d0ff9SAli Bahrami 					need_tlsbss = TRUE;
1530e64d0ff9SAli Bahrami 					tlssize = (Xword)S_ROUND(tlssize,
1531e64d0ff9SAli Bahrami 					    sym->st_value) + sym->st_size;
1532e64d0ff9SAli Bahrami 					if (sym->st_value > tlsalign)
1533e64d0ff9SAli Bahrami 						tlsalign = sym->st_value;
15347c478bd9Sstevel@tonic-gate 				}
15357c478bd9Sstevel@tonic-gate 			}
15367c478bd9Sstevel@tonic-gate 		}
15377c478bd9Sstevel@tonic-gate 
1538ba2be530Sab196087 #if	defined(_ELF64)
153954d82594Sseizo 		/*
154054d82594Sseizo 		 * Calculate the size and alignment requirement for the global
154154d82594Sseizo 		 * .lbss. TLS or partially initialized symbols do not need to be
154254d82594Sseizo 		 * considered yet.
154354d82594Sseizo 		 */
1544ba2be530Sab196087 		if ((ld_targ.t_m.m_mach == EM_AMD64) &&
1545ba2be530Sab196087 		    (sym->st_shndx == SHN_X86_64_LCOMMON)) {
1546e64d0ff9SAli Bahrami 			need_lbss = TRUE;
154754d82594Sseizo 			lbsssize = (Xword)S_ROUND(lbsssize, sym->st_value) +
154854d82594Sseizo 			    sym->st_size;
154954d82594Sseizo 			if (sym->st_value > lbssalign)
155054d82594Sseizo 				lbssalign = sym->st_value;
155154d82594Sseizo 		}
155254d82594Sseizo #endif
15537c478bd9Sstevel@tonic-gate 		/*
15547c478bd9Sstevel@tonic-gate 		 * If a symbol was referenced via the command line
15557c478bd9Sstevel@tonic-gate 		 * (ld -u <>, ...), then this counts as a reference against the
15567c478bd9Sstevel@tonic-gate 		 * symbol. Mark any section that symbol is defined in.
15577c478bd9Sstevel@tonic-gate 		 */
15587c478bd9Sstevel@tonic-gate 		if (((isp = sdp->sd_isc) != 0) &&
15597c478bd9Sstevel@tonic-gate 		    (sdp->sd_flags & FLG_SY_CMDREF)) {
15607c478bd9Sstevel@tonic-gate 			isp->is_flags |= FLG_IS_SECTREF;
15617c478bd9Sstevel@tonic-gate 			isp->is_file->ifl_flags |= FLG_IF_FILEREF;
15627c478bd9Sstevel@tonic-gate 		}
15637c478bd9Sstevel@tonic-gate 
15647c478bd9Sstevel@tonic-gate 		/*
15657c478bd9Sstevel@tonic-gate 		 * Update the symbol count and the associated name string size.
156608278a5eSRod Evans 		 * Note, a capabilities symbol must remain as visible as a
156708278a5eSRod Evans 		 * global symbol.  However, the runtime linker recognizes the
156808278a5eSRod Evans 		 * hidden requirement and ensures the symbol isn't made globally
156908278a5eSRod Evans 		 * available at runtime.
15707c478bd9Sstevel@tonic-gate 		 */
157108278a5eSRod Evans 		if (SYM_IS_HIDDEN(sdp) && (oflags & FLG_OF_PROCRED)) {
15727c478bd9Sstevel@tonic-gate 			/*
157360758829Srie 			 * If any reductions are being processed, keep a count
157460758829Srie 			 * of eliminated symbols, and if the symbol is being
157560758829Srie 			 * reduced to local, count it's size for the .symtab.
15767c478bd9Sstevel@tonic-gate 			 */
1577635216b6SRod Evans 			if (sdp->sd_flags & FLG_SY_ELIM) {
15787c478bd9Sstevel@tonic-gate 				ofl->ofl_elimcnt++;
15797c478bd9Sstevel@tonic-gate 			} else {
15807c478bd9Sstevel@tonic-gate 				ofl->ofl_scopecnt++;
15817c478bd9Sstevel@tonic-gate 				if ((((sdp->sd_flags & FLG_SY_REGSYM) == 0) ||
15827c478bd9Sstevel@tonic-gate 				    sym->st_name) && (st_insert(ofl->ofl_strtab,
15837c478bd9Sstevel@tonic-gate 				    sdp->sd_name) == -1))
15847c478bd9Sstevel@tonic-gate 					return (S_ERROR);
15859039eeafSab196087 				if (allow_ldynsym && sym->st_name &&
1586d579eb63Sab196087 				    ldynsym_symtype[type]) {
15879039eeafSab196087 					ofl->ofl_dynscopecnt++;
15889039eeafSab196087 					if (st_insert(ofl->ofl_dynstrtab,
15899039eeafSab196087 					    sdp->sd_name) == -1)
15909039eeafSab196087 						return (S_ERROR);
1591d579eb63Sab196087 					/* Include it in sort section? */
1592d579eb63Sab196087 					DYNSORT_COUNT(sdp, sym, type, ++);
15939039eeafSab196087 				}
15947c478bd9Sstevel@tonic-gate 			}
15957c478bd9Sstevel@tonic-gate 		} else {
15967c478bd9Sstevel@tonic-gate 			ofl->ofl_globcnt++;
15977c478bd9Sstevel@tonic-gate 
15987c478bd9Sstevel@tonic-gate 			/*
1599635216b6SRod Evans 			 * Check to see if this global variable should go into
1600635216b6SRod Evans 			 * a sort section. Sort sections require a
1601635216b6SRod Evans 			 * .SUNW_ldynsym section, so, don't check unless a
1602635216b6SRod Evans 			 * .SUNW_ldynsym is allowed.
1603d579eb63Sab196087 			 */
1604635216b6SRod Evans 			if (allow_ldynsym)
1605d579eb63Sab196087 				DYNSORT_COUNT(sdp, sym, type, ++);
1606d579eb63Sab196087 
1607d579eb63Sab196087 			/*
16087c478bd9Sstevel@tonic-gate 			 * If global direct bindings are in effect, or this
16097c478bd9Sstevel@tonic-gate 			 * symbol has bound to a dependency which was specified
16107c478bd9Sstevel@tonic-gate 			 * as requiring direct bindings, and it hasn't
16117c478bd9Sstevel@tonic-gate 			 * explicitly been defined as a non-direct binding
16127c478bd9Sstevel@tonic-gate 			 * symbol, mark it.
16137c478bd9Sstevel@tonic-gate 			 */
16147c478bd9Sstevel@tonic-gate 			if (((ofl->ofl_dtflags_1 & DF_1_DIRECT) || (isp &&
16157c478bd9Sstevel@tonic-gate 			    (isp->is_file->ifl_flags & FLG_IF_DIRECT))) &&
1616635216b6SRod Evans 			    ((sdp->sd_flags & FLG_SY_NDIR) == 0))
1617635216b6SRod Evans 				sdp->sd_flags |= FLG_SY_DIR;
16187c478bd9Sstevel@tonic-gate 
16197c478bd9Sstevel@tonic-gate 			/*
16207c478bd9Sstevel@tonic-gate 			 * Insert the symbol name.
16217c478bd9Sstevel@tonic-gate 			 */
16227c478bd9Sstevel@tonic-gate 			if (((sdp->sd_flags & FLG_SY_REGSYM) == 0) ||
16237c478bd9Sstevel@tonic-gate 			    sym->st_name) {
16247c478bd9Sstevel@tonic-gate 				if (st_insert(ofl->ofl_strtab,
16257c478bd9Sstevel@tonic-gate 				    sdp->sd_name) == -1)
16267c478bd9Sstevel@tonic-gate 					return (S_ERROR);
16277c478bd9Sstevel@tonic-gate 
16287c478bd9Sstevel@tonic-gate 				if (!(ofl->ofl_flags & FLG_OF_RELOBJ) &&
16297c478bd9Sstevel@tonic-gate 				    (st_insert(ofl->ofl_dynstrtab,
16307c478bd9Sstevel@tonic-gate 				    sdp->sd_name) == -1))
16317c478bd9Sstevel@tonic-gate 					return (S_ERROR);
16327c478bd9Sstevel@tonic-gate 			}
16337c478bd9Sstevel@tonic-gate 
16347c478bd9Sstevel@tonic-gate 			/*
16357c478bd9Sstevel@tonic-gate 			 * If this section offers a global symbol - record that
16367c478bd9Sstevel@tonic-gate 			 * fact.
16377c478bd9Sstevel@tonic-gate 			 */
16387c478bd9Sstevel@tonic-gate 			if (isp) {
16397c478bd9Sstevel@tonic-gate 				isp->is_flags |= FLG_IS_SECTREF;
16407c478bd9Sstevel@tonic-gate 				isp->is_file->ifl_flags |= FLG_IF_FILEREF;
16417c478bd9Sstevel@tonic-gate 			}
16427c478bd9Sstevel@tonic-gate 		}
16437c478bd9Sstevel@tonic-gate 	}
16447c478bd9Sstevel@tonic-gate 
16457c478bd9Sstevel@tonic-gate 	/*
16461007fd6fSAli Bahrami 	 * Guidance: Use -z defs|nodefs when building shared objects.
16471007fd6fSAli Bahrami 	 *
16481007fd6fSAli Bahrami 	 * Our caller issues this, unless we mask it out here. So we mask it
16491007fd6fSAli Bahrami 	 * out unless we've issued at least one warnings or fatal error.
16501007fd6fSAli Bahrami 	 */
16511007fd6fSAli Bahrami 	if (!((oflags & FLG_OF_SHAROBJ) && OFL_GUIDANCE(ofl, FLG_OFG_NO_DEFS) &&
16521007fd6fSAli Bahrami 	    (undef_state & (FLG_OF_FATAL | FLG_OF_WARN))))
16531007fd6fSAli Bahrami 		ofl->ofl_guideflags |= FLG_OFG_NO_DEFS;
16541007fd6fSAli Bahrami 
16551007fd6fSAli Bahrami 	/*
16567c478bd9Sstevel@tonic-gate 	 * If we've encountered a fatal error during symbol validation then
16577c478bd9Sstevel@tonic-gate 	 * return now.
16587c478bd9Sstevel@tonic-gate 	 */
16597c478bd9Sstevel@tonic-gate 	if (ofl->ofl_flags & FLG_OF_FATAL)
16607c478bd9Sstevel@tonic-gate 		return (1);
16617c478bd9Sstevel@tonic-gate 
16627c478bd9Sstevel@tonic-gate 	/*
16637c478bd9Sstevel@tonic-gate 	 * Now that symbol resolution is completed, scan any register symbols.
16647c478bd9Sstevel@tonic-gate 	 * From now on, we're only interested in those that contribute to the
16657c478bd9Sstevel@tonic-gate 	 * output file.
16667c478bd9Sstevel@tonic-gate 	 */
16677c478bd9Sstevel@tonic-gate 	if (ofl->ofl_regsyms) {
16687c478bd9Sstevel@tonic-gate 		int	ndx;
16697c478bd9Sstevel@tonic-gate 
16707c478bd9Sstevel@tonic-gate 		for (ndx = 0; ndx < ofl->ofl_regsymsno; ndx++) {
16716b3ba5bdSAli Bahrami 			if ((sdp = ofl->ofl_regsyms[ndx]) == NULL)
16727c478bd9Sstevel@tonic-gate 				continue;
16737c478bd9Sstevel@tonic-gate 			if (sdp->sd_ref != REF_REL_NEED) {
16746b3ba5bdSAli Bahrami 				ofl->ofl_regsyms[ndx] = NULL;
16757c478bd9Sstevel@tonic-gate 				continue;
16767c478bd9Sstevel@tonic-gate 			}
16777c478bd9Sstevel@tonic-gate 
16787c478bd9Sstevel@tonic-gate 			ofl->ofl_regsymcnt++;
16797c478bd9Sstevel@tonic-gate 			if (sdp->sd_sym->st_name == 0)
16807c478bd9Sstevel@tonic-gate 				sdp->sd_name = MSG_ORIG(MSG_STR_EMPTY);
16817c478bd9Sstevel@tonic-gate 
168208278a5eSRod Evans 			if (SYM_IS_HIDDEN(sdp) ||
16837c478bd9Sstevel@tonic-gate 			    (ELF_ST_BIND(sdp->sd_sym->st_info) == STB_LOCAL))
16847c478bd9Sstevel@tonic-gate 				ofl->ofl_lregsymcnt++;
16857c478bd9Sstevel@tonic-gate 		}
16867c478bd9Sstevel@tonic-gate 	}
16877c478bd9Sstevel@tonic-gate 
16887c478bd9Sstevel@tonic-gate 	/*
16897c478bd9Sstevel@tonic-gate 	 * Generate the .bss section now that we know its size and alignment.
16907c478bd9Sstevel@tonic-gate 	 */
1691e64d0ff9SAli Bahrami 	if (need_bss) {
169257ef7aa9SRod Evans 		if (ld_make_bss(ofl, bsssize, bssalign,
169357ef7aa9SRod Evans 		    ld_targ.t_id.id_bss) == S_ERROR)
16947c478bd9Sstevel@tonic-gate 			return (S_ERROR);
16957c478bd9Sstevel@tonic-gate 	}
1696e64d0ff9SAli Bahrami 	if (need_tlsbss) {
169757ef7aa9SRod Evans 		if (ld_make_bss(ofl, tlssize, tlsalign,
169857ef7aa9SRod Evans 		    ld_targ.t_id.id_tlsbss) == S_ERROR)
16997c478bd9Sstevel@tonic-gate 			return (S_ERROR);
17007c478bd9Sstevel@tonic-gate 	}
1701ba2be530Sab196087 #if	defined(_ELF64)
1702ba2be530Sab196087 	if ((ld_targ.t_m.m_mach == EM_AMD64) &&
1703e64d0ff9SAli Bahrami 	    need_lbss && !(oflags & FLG_OF_RELOBJ)) {
170457ef7aa9SRod Evans 		if (ld_make_bss(ofl, lbsssize, lbssalign,
170557ef7aa9SRod Evans 		    ld_targ.t_id.id_lbss) == S_ERROR)
170654d82594Sseizo 			return (S_ERROR);
170754d82594Sseizo 	}
170854d82594Sseizo #endif
17097c478bd9Sstevel@tonic-gate 	/*
17107c478bd9Sstevel@tonic-gate 	 * Determine what entry point symbol we need, and if found save its
17117c478bd9Sstevel@tonic-gate 	 * symbol descriptor so that we can update the ELF header entry with the
17127c478bd9Sstevel@tonic-gate 	 * symbols value later (see update_oehdr).  Make sure the symbol is
1713c1c6f601Srie 	 * tagged to ensure its update in case -s is in effect.  Use any -e
17147c478bd9Sstevel@tonic-gate 	 * option first, or the default entry points `_start' and `main'.
17157c478bd9Sstevel@tonic-gate 	 */
1716c1c6f601Srie 	ret = 0;
17177c478bd9Sstevel@tonic-gate 	if (ofl->ofl_entry) {
171828bda19cSRod Evans 		if ((sdp = ld_sym_find(ofl->ofl_entry, SYM_NOHASH,
171928bda19cSRod Evans 		    NULL, ofl)) == NULL) {
17201007fd6fSAli Bahrami 			ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_ARG_NOENTRY),
17211007fd6fSAli Bahrami 			    ofl->ofl_entry);
1722c1c6f601Srie 			ret++;
17231d1fba8aSrie 		} else if (ensure_sym_local(ofl, sdp,
17241d1fba8aSrie 		    MSG_INTL(MSG_SYM_ENTRY)) != 0) {
17251d1fba8aSrie 			ret++;
17261d1fba8aSrie 		} else {
17271d1fba8aSrie 			ofl->ofl_entry = (void *)sdp;
17287c478bd9Sstevel@tonic-gate 		}
17295aefb655Srie 	} else if (((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_START),
173028bda19cSRod Evans 	    SYM_NOHASH, NULL, ofl)) != NULL) && (ensure_sym_local(ofl,
1731c1c6f601Srie 	    sdp, 0) == 0)) {
17327c478bd9Sstevel@tonic-gate 		ofl->ofl_entry = (void *)sdp;
1733c1c6f601Srie 
17345aefb655Srie 	} else if (((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_MAIN),
173528bda19cSRod Evans 	    SYM_NOHASH, NULL, ofl)) != NULL) && (ensure_sym_local(ofl,
1736c1c6f601Srie 	    sdp, 0) == 0)) {
17377c478bd9Sstevel@tonic-gate 		ofl->ofl_entry = (void *)sdp;
17387c478bd9Sstevel@tonic-gate 	}
17397c478bd9Sstevel@tonic-gate 
17407c478bd9Sstevel@tonic-gate 	/*
1741c1c6f601Srie 	 * If ld -zdtrace=<sym> was given, then validate that the symbol is
1742c1c6f601Srie 	 * defined within the current object being built.
17437c478bd9Sstevel@tonic-gate 	 */
17441d1fba8aSrie 	if ((sdp = ofl->ofl_dtracesym) != 0)
1745c1c6f601Srie 		ret += ensure_sym_local(ofl, sdp, MSG_ORIG(MSG_STR_DTRACE));
1746c1c6f601Srie 
1747c1c6f601Srie 	/*
1748c1c6f601Srie 	 * If any initarray, finiarray or preinitarray functions have been
1749c1c6f601Srie 	 * requested, make sure they are defined within the current object
1750c1c6f601Srie 	 * being built.
1751c1c6f601Srie 	 */
175257ef7aa9SRod Evans 	if (ofl->ofl_initarray) {
175357ef7aa9SRod Evans 		ret += ensure_array_local(ofl, ofl->ofl_initarray,
1754c1c6f601Srie 		    MSG_ORIG(MSG_SYM_INITARRAY));
1755c1c6f601Srie 	}
175657ef7aa9SRod Evans 	if (ofl->ofl_finiarray) {
175757ef7aa9SRod Evans 		ret += ensure_array_local(ofl, ofl->ofl_finiarray,
1758c1c6f601Srie 		    MSG_ORIG(MSG_SYM_FINIARRAY));
1759c1c6f601Srie 	}
176057ef7aa9SRod Evans 	if (ofl->ofl_preiarray) {
176157ef7aa9SRod Evans 		ret += ensure_array_local(ofl, ofl->ofl_preiarray,
1762c1c6f601Srie 		    MSG_ORIG(MSG_SYM_PREINITARRAY));
1763c1c6f601Srie 	}
1764c1c6f601Srie 
1765c1c6f601Srie 	if (ret)
17667c478bd9Sstevel@tonic-gate 		return (S_ERROR);
17677c478bd9Sstevel@tonic-gate 
17687c478bd9Sstevel@tonic-gate 	/*
17697c478bd9Sstevel@tonic-gate 	 * If we're required to record any needed dependencies versioning
17707c478bd9Sstevel@tonic-gate 	 * information calculate it now that all symbols have been validated.
17717c478bd9Sstevel@tonic-gate 	 */
17727c478bd9Sstevel@tonic-gate 	if ((oflags & (FLG_OF_VERNEED | FLG_OF_NOVERSEC)) == FLG_OF_VERNEED)
17735aefb655Srie 		return (ld_vers_check_need(ofl));
17747c478bd9Sstevel@tonic-gate 	else
17757c478bd9Sstevel@tonic-gate 		return (1);
17767c478bd9Sstevel@tonic-gate }
17777c478bd9Sstevel@tonic-gate 
17787c478bd9Sstevel@tonic-gate /*
17797c478bd9Sstevel@tonic-gate  * qsort(3c) comparison function.  As an optimization for associating weak
17807c478bd9Sstevel@tonic-gate  * symbols to their strong counterparts sort global symbols according to their
17816b3ba5bdSAli Bahrami  * section index, address and binding.
17827c478bd9Sstevel@tonic-gate  */
17837c478bd9Sstevel@tonic-gate static int
compare(const void * sdpp1,const void * sdpp2)17847c478bd9Sstevel@tonic-gate compare(const void *sdpp1, const void *sdpp2)
17857c478bd9Sstevel@tonic-gate {
17867c478bd9Sstevel@tonic-gate 	Sym_desc	*sdp1 = *((Sym_desc **)sdpp1);
17877c478bd9Sstevel@tonic-gate 	Sym_desc	*sdp2 = *((Sym_desc **)sdpp2);
17887c478bd9Sstevel@tonic-gate 	Sym		*sym1, *sym2;
17897c478bd9Sstevel@tonic-gate 	uchar_t		bind1, bind2;
17907c478bd9Sstevel@tonic-gate 
17917c478bd9Sstevel@tonic-gate 	/*
17927c478bd9Sstevel@tonic-gate 	 * Symbol descriptors may be zero, move these to the front of the
17937c478bd9Sstevel@tonic-gate 	 * sorted array.
17947c478bd9Sstevel@tonic-gate 	 */
17956b3ba5bdSAli Bahrami 	if (sdp1 == NULL)
17967c478bd9Sstevel@tonic-gate 		return (-1);
17976b3ba5bdSAli Bahrami 	if (sdp2 == NULL)
17987c478bd9Sstevel@tonic-gate 		return (1);
17997c478bd9Sstevel@tonic-gate 
18007c478bd9Sstevel@tonic-gate 	sym1 = sdp1->sd_sym;
18017c478bd9Sstevel@tonic-gate 	sym2 = sdp2->sd_sym;
18027c478bd9Sstevel@tonic-gate 
18037c478bd9Sstevel@tonic-gate 	/*
18046b3ba5bdSAli Bahrami 	 * Compare the symbols section index.  This is important when sorting
18056b3ba5bdSAli Bahrami 	 * the symbol tables of relocatable objects.  In this case, a symbols
18066b3ba5bdSAli Bahrami 	 * value is the offset within the associated section, and thus many
18076b3ba5bdSAli Bahrami 	 * symbols can have the same value, but are effectively different
18086b3ba5bdSAli Bahrami 	 * addresses.
18096b3ba5bdSAli Bahrami 	 */
18106b3ba5bdSAli Bahrami 	if (sym1->st_shndx > sym2->st_shndx)
18116b3ba5bdSAli Bahrami 		return (1);
18126b3ba5bdSAli Bahrami 	if (sym1->st_shndx < sym2->st_shndx)
18136b3ba5bdSAli Bahrami 		return (-1);
18146b3ba5bdSAli Bahrami 
18156b3ba5bdSAli Bahrami 	/*
18167c478bd9Sstevel@tonic-gate 	 * Compare the symbols value (address).
18177c478bd9Sstevel@tonic-gate 	 */
18187c478bd9Sstevel@tonic-gate 	if (sym1->st_value > sym2->st_value)
18197c478bd9Sstevel@tonic-gate 		return (1);
18207c478bd9Sstevel@tonic-gate 	if (sym1->st_value < sym2->st_value)
18217c478bd9Sstevel@tonic-gate 		return (-1);
18227c478bd9Sstevel@tonic-gate 
18237c478bd9Sstevel@tonic-gate 	bind1 = ELF_ST_BIND(sym1->st_info);
18247c478bd9Sstevel@tonic-gate 	bind2 = ELF_ST_BIND(sym2->st_info);
18257c478bd9Sstevel@tonic-gate 
18267c478bd9Sstevel@tonic-gate 	/*
18277c478bd9Sstevel@tonic-gate 	 * If two symbols have the same address place the weak symbol before
18287c478bd9Sstevel@tonic-gate 	 * any strong counterpart.
18297c478bd9Sstevel@tonic-gate 	 */
18307c478bd9Sstevel@tonic-gate 	if (bind1 > bind2)
18317c478bd9Sstevel@tonic-gate 		return (-1);
18327c478bd9Sstevel@tonic-gate 	if (bind1 < bind2)
18337c478bd9Sstevel@tonic-gate 		return (1);
18347c478bd9Sstevel@tonic-gate 
18357c478bd9Sstevel@tonic-gate 	return (0);
18367c478bd9Sstevel@tonic-gate }
18377c478bd9Sstevel@tonic-gate 
18387c478bd9Sstevel@tonic-gate /*
183969a0bf0cSab196087  * Issue a MSG_SYM_BADADDR error from ld_sym_process(). This error
184069a0bf0cSab196087  * is issued when a symbol address/size is not contained by the
184169a0bf0cSab196087  * target section.
184269a0bf0cSab196087  *
184369a0bf0cSab196087  * Such objects are at least partially corrupt, and the user would
184469a0bf0cSab196087  * be well advised to be skeptical of them, and to ask their compiler
184569a0bf0cSab196087  * supplier to fix the problem. However, a distinction needs to be
184669a0bf0cSab196087  * made between symbols that reference readonly text, and those that
184769a0bf0cSab196087  * access writable data. Other than throwing off profiling results,
184869a0bf0cSab196087  * the readonly section case is less serious. We have encountered
184969a0bf0cSab196087  * such objects in the field. In order to allow existing objects
185069a0bf0cSab196087  * to continue working, we issue a warning rather than a fatal error
185169a0bf0cSab196087  * if the symbol is against readonly text. Other cases are fatal.
185269a0bf0cSab196087  */
185369a0bf0cSab196087 static void
issue_badaddr_msg(Ifl_desc * ifl,Ofl_desc * ofl,Sym_desc * sdp,Sym * sym,Word shndx)185469a0bf0cSab196087 issue_badaddr_msg(Ifl_desc *ifl, Ofl_desc *ofl, Sym_desc *sdp,
185569a0bf0cSab196087     Sym *sym, Word shndx)
185669a0bf0cSab196087 {
185769a0bf0cSab196087 	Error		err;
185869a0bf0cSab196087 	const char	*msg;
185969a0bf0cSab196087 
186069a0bf0cSab196087 	if ((sdp->sd_isc->is_shdr->sh_flags & (SHF_WRITE | SHF_ALLOC)) ==
186169a0bf0cSab196087 	    SHF_ALLOC) {
186269a0bf0cSab196087 		msg = MSG_INTL(MSG_SYM_BADADDR_ROTXT);
186369a0bf0cSab196087 		err = ERR_WARNING;
186469a0bf0cSab196087 	} else {
186569a0bf0cSab196087 		msg = MSG_INTL(MSG_SYM_BADADDR);
186669a0bf0cSab196087 		err = ERR_FATAL;
186769a0bf0cSab196087 	}
186869a0bf0cSab196087 
18691007fd6fSAli Bahrami 	ld_eprintf(ofl, err, msg, demangle(sdp->sd_name),
187069a0bf0cSab196087 	    ifl->ifl_name, shndx, sdp->sd_isc->is_name,
187169a0bf0cSab196087 	    EC_XWORD(sdp->sd_isc->is_shdr->sh_size),
187269a0bf0cSab196087 	    EC_XWORD(sym->st_value), EC_XWORD(sym->st_size));
187369a0bf0cSab196087 }
187469a0bf0cSab196087 
187508278a5eSRod Evans /*
187608278a5eSRod Evans  * Global symbols that are candidates for translation to local capability
187708278a5eSRod Evans  * symbols under -z symbolcap, are maintained on a local symbol list.  Once
187808278a5eSRod Evans  * all symbols of a file are processed, this list is traversed to cull any
187908278a5eSRod Evans  * unnecessary weak symbol aliases.
188008278a5eSRod Evans  */
188108278a5eSRod Evans typedef struct {
188208278a5eSRod Evans 	Sym_desc	*c_nsdp;	/* new lead symbol */
188308278a5eSRod Evans 	Sym_desc	*c_osdp;	/* original symbol */
188408278a5eSRod Evans 	Cap_group	*c_group;	/* symbol capability group */
188508278a5eSRod Evans 	Word		c_ndx;		/* symbol index */
188608278a5eSRod Evans } Cap_pair;
188769a0bf0cSab196087 
188869a0bf0cSab196087 /*
18897c478bd9Sstevel@tonic-gate  * Process the symbol table for the specified input file.  At this point all
18907c478bd9Sstevel@tonic-gate  * input sections from this input file have been assigned an input section
18917c478bd9Sstevel@tonic-gate  * descriptor which is saved in the `ifl_isdesc' array.
18927c478bd9Sstevel@tonic-gate  *
189308278a5eSRod Evans  *  -	local symbols are saved (as is) if the input file is a 	relocatable
189408278a5eSRod Evans  *	object
18957c478bd9Sstevel@tonic-gate  *
189608278a5eSRod Evans  *  -	global symbols are added to the linkers internal symbol table if they
189708278a5eSRod Evans  *	are not already present, otherwise a symbol resolution function is
189808278a5eSRod Evans  *	called upon to resolve the conflict.
18997c478bd9Sstevel@tonic-gate  */
19007c478bd9Sstevel@tonic-gate uintptr_t
ld_sym_process(Is_desc * isc,Ifl_desc * ifl,Ofl_desc * ofl)19015aefb655Srie ld_sym_process(Is_desc *isc, Ifl_desc *ifl, Ofl_desc *ofl)
19027c478bd9Sstevel@tonic-gate {
190375e45495Sab196087 	/*
190475e45495Sab196087 	 * This macro tests the given symbol to see if it is out of
190575e45495Sab196087 	 * range relative to the section it references.
190675e45495Sab196087 	 *
190775e45495Sab196087 	 * entry:
190875e45495Sab196087 	 *	- ifl is a relative object (ET_REL)
190975e45495Sab196087 	 *	_sdp - Symbol descriptor
191075e45495Sab196087 	 *	_sym - Symbol
191175e45495Sab196087 	 *	_type - Symbol type
191275e45495Sab196087 	 *
191375e45495Sab196087 	 * The following are tested:
191475e45495Sab196087 	 *	- Symbol length is non-zero
191575e45495Sab196087 	 *	- Symbol type is a type that references code or data
191675e45495Sab196087 	 *	- Referenced section is not 0 (indicates an UNDEF symbol)
191775e45495Sab196087 	 *	  and is not in the range of special values above SHN_LORESERVE
191875e45495Sab196087 	 *	  (excluding SHN_XINDEX, which is OK).
191975e45495Sab196087 	 *	- We have a valid section header for the target section
192075e45495Sab196087 	 *
192175e45495Sab196087 	 * If the above are all true, and the symbol position is not
192275e45495Sab196087 	 * contained by the target section, this macro evaluates to
192375e45495Sab196087 	 * True (1). Otherwise, False(0).
192475e45495Sab196087 	 */
192575e45495Sab196087 #define	SYM_LOC_BADADDR(_sdp, _sym, _type) \
1926d579eb63Sab196087 	(_sym->st_size && dynsymsort_symtype[_type] && \
192775e45495Sab196087 	(_sym->st_shndx != SHN_UNDEF) && \
192875e45495Sab196087 	((_sym->st_shndx < SHN_LORESERVE) || \
192975e45495Sab196087 		(_sym->st_shndx == SHN_XINDEX)) && \
193075e45495Sab196087 	_sdp->sd_isc && _sdp->sd_isc->is_shdr && \
193175e45495Sab196087 	((_sym->st_value + _sym->st_size) > _sdp->sd_isc->is_shdr->sh_size))
193275e45495Sab196087 
1933de777a60Sab196087 	Conv_inv_buf_t	inv_buf;
19347c478bd9Sstevel@tonic-gate 	Sym		*sym = (Sym *)isc->is_indata->d_buf;
19356b3ba5bdSAli Bahrami 	Word		*symshndx = NULL;
19367c478bd9Sstevel@tonic-gate 	Shdr		*shdr = isc->is_shdr;
19377c478bd9Sstevel@tonic-gate 	Sym_desc	*sdp;
19387c478bd9Sstevel@tonic-gate 	size_t		strsize;
19397c478bd9Sstevel@tonic-gate 	char		*strs;
19407c478bd9Sstevel@tonic-gate 	uchar_t		type, bind;
19417c478bd9Sstevel@tonic-gate 	Word		ndx, hash, local, total;
19424f680cc6SAli Bahrami 	uchar_t		osabi = ifl->ifl_ehdr->e_ident[EI_OSABI];
19434f680cc6SAli Bahrami 	Half		mach = ifl->ifl_ehdr->e_machine;
19447c478bd9Sstevel@tonic-gate 	Half		etype = ifl->ifl_ehdr->e_type;
194575e45495Sab196087 	int		etype_rel;
19467c478bd9Sstevel@tonic-gate 	const char	*symsecname, *strsecname;
19474a8d0ea7SAli Bahrami 	Word		symsecndx;
19487c478bd9Sstevel@tonic-gate 	avl_index_t	where;
19496b3ba5bdSAli Bahrami 	int		test_gnu_hidden_bit, weak;
195008278a5eSRod Evans 	Cap_desc	*cdp = NULL;
195108278a5eSRod Evans 	Alist		*cappairs = NULL;
19527c478bd9Sstevel@tonic-gate 
19537c478bd9Sstevel@tonic-gate 	/*
19547c478bd9Sstevel@tonic-gate 	 * Its possible that a file may contain more that one symbol table,
19557c478bd9Sstevel@tonic-gate 	 * ie. .dynsym and .symtab in a shared library.  Only process the first
19567c478bd9Sstevel@tonic-gate 	 * table (here, we assume .dynsym comes before .symtab).
19577c478bd9Sstevel@tonic-gate 	 */
19587c478bd9Sstevel@tonic-gate 	if (ifl->ifl_symscnt)
19597c478bd9Sstevel@tonic-gate 		return (1);
19607c478bd9Sstevel@tonic-gate 
19617c478bd9Sstevel@tonic-gate 	if (isc->is_symshndx)
19627c478bd9Sstevel@tonic-gate 		symshndx = isc->is_symshndx->is_indata->d_buf;
19637c478bd9Sstevel@tonic-gate 
19645aefb655Srie 	DBG_CALL(Dbg_syms_process(ofl->ofl_lml, ifl));
19657c478bd9Sstevel@tonic-gate 
19664a8d0ea7SAli Bahrami 	symsecndx = isc->is_scnndx;
19677c478bd9Sstevel@tonic-gate 	if (isc->is_name)
19687c478bd9Sstevel@tonic-gate 		symsecname = isc->is_name;
19697c478bd9Sstevel@tonic-gate 	else
19707c478bd9Sstevel@tonic-gate 		symsecname = MSG_ORIG(MSG_STR_EMPTY);
19717c478bd9Sstevel@tonic-gate 
19727c478bd9Sstevel@tonic-gate 	/*
19737c478bd9Sstevel@tonic-gate 	 * From the symbol tables section header information determine which
19747c478bd9Sstevel@tonic-gate 	 * strtab table is needed to locate the actual symbol names.
19757c478bd9Sstevel@tonic-gate 	 */
19767c478bd9Sstevel@tonic-gate 	if (ifl->ifl_flags & FLG_IF_HSTRTAB) {
19777c478bd9Sstevel@tonic-gate 		ndx = shdr->sh_link;
19787c478bd9Sstevel@tonic-gate 		if ((ndx == 0) || (ndx >= ifl->ifl_shnum)) {
19791007fd6fSAli Bahrami 			ld_eprintf(ofl, ERR_FATAL,
19804a8d0ea7SAli Bahrami 			    MSG_INTL(MSG_FIL_INVSHLINK), ifl->ifl_name,
19814a8d0ea7SAli Bahrami 			    EC_WORD(symsecndx), symsecname, EC_XWORD(ndx));
19827c478bd9Sstevel@tonic-gate 			return (S_ERROR);
19837c478bd9Sstevel@tonic-gate 		}
19847c478bd9Sstevel@tonic-gate 		strsize = ifl->ifl_isdesc[ndx]->is_shdr->sh_size;
19857c478bd9Sstevel@tonic-gate 		strs = ifl->ifl_isdesc[ndx]->is_indata->d_buf;
19867c478bd9Sstevel@tonic-gate 		if (ifl->ifl_isdesc[ndx]->is_name)
19877c478bd9Sstevel@tonic-gate 			strsecname = ifl->ifl_isdesc[ndx]->is_name;
19887c478bd9Sstevel@tonic-gate 		else
19897c478bd9Sstevel@tonic-gate 			strsecname = MSG_ORIG(MSG_STR_EMPTY);
19907c478bd9Sstevel@tonic-gate 	} else {
19917c478bd9Sstevel@tonic-gate 		/*
19927c478bd9Sstevel@tonic-gate 		 * There is no string table section in this input file
19937c478bd9Sstevel@tonic-gate 		 * although there are symbols in this symbol table section.
19947c478bd9Sstevel@tonic-gate 		 * This means that these symbols do not have names.
19957c478bd9Sstevel@tonic-gate 		 * Currently, only scratch register symbols are allowed
19967c478bd9Sstevel@tonic-gate 		 * not to have names.
19977c478bd9Sstevel@tonic-gate 		 */
19987c478bd9Sstevel@tonic-gate 		strsize = 0;
19997c478bd9Sstevel@tonic-gate 		strs = (char *)MSG_ORIG(MSG_STR_EMPTY);
20007c478bd9Sstevel@tonic-gate 		strsecname = MSG_ORIG(MSG_STR_EMPTY);
20017c478bd9Sstevel@tonic-gate 	}
20027c478bd9Sstevel@tonic-gate 
20037c478bd9Sstevel@tonic-gate 	/*
20047c478bd9Sstevel@tonic-gate 	 * Determine the number of local symbols together with the total
20057c478bd9Sstevel@tonic-gate 	 * number we have to process.
20067c478bd9Sstevel@tonic-gate 	 */
20077c478bd9Sstevel@tonic-gate 	total = (Word)(shdr->sh_size / shdr->sh_entsize);
20087c478bd9Sstevel@tonic-gate 	local = shdr->sh_info;
20097c478bd9Sstevel@tonic-gate 
20107c478bd9Sstevel@tonic-gate 	/*
20117c478bd9Sstevel@tonic-gate 	 * Allocate a symbol table index array and a local symbol array
20127c478bd9Sstevel@tonic-gate 	 * (global symbols are processed and added to the ofl->ofl_symbkt[]
20137c478bd9Sstevel@tonic-gate 	 * array).  If we are dealing with a relocatable object, allocate the
20147c478bd9Sstevel@tonic-gate 	 * local symbol descriptors.  If this isn't a relocatable object we
20157c478bd9Sstevel@tonic-gate 	 * still have to process any shared object locals to determine if any
20167c478bd9Sstevel@tonic-gate 	 * register symbols exist.  Although these aren't added to the output
20177c478bd9Sstevel@tonic-gate 	 * image, they are used as part of symbol resolution.
20187c478bd9Sstevel@tonic-gate 	 */
20197c478bd9Sstevel@tonic-gate 	if ((ifl->ifl_oldndx = libld_malloc((size_t)(total *
20206b3ba5bdSAli Bahrami 	    sizeof (Sym_desc *)))) == NULL)
20217c478bd9Sstevel@tonic-gate 		return (S_ERROR);
2022d579eb63Sab196087 	etype_rel = (etype == ET_REL);
202375e45495Sab196087 	if (etype_rel && local) {
20247c478bd9Sstevel@tonic-gate 		if ((ifl->ifl_locs =
20256b3ba5bdSAli Bahrami 		    libld_calloc(sizeof (Sym_desc), local)) == NULL)
20267c478bd9Sstevel@tonic-gate 			return (S_ERROR);
20277c478bd9Sstevel@tonic-gate 		/* LINTED */
20287c478bd9Sstevel@tonic-gate 		ifl->ifl_locscnt = (Word)local;
20297c478bd9Sstevel@tonic-gate 	}
20307c478bd9Sstevel@tonic-gate 	ifl->ifl_symscnt = total;
20317c478bd9Sstevel@tonic-gate 
20327c478bd9Sstevel@tonic-gate 	/*
20337c478bd9Sstevel@tonic-gate 	 * If there are local symbols to save add them to the symbol table
20347c478bd9Sstevel@tonic-gate 	 * index array.
20357c478bd9Sstevel@tonic-gate 	 */
20367c478bd9Sstevel@tonic-gate 	if (local) {
20379039eeafSab196087 		int		allow_ldynsym = OFL_ALLOW_LDYNSYM(ofl);
203824b9abbaSab196087 		Sym_desc	*last_file_sdp = NULL;
203924b9abbaSab196087 		int		last_file_ndx = 0;
204024b9abbaSab196087 
20417c478bd9Sstevel@tonic-gate 		for (sym++, ndx = 1; ndx < local; sym++, ndx++) {
2042635216b6SRod Evans 			sd_flag_t	sdflags = FLG_SY_CLEAN;
2043635216b6SRod Evans 			Word		shndx;
20447c478bd9Sstevel@tonic-gate 			const char	*name;
20457c478bd9Sstevel@tonic-gate 			Sym_desc	*rsdp;
2046ba2be530Sab196087 			int		shndx_bad = 0;
20471da7e599SAli Bahrami 			int		symtab_enter = 1;
20487c478bd9Sstevel@tonic-gate 
20497c478bd9Sstevel@tonic-gate 			/*
2050ba2be530Sab196087 			 * Determine and validate the associated section index.
20517c478bd9Sstevel@tonic-gate 			 */
2052ba2be530Sab196087 			if (symshndx && (sym->st_shndx == SHN_XINDEX)) {
20537c478bd9Sstevel@tonic-gate 				shndx = symshndx[ndx];
2054ba2be530Sab196087 			} else if ((shndx = sym->st_shndx) >= SHN_LORESERVE) {
20557c478bd9Sstevel@tonic-gate 				sdflags |= FLG_SY_SPECSEC;
2056*8878595fSRichard Lowe 			} else if (shndx > ifl->ifl_shnum) {
2057ba2be530Sab196087 				/* We need the name before we can issue error */
2058ba2be530Sab196087 				shndx_bad = 1;
2059ba2be530Sab196087 			}
20607c478bd9Sstevel@tonic-gate 
20617c478bd9Sstevel@tonic-gate 			/*
20627c478bd9Sstevel@tonic-gate 			 * Check if st_name has a valid value or not.
20637c478bd9Sstevel@tonic-gate 			 */
20645aefb655Srie 			if ((name = string(ofl, ifl, sym, strs, strsize, ndx,
20654a8d0ea7SAli Bahrami 			    shndx, symsecndx, symsecname, strsecname,
20661007fd6fSAli Bahrami 			    &sdflags)) == NULL)
20677c478bd9Sstevel@tonic-gate 				continue;
20687c478bd9Sstevel@tonic-gate 
20697c478bd9Sstevel@tonic-gate 			/*
2070ba2be530Sab196087 			 * Now that we have the name, if the section index
2071ba2be530Sab196087 			 * was bad, report it.
2072ba2be530Sab196087 			 */
2073ba2be530Sab196087 			if (shndx_bad) {
20741007fd6fSAli Bahrami 				ld_eprintf(ofl, ERR_WARNING,
2075ba2be530Sab196087 				    MSG_INTL(MSG_SYM_INVSHNDX),
20764a8d0ea7SAli Bahrami 				    demangle_symname(name, symsecname, ndx),
2077ba2be530Sab196087 				    ifl->ifl_name,
20784f680cc6SAli Bahrami 				    conv_sym_shndx(osabi, mach, sym->st_shndx,
20794f680cc6SAli Bahrami 				    CONV_FMT_DECIMAL, &inv_buf));
2080ba2be530Sab196087 				continue;
2081ba2be530Sab196087 			}
2082ba2be530Sab196087 
2083ba2be530Sab196087 			/*
20847c478bd9Sstevel@tonic-gate 			 * If this local symbol table originates from a shared
20857c478bd9Sstevel@tonic-gate 			 * object, then we're only interested in recording
20867c478bd9Sstevel@tonic-gate 			 * register symbols.  As local symbol descriptors aren't
20877c478bd9Sstevel@tonic-gate 			 * allocated for shared objects, one will be allocated
20887c478bd9Sstevel@tonic-gate 			 * to associated with the register symbol.  This symbol
20897c478bd9Sstevel@tonic-gate 			 * won't become part of the output image, but we must
20907c478bd9Sstevel@tonic-gate 			 * process it to test for register conflicts.
20917c478bd9Sstevel@tonic-gate 			 */
209228bda19cSRod Evans 			rsdp = sdp = NULL;
20937c478bd9Sstevel@tonic-gate 			if (sdflags & FLG_SY_REGSYM) {
2094ba2be530Sab196087 				/*
2095ba2be530Sab196087 				 * The presence of FLG_SY_REGSYM means that
2096ba2be530Sab196087 				 * the pointers in ld_targ.t_ms are non-NULL.
2097ba2be530Sab196087 				 */
2098ba2be530Sab196087 				rsdp = (*ld_targ.t_ms.ms_reg_find)(sym, ofl);
2099ba2be530Sab196087 				if (rsdp != 0) {
21007c478bd9Sstevel@tonic-gate 					/*
21017c478bd9Sstevel@tonic-gate 					 * The fact that another register def-
21027c478bd9Sstevel@tonic-gate 					 * inition has been found is fatal.
21037c478bd9Sstevel@tonic-gate 					 * Call the verification routine to get
21047c478bd9Sstevel@tonic-gate 					 * the error message and move on.
21057c478bd9Sstevel@tonic-gate 					 */
2106ba2be530Sab196087 					(void) (*ld_targ.t_ms.ms_reg_check)
2107ba2be530Sab196087 					    (rsdp, sym, name, ifl, ofl);
21087c478bd9Sstevel@tonic-gate 					continue;
21097c478bd9Sstevel@tonic-gate 				}
21107c478bd9Sstevel@tonic-gate 
21117c478bd9Sstevel@tonic-gate 				if (etype == ET_DYN) {
21127c478bd9Sstevel@tonic-gate 					if ((sdp = libld_calloc(
21136b3ba5bdSAli Bahrami 					    sizeof (Sym_desc), 1)) == NULL)
21147c478bd9Sstevel@tonic-gate 						return (S_ERROR);
21157c478bd9Sstevel@tonic-gate 					sdp->sd_ref = REF_DYN_SEEN;
2116ca4eed8bSAli Bahrami 
2117ca4eed8bSAli Bahrami 					/* Will not appear in output object */
21181da7e599SAli Bahrami 					symtab_enter = 0;
21197c478bd9Sstevel@tonic-gate 				}
21207c478bd9Sstevel@tonic-gate 			} else if (etype == ET_DYN)
21217c478bd9Sstevel@tonic-gate 				continue;
21227c478bd9Sstevel@tonic-gate 
21237c478bd9Sstevel@tonic-gate 			/*
21247c478bd9Sstevel@tonic-gate 			 * Fill in the remaining symbol descriptor information.
21257c478bd9Sstevel@tonic-gate 			 */
21266b3ba5bdSAli Bahrami 			if (sdp == NULL) {
21277c478bd9Sstevel@tonic-gate 				sdp = &(ifl->ifl_locs[ndx]);
21287c478bd9Sstevel@tonic-gate 				sdp->sd_ref = REF_REL_NEED;
2129635216b6SRod Evans 				sdp->sd_symndx = ndx;
21307c478bd9Sstevel@tonic-gate 			}
21316b3ba5bdSAli Bahrami 			if (rsdp == NULL) {
21327c478bd9Sstevel@tonic-gate 				sdp->sd_name = name;
21337c478bd9Sstevel@tonic-gate 				sdp->sd_sym = sym;
21347c478bd9Sstevel@tonic-gate 				sdp->sd_shndx = shndx;
21357c478bd9Sstevel@tonic-gate 				sdp->sd_flags = sdflags;
21367c478bd9Sstevel@tonic-gate 				sdp->sd_file = ifl;
21377c478bd9Sstevel@tonic-gate 				ifl->ifl_oldndx[ndx] = sdp;
21387c478bd9Sstevel@tonic-gate 			}
21397c478bd9Sstevel@tonic-gate 
21405aefb655Srie 			DBG_CALL(Dbg_syms_entry(ofl->ofl_lml, ndx, sdp));
21417c478bd9Sstevel@tonic-gate 
21427c478bd9Sstevel@tonic-gate 			/*
21437c478bd9Sstevel@tonic-gate 			 * Reclassify any SHN_SUNW_IGNORE symbols to SHN_UNDEF
21447c478bd9Sstevel@tonic-gate 			 * so as to simplify future processing.
21457c478bd9Sstevel@tonic-gate 			 */
21460bc07c75Srie 			if (sym->st_shndx == SHN_SUNW_IGNORE) {
21477c478bd9Sstevel@tonic-gate 				sdp->sd_shndx = shndx = SHN_UNDEF;
2148635216b6SRod Evans 				sdp->sd_flags |= (FLG_SY_IGNORE | FLG_SY_ELIM);
21497c478bd9Sstevel@tonic-gate 			}
21507c478bd9Sstevel@tonic-gate 
21517c478bd9Sstevel@tonic-gate 			/*
21527c478bd9Sstevel@tonic-gate 			 * Process any register symbols.
21537c478bd9Sstevel@tonic-gate 			 */
21547c478bd9Sstevel@tonic-gate 			if (sdp->sd_flags & FLG_SY_REGSYM) {
21557c478bd9Sstevel@tonic-gate 				/*
21567c478bd9Sstevel@tonic-gate 				 * Add a diagnostic to indicate we've caught a
21577c478bd9Sstevel@tonic-gate 				 * register symbol, as this can be useful if a
21587c478bd9Sstevel@tonic-gate 				 * register conflict is later discovered.
21597c478bd9Sstevel@tonic-gate 				 */
21605aefb655Srie 				DBG_CALL(Dbg_syms_entered(ofl, sym, sdp));
21617c478bd9Sstevel@tonic-gate 
21627c478bd9Sstevel@tonic-gate 				/*
21637c478bd9Sstevel@tonic-gate 				 * If this register symbol hasn't already been
21647c478bd9Sstevel@tonic-gate 				 * recorded, enter it now.
2165ba2be530Sab196087 				 *
2166ba2be530Sab196087 				 * The presence of FLG_SY_REGSYM means that
2167ba2be530Sab196087 				 * the pointers in ld_targ.t_ms are non-NULL.
21687c478bd9Sstevel@tonic-gate 				 */
21696b3ba5bdSAli Bahrami 				if ((rsdp == NULL) &&
2170ba2be530Sab196087 				    ((*ld_targ.t_ms.ms_reg_enter)(sdp, ofl) ==
2171ba2be530Sab196087 				    0))
21727c478bd9Sstevel@tonic-gate 					return (S_ERROR);
21737c478bd9Sstevel@tonic-gate 			}
21747c478bd9Sstevel@tonic-gate 
21757c478bd9Sstevel@tonic-gate 			/*
21767c478bd9Sstevel@tonic-gate 			 * Assign an input section.
21777c478bd9Sstevel@tonic-gate 			 */
21780bc07c75Srie 			if ((sym->st_shndx != SHN_UNDEF) &&
21797c478bd9Sstevel@tonic-gate 			    ((sdp->sd_flags & FLG_SY_SPECSEC) == 0))
21807c478bd9Sstevel@tonic-gate 				sdp->sd_isc = ifl->ifl_isdesc[shndx];
21817c478bd9Sstevel@tonic-gate 
21827c478bd9Sstevel@tonic-gate 			/*
21837c478bd9Sstevel@tonic-gate 			 * If this symbol falls within the range of a section
21847c478bd9Sstevel@tonic-gate 			 * being discarded, then discard the symbol itself.
21857c478bd9Sstevel@tonic-gate 			 * There is no reason to keep this local symbol.
21867c478bd9Sstevel@tonic-gate 			 */
21877c478bd9Sstevel@tonic-gate 			if (sdp->sd_isc &&
21887c478bd9Sstevel@tonic-gate 			    (sdp->sd_isc->is_flags & FLG_IS_DISCARD)) {
21897c478bd9Sstevel@tonic-gate 				sdp->sd_flags |= FLG_SY_ISDISC;
2190a194faf8Srie 				DBG_CALL(Dbg_syms_discarded(ofl->ofl_lml, sdp));
21917c478bd9Sstevel@tonic-gate 				continue;
21927c478bd9Sstevel@tonic-gate 			}
21937c478bd9Sstevel@tonic-gate 
21947c478bd9Sstevel@tonic-gate 			/*
21957c478bd9Sstevel@tonic-gate 			 * Skip any section symbols as new versions of these
21967c478bd9Sstevel@tonic-gate 			 * will be created.
21977c478bd9Sstevel@tonic-gate 			 */
21987c478bd9Sstevel@tonic-gate 			if ((type = ELF_ST_TYPE(sym->st_info)) == STT_SECTION) {
21990bc07c75Srie 				if (sym->st_shndx == SHN_UNDEF) {
22001007fd6fSAli Bahrami 					ld_eprintf(ofl, ERR_WARNING,
22017c478bd9Sstevel@tonic-gate 					    MSG_INTL(MSG_SYM_INVSHNDX),
22024a8d0ea7SAli Bahrami 					    demangle_symname(name, symsecname,
2203ba2be530Sab196087 					    ndx), ifl->ifl_name,
22044f680cc6SAli Bahrami 					    conv_sym_shndx(osabi, mach,
22054f680cc6SAli Bahrami 					    sym->st_shndx, CONV_FMT_DECIMAL,
2206de777a60Sab196087 					    &inv_buf));
22077c478bd9Sstevel@tonic-gate 				}
22087c478bd9Sstevel@tonic-gate 				continue;
22097c478bd9Sstevel@tonic-gate 			}
22107c478bd9Sstevel@tonic-gate 
22117c478bd9Sstevel@tonic-gate 			/*
221275e45495Sab196087 			 * For a relocatable object, if this symbol is defined
221375e45495Sab196087 			 * and has non-zero length and references an address
221475e45495Sab196087 			 * within an associated section, then check its extents
221575e45495Sab196087 			 * to make sure the section boundaries encompass it.
221675e45495Sab196087 			 * If they don't, the ELF file is corrupt.
221775e45495Sab196087 			 */
221824b9abbaSab196087 			if (etype_rel) {
221924b9abbaSab196087 				if (SYM_LOC_BADADDR(sdp, sym, type)) {
222024b9abbaSab196087 					issue_badaddr_msg(ifl, ofl, sdp,
222124b9abbaSab196087 					    sym, shndx);
2222ca4eed8bSAli Bahrami 					if (ofl->ofl_flags & FLG_OF_FATAL)
222375e45495Sab196087 						continue;
222475e45495Sab196087 				}
222575e45495Sab196087 
222675e45495Sab196087 				/*
222724b9abbaSab196087 				 * We have observed relocatable objects
222824b9abbaSab196087 				 * containing identical adjacent STT_FILE
222924b9abbaSab196087 				 * symbols. Discard any other than the first,
223024b9abbaSab196087 				 * as they are all equivalent and the extras
223124b9abbaSab196087 				 * do not add information.
223224b9abbaSab196087 				 *
223324b9abbaSab196087 				 * For the purpose of this test, we assume
223424b9abbaSab196087 				 * that only the symbol type and the string
223524b9abbaSab196087 				 * table offset (st_name) matter.
223624b9abbaSab196087 				 */
223724b9abbaSab196087 				if (type == STT_FILE) {
223824b9abbaSab196087 					int toss = (last_file_sdp != NULL) &&
223924b9abbaSab196087 					    ((ndx - 1) == last_file_ndx) &&
224024b9abbaSab196087 					    (sym->st_name ==
224124b9abbaSab196087 					    last_file_sdp->sd_sym->st_name);
224224b9abbaSab196087 
224324b9abbaSab196087 					last_file_sdp = sdp;
224424b9abbaSab196087 					last_file_ndx = ndx;
224524b9abbaSab196087 					if (toss) {
224624b9abbaSab196087 						sdp->sd_flags |= FLG_SY_INVALID;
224724b9abbaSab196087 						DBG_CALL(Dbg_syms_dup_discarded(
224824b9abbaSab196087 						    ofl->ofl_lml, ndx, sdp));
224924b9abbaSab196087 						continue;
225024b9abbaSab196087 					}
225124b9abbaSab196087 				}
225224b9abbaSab196087 			}
225324b9abbaSab196087 
225424b9abbaSab196087 
225524b9abbaSab196087 			/*
22567c478bd9Sstevel@tonic-gate 			 * Sanity check for TLS
22577c478bd9Sstevel@tonic-gate 			 */
22580bc07c75Srie 			if ((sym->st_size != 0) && ((type == STT_TLS) &&
22590bc07c75Srie 			    (sym->st_shndx != SHN_COMMON))) {
22607c478bd9Sstevel@tonic-gate 				Is_desc	*isp = sdp->sd_isc;
22617c478bd9Sstevel@tonic-gate 
22626b3ba5bdSAli Bahrami 				if ((isp == NULL) || (isp->is_shdr == NULL) ||
22637c478bd9Sstevel@tonic-gate 				    ((isp->is_shdr->sh_flags & SHF_TLS) == 0)) {
22641007fd6fSAli Bahrami 					ld_eprintf(ofl, ERR_FATAL,
22657c478bd9Sstevel@tonic-gate 					    MSG_INTL(MSG_SYM_TLS),
22667c478bd9Sstevel@tonic-gate 					    demangle(sdp->sd_name),
22677c478bd9Sstevel@tonic-gate 					    ifl->ifl_name);
22687c478bd9Sstevel@tonic-gate 					continue;
22697c478bd9Sstevel@tonic-gate 				}
22707c478bd9Sstevel@tonic-gate 			}
22717c478bd9Sstevel@tonic-gate 
22727c478bd9Sstevel@tonic-gate 			/*
22737c478bd9Sstevel@tonic-gate 			 * Carry our some basic sanity checks (these are just
22747c478bd9Sstevel@tonic-gate 			 * some of the erroneous symbol entries we've come
22757c478bd9Sstevel@tonic-gate 			 * across, there's probably a lot more).  The symbol
22767c478bd9Sstevel@tonic-gate 			 * will not be carried forward to the output file, which
22777c478bd9Sstevel@tonic-gate 			 * won't be a problem unless a relocation is required
22787c478bd9Sstevel@tonic-gate 			 * against it.
22797c478bd9Sstevel@tonic-gate 			 */
22807c478bd9Sstevel@tonic-gate 			if (((sdp->sd_flags & FLG_SY_SPECSEC) &&
22810bc07c75Srie 			    ((sym->st_shndx == SHN_COMMON)) ||
22820bc07c75Srie 			    ((type == STT_FILE) &&
22830bc07c75Srie 			    (sym->st_shndx != SHN_ABS))) ||
22846b3ba5bdSAli Bahrami 			    (sdp->sd_isc && (sdp->sd_isc->is_osdesc == NULL))) {
22851007fd6fSAli Bahrami 				ld_eprintf(ofl, ERR_WARNING,
22865aefb655Srie 				    MSG_INTL(MSG_SYM_INVSHNDX),
22874a8d0ea7SAli Bahrami 				    demangle_symname(name, symsecname, ndx),
2288ba2be530Sab196087 				    ifl->ifl_name,
22894f680cc6SAli Bahrami 				    conv_sym_shndx(osabi, mach, sym->st_shndx,
22904f680cc6SAli Bahrami 				    CONV_FMT_DECIMAL, &inv_buf));
22917c478bd9Sstevel@tonic-gate 				sdp->sd_isc = NULL;
22927c478bd9Sstevel@tonic-gate 				sdp->sd_flags |= FLG_SY_INVALID;
22937c478bd9Sstevel@tonic-gate 				continue;
22947c478bd9Sstevel@tonic-gate 			}
22957c478bd9Sstevel@tonic-gate 
22967c478bd9Sstevel@tonic-gate 			/*
22977c478bd9Sstevel@tonic-gate 			 * As these local symbols will become part of the output
22987c478bd9Sstevel@tonic-gate 			 * image, record their number and name string size.
22997c478bd9Sstevel@tonic-gate 			 * Globals are counted after all input file processing
23007c478bd9Sstevel@tonic-gate 			 * (and hence symbol resolution) is complete during
23017c478bd9Sstevel@tonic-gate 			 * sym_validate().
23027c478bd9Sstevel@tonic-gate 			 */
23031da7e599SAli Bahrami 			if (!(ofl->ofl_flags & FLG_OF_REDLSYM) &&
23041da7e599SAli Bahrami 			    symtab_enter) {
23057c478bd9Sstevel@tonic-gate 				ofl->ofl_locscnt++;
23067c478bd9Sstevel@tonic-gate 
23077c478bd9Sstevel@tonic-gate 				if ((((sdp->sd_flags & FLG_SY_REGSYM) == 0) ||
23087c478bd9Sstevel@tonic-gate 				    sym->st_name) && (st_insert(ofl->ofl_strtab,
23097c478bd9Sstevel@tonic-gate 				    sdp->sd_name) == -1))
23107c478bd9Sstevel@tonic-gate 					return (S_ERROR);
23119039eeafSab196087 
23129039eeafSab196087 				if (allow_ldynsym && sym->st_name &&
2313d579eb63Sab196087 				    ldynsym_symtype[type]) {
23149039eeafSab196087 					ofl->ofl_dynlocscnt++;
23159039eeafSab196087 					if (st_insert(ofl->ofl_dynstrtab,
23169039eeafSab196087 					    sdp->sd_name) == -1)
23179039eeafSab196087 						return (S_ERROR);
2318d579eb63Sab196087 					/* Include it in sort section? */
2319d579eb63Sab196087 					DYNSORT_COUNT(sdp, sym, type, ++);
23209039eeafSab196087 				}
23217c478bd9Sstevel@tonic-gate 			}
23227c478bd9Sstevel@tonic-gate 		}
23237c478bd9Sstevel@tonic-gate 	}
23247c478bd9Sstevel@tonic-gate 
23257c478bd9Sstevel@tonic-gate 	/*
2326d840867fSab196087 	 * The GNU ld interprets the top bit of the 16-bit Versym value
2327d840867fSab196087 	 * (0x8000) as the "hidden" bit. If this bit is set, the linker
2328d840867fSab196087 	 * is supposed to act as if that symbol does not exist. The Solaris
2329d840867fSab196087 	 * linker does not support this mechanism, or the model of interface
2330d840867fSab196087 	 * evolution that it allows, but we honor it in GNU ld produced
2331d840867fSab196087 	 * objects in order to interoperate with them.
2332d840867fSab196087 	 *
2333d840867fSab196087 	 * Determine if we should honor the GNU hidden bit for this file.
2334d840867fSab196087 	 */
2335d840867fSab196087 	test_gnu_hidden_bit = ((ifl->ifl_flags & FLG_IF_GNUVER) != 0) &&
2336d840867fSab196087 	    (ifl->ifl_versym != NULL);
2337d840867fSab196087 
2338d840867fSab196087 	/*
233908278a5eSRod Evans 	 * Determine whether object capabilities for this file are being
234008278a5eSRod Evans 	 * converted into symbol capabilities.  If so, global function symbols,
234108278a5eSRod Evans 	 * and initialized global data symbols, need special translation and
234208278a5eSRod Evans 	 * processing.
234308278a5eSRod Evans 	 */
234408278a5eSRod Evans 	if ((etype == ET_REL) && (ifl->ifl_flags & FLG_IF_OTOSCAP))
234508278a5eSRod Evans 		cdp = ifl->ifl_caps;
234608278a5eSRod Evans 
234708278a5eSRod Evans 	/*
23487c478bd9Sstevel@tonic-gate 	 * Now scan the global symbols entering them in the internal symbol
23497c478bd9Sstevel@tonic-gate 	 * table or resolving them as necessary.
23507c478bd9Sstevel@tonic-gate 	 */
23517c478bd9Sstevel@tonic-gate 	sym = (Sym *)isc->is_indata->d_buf;
23527c478bd9Sstevel@tonic-gate 	sym += local;
23536b3ba5bdSAli Bahrami 	weak = 0;
23547c478bd9Sstevel@tonic-gate 	/* LINTED */
23557c478bd9Sstevel@tonic-gate 	for (ndx = (int)local; ndx < total; sym++, ndx++) {
23567c478bd9Sstevel@tonic-gate 		const char	*name;
2357635216b6SRod Evans 		sd_flag_t	sdflags = 0;
2358635216b6SRod Evans 		Word		shndx;
2359ba2be530Sab196087 		int		shndx_bad = 0;
2360635216b6SRod Evans 		Sym		*nsym = sym;
236108278a5eSRod Evans 		Cap_pair	*cpp = NULL;
236208278a5eSRod Evans 		uchar_t		ntype;
23637c478bd9Sstevel@tonic-gate 
23647c478bd9Sstevel@tonic-gate 		/*
2365ba2be530Sab196087 		 * Determine and validate the associated section index.
23667c478bd9Sstevel@tonic-gate 		 */
2367635216b6SRod Evans 		if (symshndx && (nsym->st_shndx == SHN_XINDEX)) {
23687c478bd9Sstevel@tonic-gate 			shndx = symshndx[ndx];
2369635216b6SRod Evans 		} else if ((shndx = nsym->st_shndx) >= SHN_LORESERVE) {
23707c478bd9Sstevel@tonic-gate 			sdflags |= FLG_SY_SPECSEC;
2371*8878595fSRichard Lowe 		} else if (shndx > ifl->ifl_shnum) {
2372ba2be530Sab196087 			/* We need the name before we can issue error */
2373ba2be530Sab196087 			shndx_bad = 1;
23747c478bd9Sstevel@tonic-gate 		}
23757c478bd9Sstevel@tonic-gate 
23767c478bd9Sstevel@tonic-gate 		/*
23777c478bd9Sstevel@tonic-gate 		 * Check if st_name has a valid value or not.
23787c478bd9Sstevel@tonic-gate 		 */
2379635216b6SRod Evans 		if ((name = string(ofl, ifl, nsym, strs, strsize, ndx, shndx,
23801007fd6fSAli Bahrami 		    symsecndx, symsecname, strsecname, &sdflags)) == NULL)
23817c478bd9Sstevel@tonic-gate 			continue;
23827c478bd9Sstevel@tonic-gate 
23837c478bd9Sstevel@tonic-gate 		/*
238408278a5eSRod Evans 		 * Now that we have the name, report an erroneous section index.
2385ba2be530Sab196087 		 */
2386ba2be530Sab196087 		if (shndx_bad) {
23871007fd6fSAli Bahrami 			ld_eprintf(ofl, ERR_WARNING, MSG_INTL(MSG_SYM_INVSHNDX),
23884a8d0ea7SAli Bahrami 			    demangle_symname(name, symsecname, ndx),
2389ba2be530Sab196087 			    ifl->ifl_name,
2390635216b6SRod Evans 			    conv_sym_shndx(osabi, mach, nsym->st_shndx,
23914f680cc6SAli Bahrami 			    CONV_FMT_DECIMAL, &inv_buf));
2392ba2be530Sab196087 			continue;
2393ba2be530Sab196087 		}
2394ba2be530Sab196087 
2395ba2be530Sab196087 		/*
2396d840867fSab196087 		 * Test for the GNU hidden bit, and ignore symbols that
2397d840867fSab196087 		 * have it set.
23983b41b08bSab196087 		 */
2399d840867fSab196087 		if (test_gnu_hidden_bit &&
2400d840867fSab196087 		    ((ifl->ifl_versym[ndx] & 0x8000) != 0))
24013b41b08bSab196087 			continue;
24023b41b08bSab196087 
24033b41b08bSab196087 		/*
24047c478bd9Sstevel@tonic-gate 		 * The linker itself will generate symbols for _end, _etext,
24057c478bd9Sstevel@tonic-gate 		 * _edata, _DYNAMIC and _PROCEDURE_LINKAGE_TABLE_, so don't
24067c478bd9Sstevel@tonic-gate 		 * bother entering these symbols from shared objects.  This
24077c478bd9Sstevel@tonic-gate 		 * results in some wasted resolution processing, which is hard
24087c478bd9Sstevel@tonic-gate 		 * to feel, but if nothing else, pollutes diagnostic relocation
24097c478bd9Sstevel@tonic-gate 		 * output.
24107c478bd9Sstevel@tonic-gate 		 */
2411635216b6SRod Evans 		if (name[0] && (etype == ET_DYN) && (nsym->st_size == 0) &&
2412635216b6SRod Evans 		    (ELF_ST_TYPE(nsym->st_info) == STT_OBJECT) &&
24137c478bd9Sstevel@tonic-gate 		    (name[0] == '_') && ((name[1] == 'e') ||
24147c478bd9Sstevel@tonic-gate 		    (name[1] == 'D') || (name[1] == 'P')) &&
24157c478bd9Sstevel@tonic-gate 		    ((strcmp(name, MSG_ORIG(MSG_SYM_ETEXT_U)) == 0) ||
24167c478bd9Sstevel@tonic-gate 		    (strcmp(name, MSG_ORIG(MSG_SYM_EDATA_U)) == 0) ||
24177c478bd9Sstevel@tonic-gate 		    (strcmp(name, MSG_ORIG(MSG_SYM_END_U)) == 0) ||
24187c478bd9Sstevel@tonic-gate 		    (strcmp(name, MSG_ORIG(MSG_SYM_DYNAMIC_U)) == 0) ||
24197c478bd9Sstevel@tonic-gate 		    (strcmp(name, MSG_ORIG(MSG_SYM_PLKTBL_U)) == 0))) {
24207c478bd9Sstevel@tonic-gate 			ifl->ifl_oldndx[ndx] = 0;
24217c478bd9Sstevel@tonic-gate 			continue;
24227c478bd9Sstevel@tonic-gate 		}
24237c478bd9Sstevel@tonic-gate 
24247c478bd9Sstevel@tonic-gate 		/*
2425cdcc71c0SAli Bahrami 		 * The '-z wrap=XXX' option emulates the GNU ld --wrap=XXX
2426cdcc71c0SAli Bahrami 		 * option. When XXX is the symbol to be wrapped:
2427cdcc71c0SAli Bahrami 		 *
2428cdcc71c0SAli Bahrami 		 *  -	An undefined reference to XXX is converted to __wrap_XXX
2429cdcc71c0SAli Bahrami 		 *  -	An undefined reference to __real_XXX is converted to XXX
2430cdcc71c0SAli Bahrami 		 *
2431cdcc71c0SAli Bahrami 		 * The idea is that the user can supply a wrapper function
2432cdcc71c0SAli Bahrami 		 * __wrap_XXX that does some work, and then uses the name
2433cdcc71c0SAli Bahrami 		 * __real_XXX to pass the call on to the real function. The
2434cdcc71c0SAli Bahrami 		 * wrapper objects are linked with the original unmodified
2435cdcc71c0SAli Bahrami 		 * objects to produce a wrapped version of the output object.
2436cdcc71c0SAli Bahrami 		 */
2437cdcc71c0SAli Bahrami 		if (ofl->ofl_wrap && name[0] && (shndx == SHN_UNDEF)) {
2438cdcc71c0SAli Bahrami 			WrapSymNode wsn, *wsnp;
2439cdcc71c0SAli Bahrami 
2440cdcc71c0SAli Bahrami 			/*
2441cdcc71c0SAli Bahrami 			 * If this is the __real_XXX form, advance the
2442cdcc71c0SAli Bahrami 			 * pointer to reference the wrapped name.
2443cdcc71c0SAli Bahrami 			 */
2444cdcc71c0SAli Bahrami 			wsn.wsn_name = name;
2445cdcc71c0SAli Bahrami 			if ((*name == '_') &&
2446cdcc71c0SAli Bahrami 			    (strncmp(name, MSG_ORIG(MSG_STR_UU_REAL_U),
2447cdcc71c0SAli Bahrami 			    MSG_STR_UU_REAL_U_SIZE) == 0))
2448cdcc71c0SAli Bahrami 				wsn.wsn_name += MSG_STR_UU_REAL_U_SIZE;
2449cdcc71c0SAli Bahrami 
2450cdcc71c0SAli Bahrami 			/*
2451cdcc71c0SAli Bahrami 			 * Is this symbol in the wrap AVL tree? If so, map
2452cdcc71c0SAli Bahrami 			 * XXX to __wrap_XXX, and __real_XXX to XXX. Note that
2453cdcc71c0SAli Bahrami 			 * wsn.wsn_name will equal the current value of name
2454cdcc71c0SAli Bahrami 			 * if the __real_ prefix is not present.
2455cdcc71c0SAli Bahrami 			 */
2456cdcc71c0SAli Bahrami 			if ((wsnp = avl_find(ofl->ofl_wrap, &wsn, 0)) != NULL) {
2457cdcc71c0SAli Bahrami 				const char *old_name = name;
2458cdcc71c0SAli Bahrami 
2459cdcc71c0SAli Bahrami 				name = (wsn.wsn_name == name) ?
2460cdcc71c0SAli Bahrami 				    wsnp->wsn_wrapname : wsn.wsn_name;
2461cdcc71c0SAli Bahrami 				DBG_CALL(Dbg_syms_wrap(ofl->ofl_lml, ndx,
2462cdcc71c0SAli Bahrami 				    old_name, name));
2463cdcc71c0SAli Bahrami 			}
2464cdcc71c0SAli Bahrami 		}
2465cdcc71c0SAli Bahrami 
2466cdcc71c0SAli Bahrami 		/*
24677c478bd9Sstevel@tonic-gate 		 * Determine and validate the symbols binding.
24687c478bd9Sstevel@tonic-gate 		 */
2469635216b6SRod Evans 		bind = ELF_ST_BIND(nsym->st_info);
24707c478bd9Sstevel@tonic-gate 		if ((bind != STB_GLOBAL) && (bind != STB_WEAK)) {
24711007fd6fSAli Bahrami 			ld_eprintf(ofl, ERR_WARNING, MSG_INTL(MSG_SYM_NONGLOB),
24724a8d0ea7SAli Bahrami 			    demangle_symname(name, symsecname, ndx),
2473de777a60Sab196087 			    ifl->ifl_name,
2474de777a60Sab196087 			    conv_sym_info_bind(bind, 0, &inv_buf));
24757c478bd9Sstevel@tonic-gate 			continue;
24767c478bd9Sstevel@tonic-gate 		}
24776b3ba5bdSAli Bahrami 		if (bind == STB_WEAK)
24786b3ba5bdSAli Bahrami 			weak++;
24797c478bd9Sstevel@tonic-gate 
24807c478bd9Sstevel@tonic-gate 		/*
24817c478bd9Sstevel@tonic-gate 		 * If this symbol falls within the range of a section being
24827c478bd9Sstevel@tonic-gate 		 * discarded, then discard the symbol itself.
24837c478bd9Sstevel@tonic-gate 		 */
24847c478bd9Sstevel@tonic-gate 		if (((sdflags & FLG_SY_SPECSEC) == 0) &&
2485635216b6SRod Evans 		    (nsym->st_shndx != SHN_UNDEF)) {
24867c478bd9Sstevel@tonic-gate 			Is_desc	*isp;
24877c478bd9Sstevel@tonic-gate 
24887c478bd9Sstevel@tonic-gate 			if (shndx >= ifl->ifl_shnum) {
24897c478bd9Sstevel@tonic-gate 				/*
24907c478bd9Sstevel@tonic-gate 				 * Carry our some basic sanity checks
24917c478bd9Sstevel@tonic-gate 				 * The symbol will not be carried forward to
24927c478bd9Sstevel@tonic-gate 				 * the output file, which won't be a problem
24937c478bd9Sstevel@tonic-gate 				 * unless a relocation is required against it.
24947c478bd9Sstevel@tonic-gate 				 */
24951007fd6fSAli Bahrami 				ld_eprintf(ofl, ERR_WARNING,
2496ba2be530Sab196087 				    MSG_INTL(MSG_SYM_INVSHNDX),
24974a8d0ea7SAli Bahrami 				    demangle_symname(name, symsecname, ndx),
24980bc07c75Srie 				    ifl->ifl_name,
2499635216b6SRod Evans 				    conv_sym_shndx(osabi, mach, nsym->st_shndx,
25004f680cc6SAli Bahrami 				    CONV_FMT_DECIMAL, &inv_buf));
25017c478bd9Sstevel@tonic-gate 				continue;
25027c478bd9Sstevel@tonic-gate 			}
25037c478bd9Sstevel@tonic-gate 
25047c478bd9Sstevel@tonic-gate 			isp = ifl->ifl_isdesc[shndx];
25057c478bd9Sstevel@tonic-gate 			if (isp && (isp->is_flags & FLG_IS_DISCARD)) {
25067c478bd9Sstevel@tonic-gate 				if ((sdp =
25076b3ba5bdSAli Bahrami 				    libld_calloc(sizeof (Sym_desc), 1)) == NULL)
25087c478bd9Sstevel@tonic-gate 					return (S_ERROR);
25097c478bd9Sstevel@tonic-gate 
25107c478bd9Sstevel@tonic-gate 				/*
25117c478bd9Sstevel@tonic-gate 				 * Create a dummy symbol entry so that if we
25127c478bd9Sstevel@tonic-gate 				 * find any references to this discarded symbol
25137c478bd9Sstevel@tonic-gate 				 * we can compensate.
25147c478bd9Sstevel@tonic-gate 				 */
25157c478bd9Sstevel@tonic-gate 				sdp->sd_name = name;
2516635216b6SRod Evans 				sdp->sd_sym = nsym;
25177c478bd9Sstevel@tonic-gate 				sdp->sd_file = ifl;
25187c478bd9Sstevel@tonic-gate 				sdp->sd_isc = isp;
25197c478bd9Sstevel@tonic-gate 				sdp->sd_flags = FLG_SY_ISDISC;
25207c478bd9Sstevel@tonic-gate 				ifl->ifl_oldndx[ndx] = sdp;
25217c478bd9Sstevel@tonic-gate 
2522a194faf8Srie 				DBG_CALL(Dbg_syms_discarded(ofl->ofl_lml, sdp));
25237c478bd9Sstevel@tonic-gate 				continue;
25247c478bd9Sstevel@tonic-gate 			}
25257c478bd9Sstevel@tonic-gate 		}
25267c478bd9Sstevel@tonic-gate 
25277c478bd9Sstevel@tonic-gate 		/*
252808278a5eSRod Evans 		 * If object capabilities for this file are being converted
252908278a5eSRod Evans 		 * into symbol capabilities, then:
253008278a5eSRod Evans 		 *
253108278a5eSRod Evans 		 *  -	Any global function, or initialized global data symbol
253208278a5eSRod Evans 		 *	definitions (ie., those that are not associated with
253308278a5eSRod Evans 		 *	special symbol types, ie., ABS, COMMON, etc.), and which
253408278a5eSRod Evans 		 *	have not been reduced to locals, are converted to symbol
253508278a5eSRod Evans 		 *	references (UNDEF).  This ensures that any reference to
253608278a5eSRod Evans 		 *	the original symbol, for example from a relocation, get
253708278a5eSRod Evans 		 *	associated to a capabilities family lead symbol, ie., a
253808278a5eSRod Evans 		 *	generic instance.
253908278a5eSRod Evans 		 *
254008278a5eSRod Evans 		 *  -	For each global function, or object symbol definition,
254108278a5eSRod Evans 		 *	a new local symbol is created.  The function or object
254208278a5eSRod Evans 		 *	is renamed using the capabilities CA_SUNW_ID definition
254308278a5eSRod Evans 		 *	(which might have been fabricated for this purpose -
254408278a5eSRod Evans 		 *	see get_cap_group()).  The new symbol name is:
254508278a5eSRod Evans 		 *
254608278a5eSRod Evans 		 *	    <original name>%<capability group identifier>
254708278a5eSRod Evans 		 *
254808278a5eSRod Evans 		 *	This symbol is associated to the same location, and
254908278a5eSRod Evans 		 *	becomes a capabilities family member.
255008278a5eSRod Evans 		 */
255108278a5eSRod Evans 		/* LINTED */
255208278a5eSRod Evans 		hash = (Word)elf_hash(name);
255308278a5eSRod Evans 
255408278a5eSRod Evans 		ntype = ELF_ST_TYPE(nsym->st_info);
255508278a5eSRod Evans 		if (cdp && (nsym->st_shndx != SHN_UNDEF) &&
255608278a5eSRod Evans 		    ((sdflags & FLG_SY_SPECSEC) == 0) &&
255708278a5eSRod Evans 		    ((ntype == STT_FUNC) || (ntype == STT_OBJECT))) {
255808278a5eSRod Evans 			/*
255908278a5eSRod Evans 			 * Determine this symbol's visibility.  If a mapfile has
256008278a5eSRod Evans 			 * indicated this symbol should be local, then there's
256108278a5eSRod Evans 			 * no point in transforming this global symbol to a
256208278a5eSRod Evans 			 * capabilities symbol.  Otherwise, create a symbol
256308278a5eSRod Evans 			 * capability pair descriptor to record this symbol as
256408278a5eSRod Evans 			 * a candidate for translation.
256508278a5eSRod Evans 			 */
256608278a5eSRod Evans 			if (sym_cap_vis(name, hash, sym, ofl) &&
256708278a5eSRod Evans 			    ((cpp = alist_append(&cappairs, NULL,
256808278a5eSRod Evans 			    sizeof (Cap_pair), AL_CNT_CAP_PAIRS)) == NULL))
256908278a5eSRod Evans 				return (S_ERROR);
257008278a5eSRod Evans 		}
257108278a5eSRod Evans 
257208278a5eSRod Evans 		if (cpp) {
257308278a5eSRod Evans 			Sym	*rsym;
257408278a5eSRod Evans 
257508278a5eSRod Evans 			DBG_CALL(Dbg_syms_cap_convert(ofl, ndx, name, nsym));
257608278a5eSRod Evans 
257708278a5eSRod Evans 			/*
257808278a5eSRod Evans 			 * Allocate a new symbol descriptor to represent the
257908278a5eSRod Evans 			 * transformed global symbol.  The descriptor points
258008278a5eSRod Evans 			 * to the original symbol information (which might
258108278a5eSRod Evans 			 * indicate a global or weak visibility).  The symbol
258208278a5eSRod Evans 			 * information will be transformed into a local symbol
258308278a5eSRod Evans 			 * later, after any weak aliases are culled.
258408278a5eSRod Evans 			 */
258508278a5eSRod Evans 			if ((cpp->c_osdp =
258608278a5eSRod Evans 			    libld_malloc(sizeof (Sym_desc))) == NULL)
258708278a5eSRod Evans 				return (S_ERROR);
258808278a5eSRod Evans 
258908278a5eSRod Evans 			cpp->c_osdp->sd_name = name;
259008278a5eSRod Evans 			cpp->c_osdp->sd_sym = nsym;
259108278a5eSRod Evans 			cpp->c_osdp->sd_shndx = shndx;
259208278a5eSRod Evans 			cpp->c_osdp->sd_file = ifl;
259308278a5eSRod Evans 			cpp->c_osdp->sd_isc = ifl->ifl_isdesc[shndx];
259408278a5eSRod Evans 			cpp->c_osdp->sd_ref = REF_REL_NEED;
259508278a5eSRod Evans 
259608278a5eSRod Evans 			/*
259708278a5eSRod Evans 			 * Save the capabilities group this symbol belongs to,
259808278a5eSRod Evans 			 * and the original symbol index.
259908278a5eSRod Evans 			 */
260008278a5eSRod Evans 			cpp->c_group = cdp->ca_groups->apl_data[0];
260108278a5eSRod Evans 			cpp->c_ndx = ndx;
260208278a5eSRod Evans 
260308278a5eSRod Evans 			/*
260408278a5eSRod Evans 			 * Replace the original symbol definition with a symbol
260508278a5eSRod Evans 			 * reference.  Make sure this reference isn't left as a
260608278a5eSRod Evans 			 * weak.
260708278a5eSRod Evans 			 */
260808278a5eSRod Evans 			if ((rsym = libld_malloc(sizeof (Sym))) == NULL)
260908278a5eSRod Evans 				return (S_ERROR);
261008278a5eSRod Evans 
261108278a5eSRod Evans 			*rsym = *nsym;
261208278a5eSRod Evans 
261308278a5eSRod Evans 			rsym->st_info = ELF_ST_INFO(STB_GLOBAL, ntype);
261408278a5eSRod Evans 			rsym->st_shndx = shndx = SHN_UNDEF;
261508278a5eSRod Evans 			rsym->st_value = 0;
261608278a5eSRod Evans 			rsym->st_size = 0;
261708278a5eSRod Evans 
261808278a5eSRod Evans 			sdflags |= FLG_SY_CAP;
261908278a5eSRod Evans 
262008278a5eSRod Evans 			nsym = rsym;
262108278a5eSRod Evans 		}
262208278a5eSRod Evans 
262308278a5eSRod Evans 		/*
26247c478bd9Sstevel@tonic-gate 		 * If the symbol does not already exist in the internal symbol
26257c478bd9Sstevel@tonic-gate 		 * table add it, otherwise resolve the conflict.  If the symbol
26267c478bd9Sstevel@tonic-gate 		 * from this file is kept, retain its symbol table index for
26277c478bd9Sstevel@tonic-gate 		 * possible use in associating a global alias.
26287c478bd9Sstevel@tonic-gate 		 */
26295aefb655Srie 		if ((sdp = ld_sym_find(name, hash, &where, ofl)) == NULL) {
26305aefb655Srie 			DBG_CALL(Dbg_syms_global(ofl->ofl_lml, ndx, name));
2631635216b6SRod Evans 			if ((sdp = ld_sym_enter(name, nsym, hash, ifl, ofl, ndx,
2632635216b6SRod Evans 			    shndx, sdflags, &where)) == (Sym_desc *)S_ERROR)
26337c478bd9Sstevel@tonic-gate 				return (S_ERROR);
26347c478bd9Sstevel@tonic-gate 
2635635216b6SRod Evans 		} else if (ld_sym_resolve(sdp, nsym, ifl, ofl, ndx, shndx,
26367c478bd9Sstevel@tonic-gate 		    sdflags) == S_ERROR)
26377c478bd9Sstevel@tonic-gate 			return (S_ERROR);
26387c478bd9Sstevel@tonic-gate 
26397c478bd9Sstevel@tonic-gate 		/*
264008278a5eSRod Evans 		 * Now that we have a symbol descriptor, retain the descriptor
264108278a5eSRod Evans 		 * for later use by symbol capabilities processing.
264208278a5eSRod Evans 		 */
264308278a5eSRod Evans 		if (cpp)
264408278a5eSRod Evans 			cpp->c_nsdp = sdp;
264508278a5eSRod Evans 
264608278a5eSRod Evans 		/*
26477c478bd9Sstevel@tonic-gate 		 * After we've compared a defined symbol in one shared
26487c478bd9Sstevel@tonic-gate 		 * object, flag the symbol so we don't compare it again.
26497c478bd9Sstevel@tonic-gate 		 */
2650635216b6SRod Evans 		if ((etype == ET_DYN) && (nsym->st_shndx != SHN_UNDEF) &&
26517c478bd9Sstevel@tonic-gate 		    ((sdp->sd_flags & FLG_SY_SOFOUND) == 0))
26527c478bd9Sstevel@tonic-gate 			sdp->sd_flags |= FLG_SY_SOFOUND;
26537c478bd9Sstevel@tonic-gate 
26547c478bd9Sstevel@tonic-gate 		/*
26557c478bd9Sstevel@tonic-gate 		 * If the symbol is accepted from this file retain the symbol
26567c478bd9Sstevel@tonic-gate 		 * index for possible use in aliasing.
26577c478bd9Sstevel@tonic-gate 		 */
26587c478bd9Sstevel@tonic-gate 		if (sdp->sd_file == ifl)
26597c478bd9Sstevel@tonic-gate 			sdp->sd_symndx = ndx;
26607c478bd9Sstevel@tonic-gate 
26617c478bd9Sstevel@tonic-gate 		ifl->ifl_oldndx[ndx] = sdp;
26627c478bd9Sstevel@tonic-gate 
26637c478bd9Sstevel@tonic-gate 		/*
26647c478bd9Sstevel@tonic-gate 		 * If we've accepted a register symbol, continue to validate
26657c478bd9Sstevel@tonic-gate 		 * it.
26667c478bd9Sstevel@tonic-gate 		 */
26677c478bd9Sstevel@tonic-gate 		if (sdp->sd_flags & FLG_SY_REGSYM) {
26687c478bd9Sstevel@tonic-gate 			Sym_desc	*rsdp;
26697c478bd9Sstevel@tonic-gate 
2670ba2be530Sab196087 			/*
2671ba2be530Sab196087 			 * The presence of FLG_SY_REGSYM means that
2672ba2be530Sab196087 			 * the pointers in ld_targ.t_ms are non-NULL.
2673ba2be530Sab196087 			 */
2674ba2be530Sab196087 			rsdp = (*ld_targ.t_ms.ms_reg_find)(sdp->sd_sym, ofl);
26756b3ba5bdSAli Bahrami 			if (rsdp == NULL) {
2676ba2be530Sab196087 				if ((*ld_targ.t_ms.ms_reg_enter)(sdp, ofl) == 0)
26777c478bd9Sstevel@tonic-gate 					return (S_ERROR);
26787c478bd9Sstevel@tonic-gate 			} else if (rsdp != sdp) {
2679ba2be530Sab196087 				(void) (*ld_targ.t_ms.ms_reg_check)(rsdp,
2680ba2be530Sab196087 				    sdp->sd_sym, sdp->sd_name, ifl, ofl);
26817c478bd9Sstevel@tonic-gate 			}
26827c478bd9Sstevel@tonic-gate 		}
268375e45495Sab196087 
268475e45495Sab196087 		/*
268575e45495Sab196087 		 * For a relocatable object, if this symbol is defined
268675e45495Sab196087 		 * and has non-zero length and references an address
268775e45495Sab196087 		 * within an associated section, then check its extents
268875e45495Sab196087 		 * to make sure the section boundaries encompass it.
268975e45495Sab196087 		 * If they don't, the ELF file is corrupt. Note that this
269075e45495Sab196087 		 * global symbol may have come from another file to satisfy
269175e45495Sab196087 		 * an UNDEF symbol of the same name from this one. In that
269275e45495Sab196087 		 * case, we don't check it, because it was already checked
269375e45495Sab196087 		 * as part of its own file.
269475e45495Sab196087 		 */
269575e45495Sab196087 		if (etype_rel && (sdp->sd_file == ifl)) {
269675e45495Sab196087 			Sym *tsym = sdp->sd_sym;
269775e45495Sab196087 
269875e45495Sab196087 			if (SYM_LOC_BADADDR(sdp, tsym,
269975e45495Sab196087 			    ELF_ST_TYPE(tsym->st_info))) {
270069a0bf0cSab196087 				issue_badaddr_msg(ifl, ofl, sdp,
270169a0bf0cSab196087 				    tsym, tsym->st_shndx);
270275e45495Sab196087 				continue;
270375e45495Sab196087 			}
270475e45495Sab196087 		}
27057c478bd9Sstevel@tonic-gate 	}
2706dc0f59e5SAli Bahrami 	DBG_CALL(Dbg_util_nl(ofl->ofl_lml, DBG_NL_STD));
27077c478bd9Sstevel@tonic-gate 
27087c478bd9Sstevel@tonic-gate 	/*
27096b3ba5bdSAli Bahrami 	 * Associate weak (alias) symbols to their non-weak counterparts by
271008278a5eSRod Evans 	 * scanning the global symbols one more time.
27117c478bd9Sstevel@tonic-gate 	 *
27126b3ba5bdSAli Bahrami 	 * This association is needed when processing the symbols from a shared
27136b3ba5bdSAli Bahrami 	 * object dependency when a a weak definition satisfies a reference:
27147c478bd9Sstevel@tonic-gate 	 *
27156b3ba5bdSAli Bahrami 	 *  -	When building a dynamic executable, if a referenced symbol is a
27166b3ba5bdSAli Bahrami 	 *	data item, the symbol data is copied to the executables address
27176b3ba5bdSAli Bahrami 	 *	space.  In this copy-relocation case, we must also reassociate
27186b3ba5bdSAli Bahrami 	 *	the alias symbol with its new location in the executable.
27196b3ba5bdSAli Bahrami 	 *
27206b3ba5bdSAli Bahrami 	 *  -	If the referenced symbol is a function then we may need to
27216b3ba5bdSAli Bahrami 	 *	promote the symbols binding from undefined weak to undefined,
27226b3ba5bdSAli Bahrami 	 *	otherwise the run-time linker will not generate the correct
27236b3ba5bdSAli Bahrami 	 *	relocation error should the symbol not be found.
27246b3ba5bdSAli Bahrami 	 *
27256b3ba5bdSAli Bahrami 	 * Weak alias association is also required when a local dynsym table
27266b3ba5bdSAli Bahrami 	 * is being created.  This table should only contain one instance of a
27276b3ba5bdSAli Bahrami 	 * symbol that is associated to a given address.
27287c478bd9Sstevel@tonic-gate 	 *
27297c478bd9Sstevel@tonic-gate 	 * The true association between a weak/strong symbol pair is that both
27306b3ba5bdSAli Bahrami 	 * symbol entries are identical, thus first we create a sorted symbol
27316b3ba5bdSAli Bahrami 	 * list keyed off of the symbols section index and value.  If the symbol
27326b3ba5bdSAli Bahrami 	 * belongs to the same section and has the same value, then the chances
27336b3ba5bdSAli Bahrami 	 * are that the rest of the symbols data is the same.  This list is then
27346b3ba5bdSAli Bahrami 	 * scanned for weak symbols, and if one is found then any strong
27356b3ba5bdSAli Bahrami 	 * association will exist in the entries that follow.  Thus we just have
27366b3ba5bdSAli Bahrami 	 * to scan one (typically a single alias) or more (in the uncommon
27376b3ba5bdSAli Bahrami 	 * instance of multiple weak to strong associations) entries to
27386b3ba5bdSAli Bahrami 	 * determine if a match exists.
27397c478bd9Sstevel@tonic-gate 	 */
27406b3ba5bdSAli Bahrami 	if (weak && (OFL_ALLOW_LDYNSYM(ofl) || (etype == ET_DYN)) &&
2741d579eb63Sab196087 	    (total > local)) {
27426b3ba5bdSAli Bahrami 		static Sym_desc	**sort;
27436b3ba5bdSAli Bahrami 		static size_t	osize = 0;
27446b3ba5bdSAli Bahrami 		size_t		nsize = (total - local) * sizeof (Sym_desc *);
27457c478bd9Sstevel@tonic-gate 
27466b3ba5bdSAli Bahrami 		/*
27476b3ba5bdSAli Bahrami 		 * As we might be processing many input files, and many symbols,
27486b3ba5bdSAli Bahrami 		 * try and reuse a static sort buffer.  Note, presently we're
27496b3ba5bdSAli Bahrami 		 * playing the game of never freeing any buffers as there's a
27506b3ba5bdSAli Bahrami 		 * belief this wastes time.
27516b3ba5bdSAli Bahrami 		 */
27526b3ba5bdSAli Bahrami 		if ((osize == 0) || (nsize > osize)) {
27536b3ba5bdSAli Bahrami 			if ((sort = libld_malloc(nsize)) == NULL)
27547c478bd9Sstevel@tonic-gate 				return (S_ERROR);
27556b3ba5bdSAli Bahrami 			osize = nsize;
27566b3ba5bdSAli Bahrami 		}
27576b3ba5bdSAli Bahrami 		(void) memcpy((void *)sort, &ifl->ifl_oldndx[local], nsize);
27587c478bd9Sstevel@tonic-gate 
27597c478bd9Sstevel@tonic-gate 		qsort(sort, (total - local), sizeof (Sym_desc *), compare);
27607c478bd9Sstevel@tonic-gate 
27617c478bd9Sstevel@tonic-gate 		for (ndx = 0; ndx < (total - local); ndx++) {
27627c478bd9Sstevel@tonic-gate 			Sym_desc	*wsdp = sort[ndx];
27637c478bd9Sstevel@tonic-gate 			Sym		*wsym;
27647c478bd9Sstevel@tonic-gate 			int		sndx;
27657c478bd9Sstevel@tonic-gate 
27666b3ba5bdSAli Bahrami 			/*
27676b3ba5bdSAli Bahrami 			 * Ignore any empty symbol descriptor, or the case where
27686b3ba5bdSAli Bahrami 			 * the symbol has been resolved to a different file.
27696b3ba5bdSAli Bahrami 			 */
27706b3ba5bdSAli Bahrami 			if ((wsdp == NULL) || (wsdp->sd_file != ifl))
27717c478bd9Sstevel@tonic-gate 				continue;
27727c478bd9Sstevel@tonic-gate 
27737c478bd9Sstevel@tonic-gate 			wsym = wsdp->sd_sym;
27747c478bd9Sstevel@tonic-gate 
27756b3ba5bdSAli Bahrami 			if ((wsym->st_shndx == SHN_UNDEF) ||
27766b3ba5bdSAli Bahrami 			    (wsdp->sd_flags & FLG_SY_SPECSEC) ||
27776b3ba5bdSAli Bahrami 			    (ELF_ST_BIND(wsym->st_info) != STB_WEAK))
27787c478bd9Sstevel@tonic-gate 				continue;
27797c478bd9Sstevel@tonic-gate 
27807c478bd9Sstevel@tonic-gate 			/*
27817c478bd9Sstevel@tonic-gate 			 * We have a weak symbol, if it has a strong alias it
27827c478bd9Sstevel@tonic-gate 			 * will have been sorted to one of the following sort
27837c478bd9Sstevel@tonic-gate 			 * table entries.  Note that we could have multiple weak
27847c478bd9Sstevel@tonic-gate 			 * symbols aliased to one strong (if this occurs then
27857c478bd9Sstevel@tonic-gate 			 * the strong symbol only maintains one alias back to
27867c478bd9Sstevel@tonic-gate 			 * the last weak).
27877c478bd9Sstevel@tonic-gate 			 */
27887c478bd9Sstevel@tonic-gate 			for (sndx = ndx + 1; sndx < (total - local); sndx++) {
27897c478bd9Sstevel@tonic-gate 				Sym_desc	*ssdp = sort[sndx];
27907c478bd9Sstevel@tonic-gate 				Sym		*ssym;
2791635216b6SRod Evans 				sd_flag_t	w_dynbits, s_dynbits;
2792d579eb63Sab196087 
2793d579eb63Sab196087 				/*
27946b3ba5bdSAli Bahrami 				 * Ignore any empty symbol descriptor, or the
27956b3ba5bdSAli Bahrami 				 * case where the symbol has been resolved to a
27966b3ba5bdSAli Bahrami 				 * different file.
27976b3ba5bdSAli Bahrami 				 */
27986b3ba5bdSAli Bahrami 				if ((ssdp == NULL) || (ssdp->sd_file != ifl))
27996b3ba5bdSAli Bahrami 					continue;
28006b3ba5bdSAli Bahrami 
28016b3ba5bdSAli Bahrami 				ssym = ssdp->sd_sym;
28026b3ba5bdSAli Bahrami 
28036b3ba5bdSAli Bahrami 				if (ssym->st_shndx == SHN_UNDEF)
28046b3ba5bdSAli Bahrami 					continue;
28056b3ba5bdSAli Bahrami 
28066b3ba5bdSAli Bahrami 				if ((ssym->st_shndx != wsym->st_shndx) ||
28076b3ba5bdSAli Bahrami 				    (ssym->st_value != wsym->st_value))
28086b3ba5bdSAli Bahrami 					break;
28096b3ba5bdSAli Bahrami 
28106b3ba5bdSAli Bahrami 				if ((ssym->st_size != wsym->st_size) ||
28116b3ba5bdSAli Bahrami 				    (ssdp->sd_flags & FLG_SY_SPECSEC) ||
28126b3ba5bdSAli Bahrami 				    (ELF_ST_BIND(ssym->st_info) == STB_WEAK))
28136b3ba5bdSAli Bahrami 					continue;
28146b3ba5bdSAli Bahrami 
28156b3ba5bdSAli Bahrami 				/*
28166b3ba5bdSAli Bahrami 				 * If a sharable object, set link fields so
28176b3ba5bdSAli Bahrami 				 * that they reference each other.`
2818d579eb63Sab196087 				 */
2819d579eb63Sab196087 				if (etype == ET_DYN) {
28207c478bd9Sstevel@tonic-gate 					ssdp->sd_aux->sa_linkndx =
28217c478bd9Sstevel@tonic-gate 					    (Word)wsdp->sd_symndx;
28227c478bd9Sstevel@tonic-gate 					wsdp->sd_aux->sa_linkndx =
28237c478bd9Sstevel@tonic-gate 					    (Word)ssdp->sd_symndx;
2824d579eb63Sab196087 				}
28256b3ba5bdSAli Bahrami 
2826d579eb63Sab196087 				/*
28276b3ba5bdSAli Bahrami 				 * Determine which of these two symbols go into
28286b3ba5bdSAli Bahrami 				 * the sort section.  If a mapfile has made
28296b3ba5bdSAli Bahrami 				 * explicit settings of the FLG_SY_*DYNSORT
28306b3ba5bdSAli Bahrami 				 * flags for both symbols, then we do what they
28316b3ba5bdSAli Bahrami 				 * say.  If one has the DYNSORT flags set, we
28326b3ba5bdSAli Bahrami 				 * set the NODYNSORT bit in the other.  And if
28336b3ba5bdSAli Bahrami 				 * neither has an explicit setting, then we
28346b3ba5bdSAli Bahrami 				 * favor the weak symbol because they usually
2835d579eb63Sab196087 				 * lack the leading underscore.
2836d579eb63Sab196087 				 */
2837d579eb63Sab196087 				w_dynbits = wsdp->sd_flags &
2838d579eb63Sab196087 				    (FLG_SY_DYNSORT | FLG_SY_NODYNSORT);
2839d579eb63Sab196087 				s_dynbits = ssdp->sd_flags &
2840d579eb63Sab196087 				    (FLG_SY_DYNSORT | FLG_SY_NODYNSORT);
2841d579eb63Sab196087 				if (!(w_dynbits && s_dynbits)) {
2842d579eb63Sab196087 					if (s_dynbits) {
28436b3ba5bdSAli Bahrami 						if (s_dynbits == FLG_SY_DYNSORT)
2844d579eb63Sab196087 							wsdp->sd_flags |=
2845d579eb63Sab196087 							    FLG_SY_NODYNSORT;
2846d579eb63Sab196087 					} else if (w_dynbits !=
2847d579eb63Sab196087 					    FLG_SY_NODYNSORT) {
2848d579eb63Sab196087 						ssdp->sd_flags |=
2849d579eb63Sab196087 						    FLG_SY_NODYNSORT;
2850d579eb63Sab196087 					}
2851d579eb63Sab196087 				}
28527c478bd9Sstevel@tonic-gate 				break;
28537c478bd9Sstevel@tonic-gate 			}
28547c478bd9Sstevel@tonic-gate 		}
28557c478bd9Sstevel@tonic-gate 	}
285608278a5eSRod Evans 
285708278a5eSRod Evans 	/*
285808278a5eSRod Evans 	 * Having processed all symbols, under -z symbolcap, reprocess any
285908278a5eSRod Evans 	 * symbols that are being translated from global to locals.  The symbol
286008278a5eSRod Evans 	 * pair that has been collected defines the original symbol (c_osdp),
286108278a5eSRod Evans 	 * which will become a local, and the new symbol (c_nsdp), which will
286208278a5eSRod Evans 	 * become a reference (UNDEF) for the original.
286308278a5eSRod Evans 	 *
286408278a5eSRod Evans 	 * Scan these symbol pairs looking for weak symbols, which have non-weak
286508278a5eSRod Evans 	 * aliases.  There is no need to translate both of these symbols to
286608278a5eSRod Evans 	 * locals, only the global is necessary.
286708278a5eSRod Evans 	 */
286808278a5eSRod Evans 	if (cappairs) {
286908278a5eSRod Evans 		Aliste		idx1;
287008278a5eSRod Evans 		Cap_pair	*cpp1;
287108278a5eSRod Evans 
287208278a5eSRod Evans 		for (ALIST_TRAVERSE(cappairs, idx1, cpp1)) {
287308278a5eSRod Evans 			Sym_desc	*sdp1 = cpp1->c_osdp;
287408278a5eSRod Evans 			Sym		*sym1 = sdp1->sd_sym;
287508278a5eSRod Evans 			uchar_t		bind1 = ELF_ST_BIND(sym1->st_info);
287608278a5eSRod Evans 			Aliste		idx2;
287708278a5eSRod Evans 			Cap_pair	*cpp2;
287808278a5eSRod Evans 
287908278a5eSRod Evans 			/*
288008278a5eSRod Evans 			 * If this symbol isn't weak, it's capability member is
288108278a5eSRod Evans 			 * retained for the creation of a local symbol.
288208278a5eSRod Evans 			 */
288308278a5eSRod Evans 			if (bind1 != STB_WEAK)
288408278a5eSRod Evans 				continue;
288508278a5eSRod Evans 
288608278a5eSRod Evans 			/*
288708278a5eSRod Evans 			 * If this is a weak symbol, traverse the capabilities
288808278a5eSRod Evans 			 * list again to determine if a corresponding non-weak
288908278a5eSRod Evans 			 * symbol exists.
289008278a5eSRod Evans 			 */
289108278a5eSRod Evans 			for (ALIST_TRAVERSE(cappairs, idx2, cpp2)) {
289208278a5eSRod Evans 				Sym_desc	*sdp2 = cpp2->c_osdp;
289308278a5eSRod Evans 				Sym		*sym2 = sdp2->sd_sym;
289408278a5eSRod Evans 				uchar_t		bind2 =
289508278a5eSRod Evans 				    ELF_ST_BIND(sym2->st_info);
289608278a5eSRod Evans 
289708278a5eSRod Evans 				if ((cpp1 == cpp2) ||
289808278a5eSRod Evans 				    (cpp1->c_group != cpp2->c_group) ||
289908278a5eSRod Evans 				    (sym1->st_value != sym2->st_value) ||
290008278a5eSRod Evans 				    (bind2 == STB_WEAK))
290108278a5eSRod Evans 					continue;
290208278a5eSRod Evans 
290308278a5eSRod Evans 				/*
290408278a5eSRod Evans 				 * The weak symbol (sym1) has a non-weak (sym2)
290508278a5eSRod Evans 				 * counterpart.  There's no point in translating
290608278a5eSRod Evans 				 * both of these equivalent symbols to locals.
290708278a5eSRod Evans 				 * Add this symbol capability alias to the
290808278a5eSRod Evans 				 * capabilities family information, and remove
290908278a5eSRod Evans 				 * the weak symbol.
291008278a5eSRod Evans 				 */
291108278a5eSRod Evans 				if (ld_cap_add_family(ofl, cpp2->c_nsdp,
291208278a5eSRod Evans 				    cpp1->c_nsdp, NULL, NULL) == S_ERROR)
291308278a5eSRod Evans 					return (S_ERROR);
291408278a5eSRod Evans 
291508278a5eSRod Evans 				free((void *)cpp1->c_osdp);
291608278a5eSRod Evans 				(void) alist_delete(cappairs, &idx1);
291708278a5eSRod Evans 			}
291808278a5eSRod Evans 		}
291908278a5eSRod Evans 
292008278a5eSRod Evans 		DBG_CALL(Dbg_util_nl(ofl->ofl_lml, DBG_NL_STD));
292108278a5eSRod Evans 
292208278a5eSRod Evans 		/*
292308278a5eSRod Evans 		 * The capability pairs information now represents all the
292408278a5eSRod Evans 		 * global symbols that need transforming to locals.  These
292508278a5eSRod Evans 		 * local symbols are renamed using their group identifiers.
292608278a5eSRod Evans 		 */
292708278a5eSRod Evans 		for (ALIST_TRAVERSE(cappairs, idx1, cpp1)) {
292808278a5eSRod Evans 			Sym_desc	*osdp = cpp1->c_osdp;
292908278a5eSRod Evans 			Objcapset	*capset;
293008278a5eSRod Evans 			size_t		nsize, tsize;
293108278a5eSRod Evans 			const char	*oname;
293208278a5eSRod Evans 			char		*cname, *idstr;
293308278a5eSRod Evans 			Sym		*csym;
293408278a5eSRod Evans 
293508278a5eSRod Evans 			/*
293608278a5eSRod Evans 			 * If the local symbol has not yet been translated
293708278a5eSRod Evans 			 * convert it to a local symbol with a name.
293808278a5eSRod Evans 			 */
293908278a5eSRod Evans 			if ((osdp->sd_flags & FLG_SY_CAP) != 0)
294008278a5eSRod Evans 				continue;
294108278a5eSRod Evans 
294208278a5eSRod Evans 			/*
294308278a5eSRod Evans 			 * As we're converting object capabilities to symbol
294408278a5eSRod Evans 			 * capabilities, obtain the capabilities set for this
294508278a5eSRod Evans 			 * object, so as to retrieve the CA_SUNW_ID value.
294608278a5eSRod Evans 			 */
294708278a5eSRod Evans 			capset = &cpp1->c_group->cg_set;
294808278a5eSRod Evans 
294908278a5eSRod Evans 			/*
295008278a5eSRod Evans 			 * Create a new name from the existing symbol and the
295108278a5eSRod Evans 			 * capabilities group identifier.  Note, the delimiter
295208278a5eSRod Evans 			 * between the symbol name and identifier name is hard-
295308278a5eSRod Evans 			 * coded here (%), so that we establish a convention
295408278a5eSRod Evans 			 * for transformed symbol names.
295508278a5eSRod Evans 			 */
295608278a5eSRod Evans 			oname = osdp->sd_name;
295708278a5eSRod Evans 
295808278a5eSRod Evans 			idstr = capset->oc_id.cs_str;
295908278a5eSRod Evans 			nsize = strlen(oname);
296008278a5eSRod Evans 			tsize = nsize + 1 + strlen(idstr) + 1;
296108278a5eSRod Evans 			if ((cname = libld_malloc(tsize)) == 0)
296208278a5eSRod Evans 				return (S_ERROR);
296308278a5eSRod Evans 
296408278a5eSRod Evans 			(void) strcpy(cname, oname);
296508278a5eSRod Evans 			cname[nsize++] = '%';
296608278a5eSRod Evans 			(void) strcpy(&cname[nsize], idstr);
296708278a5eSRod Evans 
296808278a5eSRod Evans 			/*
296908278a5eSRod Evans 			 * Allocate a new symbol table entry, transform this
297008278a5eSRod Evans 			 * symbol to a local, and assign the new name.
297108278a5eSRod Evans 			 */
297208278a5eSRod Evans 			if ((csym = libld_malloc(sizeof (Sym))) == NULL)
297308278a5eSRod Evans 				return (S_ERROR);
297408278a5eSRod Evans 
297508278a5eSRod Evans 			*csym = *osdp->sd_sym;
297608278a5eSRod Evans 			csym->st_info = ELF_ST_INFO(STB_LOCAL,
297708278a5eSRod Evans 			    ELF_ST_TYPE(osdp->sd_sym->st_info));
297808278a5eSRod Evans 
297908278a5eSRod Evans 			osdp->sd_name = cname;
298008278a5eSRod Evans 			osdp->sd_sym = csym;
298108278a5eSRod Evans 			osdp->sd_flags = FLG_SY_CAP;
298208278a5eSRod Evans 
298308278a5eSRod Evans 			/*
298408278a5eSRod Evans 			 * Keep track of this new local symbol.  As -z symbolcap
298508278a5eSRod Evans 			 * can only be used to create a relocatable object, a
298608278a5eSRod Evans 			 * dynamic symbol table can't exist.  Ensure there is
298708278a5eSRod Evans 			 * space reserved in the string table.
298808278a5eSRod Evans 			 */
298908278a5eSRod Evans 			ofl->ofl_caploclcnt++;
299008278a5eSRod Evans 			if (st_insert(ofl->ofl_strtab, cname) == -1)
299108278a5eSRod Evans 				return (S_ERROR);
299208278a5eSRod Evans 
299308278a5eSRod Evans 			DBG_CALL(Dbg_syms_cap_local(ofl, cpp1->c_ndx,
299408278a5eSRod Evans 			    cname, csym, osdp));
299508278a5eSRod Evans 
299608278a5eSRod Evans 			/*
299708278a5eSRod Evans 			 * Establish this capability pair as a family.
299808278a5eSRod Evans 			 */
299908278a5eSRod Evans 			if (ld_cap_add_family(ofl, cpp1->c_nsdp, osdp,
300008278a5eSRod Evans 			    cpp1->c_group, &ifl->ifl_caps->ca_syms) == S_ERROR)
300108278a5eSRod Evans 				return (S_ERROR);
300208278a5eSRod Evans 		}
300308278a5eSRod Evans 	}
300408278a5eSRod Evans 
30057c478bd9Sstevel@tonic-gate 	return (1);
300675e45495Sab196087 
300775e45495Sab196087 #undef SYM_LOC_BADADDR
30087c478bd9Sstevel@tonic-gate }
30097c478bd9Sstevel@tonic-gate 
30107c478bd9Sstevel@tonic-gate /*
3011f5a18a30Srie  * Add an undefined symbol to the symbol table.  The reference originates from
301208278a5eSRod Evans  * the location identified by the message id (mid).  These references can
3013f5a18a30Srie  * originate from command line options such as -e, -u, -initarray, etc.
3014f5a18a30Srie  * (identified with MSG_INTL(MSG_STR_COMMAND)), or from internally generated
3015f5a18a30Srie  * TLS relocation references (identified with MSG_INTL(MSG_STR_TLSREL)).
30167c478bd9Sstevel@tonic-gate  */
30177c478bd9Sstevel@tonic-gate Sym_desc *
ld_sym_add_u(const char * name,Ofl_desc * ofl,Msg mid)3018f5a18a30Srie ld_sym_add_u(const char *name, Ofl_desc *ofl, Msg mid)
30197c478bd9Sstevel@tonic-gate {
30207c478bd9Sstevel@tonic-gate 	Sym		*sym;
30216b3ba5bdSAli Bahrami 	Ifl_desc	*ifl = NULL, *_ifl;
30227c478bd9Sstevel@tonic-gate 	Sym_desc	*sdp;
30237c478bd9Sstevel@tonic-gate 	Word		hash;
302457ef7aa9SRod Evans 	Aliste		idx;
30257c478bd9Sstevel@tonic-gate 	avl_index_t	where;
3026f5a18a30Srie 	const char	*reference = MSG_INTL(mid);
30277c478bd9Sstevel@tonic-gate 
30287c478bd9Sstevel@tonic-gate 	/*
3029b02637afSrie 	 * As an optimization, determine whether we've already generated this
3030b02637afSrie 	 * reference.  If the symbol doesn't already exist we'll create it.
3031b02637afSrie 	 * Or if the symbol does exist from a different source, we'll resolve
3032b02637afSrie 	 * the conflict.
30337c478bd9Sstevel@tonic-gate 	 */
30347c478bd9Sstevel@tonic-gate 	/* LINTED */
30357c478bd9Sstevel@tonic-gate 	hash = (Word)elf_hash(name);
3036b02637afSrie 	if ((sdp = ld_sym_find(name, hash, &where, ofl)) != NULL) {
3037b02637afSrie 		if ((sdp->sd_sym->st_shndx == SHN_UNDEF) &&
3038b02637afSrie 		    (sdp->sd_file->ifl_name == reference))
30397c478bd9Sstevel@tonic-gate 			return (sdp);
30407c478bd9Sstevel@tonic-gate 	}
30417c478bd9Sstevel@tonic-gate 
30427c478bd9Sstevel@tonic-gate 	/*
30437c478bd9Sstevel@tonic-gate 	 * Determine whether a pseudo input file descriptor exists to represent
30447c478bd9Sstevel@tonic-gate 	 * the command line, as any global symbol needs an input file descriptor
30457c478bd9Sstevel@tonic-gate 	 * during any symbol resolution (refer to map_ifl() which provides a
30467c478bd9Sstevel@tonic-gate 	 * similar method for adding symbols from mapfiles).
30477c478bd9Sstevel@tonic-gate 	 */
304857ef7aa9SRod Evans 	for (APLIST_TRAVERSE(ofl->ofl_objs, idx, _ifl))
3049f5a18a30Srie 		if (strcmp(_ifl->ifl_name, reference) == 0) {
30507c478bd9Sstevel@tonic-gate 			ifl = _ifl;
30517c478bd9Sstevel@tonic-gate 			break;
30527c478bd9Sstevel@tonic-gate 		}
30537c478bd9Sstevel@tonic-gate 
30547c478bd9Sstevel@tonic-gate 	/*
30557c478bd9Sstevel@tonic-gate 	 * If no descriptor exists create one.
30567c478bd9Sstevel@tonic-gate 	 */
30576b3ba5bdSAli Bahrami 	if (ifl == NULL) {
305857ef7aa9SRod Evans 		if ((ifl = libld_calloc(sizeof (Ifl_desc), 1)) == NULL)
30597c478bd9Sstevel@tonic-gate 			return ((Sym_desc *)S_ERROR);
3060f5a18a30Srie 		ifl->ifl_name = reference;
30617c478bd9Sstevel@tonic-gate 		ifl->ifl_flags = FLG_IF_NEEDED | FLG_IF_FILEREF;
306257ef7aa9SRod Evans 		if ((ifl->ifl_ehdr = libld_calloc(sizeof (Ehdr), 1)) == NULL)
30637c478bd9Sstevel@tonic-gate 			return ((Sym_desc *)S_ERROR);
30647c478bd9Sstevel@tonic-gate 		ifl->ifl_ehdr->e_type = ET_REL;
30657c478bd9Sstevel@tonic-gate 
306657ef7aa9SRod Evans 		if (aplist_append(&ofl->ofl_objs, ifl, AL_CNT_OFL_OBJS) == NULL)
30677c478bd9Sstevel@tonic-gate 			return ((Sym_desc *)S_ERROR);
30687c478bd9Sstevel@tonic-gate 	}
30697c478bd9Sstevel@tonic-gate 
30707c478bd9Sstevel@tonic-gate 	/*
30717c478bd9Sstevel@tonic-gate 	 * Allocate a symbol structure and add it to the global symbol table.
30727c478bd9Sstevel@tonic-gate 	 */
30736b3ba5bdSAli Bahrami 	if ((sym = libld_calloc(sizeof (Sym), 1)) == NULL)
30747c478bd9Sstevel@tonic-gate 		return ((Sym_desc *)S_ERROR);
30757c478bd9Sstevel@tonic-gate 	sym->st_info = ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE);
30767c478bd9Sstevel@tonic-gate 	sym->st_shndx = SHN_UNDEF;
30777c478bd9Sstevel@tonic-gate 
30785aefb655Srie 	DBG_CALL(Dbg_syms_process(ofl->ofl_lml, ifl));
3079b02637afSrie 	if (sdp == NULL) {
30805aefb655Srie 		DBG_CALL(Dbg_syms_global(ofl->ofl_lml, 0, name));
3081b02637afSrie 		if ((sdp = ld_sym_enter(name, sym, hash, ifl, ofl, 0, SHN_UNDEF,
3082635216b6SRod Evans 		    0, &where)) == (Sym_desc *)S_ERROR)
3083b02637afSrie 			return ((Sym_desc *)S_ERROR);
3084b02637afSrie 	} else if (ld_sym_resolve(sdp, sym, ifl, ofl, 0,
3085b02637afSrie 	    SHN_UNDEF, 0) == S_ERROR)
3086b02637afSrie 		return ((Sym_desc *)S_ERROR);
3087b02637afSrie 
30887c478bd9Sstevel@tonic-gate 	sdp->sd_flags &= ~FLG_SY_CLEAN;
30897c478bd9Sstevel@tonic-gate 	sdp->sd_flags |= FLG_SY_CMDREF;
30907c478bd9Sstevel@tonic-gate 
30917c478bd9Sstevel@tonic-gate 	return (sdp);
30927c478bd9Sstevel@tonic-gate }
30934a8d0ea7SAli Bahrami 
30944a8d0ea7SAli Bahrami /*
30954a8d0ea7SAli Bahrami  * STT_SECTION symbols have their st_name field set to NULL, and consequently
3096bf994817SAli Bahrami  * have no name. Generate a name suitable for diagnostic use for such a symbol
3097bf994817SAli Bahrami  * and store it in the input section descriptor. The resulting name will be
3098bf994817SAli Bahrami  * of the form:
30994a8d0ea7SAli Bahrami  *
31004a8d0ea7SAli Bahrami  *	"XXX (section)"
31014a8d0ea7SAli Bahrami  *
31024a8d0ea7SAli Bahrami  * where XXX is the name of the section.
31034a8d0ea7SAli Bahrami  *
31044a8d0ea7SAli Bahrami  * entry:
310508278a5eSRod Evans  *	isc - Input section associated with the symbol.
31064a8d0ea7SAli Bahrami  *	fmt - NULL, or format string to use.
31074a8d0ea7SAli Bahrami  *
31084a8d0ea7SAli Bahrami  * exit:
3109bf994817SAli Bahrami  *	Sets isp->is_sym_name to the allocated string. Returns the
3110bf994817SAli Bahrami  *	string pointer, or NULL on allocation failure.
31114a8d0ea7SAli Bahrami  */
31124a8d0ea7SAli Bahrami const const char *
ld_stt_section_sym_name(Is_desc * isp)31134a8d0ea7SAli Bahrami ld_stt_section_sym_name(Is_desc *isp)
31144a8d0ea7SAli Bahrami {
31154a8d0ea7SAli Bahrami 	const char	*fmt;
3116bf994817SAli Bahrami 	char		*str;
31174a8d0ea7SAli Bahrami 	size_t		len;
31184a8d0ea7SAli Bahrami 
3119bf994817SAli Bahrami 	if ((isp == NULL) || (isp->is_name == NULL))
3120bf994817SAli Bahrami 		return (NULL);
3121bf994817SAli Bahrami 
3122bf994817SAli Bahrami 	if (isp->is_sym_name == NULL) {
31234a8d0ea7SAli Bahrami 		fmt = (isp->is_flags & FLG_IS_GNSTRMRG) ?
31244a8d0ea7SAli Bahrami 		    MSG_INTL(MSG_STR_SECTION_MSTR) : MSG_INTL(MSG_STR_SECTION);
31254a8d0ea7SAli Bahrami 
31264a8d0ea7SAli Bahrami 		len = strlen(fmt) + strlen(isp->is_name) + 1;
31274a8d0ea7SAli Bahrami 
3128bf994817SAli Bahrami 		if ((str = libld_malloc(len)) == NULL)
31294a8d0ea7SAli Bahrami 			return (NULL);
3130bf994817SAli Bahrami 		(void) snprintf(str, len, fmt, isp->is_name);
3131bf994817SAli Bahrami 		isp->is_sym_name = str;
31324a8d0ea7SAli Bahrami 	}
31334a8d0ea7SAli Bahrami 
3134bf994817SAli Bahrami 	return (isp->is_sym_name);
31354a8d0ea7SAli Bahrami }
3136