xref: /illumos-gate/usr/src/cmd/sgs/libld/common/syms.c (revision 08278a5e)
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  *
2769112eddSAli Bahrami  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
287c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
297c478bd9Sstevel@tonic-gate  */
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate /*
327c478bd9Sstevel@tonic-gate  * Symbol table management routines
337c478bd9Sstevel@tonic-gate  */
34ba2be530Sab 
35ba2be530Sab #define	ELF_TARGET_AMD64
36ba2be530Sab 
377c478bd9Sstevel@tonic-gate #include	<stdio.h>
387c478bd9Sstevel@tonic-gate #include	<string.h>
395aefb655Srie #include	<debug.h>
407c478bd9Sstevel@tonic-gate #include	"msg.h"
417c478bd9Sstevel@tonic-gate #include	"_libld.h"
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate /*
447c478bd9Sstevel@tonic-gate  * AVL tree comparator function:
457c478bd9Sstevel@tonic-gate  *
466b3ba5bdSAli Bahrami  * The primary key is the symbol name hash with a secondary key of the symbol
476b3ba5bdSAli Bahrami  * name itself.
487c478bd9Sstevel@tonic-gate  */
497c478bd9Sstevel@tonic-gate int
505aefb655Srie ld_sym_avl_comp(const void *elem1, const void *elem2)
517c478bd9Sstevel@tonic-gate {
527c478bd9Sstevel@tonic-gate 	Sym_avlnode	*sav1 = (Sym_avlnode *)elem1;
537c478bd9Sstevel@tonic-gate 	Sym_avlnode	*sav2 = (Sym_avlnode *)elem2;
546b3ba5bdSAli Bahrami 	int		res;
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate 	res = sav1->sav_hash - sav2->sav_hash;
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate 	if (res < 0)
597c478bd9Sstevel@tonic-gate 		return (-1);
607c478bd9Sstevel@tonic-gate 	if (res > 0)
617c478bd9Sstevel@tonic-gate 		return (1);
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate 	/*
647c478bd9Sstevel@tonic-gate 	 * Hash is equal - now compare name
657c478bd9Sstevel@tonic-gate 	 */
667c478bd9Sstevel@tonic-gate 	res = strcmp(sav1->sav_name, sav2->sav_name);
677c478bd9Sstevel@tonic-gate 	if (res == 0)
687c478bd9Sstevel@tonic-gate 		return (0);
697c478bd9Sstevel@tonic-gate 	if (res > 0)
707c478bd9Sstevel@tonic-gate 		return (1);
717c478bd9Sstevel@tonic-gate 	return (-1);
727c478bd9Sstevel@tonic-gate }
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate /*
757c478bd9Sstevel@tonic-gate  * Focal point for verifying symbol names.
767c478bd9Sstevel@tonic-gate  */
77a194faf8Srie inline static const char *
785aefb655Srie string(Ofl_desc *ofl, Ifl_desc *ifl, Sym *sym, const char *strs, size_t strsize,
794a8d0ea7SAli Bahrami     int symndx, Word shndx, Word symsecndx, const char *symsecname,
80635216b6SRod Evans     const char *strsecname, sd_flag_t *flags)
817c478bd9Sstevel@tonic-gate {
826b3ba5bdSAli Bahrami 	Word	name = sym->st_name;
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate 	if (name) {
857c478bd9Sstevel@tonic-gate 		if ((ifl->ifl_flags & FLG_IF_HSTRTAB) == 0) {
865aefb655Srie 			eprintf(ofl->ofl_lml, ERR_FATAL,
875aefb655Srie 			    MSG_INTL(MSG_FIL_NOSTRTABLE), ifl->ifl_name,
884a8d0ea7SAli Bahrami 			    EC_WORD(symsecndx), symsecname, symndx,
894a8d0ea7SAli Bahrami 			    EC_XWORD(name));
906b3ba5bdSAli Bahrami 			return (NULL);
917c478bd9Sstevel@tonic-gate 		}
927c478bd9Sstevel@tonic-gate 		if (name >= (Word)strsize) {
935aefb655Srie 			eprintf(ofl->ofl_lml, ERR_FATAL,
945aefb655Srie 			    MSG_INTL(MSG_FIL_EXCSTRTABLE), ifl->ifl_name,
954a8d0ea7SAli Bahrami 			    EC_WORD(symsecndx), symsecname, symndx,
964a8d0ea7SAli Bahrami 			    EC_XWORD(name), strsecname, EC_XWORD(strsize));
976b3ba5bdSAli Bahrami 			return (NULL);
987c478bd9Sstevel@tonic-gate 		}
997c478bd9Sstevel@tonic-gate 	}
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate 	/*
1027c478bd9Sstevel@tonic-gate 	 * Determine if we're dealing with a register and if so validate it.
1037c478bd9Sstevel@tonic-gate 	 * If it's a scratch register, a fabricated name will be returned.
1047c478bd9Sstevel@tonic-gate 	 */
105ba2be530Sab 	if (ld_targ.t_ms.ms_is_regsym != NULL) {
106ba2be530Sab 		const char *regname = (*ld_targ.t_ms.ms_is_regsym)(ofl, ifl,
107ba2be530Sab 		    sym, strs, symndx, shndx, symsecname, flags);
108ba2be530Sab 
109ba2be530Sab 		if (regname == (const char *)S_ERROR) {
1106b3ba5bdSAli Bahrami 			return (NULL);
111ba2be530Sab 		}
112ba2be530Sab 		if (regname)
113ba2be530Sab 			return (regname);
1147c478bd9Sstevel@tonic-gate 	}
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate 	/*
1177c478bd9Sstevel@tonic-gate 	 * If this isn't a register, but we have a global symbol with a null
1187c478bd9Sstevel@tonic-gate 	 * name, we're not going to be able to hash this, search for it, or
1197c478bd9Sstevel@tonic-gate 	 * do anything interesting.  However, we've been accepting a symbol of
1207c478bd9Sstevel@tonic-gate 	 * this kind for ages now, so give the user a warning (rather than a
1217c478bd9Sstevel@tonic-gate 	 * fatal error), just in case this instance exists somewhere in the
1227c478bd9Sstevel@tonic-gate 	 * world and hasn't, as yet, been a problem.
1237c478bd9Sstevel@tonic-gate 	 */
1247c478bd9Sstevel@tonic-gate 	if ((name == 0) && (ELF_ST_BIND(sym->st_info) != STB_LOCAL)) {
1255aefb655Srie 		eprintf(ofl->ofl_lml, ERR_WARNING, MSG_INTL(MSG_FIL_NONAMESYM),
1264a8d0ea7SAli Bahrami 		    ifl->ifl_name, EC_WORD(symsecndx), symsecname, symndx,
1274a8d0ea7SAli Bahrami 		    EC_XWORD(name));
1287c478bd9Sstevel@tonic-gate 	}
1297c478bd9Sstevel@tonic-gate 	return (strs + name);
1307c478bd9Sstevel@tonic-gate }
1317c478bd9Sstevel@tonic-gate 
132ba2be530Sab /*
133ba2be530Sab  * For producing symbol names strings to use in error messages.
134ba2be530Sab  * If the symbol has a non-null name, then the string returned by
135ba2be530Sab  * this function is the output from demangle(), surrounded by
136ba2be530Sab  * single quotes. For null names, a descriptive string giving
137ba2be530Sab  * the symbol section and index is generated.
138ba2be530Sab  *
139ba2be530Sab  * This function uses an internal static buffer to hold the resulting
140ba2be530Sab  * string. The value returned is usable by the caller until the next
141ba2be530Sab  * call, at which point it is overwritten.
142ba2be530Sab  */
143ba2be530Sab static const char *
144ba2be530Sab demangle_symname(const char *name, const char *symtab_name, Word symndx)
145ba2be530Sab {
146ba2be530Sab #define	INIT_BUFSIZE 256
147ba2be530Sab 
1486b3ba5bdSAli Bahrami 	static char	*buf;
1496b3ba5bdSAli Bahrami 	static size_t	bufsize = 0;
150ba2be530Sab 	size_t		len;
151ba2be530Sab 	int		use_name;
152ba2be530Sab 
153ba2be530Sab 	use_name = (name != NULL) && (*name != '\0');
154ba2be530Sab 
155ba2be530Sab 	if (use_name) {
156ba2be530Sab 		name = demangle(name);
157ba2be530Sab 		len = strlen(name) + 2;   /* Include room for quotes */
158ba2be530Sab 	} else {
159ba2be530Sab 		name = MSG_ORIG(MSG_STR_EMPTY);
1604f680cc6SAli Bahrami 		len = strlen(symtab_name) + 2 + CONV_INV_BUFSIZE;
161ba2be530Sab 	}
162ba2be530Sab 	len++;			/* Null termination */
163ba2be530Sab 
164ba2be530Sab 	/* If our buffer is too small, double it until it is big enough */
165ba2be530Sab 	if (len > bufsize) {
166ba2be530Sab 		size_t	new_bufsize = bufsize;
167ba2be530Sab 		char	*new_buf;
168ba2be530Sab 
169ba2be530Sab 		if (new_bufsize == 0)
170ba2be530Sab 			new_bufsize = INIT_BUFSIZE;
171ba2be530Sab 		while (len > new_bufsize)
172ba2be530Sab 			new_bufsize *= 2;
1736b3ba5bdSAli Bahrami 		if ((new_buf = libld_malloc(new_bufsize)) == NULL)
174ba2be530Sab 			return (name);
175ba2be530Sab 		buf = new_buf;
176ba2be530Sab 		bufsize = new_bufsize;
177ba2be530Sab 	}
178ba2be530Sab 
179ba2be530Sab 	if (use_name) {
180ba2be530Sab 		(void) snprintf(buf, bufsize, MSG_ORIG(MSG_FMT_SYMNAM), name);
181ba2be530Sab 	} else {
182ba2be530Sab 		(void) snprintf(buf, bufsize, MSG_ORIG(MSG_FMT_NULLSYMNAM),
183ba2be530Sab 		    symtab_name, EC_WORD(symndx));
184ba2be530Sab 	}
185ba2be530Sab 
186ba2be530Sab 	return (buf);
187ba2be530Sab 
188ba2be530Sab #undef INIT_BUFSIZE
189ba2be530Sab }
190ba2be530Sab 
1917c478bd9Sstevel@tonic-gate /*
1927c478bd9Sstevel@tonic-gate  * Shared objects can be built that define specific symbols that can not be
1937c478bd9Sstevel@tonic-gate  * directly bound to.  These objects have a syminfo section (and an associated
1947c478bd9Sstevel@tonic-gate  * DF_1_NODIRECT dynamic flags entry).  Scan this table looking for symbols
1957c478bd9Sstevel@tonic-gate  * that can't be bound to directly, and if this files symbol is presently
1967c478bd9Sstevel@tonic-gate  * referenced, mark it so that we don't directly bind to it.
1977c478bd9Sstevel@tonic-gate  */
1987c478bd9Sstevel@tonic-gate uintptr_t
1999a411307Srie ld_sym_nodirect(Is_desc *isp, Ifl_desc *ifl, Ofl_desc *ofl)
2007c478bd9Sstevel@tonic-gate {
2017c478bd9Sstevel@tonic-gate 	Shdr		*sifshdr, *symshdr;
2027c478bd9Sstevel@tonic-gate 	Syminfo		*sifdata;
2037c478bd9Sstevel@tonic-gate 	Sym		*symdata;
2047c478bd9Sstevel@tonic-gate 	char		*strdata;
2057c478bd9Sstevel@tonic-gate 	ulong_t		cnt, _cnt;
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 	/*
2087c478bd9Sstevel@tonic-gate 	 * Get the syminfo data, and determine the number of entries.
2097c478bd9Sstevel@tonic-gate 	 */
2107c478bd9Sstevel@tonic-gate 	sifshdr = isp->is_shdr;
2117c478bd9Sstevel@tonic-gate 	sifdata = (Syminfo *)isp->is_indata->d_buf;
2127c478bd9Sstevel@tonic-gate 	cnt =  sifshdr->sh_size / sifshdr->sh_entsize;
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 	/*
2157c478bd9Sstevel@tonic-gate 	 * Get the associated symbol table.
2167c478bd9Sstevel@tonic-gate 	 */
21728bda19cSRod Evans 	if ((sifshdr->sh_link == 0) || (sifshdr->sh_link >= ifl->ifl_shnum)) {
21828bda19cSRod Evans 		/*
21928bda19cSRod Evans 		 * Broken input file
22028bda19cSRod Evans 		 */
22128bda19cSRod Evans 		eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_FIL_INVSHINFO),
22228bda19cSRod Evans 		    ifl->ifl_name, isp->is_name, EC_XWORD(sifshdr->sh_link));
22328bda19cSRod Evans 		ofl->ofl_flags |= FLG_OF_FATAL;
22428bda19cSRod Evans 		return (0);
22528bda19cSRod Evans 	}
2267c478bd9Sstevel@tonic-gate 	symshdr = ifl->ifl_isdesc[sifshdr->sh_link]->is_shdr;
2277c478bd9Sstevel@tonic-gate 	symdata = ifl->ifl_isdesc[sifshdr->sh_link]->is_indata->d_buf;
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate 	/*
2307c478bd9Sstevel@tonic-gate 	 * Get the string table associated with the symbol table.
2317c478bd9Sstevel@tonic-gate 	 */
2327c478bd9Sstevel@tonic-gate 	strdata = ifl->ifl_isdesc[symshdr->sh_link]->is_indata->d_buf;
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate 	/*
2357c478bd9Sstevel@tonic-gate 	 * Traverse the syminfo data for symbols that can't be directly
2367c478bd9Sstevel@tonic-gate 	 * bound to.
2377c478bd9Sstevel@tonic-gate 	 */
2387c478bd9Sstevel@tonic-gate 	for (_cnt = 1, sifdata++; _cnt < cnt; _cnt++, sifdata++) {
2397c478bd9Sstevel@tonic-gate 		Sym		*sym;
2407c478bd9Sstevel@tonic-gate 		char		*str;
2417c478bd9Sstevel@tonic-gate 		Sym_desc	*sdp;
2427c478bd9Sstevel@tonic-gate 
2433c4993fbSrie 		if ((sifdata->si_flags & SYMINFO_FLG_NOEXTDIRECT) == 0)
2447c478bd9Sstevel@tonic-gate 			continue;
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate 		sym = (Sym *)(symdata + _cnt);
2477c478bd9Sstevel@tonic-gate 		str = (char *)(strdata + sym->st_name);
2487c478bd9Sstevel@tonic-gate 
24928bda19cSRod Evans 		if ((sdp = ld_sym_find(str, SYM_NOHASH, NULL, ofl)) != NULL) {
2507c478bd9Sstevel@tonic-gate 			if (ifl != sdp->sd_file)
2517c478bd9Sstevel@tonic-gate 				continue;
2527c478bd9Sstevel@tonic-gate 
253635216b6SRod Evans 			sdp->sd_flags &= ~FLG_SY_DIR;
254635216b6SRod Evans 			sdp->sd_flags |= FLG_SY_NDIR;
2557c478bd9Sstevel@tonic-gate 		}
2567c478bd9Sstevel@tonic-gate 	}
2577c478bd9Sstevel@tonic-gate 	return (0);
2587c478bd9Sstevel@tonic-gate }
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate /*
2617c478bd9Sstevel@tonic-gate  * If, during symbol processing, it is necessary to update a local symbols
2627c478bd9Sstevel@tonic-gate  * contents before we have generated the symbol tables in the output image,
2637c478bd9Sstevel@tonic-gate  * create a new symbol structure and copy the original symbol contents.  While
2647c478bd9Sstevel@tonic-gate  * we are processing the input files, their local symbols are part of the
2657c478bd9Sstevel@tonic-gate  * read-only mapped image.  Commonly, these symbols are copied to the new output
2667c478bd9Sstevel@tonic-gate  * file image and then updated to reflect their new address and any change in
2677c478bd9Sstevel@tonic-gate  * attributes.  However, sometimes during relocation counting, it is necessary
2687c478bd9Sstevel@tonic-gate  * to adjust the symbols information.  This routine provides for the generation
2697c478bd9Sstevel@tonic-gate  * of a new symbol image so that this update can be performed.
2707c478bd9Sstevel@tonic-gate  * All global symbols are copied to an internal symbol table to improve locality
2717c478bd9Sstevel@tonic-gate  * of reference and hence performance, and thus this copying is not necessary.
2727c478bd9Sstevel@tonic-gate  */
2737c478bd9Sstevel@tonic-gate uintptr_t
2745aefb655Srie ld_sym_copy(Sym_desc *sdp)
2757c478bd9Sstevel@tonic-gate {
2767c478bd9Sstevel@tonic-gate 	Sym	*nsym;
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate 	if (sdp->sd_flags & FLG_SY_CLEAN) {
2796b3ba5bdSAli Bahrami 		if ((nsym = libld_malloc(sizeof (Sym))) == NULL)
2807c478bd9Sstevel@tonic-gate 			return (S_ERROR);
2817c478bd9Sstevel@tonic-gate 		*nsym = *(sdp->sd_sym);
2827c478bd9Sstevel@tonic-gate 		sdp->sd_sym = nsym;
2837c478bd9Sstevel@tonic-gate 		sdp->sd_flags &= ~FLG_SY_CLEAN;
2847c478bd9Sstevel@tonic-gate 	}
2857c478bd9Sstevel@tonic-gate 	return (1);
2867c478bd9Sstevel@tonic-gate }
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate /*
2897c478bd9Sstevel@tonic-gate  * Finds a given name in the link editors internal symbol table.  If no
2907c478bd9Sstevel@tonic-gate  * hash value is specified it is calculated.  A pointer to the located
2917c478bd9Sstevel@tonic-gate  * Sym_desc entry is returned, or NULL if the symbol is not found.
2927c478bd9Sstevel@tonic-gate  */
2937c478bd9Sstevel@tonic-gate Sym_desc *
2945aefb655Srie ld_sym_find(const char *name, Word hash, avl_index_t *where, Ofl_desc *ofl)
2957c478bd9Sstevel@tonic-gate {
2966b3ba5bdSAli Bahrami 	Sym_avlnode	qsav, *sav;
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 	if (hash == SYM_NOHASH)
2997c478bd9Sstevel@tonic-gate 		/* LINTED */
3007c478bd9Sstevel@tonic-gate 		hash = (Word)elf_hash((const char *)name);
3017c478bd9Sstevel@tonic-gate 	qsav.sav_hash = hash;
3027c478bd9Sstevel@tonic-gate 	qsav.sav_name = name;
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate 	/*
3057c478bd9Sstevel@tonic-gate 	 * Perform search for symbol in AVL tree.  Note that the 'where' field
3067c478bd9Sstevel@tonic-gate 	 * is passed in from the caller.  If a 'where' is present, it can be
307a194faf8Srie 	 * used in subsequent 'ld_sym_enter()' calls if required.
3087c478bd9Sstevel@tonic-gate 	 */
3097c478bd9Sstevel@tonic-gate 	sav = avl_find(&ofl->ofl_symavl, &qsav, where);
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate 	/*
3127c478bd9Sstevel@tonic-gate 	 * If symbol was not found in the avl tree, return null to show that.
3137c478bd9Sstevel@tonic-gate 	 */
3146b3ba5bdSAli Bahrami 	if (sav == NULL)
3156b3ba5bdSAli Bahrami 		return (NULL);
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate 	/*
3187c478bd9Sstevel@tonic-gate 	 * Return symbol found.
3197c478bd9Sstevel@tonic-gate 	 */
320635216b6SRod Evans 	return (sav->sav_sdp);
3217c478bd9Sstevel@tonic-gate }
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate /*
3247c478bd9Sstevel@tonic-gate  * Enter a new symbol into the link editors internal symbol table.
3257c478bd9Sstevel@tonic-gate  * If the symbol is from an input file, information regarding the input file
3267c478bd9Sstevel@tonic-gate  * and input section is also recorded.  Otherwise (file == NULL) the symbol
3277c478bd9Sstevel@tonic-gate  * has been internally generated (ie. _etext, _edata, etc.).
3287c478bd9Sstevel@tonic-gate  */
3297c478bd9Sstevel@tonic-gate Sym_desc *
3305aefb655Srie ld_sym_enter(const char *name, Sym *osym, Word hash, Ifl_desc *ifl,
331635216b6SRod Evans     Ofl_desc *ofl, Word ndx, Word shndx, sd_flag_t sdflags, avl_index_t *where)
3327c478bd9Sstevel@tonic-gate {
3337c478bd9Sstevel@tonic-gate 	Sym_desc	*sdp;
3347c478bd9Sstevel@tonic-gate 	Sym_aux		*sap;
3357c478bd9Sstevel@tonic-gate 	Sym_avlnode	*savl;
3367c478bd9Sstevel@tonic-gate 	char		*_name;
3377c478bd9Sstevel@tonic-gate 	Sym		*nsym;
3387c478bd9Sstevel@tonic-gate 	Half		etype;
33960758829Srie 	uchar_t		vis;
3407c478bd9Sstevel@tonic-gate 	avl_index_t	_where;
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate 	/*
3437c478bd9Sstevel@tonic-gate 	 * Establish the file type.
3447c478bd9Sstevel@tonic-gate 	 */
3457c478bd9Sstevel@tonic-gate 	if (ifl)
3467c478bd9Sstevel@tonic-gate 		etype = ifl->ifl_ehdr->e_type;
3477c478bd9Sstevel@tonic-gate 	else
3487c478bd9Sstevel@tonic-gate 		etype = ET_NONE;
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate 	ofl->ofl_entercnt++;
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate 	/*
3537c478bd9Sstevel@tonic-gate 	 * Allocate a Sym Descriptor, Auxiliary Descriptor, and a Sym AVLNode -
3547c478bd9Sstevel@tonic-gate 	 * contiguously.
3557c478bd9Sstevel@tonic-gate 	 */
356635216b6SRod Evans 	if ((savl = libld_calloc(S_DROUND(sizeof (Sym_avlnode)) +
357635216b6SRod Evans 	    S_DROUND(sizeof (Sym_desc)) +
358635216b6SRod Evans 	    S_DROUND(sizeof (Sym_aux)), 1)) == NULL)
3597c478bd9Sstevel@tonic-gate 		return ((Sym_desc *)S_ERROR);
360635216b6SRod Evans 	sdp = (Sym_desc *)((uintptr_t)savl +
361635216b6SRod Evans 	    S_DROUND(sizeof (Sym_avlnode)));
362635216b6SRod Evans 	sap = (Sym_aux *)((uintptr_t)sdp +
363635216b6SRod Evans 	    S_DROUND(sizeof (Sym_desc)));
3647c478bd9Sstevel@tonic-gate 
365635216b6SRod Evans 	savl->sav_sdp = sdp;
3667c478bd9Sstevel@tonic-gate 	sdp->sd_file = ifl;
3677c478bd9Sstevel@tonic-gate 	sdp->sd_aux = sap;
3687c478bd9Sstevel@tonic-gate 	savl->sav_hash = sap->sa_hash = hash;
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate 	/*
3717c478bd9Sstevel@tonic-gate 	 * Copy the symbol table entry from the input file into the internal
3727c478bd9Sstevel@tonic-gate 	 * entry and have the symbol descriptor use it.
3737c478bd9Sstevel@tonic-gate 	 */
3747c478bd9Sstevel@tonic-gate 	sdp->sd_sym = nsym = &sap->sa_sym;
3757c478bd9Sstevel@tonic-gate 	*nsym = *osym;
3767c478bd9Sstevel@tonic-gate 	sdp->sd_shndx = shndx;
3777c478bd9Sstevel@tonic-gate 	sdp->sd_flags |= sdflags;
3787c478bd9Sstevel@tonic-gate 
3796b3ba5bdSAli Bahrami 	if ((_name = libld_malloc(strlen(name) + 1)) == NULL)
3807c478bd9Sstevel@tonic-gate 		return ((Sym_desc *)S_ERROR);
3817c478bd9Sstevel@tonic-gate 	savl->sav_name = sdp->sd_name = (const char *)strcpy(_name, name);
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate 	/*
3847c478bd9Sstevel@tonic-gate 	 * Enter Symbol in AVL tree.
3857c478bd9Sstevel@tonic-gate 	 */
3867c478bd9Sstevel@tonic-gate 	if (where == 0) {
3877c478bd9Sstevel@tonic-gate 		/* LINTED */
3887c478bd9Sstevel@tonic-gate 		Sym_avlnode	*_savl;
3897c478bd9Sstevel@tonic-gate 		/*
3905aefb655Srie 		 * If a previous ld_sym_find() hasn't initialized 'where' do it
3917c478bd9Sstevel@tonic-gate 		 * now.
3927c478bd9Sstevel@tonic-gate 		 */
3937c478bd9Sstevel@tonic-gate 		where = &_where;
3947c478bd9Sstevel@tonic-gate 		_savl = avl_find(&ofl->ofl_symavl, savl, where);
3956b3ba5bdSAli Bahrami 		assert(_savl == NULL);
3967c478bd9Sstevel@tonic-gate 	}
3977c478bd9Sstevel@tonic-gate 	avl_insert(&ofl->ofl_symavl, savl, *where);
3987c478bd9Sstevel@tonic-gate 
3997c478bd9Sstevel@tonic-gate 	/*
4007c478bd9Sstevel@tonic-gate 	 * Record the section index.  This is possible because the
4017c478bd9Sstevel@tonic-gate 	 * `ifl_isdesc' table is filled before we start symbol processing.
4027c478bd9Sstevel@tonic-gate 	 */
4030bc07c75Srie 	if ((sdflags & FLG_SY_SPECSEC) || (nsym->st_shndx == SHN_UNDEF))
4047c478bd9Sstevel@tonic-gate 		sdp->sd_isc = NULL;
4057c478bd9Sstevel@tonic-gate 	else {
4067c478bd9Sstevel@tonic-gate 		sdp->sd_isc = ifl->ifl_isdesc[shndx];
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate 		/*
4097c478bd9Sstevel@tonic-gate 		 * If this symbol is from a relocatable object, make sure that
4107c478bd9Sstevel@tonic-gate 		 * it is still associated with a section.  For example, an
4117c478bd9Sstevel@tonic-gate 		 * unknown section type (SHT_NULL) would have been rejected on
4127c478bd9Sstevel@tonic-gate 		 * input with a warning.  Here, we make the use of the symbol
4137c478bd9Sstevel@tonic-gate 		 * fatal.  A symbol descriptor is still returned, so that the
4147c478bd9Sstevel@tonic-gate 		 * caller can continue processing all symbols, and hence flush
4157c478bd9Sstevel@tonic-gate 		 * out as many error conditions as possible.
4167c478bd9Sstevel@tonic-gate 		 */
4176b3ba5bdSAli Bahrami 		if ((etype == ET_REL) && (sdp->sd_isc == NULL)) {
4185aefb655Srie 			eprintf(ofl->ofl_lml, ERR_FATAL,
4195aefb655Srie 			    MSG_INTL(MSG_SYM_INVSEC), name, ifl->ifl_name,
4205aefb655Srie 			    EC_XWORD(shndx));
4217c478bd9Sstevel@tonic-gate 			ofl->ofl_flags |= FLG_OF_FATAL;
4227c478bd9Sstevel@tonic-gate 			return (sdp);
4237c478bd9Sstevel@tonic-gate 		}
4247c478bd9Sstevel@tonic-gate 	}
4257c478bd9Sstevel@tonic-gate 
4267c478bd9Sstevel@tonic-gate 	/*
4277c478bd9Sstevel@tonic-gate 	 * Mark any COMMON symbols as 'tentative'.
4287c478bd9Sstevel@tonic-gate 	 */
42954d82594Sseizo 	if (sdflags & FLG_SY_SPECSEC) {
4300bc07c75Srie 		if (nsym->st_shndx == SHN_COMMON)
43154d82594Sseizo 			sdp->sd_flags |= FLG_SY_TENTSYM;
432ba2be530Sab #if	defined(_ELF64)
433ba2be530Sab 		else if ((ld_targ.t_m.m_mach == EM_AMD64) &&
434ba2be530Sab 		    (nsym->st_shndx == SHN_X86_64_LCOMMON))
43554d82594Sseizo 			sdp->sd_flags |= FLG_SY_TENTSYM;
43654d82594Sseizo #endif
43754d82594Sseizo 	}
4387c478bd9Sstevel@tonic-gate 
4397c478bd9Sstevel@tonic-gate 	/*
44060758829Srie 	 * Establish the symbols visibility and reference.
4417c478bd9Sstevel@tonic-gate 	 */
44260758829Srie 	vis = ELF_ST_VISIBILITY(nsym->st_other);
44360758829Srie 
4447c478bd9Sstevel@tonic-gate 	if ((etype == ET_NONE) || (etype == ET_REL)) {
44560758829Srie 		switch (vis) {
44660758829Srie 		case STV_DEFAULT:
447635216b6SRod Evans 			sdp->sd_flags |= FLG_SY_DEFAULT;
44860758829Srie 			break;
44960758829Srie 		case STV_INTERNAL:
45060758829Srie 		case STV_HIDDEN:
451635216b6SRod Evans 			sdp->sd_flags |= FLG_SY_HIDDEN;
45260758829Srie 			break;
45360758829Srie 		case STV_PROTECTED:
454635216b6SRod Evans 			sdp->sd_flags |= FLG_SY_PROTECT;
45560758829Srie 			break;
45660758829Srie 		case STV_EXPORTED:
457635216b6SRod Evans 			sdp->sd_flags |= FLG_SY_EXPORT;
45860758829Srie 			break;
45960758829Srie 		case STV_SINGLETON:
460635216b6SRod Evans 			sdp->sd_flags |= (FLG_SY_SINGLE | FLG_SY_NDIR);
46128bda19cSRod Evans 			ofl->ofl_flags1 |= (FLG_OF1_NDIRECT | FLG_OF1_NGLBDIR);
46260758829Srie 			break;
46360758829Srie 		case STV_ELIMINATE:
464635216b6SRod Evans 			sdp->sd_flags |= (FLG_SY_HIDDEN | FLG_SY_ELIM);
46560758829Srie 			break;
46660758829Srie 		default:
46760758829Srie 			assert(vis <= STV_ELIMINATE);
46860758829Srie 		}
46960758829Srie 
4707c478bd9Sstevel@tonic-gate 		sdp->sd_ref = REF_REL_NEED;
4717c478bd9Sstevel@tonic-gate 
4727c478bd9Sstevel@tonic-gate 		/*
4739a411307Srie 		 * Under -Bnodirect, all exported interfaces that have not
4749a411307Srie 		 * explicitly been defined protected or directly bound to, are
4759a411307Srie 		 * tagged to prevent direct binding.
4767c478bd9Sstevel@tonic-gate 		 */
4770bc07c75Srie 		if ((ofl->ofl_flags1 & FLG_OF1_ALNODIR) &&
478635216b6SRod Evans 		    ((sdp->sd_flags & (FLG_SY_PROTECT | FLG_SY_DIR)) == 0) &&
4799a411307Srie 		    (nsym->st_shndx != SHN_UNDEF)) {
480635216b6SRod Evans 			sdp->sd_flags |= FLG_SY_NDIR;
4819a411307Srie 		}
4827c478bd9Sstevel@tonic-gate 	} else {
4837c478bd9Sstevel@tonic-gate 		sdp->sd_ref = REF_DYN_SEEN;
4847c478bd9Sstevel@tonic-gate 
4857c478bd9Sstevel@tonic-gate 		/*
48660758829Srie 		 * If this is a protected symbol, remember this.  Note, this
487635216b6SRod Evans 		 * state is different from the FLG_SY_PROTECT used to establish
48860758829Srie 		 * a symbol definitions visibility.  This state is used to warn
48960758829Srie 		 * against possible copy relocations against this referenced
49060758829Srie 		 * symbol.
4917c478bd9Sstevel@tonic-gate 		 */
49260758829Srie 		if (vis == STV_PROTECTED)
4937c478bd9Sstevel@tonic-gate 			sdp->sd_flags |= FLG_SY_PROT;
4947c478bd9Sstevel@tonic-gate 
4957c478bd9Sstevel@tonic-gate 		/*
49660758829Srie 		 * If this is a SINGLETON definition, then indicate the symbol
49760758829Srie 		 * can not be directly bound to, and retain the visibility.
49860758829Srie 		 * This visibility will be inherited by any references made to
49960758829Srie 		 * this symbol.
5007c478bd9Sstevel@tonic-gate 		 */
50160758829Srie 		if ((vis == STV_SINGLETON) && (nsym->st_shndx != SHN_UNDEF))
502635216b6SRod Evans 			sdp->sd_flags |= (FLG_SY_SINGLE | FLG_SY_NDIR);
5037c478bd9Sstevel@tonic-gate 
5047c478bd9Sstevel@tonic-gate 		/*
50560758829Srie 		 * If the new symbol is from a shared library and is associated
50660758829Srie 		 * with a SHT_NOBITS section then this symbol originated from a
50760758829Srie 		 * tentative symbol.
5087c478bd9Sstevel@tonic-gate 		 */
5097c478bd9Sstevel@tonic-gate 		if (sdp->sd_isc &&
5107c478bd9Sstevel@tonic-gate 		    (sdp->sd_isc->is_shdr->sh_type == SHT_NOBITS))
5117c478bd9Sstevel@tonic-gate 			sdp->sd_flags |= FLG_SY_TENTSYM;
5127c478bd9Sstevel@tonic-gate 	}
5137c478bd9Sstevel@tonic-gate 
5147c478bd9Sstevel@tonic-gate 	/*
5157c478bd9Sstevel@tonic-gate 	 * Reclassify any SHN_SUNW_IGNORE symbols to SHN_UNDEF so as to
5160bc07c75Srie 	 * simplify future processing.
5177c478bd9Sstevel@tonic-gate 	 */
5180bc07c75Srie 	if (nsym->st_shndx == SHN_SUNW_IGNORE) {
5197c478bd9Sstevel@tonic-gate 		sdp->sd_shndx = shndx = SHN_UNDEF;
520635216b6SRod Evans 		sdp->sd_flags |= (FLG_SY_REDUCED |
521635216b6SRod Evans 		    FLG_SY_HIDDEN | FLG_SY_IGNORE | FLG_SY_ELIM);
5227c478bd9Sstevel@tonic-gate 	}
5237c478bd9Sstevel@tonic-gate 
5247c478bd9Sstevel@tonic-gate 	/*
5257c478bd9Sstevel@tonic-gate 	 * If this is an undefined, or common symbol from a relocatable object
5267c478bd9Sstevel@tonic-gate 	 * determine whether it is a global or weak reference (see build_osym(),
5277c478bd9Sstevel@tonic-gate 	 * where REF_DYN_NEED definitions are returned back to undefines).
5287c478bd9Sstevel@tonic-gate 	 */
52954d82594Sseizo 	if ((etype == ET_REL) &&
53054d82594Sseizo 	    (ELF_ST_BIND(nsym->st_info) == STB_GLOBAL) &&
5310bc07c75Srie 	    ((nsym->st_shndx == SHN_UNDEF) || ((sdflags & FLG_SY_SPECSEC) &&
532ba2be530Sab #if	defined(_ELF64)
5330bc07c75Srie 	    ((nsym->st_shndx == SHN_COMMON) ||
534ba2be530Sab 	    ((ld_targ.t_m.m_mach == EM_AMD64) &&
535ba2be530Sab 	    (nsym->st_shndx == SHN_X86_64_LCOMMON))))))
53654d82594Sseizo #else
53733eb6ee1Sab 	/* BEGIN CSTYLED */
5380bc07c75Srie 	    (nsym->st_shndx == SHN_COMMON))))
53933eb6ee1Sab 	/* END CSTYLED */
54054d82594Sseizo #endif
5417c478bd9Sstevel@tonic-gate 		sdp->sd_flags |= FLG_SY_GLOBREF;
5427c478bd9Sstevel@tonic-gate 
5437c478bd9Sstevel@tonic-gate 	/*
5447c478bd9Sstevel@tonic-gate 	 * Record the input filename on the referenced or defined files list
5457c478bd9Sstevel@tonic-gate 	 * for possible later diagnostics.  The `sa_rfile' pointer contains the
5467c478bd9Sstevel@tonic-gate 	 * name of the file that first referenced this symbol and is used to
5477c478bd9Sstevel@tonic-gate 	 * generate undefined symbol diagnostics (refer to sym_undef_entry()).
5487c478bd9Sstevel@tonic-gate 	 * Note that this entry can be overridden if a reference from a
5497c478bd9Sstevel@tonic-gate 	 * relocatable object is found after a reference from a shared object
5507c478bd9Sstevel@tonic-gate 	 * (refer to sym_override()).
5517c478bd9Sstevel@tonic-gate 	 * The `sa_dfiles' list is used to maintain the list of files that
5527c478bd9Sstevel@tonic-gate 	 * define the same symbol.  This list can be used for two reasons:
5537c478bd9Sstevel@tonic-gate 	 *
554*08278a5eSRod Evans 	 *  -	To save the first definition of a symbol that is not available
5557c478bd9Sstevel@tonic-gate 	 *	for this link-edit.
5567c478bd9Sstevel@tonic-gate 	 *
557*08278a5eSRod Evans 	 *  -	To save all definitions of a symbol when the -m option is in
5587c478bd9Sstevel@tonic-gate 	 *	effect.  This is optional as it is used to list multiple
5597c478bd9Sstevel@tonic-gate 	 *	(interposed) definitions of a symbol (refer to ldmap_out()),
5607c478bd9Sstevel@tonic-gate 	 *	and can be quite expensive.
5617c478bd9Sstevel@tonic-gate 	 */
5620bc07c75Srie 	if (nsym->st_shndx == SHN_UNDEF) {
5637c478bd9Sstevel@tonic-gate 		sap->sa_rfile = ifl->ifl_name;
5647c478bd9Sstevel@tonic-gate 	} else {
5657c478bd9Sstevel@tonic-gate 		if (sdp->sd_ref == REF_DYN_SEEN) {
5667c478bd9Sstevel@tonic-gate 			/*
5677c478bd9Sstevel@tonic-gate 			 * A symbol is determined to be unavailable if it
5687c478bd9Sstevel@tonic-gate 			 * belongs to a version of a shared object that this
5697c478bd9Sstevel@tonic-gate 			 * user does not wish to use, or if it belongs to an
5707c478bd9Sstevel@tonic-gate 			 * implicit shared object.
5717c478bd9Sstevel@tonic-gate 			 */
5727c478bd9Sstevel@tonic-gate 			if (ifl->ifl_vercnt) {
5739039eeafSab 				Ver_index	*vip;
5747c478bd9Sstevel@tonic-gate 				Half		vndx = ifl->ifl_versym[ndx];
5757c478bd9Sstevel@tonic-gate 
5767c478bd9Sstevel@tonic-gate 				sap->sa_dverndx = vndx;
5777c478bd9Sstevel@tonic-gate 				vip = &ifl->ifl_verndx[vndx];
5787c478bd9Sstevel@tonic-gate 				if (!(vip->vi_flags & FLG_VER_AVAIL)) {
5797c478bd9Sstevel@tonic-gate 					sdp->sd_flags |= FLG_SY_NOTAVAIL;
5807c478bd9Sstevel@tonic-gate 					sap->sa_vfile = ifl->ifl_name;
5817c478bd9Sstevel@tonic-gate 				}
5827c478bd9Sstevel@tonic-gate 			}
5837c478bd9Sstevel@tonic-gate 			if (!(ifl->ifl_flags & FLG_IF_NEEDED))
5847c478bd9Sstevel@tonic-gate 				sdp->sd_flags |= FLG_SY_NOTAVAIL;
5857c478bd9Sstevel@tonic-gate 
5867c478bd9Sstevel@tonic-gate 		} else if (etype == ET_REL) {
5877c478bd9Sstevel@tonic-gate 			/*
5887c478bd9Sstevel@tonic-gate 			 * If this symbol has been obtained from a versioned
5897c478bd9Sstevel@tonic-gate 			 * input relocatable object then the new symbol must be
5907c478bd9Sstevel@tonic-gate 			 * promoted to the versioning of the output file.
5917c478bd9Sstevel@tonic-gate 			 */
5927c478bd9Sstevel@tonic-gate 			if (ifl->ifl_versym)
5935aefb655Srie 				ld_vers_promote(sdp, ndx, ifl, ofl);
5947c478bd9Sstevel@tonic-gate 		}
5957c478bd9Sstevel@tonic-gate 
5967c478bd9Sstevel@tonic-gate 		if ((ofl->ofl_flags & FLG_OF_GENMAP) &&
5977c478bd9Sstevel@tonic-gate 		    ((sdflags & FLG_SY_SPECSEC) == 0))
59857ef7aa9SRod Evans 			if (aplist_append(&sap->sa_dfiles, ifl->ifl_name,
59957ef7aa9SRod Evans 			    AL_CNT_SDP_DFILES) == NULL)
6007c478bd9Sstevel@tonic-gate 				return ((Sym_desc *)S_ERROR);
6017c478bd9Sstevel@tonic-gate 	}
6027c478bd9Sstevel@tonic-gate 
60360758829Srie 	/*
60460758829Srie 	 * Provided we're not processing a mapfile, diagnose the entered symbol.
60560758829Srie 	 * Mapfile processing requires the symbol to be updated with additional
60660758829Srie 	 * information, therefore the diagnosing of the symbol is deferred until
60760758829Srie 	 * later (see Dbg_map_symbol()).
60860758829Srie 	 */
6096b3ba5bdSAli Bahrami 	if ((ifl == NULL) || ((ifl->ifl_flags & FLG_IF_MAPFILE) == 0))
61060758829Srie 		DBG_CALL(Dbg_syms_entered(ofl, nsym, sdp));
611635216b6SRod Evans 
6127c478bd9Sstevel@tonic-gate 	return (sdp);
6137c478bd9Sstevel@tonic-gate }
6147c478bd9Sstevel@tonic-gate 
6157c478bd9Sstevel@tonic-gate /*
6167c478bd9Sstevel@tonic-gate  * Add a special symbol to the symbol table.  Takes special symbol name with
6177c478bd9Sstevel@tonic-gate  * and without underscores.  This routine is called, after all other symbol
6187c478bd9Sstevel@tonic-gate  * resolution has completed, to generate a reserved absolute symbol (the
6197c478bd9Sstevel@tonic-gate  * underscore version).  Special symbols are updated with the appropriate
620dd94ecefSrie  * values in update_osym().  If the user has already defined this symbol
6217c478bd9Sstevel@tonic-gate  * issue a warning and leave the symbol as is.  If the non-underscore symbol
6227c478bd9Sstevel@tonic-gate  * is referenced then turn it into a weak alias of the underscored symbol.
6237c478bd9Sstevel@tonic-gate  *
624635216b6SRod Evans  * The bits in sdflags_u are OR'd into the flags field of the symbol for the
625635216b6SRod Evans  * underscored symbol.
626d579eb63Sab  *
6277c478bd9Sstevel@tonic-gate  * If this is a global symbol, and it hasn't explicitly been defined as being
6287c478bd9Sstevel@tonic-gate  * directly bound to, indicate that it can't be directly bound to.
6297c478bd9Sstevel@tonic-gate  * Historically, most special symbols only have meaning to the object in which
630c1c6f601Srie  * they exist, however, they've always been global.  To ensure compatibility
631c1c6f601Srie  * with any unexpected use presently in effect, ensure these symbols don't get
6327c478bd9Sstevel@tonic-gate  * directly bound to.  Note, that establishing this state here isn't sufficient
6337c478bd9Sstevel@tonic-gate  * to create a syminfo table, only if a syminfo table is being created by some
634c1c6f601Srie  * other symbol directives will the nodirect binding be recorded.  This ensures
6357c478bd9Sstevel@tonic-gate  * we don't create syminfo sections for all objects we create, as this might add
6367c478bd9Sstevel@tonic-gate  * unnecessary bloat to users who haven't explicitly requested extra symbol
6377c478bd9Sstevel@tonic-gate  * information.
6387c478bd9Sstevel@tonic-gate  */
6397c478bd9Sstevel@tonic-gate static uintptr_t
6407c478bd9Sstevel@tonic-gate sym_add_spec(const char *name, const char *uname, Word sdaux_id,
641635216b6SRod Evans     sd_flag_t sdflags_u, sd_flag_t sdflags, Ofl_desc *ofl)
6427c478bd9Sstevel@tonic-gate {
6437c478bd9Sstevel@tonic-gate 	Sym_desc	*sdp;
6447c478bd9Sstevel@tonic-gate 	Sym_desc 	*usdp;
6457c478bd9Sstevel@tonic-gate 	Sym		*sym;
6467c478bd9Sstevel@tonic-gate 	Word		hash;
6477c478bd9Sstevel@tonic-gate 	avl_index_t	where;
6487c478bd9Sstevel@tonic-gate 
6497c478bd9Sstevel@tonic-gate 	/* LINTED */
6507c478bd9Sstevel@tonic-gate 	hash = (Word)elf_hash(uname);
6515aefb655Srie 	if (usdp = ld_sym_find(uname, hash, &where, ofl)) {
6527c478bd9Sstevel@tonic-gate 		/*
6537c478bd9Sstevel@tonic-gate 		 * If the underscore symbol exists and is undefined, or was
6547c478bd9Sstevel@tonic-gate 		 * defined in a shared library, convert it to a local symbol.
6557c478bd9Sstevel@tonic-gate 		 * Otherwise leave it as is and warn the user.
6567c478bd9Sstevel@tonic-gate 		 */
6577c478bd9Sstevel@tonic-gate 		if ((usdp->sd_shndx == SHN_UNDEF) ||
6587c478bd9Sstevel@tonic-gate 		    (usdp->sd_ref != REF_REL_NEED)) {
6597c478bd9Sstevel@tonic-gate 			usdp->sd_ref = REF_REL_NEED;
6607c478bd9Sstevel@tonic-gate 			usdp->sd_shndx = usdp->sd_sym->st_shndx = SHN_ABS;
661635216b6SRod Evans 			usdp->sd_flags |= FLG_SY_SPECSEC | sdflags_u;
6627c478bd9Sstevel@tonic-gate 			usdp->sd_sym->st_info =
6637c478bd9Sstevel@tonic-gate 			    ELF_ST_INFO(STB_GLOBAL, STT_OBJECT);
6647c478bd9Sstevel@tonic-gate 			usdp->sd_isc = NULL;
6657c478bd9Sstevel@tonic-gate 			usdp->sd_sym->st_size = 0;
6667c478bd9Sstevel@tonic-gate 			usdp->sd_sym->st_value = 0;
6677c478bd9Sstevel@tonic-gate 			/* LINTED */
6687c478bd9Sstevel@tonic-gate 			usdp->sd_aux->sa_symspec = (Half)sdaux_id;
6697c478bd9Sstevel@tonic-gate 
6707c478bd9Sstevel@tonic-gate 			/*
6719a411307Srie 			 * If a user hasn't specifically indicated that the
6729a411307Srie 			 * scope of this symbol be made local, then leave it
6739a411307Srie 			 * as global (ie. prevent automatic scoping).  The GOT
6749a411307Srie 			 * should be defined protected, whereas all other
6759a411307Srie 			 * special symbols are tagged as no-direct.
6767c478bd9Sstevel@tonic-gate 			 */
677*08278a5eSRod Evans 			if (!SYM_IS_HIDDEN(usdp) &&
678635216b6SRod Evans 			    (sdflags & FLG_SY_DEFAULT)) {
67933eb6ee1Sab 				usdp->sd_aux->sa_overndx = VER_NDX_GLOBAL;
68033eb6ee1Sab 				if (sdaux_id == SDAUX_ID_GOT) {
681635216b6SRod Evans 					usdp->sd_flags &= ~FLG_SY_NDIR;
682635216b6SRod Evans 					usdp->sd_flags |= FLG_SY_PROTECT;
68333eb6ee1Sab 					usdp->sd_sym->st_other = STV_PROTECTED;
68433eb6ee1Sab 				} else if (
685635216b6SRod Evans 				    ((usdp->sd_flags & FLG_SY_DIR) == 0) &&
68633eb6ee1Sab 				    ((ofl->ofl_flags & FLG_OF_SYMBOLIC) == 0)) {
687635216b6SRod Evans 					usdp->sd_flags |= FLG_SY_NDIR;
68833eb6ee1Sab 				}
6897c478bd9Sstevel@tonic-gate 			}
690635216b6SRod Evans 			usdp->sd_flags |= sdflags;
6917c478bd9Sstevel@tonic-gate 
6927c478bd9Sstevel@tonic-gate 			/*
693c1c6f601Srie 			 * If the reference originated from a mapfile ensure
6947c478bd9Sstevel@tonic-gate 			 * we mark the symbol as used.
6957c478bd9Sstevel@tonic-gate 			 */
6967c478bd9Sstevel@tonic-gate 			if (usdp->sd_flags & FLG_SY_MAPREF)
6977c478bd9Sstevel@tonic-gate 				usdp->sd_flags |= FLG_SY_MAPUSED;
6987c478bd9Sstevel@tonic-gate 
6995aefb655Srie 			DBG_CALL(Dbg_syms_updated(ofl, usdp, uname));
7007c478bd9Sstevel@tonic-gate 		} else
7015aefb655Srie 			eprintf(ofl->ofl_lml, ERR_WARNING,
7025aefb655Srie 			    MSG_INTL(MSG_SYM_RESERVE), uname,
7037c478bd9Sstevel@tonic-gate 			    usdp->sd_file->ifl_name);
7047c478bd9Sstevel@tonic-gate 	} else {
7057c478bd9Sstevel@tonic-gate 		/*
7067c478bd9Sstevel@tonic-gate 		 * If the symbol does not exist create it.
7077c478bd9Sstevel@tonic-gate 		 */
7086b3ba5bdSAli Bahrami 		if ((sym = libld_calloc(sizeof (Sym), 1)) == NULL)
7097c478bd9Sstevel@tonic-gate 			return (S_ERROR);
7107c478bd9Sstevel@tonic-gate 		sym->st_shndx = SHN_ABS;
7117c478bd9Sstevel@tonic-gate 		sym->st_info = ELF_ST_INFO(STB_GLOBAL, STT_OBJECT);
7127c478bd9Sstevel@tonic-gate 		sym->st_size = 0;
7137c478bd9Sstevel@tonic-gate 		sym->st_value = 0;
7145aefb655Srie 		DBG_CALL(Dbg_syms_created(ofl->ofl_lml, uname));
7155aefb655Srie 		if ((usdp = ld_sym_enter(uname, sym, hash, (Ifl_desc *)NULL,
716635216b6SRod Evans 		    ofl, 0, SHN_ABS, (FLG_SY_SPECSEC | sdflags_u), &where)) ==
7177c478bd9Sstevel@tonic-gate 		    (Sym_desc *)S_ERROR)
7187c478bd9Sstevel@tonic-gate 			return (S_ERROR);
7197c478bd9Sstevel@tonic-gate 		usdp->sd_ref = REF_REL_NEED;
7207c478bd9Sstevel@tonic-gate 		/* LINTED */
7217c478bd9Sstevel@tonic-gate 		usdp->sd_aux->sa_symspec = (Half)sdaux_id;
7227c478bd9Sstevel@tonic-gate 
7237c478bd9Sstevel@tonic-gate 		usdp->sd_aux->sa_overndx = VER_NDX_GLOBAL;
7249a411307Srie 
7259a411307Srie 		if (sdaux_id == SDAUX_ID_GOT) {
726635216b6SRod Evans 			usdp->sd_flags |= FLG_SY_PROTECT;
7279a411307Srie 			usdp->sd_sym->st_other = STV_PROTECTED;
728635216b6SRod Evans 		} else if ((sdflags & FLG_SY_DEFAULT) &&
7299a411307Srie 		    ((ofl->ofl_flags & FLG_OF_SYMBOLIC) == 0)) {
730635216b6SRod Evans 			usdp->sd_flags |= FLG_SY_NDIR;
7319a411307Srie 		}
732635216b6SRod Evans 		usdp->sd_flags |= sdflags;
7337c478bd9Sstevel@tonic-gate 	}
7347c478bd9Sstevel@tonic-gate 
73528bda19cSRod Evans 	if (name && (sdp = ld_sym_find(name, SYM_NOHASH, NULL, ofl)) &&
7360bc07c75Srie 	    (sdp->sd_sym->st_shndx == SHN_UNDEF)) {
7377c478bd9Sstevel@tonic-gate 		uchar_t	bind;
7387c478bd9Sstevel@tonic-gate 
7397c478bd9Sstevel@tonic-gate 		/*
7407c478bd9Sstevel@tonic-gate 		 * If the non-underscore symbol exists and is undefined
7417c478bd9Sstevel@tonic-gate 		 * convert it to be a local.  If the underscore has
7427c478bd9Sstevel@tonic-gate 		 * sa_symspec set (ie. it was created above) then simulate this
7437c478bd9Sstevel@tonic-gate 		 * as a weak alias.
7447c478bd9Sstevel@tonic-gate 		 */
7457c478bd9Sstevel@tonic-gate 		sdp->sd_ref = REF_REL_NEED;
7467c478bd9Sstevel@tonic-gate 		sdp->sd_shndx = sdp->sd_sym->st_shndx = SHN_ABS;
7477c478bd9Sstevel@tonic-gate 		sdp->sd_flags |= FLG_SY_SPECSEC;
7487c478bd9Sstevel@tonic-gate 		sdp->sd_isc = NULL;
7497c478bd9Sstevel@tonic-gate 		sdp->sd_sym->st_size = 0;
7507c478bd9Sstevel@tonic-gate 		sdp->sd_sym->st_value = 0;
7517c478bd9Sstevel@tonic-gate 		/* LINTED */
7527c478bd9Sstevel@tonic-gate 		sdp->sd_aux->sa_symspec = (Half)sdaux_id;
7537c478bd9Sstevel@tonic-gate 		if (usdp->sd_aux->sa_symspec) {
7547c478bd9Sstevel@tonic-gate 			usdp->sd_aux->sa_linkndx = 0;
7557c478bd9Sstevel@tonic-gate 			sdp->sd_aux->sa_linkndx = 0;
7567c478bd9Sstevel@tonic-gate 			bind = STB_WEAK;
7577c478bd9Sstevel@tonic-gate 		} else
7587c478bd9Sstevel@tonic-gate 			bind = STB_GLOBAL;
7597c478bd9Sstevel@tonic-gate 		sdp->sd_sym->st_info = ELF_ST_INFO(bind, STT_OBJECT);
7607c478bd9Sstevel@tonic-gate 
7617c478bd9Sstevel@tonic-gate 		/*
7629a411307Srie 		 * If a user hasn't specifically indicated the scope of this
7639a411307Srie 		 * symbol be made local then leave it as global (ie. prevent
7649a411307Srie 		 * automatic scoping).  The GOT should be defined protected,
7659a411307Srie 		 * whereas all other special symbols are tagged as no-direct.
7667c478bd9Sstevel@tonic-gate 		 */
767*08278a5eSRod Evans 		if (!SYM_IS_HIDDEN(sdp) &&
768635216b6SRod Evans 		    (sdflags & FLG_SY_DEFAULT)) {
7697c478bd9Sstevel@tonic-gate 			sdp->sd_aux->sa_overndx = VER_NDX_GLOBAL;
7709a411307Srie 			if (sdaux_id == SDAUX_ID_GOT) {
771635216b6SRod Evans 				sdp->sd_flags &= ~FLG_SY_NDIR;
772635216b6SRod Evans 				sdp->sd_flags |= FLG_SY_PROTECT;
7739a411307Srie 				sdp->sd_sym->st_other = STV_PROTECTED;
774635216b6SRod Evans 			} else if (((sdp->sd_flags & FLG_SY_DIR) == 0) &&
7759a411307Srie 			    ((ofl->ofl_flags & FLG_OF_SYMBOLIC) == 0)) {
776635216b6SRod Evans 				sdp->sd_flags |= FLG_SY_NDIR;
7779a411307Srie 			}
7787c478bd9Sstevel@tonic-gate 		}
779635216b6SRod Evans 		sdp->sd_flags |= sdflags;
7807c478bd9Sstevel@tonic-gate 
7817c478bd9Sstevel@tonic-gate 		/*
782c1c6f601Srie 		 * If the reference originated from a mapfile ensure
7837c478bd9Sstevel@tonic-gate 		 * we mark the symbol as used.
7847c478bd9Sstevel@tonic-gate 		 */
7857c478bd9Sstevel@tonic-gate 		if (sdp->sd_flags & FLG_SY_MAPREF)
7867c478bd9Sstevel@tonic-gate 			sdp->sd_flags |= FLG_SY_MAPUSED;
7877c478bd9Sstevel@tonic-gate 
7885aefb655Srie 		DBG_CALL(Dbg_syms_updated(ofl, sdp, name));
7897c478bd9Sstevel@tonic-gate 	}
7907c478bd9Sstevel@tonic-gate 	return (1);
7917c478bd9Sstevel@tonic-gate }
7927c478bd9Sstevel@tonic-gate 
7937c478bd9Sstevel@tonic-gate 
7947c478bd9Sstevel@tonic-gate /*
7957c478bd9Sstevel@tonic-gate  * Print undefined symbols.
7967c478bd9Sstevel@tonic-gate  */
7977c478bd9Sstevel@tonic-gate static Boolean	undef_title = TRUE;
7987c478bd9Sstevel@tonic-gate 
7995aefb655Srie static void
8005aefb655Srie sym_undef_title(Ofl_desc *ofl)
8017c478bd9Sstevel@tonic-gate {
8025aefb655Srie 	eprintf(ofl->ofl_lml, ERR_NONE, MSG_INTL(MSG_SYM_FMT_UNDEF),
80333eb6ee1Sab 	    MSG_INTL(MSG_SYM_UNDEF_ITM_11),
80433eb6ee1Sab 	    MSG_INTL(MSG_SYM_UNDEF_ITM_21),
80533eb6ee1Sab 	    MSG_INTL(MSG_SYM_UNDEF_ITM_12),
80633eb6ee1Sab 	    MSG_INTL(MSG_SYM_UNDEF_ITM_22));
8077c478bd9Sstevel@tonic-gate 
8087c478bd9Sstevel@tonic-gate 	undef_title = FALSE;
8097c478bd9Sstevel@tonic-gate }
8107c478bd9Sstevel@tonic-gate 
8117c478bd9Sstevel@tonic-gate /*
8127c478bd9Sstevel@tonic-gate  * Undefined symbols can fall into one of four types:
8137c478bd9Sstevel@tonic-gate  *
8146b3ba5bdSAli Bahrami  *  -	the symbol is really undefined (SHN_UNDEF).
8157c478bd9Sstevel@tonic-gate  *
8166b3ba5bdSAli Bahrami  *  -	versioning has been enabled, however this symbol has not been assigned
8177c478bd9Sstevel@tonic-gate  *	to one of the defined versions.
8187c478bd9Sstevel@tonic-gate  *
8196b3ba5bdSAli Bahrami  *  -	the symbol has been defined by an implicitly supplied library, ie. one
8207c478bd9Sstevel@tonic-gate  *	which was encounted because it was NEEDED by another library, rather
8217c478bd9Sstevel@tonic-gate  * 	than from a command line supplied library which would become the only
8227c478bd9Sstevel@tonic-gate  *	dependency of the output file being produced.
8237c478bd9Sstevel@tonic-gate  *
8246b3ba5bdSAli Bahrami  *  -	the symbol has been defined by a version of a shared object that is
8257c478bd9Sstevel@tonic-gate  *	not permitted for this link-edit.
8267c478bd9Sstevel@tonic-gate  *
8277c478bd9Sstevel@tonic-gate  * In all cases the file who made the first reference to this symbol will have
8287c478bd9Sstevel@tonic-gate  * been recorded via the `sa_rfile' pointer.
8297c478bd9Sstevel@tonic-gate  */
8307c478bd9Sstevel@tonic-gate typedef enum {
8317c478bd9Sstevel@tonic-gate 	UNDEF,		NOVERSION,	IMPLICIT,	NOTAVAIL,
8327c478bd9Sstevel@tonic-gate 	BNDLOCAL
8337c478bd9Sstevel@tonic-gate } Type;
8347c478bd9Sstevel@tonic-gate 
8357c478bd9Sstevel@tonic-gate static const Msg format[] = {
8367c478bd9Sstevel@tonic-gate 	MSG_SYM_UND_UNDEF,		/* MSG_INTL(MSG_SYM_UND_UNDEF) */
8377c478bd9Sstevel@tonic-gate 	MSG_SYM_UND_NOVER,		/* MSG_INTL(MSG_SYM_UND_NOVER) */
8387c478bd9Sstevel@tonic-gate 	MSG_SYM_UND_IMPL,		/* MSG_INTL(MSG_SYM_UND_IMPL) */
8397c478bd9Sstevel@tonic-gate 	MSG_SYM_UND_NOTA,		/* MSG_INTL(MSG_SYM_UND_NOTA) */
8407c478bd9Sstevel@tonic-gate 	MSG_SYM_UND_BNDLOCAL		/* MSG_INTL(MSG_SYM_UND_BNDLOCAL) */
8417c478bd9Sstevel@tonic-gate };
8427c478bd9Sstevel@tonic-gate 
8435aefb655Srie static void
8445aefb655Srie sym_undef_entry(Ofl_desc *ofl, Sym_desc *sdp, Type type)
8457c478bd9Sstevel@tonic-gate {
8467c478bd9Sstevel@tonic-gate 	const char	*name1, *name2, *name3;
8477c478bd9Sstevel@tonic-gate 	Ifl_desc	*ifl = sdp->sd_file;
8487c478bd9Sstevel@tonic-gate 	Sym_aux		*sap = sdp->sd_aux;
8497c478bd9Sstevel@tonic-gate 
8507c478bd9Sstevel@tonic-gate 	if (undef_title)
8515aefb655Srie 		sym_undef_title(ofl);
8527c478bd9Sstevel@tonic-gate 
8537c478bd9Sstevel@tonic-gate 	switch (type) {
8547c478bd9Sstevel@tonic-gate 	case UNDEF:
8557c478bd9Sstevel@tonic-gate 	case BNDLOCAL:
8567c478bd9Sstevel@tonic-gate 		name1 = sap->sa_rfile;
8577c478bd9Sstevel@tonic-gate 		break;
8587c478bd9Sstevel@tonic-gate 	case NOVERSION:
8597c478bd9Sstevel@tonic-gate 		name1 = ifl->ifl_name;
8607c478bd9Sstevel@tonic-gate 		break;
8617c478bd9Sstevel@tonic-gate 	case IMPLICIT:
8627c478bd9Sstevel@tonic-gate 		name1 = sap->sa_rfile;
8637c478bd9Sstevel@tonic-gate 		name2 = ifl->ifl_name;
8647c478bd9Sstevel@tonic-gate 		break;
8657c478bd9Sstevel@tonic-gate 	case NOTAVAIL:
8667c478bd9Sstevel@tonic-gate 		name1 = sap->sa_rfile;
8677c478bd9Sstevel@tonic-gate 		name2 = sap->sa_vfile;
8687c478bd9Sstevel@tonic-gate 		name3 = ifl->ifl_verndx[sap->sa_dverndx].vi_name;
8697c478bd9Sstevel@tonic-gate 		break;
8707c478bd9Sstevel@tonic-gate 	default:
8717c478bd9Sstevel@tonic-gate 		return;
8727c478bd9Sstevel@tonic-gate 	}
8737c478bd9Sstevel@tonic-gate 
8745aefb655Srie 	eprintf(ofl->ofl_lml, ERR_NONE, MSG_INTL(format[type]),
8755aefb655Srie 	    demangle(sdp->sd_name), name1, name2, name3);
8767c478bd9Sstevel@tonic-gate }
8777c478bd9Sstevel@tonic-gate 
8787c478bd9Sstevel@tonic-gate /*
8797c478bd9Sstevel@tonic-gate  * At this point all symbol input processing has been completed, therefore
8807c478bd9Sstevel@tonic-gate  * complete the symbol table entries by generating any necessary internal
8817c478bd9Sstevel@tonic-gate  * symbols.
8827c478bd9Sstevel@tonic-gate  */
8837c478bd9Sstevel@tonic-gate uintptr_t
8845aefb655Srie ld_sym_spec(Ofl_desc *ofl)
8857c478bd9Sstevel@tonic-gate {
8869a411307Srie 	Sym_desc	*sdp;
8877c478bd9Sstevel@tonic-gate 
8889a411307Srie 	if (ofl->ofl_flags & FLG_OF_RELOBJ)
8899a411307Srie 		return (1);
8907c478bd9Sstevel@tonic-gate 
8919a411307Srie 	DBG_CALL(Dbg_syms_spec_title(ofl->ofl_lml));
8929a411307Srie 
8939a411307Srie 	if (sym_add_spec(MSG_ORIG(MSG_SYM_ETEXT), MSG_ORIG(MSG_SYM_ETEXT_U),
894635216b6SRod Evans 	    SDAUX_ID_ETEXT, 0, (FLG_SY_DEFAULT | FLG_SY_EXPDEF),
89560758829Srie 	    ofl) == S_ERROR)
8969a411307Srie 		return (S_ERROR);
8979a411307Srie 	if (sym_add_spec(MSG_ORIG(MSG_SYM_EDATA), MSG_ORIG(MSG_SYM_EDATA_U),
898635216b6SRod Evans 	    SDAUX_ID_EDATA, 0, (FLG_SY_DEFAULT | FLG_SY_EXPDEF),
89960758829Srie 	    ofl) == S_ERROR)
9009a411307Srie 		return (S_ERROR);
9019a411307Srie 	if (sym_add_spec(MSG_ORIG(MSG_SYM_END), MSG_ORIG(MSG_SYM_END_U),
902635216b6SRod Evans 	    SDAUX_ID_END, FLG_SY_DYNSORT, (FLG_SY_DEFAULT | FLG_SY_EXPDEF),
90360758829Srie 	    ofl) == S_ERROR)
9049a411307Srie 		return (S_ERROR);
9059a411307Srie 	if (sym_add_spec(MSG_ORIG(MSG_SYM_L_END), MSG_ORIG(MSG_SYM_L_END_U),
906635216b6SRod Evans 	    SDAUX_ID_END, 0, FLG_SY_HIDDEN, ofl) == S_ERROR)
9079a411307Srie 		return (S_ERROR);
9089a411307Srie 	if (sym_add_spec(MSG_ORIG(MSG_SYM_L_START), MSG_ORIG(MSG_SYM_L_START_U),
909635216b6SRod Evans 	    SDAUX_ID_START, 0, FLG_SY_HIDDEN, ofl) == S_ERROR)
9109a411307Srie 		return (S_ERROR);
9117c478bd9Sstevel@tonic-gate 
9129a411307Srie 	/*
9139a411307Srie 	 * Historically we've always produced a _DYNAMIC symbol, even for
9149a411307Srie 	 * static executables (in which case its value will be 0).
9159a411307Srie 	 */
9169a411307Srie 	if (sym_add_spec(MSG_ORIG(MSG_SYM_DYNAMIC), MSG_ORIG(MSG_SYM_DYNAMIC_U),
917635216b6SRod Evans 	    SDAUX_ID_DYN, FLG_SY_DYNSORT, (FLG_SY_DEFAULT | FLG_SY_EXPDEF),
91860758829Srie 	    ofl) == S_ERROR)
9199a411307Srie 		return (S_ERROR);
9209a411307Srie 
921d579eb63Sab 	if (OFL_ALLOW_DYNSYM(ofl))
9229a411307Srie 		if (sym_add_spec(MSG_ORIG(MSG_SYM_PLKTBL),
9239a411307Srie 		    MSG_ORIG(MSG_SYM_PLKTBL_U), SDAUX_ID_PLT,
924635216b6SRod Evans 		    FLG_SY_DYNSORT, (FLG_SY_DEFAULT | FLG_SY_EXPDEF),
92560758829Srie 		    ofl) == S_ERROR)
9267c478bd9Sstevel@tonic-gate 			return (S_ERROR);
9277c478bd9Sstevel@tonic-gate 
9289a411307Srie 	/*
9299a411307Srie 	 * A GOT reference will be accompanied by the associated GOT symbol.
9309a411307Srie 	 * Make sure it gets assigned the appropriate special attributes.
9319a411307Srie 	 */
9329a411307Srie 	if (((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_GOFTBL_U),
933635216b6SRod Evans 	    SYM_NOHASH, NULL, ofl)) != NULL) && (sdp->sd_ref != REF_DYN_SEEN)) {
9349a411307Srie 		if (sym_add_spec(MSG_ORIG(MSG_SYM_GOFTBL),
935d579eb63Sab 		    MSG_ORIG(MSG_SYM_GOFTBL_U), SDAUX_ID_GOT, FLG_SY_DYNSORT,
936635216b6SRod Evans 		    (FLG_SY_DEFAULT | FLG_SY_EXPDEF), ofl) == S_ERROR)
9379a411307Srie 			return (S_ERROR);
9387c478bd9Sstevel@tonic-gate 	}
9399a411307Srie 
9407c478bd9Sstevel@tonic-gate 	return (1);
9417c478bd9Sstevel@tonic-gate }
9427c478bd9Sstevel@tonic-gate 
943*08278a5eSRod Evans /*
944*08278a5eSRod Evans  * Determine a potential capability symbol's visibility.
945*08278a5eSRod Evans  *
946*08278a5eSRod Evans  * The -z symbolcap option transforms an object capabilities relocatable object
947*08278a5eSRod Evans  * into a symbol capabilities relocatable object.  Any global function symbols,
948*08278a5eSRod Evans  * or initialized global data symbols are candidates for transforming into local
949*08278a5eSRod Evans  * symbol capabilities definitions.  However, if a user indicates that a symbol
950*08278a5eSRod Evans  * should be demoted to local using a mapfile, then there is no need to
951*08278a5eSRod Evans  * transform the associated global symbol.
952*08278a5eSRod Evans  *
953*08278a5eSRod Evans  * Normally, a symbol's visibility is determined after the symbol resolution
954*08278a5eSRod Evans  * process, after all symbol state has been gathered and resolved.  However,
955*08278a5eSRod Evans  * for -z symbolcap, this determination is too late.  When a global symbol is
956*08278a5eSRod Evans  * read from an input file we need to determine it's visibility so as to decide
957*08278a5eSRod Evans  * whether to create a local or not.
958*08278a5eSRod Evans  *
959*08278a5eSRod Evans  * If a user has explicitly defined this symbol as having local scope within a
960*08278a5eSRod Evans  * mapfile, then a symbol of the same name already exists.  However, explicit
961*08278a5eSRod Evans  * local definitions are uncommon, as most mapfiles define the global symbol
962*08278a5eSRod Evans  * requirements together with an auto-reduction directive '*'.  If this state
963*08278a5eSRod Evans  * has been defined, then we must make sure that the new symbol isn't a type
964*08278a5eSRod Evans  * that can not be demoted to local.
965*08278a5eSRod Evans  */
966*08278a5eSRod Evans static int
967*08278a5eSRod Evans sym_cap_vis(const char *name, Word hash, Sym *sym, Ofl_desc *ofl)
968*08278a5eSRod Evans {
969*08278a5eSRod Evans 	Sym_desc	*sdp;
970*08278a5eSRod Evans 	uchar_t		vis;
971*08278a5eSRod Evans 	avl_index_t	where;
972*08278a5eSRod Evans 	sd_flag_t	sdflags = 0;
973*08278a5eSRod Evans 
974*08278a5eSRod Evans 	/*
975*08278a5eSRod Evans 	 * Determine the visibility of the new symbol.
976*08278a5eSRod Evans 	 */
977*08278a5eSRod Evans 	vis = ELF_ST_VISIBILITY(sym->st_other);
978*08278a5eSRod Evans 	switch (vis) {
979*08278a5eSRod Evans 	case STV_EXPORTED:
980*08278a5eSRod Evans 		sdflags |= FLG_SY_EXPORT;
981*08278a5eSRod Evans 		break;
982*08278a5eSRod Evans 	case STV_SINGLETON:
983*08278a5eSRod Evans 		sdflags |= FLG_SY_SINGLE;
984*08278a5eSRod Evans 		break;
985*08278a5eSRod Evans 	}
986*08278a5eSRod Evans 
987*08278a5eSRod Evans 	/*
988*08278a5eSRod Evans 	 * Determine whether a symbol definition already exists, and if so
989*08278a5eSRod Evans 	 * obtain the visibility.
990*08278a5eSRod Evans 	 */
991*08278a5eSRod Evans 	if ((sdp = ld_sym_find(name, hash, &where, ofl)) != NULL)
992*08278a5eSRod Evans 		sdflags |= sdp->sd_flags;
993*08278a5eSRod Evans 
994*08278a5eSRod Evans 	/*
995*08278a5eSRod Evans 	 * Determine whether the symbol flags indicate this symbol should be
996*08278a5eSRod Evans 	 * hidden.
997*08278a5eSRod Evans 	 */
998*08278a5eSRod Evans 	if ((ofl->ofl_flags & (FLG_OF_AUTOLCL | FLG_OF_AUTOELM)) &&
999*08278a5eSRod Evans 	    ((sdflags & MSK_SY_NOAUTO) == 0))
1000*08278a5eSRod Evans 		sdflags |= FLG_SY_HIDDEN;
1001*08278a5eSRod Evans 
1002*08278a5eSRod Evans 	return ((sdflags & FLG_SY_HIDDEN) == 0);
1003*08278a5eSRod Evans }
1004*08278a5eSRod Evans 
10057c478bd9Sstevel@tonic-gate /*
10067c478bd9Sstevel@tonic-gate  * This routine checks to see if a symbols visibility needs to be reduced to
10077c478bd9Sstevel@tonic-gate  * either SYMBOLIC or LOCAL.  This routine can be called from either
10087c478bd9Sstevel@tonic-gate  * reloc_init() or sym_validate().
10097c478bd9Sstevel@tonic-gate  */
10107c478bd9Sstevel@tonic-gate void
10115aefb655Srie ld_sym_adjust_vis(Sym_desc *sdp, Ofl_desc *ofl)
10127c478bd9Sstevel@tonic-gate {
101344bac77bSrie 	ofl_flag_t	oflags = ofl->ofl_flags;
101444bac77bSrie 	Sym		*sym = sdp->sd_sym;
10157c478bd9Sstevel@tonic-gate 
10160bc07c75Srie 	if ((sdp->sd_ref == REF_REL_NEED) &&
10170bc07c75Srie 	    (sdp->sd_sym->st_shndx != SHN_UNDEF)) {
10187c478bd9Sstevel@tonic-gate 		/*
101960758829Srie 		 * If auto-reduction/elimination is enabled, reduce any
1020*08278a5eSRod Evans 		 * non-versioned, and non-local capabilities global symbols.
102160758829Srie 		 * A symbol is a candidate for auto-reduction/elimination if:
102260758829Srie 		 *
102328bda19cSRod Evans 		 *  -	the symbol wasn't explicitly defined within a mapfile
102460758829Srie 		 *	(in which case all the necessary state has been applied
102560758829Srie 		 *	to the symbol), or
102628bda19cSRod Evans 		 *  -	the symbol isn't one of the family of reserved
102760758829Srie 		 *	special symbols (ie. _end, _etext, etc.), or
102828bda19cSRod Evans 		 *  -	the symbol isn't a SINGLETON, or
102928bda19cSRod Evans 		 *  -	the symbol wasn't explicitly defined within a version
103060758829Srie 		 *	definition associated with an input relocatable object.
103160758829Srie 		 *
10327c478bd9Sstevel@tonic-gate 		 * Indicate that the symbol has been reduced as it may be
10337c478bd9Sstevel@tonic-gate 		 * necessary to print these symbols later.
10347c478bd9Sstevel@tonic-gate 		 */
103544bac77bSrie 		if ((oflags & (FLG_OF_AUTOLCL | FLG_OF_AUTOELM)) &&
1036635216b6SRod Evans 		    ((sdp->sd_flags & MSK_SY_NOAUTO) == 0)) {
1037635216b6SRod Evans 			if ((sdp->sd_flags & FLG_SY_HIDDEN) == 0) {
1038635216b6SRod Evans 				sdp->sd_flags |=
1039635216b6SRod Evans 				    (FLG_SY_REDUCED | FLG_SY_HIDDEN);
104060758829Srie 			}
10417c478bd9Sstevel@tonic-gate 
104244bac77bSrie 			if (oflags & (FLG_OF_REDLSYM | FLG_OF_AUTOELM)) {
1043635216b6SRod Evans 				sdp->sd_flags |= FLG_SY_ELIM;
104460758829Srie 				sym->st_other = STV_ELIMINATE |
104560758829Srie 				    (sym->st_other & ~MSK_SYM_VISIBILITY);
104660758829Srie 			} else if (ELF_ST_VISIBILITY(sym->st_other) !=
104760758829Srie 			    STV_INTERNAL)
104860758829Srie 				sym->st_other = STV_HIDDEN |
104960758829Srie 				    (sym->st_other & ~MSK_SYM_VISIBILITY);
10507c478bd9Sstevel@tonic-gate 		}
10517c478bd9Sstevel@tonic-gate 
10527c478bd9Sstevel@tonic-gate 		/*
10539a411307Srie 		 * If -Bsymbolic is in effect, and the symbol hasn't explicitly
10549a411307Srie 		 * been defined nodirect (via a mapfile), then bind the global
10559a411307Srie 		 * symbol symbolically and assign the STV_PROTECTED visibility
10567c478bd9Sstevel@tonic-gate 		 * attribute.
10577c478bd9Sstevel@tonic-gate 		 */
10587c478bd9Sstevel@tonic-gate 		if ((oflags & FLG_OF_SYMBOLIC) &&
1059635216b6SRod Evans 		    ((sdp->sd_flags & (FLG_SY_HIDDEN | FLG_SY_NDIR)) == 0)) {
1060635216b6SRod Evans 			sdp->sd_flags |= FLG_SY_PROTECT;
10617c478bd9Sstevel@tonic-gate 			if (ELF_ST_VISIBILITY(sym->st_other) == STV_DEFAULT)
10627c478bd9Sstevel@tonic-gate 				sym->st_other = STV_PROTECTED |
10637c478bd9Sstevel@tonic-gate 				    (sym->st_other & ~MSK_SYM_VISIBILITY);
10647c478bd9Sstevel@tonic-gate 		}
10657c478bd9Sstevel@tonic-gate 	}
10667c478bd9Sstevel@tonic-gate 
10677c478bd9Sstevel@tonic-gate 	/*
10687c478bd9Sstevel@tonic-gate 	 * Indicate that this symbol has had it's visibility checked so that
10697c478bd9Sstevel@tonic-gate 	 * we don't need to do this investigation again.
10707c478bd9Sstevel@tonic-gate 	 */
10717c478bd9Sstevel@tonic-gate 	sdp->sd_flags |= FLG_SY_VISIBLE;
10727c478bd9Sstevel@tonic-gate }
10737c478bd9Sstevel@tonic-gate 
1074c1c6f601Srie /*
1075c1c6f601Srie  * Make sure a symbol definition is local to the object being built.
1076c1c6f601Srie  */
107757ef7aa9SRod Evans inline static int
1078c1c6f601Srie ensure_sym_local(Ofl_desc *ofl, Sym_desc *sdp, const char *str)
1079c1c6f601Srie {
1080c1c6f601Srie 	if (sdp->sd_sym->st_shndx == SHN_UNDEF) {
1081c1c6f601Srie 		if (str) {
1082c1c6f601Srie 			eprintf(ofl->ofl_lml, ERR_FATAL,
1083c1c6f601Srie 			    MSG_INTL(MSG_SYM_UNDEF), str,
1084c1c6f601Srie 			    demangle((char *)sdp->sd_name));
1085c1c6f601Srie 		}
1086c1c6f601Srie 		return (1);
1087c1c6f601Srie 	}
1088c1c6f601Srie 	if (sdp->sd_ref != REF_REL_NEED) {
1089c1c6f601Srie 		if (str) {
1090c1c6f601Srie 			eprintf(ofl->ofl_lml, ERR_FATAL,
1091c1c6f601Srie 			    MSG_INTL(MSG_SYM_EXTERN), str,
1092c1c6f601Srie 			    demangle((char *)sdp->sd_name),
1093c1c6f601Srie 			    sdp->sd_file->ifl_name);
1094c1c6f601Srie 		}
1095c1c6f601Srie 		return (1);
1096c1c6f601Srie 	}
1097c1c6f601Srie 
1098c1c6f601Srie 	sdp->sd_flags |= FLG_SY_UPREQD;
1099c1c6f601Srie 	if (sdp->sd_isc) {
1100c1c6f601Srie 		sdp->sd_isc->is_flags |= FLG_IS_SECTREF;
1101c1c6f601Srie 		sdp->sd_isc->is_file->ifl_flags |= FLG_IF_FILEREF;
1102c1c6f601Srie 	}
1103c1c6f601Srie 	return (0);
1104c1c6f601Srie }
1105c1c6f601Srie 
1106c1c6f601Srie /*
1107c1c6f601Srie  * Make sure all the symbol definitions required for initarray, finiarray, or
1108c1c6f601Srie  * preinitarray's are local to the object being built.
1109c1c6f601Srie  */
1110c1c6f601Srie static int
111157ef7aa9SRod Evans ensure_array_local(Ofl_desc *ofl, APlist *apl, const char *str)
1112c1c6f601Srie {
111357ef7aa9SRod Evans 	Aliste		idx;
1114c1c6f601Srie 	Sym_desc	*sdp;
1115c1c6f601Srie 	int		ret = 0;
1116c1c6f601Srie 
111757ef7aa9SRod Evans 	for (APLIST_TRAVERSE(apl, idx, sdp))
1118c1c6f601Srie 		ret += ensure_sym_local(ofl, sdp, str);
1119c1c6f601Srie 
1120c1c6f601Srie 	return (ret);
1121c1c6f601Srie }
1122c1c6f601Srie 
11237c478bd9Sstevel@tonic-gate /*
11247c478bd9Sstevel@tonic-gate  * After all symbol table input processing has been finished, and all relocation
11257c478bd9Sstevel@tonic-gate  * counting has been carried out (ie. no more symbols will be read, generated,
11267c478bd9Sstevel@tonic-gate  * or modified), validate and count the relevant entries:
11277c478bd9Sstevel@tonic-gate  *
1128*08278a5eSRod Evans  *  -	check and print any undefined symbols remaining.  Note that if a symbol
1129*08278a5eSRod Evans  *	has been defined by virtue of the inclusion of 	an implicit shared
1130*08278a5eSRod Evans  *	library, it is still classed as undefined.
11317c478bd9Sstevel@tonic-gate  *
1132*08278a5eSRod Evans  *  -	count the number of global needed symbols together with the size of
1133*08278a5eSRod Evans  *	their associated name strings (if scoping has been indicated these
1134*08278a5eSRod Evans  *	symbols may be reduced to locals).
11357c478bd9Sstevel@tonic-gate  *
1136*08278a5eSRod Evans  *  -	establish the size and alignment requirements for the global .bss
1137*08278a5eSRod Evans  *	section (the alignment of this section is based on the 	first symbol
1138*08278a5eSRod Evans  *	that it will contain).
11397c478bd9Sstevel@tonic-gate  */
11407c478bd9Sstevel@tonic-gate uintptr_t
11415aefb655Srie ld_sym_validate(Ofl_desc *ofl)
11427c478bd9Sstevel@tonic-gate {
11437c478bd9Sstevel@tonic-gate 	Sym_avlnode	*sav;
11447c478bd9Sstevel@tonic-gate 	Sym_desc	*sdp;
11457c478bd9Sstevel@tonic-gate 	Sym		*sym;
11461d9df23bSab 	ofl_flag_t	oflags = ofl->ofl_flags;
11471d9df23bSab 	ofl_flag_t	undef = 0, needed = 0, verdesc = 0;
11487c478bd9Sstevel@tonic-gate 	Xword		bssalign = 0, tlsalign = 0;
1149e64d0ff9SAli Bahrami 	Boolean		need_bss, need_tlsbss;
11507c478bd9Sstevel@tonic-gate 	Xword		bsssize = 0, tlssize = 0;
1151ba2be530Sab #if	defined(_ELF64)
115254d82594Sseizo 	Xword		lbssalign = 0, lbsssize = 0;
1153e64d0ff9SAli Bahrami 	Boolean		need_lbss;
115454d82594Sseizo #endif
1155*08278a5eSRod Evans 	int		ret, allow_ldynsym;
1156d579eb63Sab 	uchar_t		type;
11577c478bd9Sstevel@tonic-gate 
11582017c965SRod Evans 	DBG_CALL(Dbg_basic_validate(ofl->ofl_lml));
11592017c965SRod Evans 
1160e64d0ff9SAli Bahrami 	/*
1161e64d0ff9SAli Bahrami 	 * The need_XXX booleans are used to determine whether we need to
1162e64d0ff9SAli Bahrami 	 * create each type of bss section. We used to create these sections
1163e64d0ff9SAli Bahrami 	 * if the sum of the required sizes for each type were non-zero.
1164e64d0ff9SAli Bahrami 	 * However, it is possible for a compiler to generate COMMON variables
1165e64d0ff9SAli Bahrami 	 * of zero-length and this tricks that logic --- even zero-length
1166e64d0ff9SAli Bahrami 	 * symbols need an output section.
1167e64d0ff9SAli Bahrami 	 */
1168e64d0ff9SAli Bahrami 	need_bss = need_tlsbss = FALSE;
1169e64d0ff9SAli Bahrami #if	defined(_ELF64)
1170e64d0ff9SAli Bahrami 	need_lbss = FALSE;
1171e64d0ff9SAli Bahrami #endif
1172e64d0ff9SAli Bahrami 
11737c478bd9Sstevel@tonic-gate 	/*
11747c478bd9Sstevel@tonic-gate 	 * If a symbol is undefined and this link-edit calls for no undefined
11757c478bd9Sstevel@tonic-gate 	 * symbols to remain (this is the default case when generating an
11767c478bd9Sstevel@tonic-gate 	 * executable but can be enforced for any object using -z defs), the
11777c478bd9Sstevel@tonic-gate 	 * symbol is classified as undefined and a fatal error condition will
11787c478bd9Sstevel@tonic-gate 	 * be indicated.
11797c478bd9Sstevel@tonic-gate 	 *
11807c478bd9Sstevel@tonic-gate 	 * If the symbol is undefined and we're creating a shared object with
11817c478bd9Sstevel@tonic-gate 	 * the -Bsymbolic flag, then the symbol is also classified as undefined
11827c478bd9Sstevel@tonic-gate 	 * and a warning condition will be indicated.
11837c478bd9Sstevel@tonic-gate 	 */
11847c478bd9Sstevel@tonic-gate 	if ((oflags & (FLG_OF_SHAROBJ | FLG_OF_SYMBOLIC)) ==
11857c478bd9Sstevel@tonic-gate 	    (FLG_OF_SHAROBJ | FLG_OF_SYMBOLIC))
11867c478bd9Sstevel@tonic-gate 		undef = FLG_OF_WARN;
11877c478bd9Sstevel@tonic-gate 	if (oflags & FLG_OF_NOUNDEF)
11887c478bd9Sstevel@tonic-gate 		undef = FLG_OF_FATAL;
11897c478bd9Sstevel@tonic-gate 
11907c478bd9Sstevel@tonic-gate 	/*
11917c478bd9Sstevel@tonic-gate 	 * If the symbol is referenced from an implicitly included shared object
11927c478bd9Sstevel@tonic-gate 	 * (ie. it's not on the NEEDED list) then the symbol is also classified
11937c478bd9Sstevel@tonic-gate 	 * as undefined and a fatal error condition will be indicated.
11947c478bd9Sstevel@tonic-gate 	 */
11957c478bd9Sstevel@tonic-gate 	if ((oflags & FLG_OF_NOUNDEF) || !(oflags & FLG_OF_SHAROBJ))
11967c478bd9Sstevel@tonic-gate 		needed = FLG_OF_FATAL;
11977c478bd9Sstevel@tonic-gate 
11987c478bd9Sstevel@tonic-gate 	/*
119928bda19cSRod Evans 	 * If the output image is being versioned, then all symbol definitions
120028bda19cSRod Evans 	 * must be associated with a version.  Any symbol that isn't associated
120128bda19cSRod Evans 	 * with a version is classified as undefined, and a fatal error
120228bda19cSRod Evans 	 * condition is indicated.
12037c478bd9Sstevel@tonic-gate 	 */
12047c478bd9Sstevel@tonic-gate 	if ((oflags & FLG_OF_VERDEF) && (ofl->ofl_vercnt > VER_NDX_GLOBAL))
12057c478bd9Sstevel@tonic-gate 		verdesc = FLG_OF_FATAL;
12067c478bd9Sstevel@tonic-gate 
12079039eeafSab 	allow_ldynsym = OFL_ALLOW_LDYNSYM(ofl);
1208d579eb63Sab 
1209d579eb63Sab 	if (allow_ldynsym) {
1210d579eb63Sab 		/*
1211d579eb63Sab 		 * Normally, we disallow symbols with 0 size from appearing
1212d579eb63Sab 		 * in a dyn[sym|tls]sort section. However, there are some
1213d579eb63Sab 		 * symbols that serve special purposes that we want to exempt
1214d579eb63Sab 		 * from this rule. Look them up, and set their
1215d579eb63Sab 		 * FLG_SY_DYNSORT flag.
1216d579eb63Sab 		 */
1217d579eb63Sab 		static const char *special[] = {
1218d579eb63Sab 			MSG_ORIG(MSG_SYM_INIT_U),	/* _init */
1219d579eb63Sab 			MSG_ORIG(MSG_SYM_FINI_U),	/* _fini */
1220d579eb63Sab 			MSG_ORIG(MSG_SYM_START),	/* _start */
1221d579eb63Sab 			NULL
1222d579eb63Sab 		};
1223d579eb63Sab 		int i;
1224d579eb63Sab 
1225d579eb63Sab 		for (i = 0; special[i] != NULL; i++) {
1226d579eb63Sab 			if (((sdp = ld_sym_find(special[i],
122728bda19cSRod Evans 			    SYM_NOHASH, NULL, ofl)) != NULL) &&
1228d579eb63Sab 			    (sdp->sd_sym->st_size == 0)) {
1229d579eb63Sab 				if (ld_sym_copy(sdp) == S_ERROR)
1230d579eb63Sab 					return (S_ERROR);
1231d579eb63Sab 				sdp->sd_flags |= FLG_SY_DYNSORT;
1232d579eb63Sab 			}
1233d579eb63Sab 		}
1234d579eb63Sab 	}
1235d579eb63Sab 
12367c478bd9Sstevel@tonic-gate 	/*
12377c478bd9Sstevel@tonic-gate 	 * Collect and validate the globals from the internal symbol table.
12387c478bd9Sstevel@tonic-gate 	 */
12397c478bd9Sstevel@tonic-gate 	for (sav = avl_first(&ofl->ofl_symavl); sav;
12407c478bd9Sstevel@tonic-gate 	    sav = AVL_NEXT(&ofl->ofl_symavl, sav)) {
124160758829Srie 		Is_desc		*isp;
12420bc07c75Srie 		int		undeferr = 0;
124360758829Srie 		uchar_t		vis;
12447c478bd9Sstevel@tonic-gate 
1245635216b6SRod Evans 		sdp = sav->sav_sdp;
12467c478bd9Sstevel@tonic-gate 
12477c478bd9Sstevel@tonic-gate 		/*
12487c478bd9Sstevel@tonic-gate 		 * If undefined symbols are allowed ignore any symbols that are
12497c478bd9Sstevel@tonic-gate 		 * not needed.
12507c478bd9Sstevel@tonic-gate 		 */
12517c478bd9Sstevel@tonic-gate 		if (!(oflags & FLG_OF_NOUNDEF) &&
12527c478bd9Sstevel@tonic-gate 		    (sdp->sd_ref == REF_DYN_SEEN))
12537c478bd9Sstevel@tonic-gate 			continue;
12547c478bd9Sstevel@tonic-gate 
12557c478bd9Sstevel@tonic-gate 		/*
12567c478bd9Sstevel@tonic-gate 		 * If the symbol originates from an external or parent mapfile
12577c478bd9Sstevel@tonic-gate 		 * reference and hasn't been matched to a reference from a
12587c478bd9Sstevel@tonic-gate 		 * relocatable object, ignore it.
12597c478bd9Sstevel@tonic-gate 		 */
12607c478bd9Sstevel@tonic-gate 		if ((sdp->sd_flags & (FLG_SY_EXTERN | FLG_SY_PARENT)) &&
12617c478bd9Sstevel@tonic-gate 		    ((sdp->sd_flags & FLG_SY_MAPUSED) == 0)) {
12627c478bd9Sstevel@tonic-gate 			sdp->sd_flags |= FLG_SY_INVALID;
12637c478bd9Sstevel@tonic-gate 			continue;
12647c478bd9Sstevel@tonic-gate 		}
12657c478bd9Sstevel@tonic-gate 
12667c478bd9Sstevel@tonic-gate 		sym = sdp->sd_sym;
1267d579eb63Sab 		type = ELF_ST_TYPE(sym->st_info);
12687c478bd9Sstevel@tonic-gate 
12697c478bd9Sstevel@tonic-gate 		/*
12707c478bd9Sstevel@tonic-gate 		 * Sanity check TLS.
12717c478bd9Sstevel@tonic-gate 		 */
1272d579eb63Sab 		if ((type == STT_TLS) && (sym->st_size != 0) &&
1273d579eb63Sab 		    (sym->st_shndx != SHN_UNDEF) &&
12740bc07c75Srie 		    (sym->st_shndx != SHN_COMMON)) {
127560758829Srie 			Is_desc		*isp = sdp->sd_isc;
127660758829Srie 			Ifl_desc	*ifl = sdp->sd_file;
12777c478bd9Sstevel@tonic-gate 
12786b3ba5bdSAli Bahrami 			if ((isp == NULL) || (isp->is_shdr == NULL) ||
12797c478bd9Sstevel@tonic-gate 			    ((isp->is_shdr->sh_flags & SHF_TLS) == 0)) {
12805aefb655Srie 				eprintf(ofl->ofl_lml, ERR_FATAL,
12815aefb655Srie 				    MSG_INTL(MSG_SYM_TLS),
12827c478bd9Sstevel@tonic-gate 				    demangle(sdp->sd_name), ifl->ifl_name);
12837c478bd9Sstevel@tonic-gate 				ofl->ofl_flags |= FLG_OF_FATAL;
12847c478bd9Sstevel@tonic-gate 				continue;
12857c478bd9Sstevel@tonic-gate 			}
12867c478bd9Sstevel@tonic-gate 		}
12877c478bd9Sstevel@tonic-gate 
12887c478bd9Sstevel@tonic-gate 		if ((sdp->sd_flags & FLG_SY_VISIBLE) == 0)
12895aefb655Srie 			ld_sym_adjust_vis(sdp, ofl);
12907c478bd9Sstevel@tonic-gate 
12917c478bd9Sstevel@tonic-gate 		if ((sdp->sd_flags & FLG_SY_REDUCED) &&
12927c478bd9Sstevel@tonic-gate 		    (oflags & FLG_OF_PROCRED)) {
12935aefb655Srie 			DBG_CALL(Dbg_syms_reduce(ofl, DBG_SYM_REDUCE_GLOBAL,
12945aefb655Srie 			    sdp, 0, 0));
12957c478bd9Sstevel@tonic-gate 		}
12967c478bd9Sstevel@tonic-gate 
129760758829Srie 		/*
129860758829Srie 		 * Record any STV_SINGLETON existence.
129960758829Srie 		 */
130060758829Srie 		if ((vis = ELF_ST_VISIBILITY(sym->st_other)) == STV_SINGLETON)
130160758829Srie 			ofl->ofl_dtflags_1 |= DF_1_SINGLETON;
130260758829Srie 
13037c478bd9Sstevel@tonic-gate 		/*
13047c478bd9Sstevel@tonic-gate 		 * If building a shared object or executable, and this is a
13057c478bd9Sstevel@tonic-gate 		 * non-weak UNDEF symbol with reduced visibility (STV_*), then
13067c478bd9Sstevel@tonic-gate 		 * give a fatal error.
13077c478bd9Sstevel@tonic-gate 		 */
130860758829Srie 		if (((oflags & FLG_OF_RELOBJ) == 0) &&
13090bc07c75Srie 		    (sym->st_shndx == SHN_UNDEF) &&
13107c478bd9Sstevel@tonic-gate 		    (ELF_ST_BIND(sym->st_info) != STB_WEAK)) {
131160758829Srie 			if (vis && (vis != STV_SINGLETON)) {
131260758829Srie 				sym_undef_entry(ofl, sdp, BNDLOCAL);
131360758829Srie 				ofl->ofl_flags |= FLG_OF_FATAL;
131460758829Srie 				continue;
131560758829Srie 			}
13167c478bd9Sstevel@tonic-gate 		}
13177c478bd9Sstevel@tonic-gate 
13187c478bd9Sstevel@tonic-gate 		/*
13197c478bd9Sstevel@tonic-gate 		 * If this symbol is defined in a non-allocatable section,
13207c478bd9Sstevel@tonic-gate 		 * reduce it to local symbol.
13217c478bd9Sstevel@tonic-gate 		 */
13227c478bd9Sstevel@tonic-gate 		if (((isp = sdp->sd_isc) != 0) && isp->is_shdr &&
13237c478bd9Sstevel@tonic-gate 		    ((isp->is_shdr->sh_flags & SHF_ALLOC) == 0)) {
1324635216b6SRod Evans 			sdp->sd_flags |= (FLG_SY_REDUCED | FLG_SY_HIDDEN);
13257c478bd9Sstevel@tonic-gate 		}
13267c478bd9Sstevel@tonic-gate 
13277c478bd9Sstevel@tonic-gate 		/*
13287c478bd9Sstevel@tonic-gate 		 * If this symbol originated as a SHN_SUNW_IGNORE, it will have
13297c478bd9Sstevel@tonic-gate 		 * been processed as an SHN_UNDEF.  Return the symbol to its
13307c478bd9Sstevel@tonic-gate 		 * original index for validation, and propagation to the output
13317c478bd9Sstevel@tonic-gate 		 * file.
13327c478bd9Sstevel@tonic-gate 		 */
1333635216b6SRod Evans 		if (sdp->sd_flags & FLG_SY_IGNORE)
13340bc07c75Srie 			sdp->sd_shndx = SHN_SUNW_IGNORE;
13357c478bd9Sstevel@tonic-gate 
13367c478bd9Sstevel@tonic-gate 		if (undef) {
13377c478bd9Sstevel@tonic-gate 			/*
13383b41b08bSab 			 * If a non-weak reference remains undefined, or if a
13397c478bd9Sstevel@tonic-gate 			 * mapfile reference is not bound to the relocatable
13407c478bd9Sstevel@tonic-gate 			 * objects that make up the object being built, we have
13417c478bd9Sstevel@tonic-gate 			 * a fatal error.
13427c478bd9Sstevel@tonic-gate 			 *
13437c478bd9Sstevel@tonic-gate 			 * The exceptions are symbols which are defined to be
13447c478bd9Sstevel@tonic-gate 			 * found in the parent (FLG_SY_PARENT), which is really
13457c478bd9Sstevel@tonic-gate 			 * only meaningful for direct binding, or are defined
13467c478bd9Sstevel@tonic-gate 			 * external (FLG_SY_EXTERN) so as to suppress -zdefs
13477c478bd9Sstevel@tonic-gate 			 * errors.
13487c478bd9Sstevel@tonic-gate 			 *
13497c478bd9Sstevel@tonic-gate 			 * Register symbols are always allowed to be UNDEF.
13507c478bd9Sstevel@tonic-gate 			 *
13517c478bd9Sstevel@tonic-gate 			 * Note that we don't include references created via -u
13527c478bd9Sstevel@tonic-gate 			 * in the same shared object binding test.  This is for
13537c478bd9Sstevel@tonic-gate 			 * backward compatibility, in that a number of archive
13547c478bd9Sstevel@tonic-gate 			 * makefile rules used -u to cause archive extraction.
13557c478bd9Sstevel@tonic-gate 			 * These same rules have been cut and pasted to apply
13567c478bd9Sstevel@tonic-gate 			 * to shared objects, and thus although the -u reference
13577c478bd9Sstevel@tonic-gate 			 * is redundant, flagging it as fatal could cause some
13587c478bd9Sstevel@tonic-gate 			 * build to fail.  Also we have documented the use of
13597c478bd9Sstevel@tonic-gate 			 * -u as a mechanism to cause binding to weak version
13607c478bd9Sstevel@tonic-gate 			 * definitions, thus giving users an error condition
13617c478bd9Sstevel@tonic-gate 			 * would be incorrect.
13627c478bd9Sstevel@tonic-gate 			 */
13637c478bd9Sstevel@tonic-gate 			if (!(sdp->sd_flags & FLG_SY_REGSYM) &&
13640bc07c75Srie 			    ((sym->st_shndx == SHN_UNDEF) &&
13657c478bd9Sstevel@tonic-gate 			    ((ELF_ST_BIND(sym->st_info) != STB_WEAK) &&
13667c478bd9Sstevel@tonic-gate 			    ((sdp->sd_flags &
13677c478bd9Sstevel@tonic-gate 			    (FLG_SY_PARENT | FLG_SY_EXTERN)) == 0)) ||
1368635216b6SRod Evans 			    ((sdp->sd_flags &
1369635216b6SRod Evans 			    (FLG_SY_MAPREF | FLG_SY_MAPUSED | FLG_SY_HIDDEN |
1370635216b6SRod Evans 			    FLG_SY_PROTECT)) == FLG_SY_MAPREF))) {
13715aefb655Srie 				sym_undef_entry(ofl, sdp, UNDEF);
13727c478bd9Sstevel@tonic-gate 				ofl->ofl_flags |= undef;
13737c478bd9Sstevel@tonic-gate 				undeferr = 1;
13747c478bd9Sstevel@tonic-gate 			}
13757c478bd9Sstevel@tonic-gate 
13767c478bd9Sstevel@tonic-gate 		} else {
13777c478bd9Sstevel@tonic-gate 			/*
13787c478bd9Sstevel@tonic-gate 			 * For building things like shared objects (or anything
13797c478bd9Sstevel@tonic-gate 			 * -znodefs), undefined symbols are allowed.
13807c478bd9Sstevel@tonic-gate 			 *
13817c478bd9Sstevel@tonic-gate 			 * If a mapfile reference remains undefined the user
13827c478bd9Sstevel@tonic-gate 			 * would probably like a warning at least (they've
13837c478bd9Sstevel@tonic-gate 			 * usually mis-spelt the reference).  Refer to the above
13847c478bd9Sstevel@tonic-gate 			 * comments for discussion on -u references, which
13857c478bd9Sstevel@tonic-gate 			 * are not tested for in the same manner.
13867c478bd9Sstevel@tonic-gate 			 */
13877c478bd9Sstevel@tonic-gate 			if ((sdp->sd_flags &
13887c478bd9Sstevel@tonic-gate 			    (FLG_SY_MAPREF | FLG_SY_MAPUSED)) ==
13897c478bd9Sstevel@tonic-gate 			    FLG_SY_MAPREF) {
13905aefb655Srie 				sym_undef_entry(ofl, sdp, UNDEF);
13917c478bd9Sstevel@tonic-gate 				ofl->ofl_flags |= FLG_OF_WARN;
13927c478bd9Sstevel@tonic-gate 				undeferr = 1;
13937c478bd9Sstevel@tonic-gate 			}
13947c478bd9Sstevel@tonic-gate 		}
13957c478bd9Sstevel@tonic-gate 
13967c478bd9Sstevel@tonic-gate 		/*
13977c478bd9Sstevel@tonic-gate 		 * If this symbol comes from a dependency mark the dependency
13987c478bd9Sstevel@tonic-gate 		 * as required (-z ignore can result in unused dependencies
13997c478bd9Sstevel@tonic-gate 		 * being dropped).  If we need to record dependency versioning
14007c478bd9Sstevel@tonic-gate 		 * information indicate what version of the needed shared object
14017c478bd9Sstevel@tonic-gate 		 * this symbol is part of.  Flag the symbol as undefined if it
14027c478bd9Sstevel@tonic-gate 		 * has not been made available to us.
14037c478bd9Sstevel@tonic-gate 		 */
14047c478bd9Sstevel@tonic-gate 		if ((sdp->sd_ref == REF_DYN_NEED) &&
14057c478bd9Sstevel@tonic-gate 		    (!(sdp->sd_flags & FLG_SY_REFRSD))) {
14067c478bd9Sstevel@tonic-gate 			sdp->sd_file->ifl_flags |= FLG_IF_DEPREQD;
14077c478bd9Sstevel@tonic-gate 
14087c478bd9Sstevel@tonic-gate 			/*
14097c478bd9Sstevel@tonic-gate 			 * Capture that we've bound to a symbol that doesn't
14107c478bd9Sstevel@tonic-gate 			 * allow being directly bound to.
14117c478bd9Sstevel@tonic-gate 			 */
1412635216b6SRod Evans 			if (sdp->sd_flags & FLG_SY_NDIR)
141328bda19cSRod Evans 				ofl->ofl_flags1 |= FLG_OF1_NGLBDIR;
14147c478bd9Sstevel@tonic-gate 
14157c478bd9Sstevel@tonic-gate 			if (sdp->sd_file->ifl_vercnt) {
14167c478bd9Sstevel@tonic-gate 				int		vndx;
141760758829Srie 				Ver_index	*vip;
14187c478bd9Sstevel@tonic-gate 
14197c478bd9Sstevel@tonic-gate 				vndx = sdp->sd_aux->sa_dverndx;
14207c478bd9Sstevel@tonic-gate 				vip = &sdp->sd_file->ifl_verndx[vndx];
14217c478bd9Sstevel@tonic-gate 				if (vip->vi_flags & FLG_VER_AVAIL) {
14227c478bd9Sstevel@tonic-gate 					vip->vi_flags |= FLG_VER_REFER;
14237c478bd9Sstevel@tonic-gate 				} else {
14245aefb655Srie 					sym_undef_entry(ofl, sdp, NOTAVAIL);
14257c478bd9Sstevel@tonic-gate 					ofl->ofl_flags |= FLG_OF_FATAL;
14267c478bd9Sstevel@tonic-gate 					continue;
14277c478bd9Sstevel@tonic-gate 				}
14287c478bd9Sstevel@tonic-gate 			}
14297c478bd9Sstevel@tonic-gate 		}
14307c478bd9Sstevel@tonic-gate 
14317c478bd9Sstevel@tonic-gate 		/*
14327c478bd9Sstevel@tonic-gate 		 * Test that we do not bind to symbol supplied from an implicit
14337c478bd9Sstevel@tonic-gate 		 * shared object.  If a binding is from a weak reference it can
14347c478bd9Sstevel@tonic-gate 		 * be ignored.
14357c478bd9Sstevel@tonic-gate 		 */
14367c478bd9Sstevel@tonic-gate 		if (needed && !undeferr && (sdp->sd_flags & FLG_SY_GLOBREF) &&
14377c478bd9Sstevel@tonic-gate 		    (sdp->sd_ref == REF_DYN_NEED) &&
14387c478bd9Sstevel@tonic-gate 		    (sdp->sd_flags & FLG_SY_NOTAVAIL)) {
14395aefb655Srie 			sym_undef_entry(ofl, sdp, IMPLICIT);
14407c478bd9Sstevel@tonic-gate 			ofl->ofl_flags |= needed;
14417c478bd9Sstevel@tonic-gate 			continue;
14427c478bd9Sstevel@tonic-gate 		}
14437c478bd9Sstevel@tonic-gate 
14447c478bd9Sstevel@tonic-gate 		/*
14457c478bd9Sstevel@tonic-gate 		 * Test that a symbol isn't going to be reduced to local scope
14467c478bd9Sstevel@tonic-gate 		 * which actually wants to bind to a shared object - if so it's
14477c478bd9Sstevel@tonic-gate 		 * a fatal error.
14487c478bd9Sstevel@tonic-gate 		 */
14497c478bd9Sstevel@tonic-gate 		if ((sdp->sd_ref == REF_DYN_NEED) &&
1450635216b6SRod Evans 		    (sdp->sd_flags & (FLG_SY_HIDDEN | FLG_SY_PROTECT))) {
14515aefb655Srie 			sym_undef_entry(ofl, sdp, BNDLOCAL);
14527c478bd9Sstevel@tonic-gate 			ofl->ofl_flags |= FLG_OF_FATAL;
14537c478bd9Sstevel@tonic-gate 			continue;
14547c478bd9Sstevel@tonic-gate 		}
14557c478bd9Sstevel@tonic-gate 
14567c478bd9Sstevel@tonic-gate 		/*
14577c478bd9Sstevel@tonic-gate 		 * If the output image is to be versioned then all symbol
145828bda19cSRod Evans 		 * definitions must be associated with a version.  Remove any
145928bda19cSRod Evans 		 * versioning that might be left associated with an undefined
146028bda19cSRod Evans 		 * symbol.
14617c478bd9Sstevel@tonic-gate 		 */
146228bda19cSRod Evans 		if (verdesc && (sdp->sd_ref == REF_REL_NEED)) {
146328bda19cSRod Evans 			if (sym->st_shndx == SHN_UNDEF) {
146428bda19cSRod Evans 				if (sdp->sd_aux && sdp->sd_aux->sa_overndx)
146528bda19cSRod Evans 					sdp->sd_aux->sa_overndx = 0;
146628bda19cSRod Evans 			} else {
1467*08278a5eSRod Evans 				if (!SYM_IS_HIDDEN(sdp) && sdp->sd_aux &&
146828bda19cSRod Evans 				    (sdp->sd_aux->sa_overndx == 0)) {
146928bda19cSRod Evans 					sym_undef_entry(ofl, sdp, NOVERSION);
147028bda19cSRod Evans 					ofl->ofl_flags |= verdesc;
147128bda19cSRod Evans 					continue;
147228bda19cSRod Evans 				}
147328bda19cSRod Evans 			}
14747c478bd9Sstevel@tonic-gate 		}
14757c478bd9Sstevel@tonic-gate 
14767c478bd9Sstevel@tonic-gate 		/*
14777c478bd9Sstevel@tonic-gate 		 * If we don't need the symbol there's no need to process it
14787c478bd9Sstevel@tonic-gate 		 * any further.
14797c478bd9Sstevel@tonic-gate 		 */
14807c478bd9Sstevel@tonic-gate 		if (sdp->sd_ref == REF_DYN_SEEN)
14817c478bd9Sstevel@tonic-gate 			continue;
14827c478bd9Sstevel@tonic-gate 
14837c478bd9Sstevel@tonic-gate 		/*
14847c478bd9Sstevel@tonic-gate 		 * Calculate the size and alignment requirements for the global
14857c478bd9Sstevel@tonic-gate 		 * .bss and .tls sections.  If we're building a relocatable
14867c478bd9Sstevel@tonic-gate 		 * object only account for scoped COMMON symbols (these will
14877c478bd9Sstevel@tonic-gate 		 * be converted to .bss references).
14887c478bd9Sstevel@tonic-gate 		 *
148935450702SAli Bahrami 		 * When -z nopartial is in effect, partially initialized
149035450702SAli Bahrami 		 * symbols are directed to the special .data section
149135450702SAli Bahrami 		 * created for that purpose (ofl->ofl_isparexpn).
149235450702SAli Bahrami 		 * Otherwise, partially initialized symbols go to .bss.
14937c478bd9Sstevel@tonic-gate 		 *
14947c478bd9Sstevel@tonic-gate 		 * Also refer to make_mvsections() in sunwmove.c
14957c478bd9Sstevel@tonic-gate 		 */
14960bc07c75Srie 		if ((sym->st_shndx == SHN_COMMON) &&
14970bc07c75Srie 		    (((oflags & FLG_OF_RELOBJ) == 0) ||
1498*08278a5eSRod Evans 		    (SYM_IS_HIDDEN(sdp) && (oflags & FLG_OF_PROCRED)))) {
149957ef7aa9SRod Evans 			if ((sdp->sd_move == NULL) ||
150035450702SAli Bahrami 			    ((sdp->sd_flags & FLG_SY_PAREXPN) == 0)) {
1501d579eb63Sab 				if (type != STT_TLS) {
1502e64d0ff9SAli Bahrami 					need_bss = TRUE;
1503e64d0ff9SAli Bahrami 					bsssize = (Xword)S_ROUND(bsssize,
1504e64d0ff9SAli Bahrami 					    sym->st_value) + sym->st_size;
1505e64d0ff9SAli Bahrami 					if (sym->st_value > bssalign)
1506e64d0ff9SAli Bahrami 						bssalign = sym->st_value;
15077c478bd9Sstevel@tonic-gate 				} else {
1508e64d0ff9SAli Bahrami 					need_tlsbss = TRUE;
1509e64d0ff9SAli Bahrami 					tlssize = (Xword)S_ROUND(tlssize,
1510e64d0ff9SAli Bahrami 					    sym->st_value) + sym->st_size;
1511e64d0ff9SAli Bahrami 					if (sym->st_value > tlsalign)
1512e64d0ff9SAli Bahrami 						tlsalign = sym->st_value;
15137c478bd9Sstevel@tonic-gate 				}
15147c478bd9Sstevel@tonic-gate 			}
15157c478bd9Sstevel@tonic-gate 		}
15167c478bd9Sstevel@tonic-gate 
1517ba2be530Sab #if	defined(_ELF64)
151854d82594Sseizo 		/*
151954d82594Sseizo 		 * Calculate the size and alignment requirement for the global
152054d82594Sseizo 		 * .lbss. TLS or partially initialized symbols do not need to be
152154d82594Sseizo 		 * considered yet.
152254d82594Sseizo 		 */
1523ba2be530Sab 		if ((ld_targ.t_m.m_mach == EM_AMD64) &&
1524ba2be530Sab 		    (sym->st_shndx == SHN_X86_64_LCOMMON)) {
1525e64d0ff9SAli Bahrami 			need_lbss = TRUE;
152654d82594Sseizo 			lbsssize = (Xword)S_ROUND(lbsssize, sym->st_value) +
152733eb6ee1Sab 			    sym->st_size;
152854d82594Sseizo 			if (sym->st_value > lbssalign)
152954d82594Sseizo 				lbssalign = sym->st_value;
153054d82594Sseizo 		}
153154d82594Sseizo #endif
15327c478bd9Sstevel@tonic-gate 		/*
15337c478bd9Sstevel@tonic-gate 		 * If a symbol was referenced via the command line
15347c478bd9Sstevel@tonic-gate 		 * (ld -u <>, ...), then this counts as a reference against the
15357c478bd9Sstevel@tonic-gate 		 * symbol. Mark any section that symbol is defined in.
15367c478bd9Sstevel@tonic-gate 		 */
15377c478bd9Sstevel@tonic-gate 		if (((isp = sdp->sd_isc) != 0) &&
15387c478bd9Sstevel@tonic-gate 		    (sdp->sd_flags & FLG_SY_CMDREF)) {
15397c478bd9Sstevel@tonic-gate 			isp->is_flags |= FLG_IS_SECTREF;
15407c478bd9Sstevel@tonic-gate 			isp->is_file->ifl_flags |= FLG_IF_FILEREF;
15417c478bd9Sstevel@tonic-gate 		}
15427c478bd9Sstevel@tonic-gate 
15437c478bd9Sstevel@tonic-gate 		/*
15447c478bd9Sstevel@tonic-gate 		 * Update the symbol count and the associated name string size.
1545*08278a5eSRod Evans 		 * Note, a capabilities symbol must remain as visible as a
1546*08278a5eSRod Evans 		 * global symbol.  However, the runtime linker recognizes the
1547*08278a5eSRod Evans 		 * hidden requirement and ensures the symbol isn't made globally
1548*08278a5eSRod Evans 		 * available at runtime.
15497c478bd9Sstevel@tonic-gate 		 */
1550*08278a5eSRod Evans 		if (SYM_IS_HIDDEN(sdp) && (oflags & FLG_OF_PROCRED)) {
15517c478bd9Sstevel@tonic-gate 			/*
155260758829Srie 			 * If any reductions are being processed, keep a count
155360758829Srie 			 * of eliminated symbols, and if the symbol is being
155460758829Srie 			 * reduced to local, count it's size for the .symtab.
15557c478bd9Sstevel@tonic-gate 			 */
1556635216b6SRod Evans 			if (sdp->sd_flags & FLG_SY_ELIM) {
15577c478bd9Sstevel@tonic-gate 				ofl->ofl_elimcnt++;
15587c478bd9Sstevel@tonic-gate 			} else {
15597c478bd9Sstevel@tonic-gate 				ofl->ofl_scopecnt++;
15607c478bd9Sstevel@tonic-gate 				if ((((sdp->sd_flags & FLG_SY_REGSYM) == 0) ||
15617c478bd9Sstevel@tonic-gate 				    sym->st_name) && (st_insert(ofl->ofl_strtab,
15627c478bd9Sstevel@tonic-gate 				    sdp->sd_name) == -1))
15637c478bd9Sstevel@tonic-gate 					return (S_ERROR);
15649039eeafSab 				if (allow_ldynsym && sym->st_name &&
1565d579eb63Sab 				    ldynsym_symtype[type]) {
15669039eeafSab 					ofl->ofl_dynscopecnt++;
15679039eeafSab 					if (st_insert(ofl->ofl_dynstrtab,
15689039eeafSab 					    sdp->sd_name) == -1)
15699039eeafSab 						return (S_ERROR);
1570d579eb63Sab 					/* Include it in sort section? */
1571d579eb63Sab 					DYNSORT_COUNT(sdp, sym, type, ++);
15729039eeafSab 				}
15737c478bd9Sstevel@tonic-gate 			}
15747c478bd9Sstevel@tonic-gate 		} else {
15757c478bd9Sstevel@tonic-gate 			ofl->ofl_globcnt++;
15767c478bd9Sstevel@tonic-gate 
1577d579eb63Sab 			/*
1578635216b6SRod Evans 			 * Check to see if this global variable should go into
1579635216b6SRod Evans 			 * a sort section. Sort sections require a
1580635216b6SRod Evans 			 * .SUNW_ldynsym section, so, don't check unless a
1581635216b6SRod Evans 			 * .SUNW_ldynsym is allowed.
1582d579eb63Sab 			 */
1583635216b6SRod Evans 			if (allow_ldynsym)
1584d579eb63Sab 				DYNSORT_COUNT(sdp, sym, type, ++);
1585d579eb63Sab 
15867c478bd9Sstevel@tonic-gate 			/*
15877c478bd9Sstevel@tonic-gate 			 * If global direct bindings are in effect, or this
15887c478bd9Sstevel@tonic-gate 			 * symbol has bound to a dependency which was specified
15897c478bd9Sstevel@tonic-gate 			 * as requiring direct bindings, and it hasn't
15907c478bd9Sstevel@tonic-gate 			 * explicitly been defined as a non-direct binding
15917c478bd9Sstevel@tonic-gate 			 * symbol, mark it.
15927c478bd9Sstevel@tonic-gate 			 */
15937c478bd9Sstevel@tonic-gate 			if (((ofl->ofl_dtflags_1 & DF_1_DIRECT) || (isp &&
15947c478bd9Sstevel@tonic-gate 			    (isp->is_file->ifl_flags & FLG_IF_DIRECT))) &&
1595635216b6SRod Evans 			    ((sdp->sd_flags & FLG_SY_NDIR) == 0))
1596635216b6SRod Evans 				sdp->sd_flags |= FLG_SY_DIR;
15977c478bd9Sstevel@tonic-gate 
15987c478bd9Sstevel@tonic-gate 			/*
15997c478bd9Sstevel@tonic-gate 			 * Insert the symbol name.
16007c478bd9Sstevel@tonic-gate 			 */
16017c478bd9Sstevel@tonic-gate 			if (((sdp->sd_flags & FLG_SY_REGSYM) == 0) ||
16027c478bd9Sstevel@tonic-gate 			    sym->st_name) {
16037c478bd9Sstevel@tonic-gate 				if (st_insert(ofl->ofl_strtab,
16047c478bd9Sstevel@tonic-gate 				    sdp->sd_name) == -1)
16057c478bd9Sstevel@tonic-gate 					return (S_ERROR);
16067c478bd9Sstevel@tonic-gate 
16077c478bd9Sstevel@tonic-gate 				if (!(ofl->ofl_flags & FLG_OF_RELOBJ) &&
16087c478bd9Sstevel@tonic-gate 				    (st_insert(ofl->ofl_dynstrtab,
16097c478bd9Sstevel@tonic-gate 				    sdp->sd_name) == -1))
16107c478bd9Sstevel@tonic-gate 					return (S_ERROR);
16117c478bd9Sstevel@tonic-gate 			}
16127c478bd9Sstevel@tonic-gate 
16137c478bd9Sstevel@tonic-gate 			/*
16147c478bd9Sstevel@tonic-gate 			 * If this section offers a global symbol - record that
16157c478bd9Sstevel@tonic-gate 			 * fact.
16167c478bd9Sstevel@tonic-gate 			 */
16177c478bd9Sstevel@tonic-gate 			if (isp) {
16187c478bd9Sstevel@tonic-gate 				isp->is_flags |= FLG_IS_SECTREF;
16197c478bd9Sstevel@tonic-gate 				isp->is_file->ifl_flags |= FLG_IF_FILEREF;
16207c478bd9Sstevel@tonic-gate 			}
16217c478bd9Sstevel@tonic-gate 		}
16227c478bd9Sstevel@tonic-gate 	}
16237c478bd9Sstevel@tonic-gate 
16247c478bd9Sstevel@tonic-gate 	/*
16257c478bd9Sstevel@tonic-gate 	 * If we've encountered a fatal error during symbol validation then
16267c478bd9Sstevel@tonic-gate 	 * return now.
16277c478bd9Sstevel@tonic-gate 	 */
16287c478bd9Sstevel@tonic-gate 	if (ofl->ofl_flags & FLG_OF_FATAL)
16297c478bd9Sstevel@tonic-gate 		return (1);
16307c478bd9Sstevel@tonic-gate 
16317c478bd9Sstevel@tonic-gate 	/*
16327c478bd9Sstevel@tonic-gate 	 * Now that symbol resolution is completed, scan any register symbols.
16337c478bd9Sstevel@tonic-gate 	 * From now on, we're only interested in those that contribute to the
16347c478bd9Sstevel@tonic-gate 	 * output file.
16357c478bd9Sstevel@tonic-gate 	 */
16367c478bd9Sstevel@tonic-gate 	if (ofl->ofl_regsyms) {
16377c478bd9Sstevel@tonic-gate 		int	ndx;
16387c478bd9Sstevel@tonic-gate 
16397c478bd9Sstevel@tonic-gate 		for (ndx = 0; ndx < ofl->ofl_regsymsno; ndx++) {
16406b3ba5bdSAli Bahrami 			if ((sdp = ofl->ofl_regsyms[ndx]) == NULL)
16417c478bd9Sstevel@tonic-gate 				continue;
16427c478bd9Sstevel@tonic-gate 			if (sdp->sd_ref != REF_REL_NEED) {
16436b3ba5bdSAli Bahrami 				ofl->ofl_regsyms[ndx] = NULL;
16447c478bd9Sstevel@tonic-gate 				continue;
16457c478bd9Sstevel@tonic-gate 			}
16467c478bd9Sstevel@tonic-gate 
16477c478bd9Sstevel@tonic-gate 			ofl->ofl_regsymcnt++;
16487c478bd9Sstevel@tonic-gate 			if (sdp->sd_sym->st_name == 0)
16497c478bd9Sstevel@tonic-gate 				sdp->sd_name = MSG_ORIG(MSG_STR_EMPTY);
16507c478bd9Sstevel@tonic-gate 
1651*08278a5eSRod Evans 			if (SYM_IS_HIDDEN(sdp) ||
16527c478bd9Sstevel@tonic-gate 			    (ELF_ST_BIND(sdp->sd_sym->st_info) == STB_LOCAL))
16537c478bd9Sstevel@tonic-gate 				ofl->ofl_lregsymcnt++;
16547c478bd9Sstevel@tonic-gate 		}
16557c478bd9Sstevel@tonic-gate 	}
16567c478bd9Sstevel@tonic-gate 
16577c478bd9Sstevel@tonic-gate 	/*
16587c478bd9Sstevel@tonic-gate 	 * Generate the .bss section now that we know its size and alignment.
16597c478bd9Sstevel@tonic-gate 	 */
1660e64d0ff9SAli Bahrami 	if (need_bss) {
166157ef7aa9SRod Evans 		if (ld_make_bss(ofl, bsssize, bssalign,
166257ef7aa9SRod Evans 		    ld_targ.t_id.id_bss) == S_ERROR)
16637c478bd9Sstevel@tonic-gate 			return (S_ERROR);
16647c478bd9Sstevel@tonic-gate 	}
1665e64d0ff9SAli Bahrami 	if (need_tlsbss) {
166657ef7aa9SRod Evans 		if (ld_make_bss(ofl, tlssize, tlsalign,
166757ef7aa9SRod Evans 		    ld_targ.t_id.id_tlsbss) == S_ERROR)
166854d82594Sseizo 			return (S_ERROR);
166954d82594Sseizo 	}
1670ba2be530Sab #if	defined(_ELF64)
1671ba2be530Sab 	if ((ld_targ.t_m.m_mach == EM_AMD64) &&
1672e64d0ff9SAli Bahrami 	    need_lbss && !(oflags & FLG_OF_RELOBJ)) {
167357ef7aa9SRod Evans 		if (ld_make_bss(ofl, lbsssize, lbssalign,
167457ef7aa9SRod Evans 		    ld_targ.t_id.id_lbss) == S_ERROR)
16757c478bd9Sstevel@tonic-gate 			return (S_ERROR);
16767c478bd9Sstevel@tonic-gate 	}
167754d82594Sseizo #endif
16787c478bd9Sstevel@tonic-gate 	/*
16797c478bd9Sstevel@tonic-gate 	 * Determine what entry point symbol we need, and if found save its
16807c478bd9Sstevel@tonic-gate 	 * symbol descriptor so that we can update the ELF header entry with the
16817c478bd9Sstevel@tonic-gate 	 * symbols value later (see update_oehdr).  Make sure the symbol is
1682c1c6f601Srie 	 * tagged to ensure its update in case -s is in effect.  Use any -e
16837c478bd9Sstevel@tonic-gate 	 * option first, or the default entry points `_start' and `main'.
16847c478bd9Sstevel@tonic-gate 	 */
1685c1c6f601Srie 	ret = 0;
16867c478bd9Sstevel@tonic-gate 	if (ofl->ofl_entry) {
168728bda19cSRod Evans 		if ((sdp = ld_sym_find(ofl->ofl_entry, SYM_NOHASH,
168828bda19cSRod Evans 		    NULL, ofl)) == NULL) {
16891d1fba8aSrie 			eprintf(ofl->ofl_lml, ERR_FATAL,
16901d1fba8aSrie 			    MSG_INTL(MSG_ARG_NOENTRY), ofl->ofl_entry);
16911d1fba8aSrie 			ret++;
16921d1fba8aSrie 		} else if (ensure_sym_local(ofl, sdp,
16931d1fba8aSrie 		    MSG_INTL(MSG_SYM_ENTRY)) != 0) {
1694c1c6f601Srie 			ret++;
16951d1fba8aSrie 		} else {
16961d1fba8aSrie 			ofl->ofl_entry = (void *)sdp;
16977c478bd9Sstevel@tonic-gate 		}
16985aefb655Srie 	} else if (((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_START),
169928bda19cSRod Evans 	    SYM_NOHASH, NULL, ofl)) != NULL) && (ensure_sym_local(ofl,
1700c1c6f601Srie 	    sdp, 0) == 0)) {
17017c478bd9Sstevel@tonic-gate 		ofl->ofl_entry = (void *)sdp;
1702c1c6f601Srie 
17035aefb655Srie 	} else if (((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_MAIN),
170428bda19cSRod Evans 	    SYM_NOHASH, NULL, ofl)) != NULL) && (ensure_sym_local(ofl,
1705c1c6f601Srie 	    sdp, 0) == 0)) {
17067c478bd9Sstevel@tonic-gate 		ofl->ofl_entry = (void *)sdp;
17077c478bd9Sstevel@tonic-gate 	}
17087c478bd9Sstevel@tonic-gate 
17097c478bd9Sstevel@tonic-gate 	/*
1710c1c6f601Srie 	 * If ld -zdtrace=<sym> was given, then validate that the symbol is
1711c1c6f601Srie 	 * defined within the current object being built.
17127c478bd9Sstevel@tonic-gate 	 */
17131d1fba8aSrie 	if ((sdp = ofl->ofl_dtracesym) != 0)
1714c1c6f601Srie 		ret += ensure_sym_local(ofl, sdp, MSG_ORIG(MSG_STR_DTRACE));
17157c478bd9Sstevel@tonic-gate 
1716c1c6f601Srie 	/*
1717c1c6f601Srie 	 * If any initarray, finiarray or preinitarray functions have been
1718c1c6f601Srie 	 * requested, make sure they are defined within the current object
1719c1c6f601Srie 	 * being built.
1720c1c6f601Srie 	 */
172157ef7aa9SRod Evans 	if (ofl->ofl_initarray) {
172257ef7aa9SRod Evans 		ret += ensure_array_local(ofl, ofl->ofl_initarray,
1723c1c6f601Srie 		    MSG_ORIG(MSG_SYM_INITARRAY));
1724c1c6f601Srie 	}
172557ef7aa9SRod Evans 	if (ofl->ofl_finiarray) {
172657ef7aa9SRod Evans 		ret += ensure_array_local(ofl, ofl->ofl_finiarray,
1727c1c6f601Srie 		    MSG_ORIG(MSG_SYM_FINIARRAY));
1728c1c6f601Srie 	}
172957ef7aa9SRod Evans 	if (ofl->ofl_preiarray) {
173057ef7aa9SRod Evans 		ret += ensure_array_local(ofl, ofl->ofl_preiarray,
1731c1c6f601Srie 		    MSG_ORIG(MSG_SYM_PREINITARRAY));
1732c1c6f601Srie 	}
1733c1c6f601Srie 
1734c1c6f601Srie 	if (ret)
1735c1c6f601Srie 		return (S_ERROR);
1736c1c6f601Srie 
17377c478bd9Sstevel@tonic-gate 	/*
17387c478bd9Sstevel@tonic-gate 	 * If we're required to record any needed dependencies versioning
17397c478bd9Sstevel@tonic-gate 	 * information calculate it now that all symbols have been validated.
17407c478bd9Sstevel@tonic-gate 	 */
17417c478bd9Sstevel@tonic-gate 	if ((oflags & (FLG_OF_VERNEED | FLG_OF_NOVERSEC)) == FLG_OF_VERNEED)
17425aefb655Srie 		return (ld_vers_check_need(ofl));
17437c478bd9Sstevel@tonic-gate 	else
17447c478bd9Sstevel@tonic-gate 		return (1);
17457c478bd9Sstevel@tonic-gate }
17467c478bd9Sstevel@tonic-gate 
17477c478bd9Sstevel@tonic-gate /*
17487c478bd9Sstevel@tonic-gate  * qsort(3c) comparison function.  As an optimization for associating weak
17497c478bd9Sstevel@tonic-gate  * symbols to their strong counterparts sort global symbols according to their
17506b3ba5bdSAli Bahrami  * section index, address and binding.
17517c478bd9Sstevel@tonic-gate  */
17527c478bd9Sstevel@tonic-gate static int
17536b3ba5bdSAli Bahrami compare(const void *sdpp1, const void *sdpp2)
17547c478bd9Sstevel@tonic-gate {
17556b3ba5bdSAli Bahrami 	Sym_desc	*sdp1 = *((Sym_desc **)sdpp1);
17566b3ba5bdSAli Bahrami 	Sym_desc	*sdp2 = *((Sym_desc **)sdpp2);
17576b3ba5bdSAli Bahrami 	Sym		*sym1, *sym2;
17587c478bd9Sstevel@tonic-gate 	uchar_t		bind1, bind2;
17597c478bd9Sstevel@tonic-gate 
17607c478bd9Sstevel@tonic-gate 	/*
17617c478bd9Sstevel@tonic-gate 	 * Symbol descriptors may be zero, move these to the front of the
17627c478bd9Sstevel@tonic-gate 	 * sorted array.
17637c478bd9Sstevel@tonic-gate 	 */
17646b3ba5bdSAli Bahrami 	if (sdp1 == NULL)
17657c478bd9Sstevel@tonic-gate 		return (-1);
17666b3ba5bdSAli Bahrami 	if (sdp2 == NULL)
17677c478bd9Sstevel@tonic-gate 		return (1);
17687c478bd9Sstevel@tonic-gate 
17697c478bd9Sstevel@tonic-gate 	sym1 = sdp1->sd_sym;
17707c478bd9Sstevel@tonic-gate 	sym2 = sdp2->sd_sym;
17717c478bd9Sstevel@tonic-gate 
17726b3ba5bdSAli Bahrami 	/*
17736b3ba5bdSAli Bahrami 	 * Compare the symbols section index.  This is important when sorting
17746b3ba5bdSAli Bahrami 	 * the symbol tables of relocatable objects.  In this case, a symbols
17756b3ba5bdSAli Bahrami 	 * value is the offset within the associated section, and thus many
17766b3ba5bdSAli Bahrami 	 * symbols can have the same value, but are effectively different
17776b3ba5bdSAli Bahrami 	 * addresses.
17786b3ba5bdSAli Bahrami 	 */
17796b3ba5bdSAli Bahrami 	if (sym1->st_shndx > sym2->st_shndx)
17806b3ba5bdSAli Bahrami 		return (1);
17816b3ba5bdSAli Bahrami 	if (sym1->st_shndx < sym2->st_shndx)
17826b3ba5bdSAli Bahrami 		return (-1);
17836b3ba5bdSAli Bahrami 
17847c478bd9Sstevel@tonic-gate 	/*
17857c478bd9Sstevel@tonic-gate 	 * Compare the symbols value (address).
17867c478bd9Sstevel@tonic-gate 	 */
17877c478bd9Sstevel@tonic-gate 	if (sym1->st_value > sym2->st_value)
17887c478bd9Sstevel@tonic-gate 		return (1);
17897c478bd9Sstevel@tonic-gate 	if (sym1->st_value < sym2->st_value)
17907c478bd9Sstevel@tonic-gate 		return (-1);
17917c478bd9Sstevel@tonic-gate 
17927c478bd9Sstevel@tonic-gate 	bind1 = ELF_ST_BIND(sym1->st_info);
17937c478bd9Sstevel@tonic-gate 	bind2 = ELF_ST_BIND(sym2->st_info);
17947c478bd9Sstevel@tonic-gate 
17957c478bd9Sstevel@tonic-gate 	/*
17967c478bd9Sstevel@tonic-gate 	 * If two symbols have the same address place the weak symbol before
17977c478bd9Sstevel@tonic-gate 	 * any strong counterpart.
17987c478bd9Sstevel@tonic-gate 	 */
17997c478bd9Sstevel@tonic-gate 	if (bind1 > bind2)
18007c478bd9Sstevel@tonic-gate 		return (-1);
18017c478bd9Sstevel@tonic-gate 	if (bind1 < bind2)
18027c478bd9Sstevel@tonic-gate 		return (1);
18037c478bd9Sstevel@tonic-gate 
18047c478bd9Sstevel@tonic-gate 	return (0);
18057c478bd9Sstevel@tonic-gate }
18067c478bd9Sstevel@tonic-gate 
180769a0bf0cSab /*
180869a0bf0cSab  * Issue a MSG_SYM_BADADDR error from ld_sym_process(). This error
180969a0bf0cSab  * is issued when a symbol address/size is not contained by the
181069a0bf0cSab  * target section.
181169a0bf0cSab  *
181269a0bf0cSab  * Such objects are at least partially corrupt, and the user would
181369a0bf0cSab  * be well advised to be skeptical of them, and to ask their compiler
181469a0bf0cSab  * supplier to fix the problem. However, a distinction needs to be
181569a0bf0cSab  * made between symbols that reference readonly text, and those that
181669a0bf0cSab  * access writable data. Other than throwing off profiling results,
181769a0bf0cSab  * the readonly section case is less serious. We have encountered
181869a0bf0cSab  * such objects in the field. In order to allow existing objects
181969a0bf0cSab  * to continue working, we issue a warning rather than a fatal error
182069a0bf0cSab  * if the symbol is against readonly text. Other cases are fatal.
182169a0bf0cSab  */
182269a0bf0cSab static void
182369a0bf0cSab issue_badaddr_msg(Ifl_desc *ifl, Ofl_desc *ofl, Sym_desc *sdp,
182469a0bf0cSab     Sym *sym, Word shndx)
182569a0bf0cSab {
18261d9df23bSab 	ofl_flag_t	flag;
18271d9df23bSab 	Error		err;
18281d9df23bSab 	const char	*msg;
182969a0bf0cSab 
183069a0bf0cSab 	if ((sdp->sd_isc->is_shdr->sh_flags & (SHF_WRITE | SHF_ALLOC)) ==
183169a0bf0cSab 	    SHF_ALLOC) {
183269a0bf0cSab 		msg = MSG_INTL(MSG_SYM_BADADDR_ROTXT);
183369a0bf0cSab 		flag = FLG_OF_WARN;
183469a0bf0cSab 		err = ERR_WARNING;
183569a0bf0cSab 	} else {
183669a0bf0cSab 		msg = MSG_INTL(MSG_SYM_BADADDR);
183769a0bf0cSab 		flag = FLG_OF_FATAL;
183869a0bf0cSab 		err = ERR_FATAL;
183969a0bf0cSab 	}
184069a0bf0cSab 
184169a0bf0cSab 	eprintf(ofl->ofl_lml, err, msg, demangle(sdp->sd_name),
184269a0bf0cSab 	    ifl->ifl_name, shndx, sdp->sd_isc->is_name,
184369a0bf0cSab 	    EC_XWORD(sdp->sd_isc->is_shdr->sh_size),
184469a0bf0cSab 	    EC_XWORD(sym->st_value), EC_XWORD(sym->st_size));
184569a0bf0cSab 	ofl->ofl_flags |= flag;
184669a0bf0cSab }
184769a0bf0cSab 
1848*08278a5eSRod Evans /*
1849*08278a5eSRod Evans  * Global symbols that are candidates for translation to local capability
1850*08278a5eSRod Evans  * symbols under -z symbolcap, are maintained on a local symbol list.  Once
1851*08278a5eSRod Evans  * all symbols of a file are processed, this list is traversed to cull any
1852*08278a5eSRod Evans  * unnecessary weak symbol aliases.
1853*08278a5eSRod Evans  */
1854*08278a5eSRod Evans typedef struct {
1855*08278a5eSRod Evans 	Sym_desc	*c_nsdp;	/* new lead symbol */
1856*08278a5eSRod Evans 	Sym_desc	*c_osdp;	/* original symbol */
1857*08278a5eSRod Evans 	Cap_group	*c_group;	/* symbol capability group */
1858*08278a5eSRod Evans 	Word		c_ndx;		/* symbol index */
1859*08278a5eSRod Evans } Cap_pair;
186069a0bf0cSab 
18617c478bd9Sstevel@tonic-gate /*
18627c478bd9Sstevel@tonic-gate  * Process the symbol table for the specified input file.  At this point all
18637c478bd9Sstevel@tonic-gate  * input sections from this input file have been assigned an input section
18647c478bd9Sstevel@tonic-gate  * descriptor which is saved in the `ifl_isdesc' array.
18657c478bd9Sstevel@tonic-gate  *
1866*08278a5eSRod Evans  *  -	local symbols are saved (as is) if the input file is a 	relocatable
1867*08278a5eSRod Evans  *	object
18687c478bd9Sstevel@tonic-gate  *
1869*08278a5eSRod Evans  *  -	global symbols are added to the linkers internal symbol table if they
1870*08278a5eSRod Evans  *	are not already present, otherwise a symbol resolution function is
1871*08278a5eSRod Evans  *	called upon to resolve the conflict.
18727c478bd9Sstevel@tonic-gate  */
18737c478bd9Sstevel@tonic-gate uintptr_t
18745aefb655Srie ld_sym_process(Is_desc *isc, Ifl_desc *ifl, Ofl_desc *ofl)
18757c478bd9Sstevel@tonic-gate {
187675e45495Sab 	/*
187775e45495Sab 	 * This macro tests the given symbol to see if it is out of
187875e45495Sab 	 * range relative to the section it references.
187975e45495Sab 	 *
188075e45495Sab 	 * entry:
188175e45495Sab 	 *	- ifl is a relative object (ET_REL)
188275e45495Sab 	 *	_sdp - Symbol descriptor
188375e45495Sab 	 *	_sym - Symbol
188475e45495Sab 	 *	_type - Symbol type
188575e45495Sab 	 *
188675e45495Sab 	 * The following are tested:
188775e45495Sab 	 *	- Symbol length is non-zero
188875e45495Sab 	 *	- Symbol type is a type that references code or data
188975e45495Sab 	 *	- Referenced section is not 0 (indicates an UNDEF symbol)
189075e45495Sab 	 *	  and is not in the range of special values above SHN_LORESERVE
189175e45495Sab 	 *	  (excluding SHN_XINDEX, which is OK).
189275e45495Sab 	 *	- We have a valid section header for the target section
189375e45495Sab 	 *
189475e45495Sab 	 * If the above are all true, and the symbol position is not
189575e45495Sab 	 * contained by the target section, this macro evaluates to
189675e45495Sab 	 * True (1). Otherwise, False(0).
189775e45495Sab 	 */
189875e45495Sab #define	SYM_LOC_BADADDR(_sdp, _sym, _type) \
1899d579eb63Sab 	(_sym->st_size && dynsymsort_symtype[_type] && \
190075e45495Sab 	(_sym->st_shndx != SHN_UNDEF) && \
190175e45495Sab 	((_sym->st_shndx < SHN_LORESERVE) || \
190275e45495Sab 		(_sym->st_shndx == SHN_XINDEX)) && \
190375e45495Sab 	_sdp->sd_isc && _sdp->sd_isc->is_shdr && \
190475e45495Sab 	((_sym->st_value + _sym->st_size) > _sdp->sd_isc->is_shdr->sh_size))
190575e45495Sab 
1906de777a60Sab 	Conv_inv_buf_t	inv_buf;
19077c478bd9Sstevel@tonic-gate 	Sym		*sym = (Sym *)isc->is_indata->d_buf;
19086b3ba5bdSAli Bahrami 	Word		*symshndx = NULL;
19097c478bd9Sstevel@tonic-gate 	Shdr		*shdr = isc->is_shdr;
19107c478bd9Sstevel@tonic-gate 	Sym_desc	*sdp;
19117c478bd9Sstevel@tonic-gate 	size_t		strsize;
19127c478bd9Sstevel@tonic-gate 	char		*strs;
19137c478bd9Sstevel@tonic-gate 	uchar_t		type, bind;
19147c478bd9Sstevel@tonic-gate 	Word		ndx, hash, local, total;
19154f680cc6SAli Bahrami 	uchar_t		osabi = ifl->ifl_ehdr->e_ident[EI_OSABI];
19164f680cc6SAli Bahrami 	Half		mach = ifl->ifl_ehdr->e_machine;
19177c478bd9Sstevel@tonic-gate 	Half		etype = ifl->ifl_ehdr->e_type;
191875e45495Sab 	int		etype_rel;
19197c478bd9Sstevel@tonic-gate 	const char	*symsecname, *strsecname;
19204a8d0ea7SAli Bahrami 	Word		symsecndx;
19217c478bd9Sstevel@tonic-gate 	avl_index_t	where;
19226b3ba5bdSAli Bahrami 	int		test_gnu_hidden_bit, weak;
1923*08278a5eSRod Evans 	Cap_desc	*cdp = NULL;
1924*08278a5eSRod Evans 	Alist		*cappairs = NULL;
19257c478bd9Sstevel@tonic-gate 
19267c478bd9Sstevel@tonic-gate 	/*
19277c478bd9Sstevel@tonic-gate 	 * Its possible that a file may contain more that one symbol table,
19287c478bd9Sstevel@tonic-gate 	 * ie. .dynsym and .symtab in a shared library.  Only process the first
19297c478bd9Sstevel@tonic-gate 	 * table (here, we assume .dynsym comes before .symtab).
19307c478bd9Sstevel@tonic-gate 	 */
19317c478bd9Sstevel@tonic-gate 	if (ifl->ifl_symscnt)
19327c478bd9Sstevel@tonic-gate 		return (1);
19337c478bd9Sstevel@tonic-gate 
19347c478bd9Sstevel@tonic-gate 	if (isc->is_symshndx)
19357c478bd9Sstevel@tonic-gate 		symshndx = isc->is_symshndx->is_indata->d_buf;
19367c478bd9Sstevel@tonic-gate 
19375aefb655Srie 	DBG_CALL(Dbg_syms_process(ofl->ofl_lml, ifl));
19387c478bd9Sstevel@tonic-gate 
19394a8d0ea7SAli Bahrami 	symsecndx = isc->is_scnndx;
19407c478bd9Sstevel@tonic-gate 	if (isc->is_name)
19417c478bd9Sstevel@tonic-gate 		symsecname = isc->is_name;
19427c478bd9Sstevel@tonic-gate 	else
19437c478bd9Sstevel@tonic-gate 		symsecname = MSG_ORIG(MSG_STR_EMPTY);
19447c478bd9Sstevel@tonic-gate 
19457c478bd9Sstevel@tonic-gate 	/*
19467c478bd9Sstevel@tonic-gate 	 * From the symbol tables section header information determine which
19477c478bd9Sstevel@tonic-gate 	 * strtab table is needed to locate the actual symbol names.
19487c478bd9Sstevel@tonic-gate 	 */
19497c478bd9Sstevel@tonic-gate 	if (ifl->ifl_flags & FLG_IF_HSTRTAB) {
19507c478bd9Sstevel@tonic-gate 		ndx = shdr->sh_link;
19517c478bd9Sstevel@tonic-gate 		if ((ndx == 0) || (ndx >= ifl->ifl_shnum)) {
19525aefb655Srie 			eprintf(ofl->ofl_lml, ERR_FATAL,
19534a8d0ea7SAli Bahrami 			    MSG_INTL(MSG_FIL_INVSHLINK), ifl->ifl_name,
19544a8d0ea7SAli Bahrami 			    EC_WORD(symsecndx), symsecname, EC_XWORD(ndx));
19557c478bd9Sstevel@tonic-gate 			return (S_ERROR);
19567c478bd9Sstevel@tonic-gate 		}
19577c478bd9Sstevel@tonic-gate 		strsize = ifl->ifl_isdesc[ndx]->is_shdr->sh_size;
19587c478bd9Sstevel@tonic-gate 		strs = ifl->ifl_isdesc[ndx]->is_indata->d_buf;
19597c478bd9Sstevel@tonic-gate 		if (ifl->ifl_isdesc[ndx]->is_name)
19607c478bd9Sstevel@tonic-gate 			strsecname = ifl->ifl_isdesc[ndx]->is_name;
19617c478bd9Sstevel@tonic-gate 		else
19627c478bd9Sstevel@tonic-gate 			strsecname = MSG_ORIG(MSG_STR_EMPTY);
19637c478bd9Sstevel@tonic-gate 	} else {
19647c478bd9Sstevel@tonic-gate 		/*
19657c478bd9Sstevel@tonic-gate 		 * There is no string table section in this input file
19667c478bd9Sstevel@tonic-gate 		 * although there are symbols in this symbol table section.
19677c478bd9Sstevel@tonic-gate 		 * This means that these symbols do not have names.
19687c478bd9Sstevel@tonic-gate 		 * Currently, only scratch register symbols are allowed
19697c478bd9Sstevel@tonic-gate 		 * not to have names.
19707c478bd9Sstevel@tonic-gate 		 */
19717c478bd9Sstevel@tonic-gate 		strsize = 0;
19727c478bd9Sstevel@tonic-gate 		strs = (char *)MSG_ORIG(MSG_STR_EMPTY);
19737c478bd9Sstevel@tonic-gate 		strsecname = MSG_ORIG(MSG_STR_EMPTY);
19747c478bd9Sstevel@tonic-gate 	}
19757c478bd9Sstevel@tonic-gate 
19767c478bd9Sstevel@tonic-gate 	/*
19777c478bd9Sstevel@tonic-gate 	 * Determine the number of local symbols together with the total
19787c478bd9Sstevel@tonic-gate 	 * number we have to process.
19797c478bd9Sstevel@tonic-gate 	 */
19807c478bd9Sstevel@tonic-gate 	total = (Word)(shdr->sh_size / shdr->sh_entsize);
19817c478bd9Sstevel@tonic-gate 	local = shdr->sh_info;
19827c478bd9Sstevel@tonic-gate 
19837c478bd9Sstevel@tonic-gate 	/*
19847c478bd9Sstevel@tonic-gate 	 * Allocate a symbol table index array and a local symbol array
19857c478bd9Sstevel@tonic-gate 	 * (global symbols are processed and added to the ofl->ofl_symbkt[]
19867c478bd9Sstevel@tonic-gate 	 * array).  If we are dealing with a relocatable object, allocate the
19877c478bd9Sstevel@tonic-gate 	 * local symbol descriptors.  If this isn't a relocatable object we
19887c478bd9Sstevel@tonic-gate 	 * still have to process any shared object locals to determine if any
19897c478bd9Sstevel@tonic-gate 	 * register symbols exist.  Although these aren't added to the output
19907c478bd9Sstevel@tonic-gate 	 * image, they are used as part of symbol resolution.
19917c478bd9Sstevel@tonic-gate 	 */
19927c478bd9Sstevel@tonic-gate 	if ((ifl->ifl_oldndx = libld_malloc((size_t)(total *
19936b3ba5bdSAli Bahrami 	    sizeof (Sym_desc *)))) == NULL)
19947c478bd9Sstevel@tonic-gate 		return (S_ERROR);
1995d579eb63Sab 	etype_rel = (etype == ET_REL);
199675e45495Sab 	if (etype_rel && local) {
19977c478bd9Sstevel@tonic-gate 		if ((ifl->ifl_locs =
19986b3ba5bdSAli Bahrami 		    libld_calloc(sizeof (Sym_desc), local)) == NULL)
19997c478bd9Sstevel@tonic-gate 			return (S_ERROR);
20007c478bd9Sstevel@tonic-gate 		/* LINTED */
20017c478bd9Sstevel@tonic-gate 		ifl->ifl_locscnt = (Word)local;
20027c478bd9Sstevel@tonic-gate 	}
20037c478bd9Sstevel@tonic-gate 	ifl->ifl_symscnt = total;
20047c478bd9Sstevel@tonic-gate 
20057c478bd9Sstevel@tonic-gate 	/*
20067c478bd9Sstevel@tonic-gate 	 * If there are local symbols to save add them to the symbol table
20077c478bd9Sstevel@tonic-gate 	 * index array.
20087c478bd9Sstevel@tonic-gate 	 */
20097c478bd9Sstevel@tonic-gate 	if (local) {
201024b9abbaSab 		int		allow_ldynsym = OFL_ALLOW_LDYNSYM(ofl);
201124b9abbaSab 		Sym_desc	*last_file_sdp = NULL;
201224b9abbaSab 		int		last_file_ndx = 0;
201324b9abbaSab 
20147c478bd9Sstevel@tonic-gate 		for (sym++, ndx = 1; ndx < local; sym++, ndx++) {
2015635216b6SRod Evans 			sd_flag_t	sdflags = FLG_SY_CLEAN;
2016635216b6SRod Evans 			Word		shndx;
20177c478bd9Sstevel@tonic-gate 			const char	*name;
20187c478bd9Sstevel@tonic-gate 			Sym_desc	*rsdp;
2019ba2be530Sab 			int		shndx_bad = 0;
20201da7e599SAli Bahrami 			int		symtab_enter = 1;
20217c478bd9Sstevel@tonic-gate 
20227c478bd9Sstevel@tonic-gate 			/*
2023ba2be530Sab 			 * Determine and validate the associated section index.
20247c478bd9Sstevel@tonic-gate 			 */
2025ba2be530Sab 			if (symshndx && (sym->st_shndx == SHN_XINDEX)) {
20267c478bd9Sstevel@tonic-gate 				shndx = symshndx[ndx];
2027ba2be530Sab 			} else if ((shndx = sym->st_shndx) >= SHN_LORESERVE) {
20287c478bd9Sstevel@tonic-gate 				sdflags |= FLG_SY_SPECSEC;
2029ba2be530Sab 			} else if (shndx > ifl->ifl_ehdr->e_shnum) {
2030ba2be530Sab 				/* We need the name before we can issue error */
2031ba2be530Sab 				shndx_bad = 1;
2032ba2be530Sab 			}
20337c478bd9Sstevel@tonic-gate 
20347c478bd9Sstevel@tonic-gate 			/*
20357c478bd9Sstevel@tonic-gate 			 * Check if st_name has a valid value or not.
20367c478bd9Sstevel@tonic-gate 			 */
20375aefb655Srie 			if ((name = string(ofl, ifl, sym, strs, strsize, ndx,
20384a8d0ea7SAli Bahrami 			    shndx, symsecndx, symsecname, strsecname,
20394a8d0ea7SAli Bahrami 			    &sdflags)) == NULL) {
20407c478bd9Sstevel@tonic-gate 				ofl->ofl_flags |= FLG_OF_FATAL;
20417c478bd9Sstevel@tonic-gate 				continue;
20427c478bd9Sstevel@tonic-gate 			}
20437c478bd9Sstevel@tonic-gate 
2044ba2be530Sab 			/*
2045ba2be530Sab 			 * Now that we have the name, if the section index
2046ba2be530Sab 			 * was bad, report it.
2047ba2be530Sab 			 */
2048ba2be530Sab 			if (shndx_bad) {
2049ba2be530Sab 				eprintf(ofl->ofl_lml, ERR_WARNING,
2050ba2be530Sab 				    MSG_INTL(MSG_SYM_INVSHNDX),
20514a8d0ea7SAli Bahrami 				    demangle_symname(name, symsecname, ndx),
2052ba2be530Sab 				    ifl->ifl_name,
20534f680cc6SAli Bahrami 				    conv_sym_shndx(osabi, mach, sym->st_shndx,
20544f680cc6SAli Bahrami 				    CONV_FMT_DECIMAL, &inv_buf));
2055ba2be530Sab 				continue;
2056ba2be530Sab 			}
2057ba2be530Sab 
20587c478bd9Sstevel@tonic-gate 			/*
20597c478bd9Sstevel@tonic-gate 			 * If this local symbol table originates from a shared
20607c478bd9Sstevel@tonic-gate 			 * object, then we're only interested in recording
20617c478bd9Sstevel@tonic-gate 			 * register symbols.  As local symbol descriptors aren't
20627c478bd9Sstevel@tonic-gate 			 * allocated for shared objects, one will be allocated
20637c478bd9Sstevel@tonic-gate 			 * to associated with the register symbol.  This symbol
20647c478bd9Sstevel@tonic-gate 			 * won't become part of the output image, but we must
20657c478bd9Sstevel@tonic-gate 			 * process it to test for register conflicts.
20667c478bd9Sstevel@tonic-gate 			 */
206728bda19cSRod Evans 			rsdp = sdp = NULL;
20687c478bd9Sstevel@tonic-gate 			if (sdflags & FLG_SY_REGSYM) {
2069ba2be530Sab 				/*
2070ba2be530Sab 				 * The presence of FLG_SY_REGSYM means that
2071ba2be530Sab 				 * the pointers in ld_targ.t_ms are non-NULL.
2072ba2be530Sab 				 */
2073ba2be530Sab 				rsdp = (*ld_targ.t_ms.ms_reg_find)(sym, ofl);
2074ba2be530Sab 				if (rsdp != 0) {
20757c478bd9Sstevel@tonic-gate 					/*
20767c478bd9Sstevel@tonic-gate 					 * The fact that another register def-
20777c478bd9Sstevel@tonic-gate 					 * inition has been found is fatal.
20787c478bd9Sstevel@tonic-gate 					 * Call the verification routine to get
20797c478bd9Sstevel@tonic-gate 					 * the error message and move on.
20807c478bd9Sstevel@tonic-gate 					 */
2081ba2be530Sab 					(void) (*ld_targ.t_ms.ms_reg_check)
2082ba2be530Sab 					    (rsdp, sym, name, ifl, ofl);
20837c478bd9Sstevel@tonic-gate 					continue;
20847c478bd9Sstevel@tonic-gate 				}
20857c478bd9Sstevel@tonic-gate 
20867c478bd9Sstevel@tonic-gate 				if (etype == ET_DYN) {
20877c478bd9Sstevel@tonic-gate 					if ((sdp = libld_calloc(
20886b3ba5bdSAli Bahrami 					    sizeof (Sym_desc), 1)) == NULL)
20897c478bd9Sstevel@tonic-gate 						return (S_ERROR);
20907c478bd9Sstevel@tonic-gate 					sdp->sd_ref = REF_DYN_SEEN;
2091ca4eed8bSAli Bahrami 
2092ca4eed8bSAli Bahrami 					/* Will not appear in output object */
20931da7e599SAli Bahrami 					symtab_enter = 0;
20947c478bd9Sstevel@tonic-gate 				}
20957c478bd9Sstevel@tonic-gate 			} else if (etype == ET_DYN)
20967c478bd9Sstevel@tonic-gate 				continue;
20977c478bd9Sstevel@tonic-gate 
20987c478bd9Sstevel@tonic-gate 			/*
20997c478bd9Sstevel@tonic-gate 			 * Fill in the remaining symbol descriptor information.
21007c478bd9Sstevel@tonic-gate 			 */
21016b3ba5bdSAli Bahrami 			if (sdp == NULL) {
21027c478bd9Sstevel@tonic-gate 				sdp = &(ifl->ifl_locs[ndx]);
21037c478bd9Sstevel@tonic-gate 				sdp->sd_ref = REF_REL_NEED;
2104635216b6SRod Evans 				sdp->sd_symndx = ndx;
21057c478bd9Sstevel@tonic-gate 			}
21066b3ba5bdSAli Bahrami 			if (rsdp == NULL) {
21077c478bd9Sstevel@tonic-gate 				sdp->sd_name = name;
21087c478bd9Sstevel@tonic-gate 				sdp->sd_sym = sym;
21097c478bd9Sstevel@tonic-gate 				sdp->sd_shndx = shndx;
21107c478bd9Sstevel@tonic-gate 				sdp->sd_flags = sdflags;
21117c478bd9Sstevel@tonic-gate 				sdp->sd_file = ifl;
21127c478bd9Sstevel@tonic-gate 				ifl->ifl_oldndx[ndx] = sdp;
21137c478bd9Sstevel@tonic-gate 			}
21147c478bd9Sstevel@tonic-gate 
21155aefb655Srie 			DBG_CALL(Dbg_syms_entry(ofl->ofl_lml, ndx, sdp));
21167c478bd9Sstevel@tonic-gate 
21177c478bd9Sstevel@tonic-gate 			/*
21187c478bd9Sstevel@tonic-gate 			 * Reclassify any SHN_SUNW_IGNORE symbols to SHN_UNDEF
21197c478bd9Sstevel@tonic-gate 			 * so as to simplify future processing.
21207c478bd9Sstevel@tonic-gate 			 */
21210bc07c75Srie 			if (sym->st_shndx == SHN_SUNW_IGNORE) {
21227c478bd9Sstevel@tonic-gate 				sdp->sd_shndx = shndx = SHN_UNDEF;
2123635216b6SRod Evans 				sdp->sd_flags |= (FLG_SY_IGNORE | FLG_SY_ELIM);
21247c478bd9Sstevel@tonic-gate 			}
21257c478bd9Sstevel@tonic-gate 
21267c478bd9Sstevel@tonic-gate 			/*
21277c478bd9Sstevel@tonic-gate 			 * Process any register symbols.
21287c478bd9Sstevel@tonic-gate 			 */
21297c478bd9Sstevel@tonic-gate 			if (sdp->sd_flags & FLG_SY_REGSYM) {
21307c478bd9Sstevel@tonic-gate 				/*
21317c478bd9Sstevel@tonic-gate 				 * Add a diagnostic to indicate we've caught a
21327c478bd9Sstevel@tonic-gate 				 * register symbol, as this can be useful if a
21337c478bd9Sstevel@tonic-gate 				 * register conflict is later discovered.
21347c478bd9Sstevel@tonic-gate 				 */
21355aefb655Srie 				DBG_CALL(Dbg_syms_entered(ofl, sym, sdp));
21367c478bd9Sstevel@tonic-gate 
21377c478bd9Sstevel@tonic-gate 				/*
21387c478bd9Sstevel@tonic-gate 				 * If this register symbol hasn't already been
21397c478bd9Sstevel@tonic-gate 				 * recorded, enter it now.
2140ba2be530Sab 				 *
2141ba2be530Sab 				 * The presence of FLG_SY_REGSYM means that
2142ba2be530Sab 				 * the pointers in ld_targ.t_ms are non-NULL.
21437c478bd9Sstevel@tonic-gate 				 */
21446b3ba5bdSAli Bahrami 				if ((rsdp == NULL) &&
2145ba2be530Sab 				    ((*ld_targ.t_ms.ms_reg_enter)(sdp, ofl) ==
2146ba2be530Sab 				    0))
21477c478bd9Sstevel@tonic-gate 					return (S_ERROR);
21487c478bd9Sstevel@tonic-gate 			}
21497c478bd9Sstevel@tonic-gate 
21507c478bd9Sstevel@tonic-gate 			/*
21517c478bd9Sstevel@tonic-gate 			 * Assign an input section.
21527c478bd9Sstevel@tonic-gate 			 */
21530bc07c75Srie 			if ((sym->st_shndx != SHN_UNDEF) &&
21547c478bd9Sstevel@tonic-gate 			    ((sdp->sd_flags & FLG_SY_SPECSEC) == 0))
21557c478bd9Sstevel@tonic-gate 				sdp->sd_isc = ifl->ifl_isdesc[shndx];
21567c478bd9Sstevel@tonic-gate 
21577c478bd9Sstevel@tonic-gate 			/*
21587c478bd9Sstevel@tonic-gate 			 * If this symbol falls within the range of a section
21597c478bd9Sstevel@tonic-gate 			 * being discarded, then discard the symbol itself.
21607c478bd9Sstevel@tonic-gate 			 * There is no reason to keep this local symbol.
21617c478bd9Sstevel@tonic-gate 			 */
21627c478bd9Sstevel@tonic-gate 			if (sdp->sd_isc &&
21637c478bd9Sstevel@tonic-gate 			    (sdp->sd_isc->is_flags & FLG_IS_DISCARD)) {
21647c478bd9Sstevel@tonic-gate 				sdp->sd_flags |= FLG_SY_ISDISC;
2165a194faf8Srie 				DBG_CALL(Dbg_syms_discarded(ofl->ofl_lml, sdp));
21667c478bd9Sstevel@tonic-gate 				continue;
21677c478bd9Sstevel@tonic-gate 			}
21687c478bd9Sstevel@tonic-gate 
21697c478bd9Sstevel@tonic-gate 			/*
21707c478bd9Sstevel@tonic-gate 			 * Skip any section symbols as new versions of these
21717c478bd9Sstevel@tonic-gate 			 * will be created.
21727c478bd9Sstevel@tonic-gate 			 */
21737c478bd9Sstevel@tonic-gate 			if ((type = ELF_ST_TYPE(sym->st_info)) == STT_SECTION) {
21740bc07c75Srie 				if (sym->st_shndx == SHN_UNDEF) {
21755aefb655Srie 					eprintf(ofl->ofl_lml, ERR_WARNING,
21767c478bd9Sstevel@tonic-gate 					    MSG_INTL(MSG_SYM_INVSHNDX),
21774a8d0ea7SAli Bahrami 					    demangle_symname(name, symsecname,
2178ba2be530Sab 					    ndx), ifl->ifl_name,
21794f680cc6SAli Bahrami 					    conv_sym_shndx(osabi, mach,
21804f680cc6SAli Bahrami 					    sym->st_shndx, CONV_FMT_DECIMAL,
2181de777a60Sab 					    &inv_buf));
21827c478bd9Sstevel@tonic-gate 				}
21837c478bd9Sstevel@tonic-gate 				continue;
21847c478bd9Sstevel@tonic-gate 			}
21857c478bd9Sstevel@tonic-gate 
218675e45495Sab 			/*
218775e45495Sab 			 * For a relocatable object, if this symbol is defined
218875e45495Sab 			 * and has non-zero length and references an address
218975e45495Sab 			 * within an associated section, then check its extents
219075e45495Sab 			 * to make sure the section boundaries encompass it.
219175e45495Sab 			 * If they don't, the ELF file is corrupt.
219275e45495Sab 			 */
219324b9abbaSab 			if (etype_rel) {
219424b9abbaSab 				if (SYM_LOC_BADADDR(sdp, sym, type)) {
219524b9abbaSab 					issue_badaddr_msg(ifl, ofl, sdp,
219624b9abbaSab 					    sym, shndx);
2197ca4eed8bSAli Bahrami 					if (ofl->ofl_flags & FLG_OF_FATAL)
2198ca4eed8bSAli Bahrami 						continue;
219924b9abbaSab 				}
220024b9abbaSab 
220124b9abbaSab 				/*
220224b9abbaSab 				 * We have observed relocatable objects
220324b9abbaSab 				 * containing identical adjacent STT_FILE
220424b9abbaSab 				 * symbols. Discard any other than the first,
220524b9abbaSab 				 * as they are all equivalent and the extras
220624b9abbaSab 				 * do not add information.
220724b9abbaSab 				 *
220824b9abbaSab 				 * For the purpose of this test, we assume
220924b9abbaSab 				 * that only the symbol type and the string
221024b9abbaSab 				 * table offset (st_name) matter.
221124b9abbaSab 				 */
221224b9abbaSab 				if (type == STT_FILE) {
221324b9abbaSab 					int toss = (last_file_sdp != NULL) &&
221424b9abbaSab 					    ((ndx - 1) == last_file_ndx) &&
221524b9abbaSab 					    (sym->st_name ==
221624b9abbaSab 					    last_file_sdp->sd_sym->st_name);
221724b9abbaSab 
221824b9abbaSab 					last_file_sdp = sdp;
221924b9abbaSab 					last_file_ndx = ndx;
222024b9abbaSab 					if (toss) {
222124b9abbaSab 						sdp->sd_flags |= FLG_SY_INVALID;
222224b9abbaSab 						DBG_CALL(Dbg_syms_dup_discarded(
222324b9abbaSab 						    ofl->ofl_lml, ndx, sdp));
222424b9abbaSab 						continue;
222524b9abbaSab 					}
222624b9abbaSab 				}
222775e45495Sab 			}
222875e45495Sab 
222924b9abbaSab 
22307c478bd9Sstevel@tonic-gate 			/*
22317c478bd9Sstevel@tonic-gate 			 * Sanity check for TLS
22327c478bd9Sstevel@tonic-gate 			 */
22330bc07c75Srie 			if ((sym->st_size != 0) && ((type == STT_TLS) &&
22340bc07c75Srie 			    (sym->st_shndx != SHN_COMMON))) {
22357c478bd9Sstevel@tonic-gate 				Is_desc	*isp = sdp->sd_isc;
22367c478bd9Sstevel@tonic-gate 
22376b3ba5bdSAli Bahrami 				if ((isp == NULL) || (isp->is_shdr == NULL) ||
22387c478bd9Sstevel@tonic-gate 				    ((isp->is_shdr->sh_flags & SHF_TLS) == 0)) {
22395aefb655Srie 					eprintf(ofl->ofl_lml, ERR_FATAL,
22407c478bd9Sstevel@tonic-gate 					    MSG_INTL(MSG_SYM_TLS),
22417c478bd9Sstevel@tonic-gate 					    demangle(sdp->sd_name),
22427c478bd9Sstevel@tonic-gate 					    ifl->ifl_name);
22437c478bd9Sstevel@tonic-gate 					ofl->ofl_flags |= FLG_OF_FATAL;
22447c478bd9Sstevel@tonic-gate 					continue;
22457c478bd9Sstevel@tonic-gate 				}
22467c478bd9Sstevel@tonic-gate 			}
22477c478bd9Sstevel@tonic-gate 
22487c478bd9Sstevel@tonic-gate 			/*
22497c478bd9Sstevel@tonic-gate 			 * Carry our some basic sanity checks (these are just
22507c478bd9Sstevel@tonic-gate 			 * some of the erroneous symbol entries we've come
22517c478bd9Sstevel@tonic-gate 			 * across, there's probably a lot more).  The symbol
22527c478bd9Sstevel@tonic-gate 			 * will not be carried forward to the output file, which
22537c478bd9Sstevel@tonic-gate 			 * won't be a problem unless a relocation is required
22547c478bd9Sstevel@tonic-gate 			 * against it.
22557c478bd9Sstevel@tonic-gate 			 */
22567c478bd9Sstevel@tonic-gate 			if (((sdp->sd_flags & FLG_SY_SPECSEC) &&
22570bc07c75Srie 			    ((sym->st_shndx == SHN_COMMON)) ||
22580bc07c75Srie 			    ((type == STT_FILE) &&
22590bc07c75Srie 			    (sym->st_shndx != SHN_ABS))) ||
22606b3ba5bdSAli Bahrami 			    (sdp->sd_isc && (sdp->sd_isc->is_osdesc == NULL))) {
22615aefb655Srie 				eprintf(ofl->ofl_lml, ERR_WARNING,
22625aefb655Srie 				    MSG_INTL(MSG_SYM_INVSHNDX),
22634a8d0ea7SAli Bahrami 				    demangle_symname(name, symsecname, ndx),
2264ba2be530Sab 				    ifl->ifl_name,
22654f680cc6SAli Bahrami 				    conv_sym_shndx(osabi, mach, sym->st_shndx,
22664f680cc6SAli Bahrami 				    CONV_FMT_DECIMAL, &inv_buf));
22677c478bd9Sstevel@tonic-gate 				sdp->sd_isc = NULL;
22687c478bd9Sstevel@tonic-gate 				sdp->sd_flags |= FLG_SY_INVALID;
22697c478bd9Sstevel@tonic-gate 				continue;
22707c478bd9Sstevel@tonic-gate 			}
22717c478bd9Sstevel@tonic-gate 
22727c478bd9Sstevel@tonic-gate 			/*
22737c478bd9Sstevel@tonic-gate 			 * As these local symbols will become part of the output
22747c478bd9Sstevel@tonic-gate 			 * image, record their number and name string size.
22757c478bd9Sstevel@tonic-gate 			 * Globals are counted after all input file processing
22767c478bd9Sstevel@tonic-gate 			 * (and hence symbol resolution) is complete during
22777c478bd9Sstevel@tonic-gate 			 * sym_validate().
22787c478bd9Sstevel@tonic-gate 			 */
22791da7e599SAli Bahrami 			if (!(ofl->ofl_flags & FLG_OF_REDLSYM) &&
22801da7e599SAli Bahrami 			    symtab_enter) {
22817c478bd9Sstevel@tonic-gate 				ofl->ofl_locscnt++;
22827c478bd9Sstevel@tonic-gate 
22837c478bd9Sstevel@tonic-gate 				if ((((sdp->sd_flags & FLG_SY_REGSYM) == 0) ||
22847c478bd9Sstevel@tonic-gate 				    sym->st_name) && (st_insert(ofl->ofl_strtab,
22857c478bd9Sstevel@tonic-gate 				    sdp->sd_name) == -1))
22867c478bd9Sstevel@tonic-gate 					return (S_ERROR);
22879039eeafSab 
22889039eeafSab 				if (allow_ldynsym && sym->st_name &&
2289d579eb63Sab 				    ldynsym_symtype[type]) {
22909039eeafSab 					ofl->ofl_dynlocscnt++;
22919039eeafSab 					if (st_insert(ofl->ofl_dynstrtab,
22929039eeafSab 					    sdp->sd_name) == -1)
22939039eeafSab 						return (S_ERROR);
2294d579eb63Sab 					/* Include it in sort section? */
2295d579eb63Sab 					DYNSORT_COUNT(sdp, sym, type, ++);
22969039eeafSab 				}
22977c478bd9Sstevel@tonic-gate 			}
22987c478bd9Sstevel@tonic-gate 		}
22997c478bd9Sstevel@tonic-gate 	}
23007c478bd9Sstevel@tonic-gate 
2301d840867fSab 	/*
2302d840867fSab 	 * The GNU ld interprets the top bit of the 16-bit Versym value
2303d840867fSab 	 * (0x8000) as the "hidden" bit. If this bit is set, the linker
2304d840867fSab 	 * is supposed to act as if that symbol does not exist. The Solaris
2305d840867fSab 	 * linker does not support this mechanism, or the model of interface
2306d840867fSab 	 * evolution that it allows, but we honor it in GNU ld produced
2307d840867fSab 	 * objects in order to interoperate with them.
2308d840867fSab 	 *
2309d840867fSab 	 * Determine if we should honor the GNU hidden bit for this file.
2310d840867fSab 	 */
2311d840867fSab 	test_gnu_hidden_bit = ((ifl->ifl_flags & FLG_IF_GNUVER) != 0) &&
2312d840867fSab 	    (ifl->ifl_versym != NULL);
2313d840867fSab 
2314*08278a5eSRod Evans 	/*
2315*08278a5eSRod Evans 	 * Determine whether object capabilities for this file are being
2316*08278a5eSRod Evans 	 * converted into symbol capabilities.  If so, global function symbols,
2317*08278a5eSRod Evans 	 * and initialized global data symbols, need special translation and
2318*08278a5eSRod Evans 	 * processing.
2319*08278a5eSRod Evans 	 */
2320*08278a5eSRod Evans 	if ((etype == ET_REL) && (ifl->ifl_flags & FLG_IF_OTOSCAP))
2321*08278a5eSRod Evans 		cdp = ifl->ifl_caps;
2322*08278a5eSRod Evans 
23237c478bd9Sstevel@tonic-gate 	/*
23247c478bd9Sstevel@tonic-gate 	 * Now scan the global symbols entering them in the internal symbol
23257c478bd9Sstevel@tonic-gate 	 * table or resolving them as necessary.
23267c478bd9Sstevel@tonic-gate 	 */
23277c478bd9Sstevel@tonic-gate 	sym = (Sym *)isc->is_indata->d_buf;
23287c478bd9Sstevel@tonic-gate 	sym += local;
23296b3ba5bdSAli Bahrami 	weak = 0;
23307c478bd9Sstevel@tonic-gate 	/* LINTED */
23317c478bd9Sstevel@tonic-gate 	for (ndx = (int)local; ndx < total; sym++, ndx++) {
23327c478bd9Sstevel@tonic-gate 		const char	*name;
2333635216b6SRod Evans 		sd_flag_t	sdflags = 0;
2334635216b6SRod Evans 		Word		shndx;
2335ba2be530Sab 		int		shndx_bad = 0;
2336635216b6SRod Evans 		Sym		*nsym = sym;
2337*08278a5eSRod Evans 		Cap_pair	*cpp = NULL;
2338*08278a5eSRod Evans 		uchar_t		ntype;
23397c478bd9Sstevel@tonic-gate 
23407c478bd9Sstevel@tonic-gate 		/*
2341ba2be530Sab 		 * Determine and validate the associated section index.
23427c478bd9Sstevel@tonic-gate 		 */
2343635216b6SRod Evans 		if (symshndx && (nsym->st_shndx == SHN_XINDEX)) {
23447c478bd9Sstevel@tonic-gate 			shndx = symshndx[ndx];
2345635216b6SRod Evans 		} else if ((shndx = nsym->st_shndx) >= SHN_LORESERVE) {
2346ba2be530Sab 			sdflags |= FLG_SY_SPECSEC;
2347ba2be530Sab 		} else if (shndx > ifl->ifl_ehdr->e_shnum) {
2348ba2be530Sab 			/* We need the name before we can issue error */
2349ba2be530Sab 			shndx_bad = 1;
23507c478bd9Sstevel@tonic-gate 		}
23517c478bd9Sstevel@tonic-gate 
23527c478bd9Sstevel@tonic-gate 		/*
23537c478bd9Sstevel@tonic-gate 		 * Check if st_name has a valid value or not.
23547c478bd9Sstevel@tonic-gate 		 */
2355635216b6SRod Evans 		if ((name = string(ofl, ifl, nsym, strs, strsize, ndx, shndx,
23564a8d0ea7SAli Bahrami 		    symsecndx, symsecname, strsecname, &sdflags)) == NULL) {
23577c478bd9Sstevel@tonic-gate 			ofl->ofl_flags |= FLG_OF_FATAL;
23587c478bd9Sstevel@tonic-gate 			continue;
23597c478bd9Sstevel@tonic-gate 		}
23607c478bd9Sstevel@tonic-gate 
2361ba2be530Sab 		/*
2362*08278a5eSRod Evans 		 * Now that we have the name, report an erroneous section index.
2363ba2be530Sab 		 */
2364ba2be530Sab 		if (shndx_bad) {
2365ba2be530Sab 			eprintf(ofl->ofl_lml, ERR_WARNING,
2366ba2be530Sab 			    MSG_INTL(MSG_SYM_INVSHNDX),
23674a8d0ea7SAli Bahrami 			    demangle_symname(name, symsecname, ndx),
2368ba2be530Sab 			    ifl->ifl_name,
2369635216b6SRod Evans 			    conv_sym_shndx(osabi, mach, nsym->st_shndx,
23704f680cc6SAli Bahrami 			    CONV_FMT_DECIMAL, &inv_buf));
2371ba2be530Sab 			continue;
2372ba2be530Sab 		}
2373ba2be530Sab 
23743b41b08bSab 		/*
2375d840867fSab 		 * Test for the GNU hidden bit, and ignore symbols that
2376d840867fSab 		 * have it set.
23773b41b08bSab 		 */
2378d840867fSab 		if (test_gnu_hidden_bit &&
2379d840867fSab 		    ((ifl->ifl_versym[ndx] & 0x8000) != 0))
23803b41b08bSab 			continue;
23813b41b08bSab 
23827c478bd9Sstevel@tonic-gate 		/*
23837c478bd9Sstevel@tonic-gate 		 * The linker itself will generate symbols for _end, _etext,
23847c478bd9Sstevel@tonic-gate 		 * _edata, _DYNAMIC and _PROCEDURE_LINKAGE_TABLE_, so don't
23857c478bd9Sstevel@tonic-gate 		 * bother entering these symbols from shared objects.  This
23867c478bd9Sstevel@tonic-gate 		 * results in some wasted resolution processing, which is hard
23877c478bd9Sstevel@tonic-gate 		 * to feel, but if nothing else, pollutes diagnostic relocation
23887c478bd9Sstevel@tonic-gate 		 * output.
23897c478bd9Sstevel@tonic-gate 		 */
2390635216b6SRod Evans 		if (name[0] && (etype == ET_DYN) && (nsym->st_size == 0) &&
2391635216b6SRod Evans 		    (ELF_ST_TYPE(nsym->st_info) == STT_OBJECT) &&
23927c478bd9Sstevel@tonic-gate 		    (name[0] == '_') && ((name[1] == 'e') ||
23937c478bd9Sstevel@tonic-gate 		    (name[1] == 'D') || (name[1] == 'P')) &&
23947c478bd9Sstevel@tonic-gate 		    ((strcmp(name, MSG_ORIG(MSG_SYM_ETEXT_U)) == 0) ||
23957c478bd9Sstevel@tonic-gate 		    (strcmp(name, MSG_ORIG(MSG_SYM_EDATA_U)) == 0) ||
23967c478bd9Sstevel@tonic-gate 		    (strcmp(name, MSG_ORIG(MSG_SYM_END_U)) == 0) ||
23977c478bd9Sstevel@tonic-gate 		    (strcmp(name, MSG_ORIG(MSG_SYM_DYNAMIC_U)) == 0) ||
23987c478bd9Sstevel@tonic-gate 		    (strcmp(name, MSG_ORIG(MSG_SYM_PLKTBL_U)) == 0))) {
23997c478bd9Sstevel@tonic-gate 			ifl->ifl_oldndx[ndx] = 0;
24007c478bd9Sstevel@tonic-gate 			continue;
24017c478bd9Sstevel@tonic-gate 		}
24027c478bd9Sstevel@tonic-gate 
2403cdcc71c0SAli Bahrami 		/*
2404cdcc71c0SAli Bahrami 		 * The '-z wrap=XXX' option emulates the GNU ld --wrap=XXX
2405cdcc71c0SAli Bahrami 		 * option. When XXX is the symbol to be wrapped:
2406cdcc71c0SAli Bahrami 		 *
2407*08278a5eSRod Evans 		 *  -	An undefined reference to XXX is converted to __wrap_XXX
2408*08278a5eSRod Evans 		 *  -	An undefined reference to __real_XXX is converted to XXX
2409cdcc71c0SAli Bahrami 		 *
2410cdcc71c0SAli Bahrami 		 * The idea is that the user can supply a wrapper function
2411cdcc71c0SAli Bahrami 		 * __wrap_XXX that does some work, and then uses the name
2412cdcc71c0SAli Bahrami 		 * __real_XXX to pass the call on to the real function. The
2413cdcc71c0SAli Bahrami 		 * wrapper objects are linked with the original unmodified
2414cdcc71c0SAli Bahrami 		 * objects to produce a wrapped version of the output object.
2415cdcc71c0SAli Bahrami 		 */
2416cdcc71c0SAli Bahrami 		if (ofl->ofl_wrap && name[0] && (shndx == SHN_UNDEF)) {
2417cdcc71c0SAli Bahrami 			WrapSymNode wsn, *wsnp;
2418cdcc71c0SAli Bahrami 
2419cdcc71c0SAli Bahrami 			/*
2420cdcc71c0SAli Bahrami 			 * If this is the __real_XXX form, advance the
2421cdcc71c0SAli Bahrami 			 * pointer to reference the wrapped name.
2422cdcc71c0SAli Bahrami 			 */
2423cdcc71c0SAli Bahrami 			wsn.wsn_name = name;
2424cdcc71c0SAli Bahrami 			if ((*name == '_') &&
2425cdcc71c0SAli Bahrami 			    (strncmp(name, MSG_ORIG(MSG_STR_UU_REAL_U),
2426cdcc71c0SAli Bahrami 			    MSG_STR_UU_REAL_U_SIZE) == 0))
2427cdcc71c0SAli Bahrami 				wsn.wsn_name += MSG_STR_UU_REAL_U_SIZE;
2428cdcc71c0SAli Bahrami 
2429cdcc71c0SAli Bahrami 			/*
2430cdcc71c0SAli Bahrami 			 * Is this symbol in the wrap AVL tree? If so, map
2431cdcc71c0SAli Bahrami 			 * XXX to __wrap_XXX, and __real_XXX to XXX. Note that
2432cdcc71c0SAli Bahrami 			 * wsn.wsn_name will equal the current value of name
2433cdcc71c0SAli Bahrami 			 * if the __real_ prefix is not present.
2434cdcc71c0SAli Bahrami 			 */
2435cdcc71c0SAli Bahrami 			if ((wsnp = avl_find(ofl->ofl_wrap, &wsn, 0)) != NULL) {
2436cdcc71c0SAli Bahrami 				const char *old_name = name;
2437cdcc71c0SAli Bahrami 
2438cdcc71c0SAli Bahrami 				name = (wsn.wsn_name == name) ?
2439cdcc71c0SAli Bahrami 				    wsnp->wsn_wrapname : wsn.wsn_name;
2440cdcc71c0SAli Bahrami 				DBG_CALL(Dbg_syms_wrap(ofl->ofl_lml, ndx,
2441cdcc71c0SAli Bahrami 				    old_name, name));
2442cdcc71c0SAli Bahrami 			}
2443cdcc71c0SAli Bahrami 		}
2444cdcc71c0SAli Bahrami 
24457c478bd9Sstevel@tonic-gate 		/*
24467c478bd9Sstevel@tonic-gate 		 * Determine and validate the symbols binding.
24477c478bd9Sstevel@tonic-gate 		 */
2448635216b6SRod Evans 		bind = ELF_ST_BIND(nsym->st_info);
24497c478bd9Sstevel@tonic-gate 		if ((bind != STB_GLOBAL) && (bind != STB_WEAK)) {
24505aefb655Srie 			eprintf(ofl->ofl_lml, ERR_WARNING,
2451ba2be530Sab 			    MSG_INTL(MSG_SYM_NONGLOB),
24524a8d0ea7SAli Bahrami 			    demangle_symname(name, symsecname, ndx),
2453de777a60Sab 			    ifl->ifl_name,
2454de777a60Sab 			    conv_sym_info_bind(bind, 0, &inv_buf));
24557c478bd9Sstevel@tonic-gate 			continue;
24567c478bd9Sstevel@tonic-gate 		}
24576b3ba5bdSAli Bahrami 		if (bind == STB_WEAK)
24586b3ba5bdSAli Bahrami 			weak++;
24597c478bd9Sstevel@tonic-gate 
24607c478bd9Sstevel@tonic-gate 		/*
24617c478bd9Sstevel@tonic-gate 		 * If this symbol falls within the range of a section being
24627c478bd9Sstevel@tonic-gate 		 * discarded, then discard the symbol itself.
24637c478bd9Sstevel@tonic-gate 		 */
24647c478bd9Sstevel@tonic-gate 		if (((sdflags & FLG_SY_SPECSEC) == 0) &&
2465635216b6SRod Evans 		    (nsym->st_shndx != SHN_UNDEF)) {
24667c478bd9Sstevel@tonic-gate 			Is_desc	*isp;
24677c478bd9Sstevel@tonic-gate 
24687c478bd9Sstevel@tonic-gate 			if (shndx >= ifl->ifl_shnum) {
24697c478bd9Sstevel@tonic-gate 				/*
24707c478bd9Sstevel@tonic-gate 				 * Carry our some basic sanity checks
24717c478bd9Sstevel@tonic-gate 				 * The symbol will not be carried forward to
24727c478bd9Sstevel@tonic-gate 				 * the output file, which won't be a problem
24737c478bd9Sstevel@tonic-gate 				 * unless a relocation is required against it.
24747c478bd9Sstevel@tonic-gate 				 */
24755aefb655Srie 				eprintf(ofl->ofl_lml, ERR_WARNING,
2476ba2be530Sab 				    MSG_INTL(MSG_SYM_INVSHNDX),
24774a8d0ea7SAli Bahrami 				    demangle_symname(name, symsecname, ndx),
24780bc07c75Srie 				    ifl->ifl_name,
2479635216b6SRod Evans 				    conv_sym_shndx(osabi, mach, nsym->st_shndx,
24804f680cc6SAli Bahrami 				    CONV_FMT_DECIMAL, &inv_buf));
24817c478bd9Sstevel@tonic-gate 				continue;
24827c478bd9Sstevel@tonic-gate 			}
24837c478bd9Sstevel@tonic-gate 
24847c478bd9Sstevel@tonic-gate 			isp = ifl->ifl_isdesc[shndx];
24857c478bd9Sstevel@tonic-gate 			if (isp && (isp->is_flags & FLG_IS_DISCARD)) {
24867c478bd9Sstevel@tonic-gate 				if ((sdp =
24876b3ba5bdSAli Bahrami 				    libld_calloc(sizeof (Sym_desc), 1)) == NULL)
24887c478bd9Sstevel@tonic-gate 					return (S_ERROR);
24897c478bd9Sstevel@tonic-gate 
24907c478bd9Sstevel@tonic-gate 				/*
24917c478bd9Sstevel@tonic-gate 				 * Create a dummy symbol entry so that if we
24927c478bd9Sstevel@tonic-gate 				 * find any references to this discarded symbol
24937c478bd9Sstevel@tonic-gate 				 * we can compensate.
24947c478bd9Sstevel@tonic-gate 				 */
24957c478bd9Sstevel@tonic-gate 				sdp->sd_name = name;
2496635216b6SRod Evans 				sdp->sd_sym = nsym;
24977c478bd9Sstevel@tonic-gate 				sdp->sd_file = ifl;
24987c478bd9Sstevel@tonic-gate 				sdp->sd_isc = isp;
24997c478bd9Sstevel@tonic-gate 				sdp->sd_flags = FLG_SY_ISDISC;
25007c478bd9Sstevel@tonic-gate 				ifl->ifl_oldndx[ndx] = sdp;
25017c478bd9Sstevel@tonic-gate 
2502a194faf8Srie 				DBG_CALL(Dbg_syms_discarded(ofl->ofl_lml, sdp));
25037c478bd9Sstevel@tonic-gate 				continue;
25047c478bd9Sstevel@tonic-gate 			}
25057c478bd9Sstevel@tonic-gate 		}
25067c478bd9Sstevel@tonic-gate 
2507*08278a5eSRod Evans 		/*
2508*08278a5eSRod Evans 		 * If object capabilities for this file are being converted
2509*08278a5eSRod Evans 		 * into symbol capabilities, then:
2510*08278a5eSRod Evans 		 *
2511*08278a5eSRod Evans 		 *  -	Any global function, or initialized global data symbol
2512*08278a5eSRod Evans 		 *	definitions (ie., those that are not associated with
2513*08278a5eSRod Evans 		 *	special symbol types, ie., ABS, COMMON, etc.), and which
2514*08278a5eSRod Evans 		 *	have not been reduced to locals, are converted to symbol
2515*08278a5eSRod Evans 		 *	references (UNDEF).  This ensures that any reference to
2516*08278a5eSRod Evans 		 *	the original symbol, for example from a relocation, get
2517*08278a5eSRod Evans 		 *	associated to a capabilities family lead symbol, ie., a
2518*08278a5eSRod Evans 		 *	generic instance.
2519*08278a5eSRod Evans 		 *
2520*08278a5eSRod Evans 		 *  -	For each global function, or object symbol definition,
2521*08278a5eSRod Evans 		 *	a new local symbol is created.  The function or object
2522*08278a5eSRod Evans 		 *	is renamed using the capabilities CA_SUNW_ID definition
2523*08278a5eSRod Evans 		 *	(which might have been fabricated for this purpose -
2524*08278a5eSRod Evans 		 *	see get_cap_group()).  The new symbol name is:
2525*08278a5eSRod Evans 		 *
2526*08278a5eSRod Evans 		 *	    <original name>%<capability group identifier>
2527*08278a5eSRod Evans 		 *
2528*08278a5eSRod Evans 		 *	This symbol is associated to the same location, and
2529*08278a5eSRod Evans 		 *	becomes a capabilities family member.
2530*08278a5eSRod Evans 		 */
2531*08278a5eSRod Evans 		/* LINTED */
2532*08278a5eSRod Evans 		hash = (Word)elf_hash(name);
2533*08278a5eSRod Evans 
2534*08278a5eSRod Evans 		ntype = ELF_ST_TYPE(nsym->st_info);
2535*08278a5eSRod Evans 		if (cdp && (nsym->st_shndx != SHN_UNDEF) &&
2536*08278a5eSRod Evans 		    ((sdflags & FLG_SY_SPECSEC) == 0) &&
2537*08278a5eSRod Evans 		    ((ntype == STT_FUNC) || (ntype == STT_OBJECT))) {
2538*08278a5eSRod Evans 			/*
2539*08278a5eSRod Evans 			 * Determine this symbol's visibility.  If a mapfile has
2540*08278a5eSRod Evans 			 * indicated this symbol should be local, then there's
2541*08278a5eSRod Evans 			 * no point in transforming this global symbol to a
2542*08278a5eSRod Evans 			 * capabilities symbol.  Otherwise, create a symbol
2543*08278a5eSRod Evans 			 * capability pair descriptor to record this symbol as
2544*08278a5eSRod Evans 			 * a candidate for translation.
2545*08278a5eSRod Evans 			 */
2546*08278a5eSRod Evans 			if (sym_cap_vis(name, hash, sym, ofl) &&
2547*08278a5eSRod Evans 			    ((cpp = alist_append(&cappairs, NULL,
2548*08278a5eSRod Evans 			    sizeof (Cap_pair), AL_CNT_CAP_PAIRS)) == NULL))
2549*08278a5eSRod Evans 				return (S_ERROR);
2550*08278a5eSRod Evans 		}
2551*08278a5eSRod Evans 
2552*08278a5eSRod Evans 		if (cpp) {
2553*08278a5eSRod Evans 			Sym	*rsym;
2554*08278a5eSRod Evans 
2555*08278a5eSRod Evans 			DBG_CALL(Dbg_syms_cap_convert(ofl, ndx, name, nsym));
2556*08278a5eSRod Evans 
2557*08278a5eSRod Evans 			/*
2558*08278a5eSRod Evans 			 * Allocate a new symbol descriptor to represent the
2559*08278a5eSRod Evans 			 * transformed global symbol.  The descriptor points
2560*08278a5eSRod Evans 			 * to the original symbol information (which might
2561*08278a5eSRod Evans 			 * indicate a global or weak visibility).  The symbol
2562*08278a5eSRod Evans 			 * information will be transformed into a local symbol
2563*08278a5eSRod Evans 			 * later, after any weak aliases are culled.
2564*08278a5eSRod Evans 			 */
2565*08278a5eSRod Evans 			if ((cpp->c_osdp =
2566*08278a5eSRod Evans 			    libld_malloc(sizeof (Sym_desc))) == NULL)
2567*08278a5eSRod Evans 				return (S_ERROR);
2568*08278a5eSRod Evans 
2569*08278a5eSRod Evans 			cpp->c_osdp->sd_name = name;
2570*08278a5eSRod Evans 			cpp->c_osdp->sd_sym = nsym;
2571*08278a5eSRod Evans 			cpp->c_osdp->sd_shndx = shndx;
2572*08278a5eSRod Evans 			cpp->c_osdp->sd_file = ifl;
2573*08278a5eSRod Evans 			cpp->c_osdp->sd_isc = ifl->ifl_isdesc[shndx];
2574*08278a5eSRod Evans 			cpp->c_osdp->sd_ref = REF_REL_NEED;
2575*08278a5eSRod Evans 
2576*08278a5eSRod Evans 			/*
2577*08278a5eSRod Evans 			 * Save the capabilities group this symbol belongs to,
2578*08278a5eSRod Evans 			 * and the original symbol index.
2579*08278a5eSRod Evans 			 */
2580*08278a5eSRod Evans 			cpp->c_group = cdp->ca_groups->apl_data[0];
2581*08278a5eSRod Evans 			cpp->c_ndx = ndx;
2582*08278a5eSRod Evans 
2583*08278a5eSRod Evans 			/*
2584*08278a5eSRod Evans 			 * Replace the original symbol definition with a symbol
2585*08278a5eSRod Evans 			 * reference.  Make sure this reference isn't left as a
2586*08278a5eSRod Evans 			 * weak.
2587*08278a5eSRod Evans 			 */
2588*08278a5eSRod Evans 			if ((rsym = libld_malloc(sizeof (Sym))) == NULL)
2589*08278a5eSRod Evans 				return (S_ERROR);
2590*08278a5eSRod Evans 
2591*08278a5eSRod Evans 			*rsym = *nsym;
2592*08278a5eSRod Evans 
2593*08278a5eSRod Evans 			rsym->st_info = ELF_ST_INFO(STB_GLOBAL, ntype);
2594*08278a5eSRod Evans 			rsym->st_shndx = shndx = SHN_UNDEF;
2595*08278a5eSRod Evans 			rsym->st_value = 0;
2596*08278a5eSRod Evans 			rsym->st_size = 0;
2597*08278a5eSRod Evans 
2598*08278a5eSRod Evans 			sdflags |= FLG_SY_CAP;
2599*08278a5eSRod Evans 
2600*08278a5eSRod Evans 			nsym = rsym;
2601*08278a5eSRod Evans 		}
2602*08278a5eSRod Evans 
26037c478bd9Sstevel@tonic-gate 		/*
26047c478bd9Sstevel@tonic-gate 		 * If the symbol does not already exist in the internal symbol
26057c478bd9Sstevel@tonic-gate 		 * table add it, otherwise resolve the conflict.  If the symbol
26067c478bd9Sstevel@tonic-gate 		 * from this file is kept, retain its symbol table index for
26077c478bd9Sstevel@tonic-gate 		 * possible use in associating a global alias.
26087c478bd9Sstevel@tonic-gate 		 */
26095aefb655Srie 		if ((sdp = ld_sym_find(name, hash, &where, ofl)) == NULL) {
26105aefb655Srie 			DBG_CALL(Dbg_syms_global(ofl->ofl_lml, ndx, name));
2611635216b6SRod Evans 			if ((sdp = ld_sym_enter(name, nsym, hash, ifl, ofl, ndx,
2612635216b6SRod Evans 			    shndx, sdflags, &where)) == (Sym_desc *)S_ERROR)
26137c478bd9Sstevel@tonic-gate 				return (S_ERROR);
26147c478bd9Sstevel@tonic-gate 
2615635216b6SRod Evans 		} else if (ld_sym_resolve(sdp, nsym, ifl, ofl, ndx, shndx,
26167c478bd9Sstevel@tonic-gate 		    sdflags) == S_ERROR)
26177c478bd9Sstevel@tonic-gate 			return (S_ERROR);
26187c478bd9Sstevel@tonic-gate 
2619*08278a5eSRod Evans 		/*
2620*08278a5eSRod Evans 		 * Now that we have a symbol descriptor, retain the descriptor
2621*08278a5eSRod Evans 		 * for later use by symbol capabilities processing.
2622*08278a5eSRod Evans 		 */
2623*08278a5eSRod Evans 		if (cpp)
2624*08278a5eSRod Evans 			cpp->c_nsdp = sdp;
2625*08278a5eSRod Evans 
26267c478bd9Sstevel@tonic-gate 		/*
26277c478bd9Sstevel@tonic-gate 		 * After we've compared a defined symbol in one shared
26287c478bd9Sstevel@tonic-gate 		 * object, flag the symbol so we don't compare it again.
26297c478bd9Sstevel@tonic-gate 		 */
2630635216b6SRod Evans 		if ((etype == ET_DYN) && (nsym->st_shndx != SHN_UNDEF) &&
26317c478bd9Sstevel@tonic-gate 		    ((sdp->sd_flags & FLG_SY_SOFOUND) == 0))
26327c478bd9Sstevel@tonic-gate 			sdp->sd_flags |= FLG_SY_SOFOUND;
26337c478bd9Sstevel@tonic-gate 
26347c478bd9Sstevel@tonic-gate 		/*
26357c478bd9Sstevel@tonic-gate 		 * If the symbol is accepted from this file retain the symbol
26367c478bd9Sstevel@tonic-gate 		 * index for possible use in aliasing.
26377c478bd9Sstevel@tonic-gate 		 */
26387c478bd9Sstevel@tonic-gate 		if (sdp->sd_file == ifl)
26397c478bd9Sstevel@tonic-gate 			sdp->sd_symndx = ndx;
26407c478bd9Sstevel@tonic-gate 
26417c478bd9Sstevel@tonic-gate 		ifl->ifl_oldndx[ndx] = sdp;
26427c478bd9Sstevel@tonic-gate 
26437c478bd9Sstevel@tonic-gate 		/*
26447c478bd9Sstevel@tonic-gate 		 * If we've accepted a register symbol, continue to validate
26457c478bd9Sstevel@tonic-gate 		 * it.
26467c478bd9Sstevel@tonic-gate 		 */
26477c478bd9Sstevel@tonic-gate 		if (sdp->sd_flags & FLG_SY_REGSYM) {
26487c478bd9Sstevel@tonic-gate 			Sym_desc	*rsdp;
26497c478bd9Sstevel@tonic-gate 
2650ba2be530Sab 			/*
2651ba2be530Sab 			 * The presence of FLG_SY_REGSYM means that
2652ba2be530Sab 			 * the pointers in ld_targ.t_ms are non-NULL.
2653ba2be530Sab 			 */
2654ba2be530Sab 			rsdp = (*ld_targ.t_ms.ms_reg_find)(sdp->sd_sym, ofl);
26556b3ba5bdSAli Bahrami 			if (rsdp == NULL) {
2656ba2be530Sab 				if ((*ld_targ.t_ms.ms_reg_enter)(sdp, ofl) == 0)
26577c478bd9Sstevel@tonic-gate 					return (S_ERROR);
26587c478bd9Sstevel@tonic-gate 			} else if (rsdp != sdp) {
2659ba2be530Sab 				(void) (*ld_targ.t_ms.ms_reg_check)(rsdp,
2660ba2be530Sab 				    sdp->sd_sym, sdp->sd_name, ifl, ofl);
26617c478bd9Sstevel@tonic-gate 			}
26627c478bd9Sstevel@tonic-gate 		}
266375e45495Sab 
266475e45495Sab 		/*
266575e45495Sab 		 * For a relocatable object, if this symbol is defined
266675e45495Sab 		 * and has non-zero length and references an address
266775e45495Sab 		 * within an associated section, then check its extents
266875e45495Sab 		 * to make sure the section boundaries encompass it.
266975e45495Sab 		 * If they don't, the ELF file is corrupt. Note that this
267075e45495Sab 		 * global symbol may have come from another file to satisfy
267175e45495Sab 		 * an UNDEF symbol of the same name from this one. In that
267275e45495Sab 		 * case, we don't check it, because it was already checked
267375e45495Sab 		 * as part of its own file.
267475e45495Sab 		 */
267575e45495Sab 		if (etype_rel && (sdp->sd_file == ifl)) {
267675e45495Sab 			Sym *tsym = sdp->sd_sym;
267775e45495Sab 
267875e45495Sab 			if (SYM_LOC_BADADDR(sdp, tsym,
267975e45495Sab 			    ELF_ST_TYPE(tsym->st_info))) {
268069a0bf0cSab 				issue_badaddr_msg(ifl, ofl, sdp,
268169a0bf0cSab 				    tsym, tsym->st_shndx);
268275e45495Sab 				continue;
268375e45495Sab 			}
268475e45495Sab 		}
26857c478bd9Sstevel@tonic-gate 	}
26867c478bd9Sstevel@tonic-gate 
26877c478bd9Sstevel@tonic-gate 	/*
26886b3ba5bdSAli Bahrami 	 * Associate weak (alias) symbols to their non-weak counterparts by
2689*08278a5eSRod Evans 	 * scanning the global symbols one more time.
26906b3ba5bdSAli Bahrami 	 *
26916b3ba5bdSAli Bahrami 	 * This association is needed when processing the symbols from a shared
26926b3ba5bdSAli Bahrami 	 * object dependency when a a weak definition satisfies a reference:
26937c478bd9Sstevel@tonic-gate 	 *
26946b3ba5bdSAli Bahrami 	 *  -	When building a dynamic executable, if a referenced symbol is a
26956b3ba5bdSAli Bahrami 	 *	data item, the symbol data is copied to the executables address
26966b3ba5bdSAli Bahrami 	 *	space.  In this copy-relocation case, we must also reassociate
26976b3ba5bdSAli Bahrami 	 *	the alias symbol with its new location in the executable.
26987c478bd9Sstevel@tonic-gate 	 *
26996b3ba5bdSAli Bahrami 	 *  -	If the referenced symbol is a function then we may need to
27006b3ba5bdSAli Bahrami 	 *	promote the symbols binding from undefined weak to undefined,
27016b3ba5bdSAli Bahrami 	 *	otherwise the run-time linker will not generate the correct
27026b3ba5bdSAli Bahrami 	 *	relocation error should the symbol not be found.
27036b3ba5bdSAli Bahrami 	 *
27046b3ba5bdSAli Bahrami 	 * Weak alias association is also required when a local dynsym table
27056b3ba5bdSAli Bahrami 	 * is being created.  This table should only contain one instance of a
27066b3ba5bdSAli Bahrami 	 * symbol that is associated to a given address.
27077c478bd9Sstevel@tonic-gate 	 *
27087c478bd9Sstevel@tonic-gate 	 * The true association between a weak/strong symbol pair is that both
27096b3ba5bdSAli Bahrami 	 * symbol entries are identical, thus first we create a sorted symbol
27106b3ba5bdSAli Bahrami 	 * list keyed off of the symbols section index and value.  If the symbol
27116b3ba5bdSAli Bahrami 	 * belongs to the same section and has the same value, then the chances
27126b3ba5bdSAli Bahrami 	 * are that the rest of the symbols data is the same.  This list is then
27136b3ba5bdSAli Bahrami 	 * scanned for weak symbols, and if one is found then any strong
27146b3ba5bdSAli Bahrami 	 * association will exist in the entries that follow.  Thus we just have
27156b3ba5bdSAli Bahrami 	 * to scan one (typically a single alias) or more (in the uncommon
27166b3ba5bdSAli Bahrami 	 * instance of multiple weak to strong associations) entries to
27176b3ba5bdSAli Bahrami 	 * determine if a match exists.
27187c478bd9Sstevel@tonic-gate 	 */
27196b3ba5bdSAli Bahrami 	if (weak && (OFL_ALLOW_LDYNSYM(ofl) || (etype == ET_DYN)) &&
2720d579eb63Sab 	    (total > local)) {
27216b3ba5bdSAli Bahrami 		static Sym_desc	**sort;
27226b3ba5bdSAli Bahrami 		static size_t	osize = 0;
27236b3ba5bdSAli Bahrami 		size_t		nsize = (total - local) * sizeof (Sym_desc *);
27247c478bd9Sstevel@tonic-gate 
27256b3ba5bdSAli Bahrami 		/*
27266b3ba5bdSAli Bahrami 		 * As we might be processing many input files, and many symbols,
27276b3ba5bdSAli Bahrami 		 * try and reuse a static sort buffer.  Note, presently we're
27286b3ba5bdSAli Bahrami 		 * playing the game of never freeing any buffers as there's a
27296b3ba5bdSAli Bahrami 		 * belief this wastes time.
27306b3ba5bdSAli Bahrami 		 */
27316b3ba5bdSAli Bahrami 		if ((osize == 0) || (nsize > osize)) {
27326b3ba5bdSAli Bahrami 			if ((sort = libld_malloc(nsize)) == NULL)
27336b3ba5bdSAli Bahrami 				return (S_ERROR);
27346b3ba5bdSAli Bahrami 			osize = nsize;
27356b3ba5bdSAli Bahrami 		}
27366b3ba5bdSAli Bahrami 		(void) memcpy((void *)sort, &ifl->ifl_oldndx[local], nsize);
27377c478bd9Sstevel@tonic-gate 
27387c478bd9Sstevel@tonic-gate 		qsort(sort, (total - local), sizeof (Sym_desc *), compare);
27397c478bd9Sstevel@tonic-gate 
27407c478bd9Sstevel@tonic-gate 		for (ndx = 0; ndx < (total - local); ndx++) {
27416b3ba5bdSAli Bahrami 			Sym_desc	*wsdp = sort[ndx];
27426b3ba5bdSAli Bahrami 			Sym		*wsym;
27437c478bd9Sstevel@tonic-gate 			int		sndx;
27447c478bd9Sstevel@tonic-gate 
27456b3ba5bdSAli Bahrami 			/*
27466b3ba5bdSAli Bahrami 			 * Ignore any empty symbol descriptor, or the case where
27476b3ba5bdSAli Bahrami 			 * the symbol has been resolved to a different file.
27486b3ba5bdSAli Bahrami 			 */
27496b3ba5bdSAli Bahrami 			if ((wsdp == NULL) || (wsdp->sd_file != ifl))
27507c478bd9Sstevel@tonic-gate 				continue;
27517c478bd9Sstevel@tonic-gate 
27527c478bd9Sstevel@tonic-gate 			wsym = wsdp->sd_sym;
27537c478bd9Sstevel@tonic-gate 
27546b3ba5bdSAli Bahrami 			if ((wsym->st_shndx == SHN_UNDEF) ||
27556b3ba5bdSAli Bahrami 			    (wsdp->sd_flags & FLG_SY_SPECSEC) ||
27566b3ba5bdSAli Bahrami 			    (ELF_ST_BIND(wsym->st_info) != STB_WEAK))
27577c478bd9Sstevel@tonic-gate 				continue;
27587c478bd9Sstevel@tonic-gate 
27597c478bd9Sstevel@tonic-gate 			/*
27607c478bd9Sstevel@tonic-gate 			 * We have a weak symbol, if it has a strong alias it
27617c478bd9Sstevel@tonic-gate 			 * will have been sorted to one of the following sort
27627c478bd9Sstevel@tonic-gate 			 * table entries.  Note that we could have multiple weak
27637c478bd9Sstevel@tonic-gate 			 * symbols aliased to one strong (if this occurs then
27647c478bd9Sstevel@tonic-gate 			 * the strong symbol only maintains one alias back to
27657c478bd9Sstevel@tonic-gate 			 * the last weak).
27667c478bd9Sstevel@tonic-gate 			 */
27677c478bd9Sstevel@tonic-gate 			for (sndx = ndx + 1; sndx < (total - local); sndx++) {
27686b3ba5bdSAli Bahrami 				Sym_desc	*ssdp = sort[sndx];
27696b3ba5bdSAli Bahrami 				Sym		*ssym;
2770635216b6SRod Evans 				sd_flag_t	w_dynbits, s_dynbits;
27717c478bd9Sstevel@tonic-gate 
27726b3ba5bdSAli Bahrami 				/*
27736b3ba5bdSAli Bahrami 				 * Ignore any empty symbol descriptor, or the
27746b3ba5bdSAli Bahrami 				 * case where the symbol has been resolved to a
27756b3ba5bdSAli Bahrami 				 * different file.
27766b3ba5bdSAli Bahrami 				 */
27776b3ba5bdSAli Bahrami 				if ((ssdp == NULL) || (ssdp->sd_file != ifl))
27786b3ba5bdSAli Bahrami 					continue;
27797c478bd9Sstevel@tonic-gate 
27807c478bd9Sstevel@tonic-gate 				ssym = ssdp->sd_sym;
27817c478bd9Sstevel@tonic-gate 
27826b3ba5bdSAli Bahrami 				if (ssym->st_shndx == SHN_UNDEF)
27836b3ba5bdSAli Bahrami 					continue;
27846b3ba5bdSAli Bahrami 
27856b3ba5bdSAli Bahrami 				if ((ssym->st_shndx != wsym->st_shndx) ||
27866b3ba5bdSAli Bahrami 				    (ssym->st_value != wsym->st_value))
27877c478bd9Sstevel@tonic-gate 					break;
27887c478bd9Sstevel@tonic-gate 
27896b3ba5bdSAli Bahrami 				if ((ssym->st_size != wsym->st_size) ||
27906b3ba5bdSAli Bahrami 				    (ssdp->sd_flags & FLG_SY_SPECSEC) ||
27916b3ba5bdSAli Bahrami 				    (ELF_ST_BIND(ssym->st_info) == STB_WEAK))
27926b3ba5bdSAli Bahrami 					continue;
2793d579eb63Sab 
27946b3ba5bdSAli Bahrami 				/*
27956b3ba5bdSAli Bahrami 				 * If a sharable object, set link fields so
27966b3ba5bdSAli Bahrami 				 * that they reference each other.`
27976b3ba5bdSAli Bahrami 				 */
27986b3ba5bdSAli Bahrami 				if (etype == ET_DYN) {
27996b3ba5bdSAli Bahrami 					ssdp->sd_aux->sa_linkndx =
28006b3ba5bdSAli Bahrami 					    (Word)wsdp->sd_symndx;
28016b3ba5bdSAli Bahrami 					wsdp->sd_aux->sa_linkndx =
28026b3ba5bdSAli Bahrami 					    (Word)ssdp->sd_symndx;
28036b3ba5bdSAli Bahrami 				}
28046b3ba5bdSAli Bahrami 
28056b3ba5bdSAli Bahrami 				/*
28066b3ba5bdSAli Bahrami 				 * Determine which of these two symbols go into
28076b3ba5bdSAli Bahrami 				 * the sort section.  If a mapfile has made
28086b3ba5bdSAli Bahrami 				 * explicit settings of the FLG_SY_*DYNSORT
28096b3ba5bdSAli Bahrami 				 * flags for both symbols, then we do what they
28106b3ba5bdSAli Bahrami 				 * say.  If one has the DYNSORT flags set, we
28116b3ba5bdSAli Bahrami 				 * set the NODYNSORT bit in the other.  And if
28126b3ba5bdSAli Bahrami 				 * neither has an explicit setting, then we
28136b3ba5bdSAli Bahrami 				 * favor the weak symbol because they usually
28146b3ba5bdSAli Bahrami 				 * lack the leading underscore.
28156b3ba5bdSAli Bahrami 				 */
28166b3ba5bdSAli Bahrami 				w_dynbits = wsdp->sd_flags &
28176b3ba5bdSAli Bahrami 				    (FLG_SY_DYNSORT | FLG_SY_NODYNSORT);
28186b3ba5bdSAli Bahrami 				s_dynbits = ssdp->sd_flags &
28196b3ba5bdSAli Bahrami 				    (FLG_SY_DYNSORT | FLG_SY_NODYNSORT);
28206b3ba5bdSAli Bahrami 				if (!(w_dynbits && s_dynbits)) {
28216b3ba5bdSAli Bahrami 					if (s_dynbits) {
28226b3ba5bdSAli Bahrami 						if (s_dynbits == FLG_SY_DYNSORT)
282333eb6ee1Sab 							wsdp->sd_flags |=
282433eb6ee1Sab 							    FLG_SY_NODYNSORT;
28256b3ba5bdSAli Bahrami 					} else if (w_dynbits !=
28266b3ba5bdSAli Bahrami 					    FLG_SY_NODYNSORT) {
28276b3ba5bdSAli Bahrami 						ssdp->sd_flags |=
28286b3ba5bdSAli Bahrami 						    FLG_SY_NODYNSORT;
2829d579eb63Sab 					}
28307c478bd9Sstevel@tonic-gate 				}
28316b3ba5bdSAli Bahrami 				break;
28327c478bd9Sstevel@tonic-gate 			}
28337c478bd9Sstevel@tonic-gate 		}
28347c478bd9Sstevel@tonic-gate 	}
2835*08278a5eSRod Evans 
2836*08278a5eSRod Evans 	/*
2837*08278a5eSRod Evans 	 * Having processed all symbols, under -z symbolcap, reprocess any
2838*08278a5eSRod Evans 	 * symbols that are being translated from global to locals.  The symbol
2839*08278a5eSRod Evans 	 * pair that has been collected defines the original symbol (c_osdp),
2840*08278a5eSRod Evans 	 * which will become a local, and the new symbol (c_nsdp), which will
2841*08278a5eSRod Evans 	 * become a reference (UNDEF) for the original.
2842*08278a5eSRod Evans 	 *
2843*08278a5eSRod Evans 	 * Scan these symbol pairs looking for weak symbols, which have non-weak
2844*08278a5eSRod Evans 	 * aliases.  There is no need to translate both of these symbols to
2845*08278a5eSRod Evans 	 * locals, only the global is necessary.
2846*08278a5eSRod Evans 	 */
2847*08278a5eSRod Evans 	if (cappairs) {
2848*08278a5eSRod Evans 		Aliste		idx1;
2849*08278a5eSRod Evans 		Cap_pair	*cpp1;
2850*08278a5eSRod Evans 
2851*08278a5eSRod Evans 		for (ALIST_TRAVERSE(cappairs, idx1, cpp1)) {
2852*08278a5eSRod Evans 			Sym_desc	*sdp1 = cpp1->c_osdp;
2853*08278a5eSRod Evans 			Sym		*sym1 = sdp1->sd_sym;
2854*08278a5eSRod Evans 			uchar_t		bind1 = ELF_ST_BIND(sym1->st_info);
2855*08278a5eSRod Evans 			Aliste		idx2;
2856*08278a5eSRod Evans 			Cap_pair	*cpp2;
2857*08278a5eSRod Evans 
2858*08278a5eSRod Evans 			/*
2859*08278a5eSRod Evans 			 * If this symbol isn't weak, it's capability member is
2860*08278a5eSRod Evans 			 * retained for the creation of a local symbol.
2861*08278a5eSRod Evans 			 */
2862*08278a5eSRod Evans 			if (bind1 != STB_WEAK)
2863*08278a5eSRod Evans 				continue;
2864*08278a5eSRod Evans 
2865*08278a5eSRod Evans 			/*
2866*08278a5eSRod Evans 			 * If this is a weak symbol, traverse the capabilities
2867*08278a5eSRod Evans 			 * list again to determine if a corresponding non-weak
2868*08278a5eSRod Evans 			 * symbol exists.
2869*08278a5eSRod Evans 			 */
2870*08278a5eSRod Evans 			for (ALIST_TRAVERSE(cappairs, idx2, cpp2)) {
2871*08278a5eSRod Evans 				Sym_desc	*sdp2 = cpp2->c_osdp;
2872*08278a5eSRod Evans 				Sym		*sym2 = sdp2->sd_sym;
2873*08278a5eSRod Evans 				uchar_t		bind2 =
2874*08278a5eSRod Evans 				    ELF_ST_BIND(sym2->st_info);
2875*08278a5eSRod Evans 
2876*08278a5eSRod Evans 				if ((cpp1 == cpp2) ||
2877*08278a5eSRod Evans 				    (cpp1->c_group != cpp2->c_group) ||
2878*08278a5eSRod Evans 				    (sym1->st_value != sym2->st_value) ||
2879*08278a5eSRod Evans 				    (bind2 == STB_WEAK))
2880*08278a5eSRod Evans 					continue;
2881*08278a5eSRod Evans 
2882*08278a5eSRod Evans 				/*
2883*08278a5eSRod Evans 				 * The weak symbol (sym1) has a non-weak (sym2)
2884*08278a5eSRod Evans 				 * counterpart.  There's no point in translating
2885*08278a5eSRod Evans 				 * both of these equivalent symbols to locals.
2886*08278a5eSRod Evans 				 * Add this symbol capability alias to the
2887*08278a5eSRod Evans 				 * capabilities family information, and remove
2888*08278a5eSRod Evans 				 * the weak symbol.
2889*08278a5eSRod Evans 				 */
2890*08278a5eSRod Evans 				if (ld_cap_add_family(ofl, cpp2->c_nsdp,
2891*08278a5eSRod Evans 				    cpp1->c_nsdp, NULL, NULL) == S_ERROR)
2892*08278a5eSRod Evans 					return (S_ERROR);
2893*08278a5eSRod Evans 
2894*08278a5eSRod Evans 				free((void *)cpp1->c_osdp);
2895*08278a5eSRod Evans 				(void) alist_delete(cappairs, &idx1);
2896*08278a5eSRod Evans 			}
2897*08278a5eSRod Evans 		}
2898*08278a5eSRod Evans 
2899*08278a5eSRod Evans 		DBG_CALL(Dbg_util_nl(ofl->ofl_lml, DBG_NL_STD));
2900*08278a5eSRod Evans 
2901*08278a5eSRod Evans 		/*
2902*08278a5eSRod Evans 		 * The capability pairs information now represents all the
2903*08278a5eSRod Evans 		 * global symbols that need transforming to locals.  These
2904*08278a5eSRod Evans 		 * local symbols are renamed using their group identifiers.
2905*08278a5eSRod Evans 		 */
2906*08278a5eSRod Evans 		for (ALIST_TRAVERSE(cappairs, idx1, cpp1)) {
2907*08278a5eSRod Evans 			Sym_desc	*osdp = cpp1->c_osdp;
2908*08278a5eSRod Evans 			Objcapset	*capset;
2909*08278a5eSRod Evans 			size_t		nsize, tsize;
2910*08278a5eSRod Evans 			const char	*oname;
2911*08278a5eSRod Evans 			char		*cname, *idstr;
2912*08278a5eSRod Evans 			Sym		*csym;
2913*08278a5eSRod Evans 
2914*08278a5eSRod Evans 			/*
2915*08278a5eSRod Evans 			 * If the local symbol has not yet been translated
2916*08278a5eSRod Evans 			 * convert it to a local symbol with a name.
2917*08278a5eSRod Evans 			 */
2918*08278a5eSRod Evans 			if ((osdp->sd_flags & FLG_SY_CAP) != 0)
2919*08278a5eSRod Evans 				continue;
2920*08278a5eSRod Evans 
2921*08278a5eSRod Evans 			/*
2922*08278a5eSRod Evans 			 * As we're converting object capabilities to symbol
2923*08278a5eSRod Evans 			 * capabilities, obtain the capabilities set for this
2924*08278a5eSRod Evans 			 * object, so as to retrieve the CA_SUNW_ID value.
2925*08278a5eSRod Evans 			 */
2926*08278a5eSRod Evans 			capset = &cpp1->c_group->cg_set;
2927*08278a5eSRod Evans 
2928*08278a5eSRod Evans 			/*
2929*08278a5eSRod Evans 			 * Create a new name from the existing symbol and the
2930*08278a5eSRod Evans 			 * capabilities group identifier.  Note, the delimiter
2931*08278a5eSRod Evans 			 * between the symbol name and identifier name is hard-
2932*08278a5eSRod Evans 			 * coded here (%), so that we establish a convention
2933*08278a5eSRod Evans 			 * for transformed symbol names.
2934*08278a5eSRod Evans 			 */
2935*08278a5eSRod Evans 			oname = osdp->sd_name;
2936*08278a5eSRod Evans 
2937*08278a5eSRod Evans 			idstr = capset->oc_id.cs_str;
2938*08278a5eSRod Evans 			nsize = strlen(oname);
2939*08278a5eSRod Evans 			tsize = nsize + 1 + strlen(idstr) + 1;
2940*08278a5eSRod Evans 			if ((cname = libld_malloc(tsize)) == 0)
2941*08278a5eSRod Evans 				return (S_ERROR);
2942*08278a5eSRod Evans 
2943*08278a5eSRod Evans 			(void) strcpy(cname, oname);
2944*08278a5eSRod Evans 			cname[nsize++] = '%';
2945*08278a5eSRod Evans 			(void) strcpy(&cname[nsize], idstr);
2946*08278a5eSRod Evans 
2947*08278a5eSRod Evans 			/*
2948*08278a5eSRod Evans 			 * Allocate a new symbol table entry, transform this
2949*08278a5eSRod Evans 			 * symbol to a local, and assign the new name.
2950*08278a5eSRod Evans 			 */
2951*08278a5eSRod Evans 			if ((csym = libld_malloc(sizeof (Sym))) == NULL)
2952*08278a5eSRod Evans 				return (S_ERROR);
2953*08278a5eSRod Evans 
2954*08278a5eSRod Evans 			*csym = *osdp->sd_sym;
2955*08278a5eSRod Evans 			csym->st_info = ELF_ST_INFO(STB_LOCAL,
2956*08278a5eSRod Evans 			    ELF_ST_TYPE(osdp->sd_sym->st_info));
2957*08278a5eSRod Evans 
2958*08278a5eSRod Evans 			osdp->sd_name = cname;
2959*08278a5eSRod Evans 			osdp->sd_sym = csym;
2960*08278a5eSRod Evans 			osdp->sd_flags = FLG_SY_CAP;
2961*08278a5eSRod Evans 
2962*08278a5eSRod Evans 			/*
2963*08278a5eSRod Evans 			 * Keep track of this new local symbol.  As -z symbolcap
2964*08278a5eSRod Evans 			 * can only be used to create a relocatable object, a
2965*08278a5eSRod Evans 			 * dynamic symbol table can't exist.  Ensure there is
2966*08278a5eSRod Evans 			 * space reserved in the string table.
2967*08278a5eSRod Evans 			 */
2968*08278a5eSRod Evans 			ofl->ofl_caploclcnt++;
2969*08278a5eSRod Evans 			if (st_insert(ofl->ofl_strtab, cname) == -1)
2970*08278a5eSRod Evans 				return (S_ERROR);
2971*08278a5eSRod Evans 
2972*08278a5eSRod Evans 			DBG_CALL(Dbg_syms_cap_local(ofl, cpp1->c_ndx,
2973*08278a5eSRod Evans 			    cname, csym, osdp));
2974*08278a5eSRod Evans 
2975*08278a5eSRod Evans 			/*
2976*08278a5eSRod Evans 			 * Establish this capability pair as a family.
2977*08278a5eSRod Evans 			 */
2978*08278a5eSRod Evans 			if (ld_cap_add_family(ofl, cpp1->c_nsdp, osdp,
2979*08278a5eSRod Evans 			    cpp1->c_group, &ifl->ifl_caps->ca_syms) == S_ERROR)
2980*08278a5eSRod Evans 				return (S_ERROR);
2981*08278a5eSRod Evans 		}
2982*08278a5eSRod Evans 	}
2983*08278a5eSRod Evans 
29847c478bd9Sstevel@tonic-gate 	return (1);
298575e45495Sab 
298675e45495Sab #undef SYM_LOC_BADADDR
29877c478bd9Sstevel@tonic-gate }
29887c478bd9Sstevel@tonic-gate 
29897c478bd9Sstevel@tonic-gate /*
2990f5a18a30Srie  * Add an undefined symbol to the symbol table.  The reference originates from
2991*08278a5eSRod Evans  * the location identified by the message id (mid).  These references can
2992f5a18a30Srie  * originate from command line options such as -e, -u, -initarray, etc.
2993f5a18a30Srie  * (identified with MSG_INTL(MSG_STR_COMMAND)), or from internally generated
2994f5a18a30Srie  * TLS relocation references (identified with MSG_INTL(MSG_STR_TLSREL)).
29957c478bd9Sstevel@tonic-gate  */
29967c478bd9Sstevel@tonic-gate Sym_desc *
2997f5a18a30Srie ld_sym_add_u(const char *name, Ofl_desc *ofl, Msg mid)
29987c478bd9Sstevel@tonic-gate {
29997c478bd9Sstevel@tonic-gate 	Sym		*sym;
30006b3ba5bdSAli Bahrami 	Ifl_desc	*ifl = NULL, *_ifl;
30017c478bd9Sstevel@tonic-gate 	Sym_desc	*sdp;
30027c478bd9Sstevel@tonic-gate 	Word		hash;
300357ef7aa9SRod Evans 	Aliste		idx;
30047c478bd9Sstevel@tonic-gate 	avl_index_t	where;
3005f5a18a30Srie 	const char	*reference = MSG_INTL(mid);
30067c478bd9Sstevel@tonic-gate 
30077c478bd9Sstevel@tonic-gate 	/*
3008b02637afSrie 	 * As an optimization, determine whether we've already generated this
3009b02637afSrie 	 * reference.  If the symbol doesn't already exist we'll create it.
3010b02637afSrie 	 * Or if the symbol does exist from a different source, we'll resolve
3011b02637afSrie 	 * the conflict.
30127c478bd9Sstevel@tonic-gate 	 */
30137c478bd9Sstevel@tonic-gate 	/* LINTED */
30147c478bd9Sstevel@tonic-gate 	hash = (Word)elf_hash(name);
3015b02637afSrie 	if ((sdp = ld_sym_find(name, hash, &where, ofl)) != NULL) {
3016b02637afSrie 		if ((sdp->sd_sym->st_shndx == SHN_UNDEF) &&
3017b02637afSrie 		    (sdp->sd_file->ifl_name == reference))
3018b02637afSrie 			return (sdp);
30197c478bd9Sstevel@tonic-gate 	}
30207c478bd9Sstevel@tonic-gate 
30217c478bd9Sstevel@tonic-gate 	/*
30227c478bd9Sstevel@tonic-gate 	 * Determine whether a pseudo input file descriptor exists to represent
30237c478bd9Sstevel@tonic-gate 	 * the command line, as any global symbol needs an input file descriptor
30247c478bd9Sstevel@tonic-gate 	 * during any symbol resolution (refer to map_ifl() which provides a
30257c478bd9Sstevel@tonic-gate 	 * similar method for adding symbols from mapfiles).
30267c478bd9Sstevel@tonic-gate 	 */
302757ef7aa9SRod Evans 	for (APLIST_TRAVERSE(ofl->ofl_objs, idx, _ifl))
3028f5a18a30Srie 		if (strcmp(_ifl->ifl_name, reference) == 0) {
30297c478bd9Sstevel@tonic-gate 			ifl = _ifl;
30307c478bd9Sstevel@tonic-gate 			break;
30317c478bd9Sstevel@tonic-gate 		}
30327c478bd9Sstevel@tonic-gate 
30337c478bd9Sstevel@tonic-gate 	/*
30347c478bd9Sstevel@tonic-gate 	 * If no descriptor exists create one.
30357c478bd9Sstevel@tonic-gate 	 */
30366b3ba5bdSAli Bahrami 	if (ifl == NULL) {
303757ef7aa9SRod Evans 		if ((ifl = libld_calloc(sizeof (Ifl_desc), 1)) == NULL)
30387c478bd9Sstevel@tonic-gate 			return ((Sym_desc *)S_ERROR);
3039f5a18a30Srie 		ifl->ifl_name = reference;
30407c478bd9Sstevel@tonic-gate 		ifl->ifl_flags = FLG_IF_NEEDED | FLG_IF_FILEREF;
304157ef7aa9SRod Evans 		if ((ifl->ifl_ehdr = libld_calloc(sizeof (Ehdr), 1)) == NULL)
30427c478bd9Sstevel@tonic-gate 			return ((Sym_desc *)S_ERROR);
30437c478bd9Sstevel@tonic-gate 		ifl->ifl_ehdr->e_type = ET_REL;
30447c478bd9Sstevel@tonic-gate 
304557ef7aa9SRod Evans 		if (aplist_append(&ofl->ofl_objs, ifl, AL_CNT_OFL_OBJS) == NULL)
30467c478bd9Sstevel@tonic-gate 			return ((Sym_desc *)S_ERROR);
30477c478bd9Sstevel@tonic-gate 	}
30487c478bd9Sstevel@tonic-gate 
30497c478bd9Sstevel@tonic-gate 	/*
30507c478bd9Sstevel@tonic-gate 	 * Allocate a symbol structure and add it to the global symbol table.
30517c478bd9Sstevel@tonic-gate 	 */
30526b3ba5bdSAli Bahrami 	if ((sym = libld_calloc(sizeof (Sym), 1)) == NULL)
30537c478bd9Sstevel@tonic-gate 		return ((Sym_desc *)S_ERROR);
30547c478bd9Sstevel@tonic-gate 	sym->st_info = ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE);
30557c478bd9Sstevel@tonic-gate 	sym->st_shndx = SHN_UNDEF;
30567c478bd9Sstevel@tonic-gate 
30575aefb655Srie 	DBG_CALL(Dbg_syms_process(ofl->ofl_lml, ifl));
3058b02637afSrie 	if (sdp == NULL) {
3059b02637afSrie 		DBG_CALL(Dbg_syms_global(ofl->ofl_lml, 0, name));
3060b02637afSrie 		if ((sdp = ld_sym_enter(name, sym, hash, ifl, ofl, 0, SHN_UNDEF,
3061635216b6SRod Evans 		    0, &where)) == (Sym_desc *)S_ERROR)
3062b02637afSrie 			return ((Sym_desc *)S_ERROR);
3063b02637afSrie 	} else if (ld_sym_resolve(sdp, sym, ifl, ofl, 0,
3064b02637afSrie 	    SHN_UNDEF, 0) == S_ERROR)
3065b02637afSrie 		return ((Sym_desc *)S_ERROR);
3066b02637afSrie 
30677c478bd9Sstevel@tonic-gate 	sdp->sd_flags &= ~FLG_SY_CLEAN;
30687c478bd9Sstevel@tonic-gate 	sdp->sd_flags |= FLG_SY_CMDREF;
30697c478bd9Sstevel@tonic-gate 
30707c478bd9Sstevel@tonic-gate 	return (sdp);
30717c478bd9Sstevel@tonic-gate }
30724a8d0ea7SAli Bahrami 
30734a8d0ea7SAli Bahrami /*
30744a8d0ea7SAli Bahrami  * STT_SECTION symbols have their st_name field set to NULL, and consequently
30754a8d0ea7SAli Bahrami  * have no name. Generate a name suitable for diagnostic use for such a symbol.
30764a8d0ea7SAli Bahrami  * The resulting name will be of the form:
30774a8d0ea7SAli Bahrami  *
30784a8d0ea7SAli Bahrami  *	"XXX (section)"
30794a8d0ea7SAli Bahrami  *
30804a8d0ea7SAli Bahrami  * where XXX is the name of the section.
30814a8d0ea7SAli Bahrami  *
30824a8d0ea7SAli Bahrami  * Diagnostics for STT_SECTION symbols tend to come in clusters,
30834a8d0ea7SAli Bahrami  * so we use a static variable to retain the last string we generate. If
30844a8d0ea7SAli Bahrami  * another one comes along for the same section before some other section
30854a8d0ea7SAli Bahrami  * intervenes, we will reuse the string.
30864a8d0ea7SAli Bahrami  *
30874a8d0ea7SAli Bahrami  * entry:
3088*08278a5eSRod Evans  *	isc - Input section associated with the symbol.
30894a8d0ea7SAli Bahrami  *	fmt - NULL, or format string to use.
30904a8d0ea7SAli Bahrami  *
30914a8d0ea7SAli Bahrami  * exit:
30924a8d0ea7SAli Bahrami  *	Returns the allocated string, or NULL on allocation failure.
30934a8d0ea7SAli Bahrami  */
30944a8d0ea7SAli Bahrami const const char *
30954a8d0ea7SAli Bahrami ld_stt_section_sym_name(Is_desc *isp)
30964a8d0ea7SAli Bahrami {
30974a8d0ea7SAli Bahrami 	static const char	*last_fmt;
30984a8d0ea7SAli Bahrami 	static Is_desc		*last_isp = NULL;
30994a8d0ea7SAli Bahrami 	static char		*namestr;
31004a8d0ea7SAli Bahrami 
31014a8d0ea7SAli Bahrami 	const char		*fmt;
31024a8d0ea7SAli Bahrami 	size_t			len;
31034a8d0ea7SAli Bahrami 
31044a8d0ea7SAli Bahrami 	if ((isp != NULL) && (isp->is_name != NULL)) {
31054a8d0ea7SAli Bahrami 		fmt = (isp->is_flags & FLG_IS_GNSTRMRG) ?
31064a8d0ea7SAli Bahrami 		    MSG_INTL(MSG_STR_SECTION_MSTR) : MSG_INTL(MSG_STR_SECTION);
31074a8d0ea7SAli Bahrami 
31084a8d0ea7SAli Bahrami 		if ((last_isp == isp) && (last_fmt == fmt))
31094a8d0ea7SAli Bahrami 			return (namestr);
31104a8d0ea7SAli Bahrami 
31114a8d0ea7SAli Bahrami 		len = strlen(fmt) + strlen(isp->is_name) + 1;
31124a8d0ea7SAli Bahrami 
31134a8d0ea7SAli Bahrami 		if ((namestr = libld_malloc(len)) == 0)
31144a8d0ea7SAli Bahrami 			return (NULL);
31154a8d0ea7SAli Bahrami 		(void) snprintf(namestr, len, fmt, isp->is_name);
31164a8d0ea7SAli Bahrami 
31174a8d0ea7SAli Bahrami 		/* Remember for next time */
31184a8d0ea7SAli Bahrami 		last_fmt = fmt;
31194a8d0ea7SAli Bahrami 		last_isp = isp;
31204a8d0ea7SAli Bahrami 
31214a8d0ea7SAli Bahrami 		return (namestr);
31224a8d0ea7SAli Bahrami 	}
31234a8d0ea7SAli Bahrami 
31244a8d0ea7SAli Bahrami 	return (NULL);
31254a8d0ea7SAli Bahrami }
3126