xref: /illumos-gate/usr/src/cmd/sgs/libld/common/syms.c (revision a8facf26)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
55aefb655Srie  * Common Development and Distribution License (the "License").
65aefb655Srie  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
215aefb655Srie 
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  *	Copyright (c) 1988 AT&T
247c478bd9Sstevel@tonic-gate  *	  All Rights Reserved
257c478bd9Sstevel@tonic-gate  *
267c478bd9Sstevel@tonic-gate  *
27bf994817SAli Bahrami  * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
287c478bd9Sstevel@tonic-gate  */
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  * Symbol table management routines
327c478bd9Sstevel@tonic-gate  */
33ba2be530Sab 
34ba2be530Sab #define	ELF_TARGET_AMD64
35ba2be530Sab 
36*a8facf26SRichard Lowe /* We deliberately choose a locale unaware ctype */
37*a8facf26SRichard Lowe #include	<sys/ctype.h>
38*a8facf26SRichard Lowe 
397c478bd9Sstevel@tonic-gate #include	<stdio.h>
407c478bd9Sstevel@tonic-gate #include	<string.h>
415aefb655Srie #include	<debug.h>
427c478bd9Sstevel@tonic-gate #include	"msg.h"
437c478bd9Sstevel@tonic-gate #include	"_libld.h"
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate /*
467c478bd9Sstevel@tonic-gate  * AVL tree comparator function:
477c478bd9Sstevel@tonic-gate  *
486b3ba5bdSAli Bahrami  * The primary key is the symbol name hash with a secondary key of the symbol
496b3ba5bdSAli Bahrami  * name itself.
507c478bd9Sstevel@tonic-gate  */
517c478bd9Sstevel@tonic-gate int
525aefb655Srie ld_sym_avl_comp(const void *elem1, const void *elem2)
537c478bd9Sstevel@tonic-gate {
547c478bd9Sstevel@tonic-gate 	Sym_avlnode	*sav1 = (Sym_avlnode *)elem1;
557c478bd9Sstevel@tonic-gate 	Sym_avlnode	*sav2 = (Sym_avlnode *)elem2;
566b3ba5bdSAli Bahrami 	int		res;
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate 	res = sav1->sav_hash - sav2->sav_hash;
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate 	if (res < 0)
617c478bd9Sstevel@tonic-gate 		return (-1);
627c478bd9Sstevel@tonic-gate 	if (res > 0)
637c478bd9Sstevel@tonic-gate 		return (1);
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate 	/*
667c478bd9Sstevel@tonic-gate 	 * Hash is equal - now compare name
677c478bd9Sstevel@tonic-gate 	 */
687c478bd9Sstevel@tonic-gate 	res = strcmp(sav1->sav_name, sav2->sav_name);
697c478bd9Sstevel@tonic-gate 	if (res == 0)
707c478bd9Sstevel@tonic-gate 		return (0);
717c478bd9Sstevel@tonic-gate 	if (res > 0)
727c478bd9Sstevel@tonic-gate 		return (1);
737c478bd9Sstevel@tonic-gate 	return (-1);
747c478bd9Sstevel@tonic-gate }
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate /*
777c478bd9Sstevel@tonic-gate  * Focal point for verifying symbol names.
787c478bd9Sstevel@tonic-gate  */
79a194faf8Srie inline static const char *
805aefb655Srie string(Ofl_desc *ofl, Ifl_desc *ifl, Sym *sym, const char *strs, size_t strsize,
814a8d0ea7SAli Bahrami     int symndx, Word shndx, Word symsecndx, const char *symsecname,
82635216b6SRod Evans     const char *strsecname, sd_flag_t *flags)
837c478bd9Sstevel@tonic-gate {
846b3ba5bdSAli Bahrami 	Word	name = sym->st_name;
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate 	if (name) {
877c478bd9Sstevel@tonic-gate 		if ((ifl->ifl_flags & FLG_IF_HSTRTAB) == 0) {
881007fd6fSAli Bahrami 			ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_FIL_NOSTRTABLE),
891007fd6fSAli Bahrami 			    ifl->ifl_name, EC_WORD(symsecndx), symsecname,
901007fd6fSAli Bahrami 			    symndx, EC_XWORD(name));
916b3ba5bdSAli Bahrami 			return (NULL);
927c478bd9Sstevel@tonic-gate 		}
937c478bd9Sstevel@tonic-gate 		if (name >= (Word)strsize) {
941007fd6fSAli Bahrami 			ld_eprintf(ofl, ERR_FATAL,
955aefb655Srie 			    MSG_INTL(MSG_FIL_EXCSTRTABLE), ifl->ifl_name,
964a8d0ea7SAli Bahrami 			    EC_WORD(symsecndx), symsecname, symndx,
974a8d0ea7SAli Bahrami 			    EC_XWORD(name), strsecname, EC_XWORD(strsize));
986b3ba5bdSAli Bahrami 			return (NULL);
997c478bd9Sstevel@tonic-gate 		}
1007c478bd9Sstevel@tonic-gate 	}
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate 	/*
1037c478bd9Sstevel@tonic-gate 	 * Determine if we're dealing with a register and if so validate it.
1047c478bd9Sstevel@tonic-gate 	 * If it's a scratch register, a fabricated name will be returned.
1057c478bd9Sstevel@tonic-gate 	 */
106ba2be530Sab 	if (ld_targ.t_ms.ms_is_regsym != NULL) {
107ba2be530Sab 		const char *regname = (*ld_targ.t_ms.ms_is_regsym)(ofl, ifl,
108ba2be530Sab 		    sym, strs, symndx, shndx, symsecname, flags);
109ba2be530Sab 
110ba2be530Sab 		if (regname == (const char *)S_ERROR) {
1116b3ba5bdSAli Bahrami 			return (NULL);
112ba2be530Sab 		}
113ba2be530Sab 		if (regname)
114ba2be530Sab 			return (regname);
1157c478bd9Sstevel@tonic-gate 	}
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 	/*
1187c478bd9Sstevel@tonic-gate 	 * If this isn't a register, but we have a global symbol with a null
1197c478bd9Sstevel@tonic-gate 	 * name, we're not going to be able to hash this, search for it, or
1207c478bd9Sstevel@tonic-gate 	 * do anything interesting.  However, we've been accepting a symbol of
1217c478bd9Sstevel@tonic-gate 	 * this kind for ages now, so give the user a warning (rather than a
1227c478bd9Sstevel@tonic-gate 	 * fatal error), just in case this instance exists somewhere in the
1237c478bd9Sstevel@tonic-gate 	 * world and hasn't, as yet, been a problem.
1247c478bd9Sstevel@tonic-gate 	 */
1257c478bd9Sstevel@tonic-gate 	if ((name == 0) && (ELF_ST_BIND(sym->st_info) != STB_LOCAL)) {
1261007fd6fSAli Bahrami 		ld_eprintf(ofl, ERR_WARNING, MSG_INTL(MSG_FIL_NONAMESYM),
1274a8d0ea7SAli Bahrami 		    ifl->ifl_name, EC_WORD(symsecndx), symsecname, symndx,
1284a8d0ea7SAli Bahrami 		    EC_XWORD(name));
1297c478bd9Sstevel@tonic-gate 	}
1307c478bd9Sstevel@tonic-gate 	return (strs + name);
1317c478bd9Sstevel@tonic-gate }
1327c478bd9Sstevel@tonic-gate 
133ba2be530Sab /*
134ba2be530Sab  * For producing symbol names strings to use in error messages.
135ba2be530Sab  * If the symbol has a non-null name, then the string returned by
136ba2be530Sab  * this function is the output from demangle(), surrounded by
137ba2be530Sab  * single quotes. For null names, a descriptive string giving
138ba2be530Sab  * the symbol section and index is generated.
139ba2be530Sab  *
140ba2be530Sab  * This function uses an internal static buffer to hold the resulting
141ba2be530Sab  * string. The value returned is usable by the caller until the next
142ba2be530Sab  * call, at which point it is overwritten.
143ba2be530Sab  */
144ba2be530Sab static const char *
145ba2be530Sab demangle_symname(const char *name, const char *symtab_name, Word symndx)
146ba2be530Sab {
147ba2be530Sab #define	INIT_BUFSIZE 256
148ba2be530Sab 
1496b3ba5bdSAli Bahrami 	static char	*buf;
1506b3ba5bdSAli Bahrami 	static size_t	bufsize = 0;
151ba2be530Sab 	size_t		len;
152ba2be530Sab 	int		use_name;
153ba2be530Sab 
154ba2be530Sab 	use_name = (name != NULL) && (*name != '\0');
155ba2be530Sab 
156ba2be530Sab 	if (use_name) {
157ba2be530Sab 		name = demangle(name);
158ba2be530Sab 		len = strlen(name) + 2;   /* Include room for quotes */
159ba2be530Sab 	} else {
160ba2be530Sab 		name = MSG_ORIG(MSG_STR_EMPTY);
1614f680cc6SAli Bahrami 		len = strlen(symtab_name) + 2 + CONV_INV_BUFSIZE;
162ba2be530Sab 	}
163ba2be530Sab 	len++;			/* Null termination */
164ba2be530Sab 
165ba2be530Sab 	/* If our buffer is too small, double it until it is big enough */
166ba2be530Sab 	if (len > bufsize) {
167ba2be530Sab 		size_t	new_bufsize = bufsize;
168ba2be530Sab 		char	*new_buf;
169ba2be530Sab 
170ba2be530Sab 		if (new_bufsize == 0)
171ba2be530Sab 			new_bufsize = INIT_BUFSIZE;
172ba2be530Sab 		while (len > new_bufsize)
173ba2be530Sab 			new_bufsize *= 2;
1746b3ba5bdSAli Bahrami 		if ((new_buf = libld_malloc(new_bufsize)) == NULL)
175ba2be530Sab 			return (name);
176ba2be530Sab 		buf = new_buf;
177ba2be530Sab 		bufsize = new_bufsize;
178ba2be530Sab 	}
179ba2be530Sab 
180ba2be530Sab 	if (use_name) {
181ba2be530Sab 		(void) snprintf(buf, bufsize, MSG_ORIG(MSG_FMT_SYMNAM), name);
182ba2be530Sab 	} else {
183ba2be530Sab 		(void) snprintf(buf, bufsize, MSG_ORIG(MSG_FMT_NULLSYMNAM),
184ba2be530Sab 		    symtab_name, EC_WORD(symndx));
185ba2be530Sab 	}
186ba2be530Sab 
187ba2be530Sab 	return (buf);
188ba2be530Sab 
189ba2be530Sab #undef INIT_BUFSIZE
190ba2be530Sab }
191ba2be530Sab 
1927c478bd9Sstevel@tonic-gate /*
1937c478bd9Sstevel@tonic-gate  * Shared objects can be built that define specific symbols that can not be
1947c478bd9Sstevel@tonic-gate  * directly bound to.  These objects have a syminfo section (and an associated
1957c478bd9Sstevel@tonic-gate  * DF_1_NODIRECT dynamic flags entry).  Scan this table looking for symbols
1967c478bd9Sstevel@tonic-gate  * that can't be bound to directly, and if this files symbol is presently
1977c478bd9Sstevel@tonic-gate  * referenced, mark it so that we don't directly bind to it.
1987c478bd9Sstevel@tonic-gate  */
1997c478bd9Sstevel@tonic-gate uintptr_t
2009a411307Srie ld_sym_nodirect(Is_desc *isp, Ifl_desc *ifl, Ofl_desc *ofl)
2017c478bd9Sstevel@tonic-gate {
2027c478bd9Sstevel@tonic-gate 	Shdr		*sifshdr, *symshdr;
2037c478bd9Sstevel@tonic-gate 	Syminfo		*sifdata;
2047c478bd9Sstevel@tonic-gate 	Sym		*symdata;
2057c478bd9Sstevel@tonic-gate 	char		*strdata;
2067c478bd9Sstevel@tonic-gate 	ulong_t		cnt, _cnt;
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 	/*
2097c478bd9Sstevel@tonic-gate 	 * Get the syminfo data, and determine the number of entries.
2107c478bd9Sstevel@tonic-gate 	 */
2117c478bd9Sstevel@tonic-gate 	sifshdr = isp->is_shdr;
2127c478bd9Sstevel@tonic-gate 	sifdata = (Syminfo *)isp->is_indata->d_buf;
2137c478bd9Sstevel@tonic-gate 	cnt =  sifshdr->sh_size / sifshdr->sh_entsize;
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 	/*
2167c478bd9Sstevel@tonic-gate 	 * Get the associated symbol table.
2177c478bd9Sstevel@tonic-gate 	 */
21828bda19cSRod Evans 	if ((sifshdr->sh_link == 0) || (sifshdr->sh_link >= ifl->ifl_shnum)) {
21928bda19cSRod Evans 		/*
22028bda19cSRod Evans 		 * Broken input file
22128bda19cSRod Evans 		 */
2221007fd6fSAli Bahrami 		ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_FIL_INVSHINFO),
22328bda19cSRod Evans 		    ifl->ifl_name, isp->is_name, EC_XWORD(sifshdr->sh_link));
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 
323*a8facf26SRichard Lowe /*
324*a8facf26SRichard Lowe  * GCC sometimes emits local aliases for otherwise global symbols, such that
325*a8facf26SRichard Lowe  * it has a guaranteed way to refer to a symbol from the current object
326*a8facf26SRichard Lowe  * regardless of interposition.
327*a8facf26SRichard Lowe  *
328*a8facf26SRichard Lowe  * The only way we can match on these aliases is by them ending either
329*a8facf26SRichard Lowe  * ".localalias" or ".localalias.N" where N is any integer.
330*a8facf26SRichard Lowe  */
331*a8facf26SRichard Lowe static inline Boolean
332*a8facf26SRichard Lowe is_gcc_localalias(Sym_desc *sdp)
333*a8facf26SRichard Lowe {
334*a8facf26SRichard Lowe 	char *p;
335*a8facf26SRichard Lowe 
336*a8facf26SRichard Lowe 	if (ELF_ST_BIND(sdp->sd_sym->st_info) != STB_LOCAL)
337*a8facf26SRichard Lowe 		return (FALSE);
338*a8facf26SRichard Lowe 
339*a8facf26SRichard Lowe 	if ((p = strstr(sdp->sd_name, MSG_ORIG(MSG_SYM_LOCALALIAS))) != NULL) {
340*a8facf26SRichard Lowe 		p += MSG_SYM_LOCALALIAS_SIZE;
341*a8facf26SRichard Lowe 		switch (*p++) {
342*a8facf26SRichard Lowe 		case '\0':			/* unnumbered */
343*a8facf26SRichard Lowe 			return (TRUE);
344*a8facf26SRichard Lowe 		case '.':			/* numbered? */
345*a8facf26SRichard Lowe 			if (*p == '\0')		/* no integer */
346*a8facf26SRichard Lowe 				return (FALSE);
347*a8facf26SRichard Lowe 			while (ISDIGIT(*p))	/* skip integer */
348*a8facf26SRichard Lowe 				p++;
349*a8facf26SRichard Lowe 			if (*p != '\0')		/* non-integer chars */
350*a8facf26SRichard Lowe 				return (FALSE);
351*a8facf26SRichard Lowe 			return (TRUE);
352*a8facf26SRichard Lowe 		}
353*a8facf26SRichard Lowe 	}
354*a8facf26SRichard Lowe 
355*a8facf26SRichard Lowe 	return (FALSE);
356*a8facf26SRichard Lowe }
357*a8facf26SRichard Lowe 
3587c478bd9Sstevel@tonic-gate /*
3597c478bd9Sstevel@tonic-gate  * Enter a new symbol into the link editors internal symbol table.
3607c478bd9Sstevel@tonic-gate  * If the symbol is from an input file, information regarding the input file
3617c478bd9Sstevel@tonic-gate  * and input section is also recorded.  Otherwise (file == NULL) the symbol
3627c478bd9Sstevel@tonic-gate  * has been internally generated (ie. _etext, _edata, etc.).
3637c478bd9Sstevel@tonic-gate  */
3647c478bd9Sstevel@tonic-gate Sym_desc *
3655aefb655Srie ld_sym_enter(const char *name, Sym *osym, Word hash, Ifl_desc *ifl,
366635216b6SRod Evans     Ofl_desc *ofl, Word ndx, Word shndx, sd_flag_t sdflags, avl_index_t *where)
3677c478bd9Sstevel@tonic-gate {
3687c478bd9Sstevel@tonic-gate 	Sym_desc	*sdp;
3697c478bd9Sstevel@tonic-gate 	Sym_aux		*sap;
3707c478bd9Sstevel@tonic-gate 	Sym_avlnode	*savl;
3717c478bd9Sstevel@tonic-gate 	char		*_name;
3727c478bd9Sstevel@tonic-gate 	Sym		*nsym;
3737c478bd9Sstevel@tonic-gate 	Half		etype;
37460758829Srie 	uchar_t		vis;
3757c478bd9Sstevel@tonic-gate 	avl_index_t	_where;
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate 	/*
3787c478bd9Sstevel@tonic-gate 	 * Establish the file type.
3797c478bd9Sstevel@tonic-gate 	 */
3807c478bd9Sstevel@tonic-gate 	if (ifl)
3817c478bd9Sstevel@tonic-gate 		etype = ifl->ifl_ehdr->e_type;
3827c478bd9Sstevel@tonic-gate 	else
3837c478bd9Sstevel@tonic-gate 		etype = ET_NONE;
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate 	ofl->ofl_entercnt++;
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate 	/*
3887c478bd9Sstevel@tonic-gate 	 * Allocate a Sym Descriptor, Auxiliary Descriptor, and a Sym AVLNode -
3897c478bd9Sstevel@tonic-gate 	 * contiguously.
3907c478bd9Sstevel@tonic-gate 	 */
391635216b6SRod Evans 	if ((savl = libld_calloc(S_DROUND(sizeof (Sym_avlnode)) +
392635216b6SRod Evans 	    S_DROUND(sizeof (Sym_desc)) +
393635216b6SRod Evans 	    S_DROUND(sizeof (Sym_aux)), 1)) == NULL)
3947c478bd9Sstevel@tonic-gate 		return ((Sym_desc *)S_ERROR);
395635216b6SRod Evans 	sdp = (Sym_desc *)((uintptr_t)savl +
396635216b6SRod Evans 	    S_DROUND(sizeof (Sym_avlnode)));
397635216b6SRod Evans 	sap = (Sym_aux *)((uintptr_t)sdp +
398635216b6SRod Evans 	    S_DROUND(sizeof (Sym_desc)));
3997c478bd9Sstevel@tonic-gate 
400635216b6SRod Evans 	savl->sav_sdp = sdp;
4017c478bd9Sstevel@tonic-gate 	sdp->sd_file = ifl;
4027c478bd9Sstevel@tonic-gate 	sdp->sd_aux = sap;
4037c478bd9Sstevel@tonic-gate 	savl->sav_hash = sap->sa_hash = hash;
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate 	/*
4067c478bd9Sstevel@tonic-gate 	 * Copy the symbol table entry from the input file into the internal
4077c478bd9Sstevel@tonic-gate 	 * entry and have the symbol descriptor use it.
4087c478bd9Sstevel@tonic-gate 	 */
4097c478bd9Sstevel@tonic-gate 	sdp->sd_sym = nsym = &sap->sa_sym;
4107c478bd9Sstevel@tonic-gate 	*nsym = *osym;
4117c478bd9Sstevel@tonic-gate 	sdp->sd_shndx = shndx;
4127c478bd9Sstevel@tonic-gate 	sdp->sd_flags |= sdflags;
4137c478bd9Sstevel@tonic-gate 
4146b3ba5bdSAli Bahrami 	if ((_name = libld_malloc(strlen(name) + 1)) == NULL)
4157c478bd9Sstevel@tonic-gate 		return ((Sym_desc *)S_ERROR);
4167c478bd9Sstevel@tonic-gate 	savl->sav_name = sdp->sd_name = (const char *)strcpy(_name, name);
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate 	/*
4197c478bd9Sstevel@tonic-gate 	 * Enter Symbol in AVL tree.
4207c478bd9Sstevel@tonic-gate 	 */
4217c478bd9Sstevel@tonic-gate 	if (where == 0) {
4227c478bd9Sstevel@tonic-gate 		/* LINTED */
4237c478bd9Sstevel@tonic-gate 		Sym_avlnode	*_savl;
4247c478bd9Sstevel@tonic-gate 		/*
4255aefb655Srie 		 * If a previous ld_sym_find() hasn't initialized 'where' do it
4267c478bd9Sstevel@tonic-gate 		 * now.
4277c478bd9Sstevel@tonic-gate 		 */
4287c478bd9Sstevel@tonic-gate 		where = &_where;
4297c478bd9Sstevel@tonic-gate 		_savl = avl_find(&ofl->ofl_symavl, savl, where);
4306b3ba5bdSAli Bahrami 		assert(_savl == NULL);
4317c478bd9Sstevel@tonic-gate 	}
4327c478bd9Sstevel@tonic-gate 	avl_insert(&ofl->ofl_symavl, savl, *where);
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate 	/*
4357c478bd9Sstevel@tonic-gate 	 * Record the section index.  This is possible because the
4367c478bd9Sstevel@tonic-gate 	 * `ifl_isdesc' table is filled before we start symbol processing.
4377c478bd9Sstevel@tonic-gate 	 */
4380bc07c75Srie 	if ((sdflags & FLG_SY_SPECSEC) || (nsym->st_shndx == SHN_UNDEF))
4397c478bd9Sstevel@tonic-gate 		sdp->sd_isc = NULL;
4407c478bd9Sstevel@tonic-gate 	else {
4417c478bd9Sstevel@tonic-gate 		sdp->sd_isc = ifl->ifl_isdesc[shndx];
4427c478bd9Sstevel@tonic-gate 
4437c478bd9Sstevel@tonic-gate 		/*
4447c478bd9Sstevel@tonic-gate 		 * If this symbol is from a relocatable object, make sure that
4457c478bd9Sstevel@tonic-gate 		 * it is still associated with a section.  For example, an
4467c478bd9Sstevel@tonic-gate 		 * unknown section type (SHT_NULL) would have been rejected on
4477c478bd9Sstevel@tonic-gate 		 * input with a warning.  Here, we make the use of the symbol
4487c478bd9Sstevel@tonic-gate 		 * fatal.  A symbol descriptor is still returned, so that the
4497c478bd9Sstevel@tonic-gate 		 * caller can continue processing all symbols, and hence flush
4507c478bd9Sstevel@tonic-gate 		 * out as many error conditions as possible.
4517c478bd9Sstevel@tonic-gate 		 */
4526b3ba5bdSAli Bahrami 		if ((etype == ET_REL) && (sdp->sd_isc == NULL)) {
4531007fd6fSAli Bahrami 			ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_SYM_INVSEC),
4541007fd6fSAli Bahrami 			    name, ifl->ifl_name, EC_XWORD(shndx));
4557c478bd9Sstevel@tonic-gate 			return (sdp);
4567c478bd9Sstevel@tonic-gate 		}
4577c478bd9Sstevel@tonic-gate 	}
4587c478bd9Sstevel@tonic-gate 
4597c478bd9Sstevel@tonic-gate 	/*
4607c478bd9Sstevel@tonic-gate 	 * Mark any COMMON symbols as 'tentative'.
4617c478bd9Sstevel@tonic-gate 	 */
46254d82594Sseizo 	if (sdflags & FLG_SY_SPECSEC) {
4630bc07c75Srie 		if (nsym->st_shndx == SHN_COMMON)
46454d82594Sseizo 			sdp->sd_flags |= FLG_SY_TENTSYM;
465ba2be530Sab #if	defined(_ELF64)
466ba2be530Sab 		else if ((ld_targ.t_m.m_mach == EM_AMD64) &&
467ba2be530Sab 		    (nsym->st_shndx == SHN_X86_64_LCOMMON))
46854d82594Sseizo 			sdp->sd_flags |= FLG_SY_TENTSYM;
46954d82594Sseizo #endif
47054d82594Sseizo 	}
4717c478bd9Sstevel@tonic-gate 
4727c478bd9Sstevel@tonic-gate 	/*
47360758829Srie 	 * Establish the symbols visibility and reference.
4747c478bd9Sstevel@tonic-gate 	 */
47560758829Srie 	vis = ELF_ST_VISIBILITY(nsym->st_other);
47660758829Srie 
4777c478bd9Sstevel@tonic-gate 	if ((etype == ET_NONE) || (etype == ET_REL)) {
47860758829Srie 		switch (vis) {
47960758829Srie 		case STV_DEFAULT:
480635216b6SRod Evans 			sdp->sd_flags |= FLG_SY_DEFAULT;
48160758829Srie 			break;
48260758829Srie 		case STV_INTERNAL:
48360758829Srie 		case STV_HIDDEN:
484635216b6SRod Evans 			sdp->sd_flags |= FLG_SY_HIDDEN;
48560758829Srie 			break;
48660758829Srie 		case STV_PROTECTED:
487635216b6SRod Evans 			sdp->sd_flags |= FLG_SY_PROTECT;
48860758829Srie 			break;
48960758829Srie 		case STV_EXPORTED:
490635216b6SRod Evans 			sdp->sd_flags |= FLG_SY_EXPORT;
49160758829Srie 			break;
49260758829Srie 		case STV_SINGLETON:
493635216b6SRod Evans 			sdp->sd_flags |= (FLG_SY_SINGLE | FLG_SY_NDIR);
49428bda19cSRod Evans 			ofl->ofl_flags1 |= (FLG_OF1_NDIRECT | FLG_OF1_NGLBDIR);
49560758829Srie 			break;
49660758829Srie 		case STV_ELIMINATE:
497635216b6SRod Evans 			sdp->sd_flags |= (FLG_SY_HIDDEN | FLG_SY_ELIM);
49860758829Srie 			break;
49960758829Srie 		default:
50060758829Srie 			assert(vis <= STV_ELIMINATE);
50160758829Srie 		}
50260758829Srie 
5037c478bd9Sstevel@tonic-gate 		sdp->sd_ref = REF_REL_NEED;
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate 		/*
5069a411307Srie 		 * Under -Bnodirect, all exported interfaces that have not
5079a411307Srie 		 * explicitly been defined protected or directly bound to, are
5089a411307Srie 		 * tagged to prevent direct binding.
5097c478bd9Sstevel@tonic-gate 		 */
5100bc07c75Srie 		if ((ofl->ofl_flags1 & FLG_OF1_ALNODIR) &&
511635216b6SRod Evans 		    ((sdp->sd_flags & (FLG_SY_PROTECT | FLG_SY_DIR)) == 0) &&
5129a411307Srie 		    (nsym->st_shndx != SHN_UNDEF)) {
513635216b6SRod Evans 			sdp->sd_flags |= FLG_SY_NDIR;
5149a411307Srie 		}
5157c478bd9Sstevel@tonic-gate 	} else {
5167c478bd9Sstevel@tonic-gate 		sdp->sd_ref = REF_DYN_SEEN;
5177c478bd9Sstevel@tonic-gate 
5187c478bd9Sstevel@tonic-gate 		/*
51960758829Srie 		 * If this is a protected symbol, remember this.  Note, this
520635216b6SRod Evans 		 * state is different from the FLG_SY_PROTECT used to establish
52160758829Srie 		 * a symbol definitions visibility.  This state is used to warn
52260758829Srie 		 * against possible copy relocations against this referenced
52360758829Srie 		 * symbol.
5247c478bd9Sstevel@tonic-gate 		 */
52560758829Srie 		if (vis == STV_PROTECTED)
5267c478bd9Sstevel@tonic-gate 			sdp->sd_flags |= FLG_SY_PROT;
5277c478bd9Sstevel@tonic-gate 
5287c478bd9Sstevel@tonic-gate 		/*
52960758829Srie 		 * If this is a SINGLETON definition, then indicate the symbol
53060758829Srie 		 * can not be directly bound to, and retain the visibility.
53160758829Srie 		 * This visibility will be inherited by any references made to
53260758829Srie 		 * this symbol.
5337c478bd9Sstevel@tonic-gate 		 */
53460758829Srie 		if ((vis == STV_SINGLETON) && (nsym->st_shndx != SHN_UNDEF))
535635216b6SRod Evans 			sdp->sd_flags |= (FLG_SY_SINGLE | FLG_SY_NDIR);
5367c478bd9Sstevel@tonic-gate 
5377c478bd9Sstevel@tonic-gate 		/*
53860758829Srie 		 * If the new symbol is from a shared library and is associated
53960758829Srie 		 * with a SHT_NOBITS section then this symbol originated from a
54060758829Srie 		 * tentative symbol.
5417c478bd9Sstevel@tonic-gate 		 */
5427c478bd9Sstevel@tonic-gate 		if (sdp->sd_isc &&
5437c478bd9Sstevel@tonic-gate 		    (sdp->sd_isc->is_shdr->sh_type == SHT_NOBITS))
5447c478bd9Sstevel@tonic-gate 			sdp->sd_flags |= FLG_SY_TENTSYM;
5457c478bd9Sstevel@tonic-gate 	}
5467c478bd9Sstevel@tonic-gate 
5477c478bd9Sstevel@tonic-gate 	/*
5487c478bd9Sstevel@tonic-gate 	 * Reclassify any SHN_SUNW_IGNORE symbols to SHN_UNDEF so as to
5490bc07c75Srie 	 * simplify future processing.
5507c478bd9Sstevel@tonic-gate 	 */
5510bc07c75Srie 	if (nsym->st_shndx == SHN_SUNW_IGNORE) {
5527c478bd9Sstevel@tonic-gate 		sdp->sd_shndx = shndx = SHN_UNDEF;
553635216b6SRod Evans 		sdp->sd_flags |= (FLG_SY_REDUCED |
554635216b6SRod Evans 		    FLG_SY_HIDDEN | FLG_SY_IGNORE | FLG_SY_ELIM);
5557c478bd9Sstevel@tonic-gate 	}
5567c478bd9Sstevel@tonic-gate 
5577c478bd9Sstevel@tonic-gate 	/*
5587c478bd9Sstevel@tonic-gate 	 * If this is an undefined, or common symbol from a relocatable object
5597c478bd9Sstevel@tonic-gate 	 * determine whether it is a global or weak reference (see build_osym(),
5607c478bd9Sstevel@tonic-gate 	 * where REF_DYN_NEED definitions are returned back to undefines).
5617c478bd9Sstevel@tonic-gate 	 */
56254d82594Sseizo 	if ((etype == ET_REL) &&
56354d82594Sseizo 	    (ELF_ST_BIND(nsym->st_info) == STB_GLOBAL) &&
5640bc07c75Srie 	    ((nsym->st_shndx == SHN_UNDEF) || ((sdflags & FLG_SY_SPECSEC) &&
565ba2be530Sab #if	defined(_ELF64)
5660bc07c75Srie 	    ((nsym->st_shndx == SHN_COMMON) ||
567ba2be530Sab 	    ((ld_targ.t_m.m_mach == EM_AMD64) &&
568ba2be530Sab 	    (nsym->st_shndx == SHN_X86_64_LCOMMON))))))
56954d82594Sseizo #else
57033eb6ee1Sab 	/* BEGIN CSTYLED */
5710bc07c75Srie 	    (nsym->st_shndx == SHN_COMMON))))
57233eb6ee1Sab 	/* END CSTYLED */
57354d82594Sseizo #endif
5747c478bd9Sstevel@tonic-gate 		sdp->sd_flags |= FLG_SY_GLOBREF;
5757c478bd9Sstevel@tonic-gate 
5767c478bd9Sstevel@tonic-gate 	/*
5777c478bd9Sstevel@tonic-gate 	 * Record the input filename on the referenced or defined files list
5787c478bd9Sstevel@tonic-gate 	 * for possible later diagnostics.  The `sa_rfile' pointer contains the
5797c478bd9Sstevel@tonic-gate 	 * name of the file that first referenced this symbol and is used to
5807c478bd9Sstevel@tonic-gate 	 * generate undefined symbol diagnostics (refer to sym_undef_entry()).
5817c478bd9Sstevel@tonic-gate 	 * Note that this entry can be overridden if a reference from a
5827c478bd9Sstevel@tonic-gate 	 * relocatable object is found after a reference from a shared object
5837c478bd9Sstevel@tonic-gate 	 * (refer to sym_override()).
5847c478bd9Sstevel@tonic-gate 	 * The `sa_dfiles' list is used to maintain the list of files that
5857c478bd9Sstevel@tonic-gate 	 * define the same symbol.  This list can be used for two reasons:
5867c478bd9Sstevel@tonic-gate 	 *
58708278a5eSRod Evans 	 *  -	To save the first definition of a symbol that is not available
5887c478bd9Sstevel@tonic-gate 	 *	for this link-edit.
5897c478bd9Sstevel@tonic-gate 	 *
59008278a5eSRod Evans 	 *  -	To save all definitions of a symbol when the -m option is in
5917c478bd9Sstevel@tonic-gate 	 *	effect.  This is optional as it is used to list multiple
5927c478bd9Sstevel@tonic-gate 	 *	(interposed) definitions of a symbol (refer to ldmap_out()),
5937c478bd9Sstevel@tonic-gate 	 *	and can be quite expensive.
5947c478bd9Sstevel@tonic-gate 	 */
5950bc07c75Srie 	if (nsym->st_shndx == SHN_UNDEF) {
5967c478bd9Sstevel@tonic-gate 		sap->sa_rfile = ifl->ifl_name;
5977c478bd9Sstevel@tonic-gate 	} else {
5987c478bd9Sstevel@tonic-gate 		if (sdp->sd_ref == REF_DYN_SEEN) {
5997c478bd9Sstevel@tonic-gate 			/*
6007c478bd9Sstevel@tonic-gate 			 * A symbol is determined to be unavailable if it
6017c478bd9Sstevel@tonic-gate 			 * belongs to a version of a shared object that this
6027c478bd9Sstevel@tonic-gate 			 * user does not wish to use, or if it belongs to an
6037c478bd9Sstevel@tonic-gate 			 * implicit shared object.
6047c478bd9Sstevel@tonic-gate 			 */
6057c478bd9Sstevel@tonic-gate 			if (ifl->ifl_vercnt) {
6069039eeafSab 				Ver_index	*vip;
6077c478bd9Sstevel@tonic-gate 				Half		vndx = ifl->ifl_versym[ndx];
6087c478bd9Sstevel@tonic-gate 
6097c478bd9Sstevel@tonic-gate 				sap->sa_dverndx = vndx;
6107c478bd9Sstevel@tonic-gate 				vip = &ifl->ifl_verndx[vndx];
6117c478bd9Sstevel@tonic-gate 				if (!(vip->vi_flags & FLG_VER_AVAIL)) {
6127c478bd9Sstevel@tonic-gate 					sdp->sd_flags |= FLG_SY_NOTAVAIL;
6137c478bd9Sstevel@tonic-gate 					sap->sa_vfile = ifl->ifl_name;
6147c478bd9Sstevel@tonic-gate 				}
6157c478bd9Sstevel@tonic-gate 			}
6167c478bd9Sstevel@tonic-gate 			if (!(ifl->ifl_flags & FLG_IF_NEEDED))
6177c478bd9Sstevel@tonic-gate 				sdp->sd_flags |= FLG_SY_NOTAVAIL;
6187c478bd9Sstevel@tonic-gate 
6197c478bd9Sstevel@tonic-gate 		} else if (etype == ET_REL) {
6207c478bd9Sstevel@tonic-gate 			/*
6217c478bd9Sstevel@tonic-gate 			 * If this symbol has been obtained from a versioned
6227c478bd9Sstevel@tonic-gate 			 * input relocatable object then the new symbol must be
6237c478bd9Sstevel@tonic-gate 			 * promoted to the versioning of the output file.
6247c478bd9Sstevel@tonic-gate 			 */
6257c478bd9Sstevel@tonic-gate 			if (ifl->ifl_versym)
6265aefb655Srie 				ld_vers_promote(sdp, ndx, ifl, ofl);
6277c478bd9Sstevel@tonic-gate 		}
6287c478bd9Sstevel@tonic-gate 
6297c478bd9Sstevel@tonic-gate 		if ((ofl->ofl_flags & FLG_OF_GENMAP) &&
6307c478bd9Sstevel@tonic-gate 		    ((sdflags & FLG_SY_SPECSEC) == 0))
63157ef7aa9SRod Evans 			if (aplist_append(&sap->sa_dfiles, ifl->ifl_name,
63257ef7aa9SRod Evans 			    AL_CNT_SDP_DFILES) == NULL)
6337c478bd9Sstevel@tonic-gate 				return ((Sym_desc *)S_ERROR);
6347c478bd9Sstevel@tonic-gate 	}
6357c478bd9Sstevel@tonic-gate 
63660758829Srie 	/*
63760758829Srie 	 * Provided we're not processing a mapfile, diagnose the entered symbol.
63860758829Srie 	 * Mapfile processing requires the symbol to be updated with additional
63960758829Srie 	 * information, therefore the diagnosing of the symbol is deferred until
64060758829Srie 	 * later (see Dbg_map_symbol()).
64160758829Srie 	 */
6426b3ba5bdSAli Bahrami 	if ((ifl == NULL) || ((ifl->ifl_flags & FLG_IF_MAPFILE) == 0))
64360758829Srie 		DBG_CALL(Dbg_syms_entered(ofl, nsym, sdp));
644635216b6SRod Evans 
6457c478bd9Sstevel@tonic-gate 	return (sdp);
6467c478bd9Sstevel@tonic-gate }
6477c478bd9Sstevel@tonic-gate 
6487c478bd9Sstevel@tonic-gate /*
6497c478bd9Sstevel@tonic-gate  * Add a special symbol to the symbol table.  Takes special symbol name with
6507c478bd9Sstevel@tonic-gate  * and without underscores.  This routine is called, after all other symbol
6517c478bd9Sstevel@tonic-gate  * resolution has completed, to generate a reserved absolute symbol (the
6527c478bd9Sstevel@tonic-gate  * underscore version).  Special symbols are updated with the appropriate
653dd94ecefSrie  * values in update_osym().  If the user has already defined this symbol
6547c478bd9Sstevel@tonic-gate  * issue a warning and leave the symbol as is.  If the non-underscore symbol
6557c478bd9Sstevel@tonic-gate  * is referenced then turn it into a weak alias of the underscored symbol.
6567c478bd9Sstevel@tonic-gate  *
657635216b6SRod Evans  * The bits in sdflags_u are OR'd into the flags field of the symbol for the
658635216b6SRod Evans  * underscored symbol.
659d579eb63Sab  *
6607c478bd9Sstevel@tonic-gate  * If this is a global symbol, and it hasn't explicitly been defined as being
6617c478bd9Sstevel@tonic-gate  * directly bound to, indicate that it can't be directly bound to.
6627c478bd9Sstevel@tonic-gate  * Historically, most special symbols only have meaning to the object in which
663c1c6f601Srie  * they exist, however, they've always been global.  To ensure compatibility
664c1c6f601Srie  * with any unexpected use presently in effect, ensure these symbols don't get
6657c478bd9Sstevel@tonic-gate  * directly bound to.  Note, that establishing this state here isn't sufficient
6667c478bd9Sstevel@tonic-gate  * to create a syminfo table, only if a syminfo table is being created by some
667c1c6f601Srie  * other symbol directives will the nodirect binding be recorded.  This ensures
6687c478bd9Sstevel@tonic-gate  * we don't create syminfo sections for all objects we create, as this might add
6697c478bd9Sstevel@tonic-gate  * unnecessary bloat to users who haven't explicitly requested extra symbol
6707c478bd9Sstevel@tonic-gate  * information.
6717c478bd9Sstevel@tonic-gate  */
6727c478bd9Sstevel@tonic-gate static uintptr_t
6737c478bd9Sstevel@tonic-gate sym_add_spec(const char *name, const char *uname, Word sdaux_id,
674635216b6SRod Evans     sd_flag_t sdflags_u, sd_flag_t sdflags, Ofl_desc *ofl)
6757c478bd9Sstevel@tonic-gate {
6767c478bd9Sstevel@tonic-gate 	Sym_desc	*sdp;
677c524b4feSRichard Lowe 	Sym_desc	*usdp;
6787c478bd9Sstevel@tonic-gate 	Sym		*sym;
6797c478bd9Sstevel@tonic-gate 	Word		hash;
6807c478bd9Sstevel@tonic-gate 	avl_index_t	where;
6817c478bd9Sstevel@tonic-gate 
6827c478bd9Sstevel@tonic-gate 	/* LINTED */
6837c478bd9Sstevel@tonic-gate 	hash = (Word)elf_hash(uname);
6845aefb655Srie 	if (usdp = ld_sym_find(uname, hash, &where, ofl)) {
6857c478bd9Sstevel@tonic-gate 		/*
6867c478bd9Sstevel@tonic-gate 		 * If the underscore symbol exists and is undefined, or was
6877c478bd9Sstevel@tonic-gate 		 * defined in a shared library, convert it to a local symbol.
6887c478bd9Sstevel@tonic-gate 		 * Otherwise leave it as is and warn the user.
6897c478bd9Sstevel@tonic-gate 		 */
6907c478bd9Sstevel@tonic-gate 		if ((usdp->sd_shndx == SHN_UNDEF) ||
6917c478bd9Sstevel@tonic-gate 		    (usdp->sd_ref != REF_REL_NEED)) {
6927c478bd9Sstevel@tonic-gate 			usdp->sd_ref = REF_REL_NEED;
6937c478bd9Sstevel@tonic-gate 			usdp->sd_shndx = usdp->sd_sym->st_shndx = SHN_ABS;
694635216b6SRod Evans 			usdp->sd_flags |= FLG_SY_SPECSEC | sdflags_u;
6957c478bd9Sstevel@tonic-gate 			usdp->sd_sym->st_info =
6967c478bd9Sstevel@tonic-gate 			    ELF_ST_INFO(STB_GLOBAL, STT_OBJECT);
6977c478bd9Sstevel@tonic-gate 			usdp->sd_isc = NULL;
6987c478bd9Sstevel@tonic-gate 			usdp->sd_sym->st_size = 0;
6997c478bd9Sstevel@tonic-gate 			usdp->sd_sym->st_value = 0;
7007c478bd9Sstevel@tonic-gate 			/* LINTED */
7017c478bd9Sstevel@tonic-gate 			usdp->sd_aux->sa_symspec = (Half)sdaux_id;
7027c478bd9Sstevel@tonic-gate 
7037c478bd9Sstevel@tonic-gate 			/*
7049a411307Srie 			 * If a user hasn't specifically indicated that the
7059a411307Srie 			 * scope of this symbol be made local, then leave it
7069a411307Srie 			 * as global (ie. prevent automatic scoping).  The GOT
7079a411307Srie 			 * should be defined protected, whereas all other
7089a411307Srie 			 * special symbols are tagged as no-direct.
7097c478bd9Sstevel@tonic-gate 			 */
71008278a5eSRod Evans 			if (!SYM_IS_HIDDEN(usdp) &&
711635216b6SRod Evans 			    (sdflags & FLG_SY_DEFAULT)) {
71233eb6ee1Sab 				usdp->sd_aux->sa_overndx = VER_NDX_GLOBAL;
71333eb6ee1Sab 				if (sdaux_id == SDAUX_ID_GOT) {
714635216b6SRod Evans 					usdp->sd_flags &= ~FLG_SY_NDIR;
715635216b6SRod Evans 					usdp->sd_flags |= FLG_SY_PROTECT;
71633eb6ee1Sab 					usdp->sd_sym->st_other = STV_PROTECTED;
71733eb6ee1Sab 				} else if (
718635216b6SRod Evans 				    ((usdp->sd_flags & FLG_SY_DIR) == 0) &&
71933eb6ee1Sab 				    ((ofl->ofl_flags & FLG_OF_SYMBOLIC) == 0)) {
720635216b6SRod Evans 					usdp->sd_flags |= FLG_SY_NDIR;
72133eb6ee1Sab 				}
7227c478bd9Sstevel@tonic-gate 			}
723635216b6SRod Evans 			usdp->sd_flags |= sdflags;
7247c478bd9Sstevel@tonic-gate 
7257c478bd9Sstevel@tonic-gate 			/*
726c1c6f601Srie 			 * If the reference originated from a mapfile ensure
7277c478bd9Sstevel@tonic-gate 			 * we mark the symbol as used.
7287c478bd9Sstevel@tonic-gate 			 */
7297c478bd9Sstevel@tonic-gate 			if (usdp->sd_flags & FLG_SY_MAPREF)
7307c478bd9Sstevel@tonic-gate 				usdp->sd_flags |= FLG_SY_MAPUSED;
7317c478bd9Sstevel@tonic-gate 
7325aefb655Srie 			DBG_CALL(Dbg_syms_updated(ofl, usdp, uname));
733b6a0e2cdSRichard Lowe 		} else {
7341007fd6fSAli Bahrami 			ld_eprintf(ofl, ERR_WARNING, MSG_INTL(MSG_SYM_RESERVE),
7351007fd6fSAli Bahrami 			    uname, usdp->sd_file->ifl_name);
736b6a0e2cdSRichard Lowe 		}
7377c478bd9Sstevel@tonic-gate 	} else {
7387c478bd9Sstevel@tonic-gate 		/*
7397c478bd9Sstevel@tonic-gate 		 * If the symbol does not exist create it.
7407c478bd9Sstevel@tonic-gate 		 */
7416b3ba5bdSAli Bahrami 		if ((sym = libld_calloc(sizeof (Sym), 1)) == NULL)
7427c478bd9Sstevel@tonic-gate 			return (S_ERROR);
7437c478bd9Sstevel@tonic-gate 		sym->st_shndx = SHN_ABS;
7447c478bd9Sstevel@tonic-gate 		sym->st_info = ELF_ST_INFO(STB_GLOBAL, STT_OBJECT);
7457c478bd9Sstevel@tonic-gate 		sym->st_size = 0;
7467c478bd9Sstevel@tonic-gate 		sym->st_value = 0;
7475aefb655Srie 		DBG_CALL(Dbg_syms_created(ofl->ofl_lml, uname));
7485aefb655Srie 		if ((usdp = ld_sym_enter(uname, sym, hash, (Ifl_desc *)NULL,
749635216b6SRod Evans 		    ofl, 0, SHN_ABS, (FLG_SY_SPECSEC | sdflags_u), &where)) ==
7507c478bd9Sstevel@tonic-gate 		    (Sym_desc *)S_ERROR)
7517c478bd9Sstevel@tonic-gate 			return (S_ERROR);
7527c478bd9Sstevel@tonic-gate 		usdp->sd_ref = REF_REL_NEED;
7537c478bd9Sstevel@tonic-gate 		/* LINTED */
7547c478bd9Sstevel@tonic-gate 		usdp->sd_aux->sa_symspec = (Half)sdaux_id;
7557c478bd9Sstevel@tonic-gate 
7567c478bd9Sstevel@tonic-gate 		usdp->sd_aux->sa_overndx = VER_NDX_GLOBAL;
7579a411307Srie 
7589a411307Srie 		if (sdaux_id == SDAUX_ID_GOT) {
759635216b6SRod Evans 			usdp->sd_flags |= FLG_SY_PROTECT;
7609a411307Srie 			usdp->sd_sym->st_other = STV_PROTECTED;
761635216b6SRod Evans 		} else if ((sdflags & FLG_SY_DEFAULT) &&
7629a411307Srie 		    ((ofl->ofl_flags & FLG_OF_SYMBOLIC) == 0)) {
763635216b6SRod Evans 			usdp->sd_flags |= FLG_SY_NDIR;
7649a411307Srie 		}
765635216b6SRod Evans 		usdp->sd_flags |= sdflags;
7667c478bd9Sstevel@tonic-gate 	}
7677c478bd9Sstevel@tonic-gate 
76828bda19cSRod Evans 	if (name && (sdp = ld_sym_find(name, SYM_NOHASH, NULL, ofl)) &&
7690bc07c75Srie 	    (sdp->sd_sym->st_shndx == SHN_UNDEF)) {
7707c478bd9Sstevel@tonic-gate 		uchar_t	bind;
7717c478bd9Sstevel@tonic-gate 
7727c478bd9Sstevel@tonic-gate 		/*
7737c478bd9Sstevel@tonic-gate 		 * If the non-underscore symbol exists and is undefined
7747c478bd9Sstevel@tonic-gate 		 * convert it to be a local.  If the underscore has
7757c478bd9Sstevel@tonic-gate 		 * sa_symspec set (ie. it was created above) then simulate this
7767c478bd9Sstevel@tonic-gate 		 * as a weak alias.
7777c478bd9Sstevel@tonic-gate 		 */
7787c478bd9Sstevel@tonic-gate 		sdp->sd_ref = REF_REL_NEED;
7797c478bd9Sstevel@tonic-gate 		sdp->sd_shndx = sdp->sd_sym->st_shndx = SHN_ABS;
7807c478bd9Sstevel@tonic-gate 		sdp->sd_flags |= FLG_SY_SPECSEC;
7817c478bd9Sstevel@tonic-gate 		sdp->sd_isc = NULL;
7827c478bd9Sstevel@tonic-gate 		sdp->sd_sym->st_size = 0;
7837c478bd9Sstevel@tonic-gate 		sdp->sd_sym->st_value = 0;
7847c478bd9Sstevel@tonic-gate 		/* LINTED */
7857c478bd9Sstevel@tonic-gate 		sdp->sd_aux->sa_symspec = (Half)sdaux_id;
7867c478bd9Sstevel@tonic-gate 		if (usdp->sd_aux->sa_symspec) {
7877c478bd9Sstevel@tonic-gate 			usdp->sd_aux->sa_linkndx = 0;
7887c478bd9Sstevel@tonic-gate 			sdp->sd_aux->sa_linkndx = 0;
7897c478bd9Sstevel@tonic-gate 			bind = STB_WEAK;
7907c478bd9Sstevel@tonic-gate 		} else
7917c478bd9Sstevel@tonic-gate 			bind = STB_GLOBAL;
7927c478bd9Sstevel@tonic-gate 		sdp->sd_sym->st_info = ELF_ST_INFO(bind, STT_OBJECT);
7937c478bd9Sstevel@tonic-gate 
7947c478bd9Sstevel@tonic-gate 		/*
7959a411307Srie 		 * If a user hasn't specifically indicated the scope of this
7969a411307Srie 		 * symbol be made local then leave it as global (ie. prevent
7979a411307Srie 		 * automatic scoping).  The GOT should be defined protected,
7989a411307Srie 		 * whereas all other special symbols are tagged as no-direct.
7997c478bd9Sstevel@tonic-gate 		 */
80008278a5eSRod Evans 		if (!SYM_IS_HIDDEN(sdp) &&
801635216b6SRod Evans 		    (sdflags & FLG_SY_DEFAULT)) {
8027c478bd9Sstevel@tonic-gate 			sdp->sd_aux->sa_overndx = VER_NDX_GLOBAL;
8039a411307Srie 			if (sdaux_id == SDAUX_ID_GOT) {
804635216b6SRod Evans 				sdp->sd_flags &= ~FLG_SY_NDIR;
805635216b6SRod Evans 				sdp->sd_flags |= FLG_SY_PROTECT;
8069a411307Srie 				sdp->sd_sym->st_other = STV_PROTECTED;
807635216b6SRod Evans 			} else if (((sdp->sd_flags & FLG_SY_DIR) == 0) &&
8089a411307Srie 			    ((ofl->ofl_flags & FLG_OF_SYMBOLIC) == 0)) {
809635216b6SRod Evans 				sdp->sd_flags |= FLG_SY_NDIR;
8109a411307Srie 			}
8117c478bd9Sstevel@tonic-gate 		}
812635216b6SRod Evans 		sdp->sd_flags |= sdflags;
8137c478bd9Sstevel@tonic-gate 
8147c478bd9Sstevel@tonic-gate 		/*
815c1c6f601Srie 		 * If the reference originated from a mapfile ensure
8167c478bd9Sstevel@tonic-gate 		 * we mark the symbol as used.
8177c478bd9Sstevel@tonic-gate 		 */
8187c478bd9Sstevel@tonic-gate 		if (sdp->sd_flags & FLG_SY_MAPREF)
8197c478bd9Sstevel@tonic-gate 			sdp->sd_flags |= FLG_SY_MAPUSED;
8207c478bd9Sstevel@tonic-gate 
8215aefb655Srie 		DBG_CALL(Dbg_syms_updated(ofl, sdp, name));
8227c478bd9Sstevel@tonic-gate 	}
8237c478bd9Sstevel@tonic-gate 	return (1);
8247c478bd9Sstevel@tonic-gate }
8257c478bd9Sstevel@tonic-gate 
8267c478bd9Sstevel@tonic-gate 
8277c478bd9Sstevel@tonic-gate /*
8287c478bd9Sstevel@tonic-gate  * Undefined symbols can fall into one of four types:
8297c478bd9Sstevel@tonic-gate  *
8306b3ba5bdSAli Bahrami  *  -	the symbol is really undefined (SHN_UNDEF).
8317c478bd9Sstevel@tonic-gate  *
8326b3ba5bdSAli Bahrami  *  -	versioning has been enabled, however this symbol has not been assigned
8337c478bd9Sstevel@tonic-gate  *	to one of the defined versions.
8347c478bd9Sstevel@tonic-gate  *
8356b3ba5bdSAli Bahrami  *  -	the symbol has been defined by an implicitly supplied library, ie. one
8367c478bd9Sstevel@tonic-gate  *	which was encounted because it was NEEDED by another library, rather
837c524b4feSRichard Lowe  *	than from a command line supplied library which would become the only
8387c478bd9Sstevel@tonic-gate  *	dependency of the output file being produced.
8397c478bd9Sstevel@tonic-gate  *
8406b3ba5bdSAli Bahrami  *  -	the symbol has been defined by a version of a shared object that is
8417c478bd9Sstevel@tonic-gate  *	not permitted for this link-edit.
8427c478bd9Sstevel@tonic-gate  *
8437c478bd9Sstevel@tonic-gate  * In all cases the file who made the first reference to this symbol will have
8447c478bd9Sstevel@tonic-gate  * been recorded via the `sa_rfile' pointer.
8457c478bd9Sstevel@tonic-gate  */
8467c478bd9Sstevel@tonic-gate typedef enum {
8477c478bd9Sstevel@tonic-gate 	UNDEF,		NOVERSION,	IMPLICIT,	NOTAVAIL,
8487c478bd9Sstevel@tonic-gate 	BNDLOCAL
8497c478bd9Sstevel@tonic-gate } Type;
8507c478bd9Sstevel@tonic-gate 
8517c478bd9Sstevel@tonic-gate static const Msg format[] = {
8527c478bd9Sstevel@tonic-gate 	MSG_SYM_UND_UNDEF,		/* MSG_INTL(MSG_SYM_UND_UNDEF) */
8537c478bd9Sstevel@tonic-gate 	MSG_SYM_UND_NOVER,		/* MSG_INTL(MSG_SYM_UND_NOVER) */
8547c478bd9Sstevel@tonic-gate 	MSG_SYM_UND_IMPL,		/* MSG_INTL(MSG_SYM_UND_IMPL) */
8557c478bd9Sstevel@tonic-gate 	MSG_SYM_UND_NOTA,		/* MSG_INTL(MSG_SYM_UND_NOTA) */
8567c478bd9Sstevel@tonic-gate 	MSG_SYM_UND_BNDLOCAL		/* MSG_INTL(MSG_SYM_UND_BNDLOCAL) */
8577c478bd9Sstevel@tonic-gate };
8587c478bd9Sstevel@tonic-gate 
8591007fd6fSAli Bahrami /*
8601007fd6fSAli Bahrami  * Issue an undefined symbol message for the given symbol.
8611007fd6fSAli Bahrami  *
8621007fd6fSAli Bahrami  * entry:
8631007fd6fSAli Bahrami  *	ofl - Output descriptor
8641007fd6fSAli Bahrami  *	sdp - Undefined symbol to report
8651007fd6fSAli Bahrami  *	type - Type of undefined symbol
8661007fd6fSAli Bahrami  *	ofl_flag - One of 0, FLG_OF_FATAL, or FLG_OF_WARN.
8671007fd6fSAli Bahrami  *	undef_state - Address of variable to be initialized to 0
8681007fd6fSAli Bahrami  *		before the first call to sym_undef_entry, and passed
8691007fd6fSAli Bahrami  *		to each subsequent call. A non-zero value for *undef_state
8701007fd6fSAli Bahrami  *		indicates that this is not the first call in the series.
8711007fd6fSAli Bahrami  *
8721007fd6fSAli Bahrami  * exit:
8731007fd6fSAli Bahrami  *	If *undef_state is 0, a title is issued.
8741007fd6fSAli Bahrami  *
8751007fd6fSAli Bahrami  *	A message for the undefined symbol is issued.
8761007fd6fSAli Bahrami  *
8771007fd6fSAli Bahrami  *	If ofl_flag is non-zero, its value is OR'd into *undef_state. Otherwise,
8781007fd6fSAli Bahrami  *	all bits other than FLG_OF_FATAL and FLG_OF_WARN are set, in order to
8791007fd6fSAli Bahrami  *	provide *undef_state with a non-zero value. These other bits have
8801007fd6fSAli Bahrami  *	no meaning beyond that, and serve to ensure that *undef_state is
8811007fd6fSAli Bahrami  *	non-zero if sym_undef_entry() has been called.
8821007fd6fSAli Bahrami  */
8835aefb655Srie static void
8841007fd6fSAli Bahrami sym_undef_entry(Ofl_desc *ofl, Sym_desc *sdp, Type type, ofl_flag_t ofl_flag,
8851007fd6fSAli Bahrami     ofl_flag_t *undef_state)
8867c478bd9Sstevel@tonic-gate {
8877c478bd9Sstevel@tonic-gate 	const char	*name1, *name2, *name3;
8887c478bd9Sstevel@tonic-gate 	Ifl_desc	*ifl = sdp->sd_file;
8897c478bd9Sstevel@tonic-gate 	Sym_aux		*sap = sdp->sd_aux;
8907c478bd9Sstevel@tonic-gate 
8911007fd6fSAli Bahrami 	if (*undef_state == 0)
8921007fd6fSAli Bahrami 		ld_eprintf(ofl, ERR_NONE, MSG_INTL(MSG_SYM_FMT_UNDEF),
8931007fd6fSAli Bahrami 		    MSG_INTL(MSG_SYM_UNDEF_ITM_11),
8941007fd6fSAli Bahrami 		    MSG_INTL(MSG_SYM_UNDEF_ITM_21),
8951007fd6fSAli Bahrami 		    MSG_INTL(MSG_SYM_UNDEF_ITM_12),
8961007fd6fSAli Bahrami 		    MSG_INTL(MSG_SYM_UNDEF_ITM_22));
8971007fd6fSAli Bahrami 
8981007fd6fSAli Bahrami 	ofl->ofl_flags |= ofl_flag;
8991007fd6fSAli Bahrami 	*undef_state |= ofl_flag ? ofl_flag : ~(FLG_OF_FATAL | FLG_OF_WARN);
9007c478bd9Sstevel@tonic-gate 
9017c478bd9Sstevel@tonic-gate 	switch (type) {
9027c478bd9Sstevel@tonic-gate 	case UNDEF:
9037c478bd9Sstevel@tonic-gate 	case BNDLOCAL:
9047c478bd9Sstevel@tonic-gate 		name1 = sap->sa_rfile;
9057c478bd9Sstevel@tonic-gate 		break;
9067c478bd9Sstevel@tonic-gate 	case NOVERSION:
9077c478bd9Sstevel@tonic-gate 		name1 = ifl->ifl_name;
9087c478bd9Sstevel@tonic-gate 		break;
9097c478bd9Sstevel@tonic-gate 	case IMPLICIT:
9107c478bd9Sstevel@tonic-gate 		name1 = sap->sa_rfile;
9117c478bd9Sstevel@tonic-gate 		name2 = ifl->ifl_name;
9127c478bd9Sstevel@tonic-gate 		break;
9137c478bd9Sstevel@tonic-gate 	case NOTAVAIL:
9147c478bd9Sstevel@tonic-gate 		name1 = sap->sa_rfile;
9157c478bd9Sstevel@tonic-gate 		name2 = sap->sa_vfile;
9167c478bd9Sstevel@tonic-gate 		name3 = ifl->ifl_verndx[sap->sa_dverndx].vi_name;
9177c478bd9Sstevel@tonic-gate 		break;
9187c478bd9Sstevel@tonic-gate 	default:
9197c478bd9Sstevel@tonic-gate 		return;
9207c478bd9Sstevel@tonic-gate 	}
9217c478bd9Sstevel@tonic-gate 
9221007fd6fSAli Bahrami 	ld_eprintf(ofl, ERR_NONE, MSG_INTL(format[type]),
9235aefb655Srie 	    demangle(sdp->sd_name), name1, name2, name3);
9247c478bd9Sstevel@tonic-gate }
9257c478bd9Sstevel@tonic-gate 
926b6a0e2cdSRichard Lowe /*
927b6a0e2cdSRichard Lowe  * If an undef symbol exists naming a bound for the output section,
928b6a0e2cdSRichard Lowe  * turn it into a defined symbol with the correct value.
929b6a0e2cdSRichard Lowe  *
930b6a0e2cdSRichard Lowe  * We set an arbitrary 1KB limit on the resulting symbol names.
931b6a0e2cdSRichard Lowe  */
932b6a0e2cdSRichard Lowe static void
933b6a0e2cdSRichard Lowe sym_add_bounds(Ofl_desc *ofl, Os_desc *osp, Word bound)
934b6a0e2cdSRichard Lowe {
935b6a0e2cdSRichard Lowe 	Sym_desc *bsdp;
936b6a0e2cdSRichard Lowe 	char symn[1024];
937b6a0e2cdSRichard Lowe 	size_t nsz;
938b6a0e2cdSRichard Lowe 
939b6a0e2cdSRichard Lowe 	switch (bound) {
940b6a0e2cdSRichard Lowe 	case SDAUX_ID_SECBOUND_START:
941b6a0e2cdSRichard Lowe 		nsz = snprintf(symn, sizeof (symn), "%s%s",
942b6a0e2cdSRichard Lowe 		    MSG_ORIG(MSG_SYM_SECBOUND_START), osp->os_name);
943b6a0e2cdSRichard Lowe 		if (nsz >= sizeof (symn))
944b6a0e2cdSRichard Lowe 			return;
945b6a0e2cdSRichard Lowe 		break;
946b6a0e2cdSRichard Lowe 	case SDAUX_ID_SECBOUND_STOP:
947b6a0e2cdSRichard Lowe 		nsz = snprintf(symn, sizeof (symn), "%s%s",
948b6a0e2cdSRichard Lowe 		    MSG_ORIG(MSG_SYM_SECBOUND_STOP), osp->os_name);
949b6a0e2cdSRichard Lowe 		if (nsz >= sizeof (symn))
950b6a0e2cdSRichard Lowe 			return;
951b6a0e2cdSRichard Lowe 		break;
952b6a0e2cdSRichard Lowe 	default:
953b6a0e2cdSRichard Lowe 		assert(0);
954b6a0e2cdSRichard Lowe 	}
955b6a0e2cdSRichard Lowe 
956b6a0e2cdSRichard Lowe 	if ((bsdp = ld_sym_find(symn, SYM_NOHASH, NULL, ofl)) != NULL) {
957b6a0e2cdSRichard Lowe 		if ((bsdp->sd_shndx != SHN_UNDEF) &&
958b6a0e2cdSRichard Lowe 		    (bsdp->sd_ref == REF_REL_NEED)) {
959b6a0e2cdSRichard Lowe 			ld_eprintf(ofl, ERR_WARNING, MSG_INTL(MSG_SYM_RESERVE),
960b6a0e2cdSRichard Lowe 			    symn, bsdp->sd_file->ifl_name);
961b6a0e2cdSRichard Lowe 			return;
962b6a0e2cdSRichard Lowe 		}
963b6a0e2cdSRichard Lowe 
964b6a0e2cdSRichard Lowe 		DBG_CALL(Dbg_syms_updated(ofl, bsdp, symn));
965b6a0e2cdSRichard Lowe 
966b6a0e2cdSRichard Lowe 		bsdp->sd_aux->sa_symspec = bound;
967b6a0e2cdSRichard Lowe 		bsdp->sd_aux->sa_boundsec = osp;
968b6a0e2cdSRichard Lowe 		bsdp->sd_flags |= FLG_SY_SPECSEC;
969b6a0e2cdSRichard Lowe 		bsdp->sd_ref = REF_REL_NEED;
970b6a0e2cdSRichard Lowe 		bsdp->sd_sym->st_info = ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE);
971b6a0e2cdSRichard Lowe 		bsdp->sd_sym->st_other = STV_PROTECTED;
972b6a0e2cdSRichard Lowe 		bsdp->sd_isc = NULL;
973b6a0e2cdSRichard Lowe 		bsdp->sd_sym->st_size = 0;
974b6a0e2cdSRichard Lowe 		bsdp->sd_sym->st_value = 0;
975b6a0e2cdSRichard Lowe 		bsdp->sd_shndx = bsdp->sd_sym->st_shndx = SHN_ABS;
976b6a0e2cdSRichard Lowe 	}
977b6a0e2cdSRichard Lowe }
978b6a0e2cdSRichard Lowe 
979b6a0e2cdSRichard Lowe static Boolean
980b6a0e2cdSRichard Lowe is_cname(const char *name)
981b6a0e2cdSRichard Lowe {
982b6a0e2cdSRichard Lowe 	if (strlen(name) == strspn(name,
983b6a0e2cdSRichard Lowe 	    "abcdefghijklmnopqrstuvwxyz"
984b6a0e2cdSRichard Lowe 	    "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
985b6a0e2cdSRichard Lowe 	    "0123456789"
986b6a0e2cdSRichard Lowe 	    "_"))
987b6a0e2cdSRichard Lowe 		return (TRUE);
988b6a0e2cdSRichard Lowe 	else
989b6a0e2cdSRichard Lowe 		return (FALSE);
990b6a0e2cdSRichard Lowe }
991b6a0e2cdSRichard Lowe 
9927c478bd9Sstevel@tonic-gate /*
9937c478bd9Sstevel@tonic-gate  * At this point all symbol input processing has been completed, therefore
9947c478bd9Sstevel@tonic-gate  * complete the symbol table entries by generating any necessary internal
9957c478bd9Sstevel@tonic-gate  * symbols.
9967c478bd9Sstevel@tonic-gate  */
9977c478bd9Sstevel@tonic-gate uintptr_t
9985aefb655Srie ld_sym_spec(Ofl_desc *ofl)
9997c478bd9Sstevel@tonic-gate {
10009a411307Srie 	Sym_desc	*sdp;
1001b6a0e2cdSRichard Lowe 	Sg_desc		*sgp;
1002b6a0e2cdSRichard Lowe 
1003b6a0e2cdSRichard Lowe 	DBG_CALL(Dbg_syms_spec_title(ofl->ofl_lml));
1004b6a0e2cdSRichard Lowe 
1005b6a0e2cdSRichard Lowe 	/*
1006b6a0e2cdSRichard Lowe 	 * For each section in the output file, look for symbols named for the
1007b6a0e2cdSRichard Lowe 	 * __start/__stop patterns.  If references exist, flesh the symbols to
1008b6a0e2cdSRichard Lowe 	 * be defined.
1009b6a0e2cdSRichard Lowe 	 *
1010b6a0e2cdSRichard Lowe 	 * The symbols are given values at the same time as the other special
1011b6a0e2cdSRichard Lowe 	 * symbols.
1012b6a0e2cdSRichard Lowe 	 */
1013b6a0e2cdSRichard Lowe 	if (!(ofl->ofl_flags & FLG_OF_RELOBJ) ||
1014b6a0e2cdSRichard Lowe 	    (ofl->ofl_flags & FLG_OF_KMOD)) {
1015b6a0e2cdSRichard Lowe 		Aliste		idx1;
1016b6a0e2cdSRichard Lowe 
1017b6a0e2cdSRichard Lowe 		for (APLIST_TRAVERSE(ofl->ofl_segs, idx1, sgp)) {
1018b6a0e2cdSRichard Lowe 			Os_desc *osp;
1019b6a0e2cdSRichard Lowe 			Aliste idx2;
1020b6a0e2cdSRichard Lowe 
1021b6a0e2cdSRichard Lowe 			for (APLIST_TRAVERSE(sgp->sg_osdescs, idx2, osp)) {
1022b6a0e2cdSRichard Lowe 				if (is_cname(osp->os_name)) {
1023b6a0e2cdSRichard Lowe 					sym_add_bounds(ofl, osp,
1024b6a0e2cdSRichard Lowe 					    SDAUX_ID_SECBOUND_START);
1025b6a0e2cdSRichard Lowe 					sym_add_bounds(ofl, osp,
1026b6a0e2cdSRichard Lowe 					    SDAUX_ID_SECBOUND_STOP);
1027b6a0e2cdSRichard Lowe 				}
1028b6a0e2cdSRichard Lowe 			}
1029b6a0e2cdSRichard Lowe 		}
1030b6a0e2cdSRichard Lowe 	}
10317c478bd9Sstevel@tonic-gate 
10329a411307Srie 	if (ofl->ofl_flags & FLG_OF_RELOBJ)
10339a411307Srie 		return (1);
10347c478bd9Sstevel@tonic-gate 
10359a411307Srie 	if (sym_add_spec(MSG_ORIG(MSG_SYM_ETEXT), MSG_ORIG(MSG_SYM_ETEXT_U),
1036635216b6SRod Evans 	    SDAUX_ID_ETEXT, 0, (FLG_SY_DEFAULT | FLG_SY_EXPDEF),
103760758829Srie 	    ofl) == S_ERROR)
10389a411307Srie 		return (S_ERROR);
10399a411307Srie 	if (sym_add_spec(MSG_ORIG(MSG_SYM_EDATA), MSG_ORIG(MSG_SYM_EDATA_U),
1040635216b6SRod Evans 	    SDAUX_ID_EDATA, 0, (FLG_SY_DEFAULT | FLG_SY_EXPDEF),
104160758829Srie 	    ofl) == S_ERROR)
10429a411307Srie 		return (S_ERROR);
10439a411307Srie 	if (sym_add_spec(MSG_ORIG(MSG_SYM_END), MSG_ORIG(MSG_SYM_END_U),
1044635216b6SRod Evans 	    SDAUX_ID_END, FLG_SY_DYNSORT, (FLG_SY_DEFAULT | FLG_SY_EXPDEF),
104560758829Srie 	    ofl) == S_ERROR)
10469a411307Srie 		return (S_ERROR);
10479a411307Srie 	if (sym_add_spec(MSG_ORIG(MSG_SYM_L_END), MSG_ORIG(MSG_SYM_L_END_U),
1048635216b6SRod Evans 	    SDAUX_ID_END, 0, FLG_SY_HIDDEN, ofl) == S_ERROR)
10499a411307Srie 		return (S_ERROR);
10509a411307Srie 	if (sym_add_spec(MSG_ORIG(MSG_SYM_L_START), MSG_ORIG(MSG_SYM_L_START_U),
1051635216b6SRod Evans 	    SDAUX_ID_START, 0, FLG_SY_HIDDEN, ofl) == S_ERROR)
10529a411307Srie 		return (S_ERROR);
10537c478bd9Sstevel@tonic-gate 
10549a411307Srie 	/*
10559a411307Srie 	 * Historically we've always produced a _DYNAMIC symbol, even for
10569a411307Srie 	 * static executables (in which case its value will be 0).
10579a411307Srie 	 */
10589a411307Srie 	if (sym_add_spec(MSG_ORIG(MSG_SYM_DYNAMIC), MSG_ORIG(MSG_SYM_DYNAMIC_U),
1059635216b6SRod Evans 	    SDAUX_ID_DYN, FLG_SY_DYNSORT, (FLG_SY_DEFAULT | FLG_SY_EXPDEF),
106060758829Srie 	    ofl) == S_ERROR)
10619a411307Srie 		return (S_ERROR);
10629a411307Srie 
1063d579eb63Sab 	if (OFL_ALLOW_DYNSYM(ofl))
10649a411307Srie 		if (sym_add_spec(MSG_ORIG(MSG_SYM_PLKTBL),
10659a411307Srie 		    MSG_ORIG(MSG_SYM_PLKTBL_U), SDAUX_ID_PLT,
1066635216b6SRod Evans 		    FLG_SY_DYNSORT, (FLG_SY_DEFAULT | FLG_SY_EXPDEF),
106760758829Srie 		    ofl) == S_ERROR)
10687c478bd9Sstevel@tonic-gate 			return (S_ERROR);
10697c478bd9Sstevel@tonic-gate 
10709a411307Srie 	/*
10719a411307Srie 	 * A GOT reference will be accompanied by the associated GOT symbol.
10729a411307Srie 	 * Make sure it gets assigned the appropriate special attributes.
10739a411307Srie 	 */
10749a411307Srie 	if (((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_GOFTBL_U),
1075635216b6SRod Evans 	    SYM_NOHASH, NULL, ofl)) != NULL) && (sdp->sd_ref != REF_DYN_SEEN)) {
10769a411307Srie 		if (sym_add_spec(MSG_ORIG(MSG_SYM_GOFTBL),
1077d579eb63Sab 		    MSG_ORIG(MSG_SYM_GOFTBL_U), SDAUX_ID_GOT, FLG_SY_DYNSORT,
1078635216b6SRod Evans 		    (FLG_SY_DEFAULT | FLG_SY_EXPDEF), ofl) == S_ERROR)
10799a411307Srie 			return (S_ERROR);
10807c478bd9Sstevel@tonic-gate 	}
10819a411307Srie 
10827c478bd9Sstevel@tonic-gate 	return (1);
10837c478bd9Sstevel@tonic-gate }
10847c478bd9Sstevel@tonic-gate 
108508278a5eSRod Evans /*
108608278a5eSRod Evans  * Determine a potential capability symbol's visibility.
108708278a5eSRod Evans  *
108808278a5eSRod Evans  * The -z symbolcap option transforms an object capabilities relocatable object
108908278a5eSRod Evans  * into a symbol capabilities relocatable object.  Any global function symbols,
109008278a5eSRod Evans  * or initialized global data symbols are candidates for transforming into local
109108278a5eSRod Evans  * symbol capabilities definitions.  However, if a user indicates that a symbol
109208278a5eSRod Evans  * should be demoted to local using a mapfile, then there is no need to
109308278a5eSRod Evans  * transform the associated global symbol.
109408278a5eSRod Evans  *
109508278a5eSRod Evans  * Normally, a symbol's visibility is determined after the symbol resolution
109608278a5eSRod Evans  * process, after all symbol state has been gathered and resolved.  However,
109708278a5eSRod Evans  * for -z symbolcap, this determination is too late.  When a global symbol is
109808278a5eSRod Evans  * read from an input file we need to determine it's visibility so as to decide
109908278a5eSRod Evans  * whether to create a local or not.
110008278a5eSRod Evans  *
110108278a5eSRod Evans  * If a user has explicitly defined this symbol as having local scope within a
110208278a5eSRod Evans  * mapfile, then a symbol of the same name already exists.  However, explicit
110308278a5eSRod Evans  * local definitions are uncommon, as most mapfiles define the global symbol
110408278a5eSRod Evans  * requirements together with an auto-reduction directive '*'.  If this state
110508278a5eSRod Evans  * has been defined, then we must make sure that the new symbol isn't a type
110608278a5eSRod Evans  * that can not be demoted to local.
110708278a5eSRod Evans  */
110808278a5eSRod Evans static int
110908278a5eSRod Evans sym_cap_vis(const char *name, Word hash, Sym *sym, Ofl_desc *ofl)
111008278a5eSRod Evans {
111108278a5eSRod Evans 	Sym_desc	*sdp;
111208278a5eSRod Evans 	uchar_t		vis;
111308278a5eSRod Evans 	avl_index_t	where;
111408278a5eSRod Evans 	sd_flag_t	sdflags = 0;
111508278a5eSRod Evans 
111608278a5eSRod Evans 	/*
111708278a5eSRod Evans 	 * Determine the visibility of the new symbol.
111808278a5eSRod Evans 	 */
111908278a5eSRod Evans 	vis = ELF_ST_VISIBILITY(sym->st_other);
112008278a5eSRod Evans 	switch (vis) {
112108278a5eSRod Evans 	case STV_EXPORTED:
112208278a5eSRod Evans 		sdflags |= FLG_SY_EXPORT;
112308278a5eSRod Evans 		break;
112408278a5eSRod Evans 	case STV_SINGLETON:
112508278a5eSRod Evans 		sdflags |= FLG_SY_SINGLE;
112608278a5eSRod Evans 		break;
1127c524b4feSRichard Lowe 	case STV_HIDDEN:
1128c524b4feSRichard Lowe 		sdflags |= FLG_SY_HIDDEN;
1129c524b4feSRichard Lowe 		break;
113008278a5eSRod Evans 	}
113108278a5eSRod Evans 
113208278a5eSRod Evans 	/*
113308278a5eSRod Evans 	 * Determine whether a symbol definition already exists, and if so
113408278a5eSRod Evans 	 * obtain the visibility.
113508278a5eSRod Evans 	 */
113608278a5eSRod Evans 	if ((sdp = ld_sym_find(name, hash, &where, ofl)) != NULL)
113708278a5eSRod Evans 		sdflags |= sdp->sd_flags;
113808278a5eSRod Evans 
113908278a5eSRod Evans 	/*
114008278a5eSRod Evans 	 * Determine whether the symbol flags indicate this symbol should be
114108278a5eSRod Evans 	 * hidden.
114208278a5eSRod Evans 	 */
114308278a5eSRod Evans 	if ((ofl->ofl_flags & (FLG_OF_AUTOLCL | FLG_OF_AUTOELM)) &&
114408278a5eSRod Evans 	    ((sdflags & MSK_SY_NOAUTO) == 0))
114508278a5eSRod Evans 		sdflags |= FLG_SY_HIDDEN;
114608278a5eSRod Evans 
114708278a5eSRod Evans 	return ((sdflags & FLG_SY_HIDDEN) == 0);
114808278a5eSRod Evans }
114908278a5eSRod Evans 
11507c478bd9Sstevel@tonic-gate /*
11517c478bd9Sstevel@tonic-gate  * This routine checks to see if a symbols visibility needs to be reduced to
11527c478bd9Sstevel@tonic-gate  * either SYMBOLIC or LOCAL.  This routine can be called from either
11537c478bd9Sstevel@tonic-gate  * reloc_init() or sym_validate().
11547c478bd9Sstevel@tonic-gate  */
11557c478bd9Sstevel@tonic-gate void
11565aefb655Srie ld_sym_adjust_vis(Sym_desc *sdp, Ofl_desc *ofl)
11577c478bd9Sstevel@tonic-gate {
115844bac77bSrie 	ofl_flag_t	oflags = ofl->ofl_flags;
115944bac77bSrie 	Sym		*sym = sdp->sd_sym;
11607c478bd9Sstevel@tonic-gate 
11610bc07c75Srie 	if ((sdp->sd_ref == REF_REL_NEED) &&
11620bc07c75Srie 	    (sdp->sd_sym->st_shndx != SHN_UNDEF)) {
11637c478bd9Sstevel@tonic-gate 		/*
116460758829Srie 		 * If auto-reduction/elimination is enabled, reduce any
116508278a5eSRod Evans 		 * non-versioned, and non-local capabilities global symbols.
116660758829Srie 		 * A symbol is a candidate for auto-reduction/elimination if:
116760758829Srie 		 *
116828bda19cSRod Evans 		 *  -	the symbol wasn't explicitly defined within a mapfile
116960758829Srie 		 *	(in which case all the necessary state has been applied
117060758829Srie 		 *	to the symbol), or
117128bda19cSRod Evans 		 *  -	the symbol isn't one of the family of reserved
117260758829Srie 		 *	special symbols (ie. _end, _etext, etc.), or
117328bda19cSRod Evans 		 *  -	the symbol isn't a SINGLETON, or
117428bda19cSRod Evans 		 *  -	the symbol wasn't explicitly defined within a version
117560758829Srie 		 *	definition associated with an input relocatable object.
117660758829Srie 		 *
11777c478bd9Sstevel@tonic-gate 		 * Indicate that the symbol has been reduced as it may be
11787c478bd9Sstevel@tonic-gate 		 * necessary to print these symbols later.
11797c478bd9Sstevel@tonic-gate 		 */
118044bac77bSrie 		if ((oflags & (FLG_OF_AUTOLCL | FLG_OF_AUTOELM)) &&
1181635216b6SRod Evans 		    ((sdp->sd_flags & MSK_SY_NOAUTO) == 0)) {
1182635216b6SRod Evans 			if ((sdp->sd_flags & FLG_SY_HIDDEN) == 0) {
1183635216b6SRod Evans 				sdp->sd_flags |=
1184635216b6SRod Evans 				    (FLG_SY_REDUCED | FLG_SY_HIDDEN);
118560758829Srie 			}
11867c478bd9Sstevel@tonic-gate 
118744bac77bSrie 			if (oflags & (FLG_OF_REDLSYM | FLG_OF_AUTOELM)) {
1188635216b6SRod Evans 				sdp->sd_flags |= FLG_SY_ELIM;
118960758829Srie 				sym->st_other = STV_ELIMINATE |
119060758829Srie 				    (sym->st_other & ~MSK_SYM_VISIBILITY);
119160758829Srie 			} else if (ELF_ST_VISIBILITY(sym->st_other) !=
119260758829Srie 			    STV_INTERNAL)
119360758829Srie 				sym->st_other = STV_HIDDEN |
119460758829Srie 				    (sym->st_other & ~MSK_SYM_VISIBILITY);
11957c478bd9Sstevel@tonic-gate 		}
11967c478bd9Sstevel@tonic-gate 
11977c478bd9Sstevel@tonic-gate 		/*
11989a411307Srie 		 * If -Bsymbolic is in effect, and the symbol hasn't explicitly
11999a411307Srie 		 * been defined nodirect (via a mapfile), then bind the global
12009a411307Srie 		 * symbol symbolically and assign the STV_PROTECTED visibility
12017c478bd9Sstevel@tonic-gate 		 * attribute.
12027c478bd9Sstevel@tonic-gate 		 */
12037c478bd9Sstevel@tonic-gate 		if ((oflags & FLG_OF_SYMBOLIC) &&
1204635216b6SRod Evans 		    ((sdp->sd_flags & (FLG_SY_HIDDEN | FLG_SY_NDIR)) == 0)) {
1205635216b6SRod Evans 			sdp->sd_flags |= FLG_SY_PROTECT;
12067c478bd9Sstevel@tonic-gate 			if (ELF_ST_VISIBILITY(sym->st_other) == STV_DEFAULT)
12077c478bd9Sstevel@tonic-gate 				sym->st_other = STV_PROTECTED |
12087c478bd9Sstevel@tonic-gate 				    (sym->st_other & ~MSK_SYM_VISIBILITY);
12097c478bd9Sstevel@tonic-gate 		}
12107c478bd9Sstevel@tonic-gate 	}
12117c478bd9Sstevel@tonic-gate 
12127c478bd9Sstevel@tonic-gate 	/*
12137c478bd9Sstevel@tonic-gate 	 * Indicate that this symbol has had it's visibility checked so that
12147c478bd9Sstevel@tonic-gate 	 * we don't need to do this investigation again.
12157c478bd9Sstevel@tonic-gate 	 */
12167c478bd9Sstevel@tonic-gate 	sdp->sd_flags |= FLG_SY_VISIBLE;
12177c478bd9Sstevel@tonic-gate }
12187c478bd9Sstevel@tonic-gate 
1219c1c6f601Srie /*
1220c1c6f601Srie  * Make sure a symbol definition is local to the object being built.
1221c1c6f601Srie  */
122257ef7aa9SRod Evans inline static int
1223c1c6f601Srie ensure_sym_local(Ofl_desc *ofl, Sym_desc *sdp, const char *str)
1224c1c6f601Srie {
1225c1c6f601Srie 	if (sdp->sd_sym->st_shndx == SHN_UNDEF) {
1226c1c6f601Srie 		if (str) {
12271007fd6fSAli Bahrami 			ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_SYM_UNDEF),
12281007fd6fSAli Bahrami 			    str, demangle((char *)sdp->sd_name));
1229c1c6f601Srie 		}
1230c1c6f601Srie 		return (1);
1231c1c6f601Srie 	}
1232c1c6f601Srie 	if (sdp->sd_ref != REF_REL_NEED) {
1233c1c6f601Srie 		if (str) {
12341007fd6fSAli Bahrami 			ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_SYM_EXTERN),
12351007fd6fSAli Bahrami 			    str, demangle((char *)sdp->sd_name),
1236c1c6f601Srie 			    sdp->sd_file->ifl_name);
1237c1c6f601Srie 		}
1238c1c6f601Srie 		return (1);
1239c1c6f601Srie 	}
1240c1c6f601Srie 
1241c1c6f601Srie 	sdp->sd_flags |= FLG_SY_UPREQD;
1242c1c6f601Srie 	if (sdp->sd_isc) {
1243c1c6f601Srie 		sdp->sd_isc->is_flags |= FLG_IS_SECTREF;
1244c1c6f601Srie 		sdp->sd_isc->is_file->ifl_flags |= FLG_IF_FILEREF;
1245c1c6f601Srie 	}
1246c1c6f601Srie 	return (0);
1247c1c6f601Srie }
1248c1c6f601Srie 
1249c1c6f601Srie /*
1250c1c6f601Srie  * Make sure all the symbol definitions required for initarray, finiarray, or
1251c1c6f601Srie  * preinitarray's are local to the object being built.
1252c1c6f601Srie  */
1253c1c6f601Srie static int
125457ef7aa9SRod Evans ensure_array_local(Ofl_desc *ofl, APlist *apl, const char *str)
1255c1c6f601Srie {
125657ef7aa9SRod Evans 	Aliste		idx;
1257c1c6f601Srie 	Sym_desc	*sdp;
1258c1c6f601Srie 	int		ret = 0;
1259c1c6f601Srie 
126057ef7aa9SRod Evans 	for (APLIST_TRAVERSE(apl, idx, sdp))
1261c1c6f601Srie 		ret += ensure_sym_local(ofl, sdp, str);
1262c1c6f601Srie 
1263c1c6f601Srie 	return (ret);
1264c1c6f601Srie }
1265c1c6f601Srie 
12667c478bd9Sstevel@tonic-gate /*
12677c478bd9Sstevel@tonic-gate  * After all symbol table input processing has been finished, and all relocation
12687c478bd9Sstevel@tonic-gate  * counting has been carried out (ie. no more symbols will be read, generated,
12697c478bd9Sstevel@tonic-gate  * or modified), validate and count the relevant entries:
12707c478bd9Sstevel@tonic-gate  *
127108278a5eSRod Evans  *  -	check and print any undefined symbols remaining.  Note that if a symbol
1272c524b4feSRichard Lowe  *	has been defined by virtue of the inclusion of	an implicit shared
127308278a5eSRod Evans  *	library, it is still classed as undefined.
12747c478bd9Sstevel@tonic-gate  *
127508278a5eSRod Evans  *  -	count the number of global needed symbols together with the size of
127608278a5eSRod Evans  *	their associated name strings (if scoping has been indicated these
127708278a5eSRod Evans  *	symbols may be reduced to locals).
12787c478bd9Sstevel@tonic-gate  *
127908278a5eSRod Evans  *  -	establish the size and alignment requirements for the global .bss
1280b6a0e2cdSRichard Lowe  *	section (the alignment of this section is based on the first symbol
128108278a5eSRod Evans  *	that it will contain).
12827c478bd9Sstevel@tonic-gate  */
12837c478bd9Sstevel@tonic-gate uintptr_t
12845aefb655Srie ld_sym_validate(Ofl_desc *ofl)
12857c478bd9Sstevel@tonic-gate {
12867c478bd9Sstevel@tonic-gate 	Sym_avlnode	*sav;
12877c478bd9Sstevel@tonic-gate 	Sym_desc	*sdp;
12887c478bd9Sstevel@tonic-gate 	Sym		*sym;
12891d9df23bSab 	ofl_flag_t	oflags = ofl->ofl_flags;
12901d9df23bSab 	ofl_flag_t	undef = 0, needed = 0, verdesc = 0;
12917c478bd9Sstevel@tonic-gate 	Xword		bssalign = 0, tlsalign = 0;
1292e64d0ff9SAli Bahrami 	Boolean		need_bss, need_tlsbss;
12937c478bd9Sstevel@tonic-gate 	Xword		bsssize = 0, tlssize = 0;
1294ba2be530Sab #if	defined(_ELF64)
129554d82594Sseizo 	Xword		lbssalign = 0, lbsssize = 0;
1296e64d0ff9SAli Bahrami 	Boolean		need_lbss;
129754d82594Sseizo #endif
129808278a5eSRod Evans 	int		ret, allow_ldynsym;
1299d579eb63Sab 	uchar_t		type;
13001007fd6fSAli Bahrami 	ofl_flag_t	undef_state = 0;
13017c478bd9Sstevel@tonic-gate 
13022017c965SRod Evans 	DBG_CALL(Dbg_basic_validate(ofl->ofl_lml));
13032017c965SRod Evans 
1304e64d0ff9SAli Bahrami 	/*
1305e64d0ff9SAli Bahrami 	 * The need_XXX booleans are used to determine whether we need to
1306e64d0ff9SAli Bahrami 	 * create each type of bss section. We used to create these sections
1307e64d0ff9SAli Bahrami 	 * if the sum of the required sizes for each type were non-zero.
1308e64d0ff9SAli Bahrami 	 * However, it is possible for a compiler to generate COMMON variables
1309e64d0ff9SAli Bahrami 	 * of zero-length and this tricks that logic --- even zero-length
1310e64d0ff9SAli Bahrami 	 * symbols need an output section.
1311e64d0ff9SAli Bahrami 	 */
1312e64d0ff9SAli Bahrami 	need_bss = need_tlsbss = FALSE;
1313e64d0ff9SAli Bahrami #if	defined(_ELF64)
1314e64d0ff9SAli Bahrami 	need_lbss = FALSE;
1315e64d0ff9SAli Bahrami #endif
1316e64d0ff9SAli Bahrami 
13177c478bd9Sstevel@tonic-gate 	/*
13181007fd6fSAli Bahrami 	 * Determine how undefined symbols are handled:
13191007fd6fSAli Bahrami 	 *
13201007fd6fSAli Bahrami 	 * fatal:
13211007fd6fSAli Bahrami 	 *	If this link-edit calls for no undefined symbols to remain
13221007fd6fSAli Bahrami 	 *	(this is the default case when generating an executable but
13231007fd6fSAli Bahrami 	 *	can be enforced for any object using -z defs), a fatal error
13241007fd6fSAli Bahrami 	 *	condition will be indicated.
13251007fd6fSAli Bahrami 	 *
13261007fd6fSAli Bahrami 	 * warning:
13271007fd6fSAli Bahrami 	 *	If we're creating a shared object, and either the -Bsymbolic
13281007fd6fSAli Bahrami 	 *	flag is set, or the user has turned on the -z guidance feature,
13291007fd6fSAli Bahrami 	 *	then a non-fatal warning is issued for each symbol.
13307c478bd9Sstevel@tonic-gate 	 *
13311007fd6fSAli Bahrami 	 * ignore:
13321007fd6fSAli Bahrami 	 *	In all other cases, undefined symbols are quietly allowed.
13337c478bd9Sstevel@tonic-gate 	 */
13341007fd6fSAli Bahrami 	if (oflags & FLG_OF_NOUNDEF) {
13357c478bd9Sstevel@tonic-gate 		undef = FLG_OF_FATAL;
13361007fd6fSAli Bahrami 	} else if (oflags & FLG_OF_SHAROBJ) {
13371007fd6fSAli Bahrami 		if ((oflags & FLG_OF_SYMBOLIC) ||
13381007fd6fSAli Bahrami 		    OFL_GUIDANCE(ofl, FLG_OFG_NO_DEFS))
13391007fd6fSAli Bahrami 			undef = FLG_OF_WARN;
13401007fd6fSAli Bahrami 	}
13417c478bd9Sstevel@tonic-gate 
13427c478bd9Sstevel@tonic-gate 	/*
13437c478bd9Sstevel@tonic-gate 	 * If the symbol is referenced from an implicitly included shared object
13447c478bd9Sstevel@tonic-gate 	 * (ie. it's not on the NEEDED list) then the symbol is also classified
13457c478bd9Sstevel@tonic-gate 	 * as undefined and a fatal error condition will be indicated.
13467c478bd9Sstevel@tonic-gate 	 */
13477c478bd9Sstevel@tonic-gate 	if ((oflags & FLG_OF_NOUNDEF) || !(oflags & FLG_OF_SHAROBJ))
13487c478bd9Sstevel@tonic-gate 		needed = FLG_OF_FATAL;
13491007fd6fSAli Bahrami 	else if ((oflags & FLG_OF_SHAROBJ) &&
13501007fd6fSAli Bahrami 	    OFL_GUIDANCE(ofl, FLG_OFG_NO_DEFS))
13511007fd6fSAli Bahrami 		needed = FLG_OF_WARN;
13527c478bd9Sstevel@tonic-gate 
13537c478bd9Sstevel@tonic-gate 	/*
135428bda19cSRod Evans 	 * If the output image is being versioned, then all symbol definitions
135528bda19cSRod Evans 	 * must be associated with a version.  Any symbol that isn't associated
135628bda19cSRod Evans 	 * with a version is classified as undefined, and a fatal error
135728bda19cSRod Evans 	 * condition is indicated.
13587c478bd9Sstevel@tonic-gate 	 */
13597c478bd9Sstevel@tonic-gate 	if ((oflags & FLG_OF_VERDEF) && (ofl->ofl_vercnt > VER_NDX_GLOBAL))
13607c478bd9Sstevel@tonic-gate 		verdesc = FLG_OF_FATAL;
13617c478bd9Sstevel@tonic-gate 
13629039eeafSab 	allow_ldynsym = OFL_ALLOW_LDYNSYM(ofl);
1363d579eb63Sab 
1364d579eb63Sab 	if (allow_ldynsym) {
1365d579eb63Sab 		/*
1366d579eb63Sab 		 * Normally, we disallow symbols with 0 size from appearing
1367d579eb63Sab 		 * in a dyn[sym|tls]sort section. However, there are some
1368d579eb63Sab 		 * symbols that serve special purposes that we want to exempt
1369d579eb63Sab 		 * from this rule. Look them up, and set their
1370d579eb63Sab 		 * FLG_SY_DYNSORT flag.
1371d579eb63Sab 		 */
1372d579eb63Sab 		static const char *special[] = {
1373d579eb63Sab 			MSG_ORIG(MSG_SYM_INIT_U),	/* _init */
1374d579eb63Sab 			MSG_ORIG(MSG_SYM_FINI_U),	/* _fini */
1375d579eb63Sab 			MSG_ORIG(MSG_SYM_START),	/* _start */
1376d579eb63Sab 			NULL
1377d579eb63Sab 		};
1378d579eb63Sab 		int i;
1379d579eb63Sab 
1380d579eb63Sab 		for (i = 0; special[i] != NULL; i++) {
1381d579eb63Sab 			if (((sdp = ld_sym_find(special[i],
138228bda19cSRod Evans 			    SYM_NOHASH, NULL, ofl)) != NULL) &&
1383d579eb63Sab 			    (sdp->sd_sym->st_size == 0)) {
1384d579eb63Sab 				if (ld_sym_copy(sdp) == S_ERROR)
1385d579eb63Sab 					return (S_ERROR);
1386d579eb63Sab 				sdp->sd_flags |= FLG_SY_DYNSORT;
1387d579eb63Sab 			}
1388d579eb63Sab 		}
1389d579eb63Sab 	}
1390d579eb63Sab 
13917c478bd9Sstevel@tonic-gate 	/*
13927c478bd9Sstevel@tonic-gate 	 * Collect and validate the globals from the internal symbol table.
13937c478bd9Sstevel@tonic-gate 	 */
13947c478bd9Sstevel@tonic-gate 	for (sav = avl_first(&ofl->ofl_symavl); sav;
13957c478bd9Sstevel@tonic-gate 	    sav = AVL_NEXT(&ofl->ofl_symavl, sav)) {
139660758829Srie 		Is_desc		*isp;
13970bc07c75Srie 		int		undeferr = 0;
139860758829Srie 		uchar_t		vis;
13997c478bd9Sstevel@tonic-gate 
1400635216b6SRod Evans 		sdp = sav->sav_sdp;
14017c478bd9Sstevel@tonic-gate 
14027c478bd9Sstevel@tonic-gate 		/*
14031007fd6fSAli Bahrami 		 * If undefined symbols are allowed, and we're not being
14041007fd6fSAli Bahrami 		 * asked to supply guidance, ignore any symbols that are
14057c478bd9Sstevel@tonic-gate 		 * not needed.
14067c478bd9Sstevel@tonic-gate 		 */
14077c478bd9Sstevel@tonic-gate 		if (!(oflags & FLG_OF_NOUNDEF) &&
14081007fd6fSAli Bahrami 		    !OFL_GUIDANCE(ofl, FLG_OFG_NO_DEFS) &&
14097c478bd9Sstevel@tonic-gate 		    (sdp->sd_ref == REF_DYN_SEEN))
14107c478bd9Sstevel@tonic-gate 			continue;
14117c478bd9Sstevel@tonic-gate 
14127c478bd9Sstevel@tonic-gate 		/*
14137c478bd9Sstevel@tonic-gate 		 * If the symbol originates from an external or parent mapfile
14147c478bd9Sstevel@tonic-gate 		 * reference and hasn't been matched to a reference from a
14157c478bd9Sstevel@tonic-gate 		 * relocatable object, ignore it.
14167c478bd9Sstevel@tonic-gate 		 */
14177c478bd9Sstevel@tonic-gate 		if ((sdp->sd_flags & (FLG_SY_EXTERN | FLG_SY_PARENT)) &&
14187c478bd9Sstevel@tonic-gate 		    ((sdp->sd_flags & FLG_SY_MAPUSED) == 0)) {
14197c478bd9Sstevel@tonic-gate 			sdp->sd_flags |= FLG_SY_INVALID;
14207c478bd9Sstevel@tonic-gate 			continue;
14217c478bd9Sstevel@tonic-gate 		}
14227c478bd9Sstevel@tonic-gate 
14237c478bd9Sstevel@tonic-gate 		sym = sdp->sd_sym;
1424d579eb63Sab 		type = ELF_ST_TYPE(sym->st_info);
14257c478bd9Sstevel@tonic-gate 
14267c478bd9Sstevel@tonic-gate 		/*
14277c478bd9Sstevel@tonic-gate 		 * Sanity check TLS.
14287c478bd9Sstevel@tonic-gate 		 */
1429d579eb63Sab 		if ((type == STT_TLS) && (sym->st_size != 0) &&
1430d579eb63Sab 		    (sym->st_shndx != SHN_UNDEF) &&
14310bc07c75Srie 		    (sym->st_shndx != SHN_COMMON)) {
143260758829Srie 			Is_desc		*isp = sdp->sd_isc;
143360758829Srie 			Ifl_desc	*ifl = sdp->sd_file;
14347c478bd9Sstevel@tonic-gate 
14356b3ba5bdSAli Bahrami 			if ((isp == NULL) || (isp->is_shdr == NULL) ||
14367c478bd9Sstevel@tonic-gate 			    ((isp->is_shdr->sh_flags & SHF_TLS) == 0)) {
14371007fd6fSAli Bahrami 				ld_eprintf(ofl, ERR_FATAL,
14385aefb655Srie 				    MSG_INTL(MSG_SYM_TLS),
14397c478bd9Sstevel@tonic-gate 				    demangle(sdp->sd_name), ifl->ifl_name);
14407c478bd9Sstevel@tonic-gate 				continue;
14417c478bd9Sstevel@tonic-gate 			}
14427c478bd9Sstevel@tonic-gate 		}
14437c478bd9Sstevel@tonic-gate 
14447c478bd9Sstevel@tonic-gate 		if ((sdp->sd_flags & FLG_SY_VISIBLE) == 0)
14455aefb655Srie 			ld_sym_adjust_vis(sdp, ofl);
14467c478bd9Sstevel@tonic-gate 
14477c478bd9Sstevel@tonic-gate 		if ((sdp->sd_flags & FLG_SY_REDUCED) &&
14487c478bd9Sstevel@tonic-gate 		    (oflags & FLG_OF_PROCRED)) {
14495aefb655Srie 			DBG_CALL(Dbg_syms_reduce(ofl, DBG_SYM_REDUCE_GLOBAL,
14505aefb655Srie 			    sdp, 0, 0));
14517c478bd9Sstevel@tonic-gate 		}
14527c478bd9Sstevel@tonic-gate 
145360758829Srie 		/*
145460758829Srie 		 * Record any STV_SINGLETON existence.
145560758829Srie 		 */
145660758829Srie 		if ((vis = ELF_ST_VISIBILITY(sym->st_other)) == STV_SINGLETON)
145760758829Srie 			ofl->ofl_dtflags_1 |= DF_1_SINGLETON;
145860758829Srie 
14597c478bd9Sstevel@tonic-gate 		/*
14607c478bd9Sstevel@tonic-gate 		 * If building a shared object or executable, and this is a
14617c478bd9Sstevel@tonic-gate 		 * non-weak UNDEF symbol with reduced visibility (STV_*), then
14627c478bd9Sstevel@tonic-gate 		 * give a fatal error.
14637c478bd9Sstevel@tonic-gate 		 */
146460758829Srie 		if (((oflags & FLG_OF_RELOBJ) == 0) &&
14650bc07c75Srie 		    (sym->st_shndx == SHN_UNDEF) &&
14667c478bd9Sstevel@tonic-gate 		    (ELF_ST_BIND(sym->st_info) != STB_WEAK)) {
146760758829Srie 			if (vis && (vis != STV_SINGLETON)) {
14681007fd6fSAli Bahrami 				sym_undef_entry(ofl, sdp, BNDLOCAL,
14691007fd6fSAli Bahrami 				    FLG_OF_FATAL, &undef_state);
147060758829Srie 				continue;
147160758829Srie 			}
14727c478bd9Sstevel@tonic-gate 		}
14737c478bd9Sstevel@tonic-gate 
14747c478bd9Sstevel@tonic-gate 		/*
14757c478bd9Sstevel@tonic-gate 		 * If this symbol is defined in a non-allocatable section,
14767c478bd9Sstevel@tonic-gate 		 * reduce it to local symbol.
14777c478bd9Sstevel@tonic-gate 		 */
14787c478bd9Sstevel@tonic-gate 		if (((isp = sdp->sd_isc) != 0) && isp->is_shdr &&
14797c478bd9Sstevel@tonic-gate 		    ((isp->is_shdr->sh_flags & SHF_ALLOC) == 0)) {
1480635216b6SRod Evans 			sdp->sd_flags |= (FLG_SY_REDUCED | FLG_SY_HIDDEN);
14817c478bd9Sstevel@tonic-gate 		}
14827c478bd9Sstevel@tonic-gate 
14837c478bd9Sstevel@tonic-gate 		/*
14847c478bd9Sstevel@tonic-gate 		 * If this symbol originated as a SHN_SUNW_IGNORE, it will have
14857c478bd9Sstevel@tonic-gate 		 * been processed as an SHN_UNDEF.  Return the symbol to its
14867c478bd9Sstevel@tonic-gate 		 * original index for validation, and propagation to the output
14877c478bd9Sstevel@tonic-gate 		 * file.
14887c478bd9Sstevel@tonic-gate 		 */
1489635216b6SRod Evans 		if (sdp->sd_flags & FLG_SY_IGNORE)
14900bc07c75Srie 			sdp->sd_shndx = SHN_SUNW_IGNORE;
14917c478bd9Sstevel@tonic-gate 
14927c478bd9Sstevel@tonic-gate 		if (undef) {
14937c478bd9Sstevel@tonic-gate 			/*
14943b41b08bSab 			 * If a non-weak reference remains undefined, or if a
14957c478bd9Sstevel@tonic-gate 			 * mapfile reference is not bound to the relocatable
14967c478bd9Sstevel@tonic-gate 			 * objects that make up the object being built, we have
14977c478bd9Sstevel@tonic-gate 			 * a fatal error.
14987c478bd9Sstevel@tonic-gate 			 *
14997c478bd9Sstevel@tonic-gate 			 * The exceptions are symbols which are defined to be
15007c478bd9Sstevel@tonic-gate 			 * found in the parent (FLG_SY_PARENT), which is really
15017c478bd9Sstevel@tonic-gate 			 * only meaningful for direct binding, or are defined
15027c478bd9Sstevel@tonic-gate 			 * external (FLG_SY_EXTERN) so as to suppress -zdefs
15037c478bd9Sstevel@tonic-gate 			 * errors.
15047c478bd9Sstevel@tonic-gate 			 *
15057c478bd9Sstevel@tonic-gate 			 * Register symbols are always allowed to be UNDEF.
15067c478bd9Sstevel@tonic-gate 			 *
15077c478bd9Sstevel@tonic-gate 			 * Note that we don't include references created via -u
15087c478bd9Sstevel@tonic-gate 			 * in the same shared object binding test.  This is for
15097c478bd9Sstevel@tonic-gate 			 * backward compatibility, in that a number of archive
15107c478bd9Sstevel@tonic-gate 			 * makefile rules used -u to cause archive extraction.
15117c478bd9Sstevel@tonic-gate 			 * These same rules have been cut and pasted to apply
15127c478bd9Sstevel@tonic-gate 			 * to shared objects, and thus although the -u reference
15137c478bd9Sstevel@tonic-gate 			 * is redundant, flagging it as fatal could cause some
15147c478bd9Sstevel@tonic-gate 			 * build to fail.  Also we have documented the use of
15157c478bd9Sstevel@tonic-gate 			 * -u as a mechanism to cause binding to weak version
15167c478bd9Sstevel@tonic-gate 			 * definitions, thus giving users an error condition
15177c478bd9Sstevel@tonic-gate 			 * would be incorrect.
15187c478bd9Sstevel@tonic-gate 			 */
15197c478bd9Sstevel@tonic-gate 			if (!(sdp->sd_flags & FLG_SY_REGSYM) &&
15200bc07c75Srie 			    ((sym->st_shndx == SHN_UNDEF) &&
15217c478bd9Sstevel@tonic-gate 			    ((ELF_ST_BIND(sym->st_info) != STB_WEAK) &&
15227c478bd9Sstevel@tonic-gate 			    ((sdp->sd_flags &
15237c478bd9Sstevel@tonic-gate 			    (FLG_SY_PARENT | FLG_SY_EXTERN)) == 0)) ||
1524635216b6SRod Evans 			    ((sdp->sd_flags &
1525635216b6SRod Evans 			    (FLG_SY_MAPREF | FLG_SY_MAPUSED | FLG_SY_HIDDEN |
1526635216b6SRod Evans 			    FLG_SY_PROTECT)) == FLG_SY_MAPREF))) {
15271007fd6fSAli Bahrami 				sym_undef_entry(ofl, sdp, UNDEF, undef,
15281007fd6fSAli Bahrami 				    &undef_state);
15297c478bd9Sstevel@tonic-gate 				undeferr = 1;
15307c478bd9Sstevel@tonic-gate 			}
15317c478bd9Sstevel@tonic-gate 
15327c478bd9Sstevel@tonic-gate 		} else {
15337c478bd9Sstevel@tonic-gate 			/*
15347c478bd9Sstevel@tonic-gate 			 * For building things like shared objects (or anything
15357c478bd9Sstevel@tonic-gate 			 * -znodefs), undefined symbols are allowed.
15367c478bd9Sstevel@tonic-gate 			 *
15377c478bd9Sstevel@tonic-gate 			 * If a mapfile reference remains undefined the user
15387c478bd9Sstevel@tonic-gate 			 * would probably like a warning at least (they've
15397c478bd9Sstevel@tonic-gate 			 * usually mis-spelt the reference).  Refer to the above
15407c478bd9Sstevel@tonic-gate 			 * comments for discussion on -u references, which
15417c478bd9Sstevel@tonic-gate 			 * are not tested for in the same manner.
15427c478bd9Sstevel@tonic-gate 			 */
15437c478bd9Sstevel@tonic-gate 			if ((sdp->sd_flags &
15447c478bd9Sstevel@tonic-gate 			    (FLG_SY_MAPREF | FLG_SY_MAPUSED)) ==
15457c478bd9Sstevel@tonic-gate 			    FLG_SY_MAPREF) {
15461007fd6fSAli Bahrami 				sym_undef_entry(ofl, sdp, UNDEF, FLG_OF_WARN,
15471007fd6fSAli Bahrami 				    &undef_state);
15487c478bd9Sstevel@tonic-gate 				undeferr = 1;
15497c478bd9Sstevel@tonic-gate 			}
15507c478bd9Sstevel@tonic-gate 		}
15517c478bd9Sstevel@tonic-gate 
15527c478bd9Sstevel@tonic-gate 		/*
15537c478bd9Sstevel@tonic-gate 		 * If this symbol comes from a dependency mark the dependency
15547c478bd9Sstevel@tonic-gate 		 * as required (-z ignore can result in unused dependencies
15557c478bd9Sstevel@tonic-gate 		 * being dropped).  If we need to record dependency versioning
15567c478bd9Sstevel@tonic-gate 		 * information indicate what version of the needed shared object
15577c478bd9Sstevel@tonic-gate 		 * this symbol is part of.  Flag the symbol as undefined if it
15587c478bd9Sstevel@tonic-gate 		 * has not been made available to us.
15597c478bd9Sstevel@tonic-gate 		 */
15607c478bd9Sstevel@tonic-gate 		if ((sdp->sd_ref == REF_DYN_NEED) &&
15617c478bd9Sstevel@tonic-gate 		    (!(sdp->sd_flags & FLG_SY_REFRSD))) {
15627c478bd9Sstevel@tonic-gate 			sdp->sd_file->ifl_flags |= FLG_IF_DEPREQD;
15637c478bd9Sstevel@tonic-gate 
15647c478bd9Sstevel@tonic-gate 			/*
15657c478bd9Sstevel@tonic-gate 			 * Capture that we've bound to a symbol that doesn't
15667c478bd9Sstevel@tonic-gate 			 * allow being directly bound to.
15677c478bd9Sstevel@tonic-gate 			 */
1568635216b6SRod Evans 			if (sdp->sd_flags & FLG_SY_NDIR)
156928bda19cSRod Evans 				ofl->ofl_flags1 |= FLG_OF1_NGLBDIR;
15707c478bd9Sstevel@tonic-gate 
15717c478bd9Sstevel@tonic-gate 			if (sdp->sd_file->ifl_vercnt) {
15727c478bd9Sstevel@tonic-gate 				int		vndx;
157360758829Srie 				Ver_index	*vip;
15747c478bd9Sstevel@tonic-gate 
15757c478bd9Sstevel@tonic-gate 				vndx = sdp->sd_aux->sa_dverndx;
15767c478bd9Sstevel@tonic-gate 				vip = &sdp->sd_file->ifl_verndx[vndx];
15777c478bd9Sstevel@tonic-gate 				if (vip->vi_flags & FLG_VER_AVAIL) {
15787c478bd9Sstevel@tonic-gate 					vip->vi_flags |= FLG_VER_REFER;
15797c478bd9Sstevel@tonic-gate 				} else {
15801007fd6fSAli Bahrami 					sym_undef_entry(ofl, sdp, NOTAVAIL,
15811007fd6fSAli Bahrami 					    FLG_OF_FATAL, &undef_state);
15827c478bd9Sstevel@tonic-gate 					continue;
15837c478bd9Sstevel@tonic-gate 				}
15847c478bd9Sstevel@tonic-gate 			}
15857c478bd9Sstevel@tonic-gate 		}
15867c478bd9Sstevel@tonic-gate 
15877c478bd9Sstevel@tonic-gate 		/*
15887c478bd9Sstevel@tonic-gate 		 * Test that we do not bind to symbol supplied from an implicit
15897c478bd9Sstevel@tonic-gate 		 * shared object.  If a binding is from a weak reference it can
15907c478bd9Sstevel@tonic-gate 		 * be ignored.
15917c478bd9Sstevel@tonic-gate 		 */
15927c478bd9Sstevel@tonic-gate 		if (needed && !undeferr && (sdp->sd_flags & FLG_SY_GLOBREF) &&
15937c478bd9Sstevel@tonic-gate 		    (sdp->sd_ref == REF_DYN_NEED) &&
15947c478bd9Sstevel@tonic-gate 		    (sdp->sd_flags & FLG_SY_NOTAVAIL)) {
15951007fd6fSAli Bahrami 			sym_undef_entry(ofl, sdp, IMPLICIT, needed,
15961007fd6fSAli Bahrami 			    &undef_state);
15971007fd6fSAli Bahrami 			if (needed == FLG_OF_FATAL)
15981007fd6fSAli Bahrami 				continue;
15997c478bd9Sstevel@tonic-gate 		}
16007c478bd9Sstevel@tonic-gate 
16017c478bd9Sstevel@tonic-gate 		/*
16027c478bd9Sstevel@tonic-gate 		 * Test that a symbol isn't going to be reduced to local scope
16037c478bd9Sstevel@tonic-gate 		 * which actually wants to bind to a shared object - if so it's
16047c478bd9Sstevel@tonic-gate 		 * a fatal error.
16057c478bd9Sstevel@tonic-gate 		 */
16067c478bd9Sstevel@tonic-gate 		if ((sdp->sd_ref == REF_DYN_NEED) &&
1607635216b6SRod Evans 		    (sdp->sd_flags & (FLG_SY_HIDDEN | FLG_SY_PROTECT))) {
16081007fd6fSAli Bahrami 			sym_undef_entry(ofl, sdp, BNDLOCAL, FLG_OF_FATAL,
16091007fd6fSAli Bahrami 			    &undef_state);
16107c478bd9Sstevel@tonic-gate 			continue;
16117c478bd9Sstevel@tonic-gate 		}
16127c478bd9Sstevel@tonic-gate 
16137c478bd9Sstevel@tonic-gate 		/*
16147c478bd9Sstevel@tonic-gate 		 * If the output image is to be versioned then all symbol
161528bda19cSRod Evans 		 * definitions must be associated with a version.  Remove any
161628bda19cSRod Evans 		 * versioning that might be left associated with an undefined
161728bda19cSRod Evans 		 * symbol.
16187c478bd9Sstevel@tonic-gate 		 */
161928bda19cSRod Evans 		if (verdesc && (sdp->sd_ref == REF_REL_NEED)) {
162028bda19cSRod Evans 			if (sym->st_shndx == SHN_UNDEF) {
162128bda19cSRod Evans 				if (sdp->sd_aux && sdp->sd_aux->sa_overndx)
162228bda19cSRod Evans 					sdp->sd_aux->sa_overndx = 0;
162328bda19cSRod Evans 			} else {
162408278a5eSRod Evans 				if (!SYM_IS_HIDDEN(sdp) && sdp->sd_aux &&
162528bda19cSRod Evans 				    (sdp->sd_aux->sa_overndx == 0)) {
16261007fd6fSAli Bahrami 					sym_undef_entry(ofl, sdp, NOVERSION,
16271007fd6fSAli Bahrami 					    verdesc, &undef_state);
162828bda19cSRod Evans 					continue;
162928bda19cSRod Evans 				}
163028bda19cSRod Evans 			}
16317c478bd9Sstevel@tonic-gate 		}
16327c478bd9Sstevel@tonic-gate 
16337c478bd9Sstevel@tonic-gate 		/*
16347c478bd9Sstevel@tonic-gate 		 * If we don't need the symbol there's no need to process it
16357c478bd9Sstevel@tonic-gate 		 * any further.
16367c478bd9Sstevel@tonic-gate 		 */
16377c478bd9Sstevel@tonic-gate 		if (sdp->sd_ref == REF_DYN_SEEN)
16387c478bd9Sstevel@tonic-gate 			continue;
16397c478bd9Sstevel@tonic-gate 
16407c478bd9Sstevel@tonic-gate 		/*
16417c478bd9Sstevel@tonic-gate 		 * Calculate the size and alignment requirements for the global
16427c478bd9Sstevel@tonic-gate 		 * .bss and .tls sections.  If we're building a relocatable
16437c478bd9Sstevel@tonic-gate 		 * object only account for scoped COMMON symbols (these will
16447c478bd9Sstevel@tonic-gate 		 * be converted to .bss references).
16457c478bd9Sstevel@tonic-gate 		 *
164635450702SAli Bahrami 		 * When -z nopartial is in effect, partially initialized
164735450702SAli Bahrami 		 * symbols are directed to the special .data section
164835450702SAli Bahrami 		 * created for that purpose (ofl->ofl_isparexpn).
164935450702SAli Bahrami 		 * Otherwise, partially initialized symbols go to .bss.
16507c478bd9Sstevel@tonic-gate 		 *
16517c478bd9Sstevel@tonic-gate 		 * Also refer to make_mvsections() in sunwmove.c
16527c478bd9Sstevel@tonic-gate 		 */
16530bc07c75Srie 		if ((sym->st_shndx == SHN_COMMON) &&
16540bc07c75Srie 		    (((oflags & FLG_OF_RELOBJ) == 0) ||
1655c524b4feSRichard Lowe 		    ld_sym_reducable(ofl, sdp))) {
165657ef7aa9SRod Evans 			if ((sdp->sd_move == NULL) ||
165735450702SAli Bahrami 			    ((sdp->sd_flags & FLG_SY_PAREXPN) == 0)) {
1658d579eb63Sab 				if (type != STT_TLS) {
1659e64d0ff9SAli Bahrami 					need_bss = TRUE;
1660e64d0ff9SAli Bahrami 					bsssize = (Xword)S_ROUND(bsssize,
1661e64d0ff9SAli Bahrami 					    sym->st_value) + sym->st_size;
1662e64d0ff9SAli Bahrami 					if (sym->st_value > bssalign)
1663e64d0ff9SAli Bahrami 						bssalign = sym->st_value;
16647c478bd9Sstevel@tonic-gate 				} else {
1665e64d0ff9SAli Bahrami 					need_tlsbss = TRUE;
1666e64d0ff9SAli Bahrami 					tlssize = (Xword)S_ROUND(tlssize,
1667e64d0ff9SAli Bahrami 					    sym->st_value) + sym->st_size;
1668e64d0ff9SAli Bahrami 					if (sym->st_value > tlsalign)
1669e64d0ff9SAli Bahrami 						tlsalign = sym->st_value;
16707c478bd9Sstevel@tonic-gate 				}
16717c478bd9Sstevel@tonic-gate 			}
16727c478bd9Sstevel@tonic-gate 		}
16737c478bd9Sstevel@tonic-gate 
1674ba2be530Sab #if	defined(_ELF64)
167554d82594Sseizo 		/*
167654d82594Sseizo 		 * Calculate the size and alignment requirement for the global
167754d82594Sseizo 		 * .lbss. TLS or partially initialized symbols do not need to be
167854d82594Sseizo 		 * considered yet.
167954d82594Sseizo 		 */
1680ba2be530Sab 		if ((ld_targ.t_m.m_mach == EM_AMD64) &&
1681ba2be530Sab 		    (sym->st_shndx == SHN_X86_64_LCOMMON)) {
1682e64d0ff9SAli Bahrami 			need_lbss = TRUE;
168354d82594Sseizo 			lbsssize = (Xword)S_ROUND(lbsssize, sym->st_value) +
168433eb6ee1Sab 			    sym->st_size;
168554d82594Sseizo 			if (sym->st_value > lbssalign)
168654d82594Sseizo 				lbssalign = sym->st_value;
168754d82594Sseizo 		}
168854d82594Sseizo #endif
16897c478bd9Sstevel@tonic-gate 		/*
16907c478bd9Sstevel@tonic-gate 		 * If a symbol was referenced via the command line
16917c478bd9Sstevel@tonic-gate 		 * (ld -u <>, ...), then this counts as a reference against the
16927c478bd9Sstevel@tonic-gate 		 * symbol. Mark any section that symbol is defined in.
16937c478bd9Sstevel@tonic-gate 		 */
16947c478bd9Sstevel@tonic-gate 		if (((isp = sdp->sd_isc) != 0) &&
16957c478bd9Sstevel@tonic-gate 		    (sdp->sd_flags & FLG_SY_CMDREF)) {
16967c478bd9Sstevel@tonic-gate 			isp->is_flags |= FLG_IS_SECTREF;
16977c478bd9Sstevel@tonic-gate 			isp->is_file->ifl_flags |= FLG_IF_FILEREF;
16987c478bd9Sstevel@tonic-gate 		}
16997c478bd9Sstevel@tonic-gate 
17007c478bd9Sstevel@tonic-gate 		/*
17017c478bd9Sstevel@tonic-gate 		 * Update the symbol count and the associated name string size.
170208278a5eSRod Evans 		 * Note, a capabilities symbol must remain as visible as a
170308278a5eSRod Evans 		 * global symbol.  However, the runtime linker recognizes the
170408278a5eSRod Evans 		 * hidden requirement and ensures the symbol isn't made globally
170508278a5eSRod Evans 		 * available at runtime.
17067c478bd9Sstevel@tonic-gate 		 */
1707c524b4feSRichard Lowe 		if (ld_sym_reducable(ofl, sdp)) {
17087c478bd9Sstevel@tonic-gate 			/*
170960758829Srie 			 * If any reductions are being processed, keep a count
171060758829Srie 			 * of eliminated symbols, and if the symbol is being
171160758829Srie 			 * reduced to local, count it's size for the .symtab.
17127c478bd9Sstevel@tonic-gate 			 */
1713635216b6SRod Evans 			if (sdp->sd_flags & FLG_SY_ELIM) {
17147c478bd9Sstevel@tonic-gate 				ofl->ofl_elimcnt++;
17157c478bd9Sstevel@tonic-gate 			} else {
17167c478bd9Sstevel@tonic-gate 				ofl->ofl_scopecnt++;
17177c478bd9Sstevel@tonic-gate 				if ((((sdp->sd_flags & FLG_SY_REGSYM) == 0) ||
17187c478bd9Sstevel@tonic-gate 				    sym->st_name) && (st_insert(ofl->ofl_strtab,
17197c478bd9Sstevel@tonic-gate 				    sdp->sd_name) == -1))
17207c478bd9Sstevel@tonic-gate 					return (S_ERROR);
17219039eeafSab 				if (allow_ldynsym && sym->st_name &&
1722d579eb63Sab 				    ldynsym_symtype[type]) {
17239039eeafSab 					ofl->ofl_dynscopecnt++;
17249039eeafSab 					if (st_insert(ofl->ofl_dynstrtab,
17259039eeafSab 					    sdp->sd_name) == -1)
17269039eeafSab 						return (S_ERROR);
1727d579eb63Sab 					/* Include it in sort section? */
1728d579eb63Sab 					DYNSORT_COUNT(sdp, sym, type, ++);
17299039eeafSab 				}
17307c478bd9Sstevel@tonic-gate 			}
17317c478bd9Sstevel@tonic-gate 		} else {
17327c478bd9Sstevel@tonic-gate 			ofl->ofl_globcnt++;
17337c478bd9Sstevel@tonic-gate 
1734d579eb63Sab 			/*
1735635216b6SRod Evans 			 * Check to see if this global variable should go into
1736635216b6SRod Evans 			 * a sort section. Sort sections require a
1737635216b6SRod Evans 			 * .SUNW_ldynsym section, so, don't check unless a
1738635216b6SRod Evans 			 * .SUNW_ldynsym is allowed.
1739d579eb63Sab 			 */
1740635216b6SRod Evans 			if (allow_ldynsym)
1741d579eb63Sab 				DYNSORT_COUNT(sdp, sym, type, ++);
1742d579eb63Sab 
17437c478bd9Sstevel@tonic-gate 			/*
17447c478bd9Sstevel@tonic-gate 			 * If global direct bindings are in effect, or this
17457c478bd9Sstevel@tonic-gate 			 * symbol has bound to a dependency which was specified
17467c478bd9Sstevel@tonic-gate 			 * as requiring direct bindings, and it hasn't
17477c478bd9Sstevel@tonic-gate 			 * explicitly been defined as a non-direct binding
17487c478bd9Sstevel@tonic-gate 			 * symbol, mark it.
17497c478bd9Sstevel@tonic-gate 			 */
17507c478bd9Sstevel@tonic-gate 			if (((ofl->ofl_dtflags_1 & DF_1_DIRECT) || (isp &&
17517c478bd9Sstevel@tonic-gate 			    (isp->is_file->ifl_flags & FLG_IF_DIRECT))) &&
1752635216b6SRod Evans 			    ((sdp->sd_flags & FLG_SY_NDIR) == 0))
1753635216b6SRod Evans 				sdp->sd_flags |= FLG_SY_DIR;
17547c478bd9Sstevel@tonic-gate 
17557c478bd9Sstevel@tonic-gate 			/*
17567c478bd9Sstevel@tonic-gate 			 * Insert the symbol name.
17577c478bd9Sstevel@tonic-gate 			 */
17587c478bd9Sstevel@tonic-gate 			if (((sdp->sd_flags & FLG_SY_REGSYM) == 0) ||
17597c478bd9Sstevel@tonic-gate 			    sym->st_name) {
17607c478bd9Sstevel@tonic-gate 				if (st_insert(ofl->ofl_strtab,
17617c478bd9Sstevel@tonic-gate 				    sdp->sd_name) == -1)
17627c478bd9Sstevel@tonic-gate 					return (S_ERROR);
17637c478bd9Sstevel@tonic-gate 
17647c478bd9Sstevel@tonic-gate 				if (!(ofl->ofl_flags & FLG_OF_RELOBJ) &&
17657c478bd9Sstevel@tonic-gate 				    (st_insert(ofl->ofl_dynstrtab,
17667c478bd9Sstevel@tonic-gate 				    sdp->sd_name) == -1))
17677c478bd9Sstevel@tonic-gate 					return (S_ERROR);
17687c478bd9Sstevel@tonic-gate 			}
17697c478bd9Sstevel@tonic-gate 
17707c478bd9Sstevel@tonic-gate 			/*
17717c478bd9Sstevel@tonic-gate 			 * If this section offers a global symbol - record that
17727c478bd9Sstevel@tonic-gate 			 * fact.
17737c478bd9Sstevel@tonic-gate 			 */
17747c478bd9Sstevel@tonic-gate 			if (isp) {
17757c478bd9Sstevel@tonic-gate 				isp->is_flags |= FLG_IS_SECTREF;
17767c478bd9Sstevel@tonic-gate 				isp->is_file->ifl_flags |= FLG_IF_FILEREF;
17777c478bd9Sstevel@tonic-gate 			}
17787c478bd9Sstevel@tonic-gate 		}
17797c478bd9Sstevel@tonic-gate 	}
17807c478bd9Sstevel@tonic-gate 
17811007fd6fSAli Bahrami 	/*
17821007fd6fSAli Bahrami 	 * Guidance: Use -z defs|nodefs when building shared objects.
17831007fd6fSAli Bahrami 	 *
17841007fd6fSAli Bahrami 	 * Our caller issues this, unless we mask it out here. So we mask it
17851007fd6fSAli Bahrami 	 * out unless we've issued at least one warnings or fatal error.
17861007fd6fSAli Bahrami 	 */
17871007fd6fSAli Bahrami 	if (!((oflags & FLG_OF_SHAROBJ) && OFL_GUIDANCE(ofl, FLG_OFG_NO_DEFS) &&
17881007fd6fSAli Bahrami 	    (undef_state & (FLG_OF_FATAL | FLG_OF_WARN))))
17891007fd6fSAli Bahrami 		ofl->ofl_guideflags |= FLG_OFG_NO_DEFS;
17901007fd6fSAli Bahrami 
17917c478bd9Sstevel@tonic-gate 	/*
17927c478bd9Sstevel@tonic-gate 	 * If we've encountered a fatal error during symbol validation then
17937c478bd9Sstevel@tonic-gate 	 * return now.
17947c478bd9Sstevel@tonic-gate 	 */
17957c478bd9Sstevel@tonic-gate 	if (ofl->ofl_flags & FLG_OF_FATAL)
17967c478bd9Sstevel@tonic-gate 		return (1);
17977c478bd9Sstevel@tonic-gate 
17987c478bd9Sstevel@tonic-gate 	/*
17997c478bd9Sstevel@tonic-gate 	 * Now that symbol resolution is completed, scan any register symbols.
18007c478bd9Sstevel@tonic-gate 	 * From now on, we're only interested in those that contribute to the
18017c478bd9Sstevel@tonic-gate 	 * output file.
18027c478bd9Sstevel@tonic-gate 	 */
18037c478bd9Sstevel@tonic-gate 	if (ofl->ofl_regsyms) {
18047c478bd9Sstevel@tonic-gate 		int	ndx;
18057c478bd9Sstevel@tonic-gate 
18067c478bd9Sstevel@tonic-gate 		for (ndx = 0; ndx < ofl->ofl_regsymsno; ndx++) {
18076b3ba5bdSAli Bahrami 			if ((sdp = ofl->ofl_regsyms[ndx]) == NULL)
18087c478bd9Sstevel@tonic-gate 				continue;
18097c478bd9Sstevel@tonic-gate 			if (sdp->sd_ref != REF_REL_NEED) {
18106b3ba5bdSAli Bahrami 				ofl->ofl_regsyms[ndx] = NULL;
18117c478bd9Sstevel@tonic-gate 				continue;
18127c478bd9Sstevel@tonic-gate 			}
18137c478bd9Sstevel@tonic-gate 
18147c478bd9Sstevel@tonic-gate 			ofl->ofl_regsymcnt++;
18157c478bd9Sstevel@tonic-gate 			if (sdp->sd_sym->st_name == 0)
18167c478bd9Sstevel@tonic-gate 				sdp->sd_name = MSG_ORIG(MSG_STR_EMPTY);
18177c478bd9Sstevel@tonic-gate 
181808278a5eSRod Evans 			if (SYM_IS_HIDDEN(sdp) ||
18197c478bd9Sstevel@tonic-gate 			    (ELF_ST_BIND(sdp->sd_sym->st_info) == STB_LOCAL))
18207c478bd9Sstevel@tonic-gate 				ofl->ofl_lregsymcnt++;
18217c478bd9Sstevel@tonic-gate 		}
18227c478bd9Sstevel@tonic-gate 	}
18237c478bd9Sstevel@tonic-gate 
18247c478bd9Sstevel@tonic-gate 	/*
18257c478bd9Sstevel@tonic-gate 	 * Generate the .bss section now that we know its size and alignment.
18267c478bd9Sstevel@tonic-gate 	 */
1827e64d0ff9SAli Bahrami 	if (need_bss) {
182857ef7aa9SRod Evans 		if (ld_make_bss(ofl, bsssize, bssalign,
182957ef7aa9SRod Evans 		    ld_targ.t_id.id_bss) == S_ERROR)
18307c478bd9Sstevel@tonic-gate 			return (S_ERROR);
18317c478bd9Sstevel@tonic-gate 	}
1832e64d0ff9SAli Bahrami 	if (need_tlsbss) {
183357ef7aa9SRod Evans 		if (ld_make_bss(ofl, tlssize, tlsalign,
183457ef7aa9SRod Evans 		    ld_targ.t_id.id_tlsbss) == S_ERROR)
183554d82594Sseizo 			return (S_ERROR);
183654d82594Sseizo 	}
1837ba2be530Sab #if	defined(_ELF64)
1838ba2be530Sab 	if ((ld_targ.t_m.m_mach == EM_AMD64) &&
1839e64d0ff9SAli Bahrami 	    need_lbss && !(oflags & FLG_OF_RELOBJ)) {
184057ef7aa9SRod Evans 		if (ld_make_bss(ofl, lbsssize, lbssalign,
184157ef7aa9SRod Evans 		    ld_targ.t_id.id_lbss) == S_ERROR)
18427c478bd9Sstevel@tonic-gate 			return (S_ERROR);
18437c478bd9Sstevel@tonic-gate 	}
184454d82594Sseizo #endif
18457c478bd9Sstevel@tonic-gate 	/*
18467c478bd9Sstevel@tonic-gate 	 * Determine what entry point symbol we need, and if found save its
18477c478bd9Sstevel@tonic-gate 	 * symbol descriptor so that we can update the ELF header entry with the
18487c478bd9Sstevel@tonic-gate 	 * symbols value later (see update_oehdr).  Make sure the symbol is
1849c1c6f601Srie 	 * tagged to ensure its update in case -s is in effect.  Use any -e
18507c478bd9Sstevel@tonic-gate 	 * option first, or the default entry points `_start' and `main'.
18517c478bd9Sstevel@tonic-gate 	 */
1852c1c6f601Srie 	ret = 0;
18537c478bd9Sstevel@tonic-gate 	if (ofl->ofl_entry) {
185428bda19cSRod Evans 		if ((sdp = ld_sym_find(ofl->ofl_entry, SYM_NOHASH,
185528bda19cSRod Evans 		    NULL, ofl)) == NULL) {
18561007fd6fSAli Bahrami 			ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_ARG_NOENTRY),
18571007fd6fSAli Bahrami 			    ofl->ofl_entry);
18581d1fba8aSrie 			ret++;
18591d1fba8aSrie 		} else if (ensure_sym_local(ofl, sdp,
18601d1fba8aSrie 		    MSG_INTL(MSG_SYM_ENTRY)) != 0) {
1861c1c6f601Srie 			ret++;
18621d1fba8aSrie 		} else {
18631d1fba8aSrie 			ofl->ofl_entry = (void *)sdp;
18647c478bd9Sstevel@tonic-gate 		}
18655aefb655Srie 	} else if (((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_START),
186628bda19cSRod Evans 	    SYM_NOHASH, NULL, ofl)) != NULL) && (ensure_sym_local(ofl,
1867c1c6f601Srie 	    sdp, 0) == 0)) {
18687c478bd9Sstevel@tonic-gate 		ofl->ofl_entry = (void *)sdp;
1869c1c6f601Srie 
18705aefb655Srie 	} else if (((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_MAIN),
187128bda19cSRod Evans 	    SYM_NOHASH, NULL, ofl)) != NULL) && (ensure_sym_local(ofl,
1872c1c6f601Srie 	    sdp, 0) == 0)) {
18737c478bd9Sstevel@tonic-gate 		ofl->ofl_entry = (void *)sdp;
18747c478bd9Sstevel@tonic-gate 	}
18757c478bd9Sstevel@tonic-gate 
18767c478bd9Sstevel@tonic-gate 	/*
1877c1c6f601Srie 	 * If ld -zdtrace=<sym> was given, then validate that the symbol is
1878c1c6f601Srie 	 * defined within the current object being built.
18797c478bd9Sstevel@tonic-gate 	 */
18801d1fba8aSrie 	if ((sdp = ofl->ofl_dtracesym) != 0)
1881c1c6f601Srie 		ret += ensure_sym_local(ofl, sdp, MSG_ORIG(MSG_STR_DTRACE));
18827c478bd9Sstevel@tonic-gate 
1883c1c6f601Srie 	/*
1884c1c6f601Srie 	 * If any initarray, finiarray or preinitarray functions have been
1885c1c6f601Srie 	 * requested, make sure they are defined within the current object
1886c1c6f601Srie 	 * being built.
1887c1c6f601Srie 	 */
188857ef7aa9SRod Evans 	if (ofl->ofl_initarray) {
188957ef7aa9SRod Evans 		ret += ensure_array_local(ofl, ofl->ofl_initarray,
1890c1c6f601Srie 		    MSG_ORIG(MSG_SYM_INITARRAY));
1891c1c6f601Srie 	}
189257ef7aa9SRod Evans 	if (ofl->ofl_finiarray) {
189357ef7aa9SRod Evans 		ret += ensure_array_local(ofl, ofl->ofl_finiarray,
1894c1c6f601Srie 		    MSG_ORIG(MSG_SYM_FINIARRAY));
1895c1c6f601Srie 	}
189657ef7aa9SRod Evans 	if (ofl->ofl_preiarray) {
189757ef7aa9SRod Evans 		ret += ensure_array_local(ofl, ofl->ofl_preiarray,
1898c1c6f601Srie 		    MSG_ORIG(MSG_SYM_PREINITARRAY));
1899c1c6f601Srie 	}
1900c1c6f601Srie 
1901c1c6f601Srie 	if (ret)
1902c1c6f601Srie 		return (S_ERROR);
1903c1c6f601Srie 
19047c478bd9Sstevel@tonic-gate 	/*
19057c478bd9Sstevel@tonic-gate 	 * If we're required to record any needed dependencies versioning
19067c478bd9Sstevel@tonic-gate 	 * information calculate it now that all symbols have been validated.
19077c478bd9Sstevel@tonic-gate 	 */
19087c478bd9Sstevel@tonic-gate 	if ((oflags & (FLG_OF_VERNEED | FLG_OF_NOVERSEC)) == FLG_OF_VERNEED)
19095aefb655Srie 		return (ld_vers_check_need(ofl));
19107c478bd9Sstevel@tonic-gate 	else
19117c478bd9Sstevel@tonic-gate 		return (1);
19127c478bd9Sstevel@tonic-gate }
19137c478bd9Sstevel@tonic-gate 
19147c478bd9Sstevel@tonic-gate /*
19157c478bd9Sstevel@tonic-gate  * qsort(3c) comparison function.  As an optimization for associating weak
19167c478bd9Sstevel@tonic-gate  * symbols to their strong counterparts sort global symbols according to their
19176b3ba5bdSAli Bahrami  * section index, address and binding.
19187c478bd9Sstevel@tonic-gate  */
19197c478bd9Sstevel@tonic-gate static int
19206b3ba5bdSAli Bahrami compare(const void *sdpp1, const void *sdpp2)
19217c478bd9Sstevel@tonic-gate {
19226b3ba5bdSAli Bahrami 	Sym_desc	*sdp1 = *((Sym_desc **)sdpp1);
19236b3ba5bdSAli Bahrami 	Sym_desc	*sdp2 = *((Sym_desc **)sdpp2);
19246b3ba5bdSAli Bahrami 	Sym		*sym1, *sym2;
19257c478bd9Sstevel@tonic-gate 	uchar_t		bind1, bind2;
19267c478bd9Sstevel@tonic-gate 
19277c478bd9Sstevel@tonic-gate 	/*
19287c478bd9Sstevel@tonic-gate 	 * Symbol descriptors may be zero, move these to the front of the
19297c478bd9Sstevel@tonic-gate 	 * sorted array.
19307c478bd9Sstevel@tonic-gate 	 */
19316b3ba5bdSAli Bahrami 	if (sdp1 == NULL)
19327c478bd9Sstevel@tonic-gate 		return (-1);
19336b3ba5bdSAli Bahrami 	if (sdp2 == NULL)
19347c478bd9Sstevel@tonic-gate 		return (1);
19357c478bd9Sstevel@tonic-gate 
19367c478bd9Sstevel@tonic-gate 	sym1 = sdp1->sd_sym;
19377c478bd9Sstevel@tonic-gate 	sym2 = sdp2->sd_sym;
19387c478bd9Sstevel@tonic-gate 
19396b3ba5bdSAli Bahrami 	/*
19406b3ba5bdSAli Bahrami 	 * Compare the symbols section index.  This is important when sorting
19416b3ba5bdSAli Bahrami 	 * the symbol tables of relocatable objects.  In this case, a symbols
19426b3ba5bdSAli Bahrami 	 * value is the offset within the associated section, and thus many
19436b3ba5bdSAli Bahrami 	 * symbols can have the same value, but are effectively different
19446b3ba5bdSAli Bahrami 	 * addresses.
19456b3ba5bdSAli Bahrami 	 */
19466b3ba5bdSAli Bahrami 	if (sym1->st_shndx > sym2->st_shndx)
19476b3ba5bdSAli Bahrami 		return (1);
19486b3ba5bdSAli Bahrami 	if (sym1->st_shndx < sym2->st_shndx)
19496b3ba5bdSAli Bahrami 		return (-1);
19506b3ba5bdSAli Bahrami 
19517c478bd9Sstevel@tonic-gate 	/*
19527c478bd9Sstevel@tonic-gate 	 * Compare the symbols value (address).
19537c478bd9Sstevel@tonic-gate 	 */
19547c478bd9Sstevel@tonic-gate 	if (sym1->st_value > sym2->st_value)
19557c478bd9Sstevel@tonic-gate 		return (1);
19567c478bd9Sstevel@tonic-gate 	if (sym1->st_value < sym2->st_value)
19577c478bd9Sstevel@tonic-gate 		return (-1);
19587c478bd9Sstevel@tonic-gate 
19597c478bd9Sstevel@tonic-gate 	bind1 = ELF_ST_BIND(sym1->st_info);
19607c478bd9Sstevel@tonic-gate 	bind2 = ELF_ST_BIND(sym2->st_info);
19617c478bd9Sstevel@tonic-gate 
19627c478bd9Sstevel@tonic-gate 	/*
19637c478bd9Sstevel@tonic-gate 	 * If two symbols have the same address place the weak symbol before
19647c478bd9Sstevel@tonic-gate 	 * any strong counterpart.
19657c478bd9Sstevel@tonic-gate 	 */
19667c478bd9Sstevel@tonic-gate 	if (bind1 > bind2)
19677c478bd9Sstevel@tonic-gate 		return (-1);
19687c478bd9Sstevel@tonic-gate 	if (bind1 < bind2)
19697c478bd9Sstevel@tonic-gate 		return (1);
19707c478bd9Sstevel@tonic-gate 
19717c478bd9Sstevel@tonic-gate 	return (0);
19727c478bd9Sstevel@tonic-gate }
19737c478bd9Sstevel@tonic-gate 
197469a0bf0cSab /*
197569a0bf0cSab  * Issue a MSG_SYM_BADADDR error from ld_sym_process(). This error
197669a0bf0cSab  * is issued when a symbol address/size is not contained by the
197769a0bf0cSab  * target section.
197869a0bf0cSab  *
197969a0bf0cSab  * Such objects are at least partially corrupt, and the user would
198069a0bf0cSab  * be well advised to be skeptical of them, and to ask their compiler
198169a0bf0cSab  * supplier to fix the problem. However, a distinction needs to be
198269a0bf0cSab  * made between symbols that reference readonly text, and those that
198369a0bf0cSab  * access writable data. Other than throwing off profiling results,
198469a0bf0cSab  * the readonly section case is less serious. We have encountered
198569a0bf0cSab  * such objects in the field. In order to allow existing objects
198669a0bf0cSab  * to continue working, we issue a warning rather than a fatal error
198769a0bf0cSab  * if the symbol is against readonly text. Other cases are fatal.
198869a0bf0cSab  */
198969a0bf0cSab static void
199069a0bf0cSab issue_badaddr_msg(Ifl_desc *ifl, Ofl_desc *ofl, Sym_desc *sdp,
199169a0bf0cSab     Sym *sym, Word shndx)
199269a0bf0cSab {
19931d9df23bSab 	Error		err;
19941d9df23bSab 	const char	*msg;
199569a0bf0cSab 
199669a0bf0cSab 	if ((sdp->sd_isc->is_shdr->sh_flags & (SHF_WRITE | SHF_ALLOC)) ==
199769a0bf0cSab 	    SHF_ALLOC) {
199869a0bf0cSab 		msg = MSG_INTL(MSG_SYM_BADADDR_ROTXT);
199969a0bf0cSab 		err = ERR_WARNING;
200069a0bf0cSab 	} else {
200169a0bf0cSab 		msg = MSG_INTL(MSG_SYM_BADADDR);
200269a0bf0cSab 		err = ERR_FATAL;
200369a0bf0cSab 	}
200469a0bf0cSab 
20051007fd6fSAli Bahrami 	ld_eprintf(ofl, err, msg, demangle(sdp->sd_name),
200669a0bf0cSab 	    ifl->ifl_name, shndx, sdp->sd_isc->is_name,
200769a0bf0cSab 	    EC_XWORD(sdp->sd_isc->is_shdr->sh_size),
200869a0bf0cSab 	    EC_XWORD(sym->st_value), EC_XWORD(sym->st_size));
200969a0bf0cSab }
201069a0bf0cSab 
201108278a5eSRod Evans /*
201208278a5eSRod Evans  * Global symbols that are candidates for translation to local capability
201308278a5eSRod Evans  * symbols under -z symbolcap, are maintained on a local symbol list.  Once
201408278a5eSRod Evans  * all symbols of a file are processed, this list is traversed to cull any
201508278a5eSRod Evans  * unnecessary weak symbol aliases.
201608278a5eSRod Evans  */
201708278a5eSRod Evans typedef struct {
201808278a5eSRod Evans 	Sym_desc	*c_nsdp;	/* new lead symbol */
201908278a5eSRod Evans 	Sym_desc	*c_osdp;	/* original symbol */
202008278a5eSRod Evans 	Cap_group	*c_group;	/* symbol capability group */
202108278a5eSRod Evans 	Word		c_ndx;		/* symbol index */
202208278a5eSRod Evans } Cap_pair;
202369a0bf0cSab 
20247c478bd9Sstevel@tonic-gate /*
20257c478bd9Sstevel@tonic-gate  * Process the symbol table for the specified input file.  At this point all
20267c478bd9Sstevel@tonic-gate  * input sections from this input file have been assigned an input section
20277c478bd9Sstevel@tonic-gate  * descriptor which is saved in the `ifl_isdesc' array.
20287c478bd9Sstevel@tonic-gate  *
2029c524b4feSRichard Lowe  *  -	local symbols are saved (as is) if the input file is a relocatable
203008278a5eSRod Evans  *	object
20317c478bd9Sstevel@tonic-gate  *
203208278a5eSRod Evans  *  -	global symbols are added to the linkers internal symbol table if they
203308278a5eSRod Evans  *	are not already present, otherwise a symbol resolution function is
203408278a5eSRod Evans  *	called upon to resolve the conflict.
20357c478bd9Sstevel@tonic-gate  */
20367c478bd9Sstevel@tonic-gate uintptr_t
20375aefb655Srie ld_sym_process(Is_desc *isc, Ifl_desc *ifl, Ofl_desc *ofl)
20387c478bd9Sstevel@tonic-gate {
203975e45495Sab 	/*
204075e45495Sab 	 * This macro tests the given symbol to see if it is out of
204175e45495Sab 	 * range relative to the section it references.
204275e45495Sab 	 *
204375e45495Sab 	 * entry:
204475e45495Sab 	 *	- ifl is a relative object (ET_REL)
204575e45495Sab 	 *	_sdp - Symbol descriptor
204675e45495Sab 	 *	_sym - Symbol
204775e45495Sab 	 *	_type - Symbol type
204875e45495Sab 	 *
204975e45495Sab 	 * The following are tested:
205075e45495Sab 	 *	- Symbol length is non-zero
205175e45495Sab 	 *	- Symbol type is a type that references code or data
205275e45495Sab 	 *	- Referenced section is not 0 (indicates an UNDEF symbol)
205375e45495Sab 	 *	  and is not in the range of special values above SHN_LORESERVE
205475e45495Sab 	 *	  (excluding SHN_XINDEX, which is OK).
205575e45495Sab 	 *	- We have a valid section header for the target section
205675e45495Sab 	 *
205775e45495Sab 	 * If the above are all true, and the symbol position is not
205875e45495Sab 	 * contained by the target section, this macro evaluates to
205975e45495Sab 	 * True (1). Otherwise, False(0).
206075e45495Sab 	 */
206175e45495Sab #define	SYM_LOC_BADADDR(_sdp, _sym, _type) \
2062d579eb63Sab 	(_sym->st_size && dynsymsort_symtype[_type] && \
206375e45495Sab 	(_sym->st_shndx != SHN_UNDEF) && \
206475e45495Sab 	((_sym->st_shndx < SHN_LORESERVE) || \
206575e45495Sab 		(_sym->st_shndx == SHN_XINDEX)) && \
206675e45495Sab 	_sdp->sd_isc && _sdp->sd_isc->is_shdr && \
206775e45495Sab 	((_sym->st_value + _sym->st_size) > _sdp->sd_isc->is_shdr->sh_size))
206875e45495Sab 
2069de777a60Sab 	Conv_inv_buf_t	inv_buf;
20707c478bd9Sstevel@tonic-gate 	Sym		*sym = (Sym *)isc->is_indata->d_buf;
20716b3ba5bdSAli Bahrami 	Word		*symshndx = NULL;
20727c478bd9Sstevel@tonic-gate 	Shdr		*shdr = isc->is_shdr;
20737c478bd9Sstevel@tonic-gate 	Sym_desc	*sdp;
20747c478bd9Sstevel@tonic-gate 	size_t		strsize;
20757c478bd9Sstevel@tonic-gate 	char		*strs;
20767c478bd9Sstevel@tonic-gate 	uchar_t		type, bind;
20777c478bd9Sstevel@tonic-gate 	Word		ndx, hash, local, total;
20784f680cc6SAli Bahrami 	uchar_t		osabi = ifl->ifl_ehdr->e_ident[EI_OSABI];
20794f680cc6SAli Bahrami 	Half		mach = ifl->ifl_ehdr->e_machine;
20807c478bd9Sstevel@tonic-gate 	Half		etype = ifl->ifl_ehdr->e_type;
20817c478bd9Sstevel@tonic-gate 	const char	*symsecname, *strsecname;
20824a8d0ea7SAli Bahrami 	Word		symsecndx;
20837c478bd9Sstevel@tonic-gate 	avl_index_t	where;
20846b3ba5bdSAli Bahrami 	int		test_gnu_hidden_bit, weak;
208508278a5eSRod Evans 	Cap_desc	*cdp = NULL;
208608278a5eSRod Evans 	Alist		*cappairs = NULL;
20877c478bd9Sstevel@tonic-gate 
20887c478bd9Sstevel@tonic-gate 	/*
20897c478bd9Sstevel@tonic-gate 	 * Its possible that a file may contain more that one symbol table,
20907c478bd9Sstevel@tonic-gate 	 * ie. .dynsym and .symtab in a shared library.  Only process the first
20917c478bd9Sstevel@tonic-gate 	 * table (here, we assume .dynsym comes before .symtab).
20927c478bd9Sstevel@tonic-gate 	 */
20937c478bd9Sstevel@tonic-gate 	if (ifl->ifl_symscnt)
20947c478bd9Sstevel@tonic-gate 		return (1);
20957c478bd9Sstevel@tonic-gate 
20967c478bd9Sstevel@tonic-gate 	if (isc->is_symshndx)
20977c478bd9Sstevel@tonic-gate 		symshndx = isc->is_symshndx->is_indata->d_buf;
20987c478bd9Sstevel@tonic-gate 
20995aefb655Srie 	DBG_CALL(Dbg_syms_process(ofl->ofl_lml, ifl));
21007c478bd9Sstevel@tonic-gate 
21014a8d0ea7SAli Bahrami 	symsecndx = isc->is_scnndx;
21027c478bd9Sstevel@tonic-gate 	if (isc->is_name)
21037c478bd9Sstevel@tonic-gate 		symsecname = isc->is_name;
21047c478bd9Sstevel@tonic-gate 	else
21057c478bd9Sstevel@tonic-gate 		symsecname = MSG_ORIG(MSG_STR_EMPTY);
21067c478bd9Sstevel@tonic-gate 
21077c478bd9Sstevel@tonic-gate 	/*
21087c478bd9Sstevel@tonic-gate 	 * From the symbol tables section header information determine which
21097c478bd9Sstevel@tonic-gate 	 * strtab table is needed to locate the actual symbol names.
21107c478bd9Sstevel@tonic-gate 	 */
21117c478bd9Sstevel@tonic-gate 	if (ifl->ifl_flags & FLG_IF_HSTRTAB) {
21127c478bd9Sstevel@tonic-gate 		ndx = shdr->sh_link;
21137c478bd9Sstevel@tonic-gate 		if ((ndx == 0) || (ndx >= ifl->ifl_shnum)) {
21141007fd6fSAli Bahrami 			ld_eprintf(ofl, ERR_FATAL,
21154a8d0ea7SAli Bahrami 			    MSG_INTL(MSG_FIL_INVSHLINK), ifl->ifl_name,
21164a8d0ea7SAli Bahrami 			    EC_WORD(symsecndx), symsecname, EC_XWORD(ndx));
21177c478bd9Sstevel@tonic-gate 			return (S_ERROR);
21187c478bd9Sstevel@tonic-gate 		}
21197c478bd9Sstevel@tonic-gate 		strsize = ifl->ifl_isdesc[ndx]->is_shdr->sh_size;
21207c478bd9Sstevel@tonic-gate 		strs = ifl->ifl_isdesc[ndx]->is_indata->d_buf;
21217c478bd9Sstevel@tonic-gate 		if (ifl->ifl_isdesc[ndx]->is_name)
21227c478bd9Sstevel@tonic-gate 			strsecname = ifl->ifl_isdesc[ndx]->is_name;
21237c478bd9Sstevel@tonic-gate 		else
21247c478bd9Sstevel@tonic-gate 			strsecname = MSG_ORIG(MSG_STR_EMPTY);
21257c478bd9Sstevel@tonic-gate 	} else {
21267c478bd9Sstevel@tonic-gate 		/*
21277c478bd9Sstevel@tonic-gate 		 * There is no string table section in this input file
21287c478bd9Sstevel@tonic-gate 		 * although there are symbols in this symbol table section.
21297c478bd9Sstevel@tonic-gate 		 * This means that these symbols do not have names.
21307c478bd9Sstevel@tonic-gate 		 * Currently, only scratch register symbols are allowed
21317c478bd9Sstevel@tonic-gate 		 * not to have names.
21327c478bd9Sstevel@tonic-gate 		 */
21337c478bd9Sstevel@tonic-gate 		strsize = 0;
21347c478bd9Sstevel@tonic-gate 		strs = (char *)MSG_ORIG(MSG_STR_EMPTY);
21357c478bd9Sstevel@tonic-gate 		strsecname = MSG_ORIG(MSG_STR_EMPTY);
21367c478bd9Sstevel@tonic-gate 	}
21377c478bd9Sstevel@tonic-gate 
21387c478bd9Sstevel@tonic-gate 	/*
21397c478bd9Sstevel@tonic-gate 	 * Determine the number of local symbols together with the total
21407c478bd9Sstevel@tonic-gate 	 * number we have to process.
21417c478bd9Sstevel@tonic-gate 	 */
21427c478bd9Sstevel@tonic-gate 	total = (Word)(shdr->sh_size / shdr->sh_entsize);
21437c478bd9Sstevel@tonic-gate 	local = shdr->sh_info;
21447c478bd9Sstevel@tonic-gate 
21457c478bd9Sstevel@tonic-gate 	/*
21467c478bd9Sstevel@tonic-gate 	 * Allocate a symbol table index array and a local symbol array
21477c478bd9Sstevel@tonic-gate 	 * (global symbols are processed and added to the ofl->ofl_symbkt[]
21487c478bd9Sstevel@tonic-gate 	 * array).  If we are dealing with a relocatable object, allocate the
21497c478bd9Sstevel@tonic-gate 	 * local symbol descriptors.  If this isn't a relocatable object we
21507c478bd9Sstevel@tonic-gate 	 * still have to process any shared object locals to determine if any
21517c478bd9Sstevel@tonic-gate 	 * register symbols exist.  Although these aren't added to the output
21527c478bd9Sstevel@tonic-gate 	 * image, they are used as part of symbol resolution.
21537c478bd9Sstevel@tonic-gate 	 */
21547c478bd9Sstevel@tonic-gate 	if ((ifl->ifl_oldndx = libld_malloc((size_t)(total *
21556b3ba5bdSAli Bahrami 	    sizeof (Sym_desc *)))) == NULL)
21567c478bd9Sstevel@tonic-gate 		return (S_ERROR);
2157*a8facf26SRichard Lowe 	if ((etype == ET_REL) && (local != 0)) {
21587c478bd9Sstevel@tonic-gate 		if ((ifl->ifl_locs =
21596b3ba5bdSAli Bahrami 		    libld_calloc(sizeof (Sym_desc), local)) == NULL)
21607c478bd9Sstevel@tonic-gate 			return (S_ERROR);
21617c478bd9Sstevel@tonic-gate 		/* LINTED */
2162*a8facf26SRichard Lowe 		ifl->ifl_locscnt = local;
21637c478bd9Sstevel@tonic-gate 	}
21647c478bd9Sstevel@tonic-gate 	ifl->ifl_symscnt = total;
21657c478bd9Sstevel@tonic-gate 
21667c478bd9Sstevel@tonic-gate 	/*
21677c478bd9Sstevel@tonic-gate 	 * If there are local symbols to save add them to the symbol table
21687c478bd9Sstevel@tonic-gate 	 * index array.
21697c478bd9Sstevel@tonic-gate 	 */
2170*a8facf26SRichard Lowe 	if (local != 0) {
217124b9abbaSab 		int		allow_ldynsym = OFL_ALLOW_LDYNSYM(ofl);
217224b9abbaSab 		Sym_desc	*last_file_sdp = NULL;
217324b9abbaSab 		int		last_file_ndx = 0;
217424b9abbaSab 
21757c478bd9Sstevel@tonic-gate 		for (sym++, ndx = 1; ndx < local; sym++, ndx++) {
2176635216b6SRod Evans 			sd_flag_t	sdflags = FLG_SY_CLEAN;
2177635216b6SRod Evans 			Word		shndx;
21787c478bd9Sstevel@tonic-gate 			const char	*name;
21797c478bd9Sstevel@tonic-gate 			Sym_desc	*rsdp;
2180ba2be530Sab 			int		shndx_bad = 0;
21811da7e599SAli Bahrami 			int		symtab_enter = 1;
21827c478bd9Sstevel@tonic-gate 
21837c478bd9Sstevel@tonic-gate 			/*
2184ba2be530Sab 			 * Determine and validate the associated section index.
21857c478bd9Sstevel@tonic-gate 			 */
2186ba2be530Sab 			if (symshndx && (sym->st_shndx == SHN_XINDEX)) {
21877c478bd9Sstevel@tonic-gate 				shndx = symshndx[ndx];
2188ba2be530Sab 			} else if ((shndx = sym->st_shndx) >= SHN_LORESERVE) {
21897c478bd9Sstevel@tonic-gate 				sdflags |= FLG_SY_SPECSEC;
21908878595fSRichard Lowe 			} else if (shndx > ifl->ifl_shnum) {
2191ba2be530Sab 				/* We need the name before we can issue error */
2192ba2be530Sab 				shndx_bad = 1;
2193ba2be530Sab 			}
21947c478bd9Sstevel@tonic-gate 
21957c478bd9Sstevel@tonic-gate 			/*
21967c478bd9Sstevel@tonic-gate 			 * Check if st_name has a valid value or not.
21977c478bd9Sstevel@tonic-gate 			 */
21985aefb655Srie 			if ((name = string(ofl, ifl, sym, strs, strsize, ndx,
21994a8d0ea7SAli Bahrami 			    shndx, symsecndx, symsecname, strsecname,
22001007fd6fSAli Bahrami 			    &sdflags)) == NULL)
22017c478bd9Sstevel@tonic-gate 				continue;
22027c478bd9Sstevel@tonic-gate 
2203ba2be530Sab 			/*
2204ba2be530Sab 			 * Now that we have the name, if the section index
2205ba2be530Sab 			 * was bad, report it.
2206ba2be530Sab 			 */
2207ba2be530Sab 			if (shndx_bad) {
22081007fd6fSAli Bahrami 				ld_eprintf(ofl, ERR_WARNING,
2209ba2be530Sab 				    MSG_INTL(MSG_SYM_INVSHNDX),
22104a8d0ea7SAli Bahrami 				    demangle_symname(name, symsecname, ndx),
2211ba2be530Sab 				    ifl->ifl_name,
22124f680cc6SAli Bahrami 				    conv_sym_shndx(osabi, mach, sym->st_shndx,
22134f680cc6SAli Bahrami 				    CONV_FMT_DECIMAL, &inv_buf));
2214ba2be530Sab 				continue;
2215ba2be530Sab 			}
2216ba2be530Sab 
22177c478bd9Sstevel@tonic-gate 			/*
22187c478bd9Sstevel@tonic-gate 			 * If this local symbol table originates from a shared
22197c478bd9Sstevel@tonic-gate 			 * object, then we're only interested in recording
22207c478bd9Sstevel@tonic-gate 			 * register symbols.  As local symbol descriptors aren't
22217c478bd9Sstevel@tonic-gate 			 * allocated for shared objects, one will be allocated
22227c478bd9Sstevel@tonic-gate 			 * to associated with the register symbol.  This symbol
22237c478bd9Sstevel@tonic-gate 			 * won't become part of the output image, but we must
22247c478bd9Sstevel@tonic-gate 			 * process it to test for register conflicts.
22257c478bd9Sstevel@tonic-gate 			 */
222628bda19cSRod Evans 			rsdp = sdp = NULL;
22277c478bd9Sstevel@tonic-gate 			if (sdflags & FLG_SY_REGSYM) {
2228ba2be530Sab 				/*
2229ba2be530Sab 				 * The presence of FLG_SY_REGSYM means that
2230ba2be530Sab 				 * the pointers in ld_targ.t_ms are non-NULL.
2231ba2be530Sab 				 */
2232ba2be530Sab 				rsdp = (*ld_targ.t_ms.ms_reg_find)(sym, ofl);
2233ba2be530Sab 				if (rsdp != 0) {
22347c478bd9Sstevel@tonic-gate 					/*
22357c478bd9Sstevel@tonic-gate 					 * The fact that another register def-
22367c478bd9Sstevel@tonic-gate 					 * inition has been found is fatal.
22377c478bd9Sstevel@tonic-gate 					 * Call the verification routine to get
22387c478bd9Sstevel@tonic-gate 					 * the error message and move on.
22397c478bd9Sstevel@tonic-gate 					 */
2240ba2be530Sab 					(void) (*ld_targ.t_ms.ms_reg_check)
2241ba2be530Sab 					    (rsdp, sym, name, ifl, ofl);
22427c478bd9Sstevel@tonic-gate 					continue;
22437c478bd9Sstevel@tonic-gate 				}
22447c478bd9Sstevel@tonic-gate 
22457c478bd9Sstevel@tonic-gate 				if (etype == ET_DYN) {
22467c478bd9Sstevel@tonic-gate 					if ((sdp = libld_calloc(
22476b3ba5bdSAli Bahrami 					    sizeof (Sym_desc), 1)) == NULL)
22487c478bd9Sstevel@tonic-gate 						return (S_ERROR);
22497c478bd9Sstevel@tonic-gate 					sdp->sd_ref = REF_DYN_SEEN;
2250ca4eed8bSAli Bahrami 
2251ca4eed8bSAli Bahrami 					/* Will not appear in output object */
22521da7e599SAli Bahrami 					symtab_enter = 0;
22537c478bd9Sstevel@tonic-gate 				}
2254*a8facf26SRichard Lowe 			} else if (etype == ET_DYN) {
22557c478bd9Sstevel@tonic-gate 				continue;
2256*a8facf26SRichard Lowe 			}
22577c478bd9Sstevel@tonic-gate 
22587c478bd9Sstevel@tonic-gate 			/*
22597c478bd9Sstevel@tonic-gate 			 * Fill in the remaining symbol descriptor information.
22607c478bd9Sstevel@tonic-gate 			 */
22616b3ba5bdSAli Bahrami 			if (sdp == NULL) {
22627c478bd9Sstevel@tonic-gate 				sdp = &(ifl->ifl_locs[ndx]);
22637c478bd9Sstevel@tonic-gate 				sdp->sd_ref = REF_REL_NEED;
2264635216b6SRod Evans 				sdp->sd_symndx = ndx;
22657c478bd9Sstevel@tonic-gate 			}
22666b3ba5bdSAli Bahrami 			if (rsdp == NULL) {
22677c478bd9Sstevel@tonic-gate 				sdp->sd_name = name;
22687c478bd9Sstevel@tonic-gate 				sdp->sd_sym = sym;
22697c478bd9Sstevel@tonic-gate 				sdp->sd_shndx = shndx;
22707c478bd9Sstevel@tonic-gate 				sdp->sd_flags = sdflags;
22717c478bd9Sstevel@tonic-gate 				sdp->sd_file = ifl;
22727c478bd9Sstevel@tonic-gate 				ifl->ifl_oldndx[ndx] = sdp;
22737c478bd9Sstevel@tonic-gate 			}
22747c478bd9Sstevel@tonic-gate 
22755aefb655Srie 			DBG_CALL(Dbg_syms_entry(ofl->ofl_lml, ndx, sdp));
22767c478bd9Sstevel@tonic-gate 
22777c478bd9Sstevel@tonic-gate 			/*
22787c478bd9Sstevel@tonic-gate 			 * Reclassify any SHN_SUNW_IGNORE symbols to SHN_UNDEF
22797c478bd9Sstevel@tonic-gate 			 * so as to simplify future processing.
22807c478bd9Sstevel@tonic-gate 			 */
22810bc07c75Srie 			if (sym->st_shndx == SHN_SUNW_IGNORE) {
22827c478bd9Sstevel@tonic-gate 				sdp->sd_shndx = shndx = SHN_UNDEF;
2283635216b6SRod Evans 				sdp->sd_flags |= (FLG_SY_IGNORE | FLG_SY_ELIM);
22847c478bd9Sstevel@tonic-gate 			}
22857c478bd9Sstevel@tonic-gate 
22867c478bd9Sstevel@tonic-gate 			/*
22877c478bd9Sstevel@tonic-gate 			 * Process any register symbols.
22887c478bd9Sstevel@tonic-gate 			 */
22897c478bd9Sstevel@tonic-gate 			if (sdp->sd_flags & FLG_SY_REGSYM) {
22907c478bd9Sstevel@tonic-gate 				/*
22917c478bd9Sstevel@tonic-gate 				 * Add a diagnostic to indicate we've caught a
22927c478bd9Sstevel@tonic-gate 				 * register symbol, as this can be useful if a
22937c478bd9Sstevel@tonic-gate 				 * register conflict is later discovered.
22947c478bd9Sstevel@tonic-gate 				 */
22955aefb655Srie 				DBG_CALL(Dbg_syms_entered(ofl, sym, sdp));
22967c478bd9Sstevel@tonic-gate 
22977c478bd9Sstevel@tonic-gate 				/*
22987c478bd9Sstevel@tonic-gate 				 * If this register symbol hasn't already been
22997c478bd9Sstevel@tonic-gate 				 * recorded, enter it now.
2300ba2be530Sab 				 *
2301ba2be530Sab 				 * The presence of FLG_SY_REGSYM means that
2302ba2be530Sab 				 * the pointers in ld_targ.t_ms are non-NULL.
23037c478bd9Sstevel@tonic-gate 				 */
23046b3ba5bdSAli Bahrami 				if ((rsdp == NULL) &&
2305ba2be530Sab 				    ((*ld_targ.t_ms.ms_reg_enter)(sdp, ofl) ==
2306ba2be530Sab 				    0))
23077c478bd9Sstevel@tonic-gate 					return (S_ERROR);
23087c478bd9Sstevel@tonic-gate 			}
23097c478bd9Sstevel@tonic-gate 
23107c478bd9Sstevel@tonic-gate 			/*
23117c478bd9Sstevel@tonic-gate 			 * Assign an input section.
23127c478bd9Sstevel@tonic-gate 			 */
23130bc07c75Srie 			if ((sym->st_shndx != SHN_UNDEF) &&
23147c478bd9Sstevel@tonic-gate 			    ((sdp->sd_flags & FLG_SY_SPECSEC) == 0))
23157c478bd9Sstevel@tonic-gate 				sdp->sd_isc = ifl->ifl_isdesc[shndx];
23167c478bd9Sstevel@tonic-gate 
23177c478bd9Sstevel@tonic-gate 			/*
23187c478bd9Sstevel@tonic-gate 			 * If this symbol falls within the range of a section
23197c478bd9Sstevel@tonic-gate 			 * being discarded, then discard the symbol itself.
23207c478bd9Sstevel@tonic-gate 			 * There is no reason to keep this local symbol.
23217c478bd9Sstevel@tonic-gate 			 */
23227c478bd9Sstevel@tonic-gate 			if (sdp->sd_isc &&
23237c478bd9Sstevel@tonic-gate 			    (sdp->sd_isc->is_flags & FLG_IS_DISCARD)) {
23247c478bd9Sstevel@tonic-gate 				sdp->sd_flags |= FLG_SY_ISDISC;
2325a194faf8Srie 				DBG_CALL(Dbg_syms_discarded(ofl->ofl_lml, sdp));
23267c478bd9Sstevel@tonic-gate 				continue;
23277c478bd9Sstevel@tonic-gate 			}
23287c478bd9Sstevel@tonic-gate 
23297c478bd9Sstevel@tonic-gate 			/*
23307c478bd9Sstevel@tonic-gate 			 * Skip any section symbols as new versions of these
23317c478bd9Sstevel@tonic-gate 			 * will be created.
23327c478bd9Sstevel@tonic-gate 			 */
23337c478bd9Sstevel@tonic-gate 			if ((type = ELF_ST_TYPE(sym->st_info)) == STT_SECTION) {
23340bc07c75Srie 				if (sym->st_shndx == SHN_UNDEF) {
23351007fd6fSAli Bahrami 					ld_eprintf(ofl, ERR_WARNING,
23367c478bd9Sstevel@tonic-gate 					    MSG_INTL(MSG_SYM_INVSHNDX),
23374a8d0ea7SAli Bahrami 					    demangle_symname(name, symsecname,
2338ba2be530Sab 					    ndx), ifl->ifl_name,
23394f680cc6SAli Bahrami 					    conv_sym_shndx(osabi, mach,
23404f680cc6SAli Bahrami 					    sym->st_shndx, CONV_FMT_DECIMAL,
2341de777a60Sab 					    &inv_buf));
23427c478bd9Sstevel@tonic-gate 				}
23437c478bd9Sstevel@tonic-gate 				continue;
23447c478bd9Sstevel@tonic-gate 			}
23457c478bd9Sstevel@tonic-gate 
234675e45495Sab 			/*
234775e45495Sab 			 * For a relocatable object, if this symbol is defined
234875e45495Sab 			 * and has non-zero length and references an address
234975e45495Sab 			 * within an associated section, then check its extents
235075e45495Sab 			 * to make sure the section boundaries encompass it.
235175e45495Sab 			 * If they don't, the ELF file is corrupt.
235275e45495Sab 			 */
2353*a8facf26SRichard Lowe 			if (etype == ET_REL) {
235424b9abbaSab 				if (SYM_LOC_BADADDR(sdp, sym, type)) {
235524b9abbaSab 					issue_badaddr_msg(ifl, ofl, sdp,
235624b9abbaSab 					    sym, shndx);
2357ca4eed8bSAli Bahrami 					if (ofl->ofl_flags & FLG_OF_FATAL)
2358ca4eed8bSAli Bahrami 						continue;
235924b9abbaSab 				}
236024b9abbaSab 
236124b9abbaSab 				/*
236224b9abbaSab 				 * We have observed relocatable objects
236324b9abbaSab 				 * containing identical adjacent STT_FILE
236424b9abbaSab 				 * symbols. Discard any other than the first,
236524b9abbaSab 				 * as they are all equivalent and the extras
236624b9abbaSab 				 * do not add information.
236724b9abbaSab 				 *
236824b9abbaSab 				 * For the purpose of this test, we assume
236924b9abbaSab 				 * that only the symbol type and the string
237024b9abbaSab 				 * table offset (st_name) matter.
237124b9abbaSab 				 */
237224b9abbaSab 				if (type == STT_FILE) {
237324b9abbaSab 					int toss = (last_file_sdp != NULL) &&
237424b9abbaSab 					    ((ndx - 1) == last_file_ndx) &&
237524b9abbaSab 					    (sym->st_name ==
237624b9abbaSab 					    last_file_sdp->sd_sym->st_name);
237724b9abbaSab 
237824b9abbaSab 					last_file_sdp = sdp;
237924b9abbaSab 					last_file_ndx = ndx;
238024b9abbaSab 					if (toss) {
238124b9abbaSab 						sdp->sd_flags |= FLG_SY_INVALID;
238224b9abbaSab 						DBG_CALL(Dbg_syms_dup_discarded(
238324b9abbaSab 						    ofl->ofl_lml, ndx, sdp));
238424b9abbaSab 						continue;
238524b9abbaSab 					}
238624b9abbaSab 				}
238775e45495Sab 			}
238875e45495Sab 
2389*a8facf26SRichard Lowe 			/*
2390*a8facf26SRichard Lowe 			 * If this symbol comes from a relocatable object and
2391*a8facf26SRichard Lowe 			 * looks like a GCC local function alias, don't
2392*a8facf26SRichard Lowe 			 * include it in dynsort sections, since the global
2393*a8facf26SRichard Lowe 			 * name will always be preferable.
2394*a8facf26SRichard Lowe 			 */
2395*a8facf26SRichard Lowe 			if ((etype == ET_REL) && is_gcc_localalias(sdp))
2396*a8facf26SRichard Lowe 				sdp->sd_flags |= FLG_SY_NODYNSORT;
239724b9abbaSab 
23987c478bd9Sstevel@tonic-gate 			/*
23997c478bd9Sstevel@tonic-gate 			 * Sanity check for TLS
24007c478bd9Sstevel@tonic-gate 			 */
24010bc07c75Srie 			if ((sym->st_size != 0) && ((type == STT_TLS) &&
24020bc07c75Srie 			    (sym->st_shndx != SHN_COMMON))) {
24037c478bd9Sstevel@tonic-gate 				Is_desc	*isp = sdp->sd_isc;
24047c478bd9Sstevel@tonic-gate 
24056b3ba5bdSAli Bahrami 				if ((isp == NULL) || (isp->is_shdr == NULL) ||
24067c478bd9Sstevel@tonic-gate 				    ((isp->is_shdr->sh_flags & SHF_TLS) == 0)) {
24071007fd6fSAli Bahrami 					ld_eprintf(ofl, ERR_FATAL,
24087c478bd9Sstevel@tonic-gate 					    MSG_INTL(MSG_SYM_TLS),
24097c478bd9Sstevel@tonic-gate 					    demangle(sdp->sd_name),
24107c478bd9Sstevel@tonic-gate 					    ifl->ifl_name);
24117c478bd9Sstevel@tonic-gate 					continue;
24127c478bd9Sstevel@tonic-gate 				}
24137c478bd9Sstevel@tonic-gate 			}
24147c478bd9Sstevel@tonic-gate 
24157c478bd9Sstevel@tonic-gate 			/*
24167c478bd9Sstevel@tonic-gate 			 * Carry our some basic sanity checks (these are just
24177c478bd9Sstevel@tonic-gate 			 * some of the erroneous symbol entries we've come
24187c478bd9Sstevel@tonic-gate 			 * across, there's probably a lot more).  The symbol
24197c478bd9Sstevel@tonic-gate 			 * will not be carried forward to the output file, which
24207c478bd9Sstevel@tonic-gate 			 * won't be a problem unless a relocation is required
24217c478bd9Sstevel@tonic-gate 			 * against it.
24227c478bd9Sstevel@tonic-gate 			 */
24237c478bd9Sstevel@tonic-gate 			if (((sdp->sd_flags & FLG_SY_SPECSEC) &&
24240bc07c75Srie 			    ((sym->st_shndx == SHN_COMMON)) ||
24250bc07c75Srie 			    ((type == STT_FILE) &&
24260bc07c75Srie 			    (sym->st_shndx != SHN_ABS))) ||
24276b3ba5bdSAli Bahrami 			    (sdp->sd_isc && (sdp->sd_isc->is_osdesc == NULL))) {
24281007fd6fSAli Bahrami 				ld_eprintf(ofl, ERR_WARNING,
24295aefb655Srie 				    MSG_INTL(MSG_SYM_INVSHNDX),
24304a8d0ea7SAli Bahrami 				    demangle_symname(name, symsecname, ndx),
2431ba2be530Sab 				    ifl->ifl_name,
24324f680cc6SAli Bahrami 				    conv_sym_shndx(osabi, mach, sym->st_shndx,
24334f680cc6SAli Bahrami 				    CONV_FMT_DECIMAL, &inv_buf));
24347c478bd9Sstevel@tonic-gate 				sdp->sd_isc = NULL;
24357c478bd9Sstevel@tonic-gate 				sdp->sd_flags |= FLG_SY_INVALID;
24367c478bd9Sstevel@tonic-gate 				continue;
24377c478bd9Sstevel@tonic-gate 			}
24387c478bd9Sstevel@tonic-gate 
24397c478bd9Sstevel@tonic-gate 			/*
24407c478bd9Sstevel@tonic-gate 			 * As these local symbols will become part of the output
24417c478bd9Sstevel@tonic-gate 			 * image, record their number and name string size.
24427c478bd9Sstevel@tonic-gate 			 * Globals are counted after all input file processing
24437c478bd9Sstevel@tonic-gate 			 * (and hence symbol resolution) is complete during
24447c478bd9Sstevel@tonic-gate 			 * sym_validate().
24457c478bd9Sstevel@tonic-gate 			 */
24461da7e599SAli Bahrami 			if (!(ofl->ofl_flags & FLG_OF_REDLSYM) &&
24471da7e599SAli Bahrami 			    symtab_enter) {
24487c478bd9Sstevel@tonic-gate 				ofl->ofl_locscnt++;
24497c478bd9Sstevel@tonic-gate 
24507c478bd9Sstevel@tonic-gate 				if ((((sdp->sd_flags & FLG_SY_REGSYM) == 0) ||
24517c478bd9Sstevel@tonic-gate 				    sym->st_name) && (st_insert(ofl->ofl_strtab,
24527c478bd9Sstevel@tonic-gate 				    sdp->sd_name) == -1))
24537c478bd9Sstevel@tonic-gate 					return (S_ERROR);
24549039eeafSab 
24559039eeafSab 				if (allow_ldynsym && sym->st_name &&
2456d579eb63Sab 				    ldynsym_symtype[type]) {
24579039eeafSab 					ofl->ofl_dynlocscnt++;
24589039eeafSab 					if (st_insert(ofl->ofl_dynstrtab,
24599039eeafSab 					    sdp->sd_name) == -1)
24609039eeafSab 						return (S_ERROR);
2461d579eb63Sab 					/* Include it in sort section? */
2462d579eb63Sab 					DYNSORT_COUNT(sdp, sym, type, ++);
24639039eeafSab 				}
24647c478bd9Sstevel@tonic-gate 			}
24657c478bd9Sstevel@tonic-gate 		}
24667c478bd9Sstevel@tonic-gate 	}
24677c478bd9Sstevel@tonic-gate 
2468d840867fSab 	/*
2469d840867fSab 	 * The GNU ld interprets the top bit of the 16-bit Versym value
2470d840867fSab 	 * (0x8000) as the "hidden" bit. If this bit is set, the linker
2471d840867fSab 	 * is supposed to act as if that symbol does not exist. The Solaris
2472d840867fSab 	 * linker does not support this mechanism, or the model of interface
2473d840867fSab 	 * evolution that it allows, but we honor it in GNU ld produced
2474d840867fSab 	 * objects in order to interoperate with them.
2475d840867fSab 	 *
2476d840867fSab 	 * Determine if we should honor the GNU hidden bit for this file.
2477d840867fSab 	 */
2478d840867fSab 	test_gnu_hidden_bit = ((ifl->ifl_flags & FLG_IF_GNUVER) != 0) &&
2479d840867fSab 	    (ifl->ifl_versym != NULL);
2480d840867fSab 
248108278a5eSRod Evans 	/*
248208278a5eSRod Evans 	 * Determine whether object capabilities for this file are being
248308278a5eSRod Evans 	 * converted into symbol capabilities.  If so, global function symbols,
248408278a5eSRod Evans 	 * and initialized global data symbols, need special translation and
248508278a5eSRod Evans 	 * processing.
248608278a5eSRod Evans 	 */
248708278a5eSRod Evans 	if ((etype == ET_REL) && (ifl->ifl_flags & FLG_IF_OTOSCAP))
248808278a5eSRod Evans 		cdp = ifl->ifl_caps;
248908278a5eSRod Evans 
24907c478bd9Sstevel@tonic-gate 	/*
24917c478bd9Sstevel@tonic-gate 	 * Now scan the global symbols entering them in the internal symbol
24927c478bd9Sstevel@tonic-gate 	 * table or resolving them as necessary.
24937c478bd9Sstevel@tonic-gate 	 */
24947c478bd9Sstevel@tonic-gate 	sym = (Sym *)isc->is_indata->d_buf;
24957c478bd9Sstevel@tonic-gate 	sym += local;
24966b3ba5bdSAli Bahrami 	weak = 0;
24977c478bd9Sstevel@tonic-gate 	/* LINTED */
24987c478bd9Sstevel@tonic-gate 	for (ndx = (int)local; ndx < total; sym++, ndx++) {
24997c478bd9Sstevel@tonic-gate 		const char	*name;
2500635216b6SRod Evans 		sd_flag_t	sdflags = 0;
2501635216b6SRod Evans 		Word		shndx;
2502ba2be530Sab 		int		shndx_bad = 0;
2503635216b6SRod Evans 		Sym		*nsym = sym;
250408278a5eSRod Evans 		Cap_pair	*cpp = NULL;
250508278a5eSRod Evans 		uchar_t		ntype;
25067c478bd9Sstevel@tonic-gate 
25077c478bd9Sstevel@tonic-gate 		/*
2508ba2be530Sab 		 * Determine and validate the associated section index.
25097c478bd9Sstevel@tonic-gate 		 */
2510635216b6SRod Evans 		if (symshndx && (nsym->st_shndx == SHN_XINDEX)) {
25117c478bd9Sstevel@tonic-gate 			shndx = symshndx[ndx];
2512635216b6SRod Evans 		} else if ((shndx = nsym->st_shndx) >= SHN_LORESERVE) {
2513ba2be530Sab 			sdflags |= FLG_SY_SPECSEC;
25148878595fSRichard Lowe 		} else if (shndx > ifl->ifl_shnum) {
2515ba2be530Sab 			/* We need the name before we can issue error */
2516ba2be530Sab 			shndx_bad = 1;
25177c478bd9Sstevel@tonic-gate 		}
25187c478bd9Sstevel@tonic-gate 
25197c478bd9Sstevel@tonic-gate 		/*
25207c478bd9Sstevel@tonic-gate 		 * Check if st_name has a valid value or not.
25217c478bd9Sstevel@tonic-gate 		 */
2522635216b6SRod Evans 		if ((name = string(ofl, ifl, nsym, strs, strsize, ndx, shndx,
25231007fd6fSAli Bahrami 		    symsecndx, symsecname, strsecname, &sdflags)) == NULL)
25247c478bd9Sstevel@tonic-gate 			continue;
25257c478bd9Sstevel@tonic-gate 
2526ba2be530Sab 		/*
252708278a5eSRod Evans 		 * Now that we have the name, report an erroneous section index.
2528ba2be530Sab 		 */
2529ba2be530Sab 		if (shndx_bad) {
25301007fd6fSAli Bahrami 			ld_eprintf(ofl, ERR_WARNING, MSG_INTL(MSG_SYM_INVSHNDX),
25314a8d0ea7SAli Bahrami 			    demangle_symname(name, symsecname, ndx),
2532ba2be530Sab 			    ifl->ifl_name,
2533635216b6SRod Evans 			    conv_sym_shndx(osabi, mach, nsym->st_shndx,
25344f680cc6SAli Bahrami 			    CONV_FMT_DECIMAL, &inv_buf));
2535ba2be530Sab 			continue;
2536ba2be530Sab 		}
2537ba2be530Sab 
25383b41b08bSab 		/*
2539d840867fSab 		 * Test for the GNU hidden bit, and ignore symbols that
2540d840867fSab 		 * have it set.
25413b41b08bSab 		 */
2542d840867fSab 		if (test_gnu_hidden_bit &&
2543d840867fSab 		    ((ifl->ifl_versym[ndx] & 0x8000) != 0))
25443b41b08bSab 			continue;
25453b41b08bSab 
25467c478bd9Sstevel@tonic-gate 		/*
25477c478bd9Sstevel@tonic-gate 		 * The linker itself will generate symbols for _end, _etext,
25487c478bd9Sstevel@tonic-gate 		 * _edata, _DYNAMIC and _PROCEDURE_LINKAGE_TABLE_, so don't
25497c478bd9Sstevel@tonic-gate 		 * bother entering these symbols from shared objects.  This
25507c478bd9Sstevel@tonic-gate 		 * results in some wasted resolution processing, which is hard
25517c478bd9Sstevel@tonic-gate 		 * to feel, but if nothing else, pollutes diagnostic relocation
25527c478bd9Sstevel@tonic-gate 		 * output.
25537c478bd9Sstevel@tonic-gate 		 */
2554635216b6SRod Evans 		if (name[0] && (etype == ET_DYN) && (nsym->st_size == 0) &&
2555635216b6SRod Evans 		    (ELF_ST_TYPE(nsym->st_info) == STT_OBJECT) &&
25567c478bd9Sstevel@tonic-gate 		    (name[0] == '_') && ((name[1] == 'e') ||
25577c478bd9Sstevel@tonic-gate 		    (name[1] == 'D') || (name[1] == 'P')) &&
25587c478bd9Sstevel@tonic-gate 		    ((strcmp(name, MSG_ORIG(MSG_SYM_ETEXT_U)) == 0) ||
25597c478bd9Sstevel@tonic-gate 		    (strcmp(name, MSG_ORIG(MSG_SYM_EDATA_U)) == 0) ||
25607c478bd9Sstevel@tonic-gate 		    (strcmp(name, MSG_ORIG(MSG_SYM_END_U)) == 0) ||
25617c478bd9Sstevel@tonic-gate 		    (strcmp(name, MSG_ORIG(MSG_SYM_DYNAMIC_U)) == 0) ||
25627c478bd9Sstevel@tonic-gate 		    (strcmp(name, MSG_ORIG(MSG_SYM_PLKTBL_U)) == 0))) {
25637c478bd9Sstevel@tonic-gate 			ifl->ifl_oldndx[ndx] = 0;
25647c478bd9Sstevel@tonic-gate 			continue;
25657c478bd9Sstevel@tonic-gate 		}
25667c478bd9Sstevel@tonic-gate 
2567cdcc71c0SAli Bahrami 		/*
2568cdcc71c0SAli Bahrami 		 * The '-z wrap=XXX' option emulates the GNU ld --wrap=XXX
2569cdcc71c0SAli Bahrami 		 * option. When XXX is the symbol to be wrapped:
2570cdcc71c0SAli Bahrami 		 *
257108278a5eSRod Evans 		 *  -	An undefined reference to XXX is converted to __wrap_XXX
257208278a5eSRod Evans 		 *  -	An undefined reference to __real_XXX is converted to XXX
2573cdcc71c0SAli Bahrami 		 *
2574cdcc71c0SAli Bahrami 		 * The idea is that the user can supply a wrapper function
2575cdcc71c0SAli Bahrami 		 * __wrap_XXX that does some work, and then uses the name
2576cdcc71c0SAli Bahrami 		 * __real_XXX to pass the call on to the real function. The
2577cdcc71c0SAli Bahrami 		 * wrapper objects are linked with the original unmodified
2578cdcc71c0SAli Bahrami 		 * objects to produce a wrapped version of the output object.
2579cdcc71c0SAli Bahrami 		 */
2580cdcc71c0SAli Bahrami 		if (ofl->ofl_wrap && name[0] && (shndx == SHN_UNDEF)) {
2581cdcc71c0SAli Bahrami 			WrapSymNode wsn, *wsnp;
2582cdcc71c0SAli Bahrami 
2583cdcc71c0SAli Bahrami 			/*
2584cdcc71c0SAli Bahrami 			 * If this is the __real_XXX form, advance the
2585cdcc71c0SAli Bahrami 			 * pointer to reference the wrapped name.
2586cdcc71c0SAli Bahrami 			 */
2587cdcc71c0SAli Bahrami 			wsn.wsn_name = name;
2588cdcc71c0SAli Bahrami 			if ((*name == '_') &&
2589cdcc71c0SAli Bahrami 			    (strncmp(name, MSG_ORIG(MSG_STR_UU_REAL_U),
2590cdcc71c0SAli Bahrami 			    MSG_STR_UU_REAL_U_SIZE) == 0))
2591cdcc71c0SAli Bahrami 				wsn.wsn_name += MSG_STR_UU_REAL_U_SIZE;
2592cdcc71c0SAli Bahrami 
2593cdcc71c0SAli Bahrami 			/*
2594cdcc71c0SAli Bahrami 			 * Is this symbol in the wrap AVL tree? If so, map
2595cdcc71c0SAli Bahrami 			 * XXX to __wrap_XXX, and __real_XXX to XXX. Note that
2596cdcc71c0SAli Bahrami 			 * wsn.wsn_name will equal the current value of name
2597cdcc71c0SAli Bahrami 			 * if the __real_ prefix is not present.
2598cdcc71c0SAli Bahrami 			 */
2599cdcc71c0SAli Bahrami 			if ((wsnp = avl_find(ofl->ofl_wrap, &wsn, 0)) != NULL) {
2600cdcc71c0SAli Bahrami 				const char *old_name = name;
2601cdcc71c0SAli Bahrami 
2602cdcc71c0SAli Bahrami 				name = (wsn.wsn_name == name) ?
2603cdcc71c0SAli Bahrami 				    wsnp->wsn_wrapname : wsn.wsn_name;
2604cdcc71c0SAli Bahrami 				DBG_CALL(Dbg_syms_wrap(ofl->ofl_lml, ndx,
2605cdcc71c0SAli Bahrami 				    old_name, name));
2606cdcc71c0SAli Bahrami 			}
2607cdcc71c0SAli Bahrami 		}
2608cdcc71c0SAli Bahrami 
26097c478bd9Sstevel@tonic-gate 		/*
26107c478bd9Sstevel@tonic-gate 		 * Determine and validate the symbols binding.
26117c478bd9Sstevel@tonic-gate 		 */
2612635216b6SRod Evans 		bind = ELF_ST_BIND(nsym->st_info);
26137c478bd9Sstevel@tonic-gate 		if ((bind != STB_GLOBAL) && (bind != STB_WEAK)) {
26141007fd6fSAli Bahrami 			ld_eprintf(ofl, ERR_WARNING, MSG_INTL(MSG_SYM_NONGLOB),
26154a8d0ea7SAli Bahrami 			    demangle_symname(name, symsecname, ndx),
2616de777a60Sab 			    ifl->ifl_name,
2617de777a60Sab 			    conv_sym_info_bind(bind, 0, &inv_buf));
26187c478bd9Sstevel@tonic-gate 			continue;
26197c478bd9Sstevel@tonic-gate 		}
26206b3ba5bdSAli Bahrami 		if (bind == STB_WEAK)
26216b3ba5bdSAli Bahrami 			weak++;
26227c478bd9Sstevel@tonic-gate 
26237c478bd9Sstevel@tonic-gate 		/*
26247c478bd9Sstevel@tonic-gate 		 * If this symbol falls within the range of a section being
26257c478bd9Sstevel@tonic-gate 		 * discarded, then discard the symbol itself.
26267c478bd9Sstevel@tonic-gate 		 */
26277c478bd9Sstevel@tonic-gate 		if (((sdflags & FLG_SY_SPECSEC) == 0) &&
2628635216b6SRod Evans 		    (nsym->st_shndx != SHN_UNDEF)) {
26297c478bd9Sstevel@tonic-gate 			Is_desc	*isp;
26307c478bd9Sstevel@tonic-gate 
26317c478bd9Sstevel@tonic-gate 			if (shndx >= ifl->ifl_shnum) {
26327c478bd9Sstevel@tonic-gate 				/*
26337c478bd9Sstevel@tonic-gate 				 * Carry our some basic sanity checks
26347c478bd9Sstevel@tonic-gate 				 * The symbol will not be carried forward to
26357c478bd9Sstevel@tonic-gate 				 * the output file, which won't be a problem
26367c478bd9Sstevel@tonic-gate 				 * unless a relocation is required against it.
26377c478bd9Sstevel@tonic-gate 				 */
26381007fd6fSAli Bahrami 				ld_eprintf(ofl, ERR_WARNING,
2639ba2be530Sab 				    MSG_INTL(MSG_SYM_INVSHNDX),
26404a8d0ea7SAli Bahrami 				    demangle_symname(name, symsecname, ndx),
26410bc07c75Srie 				    ifl->ifl_name,
2642635216b6SRod Evans 				    conv_sym_shndx(osabi, mach, nsym->st_shndx,
26434f680cc6SAli Bahrami 				    CONV_FMT_DECIMAL, &inv_buf));
26447c478bd9Sstevel@tonic-gate 				continue;
26457c478bd9Sstevel@tonic-gate 			}
26467c478bd9Sstevel@tonic-gate 
26477c478bd9Sstevel@tonic-gate 			isp = ifl->ifl_isdesc[shndx];
26487c478bd9Sstevel@tonic-gate 			if (isp && (isp->is_flags & FLG_IS_DISCARD)) {
26497c478bd9Sstevel@tonic-gate 				if ((sdp =
26506b3ba5bdSAli Bahrami 				    libld_calloc(sizeof (Sym_desc), 1)) == NULL)
26517c478bd9Sstevel@tonic-gate 					return (S_ERROR);
26527c478bd9Sstevel@tonic-gate 
26537c478bd9Sstevel@tonic-gate 				/*
26547c478bd9Sstevel@tonic-gate 				 * Create a dummy symbol entry so that if we
26557c478bd9Sstevel@tonic-gate 				 * find any references to this discarded symbol
26567c478bd9Sstevel@tonic-gate 				 * we can compensate.
26577c478bd9Sstevel@tonic-gate 				 */
26587c478bd9Sstevel@tonic-gate 				sdp->sd_name = name;
2659635216b6SRod Evans 				sdp->sd_sym = nsym;
26607c478bd9Sstevel@tonic-gate 				sdp->sd_file = ifl;
26617c478bd9Sstevel@tonic-gate 				sdp->sd_isc = isp;
26627c478bd9Sstevel@tonic-gate 				sdp->sd_flags = FLG_SY_ISDISC;
26637c478bd9Sstevel@tonic-gate 				ifl->ifl_oldndx[ndx] = sdp;
26647c478bd9Sstevel@tonic-gate 
2665a194faf8Srie 				DBG_CALL(Dbg_syms_discarded(ofl->ofl_lml, sdp));
26667c478bd9Sstevel@tonic-gate 				continue;
26677c478bd9Sstevel@tonic-gate 			}
26687c478bd9Sstevel@tonic-gate 		}
26697c478bd9Sstevel@tonic-gate 
267008278a5eSRod Evans 		/*
267108278a5eSRod Evans 		 * If object capabilities for this file are being converted
267208278a5eSRod Evans 		 * into symbol capabilities, then:
267308278a5eSRod Evans 		 *
267408278a5eSRod Evans 		 *  -	Any global function, or initialized global data symbol
267508278a5eSRod Evans 		 *	definitions (ie., those that are not associated with
267608278a5eSRod Evans 		 *	special symbol types, ie., ABS, COMMON, etc.), and which
267708278a5eSRod Evans 		 *	have not been reduced to locals, are converted to symbol
267808278a5eSRod Evans 		 *	references (UNDEF).  This ensures that any reference to
267908278a5eSRod Evans 		 *	the original symbol, for example from a relocation, get
268008278a5eSRod Evans 		 *	associated to a capabilities family lead symbol, ie., a
268108278a5eSRod Evans 		 *	generic instance.
268208278a5eSRod Evans 		 *
268308278a5eSRod Evans 		 *  -	For each global function, or object symbol definition,
268408278a5eSRod Evans 		 *	a new local symbol is created.  The function or object
268508278a5eSRod Evans 		 *	is renamed using the capabilities CA_SUNW_ID definition
268608278a5eSRod Evans 		 *	(which might have been fabricated for this purpose -
268708278a5eSRod Evans 		 *	see get_cap_group()).  The new symbol name is:
268808278a5eSRod Evans 		 *
268908278a5eSRod Evans 		 *	    <original name>%<capability group identifier>
269008278a5eSRod Evans 		 *
269108278a5eSRod Evans 		 *	This symbol is associated to the same location, and
269208278a5eSRod Evans 		 *	becomes a capabilities family member.
269308278a5eSRod Evans 		 */
269408278a5eSRod Evans 		/* LINTED */
269508278a5eSRod Evans 		hash = (Word)elf_hash(name);
269608278a5eSRod Evans 
269708278a5eSRod Evans 		ntype = ELF_ST_TYPE(nsym->st_info);
269808278a5eSRod Evans 		if (cdp && (nsym->st_shndx != SHN_UNDEF) &&
269908278a5eSRod Evans 		    ((sdflags & FLG_SY_SPECSEC) == 0) &&
270008278a5eSRod Evans 		    ((ntype == STT_FUNC) || (ntype == STT_OBJECT))) {
270108278a5eSRod Evans 			/*
270208278a5eSRod Evans 			 * Determine this symbol's visibility.  If a mapfile has
270308278a5eSRod Evans 			 * indicated this symbol should be local, then there's
270408278a5eSRod Evans 			 * no point in transforming this global symbol to a
270508278a5eSRod Evans 			 * capabilities symbol.  Otherwise, create a symbol
270608278a5eSRod Evans 			 * capability pair descriptor to record this symbol as
270708278a5eSRod Evans 			 * a candidate for translation.
270808278a5eSRod Evans 			 */
270908278a5eSRod Evans 			if (sym_cap_vis(name, hash, sym, ofl) &&
271008278a5eSRod Evans 			    ((cpp = alist_append(&cappairs, NULL,
271108278a5eSRod Evans 			    sizeof (Cap_pair), AL_CNT_CAP_PAIRS)) == NULL))
271208278a5eSRod Evans 				return (S_ERROR);
271308278a5eSRod Evans 		}
271408278a5eSRod Evans 
271508278a5eSRod Evans 		if (cpp) {
271608278a5eSRod Evans 			Sym	*rsym;
271708278a5eSRod Evans 
271808278a5eSRod Evans 			DBG_CALL(Dbg_syms_cap_convert(ofl, ndx, name, nsym));
271908278a5eSRod Evans 
272008278a5eSRod Evans 			/*
272108278a5eSRod Evans 			 * Allocate a new symbol descriptor to represent the
272208278a5eSRod Evans 			 * transformed global symbol.  The descriptor points
272308278a5eSRod Evans 			 * to the original symbol information (which might
272408278a5eSRod Evans 			 * indicate a global or weak visibility).  The symbol
272508278a5eSRod Evans 			 * information will be transformed into a local symbol
272608278a5eSRod Evans 			 * later, after any weak aliases are culled.
272708278a5eSRod Evans 			 */
272808278a5eSRod Evans 			if ((cpp->c_osdp =
272908278a5eSRod Evans 			    libld_malloc(sizeof (Sym_desc))) == NULL)
273008278a5eSRod Evans 				return (S_ERROR);
273108278a5eSRod Evans 
273208278a5eSRod Evans 			cpp->c_osdp->sd_name = name;
273308278a5eSRod Evans 			cpp->c_osdp->sd_sym = nsym;
273408278a5eSRod Evans 			cpp->c_osdp->sd_shndx = shndx;
273508278a5eSRod Evans 			cpp->c_osdp->sd_file = ifl;
273608278a5eSRod Evans 			cpp->c_osdp->sd_isc = ifl->ifl_isdesc[shndx];
273708278a5eSRod Evans 			cpp->c_osdp->sd_ref = REF_REL_NEED;
273808278a5eSRod Evans 
273908278a5eSRod Evans 			/*
274008278a5eSRod Evans 			 * Save the capabilities group this symbol belongs to,
274108278a5eSRod Evans 			 * and the original symbol index.
274208278a5eSRod Evans 			 */
274308278a5eSRod Evans 			cpp->c_group = cdp->ca_groups->apl_data[0];
274408278a5eSRod Evans 			cpp->c_ndx = ndx;
274508278a5eSRod Evans 
274608278a5eSRod Evans 			/*
274708278a5eSRod Evans 			 * Replace the original symbol definition with a symbol
274808278a5eSRod Evans 			 * reference.  Make sure this reference isn't left as a
274908278a5eSRod Evans 			 * weak.
275008278a5eSRod Evans 			 */
275108278a5eSRod Evans 			if ((rsym = libld_malloc(sizeof (Sym))) == NULL)
275208278a5eSRod Evans 				return (S_ERROR);
275308278a5eSRod Evans 
275408278a5eSRod Evans 			*rsym = *nsym;
275508278a5eSRod Evans 
275608278a5eSRod Evans 			rsym->st_info = ELF_ST_INFO(STB_GLOBAL, ntype);
275708278a5eSRod Evans 			rsym->st_shndx = shndx = SHN_UNDEF;
275808278a5eSRod Evans 			rsym->st_value = 0;
275908278a5eSRod Evans 			rsym->st_size = 0;
276008278a5eSRod Evans 
276108278a5eSRod Evans 			sdflags |= FLG_SY_CAP;
276208278a5eSRod Evans 
276308278a5eSRod Evans 			nsym = rsym;
276408278a5eSRod Evans 		}
276508278a5eSRod Evans 
27667c478bd9Sstevel@tonic-gate 		/*
27677c478bd9Sstevel@tonic-gate 		 * If the symbol does not already exist in the internal symbol
27687c478bd9Sstevel@tonic-gate 		 * table add it, otherwise resolve the conflict.  If the symbol
27697c478bd9Sstevel@tonic-gate 		 * from this file is kept, retain its symbol table index for
27707c478bd9Sstevel@tonic-gate 		 * possible use in associating a global alias.
27717c478bd9Sstevel@tonic-gate 		 */
27725aefb655Srie 		if ((sdp = ld_sym_find(name, hash, &where, ofl)) == NULL) {
27735aefb655Srie 			DBG_CALL(Dbg_syms_global(ofl->ofl_lml, ndx, name));
2774635216b6SRod Evans 			if ((sdp = ld_sym_enter(name, nsym, hash, ifl, ofl, ndx,
2775635216b6SRod Evans 			    shndx, sdflags, &where)) == (Sym_desc *)S_ERROR)
27767c478bd9Sstevel@tonic-gate 				return (S_ERROR);
2777635216b6SRod Evans 		} else if (ld_sym_resolve(sdp, nsym, ifl, ofl, ndx, shndx,
2778*a8facf26SRichard Lowe 		    sdflags) == S_ERROR) {
27797c478bd9Sstevel@tonic-gate 			return (S_ERROR);
2780*a8facf26SRichard Lowe 		}
27817c478bd9Sstevel@tonic-gate 
278208278a5eSRod Evans 		/*
278308278a5eSRod Evans 		 * Now that we have a symbol descriptor, retain the descriptor
278408278a5eSRod Evans 		 * for later use by symbol capabilities processing.
278508278a5eSRod Evans 		 */
278608278a5eSRod Evans 		if (cpp)
278708278a5eSRod Evans 			cpp->c_nsdp = sdp;
278808278a5eSRod Evans 
27897c478bd9Sstevel@tonic-gate 		/*
27907c478bd9Sstevel@tonic-gate 		 * After we've compared a defined symbol in one shared
27917c478bd9Sstevel@tonic-gate 		 * object, flag the symbol so we don't compare it again.
27927c478bd9Sstevel@tonic-gate 		 */
2793635216b6SRod Evans 		if ((etype == ET_DYN) && (nsym->st_shndx != SHN_UNDEF) &&
27947c478bd9Sstevel@tonic-gate 		    ((sdp->sd_flags & FLG_SY_SOFOUND) == 0))
27957c478bd9Sstevel@tonic-gate 			sdp->sd_flags |= FLG_SY_SOFOUND;
27967c478bd9Sstevel@tonic-gate 
27977c478bd9Sstevel@tonic-gate 		/*
27987c478bd9Sstevel@tonic-gate 		 * If the symbol is accepted from this file retain the symbol
27997c478bd9Sstevel@tonic-gate 		 * index for possible use in aliasing.
28007c478bd9Sstevel@tonic-gate 		 */
28017c478bd9Sstevel@tonic-gate 		if (sdp->sd_file == ifl)
28027c478bd9Sstevel@tonic-gate 			sdp->sd_symndx = ndx;
28037c478bd9Sstevel@tonic-gate 
28047c478bd9Sstevel@tonic-gate 		ifl->ifl_oldndx[ndx] = sdp;
28057c478bd9Sstevel@tonic-gate 
28067c478bd9Sstevel@tonic-gate 		/*
28077c478bd9Sstevel@tonic-gate 		 * If we've accepted a register symbol, continue to validate
28087c478bd9Sstevel@tonic-gate 		 * it.
28097c478bd9Sstevel@tonic-gate 		 */
28107c478bd9Sstevel@tonic-gate 		if (sdp->sd_flags & FLG_SY_REGSYM) {
28117c478bd9Sstevel@tonic-gate 			Sym_desc	*rsdp;
28127c478bd9Sstevel@tonic-gate 
2813ba2be530Sab 			/*
2814ba2be530Sab 			 * The presence of FLG_SY_REGSYM means that
2815ba2be530Sab 			 * the pointers in ld_targ.t_ms are non-NULL.
2816ba2be530Sab 			 */
2817ba2be530Sab 			rsdp = (*ld_targ.t_ms.ms_reg_find)(sdp->sd_sym, ofl);
28186b3ba5bdSAli Bahrami 			if (rsdp == NULL) {
2819ba2be530Sab 				if ((*ld_targ.t_ms.ms_reg_enter)(sdp, ofl) == 0)
28207c478bd9Sstevel@tonic-gate 					return (S_ERROR);
28217c478bd9Sstevel@tonic-gate 			} else if (rsdp != sdp) {
2822ba2be530Sab 				(void) (*ld_targ.t_ms.ms_reg_check)(rsdp,
2823ba2be530Sab 				    sdp->sd_sym, sdp->sd_name, ifl, ofl);
28247c478bd9Sstevel@tonic-gate 			}
28257c478bd9Sstevel@tonic-gate 		}
282675e45495Sab 
282775e45495Sab 		/*
282875e45495Sab 		 * For a relocatable object, if this symbol is defined
282975e45495Sab 		 * and has non-zero length and references an address
283075e45495Sab 		 * within an associated section, then check its extents
283175e45495Sab 		 * to make sure the section boundaries encompass it.
283275e45495Sab 		 * If they don't, the ELF file is corrupt. Note that this
283375e45495Sab 		 * global symbol may have come from another file to satisfy
283475e45495Sab 		 * an UNDEF symbol of the same name from this one. In that
283575e45495Sab 		 * case, we don't check it, because it was already checked
283675e45495Sab 		 * as part of its own file.
283775e45495Sab 		 */
2838*a8facf26SRichard Lowe 		if ((etype == ET_REL) && (sdp->sd_file == ifl)) {
283975e45495Sab 			Sym *tsym = sdp->sd_sym;
284075e45495Sab 
284175e45495Sab 			if (SYM_LOC_BADADDR(sdp, tsym,
284275e45495Sab 			    ELF_ST_TYPE(tsym->st_info))) {
284369a0bf0cSab 				issue_badaddr_msg(ifl, ofl, sdp,
284469a0bf0cSab 				    tsym, tsym->st_shndx);
284575e45495Sab 				continue;
284675e45495Sab 			}
284775e45495Sab 		}
28487c478bd9Sstevel@tonic-gate 	}
2849dc0f59e5SAli Bahrami 	DBG_CALL(Dbg_util_nl(ofl->ofl_lml, DBG_NL_STD));
28507c478bd9Sstevel@tonic-gate 
28517c478bd9Sstevel@tonic-gate 	/*
28526b3ba5bdSAli Bahrami 	 * Associate weak (alias) symbols to their non-weak counterparts by
285308278a5eSRod Evans 	 * scanning the global symbols one more time.
28546b3ba5bdSAli Bahrami 	 *
28556b3ba5bdSAli Bahrami 	 * This association is needed when processing the symbols from a shared
28566b3ba5bdSAli Bahrami 	 * object dependency when a a weak definition satisfies a reference:
28577c478bd9Sstevel@tonic-gate 	 *
28586b3ba5bdSAli Bahrami 	 *  -	When building a dynamic executable, if a referenced symbol is a
28596b3ba5bdSAli Bahrami 	 *	data item, the symbol data is copied to the executables address
28606b3ba5bdSAli Bahrami 	 *	space.  In this copy-relocation case, we must also reassociate
28616b3ba5bdSAli Bahrami 	 *	the alias symbol with its new location in the executable.
28627c478bd9Sstevel@tonic-gate 	 *
28636b3ba5bdSAli Bahrami 	 *  -	If the referenced symbol is a function then we may need to
28646b3ba5bdSAli Bahrami 	 *	promote the symbols binding from undefined weak to undefined,
28656b3ba5bdSAli Bahrami 	 *	otherwise the run-time linker will not generate the correct
28666b3ba5bdSAli Bahrami 	 *	relocation error should the symbol not be found.
28676b3ba5bdSAli Bahrami 	 *
28686b3ba5bdSAli Bahrami 	 * Weak alias association is also required when a local dynsym table
28696b3ba5bdSAli Bahrami 	 * is being created.  This table should only contain one instance of a
28706b3ba5bdSAli Bahrami 	 * symbol that is associated to a given address.
28717c478bd9Sstevel@tonic-gate 	 *
28727c478bd9Sstevel@tonic-gate 	 * The true association between a weak/strong symbol pair is that both
28736b3ba5bdSAli Bahrami 	 * symbol entries are identical, thus first we create a sorted symbol
28746b3ba5bdSAli Bahrami 	 * list keyed off of the symbols section index and value.  If the symbol
28756b3ba5bdSAli Bahrami 	 * belongs to the same section and has the same value, then the chances
28766b3ba5bdSAli Bahrami 	 * are that the rest of the symbols data is the same.  This list is then
28776b3ba5bdSAli Bahrami 	 * scanned for weak symbols, and if one is found then any strong
28786b3ba5bdSAli Bahrami 	 * association will exist in the entries that follow.  Thus we just have
28796b3ba5bdSAli Bahrami 	 * to scan one (typically a single alias) or more (in the uncommon
28806b3ba5bdSAli Bahrami 	 * instance of multiple weak to strong associations) entries to
28816b3ba5bdSAli Bahrami 	 * determine if a match exists.
28827c478bd9Sstevel@tonic-gate 	 */
28836b3ba5bdSAli Bahrami 	if (weak && (OFL_ALLOW_LDYNSYM(ofl) || (etype == ET_DYN)) &&
2884d579eb63Sab 	    (total > local)) {
28856b3ba5bdSAli Bahrami 		static Sym_desc	**sort;
28866b3ba5bdSAli Bahrami 		static size_t	osize = 0;
28876b3ba5bdSAli Bahrami 		size_t		nsize = (total - local) * sizeof (Sym_desc *);
28887c478bd9Sstevel@tonic-gate 
28896b3ba5bdSAli Bahrami 		/*
28906b3ba5bdSAli Bahrami 		 * As we might be processing many input files, and many symbols,
28916b3ba5bdSAli Bahrami 		 * try and reuse a static sort buffer.  Note, presently we're
28926b3ba5bdSAli Bahrami 		 * playing the game of never freeing any buffers as there's a
28936b3ba5bdSAli Bahrami 		 * belief this wastes time.
28946b3ba5bdSAli Bahrami 		 */
28956b3ba5bdSAli Bahrami 		if ((osize == 0) || (nsize > osize)) {
28966b3ba5bdSAli Bahrami 			if ((sort = libld_malloc(nsize)) == NULL)
28976b3ba5bdSAli Bahrami 				return (S_ERROR);
28986b3ba5bdSAli Bahrami 			osize = nsize;
28996b3ba5bdSAli Bahrami 		}
29006b3ba5bdSAli Bahrami 		(void) memcpy((void *)sort, &ifl->ifl_oldndx[local], nsize);
29017c478bd9Sstevel@tonic-gate 
29027c478bd9Sstevel@tonic-gate 		qsort(sort, (total - local), sizeof (Sym_desc *), compare);
29037c478bd9Sstevel@tonic-gate 
29047c478bd9Sstevel@tonic-gate 		for (ndx = 0; ndx < (total - local); ndx++) {
29056b3ba5bdSAli Bahrami 			Sym_desc	*wsdp = sort[ndx];
29066b3ba5bdSAli Bahrami 			Sym		*wsym;
29077c478bd9Sstevel@tonic-gate 			int		sndx;
29087c478bd9Sstevel@tonic-gate 
29096b3ba5bdSAli Bahrami 			/*
29106b3ba5bdSAli Bahrami 			 * Ignore any empty symbol descriptor, or the case where
29116b3ba5bdSAli Bahrami 			 * the symbol has been resolved to a different file.
29126b3ba5bdSAli Bahrami 			 */
29136b3ba5bdSAli Bahrami 			if ((wsdp == NULL) || (wsdp->sd_file != ifl))
29147c478bd9Sstevel@tonic-gate 				continue;
29157c478bd9Sstevel@tonic-gate 
29167c478bd9Sstevel@tonic-gate 			wsym = wsdp->sd_sym;
29177c478bd9Sstevel@tonic-gate 
29186b3ba5bdSAli Bahrami 			if ((wsym->st_shndx == SHN_UNDEF) ||
29196b3ba5bdSAli Bahrami 			    (wsdp->sd_flags & FLG_SY_SPECSEC) ||
29206b3ba5bdSAli Bahrami 			    (ELF_ST_BIND(wsym->st_info) != STB_WEAK))
29217c478bd9Sstevel@tonic-gate 				continue;
29227c478bd9Sstevel@tonic-gate 
29237c478bd9Sstevel@tonic-gate 			/*
29247c478bd9Sstevel@tonic-gate 			 * We have a weak symbol, if it has a strong alias it
29257c478bd9Sstevel@tonic-gate 			 * will have been sorted to one of the following sort
29267c478bd9Sstevel@tonic-gate 			 * table entries.  Note that we could have multiple weak
29277c478bd9Sstevel@tonic-gate 			 * symbols aliased to one strong (if this occurs then
29287c478bd9Sstevel@tonic-gate 			 * the strong symbol only maintains one alias back to
29297c478bd9Sstevel@tonic-gate 			 * the last weak).
29307c478bd9Sstevel@tonic-gate 			 */
29317c478bd9Sstevel@tonic-gate 			for (sndx = ndx + 1; sndx < (total - local); sndx++) {
29326b3ba5bdSAli Bahrami 				Sym_desc	*ssdp = sort[sndx];
29336b3ba5bdSAli Bahrami 				Sym		*ssym;
2934635216b6SRod Evans 				sd_flag_t	w_dynbits, s_dynbits;
29357c478bd9Sstevel@tonic-gate 
29366b3ba5bdSAli Bahrami 				/*
29376b3ba5bdSAli Bahrami 				 * Ignore any empty symbol descriptor, or the
29386b3ba5bdSAli Bahrami 				 * case where the symbol has been resolved to a
29396b3ba5bdSAli Bahrami 				 * different file.
29406b3ba5bdSAli Bahrami 				 */
29416b3ba5bdSAli Bahrami 				if ((ssdp == NULL) || (ssdp->sd_file != ifl))
29426b3ba5bdSAli Bahrami 					continue;
29437c478bd9Sstevel@tonic-gate 
29447c478bd9Sstevel@tonic-gate 				ssym = ssdp->sd_sym;
29457c478bd9Sstevel@tonic-gate 
29466b3ba5bdSAli Bahrami 				if (ssym->st_shndx == SHN_UNDEF)
29476b3ba5bdSAli Bahrami 					continue;
29486b3ba5bdSAli Bahrami 
29496b3ba5bdSAli Bahrami 				if ((ssym->st_shndx != wsym->st_shndx) ||
29506b3ba5bdSAli Bahrami 				    (ssym->st_value != wsym->st_value))
29517c478bd9Sstevel@tonic-gate 					break;
29527c478bd9Sstevel@tonic-gate 
29536b3ba5bdSAli Bahrami 				if ((ssym->st_size != wsym->st_size) ||
29546b3ba5bdSAli Bahrami 				    (ssdp->sd_flags & FLG_SY_SPECSEC) ||
29556b3ba5bdSAli Bahrami 				    (ELF_ST_BIND(ssym->st_info) == STB_WEAK))
29566b3ba5bdSAli Bahrami 					continue;
2957d579eb63Sab 
29586b3ba5bdSAli Bahrami 				/*
29596b3ba5bdSAli Bahrami 				 * If a sharable object, set link fields so
29606b3ba5bdSAli Bahrami 				 * that they reference each other.`
29616b3ba5bdSAli Bahrami 				 */
29626b3ba5bdSAli Bahrami 				if (etype == ET_DYN) {
29636b3ba5bdSAli Bahrami 					ssdp->sd_aux->sa_linkndx =
29646b3ba5bdSAli Bahrami 					    (Word)wsdp->sd_symndx;
29656b3ba5bdSAli Bahrami 					wsdp->sd_aux->sa_linkndx =
29666b3ba5bdSAli Bahrami 					    (Word)ssdp->sd_symndx;
29676b3ba5bdSAli Bahrami 				}
29686b3ba5bdSAli Bahrami 
29696b3ba5bdSAli Bahrami 				/*
29706b3ba5bdSAli Bahrami 				 * Determine which of these two symbols go into
29716b3ba5bdSAli Bahrami 				 * the sort section.  If a mapfile has made
29726b3ba5bdSAli Bahrami 				 * explicit settings of the FLG_SY_*DYNSORT
29736b3ba5bdSAli Bahrami 				 * flags for both symbols, then we do what they
29746b3ba5bdSAli Bahrami 				 * say.  If one has the DYNSORT flags set, we
29756b3ba5bdSAli Bahrami 				 * set the NODYNSORT bit in the other.  And if
29766b3ba5bdSAli Bahrami 				 * neither has an explicit setting, then we
29776b3ba5bdSAli Bahrami 				 * favor the weak symbol because they usually
29786b3ba5bdSAli Bahrami 				 * lack the leading underscore.
29796b3ba5bdSAli Bahrami 				 */
29806b3ba5bdSAli Bahrami 				w_dynbits = wsdp->sd_flags &
29816b3ba5bdSAli Bahrami 				    (FLG_SY_DYNSORT | FLG_SY_NODYNSORT);
29826b3ba5bdSAli Bahrami 				s_dynbits = ssdp->sd_flags &
29836b3ba5bdSAli Bahrami 				    (FLG_SY_DYNSORT | FLG_SY_NODYNSORT);
29846b3ba5bdSAli Bahrami 				if (!(w_dynbits && s_dynbits)) {
29856b3ba5bdSAli Bahrami 					if (s_dynbits) {
29866b3ba5bdSAli Bahrami 						if (s_dynbits == FLG_SY_DYNSORT)
298733eb6ee1Sab 							wsdp->sd_flags |=
298833eb6ee1Sab 							    FLG_SY_NODYNSORT;
29896b3ba5bdSAli Bahrami 					} else if (w_dynbits !=
29906b3ba5bdSAli Bahrami 					    FLG_SY_NODYNSORT) {
29916b3ba5bdSAli Bahrami 						ssdp->sd_flags |=
29926b3ba5bdSAli Bahrami 						    FLG_SY_NODYNSORT;
2993d579eb63Sab 					}
29947c478bd9Sstevel@tonic-gate 				}
29956b3ba5bdSAli Bahrami 				break;
29967c478bd9Sstevel@tonic-gate 			}
29977c478bd9Sstevel@tonic-gate 		}
29987c478bd9Sstevel@tonic-gate 	}
299908278a5eSRod Evans 
300008278a5eSRod Evans 	/*
300108278a5eSRod Evans 	 * Having processed all symbols, under -z symbolcap, reprocess any
300208278a5eSRod Evans 	 * symbols that are being translated from global to locals.  The symbol
300308278a5eSRod Evans 	 * pair that has been collected defines the original symbol (c_osdp),
300408278a5eSRod Evans 	 * which will become a local, and the new symbol (c_nsdp), which will
300508278a5eSRod Evans 	 * become a reference (UNDEF) for the original.
300608278a5eSRod Evans 	 *
300708278a5eSRod Evans 	 * Scan these symbol pairs looking for weak symbols, which have non-weak
300808278a5eSRod Evans 	 * aliases.  There is no need to translate both of these symbols to
300908278a5eSRod Evans 	 * locals, only the global is necessary.
301008278a5eSRod Evans 	 */
301108278a5eSRod Evans 	if (cappairs) {
301208278a5eSRod Evans 		Aliste		idx1;
301308278a5eSRod Evans 		Cap_pair	*cpp1;
301408278a5eSRod Evans 
301508278a5eSRod Evans 		for (ALIST_TRAVERSE(cappairs, idx1, cpp1)) {
301608278a5eSRod Evans 			Sym_desc	*sdp1 = cpp1->c_osdp;
301708278a5eSRod Evans 			Sym		*sym1 = sdp1->sd_sym;
301808278a5eSRod Evans 			uchar_t		bind1 = ELF_ST_BIND(sym1->st_info);
301908278a5eSRod Evans 			Aliste		idx2;
302008278a5eSRod Evans 			Cap_pair	*cpp2;
302108278a5eSRod Evans 
302208278a5eSRod Evans 			/*
302308278a5eSRod Evans 			 * If this symbol isn't weak, it's capability member is
302408278a5eSRod Evans 			 * retained for the creation of a local symbol.
302508278a5eSRod Evans 			 */
302608278a5eSRod Evans 			if (bind1 != STB_WEAK)
302708278a5eSRod Evans 				continue;
302808278a5eSRod Evans 
302908278a5eSRod Evans 			/*
303008278a5eSRod Evans 			 * If this is a weak symbol, traverse the capabilities
303108278a5eSRod Evans 			 * list again to determine if a corresponding non-weak
303208278a5eSRod Evans 			 * symbol exists.
303308278a5eSRod Evans 			 */
303408278a5eSRod Evans 			for (ALIST_TRAVERSE(cappairs, idx2, cpp2)) {
303508278a5eSRod Evans 				Sym_desc	*sdp2 = cpp2->c_osdp;
303608278a5eSRod Evans 				Sym		*sym2 = sdp2->sd_sym;
303708278a5eSRod Evans 				uchar_t		bind2 =
303808278a5eSRod Evans 				    ELF_ST_BIND(sym2->st_info);
303908278a5eSRod Evans 
304008278a5eSRod Evans 				if ((cpp1 == cpp2) ||
304108278a5eSRod Evans 				    (cpp1->c_group != cpp2->c_group) ||
304208278a5eSRod Evans 				    (sym1->st_value != sym2->st_value) ||
304308278a5eSRod Evans 				    (bind2 == STB_WEAK))
304408278a5eSRod Evans 					continue;
304508278a5eSRod Evans 
304608278a5eSRod Evans 				/*
304708278a5eSRod Evans 				 * The weak symbol (sym1) has a non-weak (sym2)
304808278a5eSRod Evans 				 * counterpart.  There's no point in translating
304908278a5eSRod Evans 				 * both of these equivalent symbols to locals.
305008278a5eSRod Evans 				 * Add this symbol capability alias to the
305108278a5eSRod Evans 				 * capabilities family information, and remove
305208278a5eSRod Evans 				 * the weak symbol.
305308278a5eSRod Evans 				 */
305408278a5eSRod Evans 				if (ld_cap_add_family(ofl, cpp2->c_nsdp,
305508278a5eSRod Evans 				    cpp1->c_nsdp, NULL, NULL) == S_ERROR)
305608278a5eSRod Evans 					return (S_ERROR);
305708278a5eSRod Evans 
305808278a5eSRod Evans 				free((void *)cpp1->c_osdp);
305908278a5eSRod Evans 				(void) alist_delete(cappairs, &idx1);
306008278a5eSRod Evans 			}
306108278a5eSRod Evans 		}
306208278a5eSRod Evans 
306308278a5eSRod Evans 		DBG_CALL(Dbg_util_nl(ofl->ofl_lml, DBG_NL_STD));
306408278a5eSRod Evans 
306508278a5eSRod Evans 		/*
306608278a5eSRod Evans 		 * The capability pairs information now represents all the
306708278a5eSRod Evans 		 * global symbols that need transforming to locals.  These
306808278a5eSRod Evans 		 * local symbols are renamed using their group identifiers.
306908278a5eSRod Evans 		 */
307008278a5eSRod Evans 		for (ALIST_TRAVERSE(cappairs, idx1, cpp1)) {
307108278a5eSRod Evans 			Sym_desc	*osdp = cpp1->c_osdp;
307208278a5eSRod Evans 			Objcapset	*capset;
307308278a5eSRod Evans 			size_t		nsize, tsize;
307408278a5eSRod Evans 			const char	*oname;
307508278a5eSRod Evans 			char		*cname, *idstr;
307608278a5eSRod Evans 			Sym		*csym;
307708278a5eSRod Evans 
307808278a5eSRod Evans 			/*
307908278a5eSRod Evans 			 * If the local symbol has not yet been translated
308008278a5eSRod Evans 			 * convert it to a local symbol with a name.
308108278a5eSRod Evans 			 */
308208278a5eSRod Evans 			if ((osdp->sd_flags & FLG_SY_CAP) != 0)
308308278a5eSRod Evans 				continue;
308408278a5eSRod Evans 
308508278a5eSRod Evans 			/*
308608278a5eSRod Evans 			 * As we're converting object capabilities to symbol
308708278a5eSRod Evans 			 * capabilities, obtain the capabilities set for this
308808278a5eSRod Evans 			 * object, so as to retrieve the CA_SUNW_ID value.
308908278a5eSRod Evans 			 */
309008278a5eSRod Evans 			capset = &cpp1->c_group->cg_set;
309108278a5eSRod Evans 
309208278a5eSRod Evans 			/*
309308278a5eSRod Evans 			 * Create a new name from the existing symbol and the
309408278a5eSRod Evans 			 * capabilities group identifier.  Note, the delimiter
309508278a5eSRod Evans 			 * between the symbol name and identifier name is hard-
309608278a5eSRod Evans 			 * coded here (%), so that we establish a convention
309708278a5eSRod Evans 			 * for transformed symbol names.
309808278a5eSRod Evans 			 */
309908278a5eSRod Evans 			oname = osdp->sd_name;
310008278a5eSRod Evans 
310108278a5eSRod Evans 			idstr = capset->oc_id.cs_str;
310208278a5eSRod Evans 			nsize = strlen(oname);
310308278a5eSRod Evans 			tsize = nsize + 1 + strlen(idstr) + 1;
310408278a5eSRod Evans 			if ((cname = libld_malloc(tsize)) == 0)
310508278a5eSRod Evans 				return (S_ERROR);
310608278a5eSRod Evans 
310708278a5eSRod Evans 			(void) strcpy(cname, oname);
310808278a5eSRod Evans 			cname[nsize++] = '%';
310908278a5eSRod Evans 			(void) strcpy(&cname[nsize], idstr);
311008278a5eSRod Evans 
311108278a5eSRod Evans 			/*
311208278a5eSRod Evans 			 * Allocate a new symbol table entry, transform this
311308278a5eSRod Evans 			 * symbol to a local, and assign the new name.
311408278a5eSRod Evans 			 */
311508278a5eSRod Evans 			if ((csym = libld_malloc(sizeof (Sym))) == NULL)
311608278a5eSRod Evans 				return (S_ERROR);
311708278a5eSRod Evans 
311808278a5eSRod Evans 			*csym = *osdp->sd_sym;
311908278a5eSRod Evans 			csym->st_info = ELF_ST_INFO(STB_LOCAL,
312008278a5eSRod Evans 			    ELF_ST_TYPE(osdp->sd_sym->st_info));
312108278a5eSRod Evans 
312208278a5eSRod Evans 			osdp->sd_name = cname;
312308278a5eSRod Evans 			osdp->sd_sym = csym;
312408278a5eSRod Evans 			osdp->sd_flags = FLG_SY_CAP;
312508278a5eSRod Evans 
312608278a5eSRod Evans 			/*
312708278a5eSRod Evans 			 * Keep track of this new local symbol.  As -z symbolcap
312808278a5eSRod Evans 			 * can only be used to create a relocatable object, a
312908278a5eSRod Evans 			 * dynamic symbol table can't exist.  Ensure there is
313008278a5eSRod Evans 			 * space reserved in the string table.
313108278a5eSRod Evans 			 */
313208278a5eSRod Evans 			ofl->ofl_caploclcnt++;
313308278a5eSRod Evans 			if (st_insert(ofl->ofl_strtab, cname) == -1)
313408278a5eSRod Evans 				return (S_ERROR);
313508278a5eSRod Evans 
313608278a5eSRod Evans 			DBG_CALL(Dbg_syms_cap_local(ofl, cpp1->c_ndx,
313708278a5eSRod Evans 			    cname, csym, osdp));
313808278a5eSRod Evans 
313908278a5eSRod Evans 			/*
314008278a5eSRod Evans 			 * Establish this capability pair as a family.
314108278a5eSRod Evans 			 */
314208278a5eSRod Evans 			if (ld_cap_add_family(ofl, cpp1->c_nsdp, osdp,
314308278a5eSRod Evans 			    cpp1->c_group, &ifl->ifl_caps->ca_syms) == S_ERROR)
314408278a5eSRod Evans 				return (S_ERROR);
314508278a5eSRod Evans 		}
314608278a5eSRod Evans 	}
314708278a5eSRod Evans 
31487c478bd9Sstevel@tonic-gate 	return (1);
314975e45495Sab 
315075e45495Sab #undef SYM_LOC_BADADDR
31517c478bd9Sstevel@tonic-gate }
31527c478bd9Sstevel@tonic-gate 
31537c478bd9Sstevel@tonic-gate /*
3154f5a18a30Srie  * Add an undefined symbol to the symbol table.  The reference originates from
315508278a5eSRod Evans  * the location identified by the message id (mid).  These references can
3156f5a18a30Srie  * originate from command line options such as -e, -u, -initarray, etc.
3157f5a18a30Srie  * (identified with MSG_INTL(MSG_STR_COMMAND)), or from internally generated
3158f5a18a30Srie  * TLS relocation references (identified with MSG_INTL(MSG_STR_TLSREL)).
31597c478bd9Sstevel@tonic-gate  */
31607c478bd9Sstevel@tonic-gate Sym_desc *
3161f5a18a30Srie ld_sym_add_u(const char *name, Ofl_desc *ofl, Msg mid)
31627c478bd9Sstevel@tonic-gate {
31637c478bd9Sstevel@tonic-gate 	Sym		*sym;
31646b3ba5bdSAli Bahrami 	Ifl_desc	*ifl = NULL, *_ifl;
31657c478bd9Sstevel@tonic-gate 	Sym_desc	*sdp;
31667c478bd9Sstevel@tonic-gate 	Word		hash;
316757ef7aa9SRod Evans 	Aliste		idx;
31687c478bd9Sstevel@tonic-gate 	avl_index_t	where;
3169f5a18a30Srie 	const char	*reference = MSG_INTL(mid);
31707c478bd9Sstevel@tonic-gate 
31717c478bd9Sstevel@tonic-gate 	/*
3172b02637afSrie 	 * As an optimization, determine whether we've already generated this
3173b02637afSrie 	 * reference.  If the symbol doesn't already exist we'll create it.
3174b02637afSrie 	 * Or if the symbol does exist from a different source, we'll resolve
3175b02637afSrie 	 * the conflict.
31767c478bd9Sstevel@tonic-gate 	 */
31777c478bd9Sstevel@tonic-gate 	/* LINTED */
31787c478bd9Sstevel@tonic-gate 	hash = (Word)elf_hash(name);
3179b02637afSrie 	if ((sdp = ld_sym_find(name, hash, &where, ofl)) != NULL) {
3180b02637afSrie 		if ((sdp->sd_sym->st_shndx == SHN_UNDEF) &&
3181b02637afSrie 		    (sdp->sd_file->ifl_name == reference))
3182b02637afSrie 			return (sdp);
31837c478bd9Sstevel@tonic-gate 	}
31847c478bd9Sstevel@tonic-gate 
31857c478bd9Sstevel@tonic-gate 	/*
31867c478bd9Sstevel@tonic-gate 	 * Determine whether a pseudo input file descriptor exists to represent
31877c478bd9Sstevel@tonic-gate 	 * the command line, as any global symbol needs an input file descriptor
31887c478bd9Sstevel@tonic-gate 	 * during any symbol resolution (refer to map_ifl() which provides a
31897c478bd9Sstevel@tonic-gate 	 * similar method for adding symbols from mapfiles).
31907c478bd9Sstevel@tonic-gate 	 */
319157ef7aa9SRod Evans 	for (APLIST_TRAVERSE(ofl->ofl_objs, idx, _ifl))
3192f5a18a30Srie 		if (strcmp(_ifl->ifl_name, reference) == 0) {
31937c478bd9Sstevel@tonic-gate 			ifl = _ifl;
31947c478bd9Sstevel@tonic-gate 			break;
31957c478bd9Sstevel@tonic-gate 		}
31967c478bd9Sstevel@tonic-gate 
31977c478bd9Sstevel@tonic-gate 	/*
31987c478bd9Sstevel@tonic-gate 	 * If no descriptor exists create one.
31997c478bd9Sstevel@tonic-gate 	 */
32006b3ba5bdSAli Bahrami 	if (ifl == NULL) {
320157ef7aa9SRod Evans 		if ((ifl = libld_calloc(sizeof (Ifl_desc), 1)) == NULL)
32027c478bd9Sstevel@tonic-gate 			return ((Sym_desc *)S_ERROR);
3203f5a18a30Srie 		ifl->ifl_name = reference;
32047c478bd9Sstevel@tonic-gate 		ifl->ifl_flags = FLG_IF_NEEDED | FLG_IF_FILEREF;
320557ef7aa9SRod Evans 		if ((ifl->ifl_ehdr = libld_calloc(sizeof (Ehdr), 1)) == NULL)
32067c478bd9Sstevel@tonic-gate 			return ((Sym_desc *)S_ERROR);
32077c478bd9Sstevel@tonic-gate 		ifl->ifl_ehdr->e_type = ET_REL;
32087c478bd9Sstevel@tonic-gate 
320957ef7aa9SRod Evans 		if (aplist_append(&ofl->ofl_objs, ifl, AL_CNT_OFL_OBJS) == NULL)
32107c478bd9Sstevel@tonic-gate 			return ((Sym_desc *)S_ERROR);
32117c478bd9Sstevel@tonic-gate 	}
32127c478bd9Sstevel@tonic-gate 
32137c478bd9Sstevel@tonic-gate 	/*
32147c478bd9Sstevel@tonic-gate 	 * Allocate a symbol structure and add it to the global symbol table.
32157c478bd9Sstevel@tonic-gate 	 */
32166b3ba5bdSAli Bahrami 	if ((sym = libld_calloc(sizeof (Sym), 1)) == NULL)
32177c478bd9Sstevel@tonic-gate 		return ((Sym_desc *)S_ERROR);
32187c478bd9Sstevel@tonic-gate 	sym->st_info = ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE);
32197c478bd9Sstevel@tonic-gate 	sym->st_shndx = SHN_UNDEF;
32207c478bd9Sstevel@tonic-gate 
32215aefb655Srie 	DBG_CALL(Dbg_syms_process(ofl->ofl_lml, ifl));
3222b02637afSrie 	if (sdp == NULL) {
3223b02637afSrie 		DBG_CALL(Dbg_syms_global(ofl->ofl_lml, 0, name));
3224b02637afSrie 		if ((sdp = ld_sym_enter(name, sym, hash, ifl, ofl, 0, SHN_UNDEF,
3225635216b6SRod Evans 		    0, &where)) == (Sym_desc *)S_ERROR)
3226b02637afSrie 			return ((Sym_desc *)S_ERROR);
3227b02637afSrie 	} else if (ld_sym_resolve(sdp, sym, ifl, ofl, 0,
3228b02637afSrie 	    SHN_UNDEF, 0) == S_ERROR)
3229b02637afSrie 		return ((Sym_desc *)S_ERROR);
3230b02637afSrie 
32317c478bd9Sstevel@tonic-gate 	sdp->sd_flags &= ~FLG_SY_CLEAN;
32327c478bd9Sstevel@tonic-gate 	sdp->sd_flags |= FLG_SY_CMDREF;
32337c478bd9Sstevel@tonic-gate 
32347c478bd9Sstevel@tonic-gate 	return (sdp);
32357c478bd9Sstevel@tonic-gate }
32364a8d0ea7SAli Bahrami 
32374a8d0ea7SAli Bahrami /*
32384a8d0ea7SAli Bahrami  * STT_SECTION symbols have their st_name field set to NULL, and consequently
3239bf994817SAli Bahrami  * have no name. Generate a name suitable for diagnostic use for such a symbol
3240bf994817SAli Bahrami  * and store it in the input section descriptor. The resulting name will be
3241bf994817SAli Bahrami  * of the form:
32424a8d0ea7SAli Bahrami  *
32434a8d0ea7SAli Bahrami  *	"XXX (section)"
32444a8d0ea7SAli Bahrami  *
32454a8d0ea7SAli Bahrami  * where XXX is the name of the section.
32464a8d0ea7SAli Bahrami  *
32474a8d0ea7SAli Bahrami  * entry:
324808278a5eSRod Evans  *	isc - Input section associated with the symbol.
32494a8d0ea7SAli Bahrami  *	fmt - NULL, or format string to use.
32504a8d0ea7SAli Bahrami  *
32514a8d0ea7SAli Bahrami  * exit:
3252bf994817SAli Bahrami  *	Sets isp->is_sym_name to the allocated string. Returns the
3253bf994817SAli Bahrami  *	string pointer, or NULL on allocation failure.
32544a8d0ea7SAli Bahrami  */
325584f79254SToomas Soome const char *
32564a8d0ea7SAli Bahrami ld_stt_section_sym_name(Is_desc *isp)
32574a8d0ea7SAli Bahrami {
3258bf994817SAli Bahrami 	const char	*fmt;
3259bf994817SAli Bahrami 	char		*str;
3260bf994817SAli Bahrami 	size_t		len;
32614a8d0ea7SAli Bahrami 
3262bf994817SAli Bahrami 	if ((isp == NULL) || (isp->is_name == NULL))
3263bf994817SAli Bahrami 		return (NULL);
32644a8d0ea7SAli Bahrami 
3265bf994817SAli Bahrami 	if (isp->is_sym_name == NULL) {
32664a8d0ea7SAli Bahrami 		fmt = (isp->is_flags & FLG_IS_GNSTRMRG) ?
32674a8d0ea7SAli Bahrami 		    MSG_INTL(MSG_STR_SECTION_MSTR) : MSG_INTL(MSG_STR_SECTION);
32684a8d0ea7SAli Bahrami 
32694a8d0ea7SAli Bahrami 		len = strlen(fmt) + strlen(isp->is_name) + 1;
32704a8d0ea7SAli Bahrami 
3271bf994817SAli Bahrami 		if ((str = libld_malloc(len)) == NULL)
32724a8d0ea7SAli Bahrami 			return (NULL);
3273bf994817SAli Bahrami 		(void) snprintf(str, len, fmt, isp->is_name);
3274bf994817SAli Bahrami 		isp->is_sym_name = str;
32754a8d0ea7SAli Bahrami 	}
32764a8d0ea7SAli Bahrami 
3277bf994817SAli Bahrami 	return (isp->is_sym_name);
32784a8d0ea7SAli Bahrami }
3279c524b4feSRichard Lowe 
3280c524b4feSRichard Lowe /*
3281c524b4feSRichard Lowe  * If we're producing a relocatable object and the symbol is eligible for
3282c524b4feSRichard Lowe  * COMDAT section, it shouldn't be reduced in scope as that will break the
3283c524b4feSRichard Lowe  * COMDAT matching when the output object is later consumed.  Leave it alone,
3284c524b4feSRichard Lowe  * and any reduction (and COMDAT) processing will occur then.
3285c524b4feSRichard Lowe  *
3286c524b4feSRichard Lowe  * Otherwise, any hidden symbol is reduced when reductions are being processed.
3287c524b4feSRichard Lowe  */
3288c524b4feSRichard Lowe Boolean
3289c524b4feSRichard Lowe ld_sym_reducable(Ofl_desc *ofl, Sym_desc *sdp)
3290c524b4feSRichard Lowe {
3291c524b4feSRichard Lowe 	Is_desc *isc = sdp->sd_isc;
3292c524b4feSRichard Lowe 
3293c524b4feSRichard Lowe 	if (((ofl->ofl_flags & FLG_OF_RELOBJ) != 0) &&
3294c524b4feSRichard Lowe 	    (isc != NULL) &&
3295c524b4feSRichard Lowe 	    ((isc->is_flags & FLG_IS_COMDAT) != 0)) {
3296c524b4feSRichard Lowe 		return (FALSE);
3297c524b4feSRichard Lowe 	} else {
3298c524b4feSRichard Lowe 		return (SYM_IS_HIDDEN(sdp) &&
3299c524b4feSRichard Lowe 		    (ofl->ofl_flags & FLG_OF_PROCRED));
3300c524b4feSRichard Lowe 	}
3301c524b4feSRichard Lowe }
3302