xref: /illumos-gate/usr/src/cmd/sgs/libld/common/syms.c (revision 0bc07c75)
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  *
275aefb655Srie  * Copyright 2006 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  */
357c478bd9Sstevel@tonic-gate #include	<stdio.h>
367c478bd9Sstevel@tonic-gate #include	<string.h>
375aefb655Srie #include	<debug.h>
387c478bd9Sstevel@tonic-gate #include	"msg.h"
397c478bd9Sstevel@tonic-gate #include	"_libld.h"
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate /*
427c478bd9Sstevel@tonic-gate  * AVL tree comparator function:
437c478bd9Sstevel@tonic-gate  *
447c478bd9Sstevel@tonic-gate  *	The primary key is the 'sa_hashval' with a secondary
457c478bd9Sstevel@tonic-gate  *	key of the symbol name itself.
467c478bd9Sstevel@tonic-gate  */
477c478bd9Sstevel@tonic-gate int
485aefb655Srie ld_sym_avl_comp(const void *elem1, const void *elem2)
497c478bd9Sstevel@tonic-gate {
507c478bd9Sstevel@tonic-gate 	int	res;
517c478bd9Sstevel@tonic-gate 	Sym_avlnode	*sav1 = (Sym_avlnode *)elem1;
527c478bd9Sstevel@tonic-gate 	Sym_avlnode	*sav2 = (Sym_avlnode *)elem2;
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate 	res = sav1->sav_hash - sav2->sav_hash;
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate 	if (res < 0)
577c478bd9Sstevel@tonic-gate 		return (-1);
587c478bd9Sstevel@tonic-gate 	if (res > 0)
597c478bd9Sstevel@tonic-gate 		return (1);
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate 	/*
627c478bd9Sstevel@tonic-gate 	 * Hash is equal - now compare name
637c478bd9Sstevel@tonic-gate 	 */
647c478bd9Sstevel@tonic-gate 	res = strcmp(sav1->sav_name, sav2->sav_name);
657c478bd9Sstevel@tonic-gate 	if (res == 0)
667c478bd9Sstevel@tonic-gate 		return (0);
677c478bd9Sstevel@tonic-gate 	if (res > 0)
687c478bd9Sstevel@tonic-gate 		return (1);
697c478bd9Sstevel@tonic-gate 	return (-1);
707c478bd9Sstevel@tonic-gate }
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate /*
747c478bd9Sstevel@tonic-gate  * Focal point for verifying symbol names.
757c478bd9Sstevel@tonic-gate  */
765aefb655Srie static const char *
775aefb655Srie string(Ofl_desc *ofl, Ifl_desc *ifl, Sym *sym, const char *strs, size_t strsize,
785aefb655Srie     int symndx, Word shndx, const char *symsecname, const char *strsecname,
795aefb655Srie     Word *flags)
807c478bd9Sstevel@tonic-gate {
817c478bd9Sstevel@tonic-gate 	const char	*regname;
827c478bd9Sstevel@tonic-gate 	Word		name = sym->st_name;
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate 	if (name) {
857c478bd9Sstevel@tonic-gate 		if ((ifl->ifl_flags & FLG_IF_HSTRTAB) == 0) {
865aefb655Srie 			eprintf(ofl->ofl_lml, ERR_FATAL,
875aefb655Srie 			    MSG_INTL(MSG_FIL_NOSTRTABLE), ifl->ifl_name,
885aefb655Srie 			    symsecname, symndx, EC_XWORD(name));
897c478bd9Sstevel@tonic-gate 			return (0);
907c478bd9Sstevel@tonic-gate 		}
917c478bd9Sstevel@tonic-gate 		if (name >= (Word)strsize) {
925aefb655Srie 			eprintf(ofl->ofl_lml, ERR_FATAL,
935aefb655Srie 			    MSG_INTL(MSG_FIL_EXCSTRTABLE), ifl->ifl_name,
945aefb655Srie 			    symsecname, symndx, EC_XWORD(name),
957c478bd9Sstevel@tonic-gate 			    strsecname, EC_XWORD(strsize));
967c478bd9Sstevel@tonic-gate 			return (0);
977c478bd9Sstevel@tonic-gate 		}
987c478bd9Sstevel@tonic-gate 	}
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 	/*
1017c478bd9Sstevel@tonic-gate 	 * Determine if we're dealing with a register and if so validate it.
1027c478bd9Sstevel@tonic-gate 	 * If it's a scratch register, a fabricated name will be returned.
1037c478bd9Sstevel@tonic-gate 	 */
1045aefb655Srie 	if ((regname = ld_is_regsym(ofl, ifl, sym, strs, symndx, shndx,
1057c478bd9Sstevel@tonic-gate 	    symsecname, flags)) == (const char *)S_ERROR) {
1067c478bd9Sstevel@tonic-gate 		return (0);
1077c478bd9Sstevel@tonic-gate 	}
1087c478bd9Sstevel@tonic-gate 	if (regname)
1097c478bd9Sstevel@tonic-gate 		return (regname);
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate 	/*
1127c478bd9Sstevel@tonic-gate 	 * If this isn't a register, but we have a global symbol with a null
1137c478bd9Sstevel@tonic-gate 	 * name, we're not going to be able to hash this, search for it, or
1147c478bd9Sstevel@tonic-gate 	 * do anything interesting.  However, we've been accepting a symbol of
1157c478bd9Sstevel@tonic-gate 	 * this kind for ages now, so give the user a warning (rather than a
1167c478bd9Sstevel@tonic-gate 	 * fatal error), just in case this instance exists somewhere in the
1177c478bd9Sstevel@tonic-gate 	 * world and hasn't, as yet, been a problem.
1187c478bd9Sstevel@tonic-gate 	 */
1197c478bd9Sstevel@tonic-gate 	if ((name == 0) && (ELF_ST_BIND(sym->st_info) != STB_LOCAL)) {
1205aefb655Srie 		eprintf(ofl->ofl_lml, ERR_WARNING, MSG_INTL(MSG_FIL_NONAMESYM),
1217c478bd9Sstevel@tonic-gate 		    ifl->ifl_name, symsecname, symndx, EC_XWORD(name));
1227c478bd9Sstevel@tonic-gate 	}
1237c478bd9Sstevel@tonic-gate 	return (strs + name);
1247c478bd9Sstevel@tonic-gate }
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate /*
1277c478bd9Sstevel@tonic-gate  * Shared objects can be built that define specific symbols that can not be
1287c478bd9Sstevel@tonic-gate  * directly bound to.  These objects have a syminfo section (and an associated
1297c478bd9Sstevel@tonic-gate  * DF_1_NODIRECT dynamic flags entry).  Scan this table looking for symbols
1307c478bd9Sstevel@tonic-gate  * that can't be bound to directly, and if this files symbol is presently
1317c478bd9Sstevel@tonic-gate  * referenced, mark it so that we don't directly bind to it.
1327c478bd9Sstevel@tonic-gate  */
1337c478bd9Sstevel@tonic-gate uintptr_t
1345aefb655Srie ld_sym_nodirect(Is_desc * isp, Ifl_desc * ifl, Ofl_desc * ofl)
1357c478bd9Sstevel@tonic-gate {
1367c478bd9Sstevel@tonic-gate 	Shdr		*sifshdr, *symshdr;
1377c478bd9Sstevel@tonic-gate 	Syminfo		*sifdata;
1387c478bd9Sstevel@tonic-gate 	Sym		*symdata;
1397c478bd9Sstevel@tonic-gate 	char		*strdata;
1407c478bd9Sstevel@tonic-gate 	ulong_t		cnt, _cnt;
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate 	/*
1437c478bd9Sstevel@tonic-gate 	 * Get the syminfo data, and determine the number of entries.
1447c478bd9Sstevel@tonic-gate 	 */
1457c478bd9Sstevel@tonic-gate 	sifshdr = isp->is_shdr;
1467c478bd9Sstevel@tonic-gate 	sifdata = (Syminfo *)isp->is_indata->d_buf;
1477c478bd9Sstevel@tonic-gate 	cnt =  sifshdr->sh_size / sifshdr->sh_entsize;
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 	/*
1507c478bd9Sstevel@tonic-gate 	 * Get the associated symbol table.
1517c478bd9Sstevel@tonic-gate 	 */
1527c478bd9Sstevel@tonic-gate 	symshdr = ifl->ifl_isdesc[sifshdr->sh_link]->is_shdr;
1537c478bd9Sstevel@tonic-gate 	symdata = ifl->ifl_isdesc[sifshdr->sh_link]->is_indata->d_buf;
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 	/*
1567c478bd9Sstevel@tonic-gate 	 * Get the string table associated with the symbol table.
1577c478bd9Sstevel@tonic-gate 	 */
1587c478bd9Sstevel@tonic-gate 	strdata = ifl->ifl_isdesc[symshdr->sh_link]->is_indata->d_buf;
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate 	/*
1617c478bd9Sstevel@tonic-gate 	 * Traverse the syminfo data for symbols that can't be directly
1627c478bd9Sstevel@tonic-gate 	 * bound to.
1637c478bd9Sstevel@tonic-gate 	 */
1647c478bd9Sstevel@tonic-gate 	for (_cnt = 1, sifdata++; _cnt < cnt; _cnt++, sifdata++) {
1657c478bd9Sstevel@tonic-gate 		Sym		*sym;
1667c478bd9Sstevel@tonic-gate 		char		*str;
1677c478bd9Sstevel@tonic-gate 		Sym_desc	*sdp;
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 		if (((sifdata->si_flags & SYMINFO_FLG_NOEXTDIRECT) == 0) ||
1707c478bd9Sstevel@tonic-gate 		    (sifdata->si_boundto < SYMINFO_BT_LOWRESERVE))
1717c478bd9Sstevel@tonic-gate 			continue;
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 		sym = (Sym *)(symdata + _cnt);
1747c478bd9Sstevel@tonic-gate 		str = (char *)(strdata + sym->st_name);
1757c478bd9Sstevel@tonic-gate 
1765aefb655Srie 		if (sdp = ld_sym_find(str, SYM_NOHASH, 0, ofl)) {
1777c478bd9Sstevel@tonic-gate 			if (ifl != sdp->sd_file)
1787c478bd9Sstevel@tonic-gate 				continue;
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 			sdp->sd_flags1 &= ~FLG_SY1_DIR;
1817c478bd9Sstevel@tonic-gate 			sdp->sd_flags1 |= FLG_SY1_NDIR;
1827c478bd9Sstevel@tonic-gate 		}
1837c478bd9Sstevel@tonic-gate 	}
1847c478bd9Sstevel@tonic-gate 	return (0);
1857c478bd9Sstevel@tonic-gate }
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate /*
1887c478bd9Sstevel@tonic-gate  * If, during symbol processing, it is necessary to update a local symbols
1897c478bd9Sstevel@tonic-gate  * contents before we have generated the symbol tables in the output image,
1907c478bd9Sstevel@tonic-gate  * create a new symbol structure and copy the original symbol contents.  While
1917c478bd9Sstevel@tonic-gate  * we are processing the input files, their local symbols are part of the
1927c478bd9Sstevel@tonic-gate  * read-only mapped image.  Commonly, these symbols are copied to the new output
1937c478bd9Sstevel@tonic-gate  * file image and then updated to reflect their new address and any change in
1947c478bd9Sstevel@tonic-gate  * attributes.  However, sometimes during relocation counting, it is necessary
1957c478bd9Sstevel@tonic-gate  * to adjust the symbols information.  This routine provides for the generation
1967c478bd9Sstevel@tonic-gate  * of a new symbol image so that this update can be performed.
1977c478bd9Sstevel@tonic-gate  * All global symbols are copied to an internal symbol table to improve locality
1987c478bd9Sstevel@tonic-gate  * of reference and hence performance, and thus this copying is not necessary.
1997c478bd9Sstevel@tonic-gate  */
2007c478bd9Sstevel@tonic-gate uintptr_t
2015aefb655Srie ld_sym_copy(Sym_desc *sdp)
2027c478bd9Sstevel@tonic-gate {
2037c478bd9Sstevel@tonic-gate 	Sym	*nsym;
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 	if (sdp->sd_flags & FLG_SY_CLEAN) {
2067c478bd9Sstevel@tonic-gate 		if ((nsym = libld_malloc(sizeof (Sym))) == 0)
2077c478bd9Sstevel@tonic-gate 			return (S_ERROR);
2087c478bd9Sstevel@tonic-gate 		*nsym = *(sdp->sd_sym);
2097c478bd9Sstevel@tonic-gate 		sdp->sd_sym = nsym;
2107c478bd9Sstevel@tonic-gate 		sdp->sd_flags &= ~FLG_SY_CLEAN;
2117c478bd9Sstevel@tonic-gate 	}
2127c478bd9Sstevel@tonic-gate 	return (1);
2137c478bd9Sstevel@tonic-gate }
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate /*
2167c478bd9Sstevel@tonic-gate  * Finds a given name in the link editors internal symbol table.  If no
2177c478bd9Sstevel@tonic-gate  * hash value is specified it is calculated.  A pointer to the located
2187c478bd9Sstevel@tonic-gate  * Sym_desc entry is returned, or NULL if the symbol is not found.
2197c478bd9Sstevel@tonic-gate  */
2207c478bd9Sstevel@tonic-gate Sym_desc *
2215aefb655Srie ld_sym_find(const char *name, Word hash, avl_index_t *where, Ofl_desc *ofl)
2227c478bd9Sstevel@tonic-gate {
2237c478bd9Sstevel@tonic-gate 	Sym_avlnode	qsav;
2247c478bd9Sstevel@tonic-gate 	Sym_avlnode	*sav;
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 	if (hash == SYM_NOHASH)
2277c478bd9Sstevel@tonic-gate 		/* LINTED */
2287c478bd9Sstevel@tonic-gate 		hash = (Word)elf_hash((const char *)name);
2297c478bd9Sstevel@tonic-gate 	qsav.sav_hash = hash;
2307c478bd9Sstevel@tonic-gate 	qsav.sav_name = name;
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate 	/*
2337c478bd9Sstevel@tonic-gate 	 * Perform search for symbol in AVL tree.  Note that the 'where' field
2347c478bd9Sstevel@tonic-gate 	 * is passed in from the caller.  If a 'where' is present, it can be
2357c478bd9Sstevel@tonic-gate 	 * used in subsequent 'sym_enter()' calls if required.
2367c478bd9Sstevel@tonic-gate 	 */
2377c478bd9Sstevel@tonic-gate 	sav = avl_find(&ofl->ofl_symavl, &qsav, where);
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate 	/*
2407c478bd9Sstevel@tonic-gate 	 * If symbol was not found in the avl tree, return null to show that.
2417c478bd9Sstevel@tonic-gate 	 */
2427c478bd9Sstevel@tonic-gate 	if (sav == 0)
2437c478bd9Sstevel@tonic-gate 		return (0);
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 	/*
2467c478bd9Sstevel@tonic-gate 	 * Return symbol found.
2477c478bd9Sstevel@tonic-gate 	 */
2487c478bd9Sstevel@tonic-gate 	return (sav->sav_symdesc);
2497c478bd9Sstevel@tonic-gate }
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate /*
2537c478bd9Sstevel@tonic-gate  * Enter a new symbol into the link editors internal symbol table.
2547c478bd9Sstevel@tonic-gate  * If the symbol is from an input file, information regarding the input file
2557c478bd9Sstevel@tonic-gate  * and input section is also recorded.  Otherwise (file == NULL) the symbol
2567c478bd9Sstevel@tonic-gate  * has been internally generated (ie. _etext, _edata, etc.).
2577c478bd9Sstevel@tonic-gate  */
2587c478bd9Sstevel@tonic-gate Sym_desc *
2595aefb655Srie ld_sym_enter(const char *name, Sym *osym, Word hash, Ifl_desc *ifl,
2605aefb655Srie     Ofl_desc *ofl, Word ndx, Word shndx, Word sdflags, Half sdflags1,
2615aefb655Srie     avl_index_t *where)
2627c478bd9Sstevel@tonic-gate {
2637c478bd9Sstevel@tonic-gate 	Sym_desc	*sdp;
2647c478bd9Sstevel@tonic-gate 	Sym_aux		*sap;
2657c478bd9Sstevel@tonic-gate 	Sym_avlnode	*savl;
2667c478bd9Sstevel@tonic-gate 	char		*_name;
2677c478bd9Sstevel@tonic-gate 	Sym		*nsym;
2687c478bd9Sstevel@tonic-gate 	Half		etype;
2697c478bd9Sstevel@tonic-gate 	avl_index_t	_where;
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate 	/*
2727c478bd9Sstevel@tonic-gate 	 * Establish the file type.
2737c478bd9Sstevel@tonic-gate 	 */
2747c478bd9Sstevel@tonic-gate 	if (ifl)
2757c478bd9Sstevel@tonic-gate 		etype = ifl->ifl_ehdr->e_type;
2767c478bd9Sstevel@tonic-gate 	else
2777c478bd9Sstevel@tonic-gate 		etype = ET_NONE;
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate 	ofl->ofl_entercnt++;
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate 	/*
2827c478bd9Sstevel@tonic-gate 	 * Allocate a Sym Descriptor, Auxiliary Descriptor, and a Sym AVLNode -
2837c478bd9Sstevel@tonic-gate 	 * contiguously.
2847c478bd9Sstevel@tonic-gate 	 */
2857c478bd9Sstevel@tonic-gate 	if ((savl = libld_calloc(sizeof (Sym_avlnode) + sizeof (Sym_desc) +
2867c478bd9Sstevel@tonic-gate 	    sizeof (Sym_aux), 1)) == 0)
2877c478bd9Sstevel@tonic-gate 		return ((Sym_desc *)S_ERROR);
2887c478bd9Sstevel@tonic-gate 	sdp = (Sym_desc *)((uintptr_t)savl + sizeof (Sym_avlnode));
2897c478bd9Sstevel@tonic-gate 	sap = (Sym_aux *)((uintptr_t)sdp + sizeof (Sym_desc));
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate 	savl->sav_symdesc = sdp;
2927c478bd9Sstevel@tonic-gate 	sdp->sd_file = ifl;
2937c478bd9Sstevel@tonic-gate 	sdp->sd_aux = sap;
2947c478bd9Sstevel@tonic-gate 	savl->sav_hash = sap->sa_hash = hash;
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate 	/*
2987c478bd9Sstevel@tonic-gate 	 * Copy the symbol table entry from the input file into the internal
2997c478bd9Sstevel@tonic-gate 	 * entry and have the symbol descriptor use it.
3007c478bd9Sstevel@tonic-gate 	 */
3017c478bd9Sstevel@tonic-gate 	sdp->sd_sym = nsym = &sap->sa_sym;
3027c478bd9Sstevel@tonic-gate 	*nsym = *osym;
3037c478bd9Sstevel@tonic-gate 	sdp->sd_shndx = shndx;
3047c478bd9Sstevel@tonic-gate 	sdp->sd_flags |= sdflags;
3057c478bd9Sstevel@tonic-gate 	sdp->sd_flags1 |= sdflags1;
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate 	if ((_name = libld_malloc(strlen(name) + 1)) == 0)
3087c478bd9Sstevel@tonic-gate 		return ((Sym_desc *)S_ERROR);
3097c478bd9Sstevel@tonic-gate 	savl->sav_name = sdp->sd_name = (const char *)strcpy(_name, name);
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate 	/*
3127c478bd9Sstevel@tonic-gate 	 * Enter Symbol in AVL tree.
3137c478bd9Sstevel@tonic-gate 	 */
3147c478bd9Sstevel@tonic-gate 	if (where == 0) {
3157c478bd9Sstevel@tonic-gate 		/* LINTED */
3167c478bd9Sstevel@tonic-gate 		Sym_avlnode	*_savl;
3177c478bd9Sstevel@tonic-gate 		/*
3185aefb655Srie 		 * If a previous ld_sym_find() hasn't initialized 'where' do it
3197c478bd9Sstevel@tonic-gate 		 * now.
3207c478bd9Sstevel@tonic-gate 		 */
3217c478bd9Sstevel@tonic-gate 		where = &_where;
3227c478bd9Sstevel@tonic-gate 		_savl = avl_find(&ofl->ofl_symavl, savl, where);
3237c478bd9Sstevel@tonic-gate 		assert(_savl == 0);
3247c478bd9Sstevel@tonic-gate 	}
3257c478bd9Sstevel@tonic-gate 	avl_insert(&ofl->ofl_symavl, savl, *where);
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate 	/*
3287c478bd9Sstevel@tonic-gate 	 * Record the section index.  This is possible because the
3297c478bd9Sstevel@tonic-gate 	 * `ifl_isdesc' table is filled before we start symbol processing.
3307c478bd9Sstevel@tonic-gate 	 */
331*0bc07c75Srie 	if ((sdflags & FLG_SY_SPECSEC) || (nsym->st_shndx == SHN_UNDEF))
3327c478bd9Sstevel@tonic-gate 		sdp->sd_isc = NULL;
3337c478bd9Sstevel@tonic-gate 	else {
3347c478bd9Sstevel@tonic-gate 		sdp->sd_isc = ifl->ifl_isdesc[shndx];
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate 		/*
3377c478bd9Sstevel@tonic-gate 		 * If this symbol is from a relocatable object, make sure that
3387c478bd9Sstevel@tonic-gate 		 * it is still associated with a section.  For example, an
3397c478bd9Sstevel@tonic-gate 		 * unknown section type (SHT_NULL) would have been rejected on
3407c478bd9Sstevel@tonic-gate 		 * input with a warning.  Here, we make the use of the symbol
3417c478bd9Sstevel@tonic-gate 		 * fatal.  A symbol descriptor is still returned, so that the
3427c478bd9Sstevel@tonic-gate 		 * caller can continue processing all symbols, and hence flush
3437c478bd9Sstevel@tonic-gate 		 * out as many error conditions as possible.
3447c478bd9Sstevel@tonic-gate 		 */
3457c478bd9Sstevel@tonic-gate 		if ((etype == ET_REL) && (sdp->sd_isc == 0)) {
3465aefb655Srie 			eprintf(ofl->ofl_lml, ERR_FATAL,
3475aefb655Srie 			    MSG_INTL(MSG_SYM_INVSEC), name, ifl->ifl_name,
3485aefb655Srie 			    EC_XWORD(shndx));
3497c478bd9Sstevel@tonic-gate 			ofl->ofl_flags |= FLG_OF_FATAL;
3507c478bd9Sstevel@tonic-gate 			return (sdp);
3517c478bd9Sstevel@tonic-gate 		}
3527c478bd9Sstevel@tonic-gate 	}
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate 	/*
3557c478bd9Sstevel@tonic-gate 	 * Mark any COMMON symbols as 'tentative'.
3567c478bd9Sstevel@tonic-gate 	 */
35754d82594Sseizo 	if (sdflags & FLG_SY_SPECSEC) {
358*0bc07c75Srie 		if (nsym->st_shndx == SHN_COMMON)
35954d82594Sseizo 			sdp->sd_flags |= FLG_SY_TENTSYM;
36054d82594Sseizo #if	(defined(__i386) || defined(__amd64)) && defined(_ELF64)
361*0bc07c75Srie 		else if (nsym->st_shndx == SHN_X86_64_LCOMMON)
36254d82594Sseizo 			sdp->sd_flags |= FLG_SY_TENTSYM;
36354d82594Sseizo #endif
36454d82594Sseizo 	}
3657c478bd9Sstevel@tonic-gate 
3667c478bd9Sstevel@tonic-gate 	/*
3677c478bd9Sstevel@tonic-gate 	 * Establish the symbols reference & visibility.
3687c478bd9Sstevel@tonic-gate 	 */
3697c478bd9Sstevel@tonic-gate 	if ((etype == ET_NONE) || (etype == ET_REL)) {
3707c478bd9Sstevel@tonic-gate 		sdp->sd_ref = REF_REL_NEED;
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate 		/*
3737c478bd9Sstevel@tonic-gate 		 * Under -Bnodirect, all exported interfaces are tagged to
3747c478bd9Sstevel@tonic-gate 		 * prevent direct binding to them.
3757c478bd9Sstevel@tonic-gate 		 */
376*0bc07c75Srie 		if ((ofl->ofl_flags1 & FLG_OF1_ALNODIR) &&
377*0bc07c75Srie 		    (nsym->st_shndx != SHN_UNDEF))
3787c478bd9Sstevel@tonic-gate 			sdp->sd_flags1 |= FLG_SY1_NDIR;
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate 	} else {
3817c478bd9Sstevel@tonic-gate 		sdp->sd_ref = REF_DYN_SEEN;
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate 		/*
3847c478bd9Sstevel@tonic-gate 		 * Record the binding file for this symbol in the sa_bindto
3857c478bd9Sstevel@tonic-gate 		 * field.  If this symbol is ever overridden by a REF_REL_NEED
3867c478bd9Sstevel@tonic-gate 		 * definition, sa_bindto is used when building a 'translator'.
3877c478bd9Sstevel@tonic-gate 		 */
388*0bc07c75Srie 		if (nsym->st_shndx != SHN_UNDEF)
3897c478bd9Sstevel@tonic-gate 			sdp->sd_aux->sa_bindto = ifl;
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate 		/*
3927c478bd9Sstevel@tonic-gate 		 * If this is a protected symbol, mark it.
3937c478bd9Sstevel@tonic-gate 		 */
3947c478bd9Sstevel@tonic-gate 		if (ELF_ST_VISIBILITY(nsym->st_other) == STV_PROTECTED)
3957c478bd9Sstevel@tonic-gate 			sdp->sd_flags |= FLG_SY_PROT;
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate 		/*
3987c478bd9Sstevel@tonic-gate 		 * Mask out any visibility info from a DYN symbol.
3997c478bd9Sstevel@tonic-gate 		 */
4007c478bd9Sstevel@tonic-gate 		nsym->st_other = nsym->st_other & ~MSK_SYM_VISIBILITY;
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate 		/*
4037c478bd9Sstevel@tonic-gate 		 * If the new symbol is from a shared library and it
4047c478bd9Sstevel@tonic-gate 		 * is associated with a SHT_NOBITS section then this
4057c478bd9Sstevel@tonic-gate 		 * symbol originated from a tentative symbol.
4067c478bd9Sstevel@tonic-gate 		 */
4077c478bd9Sstevel@tonic-gate 		if (sdp->sd_isc &&
4087c478bd9Sstevel@tonic-gate 		    (sdp->sd_isc->is_shdr->sh_type == SHT_NOBITS))
4097c478bd9Sstevel@tonic-gate 			sdp->sd_flags |= FLG_SY_TENTSYM;
4107c478bd9Sstevel@tonic-gate 	}
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate 	/*
4137c478bd9Sstevel@tonic-gate 	 * Reclassify any SHN_SUNW_IGNORE symbols to SHN_UNDEF so as to
414*0bc07c75Srie 	 * simplify future processing.
4157c478bd9Sstevel@tonic-gate 	 */
416*0bc07c75Srie 	if (nsym->st_shndx == SHN_SUNW_IGNORE) {
4177c478bd9Sstevel@tonic-gate 		sdp->sd_shndx = shndx = SHN_UNDEF;
4187c478bd9Sstevel@tonic-gate 		sdp->sd_flags |= FLG_SY_REDUCED;
4197c478bd9Sstevel@tonic-gate 		sdp->sd_flags1 |=
4207c478bd9Sstevel@tonic-gate 		    (FLG_SY1_IGNORE | FLG_SY1_LOCL | FLG_SY1_ELIM);
4217c478bd9Sstevel@tonic-gate 	}
4227c478bd9Sstevel@tonic-gate 
4237c478bd9Sstevel@tonic-gate 	/*
4247c478bd9Sstevel@tonic-gate 	 * If this is an undefined, or common symbol from a relocatable object
4257c478bd9Sstevel@tonic-gate 	 * determine whether it is a global or weak reference (see build_osym(),
4267c478bd9Sstevel@tonic-gate 	 * where REF_DYN_NEED definitions are returned back to undefines).
4277c478bd9Sstevel@tonic-gate 	 */
42854d82594Sseizo 	if ((etype == ET_REL) &&
42954d82594Sseizo 	    (ELF_ST_BIND(nsym->st_info) == STB_GLOBAL) &&
430*0bc07c75Srie 	    ((nsym->st_shndx == SHN_UNDEF) || ((sdflags & FLG_SY_SPECSEC) &&
43154d82594Sseizo #if	(defined(__i386) || defined(__amd64)) && defined(_ELF64)
432*0bc07c75Srie 	    ((nsym->st_shndx == SHN_COMMON) ||
433*0bc07c75Srie 	    (nsym->st_shndx == SHN_X86_64_LCOMMON)))))
43454d82594Sseizo #else
435*0bc07c75Srie 	    (nsym->st_shndx == SHN_COMMON))))
43654d82594Sseizo #endif
4377c478bd9Sstevel@tonic-gate 		sdp->sd_flags |= FLG_SY_GLOBREF;
4387c478bd9Sstevel@tonic-gate 
4397c478bd9Sstevel@tonic-gate 	/*
4407c478bd9Sstevel@tonic-gate 	 * Record the input filename on the referenced or defined files list
4417c478bd9Sstevel@tonic-gate 	 * for possible later diagnostics.  The `sa_rfile' pointer contains the
4427c478bd9Sstevel@tonic-gate 	 * name of the file that first referenced this symbol and is used to
4437c478bd9Sstevel@tonic-gate 	 * generate undefined symbol diagnostics (refer to sym_undef_entry()).
4447c478bd9Sstevel@tonic-gate 	 * Note that this entry can be overridden if a reference from a
4457c478bd9Sstevel@tonic-gate 	 * relocatable object is found after a reference from a shared object
4467c478bd9Sstevel@tonic-gate 	 * (refer to sym_override()).
4477c478bd9Sstevel@tonic-gate 	 * The `sa_dfiles' list is used to maintain the list of files that
4487c478bd9Sstevel@tonic-gate 	 * define the same symbol.  This list can be used for two reasons:
4497c478bd9Sstevel@tonic-gate 	 *
4507c478bd9Sstevel@tonic-gate 	 *   o	To save the first definition of a symbol that is not available
4517c478bd9Sstevel@tonic-gate 	 *	for this link-edit.
4527c478bd9Sstevel@tonic-gate 	 *
4537c478bd9Sstevel@tonic-gate 	 *   o	To save all definitions of a symbol when the -m option is in
4547c478bd9Sstevel@tonic-gate 	 *	effect.  This is optional as it is used to list multiple
4557c478bd9Sstevel@tonic-gate 	 *	(interposed) definitions of a symbol (refer to ldmap_out()),
4567c478bd9Sstevel@tonic-gate 	 *	and can be quite expensive.
4577c478bd9Sstevel@tonic-gate 	 */
458*0bc07c75Srie 	if (nsym->st_shndx == SHN_UNDEF) {
4597c478bd9Sstevel@tonic-gate 		sap->sa_rfile = ifl->ifl_name;
4607c478bd9Sstevel@tonic-gate 	} else {
4617c478bd9Sstevel@tonic-gate 		if (sdp->sd_ref == REF_DYN_SEEN) {
4627c478bd9Sstevel@tonic-gate 			/*
4637c478bd9Sstevel@tonic-gate 			 * A symbol is determined to be unavailable if it
4647c478bd9Sstevel@tonic-gate 			 * belongs to a version of a shared object that this
4657c478bd9Sstevel@tonic-gate 			 * user does not wish to use, or if it belongs to an
4667c478bd9Sstevel@tonic-gate 			 * implicit shared object.
4677c478bd9Sstevel@tonic-gate 			 */
4687c478bd9Sstevel@tonic-gate 			if (ifl->ifl_vercnt) {
4697c478bd9Sstevel@tonic-gate 				Ver_index *	vip;
4707c478bd9Sstevel@tonic-gate 				Half		vndx = ifl->ifl_versym[ndx];
4717c478bd9Sstevel@tonic-gate 
4727c478bd9Sstevel@tonic-gate 				sap->sa_dverndx = vndx;
4737c478bd9Sstevel@tonic-gate 				vip = &ifl->ifl_verndx[vndx];
4747c478bd9Sstevel@tonic-gate 				if (!(vip->vi_flags & FLG_VER_AVAIL)) {
4757c478bd9Sstevel@tonic-gate 					sdp->sd_flags |= FLG_SY_NOTAVAIL;
4767c478bd9Sstevel@tonic-gate 					sap->sa_vfile = ifl->ifl_name;
4777c478bd9Sstevel@tonic-gate 				}
4787c478bd9Sstevel@tonic-gate 			}
4797c478bd9Sstevel@tonic-gate 			if (!(ifl->ifl_flags & FLG_IF_NEEDED))
4807c478bd9Sstevel@tonic-gate 				sdp->sd_flags |= FLG_SY_NOTAVAIL;
4817c478bd9Sstevel@tonic-gate 
4827c478bd9Sstevel@tonic-gate 		} else if (etype == ET_REL) {
4837c478bd9Sstevel@tonic-gate 			/*
4847c478bd9Sstevel@tonic-gate 			 * If this symbol has been obtained from a versioned
4857c478bd9Sstevel@tonic-gate 			 * input relocatable object then the new symbol must be
4867c478bd9Sstevel@tonic-gate 			 * promoted to the versioning of the output file.
4877c478bd9Sstevel@tonic-gate 			 */
4887c478bd9Sstevel@tonic-gate 			if (ifl->ifl_versym)
4895aefb655Srie 				ld_vers_promote(sdp, ndx, ifl, ofl);
4907c478bd9Sstevel@tonic-gate 		}
4917c478bd9Sstevel@tonic-gate 
4927c478bd9Sstevel@tonic-gate 		if ((ofl->ofl_flags & FLG_OF_GENMAP) &&
4937c478bd9Sstevel@tonic-gate 		    ((sdflags & FLG_SY_SPECSEC) == 0))
4947c478bd9Sstevel@tonic-gate 			if (list_appendc(&sap->sa_dfiles, ifl->ifl_name) == 0)
4957c478bd9Sstevel@tonic-gate 				return ((Sym_desc *)S_ERROR);
4967c478bd9Sstevel@tonic-gate 	}
4977c478bd9Sstevel@tonic-gate 
4985aefb655Srie 	DBG_CALL(Dbg_syms_entered(ofl, nsym, sdp));
4997c478bd9Sstevel@tonic-gate 	return (sdp);
5007c478bd9Sstevel@tonic-gate }
5017c478bd9Sstevel@tonic-gate 
5027c478bd9Sstevel@tonic-gate /*
5037c478bd9Sstevel@tonic-gate  * Add a special symbol to the symbol table.  Takes special symbol name with
5047c478bd9Sstevel@tonic-gate  * and without underscores.  This routine is called, after all other symbol
5057c478bd9Sstevel@tonic-gate  * resolution has completed, to generate a reserved absolute symbol (the
5067c478bd9Sstevel@tonic-gate  * underscore version).  Special symbols are updated with the appropriate
507dd94ecefSrie  * values in update_osym().  If the user has already defined this symbol
5087c478bd9Sstevel@tonic-gate  * issue a warning and leave the symbol as is.  If the non-underscore symbol
5097c478bd9Sstevel@tonic-gate  * is referenced then turn it into a weak alias of the underscored symbol.
5107c478bd9Sstevel@tonic-gate  *
5117c478bd9Sstevel@tonic-gate  * If this is a global symbol, and it hasn't explicitly been defined as being
5127c478bd9Sstevel@tonic-gate  * directly bound to, indicate that it can't be directly bound to.
5137c478bd9Sstevel@tonic-gate  * Historically, most special symbols only have meaning to the object in which
5147c478bd9Sstevel@tonic-gate  * they exist, however, they've always been global.  To insure compatibility
5157c478bd9Sstevel@tonic-gate  * with any unexpected use presently in effect, insure these symbols don't get
5167c478bd9Sstevel@tonic-gate  * directly bound to.  Note, that establishing this state here isn't sufficient
5177c478bd9Sstevel@tonic-gate  * to create a syminfo table, only if a syminfo table is being created by some
5187c478bd9Sstevel@tonic-gate  * other symbol directives will the nodirect binding be recorded.  This insures
5197c478bd9Sstevel@tonic-gate  * we don't create syminfo sections for all objects we create, as this might add
5207c478bd9Sstevel@tonic-gate  * unnecessary bloat to users who haven't explicitly requested extra symbol
5217c478bd9Sstevel@tonic-gate  * information.
5227c478bd9Sstevel@tonic-gate  */
5237c478bd9Sstevel@tonic-gate static uintptr_t
5247c478bd9Sstevel@tonic-gate sym_add_spec(const char *name, const char *uname, Word sdaux_id,
5257c478bd9Sstevel@tonic-gate     Half flags1, Ofl_desc *ofl)
5267c478bd9Sstevel@tonic-gate {
5277c478bd9Sstevel@tonic-gate 	Sym_desc	*sdp;
5287c478bd9Sstevel@tonic-gate 	Sym_desc 	*usdp;
5297c478bd9Sstevel@tonic-gate 	Sym		*sym;
5307c478bd9Sstevel@tonic-gate 	Word		hash;
5317c478bd9Sstevel@tonic-gate 	avl_index_t	where;
5327c478bd9Sstevel@tonic-gate 
5337c478bd9Sstevel@tonic-gate 	/* LINTED */
5347c478bd9Sstevel@tonic-gate 	hash = (Word)elf_hash(uname);
5355aefb655Srie 	if (usdp = ld_sym_find(uname, hash, &where, ofl)) {
5367c478bd9Sstevel@tonic-gate 		/*
5377c478bd9Sstevel@tonic-gate 		 * If the underscore symbol exists and is undefined, or was
5387c478bd9Sstevel@tonic-gate 		 * defined in a shared library, convert it to a local symbol.
5397c478bd9Sstevel@tonic-gate 		 * Otherwise leave it as is and warn the user.
5407c478bd9Sstevel@tonic-gate 		 */
5417c478bd9Sstevel@tonic-gate 		if ((usdp->sd_shndx == SHN_UNDEF) ||
5427c478bd9Sstevel@tonic-gate 		    (usdp->sd_ref != REF_REL_NEED)) {
5437c478bd9Sstevel@tonic-gate 			usdp->sd_ref = REF_REL_NEED;
5447c478bd9Sstevel@tonic-gate 			usdp->sd_shndx = usdp->sd_sym->st_shndx = SHN_ABS;
5457c478bd9Sstevel@tonic-gate 			usdp->sd_flags |= FLG_SY_SPECSEC;
5467c478bd9Sstevel@tonic-gate 			usdp->sd_sym->st_info =
5477c478bd9Sstevel@tonic-gate 			    ELF_ST_INFO(STB_GLOBAL, STT_OBJECT);
5487c478bd9Sstevel@tonic-gate 			usdp->sd_isc = NULL;
5497c478bd9Sstevel@tonic-gate 			usdp->sd_sym->st_size = 0;
5507c478bd9Sstevel@tonic-gate 			usdp->sd_sym->st_value = 0;
5517c478bd9Sstevel@tonic-gate 			/* LINTED */
5527c478bd9Sstevel@tonic-gate 			usdp->sd_aux->sa_symspec = (Half)sdaux_id;
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate 			/*
5557c478bd9Sstevel@tonic-gate 			 * If a user hasn't specifically indicated the scope of
5567c478bd9Sstevel@tonic-gate 			 * this symbol be made local then leave it as global
5577c478bd9Sstevel@tonic-gate 			 * (ie. prevent automatic scoping).
5587c478bd9Sstevel@tonic-gate 			 */
5597c478bd9Sstevel@tonic-gate 			if (!(usdp->sd_flags1 & FLG_SY1_LOCL) &&
5607c478bd9Sstevel@tonic-gate 			    (flags1 & FLG_SY1_GLOB)) {
5617c478bd9Sstevel@tonic-gate 				usdp->sd_aux->sa_overndx = VER_NDX_GLOBAL;
5627c478bd9Sstevel@tonic-gate 				if ((usdp->sd_flags1 & FLG_SY1_DIR) == 0)
5637c478bd9Sstevel@tonic-gate 					usdp->sd_flags1 |= FLG_SY1_NDIR;
5647c478bd9Sstevel@tonic-gate 			}
5657c478bd9Sstevel@tonic-gate 			usdp->sd_flags1 |= flags1;
5667c478bd9Sstevel@tonic-gate 
5677c478bd9Sstevel@tonic-gate 			/*
5687c478bd9Sstevel@tonic-gate 			 * If the reference originated from a mapfile insure
5697c478bd9Sstevel@tonic-gate 			 * we mark the symbol as used.
5707c478bd9Sstevel@tonic-gate 			 */
5717c478bd9Sstevel@tonic-gate 			if (usdp->sd_flags & FLG_SY_MAPREF)
5727c478bd9Sstevel@tonic-gate 				usdp->sd_flags |= FLG_SY_MAPUSED;
5737c478bd9Sstevel@tonic-gate 
5745aefb655Srie 			DBG_CALL(Dbg_syms_updated(ofl, usdp, uname));
5757c478bd9Sstevel@tonic-gate 		} else
5765aefb655Srie 			eprintf(ofl->ofl_lml, ERR_WARNING,
5775aefb655Srie 			    MSG_INTL(MSG_SYM_RESERVE), uname,
5787c478bd9Sstevel@tonic-gate 			    usdp->sd_file->ifl_name);
5797c478bd9Sstevel@tonic-gate 	} else {
5807c478bd9Sstevel@tonic-gate 		/*
5817c478bd9Sstevel@tonic-gate 		 * If the symbol does not exist create it.
5827c478bd9Sstevel@tonic-gate 		 */
5837c478bd9Sstevel@tonic-gate 		if ((sym = libld_calloc(sizeof (Sym), 1)) == 0)
5847c478bd9Sstevel@tonic-gate 			return (S_ERROR);
5857c478bd9Sstevel@tonic-gate 		sym->st_shndx = SHN_ABS;
5867c478bd9Sstevel@tonic-gate 		sym->st_info = ELF_ST_INFO(STB_GLOBAL, STT_OBJECT);
5877c478bd9Sstevel@tonic-gate 		sym->st_size = 0;
5887c478bd9Sstevel@tonic-gate 		sym->st_value = 0;
5895aefb655Srie 		DBG_CALL(Dbg_syms_created(ofl->ofl_lml, uname));
5905aefb655Srie 		if ((usdp = ld_sym_enter(uname, sym, hash, (Ifl_desc *)NULL,
5917c478bd9Sstevel@tonic-gate 		    ofl, 0, SHN_ABS, FLG_SY_SPECSEC, 0, &where)) ==
5927c478bd9Sstevel@tonic-gate 		    (Sym_desc *)S_ERROR)
5937c478bd9Sstevel@tonic-gate 			return (S_ERROR);
5947c478bd9Sstevel@tonic-gate 		usdp->sd_ref = REF_REL_NEED;
5957c478bd9Sstevel@tonic-gate 		/* LINTED */
5967c478bd9Sstevel@tonic-gate 		usdp->sd_aux->sa_symspec = (Half)sdaux_id;
5977c478bd9Sstevel@tonic-gate 
5987c478bd9Sstevel@tonic-gate 		usdp->sd_aux->sa_overndx = VER_NDX_GLOBAL;
5997c478bd9Sstevel@tonic-gate 		if (flags1 & FLG_SY1_GLOB)
6007c478bd9Sstevel@tonic-gate 			usdp->sd_flags1 |= FLG_SY1_NDIR;
6017c478bd9Sstevel@tonic-gate 		usdp->sd_flags1 |= flags1;
6027c478bd9Sstevel@tonic-gate 	}
6037c478bd9Sstevel@tonic-gate 
6045aefb655Srie 	if (name && (sdp = ld_sym_find(name, SYM_NOHASH, 0, ofl)) &&
605*0bc07c75Srie 	    (sdp->sd_sym->st_shndx == SHN_UNDEF)) {
6067c478bd9Sstevel@tonic-gate 		uchar_t	bind;
6077c478bd9Sstevel@tonic-gate 
6087c478bd9Sstevel@tonic-gate 		/*
6097c478bd9Sstevel@tonic-gate 		 * If the non-underscore symbol exists and is undefined
6107c478bd9Sstevel@tonic-gate 		 * convert it to be a local.  If the underscore has
6117c478bd9Sstevel@tonic-gate 		 * sa_symspec set (ie. it was created above) then simulate this
6127c478bd9Sstevel@tonic-gate 		 * as a weak alias.
6137c478bd9Sstevel@tonic-gate 		 */
6147c478bd9Sstevel@tonic-gate 		sdp->sd_ref = REF_REL_NEED;
6157c478bd9Sstevel@tonic-gate 		sdp->sd_shndx = sdp->sd_sym->st_shndx = SHN_ABS;
6167c478bd9Sstevel@tonic-gate 		sdp->sd_flags |= FLG_SY_SPECSEC;
6177c478bd9Sstevel@tonic-gate 		sdp->sd_isc = NULL;
6187c478bd9Sstevel@tonic-gate 		sdp->sd_sym->st_size = 0;
6197c478bd9Sstevel@tonic-gate 		sdp->sd_sym->st_value = 0;
6207c478bd9Sstevel@tonic-gate 		/* LINTED */
6217c478bd9Sstevel@tonic-gate 		sdp->sd_aux->sa_symspec = (Half)sdaux_id;
6227c478bd9Sstevel@tonic-gate 		if (usdp->sd_aux->sa_symspec) {
6237c478bd9Sstevel@tonic-gate 			usdp->sd_aux->sa_linkndx = 0;
6247c478bd9Sstevel@tonic-gate 			sdp->sd_aux->sa_linkndx = 0;
6257c478bd9Sstevel@tonic-gate 			bind = STB_WEAK;
6267c478bd9Sstevel@tonic-gate 		} else
6277c478bd9Sstevel@tonic-gate 			bind = STB_GLOBAL;
6287c478bd9Sstevel@tonic-gate 		sdp->sd_sym->st_info = ELF_ST_INFO(bind, STT_OBJECT);
6297c478bd9Sstevel@tonic-gate 
6307c478bd9Sstevel@tonic-gate 		/*
6317c478bd9Sstevel@tonic-gate 		 * If a user hasn't specifically indicated the scope of
6327c478bd9Sstevel@tonic-gate 		 * this symbol be made local then leave it as global
6337c478bd9Sstevel@tonic-gate 		 * (ie. prevent automatic scoping).
6347c478bd9Sstevel@tonic-gate 		 */
6357c478bd9Sstevel@tonic-gate 		if (!(sdp->sd_flags1 & FLG_SY1_LOCL) &&
6367c478bd9Sstevel@tonic-gate 		    (flags1 & FLG_SY1_GLOB)) {
6377c478bd9Sstevel@tonic-gate 			sdp->sd_aux->sa_overndx = VER_NDX_GLOBAL;
6387c478bd9Sstevel@tonic-gate 			if ((sdp->sd_flags1 & FLG_SY1_DIR) == 0)
6397c478bd9Sstevel@tonic-gate 				sdp->sd_flags1 |= FLG_SY1_NDIR;
6407c478bd9Sstevel@tonic-gate 		}
6417c478bd9Sstevel@tonic-gate 		sdp->sd_flags1 |= flags1;
6427c478bd9Sstevel@tonic-gate 
6437c478bd9Sstevel@tonic-gate 		/*
6447c478bd9Sstevel@tonic-gate 		 * If the reference originated from a mapfile insure
6457c478bd9Sstevel@tonic-gate 		 * we mark the symbol as used.
6467c478bd9Sstevel@tonic-gate 		 */
6477c478bd9Sstevel@tonic-gate 		if (sdp->sd_flags & FLG_SY_MAPREF)
6487c478bd9Sstevel@tonic-gate 			sdp->sd_flags |= FLG_SY_MAPUSED;
6497c478bd9Sstevel@tonic-gate 
6505aefb655Srie 		DBG_CALL(Dbg_syms_updated(ofl, sdp, name));
6517c478bd9Sstevel@tonic-gate 	}
6527c478bd9Sstevel@tonic-gate 	return (1);
6537c478bd9Sstevel@tonic-gate }
6547c478bd9Sstevel@tonic-gate 
6557c478bd9Sstevel@tonic-gate 
6567c478bd9Sstevel@tonic-gate /*
6577c478bd9Sstevel@tonic-gate  * Print undefined symbols.
6587c478bd9Sstevel@tonic-gate  */
6597c478bd9Sstevel@tonic-gate static Boolean	undef_title = TRUE;
6607c478bd9Sstevel@tonic-gate 
6615aefb655Srie static void
6625aefb655Srie sym_undef_title(Ofl_desc *ofl)
6637c478bd9Sstevel@tonic-gate {
6645aefb655Srie 	eprintf(ofl->ofl_lml, ERR_NONE, MSG_INTL(MSG_SYM_FMT_UNDEF),
6657c478bd9Sstevel@tonic-gate 		MSG_INTL(MSG_SYM_UNDEF_ITM_11),
6667c478bd9Sstevel@tonic-gate 		MSG_INTL(MSG_SYM_UNDEF_ITM_21),
6677c478bd9Sstevel@tonic-gate 		MSG_INTL(MSG_SYM_UNDEF_ITM_12),
6687c478bd9Sstevel@tonic-gate 		MSG_INTL(MSG_SYM_UNDEF_ITM_22));
6697c478bd9Sstevel@tonic-gate 
6707c478bd9Sstevel@tonic-gate 	undef_title = FALSE;
6717c478bd9Sstevel@tonic-gate }
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate /*
6747c478bd9Sstevel@tonic-gate  * Undefined symbols can fall into one of four types:
6757c478bd9Sstevel@tonic-gate  *
6767c478bd9Sstevel@tonic-gate  *  o	the symbol is really undefined (SHN_UNDEF).
6777c478bd9Sstevel@tonic-gate  *
6787c478bd9Sstevel@tonic-gate  *  o	versioning has been enabled, however this symbol has not been assigned
6797c478bd9Sstevel@tonic-gate  *	to one of the defined versions.
6807c478bd9Sstevel@tonic-gate  *
6817c478bd9Sstevel@tonic-gate  *  o	the symbol has been defined by an implicitly supplied library, ie. one
6827c478bd9Sstevel@tonic-gate  *	which was encounted because it was NEEDED by another library, rather
6837c478bd9Sstevel@tonic-gate  * 	than from a command line supplied library which would become the only
6847c478bd9Sstevel@tonic-gate  *	dependency of the output file being produced.
6857c478bd9Sstevel@tonic-gate  *
6867c478bd9Sstevel@tonic-gate  *  o	the symbol has been defined by a version of a shared object that is
6877c478bd9Sstevel@tonic-gate  *	not permitted for this link-edit.
6887c478bd9Sstevel@tonic-gate  *
6897c478bd9Sstevel@tonic-gate  * In all cases the file who made the first reference to this symbol will have
6907c478bd9Sstevel@tonic-gate  * been recorded via the `sa_rfile' pointer.
6917c478bd9Sstevel@tonic-gate  */
6927c478bd9Sstevel@tonic-gate typedef enum {
6937c478bd9Sstevel@tonic-gate 	UNDEF,		NOVERSION,	IMPLICIT,	NOTAVAIL,
6947c478bd9Sstevel@tonic-gate 	BNDLOCAL
6957c478bd9Sstevel@tonic-gate } Type;
6967c478bd9Sstevel@tonic-gate 
6977c478bd9Sstevel@tonic-gate static const Msg format[] = {
6987c478bd9Sstevel@tonic-gate 	MSG_SYM_UND_UNDEF,		/* MSG_INTL(MSG_SYM_UND_UNDEF) */
6997c478bd9Sstevel@tonic-gate 	MSG_SYM_UND_NOVER,		/* MSG_INTL(MSG_SYM_UND_NOVER) */
7007c478bd9Sstevel@tonic-gate 	MSG_SYM_UND_IMPL,		/* MSG_INTL(MSG_SYM_UND_IMPL) */
7017c478bd9Sstevel@tonic-gate 	MSG_SYM_UND_NOTA,		/* MSG_INTL(MSG_SYM_UND_NOTA) */
7027c478bd9Sstevel@tonic-gate 	MSG_SYM_UND_BNDLOCAL		/* MSG_INTL(MSG_SYM_UND_BNDLOCAL) */
7037c478bd9Sstevel@tonic-gate };
7047c478bd9Sstevel@tonic-gate 
7055aefb655Srie static void
7065aefb655Srie sym_undef_entry(Ofl_desc *ofl, Sym_desc *sdp, Type type)
7077c478bd9Sstevel@tonic-gate {
7087c478bd9Sstevel@tonic-gate 	const char	*name1, *name2, *name3;
7097c478bd9Sstevel@tonic-gate 	Ifl_desc	*ifl = sdp->sd_file;
7107c478bd9Sstevel@tonic-gate 	Sym_aux		*sap = sdp->sd_aux;
7117c478bd9Sstevel@tonic-gate 
7127c478bd9Sstevel@tonic-gate 	if (undef_title)
7135aefb655Srie 		sym_undef_title(ofl);
7147c478bd9Sstevel@tonic-gate 
7157c478bd9Sstevel@tonic-gate 	switch (type) {
7167c478bd9Sstevel@tonic-gate 	case UNDEF:
7177c478bd9Sstevel@tonic-gate 	case BNDLOCAL:
7187c478bd9Sstevel@tonic-gate 		name1 = sap->sa_rfile;
7197c478bd9Sstevel@tonic-gate 		break;
7207c478bd9Sstevel@tonic-gate 	case NOVERSION:
7217c478bd9Sstevel@tonic-gate 		name1 = ifl->ifl_name;
7227c478bd9Sstevel@tonic-gate 		break;
7237c478bd9Sstevel@tonic-gate 	case IMPLICIT:
7247c478bd9Sstevel@tonic-gate 		name1 = sap->sa_rfile;
7257c478bd9Sstevel@tonic-gate 		name2 = ifl->ifl_name;
7267c478bd9Sstevel@tonic-gate 		break;
7277c478bd9Sstevel@tonic-gate 	case NOTAVAIL:
7287c478bd9Sstevel@tonic-gate 		name1 = sap->sa_rfile;
7297c478bd9Sstevel@tonic-gate 		name2 = sap->sa_vfile;
7307c478bd9Sstevel@tonic-gate 		name3 = ifl->ifl_verndx[sap->sa_dverndx].vi_name;
7317c478bd9Sstevel@tonic-gate 		break;
7327c478bd9Sstevel@tonic-gate 	default:
7337c478bd9Sstevel@tonic-gate 		return;
7347c478bd9Sstevel@tonic-gate 	}
7357c478bd9Sstevel@tonic-gate 
7365aefb655Srie 	eprintf(ofl->ofl_lml, ERR_NONE, MSG_INTL(format[type]),
7375aefb655Srie 	    demangle(sdp->sd_name), name1, name2, name3);
7387c478bd9Sstevel@tonic-gate }
7397c478bd9Sstevel@tonic-gate 
7407c478bd9Sstevel@tonic-gate /*
7417c478bd9Sstevel@tonic-gate  * At this point all symbol input processing has been completed, therefore
7427c478bd9Sstevel@tonic-gate  * complete the symbol table entries by generating any necessary internal
7437c478bd9Sstevel@tonic-gate  * symbols.
7447c478bd9Sstevel@tonic-gate  */
7457c478bd9Sstevel@tonic-gate uintptr_t
7465aefb655Srie ld_sym_spec(Ofl_desc *ofl)
7477c478bd9Sstevel@tonic-gate {
7487c478bd9Sstevel@tonic-gate 	Word		flags = ofl->ofl_flags;
7497c478bd9Sstevel@tonic-gate 
7507c478bd9Sstevel@tonic-gate 	if (!(flags & FLG_OF_RELOBJ)) {
7517c478bd9Sstevel@tonic-gate 
7525aefb655Srie 		DBG_CALL(Dbg_syms_spec_title(ofl->ofl_lml));
7537c478bd9Sstevel@tonic-gate 
7547c478bd9Sstevel@tonic-gate 		if (sym_add_spec(MSG_ORIG(MSG_SYM_ETEXT),
7557c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_SYM_ETEXT_U), SDAUX_ID_ETEXT,
7567c478bd9Sstevel@tonic-gate 		    FLG_SY1_GLOB, ofl) == S_ERROR)
7577c478bd9Sstevel@tonic-gate 			return (S_ERROR);
7587c478bd9Sstevel@tonic-gate 		if (sym_add_spec(MSG_ORIG(MSG_SYM_EDATA),
7597c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_SYM_EDATA_U), SDAUX_ID_EDATA,
7607c478bd9Sstevel@tonic-gate 		    FLG_SY1_GLOB, ofl) == S_ERROR)
7617c478bd9Sstevel@tonic-gate 			return (S_ERROR);
7627c478bd9Sstevel@tonic-gate 		if (sym_add_spec(MSG_ORIG(MSG_SYM_END),
7637c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_SYM_END_U), SDAUX_ID_END,
7647c478bd9Sstevel@tonic-gate 		    FLG_SY1_GLOB, ofl) == S_ERROR)
7657c478bd9Sstevel@tonic-gate 			return (S_ERROR);
7667c478bd9Sstevel@tonic-gate 		if (sym_add_spec(MSG_ORIG(MSG_SYM_L_END),
7677c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_SYM_L_END_U), SDAUX_ID_END,
7687c478bd9Sstevel@tonic-gate 		    FLG_SY1_LOCL, ofl) == S_ERROR)
7697c478bd9Sstevel@tonic-gate 			return (S_ERROR);
7707c478bd9Sstevel@tonic-gate 		if (sym_add_spec(MSG_ORIG(MSG_SYM_L_START),
7717c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_SYM_L_START_U), SDAUX_ID_START,
7727c478bd9Sstevel@tonic-gate 		    FLG_SY1_LOCL, ofl) == S_ERROR)
7737c478bd9Sstevel@tonic-gate 			return (S_ERROR);
7747c478bd9Sstevel@tonic-gate 
7757c478bd9Sstevel@tonic-gate 		/*
7767c478bd9Sstevel@tonic-gate 		 * Historically we've always produced a _DYNAMIC symbol, even
7777c478bd9Sstevel@tonic-gate 		 * for static executables (in which case its value will be 0).
7787c478bd9Sstevel@tonic-gate 		 */
7797c478bd9Sstevel@tonic-gate 		if (sym_add_spec(MSG_ORIG(MSG_SYM_DYNAMIC),
7807c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_SYM_DYNAMIC_U), SDAUX_ID_DYN,
7817c478bd9Sstevel@tonic-gate 		    FLG_SY1_GLOB, ofl) == S_ERROR)
7827c478bd9Sstevel@tonic-gate 			return (S_ERROR);
7837c478bd9Sstevel@tonic-gate 
7847c478bd9Sstevel@tonic-gate 		if ((flags & (FLG_OF_DYNAMIC | FLG_OF_RELOBJ)) ==
7857c478bd9Sstevel@tonic-gate 		    FLG_OF_DYNAMIC)
7867c478bd9Sstevel@tonic-gate 			if (sym_add_spec(MSG_ORIG(MSG_SYM_PLKTBL),
7877c478bd9Sstevel@tonic-gate 			    MSG_ORIG(MSG_SYM_PLKTBL_U), SDAUX_ID_PLT,
7887c478bd9Sstevel@tonic-gate 			    FLG_SY1_GLOB, ofl) == S_ERROR)
7897c478bd9Sstevel@tonic-gate 				return (S_ERROR);
7907c478bd9Sstevel@tonic-gate 
7915aefb655Srie 		if (ld_sym_find(MSG_ORIG(MSG_SYM_GOFTBL_U), SYM_NOHASH, 0, ofl))
7927c478bd9Sstevel@tonic-gate 			if (sym_add_spec(MSG_ORIG(MSG_SYM_GOFTBL),
7937c478bd9Sstevel@tonic-gate 			    MSG_ORIG(MSG_SYM_GOFTBL_U), SDAUX_ID_GOT,
7947c478bd9Sstevel@tonic-gate 			    FLG_SY1_GLOB, ofl) == S_ERROR)
7957c478bd9Sstevel@tonic-gate 				return (S_ERROR);
7967c478bd9Sstevel@tonic-gate 	}
7977c478bd9Sstevel@tonic-gate 	return (1);
7987c478bd9Sstevel@tonic-gate }
7997c478bd9Sstevel@tonic-gate 
8007c478bd9Sstevel@tonic-gate /*
8017c478bd9Sstevel@tonic-gate  * This routine checks to see if a symbols visibility needs to be reduced to
8027c478bd9Sstevel@tonic-gate  * either SYMBOLIC or LOCAL.  This routine can be called from either
8037c478bd9Sstevel@tonic-gate  * reloc_init() or sym_validate().
8047c478bd9Sstevel@tonic-gate  */
8057c478bd9Sstevel@tonic-gate void
8065aefb655Srie ld_sym_adjust_vis(Sym_desc *sdp, Ofl_desc *ofl)
8077c478bd9Sstevel@tonic-gate {
8087c478bd9Sstevel@tonic-gate 	Word	symvis, oflags = ofl->ofl_flags, oflags1 = ofl->ofl_flags1;
8097c478bd9Sstevel@tonic-gate 	Sym	*sym = sdp->sd_sym;
8107c478bd9Sstevel@tonic-gate 
811*0bc07c75Srie 	if ((sdp->sd_ref == REF_REL_NEED) &&
812*0bc07c75Srie 	    (sdp->sd_sym->st_shndx != SHN_UNDEF)) {
8137c478bd9Sstevel@tonic-gate 		/*
8147c478bd9Sstevel@tonic-gate 		 * If scoping is enabled, reduce any nonversioned global
8157c478bd9Sstevel@tonic-gate 		 * symbols (any symbol that has been processed for relocations
8167c478bd9Sstevel@tonic-gate 		 * will have already had this same reduction test applied).
8177c478bd9Sstevel@tonic-gate 		 * Indicate that the symbol has been reduced as it may be
8187c478bd9Sstevel@tonic-gate 		 * necessary to print these symbols later.
8197c478bd9Sstevel@tonic-gate 		 */
8207c478bd9Sstevel@tonic-gate 		if (((oflags & FLG_OF_AUTOLCL) ||
8217c478bd9Sstevel@tonic-gate 		    (oflags1 & FLG_OF1_AUTOELM)) &&
8227c478bd9Sstevel@tonic-gate 		    ((sdp->sd_flags1 & MSK_SY1_DEFINED) == 0)) {
8237c478bd9Sstevel@tonic-gate 
8247c478bd9Sstevel@tonic-gate 			sdp->sd_flags |= FLG_SY_REDUCED;
8257c478bd9Sstevel@tonic-gate 			sdp->sd_flags1 |= FLG_SY1_LOCL;
8267c478bd9Sstevel@tonic-gate 
8277c478bd9Sstevel@tonic-gate 			if (ELF_ST_VISIBILITY(sym->st_other) != STV_INTERNAL)
8287c478bd9Sstevel@tonic-gate 				sym->st_other = STV_HIDDEN |
8297c478bd9Sstevel@tonic-gate 				    (sym->st_other & ~MSK_SYM_VISIBILITY);
8307c478bd9Sstevel@tonic-gate 
8317c478bd9Sstevel@tonic-gate 			if (ofl->ofl_flags1 &
8327c478bd9Sstevel@tonic-gate 			    (FLG_OF1_REDLSYM | FLG_OF1_AUTOELM))
8337c478bd9Sstevel@tonic-gate 				sdp->sd_flags1 |= FLG_SY1_ELIM;
8347c478bd9Sstevel@tonic-gate 		}
8357c478bd9Sstevel@tonic-gate 
8367c478bd9Sstevel@tonic-gate 		/*
8377c478bd9Sstevel@tonic-gate 		 * If '-Bsymbolic' is in effect - then bind all global symbols
8387c478bd9Sstevel@tonic-gate 		 * 'symbolically' and assign the STV_PROTECTED visibility
8397c478bd9Sstevel@tonic-gate 		 * attribute.
8407c478bd9Sstevel@tonic-gate 		 */
8417c478bd9Sstevel@tonic-gate 		if ((oflags & FLG_OF_SYMBOLIC) &&
8427c478bd9Sstevel@tonic-gate 		    ((sdp->sd_flags1 & FLG_SY1_LOCL) == 0)) {
8437c478bd9Sstevel@tonic-gate 
8447c478bd9Sstevel@tonic-gate 			sdp->sd_flags1 |= FLG_SY1_PROT;
8457c478bd9Sstevel@tonic-gate 			if (ELF_ST_VISIBILITY(sym->st_other) == STV_DEFAULT)
8467c478bd9Sstevel@tonic-gate 				sym->st_other = STV_PROTECTED |
8477c478bd9Sstevel@tonic-gate 				    (sym->st_other & ~MSK_SYM_VISIBILITY);
8487c478bd9Sstevel@tonic-gate 		}
8497c478bd9Sstevel@tonic-gate 	}
8507c478bd9Sstevel@tonic-gate 
8517c478bd9Sstevel@tonic-gate 	/*
8527c478bd9Sstevel@tonic-gate 	 * Check to see if the symbol visibility needs to be adjusted due to any
8537c478bd9Sstevel@tonic-gate 	 * STV_* symbol attributes being set.
8547c478bd9Sstevel@tonic-gate 	 *
8557c478bd9Sstevel@tonic-gate 	 *    STV_PROTECTED ==	symbolic binding
8567c478bd9Sstevel@tonic-gate 	 *    STV_INTERNAL ==	reduce to local
8577c478bd9Sstevel@tonic-gate 	 *    STV_HIDDEN ==	reduce to local
8587c478bd9Sstevel@tonic-gate 	 *
8597c478bd9Sstevel@tonic-gate 	 * Note, UNDEF symbols can be assigned a visibility, thus the refencing
8607c478bd9Sstevel@tonic-gate 	 * code can be dependent on this visibility.  Here, by only ignoring
8617c478bd9Sstevel@tonic-gate 	 * REF_DYN_SEEN symbol definitions we can be assigning a visibility to
8627c478bd9Sstevel@tonic-gate 	 * REF_DYN_NEED.  If the protected, or local assignment is made to
8637c478bd9Sstevel@tonic-gate 	 * a REF_DYN_NEED symbol, it will be caught later as an illegal
8647c478bd9Sstevel@tonic-gate 	 * visibility.
8657c478bd9Sstevel@tonic-gate 	 */
8667c478bd9Sstevel@tonic-gate 	if (!(oflags & FLG_OF_RELOBJ) && (sdp->sd_ref != REF_DYN_SEEN) &&
8677c478bd9Sstevel@tonic-gate 	    (symvis = ELF_ST_VISIBILITY(sym->st_other))) {
8687c478bd9Sstevel@tonic-gate 		if (symvis == STV_PROTECTED)
8697c478bd9Sstevel@tonic-gate 			sdp->sd_flags1 |= FLG_SY1_PROT;
8707c478bd9Sstevel@tonic-gate 		else if ((symvis == STV_INTERNAL) || (symvis == STV_HIDDEN))
8717c478bd9Sstevel@tonic-gate 			sdp->sd_flags1 |= FLG_SY1_LOCL;
8727c478bd9Sstevel@tonic-gate 	}
8737c478bd9Sstevel@tonic-gate 
8747c478bd9Sstevel@tonic-gate 	/*
8757c478bd9Sstevel@tonic-gate 	 * Indicate that this symbol has had it's visibility checked so that
8767c478bd9Sstevel@tonic-gate 	 * we don't need to do this investigation again.
8777c478bd9Sstevel@tonic-gate 	 */
8787c478bd9Sstevel@tonic-gate 	sdp->sd_flags |= FLG_SY_VISIBLE;
8797c478bd9Sstevel@tonic-gate }
8807c478bd9Sstevel@tonic-gate 
8817c478bd9Sstevel@tonic-gate /*
8827c478bd9Sstevel@tonic-gate  * After all symbol table input processing has been finished, and all relocation
8837c478bd9Sstevel@tonic-gate  * counting has been carried out (ie. no more symbols will be read, generated,
8847c478bd9Sstevel@tonic-gate  * or modified), validate and count the relevant entries:
8857c478bd9Sstevel@tonic-gate  *
8867c478bd9Sstevel@tonic-gate  *	o	check and print any undefined symbols remaining.  Note that
8877c478bd9Sstevel@tonic-gate  *		if a symbol has been defined by virtue of the inclusion of
8887c478bd9Sstevel@tonic-gate  *		an implicit shared library, it is still classed as undefined.
8897c478bd9Sstevel@tonic-gate  *
8907c478bd9Sstevel@tonic-gate  * 	o	count the number of global needed symbols together with the
8917c478bd9Sstevel@tonic-gate  *		size of their associated name strings (if scoping has been
8927c478bd9Sstevel@tonic-gate  *		indicated these symbols may be reduced to locals).
8937c478bd9Sstevel@tonic-gate  *
8947c478bd9Sstevel@tonic-gate  *	o	establish the size and alignment requirements for the global
8957c478bd9Sstevel@tonic-gate  *		.bss section (the alignment of this section is based on the
8967c478bd9Sstevel@tonic-gate  *		first symbol that it will contain).
8977c478bd9Sstevel@tonic-gate  */
8987c478bd9Sstevel@tonic-gate uintptr_t
8995aefb655Srie ld_sym_validate(Ofl_desc *ofl)
9007c478bd9Sstevel@tonic-gate {
9017c478bd9Sstevel@tonic-gate 	Sym_avlnode	*sav;
9027c478bd9Sstevel@tonic-gate 	Sym_desc	*sdp;
9037c478bd9Sstevel@tonic-gate 	Sym		*sym;
9047c478bd9Sstevel@tonic-gate 	Word		oflags = ofl->ofl_flags;
9057c478bd9Sstevel@tonic-gate 	Word		undef = 0, needed = 0, verdesc = 0;
9067c478bd9Sstevel@tonic-gate 	Xword		bssalign = 0, tlsalign = 0;
9077c478bd9Sstevel@tonic-gate 	Xword		bsssize = 0, tlssize = 0;
90854d82594Sseizo #if	(defined(__i386) || defined(__amd64)) && defined(_ELF64)
90954d82594Sseizo 	Xword		lbssalign = 0, lbsssize = 0;
91054d82594Sseizo #endif
9117c478bd9Sstevel@tonic-gate 
9127c478bd9Sstevel@tonic-gate 	/*
9137c478bd9Sstevel@tonic-gate 	 * If a symbol is undefined and this link-edit calls for no undefined
9147c478bd9Sstevel@tonic-gate 	 * symbols to remain (this is the default case when generating an
9157c478bd9Sstevel@tonic-gate 	 * executable but can be enforced for any object using -z defs), the
9167c478bd9Sstevel@tonic-gate 	 * symbol is classified as undefined and a fatal error condition will
9177c478bd9Sstevel@tonic-gate 	 * be indicated.
9187c478bd9Sstevel@tonic-gate 	 *
9197c478bd9Sstevel@tonic-gate 	 * If the symbol is undefined and we're creating a shared object with
9207c478bd9Sstevel@tonic-gate 	 * the -Bsymbolic flag, then the symbol is also classified as undefined
9217c478bd9Sstevel@tonic-gate 	 * and a warning condition will be indicated.
9227c478bd9Sstevel@tonic-gate 	 */
9237c478bd9Sstevel@tonic-gate 	if ((oflags & (FLG_OF_SHAROBJ | FLG_OF_SYMBOLIC)) ==
9247c478bd9Sstevel@tonic-gate 	    (FLG_OF_SHAROBJ | FLG_OF_SYMBOLIC))
9257c478bd9Sstevel@tonic-gate 		undef = FLG_OF_WARN;
9267c478bd9Sstevel@tonic-gate 	if (oflags & FLG_OF_NOUNDEF)
9277c478bd9Sstevel@tonic-gate 		undef = FLG_OF_FATAL;
9287c478bd9Sstevel@tonic-gate 
9297c478bd9Sstevel@tonic-gate 	/*
9307c478bd9Sstevel@tonic-gate 	 * If the symbol is referenced from an implicitly included shared object
9317c478bd9Sstevel@tonic-gate 	 * (ie. it's not on the NEEDED list) then the symbol is also classified
9327c478bd9Sstevel@tonic-gate 	 * as undefined and a fatal error condition will be indicated.
9337c478bd9Sstevel@tonic-gate 	 */
9347c478bd9Sstevel@tonic-gate 	if ((oflags & FLG_OF_NOUNDEF) || !(oflags & FLG_OF_SHAROBJ))
9357c478bd9Sstevel@tonic-gate 		needed = FLG_OF_FATAL;
9367c478bd9Sstevel@tonic-gate 
9377c478bd9Sstevel@tonic-gate 	/*
9387c478bd9Sstevel@tonic-gate 	 * If the output image is being versioned all symbol definitions must be
9397c478bd9Sstevel@tonic-gate 	 * associated with a version.  Any symbol that isn't is classified as
9407c478bd9Sstevel@tonic-gate 	 * undefined and a fatal error condition will be indicated.
9417c478bd9Sstevel@tonic-gate 	 */
9427c478bd9Sstevel@tonic-gate 	if ((oflags & FLG_OF_VERDEF) && (ofl->ofl_vercnt > VER_NDX_GLOBAL))
9437c478bd9Sstevel@tonic-gate 		verdesc = FLG_OF_FATAL;
9447c478bd9Sstevel@tonic-gate 
9457c478bd9Sstevel@tonic-gate 	/*
9467c478bd9Sstevel@tonic-gate 	 * Collect and validate the globals from the internal symbol table.
9477c478bd9Sstevel@tonic-gate 	 */
9487c478bd9Sstevel@tonic-gate 	for (sav = avl_first(&ofl->ofl_symavl); sav;
9497c478bd9Sstevel@tonic-gate 	    sav = AVL_NEXT(&ofl->ofl_symavl, sav)) {
9507c478bd9Sstevel@tonic-gate 		Is_desc *	isp;
951*0bc07c75Srie 		int		undeferr = 0;
9527c478bd9Sstevel@tonic-gate 
9537c478bd9Sstevel@tonic-gate 		sdp = sav->sav_symdesc;
9547c478bd9Sstevel@tonic-gate 
9557c478bd9Sstevel@tonic-gate 		/*
9567c478bd9Sstevel@tonic-gate 		 * If undefined symbols are allowed ignore any symbols that are
9577c478bd9Sstevel@tonic-gate 		 * not needed.
9587c478bd9Sstevel@tonic-gate 		 */
9597c478bd9Sstevel@tonic-gate 		if (!(oflags & FLG_OF_NOUNDEF) &&
9607c478bd9Sstevel@tonic-gate 		    (sdp->sd_ref == REF_DYN_SEEN))
9617c478bd9Sstevel@tonic-gate 			continue;
9627c478bd9Sstevel@tonic-gate 
9637c478bd9Sstevel@tonic-gate 		/*
9647c478bd9Sstevel@tonic-gate 		 * If the symbol originates from an external or parent mapfile
9657c478bd9Sstevel@tonic-gate 		 * reference and hasn't been matched to a reference from a
9667c478bd9Sstevel@tonic-gate 		 * relocatable object, ignore it.
9677c478bd9Sstevel@tonic-gate 		 */
9687c478bd9Sstevel@tonic-gate 		if ((sdp->sd_flags & (FLG_SY_EXTERN | FLG_SY_PARENT)) &&
9697c478bd9Sstevel@tonic-gate 		    ((sdp->sd_flags & FLG_SY_MAPUSED) == 0)) {
9707c478bd9Sstevel@tonic-gate 			sdp->sd_flags |= FLG_SY_INVALID;
9717c478bd9Sstevel@tonic-gate 			continue;
9727c478bd9Sstevel@tonic-gate 		}
9737c478bd9Sstevel@tonic-gate 
9747c478bd9Sstevel@tonic-gate 		sym = sdp->sd_sym;
9757c478bd9Sstevel@tonic-gate 
9767c478bd9Sstevel@tonic-gate 		/*
9777c478bd9Sstevel@tonic-gate 		 * Sanity check TLS.
9787c478bd9Sstevel@tonic-gate 		 */
9797c478bd9Sstevel@tonic-gate 		if ((ELF_ST_TYPE(sym->st_info) == STT_TLS) &&
980*0bc07c75Srie 		    (sym->st_size != 0) && (sym->st_shndx != SHN_UNDEF) &&
981*0bc07c75Srie 		    (sym->st_shndx != SHN_COMMON)) {
9827c478bd9Sstevel@tonic-gate 			Is_desc *	isp = sdp->sd_isc;
9837c478bd9Sstevel@tonic-gate 			Ifl_desc *	ifl = sdp->sd_file;
9847c478bd9Sstevel@tonic-gate 
9857c478bd9Sstevel@tonic-gate 			if ((isp == 0) || (isp->is_shdr == 0) ||
9867c478bd9Sstevel@tonic-gate 			    ((isp->is_shdr->sh_flags & SHF_TLS) == 0)) {
9875aefb655Srie 				eprintf(ofl->ofl_lml, ERR_FATAL,
9885aefb655Srie 				    MSG_INTL(MSG_SYM_TLS),
9897c478bd9Sstevel@tonic-gate 				    demangle(sdp->sd_name), ifl->ifl_name);
9907c478bd9Sstevel@tonic-gate 				ofl->ofl_flags |= FLG_OF_FATAL;
9917c478bd9Sstevel@tonic-gate 				continue;
9927c478bd9Sstevel@tonic-gate 			}
9937c478bd9Sstevel@tonic-gate 		}
9947c478bd9Sstevel@tonic-gate 
9957c478bd9Sstevel@tonic-gate 		if ((sdp->sd_flags & FLG_SY_VISIBLE) == 0)
9965aefb655Srie 			ld_sym_adjust_vis(sdp, ofl);
9977c478bd9Sstevel@tonic-gate 
9987c478bd9Sstevel@tonic-gate 		if ((sdp->sd_flags & FLG_SY_REDUCED) &&
9997c478bd9Sstevel@tonic-gate 		    (oflags & FLG_OF_PROCRED)) {
10005aefb655Srie 			DBG_CALL(Dbg_syms_reduce(ofl, DBG_SYM_REDUCE_GLOBAL,
10015aefb655Srie 			    sdp, 0, 0));
10027c478bd9Sstevel@tonic-gate 		}
10037c478bd9Sstevel@tonic-gate 
10047c478bd9Sstevel@tonic-gate 		/*
10057c478bd9Sstevel@tonic-gate 		 * If building a shared object or executable, and this is a
10067c478bd9Sstevel@tonic-gate 		 * non-weak UNDEF symbol with reduced visibility (STV_*), then
10077c478bd9Sstevel@tonic-gate 		 * give a fatal error.
10087c478bd9Sstevel@tonic-gate 		 */
10097c478bd9Sstevel@tonic-gate 		if (!(oflags & FLG_OF_RELOBJ) &&
1010*0bc07c75Srie 		    ELF_ST_VISIBILITY(sym->st_other) &&
1011*0bc07c75Srie 		    (sym->st_shndx == SHN_UNDEF) &&
10127c478bd9Sstevel@tonic-gate 		    (ELF_ST_BIND(sym->st_info) != STB_WEAK)) {
10135aefb655Srie 			sym_undef_entry(ofl, sdp, BNDLOCAL);
10147c478bd9Sstevel@tonic-gate 			ofl->ofl_flags |= FLG_OF_FATAL;
10157c478bd9Sstevel@tonic-gate 			continue;
10167c478bd9Sstevel@tonic-gate 		}
10177c478bd9Sstevel@tonic-gate 
10187c478bd9Sstevel@tonic-gate 		/*
10197c478bd9Sstevel@tonic-gate 		 * If this symbol is defined in a non-allocatable section,
10207c478bd9Sstevel@tonic-gate 		 * reduce it to local symbol.
10217c478bd9Sstevel@tonic-gate 		 */
10227c478bd9Sstevel@tonic-gate 		if (((isp = sdp->sd_isc) != 0) && isp->is_shdr &&
10237c478bd9Sstevel@tonic-gate 		    ((isp->is_shdr->sh_flags & SHF_ALLOC) == 0)) {
10247c478bd9Sstevel@tonic-gate 			sdp->sd_flags |= FLG_SY_REDUCED;
10257c478bd9Sstevel@tonic-gate 			sdp->sd_flags1 |= FLG_SY1_LOCL;
10267c478bd9Sstevel@tonic-gate 		}
10277c478bd9Sstevel@tonic-gate 
10287c478bd9Sstevel@tonic-gate 		/*
10297c478bd9Sstevel@tonic-gate 		 * If this symbol originated as a SHN_SUNW_IGNORE, it will have
10307c478bd9Sstevel@tonic-gate 		 * been processed as an SHN_UNDEF.  Return the symbol to its
10317c478bd9Sstevel@tonic-gate 		 * original index for validation, and propagation to the output
10327c478bd9Sstevel@tonic-gate 		 * file.
10337c478bd9Sstevel@tonic-gate 		 */
10347c478bd9Sstevel@tonic-gate 		if (sdp->sd_flags1 & FLG_SY1_IGNORE)
1035*0bc07c75Srie 			sdp->sd_shndx = SHN_SUNW_IGNORE;
10367c478bd9Sstevel@tonic-gate 
10377c478bd9Sstevel@tonic-gate 		if (undef) {
10387c478bd9Sstevel@tonic-gate 			/*
10397c478bd9Sstevel@tonic-gate 			 * If an non-weak reference remains undefined, or if a
10407c478bd9Sstevel@tonic-gate 			 * mapfile reference is not bound to the relocatable
10417c478bd9Sstevel@tonic-gate 			 * objects that make up the object being built, we have
10427c478bd9Sstevel@tonic-gate 			 * a fatal error.
10437c478bd9Sstevel@tonic-gate 			 *
10447c478bd9Sstevel@tonic-gate 			 * The exceptions are symbols which are defined to be
10457c478bd9Sstevel@tonic-gate 			 * found in the parent (FLG_SY_PARENT), which is really
10467c478bd9Sstevel@tonic-gate 			 * only meaningful for direct binding, or are defined
10477c478bd9Sstevel@tonic-gate 			 * external (FLG_SY_EXTERN) so as to suppress -zdefs
10487c478bd9Sstevel@tonic-gate 			 * errors.
10497c478bd9Sstevel@tonic-gate 			 *
10507c478bd9Sstevel@tonic-gate 			 * Register symbols are always allowed to be UNDEF.
10517c478bd9Sstevel@tonic-gate 			 *
10527c478bd9Sstevel@tonic-gate 			 * Note that we don't include references created via -u
10537c478bd9Sstevel@tonic-gate 			 * in the same shared object binding test.  This is for
10547c478bd9Sstevel@tonic-gate 			 * backward compatibility, in that a number of archive
10557c478bd9Sstevel@tonic-gate 			 * makefile rules used -u to cause archive extraction.
10567c478bd9Sstevel@tonic-gate 			 * These same rules have been cut and pasted to apply
10577c478bd9Sstevel@tonic-gate 			 * to shared objects, and thus although the -u reference
10587c478bd9Sstevel@tonic-gate 			 * is redundant, flagging it as fatal could cause some
10597c478bd9Sstevel@tonic-gate 			 * build to fail.  Also we have documented the use of
10607c478bd9Sstevel@tonic-gate 			 * -u as a mechanism to cause binding to weak version
10617c478bd9Sstevel@tonic-gate 			 * definitions, thus giving users an error condition
10627c478bd9Sstevel@tonic-gate 			 * would be incorrect.
10637c478bd9Sstevel@tonic-gate 			 */
10647c478bd9Sstevel@tonic-gate 			if (!(sdp->sd_flags & FLG_SY_REGSYM) &&
1065*0bc07c75Srie 			    ((sym->st_shndx == SHN_UNDEF) &&
10667c478bd9Sstevel@tonic-gate 			    ((ELF_ST_BIND(sym->st_info) != STB_WEAK) &&
10677c478bd9Sstevel@tonic-gate 			    ((sdp->sd_flags &
10687c478bd9Sstevel@tonic-gate 			    (FLG_SY_PARENT | FLG_SY_EXTERN)) == 0)) ||
10697c478bd9Sstevel@tonic-gate 			    (((sdp->sd_flags &
10707c478bd9Sstevel@tonic-gate 			    (FLG_SY_MAPREF | FLG_SY_MAPUSED)) ==
10717c478bd9Sstevel@tonic-gate 			    FLG_SY_MAPREF) &&
10727c478bd9Sstevel@tonic-gate 			    ((sdp->sd_flags1 & (FLG_SY1_LOCL |
10737c478bd9Sstevel@tonic-gate 			    FLG_SY1_PROT)) == 0)))) {
10745aefb655Srie 				sym_undef_entry(ofl, sdp, UNDEF);
10757c478bd9Sstevel@tonic-gate 				ofl->ofl_flags |= undef;
10767c478bd9Sstevel@tonic-gate 				undeferr = 1;
10777c478bd9Sstevel@tonic-gate 			}
10787c478bd9Sstevel@tonic-gate 
10797c478bd9Sstevel@tonic-gate 		} else {
10807c478bd9Sstevel@tonic-gate 			/*
10817c478bd9Sstevel@tonic-gate 			 * For building things like shared objects (or anything
10827c478bd9Sstevel@tonic-gate 			 * -znodefs), undefined symbols are allowed.
10837c478bd9Sstevel@tonic-gate 			 *
10847c478bd9Sstevel@tonic-gate 			 * If a mapfile reference remains undefined the user
10857c478bd9Sstevel@tonic-gate 			 * would probably like a warning at least (they've
10867c478bd9Sstevel@tonic-gate 			 * usually mis-spelt the reference).  Refer to the above
10877c478bd9Sstevel@tonic-gate 			 * comments for discussion on -u references, which
10887c478bd9Sstevel@tonic-gate 			 * are not tested for in the same manner.
10897c478bd9Sstevel@tonic-gate 			 */
10907c478bd9Sstevel@tonic-gate 			if ((sdp->sd_flags &
10917c478bd9Sstevel@tonic-gate 			    (FLG_SY_MAPREF | FLG_SY_MAPUSED)) ==
10927c478bd9Sstevel@tonic-gate 			    FLG_SY_MAPREF) {
10935aefb655Srie 				sym_undef_entry(ofl, sdp, UNDEF);
10947c478bd9Sstevel@tonic-gate 				ofl->ofl_flags |= FLG_OF_WARN;
10957c478bd9Sstevel@tonic-gate 				undeferr = 1;
10967c478bd9Sstevel@tonic-gate 			}
10977c478bd9Sstevel@tonic-gate 		}
10987c478bd9Sstevel@tonic-gate 
10997c478bd9Sstevel@tonic-gate 		/*
11007c478bd9Sstevel@tonic-gate 		 * If this symbol comes from a dependency mark the dependency
11017c478bd9Sstevel@tonic-gate 		 * as required (-z ignore can result in unused dependencies
11027c478bd9Sstevel@tonic-gate 		 * being dropped).  If we need to record dependency versioning
11037c478bd9Sstevel@tonic-gate 		 * information indicate what version of the needed shared object
11047c478bd9Sstevel@tonic-gate 		 * this symbol is part of.  Flag the symbol as undefined if it
11057c478bd9Sstevel@tonic-gate 		 * has not been made available to us.
11067c478bd9Sstevel@tonic-gate 		 */
11077c478bd9Sstevel@tonic-gate 		if ((sdp->sd_ref == REF_DYN_NEED) &&
11087c478bd9Sstevel@tonic-gate 		    (!(sdp->sd_flags & FLG_SY_REFRSD))) {
11097c478bd9Sstevel@tonic-gate 			sdp->sd_file->ifl_flags |= FLG_IF_DEPREQD;
11107c478bd9Sstevel@tonic-gate 
11117c478bd9Sstevel@tonic-gate 			/*
11127c478bd9Sstevel@tonic-gate 			 * Capture that we've bound to a symbol that doesn't
11137c478bd9Sstevel@tonic-gate 			 * allow being directly bound to.
11147c478bd9Sstevel@tonic-gate 			 */
11157c478bd9Sstevel@tonic-gate 			if (sdp->sd_flags1 & FLG_SY1_NDIR)
11167c478bd9Sstevel@tonic-gate 				ofl->ofl_flags1 |= FLG_OF1_NDIRECT;
11177c478bd9Sstevel@tonic-gate 
11187c478bd9Sstevel@tonic-gate 			if (sdp->sd_file->ifl_vercnt) {
11197c478bd9Sstevel@tonic-gate 				int		vndx;
11207c478bd9Sstevel@tonic-gate 				Ver_index *	vip;
11217c478bd9Sstevel@tonic-gate 
11227c478bd9Sstevel@tonic-gate 				vndx = sdp->sd_aux->sa_dverndx;
11237c478bd9Sstevel@tonic-gate 				vip = &sdp->sd_file->ifl_verndx[vndx];
11247c478bd9Sstevel@tonic-gate 				if (vip->vi_flags & FLG_VER_AVAIL) {
11257c478bd9Sstevel@tonic-gate 					vip->vi_flags |= FLG_VER_REFER;
11267c478bd9Sstevel@tonic-gate 				} else {
11275aefb655Srie 					sym_undef_entry(ofl, sdp, NOTAVAIL);
11287c478bd9Sstevel@tonic-gate 					ofl->ofl_flags |= FLG_OF_FATAL;
11297c478bd9Sstevel@tonic-gate 					continue;
11307c478bd9Sstevel@tonic-gate 				}
11317c478bd9Sstevel@tonic-gate 			}
11327c478bd9Sstevel@tonic-gate 		}
11337c478bd9Sstevel@tonic-gate 
11347c478bd9Sstevel@tonic-gate 		/*
11357c478bd9Sstevel@tonic-gate 		 * Test that we do not bind to symbol supplied from an implicit
11367c478bd9Sstevel@tonic-gate 		 * shared object.  If a binding is from a weak reference it can
11377c478bd9Sstevel@tonic-gate 		 * be ignored.
11387c478bd9Sstevel@tonic-gate 		 */
11397c478bd9Sstevel@tonic-gate 		if (needed && !undeferr && (sdp->sd_flags & FLG_SY_GLOBREF) &&
11407c478bd9Sstevel@tonic-gate 		    (sdp->sd_ref == REF_DYN_NEED) &&
11417c478bd9Sstevel@tonic-gate 		    (sdp->sd_flags & FLG_SY_NOTAVAIL)) {
11425aefb655Srie 			sym_undef_entry(ofl, sdp, IMPLICIT);
11437c478bd9Sstevel@tonic-gate 			ofl->ofl_flags |= needed;
11447c478bd9Sstevel@tonic-gate 			continue;
11457c478bd9Sstevel@tonic-gate 		}
11467c478bd9Sstevel@tonic-gate 
11477c478bd9Sstevel@tonic-gate 		/*
11487c478bd9Sstevel@tonic-gate 		 * Test that a symbol isn't going to be reduced to local scope
11497c478bd9Sstevel@tonic-gate 		 * which actually wants to bind to a shared object - if so it's
11507c478bd9Sstevel@tonic-gate 		 * a fatal error.
11517c478bd9Sstevel@tonic-gate 		 */
11527c478bd9Sstevel@tonic-gate 		if ((sdp->sd_ref == REF_DYN_NEED) &&
11537c478bd9Sstevel@tonic-gate 		    (sdp->sd_flags1 & (FLG_SY1_LOCL | FLG_SY1_PROT))) {
11545aefb655Srie 			sym_undef_entry(ofl, sdp, BNDLOCAL);
11557c478bd9Sstevel@tonic-gate 			ofl->ofl_flags |= FLG_OF_FATAL;
11567c478bd9Sstevel@tonic-gate 			continue;
11577c478bd9Sstevel@tonic-gate 		}
11587c478bd9Sstevel@tonic-gate 
11597c478bd9Sstevel@tonic-gate 		/*
11607c478bd9Sstevel@tonic-gate 		 * If the output image is to be versioned then all symbol
11617c478bd9Sstevel@tonic-gate 		 * definitions must be associated with a version.
11627c478bd9Sstevel@tonic-gate 		 */
11637c478bd9Sstevel@tonic-gate 		if (verdesc && (sdp->sd_ref == REF_REL_NEED) &&
1164*0bc07c75Srie 		    (sym->st_shndx != SHN_UNDEF) &&
11657c478bd9Sstevel@tonic-gate 		    (!(sdp->sd_flags1 & FLG_SY1_LOCL)) &&
11667c478bd9Sstevel@tonic-gate 		    (sdp->sd_aux->sa_overndx == 0)) {
11675aefb655Srie 			sym_undef_entry(ofl, sdp, NOVERSION);
11687c478bd9Sstevel@tonic-gate 			ofl->ofl_flags |= verdesc;
11697c478bd9Sstevel@tonic-gate 			continue;
11707c478bd9Sstevel@tonic-gate 		}
11717c478bd9Sstevel@tonic-gate 
11727c478bd9Sstevel@tonic-gate 		/*
11737c478bd9Sstevel@tonic-gate 		 * If we don't need the symbol there's no need to process it
11747c478bd9Sstevel@tonic-gate 		 * any further.
11757c478bd9Sstevel@tonic-gate 		 */
11767c478bd9Sstevel@tonic-gate 		if (sdp->sd_ref == REF_DYN_SEEN)
11777c478bd9Sstevel@tonic-gate 			continue;
11787c478bd9Sstevel@tonic-gate 
11797c478bd9Sstevel@tonic-gate 		/*
11807c478bd9Sstevel@tonic-gate 		 * Calculate the size and alignment requirements for the global
11817c478bd9Sstevel@tonic-gate 		 * .bss and .tls sections.  If we're building a relocatable
11827c478bd9Sstevel@tonic-gate 		 * object only account for scoped COMMON symbols (these will
11837c478bd9Sstevel@tonic-gate 		 * be converted to .bss references).
11847c478bd9Sstevel@tonic-gate 		 *
11857c478bd9Sstevel@tonic-gate 		 * For partially initialized symbol,
11867c478bd9Sstevel@tonic-gate 		 *  if it is expanded, it goes to sunwdata1.
11877c478bd9Sstevel@tonic-gate 		 *  if it is local, it goes to .bss.
11887c478bd9Sstevel@tonic-gate 		 *  if the output is shared object, it goes to .sunwbss.
11897c478bd9Sstevel@tonic-gate 		 *
11907c478bd9Sstevel@tonic-gate 		 * Also refer to make_mvsections() in sunwmove.c
11917c478bd9Sstevel@tonic-gate 		 */
1192*0bc07c75Srie 		if ((sym->st_shndx == SHN_COMMON) &&
1193*0bc07c75Srie 		    (((oflags & FLG_OF_RELOBJ) == 0) ||
11947c478bd9Sstevel@tonic-gate 		    ((sdp->sd_flags1 & FLG_SY1_LOCL) &&
11957c478bd9Sstevel@tonic-gate 		    (oflags & FLG_OF_PROCRED)))) {
11967c478bd9Sstevel@tonic-gate 			int countbss = 0;
11977c478bd9Sstevel@tonic-gate 
11987c478bd9Sstevel@tonic-gate 			if (sdp->sd_psyminfo == 0) {
11997c478bd9Sstevel@tonic-gate 				countbss = 1;
12007c478bd9Sstevel@tonic-gate 			} else if ((sdp->sd_flags & FLG_SY_PAREXPN) != 0) {
12017c478bd9Sstevel@tonic-gate 				countbss = 0;
12027c478bd9Sstevel@tonic-gate 			} else if (ELF_ST_BIND(sym->st_info) == STB_LOCAL) {
12037c478bd9Sstevel@tonic-gate 				countbss = 1;
12047c478bd9Sstevel@tonic-gate 			} else if ((ofl->ofl_flags & FLG_OF_SHAROBJ) != 0) {
12057c478bd9Sstevel@tonic-gate 				countbss = 0;
12067c478bd9Sstevel@tonic-gate 			} else
12077c478bd9Sstevel@tonic-gate 				countbss = 1;
12087c478bd9Sstevel@tonic-gate 
12097c478bd9Sstevel@tonic-gate 			if (countbss) {
12107c478bd9Sstevel@tonic-gate 				Xword * size, * align;
12117c478bd9Sstevel@tonic-gate 
12127c478bd9Sstevel@tonic-gate 				if (ELF_ST_TYPE(sym->st_info) != STT_TLS) {
12137c478bd9Sstevel@tonic-gate 					size = &bsssize;
12147c478bd9Sstevel@tonic-gate 					align = &bssalign;
12157c478bd9Sstevel@tonic-gate 				} else {
12167c478bd9Sstevel@tonic-gate 					size = &tlssize;
12177c478bd9Sstevel@tonic-gate 					align = &tlsalign;
12187c478bd9Sstevel@tonic-gate 				}
12197c478bd9Sstevel@tonic-gate 				*size = (Xword)S_ROUND(*size, sym->st_value) +
1220dd94ecefSrie 				    sym->st_size;
12217c478bd9Sstevel@tonic-gate 				if (sym->st_value > *align)
12227c478bd9Sstevel@tonic-gate 					*align = sym->st_value;
12237c478bd9Sstevel@tonic-gate 			}
12247c478bd9Sstevel@tonic-gate 		}
12257c478bd9Sstevel@tonic-gate 
122654d82594Sseizo #if	(defined(__i386) || defined(__amd64)) && defined(_ELF64)
122754d82594Sseizo 		/*
122854d82594Sseizo 		 * Calculate the size and alignment requirement for the global
122954d82594Sseizo 		 * .lbss. TLS or partially initialized symbols do not need to be
123054d82594Sseizo 		 * considered yet.
123154d82594Sseizo 		 */
1232*0bc07c75Srie 		if (sym->st_shndx == SHN_X86_64_LCOMMON) {
123354d82594Sseizo 			lbsssize = (Xword)S_ROUND(lbsssize, sym->st_value) +
123454d82594Sseizo 				sym->st_size;
123554d82594Sseizo 			if (sym->st_value > lbssalign)
123654d82594Sseizo 				lbssalign = sym->st_value;
123754d82594Sseizo 		}
123854d82594Sseizo #endif
123954d82594Sseizo 
12407c478bd9Sstevel@tonic-gate 		/*
12417c478bd9Sstevel@tonic-gate 		 * If a symbol was referenced via the command line
12427c478bd9Sstevel@tonic-gate 		 * (ld -u <>, ...), then this counts as a reference against the
12437c478bd9Sstevel@tonic-gate 		 * symbol. Mark any section that symbol is defined in.
12447c478bd9Sstevel@tonic-gate 		 */
12457c478bd9Sstevel@tonic-gate 		if (((isp = sdp->sd_isc) != 0) &&
12467c478bd9Sstevel@tonic-gate 		    (sdp->sd_flags & FLG_SY_CMDREF)) {
12477c478bd9Sstevel@tonic-gate 			isp->is_flags |= FLG_IS_SECTREF;
12487c478bd9Sstevel@tonic-gate 			isp->is_file->ifl_flags |= FLG_IF_FILEREF;
12497c478bd9Sstevel@tonic-gate 		}
12507c478bd9Sstevel@tonic-gate 
12517c478bd9Sstevel@tonic-gate 		/*
12527c478bd9Sstevel@tonic-gate 		 * Update the symbol count and the associated name string size.
12537c478bd9Sstevel@tonic-gate 		 * If scoping is in effect for this symbol assign it will be
12547c478bd9Sstevel@tonic-gate 		 * assigned to the .symtab/.strtab sections.
12557c478bd9Sstevel@tonic-gate 		 */
12567c478bd9Sstevel@tonic-gate 		if ((sdp->sd_flags1 & FLG_SY1_LOCL) &&
12577c478bd9Sstevel@tonic-gate 		    (oflags & FLG_OF_PROCRED)) {
12587c478bd9Sstevel@tonic-gate 			/*
12597c478bd9Sstevel@tonic-gate 			 * If symbol gets eliminated count it.
12607c478bd9Sstevel@tonic-gate 			 *
12617c478bd9Sstevel@tonic-gate 			 * If symbol gets reduced to local,
12627c478bd9Sstevel@tonic-gate 			 * count it's size for the .symtab.
12637c478bd9Sstevel@tonic-gate 			 */
12647c478bd9Sstevel@tonic-gate 			if (sdp->sd_flags1 & FLG_SY1_ELIM) {
12657c478bd9Sstevel@tonic-gate 				ofl->ofl_elimcnt++;
12667c478bd9Sstevel@tonic-gate 			} else {
12677c478bd9Sstevel@tonic-gate 				ofl->ofl_scopecnt++;
12687c478bd9Sstevel@tonic-gate 				if ((((sdp->sd_flags & FLG_SY_REGSYM) == 0) ||
12697c478bd9Sstevel@tonic-gate 				    sym->st_name) && (st_insert(ofl->ofl_strtab,
12707c478bd9Sstevel@tonic-gate 				    sdp->sd_name) == -1))
12717c478bd9Sstevel@tonic-gate 					return (S_ERROR);
12727c478bd9Sstevel@tonic-gate 			}
12737c478bd9Sstevel@tonic-gate 		} else {
12747c478bd9Sstevel@tonic-gate 			ofl->ofl_globcnt++;
12757c478bd9Sstevel@tonic-gate 
12767c478bd9Sstevel@tonic-gate 			/*
12777c478bd9Sstevel@tonic-gate 			 * If global direct bindings are in effect, or this
12787c478bd9Sstevel@tonic-gate 			 * symbol has bound to a dependency which was specified
12797c478bd9Sstevel@tonic-gate 			 * as requiring direct bindings, and it hasn't
12807c478bd9Sstevel@tonic-gate 			 * explicitly been defined as a non-direct binding
12817c478bd9Sstevel@tonic-gate 			 * symbol, mark it.
12827c478bd9Sstevel@tonic-gate 			 */
12837c478bd9Sstevel@tonic-gate 			if (((ofl->ofl_dtflags_1 & DF_1_DIRECT) || (isp &&
12847c478bd9Sstevel@tonic-gate 			    (isp->is_file->ifl_flags & FLG_IF_DIRECT))) &&
12857c478bd9Sstevel@tonic-gate 			    ((sdp->sd_flags1 & FLG_SY1_NDIR) == 0))
12867c478bd9Sstevel@tonic-gate 				sdp->sd_flags1 |= FLG_SY1_DIR;
12877c478bd9Sstevel@tonic-gate 
12887c478bd9Sstevel@tonic-gate 			/*
12897c478bd9Sstevel@tonic-gate 			 * Insert the symbol name.
12907c478bd9Sstevel@tonic-gate 			 */
12917c478bd9Sstevel@tonic-gate 			if (((sdp->sd_flags & FLG_SY_REGSYM) == 0) ||
12927c478bd9Sstevel@tonic-gate 			    sym->st_name) {
12937c478bd9Sstevel@tonic-gate 				if (st_insert(ofl->ofl_strtab,
12947c478bd9Sstevel@tonic-gate 				    sdp->sd_name) == -1)
12957c478bd9Sstevel@tonic-gate 					return (S_ERROR);
12967c478bd9Sstevel@tonic-gate 
12977c478bd9Sstevel@tonic-gate 				if (!(ofl->ofl_flags & FLG_OF_RELOBJ) &&
12987c478bd9Sstevel@tonic-gate 				    (st_insert(ofl->ofl_dynstrtab,
12997c478bd9Sstevel@tonic-gate 				    sdp->sd_name) == -1))
13007c478bd9Sstevel@tonic-gate 					return (S_ERROR);
13017c478bd9Sstevel@tonic-gate 			}
13027c478bd9Sstevel@tonic-gate 
13037c478bd9Sstevel@tonic-gate 			/*
13047c478bd9Sstevel@tonic-gate 			 * If this section offers a global symbol - record that
13057c478bd9Sstevel@tonic-gate 			 * fact.
13067c478bd9Sstevel@tonic-gate 			 */
13077c478bd9Sstevel@tonic-gate 			if (isp) {
13087c478bd9Sstevel@tonic-gate 				isp->is_flags |= FLG_IS_SECTREF;
13097c478bd9Sstevel@tonic-gate 				isp->is_file->ifl_flags |= FLG_IF_FILEREF;
13107c478bd9Sstevel@tonic-gate 			}
13117c478bd9Sstevel@tonic-gate 		}
13127c478bd9Sstevel@tonic-gate 	}
13137c478bd9Sstevel@tonic-gate 
13147c478bd9Sstevel@tonic-gate 	/*
13157c478bd9Sstevel@tonic-gate 	 * If we've encountered a fatal error during symbol validation then
13167c478bd9Sstevel@tonic-gate 	 * return now.
13177c478bd9Sstevel@tonic-gate 	 */
13187c478bd9Sstevel@tonic-gate 	if (ofl->ofl_flags & FLG_OF_FATAL)
13197c478bd9Sstevel@tonic-gate 		return (1);
13207c478bd9Sstevel@tonic-gate 
13217c478bd9Sstevel@tonic-gate 	/*
13227c478bd9Sstevel@tonic-gate 	 * Now that symbol resolution is completed, scan any register symbols.
13237c478bd9Sstevel@tonic-gate 	 * From now on, we're only interested in those that contribute to the
13247c478bd9Sstevel@tonic-gate 	 * output file.
13257c478bd9Sstevel@tonic-gate 	 */
13267c478bd9Sstevel@tonic-gate 	if (ofl->ofl_regsyms) {
13277c478bd9Sstevel@tonic-gate 		int	ndx;
13287c478bd9Sstevel@tonic-gate 
13297c478bd9Sstevel@tonic-gate 		for (ndx = 0; ndx < ofl->ofl_regsymsno; ndx++) {
13307c478bd9Sstevel@tonic-gate 			if ((sdp = ofl->ofl_regsyms[ndx]) == 0)
13317c478bd9Sstevel@tonic-gate 				continue;
13327c478bd9Sstevel@tonic-gate 			if (sdp->sd_ref != REF_REL_NEED) {
13337c478bd9Sstevel@tonic-gate 				ofl->ofl_regsyms[ndx] = 0;
13347c478bd9Sstevel@tonic-gate 				continue;
13357c478bd9Sstevel@tonic-gate 			}
13367c478bd9Sstevel@tonic-gate 
13377c478bd9Sstevel@tonic-gate 			ofl->ofl_regsymcnt++;
13387c478bd9Sstevel@tonic-gate 			if (sdp->sd_sym->st_name == 0)
13397c478bd9Sstevel@tonic-gate 				sdp->sd_name = MSG_ORIG(MSG_STR_EMPTY);
13407c478bd9Sstevel@tonic-gate 
13417c478bd9Sstevel@tonic-gate 			if ((sdp->sd_flags1 & FLG_SY1_LOCL) ||
13427c478bd9Sstevel@tonic-gate 			    (ELF_ST_BIND(sdp->sd_sym->st_info) == STB_LOCAL))
13437c478bd9Sstevel@tonic-gate 				ofl->ofl_lregsymcnt++;
13447c478bd9Sstevel@tonic-gate 		}
13457c478bd9Sstevel@tonic-gate 	}
13467c478bd9Sstevel@tonic-gate 
13477c478bd9Sstevel@tonic-gate 	/*
13487c478bd9Sstevel@tonic-gate 	 * Generate the .bss section now that we know its size and alignment.
13497c478bd9Sstevel@tonic-gate 	 */
13507c478bd9Sstevel@tonic-gate 	if (bsssize || !(oflags & FLG_OF_RELOBJ)) {
13515aefb655Srie 		if (ld_make_bss(ofl, bsssize, bssalign, MAKE_BSS) == S_ERROR)
13527c478bd9Sstevel@tonic-gate 			return (S_ERROR);
13537c478bd9Sstevel@tonic-gate 	}
13547c478bd9Sstevel@tonic-gate 	if (tlssize) {
13555aefb655Srie 		if (ld_make_bss(ofl, tlssize, tlsalign, MAKE_TLS) == S_ERROR)
135654d82594Sseizo 			return (S_ERROR);
135754d82594Sseizo 	}
135854d82594Sseizo #if	(defined(__i386) || defined(__amd64)) && defined(_ELF64)
135954d82594Sseizo 	if (lbsssize && !(oflags & FLG_OF_RELOBJ)) {
13605aefb655Srie 		if (ld_make_bss(ofl, lbsssize, lbssalign, MAKE_LBSS) == S_ERROR)
13617c478bd9Sstevel@tonic-gate 			return (S_ERROR);
13627c478bd9Sstevel@tonic-gate 	}
136354d82594Sseizo #endif
13647c478bd9Sstevel@tonic-gate 
13657c478bd9Sstevel@tonic-gate 	/*
13667c478bd9Sstevel@tonic-gate 	 * Determine what entry point symbol we need, and if found save its
13677c478bd9Sstevel@tonic-gate 	 * symbol descriptor so that we can update the ELF header entry with the
13687c478bd9Sstevel@tonic-gate 	 * symbols value later (see update_oehdr).  Make sure the symbol is
13697c478bd9Sstevel@tonic-gate 	 * tagged to insure its update in case -s is in effect.  Use any -e
13707c478bd9Sstevel@tonic-gate 	 * option first, or the default entry points `_start' and `main'.
13717c478bd9Sstevel@tonic-gate 	 */
13727c478bd9Sstevel@tonic-gate 	if (ofl->ofl_entry) {
13735aefb655Srie 		if (((sdp = ld_sym_find(ofl->ofl_entry, SYM_NOHASH, 0, ofl)) ==
13747c478bd9Sstevel@tonic-gate 		    NULL) || (sdp->sd_ref != REF_REL_NEED)) {
13755aefb655Srie 			eprintf(ofl->ofl_lml, ERR_FATAL,
13765aefb655Srie 			    MSG_INTL(MSG_SYM_ENTRY),
13777c478bd9Sstevel@tonic-gate 			    demangle((char *)ofl->ofl_entry));
13787c478bd9Sstevel@tonic-gate 			return (S_ERROR);
13797c478bd9Sstevel@tonic-gate 		}
13807c478bd9Sstevel@tonic-gate 		ofl->ofl_entry = (void *)sdp;
13817c478bd9Sstevel@tonic-gate 		sdp->sd_flags |= FLG_SY_UPREQD;
13827c478bd9Sstevel@tonic-gate 		if (sdp->sd_isc) {
13837c478bd9Sstevel@tonic-gate 			sdp->sd_isc->is_flags |= FLG_IS_SECTREF;
13847c478bd9Sstevel@tonic-gate 			sdp->sd_isc->is_file->ifl_flags |= FLG_IF_FILEREF;
13857c478bd9Sstevel@tonic-gate 		}
13865aefb655Srie 	} else if (((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_START),
13877c478bd9Sstevel@tonic-gate 	    SYM_NOHASH, 0, ofl)) != NULL) && (sdp->sd_ref == REF_REL_NEED)) {
13887c478bd9Sstevel@tonic-gate 		ofl->ofl_entry = (void *)sdp;
13897c478bd9Sstevel@tonic-gate 		sdp->sd_flags |= FLG_SY_UPREQD;
13907c478bd9Sstevel@tonic-gate 		if (sdp->sd_isc) {
13917c478bd9Sstevel@tonic-gate 			sdp->sd_isc->is_flags |= FLG_IS_SECTREF;
13927c478bd9Sstevel@tonic-gate 			sdp->sd_isc->is_file->ifl_flags |= FLG_IF_FILEREF;
13937c478bd9Sstevel@tonic-gate 		}
13945aefb655Srie 	} else if (((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_MAIN),
13957c478bd9Sstevel@tonic-gate 	    SYM_NOHASH, 0, ofl)) != NULL) && (sdp->sd_ref == REF_REL_NEED)) {
13967c478bd9Sstevel@tonic-gate 		ofl->ofl_entry = (void *)sdp;
13977c478bd9Sstevel@tonic-gate 		sdp->sd_flags |= FLG_SY_UPREQD;
13987c478bd9Sstevel@tonic-gate 		if (sdp->sd_isc) {
13997c478bd9Sstevel@tonic-gate 			sdp->sd_isc->is_flags |= FLG_IS_SECTREF;
14007c478bd9Sstevel@tonic-gate 			sdp->sd_isc->is_file->ifl_flags |= FLG_IF_FILEREF;
14017c478bd9Sstevel@tonic-gate 		}
14027c478bd9Sstevel@tonic-gate 	}
14037c478bd9Sstevel@tonic-gate 
14047c478bd9Sstevel@tonic-gate 	/*
14057c478bd9Sstevel@tonic-gate 	 * If ld -zdtrace=<sym> was given, then validate that the
14067c478bd9Sstevel@tonic-gate 	 * symbol is defined within the current object being built.
14077c478bd9Sstevel@tonic-gate 	 */
14087c478bd9Sstevel@tonic-gate 	if ((sdp = ofl->ofl_dtracesym) != 0) {
14097c478bd9Sstevel@tonic-gate 		if ((sdp->sd_ref != REF_REL_NEED) ||
1410*0bc07c75Srie 		    (sdp->sd_sym->st_shndx == SHN_UNDEF)) {
14115aefb655Srie 			eprintf(ofl->ofl_lml, ERR_FATAL,
14125aefb655Srie 			    MSG_INTL(MSG_SYM_DTRACE),
14135aefb655Srie 			    demangle((char *)sdp->sd_name));
14147c478bd9Sstevel@tonic-gate 			return (S_ERROR);
14157c478bd9Sstevel@tonic-gate 		}
14167c478bd9Sstevel@tonic-gate 		sdp->sd_flags |= FLG_SY_UPREQD;
14177c478bd9Sstevel@tonic-gate 		if (sdp->sd_isc) {
14187c478bd9Sstevel@tonic-gate 			sdp->sd_isc->is_flags |= FLG_IS_SECTREF;
14197c478bd9Sstevel@tonic-gate 			sdp->sd_isc->is_file->ifl_flags |= FLG_IF_FILEREF;
14207c478bd9Sstevel@tonic-gate 		}
14217c478bd9Sstevel@tonic-gate 	}
14227c478bd9Sstevel@tonic-gate 
14237c478bd9Sstevel@tonic-gate 	/*
14247c478bd9Sstevel@tonic-gate 	 * If we're required to record any needed dependencies versioning
14257c478bd9Sstevel@tonic-gate 	 * information calculate it now that all symbols have been validated.
14267c478bd9Sstevel@tonic-gate 	 */
14277c478bd9Sstevel@tonic-gate 	if ((oflags & (FLG_OF_VERNEED | FLG_OF_NOVERSEC)) == FLG_OF_VERNEED)
14285aefb655Srie 		return (ld_vers_check_need(ofl));
14297c478bd9Sstevel@tonic-gate 	else
14307c478bd9Sstevel@tonic-gate 		return (1);
14317c478bd9Sstevel@tonic-gate }
14327c478bd9Sstevel@tonic-gate 
14337c478bd9Sstevel@tonic-gate /*
14347c478bd9Sstevel@tonic-gate  * qsort(3c) comparison function.  As an optimization for associating weak
14357c478bd9Sstevel@tonic-gate  * symbols to their strong counterparts sort global symbols according to their
14367c478bd9Sstevel@tonic-gate  * address and binding.
14377c478bd9Sstevel@tonic-gate  */
14387c478bd9Sstevel@tonic-gate static int
14397c478bd9Sstevel@tonic-gate compare(const void * sdpp1, const void * sdpp2)
14407c478bd9Sstevel@tonic-gate {
14417c478bd9Sstevel@tonic-gate 	Sym_desc *	sdp1 = *((Sym_desc **)sdpp1);
14427c478bd9Sstevel@tonic-gate 	Sym_desc *	sdp2 = *((Sym_desc **)sdpp2);
14437c478bd9Sstevel@tonic-gate 	Sym *		sym1, * sym2;
14447c478bd9Sstevel@tonic-gate 	uchar_t		bind1, bind2;
14457c478bd9Sstevel@tonic-gate 
14467c478bd9Sstevel@tonic-gate 	/*
14477c478bd9Sstevel@tonic-gate 	 * Symbol descriptors may be zero, move these to the front of the
14487c478bd9Sstevel@tonic-gate 	 * sorted array.
14497c478bd9Sstevel@tonic-gate 	 */
14507c478bd9Sstevel@tonic-gate 	if (sdp1 == 0)
14517c478bd9Sstevel@tonic-gate 		return (-1);
14527c478bd9Sstevel@tonic-gate 	if (sdp2 == 0)
14537c478bd9Sstevel@tonic-gate 		return (1);
14547c478bd9Sstevel@tonic-gate 
14557c478bd9Sstevel@tonic-gate 	sym1 = sdp1->sd_sym;
14567c478bd9Sstevel@tonic-gate 	sym2 = sdp2->sd_sym;
14577c478bd9Sstevel@tonic-gate 
14587c478bd9Sstevel@tonic-gate 	/*
14597c478bd9Sstevel@tonic-gate 	 * Compare the symbols value (address).
14607c478bd9Sstevel@tonic-gate 	 */
14617c478bd9Sstevel@tonic-gate 	if (sym1->st_value > sym2->st_value)
14627c478bd9Sstevel@tonic-gate 		return (1);
14637c478bd9Sstevel@tonic-gate 	if (sym1->st_value < sym2->st_value)
14647c478bd9Sstevel@tonic-gate 		return (-1);
14657c478bd9Sstevel@tonic-gate 
14667c478bd9Sstevel@tonic-gate 	bind1 = ELF_ST_BIND(sym1->st_info);
14677c478bd9Sstevel@tonic-gate 	bind2 = ELF_ST_BIND(sym2->st_info);
14687c478bd9Sstevel@tonic-gate 
14697c478bd9Sstevel@tonic-gate 	/*
14707c478bd9Sstevel@tonic-gate 	 * If two symbols have the same address place the weak symbol before
14717c478bd9Sstevel@tonic-gate 	 * any strong counterpart.
14727c478bd9Sstevel@tonic-gate 	 */
14737c478bd9Sstevel@tonic-gate 	if (bind1 > bind2)
14747c478bd9Sstevel@tonic-gate 		return (-1);
14757c478bd9Sstevel@tonic-gate 	if (bind1 < bind2)
14767c478bd9Sstevel@tonic-gate 		return (1);
14777c478bd9Sstevel@tonic-gate 
14787c478bd9Sstevel@tonic-gate 	return (0);
14797c478bd9Sstevel@tonic-gate }
14807c478bd9Sstevel@tonic-gate 
14817c478bd9Sstevel@tonic-gate 
14827c478bd9Sstevel@tonic-gate /*
14837c478bd9Sstevel@tonic-gate  * Process the symbol table for the specified input file.  At this point all
14847c478bd9Sstevel@tonic-gate  * input sections from this input file have been assigned an input section
14857c478bd9Sstevel@tonic-gate  * descriptor which is saved in the `ifl_isdesc' array.
14867c478bd9Sstevel@tonic-gate  *
14877c478bd9Sstevel@tonic-gate  *	o	local symbols are saved (as is) if the input file is a
14887c478bd9Sstevel@tonic-gate  *		relocatable object
14897c478bd9Sstevel@tonic-gate  *
14907c478bd9Sstevel@tonic-gate  *	o	global symbols are added to the linkers internal symbol
14917c478bd9Sstevel@tonic-gate  *		table if they are not already present, otherwise a symbol
14927c478bd9Sstevel@tonic-gate  *		resolution function is called upon to resolve the conflict.
14937c478bd9Sstevel@tonic-gate  */
14947c478bd9Sstevel@tonic-gate uintptr_t
14955aefb655Srie ld_sym_process(Is_desc *isc, Ifl_desc *ifl, Ofl_desc *ofl)
14967c478bd9Sstevel@tonic-gate {
14977c478bd9Sstevel@tonic-gate 	Sym		*sym = (Sym *)isc->is_indata->d_buf;
14987c478bd9Sstevel@tonic-gate 	Word		*symshndx = 0;
14997c478bd9Sstevel@tonic-gate 	Shdr		*shdr = isc->is_shdr;
15007c478bd9Sstevel@tonic-gate 	Sym_desc	*sdp;
15017c478bd9Sstevel@tonic-gate 	size_t		strsize;
15027c478bd9Sstevel@tonic-gate 	char		*strs;
15037c478bd9Sstevel@tonic-gate 	uchar_t		type, bind;
15047c478bd9Sstevel@tonic-gate 	Word		ndx, hash, local, total;
15057c478bd9Sstevel@tonic-gate 	Half		etype = ifl->ifl_ehdr->e_type;
15067c478bd9Sstevel@tonic-gate 	const char	*symsecname, *strsecname;
15077c478bd9Sstevel@tonic-gate 	avl_index_t	where;
15087c478bd9Sstevel@tonic-gate 
15097c478bd9Sstevel@tonic-gate 	/*
15107c478bd9Sstevel@tonic-gate 	 * Its possible that a file may contain more that one symbol table,
15117c478bd9Sstevel@tonic-gate 	 * ie. .dynsym and .symtab in a shared library.  Only process the first
15127c478bd9Sstevel@tonic-gate 	 * table (here, we assume .dynsym comes before .symtab).
15137c478bd9Sstevel@tonic-gate 	 */
15147c478bd9Sstevel@tonic-gate 	if (ifl->ifl_symscnt)
15157c478bd9Sstevel@tonic-gate 		return (1);
15167c478bd9Sstevel@tonic-gate 
15177c478bd9Sstevel@tonic-gate 	if (isc->is_symshndx)
15187c478bd9Sstevel@tonic-gate 		symshndx = isc->is_symshndx->is_indata->d_buf;
15197c478bd9Sstevel@tonic-gate 
15205aefb655Srie 	DBG_CALL(Dbg_syms_process(ofl->ofl_lml, ifl));
15217c478bd9Sstevel@tonic-gate 
15227c478bd9Sstevel@tonic-gate 	if (isc->is_name)
15237c478bd9Sstevel@tonic-gate 		symsecname = isc->is_name;
15247c478bd9Sstevel@tonic-gate 	else
15257c478bd9Sstevel@tonic-gate 		symsecname = MSG_ORIG(MSG_STR_EMPTY);
15267c478bd9Sstevel@tonic-gate 
15277c478bd9Sstevel@tonic-gate 	/*
15287c478bd9Sstevel@tonic-gate 	 * From the symbol tables section header information determine which
15297c478bd9Sstevel@tonic-gate 	 * strtab table is needed to locate the actual symbol names.
15307c478bd9Sstevel@tonic-gate 	 */
15317c478bd9Sstevel@tonic-gate 	if (ifl->ifl_flags & FLG_IF_HSTRTAB) {
15327c478bd9Sstevel@tonic-gate 		ndx = shdr->sh_link;
15337c478bd9Sstevel@tonic-gate 		if ((ndx == 0) || (ndx >= ifl->ifl_shnum)) {
15345aefb655Srie 			eprintf(ofl->ofl_lml, ERR_FATAL,
15355aefb655Srie 			    MSG_INTL(MSG_FIL_INVSHLINK),
15367c478bd9Sstevel@tonic-gate 			    ifl->ifl_name, symsecname, EC_XWORD(ndx));
15377c478bd9Sstevel@tonic-gate 			return (S_ERROR);
15387c478bd9Sstevel@tonic-gate 		}
15397c478bd9Sstevel@tonic-gate 		strsize = ifl->ifl_isdesc[ndx]->is_shdr->sh_size;
15407c478bd9Sstevel@tonic-gate 		strs = ifl->ifl_isdesc[ndx]->is_indata->d_buf;
15417c478bd9Sstevel@tonic-gate 		if (ifl->ifl_isdesc[ndx]->is_name)
15427c478bd9Sstevel@tonic-gate 			strsecname = ifl->ifl_isdesc[ndx]->is_name;
15437c478bd9Sstevel@tonic-gate 		else
15447c478bd9Sstevel@tonic-gate 			strsecname = MSG_ORIG(MSG_STR_EMPTY);
15457c478bd9Sstevel@tonic-gate 	} else {
15467c478bd9Sstevel@tonic-gate 		/*
15477c478bd9Sstevel@tonic-gate 		 * There is no string table section in this input file
15487c478bd9Sstevel@tonic-gate 		 * although there are symbols in this symbol table section.
15497c478bd9Sstevel@tonic-gate 		 * This means that these symbols do not have names.
15507c478bd9Sstevel@tonic-gate 		 * Currently, only scratch register symbols are allowed
15517c478bd9Sstevel@tonic-gate 		 * not to have names.
15527c478bd9Sstevel@tonic-gate 		 */
15537c478bd9Sstevel@tonic-gate 		strsize = 0;
15547c478bd9Sstevel@tonic-gate 		strs = (char *)MSG_ORIG(MSG_STR_EMPTY);
15557c478bd9Sstevel@tonic-gate 		strsecname = MSG_ORIG(MSG_STR_EMPTY);
15567c478bd9Sstevel@tonic-gate 	}
15577c478bd9Sstevel@tonic-gate 
15587c478bd9Sstevel@tonic-gate 	/*
15597c478bd9Sstevel@tonic-gate 	 * Determine the number of local symbols together with the total
15607c478bd9Sstevel@tonic-gate 	 * number we have to process.
15617c478bd9Sstevel@tonic-gate 	 */
15627c478bd9Sstevel@tonic-gate 	total = (Word)(shdr->sh_size / shdr->sh_entsize);
15637c478bd9Sstevel@tonic-gate 	local = shdr->sh_info;
15647c478bd9Sstevel@tonic-gate 
15657c478bd9Sstevel@tonic-gate 	/*
15667c478bd9Sstevel@tonic-gate 	 * Allocate a symbol table index array and a local symbol array
15677c478bd9Sstevel@tonic-gate 	 * (global symbols are processed and added to the ofl->ofl_symbkt[]
15687c478bd9Sstevel@tonic-gate 	 * array).  If we are dealing with a relocatable object, allocate the
15697c478bd9Sstevel@tonic-gate 	 * local symbol descriptors.  If this isn't a relocatable object we
15707c478bd9Sstevel@tonic-gate 	 * still have to process any shared object locals to determine if any
15717c478bd9Sstevel@tonic-gate 	 * register symbols exist.  Although these aren't added to the output
15727c478bd9Sstevel@tonic-gate 	 * image, they are used as part of symbol resolution.
15737c478bd9Sstevel@tonic-gate 	 */
15747c478bd9Sstevel@tonic-gate 	if ((ifl->ifl_oldndx = libld_malloc((size_t)(total *
15757c478bd9Sstevel@tonic-gate 	    sizeof (Sym_desc *)))) == 0)
15767c478bd9Sstevel@tonic-gate 		return (S_ERROR);
15777c478bd9Sstevel@tonic-gate 	if ((etype == ET_REL) && local) {
15787c478bd9Sstevel@tonic-gate 		if ((ifl->ifl_locs =
15797c478bd9Sstevel@tonic-gate 		    libld_calloc(sizeof (Sym_desc), local)) == 0)
15807c478bd9Sstevel@tonic-gate 			return (S_ERROR);
15817c478bd9Sstevel@tonic-gate 		/* LINTED */
15827c478bd9Sstevel@tonic-gate 		ifl->ifl_locscnt = (Word)local;
15837c478bd9Sstevel@tonic-gate 	}
15847c478bd9Sstevel@tonic-gate 	ifl->ifl_symscnt = total;
15857c478bd9Sstevel@tonic-gate 
15867c478bd9Sstevel@tonic-gate 	/*
15877c478bd9Sstevel@tonic-gate 	 * If there are local symbols to save add them to the symbol table
15887c478bd9Sstevel@tonic-gate 	 * index array.
15897c478bd9Sstevel@tonic-gate 	 */
15907c478bd9Sstevel@tonic-gate 	if (local) {
15917c478bd9Sstevel@tonic-gate 		for (sym++, ndx = 1; ndx < local; sym++, ndx++) {
15927c478bd9Sstevel@tonic-gate 			Word		shndx, sdflags = FLG_SY_CLEAN;
15937c478bd9Sstevel@tonic-gate 			const char	*name;
15947c478bd9Sstevel@tonic-gate 			Sym_desc	*rsdp;
15957c478bd9Sstevel@tonic-gate 
15967c478bd9Sstevel@tonic-gate 			/*
15977c478bd9Sstevel@tonic-gate 			 * Determine the associated section index.
15987c478bd9Sstevel@tonic-gate 			 */
15997c478bd9Sstevel@tonic-gate 			if (symshndx && (sym->st_shndx == SHN_XINDEX))
16007c478bd9Sstevel@tonic-gate 				shndx = symshndx[ndx];
16017c478bd9Sstevel@tonic-gate 			else if ((shndx = sym->st_shndx) >= SHN_LORESERVE)
16027c478bd9Sstevel@tonic-gate 				sdflags |= FLG_SY_SPECSEC;
16037c478bd9Sstevel@tonic-gate 
16047c478bd9Sstevel@tonic-gate 			/*
16057c478bd9Sstevel@tonic-gate 			 * Check if st_name has a valid value or not.
16067c478bd9Sstevel@tonic-gate 			 */
16075aefb655Srie 			if ((name = string(ofl, ifl, sym, strs, strsize, ndx,
16087c478bd9Sstevel@tonic-gate 			    shndx, symsecname, strsecname, &sdflags)) == 0) {
16097c478bd9Sstevel@tonic-gate 				ofl->ofl_flags |= FLG_OF_FATAL;
16107c478bd9Sstevel@tonic-gate 				continue;
16117c478bd9Sstevel@tonic-gate 			}
16127c478bd9Sstevel@tonic-gate 
16137c478bd9Sstevel@tonic-gate 			/*
16147c478bd9Sstevel@tonic-gate 			 * If this local symbol table originates from a shared
16157c478bd9Sstevel@tonic-gate 			 * object, then we're only interested in recording
16167c478bd9Sstevel@tonic-gate 			 * register symbols.  As local symbol descriptors aren't
16177c478bd9Sstevel@tonic-gate 			 * allocated for shared objects, one will be allocated
16187c478bd9Sstevel@tonic-gate 			 * to associated with the register symbol.  This symbol
16197c478bd9Sstevel@tonic-gate 			 * won't become part of the output image, but we must
16207c478bd9Sstevel@tonic-gate 			 * process it to test for register conflicts.
16217c478bd9Sstevel@tonic-gate 			 */
16227c478bd9Sstevel@tonic-gate 			rsdp = sdp = 0;
16237c478bd9Sstevel@tonic-gate 			if (sdflags & FLG_SY_REGSYM) {
16245aefb655Srie 				if ((rsdp = ld_reg_find(sym, ofl)) != 0) {
16257c478bd9Sstevel@tonic-gate 					/*
16267c478bd9Sstevel@tonic-gate 					 * The fact that another register def-
16277c478bd9Sstevel@tonic-gate 					 * inition has been found is fatal.
16287c478bd9Sstevel@tonic-gate 					 * Call the verification routine to get
16297c478bd9Sstevel@tonic-gate 					 * the error message and move on.
16307c478bd9Sstevel@tonic-gate 					 */
16315aefb655Srie 					(void) ld_reg_check(rsdp, sym, name,
16327c478bd9Sstevel@tonic-gate 					    ifl, ofl);
16337c478bd9Sstevel@tonic-gate 					continue;
16347c478bd9Sstevel@tonic-gate 				}
16357c478bd9Sstevel@tonic-gate 
16367c478bd9Sstevel@tonic-gate 				if (etype == ET_DYN) {
16377c478bd9Sstevel@tonic-gate 					if ((sdp = libld_calloc(
16387c478bd9Sstevel@tonic-gate 					    sizeof (Sym_desc), 1)) == 0)
16397c478bd9Sstevel@tonic-gate 						return (S_ERROR);
16407c478bd9Sstevel@tonic-gate 					sdp->sd_ref = REF_DYN_SEEN;
16417c478bd9Sstevel@tonic-gate 				}
16427c478bd9Sstevel@tonic-gate 			} else if (etype == ET_DYN)
16437c478bd9Sstevel@tonic-gate 				continue;
16447c478bd9Sstevel@tonic-gate 
16457c478bd9Sstevel@tonic-gate 			/*
16467c478bd9Sstevel@tonic-gate 			 * Fill in the remaining symbol descriptor information.
16477c478bd9Sstevel@tonic-gate 			 */
16487c478bd9Sstevel@tonic-gate 			if (sdp == 0) {
16497c478bd9Sstevel@tonic-gate 				sdp = &(ifl->ifl_locs[ndx]);
16507c478bd9Sstevel@tonic-gate 				sdp->sd_ref = REF_REL_NEED;
16517c478bd9Sstevel@tonic-gate 			}
16527c478bd9Sstevel@tonic-gate 			if (rsdp == 0) {
16537c478bd9Sstevel@tonic-gate 				sdp->sd_name = name;
16547c478bd9Sstevel@tonic-gate 				sdp->sd_sym = sym;
16557c478bd9Sstevel@tonic-gate 				sdp->sd_shndx = shndx;
16567c478bd9Sstevel@tonic-gate 				sdp->sd_flags = sdflags;
16577c478bd9Sstevel@tonic-gate 				sdp->sd_file = ifl;
16587c478bd9Sstevel@tonic-gate 				ifl->ifl_oldndx[ndx] = sdp;
16597c478bd9Sstevel@tonic-gate 			}
16607c478bd9Sstevel@tonic-gate 
16615aefb655Srie 			DBG_CALL(Dbg_syms_entry(ofl->ofl_lml, ndx, sdp));
16627c478bd9Sstevel@tonic-gate 
16637c478bd9Sstevel@tonic-gate 			/*
16647c478bd9Sstevel@tonic-gate 			 * Reclassify any SHN_SUNW_IGNORE symbols to SHN_UNDEF
16657c478bd9Sstevel@tonic-gate 			 * so as to simplify future processing.
16667c478bd9Sstevel@tonic-gate 			 */
1667*0bc07c75Srie 			if (sym->st_shndx == SHN_SUNW_IGNORE) {
16687c478bd9Sstevel@tonic-gate 				sdp->sd_shndx = shndx = SHN_UNDEF;
16697c478bd9Sstevel@tonic-gate 				sdp->sd_flags1 |=
16707c478bd9Sstevel@tonic-gate 				    (FLG_SY1_IGNORE | FLG_SY1_ELIM);
16717c478bd9Sstevel@tonic-gate 			}
16727c478bd9Sstevel@tonic-gate 
16737c478bd9Sstevel@tonic-gate 			/*
16747c478bd9Sstevel@tonic-gate 			 * Process any register symbols.
16757c478bd9Sstevel@tonic-gate 			 */
16767c478bd9Sstevel@tonic-gate 			if (sdp->sd_flags & FLG_SY_REGSYM) {
16777c478bd9Sstevel@tonic-gate 				/*
16787c478bd9Sstevel@tonic-gate 				 * Add a diagnostic to indicate we've caught a
16797c478bd9Sstevel@tonic-gate 				 * register symbol, as this can be useful if a
16807c478bd9Sstevel@tonic-gate 				 * register conflict is later discovered.
16817c478bd9Sstevel@tonic-gate 				 */
16825aefb655Srie 				DBG_CALL(Dbg_syms_entered(ofl, sym, sdp));
16837c478bd9Sstevel@tonic-gate 
16847c478bd9Sstevel@tonic-gate 				/*
16857c478bd9Sstevel@tonic-gate 				 * If this register symbol hasn't already been
16867c478bd9Sstevel@tonic-gate 				 * recorded, enter it now.
16877c478bd9Sstevel@tonic-gate 				 */
16885aefb655Srie 				if ((rsdp == 0) &&
16895aefb655Srie 				    (ld_reg_enter(sdp, ofl) == 0))
16907c478bd9Sstevel@tonic-gate 					return (S_ERROR);
16917c478bd9Sstevel@tonic-gate 			}
16927c478bd9Sstevel@tonic-gate 
16937c478bd9Sstevel@tonic-gate 			/*
16947c478bd9Sstevel@tonic-gate 			 * Assign an input section.
16957c478bd9Sstevel@tonic-gate 			 */
1696*0bc07c75Srie 			if ((sym->st_shndx != SHN_UNDEF) &&
16977c478bd9Sstevel@tonic-gate 			    ((sdp->sd_flags & FLG_SY_SPECSEC) == 0))
16987c478bd9Sstevel@tonic-gate 				sdp->sd_isc = ifl->ifl_isdesc[shndx];
16997c478bd9Sstevel@tonic-gate 
17007c478bd9Sstevel@tonic-gate 			/*
17017c478bd9Sstevel@tonic-gate 			 * If this symbol falls within the range of a section
17027c478bd9Sstevel@tonic-gate 			 * being discarded, then discard the symbol itself.
17037c478bd9Sstevel@tonic-gate 			 * There is no reason to keep this local symbol.
17047c478bd9Sstevel@tonic-gate 			 */
17057c478bd9Sstevel@tonic-gate 			if (sdp->sd_isc &&
17067c478bd9Sstevel@tonic-gate 			    (sdp->sd_isc->is_flags & FLG_IS_DISCARD)) {
17077c478bd9Sstevel@tonic-gate 				sdp->sd_flags |= FLG_SY_ISDISC;
17085aefb655Srie 				DBG_CALL(Dbg_syms_discarded(ofl->ofl_lml,
17095aefb655Srie 				    sdp, sdp->sd_isc));
17107c478bd9Sstevel@tonic-gate 				continue;
17117c478bd9Sstevel@tonic-gate 			}
17127c478bd9Sstevel@tonic-gate 
17137c478bd9Sstevel@tonic-gate 			/*
17147c478bd9Sstevel@tonic-gate 			 * Skip any section symbols as new versions of these
17157c478bd9Sstevel@tonic-gate 			 * will be created.
17167c478bd9Sstevel@tonic-gate 			 */
17177c478bd9Sstevel@tonic-gate 			if ((type = ELF_ST_TYPE(sym->st_info)) == STT_SECTION) {
1718*0bc07c75Srie 				if (sym->st_shndx == SHN_UNDEF) {
17195aefb655Srie 					eprintf(ofl->ofl_lml, ERR_WARNING,
17207c478bd9Sstevel@tonic-gate 					    MSG_INTL(MSG_SYM_INVSHNDX),
17217c478bd9Sstevel@tonic-gate 					    demangle(sdp->sd_name),
17227c478bd9Sstevel@tonic-gate 					    ifl->ifl_name,
1723*0bc07c75Srie 					    conv_sym_shndx(sym->st_shndx));
17247c478bd9Sstevel@tonic-gate 				}
17257c478bd9Sstevel@tonic-gate 				continue;
17267c478bd9Sstevel@tonic-gate 			}
17277c478bd9Sstevel@tonic-gate 
17287c478bd9Sstevel@tonic-gate 			/*
17297c478bd9Sstevel@tonic-gate 			 * Sanity check for TLS
17307c478bd9Sstevel@tonic-gate 			 */
1731*0bc07c75Srie 			if ((sym->st_size != 0) && ((type == STT_TLS) &&
1732*0bc07c75Srie 			    (sym->st_shndx != SHN_COMMON))) {
17337c478bd9Sstevel@tonic-gate 				Is_desc	*isp = sdp->sd_isc;
17347c478bd9Sstevel@tonic-gate 
17357c478bd9Sstevel@tonic-gate 				if ((isp == 0) || (isp->is_shdr == 0) ||
17367c478bd9Sstevel@tonic-gate 				    ((isp->is_shdr->sh_flags & SHF_TLS) == 0)) {
17375aefb655Srie 					eprintf(ofl->ofl_lml, ERR_FATAL,
17387c478bd9Sstevel@tonic-gate 					    MSG_INTL(MSG_SYM_TLS),
17397c478bd9Sstevel@tonic-gate 					    demangle(sdp->sd_name),
17407c478bd9Sstevel@tonic-gate 					    ifl->ifl_name);
17417c478bd9Sstevel@tonic-gate 					ofl->ofl_flags |= FLG_OF_FATAL;
17427c478bd9Sstevel@tonic-gate 					continue;
17437c478bd9Sstevel@tonic-gate 				}
17447c478bd9Sstevel@tonic-gate 			}
17457c478bd9Sstevel@tonic-gate 
17467c478bd9Sstevel@tonic-gate 			/*
17477c478bd9Sstevel@tonic-gate 			 * Carry our some basic sanity checks (these are just
17487c478bd9Sstevel@tonic-gate 			 * some of the erroneous symbol entries we've come
17497c478bd9Sstevel@tonic-gate 			 * across, there's probably a lot more).  The symbol
17507c478bd9Sstevel@tonic-gate 			 * will not be carried forward to the output file, which
17517c478bd9Sstevel@tonic-gate 			 * won't be a problem unless a relocation is required
17527c478bd9Sstevel@tonic-gate 			 * against it.
17537c478bd9Sstevel@tonic-gate 			 */
17547c478bd9Sstevel@tonic-gate 			if (((sdp->sd_flags & FLG_SY_SPECSEC) &&
1755*0bc07c75Srie 			    ((sym->st_shndx == SHN_COMMON)) ||
1756*0bc07c75Srie 			    ((type == STT_FILE) &&
1757*0bc07c75Srie 			    (sym->st_shndx != SHN_ABS))) ||
17587c478bd9Sstevel@tonic-gate 			    (sdp->sd_isc && (sdp->sd_isc->is_osdesc == 0))) {
17595aefb655Srie 				eprintf(ofl->ofl_lml, ERR_WARNING,
17605aefb655Srie 				    MSG_INTL(MSG_SYM_INVSHNDX),
17617c478bd9Sstevel@tonic-gate 				    demangle(sdp->sd_name), ifl->ifl_name,
1762*0bc07c75Srie 				    conv_sym_shndx(sym->st_shndx));
17637c478bd9Sstevel@tonic-gate 				sdp->sd_isc = NULL;
17647c478bd9Sstevel@tonic-gate 				sdp->sd_flags |= FLG_SY_INVALID;
17657c478bd9Sstevel@tonic-gate 				continue;
17667c478bd9Sstevel@tonic-gate 			}
17677c478bd9Sstevel@tonic-gate 
17687c478bd9Sstevel@tonic-gate 			/*
17697c478bd9Sstevel@tonic-gate 			 * As these local symbols will become part of the output
17707c478bd9Sstevel@tonic-gate 			 * image, record their number and name string size.
17717c478bd9Sstevel@tonic-gate 			 * Globals are counted after all input file processing
17727c478bd9Sstevel@tonic-gate 			 * (and hence symbol resolution) is complete during
17737c478bd9Sstevel@tonic-gate 			 * sym_validate().
17747c478bd9Sstevel@tonic-gate 			 */
17757c478bd9Sstevel@tonic-gate 			if (!(ofl->ofl_flags1 & FLG_OF1_REDLSYM)) {
17767c478bd9Sstevel@tonic-gate 				ofl->ofl_locscnt++;
17777c478bd9Sstevel@tonic-gate 
17787c478bd9Sstevel@tonic-gate 				if ((((sdp->sd_flags & FLG_SY_REGSYM) == 0) ||
17797c478bd9Sstevel@tonic-gate 				    sym->st_name) && (st_insert(ofl->ofl_strtab,
17807c478bd9Sstevel@tonic-gate 				    sdp->sd_name) == -1))
17817c478bd9Sstevel@tonic-gate 					return (S_ERROR);
17827c478bd9Sstevel@tonic-gate 			}
17837c478bd9Sstevel@tonic-gate 		}
17847c478bd9Sstevel@tonic-gate 	}
17857c478bd9Sstevel@tonic-gate 
17867c478bd9Sstevel@tonic-gate 	/*
17877c478bd9Sstevel@tonic-gate 	 * Now scan the global symbols entering them in the internal symbol
17887c478bd9Sstevel@tonic-gate 	 * table or resolving them as necessary.
17897c478bd9Sstevel@tonic-gate 	 */
17907c478bd9Sstevel@tonic-gate 	sym = (Sym *)isc->is_indata->d_buf;
17917c478bd9Sstevel@tonic-gate 	sym += local;
17927c478bd9Sstevel@tonic-gate 	/* LINTED */
17937c478bd9Sstevel@tonic-gate 	for (ndx = (int)local; ndx < total; sym++, ndx++) {
17947c478bd9Sstevel@tonic-gate 		const char	*name;
17957c478bd9Sstevel@tonic-gate 		Word		shndx, sdflags = 0;
17967c478bd9Sstevel@tonic-gate 
17977c478bd9Sstevel@tonic-gate 		/*
17987c478bd9Sstevel@tonic-gate 		 * Determine the associated section index.
17997c478bd9Sstevel@tonic-gate 		 */
18007c478bd9Sstevel@tonic-gate 		if (symshndx && (sym->st_shndx == SHN_XINDEX)) {
18017c478bd9Sstevel@tonic-gate 			shndx = symshndx[ndx];
18027c478bd9Sstevel@tonic-gate 		} else {
18037c478bd9Sstevel@tonic-gate 			shndx = sym->st_shndx;
18047c478bd9Sstevel@tonic-gate 			if (sym->st_shndx >= SHN_LORESERVE)
18057c478bd9Sstevel@tonic-gate 				sdflags |= FLG_SY_SPECSEC;
18067c478bd9Sstevel@tonic-gate 		}
18077c478bd9Sstevel@tonic-gate 
18087c478bd9Sstevel@tonic-gate 		/*
18097c478bd9Sstevel@tonic-gate 		 * Check if st_name has a valid value or not.
18107c478bd9Sstevel@tonic-gate 		 */
18115aefb655Srie 		if ((name = string(ofl, ifl, sym, strs, strsize, ndx, shndx,
18127c478bd9Sstevel@tonic-gate 		    symsecname, strsecname, &sdflags)) == 0) {
18137c478bd9Sstevel@tonic-gate 			ofl->ofl_flags |= FLG_OF_FATAL;
18147c478bd9Sstevel@tonic-gate 			continue;
18157c478bd9Sstevel@tonic-gate 		}
18167c478bd9Sstevel@tonic-gate 
18177c478bd9Sstevel@tonic-gate 		/*
18187c478bd9Sstevel@tonic-gate 		 * The linker itself will generate symbols for _end, _etext,
18197c478bd9Sstevel@tonic-gate 		 * _edata, _DYNAMIC and _PROCEDURE_LINKAGE_TABLE_, so don't
18207c478bd9Sstevel@tonic-gate 		 * bother entering these symbols from shared objects.  This
18217c478bd9Sstevel@tonic-gate 		 * results in some wasted resolution processing, which is hard
18227c478bd9Sstevel@tonic-gate 		 * to feel, but if nothing else, pollutes diagnostic relocation
18237c478bd9Sstevel@tonic-gate 		 * output.
18247c478bd9Sstevel@tonic-gate 		 */
18257c478bd9Sstevel@tonic-gate 		if (name[0] && (etype == ET_DYN) && (sym->st_size == 0) &&
18267c478bd9Sstevel@tonic-gate 		    (ELF_ST_TYPE(sym->st_info) == STT_OBJECT) &&
18277c478bd9Sstevel@tonic-gate 		    (name[0] == '_') && ((name[1] == 'e') ||
18287c478bd9Sstevel@tonic-gate 		    (name[1] == 'D') || (name[1] == 'P')) &&
18297c478bd9Sstevel@tonic-gate 		    ((strcmp(name, MSG_ORIG(MSG_SYM_ETEXT_U)) == 0) ||
18307c478bd9Sstevel@tonic-gate 		    (strcmp(name, MSG_ORIG(MSG_SYM_EDATA_U)) == 0) ||
18317c478bd9Sstevel@tonic-gate 		    (strcmp(name, MSG_ORIG(MSG_SYM_END_U)) == 0) ||
18327c478bd9Sstevel@tonic-gate 		    (strcmp(name, MSG_ORIG(MSG_SYM_DYNAMIC_U)) == 0) ||
18337c478bd9Sstevel@tonic-gate 		    (strcmp(name, MSG_ORIG(MSG_SYM_PLKTBL_U)) == 0))) {
18347c478bd9Sstevel@tonic-gate 			ifl->ifl_oldndx[ndx] = 0;
18357c478bd9Sstevel@tonic-gate 			continue;
18367c478bd9Sstevel@tonic-gate 		}
18377c478bd9Sstevel@tonic-gate 
18387c478bd9Sstevel@tonic-gate 		/*
18397c478bd9Sstevel@tonic-gate 		 * Determine and validate the symbols binding.
18407c478bd9Sstevel@tonic-gate 		 */
18417c478bd9Sstevel@tonic-gate 		bind = ELF_ST_BIND(sym->st_info);
18427c478bd9Sstevel@tonic-gate 		if ((bind != STB_GLOBAL) && (bind != STB_WEAK)) {
18435aefb655Srie 			eprintf(ofl->ofl_lml, ERR_WARNING,
18445aefb655Srie 			    MSG_INTL(MSG_SYM_NONGLOB), demangle(name),
18455aefb655Srie 			    ifl->ifl_name, conv_sym_info_bind(bind));
18467c478bd9Sstevel@tonic-gate 			continue;
18477c478bd9Sstevel@tonic-gate 		}
18487c478bd9Sstevel@tonic-gate 
18497c478bd9Sstevel@tonic-gate 		/*
18507c478bd9Sstevel@tonic-gate 		 * If this symbol falls within the range of a section being
18517c478bd9Sstevel@tonic-gate 		 * discarded, then discard the symbol itself.
18527c478bd9Sstevel@tonic-gate 		 */
18537c478bd9Sstevel@tonic-gate 		if (((sdflags & FLG_SY_SPECSEC) == 0) &&
1854*0bc07c75Srie 		    (sym->st_shndx != SHN_UNDEF)) {
18557c478bd9Sstevel@tonic-gate 			Is_desc	*isp;
18567c478bd9Sstevel@tonic-gate 
18577c478bd9Sstevel@tonic-gate 			if (shndx >= ifl->ifl_shnum) {
18587c478bd9Sstevel@tonic-gate 				/*
18597c478bd9Sstevel@tonic-gate 				 * Carry our some basic sanity checks
18607c478bd9Sstevel@tonic-gate 				 * The symbol will not be carried forward to
18617c478bd9Sstevel@tonic-gate 				 * the output file, which won't be a problem
18627c478bd9Sstevel@tonic-gate 				 * unless a relocation is required against it.
18637c478bd9Sstevel@tonic-gate 				 */
18645aefb655Srie 				eprintf(ofl->ofl_lml, ERR_WARNING,
18655aefb655Srie 				    MSG_INTL(MSG_SYM_INVSHNDX), demangle(name),
1866*0bc07c75Srie 				    ifl->ifl_name,
1867*0bc07c75Srie 				    conv_sym_shndx(sym->st_shndx));
18687c478bd9Sstevel@tonic-gate 				continue;
18697c478bd9Sstevel@tonic-gate 			}
18707c478bd9Sstevel@tonic-gate 
18717c478bd9Sstevel@tonic-gate 			isp = ifl->ifl_isdesc[shndx];
18727c478bd9Sstevel@tonic-gate 			if (isp && (isp->is_flags & FLG_IS_DISCARD)) {
18737c478bd9Sstevel@tonic-gate 				if ((sdp =
18747c478bd9Sstevel@tonic-gate 				    libld_calloc(sizeof (Sym_desc), 1)) == 0)
18757c478bd9Sstevel@tonic-gate 					return (S_ERROR);
18767c478bd9Sstevel@tonic-gate 
18777c478bd9Sstevel@tonic-gate 				/*
18787c478bd9Sstevel@tonic-gate 				 * Create a dummy symbol entry so that if we
18797c478bd9Sstevel@tonic-gate 				 * find any references to this discarded symbol
18807c478bd9Sstevel@tonic-gate 				 * we can compensate.
18817c478bd9Sstevel@tonic-gate 				 */
18827c478bd9Sstevel@tonic-gate 				sdp->sd_name = name;
18837c478bd9Sstevel@tonic-gate 				sdp->sd_sym = sym;
18847c478bd9Sstevel@tonic-gate 				sdp->sd_file = ifl;
18857c478bd9Sstevel@tonic-gate 				sdp->sd_isc = isp;
18867c478bd9Sstevel@tonic-gate 				sdp->sd_flags = FLG_SY_ISDISC;
18877c478bd9Sstevel@tonic-gate 				ifl->ifl_oldndx[ndx] = sdp;
18887c478bd9Sstevel@tonic-gate 
18895aefb655Srie 				DBG_CALL(Dbg_syms_discarded(ofl->ofl_lml, sdp,
18905aefb655Srie 				    sdp->sd_isc));
18917c478bd9Sstevel@tonic-gate 				continue;
18927c478bd9Sstevel@tonic-gate 			}
18937c478bd9Sstevel@tonic-gate 		}
18947c478bd9Sstevel@tonic-gate 
18957c478bd9Sstevel@tonic-gate 		/*
18967c478bd9Sstevel@tonic-gate 		 * If the symbol does not already exist in the internal symbol
18977c478bd9Sstevel@tonic-gate 		 * table add it, otherwise resolve the conflict.  If the symbol
18987c478bd9Sstevel@tonic-gate 		 * from this file is kept, retain its symbol table index for
18997c478bd9Sstevel@tonic-gate 		 * possible use in associating a global alias.
19007c478bd9Sstevel@tonic-gate 		 */
19017c478bd9Sstevel@tonic-gate 		/* LINTED */
19027c478bd9Sstevel@tonic-gate 		hash = (Word)elf_hash((const char *)name);
19035aefb655Srie 		if ((sdp = ld_sym_find(name, hash, &where, ofl)) == NULL) {
19045aefb655Srie 			DBG_CALL(Dbg_syms_global(ofl->ofl_lml, ndx, name));
19055aefb655Srie 			if ((sdp = ld_sym_enter(name, sym, hash, ifl, ofl, ndx,
19067c478bd9Sstevel@tonic-gate 			    shndx, sdflags, 0, &where)) == (Sym_desc *)S_ERROR)
19077c478bd9Sstevel@tonic-gate 				return (S_ERROR);
19087c478bd9Sstevel@tonic-gate 
19095aefb655Srie 		} else if (ld_sym_resolve(sdp, sym, ifl, ofl, ndx, shndx,
19107c478bd9Sstevel@tonic-gate 		    sdflags) == S_ERROR)
19117c478bd9Sstevel@tonic-gate 			return (S_ERROR);
19127c478bd9Sstevel@tonic-gate 
19137c478bd9Sstevel@tonic-gate 		/*
19147c478bd9Sstevel@tonic-gate 		 * After we've compared a defined symbol in one shared
19157c478bd9Sstevel@tonic-gate 		 * object, flag the symbol so we don't compare it again.
19167c478bd9Sstevel@tonic-gate 		 */
1917*0bc07c75Srie 		if ((etype == ET_DYN) && (sym->st_shndx != SHN_UNDEF) &&
19187c478bd9Sstevel@tonic-gate 		    ((sdp->sd_flags & FLG_SY_SOFOUND) == 0))
19197c478bd9Sstevel@tonic-gate 			sdp->sd_flags |= FLG_SY_SOFOUND;
19207c478bd9Sstevel@tonic-gate 
19217c478bd9Sstevel@tonic-gate 		/*
19227c478bd9Sstevel@tonic-gate 		 * If the symbol is accepted from this file retain the symbol
19237c478bd9Sstevel@tonic-gate 		 * index for possible use in aliasing.
19247c478bd9Sstevel@tonic-gate 		 */
19257c478bd9Sstevel@tonic-gate 		if (sdp->sd_file == ifl)
19267c478bd9Sstevel@tonic-gate 			sdp->sd_symndx = ndx;
19277c478bd9Sstevel@tonic-gate 
19287c478bd9Sstevel@tonic-gate 		ifl->ifl_oldndx[ndx] = sdp;
19297c478bd9Sstevel@tonic-gate 
19307c478bd9Sstevel@tonic-gate 		/*
19317c478bd9Sstevel@tonic-gate 		 * If we've accepted a register symbol, continue to validate
19327c478bd9Sstevel@tonic-gate 		 * it.
19337c478bd9Sstevel@tonic-gate 		 */
19347c478bd9Sstevel@tonic-gate 		if (sdp->sd_flags & FLG_SY_REGSYM) {
19357c478bd9Sstevel@tonic-gate 			Sym_desc	*rsdp;
19367c478bd9Sstevel@tonic-gate 
19375aefb655Srie 			if ((rsdp = ld_reg_find(sdp->sd_sym, ofl)) == 0) {
19385aefb655Srie 				if (ld_reg_enter(sdp, ofl) == 0)
19397c478bd9Sstevel@tonic-gate 					return (S_ERROR);
19407c478bd9Sstevel@tonic-gate 			} else if (rsdp != sdp) {
19415aefb655Srie 				(void) ld_reg_check(rsdp, sdp->sd_sym,
19427c478bd9Sstevel@tonic-gate 				    sdp->sd_name, ifl, ofl);
19437c478bd9Sstevel@tonic-gate 			}
19447c478bd9Sstevel@tonic-gate 		}
19457c478bd9Sstevel@tonic-gate 	}
19467c478bd9Sstevel@tonic-gate 
19477c478bd9Sstevel@tonic-gate 	/*
19487c478bd9Sstevel@tonic-gate 	 * If this is a shared object scan the globals one more time and
19497c478bd9Sstevel@tonic-gate 	 * associate any weak/global associations.  This association is needed
19507c478bd9Sstevel@tonic-gate 	 * should the weak definition satisfy a reference in the dynamic
19517c478bd9Sstevel@tonic-gate 	 * executable:
19527c478bd9Sstevel@tonic-gate 	 *
19537c478bd9Sstevel@tonic-gate 	 *  o	if the symbol is a data item it will be copied to the
19547c478bd9Sstevel@tonic-gate 	 *	executables address space, thus we must also reassociate the
19557c478bd9Sstevel@tonic-gate 	 *	alias symbol with its new location in the executable.
19567c478bd9Sstevel@tonic-gate 	 *
19577c478bd9Sstevel@tonic-gate 	 *  o	if the symbol is a function then we may need to promote	the
19587c478bd9Sstevel@tonic-gate 	 *	symbols binding from undefined weak to undefined, otherwise the
19597c478bd9Sstevel@tonic-gate 	 *	run-time linker will not generate the correct relocation error
19607c478bd9Sstevel@tonic-gate 	 *	should the symbol not be found.
19617c478bd9Sstevel@tonic-gate 	 *
19627c478bd9Sstevel@tonic-gate 	 * The true association between a weak/strong symbol pair is that both
19637c478bd9Sstevel@tonic-gate 	 * symbol entries are identical, thus first we created a sorted symbol
19647c478bd9Sstevel@tonic-gate 	 * list keyed off of the symbols value (if the value is the same chances
19657c478bd9Sstevel@tonic-gate 	 * are the rest of the symbols data is).  This list is then scanned for
19667c478bd9Sstevel@tonic-gate 	 * weak symbols, and if one is found then any strong association will
19677c478bd9Sstevel@tonic-gate 	 * exist in the following entries.  Thus we just have to scan one
19687c478bd9Sstevel@tonic-gate 	 * (typical single alias) or more (in the uncommon instance of multiple
19697c478bd9Sstevel@tonic-gate 	 * weak to strong associations) entries to determine if a match exists.
19707c478bd9Sstevel@tonic-gate 	 */
19717c478bd9Sstevel@tonic-gate 	if (ifl->ifl_ehdr->e_type == ET_DYN) {
19727c478bd9Sstevel@tonic-gate 		Sym_desc **	sort;
19737c478bd9Sstevel@tonic-gate 		size_t		size = (total - local) * sizeof (Sym_desc *);
19747c478bd9Sstevel@tonic-gate 
19757c478bd9Sstevel@tonic-gate 		if ((sort = libld_malloc(size)) == 0)
19767c478bd9Sstevel@tonic-gate 			return (S_ERROR);
19777c478bd9Sstevel@tonic-gate 		(void) memcpy((void *)sort, &ifl->ifl_oldndx[local], size);
19787c478bd9Sstevel@tonic-gate 
19797c478bd9Sstevel@tonic-gate 		qsort(sort, (total - local), sizeof (Sym_desc *), compare);
19807c478bd9Sstevel@tonic-gate 
19817c478bd9Sstevel@tonic-gate 		for (ndx = 0; ndx < (total - local); ndx++) {
19827c478bd9Sstevel@tonic-gate 			Sym_desc *	wsdp = sort[ndx];
19837c478bd9Sstevel@tonic-gate 			Sym *		wsym;
19847c478bd9Sstevel@tonic-gate 			int		sndx;
19857c478bd9Sstevel@tonic-gate 
19867c478bd9Sstevel@tonic-gate 			if (wsdp == 0)
19877c478bd9Sstevel@tonic-gate 				continue;
19887c478bd9Sstevel@tonic-gate 
19897c478bd9Sstevel@tonic-gate 			wsym = wsdp->sd_sym;
19907c478bd9Sstevel@tonic-gate 
19917c478bd9Sstevel@tonic-gate 			if ((ELF_ST_BIND(wsym->st_info) != STB_WEAK) ||
1992*0bc07c75Srie 			    (wsdp->sd_sym->st_shndx == SHN_UNDEF) ||
19937c478bd9Sstevel@tonic-gate 			    (wsdp->sd_flags & FLG_SY_SPECSEC))
19947c478bd9Sstevel@tonic-gate 				continue;
19957c478bd9Sstevel@tonic-gate 
19967c478bd9Sstevel@tonic-gate 			/*
19977c478bd9Sstevel@tonic-gate 			 * We have a weak symbol, if it has a strong alias it
19987c478bd9Sstevel@tonic-gate 			 * will have been sorted to one of the following sort
19997c478bd9Sstevel@tonic-gate 			 * table entries.  Note that we could have multiple weak
20007c478bd9Sstevel@tonic-gate 			 * symbols aliased to one strong (if this occurs then
20017c478bd9Sstevel@tonic-gate 			 * the strong symbol only maintains one alias back to
20027c478bd9Sstevel@tonic-gate 			 * the last weak).
20037c478bd9Sstevel@tonic-gate 			 */
20047c478bd9Sstevel@tonic-gate 			for (sndx = ndx + 1; sndx < (total - local); sndx++) {
20057c478bd9Sstevel@tonic-gate 				Sym_desc *	ssdp = sort[sndx];
20067c478bd9Sstevel@tonic-gate 				Sym *		ssym;
20077c478bd9Sstevel@tonic-gate 
20087c478bd9Sstevel@tonic-gate 				if (ssdp == 0)
20097c478bd9Sstevel@tonic-gate 					break;
20107c478bd9Sstevel@tonic-gate 
20117c478bd9Sstevel@tonic-gate 				ssym = ssdp->sd_sym;
20127c478bd9Sstevel@tonic-gate 
20137c478bd9Sstevel@tonic-gate 				if (wsym->st_value != ssym->st_value)
20147c478bd9Sstevel@tonic-gate 					break;
20157c478bd9Sstevel@tonic-gate 
20167c478bd9Sstevel@tonic-gate 				if ((ssdp->sd_file == ifl) &&
20177c478bd9Sstevel@tonic-gate 				    (wsdp->sd_file == ifl) &&
20187c478bd9Sstevel@tonic-gate 				    (wsym->st_size == ssym->st_size) &&
2019*0bc07c75Srie 				    (ssdp->sd_sym->st_shndx != SHN_UNDEF) &&
20207c478bd9Sstevel@tonic-gate 				    (ELF_ST_BIND(ssym->st_info) != STB_WEAK) &&
20217c478bd9Sstevel@tonic-gate 				    ((ssdp->sd_flags & FLG_SY_SPECSEC) == 0)) {
20227c478bd9Sstevel@tonic-gate 					ssdp->sd_aux->sa_linkndx =
20237c478bd9Sstevel@tonic-gate 					    (Word)wsdp->sd_symndx;
20247c478bd9Sstevel@tonic-gate 					wsdp->sd_aux->sa_linkndx =
20257c478bd9Sstevel@tonic-gate 					    (Word)ssdp->sd_symndx;
20267c478bd9Sstevel@tonic-gate 					break;
20277c478bd9Sstevel@tonic-gate 				}
20287c478bd9Sstevel@tonic-gate 			}
20297c478bd9Sstevel@tonic-gate 		}
20307c478bd9Sstevel@tonic-gate 	}
20317c478bd9Sstevel@tonic-gate 	return (1);
20327c478bd9Sstevel@tonic-gate }
20337c478bd9Sstevel@tonic-gate 
20347c478bd9Sstevel@tonic-gate /*
20357c478bd9Sstevel@tonic-gate  * Add an undefined symbol to the symbol table (ie. from -u name option)
20367c478bd9Sstevel@tonic-gate  */
20377c478bd9Sstevel@tonic-gate Sym_desc *
20385aefb655Srie ld_sym_add_u(const char *name, Ofl_desc *ofl)
20397c478bd9Sstevel@tonic-gate {
20407c478bd9Sstevel@tonic-gate 	Sym		*sym;
20417c478bd9Sstevel@tonic-gate 	Ifl_desc	*ifl = 0, *_ifl;
20427c478bd9Sstevel@tonic-gate 	Sym_desc	*sdp;
20437c478bd9Sstevel@tonic-gate 	Word		hash;
20447c478bd9Sstevel@tonic-gate 	Listnode	*lnp;
20457c478bd9Sstevel@tonic-gate 	avl_index_t	where;
20467c478bd9Sstevel@tonic-gate 	const char	*cmdline = MSG_INTL(MSG_STR_COMMAND);
20477c478bd9Sstevel@tonic-gate 
20487c478bd9Sstevel@tonic-gate 	/*
20497c478bd9Sstevel@tonic-gate 	 * If the symbol reference already exists indicate that a reference
20507c478bd9Sstevel@tonic-gate 	 * also came from the command line.
20517c478bd9Sstevel@tonic-gate 	 */
20527c478bd9Sstevel@tonic-gate 	/* LINTED */
20537c478bd9Sstevel@tonic-gate 	hash = (Word)elf_hash(name);
20545aefb655Srie 	if (sdp = ld_sym_find(name, hash, &where, ofl)) {
20557c478bd9Sstevel@tonic-gate 		if (sdp->sd_ref == REF_DYN_SEEN)
20567c478bd9Sstevel@tonic-gate 			sdp->sd_ref = REF_DYN_NEED;
20577c478bd9Sstevel@tonic-gate 		return (sdp);
20587c478bd9Sstevel@tonic-gate 	}
20597c478bd9Sstevel@tonic-gate 
20607c478bd9Sstevel@tonic-gate 	/*
20617c478bd9Sstevel@tonic-gate 	 * Determine whether a pseudo input file descriptor exists to represent
20627c478bd9Sstevel@tonic-gate 	 * the command line, as any global symbol needs an input file descriptor
20637c478bd9Sstevel@tonic-gate 	 * during any symbol resolution (refer to map_ifl() which provides a
20647c478bd9Sstevel@tonic-gate 	 * similar method for adding symbols from mapfiles).
20657c478bd9Sstevel@tonic-gate 	 */
20667c478bd9Sstevel@tonic-gate 	for (LIST_TRAVERSE(&ofl->ofl_objs, lnp, _ifl))
20677c478bd9Sstevel@tonic-gate 		if (strcmp(_ifl->ifl_name, cmdline) == 0) {
20687c478bd9Sstevel@tonic-gate 			ifl = _ifl;
20697c478bd9Sstevel@tonic-gate 			break;
20707c478bd9Sstevel@tonic-gate 		}
20717c478bd9Sstevel@tonic-gate 
20727c478bd9Sstevel@tonic-gate 	/*
20737c478bd9Sstevel@tonic-gate 	 * If no descriptor exists create one.
20747c478bd9Sstevel@tonic-gate 	 */
20757c478bd9Sstevel@tonic-gate 	if (ifl == 0) {
20767c478bd9Sstevel@tonic-gate 		if ((ifl = libld_calloc(sizeof (Ifl_desc), 1)) ==
20777c478bd9Sstevel@tonic-gate 		    (Ifl_desc *)0)
20787c478bd9Sstevel@tonic-gate 			return ((Sym_desc *)S_ERROR);
20797c478bd9Sstevel@tonic-gate 		ifl->ifl_name = cmdline;
20807c478bd9Sstevel@tonic-gate 		ifl->ifl_flags = FLG_IF_NEEDED | FLG_IF_FILEREF;
20817c478bd9Sstevel@tonic-gate 		if ((ifl->ifl_ehdr = libld_calloc(sizeof (Ehdr),
20827c478bd9Sstevel@tonic-gate 		    1)) == 0)
20837c478bd9Sstevel@tonic-gate 			return ((Sym_desc *)S_ERROR);
20847c478bd9Sstevel@tonic-gate 		ifl->ifl_ehdr->e_type = ET_REL;
20857c478bd9Sstevel@tonic-gate 
20867c478bd9Sstevel@tonic-gate 		if (list_appendc(&ofl->ofl_objs, ifl) == 0)
20877c478bd9Sstevel@tonic-gate 			return ((Sym_desc *)S_ERROR);
20887c478bd9Sstevel@tonic-gate 	}
20897c478bd9Sstevel@tonic-gate 
20907c478bd9Sstevel@tonic-gate 	/*
20917c478bd9Sstevel@tonic-gate 	 * Allocate a symbol structure and add it to the global symbol table.
20927c478bd9Sstevel@tonic-gate 	 */
20937c478bd9Sstevel@tonic-gate 	if ((sym = libld_calloc(sizeof (Sym), 1)) == 0)
20947c478bd9Sstevel@tonic-gate 		return ((Sym_desc *)S_ERROR);
20957c478bd9Sstevel@tonic-gate 	sym->st_info = ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE);
20967c478bd9Sstevel@tonic-gate 	sym->st_shndx = SHN_UNDEF;
20977c478bd9Sstevel@tonic-gate 
20985aefb655Srie 	DBG_CALL(Dbg_syms_process(ofl->ofl_lml, ifl));
20995aefb655Srie 	DBG_CALL(Dbg_syms_global(ofl->ofl_lml, 0, name));
21005aefb655Srie 	sdp = ld_sym_enter(name, sym, hash, ifl, ofl, 0, SHN_UNDEF,
21015aefb655Srie 	    0, 0, &where);
21027c478bd9Sstevel@tonic-gate 	sdp->sd_flags &= ~FLG_SY_CLEAN;
21037c478bd9Sstevel@tonic-gate 	sdp->sd_flags |= FLG_SY_CMDREF;
21047c478bd9Sstevel@tonic-gate 
21057c478bd9Sstevel@tonic-gate 	return (sdp);
21067c478bd9Sstevel@tonic-gate }
2107