xref: /illumos-gate/usr/src/cmd/sgs/libld/common/syms.c (revision 57ef7aa9)
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  *
276b3ba5bdSAli Bahrami  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
287c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
297c478bd9Sstevel@tonic-gate  */
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate /*
327c478bd9Sstevel@tonic-gate  * Symbol table management routines
337c478bd9Sstevel@tonic-gate  */
34ba2be530Sab 
35ba2be530Sab #define	ELF_TARGET_AMD64
36ba2be530Sab 
377c478bd9Sstevel@tonic-gate #include	<stdio.h>
387c478bd9Sstevel@tonic-gate #include	<string.h>
395aefb655Srie #include	<debug.h>
407c478bd9Sstevel@tonic-gate #include	"msg.h"
417c478bd9Sstevel@tonic-gate #include	"_libld.h"
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate /*
447c478bd9Sstevel@tonic-gate  * AVL tree comparator function:
457c478bd9Sstevel@tonic-gate  *
466b3ba5bdSAli Bahrami  * The primary key is the symbol name hash with a secondary key of the symbol
476b3ba5bdSAli Bahrami  * name itself.
487c478bd9Sstevel@tonic-gate  */
497c478bd9Sstevel@tonic-gate int
505aefb655Srie ld_sym_avl_comp(const void *elem1, const void *elem2)
517c478bd9Sstevel@tonic-gate {
527c478bd9Sstevel@tonic-gate 	Sym_avlnode	*sav1 = (Sym_avlnode *)elem1;
537c478bd9Sstevel@tonic-gate 	Sym_avlnode	*sav2 = (Sym_avlnode *)elem2;
546b3ba5bdSAli Bahrami 	int		res;
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate 	res = sav1->sav_hash - sav2->sav_hash;
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate 	if (res < 0)
597c478bd9Sstevel@tonic-gate 		return (-1);
607c478bd9Sstevel@tonic-gate 	if (res > 0)
617c478bd9Sstevel@tonic-gate 		return (1);
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate 	/*
647c478bd9Sstevel@tonic-gate 	 * Hash is equal - now compare name
657c478bd9Sstevel@tonic-gate 	 */
667c478bd9Sstevel@tonic-gate 	res = strcmp(sav1->sav_name, sav2->sav_name);
677c478bd9Sstevel@tonic-gate 	if (res == 0)
687c478bd9Sstevel@tonic-gate 		return (0);
697c478bd9Sstevel@tonic-gate 	if (res > 0)
707c478bd9Sstevel@tonic-gate 		return (1);
717c478bd9Sstevel@tonic-gate 	return (-1);
727c478bd9Sstevel@tonic-gate }
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate /*
757c478bd9Sstevel@tonic-gate  * Focal point for verifying symbol names.
767c478bd9Sstevel@tonic-gate  */
77a194faf8Srie inline static const char *
785aefb655Srie string(Ofl_desc *ofl, Ifl_desc *ifl, Sym *sym, const char *strs, size_t strsize,
795aefb655Srie     int symndx, Word shndx, const char *symsecname, const char *strsecname,
805aefb655Srie     Word *flags)
817c478bd9Sstevel@tonic-gate {
826b3ba5bdSAli Bahrami 	Word	name = sym->st_name;
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate 	if (name) {
857c478bd9Sstevel@tonic-gate 		if ((ifl->ifl_flags & FLG_IF_HSTRTAB) == 0) {
865aefb655Srie 			eprintf(ofl->ofl_lml, ERR_FATAL,
875aefb655Srie 			    MSG_INTL(MSG_FIL_NOSTRTABLE), ifl->ifl_name,
885aefb655Srie 			    symsecname, symndx, EC_XWORD(name));
896b3ba5bdSAli Bahrami 			return (NULL);
907c478bd9Sstevel@tonic-gate 		}
917c478bd9Sstevel@tonic-gate 		if (name >= (Word)strsize) {
925aefb655Srie 			eprintf(ofl->ofl_lml, ERR_FATAL,
935aefb655Srie 			    MSG_INTL(MSG_FIL_EXCSTRTABLE), ifl->ifl_name,
945aefb655Srie 			    symsecname, symndx, EC_XWORD(name),
957c478bd9Sstevel@tonic-gate 			    strsecname, EC_XWORD(strsize));
966b3ba5bdSAli Bahrami 			return (NULL);
977c478bd9Sstevel@tonic-gate 		}
987c478bd9Sstevel@tonic-gate 	}
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 	/*
1017c478bd9Sstevel@tonic-gate 	 * Determine if we're dealing with a register and if so validate it.
1027c478bd9Sstevel@tonic-gate 	 * If it's a scratch register, a fabricated name will be returned.
1037c478bd9Sstevel@tonic-gate 	 */
104ba2be530Sab 	if (ld_targ.t_ms.ms_is_regsym != NULL) {
105ba2be530Sab 		const char *regname = (*ld_targ.t_ms.ms_is_regsym)(ofl, ifl,
106ba2be530Sab 		    sym, strs, symndx, shndx, symsecname, flags);
107ba2be530Sab 
108ba2be530Sab 		if (regname == (const char *)S_ERROR) {
1096b3ba5bdSAli Bahrami 			return (NULL);
110ba2be530Sab 		}
111ba2be530Sab 		if (regname)
112ba2be530Sab 			return (regname);
1137c478bd9Sstevel@tonic-gate 	}
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate 	/*
1167c478bd9Sstevel@tonic-gate 	 * If this isn't a register, but we have a global symbol with a null
1177c478bd9Sstevel@tonic-gate 	 * name, we're not going to be able to hash this, search for it, or
1187c478bd9Sstevel@tonic-gate 	 * do anything interesting.  However, we've been accepting a symbol of
1197c478bd9Sstevel@tonic-gate 	 * this kind for ages now, so give the user a warning (rather than a
1207c478bd9Sstevel@tonic-gate 	 * fatal error), just in case this instance exists somewhere in the
1217c478bd9Sstevel@tonic-gate 	 * world and hasn't, as yet, been a problem.
1227c478bd9Sstevel@tonic-gate 	 */
1237c478bd9Sstevel@tonic-gate 	if ((name == 0) && (ELF_ST_BIND(sym->st_info) != STB_LOCAL)) {
1245aefb655Srie 		eprintf(ofl->ofl_lml, ERR_WARNING, MSG_INTL(MSG_FIL_NONAMESYM),
1257c478bd9Sstevel@tonic-gate 		    ifl->ifl_name, symsecname, symndx, EC_XWORD(name));
1267c478bd9Sstevel@tonic-gate 	}
1277c478bd9Sstevel@tonic-gate 	return (strs + name);
1287c478bd9Sstevel@tonic-gate }
1297c478bd9Sstevel@tonic-gate 
130ba2be530Sab /*
131ba2be530Sab  * For producing symbol names strings to use in error messages.
132ba2be530Sab  * If the symbol has a non-null name, then the string returned by
133ba2be530Sab  * this function is the output from demangle(), surrounded by
134ba2be530Sab  * single quotes. For null names, a descriptive string giving
135ba2be530Sab  * the symbol section and index is generated.
136ba2be530Sab  *
137ba2be530Sab  * This function uses an internal static buffer to hold the resulting
138ba2be530Sab  * string. The value returned is usable by the caller until the next
139ba2be530Sab  * call, at which point it is overwritten.
140ba2be530Sab  */
141ba2be530Sab static const char *
142ba2be530Sab demangle_symname(const char *name, const char *symtab_name, Word symndx)
143ba2be530Sab {
144ba2be530Sab #define	INIT_BUFSIZE 256
145ba2be530Sab 
1466b3ba5bdSAli Bahrami 	static char	*buf;
1476b3ba5bdSAli Bahrami 	static size_t	bufsize = 0;
148ba2be530Sab 	size_t		len;
149ba2be530Sab 	int		use_name;
150ba2be530Sab 
151ba2be530Sab 	use_name = (name != NULL) && (*name != '\0');
152ba2be530Sab 
153ba2be530Sab 	if (use_name) {
154ba2be530Sab 		name = demangle(name);
155ba2be530Sab 		len = strlen(name) + 2;   /* Include room for quotes */
156ba2be530Sab 	} else {
157ba2be530Sab 		name = MSG_ORIG(MSG_STR_EMPTY);
158ba2be530Sab 		len = strlen(symtab_name) + 2 + CONV32_INV_BUFSIZE;
159ba2be530Sab 	}
160ba2be530Sab 	len++;			/* Null termination */
161ba2be530Sab 
162ba2be530Sab 	/* If our buffer is too small, double it until it is big enough */
163ba2be530Sab 	if (len > bufsize) {
164ba2be530Sab 		size_t	new_bufsize = bufsize;
165ba2be530Sab 		char	*new_buf;
166ba2be530Sab 
167ba2be530Sab 		if (new_bufsize == 0)
168ba2be530Sab 			new_bufsize = INIT_BUFSIZE;
169ba2be530Sab 		while (len > new_bufsize)
170ba2be530Sab 			new_bufsize *= 2;
1716b3ba5bdSAli Bahrami 		if ((new_buf = libld_malloc(new_bufsize)) == NULL)
172ba2be530Sab 			return (name);
173ba2be530Sab 		buf = new_buf;
174ba2be530Sab 		bufsize = new_bufsize;
175ba2be530Sab 	}
176ba2be530Sab 
177ba2be530Sab 	if (use_name) {
178ba2be530Sab 		(void) snprintf(buf, bufsize, MSG_ORIG(MSG_FMT_SYMNAM), name);
179ba2be530Sab 	} else {
180ba2be530Sab 		(void) snprintf(buf, bufsize, MSG_ORIG(MSG_FMT_NULLSYMNAM),
181ba2be530Sab 		    symtab_name, EC_WORD(symndx));
182ba2be530Sab 	}
183ba2be530Sab 
184ba2be530Sab 	return (buf);
185ba2be530Sab 
186ba2be530Sab #undef INIT_BUFSIZE
187ba2be530Sab }
188ba2be530Sab 
1897c478bd9Sstevel@tonic-gate /*
1907c478bd9Sstevel@tonic-gate  * Shared objects can be built that define specific symbols that can not be
1917c478bd9Sstevel@tonic-gate  * directly bound to.  These objects have a syminfo section (and an associated
1927c478bd9Sstevel@tonic-gate  * DF_1_NODIRECT dynamic flags entry).  Scan this table looking for symbols
1937c478bd9Sstevel@tonic-gate  * that can't be bound to directly, and if this files symbol is presently
1947c478bd9Sstevel@tonic-gate  * referenced, mark it so that we don't directly bind to it.
1957c478bd9Sstevel@tonic-gate  */
1967c478bd9Sstevel@tonic-gate uintptr_t
1979a411307Srie ld_sym_nodirect(Is_desc *isp, Ifl_desc *ifl, Ofl_desc *ofl)
1987c478bd9Sstevel@tonic-gate {
1997c478bd9Sstevel@tonic-gate 	Shdr		*sifshdr, *symshdr;
2007c478bd9Sstevel@tonic-gate 	Syminfo		*sifdata;
2017c478bd9Sstevel@tonic-gate 	Sym		*symdata;
2027c478bd9Sstevel@tonic-gate 	char		*strdata;
2037c478bd9Sstevel@tonic-gate 	ulong_t		cnt, _cnt;
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 	/*
2067c478bd9Sstevel@tonic-gate 	 * Get the syminfo data, and determine the number of entries.
2077c478bd9Sstevel@tonic-gate 	 */
2087c478bd9Sstevel@tonic-gate 	sifshdr = isp->is_shdr;
2097c478bd9Sstevel@tonic-gate 	sifdata = (Syminfo *)isp->is_indata->d_buf;
2107c478bd9Sstevel@tonic-gate 	cnt =  sifshdr->sh_size / sifshdr->sh_entsize;
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 	/*
2137c478bd9Sstevel@tonic-gate 	 * Get the associated symbol table.
2147c478bd9Sstevel@tonic-gate 	 */
2157c478bd9Sstevel@tonic-gate 	symshdr = ifl->ifl_isdesc[sifshdr->sh_link]->is_shdr;
2167c478bd9Sstevel@tonic-gate 	symdata = ifl->ifl_isdesc[sifshdr->sh_link]->is_indata->d_buf;
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 	/*
2197c478bd9Sstevel@tonic-gate 	 * Get the string table associated with the symbol table.
2207c478bd9Sstevel@tonic-gate 	 */
2217c478bd9Sstevel@tonic-gate 	strdata = ifl->ifl_isdesc[symshdr->sh_link]->is_indata->d_buf;
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 	/*
2247c478bd9Sstevel@tonic-gate 	 * Traverse the syminfo data for symbols that can't be directly
2257c478bd9Sstevel@tonic-gate 	 * bound to.
2267c478bd9Sstevel@tonic-gate 	 */
2277c478bd9Sstevel@tonic-gate 	for (_cnt = 1, sifdata++; _cnt < cnt; _cnt++, sifdata++) {
2287c478bd9Sstevel@tonic-gate 		Sym		*sym;
2297c478bd9Sstevel@tonic-gate 		char		*str;
2307c478bd9Sstevel@tonic-gate 		Sym_desc	*sdp;
2317c478bd9Sstevel@tonic-gate 
2323c4993fbSrie 		if ((sifdata->si_flags & SYMINFO_FLG_NOEXTDIRECT) == 0)
2337c478bd9Sstevel@tonic-gate 			continue;
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 		sym = (Sym *)(symdata + _cnt);
2367c478bd9Sstevel@tonic-gate 		str = (char *)(strdata + sym->st_name);
2377c478bd9Sstevel@tonic-gate 
2385aefb655Srie 		if (sdp = ld_sym_find(str, SYM_NOHASH, 0, ofl)) {
2397c478bd9Sstevel@tonic-gate 			if (ifl != sdp->sd_file)
2407c478bd9Sstevel@tonic-gate 				continue;
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate 			sdp->sd_flags1 &= ~FLG_SY1_DIR;
2437c478bd9Sstevel@tonic-gate 			sdp->sd_flags1 |= FLG_SY1_NDIR;
2447c478bd9Sstevel@tonic-gate 		}
2457c478bd9Sstevel@tonic-gate 	}
2467c478bd9Sstevel@tonic-gate 	return (0);
2477c478bd9Sstevel@tonic-gate }
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate /*
2507c478bd9Sstevel@tonic-gate  * If, during symbol processing, it is necessary to update a local symbols
2517c478bd9Sstevel@tonic-gate  * contents before we have generated the symbol tables in the output image,
2527c478bd9Sstevel@tonic-gate  * create a new symbol structure and copy the original symbol contents.  While
2537c478bd9Sstevel@tonic-gate  * we are processing the input files, their local symbols are part of the
2547c478bd9Sstevel@tonic-gate  * read-only mapped image.  Commonly, these symbols are copied to the new output
2557c478bd9Sstevel@tonic-gate  * file image and then updated to reflect their new address and any change in
2567c478bd9Sstevel@tonic-gate  * attributes.  However, sometimes during relocation counting, it is necessary
2577c478bd9Sstevel@tonic-gate  * to adjust the symbols information.  This routine provides for the generation
2587c478bd9Sstevel@tonic-gate  * of a new symbol image so that this update can be performed.
2597c478bd9Sstevel@tonic-gate  * All global symbols are copied to an internal symbol table to improve locality
2607c478bd9Sstevel@tonic-gate  * of reference and hence performance, and thus this copying is not necessary.
2617c478bd9Sstevel@tonic-gate  */
2627c478bd9Sstevel@tonic-gate uintptr_t
2635aefb655Srie ld_sym_copy(Sym_desc *sdp)
2647c478bd9Sstevel@tonic-gate {
2657c478bd9Sstevel@tonic-gate 	Sym	*nsym;
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate 	if (sdp->sd_flags & FLG_SY_CLEAN) {
2686b3ba5bdSAli Bahrami 		if ((nsym = libld_malloc(sizeof (Sym))) == NULL)
2697c478bd9Sstevel@tonic-gate 			return (S_ERROR);
2707c478bd9Sstevel@tonic-gate 		*nsym = *(sdp->sd_sym);
2717c478bd9Sstevel@tonic-gate 		sdp->sd_sym = nsym;
2727c478bd9Sstevel@tonic-gate 		sdp->sd_flags &= ~FLG_SY_CLEAN;
2737c478bd9Sstevel@tonic-gate 	}
2747c478bd9Sstevel@tonic-gate 	return (1);
2757c478bd9Sstevel@tonic-gate }
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate /*
2787c478bd9Sstevel@tonic-gate  * Finds a given name in the link editors internal symbol table.  If no
2797c478bd9Sstevel@tonic-gate  * hash value is specified it is calculated.  A pointer to the located
2807c478bd9Sstevel@tonic-gate  * Sym_desc entry is returned, or NULL if the symbol is not found.
2817c478bd9Sstevel@tonic-gate  */
2827c478bd9Sstevel@tonic-gate Sym_desc *
2835aefb655Srie ld_sym_find(const char *name, Word hash, avl_index_t *where, Ofl_desc *ofl)
2847c478bd9Sstevel@tonic-gate {
2856b3ba5bdSAli Bahrami 	Sym_avlnode	qsav, *sav;
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate 	if (hash == SYM_NOHASH)
2887c478bd9Sstevel@tonic-gate 		/* LINTED */
2897c478bd9Sstevel@tonic-gate 		hash = (Word)elf_hash((const char *)name);
2907c478bd9Sstevel@tonic-gate 	qsav.sav_hash = hash;
2917c478bd9Sstevel@tonic-gate 	qsav.sav_name = name;
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate 	/*
2947c478bd9Sstevel@tonic-gate 	 * Perform search for symbol in AVL tree.  Note that the 'where' field
2957c478bd9Sstevel@tonic-gate 	 * is passed in from the caller.  If a 'where' is present, it can be
296a194faf8Srie 	 * used in subsequent 'ld_sym_enter()' calls if required.
2977c478bd9Sstevel@tonic-gate 	 */
2987c478bd9Sstevel@tonic-gate 	sav = avl_find(&ofl->ofl_symavl, &qsav, where);
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate 	/*
3017c478bd9Sstevel@tonic-gate 	 * If symbol was not found in the avl tree, return null to show that.
3027c478bd9Sstevel@tonic-gate 	 */
3036b3ba5bdSAli Bahrami 	if (sav == NULL)
3046b3ba5bdSAli Bahrami 		return (NULL);
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 	/*
3077c478bd9Sstevel@tonic-gate 	 * Return symbol found.
3087c478bd9Sstevel@tonic-gate 	 */
3097c478bd9Sstevel@tonic-gate 	return (sav->sav_symdesc);
3107c478bd9Sstevel@tonic-gate }
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate /*
3137c478bd9Sstevel@tonic-gate  * Enter a new symbol into the link editors internal symbol table.
3147c478bd9Sstevel@tonic-gate  * If the symbol is from an input file, information regarding the input file
3157c478bd9Sstevel@tonic-gate  * and input section is also recorded.  Otherwise (file == NULL) the symbol
3167c478bd9Sstevel@tonic-gate  * has been internally generated (ie. _etext, _edata, etc.).
3177c478bd9Sstevel@tonic-gate  */
3187c478bd9Sstevel@tonic-gate Sym_desc *
3195aefb655Srie ld_sym_enter(const char *name, Sym *osym, Word hash, Ifl_desc *ifl,
3205aefb655Srie     Ofl_desc *ofl, Word ndx, Word shndx, Word sdflags, Half sdflags1,
3215aefb655Srie     avl_index_t *where)
3227c478bd9Sstevel@tonic-gate {
3237c478bd9Sstevel@tonic-gate 	Sym_desc	*sdp;
3247c478bd9Sstevel@tonic-gate 	Sym_aux		*sap;
3257c478bd9Sstevel@tonic-gate 	Sym_avlnode	*savl;
3267c478bd9Sstevel@tonic-gate 	char		*_name;
3277c478bd9Sstevel@tonic-gate 	Sym		*nsym;
3287c478bd9Sstevel@tonic-gate 	Half		etype;
32960758829Srie 	uchar_t		vis;
3307c478bd9Sstevel@tonic-gate 	avl_index_t	_where;
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate 	/*
3337c478bd9Sstevel@tonic-gate 	 * Establish the file type.
3347c478bd9Sstevel@tonic-gate 	 */
3357c478bd9Sstevel@tonic-gate 	if (ifl)
3367c478bd9Sstevel@tonic-gate 		etype = ifl->ifl_ehdr->e_type;
3377c478bd9Sstevel@tonic-gate 	else
3387c478bd9Sstevel@tonic-gate 		etype = ET_NONE;
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate 	ofl->ofl_entercnt++;
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate 	/*
3437c478bd9Sstevel@tonic-gate 	 * Allocate a Sym Descriptor, Auxiliary Descriptor, and a Sym AVLNode -
3447c478bd9Sstevel@tonic-gate 	 * contiguously.
3457c478bd9Sstevel@tonic-gate 	 */
3467c478bd9Sstevel@tonic-gate 	if ((savl = libld_calloc(sizeof (Sym_avlnode) + sizeof (Sym_desc) +
3476b3ba5bdSAli Bahrami 	    sizeof (Sym_aux), 1)) == NULL)
3487c478bd9Sstevel@tonic-gate 		return ((Sym_desc *)S_ERROR);
3497c478bd9Sstevel@tonic-gate 	sdp = (Sym_desc *)((uintptr_t)savl + sizeof (Sym_avlnode));
3507c478bd9Sstevel@tonic-gate 	sap = (Sym_aux *)((uintptr_t)sdp + sizeof (Sym_desc));
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate 	savl->sav_symdesc = sdp;
3537c478bd9Sstevel@tonic-gate 	sdp->sd_file = ifl;
3547c478bd9Sstevel@tonic-gate 	sdp->sd_aux = sap;
3557c478bd9Sstevel@tonic-gate 	savl->sav_hash = sap->sa_hash = hash;
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate 	/*
3587c478bd9Sstevel@tonic-gate 	 * Copy the symbol table entry from the input file into the internal
3597c478bd9Sstevel@tonic-gate 	 * entry and have the symbol descriptor use it.
3607c478bd9Sstevel@tonic-gate 	 */
3617c478bd9Sstevel@tonic-gate 	sdp->sd_sym = nsym = &sap->sa_sym;
3627c478bd9Sstevel@tonic-gate 	*nsym = *osym;
3637c478bd9Sstevel@tonic-gate 	sdp->sd_shndx = shndx;
3647c478bd9Sstevel@tonic-gate 	sdp->sd_flags |= sdflags;
3657c478bd9Sstevel@tonic-gate 	sdp->sd_flags1 |= sdflags1;
3667c478bd9Sstevel@tonic-gate 
3676b3ba5bdSAli Bahrami 	if ((_name = libld_malloc(strlen(name) + 1)) == NULL)
3687c478bd9Sstevel@tonic-gate 		return ((Sym_desc *)S_ERROR);
3697c478bd9Sstevel@tonic-gate 	savl->sav_name = sdp->sd_name = (const char *)strcpy(_name, name);
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate 	/*
3727c478bd9Sstevel@tonic-gate 	 * Enter Symbol in AVL tree.
3737c478bd9Sstevel@tonic-gate 	 */
3747c478bd9Sstevel@tonic-gate 	if (where == 0) {
3757c478bd9Sstevel@tonic-gate 		/* LINTED */
3767c478bd9Sstevel@tonic-gate 		Sym_avlnode	*_savl;
3777c478bd9Sstevel@tonic-gate 		/*
3785aefb655Srie 		 * If a previous ld_sym_find() hasn't initialized 'where' do it
3797c478bd9Sstevel@tonic-gate 		 * now.
3807c478bd9Sstevel@tonic-gate 		 */
3817c478bd9Sstevel@tonic-gate 		where = &_where;
3827c478bd9Sstevel@tonic-gate 		_savl = avl_find(&ofl->ofl_symavl, savl, where);
3836b3ba5bdSAli Bahrami 		assert(_savl == NULL);
3847c478bd9Sstevel@tonic-gate 	}
3857c478bd9Sstevel@tonic-gate 	avl_insert(&ofl->ofl_symavl, savl, *where);
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate 	/*
3887c478bd9Sstevel@tonic-gate 	 * Record the section index.  This is possible because the
3897c478bd9Sstevel@tonic-gate 	 * `ifl_isdesc' table is filled before we start symbol processing.
3907c478bd9Sstevel@tonic-gate 	 */
3910bc07c75Srie 	if ((sdflags & FLG_SY_SPECSEC) || (nsym->st_shndx == SHN_UNDEF))
3927c478bd9Sstevel@tonic-gate 		sdp->sd_isc = NULL;
3937c478bd9Sstevel@tonic-gate 	else {
3947c478bd9Sstevel@tonic-gate 		sdp->sd_isc = ifl->ifl_isdesc[shndx];
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate 		/*
3977c478bd9Sstevel@tonic-gate 		 * If this symbol is from a relocatable object, make sure that
3987c478bd9Sstevel@tonic-gate 		 * it is still associated with a section.  For example, an
3997c478bd9Sstevel@tonic-gate 		 * unknown section type (SHT_NULL) would have been rejected on
4007c478bd9Sstevel@tonic-gate 		 * input with a warning.  Here, we make the use of the symbol
4017c478bd9Sstevel@tonic-gate 		 * fatal.  A symbol descriptor is still returned, so that the
4027c478bd9Sstevel@tonic-gate 		 * caller can continue processing all symbols, and hence flush
4037c478bd9Sstevel@tonic-gate 		 * out as many error conditions as possible.
4047c478bd9Sstevel@tonic-gate 		 */
4056b3ba5bdSAli Bahrami 		if ((etype == ET_REL) && (sdp->sd_isc == NULL)) {
4065aefb655Srie 			eprintf(ofl->ofl_lml, ERR_FATAL,
4075aefb655Srie 			    MSG_INTL(MSG_SYM_INVSEC), name, ifl->ifl_name,
4085aefb655Srie 			    EC_XWORD(shndx));
4097c478bd9Sstevel@tonic-gate 			ofl->ofl_flags |= FLG_OF_FATAL;
4107c478bd9Sstevel@tonic-gate 			return (sdp);
4117c478bd9Sstevel@tonic-gate 		}
4127c478bd9Sstevel@tonic-gate 	}
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate 	/*
4157c478bd9Sstevel@tonic-gate 	 * Mark any COMMON symbols as 'tentative'.
4167c478bd9Sstevel@tonic-gate 	 */
41754d82594Sseizo 	if (sdflags & FLG_SY_SPECSEC) {
4180bc07c75Srie 		if (nsym->st_shndx == SHN_COMMON)
41954d82594Sseizo 			sdp->sd_flags |= FLG_SY_TENTSYM;
420ba2be530Sab #if	defined(_ELF64)
421ba2be530Sab 		else if ((ld_targ.t_m.m_mach == EM_AMD64) &&
422ba2be530Sab 		    (nsym->st_shndx == SHN_X86_64_LCOMMON))
42354d82594Sseizo 			sdp->sd_flags |= FLG_SY_TENTSYM;
42454d82594Sseizo #endif
42554d82594Sseizo 	}
4267c478bd9Sstevel@tonic-gate 
4277c478bd9Sstevel@tonic-gate 	/*
42860758829Srie 	 * Establish the symbols visibility and reference.
4297c478bd9Sstevel@tonic-gate 	 */
43060758829Srie 	vis = ELF_ST_VISIBILITY(nsym->st_other);
43160758829Srie 
4327c478bd9Sstevel@tonic-gate 	if ((etype == ET_NONE) || (etype == ET_REL)) {
43360758829Srie 		switch (vis) {
43460758829Srie 		case STV_DEFAULT:
43560758829Srie 			sdp->sd_flags1 |= FLG_SY1_DEFAULT;
43660758829Srie 			break;
43760758829Srie 		case STV_INTERNAL:
43860758829Srie 		case STV_HIDDEN:
43960758829Srie 			sdp->sd_flags1 |= FLG_SY1_HIDDEN;
44060758829Srie 			break;
44160758829Srie 		case STV_PROTECTED:
44260758829Srie 			sdp->sd_flags1 |= FLG_SY1_PROTECT;
44360758829Srie 			break;
44460758829Srie 		case STV_EXPORTED:
44560758829Srie 			sdp->sd_flags1 |= FLG_SY1_EXPORT;
44660758829Srie 			break;
44760758829Srie 		case STV_SINGLETON:
44860758829Srie 			sdp->sd_flags1 |= (FLG_SY1_SINGLE | FLG_SY1_NDIR);
44960758829Srie 			ofl->ofl_flags1 |= FLG_OF1_NDIRECT;
45060758829Srie 			break;
45160758829Srie 		case STV_ELIMINATE:
452d4517e84Srie 			sdp->sd_flags1 |= (FLG_SY1_HIDDEN | FLG_SY1_ELIM);
45360758829Srie 			break;
45460758829Srie 		default:
45560758829Srie 			assert(vis <= STV_ELIMINATE);
45660758829Srie 		}
45760758829Srie 
4587c478bd9Sstevel@tonic-gate 		sdp->sd_ref = REF_REL_NEED;
4597c478bd9Sstevel@tonic-gate 
4607c478bd9Sstevel@tonic-gate 		/*
4619a411307Srie 		 * Under -Bnodirect, all exported interfaces that have not
4629a411307Srie 		 * explicitly been defined protected or directly bound to, are
4639a411307Srie 		 * tagged to prevent direct binding.
4647c478bd9Sstevel@tonic-gate 		 */
4650bc07c75Srie 		if ((ofl->ofl_flags1 & FLG_OF1_ALNODIR) &&
46660758829Srie 		    ((sdp->sd_flags1 & (FLG_SY1_PROTECT | FLG_SY1_DIR)) == 0) &&
4679a411307Srie 		    (nsym->st_shndx != SHN_UNDEF)) {
4687c478bd9Sstevel@tonic-gate 			sdp->sd_flags1 |= FLG_SY1_NDIR;
4699a411307Srie 		}
4707c478bd9Sstevel@tonic-gate 	} else {
4717c478bd9Sstevel@tonic-gate 		sdp->sd_ref = REF_DYN_SEEN;
4727c478bd9Sstevel@tonic-gate 
4737c478bd9Sstevel@tonic-gate 		/*
4747c478bd9Sstevel@tonic-gate 		 * Record the binding file for this symbol in the sa_bindto
4757c478bd9Sstevel@tonic-gate 		 * field.  If this symbol is ever overridden by a REF_REL_NEED
4767c478bd9Sstevel@tonic-gate 		 * definition, sa_bindto is used when building a 'translator'.
4777c478bd9Sstevel@tonic-gate 		 */
4780bc07c75Srie 		if (nsym->st_shndx != SHN_UNDEF)
4797c478bd9Sstevel@tonic-gate 			sdp->sd_aux->sa_bindto = ifl;
4807c478bd9Sstevel@tonic-gate 
4817c478bd9Sstevel@tonic-gate 		/*
48260758829Srie 		 * If this is a protected symbol, remember this.  Note, this
48360758829Srie 		 * state is different from the FLG_SY1_PROTECT used to establish
48460758829Srie 		 * a symbol definitions visibility.  This state is used to warn
48560758829Srie 		 * against possible copy relocations against this referenced
48660758829Srie 		 * symbol.
4877c478bd9Sstevel@tonic-gate 		 */
48860758829Srie 		if (vis == STV_PROTECTED)
4897c478bd9Sstevel@tonic-gate 			sdp->sd_flags |= FLG_SY_PROT;
4907c478bd9Sstevel@tonic-gate 
4917c478bd9Sstevel@tonic-gate 		/*
49260758829Srie 		 * If this is a SINGLETON definition, then indicate the symbol
49360758829Srie 		 * can not be directly bound to, and retain the visibility.
49460758829Srie 		 * This visibility will be inherited by any references made to
49560758829Srie 		 * this symbol.
4967c478bd9Sstevel@tonic-gate 		 */
49760758829Srie 		if ((vis == STV_SINGLETON) && (nsym->st_shndx != SHN_UNDEF))
49860758829Srie 			sdp->sd_flags1 |= (FLG_SY1_SINGLE | FLG_SY1_NDIR);
4997c478bd9Sstevel@tonic-gate 
5007c478bd9Sstevel@tonic-gate 		/*
50160758829Srie 		 * If the new symbol is from a shared library and is associated
50260758829Srie 		 * with a SHT_NOBITS section then this symbol originated from a
50360758829Srie 		 * tentative symbol.
5047c478bd9Sstevel@tonic-gate 		 */
5057c478bd9Sstevel@tonic-gate 		if (sdp->sd_isc &&
5067c478bd9Sstevel@tonic-gate 		    (sdp->sd_isc->is_shdr->sh_type == SHT_NOBITS))
5077c478bd9Sstevel@tonic-gate 			sdp->sd_flags |= FLG_SY_TENTSYM;
5087c478bd9Sstevel@tonic-gate 	}
5097c478bd9Sstevel@tonic-gate 
5107c478bd9Sstevel@tonic-gate 	/*
5117c478bd9Sstevel@tonic-gate 	 * Reclassify any SHN_SUNW_IGNORE symbols to SHN_UNDEF so as to
5120bc07c75Srie 	 * simplify future processing.
5137c478bd9Sstevel@tonic-gate 	 */
5140bc07c75Srie 	if (nsym->st_shndx == SHN_SUNW_IGNORE) {
5157c478bd9Sstevel@tonic-gate 		sdp->sd_shndx = shndx = SHN_UNDEF;
5167c478bd9Sstevel@tonic-gate 		sdp->sd_flags |= FLG_SY_REDUCED;
5177c478bd9Sstevel@tonic-gate 		sdp->sd_flags1 |=
51860758829Srie 		    (FLG_SY1_HIDDEN | FLG_SY1_IGNORE | FLG_SY1_ELIM);
5197c478bd9Sstevel@tonic-gate 	}
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate 	/*
5227c478bd9Sstevel@tonic-gate 	 * If this is an undefined, or common symbol from a relocatable object
5237c478bd9Sstevel@tonic-gate 	 * determine whether it is a global or weak reference (see build_osym(),
5247c478bd9Sstevel@tonic-gate 	 * where REF_DYN_NEED definitions are returned back to undefines).
5257c478bd9Sstevel@tonic-gate 	 */
52654d82594Sseizo 	if ((etype == ET_REL) &&
52754d82594Sseizo 	    (ELF_ST_BIND(nsym->st_info) == STB_GLOBAL) &&
5280bc07c75Srie 	    ((nsym->st_shndx == SHN_UNDEF) || ((sdflags & FLG_SY_SPECSEC) &&
529ba2be530Sab #if	defined(_ELF64)
5300bc07c75Srie 	    ((nsym->st_shndx == SHN_COMMON) ||
531ba2be530Sab 	    ((ld_targ.t_m.m_mach == EM_AMD64) &&
532ba2be530Sab 	    (nsym->st_shndx == SHN_X86_64_LCOMMON))))))
53354d82594Sseizo #else
53433eb6ee1Sab 	/* BEGIN CSTYLED */
5350bc07c75Srie 	    (nsym->st_shndx == SHN_COMMON))))
53633eb6ee1Sab 	/* END CSTYLED */
53754d82594Sseizo #endif
5387c478bd9Sstevel@tonic-gate 		sdp->sd_flags |= FLG_SY_GLOBREF;
5397c478bd9Sstevel@tonic-gate 
5407c478bd9Sstevel@tonic-gate 	/*
5417c478bd9Sstevel@tonic-gate 	 * Record the input filename on the referenced or defined files list
5427c478bd9Sstevel@tonic-gate 	 * for possible later diagnostics.  The `sa_rfile' pointer contains the
5437c478bd9Sstevel@tonic-gate 	 * name of the file that first referenced this symbol and is used to
5447c478bd9Sstevel@tonic-gate 	 * generate undefined symbol diagnostics (refer to sym_undef_entry()).
5457c478bd9Sstevel@tonic-gate 	 * Note that this entry can be overridden if a reference from a
5467c478bd9Sstevel@tonic-gate 	 * relocatable object is found after a reference from a shared object
5477c478bd9Sstevel@tonic-gate 	 * (refer to sym_override()).
5487c478bd9Sstevel@tonic-gate 	 * The `sa_dfiles' list is used to maintain the list of files that
5497c478bd9Sstevel@tonic-gate 	 * define the same symbol.  This list can be used for two reasons:
5507c478bd9Sstevel@tonic-gate 	 *
5516b3ba5bdSAli Bahrami 	 *   -	To save the first definition of a symbol that is not available
5527c478bd9Sstevel@tonic-gate 	 *	for this link-edit.
5537c478bd9Sstevel@tonic-gate 	 *
5546b3ba5bdSAli Bahrami 	 *   -	To save all definitions of a symbol when the -m option is in
5557c478bd9Sstevel@tonic-gate 	 *	effect.  This is optional as it is used to list multiple
5567c478bd9Sstevel@tonic-gate 	 *	(interposed) definitions of a symbol (refer to ldmap_out()),
5577c478bd9Sstevel@tonic-gate 	 *	and can be quite expensive.
5587c478bd9Sstevel@tonic-gate 	 */
5590bc07c75Srie 	if (nsym->st_shndx == SHN_UNDEF) {
5607c478bd9Sstevel@tonic-gate 		sap->sa_rfile = ifl->ifl_name;
5617c478bd9Sstevel@tonic-gate 	} else {
5627c478bd9Sstevel@tonic-gate 		if (sdp->sd_ref == REF_DYN_SEEN) {
5637c478bd9Sstevel@tonic-gate 			/*
5647c478bd9Sstevel@tonic-gate 			 * A symbol is determined to be unavailable if it
5657c478bd9Sstevel@tonic-gate 			 * belongs to a version of a shared object that this
5667c478bd9Sstevel@tonic-gate 			 * user does not wish to use, or if it belongs to an
5677c478bd9Sstevel@tonic-gate 			 * implicit shared object.
5687c478bd9Sstevel@tonic-gate 			 */
5697c478bd9Sstevel@tonic-gate 			if (ifl->ifl_vercnt) {
5709039eeafSab 				Ver_index	*vip;
5717c478bd9Sstevel@tonic-gate 				Half		vndx = ifl->ifl_versym[ndx];
5727c478bd9Sstevel@tonic-gate 
5737c478bd9Sstevel@tonic-gate 				sap->sa_dverndx = vndx;
5747c478bd9Sstevel@tonic-gate 				vip = &ifl->ifl_verndx[vndx];
5757c478bd9Sstevel@tonic-gate 				if (!(vip->vi_flags & FLG_VER_AVAIL)) {
5767c478bd9Sstevel@tonic-gate 					sdp->sd_flags |= FLG_SY_NOTAVAIL;
5777c478bd9Sstevel@tonic-gate 					sap->sa_vfile = ifl->ifl_name;
5787c478bd9Sstevel@tonic-gate 				}
5797c478bd9Sstevel@tonic-gate 			}
5807c478bd9Sstevel@tonic-gate 			if (!(ifl->ifl_flags & FLG_IF_NEEDED))
5817c478bd9Sstevel@tonic-gate 				sdp->sd_flags |= FLG_SY_NOTAVAIL;
5827c478bd9Sstevel@tonic-gate 
5837c478bd9Sstevel@tonic-gate 		} else if (etype == ET_REL) {
5847c478bd9Sstevel@tonic-gate 			/*
5857c478bd9Sstevel@tonic-gate 			 * If this symbol has been obtained from a versioned
5867c478bd9Sstevel@tonic-gate 			 * input relocatable object then the new symbol must be
5877c478bd9Sstevel@tonic-gate 			 * promoted to the versioning of the output file.
5887c478bd9Sstevel@tonic-gate 			 */
5897c478bd9Sstevel@tonic-gate 			if (ifl->ifl_versym)
5905aefb655Srie 				ld_vers_promote(sdp, ndx, ifl, ofl);
5917c478bd9Sstevel@tonic-gate 		}
5927c478bd9Sstevel@tonic-gate 
5937c478bd9Sstevel@tonic-gate 		if ((ofl->ofl_flags & FLG_OF_GENMAP) &&
5947c478bd9Sstevel@tonic-gate 		    ((sdflags & FLG_SY_SPECSEC) == 0))
595*57ef7aa9SRod Evans 			if (aplist_append(&sap->sa_dfiles, ifl->ifl_name,
596*57ef7aa9SRod Evans 			    AL_CNT_SDP_DFILES) == NULL)
5977c478bd9Sstevel@tonic-gate 				return ((Sym_desc *)S_ERROR);
5987c478bd9Sstevel@tonic-gate 	}
5997c478bd9Sstevel@tonic-gate 
60060758829Srie 	/*
60160758829Srie 	 * Provided we're not processing a mapfile, diagnose the entered symbol.
60260758829Srie 	 * Mapfile processing requires the symbol to be updated with additional
60360758829Srie 	 * information, therefore the diagnosing of the symbol is deferred until
60460758829Srie 	 * later (see Dbg_map_symbol()).
60560758829Srie 	 */
6066b3ba5bdSAli Bahrami 	if ((ifl == NULL) || ((ifl->ifl_flags & FLG_IF_MAPFILE) == 0))
60760758829Srie 		DBG_CALL(Dbg_syms_entered(ofl, nsym, sdp));
6087c478bd9Sstevel@tonic-gate 	return (sdp);
6097c478bd9Sstevel@tonic-gate }
6107c478bd9Sstevel@tonic-gate 
6117c478bd9Sstevel@tonic-gate /*
6127c478bd9Sstevel@tonic-gate  * Add a special symbol to the symbol table.  Takes special symbol name with
6137c478bd9Sstevel@tonic-gate  * and without underscores.  This routine is called, after all other symbol
6147c478bd9Sstevel@tonic-gate  * resolution has completed, to generate a reserved absolute symbol (the
6157c478bd9Sstevel@tonic-gate  * underscore version).  Special symbols are updated with the appropriate
616dd94ecefSrie  * values in update_osym().  If the user has already defined this symbol
6177c478bd9Sstevel@tonic-gate  * issue a warning and leave the symbol as is.  If the non-underscore symbol
6187c478bd9Sstevel@tonic-gate  * is referenced then turn it into a weak alias of the underscored symbol.
6197c478bd9Sstevel@tonic-gate  *
620d579eb63Sab  * The bits in flags_u are OR'd into the flags field of the symbol
621d579eb63Sab  * for the underscored symbol.
622d579eb63Sab  *
6237c478bd9Sstevel@tonic-gate  * If this is a global symbol, and it hasn't explicitly been defined as being
6247c478bd9Sstevel@tonic-gate  * directly bound to, indicate that it can't be directly bound to.
6257c478bd9Sstevel@tonic-gate  * Historically, most special symbols only have meaning to the object in which
626c1c6f601Srie  * they exist, however, they've always been global.  To ensure compatibility
627c1c6f601Srie  * with any unexpected use presently in effect, ensure these symbols don't get
6287c478bd9Sstevel@tonic-gate  * directly bound to.  Note, that establishing this state here isn't sufficient
6297c478bd9Sstevel@tonic-gate  * to create a syminfo table, only if a syminfo table is being created by some
630c1c6f601Srie  * other symbol directives will the nodirect binding be recorded.  This ensures
6317c478bd9Sstevel@tonic-gate  * we don't create syminfo sections for all objects we create, as this might add
6327c478bd9Sstevel@tonic-gate  * unnecessary bloat to users who haven't explicitly requested extra symbol
6337c478bd9Sstevel@tonic-gate  * information.
6347c478bd9Sstevel@tonic-gate  */
6357c478bd9Sstevel@tonic-gate static uintptr_t
6367c478bd9Sstevel@tonic-gate sym_add_spec(const char *name, const char *uname, Word sdaux_id,
637d579eb63Sab     Word flags_u, Half flags1, Ofl_desc *ofl)
6387c478bd9Sstevel@tonic-gate {
6397c478bd9Sstevel@tonic-gate 	Sym_desc	*sdp;
6407c478bd9Sstevel@tonic-gate 	Sym_desc 	*usdp;
6417c478bd9Sstevel@tonic-gate 	Sym		*sym;
6427c478bd9Sstevel@tonic-gate 	Word		hash;
6437c478bd9Sstevel@tonic-gate 	avl_index_t	where;
6447c478bd9Sstevel@tonic-gate 
6457c478bd9Sstevel@tonic-gate 	/* LINTED */
6467c478bd9Sstevel@tonic-gate 	hash = (Word)elf_hash(uname);
6475aefb655Srie 	if (usdp = ld_sym_find(uname, hash, &where, ofl)) {
6487c478bd9Sstevel@tonic-gate 		/*
6497c478bd9Sstevel@tonic-gate 		 * If the underscore symbol exists and is undefined, or was
6507c478bd9Sstevel@tonic-gate 		 * defined in a shared library, convert it to a local symbol.
6517c478bd9Sstevel@tonic-gate 		 * Otherwise leave it as is and warn the user.
6527c478bd9Sstevel@tonic-gate 		 */
6537c478bd9Sstevel@tonic-gate 		if ((usdp->sd_shndx == SHN_UNDEF) ||
6547c478bd9Sstevel@tonic-gate 		    (usdp->sd_ref != REF_REL_NEED)) {
6557c478bd9Sstevel@tonic-gate 			usdp->sd_ref = REF_REL_NEED;
6567c478bd9Sstevel@tonic-gate 			usdp->sd_shndx = usdp->sd_sym->st_shndx = SHN_ABS;
657d579eb63Sab 			usdp->sd_flags |= FLG_SY_SPECSEC | flags_u;
6587c478bd9Sstevel@tonic-gate 			usdp->sd_sym->st_info =
6597c478bd9Sstevel@tonic-gate 			    ELF_ST_INFO(STB_GLOBAL, STT_OBJECT);
6607c478bd9Sstevel@tonic-gate 			usdp->sd_isc = NULL;
6617c478bd9Sstevel@tonic-gate 			usdp->sd_sym->st_size = 0;
6627c478bd9Sstevel@tonic-gate 			usdp->sd_sym->st_value = 0;
6637c478bd9Sstevel@tonic-gate 			/* LINTED */
6647c478bd9Sstevel@tonic-gate 			usdp->sd_aux->sa_symspec = (Half)sdaux_id;
6657c478bd9Sstevel@tonic-gate 
6667c478bd9Sstevel@tonic-gate 			/*
6679a411307Srie 			 * If a user hasn't specifically indicated that the
6689a411307Srie 			 * scope of this symbol be made local, then leave it
6699a411307Srie 			 * as global (ie. prevent automatic scoping).  The GOT
6709a411307Srie 			 * should be defined protected, whereas all other
6719a411307Srie 			 * special symbols are tagged as no-direct.
6727c478bd9Sstevel@tonic-gate 			 */
67360758829Srie 			if (((usdp->sd_flags1 & FLG_SY1_HIDDEN) == 0) &&
67460758829Srie 			    (flags1 & FLG_SY1_DEFAULT)) {
67533eb6ee1Sab 				usdp->sd_aux->sa_overndx = VER_NDX_GLOBAL;
67633eb6ee1Sab 				if (sdaux_id == SDAUX_ID_GOT) {
67733eb6ee1Sab 					usdp->sd_flags1 &= ~FLG_SY1_NDIR;
67860758829Srie 					usdp->sd_flags1 |= FLG_SY1_PROTECT;
67933eb6ee1Sab 					usdp->sd_sym->st_other = STV_PROTECTED;
68033eb6ee1Sab 				} else if (
68133eb6ee1Sab 				    ((usdp->sd_flags1 & FLG_SY1_DIR) == 0) &&
68233eb6ee1Sab 				    ((ofl->ofl_flags & FLG_OF_SYMBOLIC) == 0)) {
68333eb6ee1Sab 					usdp->sd_flags1 |= FLG_SY1_NDIR;
68433eb6ee1Sab 				}
6857c478bd9Sstevel@tonic-gate 			}
6867c478bd9Sstevel@tonic-gate 			usdp->sd_flags1 |= flags1;
6877c478bd9Sstevel@tonic-gate 
6887c478bd9Sstevel@tonic-gate 			/*
689c1c6f601Srie 			 * If the reference originated from a mapfile ensure
6907c478bd9Sstevel@tonic-gate 			 * we mark the symbol as used.
6917c478bd9Sstevel@tonic-gate 			 */
6927c478bd9Sstevel@tonic-gate 			if (usdp->sd_flags & FLG_SY_MAPREF)
6937c478bd9Sstevel@tonic-gate 				usdp->sd_flags |= FLG_SY_MAPUSED;
6947c478bd9Sstevel@tonic-gate 
6955aefb655Srie 			DBG_CALL(Dbg_syms_updated(ofl, usdp, uname));
6967c478bd9Sstevel@tonic-gate 		} else
6975aefb655Srie 			eprintf(ofl->ofl_lml, ERR_WARNING,
6985aefb655Srie 			    MSG_INTL(MSG_SYM_RESERVE), uname,
6997c478bd9Sstevel@tonic-gate 			    usdp->sd_file->ifl_name);
7007c478bd9Sstevel@tonic-gate 	} else {
7017c478bd9Sstevel@tonic-gate 		/*
7027c478bd9Sstevel@tonic-gate 		 * If the symbol does not exist create it.
7037c478bd9Sstevel@tonic-gate 		 */
7046b3ba5bdSAli Bahrami 		if ((sym = libld_calloc(sizeof (Sym), 1)) == NULL)
7057c478bd9Sstevel@tonic-gate 			return (S_ERROR);
7067c478bd9Sstevel@tonic-gate 		sym->st_shndx = SHN_ABS;
7077c478bd9Sstevel@tonic-gate 		sym->st_info = ELF_ST_INFO(STB_GLOBAL, STT_OBJECT);
7087c478bd9Sstevel@tonic-gate 		sym->st_size = 0;
7097c478bd9Sstevel@tonic-gate 		sym->st_value = 0;
7105aefb655Srie 		DBG_CALL(Dbg_syms_created(ofl->ofl_lml, uname));
7115aefb655Srie 		if ((usdp = ld_sym_enter(uname, sym, hash, (Ifl_desc *)NULL,
712d579eb63Sab 		    ofl, 0, SHN_ABS, FLG_SY_SPECSEC | flags_u, 0, &where)) ==
7137c478bd9Sstevel@tonic-gate 		    (Sym_desc *)S_ERROR)
7147c478bd9Sstevel@tonic-gate 			return (S_ERROR);
7157c478bd9Sstevel@tonic-gate 		usdp->sd_ref = REF_REL_NEED;
7167c478bd9Sstevel@tonic-gate 		/* LINTED */
7177c478bd9Sstevel@tonic-gate 		usdp->sd_aux->sa_symspec = (Half)sdaux_id;
7187c478bd9Sstevel@tonic-gate 
7197c478bd9Sstevel@tonic-gate 		usdp->sd_aux->sa_overndx = VER_NDX_GLOBAL;
7209a411307Srie 
7219a411307Srie 		if (sdaux_id == SDAUX_ID_GOT) {
72260758829Srie 			usdp->sd_flags1 |= FLG_SY1_PROTECT;
7239a411307Srie 			usdp->sd_sym->st_other = STV_PROTECTED;
72460758829Srie 		} else if ((flags1 & FLG_SY1_DEFAULT) &&
7259a411307Srie 		    ((ofl->ofl_flags & FLG_OF_SYMBOLIC) == 0)) {
7267c478bd9Sstevel@tonic-gate 			usdp->sd_flags1 |= FLG_SY1_NDIR;
7279a411307Srie 		}
7287c478bd9Sstevel@tonic-gate 		usdp->sd_flags1 |= flags1;
7297c478bd9Sstevel@tonic-gate 	}
7307c478bd9Sstevel@tonic-gate 
7315aefb655Srie 	if (name && (sdp = ld_sym_find(name, SYM_NOHASH, 0, ofl)) &&
7320bc07c75Srie 	    (sdp->sd_sym->st_shndx == SHN_UNDEF)) {
7337c478bd9Sstevel@tonic-gate 		uchar_t	bind;
7347c478bd9Sstevel@tonic-gate 
7357c478bd9Sstevel@tonic-gate 		/*
7367c478bd9Sstevel@tonic-gate 		 * If the non-underscore symbol exists and is undefined
7377c478bd9Sstevel@tonic-gate 		 * convert it to be a local.  If the underscore has
7387c478bd9Sstevel@tonic-gate 		 * sa_symspec set (ie. it was created above) then simulate this
7397c478bd9Sstevel@tonic-gate 		 * as a weak alias.
7407c478bd9Sstevel@tonic-gate 		 */
7417c478bd9Sstevel@tonic-gate 		sdp->sd_ref = REF_REL_NEED;
7427c478bd9Sstevel@tonic-gate 		sdp->sd_shndx = sdp->sd_sym->st_shndx = SHN_ABS;
7437c478bd9Sstevel@tonic-gate 		sdp->sd_flags |= FLG_SY_SPECSEC;
7447c478bd9Sstevel@tonic-gate 		sdp->sd_isc = NULL;
7457c478bd9Sstevel@tonic-gate 		sdp->sd_sym->st_size = 0;
7467c478bd9Sstevel@tonic-gate 		sdp->sd_sym->st_value = 0;
7477c478bd9Sstevel@tonic-gate 		/* LINTED */
7487c478bd9Sstevel@tonic-gate 		sdp->sd_aux->sa_symspec = (Half)sdaux_id;
7497c478bd9Sstevel@tonic-gate 		if (usdp->sd_aux->sa_symspec) {
7507c478bd9Sstevel@tonic-gate 			usdp->sd_aux->sa_linkndx = 0;
7517c478bd9Sstevel@tonic-gate 			sdp->sd_aux->sa_linkndx = 0;
7527c478bd9Sstevel@tonic-gate 			bind = STB_WEAK;
7537c478bd9Sstevel@tonic-gate 		} else
7547c478bd9Sstevel@tonic-gate 			bind = STB_GLOBAL;
7557c478bd9Sstevel@tonic-gate 		sdp->sd_sym->st_info = ELF_ST_INFO(bind, STT_OBJECT);
7567c478bd9Sstevel@tonic-gate 
7577c478bd9Sstevel@tonic-gate 		/*
7589a411307Srie 		 * If a user hasn't specifically indicated the scope of this
7599a411307Srie 		 * symbol be made local then leave it as global (ie. prevent
7609a411307Srie 		 * automatic scoping).  The GOT should be defined protected,
7619a411307Srie 		 * whereas all other special symbols are tagged as no-direct.
7627c478bd9Sstevel@tonic-gate 		 */
76360758829Srie 		if (((sdp->sd_flags1 & FLG_SY1_HIDDEN) == 0) &&
76460758829Srie 		    (flags1 & FLG_SY1_DEFAULT)) {
7657c478bd9Sstevel@tonic-gate 			sdp->sd_aux->sa_overndx = VER_NDX_GLOBAL;
7669a411307Srie 			if (sdaux_id == SDAUX_ID_GOT) {
7679a411307Srie 				sdp->sd_flags1 &= ~FLG_SY1_NDIR;
76860758829Srie 				sdp->sd_flags1 |= FLG_SY1_PROTECT;
7699a411307Srie 				sdp->sd_sym->st_other = STV_PROTECTED;
7709a411307Srie 			} else if (((sdp->sd_flags1 & FLG_SY1_DIR) == 0) &&
7719a411307Srie 			    ((ofl->ofl_flags & FLG_OF_SYMBOLIC) == 0)) {
7727c478bd9Sstevel@tonic-gate 				sdp->sd_flags1 |= FLG_SY1_NDIR;
7739a411307Srie 			}
7747c478bd9Sstevel@tonic-gate 		}
7757c478bd9Sstevel@tonic-gate 		sdp->sd_flags1 |= flags1;
7767c478bd9Sstevel@tonic-gate 
7777c478bd9Sstevel@tonic-gate 		/*
778c1c6f601Srie 		 * If the reference originated from a mapfile ensure
7797c478bd9Sstevel@tonic-gate 		 * we mark the symbol as used.
7807c478bd9Sstevel@tonic-gate 		 */
7817c478bd9Sstevel@tonic-gate 		if (sdp->sd_flags & FLG_SY_MAPREF)
7827c478bd9Sstevel@tonic-gate 			sdp->sd_flags |= FLG_SY_MAPUSED;
7837c478bd9Sstevel@tonic-gate 
7845aefb655Srie 		DBG_CALL(Dbg_syms_updated(ofl, sdp, name));
7857c478bd9Sstevel@tonic-gate 	}
7867c478bd9Sstevel@tonic-gate 	return (1);
7877c478bd9Sstevel@tonic-gate }
7887c478bd9Sstevel@tonic-gate 
7897c478bd9Sstevel@tonic-gate 
7907c478bd9Sstevel@tonic-gate /*
7917c478bd9Sstevel@tonic-gate  * Print undefined symbols.
7927c478bd9Sstevel@tonic-gate  */
7937c478bd9Sstevel@tonic-gate static Boolean	undef_title = TRUE;
7947c478bd9Sstevel@tonic-gate 
7955aefb655Srie static void
7965aefb655Srie sym_undef_title(Ofl_desc *ofl)
7977c478bd9Sstevel@tonic-gate {
7985aefb655Srie 	eprintf(ofl->ofl_lml, ERR_NONE, MSG_INTL(MSG_SYM_FMT_UNDEF),
79933eb6ee1Sab 	    MSG_INTL(MSG_SYM_UNDEF_ITM_11),
80033eb6ee1Sab 	    MSG_INTL(MSG_SYM_UNDEF_ITM_21),
80133eb6ee1Sab 	    MSG_INTL(MSG_SYM_UNDEF_ITM_12),
80233eb6ee1Sab 	    MSG_INTL(MSG_SYM_UNDEF_ITM_22));
8037c478bd9Sstevel@tonic-gate 
8047c478bd9Sstevel@tonic-gate 	undef_title = FALSE;
8057c478bd9Sstevel@tonic-gate }
8067c478bd9Sstevel@tonic-gate 
8077c478bd9Sstevel@tonic-gate /*
8087c478bd9Sstevel@tonic-gate  * Undefined symbols can fall into one of four types:
8097c478bd9Sstevel@tonic-gate  *
8106b3ba5bdSAli Bahrami  *  -	the symbol is really undefined (SHN_UNDEF).
8117c478bd9Sstevel@tonic-gate  *
8126b3ba5bdSAli Bahrami  *  -	versioning has been enabled, however this symbol has not been assigned
8137c478bd9Sstevel@tonic-gate  *	to one of the defined versions.
8147c478bd9Sstevel@tonic-gate  *
8156b3ba5bdSAli Bahrami  *  -	the symbol has been defined by an implicitly supplied library, ie. one
8167c478bd9Sstevel@tonic-gate  *	which was encounted because it was NEEDED by another library, rather
8177c478bd9Sstevel@tonic-gate  * 	than from a command line supplied library which would become the only
8187c478bd9Sstevel@tonic-gate  *	dependency of the output file being produced.
8197c478bd9Sstevel@tonic-gate  *
8206b3ba5bdSAli Bahrami  *  -	the symbol has been defined by a version of a shared object that is
8217c478bd9Sstevel@tonic-gate  *	not permitted for this link-edit.
8227c478bd9Sstevel@tonic-gate  *
8237c478bd9Sstevel@tonic-gate  * In all cases the file who made the first reference to this symbol will have
8247c478bd9Sstevel@tonic-gate  * been recorded via the `sa_rfile' pointer.
8257c478bd9Sstevel@tonic-gate  */
8267c478bd9Sstevel@tonic-gate typedef enum {
8277c478bd9Sstevel@tonic-gate 	UNDEF,		NOVERSION,	IMPLICIT,	NOTAVAIL,
8287c478bd9Sstevel@tonic-gate 	BNDLOCAL
8297c478bd9Sstevel@tonic-gate } Type;
8307c478bd9Sstevel@tonic-gate 
8317c478bd9Sstevel@tonic-gate static const Msg format[] = {
8327c478bd9Sstevel@tonic-gate 	MSG_SYM_UND_UNDEF,		/* MSG_INTL(MSG_SYM_UND_UNDEF) */
8337c478bd9Sstevel@tonic-gate 	MSG_SYM_UND_NOVER,		/* MSG_INTL(MSG_SYM_UND_NOVER) */
8347c478bd9Sstevel@tonic-gate 	MSG_SYM_UND_IMPL,		/* MSG_INTL(MSG_SYM_UND_IMPL) */
8357c478bd9Sstevel@tonic-gate 	MSG_SYM_UND_NOTA,		/* MSG_INTL(MSG_SYM_UND_NOTA) */
8367c478bd9Sstevel@tonic-gate 	MSG_SYM_UND_BNDLOCAL		/* MSG_INTL(MSG_SYM_UND_BNDLOCAL) */
8377c478bd9Sstevel@tonic-gate };
8387c478bd9Sstevel@tonic-gate 
8395aefb655Srie static void
8405aefb655Srie sym_undef_entry(Ofl_desc *ofl, Sym_desc *sdp, Type type)
8417c478bd9Sstevel@tonic-gate {
8427c478bd9Sstevel@tonic-gate 	const char	*name1, *name2, *name3;
8437c478bd9Sstevel@tonic-gate 	Ifl_desc	*ifl = sdp->sd_file;
8447c478bd9Sstevel@tonic-gate 	Sym_aux		*sap = sdp->sd_aux;
8457c478bd9Sstevel@tonic-gate 
8467c478bd9Sstevel@tonic-gate 	if (undef_title)
8475aefb655Srie 		sym_undef_title(ofl);
8487c478bd9Sstevel@tonic-gate 
8497c478bd9Sstevel@tonic-gate 	switch (type) {
8507c478bd9Sstevel@tonic-gate 	case UNDEF:
8517c478bd9Sstevel@tonic-gate 	case BNDLOCAL:
8527c478bd9Sstevel@tonic-gate 		name1 = sap->sa_rfile;
8537c478bd9Sstevel@tonic-gate 		break;
8547c478bd9Sstevel@tonic-gate 	case NOVERSION:
8557c478bd9Sstevel@tonic-gate 		name1 = ifl->ifl_name;
8567c478bd9Sstevel@tonic-gate 		break;
8577c478bd9Sstevel@tonic-gate 	case IMPLICIT:
8587c478bd9Sstevel@tonic-gate 		name1 = sap->sa_rfile;
8597c478bd9Sstevel@tonic-gate 		name2 = ifl->ifl_name;
8607c478bd9Sstevel@tonic-gate 		break;
8617c478bd9Sstevel@tonic-gate 	case NOTAVAIL:
8627c478bd9Sstevel@tonic-gate 		name1 = sap->sa_rfile;
8637c478bd9Sstevel@tonic-gate 		name2 = sap->sa_vfile;
8647c478bd9Sstevel@tonic-gate 		name3 = ifl->ifl_verndx[sap->sa_dverndx].vi_name;
8657c478bd9Sstevel@tonic-gate 		break;
8667c478bd9Sstevel@tonic-gate 	default:
8677c478bd9Sstevel@tonic-gate 		return;
8687c478bd9Sstevel@tonic-gate 	}
8697c478bd9Sstevel@tonic-gate 
8705aefb655Srie 	eprintf(ofl->ofl_lml, ERR_NONE, MSG_INTL(format[type]),
8715aefb655Srie 	    demangle(sdp->sd_name), name1, name2, name3);
8727c478bd9Sstevel@tonic-gate }
8737c478bd9Sstevel@tonic-gate 
8747c478bd9Sstevel@tonic-gate /*
8757c478bd9Sstevel@tonic-gate  * At this point all symbol input processing has been completed, therefore
8767c478bd9Sstevel@tonic-gate  * complete the symbol table entries by generating any necessary internal
8777c478bd9Sstevel@tonic-gate  * symbols.
8787c478bd9Sstevel@tonic-gate  */
8797c478bd9Sstevel@tonic-gate uintptr_t
8805aefb655Srie ld_sym_spec(Ofl_desc *ofl)
8817c478bd9Sstevel@tonic-gate {
8829a411307Srie 	Sym_desc	*sdp;
8837c478bd9Sstevel@tonic-gate 
8849a411307Srie 	if (ofl->ofl_flags & FLG_OF_RELOBJ)
8859a411307Srie 		return (1);
8867c478bd9Sstevel@tonic-gate 
8879a411307Srie 	DBG_CALL(Dbg_syms_spec_title(ofl->ofl_lml));
8889a411307Srie 
8899a411307Srie 	if (sym_add_spec(MSG_ORIG(MSG_SYM_ETEXT), MSG_ORIG(MSG_SYM_ETEXT_U),
89060758829Srie 	    SDAUX_ID_ETEXT, 0, (FLG_SY1_DEFAULT | FLG_SY1_EXPDEF),
89160758829Srie 	    ofl) == S_ERROR)
8929a411307Srie 		return (S_ERROR);
8939a411307Srie 	if (sym_add_spec(MSG_ORIG(MSG_SYM_EDATA), MSG_ORIG(MSG_SYM_EDATA_U),
89460758829Srie 	    SDAUX_ID_EDATA, 0, (FLG_SY1_DEFAULT | FLG_SY1_EXPDEF),
89560758829Srie 	    ofl) == S_ERROR)
8969a411307Srie 		return (S_ERROR);
8979a411307Srie 	if (sym_add_spec(MSG_ORIG(MSG_SYM_END), MSG_ORIG(MSG_SYM_END_U),
89860758829Srie 	    SDAUX_ID_END, FLG_SY_DYNSORT, (FLG_SY1_DEFAULT | FLG_SY1_EXPDEF),
89960758829Srie 	    ofl) == S_ERROR)
9009a411307Srie 		return (S_ERROR);
9019a411307Srie 	if (sym_add_spec(MSG_ORIG(MSG_SYM_L_END), MSG_ORIG(MSG_SYM_L_END_U),
90260758829Srie 	    SDAUX_ID_END, 0, FLG_SY1_HIDDEN, ofl) == S_ERROR)
9039a411307Srie 		return (S_ERROR);
9049a411307Srie 	if (sym_add_spec(MSG_ORIG(MSG_SYM_L_START), MSG_ORIG(MSG_SYM_L_START_U),
90560758829Srie 	    SDAUX_ID_START, 0, FLG_SY1_HIDDEN, ofl) == S_ERROR)
9069a411307Srie 		return (S_ERROR);
9077c478bd9Sstevel@tonic-gate 
9089a411307Srie 	/*
9099a411307Srie 	 * Historically we've always produced a _DYNAMIC symbol, even for
9109a411307Srie 	 * static executables (in which case its value will be 0).
9119a411307Srie 	 */
9129a411307Srie 	if (sym_add_spec(MSG_ORIG(MSG_SYM_DYNAMIC), MSG_ORIG(MSG_SYM_DYNAMIC_U),
91360758829Srie 	    SDAUX_ID_DYN, FLG_SY_DYNSORT, (FLG_SY1_DEFAULT | FLG_SY1_EXPDEF),
91460758829Srie 	    ofl) == S_ERROR)
9159a411307Srie 		return (S_ERROR);
9169a411307Srie 
917d579eb63Sab 	if (OFL_ALLOW_DYNSYM(ofl))
9189a411307Srie 		if (sym_add_spec(MSG_ORIG(MSG_SYM_PLKTBL),
9199a411307Srie 		    MSG_ORIG(MSG_SYM_PLKTBL_U), SDAUX_ID_PLT,
92060758829Srie 		    FLG_SY_DYNSORT, (FLG_SY1_DEFAULT | FLG_SY1_EXPDEF),
92160758829Srie 		    ofl) == S_ERROR)
9227c478bd9Sstevel@tonic-gate 			return (S_ERROR);
9237c478bd9Sstevel@tonic-gate 
9249a411307Srie 	/*
9259a411307Srie 	 * A GOT reference will be accompanied by the associated GOT symbol.
9269a411307Srie 	 * Make sure it gets assigned the appropriate special attributes.
9279a411307Srie 	 */
9289a411307Srie 	if (((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_GOFTBL_U),
9299a411307Srie 	    SYM_NOHASH, 0, ofl)) != 0) && (sdp->sd_ref != REF_DYN_SEEN)) {
9309a411307Srie 		if (sym_add_spec(MSG_ORIG(MSG_SYM_GOFTBL),
931d579eb63Sab 		    MSG_ORIG(MSG_SYM_GOFTBL_U), SDAUX_ID_GOT, FLG_SY_DYNSORT,
93260758829Srie 		    (FLG_SY1_DEFAULT | FLG_SY1_EXPDEF), ofl) == S_ERROR)
9339a411307Srie 			return (S_ERROR);
9347c478bd9Sstevel@tonic-gate 	}
9359a411307Srie 
9367c478bd9Sstevel@tonic-gate 	return (1);
9377c478bd9Sstevel@tonic-gate }
9387c478bd9Sstevel@tonic-gate 
9397c478bd9Sstevel@tonic-gate /*
9407c478bd9Sstevel@tonic-gate  * This routine checks to see if a symbols visibility needs to be reduced to
9417c478bd9Sstevel@tonic-gate  * either SYMBOLIC or LOCAL.  This routine can be called from either
9427c478bd9Sstevel@tonic-gate  * reloc_init() or sym_validate().
9437c478bd9Sstevel@tonic-gate  */
9447c478bd9Sstevel@tonic-gate void
9455aefb655Srie ld_sym_adjust_vis(Sym_desc *sdp, Ofl_desc *ofl)
9467c478bd9Sstevel@tonic-gate {
94744bac77bSrie 	ofl_flag_t	oflags = ofl->ofl_flags;
94844bac77bSrie 	Sym		*sym = sdp->sd_sym;
9497c478bd9Sstevel@tonic-gate 
9500bc07c75Srie 	if ((sdp->sd_ref == REF_REL_NEED) &&
9510bc07c75Srie 	    (sdp->sd_sym->st_shndx != SHN_UNDEF)) {
9527c478bd9Sstevel@tonic-gate 		/*
95360758829Srie 		 * If auto-reduction/elimination is enabled, reduce any
95460758829Srie 		 * non-versioned global symbols.  This routine is called either
95560758829Srie 		 * from any initial relocation processing that references this
95660758829Srie 		 * symbol, or from the symbol validation processing.
95760758829Srie 		 *
95860758829Srie 		 * A symbol is a candidate for auto-reduction/elimination if:
95960758829Srie 		 *
96060758829Srie 		 *   .  the symbol wasn't explicitly defined within a mapfile
96160758829Srie 		 *	(in which case all the necessary state has been applied
96260758829Srie 		 *	to the symbol), or
96360758829Srie 		 *   .	the symbol isn't one of the family of reserved
96460758829Srie 		 *	special symbols (ie. _end, _etext, etc.), or
96560758829Srie 		 *   .	the symbol isn't a SINGLETON, or
96660758829Srie 		 *   .  the symbol wasn't explicitly defined within a version
96760758829Srie 		 *	definition associated with an input relocatable object.
96860758829Srie 		 *
9697c478bd9Sstevel@tonic-gate 		 * Indicate that the symbol has been reduced as it may be
9707c478bd9Sstevel@tonic-gate 		 * necessary to print these symbols later.
9717c478bd9Sstevel@tonic-gate 		 */
97244bac77bSrie 		if ((oflags & (FLG_OF_AUTOLCL | FLG_OF_AUTOELM)) &&
97360758829Srie 		    ((sdp->sd_flags1 & MSK_SY1_NOAUTO) == 0)) {
97460758829Srie 			if ((sdp->sd_flags1 & FLG_SY1_HIDDEN) == 0) {
97560758829Srie 				sdp->sd_flags |= FLG_SY_REDUCED;
97660758829Srie 				sdp->sd_flags1 |= FLG_SY1_HIDDEN;
97760758829Srie 			}
9787c478bd9Sstevel@tonic-gate 
97944bac77bSrie 			if (oflags & (FLG_OF_REDLSYM | FLG_OF_AUTOELM)) {
9807c478bd9Sstevel@tonic-gate 				sdp->sd_flags1 |= FLG_SY1_ELIM;
98160758829Srie 				sym->st_other = STV_ELIMINATE |
98260758829Srie 				    (sym->st_other & ~MSK_SYM_VISIBILITY);
98360758829Srie 			} else if (ELF_ST_VISIBILITY(sym->st_other) !=
98460758829Srie 			    STV_INTERNAL)
98560758829Srie 				sym->st_other = STV_HIDDEN |
98660758829Srie 				    (sym->st_other & ~MSK_SYM_VISIBILITY);
9877c478bd9Sstevel@tonic-gate 		}
9887c478bd9Sstevel@tonic-gate 
9897c478bd9Sstevel@tonic-gate 		/*
9909a411307Srie 		 * If -Bsymbolic is in effect, and the symbol hasn't explicitly
9919a411307Srie 		 * been defined nodirect (via a mapfile), then bind the global
9929a411307Srie 		 * symbol symbolically and assign the STV_PROTECTED visibility
9937c478bd9Sstevel@tonic-gate 		 * attribute.
9947c478bd9Sstevel@tonic-gate 		 */
9957c478bd9Sstevel@tonic-gate 		if ((oflags & FLG_OF_SYMBOLIC) &&
99660758829Srie 		    ((sdp->sd_flags1 & (FLG_SY1_HIDDEN | FLG_SY1_NDIR)) == 0)) {
99760758829Srie 			sdp->sd_flags1 |= FLG_SY1_PROTECT;
9987c478bd9Sstevel@tonic-gate 			if (ELF_ST_VISIBILITY(sym->st_other) == STV_DEFAULT)
9997c478bd9Sstevel@tonic-gate 				sym->st_other = STV_PROTECTED |
10007c478bd9Sstevel@tonic-gate 				    (sym->st_other & ~MSK_SYM_VISIBILITY);
10017c478bd9Sstevel@tonic-gate 		}
10027c478bd9Sstevel@tonic-gate 	}
10037c478bd9Sstevel@tonic-gate 
10047c478bd9Sstevel@tonic-gate 	/*
10057c478bd9Sstevel@tonic-gate 	 * Indicate that this symbol has had it's visibility checked so that
10067c478bd9Sstevel@tonic-gate 	 * we don't need to do this investigation again.
10077c478bd9Sstevel@tonic-gate 	 */
10087c478bd9Sstevel@tonic-gate 	sdp->sd_flags |= FLG_SY_VISIBLE;
10097c478bd9Sstevel@tonic-gate }
10107c478bd9Sstevel@tonic-gate 
1011c1c6f601Srie /*
1012c1c6f601Srie  * Make sure a symbol definition is local to the object being built.
1013c1c6f601Srie  */
1014*57ef7aa9SRod Evans inline static int
1015c1c6f601Srie ensure_sym_local(Ofl_desc *ofl, Sym_desc *sdp, const char *str)
1016c1c6f601Srie {
1017c1c6f601Srie 	if (sdp->sd_sym->st_shndx == SHN_UNDEF) {
1018c1c6f601Srie 		if (str) {
1019c1c6f601Srie 			eprintf(ofl->ofl_lml, ERR_FATAL,
1020c1c6f601Srie 			    MSG_INTL(MSG_SYM_UNDEF), str,
1021c1c6f601Srie 			    demangle((char *)sdp->sd_name));
1022c1c6f601Srie 		}
1023c1c6f601Srie 		return (1);
1024c1c6f601Srie 	}
1025c1c6f601Srie 	if (sdp->sd_ref != REF_REL_NEED) {
1026c1c6f601Srie 		if (str) {
1027c1c6f601Srie 			eprintf(ofl->ofl_lml, ERR_FATAL,
1028c1c6f601Srie 			    MSG_INTL(MSG_SYM_EXTERN), str,
1029c1c6f601Srie 			    demangle((char *)sdp->sd_name),
1030c1c6f601Srie 			    sdp->sd_file->ifl_name);
1031c1c6f601Srie 		}
1032c1c6f601Srie 		return (1);
1033c1c6f601Srie 	}
1034c1c6f601Srie 
1035c1c6f601Srie 	sdp->sd_flags |= FLG_SY_UPREQD;
1036c1c6f601Srie 	if (sdp->sd_isc) {
1037c1c6f601Srie 		sdp->sd_isc->is_flags |= FLG_IS_SECTREF;
1038c1c6f601Srie 		sdp->sd_isc->is_file->ifl_flags |= FLG_IF_FILEREF;
1039c1c6f601Srie 	}
1040c1c6f601Srie 	return (0);
1041c1c6f601Srie }
1042c1c6f601Srie 
1043c1c6f601Srie /*
1044c1c6f601Srie  * Make sure all the symbol definitions required for initarray, finiarray, or
1045c1c6f601Srie  * preinitarray's are local to the object being built.
1046c1c6f601Srie  */
1047c1c6f601Srie static int
1048*57ef7aa9SRod Evans ensure_array_local(Ofl_desc *ofl, APlist *apl, const char *str)
1049c1c6f601Srie {
1050*57ef7aa9SRod Evans 	Aliste		idx;
1051c1c6f601Srie 	Sym_desc	*sdp;
1052c1c6f601Srie 	int		ret = 0;
1053c1c6f601Srie 
1054*57ef7aa9SRod Evans 	for (APLIST_TRAVERSE(apl, idx, sdp))
1055c1c6f601Srie 		ret += ensure_sym_local(ofl, sdp, str);
1056c1c6f601Srie 
1057c1c6f601Srie 	return (ret);
1058c1c6f601Srie }
1059c1c6f601Srie 
10607c478bd9Sstevel@tonic-gate /*
10617c478bd9Sstevel@tonic-gate  * After all symbol table input processing has been finished, and all relocation
10627c478bd9Sstevel@tonic-gate  * counting has been carried out (ie. no more symbols will be read, generated,
10637c478bd9Sstevel@tonic-gate  * or modified), validate and count the relevant entries:
10647c478bd9Sstevel@tonic-gate  *
10656b3ba5bdSAli Bahrami  *	-	check and print any undefined symbols remaining.  Note that
10667c478bd9Sstevel@tonic-gate  *		if a symbol has been defined by virtue of the inclusion of
10677c478bd9Sstevel@tonic-gate  *		an implicit shared library, it is still classed as undefined.
10687c478bd9Sstevel@tonic-gate  *
10696b3ba5bdSAli Bahrami  * 	-	count the number of global needed symbols together with the
10707c478bd9Sstevel@tonic-gate  *		size of their associated name strings (if scoping has been
10717c478bd9Sstevel@tonic-gate  *		indicated these symbols may be reduced to locals).
10727c478bd9Sstevel@tonic-gate  *
10736b3ba5bdSAli Bahrami  *	-	establish the size and alignment requirements for the global
10747c478bd9Sstevel@tonic-gate  *		.bss section (the alignment of this section is based on the
10757c478bd9Sstevel@tonic-gate  *		first symbol that it will contain).
10767c478bd9Sstevel@tonic-gate  */
10777c478bd9Sstevel@tonic-gate uintptr_t
10785aefb655Srie ld_sym_validate(Ofl_desc *ofl)
10797c478bd9Sstevel@tonic-gate {
10807c478bd9Sstevel@tonic-gate 	Sym_avlnode	*sav;
10817c478bd9Sstevel@tonic-gate 	Sym_desc	*sdp;
10827c478bd9Sstevel@tonic-gate 	Sym		*sym;
10831d9df23bSab 	ofl_flag_t	oflags = ofl->ofl_flags;
10841d9df23bSab 	ofl_flag_t	undef = 0, needed = 0, verdesc = 0;
10857c478bd9Sstevel@tonic-gate 	Xword		bssalign = 0, tlsalign = 0;
10867c478bd9Sstevel@tonic-gate 	Xword		bsssize = 0, tlssize = 0;
1087ba2be530Sab #if	defined(_ELF64)
108854d82594Sseizo 	Xword		lbssalign = 0, lbsssize = 0;
108954d82594Sseizo #endif
1090c1c6f601Srie 	int		ret;
10919039eeafSab 	int		allow_ldynsym;
1092d579eb63Sab 	uchar_t		type;
10937c478bd9Sstevel@tonic-gate 
10947c478bd9Sstevel@tonic-gate 	/*
10957c478bd9Sstevel@tonic-gate 	 * If a symbol is undefined and this link-edit calls for no undefined
10967c478bd9Sstevel@tonic-gate 	 * symbols to remain (this is the default case when generating an
10977c478bd9Sstevel@tonic-gate 	 * executable but can be enforced for any object using -z defs), the
10987c478bd9Sstevel@tonic-gate 	 * symbol is classified as undefined and a fatal error condition will
10997c478bd9Sstevel@tonic-gate 	 * be indicated.
11007c478bd9Sstevel@tonic-gate 	 *
11017c478bd9Sstevel@tonic-gate 	 * If the symbol is undefined and we're creating a shared object with
11027c478bd9Sstevel@tonic-gate 	 * the -Bsymbolic flag, then the symbol is also classified as undefined
11037c478bd9Sstevel@tonic-gate 	 * and a warning condition will be indicated.
11047c478bd9Sstevel@tonic-gate 	 */
11057c478bd9Sstevel@tonic-gate 	if ((oflags & (FLG_OF_SHAROBJ | FLG_OF_SYMBOLIC)) ==
11067c478bd9Sstevel@tonic-gate 	    (FLG_OF_SHAROBJ | FLG_OF_SYMBOLIC))
11077c478bd9Sstevel@tonic-gate 		undef = FLG_OF_WARN;
11087c478bd9Sstevel@tonic-gate 	if (oflags & FLG_OF_NOUNDEF)
11097c478bd9Sstevel@tonic-gate 		undef = FLG_OF_FATAL;
11107c478bd9Sstevel@tonic-gate 
11117c478bd9Sstevel@tonic-gate 	/*
11127c478bd9Sstevel@tonic-gate 	 * If the symbol is referenced from an implicitly included shared object
11137c478bd9Sstevel@tonic-gate 	 * (ie. it's not on the NEEDED list) then the symbol is also classified
11147c478bd9Sstevel@tonic-gate 	 * as undefined and a fatal error condition will be indicated.
11157c478bd9Sstevel@tonic-gate 	 */
11167c478bd9Sstevel@tonic-gate 	if ((oflags & FLG_OF_NOUNDEF) || !(oflags & FLG_OF_SHAROBJ))
11177c478bd9Sstevel@tonic-gate 		needed = FLG_OF_FATAL;
11187c478bd9Sstevel@tonic-gate 
11197c478bd9Sstevel@tonic-gate 	/*
11207c478bd9Sstevel@tonic-gate 	 * If the output image is being versioned all symbol definitions must be
11217c478bd9Sstevel@tonic-gate 	 * associated with a version.  Any symbol that isn't is classified as
11227c478bd9Sstevel@tonic-gate 	 * undefined and a fatal error condition will be indicated.
11237c478bd9Sstevel@tonic-gate 	 */
11247c478bd9Sstevel@tonic-gate 	if ((oflags & FLG_OF_VERDEF) && (ofl->ofl_vercnt > VER_NDX_GLOBAL))
11257c478bd9Sstevel@tonic-gate 		verdesc = FLG_OF_FATAL;
11267c478bd9Sstevel@tonic-gate 
11279039eeafSab 	allow_ldynsym = OFL_ALLOW_LDYNSYM(ofl);
1128d579eb63Sab 
1129d579eb63Sab 	if (allow_ldynsym) {
1130d579eb63Sab 		/*
1131d579eb63Sab 		 * Normally, we disallow symbols with 0 size from appearing
1132d579eb63Sab 		 * in a dyn[sym|tls]sort section. However, there are some
1133d579eb63Sab 		 * symbols that serve special purposes that we want to exempt
1134d579eb63Sab 		 * from this rule. Look them up, and set their
1135d579eb63Sab 		 * FLG_SY_DYNSORT flag.
1136d579eb63Sab 		 */
1137d579eb63Sab 		static const char *special[] = {
1138d579eb63Sab 			MSG_ORIG(MSG_SYM_INIT_U),	/* _init */
1139d579eb63Sab 			MSG_ORIG(MSG_SYM_FINI_U),	/* _fini */
1140d579eb63Sab 			MSG_ORIG(MSG_SYM_START),	/* _start */
1141d579eb63Sab 			NULL
1142d579eb63Sab 		};
1143d579eb63Sab 		int i;
1144d579eb63Sab 
1145d579eb63Sab 		for (i = 0; special[i] != NULL; i++) {
1146d579eb63Sab 			if (((sdp = ld_sym_find(special[i],
1147d579eb63Sab 			    SYM_NOHASH, 0, ofl)) != NULL) &&
1148d579eb63Sab 			    (sdp->sd_sym->st_size == 0)) {
1149d579eb63Sab 				if (ld_sym_copy(sdp) == S_ERROR)
1150d579eb63Sab 					return (S_ERROR);
1151d579eb63Sab 				sdp->sd_flags |= FLG_SY_DYNSORT;
1152d579eb63Sab 			}
1153d579eb63Sab 		}
1154d579eb63Sab 	}
1155d579eb63Sab 
11567c478bd9Sstevel@tonic-gate 	/*
11577c478bd9Sstevel@tonic-gate 	 * Collect and validate the globals from the internal symbol table.
11587c478bd9Sstevel@tonic-gate 	 */
11597c478bd9Sstevel@tonic-gate 	for (sav = avl_first(&ofl->ofl_symavl); sav;
11607c478bd9Sstevel@tonic-gate 	    sav = AVL_NEXT(&ofl->ofl_symavl, sav)) {
116160758829Srie 		Is_desc		*isp;
11620bc07c75Srie 		int		undeferr = 0;
116360758829Srie 		uchar_t		vis;
11647c478bd9Sstevel@tonic-gate 
11657c478bd9Sstevel@tonic-gate 		sdp = sav->sav_symdesc;
11667c478bd9Sstevel@tonic-gate 
11677c478bd9Sstevel@tonic-gate 		/*
11687c478bd9Sstevel@tonic-gate 		 * If undefined symbols are allowed ignore any symbols that are
11697c478bd9Sstevel@tonic-gate 		 * not needed.
11707c478bd9Sstevel@tonic-gate 		 */
11717c478bd9Sstevel@tonic-gate 		if (!(oflags & FLG_OF_NOUNDEF) &&
11727c478bd9Sstevel@tonic-gate 		    (sdp->sd_ref == REF_DYN_SEEN))
11737c478bd9Sstevel@tonic-gate 			continue;
11747c478bd9Sstevel@tonic-gate 
11757c478bd9Sstevel@tonic-gate 		/*
11767c478bd9Sstevel@tonic-gate 		 * If the symbol originates from an external or parent mapfile
11777c478bd9Sstevel@tonic-gate 		 * reference and hasn't been matched to a reference from a
11787c478bd9Sstevel@tonic-gate 		 * relocatable object, ignore it.
11797c478bd9Sstevel@tonic-gate 		 */
11807c478bd9Sstevel@tonic-gate 		if ((sdp->sd_flags & (FLG_SY_EXTERN | FLG_SY_PARENT)) &&
11817c478bd9Sstevel@tonic-gate 		    ((sdp->sd_flags & FLG_SY_MAPUSED) == 0)) {
11827c478bd9Sstevel@tonic-gate 			sdp->sd_flags |= FLG_SY_INVALID;
11837c478bd9Sstevel@tonic-gate 			continue;
11847c478bd9Sstevel@tonic-gate 		}
11857c478bd9Sstevel@tonic-gate 
11867c478bd9Sstevel@tonic-gate 		sym = sdp->sd_sym;
1187d579eb63Sab 		type = ELF_ST_TYPE(sym->st_info);
11887c478bd9Sstevel@tonic-gate 
11897c478bd9Sstevel@tonic-gate 		/*
11907c478bd9Sstevel@tonic-gate 		 * Sanity check TLS.
11917c478bd9Sstevel@tonic-gate 		 */
1192d579eb63Sab 		if ((type == STT_TLS) && (sym->st_size != 0) &&
1193d579eb63Sab 		    (sym->st_shndx != SHN_UNDEF) &&
11940bc07c75Srie 		    (sym->st_shndx != SHN_COMMON)) {
119560758829Srie 			Is_desc		*isp = sdp->sd_isc;
119660758829Srie 			Ifl_desc	*ifl = sdp->sd_file;
11977c478bd9Sstevel@tonic-gate 
11986b3ba5bdSAli Bahrami 			if ((isp == NULL) || (isp->is_shdr == NULL) ||
11997c478bd9Sstevel@tonic-gate 			    ((isp->is_shdr->sh_flags & SHF_TLS) == 0)) {
12005aefb655Srie 				eprintf(ofl->ofl_lml, ERR_FATAL,
12015aefb655Srie 				    MSG_INTL(MSG_SYM_TLS),
12027c478bd9Sstevel@tonic-gate 				    demangle(sdp->sd_name), ifl->ifl_name);
12037c478bd9Sstevel@tonic-gate 				ofl->ofl_flags |= FLG_OF_FATAL;
12047c478bd9Sstevel@tonic-gate 				continue;
12057c478bd9Sstevel@tonic-gate 			}
12067c478bd9Sstevel@tonic-gate 		}
12077c478bd9Sstevel@tonic-gate 
12087c478bd9Sstevel@tonic-gate 		if ((sdp->sd_flags & FLG_SY_VISIBLE) == 0)
12095aefb655Srie 			ld_sym_adjust_vis(sdp, ofl);
12107c478bd9Sstevel@tonic-gate 
12117c478bd9Sstevel@tonic-gate 		if ((sdp->sd_flags & FLG_SY_REDUCED) &&
12127c478bd9Sstevel@tonic-gate 		    (oflags & FLG_OF_PROCRED)) {
12135aefb655Srie 			DBG_CALL(Dbg_syms_reduce(ofl, DBG_SYM_REDUCE_GLOBAL,
12145aefb655Srie 			    sdp, 0, 0));
12157c478bd9Sstevel@tonic-gate 		}
12167c478bd9Sstevel@tonic-gate 
121760758829Srie 		/*
121860758829Srie 		 * Record any STV_SINGLETON existence.
121960758829Srie 		 */
122060758829Srie 		if ((vis = ELF_ST_VISIBILITY(sym->st_other)) == STV_SINGLETON)
122160758829Srie 			ofl->ofl_dtflags_1 |= DF_1_SINGLETON;
122260758829Srie 
12237c478bd9Sstevel@tonic-gate 		/*
12247c478bd9Sstevel@tonic-gate 		 * If building a shared object or executable, and this is a
12257c478bd9Sstevel@tonic-gate 		 * non-weak UNDEF symbol with reduced visibility (STV_*), then
12267c478bd9Sstevel@tonic-gate 		 * give a fatal error.
12277c478bd9Sstevel@tonic-gate 		 */
122860758829Srie 		if (((oflags & FLG_OF_RELOBJ) == 0) &&
12290bc07c75Srie 		    (sym->st_shndx == SHN_UNDEF) &&
12307c478bd9Sstevel@tonic-gate 		    (ELF_ST_BIND(sym->st_info) != STB_WEAK)) {
123160758829Srie 			if (vis && (vis != STV_SINGLETON)) {
123260758829Srie 				sym_undef_entry(ofl, sdp, BNDLOCAL);
123360758829Srie 				ofl->ofl_flags |= FLG_OF_FATAL;
123460758829Srie 				continue;
123560758829Srie 			}
12367c478bd9Sstevel@tonic-gate 		}
12377c478bd9Sstevel@tonic-gate 
12387c478bd9Sstevel@tonic-gate 		/*
12397c478bd9Sstevel@tonic-gate 		 * If this symbol is defined in a non-allocatable section,
12407c478bd9Sstevel@tonic-gate 		 * reduce it to local symbol.
12417c478bd9Sstevel@tonic-gate 		 */
12427c478bd9Sstevel@tonic-gate 		if (((isp = sdp->sd_isc) != 0) && isp->is_shdr &&
12437c478bd9Sstevel@tonic-gate 		    ((isp->is_shdr->sh_flags & SHF_ALLOC) == 0)) {
12447c478bd9Sstevel@tonic-gate 			sdp->sd_flags |= FLG_SY_REDUCED;
124560758829Srie 			sdp->sd_flags1 |= FLG_SY1_HIDDEN;
12467c478bd9Sstevel@tonic-gate 		}
12477c478bd9Sstevel@tonic-gate 
12487c478bd9Sstevel@tonic-gate 		/*
12497c478bd9Sstevel@tonic-gate 		 * If this symbol originated as a SHN_SUNW_IGNORE, it will have
12507c478bd9Sstevel@tonic-gate 		 * been processed as an SHN_UNDEF.  Return the symbol to its
12517c478bd9Sstevel@tonic-gate 		 * original index for validation, and propagation to the output
12527c478bd9Sstevel@tonic-gate 		 * file.
12537c478bd9Sstevel@tonic-gate 		 */
12547c478bd9Sstevel@tonic-gate 		if (sdp->sd_flags1 & FLG_SY1_IGNORE)
12550bc07c75Srie 			sdp->sd_shndx = SHN_SUNW_IGNORE;
12567c478bd9Sstevel@tonic-gate 
12577c478bd9Sstevel@tonic-gate 		if (undef) {
12587c478bd9Sstevel@tonic-gate 			/*
12593b41b08bSab 			 * If a non-weak reference remains undefined, or if a
12607c478bd9Sstevel@tonic-gate 			 * mapfile reference is not bound to the relocatable
12617c478bd9Sstevel@tonic-gate 			 * objects that make up the object being built, we have
12627c478bd9Sstevel@tonic-gate 			 * a fatal error.
12637c478bd9Sstevel@tonic-gate 			 *
12647c478bd9Sstevel@tonic-gate 			 * The exceptions are symbols which are defined to be
12657c478bd9Sstevel@tonic-gate 			 * found in the parent (FLG_SY_PARENT), which is really
12667c478bd9Sstevel@tonic-gate 			 * only meaningful for direct binding, or are defined
12677c478bd9Sstevel@tonic-gate 			 * external (FLG_SY_EXTERN) so as to suppress -zdefs
12687c478bd9Sstevel@tonic-gate 			 * errors.
12697c478bd9Sstevel@tonic-gate 			 *
12707c478bd9Sstevel@tonic-gate 			 * Register symbols are always allowed to be UNDEF.
12717c478bd9Sstevel@tonic-gate 			 *
12727c478bd9Sstevel@tonic-gate 			 * Note that we don't include references created via -u
12737c478bd9Sstevel@tonic-gate 			 * in the same shared object binding test.  This is for
12747c478bd9Sstevel@tonic-gate 			 * backward compatibility, in that a number of archive
12757c478bd9Sstevel@tonic-gate 			 * makefile rules used -u to cause archive extraction.
12767c478bd9Sstevel@tonic-gate 			 * These same rules have been cut and pasted to apply
12777c478bd9Sstevel@tonic-gate 			 * to shared objects, and thus although the -u reference
12787c478bd9Sstevel@tonic-gate 			 * is redundant, flagging it as fatal could cause some
12797c478bd9Sstevel@tonic-gate 			 * build to fail.  Also we have documented the use of
12807c478bd9Sstevel@tonic-gate 			 * -u as a mechanism to cause binding to weak version
12817c478bd9Sstevel@tonic-gate 			 * definitions, thus giving users an error condition
12827c478bd9Sstevel@tonic-gate 			 * would be incorrect.
12837c478bd9Sstevel@tonic-gate 			 */
12847c478bd9Sstevel@tonic-gate 			if (!(sdp->sd_flags & FLG_SY_REGSYM) &&
12850bc07c75Srie 			    ((sym->st_shndx == SHN_UNDEF) &&
12867c478bd9Sstevel@tonic-gate 			    ((ELF_ST_BIND(sym->st_info) != STB_WEAK) &&
12877c478bd9Sstevel@tonic-gate 			    ((sdp->sd_flags &
12887c478bd9Sstevel@tonic-gate 			    (FLG_SY_PARENT | FLG_SY_EXTERN)) == 0)) ||
12897c478bd9Sstevel@tonic-gate 			    (((sdp->sd_flags &
12907c478bd9Sstevel@tonic-gate 			    (FLG_SY_MAPREF | FLG_SY_MAPUSED)) ==
12917c478bd9Sstevel@tonic-gate 			    FLG_SY_MAPREF) &&
129260758829Srie 			    ((sdp->sd_flags1 & (FLG_SY1_HIDDEN |
129360758829Srie 			    FLG_SY1_PROTECT)) == 0)))) {
12945aefb655Srie 				sym_undef_entry(ofl, sdp, UNDEF);
12957c478bd9Sstevel@tonic-gate 				ofl->ofl_flags |= undef;
12967c478bd9Sstevel@tonic-gate 				undeferr = 1;
12977c478bd9Sstevel@tonic-gate 			}
12987c478bd9Sstevel@tonic-gate 
12997c478bd9Sstevel@tonic-gate 		} else {
13007c478bd9Sstevel@tonic-gate 			/*
13017c478bd9Sstevel@tonic-gate 			 * For building things like shared objects (or anything
13027c478bd9Sstevel@tonic-gate 			 * -znodefs), undefined symbols are allowed.
13037c478bd9Sstevel@tonic-gate 			 *
13047c478bd9Sstevel@tonic-gate 			 * If a mapfile reference remains undefined the user
13057c478bd9Sstevel@tonic-gate 			 * would probably like a warning at least (they've
13067c478bd9Sstevel@tonic-gate 			 * usually mis-spelt the reference).  Refer to the above
13077c478bd9Sstevel@tonic-gate 			 * comments for discussion on -u references, which
13087c478bd9Sstevel@tonic-gate 			 * are not tested for in the same manner.
13097c478bd9Sstevel@tonic-gate 			 */
13107c478bd9Sstevel@tonic-gate 			if ((sdp->sd_flags &
13117c478bd9Sstevel@tonic-gate 			    (FLG_SY_MAPREF | FLG_SY_MAPUSED)) ==
13127c478bd9Sstevel@tonic-gate 			    FLG_SY_MAPREF) {
13135aefb655Srie 				sym_undef_entry(ofl, sdp, UNDEF);
13147c478bd9Sstevel@tonic-gate 				ofl->ofl_flags |= FLG_OF_WARN;
13157c478bd9Sstevel@tonic-gate 				undeferr = 1;
13167c478bd9Sstevel@tonic-gate 			}
13177c478bd9Sstevel@tonic-gate 		}
13187c478bd9Sstevel@tonic-gate 
13197c478bd9Sstevel@tonic-gate 		/*
13207c478bd9Sstevel@tonic-gate 		 * If this symbol comes from a dependency mark the dependency
13217c478bd9Sstevel@tonic-gate 		 * as required (-z ignore can result in unused dependencies
13227c478bd9Sstevel@tonic-gate 		 * being dropped).  If we need to record dependency versioning
13237c478bd9Sstevel@tonic-gate 		 * information indicate what version of the needed shared object
13247c478bd9Sstevel@tonic-gate 		 * this symbol is part of.  Flag the symbol as undefined if it
13257c478bd9Sstevel@tonic-gate 		 * has not been made available to us.
13267c478bd9Sstevel@tonic-gate 		 */
13277c478bd9Sstevel@tonic-gate 		if ((sdp->sd_ref == REF_DYN_NEED) &&
13287c478bd9Sstevel@tonic-gate 		    (!(sdp->sd_flags & FLG_SY_REFRSD))) {
13297c478bd9Sstevel@tonic-gate 			sdp->sd_file->ifl_flags |= FLG_IF_DEPREQD;
13307c478bd9Sstevel@tonic-gate 
13317c478bd9Sstevel@tonic-gate 			/*
13327c478bd9Sstevel@tonic-gate 			 * Capture that we've bound to a symbol that doesn't
13337c478bd9Sstevel@tonic-gate 			 * allow being directly bound to.
13347c478bd9Sstevel@tonic-gate 			 */
13357c478bd9Sstevel@tonic-gate 			if (sdp->sd_flags1 & FLG_SY1_NDIR)
13367c478bd9Sstevel@tonic-gate 				ofl->ofl_flags1 |= FLG_OF1_NDIRECT;
13377c478bd9Sstevel@tonic-gate 
13387c478bd9Sstevel@tonic-gate 			if (sdp->sd_file->ifl_vercnt) {
13397c478bd9Sstevel@tonic-gate 				int		vndx;
134060758829Srie 				Ver_index	*vip;
13417c478bd9Sstevel@tonic-gate 
13427c478bd9Sstevel@tonic-gate 				vndx = sdp->sd_aux->sa_dverndx;
13437c478bd9Sstevel@tonic-gate 				vip = &sdp->sd_file->ifl_verndx[vndx];
13447c478bd9Sstevel@tonic-gate 				if (vip->vi_flags & FLG_VER_AVAIL) {
13457c478bd9Sstevel@tonic-gate 					vip->vi_flags |= FLG_VER_REFER;
13467c478bd9Sstevel@tonic-gate 				} else {
13475aefb655Srie 					sym_undef_entry(ofl, sdp, NOTAVAIL);
13487c478bd9Sstevel@tonic-gate 					ofl->ofl_flags |= FLG_OF_FATAL;
13497c478bd9Sstevel@tonic-gate 					continue;
13507c478bd9Sstevel@tonic-gate 				}
13517c478bd9Sstevel@tonic-gate 			}
13527c478bd9Sstevel@tonic-gate 		}
13537c478bd9Sstevel@tonic-gate 
13547c478bd9Sstevel@tonic-gate 		/*
13557c478bd9Sstevel@tonic-gate 		 * Test that we do not bind to symbol supplied from an implicit
13567c478bd9Sstevel@tonic-gate 		 * shared object.  If a binding is from a weak reference it can
13577c478bd9Sstevel@tonic-gate 		 * be ignored.
13587c478bd9Sstevel@tonic-gate 		 */
13597c478bd9Sstevel@tonic-gate 		if (needed && !undeferr && (sdp->sd_flags & FLG_SY_GLOBREF) &&
13607c478bd9Sstevel@tonic-gate 		    (sdp->sd_ref == REF_DYN_NEED) &&
13617c478bd9Sstevel@tonic-gate 		    (sdp->sd_flags & FLG_SY_NOTAVAIL)) {
13625aefb655Srie 			sym_undef_entry(ofl, sdp, IMPLICIT);
13637c478bd9Sstevel@tonic-gate 			ofl->ofl_flags |= needed;
13647c478bd9Sstevel@tonic-gate 			continue;
13657c478bd9Sstevel@tonic-gate 		}
13667c478bd9Sstevel@tonic-gate 
13677c478bd9Sstevel@tonic-gate 		/*
13687c478bd9Sstevel@tonic-gate 		 * Test that a symbol isn't going to be reduced to local scope
13697c478bd9Sstevel@tonic-gate 		 * which actually wants to bind to a shared object - if so it's
13707c478bd9Sstevel@tonic-gate 		 * a fatal error.
13717c478bd9Sstevel@tonic-gate 		 */
13727c478bd9Sstevel@tonic-gate 		if ((sdp->sd_ref == REF_DYN_NEED) &&
137360758829Srie 		    (sdp->sd_flags1 & (FLG_SY1_HIDDEN | FLG_SY1_PROTECT))) {
13745aefb655Srie 			sym_undef_entry(ofl, sdp, BNDLOCAL);
13757c478bd9Sstevel@tonic-gate 			ofl->ofl_flags |= FLG_OF_FATAL;
13767c478bd9Sstevel@tonic-gate 			continue;
13777c478bd9Sstevel@tonic-gate 		}
13787c478bd9Sstevel@tonic-gate 
13797c478bd9Sstevel@tonic-gate 		/*
13807c478bd9Sstevel@tonic-gate 		 * If the output image is to be versioned then all symbol
13817c478bd9Sstevel@tonic-gate 		 * definitions must be associated with a version.
13827c478bd9Sstevel@tonic-gate 		 */
13837c478bd9Sstevel@tonic-gate 		if (verdesc && (sdp->sd_ref == REF_REL_NEED) &&
13840bc07c75Srie 		    (sym->st_shndx != SHN_UNDEF) &&
138560758829Srie 		    (!(sdp->sd_flags1 & FLG_SY1_HIDDEN)) &&
13867c478bd9Sstevel@tonic-gate 		    (sdp->sd_aux->sa_overndx == 0)) {
13875aefb655Srie 			sym_undef_entry(ofl, sdp, NOVERSION);
13887c478bd9Sstevel@tonic-gate 			ofl->ofl_flags |= verdesc;
13897c478bd9Sstevel@tonic-gate 			continue;
13907c478bd9Sstevel@tonic-gate 		}
13917c478bd9Sstevel@tonic-gate 
13927c478bd9Sstevel@tonic-gate 		/*
13937c478bd9Sstevel@tonic-gate 		 * If we don't need the symbol there's no need to process it
13947c478bd9Sstevel@tonic-gate 		 * any further.
13957c478bd9Sstevel@tonic-gate 		 */
13967c478bd9Sstevel@tonic-gate 		if (sdp->sd_ref == REF_DYN_SEEN)
13977c478bd9Sstevel@tonic-gate 			continue;
13987c478bd9Sstevel@tonic-gate 
13997c478bd9Sstevel@tonic-gate 		/*
14007c478bd9Sstevel@tonic-gate 		 * Calculate the size and alignment requirements for the global
14017c478bd9Sstevel@tonic-gate 		 * .bss and .tls sections.  If we're building a relocatable
14027c478bd9Sstevel@tonic-gate 		 * object only account for scoped COMMON symbols (these will
14037c478bd9Sstevel@tonic-gate 		 * be converted to .bss references).
14047c478bd9Sstevel@tonic-gate 		 *
140535450702SAli Bahrami 		 * When -z nopartial is in effect, partially initialized
140635450702SAli Bahrami 		 * symbols are directed to the special .data section
140735450702SAli Bahrami 		 * created for that purpose (ofl->ofl_isparexpn).
140835450702SAli Bahrami 		 * Otherwise, partially initialized symbols go to .bss.
14097c478bd9Sstevel@tonic-gate 		 *
14107c478bd9Sstevel@tonic-gate 		 * Also refer to make_mvsections() in sunwmove.c
14117c478bd9Sstevel@tonic-gate 		 */
14120bc07c75Srie 		if ((sym->st_shndx == SHN_COMMON) &&
14130bc07c75Srie 		    (((oflags & FLG_OF_RELOBJ) == 0) ||
141460758829Srie 		    ((sdp->sd_flags1 & FLG_SY1_HIDDEN) &&
14157c478bd9Sstevel@tonic-gate 		    (oflags & FLG_OF_PROCRED)))) {
1416*57ef7aa9SRod Evans 			if ((sdp->sd_move == NULL) ||
141735450702SAli Bahrami 			    ((sdp->sd_flags & FLG_SY_PAREXPN) == 0)) {
14187c478bd9Sstevel@tonic-gate 				Xword * size, * align;
14197c478bd9Sstevel@tonic-gate 
1420d579eb63Sab 				if (type != STT_TLS) {
14217c478bd9Sstevel@tonic-gate 					size = &bsssize;
14227c478bd9Sstevel@tonic-gate 					align = &bssalign;
14237c478bd9Sstevel@tonic-gate 				} else {
14247c478bd9Sstevel@tonic-gate 					size = &tlssize;
14257c478bd9Sstevel@tonic-gate 					align = &tlsalign;
14267c478bd9Sstevel@tonic-gate 				}
14277c478bd9Sstevel@tonic-gate 				*size = (Xword)S_ROUND(*size, sym->st_value) +
1428dd94ecefSrie 				    sym->st_size;
14297c478bd9Sstevel@tonic-gate 				if (sym->st_value > *align)
14307c478bd9Sstevel@tonic-gate 					*align = sym->st_value;
14317c478bd9Sstevel@tonic-gate 			}
14327c478bd9Sstevel@tonic-gate 		}
14337c478bd9Sstevel@tonic-gate 
1434ba2be530Sab #if	defined(_ELF64)
143554d82594Sseizo 		/*
143654d82594Sseizo 		 * Calculate the size and alignment requirement for the global
143754d82594Sseizo 		 * .lbss. TLS or partially initialized symbols do not need to be
143854d82594Sseizo 		 * considered yet.
143954d82594Sseizo 		 */
1440ba2be530Sab 		if ((ld_targ.t_m.m_mach == EM_AMD64) &&
1441ba2be530Sab 		    (sym->st_shndx == SHN_X86_64_LCOMMON)) {
144254d82594Sseizo 			lbsssize = (Xword)S_ROUND(lbsssize, sym->st_value) +
144333eb6ee1Sab 			    sym->st_size;
144454d82594Sseizo 			if (sym->st_value > lbssalign)
144554d82594Sseizo 				lbssalign = sym->st_value;
144654d82594Sseizo 		}
144754d82594Sseizo #endif
144854d82594Sseizo 
14497c478bd9Sstevel@tonic-gate 		/*
14507c478bd9Sstevel@tonic-gate 		 * If a symbol was referenced via the command line
14517c478bd9Sstevel@tonic-gate 		 * (ld -u <>, ...), then this counts as a reference against the
14527c478bd9Sstevel@tonic-gate 		 * symbol. Mark any section that symbol is defined in.
14537c478bd9Sstevel@tonic-gate 		 */
14547c478bd9Sstevel@tonic-gate 		if (((isp = sdp->sd_isc) != 0) &&
14557c478bd9Sstevel@tonic-gate 		    (sdp->sd_flags & FLG_SY_CMDREF)) {
14567c478bd9Sstevel@tonic-gate 			isp->is_flags |= FLG_IS_SECTREF;
14577c478bd9Sstevel@tonic-gate 			isp->is_file->ifl_flags |= FLG_IF_FILEREF;
14587c478bd9Sstevel@tonic-gate 		}
14597c478bd9Sstevel@tonic-gate 
14607c478bd9Sstevel@tonic-gate 		/*
14617c478bd9Sstevel@tonic-gate 		 * Update the symbol count and the associated name string size.
14627c478bd9Sstevel@tonic-gate 		 */
1463d4517e84Srie 		if ((sdp->sd_flags1 & FLG_SY1_HIDDEN) &&
14647c478bd9Sstevel@tonic-gate 		    (oflags & FLG_OF_PROCRED)) {
14657c478bd9Sstevel@tonic-gate 			/*
146660758829Srie 			 * If any reductions are being processed, keep a count
146760758829Srie 			 * of eliminated symbols, and if the symbol is being
146860758829Srie 			 * reduced to local, count it's size for the .symtab.
14697c478bd9Sstevel@tonic-gate 			 */
14707c478bd9Sstevel@tonic-gate 			if (sdp->sd_flags1 & FLG_SY1_ELIM) {
14717c478bd9Sstevel@tonic-gate 				ofl->ofl_elimcnt++;
14727c478bd9Sstevel@tonic-gate 			} else {
14737c478bd9Sstevel@tonic-gate 				ofl->ofl_scopecnt++;
14747c478bd9Sstevel@tonic-gate 				if ((((sdp->sd_flags & FLG_SY_REGSYM) == 0) ||
14757c478bd9Sstevel@tonic-gate 				    sym->st_name) && (st_insert(ofl->ofl_strtab,
14767c478bd9Sstevel@tonic-gate 				    sdp->sd_name) == -1))
14777c478bd9Sstevel@tonic-gate 					return (S_ERROR);
14789039eeafSab 				if (allow_ldynsym && sym->st_name &&
1479d579eb63Sab 				    ldynsym_symtype[type]) {
14809039eeafSab 					ofl->ofl_dynscopecnt++;
14819039eeafSab 					if (st_insert(ofl->ofl_dynstrtab,
14829039eeafSab 					    sdp->sd_name) == -1)
14839039eeafSab 						return (S_ERROR);
1484d579eb63Sab 					/* Include it in sort section? */
1485d579eb63Sab 					DYNSORT_COUNT(sdp, sym, type, ++);
14869039eeafSab 				}
14877c478bd9Sstevel@tonic-gate 			}
14887c478bd9Sstevel@tonic-gate 		} else {
14897c478bd9Sstevel@tonic-gate 			ofl->ofl_globcnt++;
14907c478bd9Sstevel@tonic-gate 
1491d579eb63Sab 			/*
1492d579eb63Sab 			 * Check to see if this global variable should
1493d579eb63Sab 			 * go into a sort section. Sort sections require
1494d579eb63Sab 			 * a .SUNW_ldynsym section, so, don't check
1495d579eb63Sab 			 * unless a .SUNW_ldynsym is allowed.
1496d579eb63Sab 			 */
1497d579eb63Sab 			if (allow_ldynsym) {
1498d579eb63Sab 				DYNSORT_COUNT(sdp, sym, type, ++);
1499d579eb63Sab 			}
1500d579eb63Sab 
15017c478bd9Sstevel@tonic-gate 			/*
15027c478bd9Sstevel@tonic-gate 			 * If global direct bindings are in effect, or this
15037c478bd9Sstevel@tonic-gate 			 * symbol has bound to a dependency which was specified
15047c478bd9Sstevel@tonic-gate 			 * as requiring direct bindings, and it hasn't
15057c478bd9Sstevel@tonic-gate 			 * explicitly been defined as a non-direct binding
15067c478bd9Sstevel@tonic-gate 			 * symbol, mark it.
15077c478bd9Sstevel@tonic-gate 			 */
15087c478bd9Sstevel@tonic-gate 			if (((ofl->ofl_dtflags_1 & DF_1_DIRECT) || (isp &&
15097c478bd9Sstevel@tonic-gate 			    (isp->is_file->ifl_flags & FLG_IF_DIRECT))) &&
15107c478bd9Sstevel@tonic-gate 			    ((sdp->sd_flags1 & FLG_SY1_NDIR) == 0))
15117c478bd9Sstevel@tonic-gate 				sdp->sd_flags1 |= FLG_SY1_DIR;
15127c478bd9Sstevel@tonic-gate 
15137c478bd9Sstevel@tonic-gate 			/*
15147c478bd9Sstevel@tonic-gate 			 * Insert the symbol name.
15157c478bd9Sstevel@tonic-gate 			 */
15167c478bd9Sstevel@tonic-gate 			if (((sdp->sd_flags & FLG_SY_REGSYM) == 0) ||
15177c478bd9Sstevel@tonic-gate 			    sym->st_name) {
15187c478bd9Sstevel@tonic-gate 				if (st_insert(ofl->ofl_strtab,
15197c478bd9Sstevel@tonic-gate 				    sdp->sd_name) == -1)
15207c478bd9Sstevel@tonic-gate 					return (S_ERROR);
15217c478bd9Sstevel@tonic-gate 
15227c478bd9Sstevel@tonic-gate 				if (!(ofl->ofl_flags & FLG_OF_RELOBJ) &&
15237c478bd9Sstevel@tonic-gate 				    (st_insert(ofl->ofl_dynstrtab,
15247c478bd9Sstevel@tonic-gate 				    sdp->sd_name) == -1))
15257c478bd9Sstevel@tonic-gate 					return (S_ERROR);
15267c478bd9Sstevel@tonic-gate 			}
15277c478bd9Sstevel@tonic-gate 
15287c478bd9Sstevel@tonic-gate 			/*
15297c478bd9Sstevel@tonic-gate 			 * If this section offers a global symbol - record that
15307c478bd9Sstevel@tonic-gate 			 * fact.
15317c478bd9Sstevel@tonic-gate 			 */
15327c478bd9Sstevel@tonic-gate 			if (isp) {
15337c478bd9Sstevel@tonic-gate 				isp->is_flags |= FLG_IS_SECTREF;
15347c478bd9Sstevel@tonic-gate 				isp->is_file->ifl_flags |= FLG_IF_FILEREF;
15357c478bd9Sstevel@tonic-gate 			}
15367c478bd9Sstevel@tonic-gate 		}
15377c478bd9Sstevel@tonic-gate 	}
15387c478bd9Sstevel@tonic-gate 
15397c478bd9Sstevel@tonic-gate 	/*
15407c478bd9Sstevel@tonic-gate 	 * If we've encountered a fatal error during symbol validation then
15417c478bd9Sstevel@tonic-gate 	 * return now.
15427c478bd9Sstevel@tonic-gate 	 */
15437c478bd9Sstevel@tonic-gate 	if (ofl->ofl_flags & FLG_OF_FATAL)
15447c478bd9Sstevel@tonic-gate 		return (1);
15457c478bd9Sstevel@tonic-gate 
15467c478bd9Sstevel@tonic-gate 	/*
15477c478bd9Sstevel@tonic-gate 	 * Now that symbol resolution is completed, scan any register symbols.
15487c478bd9Sstevel@tonic-gate 	 * From now on, we're only interested in those that contribute to the
15497c478bd9Sstevel@tonic-gate 	 * output file.
15507c478bd9Sstevel@tonic-gate 	 */
15517c478bd9Sstevel@tonic-gate 	if (ofl->ofl_regsyms) {
15527c478bd9Sstevel@tonic-gate 		int	ndx;
15537c478bd9Sstevel@tonic-gate 
15547c478bd9Sstevel@tonic-gate 		for (ndx = 0; ndx < ofl->ofl_regsymsno; ndx++) {
15556b3ba5bdSAli Bahrami 			if ((sdp = ofl->ofl_regsyms[ndx]) == NULL)
15567c478bd9Sstevel@tonic-gate 				continue;
15577c478bd9Sstevel@tonic-gate 			if (sdp->sd_ref != REF_REL_NEED) {
15586b3ba5bdSAli Bahrami 				ofl->ofl_regsyms[ndx] = NULL;
15597c478bd9Sstevel@tonic-gate 				continue;
15607c478bd9Sstevel@tonic-gate 			}
15617c478bd9Sstevel@tonic-gate 
15627c478bd9Sstevel@tonic-gate 			ofl->ofl_regsymcnt++;
15637c478bd9Sstevel@tonic-gate 			if (sdp->sd_sym->st_name == 0)
15647c478bd9Sstevel@tonic-gate 				sdp->sd_name = MSG_ORIG(MSG_STR_EMPTY);
15657c478bd9Sstevel@tonic-gate 
156660758829Srie 			if ((sdp->sd_flags1 & FLG_SY1_HIDDEN) ||
15677c478bd9Sstevel@tonic-gate 			    (ELF_ST_BIND(sdp->sd_sym->st_info) == STB_LOCAL))
15687c478bd9Sstevel@tonic-gate 				ofl->ofl_lregsymcnt++;
15697c478bd9Sstevel@tonic-gate 		}
15707c478bd9Sstevel@tonic-gate 	}
15717c478bd9Sstevel@tonic-gate 
15727c478bd9Sstevel@tonic-gate 	/*
15737c478bd9Sstevel@tonic-gate 	 * Generate the .bss section now that we know its size and alignment.
15747c478bd9Sstevel@tonic-gate 	 */
1575*57ef7aa9SRod Evans 	if (bsssize) {
1576*57ef7aa9SRod Evans 		if (ld_make_bss(ofl, bsssize, bssalign,
1577*57ef7aa9SRod Evans 		    ld_targ.t_id.id_bss) == S_ERROR)
15787c478bd9Sstevel@tonic-gate 			return (S_ERROR);
15797c478bd9Sstevel@tonic-gate 	}
15807c478bd9Sstevel@tonic-gate 	if (tlssize) {
1581*57ef7aa9SRod Evans 		if (ld_make_bss(ofl, tlssize, tlsalign,
1582*57ef7aa9SRod Evans 		    ld_targ.t_id.id_tlsbss) == S_ERROR)
158354d82594Sseizo 			return (S_ERROR);
158454d82594Sseizo 	}
1585ba2be530Sab #if	defined(_ELF64)
1586ba2be530Sab 	if ((ld_targ.t_m.m_mach == EM_AMD64) &&
1587ba2be530Sab 	    lbsssize && !(oflags & FLG_OF_RELOBJ)) {
1588*57ef7aa9SRod Evans 		if (ld_make_bss(ofl, lbsssize, lbssalign,
1589*57ef7aa9SRod Evans 		    ld_targ.t_id.id_lbss) == S_ERROR)
15907c478bd9Sstevel@tonic-gate 			return (S_ERROR);
15917c478bd9Sstevel@tonic-gate 	}
159254d82594Sseizo #endif
15937c478bd9Sstevel@tonic-gate 	/*
15947c478bd9Sstevel@tonic-gate 	 * Determine what entry point symbol we need, and if found save its
15957c478bd9Sstevel@tonic-gate 	 * symbol descriptor so that we can update the ELF header entry with the
15967c478bd9Sstevel@tonic-gate 	 * symbols value later (see update_oehdr).  Make sure the symbol is
1597c1c6f601Srie 	 * tagged to ensure its update in case -s is in effect.  Use any -e
15987c478bd9Sstevel@tonic-gate 	 * option first, or the default entry points `_start' and `main'.
15997c478bd9Sstevel@tonic-gate 	 */
1600c1c6f601Srie 	ret = 0;
16017c478bd9Sstevel@tonic-gate 	if (ofl->ofl_entry) {
16021d1fba8aSrie 		if ((sdp =
16031d1fba8aSrie 		    ld_sym_find(ofl->ofl_entry, SYM_NOHASH, 0, ofl)) == NULL) {
16041d1fba8aSrie 			eprintf(ofl->ofl_lml, ERR_FATAL,
16051d1fba8aSrie 			    MSG_INTL(MSG_ARG_NOENTRY), ofl->ofl_entry);
16061d1fba8aSrie 			ret++;
16071d1fba8aSrie 		} else if (ensure_sym_local(ofl, sdp,
16081d1fba8aSrie 		    MSG_INTL(MSG_SYM_ENTRY)) != 0) {
1609c1c6f601Srie 			ret++;
16101d1fba8aSrie 		} else {
16111d1fba8aSrie 			ofl->ofl_entry = (void *)sdp;
16127c478bd9Sstevel@tonic-gate 		}
16135aefb655Srie 	} else if (((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_START),
1614c1c6f601Srie 	    SYM_NOHASH, 0, ofl)) != NULL) && (ensure_sym_local(ofl,
1615c1c6f601Srie 	    sdp, 0) == 0)) {
16167c478bd9Sstevel@tonic-gate 		ofl->ofl_entry = (void *)sdp;
1617c1c6f601Srie 
16185aefb655Srie 	} else if (((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_MAIN),
1619c1c6f601Srie 	    SYM_NOHASH, 0, ofl)) != NULL) && (ensure_sym_local(ofl,
1620c1c6f601Srie 	    sdp, 0) == 0)) {
16217c478bd9Sstevel@tonic-gate 		ofl->ofl_entry = (void *)sdp;
16227c478bd9Sstevel@tonic-gate 	}
16237c478bd9Sstevel@tonic-gate 
16247c478bd9Sstevel@tonic-gate 	/*
1625c1c6f601Srie 	 * If ld -zdtrace=<sym> was given, then validate that the symbol is
1626c1c6f601Srie 	 * defined within the current object being built.
16277c478bd9Sstevel@tonic-gate 	 */
16281d1fba8aSrie 	if ((sdp = ofl->ofl_dtracesym) != 0)
1629c1c6f601Srie 		ret += ensure_sym_local(ofl, sdp, MSG_ORIG(MSG_STR_DTRACE));
16307c478bd9Sstevel@tonic-gate 
1631c1c6f601Srie 	/*
1632c1c6f601Srie 	 * If any initarray, finiarray or preinitarray functions have been
1633c1c6f601Srie 	 * requested, make sure they are defined within the current object
1634c1c6f601Srie 	 * being built.
1635c1c6f601Srie 	 */
1636*57ef7aa9SRod Evans 	if (ofl->ofl_initarray) {
1637*57ef7aa9SRod Evans 		ret += ensure_array_local(ofl, ofl->ofl_initarray,
1638c1c6f601Srie 		    MSG_ORIG(MSG_SYM_INITARRAY));
1639c1c6f601Srie 	}
1640*57ef7aa9SRod Evans 	if (ofl->ofl_finiarray) {
1641*57ef7aa9SRod Evans 		ret += ensure_array_local(ofl, ofl->ofl_finiarray,
1642c1c6f601Srie 		    MSG_ORIG(MSG_SYM_FINIARRAY));
1643c1c6f601Srie 	}
1644*57ef7aa9SRod Evans 	if (ofl->ofl_preiarray) {
1645*57ef7aa9SRod Evans 		ret += ensure_array_local(ofl, ofl->ofl_preiarray,
1646c1c6f601Srie 		    MSG_ORIG(MSG_SYM_PREINITARRAY));
1647c1c6f601Srie 	}
1648c1c6f601Srie 
1649c1c6f601Srie 	if (ret)
1650c1c6f601Srie 		return (S_ERROR);
1651c1c6f601Srie 
16527c478bd9Sstevel@tonic-gate 	/*
16537c478bd9Sstevel@tonic-gate 	 * If we're required to record any needed dependencies versioning
16547c478bd9Sstevel@tonic-gate 	 * information calculate it now that all symbols have been validated.
16557c478bd9Sstevel@tonic-gate 	 */
16567c478bd9Sstevel@tonic-gate 	if ((oflags & (FLG_OF_VERNEED | FLG_OF_NOVERSEC)) == FLG_OF_VERNEED)
16575aefb655Srie 		return (ld_vers_check_need(ofl));
16587c478bd9Sstevel@tonic-gate 	else
16597c478bd9Sstevel@tonic-gate 		return (1);
16607c478bd9Sstevel@tonic-gate }
16617c478bd9Sstevel@tonic-gate 
16627c478bd9Sstevel@tonic-gate /*
16637c478bd9Sstevel@tonic-gate  * qsort(3c) comparison function.  As an optimization for associating weak
16647c478bd9Sstevel@tonic-gate  * symbols to their strong counterparts sort global symbols according to their
16656b3ba5bdSAli Bahrami  * section index, address and binding.
16667c478bd9Sstevel@tonic-gate  */
16677c478bd9Sstevel@tonic-gate static int
16686b3ba5bdSAli Bahrami compare(const void *sdpp1, const void *sdpp2)
16697c478bd9Sstevel@tonic-gate {
16706b3ba5bdSAli Bahrami 	Sym_desc	*sdp1 = *((Sym_desc **)sdpp1);
16716b3ba5bdSAli Bahrami 	Sym_desc	*sdp2 = *((Sym_desc **)sdpp2);
16726b3ba5bdSAli Bahrami 	Sym		*sym1, *sym2;
16737c478bd9Sstevel@tonic-gate 	uchar_t		bind1, bind2;
16747c478bd9Sstevel@tonic-gate 
16757c478bd9Sstevel@tonic-gate 	/*
16767c478bd9Sstevel@tonic-gate 	 * Symbol descriptors may be zero, move these to the front of the
16777c478bd9Sstevel@tonic-gate 	 * sorted array.
16787c478bd9Sstevel@tonic-gate 	 */
16796b3ba5bdSAli Bahrami 	if (sdp1 == NULL)
16807c478bd9Sstevel@tonic-gate 		return (-1);
16816b3ba5bdSAli Bahrami 	if (sdp2 == NULL)
16827c478bd9Sstevel@tonic-gate 		return (1);
16837c478bd9Sstevel@tonic-gate 
16847c478bd9Sstevel@tonic-gate 	sym1 = sdp1->sd_sym;
16857c478bd9Sstevel@tonic-gate 	sym2 = sdp2->sd_sym;
16867c478bd9Sstevel@tonic-gate 
16876b3ba5bdSAli Bahrami 	/*
16886b3ba5bdSAli Bahrami 	 * Compare the symbols section index.  This is important when sorting
16896b3ba5bdSAli Bahrami 	 * the symbol tables of relocatable objects.  In this case, a symbols
16906b3ba5bdSAli Bahrami 	 * value is the offset within the associated section, and thus many
16916b3ba5bdSAli Bahrami 	 * symbols can have the same value, but are effectively different
16926b3ba5bdSAli Bahrami 	 * addresses.
16936b3ba5bdSAli Bahrami 	 */
16946b3ba5bdSAli Bahrami 	if (sym1->st_shndx > sym2->st_shndx)
16956b3ba5bdSAli Bahrami 		return (1);
16966b3ba5bdSAli Bahrami 	if (sym1->st_shndx < sym2->st_shndx)
16976b3ba5bdSAli Bahrami 		return (-1);
16986b3ba5bdSAli Bahrami 
16997c478bd9Sstevel@tonic-gate 	/*
17007c478bd9Sstevel@tonic-gate 	 * Compare the symbols value (address).
17017c478bd9Sstevel@tonic-gate 	 */
17027c478bd9Sstevel@tonic-gate 	if (sym1->st_value > sym2->st_value)
17037c478bd9Sstevel@tonic-gate 		return (1);
17047c478bd9Sstevel@tonic-gate 	if (sym1->st_value < sym2->st_value)
17057c478bd9Sstevel@tonic-gate 		return (-1);
17067c478bd9Sstevel@tonic-gate 
17077c478bd9Sstevel@tonic-gate 	bind1 = ELF_ST_BIND(sym1->st_info);
17087c478bd9Sstevel@tonic-gate 	bind2 = ELF_ST_BIND(sym2->st_info);
17097c478bd9Sstevel@tonic-gate 
17107c478bd9Sstevel@tonic-gate 	/*
17117c478bd9Sstevel@tonic-gate 	 * If two symbols have the same address place the weak symbol before
17127c478bd9Sstevel@tonic-gate 	 * any strong counterpart.
17137c478bd9Sstevel@tonic-gate 	 */
17147c478bd9Sstevel@tonic-gate 	if (bind1 > bind2)
17157c478bd9Sstevel@tonic-gate 		return (-1);
17167c478bd9Sstevel@tonic-gate 	if (bind1 < bind2)
17177c478bd9Sstevel@tonic-gate 		return (1);
17187c478bd9Sstevel@tonic-gate 
17197c478bd9Sstevel@tonic-gate 	return (0);
17207c478bd9Sstevel@tonic-gate }
17217c478bd9Sstevel@tonic-gate 
172269a0bf0cSab /*
172369a0bf0cSab  * Issue a MSG_SYM_BADADDR error from ld_sym_process(). This error
172469a0bf0cSab  * is issued when a symbol address/size is not contained by the
172569a0bf0cSab  * target section.
172669a0bf0cSab  *
172769a0bf0cSab  * Such objects are at least partially corrupt, and the user would
172869a0bf0cSab  * be well advised to be skeptical of them, and to ask their compiler
172969a0bf0cSab  * supplier to fix the problem. However, a distinction needs to be
173069a0bf0cSab  * made between symbols that reference readonly text, and those that
173169a0bf0cSab  * access writable data. Other than throwing off profiling results,
173269a0bf0cSab  * the readonly section case is less serious. We have encountered
173369a0bf0cSab  * such objects in the field. In order to allow existing objects
173469a0bf0cSab  * to continue working, we issue a warning rather than a fatal error
173569a0bf0cSab  * if the symbol is against readonly text. Other cases are fatal.
173669a0bf0cSab  */
173769a0bf0cSab static void
173869a0bf0cSab issue_badaddr_msg(Ifl_desc *ifl, Ofl_desc *ofl, Sym_desc *sdp,
173969a0bf0cSab     Sym *sym, Word shndx)
174069a0bf0cSab {
17411d9df23bSab 	ofl_flag_t	flag;
17421d9df23bSab 	Error		err;
17431d9df23bSab 	const char	*msg;
174469a0bf0cSab 
174569a0bf0cSab 	if ((sdp->sd_isc->is_shdr->sh_flags & (SHF_WRITE | SHF_ALLOC)) ==
174669a0bf0cSab 	    SHF_ALLOC) {
174769a0bf0cSab 		msg = MSG_INTL(MSG_SYM_BADADDR_ROTXT);
174869a0bf0cSab 		flag = FLG_OF_WARN;
174969a0bf0cSab 		err = ERR_WARNING;
175069a0bf0cSab 	} else {
175169a0bf0cSab 		msg = MSG_INTL(MSG_SYM_BADADDR);
175269a0bf0cSab 		flag = FLG_OF_FATAL;
175369a0bf0cSab 		err = ERR_FATAL;
175469a0bf0cSab 	}
175569a0bf0cSab 
175669a0bf0cSab 	eprintf(ofl->ofl_lml, err, msg, demangle(sdp->sd_name),
175769a0bf0cSab 	    ifl->ifl_name, shndx, sdp->sd_isc->is_name,
175869a0bf0cSab 	    EC_XWORD(sdp->sd_isc->is_shdr->sh_size),
175969a0bf0cSab 	    EC_XWORD(sym->st_value), EC_XWORD(sym->st_size));
176069a0bf0cSab 	ofl->ofl_flags |= flag;
176169a0bf0cSab }
176269a0bf0cSab 
176369a0bf0cSab 
17647c478bd9Sstevel@tonic-gate /*
17657c478bd9Sstevel@tonic-gate  * Process the symbol table for the specified input file.  At this point all
17667c478bd9Sstevel@tonic-gate  * input sections from this input file have been assigned an input section
17677c478bd9Sstevel@tonic-gate  * descriptor which is saved in the `ifl_isdesc' array.
17687c478bd9Sstevel@tonic-gate  *
17696b3ba5bdSAli Bahrami  *	-	local symbols are saved (as is) if the input file is a
17707c478bd9Sstevel@tonic-gate  *		relocatable object
17717c478bd9Sstevel@tonic-gate  *
17726b3ba5bdSAli Bahrami  *	-	global symbols are added to the linkers internal symbol
17737c478bd9Sstevel@tonic-gate  *		table if they are not already present, otherwise a symbol
17747c478bd9Sstevel@tonic-gate  *		resolution function is called upon to resolve the conflict.
17757c478bd9Sstevel@tonic-gate  */
17767c478bd9Sstevel@tonic-gate uintptr_t
17775aefb655Srie ld_sym_process(Is_desc *isc, Ifl_desc *ifl, Ofl_desc *ofl)
17787c478bd9Sstevel@tonic-gate {
177975e45495Sab 	/*
178075e45495Sab 	 * This macro tests the given symbol to see if it is out of
178175e45495Sab 	 * range relative to the section it references.
178275e45495Sab 	 *
178375e45495Sab 	 * entry:
178475e45495Sab 	 *	- ifl is a relative object (ET_REL)
178575e45495Sab 	 *	_sdp - Symbol descriptor
178675e45495Sab 	 *	_sym - Symbol
178775e45495Sab 	 *	_type - Symbol type
178875e45495Sab 	 *
178975e45495Sab 	 * The following are tested:
179075e45495Sab 	 *	- Symbol length is non-zero
179175e45495Sab 	 *	- Symbol type is a type that references code or data
179275e45495Sab 	 *	- Referenced section is not 0 (indicates an UNDEF symbol)
179375e45495Sab 	 *	  and is not in the range of special values above SHN_LORESERVE
179475e45495Sab 	 *	  (excluding SHN_XINDEX, which is OK).
179575e45495Sab 	 *	- We have a valid section header for the target section
179675e45495Sab 	 *
179775e45495Sab 	 * If the above are all true, and the symbol position is not
179875e45495Sab 	 * contained by the target section, this macro evaluates to
179975e45495Sab 	 * True (1). Otherwise, False(0).
180075e45495Sab 	 */
180175e45495Sab #define	SYM_LOC_BADADDR(_sdp, _sym, _type) \
1802d579eb63Sab 	(_sym->st_size && dynsymsort_symtype[_type] && \
180375e45495Sab 	(_sym->st_shndx != SHN_UNDEF) && \
180475e45495Sab 	((_sym->st_shndx < SHN_LORESERVE) || \
180575e45495Sab 		(_sym->st_shndx == SHN_XINDEX)) && \
180675e45495Sab 	_sdp->sd_isc && _sdp->sd_isc->is_shdr && \
180775e45495Sab 	((_sym->st_value + _sym->st_size) > _sdp->sd_isc->is_shdr->sh_size))
180875e45495Sab 
1809de777a60Sab 	Conv_inv_buf_t	inv_buf;
18107c478bd9Sstevel@tonic-gate 	Sym		*sym = (Sym *)isc->is_indata->d_buf;
18116b3ba5bdSAli Bahrami 	Word		*symshndx = NULL;
18127c478bd9Sstevel@tonic-gate 	Shdr		*shdr = isc->is_shdr;
18137c478bd9Sstevel@tonic-gate 	Sym_desc	*sdp;
18147c478bd9Sstevel@tonic-gate 	size_t		strsize;
18157c478bd9Sstevel@tonic-gate 	char		*strs;
18167c478bd9Sstevel@tonic-gate 	uchar_t		type, bind;
18177c478bd9Sstevel@tonic-gate 	Word		ndx, hash, local, total;
18187c478bd9Sstevel@tonic-gate 	Half		etype = ifl->ifl_ehdr->e_type;
181975e45495Sab 	int		etype_rel;
18207c478bd9Sstevel@tonic-gate 	const char	*symsecname, *strsecname;
18217c478bd9Sstevel@tonic-gate 	avl_index_t	where;
18226b3ba5bdSAli Bahrami 	int		test_gnu_hidden_bit, weak;
18237c478bd9Sstevel@tonic-gate 
18247c478bd9Sstevel@tonic-gate 	/*
18257c478bd9Sstevel@tonic-gate 	 * Its possible that a file may contain more that one symbol table,
18267c478bd9Sstevel@tonic-gate 	 * ie. .dynsym and .symtab in a shared library.  Only process the first
18277c478bd9Sstevel@tonic-gate 	 * table (here, we assume .dynsym comes before .symtab).
18287c478bd9Sstevel@tonic-gate 	 */
18297c478bd9Sstevel@tonic-gate 	if (ifl->ifl_symscnt)
18307c478bd9Sstevel@tonic-gate 		return (1);
18317c478bd9Sstevel@tonic-gate 
18327c478bd9Sstevel@tonic-gate 	if (isc->is_symshndx)
18337c478bd9Sstevel@tonic-gate 		symshndx = isc->is_symshndx->is_indata->d_buf;
18347c478bd9Sstevel@tonic-gate 
18355aefb655Srie 	DBG_CALL(Dbg_syms_process(ofl->ofl_lml, ifl));
18367c478bd9Sstevel@tonic-gate 
18377c478bd9Sstevel@tonic-gate 	if (isc->is_name)
18387c478bd9Sstevel@tonic-gate 		symsecname = isc->is_name;
18397c478bd9Sstevel@tonic-gate 	else
18407c478bd9Sstevel@tonic-gate 		symsecname = MSG_ORIG(MSG_STR_EMPTY);
18417c478bd9Sstevel@tonic-gate 
18427c478bd9Sstevel@tonic-gate 	/*
18437c478bd9Sstevel@tonic-gate 	 * From the symbol tables section header information determine which
18447c478bd9Sstevel@tonic-gate 	 * strtab table is needed to locate the actual symbol names.
18457c478bd9Sstevel@tonic-gate 	 */
18467c478bd9Sstevel@tonic-gate 	if (ifl->ifl_flags & FLG_IF_HSTRTAB) {
18477c478bd9Sstevel@tonic-gate 		ndx = shdr->sh_link;
18487c478bd9Sstevel@tonic-gate 		if ((ndx == 0) || (ndx >= ifl->ifl_shnum)) {
18495aefb655Srie 			eprintf(ofl->ofl_lml, ERR_FATAL,
18505aefb655Srie 			    MSG_INTL(MSG_FIL_INVSHLINK),
18517c478bd9Sstevel@tonic-gate 			    ifl->ifl_name, symsecname, EC_XWORD(ndx));
18527c478bd9Sstevel@tonic-gate 			return (S_ERROR);
18537c478bd9Sstevel@tonic-gate 		}
18547c478bd9Sstevel@tonic-gate 		strsize = ifl->ifl_isdesc[ndx]->is_shdr->sh_size;
18557c478bd9Sstevel@tonic-gate 		strs = ifl->ifl_isdesc[ndx]->is_indata->d_buf;
18567c478bd9Sstevel@tonic-gate 		if (ifl->ifl_isdesc[ndx]->is_name)
18577c478bd9Sstevel@tonic-gate 			strsecname = ifl->ifl_isdesc[ndx]->is_name;
18587c478bd9Sstevel@tonic-gate 		else
18597c478bd9Sstevel@tonic-gate 			strsecname = MSG_ORIG(MSG_STR_EMPTY);
18607c478bd9Sstevel@tonic-gate 	} else {
18617c478bd9Sstevel@tonic-gate 		/*
18627c478bd9Sstevel@tonic-gate 		 * There is no string table section in this input file
18637c478bd9Sstevel@tonic-gate 		 * although there are symbols in this symbol table section.
18647c478bd9Sstevel@tonic-gate 		 * This means that these symbols do not have names.
18657c478bd9Sstevel@tonic-gate 		 * Currently, only scratch register symbols are allowed
18667c478bd9Sstevel@tonic-gate 		 * not to have names.
18677c478bd9Sstevel@tonic-gate 		 */
18687c478bd9Sstevel@tonic-gate 		strsize = 0;
18697c478bd9Sstevel@tonic-gate 		strs = (char *)MSG_ORIG(MSG_STR_EMPTY);
18707c478bd9Sstevel@tonic-gate 		strsecname = MSG_ORIG(MSG_STR_EMPTY);
18717c478bd9Sstevel@tonic-gate 	}
18727c478bd9Sstevel@tonic-gate 
18737c478bd9Sstevel@tonic-gate 	/*
18747c478bd9Sstevel@tonic-gate 	 * Determine the number of local symbols together with the total
18757c478bd9Sstevel@tonic-gate 	 * number we have to process.
18767c478bd9Sstevel@tonic-gate 	 */
18777c478bd9Sstevel@tonic-gate 	total = (Word)(shdr->sh_size / shdr->sh_entsize);
18787c478bd9Sstevel@tonic-gate 	local = shdr->sh_info;
18797c478bd9Sstevel@tonic-gate 
18807c478bd9Sstevel@tonic-gate 	/*
18817c478bd9Sstevel@tonic-gate 	 * Allocate a symbol table index array and a local symbol array
18827c478bd9Sstevel@tonic-gate 	 * (global symbols are processed and added to the ofl->ofl_symbkt[]
18837c478bd9Sstevel@tonic-gate 	 * array).  If we are dealing with a relocatable object, allocate the
18847c478bd9Sstevel@tonic-gate 	 * local symbol descriptors.  If this isn't a relocatable object we
18857c478bd9Sstevel@tonic-gate 	 * still have to process any shared object locals to determine if any
18867c478bd9Sstevel@tonic-gate 	 * register symbols exist.  Although these aren't added to the output
18877c478bd9Sstevel@tonic-gate 	 * image, they are used as part of symbol resolution.
18887c478bd9Sstevel@tonic-gate 	 */
18897c478bd9Sstevel@tonic-gate 	if ((ifl->ifl_oldndx = libld_malloc((size_t)(total *
18906b3ba5bdSAli Bahrami 	    sizeof (Sym_desc *)))) == NULL)
18917c478bd9Sstevel@tonic-gate 		return (S_ERROR);
1892d579eb63Sab 	etype_rel = (etype == ET_REL);
189375e45495Sab 	if (etype_rel && local) {
18947c478bd9Sstevel@tonic-gate 		if ((ifl->ifl_locs =
18956b3ba5bdSAli Bahrami 		    libld_calloc(sizeof (Sym_desc), local)) == NULL)
18967c478bd9Sstevel@tonic-gate 			return (S_ERROR);
18977c478bd9Sstevel@tonic-gate 		/* LINTED */
18987c478bd9Sstevel@tonic-gate 		ifl->ifl_locscnt = (Word)local;
18997c478bd9Sstevel@tonic-gate 	}
19007c478bd9Sstevel@tonic-gate 	ifl->ifl_symscnt = total;
19017c478bd9Sstevel@tonic-gate 
19027c478bd9Sstevel@tonic-gate 	/*
19037c478bd9Sstevel@tonic-gate 	 * If there are local symbols to save add them to the symbol table
19047c478bd9Sstevel@tonic-gate 	 * index array.
19057c478bd9Sstevel@tonic-gate 	 */
19067c478bd9Sstevel@tonic-gate 	if (local) {
190724b9abbaSab 		int		allow_ldynsym = OFL_ALLOW_LDYNSYM(ofl);
190824b9abbaSab 		Sym_desc	*last_file_sdp = NULL;
190924b9abbaSab 		int		last_file_ndx = 0;
191024b9abbaSab 
19117c478bd9Sstevel@tonic-gate 		for (sym++, ndx = 1; ndx < local; sym++, ndx++) {
19127c478bd9Sstevel@tonic-gate 			Word		shndx, sdflags = FLG_SY_CLEAN;
19137c478bd9Sstevel@tonic-gate 			const char	*name;
19147c478bd9Sstevel@tonic-gate 			Sym_desc	*rsdp;
1915ba2be530Sab 			int		shndx_bad = 0;
19161da7e599SAli Bahrami 			int		symtab_enter = 1;
19177c478bd9Sstevel@tonic-gate 
19187c478bd9Sstevel@tonic-gate 			/*
1919ba2be530Sab 			 * Determine and validate the associated section index.
19207c478bd9Sstevel@tonic-gate 			 */
1921ba2be530Sab 			if (symshndx && (sym->st_shndx == SHN_XINDEX)) {
19227c478bd9Sstevel@tonic-gate 				shndx = symshndx[ndx];
1923ba2be530Sab 			} else if ((shndx = sym->st_shndx) >= SHN_LORESERVE) {
19247c478bd9Sstevel@tonic-gate 				sdflags |= FLG_SY_SPECSEC;
1925ba2be530Sab 			} else if (shndx > ifl->ifl_ehdr->e_shnum) {
1926ba2be530Sab 				/* We need the name before we can issue error */
1927ba2be530Sab 				shndx_bad = 1;
1928ba2be530Sab 			}
19297c478bd9Sstevel@tonic-gate 
19307c478bd9Sstevel@tonic-gate 			/*
19317c478bd9Sstevel@tonic-gate 			 * Check if st_name has a valid value or not.
19327c478bd9Sstevel@tonic-gate 			 */
19335aefb655Srie 			if ((name = string(ofl, ifl, sym, strs, strsize, ndx,
19346b3ba5bdSAli Bahrami 			    shndx, symsecname, strsecname, &sdflags)) == NULL) {
19357c478bd9Sstevel@tonic-gate 				ofl->ofl_flags |= FLG_OF_FATAL;
19367c478bd9Sstevel@tonic-gate 				continue;
19377c478bd9Sstevel@tonic-gate 			}
19387c478bd9Sstevel@tonic-gate 
1939ba2be530Sab 			/*
1940ba2be530Sab 			 * Now that we have the name, if the section index
1941ba2be530Sab 			 * was bad, report it.
1942ba2be530Sab 			 */
1943ba2be530Sab 			if (shndx_bad) {
1944ba2be530Sab 				eprintf(ofl->ofl_lml, ERR_WARNING,
1945ba2be530Sab 				    MSG_INTL(MSG_SYM_INVSHNDX),
1946ba2be530Sab 				    demangle_symname(name, isc->is_name, ndx),
1947ba2be530Sab 				    ifl->ifl_name,
1948ba2be530Sab 				    conv_sym_shndx(sym->st_shndx, &inv_buf));
1949ba2be530Sab 				continue;
1950ba2be530Sab 			}
1951ba2be530Sab 
19527c478bd9Sstevel@tonic-gate 			/*
19537c478bd9Sstevel@tonic-gate 			 * If this local symbol table originates from a shared
19547c478bd9Sstevel@tonic-gate 			 * object, then we're only interested in recording
19557c478bd9Sstevel@tonic-gate 			 * register symbols.  As local symbol descriptors aren't
19567c478bd9Sstevel@tonic-gate 			 * allocated for shared objects, one will be allocated
19577c478bd9Sstevel@tonic-gate 			 * to associated with the register symbol.  This symbol
19587c478bd9Sstevel@tonic-gate 			 * won't become part of the output image, but we must
19597c478bd9Sstevel@tonic-gate 			 * process it to test for register conflicts.
19607c478bd9Sstevel@tonic-gate 			 */
19617c478bd9Sstevel@tonic-gate 			rsdp = sdp = 0;
19627c478bd9Sstevel@tonic-gate 			if (sdflags & FLG_SY_REGSYM) {
1963ba2be530Sab 				/*
1964ba2be530Sab 				 * The presence of FLG_SY_REGSYM means that
1965ba2be530Sab 				 * the pointers in ld_targ.t_ms are non-NULL.
1966ba2be530Sab 				 */
1967ba2be530Sab 				rsdp = (*ld_targ.t_ms.ms_reg_find)(sym, ofl);
1968ba2be530Sab 				if (rsdp != 0) {
19697c478bd9Sstevel@tonic-gate 					/*
19707c478bd9Sstevel@tonic-gate 					 * The fact that another register def-
19717c478bd9Sstevel@tonic-gate 					 * inition has been found is fatal.
19727c478bd9Sstevel@tonic-gate 					 * Call the verification routine to get
19737c478bd9Sstevel@tonic-gate 					 * the error message and move on.
19747c478bd9Sstevel@tonic-gate 					 */
1975ba2be530Sab 					(void) (*ld_targ.t_ms.ms_reg_check)
1976ba2be530Sab 					    (rsdp, sym, name, ifl, ofl);
19777c478bd9Sstevel@tonic-gate 					continue;
19787c478bd9Sstevel@tonic-gate 				}
19797c478bd9Sstevel@tonic-gate 
19807c478bd9Sstevel@tonic-gate 				if (etype == ET_DYN) {
19817c478bd9Sstevel@tonic-gate 					if ((sdp = libld_calloc(
19826b3ba5bdSAli Bahrami 					    sizeof (Sym_desc), 1)) == NULL)
19837c478bd9Sstevel@tonic-gate 						return (S_ERROR);
19847c478bd9Sstevel@tonic-gate 					sdp->sd_ref = REF_DYN_SEEN;
1985ca4eed8bSAli Bahrami 
1986ca4eed8bSAli Bahrami 					/* Will not appear in output object */
19871da7e599SAli Bahrami 					symtab_enter = 0;
19887c478bd9Sstevel@tonic-gate 				}
19897c478bd9Sstevel@tonic-gate 			} else if (etype == ET_DYN)
19907c478bd9Sstevel@tonic-gate 				continue;
19917c478bd9Sstevel@tonic-gate 
19927c478bd9Sstevel@tonic-gate 			/*
19937c478bd9Sstevel@tonic-gate 			 * Fill in the remaining symbol descriptor information.
19947c478bd9Sstevel@tonic-gate 			 */
19956b3ba5bdSAli Bahrami 			if (sdp == NULL) {
19967c478bd9Sstevel@tonic-gate 				sdp = &(ifl->ifl_locs[ndx]);
19977c478bd9Sstevel@tonic-gate 				sdp->sd_ref = REF_REL_NEED;
19987c478bd9Sstevel@tonic-gate 			}
19996b3ba5bdSAli Bahrami 			if (rsdp == NULL) {
20007c478bd9Sstevel@tonic-gate 				sdp->sd_name = name;
20017c478bd9Sstevel@tonic-gate 				sdp->sd_sym = sym;
20027c478bd9Sstevel@tonic-gate 				sdp->sd_shndx = shndx;
20037c478bd9Sstevel@tonic-gate 				sdp->sd_flags = sdflags;
20047c478bd9Sstevel@tonic-gate 				sdp->sd_file = ifl;
20057c478bd9Sstevel@tonic-gate 				ifl->ifl_oldndx[ndx] = sdp;
20067c478bd9Sstevel@tonic-gate 			}
20077c478bd9Sstevel@tonic-gate 
20085aefb655Srie 			DBG_CALL(Dbg_syms_entry(ofl->ofl_lml, ndx, sdp));
20097c478bd9Sstevel@tonic-gate 
20107c478bd9Sstevel@tonic-gate 			/*
20117c478bd9Sstevel@tonic-gate 			 * Reclassify any SHN_SUNW_IGNORE symbols to SHN_UNDEF
20127c478bd9Sstevel@tonic-gate 			 * so as to simplify future processing.
20137c478bd9Sstevel@tonic-gate 			 */
20140bc07c75Srie 			if (sym->st_shndx == SHN_SUNW_IGNORE) {
20157c478bd9Sstevel@tonic-gate 				sdp->sd_shndx = shndx = SHN_UNDEF;
20167c478bd9Sstevel@tonic-gate 				sdp->sd_flags1 |=
20177c478bd9Sstevel@tonic-gate 				    (FLG_SY1_IGNORE | FLG_SY1_ELIM);
20187c478bd9Sstevel@tonic-gate 			}
20197c478bd9Sstevel@tonic-gate 
20207c478bd9Sstevel@tonic-gate 			/*
20217c478bd9Sstevel@tonic-gate 			 * Process any register symbols.
20227c478bd9Sstevel@tonic-gate 			 */
20237c478bd9Sstevel@tonic-gate 			if (sdp->sd_flags & FLG_SY_REGSYM) {
20247c478bd9Sstevel@tonic-gate 				/*
20257c478bd9Sstevel@tonic-gate 				 * Add a diagnostic to indicate we've caught a
20267c478bd9Sstevel@tonic-gate 				 * register symbol, as this can be useful if a
20277c478bd9Sstevel@tonic-gate 				 * register conflict is later discovered.
20287c478bd9Sstevel@tonic-gate 				 */
20295aefb655Srie 				DBG_CALL(Dbg_syms_entered(ofl, sym, sdp));
20307c478bd9Sstevel@tonic-gate 
20317c478bd9Sstevel@tonic-gate 				/*
20327c478bd9Sstevel@tonic-gate 				 * If this register symbol hasn't already been
20337c478bd9Sstevel@tonic-gate 				 * recorded, enter it now.
2034ba2be530Sab 				 *
2035ba2be530Sab 				 * The presence of FLG_SY_REGSYM means that
2036ba2be530Sab 				 * the pointers in ld_targ.t_ms are non-NULL.
20377c478bd9Sstevel@tonic-gate 				 */
20386b3ba5bdSAli Bahrami 				if ((rsdp == NULL) &&
2039ba2be530Sab 				    ((*ld_targ.t_ms.ms_reg_enter)(sdp, ofl) ==
2040ba2be530Sab 				    0))
20417c478bd9Sstevel@tonic-gate 					return (S_ERROR);
20427c478bd9Sstevel@tonic-gate 			}
20437c478bd9Sstevel@tonic-gate 
20447c478bd9Sstevel@tonic-gate 			/*
20457c478bd9Sstevel@tonic-gate 			 * Assign an input section.
20467c478bd9Sstevel@tonic-gate 			 */
20470bc07c75Srie 			if ((sym->st_shndx != SHN_UNDEF) &&
20487c478bd9Sstevel@tonic-gate 			    ((sdp->sd_flags & FLG_SY_SPECSEC) == 0))
20497c478bd9Sstevel@tonic-gate 				sdp->sd_isc = ifl->ifl_isdesc[shndx];
20507c478bd9Sstevel@tonic-gate 
20517c478bd9Sstevel@tonic-gate 			/*
20527c478bd9Sstevel@tonic-gate 			 * If this symbol falls within the range of a section
20537c478bd9Sstevel@tonic-gate 			 * being discarded, then discard the symbol itself.
20547c478bd9Sstevel@tonic-gate 			 * There is no reason to keep this local symbol.
20557c478bd9Sstevel@tonic-gate 			 */
20567c478bd9Sstevel@tonic-gate 			if (sdp->sd_isc &&
20577c478bd9Sstevel@tonic-gate 			    (sdp->sd_isc->is_flags & FLG_IS_DISCARD)) {
20587c478bd9Sstevel@tonic-gate 				sdp->sd_flags |= FLG_SY_ISDISC;
2059a194faf8Srie 				DBG_CALL(Dbg_syms_discarded(ofl->ofl_lml, sdp));
20607c478bd9Sstevel@tonic-gate 				continue;
20617c478bd9Sstevel@tonic-gate 			}
20627c478bd9Sstevel@tonic-gate 
20637c478bd9Sstevel@tonic-gate 			/*
20647c478bd9Sstevel@tonic-gate 			 * Skip any section symbols as new versions of these
20657c478bd9Sstevel@tonic-gate 			 * will be created.
20667c478bd9Sstevel@tonic-gate 			 */
20677c478bd9Sstevel@tonic-gate 			if ((type = ELF_ST_TYPE(sym->st_info)) == STT_SECTION) {
20680bc07c75Srie 				if (sym->st_shndx == SHN_UNDEF) {
20695aefb655Srie 					eprintf(ofl->ofl_lml, ERR_WARNING,
20707c478bd9Sstevel@tonic-gate 					    MSG_INTL(MSG_SYM_INVSHNDX),
2071ba2be530Sab 					    demangle_symname(name, isc->is_name,
2072ba2be530Sab 					    ndx), ifl->ifl_name,
2073de777a60Sab 					    conv_sym_shndx(sym->st_shndx,
2074de777a60Sab 					    &inv_buf));
20757c478bd9Sstevel@tonic-gate 				}
20767c478bd9Sstevel@tonic-gate 				continue;
20777c478bd9Sstevel@tonic-gate 			}
20787c478bd9Sstevel@tonic-gate 
207975e45495Sab 			/*
208075e45495Sab 			 * For a relocatable object, if this symbol is defined
208175e45495Sab 			 * and has non-zero length and references an address
208275e45495Sab 			 * within an associated section, then check its extents
208375e45495Sab 			 * to make sure the section boundaries encompass it.
208475e45495Sab 			 * If they don't, the ELF file is corrupt.
208575e45495Sab 			 */
208624b9abbaSab 			if (etype_rel) {
208724b9abbaSab 				if (SYM_LOC_BADADDR(sdp, sym, type)) {
208824b9abbaSab 					issue_badaddr_msg(ifl, ofl, sdp,
208924b9abbaSab 					    sym, shndx);
2090ca4eed8bSAli Bahrami 					if (ofl->ofl_flags & FLG_OF_FATAL)
2091ca4eed8bSAli Bahrami 						continue;
209224b9abbaSab 				}
209324b9abbaSab 
209424b9abbaSab 				/*
209524b9abbaSab 				 * We have observed relocatable objects
209624b9abbaSab 				 * containing identical adjacent STT_FILE
209724b9abbaSab 				 * symbols. Discard any other than the first,
209824b9abbaSab 				 * as they are all equivalent and the extras
209924b9abbaSab 				 * do not add information.
210024b9abbaSab 				 *
210124b9abbaSab 				 * For the purpose of this test, we assume
210224b9abbaSab 				 * that only the symbol type and the string
210324b9abbaSab 				 * table offset (st_name) matter.
210424b9abbaSab 				 */
210524b9abbaSab 				if (type == STT_FILE) {
210624b9abbaSab 					int toss = (last_file_sdp != NULL) &&
210724b9abbaSab 					    ((ndx - 1) == last_file_ndx) &&
210824b9abbaSab 					    (sym->st_name ==
210924b9abbaSab 					    last_file_sdp->sd_sym->st_name);
211024b9abbaSab 
211124b9abbaSab 					last_file_sdp = sdp;
211224b9abbaSab 					last_file_ndx = ndx;
211324b9abbaSab 					if (toss) {
211424b9abbaSab 						sdp->sd_flags |= FLG_SY_INVALID;
211524b9abbaSab 						DBG_CALL(Dbg_syms_dup_discarded(
211624b9abbaSab 						    ofl->ofl_lml, ndx, sdp));
211724b9abbaSab 						continue;
211824b9abbaSab 					}
211924b9abbaSab 				}
212075e45495Sab 			}
212175e45495Sab 
212224b9abbaSab 
21237c478bd9Sstevel@tonic-gate 			/*
21247c478bd9Sstevel@tonic-gate 			 * Sanity check for TLS
21257c478bd9Sstevel@tonic-gate 			 */
21260bc07c75Srie 			if ((sym->st_size != 0) && ((type == STT_TLS) &&
21270bc07c75Srie 			    (sym->st_shndx != SHN_COMMON))) {
21287c478bd9Sstevel@tonic-gate 				Is_desc	*isp = sdp->sd_isc;
21297c478bd9Sstevel@tonic-gate 
21306b3ba5bdSAli Bahrami 				if ((isp == NULL) || (isp->is_shdr == NULL) ||
21317c478bd9Sstevel@tonic-gate 				    ((isp->is_shdr->sh_flags & SHF_TLS) == 0)) {
21325aefb655Srie 					eprintf(ofl->ofl_lml, ERR_FATAL,
21337c478bd9Sstevel@tonic-gate 					    MSG_INTL(MSG_SYM_TLS),
21347c478bd9Sstevel@tonic-gate 					    demangle(sdp->sd_name),
21357c478bd9Sstevel@tonic-gate 					    ifl->ifl_name);
21367c478bd9Sstevel@tonic-gate 					ofl->ofl_flags |= FLG_OF_FATAL;
21377c478bd9Sstevel@tonic-gate 					continue;
21387c478bd9Sstevel@tonic-gate 				}
21397c478bd9Sstevel@tonic-gate 			}
21407c478bd9Sstevel@tonic-gate 
21417c478bd9Sstevel@tonic-gate 			/*
21427c478bd9Sstevel@tonic-gate 			 * Carry our some basic sanity checks (these are just
21437c478bd9Sstevel@tonic-gate 			 * some of the erroneous symbol entries we've come
21447c478bd9Sstevel@tonic-gate 			 * across, there's probably a lot more).  The symbol
21457c478bd9Sstevel@tonic-gate 			 * will not be carried forward to the output file, which
21467c478bd9Sstevel@tonic-gate 			 * won't be a problem unless a relocation is required
21477c478bd9Sstevel@tonic-gate 			 * against it.
21487c478bd9Sstevel@tonic-gate 			 */
21497c478bd9Sstevel@tonic-gate 			if (((sdp->sd_flags & FLG_SY_SPECSEC) &&
21500bc07c75Srie 			    ((sym->st_shndx == SHN_COMMON)) ||
21510bc07c75Srie 			    ((type == STT_FILE) &&
21520bc07c75Srie 			    (sym->st_shndx != SHN_ABS))) ||
21536b3ba5bdSAli Bahrami 			    (sdp->sd_isc && (sdp->sd_isc->is_osdesc == NULL))) {
21545aefb655Srie 				eprintf(ofl->ofl_lml, ERR_WARNING,
21555aefb655Srie 				    MSG_INTL(MSG_SYM_INVSHNDX),
2156ba2be530Sab 				    demangle_symname(name, isc->is_name, ndx),
2157ba2be530Sab 				    ifl->ifl_name,
2158de777a60Sab 				    conv_sym_shndx(sym->st_shndx, &inv_buf));
21597c478bd9Sstevel@tonic-gate 				sdp->sd_isc = NULL;
21607c478bd9Sstevel@tonic-gate 				sdp->sd_flags |= FLG_SY_INVALID;
21617c478bd9Sstevel@tonic-gate 				continue;
21627c478bd9Sstevel@tonic-gate 			}
21637c478bd9Sstevel@tonic-gate 
21647c478bd9Sstevel@tonic-gate 			/*
21657c478bd9Sstevel@tonic-gate 			 * As these local symbols will become part of the output
21667c478bd9Sstevel@tonic-gate 			 * image, record their number and name string size.
21677c478bd9Sstevel@tonic-gate 			 * Globals are counted after all input file processing
21687c478bd9Sstevel@tonic-gate 			 * (and hence symbol resolution) is complete during
21697c478bd9Sstevel@tonic-gate 			 * sym_validate().
21707c478bd9Sstevel@tonic-gate 			 */
21711da7e599SAli Bahrami 			if (!(ofl->ofl_flags & FLG_OF_REDLSYM) &&
21721da7e599SAli Bahrami 			    symtab_enter) {
21737c478bd9Sstevel@tonic-gate 				ofl->ofl_locscnt++;
21747c478bd9Sstevel@tonic-gate 
21757c478bd9Sstevel@tonic-gate 				if ((((sdp->sd_flags & FLG_SY_REGSYM) == 0) ||
21767c478bd9Sstevel@tonic-gate 				    sym->st_name) && (st_insert(ofl->ofl_strtab,
21777c478bd9Sstevel@tonic-gate 				    sdp->sd_name) == -1))
21787c478bd9Sstevel@tonic-gate 					return (S_ERROR);
21799039eeafSab 
21809039eeafSab 				if (allow_ldynsym && sym->st_name &&
2181d579eb63Sab 				    ldynsym_symtype[type]) {
21829039eeafSab 					ofl->ofl_dynlocscnt++;
21839039eeafSab 					if (st_insert(ofl->ofl_dynstrtab,
21849039eeafSab 					    sdp->sd_name) == -1)
21859039eeafSab 						return (S_ERROR);
2186d579eb63Sab 					/* Include it in sort section? */
2187d579eb63Sab 					DYNSORT_COUNT(sdp, sym, type, ++);
21889039eeafSab 				}
21897c478bd9Sstevel@tonic-gate 			}
21907c478bd9Sstevel@tonic-gate 		}
21917c478bd9Sstevel@tonic-gate 	}
21927c478bd9Sstevel@tonic-gate 
2193d840867fSab 	/*
2194d840867fSab 	 * The GNU ld interprets the top bit of the 16-bit Versym value
2195d840867fSab 	 * (0x8000) as the "hidden" bit. If this bit is set, the linker
2196d840867fSab 	 * is supposed to act as if that symbol does not exist. The Solaris
2197d840867fSab 	 * linker does not support this mechanism, or the model of interface
2198d840867fSab 	 * evolution that it allows, but we honor it in GNU ld produced
2199d840867fSab 	 * objects in order to interoperate with them.
2200d840867fSab 	 *
2201d840867fSab 	 * Determine if we should honor the GNU hidden bit for this file.
2202d840867fSab 	 */
2203d840867fSab 	test_gnu_hidden_bit = ((ifl->ifl_flags & FLG_IF_GNUVER) != 0) &&
2204d840867fSab 	    (ifl->ifl_versym != NULL);
2205d840867fSab 
22067c478bd9Sstevel@tonic-gate 	/*
22077c478bd9Sstevel@tonic-gate 	 * Now scan the global symbols entering them in the internal symbol
22087c478bd9Sstevel@tonic-gate 	 * table or resolving them as necessary.
22097c478bd9Sstevel@tonic-gate 	 */
22107c478bd9Sstevel@tonic-gate 	sym = (Sym *)isc->is_indata->d_buf;
22117c478bd9Sstevel@tonic-gate 	sym += local;
22126b3ba5bdSAli Bahrami 	weak = 0;
22137c478bd9Sstevel@tonic-gate 	/* LINTED */
22147c478bd9Sstevel@tonic-gate 	for (ndx = (int)local; ndx < total; sym++, ndx++) {
22157c478bd9Sstevel@tonic-gate 		const char	*name;
22167c478bd9Sstevel@tonic-gate 		Word		shndx, sdflags = 0;
2217ba2be530Sab 		int		shndx_bad = 0;
22187c478bd9Sstevel@tonic-gate 
22197c478bd9Sstevel@tonic-gate 		/*
2220ba2be530Sab 		 * Determine and validate the associated section index.
22217c478bd9Sstevel@tonic-gate 		 */
22227c478bd9Sstevel@tonic-gate 		if (symshndx && (sym->st_shndx == SHN_XINDEX)) {
22237c478bd9Sstevel@tonic-gate 			shndx = symshndx[ndx];
2224ba2be530Sab 		} else if ((shndx = sym->st_shndx) >= SHN_LORESERVE) {
2225ba2be530Sab 			sdflags |= FLG_SY_SPECSEC;
2226ba2be530Sab 		} else if (shndx > ifl->ifl_ehdr->e_shnum) {
2227ba2be530Sab 			/* We need the name before we can issue error */
2228ba2be530Sab 			shndx_bad = 1;
22297c478bd9Sstevel@tonic-gate 		}
22307c478bd9Sstevel@tonic-gate 
22317c478bd9Sstevel@tonic-gate 		/*
22327c478bd9Sstevel@tonic-gate 		 * Check if st_name has a valid value or not.
22337c478bd9Sstevel@tonic-gate 		 */
22345aefb655Srie 		if ((name = string(ofl, ifl, sym, strs, strsize, ndx, shndx,
22356b3ba5bdSAli Bahrami 		    symsecname, strsecname, &sdflags)) == NULL) {
22367c478bd9Sstevel@tonic-gate 			ofl->ofl_flags |= FLG_OF_FATAL;
22377c478bd9Sstevel@tonic-gate 			continue;
22387c478bd9Sstevel@tonic-gate 		}
22397c478bd9Sstevel@tonic-gate 
2240ba2be530Sab 		/*
2241ba2be530Sab 		 * Now that we have the name, if the section index
2242ba2be530Sab 		 * was bad, report it.
2243ba2be530Sab 		 */
2244ba2be530Sab 		if (shndx_bad) {
2245ba2be530Sab 			eprintf(ofl->ofl_lml, ERR_WARNING,
2246ba2be530Sab 			    MSG_INTL(MSG_SYM_INVSHNDX),
2247ba2be530Sab 			    demangle_symname(name, isc->is_name, ndx),
2248ba2be530Sab 			    ifl->ifl_name,
2249ba2be530Sab 			    conv_sym_shndx(sym->st_shndx, &inv_buf));
2250ba2be530Sab 			continue;
2251ba2be530Sab 		}
2252ba2be530Sab 
2253ba2be530Sab 
22543b41b08bSab 		/*
2255d840867fSab 		 * Test for the GNU hidden bit, and ignore symbols that
2256d840867fSab 		 * have it set.
22573b41b08bSab 		 */
2258d840867fSab 		if (test_gnu_hidden_bit &&
2259d840867fSab 		    ((ifl->ifl_versym[ndx] & 0x8000) != 0))
22603b41b08bSab 			continue;
22613b41b08bSab 
22627c478bd9Sstevel@tonic-gate 		/*
22637c478bd9Sstevel@tonic-gate 		 * The linker itself will generate symbols for _end, _etext,
22647c478bd9Sstevel@tonic-gate 		 * _edata, _DYNAMIC and _PROCEDURE_LINKAGE_TABLE_, so don't
22657c478bd9Sstevel@tonic-gate 		 * bother entering these symbols from shared objects.  This
22667c478bd9Sstevel@tonic-gate 		 * results in some wasted resolution processing, which is hard
22677c478bd9Sstevel@tonic-gate 		 * to feel, but if nothing else, pollutes diagnostic relocation
22687c478bd9Sstevel@tonic-gate 		 * output.
22697c478bd9Sstevel@tonic-gate 		 */
22707c478bd9Sstevel@tonic-gate 		if (name[0] && (etype == ET_DYN) && (sym->st_size == 0) &&
22717c478bd9Sstevel@tonic-gate 		    (ELF_ST_TYPE(sym->st_info) == STT_OBJECT) &&
22727c478bd9Sstevel@tonic-gate 		    (name[0] == '_') && ((name[1] == 'e') ||
22737c478bd9Sstevel@tonic-gate 		    (name[1] == 'D') || (name[1] == 'P')) &&
22747c478bd9Sstevel@tonic-gate 		    ((strcmp(name, MSG_ORIG(MSG_SYM_ETEXT_U)) == 0) ||
22757c478bd9Sstevel@tonic-gate 		    (strcmp(name, MSG_ORIG(MSG_SYM_EDATA_U)) == 0) ||
22767c478bd9Sstevel@tonic-gate 		    (strcmp(name, MSG_ORIG(MSG_SYM_END_U)) == 0) ||
22777c478bd9Sstevel@tonic-gate 		    (strcmp(name, MSG_ORIG(MSG_SYM_DYNAMIC_U)) == 0) ||
22787c478bd9Sstevel@tonic-gate 		    (strcmp(name, MSG_ORIG(MSG_SYM_PLKTBL_U)) == 0))) {
22797c478bd9Sstevel@tonic-gate 			ifl->ifl_oldndx[ndx] = 0;
22807c478bd9Sstevel@tonic-gate 			continue;
22817c478bd9Sstevel@tonic-gate 		}
22827c478bd9Sstevel@tonic-gate 
22837c478bd9Sstevel@tonic-gate 		/*
22847c478bd9Sstevel@tonic-gate 		 * Determine and validate the symbols binding.
22857c478bd9Sstevel@tonic-gate 		 */
22867c478bd9Sstevel@tonic-gate 		bind = ELF_ST_BIND(sym->st_info);
22877c478bd9Sstevel@tonic-gate 		if ((bind != STB_GLOBAL) && (bind != STB_WEAK)) {
22885aefb655Srie 			eprintf(ofl->ofl_lml, ERR_WARNING,
2289ba2be530Sab 			    MSG_INTL(MSG_SYM_NONGLOB),
2290ba2be530Sab 			    demangle_symname(name, isc->is_name, ndx),
2291de777a60Sab 			    ifl->ifl_name,
2292de777a60Sab 			    conv_sym_info_bind(bind, 0, &inv_buf));
22937c478bd9Sstevel@tonic-gate 			continue;
22947c478bd9Sstevel@tonic-gate 		}
22956b3ba5bdSAli Bahrami 		if (bind == STB_WEAK)
22966b3ba5bdSAli Bahrami 			weak++;
22977c478bd9Sstevel@tonic-gate 
22987c478bd9Sstevel@tonic-gate 		/*
22997c478bd9Sstevel@tonic-gate 		 * If this symbol falls within the range of a section being
23007c478bd9Sstevel@tonic-gate 		 * discarded, then discard the symbol itself.
23017c478bd9Sstevel@tonic-gate 		 */
23027c478bd9Sstevel@tonic-gate 		if (((sdflags & FLG_SY_SPECSEC) == 0) &&
23030bc07c75Srie 		    (sym->st_shndx != SHN_UNDEF)) {
23047c478bd9Sstevel@tonic-gate 			Is_desc	*isp;
23057c478bd9Sstevel@tonic-gate 
23067c478bd9Sstevel@tonic-gate 			if (shndx >= ifl->ifl_shnum) {
23077c478bd9Sstevel@tonic-gate 				/*
23087c478bd9Sstevel@tonic-gate 				 * Carry our some basic sanity checks
23097c478bd9Sstevel@tonic-gate 				 * The symbol will not be carried forward to
23107c478bd9Sstevel@tonic-gate 				 * the output file, which won't be a problem
23117c478bd9Sstevel@tonic-gate 				 * unless a relocation is required against it.
23127c478bd9Sstevel@tonic-gate 				 */
23135aefb655Srie 				eprintf(ofl->ofl_lml, ERR_WARNING,
2314ba2be530Sab 				    MSG_INTL(MSG_SYM_INVSHNDX),
2315ba2be530Sab 				    demangle_symname(name, isc->is_name, ndx),
23160bc07c75Srie 				    ifl->ifl_name,
2317de777a60Sab 				    conv_sym_shndx(sym->st_shndx, &inv_buf));
23187c478bd9Sstevel@tonic-gate 				continue;
23197c478bd9Sstevel@tonic-gate 			}
23207c478bd9Sstevel@tonic-gate 
23217c478bd9Sstevel@tonic-gate 			isp = ifl->ifl_isdesc[shndx];
23227c478bd9Sstevel@tonic-gate 			if (isp && (isp->is_flags & FLG_IS_DISCARD)) {
23237c478bd9Sstevel@tonic-gate 				if ((sdp =
23246b3ba5bdSAli Bahrami 				    libld_calloc(sizeof (Sym_desc), 1)) == NULL)
23257c478bd9Sstevel@tonic-gate 					return (S_ERROR);
23267c478bd9Sstevel@tonic-gate 
23277c478bd9Sstevel@tonic-gate 				/*
23287c478bd9Sstevel@tonic-gate 				 * Create a dummy symbol entry so that if we
23297c478bd9Sstevel@tonic-gate 				 * find any references to this discarded symbol
23307c478bd9Sstevel@tonic-gate 				 * we can compensate.
23317c478bd9Sstevel@tonic-gate 				 */
23327c478bd9Sstevel@tonic-gate 				sdp->sd_name = name;
23337c478bd9Sstevel@tonic-gate 				sdp->sd_sym = sym;
23347c478bd9Sstevel@tonic-gate 				sdp->sd_file = ifl;
23357c478bd9Sstevel@tonic-gate 				sdp->sd_isc = isp;
23367c478bd9Sstevel@tonic-gate 				sdp->sd_flags = FLG_SY_ISDISC;
23377c478bd9Sstevel@tonic-gate 				ifl->ifl_oldndx[ndx] = sdp;
23387c478bd9Sstevel@tonic-gate 
2339a194faf8Srie 				DBG_CALL(Dbg_syms_discarded(ofl->ofl_lml, sdp));
23407c478bd9Sstevel@tonic-gate 				continue;
23417c478bd9Sstevel@tonic-gate 			}
23427c478bd9Sstevel@tonic-gate 		}
23437c478bd9Sstevel@tonic-gate 
23447c478bd9Sstevel@tonic-gate 		/*
23457c478bd9Sstevel@tonic-gate 		 * If the symbol does not already exist in the internal symbol
23467c478bd9Sstevel@tonic-gate 		 * table add it, otherwise resolve the conflict.  If the symbol
23477c478bd9Sstevel@tonic-gate 		 * from this file is kept, retain its symbol table index for
23487c478bd9Sstevel@tonic-gate 		 * possible use in associating a global alias.
23497c478bd9Sstevel@tonic-gate 		 */
23507c478bd9Sstevel@tonic-gate 		/* LINTED */
23517c478bd9Sstevel@tonic-gate 		hash = (Word)elf_hash((const char *)name);
23525aefb655Srie 		if ((sdp = ld_sym_find(name, hash, &where, ofl)) == NULL) {
23535aefb655Srie 			DBG_CALL(Dbg_syms_global(ofl->ofl_lml, ndx, name));
23545aefb655Srie 			if ((sdp = ld_sym_enter(name, sym, hash, ifl, ofl, ndx,
23557c478bd9Sstevel@tonic-gate 			    shndx, sdflags, 0, &where)) == (Sym_desc *)S_ERROR)
23567c478bd9Sstevel@tonic-gate 				return (S_ERROR);
23577c478bd9Sstevel@tonic-gate 
23585aefb655Srie 		} else if (ld_sym_resolve(sdp, sym, ifl, ofl, ndx, shndx,
23597c478bd9Sstevel@tonic-gate 		    sdflags) == S_ERROR)
23607c478bd9Sstevel@tonic-gate 			return (S_ERROR);
23617c478bd9Sstevel@tonic-gate 
23627c478bd9Sstevel@tonic-gate 		/*
23637c478bd9Sstevel@tonic-gate 		 * After we've compared a defined symbol in one shared
23647c478bd9Sstevel@tonic-gate 		 * object, flag the symbol so we don't compare it again.
23657c478bd9Sstevel@tonic-gate 		 */
23660bc07c75Srie 		if ((etype == ET_DYN) && (sym->st_shndx != SHN_UNDEF) &&
23677c478bd9Sstevel@tonic-gate 		    ((sdp->sd_flags & FLG_SY_SOFOUND) == 0))
23687c478bd9Sstevel@tonic-gate 			sdp->sd_flags |= FLG_SY_SOFOUND;
23697c478bd9Sstevel@tonic-gate 
23707c478bd9Sstevel@tonic-gate 		/*
23717c478bd9Sstevel@tonic-gate 		 * If the symbol is accepted from this file retain the symbol
23727c478bd9Sstevel@tonic-gate 		 * index for possible use in aliasing.
23737c478bd9Sstevel@tonic-gate 		 */
23747c478bd9Sstevel@tonic-gate 		if (sdp->sd_file == ifl)
23757c478bd9Sstevel@tonic-gate 			sdp->sd_symndx = ndx;
23767c478bd9Sstevel@tonic-gate 
23777c478bd9Sstevel@tonic-gate 		ifl->ifl_oldndx[ndx] = sdp;
23787c478bd9Sstevel@tonic-gate 
23797c478bd9Sstevel@tonic-gate 		/*
23807c478bd9Sstevel@tonic-gate 		 * If we've accepted a register symbol, continue to validate
23817c478bd9Sstevel@tonic-gate 		 * it.
23827c478bd9Sstevel@tonic-gate 		 */
23837c478bd9Sstevel@tonic-gate 		if (sdp->sd_flags & FLG_SY_REGSYM) {
23847c478bd9Sstevel@tonic-gate 			Sym_desc	*rsdp;
23857c478bd9Sstevel@tonic-gate 
2386ba2be530Sab 			/*
2387ba2be530Sab 			 * The presence of FLG_SY_REGSYM means that
2388ba2be530Sab 			 * the pointers in ld_targ.t_ms are non-NULL.
2389ba2be530Sab 			 */
2390ba2be530Sab 			rsdp = (*ld_targ.t_ms.ms_reg_find)(sdp->sd_sym, ofl);
23916b3ba5bdSAli Bahrami 			if (rsdp == NULL) {
2392ba2be530Sab 				if ((*ld_targ.t_ms.ms_reg_enter)(sdp, ofl) == 0)
23937c478bd9Sstevel@tonic-gate 					return (S_ERROR);
23947c478bd9Sstevel@tonic-gate 			} else if (rsdp != sdp) {
2395ba2be530Sab 				(void) (*ld_targ.t_ms.ms_reg_check)(rsdp,
2396ba2be530Sab 				    sdp->sd_sym, sdp->sd_name, ifl, ofl);
23977c478bd9Sstevel@tonic-gate 			}
23987c478bd9Sstevel@tonic-gate 		}
239975e45495Sab 
240075e45495Sab 		/*
240175e45495Sab 		 * For a relocatable object, if this symbol is defined
240275e45495Sab 		 * and has non-zero length and references an address
240375e45495Sab 		 * within an associated section, then check its extents
240475e45495Sab 		 * to make sure the section boundaries encompass it.
240575e45495Sab 		 * If they don't, the ELF file is corrupt. Note that this
240675e45495Sab 		 * global symbol may have come from another file to satisfy
240775e45495Sab 		 * an UNDEF symbol of the same name from this one. In that
240875e45495Sab 		 * case, we don't check it, because it was already checked
240975e45495Sab 		 * as part of its own file.
241075e45495Sab 		 */
241175e45495Sab 		if (etype_rel && (sdp->sd_file == ifl)) {
241275e45495Sab 			Sym *tsym = sdp->sd_sym;
241375e45495Sab 
241475e45495Sab 			if (SYM_LOC_BADADDR(sdp, tsym,
241575e45495Sab 			    ELF_ST_TYPE(tsym->st_info))) {
241669a0bf0cSab 				issue_badaddr_msg(ifl, ofl, sdp,
241769a0bf0cSab 				    tsym, tsym->st_shndx);
241875e45495Sab 				continue;
241975e45495Sab 			}
242075e45495Sab 		}
24217c478bd9Sstevel@tonic-gate 	}
24227c478bd9Sstevel@tonic-gate 
24237c478bd9Sstevel@tonic-gate 	/*
24246b3ba5bdSAli Bahrami 	 * Associate weak (alias) symbols to their non-weak counterparts by
24256b3ba5bdSAli Bahrami 	 * scaning the global symbols one more time.
24266b3ba5bdSAli Bahrami 	 *
24276b3ba5bdSAli Bahrami 	 * This association is needed when processing the symbols from a shared
24286b3ba5bdSAli Bahrami 	 * object dependency when a a weak definition satisfies a reference:
24297c478bd9Sstevel@tonic-gate 	 *
24306b3ba5bdSAli Bahrami 	 *  -	When building a dynamic executable, if a referenced symbol is a
24316b3ba5bdSAli Bahrami 	 *	data item, the symbol data is copied to the executables address
24326b3ba5bdSAli Bahrami 	 *	space.  In this copy-relocation case, we must also reassociate
24336b3ba5bdSAli Bahrami 	 *	the alias symbol with its new location in the executable.
24347c478bd9Sstevel@tonic-gate 	 *
24356b3ba5bdSAli Bahrami 	 *  -	If the referenced symbol is a function then we may need to
24366b3ba5bdSAli Bahrami 	 *	promote the symbols binding from undefined weak to undefined,
24376b3ba5bdSAli Bahrami 	 *	otherwise the run-time linker will not generate the correct
24386b3ba5bdSAli Bahrami 	 *	relocation error should the symbol not be found.
24396b3ba5bdSAli Bahrami 	 *
24406b3ba5bdSAli Bahrami 	 * Weak alias association is also required when a local dynsym table
24416b3ba5bdSAli Bahrami 	 * is being created.  This table should only contain one instance of a
24426b3ba5bdSAli Bahrami 	 * symbol that is associated to a given address.
24437c478bd9Sstevel@tonic-gate 	 *
24447c478bd9Sstevel@tonic-gate 	 * The true association between a weak/strong symbol pair is that both
24456b3ba5bdSAli Bahrami 	 * symbol entries are identical, thus first we create a sorted symbol
24466b3ba5bdSAli Bahrami 	 * list keyed off of the symbols section index and value.  If the symbol
24476b3ba5bdSAli Bahrami 	 * belongs to the same section and has the same value, then the chances
24486b3ba5bdSAli Bahrami 	 * are that the rest of the symbols data is the same.  This list is then
24496b3ba5bdSAli Bahrami 	 * scanned for weak symbols, and if one is found then any strong
24506b3ba5bdSAli Bahrami 	 * association will exist in the entries that follow.  Thus we just have
24516b3ba5bdSAli Bahrami 	 * to scan one (typically a single alias) or more (in the uncommon
24526b3ba5bdSAli Bahrami 	 * instance of multiple weak to strong associations) entries to
24536b3ba5bdSAli Bahrami 	 * determine if a match exists.
24547c478bd9Sstevel@tonic-gate 	 */
24556b3ba5bdSAli Bahrami 	if (weak && (OFL_ALLOW_LDYNSYM(ofl) || (etype == ET_DYN)) &&
2456d579eb63Sab 	    (total > local)) {
24576b3ba5bdSAli Bahrami 		static Sym_desc	**sort;
24586b3ba5bdSAli Bahrami 		static size_t	osize = 0;
24596b3ba5bdSAli Bahrami 		size_t		nsize = (total - local) * sizeof (Sym_desc *);
24607c478bd9Sstevel@tonic-gate 
24616b3ba5bdSAli Bahrami 		/*
24626b3ba5bdSAli Bahrami 		 * As we might be processing many input files, and many symbols,
24636b3ba5bdSAli Bahrami 		 * try and reuse a static sort buffer.  Note, presently we're
24646b3ba5bdSAli Bahrami 		 * playing the game of never freeing any buffers as there's a
24656b3ba5bdSAli Bahrami 		 * belief this wastes time.
24666b3ba5bdSAli Bahrami 		 */
24676b3ba5bdSAli Bahrami 		if ((osize == 0) || (nsize > osize)) {
24686b3ba5bdSAli Bahrami 			if ((sort = libld_malloc(nsize)) == NULL)
24696b3ba5bdSAli Bahrami 				return (S_ERROR);
24706b3ba5bdSAli Bahrami 			osize = nsize;
24716b3ba5bdSAli Bahrami 		}
24726b3ba5bdSAli Bahrami 		(void) memcpy((void *)sort, &ifl->ifl_oldndx[local], nsize);
24737c478bd9Sstevel@tonic-gate 
24747c478bd9Sstevel@tonic-gate 		qsort(sort, (total - local), sizeof (Sym_desc *), compare);
24757c478bd9Sstevel@tonic-gate 
24767c478bd9Sstevel@tonic-gate 		for (ndx = 0; ndx < (total - local); ndx++) {
24776b3ba5bdSAli Bahrami 			Sym_desc	*wsdp = sort[ndx];
24786b3ba5bdSAli Bahrami 			Sym		*wsym;
24797c478bd9Sstevel@tonic-gate 			int		sndx;
24807c478bd9Sstevel@tonic-gate 
24816b3ba5bdSAli Bahrami 			/*
24826b3ba5bdSAli Bahrami 			 * Ignore any empty symbol descriptor, or the case where
24836b3ba5bdSAli Bahrami 			 * the symbol has been resolved to a different file.
24846b3ba5bdSAli Bahrami 			 */
24856b3ba5bdSAli Bahrami 			if ((wsdp == NULL) || (wsdp->sd_file != ifl))
24867c478bd9Sstevel@tonic-gate 				continue;
24877c478bd9Sstevel@tonic-gate 
24887c478bd9Sstevel@tonic-gate 			wsym = wsdp->sd_sym;
24897c478bd9Sstevel@tonic-gate 
24906b3ba5bdSAli Bahrami 			if ((wsym->st_shndx == SHN_UNDEF) ||
24916b3ba5bdSAli Bahrami 			    (wsdp->sd_flags & FLG_SY_SPECSEC) ||
24926b3ba5bdSAli Bahrami 			    (ELF_ST_BIND(wsym->st_info) != STB_WEAK))
24937c478bd9Sstevel@tonic-gate 				continue;
24947c478bd9Sstevel@tonic-gate 
24957c478bd9Sstevel@tonic-gate 			/*
24967c478bd9Sstevel@tonic-gate 			 * We have a weak symbol, if it has a strong alias it
24977c478bd9Sstevel@tonic-gate 			 * will have been sorted to one of the following sort
24987c478bd9Sstevel@tonic-gate 			 * table entries.  Note that we could have multiple weak
24997c478bd9Sstevel@tonic-gate 			 * symbols aliased to one strong (if this occurs then
25007c478bd9Sstevel@tonic-gate 			 * the strong symbol only maintains one alias back to
25017c478bd9Sstevel@tonic-gate 			 * the last weak).
25027c478bd9Sstevel@tonic-gate 			 */
25037c478bd9Sstevel@tonic-gate 			for (sndx = ndx + 1; sndx < (total - local); sndx++) {
25046b3ba5bdSAli Bahrami 				Sym_desc	*ssdp = sort[sndx];
25056b3ba5bdSAli Bahrami 				Sym		*ssym;
25066b3ba5bdSAli Bahrami 				int		w_dynbits, s_dynbits;
25077c478bd9Sstevel@tonic-gate 
25086b3ba5bdSAli Bahrami 				/*
25096b3ba5bdSAli Bahrami 				 * Ignore any empty symbol descriptor, or the
25106b3ba5bdSAli Bahrami 				 * case where the symbol has been resolved to a
25116b3ba5bdSAli Bahrami 				 * different file.
25126b3ba5bdSAli Bahrami 				 */
25136b3ba5bdSAli Bahrami 				if ((ssdp == NULL) || (ssdp->sd_file != ifl))
25146b3ba5bdSAli Bahrami 					continue;
25157c478bd9Sstevel@tonic-gate 
25167c478bd9Sstevel@tonic-gate 				ssym = ssdp->sd_sym;
25177c478bd9Sstevel@tonic-gate 
25186b3ba5bdSAli Bahrami 				if (ssym->st_shndx == SHN_UNDEF)
25196b3ba5bdSAli Bahrami 					continue;
25206b3ba5bdSAli Bahrami 
25216b3ba5bdSAli Bahrami 				if ((ssym->st_shndx != wsym->st_shndx) ||
25226b3ba5bdSAli Bahrami 				    (ssym->st_value != wsym->st_value))
25237c478bd9Sstevel@tonic-gate 					break;
25247c478bd9Sstevel@tonic-gate 
25256b3ba5bdSAli Bahrami 				if ((ssym->st_size != wsym->st_size) ||
25266b3ba5bdSAli Bahrami 				    (ssdp->sd_flags & FLG_SY_SPECSEC) ||
25276b3ba5bdSAli Bahrami 				    (ELF_ST_BIND(ssym->st_info) == STB_WEAK))
25286b3ba5bdSAli Bahrami 					continue;
2529d579eb63Sab 
25306b3ba5bdSAli Bahrami 				/*
25316b3ba5bdSAli Bahrami 				 * If a sharable object, set link fields so
25326b3ba5bdSAli Bahrami 				 * that they reference each other.`
25336b3ba5bdSAli Bahrami 				 */
25346b3ba5bdSAli Bahrami 				if (etype == ET_DYN) {
25356b3ba5bdSAli Bahrami 					ssdp->sd_aux->sa_linkndx =
25366b3ba5bdSAli Bahrami 					    (Word)wsdp->sd_symndx;
25376b3ba5bdSAli Bahrami 					wsdp->sd_aux->sa_linkndx =
25386b3ba5bdSAli Bahrami 					    (Word)ssdp->sd_symndx;
25396b3ba5bdSAli Bahrami 				}
25406b3ba5bdSAli Bahrami 
25416b3ba5bdSAli Bahrami 				/*
25426b3ba5bdSAli Bahrami 				 * Determine which of these two symbols go into
25436b3ba5bdSAli Bahrami 				 * the sort section.  If a mapfile has made
25446b3ba5bdSAli Bahrami 				 * explicit settings of the FLG_SY_*DYNSORT
25456b3ba5bdSAli Bahrami 				 * flags for both symbols, then we do what they
25466b3ba5bdSAli Bahrami 				 * say.  If one has the DYNSORT flags set, we
25476b3ba5bdSAli Bahrami 				 * set the NODYNSORT bit in the other.  And if
25486b3ba5bdSAli Bahrami 				 * neither has an explicit setting, then we
25496b3ba5bdSAli Bahrami 				 * favor the weak symbol because they usually
25506b3ba5bdSAli Bahrami 				 * lack the leading underscore.
25516b3ba5bdSAli Bahrami 				 */
25526b3ba5bdSAli Bahrami 				w_dynbits = wsdp->sd_flags &
25536b3ba5bdSAli Bahrami 				    (FLG_SY_DYNSORT | FLG_SY_NODYNSORT);
25546b3ba5bdSAli Bahrami 				s_dynbits = ssdp->sd_flags &
25556b3ba5bdSAli Bahrami 				    (FLG_SY_DYNSORT | FLG_SY_NODYNSORT);
25566b3ba5bdSAli Bahrami 				if (!(w_dynbits && s_dynbits)) {
25576b3ba5bdSAli Bahrami 					if (s_dynbits) {
25586b3ba5bdSAli Bahrami 						if (s_dynbits == FLG_SY_DYNSORT)
255933eb6ee1Sab 							wsdp->sd_flags |=
256033eb6ee1Sab 							    FLG_SY_NODYNSORT;
25616b3ba5bdSAli Bahrami 					} else if (w_dynbits !=
25626b3ba5bdSAli Bahrami 					    FLG_SY_NODYNSORT) {
25636b3ba5bdSAli Bahrami 						ssdp->sd_flags |=
25646b3ba5bdSAli Bahrami 						    FLG_SY_NODYNSORT;
2565d579eb63Sab 					}
25667c478bd9Sstevel@tonic-gate 				}
25676b3ba5bdSAli Bahrami 				break;
25687c478bd9Sstevel@tonic-gate 			}
25697c478bd9Sstevel@tonic-gate 		}
25707c478bd9Sstevel@tonic-gate 	}
25717c478bd9Sstevel@tonic-gate 	return (1);
257275e45495Sab 
257375e45495Sab #undef SYM_LOC_BADADDR
25747c478bd9Sstevel@tonic-gate }
25757c478bd9Sstevel@tonic-gate 
25767c478bd9Sstevel@tonic-gate /*
2577f5a18a30Srie  * Add an undefined symbol to the symbol table.  The reference originates from
2578f5a18a30Srie  * the location identifed by the message id (mid).  These references can
2579f5a18a30Srie  * originate from command line options such as -e, -u, -initarray, etc.
2580f5a18a30Srie  * (identified with MSG_INTL(MSG_STR_COMMAND)), or from internally generated
2581f5a18a30Srie  * TLS relocation references (identified with MSG_INTL(MSG_STR_TLSREL)).
25827c478bd9Sstevel@tonic-gate  */
25837c478bd9Sstevel@tonic-gate Sym_desc *
2584f5a18a30Srie ld_sym_add_u(const char *name, Ofl_desc *ofl, Msg mid)
25857c478bd9Sstevel@tonic-gate {
25867c478bd9Sstevel@tonic-gate 	Sym		*sym;
25876b3ba5bdSAli Bahrami 	Ifl_desc	*ifl = NULL, *_ifl;
25887c478bd9Sstevel@tonic-gate 	Sym_desc	*sdp;
25897c478bd9Sstevel@tonic-gate 	Word		hash;
2590*57ef7aa9SRod Evans 	Aliste		idx;
25917c478bd9Sstevel@tonic-gate 	avl_index_t	where;
2592f5a18a30Srie 	const char	*reference = MSG_INTL(mid);
25937c478bd9Sstevel@tonic-gate 
25947c478bd9Sstevel@tonic-gate 	/*
2595b02637afSrie 	 * As an optimization, determine whether we've already generated this
2596b02637afSrie 	 * reference.  If the symbol doesn't already exist we'll create it.
2597b02637afSrie 	 * Or if the symbol does exist from a different source, we'll resolve
2598b02637afSrie 	 * the conflict.
25997c478bd9Sstevel@tonic-gate 	 */
26007c478bd9Sstevel@tonic-gate 	/* LINTED */
26017c478bd9Sstevel@tonic-gate 	hash = (Word)elf_hash(name);
2602b02637afSrie 	if ((sdp = ld_sym_find(name, hash, &where, ofl)) != NULL) {
2603b02637afSrie 		if ((sdp->sd_sym->st_shndx == SHN_UNDEF) &&
2604b02637afSrie 		    (sdp->sd_file->ifl_name == reference))
2605b02637afSrie 			return (sdp);
26067c478bd9Sstevel@tonic-gate 	}
26077c478bd9Sstevel@tonic-gate 
26087c478bd9Sstevel@tonic-gate 	/*
26097c478bd9Sstevel@tonic-gate 	 * Determine whether a pseudo input file descriptor exists to represent
26107c478bd9Sstevel@tonic-gate 	 * the command line, as any global symbol needs an input file descriptor
26117c478bd9Sstevel@tonic-gate 	 * during any symbol resolution (refer to map_ifl() which provides a
26127c478bd9Sstevel@tonic-gate 	 * similar method for adding symbols from mapfiles).
26137c478bd9Sstevel@tonic-gate 	 */
2614*57ef7aa9SRod Evans 	for (APLIST_TRAVERSE(ofl->ofl_objs, idx, _ifl))
2615f5a18a30Srie 		if (strcmp(_ifl->ifl_name, reference) == 0) {
26167c478bd9Sstevel@tonic-gate 			ifl = _ifl;
26177c478bd9Sstevel@tonic-gate 			break;
26187c478bd9Sstevel@tonic-gate 		}
26197c478bd9Sstevel@tonic-gate 
26207c478bd9Sstevel@tonic-gate 	/*
26217c478bd9Sstevel@tonic-gate 	 * If no descriptor exists create one.
26227c478bd9Sstevel@tonic-gate 	 */
26236b3ba5bdSAli Bahrami 	if (ifl == NULL) {
2624*57ef7aa9SRod Evans 		if ((ifl = libld_calloc(sizeof (Ifl_desc), 1)) == NULL)
26257c478bd9Sstevel@tonic-gate 			return ((Sym_desc *)S_ERROR);
2626f5a18a30Srie 		ifl->ifl_name = reference;
26277c478bd9Sstevel@tonic-gate 		ifl->ifl_flags = FLG_IF_NEEDED | FLG_IF_FILEREF;
2628*57ef7aa9SRod Evans 		if ((ifl->ifl_ehdr = libld_calloc(sizeof (Ehdr), 1)) == NULL)
26297c478bd9Sstevel@tonic-gate 			return ((Sym_desc *)S_ERROR);
26307c478bd9Sstevel@tonic-gate 		ifl->ifl_ehdr->e_type = ET_REL;
26317c478bd9Sstevel@tonic-gate 
2632*57ef7aa9SRod Evans 		if (aplist_append(&ofl->ofl_objs, ifl, AL_CNT_OFL_OBJS) == NULL)
26337c478bd9Sstevel@tonic-gate 			return ((Sym_desc *)S_ERROR);
26347c478bd9Sstevel@tonic-gate 	}
26357c478bd9Sstevel@tonic-gate 
26367c478bd9Sstevel@tonic-gate 	/*
26377c478bd9Sstevel@tonic-gate 	 * Allocate a symbol structure and add it to the global symbol table.
26387c478bd9Sstevel@tonic-gate 	 */
26396b3ba5bdSAli Bahrami 	if ((sym = libld_calloc(sizeof (Sym), 1)) == NULL)
26407c478bd9Sstevel@tonic-gate 		return ((Sym_desc *)S_ERROR);
26417c478bd9Sstevel@tonic-gate 	sym->st_info = ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE);
26427c478bd9Sstevel@tonic-gate 	sym->st_shndx = SHN_UNDEF;
26437c478bd9Sstevel@tonic-gate 
26445aefb655Srie 	DBG_CALL(Dbg_syms_process(ofl->ofl_lml, ifl));
2645b02637afSrie 	if (sdp == NULL) {
2646b02637afSrie 		DBG_CALL(Dbg_syms_global(ofl->ofl_lml, 0, name));
2647b02637afSrie 		if ((sdp = ld_sym_enter(name, sym, hash, ifl, ofl, 0, SHN_UNDEF,
2648b02637afSrie 		    0, 0, &where)) == (Sym_desc *)S_ERROR)
2649b02637afSrie 			return ((Sym_desc *)S_ERROR);
2650b02637afSrie 	} else if (ld_sym_resolve(sdp, sym, ifl, ofl, 0,
2651b02637afSrie 	    SHN_UNDEF, 0) == S_ERROR)
2652b02637afSrie 		return ((Sym_desc *)S_ERROR);
2653b02637afSrie 
26547c478bd9Sstevel@tonic-gate 	sdp->sd_flags &= ~FLG_SY_CLEAN;
26557c478bd9Sstevel@tonic-gate 	sdp->sd_flags |= FLG_SY_CMDREF;
26567c478bd9Sstevel@tonic-gate 
26577c478bd9Sstevel@tonic-gate 	return (sdp);
26587c478bd9Sstevel@tonic-gate }
2659