xref: /onnv-gate/usr/src/cmd/sgs/libld/common/files.c (revision 11827)
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 	/*
248*11827SRod.Evans@Sun.COM 	 * If a mapfile has established definitions to override any object
249*11827SRod.Evans@Sun.COM 	 * capabilities, ignore any new object capabilities.
2500Sstevel@tonic-gate 	 */
25111734SAli.Bahrami@Sun.COM 	if (ofl->ofl_flags1 & FLG_OF1_OVSFCAP1) {
252*11827SRod.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)
258*11827SRod.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) &&
312*11827SRod.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) {
323*11827SRod.Evans@Sun.COM 		Dbg_cap_val_entry(ofl->ofl_lml, DBG_STATE_CURRENT, CA_SUNW_SF_1,
324*11827SRod.Evans@Sun.COM 		    ofl->ofl_ocapset.oc_sf_1.cm_val, ld_targ.t_m.m_mach);
325*11827SRod.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 	 */
333*11827SRod.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)
339*11827SRod.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 
347*11827SRod.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 		 */
353*11827SRod.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 
359*11827SRod.Evans@Sun.COM 	ofl->ofl_ocapset.oc_sf_1.cm_val |= val;
360*11827SRod.Evans@Sun.COM 
361*11827SRod.Evans@Sun.COM 	DBG_CALL(Dbg_cap_val_entry(ofl->ofl_lml, DBG_STATE_RESOLVED,
362*11827SRod.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
374*11827SRod.Evans@Sun.COM hw_cap(Ofl_desc *ofl, Xword tag, Xword val)
3750Sstevel@tonic-gate {
376*11827SRod.Evans@Sun.COM 	elfcap_mask_t	*hwcap;
377*11827SRod.Evans@Sun.COM 	ofl_flag_t	flags1;
378*11827SRod.Evans@Sun.COM 
379*11827SRod.Evans@Sun.COM 	if (tag == CA_SUNW_HW_1) {
380*11827SRod.Evans@Sun.COM 		hwcap = &ofl->ofl_ocapset.oc_hw_1.cm_val;
381*11827SRod.Evans@Sun.COM 		flags1 = FLG_OF1_OVHWCAP1;
382*11827SRod.Evans@Sun.COM 	} else {
383*11827SRod.Evans@Sun.COM 		hwcap = &ofl->ofl_ocapset.oc_hw_2.cm_val;
384*11827SRod.Evans@Sun.COM 		flags1 = FLG_OF1_OVHWCAP2;
385*11827SRod.Evans@Sun.COM 	}
386*11827SRod.Evans@Sun.COM 
3870Sstevel@tonic-gate 	/*
388*11827SRod.Evans@Sun.COM 	 * If a mapfile has established definitions to override any object
389*11827SRod.Evans@Sun.COM 	 * capabilities, ignore any new object capabilities.
3900Sstevel@tonic-gate 	 */
391*11827SRod.Evans@Sun.COM 	if (ofl->ofl_flags1 & flags1) {
392*11827SRod.Evans@Sun.COM 		DBG_CALL(Dbg_cap_val_entry(ofl->ofl_lml, DBG_STATE_IGNORED,
393*11827SRod.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) {
405*11827SRod.Evans@Sun.COM 		Dbg_cap_val_entry(ofl->ofl_lml, DBG_STATE_CURRENT, CA_SUNW_HW_1,
406*11827SRod.Evans@Sun.COM 		    ofl->ofl_ocapset.oc_hw_1.cm_val, ld_targ.t_m.m_mach);
407*11827SRod.Evans@Sun.COM 		Dbg_cap_val_entry(ofl->ofl_lml, DBG_STATE_NEW, CA_SUNW_HW_1,
408*11827SRod.Evans@Sun.COM 		    val, ld_targ.t_m.m_mach);
409*11827SRod.Evans@Sun.COM 	}
410*11827SRod.Evans@Sun.COM 
411*11827SRod.Evans@Sun.COM 	*hwcap |= val;
412*11827SRod.Evans@Sun.COM 
413*11827SRod.Evans@Sun.COM 	DBG_CALL(Dbg_cap_val_entry(ofl->ofl_lml, DBG_STATE_RESOLVED, tag,
414*11827SRod.Evans@Sun.COM 	    *hwcap, ld_targ.t_m.m_mach));
415*11827SRod.Evans@Sun.COM }
416*11827SRod.Evans@Sun.COM 
417*11827SRod.Evans@Sun.COM /*
418*11827SRod.Evans@Sun.COM  * Promote a machine capability or platform capability to the output file.
419*11827SRod.Evans@Sun.COM  * Multiple instances of these names can be defined.
420*11827SRod.Evans@Sun.COM  */
421*11827SRod.Evans@Sun.COM static void
422*11827SRod.Evans@Sun.COM str_cap(Ofl_desc *ofl, char *pstr, ofl_flag_t flags, Xword tag, Caplist *list)
423*11827SRod.Evans@Sun.COM {
424*11827SRod.Evans@Sun.COM 	Capstr		*capstr;
425*11827SRod.Evans@Sun.COM 	Aliste		idx;
426*11827SRod.Evans@Sun.COM 	Boolean		found = FALSE;
427*11827SRod.Evans@Sun.COM 
428*11827SRod.Evans@Sun.COM 	/*
429*11827SRod.Evans@Sun.COM 	 * If a mapfile has established definitions to override this capability,
430*11827SRod.Evans@Sun.COM 	 * ignore any new capability.
431*11827SRod.Evans@Sun.COM 	 */
432*11827SRod.Evans@Sun.COM 	if (ofl->ofl_flags1 & flags) {
433*11827SRod.Evans@Sun.COM 		DBG_CALL(Dbg_cap_ptr_entry(ofl->ofl_lml, DBG_STATE_IGNORED,
434*11827SRod.Evans@Sun.COM 		    tag, pstr));
435*11827SRod.Evans@Sun.COM 		return;
436*11827SRod.Evans@Sun.COM 	}
437*11827SRod.Evans@Sun.COM 
438*11827SRod.Evans@Sun.COM 	for (ALIST_TRAVERSE(list->cl_val, idx, capstr)) {
439*11827SRod.Evans@Sun.COM 		DBG_CALL(Dbg_cap_ptr_entry(ofl->ofl_lml,
440*11827SRod.Evans@Sun.COM 		    DBG_STATE_CURRENT, tag, capstr->cs_str));
441*11827SRod.Evans@Sun.COM 		if (strcmp(capstr->cs_str, pstr) == 0)
442*11827SRod.Evans@Sun.COM 			found = TRUE;
443*11827SRod.Evans@Sun.COM 	}
444*11827SRod.Evans@Sun.COM 
445*11827SRod.Evans@Sun.COM 	DBG_CALL(Dbg_cap_ptr_entry(ofl->ofl_lml, DBG_STATE_NEW, tag, pstr));
446*11827SRod.Evans@Sun.COM 
447*11827SRod.Evans@Sun.COM 	if (found == FALSE) {
448*11827SRod.Evans@Sun.COM 		if ((capstr = alist_append(&list->cl_val, NULL,
449*11827SRod.Evans@Sun.COM 		    sizeof (Capstr), AL_CNT_CAP_NAMES)) == NULL) {
450*11827SRod.Evans@Sun.COM 			ofl->ofl_flags |= FLG_OF_FATAL;
451*11827SRod.Evans@Sun.COM 			return;
452*11827SRod.Evans@Sun.COM 		}
453*11827SRod.Evans@Sun.COM 		capstr->cs_str = pstr;
454*11827SRod.Evans@Sun.COM 	}
455*11827SRod.Evans@Sun.COM 
456*11827SRod.Evans@Sun.COM 	if (DBG_ENABLED) {
457*11827SRod.Evans@Sun.COM 		for (ALIST_TRAVERSE(list->cl_val, idx, capstr)) {
458*11827SRod.Evans@Sun.COM 			DBG_CALL(Dbg_cap_ptr_entry(ofl->ofl_lml,
459*11827SRod.Evans@Sun.COM 			    DBG_STATE_RESOLVED, tag, capstr->cs_str));
460*11827SRod.Evans@Sun.COM 		}
46111734SAli.Bahrami@Sun.COM 	}
462*11827SRod.Evans@Sun.COM }
463*11827SRod.Evans@Sun.COM 
464*11827SRod.Evans@Sun.COM /*
465*11827SRod.Evans@Sun.COM  * Promote a capability identifier to the output file.  A capability group can
466*11827SRod.Evans@Sun.COM  * only have one identifier, and thus only the first identifier seen from any
467*11827SRod.Evans@Sun.COM  * input relocatable objects is retained.  An explicit user defined identifier,
468*11827SRod.Evans@Sun.COM  * rather than an an identifier fabricated by ld(1) with -z symbcap processing,
469*11827SRod.Evans@Sun.COM  * takes precedence.  Note, a user may have defined an identifier via a mapfile,
470*11827SRod.Evans@Sun.COM  * in which case the mapfile identifier is retained.
471*11827SRod.Evans@Sun.COM  */
472*11827SRod.Evans@Sun.COM static void
473*11827SRod.Evans@Sun.COM id_cap(Ofl_desc *ofl, char *pstr, oc_flag_t flags)
474*11827SRod.Evans@Sun.COM {
475*11827SRod.Evans@Sun.COM 	Objcapset	*ocapset = &ofl->ofl_ocapset;
476*11827SRod.Evans@Sun.COM 
477*11827SRod.Evans@Sun.COM 	if (ocapset->oc_id.cs_str) {
478*11827SRod.Evans@Sun.COM 		DBG_CALL(Dbg_cap_ptr_entry(ofl->ofl_lml, DBG_STATE_CURRENT,
479*11827SRod.Evans@Sun.COM 		    CA_SUNW_ID, ocapset->oc_id.cs_str));
480*11827SRod.Evans@Sun.COM 
481*11827SRod.Evans@Sun.COM 		if ((ocapset->oc_flags & FLG_OCS_USRDEFID) ||
482*11827SRod.Evans@Sun.COM 		    ((flags & FLG_OCS_USRDEFID) == 0)) {
483*11827SRod.Evans@Sun.COM 			DBG_CALL(Dbg_cap_ptr_entry(ofl->ofl_lml,
484*11827SRod.Evans@Sun.COM 			    DBG_STATE_IGNORED, CA_SUNW_ID, pstr));
485*11827SRod.Evans@Sun.COM 			return;
486*11827SRod.Evans@Sun.COM 		}
487*11827SRod.Evans@Sun.COM 	}
488*11827SRod.Evans@Sun.COM 
489*11827SRod.Evans@Sun.COM 	DBG_CALL(Dbg_cap_ptr_entry(ofl->ofl_lml, DBG_STATE_NEW,
490*11827SRod.Evans@Sun.COM 	    CA_SUNW_ID, pstr));
491*11827SRod.Evans@Sun.COM 
492*11827SRod.Evans@Sun.COM 	ocapset->oc_id.cs_str = pstr;
493*11827SRod.Evans@Sun.COM 	ocapset->oc_flags |= flags;
494*11827SRod.Evans@Sun.COM 
495*11827SRod.Evans@Sun.COM 	DBG_CALL(Dbg_cap_ptr_entry(ofl->ofl_lml, DBG_STATE_RESOLVED,
496*11827SRod.Evans@Sun.COM 	    CA_SUNW_ID, pstr));
497*11827SRod.Evans@Sun.COM }
498*11827SRod.Evans@Sun.COM 
499*11827SRod.Evans@Sun.COM /*
500*11827SRod.Evans@Sun.COM  * Promote a capabilities group to the object capabilities.  This catches a
501*11827SRod.Evans@Sun.COM  * corner case.  An object capabilities file can be converted to symbol
502*11827SRod.Evans@Sun.COM  * capabilities with -z symbolcap.  However, if the user has indicated that all
503*11827SRod.Evans@Sun.COM  * the symbols should be demoted, we'd be left with a symbol capabilities file,
504*11827SRod.Evans@Sun.COM  * with no associated symbols.  Catch this case by promoting the symbol
505*11827SRod.Evans@Sun.COM  * capabilities back to object capabilities.
506*11827SRod.Evans@Sun.COM  */
507*11827SRod.Evans@Sun.COM void
508*11827SRod.Evans@Sun.COM ld_cap_move_symtoobj(Ofl_desc *ofl)
509*11827SRod.Evans@Sun.COM {
510*11827SRod.Evans@Sun.COM 	Cap_group	*cgp;
511*11827SRod.Evans@Sun.COM 	Aliste		idx1;
512*11827SRod.Evans@Sun.COM 
513*11827SRod.Evans@Sun.COM 	for (APLIST_TRAVERSE(ofl->ofl_capgroups, idx1, cgp)) {
514*11827SRod.Evans@Sun.COM 		Objcapset	*scapset = &cgp->cg_set;
515*11827SRod.Evans@Sun.COM 		Capstr		*capstr;
516*11827SRod.Evans@Sun.COM 		Aliste		idx2;
517*11827SRod.Evans@Sun.COM 
518*11827SRod.Evans@Sun.COM 		if (scapset->oc_id.cs_str) {
519*11827SRod.Evans@Sun.COM 			if (scapset->oc_flags & FLG_OCS_USRDEFID)
520*11827SRod.Evans@Sun.COM 				id_cap(ofl, scapset->oc_id.cs_str,
521*11827SRod.Evans@Sun.COM 				    scapset->oc_flags);
522*11827SRod.Evans@Sun.COM 		}
523*11827SRod.Evans@Sun.COM 		if (scapset->oc_plat.cl_val) {
524*11827SRod.Evans@Sun.COM 			for (ALIST_TRAVERSE(scapset->oc_plat.cl_val, idx2,
525*11827SRod.Evans@Sun.COM 			    capstr)) {
526*11827SRod.Evans@Sun.COM 				str_cap(ofl, capstr->cs_str, FLG_OF1_OVPLATCAP,
527*11827SRod.Evans@Sun.COM 				    CA_SUNW_PLAT, &ofl->ofl_ocapset.oc_plat);
528*11827SRod.Evans@Sun.COM 			}
529*11827SRod.Evans@Sun.COM 		}
530*11827SRod.Evans@Sun.COM 		if (scapset->oc_mach.cl_val) {
531*11827SRod.Evans@Sun.COM 			for (ALIST_TRAVERSE(scapset->oc_mach.cl_val, idx2,
532*11827SRod.Evans@Sun.COM 			    capstr)) {
533*11827SRod.Evans@Sun.COM 				str_cap(ofl, capstr->cs_str, FLG_OF1_OVMACHCAP,
534*11827SRod.Evans@Sun.COM 				    CA_SUNW_MACH, &ofl->ofl_ocapset.oc_mach);
535*11827SRod.Evans@Sun.COM 			}
536*11827SRod.Evans@Sun.COM 		}
537*11827SRod.Evans@Sun.COM 		if (scapset->oc_hw_2.cm_val)
538*11827SRod.Evans@Sun.COM 			hw_cap(ofl, CA_SUNW_HW_2, scapset->oc_hw_2.cm_val);
539*11827SRod.Evans@Sun.COM 
540*11827SRod.Evans@Sun.COM 		if (scapset->oc_hw_1.cm_val)
541*11827SRod.Evans@Sun.COM 			hw_cap(ofl, CA_SUNW_HW_1, scapset->oc_hw_1.cm_val);
542*11827SRod.Evans@Sun.COM 
543*11827SRod.Evans@Sun.COM 		if (scapset->oc_sf_1.cm_val)
544*11827SRod.Evans@Sun.COM 			sf1_cap(ofl, scapset->oc_sf_1.cm_val, NULL, NULL);
545*11827SRod.Evans@Sun.COM 	}
5460Sstevel@tonic-gate }
5470Sstevel@tonic-gate 
5480Sstevel@tonic-gate /*
549*11827SRod.Evans@Sun.COM  * Determine whether a capabilities group already exists that describes this
550*11827SRod.Evans@Sun.COM  * new capabilities group.
551*11827SRod.Evans@Sun.COM  *
552*11827SRod.Evans@Sun.COM  * Note, a capability group identifier, CA_SUNW_ID, isn't used as part of the
553*11827SRod.Evans@Sun.COM  * comparison.  This attribute simply assigns a diagnostic name to the group,
554*11827SRod.Evans@Sun.COM  * and in the case of multiple identifiers, the first will be taken.
5550Sstevel@tonic-gate  */
556*11827SRod.Evans@Sun.COM static Cap_group *
557*11827SRod.Evans@Sun.COM get_cap_group(Objcapset *ocapset, Word cnum, Ofl_desc *ofl, Is_desc *isp)
5580Sstevel@tonic-gate {
559*11827SRod.Evans@Sun.COM 	Aliste		idx;
560*11827SRod.Evans@Sun.COM 	Cap_group	*cgp;
561*11827SRod.Evans@Sun.COM 	Word		ccnum = cnum;
562*11827SRod.Evans@Sun.COM 
563*11827SRod.Evans@Sun.COM 	/*
564*11827SRod.Evans@Sun.COM 	 * If the new capabilities contains a CA_SUNW_ID, drop the count of the
565*11827SRod.Evans@Sun.COM 	 * number of comparable items.
566*11827SRod.Evans@Sun.COM 	 */
567*11827SRod.Evans@Sun.COM 	if (ocapset->oc_id.cs_str)
568*11827SRod.Evans@Sun.COM 		ccnum--;
569*11827SRod.Evans@Sun.COM 
570*11827SRod.Evans@Sun.COM 	/*
571*11827SRod.Evans@Sun.COM 	 * Traverse the existing symbols capabilities groups.
572*11827SRod.Evans@Sun.COM 	 */
573*11827SRod.Evans@Sun.COM 	for (APLIST_TRAVERSE(ofl->ofl_capgroups, idx, cgp)) {
574*11827SRod.Evans@Sun.COM 		Word	onum = cgp->cg_num;
575*11827SRod.Evans@Sun.COM 		Alist	*calp, *oalp;
576*11827SRod.Evans@Sun.COM 
577*11827SRod.Evans@Sun.COM 		if (cgp->cg_set.oc_id.cs_str)
578*11827SRod.Evans@Sun.COM 			onum--;
579*11827SRod.Evans@Sun.COM 
580*11827SRod.Evans@Sun.COM 		if (onum != ccnum)
581*11827SRod.Evans@Sun.COM 			continue;
582*11827SRod.Evans@Sun.COM 
583*11827SRod.Evans@Sun.COM 		if (cgp->cg_set.oc_hw_1.cm_val != ocapset->oc_hw_1.cm_val)
584*11827SRod.Evans@Sun.COM 			continue;
585*11827SRod.Evans@Sun.COM 		if (cgp->cg_set.oc_sf_1.cm_val != ocapset->oc_sf_1.cm_val)
586*11827SRod.Evans@Sun.COM 			continue;
587*11827SRod.Evans@Sun.COM 		if (cgp->cg_set.oc_hw_2.cm_val != ocapset->oc_hw_2.cm_val)
588*11827SRod.Evans@Sun.COM 			continue;
589*11827SRod.Evans@Sun.COM 
590*11827SRod.Evans@Sun.COM 		calp = cgp->cg_set.oc_plat.cl_val;
591*11827SRod.Evans@Sun.COM 		oalp = ocapset->oc_plat.cl_val;
592*11827SRod.Evans@Sun.COM 		if ((calp == NULL) && oalp)
593*11827SRod.Evans@Sun.COM 			continue;
594*11827SRod.Evans@Sun.COM 		if (calp && ((oalp == NULL) || cap_names_match(calp, oalp)))
595*11827SRod.Evans@Sun.COM 			continue;
596*11827SRod.Evans@Sun.COM 
597*11827SRod.Evans@Sun.COM 		calp = cgp->cg_set.oc_mach.cl_val;
598*11827SRod.Evans@Sun.COM 		oalp = ocapset->oc_mach.cl_val;
599*11827SRod.Evans@Sun.COM 		if ((calp == NULL) && oalp)
600*11827SRod.Evans@Sun.COM 			continue;
601*11827SRod.Evans@Sun.COM 		if (calp && ((oalp == NULL) || cap_names_match(calp, oalp)))
602*11827SRod.Evans@Sun.COM 			continue;
603*11827SRod.Evans@Sun.COM 
604*11827SRod.Evans@Sun.COM 		/*
605*11827SRod.Evans@Sun.COM 		 * If a matching group is found, then this new group has
606*11827SRod.Evans@Sun.COM 		 * already been supplied by a previous file, and hence the
607*11827SRod.Evans@Sun.COM 		 * existing group can be used.  Record this new input section,
608*11827SRod.Evans@Sun.COM 		 * from which we can also derive the input file name, on the
609*11827SRod.Evans@Sun.COM 		 * existing groups input sections.
610*11827SRod.Evans@Sun.COM 		 */
611*11827SRod.Evans@Sun.COM 		if (aplist_append(&(cgp->cg_secs), isp,
612*11827SRod.Evans@Sun.COM 		    AL_CNT_CAP_SECS) == NULL)
613*11827SRod.Evans@Sun.COM 			return (NULL);
614*11827SRod.Evans@Sun.COM 		return (cgp);
615*11827SRod.Evans@Sun.COM 	}
616*11827SRod.Evans@Sun.COM 
617*11827SRod.Evans@Sun.COM 	/*
618*11827SRod.Evans@Sun.COM 	 * If a capabilities group is not found, create a new one.
619*11827SRod.Evans@Sun.COM 	 */
620*11827SRod.Evans@Sun.COM 	if (((cgp = libld_calloc(sizeof (Cap_group), 1)) == NULL) ||
621*11827SRod.Evans@Sun.COM 	    (aplist_append(&(ofl->ofl_capgroups), cgp,
622*11827SRod.Evans@Sun.COM 	    AL_CNT_CAP_DESCS) == NULL))
623*11827SRod.Evans@Sun.COM 		return (NULL);
624*11827SRod.Evans@Sun.COM 
625*11827SRod.Evans@Sun.COM 	/*
626*11827SRod.Evans@Sun.COM 	 * If we're converting object capabilities to symbol capabilities and
627*11827SRod.Evans@Sun.COM 	 * no CA_SUNW_ID is defined, fabricate one.  This identifier is appended
628*11827SRod.Evans@Sun.COM 	 * to all symbol names that are converted into capabilities symbols,
629*11827SRod.Evans@Sun.COM 	 * see ld_sym_process().
630*11827SRod.Evans@Sun.COM 	 */
631*11827SRod.Evans@Sun.COM 	if ((isp->is_file->ifl_flags & FLG_IF_OTOSCAP) &&
632*11827SRod.Evans@Sun.COM 	    (ocapset->oc_id.cs_str == NULL)) {
633*11827SRod.Evans@Sun.COM 		size_t	len;
634*11827SRod.Evans@Sun.COM 
635*11827SRod.Evans@Sun.COM 		/*
636*11827SRod.Evans@Sun.COM 		 * Create an identifier using the group number together with a
637*11827SRod.Evans@Sun.COM 		 * default template.  We allocate a buffer large enough for any
638*11827SRod.Evans@Sun.COM 		 * possible number of items (way more than we need).
639*11827SRod.Evans@Sun.COM 		 */
640*11827SRod.Evans@Sun.COM 		len = MSG_STR_CAPGROUPID_SIZE + CONV_INV_BUFSIZE;
641*11827SRod.Evans@Sun.COM 		if ((ocapset->oc_id.cs_str = libld_malloc(len)) == NULL)
642*11827SRod.Evans@Sun.COM 			return (NULL);
643*11827SRod.Evans@Sun.COM 
644*11827SRod.Evans@Sun.COM 		(void) snprintf(ocapset->oc_id.cs_str, len,
645*11827SRod.Evans@Sun.COM 		    MSG_ORIG(MSG_STR_CAPGROUPID),
646*11827SRod.Evans@Sun.COM 		    aplist_nitems(ofl->ofl_capgroups));
647*11827SRod.Evans@Sun.COM 		cnum++;
648*11827SRod.Evans@Sun.COM 	}
649*11827SRod.Evans@Sun.COM 
650*11827SRod.Evans@Sun.COM 	cgp->cg_set = *ocapset;
651*11827SRod.Evans@Sun.COM 	cgp->cg_num = cnum;
652*11827SRod.Evans@Sun.COM 
653*11827SRod.Evans@Sun.COM 	/*
654*11827SRod.Evans@Sun.COM 	 * Null the callers alist's as they've effectively been transferred
655*11827SRod.Evans@Sun.COM 	 * to this new Cap_group.
656*11827SRod.Evans@Sun.COM 	 */
657*11827SRod.Evans@Sun.COM 	ocapset->oc_plat.cl_val = ocapset->oc_mach.cl_val = NULL;
658*11827SRod.Evans@Sun.COM 
659*11827SRod.Evans@Sun.COM 	/*
660*11827SRod.Evans@Sun.COM 	 * Keep track of which input section, and hence input file, established
661*11827SRod.Evans@Sun.COM 	 * this group.
662*11827SRod.Evans@Sun.COM 	 */
663*11827SRod.Evans@Sun.COM 	if (aplist_append(&(cgp->cg_secs), isp, AL_CNT_CAP_SECS) == NULL)
664*11827SRod.Evans@Sun.COM 		return (NULL);
665*11827SRod.Evans@Sun.COM 
666*11827SRod.Evans@Sun.COM 	/*
667*11827SRod.Evans@Sun.COM 	 * Keep track of the number of symbol capabilities entries that will be
668*11827SRod.Evans@Sun.COM 	 * required in the output file.  Each group requires a terminating
669*11827SRod.Evans@Sun.COM 	 * CA_SUNW_NULL.
670*11827SRod.Evans@Sun.COM 	 */
671*11827SRod.Evans@Sun.COM 	ofl->ofl_capsymcnt += (cnum + 1);
672*11827SRod.Evans@Sun.COM 	return (cgp);
673*11827SRod.Evans@Sun.COM }
674*11827SRod.Evans@Sun.COM 
675*11827SRod.Evans@Sun.COM /*
676*11827SRod.Evans@Sun.COM  * Capture symbol capability family information.  This data structure is focal
677*11827SRod.Evans@Sun.COM  * in maintaining all symbol capability relationships, and provides for the
678*11827SRod.Evans@Sun.COM  * eventual creation of a capabilities information section, and possibly a
679*11827SRod.Evans@Sun.COM  * capabilities chain section.
680*11827SRod.Evans@Sun.COM  *
681*11827SRod.Evans@Sun.COM  * Capabilities families are lead by a CAPINFO_SUNW_GLOB symbol.  This symbol
682*11827SRod.Evans@Sun.COM  * provides the visible global symbol that is referenced by all external
683*11827SRod.Evans@Sun.COM  * callers.  This symbol may have aliases.  For example, a weak/global symbol
684*11827SRod.Evans@Sun.COM  * pair, such as memcpy()/_memcpy() may lead the same capabilities family.
685*11827SRod.Evans@Sun.COM  * Each family contains one or more local symbol members.  These members provide
686*11827SRod.Evans@Sun.COM  * the capabilities specific functions, and are associated to a capabilities
687*11827SRod.Evans@Sun.COM  * group.  For example, the capability members memcpy%sun4u and memcpy%sun4v
688*11827SRod.Evans@Sun.COM  * might be associated with the memcpy() capability family.
689*11827SRod.Evans@Sun.COM  *
690*11827SRod.Evans@Sun.COM  * This routine is called when a relocatable object that provides object
691*11827SRod.Evans@Sun.COM  * capabilities is transformed into a symbol capabilities object, using the
692*11827SRod.Evans@Sun.COM  * -z symbolcap option.
693*11827SRod.Evans@Sun.COM  *
694*11827SRod.Evans@Sun.COM  * This routine is also called to collect the SUNW_capinfo section information
695*11827SRod.Evans@Sun.COM  * of a relocatable object that contains symbol capability definitions.
696*11827SRod.Evans@Sun.COM  */
697*11827SRod.Evans@Sun.COM uintptr_t
698*11827SRod.Evans@Sun.COM ld_cap_add_family(Ofl_desc *ofl, Sym_desc *lsdp, Sym_desc *csdp, Cap_group *cgp,
699*11827SRod.Evans@Sun.COM     APlist **csyms)
700*11827SRod.Evans@Sun.COM {
701*11827SRod.Evans@Sun.COM 	Cap_avlnode	qcav, *cav;
702*11827SRod.Evans@Sun.COM 	avl_tree_t	*avlt;
703*11827SRod.Evans@Sun.COM 	avl_index_t	where = 0;
704*11827SRod.Evans@Sun.COM 	Cap_sym		*mcsp;
705*11827SRod.Evans@Sun.COM 	Aliste		idx;
706*11827SRod.Evans@Sun.COM 
707*11827SRod.Evans@Sun.COM 	/*
708*11827SRod.Evans@Sun.COM 	 * Make sure the capability families have an initialized AVL tree.
709*11827SRod.Evans@Sun.COM 	 */
710*11827SRod.Evans@Sun.COM 	if ((avlt = ofl->ofl_capfamilies) == NULL) {
711*11827SRod.Evans@Sun.COM 		if ((avlt = libld_calloc(sizeof (avl_tree_t), 1)) == NULL)
712*11827SRod.Evans@Sun.COM 			return (S_ERROR);
713*11827SRod.Evans@Sun.COM 		avl_create(avlt, &ld_sym_avl_comp, sizeof (Cap_avlnode),
714*11827SRod.Evans@Sun.COM 		    SGSOFFSETOF(Cap_avlnode, cn_symavlnode.sav_node));
715*11827SRod.Evans@Sun.COM 		ofl->ofl_capfamilies = avlt;
716*11827SRod.Evans@Sun.COM 
717*11827SRod.Evans@Sun.COM 		/*
718*11827SRod.Evans@Sun.COM 		 * When creating a dynamic object, capability family members
719*11827SRod.Evans@Sun.COM 		 * are maintained in a .SUNW_capchain, the first entry of
720*11827SRod.Evans@Sun.COM 		 * which is the version number of the chain.
721*11827SRod.Evans@Sun.COM 		 */
722*11827SRod.Evans@Sun.COM 		ofl->ofl_capchaincnt = 1;
723*11827SRod.Evans@Sun.COM 	}
724*11827SRod.Evans@Sun.COM 
725*11827SRod.Evans@Sun.COM 	/*
726*11827SRod.Evans@Sun.COM 	 * Determine whether a family already exists, and if not, create one
727*11827SRod.Evans@Sun.COM 	 * using the lead family symbol.
728*11827SRod.Evans@Sun.COM 	 */
729*11827SRod.Evans@Sun.COM 	qcav.cn_symavlnode.sav_hash = (Word)elf_hash(lsdp->sd_name);
730*11827SRod.Evans@Sun.COM 	qcav.cn_symavlnode.sav_name = lsdp->sd_name;
731*11827SRod.Evans@Sun.COM 
732*11827SRod.Evans@Sun.COM 	if ((cav = avl_find(avlt, &qcav, &where)) == NULL) {
733*11827SRod.Evans@Sun.COM 		if ((cav = libld_calloc(sizeof (Cap_avlnode), 1)) == NULL)
734*11827SRod.Evans@Sun.COM 			return (S_ERROR);
735*11827SRod.Evans@Sun.COM 		cav->cn_symavlnode.sav_hash = qcav.cn_symavlnode.sav_hash;
736*11827SRod.Evans@Sun.COM 		cav->cn_symavlnode.sav_name = qcav.cn_symavlnode.sav_name;
737*11827SRod.Evans@Sun.COM 		cav->cn_symavlnode.sav_sdp = lsdp;
738*11827SRod.Evans@Sun.COM 
739*11827SRod.Evans@Sun.COM 		avl_insert(avlt, cav, where);
740*11827SRod.Evans@Sun.COM 
741*11827SRod.Evans@Sun.COM 		/*
742*11827SRod.Evans@Sun.COM 		 * When creating a dynamic object, capability family members
743*11827SRod.Evans@Sun.COM 		 * are maintained in a .SUNW_capchain, each family starts with
744*11827SRod.Evans@Sun.COM 		 * this lead symbol, and is terminated with a 0 element.
745*11827SRod.Evans@Sun.COM 		 */
746*11827SRod.Evans@Sun.COM 		ofl->ofl_capchaincnt += 2;
747*11827SRod.Evans@Sun.COM 	}
748*11827SRod.Evans@Sun.COM 
749*11827SRod.Evans@Sun.COM 	/*
750*11827SRod.Evans@Sun.COM 	 * If no group information is provided then this request is to add a
751*11827SRod.Evans@Sun.COM 	 * lead capability symbol, or lead symbol alias.  If this is the lead
752*11827SRod.Evans@Sun.COM 	 * symbol there's nothing more to do.  Otherwise save the alias.
753*11827SRod.Evans@Sun.COM 	 */
754*11827SRod.Evans@Sun.COM 	if (cgp == NULL) {
755*11827SRod.Evans@Sun.COM 		if ((lsdp != csdp) && (aplist_append(&cav->cn_aliases, csdp,
756*11827SRod.Evans@Sun.COM 		    AL_CNT_CAP_ALIASES) == NULL))
757*11827SRod.Evans@Sun.COM 			return (S_ERROR);
758*11827SRod.Evans@Sun.COM 
759*11827SRod.Evans@Sun.COM 		return (0);
760*11827SRod.Evans@Sun.COM 	}
761*11827SRod.Evans@Sun.COM 
762*11827SRod.Evans@Sun.COM 	/*
763*11827SRod.Evans@Sun.COM 	 * Determine whether a member of the same group as this new member is
764*11827SRod.Evans@Sun.COM 	 * already defined within this family.  If so, we have a multiply
765*11827SRod.Evans@Sun.COM 	 * defined symbol.
766*11827SRod.Evans@Sun.COM 	 */
767*11827SRod.Evans@Sun.COM 	for (APLIST_TRAVERSE(cav->cn_members, idx, mcsp)) {
768*11827SRod.Evans@Sun.COM 		Sym_desc	*msdp;
769*11827SRod.Evans@Sun.COM 
770*11827SRod.Evans@Sun.COM 		if (cgp != mcsp->cs_group)
771*11827SRod.Evans@Sun.COM 			continue;
772*11827SRod.Evans@Sun.COM 
773*11827SRod.Evans@Sun.COM 		/*
774*11827SRod.Evans@Sun.COM 		 * Diagnose that a multiple symbol definition exists.
775*11827SRod.Evans@Sun.COM 		 */
776*11827SRod.Evans@Sun.COM 		msdp = mcsp->cs_sdp;
777*11827SRod.Evans@Sun.COM 
778*11827SRod.Evans@Sun.COM 		eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_CAP_MULDEF),
779*11827SRod.Evans@Sun.COM 		    demangle(lsdp->sd_name));
780*11827SRod.Evans@Sun.COM 		eprintf(ofl->ofl_lml, ERR_NONE, MSG_INTL(MSG_CAP_MULDEFSYMS),
781*11827SRod.Evans@Sun.COM 		    msdp->sd_file->ifl_name, msdp->sd_name,
782*11827SRod.Evans@Sun.COM 		    csdp->sd_file->ifl_name, csdp->sd_name);
783*11827SRod.Evans@Sun.COM 		ofl->ofl_flags |= FLG_OF_FATAL;
784*11827SRod.Evans@Sun.COM 	}
785*11827SRod.Evans@Sun.COM 
786*11827SRod.Evans@Sun.COM 	/*
787*11827SRod.Evans@Sun.COM 	 * Add this capabilities symbol member to the family.
788*11827SRod.Evans@Sun.COM 	 */
789*11827SRod.Evans@Sun.COM 	if (((mcsp = libld_malloc(sizeof (Cap_sym))) == NULL) ||
790*11827SRod.Evans@Sun.COM 	    (aplist_append(&cav->cn_members, mcsp, AL_CNT_CAP_MEMS) == NULL))
791*11827SRod.Evans@Sun.COM 		return (S_ERROR);
792*11827SRod.Evans@Sun.COM 
793*11827SRod.Evans@Sun.COM 	mcsp->cs_sdp = csdp;
794*11827SRod.Evans@Sun.COM 	mcsp->cs_group = cgp;
795*11827SRod.Evans@Sun.COM 
796*11827SRod.Evans@Sun.COM 	/*
797*11827SRod.Evans@Sun.COM 	 * When creating a dynamic object, capability family members are
798*11827SRod.Evans@Sun.COM 	 * maintained in a .SUNW_capchain.  Account for this family member.
799*11827SRod.Evans@Sun.COM 	 */
800*11827SRod.Evans@Sun.COM 	ofl->ofl_capchaincnt++;
801*11827SRod.Evans@Sun.COM 
802*11827SRod.Evans@Sun.COM 	/*
803*11827SRod.Evans@Sun.COM 	 * If this input file is undergoing object capabilities to symbol
804*11827SRod.Evans@Sun.COM 	 * capabilities conversion, then this member is a new local symbol
805*11827SRod.Evans@Sun.COM 	 * that has been generated from an original global symbol.  Keep track
806*11827SRod.Evans@Sun.COM 	 * of this symbol so that the output file symbol table can be populated
807*11827SRod.Evans@Sun.COM 	 * with these new symbol entries.
808*11827SRod.Evans@Sun.COM 	 */
809*11827SRod.Evans@Sun.COM 	if (csyms && (aplist_append(csyms, mcsp, AL_CNT_CAP_SYMS) == NULL))
810*11827SRod.Evans@Sun.COM 		return (S_ERROR);
811*11827SRod.Evans@Sun.COM 
812*11827SRod.Evans@Sun.COM 	return (0);
813*11827SRod.Evans@Sun.COM }
814*11827SRod.Evans@Sun.COM 
815*11827SRod.Evans@Sun.COM /*
816*11827SRod.Evans@Sun.COM  * Process a SHT_SUNW_cap capabilities section.
817*11827SRod.Evans@Sun.COM  */
818*11827SRod.Evans@Sun.COM static uintptr_t
819*11827SRod.Evans@Sun.COM process_cap(Ofl_desc *ofl, Ifl_desc *ifl, Is_desc *cisp)
820*11827SRod.Evans@Sun.COM {
821*11827SRod.Evans@Sun.COM 	Objcapset	ocapset = { 0 };
822*11827SRod.Evans@Sun.COM 	Cap_desc	*cdp;
823*11827SRod.Evans@Sun.COM 	Cap		*data, *cdata;
824*11827SRod.Evans@Sun.COM 	char		*strs;
825*11827SRod.Evans@Sun.COM 	Word		ndx, cnum;
826*11827SRod.Evans@Sun.COM 	int		objcapndx, descapndx, symcapndx;
827*11827SRod.Evans@Sun.COM 	int		nulls, capstrs = 0;
828*11827SRod.Evans@Sun.COM 
829*11827SRod.Evans@Sun.COM 	/*
830*11827SRod.Evans@Sun.COM 	 * Determine the capabilities data and size.
831*11827SRod.Evans@Sun.COM 	 */
832*11827SRod.Evans@Sun.COM 	cdata = (Cap *)cisp->is_indata->d_buf;
833*11827SRod.Evans@Sun.COM 	cnum = (Word)(cisp->is_shdr->sh_size / cisp->is_shdr->sh_entsize);
834*11827SRod.Evans@Sun.COM 
835*11827SRod.Evans@Sun.COM 	if ((cdata == NULL) || (cnum == 0))
836*11827SRod.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 	/*
841*11827SRod.Evans@Sun.COM 	 * Traverse the section to determine what capabilities groups are
842*11827SRod.Evans@Sun.COM 	 * available.
843*11827SRod.Evans@Sun.COM 	 *
844*11827SRod.Evans@Sun.COM 	 * A capabilities section can contain one or more, CA_SUNW_NULL
845*11827SRod.Evans@Sun.COM 	 * terminated groups.
846*11827SRod.Evans@Sun.COM 	 *
847*11827SRod.Evans@Sun.COM 	 *  -	The first group defines the object capabilities.
848*11827SRod.Evans@Sun.COM 	 *  -	Additional groups define symbol capabilities.
849*11827SRod.Evans@Sun.COM 	 *  -	Since the initial group is always reserved for object
850*11827SRod.Evans@Sun.COM 	 *	capabilities, any object with symbol capabilities must also
851*11827SRod.Evans@Sun.COM 	 *	have an object capabilities group.  If the object has no object
852*11827SRod.Evans@Sun.COM 	 *	capabilities, an empty object group is defined, consisting of a
853*11827SRod.Evans@Sun.COM 	 *	CA_SUNW_NULL element in index [0].
854*11827SRod.Evans@Sun.COM 	 *  -	If any capabilities require references to a named string, then
855*11827SRod.Evans@Sun.COM 	 *	the section header sh_info points to the associated string
856*11827SRod.Evans@Sun.COM 	 *	table.
857*11827SRod.Evans@Sun.COM 	 *  -	If an object contains symbol capability groups, then the
858*11827SRod.Evans@Sun.COM 	 *	section header sh_link points to the associated capinfo table.
8593617Srie 	 */
860*11827SRod.Evans@Sun.COM 	objcapndx = 0;
861*11827SRod.Evans@Sun.COM 	descapndx = symcapndx = -1;
862*11827SRod.Evans@Sun.COM 	nulls = 0;
863*11827SRod.Evans@Sun.COM 
864*11827SRod.Evans@Sun.COM 	for (ndx = 0, data = cdata; ndx < cnum; ndx++, data++) {
865*11827SRod.Evans@Sun.COM 		switch (data->c_tag) {
866*11827SRod.Evans@Sun.COM 		case CA_SUNW_NULL:
867*11827SRod.Evans@Sun.COM 			/*
868*11827SRod.Evans@Sun.COM 			 * If this is the first CA_SUNW_NULL entry, and no
869*11827SRod.Evans@Sun.COM 			 * capabilities group has been found, then this object
870*11827SRod.Evans@Sun.COM 			 * does not define any object capabilities.
871*11827SRod.Evans@Sun.COM 			 */
872*11827SRod.Evans@Sun.COM 			if (nulls++ == 0) {
873*11827SRod.Evans@Sun.COM 				if (ndx == 0)
874*11827SRod.Evans@Sun.COM 					objcapndx = -1;
875*11827SRod.Evans@Sun.COM 			} else if ((symcapndx == -1) && (descapndx != -1))
876*11827SRod.Evans@Sun.COM 				symcapndx = descapndx;
877*11827SRod.Evans@Sun.COM 
878*11827SRod.Evans@Sun.COM 			break;
879*11827SRod.Evans@Sun.COM 
880*11827SRod.Evans@Sun.COM 		case CA_SUNW_PLAT:
881*11827SRod.Evans@Sun.COM 		case CA_SUNW_MACH:
882*11827SRod.Evans@Sun.COM 		case CA_SUNW_ID:
883*11827SRod.Evans@Sun.COM 			capstrs++;
884*11827SRod.Evans@Sun.COM 			/* FALLTHROUGH */
885*11827SRod.Evans@Sun.COM 
886*11827SRod.Evans@Sun.COM 		case CA_SUNW_HW_1:
887*11827SRod.Evans@Sun.COM 		case CA_SUNW_SF_1:
888*11827SRod.Evans@Sun.COM 		case CA_SUNW_HW_2:
889*11827SRod.Evans@Sun.COM 			/*
890*11827SRod.Evans@Sun.COM 			 * If this is the start of a new group, save it.
891*11827SRod.Evans@Sun.COM 			 */
892*11827SRod.Evans@Sun.COM 			if (descapndx == -1)
893*11827SRod.Evans@Sun.COM 				descapndx = ndx;
894*11827SRod.Evans@Sun.COM 			break;
895*11827SRod.Evans@Sun.COM 
896*11827SRod.Evans@Sun.COM 		default:
897*11827SRod.Evans@Sun.COM 			eprintf(ofl->ofl_lml, ERR_WARNING,
898*11827SRod.Evans@Sun.COM 			    MSG_INTL(MSG_FIL_UNKCAP), ifl->ifl_name,
899*11827SRod.Evans@Sun.COM 			    EC_WORD(cisp->is_scnndx), cisp->is_name,
900*11827SRod.Evans@Sun.COM 			    data->c_tag);
9010Sstevel@tonic-gate 		}
9020Sstevel@tonic-gate 	}
903*11827SRod.Evans@Sun.COM 
904*11827SRod.Evans@Sun.COM 	/*
905*11827SRod.Evans@Sun.COM 	 * If a string capabilities entry has been found, the capabilities
906*11827SRod.Evans@Sun.COM 	 * section must reference the associated string table.
907*11827SRod.Evans@Sun.COM 	 */
908*11827SRod.Evans@Sun.COM 	if (capstrs) {
909*11827SRod.Evans@Sun.COM 		Word	info = cisp->is_shdr->sh_info;
910*11827SRod.Evans@Sun.COM 
911*11827SRod.Evans@Sun.COM 		if ((info == 0) || (info > ifl->ifl_shnum)) {
912*11827SRod.Evans@Sun.COM 			eprintf(ofl->ofl_lml, ERR_FATAL,
913*11827SRod.Evans@Sun.COM 			    MSG_INTL(MSG_FIL_INVSHINFO), ifl->ifl_name,
914*11827SRod.Evans@Sun.COM 			    EC_WORD(cisp->is_scnndx), cisp->is_name,
915*11827SRod.Evans@Sun.COM 			    EC_XWORD(info));
916*11827SRod.Evans@Sun.COM 			ofl->ofl_flags |= FLG_OF_FATAL;
917*11827SRod.Evans@Sun.COM 			return (S_ERROR);
918*11827SRod.Evans@Sun.COM 		}
919*11827SRod.Evans@Sun.COM 		strs = (char *)ifl->ifl_isdesc[info]->is_indata->d_buf;
920*11827SRod.Evans@Sun.COM 	}
921*11827SRod.Evans@Sun.COM 
922*11827SRod.Evans@Sun.COM 	/*
923*11827SRod.Evans@Sun.COM 	 * The processing of capabilities groups is as follows:
924*11827SRod.Evans@Sun.COM 	 *
925*11827SRod.Evans@Sun.COM 	 *  -	if a relocatable object provides only object capabilities, and
926*11827SRod.Evans@Sun.COM 	 *	the -z symbolcap option is in effect, then the object
927*11827SRod.Evans@Sun.COM 	 *	capabilities are transformed into symbol capabilities and the
928*11827SRod.Evans@Sun.COM 	 *	symbol capabilities are carried over to the output file.
929*11827SRod.Evans@Sun.COM 	 *  -	in all other cases, any capabilities present in an input
930*11827SRod.Evans@Sun.COM 	 *	relocatable object are carried from the input object to the
931*11827SRod.Evans@Sun.COM 	 *	output without any transformation or conversion.
932*11827SRod.Evans@Sun.COM 	 *
933*11827SRod.Evans@Sun.COM 	 * Capture any object capabilities that are to be carried over to the
934*11827SRod.Evans@Sun.COM 	 * output file.
935*11827SRod.Evans@Sun.COM 	 */
936*11827SRod.Evans@Sun.COM 	if ((objcapndx == 0) &&
937*11827SRod.Evans@Sun.COM 	    ((symcapndx != -1) || ((ofl->ofl_flags & FLG_OF_OTOSCAP) == 0))) {
938*11827SRod.Evans@Sun.COM 		for (ndx = 0, data = cdata; ndx < cnum; ndx++, data++) {
939*11827SRod.Evans@Sun.COM 			/*
940*11827SRod.Evans@Sun.COM 			 * Object capabilities end at the first null.
941*11827SRod.Evans@Sun.COM 			 */
942*11827SRod.Evans@Sun.COM 			if (data->c_tag == CA_SUNW_NULL)
943*11827SRod.Evans@Sun.COM 				break;
944*11827SRod.Evans@Sun.COM 
945*11827SRod.Evans@Sun.COM 			/*
946*11827SRod.Evans@Sun.COM 			 * Only the object software capabilities that are
947*11827SRod.Evans@Sun.COM 			 * defined in a relocatable object become part of the
948*11827SRod.Evans@Sun.COM 			 * object software capabilities in the output file.
949*11827SRod.Evans@Sun.COM 			 * However, check the validity of any object software
950*11827SRod.Evans@Sun.COM 			 * capabilities of any dependencies.
951*11827SRod.Evans@Sun.COM 			 */
952*11827SRod.Evans@Sun.COM 			if (data->c_tag == CA_SUNW_SF_1) {
953*11827SRod.Evans@Sun.COM 				sf1_cap(ofl, data->c_un.c_val, ifl, cisp);
954*11827SRod.Evans@Sun.COM 				continue;
955*11827SRod.Evans@Sun.COM 			}
956*11827SRod.Evans@Sun.COM 
957*11827SRod.Evans@Sun.COM 			/*
958*11827SRod.Evans@Sun.COM 			 * The remaining capability types must come from a
959*11827SRod.Evans@Sun.COM 			 * relocatable object in order to contribute to the
960*11827SRod.Evans@Sun.COM 			 * output.
961*11827SRod.Evans@Sun.COM 			 */
962*11827SRod.Evans@Sun.COM 			if (ifl->ifl_ehdr->e_type != ET_REL)
963*11827SRod.Evans@Sun.COM 				continue;
964*11827SRod.Evans@Sun.COM 
965*11827SRod.Evans@Sun.COM 			switch (data->c_tag) {
966*11827SRod.Evans@Sun.COM 			case CA_SUNW_HW_1:
967*11827SRod.Evans@Sun.COM 			case CA_SUNW_HW_2:
968*11827SRod.Evans@Sun.COM 				hw_cap(ofl, data->c_tag, data->c_un.c_val);
969*11827SRod.Evans@Sun.COM 				break;
970*11827SRod.Evans@Sun.COM 
971*11827SRod.Evans@Sun.COM 			case CA_SUNW_PLAT:
972*11827SRod.Evans@Sun.COM 				str_cap(ofl, strs + data->c_un.c_ptr,
973*11827SRod.Evans@Sun.COM 				    FLG_OF1_OVPLATCAP, CA_SUNW_PLAT,
974*11827SRod.Evans@Sun.COM 				    &ofl->ofl_ocapset.oc_plat);
975*11827SRod.Evans@Sun.COM 				break;
976*11827SRod.Evans@Sun.COM 
977*11827SRod.Evans@Sun.COM 			case CA_SUNW_MACH:
978*11827SRod.Evans@Sun.COM 				str_cap(ofl, strs + data->c_un.c_ptr,
979*11827SRod.Evans@Sun.COM 				    FLG_OF1_OVMACHCAP, CA_SUNW_MACH,
980*11827SRod.Evans@Sun.COM 				    &ofl->ofl_ocapset.oc_mach);
981*11827SRod.Evans@Sun.COM 				break;
982*11827SRod.Evans@Sun.COM 
983*11827SRod.Evans@Sun.COM 			case CA_SUNW_ID:
984*11827SRod.Evans@Sun.COM 				id_cap(ofl, strs + data->c_un.c_ptr,
985*11827SRod.Evans@Sun.COM 				    FLG_OCS_USRDEFID);
986*11827SRod.Evans@Sun.COM 				break;
987*11827SRod.Evans@Sun.COM 
988*11827SRod.Evans@Sun.COM 			default:
989*11827SRod.Evans@Sun.COM 				assert(0);	/* Unknown capability type */
990*11827SRod.Evans@Sun.COM 			}
991*11827SRod.Evans@Sun.COM 		}
992*11827SRod.Evans@Sun.COM 
993*11827SRod.Evans@Sun.COM 		/*
994*11827SRod.Evans@Sun.COM 		 * If there are no symbol capabilities, or this objects
995*11827SRod.Evans@Sun.COM 		 * capabilities aren't being transformed into a symbol
996*11827SRod.Evans@Sun.COM 		 * capabilities, then we're done.
997*11827SRod.Evans@Sun.COM 		 */
998*11827SRod.Evans@Sun.COM 		if ((symcapndx == -1) &&
999*11827SRod.Evans@Sun.COM 		    ((ofl->ofl_flags & FLG_OF_OTOSCAP) == 0))
1000*11827SRod.Evans@Sun.COM 			return (1);
1001*11827SRod.Evans@Sun.COM 	}
1002*11827SRod.Evans@Sun.COM 
1003*11827SRod.Evans@Sun.COM 	/*
1004*11827SRod.Evans@Sun.COM 	 * If these capabilities don't originate from a relocatable object
1005*11827SRod.Evans@Sun.COM 	 * there's no further processing required.
1006*11827SRod.Evans@Sun.COM 	 */
1007*11827SRod.Evans@Sun.COM 	if (ifl->ifl_ehdr->e_type != ET_REL)
1008*11827SRod.Evans@Sun.COM 		return (1);
1009*11827SRod.Evans@Sun.COM 
1010*11827SRod.Evans@Sun.COM 	/*
1011*11827SRod.Evans@Sun.COM 	 * If this object only defines an object capabilities group, and the
1012*11827SRod.Evans@Sun.COM 	 * -z symbolcap option is in effect, then all global function symbols
1013*11827SRod.Evans@Sun.COM 	 * and initialized global data symbols are renamed and assigned to the
1014*11827SRod.Evans@Sun.COM 	 * transformed symbol capabilities group.
1015*11827SRod.Evans@Sun.COM 	 */
1016*11827SRod.Evans@Sun.COM 	if ((objcapndx == 0) &&
1017*11827SRod.Evans@Sun.COM 	    (symcapndx == -1) && (ofl->ofl_flags & FLG_OF_OTOSCAP))
1018*11827SRod.Evans@Sun.COM 		ifl->ifl_flags |= FLG_IF_OTOSCAP;
1019*11827SRod.Evans@Sun.COM 
1020*11827SRod.Evans@Sun.COM 	/*
1021*11827SRod.Evans@Sun.COM 	 * Allocate a capabilities descriptor to collect the capabilities data
1022*11827SRod.Evans@Sun.COM 	 * for this input file.  Allocate a mirror of the raw capabilities data
1023*11827SRod.Evans@Sun.COM 	 * that points to the individual symbol capabilities groups.  An APlist
1024*11827SRod.Evans@Sun.COM 	 * is used, although it will be sparsely populated, as the list provides
1025*11827SRod.Evans@Sun.COM 	 * a convenient mechanism for traversal later.
1026*11827SRod.Evans@Sun.COM 	 */
1027*11827SRod.Evans@Sun.COM 	if (((cdp = libld_calloc(sizeof (Cap_desc), 1)) == NULL) ||
1028*11827SRod.Evans@Sun.COM 	    (aplist_append(&(cdp->ca_groups), NULL, cnum) == NULL))
1029*11827SRod.Evans@Sun.COM 		return (S_ERROR);
1030*11827SRod.Evans@Sun.COM 
1031*11827SRod.Evans@Sun.COM 	/*
1032*11827SRod.Evans@Sun.COM 	 * Clear the allocated APlist data array, and assign the number of
1033*11827SRod.Evans@Sun.COM 	 * items as the total number of array items.
1034*11827SRod.Evans@Sun.COM 	 */
1035*11827SRod.Evans@Sun.COM 	(void) memset(&cdp->ca_groups->apl_data[0], 0,
1036*11827SRod.Evans@Sun.COM 	    (cnum * sizeof (void *)));
1037*11827SRod.Evans@Sun.COM 	cdp->ca_groups->apl_nitems = cnum;
1038*11827SRod.Evans@Sun.COM 
1039*11827SRod.Evans@Sun.COM 	ifl->ifl_caps = cdp;
1040*11827SRod.Evans@Sun.COM 
1041*11827SRod.Evans@Sun.COM 	/*
1042*11827SRod.Evans@Sun.COM 	 * Traverse the capabilities data, unpacking the data into a
1043*11827SRod.Evans@Sun.COM 	 * capabilities set.  Process each capabilities set as a unique group.
1044*11827SRod.Evans@Sun.COM 	 */
1045*11827SRod.Evans@Sun.COM 	descapndx = -1;
1046*11827SRod.Evans@Sun.COM 	nulls = 0;
1047*11827SRod.Evans@Sun.COM 
1048*11827SRod.Evans@Sun.COM 	for (ndx = 0, data = cdata; ndx < cnum; ndx++, data++) {
1049*11827SRod.Evans@Sun.COM 		Capstr	*capstr;
1050*11827SRod.Evans@Sun.COM 
1051*11827SRod.Evans@Sun.COM 		switch (data->c_tag) {
1052*11827SRod.Evans@Sun.COM 		case CA_SUNW_NULL:
1053*11827SRod.Evans@Sun.COM 			nulls++;
1054*11827SRod.Evans@Sun.COM 
1055*11827SRod.Evans@Sun.COM 			/*
1056*11827SRod.Evans@Sun.COM 			 * Process the capabilities group that this null entry
1057*11827SRod.Evans@Sun.COM 			 * terminates.  The capabilities group that is returned
1058*11827SRod.Evans@Sun.COM 			 * will either point to this file's data, or to a
1059*11827SRod.Evans@Sun.COM 			 * matching capabilities group that has already been
1060*11827SRod.Evans@Sun.COM 			 * processed.
1061*11827SRod.Evans@Sun.COM 			 *
1062*11827SRod.Evans@Sun.COM 			 * Note, if this object defines object capabilities,
1063*11827SRod.Evans@Sun.COM 			 * the first group descriptor points to these object
1064*11827SRod.Evans@Sun.COM 			 * capabilities.  It is only necessary to save this
1065*11827SRod.Evans@Sun.COM 			 * descriptor when object capabilities are being
1066*11827SRod.Evans@Sun.COM 			 * transformed into symbol capabilities (-z symbolcap).
1067*11827SRod.Evans@Sun.COM 			 */
1068*11827SRod.Evans@Sun.COM 			if (descapndx != -1) {
1069*11827SRod.Evans@Sun.COM 				if ((nulls > 1) ||
1070*11827SRod.Evans@Sun.COM 				    (ifl->ifl_flags & FLG_IF_OTOSCAP)) {
1071*11827SRod.Evans@Sun.COM 					APlist	*alp = cdp->ca_groups;
1072*11827SRod.Evans@Sun.COM 
1073*11827SRod.Evans@Sun.COM 					if ((alp->apl_data[descapndx] =
1074*11827SRod.Evans@Sun.COM 					    get_cap_group(&ocapset,
1075*11827SRod.Evans@Sun.COM 					    (ndx - descapndx), ofl,
1076*11827SRod.Evans@Sun.COM 					    cisp)) == NULL)
1077*11827SRod.Evans@Sun.COM 						return (S_ERROR);
1078*11827SRod.Evans@Sun.COM 				}
1079*11827SRod.Evans@Sun.COM 
1080*11827SRod.Evans@Sun.COM 				/*
1081*11827SRod.Evans@Sun.COM 				 * Clean up the capabilities data in preparation
1082*11827SRod.Evans@Sun.COM 				 * for processing additional groups.  If the
1083*11827SRod.Evans@Sun.COM 				 * collected capabilities strings were used to
1084*11827SRod.Evans@Sun.COM 				 * establish a new output group, they will have
1085*11827SRod.Evans@Sun.COM 				 * been saved in get_cap_group().  If these
1086*11827SRod.Evans@Sun.COM 				 * descriptors still exist, then an existing
1087*11827SRod.Evans@Sun.COM 				 * descriptor has been used to associate with
1088*11827SRod.Evans@Sun.COM 				 * this file, and these string descriptors can
1089*11827SRod.Evans@Sun.COM 				 * be freed.
1090*11827SRod.Evans@Sun.COM 				 */
1091*11827SRod.Evans@Sun.COM 				ocapset.oc_hw_1.cm_val =
1092*11827SRod.Evans@Sun.COM 				    ocapset.oc_sf_1.cm_val =
1093*11827SRod.Evans@Sun.COM 				    ocapset.oc_hw_2.cm_val = 0;
1094*11827SRod.Evans@Sun.COM 				if (ocapset.oc_plat.cl_val) {
1095*11827SRod.Evans@Sun.COM 					free((void *)ocapset.oc_plat.cl_val);
1096*11827SRod.Evans@Sun.COM 					ocapset.oc_plat.cl_val = NULL;
1097*11827SRod.Evans@Sun.COM 				}
1098*11827SRod.Evans@Sun.COM 				if (ocapset.oc_mach.cl_val) {
1099*11827SRod.Evans@Sun.COM 					free((void *)ocapset.oc_mach.cl_val);
1100*11827SRod.Evans@Sun.COM 					ocapset.oc_mach.cl_val = NULL;
1101*11827SRod.Evans@Sun.COM 				}
1102*11827SRod.Evans@Sun.COM 				descapndx = -1;
1103*11827SRod.Evans@Sun.COM 			}
1104*11827SRod.Evans@Sun.COM 			continue;
1105*11827SRod.Evans@Sun.COM 
1106*11827SRod.Evans@Sun.COM 		case CA_SUNW_HW_1:
1107*11827SRod.Evans@Sun.COM 			ocapset.oc_hw_1.cm_val = data->c_un.c_val;
1108*11827SRod.Evans@Sun.COM 			DBG_CALL(Dbg_cap_val_entry(ofl->ofl_lml,
1109*11827SRod.Evans@Sun.COM 			    DBG_STATE_ORIGINAL, CA_SUNW_HW_1,
1110*11827SRod.Evans@Sun.COM 			    ocapset.oc_hw_1.cm_val, ld_targ.t_m.m_mach));
1111*11827SRod.Evans@Sun.COM 			break;
1112*11827SRod.Evans@Sun.COM 
1113*11827SRod.Evans@Sun.COM 		case CA_SUNW_SF_1:
1114*11827SRod.Evans@Sun.COM 			ocapset.oc_sf_1.cm_val = data->c_un.c_val;
1115*11827SRod.Evans@Sun.COM 			DBG_CALL(Dbg_cap_val_entry(ofl->ofl_lml,
1116*11827SRod.Evans@Sun.COM 			    DBG_STATE_ORIGINAL, CA_SUNW_SF_1,
1117*11827SRod.Evans@Sun.COM 			    ocapset.oc_sf_1.cm_val, ld_targ.t_m.m_mach));
1118*11827SRod.Evans@Sun.COM 			break;
1119*11827SRod.Evans@Sun.COM 
1120*11827SRod.Evans@Sun.COM 		case CA_SUNW_HW_2:
1121*11827SRod.Evans@Sun.COM 			ocapset.oc_hw_2.cm_val = data->c_un.c_val;
1122*11827SRod.Evans@Sun.COM 			DBG_CALL(Dbg_cap_val_entry(ofl->ofl_lml,
1123*11827SRod.Evans@Sun.COM 			    DBG_STATE_ORIGINAL, CA_SUNW_HW_2,
1124*11827SRod.Evans@Sun.COM 			    ocapset.oc_hw_2.cm_val, ld_targ.t_m.m_mach));
1125*11827SRod.Evans@Sun.COM 			break;
1126*11827SRod.Evans@Sun.COM 
1127*11827SRod.Evans@Sun.COM 		case CA_SUNW_PLAT:
1128*11827SRod.Evans@Sun.COM 			if ((capstr = alist_append(&ocapset.oc_plat.cl_val,
1129*11827SRod.Evans@Sun.COM 			    NULL, sizeof (Capstr), AL_CNT_CAP_NAMES)) == NULL)
1130*11827SRod.Evans@Sun.COM 				return (S_ERROR);
1131*11827SRod.Evans@Sun.COM 			capstr->cs_str = strs + data->c_un.c_ptr;
1132*11827SRod.Evans@Sun.COM 			DBG_CALL(Dbg_cap_ptr_entry(ofl->ofl_lml,
1133*11827SRod.Evans@Sun.COM 			    DBG_STATE_ORIGINAL, CA_SUNW_PLAT, capstr->cs_str));
1134*11827SRod.Evans@Sun.COM 			break;
1135*11827SRod.Evans@Sun.COM 
1136*11827SRod.Evans@Sun.COM 		case CA_SUNW_MACH:
1137*11827SRod.Evans@Sun.COM 			if ((capstr = alist_append(&ocapset.oc_mach.cl_val,
1138*11827SRod.Evans@Sun.COM 			    NULL, sizeof (Capstr), AL_CNT_CAP_NAMES)) == NULL)
1139*11827SRod.Evans@Sun.COM 				return (S_ERROR);
1140*11827SRod.Evans@Sun.COM 			capstr->cs_str = strs + data->c_un.c_ptr;
1141*11827SRod.Evans@Sun.COM 			DBG_CALL(Dbg_cap_ptr_entry(ofl->ofl_lml,
1142*11827SRod.Evans@Sun.COM 			    DBG_STATE_ORIGINAL, CA_SUNW_MACH, capstr->cs_str));
1143*11827SRod.Evans@Sun.COM 			break;
1144*11827SRod.Evans@Sun.COM 
1145*11827SRod.Evans@Sun.COM 		case CA_SUNW_ID:
1146*11827SRod.Evans@Sun.COM 			ocapset.oc_id.cs_str = strs + data->c_un.c_ptr;
1147*11827SRod.Evans@Sun.COM 			DBG_CALL(Dbg_cap_ptr_entry(ofl->ofl_lml,
1148*11827SRod.Evans@Sun.COM 			    DBG_STATE_ORIGINAL, CA_SUNW_ID,
1149*11827SRod.Evans@Sun.COM 			    ocapset.oc_id.cs_str));
1150*11827SRod.Evans@Sun.COM 			break;
1151*11827SRod.Evans@Sun.COM 		}
1152*11827SRod.Evans@Sun.COM 
1153*11827SRod.Evans@Sun.COM 		/*
1154*11827SRod.Evans@Sun.COM 		 * Save the start of this new group.
1155*11827SRod.Evans@Sun.COM 		 */
1156*11827SRod.Evans@Sun.COM 		if (descapndx == -1)
1157*11827SRod.Evans@Sun.COM 			descapndx = ndx;
1158*11827SRod.Evans@Sun.COM 	}
1159*11827SRod.Evans@Sun.COM 	return (1);
1160*11827SRod.Evans@Sun.COM }
1161*11827SRod.Evans@Sun.COM 
1162*11827SRod.Evans@Sun.COM /*
1163*11827SRod.Evans@Sun.COM  * Capture any symbol capabilities symbols.  An object file that contains symbol
1164*11827SRod.Evans@Sun.COM  * capabilities has an associated .SUNW_capinfo section.  This section
1165*11827SRod.Evans@Sun.COM  * identifies which symbols are associated to which capabilities, together with
1166*11827SRod.Evans@Sun.COM  * their associated lead symbol.  Each of these symbol pairs are recorded for
1167*11827SRod.Evans@Sun.COM  * processing later.
1168*11827SRod.Evans@Sun.COM  */
1169*11827SRod.Evans@Sun.COM static uintptr_t
1170*11827SRod.Evans@Sun.COM process_capinfo(Ofl_desc *ofl, Ifl_desc *ifl, Is_desc *isp)
1171*11827SRod.Evans@Sun.COM {
1172*11827SRod.Evans@Sun.COM 	Cap_desc	*cdp = ifl->ifl_caps;
1173*11827SRod.Evans@Sun.COM 	Capinfo		*capinfo = isp->is_indata->d_buf;
1174*11827SRod.Evans@Sun.COM 	Shdr		*shdr = isp->is_shdr;
1175*11827SRod.Evans@Sun.COM 	Word		cndx, capinfonum;
1176*11827SRod.Evans@Sun.COM 
1177*11827SRod.Evans@Sun.COM 	capinfonum = (Word)(shdr->sh_size / shdr->sh_entsize);
1178*11827SRod.Evans@Sun.COM 
1179*11827SRod.Evans@Sun.COM 	if ((cdp == NULL) || (capinfo == NULL) || (capinfonum == 0))
1180*11827SRod.Evans@Sun.COM 		return (0);
1181*11827SRod.Evans@Sun.COM 
1182*11827SRod.Evans@Sun.COM 	for (cndx = 1, capinfo++; cndx < capinfonum; cndx++, capinfo++) {
1183*11827SRod.Evans@Sun.COM 		Sym_desc	*sdp, *lsdp;
1184*11827SRod.Evans@Sun.COM 		Word		lndx;
1185*11827SRod.Evans@Sun.COM 		uchar_t		gndx;
1186*11827SRod.Evans@Sun.COM 
1187*11827SRod.Evans@Sun.COM 		if ((gndx = (uchar_t)ELF_C_GROUP(*capinfo)) == 0)
1188*11827SRod.Evans@Sun.COM 			continue;
1189*11827SRod.Evans@Sun.COM 		lndx = (Word)ELF_C_SYM(*capinfo);
1190*11827SRod.Evans@Sun.COM 
1191*11827SRod.Evans@Sun.COM 		/*
1192*11827SRod.Evans@Sun.COM 		 * Catch any anomalies.  A capabilities symbol should be valid,
1193*11827SRod.Evans@Sun.COM 		 * and the capabilities lead symbol should also be global.
1194*11827SRod.Evans@Sun.COM 		 * Note, ld(1) -z symbolcap would create local capabilities
1195*11827SRod.Evans@Sun.COM 		 * symbols, but we don't enforce this so as to give the
1196*11827SRod.Evans@Sun.COM 		 * compilation environment a little more freedom.
1197*11827SRod.Evans@Sun.COM 		 */
1198*11827SRod.Evans@Sun.COM 		if ((sdp = ifl->ifl_oldndx[cndx]) == NULL) {
1199*11827SRod.Evans@Sun.COM 			eprintf(ofl->ofl_lml, ERR_WARNING,
1200*11827SRod.Evans@Sun.COM 			    MSG_INTL(MSG_CAPINFO_INVALSYM), ifl->ifl_name,
1201*11827SRod.Evans@Sun.COM 			    EC_WORD(isp->is_scnndx), isp->is_name, cndx,
1202*11827SRod.Evans@Sun.COM 			    MSG_INTL(MSG_STR_UNKNOWN));
1203*11827SRod.Evans@Sun.COM 			continue;
1204*11827SRod.Evans@Sun.COM 		}
1205*11827SRod.Evans@Sun.COM 		if ((lndx == 0) || (lndx >= ifl->ifl_symscnt) ||
1206*11827SRod.Evans@Sun.COM 		    ((lsdp = ifl->ifl_oldndx[lndx]) == NULL) ||
1207*11827SRod.Evans@Sun.COM 		    (ELF_ST_BIND(lsdp->sd_sym->st_info) != STB_GLOBAL)) {
1208*11827SRod.Evans@Sun.COM 			eprintf(ofl->ofl_lml, ERR_WARNING,
1209*11827SRod.Evans@Sun.COM 			    MSG_INTL(MSG_CAPINFO_INVALLEAD), ifl->ifl_name,
1210*11827SRod.Evans@Sun.COM 			    EC_WORD(isp->is_scnndx), isp->is_name, cndx, lsdp ?
1211*11827SRod.Evans@Sun.COM 			    demangle(lsdp->sd_name) : MSG_INTL(MSG_STR_UNKNOWN),
1212*11827SRod.Evans@Sun.COM 			    lndx);
1213*11827SRod.Evans@Sun.COM 			continue;
1214*11827SRod.Evans@Sun.COM 		}
1215*11827SRod.Evans@Sun.COM 
1216*11827SRod.Evans@Sun.COM 		/*
1217*11827SRod.Evans@Sun.COM 		 * Indicate that this is a capabilities symbol.
1218*11827SRod.Evans@Sun.COM 		 */
1219*11827SRod.Evans@Sun.COM 		sdp->sd_flags |= FLG_SY_CAP;
1220*11827SRod.Evans@Sun.COM 
1221*11827SRod.Evans@Sun.COM 		/*
1222*11827SRod.Evans@Sun.COM 		 * Save any global capability symbols.  Global capability
1223*11827SRod.Evans@Sun.COM 		 * symbols are identified with a CAPINFO_SUNW_GLOB group id.
1224*11827SRod.Evans@Sun.COM 		 * The lead symbol for this global capability symbol is either
1225*11827SRod.Evans@Sun.COM 		 * the symbol itself, or an alias.
1226*11827SRod.Evans@Sun.COM 		 */
1227*11827SRod.Evans@Sun.COM 		if (gndx == CAPINFO_SUNW_GLOB) {
1228*11827SRod.Evans@Sun.COM 			if (ld_cap_add_family(ofl, lsdp, sdp,
1229*11827SRod.Evans@Sun.COM 			    NULL, NULL) == S_ERROR)
1230*11827SRod.Evans@Sun.COM 				return (S_ERROR);
1231*11827SRod.Evans@Sun.COM 			continue;
1232*11827SRod.Evans@Sun.COM 		}
1233*11827SRod.Evans@Sun.COM 
1234*11827SRod.Evans@Sun.COM 		/*
1235*11827SRod.Evans@Sun.COM 		 * Track the number of non-global capabilities symbols, as these
1236*11827SRod.Evans@Sun.COM 		 * are used to size any symbol tables.  If we're generating a
1237*11827SRod.Evans@Sun.COM 		 * dynamic object, this symbol will be added to the dynamic
1238*11827SRod.Evans@Sun.COM 		 * symbol table, therefore ensure there is space in the dynamic
1239*11827SRod.Evans@Sun.COM 		 * string table.
1240*11827SRod.Evans@Sun.COM 		 */
1241*11827SRod.Evans@Sun.COM 		ofl->ofl_caploclcnt++;
1242*11827SRod.Evans@Sun.COM 		if (((ofl->ofl_flags & FLG_OF_RELOBJ) == 0) &&
1243*11827SRod.Evans@Sun.COM 		    (st_insert(ofl->ofl_dynstrtab, sdp->sd_name) == -1))
1244*11827SRod.Evans@Sun.COM 			return (S_ERROR);
1245*11827SRod.Evans@Sun.COM 
1246*11827SRod.Evans@Sun.COM 		/*
1247*11827SRod.Evans@Sun.COM 		 * As we're tracking this local symbol as a capabilities symbol,
1248*11827SRod.Evans@Sun.COM 		 * reduce the local symbol count to compensate.
1249*11827SRod.Evans@Sun.COM 		 */
1250*11827SRod.Evans@Sun.COM 		ofl->ofl_locscnt--;
1251*11827SRod.Evans@Sun.COM 
1252*11827SRod.Evans@Sun.COM 		/*
1253*11827SRod.Evans@Sun.COM 		 * Determine whether the associated lead symbol indicates
1254*11827SRod.Evans@Sun.COM 		 * NODYNSORT.  If so, remove this local entry from the
1255*11827SRod.Evans@Sun.COM 		 * SUNW_dynsort section too.  NODYNSORT tagging can only be
1256*11827SRod.Evans@Sun.COM 		 * obtained from a mapfile symbol definition, and thus any
1257*11827SRod.Evans@Sun.COM 		 * global definition that has this tagging has already been
1258*11827SRod.Evans@Sun.COM 		 * instantiated and this instance resolved to it.
1259*11827SRod.Evans@Sun.COM 		 */
1260*11827SRod.Evans@Sun.COM 		if (lsdp->sd_flags & FLG_SY_NODYNSORT) {
1261*11827SRod.Evans@Sun.COM 			Sym	*lsym = lsdp->sd_sym;
1262*11827SRod.Evans@Sun.COM 			uchar_t ltype = ELF_ST_TYPE(lsym->st_info);
1263*11827SRod.Evans@Sun.COM 
1264*11827SRod.Evans@Sun.COM 			DYNSORT_COUNT(lsdp, lsym, ltype, --);
1265*11827SRod.Evans@Sun.COM 			lsdp->sd_flags |= FLG_SY_NODYNSORT;
1266*11827SRod.Evans@Sun.COM 		}
1267*11827SRod.Evans@Sun.COM 
1268*11827SRod.Evans@Sun.COM 		/*
1269*11827SRod.Evans@Sun.COM 		 * Track this family member, together with its associated group.
1270*11827SRod.Evans@Sun.COM 		 */
1271*11827SRod.Evans@Sun.COM 		if (ld_cap_add_family(ofl, lsdp, sdp,
1272*11827SRod.Evans@Sun.COM 		    cdp->ca_groups->apl_data[gndx], NULL) == S_ERROR)
1273*11827SRod.Evans@Sun.COM 			return (S_ERROR);
1274*11827SRod.Evans@Sun.COM 	}
1275*11827SRod.Evans@Sun.COM 
1276*11827SRod.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 /*
14270Sstevel@tonic-gate  * Process a progbits section.
14280Sstevel@tonic-gate  */
14291618Srie static uintptr_t
14300Sstevel@tonic-gate process_progbits(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
14310Sstevel@tonic-gate     Word ndx, int ident, Ofl_desc *ofl)
14320Sstevel@tonic-gate {
14339085SAli.Bahrami@Sun.COM 	int		stab_index = 0;
14349085SAli.Bahrami@Sun.COM 	Word		is_flags = 0;
14359085SAli.Bahrami@Sun.COM 	uintptr_t	r;
14360Sstevel@tonic-gate 
14370Sstevel@tonic-gate 	/*
14380Sstevel@tonic-gate 	 * Never include .stab.excl sections in any output file.
14390Sstevel@tonic-gate 	 * If the -s flag has been specified strip any .stab sections.
14400Sstevel@tonic-gate 	 */
14410Sstevel@tonic-gate 	if (ident && (strncmp(name, MSG_ORIG(MSG_SCN_STAB),
14420Sstevel@tonic-gate 	    MSG_SCN_STAB_SIZE) == 0)) {
14430Sstevel@tonic-gate 		if ((ofl->ofl_flags & FLG_OF_STRIP) ||
14440Sstevel@tonic-gate 		    (strcmp((name + MSG_SCN_STAB_SIZE),
14454716Sab196087 		    MSG_ORIG(MSG_SCN_EXCL)) == 0))
14460Sstevel@tonic-gate 			return (1);
14470Sstevel@tonic-gate 
14480Sstevel@tonic-gate 		if (strcmp((name + MSG_SCN_STAB_SIZE),
14490Sstevel@tonic-gate 		    MSG_ORIG(MSG_SCN_INDEX)) == 0)
14500Sstevel@tonic-gate 			stab_index = 1;
14510Sstevel@tonic-gate 	}
14520Sstevel@tonic-gate 
14530Sstevel@tonic-gate 	if ((ofl->ofl_flags & FLG_OF_STRIP) && ident) {
14540Sstevel@tonic-gate 		if ((strncmp(name, MSG_ORIG(MSG_SCN_DEBUG),
14550Sstevel@tonic-gate 		    MSG_SCN_DEBUG_SIZE) == 0) ||
14560Sstevel@tonic-gate 		    (strcmp(name, MSG_ORIG(MSG_SCN_LINE)) == 0))
14570Sstevel@tonic-gate 			return (1);
14580Sstevel@tonic-gate 	}
14590Sstevel@tonic-gate 
14600Sstevel@tonic-gate 	/*
14610Sstevel@tonic-gate 	 * Update the ident to reflect the type of section we've got.
14620Sstevel@tonic-gate 	 *
14630Sstevel@tonic-gate 	 * If there is any .plt or .got section to generate we'll be creating
14640Sstevel@tonic-gate 	 * our own version, so don't allow any input sections of these types to
14650Sstevel@tonic-gate 	 * be added to the output section list (why a relocatable object would
14660Sstevel@tonic-gate 	 * have a .plt or .got is a mystery, but stranger things have occurred).
14679085SAli.Bahrami@Sun.COM 	 *
14689085SAli.Bahrami@Sun.COM 	 * If there are any unwind sections, and this is a platform that uses
14699085SAli.Bahrami@Sun.COM 	 * SHT_PROGBITS for unwind sections, then set their ident to reflect
14709085SAli.Bahrami@Sun.COM 	 * that.
14710Sstevel@tonic-gate 	 */
14720Sstevel@tonic-gate 	if (ident) {
14739085SAli.Bahrami@Sun.COM 		if (shdr->sh_flags & SHF_TLS) {
14746206Sab196087 			ident = ld_targ.t_id.id_tls;
14759085SAli.Bahrami@Sun.COM 		} else if ((shdr->sh_flags & ~ALL_SHF_IGNORE) ==
14769085SAli.Bahrami@Sun.COM 		    (SHF_ALLOC | SHF_EXECINSTR)) {
14776206Sab196087 			ident = ld_targ.t_id.id_text;
14789085SAli.Bahrami@Sun.COM 		} else if (shdr->sh_flags & SHF_ALLOC) {
14799085SAli.Bahrami@Sun.COM 			int done = 0;
14809085SAli.Bahrami@Sun.COM 
14819085SAli.Bahrami@Sun.COM 			if (name[0] == '.') {
14829085SAli.Bahrami@Sun.COM 				switch (name[1]) {
14839085SAli.Bahrami@Sun.COM 				case 'e':
14849085SAli.Bahrami@Sun.COM 					if ((ld_targ.t_m.m_sht_unwind ==
14859085SAli.Bahrami@Sun.COM 					    SHT_PROGBITS) &&
148610454SAli.Bahrami@Sun.COM 					    is_name_cmp(name,
148710454SAli.Bahrami@Sun.COM 					    MSG_ORIG(MSG_SCN_EHFRAME),
148810454SAli.Bahrami@Sun.COM 					    MSG_SCN_EHFRAME_SIZE)) {
14899085SAli.Bahrami@Sun.COM 						ident = ld_targ.t_id.id_unwind;
14909085SAli.Bahrami@Sun.COM 						is_flags = FLG_IS_EHFRAME;
14919085SAli.Bahrami@Sun.COM 						done = 1;
14929085SAli.Bahrami@Sun.COM 					}
14939085SAli.Bahrami@Sun.COM 					break;
14949085SAli.Bahrami@Sun.COM 				case 'g':
149510454SAli.Bahrami@Sun.COM 					if (is_name_cmp(name,
149610454SAli.Bahrami@Sun.COM 					    MSG_ORIG(MSG_SCN_GOT),
149710454SAli.Bahrami@Sun.COM 					    MSG_SCN_GOT_SIZE)) {
14989085SAli.Bahrami@Sun.COM 						ident = ld_targ.t_id.id_null;
14999085SAli.Bahrami@Sun.COM 						done = 1;
15009085SAli.Bahrami@Sun.COM 						break;
15019085SAli.Bahrami@Sun.COM 					}
15029085SAli.Bahrami@Sun.COM 					if ((ld_targ.t_m.m_sht_unwind ==
150310454SAli.Bahrami@Sun.COM 					    SHT_PROGBITS) &&
150410454SAli.Bahrami@Sun.COM 					    is_name_cmp(name,
150510454SAli.Bahrami@Sun.COM 					    MSG_ORIG(MSG_SCN_GCC_X_TBL),
150610454SAli.Bahrami@Sun.COM 					    MSG_SCN_GCC_X_TBL_SIZE)) {
15079085SAli.Bahrami@Sun.COM 						ident = ld_targ.t_id.id_unwind;
15089085SAli.Bahrami@Sun.COM 						done = 1;
15099085SAli.Bahrami@Sun.COM 						break;
15109085SAli.Bahrami@Sun.COM 					}
15119085SAli.Bahrami@Sun.COM 					break;
15129085SAli.Bahrami@Sun.COM 				case 'p':
151310454SAli.Bahrami@Sun.COM 					if (is_name_cmp(name,
151410454SAli.Bahrami@Sun.COM 					    MSG_ORIG(MSG_SCN_PLT),
151510454SAli.Bahrami@Sun.COM 					    MSG_SCN_PLT_SIZE)) {
15169085SAli.Bahrami@Sun.COM 						ident = ld_targ.t_id.id_null;
15179085SAli.Bahrami@Sun.COM 						done = 1;
15189085SAli.Bahrami@Sun.COM 					}
15199085SAli.Bahrami@Sun.COM 					break;
15209085SAli.Bahrami@Sun.COM 				}
15219085SAli.Bahrami@Sun.COM 			}
15229085SAli.Bahrami@Sun.COM 			if (!done) {
15239085SAli.Bahrami@Sun.COM 				if (stab_index) {
15249085SAli.Bahrami@Sun.COM 					/*
15259085SAli.Bahrami@Sun.COM 					 * This is a work-around for x86
15269085SAli.Bahrami@Sun.COM 					 * compilers that have set SHF_ALLOC
15279085SAli.Bahrami@Sun.COM 					 * for the .stab.index section.
15289085SAli.Bahrami@Sun.COM 					 *
15299085SAli.Bahrami@Sun.COM 					 * Because of this, make sure that the
15309085SAli.Bahrami@Sun.COM 					 * .stab.index does not end up as the
15319085SAli.Bahrami@Sun.COM 					 * last section in the text segment.
15329085SAli.Bahrami@Sun.COM 					 * Older linkers can produce
15339085SAli.Bahrami@Sun.COM 					 * segmentation violations when they
15349085SAli.Bahrami@Sun.COM 					 * strip (ld -s) against a shared
15359085SAli.Bahrami@Sun.COM 					 * object whose last section in the
15369085SAli.Bahrami@Sun.COM 					 * text segment is a .stab.
15379085SAli.Bahrami@Sun.COM 					 */
15389085SAli.Bahrami@Sun.COM 					ident = ld_targ.t_id.id_interp;
15399085SAli.Bahrami@Sun.COM 				} else {
15409085SAli.Bahrami@Sun.COM 					ident = ld_targ.t_id.id_data;
15419085SAli.Bahrami@Sun.COM 				}
15429085SAli.Bahrami@Sun.COM 			}
15430Sstevel@tonic-gate 		} else
15446206Sab196087 			ident = ld_targ.t_id.id_note;
15450Sstevel@tonic-gate 	}
15469085SAli.Bahrami@Sun.COM 
15479085SAli.Bahrami@Sun.COM 	r = process_section(name, ifl, shdr, scn, ndx, ident, ofl);
15489085SAli.Bahrami@Sun.COM 
15499085SAli.Bahrami@Sun.COM 	/*
15509085SAli.Bahrami@Sun.COM 	 * On success, process_section() creates an input section descriptor.
15519085SAli.Bahrami@Sun.COM 	 * Now that it exists, we can add any pending input section flags.
15529085SAli.Bahrami@Sun.COM 	 */
15539085SAli.Bahrami@Sun.COM 	if ((is_flags != 0) && (r == 1))
15549085SAli.Bahrami@Sun.COM 		ifl->ifl_isdesc[ndx]->is_flags |= is_flags;
15559085SAli.Bahrami@Sun.COM 
15569085SAli.Bahrami@Sun.COM 	return (r);
15570Sstevel@tonic-gate }
15580Sstevel@tonic-gate 
15590Sstevel@tonic-gate /*
15600Sstevel@tonic-gate  * Handles the SHT_SUNW_{DEBUG,DEBUGSTR) sections.
15610Sstevel@tonic-gate  */
15621618Srie static uintptr_t
15630Sstevel@tonic-gate process_debug(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
15640Sstevel@tonic-gate     Word ndx, int ident, Ofl_desc *ofl)
15650Sstevel@tonic-gate {
15660Sstevel@tonic-gate 	/*
15670Sstevel@tonic-gate 	 * Debug information is discarded when the 'ld -s' flag is invoked.
15680Sstevel@tonic-gate 	 */
15690Sstevel@tonic-gate 	if (ofl->ofl_flags & FLG_OF_STRIP) {
15700Sstevel@tonic-gate 		return (1);
15710Sstevel@tonic-gate 	}
15720Sstevel@tonic-gate 	return (process_progbits(name, ifl, shdr, scn, ndx, ident, ofl));
15730Sstevel@tonic-gate }
15740Sstevel@tonic-gate 
15750Sstevel@tonic-gate /*
15760Sstevel@tonic-gate  * Process a nobits section.
15770Sstevel@tonic-gate  */
15781618Srie static uintptr_t
15790Sstevel@tonic-gate process_nobits(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
15800Sstevel@tonic-gate     Word ndx, int ident, Ofl_desc *ofl)
15810Sstevel@tonic-gate {
15820Sstevel@tonic-gate 	if (ident) {
15830Sstevel@tonic-gate 		if (shdr->sh_flags & SHF_TLS)
15846206Sab196087 			ident = ld_targ.t_id.id_tlsbss;
15856206Sab196087 #if	defined(_ELF64)
15866206Sab196087 		else if ((shdr->sh_flags & SHF_AMD64_LARGE) &&
15876206Sab196087 		    (ld_targ.t_m.m_mach == EM_AMD64))
15886206Sab196087 			ident = ld_targ.t_id.id_lbss;
1589574Sseizo #endif
15900Sstevel@tonic-gate 		else
15916206Sab196087 			ident = ld_targ.t_id.id_bss;
15920Sstevel@tonic-gate 	}
15930Sstevel@tonic-gate 	return (process_section(name, ifl, shdr, scn, ndx, ident, ofl));
15940Sstevel@tonic-gate }
15950Sstevel@tonic-gate 
15960Sstevel@tonic-gate /*
15970Sstevel@tonic-gate  * Process a SHT_*_ARRAY section.
15980Sstevel@tonic-gate  */
15991618Srie static uintptr_t
16000Sstevel@tonic-gate process_array(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
16010Sstevel@tonic-gate     Word ndx, int ident, Ofl_desc *ofl)
16020Sstevel@tonic-gate {
16037463SRod.Evans@Sun.COM 	uintptr_t	error;
16040Sstevel@tonic-gate 
16050Sstevel@tonic-gate 	if (ident)
16066206Sab196087 		ident = ld_targ.t_id.id_array;
16070Sstevel@tonic-gate 
16087463SRod.Evans@Sun.COM 	error = process_section(name, ifl, shdr, scn, ndx, ident, ofl);
16097463SRod.Evans@Sun.COM 	if ((error == 0) || (error == S_ERROR))
16107463SRod.Evans@Sun.COM 		return (error);
16117463SRod.Evans@Sun.COM 
16127463SRod.Evans@Sun.COM 	return (1);
16137463SRod.Evans@Sun.COM }
16140Sstevel@tonic-gate 
16157463SRod.Evans@Sun.COM static uintptr_t
16167463SRod.Evans@Sun.COM /* ARGSUSED1 */
16177463SRod.Evans@Sun.COM array_process(Is_desc *isc, Ifl_desc *ifl, Ofl_desc *ofl)
16187463SRod.Evans@Sun.COM {
16197463SRod.Evans@Sun.COM 	Os_desc	*osp;
16207463SRod.Evans@Sun.COM 	Shdr	*shdr;
16217463SRod.Evans@Sun.COM 
16227463SRod.Evans@Sun.COM 	if ((isc == NULL) || ((osp = isc->is_osdesc) == NULL))
16230Sstevel@tonic-gate 		return (0);
16240Sstevel@tonic-gate 
16257463SRod.Evans@Sun.COM 	shdr = isc->is_shdr;
16267463SRod.Evans@Sun.COM 
16270Sstevel@tonic-gate 	if ((shdr->sh_type == SHT_FINI_ARRAY) &&
16287463SRod.Evans@Sun.COM 	    (ofl->ofl_osfiniarray == NULL))
16290Sstevel@tonic-gate 		ofl->ofl_osfiniarray = osp;
16300Sstevel@tonic-gate 	else if ((shdr->sh_type == SHT_INIT_ARRAY) &&
16317463SRod.Evans@Sun.COM 	    (ofl->ofl_osinitarray == NULL))
16320Sstevel@tonic-gate 		ofl->ofl_osinitarray = osp;
16330Sstevel@tonic-gate 	else if ((shdr->sh_type == SHT_PREINIT_ARRAY) &&
16347463SRod.Evans@Sun.COM 	    (ofl->ofl_ospreinitarray == NULL))
16350Sstevel@tonic-gate 		ofl->ofl_ospreinitarray = osp;
16360Sstevel@tonic-gate 
16370Sstevel@tonic-gate 	return (1);
16380Sstevel@tonic-gate }
16390Sstevel@tonic-gate 
16400Sstevel@tonic-gate /*
16410Sstevel@tonic-gate  * Process a SHT_SYMTAB_SHNDX section.
16420Sstevel@tonic-gate  */
16431618Srie static uintptr_t
16440Sstevel@tonic-gate process_sym_shndx(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
16450Sstevel@tonic-gate     Word ndx, int ident, Ofl_desc *ofl)
16460Sstevel@tonic-gate {
16470Sstevel@tonic-gate 	if (process_input(name, ifl, shdr, scn, ndx, ident, ofl) == S_ERROR)
16480Sstevel@tonic-gate 		return (S_ERROR);
16490Sstevel@tonic-gate 
16500Sstevel@tonic-gate 	/*
16510Sstevel@tonic-gate 	 * Have we already seen the related SYMTAB - if so verify it now.
16520Sstevel@tonic-gate 	 */
16530Sstevel@tonic-gate 	if (shdr->sh_link < ndx) {
16540Sstevel@tonic-gate 		Is_desc	*isp = ifl->ifl_isdesc[shdr->sh_link];
16550Sstevel@tonic-gate 
165610792SRod.Evans@Sun.COM 		if ((isp == NULL) || ((isp->is_shdr->sh_type != SHT_SYMTAB) &&
16570Sstevel@tonic-gate 		    (isp->is_shdr->sh_type != SHT_DYNSYM))) {
16581618Srie 			eprintf(ofl->ofl_lml, ERR_FATAL,
16599878SAli.Bahrami@Sun.COM 			    MSG_INTL(MSG_FIL_INVSHLINK), ifl->ifl_name,
16609878SAli.Bahrami@Sun.COM 			    EC_WORD(ndx), name, EC_XWORD(shdr->sh_link));
16610Sstevel@tonic-gate 			return (S_ERROR);
16620Sstevel@tonic-gate 		}
16630Sstevel@tonic-gate 		isp->is_symshndx = ifl->ifl_isdesc[ndx];
16640Sstevel@tonic-gate 	}
16650Sstevel@tonic-gate 	return (1);
16660Sstevel@tonic-gate }
16670Sstevel@tonic-gate 
16680Sstevel@tonic-gate /*
16690Sstevel@tonic-gate  * Final processing for SHT_SYMTAB_SHNDX section.
16700Sstevel@tonic-gate  */
16711618Srie static uintptr_t
16720Sstevel@tonic-gate /* ARGSUSED2 */
16730Sstevel@tonic-gate sym_shndx_process(Is_desc *isc, Ifl_desc *ifl, Ofl_desc *ofl)
16740Sstevel@tonic-gate {
16750Sstevel@tonic-gate 	if (isc->is_shdr->sh_link > isc->is_scnndx) {
16760Sstevel@tonic-gate 		Is_desc	*isp = ifl->ifl_isdesc[isc->is_shdr->sh_link];
16770Sstevel@tonic-gate 
167810792SRod.Evans@Sun.COM 		if ((isp == NULL) || ((isp->is_shdr->sh_type != SHT_SYMTAB) &&
16790Sstevel@tonic-gate 		    (isp->is_shdr->sh_type != SHT_DYNSYM))) {
16801618Srie 			eprintf(ofl->ofl_lml, ERR_FATAL,
16811618Srie 			    MSG_INTL(MSG_FIL_INVSHLINK), isc->is_file->ifl_name,
16829878SAli.Bahrami@Sun.COM 			    EC_WORD(isc->is_scnndx), isc->is_name,
16839878SAli.Bahrami@Sun.COM 			    EC_XWORD(isc->is_shdr->sh_link));
16840Sstevel@tonic-gate 			return (S_ERROR);
16850Sstevel@tonic-gate 		}
16860Sstevel@tonic-gate 		isp->is_symshndx = isc;
16870Sstevel@tonic-gate 	}
16880Sstevel@tonic-gate 	return (1);
16890Sstevel@tonic-gate }
16900Sstevel@tonic-gate 
16910Sstevel@tonic-gate /*
16920Sstevel@tonic-gate  * Process .dynamic section from a relocatable object.
16930Sstevel@tonic-gate  *
16940Sstevel@tonic-gate  * Note: That the .dynamic section is only considered interesting when
16950Sstevel@tonic-gate  *	 dlopen()ing a relocatable object (thus FLG_OF1_RELDYN can only get
16960Sstevel@tonic-gate  *	 set when libld is called from ld.so.1).
16970Sstevel@tonic-gate  */
16980Sstevel@tonic-gate /*ARGSUSED*/
16991618Srie static uintptr_t
17000Sstevel@tonic-gate process_rel_dynamic(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
17010Sstevel@tonic-gate     Word ndx, int ident, Ofl_desc *ofl)
17020Sstevel@tonic-gate {
17030Sstevel@tonic-gate 	Dyn		*dyn;
17040Sstevel@tonic-gate 	Elf_Scn		*strscn;
17050Sstevel@tonic-gate 	Elf_Data	*dp;
17060Sstevel@tonic-gate 	char		*str;
17070Sstevel@tonic-gate 
17080Sstevel@tonic-gate 	/*
17090Sstevel@tonic-gate 	 * Process .dynamic sections from relocatable objects ?
17100Sstevel@tonic-gate 	 */
17110Sstevel@tonic-gate 	if ((ofl->ofl_flags1 & FLG_OF1_RELDYN) == 0)
17120Sstevel@tonic-gate 		return (1);
17130Sstevel@tonic-gate 
17140Sstevel@tonic-gate 	/*
17150Sstevel@tonic-gate 	 * Find the string section associated with the .dynamic section.
17160Sstevel@tonic-gate 	 */
17170Sstevel@tonic-gate 	if ((strscn = elf_getscn(ifl->ifl_elf, shdr->sh_link)) == NULL) {
17181618Srie 		eprintf(ofl->ofl_lml, ERR_ELF, MSG_INTL(MSG_ELF_GETSCN),
17191618Srie 		    ifl->ifl_name);
17200Sstevel@tonic-gate 		ofl->ofl_flags |= FLG_OF_FATAL;
17210Sstevel@tonic-gate 		return (0);
17220Sstevel@tonic-gate 	}
17230Sstevel@tonic-gate 	dp = elf_getdata(strscn, NULL);
17240Sstevel@tonic-gate 	str = (char *)dp->d_buf;
17250Sstevel@tonic-gate 
17260Sstevel@tonic-gate 	/*
17270Sstevel@tonic-gate 	 * And get the .dynamic data
17280Sstevel@tonic-gate 	 */
17290Sstevel@tonic-gate 	dp = elf_getdata(scn, NULL);
17300Sstevel@tonic-gate 
17310Sstevel@tonic-gate 	for (dyn = (Dyn *)dp->d_buf; dyn->d_tag != DT_NULL; dyn++) {
17321109Srie 		Ifl_desc	*difl;
17330Sstevel@tonic-gate 
17340Sstevel@tonic-gate 		switch (dyn->d_tag) {
17350Sstevel@tonic-gate 		case DT_NEEDED:
17360Sstevel@tonic-gate 		case DT_USED:
17379131SRod.Evans@Sun.COM 			if (((difl = libld_calloc(1,
17389131SRod.Evans@Sun.COM 			    sizeof (Ifl_desc))) == NULL) ||
17399131SRod.Evans@Sun.COM 			    (aplist_append(&ofl->ofl_sos, difl,
17409131SRod.Evans@Sun.COM 			    AL_CNT_OFL_LIBS) == NULL))
17410Sstevel@tonic-gate 				return (S_ERROR);
17421109Srie 
17431109Srie 			difl->ifl_name = MSG_ORIG(MSG_STR_DYNAMIC);
17441109Srie 			difl->ifl_soname = str + (size_t)dyn->d_un.d_val;
17451109Srie 			difl->ifl_flags = FLG_IF_NEEDSTR;
17460Sstevel@tonic-gate 			break;
17470Sstevel@tonic-gate 		case DT_RPATH:
17480Sstevel@tonic-gate 		case DT_RUNPATH:
17490Sstevel@tonic-gate 			if ((ofl->ofl_rpath = add_string(ofl->ofl_rpath,
17500Sstevel@tonic-gate 			    (str + (size_t)dyn->d_un.d_val))) ==
17510Sstevel@tonic-gate 			    (const char *)S_ERROR)
17520Sstevel@tonic-gate 				return (S_ERROR);
17530Sstevel@tonic-gate 			break;
17544716Sab196087 		case DT_VERSYM:
17554716Sab196087 			/*
17564716Sab196087 			 * The Solaris ld does not put DT_VERSYM in the
17574716Sab196087 			 * dynamic section. If the object has DT_VERSYM,
17584716Sab196087 			 * then it must have been produced by the GNU ld,
17594716Sab196087 			 * and is using the GNU style of versioning.
17604716Sab196087 			 */
17614716Sab196087 			ifl->ifl_flags |= FLG_IF_GNUVER;
17624716Sab196087 			break;
17630Sstevel@tonic-gate 		}
17640Sstevel@tonic-gate 	}
17650Sstevel@tonic-gate 	return (1);
17660Sstevel@tonic-gate }
17670Sstevel@tonic-gate 
17680Sstevel@tonic-gate /*
17690Sstevel@tonic-gate  * Expand implicit references.  Dependencies can be specified in terms of the
1770*11827SRod.Evans@Sun.COM  * $ORIGIN, $MACHINE, $PLATFORM, $OSREL and $OSNAME tokens, either from their
1771*11827SRod.Evans@Sun.COM  * needed name, or via a runpath.  In addition runpaths may also specify the
1772*11827SRod.Evans@Sun.COM  * $ISALIST token.
17730Sstevel@tonic-gate  *
17741109Srie  * Probably the most common reference to explicit dependencies (via -L) will be
17750Sstevel@tonic-gate  * sufficient to find any associated implicit dependencies, but just in case we
17760Sstevel@tonic-gate  * expand any occurrence of these known tokens here.
17770Sstevel@tonic-gate  *
17780Sstevel@tonic-gate  * Note, if any errors occur we simply return the original name.
17790Sstevel@tonic-gate  *
17800Sstevel@tonic-gate  * This code is remarkably similar to expand() in rtld/common/paths.c.
17810Sstevel@tonic-gate  */
1782*11827SRod.Evans@Sun.COM static char		*machine = NULL;
1783*11827SRod.Evans@Sun.COM static size_t		machine_sz = 0;
178410792SRod.Evans@Sun.COM static char		*platform = NULL;
17850Sstevel@tonic-gate static size_t		platform_sz = 0;
178610792SRod.Evans@Sun.COM static Isa_desc		*isa = NULL;
178710792SRod.Evans@Sun.COM static Uts_desc		*uts = NULL;
17880Sstevel@tonic-gate 
17890Sstevel@tonic-gate static char *
17900Sstevel@tonic-gate expand(const char *parent, const char *name, char **next)
17910Sstevel@tonic-gate {
17920Sstevel@tonic-gate 	char		_name[PATH_MAX], *nptr, *_next;
17930Sstevel@tonic-gate 	const char	*optr;
17940Sstevel@tonic-gate 	size_t		nrem = PATH_MAX - 1;
17950Sstevel@tonic-gate 	int		expanded = 0, _expanded, isaflag = 0;
17960Sstevel@tonic-gate 
17970Sstevel@tonic-gate 	optr = name;
17980Sstevel@tonic-gate 	nptr = _name;
17990Sstevel@tonic-gate 
18000Sstevel@tonic-gate 	while (*optr) {
18010Sstevel@tonic-gate 		if (nrem == 0)
18020Sstevel@tonic-gate 			return ((char *)name);
18030Sstevel@tonic-gate 
18040Sstevel@tonic-gate 		if (*optr != '$') {
18050Sstevel@tonic-gate 			*nptr++ = *optr++, nrem--;
18060Sstevel@tonic-gate 			continue;
18070Sstevel@tonic-gate 		}
18080Sstevel@tonic-gate 
18090Sstevel@tonic-gate 		_expanded = 0;
18100Sstevel@tonic-gate 
18110Sstevel@tonic-gate 		if (strncmp(optr, MSG_ORIG(MSG_STR_ORIGIN),
18120Sstevel@tonic-gate 		    MSG_STR_ORIGIN_SIZE) == 0) {
18130Sstevel@tonic-gate 			char *eptr;
18140Sstevel@tonic-gate 
18150Sstevel@tonic-gate 			/*
18160Sstevel@tonic-gate 			 * For $ORIGIN, expansion is really just a concatenation
18170Sstevel@tonic-gate 			 * of the parents directory name.  For example, an
18181109Srie 			 * explicit dependency foo/bar/lib1.so with a dependency
18190Sstevel@tonic-gate 			 * on $ORIGIN/lib2.so would be expanded to
18200Sstevel@tonic-gate 			 * foo/bar/lib2.so.
18210Sstevel@tonic-gate 			 */
182210792SRod.Evans@Sun.COM 			if ((eptr = strrchr(parent, '/')) == NULL) {
18230Sstevel@tonic-gate 				*nptr++ = '.';
18240Sstevel@tonic-gate 				nrem--;
18250Sstevel@tonic-gate 			} else {
18260Sstevel@tonic-gate 				size_t	len = eptr - parent;
18270Sstevel@tonic-gate 
18280Sstevel@tonic-gate 				if (len >= nrem)
18290Sstevel@tonic-gate 					return ((char *)name);
18300Sstevel@tonic-gate 
18310Sstevel@tonic-gate 				(void) strncpy(nptr, parent, len);
18320Sstevel@tonic-gate 				nptr = nptr + len;
18330Sstevel@tonic-gate 				nrem -= len;
18340Sstevel@tonic-gate 			}
18350Sstevel@tonic-gate 			optr += MSG_STR_ORIGIN_SIZE;
18360Sstevel@tonic-gate 			expanded = _expanded = 1;
18370Sstevel@tonic-gate 
1838*11827SRod.Evans@Sun.COM 		} else if (strncmp(optr, MSG_ORIG(MSG_STR_MACHINE),
1839*11827SRod.Evans@Sun.COM 		    MSG_STR_MACHINE_SIZE) == 0) {
1840*11827SRod.Evans@Sun.COM 			/*
1841*11827SRod.Evans@Sun.COM 			 * Establish the machine from sysconf - like uname -i.
1842*11827SRod.Evans@Sun.COM 			 */
1843*11827SRod.Evans@Sun.COM 			if ((machine == NULL) && (machine_sz == 0)) {
1844*11827SRod.Evans@Sun.COM 				char	info[SYS_NMLN];
1845*11827SRod.Evans@Sun.COM 				long	size;
1846*11827SRod.Evans@Sun.COM 
1847*11827SRod.Evans@Sun.COM 				size = sysinfo(SI_MACHINE, info, SYS_NMLN);
1848*11827SRod.Evans@Sun.COM 				if ((size != -1) &&
1849*11827SRod.Evans@Sun.COM 				    (machine = libld_malloc((size_t)size))) {
1850*11827SRod.Evans@Sun.COM 					(void) strcpy(machine, info);
1851*11827SRod.Evans@Sun.COM 					machine_sz = (size_t)size - 1;
1852*11827SRod.Evans@Sun.COM 				} else
1853*11827SRod.Evans@Sun.COM 					machine_sz = 1;
1854*11827SRod.Evans@Sun.COM 			}
1855*11827SRod.Evans@Sun.COM 			if (machine) {
1856*11827SRod.Evans@Sun.COM 				if (machine_sz >= nrem)
1857*11827SRod.Evans@Sun.COM 					return ((char *)name);
1858*11827SRod.Evans@Sun.COM 
1859*11827SRod.Evans@Sun.COM 				(void) strncpy(nptr, machine, machine_sz);
1860*11827SRod.Evans@Sun.COM 				nptr = nptr + machine_sz;
1861*11827SRod.Evans@Sun.COM 				nrem -= machine_sz;
1862*11827SRod.Evans@Sun.COM 
1863*11827SRod.Evans@Sun.COM 				optr += MSG_STR_MACHINE_SIZE;
1864*11827SRod.Evans@Sun.COM 				expanded = _expanded = 1;
1865*11827SRod.Evans@Sun.COM 			}
1866*11827SRod.Evans@Sun.COM 
18670Sstevel@tonic-gate 		} else if (strncmp(optr, MSG_ORIG(MSG_STR_PLATFORM),
18680Sstevel@tonic-gate 		    MSG_STR_PLATFORM_SIZE) == 0) {
18690Sstevel@tonic-gate 			/*
18700Sstevel@tonic-gate 			 * Establish the platform from sysconf - like uname -i.
18710Sstevel@tonic-gate 			 */
187210792SRod.Evans@Sun.COM 			if ((platform == NULL) && (platform_sz == 0)) {
18730Sstevel@tonic-gate 				char	info[SYS_NMLN];
18740Sstevel@tonic-gate 				long	size;
18750Sstevel@tonic-gate 
18760Sstevel@tonic-gate 				size = sysinfo(SI_PLATFORM, info, SYS_NMLN);
18770Sstevel@tonic-gate 				if ((size != -1) &&
18780Sstevel@tonic-gate 				    (platform = libld_malloc((size_t)size))) {
18790Sstevel@tonic-gate 					(void) strcpy(platform, info);
18800Sstevel@tonic-gate 					platform_sz = (size_t)size - 1;
18810Sstevel@tonic-gate 				} else
18820Sstevel@tonic-gate 					platform_sz = 1;
18830Sstevel@tonic-gate 			}
188410792SRod.Evans@Sun.COM 			if (platform) {
18850Sstevel@tonic-gate 				if (platform_sz >= nrem)
18860Sstevel@tonic-gate 					return ((char *)name);
18870Sstevel@tonic-gate 
18880Sstevel@tonic-gate 				(void) strncpy(nptr, platform, platform_sz);
18890Sstevel@tonic-gate 				nptr = nptr + platform_sz;
18900Sstevel@tonic-gate 				nrem -= platform_sz;
18910Sstevel@tonic-gate 
18920Sstevel@tonic-gate 				optr += MSG_STR_PLATFORM_SIZE;
18930Sstevel@tonic-gate 				expanded = _expanded = 1;
18940Sstevel@tonic-gate 			}
18950Sstevel@tonic-gate 
18960Sstevel@tonic-gate 		} else if (strncmp(optr, MSG_ORIG(MSG_STR_OSNAME),
18970Sstevel@tonic-gate 		    MSG_STR_OSNAME_SIZE) == 0) {
18980Sstevel@tonic-gate 			/*
18990Sstevel@tonic-gate 			 * Establish the os name - like uname -s.
19000Sstevel@tonic-gate 			 */
190110792SRod.Evans@Sun.COM 			if (uts == NULL)
19020Sstevel@tonic-gate 				uts = conv_uts();
19030Sstevel@tonic-gate 
19040Sstevel@tonic-gate 			if (uts && uts->uts_osnamesz) {
19050Sstevel@tonic-gate 				if (uts->uts_osnamesz >= nrem)
19060Sstevel@tonic-gate 					return ((char *)name);
19070Sstevel@tonic-gate 
19080Sstevel@tonic-gate 				(void) strncpy(nptr, uts->uts_osname,
19090Sstevel@tonic-gate 				    uts->uts_osnamesz);
19100Sstevel@tonic-gate 				nptr = nptr + uts->uts_osnamesz;
19110Sstevel@tonic-gate 				nrem -= uts->uts_osnamesz;
19120Sstevel@tonic-gate 
19130Sstevel@tonic-gate 				optr += MSG_STR_OSNAME_SIZE;
19140Sstevel@tonic-gate 				expanded = _expanded = 1;
19150Sstevel@tonic-gate 			}
19160Sstevel@tonic-gate 
19170Sstevel@tonic-gate 		} else if (strncmp(optr, MSG_ORIG(MSG_STR_OSREL),
19180Sstevel@tonic-gate 		    MSG_STR_OSREL_SIZE) == 0) {
19190Sstevel@tonic-gate 			/*
19200Sstevel@tonic-gate 			 * Establish the os release - like uname -r.
19210Sstevel@tonic-gate 			 */
192210792SRod.Evans@Sun.COM 			if (uts == NULL)
19230Sstevel@tonic-gate 				uts = conv_uts();
19240Sstevel@tonic-gate 
19250Sstevel@tonic-gate 			if (uts && uts->uts_osrelsz) {
19260Sstevel@tonic-gate 				if (uts->uts_osrelsz >= nrem)
19270Sstevel@tonic-gate 					return ((char *)name);
19280Sstevel@tonic-gate 
19290Sstevel@tonic-gate 				(void) strncpy(nptr, uts->uts_osrel,
19300Sstevel@tonic-gate 				    uts->uts_osrelsz);
19310Sstevel@tonic-gate 				nptr = nptr + uts->uts_osrelsz;
19320Sstevel@tonic-gate 				nrem -= uts->uts_osrelsz;
19330Sstevel@tonic-gate 
19340Sstevel@tonic-gate 				optr += MSG_STR_OSREL_SIZE;
19350Sstevel@tonic-gate 				expanded = _expanded = 1;
19360Sstevel@tonic-gate 			}
19370Sstevel@tonic-gate 
19380Sstevel@tonic-gate 		} else if ((strncmp(optr, MSG_ORIG(MSG_STR_ISALIST),
19390Sstevel@tonic-gate 		    MSG_STR_ISALIST_SIZE) == 0) && next && (isaflag++ == 0)) {
19400Sstevel@tonic-gate 			/*
19410Sstevel@tonic-gate 			 * Establish instruction sets from sysconf.  Note that
19420Sstevel@tonic-gate 			 * this is only meaningful from runpaths.
19430Sstevel@tonic-gate 			 */
194410792SRod.Evans@Sun.COM 			if (isa == NULL)
19450Sstevel@tonic-gate 				isa = conv_isalist();
19460Sstevel@tonic-gate 
19470Sstevel@tonic-gate 			if (isa && isa->isa_listsz &&
19480Sstevel@tonic-gate 			    (nrem > isa->isa_opt->isa_namesz)) {
19490Sstevel@tonic-gate 				size_t		mlen, tlen, hlen = optr - name;
19500Sstevel@tonic-gate 				size_t		no;
19510Sstevel@tonic-gate 				char		*lptr;
19520Sstevel@tonic-gate 				Isa_opt		*opt = isa->isa_opt;
19530Sstevel@tonic-gate 
19540Sstevel@tonic-gate 				(void) strncpy(nptr, opt->isa_name,
19550Sstevel@tonic-gate 				    opt->isa_namesz);
19560Sstevel@tonic-gate 				nptr = nptr + opt->isa_namesz;
19570Sstevel@tonic-gate 				nrem -= opt->isa_namesz;
19580Sstevel@tonic-gate 
19590Sstevel@tonic-gate 				optr += MSG_STR_ISALIST_SIZE;
19600Sstevel@tonic-gate 				expanded = _expanded = 1;
19610Sstevel@tonic-gate 
19620Sstevel@tonic-gate 				tlen = strlen(optr);
19630Sstevel@tonic-gate 
19640Sstevel@tonic-gate 				/*
19650Sstevel@tonic-gate 				 * As ISALIST expands to a number of elements,
19660Sstevel@tonic-gate 				 * establish a new list to return to the caller.
19670Sstevel@tonic-gate 				 * This will contain the present path being
19680Sstevel@tonic-gate 				 * processed redefined for each isalist option,
19690Sstevel@tonic-gate 				 * plus the original remaining list entries.
19700Sstevel@tonic-gate 				 */
19710Sstevel@tonic-gate 				mlen = ((hlen + tlen) * (isa->isa_optno - 1)) +
19720Sstevel@tonic-gate 				    isa->isa_listsz - opt->isa_namesz;
19730Sstevel@tonic-gate 				if (*next)
19744716Sab196087 					mlen += strlen(*next);
197510792SRod.Evans@Sun.COM 				if ((_next = lptr = libld_malloc(mlen)) == NULL)
19760Sstevel@tonic-gate 					return (0);
19770Sstevel@tonic-gate 
19780Sstevel@tonic-gate 				for (no = 1, opt++; no < isa->isa_optno;
19790Sstevel@tonic-gate 				    no++, opt++) {
19800Sstevel@tonic-gate 					(void) strncpy(lptr, name, hlen);
19810Sstevel@tonic-gate 					lptr = lptr + hlen;
19820Sstevel@tonic-gate 					(void) strncpy(lptr, opt->isa_name,
19830Sstevel@tonic-gate 					    opt->isa_namesz);
19840Sstevel@tonic-gate 					lptr = lptr + opt->isa_namesz;
19850Sstevel@tonic-gate 					(void) strncpy(lptr, optr, tlen);
19860Sstevel@tonic-gate 					lptr = lptr + tlen;
19870Sstevel@tonic-gate 					*lptr++ = ':';
19880Sstevel@tonic-gate 				}
19890Sstevel@tonic-gate 				if (*next)
19900Sstevel@tonic-gate 					(void) strcpy(lptr, *next);
19910Sstevel@tonic-gate 				else
19920Sstevel@tonic-gate 					*--lptr = '\0';
19930Sstevel@tonic-gate 			}
19940Sstevel@tonic-gate 		}
19950Sstevel@tonic-gate 
19960Sstevel@tonic-gate 		/*
19970Sstevel@tonic-gate 		 * If no expansion occurred skip the $ and continue.
19980Sstevel@tonic-gate 		 */
19990Sstevel@tonic-gate 		if (_expanded == 0)
20000Sstevel@tonic-gate 			*nptr++ = *optr++, nrem--;
20010Sstevel@tonic-gate 	}
20020Sstevel@tonic-gate 
20030Sstevel@tonic-gate 	/*
20040Sstevel@tonic-gate 	 * If any ISALIST processing has occurred not only do we return the
20050Sstevel@tonic-gate 	 * expanded node we're presently working on, but we must also update the
20060Sstevel@tonic-gate 	 * remaining list so that it is effectively prepended with this node
20070Sstevel@tonic-gate 	 * expanded to all remaining isalist options.  Note that we can only
20080Sstevel@tonic-gate 	 * handle one ISALIST per node.  For more than one ISALIST to be
20090Sstevel@tonic-gate 	 * processed we'd need a better algorithm than above to replace the
20100Sstevel@tonic-gate 	 * newly generated list.  Whether we want to encourage the number of
20110Sstevel@tonic-gate 	 * pathname permutations this would provide is another question. So, for
20120Sstevel@tonic-gate 	 * now if more than one ISALIST is encountered we return the original
20130Sstevel@tonic-gate 	 * node untouched.
20140Sstevel@tonic-gate 	 */
20150Sstevel@tonic-gate 	if (isaflag) {
20160Sstevel@tonic-gate 		if (isaflag == 1)
20170Sstevel@tonic-gate 			*next = _next;
20180Sstevel@tonic-gate 		else
20190Sstevel@tonic-gate 			return ((char *)name);
20200Sstevel@tonic-gate 	}
20210Sstevel@tonic-gate 
20220Sstevel@tonic-gate 	*nptr = '\0';
20230Sstevel@tonic-gate 
20240Sstevel@tonic-gate 	if (expanded) {
202510792SRod.Evans@Sun.COM 		if ((nptr = libld_malloc(strlen(_name) + 1)) == NULL)
20260Sstevel@tonic-gate 			return ((char *)name);
20270Sstevel@tonic-gate 		(void) strcpy(nptr, _name);
20280Sstevel@tonic-gate 		return (nptr);
20290Sstevel@tonic-gate 	}
20300Sstevel@tonic-gate 	return ((char *)name);
20310Sstevel@tonic-gate }
20320Sstevel@tonic-gate 
20330Sstevel@tonic-gate /*
20344716Sab196087  * The Solaris ld does not put DT_VERSYM in the dynamic section, but the
20354716Sab196087  * GNU ld does, and it is used by the runtime linker to implement their
20364716Sab196087  * versioning scheme. Use this fact to determine if the sharable object
20374716Sab196087  * was produced by the GNU ld rather than the Solaris one, and to set
20384716Sab196087  * FLG_IF_GNUVER if so. This needs to be done before the symbols are
20394716Sab196087  * processed, since the answer determines whether we interpret the
20404716Sab196087  * symbols versions according to Solaris or GNU rules.
20414716Sab196087  */
20424716Sab196087 /*ARGSUSED*/
20434716Sab196087 static uintptr_t
20444716Sab196087 process_dynamic_isgnu(const char *name, Ifl_desc *ifl, Shdr *shdr,
20454716Sab196087     Elf_Scn *scn, Word ndx, int ident, Ofl_desc *ofl)
20464716Sab196087 {
20474716Sab196087 	Dyn		*dyn;
20484716Sab196087 	Elf_Data	*dp;
20497463SRod.Evans@Sun.COM 	uintptr_t	error;
20504716Sab196087 
20517463SRod.Evans@Sun.COM 	error = process_section(name, ifl, shdr, scn, ndx, ident, ofl);
20527463SRod.Evans@Sun.COM 	if ((error == 0) || (error == S_ERROR))
20537463SRod.Evans@Sun.COM 		return (error);
20544716Sab196087 
20554716Sab196087 	/* Get the .dynamic data */
20564716Sab196087 	dp = elf_getdata(scn, NULL);
20574716Sab196087 
20584716Sab196087 	for (dyn = (Dyn *)dp->d_buf; dyn->d_tag != DT_NULL; dyn++) {
20594716Sab196087 		if (dyn->d_tag == DT_VERSYM) {
20604716Sab196087 			ifl->ifl_flags |= FLG_IF_GNUVER;
20614716Sab196087 			break;
20624716Sab196087 		}
20634716Sab196087 	}
20644716Sab196087 	return (1);
20654716Sab196087 }
20664716Sab196087 
20674716Sab196087 /*
20680Sstevel@tonic-gate  * Process a dynamic section.  If we are processing an explicit shared object
20690Sstevel@tonic-gate  * then we need to determine if it has a recorded SONAME, if so, this name will
20700Sstevel@tonic-gate  * be recorded in the output file being generated as the NEEDED entry rather
20710Sstevel@tonic-gate  * than the shared objects filename itself.
20720Sstevel@tonic-gate  * If the mode of the link-edit indicates that no undefined symbols should
20730Sstevel@tonic-gate  * remain, then we also need to build up a list of any additional shared object
20740Sstevel@tonic-gate  * dependencies this object may have.  In this case save any NEEDED entries
20750Sstevel@tonic-gate  * together with any associated run-path specifications.  This information is
20760Sstevel@tonic-gate  * recorded on the `ofl_soneed' list and will be analyzed after all explicit
20770Sstevel@tonic-gate  * file processing has been completed (refer finish_libs()).
20780Sstevel@tonic-gate  */
20791618Srie static uintptr_t
20800Sstevel@tonic-gate process_dynamic(Is_desc *isc, Ifl_desc *ifl, Ofl_desc *ofl)
20810Sstevel@tonic-gate {
20820Sstevel@tonic-gate 	Dyn		*data, *dyn;
20830Sstevel@tonic-gate 	char		*str, *rpath = NULL;
20840Sstevel@tonic-gate 	const char	*soname, *needed;
20850Sstevel@tonic-gate 
20860Sstevel@tonic-gate 	data = (Dyn *)isc->is_indata->d_buf;
20870Sstevel@tonic-gate 	str = (char *)ifl->ifl_isdesc[isc->is_shdr->sh_link]->is_indata->d_buf;
20880Sstevel@tonic-gate 
20890Sstevel@tonic-gate 	/*
20900Sstevel@tonic-gate 	 * First loop through the dynamic section looking for a run path.
20910Sstevel@tonic-gate 	 */
20920Sstevel@tonic-gate 	if (ofl->ofl_flags & (FLG_OF_NOUNDEF | FLG_OF_SYMBOLIC)) {
20930Sstevel@tonic-gate 		for (dyn = data; dyn->d_tag != DT_NULL; dyn++) {
20940Sstevel@tonic-gate 			if ((dyn->d_tag != DT_RPATH) &&
20950Sstevel@tonic-gate 			    (dyn->d_tag != DT_RUNPATH))
20960Sstevel@tonic-gate 				continue;
20970Sstevel@tonic-gate 			if ((rpath = str + (size_t)dyn->d_un.d_val) == NULL)
20980Sstevel@tonic-gate 				continue;
20990Sstevel@tonic-gate 			break;
21000Sstevel@tonic-gate 		}
21010Sstevel@tonic-gate 	}
21020Sstevel@tonic-gate 
21030Sstevel@tonic-gate 	/*
21040Sstevel@tonic-gate 	 * Now look for any needed dependencies (which may use the rpath)
21050Sstevel@tonic-gate 	 * or a new SONAME.
21060Sstevel@tonic-gate 	 */
21070Sstevel@tonic-gate 	for (dyn = data; dyn->d_tag != DT_NULL; dyn++) {
21080Sstevel@tonic-gate 		if (dyn->d_tag == DT_SONAME) {
21090Sstevel@tonic-gate 			if ((soname = str + (size_t)dyn->d_un.d_val) == NULL)
21100Sstevel@tonic-gate 				continue;
21110Sstevel@tonic-gate 
21120Sstevel@tonic-gate 			/*
21130Sstevel@tonic-gate 			 * Update the input file structure with this new name.
21140Sstevel@tonic-gate 			 */
21150Sstevel@tonic-gate 			ifl->ifl_soname = soname;
21160Sstevel@tonic-gate 
21170Sstevel@tonic-gate 		} else if ((dyn->d_tag == DT_NEEDED) ||
21180Sstevel@tonic-gate 		    (dyn->d_tag == DT_USED)) {
21199131SRod.Evans@Sun.COM 			Sdf_desc	*sdf;
21209131SRod.Evans@Sun.COM 
21210Sstevel@tonic-gate 			if (!(ofl->ofl_flags &
21220Sstevel@tonic-gate 			    (FLG_OF_NOUNDEF | FLG_OF_SYMBOLIC)))
21230Sstevel@tonic-gate 				continue;
21240Sstevel@tonic-gate 			if ((needed = str + (size_t)dyn->d_un.d_val) == NULL)
21250Sstevel@tonic-gate 				continue;
21260Sstevel@tonic-gate 
21270Sstevel@tonic-gate 			/*
21280Sstevel@tonic-gate 			 * Determine if this needed entry is already recorded on
21290Sstevel@tonic-gate 			 * the shared object needed list, if not create a new
21300Sstevel@tonic-gate 			 * definition for later processing (see finish_libs()).
21310Sstevel@tonic-gate 			 */
21329131SRod.Evans@Sun.COM 			needed = expand(ifl->ifl_name, needed, NULL);
21330Sstevel@tonic-gate 
21349131SRod.Evans@Sun.COM 			if ((sdf = sdf_find(needed, ofl->ofl_soneed)) == NULL) {
21350Sstevel@tonic-gate 				if ((sdf = sdf_add(needed,
21360Sstevel@tonic-gate 				    &ofl->ofl_soneed)) == (Sdf_desc *)S_ERROR)
21370Sstevel@tonic-gate 					return (S_ERROR);
21380Sstevel@tonic-gate 				sdf->sdf_rfile = ifl->ifl_name;
21390Sstevel@tonic-gate 			}
21400Sstevel@tonic-gate 
21410Sstevel@tonic-gate 			/*
21420Sstevel@tonic-gate 			 * Record the runpath (Note that we take the first
21430Sstevel@tonic-gate 			 * runpath which is exactly what ld.so.1 would do during
21440Sstevel@tonic-gate 			 * its dependency processing).
21450Sstevel@tonic-gate 			 */
214610792SRod.Evans@Sun.COM 			if (rpath && (sdf->sdf_rpath == NULL))
21470Sstevel@tonic-gate 				sdf->sdf_rpath = rpath;
21480Sstevel@tonic-gate 
21490Sstevel@tonic-gate 		} else if (dyn->d_tag == DT_FLAGS_1) {
21500Sstevel@tonic-gate 			if (dyn->d_un.d_val & (DF_1_INITFIRST | DF_1_INTERPOSE))
21510Sstevel@tonic-gate 				ifl->ifl_flags &= ~FLG_IF_LAZYLD;
21520Sstevel@tonic-gate 			if (dyn->d_un.d_val & DF_1_DISPRELPND)
21530Sstevel@tonic-gate 				ifl->ifl_flags |= FLG_IF_DISPPEND;
21540Sstevel@tonic-gate 			if (dyn->d_un.d_val & DF_1_DISPRELDNE)
21550Sstevel@tonic-gate 				ifl->ifl_flags |= FLG_IF_DISPDONE;
21560Sstevel@tonic-gate 			if (dyn->d_un.d_val & DF_1_NODIRECT)
21570Sstevel@tonic-gate 				ifl->ifl_flags |= FLG_IF_NODIRECT;
21580Sstevel@tonic-gate 
21590Sstevel@tonic-gate 		} else if ((dyn->d_tag == DT_AUDIT) &&
21600Sstevel@tonic-gate 		    (ifl->ifl_flags & FLG_IF_NEEDED)) {
21610Sstevel@tonic-gate 			/*
21620Sstevel@tonic-gate 			 * Record audit string as DT_DEPAUDIT.
21630Sstevel@tonic-gate 			 */
21640Sstevel@tonic-gate 			if ((ofl->ofl_depaudit = add_string(ofl->ofl_depaudit,
21650Sstevel@tonic-gate 			    (str + (size_t)dyn->d_un.d_val))) ==
21660Sstevel@tonic-gate 			    (const char *)S_ERROR)
21670Sstevel@tonic-gate 				return (S_ERROR);
21680Sstevel@tonic-gate 
21690Sstevel@tonic-gate 		} else if (dyn->d_tag == DT_SUNW_RTLDINF) {
21700Sstevel@tonic-gate 			/*
21710Sstevel@tonic-gate 			 * If a library has the SUNW_RTLDINF .dynamic entry
21720Sstevel@tonic-gate 			 * then we must not permit lazyloading of this library.
21730Sstevel@tonic-gate 			 * This is because critical startup information (TLS
21740Sstevel@tonic-gate 			 * routines) are provided as part of these interfaces
21750Sstevel@tonic-gate 			 * and we must have them as part of process startup.
21760Sstevel@tonic-gate 			 */
21770Sstevel@tonic-gate 			ifl->ifl_flags &= ~FLG_IF_LAZYLD;
21780Sstevel@tonic-gate 		}
21790Sstevel@tonic-gate 	}
21800Sstevel@tonic-gate 
21810Sstevel@tonic-gate 	/*
21820Sstevel@tonic-gate 	 * Perform some SONAME sanity checks.
21830Sstevel@tonic-gate 	 */
21840Sstevel@tonic-gate 	if (ifl->ifl_flags & FLG_IF_NEEDED) {
21857463SRod.Evans@Sun.COM 		Ifl_desc	*sifl;
21869131SRod.Evans@Sun.COM 		Aliste		idx;
21870Sstevel@tonic-gate 
21880Sstevel@tonic-gate 		/*
21890Sstevel@tonic-gate 		 * Determine if anyone else will cause the same SONAME to be
21900Sstevel@tonic-gate 		 * used (this is either caused by two different files having the
21910Sstevel@tonic-gate 		 * same SONAME, or by one file SONAME actually matching another
21920Sstevel@tonic-gate 		 * file basename (if no SONAME is specified within a shared
21930Sstevel@tonic-gate 		 * library its basename will be used)). Probably rare, but some
21940Sstevel@tonic-gate 		 * idiot will do it.
21950Sstevel@tonic-gate 		 */
21969131SRod.Evans@Sun.COM 		for (APLIST_TRAVERSE(ofl->ofl_sos, idx, sifl)) {
21970Sstevel@tonic-gate 			if ((strcmp(ifl->ifl_soname, sifl->ifl_soname) == 0) &&
21980Sstevel@tonic-gate 			    (ifl != sifl)) {
21990Sstevel@tonic-gate 				const char	*hint, *iflb, *siflb;
22000Sstevel@tonic-gate 
22010Sstevel@tonic-gate 				/*
22020Sstevel@tonic-gate 				 * Determine the basename of each file. Perhaps
22030Sstevel@tonic-gate 				 * there are multiple copies of the same file
22040Sstevel@tonic-gate 				 * being brought in using different -L search
22050Sstevel@tonic-gate 				 * paths, and if so give an extra hint in the
22060Sstevel@tonic-gate 				 * error message.
22070Sstevel@tonic-gate 				 */
22080Sstevel@tonic-gate 				iflb = strrchr(ifl->ifl_name, '/');
22090Sstevel@tonic-gate 				if (iflb == NULL)
22100Sstevel@tonic-gate 					iflb = ifl->ifl_name;
22110Sstevel@tonic-gate 				else
22120Sstevel@tonic-gate 					iflb++;
22130Sstevel@tonic-gate 
22140Sstevel@tonic-gate 				siflb = strrchr(sifl->ifl_name, '/');
22150Sstevel@tonic-gate 				if (siflb == NULL)
22160Sstevel@tonic-gate 					siflb = sifl->ifl_name;
22170Sstevel@tonic-gate 				else
22180Sstevel@tonic-gate 					siflb++;
22190Sstevel@tonic-gate 
22200Sstevel@tonic-gate 				if (strcmp(iflb, siflb) == 0)
22210Sstevel@tonic-gate 					hint = MSG_INTL(MSG_REC_CNFLTHINT);
22220Sstevel@tonic-gate 				else
22230Sstevel@tonic-gate 					hint = MSG_ORIG(MSG_STR_EMPTY);
22240Sstevel@tonic-gate 
22251618Srie 				eprintf(ofl->ofl_lml, ERR_FATAL,
22261618Srie 				    MSG_INTL(MSG_REC_OBJCNFLT), sifl->ifl_name,
22271618Srie 				    ifl->ifl_name, sifl->ifl_soname, hint);
22280Sstevel@tonic-gate 				ofl->ofl_flags |= FLG_OF_FATAL;
22290Sstevel@tonic-gate 				return (0);
22300Sstevel@tonic-gate 			}
22310Sstevel@tonic-gate 		}
22320Sstevel@tonic-gate 
22330Sstevel@tonic-gate 		/*
22340Sstevel@tonic-gate 		 * If the SONAME is the same as the name the user wishes to
22350Sstevel@tonic-gate 		 * record when building a dynamic library (refer -h option),
22360Sstevel@tonic-gate 		 * we also have a name clash.
22370Sstevel@tonic-gate 		 */
22380Sstevel@tonic-gate 		if (ofl->ofl_soname &&
22390Sstevel@tonic-gate 		    (strcmp(ofl->ofl_soname, ifl->ifl_soname) == 0)) {
22401618Srie 			eprintf(ofl->ofl_lml, ERR_FATAL,
22411618Srie 			    MSG_INTL(MSG_REC_OPTCNFLT), ifl->ifl_name,
22427983SAli.Bahrami@Sun.COM 			    MSG_INTL(MSG_MARG_SONAME), ifl->ifl_soname);
22430Sstevel@tonic-gate 			ofl->ofl_flags |= FLG_OF_FATAL;
22440Sstevel@tonic-gate 			return (0);
22450Sstevel@tonic-gate 		}
22460Sstevel@tonic-gate 	}
22470Sstevel@tonic-gate 	return (1);
22480Sstevel@tonic-gate }
22490Sstevel@tonic-gate 
22500Sstevel@tonic-gate /*
22519085SAli.Bahrami@Sun.COM  * Process a progbits section from a relocatable object (ET_REL).
22529085SAli.Bahrami@Sun.COM  * This is used on non-amd64 objects to recognize .eh_frame sections.
22539085SAli.Bahrami@Sun.COM  */
22549085SAli.Bahrami@Sun.COM /*ARGSUSED1*/
22559085SAli.Bahrami@Sun.COM static uintptr_t
22569085SAli.Bahrami@Sun.COM process_progbits_final(Is_desc *isc, Ifl_desc *ifl, Ofl_desc *ofl)
22579085SAli.Bahrami@Sun.COM {
22589085SAli.Bahrami@Sun.COM 	if (isc->is_osdesc && (isc->is_flags & FLG_IS_EHFRAME) &&
22599085SAli.Bahrami@Sun.COM 	    (ld_unwind_register(isc->is_osdesc, ofl) == S_ERROR))
22609085SAli.Bahrami@Sun.COM 		return (S_ERROR);
22619085SAli.Bahrami@Sun.COM 
22629085SAli.Bahrami@Sun.COM 	return (1);
22639085SAli.Bahrami@Sun.COM }
22649085SAli.Bahrami@Sun.COM 
22659085SAli.Bahrami@Sun.COM /*
22667463SRod.Evans@Sun.COM  * Process a group section.
22677463SRod.Evans@Sun.COM  */
22687463SRod.Evans@Sun.COM static uintptr_t
22697463SRod.Evans@Sun.COM process_group(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
22707463SRod.Evans@Sun.COM     Word ndx, int ident, Ofl_desc *ofl)
22717463SRod.Evans@Sun.COM {
22727463SRod.Evans@Sun.COM 	uintptr_t	error;
22737463SRod.Evans@Sun.COM 
22747463SRod.Evans@Sun.COM 	error = process_section(name, ifl, shdr, scn, ndx, ident, ofl);
22757463SRod.Evans@Sun.COM 	if ((error == 0) || (error == S_ERROR))
22767463SRod.Evans@Sun.COM 		return (error);
22777463SRod.Evans@Sun.COM 
22787463SRod.Evans@Sun.COM 	/*
22797463SRod.Evans@Sun.COM 	 * Indicate that this input file has groups to process.  Groups are
22807463SRod.Evans@Sun.COM 	 * processed after all input sections have been processed.
22817463SRod.Evans@Sun.COM 	 */
22827463SRod.Evans@Sun.COM 	ifl->ifl_flags |= FLG_IS_GROUPS;
22837463SRod.Evans@Sun.COM 
22847463SRod.Evans@Sun.COM 	return (1);
22857463SRod.Evans@Sun.COM }
22867463SRod.Evans@Sun.COM 
22877463SRod.Evans@Sun.COM /*
22880Sstevel@tonic-gate  * Process a relocation entry. At this point all input sections from this
22890Sstevel@tonic-gate  * input file have been assigned an input section descriptor which is saved
22900Sstevel@tonic-gate  * in the `ifl_isdesc' array.
22910Sstevel@tonic-gate  */
22921618Srie static uintptr_t
22930Sstevel@tonic-gate rel_process(Is_desc *isc, Ifl_desc *ifl, Ofl_desc *ofl)
22940Sstevel@tonic-gate {
22950Sstevel@tonic-gate 	Word 	rndx;
22960Sstevel@tonic-gate 	Is_desc	*risc;
22970Sstevel@tonic-gate 	Os_desc	*osp;
22980Sstevel@tonic-gate 	Shdr	*shdr = isc->is_shdr;
22994734Sab196087 	Conv_inv_buf_t inv_buf;
23000Sstevel@tonic-gate 
23010Sstevel@tonic-gate 	/*
23020Sstevel@tonic-gate 	 * Make sure this is a valid relocation we can handle.
23030Sstevel@tonic-gate 	 */
23046206Sab196087 	if (shdr->sh_type != ld_targ.t_m.m_rel_sht_type) {
23051618Srie 		eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_FIL_INVALSEC),
23069878SAli.Bahrami@Sun.COM 		    ifl->ifl_name, EC_WORD(isc->is_scnndx), isc->is_name,
23079273SAli.Bahrami@Sun.COM 		    conv_sec_type(ifl->ifl_ehdr->e_ident[EI_OSABI],
23089273SAli.Bahrami@Sun.COM 		    ifl->ifl_ehdr->e_machine, shdr->sh_type, 0, &inv_buf));
23090Sstevel@tonic-gate 		ofl->ofl_flags |= FLG_OF_FATAL;
23100Sstevel@tonic-gate 		return (0);
23110Sstevel@tonic-gate 	}
23120Sstevel@tonic-gate 
23130Sstevel@tonic-gate 	/*
23140Sstevel@tonic-gate 	 * From the relocation section header information determine which
23150Sstevel@tonic-gate 	 * section needs the actual relocation.  Determine which output section
23160Sstevel@tonic-gate 	 * this input section has been assigned to and add to its relocation
23170Sstevel@tonic-gate 	 * list.  Note that the relocation section may be null if it is not
23180Sstevel@tonic-gate 	 * required (ie. .debug, .stabs, etc).
23190Sstevel@tonic-gate 	 */
23200Sstevel@tonic-gate 	rndx = shdr->sh_info;
23210Sstevel@tonic-gate 	if (rndx >= ifl->ifl_shnum) {
23220Sstevel@tonic-gate 		/*
23230Sstevel@tonic-gate 		 * Broken input file.
23240Sstevel@tonic-gate 		 */
23251618Srie 		eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_FIL_INVSHINFO),
23269878SAli.Bahrami@Sun.COM 		    ifl->ifl_name, EC_WORD(isc->is_scnndx), isc->is_name,
23279878SAli.Bahrami@Sun.COM 		    EC_XWORD(rndx));
23280Sstevel@tonic-gate 		ofl->ofl_flags |= FLG_OF_FATAL;
23290Sstevel@tonic-gate 		return (0);
23300Sstevel@tonic-gate 	}
23310Sstevel@tonic-gate 	if (rndx == 0) {
23329131SRod.Evans@Sun.COM 		if (aplist_append(&ofl->ofl_extrarels, isc,
23339131SRod.Evans@Sun.COM 		    AL_CNT_OFL_RELS) == NULL)
23340Sstevel@tonic-gate 			return (S_ERROR);
23359131SRod.Evans@Sun.COM 
23369131SRod.Evans@Sun.COM 	} else if ((risc = ifl->ifl_isdesc[rndx]) != NULL) {
23370Sstevel@tonic-gate 		/*
23380Sstevel@tonic-gate 		 * Discard relocations if they are against a section
23390Sstevel@tonic-gate 		 * which has been discarded.
23400Sstevel@tonic-gate 		 */
23410Sstevel@tonic-gate 		if (risc->is_flags & FLG_IS_DISCARD)
23420Sstevel@tonic-gate 			return (1);
23439131SRod.Evans@Sun.COM 
23449131SRod.Evans@Sun.COM 		if ((osp = risc->is_osdesc) == NULL) {
23450Sstevel@tonic-gate 			if (risc->is_shdr->sh_type == SHT_SUNW_move) {
23460Sstevel@tonic-gate 				/*
23479131SRod.Evans@Sun.COM 				 * This section is processed later in
23489131SRod.Evans@Sun.COM 				 * process_movereloc().
23490Sstevel@tonic-gate 				 */
23509131SRod.Evans@Sun.COM 				if (aplist_append(&ofl->ofl_ismoverel,
23519131SRod.Evans@Sun.COM 				    isc, AL_CNT_OFL_MOVE) == NULL)
23520Sstevel@tonic-gate 					return (S_ERROR);
23530Sstevel@tonic-gate 				return (1);
23540Sstevel@tonic-gate 			}
23551618Srie 			eprintf(ofl->ofl_lml, ERR_FATAL,
23561618Srie 			    MSG_INTL(MSG_FIL_INVRELOC1), ifl->ifl_name,
23579878SAli.Bahrami@Sun.COM 			    EC_WORD(isc->is_scnndx), isc->is_name,
23589878SAli.Bahrami@Sun.COM 			    EC_WORD(risc->is_scnndx), risc->is_name);
23590Sstevel@tonic-gate 			return (0);
23600Sstevel@tonic-gate 		}
23618608SAli.Bahrami@Sun.COM 		if (aplist_append(&osp->os_relisdescs, isc,
23628608SAli.Bahrami@Sun.COM 		    AL_CNT_OS_RELISDESCS) == NULL)
23630Sstevel@tonic-gate 			return (S_ERROR);
23640Sstevel@tonic-gate 	}
23650Sstevel@tonic-gate 	return (1);
23660Sstevel@tonic-gate }
23670Sstevel@tonic-gate 
23680Sstevel@tonic-gate /*
23690Sstevel@tonic-gate  * SHF_EXCLUDE flags is set for this section.
23700Sstevel@tonic-gate  */
23710Sstevel@tonic-gate static uintptr_t
23720Sstevel@tonic-gate process_exclude(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
23730Sstevel@tonic-gate     Word ndx, Ofl_desc *ofl)
23740Sstevel@tonic-gate {
23750Sstevel@tonic-gate 	/*
23760Sstevel@tonic-gate 	 * Sections SHT_SYMTAB and SHT_DYNDYM, even if SHF_EXCLUDE is on, might
23770Sstevel@tonic-gate 	 * be needed for ld processing.  These sections need to be in the
23780Sstevel@tonic-gate 	 * internal table.  Later it will be determined whether they can be
23790Sstevel@tonic-gate 	 * eliminated or not.
23800Sstevel@tonic-gate 	 */
23810Sstevel@tonic-gate 	if (shdr->sh_type == SHT_SYMTAB || shdr->sh_type == SHT_DYNSYM)
23820Sstevel@tonic-gate 		return (0);
23830Sstevel@tonic-gate 
23840Sstevel@tonic-gate 	/*
23850Sstevel@tonic-gate 	 * Other checks
23860Sstevel@tonic-gate 	 */
23870Sstevel@tonic-gate 	if (shdr->sh_flags & SHF_ALLOC) {
23880Sstevel@tonic-gate 		/*
23890Sstevel@tonic-gate 		 * A conflict, issue an warning message, and ignore the section.
23900Sstevel@tonic-gate 		 */
23911618Srie 		eprintf(ofl->ofl_lml, ERR_WARNING, MSG_INTL(MSG_FIL_EXCLUDE),
23929878SAli.Bahrami@Sun.COM 		    ifl->ifl_name, EC_WORD(ndx), name);
23930Sstevel@tonic-gate 		return (0);
23940Sstevel@tonic-gate 	}
23950Sstevel@tonic-gate 
23960Sstevel@tonic-gate 	/*
23970Sstevel@tonic-gate 	 * This sections is not going to the output file.
23980Sstevel@tonic-gate 	 */
23990Sstevel@tonic-gate 	return (process_section(name, ifl, shdr, scn, ndx, 0, ofl));
24000Sstevel@tonic-gate }
24010Sstevel@tonic-gate 
24020Sstevel@tonic-gate /*
24030Sstevel@tonic-gate  * Section processing state table.  `Initial' describes the required initial
24040Sstevel@tonic-gate  * procedure to be called (if any), `Final' describes the final processing
24050Sstevel@tonic-gate  * procedure (ie. things that can only be done when all required sections
24060Sstevel@tonic-gate  * have been collected).
24070Sstevel@tonic-gate  */
24089085SAli.Bahrami@Sun.COM typedef uintptr_t	(* initial_func_t)(const char *, Ifl_desc *, Shdr *,
24099085SAli.Bahrami@Sun.COM 			    Elf_Scn *, Word, int, Ofl_desc *);
24100Sstevel@tonic-gate 
24119085SAli.Bahrami@Sun.COM static initial_func_t Initial[SHT_NUM][2] = {
24120Sstevel@tonic-gate /*			ET_REL			ET_DYN			*/
24130Sstevel@tonic-gate 
24140Sstevel@tonic-gate /* SHT_NULL	*/	invalid_section,	invalid_section,
24150Sstevel@tonic-gate /* SHT_PROGBITS	*/	process_progbits,	process_progbits,
24160Sstevel@tonic-gate /* SHT_SYMTAB	*/	process_input,		process_input,
24170Sstevel@tonic-gate /* SHT_STRTAB	*/	process_strtab,		process_strtab,
24180Sstevel@tonic-gate /* SHT_RELA	*/	process_reloc,		process_reloc,
24190Sstevel@tonic-gate /* SHT_HASH	*/	invalid_section,	NULL,
24204716Sab196087 /* SHT_DYNAMIC	*/	process_rel_dynamic,	process_dynamic_isgnu,
24210Sstevel@tonic-gate /* SHT_NOTE	*/	process_section,	NULL,
24220Sstevel@tonic-gate /* SHT_NOBITS	*/	process_nobits,		process_nobits,
24230Sstevel@tonic-gate /* SHT_REL	*/	process_reloc,		process_reloc,
24240Sstevel@tonic-gate /* SHT_SHLIB	*/	process_section,	invalid_section,
24250Sstevel@tonic-gate /* SHT_DYNSYM	*/	invalid_section,	process_input,
24260Sstevel@tonic-gate /* SHT_UNKNOWN12 */	process_progbits,	process_progbits,
24270Sstevel@tonic-gate /* SHT_UNKNOWN13 */	process_progbits,	process_progbits,
24280Sstevel@tonic-gate /* SHT_INIT_ARRAY */	process_array,		NULL,
24290Sstevel@tonic-gate /* SHT_FINI_ARRAY */	process_array,		NULL,
24300Sstevel@tonic-gate /* SHT_PREINIT_ARRAY */	process_array,		NULL,
24317463SRod.Evans@Sun.COM /* SHT_GROUP */		process_group,		invalid_section,
24320Sstevel@tonic-gate /* SHT_SYMTAB_SHNDX */	process_sym_shndx,	NULL
24330Sstevel@tonic-gate };
24340Sstevel@tonic-gate 
24359085SAli.Bahrami@Sun.COM typedef uintptr_t	(* final_func_t)(Is_desc *, Ifl_desc *, Ofl_desc *);
24369085SAli.Bahrami@Sun.COM 
24379085SAli.Bahrami@Sun.COM static final_func_t Final[SHT_NUM][2] = {
24389085SAli.Bahrami@Sun.COM /*			ET_REL			ET_DYN			*/
24390Sstevel@tonic-gate 
24400Sstevel@tonic-gate /* SHT_NULL	*/	NULL,			NULL,
24419085SAli.Bahrami@Sun.COM /* SHT_PROGBITS	*/	process_progbits_final,	NULL,
24421618Srie /* SHT_SYMTAB	*/	ld_sym_process,		ld_sym_process,
24430Sstevel@tonic-gate /* SHT_STRTAB	*/	NULL,			NULL,
24440Sstevel@tonic-gate /* SHT_RELA	*/	rel_process,		NULL,
24450Sstevel@tonic-gate /* SHT_HASH	*/	NULL,			NULL,
24460Sstevel@tonic-gate /* SHT_DYNAMIC	*/	NULL,			process_dynamic,
24470Sstevel@tonic-gate /* SHT_NOTE	*/	NULL,			NULL,
24480Sstevel@tonic-gate /* SHT_NOBITS	*/	NULL,			NULL,
24490Sstevel@tonic-gate /* SHT_REL	*/	rel_process,		NULL,
24500Sstevel@tonic-gate /* SHT_SHLIB	*/	NULL,			NULL,
24511618Srie /* SHT_DYNSYM	*/	NULL,			ld_sym_process,
24520Sstevel@tonic-gate /* SHT_UNKNOWN12 */	NULL,			NULL,
24530Sstevel@tonic-gate /* SHT_UNKNOWN13 */	NULL,			NULL,
24547463SRod.Evans@Sun.COM /* SHT_INIT_ARRAY */	array_process,		NULL,
24557463SRod.Evans@Sun.COM /* SHT_FINI_ARRAY */	array_process,		NULL,
24567463SRod.Evans@Sun.COM /* SHT_PREINIT_ARRAY */	array_process,		NULL,
24570Sstevel@tonic-gate /* SHT_GROUP */		NULL,			NULL,
24580Sstevel@tonic-gate /* SHT_SYMTAB_SHNDX */	sym_shndx_process,	NULL
24590Sstevel@tonic-gate };
24600Sstevel@tonic-gate 
24610Sstevel@tonic-gate #define	MAXNDXSIZE	10
24620Sstevel@tonic-gate 
24630Sstevel@tonic-gate /*
24640Sstevel@tonic-gate  * Process an elf file.  Each section is compared against the section state
24650Sstevel@tonic-gate  * table to determine whether it should be processed (saved), ignored, or
24660Sstevel@tonic-gate  * is invalid for the type of input file being processed.
24670Sstevel@tonic-gate  */
24681618Srie static uintptr_t
24690Sstevel@tonic-gate process_elf(Ifl_desc *ifl, Elf *elf, Ofl_desc *ofl)
24700Sstevel@tonic-gate {
24710Sstevel@tonic-gate 	Elf_Scn		*scn;
24720Sstevel@tonic-gate 	Shdr		*shdr;
24737463SRod.Evans@Sun.COM 	Word		ndx, sndx, ordndx = 0, ordcnt = 0;
24749878SAli.Bahrami@Sun.COM 	char		*str, *name;
24750Sstevel@tonic-gate 	Word		row, column;
24760Sstevel@tonic-gate 	int		ident;
24770Sstevel@tonic-gate 	uintptr_t	error;
2478*11827SRod.Evans@Sun.COM 	Is_desc		*vdfisp, *vndisp, *vsyisp, *sifisp;
2479*11827SRod.Evans@Sun.COM 	Is_desc		*capinfoisp, *capisp;
24800Sstevel@tonic-gate 	Sdf_desc	*sdf;
248111734SAli.Bahrami@Sun.COM 	Place_path_info	path_info_buf, *path_info;
248211734SAli.Bahrami@Sun.COM 
248311734SAli.Bahrami@Sun.COM 	/*
248411734SAli.Bahrami@Sun.COM 	 * Path information buffer used by ld_place_section() and related
248511734SAli.Bahrami@Sun.COM 	 * routines. This information is used to evaluate entrance criteria
248611734SAli.Bahrami@Sun.COM 	 * with non-empty file matching lists (ec_files).
248711734SAli.Bahrami@Sun.COM 	 */
248811734SAli.Bahrami@Sun.COM 	path_info = ld_place_path_info_init(ofl, ifl, &path_info_buf);
24890Sstevel@tonic-gate 
24900Sstevel@tonic-gate 	/*
24910Sstevel@tonic-gate 	 * First process the .shstrtab section so that later sections can
24920Sstevel@tonic-gate 	 * reference their name.
24930Sstevel@tonic-gate 	 */
24941618Srie 	ld_sup_file(ofl, ifl->ifl_name, elf_kind(elf), ifl->ifl_flags, elf);
24950Sstevel@tonic-gate 
24960Sstevel@tonic-gate 	sndx = ifl->ifl_shstrndx;
24970Sstevel@tonic-gate 	if ((scn = elf_getscn(elf, (size_t)sndx)) == NULL) {
24981618Srie 		eprintf(ofl->ofl_lml, ERR_ELF, MSG_INTL(MSG_ELF_GETSCN),
24991618Srie 		    ifl->ifl_name);
25000Sstevel@tonic-gate 		ofl->ofl_flags |= FLG_OF_FATAL;
25010Sstevel@tonic-gate 		return (0);
25020Sstevel@tonic-gate 	}
25030Sstevel@tonic-gate 	if ((shdr = elf_getshdr(scn)) == NULL) {
25041618Srie 		eprintf(ofl->ofl_lml, ERR_ELF, MSG_INTL(MSG_ELF_GETSHDR),
25051618Srie 		    ifl->ifl_name);
25060Sstevel@tonic-gate 		ofl->ofl_flags |= FLG_OF_FATAL;
25070Sstevel@tonic-gate 		return (0);
25080Sstevel@tonic-gate 	}
25090Sstevel@tonic-gate 	if ((name = elf_strptr(elf, (size_t)sndx, (size_t)shdr->sh_name)) ==
25100Sstevel@tonic-gate 	    NULL) {
25111618Srie 		eprintf(ofl->ofl_lml, ERR_ELF, MSG_INTL(MSG_ELF_STRPTR),
25121618Srie 		    ifl->ifl_name);
25130Sstevel@tonic-gate 		ofl->ofl_flags |= FLG_OF_FATAL;
25140Sstevel@tonic-gate 		return (0);
25150Sstevel@tonic-gate 	}
25160Sstevel@tonic-gate 
25172647Srie 	if (ld_sup_input_section(ofl, ifl, name, &shdr, sndx, scn,
25182647Srie 	    elf) == S_ERROR)
25190Sstevel@tonic-gate 		return (S_ERROR);
25200Sstevel@tonic-gate 
25210Sstevel@tonic-gate 	/*
25220Sstevel@tonic-gate 	 * Reset the name since the shdr->sh_name could have been changed as
25239878SAli.Bahrami@Sun.COM 	 * part of ld_sup_input_section().
25240Sstevel@tonic-gate 	 */
25259878SAli.Bahrami@Sun.COM 	if ((name = elf_strptr(elf, (size_t)sndx, (size_t)shdr->sh_name)) ==
25269878SAli.Bahrami@Sun.COM 	    NULL) {
25271618Srie 		eprintf(ofl->ofl_lml, ERR_ELF, MSG_INTL(MSG_ELF_STRPTR),
25281618Srie 		    ifl->ifl_name);
25290Sstevel@tonic-gate 		ofl->ofl_flags |= FLG_OF_FATAL;
25300Sstevel@tonic-gate 		return (0);
25310Sstevel@tonic-gate 	}
25320Sstevel@tonic-gate 
25330Sstevel@tonic-gate 	error = process_strtab(name, ifl, shdr, scn, sndx, FALSE, ofl);
25340Sstevel@tonic-gate 	if ((error == 0) || (error == S_ERROR))
25350Sstevel@tonic-gate 		return (error);
25360Sstevel@tonic-gate 	str = ifl->ifl_isdesc[sndx]->is_indata->d_buf;
25370Sstevel@tonic-gate 
25380Sstevel@tonic-gate 	/*
25390Sstevel@tonic-gate 	 * Determine the state table column from the input file type.  Note,
25400Sstevel@tonic-gate 	 * shared library sections are not added to the output section list.
25410Sstevel@tonic-gate 	 */
25420Sstevel@tonic-gate 	if (ifl->ifl_ehdr->e_type == ET_DYN) {
25430Sstevel@tonic-gate 		column = 1;
25440Sstevel@tonic-gate 		ofl->ofl_soscnt++;
25456206Sab196087 		ident = ld_targ.t_id.id_null;
25460Sstevel@tonic-gate 	} else {
25470Sstevel@tonic-gate 		column = 0;
25480Sstevel@tonic-gate 		ofl->ofl_objscnt++;
25496206Sab196087 		ident = ld_targ.t_id.id_unknown;
25500Sstevel@tonic-gate 	}
25510Sstevel@tonic-gate 
25521618Srie 	DBG_CALL(Dbg_file_generic(ofl->ofl_lml, ifl));
25530Sstevel@tonic-gate 	ndx = 0;
2554*11827SRod.Evans@Sun.COM 	vdfisp = vndisp = vsyisp = sifisp = capinfoisp = capisp = NULL;
25550Sstevel@tonic-gate 	scn = NULL;
25560Sstevel@tonic-gate 	while (scn = elf_nextscn(elf, scn)) {
25570Sstevel@tonic-gate 		ndx++;
25587463SRod.Evans@Sun.COM 
25590Sstevel@tonic-gate 		/*
25600Sstevel@tonic-gate 		 * As we've already processed the .shstrtab don't do it again.
25610Sstevel@tonic-gate 		 */
25620Sstevel@tonic-gate 		if (ndx == sndx)
25630Sstevel@tonic-gate 			continue;
25640Sstevel@tonic-gate 
25650Sstevel@tonic-gate 		if ((shdr = elf_getshdr(scn)) == NULL) {
25661618Srie 			eprintf(ofl->ofl_lml, ERR_ELF,
25671618Srie 			    MSG_INTL(MSG_ELF_GETSHDR), ifl->ifl_name);
25680Sstevel@tonic-gate 			ofl->ofl_flags |= FLG_OF_FATAL;
25690Sstevel@tonic-gate 			return (0);
25700Sstevel@tonic-gate 		}
25710Sstevel@tonic-gate 		name = str + (size_t)(shdr->sh_name);
25720Sstevel@tonic-gate 
25732647Srie 		if (ld_sup_input_section(ofl, ifl, name, &shdr, ndx, scn,
25742647Srie 		    elf) == S_ERROR)
25750Sstevel@tonic-gate 			return (S_ERROR);
25760Sstevel@tonic-gate 
25770Sstevel@tonic-gate 		/*
25780Sstevel@tonic-gate 		 * Reset the name since the shdr->sh_name could have been
25799878SAli.Bahrami@Sun.COM 		 * changed as part of ld_sup_input_section().
25800Sstevel@tonic-gate 		 */
25819878SAli.Bahrami@Sun.COM 		name = str + (size_t)(shdr->sh_name);
25820Sstevel@tonic-gate 
25830Sstevel@tonic-gate 		row = shdr->sh_type;
25840Sstevel@tonic-gate 
25850Sstevel@tonic-gate 		/*
25860Sstevel@tonic-gate 		 * If the section has the SHF_EXCLUDE flag on, and we're not
25870Sstevel@tonic-gate 		 * generating a relocatable object, exclude the section.
25880Sstevel@tonic-gate 		 */
25890Sstevel@tonic-gate 		if (((shdr->sh_flags & SHF_EXCLUDE) != 0) &&
25900Sstevel@tonic-gate 		    ((ofl->ofl_flags & FLG_OF_RELOBJ) == 0)) {
25910Sstevel@tonic-gate 			if ((error = process_exclude(name, ifl, shdr, scn,
25920Sstevel@tonic-gate 			    ndx, ofl)) == S_ERROR)
25930Sstevel@tonic-gate 				return (S_ERROR);
25940Sstevel@tonic-gate 			if (error == 1)
25950Sstevel@tonic-gate 				continue;
25960Sstevel@tonic-gate 		}
25970Sstevel@tonic-gate 
25980Sstevel@tonic-gate 		/*
25990Sstevel@tonic-gate 		 * If this is a standard section type process it via the
26000Sstevel@tonic-gate 		 * appropriate action routine.
26010Sstevel@tonic-gate 		 */
26020Sstevel@tonic-gate 		if (row < SHT_NUM) {
26030Sstevel@tonic-gate 			if (Initial[row][column] != NULL) {
26040Sstevel@tonic-gate 				if (Initial[row][column](name, ifl, shdr, scn,
26050Sstevel@tonic-gate 				    ndx, ident, ofl) == S_ERROR)
26060Sstevel@tonic-gate 					return (S_ERROR);
26070Sstevel@tonic-gate 			}
26080Sstevel@tonic-gate 		} else {
26090Sstevel@tonic-gate 			/*
26100Sstevel@tonic-gate 			 * If this section is below SHT_LOSUNW then we don't
26110Sstevel@tonic-gate 			 * really know what to do with it, issue a warning
26120Sstevel@tonic-gate 			 * message but do the basic section processing anyway.
26130Sstevel@tonic-gate 			 */
26144734Sab196087 			if (row < (Word)SHT_LOSUNW) {
26154734Sab196087 				Conv_inv_buf_t inv_buf;
26164734Sab196087 
26171618Srie 				eprintf(ofl->ofl_lml, ERR_WARNING,
26181618Srie 				    MSG_INTL(MSG_FIL_INVALSEC), ifl->ifl_name,
26199878SAli.Bahrami@Sun.COM 				    EC_WORD(ndx), name, conv_sec_type(
26209273SAli.Bahrami@Sun.COM 				    ifl->ifl_ehdr->e_ident[EI_OSABI],
26219273SAli.Bahrami@Sun.COM 				    ifl->ifl_ehdr->e_machine,
26224734Sab196087 				    shdr->sh_type, 0, &inv_buf));
26234734Sab196087 			}
26240Sstevel@tonic-gate 
26250Sstevel@tonic-gate 			/*
26260Sstevel@tonic-gate 			 * Handle sections greater than SHT_LOSUNW.
26270Sstevel@tonic-gate 			 */
26280Sstevel@tonic-gate 			switch (row) {
26290Sstevel@tonic-gate 			case SHT_SUNW_dof:
26300Sstevel@tonic-gate 				if (process_section(name, ifl, shdr, scn,
26310Sstevel@tonic-gate 				    ndx, ident, ofl) == S_ERROR)
26320Sstevel@tonic-gate 					return (S_ERROR);
26330Sstevel@tonic-gate 				break;
26340Sstevel@tonic-gate 			case SHT_SUNW_cap:
26359085SAli.Bahrami@Sun.COM 				if (process_section(name, ifl, shdr, scn, ndx,
26369085SAli.Bahrami@Sun.COM 				    ld_targ.t_id.id_null, ofl) == S_ERROR)
26370Sstevel@tonic-gate 					return (S_ERROR);
26380Sstevel@tonic-gate 				capisp = ifl->ifl_isdesc[ndx];
26390Sstevel@tonic-gate 				break;
2640*11827SRod.Evans@Sun.COM 			case SHT_SUNW_capinfo:
2641*11827SRod.Evans@Sun.COM 				if (process_section(name, ifl, shdr, scn, ndx,
2642*11827SRod.Evans@Sun.COM 				    ld_targ.t_id.id_null, ofl) == S_ERROR)
2643*11827SRod.Evans@Sun.COM 					return (S_ERROR);
2644*11827SRod.Evans@Sun.COM 				capinfoisp = ifl->ifl_isdesc[ndx];
2645*11827SRod.Evans@Sun.COM 				break;
26460Sstevel@tonic-gate 			case SHT_SUNW_DEBUGSTR:
26470Sstevel@tonic-gate 			case SHT_SUNW_DEBUG:
26480Sstevel@tonic-gate 				if (process_debug(name, ifl, shdr, scn,
26490Sstevel@tonic-gate 				    ndx, ident, ofl) == S_ERROR)
26500Sstevel@tonic-gate 					return (S_ERROR);
26510Sstevel@tonic-gate 				break;
26520Sstevel@tonic-gate 			case SHT_SUNW_move:
26539085SAli.Bahrami@Sun.COM 				if (process_section(name, ifl, shdr, scn, ndx,
26549085SAli.Bahrami@Sun.COM 				    ld_targ.t_id.id_null, ofl) == S_ERROR)
26550Sstevel@tonic-gate 					return (S_ERROR);
26560Sstevel@tonic-gate 				break;
26570Sstevel@tonic-gate 			case SHT_SUNW_syminfo:
26589085SAli.Bahrami@Sun.COM 				if (process_section(name, ifl, shdr, scn, ndx,
26599085SAli.Bahrami@Sun.COM 				    ld_targ.t_id.id_null, ofl) == S_ERROR)
26600Sstevel@tonic-gate 					return (S_ERROR);
26610Sstevel@tonic-gate 				sifisp = ifl->ifl_isdesc[ndx];
26620Sstevel@tonic-gate 				break;
26630Sstevel@tonic-gate 			case SHT_SUNW_ANNOTATE:
26647463SRod.Evans@Sun.COM 				if (process_progbits(name, ifl, shdr, scn,
26657463SRod.Evans@Sun.COM 				    ndx, ident, ofl) == S_ERROR)
26667463SRod.Evans@Sun.COM 					return (S_ERROR);
26677463SRod.Evans@Sun.COM 				break;
26680Sstevel@tonic-gate 			case SHT_SUNW_COMDAT:
26690Sstevel@tonic-gate 				if (process_progbits(name, ifl, shdr, scn,
26700Sstevel@tonic-gate 				    ndx, ident, ofl) == S_ERROR)
26710Sstevel@tonic-gate 					return (S_ERROR);
26727463SRod.Evans@Sun.COM 				ifl->ifl_isdesc[ndx]->is_flags |= FLG_IS_COMDAT;
26730Sstevel@tonic-gate 				break;
26740Sstevel@tonic-gate 			case SHT_SUNW_verdef:
26759085SAli.Bahrami@Sun.COM 				if (process_section(name, ifl, shdr, scn, ndx,
26769085SAli.Bahrami@Sun.COM 				    ld_targ.t_id.id_null, ofl) == S_ERROR)
26770Sstevel@tonic-gate 					return (S_ERROR);
26780Sstevel@tonic-gate 				vdfisp = ifl->ifl_isdesc[ndx];
26790Sstevel@tonic-gate 				break;
26800Sstevel@tonic-gate 			case SHT_SUNW_verneed:
26819085SAli.Bahrami@Sun.COM 				if (process_section(name, ifl, shdr, scn, ndx,
26829085SAli.Bahrami@Sun.COM 				    ld_targ.t_id.id_null, ofl) == S_ERROR)
26830Sstevel@tonic-gate 					return (S_ERROR);
26840Sstevel@tonic-gate 				vndisp = ifl->ifl_isdesc[ndx];
26850Sstevel@tonic-gate 				break;
26860Sstevel@tonic-gate 			case SHT_SUNW_versym:
26879085SAli.Bahrami@Sun.COM 				if (process_section(name, ifl, shdr, scn, ndx,
26889085SAli.Bahrami@Sun.COM 				    ld_targ.t_id.id_null, ofl) == S_ERROR)
26890Sstevel@tonic-gate 					return (S_ERROR);
26900Sstevel@tonic-gate 				vsyisp = ifl->ifl_isdesc[ndx];
26910Sstevel@tonic-gate 				break;
26920Sstevel@tonic-gate 			case SHT_SPARC_GOTDATA:
26936206Sab196087 				/*
26946206Sab196087 				 * SHT_SPARC_GOTDATA (0x70000000) is in the
26956206Sab196087 				 * SHT_LOPROC - SHT_HIPROC range reserved
26966206Sab196087 				 * for processor-specific semantics. It is
26976206Sab196087 				 * only meaningful for sparc targets.
26986206Sab196087 				 */
26996206Sab196087 				if (ld_targ.t_m.m_mach !=
27006206Sab196087 				    LD_TARG_BYCLASS(EM_SPARC, EM_SPARCV9))
27016206Sab196087 					goto do_default;
27029085SAli.Bahrami@Sun.COM 				if (process_section(name, ifl, shdr, scn, ndx,
27039085SAli.Bahrami@Sun.COM 				    ld_targ.t_id.id_gotdata, ofl) == S_ERROR)
27040Sstevel@tonic-gate 					return (S_ERROR);
27050Sstevel@tonic-gate 				break;
27066206Sab196087 #if	defined(_ELF64)
27070Sstevel@tonic-gate 			case SHT_AMD64_UNWIND:
27086206Sab196087 				/*
27096206Sab196087 				 * SHT_AMD64_UNWIND (0x70000001) is in the
27106206Sab196087 				 * SHT_LOPROC - SHT_HIPROC range reserved
27116206Sab196087 				 * for processor-specific semantics. It is
27126206Sab196087 				 * only meaningful for amd64 targets.
27136206Sab196087 				 */
27146206Sab196087 				if (ld_targ.t_m.m_mach != EM_AMD64)
27156206Sab196087 					goto do_default;
27167463SRod.Evans@Sun.COM 
27176206Sab196087 				/*
27186206Sab196087 				 * Target is x86, so this really is
27196206Sab196087 				 * SHT_AMD64_UNWIND
27206206Sab196087 				 */
27210Sstevel@tonic-gate 				if (column == 0) {
27220Sstevel@tonic-gate 					/*
27230Sstevel@tonic-gate 					 * column == ET_REL
27240Sstevel@tonic-gate 					 */
27257463SRod.Evans@Sun.COM 					if (process_section(name, ifl, shdr,
27267463SRod.Evans@Sun.COM 					    scn, ndx, ld_targ.t_id.id_unwind,
27277463SRod.Evans@Sun.COM 					    ofl) == S_ERROR)
27280Sstevel@tonic-gate 						return (S_ERROR);
27299085SAli.Bahrami@Sun.COM 					ifl->ifl_isdesc[ndx]->is_flags |=
27309085SAli.Bahrami@Sun.COM 					    FLG_IS_EHFRAME;
27310Sstevel@tonic-gate 				}
27320Sstevel@tonic-gate 				break;
27330Sstevel@tonic-gate #endif
27340Sstevel@tonic-gate 			default:
27356206Sab196087 			do_default:
27369085SAli.Bahrami@Sun.COM 				if (process_section(name, ifl, shdr, scn, ndx,
27379085SAli.Bahrami@Sun.COM 				    ((ident == ld_targ.t_id.id_null) ?
27389085SAli.Bahrami@Sun.COM 				    ident : ld_targ.t_id.id_user), ofl) ==
27399085SAli.Bahrami@Sun.COM 				    S_ERROR)
27400Sstevel@tonic-gate 					return (S_ERROR);
27410Sstevel@tonic-gate 				break;
27420Sstevel@tonic-gate 			}
27430Sstevel@tonic-gate 		}
27447463SRod.Evans@Sun.COM 	}
27450Sstevel@tonic-gate 
27467463SRod.Evans@Sun.COM 	/*
27477463SRod.Evans@Sun.COM 	 * Now that all input sections have been analyzed, and prior to placing
27487463SRod.Evans@Sun.COM 	 * any input sections to their output sections, process any groups.
27497463SRod.Evans@Sun.COM 	 * Groups can contribute COMDAT items, which may get discarded as part
27507463SRod.Evans@Sun.COM 	 * of placement.  In addition, COMDAT names may require transformation
27517463SRod.Evans@Sun.COM 	 * to indicate different output section placement.
27527463SRod.Evans@Sun.COM 	 */
27537463SRod.Evans@Sun.COM 	if (ifl->ifl_flags & FLG_IS_GROUPS) {
27547463SRod.Evans@Sun.COM 		for (ndx = 1; ndx < ifl->ifl_shnum; ndx++) {
27557463SRod.Evans@Sun.COM 			Is_desc	*isp;
27567463SRod.Evans@Sun.COM 
27577463SRod.Evans@Sun.COM 			if (((isp = ifl->ifl_isdesc[ndx]) == NULL) ||
27587463SRod.Evans@Sun.COM 			    (isp->is_shdr->sh_type != SHT_GROUP))
27597463SRod.Evans@Sun.COM 				continue;
27607463SRod.Evans@Sun.COM 
27617463SRod.Evans@Sun.COM 			if (ld_group_process(isp, ofl) == S_ERROR)
27627463SRod.Evans@Sun.COM 				return (S_ERROR);
27630Sstevel@tonic-gate 		}
27640Sstevel@tonic-gate 	}
27650Sstevel@tonic-gate 
27660Sstevel@tonic-gate 	/*
27679615SAli.Bahrami@Sun.COM 	 * Now that all of the input sections have been processed, place
27689615SAli.Bahrami@Sun.COM 	 * them in the appropriate output sections.
27690Sstevel@tonic-gate 	 */
27707463SRod.Evans@Sun.COM 	for (ndx = 1; ndx < ifl->ifl_shnum; ndx++) {
27717463SRod.Evans@Sun.COM 		Is_desc	*isp;
27727463SRod.Evans@Sun.COM 
27737463SRod.Evans@Sun.COM 		if (((isp = ifl->ifl_isdesc[ndx]) == NULL) ||
27747463SRod.Evans@Sun.COM 		    ((isp->is_flags & FLG_IS_PLACE) == 0))
27757463SRod.Evans@Sun.COM 			continue;
27767463SRod.Evans@Sun.COM 
27777463SRod.Evans@Sun.COM 		/*
27787463SRod.Evans@Sun.COM 		 * Place all non-ordered sections within their appropriate
27797463SRod.Evans@Sun.COM 		 * output section.
27807463SRod.Evans@Sun.COM 		 */
27819615SAli.Bahrami@Sun.COM 		if ((isp->is_flags & FLG_IS_ORDERED) == 0) {
278211734SAli.Bahrami@Sun.COM 			if (ld_place_section(ofl, isp, path_info,
27839615SAli.Bahrami@Sun.COM 			    isp->is_keyident, NULL) == (Os_desc *)S_ERROR)
27847463SRod.Evans@Sun.COM 				return (S_ERROR);
27859615SAli.Bahrami@Sun.COM 			continue;
27867463SRod.Evans@Sun.COM 		}
27877463SRod.Evans@Sun.COM 
27887463SRod.Evans@Sun.COM 		/*
27899615SAli.Bahrami@Sun.COM 		 * Count the number of ordered sections and retain the first
27909615SAli.Bahrami@Sun.COM 		 * ordered section index. This will be used to optimize the
27919615SAli.Bahrami@Sun.COM 		 * ordered section loop that immediately follows this one.
27927463SRod.Evans@Sun.COM 		 */
27939615SAli.Bahrami@Sun.COM 		ordcnt++;
27949615SAli.Bahrami@Sun.COM 		if (ordndx == 0)
27959615SAli.Bahrami@Sun.COM 			ordndx = ndx;
27969615SAli.Bahrami@Sun.COM 	}
27979615SAli.Bahrami@Sun.COM 
27989615SAli.Bahrami@Sun.COM 	/*
27999615SAli.Bahrami@Sun.COM 	 * Having placed all the non-ordered sections, it is now
28009615SAli.Bahrami@Sun.COM 	 * safe to place SHF_ORDERED/SHF_LINK_ORDER sections.
28019615SAli.Bahrami@Sun.COM 	 */
28029615SAli.Bahrami@Sun.COM 	if (ifl->ifl_flags & FLG_IF_ORDERED) {
28039615SAli.Bahrami@Sun.COM 		for (ndx = ordndx; ndx < ifl->ifl_shnum; ndx++) {
28049615SAli.Bahrami@Sun.COM 			Is_desc	*isp;
28059615SAli.Bahrami@Sun.COM 
28069615SAli.Bahrami@Sun.COM 			if (((isp = ifl->ifl_isdesc[ndx]) == NULL) ||
28079615SAli.Bahrami@Sun.COM 			    ((isp->is_flags &
28089615SAli.Bahrami@Sun.COM 			    (FLG_IS_PLACE | FLG_IS_ORDERED)) !=
28099615SAli.Bahrami@Sun.COM 			    (FLG_IS_PLACE | FLG_IS_ORDERED)))
28109615SAli.Bahrami@Sun.COM 				continue;
28119615SAli.Bahrami@Sun.COM 
28129615SAli.Bahrami@Sun.COM 			/* ld_process_ordered() calls ld_place_section() */
281311734SAli.Bahrami@Sun.COM 			if (ld_process_ordered(ofl, ifl, path_info, ndx) ==
281411734SAli.Bahrami@Sun.COM 			    S_ERROR)
28159615SAli.Bahrami@Sun.COM 				return (S_ERROR);
28169615SAli.Bahrami@Sun.COM 
28179615SAli.Bahrami@Sun.COM 			/* If we've done them all, stop searching */
28189615SAli.Bahrami@Sun.COM 			if (--ordcnt == 0)
28199615SAli.Bahrami@Sun.COM 				break;
28207463SRod.Evans@Sun.COM 		}
28217463SRod.Evans@Sun.COM 	}
28227463SRod.Evans@Sun.COM 
28237463SRod.Evans@Sun.COM 	/*
28249615SAli.Bahrami@Sun.COM 	 * If this is a shared object explicitly specified on the command
28259615SAli.Bahrami@Sun.COM 	 * line (as opposed to being a dependency of such an object),
28269615SAli.Bahrami@Sun.COM 	 * determine if the user has specified a control definition. This
28279615SAli.Bahrami@Sun.COM 	 * descriptor may specify which version definitions can be used
28289615SAli.Bahrami@Sun.COM 	 * from this object. It may also update the dependency to USED and
28299615SAli.Bahrami@Sun.COM 	 * supply an alternative SONAME.
28300Sstevel@tonic-gate 	 */
283110792SRod.Evans@Sun.COM 	sdf = NULL;
28320Sstevel@tonic-gate 	if (column && (ifl->ifl_flags & FLG_IF_NEEDED)) {
28330Sstevel@tonic-gate 		const char	*base;
28340Sstevel@tonic-gate 
28350Sstevel@tonic-gate 		/*
28360Sstevel@tonic-gate 		 * Use the basename of the input file (typically this is the
28370Sstevel@tonic-gate 		 * compilation environment name, ie. libfoo.so).
28380Sstevel@tonic-gate 		 */
28390Sstevel@tonic-gate 		if ((base = strrchr(ifl->ifl_name, '/')) == NULL)
28400Sstevel@tonic-gate 			base = ifl->ifl_name;
28410Sstevel@tonic-gate 		else
28420Sstevel@tonic-gate 			base++;
28430Sstevel@tonic-gate 
28449131SRod.Evans@Sun.COM 		if ((sdf = sdf_find(base, ofl->ofl_socntl)) != NULL) {
28450Sstevel@tonic-gate 			sdf->sdf_file = ifl;
28460Sstevel@tonic-gate 			ifl->ifl_sdfdesc = sdf;
28470Sstevel@tonic-gate 		}
28480Sstevel@tonic-gate 	}
28490Sstevel@tonic-gate 
28500Sstevel@tonic-gate 	/*
2851*11827SRod.Evans@Sun.COM 	 * Before symbol processing, process any capabilities.  Capabilities
2852*11827SRod.Evans@Sun.COM 	 * can reference a string table, which is why this processing is
2853*11827SRod.Evans@Sun.COM 	 * carried out after the initial section processing.  Capabilities,
2854*11827SRod.Evans@Sun.COM 	 * together with -z symbolcap, can require the conversion of global
2855*11827SRod.Evans@Sun.COM 	 * symbols to local symbols.
28560Sstevel@tonic-gate 	 */
2857*11827SRod.Evans@Sun.COM 	if (capisp && (process_cap(ofl, ifl, capisp) == S_ERROR))
2858*11827SRod.Evans@Sun.COM 		return (S_ERROR);
28590Sstevel@tonic-gate 
28600Sstevel@tonic-gate 	/*
28610Sstevel@tonic-gate 	 * Process any version dependencies.  These will establish shared object
28620Sstevel@tonic-gate 	 * `needed' entries in the same manner as will be generated from the
28630Sstevel@tonic-gate 	 * .dynamic's NEEDED entries.
28640Sstevel@tonic-gate 	 */
28650Sstevel@tonic-gate 	if (vndisp && (ofl->ofl_flags & (FLG_OF_NOUNDEF | FLG_OF_SYMBOLIC)))
28661618Srie 		if (ld_vers_need_process(vndisp, ifl, ofl) == S_ERROR)
28670Sstevel@tonic-gate 			return (S_ERROR);
28680Sstevel@tonic-gate 
28690Sstevel@tonic-gate 	/*
28700Sstevel@tonic-gate 	 * Before processing any symbol resolution or relocations process any
28710Sstevel@tonic-gate 	 * version sections.
28720Sstevel@tonic-gate 	 */
28730Sstevel@tonic-gate 	if (vsyisp)
28741618Srie 		(void) ld_vers_sym_process(ofl->ofl_lml, vsyisp, ifl);
28750Sstevel@tonic-gate 
28760Sstevel@tonic-gate 	if (ifl->ifl_versym &&
28770Sstevel@tonic-gate 	    (vdfisp || (sdf && (sdf->sdf_flags & FLG_SDF_SELECT))))
28781618Srie 		if (ld_vers_def_process(vdfisp, ifl, ofl) == S_ERROR)
28790Sstevel@tonic-gate 			return (S_ERROR);
28800Sstevel@tonic-gate 
28810Sstevel@tonic-gate 	/*
28820Sstevel@tonic-gate 	 * Having collected the appropriate sections carry out any additional
28830Sstevel@tonic-gate 	 * processing if necessary.
28840Sstevel@tonic-gate 	 */
28850Sstevel@tonic-gate 	for (ndx = 0; ndx < ifl->ifl_shnum; ndx++) {
28867463SRod.Evans@Sun.COM 		Is_desc	*isp;
28870Sstevel@tonic-gate 
288810792SRod.Evans@Sun.COM 		if ((isp = ifl->ifl_isdesc[ndx]) == NULL)
28890Sstevel@tonic-gate 			continue;
28900Sstevel@tonic-gate 		row = isp->is_shdr->sh_type;
28910Sstevel@tonic-gate 
28920Sstevel@tonic-gate 		if ((isp->is_flags & FLG_IS_DISCARD) == 0)
28931618Srie 			ld_sup_section(ofl, isp->is_name, isp->is_shdr, ndx,
28941618Srie 			    isp->is_indata, elf);
28950Sstevel@tonic-gate 
28960Sstevel@tonic-gate 		/*
28979131SRod.Evans@Sun.COM 		 * If this is a SHT_SUNW_move section from a relocatable file,
28989131SRod.Evans@Sun.COM 		 * keep track of the section for later processing.
28990Sstevel@tonic-gate 		 */
29000Sstevel@tonic-gate 		if ((row == SHT_SUNW_move) && (column == 0)) {
29019131SRod.Evans@Sun.COM 			if (aplist_append(&(ofl->ofl_ismove), isp,
29029131SRod.Evans@Sun.COM 			    AL_CNT_OFL_MOVE) == NULL)
29030Sstevel@tonic-gate 				return (S_ERROR);
29040Sstevel@tonic-gate 		}
29050Sstevel@tonic-gate 
29060Sstevel@tonic-gate 		/*
29070Sstevel@tonic-gate 		 * If this is a standard section type process it via the
29080Sstevel@tonic-gate 		 * appropriate action routine.
29090Sstevel@tonic-gate 		 */
29100Sstevel@tonic-gate 		if (row < SHT_NUM) {
29117463SRod.Evans@Sun.COM 			if (Final[row][column] != NULL) {
29127463SRod.Evans@Sun.COM 				if (Final[row][column](isp, ifl,
29137463SRod.Evans@Sun.COM 				    ofl) == S_ERROR)
29140Sstevel@tonic-gate 					return (S_ERROR);
29157463SRod.Evans@Sun.COM 			}
29167463SRod.Evans@Sun.COM #if	defined(_ELF64)
29177463SRod.Evans@Sun.COM 		} else if ((row == SHT_AMD64_UNWIND) && (column == 0)) {
29187463SRod.Evans@Sun.COM 			Os_desc	*osp = isp->is_osdesc;
29197463SRod.Evans@Sun.COM 
29207463SRod.Evans@Sun.COM 			/*
29217463SRod.Evans@Sun.COM 			 * SHT_AMD64_UNWIND (0x70000001) is in the SHT_LOPROC -
29227463SRod.Evans@Sun.COM 			 * SHT_HIPROC range reserved for processor-specific
29237463SRod.Evans@Sun.COM 			 * semantics, and is only meaningful for amd64 targets.
29247463SRod.Evans@Sun.COM 			 *
29257463SRod.Evans@Sun.COM 			 * Only process unwind contents from relocatable
29267463SRod.Evans@Sun.COM 			 * objects.
29277463SRod.Evans@Sun.COM 			 */
29287463SRod.Evans@Sun.COM 			if (osp && (ld_targ.t_m.m_mach == EM_AMD64) &&
29299085SAli.Bahrami@Sun.COM 			    (ld_unwind_register(osp, ofl) == S_ERROR))
29307463SRod.Evans@Sun.COM 				return (S_ERROR);
29317463SRod.Evans@Sun.COM #endif
29320Sstevel@tonic-gate 		}
29330Sstevel@tonic-gate 	}
29340Sstevel@tonic-gate 
29350Sstevel@tonic-gate 	/*
2936*11827SRod.Evans@Sun.COM 	 * Following symbol processing, if this relocatable object input file
2937*11827SRod.Evans@Sun.COM 	 * provides symbol capabilities, tag the associated symbols so that
2938*11827SRod.Evans@Sun.COM 	 * the symbols can be re-assigned to the new capabilities symbol
2939*11827SRod.Evans@Sun.COM 	 * section that will be created for the output file.
2940*11827SRod.Evans@Sun.COM 	 */
2941*11827SRod.Evans@Sun.COM 	if (capinfoisp && (ifl->ifl_ehdr->e_type == ET_REL) &&
2942*11827SRod.Evans@Sun.COM 	    (process_capinfo(ofl, ifl, capinfoisp) == S_ERROR))
2943*11827SRod.Evans@Sun.COM 		return (S_ERROR);
2944*11827SRod.Evans@Sun.COM 
2945*11827SRod.Evans@Sun.COM 	/*
29460Sstevel@tonic-gate 	 * After processing any symbol resolution, and if this dependency
29470Sstevel@tonic-gate 	 * indicates it contains symbols that can't be directly bound to,
29480Sstevel@tonic-gate 	 * set the symbols appropriately.
29490Sstevel@tonic-gate 	 */
29500Sstevel@tonic-gate 	if (sifisp && ((ifl->ifl_flags & (FLG_IF_NEEDED | FLG_IF_NODIRECT)) ==
29510Sstevel@tonic-gate 	    (FLG_IF_NEEDED | FLG_IF_NODIRECT)))
29521618Srie 		(void) ld_sym_nodirect(sifisp, ifl, ofl);
29530Sstevel@tonic-gate 
29540Sstevel@tonic-gate 	return (1);
29550Sstevel@tonic-gate }
29560Sstevel@tonic-gate 
29570Sstevel@tonic-gate /*
29580Sstevel@tonic-gate  * Process the current input file.  There are basically three types of files
29590Sstevel@tonic-gate  * that come through here:
29600Sstevel@tonic-gate  *
296110792SRod.Evans@Sun.COM  *  -	files explicitly defined on the command line (ie. foo.o or bar.so),
29620Sstevel@tonic-gate  *	in this case only the `name' field is valid.
29630Sstevel@tonic-gate  *
296410792SRod.Evans@Sun.COM  *  -	libraries determined from the -l command line option (ie. -lbar),
29650Sstevel@tonic-gate  *	in this case the `soname' field contains the basename of the located
29660Sstevel@tonic-gate  *	file.
29670Sstevel@tonic-gate  *
29680Sstevel@tonic-gate  * Any shared object specified via the above two conventions must be recorded
29690Sstevel@tonic-gate  * as a needed dependency.
29700Sstevel@tonic-gate  *
297110792SRod.Evans@Sun.COM  *  -	libraries specified as dependencies of those libraries already obtained
29720Sstevel@tonic-gate  *	via the command line (ie. bar.so has a DT_NEEDED entry of fred.so.1),
29730Sstevel@tonic-gate  *	in this case the `soname' field contains either a full pathname (if the
29740Sstevel@tonic-gate  *	needed entry contained a `/'), or the basename of the located file.
29750Sstevel@tonic-gate  *	These libraries are processed to verify symbol binding but are not
29760Sstevel@tonic-gate  *	recorded as dependencies of the output file being generated.
29770Sstevel@tonic-gate  */
29780Sstevel@tonic-gate Ifl_desc *
29791618Srie ld_process_ifl(const char *name, const char *soname, int fd, Elf *elf,
29807359SRod.Evans@Sun.COM     Word flags, Ofl_desc *ofl, Rej_desc *rej)
29810Sstevel@tonic-gate {
29820Sstevel@tonic-gate 	Ifl_desc	*ifl;
29830Sstevel@tonic-gate 	Ehdr		*ehdr;
29840Sstevel@tonic-gate 	uintptr_t	error = 0;
29850Sstevel@tonic-gate 	struct stat	status;
29860Sstevel@tonic-gate 	Ar_desc		*adp;
29870Sstevel@tonic-gate 	Rej_desc	_rej;
29880Sstevel@tonic-gate 
29890Sstevel@tonic-gate 	/*
29900Sstevel@tonic-gate 	 * If this file was not extracted from an archive obtain its device
29910Sstevel@tonic-gate 	 * information.  This will be used to determine if the file has already
29920Sstevel@tonic-gate 	 * been processed (rather than simply comparing filenames, the device
29930Sstevel@tonic-gate 	 * information provides a quicker comparison and detects linked files).
29940Sstevel@tonic-gate 	 */
29958598SRod.Evans@Sun.COM 	if (fd && ((flags & FLG_IF_EXTRACT) == 0))
29960Sstevel@tonic-gate 		(void) fstat(fd, &status);
29970Sstevel@tonic-gate 	else {
29980Sstevel@tonic-gate 		status.st_dev = 0;
29990Sstevel@tonic-gate 		status.st_ino = 0;
30000Sstevel@tonic-gate 	}
30010Sstevel@tonic-gate 
30020Sstevel@tonic-gate 	switch (elf_kind(elf)) {
30030Sstevel@tonic-gate 	case ELF_K_AR:
30040Sstevel@tonic-gate 		/*
30050Sstevel@tonic-gate 		 * Determine if we've already come across this archive file.
30060Sstevel@tonic-gate 		 */
30070Sstevel@tonic-gate 		if (!(flags & FLG_IF_EXTRACT)) {
30089131SRod.Evans@Sun.COM 			Aliste	idx;
30099131SRod.Evans@Sun.COM 
30109131SRod.Evans@Sun.COM 			for (APLIST_TRAVERSE(ofl->ofl_ars, idx, adp)) {
30110Sstevel@tonic-gate 				if ((adp->ad_stdev != status.st_dev) ||
30120Sstevel@tonic-gate 				    (adp->ad_stino != status.st_ino))
30130Sstevel@tonic-gate 					continue;
30140Sstevel@tonic-gate 
30150Sstevel@tonic-gate 				/*
30160Sstevel@tonic-gate 				 * We've seen this file before so reuse the
30170Sstevel@tonic-gate 				 * original archive descriptor and discard the
30187359SRod.Evans@Sun.COM 				 * new elf descriptor.  Note that a file
30197359SRod.Evans@Sun.COM 				 * descriptor is unnecessary, as the file is
30207359SRod.Evans@Sun.COM 				 * already available in memory.
30210Sstevel@tonic-gate 				 */
30221618Srie 				DBG_CALL(Dbg_file_reuse(ofl->ofl_lml, name,
30231618Srie 				    adp->ad_name));
30240Sstevel@tonic-gate 				(void) elf_end(elf);
30257359SRod.Evans@Sun.COM 				return ((Ifl_desc *)ld_process_archive(name, -1,
30260Sstevel@tonic-gate 				    adp, ofl));
30270Sstevel@tonic-gate 			}
30280Sstevel@tonic-gate 		}
30290Sstevel@tonic-gate 
30300Sstevel@tonic-gate 		/*
30310Sstevel@tonic-gate 		 * As we haven't processed this file before establish a new
30320Sstevel@tonic-gate 		 * archive descriptor.
30330Sstevel@tonic-gate 		 */
30341618Srie 		adp = ld_ar_setup(name, elf, ofl);
303510792SRod.Evans@Sun.COM 		if ((adp == NULL) || (adp == (Ar_desc *)S_ERROR))
30360Sstevel@tonic-gate 			return ((Ifl_desc *)adp);
30370Sstevel@tonic-gate 		adp->ad_stdev = status.st_dev;
30380Sstevel@tonic-gate 		adp->ad_stino = status.st_ino;
30390Sstevel@tonic-gate 
30401618Srie 		ld_sup_file(ofl, name, ELF_K_AR, flags, elf);
30410Sstevel@tonic-gate 
30427359SRod.Evans@Sun.COM 		/*
30437359SRod.Evans@Sun.COM 		 * Indicate that the ELF descriptor no longer requires a file
30447359SRod.Evans@Sun.COM 		 * descriptor by reading the entire file.  The file is already
30457359SRod.Evans@Sun.COM 		 * read via the initial mmap(2) behind elf_begin(3elf), thus
30467359SRod.Evans@Sun.COM 		 * this operation is effectively a no-op.  However, a side-
30477359SRod.Evans@Sun.COM 		 * effect is that the internal file descriptor, maintained in
30487359SRod.Evans@Sun.COM 		 * the ELF descriptor, is set to -1.  This setting will not
30497359SRod.Evans@Sun.COM 		 * be compared with any file descriptor that is passed to
30507359SRod.Evans@Sun.COM 		 * elf_begin(), should this archive, or one of the archive
30517359SRod.Evans@Sun.COM 		 * members, be processed again from the command line or
30527359SRod.Evans@Sun.COM 		 * because of a -z rescan.
30537359SRod.Evans@Sun.COM 		 */
30547359SRod.Evans@Sun.COM 		if (elf_cntl(elf, ELF_C_FDREAD) == -1) {
30557359SRod.Evans@Sun.COM 			eprintf(ofl->ofl_lml, ERR_ELF, MSG_INTL(MSG_ELF_CNTL),
30567359SRod.Evans@Sun.COM 			    name);
30577359SRod.Evans@Sun.COM 			ofl->ofl_flags |= FLG_OF_FATAL;
30587359SRod.Evans@Sun.COM 			return (NULL);
30597359SRod.Evans@Sun.COM 		}
30607359SRod.Evans@Sun.COM 
30617359SRod.Evans@Sun.COM 		return ((Ifl_desc *)ld_process_archive(name, -1, adp, ofl));
30620Sstevel@tonic-gate 
30630Sstevel@tonic-gate 	case ELF_K_ELF:
30640Sstevel@tonic-gate 		/*
30650Sstevel@tonic-gate 		 * Obtain the elf header so that we can determine what type of
30660Sstevel@tonic-gate 		 * elf ELF_K_ELF file this is.
30670Sstevel@tonic-gate 		 */
30680Sstevel@tonic-gate 		if ((ehdr = elf_getehdr(elf)) == NULL) {
30690Sstevel@tonic-gate 			int	_class = gelf_getclass(elf);
30700Sstevel@tonic-gate 
30710Sstevel@tonic-gate 			/*
30720Sstevel@tonic-gate 			 * Failure could occur for a number of reasons at this
30730Sstevel@tonic-gate 			 * point.  Typically the files class is incorrect (ie.
30740Sstevel@tonic-gate 			 * user is building 64-bit but managed to pint at 32-bit
30750Sstevel@tonic-gate 			 * libraries).  However any number of elf errors can
30760Sstevel@tonic-gate 			 * also occur, such as from a truncated or corrupt file.
30770Sstevel@tonic-gate 			 * Here we try and get the best error message possible.
30780Sstevel@tonic-gate 			 */
30796206Sab196087 			if (ld_targ.t_m.m_class != _class) {
30800Sstevel@tonic-gate 				_rej.rej_type = SGS_REJ_CLASS;
30810Sstevel@tonic-gate 				_rej.rej_info = (uint_t)_class;
30820Sstevel@tonic-gate 			} else {
30830Sstevel@tonic-gate 				_rej.rej_type = SGS_REJ_STR;
30840Sstevel@tonic-gate 				_rej.rej_str = elf_errmsg(-1);
30850Sstevel@tonic-gate 			}
30860Sstevel@tonic-gate 			_rej.rej_name = name;
30876206Sab196087 			DBG_CALL(Dbg_file_rejected(ofl->ofl_lml, &_rej,
30886206Sab196087 			    ld_targ.t_m.m_mach));
30890Sstevel@tonic-gate 			if (rej->rej_type == 0) {
30900Sstevel@tonic-gate 				*rej = _rej;
30910Sstevel@tonic-gate 				rej->rej_name = strdup(_rej.rej_name);
30920Sstevel@tonic-gate 			}
30937359SRod.Evans@Sun.COM 			return (NULL);
30940Sstevel@tonic-gate 		}
30950Sstevel@tonic-gate 
30960Sstevel@tonic-gate 		/*
30970Sstevel@tonic-gate 		 * Determine if we've already come across this file.
30980Sstevel@tonic-gate 		 */
30990Sstevel@tonic-gate 		if (!(flags & FLG_IF_EXTRACT)) {
31009131SRod.Evans@Sun.COM 			APlist	*apl;
31019131SRod.Evans@Sun.COM 			Aliste	idx;
31020Sstevel@tonic-gate 
31030Sstevel@tonic-gate 			if (ehdr->e_type == ET_REL)
31049131SRod.Evans@Sun.COM 				apl = ofl->ofl_objs;
31050Sstevel@tonic-gate 			else
31069131SRod.Evans@Sun.COM 				apl = ofl->ofl_sos;
31070Sstevel@tonic-gate 
31080Sstevel@tonic-gate 			/*
31090Sstevel@tonic-gate 			 * Traverse the appropriate file list and determine if
31100Sstevel@tonic-gate 			 * a dev/inode match is found.
31110Sstevel@tonic-gate 			 */
31129131SRod.Evans@Sun.COM 			for (APLIST_TRAVERSE(apl, idx, ifl)) {
31130Sstevel@tonic-gate 				/*
31140Sstevel@tonic-gate 				 * Ifl_desc generated via -Nneed, therefore no
31150Sstevel@tonic-gate 				 * actual file behind it.
31160Sstevel@tonic-gate 				 */
31170Sstevel@tonic-gate 				if (ifl->ifl_flags & FLG_IF_NEEDSTR)
31180Sstevel@tonic-gate 					continue;
31190Sstevel@tonic-gate 
31200Sstevel@tonic-gate 				if ((ifl->ifl_stino != status.st_ino) ||
31210Sstevel@tonic-gate 				    (ifl->ifl_stdev != status.st_dev))
31220Sstevel@tonic-gate 					continue;
31230Sstevel@tonic-gate 
31240Sstevel@tonic-gate 				/*
31250Sstevel@tonic-gate 				 * Disregard (skip) this image.
31260Sstevel@tonic-gate 				 */
31271618Srie 				DBG_CALL(Dbg_file_skip(ofl->ofl_lml,
31281618Srie 				    ifl->ifl_name, name));
31290Sstevel@tonic-gate 				(void) elf_end(elf);
31300Sstevel@tonic-gate 
31310Sstevel@tonic-gate 				/*
31320Sstevel@tonic-gate 				 * If the file was explicitly defined on the
31330Sstevel@tonic-gate 				 * command line (this is always the case for
31340Sstevel@tonic-gate 				 * relocatable objects, and is true for shared
31350Sstevel@tonic-gate 				 * objects when they weren't specified via -l or
31360Sstevel@tonic-gate 				 * were dragged in as an implicit dependency),
31370Sstevel@tonic-gate 				 * then warn the user.
31380Sstevel@tonic-gate 				 */
31390Sstevel@tonic-gate 				if ((flags & FLG_IF_CMDLINE) ||
31400Sstevel@tonic-gate 				    (ifl->ifl_flags & FLG_IF_CMDLINE)) {
31410Sstevel@tonic-gate 					const char	*errmsg;
31420Sstevel@tonic-gate 
31430Sstevel@tonic-gate 					/*
31440Sstevel@tonic-gate 					 * Determine whether this is the same
31450Sstevel@tonic-gate 					 * file name as originally encountered
31460Sstevel@tonic-gate 					 * so as to provide the most
31470Sstevel@tonic-gate 					 * descriptive diagnostic.
31480Sstevel@tonic-gate 					 */
31494734Sab196087 					errmsg =
31504734Sab196087 					    (strcmp(name, ifl->ifl_name) == 0) ?
31514734Sab196087 					    MSG_INTL(MSG_FIL_MULINC_1) :
31524734Sab196087 					    MSG_INTL(MSG_FIL_MULINC_2);
31531618Srie 					eprintf(ofl->ofl_lml, ERR_WARNING,
31541618Srie 					    errmsg, name, ifl->ifl_name);
31550Sstevel@tonic-gate 				}
31560Sstevel@tonic-gate 				return (ifl);
31570Sstevel@tonic-gate 			}
31580Sstevel@tonic-gate 		}
31590Sstevel@tonic-gate 
31600Sstevel@tonic-gate 		/*
31610Sstevel@tonic-gate 		 * At this point, we know we need the file.  Establish an input
31620Sstevel@tonic-gate 		 * file descriptor and continue processing.
31630Sstevel@tonic-gate 		 */
31640Sstevel@tonic-gate 		ifl = ifl_setup(name, ehdr, elf, flags, ofl, rej);
316510792SRod.Evans@Sun.COM 		if ((ifl == NULL) || (ifl == (Ifl_desc *)S_ERROR))
31660Sstevel@tonic-gate 			return (ifl);
31670Sstevel@tonic-gate 		ifl->ifl_stdev = status.st_dev;
31680Sstevel@tonic-gate 		ifl->ifl_stino = status.st_ino;
31690Sstevel@tonic-gate 
31700Sstevel@tonic-gate 		/*
31710Sstevel@tonic-gate 		 * If -zignore is in effect, mark this file as a potential
31720Sstevel@tonic-gate 		 * candidate (the files use isn't actually determined until
31730Sstevel@tonic-gate 		 * symbol resolution and relocation processing are completed).
31740Sstevel@tonic-gate 		 */
31750Sstevel@tonic-gate 		if (ofl->ofl_flags1 & FLG_OF1_IGNORE)
31760Sstevel@tonic-gate 			ifl->ifl_flags |= FLG_IF_IGNORE;
31770Sstevel@tonic-gate 
31780Sstevel@tonic-gate 		switch (ehdr->e_type) {
31790Sstevel@tonic-gate 		case ET_REL:
31806206Sab196087 			(*ld_targ.t_mr.mr_mach_eflags)(ehdr, ofl);
31810Sstevel@tonic-gate 			error = process_elf(ifl, elf, ofl);
31820Sstevel@tonic-gate 			break;
31830Sstevel@tonic-gate 		case ET_DYN:
31840Sstevel@tonic-gate 			if ((ofl->ofl_flags & FLG_OF_STATIC) ||
31850Sstevel@tonic-gate 			    !(ofl->ofl_flags & FLG_OF_DYNLIBS)) {
31861618Srie 				eprintf(ofl->ofl_lml, ERR_FATAL,
31871618Srie 				    MSG_INTL(MSG_FIL_SOINSTAT), name);
31880Sstevel@tonic-gate 				ofl->ofl_flags |= FLG_OF_FATAL;
31897359SRod.Evans@Sun.COM 				return (NULL);
31900Sstevel@tonic-gate 			}
31910Sstevel@tonic-gate 
31920Sstevel@tonic-gate 			/*
31930Sstevel@tonic-gate 			 * Record any additional shared object information.
31940Sstevel@tonic-gate 			 * If no soname is specified (eg. this file was
31950Sstevel@tonic-gate 			 * derived from a explicit filename declaration on the
31960Sstevel@tonic-gate 			 * command line, ie. bar.so) use the pathname.
31970Sstevel@tonic-gate 			 * This entry may be overridden if the files dynamic
31980Sstevel@tonic-gate 			 * section specifies an DT_SONAME value.
31990Sstevel@tonic-gate 			 */
32000Sstevel@tonic-gate 			if (soname == NULL)
32010Sstevel@tonic-gate 				ifl->ifl_soname = ifl->ifl_name;
32020Sstevel@tonic-gate 			else
32030Sstevel@tonic-gate 				ifl->ifl_soname = soname;
32040Sstevel@tonic-gate 
32050Sstevel@tonic-gate 			/*
32060Sstevel@tonic-gate 			 * If direct bindings, lazy loading, or group
32070Sstevel@tonic-gate 			 * permissions need to be established, mark this object.
32080Sstevel@tonic-gate 			 */
32090Sstevel@tonic-gate 			if (ofl->ofl_flags1 & FLG_OF1_ZDIRECT)
32100Sstevel@tonic-gate 				ifl->ifl_flags |= FLG_IF_DIRECT;
32110Sstevel@tonic-gate 			if (ofl->ofl_flags1 & FLG_OF1_LAZYLD)
32120Sstevel@tonic-gate 				ifl->ifl_flags |= FLG_IF_LAZYLD;
32130Sstevel@tonic-gate 			if (ofl->ofl_flags1 & FLG_OF1_GRPPRM)
32140Sstevel@tonic-gate 				ifl->ifl_flags |= FLG_IF_GRPPRM;
32150Sstevel@tonic-gate 			error = process_elf(ifl, elf, ofl);
32160Sstevel@tonic-gate 
32170Sstevel@tonic-gate 			/*
32180Sstevel@tonic-gate 			 * At this point we know if this file will be
32190Sstevel@tonic-gate 			 * lazyloaded, or whether bindings to it must be direct.
32200Sstevel@tonic-gate 			 * In either case, a syminfo section is required.
32210Sstevel@tonic-gate 			 */
32220Sstevel@tonic-gate 			if (ifl->ifl_flags & (FLG_IF_LAZYLD | FLG_IF_DIRECT))
32230Sstevel@tonic-gate 				ofl->ofl_flags |= FLG_OF_SYMINFO;
32240Sstevel@tonic-gate 
32250Sstevel@tonic-gate 			break;
32260Sstevel@tonic-gate 		default:
32270Sstevel@tonic-gate 			(void) elf_errno();
32280Sstevel@tonic-gate 			_rej.rej_type = SGS_REJ_UNKFILE;
32290Sstevel@tonic-gate 			_rej.rej_name = name;
32306206Sab196087 			DBG_CALL(Dbg_file_rejected(ofl->ofl_lml, &_rej,
32316206Sab196087 			    ld_targ.t_m.m_mach));
32320Sstevel@tonic-gate 			if (rej->rej_type == 0) {
32330Sstevel@tonic-gate 				*rej = _rej;
32340Sstevel@tonic-gate 				rej->rej_name = strdup(_rej.rej_name);
32350Sstevel@tonic-gate 			}
32367359SRod.Evans@Sun.COM 			return (NULL);
32370Sstevel@tonic-gate 		}
32380Sstevel@tonic-gate 		break;
32390Sstevel@tonic-gate 	default:
32400Sstevel@tonic-gate 		(void) elf_errno();
32410Sstevel@tonic-gate 		_rej.rej_type = SGS_REJ_UNKFILE;
32420Sstevel@tonic-gate 		_rej.rej_name = name;
32436206Sab196087 		DBG_CALL(Dbg_file_rejected(ofl->ofl_lml, &_rej,
32446206Sab196087 		    ld_targ.t_m.m_mach));
32450Sstevel@tonic-gate 		if (rej->rej_type == 0) {
32460Sstevel@tonic-gate 			*rej = _rej;
32470Sstevel@tonic-gate 			rej->rej_name = strdup(_rej.rej_name);
32480Sstevel@tonic-gate 		}
32497359SRod.Evans@Sun.COM 		return (NULL);
32500Sstevel@tonic-gate 	}
32510Sstevel@tonic-gate 	if ((error == 0) || (error == S_ERROR))
32520Sstevel@tonic-gate 		return ((Ifl_desc *)error);
32530Sstevel@tonic-gate 	else
32540Sstevel@tonic-gate 		return (ifl);
32550Sstevel@tonic-gate }
32560Sstevel@tonic-gate 
32570Sstevel@tonic-gate /*
32580Sstevel@tonic-gate  * Having successfully opened a file, set up the necessary elf structures to
32590Sstevel@tonic-gate  * process it further.  This small section of processing is slightly different
32600Sstevel@tonic-gate  * from the elf initialization required to process a relocatable object from an
32612850Srie  * archive (see libs.c: ld_process_archive()).
32620Sstevel@tonic-gate  */
32630Sstevel@tonic-gate Ifl_desc *
32642978Srie ld_process_open(const char *opath, const char *ofile, int *fd, Ofl_desc *ofl,
32654716Sab196087     Word flags, Rej_desc *rej)
32660Sstevel@tonic-gate {
32672978Srie 	Elf		*elf;
32682978Srie 	const char	*npath = opath;
32692978Srie 	const char	*nfile = ofile;
32700Sstevel@tonic-gate 
32712978Srie 	if ((elf = elf_begin(*fd, ELF_C_READ, NULL)) == NULL) {
32722978Srie 		eprintf(ofl->ofl_lml, ERR_ELF, MSG_INTL(MSG_ELF_BEGIN), npath);
32730Sstevel@tonic-gate 		ofl->ofl_flags |= FLG_OF_FATAL;
32747359SRod.Evans@Sun.COM 		return (NULL);
32750Sstevel@tonic-gate 	}
32760Sstevel@tonic-gate 
32772978Srie 	/*
32782978Srie 	 * Determine whether the support library wishes to process this open.
32792978Srie 	 * The support library may return:
32802978Srie 	 *   .	a different ELF descriptor (in which case they should have
32812978Srie 	 *	closed the original)
32822978Srie 	 *   .	a different file descriptor (in which case they should have
32832978Srie 	 *	closed the original)
32842978Srie 	 *   .	a different path and file name (presumably associated with
32852978Srie 	 *	a different file descriptor)
32862978Srie 	 *
32872978Srie 	 * A file descriptor of -1, or and ELF descriptor of zero indicates
32882978Srie 	 * the file should be ignored.
32892978Srie 	 */
32902978Srie 	ld_sup_open(ofl, &npath, &nfile, fd, flags, &elf, NULL, 0,
32912978Srie 	    elf_kind(elf));
32922978Srie 
32932978Srie 	if ((*fd == -1) || (elf == NULL))
32947359SRod.Evans@Sun.COM 		return (NULL);
32952978Srie 
32962978Srie 	return (ld_process_ifl(npath, nfile, *fd, elf, flags, ofl, rej));
32970Sstevel@tonic-gate }
32980Sstevel@tonic-gate 
32990Sstevel@tonic-gate /*
33008598SRod.Evans@Sun.COM  * Having successfully mapped a file, set up the necessary elf structures to
33018598SRod.Evans@Sun.COM  * process it further.  This routine is patterned after ld_process_open() and
33028598SRod.Evans@Sun.COM  * is only called by ld.so.1(1) to process a relocatable object.
33038598SRod.Evans@Sun.COM  */
33048598SRod.Evans@Sun.COM Ifl_desc *
33058598SRod.Evans@Sun.COM ld_process_mem(const char *path, const char *file, char *addr, size_t size,
33068598SRod.Evans@Sun.COM     Ofl_desc *ofl, Rej_desc *rej)
33078598SRod.Evans@Sun.COM {
33088598SRod.Evans@Sun.COM 	Elf	*elf;
33098598SRod.Evans@Sun.COM 
33108598SRod.Evans@Sun.COM 	if ((elf = elf_memory(addr, size)) == NULL) {
33118598SRod.Evans@Sun.COM 		eprintf(ofl->ofl_lml, ERR_ELF, MSG_INTL(MSG_ELF_MEMORY), path);
33128598SRod.Evans@Sun.COM 		ofl->ofl_flags |= FLG_OF_FATAL;
33138598SRod.Evans@Sun.COM 		return (0);
33148598SRod.Evans@Sun.COM 	}
33158598SRod.Evans@Sun.COM 
33168598SRod.Evans@Sun.COM 	return (ld_process_ifl(path, file, 0, elf, 0, ofl, rej));
33178598SRod.Evans@Sun.COM }
33188598SRod.Evans@Sun.COM 
33198598SRod.Evans@Sun.COM /*
33200Sstevel@tonic-gate  * Process a required library (i.e. the dependency of a shared object).
33210Sstevel@tonic-gate  * Combine the directory and filename, check the resultant path size, and try
33220Sstevel@tonic-gate  * opening the pathname.
33230Sstevel@tonic-gate  */
33241618Srie static Ifl_desc *
33250Sstevel@tonic-gate process_req_lib(Sdf_desc *sdf, const char *dir, const char *file,
33267463SRod.Evans@Sun.COM     Ofl_desc *ofl, Rej_desc *rej)
33270Sstevel@tonic-gate {
33280Sstevel@tonic-gate 	size_t		dlen, plen;
33290Sstevel@tonic-gate 	int		fd;
33300Sstevel@tonic-gate 	char		path[PATH_MAX];
33310Sstevel@tonic-gate 	const char	*_dir = dir;
33320Sstevel@tonic-gate 
33330Sstevel@tonic-gate 	/*
33340Sstevel@tonic-gate 	 * Determine the sizes of the directory and filename to insure we don't
33350Sstevel@tonic-gate 	 * exceed our buffer.
33360Sstevel@tonic-gate 	 */
33370Sstevel@tonic-gate 	if ((dlen = strlen(dir)) == 0) {
33380Sstevel@tonic-gate 		_dir = MSG_ORIG(MSG_STR_DOT);
33390Sstevel@tonic-gate 		dlen = 1;
33400Sstevel@tonic-gate 	}
33410Sstevel@tonic-gate 	dlen++;
33420Sstevel@tonic-gate 	plen = dlen + strlen(file) + 1;
33430Sstevel@tonic-gate 	if (plen > PATH_MAX) {
33441618Srie 		eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_FIL_PTHTOLONG),
33451618Srie 		    _dir, file);
33460Sstevel@tonic-gate 		ofl->ofl_flags |= FLG_OF_FATAL;
33470Sstevel@tonic-gate 		return (0);
33480Sstevel@tonic-gate 	}
33490Sstevel@tonic-gate 
33500Sstevel@tonic-gate 	/*
33510Sstevel@tonic-gate 	 * Build the entire pathname and try and open the file.
33520Sstevel@tonic-gate 	 */
33530Sstevel@tonic-gate 	(void) strcpy(path, _dir);
33540Sstevel@tonic-gate 	(void) strcat(path, MSG_ORIG(MSG_STR_SLASH));
33550Sstevel@tonic-gate 	(void) strcat(path, file);
33561618Srie 	DBG_CALL(Dbg_libs_req(ofl->ofl_lml, sdf->sdf_name,
33571618Srie 	    sdf->sdf_rfile, path));
33580Sstevel@tonic-gate 
33590Sstevel@tonic-gate 	if ((fd = open(path, O_RDONLY)) == -1)
33600Sstevel@tonic-gate 		return (0);
33610Sstevel@tonic-gate 	else {
33620Sstevel@tonic-gate 		Ifl_desc	*ifl;
33630Sstevel@tonic-gate 		char		*_path;
33640Sstevel@tonic-gate 
336510792SRod.Evans@Sun.COM 		if ((_path = libld_malloc(strlen(path) + 1)) == NULL)
33660Sstevel@tonic-gate 			return ((Ifl_desc *)S_ERROR);
33670Sstevel@tonic-gate 		(void) strcpy(_path, path);
33684716Sab196087 		ifl = ld_process_open(_path, &_path[dlen], &fd, ofl, 0, rej);
33692978Srie 		if (fd != -1)
33702978Srie 			(void) close(fd);
33710Sstevel@tonic-gate 		return (ifl);
33720Sstevel@tonic-gate 	}
33730Sstevel@tonic-gate }
33740Sstevel@tonic-gate 
33750Sstevel@tonic-gate /*
33760Sstevel@tonic-gate  * Finish any library processing.  Walk the list of so's that have been listed
33770Sstevel@tonic-gate  * as "included" by shared objects we have previously processed.  Examine them,
33780Sstevel@tonic-gate  * without adding them as explicit dependents of this program, in order to
33790Sstevel@tonic-gate  * complete our symbol definition process.  The search path rules are:
33800Sstevel@tonic-gate  *
338110792SRod.Evans@Sun.COM  *  -	use any user supplied paths, i.e. LD_LIBRARY_PATH and -L, then
33820Sstevel@tonic-gate  *
338310792SRod.Evans@Sun.COM  *  -	use any RPATH defined within the parent shared object, then
33840Sstevel@tonic-gate  *
338510792SRod.Evans@Sun.COM  *  -	use the default directories, i.e. LIBPATH or -YP.
33860Sstevel@tonic-gate  */
33870Sstevel@tonic-gate uintptr_t
33881618Srie ld_finish_libs(Ofl_desc *ofl)
33890Sstevel@tonic-gate {
33909131SRod.Evans@Sun.COM 	Aliste		idx1;
33910Sstevel@tonic-gate 	Sdf_desc	*sdf;
33920Sstevel@tonic-gate 	Rej_desc	rej = { 0 };
33930Sstevel@tonic-gate 
33940Sstevel@tonic-gate 	/*
33950Sstevel@tonic-gate 	 * Make sure we are back in dynamic mode.
33960Sstevel@tonic-gate 	 */
33970Sstevel@tonic-gate 	ofl->ofl_flags |= FLG_OF_DYNLIBS;
33980Sstevel@tonic-gate 
33999131SRod.Evans@Sun.COM 	for (APLIST_TRAVERSE(ofl->ofl_soneed, idx1, sdf)) {
34009131SRod.Evans@Sun.COM 		Aliste		idx2;
34012850Srie 		char		*path, *slash = NULL;
34020Sstevel@tonic-gate 		int		fd;
34030Sstevel@tonic-gate 		Ifl_desc	*ifl;
34042850Srie 		char		*file = (char *)sdf->sdf_name;
34050Sstevel@tonic-gate 
34060Sstevel@tonic-gate 		/*
34070Sstevel@tonic-gate 		 * See if this file has already been processed.  At the time
34080Sstevel@tonic-gate 		 * this implicit dependency was determined there may still have
34091109Srie 		 * been more explicit dependencies to process.  Note, if we ever
34100Sstevel@tonic-gate 		 * do parse the command line three times we would be able to
34111109Srie 		 * do all this checking when processing the dynamic section.
34120Sstevel@tonic-gate 		 */
34130Sstevel@tonic-gate 		if (sdf->sdf_file)
34140Sstevel@tonic-gate 			continue;
34150Sstevel@tonic-gate 
34169131SRod.Evans@Sun.COM 		for (APLIST_TRAVERSE(ofl->ofl_sos, idx2, ifl)) {
34170Sstevel@tonic-gate 			if (!(ifl->ifl_flags & FLG_IF_NEEDSTR) &&
34180Sstevel@tonic-gate 			    (strcmp(file, ifl->ifl_soname) == 0)) {
34190Sstevel@tonic-gate 				sdf->sdf_file = ifl;
34200Sstevel@tonic-gate 				break;
34210Sstevel@tonic-gate 			}
34220Sstevel@tonic-gate 		}
34230Sstevel@tonic-gate 		if (sdf->sdf_file)
34240Sstevel@tonic-gate 			continue;
34250Sstevel@tonic-gate 
34260Sstevel@tonic-gate 		/*
34272850Srie 		 * If the current path name element embeds a "/", then it's to
34282850Srie 		 * be taken "as is", with no searching involved.  Process all
34292850Srie 		 * "/" occurrences, so that we can deduce the base file name.
34300Sstevel@tonic-gate 		 */
34312850Srie 		for (path = file; *path; path++) {
34320Sstevel@tonic-gate 			if (*path == '/')
34332850Srie 				slash = path;
34342850Srie 		}
34352850Srie 		if (slash) {
34361618Srie 			DBG_CALL(Dbg_libs_req(ofl->ofl_lml, sdf->sdf_name,
34371618Srie 			    sdf->sdf_rfile, file));
34380Sstevel@tonic-gate 			if ((fd = open(file, O_RDONLY)) == -1) {
34391618Srie 				eprintf(ofl->ofl_lml, ERR_WARNING,
34401618Srie 				    MSG_INTL(MSG_FIL_NOTFOUND), file,
34411618Srie 				    sdf->sdf_rfile);
34420Sstevel@tonic-gate 			} else {
34430Sstevel@tonic-gate 				Rej_desc	_rej = { 0 };
34440Sstevel@tonic-gate 
34452978Srie 				ifl = ld_process_open(file, ++slash, &fd, ofl,
34464716Sab196087 				    0, &_rej);
34472978Srie 				if (fd != -1)
34482978Srie 					(void) close(fd);
34497359SRod.Evans@Sun.COM 				if (ifl == (Ifl_desc *)S_ERROR)
34507359SRod.Evans@Sun.COM 					return (S_ERROR);
34510Sstevel@tonic-gate 
34520Sstevel@tonic-gate 				if (_rej.rej_type) {
34534734Sab196087 					Conv_reject_desc_buf_t rej_buf;
34544734Sab196087 
34551618Srie 					eprintf(ofl->ofl_lml, ERR_WARNING,
34560Sstevel@tonic-gate 					    MSG_INTL(reject[_rej.rej_type]),
34570Sstevel@tonic-gate 					    _rej.rej_name ? rej.rej_name :
34580Sstevel@tonic-gate 					    MSG_INTL(MSG_STR_UNKNOWN),
34596206Sab196087 					    conv_reject_desc(&_rej, &rej_buf,
34606206Sab196087 					    ld_targ.t_m.m_mach));
34610Sstevel@tonic-gate 				} else
34620Sstevel@tonic-gate 					sdf->sdf_file = ifl;
34630Sstevel@tonic-gate 			}
34640Sstevel@tonic-gate 			continue;
34650Sstevel@tonic-gate 		}
34660Sstevel@tonic-gate 
34670Sstevel@tonic-gate 		/*
34680Sstevel@tonic-gate 		 * Now search for this file in any user defined directories.
34690Sstevel@tonic-gate 		 */
34709131SRod.Evans@Sun.COM 		for (APLIST_TRAVERSE(ofl->ofl_ulibdirs, idx2, path)) {
34710Sstevel@tonic-gate 			Rej_desc	_rej = { 0 };
34720Sstevel@tonic-gate 
34730Sstevel@tonic-gate 			ifl = process_req_lib(sdf, path, file, ofl, &_rej);
34740Sstevel@tonic-gate 			if (ifl == (Ifl_desc *)S_ERROR) {
34750Sstevel@tonic-gate 				return (S_ERROR);
34760Sstevel@tonic-gate 			}
34770Sstevel@tonic-gate 			if (_rej.rej_type) {
34780Sstevel@tonic-gate 				if (rej.rej_type == 0) {
34790Sstevel@tonic-gate 					rej = _rej;
34800Sstevel@tonic-gate 					rej.rej_name = strdup(_rej.rej_name);
34810Sstevel@tonic-gate 				}
34820Sstevel@tonic-gate 			}
34830Sstevel@tonic-gate 			if (ifl) {
34840Sstevel@tonic-gate 				sdf->sdf_file = ifl;
34850Sstevel@tonic-gate 				break;
34860Sstevel@tonic-gate 			}
34870Sstevel@tonic-gate 		}
34880Sstevel@tonic-gate 		if (sdf->sdf_file)
34890Sstevel@tonic-gate 			continue;
34900Sstevel@tonic-gate 
34910Sstevel@tonic-gate 		/*
34920Sstevel@tonic-gate 		 * Next use the local rules defined within the parent shared
34930Sstevel@tonic-gate 		 * object.
34940Sstevel@tonic-gate 		 */
34950Sstevel@tonic-gate 		if (sdf->sdf_rpath != NULL) {
34960Sstevel@tonic-gate 			char	*rpath, *next;
34970Sstevel@tonic-gate 
34980Sstevel@tonic-gate 			rpath = libld_malloc(strlen(sdf->sdf_rpath) + 1);
349910792SRod.Evans@Sun.COM 			if (rpath == NULL)
35000Sstevel@tonic-gate 				return (S_ERROR);
35010Sstevel@tonic-gate 			(void) strcpy(rpath, sdf->sdf_rpath);
35021618Srie 			DBG_CALL(Dbg_libs_path(ofl->ofl_lml, rpath,
35031618Srie 			    LA_SER_RUNPATH, sdf->sdf_rfile));
35040Sstevel@tonic-gate 			if ((path = strtok_r(rpath,
35050Sstevel@tonic-gate 			    MSG_ORIG(MSG_STR_COLON), &next)) != NULL) {
35060Sstevel@tonic-gate 				do {
35070Sstevel@tonic-gate 					Rej_desc	_rej = { 0 };
35080Sstevel@tonic-gate 
35090Sstevel@tonic-gate 					path = expand(sdf->sdf_rfile, path,
35100Sstevel@tonic-gate 					    &next);
35110Sstevel@tonic-gate 
35120Sstevel@tonic-gate 					ifl = process_req_lib(sdf, path,
35134716Sab196087 					    file, ofl, &_rej);
35140Sstevel@tonic-gate 					if (ifl == (Ifl_desc *)S_ERROR) {
35150Sstevel@tonic-gate 						return (S_ERROR);
35160Sstevel@tonic-gate 					}
35174716Sab196087 					if ((_rej.rej_type) &&
35184716Sab196087 					    (rej.rej_type == 0)) {
35194716Sab196087 						rej = _rej;
35204716Sab196087 						rej.rej_name =
35214716Sab196087 						    strdup(_rej.rej_name);
35220Sstevel@tonic-gate 					}
35230Sstevel@tonic-gate 					if (ifl) {
35240Sstevel@tonic-gate 						sdf->sdf_file = ifl;
35250Sstevel@tonic-gate 						break;
35260Sstevel@tonic-gate 					}
35270Sstevel@tonic-gate 				} while ((path = strtok_r(NULL,
35280Sstevel@tonic-gate 				    MSG_ORIG(MSG_STR_COLON), &next)) != NULL);
35290Sstevel@tonic-gate 			}
35300Sstevel@tonic-gate 		}
35310Sstevel@tonic-gate 		if (sdf->sdf_file)
35320Sstevel@tonic-gate 			continue;
35330Sstevel@tonic-gate 
35340Sstevel@tonic-gate 		/*
35350Sstevel@tonic-gate 		 * Finally try the default library search directories.
35360Sstevel@tonic-gate 		 */
35379131SRod.Evans@Sun.COM 		for (APLIST_TRAVERSE(ofl->ofl_dlibdirs, idx2, path)) {
35380Sstevel@tonic-gate 			Rej_desc	_rej = { 0 };
35390Sstevel@tonic-gate 
35400Sstevel@tonic-gate 			ifl = process_req_lib(sdf, path, file, ofl, &rej);
35410Sstevel@tonic-gate 			if (ifl == (Ifl_desc *)S_ERROR) {
35420Sstevel@tonic-gate 				return (S_ERROR);
35430Sstevel@tonic-gate 			}
35440Sstevel@tonic-gate 			if (_rej.rej_type) {
35450Sstevel@tonic-gate 				if (rej.rej_type == 0) {
35460Sstevel@tonic-gate 					rej = _rej;
35470Sstevel@tonic-gate 					rej.rej_name = strdup(_rej.rej_name);
35480Sstevel@tonic-gate 				}
35490Sstevel@tonic-gate 			}
35500Sstevel@tonic-gate 			if (ifl) {
35510Sstevel@tonic-gate 				sdf->sdf_file = ifl;
35520Sstevel@tonic-gate 				break;
35530Sstevel@tonic-gate 			}
35540Sstevel@tonic-gate 		}
35550Sstevel@tonic-gate 		if (sdf->sdf_file)
35560Sstevel@tonic-gate 			continue;
35570Sstevel@tonic-gate 
35580Sstevel@tonic-gate 		/*
35590Sstevel@tonic-gate 		 * If we've got this far we haven't found the shared object.
35600Sstevel@tonic-gate 		 * If an object was found, but was rejected for some reason,
35610Sstevel@tonic-gate 		 * print a diagnostic to that effect, otherwise generate a
35620Sstevel@tonic-gate 		 * generic "not found" diagnostic.
35630Sstevel@tonic-gate 		 */
35640Sstevel@tonic-gate 		if (rej.rej_type) {
35654734Sab196087 			Conv_reject_desc_buf_t rej_buf;
35664734Sab196087 
35671618Srie 			eprintf(ofl->ofl_lml, ERR_WARNING,
35681618Srie 			    MSG_INTL(reject[rej.rej_type]),
35690Sstevel@tonic-gate 			    rej.rej_name ? rej.rej_name :
35704734Sab196087 			    MSG_INTL(MSG_STR_UNKNOWN),
35716206Sab196087 			    conv_reject_desc(&rej, &rej_buf,
35726206Sab196087 			    ld_targ.t_m.m_mach));
35730Sstevel@tonic-gate 		} else {
35741618Srie 			eprintf(ofl->ofl_lml, ERR_WARNING,
35751618Srie 			    MSG_INTL(MSG_FIL_NOTFOUND), file, sdf->sdf_rfile);
35760Sstevel@tonic-gate 		}
35770Sstevel@tonic-gate 	}
35780Sstevel@tonic-gate 
35790Sstevel@tonic-gate 	/*
35800Sstevel@tonic-gate 	 * Finally, now that all objects have been input, make sure any version
35810Sstevel@tonic-gate 	 * requirements have been met.
35820Sstevel@tonic-gate 	 */
35831618Srie 	return (ld_vers_verify(ofl));
35840Sstevel@tonic-gate }
3585