xref: /illumos-gate/usr/src/cmd/sgs/libld/common/syms.c (revision ba2be530)
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  *
27d4517e84Srie  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
287c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
297c478bd9Sstevel@tonic-gate  */
307c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate /*
337c478bd9Sstevel@tonic-gate  * Symbol table management routines
347c478bd9Sstevel@tonic-gate  */
35*ba2be530Sab 
36*ba2be530Sab #define	ELF_TARGET_AMD64
37*ba2be530Sab 
387c478bd9Sstevel@tonic-gate #include	<stdio.h>
397c478bd9Sstevel@tonic-gate #include	<string.h>
405aefb655Srie #include	<debug.h>
417c478bd9Sstevel@tonic-gate #include	"msg.h"
427c478bd9Sstevel@tonic-gate #include	"_libld.h"
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate /*
457c478bd9Sstevel@tonic-gate  * AVL tree comparator function:
467c478bd9Sstevel@tonic-gate  *
477c478bd9Sstevel@tonic-gate  *	The primary key is the 'sa_hashval' with a secondary
487c478bd9Sstevel@tonic-gate  *	key of the symbol name itself.
497c478bd9Sstevel@tonic-gate  */
507c478bd9Sstevel@tonic-gate int
515aefb655Srie ld_sym_avl_comp(const void *elem1, const void *elem2)
527c478bd9Sstevel@tonic-gate {
537c478bd9Sstevel@tonic-gate 	int	res;
547c478bd9Sstevel@tonic-gate 	Sym_avlnode	*sav1 = (Sym_avlnode *)elem1;
557c478bd9Sstevel@tonic-gate 	Sym_avlnode	*sav2 = (Sym_avlnode *)elem2;
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate 	res = sav1->sav_hash - sav2->sav_hash;
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate 	if (res < 0)
607c478bd9Sstevel@tonic-gate 		return (-1);
617c478bd9Sstevel@tonic-gate 	if (res > 0)
627c478bd9Sstevel@tonic-gate 		return (1);
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate 	/*
657c478bd9Sstevel@tonic-gate 	 * Hash is equal - now compare name
667c478bd9Sstevel@tonic-gate 	 */
677c478bd9Sstevel@tonic-gate 	res = strcmp(sav1->sav_name, sav2->sav_name);
687c478bd9Sstevel@tonic-gate 	if (res == 0)
697c478bd9Sstevel@tonic-gate 		return (0);
707c478bd9Sstevel@tonic-gate 	if (res > 0)
717c478bd9Sstevel@tonic-gate 		return (1);
727c478bd9Sstevel@tonic-gate 	return (-1);
737c478bd9Sstevel@tonic-gate }
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate /*
777c478bd9Sstevel@tonic-gate  * Focal point for verifying symbol names.
787c478bd9Sstevel@tonic-gate  */
79a194faf8Srie inline static const char *
805aefb655Srie string(Ofl_desc *ofl, Ifl_desc *ifl, Sym *sym, const char *strs, size_t strsize,
815aefb655Srie     int symndx, Word shndx, const char *symsecname, const char *strsecname,
825aefb655Srie     Word *flags)
837c478bd9Sstevel@tonic-gate {
847c478bd9Sstevel@tonic-gate 	Word		name = sym->st_name;
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate 	if (name) {
877c478bd9Sstevel@tonic-gate 		if ((ifl->ifl_flags & FLG_IF_HSTRTAB) == 0) {
885aefb655Srie 			eprintf(ofl->ofl_lml, ERR_FATAL,
895aefb655Srie 			    MSG_INTL(MSG_FIL_NOSTRTABLE), ifl->ifl_name,
905aefb655Srie 			    symsecname, symndx, EC_XWORD(name));
917c478bd9Sstevel@tonic-gate 			return (0);
927c478bd9Sstevel@tonic-gate 		}
937c478bd9Sstevel@tonic-gate 		if (name >= (Word)strsize) {
945aefb655Srie 			eprintf(ofl->ofl_lml, ERR_FATAL,
955aefb655Srie 			    MSG_INTL(MSG_FIL_EXCSTRTABLE), ifl->ifl_name,
965aefb655Srie 			    symsecname, symndx, EC_XWORD(name),
977c478bd9Sstevel@tonic-gate 			    strsecname, EC_XWORD(strsize));
987c478bd9Sstevel@tonic-gate 			return (0);
997c478bd9Sstevel@tonic-gate 		}
1007c478bd9Sstevel@tonic-gate 	}
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate 	/*
1037c478bd9Sstevel@tonic-gate 	 * Determine if we're dealing with a register and if so validate it.
1047c478bd9Sstevel@tonic-gate 	 * If it's a scratch register, a fabricated name will be returned.
1057c478bd9Sstevel@tonic-gate 	 */
106*ba2be530Sab 	if (ld_targ.t_ms.ms_is_regsym != NULL) {
107*ba2be530Sab 		const char *regname = (*ld_targ.t_ms.ms_is_regsym)(ofl, ifl,
108*ba2be530Sab 		    sym, strs, symndx, shndx, symsecname, flags);
109*ba2be530Sab 
110*ba2be530Sab 		if (regname == (const char *)S_ERROR) {
111*ba2be530Sab 			return (0);
112*ba2be530Sab 		}
113*ba2be530Sab 		if (regname)
114*ba2be530Sab 			return (regname);
1157c478bd9Sstevel@tonic-gate 	}
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 	/*
1187c478bd9Sstevel@tonic-gate 	 * If this isn't a register, but we have a global symbol with a null
1197c478bd9Sstevel@tonic-gate 	 * name, we're not going to be able to hash this, search for it, or
1207c478bd9Sstevel@tonic-gate 	 * do anything interesting.  However, we've been accepting a symbol of
1217c478bd9Sstevel@tonic-gate 	 * this kind for ages now, so give the user a warning (rather than a
1227c478bd9Sstevel@tonic-gate 	 * fatal error), just in case this instance exists somewhere in the
1237c478bd9Sstevel@tonic-gate 	 * world and hasn't, as yet, been a problem.
1247c478bd9Sstevel@tonic-gate 	 */
1257c478bd9Sstevel@tonic-gate 	if ((name == 0) && (ELF_ST_BIND(sym->st_info) != STB_LOCAL)) {
1265aefb655Srie 		eprintf(ofl->ofl_lml, ERR_WARNING, MSG_INTL(MSG_FIL_NONAMESYM),
1277c478bd9Sstevel@tonic-gate 		    ifl->ifl_name, symsecname, symndx, EC_XWORD(name));
1287c478bd9Sstevel@tonic-gate 	}
1297c478bd9Sstevel@tonic-gate 	return (strs + name);
1307c478bd9Sstevel@tonic-gate }
1317c478bd9Sstevel@tonic-gate 
132*ba2be530Sab 
133*ba2be530Sab /*
134*ba2be530Sab  * For producing symbol names strings to use in error messages.
135*ba2be530Sab  * If the symbol has a non-null name, then the string returned by
136*ba2be530Sab  * this function is the output from demangle(), surrounded by
137*ba2be530Sab  * single quotes. For null names, a descriptive string giving
138*ba2be530Sab  * the symbol section and index is generated.
139*ba2be530Sab  *
140*ba2be530Sab  * This function uses an internal static buffer to hold the resulting
141*ba2be530Sab  * string. The value returned is usable by the caller until the next
142*ba2be530Sab  * call, at which point it is overwritten.
143*ba2be530Sab  */
144*ba2be530Sab static const char *
145*ba2be530Sab demangle_symname(const char *name, const char *symtab_name, Word symndx)
146*ba2be530Sab {
147*ba2be530Sab #define	INIT_BUFSIZE 256
148*ba2be530Sab 
149*ba2be530Sab 	static char *buf;
150*ba2be530Sab 	static size_t bufsize = 0;
151*ba2be530Sab 
152*ba2be530Sab 	size_t		len;
153*ba2be530Sab 	int		use_name;
154*ba2be530Sab 
155*ba2be530Sab 
156*ba2be530Sab 	use_name = (name != NULL) && (*name != '\0');
157*ba2be530Sab 
158*ba2be530Sab 	if (use_name) {
159*ba2be530Sab 		name = demangle(name);
160*ba2be530Sab 		len = strlen(name) + 2;   /* Include room for quotes */
161*ba2be530Sab 	} else {
162*ba2be530Sab 		name = MSG_ORIG(MSG_STR_EMPTY);
163*ba2be530Sab 		len = strlen(symtab_name) + 2 + CONV32_INV_BUFSIZE;
164*ba2be530Sab 	}
165*ba2be530Sab 	len++;			/* Null termination */
166*ba2be530Sab 
167*ba2be530Sab 	/* If our buffer is too small, double it until it is big enough */
168*ba2be530Sab 	if (len > bufsize) {
169*ba2be530Sab 		size_t	new_bufsize = bufsize;
170*ba2be530Sab 		char	*new_buf;
171*ba2be530Sab 
172*ba2be530Sab 		if (new_bufsize == 0)
173*ba2be530Sab 			new_bufsize = INIT_BUFSIZE;
174*ba2be530Sab 		while (len > new_bufsize)
175*ba2be530Sab 			new_bufsize *= 2;
176*ba2be530Sab 		new_buf = libld_malloc(new_bufsize);
177*ba2be530Sab 		if (new_buf == NULL)
178*ba2be530Sab 			return (name);
179*ba2be530Sab 		buf = new_buf;
180*ba2be530Sab 		bufsize = new_bufsize;
181*ba2be530Sab 	}
182*ba2be530Sab 
183*ba2be530Sab 	if (use_name) {
184*ba2be530Sab 		(void) snprintf(buf, bufsize, MSG_ORIG(MSG_FMT_SYMNAM), name);
185*ba2be530Sab 	} else {
186*ba2be530Sab 		(void) snprintf(buf, bufsize, MSG_ORIG(MSG_FMT_NULLSYMNAM),
187*ba2be530Sab 		    symtab_name, EC_WORD(symndx));
188*ba2be530Sab 	}
189*ba2be530Sab 
190*ba2be530Sab 	return (buf);
191*ba2be530Sab 
192*ba2be530Sab #undef INIT_BUFSIZE
193*ba2be530Sab }
194*ba2be530Sab 
1957c478bd9Sstevel@tonic-gate /*
1967c478bd9Sstevel@tonic-gate  * Shared objects can be built that define specific symbols that can not be
1977c478bd9Sstevel@tonic-gate  * directly bound to.  These objects have a syminfo section (and an associated
1987c478bd9Sstevel@tonic-gate  * DF_1_NODIRECT dynamic flags entry).  Scan this table looking for symbols
1997c478bd9Sstevel@tonic-gate  * that can't be bound to directly, and if this files symbol is presently
2007c478bd9Sstevel@tonic-gate  * referenced, mark it so that we don't directly bind to it.
2017c478bd9Sstevel@tonic-gate  */
2027c478bd9Sstevel@tonic-gate uintptr_t
2039a411307Srie ld_sym_nodirect(Is_desc *isp, Ifl_desc *ifl, Ofl_desc *ofl)
2047c478bd9Sstevel@tonic-gate {
2057c478bd9Sstevel@tonic-gate 	Shdr		*sifshdr, *symshdr;
2067c478bd9Sstevel@tonic-gate 	Syminfo		*sifdata;
2077c478bd9Sstevel@tonic-gate 	Sym		*symdata;
2087c478bd9Sstevel@tonic-gate 	char		*strdata;
2097c478bd9Sstevel@tonic-gate 	ulong_t		cnt, _cnt;
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 	/*
2127c478bd9Sstevel@tonic-gate 	 * Get the syminfo data, and determine the number of entries.
2137c478bd9Sstevel@tonic-gate 	 */
2147c478bd9Sstevel@tonic-gate 	sifshdr = isp->is_shdr;
2157c478bd9Sstevel@tonic-gate 	sifdata = (Syminfo *)isp->is_indata->d_buf;
2167c478bd9Sstevel@tonic-gate 	cnt =  sifshdr->sh_size / sifshdr->sh_entsize;
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 	/*
2197c478bd9Sstevel@tonic-gate 	 * Get the associated symbol table.
2207c478bd9Sstevel@tonic-gate 	 */
2217c478bd9Sstevel@tonic-gate 	symshdr = ifl->ifl_isdesc[sifshdr->sh_link]->is_shdr;
2227c478bd9Sstevel@tonic-gate 	symdata = ifl->ifl_isdesc[sifshdr->sh_link]->is_indata->d_buf;
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 	/*
2257c478bd9Sstevel@tonic-gate 	 * Get the string table associated with the symbol table.
2267c478bd9Sstevel@tonic-gate 	 */
2277c478bd9Sstevel@tonic-gate 	strdata = ifl->ifl_isdesc[symshdr->sh_link]->is_indata->d_buf;
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate 	/*
2307c478bd9Sstevel@tonic-gate 	 * Traverse the syminfo data for symbols that can't be directly
2317c478bd9Sstevel@tonic-gate 	 * bound to.
2327c478bd9Sstevel@tonic-gate 	 */
2337c478bd9Sstevel@tonic-gate 	for (_cnt = 1, sifdata++; _cnt < cnt; _cnt++, sifdata++) {
2347c478bd9Sstevel@tonic-gate 		Sym		*sym;
2357c478bd9Sstevel@tonic-gate 		char		*str;
2367c478bd9Sstevel@tonic-gate 		Sym_desc	*sdp;
2377c478bd9Sstevel@tonic-gate 
2383c4993fbSrie 		if ((sifdata->si_flags & SYMINFO_FLG_NOEXTDIRECT) == 0)
2397c478bd9Sstevel@tonic-gate 			continue;
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 		sym = (Sym *)(symdata + _cnt);
2427c478bd9Sstevel@tonic-gate 		str = (char *)(strdata + sym->st_name);
2437c478bd9Sstevel@tonic-gate 
2445aefb655Srie 		if (sdp = ld_sym_find(str, SYM_NOHASH, 0, ofl)) {
2457c478bd9Sstevel@tonic-gate 			if (ifl != sdp->sd_file)
2467c478bd9Sstevel@tonic-gate 				continue;
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 			sdp->sd_flags1 &= ~FLG_SY1_DIR;
2497c478bd9Sstevel@tonic-gate 			sdp->sd_flags1 |= FLG_SY1_NDIR;
2507c478bd9Sstevel@tonic-gate 		}
2517c478bd9Sstevel@tonic-gate 	}
2527c478bd9Sstevel@tonic-gate 	return (0);
2537c478bd9Sstevel@tonic-gate }
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate /*
2567c478bd9Sstevel@tonic-gate  * If, during symbol processing, it is necessary to update a local symbols
2577c478bd9Sstevel@tonic-gate  * contents before we have generated the symbol tables in the output image,
2587c478bd9Sstevel@tonic-gate  * create a new symbol structure and copy the original symbol contents.  While
2597c478bd9Sstevel@tonic-gate  * we are processing the input files, their local symbols are part of the
2607c478bd9Sstevel@tonic-gate  * read-only mapped image.  Commonly, these symbols are copied to the new output
2617c478bd9Sstevel@tonic-gate  * file image and then updated to reflect their new address and any change in
2627c478bd9Sstevel@tonic-gate  * attributes.  However, sometimes during relocation counting, it is necessary
2637c478bd9Sstevel@tonic-gate  * to adjust the symbols information.  This routine provides for the generation
2647c478bd9Sstevel@tonic-gate  * of a new symbol image so that this update can be performed.
2657c478bd9Sstevel@tonic-gate  * All global symbols are copied to an internal symbol table to improve locality
2667c478bd9Sstevel@tonic-gate  * of reference and hence performance, and thus this copying is not necessary.
2677c478bd9Sstevel@tonic-gate  */
2687c478bd9Sstevel@tonic-gate uintptr_t
2695aefb655Srie ld_sym_copy(Sym_desc *sdp)
2707c478bd9Sstevel@tonic-gate {
2717c478bd9Sstevel@tonic-gate 	Sym	*nsym;
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate 	if (sdp->sd_flags & FLG_SY_CLEAN) {
2747c478bd9Sstevel@tonic-gate 		if ((nsym = libld_malloc(sizeof (Sym))) == 0)
2757c478bd9Sstevel@tonic-gate 			return (S_ERROR);
2767c478bd9Sstevel@tonic-gate 		*nsym = *(sdp->sd_sym);
2777c478bd9Sstevel@tonic-gate 		sdp->sd_sym = nsym;
2787c478bd9Sstevel@tonic-gate 		sdp->sd_flags &= ~FLG_SY_CLEAN;
2797c478bd9Sstevel@tonic-gate 	}
2807c478bd9Sstevel@tonic-gate 	return (1);
2817c478bd9Sstevel@tonic-gate }
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate /*
2847c478bd9Sstevel@tonic-gate  * Finds a given name in the link editors internal symbol table.  If no
2857c478bd9Sstevel@tonic-gate  * hash value is specified it is calculated.  A pointer to the located
2867c478bd9Sstevel@tonic-gate  * Sym_desc entry is returned, or NULL if the symbol is not found.
2877c478bd9Sstevel@tonic-gate  */
2887c478bd9Sstevel@tonic-gate Sym_desc *
2895aefb655Srie ld_sym_find(const char *name, Word hash, avl_index_t *where, Ofl_desc *ofl)
2907c478bd9Sstevel@tonic-gate {
2917c478bd9Sstevel@tonic-gate 	Sym_avlnode	qsav;
2927c478bd9Sstevel@tonic-gate 	Sym_avlnode	*sav;
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate 	if (hash == SYM_NOHASH)
2957c478bd9Sstevel@tonic-gate 		/* LINTED */
2967c478bd9Sstevel@tonic-gate 		hash = (Word)elf_hash((const char *)name);
2977c478bd9Sstevel@tonic-gate 	qsav.sav_hash = hash;
2987c478bd9Sstevel@tonic-gate 	qsav.sav_name = name;
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate 	/*
3017c478bd9Sstevel@tonic-gate 	 * Perform search for symbol in AVL tree.  Note that the 'where' field
3027c478bd9Sstevel@tonic-gate 	 * is passed in from the caller.  If a 'where' is present, it can be
303a194faf8Srie 	 * used in subsequent 'ld_sym_enter()' calls if required.
3047c478bd9Sstevel@tonic-gate 	 */
3057c478bd9Sstevel@tonic-gate 	sav = avl_find(&ofl->ofl_symavl, &qsav, where);
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate 	/*
3087c478bd9Sstevel@tonic-gate 	 * If symbol was not found in the avl tree, return null to show that.
3097c478bd9Sstevel@tonic-gate 	 */
3107c478bd9Sstevel@tonic-gate 	if (sav == 0)
3117c478bd9Sstevel@tonic-gate 		return (0);
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate 	/*
3147c478bd9Sstevel@tonic-gate 	 * Return symbol found.
3157c478bd9Sstevel@tonic-gate 	 */
3167c478bd9Sstevel@tonic-gate 	return (sav->sav_symdesc);
3177c478bd9Sstevel@tonic-gate }
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate /*
3217c478bd9Sstevel@tonic-gate  * Enter a new symbol into the link editors internal symbol table.
3227c478bd9Sstevel@tonic-gate  * If the symbol is from an input file, information regarding the input file
3237c478bd9Sstevel@tonic-gate  * and input section is also recorded.  Otherwise (file == NULL) the symbol
3247c478bd9Sstevel@tonic-gate  * has been internally generated (ie. _etext, _edata, etc.).
3257c478bd9Sstevel@tonic-gate  */
3267c478bd9Sstevel@tonic-gate Sym_desc *
3275aefb655Srie ld_sym_enter(const char *name, Sym *osym, Word hash, Ifl_desc *ifl,
3285aefb655Srie     Ofl_desc *ofl, Word ndx, Word shndx, Word sdflags, Half sdflags1,
3295aefb655Srie     avl_index_t *where)
3307c478bd9Sstevel@tonic-gate {
3317c478bd9Sstevel@tonic-gate 	Sym_desc	*sdp;
3327c478bd9Sstevel@tonic-gate 	Sym_aux		*sap;
3337c478bd9Sstevel@tonic-gate 	Sym_avlnode	*savl;
3347c478bd9Sstevel@tonic-gate 	char		*_name;
3357c478bd9Sstevel@tonic-gate 	Sym		*nsym;
3367c478bd9Sstevel@tonic-gate 	Half		etype;
33760758829Srie 	uchar_t		vis;
3387c478bd9Sstevel@tonic-gate 	avl_index_t	_where;
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate 	/*
3417c478bd9Sstevel@tonic-gate 	 * Establish the file type.
3427c478bd9Sstevel@tonic-gate 	 */
3437c478bd9Sstevel@tonic-gate 	if (ifl)
3447c478bd9Sstevel@tonic-gate 		etype = ifl->ifl_ehdr->e_type;
3457c478bd9Sstevel@tonic-gate 	else
3467c478bd9Sstevel@tonic-gate 		etype = ET_NONE;
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate 	ofl->ofl_entercnt++;
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate 	/*
3517c478bd9Sstevel@tonic-gate 	 * Allocate a Sym Descriptor, Auxiliary Descriptor, and a Sym AVLNode -
3527c478bd9Sstevel@tonic-gate 	 * contiguously.
3537c478bd9Sstevel@tonic-gate 	 */
3547c478bd9Sstevel@tonic-gate 	if ((savl = libld_calloc(sizeof (Sym_avlnode) + sizeof (Sym_desc) +
3557c478bd9Sstevel@tonic-gate 	    sizeof (Sym_aux), 1)) == 0)
3567c478bd9Sstevel@tonic-gate 		return ((Sym_desc *)S_ERROR);
3577c478bd9Sstevel@tonic-gate 	sdp = (Sym_desc *)((uintptr_t)savl + sizeof (Sym_avlnode));
3587c478bd9Sstevel@tonic-gate 	sap = (Sym_aux *)((uintptr_t)sdp + sizeof (Sym_desc));
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate 	savl->sav_symdesc = sdp;
3617c478bd9Sstevel@tonic-gate 	sdp->sd_file = ifl;
3627c478bd9Sstevel@tonic-gate 	sdp->sd_aux = sap;
3637c478bd9Sstevel@tonic-gate 	savl->sav_hash = sap->sa_hash = hash;
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate 	/*
3667c478bd9Sstevel@tonic-gate 	 * Copy the symbol table entry from the input file into the internal
3677c478bd9Sstevel@tonic-gate 	 * entry and have the symbol descriptor use it.
3687c478bd9Sstevel@tonic-gate 	 */
3697c478bd9Sstevel@tonic-gate 	sdp->sd_sym = nsym = &sap->sa_sym;
3707c478bd9Sstevel@tonic-gate 	*nsym = *osym;
3717c478bd9Sstevel@tonic-gate 	sdp->sd_shndx = shndx;
3727c478bd9Sstevel@tonic-gate 	sdp->sd_flags |= sdflags;
3737c478bd9Sstevel@tonic-gate 	sdp->sd_flags1 |= sdflags1;
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate 	if ((_name = libld_malloc(strlen(name) + 1)) == 0)
3767c478bd9Sstevel@tonic-gate 		return ((Sym_desc *)S_ERROR);
3777c478bd9Sstevel@tonic-gate 	savl->sav_name = sdp->sd_name = (const char *)strcpy(_name, name);
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate 	/*
3807c478bd9Sstevel@tonic-gate 	 * Enter Symbol in AVL tree.
3817c478bd9Sstevel@tonic-gate 	 */
3827c478bd9Sstevel@tonic-gate 	if (where == 0) {
3837c478bd9Sstevel@tonic-gate 		/* LINTED */
3847c478bd9Sstevel@tonic-gate 		Sym_avlnode	*_savl;
3857c478bd9Sstevel@tonic-gate 		/*
3865aefb655Srie 		 * If a previous ld_sym_find() hasn't initialized 'where' do it
3877c478bd9Sstevel@tonic-gate 		 * now.
3887c478bd9Sstevel@tonic-gate 		 */
3897c478bd9Sstevel@tonic-gate 		where = &_where;
3907c478bd9Sstevel@tonic-gate 		_savl = avl_find(&ofl->ofl_symavl, savl, where);
3917c478bd9Sstevel@tonic-gate 		assert(_savl == 0);
3927c478bd9Sstevel@tonic-gate 	}
3937c478bd9Sstevel@tonic-gate 	avl_insert(&ofl->ofl_symavl, savl, *where);
3947c478bd9Sstevel@tonic-gate 
3957c478bd9Sstevel@tonic-gate 	/*
3967c478bd9Sstevel@tonic-gate 	 * Record the section index.  This is possible because the
3977c478bd9Sstevel@tonic-gate 	 * `ifl_isdesc' table is filled before we start symbol processing.
3987c478bd9Sstevel@tonic-gate 	 */
3990bc07c75Srie 	if ((sdflags & FLG_SY_SPECSEC) || (nsym->st_shndx == SHN_UNDEF))
4007c478bd9Sstevel@tonic-gate 		sdp->sd_isc = NULL;
4017c478bd9Sstevel@tonic-gate 	else {
4027c478bd9Sstevel@tonic-gate 		sdp->sd_isc = ifl->ifl_isdesc[shndx];
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate 		/*
4057c478bd9Sstevel@tonic-gate 		 * If this symbol is from a relocatable object, make sure that
4067c478bd9Sstevel@tonic-gate 		 * it is still associated with a section.  For example, an
4077c478bd9Sstevel@tonic-gate 		 * unknown section type (SHT_NULL) would have been rejected on
4087c478bd9Sstevel@tonic-gate 		 * input with a warning.  Here, we make the use of the symbol
4097c478bd9Sstevel@tonic-gate 		 * fatal.  A symbol descriptor is still returned, so that the
4107c478bd9Sstevel@tonic-gate 		 * caller can continue processing all symbols, and hence flush
4117c478bd9Sstevel@tonic-gate 		 * out as many error conditions as possible.
4127c478bd9Sstevel@tonic-gate 		 */
4137c478bd9Sstevel@tonic-gate 		if ((etype == ET_REL) && (sdp->sd_isc == 0)) {
4145aefb655Srie 			eprintf(ofl->ofl_lml, ERR_FATAL,
4155aefb655Srie 			    MSG_INTL(MSG_SYM_INVSEC), name, ifl->ifl_name,
4165aefb655Srie 			    EC_XWORD(shndx));
4177c478bd9Sstevel@tonic-gate 			ofl->ofl_flags |= FLG_OF_FATAL;
4187c478bd9Sstevel@tonic-gate 			return (sdp);
4197c478bd9Sstevel@tonic-gate 		}
4207c478bd9Sstevel@tonic-gate 	}
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate 	/*
4237c478bd9Sstevel@tonic-gate 	 * Mark any COMMON symbols as 'tentative'.
4247c478bd9Sstevel@tonic-gate 	 */
42554d82594Sseizo 	if (sdflags & FLG_SY_SPECSEC) {
4260bc07c75Srie 		if (nsym->st_shndx == SHN_COMMON)
42754d82594Sseizo 			sdp->sd_flags |= FLG_SY_TENTSYM;
428*ba2be530Sab #if	defined(_ELF64)
429*ba2be530Sab 		else if ((ld_targ.t_m.m_mach == EM_AMD64) &&
430*ba2be530Sab 		    (nsym->st_shndx == SHN_X86_64_LCOMMON))
43154d82594Sseizo 			sdp->sd_flags |= FLG_SY_TENTSYM;
43254d82594Sseizo #endif
43354d82594Sseizo 	}
4347c478bd9Sstevel@tonic-gate 
4357c478bd9Sstevel@tonic-gate 	/*
43660758829Srie 	 * Establish the symbols visibility and reference.
4377c478bd9Sstevel@tonic-gate 	 */
43860758829Srie 	vis = ELF_ST_VISIBILITY(nsym->st_other);
43960758829Srie 
4407c478bd9Sstevel@tonic-gate 	if ((etype == ET_NONE) || (etype == ET_REL)) {
44160758829Srie 		switch (vis) {
44260758829Srie 		case STV_DEFAULT:
44360758829Srie 			sdp->sd_flags1 |= FLG_SY1_DEFAULT;
44460758829Srie 			break;
44560758829Srie 		case STV_INTERNAL:
44660758829Srie 		case STV_HIDDEN:
44760758829Srie 			sdp->sd_flags1 |= FLG_SY1_HIDDEN;
44860758829Srie 			break;
44960758829Srie 		case STV_PROTECTED:
45060758829Srie 			sdp->sd_flags1 |= FLG_SY1_PROTECT;
45160758829Srie 			break;
45260758829Srie 		case STV_EXPORTED:
45360758829Srie 			sdp->sd_flags1 |= FLG_SY1_EXPORT;
45460758829Srie 			break;
45560758829Srie 		case STV_SINGLETON:
45660758829Srie 			sdp->sd_flags1 |= (FLG_SY1_SINGLE | FLG_SY1_NDIR);
45760758829Srie 			ofl->ofl_flags1 |= FLG_OF1_NDIRECT;
45860758829Srie 			break;
45960758829Srie 		case STV_ELIMINATE:
460d4517e84Srie 			sdp->sd_flags1 |= (FLG_SY1_HIDDEN | FLG_SY1_ELIM);
46160758829Srie 			break;
46260758829Srie 		default:
46360758829Srie 			assert(vis <= STV_ELIMINATE);
46460758829Srie 		}
46560758829Srie 
4667c478bd9Sstevel@tonic-gate 		sdp->sd_ref = REF_REL_NEED;
4677c478bd9Sstevel@tonic-gate 
4687c478bd9Sstevel@tonic-gate 		/*
4699a411307Srie 		 * Under -Bnodirect, all exported interfaces that have not
4709a411307Srie 		 * explicitly been defined protected or directly bound to, are
4719a411307Srie 		 * tagged to prevent direct binding.
4727c478bd9Sstevel@tonic-gate 		 */
4730bc07c75Srie 		if ((ofl->ofl_flags1 & FLG_OF1_ALNODIR) &&
47460758829Srie 		    ((sdp->sd_flags1 & (FLG_SY1_PROTECT | FLG_SY1_DIR)) == 0) &&
4759a411307Srie 		    (nsym->st_shndx != SHN_UNDEF)) {
4767c478bd9Sstevel@tonic-gate 			sdp->sd_flags1 |= FLG_SY1_NDIR;
4779a411307Srie 		}
4787c478bd9Sstevel@tonic-gate 	} else {
4797c478bd9Sstevel@tonic-gate 		sdp->sd_ref = REF_DYN_SEEN;
4807c478bd9Sstevel@tonic-gate 
4817c478bd9Sstevel@tonic-gate 		/*
4827c478bd9Sstevel@tonic-gate 		 * Record the binding file for this symbol in the sa_bindto
4837c478bd9Sstevel@tonic-gate 		 * field.  If this symbol is ever overridden by a REF_REL_NEED
4847c478bd9Sstevel@tonic-gate 		 * definition, sa_bindto is used when building a 'translator'.
4857c478bd9Sstevel@tonic-gate 		 */
4860bc07c75Srie 		if (nsym->st_shndx != SHN_UNDEF)
4877c478bd9Sstevel@tonic-gate 			sdp->sd_aux->sa_bindto = ifl;
4887c478bd9Sstevel@tonic-gate 
4897c478bd9Sstevel@tonic-gate 		/*
49060758829Srie 		 * If this is a protected symbol, remember this.  Note, this
49160758829Srie 		 * state is different from the FLG_SY1_PROTECT used to establish
49260758829Srie 		 * a symbol definitions visibility.  This state is used to warn
49360758829Srie 		 * against possible copy relocations against this referenced
49460758829Srie 		 * symbol.
4957c478bd9Sstevel@tonic-gate 		 */
49660758829Srie 		if (vis == STV_PROTECTED)
4977c478bd9Sstevel@tonic-gate 			sdp->sd_flags |= FLG_SY_PROT;
4987c478bd9Sstevel@tonic-gate 
4997c478bd9Sstevel@tonic-gate 		/*
50060758829Srie 		 * If this is a SINGLETON definition, then indicate the symbol
50160758829Srie 		 * can not be directly bound to, and retain the visibility.
50260758829Srie 		 * This visibility will be inherited by any references made to
50360758829Srie 		 * this symbol.
5047c478bd9Sstevel@tonic-gate 		 */
50560758829Srie 		if ((vis == STV_SINGLETON) && (nsym->st_shndx != SHN_UNDEF))
50660758829Srie 			sdp->sd_flags1 |= (FLG_SY1_SINGLE | FLG_SY1_NDIR);
5077c478bd9Sstevel@tonic-gate 
5087c478bd9Sstevel@tonic-gate 		/*
50960758829Srie 		 * If the new symbol is from a shared library and is associated
51060758829Srie 		 * with a SHT_NOBITS section then this symbol originated from a
51160758829Srie 		 * tentative symbol.
5127c478bd9Sstevel@tonic-gate 		 */
5137c478bd9Sstevel@tonic-gate 		if (sdp->sd_isc &&
5147c478bd9Sstevel@tonic-gate 		    (sdp->sd_isc->is_shdr->sh_type == SHT_NOBITS))
5157c478bd9Sstevel@tonic-gate 			sdp->sd_flags |= FLG_SY_TENTSYM;
5167c478bd9Sstevel@tonic-gate 	}
5177c478bd9Sstevel@tonic-gate 
5187c478bd9Sstevel@tonic-gate 	/*
5197c478bd9Sstevel@tonic-gate 	 * Reclassify any SHN_SUNW_IGNORE symbols to SHN_UNDEF so as to
5200bc07c75Srie 	 * simplify future processing.
5217c478bd9Sstevel@tonic-gate 	 */
5220bc07c75Srie 	if (nsym->st_shndx == SHN_SUNW_IGNORE) {
5237c478bd9Sstevel@tonic-gate 		sdp->sd_shndx = shndx = SHN_UNDEF;
5247c478bd9Sstevel@tonic-gate 		sdp->sd_flags |= FLG_SY_REDUCED;
5257c478bd9Sstevel@tonic-gate 		sdp->sd_flags1 |=
52660758829Srie 		    (FLG_SY1_HIDDEN | FLG_SY1_IGNORE | FLG_SY1_ELIM);
5277c478bd9Sstevel@tonic-gate 	}
5287c478bd9Sstevel@tonic-gate 
5297c478bd9Sstevel@tonic-gate 	/*
5307c478bd9Sstevel@tonic-gate 	 * If this is an undefined, or common symbol from a relocatable object
5317c478bd9Sstevel@tonic-gate 	 * determine whether it is a global or weak reference (see build_osym(),
5327c478bd9Sstevel@tonic-gate 	 * where REF_DYN_NEED definitions are returned back to undefines).
5337c478bd9Sstevel@tonic-gate 	 */
53454d82594Sseizo 	if ((etype == ET_REL) &&
53554d82594Sseizo 	    (ELF_ST_BIND(nsym->st_info) == STB_GLOBAL) &&
5360bc07c75Srie 	    ((nsym->st_shndx == SHN_UNDEF) || ((sdflags & FLG_SY_SPECSEC) &&
537*ba2be530Sab #if	defined(_ELF64)
5380bc07c75Srie 	    ((nsym->st_shndx == SHN_COMMON) ||
539*ba2be530Sab 	    ((ld_targ.t_m.m_mach == EM_AMD64) &&
540*ba2be530Sab 	    (nsym->st_shndx == SHN_X86_64_LCOMMON))))))
54154d82594Sseizo #else
54233eb6ee1Sab 	/* BEGIN CSTYLED */
5430bc07c75Srie 	    (nsym->st_shndx == SHN_COMMON))))
54433eb6ee1Sab 	/* END CSTYLED */
54554d82594Sseizo #endif
5467c478bd9Sstevel@tonic-gate 		sdp->sd_flags |= FLG_SY_GLOBREF;
5477c478bd9Sstevel@tonic-gate 
5487c478bd9Sstevel@tonic-gate 	/*
5497c478bd9Sstevel@tonic-gate 	 * Record the input filename on the referenced or defined files list
5507c478bd9Sstevel@tonic-gate 	 * for possible later diagnostics.  The `sa_rfile' pointer contains the
5517c478bd9Sstevel@tonic-gate 	 * name of the file that first referenced this symbol and is used to
5527c478bd9Sstevel@tonic-gate 	 * generate undefined symbol diagnostics (refer to sym_undef_entry()).
5537c478bd9Sstevel@tonic-gate 	 * Note that this entry can be overridden if a reference from a
5547c478bd9Sstevel@tonic-gate 	 * relocatable object is found after a reference from a shared object
5557c478bd9Sstevel@tonic-gate 	 * (refer to sym_override()).
5567c478bd9Sstevel@tonic-gate 	 * The `sa_dfiles' list is used to maintain the list of files that
5577c478bd9Sstevel@tonic-gate 	 * define the same symbol.  This list can be used for two reasons:
5587c478bd9Sstevel@tonic-gate 	 *
5597c478bd9Sstevel@tonic-gate 	 *   o	To save the first definition of a symbol that is not available
5607c478bd9Sstevel@tonic-gate 	 *	for this link-edit.
5617c478bd9Sstevel@tonic-gate 	 *
5627c478bd9Sstevel@tonic-gate 	 *   o	To save all definitions of a symbol when the -m option is in
5637c478bd9Sstevel@tonic-gate 	 *	effect.  This is optional as it is used to list multiple
5647c478bd9Sstevel@tonic-gate 	 *	(interposed) definitions of a symbol (refer to ldmap_out()),
5657c478bd9Sstevel@tonic-gate 	 *	and can be quite expensive.
5667c478bd9Sstevel@tonic-gate 	 */
5670bc07c75Srie 	if (nsym->st_shndx == SHN_UNDEF) {
5687c478bd9Sstevel@tonic-gate 		sap->sa_rfile = ifl->ifl_name;
5697c478bd9Sstevel@tonic-gate 	} else {
5707c478bd9Sstevel@tonic-gate 		if (sdp->sd_ref == REF_DYN_SEEN) {
5717c478bd9Sstevel@tonic-gate 			/*
5727c478bd9Sstevel@tonic-gate 			 * A symbol is determined to be unavailable if it
5737c478bd9Sstevel@tonic-gate 			 * belongs to a version of a shared object that this
5747c478bd9Sstevel@tonic-gate 			 * user does not wish to use, or if it belongs to an
5757c478bd9Sstevel@tonic-gate 			 * implicit shared object.
5767c478bd9Sstevel@tonic-gate 			 */
5777c478bd9Sstevel@tonic-gate 			if (ifl->ifl_vercnt) {
5789039eeafSab 				Ver_index	*vip;
5797c478bd9Sstevel@tonic-gate 				Half		vndx = ifl->ifl_versym[ndx];
5807c478bd9Sstevel@tonic-gate 
5817c478bd9Sstevel@tonic-gate 				sap->sa_dverndx = vndx;
5827c478bd9Sstevel@tonic-gate 				vip = &ifl->ifl_verndx[vndx];
5837c478bd9Sstevel@tonic-gate 				if (!(vip->vi_flags & FLG_VER_AVAIL)) {
5847c478bd9Sstevel@tonic-gate 					sdp->sd_flags |= FLG_SY_NOTAVAIL;
5857c478bd9Sstevel@tonic-gate 					sap->sa_vfile = ifl->ifl_name;
5867c478bd9Sstevel@tonic-gate 				}
5877c478bd9Sstevel@tonic-gate 			}
5887c478bd9Sstevel@tonic-gate 			if (!(ifl->ifl_flags & FLG_IF_NEEDED))
5897c478bd9Sstevel@tonic-gate 				sdp->sd_flags |= FLG_SY_NOTAVAIL;
5907c478bd9Sstevel@tonic-gate 
5917c478bd9Sstevel@tonic-gate 		} else if (etype == ET_REL) {
5927c478bd9Sstevel@tonic-gate 			/*
5937c478bd9Sstevel@tonic-gate 			 * If this symbol has been obtained from a versioned
5947c478bd9Sstevel@tonic-gate 			 * input relocatable object then the new symbol must be
5957c478bd9Sstevel@tonic-gate 			 * promoted to the versioning of the output file.
5967c478bd9Sstevel@tonic-gate 			 */
5977c478bd9Sstevel@tonic-gate 			if (ifl->ifl_versym)
5985aefb655Srie 				ld_vers_promote(sdp, ndx, ifl, ofl);
5997c478bd9Sstevel@tonic-gate 		}
6007c478bd9Sstevel@tonic-gate 
6017c478bd9Sstevel@tonic-gate 		if ((ofl->ofl_flags & FLG_OF_GENMAP) &&
6027c478bd9Sstevel@tonic-gate 		    ((sdflags & FLG_SY_SPECSEC) == 0))
6037c478bd9Sstevel@tonic-gate 			if (list_appendc(&sap->sa_dfiles, ifl->ifl_name) == 0)
6047c478bd9Sstevel@tonic-gate 				return ((Sym_desc *)S_ERROR);
6057c478bd9Sstevel@tonic-gate 	}
6067c478bd9Sstevel@tonic-gate 
60760758829Srie 	/*
60860758829Srie 	 * Provided we're not processing a mapfile, diagnose the entered symbol.
60960758829Srie 	 * Mapfile processing requires the symbol to be updated with additional
61060758829Srie 	 * information, therefore the diagnosing of the symbol is deferred until
61160758829Srie 	 * later (see Dbg_map_symbol()).
61260758829Srie 	 */
61360758829Srie 	if ((ifl == 0) || ((ifl->ifl_flags & FLG_IF_MAPFILE) == 0))
61460758829Srie 		DBG_CALL(Dbg_syms_entered(ofl, nsym, sdp));
6157c478bd9Sstevel@tonic-gate 	return (sdp);
6167c478bd9Sstevel@tonic-gate }
6177c478bd9Sstevel@tonic-gate 
6187c478bd9Sstevel@tonic-gate /*
6197c478bd9Sstevel@tonic-gate  * Add a special symbol to the symbol table.  Takes special symbol name with
6207c478bd9Sstevel@tonic-gate  * and without underscores.  This routine is called, after all other symbol
6217c478bd9Sstevel@tonic-gate  * resolution has completed, to generate a reserved absolute symbol (the
6227c478bd9Sstevel@tonic-gate  * underscore version).  Special symbols are updated with the appropriate
623dd94ecefSrie  * values in update_osym().  If the user has already defined this symbol
6247c478bd9Sstevel@tonic-gate  * issue a warning and leave the symbol as is.  If the non-underscore symbol
6257c478bd9Sstevel@tonic-gate  * is referenced then turn it into a weak alias of the underscored symbol.
6267c478bd9Sstevel@tonic-gate  *
627d579eb63Sab  * The bits in flags_u are OR'd into the flags field of the symbol
628d579eb63Sab  * for the underscored symbol.
629d579eb63Sab  *
6307c478bd9Sstevel@tonic-gate  * If this is a global symbol, and it hasn't explicitly been defined as being
6317c478bd9Sstevel@tonic-gate  * directly bound to, indicate that it can't be directly bound to.
6327c478bd9Sstevel@tonic-gate  * Historically, most special symbols only have meaning to the object in which
633c1c6f601Srie  * they exist, however, they've always been global.  To ensure compatibility
634c1c6f601Srie  * with any unexpected use presently in effect, ensure these symbols don't get
6357c478bd9Sstevel@tonic-gate  * directly bound to.  Note, that establishing this state here isn't sufficient
6367c478bd9Sstevel@tonic-gate  * to create a syminfo table, only if a syminfo table is being created by some
637c1c6f601Srie  * other symbol directives will the nodirect binding be recorded.  This ensures
6387c478bd9Sstevel@tonic-gate  * we don't create syminfo sections for all objects we create, as this might add
6397c478bd9Sstevel@tonic-gate  * unnecessary bloat to users who haven't explicitly requested extra symbol
6407c478bd9Sstevel@tonic-gate  * information.
6417c478bd9Sstevel@tonic-gate  */
6427c478bd9Sstevel@tonic-gate static uintptr_t
6437c478bd9Sstevel@tonic-gate sym_add_spec(const char *name, const char *uname, Word sdaux_id,
644d579eb63Sab     Word flags_u, Half flags1, Ofl_desc *ofl)
6457c478bd9Sstevel@tonic-gate {
6467c478bd9Sstevel@tonic-gate 	Sym_desc	*sdp;
6477c478bd9Sstevel@tonic-gate 	Sym_desc 	*usdp;
6487c478bd9Sstevel@tonic-gate 	Sym		*sym;
6497c478bd9Sstevel@tonic-gate 	Word		hash;
6507c478bd9Sstevel@tonic-gate 	avl_index_t	where;
6517c478bd9Sstevel@tonic-gate 
6527c478bd9Sstevel@tonic-gate 	/* LINTED */
6537c478bd9Sstevel@tonic-gate 	hash = (Word)elf_hash(uname);
6545aefb655Srie 	if (usdp = ld_sym_find(uname, hash, &where, ofl)) {
6557c478bd9Sstevel@tonic-gate 		/*
6567c478bd9Sstevel@tonic-gate 		 * If the underscore symbol exists and is undefined, or was
6577c478bd9Sstevel@tonic-gate 		 * defined in a shared library, convert it to a local symbol.
6587c478bd9Sstevel@tonic-gate 		 * Otherwise leave it as is and warn the user.
6597c478bd9Sstevel@tonic-gate 		 */
6607c478bd9Sstevel@tonic-gate 		if ((usdp->sd_shndx == SHN_UNDEF) ||
6617c478bd9Sstevel@tonic-gate 		    (usdp->sd_ref != REF_REL_NEED)) {
6627c478bd9Sstevel@tonic-gate 			usdp->sd_ref = REF_REL_NEED;
6637c478bd9Sstevel@tonic-gate 			usdp->sd_shndx = usdp->sd_sym->st_shndx = SHN_ABS;
664d579eb63Sab 			usdp->sd_flags |= FLG_SY_SPECSEC | flags_u;
6657c478bd9Sstevel@tonic-gate 			usdp->sd_sym->st_info =
6667c478bd9Sstevel@tonic-gate 			    ELF_ST_INFO(STB_GLOBAL, STT_OBJECT);
6677c478bd9Sstevel@tonic-gate 			usdp->sd_isc = NULL;
6687c478bd9Sstevel@tonic-gate 			usdp->sd_sym->st_size = 0;
6697c478bd9Sstevel@tonic-gate 			usdp->sd_sym->st_value = 0;
6707c478bd9Sstevel@tonic-gate 			/* LINTED */
6717c478bd9Sstevel@tonic-gate 			usdp->sd_aux->sa_symspec = (Half)sdaux_id;
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate 			/*
6749a411307Srie 			 * If a user hasn't specifically indicated that the
6759a411307Srie 			 * scope of this symbol be made local, then leave it
6769a411307Srie 			 * as global (ie. prevent automatic scoping).  The GOT
6779a411307Srie 			 * should be defined protected, whereas all other
6789a411307Srie 			 * special symbols are tagged as no-direct.
6797c478bd9Sstevel@tonic-gate 			 */
68060758829Srie 			if (((usdp->sd_flags1 & FLG_SY1_HIDDEN) == 0) &&
68160758829Srie 			    (flags1 & FLG_SY1_DEFAULT)) {
68233eb6ee1Sab 				usdp->sd_aux->sa_overndx = VER_NDX_GLOBAL;
68333eb6ee1Sab 				if (sdaux_id == SDAUX_ID_GOT) {
68433eb6ee1Sab 					usdp->sd_flags1 &= ~FLG_SY1_NDIR;
68560758829Srie 					usdp->sd_flags1 |= FLG_SY1_PROTECT;
68633eb6ee1Sab 					usdp->sd_sym->st_other = STV_PROTECTED;
68733eb6ee1Sab 				} else if (
68833eb6ee1Sab 				    ((usdp->sd_flags1 & FLG_SY1_DIR) == 0) &&
68933eb6ee1Sab 				    ((ofl->ofl_flags & FLG_OF_SYMBOLIC) == 0)) {
69033eb6ee1Sab 					usdp->sd_flags1 |= FLG_SY1_NDIR;
69133eb6ee1Sab 				}
6927c478bd9Sstevel@tonic-gate 			}
6937c478bd9Sstevel@tonic-gate 			usdp->sd_flags1 |= flags1;
6947c478bd9Sstevel@tonic-gate 
6957c478bd9Sstevel@tonic-gate 			/*
696c1c6f601Srie 			 * If the reference originated from a mapfile ensure
6977c478bd9Sstevel@tonic-gate 			 * we mark the symbol as used.
6987c478bd9Sstevel@tonic-gate 			 */
6997c478bd9Sstevel@tonic-gate 			if (usdp->sd_flags & FLG_SY_MAPREF)
7007c478bd9Sstevel@tonic-gate 				usdp->sd_flags |= FLG_SY_MAPUSED;
7017c478bd9Sstevel@tonic-gate 
7025aefb655Srie 			DBG_CALL(Dbg_syms_updated(ofl, usdp, uname));
7037c478bd9Sstevel@tonic-gate 		} else
7045aefb655Srie 			eprintf(ofl->ofl_lml, ERR_WARNING,
7055aefb655Srie 			    MSG_INTL(MSG_SYM_RESERVE), uname,
7067c478bd9Sstevel@tonic-gate 			    usdp->sd_file->ifl_name);
7077c478bd9Sstevel@tonic-gate 	} else {
7087c478bd9Sstevel@tonic-gate 		/*
7097c478bd9Sstevel@tonic-gate 		 * If the symbol does not exist create it.
7107c478bd9Sstevel@tonic-gate 		 */
7117c478bd9Sstevel@tonic-gate 		if ((sym = libld_calloc(sizeof (Sym), 1)) == 0)
7127c478bd9Sstevel@tonic-gate 			return (S_ERROR);
7137c478bd9Sstevel@tonic-gate 		sym->st_shndx = SHN_ABS;
7147c478bd9Sstevel@tonic-gate 		sym->st_info = ELF_ST_INFO(STB_GLOBAL, STT_OBJECT);
7157c478bd9Sstevel@tonic-gate 		sym->st_size = 0;
7167c478bd9Sstevel@tonic-gate 		sym->st_value = 0;
7175aefb655Srie 		DBG_CALL(Dbg_syms_created(ofl->ofl_lml, uname));
7185aefb655Srie 		if ((usdp = ld_sym_enter(uname, sym, hash, (Ifl_desc *)NULL,
719d579eb63Sab 		    ofl, 0, SHN_ABS, FLG_SY_SPECSEC | flags_u, 0, &where)) ==
7207c478bd9Sstevel@tonic-gate 		    (Sym_desc *)S_ERROR)
7217c478bd9Sstevel@tonic-gate 			return (S_ERROR);
7227c478bd9Sstevel@tonic-gate 		usdp->sd_ref = REF_REL_NEED;
7237c478bd9Sstevel@tonic-gate 		/* LINTED */
7247c478bd9Sstevel@tonic-gate 		usdp->sd_aux->sa_symspec = (Half)sdaux_id;
7257c478bd9Sstevel@tonic-gate 
7267c478bd9Sstevel@tonic-gate 		usdp->sd_aux->sa_overndx = VER_NDX_GLOBAL;
7279a411307Srie 
7289a411307Srie 		if (sdaux_id == SDAUX_ID_GOT) {
72960758829Srie 			usdp->sd_flags1 |= FLG_SY1_PROTECT;
7309a411307Srie 			usdp->sd_sym->st_other = STV_PROTECTED;
73160758829Srie 		} else if ((flags1 & FLG_SY1_DEFAULT) &&
7329a411307Srie 		    ((ofl->ofl_flags & FLG_OF_SYMBOLIC) == 0)) {
7337c478bd9Sstevel@tonic-gate 			usdp->sd_flags1 |= FLG_SY1_NDIR;
7349a411307Srie 		}
7357c478bd9Sstevel@tonic-gate 		usdp->sd_flags1 |= flags1;
7367c478bd9Sstevel@tonic-gate 	}
7377c478bd9Sstevel@tonic-gate 
7385aefb655Srie 	if (name && (sdp = ld_sym_find(name, SYM_NOHASH, 0, ofl)) &&
7390bc07c75Srie 	    (sdp->sd_sym->st_shndx == SHN_UNDEF)) {
7407c478bd9Sstevel@tonic-gate 		uchar_t	bind;
7417c478bd9Sstevel@tonic-gate 
7427c478bd9Sstevel@tonic-gate 		/*
7437c478bd9Sstevel@tonic-gate 		 * If the non-underscore symbol exists and is undefined
7447c478bd9Sstevel@tonic-gate 		 * convert it to be a local.  If the underscore has
7457c478bd9Sstevel@tonic-gate 		 * sa_symspec set (ie. it was created above) then simulate this
7467c478bd9Sstevel@tonic-gate 		 * as a weak alias.
7477c478bd9Sstevel@tonic-gate 		 */
7487c478bd9Sstevel@tonic-gate 		sdp->sd_ref = REF_REL_NEED;
7497c478bd9Sstevel@tonic-gate 		sdp->sd_shndx = sdp->sd_sym->st_shndx = SHN_ABS;
7507c478bd9Sstevel@tonic-gate 		sdp->sd_flags |= FLG_SY_SPECSEC;
7517c478bd9Sstevel@tonic-gate 		sdp->sd_isc = NULL;
7527c478bd9Sstevel@tonic-gate 		sdp->sd_sym->st_size = 0;
7537c478bd9Sstevel@tonic-gate 		sdp->sd_sym->st_value = 0;
7547c478bd9Sstevel@tonic-gate 		/* LINTED */
7557c478bd9Sstevel@tonic-gate 		sdp->sd_aux->sa_symspec = (Half)sdaux_id;
7567c478bd9Sstevel@tonic-gate 		if (usdp->sd_aux->sa_symspec) {
7577c478bd9Sstevel@tonic-gate 			usdp->sd_aux->sa_linkndx = 0;
7587c478bd9Sstevel@tonic-gate 			sdp->sd_aux->sa_linkndx = 0;
7597c478bd9Sstevel@tonic-gate 			bind = STB_WEAK;
7607c478bd9Sstevel@tonic-gate 		} else
7617c478bd9Sstevel@tonic-gate 			bind = STB_GLOBAL;
7627c478bd9Sstevel@tonic-gate 		sdp->sd_sym->st_info = ELF_ST_INFO(bind, STT_OBJECT);
7637c478bd9Sstevel@tonic-gate 
7647c478bd9Sstevel@tonic-gate 		/*
7659a411307Srie 		 * If a user hasn't specifically indicated the scope of this
7669a411307Srie 		 * symbol be made local then leave it as global (ie. prevent
7679a411307Srie 		 * automatic scoping).  The GOT should be defined protected,
7689a411307Srie 		 * whereas all other special symbols are tagged as no-direct.
7697c478bd9Sstevel@tonic-gate 		 */
77060758829Srie 		if (((sdp->sd_flags1 & FLG_SY1_HIDDEN) == 0) &&
77160758829Srie 		    (flags1 & FLG_SY1_DEFAULT)) {
7727c478bd9Sstevel@tonic-gate 			sdp->sd_aux->sa_overndx = VER_NDX_GLOBAL;
7739a411307Srie 			if (sdaux_id == SDAUX_ID_GOT) {
7749a411307Srie 				sdp->sd_flags1 &= ~FLG_SY1_NDIR;
77560758829Srie 				sdp->sd_flags1 |= FLG_SY1_PROTECT;
7769a411307Srie 				sdp->sd_sym->st_other = STV_PROTECTED;
7779a411307Srie 			} else if (((sdp->sd_flags1 & FLG_SY1_DIR) == 0) &&
7789a411307Srie 			    ((ofl->ofl_flags & FLG_OF_SYMBOLIC) == 0)) {
7797c478bd9Sstevel@tonic-gate 				sdp->sd_flags1 |= FLG_SY1_NDIR;
7809a411307Srie 			}
7817c478bd9Sstevel@tonic-gate 		}
7827c478bd9Sstevel@tonic-gate 		sdp->sd_flags1 |= flags1;
7837c478bd9Sstevel@tonic-gate 
7847c478bd9Sstevel@tonic-gate 		/*
785c1c6f601Srie 		 * If the reference originated from a mapfile ensure
7867c478bd9Sstevel@tonic-gate 		 * we mark the symbol as used.
7877c478bd9Sstevel@tonic-gate 		 */
7887c478bd9Sstevel@tonic-gate 		if (sdp->sd_flags & FLG_SY_MAPREF)
7897c478bd9Sstevel@tonic-gate 			sdp->sd_flags |= FLG_SY_MAPUSED;
7907c478bd9Sstevel@tonic-gate 
7915aefb655Srie 		DBG_CALL(Dbg_syms_updated(ofl, sdp, name));
7927c478bd9Sstevel@tonic-gate 	}
7937c478bd9Sstevel@tonic-gate 	return (1);
7947c478bd9Sstevel@tonic-gate }
7957c478bd9Sstevel@tonic-gate 
7967c478bd9Sstevel@tonic-gate 
7977c478bd9Sstevel@tonic-gate /*
7987c478bd9Sstevel@tonic-gate  * Print undefined symbols.
7997c478bd9Sstevel@tonic-gate  */
8007c478bd9Sstevel@tonic-gate static Boolean	undef_title = TRUE;
8017c478bd9Sstevel@tonic-gate 
8025aefb655Srie static void
8035aefb655Srie sym_undef_title(Ofl_desc *ofl)
8047c478bd9Sstevel@tonic-gate {
8055aefb655Srie 	eprintf(ofl->ofl_lml, ERR_NONE, MSG_INTL(MSG_SYM_FMT_UNDEF),
80633eb6ee1Sab 	    MSG_INTL(MSG_SYM_UNDEF_ITM_11),
80733eb6ee1Sab 	    MSG_INTL(MSG_SYM_UNDEF_ITM_21),
80833eb6ee1Sab 	    MSG_INTL(MSG_SYM_UNDEF_ITM_12),
80933eb6ee1Sab 	    MSG_INTL(MSG_SYM_UNDEF_ITM_22));
8107c478bd9Sstevel@tonic-gate 
8117c478bd9Sstevel@tonic-gate 	undef_title = FALSE;
8127c478bd9Sstevel@tonic-gate }
8137c478bd9Sstevel@tonic-gate 
8147c478bd9Sstevel@tonic-gate /*
8157c478bd9Sstevel@tonic-gate  * Undefined symbols can fall into one of four types:
8167c478bd9Sstevel@tonic-gate  *
8177c478bd9Sstevel@tonic-gate  *  o	the symbol is really undefined (SHN_UNDEF).
8187c478bd9Sstevel@tonic-gate  *
8197c478bd9Sstevel@tonic-gate  *  o	versioning has been enabled, however this symbol has not been assigned
8207c478bd9Sstevel@tonic-gate  *	to one of the defined versions.
8217c478bd9Sstevel@tonic-gate  *
8227c478bd9Sstevel@tonic-gate  *  o	the symbol has been defined by an implicitly supplied library, ie. one
8237c478bd9Sstevel@tonic-gate  *	which was encounted because it was NEEDED by another library, rather
8247c478bd9Sstevel@tonic-gate  * 	than from a command line supplied library which would become the only
8257c478bd9Sstevel@tonic-gate  *	dependency of the output file being produced.
8267c478bd9Sstevel@tonic-gate  *
8277c478bd9Sstevel@tonic-gate  *  o	the symbol has been defined by a version of a shared object that is
8287c478bd9Sstevel@tonic-gate  *	not permitted for this link-edit.
8297c478bd9Sstevel@tonic-gate  *
8307c478bd9Sstevel@tonic-gate  * In all cases the file who made the first reference to this symbol will have
8317c478bd9Sstevel@tonic-gate  * been recorded via the `sa_rfile' pointer.
8327c478bd9Sstevel@tonic-gate  */
8337c478bd9Sstevel@tonic-gate typedef enum {
8347c478bd9Sstevel@tonic-gate 	UNDEF,		NOVERSION,	IMPLICIT,	NOTAVAIL,
8357c478bd9Sstevel@tonic-gate 	BNDLOCAL
8367c478bd9Sstevel@tonic-gate } Type;
8377c478bd9Sstevel@tonic-gate 
8387c478bd9Sstevel@tonic-gate static const Msg format[] = {
8397c478bd9Sstevel@tonic-gate 	MSG_SYM_UND_UNDEF,		/* MSG_INTL(MSG_SYM_UND_UNDEF) */
8407c478bd9Sstevel@tonic-gate 	MSG_SYM_UND_NOVER,		/* MSG_INTL(MSG_SYM_UND_NOVER) */
8417c478bd9Sstevel@tonic-gate 	MSG_SYM_UND_IMPL,		/* MSG_INTL(MSG_SYM_UND_IMPL) */
8427c478bd9Sstevel@tonic-gate 	MSG_SYM_UND_NOTA,		/* MSG_INTL(MSG_SYM_UND_NOTA) */
8437c478bd9Sstevel@tonic-gate 	MSG_SYM_UND_BNDLOCAL		/* MSG_INTL(MSG_SYM_UND_BNDLOCAL) */
8447c478bd9Sstevel@tonic-gate };
8457c478bd9Sstevel@tonic-gate 
8465aefb655Srie static void
8475aefb655Srie sym_undef_entry(Ofl_desc *ofl, Sym_desc *sdp, Type type)
8487c478bd9Sstevel@tonic-gate {
8497c478bd9Sstevel@tonic-gate 	const char	*name1, *name2, *name3;
8507c478bd9Sstevel@tonic-gate 	Ifl_desc	*ifl = sdp->sd_file;
8517c478bd9Sstevel@tonic-gate 	Sym_aux		*sap = sdp->sd_aux;
8527c478bd9Sstevel@tonic-gate 
8537c478bd9Sstevel@tonic-gate 	if (undef_title)
8545aefb655Srie 		sym_undef_title(ofl);
8557c478bd9Sstevel@tonic-gate 
8567c478bd9Sstevel@tonic-gate 	switch (type) {
8577c478bd9Sstevel@tonic-gate 	case UNDEF:
8587c478bd9Sstevel@tonic-gate 	case BNDLOCAL:
8597c478bd9Sstevel@tonic-gate 		name1 = sap->sa_rfile;
8607c478bd9Sstevel@tonic-gate 		break;
8617c478bd9Sstevel@tonic-gate 	case NOVERSION:
8627c478bd9Sstevel@tonic-gate 		name1 = ifl->ifl_name;
8637c478bd9Sstevel@tonic-gate 		break;
8647c478bd9Sstevel@tonic-gate 	case IMPLICIT:
8657c478bd9Sstevel@tonic-gate 		name1 = sap->sa_rfile;
8667c478bd9Sstevel@tonic-gate 		name2 = ifl->ifl_name;
8677c478bd9Sstevel@tonic-gate 		break;
8687c478bd9Sstevel@tonic-gate 	case NOTAVAIL:
8697c478bd9Sstevel@tonic-gate 		name1 = sap->sa_rfile;
8707c478bd9Sstevel@tonic-gate 		name2 = sap->sa_vfile;
8717c478bd9Sstevel@tonic-gate 		name3 = ifl->ifl_verndx[sap->sa_dverndx].vi_name;
8727c478bd9Sstevel@tonic-gate 		break;
8737c478bd9Sstevel@tonic-gate 	default:
8747c478bd9Sstevel@tonic-gate 		return;
8757c478bd9Sstevel@tonic-gate 	}
8767c478bd9Sstevel@tonic-gate 
8775aefb655Srie 	eprintf(ofl->ofl_lml, ERR_NONE, MSG_INTL(format[type]),
8785aefb655Srie 	    demangle(sdp->sd_name), name1, name2, name3);
8797c478bd9Sstevel@tonic-gate }
8807c478bd9Sstevel@tonic-gate 
8817c478bd9Sstevel@tonic-gate /*
8827c478bd9Sstevel@tonic-gate  * At this point all symbol input processing has been completed, therefore
8837c478bd9Sstevel@tonic-gate  * complete the symbol table entries by generating any necessary internal
8847c478bd9Sstevel@tonic-gate  * symbols.
8857c478bd9Sstevel@tonic-gate  */
8867c478bd9Sstevel@tonic-gate uintptr_t
8875aefb655Srie ld_sym_spec(Ofl_desc *ofl)
8887c478bd9Sstevel@tonic-gate {
8899a411307Srie 	Sym_desc	*sdp;
8907c478bd9Sstevel@tonic-gate 
8919a411307Srie 	if (ofl->ofl_flags & FLG_OF_RELOBJ)
8929a411307Srie 		return (1);
8937c478bd9Sstevel@tonic-gate 
8949a411307Srie 	DBG_CALL(Dbg_syms_spec_title(ofl->ofl_lml));
8959a411307Srie 
8969a411307Srie 	if (sym_add_spec(MSG_ORIG(MSG_SYM_ETEXT), MSG_ORIG(MSG_SYM_ETEXT_U),
89760758829Srie 	    SDAUX_ID_ETEXT, 0, (FLG_SY1_DEFAULT | FLG_SY1_EXPDEF),
89860758829Srie 	    ofl) == S_ERROR)
8999a411307Srie 		return (S_ERROR);
9009a411307Srie 	if (sym_add_spec(MSG_ORIG(MSG_SYM_EDATA), MSG_ORIG(MSG_SYM_EDATA_U),
90160758829Srie 	    SDAUX_ID_EDATA, 0, (FLG_SY1_DEFAULT | FLG_SY1_EXPDEF),
90260758829Srie 	    ofl) == S_ERROR)
9039a411307Srie 		return (S_ERROR);
9049a411307Srie 	if (sym_add_spec(MSG_ORIG(MSG_SYM_END), MSG_ORIG(MSG_SYM_END_U),
90560758829Srie 	    SDAUX_ID_END, FLG_SY_DYNSORT, (FLG_SY1_DEFAULT | FLG_SY1_EXPDEF),
90660758829Srie 	    ofl) == S_ERROR)
9079a411307Srie 		return (S_ERROR);
9089a411307Srie 	if (sym_add_spec(MSG_ORIG(MSG_SYM_L_END), MSG_ORIG(MSG_SYM_L_END_U),
90960758829Srie 	    SDAUX_ID_END, 0, FLG_SY1_HIDDEN, ofl) == S_ERROR)
9109a411307Srie 		return (S_ERROR);
9119a411307Srie 	if (sym_add_spec(MSG_ORIG(MSG_SYM_L_START), MSG_ORIG(MSG_SYM_L_START_U),
91260758829Srie 	    SDAUX_ID_START, 0, FLG_SY1_HIDDEN, ofl) == S_ERROR)
9139a411307Srie 		return (S_ERROR);
9147c478bd9Sstevel@tonic-gate 
9159a411307Srie 	/*
9169a411307Srie 	 * Historically we've always produced a _DYNAMIC symbol, even for
9179a411307Srie 	 * static executables (in which case its value will be 0).
9189a411307Srie 	 */
9199a411307Srie 	if (sym_add_spec(MSG_ORIG(MSG_SYM_DYNAMIC), MSG_ORIG(MSG_SYM_DYNAMIC_U),
92060758829Srie 	    SDAUX_ID_DYN, FLG_SY_DYNSORT, (FLG_SY1_DEFAULT | FLG_SY1_EXPDEF),
92160758829Srie 	    ofl) == S_ERROR)
9229a411307Srie 		return (S_ERROR);
9239a411307Srie 
924d579eb63Sab 	if (OFL_ALLOW_DYNSYM(ofl))
9259a411307Srie 		if (sym_add_spec(MSG_ORIG(MSG_SYM_PLKTBL),
9269a411307Srie 		    MSG_ORIG(MSG_SYM_PLKTBL_U), SDAUX_ID_PLT,
92760758829Srie 		    FLG_SY_DYNSORT, (FLG_SY1_DEFAULT | FLG_SY1_EXPDEF),
92860758829Srie 		    ofl) == S_ERROR)
9297c478bd9Sstevel@tonic-gate 			return (S_ERROR);
9307c478bd9Sstevel@tonic-gate 
9319a411307Srie 	/*
9329a411307Srie 	 * A GOT reference will be accompanied by the associated GOT symbol.
9339a411307Srie 	 * Make sure it gets assigned the appropriate special attributes.
9349a411307Srie 	 */
9359a411307Srie 	if (((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_GOFTBL_U),
9369a411307Srie 	    SYM_NOHASH, 0, ofl)) != 0) && (sdp->sd_ref != REF_DYN_SEEN)) {
9379a411307Srie 		if (sym_add_spec(MSG_ORIG(MSG_SYM_GOFTBL),
938d579eb63Sab 		    MSG_ORIG(MSG_SYM_GOFTBL_U), SDAUX_ID_GOT, FLG_SY_DYNSORT,
93960758829Srie 		    (FLG_SY1_DEFAULT | FLG_SY1_EXPDEF), ofl) == S_ERROR)
9409a411307Srie 			return (S_ERROR);
9417c478bd9Sstevel@tonic-gate 	}
9429a411307Srie 
9437c478bd9Sstevel@tonic-gate 	return (1);
9447c478bd9Sstevel@tonic-gate }
9457c478bd9Sstevel@tonic-gate 
9467c478bd9Sstevel@tonic-gate /*
9477c478bd9Sstevel@tonic-gate  * This routine checks to see if a symbols visibility needs to be reduced to
9487c478bd9Sstevel@tonic-gate  * either SYMBOLIC or LOCAL.  This routine can be called from either
9497c478bd9Sstevel@tonic-gate  * reloc_init() or sym_validate().
9507c478bd9Sstevel@tonic-gate  */
9517c478bd9Sstevel@tonic-gate void
9525aefb655Srie ld_sym_adjust_vis(Sym_desc *sdp, Ofl_desc *ofl)
9537c478bd9Sstevel@tonic-gate {
95460758829Srie 	Word	oflags = ofl->ofl_flags, oflags1 = ofl->ofl_flags1;
9557c478bd9Sstevel@tonic-gate 	Sym	*sym = sdp->sd_sym;
9567c478bd9Sstevel@tonic-gate 
9570bc07c75Srie 	if ((sdp->sd_ref == REF_REL_NEED) &&
9580bc07c75Srie 	    (sdp->sd_sym->st_shndx != SHN_UNDEF)) {
9597c478bd9Sstevel@tonic-gate 		/*
96060758829Srie 		 * If auto-reduction/elimination is enabled, reduce any
96160758829Srie 		 * non-versioned global symbols.  This routine is called either
96260758829Srie 		 * from any initial relocation processing that references this
96360758829Srie 		 * symbol, or from the symbol validation processing.
96460758829Srie 		 *
96560758829Srie 		 * A symbol is a candidate for auto-reduction/elimination if:
96660758829Srie 		 *
96760758829Srie 		 *   .  the symbol wasn't explicitly defined within a mapfile
96860758829Srie 		 *	(in which case all the necessary state has been applied
96960758829Srie 		 *	to the symbol), or
97060758829Srie 		 *   .	the symbol isn't one of the family of reserved
97160758829Srie 		 *	special symbols (ie. _end, _etext, etc.), or
97260758829Srie 		 *   .	the symbol isn't a SINGLETON, or
97360758829Srie 		 *   .  the symbol wasn't explicitly defined within a version
97460758829Srie 		 *	definition associated with an input relocatable object.
97560758829Srie 		 *
9767c478bd9Sstevel@tonic-gate 		 * Indicate that the symbol has been reduced as it may be
9777c478bd9Sstevel@tonic-gate 		 * necessary to print these symbols later.
9787c478bd9Sstevel@tonic-gate 		 */
9797c478bd9Sstevel@tonic-gate 		if (((oflags & FLG_OF_AUTOLCL) ||
9807c478bd9Sstevel@tonic-gate 		    (oflags1 & FLG_OF1_AUTOELM)) &&
98160758829Srie 		    ((sdp->sd_flags1 & MSK_SY1_NOAUTO) == 0)) {
98260758829Srie 			if ((sdp->sd_flags1 & FLG_SY1_HIDDEN) == 0) {
98360758829Srie 				sdp->sd_flags |= FLG_SY_REDUCED;
98460758829Srie 				sdp->sd_flags1 |= FLG_SY1_HIDDEN;
98560758829Srie 			}
9867c478bd9Sstevel@tonic-gate 
9877c478bd9Sstevel@tonic-gate 			if (ofl->ofl_flags1 &
98860758829Srie 			    (FLG_OF1_REDLSYM | FLG_OF1_AUTOELM)) {
9897c478bd9Sstevel@tonic-gate 				sdp->sd_flags1 |= FLG_SY1_ELIM;
99060758829Srie 				sym->st_other = STV_ELIMINATE |
99160758829Srie 				    (sym->st_other & ~MSK_SYM_VISIBILITY);
99260758829Srie 			} else if (ELF_ST_VISIBILITY(sym->st_other) !=
99360758829Srie 			    STV_INTERNAL)
99460758829Srie 				sym->st_other = STV_HIDDEN |
99560758829Srie 				    (sym->st_other & ~MSK_SYM_VISIBILITY);
9967c478bd9Sstevel@tonic-gate 		}
9977c478bd9Sstevel@tonic-gate 
9987c478bd9Sstevel@tonic-gate 		/*
9999a411307Srie 		 * If -Bsymbolic is in effect, and the symbol hasn't explicitly
10009a411307Srie 		 * been defined nodirect (via a mapfile), then bind the global
10019a411307Srie 		 * symbol symbolically and assign the STV_PROTECTED visibility
10027c478bd9Sstevel@tonic-gate 		 * attribute.
10037c478bd9Sstevel@tonic-gate 		 */
10047c478bd9Sstevel@tonic-gate 		if ((oflags & FLG_OF_SYMBOLIC) &&
100560758829Srie 		    ((sdp->sd_flags1 & (FLG_SY1_HIDDEN | FLG_SY1_NDIR)) == 0)) {
100660758829Srie 			sdp->sd_flags1 |= FLG_SY1_PROTECT;
10077c478bd9Sstevel@tonic-gate 			if (ELF_ST_VISIBILITY(sym->st_other) == STV_DEFAULT)
10087c478bd9Sstevel@tonic-gate 				sym->st_other = STV_PROTECTED |
10097c478bd9Sstevel@tonic-gate 				    (sym->st_other & ~MSK_SYM_VISIBILITY);
10107c478bd9Sstevel@tonic-gate 		}
10117c478bd9Sstevel@tonic-gate 	}
10127c478bd9Sstevel@tonic-gate 
10137c478bd9Sstevel@tonic-gate 	/*
10147c478bd9Sstevel@tonic-gate 	 * Indicate that this symbol has had it's visibility checked so that
10157c478bd9Sstevel@tonic-gate 	 * we don't need to do this investigation again.
10167c478bd9Sstevel@tonic-gate 	 */
10177c478bd9Sstevel@tonic-gate 	sdp->sd_flags |= FLG_SY_VISIBLE;
10187c478bd9Sstevel@tonic-gate }
10197c478bd9Sstevel@tonic-gate 
1020c1c6f601Srie /*
1021c1c6f601Srie  * Make sure a symbol definition is local to the object being built.
1022c1c6f601Srie  */
1023c1c6f601Srie static int
1024c1c6f601Srie ensure_sym_local(Ofl_desc *ofl, Sym_desc *sdp, const char *str)
1025c1c6f601Srie {
1026c1c6f601Srie 	if (sdp->sd_sym->st_shndx == SHN_UNDEF) {
1027c1c6f601Srie 		if (str) {
1028c1c6f601Srie 			eprintf(ofl->ofl_lml, ERR_FATAL,
1029c1c6f601Srie 			    MSG_INTL(MSG_SYM_UNDEF), str,
1030c1c6f601Srie 			    demangle((char *)sdp->sd_name));
1031c1c6f601Srie 		}
1032c1c6f601Srie 		return (1);
1033c1c6f601Srie 	}
1034c1c6f601Srie 	if (sdp->sd_ref != REF_REL_NEED) {
1035c1c6f601Srie 		if (str) {
1036c1c6f601Srie 			eprintf(ofl->ofl_lml, ERR_FATAL,
1037c1c6f601Srie 			    MSG_INTL(MSG_SYM_EXTERN), str,
1038c1c6f601Srie 			    demangle((char *)sdp->sd_name),
1039c1c6f601Srie 			    sdp->sd_file->ifl_name);
1040c1c6f601Srie 		}
1041c1c6f601Srie 		return (1);
1042c1c6f601Srie 	}
1043c1c6f601Srie 
1044c1c6f601Srie 	sdp->sd_flags |= FLG_SY_UPREQD;
1045c1c6f601Srie 	if (sdp->sd_isc) {
1046c1c6f601Srie 		sdp->sd_isc->is_flags |= FLG_IS_SECTREF;
1047c1c6f601Srie 		sdp->sd_isc->is_file->ifl_flags |= FLG_IF_FILEREF;
1048c1c6f601Srie 	}
1049c1c6f601Srie 	return (0);
1050c1c6f601Srie }
1051c1c6f601Srie 
1052c1c6f601Srie /*
1053c1c6f601Srie  * Make sure all the symbol definitions required for initarray, finiarray, or
1054c1c6f601Srie  * preinitarray's are local to the object being built.
1055c1c6f601Srie  */
1056c1c6f601Srie static int
1057c1c6f601Srie ensure_array_local(Ofl_desc *ofl, List *list, const char *str)
1058c1c6f601Srie {
1059c1c6f601Srie 	Listnode	*lnp;
1060c1c6f601Srie 	Sym_desc	*sdp;
1061c1c6f601Srie 	int		ret = 0;
1062c1c6f601Srie 
1063c1c6f601Srie 	for (LIST_TRAVERSE(list, lnp, sdp))
1064c1c6f601Srie 		ret += ensure_sym_local(ofl, sdp, str);
1065c1c6f601Srie 
1066c1c6f601Srie 	return (ret);
1067c1c6f601Srie }
1068c1c6f601Srie 
10697c478bd9Sstevel@tonic-gate /*
10707c478bd9Sstevel@tonic-gate  * After all symbol table input processing has been finished, and all relocation
10717c478bd9Sstevel@tonic-gate  * counting has been carried out (ie. no more symbols will be read, generated,
10727c478bd9Sstevel@tonic-gate  * or modified), validate and count the relevant entries:
10737c478bd9Sstevel@tonic-gate  *
10747c478bd9Sstevel@tonic-gate  *	o	check and print any undefined symbols remaining.  Note that
10757c478bd9Sstevel@tonic-gate  *		if a symbol has been defined by virtue of the inclusion of
10767c478bd9Sstevel@tonic-gate  *		an implicit shared library, it is still classed as undefined.
10777c478bd9Sstevel@tonic-gate  *
10787c478bd9Sstevel@tonic-gate  * 	o	count the number of global needed symbols together with the
10797c478bd9Sstevel@tonic-gate  *		size of their associated name strings (if scoping has been
10807c478bd9Sstevel@tonic-gate  *		indicated these symbols may be reduced to locals).
10817c478bd9Sstevel@tonic-gate  *
10827c478bd9Sstevel@tonic-gate  *	o	establish the size and alignment requirements for the global
10837c478bd9Sstevel@tonic-gate  *		.bss section (the alignment of this section is based on the
10847c478bd9Sstevel@tonic-gate  *		first symbol that it will contain).
10857c478bd9Sstevel@tonic-gate  */
10867c478bd9Sstevel@tonic-gate uintptr_t
10875aefb655Srie ld_sym_validate(Ofl_desc *ofl)
10887c478bd9Sstevel@tonic-gate {
10897c478bd9Sstevel@tonic-gate 	Sym_avlnode	*sav;
10907c478bd9Sstevel@tonic-gate 	Sym_desc	*sdp;
10917c478bd9Sstevel@tonic-gate 	Sym		*sym;
10927c478bd9Sstevel@tonic-gate 	Word		oflags = ofl->ofl_flags;
10937c478bd9Sstevel@tonic-gate 	Word		undef = 0, needed = 0, verdesc = 0;
10947c478bd9Sstevel@tonic-gate 	Xword		bssalign = 0, tlsalign = 0;
10957c478bd9Sstevel@tonic-gate 	Xword		bsssize = 0, tlssize = 0;
1096*ba2be530Sab #if	defined(_ELF64)
109754d82594Sseizo 	Xword		lbssalign = 0, lbsssize = 0;
109854d82594Sseizo #endif
1099c1c6f601Srie 	int		ret;
11009039eeafSab 	int		allow_ldynsym;
1101d579eb63Sab 	uchar_t		type;
11027c478bd9Sstevel@tonic-gate 
11037c478bd9Sstevel@tonic-gate 	/*
11047c478bd9Sstevel@tonic-gate 	 * If a symbol is undefined and this link-edit calls for no undefined
11057c478bd9Sstevel@tonic-gate 	 * symbols to remain (this is the default case when generating an
11067c478bd9Sstevel@tonic-gate 	 * executable but can be enforced for any object using -z defs), the
11077c478bd9Sstevel@tonic-gate 	 * symbol is classified as undefined and a fatal error condition will
11087c478bd9Sstevel@tonic-gate 	 * be indicated.
11097c478bd9Sstevel@tonic-gate 	 *
11107c478bd9Sstevel@tonic-gate 	 * If the symbol is undefined and we're creating a shared object with
11117c478bd9Sstevel@tonic-gate 	 * the -Bsymbolic flag, then the symbol is also classified as undefined
11127c478bd9Sstevel@tonic-gate 	 * and a warning condition will be indicated.
11137c478bd9Sstevel@tonic-gate 	 */
11147c478bd9Sstevel@tonic-gate 	if ((oflags & (FLG_OF_SHAROBJ | FLG_OF_SYMBOLIC)) ==
11157c478bd9Sstevel@tonic-gate 	    (FLG_OF_SHAROBJ | FLG_OF_SYMBOLIC))
11167c478bd9Sstevel@tonic-gate 		undef = FLG_OF_WARN;
11177c478bd9Sstevel@tonic-gate 	if (oflags & FLG_OF_NOUNDEF)
11187c478bd9Sstevel@tonic-gate 		undef = FLG_OF_FATAL;
11197c478bd9Sstevel@tonic-gate 
11207c478bd9Sstevel@tonic-gate 	/*
11217c478bd9Sstevel@tonic-gate 	 * If the symbol is referenced from an implicitly included shared object
11227c478bd9Sstevel@tonic-gate 	 * (ie. it's not on the NEEDED list) then the symbol is also classified
11237c478bd9Sstevel@tonic-gate 	 * as undefined and a fatal error condition will be indicated.
11247c478bd9Sstevel@tonic-gate 	 */
11257c478bd9Sstevel@tonic-gate 	if ((oflags & FLG_OF_NOUNDEF) || !(oflags & FLG_OF_SHAROBJ))
11267c478bd9Sstevel@tonic-gate 		needed = FLG_OF_FATAL;
11277c478bd9Sstevel@tonic-gate 
11287c478bd9Sstevel@tonic-gate 	/*
11297c478bd9Sstevel@tonic-gate 	 * If the output image is being versioned all symbol definitions must be
11307c478bd9Sstevel@tonic-gate 	 * associated with a version.  Any symbol that isn't is classified as
11317c478bd9Sstevel@tonic-gate 	 * undefined and a fatal error condition will be indicated.
11327c478bd9Sstevel@tonic-gate 	 */
11337c478bd9Sstevel@tonic-gate 	if ((oflags & FLG_OF_VERDEF) && (ofl->ofl_vercnt > VER_NDX_GLOBAL))
11347c478bd9Sstevel@tonic-gate 		verdesc = FLG_OF_FATAL;
11357c478bd9Sstevel@tonic-gate 
11369039eeafSab 	allow_ldynsym = OFL_ALLOW_LDYNSYM(ofl);
1137d579eb63Sab 
1138d579eb63Sab 	if (allow_ldynsym) {
1139d579eb63Sab 		/*
1140d579eb63Sab 		 * Normally, we disallow symbols with 0 size from appearing
1141d579eb63Sab 		 * in a dyn[sym|tls]sort section. However, there are some
1142d579eb63Sab 		 * symbols that serve special purposes that we want to exempt
1143d579eb63Sab 		 * from this rule. Look them up, and set their
1144d579eb63Sab 		 * FLG_SY_DYNSORT flag.
1145d579eb63Sab 		 */
1146d579eb63Sab 		static const char *special[] = {
1147d579eb63Sab 			MSG_ORIG(MSG_SYM_INIT_U),	/* _init */
1148d579eb63Sab 			MSG_ORIG(MSG_SYM_FINI_U),	/* _fini */
1149d579eb63Sab 			MSG_ORIG(MSG_SYM_START),	/* _start */
1150d579eb63Sab 			NULL
1151d579eb63Sab 		};
1152d579eb63Sab 		int i;
1153d579eb63Sab 
1154d579eb63Sab 		for (i = 0; special[i] != NULL; i++) {
1155d579eb63Sab 			if (((sdp = ld_sym_find(special[i],
1156d579eb63Sab 			    SYM_NOHASH, 0, ofl)) != NULL) &&
1157d579eb63Sab 			    (sdp->sd_sym->st_size == 0)) {
1158d579eb63Sab 				if (ld_sym_copy(sdp) == S_ERROR)
1159d579eb63Sab 					return (S_ERROR);
1160d579eb63Sab 				sdp->sd_flags |= FLG_SY_DYNSORT;
1161d579eb63Sab 			}
1162d579eb63Sab 		}
1163d579eb63Sab 	}
1164d579eb63Sab 
11657c478bd9Sstevel@tonic-gate 	/*
11667c478bd9Sstevel@tonic-gate 	 * Collect and validate the globals from the internal symbol table.
11677c478bd9Sstevel@tonic-gate 	 */
11687c478bd9Sstevel@tonic-gate 	for (sav = avl_first(&ofl->ofl_symavl); sav;
11697c478bd9Sstevel@tonic-gate 	    sav = AVL_NEXT(&ofl->ofl_symavl, sav)) {
117060758829Srie 		Is_desc		*isp;
11710bc07c75Srie 		int		undeferr = 0;
117260758829Srie 		uchar_t		vis;
11737c478bd9Sstevel@tonic-gate 
11747c478bd9Sstevel@tonic-gate 		sdp = sav->sav_symdesc;
11757c478bd9Sstevel@tonic-gate 
11767c478bd9Sstevel@tonic-gate 		/*
11777c478bd9Sstevel@tonic-gate 		 * If undefined symbols are allowed ignore any symbols that are
11787c478bd9Sstevel@tonic-gate 		 * not needed.
11797c478bd9Sstevel@tonic-gate 		 */
11807c478bd9Sstevel@tonic-gate 		if (!(oflags & FLG_OF_NOUNDEF) &&
11817c478bd9Sstevel@tonic-gate 		    (sdp->sd_ref == REF_DYN_SEEN))
11827c478bd9Sstevel@tonic-gate 			continue;
11837c478bd9Sstevel@tonic-gate 
11847c478bd9Sstevel@tonic-gate 		/*
11857c478bd9Sstevel@tonic-gate 		 * If the symbol originates from an external or parent mapfile
11867c478bd9Sstevel@tonic-gate 		 * reference and hasn't been matched to a reference from a
11877c478bd9Sstevel@tonic-gate 		 * relocatable object, ignore it.
11887c478bd9Sstevel@tonic-gate 		 */
11897c478bd9Sstevel@tonic-gate 		if ((sdp->sd_flags & (FLG_SY_EXTERN | FLG_SY_PARENT)) &&
11907c478bd9Sstevel@tonic-gate 		    ((sdp->sd_flags & FLG_SY_MAPUSED) == 0)) {
11917c478bd9Sstevel@tonic-gate 			sdp->sd_flags |= FLG_SY_INVALID;
11927c478bd9Sstevel@tonic-gate 			continue;
11937c478bd9Sstevel@tonic-gate 		}
11947c478bd9Sstevel@tonic-gate 
11957c478bd9Sstevel@tonic-gate 		sym = sdp->sd_sym;
1196d579eb63Sab 		type = ELF_ST_TYPE(sym->st_info);
11977c478bd9Sstevel@tonic-gate 
11987c478bd9Sstevel@tonic-gate 		/*
11997c478bd9Sstevel@tonic-gate 		 * Sanity check TLS.
12007c478bd9Sstevel@tonic-gate 		 */
1201d579eb63Sab 		if ((type == STT_TLS) && (sym->st_size != 0) &&
1202d579eb63Sab 		    (sym->st_shndx != SHN_UNDEF) &&
12030bc07c75Srie 		    (sym->st_shndx != SHN_COMMON)) {
120460758829Srie 			Is_desc		*isp = sdp->sd_isc;
120560758829Srie 			Ifl_desc	*ifl = sdp->sd_file;
12067c478bd9Sstevel@tonic-gate 
12077c478bd9Sstevel@tonic-gate 			if ((isp == 0) || (isp->is_shdr == 0) ||
12087c478bd9Sstevel@tonic-gate 			    ((isp->is_shdr->sh_flags & SHF_TLS) == 0)) {
12095aefb655Srie 				eprintf(ofl->ofl_lml, ERR_FATAL,
12105aefb655Srie 				    MSG_INTL(MSG_SYM_TLS),
12117c478bd9Sstevel@tonic-gate 				    demangle(sdp->sd_name), ifl->ifl_name);
12127c478bd9Sstevel@tonic-gate 				ofl->ofl_flags |= FLG_OF_FATAL;
12137c478bd9Sstevel@tonic-gate 				continue;
12147c478bd9Sstevel@tonic-gate 			}
12157c478bd9Sstevel@tonic-gate 		}
12167c478bd9Sstevel@tonic-gate 
12177c478bd9Sstevel@tonic-gate 		if ((sdp->sd_flags & FLG_SY_VISIBLE) == 0)
12185aefb655Srie 			ld_sym_adjust_vis(sdp, ofl);
12197c478bd9Sstevel@tonic-gate 
12207c478bd9Sstevel@tonic-gate 		if ((sdp->sd_flags & FLG_SY_REDUCED) &&
12217c478bd9Sstevel@tonic-gate 		    (oflags & FLG_OF_PROCRED)) {
12225aefb655Srie 			DBG_CALL(Dbg_syms_reduce(ofl, DBG_SYM_REDUCE_GLOBAL,
12235aefb655Srie 			    sdp, 0, 0));
12247c478bd9Sstevel@tonic-gate 		}
12257c478bd9Sstevel@tonic-gate 
122660758829Srie 		/*
122760758829Srie 		 * Record any STV_SINGLETON existence.
122860758829Srie 		 */
122960758829Srie 		if ((vis = ELF_ST_VISIBILITY(sym->st_other)) == STV_SINGLETON)
123060758829Srie 			ofl->ofl_dtflags_1 |= DF_1_SINGLETON;
123160758829Srie 
12327c478bd9Sstevel@tonic-gate 		/*
12337c478bd9Sstevel@tonic-gate 		 * If building a shared object or executable, and this is a
12347c478bd9Sstevel@tonic-gate 		 * non-weak UNDEF symbol with reduced visibility (STV_*), then
12357c478bd9Sstevel@tonic-gate 		 * give a fatal error.
12367c478bd9Sstevel@tonic-gate 		 */
123760758829Srie 		if (((oflags & FLG_OF_RELOBJ) == 0) &&
12380bc07c75Srie 		    (sym->st_shndx == SHN_UNDEF) &&
12397c478bd9Sstevel@tonic-gate 		    (ELF_ST_BIND(sym->st_info) != STB_WEAK)) {
124060758829Srie 			if (vis && (vis != STV_SINGLETON)) {
124160758829Srie 				sym_undef_entry(ofl, sdp, BNDLOCAL);
124260758829Srie 				ofl->ofl_flags |= FLG_OF_FATAL;
124360758829Srie 				continue;
124460758829Srie 			}
12457c478bd9Sstevel@tonic-gate 		}
12467c478bd9Sstevel@tonic-gate 
12477c478bd9Sstevel@tonic-gate 		/*
12487c478bd9Sstevel@tonic-gate 		 * If this symbol is defined in a non-allocatable section,
12497c478bd9Sstevel@tonic-gate 		 * reduce it to local symbol.
12507c478bd9Sstevel@tonic-gate 		 */
12517c478bd9Sstevel@tonic-gate 		if (((isp = sdp->sd_isc) != 0) && isp->is_shdr &&
12527c478bd9Sstevel@tonic-gate 		    ((isp->is_shdr->sh_flags & SHF_ALLOC) == 0)) {
12537c478bd9Sstevel@tonic-gate 			sdp->sd_flags |= FLG_SY_REDUCED;
125460758829Srie 			sdp->sd_flags1 |= FLG_SY1_HIDDEN;
12557c478bd9Sstevel@tonic-gate 		}
12567c478bd9Sstevel@tonic-gate 
12577c478bd9Sstevel@tonic-gate 		/*
12587c478bd9Sstevel@tonic-gate 		 * If this symbol originated as a SHN_SUNW_IGNORE, it will have
12597c478bd9Sstevel@tonic-gate 		 * been processed as an SHN_UNDEF.  Return the symbol to its
12607c478bd9Sstevel@tonic-gate 		 * original index for validation, and propagation to the output
12617c478bd9Sstevel@tonic-gate 		 * file.
12627c478bd9Sstevel@tonic-gate 		 */
12637c478bd9Sstevel@tonic-gate 		if (sdp->sd_flags1 & FLG_SY1_IGNORE)
12640bc07c75Srie 			sdp->sd_shndx = SHN_SUNW_IGNORE;
12657c478bd9Sstevel@tonic-gate 
12667c478bd9Sstevel@tonic-gate 		if (undef) {
12677c478bd9Sstevel@tonic-gate 			/*
12683b41b08bSab 			 * If a non-weak reference remains undefined, or if a
12697c478bd9Sstevel@tonic-gate 			 * mapfile reference is not bound to the relocatable
12707c478bd9Sstevel@tonic-gate 			 * objects that make up the object being built, we have
12717c478bd9Sstevel@tonic-gate 			 * a fatal error.
12727c478bd9Sstevel@tonic-gate 			 *
12737c478bd9Sstevel@tonic-gate 			 * The exceptions are symbols which are defined to be
12747c478bd9Sstevel@tonic-gate 			 * found in the parent (FLG_SY_PARENT), which is really
12757c478bd9Sstevel@tonic-gate 			 * only meaningful for direct binding, or are defined
12767c478bd9Sstevel@tonic-gate 			 * external (FLG_SY_EXTERN) so as to suppress -zdefs
12777c478bd9Sstevel@tonic-gate 			 * errors.
12787c478bd9Sstevel@tonic-gate 			 *
12797c478bd9Sstevel@tonic-gate 			 * Register symbols are always allowed to be UNDEF.
12807c478bd9Sstevel@tonic-gate 			 *
12817c478bd9Sstevel@tonic-gate 			 * Note that we don't include references created via -u
12827c478bd9Sstevel@tonic-gate 			 * in the same shared object binding test.  This is for
12837c478bd9Sstevel@tonic-gate 			 * backward compatibility, in that a number of archive
12847c478bd9Sstevel@tonic-gate 			 * makefile rules used -u to cause archive extraction.
12857c478bd9Sstevel@tonic-gate 			 * These same rules have been cut and pasted to apply
12867c478bd9Sstevel@tonic-gate 			 * to shared objects, and thus although the -u reference
12877c478bd9Sstevel@tonic-gate 			 * is redundant, flagging it as fatal could cause some
12887c478bd9Sstevel@tonic-gate 			 * build to fail.  Also we have documented the use of
12897c478bd9Sstevel@tonic-gate 			 * -u as a mechanism to cause binding to weak version
12907c478bd9Sstevel@tonic-gate 			 * definitions, thus giving users an error condition
12917c478bd9Sstevel@tonic-gate 			 * would be incorrect.
12927c478bd9Sstevel@tonic-gate 			 */
12937c478bd9Sstevel@tonic-gate 			if (!(sdp->sd_flags & FLG_SY_REGSYM) &&
12940bc07c75Srie 			    ((sym->st_shndx == SHN_UNDEF) &&
12957c478bd9Sstevel@tonic-gate 			    ((ELF_ST_BIND(sym->st_info) != STB_WEAK) &&
12967c478bd9Sstevel@tonic-gate 			    ((sdp->sd_flags &
12977c478bd9Sstevel@tonic-gate 			    (FLG_SY_PARENT | FLG_SY_EXTERN)) == 0)) ||
12987c478bd9Sstevel@tonic-gate 			    (((sdp->sd_flags &
12997c478bd9Sstevel@tonic-gate 			    (FLG_SY_MAPREF | FLG_SY_MAPUSED)) ==
13007c478bd9Sstevel@tonic-gate 			    FLG_SY_MAPREF) &&
130160758829Srie 			    ((sdp->sd_flags1 & (FLG_SY1_HIDDEN |
130260758829Srie 			    FLG_SY1_PROTECT)) == 0)))) {
13035aefb655Srie 				sym_undef_entry(ofl, sdp, UNDEF);
13047c478bd9Sstevel@tonic-gate 				ofl->ofl_flags |= undef;
13057c478bd9Sstevel@tonic-gate 				undeferr = 1;
13067c478bd9Sstevel@tonic-gate 			}
13077c478bd9Sstevel@tonic-gate 
13087c478bd9Sstevel@tonic-gate 		} else {
13097c478bd9Sstevel@tonic-gate 			/*
13107c478bd9Sstevel@tonic-gate 			 * For building things like shared objects (or anything
13117c478bd9Sstevel@tonic-gate 			 * -znodefs), undefined symbols are allowed.
13127c478bd9Sstevel@tonic-gate 			 *
13137c478bd9Sstevel@tonic-gate 			 * If a mapfile reference remains undefined the user
13147c478bd9Sstevel@tonic-gate 			 * would probably like a warning at least (they've
13157c478bd9Sstevel@tonic-gate 			 * usually mis-spelt the reference).  Refer to the above
13167c478bd9Sstevel@tonic-gate 			 * comments for discussion on -u references, which
13177c478bd9Sstevel@tonic-gate 			 * are not tested for in the same manner.
13187c478bd9Sstevel@tonic-gate 			 */
13197c478bd9Sstevel@tonic-gate 			if ((sdp->sd_flags &
13207c478bd9Sstevel@tonic-gate 			    (FLG_SY_MAPREF | FLG_SY_MAPUSED)) ==
13217c478bd9Sstevel@tonic-gate 			    FLG_SY_MAPREF) {
13225aefb655Srie 				sym_undef_entry(ofl, sdp, UNDEF);
13237c478bd9Sstevel@tonic-gate 				ofl->ofl_flags |= FLG_OF_WARN;
13247c478bd9Sstevel@tonic-gate 				undeferr = 1;
13257c478bd9Sstevel@tonic-gate 			}
13267c478bd9Sstevel@tonic-gate 		}
13277c478bd9Sstevel@tonic-gate 
13287c478bd9Sstevel@tonic-gate 		/*
13297c478bd9Sstevel@tonic-gate 		 * If this symbol comes from a dependency mark the dependency
13307c478bd9Sstevel@tonic-gate 		 * as required (-z ignore can result in unused dependencies
13317c478bd9Sstevel@tonic-gate 		 * being dropped).  If we need to record dependency versioning
13327c478bd9Sstevel@tonic-gate 		 * information indicate what version of the needed shared object
13337c478bd9Sstevel@tonic-gate 		 * this symbol is part of.  Flag the symbol as undefined if it
13347c478bd9Sstevel@tonic-gate 		 * has not been made available to us.
13357c478bd9Sstevel@tonic-gate 		 */
13367c478bd9Sstevel@tonic-gate 		if ((sdp->sd_ref == REF_DYN_NEED) &&
13377c478bd9Sstevel@tonic-gate 		    (!(sdp->sd_flags & FLG_SY_REFRSD))) {
13387c478bd9Sstevel@tonic-gate 			sdp->sd_file->ifl_flags |= FLG_IF_DEPREQD;
13397c478bd9Sstevel@tonic-gate 
13407c478bd9Sstevel@tonic-gate 			/*
13417c478bd9Sstevel@tonic-gate 			 * Capture that we've bound to a symbol that doesn't
13427c478bd9Sstevel@tonic-gate 			 * allow being directly bound to.
13437c478bd9Sstevel@tonic-gate 			 */
13447c478bd9Sstevel@tonic-gate 			if (sdp->sd_flags1 & FLG_SY1_NDIR)
13457c478bd9Sstevel@tonic-gate 				ofl->ofl_flags1 |= FLG_OF1_NDIRECT;
13467c478bd9Sstevel@tonic-gate 
13477c478bd9Sstevel@tonic-gate 			if (sdp->sd_file->ifl_vercnt) {
13487c478bd9Sstevel@tonic-gate 				int		vndx;
134960758829Srie 				Ver_index	*vip;
13507c478bd9Sstevel@tonic-gate 
13517c478bd9Sstevel@tonic-gate 				vndx = sdp->sd_aux->sa_dverndx;
13527c478bd9Sstevel@tonic-gate 				vip = &sdp->sd_file->ifl_verndx[vndx];
13537c478bd9Sstevel@tonic-gate 				if (vip->vi_flags & FLG_VER_AVAIL) {
13547c478bd9Sstevel@tonic-gate 					vip->vi_flags |= FLG_VER_REFER;
13557c478bd9Sstevel@tonic-gate 				} else {
13565aefb655Srie 					sym_undef_entry(ofl, sdp, NOTAVAIL);
13577c478bd9Sstevel@tonic-gate 					ofl->ofl_flags |= FLG_OF_FATAL;
13587c478bd9Sstevel@tonic-gate 					continue;
13597c478bd9Sstevel@tonic-gate 				}
13607c478bd9Sstevel@tonic-gate 			}
13617c478bd9Sstevel@tonic-gate 		}
13627c478bd9Sstevel@tonic-gate 
13637c478bd9Sstevel@tonic-gate 		/*
13647c478bd9Sstevel@tonic-gate 		 * Test that we do not bind to symbol supplied from an implicit
13657c478bd9Sstevel@tonic-gate 		 * shared object.  If a binding is from a weak reference it can
13667c478bd9Sstevel@tonic-gate 		 * be ignored.
13677c478bd9Sstevel@tonic-gate 		 */
13687c478bd9Sstevel@tonic-gate 		if (needed && !undeferr && (sdp->sd_flags & FLG_SY_GLOBREF) &&
13697c478bd9Sstevel@tonic-gate 		    (sdp->sd_ref == REF_DYN_NEED) &&
13707c478bd9Sstevel@tonic-gate 		    (sdp->sd_flags & FLG_SY_NOTAVAIL)) {
13715aefb655Srie 			sym_undef_entry(ofl, sdp, IMPLICIT);
13727c478bd9Sstevel@tonic-gate 			ofl->ofl_flags |= needed;
13737c478bd9Sstevel@tonic-gate 			continue;
13747c478bd9Sstevel@tonic-gate 		}
13757c478bd9Sstevel@tonic-gate 
13767c478bd9Sstevel@tonic-gate 		/*
13777c478bd9Sstevel@tonic-gate 		 * Test that a symbol isn't going to be reduced to local scope
13787c478bd9Sstevel@tonic-gate 		 * which actually wants to bind to a shared object - if so it's
13797c478bd9Sstevel@tonic-gate 		 * a fatal error.
13807c478bd9Sstevel@tonic-gate 		 */
13817c478bd9Sstevel@tonic-gate 		if ((sdp->sd_ref == REF_DYN_NEED) &&
138260758829Srie 		    (sdp->sd_flags1 & (FLG_SY1_HIDDEN | FLG_SY1_PROTECT))) {
13835aefb655Srie 			sym_undef_entry(ofl, sdp, BNDLOCAL);
13847c478bd9Sstevel@tonic-gate 			ofl->ofl_flags |= FLG_OF_FATAL;
13857c478bd9Sstevel@tonic-gate 			continue;
13867c478bd9Sstevel@tonic-gate 		}
13877c478bd9Sstevel@tonic-gate 
13887c478bd9Sstevel@tonic-gate 		/*
13897c478bd9Sstevel@tonic-gate 		 * If the output image is to be versioned then all symbol
13907c478bd9Sstevel@tonic-gate 		 * definitions must be associated with a version.
13917c478bd9Sstevel@tonic-gate 		 */
13927c478bd9Sstevel@tonic-gate 		if (verdesc && (sdp->sd_ref == REF_REL_NEED) &&
13930bc07c75Srie 		    (sym->st_shndx != SHN_UNDEF) &&
139460758829Srie 		    (!(sdp->sd_flags1 & FLG_SY1_HIDDEN)) &&
13957c478bd9Sstevel@tonic-gate 		    (sdp->sd_aux->sa_overndx == 0)) {
13965aefb655Srie 			sym_undef_entry(ofl, sdp, NOVERSION);
13977c478bd9Sstevel@tonic-gate 			ofl->ofl_flags |= verdesc;
13987c478bd9Sstevel@tonic-gate 			continue;
13997c478bd9Sstevel@tonic-gate 		}
14007c478bd9Sstevel@tonic-gate 
14017c478bd9Sstevel@tonic-gate 		/*
14027c478bd9Sstevel@tonic-gate 		 * If we don't need the symbol there's no need to process it
14037c478bd9Sstevel@tonic-gate 		 * any further.
14047c478bd9Sstevel@tonic-gate 		 */
14057c478bd9Sstevel@tonic-gate 		if (sdp->sd_ref == REF_DYN_SEEN)
14067c478bd9Sstevel@tonic-gate 			continue;
14077c478bd9Sstevel@tonic-gate 
14087c478bd9Sstevel@tonic-gate 		/*
14097c478bd9Sstevel@tonic-gate 		 * Calculate the size and alignment requirements for the global
14107c478bd9Sstevel@tonic-gate 		 * .bss and .tls sections.  If we're building a relocatable
14117c478bd9Sstevel@tonic-gate 		 * object only account for scoped COMMON symbols (these will
14127c478bd9Sstevel@tonic-gate 		 * be converted to .bss references).
14137c478bd9Sstevel@tonic-gate 		 *
14147c478bd9Sstevel@tonic-gate 		 * For partially initialized symbol,
14157c478bd9Sstevel@tonic-gate 		 *  if it is expanded, it goes to sunwdata1.
14167c478bd9Sstevel@tonic-gate 		 *  if it is local, it goes to .bss.
14177c478bd9Sstevel@tonic-gate 		 *  if the output is shared object, it goes to .sunwbss.
14187c478bd9Sstevel@tonic-gate 		 *
14197c478bd9Sstevel@tonic-gate 		 * Also refer to make_mvsections() in sunwmove.c
14207c478bd9Sstevel@tonic-gate 		 */
14210bc07c75Srie 		if ((sym->st_shndx == SHN_COMMON) &&
14220bc07c75Srie 		    (((oflags & FLG_OF_RELOBJ) == 0) ||
142360758829Srie 		    ((sdp->sd_flags1 & FLG_SY1_HIDDEN) &&
14247c478bd9Sstevel@tonic-gate 		    (oflags & FLG_OF_PROCRED)))) {
14257c478bd9Sstevel@tonic-gate 			int countbss = 0;
14267c478bd9Sstevel@tonic-gate 
14277c478bd9Sstevel@tonic-gate 			if (sdp->sd_psyminfo == 0) {
14287c478bd9Sstevel@tonic-gate 				countbss = 1;
14297c478bd9Sstevel@tonic-gate 			} else if ((sdp->sd_flags & FLG_SY_PAREXPN) != 0) {
14307c478bd9Sstevel@tonic-gate 				countbss = 0;
14317c478bd9Sstevel@tonic-gate 			} else if (ELF_ST_BIND(sym->st_info) == STB_LOCAL) {
14327c478bd9Sstevel@tonic-gate 				countbss = 1;
14337c478bd9Sstevel@tonic-gate 			} else if ((ofl->ofl_flags & FLG_OF_SHAROBJ) != 0) {
14347c478bd9Sstevel@tonic-gate 				countbss = 0;
14357c478bd9Sstevel@tonic-gate 			} else
14367c478bd9Sstevel@tonic-gate 				countbss = 1;
14377c478bd9Sstevel@tonic-gate 
14387c478bd9Sstevel@tonic-gate 			if (countbss) {
14397c478bd9Sstevel@tonic-gate 				Xword * size, * align;
14407c478bd9Sstevel@tonic-gate 
1441d579eb63Sab 				if (type != STT_TLS) {
14427c478bd9Sstevel@tonic-gate 					size = &bsssize;
14437c478bd9Sstevel@tonic-gate 					align = &bssalign;
14447c478bd9Sstevel@tonic-gate 				} else {
14457c478bd9Sstevel@tonic-gate 					size = &tlssize;
14467c478bd9Sstevel@tonic-gate 					align = &tlsalign;
14477c478bd9Sstevel@tonic-gate 				}
14487c478bd9Sstevel@tonic-gate 				*size = (Xword)S_ROUND(*size, sym->st_value) +
1449dd94ecefSrie 				    sym->st_size;
14507c478bd9Sstevel@tonic-gate 				if (sym->st_value > *align)
14517c478bd9Sstevel@tonic-gate 					*align = sym->st_value;
14527c478bd9Sstevel@tonic-gate 			}
14537c478bd9Sstevel@tonic-gate 		}
14547c478bd9Sstevel@tonic-gate 
1455*ba2be530Sab #if	defined(_ELF64)
145654d82594Sseizo 		/*
145754d82594Sseizo 		 * Calculate the size and alignment requirement for the global
145854d82594Sseizo 		 * .lbss. TLS or partially initialized symbols do not need to be
145954d82594Sseizo 		 * considered yet.
146054d82594Sseizo 		 */
1461*ba2be530Sab 		if ((ld_targ.t_m.m_mach == EM_AMD64) &&
1462*ba2be530Sab 		    (sym->st_shndx == SHN_X86_64_LCOMMON)) {
146354d82594Sseizo 			lbsssize = (Xword)S_ROUND(lbsssize, sym->st_value) +
146433eb6ee1Sab 			    sym->st_size;
146554d82594Sseizo 			if (sym->st_value > lbssalign)
146654d82594Sseizo 				lbssalign = sym->st_value;
146754d82594Sseizo 		}
146854d82594Sseizo #endif
146954d82594Sseizo 
14707c478bd9Sstevel@tonic-gate 		/*
14717c478bd9Sstevel@tonic-gate 		 * If a symbol was referenced via the command line
14727c478bd9Sstevel@tonic-gate 		 * (ld -u <>, ...), then this counts as a reference against the
14737c478bd9Sstevel@tonic-gate 		 * symbol. Mark any section that symbol is defined in.
14747c478bd9Sstevel@tonic-gate 		 */
14757c478bd9Sstevel@tonic-gate 		if (((isp = sdp->sd_isc) != 0) &&
14767c478bd9Sstevel@tonic-gate 		    (sdp->sd_flags & FLG_SY_CMDREF)) {
14777c478bd9Sstevel@tonic-gate 			isp->is_flags |= FLG_IS_SECTREF;
14787c478bd9Sstevel@tonic-gate 			isp->is_file->ifl_flags |= FLG_IF_FILEREF;
14797c478bd9Sstevel@tonic-gate 		}
14807c478bd9Sstevel@tonic-gate 
14817c478bd9Sstevel@tonic-gate 		/*
14827c478bd9Sstevel@tonic-gate 		 * Update the symbol count and the associated name string size.
14837c478bd9Sstevel@tonic-gate 		 */
1484d4517e84Srie 		if ((sdp->sd_flags1 & FLG_SY1_HIDDEN) &&
14857c478bd9Sstevel@tonic-gate 		    (oflags & FLG_OF_PROCRED)) {
14867c478bd9Sstevel@tonic-gate 			/*
148760758829Srie 			 * If any reductions are being processed, keep a count
148860758829Srie 			 * of eliminated symbols, and if the symbol is being
148960758829Srie 			 * reduced to local, count it's size for the .symtab.
14907c478bd9Sstevel@tonic-gate 			 */
14917c478bd9Sstevel@tonic-gate 			if (sdp->sd_flags1 & FLG_SY1_ELIM) {
14927c478bd9Sstevel@tonic-gate 				ofl->ofl_elimcnt++;
14937c478bd9Sstevel@tonic-gate 			} else {
14947c478bd9Sstevel@tonic-gate 				ofl->ofl_scopecnt++;
14957c478bd9Sstevel@tonic-gate 				if ((((sdp->sd_flags & FLG_SY_REGSYM) == 0) ||
14967c478bd9Sstevel@tonic-gate 				    sym->st_name) && (st_insert(ofl->ofl_strtab,
14977c478bd9Sstevel@tonic-gate 				    sdp->sd_name) == -1))
14987c478bd9Sstevel@tonic-gate 					return (S_ERROR);
14999039eeafSab 				if (allow_ldynsym && sym->st_name &&
1500d579eb63Sab 				    ldynsym_symtype[type]) {
15019039eeafSab 					ofl->ofl_dynscopecnt++;
15029039eeafSab 					if (st_insert(ofl->ofl_dynstrtab,
15039039eeafSab 					    sdp->sd_name) == -1)
15049039eeafSab 						return (S_ERROR);
1505d579eb63Sab 					/* Include it in sort section? */
1506d579eb63Sab 					DYNSORT_COUNT(sdp, sym, type, ++);
15079039eeafSab 				}
15087c478bd9Sstevel@tonic-gate 			}
15097c478bd9Sstevel@tonic-gate 		} else {
15107c478bd9Sstevel@tonic-gate 			ofl->ofl_globcnt++;
15117c478bd9Sstevel@tonic-gate 
1512d579eb63Sab 			/*
1513d579eb63Sab 			 * Check to see if this global variable should
1514d579eb63Sab 			 * go into a sort section. Sort sections require
1515d579eb63Sab 			 * a .SUNW_ldynsym section, so, don't check
1516d579eb63Sab 			 * unless a .SUNW_ldynsym is allowed.
1517d579eb63Sab 			 */
1518d579eb63Sab 			if (allow_ldynsym) {
1519d579eb63Sab 				DYNSORT_COUNT(sdp, sym, type, ++);
1520d579eb63Sab 			}
1521d579eb63Sab 
15227c478bd9Sstevel@tonic-gate 			/*
15237c478bd9Sstevel@tonic-gate 			 * If global direct bindings are in effect, or this
15247c478bd9Sstevel@tonic-gate 			 * symbol has bound to a dependency which was specified
15257c478bd9Sstevel@tonic-gate 			 * as requiring direct bindings, and it hasn't
15267c478bd9Sstevel@tonic-gate 			 * explicitly been defined as a non-direct binding
15277c478bd9Sstevel@tonic-gate 			 * symbol, mark it.
15287c478bd9Sstevel@tonic-gate 			 */
15297c478bd9Sstevel@tonic-gate 			if (((ofl->ofl_dtflags_1 & DF_1_DIRECT) || (isp &&
15307c478bd9Sstevel@tonic-gate 			    (isp->is_file->ifl_flags & FLG_IF_DIRECT))) &&
15317c478bd9Sstevel@tonic-gate 			    ((sdp->sd_flags1 & FLG_SY1_NDIR) == 0))
15327c478bd9Sstevel@tonic-gate 				sdp->sd_flags1 |= FLG_SY1_DIR;
15337c478bd9Sstevel@tonic-gate 
15347c478bd9Sstevel@tonic-gate 			/*
15357c478bd9Sstevel@tonic-gate 			 * Insert the symbol name.
15367c478bd9Sstevel@tonic-gate 			 */
15377c478bd9Sstevel@tonic-gate 			if (((sdp->sd_flags & FLG_SY_REGSYM) == 0) ||
15387c478bd9Sstevel@tonic-gate 			    sym->st_name) {
15397c478bd9Sstevel@tonic-gate 				if (st_insert(ofl->ofl_strtab,
15407c478bd9Sstevel@tonic-gate 				    sdp->sd_name) == -1)
15417c478bd9Sstevel@tonic-gate 					return (S_ERROR);
15427c478bd9Sstevel@tonic-gate 
15437c478bd9Sstevel@tonic-gate 				if (!(ofl->ofl_flags & FLG_OF_RELOBJ) &&
15447c478bd9Sstevel@tonic-gate 				    (st_insert(ofl->ofl_dynstrtab,
15457c478bd9Sstevel@tonic-gate 				    sdp->sd_name) == -1))
15467c478bd9Sstevel@tonic-gate 					return (S_ERROR);
15477c478bd9Sstevel@tonic-gate 			}
15487c478bd9Sstevel@tonic-gate 
15497c478bd9Sstevel@tonic-gate 			/*
15507c478bd9Sstevel@tonic-gate 			 * If this section offers a global symbol - record that
15517c478bd9Sstevel@tonic-gate 			 * fact.
15527c478bd9Sstevel@tonic-gate 			 */
15537c478bd9Sstevel@tonic-gate 			if (isp) {
15547c478bd9Sstevel@tonic-gate 				isp->is_flags |= FLG_IS_SECTREF;
15557c478bd9Sstevel@tonic-gate 				isp->is_file->ifl_flags |= FLG_IF_FILEREF;
15567c478bd9Sstevel@tonic-gate 			}
15577c478bd9Sstevel@tonic-gate 		}
15587c478bd9Sstevel@tonic-gate 	}
15597c478bd9Sstevel@tonic-gate 
15607c478bd9Sstevel@tonic-gate 	/*
15617c478bd9Sstevel@tonic-gate 	 * If we've encountered a fatal error during symbol validation then
15627c478bd9Sstevel@tonic-gate 	 * return now.
15637c478bd9Sstevel@tonic-gate 	 */
15647c478bd9Sstevel@tonic-gate 	if (ofl->ofl_flags & FLG_OF_FATAL)
15657c478bd9Sstevel@tonic-gate 		return (1);
15667c478bd9Sstevel@tonic-gate 
15677c478bd9Sstevel@tonic-gate 	/*
15687c478bd9Sstevel@tonic-gate 	 * Now that symbol resolution is completed, scan any register symbols.
15697c478bd9Sstevel@tonic-gate 	 * From now on, we're only interested in those that contribute to the
15707c478bd9Sstevel@tonic-gate 	 * output file.
15717c478bd9Sstevel@tonic-gate 	 */
15727c478bd9Sstevel@tonic-gate 	if (ofl->ofl_regsyms) {
15737c478bd9Sstevel@tonic-gate 		int	ndx;
15747c478bd9Sstevel@tonic-gate 
15757c478bd9Sstevel@tonic-gate 		for (ndx = 0; ndx < ofl->ofl_regsymsno; ndx++) {
15767c478bd9Sstevel@tonic-gate 			if ((sdp = ofl->ofl_regsyms[ndx]) == 0)
15777c478bd9Sstevel@tonic-gate 				continue;
15787c478bd9Sstevel@tonic-gate 			if (sdp->sd_ref != REF_REL_NEED) {
15797c478bd9Sstevel@tonic-gate 				ofl->ofl_regsyms[ndx] = 0;
15807c478bd9Sstevel@tonic-gate 				continue;
15817c478bd9Sstevel@tonic-gate 			}
15827c478bd9Sstevel@tonic-gate 
15837c478bd9Sstevel@tonic-gate 			ofl->ofl_regsymcnt++;
15847c478bd9Sstevel@tonic-gate 			if (sdp->sd_sym->st_name == 0)
15857c478bd9Sstevel@tonic-gate 				sdp->sd_name = MSG_ORIG(MSG_STR_EMPTY);
15867c478bd9Sstevel@tonic-gate 
158760758829Srie 			if ((sdp->sd_flags1 & FLG_SY1_HIDDEN) ||
15887c478bd9Sstevel@tonic-gate 			    (ELF_ST_BIND(sdp->sd_sym->st_info) == STB_LOCAL))
15897c478bd9Sstevel@tonic-gate 				ofl->ofl_lregsymcnt++;
15907c478bd9Sstevel@tonic-gate 		}
15917c478bd9Sstevel@tonic-gate 	}
15927c478bd9Sstevel@tonic-gate 
15937c478bd9Sstevel@tonic-gate 	/*
15947c478bd9Sstevel@tonic-gate 	 * Generate the .bss section now that we know its size and alignment.
15957c478bd9Sstevel@tonic-gate 	 */
15967c478bd9Sstevel@tonic-gate 	if (bsssize || !(oflags & FLG_OF_RELOBJ)) {
15975aefb655Srie 		if (ld_make_bss(ofl, bsssize, bssalign, MAKE_BSS) == S_ERROR)
15987c478bd9Sstevel@tonic-gate 			return (S_ERROR);
15997c478bd9Sstevel@tonic-gate 	}
16007c478bd9Sstevel@tonic-gate 	if (tlssize) {
16015aefb655Srie 		if (ld_make_bss(ofl, tlssize, tlsalign, MAKE_TLS) == S_ERROR)
160254d82594Sseizo 			return (S_ERROR);
160354d82594Sseizo 	}
1604*ba2be530Sab #if	defined(_ELF64)
1605*ba2be530Sab 	if ((ld_targ.t_m.m_mach == EM_AMD64) &&
1606*ba2be530Sab 	    lbsssize && !(oflags & FLG_OF_RELOBJ)) {
16075aefb655Srie 		if (ld_make_bss(ofl, lbsssize, lbssalign, MAKE_LBSS) == S_ERROR)
16087c478bd9Sstevel@tonic-gate 			return (S_ERROR);
16097c478bd9Sstevel@tonic-gate 	}
161054d82594Sseizo #endif
16117c478bd9Sstevel@tonic-gate 
16127c478bd9Sstevel@tonic-gate 	/*
16137c478bd9Sstevel@tonic-gate 	 * Determine what entry point symbol we need, and if found save its
16147c478bd9Sstevel@tonic-gate 	 * symbol descriptor so that we can update the ELF header entry with the
16157c478bd9Sstevel@tonic-gate 	 * symbols value later (see update_oehdr).  Make sure the symbol is
1616c1c6f601Srie 	 * tagged to ensure its update in case -s is in effect.  Use any -e
16177c478bd9Sstevel@tonic-gate 	 * option first, or the default entry points `_start' and `main'.
16187c478bd9Sstevel@tonic-gate 	 */
1619c1c6f601Srie 	ret = 0;
16207c478bd9Sstevel@tonic-gate 	if (ofl->ofl_entry) {
16211d1fba8aSrie 		if ((sdp =
16221d1fba8aSrie 		    ld_sym_find(ofl->ofl_entry, SYM_NOHASH, 0, ofl)) == NULL) {
16231d1fba8aSrie 			eprintf(ofl->ofl_lml, ERR_FATAL,
16241d1fba8aSrie 			    MSG_INTL(MSG_ARG_NOENTRY), ofl->ofl_entry);
16251d1fba8aSrie 			ret++;
16261d1fba8aSrie 		} else if (ensure_sym_local(ofl, sdp,
16271d1fba8aSrie 		    MSG_INTL(MSG_SYM_ENTRY)) != 0) {
1628c1c6f601Srie 			ret++;
16291d1fba8aSrie 		} else {
16301d1fba8aSrie 			ofl->ofl_entry = (void *)sdp;
16317c478bd9Sstevel@tonic-gate 		}
16325aefb655Srie 	} else if (((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_START),
1633c1c6f601Srie 	    SYM_NOHASH, 0, ofl)) != NULL) && (ensure_sym_local(ofl,
1634c1c6f601Srie 	    sdp, 0) == 0)) {
16357c478bd9Sstevel@tonic-gate 		ofl->ofl_entry = (void *)sdp;
1636c1c6f601Srie 
16375aefb655Srie 	} else if (((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_MAIN),
1638c1c6f601Srie 	    SYM_NOHASH, 0, ofl)) != NULL) && (ensure_sym_local(ofl,
1639c1c6f601Srie 	    sdp, 0) == 0)) {
16407c478bd9Sstevel@tonic-gate 		ofl->ofl_entry = (void *)sdp;
16417c478bd9Sstevel@tonic-gate 	}
16427c478bd9Sstevel@tonic-gate 
16437c478bd9Sstevel@tonic-gate 	/*
1644c1c6f601Srie 	 * If ld -zdtrace=<sym> was given, then validate that the symbol is
1645c1c6f601Srie 	 * defined within the current object being built.
16467c478bd9Sstevel@tonic-gate 	 */
16471d1fba8aSrie 	if ((sdp = ofl->ofl_dtracesym) != 0)
1648c1c6f601Srie 		ret += ensure_sym_local(ofl, sdp, MSG_ORIG(MSG_STR_DTRACE));
16497c478bd9Sstevel@tonic-gate 
1650c1c6f601Srie 	/*
1651c1c6f601Srie 	 * If any initarray, finiarray or preinitarray functions have been
1652c1c6f601Srie 	 * requested, make sure they are defined within the current object
1653c1c6f601Srie 	 * being built.
1654c1c6f601Srie 	 */
1655c1c6f601Srie 	if (ofl->ofl_initarray.head) {
1656c1c6f601Srie 		ret += ensure_array_local(ofl, &ofl->ofl_initarray,
1657c1c6f601Srie 		    MSG_ORIG(MSG_SYM_INITARRAY));
1658c1c6f601Srie 	}
1659c1c6f601Srie 	if (ofl->ofl_finiarray.head) {
1660c1c6f601Srie 		ret += ensure_array_local(ofl, &ofl->ofl_finiarray,
1661c1c6f601Srie 		    MSG_ORIG(MSG_SYM_FINIARRAY));
1662c1c6f601Srie 	}
1663c1c6f601Srie 	if (ofl->ofl_preiarray.head) {
1664c1c6f601Srie 		ret += ensure_array_local(ofl, &ofl->ofl_preiarray,
1665c1c6f601Srie 		    MSG_ORIG(MSG_SYM_PREINITARRAY));
1666c1c6f601Srie 	}
1667c1c6f601Srie 
1668c1c6f601Srie 	if (ret)
1669c1c6f601Srie 		return (S_ERROR);
1670c1c6f601Srie 
16717c478bd9Sstevel@tonic-gate 	/*
16727c478bd9Sstevel@tonic-gate 	 * If we're required to record any needed dependencies versioning
16737c478bd9Sstevel@tonic-gate 	 * information calculate it now that all symbols have been validated.
16747c478bd9Sstevel@tonic-gate 	 */
16757c478bd9Sstevel@tonic-gate 	if ((oflags & (FLG_OF_VERNEED | FLG_OF_NOVERSEC)) == FLG_OF_VERNEED)
16765aefb655Srie 		return (ld_vers_check_need(ofl));
16777c478bd9Sstevel@tonic-gate 	else
16787c478bd9Sstevel@tonic-gate 		return (1);
16797c478bd9Sstevel@tonic-gate }
16807c478bd9Sstevel@tonic-gate 
16817c478bd9Sstevel@tonic-gate /*
16827c478bd9Sstevel@tonic-gate  * qsort(3c) comparison function.  As an optimization for associating weak
16837c478bd9Sstevel@tonic-gate  * symbols to their strong counterparts sort global symbols according to their
16847c478bd9Sstevel@tonic-gate  * address and binding.
16857c478bd9Sstevel@tonic-gate  */
16867c478bd9Sstevel@tonic-gate static int
16877c478bd9Sstevel@tonic-gate compare(const void * sdpp1, const void * sdpp2)
16887c478bd9Sstevel@tonic-gate {
16897c478bd9Sstevel@tonic-gate 	Sym_desc *	sdp1 = *((Sym_desc **)sdpp1);
16907c478bd9Sstevel@tonic-gate 	Sym_desc *	sdp2 = *((Sym_desc **)sdpp2);
16917c478bd9Sstevel@tonic-gate 	Sym *		sym1, * sym2;
16927c478bd9Sstevel@tonic-gate 	uchar_t		bind1, bind2;
16937c478bd9Sstevel@tonic-gate 
16947c478bd9Sstevel@tonic-gate 	/*
16957c478bd9Sstevel@tonic-gate 	 * Symbol descriptors may be zero, move these to the front of the
16967c478bd9Sstevel@tonic-gate 	 * sorted array.
16977c478bd9Sstevel@tonic-gate 	 */
16987c478bd9Sstevel@tonic-gate 	if (sdp1 == 0)
16997c478bd9Sstevel@tonic-gate 		return (-1);
17007c478bd9Sstevel@tonic-gate 	if (sdp2 == 0)
17017c478bd9Sstevel@tonic-gate 		return (1);
17027c478bd9Sstevel@tonic-gate 
17037c478bd9Sstevel@tonic-gate 	sym1 = sdp1->sd_sym;
17047c478bd9Sstevel@tonic-gate 	sym2 = sdp2->sd_sym;
17057c478bd9Sstevel@tonic-gate 
17067c478bd9Sstevel@tonic-gate 	/*
17077c478bd9Sstevel@tonic-gate 	 * Compare the symbols value (address).
17087c478bd9Sstevel@tonic-gate 	 */
17097c478bd9Sstevel@tonic-gate 	if (sym1->st_value > sym2->st_value)
17107c478bd9Sstevel@tonic-gate 		return (1);
17117c478bd9Sstevel@tonic-gate 	if (sym1->st_value < sym2->st_value)
17127c478bd9Sstevel@tonic-gate 		return (-1);
17137c478bd9Sstevel@tonic-gate 
17147c478bd9Sstevel@tonic-gate 	bind1 = ELF_ST_BIND(sym1->st_info);
17157c478bd9Sstevel@tonic-gate 	bind2 = ELF_ST_BIND(sym2->st_info);
17167c478bd9Sstevel@tonic-gate 
17177c478bd9Sstevel@tonic-gate 	/*
17187c478bd9Sstevel@tonic-gate 	 * If two symbols have the same address place the weak symbol before
17197c478bd9Sstevel@tonic-gate 	 * any strong counterpart.
17207c478bd9Sstevel@tonic-gate 	 */
17217c478bd9Sstevel@tonic-gate 	if (bind1 > bind2)
17227c478bd9Sstevel@tonic-gate 		return (-1);
17237c478bd9Sstevel@tonic-gate 	if (bind1 < bind2)
17247c478bd9Sstevel@tonic-gate 		return (1);
17257c478bd9Sstevel@tonic-gate 
17267c478bd9Sstevel@tonic-gate 	return (0);
17277c478bd9Sstevel@tonic-gate }
17287c478bd9Sstevel@tonic-gate 
17297c478bd9Sstevel@tonic-gate 
173069a0bf0cSab /*
173169a0bf0cSab  * Issue a MSG_SYM_BADADDR error from ld_sym_process(). This error
173269a0bf0cSab  * is issued when a symbol address/size is not contained by the
173369a0bf0cSab  * target section.
173469a0bf0cSab  *
173569a0bf0cSab  * Such objects are at least partially corrupt, and the user would
173669a0bf0cSab  * be well advised to be skeptical of them, and to ask their compiler
173769a0bf0cSab  * supplier to fix the problem. However, a distinction needs to be
173869a0bf0cSab  * made between symbols that reference readonly text, and those that
173969a0bf0cSab  * access writable data. Other than throwing off profiling results,
174069a0bf0cSab  * the readonly section case is less serious. We have encountered
174169a0bf0cSab  * such objects in the field. In order to allow existing objects
174269a0bf0cSab  * to continue working, we issue a warning rather than a fatal error
174369a0bf0cSab  * if the symbol is against readonly text. Other cases are fatal.
174469a0bf0cSab  */
174569a0bf0cSab static void
174669a0bf0cSab issue_badaddr_msg(Ifl_desc *ifl, Ofl_desc *ofl, Sym_desc *sdp,
174769a0bf0cSab     Sym *sym, Word shndx)
174869a0bf0cSab {
174969a0bf0cSab 	Lword	flag;
175069a0bf0cSab 	Error	err;
175169a0bf0cSab 	const char *msg;
175269a0bf0cSab 
175369a0bf0cSab 	if ((sdp->sd_isc->is_shdr->sh_flags & (SHF_WRITE | SHF_ALLOC)) ==
175469a0bf0cSab 	    SHF_ALLOC) {
175569a0bf0cSab 		msg = MSG_INTL(MSG_SYM_BADADDR_ROTXT);
175669a0bf0cSab 		flag = FLG_OF_WARN;
175769a0bf0cSab 		err = ERR_WARNING;
175869a0bf0cSab 	} else {
175969a0bf0cSab 		msg = MSG_INTL(MSG_SYM_BADADDR);
176069a0bf0cSab 		flag = FLG_OF_FATAL;
176169a0bf0cSab 		err = ERR_FATAL;
176269a0bf0cSab 	}
176369a0bf0cSab 
176469a0bf0cSab 	eprintf(ofl->ofl_lml, err, msg, demangle(sdp->sd_name),
176569a0bf0cSab 	    ifl->ifl_name, shndx, sdp->sd_isc->is_name,
176669a0bf0cSab 	    EC_XWORD(sdp->sd_isc->is_shdr->sh_size),
176769a0bf0cSab 	    EC_XWORD(sym->st_value), EC_XWORD(sym->st_size));
176869a0bf0cSab 	ofl->ofl_flags |= flag;
176969a0bf0cSab }
177069a0bf0cSab 
177169a0bf0cSab 
17727c478bd9Sstevel@tonic-gate /*
17737c478bd9Sstevel@tonic-gate  * Process the symbol table for the specified input file.  At this point all
17747c478bd9Sstevel@tonic-gate  * input sections from this input file have been assigned an input section
17757c478bd9Sstevel@tonic-gate  * descriptor which is saved in the `ifl_isdesc' array.
17767c478bd9Sstevel@tonic-gate  *
17777c478bd9Sstevel@tonic-gate  *	o	local symbols are saved (as is) if the input file is a
17787c478bd9Sstevel@tonic-gate  *		relocatable object
17797c478bd9Sstevel@tonic-gate  *
17807c478bd9Sstevel@tonic-gate  *	o	global symbols are added to the linkers internal symbol
17817c478bd9Sstevel@tonic-gate  *		table if they are not already present, otherwise a symbol
17827c478bd9Sstevel@tonic-gate  *		resolution function is called upon to resolve the conflict.
17837c478bd9Sstevel@tonic-gate  */
17847c478bd9Sstevel@tonic-gate uintptr_t
17855aefb655Srie ld_sym_process(Is_desc *isc, Ifl_desc *ifl, Ofl_desc *ofl)
17867c478bd9Sstevel@tonic-gate {
178775e45495Sab 	/*
178875e45495Sab 	 * This macro tests the given symbol to see if it is out of
178975e45495Sab 	 * range relative to the section it references.
179075e45495Sab 	 *
179175e45495Sab 	 * entry:
179275e45495Sab 	 *	- ifl is a relative object (ET_REL)
179375e45495Sab 	 *	_sdp - Symbol descriptor
179475e45495Sab 	 *	_sym - Symbol
179575e45495Sab 	 *	_type - Symbol type
179675e45495Sab 	 *
179775e45495Sab 	 * The following are tested:
179875e45495Sab 	 *	- Symbol length is non-zero
179975e45495Sab 	 *	- Symbol type is a type that references code or data
180075e45495Sab 	 *	- Referenced section is not 0 (indicates an UNDEF symbol)
180175e45495Sab 	 *	  and is not in the range of special values above SHN_LORESERVE
180275e45495Sab 	 *	  (excluding SHN_XINDEX, which is OK).
180375e45495Sab 	 *	- We have a valid section header for the target section
180475e45495Sab 	 *
180575e45495Sab 	 * If the above are all true, and the symbol position is not
180675e45495Sab 	 * contained by the target section, this macro evaluates to
180775e45495Sab 	 * True (1). Otherwise, False(0).
180875e45495Sab 	 */
180975e45495Sab #define	SYM_LOC_BADADDR(_sdp, _sym, _type) \
1810d579eb63Sab 	(_sym->st_size && dynsymsort_symtype[_type] && \
181175e45495Sab 	(_sym->st_shndx != SHN_UNDEF) && \
181275e45495Sab 	((_sym->st_shndx < SHN_LORESERVE) || \
181375e45495Sab 		(_sym->st_shndx == SHN_XINDEX)) && \
181475e45495Sab 	_sdp->sd_isc && _sdp->sd_isc->is_shdr && \
181575e45495Sab 	((_sym->st_value + _sym->st_size) > _sdp->sd_isc->is_shdr->sh_size))
181675e45495Sab 
1817de777a60Sab 	Conv_inv_buf_t	inv_buf;
18187c478bd9Sstevel@tonic-gate 	Sym		*sym = (Sym *)isc->is_indata->d_buf;
18197c478bd9Sstevel@tonic-gate 	Word		*symshndx = 0;
18207c478bd9Sstevel@tonic-gate 	Shdr		*shdr = isc->is_shdr;
18217c478bd9Sstevel@tonic-gate 	Sym_desc	*sdp;
18227c478bd9Sstevel@tonic-gate 	size_t		strsize;
18237c478bd9Sstevel@tonic-gate 	char		*strs;
18247c478bd9Sstevel@tonic-gate 	uchar_t		type, bind;
18257c478bd9Sstevel@tonic-gate 	Word		ndx, hash, local, total;
18267c478bd9Sstevel@tonic-gate 	Half		etype = ifl->ifl_ehdr->e_type;
182775e45495Sab 	int		etype_rel;
18287c478bd9Sstevel@tonic-gate 	const char	*symsecname, *strsecname;
18297c478bd9Sstevel@tonic-gate 	avl_index_t	where;
1830d840867fSab 	int		test_gnu_hidden_bit;
18317c478bd9Sstevel@tonic-gate 
18327c478bd9Sstevel@tonic-gate 	/*
18337c478bd9Sstevel@tonic-gate 	 * Its possible that a file may contain more that one symbol table,
18347c478bd9Sstevel@tonic-gate 	 * ie. .dynsym and .symtab in a shared library.  Only process the first
18357c478bd9Sstevel@tonic-gate 	 * table (here, we assume .dynsym comes before .symtab).
18367c478bd9Sstevel@tonic-gate 	 */
18377c478bd9Sstevel@tonic-gate 	if (ifl->ifl_symscnt)
18387c478bd9Sstevel@tonic-gate 		return (1);
18397c478bd9Sstevel@tonic-gate 
18407c478bd9Sstevel@tonic-gate 	if (isc->is_symshndx)
18417c478bd9Sstevel@tonic-gate 		symshndx = isc->is_symshndx->is_indata->d_buf;
18427c478bd9Sstevel@tonic-gate 
18435aefb655Srie 	DBG_CALL(Dbg_syms_process(ofl->ofl_lml, ifl));
18447c478bd9Sstevel@tonic-gate 
18457c478bd9Sstevel@tonic-gate 	if (isc->is_name)
18467c478bd9Sstevel@tonic-gate 		symsecname = isc->is_name;
18477c478bd9Sstevel@tonic-gate 	else
18487c478bd9Sstevel@tonic-gate 		symsecname = MSG_ORIG(MSG_STR_EMPTY);
18497c478bd9Sstevel@tonic-gate 
18507c478bd9Sstevel@tonic-gate 	/*
18517c478bd9Sstevel@tonic-gate 	 * From the symbol tables section header information determine which
18527c478bd9Sstevel@tonic-gate 	 * strtab table is needed to locate the actual symbol names.
18537c478bd9Sstevel@tonic-gate 	 */
18547c478bd9Sstevel@tonic-gate 	if (ifl->ifl_flags & FLG_IF_HSTRTAB) {
18557c478bd9Sstevel@tonic-gate 		ndx = shdr->sh_link;
18567c478bd9Sstevel@tonic-gate 		if ((ndx == 0) || (ndx >= ifl->ifl_shnum)) {
18575aefb655Srie 			eprintf(ofl->ofl_lml, ERR_FATAL,
18585aefb655Srie 			    MSG_INTL(MSG_FIL_INVSHLINK),
18597c478bd9Sstevel@tonic-gate 			    ifl->ifl_name, symsecname, EC_XWORD(ndx));
18607c478bd9Sstevel@tonic-gate 			return (S_ERROR);
18617c478bd9Sstevel@tonic-gate 		}
18627c478bd9Sstevel@tonic-gate 		strsize = ifl->ifl_isdesc[ndx]->is_shdr->sh_size;
18637c478bd9Sstevel@tonic-gate 		strs = ifl->ifl_isdesc[ndx]->is_indata->d_buf;
18647c478bd9Sstevel@tonic-gate 		if (ifl->ifl_isdesc[ndx]->is_name)
18657c478bd9Sstevel@tonic-gate 			strsecname = ifl->ifl_isdesc[ndx]->is_name;
18667c478bd9Sstevel@tonic-gate 		else
18677c478bd9Sstevel@tonic-gate 			strsecname = MSG_ORIG(MSG_STR_EMPTY);
18687c478bd9Sstevel@tonic-gate 	} else {
18697c478bd9Sstevel@tonic-gate 		/*
18707c478bd9Sstevel@tonic-gate 		 * There is no string table section in this input file
18717c478bd9Sstevel@tonic-gate 		 * although there are symbols in this symbol table section.
18727c478bd9Sstevel@tonic-gate 		 * This means that these symbols do not have names.
18737c478bd9Sstevel@tonic-gate 		 * Currently, only scratch register symbols are allowed
18747c478bd9Sstevel@tonic-gate 		 * not to have names.
18757c478bd9Sstevel@tonic-gate 		 */
18767c478bd9Sstevel@tonic-gate 		strsize = 0;
18777c478bd9Sstevel@tonic-gate 		strs = (char *)MSG_ORIG(MSG_STR_EMPTY);
18787c478bd9Sstevel@tonic-gate 		strsecname = MSG_ORIG(MSG_STR_EMPTY);
18797c478bd9Sstevel@tonic-gate 	}
18807c478bd9Sstevel@tonic-gate 
18817c478bd9Sstevel@tonic-gate 	/*
18827c478bd9Sstevel@tonic-gate 	 * Determine the number of local symbols together with the total
18837c478bd9Sstevel@tonic-gate 	 * number we have to process.
18847c478bd9Sstevel@tonic-gate 	 */
18857c478bd9Sstevel@tonic-gate 	total = (Word)(shdr->sh_size / shdr->sh_entsize);
18867c478bd9Sstevel@tonic-gate 	local = shdr->sh_info;
18877c478bd9Sstevel@tonic-gate 
18887c478bd9Sstevel@tonic-gate 	/*
18897c478bd9Sstevel@tonic-gate 	 * Allocate a symbol table index array and a local symbol array
18907c478bd9Sstevel@tonic-gate 	 * (global symbols are processed and added to the ofl->ofl_symbkt[]
18917c478bd9Sstevel@tonic-gate 	 * array).  If we are dealing with a relocatable object, allocate the
18927c478bd9Sstevel@tonic-gate 	 * local symbol descriptors.  If this isn't a relocatable object we
18937c478bd9Sstevel@tonic-gate 	 * still have to process any shared object locals to determine if any
18947c478bd9Sstevel@tonic-gate 	 * register symbols exist.  Although these aren't added to the output
18957c478bd9Sstevel@tonic-gate 	 * image, they are used as part of symbol resolution.
18967c478bd9Sstevel@tonic-gate 	 */
18977c478bd9Sstevel@tonic-gate 	if ((ifl->ifl_oldndx = libld_malloc((size_t)(total *
18987c478bd9Sstevel@tonic-gate 	    sizeof (Sym_desc *)))) == 0)
18997c478bd9Sstevel@tonic-gate 		return (S_ERROR);
1900d579eb63Sab 	etype_rel = (etype == ET_REL);
190175e45495Sab 	if (etype_rel && local) {
19027c478bd9Sstevel@tonic-gate 		if ((ifl->ifl_locs =
19037c478bd9Sstevel@tonic-gate 		    libld_calloc(sizeof (Sym_desc), local)) == 0)
19047c478bd9Sstevel@tonic-gate 			return (S_ERROR);
19057c478bd9Sstevel@tonic-gate 		/* LINTED */
19067c478bd9Sstevel@tonic-gate 		ifl->ifl_locscnt = (Word)local;
19077c478bd9Sstevel@tonic-gate 	}
19087c478bd9Sstevel@tonic-gate 	ifl->ifl_symscnt = total;
19097c478bd9Sstevel@tonic-gate 
19107c478bd9Sstevel@tonic-gate 	/*
19117c478bd9Sstevel@tonic-gate 	 * If there are local symbols to save add them to the symbol table
19127c478bd9Sstevel@tonic-gate 	 * index array.
19137c478bd9Sstevel@tonic-gate 	 */
19147c478bd9Sstevel@tonic-gate 	if (local) {
19159039eeafSab 		int allow_ldynsym = OFL_ALLOW_LDYNSYM(ofl);
19167c478bd9Sstevel@tonic-gate 		for (sym++, ndx = 1; ndx < local; sym++, ndx++) {
19177c478bd9Sstevel@tonic-gate 			Word		shndx, sdflags = FLG_SY_CLEAN;
19187c478bd9Sstevel@tonic-gate 			const char	*name;
19197c478bd9Sstevel@tonic-gate 			Sym_desc	*rsdp;
1920*ba2be530Sab 			int		shndx_bad = 0;
19217c478bd9Sstevel@tonic-gate 
19227c478bd9Sstevel@tonic-gate 			/*
1923*ba2be530Sab 			 * Determine and validate the associated section index.
19247c478bd9Sstevel@tonic-gate 			 */
1925*ba2be530Sab 			if (symshndx && (sym->st_shndx == SHN_XINDEX)) {
19267c478bd9Sstevel@tonic-gate 				shndx = symshndx[ndx];
1927*ba2be530Sab 			} else if ((shndx = sym->st_shndx) >= SHN_LORESERVE) {
19287c478bd9Sstevel@tonic-gate 				sdflags |= FLG_SY_SPECSEC;
1929*ba2be530Sab 			} else if (shndx > ifl->ifl_ehdr->e_shnum) {
1930*ba2be530Sab 				/* We need the name before we can issue error */
1931*ba2be530Sab 				shndx_bad = 1;
1932*ba2be530Sab 			}
19337c478bd9Sstevel@tonic-gate 
19347c478bd9Sstevel@tonic-gate 			/*
19357c478bd9Sstevel@tonic-gate 			 * Check if st_name has a valid value or not.
19367c478bd9Sstevel@tonic-gate 			 */
19375aefb655Srie 			if ((name = string(ofl, ifl, sym, strs, strsize, ndx,
19387c478bd9Sstevel@tonic-gate 			    shndx, symsecname, strsecname, &sdflags)) == 0) {
19397c478bd9Sstevel@tonic-gate 				ofl->ofl_flags |= FLG_OF_FATAL;
19407c478bd9Sstevel@tonic-gate 				continue;
19417c478bd9Sstevel@tonic-gate 			}
19427c478bd9Sstevel@tonic-gate 
1943*ba2be530Sab 			/*
1944*ba2be530Sab 			 * Now that we have the name, if the section index
1945*ba2be530Sab 			 * was bad, report it.
1946*ba2be530Sab 			 */
1947*ba2be530Sab 			if (shndx_bad) {
1948*ba2be530Sab 				eprintf(ofl->ofl_lml, ERR_WARNING,
1949*ba2be530Sab 				    MSG_INTL(MSG_SYM_INVSHNDX),
1950*ba2be530Sab 				    demangle_symname(name, isc->is_name, ndx),
1951*ba2be530Sab 				    ifl->ifl_name,
1952*ba2be530Sab 				    conv_sym_shndx(sym->st_shndx, &inv_buf));
1953*ba2be530Sab 				continue;
1954*ba2be530Sab 			}
1955*ba2be530Sab 
19567c478bd9Sstevel@tonic-gate 			/*
19577c478bd9Sstevel@tonic-gate 			 * If this local symbol table originates from a shared
19587c478bd9Sstevel@tonic-gate 			 * object, then we're only interested in recording
19597c478bd9Sstevel@tonic-gate 			 * register symbols.  As local symbol descriptors aren't
19607c478bd9Sstevel@tonic-gate 			 * allocated for shared objects, one will be allocated
19617c478bd9Sstevel@tonic-gate 			 * to associated with the register symbol.  This symbol
19627c478bd9Sstevel@tonic-gate 			 * won't become part of the output image, but we must
19637c478bd9Sstevel@tonic-gate 			 * process it to test for register conflicts.
19647c478bd9Sstevel@tonic-gate 			 */
19657c478bd9Sstevel@tonic-gate 			rsdp = sdp = 0;
19667c478bd9Sstevel@tonic-gate 			if (sdflags & FLG_SY_REGSYM) {
1967*ba2be530Sab 				/*
1968*ba2be530Sab 				 * The presence of FLG_SY_REGSYM means that
1969*ba2be530Sab 				 * the pointers in ld_targ.t_ms are non-NULL.
1970*ba2be530Sab 				 */
1971*ba2be530Sab 				rsdp = (*ld_targ.t_ms.ms_reg_find)(sym, ofl);
1972*ba2be530Sab 				if (rsdp != 0) {
19737c478bd9Sstevel@tonic-gate 					/*
19747c478bd9Sstevel@tonic-gate 					 * The fact that another register def-
19757c478bd9Sstevel@tonic-gate 					 * inition has been found is fatal.
19767c478bd9Sstevel@tonic-gate 					 * Call the verification routine to get
19777c478bd9Sstevel@tonic-gate 					 * the error message and move on.
19787c478bd9Sstevel@tonic-gate 					 */
1979*ba2be530Sab 					(void) (*ld_targ.t_ms.ms_reg_check)
1980*ba2be530Sab 					    (rsdp, sym, name, ifl, ofl);
19817c478bd9Sstevel@tonic-gate 					continue;
19827c478bd9Sstevel@tonic-gate 				}
19837c478bd9Sstevel@tonic-gate 
19847c478bd9Sstevel@tonic-gate 				if (etype == ET_DYN) {
19857c478bd9Sstevel@tonic-gate 					if ((sdp = libld_calloc(
19867c478bd9Sstevel@tonic-gate 					    sizeof (Sym_desc), 1)) == 0)
19877c478bd9Sstevel@tonic-gate 						return (S_ERROR);
19887c478bd9Sstevel@tonic-gate 					sdp->sd_ref = REF_DYN_SEEN;
19897c478bd9Sstevel@tonic-gate 				}
19907c478bd9Sstevel@tonic-gate 			} else if (etype == ET_DYN)
19917c478bd9Sstevel@tonic-gate 				continue;
19927c478bd9Sstevel@tonic-gate 
19937c478bd9Sstevel@tonic-gate 			/*
19947c478bd9Sstevel@tonic-gate 			 * Fill in the remaining symbol descriptor information.
19957c478bd9Sstevel@tonic-gate 			 */
19967c478bd9Sstevel@tonic-gate 			if (sdp == 0) {
19977c478bd9Sstevel@tonic-gate 				sdp = &(ifl->ifl_locs[ndx]);
19987c478bd9Sstevel@tonic-gate 				sdp->sd_ref = REF_REL_NEED;
19997c478bd9Sstevel@tonic-gate 			}
20007c478bd9Sstevel@tonic-gate 			if (rsdp == 0) {
20017c478bd9Sstevel@tonic-gate 				sdp->sd_name = name;
20027c478bd9Sstevel@tonic-gate 				sdp->sd_sym = sym;
20037c478bd9Sstevel@tonic-gate 				sdp->sd_shndx = shndx;
20047c478bd9Sstevel@tonic-gate 				sdp->sd_flags = sdflags;
20057c478bd9Sstevel@tonic-gate 				sdp->sd_file = ifl;
20067c478bd9Sstevel@tonic-gate 				ifl->ifl_oldndx[ndx] = sdp;
20077c478bd9Sstevel@tonic-gate 			}
20087c478bd9Sstevel@tonic-gate 
20095aefb655Srie 			DBG_CALL(Dbg_syms_entry(ofl->ofl_lml, ndx, sdp));
20107c478bd9Sstevel@tonic-gate 
20117c478bd9Sstevel@tonic-gate 			/*
20127c478bd9Sstevel@tonic-gate 			 * Reclassify any SHN_SUNW_IGNORE symbols to SHN_UNDEF
20137c478bd9Sstevel@tonic-gate 			 * so as to simplify future processing.
20147c478bd9Sstevel@tonic-gate 			 */
20150bc07c75Srie 			if (sym->st_shndx == SHN_SUNW_IGNORE) {
20167c478bd9Sstevel@tonic-gate 				sdp->sd_shndx = shndx = SHN_UNDEF;
20177c478bd9Sstevel@tonic-gate 				sdp->sd_flags1 |=
20187c478bd9Sstevel@tonic-gate 				    (FLG_SY1_IGNORE | FLG_SY1_ELIM);
20197c478bd9Sstevel@tonic-gate 			}
20207c478bd9Sstevel@tonic-gate 
20217c478bd9Sstevel@tonic-gate 			/*
20227c478bd9Sstevel@tonic-gate 			 * Process any register symbols.
20237c478bd9Sstevel@tonic-gate 			 */
20247c478bd9Sstevel@tonic-gate 			if (sdp->sd_flags & FLG_SY_REGSYM) {
20257c478bd9Sstevel@tonic-gate 				/*
20267c478bd9Sstevel@tonic-gate 				 * Add a diagnostic to indicate we've caught a
20277c478bd9Sstevel@tonic-gate 				 * register symbol, as this can be useful if a
20287c478bd9Sstevel@tonic-gate 				 * register conflict is later discovered.
20297c478bd9Sstevel@tonic-gate 				 */
20305aefb655Srie 				DBG_CALL(Dbg_syms_entered(ofl, sym, sdp));
20317c478bd9Sstevel@tonic-gate 
20327c478bd9Sstevel@tonic-gate 				/*
20337c478bd9Sstevel@tonic-gate 				 * If this register symbol hasn't already been
20347c478bd9Sstevel@tonic-gate 				 * recorded, enter it now.
2035*ba2be530Sab 				 *
2036*ba2be530Sab 				 * The presence of FLG_SY_REGSYM means that
2037*ba2be530Sab 				 * the pointers in ld_targ.t_ms are non-NULL.
20387c478bd9Sstevel@tonic-gate 				 */
20395aefb655Srie 				if ((rsdp == 0) &&
2040*ba2be530Sab 				    ((*ld_targ.t_ms.ms_reg_enter)(sdp, ofl) ==
2041*ba2be530Sab 				    0))
20427c478bd9Sstevel@tonic-gate 					return (S_ERROR);
20437c478bd9Sstevel@tonic-gate 			}
20447c478bd9Sstevel@tonic-gate 
20457c478bd9Sstevel@tonic-gate 			/*
20467c478bd9Sstevel@tonic-gate 			 * Assign an input section.
20477c478bd9Sstevel@tonic-gate 			 */
20480bc07c75Srie 			if ((sym->st_shndx != SHN_UNDEF) &&
20497c478bd9Sstevel@tonic-gate 			    ((sdp->sd_flags & FLG_SY_SPECSEC) == 0))
20507c478bd9Sstevel@tonic-gate 				sdp->sd_isc = ifl->ifl_isdesc[shndx];
20517c478bd9Sstevel@tonic-gate 
20527c478bd9Sstevel@tonic-gate 			/*
20537c478bd9Sstevel@tonic-gate 			 * If this symbol falls within the range of a section
20547c478bd9Sstevel@tonic-gate 			 * being discarded, then discard the symbol itself.
20557c478bd9Sstevel@tonic-gate 			 * There is no reason to keep this local symbol.
20567c478bd9Sstevel@tonic-gate 			 */
20577c478bd9Sstevel@tonic-gate 			if (sdp->sd_isc &&
20587c478bd9Sstevel@tonic-gate 			    (sdp->sd_isc->is_flags & FLG_IS_DISCARD)) {
20597c478bd9Sstevel@tonic-gate 				sdp->sd_flags |= FLG_SY_ISDISC;
2060a194faf8Srie 				DBG_CALL(Dbg_syms_discarded(ofl->ofl_lml, sdp));
20617c478bd9Sstevel@tonic-gate 				continue;
20627c478bd9Sstevel@tonic-gate 			}
20637c478bd9Sstevel@tonic-gate 
20647c478bd9Sstevel@tonic-gate 			/*
20657c478bd9Sstevel@tonic-gate 			 * Skip any section symbols as new versions of these
20667c478bd9Sstevel@tonic-gate 			 * will be created.
20677c478bd9Sstevel@tonic-gate 			 */
20687c478bd9Sstevel@tonic-gate 			if ((type = ELF_ST_TYPE(sym->st_info)) == STT_SECTION) {
20690bc07c75Srie 				if (sym->st_shndx == SHN_UNDEF) {
20705aefb655Srie 					eprintf(ofl->ofl_lml, ERR_WARNING,
20717c478bd9Sstevel@tonic-gate 					    MSG_INTL(MSG_SYM_INVSHNDX),
2072*ba2be530Sab 					    demangle_symname(name, isc->is_name,
2073*ba2be530Sab 					    ndx), ifl->ifl_name,
2074de777a60Sab 					    conv_sym_shndx(sym->st_shndx,
2075de777a60Sab 					    &inv_buf));
20767c478bd9Sstevel@tonic-gate 				}
20777c478bd9Sstevel@tonic-gate 				continue;
20787c478bd9Sstevel@tonic-gate 			}
20797c478bd9Sstevel@tonic-gate 
208075e45495Sab 			/*
208175e45495Sab 			 * For a relocatable object, if this symbol is defined
208275e45495Sab 			 * and has non-zero length and references an address
208375e45495Sab 			 * within an associated section, then check its extents
208475e45495Sab 			 * to make sure the section boundaries encompass it.
208575e45495Sab 			 * If they don't, the ELF file is corrupt.
208675e45495Sab 			 */
208775e45495Sab 			if (etype_rel && SYM_LOC_BADADDR(sdp, sym, type)) {
208869a0bf0cSab 				issue_badaddr_msg(ifl, ofl, sdp, sym, shndx);
208975e45495Sab 				continue;
209075e45495Sab 			}
209175e45495Sab 
20927c478bd9Sstevel@tonic-gate 			/*
20937c478bd9Sstevel@tonic-gate 			 * Sanity check for TLS
20947c478bd9Sstevel@tonic-gate 			 */
20950bc07c75Srie 			if ((sym->st_size != 0) && ((type == STT_TLS) &&
20960bc07c75Srie 			    (sym->st_shndx != SHN_COMMON))) {
20977c478bd9Sstevel@tonic-gate 				Is_desc	*isp = sdp->sd_isc;
20987c478bd9Sstevel@tonic-gate 
20997c478bd9Sstevel@tonic-gate 				if ((isp == 0) || (isp->is_shdr == 0) ||
21007c478bd9Sstevel@tonic-gate 				    ((isp->is_shdr->sh_flags & SHF_TLS) == 0)) {
21015aefb655Srie 					eprintf(ofl->ofl_lml, ERR_FATAL,
21027c478bd9Sstevel@tonic-gate 					    MSG_INTL(MSG_SYM_TLS),
21037c478bd9Sstevel@tonic-gate 					    demangle(sdp->sd_name),
21047c478bd9Sstevel@tonic-gate 					    ifl->ifl_name);
21057c478bd9Sstevel@tonic-gate 					ofl->ofl_flags |= FLG_OF_FATAL;
21067c478bd9Sstevel@tonic-gate 					continue;
21077c478bd9Sstevel@tonic-gate 				}
21087c478bd9Sstevel@tonic-gate 			}
21097c478bd9Sstevel@tonic-gate 
21107c478bd9Sstevel@tonic-gate 			/*
21117c478bd9Sstevel@tonic-gate 			 * Carry our some basic sanity checks (these are just
21127c478bd9Sstevel@tonic-gate 			 * some of the erroneous symbol entries we've come
21137c478bd9Sstevel@tonic-gate 			 * across, there's probably a lot more).  The symbol
21147c478bd9Sstevel@tonic-gate 			 * will not be carried forward to the output file, which
21157c478bd9Sstevel@tonic-gate 			 * won't be a problem unless a relocation is required
21167c478bd9Sstevel@tonic-gate 			 * against it.
21177c478bd9Sstevel@tonic-gate 			 */
21187c478bd9Sstevel@tonic-gate 			if (((sdp->sd_flags & FLG_SY_SPECSEC) &&
21190bc07c75Srie 			    ((sym->st_shndx == SHN_COMMON)) ||
21200bc07c75Srie 			    ((type == STT_FILE) &&
21210bc07c75Srie 			    (sym->st_shndx != SHN_ABS))) ||
21227c478bd9Sstevel@tonic-gate 			    (sdp->sd_isc && (sdp->sd_isc->is_osdesc == 0))) {
21235aefb655Srie 				eprintf(ofl->ofl_lml, ERR_WARNING,
21245aefb655Srie 				    MSG_INTL(MSG_SYM_INVSHNDX),
2125*ba2be530Sab 				    demangle_symname(name, isc->is_name, ndx),
2126*ba2be530Sab 				    ifl->ifl_name,
2127de777a60Sab 				    conv_sym_shndx(sym->st_shndx, &inv_buf));
21287c478bd9Sstevel@tonic-gate 				sdp->sd_isc = NULL;
21297c478bd9Sstevel@tonic-gate 				sdp->sd_flags |= FLG_SY_INVALID;
21307c478bd9Sstevel@tonic-gate 				continue;
21317c478bd9Sstevel@tonic-gate 			}
21327c478bd9Sstevel@tonic-gate 
21337c478bd9Sstevel@tonic-gate 			/*
21347c478bd9Sstevel@tonic-gate 			 * As these local symbols will become part of the output
21357c478bd9Sstevel@tonic-gate 			 * image, record their number and name string size.
21367c478bd9Sstevel@tonic-gate 			 * Globals are counted after all input file processing
21377c478bd9Sstevel@tonic-gate 			 * (and hence symbol resolution) is complete during
21387c478bd9Sstevel@tonic-gate 			 * sym_validate().
21397c478bd9Sstevel@tonic-gate 			 */
21407c478bd9Sstevel@tonic-gate 			if (!(ofl->ofl_flags1 & FLG_OF1_REDLSYM)) {
21417c478bd9Sstevel@tonic-gate 				ofl->ofl_locscnt++;
21427c478bd9Sstevel@tonic-gate 
21437c478bd9Sstevel@tonic-gate 				if ((((sdp->sd_flags & FLG_SY_REGSYM) == 0) ||
21447c478bd9Sstevel@tonic-gate 				    sym->st_name) && (st_insert(ofl->ofl_strtab,
21457c478bd9Sstevel@tonic-gate 				    sdp->sd_name) == -1))
21467c478bd9Sstevel@tonic-gate 					return (S_ERROR);
21479039eeafSab 
21489039eeafSab 				if (allow_ldynsym && sym->st_name &&
2149d579eb63Sab 				    ldynsym_symtype[type]) {
21509039eeafSab 					ofl->ofl_dynlocscnt++;
21519039eeafSab 					if (st_insert(ofl->ofl_dynstrtab,
21529039eeafSab 					    sdp->sd_name) == -1)
21539039eeafSab 						return (S_ERROR);
2154d579eb63Sab 					/* Include it in sort section? */
2155d579eb63Sab 					DYNSORT_COUNT(sdp, sym, type, ++);
21569039eeafSab 				}
21577c478bd9Sstevel@tonic-gate 			}
21587c478bd9Sstevel@tonic-gate 		}
21597c478bd9Sstevel@tonic-gate 	}
21607c478bd9Sstevel@tonic-gate 
2161d840867fSab 	/*
2162d840867fSab 	 * The GNU ld interprets the top bit of the 16-bit Versym value
2163d840867fSab 	 * (0x8000) as the "hidden" bit. If this bit is set, the linker
2164d840867fSab 	 * is supposed to act as if that symbol does not exist. The Solaris
2165d840867fSab 	 * linker does not support this mechanism, or the model of interface
2166d840867fSab 	 * evolution that it allows, but we honor it in GNU ld produced
2167d840867fSab 	 * objects in order to interoperate with them.
2168d840867fSab 	 *
2169d840867fSab 	 * Determine if we should honor the GNU hidden bit for this file.
2170d840867fSab 	 */
2171d840867fSab 	test_gnu_hidden_bit = ((ifl->ifl_flags & FLG_IF_GNUVER) != 0) &&
2172d840867fSab 	    (ifl->ifl_versym != NULL);
2173d840867fSab 
21747c478bd9Sstevel@tonic-gate 	/*
21757c478bd9Sstevel@tonic-gate 	 * Now scan the global symbols entering them in the internal symbol
21767c478bd9Sstevel@tonic-gate 	 * table or resolving them as necessary.
21777c478bd9Sstevel@tonic-gate 	 */
21787c478bd9Sstevel@tonic-gate 	sym = (Sym *)isc->is_indata->d_buf;
21797c478bd9Sstevel@tonic-gate 	sym += local;
21807c478bd9Sstevel@tonic-gate 	/* LINTED */
21817c478bd9Sstevel@tonic-gate 	for (ndx = (int)local; ndx < total; sym++, ndx++) {
21827c478bd9Sstevel@tonic-gate 		const char	*name;
21837c478bd9Sstevel@tonic-gate 		Word		shndx, sdflags = 0;
2184*ba2be530Sab 		int		shndx_bad = 0;
21857c478bd9Sstevel@tonic-gate 
21867c478bd9Sstevel@tonic-gate 		/*
2187*ba2be530Sab 		 * Determine and validate the associated section index.
21887c478bd9Sstevel@tonic-gate 		 */
21897c478bd9Sstevel@tonic-gate 		if (symshndx && (sym->st_shndx == SHN_XINDEX)) {
21907c478bd9Sstevel@tonic-gate 			shndx = symshndx[ndx];
2191*ba2be530Sab 		} else if ((shndx = sym->st_shndx) >= SHN_LORESERVE) {
2192*ba2be530Sab 			sdflags |= FLG_SY_SPECSEC;
2193*ba2be530Sab 		} else if (shndx > ifl->ifl_ehdr->e_shnum) {
2194*ba2be530Sab 			/* We need the name before we can issue error */
2195*ba2be530Sab 			shndx_bad = 1;
21967c478bd9Sstevel@tonic-gate 		}
21977c478bd9Sstevel@tonic-gate 
21987c478bd9Sstevel@tonic-gate 		/*
21997c478bd9Sstevel@tonic-gate 		 * Check if st_name has a valid value or not.
22007c478bd9Sstevel@tonic-gate 		 */
22015aefb655Srie 		if ((name = string(ofl, ifl, sym, strs, strsize, ndx, shndx,
22027c478bd9Sstevel@tonic-gate 		    symsecname, strsecname, &sdflags)) == 0) {
22037c478bd9Sstevel@tonic-gate 			ofl->ofl_flags |= FLG_OF_FATAL;
22047c478bd9Sstevel@tonic-gate 			continue;
22057c478bd9Sstevel@tonic-gate 		}
22067c478bd9Sstevel@tonic-gate 
2207*ba2be530Sab 		/*
2208*ba2be530Sab 		 * Now that we have the name, if the section index
2209*ba2be530Sab 		 * was bad, report it.
2210*ba2be530Sab 		 */
2211*ba2be530Sab 		if (shndx_bad) {
2212*ba2be530Sab 			eprintf(ofl->ofl_lml, ERR_WARNING,
2213*ba2be530Sab 			    MSG_INTL(MSG_SYM_INVSHNDX),
2214*ba2be530Sab 			    demangle_symname(name, isc->is_name, ndx),
2215*ba2be530Sab 			    ifl->ifl_name,
2216*ba2be530Sab 			    conv_sym_shndx(sym->st_shndx, &inv_buf));
2217*ba2be530Sab 			continue;
2218*ba2be530Sab 		}
2219*ba2be530Sab 
2220*ba2be530Sab 
22213b41b08bSab 		/*
2222d840867fSab 		 * Test for the GNU hidden bit, and ignore symbols that
2223d840867fSab 		 * have it set.
22243b41b08bSab 		 */
2225d840867fSab 		if (test_gnu_hidden_bit &&
2226d840867fSab 		    ((ifl->ifl_versym[ndx] & 0x8000) != 0))
22273b41b08bSab 			continue;
22283b41b08bSab 
22297c478bd9Sstevel@tonic-gate 		/*
22307c478bd9Sstevel@tonic-gate 		 * The linker itself will generate symbols for _end, _etext,
22317c478bd9Sstevel@tonic-gate 		 * _edata, _DYNAMIC and _PROCEDURE_LINKAGE_TABLE_, so don't
22327c478bd9Sstevel@tonic-gate 		 * bother entering these symbols from shared objects.  This
22337c478bd9Sstevel@tonic-gate 		 * results in some wasted resolution processing, which is hard
22347c478bd9Sstevel@tonic-gate 		 * to feel, but if nothing else, pollutes diagnostic relocation
22357c478bd9Sstevel@tonic-gate 		 * output.
22367c478bd9Sstevel@tonic-gate 		 */
22377c478bd9Sstevel@tonic-gate 		if (name[0] && (etype == ET_DYN) && (sym->st_size == 0) &&
22387c478bd9Sstevel@tonic-gate 		    (ELF_ST_TYPE(sym->st_info) == STT_OBJECT) &&
22397c478bd9Sstevel@tonic-gate 		    (name[0] == '_') && ((name[1] == 'e') ||
22407c478bd9Sstevel@tonic-gate 		    (name[1] == 'D') || (name[1] == 'P')) &&
22417c478bd9Sstevel@tonic-gate 		    ((strcmp(name, MSG_ORIG(MSG_SYM_ETEXT_U)) == 0) ||
22427c478bd9Sstevel@tonic-gate 		    (strcmp(name, MSG_ORIG(MSG_SYM_EDATA_U)) == 0) ||
22437c478bd9Sstevel@tonic-gate 		    (strcmp(name, MSG_ORIG(MSG_SYM_END_U)) == 0) ||
22447c478bd9Sstevel@tonic-gate 		    (strcmp(name, MSG_ORIG(MSG_SYM_DYNAMIC_U)) == 0) ||
22457c478bd9Sstevel@tonic-gate 		    (strcmp(name, MSG_ORIG(MSG_SYM_PLKTBL_U)) == 0))) {
22467c478bd9Sstevel@tonic-gate 			ifl->ifl_oldndx[ndx] = 0;
22477c478bd9Sstevel@tonic-gate 			continue;
22487c478bd9Sstevel@tonic-gate 		}
22497c478bd9Sstevel@tonic-gate 
22507c478bd9Sstevel@tonic-gate 		/*
22517c478bd9Sstevel@tonic-gate 		 * Determine and validate the symbols binding.
22527c478bd9Sstevel@tonic-gate 		 */
22537c478bd9Sstevel@tonic-gate 		bind = ELF_ST_BIND(sym->st_info);
22547c478bd9Sstevel@tonic-gate 		if ((bind != STB_GLOBAL) && (bind != STB_WEAK)) {
22555aefb655Srie 			eprintf(ofl->ofl_lml, ERR_WARNING,
2256*ba2be530Sab 			    MSG_INTL(MSG_SYM_NONGLOB),
2257*ba2be530Sab 			    demangle_symname(name, isc->is_name, ndx),
2258de777a60Sab 			    ifl->ifl_name,
2259de777a60Sab 			    conv_sym_info_bind(bind, 0, &inv_buf));
22607c478bd9Sstevel@tonic-gate 			continue;
22617c478bd9Sstevel@tonic-gate 		}
22627c478bd9Sstevel@tonic-gate 
22637c478bd9Sstevel@tonic-gate 		/*
22647c478bd9Sstevel@tonic-gate 		 * If this symbol falls within the range of a section being
22657c478bd9Sstevel@tonic-gate 		 * discarded, then discard the symbol itself.
22667c478bd9Sstevel@tonic-gate 		 */
22677c478bd9Sstevel@tonic-gate 		if (((sdflags & FLG_SY_SPECSEC) == 0) &&
22680bc07c75Srie 		    (sym->st_shndx != SHN_UNDEF)) {
22697c478bd9Sstevel@tonic-gate 			Is_desc	*isp;
22707c478bd9Sstevel@tonic-gate 
22717c478bd9Sstevel@tonic-gate 			if (shndx >= ifl->ifl_shnum) {
22727c478bd9Sstevel@tonic-gate 				/*
22737c478bd9Sstevel@tonic-gate 				 * Carry our some basic sanity checks
22747c478bd9Sstevel@tonic-gate 				 * The symbol will not be carried forward to
22757c478bd9Sstevel@tonic-gate 				 * the output file, which won't be a problem
22767c478bd9Sstevel@tonic-gate 				 * unless a relocation is required against it.
22777c478bd9Sstevel@tonic-gate 				 */
22785aefb655Srie 				eprintf(ofl->ofl_lml, ERR_WARNING,
2279*ba2be530Sab 				    MSG_INTL(MSG_SYM_INVSHNDX),
2280*ba2be530Sab 				    demangle_symname(name, isc->is_name, ndx),
22810bc07c75Srie 				    ifl->ifl_name,
2282de777a60Sab 				    conv_sym_shndx(sym->st_shndx, &inv_buf));
22837c478bd9Sstevel@tonic-gate 				continue;
22847c478bd9Sstevel@tonic-gate 			}
22857c478bd9Sstevel@tonic-gate 
22867c478bd9Sstevel@tonic-gate 			isp = ifl->ifl_isdesc[shndx];
22877c478bd9Sstevel@tonic-gate 			if (isp && (isp->is_flags & FLG_IS_DISCARD)) {
22887c478bd9Sstevel@tonic-gate 				if ((sdp =
22897c478bd9Sstevel@tonic-gate 				    libld_calloc(sizeof (Sym_desc), 1)) == 0)
22907c478bd9Sstevel@tonic-gate 					return (S_ERROR);
22917c478bd9Sstevel@tonic-gate 
22927c478bd9Sstevel@tonic-gate 				/*
22937c478bd9Sstevel@tonic-gate 				 * Create a dummy symbol entry so that if we
22947c478bd9Sstevel@tonic-gate 				 * find any references to this discarded symbol
22957c478bd9Sstevel@tonic-gate 				 * we can compensate.
22967c478bd9Sstevel@tonic-gate 				 */
22977c478bd9Sstevel@tonic-gate 				sdp->sd_name = name;
22987c478bd9Sstevel@tonic-gate 				sdp->sd_sym = sym;
22997c478bd9Sstevel@tonic-gate 				sdp->sd_file = ifl;
23007c478bd9Sstevel@tonic-gate 				sdp->sd_isc = isp;
23017c478bd9Sstevel@tonic-gate 				sdp->sd_flags = FLG_SY_ISDISC;
23027c478bd9Sstevel@tonic-gate 				ifl->ifl_oldndx[ndx] = sdp;
23037c478bd9Sstevel@tonic-gate 
2304a194faf8Srie 				DBG_CALL(Dbg_syms_discarded(ofl->ofl_lml, sdp));
23057c478bd9Sstevel@tonic-gate 				continue;
23067c478bd9Sstevel@tonic-gate 			}
23077c478bd9Sstevel@tonic-gate 		}
23087c478bd9Sstevel@tonic-gate 
23097c478bd9Sstevel@tonic-gate 		/*
23107c478bd9Sstevel@tonic-gate 		 * If the symbol does not already exist in the internal symbol
23117c478bd9Sstevel@tonic-gate 		 * table add it, otherwise resolve the conflict.  If the symbol
23127c478bd9Sstevel@tonic-gate 		 * from this file is kept, retain its symbol table index for
23137c478bd9Sstevel@tonic-gate 		 * possible use in associating a global alias.
23147c478bd9Sstevel@tonic-gate 		 */
23157c478bd9Sstevel@tonic-gate 		/* LINTED */
23167c478bd9Sstevel@tonic-gate 		hash = (Word)elf_hash((const char *)name);
23175aefb655Srie 		if ((sdp = ld_sym_find(name, hash, &where, ofl)) == NULL) {
23185aefb655Srie 			DBG_CALL(Dbg_syms_global(ofl->ofl_lml, ndx, name));
23195aefb655Srie 			if ((sdp = ld_sym_enter(name, sym, hash, ifl, ofl, ndx,
23207c478bd9Sstevel@tonic-gate 			    shndx, sdflags, 0, &where)) == (Sym_desc *)S_ERROR)
23217c478bd9Sstevel@tonic-gate 				return (S_ERROR);
23227c478bd9Sstevel@tonic-gate 
23235aefb655Srie 		} else if (ld_sym_resolve(sdp, sym, ifl, ofl, ndx, shndx,
23247c478bd9Sstevel@tonic-gate 		    sdflags) == S_ERROR)
23257c478bd9Sstevel@tonic-gate 			return (S_ERROR);
23267c478bd9Sstevel@tonic-gate 
23277c478bd9Sstevel@tonic-gate 		/*
23287c478bd9Sstevel@tonic-gate 		 * After we've compared a defined symbol in one shared
23297c478bd9Sstevel@tonic-gate 		 * object, flag the symbol so we don't compare it again.
23307c478bd9Sstevel@tonic-gate 		 */
23310bc07c75Srie 		if ((etype == ET_DYN) && (sym->st_shndx != SHN_UNDEF) &&
23327c478bd9Sstevel@tonic-gate 		    ((sdp->sd_flags & FLG_SY_SOFOUND) == 0))
23337c478bd9Sstevel@tonic-gate 			sdp->sd_flags |= FLG_SY_SOFOUND;
23347c478bd9Sstevel@tonic-gate 
23357c478bd9Sstevel@tonic-gate 		/*
23367c478bd9Sstevel@tonic-gate 		 * If the symbol is accepted from this file retain the symbol
23377c478bd9Sstevel@tonic-gate 		 * index for possible use in aliasing.
23387c478bd9Sstevel@tonic-gate 		 */
23397c478bd9Sstevel@tonic-gate 		if (sdp->sd_file == ifl)
23407c478bd9Sstevel@tonic-gate 			sdp->sd_symndx = ndx;
23417c478bd9Sstevel@tonic-gate 
23427c478bd9Sstevel@tonic-gate 		ifl->ifl_oldndx[ndx] = sdp;
23437c478bd9Sstevel@tonic-gate 
23447c478bd9Sstevel@tonic-gate 		/*
23457c478bd9Sstevel@tonic-gate 		 * If we've accepted a register symbol, continue to validate
23467c478bd9Sstevel@tonic-gate 		 * it.
23477c478bd9Sstevel@tonic-gate 		 */
23487c478bd9Sstevel@tonic-gate 		if (sdp->sd_flags & FLG_SY_REGSYM) {
23497c478bd9Sstevel@tonic-gate 			Sym_desc	*rsdp;
23507c478bd9Sstevel@tonic-gate 
2351*ba2be530Sab 			/*
2352*ba2be530Sab 			 * The presence of FLG_SY_REGSYM means that
2353*ba2be530Sab 			 * the pointers in ld_targ.t_ms are non-NULL.
2354*ba2be530Sab 			 */
2355*ba2be530Sab 			rsdp = (*ld_targ.t_ms.ms_reg_find)(sdp->sd_sym, ofl);
2356*ba2be530Sab 			if (rsdp == 0) {
2357*ba2be530Sab 				if ((*ld_targ.t_ms.ms_reg_enter)(sdp, ofl) == 0)
23587c478bd9Sstevel@tonic-gate 					return (S_ERROR);
23597c478bd9Sstevel@tonic-gate 			} else if (rsdp != sdp) {
2360*ba2be530Sab 				(void) (*ld_targ.t_ms.ms_reg_check)(rsdp,
2361*ba2be530Sab 				    sdp->sd_sym, sdp->sd_name, ifl, ofl);
23627c478bd9Sstevel@tonic-gate 			}
23637c478bd9Sstevel@tonic-gate 		}
236475e45495Sab 
236575e45495Sab 		/*
236675e45495Sab 		 * For a relocatable object, if this symbol is defined
236775e45495Sab 		 * and has non-zero length and references an address
236875e45495Sab 		 * within an associated section, then check its extents
236975e45495Sab 		 * to make sure the section boundaries encompass it.
237075e45495Sab 		 * If they don't, the ELF file is corrupt. Note that this
237175e45495Sab 		 * global symbol may have come from another file to satisfy
237275e45495Sab 		 * an UNDEF symbol of the same name from this one. In that
237375e45495Sab 		 * case, we don't check it, because it was already checked
237475e45495Sab 		 * as part of its own file.
237575e45495Sab 		 */
237675e45495Sab 		if (etype_rel && (sdp->sd_file == ifl)) {
237775e45495Sab 			Sym *tsym = sdp->sd_sym;
237875e45495Sab 
237975e45495Sab 			if (SYM_LOC_BADADDR(sdp, tsym,
238075e45495Sab 			    ELF_ST_TYPE(tsym->st_info))) {
238169a0bf0cSab 				issue_badaddr_msg(ifl, ofl, sdp,
238269a0bf0cSab 				    tsym, tsym->st_shndx);
238375e45495Sab 				continue;
238475e45495Sab 			}
238575e45495Sab 		}
23867c478bd9Sstevel@tonic-gate 	}
23877c478bd9Sstevel@tonic-gate 
23887c478bd9Sstevel@tonic-gate 	/*
23897c478bd9Sstevel@tonic-gate 	 * If this is a shared object scan the globals one more time and
23907c478bd9Sstevel@tonic-gate 	 * associate any weak/global associations.  This association is needed
23917c478bd9Sstevel@tonic-gate 	 * should the weak definition satisfy a reference in the dynamic
23927c478bd9Sstevel@tonic-gate 	 * executable:
23937c478bd9Sstevel@tonic-gate 	 *
23947c478bd9Sstevel@tonic-gate 	 *  o	if the symbol is a data item it will be copied to the
23957c478bd9Sstevel@tonic-gate 	 *	executables address space, thus we must also reassociate the
23967c478bd9Sstevel@tonic-gate 	 *	alias symbol with its new location in the executable.
23977c478bd9Sstevel@tonic-gate 	 *
23987c478bd9Sstevel@tonic-gate 	 *  o	if the symbol is a function then we may need to promote	the
23997c478bd9Sstevel@tonic-gate 	 *	symbols binding from undefined weak to undefined, otherwise the
24007c478bd9Sstevel@tonic-gate 	 *	run-time linker will not generate the correct relocation error
24017c478bd9Sstevel@tonic-gate 	 *	should the symbol not be found.
24027c478bd9Sstevel@tonic-gate 	 *
24037c478bd9Sstevel@tonic-gate 	 * The true association between a weak/strong symbol pair is that both
24047c478bd9Sstevel@tonic-gate 	 * symbol entries are identical, thus first we created a sorted symbol
24057c478bd9Sstevel@tonic-gate 	 * list keyed off of the symbols value (if the value is the same chances
24067c478bd9Sstevel@tonic-gate 	 * are the rest of the symbols data is).  This list is then scanned for
24077c478bd9Sstevel@tonic-gate 	 * weak symbols, and if one is found then any strong association will
24087c478bd9Sstevel@tonic-gate 	 * exist in the following entries.  Thus we just have to scan one
24097c478bd9Sstevel@tonic-gate 	 * (typical single alias) or more (in the uncommon instance of multiple
24107c478bd9Sstevel@tonic-gate 	 * weak to strong associations) entries to determine if a match exists.
24117c478bd9Sstevel@tonic-gate 	 */
2412d579eb63Sab 	if ((OFL_ALLOW_LDYNSYM(ofl) || (etype == ET_DYN)) &&
2413d579eb63Sab 	    (total > local)) {
24147c478bd9Sstevel@tonic-gate 		Sym_desc **	sort;
24157c478bd9Sstevel@tonic-gate 		size_t		size = (total - local) * sizeof (Sym_desc *);
24167c478bd9Sstevel@tonic-gate 
24177c478bd9Sstevel@tonic-gate 		if ((sort = libld_malloc(size)) == 0)
24187c478bd9Sstevel@tonic-gate 			return (S_ERROR);
24197c478bd9Sstevel@tonic-gate 		(void) memcpy((void *)sort, &ifl->ifl_oldndx[local], size);
24207c478bd9Sstevel@tonic-gate 
24217c478bd9Sstevel@tonic-gate 		qsort(sort, (total - local), sizeof (Sym_desc *), compare);
24227c478bd9Sstevel@tonic-gate 
24237c478bd9Sstevel@tonic-gate 		for (ndx = 0; ndx < (total - local); ndx++) {
24247c478bd9Sstevel@tonic-gate 			Sym_desc *	wsdp = sort[ndx];
24257c478bd9Sstevel@tonic-gate 			Sym *		wsym;
24267c478bd9Sstevel@tonic-gate 			int		sndx;
24277c478bd9Sstevel@tonic-gate 
24287c478bd9Sstevel@tonic-gate 			if (wsdp == 0)
24297c478bd9Sstevel@tonic-gate 				continue;
24307c478bd9Sstevel@tonic-gate 
24317c478bd9Sstevel@tonic-gate 			wsym = wsdp->sd_sym;
24327c478bd9Sstevel@tonic-gate 
24337c478bd9Sstevel@tonic-gate 			if ((ELF_ST_BIND(wsym->st_info) != STB_WEAK) ||
24340bc07c75Srie 			    (wsdp->sd_sym->st_shndx == SHN_UNDEF) ||
24357c478bd9Sstevel@tonic-gate 			    (wsdp->sd_flags & FLG_SY_SPECSEC))
24367c478bd9Sstevel@tonic-gate 				continue;
24377c478bd9Sstevel@tonic-gate 
24387c478bd9Sstevel@tonic-gate 			/*
24397c478bd9Sstevel@tonic-gate 			 * We have a weak symbol, if it has a strong alias it
24407c478bd9Sstevel@tonic-gate 			 * will have been sorted to one of the following sort
24417c478bd9Sstevel@tonic-gate 			 * table entries.  Note that we could have multiple weak
24427c478bd9Sstevel@tonic-gate 			 * symbols aliased to one strong (if this occurs then
24437c478bd9Sstevel@tonic-gate 			 * the strong symbol only maintains one alias back to
24447c478bd9Sstevel@tonic-gate 			 * the last weak).
24457c478bd9Sstevel@tonic-gate 			 */
24467c478bd9Sstevel@tonic-gate 			for (sndx = ndx + 1; sndx < (total - local); sndx++) {
24477c478bd9Sstevel@tonic-gate 				Sym_desc *	ssdp = sort[sndx];
24487c478bd9Sstevel@tonic-gate 				Sym *		ssym;
24497c478bd9Sstevel@tonic-gate 
24507c478bd9Sstevel@tonic-gate 				if (ssdp == 0)
24517c478bd9Sstevel@tonic-gate 					break;
24527c478bd9Sstevel@tonic-gate 
24537c478bd9Sstevel@tonic-gate 				ssym = ssdp->sd_sym;
24547c478bd9Sstevel@tonic-gate 
24557c478bd9Sstevel@tonic-gate 				if (wsym->st_value != ssym->st_value)
24567c478bd9Sstevel@tonic-gate 					break;
24577c478bd9Sstevel@tonic-gate 
24587c478bd9Sstevel@tonic-gate 				if ((ssdp->sd_file == ifl) &&
24597c478bd9Sstevel@tonic-gate 				    (wsdp->sd_file == ifl) &&
24607c478bd9Sstevel@tonic-gate 				    (wsym->st_size == ssym->st_size) &&
24610bc07c75Srie 				    (ssdp->sd_sym->st_shndx != SHN_UNDEF) &&
24627c478bd9Sstevel@tonic-gate 				    (ELF_ST_BIND(ssym->st_info) != STB_WEAK) &&
24637c478bd9Sstevel@tonic-gate 				    ((ssdp->sd_flags & FLG_SY_SPECSEC) == 0)) {
2464d579eb63Sab 					int w_dynbits, s_dynbits;
2465d579eb63Sab 
2466d579eb63Sab 					/*
2467d579eb63Sab 					 * If a sharable object, set link
2468d579eb63Sab 					 * fields so they reference each other
2469d579eb63Sab 					 */
2470d579eb63Sab 					if (etype == ET_DYN) {
2471d579eb63Sab 						ssdp->sd_aux->sa_linkndx =
2472d579eb63Sab 						    (Word)wsdp->sd_symndx;
2473d579eb63Sab 						wsdp->sd_aux->sa_linkndx =
2474d579eb63Sab 						    (Word)ssdp->sd_symndx;
2475d579eb63Sab 					}
2476d579eb63Sab 					/*
2477d579eb63Sab 					 * Determine which of these two symbols
2478d579eb63Sab 					 * go into the sort section. If the
2479d579eb63Sab 					 * mapfile has made explicit settings
2480d579eb63Sab 					 * of the FLG_SY_*DYNSORT flags for both
2481d579eb63Sab 					 * symbols, then we do what they say.
2482d579eb63Sab 					 * If one has the DYNSORT flags set,
2483d579eb63Sab 					 * we set the NODYNSORT bit in the
2484d579eb63Sab 					 * other. And if neither has an
2485d579eb63Sab 					 * explicit setting, then we favor the
2486d579eb63Sab 					 * weak symbol because they usually
2487d579eb63Sab 					 * lack the leading underscore.
2488d579eb63Sab 					 */
2489d579eb63Sab 					w_dynbits = wsdp->sd_flags &
2490d579eb63Sab 					    (FLG_SY_DYNSORT | FLG_SY_NODYNSORT);
2491d579eb63Sab 					s_dynbits = ssdp->sd_flags &
2492d579eb63Sab 					    (FLG_SY_DYNSORT | FLG_SY_NODYNSORT);
2493d579eb63Sab 					if (!(w_dynbits && s_dynbits)) {
249433eb6ee1Sab 						if (s_dynbits) {
249533eb6ee1Sab 							if (s_dynbits ==
249633eb6ee1Sab 							    FLG_SY_DYNSORT)
249733eb6ee1Sab 							wsdp->sd_flags |=
249833eb6ee1Sab 							    FLG_SY_NODYNSORT;
249933eb6ee1Sab 						} else if (w_dynbits !=
250033eb6ee1Sab 						    FLG_SY_NODYNSORT) {
250133eb6ee1Sab 							ssdp->sd_flags |=
250233eb6ee1Sab 							    FLG_SY_NODYNSORT;
250333eb6ee1Sab 						}
2504d579eb63Sab 					}
25057c478bd9Sstevel@tonic-gate 					break;
25067c478bd9Sstevel@tonic-gate 				}
25077c478bd9Sstevel@tonic-gate 			}
25087c478bd9Sstevel@tonic-gate 		}
25097c478bd9Sstevel@tonic-gate 	}
25107c478bd9Sstevel@tonic-gate 	return (1);
251175e45495Sab 
251275e45495Sab #undef SYM_LOC_BADADDR
25137c478bd9Sstevel@tonic-gate }
25147c478bd9Sstevel@tonic-gate 
25157c478bd9Sstevel@tonic-gate /*
2516f5a18a30Srie  * Add an undefined symbol to the symbol table.  The reference originates from
2517f5a18a30Srie  * the location identifed by the message id (mid).  These references can
2518f5a18a30Srie  * originate from command line options such as -e, -u, -initarray, etc.
2519f5a18a30Srie  * (identified with MSG_INTL(MSG_STR_COMMAND)), or from internally generated
2520f5a18a30Srie  * TLS relocation references (identified with MSG_INTL(MSG_STR_TLSREL)).
25217c478bd9Sstevel@tonic-gate  */
25227c478bd9Sstevel@tonic-gate Sym_desc *
2523f5a18a30Srie ld_sym_add_u(const char *name, Ofl_desc *ofl, Msg mid)
25247c478bd9Sstevel@tonic-gate {
25257c478bd9Sstevel@tonic-gate 	Sym		*sym;
25267c478bd9Sstevel@tonic-gate 	Ifl_desc	*ifl = 0, *_ifl;
25277c478bd9Sstevel@tonic-gate 	Sym_desc	*sdp;
25287c478bd9Sstevel@tonic-gate 	Word		hash;
25297c478bd9Sstevel@tonic-gate 	Listnode	*lnp;
25307c478bd9Sstevel@tonic-gate 	avl_index_t	where;
2531f5a18a30Srie 	const char	*reference = MSG_INTL(mid);
25327c478bd9Sstevel@tonic-gate 
25337c478bd9Sstevel@tonic-gate 	/*
2534b02637afSrie 	 * As an optimization, determine whether we've already generated this
2535b02637afSrie 	 * reference.  If the symbol doesn't already exist we'll create it.
2536b02637afSrie 	 * Or if the symbol does exist from a different source, we'll resolve
2537b02637afSrie 	 * the conflict.
25387c478bd9Sstevel@tonic-gate 	 */
25397c478bd9Sstevel@tonic-gate 	/* LINTED */
25407c478bd9Sstevel@tonic-gate 	hash = (Word)elf_hash(name);
2541b02637afSrie 	if ((sdp = ld_sym_find(name, hash, &where, ofl)) != NULL) {
2542b02637afSrie 		if ((sdp->sd_sym->st_shndx == SHN_UNDEF) &&
2543b02637afSrie 		    (sdp->sd_file->ifl_name == reference))
2544b02637afSrie 			return (sdp);
25457c478bd9Sstevel@tonic-gate 	}
25467c478bd9Sstevel@tonic-gate 
25477c478bd9Sstevel@tonic-gate 	/*
25487c478bd9Sstevel@tonic-gate 	 * Determine whether a pseudo input file descriptor exists to represent
25497c478bd9Sstevel@tonic-gate 	 * the command line, as any global symbol needs an input file descriptor
25507c478bd9Sstevel@tonic-gate 	 * during any symbol resolution (refer to map_ifl() which provides a
25517c478bd9Sstevel@tonic-gate 	 * similar method for adding symbols from mapfiles).
25527c478bd9Sstevel@tonic-gate 	 */
25537c478bd9Sstevel@tonic-gate 	for (LIST_TRAVERSE(&ofl->ofl_objs, lnp, _ifl))
2554f5a18a30Srie 		if (strcmp(_ifl->ifl_name, reference) == 0) {
25557c478bd9Sstevel@tonic-gate 			ifl = _ifl;
25567c478bd9Sstevel@tonic-gate 			break;
25577c478bd9Sstevel@tonic-gate 		}
25587c478bd9Sstevel@tonic-gate 
25597c478bd9Sstevel@tonic-gate 	/*
25607c478bd9Sstevel@tonic-gate 	 * If no descriptor exists create one.
25617c478bd9Sstevel@tonic-gate 	 */
25627c478bd9Sstevel@tonic-gate 	if (ifl == 0) {
25637c478bd9Sstevel@tonic-gate 		if ((ifl = libld_calloc(sizeof (Ifl_desc), 1)) ==
25647c478bd9Sstevel@tonic-gate 		    (Ifl_desc *)0)
25657c478bd9Sstevel@tonic-gate 			return ((Sym_desc *)S_ERROR);
2566f5a18a30Srie 		ifl->ifl_name = reference;
25677c478bd9Sstevel@tonic-gate 		ifl->ifl_flags = FLG_IF_NEEDED | FLG_IF_FILEREF;
25687c478bd9Sstevel@tonic-gate 		if ((ifl->ifl_ehdr = libld_calloc(sizeof (Ehdr),
25697c478bd9Sstevel@tonic-gate 		    1)) == 0)
25707c478bd9Sstevel@tonic-gate 			return ((Sym_desc *)S_ERROR);
25717c478bd9Sstevel@tonic-gate 		ifl->ifl_ehdr->e_type = ET_REL;
25727c478bd9Sstevel@tonic-gate 
25737c478bd9Sstevel@tonic-gate 		if (list_appendc(&ofl->ofl_objs, ifl) == 0)
25747c478bd9Sstevel@tonic-gate 			return ((Sym_desc *)S_ERROR);
25757c478bd9Sstevel@tonic-gate 	}
25767c478bd9Sstevel@tonic-gate 
25777c478bd9Sstevel@tonic-gate 	/*
25787c478bd9Sstevel@tonic-gate 	 * Allocate a symbol structure and add it to the global symbol table.
25797c478bd9Sstevel@tonic-gate 	 */
25807c478bd9Sstevel@tonic-gate 	if ((sym = libld_calloc(sizeof (Sym), 1)) == 0)
25817c478bd9Sstevel@tonic-gate 		return ((Sym_desc *)S_ERROR);
25827c478bd9Sstevel@tonic-gate 	sym->st_info = ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE);
25837c478bd9Sstevel@tonic-gate 	sym->st_shndx = SHN_UNDEF;
25847c478bd9Sstevel@tonic-gate 
25855aefb655Srie 	DBG_CALL(Dbg_syms_process(ofl->ofl_lml, ifl));
2586b02637afSrie 	if (sdp == NULL) {
2587b02637afSrie 		DBG_CALL(Dbg_syms_global(ofl->ofl_lml, 0, name));
2588b02637afSrie 		if ((sdp = ld_sym_enter(name, sym, hash, ifl, ofl, 0, SHN_UNDEF,
2589b02637afSrie 		    0, 0, &where)) == (Sym_desc *)S_ERROR)
2590b02637afSrie 			return ((Sym_desc *)S_ERROR);
2591b02637afSrie 	} else if (ld_sym_resolve(sdp, sym, ifl, ofl, 0,
2592b02637afSrie 	    SHN_UNDEF, 0) == S_ERROR)
2593b02637afSrie 		return ((Sym_desc *)S_ERROR);
2594b02637afSrie 
25957c478bd9Sstevel@tonic-gate 	sdp->sd_flags &= ~FLG_SY_CLEAN;
25967c478bd9Sstevel@tonic-gate 	sdp->sd_flags |= FLG_SY_CMDREF;
25977c478bd9Sstevel@tonic-gate 
25987c478bd9Sstevel@tonic-gate 	return (sdp);
25997c478bd9Sstevel@tonic-gate }
2600