xref: /onnv-gate/usr/src/cmd/sgs/libld/common/files.c (revision 13074:787bf65954d0)
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  *
2612254SAli.Bahrami@Oracle.COM  * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
270Sstevel@tonic-gate  */
280Sstevel@tonic-gate 
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate  * Processing of relocatable objects and shared objects.
310Sstevel@tonic-gate  */
326206Sab196087 
336206Sab196087 #define	ELF_TARGET_AMD64
346206Sab196087 #define	ELF_TARGET_SPARC
356206Sab196087 
360Sstevel@tonic-gate #include	<stdio.h>
370Sstevel@tonic-gate #include	<string.h>
380Sstevel@tonic-gate #include	<fcntl.h>
390Sstevel@tonic-gate #include	<unistd.h>
400Sstevel@tonic-gate #include	<link.h>
410Sstevel@tonic-gate #include	<limits.h>
420Sstevel@tonic-gate #include	<sys/stat.h>
430Sstevel@tonic-gate #include	<sys/systeminfo.h>
440Sstevel@tonic-gate #include	<debug.h>
450Sstevel@tonic-gate #include	<msg.h>
460Sstevel@tonic-gate #include	<_libld.h>
470Sstevel@tonic-gate 
480Sstevel@tonic-gate /*
490Sstevel@tonic-gate  * Decide if we can link against this input file.
500Sstevel@tonic-gate  */
510Sstevel@tonic-gate static int
ifl_verify(Ehdr * ehdr,Ofl_desc * ofl,Rej_desc * rej)527463SRod.Evans@Sun.COM ifl_verify(Ehdr *ehdr, Ofl_desc *ofl, Rej_desc *rej)
530Sstevel@tonic-gate {
540Sstevel@tonic-gate 	/*
550Sstevel@tonic-gate 	 * Check the validity of the elf header information for compatibility
560Sstevel@tonic-gate 	 * with this machine and our own internal elf library.
570Sstevel@tonic-gate 	 */
586206Sab196087 	if ((ehdr->e_machine != ld_targ.t_m.m_mach) &&
596206Sab196087 	    ((ehdr->e_machine != ld_targ.t_m.m_machplus) &&
606206Sab196087 	    ((ehdr->e_flags & ld_targ.t_m.m_flagsplus) == 0))) {
610Sstevel@tonic-gate 		rej->rej_type = SGS_REJ_MACH;
620Sstevel@tonic-gate 		rej->rej_info = (uint_t)ehdr->e_machine;
630Sstevel@tonic-gate 		return (0);
640Sstevel@tonic-gate 	}
656206Sab196087 	if (ehdr->e_ident[EI_DATA] != ld_targ.t_m.m_data) {
660Sstevel@tonic-gate 		rej->rej_type = SGS_REJ_DATA;
670Sstevel@tonic-gate 		rej->rej_info = (uint_t)ehdr->e_ident[EI_DATA];
680Sstevel@tonic-gate 		return (0);
690Sstevel@tonic-gate 	}
701618Srie 	if (ehdr->e_version > ofl->ofl_dehdr->e_version) {
710Sstevel@tonic-gate 		rej->rej_type = SGS_REJ_VERSION;
720Sstevel@tonic-gate 		rej->rej_info = (uint_t)ehdr->e_version;
730Sstevel@tonic-gate 		return (0);
740Sstevel@tonic-gate 	}
750Sstevel@tonic-gate 	return (1);
760Sstevel@tonic-gate }
770Sstevel@tonic-gate 
780Sstevel@tonic-gate /*
790Sstevel@tonic-gate  * Check sanity of file header and allocate an infile descriptor
800Sstevel@tonic-gate  * for the file being processed.
810Sstevel@tonic-gate  */
821618Srie static Ifl_desc *
ifl_setup(const char * name,Ehdr * ehdr,Elf * elf,Word flags,Ofl_desc * ofl,Rej_desc * rej)834716Sab196087 ifl_setup(const char *name, Ehdr *ehdr, Elf *elf, Word flags, Ofl_desc *ofl,
840Sstevel@tonic-gate     Rej_desc *rej)
850Sstevel@tonic-gate {
860Sstevel@tonic-gate 	Ifl_desc	*ifl;
870Sstevel@tonic-gate 	Rej_desc	_rej = { 0 };
880Sstevel@tonic-gate 
890Sstevel@tonic-gate 	if (ifl_verify(ehdr, ofl, &_rej) == 0) {
900Sstevel@tonic-gate 		_rej.rej_name = name;
916206Sab196087 		DBG_CALL(Dbg_file_rejected(ofl->ofl_lml, &_rej,
926206Sab196087 		    ld_targ.t_m.m_mach));
930Sstevel@tonic-gate 		if (rej->rej_type == 0) {
940Sstevel@tonic-gate 			*rej = _rej;
950Sstevel@tonic-gate 			rej->rej_name = strdup(_rej.rej_name);
960Sstevel@tonic-gate 		}
970Sstevel@tonic-gate 		return (0);
980Sstevel@tonic-gate 	}
990Sstevel@tonic-gate 
10010792SRod.Evans@Sun.COM 	if ((ifl = libld_calloc(1, sizeof (Ifl_desc))) == NULL)
1010Sstevel@tonic-gate 		return ((Ifl_desc *)S_ERROR);
1020Sstevel@tonic-gate 	ifl->ifl_name = name;
1030Sstevel@tonic-gate 	ifl->ifl_ehdr = ehdr;
1040Sstevel@tonic-gate 	ifl->ifl_elf = elf;
1050Sstevel@tonic-gate 	ifl->ifl_flags = flags;
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate 	/*
1080Sstevel@tonic-gate 	 * Is this file using 'extended Section Indexes'.  If so, use the
1090Sstevel@tonic-gate 	 * e_shnum & e_shstrndx which can be found at:
1100Sstevel@tonic-gate 	 *
1110Sstevel@tonic-gate 	 *	e_shnum == Shdr[0].sh_size
1120Sstevel@tonic-gate 	 *	e_shstrndx == Shdr[0].sh_link
1130Sstevel@tonic-gate 	 */
1140Sstevel@tonic-gate 	if ((ehdr->e_shnum == 0) && (ehdr->e_shoff != 0)) {
1150Sstevel@tonic-gate 		Elf_Scn	*scn;
1160Sstevel@tonic-gate 		Shdr	*shdr0;
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate 		if ((scn = elf_getscn(elf, 0)) == NULL) {
119*13074SAli.Bahrami@Oracle.COM 			ld_eprintf(ofl, ERR_ELF, MSG_INTL(MSG_ELF_GETSCN),
120*13074SAli.Bahrami@Oracle.COM 			    name);
1210Sstevel@tonic-gate 			return ((Ifl_desc *)S_ERROR);
1220Sstevel@tonic-gate 		}
1230Sstevel@tonic-gate 		if ((shdr0 = elf_getshdr(scn)) == NULL) {
124*13074SAli.Bahrami@Oracle.COM 			ld_eprintf(ofl, ERR_ELF, MSG_INTL(MSG_ELF_GETSHDR),
125*13074SAli.Bahrami@Oracle.COM 			    name);
1260Sstevel@tonic-gate 			return ((Ifl_desc *)S_ERROR);
1270Sstevel@tonic-gate 		}
1280Sstevel@tonic-gate 		ifl->ifl_shnum = (Word)shdr0->sh_size;
1290Sstevel@tonic-gate 		if (ehdr->e_shstrndx == SHN_XINDEX)
1300Sstevel@tonic-gate 			ifl->ifl_shstrndx = shdr0->sh_link;
1310Sstevel@tonic-gate 		else
1320Sstevel@tonic-gate 			ifl->ifl_shstrndx = ehdr->e_shstrndx;
1330Sstevel@tonic-gate 	} else {
1340Sstevel@tonic-gate 		ifl->ifl_shnum = ehdr->e_shnum;
1350Sstevel@tonic-gate 		ifl->ifl_shstrndx = ehdr->e_shstrndx;
1360Sstevel@tonic-gate 	}
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate 	if ((ifl->ifl_isdesc = libld_calloc(ifl->ifl_shnum,
13910792SRod.Evans@Sun.COM 	    sizeof (Is_desc *))) == NULL)
1400Sstevel@tonic-gate 		return ((Ifl_desc *)S_ERROR);
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate 	/*
1430Sstevel@tonic-gate 	 * Record this new input file on the shared object or relocatable
1440Sstevel@tonic-gate 	 * object input file list.
1450Sstevel@tonic-gate 	 */
1460Sstevel@tonic-gate 	if (ifl->ifl_ehdr->e_type == ET_DYN) {
1479131SRod.Evans@Sun.COM 		if (aplist_append(&ofl->ofl_sos, ifl, AL_CNT_OFL_LIBS) == NULL)
1489131SRod.Evans@Sun.COM 			return ((Ifl_desc *)S_ERROR);
1490Sstevel@tonic-gate 	} else {
1509131SRod.Evans@Sun.COM 		if (aplist_append(&ofl->ofl_objs, ifl, AL_CNT_OFL_OBJS) == NULL)
1519131SRod.Evans@Sun.COM 			return ((Ifl_desc *)S_ERROR);
1520Sstevel@tonic-gate 	}
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate 	return (ifl);
1550Sstevel@tonic-gate }
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate /*
1580Sstevel@tonic-gate  * Process a generic section.  The appropriate section information is added
1590Sstevel@tonic-gate  * to the files input descriptor list.
1600Sstevel@tonic-gate  */
1611618Srie static uintptr_t
process_section(const char * name,Ifl_desc * ifl,Shdr * shdr,Elf_Scn * scn,Word ndx,int ident,Ofl_desc * ofl)1620Sstevel@tonic-gate process_section(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
1630Sstevel@tonic-gate     Word ndx, int ident, Ofl_desc *ofl)
1640Sstevel@tonic-gate {
1650Sstevel@tonic-gate 	Is_desc	*isp;
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate 	/*
1680Sstevel@tonic-gate 	 * Create a new input section descriptor.  If this is a NOBITS
1690Sstevel@tonic-gate 	 * section elf_getdata() will still create a data buffer (the buffer
1700Sstevel@tonic-gate 	 * will be null and the size will reflect the actual memory size).
1710Sstevel@tonic-gate 	 */
17210792SRod.Evans@Sun.COM 	if ((isp = libld_calloc(sizeof (Is_desc), 1)) == NULL)
1730Sstevel@tonic-gate 		return (S_ERROR);
1740Sstevel@tonic-gate 	isp->is_shdr = shdr;
1750Sstevel@tonic-gate 	isp->is_file = ifl;
1760Sstevel@tonic-gate 	isp->is_name = name;
1770Sstevel@tonic-gate 	isp->is_scnndx = ndx;
178574Sseizo 	isp->is_flags = FLG_IS_EXTERNAL;
1797463SRod.Evans@Sun.COM 	isp->is_keyident = ident;
1807463SRod.Evans@Sun.COM 
1810Sstevel@tonic-gate 	if ((isp->is_indata = elf_getdata(scn, NULL)) == NULL) {
182*13074SAli.Bahrami@Oracle.COM 		ld_eprintf(ofl, ERR_ELF, MSG_INTL(MSG_ELF_GETDATA),
1831618Srie 		    ifl->ifl_name);
1840Sstevel@tonic-gate 		return (0);
1850Sstevel@tonic-gate 	}
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate 	if ((shdr->sh_flags & SHF_EXCLUDE) &&
1880Sstevel@tonic-gate 	    ((ofl->ofl_flags & FLG_OF_RELOBJ) == 0)) {
1890Sstevel@tonic-gate 		isp->is_flags |= FLG_IS_DISCARD;
1900Sstevel@tonic-gate 	}
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate 	/*
1930Sstevel@tonic-gate 	 * Add the new input section to the files input section list and
1947463SRod.Evans@Sun.COM 	 * flag whether the section needs placing in an output section.  This
1957463SRod.Evans@Sun.COM 	 * placement is deferred until all input section processing has been
1967463SRod.Evans@Sun.COM 	 * completed, as SHT_GROUP sections can provide information that will
1977463SRod.Evans@Sun.COM 	 * affect how other sections within the file should be placed.
1980Sstevel@tonic-gate 	 */
1990Sstevel@tonic-gate 	ifl->ifl_isdesc[ndx] = isp;
2000Sstevel@tonic-gate 
2017463SRod.Evans@Sun.COM 	if (ident) {
2027463SRod.Evans@Sun.COM 		if (shdr->sh_flags & ALL_SHF_ORDER) {
2037463SRod.Evans@Sun.COM 			isp->is_flags |= FLG_IS_ORDERED;
2047463SRod.Evans@Sun.COM 			ifl->ifl_flags |= FLG_IF_ORDERED;
2052647Srie 		}
2067463SRod.Evans@Sun.COM 		isp->is_flags |= FLG_IS_PLACE;
2070Sstevel@tonic-gate 	}
2080Sstevel@tonic-gate 	return (1);
2090Sstevel@tonic-gate }
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate /*
2120Sstevel@tonic-gate  * Determine the software capabilities of the object being built from the
2130Sstevel@tonic-gate  * capabilities of the input relocatable objects.   One software capability
2140Sstevel@tonic-gate  * is presently recognized, and represented with the following (sys/elf.h):
2150Sstevel@tonic-gate  *
2160Sstevel@tonic-gate  *   SF1_SUNW_FPKNWN	use/non-use of frame pointer is known, and
2170Sstevel@tonic-gate  *   SF1_SUNW_FPUSED    the frame pointer is in use.
2180Sstevel@tonic-gate  *
2190Sstevel@tonic-gate  * The resolution of the present fame pointer state, and the capabilities
2200Sstevel@tonic-gate  * provided by a new input relocatable object are:
2210Sstevel@tonic-gate  *
2220Sstevel@tonic-gate  *                              new input relocatable object
2230Sstevel@tonic-gate  *
2240Sstevel@tonic-gate  *      present      |  SF1_SUNW_FPKNWN  |  SF1_SUNW_FPKNWN  |    <unknown>
2250Sstevel@tonic-gate  *       state       |  SF1_SUNW_FPUSED  |                   |
2260Sstevel@tonic-gate  *  ---------------------------------------------------------------------------
2270Sstevel@tonic-gate  *  SF1_SUNW_FPKNWN  |  SF1_SUNW_FPKNWN  |  SF1_SUNW_FPKNWN  |  SF1_SUNW_FPKNWN
2280Sstevel@tonic-gate  *  SF1_SUNW_FPUSED  |  SF1_SUNW_FPUSED  |                   |  SF1_SUNW_FPUSED
2290Sstevel@tonic-gate  *  ---------------------------------------------------------------------------
2300Sstevel@tonic-gate  *  SF1_SUNW_FPKNWN  |  SF1_SUNW_FPKNWN  |  SF1_SUNW_FPKNWN  |  SF1_SUNW_FPKNWN
2310Sstevel@tonic-gate  *                   |                   |                   |
2320Sstevel@tonic-gate  *  ---------------------------------------------------------------------------
2330Sstevel@tonic-gate  *     <unknown>     |  SF1_SUNW_FPKNWN  |  SF1_SUNW_FPKNWN  |    <unknown>
2340Sstevel@tonic-gate  *                   |  SF1_SUNW_FPUSED  |                   |
2350Sstevel@tonic-gate  */
2360Sstevel@tonic-gate static void
sf1_cap(Ofl_desc * ofl,Xword val,Ifl_desc * ifl,Is_desc * cisp)2379878SAli.Bahrami@Sun.COM sf1_cap(Ofl_desc *ofl, Xword val, Ifl_desc *ifl, Is_desc *cisp)
2380Sstevel@tonic-gate {
23911734SAli.Bahrami@Sun.COM #define	FP_FLAGS	(SF1_SUNW_FPKNWN | SF1_SUNW_FPUSED)
24011734SAli.Bahrami@Sun.COM 
2410Sstevel@tonic-gate 	Xword	badval;
2420Sstevel@tonic-gate 
2430Sstevel@tonic-gate 	/*
24411827SRod.Evans@Sun.COM 	 * If a mapfile has established definitions to override any object
24511827SRod.Evans@Sun.COM 	 * capabilities, ignore any new object capabilities.
2460Sstevel@tonic-gate 	 */
24711734SAli.Bahrami@Sun.COM 	if (ofl->ofl_flags1 & FLG_OF1_OVSFCAP1) {
24811827SRod.Evans@Sun.COM 		DBG_CALL(Dbg_cap_val_entry(ofl->ofl_lml, DBG_STATE_IGNORED,
24911734SAli.Bahrami@Sun.COM 		    CA_SUNW_SF_1, val, ld_targ.t_m.m_mach));
2500Sstevel@tonic-gate 		return;
2510Sstevel@tonic-gate 	}
2520Sstevel@tonic-gate 
2537833SRod.Evans@Sun.COM #if	!defined(_ELF64)
25411827SRod.Evans@Sun.COM 	if (ifl && (ifl->ifl_ehdr->e_type == ET_REL)) {
2557833SRod.Evans@Sun.COM 		/*
2567833SRod.Evans@Sun.COM 		 * The SF1_SUNW_ADDR32 is only meaningful when building a 64-bit
2577833SRod.Evans@Sun.COM 		 * object.  Warn the user, and remove the setting, if we're
2587833SRod.Evans@Sun.COM 		 * building a 32-bit object.
2597833SRod.Evans@Sun.COM 		 */
2607833SRod.Evans@Sun.COM 		if (val & SF1_SUNW_ADDR32) {
261*13074SAli.Bahrami@Oracle.COM 			ld_eprintf(ofl, ERR_WARNING,
2629878SAli.Bahrami@Sun.COM 			    MSG_INTL(MSG_FIL_INADDR32SF1), ifl->ifl_name,
2639878SAli.Bahrami@Sun.COM 			    EC_WORD(cisp->is_scnndx), cisp->is_name);
2647833SRod.Evans@Sun.COM 			val &= ~SF1_SUNW_ADDR32;
2657833SRod.Evans@Sun.COM 		}
2667833SRod.Evans@Sun.COM 	}
2677833SRod.Evans@Sun.COM #endif
2680Sstevel@tonic-gate 	/*
2690Sstevel@tonic-gate 	 * If this object doesn't specify any capabilities, ignore it, and
2700Sstevel@tonic-gate 	 * leave the state as is.
2710Sstevel@tonic-gate 	 */
2720Sstevel@tonic-gate 	if (val == 0)
2730Sstevel@tonic-gate 		return;
2740Sstevel@tonic-gate 
2750Sstevel@tonic-gate 	/*
2760Sstevel@tonic-gate 	 * Make sure we only accept known software capabilities.  Note, that
2770Sstevel@tonic-gate 	 * an F1_SUNW_FPUSED by itself is viewed as bad practice.
2780Sstevel@tonic-gate 	 */
2790Sstevel@tonic-gate 	if ((badval = (val & ~SF1_SUNW_MASK)) != 0) {
280*13074SAli.Bahrami@Oracle.COM 		ld_eprintf(ofl, ERR_WARNING, MSG_INTL(MSG_FIL_BADSF1),
2819878SAli.Bahrami@Sun.COM 		    ifl->ifl_name, EC_WORD(cisp->is_scnndx), cisp->is_name,
2829878SAli.Bahrami@Sun.COM 		    EC_XWORD(badval));
2830Sstevel@tonic-gate 		val &= SF1_SUNW_MASK;
2840Sstevel@tonic-gate 	}
28511734SAli.Bahrami@Sun.COM 	if ((val & FP_FLAGS) == SF1_SUNW_FPUSED) {
286*13074SAli.Bahrami@Oracle.COM 		ld_eprintf(ofl, ERR_WARNING, MSG_INTL(MSG_FIL_BADSF1),
2879878SAli.Bahrami@Sun.COM 		    ifl->ifl_name, EC_WORD(cisp->is_scnndx), cisp->is_name,
2889878SAli.Bahrami@Sun.COM 		    EC_XWORD(val));
2890Sstevel@tonic-gate 		return;
2900Sstevel@tonic-gate 	}
2910Sstevel@tonic-gate 
2927833SRod.Evans@Sun.COM 	/*
2937833SRod.Evans@Sun.COM 	 * If the input file is not a relocatable object, then we're only here
2947833SRod.Evans@Sun.COM 	 * to warn the user of any questionable capabilities.
2957833SRod.Evans@Sun.COM 	 */
2967833SRod.Evans@Sun.COM 	if (ifl->ifl_ehdr->e_type != ET_REL) {
2977833SRod.Evans@Sun.COM #if	defined(_ELF64)
2987833SRod.Evans@Sun.COM 		/*
2997833SRod.Evans@Sun.COM 		 * If we're building a 64-bit executable, and we come across a
3007833SRod.Evans@Sun.COM 		 * dependency that requires a restricted address space, then
3017833SRod.Evans@Sun.COM 		 * that dependencies requirement can only be satisfied if the
3027833SRod.Evans@Sun.COM 		 * executable triggers the restricted address space.  This is a
3037833SRod.Evans@Sun.COM 		 * warning rather than a fatal error, as the possibility exists
3047833SRod.Evans@Sun.COM 		 * that an appropriate dependency will be provided at runtime.
3057833SRod.Evans@Sun.COM 		 * The runtime linker will refuse to use this dependency.
3067833SRod.Evans@Sun.COM 		 */
3077833SRod.Evans@Sun.COM 		if ((val & SF1_SUNW_ADDR32) && (ofl->ofl_flags & FLG_OF_EXEC) &&
30811827SRod.Evans@Sun.COM 		    ((ofl->ofl_ocapset.oc_sf_1.cm_val &
30911734SAli.Bahrami@Sun.COM 		    SF1_SUNW_ADDR32) == 0)) {
310*13074SAli.Bahrami@Oracle.COM 			ld_eprintf(ofl, ERR_WARNING,
3119878SAli.Bahrami@Sun.COM 			    MSG_INTL(MSG_FIL_EXADDR32SF1), ifl->ifl_name,
3129878SAli.Bahrami@Sun.COM 			    EC_WORD(cisp->is_scnndx), cisp->is_name);
3137833SRod.Evans@Sun.COM 		}
3147833SRod.Evans@Sun.COM #endif
3157833SRod.Evans@Sun.COM 		return;
3167833SRod.Evans@Sun.COM 	}
3177833SRod.Evans@Sun.COM 
31811734SAli.Bahrami@Sun.COM 	if (DBG_ENABLED) {
31911827SRod.Evans@Sun.COM 		Dbg_cap_val_entry(ofl->ofl_lml, DBG_STATE_CURRENT, CA_SUNW_SF_1,
32011827SRod.Evans@Sun.COM 		    ofl->ofl_ocapset.oc_sf_1.cm_val, ld_targ.t_m.m_mach);
32111827SRod.Evans@Sun.COM 		Dbg_cap_val_entry(ofl->ofl_lml, DBG_STATE_NEW, CA_SUNW_SF_1,
32211734SAli.Bahrami@Sun.COM 		    val, ld_targ.t_m.m_mach);
32311734SAli.Bahrami@Sun.COM 	}
3240Sstevel@tonic-gate 
3250Sstevel@tonic-gate 	/*
3260Sstevel@tonic-gate 	 * Determine the resolution of the present frame pointer and the
3270Sstevel@tonic-gate 	 * new input relocatable objects frame pointer.
3280Sstevel@tonic-gate 	 */
32911827SRod.Evans@Sun.COM 	if ((ofl->ofl_ocapset.oc_sf_1.cm_val & FP_FLAGS) == FP_FLAGS) {
3300Sstevel@tonic-gate 		/*
3310Sstevel@tonic-gate 		 * If the new relocatable object isn't using a frame pointer,
3320Sstevel@tonic-gate 		 * reduce the present state to unused.
3330Sstevel@tonic-gate 		 */
33411734SAli.Bahrami@Sun.COM 		if ((val & FP_FLAGS) != FP_FLAGS)
33511827SRod.Evans@Sun.COM 			ofl->ofl_ocapset.oc_sf_1.cm_val &= ~SF1_SUNW_FPUSED;
33611734SAli.Bahrami@Sun.COM 
33711734SAli.Bahrami@Sun.COM 		/*
33811734SAli.Bahrami@Sun.COM 		 * Having processed the frame pointer bits, remove them from
33911734SAli.Bahrami@Sun.COM 		 * the value so they don't get OR'd in below.
34011734SAli.Bahrami@Sun.COM 		 */
34111734SAli.Bahrami@Sun.COM 		val &= ~FP_FLAGS;
3420Sstevel@tonic-gate 
34311827SRod.Evans@Sun.COM 	} else if ((ofl->ofl_ocapset.oc_sf_1.cm_val & SF1_SUNW_FPKNWN) == 0) {
3440Sstevel@tonic-gate 		/*
34511734SAli.Bahrami@Sun.COM 		 * If the present frame pointer state is unknown, mask it out
34611734SAli.Bahrami@Sun.COM 		 * and allow the values from the new relocatable object
34711734SAli.Bahrami@Sun.COM 		 * to overwrite them.
3480Sstevel@tonic-gate 		 */
34911827SRod.Evans@Sun.COM 		ofl->ofl_ocapset.oc_sf_1.cm_val &= ~FP_FLAGS;
35011734SAli.Bahrami@Sun.COM 	} else {
35111734SAli.Bahrami@Sun.COM 		/* Do not take the frame pointer flags from the object */
35211734SAli.Bahrami@Sun.COM 		val &= ~FP_FLAGS;
3530Sstevel@tonic-gate 	}
3540Sstevel@tonic-gate 
35511827SRod.Evans@Sun.COM 	ofl->ofl_ocapset.oc_sf_1.cm_val |= val;
35611827SRod.Evans@Sun.COM 
35711827SRod.Evans@Sun.COM 	DBG_CALL(Dbg_cap_val_entry(ofl->ofl_lml, DBG_STATE_RESOLVED,
35811827SRod.Evans@Sun.COM 	    CA_SUNW_SF_1, ofl->ofl_ocapset.oc_sf_1.cm_val, ld_targ.t_m.m_mach));
35911734SAli.Bahrami@Sun.COM 
36011734SAli.Bahrami@Sun.COM #undef FP_FLAGS
3610Sstevel@tonic-gate }
3620Sstevel@tonic-gate 
3630Sstevel@tonic-gate /*
3640Sstevel@tonic-gate  * Determine the hardware capabilities of the object being built from the
3650Sstevel@tonic-gate  * capabilities of the input relocatable objects.  There's really little to
3660Sstevel@tonic-gate  * do here, other than to offer diagnostics, hardware capabilities are simply
3670Sstevel@tonic-gate  * additive.
3680Sstevel@tonic-gate  */
3690Sstevel@tonic-gate static void
hw_cap(Ofl_desc * ofl,Xword tag,Xword val)37011827SRod.Evans@Sun.COM hw_cap(Ofl_desc *ofl, Xword tag, Xword val)
3710Sstevel@tonic-gate {
37211827SRod.Evans@Sun.COM 	elfcap_mask_t	*hwcap;
37311827SRod.Evans@Sun.COM 	ofl_flag_t	flags1;
37411827SRod.Evans@Sun.COM 
37511827SRod.Evans@Sun.COM 	if (tag == CA_SUNW_HW_1) {
37611827SRod.Evans@Sun.COM 		hwcap = &ofl->ofl_ocapset.oc_hw_1.cm_val;
37711827SRod.Evans@Sun.COM 		flags1 = FLG_OF1_OVHWCAP1;
37811827SRod.Evans@Sun.COM 	} else {
37911827SRod.Evans@Sun.COM 		hwcap = &ofl->ofl_ocapset.oc_hw_2.cm_val;
38011827SRod.Evans@Sun.COM 		flags1 = FLG_OF1_OVHWCAP2;
38111827SRod.Evans@Sun.COM 	}
38211827SRod.Evans@Sun.COM 
3830Sstevel@tonic-gate 	/*
38411827SRod.Evans@Sun.COM 	 * If a mapfile has established definitions to override any object
38511827SRod.Evans@Sun.COM 	 * capabilities, ignore any new object capabilities.
3860Sstevel@tonic-gate 	 */
38711827SRod.Evans@Sun.COM 	if (ofl->ofl_flags1 & flags1) {
38811827SRod.Evans@Sun.COM 		DBG_CALL(Dbg_cap_val_entry(ofl->ofl_lml, DBG_STATE_IGNORED,
38911827SRod.Evans@Sun.COM 		    tag, val, ld_targ.t_m.m_mach));
3900Sstevel@tonic-gate 		return;
3910Sstevel@tonic-gate 	}
3920Sstevel@tonic-gate 
3930Sstevel@tonic-gate 	/*
3940Sstevel@tonic-gate 	 * If this object doesn't specify any capabilities, ignore it, and
3950Sstevel@tonic-gate 	 * leave the state as is.
3960Sstevel@tonic-gate 	 */
3970Sstevel@tonic-gate 	if (val == 0)
3980Sstevel@tonic-gate 		return;
3990Sstevel@tonic-gate 
40011734SAli.Bahrami@Sun.COM 	if (DBG_ENABLED) {
40111827SRod.Evans@Sun.COM 		Dbg_cap_val_entry(ofl->ofl_lml, DBG_STATE_CURRENT, CA_SUNW_HW_1,
40211827SRod.Evans@Sun.COM 		    ofl->ofl_ocapset.oc_hw_1.cm_val, ld_targ.t_m.m_mach);
40311827SRod.Evans@Sun.COM 		Dbg_cap_val_entry(ofl->ofl_lml, DBG_STATE_NEW, CA_SUNW_HW_1,
40411827SRod.Evans@Sun.COM 		    val, ld_targ.t_m.m_mach);
40511827SRod.Evans@Sun.COM 	}
40611827SRod.Evans@Sun.COM 
40711827SRod.Evans@Sun.COM 	*hwcap |= val;
40811827SRod.Evans@Sun.COM 
40911827SRod.Evans@Sun.COM 	DBG_CALL(Dbg_cap_val_entry(ofl->ofl_lml, DBG_STATE_RESOLVED, tag,
41011827SRod.Evans@Sun.COM 	    *hwcap, ld_targ.t_m.m_mach));
41111827SRod.Evans@Sun.COM }
41211827SRod.Evans@Sun.COM 
41311827SRod.Evans@Sun.COM /*
41411827SRod.Evans@Sun.COM  * Promote a machine capability or platform capability to the output file.
41511827SRod.Evans@Sun.COM  * Multiple instances of these names can be defined.
41611827SRod.Evans@Sun.COM  */
41711827SRod.Evans@Sun.COM static void
str_cap(Ofl_desc * ofl,char * pstr,ofl_flag_t flags,Xword tag,Caplist * list)41811827SRod.Evans@Sun.COM str_cap(Ofl_desc *ofl, char *pstr, ofl_flag_t flags, Xword tag, Caplist *list)
41911827SRod.Evans@Sun.COM {
42011827SRod.Evans@Sun.COM 	Capstr		*capstr;
42111827SRod.Evans@Sun.COM 	Aliste		idx;
42211827SRod.Evans@Sun.COM 	Boolean		found = FALSE;
42311827SRod.Evans@Sun.COM 
42411827SRod.Evans@Sun.COM 	/*
42511827SRod.Evans@Sun.COM 	 * If a mapfile has established definitions to override this capability,
42611827SRod.Evans@Sun.COM 	 * ignore any new capability.
42711827SRod.Evans@Sun.COM 	 */
42811827SRod.Evans@Sun.COM 	if (ofl->ofl_flags1 & flags) {
42911827SRod.Evans@Sun.COM 		DBG_CALL(Dbg_cap_ptr_entry(ofl->ofl_lml, DBG_STATE_IGNORED,
43011827SRod.Evans@Sun.COM 		    tag, pstr));
43111827SRod.Evans@Sun.COM 		return;
43211827SRod.Evans@Sun.COM 	}
43311827SRod.Evans@Sun.COM 
43411827SRod.Evans@Sun.COM 	for (ALIST_TRAVERSE(list->cl_val, idx, capstr)) {
43511827SRod.Evans@Sun.COM 		DBG_CALL(Dbg_cap_ptr_entry(ofl->ofl_lml,
43611827SRod.Evans@Sun.COM 		    DBG_STATE_CURRENT, tag, capstr->cs_str));
43711827SRod.Evans@Sun.COM 		if (strcmp(capstr->cs_str, pstr) == 0)
43811827SRod.Evans@Sun.COM 			found = TRUE;
43911827SRod.Evans@Sun.COM 	}
44011827SRod.Evans@Sun.COM 
44111827SRod.Evans@Sun.COM 	DBG_CALL(Dbg_cap_ptr_entry(ofl->ofl_lml, DBG_STATE_NEW, tag, pstr));
44211827SRod.Evans@Sun.COM 
44311827SRod.Evans@Sun.COM 	if (found == FALSE) {
44411827SRod.Evans@Sun.COM 		if ((capstr = alist_append(&list->cl_val, NULL,
44511827SRod.Evans@Sun.COM 		    sizeof (Capstr), AL_CNT_CAP_NAMES)) == NULL) {
44611827SRod.Evans@Sun.COM 			ofl->ofl_flags |= FLG_OF_FATAL;
44711827SRod.Evans@Sun.COM 			return;
44811827SRod.Evans@Sun.COM 		}
44911827SRod.Evans@Sun.COM 		capstr->cs_str = pstr;
45011827SRod.Evans@Sun.COM 	}
45111827SRod.Evans@Sun.COM 
45211827SRod.Evans@Sun.COM 	if (DBG_ENABLED) {
45311827SRod.Evans@Sun.COM 		for (ALIST_TRAVERSE(list->cl_val, idx, capstr)) {
45411827SRod.Evans@Sun.COM 			DBG_CALL(Dbg_cap_ptr_entry(ofl->ofl_lml,
45511827SRod.Evans@Sun.COM 			    DBG_STATE_RESOLVED, tag, capstr->cs_str));
45611827SRod.Evans@Sun.COM 		}
45711734SAli.Bahrami@Sun.COM 	}
45811827SRod.Evans@Sun.COM }
45911827SRod.Evans@Sun.COM 
46011827SRod.Evans@Sun.COM /*
46111827SRod.Evans@Sun.COM  * Promote a capability identifier to the output file.  A capability group can
46211827SRod.Evans@Sun.COM  * only have one identifier, and thus only the first identifier seen from any
46311827SRod.Evans@Sun.COM  * input relocatable objects is retained.  An explicit user defined identifier,
46411827SRod.Evans@Sun.COM  * rather than an an identifier fabricated by ld(1) with -z symbcap processing,
46511827SRod.Evans@Sun.COM  * takes precedence.  Note, a user may have defined an identifier via a mapfile,
46611827SRod.Evans@Sun.COM  * in which case the mapfile identifier is retained.
46711827SRod.Evans@Sun.COM  */
46811827SRod.Evans@Sun.COM static void
id_cap(Ofl_desc * ofl,char * pstr,oc_flag_t flags)46911827SRod.Evans@Sun.COM id_cap(Ofl_desc *ofl, char *pstr, oc_flag_t flags)
47011827SRod.Evans@Sun.COM {
47111827SRod.Evans@Sun.COM 	Objcapset	*ocapset = &ofl->ofl_ocapset;
47211827SRod.Evans@Sun.COM 
47311827SRod.Evans@Sun.COM 	if (ocapset->oc_id.cs_str) {
47411827SRod.Evans@Sun.COM 		DBG_CALL(Dbg_cap_ptr_entry(ofl->ofl_lml, DBG_STATE_CURRENT,
47511827SRod.Evans@Sun.COM 		    CA_SUNW_ID, ocapset->oc_id.cs_str));
47611827SRod.Evans@Sun.COM 
47711827SRod.Evans@Sun.COM 		if ((ocapset->oc_flags & FLG_OCS_USRDEFID) ||
47811827SRod.Evans@Sun.COM 		    ((flags & FLG_OCS_USRDEFID) == 0)) {
47911827SRod.Evans@Sun.COM 			DBG_CALL(Dbg_cap_ptr_entry(ofl->ofl_lml,
48011827SRod.Evans@Sun.COM 			    DBG_STATE_IGNORED, CA_SUNW_ID, pstr));
48111827SRod.Evans@Sun.COM 			return;
48211827SRod.Evans@Sun.COM 		}
48311827SRod.Evans@Sun.COM 	}
48411827SRod.Evans@Sun.COM 
48511827SRod.Evans@Sun.COM 	DBG_CALL(Dbg_cap_ptr_entry(ofl->ofl_lml, DBG_STATE_NEW,
48611827SRod.Evans@Sun.COM 	    CA_SUNW_ID, pstr));
48711827SRod.Evans@Sun.COM 
48811827SRod.Evans@Sun.COM 	ocapset->oc_id.cs_str = pstr;
48911827SRod.Evans@Sun.COM 	ocapset->oc_flags |= flags;
49011827SRod.Evans@Sun.COM 
49111827SRod.Evans@Sun.COM 	DBG_CALL(Dbg_cap_ptr_entry(ofl->ofl_lml, DBG_STATE_RESOLVED,
49211827SRod.Evans@Sun.COM 	    CA_SUNW_ID, pstr));
49311827SRod.Evans@Sun.COM }
49411827SRod.Evans@Sun.COM 
49511827SRod.Evans@Sun.COM /*
49611827SRod.Evans@Sun.COM  * Promote a capabilities group to the object capabilities.  This catches a
49711827SRod.Evans@Sun.COM  * corner case.  An object capabilities file can be converted to symbol
49811827SRod.Evans@Sun.COM  * capabilities with -z symbolcap.  However, if the user has indicated that all
49911827SRod.Evans@Sun.COM  * the symbols should be demoted, we'd be left with a symbol capabilities file,
50011827SRod.Evans@Sun.COM  * with no associated symbols.  Catch this case by promoting the symbol
50111827SRod.Evans@Sun.COM  * capabilities back to object capabilities.
50211827SRod.Evans@Sun.COM  */
50311827SRod.Evans@Sun.COM void
ld_cap_move_symtoobj(Ofl_desc * ofl)50411827SRod.Evans@Sun.COM ld_cap_move_symtoobj(Ofl_desc *ofl)
50511827SRod.Evans@Sun.COM {
50611827SRod.Evans@Sun.COM 	Cap_group	*cgp;
50711827SRod.Evans@Sun.COM 	Aliste		idx1;
50811827SRod.Evans@Sun.COM 
50911827SRod.Evans@Sun.COM 	for (APLIST_TRAVERSE(ofl->ofl_capgroups, idx1, cgp)) {
51011827SRod.Evans@Sun.COM 		Objcapset	*scapset = &cgp->cg_set;
51111827SRod.Evans@Sun.COM 		Capstr		*capstr;
51211827SRod.Evans@Sun.COM 		Aliste		idx2;
51311827SRod.Evans@Sun.COM 
51411827SRod.Evans@Sun.COM 		if (scapset->oc_id.cs_str) {
51511827SRod.Evans@Sun.COM 			if (scapset->oc_flags & FLG_OCS_USRDEFID)
51611827SRod.Evans@Sun.COM 				id_cap(ofl, scapset->oc_id.cs_str,
51711827SRod.Evans@Sun.COM 				    scapset->oc_flags);
51811827SRod.Evans@Sun.COM 		}
51911827SRod.Evans@Sun.COM 		if (scapset->oc_plat.cl_val) {
52011827SRod.Evans@Sun.COM 			for (ALIST_TRAVERSE(scapset->oc_plat.cl_val, idx2,
52111827SRod.Evans@Sun.COM 			    capstr)) {
52211827SRod.Evans@Sun.COM 				str_cap(ofl, capstr->cs_str, FLG_OF1_OVPLATCAP,
52311827SRod.Evans@Sun.COM 				    CA_SUNW_PLAT, &ofl->ofl_ocapset.oc_plat);
52411827SRod.Evans@Sun.COM 			}
52511827SRod.Evans@Sun.COM 		}
52611827SRod.Evans@Sun.COM 		if (scapset->oc_mach.cl_val) {
52711827SRod.Evans@Sun.COM 			for (ALIST_TRAVERSE(scapset->oc_mach.cl_val, idx2,
52811827SRod.Evans@Sun.COM 			    capstr)) {
52911827SRod.Evans@Sun.COM 				str_cap(ofl, capstr->cs_str, FLG_OF1_OVMACHCAP,
53011827SRod.Evans@Sun.COM 				    CA_SUNW_MACH, &ofl->ofl_ocapset.oc_mach);
53111827SRod.Evans@Sun.COM 			}
53211827SRod.Evans@Sun.COM 		}
53311827SRod.Evans@Sun.COM 		if (scapset->oc_hw_2.cm_val)
53411827SRod.Evans@Sun.COM 			hw_cap(ofl, CA_SUNW_HW_2, scapset->oc_hw_2.cm_val);
53511827SRod.Evans@Sun.COM 
53611827SRod.Evans@Sun.COM 		if (scapset->oc_hw_1.cm_val)
53711827SRod.Evans@Sun.COM 			hw_cap(ofl, CA_SUNW_HW_1, scapset->oc_hw_1.cm_val);
53811827SRod.Evans@Sun.COM 
53911827SRod.Evans@Sun.COM 		if (scapset->oc_sf_1.cm_val)
54011827SRod.Evans@Sun.COM 			sf1_cap(ofl, scapset->oc_sf_1.cm_val, NULL, NULL);
54111827SRod.Evans@Sun.COM 	}
5420Sstevel@tonic-gate }
5430Sstevel@tonic-gate 
5440Sstevel@tonic-gate /*
54511827SRod.Evans@Sun.COM  * Determine whether a capabilities group already exists that describes this
54611827SRod.Evans@Sun.COM  * new capabilities group.
54711827SRod.Evans@Sun.COM  *
54811827SRod.Evans@Sun.COM  * Note, a capability group identifier, CA_SUNW_ID, isn't used as part of the
54911827SRod.Evans@Sun.COM  * comparison.  This attribute simply assigns a diagnostic name to the group,
55011827SRod.Evans@Sun.COM  * and in the case of multiple identifiers, the first will be taken.
5510Sstevel@tonic-gate  */
55211827SRod.Evans@Sun.COM static Cap_group *
get_cap_group(Objcapset * ocapset,Word cnum,Ofl_desc * ofl,Is_desc * isp)55311827SRod.Evans@Sun.COM get_cap_group(Objcapset *ocapset, Word cnum, Ofl_desc *ofl, Is_desc *isp)
5540Sstevel@tonic-gate {
55511827SRod.Evans@Sun.COM 	Aliste		idx;
55611827SRod.Evans@Sun.COM 	Cap_group	*cgp;
55711827SRod.Evans@Sun.COM 	Word		ccnum = cnum;
55811827SRod.Evans@Sun.COM 
55911827SRod.Evans@Sun.COM 	/*
56011827SRod.Evans@Sun.COM 	 * If the new capabilities contains a CA_SUNW_ID, drop the count of the
56111827SRod.Evans@Sun.COM 	 * number of comparable items.
56211827SRod.Evans@Sun.COM 	 */
56311827SRod.Evans@Sun.COM 	if (ocapset->oc_id.cs_str)
56411827SRod.Evans@Sun.COM 		ccnum--;
56511827SRod.Evans@Sun.COM 
56611827SRod.Evans@Sun.COM 	/*
56711827SRod.Evans@Sun.COM 	 * Traverse the existing symbols capabilities groups.
56811827SRod.Evans@Sun.COM 	 */
56911827SRod.Evans@Sun.COM 	for (APLIST_TRAVERSE(ofl->ofl_capgroups, idx, cgp)) {
57011827SRod.Evans@Sun.COM 		Word	onum = cgp->cg_num;
57111827SRod.Evans@Sun.COM 		Alist	*calp, *oalp;
57211827SRod.Evans@Sun.COM 
57311827SRod.Evans@Sun.COM 		if (cgp->cg_set.oc_id.cs_str)
57411827SRod.Evans@Sun.COM 			onum--;
57511827SRod.Evans@Sun.COM 
57611827SRod.Evans@Sun.COM 		if (onum != ccnum)
57711827SRod.Evans@Sun.COM 			continue;
57811827SRod.Evans@Sun.COM 
57911827SRod.Evans@Sun.COM 		if (cgp->cg_set.oc_hw_1.cm_val != ocapset->oc_hw_1.cm_val)
58011827SRod.Evans@Sun.COM 			continue;
58111827SRod.Evans@Sun.COM 		if (cgp->cg_set.oc_sf_1.cm_val != ocapset->oc_sf_1.cm_val)
58211827SRod.Evans@Sun.COM 			continue;
58311827SRod.Evans@Sun.COM 		if (cgp->cg_set.oc_hw_2.cm_val != ocapset->oc_hw_2.cm_val)
58411827SRod.Evans@Sun.COM 			continue;
58511827SRod.Evans@Sun.COM 
58611827SRod.Evans@Sun.COM 		calp = cgp->cg_set.oc_plat.cl_val;
58711827SRod.Evans@Sun.COM 		oalp = ocapset->oc_plat.cl_val;
58811827SRod.Evans@Sun.COM 		if ((calp == NULL) && oalp)
58911827SRod.Evans@Sun.COM 			continue;
59011827SRod.Evans@Sun.COM 		if (calp && ((oalp == NULL) || cap_names_match(calp, oalp)))
59111827SRod.Evans@Sun.COM 			continue;
59211827SRod.Evans@Sun.COM 
59311827SRod.Evans@Sun.COM 		calp = cgp->cg_set.oc_mach.cl_val;
59411827SRod.Evans@Sun.COM 		oalp = ocapset->oc_mach.cl_val;
59511827SRod.Evans@Sun.COM 		if ((calp == NULL) && oalp)
59611827SRod.Evans@Sun.COM 			continue;
59711827SRod.Evans@Sun.COM 		if (calp && ((oalp == NULL) || cap_names_match(calp, oalp)))
59811827SRod.Evans@Sun.COM 			continue;
59911827SRod.Evans@Sun.COM 
60011827SRod.Evans@Sun.COM 		/*
60111827SRod.Evans@Sun.COM 		 * If a matching group is found, then this new group has
60211827SRod.Evans@Sun.COM 		 * already been supplied by a previous file, and hence the
60311827SRod.Evans@Sun.COM 		 * existing group can be used.  Record this new input section,
60411827SRod.Evans@Sun.COM 		 * from which we can also derive the input file name, on the
60511827SRod.Evans@Sun.COM 		 * existing groups input sections.
60611827SRod.Evans@Sun.COM 		 */
60711827SRod.Evans@Sun.COM 		if (aplist_append(&(cgp->cg_secs), isp,
60811827SRod.Evans@Sun.COM 		    AL_CNT_CAP_SECS) == NULL)
60911827SRod.Evans@Sun.COM 			return (NULL);
61011827SRod.Evans@Sun.COM 		return (cgp);
61111827SRod.Evans@Sun.COM 	}
61211827SRod.Evans@Sun.COM 
61311827SRod.Evans@Sun.COM 	/*
61411827SRod.Evans@Sun.COM 	 * If a capabilities group is not found, create a new one.
61511827SRod.Evans@Sun.COM 	 */
61611827SRod.Evans@Sun.COM 	if (((cgp = libld_calloc(sizeof (Cap_group), 1)) == NULL) ||
61711827SRod.Evans@Sun.COM 	    (aplist_append(&(ofl->ofl_capgroups), cgp,
61811827SRod.Evans@Sun.COM 	    AL_CNT_CAP_DESCS) == NULL))
61911827SRod.Evans@Sun.COM 		return (NULL);
62011827SRod.Evans@Sun.COM 
62111827SRod.Evans@Sun.COM 	/*
62211827SRod.Evans@Sun.COM 	 * If we're converting object capabilities to symbol capabilities and
62311827SRod.Evans@Sun.COM 	 * no CA_SUNW_ID is defined, fabricate one.  This identifier is appended
62411827SRod.Evans@Sun.COM 	 * to all symbol names that are converted into capabilities symbols,
62511827SRod.Evans@Sun.COM 	 * see ld_sym_process().
62611827SRod.Evans@Sun.COM 	 */
62711827SRod.Evans@Sun.COM 	if ((isp->is_file->ifl_flags & FLG_IF_OTOSCAP) &&
62811827SRod.Evans@Sun.COM 	    (ocapset->oc_id.cs_str == NULL)) {
62911827SRod.Evans@Sun.COM 		size_t	len;
63011827SRod.Evans@Sun.COM 
63111827SRod.Evans@Sun.COM 		/*
63211827SRod.Evans@Sun.COM 		 * Create an identifier using the group number together with a
63311827SRod.Evans@Sun.COM 		 * default template.  We allocate a buffer large enough for any
63411827SRod.Evans@Sun.COM 		 * possible number of items (way more than we need).
63511827SRod.Evans@Sun.COM 		 */
63611827SRod.Evans@Sun.COM 		len = MSG_STR_CAPGROUPID_SIZE + CONV_INV_BUFSIZE;
63711827SRod.Evans@Sun.COM 		if ((ocapset->oc_id.cs_str = libld_malloc(len)) == NULL)
63811827SRod.Evans@Sun.COM 			return (NULL);
63911827SRod.Evans@Sun.COM 
64011827SRod.Evans@Sun.COM 		(void) snprintf(ocapset->oc_id.cs_str, len,
64111827SRod.Evans@Sun.COM 		    MSG_ORIG(MSG_STR_CAPGROUPID),
64211827SRod.Evans@Sun.COM 		    aplist_nitems(ofl->ofl_capgroups));
64311827SRod.Evans@Sun.COM 		cnum++;
64411827SRod.Evans@Sun.COM 	}
64511827SRod.Evans@Sun.COM 
64611827SRod.Evans@Sun.COM 	cgp->cg_set = *ocapset;
64711827SRod.Evans@Sun.COM 	cgp->cg_num = cnum;
64811827SRod.Evans@Sun.COM 
64911827SRod.Evans@Sun.COM 	/*
65011827SRod.Evans@Sun.COM 	 * Null the callers alist's as they've effectively been transferred
65111827SRod.Evans@Sun.COM 	 * to this new Cap_group.
65211827SRod.Evans@Sun.COM 	 */
65311827SRod.Evans@Sun.COM 	ocapset->oc_plat.cl_val = ocapset->oc_mach.cl_val = NULL;
65411827SRod.Evans@Sun.COM 
65511827SRod.Evans@Sun.COM 	/*
65611827SRod.Evans@Sun.COM 	 * Keep track of which input section, and hence input file, established
65711827SRod.Evans@Sun.COM 	 * this group.
65811827SRod.Evans@Sun.COM 	 */
65911827SRod.Evans@Sun.COM 	if (aplist_append(&(cgp->cg_secs), isp, AL_CNT_CAP_SECS) == NULL)
66011827SRod.Evans@Sun.COM 		return (NULL);
66111827SRod.Evans@Sun.COM 
66211827SRod.Evans@Sun.COM 	/*
66311827SRod.Evans@Sun.COM 	 * Keep track of the number of symbol capabilities entries that will be
66411827SRod.Evans@Sun.COM 	 * required in the output file.  Each group requires a terminating
66511827SRod.Evans@Sun.COM 	 * CA_SUNW_NULL.
66611827SRod.Evans@Sun.COM 	 */
66711827SRod.Evans@Sun.COM 	ofl->ofl_capsymcnt += (cnum + 1);
66811827SRod.Evans@Sun.COM 	return (cgp);
66911827SRod.Evans@Sun.COM }
67011827SRod.Evans@Sun.COM 
67111827SRod.Evans@Sun.COM /*
67211827SRod.Evans@Sun.COM  * Capture symbol capability family information.  This data structure is focal
67311827SRod.Evans@Sun.COM  * in maintaining all symbol capability relationships, and provides for the
67411827SRod.Evans@Sun.COM  * eventual creation of a capabilities information section, and possibly a
67511827SRod.Evans@Sun.COM  * capabilities chain section.
67611827SRod.Evans@Sun.COM  *
67711827SRod.Evans@Sun.COM  * Capabilities families are lead by a CAPINFO_SUNW_GLOB symbol.  This symbol
67811827SRod.Evans@Sun.COM  * provides the visible global symbol that is referenced by all external
67911827SRod.Evans@Sun.COM  * callers.  This symbol may have aliases.  For example, a weak/global symbol
68011827SRod.Evans@Sun.COM  * pair, such as memcpy()/_memcpy() may lead the same capabilities family.
68111827SRod.Evans@Sun.COM  * Each family contains one or more local symbol members.  These members provide
68211827SRod.Evans@Sun.COM  * the capabilities specific functions, and are associated to a capabilities
68311827SRod.Evans@Sun.COM  * group.  For example, the capability members memcpy%sun4u and memcpy%sun4v
68411827SRod.Evans@Sun.COM  * might be associated with the memcpy() capability family.
68511827SRod.Evans@Sun.COM  *
68611827SRod.Evans@Sun.COM  * This routine is called when a relocatable object that provides object
68711827SRod.Evans@Sun.COM  * capabilities is transformed into a symbol capabilities object, using the
68811827SRod.Evans@Sun.COM  * -z symbolcap option.
68911827SRod.Evans@Sun.COM  *
69011827SRod.Evans@Sun.COM  * This routine is also called to collect the SUNW_capinfo section information
69111827SRod.Evans@Sun.COM  * of a relocatable object that contains symbol capability definitions.
69211827SRod.Evans@Sun.COM  */
69311827SRod.Evans@Sun.COM uintptr_t
ld_cap_add_family(Ofl_desc * ofl,Sym_desc * lsdp,Sym_desc * csdp,Cap_group * cgp,APlist ** csyms)69411827SRod.Evans@Sun.COM ld_cap_add_family(Ofl_desc *ofl, Sym_desc *lsdp, Sym_desc *csdp, Cap_group *cgp,
69511827SRod.Evans@Sun.COM     APlist **csyms)
69611827SRod.Evans@Sun.COM {
69711827SRod.Evans@Sun.COM 	Cap_avlnode	qcav, *cav;
69811827SRod.Evans@Sun.COM 	avl_tree_t	*avlt;
69911827SRod.Evans@Sun.COM 	avl_index_t	where = 0;
70011827SRod.Evans@Sun.COM 	Cap_sym		*mcsp;
70111827SRod.Evans@Sun.COM 	Aliste		idx;
70211827SRod.Evans@Sun.COM 
70311827SRod.Evans@Sun.COM 	/*
70411827SRod.Evans@Sun.COM 	 * Make sure the capability families have an initialized AVL tree.
70511827SRod.Evans@Sun.COM 	 */
70611827SRod.Evans@Sun.COM 	if ((avlt = ofl->ofl_capfamilies) == NULL) {
70711827SRod.Evans@Sun.COM 		if ((avlt = libld_calloc(sizeof (avl_tree_t), 1)) == NULL)
70811827SRod.Evans@Sun.COM 			return (S_ERROR);
70911827SRod.Evans@Sun.COM 		avl_create(avlt, &ld_sym_avl_comp, sizeof (Cap_avlnode),
71011827SRod.Evans@Sun.COM 		    SGSOFFSETOF(Cap_avlnode, cn_symavlnode.sav_node));
71111827SRod.Evans@Sun.COM 		ofl->ofl_capfamilies = avlt;
71211827SRod.Evans@Sun.COM 
71311827SRod.Evans@Sun.COM 		/*
71411827SRod.Evans@Sun.COM 		 * When creating a dynamic object, capability family members
71511827SRod.Evans@Sun.COM 		 * are maintained in a .SUNW_capchain, the first entry of
71611827SRod.Evans@Sun.COM 		 * which is the version number of the chain.
71711827SRod.Evans@Sun.COM 		 */
71811827SRod.Evans@Sun.COM 		ofl->ofl_capchaincnt = 1;
71911827SRod.Evans@Sun.COM 	}
72011827SRod.Evans@Sun.COM 
72111827SRod.Evans@Sun.COM 	/*
72211827SRod.Evans@Sun.COM 	 * Determine whether a family already exists, and if not, create one
72311827SRod.Evans@Sun.COM 	 * using the lead family symbol.
72411827SRod.Evans@Sun.COM 	 */
72511827SRod.Evans@Sun.COM 	qcav.cn_symavlnode.sav_hash = (Word)elf_hash(lsdp->sd_name);
72611827SRod.Evans@Sun.COM 	qcav.cn_symavlnode.sav_name = lsdp->sd_name;
72711827SRod.Evans@Sun.COM 
72811827SRod.Evans@Sun.COM 	if ((cav = avl_find(avlt, &qcav, &where)) == NULL) {
72911827SRod.Evans@Sun.COM 		if ((cav = libld_calloc(sizeof (Cap_avlnode), 1)) == NULL)
73011827SRod.Evans@Sun.COM 			return (S_ERROR);
73111827SRod.Evans@Sun.COM 		cav->cn_symavlnode.sav_hash = qcav.cn_symavlnode.sav_hash;
73211827SRod.Evans@Sun.COM 		cav->cn_symavlnode.sav_name = qcav.cn_symavlnode.sav_name;
73311827SRod.Evans@Sun.COM 		cav->cn_symavlnode.sav_sdp = lsdp;
73411827SRod.Evans@Sun.COM 
73511827SRod.Evans@Sun.COM 		avl_insert(avlt, cav, where);
73611827SRod.Evans@Sun.COM 
73711827SRod.Evans@Sun.COM 		/*
73811827SRod.Evans@Sun.COM 		 * When creating a dynamic object, capability family members
73911827SRod.Evans@Sun.COM 		 * are maintained in a .SUNW_capchain, each family starts with
74011827SRod.Evans@Sun.COM 		 * this lead symbol, and is terminated with a 0 element.
74111827SRod.Evans@Sun.COM 		 */
74211827SRod.Evans@Sun.COM 		ofl->ofl_capchaincnt += 2;
74311827SRod.Evans@Sun.COM 	}
74411827SRod.Evans@Sun.COM 
74511827SRod.Evans@Sun.COM 	/*
74611827SRod.Evans@Sun.COM 	 * If no group information is provided then this request is to add a
74711827SRod.Evans@Sun.COM 	 * lead capability symbol, or lead symbol alias.  If this is the lead
74811827SRod.Evans@Sun.COM 	 * symbol there's nothing more to do.  Otherwise save the alias.
74911827SRod.Evans@Sun.COM 	 */
75011827SRod.Evans@Sun.COM 	if (cgp == NULL) {
75111827SRod.Evans@Sun.COM 		if ((lsdp != csdp) && (aplist_append(&cav->cn_aliases, csdp,
75211827SRod.Evans@Sun.COM 		    AL_CNT_CAP_ALIASES) == NULL))
75311827SRod.Evans@Sun.COM 			return (S_ERROR);
75411827SRod.Evans@Sun.COM 
75511827SRod.Evans@Sun.COM 		return (0);
75611827SRod.Evans@Sun.COM 	}
75711827SRod.Evans@Sun.COM 
75811827SRod.Evans@Sun.COM 	/*
75911827SRod.Evans@Sun.COM 	 * Determine whether a member of the same group as this new member is
76011827SRod.Evans@Sun.COM 	 * already defined within this family.  If so, we have a multiply
76111827SRod.Evans@Sun.COM 	 * defined symbol.
76211827SRod.Evans@Sun.COM 	 */
76311827SRod.Evans@Sun.COM 	for (APLIST_TRAVERSE(cav->cn_members, idx, mcsp)) {
76411827SRod.Evans@Sun.COM 		Sym_desc	*msdp;
76511827SRod.Evans@Sun.COM 
76611827SRod.Evans@Sun.COM 		if (cgp != mcsp->cs_group)
76711827SRod.Evans@Sun.COM 			continue;
76811827SRod.Evans@Sun.COM 
76911827SRod.Evans@Sun.COM 		/*
77011827SRod.Evans@Sun.COM 		 * Diagnose that a multiple symbol definition exists.
77111827SRod.Evans@Sun.COM 		 */
77211827SRod.Evans@Sun.COM 		msdp = mcsp->cs_sdp;
77311827SRod.Evans@Sun.COM 
774*13074SAli.Bahrami@Oracle.COM 		ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_CAP_MULDEF),
77511827SRod.Evans@Sun.COM 		    demangle(lsdp->sd_name));
776*13074SAli.Bahrami@Oracle.COM 		ld_eprintf(ofl, ERR_NONE, MSG_INTL(MSG_CAP_MULDEFSYMS),
77711827SRod.Evans@Sun.COM 		    msdp->sd_file->ifl_name, msdp->sd_name,
77811827SRod.Evans@Sun.COM 		    csdp->sd_file->ifl_name, csdp->sd_name);
77911827SRod.Evans@Sun.COM 	}
78011827SRod.Evans@Sun.COM 
78111827SRod.Evans@Sun.COM 	/*
78211827SRod.Evans@Sun.COM 	 * Add this capabilities symbol member to the family.
78311827SRod.Evans@Sun.COM 	 */
78411827SRod.Evans@Sun.COM 	if (((mcsp = libld_malloc(sizeof (Cap_sym))) == NULL) ||
78511827SRod.Evans@Sun.COM 	    (aplist_append(&cav->cn_members, mcsp, AL_CNT_CAP_MEMS) == NULL))
78611827SRod.Evans@Sun.COM 		return (S_ERROR);
78711827SRod.Evans@Sun.COM 
78811827SRod.Evans@Sun.COM 	mcsp->cs_sdp = csdp;
78911827SRod.Evans@Sun.COM 	mcsp->cs_group = cgp;
79011827SRod.Evans@Sun.COM 
79111827SRod.Evans@Sun.COM 	/*
79211827SRod.Evans@Sun.COM 	 * When creating a dynamic object, capability family members are
79311827SRod.Evans@Sun.COM 	 * maintained in a .SUNW_capchain.  Account for this family member.
79411827SRod.Evans@Sun.COM 	 */
79511827SRod.Evans@Sun.COM 	ofl->ofl_capchaincnt++;
79611827SRod.Evans@Sun.COM 
79711827SRod.Evans@Sun.COM 	/*
79811827SRod.Evans@Sun.COM 	 * If this input file is undergoing object capabilities to symbol
79911827SRod.Evans@Sun.COM 	 * capabilities conversion, then this member is a new local symbol
80011827SRod.Evans@Sun.COM 	 * that has been generated from an original global symbol.  Keep track
80111827SRod.Evans@Sun.COM 	 * of this symbol so that the output file symbol table can be populated
80211827SRod.Evans@Sun.COM 	 * with these new symbol entries.
80311827SRod.Evans@Sun.COM 	 */
80411827SRod.Evans@Sun.COM 	if (csyms && (aplist_append(csyms, mcsp, AL_CNT_CAP_SYMS) == NULL))
80511827SRod.Evans@Sun.COM 		return (S_ERROR);
80611827SRod.Evans@Sun.COM 
80711827SRod.Evans@Sun.COM 	return (0);
80811827SRod.Evans@Sun.COM }
80911827SRod.Evans@Sun.COM 
81011827SRod.Evans@Sun.COM /*
81111827SRod.Evans@Sun.COM  * Process a SHT_SUNW_cap capabilities section.
81211827SRod.Evans@Sun.COM  */
81311827SRod.Evans@Sun.COM static uintptr_t
process_cap(Ofl_desc * ofl,Ifl_desc * ifl,Is_desc * cisp)81411827SRod.Evans@Sun.COM process_cap(Ofl_desc *ofl, Ifl_desc *ifl, Is_desc *cisp)
81511827SRod.Evans@Sun.COM {
81611827SRod.Evans@Sun.COM 	Objcapset	ocapset = { 0 };
81711827SRod.Evans@Sun.COM 	Cap_desc	*cdp;
81811827SRod.Evans@Sun.COM 	Cap		*data, *cdata;
81911827SRod.Evans@Sun.COM 	char		*strs;
82011827SRod.Evans@Sun.COM 	Word		ndx, cnum;
82111827SRod.Evans@Sun.COM 	int		objcapndx, descapndx, symcapndx;
82211827SRod.Evans@Sun.COM 	int		nulls, capstrs = 0;
82311827SRod.Evans@Sun.COM 
82411827SRod.Evans@Sun.COM 	/*
82511827SRod.Evans@Sun.COM 	 * Determine the capabilities data and size.
82611827SRod.Evans@Sun.COM 	 */
82711827SRod.Evans@Sun.COM 	cdata = (Cap *)cisp->is_indata->d_buf;
82811827SRod.Evans@Sun.COM 	cnum = (Word)(cisp->is_shdr->sh_size / cisp->is_shdr->sh_entsize);
82911827SRod.Evans@Sun.COM 
83011827SRod.Evans@Sun.COM 	if ((cdata == NULL) || (cnum == 0))
83111827SRod.Evans@Sun.COM 		return (0);
8320Sstevel@tonic-gate 
8338501SRod.Evans@Sun.COM 	DBG_CALL(Dbg_cap_sec_title(ofl->ofl_lml, ifl->ifl_name));
8340Sstevel@tonic-gate 
8353617Srie 	/*
83611827SRod.Evans@Sun.COM 	 * Traverse the section to determine what capabilities groups are
83711827SRod.Evans@Sun.COM 	 * available.
83811827SRod.Evans@Sun.COM 	 *
83911827SRod.Evans@Sun.COM 	 * A capabilities section can contain one or more, CA_SUNW_NULL
84011827SRod.Evans@Sun.COM 	 * terminated groups.
84111827SRod.Evans@Sun.COM 	 *
84211827SRod.Evans@Sun.COM 	 *  -	The first group defines the object capabilities.
84311827SRod.Evans@Sun.COM 	 *  -	Additional groups define symbol capabilities.
84411827SRod.Evans@Sun.COM 	 *  -	Since the initial group is always reserved for object
84511827SRod.Evans@Sun.COM 	 *	capabilities, any object with symbol capabilities must also
84611827SRod.Evans@Sun.COM 	 *	have an object capabilities group.  If the object has no object
84711827SRod.Evans@Sun.COM 	 *	capabilities, an empty object group is defined, consisting of a
84811827SRod.Evans@Sun.COM 	 *	CA_SUNW_NULL element in index [0].
84911827SRod.Evans@Sun.COM 	 *  -	If any capabilities require references to a named string, then
85011827SRod.Evans@Sun.COM 	 *	the section header sh_info points to the associated string
85111827SRod.Evans@Sun.COM 	 *	table.
85211827SRod.Evans@Sun.COM 	 *  -	If an object contains symbol capability groups, then the
85311827SRod.Evans@Sun.COM 	 *	section header sh_link points to the associated capinfo table.
8543617Srie 	 */
85511827SRod.Evans@Sun.COM 	objcapndx = 0;
85611827SRod.Evans@Sun.COM 	descapndx = symcapndx = -1;
85711827SRod.Evans@Sun.COM 	nulls = 0;
85811827SRod.Evans@Sun.COM 
85911827SRod.Evans@Sun.COM 	for (ndx = 0, data = cdata; ndx < cnum; ndx++, data++) {
86011827SRod.Evans@Sun.COM 		switch (data->c_tag) {
86111827SRod.Evans@Sun.COM 		case CA_SUNW_NULL:
86211827SRod.Evans@Sun.COM 			/*
86311827SRod.Evans@Sun.COM 			 * If this is the first CA_SUNW_NULL entry, and no
86411827SRod.Evans@Sun.COM 			 * capabilities group has been found, then this object
86511827SRod.Evans@Sun.COM 			 * does not define any object capabilities.
86611827SRod.Evans@Sun.COM 			 */
86711827SRod.Evans@Sun.COM 			if (nulls++ == 0) {
86811827SRod.Evans@Sun.COM 				if (ndx == 0)
86911827SRod.Evans@Sun.COM 					objcapndx = -1;
87011827SRod.Evans@Sun.COM 			} else if ((symcapndx == -1) && (descapndx != -1))
87111827SRod.Evans@Sun.COM 				symcapndx = descapndx;
87211827SRod.Evans@Sun.COM 
87311827SRod.Evans@Sun.COM 			break;
87411827SRod.Evans@Sun.COM 
87511827SRod.Evans@Sun.COM 		case CA_SUNW_PLAT:
87611827SRod.Evans@Sun.COM 		case CA_SUNW_MACH:
87711827SRod.Evans@Sun.COM 		case CA_SUNW_ID:
87811827SRod.Evans@Sun.COM 			capstrs++;
87911827SRod.Evans@Sun.COM 			/* FALLTHROUGH */
88011827SRod.Evans@Sun.COM 
88111827SRod.Evans@Sun.COM 		case CA_SUNW_HW_1:
88211827SRod.Evans@Sun.COM 		case CA_SUNW_SF_1:
88311827SRod.Evans@Sun.COM 		case CA_SUNW_HW_2:
88411827SRod.Evans@Sun.COM 			/*
88511827SRod.Evans@Sun.COM 			 * If this is the start of a new group, save it.
88611827SRod.Evans@Sun.COM 			 */
88711827SRod.Evans@Sun.COM 			if (descapndx == -1)
88811827SRod.Evans@Sun.COM 				descapndx = ndx;
88911827SRod.Evans@Sun.COM 			break;
89011827SRod.Evans@Sun.COM 
89111827SRod.Evans@Sun.COM 		default:
892*13074SAli.Bahrami@Oracle.COM 			ld_eprintf(ofl, ERR_WARNING, MSG_INTL(MSG_FIL_UNKCAP),
893*13074SAli.Bahrami@Oracle.COM 			    ifl->ifl_name, EC_WORD(cisp->is_scnndx),
894*13074SAli.Bahrami@Oracle.COM 			    cisp->is_name, data->c_tag);
8950Sstevel@tonic-gate 		}
8960Sstevel@tonic-gate 	}
89711827SRod.Evans@Sun.COM 
89811827SRod.Evans@Sun.COM 	/*
89911827SRod.Evans@Sun.COM 	 * If a string capabilities entry has been found, the capabilities
90011827SRod.Evans@Sun.COM 	 * section must reference the associated string table.
90111827SRod.Evans@Sun.COM 	 */
90211827SRod.Evans@Sun.COM 	if (capstrs) {
90311827SRod.Evans@Sun.COM 		Word	info = cisp->is_shdr->sh_info;
90411827SRod.Evans@Sun.COM 
90511827SRod.Evans@Sun.COM 		if ((info == 0) || (info > ifl->ifl_shnum)) {
906*13074SAli.Bahrami@Oracle.COM 			ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_FIL_INVSHINFO),
907*13074SAli.Bahrami@Oracle.COM 			    ifl->ifl_name, EC_WORD(cisp->is_scnndx),
908*13074SAli.Bahrami@Oracle.COM 			    cisp->is_name, EC_XWORD(info));
90911827SRod.Evans@Sun.COM 			return (S_ERROR);
91011827SRod.Evans@Sun.COM 		}
91111827SRod.Evans@Sun.COM 		strs = (char *)ifl->ifl_isdesc[info]->is_indata->d_buf;
91211827SRod.Evans@Sun.COM 	}
91311827SRod.Evans@Sun.COM 
91411827SRod.Evans@Sun.COM 	/*
91511827SRod.Evans@Sun.COM 	 * The processing of capabilities groups is as follows:
91611827SRod.Evans@Sun.COM 	 *
91711827SRod.Evans@Sun.COM 	 *  -	if a relocatable object provides only object capabilities, and
91811827SRod.Evans@Sun.COM 	 *	the -z symbolcap option is in effect, then the object
91911827SRod.Evans@Sun.COM 	 *	capabilities are transformed into symbol capabilities and the
92011827SRod.Evans@Sun.COM 	 *	symbol capabilities are carried over to the output file.
92111827SRod.Evans@Sun.COM 	 *  -	in all other cases, any capabilities present in an input
92211827SRod.Evans@Sun.COM 	 *	relocatable object are carried from the input object to the
92311827SRod.Evans@Sun.COM 	 *	output without any transformation or conversion.
92411827SRod.Evans@Sun.COM 	 *
92511827SRod.Evans@Sun.COM 	 * Capture any object capabilities that are to be carried over to the
92611827SRod.Evans@Sun.COM 	 * output file.
92711827SRod.Evans@Sun.COM 	 */
92811827SRod.Evans@Sun.COM 	if ((objcapndx == 0) &&
92911827SRod.Evans@Sun.COM 	    ((symcapndx != -1) || ((ofl->ofl_flags & FLG_OF_OTOSCAP) == 0))) {
93011827SRod.Evans@Sun.COM 		for (ndx = 0, data = cdata; ndx < cnum; ndx++, data++) {
93111827SRod.Evans@Sun.COM 			/*
93211827SRod.Evans@Sun.COM 			 * Object capabilities end at the first null.
93311827SRod.Evans@Sun.COM 			 */
93411827SRod.Evans@Sun.COM 			if (data->c_tag == CA_SUNW_NULL)
93511827SRod.Evans@Sun.COM 				break;
93611827SRod.Evans@Sun.COM 
93711827SRod.Evans@Sun.COM 			/*
93811827SRod.Evans@Sun.COM 			 * Only the object software capabilities that are
93911827SRod.Evans@Sun.COM 			 * defined in a relocatable object become part of the
94011827SRod.Evans@Sun.COM 			 * object software capabilities in the output file.
94111827SRod.Evans@Sun.COM 			 * However, check the validity of any object software
94211827SRod.Evans@Sun.COM 			 * capabilities of any dependencies.
94311827SRod.Evans@Sun.COM 			 */
94411827SRod.Evans@Sun.COM 			if (data->c_tag == CA_SUNW_SF_1) {
94511827SRod.Evans@Sun.COM 				sf1_cap(ofl, data->c_un.c_val, ifl, cisp);
94611827SRod.Evans@Sun.COM 				continue;
94711827SRod.Evans@Sun.COM 			}
94811827SRod.Evans@Sun.COM 
94911827SRod.Evans@Sun.COM 			/*
95011827SRod.Evans@Sun.COM 			 * The remaining capability types must come from a
95111827SRod.Evans@Sun.COM 			 * relocatable object in order to contribute to the
95211827SRod.Evans@Sun.COM 			 * output.
95311827SRod.Evans@Sun.COM 			 */
95411827SRod.Evans@Sun.COM 			if (ifl->ifl_ehdr->e_type != ET_REL)
95511827SRod.Evans@Sun.COM 				continue;
95611827SRod.Evans@Sun.COM 
95711827SRod.Evans@Sun.COM 			switch (data->c_tag) {
95811827SRod.Evans@Sun.COM 			case CA_SUNW_HW_1:
95911827SRod.Evans@Sun.COM 			case CA_SUNW_HW_2:
96011827SRod.Evans@Sun.COM 				hw_cap(ofl, data->c_tag, data->c_un.c_val);
96111827SRod.Evans@Sun.COM 				break;
96211827SRod.Evans@Sun.COM 
96311827SRod.Evans@Sun.COM 			case CA_SUNW_PLAT:
96411827SRod.Evans@Sun.COM 				str_cap(ofl, strs + data->c_un.c_ptr,
96511827SRod.Evans@Sun.COM 				    FLG_OF1_OVPLATCAP, CA_SUNW_PLAT,
96611827SRod.Evans@Sun.COM 				    &ofl->ofl_ocapset.oc_plat);
96711827SRod.Evans@Sun.COM 				break;
96811827SRod.Evans@Sun.COM 
96911827SRod.Evans@Sun.COM 			case CA_SUNW_MACH:
97011827SRod.Evans@Sun.COM 				str_cap(ofl, strs + data->c_un.c_ptr,
97111827SRod.Evans@Sun.COM 				    FLG_OF1_OVMACHCAP, CA_SUNW_MACH,
97211827SRod.Evans@Sun.COM 				    &ofl->ofl_ocapset.oc_mach);
97311827SRod.Evans@Sun.COM 				break;
97411827SRod.Evans@Sun.COM 
97511827SRod.Evans@Sun.COM 			case CA_SUNW_ID:
97611827SRod.Evans@Sun.COM 				id_cap(ofl, strs + data->c_un.c_ptr,
97711827SRod.Evans@Sun.COM 				    FLG_OCS_USRDEFID);
97811827SRod.Evans@Sun.COM 				break;
97911827SRod.Evans@Sun.COM 
98011827SRod.Evans@Sun.COM 			default:
98111827SRod.Evans@Sun.COM 				assert(0);	/* Unknown capability type */
98211827SRod.Evans@Sun.COM 			}
98311827SRod.Evans@Sun.COM 		}
98411827SRod.Evans@Sun.COM 
98511827SRod.Evans@Sun.COM 		/*
98611827SRod.Evans@Sun.COM 		 * If there are no symbol capabilities, or this objects
98711827SRod.Evans@Sun.COM 		 * capabilities aren't being transformed into a symbol
98811827SRod.Evans@Sun.COM 		 * capabilities, then we're done.
98911827SRod.Evans@Sun.COM 		 */
99011827SRod.Evans@Sun.COM 		if ((symcapndx == -1) &&
99111827SRod.Evans@Sun.COM 		    ((ofl->ofl_flags & FLG_OF_OTOSCAP) == 0))
99211827SRod.Evans@Sun.COM 			return (1);
99311827SRod.Evans@Sun.COM 	}
99411827SRod.Evans@Sun.COM 
99511827SRod.Evans@Sun.COM 	/*
99611827SRod.Evans@Sun.COM 	 * If these capabilities don't originate from a relocatable object
99711827SRod.Evans@Sun.COM 	 * there's no further processing required.
99811827SRod.Evans@Sun.COM 	 */
99911827SRod.Evans@Sun.COM 	if (ifl->ifl_ehdr->e_type != ET_REL)
100011827SRod.Evans@Sun.COM 		return (1);
100111827SRod.Evans@Sun.COM 
100211827SRod.Evans@Sun.COM 	/*
100311827SRod.Evans@Sun.COM 	 * If this object only defines an object capabilities group, and the
100411827SRod.Evans@Sun.COM 	 * -z symbolcap option is in effect, then all global function symbols
100511827SRod.Evans@Sun.COM 	 * and initialized global data symbols are renamed and assigned to the
100611827SRod.Evans@Sun.COM 	 * transformed symbol capabilities group.
100711827SRod.Evans@Sun.COM 	 */
100811827SRod.Evans@Sun.COM 	if ((objcapndx == 0) &&
100911827SRod.Evans@Sun.COM 	    (symcapndx == -1) && (ofl->ofl_flags & FLG_OF_OTOSCAP))
101011827SRod.Evans@Sun.COM 		ifl->ifl_flags |= FLG_IF_OTOSCAP;
101111827SRod.Evans@Sun.COM 
101211827SRod.Evans@Sun.COM 	/*
101311827SRod.Evans@Sun.COM 	 * Allocate a capabilities descriptor to collect the capabilities data
101411827SRod.Evans@Sun.COM 	 * for this input file.  Allocate a mirror of the raw capabilities data
101511827SRod.Evans@Sun.COM 	 * that points to the individual symbol capabilities groups.  An APlist
101611827SRod.Evans@Sun.COM 	 * is used, although it will be sparsely populated, as the list provides
101711827SRod.Evans@Sun.COM 	 * a convenient mechanism for traversal later.
101811827SRod.Evans@Sun.COM 	 */
101911827SRod.Evans@Sun.COM 	if (((cdp = libld_calloc(sizeof (Cap_desc), 1)) == NULL) ||
102011827SRod.Evans@Sun.COM 	    (aplist_append(&(cdp->ca_groups), NULL, cnum) == NULL))
102111827SRod.Evans@Sun.COM 		return (S_ERROR);
102211827SRod.Evans@Sun.COM 
102311827SRod.Evans@Sun.COM 	/*
102411827SRod.Evans@Sun.COM 	 * Clear the allocated APlist data array, and assign the number of
102511827SRod.Evans@Sun.COM 	 * items as the total number of array items.
102611827SRod.Evans@Sun.COM 	 */
102711827SRod.Evans@Sun.COM 	(void) memset(&cdp->ca_groups->apl_data[0], 0,
102811827SRod.Evans@Sun.COM 	    (cnum * sizeof (void *)));
102911827SRod.Evans@Sun.COM 	cdp->ca_groups->apl_nitems = cnum;
103011827SRod.Evans@Sun.COM 
103111827SRod.Evans@Sun.COM 	ifl->ifl_caps = cdp;
103211827SRod.Evans@Sun.COM 
103311827SRod.Evans@Sun.COM 	/*
103411827SRod.Evans@Sun.COM 	 * Traverse the capabilities data, unpacking the data into a
103511827SRod.Evans@Sun.COM 	 * capabilities set.  Process each capabilities set as a unique group.
103611827SRod.Evans@Sun.COM 	 */
103711827SRod.Evans@Sun.COM 	descapndx = -1;
103811827SRod.Evans@Sun.COM 	nulls = 0;
103911827SRod.Evans@Sun.COM 
104011827SRod.Evans@Sun.COM 	for (ndx = 0, data = cdata; ndx < cnum; ndx++, data++) {
104111827SRod.Evans@Sun.COM 		Capstr	*capstr;
104211827SRod.Evans@Sun.COM 
104311827SRod.Evans@Sun.COM 		switch (data->c_tag) {
104411827SRod.Evans@Sun.COM 		case CA_SUNW_NULL:
104511827SRod.Evans@Sun.COM 			nulls++;
104611827SRod.Evans@Sun.COM 
104711827SRod.Evans@Sun.COM 			/*
104811827SRod.Evans@Sun.COM 			 * Process the capabilities group that this null entry
104911827SRod.Evans@Sun.COM 			 * terminates.  The capabilities group that is returned
105011827SRod.Evans@Sun.COM 			 * will either point to this file's data, or to a
105111827SRod.Evans@Sun.COM 			 * matching capabilities group that has already been
105211827SRod.Evans@Sun.COM 			 * processed.
105311827SRod.Evans@Sun.COM 			 *
105411827SRod.Evans@Sun.COM 			 * Note, if this object defines object capabilities,
105511827SRod.Evans@Sun.COM 			 * the first group descriptor points to these object
105611827SRod.Evans@Sun.COM 			 * capabilities.  It is only necessary to save this
105711827SRod.Evans@Sun.COM 			 * descriptor when object capabilities are being
105811827SRod.Evans@Sun.COM 			 * transformed into symbol capabilities (-z symbolcap).
105911827SRod.Evans@Sun.COM 			 */
106011827SRod.Evans@Sun.COM 			if (descapndx != -1) {
106111827SRod.Evans@Sun.COM 				if ((nulls > 1) ||
106211827SRod.Evans@Sun.COM 				    (ifl->ifl_flags & FLG_IF_OTOSCAP)) {
106311827SRod.Evans@Sun.COM 					APlist	*alp = cdp->ca_groups;
106411827SRod.Evans@Sun.COM 
106511827SRod.Evans@Sun.COM 					if ((alp->apl_data[descapndx] =
106611827SRod.Evans@Sun.COM 					    get_cap_group(&ocapset,
106711827SRod.Evans@Sun.COM 					    (ndx - descapndx), ofl,
106811827SRod.Evans@Sun.COM 					    cisp)) == NULL)
106911827SRod.Evans@Sun.COM 						return (S_ERROR);
107011827SRod.Evans@Sun.COM 				}
107111827SRod.Evans@Sun.COM 
107211827SRod.Evans@Sun.COM 				/*
107311827SRod.Evans@Sun.COM 				 * Clean up the capabilities data in preparation
107411827SRod.Evans@Sun.COM 				 * for processing additional groups.  If the
107511827SRod.Evans@Sun.COM 				 * collected capabilities strings were used to
107611827SRod.Evans@Sun.COM 				 * establish a new output group, they will have
107711827SRod.Evans@Sun.COM 				 * been saved in get_cap_group().  If these
107811827SRod.Evans@Sun.COM 				 * descriptors still exist, then an existing
107911827SRod.Evans@Sun.COM 				 * descriptor has been used to associate with
108011827SRod.Evans@Sun.COM 				 * this file, and these string descriptors can
108111827SRod.Evans@Sun.COM 				 * be freed.
108211827SRod.Evans@Sun.COM 				 */
108311827SRod.Evans@Sun.COM 				ocapset.oc_hw_1.cm_val =
108411827SRod.Evans@Sun.COM 				    ocapset.oc_sf_1.cm_val =
108511827SRod.Evans@Sun.COM 				    ocapset.oc_hw_2.cm_val = 0;
108611827SRod.Evans@Sun.COM 				if (ocapset.oc_plat.cl_val) {
108711827SRod.Evans@Sun.COM 					free((void *)ocapset.oc_plat.cl_val);
108811827SRod.Evans@Sun.COM 					ocapset.oc_plat.cl_val = NULL;
108911827SRod.Evans@Sun.COM 				}
109011827SRod.Evans@Sun.COM 				if (ocapset.oc_mach.cl_val) {
109111827SRod.Evans@Sun.COM 					free((void *)ocapset.oc_mach.cl_val);
109211827SRod.Evans@Sun.COM 					ocapset.oc_mach.cl_val = NULL;
109311827SRod.Evans@Sun.COM 				}
109411827SRod.Evans@Sun.COM 				descapndx = -1;
109511827SRod.Evans@Sun.COM 			}
109611827SRod.Evans@Sun.COM 			continue;
109711827SRod.Evans@Sun.COM 
109811827SRod.Evans@Sun.COM 		case CA_SUNW_HW_1:
109911827SRod.Evans@Sun.COM 			ocapset.oc_hw_1.cm_val = data->c_un.c_val;
110011827SRod.Evans@Sun.COM 			DBG_CALL(Dbg_cap_val_entry(ofl->ofl_lml,
110111827SRod.Evans@Sun.COM 			    DBG_STATE_ORIGINAL, CA_SUNW_HW_1,
110211827SRod.Evans@Sun.COM 			    ocapset.oc_hw_1.cm_val, ld_targ.t_m.m_mach));
110311827SRod.Evans@Sun.COM 			break;
110411827SRod.Evans@Sun.COM 
110511827SRod.Evans@Sun.COM 		case CA_SUNW_SF_1:
110611827SRod.Evans@Sun.COM 			ocapset.oc_sf_1.cm_val = data->c_un.c_val;
110711827SRod.Evans@Sun.COM 			DBG_CALL(Dbg_cap_val_entry(ofl->ofl_lml,
110811827SRod.Evans@Sun.COM 			    DBG_STATE_ORIGINAL, CA_SUNW_SF_1,
110911827SRod.Evans@Sun.COM 			    ocapset.oc_sf_1.cm_val, ld_targ.t_m.m_mach));
111011827SRod.Evans@Sun.COM 			break;
111111827SRod.Evans@Sun.COM 
111211827SRod.Evans@Sun.COM 		case CA_SUNW_HW_2:
111311827SRod.Evans@Sun.COM 			ocapset.oc_hw_2.cm_val = data->c_un.c_val;
111411827SRod.Evans@Sun.COM 			DBG_CALL(Dbg_cap_val_entry(ofl->ofl_lml,
111511827SRod.Evans@Sun.COM 			    DBG_STATE_ORIGINAL, CA_SUNW_HW_2,
111611827SRod.Evans@Sun.COM 			    ocapset.oc_hw_2.cm_val, ld_targ.t_m.m_mach));
111711827SRod.Evans@Sun.COM 			break;
111811827SRod.Evans@Sun.COM 
111911827SRod.Evans@Sun.COM 		case CA_SUNW_PLAT:
112011827SRod.Evans@Sun.COM 			if ((capstr = alist_append(&ocapset.oc_plat.cl_val,
112111827SRod.Evans@Sun.COM 			    NULL, sizeof (Capstr), AL_CNT_CAP_NAMES)) == NULL)
112211827SRod.Evans@Sun.COM 				return (S_ERROR);
112311827SRod.Evans@Sun.COM 			capstr->cs_str = strs + data->c_un.c_ptr;
112411827SRod.Evans@Sun.COM 			DBG_CALL(Dbg_cap_ptr_entry(ofl->ofl_lml,
112511827SRod.Evans@Sun.COM 			    DBG_STATE_ORIGINAL, CA_SUNW_PLAT, capstr->cs_str));
112611827SRod.Evans@Sun.COM 			break;
112711827SRod.Evans@Sun.COM 
112811827SRod.Evans@Sun.COM 		case CA_SUNW_MACH:
112911827SRod.Evans@Sun.COM 			if ((capstr = alist_append(&ocapset.oc_mach.cl_val,
113011827SRod.Evans@Sun.COM 			    NULL, sizeof (Capstr), AL_CNT_CAP_NAMES)) == NULL)
113111827SRod.Evans@Sun.COM 				return (S_ERROR);
113211827SRod.Evans@Sun.COM 			capstr->cs_str = strs + data->c_un.c_ptr;
113311827SRod.Evans@Sun.COM 			DBG_CALL(Dbg_cap_ptr_entry(ofl->ofl_lml,
113411827SRod.Evans@Sun.COM 			    DBG_STATE_ORIGINAL, CA_SUNW_MACH, capstr->cs_str));
113511827SRod.Evans@Sun.COM 			break;
113611827SRod.Evans@Sun.COM 
113711827SRod.Evans@Sun.COM 		case CA_SUNW_ID:
113811827SRod.Evans@Sun.COM 			ocapset.oc_id.cs_str = strs + data->c_un.c_ptr;
113911827SRod.Evans@Sun.COM 			DBG_CALL(Dbg_cap_ptr_entry(ofl->ofl_lml,
114011827SRod.Evans@Sun.COM 			    DBG_STATE_ORIGINAL, CA_SUNW_ID,
114111827SRod.Evans@Sun.COM 			    ocapset.oc_id.cs_str));
114211827SRod.Evans@Sun.COM 			break;
114311827SRod.Evans@Sun.COM 		}
114411827SRod.Evans@Sun.COM 
114511827SRod.Evans@Sun.COM 		/*
114611827SRod.Evans@Sun.COM 		 * Save the start of this new group.
114711827SRod.Evans@Sun.COM 		 */
114811827SRod.Evans@Sun.COM 		if (descapndx == -1)
114911827SRod.Evans@Sun.COM 			descapndx = ndx;
115011827SRod.Evans@Sun.COM 	}
115111827SRod.Evans@Sun.COM 	return (1);
115211827SRod.Evans@Sun.COM }
115311827SRod.Evans@Sun.COM 
115411827SRod.Evans@Sun.COM /*
115511827SRod.Evans@Sun.COM  * Capture any symbol capabilities symbols.  An object file that contains symbol
115611827SRod.Evans@Sun.COM  * capabilities has an associated .SUNW_capinfo section.  This section
115711827SRod.Evans@Sun.COM  * identifies which symbols are associated to which capabilities, together with
115811827SRod.Evans@Sun.COM  * their associated lead symbol.  Each of these symbol pairs are recorded for
115911827SRod.Evans@Sun.COM  * processing later.
116011827SRod.Evans@Sun.COM  */
116111827SRod.Evans@Sun.COM static uintptr_t
process_capinfo(Ofl_desc * ofl,Ifl_desc * ifl,Is_desc * isp)116211827SRod.Evans@Sun.COM process_capinfo(Ofl_desc *ofl, Ifl_desc *ifl, Is_desc *isp)
116311827SRod.Evans@Sun.COM {
116411827SRod.Evans@Sun.COM 	Cap_desc	*cdp = ifl->ifl_caps;
116511827SRod.Evans@Sun.COM 	Capinfo		*capinfo = isp->is_indata->d_buf;
116611827SRod.Evans@Sun.COM 	Shdr		*shdr = isp->is_shdr;
116711827SRod.Evans@Sun.COM 	Word		cndx, capinfonum;
116811827SRod.Evans@Sun.COM 
116911827SRod.Evans@Sun.COM 	capinfonum = (Word)(shdr->sh_size / shdr->sh_entsize);
117011827SRod.Evans@Sun.COM 
117111827SRod.Evans@Sun.COM 	if ((cdp == NULL) || (capinfo == NULL) || (capinfonum == 0))
117211827SRod.Evans@Sun.COM 		return (0);
117311827SRod.Evans@Sun.COM 
117411827SRod.Evans@Sun.COM 	for (cndx = 1, capinfo++; cndx < capinfonum; cndx++, capinfo++) {
117511827SRod.Evans@Sun.COM 		Sym_desc	*sdp, *lsdp;
117611827SRod.Evans@Sun.COM 		Word		lndx;
117711827SRod.Evans@Sun.COM 		uchar_t		gndx;
117811827SRod.Evans@Sun.COM 
117911827SRod.Evans@Sun.COM 		if ((gndx = (uchar_t)ELF_C_GROUP(*capinfo)) == 0)
118011827SRod.Evans@Sun.COM 			continue;
118111827SRod.Evans@Sun.COM 		lndx = (Word)ELF_C_SYM(*capinfo);
118211827SRod.Evans@Sun.COM 
118311827SRod.Evans@Sun.COM 		/*
118411827SRod.Evans@Sun.COM 		 * Catch any anomalies.  A capabilities symbol should be valid,
118511827SRod.Evans@Sun.COM 		 * and the capabilities lead symbol should also be global.
118611827SRod.Evans@Sun.COM 		 * Note, ld(1) -z symbolcap would create local capabilities
118711827SRod.Evans@Sun.COM 		 * symbols, but we don't enforce this so as to give the
118811827SRod.Evans@Sun.COM 		 * compilation environment a little more freedom.
118911827SRod.Evans@Sun.COM 		 */
119011827SRod.Evans@Sun.COM 		if ((sdp = ifl->ifl_oldndx[cndx]) == NULL) {
1191*13074SAli.Bahrami@Oracle.COM 			ld_eprintf(ofl, ERR_WARNING,
119211827SRod.Evans@Sun.COM 			    MSG_INTL(MSG_CAPINFO_INVALSYM), ifl->ifl_name,
119311827SRod.Evans@Sun.COM 			    EC_WORD(isp->is_scnndx), isp->is_name, cndx,
119411827SRod.Evans@Sun.COM 			    MSG_INTL(MSG_STR_UNKNOWN));
119511827SRod.Evans@Sun.COM 			continue;
119611827SRod.Evans@Sun.COM 		}
119711827SRod.Evans@Sun.COM 		if ((lndx == 0) || (lndx >= ifl->ifl_symscnt) ||
119811827SRod.Evans@Sun.COM 		    ((lsdp = ifl->ifl_oldndx[lndx]) == NULL) ||
119911827SRod.Evans@Sun.COM 		    (ELF_ST_BIND(lsdp->sd_sym->st_info) != STB_GLOBAL)) {
1200*13074SAli.Bahrami@Oracle.COM 			ld_eprintf(ofl, ERR_WARNING,
120111827SRod.Evans@Sun.COM 			    MSG_INTL(MSG_CAPINFO_INVALLEAD), ifl->ifl_name,
120211827SRod.Evans@Sun.COM 			    EC_WORD(isp->is_scnndx), isp->is_name, cndx, lsdp ?
120311827SRod.Evans@Sun.COM 			    demangle(lsdp->sd_name) : MSG_INTL(MSG_STR_UNKNOWN),
120411827SRod.Evans@Sun.COM 			    lndx);
120511827SRod.Evans@Sun.COM 			continue;
120611827SRod.Evans@Sun.COM 		}
120711827SRod.Evans@Sun.COM 
120811827SRod.Evans@Sun.COM 		/*
120911827SRod.Evans@Sun.COM 		 * Indicate that this is a capabilities symbol.
121011827SRod.Evans@Sun.COM 		 */
121111827SRod.Evans@Sun.COM 		sdp->sd_flags |= FLG_SY_CAP;
121211827SRod.Evans@Sun.COM 
121311827SRod.Evans@Sun.COM 		/*
121411827SRod.Evans@Sun.COM 		 * Save any global capability symbols.  Global capability
121511827SRod.Evans@Sun.COM 		 * symbols are identified with a CAPINFO_SUNW_GLOB group id.
121611827SRod.Evans@Sun.COM 		 * The lead symbol for this global capability symbol is either
121711827SRod.Evans@Sun.COM 		 * the symbol itself, or an alias.
121811827SRod.Evans@Sun.COM 		 */
121911827SRod.Evans@Sun.COM 		if (gndx == CAPINFO_SUNW_GLOB) {
122011827SRod.Evans@Sun.COM 			if (ld_cap_add_family(ofl, lsdp, sdp,
122111827SRod.Evans@Sun.COM 			    NULL, NULL) == S_ERROR)
122211827SRod.Evans@Sun.COM 				return (S_ERROR);
122311827SRod.Evans@Sun.COM 			continue;
122411827SRod.Evans@Sun.COM 		}
122511827SRod.Evans@Sun.COM 
122611827SRod.Evans@Sun.COM 		/*
122711827SRod.Evans@Sun.COM 		 * Track the number of non-global capabilities symbols, as these
122811827SRod.Evans@Sun.COM 		 * are used to size any symbol tables.  If we're generating a
122911827SRod.Evans@Sun.COM 		 * dynamic object, this symbol will be added to the dynamic
123011827SRod.Evans@Sun.COM 		 * symbol table, therefore ensure there is space in the dynamic
123111827SRod.Evans@Sun.COM 		 * string table.
123211827SRod.Evans@Sun.COM 		 */
123311827SRod.Evans@Sun.COM 		ofl->ofl_caploclcnt++;
123411827SRod.Evans@Sun.COM 		if (((ofl->ofl_flags & FLG_OF_RELOBJ) == 0) &&
123511827SRod.Evans@Sun.COM 		    (st_insert(ofl->ofl_dynstrtab, sdp->sd_name) == -1))
123611827SRod.Evans@Sun.COM 			return (S_ERROR);
123711827SRod.Evans@Sun.COM 
123811827SRod.Evans@Sun.COM 		/*
123911827SRod.Evans@Sun.COM 		 * As we're tracking this local symbol as a capabilities symbol,
124011827SRod.Evans@Sun.COM 		 * reduce the local symbol count to compensate.
124111827SRod.Evans@Sun.COM 		 */
124211827SRod.Evans@Sun.COM 		ofl->ofl_locscnt--;
124311827SRod.Evans@Sun.COM 
124411827SRod.Evans@Sun.COM 		/*
124511827SRod.Evans@Sun.COM 		 * Determine whether the associated lead symbol indicates
124611827SRod.Evans@Sun.COM 		 * NODYNSORT.  If so, remove this local entry from the
124711827SRod.Evans@Sun.COM 		 * SUNW_dynsort section too.  NODYNSORT tagging can only be
124811827SRod.Evans@Sun.COM 		 * obtained from a mapfile symbol definition, and thus any
124911827SRod.Evans@Sun.COM 		 * global definition that has this tagging has already been
125011827SRod.Evans@Sun.COM 		 * instantiated and this instance resolved to it.
125111827SRod.Evans@Sun.COM 		 */
125211827SRod.Evans@Sun.COM 		if (lsdp->sd_flags & FLG_SY_NODYNSORT) {
125311827SRod.Evans@Sun.COM 			Sym	*lsym = lsdp->sd_sym;
125411827SRod.Evans@Sun.COM 			uchar_t ltype = ELF_ST_TYPE(lsym->st_info);
125511827SRod.Evans@Sun.COM 
125611827SRod.Evans@Sun.COM 			DYNSORT_COUNT(lsdp, lsym, ltype, --);
125711827SRod.Evans@Sun.COM 			lsdp->sd_flags |= FLG_SY_NODYNSORT;
125811827SRod.Evans@Sun.COM 		}
125911827SRod.Evans@Sun.COM 
126011827SRod.Evans@Sun.COM 		/*
126111827SRod.Evans@Sun.COM 		 * Track this family member, together with its associated group.
126211827SRod.Evans@Sun.COM 		 */
126311827SRod.Evans@Sun.COM 		if (ld_cap_add_family(ofl, lsdp, sdp,
126411827SRod.Evans@Sun.COM 		    cdp->ca_groups->apl_data[gndx], NULL) == S_ERROR)
126511827SRod.Evans@Sun.COM 			return (S_ERROR);
126611827SRod.Evans@Sun.COM 	}
126711827SRod.Evans@Sun.COM 
126811827SRod.Evans@Sun.COM 	return (0);
12690Sstevel@tonic-gate }
12700Sstevel@tonic-gate 
12710Sstevel@tonic-gate /*
12720Sstevel@tonic-gate  * Simply process the section so that we have pointers to the data for use
12730Sstevel@tonic-gate  * in later routines, however don't add the section to the output section
12740Sstevel@tonic-gate  * list as we will be creating our own replacement sections later (ie.
12750Sstevel@tonic-gate  * symtab and relocation).
12760Sstevel@tonic-gate  */
12771618Srie static uintptr_t
12780Sstevel@tonic-gate /* ARGSUSED5 */
process_input(const char * name,Ifl_desc * ifl,Shdr * shdr,Elf_Scn * scn,Word ndx,int ident,Ofl_desc * ofl)12790Sstevel@tonic-gate process_input(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
12807463SRod.Evans@Sun.COM     Word ndx, int ident, Ofl_desc *ofl)
12810Sstevel@tonic-gate {
12826206Sab196087 	return (process_section(name, ifl, shdr, scn, ndx,
12836206Sab196087 	    ld_targ.t_id.id_null, ofl));
12840Sstevel@tonic-gate }
12850Sstevel@tonic-gate 
12860Sstevel@tonic-gate /*
12870Sstevel@tonic-gate  * Keep a running count of relocation entries from input relocatable objects for
12880Sstevel@tonic-gate  * sizing relocation buckets later.  If we're building an executable, save any
12890Sstevel@tonic-gate  * relocations from shared objects to determine if any copy relocation symbol
12900Sstevel@tonic-gate  * has a displacement relocation against it.
12910Sstevel@tonic-gate  */
12921618Srie static uintptr_t
12930Sstevel@tonic-gate /* ARGSUSED5 */
process_reloc(const char * name,Ifl_desc * ifl,Shdr * shdr,Elf_Scn * scn,Word ndx,int ident,Ofl_desc * ofl)12940Sstevel@tonic-gate process_reloc(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
12957463SRod.Evans@Sun.COM     Word ndx, int ident, Ofl_desc *ofl)
12960Sstevel@tonic-gate {
12970Sstevel@tonic-gate 	if (process_section(name, ifl,
12986206Sab196087 	    shdr, scn, ndx, ld_targ.t_id.id_null, ofl) == S_ERROR)
12990Sstevel@tonic-gate 		return (S_ERROR);
13000Sstevel@tonic-gate 
13010Sstevel@tonic-gate 	if (ifl->ifl_ehdr->e_type == ET_REL) {
13020Sstevel@tonic-gate 		if (shdr->sh_entsize && (shdr->sh_entsize <= shdr->sh_size))
13030Sstevel@tonic-gate 			/* LINTED */
13040Sstevel@tonic-gate 			ofl->ofl_relocincnt +=
13050Sstevel@tonic-gate 			    (Word)(shdr->sh_size / shdr->sh_entsize);
13060Sstevel@tonic-gate 	} else if (ofl->ofl_flags & FLG_OF_EXEC) {
13079131SRod.Evans@Sun.COM 		if (aplist_append(&ifl->ifl_relsect, ifl->ifl_isdesc[ndx],
13089131SRod.Evans@Sun.COM 		    AL_CNT_IFL_RELSECS) == NULL)
13090Sstevel@tonic-gate 			return (S_ERROR);
13100Sstevel@tonic-gate 	}
13110Sstevel@tonic-gate 	return (1);
13120Sstevel@tonic-gate }
13130Sstevel@tonic-gate 
13140Sstevel@tonic-gate /*
13150Sstevel@tonic-gate  * Process a string table section.  A valid section contains an initial and
13160Sstevel@tonic-gate  * final null byte.
13170Sstevel@tonic-gate  */
13181618Srie static uintptr_t
process_strtab(const char * name,Ifl_desc * ifl,Shdr * shdr,Elf_Scn * scn,Word ndx,int ident,Ofl_desc * ofl)13190Sstevel@tonic-gate process_strtab(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
13207463SRod.Evans@Sun.COM     Word ndx, int ident, Ofl_desc *ofl)
13210Sstevel@tonic-gate {
13220Sstevel@tonic-gate 	char		*data;
13230Sstevel@tonic-gate 	size_t		size;
13240Sstevel@tonic-gate 	Is_desc		*isp;
13250Sstevel@tonic-gate 	uintptr_t	error;
13260Sstevel@tonic-gate 
13270Sstevel@tonic-gate 	/*
13280Sstevel@tonic-gate 	 * Never include .stab.excl sections in any output file.
13290Sstevel@tonic-gate 	 * If the -s flag has been specified strip any .stab sections.
13300Sstevel@tonic-gate 	 */
13310Sstevel@tonic-gate 	if (((ofl->ofl_flags & FLG_OF_STRIP) && ident &&
13320Sstevel@tonic-gate 	    (strncmp(name, MSG_ORIG(MSG_SCN_STAB), MSG_SCN_STAB_SIZE) == 0)) ||
13330Sstevel@tonic-gate 	    (strcmp(name, MSG_ORIG(MSG_SCN_STABEXCL)) == 0) && ident)
13340Sstevel@tonic-gate 		return (1);
13350Sstevel@tonic-gate 
13360Sstevel@tonic-gate 	/*
13370Sstevel@tonic-gate 	 * If we got here to process a .shstrtab or .dynstr table, `ident' will
13380Sstevel@tonic-gate 	 * be null.  Otherwise make sure we don't have a .strtab section as this
13390Sstevel@tonic-gate 	 * should not be added to the output section list either.
13400Sstevel@tonic-gate 	 */
13416206Sab196087 	if ((ident != ld_targ.t_id.id_null) &&
13420Sstevel@tonic-gate 	    (strcmp(name, MSG_ORIG(MSG_SCN_STRTAB)) == 0))
13436206Sab196087 		ident = ld_targ.t_id.id_null;
13440Sstevel@tonic-gate 
13450Sstevel@tonic-gate 	error = process_section(name, ifl, shdr, scn, ndx, ident, ofl);
13460Sstevel@tonic-gate 	if ((error == 0) || (error == S_ERROR))
13470Sstevel@tonic-gate 		return (error);
13480Sstevel@tonic-gate 
13490Sstevel@tonic-gate 	/*
13500Sstevel@tonic-gate 	 * String tables should start and end with a NULL byte.  Note, it has
13510Sstevel@tonic-gate 	 * been known for the assembler to create empty string tables, so check
13520Sstevel@tonic-gate 	 * the size before attempting to verify the data itself.
13530Sstevel@tonic-gate 	 */
13540Sstevel@tonic-gate 	isp = ifl->ifl_isdesc[ndx];
13550Sstevel@tonic-gate 	size = isp->is_indata->d_size;
13560Sstevel@tonic-gate 	if (size) {
13570Sstevel@tonic-gate 		data = isp->is_indata->d_buf;
13580Sstevel@tonic-gate 		if (data[0] != '\0' || data[size - 1] != '\0')
1359*13074SAli.Bahrami@Oracle.COM 			ld_eprintf(ofl, ERR_WARNING,
13609878SAli.Bahrami@Sun.COM 			    MSG_INTL(MSG_FIL_MALSTR), ifl->ifl_name,
13619878SAli.Bahrami@Sun.COM 			    EC_WORD(isp->is_scnndx), name);
13620Sstevel@tonic-gate 	} else
13630Sstevel@tonic-gate 		isp->is_indata->d_buf = (void *)MSG_ORIG(MSG_STR_EMPTY);
13640Sstevel@tonic-gate 
13650Sstevel@tonic-gate 	ifl->ifl_flags |= FLG_IF_HSTRTAB;
13660Sstevel@tonic-gate 	return (1);
13670Sstevel@tonic-gate }
13680Sstevel@tonic-gate 
13690Sstevel@tonic-gate /*
13700Sstevel@tonic-gate  * Invalid sections produce a warning and are skipped.
13710Sstevel@tonic-gate  */
13721618Srie static uintptr_t
13730Sstevel@tonic-gate /* ARGSUSED3 */
invalid_section(const char * name,Ifl_desc * ifl,Shdr * shdr,Elf_Scn * scn,Word ndx,int ident,Ofl_desc * ofl)13740Sstevel@tonic-gate invalid_section(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
13750Sstevel@tonic-gate     Word ndx, int ident, Ofl_desc *ofl)
13760Sstevel@tonic-gate {
13774734Sab196087 	Conv_inv_buf_t inv_buf;
13784734Sab196087 
1379*13074SAli.Bahrami@Oracle.COM 	ld_eprintf(ofl, ERR_WARNING, MSG_INTL(MSG_FIL_INVALSEC),
13809878SAli.Bahrami@Sun.COM 	    ifl->ifl_name, EC_WORD(ndx), name,
13819273SAli.Bahrami@Sun.COM 	    conv_sec_type(ifl->ifl_ehdr->e_ident[EI_OSABI],
13829273SAli.Bahrami@Sun.COM 	    ifl->ifl_ehdr->e_machine, shdr->sh_type, 0, &inv_buf));
13830Sstevel@tonic-gate 	return (1);
13840Sstevel@tonic-gate }
13850Sstevel@tonic-gate 
13860Sstevel@tonic-gate /*
138710454SAli.Bahrami@Sun.COM  * Compare an input section name to a given string, taking the ELF '%'
138810454SAli.Bahrami@Sun.COM  * section naming convention into account. If an input section name
138910454SAli.Bahrami@Sun.COM  * contains a '%' character, the '%' and all following characters are
139010454SAli.Bahrami@Sun.COM  * ignored in the comparison.
139110454SAli.Bahrami@Sun.COM  *
139210454SAli.Bahrami@Sun.COM  * entry:
139310454SAli.Bahrami@Sun.COM  *	is_name - Name of input section
139410454SAli.Bahrami@Sun.COM  *	match_name - Name to compare to
139510454SAli.Bahrami@Sun.COM  *	match_len - strlen(match_name)
139610454SAli.Bahrami@Sun.COM  *
139710454SAli.Bahrami@Sun.COM  * exit:
139810454SAli.Bahrami@Sun.COM  *	Returns True (1) if the names match, and False (0) otherwise.
139910454SAli.Bahrami@Sun.COM  */
140010454SAli.Bahrami@Sun.COM inline static int
is_name_cmp(const char * is_name,const char * match_name,size_t match_len)140110454SAli.Bahrami@Sun.COM is_name_cmp(const char *is_name, const char *match_name, size_t match_len)
140210454SAli.Bahrami@Sun.COM {
140310454SAli.Bahrami@Sun.COM 	/*
140410454SAli.Bahrami@Sun.COM 	 * If the start of is_name is not a match for name,
140510454SAli.Bahrami@Sun.COM 	 * the match fails.
140610454SAli.Bahrami@Sun.COM 	 */
140710454SAli.Bahrami@Sun.COM 	if (strncmp(is_name, match_name, match_len) != 0)
140810454SAli.Bahrami@Sun.COM 		return (0);
140910454SAli.Bahrami@Sun.COM 
141010454SAli.Bahrami@Sun.COM 	/*
141110454SAli.Bahrami@Sun.COM 	 * The prefix matched. The next character must be either '%', or
141210454SAli.Bahrami@Sun.COM 	 * NULL, in order for a match to be true.
141310454SAli.Bahrami@Sun.COM 	 */
141410454SAli.Bahrami@Sun.COM 	is_name += match_len;
141510454SAli.Bahrami@Sun.COM 	return ((*is_name == '\0') || (*is_name == '%'));
141610454SAli.Bahrami@Sun.COM }
141710454SAli.Bahrami@Sun.COM 
141810454SAli.Bahrami@Sun.COM /*
141911993SAli.Bahrami@Sun.COM  * Helper routine for process_progbits() to process allocable sections.
142011993SAli.Bahrami@Sun.COM  *
142111993SAli.Bahrami@Sun.COM  * entry:
142211993SAli.Bahrami@Sun.COM  *	name, ifl, shdr, ndx, ident, ofl - As passed to process_progbits().
142311993SAli.Bahrami@Sun.COM  *	is_stab_index - TRUE if section is .index.
142411993SAli.Bahrami@Sun.COM  *	is_flags - Additional flags to be added to the input section.
142511993SAli.Bahrami@Sun.COM  *
142611993SAli.Bahrami@Sun.COM  * exit:
142711993SAli.Bahrami@Sun.COM  *	The allocable section has been processed. *ident and *is_flags
142811993SAli.Bahrami@Sun.COM  *	are updated as necessary to reflect the changes. Returns TRUE
142911993SAli.Bahrami@Sun.COM  *	for success, FALSE for failure.
143011993SAli.Bahrami@Sun.COM  */
143111993SAli.Bahrami@Sun.COM inline static Boolean
process_progbits_alloc(const char * name,Ifl_desc * ifl,Shdr * shdr,Word ndx,int * ident,Ofl_desc * ofl,Boolean is_stab_index,Word * is_flags)143211993SAli.Bahrami@Sun.COM process_progbits_alloc(const char *name, Ifl_desc *ifl, Shdr *shdr,
143311993SAli.Bahrami@Sun.COM     Word ndx, int *ident, Ofl_desc *ofl, Boolean is_stab_index,
143411993SAli.Bahrami@Sun.COM     Word *is_flags)
143511993SAli.Bahrami@Sun.COM {
143611993SAli.Bahrami@Sun.COM 	Boolean done = FALSE;
143711993SAli.Bahrami@Sun.COM 
143811993SAli.Bahrami@Sun.COM 	if (name[0] == '.') {
143911993SAli.Bahrami@Sun.COM 		Conv_inv_buf_t inv_buf1, inv_buf2;
144011993SAli.Bahrami@Sun.COM 
144111993SAli.Bahrami@Sun.COM 		switch (name[1]) {
144211993SAli.Bahrami@Sun.COM 		case 'e':
144311993SAli.Bahrami@Sun.COM 			if (!is_name_cmp(name, MSG_ORIG(MSG_SCN_EHFRAME),
144411993SAli.Bahrami@Sun.COM 			    MSG_SCN_EHFRAME_SIZE))
144511993SAli.Bahrami@Sun.COM 				break;
144611993SAli.Bahrami@Sun.COM 
144711993SAli.Bahrami@Sun.COM 			*ident = ld_targ.t_id.id_unwind;
144811993SAli.Bahrami@Sun.COM 			*is_flags |= FLG_IS_EHFRAME;
144911993SAli.Bahrami@Sun.COM 			done = TRUE;
145011993SAli.Bahrami@Sun.COM 
145111993SAli.Bahrami@Sun.COM 			/*
145211993SAli.Bahrami@Sun.COM 			 * Only accept a progbits .eh_frame on a platform
145311993SAli.Bahrami@Sun.COM 			 * for which this is the expected type.
145411993SAli.Bahrami@Sun.COM 			 */
145511993SAli.Bahrami@Sun.COM 			if (ld_targ.t_m.m_sht_unwind == SHT_PROGBITS)
145611993SAli.Bahrami@Sun.COM 				break;
1457*13074SAli.Bahrami@Oracle.COM 			ld_eprintf(ofl, ERR_FATAL,
145811993SAli.Bahrami@Sun.COM 			    MSG_INTL(MSG_FIL_EXEHFRMTYP), ifl->ifl_name,
145911993SAli.Bahrami@Sun.COM 			    EC_WORD(ndx), name,
146011993SAli.Bahrami@Sun.COM 			    conv_sec_type(ifl->ifl_ehdr->e_ident[EI_OSABI],
146111993SAli.Bahrami@Sun.COM 			    ifl->ifl_ehdr->e_machine, shdr->sh_type,
146211993SAli.Bahrami@Sun.COM 			    CONV_FMT_ALT_CF, &inv_buf1),
146311993SAli.Bahrami@Sun.COM 			    conv_sec_type(ifl->ifl_ehdr->e_ident[EI_OSABI],
146411993SAli.Bahrami@Sun.COM 			    ifl->ifl_ehdr->e_machine, ld_targ.t_m.m_sht_unwind,
146511993SAli.Bahrami@Sun.COM 			    CONV_FMT_ALT_CF, &inv_buf2));
146611993SAli.Bahrami@Sun.COM 			return (FALSE);
146711993SAli.Bahrami@Sun.COM 		case 'g':
146811993SAli.Bahrami@Sun.COM 			if (is_name_cmp(name, MSG_ORIG(MSG_SCN_GOT),
146911993SAli.Bahrami@Sun.COM 			    MSG_SCN_GOT_SIZE)) {
147011993SAli.Bahrami@Sun.COM 				*ident = ld_targ.t_id.id_null;
147111993SAli.Bahrami@Sun.COM 				done = TRUE;
147211993SAli.Bahrami@Sun.COM 				break;
147311993SAli.Bahrami@Sun.COM 			}
147411993SAli.Bahrami@Sun.COM 			if ((ld_targ.t_m.m_sht_unwind == SHT_PROGBITS) &&
147511993SAli.Bahrami@Sun.COM 			    is_name_cmp(name, MSG_ORIG(MSG_SCN_GCC_X_TBL),
147611993SAli.Bahrami@Sun.COM 			    MSG_SCN_GCC_X_TBL_SIZE)) {
147711993SAli.Bahrami@Sun.COM 				*ident = ld_targ.t_id.id_unwind;
147811993SAli.Bahrami@Sun.COM 				done = TRUE;
147911993SAli.Bahrami@Sun.COM 				break;
148011993SAli.Bahrami@Sun.COM 			}
148111993SAli.Bahrami@Sun.COM 			break;
148211993SAli.Bahrami@Sun.COM 		case 'p':
148311993SAli.Bahrami@Sun.COM 			if (is_name_cmp(name, MSG_ORIG(MSG_SCN_PLT),
148411993SAli.Bahrami@Sun.COM 			    MSG_SCN_PLT_SIZE)) {
148511993SAli.Bahrami@Sun.COM 				*ident = ld_targ.t_id.id_null;
148611993SAli.Bahrami@Sun.COM 				done = TRUE;
148711993SAli.Bahrami@Sun.COM 			}
148811993SAli.Bahrami@Sun.COM 			break;
148911993SAli.Bahrami@Sun.COM 		}
149011993SAli.Bahrami@Sun.COM 	}
149111993SAli.Bahrami@Sun.COM 	if (!done) {
149211993SAli.Bahrami@Sun.COM 		if (is_stab_index) {
149311993SAli.Bahrami@Sun.COM 			/*
149411993SAli.Bahrami@Sun.COM 			 * This is a work-around for x86 compilers that have
149511993SAli.Bahrami@Sun.COM 			 * set SHF_ALLOC for the .stab.index section.
149611993SAli.Bahrami@Sun.COM 			 *
149711993SAli.Bahrami@Sun.COM 			 * Because of this, make sure that the .stab.index
149811993SAli.Bahrami@Sun.COM 			 * does not end up as the last section in the text
149911993SAli.Bahrami@Sun.COM 			 * segment. Older linkers can produce segmentation
150011993SAli.Bahrami@Sun.COM 			 * violations when they strip (ld -s) against a
150111993SAli.Bahrami@Sun.COM 			 * shared object whose last section in the text
150211993SAli.Bahrami@Sun.COM 			 * segment is a .stab.
150311993SAli.Bahrami@Sun.COM 			 */
150411993SAli.Bahrami@Sun.COM 			*ident = ld_targ.t_id.id_interp;
150511993SAli.Bahrami@Sun.COM 		} else {
150611993SAli.Bahrami@Sun.COM 			*ident = ld_targ.t_id.id_data;
150711993SAli.Bahrami@Sun.COM 		}
150811993SAli.Bahrami@Sun.COM 	}
150911993SAli.Bahrami@Sun.COM 
151011993SAli.Bahrami@Sun.COM 	return (TRUE);
151111993SAli.Bahrami@Sun.COM }
151211993SAli.Bahrami@Sun.COM 
151311993SAli.Bahrami@Sun.COM /*
15140Sstevel@tonic-gate  * Process a progbits section.
15150Sstevel@tonic-gate  */
15161618Srie static uintptr_t
process_progbits(const char * name,Ifl_desc * ifl,Shdr * shdr,Elf_Scn * scn,Word ndx,int ident,Ofl_desc * ofl)15170Sstevel@tonic-gate process_progbits(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
15180Sstevel@tonic-gate     Word ndx, int ident, Ofl_desc *ofl)
15190Sstevel@tonic-gate {
152011993SAli.Bahrami@Sun.COM 	Boolean		is_stab_index = FALSE;
15219085SAli.Bahrami@Sun.COM 	Word		is_flags = 0;
15229085SAli.Bahrami@Sun.COM 	uintptr_t	r;
15230Sstevel@tonic-gate 
15240Sstevel@tonic-gate 	/*
15250Sstevel@tonic-gate 	 * Never include .stab.excl sections in any output file.
15260Sstevel@tonic-gate 	 * If the -s flag has been specified strip any .stab sections.
15270Sstevel@tonic-gate 	 */
15280Sstevel@tonic-gate 	if (ident && (strncmp(name, MSG_ORIG(MSG_SCN_STAB),
15290Sstevel@tonic-gate 	    MSG_SCN_STAB_SIZE) == 0)) {
15300Sstevel@tonic-gate 		if ((ofl->ofl_flags & FLG_OF_STRIP) ||
15310Sstevel@tonic-gate 		    (strcmp((name + MSG_SCN_STAB_SIZE),
15324716Sab196087 		    MSG_ORIG(MSG_SCN_EXCL)) == 0))
15330Sstevel@tonic-gate 			return (1);
15340Sstevel@tonic-gate 
15350Sstevel@tonic-gate 		if (strcmp((name + MSG_SCN_STAB_SIZE),
15360Sstevel@tonic-gate 		    MSG_ORIG(MSG_SCN_INDEX)) == 0)
153711993SAli.Bahrami@Sun.COM 			is_stab_index = TRUE;
15380Sstevel@tonic-gate 	}
15390Sstevel@tonic-gate 
15400Sstevel@tonic-gate 	if ((ofl->ofl_flags & FLG_OF_STRIP) && ident) {
15410Sstevel@tonic-gate 		if ((strncmp(name, MSG_ORIG(MSG_SCN_DEBUG),
15420Sstevel@tonic-gate 		    MSG_SCN_DEBUG_SIZE) == 0) ||
15430Sstevel@tonic-gate 		    (strcmp(name, MSG_ORIG(MSG_SCN_LINE)) == 0))
15440Sstevel@tonic-gate 			return (1);
15450Sstevel@tonic-gate 	}
15460Sstevel@tonic-gate 
15470Sstevel@tonic-gate 	/*
15480Sstevel@tonic-gate 	 * Update the ident to reflect the type of section we've got.
15490Sstevel@tonic-gate 	 *
15500Sstevel@tonic-gate 	 * If there is any .plt or .got section to generate we'll be creating
15510Sstevel@tonic-gate 	 * our own version, so don't allow any input sections of these types to
15520Sstevel@tonic-gate 	 * be added to the output section list (why a relocatable object would
15530Sstevel@tonic-gate 	 * have a .plt or .got is a mystery, but stranger things have occurred).
15549085SAli.Bahrami@Sun.COM 	 *
15559085SAli.Bahrami@Sun.COM 	 * If there are any unwind sections, and this is a platform that uses
15569085SAli.Bahrami@Sun.COM 	 * SHT_PROGBITS for unwind sections, then set their ident to reflect
15579085SAli.Bahrami@Sun.COM 	 * that.
15580Sstevel@tonic-gate 	 */
15590Sstevel@tonic-gate 	if (ident) {
15609085SAli.Bahrami@Sun.COM 		if (shdr->sh_flags & SHF_TLS) {
15616206Sab196087 			ident = ld_targ.t_id.id_tls;
15629085SAli.Bahrami@Sun.COM 		} else if ((shdr->sh_flags & ~ALL_SHF_IGNORE) ==
15639085SAli.Bahrami@Sun.COM 		    (SHF_ALLOC | SHF_EXECINSTR)) {
15646206Sab196087 			ident = ld_targ.t_id.id_text;
15659085SAli.Bahrami@Sun.COM 		} else if (shdr->sh_flags & SHF_ALLOC) {
156611993SAli.Bahrami@Sun.COM 			if (process_progbits_alloc(name, ifl, shdr, ndx,
156711993SAli.Bahrami@Sun.COM 			    &ident, ofl, is_stab_index, &is_flags) == FALSE)
156811993SAli.Bahrami@Sun.COM 				return (S_ERROR);
156911993SAli.Bahrami@Sun.COM 		} else {
15706206Sab196087 			ident = ld_targ.t_id.id_note;
157111993SAli.Bahrami@Sun.COM 		}
15720Sstevel@tonic-gate 	}
15739085SAli.Bahrami@Sun.COM 
15749085SAli.Bahrami@Sun.COM 	r = process_section(name, ifl, shdr, scn, ndx, ident, ofl);
15759085SAli.Bahrami@Sun.COM 
15769085SAli.Bahrami@Sun.COM 	/*
15779085SAli.Bahrami@Sun.COM 	 * On success, process_section() creates an input section descriptor.
15789085SAli.Bahrami@Sun.COM 	 * Now that it exists, we can add any pending input section flags.
15799085SAli.Bahrami@Sun.COM 	 */
15809085SAli.Bahrami@Sun.COM 	if ((is_flags != 0) && (r == 1))
15819085SAli.Bahrami@Sun.COM 		ifl->ifl_isdesc[ndx]->is_flags |= is_flags;
15829085SAli.Bahrami@Sun.COM 
15839085SAli.Bahrami@Sun.COM 	return (r);
15840Sstevel@tonic-gate }
15850Sstevel@tonic-gate 
15860Sstevel@tonic-gate /*
15870Sstevel@tonic-gate  * Handles the SHT_SUNW_{DEBUG,DEBUGSTR) sections.
15880Sstevel@tonic-gate  */
15891618Srie static uintptr_t
process_debug(const char * name,Ifl_desc * ifl,Shdr * shdr,Elf_Scn * scn,Word ndx,int ident,Ofl_desc * ofl)15900Sstevel@tonic-gate process_debug(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
15910Sstevel@tonic-gate     Word ndx, int ident, Ofl_desc *ofl)
15920Sstevel@tonic-gate {
15930Sstevel@tonic-gate 	/*
15940Sstevel@tonic-gate 	 * Debug information is discarded when the 'ld -s' flag is invoked.
15950Sstevel@tonic-gate 	 */
15960Sstevel@tonic-gate 	if (ofl->ofl_flags & FLG_OF_STRIP) {
15970Sstevel@tonic-gate 		return (1);
15980Sstevel@tonic-gate 	}
15990Sstevel@tonic-gate 	return (process_progbits(name, ifl, shdr, scn, ndx, ident, ofl));
16000Sstevel@tonic-gate }
16010Sstevel@tonic-gate 
16020Sstevel@tonic-gate /*
16030Sstevel@tonic-gate  * Process a nobits section.
16040Sstevel@tonic-gate  */
16051618Srie static uintptr_t
process_nobits(const char * name,Ifl_desc * ifl,Shdr * shdr,Elf_Scn * scn,Word ndx,int ident,Ofl_desc * ofl)16060Sstevel@tonic-gate process_nobits(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
16070Sstevel@tonic-gate     Word ndx, int ident, Ofl_desc *ofl)
16080Sstevel@tonic-gate {
16090Sstevel@tonic-gate 	if (ident) {
16100Sstevel@tonic-gate 		if (shdr->sh_flags & SHF_TLS)
16116206Sab196087 			ident = ld_targ.t_id.id_tlsbss;
16126206Sab196087 #if	defined(_ELF64)
16136206Sab196087 		else if ((shdr->sh_flags & SHF_AMD64_LARGE) &&
16146206Sab196087 		    (ld_targ.t_m.m_mach == EM_AMD64))
16156206Sab196087 			ident = ld_targ.t_id.id_lbss;
1616574Sseizo #endif
16170Sstevel@tonic-gate 		else
16186206Sab196087 			ident = ld_targ.t_id.id_bss;
16190Sstevel@tonic-gate 	}
16200Sstevel@tonic-gate 	return (process_section(name, ifl, shdr, scn, ndx, ident, ofl));
16210Sstevel@tonic-gate }
16220Sstevel@tonic-gate 
16230Sstevel@tonic-gate /*
16240Sstevel@tonic-gate  * Process a SHT_*_ARRAY section.
16250Sstevel@tonic-gate  */
16261618Srie static uintptr_t
process_array(const char * name,Ifl_desc * ifl,Shdr * shdr,Elf_Scn * scn,Word ndx,int ident,Ofl_desc * ofl)16270Sstevel@tonic-gate process_array(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
16280Sstevel@tonic-gate     Word ndx, int ident, Ofl_desc *ofl)
16290Sstevel@tonic-gate {
16307463SRod.Evans@Sun.COM 	uintptr_t	error;
16310Sstevel@tonic-gate 
16320Sstevel@tonic-gate 	if (ident)
16336206Sab196087 		ident = ld_targ.t_id.id_array;
16340Sstevel@tonic-gate 
16357463SRod.Evans@Sun.COM 	error = process_section(name, ifl, shdr, scn, ndx, ident, ofl);
16367463SRod.Evans@Sun.COM 	if ((error == 0) || (error == S_ERROR))
16377463SRod.Evans@Sun.COM 		return (error);
16387463SRod.Evans@Sun.COM 
16397463SRod.Evans@Sun.COM 	return (1);
16407463SRod.Evans@Sun.COM }
16410Sstevel@tonic-gate 
16427463SRod.Evans@Sun.COM static uintptr_t
16437463SRod.Evans@Sun.COM /* ARGSUSED1 */
array_process(Is_desc * isc,Ifl_desc * ifl,Ofl_desc * ofl)16447463SRod.Evans@Sun.COM array_process(Is_desc *isc, Ifl_desc *ifl, Ofl_desc *ofl)
16457463SRod.Evans@Sun.COM {
16467463SRod.Evans@Sun.COM 	Os_desc	*osp;
16477463SRod.Evans@Sun.COM 	Shdr	*shdr;
16487463SRod.Evans@Sun.COM 
16497463SRod.Evans@Sun.COM 	if ((isc == NULL) || ((osp = isc->is_osdesc) == NULL))
16500Sstevel@tonic-gate 		return (0);
16510Sstevel@tonic-gate 
16527463SRod.Evans@Sun.COM 	shdr = isc->is_shdr;
16537463SRod.Evans@Sun.COM 
16540Sstevel@tonic-gate 	if ((shdr->sh_type == SHT_FINI_ARRAY) &&
16557463SRod.Evans@Sun.COM 	    (ofl->ofl_osfiniarray == NULL))
16560Sstevel@tonic-gate 		ofl->ofl_osfiniarray = osp;
16570Sstevel@tonic-gate 	else if ((shdr->sh_type == SHT_INIT_ARRAY) &&
16587463SRod.Evans@Sun.COM 	    (ofl->ofl_osinitarray == NULL))
16590Sstevel@tonic-gate 		ofl->ofl_osinitarray = osp;
16600Sstevel@tonic-gate 	else if ((shdr->sh_type == SHT_PREINIT_ARRAY) &&
16617463SRod.Evans@Sun.COM 	    (ofl->ofl_ospreinitarray == NULL))
16620Sstevel@tonic-gate 		ofl->ofl_ospreinitarray = osp;
16630Sstevel@tonic-gate 
16640Sstevel@tonic-gate 	return (1);
16650Sstevel@tonic-gate }
16660Sstevel@tonic-gate 
16670Sstevel@tonic-gate /*
16680Sstevel@tonic-gate  * Process a SHT_SYMTAB_SHNDX section.
16690Sstevel@tonic-gate  */
16701618Srie static uintptr_t
process_sym_shndx(const char * name,Ifl_desc * ifl,Shdr * shdr,Elf_Scn * scn,Word ndx,int ident,Ofl_desc * ofl)16710Sstevel@tonic-gate process_sym_shndx(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
16720Sstevel@tonic-gate     Word ndx, int ident, Ofl_desc *ofl)
16730Sstevel@tonic-gate {
16740Sstevel@tonic-gate 	if (process_input(name, ifl, shdr, scn, ndx, ident, ofl) == S_ERROR)
16750Sstevel@tonic-gate 		return (S_ERROR);
16760Sstevel@tonic-gate 
16770Sstevel@tonic-gate 	/*
16780Sstevel@tonic-gate 	 * Have we already seen the related SYMTAB - if so verify it now.
16790Sstevel@tonic-gate 	 */
16800Sstevel@tonic-gate 	if (shdr->sh_link < ndx) {
16810Sstevel@tonic-gate 		Is_desc	*isp = ifl->ifl_isdesc[shdr->sh_link];
16820Sstevel@tonic-gate 
168310792SRod.Evans@Sun.COM 		if ((isp == NULL) || ((isp->is_shdr->sh_type != SHT_SYMTAB) &&
16840Sstevel@tonic-gate 		    (isp->is_shdr->sh_type != SHT_DYNSYM))) {
1685*13074SAli.Bahrami@Oracle.COM 			ld_eprintf(ofl, ERR_FATAL,
16869878SAli.Bahrami@Sun.COM 			    MSG_INTL(MSG_FIL_INVSHLINK), ifl->ifl_name,
16879878SAli.Bahrami@Sun.COM 			    EC_WORD(ndx), name, EC_XWORD(shdr->sh_link));
16880Sstevel@tonic-gate 			return (S_ERROR);
16890Sstevel@tonic-gate 		}
16900Sstevel@tonic-gate 		isp->is_symshndx = ifl->ifl_isdesc[ndx];
16910Sstevel@tonic-gate 	}
16920Sstevel@tonic-gate 	return (1);
16930Sstevel@tonic-gate }
16940Sstevel@tonic-gate 
16950Sstevel@tonic-gate /*
16960Sstevel@tonic-gate  * Final processing for SHT_SYMTAB_SHNDX section.
16970Sstevel@tonic-gate  */
16981618Srie static uintptr_t
16990Sstevel@tonic-gate /* ARGSUSED2 */
sym_shndx_process(Is_desc * isc,Ifl_desc * ifl,Ofl_desc * ofl)17000Sstevel@tonic-gate sym_shndx_process(Is_desc *isc, Ifl_desc *ifl, Ofl_desc *ofl)
17010Sstevel@tonic-gate {
17020Sstevel@tonic-gate 	if (isc->is_shdr->sh_link > isc->is_scnndx) {
17030Sstevel@tonic-gate 		Is_desc	*isp = ifl->ifl_isdesc[isc->is_shdr->sh_link];
17040Sstevel@tonic-gate 
170510792SRod.Evans@Sun.COM 		if ((isp == NULL) || ((isp->is_shdr->sh_type != SHT_SYMTAB) &&
17060Sstevel@tonic-gate 		    (isp->is_shdr->sh_type != SHT_DYNSYM))) {
1707*13074SAli.Bahrami@Oracle.COM 			ld_eprintf(ofl, ERR_FATAL,
17081618Srie 			    MSG_INTL(MSG_FIL_INVSHLINK), isc->is_file->ifl_name,
17099878SAli.Bahrami@Sun.COM 			    EC_WORD(isc->is_scnndx), isc->is_name,
17109878SAli.Bahrami@Sun.COM 			    EC_XWORD(isc->is_shdr->sh_link));
17110Sstevel@tonic-gate 			return (S_ERROR);
17120Sstevel@tonic-gate 		}
17130Sstevel@tonic-gate 		isp->is_symshndx = isc;
17140Sstevel@tonic-gate 	}
17150Sstevel@tonic-gate 	return (1);
17160Sstevel@tonic-gate }
17170Sstevel@tonic-gate 
17180Sstevel@tonic-gate /*
17190Sstevel@tonic-gate  * Process .dynamic section from a relocatable object.
17200Sstevel@tonic-gate  *
17210Sstevel@tonic-gate  * Note: That the .dynamic section is only considered interesting when
17220Sstevel@tonic-gate  *	 dlopen()ing a relocatable object (thus FLG_OF1_RELDYN can only get
17230Sstevel@tonic-gate  *	 set when libld is called from ld.so.1).
17240Sstevel@tonic-gate  */
17250Sstevel@tonic-gate /*ARGSUSED*/
17261618Srie static uintptr_t
process_rel_dynamic(const char * name,Ifl_desc * ifl,Shdr * shdr,Elf_Scn * scn,Word ndx,int ident,Ofl_desc * ofl)17270Sstevel@tonic-gate process_rel_dynamic(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
17280Sstevel@tonic-gate     Word ndx, int ident, Ofl_desc *ofl)
17290Sstevel@tonic-gate {
17300Sstevel@tonic-gate 	Dyn		*dyn;
17310Sstevel@tonic-gate 	Elf_Scn		*strscn;
17320Sstevel@tonic-gate 	Elf_Data	*dp;
17330Sstevel@tonic-gate 	char		*str;
17340Sstevel@tonic-gate 
17350Sstevel@tonic-gate 	/*
17360Sstevel@tonic-gate 	 * Process .dynamic sections from relocatable objects ?
17370Sstevel@tonic-gate 	 */
17380Sstevel@tonic-gate 	if ((ofl->ofl_flags1 & FLG_OF1_RELDYN) == 0)
17390Sstevel@tonic-gate 		return (1);
17400Sstevel@tonic-gate 
17410Sstevel@tonic-gate 	/*
17420Sstevel@tonic-gate 	 * Find the string section associated with the .dynamic section.
17430Sstevel@tonic-gate 	 */
17440Sstevel@tonic-gate 	if ((strscn = elf_getscn(ifl->ifl_elf, shdr->sh_link)) == NULL) {
1745*13074SAli.Bahrami@Oracle.COM 		ld_eprintf(ofl, ERR_ELF, MSG_INTL(MSG_ELF_GETSCN),
17461618Srie 		    ifl->ifl_name);
17470Sstevel@tonic-gate 		return (0);
17480Sstevel@tonic-gate 	}
17490Sstevel@tonic-gate 	dp = elf_getdata(strscn, NULL);
17500Sstevel@tonic-gate 	str = (char *)dp->d_buf;
17510Sstevel@tonic-gate 
17520Sstevel@tonic-gate 	/*
17530Sstevel@tonic-gate 	 * And get the .dynamic data
17540Sstevel@tonic-gate 	 */
17550Sstevel@tonic-gate 	dp = elf_getdata(scn, NULL);
17560Sstevel@tonic-gate 
17570Sstevel@tonic-gate 	for (dyn = (Dyn *)dp->d_buf; dyn->d_tag != DT_NULL; dyn++) {
17581109Srie 		Ifl_desc	*difl;
17590Sstevel@tonic-gate 
17600Sstevel@tonic-gate 		switch (dyn->d_tag) {
17610Sstevel@tonic-gate 		case DT_NEEDED:
17620Sstevel@tonic-gate 		case DT_USED:
17639131SRod.Evans@Sun.COM 			if (((difl = libld_calloc(1,
17649131SRod.Evans@Sun.COM 			    sizeof (Ifl_desc))) == NULL) ||
17659131SRod.Evans@Sun.COM 			    (aplist_append(&ofl->ofl_sos, difl,
17669131SRod.Evans@Sun.COM 			    AL_CNT_OFL_LIBS) == NULL))
17670Sstevel@tonic-gate 				return (S_ERROR);
17681109Srie 
17691109Srie 			difl->ifl_name = MSG_ORIG(MSG_STR_DYNAMIC);
17701109Srie 			difl->ifl_soname = str + (size_t)dyn->d_un.d_val;
17711109Srie 			difl->ifl_flags = FLG_IF_NEEDSTR;
17720Sstevel@tonic-gate 			break;
17730Sstevel@tonic-gate 		case DT_RPATH:
17740Sstevel@tonic-gate 		case DT_RUNPATH:
17750Sstevel@tonic-gate 			if ((ofl->ofl_rpath = add_string(ofl->ofl_rpath,
17760Sstevel@tonic-gate 			    (str + (size_t)dyn->d_un.d_val))) ==
17770Sstevel@tonic-gate 			    (const char *)S_ERROR)
17780Sstevel@tonic-gate 				return (S_ERROR);
17790Sstevel@tonic-gate 			break;
17804716Sab196087 		case DT_VERSYM:
17814716Sab196087 			/*
17824716Sab196087 			 * The Solaris ld does not put DT_VERSYM in the
17834716Sab196087 			 * dynamic section. If the object has DT_VERSYM,
17844716Sab196087 			 * then it must have been produced by the GNU ld,
17854716Sab196087 			 * and is using the GNU style of versioning.
17864716Sab196087 			 */
17874716Sab196087 			ifl->ifl_flags |= FLG_IF_GNUVER;
17884716Sab196087 			break;
17890Sstevel@tonic-gate 		}
17900Sstevel@tonic-gate 	}
17910Sstevel@tonic-gate 	return (1);
17920Sstevel@tonic-gate }
17930Sstevel@tonic-gate 
17940Sstevel@tonic-gate /*
17950Sstevel@tonic-gate  * Expand implicit references.  Dependencies can be specified in terms of the
179611827SRod.Evans@Sun.COM  * $ORIGIN, $MACHINE, $PLATFORM, $OSREL and $OSNAME tokens, either from their
179711827SRod.Evans@Sun.COM  * needed name, or via a runpath.  In addition runpaths may also specify the
179811827SRod.Evans@Sun.COM  * $ISALIST token.
17990Sstevel@tonic-gate  *
18001109Srie  * Probably the most common reference to explicit dependencies (via -L) will be
18010Sstevel@tonic-gate  * sufficient to find any associated implicit dependencies, but just in case we
18020Sstevel@tonic-gate  * expand any occurrence of these known tokens here.
18030Sstevel@tonic-gate  *
18040Sstevel@tonic-gate  * Note, if any errors occur we simply return the original name.
18050Sstevel@tonic-gate  *
18060Sstevel@tonic-gate  * This code is remarkably similar to expand() in rtld/common/paths.c.
18070Sstevel@tonic-gate  */
180811827SRod.Evans@Sun.COM static char		*machine = NULL;
180911827SRod.Evans@Sun.COM static size_t		machine_sz = 0;
181010792SRod.Evans@Sun.COM static char		*platform = NULL;
18110Sstevel@tonic-gate static size_t		platform_sz = 0;
181210792SRod.Evans@Sun.COM static Isa_desc		*isa = NULL;
181310792SRod.Evans@Sun.COM static Uts_desc		*uts = NULL;
18140Sstevel@tonic-gate 
18150Sstevel@tonic-gate static char *
expand(const char * parent,const char * name,char ** next)18160Sstevel@tonic-gate expand(const char *parent, const char *name, char **next)
18170Sstevel@tonic-gate {
18180Sstevel@tonic-gate 	char		_name[PATH_MAX], *nptr, *_next;
18190Sstevel@tonic-gate 	const char	*optr;
18200Sstevel@tonic-gate 	size_t		nrem = PATH_MAX - 1;
18210Sstevel@tonic-gate 	int		expanded = 0, _expanded, isaflag = 0;
18220Sstevel@tonic-gate 
18230Sstevel@tonic-gate 	optr = name;
18240Sstevel@tonic-gate 	nptr = _name;
18250Sstevel@tonic-gate 
18260Sstevel@tonic-gate 	while (*optr) {
18270Sstevel@tonic-gate 		if (nrem == 0)
18280Sstevel@tonic-gate 			return ((char *)name);
18290Sstevel@tonic-gate 
18300Sstevel@tonic-gate 		if (*optr != '$') {
18310Sstevel@tonic-gate 			*nptr++ = *optr++, nrem--;
18320Sstevel@tonic-gate 			continue;
18330Sstevel@tonic-gate 		}
18340Sstevel@tonic-gate 
18350Sstevel@tonic-gate 		_expanded = 0;
18360Sstevel@tonic-gate 
18370Sstevel@tonic-gate 		if (strncmp(optr, MSG_ORIG(MSG_STR_ORIGIN),
18380Sstevel@tonic-gate 		    MSG_STR_ORIGIN_SIZE) == 0) {
18390Sstevel@tonic-gate 			char *eptr;
18400Sstevel@tonic-gate 
18410Sstevel@tonic-gate 			/*
18420Sstevel@tonic-gate 			 * For $ORIGIN, expansion is really just a concatenation
18430Sstevel@tonic-gate 			 * of the parents directory name.  For example, an
18441109Srie 			 * explicit dependency foo/bar/lib1.so with a dependency
18450Sstevel@tonic-gate 			 * on $ORIGIN/lib2.so would be expanded to
18460Sstevel@tonic-gate 			 * foo/bar/lib2.so.
18470Sstevel@tonic-gate 			 */
184810792SRod.Evans@Sun.COM 			if ((eptr = strrchr(parent, '/')) == NULL) {
18490Sstevel@tonic-gate 				*nptr++ = '.';
18500Sstevel@tonic-gate 				nrem--;
18510Sstevel@tonic-gate 			} else {
18520Sstevel@tonic-gate 				size_t	len = eptr - parent;
18530Sstevel@tonic-gate 
18540Sstevel@tonic-gate 				if (len >= nrem)
18550Sstevel@tonic-gate 					return ((char *)name);
18560Sstevel@tonic-gate 
18570Sstevel@tonic-gate 				(void) strncpy(nptr, parent, len);
18580Sstevel@tonic-gate 				nptr = nptr + len;
18590Sstevel@tonic-gate 				nrem -= len;
18600Sstevel@tonic-gate 			}
18610Sstevel@tonic-gate 			optr += MSG_STR_ORIGIN_SIZE;
18620Sstevel@tonic-gate 			expanded = _expanded = 1;
18630Sstevel@tonic-gate 
186411827SRod.Evans@Sun.COM 		} else if (strncmp(optr, MSG_ORIG(MSG_STR_MACHINE),
186511827SRod.Evans@Sun.COM 		    MSG_STR_MACHINE_SIZE) == 0) {
186611827SRod.Evans@Sun.COM 			/*
186711827SRod.Evans@Sun.COM 			 * Establish the machine from sysconf - like uname -i.
186811827SRod.Evans@Sun.COM 			 */
186911827SRod.Evans@Sun.COM 			if ((machine == NULL) && (machine_sz == 0)) {
187011827SRod.Evans@Sun.COM 				char	info[SYS_NMLN];
187111827SRod.Evans@Sun.COM 				long	size;
187211827SRod.Evans@Sun.COM 
187311827SRod.Evans@Sun.COM 				size = sysinfo(SI_MACHINE, info, SYS_NMLN);
187411827SRod.Evans@Sun.COM 				if ((size != -1) &&
187511827SRod.Evans@Sun.COM 				    (machine = libld_malloc((size_t)size))) {
187611827SRod.Evans@Sun.COM 					(void) strcpy(machine, info);
187711827SRod.Evans@Sun.COM 					machine_sz = (size_t)size - 1;
187811827SRod.Evans@Sun.COM 				} else
187911827SRod.Evans@Sun.COM 					machine_sz = 1;
188011827SRod.Evans@Sun.COM 			}
188111827SRod.Evans@Sun.COM 			if (machine) {
188211827SRod.Evans@Sun.COM 				if (machine_sz >= nrem)
188311827SRod.Evans@Sun.COM 					return ((char *)name);
188411827SRod.Evans@Sun.COM 
188511827SRod.Evans@Sun.COM 				(void) strncpy(nptr, machine, machine_sz);
188611827SRod.Evans@Sun.COM 				nptr = nptr + machine_sz;
188711827SRod.Evans@Sun.COM 				nrem -= machine_sz;
188811827SRod.Evans@Sun.COM 
188911827SRod.Evans@Sun.COM 				optr += MSG_STR_MACHINE_SIZE;
189011827SRod.Evans@Sun.COM 				expanded = _expanded = 1;
189111827SRod.Evans@Sun.COM 			}
189211827SRod.Evans@Sun.COM 
18930Sstevel@tonic-gate 		} else if (strncmp(optr, MSG_ORIG(MSG_STR_PLATFORM),
18940Sstevel@tonic-gate 		    MSG_STR_PLATFORM_SIZE) == 0) {
18950Sstevel@tonic-gate 			/*
18960Sstevel@tonic-gate 			 * Establish the platform from sysconf - like uname -i.
18970Sstevel@tonic-gate 			 */
189810792SRod.Evans@Sun.COM 			if ((platform == NULL) && (platform_sz == 0)) {
18990Sstevel@tonic-gate 				char	info[SYS_NMLN];
19000Sstevel@tonic-gate 				long	size;
19010Sstevel@tonic-gate 
19020Sstevel@tonic-gate 				size = sysinfo(SI_PLATFORM, info, SYS_NMLN);
19030Sstevel@tonic-gate 				if ((size != -1) &&
19040Sstevel@tonic-gate 				    (platform = libld_malloc((size_t)size))) {
19050Sstevel@tonic-gate 					(void) strcpy(platform, info);
19060Sstevel@tonic-gate 					platform_sz = (size_t)size - 1;
19070Sstevel@tonic-gate 				} else
19080Sstevel@tonic-gate 					platform_sz = 1;
19090Sstevel@tonic-gate 			}
191010792SRod.Evans@Sun.COM 			if (platform) {
19110Sstevel@tonic-gate 				if (platform_sz >= nrem)
19120Sstevel@tonic-gate 					return ((char *)name);
19130Sstevel@tonic-gate 
19140Sstevel@tonic-gate 				(void) strncpy(nptr, platform, platform_sz);
19150Sstevel@tonic-gate 				nptr = nptr + platform_sz;
19160Sstevel@tonic-gate 				nrem -= platform_sz;
19170Sstevel@tonic-gate 
19180Sstevel@tonic-gate 				optr += MSG_STR_PLATFORM_SIZE;
19190Sstevel@tonic-gate 				expanded = _expanded = 1;
19200Sstevel@tonic-gate 			}
19210Sstevel@tonic-gate 
19220Sstevel@tonic-gate 		} else if (strncmp(optr, MSG_ORIG(MSG_STR_OSNAME),
19230Sstevel@tonic-gate 		    MSG_STR_OSNAME_SIZE) == 0) {
19240Sstevel@tonic-gate 			/*
19250Sstevel@tonic-gate 			 * Establish the os name - like uname -s.
19260Sstevel@tonic-gate 			 */
192710792SRod.Evans@Sun.COM 			if (uts == NULL)
19280Sstevel@tonic-gate 				uts = conv_uts();
19290Sstevel@tonic-gate 
19300Sstevel@tonic-gate 			if (uts && uts->uts_osnamesz) {
19310Sstevel@tonic-gate 				if (uts->uts_osnamesz >= nrem)
19320Sstevel@tonic-gate 					return ((char *)name);
19330Sstevel@tonic-gate 
19340Sstevel@tonic-gate 				(void) strncpy(nptr, uts->uts_osname,
19350Sstevel@tonic-gate 				    uts->uts_osnamesz);
19360Sstevel@tonic-gate 				nptr = nptr + uts->uts_osnamesz;
19370Sstevel@tonic-gate 				nrem -= uts->uts_osnamesz;
19380Sstevel@tonic-gate 
19390Sstevel@tonic-gate 				optr += MSG_STR_OSNAME_SIZE;
19400Sstevel@tonic-gate 				expanded = _expanded = 1;
19410Sstevel@tonic-gate 			}
19420Sstevel@tonic-gate 
19430Sstevel@tonic-gate 		} else if (strncmp(optr, MSG_ORIG(MSG_STR_OSREL),
19440Sstevel@tonic-gate 		    MSG_STR_OSREL_SIZE) == 0) {
19450Sstevel@tonic-gate 			/*
19460Sstevel@tonic-gate 			 * Establish the os release - like uname -r.
19470Sstevel@tonic-gate 			 */
194810792SRod.Evans@Sun.COM 			if (uts == NULL)
19490Sstevel@tonic-gate 				uts = conv_uts();
19500Sstevel@tonic-gate 
19510Sstevel@tonic-gate 			if (uts && uts->uts_osrelsz) {
19520Sstevel@tonic-gate 				if (uts->uts_osrelsz >= nrem)
19530Sstevel@tonic-gate 					return ((char *)name);
19540Sstevel@tonic-gate 
19550Sstevel@tonic-gate 				(void) strncpy(nptr, uts->uts_osrel,
19560Sstevel@tonic-gate 				    uts->uts_osrelsz);
19570Sstevel@tonic-gate 				nptr = nptr + uts->uts_osrelsz;
19580Sstevel@tonic-gate 				nrem -= uts->uts_osrelsz;
19590Sstevel@tonic-gate 
19600Sstevel@tonic-gate 				optr += MSG_STR_OSREL_SIZE;
19610Sstevel@tonic-gate 				expanded = _expanded = 1;
19620Sstevel@tonic-gate 			}
19630Sstevel@tonic-gate 
19640Sstevel@tonic-gate 		} else if ((strncmp(optr, MSG_ORIG(MSG_STR_ISALIST),
19650Sstevel@tonic-gate 		    MSG_STR_ISALIST_SIZE) == 0) && next && (isaflag++ == 0)) {
19660Sstevel@tonic-gate 			/*
19670Sstevel@tonic-gate 			 * Establish instruction sets from sysconf.  Note that
19680Sstevel@tonic-gate 			 * this is only meaningful from runpaths.
19690Sstevel@tonic-gate 			 */
197010792SRod.Evans@Sun.COM 			if (isa == NULL)
19710Sstevel@tonic-gate 				isa = conv_isalist();
19720Sstevel@tonic-gate 
19730Sstevel@tonic-gate 			if (isa && isa->isa_listsz &&
19740Sstevel@tonic-gate 			    (nrem > isa->isa_opt->isa_namesz)) {
19750Sstevel@tonic-gate 				size_t		mlen, tlen, hlen = optr - name;
19760Sstevel@tonic-gate 				size_t		no;
19770Sstevel@tonic-gate 				char		*lptr;
19780Sstevel@tonic-gate 				Isa_opt		*opt = isa->isa_opt;
19790Sstevel@tonic-gate 
19800Sstevel@tonic-gate 				(void) strncpy(nptr, opt->isa_name,
19810Sstevel@tonic-gate 				    opt->isa_namesz);
19820Sstevel@tonic-gate 				nptr = nptr + opt->isa_namesz;
19830Sstevel@tonic-gate 				nrem -= opt->isa_namesz;
19840Sstevel@tonic-gate 
19850Sstevel@tonic-gate 				optr += MSG_STR_ISALIST_SIZE;
19860Sstevel@tonic-gate 				expanded = _expanded = 1;
19870Sstevel@tonic-gate 
19880Sstevel@tonic-gate 				tlen = strlen(optr);
19890Sstevel@tonic-gate 
19900Sstevel@tonic-gate 				/*
19910Sstevel@tonic-gate 				 * As ISALIST expands to a number of elements,
19920Sstevel@tonic-gate 				 * establish a new list to return to the caller.
19930Sstevel@tonic-gate 				 * This will contain the present path being
19940Sstevel@tonic-gate 				 * processed redefined for each isalist option,
19950Sstevel@tonic-gate 				 * plus the original remaining list entries.
19960Sstevel@tonic-gate 				 */
19970Sstevel@tonic-gate 				mlen = ((hlen + tlen) * (isa->isa_optno - 1)) +
19980Sstevel@tonic-gate 				    isa->isa_listsz - opt->isa_namesz;
19990Sstevel@tonic-gate 				if (*next)
20004716Sab196087 					mlen += strlen(*next);
200110792SRod.Evans@Sun.COM 				if ((_next = lptr = libld_malloc(mlen)) == NULL)
20020Sstevel@tonic-gate 					return (0);
20030Sstevel@tonic-gate 
20040Sstevel@tonic-gate 				for (no = 1, opt++; no < isa->isa_optno;
20050Sstevel@tonic-gate 				    no++, opt++) {
20060Sstevel@tonic-gate 					(void) strncpy(lptr, name, hlen);
20070Sstevel@tonic-gate 					lptr = lptr + hlen;
20080Sstevel@tonic-gate 					(void) strncpy(lptr, opt->isa_name,
20090Sstevel@tonic-gate 					    opt->isa_namesz);
20100Sstevel@tonic-gate 					lptr = lptr + opt->isa_namesz;
20110Sstevel@tonic-gate 					(void) strncpy(lptr, optr, tlen);
20120Sstevel@tonic-gate 					lptr = lptr + tlen;
20130Sstevel@tonic-gate 					*lptr++ = ':';
20140Sstevel@tonic-gate 				}
20150Sstevel@tonic-gate 				if (*next)
20160Sstevel@tonic-gate 					(void) strcpy(lptr, *next);
20170Sstevel@tonic-gate 				else
20180Sstevel@tonic-gate 					*--lptr = '\0';
20190Sstevel@tonic-gate 			}
20200Sstevel@tonic-gate 		}
20210Sstevel@tonic-gate 
20220Sstevel@tonic-gate 		/*
20230Sstevel@tonic-gate 		 * If no expansion occurred skip the $ and continue.
20240Sstevel@tonic-gate 		 */
20250Sstevel@tonic-gate 		if (_expanded == 0)
20260Sstevel@tonic-gate 			*nptr++ = *optr++, nrem--;
20270Sstevel@tonic-gate 	}
20280Sstevel@tonic-gate 
20290Sstevel@tonic-gate 	/*
20300Sstevel@tonic-gate 	 * If any ISALIST processing has occurred not only do we return the
20310Sstevel@tonic-gate 	 * expanded node we're presently working on, but we must also update the
20320Sstevel@tonic-gate 	 * remaining list so that it is effectively prepended with this node
20330Sstevel@tonic-gate 	 * expanded to all remaining isalist options.  Note that we can only
20340Sstevel@tonic-gate 	 * handle one ISALIST per node.  For more than one ISALIST to be
20350Sstevel@tonic-gate 	 * processed we'd need a better algorithm than above to replace the
20360Sstevel@tonic-gate 	 * newly generated list.  Whether we want to encourage the number of
20370Sstevel@tonic-gate 	 * pathname permutations this would provide is another question. So, for
20380Sstevel@tonic-gate 	 * now if more than one ISALIST is encountered we return the original
20390Sstevel@tonic-gate 	 * node untouched.
20400Sstevel@tonic-gate 	 */
20410Sstevel@tonic-gate 	if (isaflag) {
20420Sstevel@tonic-gate 		if (isaflag == 1)
20430Sstevel@tonic-gate 			*next = _next;
20440Sstevel@tonic-gate 		else
20450Sstevel@tonic-gate 			return ((char *)name);
20460Sstevel@tonic-gate 	}
20470Sstevel@tonic-gate 
20480Sstevel@tonic-gate 	*nptr = '\0';
20490Sstevel@tonic-gate 
20500Sstevel@tonic-gate 	if (expanded) {
205110792SRod.Evans@Sun.COM 		if ((nptr = libld_malloc(strlen(_name) + 1)) == NULL)
20520Sstevel@tonic-gate 			return ((char *)name);
20530Sstevel@tonic-gate 		(void) strcpy(nptr, _name);
20540Sstevel@tonic-gate 		return (nptr);
20550Sstevel@tonic-gate 	}
20560Sstevel@tonic-gate 	return ((char *)name);
20570Sstevel@tonic-gate }
20580Sstevel@tonic-gate 
20590Sstevel@tonic-gate /*
20604716Sab196087  * The Solaris ld does not put DT_VERSYM in the dynamic section, but the
20614716Sab196087  * GNU ld does, and it is used by the runtime linker to implement their
20624716Sab196087  * versioning scheme. Use this fact to determine if the sharable object
20634716Sab196087  * was produced by the GNU ld rather than the Solaris one, and to set
20644716Sab196087  * FLG_IF_GNUVER if so. This needs to be done before the symbols are
20654716Sab196087  * processed, since the answer determines whether we interpret the
20664716Sab196087  * symbols versions according to Solaris or GNU rules.
20674716Sab196087  */
20684716Sab196087 /*ARGSUSED*/
20694716Sab196087 static uintptr_t
process_dynamic_isgnu(const char * name,Ifl_desc * ifl,Shdr * shdr,Elf_Scn * scn,Word ndx,int ident,Ofl_desc * ofl)20704716Sab196087 process_dynamic_isgnu(const char *name, Ifl_desc *ifl, Shdr *shdr,
20714716Sab196087     Elf_Scn *scn, Word ndx, int ident, Ofl_desc *ofl)
20724716Sab196087 {
20734716Sab196087 	Dyn		*dyn;
20744716Sab196087 	Elf_Data	*dp;
20757463SRod.Evans@Sun.COM 	uintptr_t	error;
20764716Sab196087 
20777463SRod.Evans@Sun.COM 	error = process_section(name, ifl, shdr, scn, ndx, ident, ofl);
20787463SRod.Evans@Sun.COM 	if ((error == 0) || (error == S_ERROR))
20797463SRod.Evans@Sun.COM 		return (error);
20804716Sab196087 
20814716Sab196087 	/* Get the .dynamic data */
20824716Sab196087 	dp = elf_getdata(scn, NULL);
20834716Sab196087 
20844716Sab196087 	for (dyn = (Dyn *)dp->d_buf; dyn->d_tag != DT_NULL; dyn++) {
20854716Sab196087 		if (dyn->d_tag == DT_VERSYM) {
20864716Sab196087 			ifl->ifl_flags |= FLG_IF_GNUVER;
20874716Sab196087 			break;
20884716Sab196087 		}
20894716Sab196087 	}
20904716Sab196087 	return (1);
20914716Sab196087 }
20924716Sab196087 
20934716Sab196087 /*
20940Sstevel@tonic-gate  * Process a dynamic section.  If we are processing an explicit shared object
20950Sstevel@tonic-gate  * then we need to determine if it has a recorded SONAME, if so, this name will
20960Sstevel@tonic-gate  * be recorded in the output file being generated as the NEEDED entry rather
20970Sstevel@tonic-gate  * than the shared objects filename itself.
20980Sstevel@tonic-gate  * If the mode of the link-edit indicates that no undefined symbols should
20990Sstevel@tonic-gate  * remain, then we also need to build up a list of any additional shared object
21000Sstevel@tonic-gate  * dependencies this object may have.  In this case save any NEEDED entries
21010Sstevel@tonic-gate  * together with any associated run-path specifications.  This information is
21020Sstevel@tonic-gate  * recorded on the `ofl_soneed' list and will be analyzed after all explicit
21030Sstevel@tonic-gate  * file processing has been completed (refer finish_libs()).
21040Sstevel@tonic-gate  */
21051618Srie static uintptr_t
process_dynamic(Is_desc * isc,Ifl_desc * ifl,Ofl_desc * ofl)21060Sstevel@tonic-gate process_dynamic(Is_desc *isc, Ifl_desc *ifl, Ofl_desc *ofl)
21070Sstevel@tonic-gate {
21080Sstevel@tonic-gate 	Dyn		*data, *dyn;
21090Sstevel@tonic-gate 	char		*str, *rpath = NULL;
21100Sstevel@tonic-gate 	const char	*soname, *needed;
2111*13074SAli.Bahrami@Oracle.COM 	Boolean		no_undef;
21120Sstevel@tonic-gate 
21130Sstevel@tonic-gate 	data = (Dyn *)isc->is_indata->d_buf;
21140Sstevel@tonic-gate 	str = (char *)ifl->ifl_isdesc[isc->is_shdr->sh_link]->is_indata->d_buf;
21150Sstevel@tonic-gate 
2116*13074SAli.Bahrami@Oracle.COM 	/* Determine if we need to examine the runpaths and NEEDED entries */
2117*13074SAli.Bahrami@Oracle.COM 	no_undef = (ofl->ofl_flags & (FLG_OF_NOUNDEF | FLG_OF_SYMBOLIC)) ||
2118*13074SAli.Bahrami@Oracle.COM 	    OFL_GUIDANCE(ofl, FLG_OFG_NO_DEFS);
2119*13074SAli.Bahrami@Oracle.COM 
21200Sstevel@tonic-gate 	/*
21210Sstevel@tonic-gate 	 * First loop through the dynamic section looking for a run path.
21220Sstevel@tonic-gate 	 */
2123*13074SAli.Bahrami@Oracle.COM 	if (no_undef) {
21240Sstevel@tonic-gate 		for (dyn = data; dyn->d_tag != DT_NULL; dyn++) {
21250Sstevel@tonic-gate 			if ((dyn->d_tag != DT_RPATH) &&
21260Sstevel@tonic-gate 			    (dyn->d_tag != DT_RUNPATH))
21270Sstevel@tonic-gate 				continue;
21280Sstevel@tonic-gate 			if ((rpath = str + (size_t)dyn->d_un.d_val) == NULL)
21290Sstevel@tonic-gate 				continue;
21300Sstevel@tonic-gate 			break;
21310Sstevel@tonic-gate 		}
21320Sstevel@tonic-gate 	}
21330Sstevel@tonic-gate 
21340Sstevel@tonic-gate 	/*
21350Sstevel@tonic-gate 	 * Now look for any needed dependencies (which may use the rpath)
21360Sstevel@tonic-gate 	 * or a new SONAME.
21370Sstevel@tonic-gate 	 */
21380Sstevel@tonic-gate 	for (dyn = data; dyn->d_tag != DT_NULL; dyn++) {
21390Sstevel@tonic-gate 		if (dyn->d_tag == DT_SONAME) {
21400Sstevel@tonic-gate 			if ((soname = str + (size_t)dyn->d_un.d_val) == NULL)
21410Sstevel@tonic-gate 				continue;
21420Sstevel@tonic-gate 
21430Sstevel@tonic-gate 			/*
21440Sstevel@tonic-gate 			 * Update the input file structure with this new name.
21450Sstevel@tonic-gate 			 */
21460Sstevel@tonic-gate 			ifl->ifl_soname = soname;
21470Sstevel@tonic-gate 
21480Sstevel@tonic-gate 		} else if ((dyn->d_tag == DT_NEEDED) ||
21490Sstevel@tonic-gate 		    (dyn->d_tag == DT_USED)) {
21509131SRod.Evans@Sun.COM 			Sdf_desc	*sdf;
21519131SRod.Evans@Sun.COM 
2152*13074SAli.Bahrami@Oracle.COM 			if (!no_undef)
21530Sstevel@tonic-gate 				continue;
21540Sstevel@tonic-gate 			if ((needed = str + (size_t)dyn->d_un.d_val) == NULL)
21550Sstevel@tonic-gate 				continue;
21560Sstevel@tonic-gate 
21570Sstevel@tonic-gate 			/*
21580Sstevel@tonic-gate 			 * Determine if this needed entry is already recorded on
21590Sstevel@tonic-gate 			 * the shared object needed list, if not create a new
21600Sstevel@tonic-gate 			 * definition for later processing (see finish_libs()).
21610Sstevel@tonic-gate 			 */
21629131SRod.Evans@Sun.COM 			needed = expand(ifl->ifl_name, needed, NULL);
21630Sstevel@tonic-gate 
21649131SRod.Evans@Sun.COM 			if ((sdf = sdf_find(needed, ofl->ofl_soneed)) == NULL) {
21650Sstevel@tonic-gate 				if ((sdf = sdf_add(needed,
21660Sstevel@tonic-gate 				    &ofl->ofl_soneed)) == (Sdf_desc *)S_ERROR)
21670Sstevel@tonic-gate 					return (S_ERROR);
21680Sstevel@tonic-gate 				sdf->sdf_rfile = ifl->ifl_name;
21690Sstevel@tonic-gate 			}
21700Sstevel@tonic-gate 
21710Sstevel@tonic-gate 			/*
21720Sstevel@tonic-gate 			 * Record the runpath (Note that we take the first
21730Sstevel@tonic-gate 			 * runpath which is exactly what ld.so.1 would do during
21740Sstevel@tonic-gate 			 * its dependency processing).
21750Sstevel@tonic-gate 			 */
217610792SRod.Evans@Sun.COM 			if (rpath && (sdf->sdf_rpath == NULL))
21770Sstevel@tonic-gate 				sdf->sdf_rpath = rpath;
21780Sstevel@tonic-gate 
21790Sstevel@tonic-gate 		} else if (dyn->d_tag == DT_FLAGS_1) {
21800Sstevel@tonic-gate 			if (dyn->d_un.d_val & (DF_1_INITFIRST | DF_1_INTERPOSE))
21810Sstevel@tonic-gate 				ifl->ifl_flags &= ~FLG_IF_LAZYLD;
21820Sstevel@tonic-gate 			if (dyn->d_un.d_val & DF_1_DISPRELPND)
21830Sstevel@tonic-gate 				ifl->ifl_flags |= FLG_IF_DISPPEND;
21840Sstevel@tonic-gate 			if (dyn->d_un.d_val & DF_1_DISPRELDNE)
21850Sstevel@tonic-gate 				ifl->ifl_flags |= FLG_IF_DISPDONE;
21860Sstevel@tonic-gate 			if (dyn->d_un.d_val & DF_1_NODIRECT)
21870Sstevel@tonic-gate 				ifl->ifl_flags |= FLG_IF_NODIRECT;
21880Sstevel@tonic-gate 
2189*13074SAli.Bahrami@Oracle.COM 			/*
2190*13074SAli.Bahrami@Oracle.COM 			 * If we are building an executable, and this
2191*13074SAli.Bahrami@Oracle.COM 			 * dependency is tagged as an interposer, then
2192*13074SAli.Bahrami@Oracle.COM 			 * assume that it is required even if symbol
2193*13074SAli.Bahrami@Oracle.COM 			 * resolution uncovers no evident use.
2194*13074SAli.Bahrami@Oracle.COM 			 *
2195*13074SAli.Bahrami@Oracle.COM 			 * If we are building a shared object, then an
2196*13074SAli.Bahrami@Oracle.COM 			 * interposer dependency has no special meaning, and we
2197*13074SAli.Bahrami@Oracle.COM 			 * treat it as a regular dependency. By definition, all
2198*13074SAli.Bahrami@Oracle.COM 			 * interposers must be visible to the runtime linker
2199*13074SAli.Bahrami@Oracle.COM 			 * at initialization time, and cannot be added later.
2200*13074SAli.Bahrami@Oracle.COM 			 */
2201*13074SAli.Bahrami@Oracle.COM 			if ((dyn->d_un.d_val & DF_1_INTERPOSE) &&
2202*13074SAli.Bahrami@Oracle.COM 			    (ofl->ofl_flags & FLG_OF_EXEC))
2203*13074SAli.Bahrami@Oracle.COM 				ifl->ifl_flags |= FLG_IF_DEPREQD;
2204*13074SAli.Bahrami@Oracle.COM 
22050Sstevel@tonic-gate 		} else if ((dyn->d_tag == DT_AUDIT) &&
22060Sstevel@tonic-gate 		    (ifl->ifl_flags & FLG_IF_NEEDED)) {
22070Sstevel@tonic-gate 			/*
22080Sstevel@tonic-gate 			 * Record audit string as DT_DEPAUDIT.
22090Sstevel@tonic-gate 			 */
22100Sstevel@tonic-gate 			if ((ofl->ofl_depaudit = add_string(ofl->ofl_depaudit,
22110Sstevel@tonic-gate 			    (str + (size_t)dyn->d_un.d_val))) ==
22120Sstevel@tonic-gate 			    (const char *)S_ERROR)
22130Sstevel@tonic-gate 				return (S_ERROR);
22140Sstevel@tonic-gate 
22150Sstevel@tonic-gate 		} else if (dyn->d_tag == DT_SUNW_RTLDINF) {
22160Sstevel@tonic-gate 			/*
221712449SRod.Evans@Sun.COM 			 * If this dependency has the DT_SUNW_RTLDINF .dynamic
221812449SRod.Evans@Sun.COM 			 * entry, then ensure no specialized dependency
221912449SRod.Evans@Sun.COM 			 * processing is in effect.  This tag identifies libc,
222012449SRod.Evans@Sun.COM 			 * which provides critical startup information (TLS
222112449SRod.Evans@Sun.COM 			 * routines, threads initialization, etc.) that must
222212449SRod.Evans@Sun.COM 			 * be exercised as part of process initialization.
22230Sstevel@tonic-gate 			 */
222412449SRod.Evans@Sun.COM 			ifl->ifl_flags &= ~MSK_IF_POSFLAG1;
2225*13074SAli.Bahrami@Oracle.COM 
2226*13074SAli.Bahrami@Oracle.COM 			/*
2227*13074SAli.Bahrami@Oracle.COM 			 * libc is not subject to the usual guidance checks
2228*13074SAli.Bahrami@Oracle.COM 			 * for lazy loading. It cannot be lazy loaded, libld
2229*13074SAli.Bahrami@Oracle.COM 			 * ignores the request, and rtld would ignore the
2230*13074SAli.Bahrami@Oracle.COM 			 * setting if it were present.
2231*13074SAli.Bahrami@Oracle.COM 			 */
2232*13074SAli.Bahrami@Oracle.COM 			ifl->ifl_flags |= FLG_IF_RTLDINF;
22330Sstevel@tonic-gate 		}
22340Sstevel@tonic-gate 	}
22350Sstevel@tonic-gate 
22360Sstevel@tonic-gate 	/*
22370Sstevel@tonic-gate 	 * Perform some SONAME sanity checks.
22380Sstevel@tonic-gate 	 */
22390Sstevel@tonic-gate 	if (ifl->ifl_flags & FLG_IF_NEEDED) {
22407463SRod.Evans@Sun.COM 		Ifl_desc	*sifl;
22419131SRod.Evans@Sun.COM 		Aliste		idx;
22420Sstevel@tonic-gate 
22430Sstevel@tonic-gate 		/*
22440Sstevel@tonic-gate 		 * Determine if anyone else will cause the same SONAME to be
22450Sstevel@tonic-gate 		 * used (this is either caused by two different files having the
22460Sstevel@tonic-gate 		 * same SONAME, or by one file SONAME actually matching another
22470Sstevel@tonic-gate 		 * file basename (if no SONAME is specified within a shared
22480Sstevel@tonic-gate 		 * library its basename will be used)). Probably rare, but some
22490Sstevel@tonic-gate 		 * idiot will do it.
22500Sstevel@tonic-gate 		 */
22519131SRod.Evans@Sun.COM 		for (APLIST_TRAVERSE(ofl->ofl_sos, idx, sifl)) {
22520Sstevel@tonic-gate 			if ((strcmp(ifl->ifl_soname, sifl->ifl_soname) == 0) &&
22530Sstevel@tonic-gate 			    (ifl != sifl)) {
22540Sstevel@tonic-gate 				const char	*hint, *iflb, *siflb;
22550Sstevel@tonic-gate 
22560Sstevel@tonic-gate 				/*
22570Sstevel@tonic-gate 				 * Determine the basename of each file. Perhaps
22580Sstevel@tonic-gate 				 * there are multiple copies of the same file
22590Sstevel@tonic-gate 				 * being brought in using different -L search
22600Sstevel@tonic-gate 				 * paths, and if so give an extra hint in the
22610Sstevel@tonic-gate 				 * error message.
22620Sstevel@tonic-gate 				 */
22630Sstevel@tonic-gate 				iflb = strrchr(ifl->ifl_name, '/');
22640Sstevel@tonic-gate 				if (iflb == NULL)
22650Sstevel@tonic-gate 					iflb = ifl->ifl_name;
22660Sstevel@tonic-gate 				else
22670Sstevel@tonic-gate 					iflb++;
22680Sstevel@tonic-gate 
22690Sstevel@tonic-gate 				siflb = strrchr(sifl->ifl_name, '/');
22700Sstevel@tonic-gate 				if (siflb == NULL)
22710Sstevel@tonic-gate 					siflb = sifl->ifl_name;
22720Sstevel@tonic-gate 				else
22730Sstevel@tonic-gate 					siflb++;
22740Sstevel@tonic-gate 
22750Sstevel@tonic-gate 				if (strcmp(iflb, siflb) == 0)
22760Sstevel@tonic-gate 					hint = MSG_INTL(MSG_REC_CNFLTHINT);
22770Sstevel@tonic-gate 				else
22780Sstevel@tonic-gate 					hint = MSG_ORIG(MSG_STR_EMPTY);
22790Sstevel@tonic-gate 
2280*13074SAli.Bahrami@Oracle.COM 				ld_eprintf(ofl, ERR_FATAL,
22811618Srie 				    MSG_INTL(MSG_REC_OBJCNFLT), sifl->ifl_name,
22821618Srie 				    ifl->ifl_name, sifl->ifl_soname, hint);
22830Sstevel@tonic-gate 				return (0);
22840Sstevel@tonic-gate 			}
22850Sstevel@tonic-gate 		}
22860Sstevel@tonic-gate 
22870Sstevel@tonic-gate 		/*
22880Sstevel@tonic-gate 		 * If the SONAME is the same as the name the user wishes to
22890Sstevel@tonic-gate 		 * record when building a dynamic library (refer -h option),
22900Sstevel@tonic-gate 		 * we also have a name clash.
22910Sstevel@tonic-gate 		 */
22920Sstevel@tonic-gate 		if (ofl->ofl_soname &&
22930Sstevel@tonic-gate 		    (strcmp(ofl->ofl_soname, ifl->ifl_soname) == 0)) {
2294*13074SAli.Bahrami@Oracle.COM 			ld_eprintf(ofl, ERR_FATAL,
22951618Srie 			    MSG_INTL(MSG_REC_OPTCNFLT), ifl->ifl_name,
22967983SAli.Bahrami@Sun.COM 			    MSG_INTL(MSG_MARG_SONAME), ifl->ifl_soname);
22970Sstevel@tonic-gate 			return (0);
22980Sstevel@tonic-gate 		}
22990Sstevel@tonic-gate 	}
23000Sstevel@tonic-gate 	return (1);
23010Sstevel@tonic-gate }
23020Sstevel@tonic-gate 
23030Sstevel@tonic-gate /*
23049085SAli.Bahrami@Sun.COM  * Process a progbits section from a relocatable object (ET_REL).
23059085SAli.Bahrami@Sun.COM  * This is used on non-amd64 objects to recognize .eh_frame sections.
23069085SAli.Bahrami@Sun.COM  */
23079085SAli.Bahrami@Sun.COM /*ARGSUSED1*/
23089085SAli.Bahrami@Sun.COM static uintptr_t
process_progbits_final(Is_desc * isc,Ifl_desc * ifl,Ofl_desc * ofl)23099085SAli.Bahrami@Sun.COM process_progbits_final(Is_desc *isc, Ifl_desc *ifl, Ofl_desc *ofl)
23109085SAli.Bahrami@Sun.COM {
23119085SAli.Bahrami@Sun.COM 	if (isc->is_osdesc && (isc->is_flags & FLG_IS_EHFRAME) &&
23129085SAli.Bahrami@Sun.COM 	    (ld_unwind_register(isc->is_osdesc, ofl) == S_ERROR))
23139085SAli.Bahrami@Sun.COM 		return (S_ERROR);
23149085SAli.Bahrami@Sun.COM 
23159085SAli.Bahrami@Sun.COM 	return (1);
23169085SAli.Bahrami@Sun.COM }
23179085SAli.Bahrami@Sun.COM 
23189085SAli.Bahrami@Sun.COM /*
23197463SRod.Evans@Sun.COM  * Process a group section.
23207463SRod.Evans@Sun.COM  */
23217463SRod.Evans@Sun.COM static uintptr_t
process_group(const char * name,Ifl_desc * ifl,Shdr * shdr,Elf_Scn * scn,Word ndx,int ident,Ofl_desc * ofl)23227463SRod.Evans@Sun.COM process_group(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
23237463SRod.Evans@Sun.COM     Word ndx, int ident, Ofl_desc *ofl)
23247463SRod.Evans@Sun.COM {
23257463SRod.Evans@Sun.COM 	uintptr_t	error;
23267463SRod.Evans@Sun.COM 
23277463SRod.Evans@Sun.COM 	error = process_section(name, ifl, shdr, scn, ndx, ident, ofl);
23287463SRod.Evans@Sun.COM 	if ((error == 0) || (error == S_ERROR))
23297463SRod.Evans@Sun.COM 		return (error);
23307463SRod.Evans@Sun.COM 
23317463SRod.Evans@Sun.COM 	/*
23327463SRod.Evans@Sun.COM 	 * Indicate that this input file has groups to process.  Groups are
23337463SRod.Evans@Sun.COM 	 * processed after all input sections have been processed.
23347463SRod.Evans@Sun.COM 	 */
23357463SRod.Evans@Sun.COM 	ifl->ifl_flags |= FLG_IS_GROUPS;
23367463SRod.Evans@Sun.COM 
23377463SRod.Evans@Sun.COM 	return (1);
23387463SRod.Evans@Sun.COM }
23397463SRod.Evans@Sun.COM 
23407463SRod.Evans@Sun.COM /*
23410Sstevel@tonic-gate  * Process a relocation entry. At this point all input sections from this
23420Sstevel@tonic-gate  * input file have been assigned an input section descriptor which is saved
23430Sstevel@tonic-gate  * in the `ifl_isdesc' array.
23440Sstevel@tonic-gate  */
23451618Srie static uintptr_t
rel_process(Is_desc * isc,Ifl_desc * ifl,Ofl_desc * ofl)23460Sstevel@tonic-gate rel_process(Is_desc *isc, Ifl_desc *ifl, Ofl_desc *ofl)
23470Sstevel@tonic-gate {
23480Sstevel@tonic-gate 	Word 	rndx;
23490Sstevel@tonic-gate 	Is_desc	*risc;
23500Sstevel@tonic-gate 	Os_desc	*osp;
23510Sstevel@tonic-gate 	Shdr	*shdr = isc->is_shdr;
23524734Sab196087 	Conv_inv_buf_t inv_buf;
23530Sstevel@tonic-gate 
23540Sstevel@tonic-gate 	/*
23550Sstevel@tonic-gate 	 * Make sure this is a valid relocation we can handle.
23560Sstevel@tonic-gate 	 */
23576206Sab196087 	if (shdr->sh_type != ld_targ.t_m.m_rel_sht_type) {
2358*13074SAli.Bahrami@Oracle.COM 		ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_FIL_INVALSEC),
23599878SAli.Bahrami@Sun.COM 		    ifl->ifl_name, EC_WORD(isc->is_scnndx), isc->is_name,
23609273SAli.Bahrami@Sun.COM 		    conv_sec_type(ifl->ifl_ehdr->e_ident[EI_OSABI],
23619273SAli.Bahrami@Sun.COM 		    ifl->ifl_ehdr->e_machine, shdr->sh_type, 0, &inv_buf));
23620Sstevel@tonic-gate 		return (0);
23630Sstevel@tonic-gate 	}
23640Sstevel@tonic-gate 
23650Sstevel@tonic-gate 	/*
23660Sstevel@tonic-gate 	 * From the relocation section header information determine which
23670Sstevel@tonic-gate 	 * section needs the actual relocation.  Determine which output section
23680Sstevel@tonic-gate 	 * this input section has been assigned to and add to its relocation
23690Sstevel@tonic-gate 	 * list.  Note that the relocation section may be null if it is not
23700Sstevel@tonic-gate 	 * required (ie. .debug, .stabs, etc).
23710Sstevel@tonic-gate 	 */
23720Sstevel@tonic-gate 	rndx = shdr->sh_info;
23730Sstevel@tonic-gate 	if (rndx >= ifl->ifl_shnum) {
23740Sstevel@tonic-gate 		/*
23750Sstevel@tonic-gate 		 * Broken input file.
23760Sstevel@tonic-gate 		 */
2377*13074SAli.Bahrami@Oracle.COM 		ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_FIL_INVSHINFO),
23789878SAli.Bahrami@Sun.COM 		    ifl->ifl_name, EC_WORD(isc->is_scnndx), isc->is_name,
23799878SAli.Bahrami@Sun.COM 		    EC_XWORD(rndx));
23800Sstevel@tonic-gate 		return (0);
23810Sstevel@tonic-gate 	}
23820Sstevel@tonic-gate 	if (rndx == 0) {
23839131SRod.Evans@Sun.COM 		if (aplist_append(&ofl->ofl_extrarels, isc,
23849131SRod.Evans@Sun.COM 		    AL_CNT_OFL_RELS) == NULL)
23850Sstevel@tonic-gate 			return (S_ERROR);
23869131SRod.Evans@Sun.COM 
23879131SRod.Evans@Sun.COM 	} else if ((risc = ifl->ifl_isdesc[rndx]) != NULL) {
23880Sstevel@tonic-gate 		/*
23890Sstevel@tonic-gate 		 * Discard relocations if they are against a section
23900Sstevel@tonic-gate 		 * which has been discarded.
23910Sstevel@tonic-gate 		 */
23920Sstevel@tonic-gate 		if (risc->is_flags & FLG_IS_DISCARD)
23930Sstevel@tonic-gate 			return (1);
23949131SRod.Evans@Sun.COM 
23959131SRod.Evans@Sun.COM 		if ((osp = risc->is_osdesc) == NULL) {
23960Sstevel@tonic-gate 			if (risc->is_shdr->sh_type == SHT_SUNW_move) {
23970Sstevel@tonic-gate 				/*
23989131SRod.Evans@Sun.COM 				 * This section is processed later in
23999131SRod.Evans@Sun.COM 				 * process_movereloc().
24000Sstevel@tonic-gate 				 */
24019131SRod.Evans@Sun.COM 				if (aplist_append(&ofl->ofl_ismoverel,
24029131SRod.Evans@Sun.COM 				    isc, AL_CNT_OFL_MOVE) == NULL)
24030Sstevel@tonic-gate 					return (S_ERROR);
24040Sstevel@tonic-gate 				return (1);
24050Sstevel@tonic-gate 			}
2406*13074SAli.Bahrami@Oracle.COM 			ld_eprintf(ofl, ERR_FATAL,
24071618Srie 			    MSG_INTL(MSG_FIL_INVRELOC1), ifl->ifl_name,
24089878SAli.Bahrami@Sun.COM 			    EC_WORD(isc->is_scnndx), isc->is_name,
24099878SAli.Bahrami@Sun.COM 			    EC_WORD(risc->is_scnndx), risc->is_name);
24100Sstevel@tonic-gate 			return (0);
24110Sstevel@tonic-gate 		}
24128608SAli.Bahrami@Sun.COM 		if (aplist_append(&osp->os_relisdescs, isc,
24138608SAli.Bahrami@Sun.COM 		    AL_CNT_OS_RELISDESCS) == NULL)
24140Sstevel@tonic-gate 			return (S_ERROR);
24150Sstevel@tonic-gate 	}
24160Sstevel@tonic-gate 	return (1);
24170Sstevel@tonic-gate }
24180Sstevel@tonic-gate 
24190Sstevel@tonic-gate /*
24200Sstevel@tonic-gate  * SHF_EXCLUDE flags is set for this section.
24210Sstevel@tonic-gate  */
24220Sstevel@tonic-gate static uintptr_t
process_exclude(const char * name,Ifl_desc * ifl,Shdr * shdr,Elf_Scn * scn,Word ndx,Ofl_desc * ofl)24230Sstevel@tonic-gate process_exclude(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
24240Sstevel@tonic-gate     Word ndx, Ofl_desc *ofl)
24250Sstevel@tonic-gate {
24260Sstevel@tonic-gate 	/*
24270Sstevel@tonic-gate 	 * Sections SHT_SYMTAB and SHT_DYNDYM, even if SHF_EXCLUDE is on, might
24280Sstevel@tonic-gate 	 * be needed for ld processing.  These sections need to be in the
24290Sstevel@tonic-gate 	 * internal table.  Later it will be determined whether they can be
24300Sstevel@tonic-gate 	 * eliminated or not.
24310Sstevel@tonic-gate 	 */
24320Sstevel@tonic-gate 	if (shdr->sh_type == SHT_SYMTAB || shdr->sh_type == SHT_DYNSYM)
24330Sstevel@tonic-gate 		return (0);
24340Sstevel@tonic-gate 
24350Sstevel@tonic-gate 	/*
24360Sstevel@tonic-gate 	 * Other checks
24370Sstevel@tonic-gate 	 */
24380Sstevel@tonic-gate 	if (shdr->sh_flags & SHF_ALLOC) {
24390Sstevel@tonic-gate 		/*
24400Sstevel@tonic-gate 		 * A conflict, issue an warning message, and ignore the section.
24410Sstevel@tonic-gate 		 */
2442*13074SAli.Bahrami@Oracle.COM 		ld_eprintf(ofl, ERR_WARNING, MSG_INTL(MSG_FIL_EXCLUDE),
24439878SAli.Bahrami@Sun.COM 		    ifl->ifl_name, EC_WORD(ndx), name);
24440Sstevel@tonic-gate 		return (0);
24450Sstevel@tonic-gate 	}
24460Sstevel@tonic-gate 
24470Sstevel@tonic-gate 	/*
24480Sstevel@tonic-gate 	 * This sections is not going to the output file.
24490Sstevel@tonic-gate 	 */
24500Sstevel@tonic-gate 	return (process_section(name, ifl, shdr, scn, ndx, 0, ofl));
24510Sstevel@tonic-gate }
24520Sstevel@tonic-gate 
24530Sstevel@tonic-gate /*
24540Sstevel@tonic-gate  * Section processing state table.  `Initial' describes the required initial
24550Sstevel@tonic-gate  * procedure to be called (if any), `Final' describes the final processing
24560Sstevel@tonic-gate  * procedure (ie. things that can only be done when all required sections
24570Sstevel@tonic-gate  * have been collected).
24580Sstevel@tonic-gate  */
24599085SAli.Bahrami@Sun.COM typedef uintptr_t	(* initial_func_t)(const char *, Ifl_desc *, Shdr *,
24609085SAli.Bahrami@Sun.COM 			    Elf_Scn *, Word, int, Ofl_desc *);
24610Sstevel@tonic-gate 
24629085SAli.Bahrami@Sun.COM static initial_func_t Initial[SHT_NUM][2] = {
24630Sstevel@tonic-gate /*			ET_REL			ET_DYN			*/
24640Sstevel@tonic-gate 
24650Sstevel@tonic-gate /* SHT_NULL	*/	invalid_section,	invalid_section,
24660Sstevel@tonic-gate /* SHT_PROGBITS	*/	process_progbits,	process_progbits,
24670Sstevel@tonic-gate /* SHT_SYMTAB	*/	process_input,		process_input,
24680Sstevel@tonic-gate /* SHT_STRTAB	*/	process_strtab,		process_strtab,
24690Sstevel@tonic-gate /* SHT_RELA	*/	process_reloc,		process_reloc,
24700Sstevel@tonic-gate /* SHT_HASH	*/	invalid_section,	NULL,
24714716Sab196087 /* SHT_DYNAMIC	*/	process_rel_dynamic,	process_dynamic_isgnu,
24720Sstevel@tonic-gate /* SHT_NOTE	*/	process_section,	NULL,
24730Sstevel@tonic-gate /* SHT_NOBITS	*/	process_nobits,		process_nobits,
24740Sstevel@tonic-gate /* SHT_REL	*/	process_reloc,		process_reloc,
24750Sstevel@tonic-gate /* SHT_SHLIB	*/	process_section,	invalid_section,
24760Sstevel@tonic-gate /* SHT_DYNSYM	*/	invalid_section,	process_input,
24770Sstevel@tonic-gate /* SHT_UNKNOWN12 */	process_progbits,	process_progbits,
24780Sstevel@tonic-gate /* SHT_UNKNOWN13 */	process_progbits,	process_progbits,
24790Sstevel@tonic-gate /* SHT_INIT_ARRAY */	process_array,		NULL,
24800Sstevel@tonic-gate /* SHT_FINI_ARRAY */	process_array,		NULL,
24810Sstevel@tonic-gate /* SHT_PREINIT_ARRAY */	process_array,		NULL,
24827463SRod.Evans@Sun.COM /* SHT_GROUP */		process_group,		invalid_section,
24830Sstevel@tonic-gate /* SHT_SYMTAB_SHNDX */	process_sym_shndx,	NULL
24840Sstevel@tonic-gate };
24850Sstevel@tonic-gate 
24869085SAli.Bahrami@Sun.COM typedef uintptr_t	(* final_func_t)(Is_desc *, Ifl_desc *, Ofl_desc *);
24879085SAli.Bahrami@Sun.COM 
24889085SAli.Bahrami@Sun.COM static final_func_t Final[SHT_NUM][2] = {
24899085SAli.Bahrami@Sun.COM /*			ET_REL			ET_DYN			*/
24900Sstevel@tonic-gate 
24910Sstevel@tonic-gate /* SHT_NULL	*/	NULL,			NULL,
24929085SAli.Bahrami@Sun.COM /* SHT_PROGBITS	*/	process_progbits_final,	NULL,
24931618Srie /* SHT_SYMTAB	*/	ld_sym_process,		ld_sym_process,
24940Sstevel@tonic-gate /* SHT_STRTAB	*/	NULL,			NULL,
24950Sstevel@tonic-gate /* SHT_RELA	*/	rel_process,		NULL,
24960Sstevel@tonic-gate /* SHT_HASH	*/	NULL,			NULL,
24970Sstevel@tonic-gate /* SHT_DYNAMIC	*/	NULL,			process_dynamic,
24980Sstevel@tonic-gate /* SHT_NOTE	*/	NULL,			NULL,
24990Sstevel@tonic-gate /* SHT_NOBITS	*/	NULL,			NULL,
25000Sstevel@tonic-gate /* SHT_REL	*/	rel_process,		NULL,
25010Sstevel@tonic-gate /* SHT_SHLIB	*/	NULL,			NULL,
25021618Srie /* SHT_DYNSYM	*/	NULL,			ld_sym_process,
25030Sstevel@tonic-gate /* SHT_UNKNOWN12 */	NULL,			NULL,
25040Sstevel@tonic-gate /* SHT_UNKNOWN13 */	NULL,			NULL,
25057463SRod.Evans@Sun.COM /* SHT_INIT_ARRAY */	array_process,		NULL,
25067463SRod.Evans@Sun.COM /* SHT_FINI_ARRAY */	array_process,		NULL,
25077463SRod.Evans@Sun.COM /* SHT_PREINIT_ARRAY */	array_process,		NULL,
25080Sstevel@tonic-gate /* SHT_GROUP */		NULL,			NULL,
25090Sstevel@tonic-gate /* SHT_SYMTAB_SHNDX */	sym_shndx_process,	NULL
25100Sstevel@tonic-gate };
25110Sstevel@tonic-gate 
25120Sstevel@tonic-gate #define	MAXNDXSIZE	10
25130Sstevel@tonic-gate 
25140Sstevel@tonic-gate /*
25150Sstevel@tonic-gate  * Process an elf file.  Each section is compared against the section state
25160Sstevel@tonic-gate  * table to determine whether it should be processed (saved), ignored, or
25170Sstevel@tonic-gate  * is invalid for the type of input file being processed.
25180Sstevel@tonic-gate  */
25191618Srie static uintptr_t
process_elf(Ifl_desc * ifl,Elf * elf,Ofl_desc * ofl)25200Sstevel@tonic-gate process_elf(Ifl_desc *ifl, Elf *elf, Ofl_desc *ofl)
25210Sstevel@tonic-gate {
25220Sstevel@tonic-gate 	Elf_Scn		*scn;
25230Sstevel@tonic-gate 	Shdr		*shdr;
25247463SRod.Evans@Sun.COM 	Word		ndx, sndx, ordndx = 0, ordcnt = 0;
25259878SAli.Bahrami@Sun.COM 	char		*str, *name;
25260Sstevel@tonic-gate 	Word		row, column;
25270Sstevel@tonic-gate 	int		ident;
25280Sstevel@tonic-gate 	uintptr_t	error;
252911827SRod.Evans@Sun.COM 	Is_desc		*vdfisp, *vndisp, *vsyisp, *sifisp;
253011827SRod.Evans@Sun.COM 	Is_desc		*capinfoisp, *capisp;
25310Sstevel@tonic-gate 	Sdf_desc	*sdf;
253211734SAli.Bahrami@Sun.COM 	Place_path_info	path_info_buf, *path_info;
253311734SAli.Bahrami@Sun.COM 
253411734SAli.Bahrami@Sun.COM 	/*
253511734SAli.Bahrami@Sun.COM 	 * Path information buffer used by ld_place_section() and related
253611734SAli.Bahrami@Sun.COM 	 * routines. This information is used to evaluate entrance criteria
253711734SAli.Bahrami@Sun.COM 	 * with non-empty file matching lists (ec_files).
253811734SAli.Bahrami@Sun.COM 	 */
253911734SAli.Bahrami@Sun.COM 	path_info = ld_place_path_info_init(ofl, ifl, &path_info_buf);
25400Sstevel@tonic-gate 
25410Sstevel@tonic-gate 	/*
25420Sstevel@tonic-gate 	 * First process the .shstrtab section so that later sections can
25430Sstevel@tonic-gate 	 * reference their name.
25440Sstevel@tonic-gate 	 */
25451618Srie 	ld_sup_file(ofl, ifl->ifl_name, elf_kind(elf), ifl->ifl_flags, elf);
25460Sstevel@tonic-gate 
25470Sstevel@tonic-gate 	sndx = ifl->ifl_shstrndx;
25480Sstevel@tonic-gate 	if ((scn = elf_getscn(elf, (size_t)sndx)) == NULL) {
2549*13074SAli.Bahrami@Oracle.COM 		ld_eprintf(ofl, ERR_ELF, MSG_INTL(MSG_ELF_GETSCN),
25501618Srie 		    ifl->ifl_name);
25510Sstevel@tonic-gate 		return (0);
25520Sstevel@tonic-gate 	}
25530Sstevel@tonic-gate 	if ((shdr = elf_getshdr(scn)) == NULL) {
2554*13074SAli.Bahrami@Oracle.COM 		ld_eprintf(ofl, ERR_ELF, MSG_INTL(MSG_ELF_GETSHDR),
25551618Srie 		    ifl->ifl_name);
25560Sstevel@tonic-gate 		return (0);
25570Sstevel@tonic-gate 	}
25580Sstevel@tonic-gate 	if ((name = elf_strptr(elf, (size_t)sndx, (size_t)shdr->sh_name)) ==
25590Sstevel@tonic-gate 	    NULL) {
2560*13074SAli.Bahrami@Oracle.COM 		ld_eprintf(ofl, ERR_ELF, MSG_INTL(MSG_ELF_STRPTR),
25611618Srie 		    ifl->ifl_name);
25620Sstevel@tonic-gate 		return (0);
25630Sstevel@tonic-gate 	}
25640Sstevel@tonic-gate 
25652647Srie 	if (ld_sup_input_section(ofl, ifl, name, &shdr, sndx, scn,
25662647Srie 	    elf) == S_ERROR)
25670Sstevel@tonic-gate 		return (S_ERROR);
25680Sstevel@tonic-gate 
25690Sstevel@tonic-gate 	/*
25700Sstevel@tonic-gate 	 * Reset the name since the shdr->sh_name could have been changed as
25719878SAli.Bahrami@Sun.COM 	 * part of ld_sup_input_section().
25720Sstevel@tonic-gate 	 */
25739878SAli.Bahrami@Sun.COM 	if ((name = elf_strptr(elf, (size_t)sndx, (size_t)shdr->sh_name)) ==
25749878SAli.Bahrami@Sun.COM 	    NULL) {
2575*13074SAli.Bahrami@Oracle.COM 		ld_eprintf(ofl, ERR_ELF, MSG_INTL(MSG_ELF_STRPTR),
25761618Srie 		    ifl->ifl_name);
25770Sstevel@tonic-gate 		return (0);
25780Sstevel@tonic-gate 	}
25790Sstevel@tonic-gate 
25800Sstevel@tonic-gate 	error = process_strtab(name, ifl, shdr, scn, sndx, FALSE, ofl);
25810Sstevel@tonic-gate 	if ((error == 0) || (error == S_ERROR))
25820Sstevel@tonic-gate 		return (error);
25830Sstevel@tonic-gate 	str = ifl->ifl_isdesc[sndx]->is_indata->d_buf;
25840Sstevel@tonic-gate 
25850Sstevel@tonic-gate 	/*
25860Sstevel@tonic-gate 	 * Determine the state table column from the input file type.  Note,
25870Sstevel@tonic-gate 	 * shared library sections are not added to the output section list.
25880Sstevel@tonic-gate 	 */
25890Sstevel@tonic-gate 	if (ifl->ifl_ehdr->e_type == ET_DYN) {
25900Sstevel@tonic-gate 		column = 1;
25910Sstevel@tonic-gate 		ofl->ofl_soscnt++;
25926206Sab196087 		ident = ld_targ.t_id.id_null;
25930Sstevel@tonic-gate 	} else {
25940Sstevel@tonic-gate 		column = 0;
25950Sstevel@tonic-gate 		ofl->ofl_objscnt++;
25966206Sab196087 		ident = ld_targ.t_id.id_unknown;
25970Sstevel@tonic-gate 	}
25980Sstevel@tonic-gate 
25991618Srie 	DBG_CALL(Dbg_file_generic(ofl->ofl_lml, ifl));
26000Sstevel@tonic-gate 	ndx = 0;
260111827SRod.Evans@Sun.COM 	vdfisp = vndisp = vsyisp = sifisp = capinfoisp = capisp = NULL;
26020Sstevel@tonic-gate 	scn = NULL;
26030Sstevel@tonic-gate 	while (scn = elf_nextscn(elf, scn)) {
26040Sstevel@tonic-gate 		ndx++;
26057463SRod.Evans@Sun.COM 
26060Sstevel@tonic-gate 		/*
26070Sstevel@tonic-gate 		 * As we've already processed the .shstrtab don't do it again.
26080Sstevel@tonic-gate 		 */
26090Sstevel@tonic-gate 		if (ndx == sndx)
26100Sstevel@tonic-gate 			continue;
26110Sstevel@tonic-gate 
26120Sstevel@tonic-gate 		if ((shdr = elf_getshdr(scn)) == NULL) {
2613*13074SAli.Bahrami@Oracle.COM 			ld_eprintf(ofl, ERR_ELF, MSG_INTL(MSG_ELF_GETSHDR),
2614*13074SAli.Bahrami@Oracle.COM 			    ifl->ifl_name);
26150Sstevel@tonic-gate 			return (0);
26160Sstevel@tonic-gate 		}
26170Sstevel@tonic-gate 		name = str + (size_t)(shdr->sh_name);
26180Sstevel@tonic-gate 
26192647Srie 		if (ld_sup_input_section(ofl, ifl, name, &shdr, ndx, scn,
26202647Srie 		    elf) == S_ERROR)
26210Sstevel@tonic-gate 			return (S_ERROR);
26220Sstevel@tonic-gate 
26230Sstevel@tonic-gate 		/*
26240Sstevel@tonic-gate 		 * Reset the name since the shdr->sh_name could have been
26259878SAli.Bahrami@Sun.COM 		 * changed as part of ld_sup_input_section().
26260Sstevel@tonic-gate 		 */
26279878SAli.Bahrami@Sun.COM 		name = str + (size_t)(shdr->sh_name);
26280Sstevel@tonic-gate 
26290Sstevel@tonic-gate 		row = shdr->sh_type;
26300Sstevel@tonic-gate 
26310Sstevel@tonic-gate 		/*
26320Sstevel@tonic-gate 		 * If the section has the SHF_EXCLUDE flag on, and we're not
26330Sstevel@tonic-gate 		 * generating a relocatable object, exclude the section.
26340Sstevel@tonic-gate 		 */
26350Sstevel@tonic-gate 		if (((shdr->sh_flags & SHF_EXCLUDE) != 0) &&
26360Sstevel@tonic-gate 		    ((ofl->ofl_flags & FLG_OF_RELOBJ) == 0)) {
26370Sstevel@tonic-gate 			if ((error = process_exclude(name, ifl, shdr, scn,
26380Sstevel@tonic-gate 			    ndx, ofl)) == S_ERROR)
26390Sstevel@tonic-gate 				return (S_ERROR);
26400Sstevel@tonic-gate 			if (error == 1)
26410Sstevel@tonic-gate 				continue;
26420Sstevel@tonic-gate 		}
26430Sstevel@tonic-gate 
26440Sstevel@tonic-gate 		/*
26450Sstevel@tonic-gate 		 * If this is a standard section type process it via the
26460Sstevel@tonic-gate 		 * appropriate action routine.
26470Sstevel@tonic-gate 		 */
26480Sstevel@tonic-gate 		if (row < SHT_NUM) {
26490Sstevel@tonic-gate 			if (Initial[row][column] != NULL) {
26500Sstevel@tonic-gate 				if (Initial[row][column](name, ifl, shdr, scn,
26510Sstevel@tonic-gate 				    ndx, ident, ofl) == S_ERROR)
26520Sstevel@tonic-gate 					return (S_ERROR);
26530Sstevel@tonic-gate 			}
26540Sstevel@tonic-gate 		} else {
26550Sstevel@tonic-gate 			/*
26560Sstevel@tonic-gate 			 * If this section is below SHT_LOSUNW then we don't
26570Sstevel@tonic-gate 			 * really know what to do with it, issue a warning
26580Sstevel@tonic-gate 			 * message but do the basic section processing anyway.
26590Sstevel@tonic-gate 			 */
26604734Sab196087 			if (row < (Word)SHT_LOSUNW) {
26614734Sab196087 				Conv_inv_buf_t inv_buf;
26624734Sab196087 
2663*13074SAli.Bahrami@Oracle.COM 				ld_eprintf(ofl, ERR_WARNING,
26641618Srie 				    MSG_INTL(MSG_FIL_INVALSEC), ifl->ifl_name,
26659878SAli.Bahrami@Sun.COM 				    EC_WORD(ndx), name, conv_sec_type(
26669273SAli.Bahrami@Sun.COM 				    ifl->ifl_ehdr->e_ident[EI_OSABI],
26679273SAli.Bahrami@Sun.COM 				    ifl->ifl_ehdr->e_machine,
26684734Sab196087 				    shdr->sh_type, 0, &inv_buf));
26694734Sab196087 			}
26700Sstevel@tonic-gate 
26710Sstevel@tonic-gate 			/*
26720Sstevel@tonic-gate 			 * Handle sections greater than SHT_LOSUNW.
26730Sstevel@tonic-gate 			 */
26740Sstevel@tonic-gate 			switch (row) {
26750Sstevel@tonic-gate 			case SHT_SUNW_dof:
26760Sstevel@tonic-gate 				if (process_section(name, ifl, shdr, scn,
26770Sstevel@tonic-gate 				    ndx, ident, ofl) == S_ERROR)
26780Sstevel@tonic-gate 					return (S_ERROR);
26790Sstevel@tonic-gate 				break;
26800Sstevel@tonic-gate 			case SHT_SUNW_cap:
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 				capisp = ifl->ifl_isdesc[ndx];
26850Sstevel@tonic-gate 				break;
268611827SRod.Evans@Sun.COM 			case SHT_SUNW_capinfo:
268711827SRod.Evans@Sun.COM 				if (process_section(name, ifl, shdr, scn, ndx,
268811827SRod.Evans@Sun.COM 				    ld_targ.t_id.id_null, ofl) == S_ERROR)
268911827SRod.Evans@Sun.COM 					return (S_ERROR);
269011827SRod.Evans@Sun.COM 				capinfoisp = ifl->ifl_isdesc[ndx];
269111827SRod.Evans@Sun.COM 				break;
26920Sstevel@tonic-gate 			case SHT_SUNW_DEBUGSTR:
26930Sstevel@tonic-gate 			case SHT_SUNW_DEBUG:
26940Sstevel@tonic-gate 				if (process_debug(name, ifl, shdr, scn,
26950Sstevel@tonic-gate 				    ndx, ident, ofl) == S_ERROR)
26960Sstevel@tonic-gate 					return (S_ERROR);
26970Sstevel@tonic-gate 				break;
26980Sstevel@tonic-gate 			case SHT_SUNW_move:
26999085SAli.Bahrami@Sun.COM 				if (process_section(name, ifl, shdr, scn, ndx,
27009085SAli.Bahrami@Sun.COM 				    ld_targ.t_id.id_null, ofl) == S_ERROR)
27010Sstevel@tonic-gate 					return (S_ERROR);
27020Sstevel@tonic-gate 				break;
27030Sstevel@tonic-gate 			case SHT_SUNW_syminfo:
27049085SAli.Bahrami@Sun.COM 				if (process_section(name, ifl, shdr, scn, ndx,
27059085SAli.Bahrami@Sun.COM 				    ld_targ.t_id.id_null, ofl) == S_ERROR)
27060Sstevel@tonic-gate 					return (S_ERROR);
27070Sstevel@tonic-gate 				sifisp = ifl->ifl_isdesc[ndx];
27080Sstevel@tonic-gate 				break;
27090Sstevel@tonic-gate 			case SHT_SUNW_ANNOTATE:
27107463SRod.Evans@Sun.COM 				if (process_progbits(name, ifl, shdr, scn,
27117463SRod.Evans@Sun.COM 				    ndx, ident, ofl) == S_ERROR)
27127463SRod.Evans@Sun.COM 					return (S_ERROR);
27137463SRod.Evans@Sun.COM 				break;
27140Sstevel@tonic-gate 			case SHT_SUNW_COMDAT:
27150Sstevel@tonic-gate 				if (process_progbits(name, ifl, shdr, scn,
27160Sstevel@tonic-gate 				    ndx, ident, ofl) == S_ERROR)
27170Sstevel@tonic-gate 					return (S_ERROR);
27187463SRod.Evans@Sun.COM 				ifl->ifl_isdesc[ndx]->is_flags |= FLG_IS_COMDAT;
27190Sstevel@tonic-gate 				break;
27200Sstevel@tonic-gate 			case SHT_SUNW_verdef:
27219085SAli.Bahrami@Sun.COM 				if (process_section(name, ifl, shdr, scn, ndx,
27229085SAli.Bahrami@Sun.COM 				    ld_targ.t_id.id_null, ofl) == S_ERROR)
27230Sstevel@tonic-gate 					return (S_ERROR);
27240Sstevel@tonic-gate 				vdfisp = ifl->ifl_isdesc[ndx];
27250Sstevel@tonic-gate 				break;
27260Sstevel@tonic-gate 			case SHT_SUNW_verneed:
27279085SAli.Bahrami@Sun.COM 				if (process_section(name, ifl, shdr, scn, ndx,
27289085SAli.Bahrami@Sun.COM 				    ld_targ.t_id.id_null, ofl) == S_ERROR)
27290Sstevel@tonic-gate 					return (S_ERROR);
27300Sstevel@tonic-gate 				vndisp = ifl->ifl_isdesc[ndx];
27310Sstevel@tonic-gate 				break;
27320Sstevel@tonic-gate 			case SHT_SUNW_versym:
27339085SAli.Bahrami@Sun.COM 				if (process_section(name, ifl, shdr, scn, ndx,
27349085SAli.Bahrami@Sun.COM 				    ld_targ.t_id.id_null, ofl) == S_ERROR)
27350Sstevel@tonic-gate 					return (S_ERROR);
27360Sstevel@tonic-gate 				vsyisp = ifl->ifl_isdesc[ndx];
27370Sstevel@tonic-gate 				break;
27380Sstevel@tonic-gate 			case SHT_SPARC_GOTDATA:
27396206Sab196087 				/*
27406206Sab196087 				 * SHT_SPARC_GOTDATA (0x70000000) is in the
27416206Sab196087 				 * SHT_LOPROC - SHT_HIPROC range reserved
27426206Sab196087 				 * for processor-specific semantics. It is
27436206Sab196087 				 * only meaningful for sparc targets.
27446206Sab196087 				 */
27456206Sab196087 				if (ld_targ.t_m.m_mach !=
27466206Sab196087 				    LD_TARG_BYCLASS(EM_SPARC, EM_SPARCV9))
27476206Sab196087 					goto do_default;
27489085SAli.Bahrami@Sun.COM 				if (process_section(name, ifl, shdr, scn, ndx,
27499085SAli.Bahrami@Sun.COM 				    ld_targ.t_id.id_gotdata, ofl) == S_ERROR)
27500Sstevel@tonic-gate 					return (S_ERROR);
27510Sstevel@tonic-gate 				break;
27526206Sab196087 #if	defined(_ELF64)
27530Sstevel@tonic-gate 			case SHT_AMD64_UNWIND:
27546206Sab196087 				/*
27556206Sab196087 				 * SHT_AMD64_UNWIND (0x70000001) is in the
27566206Sab196087 				 * SHT_LOPROC - SHT_HIPROC range reserved
27576206Sab196087 				 * for processor-specific semantics. It is
27586206Sab196087 				 * only meaningful for amd64 targets.
27596206Sab196087 				 */
27606206Sab196087 				if (ld_targ.t_m.m_mach != EM_AMD64)
27616206Sab196087 					goto do_default;
27627463SRod.Evans@Sun.COM 
27636206Sab196087 				/*
27646206Sab196087 				 * Target is x86, so this really is
27656206Sab196087 				 * SHT_AMD64_UNWIND
27666206Sab196087 				 */
27670Sstevel@tonic-gate 				if (column == 0) {
27680Sstevel@tonic-gate 					/*
27690Sstevel@tonic-gate 					 * column == ET_REL
27700Sstevel@tonic-gate 					 */
27717463SRod.Evans@Sun.COM 					if (process_section(name, ifl, shdr,
27727463SRod.Evans@Sun.COM 					    scn, ndx, ld_targ.t_id.id_unwind,
27737463SRod.Evans@Sun.COM 					    ofl) == S_ERROR)
27740Sstevel@tonic-gate 						return (S_ERROR);
27759085SAli.Bahrami@Sun.COM 					ifl->ifl_isdesc[ndx]->is_flags |=
27769085SAli.Bahrami@Sun.COM 					    FLG_IS_EHFRAME;
27770Sstevel@tonic-gate 				}
27780Sstevel@tonic-gate 				break;
27790Sstevel@tonic-gate #endif
27800Sstevel@tonic-gate 			default:
27816206Sab196087 			do_default:
27829085SAli.Bahrami@Sun.COM 				if (process_section(name, ifl, shdr, scn, ndx,
27839085SAli.Bahrami@Sun.COM 				    ((ident == ld_targ.t_id.id_null) ?
27849085SAli.Bahrami@Sun.COM 				    ident : ld_targ.t_id.id_user), ofl) ==
27859085SAli.Bahrami@Sun.COM 				    S_ERROR)
27860Sstevel@tonic-gate 					return (S_ERROR);
27870Sstevel@tonic-gate 				break;
27880Sstevel@tonic-gate 			}
27890Sstevel@tonic-gate 		}
27907463SRod.Evans@Sun.COM 	}
27910Sstevel@tonic-gate 
27927463SRod.Evans@Sun.COM 	/*
27937463SRod.Evans@Sun.COM 	 * Now that all input sections have been analyzed, and prior to placing
27947463SRod.Evans@Sun.COM 	 * any input sections to their output sections, process any groups.
27957463SRod.Evans@Sun.COM 	 * Groups can contribute COMDAT items, which may get discarded as part
27967463SRod.Evans@Sun.COM 	 * of placement.  In addition, COMDAT names may require transformation
27977463SRod.Evans@Sun.COM 	 * to indicate different output section placement.
27987463SRod.Evans@Sun.COM 	 */
27997463SRod.Evans@Sun.COM 	if (ifl->ifl_flags & FLG_IS_GROUPS) {
28007463SRod.Evans@Sun.COM 		for (ndx = 1; ndx < ifl->ifl_shnum; ndx++) {
28017463SRod.Evans@Sun.COM 			Is_desc	*isp;
28027463SRod.Evans@Sun.COM 
28037463SRod.Evans@Sun.COM 			if (((isp = ifl->ifl_isdesc[ndx]) == NULL) ||
28047463SRod.Evans@Sun.COM 			    (isp->is_shdr->sh_type != SHT_GROUP))
28057463SRod.Evans@Sun.COM 				continue;
28067463SRod.Evans@Sun.COM 
28077463SRod.Evans@Sun.COM 			if (ld_group_process(isp, ofl) == S_ERROR)
28087463SRod.Evans@Sun.COM 				return (S_ERROR);
28090Sstevel@tonic-gate 		}
28100Sstevel@tonic-gate 	}
28110Sstevel@tonic-gate 
28120Sstevel@tonic-gate 	/*
28139615SAli.Bahrami@Sun.COM 	 * Now that all of the input sections have been processed, place
28149615SAli.Bahrami@Sun.COM 	 * them in the appropriate output sections.
28150Sstevel@tonic-gate 	 */
28167463SRod.Evans@Sun.COM 	for (ndx = 1; ndx < ifl->ifl_shnum; ndx++) {
28177463SRod.Evans@Sun.COM 		Is_desc	*isp;
28187463SRod.Evans@Sun.COM 
28197463SRod.Evans@Sun.COM 		if (((isp = ifl->ifl_isdesc[ndx]) == NULL) ||
28207463SRod.Evans@Sun.COM 		    ((isp->is_flags & FLG_IS_PLACE) == 0))
28217463SRod.Evans@Sun.COM 			continue;
28227463SRod.Evans@Sun.COM 
28237463SRod.Evans@Sun.COM 		/*
28247463SRod.Evans@Sun.COM 		 * Place all non-ordered sections within their appropriate
28257463SRod.Evans@Sun.COM 		 * output section.
28267463SRod.Evans@Sun.COM 		 */
28279615SAli.Bahrami@Sun.COM 		if ((isp->is_flags & FLG_IS_ORDERED) == 0) {
282811734SAli.Bahrami@Sun.COM 			if (ld_place_section(ofl, isp, path_info,
28299615SAli.Bahrami@Sun.COM 			    isp->is_keyident, NULL) == (Os_desc *)S_ERROR)
28307463SRod.Evans@Sun.COM 				return (S_ERROR);
28319615SAli.Bahrami@Sun.COM 			continue;
28327463SRod.Evans@Sun.COM 		}
28337463SRod.Evans@Sun.COM 
28347463SRod.Evans@Sun.COM 		/*
28359615SAli.Bahrami@Sun.COM 		 * Count the number of ordered sections and retain the first
28369615SAli.Bahrami@Sun.COM 		 * ordered section index. This will be used to optimize the
28379615SAli.Bahrami@Sun.COM 		 * ordered section loop that immediately follows this one.
28387463SRod.Evans@Sun.COM 		 */
28399615SAli.Bahrami@Sun.COM 		ordcnt++;
28409615SAli.Bahrami@Sun.COM 		if (ordndx == 0)
28419615SAli.Bahrami@Sun.COM 			ordndx = ndx;
28429615SAli.Bahrami@Sun.COM 	}
28439615SAli.Bahrami@Sun.COM 
28449615SAli.Bahrami@Sun.COM 	/*
28459615SAli.Bahrami@Sun.COM 	 * Having placed all the non-ordered sections, it is now
28469615SAli.Bahrami@Sun.COM 	 * safe to place SHF_ORDERED/SHF_LINK_ORDER sections.
28479615SAli.Bahrami@Sun.COM 	 */
28489615SAli.Bahrami@Sun.COM 	if (ifl->ifl_flags & FLG_IF_ORDERED) {
28499615SAli.Bahrami@Sun.COM 		for (ndx = ordndx; ndx < ifl->ifl_shnum; ndx++) {
28509615SAli.Bahrami@Sun.COM 			Is_desc	*isp;
28519615SAli.Bahrami@Sun.COM 
28529615SAli.Bahrami@Sun.COM 			if (((isp = ifl->ifl_isdesc[ndx]) == NULL) ||
28539615SAli.Bahrami@Sun.COM 			    ((isp->is_flags &
28549615SAli.Bahrami@Sun.COM 			    (FLG_IS_PLACE | FLG_IS_ORDERED)) !=
28559615SAli.Bahrami@Sun.COM 			    (FLG_IS_PLACE | FLG_IS_ORDERED)))
28569615SAli.Bahrami@Sun.COM 				continue;
28579615SAli.Bahrami@Sun.COM 
28589615SAli.Bahrami@Sun.COM 			/* ld_process_ordered() calls ld_place_section() */
285911734SAli.Bahrami@Sun.COM 			if (ld_process_ordered(ofl, ifl, path_info, ndx) ==
286011734SAli.Bahrami@Sun.COM 			    S_ERROR)
28619615SAli.Bahrami@Sun.COM 				return (S_ERROR);
28629615SAli.Bahrami@Sun.COM 
28639615SAli.Bahrami@Sun.COM 			/* If we've done them all, stop searching */
28649615SAli.Bahrami@Sun.COM 			if (--ordcnt == 0)
28659615SAli.Bahrami@Sun.COM 				break;
28667463SRod.Evans@Sun.COM 		}
28677463SRod.Evans@Sun.COM 	}
28687463SRod.Evans@Sun.COM 
28697463SRod.Evans@Sun.COM 	/*
28709615SAli.Bahrami@Sun.COM 	 * If this is a shared object explicitly specified on the command
28719615SAli.Bahrami@Sun.COM 	 * line (as opposed to being a dependency of such an object),
28729615SAli.Bahrami@Sun.COM 	 * determine if the user has specified a control definition. This
28739615SAli.Bahrami@Sun.COM 	 * descriptor may specify which version definitions can be used
28749615SAli.Bahrami@Sun.COM 	 * from this object. It may also update the dependency to USED and
28759615SAli.Bahrami@Sun.COM 	 * supply an alternative SONAME.
28760Sstevel@tonic-gate 	 */
287710792SRod.Evans@Sun.COM 	sdf = NULL;
28780Sstevel@tonic-gate 	if (column && (ifl->ifl_flags & FLG_IF_NEEDED)) {
28790Sstevel@tonic-gate 		const char	*base;
28800Sstevel@tonic-gate 
28810Sstevel@tonic-gate 		/*
28820Sstevel@tonic-gate 		 * Use the basename of the input file (typically this is the
28830Sstevel@tonic-gate 		 * compilation environment name, ie. libfoo.so).
28840Sstevel@tonic-gate 		 */
28850Sstevel@tonic-gate 		if ((base = strrchr(ifl->ifl_name, '/')) == NULL)
28860Sstevel@tonic-gate 			base = ifl->ifl_name;
28870Sstevel@tonic-gate 		else
28880Sstevel@tonic-gate 			base++;
28890Sstevel@tonic-gate 
28909131SRod.Evans@Sun.COM 		if ((sdf = sdf_find(base, ofl->ofl_socntl)) != NULL) {
28910Sstevel@tonic-gate 			sdf->sdf_file = ifl;
28920Sstevel@tonic-gate 			ifl->ifl_sdfdesc = sdf;
28930Sstevel@tonic-gate 		}
28940Sstevel@tonic-gate 	}
28950Sstevel@tonic-gate 
28960Sstevel@tonic-gate 	/*
289711827SRod.Evans@Sun.COM 	 * Before symbol processing, process any capabilities.  Capabilities
289811827SRod.Evans@Sun.COM 	 * can reference a string table, which is why this processing is
289911827SRod.Evans@Sun.COM 	 * carried out after the initial section processing.  Capabilities,
290011827SRod.Evans@Sun.COM 	 * together with -z symbolcap, can require the conversion of global
290111827SRod.Evans@Sun.COM 	 * symbols to local symbols.
29020Sstevel@tonic-gate 	 */
290311827SRod.Evans@Sun.COM 	if (capisp && (process_cap(ofl, ifl, capisp) == S_ERROR))
290411827SRod.Evans@Sun.COM 		return (S_ERROR);
29050Sstevel@tonic-gate 
29060Sstevel@tonic-gate 	/*
29070Sstevel@tonic-gate 	 * Process any version dependencies.  These will establish shared object
29080Sstevel@tonic-gate 	 * `needed' entries in the same manner as will be generated from the
29090Sstevel@tonic-gate 	 * .dynamic's NEEDED entries.
29100Sstevel@tonic-gate 	 */
2911*13074SAli.Bahrami@Oracle.COM 	if (vndisp && ((ofl->ofl_flags & (FLG_OF_NOUNDEF | FLG_OF_SYMBOLIC)) ||
2912*13074SAli.Bahrami@Oracle.COM 	    OFL_GUIDANCE(ofl, FLG_OFG_NO_DEFS)))
29131618Srie 		if (ld_vers_need_process(vndisp, ifl, ofl) == S_ERROR)
29140Sstevel@tonic-gate 			return (S_ERROR);
29150Sstevel@tonic-gate 
29160Sstevel@tonic-gate 	/*
29170Sstevel@tonic-gate 	 * Before processing any symbol resolution or relocations process any
29180Sstevel@tonic-gate 	 * version sections.
29190Sstevel@tonic-gate 	 */
29200Sstevel@tonic-gate 	if (vsyisp)
2921*13074SAli.Bahrami@Oracle.COM 		(void) ld_vers_sym_process(ofl, vsyisp, ifl);
29220Sstevel@tonic-gate 
29230Sstevel@tonic-gate 	if (ifl->ifl_versym &&
29240Sstevel@tonic-gate 	    (vdfisp || (sdf && (sdf->sdf_flags & FLG_SDF_SELECT))))
29251618Srie 		if (ld_vers_def_process(vdfisp, ifl, ofl) == S_ERROR)
29260Sstevel@tonic-gate 			return (S_ERROR);
29270Sstevel@tonic-gate 
29280Sstevel@tonic-gate 	/*
29290Sstevel@tonic-gate 	 * Having collected the appropriate sections carry out any additional
29300Sstevel@tonic-gate 	 * processing if necessary.
29310Sstevel@tonic-gate 	 */
29320Sstevel@tonic-gate 	for (ndx = 0; ndx < ifl->ifl_shnum; ndx++) {
29337463SRod.Evans@Sun.COM 		Is_desc	*isp;
29340Sstevel@tonic-gate 
293510792SRod.Evans@Sun.COM 		if ((isp = ifl->ifl_isdesc[ndx]) == NULL)
29360Sstevel@tonic-gate 			continue;
29370Sstevel@tonic-gate 		row = isp->is_shdr->sh_type;
29380Sstevel@tonic-gate 
29390Sstevel@tonic-gate 		if ((isp->is_flags & FLG_IS_DISCARD) == 0)
29401618Srie 			ld_sup_section(ofl, isp->is_name, isp->is_shdr, ndx,
29411618Srie 			    isp->is_indata, elf);
29420Sstevel@tonic-gate 
29430Sstevel@tonic-gate 		/*
29449131SRod.Evans@Sun.COM 		 * If this is a SHT_SUNW_move section from a relocatable file,
29459131SRod.Evans@Sun.COM 		 * keep track of the section for later processing.
29460Sstevel@tonic-gate 		 */
29470Sstevel@tonic-gate 		if ((row == SHT_SUNW_move) && (column == 0)) {
29489131SRod.Evans@Sun.COM 			if (aplist_append(&(ofl->ofl_ismove), isp,
29499131SRod.Evans@Sun.COM 			    AL_CNT_OFL_MOVE) == NULL)
29500Sstevel@tonic-gate 				return (S_ERROR);
29510Sstevel@tonic-gate 		}
29520Sstevel@tonic-gate 
29530Sstevel@tonic-gate 		/*
29540Sstevel@tonic-gate 		 * If this is a standard section type process it via the
29550Sstevel@tonic-gate 		 * appropriate action routine.
29560Sstevel@tonic-gate 		 */
29570Sstevel@tonic-gate 		if (row < SHT_NUM) {
29587463SRod.Evans@Sun.COM 			if (Final[row][column] != NULL) {
29597463SRod.Evans@Sun.COM 				if (Final[row][column](isp, ifl,
29607463SRod.Evans@Sun.COM 				    ofl) == S_ERROR)
29610Sstevel@tonic-gate 					return (S_ERROR);
29627463SRod.Evans@Sun.COM 			}
29637463SRod.Evans@Sun.COM #if	defined(_ELF64)
29647463SRod.Evans@Sun.COM 		} else if ((row == SHT_AMD64_UNWIND) && (column == 0)) {
29657463SRod.Evans@Sun.COM 			Os_desc	*osp = isp->is_osdesc;
29667463SRod.Evans@Sun.COM 
29677463SRod.Evans@Sun.COM 			/*
29687463SRod.Evans@Sun.COM 			 * SHT_AMD64_UNWIND (0x70000001) is in the SHT_LOPROC -
29697463SRod.Evans@Sun.COM 			 * SHT_HIPROC range reserved for processor-specific
29707463SRod.Evans@Sun.COM 			 * semantics, and is only meaningful for amd64 targets.
29717463SRod.Evans@Sun.COM 			 *
29727463SRod.Evans@Sun.COM 			 * Only process unwind contents from relocatable
29737463SRod.Evans@Sun.COM 			 * objects.
29747463SRod.Evans@Sun.COM 			 */
29757463SRod.Evans@Sun.COM 			if (osp && (ld_targ.t_m.m_mach == EM_AMD64) &&
29769085SAli.Bahrami@Sun.COM 			    (ld_unwind_register(osp, ofl) == S_ERROR))
29777463SRod.Evans@Sun.COM 				return (S_ERROR);
29787463SRod.Evans@Sun.COM #endif
29790Sstevel@tonic-gate 		}
29800Sstevel@tonic-gate 	}
29810Sstevel@tonic-gate 
29820Sstevel@tonic-gate 	/*
298311827SRod.Evans@Sun.COM 	 * Following symbol processing, if this relocatable object input file
298411827SRod.Evans@Sun.COM 	 * provides symbol capabilities, tag the associated symbols so that
298511827SRod.Evans@Sun.COM 	 * the symbols can be re-assigned to the new capabilities symbol
298611827SRod.Evans@Sun.COM 	 * section that will be created for the output file.
298711827SRod.Evans@Sun.COM 	 */
298811827SRod.Evans@Sun.COM 	if (capinfoisp && (ifl->ifl_ehdr->e_type == ET_REL) &&
298911827SRod.Evans@Sun.COM 	    (process_capinfo(ofl, ifl, capinfoisp) == S_ERROR))
299011827SRod.Evans@Sun.COM 		return (S_ERROR);
299111827SRod.Evans@Sun.COM 
299211827SRod.Evans@Sun.COM 	/*
29930Sstevel@tonic-gate 	 * After processing any symbol resolution, and if this dependency
29940Sstevel@tonic-gate 	 * indicates it contains symbols that can't be directly bound to,
29950Sstevel@tonic-gate 	 * set the symbols appropriately.
29960Sstevel@tonic-gate 	 */
29970Sstevel@tonic-gate 	if (sifisp && ((ifl->ifl_flags & (FLG_IF_NEEDED | FLG_IF_NODIRECT)) ==
29980Sstevel@tonic-gate 	    (FLG_IF_NEEDED | FLG_IF_NODIRECT)))
29991618Srie 		(void) ld_sym_nodirect(sifisp, ifl, ofl);
30000Sstevel@tonic-gate 
30010Sstevel@tonic-gate 	return (1);
30020Sstevel@tonic-gate }
30030Sstevel@tonic-gate 
30040Sstevel@tonic-gate /*
30050Sstevel@tonic-gate  * Process the current input file.  There are basically three types of files
30060Sstevel@tonic-gate  * that come through here:
30070Sstevel@tonic-gate  *
300810792SRod.Evans@Sun.COM  *  -	files explicitly defined on the command line (ie. foo.o or bar.so),
30090Sstevel@tonic-gate  *	in this case only the `name' field is valid.
30100Sstevel@tonic-gate  *
301110792SRod.Evans@Sun.COM  *  -	libraries determined from the -l command line option (ie. -lbar),
30120Sstevel@tonic-gate  *	in this case the `soname' field contains the basename of the located
30130Sstevel@tonic-gate  *	file.
30140Sstevel@tonic-gate  *
30150Sstevel@tonic-gate  * Any shared object specified via the above two conventions must be recorded
30160Sstevel@tonic-gate  * as a needed dependency.
30170Sstevel@tonic-gate  *
301810792SRod.Evans@Sun.COM  *  -	libraries specified as dependencies of those libraries already obtained
30190Sstevel@tonic-gate  *	via the command line (ie. bar.so has a DT_NEEDED entry of fred.so.1),
30200Sstevel@tonic-gate  *	in this case the `soname' field contains either a full pathname (if the
30210Sstevel@tonic-gate  *	needed entry contained a `/'), or the basename of the located file.
30220Sstevel@tonic-gate  *	These libraries are processed to verify symbol binding but are not
30230Sstevel@tonic-gate  *	recorded as dependencies of the output file being generated.
302412254SAli.Bahrami@Oracle.COM  *
302512254SAli.Bahrami@Oracle.COM  * entry:
302612254SAli.Bahrami@Oracle.COM  *	name - File name
302712254SAli.Bahrami@Oracle.COM  *	soname - SONAME for needed sharable library, as described above
302812254SAli.Bahrami@Oracle.COM  *	fd - Open file descriptor
302912254SAli.Bahrami@Oracle.COM  *	elf - Open ELF handle
303012254SAli.Bahrami@Oracle.COM  *	flags - FLG_IF_ flags applicable to file
303112254SAli.Bahrami@Oracle.COM  *	ofl - Output file descriptor
303212254SAli.Bahrami@Oracle.COM  *	rej - Rejection descriptor used to record rejection reason
303312254SAli.Bahrami@Oracle.COM  *	ifl_ret - NULL, or address of pointer to receive reference to
303412254SAli.Bahrami@Oracle.COM  *		resulting input descriptor for file. If ifl_ret is non-NULL,
303512254SAli.Bahrami@Oracle.COM  *		the file cannot be an archive or it will be rejected.
303612254SAli.Bahrami@Oracle.COM  *
303712254SAli.Bahrami@Oracle.COM  * exit:
303812254SAli.Bahrami@Oracle.COM  *	If a error occurs in examining the file, S_ERROR is returned.
303912254SAli.Bahrami@Oracle.COM  *	If the file can be examined, but is not suitable, *rej is updated,
304012254SAli.Bahrami@Oracle.COM  *	and 0 is returned. If the file is acceptable, 1 is returned, and if
304112254SAli.Bahrami@Oracle.COM  *	ifl_ret is non-NULL, *ifl_ret is set to contain the pointer to the
304212254SAli.Bahrami@Oracle.COM  *	resulting input descriptor.
30430Sstevel@tonic-gate  */
304412254SAli.Bahrami@Oracle.COM uintptr_t
ld_process_ifl(const char * name,const char * soname,int fd,Elf * elf,Word flags,Ofl_desc * ofl,Rej_desc * rej,Ifl_desc ** ifl_ret)30451618Srie ld_process_ifl(const char *name, const char *soname, int fd, Elf *elf,
304612254SAli.Bahrami@Oracle.COM     Word flags, Ofl_desc *ofl, Rej_desc *rej, Ifl_desc **ifl_ret)
30470Sstevel@tonic-gate {
30480Sstevel@tonic-gate 	Ifl_desc	*ifl;
30490Sstevel@tonic-gate 	Ehdr		*ehdr;
30500Sstevel@tonic-gate 	uintptr_t	error = 0;
30510Sstevel@tonic-gate 	struct stat	status;
30520Sstevel@tonic-gate 	Ar_desc		*adp;
30530Sstevel@tonic-gate 	Rej_desc	_rej;
30540Sstevel@tonic-gate 
30550Sstevel@tonic-gate 	/*
30560Sstevel@tonic-gate 	 * If this file was not extracted from an archive obtain its device
30570Sstevel@tonic-gate 	 * information.  This will be used to determine if the file has already
30580Sstevel@tonic-gate 	 * been processed (rather than simply comparing filenames, the device
30590Sstevel@tonic-gate 	 * information provides a quicker comparison and detects linked files).
30600Sstevel@tonic-gate 	 */
30618598SRod.Evans@Sun.COM 	if (fd && ((flags & FLG_IF_EXTRACT) == 0))
30620Sstevel@tonic-gate 		(void) fstat(fd, &status);
30630Sstevel@tonic-gate 	else {
30640Sstevel@tonic-gate 		status.st_dev = 0;
30650Sstevel@tonic-gate 		status.st_ino = 0;
30660Sstevel@tonic-gate 	}
30670Sstevel@tonic-gate 
30680Sstevel@tonic-gate 	switch (elf_kind(elf)) {
30690Sstevel@tonic-gate 	case ELF_K_AR:
30700Sstevel@tonic-gate 		/*
307112254SAli.Bahrami@Oracle.COM 		 * If the caller has supplied a non-NULL ifl_ret, then
307212254SAli.Bahrami@Oracle.COM 		 * we cannot process archives, for there will be no
307312254SAli.Bahrami@Oracle.COM 		 * input file descriptor for us to return. In this case,
307412254SAli.Bahrami@Oracle.COM 		 * reject the attempt.
307512254SAli.Bahrami@Oracle.COM 		 */
307612254SAli.Bahrami@Oracle.COM 		if (ifl_ret != NULL) {
307712254SAli.Bahrami@Oracle.COM 			_rej.rej_type = SGS_REJ_ARCHIVE;
307812254SAli.Bahrami@Oracle.COM 			_rej.rej_name = name;
307912254SAli.Bahrami@Oracle.COM 			DBG_CALL(Dbg_file_rejected(ofl->ofl_lml, &_rej,
308012254SAli.Bahrami@Oracle.COM 			    ld_targ.t_m.m_mach));
308112254SAli.Bahrami@Oracle.COM 			if (rej->rej_type == 0) {
308212254SAli.Bahrami@Oracle.COM 				*rej = _rej;
308312254SAli.Bahrami@Oracle.COM 				rej->rej_name = strdup(_rej.rej_name);
308412254SAli.Bahrami@Oracle.COM 			}
308512254SAli.Bahrami@Oracle.COM 			return (0);
308612254SAli.Bahrami@Oracle.COM 		}
308712254SAli.Bahrami@Oracle.COM 
308812254SAli.Bahrami@Oracle.COM 		/*
30890Sstevel@tonic-gate 		 * Determine if we've already come across this archive file.
30900Sstevel@tonic-gate 		 */
30910Sstevel@tonic-gate 		if (!(flags & FLG_IF_EXTRACT)) {
30929131SRod.Evans@Sun.COM 			Aliste	idx;
30939131SRod.Evans@Sun.COM 
30949131SRod.Evans@Sun.COM 			for (APLIST_TRAVERSE(ofl->ofl_ars, idx, adp)) {
30950Sstevel@tonic-gate 				if ((adp->ad_stdev != status.st_dev) ||
30960Sstevel@tonic-gate 				    (adp->ad_stino != status.st_ino))
30970Sstevel@tonic-gate 					continue;
30980Sstevel@tonic-gate 
30990Sstevel@tonic-gate 				/*
31000Sstevel@tonic-gate 				 * We've seen this file before so reuse the
31010Sstevel@tonic-gate 				 * original archive descriptor and discard the
31027359SRod.Evans@Sun.COM 				 * new elf descriptor.  Note that a file
31037359SRod.Evans@Sun.COM 				 * descriptor is unnecessary, as the file is
31047359SRod.Evans@Sun.COM 				 * already available in memory.
31050Sstevel@tonic-gate 				 */
31061618Srie 				DBG_CALL(Dbg_file_reuse(ofl->ofl_lml, name,
31071618Srie 				    adp->ad_name));
31080Sstevel@tonic-gate 				(void) elf_end(elf);
310912254SAli.Bahrami@Oracle.COM 				if (!ld_process_archive(name, -1, adp, ofl))
311012254SAli.Bahrami@Oracle.COM 					return (S_ERROR);
311112254SAli.Bahrami@Oracle.COM 				return (1);
31120Sstevel@tonic-gate 			}
31130Sstevel@tonic-gate 		}
31140Sstevel@tonic-gate 
31150Sstevel@tonic-gate 		/*
31160Sstevel@tonic-gate 		 * As we haven't processed this file before establish a new
31170Sstevel@tonic-gate 		 * archive descriptor.
31180Sstevel@tonic-gate 		 */
31191618Srie 		adp = ld_ar_setup(name, elf, ofl);
312010792SRod.Evans@Sun.COM 		if ((adp == NULL) || (adp == (Ar_desc *)S_ERROR))
312112254SAli.Bahrami@Oracle.COM 			return ((uintptr_t)adp);
31220Sstevel@tonic-gate 		adp->ad_stdev = status.st_dev;
31230Sstevel@tonic-gate 		adp->ad_stino = status.st_ino;
31240Sstevel@tonic-gate 
31251618Srie 		ld_sup_file(ofl, name, ELF_K_AR, flags, elf);
31260Sstevel@tonic-gate 
31277359SRod.Evans@Sun.COM 		/*
31287359SRod.Evans@Sun.COM 		 * Indicate that the ELF descriptor no longer requires a file
31297359SRod.Evans@Sun.COM 		 * descriptor by reading the entire file.  The file is already
31307359SRod.Evans@Sun.COM 		 * read via the initial mmap(2) behind elf_begin(3elf), thus
31317359SRod.Evans@Sun.COM 		 * this operation is effectively a no-op.  However, a side-
31327359SRod.Evans@Sun.COM 		 * effect is that the internal file descriptor, maintained in
31337359SRod.Evans@Sun.COM 		 * the ELF descriptor, is set to -1.  This setting will not
31347359SRod.Evans@Sun.COM 		 * be compared with any file descriptor that is passed to
31357359SRod.Evans@Sun.COM 		 * elf_begin(), should this archive, or one of the archive
31367359SRod.Evans@Sun.COM 		 * members, be processed again from the command line or
31377359SRod.Evans@Sun.COM 		 * because of a -z rescan.
31387359SRod.Evans@Sun.COM 		 */
31397359SRod.Evans@Sun.COM 		if (elf_cntl(elf, ELF_C_FDREAD) == -1) {
3140*13074SAli.Bahrami@Oracle.COM 			ld_eprintf(ofl, ERR_ELF, MSG_INTL(MSG_ELF_CNTL),
31417359SRod.Evans@Sun.COM 			    name);
314212254SAli.Bahrami@Oracle.COM 			return (0);
31437359SRod.Evans@Sun.COM 		}
31447359SRod.Evans@Sun.COM 
314512254SAli.Bahrami@Oracle.COM 		if (!ld_process_archive(name, -1, adp, ofl))
314612254SAli.Bahrami@Oracle.COM 			return (S_ERROR);
314712254SAli.Bahrami@Oracle.COM 		return (1);
31480Sstevel@tonic-gate 
31490Sstevel@tonic-gate 	case ELF_K_ELF:
31500Sstevel@tonic-gate 		/*
31510Sstevel@tonic-gate 		 * Obtain the elf header so that we can determine what type of
31520Sstevel@tonic-gate 		 * elf ELF_K_ELF file this is.
31530Sstevel@tonic-gate 		 */
31540Sstevel@tonic-gate 		if ((ehdr = elf_getehdr(elf)) == NULL) {
31550Sstevel@tonic-gate 			int	_class = gelf_getclass(elf);
31560Sstevel@tonic-gate 
31570Sstevel@tonic-gate 			/*
315812254SAli.Bahrami@Oracle.COM 			 * This can fail for a number of reasons. Typically
315912254SAli.Bahrami@Oracle.COM 			 * the object class is incorrect (ie. user is building
316012254SAli.Bahrami@Oracle.COM 			 * 64-bit but managed to point at 32-bit libraries).
316112254SAli.Bahrami@Oracle.COM 			 * Other ELF errors can include a truncated or corrupt
316212254SAli.Bahrami@Oracle.COM 			 * file. Try to get the best error message possible.
31630Sstevel@tonic-gate 			 */
31646206Sab196087 			if (ld_targ.t_m.m_class != _class) {
31650Sstevel@tonic-gate 				_rej.rej_type = SGS_REJ_CLASS;
31660Sstevel@tonic-gate 				_rej.rej_info = (uint_t)_class;
31670Sstevel@tonic-gate 			} else {
31680Sstevel@tonic-gate 				_rej.rej_type = SGS_REJ_STR;
31690Sstevel@tonic-gate 				_rej.rej_str = elf_errmsg(-1);
31700Sstevel@tonic-gate 			}
31710Sstevel@tonic-gate 			_rej.rej_name = name;
31726206Sab196087 			DBG_CALL(Dbg_file_rejected(ofl->ofl_lml, &_rej,
31736206Sab196087 			    ld_targ.t_m.m_mach));
31740Sstevel@tonic-gate 			if (rej->rej_type == 0) {
31750Sstevel@tonic-gate 				*rej = _rej;
31760Sstevel@tonic-gate 				rej->rej_name = strdup(_rej.rej_name);
31770Sstevel@tonic-gate 			}
317812254SAli.Bahrami@Oracle.COM 			return (0);
31790Sstevel@tonic-gate 		}
31800Sstevel@tonic-gate 
31810Sstevel@tonic-gate 		/*
31820Sstevel@tonic-gate 		 * Determine if we've already come across this file.
31830Sstevel@tonic-gate 		 */
31840Sstevel@tonic-gate 		if (!(flags & FLG_IF_EXTRACT)) {
31859131SRod.Evans@Sun.COM 			APlist	*apl;
31869131SRod.Evans@Sun.COM 			Aliste	idx;
31870Sstevel@tonic-gate 
31880Sstevel@tonic-gate 			if (ehdr->e_type == ET_REL)
31899131SRod.Evans@Sun.COM 				apl = ofl->ofl_objs;
31900Sstevel@tonic-gate 			else
31919131SRod.Evans@Sun.COM 				apl = ofl->ofl_sos;
31920Sstevel@tonic-gate 
31930Sstevel@tonic-gate 			/*
31940Sstevel@tonic-gate 			 * Traverse the appropriate file list and determine if
31950Sstevel@tonic-gate 			 * a dev/inode match is found.
31960Sstevel@tonic-gate 			 */
31979131SRod.Evans@Sun.COM 			for (APLIST_TRAVERSE(apl, idx, ifl)) {
31980Sstevel@tonic-gate 				/*
31990Sstevel@tonic-gate 				 * Ifl_desc generated via -Nneed, therefore no
32000Sstevel@tonic-gate 				 * actual file behind it.
32010Sstevel@tonic-gate 				 */
32020Sstevel@tonic-gate 				if (ifl->ifl_flags & FLG_IF_NEEDSTR)
32030Sstevel@tonic-gate 					continue;
32040Sstevel@tonic-gate 
32050Sstevel@tonic-gate 				if ((ifl->ifl_stino != status.st_ino) ||
32060Sstevel@tonic-gate 				    (ifl->ifl_stdev != status.st_dev))
32070Sstevel@tonic-gate 					continue;
32080Sstevel@tonic-gate 
32090Sstevel@tonic-gate 				/*
32100Sstevel@tonic-gate 				 * Disregard (skip) this image.
32110Sstevel@tonic-gate 				 */
32121618Srie 				DBG_CALL(Dbg_file_skip(ofl->ofl_lml,
32131618Srie 				    ifl->ifl_name, name));
32140Sstevel@tonic-gate 				(void) elf_end(elf);
32150Sstevel@tonic-gate 
32160Sstevel@tonic-gate 				/*
32170Sstevel@tonic-gate 				 * If the file was explicitly defined on the
32180Sstevel@tonic-gate 				 * command line (this is always the case for
32190Sstevel@tonic-gate 				 * relocatable objects, and is true for shared
32200Sstevel@tonic-gate 				 * objects when they weren't specified via -l or
32210Sstevel@tonic-gate 				 * were dragged in as an implicit dependency),
32220Sstevel@tonic-gate 				 * then warn the user.
32230Sstevel@tonic-gate 				 */
32240Sstevel@tonic-gate 				if ((flags & FLG_IF_CMDLINE) ||
32250Sstevel@tonic-gate 				    (ifl->ifl_flags & FLG_IF_CMDLINE)) {
32260Sstevel@tonic-gate 					const char	*errmsg;
32270Sstevel@tonic-gate 
32280Sstevel@tonic-gate 					/*
32290Sstevel@tonic-gate 					 * Determine whether this is the same
32300Sstevel@tonic-gate 					 * file name as originally encountered
32310Sstevel@tonic-gate 					 * so as to provide the most
32320Sstevel@tonic-gate 					 * descriptive diagnostic.
32330Sstevel@tonic-gate 					 */
32344734Sab196087 					errmsg =
32354734Sab196087 					    (strcmp(name, ifl->ifl_name) == 0) ?
32364734Sab196087 					    MSG_INTL(MSG_FIL_MULINC_1) :
32374734Sab196087 					    MSG_INTL(MSG_FIL_MULINC_2);
3238*13074SAli.Bahrami@Oracle.COM 					ld_eprintf(ofl, ERR_WARNING,
32391618Srie 					    errmsg, name, ifl->ifl_name);
32400Sstevel@tonic-gate 				}
324112254SAli.Bahrami@Oracle.COM 				if (ifl_ret)
324212254SAli.Bahrami@Oracle.COM 					*ifl_ret = ifl;
324312254SAli.Bahrami@Oracle.COM 				return (1);
32440Sstevel@tonic-gate 			}
32450Sstevel@tonic-gate 		}
32460Sstevel@tonic-gate 
32470Sstevel@tonic-gate 		/*
32480Sstevel@tonic-gate 		 * At this point, we know we need the file.  Establish an input
32490Sstevel@tonic-gate 		 * file descriptor and continue processing.
32500Sstevel@tonic-gate 		 */
32510Sstevel@tonic-gate 		ifl = ifl_setup(name, ehdr, elf, flags, ofl, rej);
325210792SRod.Evans@Sun.COM 		if ((ifl == NULL) || (ifl == (Ifl_desc *)S_ERROR))
325312254SAli.Bahrami@Oracle.COM 			return ((uintptr_t)ifl);
32540Sstevel@tonic-gate 		ifl->ifl_stdev = status.st_dev;
32550Sstevel@tonic-gate 		ifl->ifl_stino = status.st_ino;
32560Sstevel@tonic-gate 
32570Sstevel@tonic-gate 		/*
32580Sstevel@tonic-gate 		 * If -zignore is in effect, mark this file as a potential
32590Sstevel@tonic-gate 		 * candidate (the files use isn't actually determined until
32600Sstevel@tonic-gate 		 * symbol resolution and relocation processing are completed).
32610Sstevel@tonic-gate 		 */
32620Sstevel@tonic-gate 		if (ofl->ofl_flags1 & FLG_OF1_IGNORE)
32630Sstevel@tonic-gate 			ifl->ifl_flags |= FLG_IF_IGNORE;
32640Sstevel@tonic-gate 
32650Sstevel@tonic-gate 		switch (ehdr->e_type) {
32660Sstevel@tonic-gate 		case ET_REL:
32676206Sab196087 			(*ld_targ.t_mr.mr_mach_eflags)(ehdr, ofl);
32680Sstevel@tonic-gate 			error = process_elf(ifl, elf, ofl);
32690Sstevel@tonic-gate 			break;
32700Sstevel@tonic-gate 		case ET_DYN:
32710Sstevel@tonic-gate 			if ((ofl->ofl_flags & FLG_OF_STATIC) ||
32720Sstevel@tonic-gate 			    !(ofl->ofl_flags & FLG_OF_DYNLIBS)) {
3273*13074SAli.Bahrami@Oracle.COM 				ld_eprintf(ofl, ERR_FATAL,
32741618Srie 				    MSG_INTL(MSG_FIL_SOINSTAT), name);
327512254SAli.Bahrami@Oracle.COM 				return (0);
32760Sstevel@tonic-gate 			}
32770Sstevel@tonic-gate 
32780Sstevel@tonic-gate 			/*
32790Sstevel@tonic-gate 			 * Record any additional shared object information.
32800Sstevel@tonic-gate 			 * If no soname is specified (eg. this file was
32810Sstevel@tonic-gate 			 * derived from a explicit filename declaration on the
32820Sstevel@tonic-gate 			 * command line, ie. bar.so) use the pathname.
32830Sstevel@tonic-gate 			 * This entry may be overridden if the files dynamic
32840Sstevel@tonic-gate 			 * section specifies an DT_SONAME value.
32850Sstevel@tonic-gate 			 */
32860Sstevel@tonic-gate 			if (soname == NULL)
32870Sstevel@tonic-gate 				ifl->ifl_soname = ifl->ifl_name;
32880Sstevel@tonic-gate 			else
32890Sstevel@tonic-gate 				ifl->ifl_soname = soname;
32900Sstevel@tonic-gate 
32910Sstevel@tonic-gate 			/*
329212449SRod.Evans@Sun.COM 			 * If direct bindings, lazy loading, group permissions,
329312449SRod.Evans@Sun.COM 			 * or deferred dependencies need to be established, mark
329412449SRod.Evans@Sun.COM 			 * this object.
32950Sstevel@tonic-gate 			 */
32960Sstevel@tonic-gate 			if (ofl->ofl_flags1 & FLG_OF1_ZDIRECT)
32970Sstevel@tonic-gate 				ifl->ifl_flags |= FLG_IF_DIRECT;
32980Sstevel@tonic-gate 			if (ofl->ofl_flags1 & FLG_OF1_LAZYLD)
32990Sstevel@tonic-gate 				ifl->ifl_flags |= FLG_IF_LAZYLD;
33000Sstevel@tonic-gate 			if (ofl->ofl_flags1 & FLG_OF1_GRPPRM)
33010Sstevel@tonic-gate 				ifl->ifl_flags |= FLG_IF_GRPPRM;
330212449SRod.Evans@Sun.COM 			if (ofl->ofl_flags1 & FLG_OF1_DEFERRED)
330312449SRod.Evans@Sun.COM 				ifl->ifl_flags |=
330412449SRod.Evans@Sun.COM 				    (FLG_IF_LAZYLD | FLG_IF_DEFERRED);
330512449SRod.Evans@Sun.COM 
33060Sstevel@tonic-gate 			error = process_elf(ifl, elf, ofl);
33070Sstevel@tonic-gate 
33080Sstevel@tonic-gate 			/*
330912449SRod.Evans@Sun.COM 			 * Determine whether this dependency requires a syminfo.
33100Sstevel@tonic-gate 			 */
331112449SRod.Evans@Sun.COM 			if (ifl->ifl_flags & MSK_IF_SYMINFO)
33120Sstevel@tonic-gate 				ofl->ofl_flags |= FLG_OF_SYMINFO;
33130Sstevel@tonic-gate 
3314*13074SAli.Bahrami@Oracle.COM 			/*
3315*13074SAli.Bahrami@Oracle.COM 			 * Guidance: Use -z lazyload/nolazyload.
3316*13074SAli.Bahrami@Oracle.COM 			 * libc is exempt from this advice, because it cannot
3317*13074SAli.Bahrami@Oracle.COM 			 * be lazy loaded, and requests to do so are ignored.
3318*13074SAli.Bahrami@Oracle.COM 			 */
3319*13074SAli.Bahrami@Oracle.COM 			if (OFL_GUIDANCE(ofl, FLG_OFG_NO_LAZY) &&
3320*13074SAli.Bahrami@Oracle.COM 			    ((ifl->ifl_flags & FLG_IF_RTLDINF) == 0)) {
3321*13074SAli.Bahrami@Oracle.COM 				ld_eprintf(ofl, ERR_GUIDANCE,
3322*13074SAli.Bahrami@Oracle.COM 				    MSG_INTL(MSG_GUIDE_LAZYLOAD));
3323*13074SAli.Bahrami@Oracle.COM 				ofl->ofl_guideflags |= FLG_OFG_NO_LAZY;
3324*13074SAli.Bahrami@Oracle.COM 			}
3325*13074SAli.Bahrami@Oracle.COM 
3326*13074SAli.Bahrami@Oracle.COM 			/*
3327*13074SAli.Bahrami@Oracle.COM 			 * Guidance: Use -B direct/nodirect or
3328*13074SAli.Bahrami@Oracle.COM 			 * -z direct/nodirect.
3329*13074SAli.Bahrami@Oracle.COM 			 */
3330*13074SAli.Bahrami@Oracle.COM 			if (OFL_GUIDANCE(ofl, FLG_OFG_NO_DB)) {
3331*13074SAli.Bahrami@Oracle.COM 				ld_eprintf(ofl, ERR_GUIDANCE,
3332*13074SAli.Bahrami@Oracle.COM 				    MSG_INTL(MSG_GUIDE_DIRECT));
3333*13074SAli.Bahrami@Oracle.COM 				ofl->ofl_guideflags |= FLG_OFG_NO_DB;
3334*13074SAli.Bahrami@Oracle.COM 			}
3335*13074SAli.Bahrami@Oracle.COM 
33360Sstevel@tonic-gate 			break;
33370Sstevel@tonic-gate 		default:
33380Sstevel@tonic-gate 			(void) elf_errno();
33390Sstevel@tonic-gate 			_rej.rej_type = SGS_REJ_UNKFILE;
33400Sstevel@tonic-gate 			_rej.rej_name = name;
33416206Sab196087 			DBG_CALL(Dbg_file_rejected(ofl->ofl_lml, &_rej,
33426206Sab196087 			    ld_targ.t_m.m_mach));
33430Sstevel@tonic-gate 			if (rej->rej_type == 0) {
33440Sstevel@tonic-gate 				*rej = _rej;
33450Sstevel@tonic-gate 				rej->rej_name = strdup(_rej.rej_name);
33460Sstevel@tonic-gate 			}
334712254SAli.Bahrami@Oracle.COM 			return (0);
33480Sstevel@tonic-gate 		}
33490Sstevel@tonic-gate 		break;
33500Sstevel@tonic-gate 	default:
33510Sstevel@tonic-gate 		(void) elf_errno();
33520Sstevel@tonic-gate 		_rej.rej_type = SGS_REJ_UNKFILE;
33530Sstevel@tonic-gate 		_rej.rej_name = name;
33546206Sab196087 		DBG_CALL(Dbg_file_rejected(ofl->ofl_lml, &_rej,
33556206Sab196087 		    ld_targ.t_m.m_mach));
33560Sstevel@tonic-gate 		if (rej->rej_type == 0) {
33570Sstevel@tonic-gate 			*rej = _rej;
33580Sstevel@tonic-gate 			rej->rej_name = strdup(_rej.rej_name);
33590Sstevel@tonic-gate 		}
336012254SAli.Bahrami@Oracle.COM 		return (0);
33610Sstevel@tonic-gate 	}
33620Sstevel@tonic-gate 	if ((error == 0) || (error == S_ERROR))
336312254SAli.Bahrami@Oracle.COM 		return (error);
336412254SAli.Bahrami@Oracle.COM 
336512254SAli.Bahrami@Oracle.COM 	if (ifl_ret)
336612254SAli.Bahrami@Oracle.COM 		*ifl_ret = ifl;
336712254SAli.Bahrami@Oracle.COM 	return (1);
33680Sstevel@tonic-gate }
33690Sstevel@tonic-gate 
33700Sstevel@tonic-gate /*
33710Sstevel@tonic-gate  * Having successfully opened a file, set up the necessary elf structures to
33720Sstevel@tonic-gate  * process it further.  This small section of processing is slightly different
33730Sstevel@tonic-gate  * from the elf initialization required to process a relocatable object from an
33742850Srie  * archive (see libs.c: ld_process_archive()).
33750Sstevel@tonic-gate  */
337612254SAli.Bahrami@Oracle.COM uintptr_t
ld_process_open(const char * opath,const char * ofile,int * fd,Ofl_desc * ofl,Word flags,Rej_desc * rej,Ifl_desc ** ifl_ret)33772978Srie ld_process_open(const char *opath, const char *ofile, int *fd, Ofl_desc *ofl,
337812254SAli.Bahrami@Oracle.COM     Word flags, Rej_desc *rej, Ifl_desc **ifl_ret)
33790Sstevel@tonic-gate {
33802978Srie 	Elf		*elf;
33812978Srie 	const char	*npath = opath;
33822978Srie 	const char	*nfile = ofile;
33830Sstevel@tonic-gate 
33842978Srie 	if ((elf = elf_begin(*fd, ELF_C_READ, NULL)) == NULL) {
3385*13074SAli.Bahrami@Oracle.COM 		ld_eprintf(ofl, ERR_ELF, MSG_INTL(MSG_ELF_BEGIN), npath);
338612254SAli.Bahrami@Oracle.COM 		return (0);
33870Sstevel@tonic-gate 	}
33880Sstevel@tonic-gate 
33892978Srie 	/*
33902978Srie 	 * Determine whether the support library wishes to process this open.
33912978Srie 	 * The support library may return:
33922978Srie 	 *   .	a different ELF descriptor (in which case they should have
33932978Srie 	 *	closed the original)
33942978Srie 	 *   .	a different file descriptor (in which case they should have
33952978Srie 	 *	closed the original)
33962978Srie 	 *   .	a different path and file name (presumably associated with
33972978Srie 	 *	a different file descriptor)
33982978Srie 	 *
33992978Srie 	 * A file descriptor of -1, or and ELF descriptor of zero indicates
34002978Srie 	 * the file should be ignored.
34012978Srie 	 */
34022978Srie 	ld_sup_open(ofl, &npath, &nfile, fd, flags, &elf, NULL, 0,
34032978Srie 	    elf_kind(elf));
34042978Srie 
34052978Srie 	if ((*fd == -1) || (elf == NULL))
340612254SAli.Bahrami@Oracle.COM 		return (0);
340712254SAli.Bahrami@Oracle.COM 
340812254SAli.Bahrami@Oracle.COM 	return (ld_process_ifl(npath, nfile, *fd, elf, flags, ofl, rej,
340912254SAli.Bahrami@Oracle.COM 	    ifl_ret));
34100Sstevel@tonic-gate }
34110Sstevel@tonic-gate 
34120Sstevel@tonic-gate /*
34138598SRod.Evans@Sun.COM  * Having successfully mapped a file, set up the necessary elf structures to
34148598SRod.Evans@Sun.COM  * process it further.  This routine is patterned after ld_process_open() and
34158598SRod.Evans@Sun.COM  * is only called by ld.so.1(1) to process a relocatable object.
34168598SRod.Evans@Sun.COM  */
34178598SRod.Evans@Sun.COM Ifl_desc *
ld_process_mem(const char * path,const char * file,char * addr,size_t size,Ofl_desc * ofl,Rej_desc * rej)34188598SRod.Evans@Sun.COM ld_process_mem(const char *path, const char *file, char *addr, size_t size,
34198598SRod.Evans@Sun.COM     Ofl_desc *ofl, Rej_desc *rej)
34208598SRod.Evans@Sun.COM {
342112254SAli.Bahrami@Oracle.COM 	Elf		*elf;
342212254SAli.Bahrami@Oracle.COM 	uintptr_t	open_ret;
342312254SAli.Bahrami@Oracle.COM 	Ifl_desc	*ifl;
34248598SRod.Evans@Sun.COM 
34258598SRod.Evans@Sun.COM 	if ((elf = elf_memory(addr, size)) == NULL) {
3426*13074SAli.Bahrami@Oracle.COM 		ld_eprintf(ofl, ERR_ELF, MSG_INTL(MSG_ELF_MEMORY), path);
34278598SRod.Evans@Sun.COM 		return (0);
34288598SRod.Evans@Sun.COM 	}
34298598SRod.Evans@Sun.COM 
343012254SAli.Bahrami@Oracle.COM 	open_ret = ld_process_ifl(path, file, 0, elf, 0, ofl, rej, &ifl);
343112254SAli.Bahrami@Oracle.COM 	if (open_ret != 1)
343212254SAli.Bahrami@Oracle.COM 		return ((Ifl_desc *) open_ret);
343312254SAli.Bahrami@Oracle.COM 	return (ifl);
34348598SRod.Evans@Sun.COM }
34358598SRod.Evans@Sun.COM 
34368598SRod.Evans@Sun.COM /*
34370Sstevel@tonic-gate  * Process a required library (i.e. the dependency of a shared object).
34380Sstevel@tonic-gate  * Combine the directory and filename, check the resultant path size, and try
34390Sstevel@tonic-gate  * opening the pathname.
34400Sstevel@tonic-gate  */
34411618Srie static Ifl_desc *
process_req_lib(Sdf_desc * sdf,const char * dir,const char * file,Ofl_desc * ofl,Rej_desc * rej)34420Sstevel@tonic-gate process_req_lib(Sdf_desc *sdf, const char *dir, const char *file,
34437463SRod.Evans@Sun.COM     Ofl_desc *ofl, Rej_desc *rej)
34440Sstevel@tonic-gate {
34450Sstevel@tonic-gate 	size_t		dlen, plen;
34460Sstevel@tonic-gate 	int		fd;
34470Sstevel@tonic-gate 	char		path[PATH_MAX];
34480Sstevel@tonic-gate 	const char	*_dir = dir;
34490Sstevel@tonic-gate 
34500Sstevel@tonic-gate 	/*
34510Sstevel@tonic-gate 	 * Determine the sizes of the directory and filename to insure we don't
34520Sstevel@tonic-gate 	 * exceed our buffer.
34530Sstevel@tonic-gate 	 */
34540Sstevel@tonic-gate 	if ((dlen = strlen(dir)) == 0) {
34550Sstevel@tonic-gate 		_dir = MSG_ORIG(MSG_STR_DOT);
34560Sstevel@tonic-gate 		dlen = 1;
34570Sstevel@tonic-gate 	}
34580Sstevel@tonic-gate 	dlen++;
34590Sstevel@tonic-gate 	plen = dlen + strlen(file) + 1;
34600Sstevel@tonic-gate 	if (plen > PATH_MAX) {
3461*13074SAli.Bahrami@Oracle.COM 		ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_FIL_PTHTOLONG),
34621618Srie 		    _dir, file);
34630Sstevel@tonic-gate 		return (0);
34640Sstevel@tonic-gate 	}
34650Sstevel@tonic-gate 
34660Sstevel@tonic-gate 	/*
34670Sstevel@tonic-gate 	 * Build the entire pathname and try and open the file.
34680Sstevel@tonic-gate 	 */
34690Sstevel@tonic-gate 	(void) strcpy(path, _dir);
34700Sstevel@tonic-gate 	(void) strcat(path, MSG_ORIG(MSG_STR_SLASH));
34710Sstevel@tonic-gate 	(void) strcat(path, file);
34721618Srie 	DBG_CALL(Dbg_libs_req(ofl->ofl_lml, sdf->sdf_name,
34731618Srie 	    sdf->sdf_rfile, path));
34740Sstevel@tonic-gate 
34750Sstevel@tonic-gate 	if ((fd = open(path, O_RDONLY)) == -1)
34760Sstevel@tonic-gate 		return (0);
34770Sstevel@tonic-gate 	else {
347812254SAli.Bahrami@Oracle.COM 		uintptr_t	open_ret;
34790Sstevel@tonic-gate 		Ifl_desc	*ifl;
34800Sstevel@tonic-gate 		char		*_path;
34810Sstevel@tonic-gate 
348210792SRod.Evans@Sun.COM 		if ((_path = libld_malloc(strlen(path) + 1)) == NULL)
34830Sstevel@tonic-gate 			return ((Ifl_desc *)S_ERROR);
34840Sstevel@tonic-gate 		(void) strcpy(_path, path);
348512254SAli.Bahrami@Oracle.COM 		open_ret = ld_process_open(_path, &_path[dlen], &fd, ofl,
348612254SAli.Bahrami@Oracle.COM 		    0, rej, &ifl);
34872978Srie 		if (fd != -1)
34882978Srie 			(void) close(fd);
348912254SAli.Bahrami@Oracle.COM 		if (open_ret != 1)
349012254SAli.Bahrami@Oracle.COM 			return ((Ifl_desc *)open_ret);
34910Sstevel@tonic-gate 		return (ifl);
34920Sstevel@tonic-gate 	}
34930Sstevel@tonic-gate }
34940Sstevel@tonic-gate 
34950Sstevel@tonic-gate /*
34960Sstevel@tonic-gate  * Finish any library processing.  Walk the list of so's that have been listed
34970Sstevel@tonic-gate  * as "included" by shared objects we have previously processed.  Examine them,
34980Sstevel@tonic-gate  * without adding them as explicit dependents of this program, in order to
34990Sstevel@tonic-gate  * complete our symbol definition process.  The search path rules are:
35000Sstevel@tonic-gate  *
350110792SRod.Evans@Sun.COM  *  -	use any user supplied paths, i.e. LD_LIBRARY_PATH and -L, then
35020Sstevel@tonic-gate  *
350310792SRod.Evans@Sun.COM  *  -	use any RPATH defined within the parent shared object, then
35040Sstevel@tonic-gate  *
350510792SRod.Evans@Sun.COM  *  -	use the default directories, i.e. LIBPATH or -YP.
35060Sstevel@tonic-gate  */
35070Sstevel@tonic-gate uintptr_t
ld_finish_libs(Ofl_desc * ofl)35081618Srie ld_finish_libs(Ofl_desc *ofl)
35090Sstevel@tonic-gate {
35109131SRod.Evans@Sun.COM 	Aliste		idx1;
35110Sstevel@tonic-gate 	Sdf_desc	*sdf;
35120Sstevel@tonic-gate 	Rej_desc	rej = { 0 };
35130Sstevel@tonic-gate 
35140Sstevel@tonic-gate 	/*
35150Sstevel@tonic-gate 	 * Make sure we are back in dynamic mode.
35160Sstevel@tonic-gate 	 */
35170Sstevel@tonic-gate 	ofl->ofl_flags |= FLG_OF_DYNLIBS;
35180Sstevel@tonic-gate 
35199131SRod.Evans@Sun.COM 	for (APLIST_TRAVERSE(ofl->ofl_soneed, idx1, sdf)) {
35209131SRod.Evans@Sun.COM 		Aliste		idx2;
35212850Srie 		char		*path, *slash = NULL;
35220Sstevel@tonic-gate 		int		fd;
35230Sstevel@tonic-gate 		Ifl_desc	*ifl;
35242850Srie 		char		*file = (char *)sdf->sdf_name;
35250Sstevel@tonic-gate 
35260Sstevel@tonic-gate 		/*
35270Sstevel@tonic-gate 		 * See if this file has already been processed.  At the time
35280Sstevel@tonic-gate 		 * this implicit dependency was determined there may still have
35291109Srie 		 * been more explicit dependencies to process.  Note, if we ever
35300Sstevel@tonic-gate 		 * do parse the command line three times we would be able to
35311109Srie 		 * do all this checking when processing the dynamic section.
35320Sstevel@tonic-gate 		 */
35330Sstevel@tonic-gate 		if (sdf->sdf_file)
35340Sstevel@tonic-gate 			continue;
35350Sstevel@tonic-gate 
35369131SRod.Evans@Sun.COM 		for (APLIST_TRAVERSE(ofl->ofl_sos, idx2, ifl)) {
35370Sstevel@tonic-gate 			if (!(ifl->ifl_flags & FLG_IF_NEEDSTR) &&
35380Sstevel@tonic-gate 			    (strcmp(file, ifl->ifl_soname) == 0)) {
35390Sstevel@tonic-gate 				sdf->sdf_file = ifl;
35400Sstevel@tonic-gate 				break;
35410Sstevel@tonic-gate 			}
35420Sstevel@tonic-gate 		}
35430Sstevel@tonic-gate 		if (sdf->sdf_file)
35440Sstevel@tonic-gate 			continue;
35450Sstevel@tonic-gate 
35460Sstevel@tonic-gate 		/*
35472850Srie 		 * If the current path name element embeds a "/", then it's to
35482850Srie 		 * be taken "as is", with no searching involved.  Process all
35492850Srie 		 * "/" occurrences, so that we can deduce the base file name.
35500Sstevel@tonic-gate 		 */
35512850Srie 		for (path = file; *path; path++) {
35520Sstevel@tonic-gate 			if (*path == '/')
35532850Srie 				slash = path;
35542850Srie 		}
35552850Srie 		if (slash) {
35561618Srie 			DBG_CALL(Dbg_libs_req(ofl->ofl_lml, sdf->sdf_name,
35571618Srie 			    sdf->sdf_rfile, file));
35580Sstevel@tonic-gate 			if ((fd = open(file, O_RDONLY)) == -1) {
3559*13074SAli.Bahrami@Oracle.COM 				ld_eprintf(ofl, ERR_WARNING,
35601618Srie 				    MSG_INTL(MSG_FIL_NOTFOUND), file,
35611618Srie 				    sdf->sdf_rfile);
35620Sstevel@tonic-gate 			} else {
356312254SAli.Bahrami@Oracle.COM 				uintptr_t	open_ret;
35640Sstevel@tonic-gate 				Rej_desc	_rej = { 0 };
35650Sstevel@tonic-gate 
3566*13074SAli.Bahrami@Oracle.COM 				open_ret = ld_process_open(file, ++slash,
3567*13074SAli.Bahrami@Oracle.COM 				    &fd, ofl, 0, &_rej, &ifl);
35682978Srie 				if (fd != -1)
35692978Srie 					(void) close(fd);
357012254SAli.Bahrami@Oracle.COM 				if (open_ret == S_ERROR)
35717359SRod.Evans@Sun.COM 					return (S_ERROR);
35720Sstevel@tonic-gate 
35730Sstevel@tonic-gate 				if (_rej.rej_type) {
35744734Sab196087 					Conv_reject_desc_buf_t rej_buf;
35754734Sab196087 
3576*13074SAli.Bahrami@Oracle.COM 					ld_eprintf(ofl, ERR_WARNING,
35770Sstevel@tonic-gate 					    MSG_INTL(reject[_rej.rej_type]),
35780Sstevel@tonic-gate 					    _rej.rej_name ? rej.rej_name :
35790Sstevel@tonic-gate 					    MSG_INTL(MSG_STR_UNKNOWN),
35806206Sab196087 					    conv_reject_desc(&_rej, &rej_buf,
35816206Sab196087 					    ld_targ.t_m.m_mach));
35820Sstevel@tonic-gate 				} else
35830Sstevel@tonic-gate 					sdf->sdf_file = ifl;
35840Sstevel@tonic-gate 			}
35850Sstevel@tonic-gate 			continue;
35860Sstevel@tonic-gate 		}
35870Sstevel@tonic-gate 
35880Sstevel@tonic-gate 		/*
35890Sstevel@tonic-gate 		 * Now search for this file in any user defined directories.
35900Sstevel@tonic-gate 		 */
35919131SRod.Evans@Sun.COM 		for (APLIST_TRAVERSE(ofl->ofl_ulibdirs, idx2, path)) {
35920Sstevel@tonic-gate 			Rej_desc	_rej = { 0 };
35930Sstevel@tonic-gate 
35940Sstevel@tonic-gate 			ifl = process_req_lib(sdf, path, file, ofl, &_rej);
35950Sstevel@tonic-gate 			if (ifl == (Ifl_desc *)S_ERROR) {
35960Sstevel@tonic-gate 				return (S_ERROR);
35970Sstevel@tonic-gate 			}
35980Sstevel@tonic-gate 			if (_rej.rej_type) {
35990Sstevel@tonic-gate 				if (rej.rej_type == 0) {
36000Sstevel@tonic-gate 					rej = _rej;
36010Sstevel@tonic-gate 					rej.rej_name = strdup(_rej.rej_name);
36020Sstevel@tonic-gate 				}
36030Sstevel@tonic-gate 			}
36040Sstevel@tonic-gate 			if (ifl) {
36050Sstevel@tonic-gate 				sdf->sdf_file = ifl;
36060Sstevel@tonic-gate 				break;
36070Sstevel@tonic-gate 			}
36080Sstevel@tonic-gate 		}
36090Sstevel@tonic-gate 		if (sdf->sdf_file)
36100Sstevel@tonic-gate 			continue;
36110Sstevel@tonic-gate 
36120Sstevel@tonic-gate 		/*
36130Sstevel@tonic-gate 		 * Next use the local rules defined within the parent shared
36140Sstevel@tonic-gate 		 * object.
36150Sstevel@tonic-gate 		 */
36160Sstevel@tonic-gate 		if (sdf->sdf_rpath != NULL) {
36170Sstevel@tonic-gate 			char	*rpath, *next;
36180Sstevel@tonic-gate 
36190Sstevel@tonic-gate 			rpath = libld_malloc(strlen(sdf->sdf_rpath) + 1);
362010792SRod.Evans@Sun.COM 			if (rpath == NULL)
36210Sstevel@tonic-gate 				return (S_ERROR);
36220Sstevel@tonic-gate 			(void) strcpy(rpath, sdf->sdf_rpath);
36231618Srie 			DBG_CALL(Dbg_libs_path(ofl->ofl_lml, rpath,
36241618Srie 			    LA_SER_RUNPATH, sdf->sdf_rfile));
36250Sstevel@tonic-gate 			if ((path = strtok_r(rpath,
36260Sstevel@tonic-gate 			    MSG_ORIG(MSG_STR_COLON), &next)) != NULL) {
36270Sstevel@tonic-gate 				do {
36280Sstevel@tonic-gate 					Rej_desc	_rej = { 0 };
36290Sstevel@tonic-gate 
36300Sstevel@tonic-gate 					path = expand(sdf->sdf_rfile, path,
36310Sstevel@tonic-gate 					    &next);
36320Sstevel@tonic-gate 
36330Sstevel@tonic-gate 					ifl = process_req_lib(sdf, path,
36344716Sab196087 					    file, ofl, &_rej);
36350Sstevel@tonic-gate 					if (ifl == (Ifl_desc *)S_ERROR) {
36360Sstevel@tonic-gate 						return (S_ERROR);
36370Sstevel@tonic-gate 					}
36384716Sab196087 					if ((_rej.rej_type) &&
36394716Sab196087 					    (rej.rej_type == 0)) {
36404716Sab196087 						rej = _rej;
36414716Sab196087 						rej.rej_name =
36424716Sab196087 						    strdup(_rej.rej_name);
36430Sstevel@tonic-gate 					}
36440Sstevel@tonic-gate 					if (ifl) {
36450Sstevel@tonic-gate 						sdf->sdf_file = ifl;
36460Sstevel@tonic-gate 						break;
36470Sstevel@tonic-gate 					}
36480Sstevel@tonic-gate 				} while ((path = strtok_r(NULL,
36490Sstevel@tonic-gate 				    MSG_ORIG(MSG_STR_COLON), &next)) != NULL);
36500Sstevel@tonic-gate 			}
36510Sstevel@tonic-gate 		}
36520Sstevel@tonic-gate 		if (sdf->sdf_file)
36530Sstevel@tonic-gate 			continue;
36540Sstevel@tonic-gate 
36550Sstevel@tonic-gate 		/*
36560Sstevel@tonic-gate 		 * Finally try the default library search directories.
36570Sstevel@tonic-gate 		 */
36589131SRod.Evans@Sun.COM 		for (APLIST_TRAVERSE(ofl->ofl_dlibdirs, idx2, path)) {
36590Sstevel@tonic-gate 			Rej_desc	_rej = { 0 };
36600Sstevel@tonic-gate 
36610Sstevel@tonic-gate 			ifl = process_req_lib(sdf, path, file, ofl, &rej);
36620Sstevel@tonic-gate 			if (ifl == (Ifl_desc *)S_ERROR) {
36630Sstevel@tonic-gate 				return (S_ERROR);
36640Sstevel@tonic-gate 			}
36650Sstevel@tonic-gate 			if (_rej.rej_type) {
36660Sstevel@tonic-gate 				if (rej.rej_type == 0) {
36670Sstevel@tonic-gate 					rej = _rej;
36680Sstevel@tonic-gate 					rej.rej_name = strdup(_rej.rej_name);
36690Sstevel@tonic-gate 				}
36700Sstevel@tonic-gate 			}
36710Sstevel@tonic-gate 			if (ifl) {
36720Sstevel@tonic-gate 				sdf->sdf_file = ifl;
36730Sstevel@tonic-gate 				break;
36740Sstevel@tonic-gate 			}
36750Sstevel@tonic-gate 		}
36760Sstevel@tonic-gate 		if (sdf->sdf_file)
36770Sstevel@tonic-gate 			continue;
36780Sstevel@tonic-gate 
36790Sstevel@tonic-gate 		/*
36800Sstevel@tonic-gate 		 * If we've got this far we haven't found the shared object.
36810Sstevel@tonic-gate 		 * If an object was found, but was rejected for some reason,
36820Sstevel@tonic-gate 		 * print a diagnostic to that effect, otherwise generate a
36830Sstevel@tonic-gate 		 * generic "not found" diagnostic.
36840Sstevel@tonic-gate 		 */
36850Sstevel@tonic-gate 		if (rej.rej_type) {
36864734Sab196087 			Conv_reject_desc_buf_t rej_buf;
36874734Sab196087 
3688*13074SAli.Bahrami@Oracle.COM 			ld_eprintf(ofl, ERR_WARNING,
36891618Srie 			    MSG_INTL(reject[rej.rej_type]),
36900Sstevel@tonic-gate 			    rej.rej_name ? rej.rej_name :
36914734Sab196087 			    MSG_INTL(MSG_STR_UNKNOWN),
36926206Sab196087 			    conv_reject_desc(&rej, &rej_buf,
36936206Sab196087 			    ld_targ.t_m.m_mach));
36940Sstevel@tonic-gate 		} else {
3695*13074SAli.Bahrami@Oracle.COM 			ld_eprintf(ofl, ERR_WARNING,
36961618Srie 			    MSG_INTL(MSG_FIL_NOTFOUND), file, sdf->sdf_rfile);
36970Sstevel@tonic-gate 		}
36980Sstevel@tonic-gate 	}
36990Sstevel@tonic-gate 
37000Sstevel@tonic-gate 	/*
37010Sstevel@tonic-gate 	 * Finally, now that all objects have been input, make sure any version
37020Sstevel@tonic-gate 	 * requirements have been met.
37030Sstevel@tonic-gate 	 */
37041618Srie 	return (ld_vers_verify(ofl));
37050Sstevel@tonic-gate }
3706