xref: /onnv-gate/usr/src/cmd/sgs/libld/common/files.c (revision 11993)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51618Srie  * Common Development and Distribution License (the "License").
61618Srie  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
211109Srie 
220Sstevel@tonic-gate /*
230Sstevel@tonic-gate  *	Copyright (c) 1988 AT&T
240Sstevel@tonic-gate  *	  All Rights Reserved
250Sstevel@tonic-gate  *
2611734SAli.Bahrami@Sun.COM  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
270Sstevel@tonic-gate  * Use is subject to license terms.
280Sstevel@tonic-gate  */
290Sstevel@tonic-gate 
300Sstevel@tonic-gate /*
310Sstevel@tonic-gate  * Processing of relocatable objects and shared objects.
320Sstevel@tonic-gate  */
336206Sab196087 
346206Sab196087 #define	ELF_TARGET_AMD64
356206Sab196087 #define	ELF_TARGET_SPARC
366206Sab196087 
370Sstevel@tonic-gate #include	<stdio.h>
380Sstevel@tonic-gate #include	<string.h>
390Sstevel@tonic-gate #include	<fcntl.h>
400Sstevel@tonic-gate #include	<unistd.h>
410Sstevel@tonic-gate #include	<link.h>
420Sstevel@tonic-gate #include	<limits.h>
430Sstevel@tonic-gate #include	<sys/stat.h>
440Sstevel@tonic-gate #include	<sys/systeminfo.h>
450Sstevel@tonic-gate #include	<debug.h>
460Sstevel@tonic-gate #include	<msg.h>
470Sstevel@tonic-gate #include	<_libld.h>
480Sstevel@tonic-gate 
490Sstevel@tonic-gate /*
500Sstevel@tonic-gate  * Decide if we can link against this input file.
510Sstevel@tonic-gate  */
520Sstevel@tonic-gate static int
537463SRod.Evans@Sun.COM ifl_verify(Ehdr *ehdr, Ofl_desc *ofl, Rej_desc *rej)
540Sstevel@tonic-gate {
550Sstevel@tonic-gate 	/*
560Sstevel@tonic-gate 	 * Check the validity of the elf header information for compatibility
570Sstevel@tonic-gate 	 * with this machine and our own internal elf library.
580Sstevel@tonic-gate 	 */
596206Sab196087 	if ((ehdr->e_machine != ld_targ.t_m.m_mach) &&
606206Sab196087 	    ((ehdr->e_machine != ld_targ.t_m.m_machplus) &&
616206Sab196087 	    ((ehdr->e_flags & ld_targ.t_m.m_flagsplus) == 0))) {
620Sstevel@tonic-gate 		rej->rej_type = SGS_REJ_MACH;
630Sstevel@tonic-gate 		rej->rej_info = (uint_t)ehdr->e_machine;
640Sstevel@tonic-gate 		return (0);
650Sstevel@tonic-gate 	}
666206Sab196087 	if (ehdr->e_ident[EI_DATA] != ld_targ.t_m.m_data) {
670Sstevel@tonic-gate 		rej->rej_type = SGS_REJ_DATA;
680Sstevel@tonic-gate 		rej->rej_info = (uint_t)ehdr->e_ident[EI_DATA];
690Sstevel@tonic-gate 		return (0);
700Sstevel@tonic-gate 	}
711618Srie 	if (ehdr->e_version > ofl->ofl_dehdr->e_version) {
720Sstevel@tonic-gate 		rej->rej_type = SGS_REJ_VERSION;
730Sstevel@tonic-gate 		rej->rej_info = (uint_t)ehdr->e_version;
740Sstevel@tonic-gate 		return (0);
750Sstevel@tonic-gate 	}
760Sstevel@tonic-gate 	return (1);
770Sstevel@tonic-gate }
780Sstevel@tonic-gate 
790Sstevel@tonic-gate /*
800Sstevel@tonic-gate  * Check sanity of file header and allocate an infile descriptor
810Sstevel@tonic-gate  * for the file being processed.
820Sstevel@tonic-gate  */
831618Srie static Ifl_desc *
844716Sab196087 ifl_setup(const char *name, Ehdr *ehdr, Elf *elf, Word flags, Ofl_desc *ofl,
850Sstevel@tonic-gate     Rej_desc *rej)
860Sstevel@tonic-gate {
870Sstevel@tonic-gate 	Ifl_desc	*ifl;
880Sstevel@tonic-gate 	Rej_desc	_rej = { 0 };
890Sstevel@tonic-gate 
900Sstevel@tonic-gate 	if (ifl_verify(ehdr, ofl, &_rej) == 0) {
910Sstevel@tonic-gate 		_rej.rej_name = name;
926206Sab196087 		DBG_CALL(Dbg_file_rejected(ofl->ofl_lml, &_rej,
936206Sab196087 		    ld_targ.t_m.m_mach));
940Sstevel@tonic-gate 		if (rej->rej_type == 0) {
950Sstevel@tonic-gate 			*rej = _rej;
960Sstevel@tonic-gate 			rej->rej_name = strdup(_rej.rej_name);
970Sstevel@tonic-gate 		}
980Sstevel@tonic-gate 		return (0);
990Sstevel@tonic-gate 	}
1000Sstevel@tonic-gate 
10110792SRod.Evans@Sun.COM 	if ((ifl = libld_calloc(1, sizeof (Ifl_desc))) == NULL)
1020Sstevel@tonic-gate 		return ((Ifl_desc *)S_ERROR);
1030Sstevel@tonic-gate 	ifl->ifl_name = name;
1040Sstevel@tonic-gate 	ifl->ifl_ehdr = ehdr;
1050Sstevel@tonic-gate 	ifl->ifl_elf = elf;
1060Sstevel@tonic-gate 	ifl->ifl_flags = flags;
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate 	/*
1090Sstevel@tonic-gate 	 * Is this file using 'extended Section Indexes'.  If so, use the
1100Sstevel@tonic-gate 	 * e_shnum & e_shstrndx which can be found at:
1110Sstevel@tonic-gate 	 *
1120Sstevel@tonic-gate 	 *	e_shnum == Shdr[0].sh_size
1130Sstevel@tonic-gate 	 *	e_shstrndx == Shdr[0].sh_link
1140Sstevel@tonic-gate 	 */
1150Sstevel@tonic-gate 	if ((ehdr->e_shnum == 0) && (ehdr->e_shoff != 0)) {
1160Sstevel@tonic-gate 		Elf_Scn	*scn;
1170Sstevel@tonic-gate 		Shdr	*shdr0;
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate 		if ((scn = elf_getscn(elf, 0)) == NULL) {
1201618Srie 			eprintf(ofl->ofl_lml, ERR_ELF,
1211618Srie 			    MSG_INTL(MSG_ELF_GETSCN), name);
1220Sstevel@tonic-gate 			ofl->ofl_flags |= FLG_OF_FATAL;
1230Sstevel@tonic-gate 			return ((Ifl_desc *)S_ERROR);
1240Sstevel@tonic-gate 		}
1250Sstevel@tonic-gate 		if ((shdr0 = elf_getshdr(scn)) == NULL) {
1261618Srie 			eprintf(ofl->ofl_lml, ERR_ELF,
1271618Srie 			    MSG_INTL(MSG_ELF_GETSHDR), name);
1280Sstevel@tonic-gate 			ofl->ofl_flags |= FLG_OF_FATAL;
1290Sstevel@tonic-gate 			return ((Ifl_desc *)S_ERROR);
1300Sstevel@tonic-gate 		}
1310Sstevel@tonic-gate 		ifl->ifl_shnum = (Word)shdr0->sh_size;
1320Sstevel@tonic-gate 		if (ehdr->e_shstrndx == SHN_XINDEX)
1330Sstevel@tonic-gate 			ifl->ifl_shstrndx = shdr0->sh_link;
1340Sstevel@tonic-gate 		else
1350Sstevel@tonic-gate 			ifl->ifl_shstrndx = ehdr->e_shstrndx;
1360Sstevel@tonic-gate 	} else {
1370Sstevel@tonic-gate 		ifl->ifl_shnum = ehdr->e_shnum;
1380Sstevel@tonic-gate 		ifl->ifl_shstrndx = ehdr->e_shstrndx;
1390Sstevel@tonic-gate 	}
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate 	if ((ifl->ifl_isdesc = libld_calloc(ifl->ifl_shnum,
14210792SRod.Evans@Sun.COM 	    sizeof (Is_desc *))) == NULL)
1430Sstevel@tonic-gate 		return ((Ifl_desc *)S_ERROR);
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate 	/*
1460Sstevel@tonic-gate 	 * Record this new input file on the shared object or relocatable
1470Sstevel@tonic-gate 	 * object input file list.
1480Sstevel@tonic-gate 	 */
1490Sstevel@tonic-gate 	if (ifl->ifl_ehdr->e_type == ET_DYN) {
1509131SRod.Evans@Sun.COM 		if (aplist_append(&ofl->ofl_sos, ifl, AL_CNT_OFL_LIBS) == NULL)
1519131SRod.Evans@Sun.COM 			return ((Ifl_desc *)S_ERROR);
1520Sstevel@tonic-gate 	} else {
1539131SRod.Evans@Sun.COM 		if (aplist_append(&ofl->ofl_objs, ifl, AL_CNT_OFL_OBJS) == NULL)
1549131SRod.Evans@Sun.COM 			return ((Ifl_desc *)S_ERROR);
1550Sstevel@tonic-gate 	}
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate 	return (ifl);
1580Sstevel@tonic-gate }
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate /*
1610Sstevel@tonic-gate  * Process a generic section.  The appropriate section information is added
1620Sstevel@tonic-gate  * to the files input descriptor list.
1630Sstevel@tonic-gate  */
1641618Srie static uintptr_t
1650Sstevel@tonic-gate process_section(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
1660Sstevel@tonic-gate     Word ndx, int ident, Ofl_desc *ofl)
1670Sstevel@tonic-gate {
1680Sstevel@tonic-gate 	Is_desc	*isp;
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate 	/*
1710Sstevel@tonic-gate 	 * Create a new input section descriptor.  If this is a NOBITS
1720Sstevel@tonic-gate 	 * section elf_getdata() will still create a data buffer (the buffer
1730Sstevel@tonic-gate 	 * will be null and the size will reflect the actual memory size).
1740Sstevel@tonic-gate 	 */
17510792SRod.Evans@Sun.COM 	if ((isp = libld_calloc(sizeof (Is_desc), 1)) == NULL)
1760Sstevel@tonic-gate 		return (S_ERROR);
1770Sstevel@tonic-gate 	isp->is_shdr = shdr;
1780Sstevel@tonic-gate 	isp->is_file = ifl;
1790Sstevel@tonic-gate 	isp->is_name = name;
1800Sstevel@tonic-gate 	isp->is_scnndx = ndx;
181574Sseizo 	isp->is_flags = FLG_IS_EXTERNAL;
1827463SRod.Evans@Sun.COM 	isp->is_keyident = ident;
1837463SRod.Evans@Sun.COM 
1840Sstevel@tonic-gate 	if ((isp->is_indata = elf_getdata(scn, NULL)) == NULL) {
1851618Srie 		eprintf(ofl->ofl_lml, ERR_ELF, MSG_INTL(MSG_ELF_GETDATA),
1861618Srie 		    ifl->ifl_name);
1870Sstevel@tonic-gate 		ofl->ofl_flags |= FLG_OF_FATAL;
1880Sstevel@tonic-gate 		return (0);
1890Sstevel@tonic-gate 	}
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate 	if ((shdr->sh_flags & SHF_EXCLUDE) &&
1920Sstevel@tonic-gate 	    ((ofl->ofl_flags & FLG_OF_RELOBJ) == 0)) {
1930Sstevel@tonic-gate 		isp->is_flags |= FLG_IS_DISCARD;
1940Sstevel@tonic-gate 	}
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate 	/*
1970Sstevel@tonic-gate 	 * Add the new input section to the files input section list and
1987463SRod.Evans@Sun.COM 	 * flag whether the section needs placing in an output section.  This
1997463SRod.Evans@Sun.COM 	 * placement is deferred until all input section processing has been
2007463SRod.Evans@Sun.COM 	 * completed, as SHT_GROUP sections can provide information that will
2017463SRod.Evans@Sun.COM 	 * affect how other sections within the file should be placed.
2020Sstevel@tonic-gate 	 */
2030Sstevel@tonic-gate 	ifl->ifl_isdesc[ndx] = isp;
2040Sstevel@tonic-gate 
2057463SRod.Evans@Sun.COM 	if (ident) {
2067463SRod.Evans@Sun.COM 		if (shdr->sh_flags & ALL_SHF_ORDER) {
2077463SRod.Evans@Sun.COM 			isp->is_flags |= FLG_IS_ORDERED;
2087463SRod.Evans@Sun.COM 			ifl->ifl_flags |= FLG_IF_ORDERED;
2092647Srie 		}
2107463SRod.Evans@Sun.COM 		isp->is_flags |= FLG_IS_PLACE;
2110Sstevel@tonic-gate 	}
2120Sstevel@tonic-gate 	return (1);
2130Sstevel@tonic-gate }
2140Sstevel@tonic-gate 
2150Sstevel@tonic-gate /*
2160Sstevel@tonic-gate  * Determine the software capabilities of the object being built from the
2170Sstevel@tonic-gate  * capabilities of the input relocatable objects.   One software capability
2180Sstevel@tonic-gate  * is presently recognized, and represented with the following (sys/elf.h):
2190Sstevel@tonic-gate  *
2200Sstevel@tonic-gate  *   SF1_SUNW_FPKNWN	use/non-use of frame pointer is known, and
2210Sstevel@tonic-gate  *   SF1_SUNW_FPUSED    the frame pointer is in use.
2220Sstevel@tonic-gate  *
2230Sstevel@tonic-gate  * The resolution of the present fame pointer state, and the capabilities
2240Sstevel@tonic-gate  * provided by a new input relocatable object are:
2250Sstevel@tonic-gate  *
2260Sstevel@tonic-gate  *                              new input relocatable object
2270Sstevel@tonic-gate  *
2280Sstevel@tonic-gate  *      present      |  SF1_SUNW_FPKNWN  |  SF1_SUNW_FPKNWN  |    <unknown>
2290Sstevel@tonic-gate  *       state       |  SF1_SUNW_FPUSED  |                   |
2300Sstevel@tonic-gate  *  ---------------------------------------------------------------------------
2310Sstevel@tonic-gate  *  SF1_SUNW_FPKNWN  |  SF1_SUNW_FPKNWN  |  SF1_SUNW_FPKNWN  |  SF1_SUNW_FPKNWN
2320Sstevel@tonic-gate  *  SF1_SUNW_FPUSED  |  SF1_SUNW_FPUSED  |                   |  SF1_SUNW_FPUSED
2330Sstevel@tonic-gate  *  ---------------------------------------------------------------------------
2340Sstevel@tonic-gate  *  SF1_SUNW_FPKNWN  |  SF1_SUNW_FPKNWN  |  SF1_SUNW_FPKNWN  |  SF1_SUNW_FPKNWN
2350Sstevel@tonic-gate  *                   |                   |                   |
2360Sstevel@tonic-gate  *  ---------------------------------------------------------------------------
2370Sstevel@tonic-gate  *     <unknown>     |  SF1_SUNW_FPKNWN  |  SF1_SUNW_FPKNWN  |    <unknown>
2380Sstevel@tonic-gate  *                   |  SF1_SUNW_FPUSED  |                   |
2390Sstevel@tonic-gate  */
2400Sstevel@tonic-gate static void
2419878SAli.Bahrami@Sun.COM sf1_cap(Ofl_desc *ofl, Xword val, Ifl_desc *ifl, Is_desc *cisp)
2420Sstevel@tonic-gate {
24311734SAli.Bahrami@Sun.COM #define	FP_FLAGS	(SF1_SUNW_FPKNWN | SF1_SUNW_FPUSED)
24411734SAli.Bahrami@Sun.COM 
2450Sstevel@tonic-gate 	Xword	badval;
2460Sstevel@tonic-gate 
2470Sstevel@tonic-gate 	/*
24811827SRod.Evans@Sun.COM 	 * If a mapfile has established definitions to override any object
24911827SRod.Evans@Sun.COM 	 * capabilities, ignore any new object capabilities.
2500Sstevel@tonic-gate 	 */
25111734SAli.Bahrami@Sun.COM 	if (ofl->ofl_flags1 & FLG_OF1_OVSFCAP1) {
25211827SRod.Evans@Sun.COM 		DBG_CALL(Dbg_cap_val_entry(ofl->ofl_lml, DBG_STATE_IGNORED,
25311734SAli.Bahrami@Sun.COM 		    CA_SUNW_SF_1, val, ld_targ.t_m.m_mach));
2540Sstevel@tonic-gate 		return;
2550Sstevel@tonic-gate 	}
2560Sstevel@tonic-gate 
2577833SRod.Evans@Sun.COM #if	!defined(_ELF64)
25811827SRod.Evans@Sun.COM 	if (ifl && (ifl->ifl_ehdr->e_type == ET_REL)) {
2597833SRod.Evans@Sun.COM 		/*
2607833SRod.Evans@Sun.COM 		 * The SF1_SUNW_ADDR32 is only meaningful when building a 64-bit
2617833SRod.Evans@Sun.COM 		 * object.  Warn the user, and remove the setting, if we're
2627833SRod.Evans@Sun.COM 		 * building a 32-bit object.
2637833SRod.Evans@Sun.COM 		 */
2647833SRod.Evans@Sun.COM 		if (val & SF1_SUNW_ADDR32) {
2657833SRod.Evans@Sun.COM 			eprintf(ofl->ofl_lml, ERR_WARNING,
2669878SAli.Bahrami@Sun.COM 			    MSG_INTL(MSG_FIL_INADDR32SF1), ifl->ifl_name,
2679878SAli.Bahrami@Sun.COM 			    EC_WORD(cisp->is_scnndx), cisp->is_name);
2687833SRod.Evans@Sun.COM 			val &= ~SF1_SUNW_ADDR32;
2697833SRod.Evans@Sun.COM 		}
2707833SRod.Evans@Sun.COM 	}
2717833SRod.Evans@Sun.COM #endif
2720Sstevel@tonic-gate 	/*
2730Sstevel@tonic-gate 	 * If this object doesn't specify any capabilities, ignore it, and
2740Sstevel@tonic-gate 	 * leave the state as is.
2750Sstevel@tonic-gate 	 */
2760Sstevel@tonic-gate 	if (val == 0)
2770Sstevel@tonic-gate 		return;
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate 	/*
2800Sstevel@tonic-gate 	 * Make sure we only accept known software capabilities.  Note, that
2810Sstevel@tonic-gate 	 * an F1_SUNW_FPUSED by itself is viewed as bad practice.
2820Sstevel@tonic-gate 	 */
2830Sstevel@tonic-gate 	if ((badval = (val & ~SF1_SUNW_MASK)) != 0) {
2841618Srie 		eprintf(ofl->ofl_lml, ERR_WARNING, MSG_INTL(MSG_FIL_BADSF1),
2859878SAli.Bahrami@Sun.COM 		    ifl->ifl_name, EC_WORD(cisp->is_scnndx), cisp->is_name,
2869878SAli.Bahrami@Sun.COM 		    EC_XWORD(badval));
2870Sstevel@tonic-gate 		val &= SF1_SUNW_MASK;
2880Sstevel@tonic-gate 	}
28911734SAli.Bahrami@Sun.COM 	if ((val & FP_FLAGS) == SF1_SUNW_FPUSED) {
2901618Srie 		eprintf(ofl->ofl_lml, ERR_WARNING, MSG_INTL(MSG_FIL_BADSF1),
2919878SAli.Bahrami@Sun.COM 		    ifl->ifl_name, EC_WORD(cisp->is_scnndx), cisp->is_name,
2929878SAli.Bahrami@Sun.COM 		    EC_XWORD(val));
2930Sstevel@tonic-gate 		return;
2940Sstevel@tonic-gate 	}
2950Sstevel@tonic-gate 
2967833SRod.Evans@Sun.COM 	/*
2977833SRod.Evans@Sun.COM 	 * If the input file is not a relocatable object, then we're only here
2987833SRod.Evans@Sun.COM 	 * to warn the user of any questionable capabilities.
2997833SRod.Evans@Sun.COM 	 */
3007833SRod.Evans@Sun.COM 	if (ifl->ifl_ehdr->e_type != ET_REL) {
3017833SRod.Evans@Sun.COM #if	defined(_ELF64)
3027833SRod.Evans@Sun.COM 		/*
3037833SRod.Evans@Sun.COM 		 * If we're building a 64-bit executable, and we come across a
3047833SRod.Evans@Sun.COM 		 * dependency that requires a restricted address space, then
3057833SRod.Evans@Sun.COM 		 * that dependencies requirement can only be satisfied if the
3067833SRod.Evans@Sun.COM 		 * executable triggers the restricted address space.  This is a
3077833SRod.Evans@Sun.COM 		 * warning rather than a fatal error, as the possibility exists
3087833SRod.Evans@Sun.COM 		 * that an appropriate dependency will be provided at runtime.
3097833SRod.Evans@Sun.COM 		 * The runtime linker will refuse to use this dependency.
3107833SRod.Evans@Sun.COM 		 */
3117833SRod.Evans@Sun.COM 		if ((val & SF1_SUNW_ADDR32) && (ofl->ofl_flags & FLG_OF_EXEC) &&
31211827SRod.Evans@Sun.COM 		    ((ofl->ofl_ocapset.oc_sf_1.cm_val &
31311734SAli.Bahrami@Sun.COM 		    SF1_SUNW_ADDR32) == 0)) {
3147833SRod.Evans@Sun.COM 			eprintf(ofl->ofl_lml, ERR_WARNING,
3159878SAli.Bahrami@Sun.COM 			    MSG_INTL(MSG_FIL_EXADDR32SF1), ifl->ifl_name,
3169878SAli.Bahrami@Sun.COM 			    EC_WORD(cisp->is_scnndx), cisp->is_name);
3177833SRod.Evans@Sun.COM 		}
3187833SRod.Evans@Sun.COM #endif
3197833SRod.Evans@Sun.COM 		return;
3207833SRod.Evans@Sun.COM 	}
3217833SRod.Evans@Sun.COM 
32211734SAli.Bahrami@Sun.COM 	if (DBG_ENABLED) {
32311827SRod.Evans@Sun.COM 		Dbg_cap_val_entry(ofl->ofl_lml, DBG_STATE_CURRENT, CA_SUNW_SF_1,
32411827SRod.Evans@Sun.COM 		    ofl->ofl_ocapset.oc_sf_1.cm_val, ld_targ.t_m.m_mach);
32511827SRod.Evans@Sun.COM 		Dbg_cap_val_entry(ofl->ofl_lml, DBG_STATE_NEW, CA_SUNW_SF_1,
32611734SAli.Bahrami@Sun.COM 		    val, ld_targ.t_m.m_mach);
32711734SAli.Bahrami@Sun.COM 	}
3280Sstevel@tonic-gate 
3290Sstevel@tonic-gate 	/*
3300Sstevel@tonic-gate 	 * Determine the resolution of the present frame pointer and the
3310Sstevel@tonic-gate 	 * new input relocatable objects frame pointer.
3320Sstevel@tonic-gate 	 */
33311827SRod.Evans@Sun.COM 	if ((ofl->ofl_ocapset.oc_sf_1.cm_val & FP_FLAGS) == FP_FLAGS) {
3340Sstevel@tonic-gate 		/*
3350Sstevel@tonic-gate 		 * If the new relocatable object isn't using a frame pointer,
3360Sstevel@tonic-gate 		 * reduce the present state to unused.
3370Sstevel@tonic-gate 		 */
33811734SAli.Bahrami@Sun.COM 		if ((val & FP_FLAGS) != FP_FLAGS)
33911827SRod.Evans@Sun.COM 			ofl->ofl_ocapset.oc_sf_1.cm_val &= ~SF1_SUNW_FPUSED;
34011734SAli.Bahrami@Sun.COM 
34111734SAli.Bahrami@Sun.COM 		/*
34211734SAli.Bahrami@Sun.COM 		 * Having processed the frame pointer bits, remove them from
34311734SAli.Bahrami@Sun.COM 		 * the value so they don't get OR'd in below.
34411734SAli.Bahrami@Sun.COM 		 */
34511734SAli.Bahrami@Sun.COM 		val &= ~FP_FLAGS;
3460Sstevel@tonic-gate 
34711827SRod.Evans@Sun.COM 	} else if ((ofl->ofl_ocapset.oc_sf_1.cm_val & SF1_SUNW_FPKNWN) == 0) {
3480Sstevel@tonic-gate 		/*
34911734SAli.Bahrami@Sun.COM 		 * If the present frame pointer state is unknown, mask it out
35011734SAli.Bahrami@Sun.COM 		 * and allow the values from the new relocatable object
35111734SAli.Bahrami@Sun.COM 		 * to overwrite them.
3520Sstevel@tonic-gate 		 */
35311827SRod.Evans@Sun.COM 		ofl->ofl_ocapset.oc_sf_1.cm_val &= ~FP_FLAGS;
35411734SAli.Bahrami@Sun.COM 	} else {
35511734SAli.Bahrami@Sun.COM 		/* Do not take the frame pointer flags from the object */
35611734SAli.Bahrami@Sun.COM 		val &= ~FP_FLAGS;
3570Sstevel@tonic-gate 	}
3580Sstevel@tonic-gate 
35911827SRod.Evans@Sun.COM 	ofl->ofl_ocapset.oc_sf_1.cm_val |= val;
36011827SRod.Evans@Sun.COM 
36111827SRod.Evans@Sun.COM 	DBG_CALL(Dbg_cap_val_entry(ofl->ofl_lml, DBG_STATE_RESOLVED,
36211827SRod.Evans@Sun.COM 	    CA_SUNW_SF_1, ofl->ofl_ocapset.oc_sf_1.cm_val, ld_targ.t_m.m_mach));
36311734SAli.Bahrami@Sun.COM 
36411734SAli.Bahrami@Sun.COM #undef FP_FLAGS
3650Sstevel@tonic-gate }
3660Sstevel@tonic-gate 
3670Sstevel@tonic-gate /*
3680Sstevel@tonic-gate  * Determine the hardware capabilities of the object being built from the
3690Sstevel@tonic-gate  * capabilities of the input relocatable objects.  There's really little to
3700Sstevel@tonic-gate  * do here, other than to offer diagnostics, hardware capabilities are simply
3710Sstevel@tonic-gate  * additive.
3720Sstevel@tonic-gate  */
3730Sstevel@tonic-gate static void
37411827SRod.Evans@Sun.COM hw_cap(Ofl_desc *ofl, Xword tag, Xword val)
3750Sstevel@tonic-gate {
37611827SRod.Evans@Sun.COM 	elfcap_mask_t	*hwcap;
37711827SRod.Evans@Sun.COM 	ofl_flag_t	flags1;
37811827SRod.Evans@Sun.COM 
37911827SRod.Evans@Sun.COM 	if (tag == CA_SUNW_HW_1) {
38011827SRod.Evans@Sun.COM 		hwcap = &ofl->ofl_ocapset.oc_hw_1.cm_val;
38111827SRod.Evans@Sun.COM 		flags1 = FLG_OF1_OVHWCAP1;
38211827SRod.Evans@Sun.COM 	} else {
38311827SRod.Evans@Sun.COM 		hwcap = &ofl->ofl_ocapset.oc_hw_2.cm_val;
38411827SRod.Evans@Sun.COM 		flags1 = FLG_OF1_OVHWCAP2;
38511827SRod.Evans@Sun.COM 	}
38611827SRod.Evans@Sun.COM 
3870Sstevel@tonic-gate 	/*
38811827SRod.Evans@Sun.COM 	 * If a mapfile has established definitions to override any object
38911827SRod.Evans@Sun.COM 	 * capabilities, ignore any new object capabilities.
3900Sstevel@tonic-gate 	 */
39111827SRod.Evans@Sun.COM 	if (ofl->ofl_flags1 & flags1) {
39211827SRod.Evans@Sun.COM 		DBG_CALL(Dbg_cap_val_entry(ofl->ofl_lml, DBG_STATE_IGNORED,
39311827SRod.Evans@Sun.COM 		    tag, val, ld_targ.t_m.m_mach));
3940Sstevel@tonic-gate 		return;
3950Sstevel@tonic-gate 	}
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate 	/*
3980Sstevel@tonic-gate 	 * If this object doesn't specify any capabilities, ignore it, and
3990Sstevel@tonic-gate 	 * leave the state as is.
4000Sstevel@tonic-gate 	 */
4010Sstevel@tonic-gate 	if (val == 0)
4020Sstevel@tonic-gate 		return;
4030Sstevel@tonic-gate 
40411734SAli.Bahrami@Sun.COM 	if (DBG_ENABLED) {
40511827SRod.Evans@Sun.COM 		Dbg_cap_val_entry(ofl->ofl_lml, DBG_STATE_CURRENT, CA_SUNW_HW_1,
40611827SRod.Evans@Sun.COM 		    ofl->ofl_ocapset.oc_hw_1.cm_val, ld_targ.t_m.m_mach);
40711827SRod.Evans@Sun.COM 		Dbg_cap_val_entry(ofl->ofl_lml, DBG_STATE_NEW, CA_SUNW_HW_1,
40811827SRod.Evans@Sun.COM 		    val, ld_targ.t_m.m_mach);
40911827SRod.Evans@Sun.COM 	}
41011827SRod.Evans@Sun.COM 
41111827SRod.Evans@Sun.COM 	*hwcap |= val;
41211827SRod.Evans@Sun.COM 
41311827SRod.Evans@Sun.COM 	DBG_CALL(Dbg_cap_val_entry(ofl->ofl_lml, DBG_STATE_RESOLVED, tag,
41411827SRod.Evans@Sun.COM 	    *hwcap, ld_targ.t_m.m_mach));
41511827SRod.Evans@Sun.COM }
41611827SRod.Evans@Sun.COM 
41711827SRod.Evans@Sun.COM /*
41811827SRod.Evans@Sun.COM  * Promote a machine capability or platform capability to the output file.
41911827SRod.Evans@Sun.COM  * Multiple instances of these names can be defined.
42011827SRod.Evans@Sun.COM  */
42111827SRod.Evans@Sun.COM static void
42211827SRod.Evans@Sun.COM str_cap(Ofl_desc *ofl, char *pstr, ofl_flag_t flags, Xword tag, Caplist *list)
42311827SRod.Evans@Sun.COM {
42411827SRod.Evans@Sun.COM 	Capstr		*capstr;
42511827SRod.Evans@Sun.COM 	Aliste		idx;
42611827SRod.Evans@Sun.COM 	Boolean		found = FALSE;
42711827SRod.Evans@Sun.COM 
42811827SRod.Evans@Sun.COM 	/*
42911827SRod.Evans@Sun.COM 	 * If a mapfile has established definitions to override this capability,
43011827SRod.Evans@Sun.COM 	 * ignore any new capability.
43111827SRod.Evans@Sun.COM 	 */
43211827SRod.Evans@Sun.COM 	if (ofl->ofl_flags1 & flags) {
43311827SRod.Evans@Sun.COM 		DBG_CALL(Dbg_cap_ptr_entry(ofl->ofl_lml, DBG_STATE_IGNORED,
43411827SRod.Evans@Sun.COM 		    tag, pstr));
43511827SRod.Evans@Sun.COM 		return;
43611827SRod.Evans@Sun.COM 	}
43711827SRod.Evans@Sun.COM 
43811827SRod.Evans@Sun.COM 	for (ALIST_TRAVERSE(list->cl_val, idx, capstr)) {
43911827SRod.Evans@Sun.COM 		DBG_CALL(Dbg_cap_ptr_entry(ofl->ofl_lml,
44011827SRod.Evans@Sun.COM 		    DBG_STATE_CURRENT, tag, capstr->cs_str));
44111827SRod.Evans@Sun.COM 		if (strcmp(capstr->cs_str, pstr) == 0)
44211827SRod.Evans@Sun.COM 			found = TRUE;
44311827SRod.Evans@Sun.COM 	}
44411827SRod.Evans@Sun.COM 
44511827SRod.Evans@Sun.COM 	DBG_CALL(Dbg_cap_ptr_entry(ofl->ofl_lml, DBG_STATE_NEW, tag, pstr));
44611827SRod.Evans@Sun.COM 
44711827SRod.Evans@Sun.COM 	if (found == FALSE) {
44811827SRod.Evans@Sun.COM 		if ((capstr = alist_append(&list->cl_val, NULL,
44911827SRod.Evans@Sun.COM 		    sizeof (Capstr), AL_CNT_CAP_NAMES)) == NULL) {
45011827SRod.Evans@Sun.COM 			ofl->ofl_flags |= FLG_OF_FATAL;
45111827SRod.Evans@Sun.COM 			return;
45211827SRod.Evans@Sun.COM 		}
45311827SRod.Evans@Sun.COM 		capstr->cs_str = pstr;
45411827SRod.Evans@Sun.COM 	}
45511827SRod.Evans@Sun.COM 
45611827SRod.Evans@Sun.COM 	if (DBG_ENABLED) {
45711827SRod.Evans@Sun.COM 		for (ALIST_TRAVERSE(list->cl_val, idx, capstr)) {
45811827SRod.Evans@Sun.COM 			DBG_CALL(Dbg_cap_ptr_entry(ofl->ofl_lml,
45911827SRod.Evans@Sun.COM 			    DBG_STATE_RESOLVED, tag, capstr->cs_str));
46011827SRod.Evans@Sun.COM 		}
46111734SAli.Bahrami@Sun.COM 	}
46211827SRod.Evans@Sun.COM }
46311827SRod.Evans@Sun.COM 
46411827SRod.Evans@Sun.COM /*
46511827SRod.Evans@Sun.COM  * Promote a capability identifier to the output file.  A capability group can
46611827SRod.Evans@Sun.COM  * only have one identifier, and thus only the first identifier seen from any
46711827SRod.Evans@Sun.COM  * input relocatable objects is retained.  An explicit user defined identifier,
46811827SRod.Evans@Sun.COM  * rather than an an identifier fabricated by ld(1) with -z symbcap processing,
46911827SRod.Evans@Sun.COM  * takes precedence.  Note, a user may have defined an identifier via a mapfile,
47011827SRod.Evans@Sun.COM  * in which case the mapfile identifier is retained.
47111827SRod.Evans@Sun.COM  */
47211827SRod.Evans@Sun.COM static void
47311827SRod.Evans@Sun.COM id_cap(Ofl_desc *ofl, char *pstr, oc_flag_t flags)
47411827SRod.Evans@Sun.COM {
47511827SRod.Evans@Sun.COM 	Objcapset	*ocapset = &ofl->ofl_ocapset;
47611827SRod.Evans@Sun.COM 
47711827SRod.Evans@Sun.COM 	if (ocapset->oc_id.cs_str) {
47811827SRod.Evans@Sun.COM 		DBG_CALL(Dbg_cap_ptr_entry(ofl->ofl_lml, DBG_STATE_CURRENT,
47911827SRod.Evans@Sun.COM 		    CA_SUNW_ID, ocapset->oc_id.cs_str));
48011827SRod.Evans@Sun.COM 
48111827SRod.Evans@Sun.COM 		if ((ocapset->oc_flags & FLG_OCS_USRDEFID) ||
48211827SRod.Evans@Sun.COM 		    ((flags & FLG_OCS_USRDEFID) == 0)) {
48311827SRod.Evans@Sun.COM 			DBG_CALL(Dbg_cap_ptr_entry(ofl->ofl_lml,
48411827SRod.Evans@Sun.COM 			    DBG_STATE_IGNORED, CA_SUNW_ID, pstr));
48511827SRod.Evans@Sun.COM 			return;
48611827SRod.Evans@Sun.COM 		}
48711827SRod.Evans@Sun.COM 	}
48811827SRod.Evans@Sun.COM 
48911827SRod.Evans@Sun.COM 	DBG_CALL(Dbg_cap_ptr_entry(ofl->ofl_lml, DBG_STATE_NEW,
49011827SRod.Evans@Sun.COM 	    CA_SUNW_ID, pstr));
49111827SRod.Evans@Sun.COM 
49211827SRod.Evans@Sun.COM 	ocapset->oc_id.cs_str = pstr;
49311827SRod.Evans@Sun.COM 	ocapset->oc_flags |= flags;
49411827SRod.Evans@Sun.COM 
49511827SRod.Evans@Sun.COM 	DBG_CALL(Dbg_cap_ptr_entry(ofl->ofl_lml, DBG_STATE_RESOLVED,
49611827SRod.Evans@Sun.COM 	    CA_SUNW_ID, pstr));
49711827SRod.Evans@Sun.COM }
49811827SRod.Evans@Sun.COM 
49911827SRod.Evans@Sun.COM /*
50011827SRod.Evans@Sun.COM  * Promote a capabilities group to the object capabilities.  This catches a
50111827SRod.Evans@Sun.COM  * corner case.  An object capabilities file can be converted to symbol
50211827SRod.Evans@Sun.COM  * capabilities with -z symbolcap.  However, if the user has indicated that all
50311827SRod.Evans@Sun.COM  * the symbols should be demoted, we'd be left with a symbol capabilities file,
50411827SRod.Evans@Sun.COM  * with no associated symbols.  Catch this case by promoting the symbol
50511827SRod.Evans@Sun.COM  * capabilities back to object capabilities.
50611827SRod.Evans@Sun.COM  */
50711827SRod.Evans@Sun.COM void
50811827SRod.Evans@Sun.COM ld_cap_move_symtoobj(Ofl_desc *ofl)
50911827SRod.Evans@Sun.COM {
51011827SRod.Evans@Sun.COM 	Cap_group	*cgp;
51111827SRod.Evans@Sun.COM 	Aliste		idx1;
51211827SRod.Evans@Sun.COM 
51311827SRod.Evans@Sun.COM 	for (APLIST_TRAVERSE(ofl->ofl_capgroups, idx1, cgp)) {
51411827SRod.Evans@Sun.COM 		Objcapset	*scapset = &cgp->cg_set;
51511827SRod.Evans@Sun.COM 		Capstr		*capstr;
51611827SRod.Evans@Sun.COM 		Aliste		idx2;
51711827SRod.Evans@Sun.COM 
51811827SRod.Evans@Sun.COM 		if (scapset->oc_id.cs_str) {
51911827SRod.Evans@Sun.COM 			if (scapset->oc_flags & FLG_OCS_USRDEFID)
52011827SRod.Evans@Sun.COM 				id_cap(ofl, scapset->oc_id.cs_str,
52111827SRod.Evans@Sun.COM 				    scapset->oc_flags);
52211827SRod.Evans@Sun.COM 		}
52311827SRod.Evans@Sun.COM 		if (scapset->oc_plat.cl_val) {
52411827SRod.Evans@Sun.COM 			for (ALIST_TRAVERSE(scapset->oc_plat.cl_val, idx2,
52511827SRod.Evans@Sun.COM 			    capstr)) {
52611827SRod.Evans@Sun.COM 				str_cap(ofl, capstr->cs_str, FLG_OF1_OVPLATCAP,
52711827SRod.Evans@Sun.COM 				    CA_SUNW_PLAT, &ofl->ofl_ocapset.oc_plat);
52811827SRod.Evans@Sun.COM 			}
52911827SRod.Evans@Sun.COM 		}
53011827SRod.Evans@Sun.COM 		if (scapset->oc_mach.cl_val) {
53111827SRod.Evans@Sun.COM 			for (ALIST_TRAVERSE(scapset->oc_mach.cl_val, idx2,
53211827SRod.Evans@Sun.COM 			    capstr)) {
53311827SRod.Evans@Sun.COM 				str_cap(ofl, capstr->cs_str, FLG_OF1_OVMACHCAP,
53411827SRod.Evans@Sun.COM 				    CA_SUNW_MACH, &ofl->ofl_ocapset.oc_mach);
53511827SRod.Evans@Sun.COM 			}
53611827SRod.Evans@Sun.COM 		}
53711827SRod.Evans@Sun.COM 		if (scapset->oc_hw_2.cm_val)
53811827SRod.Evans@Sun.COM 			hw_cap(ofl, CA_SUNW_HW_2, scapset->oc_hw_2.cm_val);
53911827SRod.Evans@Sun.COM 
54011827SRod.Evans@Sun.COM 		if (scapset->oc_hw_1.cm_val)
54111827SRod.Evans@Sun.COM 			hw_cap(ofl, CA_SUNW_HW_1, scapset->oc_hw_1.cm_val);
54211827SRod.Evans@Sun.COM 
54311827SRod.Evans@Sun.COM 		if (scapset->oc_sf_1.cm_val)
54411827SRod.Evans@Sun.COM 			sf1_cap(ofl, scapset->oc_sf_1.cm_val, NULL, NULL);
54511827SRod.Evans@Sun.COM 	}
5460Sstevel@tonic-gate }
5470Sstevel@tonic-gate 
5480Sstevel@tonic-gate /*
54911827SRod.Evans@Sun.COM  * Determine whether a capabilities group already exists that describes this
55011827SRod.Evans@Sun.COM  * new capabilities group.
55111827SRod.Evans@Sun.COM  *
55211827SRod.Evans@Sun.COM  * Note, a capability group identifier, CA_SUNW_ID, isn't used as part of the
55311827SRod.Evans@Sun.COM  * comparison.  This attribute simply assigns a diagnostic name to the group,
55411827SRod.Evans@Sun.COM  * and in the case of multiple identifiers, the first will be taken.
5550Sstevel@tonic-gate  */
55611827SRod.Evans@Sun.COM static Cap_group *
55711827SRod.Evans@Sun.COM get_cap_group(Objcapset *ocapset, Word cnum, Ofl_desc *ofl, Is_desc *isp)
5580Sstevel@tonic-gate {
55911827SRod.Evans@Sun.COM 	Aliste		idx;
56011827SRod.Evans@Sun.COM 	Cap_group	*cgp;
56111827SRod.Evans@Sun.COM 	Word		ccnum = cnum;
56211827SRod.Evans@Sun.COM 
56311827SRod.Evans@Sun.COM 	/*
56411827SRod.Evans@Sun.COM 	 * If the new capabilities contains a CA_SUNW_ID, drop the count of the
56511827SRod.Evans@Sun.COM 	 * number of comparable items.
56611827SRod.Evans@Sun.COM 	 */
56711827SRod.Evans@Sun.COM 	if (ocapset->oc_id.cs_str)
56811827SRod.Evans@Sun.COM 		ccnum--;
56911827SRod.Evans@Sun.COM 
57011827SRod.Evans@Sun.COM 	/*
57111827SRod.Evans@Sun.COM 	 * Traverse the existing symbols capabilities groups.
57211827SRod.Evans@Sun.COM 	 */
57311827SRod.Evans@Sun.COM 	for (APLIST_TRAVERSE(ofl->ofl_capgroups, idx, cgp)) {
57411827SRod.Evans@Sun.COM 		Word	onum = cgp->cg_num;
57511827SRod.Evans@Sun.COM 		Alist	*calp, *oalp;
57611827SRod.Evans@Sun.COM 
57711827SRod.Evans@Sun.COM 		if (cgp->cg_set.oc_id.cs_str)
57811827SRod.Evans@Sun.COM 			onum--;
57911827SRod.Evans@Sun.COM 
58011827SRod.Evans@Sun.COM 		if (onum != ccnum)
58111827SRod.Evans@Sun.COM 			continue;
58211827SRod.Evans@Sun.COM 
58311827SRod.Evans@Sun.COM 		if (cgp->cg_set.oc_hw_1.cm_val != ocapset->oc_hw_1.cm_val)
58411827SRod.Evans@Sun.COM 			continue;
58511827SRod.Evans@Sun.COM 		if (cgp->cg_set.oc_sf_1.cm_val != ocapset->oc_sf_1.cm_val)
58611827SRod.Evans@Sun.COM 			continue;
58711827SRod.Evans@Sun.COM 		if (cgp->cg_set.oc_hw_2.cm_val != ocapset->oc_hw_2.cm_val)
58811827SRod.Evans@Sun.COM 			continue;
58911827SRod.Evans@Sun.COM 
59011827SRod.Evans@Sun.COM 		calp = cgp->cg_set.oc_plat.cl_val;
59111827SRod.Evans@Sun.COM 		oalp = ocapset->oc_plat.cl_val;
59211827SRod.Evans@Sun.COM 		if ((calp == NULL) && oalp)
59311827SRod.Evans@Sun.COM 			continue;
59411827SRod.Evans@Sun.COM 		if (calp && ((oalp == NULL) || cap_names_match(calp, oalp)))
59511827SRod.Evans@Sun.COM 			continue;
59611827SRod.Evans@Sun.COM 
59711827SRod.Evans@Sun.COM 		calp = cgp->cg_set.oc_mach.cl_val;
59811827SRod.Evans@Sun.COM 		oalp = ocapset->oc_mach.cl_val;
59911827SRod.Evans@Sun.COM 		if ((calp == NULL) && oalp)
60011827SRod.Evans@Sun.COM 			continue;
60111827SRod.Evans@Sun.COM 		if (calp && ((oalp == NULL) || cap_names_match(calp, oalp)))
60211827SRod.Evans@Sun.COM 			continue;
60311827SRod.Evans@Sun.COM 
60411827SRod.Evans@Sun.COM 		/*
60511827SRod.Evans@Sun.COM 		 * If a matching group is found, then this new group has
60611827SRod.Evans@Sun.COM 		 * already been supplied by a previous file, and hence the
60711827SRod.Evans@Sun.COM 		 * existing group can be used.  Record this new input section,
60811827SRod.Evans@Sun.COM 		 * from which we can also derive the input file name, on the
60911827SRod.Evans@Sun.COM 		 * existing groups input sections.
61011827SRod.Evans@Sun.COM 		 */
61111827SRod.Evans@Sun.COM 		if (aplist_append(&(cgp->cg_secs), isp,
61211827SRod.Evans@Sun.COM 		    AL_CNT_CAP_SECS) == NULL)
61311827SRod.Evans@Sun.COM 			return (NULL);
61411827SRod.Evans@Sun.COM 		return (cgp);
61511827SRod.Evans@Sun.COM 	}
61611827SRod.Evans@Sun.COM 
61711827SRod.Evans@Sun.COM 	/*
61811827SRod.Evans@Sun.COM 	 * If a capabilities group is not found, create a new one.
61911827SRod.Evans@Sun.COM 	 */
62011827SRod.Evans@Sun.COM 	if (((cgp = libld_calloc(sizeof (Cap_group), 1)) == NULL) ||
62111827SRod.Evans@Sun.COM 	    (aplist_append(&(ofl->ofl_capgroups), cgp,
62211827SRod.Evans@Sun.COM 	    AL_CNT_CAP_DESCS) == NULL))
62311827SRod.Evans@Sun.COM 		return (NULL);
62411827SRod.Evans@Sun.COM 
62511827SRod.Evans@Sun.COM 	/*
62611827SRod.Evans@Sun.COM 	 * If we're converting object capabilities to symbol capabilities and
62711827SRod.Evans@Sun.COM 	 * no CA_SUNW_ID is defined, fabricate one.  This identifier is appended
62811827SRod.Evans@Sun.COM 	 * to all symbol names that are converted into capabilities symbols,
62911827SRod.Evans@Sun.COM 	 * see ld_sym_process().
63011827SRod.Evans@Sun.COM 	 */
63111827SRod.Evans@Sun.COM 	if ((isp->is_file->ifl_flags & FLG_IF_OTOSCAP) &&
63211827SRod.Evans@Sun.COM 	    (ocapset->oc_id.cs_str == NULL)) {
63311827SRod.Evans@Sun.COM 		size_t	len;
63411827SRod.Evans@Sun.COM 
63511827SRod.Evans@Sun.COM 		/*
63611827SRod.Evans@Sun.COM 		 * Create an identifier using the group number together with a
63711827SRod.Evans@Sun.COM 		 * default template.  We allocate a buffer large enough for any
63811827SRod.Evans@Sun.COM 		 * possible number of items (way more than we need).
63911827SRod.Evans@Sun.COM 		 */
64011827SRod.Evans@Sun.COM 		len = MSG_STR_CAPGROUPID_SIZE + CONV_INV_BUFSIZE;
64111827SRod.Evans@Sun.COM 		if ((ocapset->oc_id.cs_str = libld_malloc(len)) == NULL)
64211827SRod.Evans@Sun.COM 			return (NULL);
64311827SRod.Evans@Sun.COM 
64411827SRod.Evans@Sun.COM 		(void) snprintf(ocapset->oc_id.cs_str, len,
64511827SRod.Evans@Sun.COM 		    MSG_ORIG(MSG_STR_CAPGROUPID),
64611827SRod.Evans@Sun.COM 		    aplist_nitems(ofl->ofl_capgroups));
64711827SRod.Evans@Sun.COM 		cnum++;
64811827SRod.Evans@Sun.COM 	}
64911827SRod.Evans@Sun.COM 
65011827SRod.Evans@Sun.COM 	cgp->cg_set = *ocapset;
65111827SRod.Evans@Sun.COM 	cgp->cg_num = cnum;
65211827SRod.Evans@Sun.COM 
65311827SRod.Evans@Sun.COM 	/*
65411827SRod.Evans@Sun.COM 	 * Null the callers alist's as they've effectively been transferred
65511827SRod.Evans@Sun.COM 	 * to this new Cap_group.
65611827SRod.Evans@Sun.COM 	 */
65711827SRod.Evans@Sun.COM 	ocapset->oc_plat.cl_val = ocapset->oc_mach.cl_val = NULL;
65811827SRod.Evans@Sun.COM 
65911827SRod.Evans@Sun.COM 	/*
66011827SRod.Evans@Sun.COM 	 * Keep track of which input section, and hence input file, established
66111827SRod.Evans@Sun.COM 	 * this group.
66211827SRod.Evans@Sun.COM 	 */
66311827SRod.Evans@Sun.COM 	if (aplist_append(&(cgp->cg_secs), isp, AL_CNT_CAP_SECS) == NULL)
66411827SRod.Evans@Sun.COM 		return (NULL);
66511827SRod.Evans@Sun.COM 
66611827SRod.Evans@Sun.COM 	/*
66711827SRod.Evans@Sun.COM 	 * Keep track of the number of symbol capabilities entries that will be
66811827SRod.Evans@Sun.COM 	 * required in the output file.  Each group requires a terminating
66911827SRod.Evans@Sun.COM 	 * CA_SUNW_NULL.
67011827SRod.Evans@Sun.COM 	 */
67111827SRod.Evans@Sun.COM 	ofl->ofl_capsymcnt += (cnum + 1);
67211827SRod.Evans@Sun.COM 	return (cgp);
67311827SRod.Evans@Sun.COM }
67411827SRod.Evans@Sun.COM 
67511827SRod.Evans@Sun.COM /*
67611827SRod.Evans@Sun.COM  * Capture symbol capability family information.  This data structure is focal
67711827SRod.Evans@Sun.COM  * in maintaining all symbol capability relationships, and provides for the
67811827SRod.Evans@Sun.COM  * eventual creation of a capabilities information section, and possibly a
67911827SRod.Evans@Sun.COM  * capabilities chain section.
68011827SRod.Evans@Sun.COM  *
68111827SRod.Evans@Sun.COM  * Capabilities families are lead by a CAPINFO_SUNW_GLOB symbol.  This symbol
68211827SRod.Evans@Sun.COM  * provides the visible global symbol that is referenced by all external
68311827SRod.Evans@Sun.COM  * callers.  This symbol may have aliases.  For example, a weak/global symbol
68411827SRod.Evans@Sun.COM  * pair, such as memcpy()/_memcpy() may lead the same capabilities family.
68511827SRod.Evans@Sun.COM  * Each family contains one or more local symbol members.  These members provide
68611827SRod.Evans@Sun.COM  * the capabilities specific functions, and are associated to a capabilities
68711827SRod.Evans@Sun.COM  * group.  For example, the capability members memcpy%sun4u and memcpy%sun4v
68811827SRod.Evans@Sun.COM  * might be associated with the memcpy() capability family.
68911827SRod.Evans@Sun.COM  *
69011827SRod.Evans@Sun.COM  * This routine is called when a relocatable object that provides object
69111827SRod.Evans@Sun.COM  * capabilities is transformed into a symbol capabilities object, using the
69211827SRod.Evans@Sun.COM  * -z symbolcap option.
69311827SRod.Evans@Sun.COM  *
69411827SRod.Evans@Sun.COM  * This routine is also called to collect the SUNW_capinfo section information
69511827SRod.Evans@Sun.COM  * of a relocatable object that contains symbol capability definitions.
69611827SRod.Evans@Sun.COM  */
69711827SRod.Evans@Sun.COM uintptr_t
69811827SRod.Evans@Sun.COM ld_cap_add_family(Ofl_desc *ofl, Sym_desc *lsdp, Sym_desc *csdp, Cap_group *cgp,
69911827SRod.Evans@Sun.COM     APlist **csyms)
70011827SRod.Evans@Sun.COM {
70111827SRod.Evans@Sun.COM 	Cap_avlnode	qcav, *cav;
70211827SRod.Evans@Sun.COM 	avl_tree_t	*avlt;
70311827SRod.Evans@Sun.COM 	avl_index_t	where = 0;
70411827SRod.Evans@Sun.COM 	Cap_sym		*mcsp;
70511827SRod.Evans@Sun.COM 	Aliste		idx;
70611827SRod.Evans@Sun.COM 
70711827SRod.Evans@Sun.COM 	/*
70811827SRod.Evans@Sun.COM 	 * Make sure the capability families have an initialized AVL tree.
70911827SRod.Evans@Sun.COM 	 */
71011827SRod.Evans@Sun.COM 	if ((avlt = ofl->ofl_capfamilies) == NULL) {
71111827SRod.Evans@Sun.COM 		if ((avlt = libld_calloc(sizeof (avl_tree_t), 1)) == NULL)
71211827SRod.Evans@Sun.COM 			return (S_ERROR);
71311827SRod.Evans@Sun.COM 		avl_create(avlt, &ld_sym_avl_comp, sizeof (Cap_avlnode),
71411827SRod.Evans@Sun.COM 		    SGSOFFSETOF(Cap_avlnode, cn_symavlnode.sav_node));
71511827SRod.Evans@Sun.COM 		ofl->ofl_capfamilies = avlt;
71611827SRod.Evans@Sun.COM 
71711827SRod.Evans@Sun.COM 		/*
71811827SRod.Evans@Sun.COM 		 * When creating a dynamic object, capability family members
71911827SRod.Evans@Sun.COM 		 * are maintained in a .SUNW_capchain, the first entry of
72011827SRod.Evans@Sun.COM 		 * which is the version number of the chain.
72111827SRod.Evans@Sun.COM 		 */
72211827SRod.Evans@Sun.COM 		ofl->ofl_capchaincnt = 1;
72311827SRod.Evans@Sun.COM 	}
72411827SRod.Evans@Sun.COM 
72511827SRod.Evans@Sun.COM 	/*
72611827SRod.Evans@Sun.COM 	 * Determine whether a family already exists, and if not, create one
72711827SRod.Evans@Sun.COM 	 * using the lead family symbol.
72811827SRod.Evans@Sun.COM 	 */
72911827SRod.Evans@Sun.COM 	qcav.cn_symavlnode.sav_hash = (Word)elf_hash(lsdp->sd_name);
73011827SRod.Evans@Sun.COM 	qcav.cn_symavlnode.sav_name = lsdp->sd_name;
73111827SRod.Evans@Sun.COM 
73211827SRod.Evans@Sun.COM 	if ((cav = avl_find(avlt, &qcav, &where)) == NULL) {
73311827SRod.Evans@Sun.COM 		if ((cav = libld_calloc(sizeof (Cap_avlnode), 1)) == NULL)
73411827SRod.Evans@Sun.COM 			return (S_ERROR);
73511827SRod.Evans@Sun.COM 		cav->cn_symavlnode.sav_hash = qcav.cn_symavlnode.sav_hash;
73611827SRod.Evans@Sun.COM 		cav->cn_symavlnode.sav_name = qcav.cn_symavlnode.sav_name;
73711827SRod.Evans@Sun.COM 		cav->cn_symavlnode.sav_sdp = lsdp;
73811827SRod.Evans@Sun.COM 
73911827SRod.Evans@Sun.COM 		avl_insert(avlt, cav, where);
74011827SRod.Evans@Sun.COM 
74111827SRod.Evans@Sun.COM 		/*
74211827SRod.Evans@Sun.COM 		 * When creating a dynamic object, capability family members
74311827SRod.Evans@Sun.COM 		 * are maintained in a .SUNW_capchain, each family starts with
74411827SRod.Evans@Sun.COM 		 * this lead symbol, and is terminated with a 0 element.
74511827SRod.Evans@Sun.COM 		 */
74611827SRod.Evans@Sun.COM 		ofl->ofl_capchaincnt += 2;
74711827SRod.Evans@Sun.COM 	}
74811827SRod.Evans@Sun.COM 
74911827SRod.Evans@Sun.COM 	/*
75011827SRod.Evans@Sun.COM 	 * If no group information is provided then this request is to add a
75111827SRod.Evans@Sun.COM 	 * lead capability symbol, or lead symbol alias.  If this is the lead
75211827SRod.Evans@Sun.COM 	 * symbol there's nothing more to do.  Otherwise save the alias.
75311827SRod.Evans@Sun.COM 	 */
75411827SRod.Evans@Sun.COM 	if (cgp == NULL) {
75511827SRod.Evans@Sun.COM 		if ((lsdp != csdp) && (aplist_append(&cav->cn_aliases, csdp,
75611827SRod.Evans@Sun.COM 		    AL_CNT_CAP_ALIASES) == NULL))
75711827SRod.Evans@Sun.COM 			return (S_ERROR);
75811827SRod.Evans@Sun.COM 
75911827SRod.Evans@Sun.COM 		return (0);
76011827SRod.Evans@Sun.COM 	}
76111827SRod.Evans@Sun.COM 
76211827SRod.Evans@Sun.COM 	/*
76311827SRod.Evans@Sun.COM 	 * Determine whether a member of the same group as this new member is
76411827SRod.Evans@Sun.COM 	 * already defined within this family.  If so, we have a multiply
76511827SRod.Evans@Sun.COM 	 * defined symbol.
76611827SRod.Evans@Sun.COM 	 */
76711827SRod.Evans@Sun.COM 	for (APLIST_TRAVERSE(cav->cn_members, idx, mcsp)) {
76811827SRod.Evans@Sun.COM 		Sym_desc	*msdp;
76911827SRod.Evans@Sun.COM 
77011827SRod.Evans@Sun.COM 		if (cgp != mcsp->cs_group)
77111827SRod.Evans@Sun.COM 			continue;
77211827SRod.Evans@Sun.COM 
77311827SRod.Evans@Sun.COM 		/*
77411827SRod.Evans@Sun.COM 		 * Diagnose that a multiple symbol definition exists.
77511827SRod.Evans@Sun.COM 		 */
77611827SRod.Evans@Sun.COM 		msdp = mcsp->cs_sdp;
77711827SRod.Evans@Sun.COM 
77811827SRod.Evans@Sun.COM 		eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_CAP_MULDEF),
77911827SRod.Evans@Sun.COM 		    demangle(lsdp->sd_name));
78011827SRod.Evans@Sun.COM 		eprintf(ofl->ofl_lml, ERR_NONE, MSG_INTL(MSG_CAP_MULDEFSYMS),
78111827SRod.Evans@Sun.COM 		    msdp->sd_file->ifl_name, msdp->sd_name,
78211827SRod.Evans@Sun.COM 		    csdp->sd_file->ifl_name, csdp->sd_name);
78311827SRod.Evans@Sun.COM 		ofl->ofl_flags |= FLG_OF_FATAL;
78411827SRod.Evans@Sun.COM 	}
78511827SRod.Evans@Sun.COM 
78611827SRod.Evans@Sun.COM 	/*
78711827SRod.Evans@Sun.COM 	 * Add this capabilities symbol member to the family.
78811827SRod.Evans@Sun.COM 	 */
78911827SRod.Evans@Sun.COM 	if (((mcsp = libld_malloc(sizeof (Cap_sym))) == NULL) ||
79011827SRod.Evans@Sun.COM 	    (aplist_append(&cav->cn_members, mcsp, AL_CNT_CAP_MEMS) == NULL))
79111827SRod.Evans@Sun.COM 		return (S_ERROR);
79211827SRod.Evans@Sun.COM 
79311827SRod.Evans@Sun.COM 	mcsp->cs_sdp = csdp;
79411827SRod.Evans@Sun.COM 	mcsp->cs_group = cgp;
79511827SRod.Evans@Sun.COM 
79611827SRod.Evans@Sun.COM 	/*
79711827SRod.Evans@Sun.COM 	 * When creating a dynamic object, capability family members are
79811827SRod.Evans@Sun.COM 	 * maintained in a .SUNW_capchain.  Account for this family member.
79911827SRod.Evans@Sun.COM 	 */
80011827SRod.Evans@Sun.COM 	ofl->ofl_capchaincnt++;
80111827SRod.Evans@Sun.COM 
80211827SRod.Evans@Sun.COM 	/*
80311827SRod.Evans@Sun.COM 	 * If this input file is undergoing object capabilities to symbol
80411827SRod.Evans@Sun.COM 	 * capabilities conversion, then this member is a new local symbol
80511827SRod.Evans@Sun.COM 	 * that has been generated from an original global symbol.  Keep track
80611827SRod.Evans@Sun.COM 	 * of this symbol so that the output file symbol table can be populated
80711827SRod.Evans@Sun.COM 	 * with these new symbol entries.
80811827SRod.Evans@Sun.COM 	 */
80911827SRod.Evans@Sun.COM 	if (csyms && (aplist_append(csyms, mcsp, AL_CNT_CAP_SYMS) == NULL))
81011827SRod.Evans@Sun.COM 		return (S_ERROR);
81111827SRod.Evans@Sun.COM 
81211827SRod.Evans@Sun.COM 	return (0);
81311827SRod.Evans@Sun.COM }
81411827SRod.Evans@Sun.COM 
81511827SRod.Evans@Sun.COM /*
81611827SRod.Evans@Sun.COM  * Process a SHT_SUNW_cap capabilities section.
81711827SRod.Evans@Sun.COM  */
81811827SRod.Evans@Sun.COM static uintptr_t
81911827SRod.Evans@Sun.COM process_cap(Ofl_desc *ofl, Ifl_desc *ifl, Is_desc *cisp)
82011827SRod.Evans@Sun.COM {
82111827SRod.Evans@Sun.COM 	Objcapset	ocapset = { 0 };
82211827SRod.Evans@Sun.COM 	Cap_desc	*cdp;
82311827SRod.Evans@Sun.COM 	Cap		*data, *cdata;
82411827SRod.Evans@Sun.COM 	char		*strs;
82511827SRod.Evans@Sun.COM 	Word		ndx, cnum;
82611827SRod.Evans@Sun.COM 	int		objcapndx, descapndx, symcapndx;
82711827SRod.Evans@Sun.COM 	int		nulls, capstrs = 0;
82811827SRod.Evans@Sun.COM 
82911827SRod.Evans@Sun.COM 	/*
83011827SRod.Evans@Sun.COM 	 * Determine the capabilities data and size.
83111827SRod.Evans@Sun.COM 	 */
83211827SRod.Evans@Sun.COM 	cdata = (Cap *)cisp->is_indata->d_buf;
83311827SRod.Evans@Sun.COM 	cnum = (Word)(cisp->is_shdr->sh_size / cisp->is_shdr->sh_entsize);
83411827SRod.Evans@Sun.COM 
83511827SRod.Evans@Sun.COM 	if ((cdata == NULL) || (cnum == 0))
83611827SRod.Evans@Sun.COM 		return (0);
8370Sstevel@tonic-gate 
8388501SRod.Evans@Sun.COM 	DBG_CALL(Dbg_cap_sec_title(ofl->ofl_lml, ifl->ifl_name));
8390Sstevel@tonic-gate 
8403617Srie 	/*
84111827SRod.Evans@Sun.COM 	 * Traverse the section to determine what capabilities groups are
84211827SRod.Evans@Sun.COM 	 * available.
84311827SRod.Evans@Sun.COM 	 *
84411827SRod.Evans@Sun.COM 	 * A capabilities section can contain one or more, CA_SUNW_NULL
84511827SRod.Evans@Sun.COM 	 * terminated groups.
84611827SRod.Evans@Sun.COM 	 *
84711827SRod.Evans@Sun.COM 	 *  -	The first group defines the object capabilities.
84811827SRod.Evans@Sun.COM 	 *  -	Additional groups define symbol capabilities.
84911827SRod.Evans@Sun.COM 	 *  -	Since the initial group is always reserved for object
85011827SRod.Evans@Sun.COM 	 *	capabilities, any object with symbol capabilities must also
85111827SRod.Evans@Sun.COM 	 *	have an object capabilities group.  If the object has no object
85211827SRod.Evans@Sun.COM 	 *	capabilities, an empty object group is defined, consisting of a
85311827SRod.Evans@Sun.COM 	 *	CA_SUNW_NULL element in index [0].
85411827SRod.Evans@Sun.COM 	 *  -	If any capabilities require references to a named string, then
85511827SRod.Evans@Sun.COM 	 *	the section header sh_info points to the associated string
85611827SRod.Evans@Sun.COM 	 *	table.
85711827SRod.Evans@Sun.COM 	 *  -	If an object contains symbol capability groups, then the
85811827SRod.Evans@Sun.COM 	 *	section header sh_link points to the associated capinfo table.
8593617Srie 	 */
86011827SRod.Evans@Sun.COM 	objcapndx = 0;
86111827SRod.Evans@Sun.COM 	descapndx = symcapndx = -1;
86211827SRod.Evans@Sun.COM 	nulls = 0;
86311827SRod.Evans@Sun.COM 
86411827SRod.Evans@Sun.COM 	for (ndx = 0, data = cdata; ndx < cnum; ndx++, data++) {
86511827SRod.Evans@Sun.COM 		switch (data->c_tag) {
86611827SRod.Evans@Sun.COM 		case CA_SUNW_NULL:
86711827SRod.Evans@Sun.COM 			/*
86811827SRod.Evans@Sun.COM 			 * If this is the first CA_SUNW_NULL entry, and no
86911827SRod.Evans@Sun.COM 			 * capabilities group has been found, then this object
87011827SRod.Evans@Sun.COM 			 * does not define any object capabilities.
87111827SRod.Evans@Sun.COM 			 */
87211827SRod.Evans@Sun.COM 			if (nulls++ == 0) {
87311827SRod.Evans@Sun.COM 				if (ndx == 0)
87411827SRod.Evans@Sun.COM 					objcapndx = -1;
87511827SRod.Evans@Sun.COM 			} else if ((symcapndx == -1) && (descapndx != -1))
87611827SRod.Evans@Sun.COM 				symcapndx = descapndx;
87711827SRod.Evans@Sun.COM 
87811827SRod.Evans@Sun.COM 			break;
87911827SRod.Evans@Sun.COM 
88011827SRod.Evans@Sun.COM 		case CA_SUNW_PLAT:
88111827SRod.Evans@Sun.COM 		case CA_SUNW_MACH:
88211827SRod.Evans@Sun.COM 		case CA_SUNW_ID:
88311827SRod.Evans@Sun.COM 			capstrs++;
88411827SRod.Evans@Sun.COM 			/* FALLTHROUGH */
88511827SRod.Evans@Sun.COM 
88611827SRod.Evans@Sun.COM 		case CA_SUNW_HW_1:
88711827SRod.Evans@Sun.COM 		case CA_SUNW_SF_1:
88811827SRod.Evans@Sun.COM 		case CA_SUNW_HW_2:
88911827SRod.Evans@Sun.COM 			/*
89011827SRod.Evans@Sun.COM 			 * If this is the start of a new group, save it.
89111827SRod.Evans@Sun.COM 			 */
89211827SRod.Evans@Sun.COM 			if (descapndx == -1)
89311827SRod.Evans@Sun.COM 				descapndx = ndx;
89411827SRod.Evans@Sun.COM 			break;
89511827SRod.Evans@Sun.COM 
89611827SRod.Evans@Sun.COM 		default:
89711827SRod.Evans@Sun.COM 			eprintf(ofl->ofl_lml, ERR_WARNING,
89811827SRod.Evans@Sun.COM 			    MSG_INTL(MSG_FIL_UNKCAP), ifl->ifl_name,
89911827SRod.Evans@Sun.COM 			    EC_WORD(cisp->is_scnndx), cisp->is_name,
90011827SRod.Evans@Sun.COM 			    data->c_tag);
9010Sstevel@tonic-gate 		}
9020Sstevel@tonic-gate 	}
90311827SRod.Evans@Sun.COM 
90411827SRod.Evans@Sun.COM 	/*
90511827SRod.Evans@Sun.COM 	 * If a string capabilities entry has been found, the capabilities
90611827SRod.Evans@Sun.COM 	 * section must reference the associated string table.
90711827SRod.Evans@Sun.COM 	 */
90811827SRod.Evans@Sun.COM 	if (capstrs) {
90911827SRod.Evans@Sun.COM 		Word	info = cisp->is_shdr->sh_info;
91011827SRod.Evans@Sun.COM 
91111827SRod.Evans@Sun.COM 		if ((info == 0) || (info > ifl->ifl_shnum)) {
91211827SRod.Evans@Sun.COM 			eprintf(ofl->ofl_lml, ERR_FATAL,
91311827SRod.Evans@Sun.COM 			    MSG_INTL(MSG_FIL_INVSHINFO), ifl->ifl_name,
91411827SRod.Evans@Sun.COM 			    EC_WORD(cisp->is_scnndx), cisp->is_name,
91511827SRod.Evans@Sun.COM 			    EC_XWORD(info));
91611827SRod.Evans@Sun.COM 			ofl->ofl_flags |= FLG_OF_FATAL;
91711827SRod.Evans@Sun.COM 			return (S_ERROR);
91811827SRod.Evans@Sun.COM 		}
91911827SRod.Evans@Sun.COM 		strs = (char *)ifl->ifl_isdesc[info]->is_indata->d_buf;
92011827SRod.Evans@Sun.COM 	}
92111827SRod.Evans@Sun.COM 
92211827SRod.Evans@Sun.COM 	/*
92311827SRod.Evans@Sun.COM 	 * The processing of capabilities groups is as follows:
92411827SRod.Evans@Sun.COM 	 *
92511827SRod.Evans@Sun.COM 	 *  -	if a relocatable object provides only object capabilities, and
92611827SRod.Evans@Sun.COM 	 *	the -z symbolcap option is in effect, then the object
92711827SRod.Evans@Sun.COM 	 *	capabilities are transformed into symbol capabilities and the
92811827SRod.Evans@Sun.COM 	 *	symbol capabilities are carried over to the output file.
92911827SRod.Evans@Sun.COM 	 *  -	in all other cases, any capabilities present in an input
93011827SRod.Evans@Sun.COM 	 *	relocatable object are carried from the input object to the
93111827SRod.Evans@Sun.COM 	 *	output without any transformation or conversion.
93211827SRod.Evans@Sun.COM 	 *
93311827SRod.Evans@Sun.COM 	 * Capture any object capabilities that are to be carried over to the
93411827SRod.Evans@Sun.COM 	 * output file.
93511827SRod.Evans@Sun.COM 	 */
93611827SRod.Evans@Sun.COM 	if ((objcapndx == 0) &&
93711827SRod.Evans@Sun.COM 	    ((symcapndx != -1) || ((ofl->ofl_flags & FLG_OF_OTOSCAP) == 0))) {
93811827SRod.Evans@Sun.COM 		for (ndx = 0, data = cdata; ndx < cnum; ndx++, data++) {
93911827SRod.Evans@Sun.COM 			/*
94011827SRod.Evans@Sun.COM 			 * Object capabilities end at the first null.
94111827SRod.Evans@Sun.COM 			 */
94211827SRod.Evans@Sun.COM 			if (data->c_tag == CA_SUNW_NULL)
94311827SRod.Evans@Sun.COM 				break;
94411827SRod.Evans@Sun.COM 
94511827SRod.Evans@Sun.COM 			/*
94611827SRod.Evans@Sun.COM 			 * Only the object software capabilities that are
94711827SRod.Evans@Sun.COM 			 * defined in a relocatable object become part of the
94811827SRod.Evans@Sun.COM 			 * object software capabilities in the output file.
94911827SRod.Evans@Sun.COM 			 * However, check the validity of any object software
95011827SRod.Evans@Sun.COM 			 * capabilities of any dependencies.
95111827SRod.Evans@Sun.COM 			 */
95211827SRod.Evans@Sun.COM 			if (data->c_tag == CA_SUNW_SF_1) {
95311827SRod.Evans@Sun.COM 				sf1_cap(ofl, data->c_un.c_val, ifl, cisp);
95411827SRod.Evans@Sun.COM 				continue;
95511827SRod.Evans@Sun.COM 			}
95611827SRod.Evans@Sun.COM 
95711827SRod.Evans@Sun.COM 			/*
95811827SRod.Evans@Sun.COM 			 * The remaining capability types must come from a
95911827SRod.Evans@Sun.COM 			 * relocatable object in order to contribute to the
96011827SRod.Evans@Sun.COM 			 * output.
96111827SRod.Evans@Sun.COM 			 */
96211827SRod.Evans@Sun.COM 			if (ifl->ifl_ehdr->e_type != ET_REL)
96311827SRod.Evans@Sun.COM 				continue;
96411827SRod.Evans@Sun.COM 
96511827SRod.Evans@Sun.COM 			switch (data->c_tag) {
96611827SRod.Evans@Sun.COM 			case CA_SUNW_HW_1:
96711827SRod.Evans@Sun.COM 			case CA_SUNW_HW_2:
96811827SRod.Evans@Sun.COM 				hw_cap(ofl, data->c_tag, data->c_un.c_val);
96911827SRod.Evans@Sun.COM 				break;
97011827SRod.Evans@Sun.COM 
97111827SRod.Evans@Sun.COM 			case CA_SUNW_PLAT:
97211827SRod.Evans@Sun.COM 				str_cap(ofl, strs + data->c_un.c_ptr,
97311827SRod.Evans@Sun.COM 				    FLG_OF1_OVPLATCAP, CA_SUNW_PLAT,
97411827SRod.Evans@Sun.COM 				    &ofl->ofl_ocapset.oc_plat);
97511827SRod.Evans@Sun.COM 				break;
97611827SRod.Evans@Sun.COM 
97711827SRod.Evans@Sun.COM 			case CA_SUNW_MACH:
97811827SRod.Evans@Sun.COM 				str_cap(ofl, strs + data->c_un.c_ptr,
97911827SRod.Evans@Sun.COM 				    FLG_OF1_OVMACHCAP, CA_SUNW_MACH,
98011827SRod.Evans@Sun.COM 				    &ofl->ofl_ocapset.oc_mach);
98111827SRod.Evans@Sun.COM 				break;
98211827SRod.Evans@Sun.COM 
98311827SRod.Evans@Sun.COM 			case CA_SUNW_ID:
98411827SRod.Evans@Sun.COM 				id_cap(ofl, strs + data->c_un.c_ptr,
98511827SRod.Evans@Sun.COM 				    FLG_OCS_USRDEFID);
98611827SRod.Evans@Sun.COM 				break;
98711827SRod.Evans@Sun.COM 
98811827SRod.Evans@Sun.COM 			default:
98911827SRod.Evans@Sun.COM 				assert(0);	/* Unknown capability type */
99011827SRod.Evans@Sun.COM 			}
99111827SRod.Evans@Sun.COM 		}
99211827SRod.Evans@Sun.COM 
99311827SRod.Evans@Sun.COM 		/*
99411827SRod.Evans@Sun.COM 		 * If there are no symbol capabilities, or this objects
99511827SRod.Evans@Sun.COM 		 * capabilities aren't being transformed into a symbol
99611827SRod.Evans@Sun.COM 		 * capabilities, then we're done.
99711827SRod.Evans@Sun.COM 		 */
99811827SRod.Evans@Sun.COM 		if ((symcapndx == -1) &&
99911827SRod.Evans@Sun.COM 		    ((ofl->ofl_flags & FLG_OF_OTOSCAP) == 0))
100011827SRod.Evans@Sun.COM 			return (1);
100111827SRod.Evans@Sun.COM 	}
100211827SRod.Evans@Sun.COM 
100311827SRod.Evans@Sun.COM 	/*
100411827SRod.Evans@Sun.COM 	 * If these capabilities don't originate from a relocatable object
100511827SRod.Evans@Sun.COM 	 * there's no further processing required.
100611827SRod.Evans@Sun.COM 	 */
100711827SRod.Evans@Sun.COM 	if (ifl->ifl_ehdr->e_type != ET_REL)
100811827SRod.Evans@Sun.COM 		return (1);
100911827SRod.Evans@Sun.COM 
101011827SRod.Evans@Sun.COM 	/*
101111827SRod.Evans@Sun.COM 	 * If this object only defines an object capabilities group, and the
101211827SRod.Evans@Sun.COM 	 * -z symbolcap option is in effect, then all global function symbols
101311827SRod.Evans@Sun.COM 	 * and initialized global data symbols are renamed and assigned to the
101411827SRod.Evans@Sun.COM 	 * transformed symbol capabilities group.
101511827SRod.Evans@Sun.COM 	 */
101611827SRod.Evans@Sun.COM 	if ((objcapndx == 0) &&
101711827SRod.Evans@Sun.COM 	    (symcapndx == -1) && (ofl->ofl_flags & FLG_OF_OTOSCAP))
101811827SRod.Evans@Sun.COM 		ifl->ifl_flags |= FLG_IF_OTOSCAP;
101911827SRod.Evans@Sun.COM 
102011827SRod.Evans@Sun.COM 	/*
102111827SRod.Evans@Sun.COM 	 * Allocate a capabilities descriptor to collect the capabilities data
102211827SRod.Evans@Sun.COM 	 * for this input file.  Allocate a mirror of the raw capabilities data
102311827SRod.Evans@Sun.COM 	 * that points to the individual symbol capabilities groups.  An APlist
102411827SRod.Evans@Sun.COM 	 * is used, although it will be sparsely populated, as the list provides
102511827SRod.Evans@Sun.COM 	 * a convenient mechanism for traversal later.
102611827SRod.Evans@Sun.COM 	 */
102711827SRod.Evans@Sun.COM 	if (((cdp = libld_calloc(sizeof (Cap_desc), 1)) == NULL) ||
102811827SRod.Evans@Sun.COM 	    (aplist_append(&(cdp->ca_groups), NULL, cnum) == NULL))
102911827SRod.Evans@Sun.COM 		return (S_ERROR);
103011827SRod.Evans@Sun.COM 
103111827SRod.Evans@Sun.COM 	/*
103211827SRod.Evans@Sun.COM 	 * Clear the allocated APlist data array, and assign the number of
103311827SRod.Evans@Sun.COM 	 * items as the total number of array items.
103411827SRod.Evans@Sun.COM 	 */
103511827SRod.Evans@Sun.COM 	(void) memset(&cdp->ca_groups->apl_data[0], 0,
103611827SRod.Evans@Sun.COM 	    (cnum * sizeof (void *)));
103711827SRod.Evans@Sun.COM 	cdp->ca_groups->apl_nitems = cnum;
103811827SRod.Evans@Sun.COM 
103911827SRod.Evans@Sun.COM 	ifl->ifl_caps = cdp;
104011827SRod.Evans@Sun.COM 
104111827SRod.Evans@Sun.COM 	/*
104211827SRod.Evans@Sun.COM 	 * Traverse the capabilities data, unpacking the data into a
104311827SRod.Evans@Sun.COM 	 * capabilities set.  Process each capabilities set as a unique group.
104411827SRod.Evans@Sun.COM 	 */
104511827SRod.Evans@Sun.COM 	descapndx = -1;
104611827SRod.Evans@Sun.COM 	nulls = 0;
104711827SRod.Evans@Sun.COM 
104811827SRod.Evans@Sun.COM 	for (ndx = 0, data = cdata; ndx < cnum; ndx++, data++) {
104911827SRod.Evans@Sun.COM 		Capstr	*capstr;
105011827SRod.Evans@Sun.COM 
105111827SRod.Evans@Sun.COM 		switch (data->c_tag) {
105211827SRod.Evans@Sun.COM 		case CA_SUNW_NULL:
105311827SRod.Evans@Sun.COM 			nulls++;
105411827SRod.Evans@Sun.COM 
105511827SRod.Evans@Sun.COM 			/*
105611827SRod.Evans@Sun.COM 			 * Process the capabilities group that this null entry
105711827SRod.Evans@Sun.COM 			 * terminates.  The capabilities group that is returned
105811827SRod.Evans@Sun.COM 			 * will either point to this file's data, or to a
105911827SRod.Evans@Sun.COM 			 * matching capabilities group that has already been
106011827SRod.Evans@Sun.COM 			 * processed.
106111827SRod.Evans@Sun.COM 			 *
106211827SRod.Evans@Sun.COM 			 * Note, if this object defines object capabilities,
106311827SRod.Evans@Sun.COM 			 * the first group descriptor points to these object
106411827SRod.Evans@Sun.COM 			 * capabilities.  It is only necessary to save this
106511827SRod.Evans@Sun.COM 			 * descriptor when object capabilities are being
106611827SRod.Evans@Sun.COM 			 * transformed into symbol capabilities (-z symbolcap).
106711827SRod.Evans@Sun.COM 			 */
106811827SRod.Evans@Sun.COM 			if (descapndx != -1) {
106911827SRod.Evans@Sun.COM 				if ((nulls > 1) ||
107011827SRod.Evans@Sun.COM 				    (ifl->ifl_flags & FLG_IF_OTOSCAP)) {
107111827SRod.Evans@Sun.COM 					APlist	*alp = cdp->ca_groups;
107211827SRod.Evans@Sun.COM 
107311827SRod.Evans@Sun.COM 					if ((alp->apl_data[descapndx] =
107411827SRod.Evans@Sun.COM 					    get_cap_group(&ocapset,
107511827SRod.Evans@Sun.COM 					    (ndx - descapndx), ofl,
107611827SRod.Evans@Sun.COM 					    cisp)) == NULL)
107711827SRod.Evans@Sun.COM 						return (S_ERROR);
107811827SRod.Evans@Sun.COM 				}
107911827SRod.Evans@Sun.COM 
108011827SRod.Evans@Sun.COM 				/*
108111827SRod.Evans@Sun.COM 				 * Clean up the capabilities data in preparation
108211827SRod.Evans@Sun.COM 				 * for processing additional groups.  If the
108311827SRod.Evans@Sun.COM 				 * collected capabilities strings were used to
108411827SRod.Evans@Sun.COM 				 * establish a new output group, they will have
108511827SRod.Evans@Sun.COM 				 * been saved in get_cap_group().  If these
108611827SRod.Evans@Sun.COM 				 * descriptors still exist, then an existing
108711827SRod.Evans@Sun.COM 				 * descriptor has been used to associate with
108811827SRod.Evans@Sun.COM 				 * this file, and these string descriptors can
108911827SRod.Evans@Sun.COM 				 * be freed.
109011827SRod.Evans@Sun.COM 				 */
109111827SRod.Evans@Sun.COM 				ocapset.oc_hw_1.cm_val =
109211827SRod.Evans@Sun.COM 				    ocapset.oc_sf_1.cm_val =
109311827SRod.Evans@Sun.COM 				    ocapset.oc_hw_2.cm_val = 0;
109411827SRod.Evans@Sun.COM 				if (ocapset.oc_plat.cl_val) {
109511827SRod.Evans@Sun.COM 					free((void *)ocapset.oc_plat.cl_val);
109611827SRod.Evans@Sun.COM 					ocapset.oc_plat.cl_val = NULL;
109711827SRod.Evans@Sun.COM 				}
109811827SRod.Evans@Sun.COM 				if (ocapset.oc_mach.cl_val) {
109911827SRod.Evans@Sun.COM 					free((void *)ocapset.oc_mach.cl_val);
110011827SRod.Evans@Sun.COM 					ocapset.oc_mach.cl_val = NULL;
110111827SRod.Evans@Sun.COM 				}
110211827SRod.Evans@Sun.COM 				descapndx = -1;
110311827SRod.Evans@Sun.COM 			}
110411827SRod.Evans@Sun.COM 			continue;
110511827SRod.Evans@Sun.COM 
110611827SRod.Evans@Sun.COM 		case CA_SUNW_HW_1:
110711827SRod.Evans@Sun.COM 			ocapset.oc_hw_1.cm_val = data->c_un.c_val;
110811827SRod.Evans@Sun.COM 			DBG_CALL(Dbg_cap_val_entry(ofl->ofl_lml,
110911827SRod.Evans@Sun.COM 			    DBG_STATE_ORIGINAL, CA_SUNW_HW_1,
111011827SRod.Evans@Sun.COM 			    ocapset.oc_hw_1.cm_val, ld_targ.t_m.m_mach));
111111827SRod.Evans@Sun.COM 			break;
111211827SRod.Evans@Sun.COM 
111311827SRod.Evans@Sun.COM 		case CA_SUNW_SF_1:
111411827SRod.Evans@Sun.COM 			ocapset.oc_sf_1.cm_val = data->c_un.c_val;
111511827SRod.Evans@Sun.COM 			DBG_CALL(Dbg_cap_val_entry(ofl->ofl_lml,
111611827SRod.Evans@Sun.COM 			    DBG_STATE_ORIGINAL, CA_SUNW_SF_1,
111711827SRod.Evans@Sun.COM 			    ocapset.oc_sf_1.cm_val, ld_targ.t_m.m_mach));
111811827SRod.Evans@Sun.COM 			break;
111911827SRod.Evans@Sun.COM 
112011827SRod.Evans@Sun.COM 		case CA_SUNW_HW_2:
112111827SRod.Evans@Sun.COM 			ocapset.oc_hw_2.cm_val = data->c_un.c_val;
112211827SRod.Evans@Sun.COM 			DBG_CALL(Dbg_cap_val_entry(ofl->ofl_lml,
112311827SRod.Evans@Sun.COM 			    DBG_STATE_ORIGINAL, CA_SUNW_HW_2,
112411827SRod.Evans@Sun.COM 			    ocapset.oc_hw_2.cm_val, ld_targ.t_m.m_mach));
112511827SRod.Evans@Sun.COM 			break;
112611827SRod.Evans@Sun.COM 
112711827SRod.Evans@Sun.COM 		case CA_SUNW_PLAT:
112811827SRod.Evans@Sun.COM 			if ((capstr = alist_append(&ocapset.oc_plat.cl_val,
112911827SRod.Evans@Sun.COM 			    NULL, sizeof (Capstr), AL_CNT_CAP_NAMES)) == NULL)
113011827SRod.Evans@Sun.COM 				return (S_ERROR);
113111827SRod.Evans@Sun.COM 			capstr->cs_str = strs + data->c_un.c_ptr;
113211827SRod.Evans@Sun.COM 			DBG_CALL(Dbg_cap_ptr_entry(ofl->ofl_lml,
113311827SRod.Evans@Sun.COM 			    DBG_STATE_ORIGINAL, CA_SUNW_PLAT, capstr->cs_str));
113411827SRod.Evans@Sun.COM 			break;
113511827SRod.Evans@Sun.COM 
113611827SRod.Evans@Sun.COM 		case CA_SUNW_MACH:
113711827SRod.Evans@Sun.COM 			if ((capstr = alist_append(&ocapset.oc_mach.cl_val,
113811827SRod.Evans@Sun.COM 			    NULL, sizeof (Capstr), AL_CNT_CAP_NAMES)) == NULL)
113911827SRod.Evans@Sun.COM 				return (S_ERROR);
114011827SRod.Evans@Sun.COM 			capstr->cs_str = strs + data->c_un.c_ptr;
114111827SRod.Evans@Sun.COM 			DBG_CALL(Dbg_cap_ptr_entry(ofl->ofl_lml,
114211827SRod.Evans@Sun.COM 			    DBG_STATE_ORIGINAL, CA_SUNW_MACH, capstr->cs_str));
114311827SRod.Evans@Sun.COM 			break;
114411827SRod.Evans@Sun.COM 
114511827SRod.Evans@Sun.COM 		case CA_SUNW_ID:
114611827SRod.Evans@Sun.COM 			ocapset.oc_id.cs_str = strs + data->c_un.c_ptr;
114711827SRod.Evans@Sun.COM 			DBG_CALL(Dbg_cap_ptr_entry(ofl->ofl_lml,
114811827SRod.Evans@Sun.COM 			    DBG_STATE_ORIGINAL, CA_SUNW_ID,
114911827SRod.Evans@Sun.COM 			    ocapset.oc_id.cs_str));
115011827SRod.Evans@Sun.COM 			break;
115111827SRod.Evans@Sun.COM 		}
115211827SRod.Evans@Sun.COM 
115311827SRod.Evans@Sun.COM 		/*
115411827SRod.Evans@Sun.COM 		 * Save the start of this new group.
115511827SRod.Evans@Sun.COM 		 */
115611827SRod.Evans@Sun.COM 		if (descapndx == -1)
115711827SRod.Evans@Sun.COM 			descapndx = ndx;
115811827SRod.Evans@Sun.COM 	}
115911827SRod.Evans@Sun.COM 	return (1);
116011827SRod.Evans@Sun.COM }
116111827SRod.Evans@Sun.COM 
116211827SRod.Evans@Sun.COM /*
116311827SRod.Evans@Sun.COM  * Capture any symbol capabilities symbols.  An object file that contains symbol
116411827SRod.Evans@Sun.COM  * capabilities has an associated .SUNW_capinfo section.  This section
116511827SRod.Evans@Sun.COM  * identifies which symbols are associated to which capabilities, together with
116611827SRod.Evans@Sun.COM  * their associated lead symbol.  Each of these symbol pairs are recorded for
116711827SRod.Evans@Sun.COM  * processing later.
116811827SRod.Evans@Sun.COM  */
116911827SRod.Evans@Sun.COM static uintptr_t
117011827SRod.Evans@Sun.COM process_capinfo(Ofl_desc *ofl, Ifl_desc *ifl, Is_desc *isp)
117111827SRod.Evans@Sun.COM {
117211827SRod.Evans@Sun.COM 	Cap_desc	*cdp = ifl->ifl_caps;
117311827SRod.Evans@Sun.COM 	Capinfo		*capinfo = isp->is_indata->d_buf;
117411827SRod.Evans@Sun.COM 	Shdr		*shdr = isp->is_shdr;
117511827SRod.Evans@Sun.COM 	Word		cndx, capinfonum;
117611827SRod.Evans@Sun.COM 
117711827SRod.Evans@Sun.COM 	capinfonum = (Word)(shdr->sh_size / shdr->sh_entsize);
117811827SRod.Evans@Sun.COM 
117911827SRod.Evans@Sun.COM 	if ((cdp == NULL) || (capinfo == NULL) || (capinfonum == 0))
118011827SRod.Evans@Sun.COM 		return (0);
118111827SRod.Evans@Sun.COM 
118211827SRod.Evans@Sun.COM 	for (cndx = 1, capinfo++; cndx < capinfonum; cndx++, capinfo++) {
118311827SRod.Evans@Sun.COM 		Sym_desc	*sdp, *lsdp;
118411827SRod.Evans@Sun.COM 		Word		lndx;
118511827SRod.Evans@Sun.COM 		uchar_t		gndx;
118611827SRod.Evans@Sun.COM 
118711827SRod.Evans@Sun.COM 		if ((gndx = (uchar_t)ELF_C_GROUP(*capinfo)) == 0)
118811827SRod.Evans@Sun.COM 			continue;
118911827SRod.Evans@Sun.COM 		lndx = (Word)ELF_C_SYM(*capinfo);
119011827SRod.Evans@Sun.COM 
119111827SRod.Evans@Sun.COM 		/*
119211827SRod.Evans@Sun.COM 		 * Catch any anomalies.  A capabilities symbol should be valid,
119311827SRod.Evans@Sun.COM 		 * and the capabilities lead symbol should also be global.
119411827SRod.Evans@Sun.COM 		 * Note, ld(1) -z symbolcap would create local capabilities
119511827SRod.Evans@Sun.COM 		 * symbols, but we don't enforce this so as to give the
119611827SRod.Evans@Sun.COM 		 * compilation environment a little more freedom.
119711827SRod.Evans@Sun.COM 		 */
119811827SRod.Evans@Sun.COM 		if ((sdp = ifl->ifl_oldndx[cndx]) == NULL) {
119911827SRod.Evans@Sun.COM 			eprintf(ofl->ofl_lml, ERR_WARNING,
120011827SRod.Evans@Sun.COM 			    MSG_INTL(MSG_CAPINFO_INVALSYM), ifl->ifl_name,
120111827SRod.Evans@Sun.COM 			    EC_WORD(isp->is_scnndx), isp->is_name, cndx,
120211827SRod.Evans@Sun.COM 			    MSG_INTL(MSG_STR_UNKNOWN));
120311827SRod.Evans@Sun.COM 			continue;
120411827SRod.Evans@Sun.COM 		}
120511827SRod.Evans@Sun.COM 		if ((lndx == 0) || (lndx >= ifl->ifl_symscnt) ||
120611827SRod.Evans@Sun.COM 		    ((lsdp = ifl->ifl_oldndx[lndx]) == NULL) ||
120711827SRod.Evans@Sun.COM 		    (ELF_ST_BIND(lsdp->sd_sym->st_info) != STB_GLOBAL)) {
120811827SRod.Evans@Sun.COM 			eprintf(ofl->ofl_lml, ERR_WARNING,
120911827SRod.Evans@Sun.COM 			    MSG_INTL(MSG_CAPINFO_INVALLEAD), ifl->ifl_name,
121011827SRod.Evans@Sun.COM 			    EC_WORD(isp->is_scnndx), isp->is_name, cndx, lsdp ?
121111827SRod.Evans@Sun.COM 			    demangle(lsdp->sd_name) : MSG_INTL(MSG_STR_UNKNOWN),
121211827SRod.Evans@Sun.COM 			    lndx);
121311827SRod.Evans@Sun.COM 			continue;
121411827SRod.Evans@Sun.COM 		}
121511827SRod.Evans@Sun.COM 
121611827SRod.Evans@Sun.COM 		/*
121711827SRod.Evans@Sun.COM 		 * Indicate that this is a capabilities symbol.
121811827SRod.Evans@Sun.COM 		 */
121911827SRod.Evans@Sun.COM 		sdp->sd_flags |= FLG_SY_CAP;
122011827SRod.Evans@Sun.COM 
122111827SRod.Evans@Sun.COM 		/*
122211827SRod.Evans@Sun.COM 		 * Save any global capability symbols.  Global capability
122311827SRod.Evans@Sun.COM 		 * symbols are identified with a CAPINFO_SUNW_GLOB group id.
122411827SRod.Evans@Sun.COM 		 * The lead symbol for this global capability symbol is either
122511827SRod.Evans@Sun.COM 		 * the symbol itself, or an alias.
122611827SRod.Evans@Sun.COM 		 */
122711827SRod.Evans@Sun.COM 		if (gndx == CAPINFO_SUNW_GLOB) {
122811827SRod.Evans@Sun.COM 			if (ld_cap_add_family(ofl, lsdp, sdp,
122911827SRod.Evans@Sun.COM 			    NULL, NULL) == S_ERROR)
123011827SRod.Evans@Sun.COM 				return (S_ERROR);
123111827SRod.Evans@Sun.COM 			continue;
123211827SRod.Evans@Sun.COM 		}
123311827SRod.Evans@Sun.COM 
123411827SRod.Evans@Sun.COM 		/*
123511827SRod.Evans@Sun.COM 		 * Track the number of non-global capabilities symbols, as these
123611827SRod.Evans@Sun.COM 		 * are used to size any symbol tables.  If we're generating a
123711827SRod.Evans@Sun.COM 		 * dynamic object, this symbol will be added to the dynamic
123811827SRod.Evans@Sun.COM 		 * symbol table, therefore ensure there is space in the dynamic
123911827SRod.Evans@Sun.COM 		 * string table.
124011827SRod.Evans@Sun.COM 		 */
124111827SRod.Evans@Sun.COM 		ofl->ofl_caploclcnt++;
124211827SRod.Evans@Sun.COM 		if (((ofl->ofl_flags & FLG_OF_RELOBJ) == 0) &&
124311827SRod.Evans@Sun.COM 		    (st_insert(ofl->ofl_dynstrtab, sdp->sd_name) == -1))
124411827SRod.Evans@Sun.COM 			return (S_ERROR);
124511827SRod.Evans@Sun.COM 
124611827SRod.Evans@Sun.COM 		/*
124711827SRod.Evans@Sun.COM 		 * As we're tracking this local symbol as a capabilities symbol,
124811827SRod.Evans@Sun.COM 		 * reduce the local symbol count to compensate.
124911827SRod.Evans@Sun.COM 		 */
125011827SRod.Evans@Sun.COM 		ofl->ofl_locscnt--;
125111827SRod.Evans@Sun.COM 
125211827SRod.Evans@Sun.COM 		/*
125311827SRod.Evans@Sun.COM 		 * Determine whether the associated lead symbol indicates
125411827SRod.Evans@Sun.COM 		 * NODYNSORT.  If so, remove this local entry from the
125511827SRod.Evans@Sun.COM 		 * SUNW_dynsort section too.  NODYNSORT tagging can only be
125611827SRod.Evans@Sun.COM 		 * obtained from a mapfile symbol definition, and thus any
125711827SRod.Evans@Sun.COM 		 * global definition that has this tagging has already been
125811827SRod.Evans@Sun.COM 		 * instantiated and this instance resolved to it.
125911827SRod.Evans@Sun.COM 		 */
126011827SRod.Evans@Sun.COM 		if (lsdp->sd_flags & FLG_SY_NODYNSORT) {
126111827SRod.Evans@Sun.COM 			Sym	*lsym = lsdp->sd_sym;
126211827SRod.Evans@Sun.COM 			uchar_t ltype = ELF_ST_TYPE(lsym->st_info);
126311827SRod.Evans@Sun.COM 
126411827SRod.Evans@Sun.COM 			DYNSORT_COUNT(lsdp, lsym, ltype, --);
126511827SRod.Evans@Sun.COM 			lsdp->sd_flags |= FLG_SY_NODYNSORT;
126611827SRod.Evans@Sun.COM 		}
126711827SRod.Evans@Sun.COM 
126811827SRod.Evans@Sun.COM 		/*
126911827SRod.Evans@Sun.COM 		 * Track this family member, together with its associated group.
127011827SRod.Evans@Sun.COM 		 */
127111827SRod.Evans@Sun.COM 		if (ld_cap_add_family(ofl, lsdp, sdp,
127211827SRod.Evans@Sun.COM 		    cdp->ca_groups->apl_data[gndx], NULL) == S_ERROR)
127311827SRod.Evans@Sun.COM 			return (S_ERROR);
127411827SRod.Evans@Sun.COM 	}
127511827SRod.Evans@Sun.COM 
127611827SRod.Evans@Sun.COM 	return (0);
12770Sstevel@tonic-gate }
12780Sstevel@tonic-gate 
12790Sstevel@tonic-gate /*
12800Sstevel@tonic-gate  * Simply process the section so that we have pointers to the data for use
12810Sstevel@tonic-gate  * in later routines, however don't add the section to the output section
12820Sstevel@tonic-gate  * list as we will be creating our own replacement sections later (ie.
12830Sstevel@tonic-gate  * symtab and relocation).
12840Sstevel@tonic-gate  */
12851618Srie static uintptr_t
12860Sstevel@tonic-gate /* ARGSUSED5 */
12870Sstevel@tonic-gate process_input(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
12887463SRod.Evans@Sun.COM     Word ndx, int ident, Ofl_desc *ofl)
12890Sstevel@tonic-gate {
12906206Sab196087 	return (process_section(name, ifl, shdr, scn, ndx,
12916206Sab196087 	    ld_targ.t_id.id_null, ofl));
12920Sstevel@tonic-gate }
12930Sstevel@tonic-gate 
12940Sstevel@tonic-gate /*
12950Sstevel@tonic-gate  * Keep a running count of relocation entries from input relocatable objects for
12960Sstevel@tonic-gate  * sizing relocation buckets later.  If we're building an executable, save any
12970Sstevel@tonic-gate  * relocations from shared objects to determine if any copy relocation symbol
12980Sstevel@tonic-gate  * has a displacement relocation against it.
12990Sstevel@tonic-gate  */
13001618Srie static uintptr_t
13010Sstevel@tonic-gate /* ARGSUSED5 */
13020Sstevel@tonic-gate process_reloc(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
13037463SRod.Evans@Sun.COM     Word ndx, int ident, Ofl_desc *ofl)
13040Sstevel@tonic-gate {
13050Sstevel@tonic-gate 	if (process_section(name, ifl,
13066206Sab196087 	    shdr, scn, ndx, ld_targ.t_id.id_null, ofl) == S_ERROR)
13070Sstevel@tonic-gate 		return (S_ERROR);
13080Sstevel@tonic-gate 
13090Sstevel@tonic-gate 	if (ifl->ifl_ehdr->e_type == ET_REL) {
13100Sstevel@tonic-gate 		if (shdr->sh_entsize && (shdr->sh_entsize <= shdr->sh_size))
13110Sstevel@tonic-gate 			/* LINTED */
13120Sstevel@tonic-gate 			ofl->ofl_relocincnt +=
13130Sstevel@tonic-gate 			    (Word)(shdr->sh_size / shdr->sh_entsize);
13140Sstevel@tonic-gate 	} else if (ofl->ofl_flags & FLG_OF_EXEC) {
13159131SRod.Evans@Sun.COM 		if (aplist_append(&ifl->ifl_relsect, ifl->ifl_isdesc[ndx],
13169131SRod.Evans@Sun.COM 		    AL_CNT_IFL_RELSECS) == NULL)
13170Sstevel@tonic-gate 			return (S_ERROR);
13180Sstevel@tonic-gate 	}
13190Sstevel@tonic-gate 	return (1);
13200Sstevel@tonic-gate }
13210Sstevel@tonic-gate 
13220Sstevel@tonic-gate /*
13230Sstevel@tonic-gate  * Process a string table section.  A valid section contains an initial and
13240Sstevel@tonic-gate  * final null byte.
13250Sstevel@tonic-gate  */
13261618Srie static uintptr_t
13270Sstevel@tonic-gate process_strtab(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
13287463SRod.Evans@Sun.COM     Word ndx, int ident, Ofl_desc *ofl)
13290Sstevel@tonic-gate {
13300Sstevel@tonic-gate 	char		*data;
13310Sstevel@tonic-gate 	size_t		size;
13320Sstevel@tonic-gate 	Is_desc		*isp;
13330Sstevel@tonic-gate 	uintptr_t	error;
13340Sstevel@tonic-gate 
13350Sstevel@tonic-gate 	/*
13360Sstevel@tonic-gate 	 * Never include .stab.excl sections in any output file.
13370Sstevel@tonic-gate 	 * If the -s flag has been specified strip any .stab sections.
13380Sstevel@tonic-gate 	 */
13390Sstevel@tonic-gate 	if (((ofl->ofl_flags & FLG_OF_STRIP) && ident &&
13400Sstevel@tonic-gate 	    (strncmp(name, MSG_ORIG(MSG_SCN_STAB), MSG_SCN_STAB_SIZE) == 0)) ||
13410Sstevel@tonic-gate 	    (strcmp(name, MSG_ORIG(MSG_SCN_STABEXCL)) == 0) && ident)
13420Sstevel@tonic-gate 		return (1);
13430Sstevel@tonic-gate 
13440Sstevel@tonic-gate 	/*
13450Sstevel@tonic-gate 	 * If we got here to process a .shstrtab or .dynstr table, `ident' will
13460Sstevel@tonic-gate 	 * be null.  Otherwise make sure we don't have a .strtab section as this
13470Sstevel@tonic-gate 	 * should not be added to the output section list either.
13480Sstevel@tonic-gate 	 */
13496206Sab196087 	if ((ident != ld_targ.t_id.id_null) &&
13500Sstevel@tonic-gate 	    (strcmp(name, MSG_ORIG(MSG_SCN_STRTAB)) == 0))
13516206Sab196087 		ident = ld_targ.t_id.id_null;
13520Sstevel@tonic-gate 
13530Sstevel@tonic-gate 	error = process_section(name, ifl, shdr, scn, ndx, ident, ofl);
13540Sstevel@tonic-gate 	if ((error == 0) || (error == S_ERROR))
13550Sstevel@tonic-gate 		return (error);
13560Sstevel@tonic-gate 
13570Sstevel@tonic-gate 	/*
13580Sstevel@tonic-gate 	 * String tables should start and end with a NULL byte.  Note, it has
13590Sstevel@tonic-gate 	 * been known for the assembler to create empty string tables, so check
13600Sstevel@tonic-gate 	 * the size before attempting to verify the data itself.
13610Sstevel@tonic-gate 	 */
13620Sstevel@tonic-gate 	isp = ifl->ifl_isdesc[ndx];
13630Sstevel@tonic-gate 	size = isp->is_indata->d_size;
13640Sstevel@tonic-gate 	if (size) {
13650Sstevel@tonic-gate 		data = isp->is_indata->d_buf;
13660Sstevel@tonic-gate 		if (data[0] != '\0' || data[size - 1] != '\0')
13671618Srie 			eprintf(ofl->ofl_lml, ERR_WARNING,
13689878SAli.Bahrami@Sun.COM 			    MSG_INTL(MSG_FIL_MALSTR), ifl->ifl_name,
13699878SAli.Bahrami@Sun.COM 			    EC_WORD(isp->is_scnndx), name);
13700Sstevel@tonic-gate 	} else
13710Sstevel@tonic-gate 		isp->is_indata->d_buf = (void *)MSG_ORIG(MSG_STR_EMPTY);
13720Sstevel@tonic-gate 
13730Sstevel@tonic-gate 	ifl->ifl_flags |= FLG_IF_HSTRTAB;
13740Sstevel@tonic-gate 	return (1);
13750Sstevel@tonic-gate }
13760Sstevel@tonic-gate 
13770Sstevel@tonic-gate /*
13780Sstevel@tonic-gate  * Invalid sections produce a warning and are skipped.
13790Sstevel@tonic-gate  */
13801618Srie static uintptr_t
13810Sstevel@tonic-gate /* ARGSUSED3 */
13820Sstevel@tonic-gate invalid_section(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
13830Sstevel@tonic-gate     Word ndx, int ident, Ofl_desc *ofl)
13840Sstevel@tonic-gate {
13854734Sab196087 	Conv_inv_buf_t inv_buf;
13864734Sab196087 
13871618Srie 	eprintf(ofl->ofl_lml, ERR_WARNING, MSG_INTL(MSG_FIL_INVALSEC),
13889878SAli.Bahrami@Sun.COM 	    ifl->ifl_name, EC_WORD(ndx), name,
13899273SAli.Bahrami@Sun.COM 	    conv_sec_type(ifl->ifl_ehdr->e_ident[EI_OSABI],
13909273SAli.Bahrami@Sun.COM 	    ifl->ifl_ehdr->e_machine, shdr->sh_type, 0, &inv_buf));
13910Sstevel@tonic-gate 	return (1);
13920Sstevel@tonic-gate }
13930Sstevel@tonic-gate 
13940Sstevel@tonic-gate /*
139510454SAli.Bahrami@Sun.COM  * Compare an input section name to a given string, taking the ELF '%'
139610454SAli.Bahrami@Sun.COM  * section naming convention into account. If an input section name
139710454SAli.Bahrami@Sun.COM  * contains a '%' character, the '%' and all following characters are
139810454SAli.Bahrami@Sun.COM  * ignored in the comparison.
139910454SAli.Bahrami@Sun.COM  *
140010454SAli.Bahrami@Sun.COM  * entry:
140110454SAli.Bahrami@Sun.COM  *	is_name - Name of input section
140210454SAli.Bahrami@Sun.COM  *	match_name - Name to compare to
140310454SAli.Bahrami@Sun.COM  *	match_len - strlen(match_name)
140410454SAli.Bahrami@Sun.COM  *
140510454SAli.Bahrami@Sun.COM  * exit:
140610454SAli.Bahrami@Sun.COM  *	Returns True (1) if the names match, and False (0) otherwise.
140710454SAli.Bahrami@Sun.COM  */
140810454SAli.Bahrami@Sun.COM inline static int
140910454SAli.Bahrami@Sun.COM is_name_cmp(const char *is_name, const char *match_name, size_t match_len)
141010454SAli.Bahrami@Sun.COM {
141110454SAli.Bahrami@Sun.COM 	/*
141210454SAli.Bahrami@Sun.COM 	 * If the start of is_name is not a match for name,
141310454SAli.Bahrami@Sun.COM 	 * the match fails.
141410454SAli.Bahrami@Sun.COM 	 */
141510454SAli.Bahrami@Sun.COM 	if (strncmp(is_name, match_name, match_len) != 0)
141610454SAli.Bahrami@Sun.COM 		return (0);
141710454SAli.Bahrami@Sun.COM 
141810454SAli.Bahrami@Sun.COM 	/*
141910454SAli.Bahrami@Sun.COM 	 * The prefix matched. The next character must be either '%', or
142010454SAli.Bahrami@Sun.COM 	 * NULL, in order for a match to be true.
142110454SAli.Bahrami@Sun.COM 	 */
142210454SAli.Bahrami@Sun.COM 	is_name += match_len;
142310454SAli.Bahrami@Sun.COM 	return ((*is_name == '\0') || (*is_name == '%'));
142410454SAli.Bahrami@Sun.COM }
142510454SAli.Bahrami@Sun.COM 
142610454SAli.Bahrami@Sun.COM /*
1427*11993SAli.Bahrami@Sun.COM  * Helper routine for process_progbits() to process allocable sections.
1428*11993SAli.Bahrami@Sun.COM  *
1429*11993SAli.Bahrami@Sun.COM  * entry:
1430*11993SAli.Bahrami@Sun.COM  *	name, ifl, shdr, ndx, ident, ofl - As passed to process_progbits().
1431*11993SAli.Bahrami@Sun.COM  *	is_stab_index - TRUE if section is .index.
1432*11993SAli.Bahrami@Sun.COM  *	is_flags - Additional flags to be added to the input section.
1433*11993SAli.Bahrami@Sun.COM  *
1434*11993SAli.Bahrami@Sun.COM  * exit:
1435*11993SAli.Bahrami@Sun.COM  *	The allocable section has been processed. *ident and *is_flags
1436*11993SAli.Bahrami@Sun.COM  *	are updated as necessary to reflect the changes. Returns TRUE
1437*11993SAli.Bahrami@Sun.COM  *	for success, FALSE for failure.
1438*11993SAli.Bahrami@Sun.COM  */
1439*11993SAli.Bahrami@Sun.COM inline static Boolean
1440*11993SAli.Bahrami@Sun.COM process_progbits_alloc(const char *name, Ifl_desc *ifl, Shdr *shdr,
1441*11993SAli.Bahrami@Sun.COM     Word ndx, int *ident, Ofl_desc *ofl, Boolean is_stab_index,
1442*11993SAli.Bahrami@Sun.COM     Word *is_flags)
1443*11993SAli.Bahrami@Sun.COM {
1444*11993SAli.Bahrami@Sun.COM 	Boolean done = FALSE;
1445*11993SAli.Bahrami@Sun.COM 
1446*11993SAli.Bahrami@Sun.COM 	if (name[0] == '.') {
1447*11993SAli.Bahrami@Sun.COM 		Conv_inv_buf_t inv_buf1, inv_buf2;
1448*11993SAli.Bahrami@Sun.COM 
1449*11993SAli.Bahrami@Sun.COM 		switch (name[1]) {
1450*11993SAli.Bahrami@Sun.COM 		case 'e':
1451*11993SAli.Bahrami@Sun.COM 			if (!is_name_cmp(name, MSG_ORIG(MSG_SCN_EHFRAME),
1452*11993SAli.Bahrami@Sun.COM 			    MSG_SCN_EHFRAME_SIZE))
1453*11993SAli.Bahrami@Sun.COM 				break;
1454*11993SAli.Bahrami@Sun.COM 
1455*11993SAli.Bahrami@Sun.COM 			*ident = ld_targ.t_id.id_unwind;
1456*11993SAli.Bahrami@Sun.COM 			*is_flags |= FLG_IS_EHFRAME;
1457*11993SAli.Bahrami@Sun.COM 			done = TRUE;
1458*11993SAli.Bahrami@Sun.COM 
1459*11993SAli.Bahrami@Sun.COM 			/*
1460*11993SAli.Bahrami@Sun.COM 			 * Only accept a progbits .eh_frame on a platform
1461*11993SAli.Bahrami@Sun.COM 			 * for which this is the expected type.
1462*11993SAli.Bahrami@Sun.COM 			 */
1463*11993SAli.Bahrami@Sun.COM 			if (ld_targ.t_m.m_sht_unwind == SHT_PROGBITS)
1464*11993SAli.Bahrami@Sun.COM 				break;
1465*11993SAli.Bahrami@Sun.COM 			eprintf(ofl->ofl_lml, ERR_FATAL,
1466*11993SAli.Bahrami@Sun.COM 			    MSG_INTL(MSG_FIL_EXEHFRMTYP), ifl->ifl_name,
1467*11993SAli.Bahrami@Sun.COM 			    EC_WORD(ndx), name,
1468*11993SAli.Bahrami@Sun.COM 			    conv_sec_type(ifl->ifl_ehdr->e_ident[EI_OSABI],
1469*11993SAli.Bahrami@Sun.COM 			    ifl->ifl_ehdr->e_machine, shdr->sh_type,
1470*11993SAli.Bahrami@Sun.COM 			    CONV_FMT_ALT_CF, &inv_buf1),
1471*11993SAli.Bahrami@Sun.COM 			    conv_sec_type(ifl->ifl_ehdr->e_ident[EI_OSABI],
1472*11993SAli.Bahrami@Sun.COM 			    ifl->ifl_ehdr->e_machine, ld_targ.t_m.m_sht_unwind,
1473*11993SAli.Bahrami@Sun.COM 			    CONV_FMT_ALT_CF, &inv_buf2));
1474*11993SAli.Bahrami@Sun.COM 			ofl->ofl_flags |= FLG_OF_FATAL;
1475*11993SAli.Bahrami@Sun.COM 			return (FALSE);
1476*11993SAli.Bahrami@Sun.COM 		case 'g':
1477*11993SAli.Bahrami@Sun.COM 			if (is_name_cmp(name, MSG_ORIG(MSG_SCN_GOT),
1478*11993SAli.Bahrami@Sun.COM 			    MSG_SCN_GOT_SIZE)) {
1479*11993SAli.Bahrami@Sun.COM 				*ident = ld_targ.t_id.id_null;
1480*11993SAli.Bahrami@Sun.COM 				done = TRUE;
1481*11993SAli.Bahrami@Sun.COM 				break;
1482*11993SAli.Bahrami@Sun.COM 			}
1483*11993SAli.Bahrami@Sun.COM 			if ((ld_targ.t_m.m_sht_unwind == SHT_PROGBITS) &&
1484*11993SAli.Bahrami@Sun.COM 			    is_name_cmp(name, MSG_ORIG(MSG_SCN_GCC_X_TBL),
1485*11993SAli.Bahrami@Sun.COM 			    MSG_SCN_GCC_X_TBL_SIZE)) {
1486*11993SAli.Bahrami@Sun.COM 				*ident = ld_targ.t_id.id_unwind;
1487*11993SAli.Bahrami@Sun.COM 				done = TRUE;
1488*11993SAli.Bahrami@Sun.COM 				break;
1489*11993SAli.Bahrami@Sun.COM 			}
1490*11993SAli.Bahrami@Sun.COM 			break;
1491*11993SAli.Bahrami@Sun.COM 		case 'p':
1492*11993SAli.Bahrami@Sun.COM 			if (is_name_cmp(name, MSG_ORIG(MSG_SCN_PLT),
1493*11993SAli.Bahrami@Sun.COM 			    MSG_SCN_PLT_SIZE)) {
1494*11993SAli.Bahrami@Sun.COM 				*ident = ld_targ.t_id.id_null;
1495*11993SAli.Bahrami@Sun.COM 				done = TRUE;
1496*11993SAli.Bahrami@Sun.COM 			}
1497*11993SAli.Bahrami@Sun.COM 			break;
1498*11993SAli.Bahrami@Sun.COM 		}
1499*11993SAli.Bahrami@Sun.COM 	}
1500*11993SAli.Bahrami@Sun.COM 	if (!done) {
1501*11993SAli.Bahrami@Sun.COM 		if (is_stab_index) {
1502*11993SAli.Bahrami@Sun.COM 			/*
1503*11993SAli.Bahrami@Sun.COM 			 * This is a work-around for x86 compilers that have
1504*11993SAli.Bahrami@Sun.COM 			 * set SHF_ALLOC for the .stab.index section.
1505*11993SAli.Bahrami@Sun.COM 			 *
1506*11993SAli.Bahrami@Sun.COM 			 * Because of this, make sure that the .stab.index
1507*11993SAli.Bahrami@Sun.COM 			 * does not end up as the last section in the text
1508*11993SAli.Bahrami@Sun.COM 			 * segment. Older linkers can produce segmentation
1509*11993SAli.Bahrami@Sun.COM 			 * violations when they strip (ld -s) against a
1510*11993SAli.Bahrami@Sun.COM 			 * shared object whose last section in the text
1511*11993SAli.Bahrami@Sun.COM 			 * segment is a .stab.
1512*11993SAli.Bahrami@Sun.COM 			 */
1513*11993SAli.Bahrami@Sun.COM 			*ident = ld_targ.t_id.id_interp;
1514*11993SAli.Bahrami@Sun.COM 		} else {
1515*11993SAli.Bahrami@Sun.COM 			*ident = ld_targ.t_id.id_data;
1516*11993SAli.Bahrami@Sun.COM 		}
1517*11993SAli.Bahrami@Sun.COM 	}
1518*11993SAli.Bahrami@Sun.COM 
1519*11993SAli.Bahrami@Sun.COM 	return (TRUE);
1520*11993SAli.Bahrami@Sun.COM }
1521*11993SAli.Bahrami@Sun.COM 
1522*11993SAli.Bahrami@Sun.COM /*
15230Sstevel@tonic-gate  * Process a progbits section.
15240Sstevel@tonic-gate  */
15251618Srie static uintptr_t
15260Sstevel@tonic-gate process_progbits(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
15270Sstevel@tonic-gate     Word ndx, int ident, Ofl_desc *ofl)
15280Sstevel@tonic-gate {
1529*11993SAli.Bahrami@Sun.COM 	Boolean		is_stab_index = FALSE;
15309085SAli.Bahrami@Sun.COM 	Word		is_flags = 0;
15319085SAli.Bahrami@Sun.COM 	uintptr_t	r;
15320Sstevel@tonic-gate 
15330Sstevel@tonic-gate 	/*
15340Sstevel@tonic-gate 	 * Never include .stab.excl sections in any output file.
15350Sstevel@tonic-gate 	 * If the -s flag has been specified strip any .stab sections.
15360Sstevel@tonic-gate 	 */
15370Sstevel@tonic-gate 	if (ident && (strncmp(name, MSG_ORIG(MSG_SCN_STAB),
15380Sstevel@tonic-gate 	    MSG_SCN_STAB_SIZE) == 0)) {
15390Sstevel@tonic-gate 		if ((ofl->ofl_flags & FLG_OF_STRIP) ||
15400Sstevel@tonic-gate 		    (strcmp((name + MSG_SCN_STAB_SIZE),
15414716Sab196087 		    MSG_ORIG(MSG_SCN_EXCL)) == 0))
15420Sstevel@tonic-gate 			return (1);
15430Sstevel@tonic-gate 
15440Sstevel@tonic-gate 		if (strcmp((name + MSG_SCN_STAB_SIZE),
15450Sstevel@tonic-gate 		    MSG_ORIG(MSG_SCN_INDEX)) == 0)
1546*11993SAli.Bahrami@Sun.COM 			is_stab_index = TRUE;
15470Sstevel@tonic-gate 	}
15480Sstevel@tonic-gate 
15490Sstevel@tonic-gate 	if ((ofl->ofl_flags & FLG_OF_STRIP) && ident) {
15500Sstevel@tonic-gate 		if ((strncmp(name, MSG_ORIG(MSG_SCN_DEBUG),
15510Sstevel@tonic-gate 		    MSG_SCN_DEBUG_SIZE) == 0) ||
15520Sstevel@tonic-gate 		    (strcmp(name, MSG_ORIG(MSG_SCN_LINE)) == 0))
15530Sstevel@tonic-gate 			return (1);
15540Sstevel@tonic-gate 	}
15550Sstevel@tonic-gate 
15560Sstevel@tonic-gate 	/*
15570Sstevel@tonic-gate 	 * Update the ident to reflect the type of section we've got.
15580Sstevel@tonic-gate 	 *
15590Sstevel@tonic-gate 	 * If there is any .plt or .got section to generate we'll be creating
15600Sstevel@tonic-gate 	 * our own version, so don't allow any input sections of these types to
15610Sstevel@tonic-gate 	 * be added to the output section list (why a relocatable object would
15620Sstevel@tonic-gate 	 * have a .plt or .got is a mystery, but stranger things have occurred).
15639085SAli.Bahrami@Sun.COM 	 *
15649085SAli.Bahrami@Sun.COM 	 * If there are any unwind sections, and this is a platform that uses
15659085SAli.Bahrami@Sun.COM 	 * SHT_PROGBITS for unwind sections, then set their ident to reflect
15669085SAli.Bahrami@Sun.COM 	 * that.
15670Sstevel@tonic-gate 	 */
15680Sstevel@tonic-gate 	if (ident) {
15699085SAli.Bahrami@Sun.COM 		if (shdr->sh_flags & SHF_TLS) {
15706206Sab196087 			ident = ld_targ.t_id.id_tls;
15719085SAli.Bahrami@Sun.COM 		} else if ((shdr->sh_flags & ~ALL_SHF_IGNORE) ==
15729085SAli.Bahrami@Sun.COM 		    (SHF_ALLOC | SHF_EXECINSTR)) {
15736206Sab196087 			ident = ld_targ.t_id.id_text;
15749085SAli.Bahrami@Sun.COM 		} else if (shdr->sh_flags & SHF_ALLOC) {
1575*11993SAli.Bahrami@Sun.COM 			if (process_progbits_alloc(name, ifl, shdr, ndx,
1576*11993SAli.Bahrami@Sun.COM 			    &ident, ofl, is_stab_index, &is_flags) == FALSE)
1577*11993SAli.Bahrami@Sun.COM 				return (S_ERROR);
1578*11993SAli.Bahrami@Sun.COM 		} else {
15796206Sab196087 			ident = ld_targ.t_id.id_note;
1580*11993SAli.Bahrami@Sun.COM 		}
15810Sstevel@tonic-gate 	}
15829085SAli.Bahrami@Sun.COM 
15839085SAli.Bahrami@Sun.COM 	r = process_section(name, ifl, shdr, scn, ndx, ident, ofl);
15849085SAli.Bahrami@Sun.COM 
15859085SAli.Bahrami@Sun.COM 	/*
15869085SAli.Bahrami@Sun.COM 	 * On success, process_section() creates an input section descriptor.
15879085SAli.Bahrami@Sun.COM 	 * Now that it exists, we can add any pending input section flags.
15889085SAli.Bahrami@Sun.COM 	 */
15899085SAli.Bahrami@Sun.COM 	if ((is_flags != 0) && (r == 1))
15909085SAli.Bahrami@Sun.COM 		ifl->ifl_isdesc[ndx]->is_flags |= is_flags;
15919085SAli.Bahrami@Sun.COM 
15929085SAli.Bahrami@Sun.COM 	return (r);
15930Sstevel@tonic-gate }
15940Sstevel@tonic-gate 
15950Sstevel@tonic-gate /*
15960Sstevel@tonic-gate  * Handles the SHT_SUNW_{DEBUG,DEBUGSTR) sections.
15970Sstevel@tonic-gate  */
15981618Srie static uintptr_t
15990Sstevel@tonic-gate process_debug(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
16000Sstevel@tonic-gate     Word ndx, int ident, Ofl_desc *ofl)
16010Sstevel@tonic-gate {
16020Sstevel@tonic-gate 	/*
16030Sstevel@tonic-gate 	 * Debug information is discarded when the 'ld -s' flag is invoked.
16040Sstevel@tonic-gate 	 */
16050Sstevel@tonic-gate 	if (ofl->ofl_flags & FLG_OF_STRIP) {
16060Sstevel@tonic-gate 		return (1);
16070Sstevel@tonic-gate 	}
16080Sstevel@tonic-gate 	return (process_progbits(name, ifl, shdr, scn, ndx, ident, ofl));
16090Sstevel@tonic-gate }
16100Sstevel@tonic-gate 
16110Sstevel@tonic-gate /*
16120Sstevel@tonic-gate  * Process a nobits section.
16130Sstevel@tonic-gate  */
16141618Srie static uintptr_t
16150Sstevel@tonic-gate process_nobits(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
16160Sstevel@tonic-gate     Word ndx, int ident, Ofl_desc *ofl)
16170Sstevel@tonic-gate {
16180Sstevel@tonic-gate 	if (ident) {
16190Sstevel@tonic-gate 		if (shdr->sh_flags & SHF_TLS)
16206206Sab196087 			ident = ld_targ.t_id.id_tlsbss;
16216206Sab196087 #if	defined(_ELF64)
16226206Sab196087 		else if ((shdr->sh_flags & SHF_AMD64_LARGE) &&
16236206Sab196087 		    (ld_targ.t_m.m_mach == EM_AMD64))
16246206Sab196087 			ident = ld_targ.t_id.id_lbss;
1625574Sseizo #endif
16260Sstevel@tonic-gate 		else
16276206Sab196087 			ident = ld_targ.t_id.id_bss;
16280Sstevel@tonic-gate 	}
16290Sstevel@tonic-gate 	return (process_section(name, ifl, shdr, scn, ndx, ident, ofl));
16300Sstevel@tonic-gate }
16310Sstevel@tonic-gate 
16320Sstevel@tonic-gate /*
16330Sstevel@tonic-gate  * Process a SHT_*_ARRAY section.
16340Sstevel@tonic-gate  */
16351618Srie static uintptr_t
16360Sstevel@tonic-gate process_array(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
16370Sstevel@tonic-gate     Word ndx, int ident, Ofl_desc *ofl)
16380Sstevel@tonic-gate {
16397463SRod.Evans@Sun.COM 	uintptr_t	error;
16400Sstevel@tonic-gate 
16410Sstevel@tonic-gate 	if (ident)
16426206Sab196087 		ident = ld_targ.t_id.id_array;
16430Sstevel@tonic-gate 
16447463SRod.Evans@Sun.COM 	error = process_section(name, ifl, shdr, scn, ndx, ident, ofl);
16457463SRod.Evans@Sun.COM 	if ((error == 0) || (error == S_ERROR))
16467463SRod.Evans@Sun.COM 		return (error);
16477463SRod.Evans@Sun.COM 
16487463SRod.Evans@Sun.COM 	return (1);
16497463SRod.Evans@Sun.COM }
16500Sstevel@tonic-gate 
16517463SRod.Evans@Sun.COM static uintptr_t
16527463SRod.Evans@Sun.COM /* ARGSUSED1 */
16537463SRod.Evans@Sun.COM array_process(Is_desc *isc, Ifl_desc *ifl, Ofl_desc *ofl)
16547463SRod.Evans@Sun.COM {
16557463SRod.Evans@Sun.COM 	Os_desc	*osp;
16567463SRod.Evans@Sun.COM 	Shdr	*shdr;
16577463SRod.Evans@Sun.COM 
16587463SRod.Evans@Sun.COM 	if ((isc == NULL) || ((osp = isc->is_osdesc) == NULL))
16590Sstevel@tonic-gate 		return (0);
16600Sstevel@tonic-gate 
16617463SRod.Evans@Sun.COM 	shdr = isc->is_shdr;
16627463SRod.Evans@Sun.COM 
16630Sstevel@tonic-gate 	if ((shdr->sh_type == SHT_FINI_ARRAY) &&
16647463SRod.Evans@Sun.COM 	    (ofl->ofl_osfiniarray == NULL))
16650Sstevel@tonic-gate 		ofl->ofl_osfiniarray = osp;
16660Sstevel@tonic-gate 	else if ((shdr->sh_type == SHT_INIT_ARRAY) &&
16677463SRod.Evans@Sun.COM 	    (ofl->ofl_osinitarray == NULL))
16680Sstevel@tonic-gate 		ofl->ofl_osinitarray = osp;
16690Sstevel@tonic-gate 	else if ((shdr->sh_type == SHT_PREINIT_ARRAY) &&
16707463SRod.Evans@Sun.COM 	    (ofl->ofl_ospreinitarray == NULL))
16710Sstevel@tonic-gate 		ofl->ofl_ospreinitarray = osp;
16720Sstevel@tonic-gate 
16730Sstevel@tonic-gate 	return (1);
16740Sstevel@tonic-gate }
16750Sstevel@tonic-gate 
16760Sstevel@tonic-gate /*
16770Sstevel@tonic-gate  * Process a SHT_SYMTAB_SHNDX section.
16780Sstevel@tonic-gate  */
16791618Srie static uintptr_t
16800Sstevel@tonic-gate process_sym_shndx(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
16810Sstevel@tonic-gate     Word ndx, int ident, Ofl_desc *ofl)
16820Sstevel@tonic-gate {
16830Sstevel@tonic-gate 	if (process_input(name, ifl, shdr, scn, ndx, ident, ofl) == S_ERROR)
16840Sstevel@tonic-gate 		return (S_ERROR);
16850Sstevel@tonic-gate 
16860Sstevel@tonic-gate 	/*
16870Sstevel@tonic-gate 	 * Have we already seen the related SYMTAB - if so verify it now.
16880Sstevel@tonic-gate 	 */
16890Sstevel@tonic-gate 	if (shdr->sh_link < ndx) {
16900Sstevel@tonic-gate 		Is_desc	*isp = ifl->ifl_isdesc[shdr->sh_link];
16910Sstevel@tonic-gate 
169210792SRod.Evans@Sun.COM 		if ((isp == NULL) || ((isp->is_shdr->sh_type != SHT_SYMTAB) &&
16930Sstevel@tonic-gate 		    (isp->is_shdr->sh_type != SHT_DYNSYM))) {
16941618Srie 			eprintf(ofl->ofl_lml, ERR_FATAL,
16959878SAli.Bahrami@Sun.COM 			    MSG_INTL(MSG_FIL_INVSHLINK), ifl->ifl_name,
16969878SAli.Bahrami@Sun.COM 			    EC_WORD(ndx), name, EC_XWORD(shdr->sh_link));
16970Sstevel@tonic-gate 			return (S_ERROR);
16980Sstevel@tonic-gate 		}
16990Sstevel@tonic-gate 		isp->is_symshndx = ifl->ifl_isdesc[ndx];
17000Sstevel@tonic-gate 	}
17010Sstevel@tonic-gate 	return (1);
17020Sstevel@tonic-gate }
17030Sstevel@tonic-gate 
17040Sstevel@tonic-gate /*
17050Sstevel@tonic-gate  * Final processing for SHT_SYMTAB_SHNDX section.
17060Sstevel@tonic-gate  */
17071618Srie static uintptr_t
17080Sstevel@tonic-gate /* ARGSUSED2 */
17090Sstevel@tonic-gate sym_shndx_process(Is_desc *isc, Ifl_desc *ifl, Ofl_desc *ofl)
17100Sstevel@tonic-gate {
17110Sstevel@tonic-gate 	if (isc->is_shdr->sh_link > isc->is_scnndx) {
17120Sstevel@tonic-gate 		Is_desc	*isp = ifl->ifl_isdesc[isc->is_shdr->sh_link];
17130Sstevel@tonic-gate 
171410792SRod.Evans@Sun.COM 		if ((isp == NULL) || ((isp->is_shdr->sh_type != SHT_SYMTAB) &&
17150Sstevel@tonic-gate 		    (isp->is_shdr->sh_type != SHT_DYNSYM))) {
17161618Srie 			eprintf(ofl->ofl_lml, ERR_FATAL,
17171618Srie 			    MSG_INTL(MSG_FIL_INVSHLINK), isc->is_file->ifl_name,
17189878SAli.Bahrami@Sun.COM 			    EC_WORD(isc->is_scnndx), isc->is_name,
17199878SAli.Bahrami@Sun.COM 			    EC_XWORD(isc->is_shdr->sh_link));
17200Sstevel@tonic-gate 			return (S_ERROR);
17210Sstevel@tonic-gate 		}
17220Sstevel@tonic-gate 		isp->is_symshndx = isc;
17230Sstevel@tonic-gate 	}
17240Sstevel@tonic-gate 	return (1);
17250Sstevel@tonic-gate }
17260Sstevel@tonic-gate 
17270Sstevel@tonic-gate /*
17280Sstevel@tonic-gate  * Process .dynamic section from a relocatable object.
17290Sstevel@tonic-gate  *
17300Sstevel@tonic-gate  * Note: That the .dynamic section is only considered interesting when
17310Sstevel@tonic-gate  *	 dlopen()ing a relocatable object (thus FLG_OF1_RELDYN can only get
17320Sstevel@tonic-gate  *	 set when libld is called from ld.so.1).
17330Sstevel@tonic-gate  */
17340Sstevel@tonic-gate /*ARGSUSED*/
17351618Srie static uintptr_t
17360Sstevel@tonic-gate process_rel_dynamic(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
17370Sstevel@tonic-gate     Word ndx, int ident, Ofl_desc *ofl)
17380Sstevel@tonic-gate {
17390Sstevel@tonic-gate 	Dyn		*dyn;
17400Sstevel@tonic-gate 	Elf_Scn		*strscn;
17410Sstevel@tonic-gate 	Elf_Data	*dp;
17420Sstevel@tonic-gate 	char		*str;
17430Sstevel@tonic-gate 
17440Sstevel@tonic-gate 	/*
17450Sstevel@tonic-gate 	 * Process .dynamic sections from relocatable objects ?
17460Sstevel@tonic-gate 	 */
17470Sstevel@tonic-gate 	if ((ofl->ofl_flags1 & FLG_OF1_RELDYN) == 0)
17480Sstevel@tonic-gate 		return (1);
17490Sstevel@tonic-gate 
17500Sstevel@tonic-gate 	/*
17510Sstevel@tonic-gate 	 * Find the string section associated with the .dynamic section.
17520Sstevel@tonic-gate 	 */
17530Sstevel@tonic-gate 	if ((strscn = elf_getscn(ifl->ifl_elf, shdr->sh_link)) == NULL) {
17541618Srie 		eprintf(ofl->ofl_lml, ERR_ELF, MSG_INTL(MSG_ELF_GETSCN),
17551618Srie 		    ifl->ifl_name);
17560Sstevel@tonic-gate 		ofl->ofl_flags |= FLG_OF_FATAL;
17570Sstevel@tonic-gate 		return (0);
17580Sstevel@tonic-gate 	}
17590Sstevel@tonic-gate 	dp = elf_getdata(strscn, NULL);
17600Sstevel@tonic-gate 	str = (char *)dp->d_buf;
17610Sstevel@tonic-gate 
17620Sstevel@tonic-gate 	/*
17630Sstevel@tonic-gate 	 * And get the .dynamic data
17640Sstevel@tonic-gate 	 */
17650Sstevel@tonic-gate 	dp = elf_getdata(scn, NULL);
17660Sstevel@tonic-gate 
17670Sstevel@tonic-gate 	for (dyn = (Dyn *)dp->d_buf; dyn->d_tag != DT_NULL; dyn++) {
17681109Srie 		Ifl_desc	*difl;
17690Sstevel@tonic-gate 
17700Sstevel@tonic-gate 		switch (dyn->d_tag) {
17710Sstevel@tonic-gate 		case DT_NEEDED:
17720Sstevel@tonic-gate 		case DT_USED:
17739131SRod.Evans@Sun.COM 			if (((difl = libld_calloc(1,
17749131SRod.Evans@Sun.COM 			    sizeof (Ifl_desc))) == NULL) ||
17759131SRod.Evans@Sun.COM 			    (aplist_append(&ofl->ofl_sos, difl,
17769131SRod.Evans@Sun.COM 			    AL_CNT_OFL_LIBS) == NULL))
17770Sstevel@tonic-gate 				return (S_ERROR);
17781109Srie 
17791109Srie 			difl->ifl_name = MSG_ORIG(MSG_STR_DYNAMIC);
17801109Srie 			difl->ifl_soname = str + (size_t)dyn->d_un.d_val;
17811109Srie 			difl->ifl_flags = FLG_IF_NEEDSTR;
17820Sstevel@tonic-gate 			break;
17830Sstevel@tonic-gate 		case DT_RPATH:
17840Sstevel@tonic-gate 		case DT_RUNPATH:
17850Sstevel@tonic-gate 			if ((ofl->ofl_rpath = add_string(ofl->ofl_rpath,
17860Sstevel@tonic-gate 			    (str + (size_t)dyn->d_un.d_val))) ==
17870Sstevel@tonic-gate 			    (const char *)S_ERROR)
17880Sstevel@tonic-gate 				return (S_ERROR);
17890Sstevel@tonic-gate 			break;
17904716Sab196087 		case DT_VERSYM:
17914716Sab196087 			/*
17924716Sab196087 			 * The Solaris ld does not put DT_VERSYM in the
17934716Sab196087 			 * dynamic section. If the object has DT_VERSYM,
17944716Sab196087 			 * then it must have been produced by the GNU ld,
17954716Sab196087 			 * and is using the GNU style of versioning.
17964716Sab196087 			 */
17974716Sab196087 			ifl->ifl_flags |= FLG_IF_GNUVER;
17984716Sab196087 			break;
17990Sstevel@tonic-gate 		}
18000Sstevel@tonic-gate 	}
18010Sstevel@tonic-gate 	return (1);
18020Sstevel@tonic-gate }
18030Sstevel@tonic-gate 
18040Sstevel@tonic-gate /*
18050Sstevel@tonic-gate  * Expand implicit references.  Dependencies can be specified in terms of the
180611827SRod.Evans@Sun.COM  * $ORIGIN, $MACHINE, $PLATFORM, $OSREL and $OSNAME tokens, either from their
180711827SRod.Evans@Sun.COM  * needed name, or via a runpath.  In addition runpaths may also specify the
180811827SRod.Evans@Sun.COM  * $ISALIST token.
18090Sstevel@tonic-gate  *
18101109Srie  * Probably the most common reference to explicit dependencies (via -L) will be
18110Sstevel@tonic-gate  * sufficient to find any associated implicit dependencies, but just in case we
18120Sstevel@tonic-gate  * expand any occurrence of these known tokens here.
18130Sstevel@tonic-gate  *
18140Sstevel@tonic-gate  * Note, if any errors occur we simply return the original name.
18150Sstevel@tonic-gate  *
18160Sstevel@tonic-gate  * This code is remarkably similar to expand() in rtld/common/paths.c.
18170Sstevel@tonic-gate  */
181811827SRod.Evans@Sun.COM static char		*machine = NULL;
181911827SRod.Evans@Sun.COM static size_t		machine_sz = 0;
182010792SRod.Evans@Sun.COM static char		*platform = NULL;
18210Sstevel@tonic-gate static size_t		platform_sz = 0;
182210792SRod.Evans@Sun.COM static Isa_desc		*isa = NULL;
182310792SRod.Evans@Sun.COM static Uts_desc		*uts = NULL;
18240Sstevel@tonic-gate 
18250Sstevel@tonic-gate static char *
18260Sstevel@tonic-gate expand(const char *parent, const char *name, char **next)
18270Sstevel@tonic-gate {
18280Sstevel@tonic-gate 	char		_name[PATH_MAX], *nptr, *_next;
18290Sstevel@tonic-gate 	const char	*optr;
18300Sstevel@tonic-gate 	size_t		nrem = PATH_MAX - 1;
18310Sstevel@tonic-gate 	int		expanded = 0, _expanded, isaflag = 0;
18320Sstevel@tonic-gate 
18330Sstevel@tonic-gate 	optr = name;
18340Sstevel@tonic-gate 	nptr = _name;
18350Sstevel@tonic-gate 
18360Sstevel@tonic-gate 	while (*optr) {
18370Sstevel@tonic-gate 		if (nrem == 0)
18380Sstevel@tonic-gate 			return ((char *)name);
18390Sstevel@tonic-gate 
18400Sstevel@tonic-gate 		if (*optr != '$') {
18410Sstevel@tonic-gate 			*nptr++ = *optr++, nrem--;
18420Sstevel@tonic-gate 			continue;
18430Sstevel@tonic-gate 		}
18440Sstevel@tonic-gate 
18450Sstevel@tonic-gate 		_expanded = 0;
18460Sstevel@tonic-gate 
18470Sstevel@tonic-gate 		if (strncmp(optr, MSG_ORIG(MSG_STR_ORIGIN),
18480Sstevel@tonic-gate 		    MSG_STR_ORIGIN_SIZE) == 0) {
18490Sstevel@tonic-gate 			char *eptr;
18500Sstevel@tonic-gate 
18510Sstevel@tonic-gate 			/*
18520Sstevel@tonic-gate 			 * For $ORIGIN, expansion is really just a concatenation
18530Sstevel@tonic-gate 			 * of the parents directory name.  For example, an
18541109Srie 			 * explicit dependency foo/bar/lib1.so with a dependency
18550Sstevel@tonic-gate 			 * on $ORIGIN/lib2.so would be expanded to
18560Sstevel@tonic-gate 			 * foo/bar/lib2.so.
18570Sstevel@tonic-gate 			 */
185810792SRod.Evans@Sun.COM 			if ((eptr = strrchr(parent, '/')) == NULL) {
18590Sstevel@tonic-gate 				*nptr++ = '.';
18600Sstevel@tonic-gate 				nrem--;
18610Sstevel@tonic-gate 			} else {
18620Sstevel@tonic-gate 				size_t	len = eptr - parent;
18630Sstevel@tonic-gate 
18640Sstevel@tonic-gate 				if (len >= nrem)
18650Sstevel@tonic-gate 					return ((char *)name);
18660Sstevel@tonic-gate 
18670Sstevel@tonic-gate 				(void) strncpy(nptr, parent, len);
18680Sstevel@tonic-gate 				nptr = nptr + len;
18690Sstevel@tonic-gate 				nrem -= len;
18700Sstevel@tonic-gate 			}
18710Sstevel@tonic-gate 			optr += MSG_STR_ORIGIN_SIZE;
18720Sstevel@tonic-gate 			expanded = _expanded = 1;
18730Sstevel@tonic-gate 
187411827SRod.Evans@Sun.COM 		} else if (strncmp(optr, MSG_ORIG(MSG_STR_MACHINE),
187511827SRod.Evans@Sun.COM 		    MSG_STR_MACHINE_SIZE) == 0) {
187611827SRod.Evans@Sun.COM 			/*
187711827SRod.Evans@Sun.COM 			 * Establish the machine from sysconf - like uname -i.
187811827SRod.Evans@Sun.COM 			 */
187911827SRod.Evans@Sun.COM 			if ((machine == NULL) && (machine_sz == 0)) {
188011827SRod.Evans@Sun.COM 				char	info[SYS_NMLN];
188111827SRod.Evans@Sun.COM 				long	size;
188211827SRod.Evans@Sun.COM 
188311827SRod.Evans@Sun.COM 				size = sysinfo(SI_MACHINE, info, SYS_NMLN);
188411827SRod.Evans@Sun.COM 				if ((size != -1) &&
188511827SRod.Evans@Sun.COM 				    (machine = libld_malloc((size_t)size))) {
188611827SRod.Evans@Sun.COM 					(void) strcpy(machine, info);
188711827SRod.Evans@Sun.COM 					machine_sz = (size_t)size - 1;
188811827SRod.Evans@Sun.COM 				} else
188911827SRod.Evans@Sun.COM 					machine_sz = 1;
189011827SRod.Evans@Sun.COM 			}
189111827SRod.Evans@Sun.COM 			if (machine) {
189211827SRod.Evans@Sun.COM 				if (machine_sz >= nrem)
189311827SRod.Evans@Sun.COM 					return ((char *)name);
189411827SRod.Evans@Sun.COM 
189511827SRod.Evans@Sun.COM 				(void) strncpy(nptr, machine, machine_sz);
189611827SRod.Evans@Sun.COM 				nptr = nptr + machine_sz;
189711827SRod.Evans@Sun.COM 				nrem -= machine_sz;
189811827SRod.Evans@Sun.COM 
189911827SRod.Evans@Sun.COM 				optr += MSG_STR_MACHINE_SIZE;
190011827SRod.Evans@Sun.COM 				expanded = _expanded = 1;
190111827SRod.Evans@Sun.COM 			}
190211827SRod.Evans@Sun.COM 
19030Sstevel@tonic-gate 		} else if (strncmp(optr, MSG_ORIG(MSG_STR_PLATFORM),
19040Sstevel@tonic-gate 		    MSG_STR_PLATFORM_SIZE) == 0) {
19050Sstevel@tonic-gate 			/*
19060Sstevel@tonic-gate 			 * Establish the platform from sysconf - like uname -i.
19070Sstevel@tonic-gate 			 */
190810792SRod.Evans@Sun.COM 			if ((platform == NULL) && (platform_sz == 0)) {
19090Sstevel@tonic-gate 				char	info[SYS_NMLN];
19100Sstevel@tonic-gate 				long	size;
19110Sstevel@tonic-gate 
19120Sstevel@tonic-gate 				size = sysinfo(SI_PLATFORM, info, SYS_NMLN);
19130Sstevel@tonic-gate 				if ((size != -1) &&
19140Sstevel@tonic-gate 				    (platform = libld_malloc((size_t)size))) {
19150Sstevel@tonic-gate 					(void) strcpy(platform, info);
19160Sstevel@tonic-gate 					platform_sz = (size_t)size - 1;
19170Sstevel@tonic-gate 				} else
19180Sstevel@tonic-gate 					platform_sz = 1;
19190Sstevel@tonic-gate 			}
192010792SRod.Evans@Sun.COM 			if (platform) {
19210Sstevel@tonic-gate 				if (platform_sz >= nrem)
19220Sstevel@tonic-gate 					return ((char *)name);
19230Sstevel@tonic-gate 
19240Sstevel@tonic-gate 				(void) strncpy(nptr, platform, platform_sz);
19250Sstevel@tonic-gate 				nptr = nptr + platform_sz;
19260Sstevel@tonic-gate 				nrem -= platform_sz;
19270Sstevel@tonic-gate 
19280Sstevel@tonic-gate 				optr += MSG_STR_PLATFORM_SIZE;
19290Sstevel@tonic-gate 				expanded = _expanded = 1;
19300Sstevel@tonic-gate 			}
19310Sstevel@tonic-gate 
19320Sstevel@tonic-gate 		} else if (strncmp(optr, MSG_ORIG(MSG_STR_OSNAME),
19330Sstevel@tonic-gate 		    MSG_STR_OSNAME_SIZE) == 0) {
19340Sstevel@tonic-gate 			/*
19350Sstevel@tonic-gate 			 * Establish the os name - like uname -s.
19360Sstevel@tonic-gate 			 */
193710792SRod.Evans@Sun.COM 			if (uts == NULL)
19380Sstevel@tonic-gate 				uts = conv_uts();
19390Sstevel@tonic-gate 
19400Sstevel@tonic-gate 			if (uts && uts->uts_osnamesz) {
19410Sstevel@tonic-gate 				if (uts->uts_osnamesz >= nrem)
19420Sstevel@tonic-gate 					return ((char *)name);
19430Sstevel@tonic-gate 
19440Sstevel@tonic-gate 				(void) strncpy(nptr, uts->uts_osname,
19450Sstevel@tonic-gate 				    uts->uts_osnamesz);
19460Sstevel@tonic-gate 				nptr = nptr + uts->uts_osnamesz;
19470Sstevel@tonic-gate 				nrem -= uts->uts_osnamesz;
19480Sstevel@tonic-gate 
19490Sstevel@tonic-gate 				optr += MSG_STR_OSNAME_SIZE;
19500Sstevel@tonic-gate 				expanded = _expanded = 1;
19510Sstevel@tonic-gate 			}
19520Sstevel@tonic-gate 
19530Sstevel@tonic-gate 		} else if (strncmp(optr, MSG_ORIG(MSG_STR_OSREL),
19540Sstevel@tonic-gate 		    MSG_STR_OSREL_SIZE) == 0) {
19550Sstevel@tonic-gate 			/*
19560Sstevel@tonic-gate 			 * Establish the os release - like uname -r.
19570Sstevel@tonic-gate 			 */
195810792SRod.Evans@Sun.COM 			if (uts == NULL)
19590Sstevel@tonic-gate 				uts = conv_uts();
19600Sstevel@tonic-gate 
19610Sstevel@tonic-gate 			if (uts && uts->uts_osrelsz) {
19620Sstevel@tonic-gate 				if (uts->uts_osrelsz >= nrem)
19630Sstevel@tonic-gate 					return ((char *)name);
19640Sstevel@tonic-gate 
19650Sstevel@tonic-gate 				(void) strncpy(nptr, uts->uts_osrel,
19660Sstevel@tonic-gate 				    uts->uts_osrelsz);
19670Sstevel@tonic-gate 				nptr = nptr + uts->uts_osrelsz;
19680Sstevel@tonic-gate 				nrem -= uts->uts_osrelsz;
19690Sstevel@tonic-gate 
19700Sstevel@tonic-gate 				optr += MSG_STR_OSREL_SIZE;
19710Sstevel@tonic-gate 				expanded = _expanded = 1;
19720Sstevel@tonic-gate 			}
19730Sstevel@tonic-gate 
19740Sstevel@tonic-gate 		} else if ((strncmp(optr, MSG_ORIG(MSG_STR_ISALIST),
19750Sstevel@tonic-gate 		    MSG_STR_ISALIST_SIZE) == 0) && next && (isaflag++ == 0)) {
19760Sstevel@tonic-gate 			/*
19770Sstevel@tonic-gate 			 * Establish instruction sets from sysconf.  Note that
19780Sstevel@tonic-gate 			 * this is only meaningful from runpaths.
19790Sstevel@tonic-gate 			 */
198010792SRod.Evans@Sun.COM 			if (isa == NULL)
19810Sstevel@tonic-gate 				isa = conv_isalist();
19820Sstevel@tonic-gate 
19830Sstevel@tonic-gate 			if (isa && isa->isa_listsz &&
19840Sstevel@tonic-gate 			    (nrem > isa->isa_opt->isa_namesz)) {
19850Sstevel@tonic-gate 				size_t		mlen, tlen, hlen = optr - name;
19860Sstevel@tonic-gate 				size_t		no;
19870Sstevel@tonic-gate 				char		*lptr;
19880Sstevel@tonic-gate 				Isa_opt		*opt = isa->isa_opt;
19890Sstevel@tonic-gate 
19900Sstevel@tonic-gate 				(void) strncpy(nptr, opt->isa_name,
19910Sstevel@tonic-gate 				    opt->isa_namesz);
19920Sstevel@tonic-gate 				nptr = nptr + opt->isa_namesz;
19930Sstevel@tonic-gate 				nrem -= opt->isa_namesz;
19940Sstevel@tonic-gate 
19950Sstevel@tonic-gate 				optr += MSG_STR_ISALIST_SIZE;
19960Sstevel@tonic-gate 				expanded = _expanded = 1;
19970Sstevel@tonic-gate 
19980Sstevel@tonic-gate 				tlen = strlen(optr);
19990Sstevel@tonic-gate 
20000Sstevel@tonic-gate 				/*
20010Sstevel@tonic-gate 				 * As ISALIST expands to a number of elements,
20020Sstevel@tonic-gate 				 * establish a new list to return to the caller.
20030Sstevel@tonic-gate 				 * This will contain the present path being
20040Sstevel@tonic-gate 				 * processed redefined for each isalist option,
20050Sstevel@tonic-gate 				 * plus the original remaining list entries.
20060Sstevel@tonic-gate 				 */
20070Sstevel@tonic-gate 				mlen = ((hlen + tlen) * (isa->isa_optno - 1)) +
20080Sstevel@tonic-gate 				    isa->isa_listsz - opt->isa_namesz;
20090Sstevel@tonic-gate 				if (*next)
20104716Sab196087 					mlen += strlen(*next);
201110792SRod.Evans@Sun.COM 				if ((_next = lptr = libld_malloc(mlen)) == NULL)
20120Sstevel@tonic-gate 					return (0);
20130Sstevel@tonic-gate 
20140Sstevel@tonic-gate 				for (no = 1, opt++; no < isa->isa_optno;
20150Sstevel@tonic-gate 				    no++, opt++) {
20160Sstevel@tonic-gate 					(void) strncpy(lptr, name, hlen);
20170Sstevel@tonic-gate 					lptr = lptr + hlen;
20180Sstevel@tonic-gate 					(void) strncpy(lptr, opt->isa_name,
20190Sstevel@tonic-gate 					    opt->isa_namesz);
20200Sstevel@tonic-gate 					lptr = lptr + opt->isa_namesz;
20210Sstevel@tonic-gate 					(void) strncpy(lptr, optr, tlen);
20220Sstevel@tonic-gate 					lptr = lptr + tlen;
20230Sstevel@tonic-gate 					*lptr++ = ':';
20240Sstevel@tonic-gate 				}
20250Sstevel@tonic-gate 				if (*next)
20260Sstevel@tonic-gate 					(void) strcpy(lptr, *next);
20270Sstevel@tonic-gate 				else
20280Sstevel@tonic-gate 					*--lptr = '\0';
20290Sstevel@tonic-gate 			}
20300Sstevel@tonic-gate 		}
20310Sstevel@tonic-gate 
20320Sstevel@tonic-gate 		/*
20330Sstevel@tonic-gate 		 * If no expansion occurred skip the $ and continue.
20340Sstevel@tonic-gate 		 */
20350Sstevel@tonic-gate 		if (_expanded == 0)
20360Sstevel@tonic-gate 			*nptr++ = *optr++, nrem--;
20370Sstevel@tonic-gate 	}
20380Sstevel@tonic-gate 
20390Sstevel@tonic-gate 	/*
20400Sstevel@tonic-gate 	 * If any ISALIST processing has occurred not only do we return the
20410Sstevel@tonic-gate 	 * expanded node we're presently working on, but we must also update the
20420Sstevel@tonic-gate 	 * remaining list so that it is effectively prepended with this node
20430Sstevel@tonic-gate 	 * expanded to all remaining isalist options.  Note that we can only
20440Sstevel@tonic-gate 	 * handle one ISALIST per node.  For more than one ISALIST to be
20450Sstevel@tonic-gate 	 * processed we'd need a better algorithm than above to replace the
20460Sstevel@tonic-gate 	 * newly generated list.  Whether we want to encourage the number of
20470Sstevel@tonic-gate 	 * pathname permutations this would provide is another question. So, for
20480Sstevel@tonic-gate 	 * now if more than one ISALIST is encountered we return the original
20490Sstevel@tonic-gate 	 * node untouched.
20500Sstevel@tonic-gate 	 */
20510Sstevel@tonic-gate 	if (isaflag) {
20520Sstevel@tonic-gate 		if (isaflag == 1)
20530Sstevel@tonic-gate 			*next = _next;
20540Sstevel@tonic-gate 		else
20550Sstevel@tonic-gate 			return ((char *)name);
20560Sstevel@tonic-gate 	}
20570Sstevel@tonic-gate 
20580Sstevel@tonic-gate 	*nptr = '\0';
20590Sstevel@tonic-gate 
20600Sstevel@tonic-gate 	if (expanded) {
206110792SRod.Evans@Sun.COM 		if ((nptr = libld_malloc(strlen(_name) + 1)) == NULL)
20620Sstevel@tonic-gate 			return ((char *)name);
20630Sstevel@tonic-gate 		(void) strcpy(nptr, _name);
20640Sstevel@tonic-gate 		return (nptr);
20650Sstevel@tonic-gate 	}
20660Sstevel@tonic-gate 	return ((char *)name);
20670Sstevel@tonic-gate }
20680Sstevel@tonic-gate 
20690Sstevel@tonic-gate /*
20704716Sab196087  * The Solaris ld does not put DT_VERSYM in the dynamic section, but the
20714716Sab196087  * GNU ld does, and it is used by the runtime linker to implement their
20724716Sab196087  * versioning scheme. Use this fact to determine if the sharable object
20734716Sab196087  * was produced by the GNU ld rather than the Solaris one, and to set
20744716Sab196087  * FLG_IF_GNUVER if so. This needs to be done before the symbols are
20754716Sab196087  * processed, since the answer determines whether we interpret the
20764716Sab196087  * symbols versions according to Solaris or GNU rules.
20774716Sab196087  */
20784716Sab196087 /*ARGSUSED*/
20794716Sab196087 static uintptr_t
20804716Sab196087 process_dynamic_isgnu(const char *name, Ifl_desc *ifl, Shdr *shdr,
20814716Sab196087     Elf_Scn *scn, Word ndx, int ident, Ofl_desc *ofl)
20824716Sab196087 {
20834716Sab196087 	Dyn		*dyn;
20844716Sab196087 	Elf_Data	*dp;
20857463SRod.Evans@Sun.COM 	uintptr_t	error;
20864716Sab196087 
20877463SRod.Evans@Sun.COM 	error = process_section(name, ifl, shdr, scn, ndx, ident, ofl);
20887463SRod.Evans@Sun.COM 	if ((error == 0) || (error == S_ERROR))
20897463SRod.Evans@Sun.COM 		return (error);
20904716Sab196087 
20914716Sab196087 	/* Get the .dynamic data */
20924716Sab196087 	dp = elf_getdata(scn, NULL);
20934716Sab196087 
20944716Sab196087 	for (dyn = (Dyn *)dp->d_buf; dyn->d_tag != DT_NULL; dyn++) {
20954716Sab196087 		if (dyn->d_tag == DT_VERSYM) {
20964716Sab196087 			ifl->ifl_flags |= FLG_IF_GNUVER;
20974716Sab196087 			break;
20984716Sab196087 		}
20994716Sab196087 	}
21004716Sab196087 	return (1);
21014716Sab196087 }
21024716Sab196087 
21034716Sab196087 /*
21040Sstevel@tonic-gate  * Process a dynamic section.  If we are processing an explicit shared object
21050Sstevel@tonic-gate  * then we need to determine if it has a recorded SONAME, if so, this name will
21060Sstevel@tonic-gate  * be recorded in the output file being generated as the NEEDED entry rather
21070Sstevel@tonic-gate  * than the shared objects filename itself.
21080Sstevel@tonic-gate  * If the mode of the link-edit indicates that no undefined symbols should
21090Sstevel@tonic-gate  * remain, then we also need to build up a list of any additional shared object
21100Sstevel@tonic-gate  * dependencies this object may have.  In this case save any NEEDED entries
21110Sstevel@tonic-gate  * together with any associated run-path specifications.  This information is
21120Sstevel@tonic-gate  * recorded on the `ofl_soneed' list and will be analyzed after all explicit
21130Sstevel@tonic-gate  * file processing has been completed (refer finish_libs()).
21140Sstevel@tonic-gate  */
21151618Srie static uintptr_t
21160Sstevel@tonic-gate process_dynamic(Is_desc *isc, Ifl_desc *ifl, Ofl_desc *ofl)
21170Sstevel@tonic-gate {
21180Sstevel@tonic-gate 	Dyn		*data, *dyn;
21190Sstevel@tonic-gate 	char		*str, *rpath = NULL;
21200Sstevel@tonic-gate 	const char	*soname, *needed;
21210Sstevel@tonic-gate 
21220Sstevel@tonic-gate 	data = (Dyn *)isc->is_indata->d_buf;
21230Sstevel@tonic-gate 	str = (char *)ifl->ifl_isdesc[isc->is_shdr->sh_link]->is_indata->d_buf;
21240Sstevel@tonic-gate 
21250Sstevel@tonic-gate 	/*
21260Sstevel@tonic-gate 	 * First loop through the dynamic section looking for a run path.
21270Sstevel@tonic-gate 	 */
21280Sstevel@tonic-gate 	if (ofl->ofl_flags & (FLG_OF_NOUNDEF | FLG_OF_SYMBOLIC)) {
21290Sstevel@tonic-gate 		for (dyn = data; dyn->d_tag != DT_NULL; dyn++) {
21300Sstevel@tonic-gate 			if ((dyn->d_tag != DT_RPATH) &&
21310Sstevel@tonic-gate 			    (dyn->d_tag != DT_RUNPATH))
21320Sstevel@tonic-gate 				continue;
21330Sstevel@tonic-gate 			if ((rpath = str + (size_t)dyn->d_un.d_val) == NULL)
21340Sstevel@tonic-gate 				continue;
21350Sstevel@tonic-gate 			break;
21360Sstevel@tonic-gate 		}
21370Sstevel@tonic-gate 	}
21380Sstevel@tonic-gate 
21390Sstevel@tonic-gate 	/*
21400Sstevel@tonic-gate 	 * Now look for any needed dependencies (which may use the rpath)
21410Sstevel@tonic-gate 	 * or a new SONAME.
21420Sstevel@tonic-gate 	 */
21430Sstevel@tonic-gate 	for (dyn = data; dyn->d_tag != DT_NULL; dyn++) {
21440Sstevel@tonic-gate 		if (dyn->d_tag == DT_SONAME) {
21450Sstevel@tonic-gate 			if ((soname = str + (size_t)dyn->d_un.d_val) == NULL)
21460Sstevel@tonic-gate 				continue;
21470Sstevel@tonic-gate 
21480Sstevel@tonic-gate 			/*
21490Sstevel@tonic-gate 			 * Update the input file structure with this new name.
21500Sstevel@tonic-gate 			 */
21510Sstevel@tonic-gate 			ifl->ifl_soname = soname;
21520Sstevel@tonic-gate 
21530Sstevel@tonic-gate 		} else if ((dyn->d_tag == DT_NEEDED) ||
21540Sstevel@tonic-gate 		    (dyn->d_tag == DT_USED)) {
21559131SRod.Evans@Sun.COM 			Sdf_desc	*sdf;
21569131SRod.Evans@Sun.COM 
21570Sstevel@tonic-gate 			if (!(ofl->ofl_flags &
21580Sstevel@tonic-gate 			    (FLG_OF_NOUNDEF | FLG_OF_SYMBOLIC)))
21590Sstevel@tonic-gate 				continue;
21600Sstevel@tonic-gate 			if ((needed = str + (size_t)dyn->d_un.d_val) == NULL)
21610Sstevel@tonic-gate 				continue;
21620Sstevel@tonic-gate 
21630Sstevel@tonic-gate 			/*
21640Sstevel@tonic-gate 			 * Determine if this needed entry is already recorded on
21650Sstevel@tonic-gate 			 * the shared object needed list, if not create a new
21660Sstevel@tonic-gate 			 * definition for later processing (see finish_libs()).
21670Sstevel@tonic-gate 			 */
21689131SRod.Evans@Sun.COM 			needed = expand(ifl->ifl_name, needed, NULL);
21690Sstevel@tonic-gate 
21709131SRod.Evans@Sun.COM 			if ((sdf = sdf_find(needed, ofl->ofl_soneed)) == NULL) {
21710Sstevel@tonic-gate 				if ((sdf = sdf_add(needed,
21720Sstevel@tonic-gate 				    &ofl->ofl_soneed)) == (Sdf_desc *)S_ERROR)
21730Sstevel@tonic-gate 					return (S_ERROR);
21740Sstevel@tonic-gate 				sdf->sdf_rfile = ifl->ifl_name;
21750Sstevel@tonic-gate 			}
21760Sstevel@tonic-gate 
21770Sstevel@tonic-gate 			/*
21780Sstevel@tonic-gate 			 * Record the runpath (Note that we take the first
21790Sstevel@tonic-gate 			 * runpath which is exactly what ld.so.1 would do during
21800Sstevel@tonic-gate 			 * its dependency processing).
21810Sstevel@tonic-gate 			 */
218210792SRod.Evans@Sun.COM 			if (rpath && (sdf->sdf_rpath == NULL))
21830Sstevel@tonic-gate 				sdf->sdf_rpath = rpath;
21840Sstevel@tonic-gate 
21850Sstevel@tonic-gate 		} else if (dyn->d_tag == DT_FLAGS_1) {
21860Sstevel@tonic-gate 			if (dyn->d_un.d_val & (DF_1_INITFIRST | DF_1_INTERPOSE))
21870Sstevel@tonic-gate 				ifl->ifl_flags &= ~FLG_IF_LAZYLD;
21880Sstevel@tonic-gate 			if (dyn->d_un.d_val & DF_1_DISPRELPND)
21890Sstevel@tonic-gate 				ifl->ifl_flags |= FLG_IF_DISPPEND;
21900Sstevel@tonic-gate 			if (dyn->d_un.d_val & DF_1_DISPRELDNE)
21910Sstevel@tonic-gate 				ifl->ifl_flags |= FLG_IF_DISPDONE;
21920Sstevel@tonic-gate 			if (dyn->d_un.d_val & DF_1_NODIRECT)
21930Sstevel@tonic-gate 				ifl->ifl_flags |= FLG_IF_NODIRECT;
21940Sstevel@tonic-gate 
21950Sstevel@tonic-gate 		} else if ((dyn->d_tag == DT_AUDIT) &&
21960Sstevel@tonic-gate 		    (ifl->ifl_flags & FLG_IF_NEEDED)) {
21970Sstevel@tonic-gate 			/*
21980Sstevel@tonic-gate 			 * Record audit string as DT_DEPAUDIT.
21990Sstevel@tonic-gate 			 */
22000Sstevel@tonic-gate 			if ((ofl->ofl_depaudit = add_string(ofl->ofl_depaudit,
22010Sstevel@tonic-gate 			    (str + (size_t)dyn->d_un.d_val))) ==
22020Sstevel@tonic-gate 			    (const char *)S_ERROR)
22030Sstevel@tonic-gate 				return (S_ERROR);
22040Sstevel@tonic-gate 
22050Sstevel@tonic-gate 		} else if (dyn->d_tag == DT_SUNW_RTLDINF) {
22060Sstevel@tonic-gate 			/*
22070Sstevel@tonic-gate 			 * If a library has the SUNW_RTLDINF .dynamic entry
22080Sstevel@tonic-gate 			 * then we must not permit lazyloading of this library.
22090Sstevel@tonic-gate 			 * This is because critical startup information (TLS
22100Sstevel@tonic-gate 			 * routines) are provided as part of these interfaces
22110Sstevel@tonic-gate 			 * and we must have them as part of process startup.
22120Sstevel@tonic-gate 			 */
22130Sstevel@tonic-gate 			ifl->ifl_flags &= ~FLG_IF_LAZYLD;
22140Sstevel@tonic-gate 		}
22150Sstevel@tonic-gate 	}
22160Sstevel@tonic-gate 
22170Sstevel@tonic-gate 	/*
22180Sstevel@tonic-gate 	 * Perform some SONAME sanity checks.
22190Sstevel@tonic-gate 	 */
22200Sstevel@tonic-gate 	if (ifl->ifl_flags & FLG_IF_NEEDED) {
22217463SRod.Evans@Sun.COM 		Ifl_desc	*sifl;
22229131SRod.Evans@Sun.COM 		Aliste		idx;
22230Sstevel@tonic-gate 
22240Sstevel@tonic-gate 		/*
22250Sstevel@tonic-gate 		 * Determine if anyone else will cause the same SONAME to be
22260Sstevel@tonic-gate 		 * used (this is either caused by two different files having the
22270Sstevel@tonic-gate 		 * same SONAME, or by one file SONAME actually matching another
22280Sstevel@tonic-gate 		 * file basename (if no SONAME is specified within a shared
22290Sstevel@tonic-gate 		 * library its basename will be used)). Probably rare, but some
22300Sstevel@tonic-gate 		 * idiot will do it.
22310Sstevel@tonic-gate 		 */
22329131SRod.Evans@Sun.COM 		for (APLIST_TRAVERSE(ofl->ofl_sos, idx, sifl)) {
22330Sstevel@tonic-gate 			if ((strcmp(ifl->ifl_soname, sifl->ifl_soname) == 0) &&
22340Sstevel@tonic-gate 			    (ifl != sifl)) {
22350Sstevel@tonic-gate 				const char	*hint, *iflb, *siflb;
22360Sstevel@tonic-gate 
22370Sstevel@tonic-gate 				/*
22380Sstevel@tonic-gate 				 * Determine the basename of each file. Perhaps
22390Sstevel@tonic-gate 				 * there are multiple copies of the same file
22400Sstevel@tonic-gate 				 * being brought in using different -L search
22410Sstevel@tonic-gate 				 * paths, and if so give an extra hint in the
22420Sstevel@tonic-gate 				 * error message.
22430Sstevel@tonic-gate 				 */
22440Sstevel@tonic-gate 				iflb = strrchr(ifl->ifl_name, '/');
22450Sstevel@tonic-gate 				if (iflb == NULL)
22460Sstevel@tonic-gate 					iflb = ifl->ifl_name;
22470Sstevel@tonic-gate 				else
22480Sstevel@tonic-gate 					iflb++;
22490Sstevel@tonic-gate 
22500Sstevel@tonic-gate 				siflb = strrchr(sifl->ifl_name, '/');
22510Sstevel@tonic-gate 				if (siflb == NULL)
22520Sstevel@tonic-gate 					siflb = sifl->ifl_name;
22530Sstevel@tonic-gate 				else
22540Sstevel@tonic-gate 					siflb++;
22550Sstevel@tonic-gate 
22560Sstevel@tonic-gate 				if (strcmp(iflb, siflb) == 0)
22570Sstevel@tonic-gate 					hint = MSG_INTL(MSG_REC_CNFLTHINT);
22580Sstevel@tonic-gate 				else
22590Sstevel@tonic-gate 					hint = MSG_ORIG(MSG_STR_EMPTY);
22600Sstevel@tonic-gate 
22611618Srie 				eprintf(ofl->ofl_lml, ERR_FATAL,
22621618Srie 				    MSG_INTL(MSG_REC_OBJCNFLT), sifl->ifl_name,
22631618Srie 				    ifl->ifl_name, sifl->ifl_soname, hint);
22640Sstevel@tonic-gate 				ofl->ofl_flags |= FLG_OF_FATAL;
22650Sstevel@tonic-gate 				return (0);
22660Sstevel@tonic-gate 			}
22670Sstevel@tonic-gate 		}
22680Sstevel@tonic-gate 
22690Sstevel@tonic-gate 		/*
22700Sstevel@tonic-gate 		 * If the SONAME is the same as the name the user wishes to
22710Sstevel@tonic-gate 		 * record when building a dynamic library (refer -h option),
22720Sstevel@tonic-gate 		 * we also have a name clash.
22730Sstevel@tonic-gate 		 */
22740Sstevel@tonic-gate 		if (ofl->ofl_soname &&
22750Sstevel@tonic-gate 		    (strcmp(ofl->ofl_soname, ifl->ifl_soname) == 0)) {
22761618Srie 			eprintf(ofl->ofl_lml, ERR_FATAL,
22771618Srie 			    MSG_INTL(MSG_REC_OPTCNFLT), ifl->ifl_name,
22787983SAli.Bahrami@Sun.COM 			    MSG_INTL(MSG_MARG_SONAME), ifl->ifl_soname);
22790Sstevel@tonic-gate 			ofl->ofl_flags |= FLG_OF_FATAL;
22800Sstevel@tonic-gate 			return (0);
22810Sstevel@tonic-gate 		}
22820Sstevel@tonic-gate 	}
22830Sstevel@tonic-gate 	return (1);
22840Sstevel@tonic-gate }
22850Sstevel@tonic-gate 
22860Sstevel@tonic-gate /*
22879085SAli.Bahrami@Sun.COM  * Process a progbits section from a relocatable object (ET_REL).
22889085SAli.Bahrami@Sun.COM  * This is used on non-amd64 objects to recognize .eh_frame sections.
22899085SAli.Bahrami@Sun.COM  */
22909085SAli.Bahrami@Sun.COM /*ARGSUSED1*/
22919085SAli.Bahrami@Sun.COM static uintptr_t
22929085SAli.Bahrami@Sun.COM process_progbits_final(Is_desc *isc, Ifl_desc *ifl, Ofl_desc *ofl)
22939085SAli.Bahrami@Sun.COM {
22949085SAli.Bahrami@Sun.COM 	if (isc->is_osdesc && (isc->is_flags & FLG_IS_EHFRAME) &&
22959085SAli.Bahrami@Sun.COM 	    (ld_unwind_register(isc->is_osdesc, ofl) == S_ERROR))
22969085SAli.Bahrami@Sun.COM 		return (S_ERROR);
22979085SAli.Bahrami@Sun.COM 
22989085SAli.Bahrami@Sun.COM 	return (1);
22999085SAli.Bahrami@Sun.COM }
23009085SAli.Bahrami@Sun.COM 
23019085SAli.Bahrami@Sun.COM /*
23027463SRod.Evans@Sun.COM  * Process a group section.
23037463SRod.Evans@Sun.COM  */
23047463SRod.Evans@Sun.COM static uintptr_t
23057463SRod.Evans@Sun.COM process_group(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
23067463SRod.Evans@Sun.COM     Word ndx, int ident, Ofl_desc *ofl)
23077463SRod.Evans@Sun.COM {
23087463SRod.Evans@Sun.COM 	uintptr_t	error;
23097463SRod.Evans@Sun.COM 
23107463SRod.Evans@Sun.COM 	error = process_section(name, ifl, shdr, scn, ndx, ident, ofl);
23117463SRod.Evans@Sun.COM 	if ((error == 0) || (error == S_ERROR))
23127463SRod.Evans@Sun.COM 		return (error);
23137463SRod.Evans@Sun.COM 
23147463SRod.Evans@Sun.COM 	/*
23157463SRod.Evans@Sun.COM 	 * Indicate that this input file has groups to process.  Groups are
23167463SRod.Evans@Sun.COM 	 * processed after all input sections have been processed.
23177463SRod.Evans@Sun.COM 	 */
23187463SRod.Evans@Sun.COM 	ifl->ifl_flags |= FLG_IS_GROUPS;
23197463SRod.Evans@Sun.COM 
23207463SRod.Evans@Sun.COM 	return (1);
23217463SRod.Evans@Sun.COM }
23227463SRod.Evans@Sun.COM 
23237463SRod.Evans@Sun.COM /*
23240Sstevel@tonic-gate  * Process a relocation entry. At this point all input sections from this
23250Sstevel@tonic-gate  * input file have been assigned an input section descriptor which is saved
23260Sstevel@tonic-gate  * in the `ifl_isdesc' array.
23270Sstevel@tonic-gate  */
23281618Srie static uintptr_t
23290Sstevel@tonic-gate rel_process(Is_desc *isc, Ifl_desc *ifl, Ofl_desc *ofl)
23300Sstevel@tonic-gate {
23310Sstevel@tonic-gate 	Word 	rndx;
23320Sstevel@tonic-gate 	Is_desc	*risc;
23330Sstevel@tonic-gate 	Os_desc	*osp;
23340Sstevel@tonic-gate 	Shdr	*shdr = isc->is_shdr;
23354734Sab196087 	Conv_inv_buf_t inv_buf;
23360Sstevel@tonic-gate 
23370Sstevel@tonic-gate 	/*
23380Sstevel@tonic-gate 	 * Make sure this is a valid relocation we can handle.
23390Sstevel@tonic-gate 	 */
23406206Sab196087 	if (shdr->sh_type != ld_targ.t_m.m_rel_sht_type) {
23411618Srie 		eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_FIL_INVALSEC),
23429878SAli.Bahrami@Sun.COM 		    ifl->ifl_name, EC_WORD(isc->is_scnndx), isc->is_name,
23439273SAli.Bahrami@Sun.COM 		    conv_sec_type(ifl->ifl_ehdr->e_ident[EI_OSABI],
23449273SAli.Bahrami@Sun.COM 		    ifl->ifl_ehdr->e_machine, shdr->sh_type, 0, &inv_buf));
23450Sstevel@tonic-gate 		ofl->ofl_flags |= FLG_OF_FATAL;
23460Sstevel@tonic-gate 		return (0);
23470Sstevel@tonic-gate 	}
23480Sstevel@tonic-gate 
23490Sstevel@tonic-gate 	/*
23500Sstevel@tonic-gate 	 * From the relocation section header information determine which
23510Sstevel@tonic-gate 	 * section needs the actual relocation.  Determine which output section
23520Sstevel@tonic-gate 	 * this input section has been assigned to and add to its relocation
23530Sstevel@tonic-gate 	 * list.  Note that the relocation section may be null if it is not
23540Sstevel@tonic-gate 	 * required (ie. .debug, .stabs, etc).
23550Sstevel@tonic-gate 	 */
23560Sstevel@tonic-gate 	rndx = shdr->sh_info;
23570Sstevel@tonic-gate 	if (rndx >= ifl->ifl_shnum) {
23580Sstevel@tonic-gate 		/*
23590Sstevel@tonic-gate 		 * Broken input file.
23600Sstevel@tonic-gate 		 */
23611618Srie 		eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_FIL_INVSHINFO),
23629878SAli.Bahrami@Sun.COM 		    ifl->ifl_name, EC_WORD(isc->is_scnndx), isc->is_name,
23639878SAli.Bahrami@Sun.COM 		    EC_XWORD(rndx));
23640Sstevel@tonic-gate 		ofl->ofl_flags |= FLG_OF_FATAL;
23650Sstevel@tonic-gate 		return (0);
23660Sstevel@tonic-gate 	}
23670Sstevel@tonic-gate 	if (rndx == 0) {
23689131SRod.Evans@Sun.COM 		if (aplist_append(&ofl->ofl_extrarels, isc,
23699131SRod.Evans@Sun.COM 		    AL_CNT_OFL_RELS) == NULL)
23700Sstevel@tonic-gate 			return (S_ERROR);
23719131SRod.Evans@Sun.COM 
23729131SRod.Evans@Sun.COM 	} else if ((risc = ifl->ifl_isdesc[rndx]) != NULL) {
23730Sstevel@tonic-gate 		/*
23740Sstevel@tonic-gate 		 * Discard relocations if they are against a section
23750Sstevel@tonic-gate 		 * which has been discarded.
23760Sstevel@tonic-gate 		 */
23770Sstevel@tonic-gate 		if (risc->is_flags & FLG_IS_DISCARD)
23780Sstevel@tonic-gate 			return (1);
23799131SRod.Evans@Sun.COM 
23809131SRod.Evans@Sun.COM 		if ((osp = risc->is_osdesc) == NULL) {
23810Sstevel@tonic-gate 			if (risc->is_shdr->sh_type == SHT_SUNW_move) {
23820Sstevel@tonic-gate 				/*
23839131SRod.Evans@Sun.COM 				 * This section is processed later in
23849131SRod.Evans@Sun.COM 				 * process_movereloc().
23850Sstevel@tonic-gate 				 */
23869131SRod.Evans@Sun.COM 				if (aplist_append(&ofl->ofl_ismoverel,
23879131SRod.Evans@Sun.COM 				    isc, AL_CNT_OFL_MOVE) == NULL)
23880Sstevel@tonic-gate 					return (S_ERROR);
23890Sstevel@tonic-gate 				return (1);
23900Sstevel@tonic-gate 			}
23911618Srie 			eprintf(ofl->ofl_lml, ERR_FATAL,
23921618Srie 			    MSG_INTL(MSG_FIL_INVRELOC1), ifl->ifl_name,
23939878SAli.Bahrami@Sun.COM 			    EC_WORD(isc->is_scnndx), isc->is_name,
23949878SAli.Bahrami@Sun.COM 			    EC_WORD(risc->is_scnndx), risc->is_name);
23950Sstevel@tonic-gate 			return (0);
23960Sstevel@tonic-gate 		}
23978608SAli.Bahrami@Sun.COM 		if (aplist_append(&osp->os_relisdescs, isc,
23988608SAli.Bahrami@Sun.COM 		    AL_CNT_OS_RELISDESCS) == NULL)
23990Sstevel@tonic-gate 			return (S_ERROR);
24000Sstevel@tonic-gate 	}
24010Sstevel@tonic-gate 	return (1);
24020Sstevel@tonic-gate }
24030Sstevel@tonic-gate 
24040Sstevel@tonic-gate /*
24050Sstevel@tonic-gate  * SHF_EXCLUDE flags is set for this section.
24060Sstevel@tonic-gate  */
24070Sstevel@tonic-gate static uintptr_t
24080Sstevel@tonic-gate process_exclude(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
24090Sstevel@tonic-gate     Word ndx, Ofl_desc *ofl)
24100Sstevel@tonic-gate {
24110Sstevel@tonic-gate 	/*
24120Sstevel@tonic-gate 	 * Sections SHT_SYMTAB and SHT_DYNDYM, even if SHF_EXCLUDE is on, might
24130Sstevel@tonic-gate 	 * be needed for ld processing.  These sections need to be in the
24140Sstevel@tonic-gate 	 * internal table.  Later it will be determined whether they can be
24150Sstevel@tonic-gate 	 * eliminated or not.
24160Sstevel@tonic-gate 	 */
24170Sstevel@tonic-gate 	if (shdr->sh_type == SHT_SYMTAB || shdr->sh_type == SHT_DYNSYM)
24180Sstevel@tonic-gate 		return (0);
24190Sstevel@tonic-gate 
24200Sstevel@tonic-gate 	/*
24210Sstevel@tonic-gate 	 * Other checks
24220Sstevel@tonic-gate 	 */
24230Sstevel@tonic-gate 	if (shdr->sh_flags & SHF_ALLOC) {
24240Sstevel@tonic-gate 		/*
24250Sstevel@tonic-gate 		 * A conflict, issue an warning message, and ignore the section.
24260Sstevel@tonic-gate 		 */
24271618Srie 		eprintf(ofl->ofl_lml, ERR_WARNING, MSG_INTL(MSG_FIL_EXCLUDE),
24289878SAli.Bahrami@Sun.COM 		    ifl->ifl_name, EC_WORD(ndx), name);
24290Sstevel@tonic-gate 		return (0);
24300Sstevel@tonic-gate 	}
24310Sstevel@tonic-gate 
24320Sstevel@tonic-gate 	/*
24330Sstevel@tonic-gate 	 * This sections is not going to the output file.
24340Sstevel@tonic-gate 	 */
24350Sstevel@tonic-gate 	return (process_section(name, ifl, shdr, scn, ndx, 0, ofl));
24360Sstevel@tonic-gate }
24370Sstevel@tonic-gate 
24380Sstevel@tonic-gate /*
24390Sstevel@tonic-gate  * Section processing state table.  `Initial' describes the required initial
24400Sstevel@tonic-gate  * procedure to be called (if any), `Final' describes the final processing
24410Sstevel@tonic-gate  * procedure (ie. things that can only be done when all required sections
24420Sstevel@tonic-gate  * have been collected).
24430Sstevel@tonic-gate  */
24449085SAli.Bahrami@Sun.COM typedef uintptr_t	(* initial_func_t)(const char *, Ifl_desc *, Shdr *,
24459085SAli.Bahrami@Sun.COM 			    Elf_Scn *, Word, int, Ofl_desc *);
24460Sstevel@tonic-gate 
24479085SAli.Bahrami@Sun.COM static initial_func_t Initial[SHT_NUM][2] = {
24480Sstevel@tonic-gate /*			ET_REL			ET_DYN			*/
24490Sstevel@tonic-gate 
24500Sstevel@tonic-gate /* SHT_NULL	*/	invalid_section,	invalid_section,
24510Sstevel@tonic-gate /* SHT_PROGBITS	*/	process_progbits,	process_progbits,
24520Sstevel@tonic-gate /* SHT_SYMTAB	*/	process_input,		process_input,
24530Sstevel@tonic-gate /* SHT_STRTAB	*/	process_strtab,		process_strtab,
24540Sstevel@tonic-gate /* SHT_RELA	*/	process_reloc,		process_reloc,
24550Sstevel@tonic-gate /* SHT_HASH	*/	invalid_section,	NULL,
24564716Sab196087 /* SHT_DYNAMIC	*/	process_rel_dynamic,	process_dynamic_isgnu,
24570Sstevel@tonic-gate /* SHT_NOTE	*/	process_section,	NULL,
24580Sstevel@tonic-gate /* SHT_NOBITS	*/	process_nobits,		process_nobits,
24590Sstevel@tonic-gate /* SHT_REL	*/	process_reloc,		process_reloc,
24600Sstevel@tonic-gate /* SHT_SHLIB	*/	process_section,	invalid_section,
24610Sstevel@tonic-gate /* SHT_DYNSYM	*/	invalid_section,	process_input,
24620Sstevel@tonic-gate /* SHT_UNKNOWN12 */	process_progbits,	process_progbits,
24630Sstevel@tonic-gate /* SHT_UNKNOWN13 */	process_progbits,	process_progbits,
24640Sstevel@tonic-gate /* SHT_INIT_ARRAY */	process_array,		NULL,
24650Sstevel@tonic-gate /* SHT_FINI_ARRAY */	process_array,		NULL,
24660Sstevel@tonic-gate /* SHT_PREINIT_ARRAY */	process_array,		NULL,
24677463SRod.Evans@Sun.COM /* SHT_GROUP */		process_group,		invalid_section,
24680Sstevel@tonic-gate /* SHT_SYMTAB_SHNDX */	process_sym_shndx,	NULL
24690Sstevel@tonic-gate };
24700Sstevel@tonic-gate 
24719085SAli.Bahrami@Sun.COM typedef uintptr_t	(* final_func_t)(Is_desc *, Ifl_desc *, Ofl_desc *);
24729085SAli.Bahrami@Sun.COM 
24739085SAli.Bahrami@Sun.COM static final_func_t Final[SHT_NUM][2] = {
24749085SAli.Bahrami@Sun.COM /*			ET_REL			ET_DYN			*/
24750Sstevel@tonic-gate 
24760Sstevel@tonic-gate /* SHT_NULL	*/	NULL,			NULL,
24779085SAli.Bahrami@Sun.COM /* SHT_PROGBITS	*/	process_progbits_final,	NULL,
24781618Srie /* SHT_SYMTAB	*/	ld_sym_process,		ld_sym_process,
24790Sstevel@tonic-gate /* SHT_STRTAB	*/	NULL,			NULL,
24800Sstevel@tonic-gate /* SHT_RELA	*/	rel_process,		NULL,
24810Sstevel@tonic-gate /* SHT_HASH	*/	NULL,			NULL,
24820Sstevel@tonic-gate /* SHT_DYNAMIC	*/	NULL,			process_dynamic,
24830Sstevel@tonic-gate /* SHT_NOTE	*/	NULL,			NULL,
24840Sstevel@tonic-gate /* SHT_NOBITS	*/	NULL,			NULL,
24850Sstevel@tonic-gate /* SHT_REL	*/	rel_process,		NULL,
24860Sstevel@tonic-gate /* SHT_SHLIB	*/	NULL,			NULL,
24871618Srie /* SHT_DYNSYM	*/	NULL,			ld_sym_process,
24880Sstevel@tonic-gate /* SHT_UNKNOWN12 */	NULL,			NULL,
24890Sstevel@tonic-gate /* SHT_UNKNOWN13 */	NULL,			NULL,
24907463SRod.Evans@Sun.COM /* SHT_INIT_ARRAY */	array_process,		NULL,
24917463SRod.Evans@Sun.COM /* SHT_FINI_ARRAY */	array_process,		NULL,
24927463SRod.Evans@Sun.COM /* SHT_PREINIT_ARRAY */	array_process,		NULL,
24930Sstevel@tonic-gate /* SHT_GROUP */		NULL,			NULL,
24940Sstevel@tonic-gate /* SHT_SYMTAB_SHNDX */	sym_shndx_process,	NULL
24950Sstevel@tonic-gate };
24960Sstevel@tonic-gate 
24970Sstevel@tonic-gate #define	MAXNDXSIZE	10
24980Sstevel@tonic-gate 
24990Sstevel@tonic-gate /*
25000Sstevel@tonic-gate  * Process an elf file.  Each section is compared against the section state
25010Sstevel@tonic-gate  * table to determine whether it should be processed (saved), ignored, or
25020Sstevel@tonic-gate  * is invalid for the type of input file being processed.
25030Sstevel@tonic-gate  */
25041618Srie static uintptr_t
25050Sstevel@tonic-gate process_elf(Ifl_desc *ifl, Elf *elf, Ofl_desc *ofl)
25060Sstevel@tonic-gate {
25070Sstevel@tonic-gate 	Elf_Scn		*scn;
25080Sstevel@tonic-gate 	Shdr		*shdr;
25097463SRod.Evans@Sun.COM 	Word		ndx, sndx, ordndx = 0, ordcnt = 0;
25109878SAli.Bahrami@Sun.COM 	char		*str, *name;
25110Sstevel@tonic-gate 	Word		row, column;
25120Sstevel@tonic-gate 	int		ident;
25130Sstevel@tonic-gate 	uintptr_t	error;
251411827SRod.Evans@Sun.COM 	Is_desc		*vdfisp, *vndisp, *vsyisp, *sifisp;
251511827SRod.Evans@Sun.COM 	Is_desc		*capinfoisp, *capisp;
25160Sstevel@tonic-gate 	Sdf_desc	*sdf;
251711734SAli.Bahrami@Sun.COM 	Place_path_info	path_info_buf, *path_info;
251811734SAli.Bahrami@Sun.COM 
251911734SAli.Bahrami@Sun.COM 	/*
252011734SAli.Bahrami@Sun.COM 	 * Path information buffer used by ld_place_section() and related
252111734SAli.Bahrami@Sun.COM 	 * routines. This information is used to evaluate entrance criteria
252211734SAli.Bahrami@Sun.COM 	 * with non-empty file matching lists (ec_files).
252311734SAli.Bahrami@Sun.COM 	 */
252411734SAli.Bahrami@Sun.COM 	path_info = ld_place_path_info_init(ofl, ifl, &path_info_buf);
25250Sstevel@tonic-gate 
25260Sstevel@tonic-gate 	/*
25270Sstevel@tonic-gate 	 * First process the .shstrtab section so that later sections can
25280Sstevel@tonic-gate 	 * reference their name.
25290Sstevel@tonic-gate 	 */
25301618Srie 	ld_sup_file(ofl, ifl->ifl_name, elf_kind(elf), ifl->ifl_flags, elf);
25310Sstevel@tonic-gate 
25320Sstevel@tonic-gate 	sndx = ifl->ifl_shstrndx;
25330Sstevel@tonic-gate 	if ((scn = elf_getscn(elf, (size_t)sndx)) == NULL) {
25341618Srie 		eprintf(ofl->ofl_lml, ERR_ELF, MSG_INTL(MSG_ELF_GETSCN),
25351618Srie 		    ifl->ifl_name);
25360Sstevel@tonic-gate 		ofl->ofl_flags |= FLG_OF_FATAL;
25370Sstevel@tonic-gate 		return (0);
25380Sstevel@tonic-gate 	}
25390Sstevel@tonic-gate 	if ((shdr = elf_getshdr(scn)) == NULL) {
25401618Srie 		eprintf(ofl->ofl_lml, ERR_ELF, MSG_INTL(MSG_ELF_GETSHDR),
25411618Srie 		    ifl->ifl_name);
25420Sstevel@tonic-gate 		ofl->ofl_flags |= FLG_OF_FATAL;
25430Sstevel@tonic-gate 		return (0);
25440Sstevel@tonic-gate 	}
25450Sstevel@tonic-gate 	if ((name = elf_strptr(elf, (size_t)sndx, (size_t)shdr->sh_name)) ==
25460Sstevel@tonic-gate 	    NULL) {
25471618Srie 		eprintf(ofl->ofl_lml, ERR_ELF, MSG_INTL(MSG_ELF_STRPTR),
25481618Srie 		    ifl->ifl_name);
25490Sstevel@tonic-gate 		ofl->ofl_flags |= FLG_OF_FATAL;
25500Sstevel@tonic-gate 		return (0);
25510Sstevel@tonic-gate 	}
25520Sstevel@tonic-gate 
25532647Srie 	if (ld_sup_input_section(ofl, ifl, name, &shdr, sndx, scn,
25542647Srie 	    elf) == S_ERROR)
25550Sstevel@tonic-gate 		return (S_ERROR);
25560Sstevel@tonic-gate 
25570Sstevel@tonic-gate 	/*
25580Sstevel@tonic-gate 	 * Reset the name since the shdr->sh_name could have been changed as
25599878SAli.Bahrami@Sun.COM 	 * part of ld_sup_input_section().
25600Sstevel@tonic-gate 	 */
25619878SAli.Bahrami@Sun.COM 	if ((name = elf_strptr(elf, (size_t)sndx, (size_t)shdr->sh_name)) ==
25629878SAli.Bahrami@Sun.COM 	    NULL) {
25631618Srie 		eprintf(ofl->ofl_lml, ERR_ELF, MSG_INTL(MSG_ELF_STRPTR),
25641618Srie 		    ifl->ifl_name);
25650Sstevel@tonic-gate 		ofl->ofl_flags |= FLG_OF_FATAL;
25660Sstevel@tonic-gate 		return (0);
25670Sstevel@tonic-gate 	}
25680Sstevel@tonic-gate 
25690Sstevel@tonic-gate 	error = process_strtab(name, ifl, shdr, scn, sndx, FALSE, ofl);
25700Sstevel@tonic-gate 	if ((error == 0) || (error == S_ERROR))
25710Sstevel@tonic-gate 		return (error);
25720Sstevel@tonic-gate 	str = ifl->ifl_isdesc[sndx]->is_indata->d_buf;
25730Sstevel@tonic-gate 
25740Sstevel@tonic-gate 	/*
25750Sstevel@tonic-gate 	 * Determine the state table column from the input file type.  Note,
25760Sstevel@tonic-gate 	 * shared library sections are not added to the output section list.
25770Sstevel@tonic-gate 	 */
25780Sstevel@tonic-gate 	if (ifl->ifl_ehdr->e_type == ET_DYN) {
25790Sstevel@tonic-gate 		column = 1;
25800Sstevel@tonic-gate 		ofl->ofl_soscnt++;
25816206Sab196087 		ident = ld_targ.t_id.id_null;
25820Sstevel@tonic-gate 	} else {
25830Sstevel@tonic-gate 		column = 0;
25840Sstevel@tonic-gate 		ofl->ofl_objscnt++;
25856206Sab196087 		ident = ld_targ.t_id.id_unknown;
25860Sstevel@tonic-gate 	}
25870Sstevel@tonic-gate 
25881618Srie 	DBG_CALL(Dbg_file_generic(ofl->ofl_lml, ifl));
25890Sstevel@tonic-gate 	ndx = 0;
259011827SRod.Evans@Sun.COM 	vdfisp = vndisp = vsyisp = sifisp = capinfoisp = capisp = NULL;
25910Sstevel@tonic-gate 	scn = NULL;
25920Sstevel@tonic-gate 	while (scn = elf_nextscn(elf, scn)) {
25930Sstevel@tonic-gate 		ndx++;
25947463SRod.Evans@Sun.COM 
25950Sstevel@tonic-gate 		/*
25960Sstevel@tonic-gate 		 * As we've already processed the .shstrtab don't do it again.
25970Sstevel@tonic-gate 		 */
25980Sstevel@tonic-gate 		if (ndx == sndx)
25990Sstevel@tonic-gate 			continue;
26000Sstevel@tonic-gate 
26010Sstevel@tonic-gate 		if ((shdr = elf_getshdr(scn)) == NULL) {
26021618Srie 			eprintf(ofl->ofl_lml, ERR_ELF,
26031618Srie 			    MSG_INTL(MSG_ELF_GETSHDR), ifl->ifl_name);
26040Sstevel@tonic-gate 			ofl->ofl_flags |= FLG_OF_FATAL;
26050Sstevel@tonic-gate 			return (0);
26060Sstevel@tonic-gate 		}
26070Sstevel@tonic-gate 		name = str + (size_t)(shdr->sh_name);
26080Sstevel@tonic-gate 
26092647Srie 		if (ld_sup_input_section(ofl, ifl, name, &shdr, ndx, scn,
26102647Srie 		    elf) == S_ERROR)
26110Sstevel@tonic-gate 			return (S_ERROR);
26120Sstevel@tonic-gate 
26130Sstevel@tonic-gate 		/*
26140Sstevel@tonic-gate 		 * Reset the name since the shdr->sh_name could have been
26159878SAli.Bahrami@Sun.COM 		 * changed as part of ld_sup_input_section().
26160Sstevel@tonic-gate 		 */
26179878SAli.Bahrami@Sun.COM 		name = str + (size_t)(shdr->sh_name);
26180Sstevel@tonic-gate 
26190Sstevel@tonic-gate 		row = shdr->sh_type;
26200Sstevel@tonic-gate 
26210Sstevel@tonic-gate 		/*
26220Sstevel@tonic-gate 		 * If the section has the SHF_EXCLUDE flag on, and we're not
26230Sstevel@tonic-gate 		 * generating a relocatable object, exclude the section.
26240Sstevel@tonic-gate 		 */
26250Sstevel@tonic-gate 		if (((shdr->sh_flags & SHF_EXCLUDE) != 0) &&
26260Sstevel@tonic-gate 		    ((ofl->ofl_flags & FLG_OF_RELOBJ) == 0)) {
26270Sstevel@tonic-gate 			if ((error = process_exclude(name, ifl, shdr, scn,
26280Sstevel@tonic-gate 			    ndx, ofl)) == S_ERROR)
26290Sstevel@tonic-gate 				return (S_ERROR);
26300Sstevel@tonic-gate 			if (error == 1)
26310Sstevel@tonic-gate 				continue;
26320Sstevel@tonic-gate 		}
26330Sstevel@tonic-gate 
26340Sstevel@tonic-gate 		/*
26350Sstevel@tonic-gate 		 * If this is a standard section type process it via the
26360Sstevel@tonic-gate 		 * appropriate action routine.
26370Sstevel@tonic-gate 		 */
26380Sstevel@tonic-gate 		if (row < SHT_NUM) {
26390Sstevel@tonic-gate 			if (Initial[row][column] != NULL) {
26400Sstevel@tonic-gate 				if (Initial[row][column](name, ifl, shdr, scn,
26410Sstevel@tonic-gate 				    ndx, ident, ofl) == S_ERROR)
26420Sstevel@tonic-gate 					return (S_ERROR);
26430Sstevel@tonic-gate 			}
26440Sstevel@tonic-gate 		} else {
26450Sstevel@tonic-gate 			/*
26460Sstevel@tonic-gate 			 * If this section is below SHT_LOSUNW then we don't
26470Sstevel@tonic-gate 			 * really know what to do with it, issue a warning
26480Sstevel@tonic-gate 			 * message but do the basic section processing anyway.
26490Sstevel@tonic-gate 			 */
26504734Sab196087 			if (row < (Word)SHT_LOSUNW) {
26514734Sab196087 				Conv_inv_buf_t inv_buf;
26524734Sab196087 
26531618Srie 				eprintf(ofl->ofl_lml, ERR_WARNING,
26541618Srie 				    MSG_INTL(MSG_FIL_INVALSEC), ifl->ifl_name,
26559878SAli.Bahrami@Sun.COM 				    EC_WORD(ndx), name, conv_sec_type(
26569273SAli.Bahrami@Sun.COM 				    ifl->ifl_ehdr->e_ident[EI_OSABI],
26579273SAli.Bahrami@Sun.COM 				    ifl->ifl_ehdr->e_machine,
26584734Sab196087 				    shdr->sh_type, 0, &inv_buf));
26594734Sab196087 			}
26600Sstevel@tonic-gate 
26610Sstevel@tonic-gate 			/*
26620Sstevel@tonic-gate 			 * Handle sections greater than SHT_LOSUNW.
26630Sstevel@tonic-gate 			 */
26640Sstevel@tonic-gate 			switch (row) {
26650Sstevel@tonic-gate 			case SHT_SUNW_dof:
26660Sstevel@tonic-gate 				if (process_section(name, ifl, shdr, scn,
26670Sstevel@tonic-gate 				    ndx, ident, ofl) == S_ERROR)
26680Sstevel@tonic-gate 					return (S_ERROR);
26690Sstevel@tonic-gate 				break;
26700Sstevel@tonic-gate 			case SHT_SUNW_cap:
26719085SAli.Bahrami@Sun.COM 				if (process_section(name, ifl, shdr, scn, ndx,
26729085SAli.Bahrami@Sun.COM 				    ld_targ.t_id.id_null, ofl) == S_ERROR)
26730Sstevel@tonic-gate 					return (S_ERROR);
26740Sstevel@tonic-gate 				capisp = ifl->ifl_isdesc[ndx];
26750Sstevel@tonic-gate 				break;
267611827SRod.Evans@Sun.COM 			case SHT_SUNW_capinfo:
267711827SRod.Evans@Sun.COM 				if (process_section(name, ifl, shdr, scn, ndx,
267811827SRod.Evans@Sun.COM 				    ld_targ.t_id.id_null, ofl) == S_ERROR)
267911827SRod.Evans@Sun.COM 					return (S_ERROR);
268011827SRod.Evans@Sun.COM 				capinfoisp = ifl->ifl_isdesc[ndx];
268111827SRod.Evans@Sun.COM 				break;
26820Sstevel@tonic-gate 			case SHT_SUNW_DEBUGSTR:
26830Sstevel@tonic-gate 			case SHT_SUNW_DEBUG:
26840Sstevel@tonic-gate 				if (process_debug(name, ifl, shdr, scn,
26850Sstevel@tonic-gate 				    ndx, ident, ofl) == S_ERROR)
26860Sstevel@tonic-gate 					return (S_ERROR);
26870Sstevel@tonic-gate 				break;
26880Sstevel@tonic-gate 			case SHT_SUNW_move:
26899085SAli.Bahrami@Sun.COM 				if (process_section(name, ifl, shdr, scn, ndx,
26909085SAli.Bahrami@Sun.COM 				    ld_targ.t_id.id_null, ofl) == S_ERROR)
26910Sstevel@tonic-gate 					return (S_ERROR);
26920Sstevel@tonic-gate 				break;
26930Sstevel@tonic-gate 			case SHT_SUNW_syminfo:
26949085SAli.Bahrami@Sun.COM 				if (process_section(name, ifl, shdr, scn, ndx,
26959085SAli.Bahrami@Sun.COM 				    ld_targ.t_id.id_null, ofl) == S_ERROR)
26960Sstevel@tonic-gate 					return (S_ERROR);
26970Sstevel@tonic-gate 				sifisp = ifl->ifl_isdesc[ndx];
26980Sstevel@tonic-gate 				break;
26990Sstevel@tonic-gate 			case SHT_SUNW_ANNOTATE:
27007463SRod.Evans@Sun.COM 				if (process_progbits(name, ifl, shdr, scn,
27017463SRod.Evans@Sun.COM 				    ndx, ident, ofl) == S_ERROR)
27027463SRod.Evans@Sun.COM 					return (S_ERROR);
27037463SRod.Evans@Sun.COM 				break;
27040Sstevel@tonic-gate 			case SHT_SUNW_COMDAT:
27050Sstevel@tonic-gate 				if (process_progbits(name, ifl, shdr, scn,
27060Sstevel@tonic-gate 				    ndx, ident, ofl) == S_ERROR)
27070Sstevel@tonic-gate 					return (S_ERROR);
27087463SRod.Evans@Sun.COM 				ifl->ifl_isdesc[ndx]->is_flags |= FLG_IS_COMDAT;
27090Sstevel@tonic-gate 				break;
27100Sstevel@tonic-gate 			case SHT_SUNW_verdef:
27119085SAli.Bahrami@Sun.COM 				if (process_section(name, ifl, shdr, scn, ndx,
27129085SAli.Bahrami@Sun.COM 				    ld_targ.t_id.id_null, ofl) == S_ERROR)
27130Sstevel@tonic-gate 					return (S_ERROR);
27140Sstevel@tonic-gate 				vdfisp = ifl->ifl_isdesc[ndx];
27150Sstevel@tonic-gate 				break;
27160Sstevel@tonic-gate 			case SHT_SUNW_verneed:
27179085SAli.Bahrami@Sun.COM 				if (process_section(name, ifl, shdr, scn, ndx,
27189085SAli.Bahrami@Sun.COM 				    ld_targ.t_id.id_null, ofl) == S_ERROR)
27190Sstevel@tonic-gate 					return (S_ERROR);
27200Sstevel@tonic-gate 				vndisp = ifl->ifl_isdesc[ndx];
27210Sstevel@tonic-gate 				break;
27220Sstevel@tonic-gate 			case SHT_SUNW_versym:
27239085SAli.Bahrami@Sun.COM 				if (process_section(name, ifl, shdr, scn, ndx,
27249085SAli.Bahrami@Sun.COM 				    ld_targ.t_id.id_null, ofl) == S_ERROR)
27250Sstevel@tonic-gate 					return (S_ERROR);
27260Sstevel@tonic-gate 				vsyisp = ifl->ifl_isdesc[ndx];
27270Sstevel@tonic-gate 				break;
27280Sstevel@tonic-gate 			case SHT_SPARC_GOTDATA:
27296206Sab196087 				/*
27306206Sab196087 				 * SHT_SPARC_GOTDATA (0x70000000) is in the
27316206Sab196087 				 * SHT_LOPROC - SHT_HIPROC range reserved
27326206Sab196087 				 * for processor-specific semantics. It is
27336206Sab196087 				 * only meaningful for sparc targets.
27346206Sab196087 				 */
27356206Sab196087 				if (ld_targ.t_m.m_mach !=
27366206Sab196087 				    LD_TARG_BYCLASS(EM_SPARC, EM_SPARCV9))
27376206Sab196087 					goto do_default;
27389085SAli.Bahrami@Sun.COM 				if (process_section(name, ifl, shdr, scn, ndx,
27399085SAli.Bahrami@Sun.COM 				    ld_targ.t_id.id_gotdata, ofl) == S_ERROR)
27400Sstevel@tonic-gate 					return (S_ERROR);
27410Sstevel@tonic-gate 				break;
27426206Sab196087 #if	defined(_ELF64)
27430Sstevel@tonic-gate 			case SHT_AMD64_UNWIND:
27446206Sab196087 				/*
27456206Sab196087 				 * SHT_AMD64_UNWIND (0x70000001) is in the
27466206Sab196087 				 * SHT_LOPROC - SHT_HIPROC range reserved
27476206Sab196087 				 * for processor-specific semantics. It is
27486206Sab196087 				 * only meaningful for amd64 targets.
27496206Sab196087 				 */
27506206Sab196087 				if (ld_targ.t_m.m_mach != EM_AMD64)
27516206Sab196087 					goto do_default;
27527463SRod.Evans@Sun.COM 
27536206Sab196087 				/*
27546206Sab196087 				 * Target is x86, so this really is
27556206Sab196087 				 * SHT_AMD64_UNWIND
27566206Sab196087 				 */
27570Sstevel@tonic-gate 				if (column == 0) {
27580Sstevel@tonic-gate 					/*
27590Sstevel@tonic-gate 					 * column == ET_REL
27600Sstevel@tonic-gate 					 */
27617463SRod.Evans@Sun.COM 					if (process_section(name, ifl, shdr,
27627463SRod.Evans@Sun.COM 					    scn, ndx, ld_targ.t_id.id_unwind,
27637463SRod.Evans@Sun.COM 					    ofl) == S_ERROR)
27640Sstevel@tonic-gate 						return (S_ERROR);
27659085SAli.Bahrami@Sun.COM 					ifl->ifl_isdesc[ndx]->is_flags |=
27669085SAli.Bahrami@Sun.COM 					    FLG_IS_EHFRAME;
27670Sstevel@tonic-gate 				}
27680Sstevel@tonic-gate 				break;
27690Sstevel@tonic-gate #endif
27700Sstevel@tonic-gate 			default:
27716206Sab196087 			do_default:
27729085SAli.Bahrami@Sun.COM 				if (process_section(name, ifl, shdr, scn, ndx,
27739085SAli.Bahrami@Sun.COM 				    ((ident == ld_targ.t_id.id_null) ?
27749085SAli.Bahrami@Sun.COM 				    ident : ld_targ.t_id.id_user), ofl) ==
27759085SAli.Bahrami@Sun.COM 				    S_ERROR)
27760Sstevel@tonic-gate 					return (S_ERROR);
27770Sstevel@tonic-gate 				break;
27780Sstevel@tonic-gate 			}
27790Sstevel@tonic-gate 		}
27807463SRod.Evans@Sun.COM 	}
27810Sstevel@tonic-gate 
27827463SRod.Evans@Sun.COM 	/*
27837463SRod.Evans@Sun.COM 	 * Now that all input sections have been analyzed, and prior to placing
27847463SRod.Evans@Sun.COM 	 * any input sections to their output sections, process any groups.
27857463SRod.Evans@Sun.COM 	 * Groups can contribute COMDAT items, which may get discarded as part
27867463SRod.Evans@Sun.COM 	 * of placement.  In addition, COMDAT names may require transformation
27877463SRod.Evans@Sun.COM 	 * to indicate different output section placement.
27887463SRod.Evans@Sun.COM 	 */
27897463SRod.Evans@Sun.COM 	if (ifl->ifl_flags & FLG_IS_GROUPS) {
27907463SRod.Evans@Sun.COM 		for (ndx = 1; ndx < ifl->ifl_shnum; ndx++) {
27917463SRod.Evans@Sun.COM 			Is_desc	*isp;
27927463SRod.Evans@Sun.COM 
27937463SRod.Evans@Sun.COM 			if (((isp = ifl->ifl_isdesc[ndx]) == NULL) ||
27947463SRod.Evans@Sun.COM 			    (isp->is_shdr->sh_type != SHT_GROUP))
27957463SRod.Evans@Sun.COM 				continue;
27967463SRod.Evans@Sun.COM 
27977463SRod.Evans@Sun.COM 			if (ld_group_process(isp, ofl) == S_ERROR)
27987463SRod.Evans@Sun.COM 				return (S_ERROR);
27990Sstevel@tonic-gate 		}
28000Sstevel@tonic-gate 	}
28010Sstevel@tonic-gate 
28020Sstevel@tonic-gate 	/*
28039615SAli.Bahrami@Sun.COM 	 * Now that all of the input sections have been processed, place
28049615SAli.Bahrami@Sun.COM 	 * them in the appropriate output sections.
28050Sstevel@tonic-gate 	 */
28067463SRod.Evans@Sun.COM 	for (ndx = 1; ndx < ifl->ifl_shnum; ndx++) {
28077463SRod.Evans@Sun.COM 		Is_desc	*isp;
28087463SRod.Evans@Sun.COM 
28097463SRod.Evans@Sun.COM 		if (((isp = ifl->ifl_isdesc[ndx]) == NULL) ||
28107463SRod.Evans@Sun.COM 		    ((isp->is_flags & FLG_IS_PLACE) == 0))
28117463SRod.Evans@Sun.COM 			continue;
28127463SRod.Evans@Sun.COM 
28137463SRod.Evans@Sun.COM 		/*
28147463SRod.Evans@Sun.COM 		 * Place all non-ordered sections within their appropriate
28157463SRod.Evans@Sun.COM 		 * output section.
28167463SRod.Evans@Sun.COM 		 */
28179615SAli.Bahrami@Sun.COM 		if ((isp->is_flags & FLG_IS_ORDERED) == 0) {
281811734SAli.Bahrami@Sun.COM 			if (ld_place_section(ofl, isp, path_info,
28199615SAli.Bahrami@Sun.COM 			    isp->is_keyident, NULL) == (Os_desc *)S_ERROR)
28207463SRod.Evans@Sun.COM 				return (S_ERROR);
28219615SAli.Bahrami@Sun.COM 			continue;
28227463SRod.Evans@Sun.COM 		}
28237463SRod.Evans@Sun.COM 
28247463SRod.Evans@Sun.COM 		/*
28259615SAli.Bahrami@Sun.COM 		 * Count the number of ordered sections and retain the first
28269615SAli.Bahrami@Sun.COM 		 * ordered section index. This will be used to optimize the
28279615SAli.Bahrami@Sun.COM 		 * ordered section loop that immediately follows this one.
28287463SRod.Evans@Sun.COM 		 */
28299615SAli.Bahrami@Sun.COM 		ordcnt++;
28309615SAli.Bahrami@Sun.COM 		if (ordndx == 0)
28319615SAli.Bahrami@Sun.COM 			ordndx = ndx;
28329615SAli.Bahrami@Sun.COM 	}
28339615SAli.Bahrami@Sun.COM 
28349615SAli.Bahrami@Sun.COM 	/*
28359615SAli.Bahrami@Sun.COM 	 * Having placed all the non-ordered sections, it is now
28369615SAli.Bahrami@Sun.COM 	 * safe to place SHF_ORDERED/SHF_LINK_ORDER sections.
28379615SAli.Bahrami@Sun.COM 	 */
28389615SAli.Bahrami@Sun.COM 	if (ifl->ifl_flags & FLG_IF_ORDERED) {
28399615SAli.Bahrami@Sun.COM 		for (ndx = ordndx; ndx < ifl->ifl_shnum; ndx++) {
28409615SAli.Bahrami@Sun.COM 			Is_desc	*isp;
28419615SAli.Bahrami@Sun.COM 
28429615SAli.Bahrami@Sun.COM 			if (((isp = ifl->ifl_isdesc[ndx]) == NULL) ||
28439615SAli.Bahrami@Sun.COM 			    ((isp->is_flags &
28449615SAli.Bahrami@Sun.COM 			    (FLG_IS_PLACE | FLG_IS_ORDERED)) !=
28459615SAli.Bahrami@Sun.COM 			    (FLG_IS_PLACE | FLG_IS_ORDERED)))
28469615SAli.Bahrami@Sun.COM 				continue;
28479615SAli.Bahrami@Sun.COM 
28489615SAli.Bahrami@Sun.COM 			/* ld_process_ordered() calls ld_place_section() */
284911734SAli.Bahrami@Sun.COM 			if (ld_process_ordered(ofl, ifl, path_info, ndx) ==
285011734SAli.Bahrami@Sun.COM 			    S_ERROR)
28519615SAli.Bahrami@Sun.COM 				return (S_ERROR);
28529615SAli.Bahrami@Sun.COM 
28539615SAli.Bahrami@Sun.COM 			/* If we've done them all, stop searching */
28549615SAli.Bahrami@Sun.COM 			if (--ordcnt == 0)
28559615SAli.Bahrami@Sun.COM 				break;
28567463SRod.Evans@Sun.COM 		}
28577463SRod.Evans@Sun.COM 	}
28587463SRod.Evans@Sun.COM 
28597463SRod.Evans@Sun.COM 	/*
28609615SAli.Bahrami@Sun.COM 	 * If this is a shared object explicitly specified on the command
28619615SAli.Bahrami@Sun.COM 	 * line (as opposed to being a dependency of such an object),
28629615SAli.Bahrami@Sun.COM 	 * determine if the user has specified a control definition. This
28639615SAli.Bahrami@Sun.COM 	 * descriptor may specify which version definitions can be used
28649615SAli.Bahrami@Sun.COM 	 * from this object. It may also update the dependency to USED and
28659615SAli.Bahrami@Sun.COM 	 * supply an alternative SONAME.
28660Sstevel@tonic-gate 	 */
286710792SRod.Evans@Sun.COM 	sdf = NULL;
28680Sstevel@tonic-gate 	if (column && (ifl->ifl_flags & FLG_IF_NEEDED)) {
28690Sstevel@tonic-gate 		const char	*base;
28700Sstevel@tonic-gate 
28710Sstevel@tonic-gate 		/*
28720Sstevel@tonic-gate 		 * Use the basename of the input file (typically this is the
28730Sstevel@tonic-gate 		 * compilation environment name, ie. libfoo.so).
28740Sstevel@tonic-gate 		 */
28750Sstevel@tonic-gate 		if ((base = strrchr(ifl->ifl_name, '/')) == NULL)
28760Sstevel@tonic-gate 			base = ifl->ifl_name;
28770Sstevel@tonic-gate 		else
28780Sstevel@tonic-gate 			base++;
28790Sstevel@tonic-gate 
28809131SRod.Evans@Sun.COM 		if ((sdf = sdf_find(base, ofl->ofl_socntl)) != NULL) {
28810Sstevel@tonic-gate 			sdf->sdf_file = ifl;
28820Sstevel@tonic-gate 			ifl->ifl_sdfdesc = sdf;
28830Sstevel@tonic-gate 		}
28840Sstevel@tonic-gate 	}
28850Sstevel@tonic-gate 
28860Sstevel@tonic-gate 	/*
288711827SRod.Evans@Sun.COM 	 * Before symbol processing, process any capabilities.  Capabilities
288811827SRod.Evans@Sun.COM 	 * can reference a string table, which is why this processing is
288911827SRod.Evans@Sun.COM 	 * carried out after the initial section processing.  Capabilities,
289011827SRod.Evans@Sun.COM 	 * together with -z symbolcap, can require the conversion of global
289111827SRod.Evans@Sun.COM 	 * symbols to local symbols.
28920Sstevel@tonic-gate 	 */
289311827SRod.Evans@Sun.COM 	if (capisp && (process_cap(ofl, ifl, capisp) == S_ERROR))
289411827SRod.Evans@Sun.COM 		return (S_ERROR);
28950Sstevel@tonic-gate 
28960Sstevel@tonic-gate 	/*
28970Sstevel@tonic-gate 	 * Process any version dependencies.  These will establish shared object
28980Sstevel@tonic-gate 	 * `needed' entries in the same manner as will be generated from the
28990Sstevel@tonic-gate 	 * .dynamic's NEEDED entries.
29000Sstevel@tonic-gate 	 */
29010Sstevel@tonic-gate 	if (vndisp && (ofl->ofl_flags & (FLG_OF_NOUNDEF | FLG_OF_SYMBOLIC)))
29021618Srie 		if (ld_vers_need_process(vndisp, ifl, ofl) == S_ERROR)
29030Sstevel@tonic-gate 			return (S_ERROR);
29040Sstevel@tonic-gate 
29050Sstevel@tonic-gate 	/*
29060Sstevel@tonic-gate 	 * Before processing any symbol resolution or relocations process any
29070Sstevel@tonic-gate 	 * version sections.
29080Sstevel@tonic-gate 	 */
29090Sstevel@tonic-gate 	if (vsyisp)
29101618Srie 		(void) ld_vers_sym_process(ofl->ofl_lml, vsyisp, ifl);
29110Sstevel@tonic-gate 
29120Sstevel@tonic-gate 	if (ifl->ifl_versym &&
29130Sstevel@tonic-gate 	    (vdfisp || (sdf && (sdf->sdf_flags & FLG_SDF_SELECT))))
29141618Srie 		if (ld_vers_def_process(vdfisp, ifl, ofl) == S_ERROR)
29150Sstevel@tonic-gate 			return (S_ERROR);
29160Sstevel@tonic-gate 
29170Sstevel@tonic-gate 	/*
29180Sstevel@tonic-gate 	 * Having collected the appropriate sections carry out any additional
29190Sstevel@tonic-gate 	 * processing if necessary.
29200Sstevel@tonic-gate 	 */
29210Sstevel@tonic-gate 	for (ndx = 0; ndx < ifl->ifl_shnum; ndx++) {
29227463SRod.Evans@Sun.COM 		Is_desc	*isp;
29230Sstevel@tonic-gate 
292410792SRod.Evans@Sun.COM 		if ((isp = ifl->ifl_isdesc[ndx]) == NULL)
29250Sstevel@tonic-gate 			continue;
29260Sstevel@tonic-gate 		row = isp->is_shdr->sh_type;
29270Sstevel@tonic-gate 
29280Sstevel@tonic-gate 		if ((isp->is_flags & FLG_IS_DISCARD) == 0)
29291618Srie 			ld_sup_section(ofl, isp->is_name, isp->is_shdr, ndx,
29301618Srie 			    isp->is_indata, elf);
29310Sstevel@tonic-gate 
29320Sstevel@tonic-gate 		/*
29339131SRod.Evans@Sun.COM 		 * If this is a SHT_SUNW_move section from a relocatable file,
29349131SRod.Evans@Sun.COM 		 * keep track of the section for later processing.
29350Sstevel@tonic-gate 		 */
29360Sstevel@tonic-gate 		if ((row == SHT_SUNW_move) && (column == 0)) {
29379131SRod.Evans@Sun.COM 			if (aplist_append(&(ofl->ofl_ismove), isp,
29389131SRod.Evans@Sun.COM 			    AL_CNT_OFL_MOVE) == NULL)
29390Sstevel@tonic-gate 				return (S_ERROR);
29400Sstevel@tonic-gate 		}
29410Sstevel@tonic-gate 
29420Sstevel@tonic-gate 		/*
29430Sstevel@tonic-gate 		 * If this is a standard section type process it via the
29440Sstevel@tonic-gate 		 * appropriate action routine.
29450Sstevel@tonic-gate 		 */
29460Sstevel@tonic-gate 		if (row < SHT_NUM) {
29477463SRod.Evans@Sun.COM 			if (Final[row][column] != NULL) {
29487463SRod.Evans@Sun.COM 				if (Final[row][column](isp, ifl,
29497463SRod.Evans@Sun.COM 				    ofl) == S_ERROR)
29500Sstevel@tonic-gate 					return (S_ERROR);
29517463SRod.Evans@Sun.COM 			}
29527463SRod.Evans@Sun.COM #if	defined(_ELF64)
29537463SRod.Evans@Sun.COM 		} else if ((row == SHT_AMD64_UNWIND) && (column == 0)) {
29547463SRod.Evans@Sun.COM 			Os_desc	*osp = isp->is_osdesc;
29557463SRod.Evans@Sun.COM 
29567463SRod.Evans@Sun.COM 			/*
29577463SRod.Evans@Sun.COM 			 * SHT_AMD64_UNWIND (0x70000001) is in the SHT_LOPROC -
29587463SRod.Evans@Sun.COM 			 * SHT_HIPROC range reserved for processor-specific
29597463SRod.Evans@Sun.COM 			 * semantics, and is only meaningful for amd64 targets.
29607463SRod.Evans@Sun.COM 			 *
29617463SRod.Evans@Sun.COM 			 * Only process unwind contents from relocatable
29627463SRod.Evans@Sun.COM 			 * objects.
29637463SRod.Evans@Sun.COM 			 */
29647463SRod.Evans@Sun.COM 			if (osp && (ld_targ.t_m.m_mach == EM_AMD64) &&
29659085SAli.Bahrami@Sun.COM 			    (ld_unwind_register(osp, ofl) == S_ERROR))
29667463SRod.Evans@Sun.COM 				return (S_ERROR);
29677463SRod.Evans@Sun.COM #endif
29680Sstevel@tonic-gate 		}
29690Sstevel@tonic-gate 	}
29700Sstevel@tonic-gate 
29710Sstevel@tonic-gate 	/*
297211827SRod.Evans@Sun.COM 	 * Following symbol processing, if this relocatable object input file
297311827SRod.Evans@Sun.COM 	 * provides symbol capabilities, tag the associated symbols so that
297411827SRod.Evans@Sun.COM 	 * the symbols can be re-assigned to the new capabilities symbol
297511827SRod.Evans@Sun.COM 	 * section that will be created for the output file.
297611827SRod.Evans@Sun.COM 	 */
297711827SRod.Evans@Sun.COM 	if (capinfoisp && (ifl->ifl_ehdr->e_type == ET_REL) &&
297811827SRod.Evans@Sun.COM 	    (process_capinfo(ofl, ifl, capinfoisp) == S_ERROR))
297911827SRod.Evans@Sun.COM 		return (S_ERROR);
298011827SRod.Evans@Sun.COM 
298111827SRod.Evans@Sun.COM 	/*
29820Sstevel@tonic-gate 	 * After processing any symbol resolution, and if this dependency
29830Sstevel@tonic-gate 	 * indicates it contains symbols that can't be directly bound to,
29840Sstevel@tonic-gate 	 * set the symbols appropriately.
29850Sstevel@tonic-gate 	 */
29860Sstevel@tonic-gate 	if (sifisp && ((ifl->ifl_flags & (FLG_IF_NEEDED | FLG_IF_NODIRECT)) ==
29870Sstevel@tonic-gate 	    (FLG_IF_NEEDED | FLG_IF_NODIRECT)))
29881618Srie 		(void) ld_sym_nodirect(sifisp, ifl, ofl);
29890Sstevel@tonic-gate 
29900Sstevel@tonic-gate 	return (1);
29910Sstevel@tonic-gate }
29920Sstevel@tonic-gate 
29930Sstevel@tonic-gate /*
29940Sstevel@tonic-gate  * Process the current input file.  There are basically three types of files
29950Sstevel@tonic-gate  * that come through here:
29960Sstevel@tonic-gate  *
299710792SRod.Evans@Sun.COM  *  -	files explicitly defined on the command line (ie. foo.o or bar.so),
29980Sstevel@tonic-gate  *	in this case only the `name' field is valid.
29990Sstevel@tonic-gate  *
300010792SRod.Evans@Sun.COM  *  -	libraries determined from the -l command line option (ie. -lbar),
30010Sstevel@tonic-gate  *	in this case the `soname' field contains the basename of the located
30020Sstevel@tonic-gate  *	file.
30030Sstevel@tonic-gate  *
30040Sstevel@tonic-gate  * Any shared object specified via the above two conventions must be recorded
30050Sstevel@tonic-gate  * as a needed dependency.
30060Sstevel@tonic-gate  *
300710792SRod.Evans@Sun.COM  *  -	libraries specified as dependencies of those libraries already obtained
30080Sstevel@tonic-gate  *	via the command line (ie. bar.so has a DT_NEEDED entry of fred.so.1),
30090Sstevel@tonic-gate  *	in this case the `soname' field contains either a full pathname (if the
30100Sstevel@tonic-gate  *	needed entry contained a `/'), or the basename of the located file.
30110Sstevel@tonic-gate  *	These libraries are processed to verify symbol binding but are not
30120Sstevel@tonic-gate  *	recorded as dependencies of the output file being generated.
30130Sstevel@tonic-gate  */
30140Sstevel@tonic-gate Ifl_desc *
30151618Srie ld_process_ifl(const char *name, const char *soname, int fd, Elf *elf,
30167359SRod.Evans@Sun.COM     Word flags, Ofl_desc *ofl, Rej_desc *rej)
30170Sstevel@tonic-gate {
30180Sstevel@tonic-gate 	Ifl_desc	*ifl;
30190Sstevel@tonic-gate 	Ehdr		*ehdr;
30200Sstevel@tonic-gate 	uintptr_t	error = 0;
30210Sstevel@tonic-gate 	struct stat	status;
30220Sstevel@tonic-gate 	Ar_desc		*adp;
30230Sstevel@tonic-gate 	Rej_desc	_rej;
30240Sstevel@tonic-gate 
30250Sstevel@tonic-gate 	/*
30260Sstevel@tonic-gate 	 * If this file was not extracted from an archive obtain its device
30270Sstevel@tonic-gate 	 * information.  This will be used to determine if the file has already
30280Sstevel@tonic-gate 	 * been processed (rather than simply comparing filenames, the device
30290Sstevel@tonic-gate 	 * information provides a quicker comparison and detects linked files).
30300Sstevel@tonic-gate 	 */
30318598SRod.Evans@Sun.COM 	if (fd && ((flags & FLG_IF_EXTRACT) == 0))
30320Sstevel@tonic-gate 		(void) fstat(fd, &status);
30330Sstevel@tonic-gate 	else {
30340Sstevel@tonic-gate 		status.st_dev = 0;
30350Sstevel@tonic-gate 		status.st_ino = 0;
30360Sstevel@tonic-gate 	}
30370Sstevel@tonic-gate 
30380Sstevel@tonic-gate 	switch (elf_kind(elf)) {
30390Sstevel@tonic-gate 	case ELF_K_AR:
30400Sstevel@tonic-gate 		/*
30410Sstevel@tonic-gate 		 * Determine if we've already come across this archive file.
30420Sstevel@tonic-gate 		 */
30430Sstevel@tonic-gate 		if (!(flags & FLG_IF_EXTRACT)) {
30449131SRod.Evans@Sun.COM 			Aliste	idx;
30459131SRod.Evans@Sun.COM 
30469131SRod.Evans@Sun.COM 			for (APLIST_TRAVERSE(ofl->ofl_ars, idx, adp)) {
30470Sstevel@tonic-gate 				if ((adp->ad_stdev != status.st_dev) ||
30480Sstevel@tonic-gate 				    (adp->ad_stino != status.st_ino))
30490Sstevel@tonic-gate 					continue;
30500Sstevel@tonic-gate 
30510Sstevel@tonic-gate 				/*
30520Sstevel@tonic-gate 				 * We've seen this file before so reuse the
30530Sstevel@tonic-gate 				 * original archive descriptor and discard the
30547359SRod.Evans@Sun.COM 				 * new elf descriptor.  Note that a file
30557359SRod.Evans@Sun.COM 				 * descriptor is unnecessary, as the file is
30567359SRod.Evans@Sun.COM 				 * already available in memory.
30570Sstevel@tonic-gate 				 */
30581618Srie 				DBG_CALL(Dbg_file_reuse(ofl->ofl_lml, name,
30591618Srie 				    adp->ad_name));
30600Sstevel@tonic-gate 				(void) elf_end(elf);
30617359SRod.Evans@Sun.COM 				return ((Ifl_desc *)ld_process_archive(name, -1,
30620Sstevel@tonic-gate 				    adp, ofl));
30630Sstevel@tonic-gate 			}
30640Sstevel@tonic-gate 		}
30650Sstevel@tonic-gate 
30660Sstevel@tonic-gate 		/*
30670Sstevel@tonic-gate 		 * As we haven't processed this file before establish a new
30680Sstevel@tonic-gate 		 * archive descriptor.
30690Sstevel@tonic-gate 		 */
30701618Srie 		adp = ld_ar_setup(name, elf, ofl);
307110792SRod.Evans@Sun.COM 		if ((adp == NULL) || (adp == (Ar_desc *)S_ERROR))
30720Sstevel@tonic-gate 			return ((Ifl_desc *)adp);
30730Sstevel@tonic-gate 		adp->ad_stdev = status.st_dev;
30740Sstevel@tonic-gate 		adp->ad_stino = status.st_ino;
30750Sstevel@tonic-gate 
30761618Srie 		ld_sup_file(ofl, name, ELF_K_AR, flags, elf);
30770Sstevel@tonic-gate 
30787359SRod.Evans@Sun.COM 		/*
30797359SRod.Evans@Sun.COM 		 * Indicate that the ELF descriptor no longer requires a file
30807359SRod.Evans@Sun.COM 		 * descriptor by reading the entire file.  The file is already
30817359SRod.Evans@Sun.COM 		 * read via the initial mmap(2) behind elf_begin(3elf), thus
30827359SRod.Evans@Sun.COM 		 * this operation is effectively a no-op.  However, a side-
30837359SRod.Evans@Sun.COM 		 * effect is that the internal file descriptor, maintained in
30847359SRod.Evans@Sun.COM 		 * the ELF descriptor, is set to -1.  This setting will not
30857359SRod.Evans@Sun.COM 		 * be compared with any file descriptor that is passed to
30867359SRod.Evans@Sun.COM 		 * elf_begin(), should this archive, or one of the archive
30877359SRod.Evans@Sun.COM 		 * members, be processed again from the command line or
30887359SRod.Evans@Sun.COM 		 * because of a -z rescan.
30897359SRod.Evans@Sun.COM 		 */
30907359SRod.Evans@Sun.COM 		if (elf_cntl(elf, ELF_C_FDREAD) == -1) {
30917359SRod.Evans@Sun.COM 			eprintf(ofl->ofl_lml, ERR_ELF, MSG_INTL(MSG_ELF_CNTL),
30927359SRod.Evans@Sun.COM 			    name);
30937359SRod.Evans@Sun.COM 			ofl->ofl_flags |= FLG_OF_FATAL;
30947359SRod.Evans@Sun.COM 			return (NULL);
30957359SRod.Evans@Sun.COM 		}
30967359SRod.Evans@Sun.COM 
30977359SRod.Evans@Sun.COM 		return ((Ifl_desc *)ld_process_archive(name, -1, adp, ofl));
30980Sstevel@tonic-gate 
30990Sstevel@tonic-gate 	case ELF_K_ELF:
31000Sstevel@tonic-gate 		/*
31010Sstevel@tonic-gate 		 * Obtain the elf header so that we can determine what type of
31020Sstevel@tonic-gate 		 * elf ELF_K_ELF file this is.
31030Sstevel@tonic-gate 		 */
31040Sstevel@tonic-gate 		if ((ehdr = elf_getehdr(elf)) == NULL) {
31050Sstevel@tonic-gate 			int	_class = gelf_getclass(elf);
31060Sstevel@tonic-gate 
31070Sstevel@tonic-gate 			/*
31080Sstevel@tonic-gate 			 * Failure could occur for a number of reasons at this
31090Sstevel@tonic-gate 			 * point.  Typically the files class is incorrect (ie.
31100Sstevel@tonic-gate 			 * user is building 64-bit but managed to pint at 32-bit
31110Sstevel@tonic-gate 			 * libraries).  However any number of elf errors can
31120Sstevel@tonic-gate 			 * also occur, such as from a truncated or corrupt file.
31130Sstevel@tonic-gate 			 * Here we try and get the best error message possible.
31140Sstevel@tonic-gate 			 */
31156206Sab196087 			if (ld_targ.t_m.m_class != _class) {
31160Sstevel@tonic-gate 				_rej.rej_type = SGS_REJ_CLASS;
31170Sstevel@tonic-gate 				_rej.rej_info = (uint_t)_class;
31180Sstevel@tonic-gate 			} else {
31190Sstevel@tonic-gate 				_rej.rej_type = SGS_REJ_STR;
31200Sstevel@tonic-gate 				_rej.rej_str = elf_errmsg(-1);
31210Sstevel@tonic-gate 			}
31220Sstevel@tonic-gate 			_rej.rej_name = name;
31236206Sab196087 			DBG_CALL(Dbg_file_rejected(ofl->ofl_lml, &_rej,
31246206Sab196087 			    ld_targ.t_m.m_mach));
31250Sstevel@tonic-gate 			if (rej->rej_type == 0) {
31260Sstevel@tonic-gate 				*rej = _rej;
31270Sstevel@tonic-gate 				rej->rej_name = strdup(_rej.rej_name);
31280Sstevel@tonic-gate 			}
31297359SRod.Evans@Sun.COM 			return (NULL);
31300Sstevel@tonic-gate 		}
31310Sstevel@tonic-gate 
31320Sstevel@tonic-gate 		/*
31330Sstevel@tonic-gate 		 * Determine if we've already come across this file.
31340Sstevel@tonic-gate 		 */
31350Sstevel@tonic-gate 		if (!(flags & FLG_IF_EXTRACT)) {
31369131SRod.Evans@Sun.COM 			APlist	*apl;
31379131SRod.Evans@Sun.COM 			Aliste	idx;
31380Sstevel@tonic-gate 
31390Sstevel@tonic-gate 			if (ehdr->e_type == ET_REL)
31409131SRod.Evans@Sun.COM 				apl = ofl->ofl_objs;
31410Sstevel@tonic-gate 			else
31429131SRod.Evans@Sun.COM 				apl = ofl->ofl_sos;
31430Sstevel@tonic-gate 
31440Sstevel@tonic-gate 			/*
31450Sstevel@tonic-gate 			 * Traverse the appropriate file list and determine if
31460Sstevel@tonic-gate 			 * a dev/inode match is found.
31470Sstevel@tonic-gate 			 */
31489131SRod.Evans@Sun.COM 			for (APLIST_TRAVERSE(apl, idx, ifl)) {
31490Sstevel@tonic-gate 				/*
31500Sstevel@tonic-gate 				 * Ifl_desc generated via -Nneed, therefore no
31510Sstevel@tonic-gate 				 * actual file behind it.
31520Sstevel@tonic-gate 				 */
31530Sstevel@tonic-gate 				if (ifl->ifl_flags & FLG_IF_NEEDSTR)
31540Sstevel@tonic-gate 					continue;
31550Sstevel@tonic-gate 
31560Sstevel@tonic-gate 				if ((ifl->ifl_stino != status.st_ino) ||
31570Sstevel@tonic-gate 				    (ifl->ifl_stdev != status.st_dev))
31580Sstevel@tonic-gate 					continue;
31590Sstevel@tonic-gate 
31600Sstevel@tonic-gate 				/*
31610Sstevel@tonic-gate 				 * Disregard (skip) this image.
31620Sstevel@tonic-gate 				 */
31631618Srie 				DBG_CALL(Dbg_file_skip(ofl->ofl_lml,
31641618Srie 				    ifl->ifl_name, name));
31650Sstevel@tonic-gate 				(void) elf_end(elf);
31660Sstevel@tonic-gate 
31670Sstevel@tonic-gate 				/*
31680Sstevel@tonic-gate 				 * If the file was explicitly defined on the
31690Sstevel@tonic-gate 				 * command line (this is always the case for
31700Sstevel@tonic-gate 				 * relocatable objects, and is true for shared
31710Sstevel@tonic-gate 				 * objects when they weren't specified via -l or
31720Sstevel@tonic-gate 				 * were dragged in as an implicit dependency),
31730Sstevel@tonic-gate 				 * then warn the user.
31740Sstevel@tonic-gate 				 */
31750Sstevel@tonic-gate 				if ((flags & FLG_IF_CMDLINE) ||
31760Sstevel@tonic-gate 				    (ifl->ifl_flags & FLG_IF_CMDLINE)) {
31770Sstevel@tonic-gate 					const char	*errmsg;
31780Sstevel@tonic-gate 
31790Sstevel@tonic-gate 					/*
31800Sstevel@tonic-gate 					 * Determine whether this is the same
31810Sstevel@tonic-gate 					 * file name as originally encountered
31820Sstevel@tonic-gate 					 * so as to provide the most
31830Sstevel@tonic-gate 					 * descriptive diagnostic.
31840Sstevel@tonic-gate 					 */
31854734Sab196087 					errmsg =
31864734Sab196087 					    (strcmp(name, ifl->ifl_name) == 0) ?
31874734Sab196087 					    MSG_INTL(MSG_FIL_MULINC_1) :
31884734Sab196087 					    MSG_INTL(MSG_FIL_MULINC_2);
31891618Srie 					eprintf(ofl->ofl_lml, ERR_WARNING,
31901618Srie 					    errmsg, name, ifl->ifl_name);
31910Sstevel@tonic-gate 				}
31920Sstevel@tonic-gate 				return (ifl);
31930Sstevel@tonic-gate 			}
31940Sstevel@tonic-gate 		}
31950Sstevel@tonic-gate 
31960Sstevel@tonic-gate 		/*
31970Sstevel@tonic-gate 		 * At this point, we know we need the file.  Establish an input
31980Sstevel@tonic-gate 		 * file descriptor and continue processing.
31990Sstevel@tonic-gate 		 */
32000Sstevel@tonic-gate 		ifl = ifl_setup(name, ehdr, elf, flags, ofl, rej);
320110792SRod.Evans@Sun.COM 		if ((ifl == NULL) || (ifl == (Ifl_desc *)S_ERROR))
32020Sstevel@tonic-gate 			return (ifl);
32030Sstevel@tonic-gate 		ifl->ifl_stdev = status.st_dev;
32040Sstevel@tonic-gate 		ifl->ifl_stino = status.st_ino;
32050Sstevel@tonic-gate 
32060Sstevel@tonic-gate 		/*
32070Sstevel@tonic-gate 		 * If -zignore is in effect, mark this file as a potential
32080Sstevel@tonic-gate 		 * candidate (the files use isn't actually determined until
32090Sstevel@tonic-gate 		 * symbol resolution and relocation processing are completed).
32100Sstevel@tonic-gate 		 */
32110Sstevel@tonic-gate 		if (ofl->ofl_flags1 & FLG_OF1_IGNORE)
32120Sstevel@tonic-gate 			ifl->ifl_flags |= FLG_IF_IGNORE;
32130Sstevel@tonic-gate 
32140Sstevel@tonic-gate 		switch (ehdr->e_type) {
32150Sstevel@tonic-gate 		case ET_REL:
32166206Sab196087 			(*ld_targ.t_mr.mr_mach_eflags)(ehdr, ofl);
32170Sstevel@tonic-gate 			error = process_elf(ifl, elf, ofl);
32180Sstevel@tonic-gate 			break;
32190Sstevel@tonic-gate 		case ET_DYN:
32200Sstevel@tonic-gate 			if ((ofl->ofl_flags & FLG_OF_STATIC) ||
32210Sstevel@tonic-gate 			    !(ofl->ofl_flags & FLG_OF_DYNLIBS)) {
32221618Srie 				eprintf(ofl->ofl_lml, ERR_FATAL,
32231618Srie 				    MSG_INTL(MSG_FIL_SOINSTAT), name);
32240Sstevel@tonic-gate 				ofl->ofl_flags |= FLG_OF_FATAL;
32257359SRod.Evans@Sun.COM 				return (NULL);
32260Sstevel@tonic-gate 			}
32270Sstevel@tonic-gate 
32280Sstevel@tonic-gate 			/*
32290Sstevel@tonic-gate 			 * Record any additional shared object information.
32300Sstevel@tonic-gate 			 * If no soname is specified (eg. this file was
32310Sstevel@tonic-gate 			 * derived from a explicit filename declaration on the
32320Sstevel@tonic-gate 			 * command line, ie. bar.so) use the pathname.
32330Sstevel@tonic-gate 			 * This entry may be overridden if the files dynamic
32340Sstevel@tonic-gate 			 * section specifies an DT_SONAME value.
32350Sstevel@tonic-gate 			 */
32360Sstevel@tonic-gate 			if (soname == NULL)
32370Sstevel@tonic-gate 				ifl->ifl_soname = ifl->ifl_name;
32380Sstevel@tonic-gate 			else
32390Sstevel@tonic-gate 				ifl->ifl_soname = soname;
32400Sstevel@tonic-gate 
32410Sstevel@tonic-gate 			/*
32420Sstevel@tonic-gate 			 * If direct bindings, lazy loading, or group
32430Sstevel@tonic-gate 			 * permissions need to be established, mark this object.
32440Sstevel@tonic-gate 			 */
32450Sstevel@tonic-gate 			if (ofl->ofl_flags1 & FLG_OF1_ZDIRECT)
32460Sstevel@tonic-gate 				ifl->ifl_flags |= FLG_IF_DIRECT;
32470Sstevel@tonic-gate 			if (ofl->ofl_flags1 & FLG_OF1_LAZYLD)
32480Sstevel@tonic-gate 				ifl->ifl_flags |= FLG_IF_LAZYLD;
32490Sstevel@tonic-gate 			if (ofl->ofl_flags1 & FLG_OF1_GRPPRM)
32500Sstevel@tonic-gate 				ifl->ifl_flags |= FLG_IF_GRPPRM;
32510Sstevel@tonic-gate 			error = process_elf(ifl, elf, ofl);
32520Sstevel@tonic-gate 
32530Sstevel@tonic-gate 			/*
32540Sstevel@tonic-gate 			 * At this point we know if this file will be
32550Sstevel@tonic-gate 			 * lazyloaded, or whether bindings to it must be direct.
32560Sstevel@tonic-gate 			 * In either case, a syminfo section is required.
32570Sstevel@tonic-gate 			 */
32580Sstevel@tonic-gate 			if (ifl->ifl_flags & (FLG_IF_LAZYLD | FLG_IF_DIRECT))
32590Sstevel@tonic-gate 				ofl->ofl_flags |= FLG_OF_SYMINFO;
32600Sstevel@tonic-gate 
32610Sstevel@tonic-gate 			break;
32620Sstevel@tonic-gate 		default:
32630Sstevel@tonic-gate 			(void) elf_errno();
32640Sstevel@tonic-gate 			_rej.rej_type = SGS_REJ_UNKFILE;
32650Sstevel@tonic-gate 			_rej.rej_name = name;
32666206Sab196087 			DBG_CALL(Dbg_file_rejected(ofl->ofl_lml, &_rej,
32676206Sab196087 			    ld_targ.t_m.m_mach));
32680Sstevel@tonic-gate 			if (rej->rej_type == 0) {
32690Sstevel@tonic-gate 				*rej = _rej;
32700Sstevel@tonic-gate 				rej->rej_name = strdup(_rej.rej_name);
32710Sstevel@tonic-gate 			}
32727359SRod.Evans@Sun.COM 			return (NULL);
32730Sstevel@tonic-gate 		}
32740Sstevel@tonic-gate 		break;
32750Sstevel@tonic-gate 	default:
32760Sstevel@tonic-gate 		(void) elf_errno();
32770Sstevel@tonic-gate 		_rej.rej_type = SGS_REJ_UNKFILE;
32780Sstevel@tonic-gate 		_rej.rej_name = name;
32796206Sab196087 		DBG_CALL(Dbg_file_rejected(ofl->ofl_lml, &_rej,
32806206Sab196087 		    ld_targ.t_m.m_mach));
32810Sstevel@tonic-gate 		if (rej->rej_type == 0) {
32820Sstevel@tonic-gate 			*rej = _rej;
32830Sstevel@tonic-gate 			rej->rej_name = strdup(_rej.rej_name);
32840Sstevel@tonic-gate 		}
32857359SRod.Evans@Sun.COM 		return (NULL);
32860Sstevel@tonic-gate 	}
32870Sstevel@tonic-gate 	if ((error == 0) || (error == S_ERROR))
32880Sstevel@tonic-gate 		return ((Ifl_desc *)error);
32890Sstevel@tonic-gate 	else
32900Sstevel@tonic-gate 		return (ifl);
32910Sstevel@tonic-gate }
32920Sstevel@tonic-gate 
32930Sstevel@tonic-gate /*
32940Sstevel@tonic-gate  * Having successfully opened a file, set up the necessary elf structures to
32950Sstevel@tonic-gate  * process it further.  This small section of processing is slightly different
32960Sstevel@tonic-gate  * from the elf initialization required to process a relocatable object from an
32972850Srie  * archive (see libs.c: ld_process_archive()).
32980Sstevel@tonic-gate  */
32990Sstevel@tonic-gate Ifl_desc *
33002978Srie ld_process_open(const char *opath, const char *ofile, int *fd, Ofl_desc *ofl,
33014716Sab196087     Word flags, Rej_desc *rej)
33020Sstevel@tonic-gate {
33032978Srie 	Elf		*elf;
33042978Srie 	const char	*npath = opath;
33052978Srie 	const char	*nfile = ofile;
33060Sstevel@tonic-gate 
33072978Srie 	if ((elf = elf_begin(*fd, ELF_C_READ, NULL)) == NULL) {
33082978Srie 		eprintf(ofl->ofl_lml, ERR_ELF, MSG_INTL(MSG_ELF_BEGIN), npath);
33090Sstevel@tonic-gate 		ofl->ofl_flags |= FLG_OF_FATAL;
33107359SRod.Evans@Sun.COM 		return (NULL);
33110Sstevel@tonic-gate 	}
33120Sstevel@tonic-gate 
33132978Srie 	/*
33142978Srie 	 * Determine whether the support library wishes to process this open.
33152978Srie 	 * The support library may return:
33162978Srie 	 *   .	a different ELF descriptor (in which case they should have
33172978Srie 	 *	closed the original)
33182978Srie 	 *   .	a different file descriptor (in which case they should have
33192978Srie 	 *	closed the original)
33202978Srie 	 *   .	a different path and file name (presumably associated with
33212978Srie 	 *	a different file descriptor)
33222978Srie 	 *
33232978Srie 	 * A file descriptor of -1, or and ELF descriptor of zero indicates
33242978Srie 	 * the file should be ignored.
33252978Srie 	 */
33262978Srie 	ld_sup_open(ofl, &npath, &nfile, fd, flags, &elf, NULL, 0,
33272978Srie 	    elf_kind(elf));
33282978Srie 
33292978Srie 	if ((*fd == -1) || (elf == NULL))
33307359SRod.Evans@Sun.COM 		return (NULL);
33312978Srie 
33322978Srie 	return (ld_process_ifl(npath, nfile, *fd, elf, flags, ofl, rej));
33330Sstevel@tonic-gate }
33340Sstevel@tonic-gate 
33350Sstevel@tonic-gate /*
33368598SRod.Evans@Sun.COM  * Having successfully mapped a file, set up the necessary elf structures to
33378598SRod.Evans@Sun.COM  * process it further.  This routine is patterned after ld_process_open() and
33388598SRod.Evans@Sun.COM  * is only called by ld.so.1(1) to process a relocatable object.
33398598SRod.Evans@Sun.COM  */
33408598SRod.Evans@Sun.COM Ifl_desc *
33418598SRod.Evans@Sun.COM ld_process_mem(const char *path, const char *file, char *addr, size_t size,
33428598SRod.Evans@Sun.COM     Ofl_desc *ofl, Rej_desc *rej)
33438598SRod.Evans@Sun.COM {
33448598SRod.Evans@Sun.COM 	Elf	*elf;
33458598SRod.Evans@Sun.COM 
33468598SRod.Evans@Sun.COM 	if ((elf = elf_memory(addr, size)) == NULL) {
33478598SRod.Evans@Sun.COM 		eprintf(ofl->ofl_lml, ERR_ELF, MSG_INTL(MSG_ELF_MEMORY), path);
33488598SRod.Evans@Sun.COM 		ofl->ofl_flags |= FLG_OF_FATAL;
33498598SRod.Evans@Sun.COM 		return (0);
33508598SRod.Evans@Sun.COM 	}
33518598SRod.Evans@Sun.COM 
33528598SRod.Evans@Sun.COM 	return (ld_process_ifl(path, file, 0, elf, 0, ofl, rej));
33538598SRod.Evans@Sun.COM }
33548598SRod.Evans@Sun.COM 
33558598SRod.Evans@Sun.COM /*
33560Sstevel@tonic-gate  * Process a required library (i.e. the dependency of a shared object).
33570Sstevel@tonic-gate  * Combine the directory and filename, check the resultant path size, and try
33580Sstevel@tonic-gate  * opening the pathname.
33590Sstevel@tonic-gate  */
33601618Srie static Ifl_desc *
33610Sstevel@tonic-gate process_req_lib(Sdf_desc *sdf, const char *dir, const char *file,
33627463SRod.Evans@Sun.COM     Ofl_desc *ofl, Rej_desc *rej)
33630Sstevel@tonic-gate {
33640Sstevel@tonic-gate 	size_t		dlen, plen;
33650Sstevel@tonic-gate 	int		fd;
33660Sstevel@tonic-gate 	char		path[PATH_MAX];
33670Sstevel@tonic-gate 	const char	*_dir = dir;
33680Sstevel@tonic-gate 
33690Sstevel@tonic-gate 	/*
33700Sstevel@tonic-gate 	 * Determine the sizes of the directory and filename to insure we don't
33710Sstevel@tonic-gate 	 * exceed our buffer.
33720Sstevel@tonic-gate 	 */
33730Sstevel@tonic-gate 	if ((dlen = strlen(dir)) == 0) {
33740Sstevel@tonic-gate 		_dir = MSG_ORIG(MSG_STR_DOT);
33750Sstevel@tonic-gate 		dlen = 1;
33760Sstevel@tonic-gate 	}
33770Sstevel@tonic-gate 	dlen++;
33780Sstevel@tonic-gate 	plen = dlen + strlen(file) + 1;
33790Sstevel@tonic-gate 	if (plen > PATH_MAX) {
33801618Srie 		eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_FIL_PTHTOLONG),
33811618Srie 		    _dir, file);
33820Sstevel@tonic-gate 		ofl->ofl_flags |= FLG_OF_FATAL;
33830Sstevel@tonic-gate 		return (0);
33840Sstevel@tonic-gate 	}
33850Sstevel@tonic-gate 
33860Sstevel@tonic-gate 	/*
33870Sstevel@tonic-gate 	 * Build the entire pathname and try and open the file.
33880Sstevel@tonic-gate 	 */
33890Sstevel@tonic-gate 	(void) strcpy(path, _dir);
33900Sstevel@tonic-gate 	(void) strcat(path, MSG_ORIG(MSG_STR_SLASH));
33910Sstevel@tonic-gate 	(void) strcat(path, file);
33921618Srie 	DBG_CALL(Dbg_libs_req(ofl->ofl_lml, sdf->sdf_name,
33931618Srie 	    sdf->sdf_rfile, path));
33940Sstevel@tonic-gate 
33950Sstevel@tonic-gate 	if ((fd = open(path, O_RDONLY)) == -1)
33960Sstevel@tonic-gate 		return (0);
33970Sstevel@tonic-gate 	else {
33980Sstevel@tonic-gate 		Ifl_desc	*ifl;
33990Sstevel@tonic-gate 		char		*_path;
34000Sstevel@tonic-gate 
340110792SRod.Evans@Sun.COM 		if ((_path = libld_malloc(strlen(path) + 1)) == NULL)
34020Sstevel@tonic-gate 			return ((Ifl_desc *)S_ERROR);
34030Sstevel@tonic-gate 		(void) strcpy(_path, path);
34044716Sab196087 		ifl = ld_process_open(_path, &_path[dlen], &fd, ofl, 0, rej);
34052978Srie 		if (fd != -1)
34062978Srie 			(void) close(fd);
34070Sstevel@tonic-gate 		return (ifl);
34080Sstevel@tonic-gate 	}
34090Sstevel@tonic-gate }
34100Sstevel@tonic-gate 
34110Sstevel@tonic-gate /*
34120Sstevel@tonic-gate  * Finish any library processing.  Walk the list of so's that have been listed
34130Sstevel@tonic-gate  * as "included" by shared objects we have previously processed.  Examine them,
34140Sstevel@tonic-gate  * without adding them as explicit dependents of this program, in order to
34150Sstevel@tonic-gate  * complete our symbol definition process.  The search path rules are:
34160Sstevel@tonic-gate  *
341710792SRod.Evans@Sun.COM  *  -	use any user supplied paths, i.e. LD_LIBRARY_PATH and -L, then
34180Sstevel@tonic-gate  *
341910792SRod.Evans@Sun.COM  *  -	use any RPATH defined within the parent shared object, then
34200Sstevel@tonic-gate  *
342110792SRod.Evans@Sun.COM  *  -	use the default directories, i.e. LIBPATH or -YP.
34220Sstevel@tonic-gate  */
34230Sstevel@tonic-gate uintptr_t
34241618Srie ld_finish_libs(Ofl_desc *ofl)
34250Sstevel@tonic-gate {
34269131SRod.Evans@Sun.COM 	Aliste		idx1;
34270Sstevel@tonic-gate 	Sdf_desc	*sdf;
34280Sstevel@tonic-gate 	Rej_desc	rej = { 0 };
34290Sstevel@tonic-gate 
34300Sstevel@tonic-gate 	/*
34310Sstevel@tonic-gate 	 * Make sure we are back in dynamic mode.
34320Sstevel@tonic-gate 	 */
34330Sstevel@tonic-gate 	ofl->ofl_flags |= FLG_OF_DYNLIBS;
34340Sstevel@tonic-gate 
34359131SRod.Evans@Sun.COM 	for (APLIST_TRAVERSE(ofl->ofl_soneed, idx1, sdf)) {
34369131SRod.Evans@Sun.COM 		Aliste		idx2;
34372850Srie 		char		*path, *slash = NULL;
34380Sstevel@tonic-gate 		int		fd;
34390Sstevel@tonic-gate 		Ifl_desc	*ifl;
34402850Srie 		char		*file = (char *)sdf->sdf_name;
34410Sstevel@tonic-gate 
34420Sstevel@tonic-gate 		/*
34430Sstevel@tonic-gate 		 * See if this file has already been processed.  At the time
34440Sstevel@tonic-gate 		 * this implicit dependency was determined there may still have
34451109Srie 		 * been more explicit dependencies to process.  Note, if we ever
34460Sstevel@tonic-gate 		 * do parse the command line three times we would be able to
34471109Srie 		 * do all this checking when processing the dynamic section.
34480Sstevel@tonic-gate 		 */
34490Sstevel@tonic-gate 		if (sdf->sdf_file)
34500Sstevel@tonic-gate 			continue;
34510Sstevel@tonic-gate 
34529131SRod.Evans@Sun.COM 		for (APLIST_TRAVERSE(ofl->ofl_sos, idx2, ifl)) {
34530Sstevel@tonic-gate 			if (!(ifl->ifl_flags & FLG_IF_NEEDSTR) &&
34540Sstevel@tonic-gate 			    (strcmp(file, ifl->ifl_soname) == 0)) {
34550Sstevel@tonic-gate 				sdf->sdf_file = ifl;
34560Sstevel@tonic-gate 				break;
34570Sstevel@tonic-gate 			}
34580Sstevel@tonic-gate 		}
34590Sstevel@tonic-gate 		if (sdf->sdf_file)
34600Sstevel@tonic-gate 			continue;
34610Sstevel@tonic-gate 
34620Sstevel@tonic-gate 		/*
34632850Srie 		 * If the current path name element embeds a "/", then it's to
34642850Srie 		 * be taken "as is", with no searching involved.  Process all
34652850Srie 		 * "/" occurrences, so that we can deduce the base file name.
34660Sstevel@tonic-gate 		 */
34672850Srie 		for (path = file; *path; path++) {
34680Sstevel@tonic-gate 			if (*path == '/')
34692850Srie 				slash = path;
34702850Srie 		}
34712850Srie 		if (slash) {
34721618Srie 			DBG_CALL(Dbg_libs_req(ofl->ofl_lml, sdf->sdf_name,
34731618Srie 			    sdf->sdf_rfile, file));
34740Sstevel@tonic-gate 			if ((fd = open(file, O_RDONLY)) == -1) {
34751618Srie 				eprintf(ofl->ofl_lml, ERR_WARNING,
34761618Srie 				    MSG_INTL(MSG_FIL_NOTFOUND), file,
34771618Srie 				    sdf->sdf_rfile);
34780Sstevel@tonic-gate 			} else {
34790Sstevel@tonic-gate 				Rej_desc	_rej = { 0 };
34800Sstevel@tonic-gate 
34812978Srie 				ifl = ld_process_open(file, ++slash, &fd, ofl,
34824716Sab196087 				    0, &_rej);
34832978Srie 				if (fd != -1)
34842978Srie 					(void) close(fd);
34857359SRod.Evans@Sun.COM 				if (ifl == (Ifl_desc *)S_ERROR)
34867359SRod.Evans@Sun.COM 					return (S_ERROR);
34870Sstevel@tonic-gate 
34880Sstevel@tonic-gate 				if (_rej.rej_type) {
34894734Sab196087 					Conv_reject_desc_buf_t rej_buf;
34904734Sab196087 
34911618Srie 					eprintf(ofl->ofl_lml, ERR_WARNING,
34920Sstevel@tonic-gate 					    MSG_INTL(reject[_rej.rej_type]),
34930Sstevel@tonic-gate 					    _rej.rej_name ? rej.rej_name :
34940Sstevel@tonic-gate 					    MSG_INTL(MSG_STR_UNKNOWN),
34956206Sab196087 					    conv_reject_desc(&_rej, &rej_buf,
34966206Sab196087 					    ld_targ.t_m.m_mach));
34970Sstevel@tonic-gate 				} else
34980Sstevel@tonic-gate 					sdf->sdf_file = ifl;
34990Sstevel@tonic-gate 			}
35000Sstevel@tonic-gate 			continue;
35010Sstevel@tonic-gate 		}
35020Sstevel@tonic-gate 
35030Sstevel@tonic-gate 		/*
35040Sstevel@tonic-gate 		 * Now search for this file in any user defined directories.
35050Sstevel@tonic-gate 		 */
35069131SRod.Evans@Sun.COM 		for (APLIST_TRAVERSE(ofl->ofl_ulibdirs, idx2, path)) {
35070Sstevel@tonic-gate 			Rej_desc	_rej = { 0 };
35080Sstevel@tonic-gate 
35090Sstevel@tonic-gate 			ifl = process_req_lib(sdf, path, file, ofl, &_rej);
35100Sstevel@tonic-gate 			if (ifl == (Ifl_desc *)S_ERROR) {
35110Sstevel@tonic-gate 				return (S_ERROR);
35120Sstevel@tonic-gate 			}
35130Sstevel@tonic-gate 			if (_rej.rej_type) {
35140Sstevel@tonic-gate 				if (rej.rej_type == 0) {
35150Sstevel@tonic-gate 					rej = _rej;
35160Sstevel@tonic-gate 					rej.rej_name = strdup(_rej.rej_name);
35170Sstevel@tonic-gate 				}
35180Sstevel@tonic-gate 			}
35190Sstevel@tonic-gate 			if (ifl) {
35200Sstevel@tonic-gate 				sdf->sdf_file = ifl;
35210Sstevel@tonic-gate 				break;
35220Sstevel@tonic-gate 			}
35230Sstevel@tonic-gate 		}
35240Sstevel@tonic-gate 		if (sdf->sdf_file)
35250Sstevel@tonic-gate 			continue;
35260Sstevel@tonic-gate 
35270Sstevel@tonic-gate 		/*
35280Sstevel@tonic-gate 		 * Next use the local rules defined within the parent shared
35290Sstevel@tonic-gate 		 * object.
35300Sstevel@tonic-gate 		 */
35310Sstevel@tonic-gate 		if (sdf->sdf_rpath != NULL) {
35320Sstevel@tonic-gate 			char	*rpath, *next;
35330Sstevel@tonic-gate 
35340Sstevel@tonic-gate 			rpath = libld_malloc(strlen(sdf->sdf_rpath) + 1);
353510792SRod.Evans@Sun.COM 			if (rpath == NULL)
35360Sstevel@tonic-gate 				return (S_ERROR);
35370Sstevel@tonic-gate 			(void) strcpy(rpath, sdf->sdf_rpath);
35381618Srie 			DBG_CALL(Dbg_libs_path(ofl->ofl_lml, rpath,
35391618Srie 			    LA_SER_RUNPATH, sdf->sdf_rfile));
35400Sstevel@tonic-gate 			if ((path = strtok_r(rpath,
35410Sstevel@tonic-gate 			    MSG_ORIG(MSG_STR_COLON), &next)) != NULL) {
35420Sstevel@tonic-gate 				do {
35430Sstevel@tonic-gate 					Rej_desc	_rej = { 0 };
35440Sstevel@tonic-gate 
35450Sstevel@tonic-gate 					path = expand(sdf->sdf_rfile, path,
35460Sstevel@tonic-gate 					    &next);
35470Sstevel@tonic-gate 
35480Sstevel@tonic-gate 					ifl = process_req_lib(sdf, path,
35494716Sab196087 					    file, ofl, &_rej);
35500Sstevel@tonic-gate 					if (ifl == (Ifl_desc *)S_ERROR) {
35510Sstevel@tonic-gate 						return (S_ERROR);
35520Sstevel@tonic-gate 					}
35534716Sab196087 					if ((_rej.rej_type) &&
35544716Sab196087 					    (rej.rej_type == 0)) {
35554716Sab196087 						rej = _rej;
35564716Sab196087 						rej.rej_name =
35574716Sab196087 						    strdup(_rej.rej_name);
35580Sstevel@tonic-gate 					}
35590Sstevel@tonic-gate 					if (ifl) {
35600Sstevel@tonic-gate 						sdf->sdf_file = ifl;
35610Sstevel@tonic-gate 						break;
35620Sstevel@tonic-gate 					}
35630Sstevel@tonic-gate 				} while ((path = strtok_r(NULL,
35640Sstevel@tonic-gate 				    MSG_ORIG(MSG_STR_COLON), &next)) != NULL);
35650Sstevel@tonic-gate 			}
35660Sstevel@tonic-gate 		}
35670Sstevel@tonic-gate 		if (sdf->sdf_file)
35680Sstevel@tonic-gate 			continue;
35690Sstevel@tonic-gate 
35700Sstevel@tonic-gate 		/*
35710Sstevel@tonic-gate 		 * Finally try the default library search directories.
35720Sstevel@tonic-gate 		 */
35739131SRod.Evans@Sun.COM 		for (APLIST_TRAVERSE(ofl->ofl_dlibdirs, idx2, path)) {
35740Sstevel@tonic-gate 			Rej_desc	_rej = { 0 };
35750Sstevel@tonic-gate 
35760Sstevel@tonic-gate 			ifl = process_req_lib(sdf, path, file, ofl, &rej);
35770Sstevel@tonic-gate 			if (ifl == (Ifl_desc *)S_ERROR) {
35780Sstevel@tonic-gate 				return (S_ERROR);
35790Sstevel@tonic-gate 			}
35800Sstevel@tonic-gate 			if (_rej.rej_type) {
35810Sstevel@tonic-gate 				if (rej.rej_type == 0) {
35820Sstevel@tonic-gate 					rej = _rej;
35830Sstevel@tonic-gate 					rej.rej_name = strdup(_rej.rej_name);
35840Sstevel@tonic-gate 				}
35850Sstevel@tonic-gate 			}
35860Sstevel@tonic-gate 			if (ifl) {
35870Sstevel@tonic-gate 				sdf->sdf_file = ifl;
35880Sstevel@tonic-gate 				break;
35890Sstevel@tonic-gate 			}
35900Sstevel@tonic-gate 		}
35910Sstevel@tonic-gate 		if (sdf->sdf_file)
35920Sstevel@tonic-gate 			continue;
35930Sstevel@tonic-gate 
35940Sstevel@tonic-gate 		/*
35950Sstevel@tonic-gate 		 * If we've got this far we haven't found the shared object.
35960Sstevel@tonic-gate 		 * If an object was found, but was rejected for some reason,
35970Sstevel@tonic-gate 		 * print a diagnostic to that effect, otherwise generate a
35980Sstevel@tonic-gate 		 * generic "not found" diagnostic.
35990Sstevel@tonic-gate 		 */
36000Sstevel@tonic-gate 		if (rej.rej_type) {
36014734Sab196087 			Conv_reject_desc_buf_t rej_buf;
36024734Sab196087 
36031618Srie 			eprintf(ofl->ofl_lml, ERR_WARNING,
36041618Srie 			    MSG_INTL(reject[rej.rej_type]),
36050Sstevel@tonic-gate 			    rej.rej_name ? rej.rej_name :
36064734Sab196087 			    MSG_INTL(MSG_STR_UNKNOWN),
36076206Sab196087 			    conv_reject_desc(&rej, &rej_buf,
36086206Sab196087 			    ld_targ.t_m.m_mach));
36090Sstevel@tonic-gate 		} else {
36101618Srie 			eprintf(ofl->ofl_lml, ERR_WARNING,
36111618Srie 			    MSG_INTL(MSG_FIL_NOTFOUND), file, sdf->sdf_rfile);
36120Sstevel@tonic-gate 		}
36130Sstevel@tonic-gate 	}
36140Sstevel@tonic-gate 
36150Sstevel@tonic-gate 	/*
36160Sstevel@tonic-gate 	 * Finally, now that all objects have been input, make sure any version
36170Sstevel@tonic-gate 	 * requirements have been met.
36180Sstevel@tonic-gate 	 */
36191618Srie 	return (ld_vers_verify(ofl));
36200Sstevel@tonic-gate }
3621