xref: /onnv-gate/usr/src/cmd/sgs/libld/common/files.c (revision 10454)
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  *
268501SRod.Evans@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
270Sstevel@tonic-gate  * Use is subject to license terms.
280Sstevel@tonic-gate  */
290Sstevel@tonic-gate 
300Sstevel@tonic-gate /*
310Sstevel@tonic-gate  * Processing of relocatable objects and shared objects.
320Sstevel@tonic-gate  */
336206Sab196087 
346206Sab196087 #define	ELF_TARGET_AMD64
356206Sab196087 #define	ELF_TARGET_SPARC
366206Sab196087 
370Sstevel@tonic-gate #include	<stdio.h>
380Sstevel@tonic-gate #include	<string.h>
390Sstevel@tonic-gate #include	<fcntl.h>
400Sstevel@tonic-gate #include	<unistd.h>
410Sstevel@tonic-gate #include	<link.h>
420Sstevel@tonic-gate #include	<limits.h>
430Sstevel@tonic-gate #include	<sys/stat.h>
440Sstevel@tonic-gate #include	<sys/systeminfo.h>
450Sstevel@tonic-gate #include	<debug.h>
460Sstevel@tonic-gate #include	<msg.h>
470Sstevel@tonic-gate #include	<_libld.h>
480Sstevel@tonic-gate 
490Sstevel@tonic-gate /*
500Sstevel@tonic-gate  * Decide if we can link against this input file.
510Sstevel@tonic-gate  */
520Sstevel@tonic-gate static int
537463SRod.Evans@Sun.COM ifl_verify(Ehdr *ehdr, Ofl_desc *ofl, Rej_desc *rej)
540Sstevel@tonic-gate {
550Sstevel@tonic-gate 	/*
560Sstevel@tonic-gate 	 * Check the validity of the elf header information for compatibility
570Sstevel@tonic-gate 	 * with this machine and our own internal elf library.
580Sstevel@tonic-gate 	 */
596206Sab196087 	if ((ehdr->e_machine != ld_targ.t_m.m_mach) &&
606206Sab196087 	    ((ehdr->e_machine != ld_targ.t_m.m_machplus) &&
616206Sab196087 	    ((ehdr->e_flags & ld_targ.t_m.m_flagsplus) == 0))) {
620Sstevel@tonic-gate 		rej->rej_type = SGS_REJ_MACH;
630Sstevel@tonic-gate 		rej->rej_info = (uint_t)ehdr->e_machine;
640Sstevel@tonic-gate 		return (0);
650Sstevel@tonic-gate 	}
666206Sab196087 	if (ehdr->e_ident[EI_DATA] != ld_targ.t_m.m_data) {
670Sstevel@tonic-gate 		rej->rej_type = SGS_REJ_DATA;
680Sstevel@tonic-gate 		rej->rej_info = (uint_t)ehdr->e_ident[EI_DATA];
690Sstevel@tonic-gate 		return (0);
700Sstevel@tonic-gate 	}
711618Srie 	if (ehdr->e_version > ofl->ofl_dehdr->e_version) {
720Sstevel@tonic-gate 		rej->rej_type = SGS_REJ_VERSION;
730Sstevel@tonic-gate 		rej->rej_info = (uint_t)ehdr->e_version;
740Sstevel@tonic-gate 		return (0);
750Sstevel@tonic-gate 	}
760Sstevel@tonic-gate 	return (1);
770Sstevel@tonic-gate }
780Sstevel@tonic-gate 
790Sstevel@tonic-gate /*
800Sstevel@tonic-gate  * Check sanity of file header and allocate an infile descriptor
810Sstevel@tonic-gate  * for the file being processed.
820Sstevel@tonic-gate  */
831618Srie static Ifl_desc *
844716Sab196087 ifl_setup(const char *name, Ehdr *ehdr, Elf *elf, Word flags, Ofl_desc *ofl,
850Sstevel@tonic-gate     Rej_desc *rej)
860Sstevel@tonic-gate {
870Sstevel@tonic-gate 	Ifl_desc	*ifl;
880Sstevel@tonic-gate 	Rej_desc	_rej = { 0 };
890Sstevel@tonic-gate 
900Sstevel@tonic-gate 	if (ifl_verify(ehdr, ofl, &_rej) == 0) {
910Sstevel@tonic-gate 		_rej.rej_name = name;
926206Sab196087 		DBG_CALL(Dbg_file_rejected(ofl->ofl_lml, &_rej,
936206Sab196087 		    ld_targ.t_m.m_mach));
940Sstevel@tonic-gate 		if (rej->rej_type == 0) {
950Sstevel@tonic-gate 			*rej = _rej;
960Sstevel@tonic-gate 			rej->rej_name = strdup(_rej.rej_name);
970Sstevel@tonic-gate 		}
980Sstevel@tonic-gate 		return (0);
990Sstevel@tonic-gate 	}
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate 	if ((ifl = libld_calloc(1, sizeof (Ifl_desc))) == 0)
1020Sstevel@tonic-gate 		return ((Ifl_desc *)S_ERROR);
1030Sstevel@tonic-gate 	ifl->ifl_name = name;
1040Sstevel@tonic-gate 	ifl->ifl_ehdr = ehdr;
1050Sstevel@tonic-gate 	ifl->ifl_elf = elf;
1060Sstevel@tonic-gate 	ifl->ifl_flags = flags;
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate 	/*
1090Sstevel@tonic-gate 	 * Is this file using 'extended Section Indexes'.  If so, use the
1100Sstevel@tonic-gate 	 * e_shnum & e_shstrndx which can be found at:
1110Sstevel@tonic-gate 	 *
1120Sstevel@tonic-gate 	 *	e_shnum == Shdr[0].sh_size
1130Sstevel@tonic-gate 	 *	e_shstrndx == Shdr[0].sh_link
1140Sstevel@tonic-gate 	 */
1150Sstevel@tonic-gate 	if ((ehdr->e_shnum == 0) && (ehdr->e_shoff != 0)) {
1160Sstevel@tonic-gate 		Elf_Scn	*scn;
1170Sstevel@tonic-gate 		Shdr	*shdr0;
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate 		if ((scn = elf_getscn(elf, 0)) == NULL) {
1201618Srie 			eprintf(ofl->ofl_lml, ERR_ELF,
1211618Srie 			    MSG_INTL(MSG_ELF_GETSCN), name);
1220Sstevel@tonic-gate 			ofl->ofl_flags |= FLG_OF_FATAL;
1230Sstevel@tonic-gate 			return ((Ifl_desc *)S_ERROR);
1240Sstevel@tonic-gate 		}
1250Sstevel@tonic-gate 		if ((shdr0 = elf_getshdr(scn)) == NULL) {
1261618Srie 			eprintf(ofl->ofl_lml, ERR_ELF,
1271618Srie 			    MSG_INTL(MSG_ELF_GETSHDR), name);
1280Sstevel@tonic-gate 			ofl->ofl_flags |= FLG_OF_FATAL;
1290Sstevel@tonic-gate 			return ((Ifl_desc *)S_ERROR);
1300Sstevel@tonic-gate 		}
1310Sstevel@tonic-gate 		ifl->ifl_shnum = (Word)shdr0->sh_size;
1320Sstevel@tonic-gate 		if (ehdr->e_shstrndx == SHN_XINDEX)
1330Sstevel@tonic-gate 			ifl->ifl_shstrndx = shdr0->sh_link;
1340Sstevel@tonic-gate 		else
1350Sstevel@tonic-gate 			ifl->ifl_shstrndx = ehdr->e_shstrndx;
1360Sstevel@tonic-gate 	} else {
1370Sstevel@tonic-gate 		ifl->ifl_shnum = ehdr->e_shnum;
1380Sstevel@tonic-gate 		ifl->ifl_shstrndx = ehdr->e_shstrndx;
1390Sstevel@tonic-gate 	}
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate 	if ((ifl->ifl_isdesc = libld_calloc(ifl->ifl_shnum,
1420Sstevel@tonic-gate 	    sizeof (Is_desc *))) == 0)
1430Sstevel@tonic-gate 		return ((Ifl_desc *)S_ERROR);
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate 	/*
1460Sstevel@tonic-gate 	 * Record this new input file on the shared object or relocatable
1470Sstevel@tonic-gate 	 * object input file list.
1480Sstevel@tonic-gate 	 */
1490Sstevel@tonic-gate 	if (ifl->ifl_ehdr->e_type == ET_DYN) {
1509131SRod.Evans@Sun.COM 		if (aplist_append(&ofl->ofl_sos, ifl, AL_CNT_OFL_LIBS) == NULL)
1519131SRod.Evans@Sun.COM 			return ((Ifl_desc *)S_ERROR);
1520Sstevel@tonic-gate 	} else {
1539131SRod.Evans@Sun.COM 		if (aplist_append(&ofl->ofl_objs, ifl, AL_CNT_OFL_OBJS) == NULL)
1549131SRod.Evans@Sun.COM 			return ((Ifl_desc *)S_ERROR);
1550Sstevel@tonic-gate 	}
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate 	return (ifl);
1580Sstevel@tonic-gate }
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate /*
1610Sstevel@tonic-gate  * Process a generic section.  The appropriate section information is added
1620Sstevel@tonic-gate  * to the files input descriptor list.
1630Sstevel@tonic-gate  */
1641618Srie static uintptr_t
1650Sstevel@tonic-gate process_section(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
1660Sstevel@tonic-gate     Word ndx, int ident, Ofl_desc *ofl)
1670Sstevel@tonic-gate {
1680Sstevel@tonic-gate 	Is_desc	*isp;
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate 	/*
1710Sstevel@tonic-gate 	 * Create a new input section descriptor.  If this is a NOBITS
1720Sstevel@tonic-gate 	 * section elf_getdata() will still create a data buffer (the buffer
1730Sstevel@tonic-gate 	 * will be null and the size will reflect the actual memory size).
1740Sstevel@tonic-gate 	 */
1750Sstevel@tonic-gate 	if ((isp = libld_calloc(sizeof (Is_desc), 1)) == 0)
1760Sstevel@tonic-gate 		return (S_ERROR);
1770Sstevel@tonic-gate 	isp->is_shdr = shdr;
1780Sstevel@tonic-gate 	isp->is_file = ifl;
1790Sstevel@tonic-gate 	isp->is_name = name;
1800Sstevel@tonic-gate 	isp->is_scnndx = ndx;
181574Sseizo 	isp->is_flags = FLG_IS_EXTERNAL;
1827463SRod.Evans@Sun.COM 	isp->is_keyident = ident;
1837463SRod.Evans@Sun.COM 
1840Sstevel@tonic-gate 	if ((isp->is_indata = elf_getdata(scn, NULL)) == NULL) {
1851618Srie 		eprintf(ofl->ofl_lml, ERR_ELF, MSG_INTL(MSG_ELF_GETDATA),
1861618Srie 		    ifl->ifl_name);
1870Sstevel@tonic-gate 		ofl->ofl_flags |= FLG_OF_FATAL;
1880Sstevel@tonic-gate 		return (0);
1890Sstevel@tonic-gate 	}
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate 	if ((shdr->sh_flags & SHF_EXCLUDE) &&
1920Sstevel@tonic-gate 	    ((ofl->ofl_flags & FLG_OF_RELOBJ) == 0)) {
1930Sstevel@tonic-gate 		isp->is_flags |= FLG_IS_DISCARD;
1940Sstevel@tonic-gate 	}
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate 	/*
1970Sstevel@tonic-gate 	 * Add the new input section to the files input section list and
1987463SRod.Evans@Sun.COM 	 * flag whether the section needs placing in an output section.  This
1997463SRod.Evans@Sun.COM 	 * placement is deferred until all input section processing has been
2007463SRod.Evans@Sun.COM 	 * completed, as SHT_GROUP sections can provide information that will
2017463SRod.Evans@Sun.COM 	 * affect how other sections within the file should be placed.
2020Sstevel@tonic-gate 	 */
2030Sstevel@tonic-gate 	ifl->ifl_isdesc[ndx] = isp;
2040Sstevel@tonic-gate 
2057463SRod.Evans@Sun.COM 	if (ident) {
2067463SRod.Evans@Sun.COM 		if (shdr->sh_flags & ALL_SHF_ORDER) {
2077463SRod.Evans@Sun.COM 			isp->is_flags |= FLG_IS_ORDERED;
2087463SRod.Evans@Sun.COM 			ifl->ifl_flags |= FLG_IF_ORDERED;
2092647Srie 		}
2107463SRod.Evans@Sun.COM 		isp->is_flags |= FLG_IS_PLACE;
2110Sstevel@tonic-gate 	}
2120Sstevel@tonic-gate 	return (1);
2130Sstevel@tonic-gate }
2140Sstevel@tonic-gate 
2150Sstevel@tonic-gate /*
2160Sstevel@tonic-gate  * Determine the software capabilities of the object being built from the
2170Sstevel@tonic-gate  * capabilities of the input relocatable objects.   One software capability
2180Sstevel@tonic-gate  * is presently recognized, and represented with the following (sys/elf.h):
2190Sstevel@tonic-gate  *
2200Sstevel@tonic-gate  *   SF1_SUNW_FPKNWN	use/non-use of frame pointer is known, and
2210Sstevel@tonic-gate  *   SF1_SUNW_FPUSED    the frame pointer is in use.
2220Sstevel@tonic-gate  *
2230Sstevel@tonic-gate  * The resolution of the present fame pointer state, and the capabilities
2240Sstevel@tonic-gate  * provided by a new input relocatable object are:
2250Sstevel@tonic-gate  *
2260Sstevel@tonic-gate  *                              new input relocatable object
2270Sstevel@tonic-gate  *
2280Sstevel@tonic-gate  *      present      |  SF1_SUNW_FPKNWN  |  SF1_SUNW_FPKNWN  |    <unknown>
2290Sstevel@tonic-gate  *       state       |  SF1_SUNW_FPUSED  |                   |
2300Sstevel@tonic-gate  *  ---------------------------------------------------------------------------
2310Sstevel@tonic-gate  *  SF1_SUNW_FPKNWN  |  SF1_SUNW_FPKNWN  |  SF1_SUNW_FPKNWN  |  SF1_SUNW_FPKNWN
2320Sstevel@tonic-gate  *  SF1_SUNW_FPUSED  |  SF1_SUNW_FPUSED  |                   |  SF1_SUNW_FPUSED
2330Sstevel@tonic-gate  *  ---------------------------------------------------------------------------
2340Sstevel@tonic-gate  *  SF1_SUNW_FPKNWN  |  SF1_SUNW_FPKNWN  |  SF1_SUNW_FPKNWN  |  SF1_SUNW_FPKNWN
2350Sstevel@tonic-gate  *                   |                   |                   |
2360Sstevel@tonic-gate  *  ---------------------------------------------------------------------------
2370Sstevel@tonic-gate  *     <unknown>     |  SF1_SUNW_FPKNWN  |  SF1_SUNW_FPKNWN  |    <unknown>
2380Sstevel@tonic-gate  *                   |  SF1_SUNW_FPUSED  |                   |
2390Sstevel@tonic-gate  */
2400Sstevel@tonic-gate static void
2419878SAli.Bahrami@Sun.COM sf1_cap(Ofl_desc *ofl, Xword val, Ifl_desc *ifl, Is_desc *cisp)
2420Sstevel@tonic-gate {
2430Sstevel@tonic-gate 	Xword	badval;
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate 	/*
2460Sstevel@tonic-gate 	 * If a mapfile has established definitions to override any input
2470Sstevel@tonic-gate 	 * capabilities, ignore any new input capabilities.
2480Sstevel@tonic-gate 	 */
2490Sstevel@tonic-gate 	if (ofl->ofl_flags1 & FLG_OF1_OVSFCAP) {
2501618Srie 		Dbg_cap_sec_entry(ofl->ofl_lml, DBG_CAP_IGNORE, CA_SUNW_SF_1,
2516206Sab196087 		    val, ld_targ.t_m.m_mach);
2520Sstevel@tonic-gate 		return;
2530Sstevel@tonic-gate 	}
2540Sstevel@tonic-gate 
2557833SRod.Evans@Sun.COM #if	!defined(_ELF64)
2567833SRod.Evans@Sun.COM 	if (ifl->ifl_ehdr->e_type == ET_REL) {
2577833SRod.Evans@Sun.COM 		/*
2587833SRod.Evans@Sun.COM 		 * The SF1_SUNW_ADDR32 is only meaningful when building a 64-bit
2597833SRod.Evans@Sun.COM 		 * object.  Warn the user, and remove the setting, if we're
2607833SRod.Evans@Sun.COM 		 * building a 32-bit object.
2617833SRod.Evans@Sun.COM 		 */
2627833SRod.Evans@Sun.COM 		if (val & SF1_SUNW_ADDR32) {
2637833SRod.Evans@Sun.COM 			eprintf(ofl->ofl_lml, ERR_WARNING,
2649878SAli.Bahrami@Sun.COM 			    MSG_INTL(MSG_FIL_INADDR32SF1), ifl->ifl_name,
2659878SAli.Bahrami@Sun.COM 			    EC_WORD(cisp->is_scnndx), cisp->is_name);
2667833SRod.Evans@Sun.COM 			val &= ~SF1_SUNW_ADDR32;
2677833SRod.Evans@Sun.COM 		}
2687833SRod.Evans@Sun.COM 	}
2697833SRod.Evans@Sun.COM #endif
2700Sstevel@tonic-gate 	/*
2710Sstevel@tonic-gate 	 * If this object doesn't specify any capabilities, ignore it, and
2720Sstevel@tonic-gate 	 * leave the state as is.
2730Sstevel@tonic-gate 	 */
2740Sstevel@tonic-gate 	if (val == 0)
2750Sstevel@tonic-gate 		return;
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate 	/*
2780Sstevel@tonic-gate 	 * Make sure we only accept known software capabilities.  Note, that
2790Sstevel@tonic-gate 	 * an F1_SUNW_FPUSED by itself is viewed as bad practice.
2800Sstevel@tonic-gate 	 */
2810Sstevel@tonic-gate 	if ((badval = (val & ~SF1_SUNW_MASK)) != 0) {
2821618Srie 		eprintf(ofl->ofl_lml, ERR_WARNING, MSG_INTL(MSG_FIL_BADSF1),
2839878SAli.Bahrami@Sun.COM 		    ifl->ifl_name, EC_WORD(cisp->is_scnndx), cisp->is_name,
2849878SAli.Bahrami@Sun.COM 		    EC_XWORD(badval));
2850Sstevel@tonic-gate 		val &= SF1_SUNW_MASK;
2860Sstevel@tonic-gate 	}
2877833SRod.Evans@Sun.COM 	if ((val & (SF1_SUNW_FPKNWN | SF1_SUNW_FPUSED)) == SF1_SUNW_FPUSED) {
2881618Srie 		eprintf(ofl->ofl_lml, ERR_WARNING, MSG_INTL(MSG_FIL_BADSF1),
2899878SAli.Bahrami@Sun.COM 		    ifl->ifl_name, EC_WORD(cisp->is_scnndx), cisp->is_name,
2909878SAli.Bahrami@Sun.COM 		    EC_XWORD(val));
2910Sstevel@tonic-gate 		return;
2920Sstevel@tonic-gate 	}
2930Sstevel@tonic-gate 
2947833SRod.Evans@Sun.COM 	/*
2957833SRod.Evans@Sun.COM 	 * If the input file is not a relocatable object, then we're only here
2967833SRod.Evans@Sun.COM 	 * to warn the user of any questionable capabilities.
2977833SRod.Evans@Sun.COM 	 */
2987833SRod.Evans@Sun.COM 	if (ifl->ifl_ehdr->e_type != ET_REL) {
2997833SRod.Evans@Sun.COM #if	defined(_ELF64)
3007833SRod.Evans@Sun.COM 		/*
3017833SRod.Evans@Sun.COM 		 * If we're building a 64-bit executable, and we come across a
3027833SRod.Evans@Sun.COM 		 * dependency that requires a restricted address space, then
3037833SRod.Evans@Sun.COM 		 * that dependencies requirement can only be satisfied if the
3047833SRod.Evans@Sun.COM 		 * executable triggers the restricted address space.  This is a
3057833SRod.Evans@Sun.COM 		 * warning rather than a fatal error, as the possibility exists
3067833SRod.Evans@Sun.COM 		 * that an appropriate dependency will be provided at runtime.
3077833SRod.Evans@Sun.COM 		 * The runtime linker will refuse to use this dependency.
3087833SRod.Evans@Sun.COM 		 */
3097833SRod.Evans@Sun.COM 		if ((val & SF1_SUNW_ADDR32) && (ofl->ofl_flags & FLG_OF_EXEC) &&
3107833SRod.Evans@Sun.COM 		    ((ofl->ofl_sfcap_1 & SF1_SUNW_ADDR32) == 0)) {
3117833SRod.Evans@Sun.COM 			eprintf(ofl->ofl_lml, ERR_WARNING,
3129878SAli.Bahrami@Sun.COM 			    MSG_INTL(MSG_FIL_EXADDR32SF1), ifl->ifl_name,
3139878SAli.Bahrami@Sun.COM 			    EC_WORD(cisp->is_scnndx), cisp->is_name);
3147833SRod.Evans@Sun.COM 		}
3157833SRod.Evans@Sun.COM #endif
3167833SRod.Evans@Sun.COM 		return;
3177833SRod.Evans@Sun.COM 	}
3187833SRod.Evans@Sun.COM 
3191618Srie 	Dbg_cap_sec_entry(ofl->ofl_lml, DBG_CAP_OLD, CA_SUNW_SF_1,
3206206Sab196087 	    ofl->ofl_sfcap_1, ld_targ.t_m.m_mach);
3211618Srie 	Dbg_cap_sec_entry(ofl->ofl_lml, DBG_CAP_NEW, CA_SUNW_SF_1,
3226206Sab196087 	    val, ld_targ.t_m.m_mach);
3230Sstevel@tonic-gate 
3240Sstevel@tonic-gate 	/*
3250Sstevel@tonic-gate 	 * Determine the resolution of the present frame pointer and the
3260Sstevel@tonic-gate 	 * new input relocatable objects frame pointer.
3270Sstevel@tonic-gate 	 */
3280Sstevel@tonic-gate 	if ((ofl->ofl_sfcap_1 & (SF1_SUNW_FPKNWN | SF1_SUNW_FPUSED)) ==
3290Sstevel@tonic-gate 	    (SF1_SUNW_FPKNWN | SF1_SUNW_FPUSED)) {
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 		 */
3340Sstevel@tonic-gate 		if ((val & (SF1_SUNW_FPKNWN | SF1_SUNW_FPUSED)) !=
3350Sstevel@tonic-gate 		    (SF1_SUNW_FPKNWN | SF1_SUNW_FPUSED))
3360Sstevel@tonic-gate 			ofl->ofl_sfcap_1 &= ~SF1_SUNW_FPUSED;
3370Sstevel@tonic-gate 
3380Sstevel@tonic-gate 	} else if ((ofl->ofl_sfcap_1 & SF1_SUNW_FPKNWN) == 0) {
3390Sstevel@tonic-gate 		/*
3400Sstevel@tonic-gate 		 * If the present state is unknown, take the new relocatable
3410Sstevel@tonic-gate 		 * object frame pointer usage.
3420Sstevel@tonic-gate 		 */
3430Sstevel@tonic-gate 		ofl->ofl_sfcap_1 = val;
3440Sstevel@tonic-gate 	}
3450Sstevel@tonic-gate 
3461618Srie 	Dbg_cap_sec_entry(ofl->ofl_lml, DBG_CAP_RESOLVED, CA_SUNW_SF_1,
3476206Sab196087 	    ofl->ofl_sfcap_1, ld_targ.t_m.m_mach);
3480Sstevel@tonic-gate }
3490Sstevel@tonic-gate 
3500Sstevel@tonic-gate /*
3510Sstevel@tonic-gate  * Determine the hardware capabilities of the object being built from the
3520Sstevel@tonic-gate  * capabilities of the input relocatable objects.  There's really little to
3530Sstevel@tonic-gate  * do here, other than to offer diagnostics, hardware capabilities are simply
3540Sstevel@tonic-gate  * additive.
3550Sstevel@tonic-gate  */
3560Sstevel@tonic-gate static void
3570Sstevel@tonic-gate hw1_cap(Ofl_desc *ofl, Xword val)
3580Sstevel@tonic-gate {
3590Sstevel@tonic-gate 	/*
3600Sstevel@tonic-gate 	 * If a mapfile has established definitions to override any input
3610Sstevel@tonic-gate 	 * capabilities, ignore any new input capabilities.
3620Sstevel@tonic-gate 	 */
3630Sstevel@tonic-gate 	if (ofl->ofl_flags1 & FLG_OF1_OVHWCAP) {
3641618Srie 		Dbg_cap_sec_entry(ofl->ofl_lml, DBG_CAP_IGNORE, CA_SUNW_HW_1,
3656206Sab196087 		    val, ld_targ.t_m.m_mach);
3660Sstevel@tonic-gate 		return;
3670Sstevel@tonic-gate 	}
3680Sstevel@tonic-gate 
3690Sstevel@tonic-gate 	/*
3700Sstevel@tonic-gate 	 * If this object doesn't specify any capabilities, ignore it, and
3710Sstevel@tonic-gate 	 * leave the state as is.
3720Sstevel@tonic-gate 	 */
3730Sstevel@tonic-gate 	if (val == 0)
3740Sstevel@tonic-gate 		return;
3750Sstevel@tonic-gate 
3761618Srie 	Dbg_cap_sec_entry(ofl->ofl_lml, DBG_CAP_OLD, CA_SUNW_HW_1,
3776206Sab196087 	    ofl->ofl_hwcap_1, ld_targ.t_m.m_mach);
3786206Sab196087 	Dbg_cap_sec_entry(ofl->ofl_lml, DBG_CAP_NEW, CA_SUNW_HW_1, val,
3796206Sab196087 	    ld_targ.t_m.m_mach);
3800Sstevel@tonic-gate 
3810Sstevel@tonic-gate 	ofl->ofl_hwcap_1 |= val;
3820Sstevel@tonic-gate 
3831618Srie 	Dbg_cap_sec_entry(ofl->ofl_lml, DBG_CAP_RESOLVED, CA_SUNW_HW_1,
3846206Sab196087 	    ofl->ofl_hwcap_1, ld_targ.t_m.m_mach);
3850Sstevel@tonic-gate }
3860Sstevel@tonic-gate 
3870Sstevel@tonic-gate /*
3880Sstevel@tonic-gate  * Process a hardware/software capabilities section.  Traverse the section
3890Sstevel@tonic-gate  * updating the global capabilities variables as necessary.
3900Sstevel@tonic-gate  */
3910Sstevel@tonic-gate static void
3923617Srie process_cap(Ifl_desc *ifl, Is_desc *cisp, Ofl_desc *ofl)
3930Sstevel@tonic-gate {
3941618Srie 	Cap	*cdata;
3953617Srie 	Word	ndx, cnum;
3960Sstevel@tonic-gate 
3978501SRod.Evans@Sun.COM 	DBG_CALL(Dbg_cap_sec_title(ofl->ofl_lml, ifl->ifl_name));
3980Sstevel@tonic-gate 
3993617Srie 	/*
4003617Srie 	 * The capabilities are supposed to be terminated with a CA_SUNW_NULL
4013617Srie 	 * entry.  However, the compilers have been known to not follow this
4023617Srie 	 * convention.  Use the section information to determine the number
4033617Srie 	 * of capabilities, and skip any CA_SUNW_NULL entries.
4043617Srie 	 */
4053617Srie 	cdata = (Cap *)cisp->is_indata->d_buf;
4063617Srie 	cnum = (Word)(cisp->is_shdr->sh_size / cisp->is_shdr->sh_entsize);
4073617Srie 
4083617Srie 	for (ndx = 0; ndx < cnum; cdata++, ndx++) {
4090Sstevel@tonic-gate 		switch (cdata->c_tag) {
4100Sstevel@tonic-gate 			case CA_SUNW_HW_1:
4117833SRod.Evans@Sun.COM 				/*
4127833SRod.Evans@Sun.COM 				 * Only the hardware capabilities that are
4137833SRod.Evans@Sun.COM 				 * defined in a relocatable object become part
4147833SRod.Evans@Sun.COM 				 * of the hardware capabilities in the output
4157833SRod.Evans@Sun.COM 				 * file.
4167833SRod.Evans@Sun.COM 				 */
4177833SRod.Evans@Sun.COM 				if (ifl->ifl_ehdr->e_type == ET_REL)
4187833SRod.Evans@Sun.COM 					hw1_cap(ofl, cdata->c_un.c_val);
4190Sstevel@tonic-gate 				break;
4200Sstevel@tonic-gate 			case CA_SUNW_SF_1:
4217833SRod.Evans@Sun.COM 				/*
4227833SRod.Evans@Sun.COM 				 * Only the software capabilities that are
4237833SRod.Evans@Sun.COM 				 * defined in a relocatable object become part
4247833SRod.Evans@Sun.COM 				 * of the software capabilities in the output
4257833SRod.Evans@Sun.COM 				 * file.  However, check the validity of the
4267833SRod.Evans@Sun.COM 				 * software capabilities of any dependencies.
4277833SRod.Evans@Sun.COM 				 */
4289878SAli.Bahrami@Sun.COM 				sf1_cap(ofl, cdata->c_un.c_val, ifl, cisp);
4293617Srie 				break;
4303617Srie 			case CA_SUNW_NULL:
4310Sstevel@tonic-gate 				break;
4320Sstevel@tonic-gate 			default:
4331618Srie 				eprintf(ofl->ofl_lml, ERR_WARNING,
4349878SAli.Bahrami@Sun.COM 				    MSG_INTL(MSG_FIL_UNKCAP), ifl->ifl_name,
4359878SAli.Bahrami@Sun.COM 				    EC_WORD(cisp->is_scnndx), cisp->is_name,
4369878SAli.Bahrami@Sun.COM 				    cdata->c_tag);
4370Sstevel@tonic-gate 		}
4380Sstevel@tonic-gate 	}
4390Sstevel@tonic-gate }
4400Sstevel@tonic-gate 
4410Sstevel@tonic-gate /*
4420Sstevel@tonic-gate  * Simply process the section so that we have pointers to the data for use
4430Sstevel@tonic-gate  * in later routines, however don't add the section to the output section
4440Sstevel@tonic-gate  * list as we will be creating our own replacement sections later (ie.
4450Sstevel@tonic-gate  * symtab and relocation).
4460Sstevel@tonic-gate  */
4471618Srie static uintptr_t
4480Sstevel@tonic-gate /* ARGSUSED5 */
4490Sstevel@tonic-gate process_input(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
4507463SRod.Evans@Sun.COM     Word ndx, int ident, Ofl_desc *ofl)
4510Sstevel@tonic-gate {
4526206Sab196087 	return (process_section(name, ifl, shdr, scn, ndx,
4536206Sab196087 	    ld_targ.t_id.id_null, ofl));
4540Sstevel@tonic-gate }
4550Sstevel@tonic-gate 
4560Sstevel@tonic-gate /*
4570Sstevel@tonic-gate  * Keep a running count of relocation entries from input relocatable objects for
4580Sstevel@tonic-gate  * sizing relocation buckets later.  If we're building an executable, save any
4590Sstevel@tonic-gate  * relocations from shared objects to determine if any copy relocation symbol
4600Sstevel@tonic-gate  * has a displacement relocation against it.
4610Sstevel@tonic-gate  */
4621618Srie static uintptr_t
4630Sstevel@tonic-gate /* ARGSUSED5 */
4640Sstevel@tonic-gate process_reloc(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
4657463SRod.Evans@Sun.COM     Word ndx, int ident, Ofl_desc *ofl)
4660Sstevel@tonic-gate {
4670Sstevel@tonic-gate 	if (process_section(name, ifl,
4686206Sab196087 	    shdr, scn, ndx, ld_targ.t_id.id_null, ofl) == S_ERROR)
4690Sstevel@tonic-gate 		return (S_ERROR);
4700Sstevel@tonic-gate 
4710Sstevel@tonic-gate 	if (ifl->ifl_ehdr->e_type == ET_REL) {
4720Sstevel@tonic-gate 		if (shdr->sh_entsize && (shdr->sh_entsize <= shdr->sh_size))
4730Sstevel@tonic-gate 			/* LINTED */
4740Sstevel@tonic-gate 			ofl->ofl_relocincnt +=
4750Sstevel@tonic-gate 			    (Word)(shdr->sh_size / shdr->sh_entsize);
4760Sstevel@tonic-gate 	} else if (ofl->ofl_flags & FLG_OF_EXEC) {
4779131SRod.Evans@Sun.COM 		if (aplist_append(&ifl->ifl_relsect, ifl->ifl_isdesc[ndx],
4789131SRod.Evans@Sun.COM 		    AL_CNT_IFL_RELSECS) == NULL)
4790Sstevel@tonic-gate 			return (S_ERROR);
4800Sstevel@tonic-gate 	}
4810Sstevel@tonic-gate 	return (1);
4820Sstevel@tonic-gate }
4830Sstevel@tonic-gate 
4840Sstevel@tonic-gate 
4850Sstevel@tonic-gate /*
4860Sstevel@tonic-gate  * Process a string table section.  A valid section contains an initial and
4870Sstevel@tonic-gate  * final null byte.
4880Sstevel@tonic-gate  */
4891618Srie static uintptr_t
4900Sstevel@tonic-gate process_strtab(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
4917463SRod.Evans@Sun.COM     Word ndx, int ident, Ofl_desc *ofl)
4920Sstevel@tonic-gate {
4930Sstevel@tonic-gate 	char		*data;
4940Sstevel@tonic-gate 	size_t		size;
4950Sstevel@tonic-gate 	Is_desc		*isp;
4960Sstevel@tonic-gate 	uintptr_t	error;
4970Sstevel@tonic-gate 
4980Sstevel@tonic-gate 	/*
4990Sstevel@tonic-gate 	 * Never include .stab.excl sections in any output file.
5000Sstevel@tonic-gate 	 * If the -s flag has been specified strip any .stab sections.
5010Sstevel@tonic-gate 	 */
5020Sstevel@tonic-gate 	if (((ofl->ofl_flags & FLG_OF_STRIP) && ident &&
5030Sstevel@tonic-gate 	    (strncmp(name, MSG_ORIG(MSG_SCN_STAB), MSG_SCN_STAB_SIZE) == 0)) ||
5040Sstevel@tonic-gate 	    (strcmp(name, MSG_ORIG(MSG_SCN_STABEXCL)) == 0) && ident)
5050Sstevel@tonic-gate 		return (1);
5060Sstevel@tonic-gate 
5070Sstevel@tonic-gate 	/*
5080Sstevel@tonic-gate 	 * If we got here to process a .shstrtab or .dynstr table, `ident' will
5090Sstevel@tonic-gate 	 * be null.  Otherwise make sure we don't have a .strtab section as this
5100Sstevel@tonic-gate 	 * should not be added to the output section list either.
5110Sstevel@tonic-gate 	 */
5126206Sab196087 	if ((ident != ld_targ.t_id.id_null) &&
5130Sstevel@tonic-gate 	    (strcmp(name, MSG_ORIG(MSG_SCN_STRTAB)) == 0))
5146206Sab196087 		ident = ld_targ.t_id.id_null;
5150Sstevel@tonic-gate 
5160Sstevel@tonic-gate 	error = process_section(name, ifl, shdr, scn, ndx, ident, ofl);
5170Sstevel@tonic-gate 	if ((error == 0) || (error == S_ERROR))
5180Sstevel@tonic-gate 		return (error);
5190Sstevel@tonic-gate 
5200Sstevel@tonic-gate 	/*
5210Sstevel@tonic-gate 	 * String tables should start and end with a NULL byte.  Note, it has
5220Sstevel@tonic-gate 	 * been known for the assembler to create empty string tables, so check
5230Sstevel@tonic-gate 	 * the size before attempting to verify the data itself.
5240Sstevel@tonic-gate 	 */
5250Sstevel@tonic-gate 	isp = ifl->ifl_isdesc[ndx];
5260Sstevel@tonic-gate 	size = isp->is_indata->d_size;
5270Sstevel@tonic-gate 	if (size) {
5280Sstevel@tonic-gate 		data = isp->is_indata->d_buf;
5290Sstevel@tonic-gate 		if (data[0] != '\0' || data[size - 1] != '\0')
5301618Srie 			eprintf(ofl->ofl_lml, ERR_WARNING,
5319878SAli.Bahrami@Sun.COM 			    MSG_INTL(MSG_FIL_MALSTR), ifl->ifl_name,
5329878SAli.Bahrami@Sun.COM 			    EC_WORD(isp->is_scnndx), name);
5330Sstevel@tonic-gate 	} else
5340Sstevel@tonic-gate 		isp->is_indata->d_buf = (void *)MSG_ORIG(MSG_STR_EMPTY);
5350Sstevel@tonic-gate 
5360Sstevel@tonic-gate 	ifl->ifl_flags |= FLG_IF_HSTRTAB;
5370Sstevel@tonic-gate 	return (1);
5380Sstevel@tonic-gate }
5390Sstevel@tonic-gate 
5400Sstevel@tonic-gate /*
5410Sstevel@tonic-gate  * Invalid sections produce a warning and are skipped.
5420Sstevel@tonic-gate  */
5431618Srie static uintptr_t
5440Sstevel@tonic-gate /* ARGSUSED3 */
5450Sstevel@tonic-gate invalid_section(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
5460Sstevel@tonic-gate     Word ndx, int ident, Ofl_desc *ofl)
5470Sstevel@tonic-gate {
5484734Sab196087 	Conv_inv_buf_t inv_buf;
5494734Sab196087 
5501618Srie 	eprintf(ofl->ofl_lml, ERR_WARNING, MSG_INTL(MSG_FIL_INVALSEC),
5519878SAli.Bahrami@Sun.COM 	    ifl->ifl_name, EC_WORD(ndx), name,
5529273SAli.Bahrami@Sun.COM 	    conv_sec_type(ifl->ifl_ehdr->e_ident[EI_OSABI],
5539273SAli.Bahrami@Sun.COM 	    ifl->ifl_ehdr->e_machine, shdr->sh_type, 0, &inv_buf));
5540Sstevel@tonic-gate 	return (1);
5550Sstevel@tonic-gate }
5560Sstevel@tonic-gate 
5570Sstevel@tonic-gate /*
558*10454SAli.Bahrami@Sun.COM  * Compare an input section name to a given string, taking the ELF '%'
559*10454SAli.Bahrami@Sun.COM  * section naming convention into account. If an input section name
560*10454SAli.Bahrami@Sun.COM  * contains a '%' character, the '%' and all following characters are
561*10454SAli.Bahrami@Sun.COM  * ignored in the comparison.
562*10454SAli.Bahrami@Sun.COM  *
563*10454SAli.Bahrami@Sun.COM  * entry:
564*10454SAli.Bahrami@Sun.COM  *	is_name - Name of input section
565*10454SAli.Bahrami@Sun.COM  *	match_name - Name to compare to
566*10454SAli.Bahrami@Sun.COM  *	match_len - strlen(match_name)
567*10454SAli.Bahrami@Sun.COM  *
568*10454SAli.Bahrami@Sun.COM  * exit:
569*10454SAli.Bahrami@Sun.COM  *	Returns True (1) if the names match, and False (0) otherwise.
570*10454SAli.Bahrami@Sun.COM  */
571*10454SAli.Bahrami@Sun.COM inline static int
572*10454SAli.Bahrami@Sun.COM is_name_cmp(const char *is_name, const char *match_name, size_t match_len)
573*10454SAli.Bahrami@Sun.COM {
574*10454SAli.Bahrami@Sun.COM 	/*
575*10454SAli.Bahrami@Sun.COM 	 * If the start of is_name is not a match for name,
576*10454SAli.Bahrami@Sun.COM 	 * the match fails.
577*10454SAli.Bahrami@Sun.COM 	 */
578*10454SAli.Bahrami@Sun.COM 	if (strncmp(is_name, match_name, match_len) != 0)
579*10454SAli.Bahrami@Sun.COM 		return (0);
580*10454SAli.Bahrami@Sun.COM 
581*10454SAli.Bahrami@Sun.COM 	/*
582*10454SAli.Bahrami@Sun.COM 	 * The prefix matched. The next character must be either '%', or
583*10454SAli.Bahrami@Sun.COM 	 * NULL, in order for a match to be true.
584*10454SAli.Bahrami@Sun.COM 	 */
585*10454SAli.Bahrami@Sun.COM 	is_name += match_len;
586*10454SAli.Bahrami@Sun.COM 	return ((*is_name == '\0') || (*is_name == '%'));
587*10454SAli.Bahrami@Sun.COM }
588*10454SAli.Bahrami@Sun.COM 
589*10454SAli.Bahrami@Sun.COM /*
5900Sstevel@tonic-gate  * Process a progbits section.
5910Sstevel@tonic-gate  */
5921618Srie static uintptr_t
5930Sstevel@tonic-gate process_progbits(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
5940Sstevel@tonic-gate     Word ndx, int ident, Ofl_desc *ofl)
5950Sstevel@tonic-gate {
5969085SAli.Bahrami@Sun.COM 	int		stab_index = 0;
5979085SAli.Bahrami@Sun.COM 	Word		is_flags = 0;
5989085SAli.Bahrami@Sun.COM 	uintptr_t	r;
5990Sstevel@tonic-gate 
6000Sstevel@tonic-gate 	/*
6010Sstevel@tonic-gate 	 * Never include .stab.excl sections in any output file.
6020Sstevel@tonic-gate 	 * If the -s flag has been specified strip any .stab sections.
6030Sstevel@tonic-gate 	 */
6040Sstevel@tonic-gate 	if (ident && (strncmp(name, MSG_ORIG(MSG_SCN_STAB),
6050Sstevel@tonic-gate 	    MSG_SCN_STAB_SIZE) == 0)) {
6060Sstevel@tonic-gate 		if ((ofl->ofl_flags & FLG_OF_STRIP) ||
6070Sstevel@tonic-gate 		    (strcmp((name + MSG_SCN_STAB_SIZE),
6084716Sab196087 		    MSG_ORIG(MSG_SCN_EXCL)) == 0))
6090Sstevel@tonic-gate 			return (1);
6100Sstevel@tonic-gate 
6110Sstevel@tonic-gate 		if (strcmp((name + MSG_SCN_STAB_SIZE),
6120Sstevel@tonic-gate 		    MSG_ORIG(MSG_SCN_INDEX)) == 0)
6130Sstevel@tonic-gate 			stab_index = 1;
6140Sstevel@tonic-gate 	}
6150Sstevel@tonic-gate 
6160Sstevel@tonic-gate 	if ((ofl->ofl_flags & FLG_OF_STRIP) && ident) {
6170Sstevel@tonic-gate 		if ((strncmp(name, MSG_ORIG(MSG_SCN_DEBUG),
6180Sstevel@tonic-gate 		    MSG_SCN_DEBUG_SIZE) == 0) ||
6190Sstevel@tonic-gate 		    (strcmp(name, MSG_ORIG(MSG_SCN_LINE)) == 0))
6200Sstevel@tonic-gate 			return (1);
6210Sstevel@tonic-gate 	}
6220Sstevel@tonic-gate 
6230Sstevel@tonic-gate 	/*
6240Sstevel@tonic-gate 	 * Update the ident to reflect the type of section we've got.
6250Sstevel@tonic-gate 	 *
6260Sstevel@tonic-gate 	 * If there is any .plt or .got section to generate we'll be creating
6270Sstevel@tonic-gate 	 * our own version, so don't allow any input sections of these types to
6280Sstevel@tonic-gate 	 * be added to the output section list (why a relocatable object would
6290Sstevel@tonic-gate 	 * have a .plt or .got is a mystery, but stranger things have occurred).
6309085SAli.Bahrami@Sun.COM 	 *
6319085SAli.Bahrami@Sun.COM 	 * If there are any unwind sections, and this is a platform that uses
6329085SAli.Bahrami@Sun.COM 	 * SHT_PROGBITS for unwind sections, then set their ident to reflect
6339085SAli.Bahrami@Sun.COM 	 * that.
6340Sstevel@tonic-gate 	 */
6350Sstevel@tonic-gate 	if (ident) {
6369085SAli.Bahrami@Sun.COM 		if (shdr->sh_flags & SHF_TLS) {
6376206Sab196087 			ident = ld_targ.t_id.id_tls;
6389085SAli.Bahrami@Sun.COM 		} else if ((shdr->sh_flags & ~ALL_SHF_IGNORE) ==
6399085SAli.Bahrami@Sun.COM 		    (SHF_ALLOC | SHF_EXECINSTR)) {
6406206Sab196087 			ident = ld_targ.t_id.id_text;
6419085SAli.Bahrami@Sun.COM 		} else if (shdr->sh_flags & SHF_ALLOC) {
6429085SAli.Bahrami@Sun.COM 			int done = 0;
6439085SAli.Bahrami@Sun.COM 
6449085SAli.Bahrami@Sun.COM 			if (name[0] == '.') {
6459085SAli.Bahrami@Sun.COM 				switch (name[1]) {
6469085SAli.Bahrami@Sun.COM 				case 'e':
6479085SAli.Bahrami@Sun.COM 					if ((ld_targ.t_m.m_sht_unwind ==
6489085SAli.Bahrami@Sun.COM 					    SHT_PROGBITS) &&
649*10454SAli.Bahrami@Sun.COM 					    is_name_cmp(name,
650*10454SAli.Bahrami@Sun.COM 					    MSG_ORIG(MSG_SCN_EHFRAME),
651*10454SAli.Bahrami@Sun.COM 					    MSG_SCN_EHFRAME_SIZE)) {
6529085SAli.Bahrami@Sun.COM 						ident = ld_targ.t_id.id_unwind;
6539085SAli.Bahrami@Sun.COM 						is_flags = FLG_IS_EHFRAME;
6549085SAli.Bahrami@Sun.COM 						done = 1;
6559085SAli.Bahrami@Sun.COM 					}
6569085SAli.Bahrami@Sun.COM 					break;
6579085SAli.Bahrami@Sun.COM 				case 'g':
658*10454SAli.Bahrami@Sun.COM 					if (is_name_cmp(name,
659*10454SAli.Bahrami@Sun.COM 					    MSG_ORIG(MSG_SCN_GOT),
660*10454SAli.Bahrami@Sun.COM 					    MSG_SCN_GOT_SIZE)) {
6619085SAli.Bahrami@Sun.COM 						ident = ld_targ.t_id.id_null;
6629085SAli.Bahrami@Sun.COM 						done = 1;
6639085SAli.Bahrami@Sun.COM 						break;
6649085SAli.Bahrami@Sun.COM 					}
6659085SAli.Bahrami@Sun.COM 					if ((ld_targ.t_m.m_sht_unwind ==
666*10454SAli.Bahrami@Sun.COM 					    SHT_PROGBITS) &&
667*10454SAli.Bahrami@Sun.COM 					    is_name_cmp(name,
668*10454SAli.Bahrami@Sun.COM 					    MSG_ORIG(MSG_SCN_GCC_X_TBL),
669*10454SAli.Bahrami@Sun.COM 					    MSG_SCN_GCC_X_TBL_SIZE)) {
6709085SAli.Bahrami@Sun.COM 						ident = ld_targ.t_id.id_unwind;
6719085SAli.Bahrami@Sun.COM 						done = 1;
6729085SAli.Bahrami@Sun.COM 						break;
6739085SAli.Bahrami@Sun.COM 					}
6749085SAli.Bahrami@Sun.COM 					break;
6759085SAli.Bahrami@Sun.COM 				case 'p':
676*10454SAli.Bahrami@Sun.COM 					if (is_name_cmp(name,
677*10454SAli.Bahrami@Sun.COM 					    MSG_ORIG(MSG_SCN_PLT),
678*10454SAli.Bahrami@Sun.COM 					    MSG_SCN_PLT_SIZE)) {
6799085SAli.Bahrami@Sun.COM 						ident = ld_targ.t_id.id_null;
6809085SAli.Bahrami@Sun.COM 						done = 1;
6819085SAli.Bahrami@Sun.COM 					}
6829085SAli.Bahrami@Sun.COM 					break;
6839085SAli.Bahrami@Sun.COM 				}
6849085SAli.Bahrami@Sun.COM 			}
6859085SAli.Bahrami@Sun.COM 			if (!done) {
6869085SAli.Bahrami@Sun.COM 				if (stab_index) {
6879085SAli.Bahrami@Sun.COM 					/*
6889085SAli.Bahrami@Sun.COM 					 * This is a work-around for x86
6899085SAli.Bahrami@Sun.COM 					 * compilers that have set SHF_ALLOC
6909085SAli.Bahrami@Sun.COM 					 * for the .stab.index section.
6919085SAli.Bahrami@Sun.COM 					 *
6929085SAli.Bahrami@Sun.COM 					 * Because of this, make sure that the
6939085SAli.Bahrami@Sun.COM 					 * .stab.index does not end up as the
6949085SAli.Bahrami@Sun.COM 					 * last section in the text segment.
6959085SAli.Bahrami@Sun.COM 					 * Older linkers can produce
6969085SAli.Bahrami@Sun.COM 					 * segmentation violations when they
6979085SAli.Bahrami@Sun.COM 					 * strip (ld -s) against a shared
6989085SAli.Bahrami@Sun.COM 					 * object whose last section in the
6999085SAli.Bahrami@Sun.COM 					 * text segment is a .stab.
7009085SAli.Bahrami@Sun.COM 					 */
7019085SAli.Bahrami@Sun.COM 					ident = ld_targ.t_id.id_interp;
7029085SAli.Bahrami@Sun.COM 				} else {
7039085SAli.Bahrami@Sun.COM 					ident = ld_targ.t_id.id_data;
7049085SAli.Bahrami@Sun.COM 				}
7059085SAli.Bahrami@Sun.COM 			}
7060Sstevel@tonic-gate 		} else
7076206Sab196087 			ident = ld_targ.t_id.id_note;
7080Sstevel@tonic-gate 	}
7099085SAli.Bahrami@Sun.COM 
7109085SAli.Bahrami@Sun.COM 	r = process_section(name, ifl, shdr, scn, ndx, ident, ofl);
7119085SAli.Bahrami@Sun.COM 
7129085SAli.Bahrami@Sun.COM 	/*
7139085SAli.Bahrami@Sun.COM 	 * On success, process_section() creates an input section descriptor.
7149085SAli.Bahrami@Sun.COM 	 * Now that it exists, we can add any pending input section flags.
7159085SAli.Bahrami@Sun.COM 	 */
7169085SAli.Bahrami@Sun.COM 	if ((is_flags != 0) && (r == 1))
7179085SAli.Bahrami@Sun.COM 		ifl->ifl_isdesc[ndx]->is_flags |= is_flags;
7189085SAli.Bahrami@Sun.COM 
7199085SAli.Bahrami@Sun.COM 	return (r);
7200Sstevel@tonic-gate }
7210Sstevel@tonic-gate 
7220Sstevel@tonic-gate /*
7230Sstevel@tonic-gate  * Handles the SHT_SUNW_{DEBUG,DEBUGSTR) sections.
7240Sstevel@tonic-gate  */
7251618Srie static uintptr_t
7260Sstevel@tonic-gate process_debug(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
7270Sstevel@tonic-gate     Word ndx, int ident, Ofl_desc *ofl)
7280Sstevel@tonic-gate {
7290Sstevel@tonic-gate 	/*
7300Sstevel@tonic-gate 	 * Debug information is discarded when the 'ld -s' flag is invoked.
7310Sstevel@tonic-gate 	 */
7320Sstevel@tonic-gate 	if (ofl->ofl_flags & FLG_OF_STRIP) {
7330Sstevel@tonic-gate 		return (1);
7340Sstevel@tonic-gate 	}
7350Sstevel@tonic-gate 	return (process_progbits(name, ifl, shdr, scn, ndx, ident, ofl));
7360Sstevel@tonic-gate }
7370Sstevel@tonic-gate 
7380Sstevel@tonic-gate /*
7390Sstevel@tonic-gate  * Process a nobits section.
7400Sstevel@tonic-gate  */
7411618Srie static uintptr_t
7420Sstevel@tonic-gate process_nobits(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
7430Sstevel@tonic-gate     Word ndx, int ident, Ofl_desc *ofl)
7440Sstevel@tonic-gate {
7450Sstevel@tonic-gate 	if (ident) {
7460Sstevel@tonic-gate 		if (shdr->sh_flags & SHF_TLS)
7476206Sab196087 			ident = ld_targ.t_id.id_tlsbss;
7486206Sab196087 #if	defined(_ELF64)
7496206Sab196087 		else if ((shdr->sh_flags & SHF_AMD64_LARGE) &&
7506206Sab196087 		    (ld_targ.t_m.m_mach == EM_AMD64))
7516206Sab196087 			ident = ld_targ.t_id.id_lbss;
752574Sseizo #endif
7530Sstevel@tonic-gate 		else
7546206Sab196087 			ident = ld_targ.t_id.id_bss;
7550Sstevel@tonic-gate 	}
7560Sstevel@tonic-gate 	return (process_section(name, ifl, shdr, scn, ndx, ident, ofl));
7570Sstevel@tonic-gate }
7580Sstevel@tonic-gate 
7590Sstevel@tonic-gate /*
7600Sstevel@tonic-gate  * Process a SHT_*_ARRAY section.
7610Sstevel@tonic-gate  */
7621618Srie static uintptr_t
7630Sstevel@tonic-gate process_array(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
7640Sstevel@tonic-gate     Word ndx, int ident, Ofl_desc *ofl)
7650Sstevel@tonic-gate {
7667463SRod.Evans@Sun.COM 	uintptr_t	error;
7670Sstevel@tonic-gate 
7680Sstevel@tonic-gate 	if (ident)
7696206Sab196087 		ident = ld_targ.t_id.id_array;
7700Sstevel@tonic-gate 
7717463SRod.Evans@Sun.COM 	error = process_section(name, ifl, shdr, scn, ndx, ident, ofl);
7727463SRod.Evans@Sun.COM 	if ((error == 0) || (error == S_ERROR))
7737463SRod.Evans@Sun.COM 		return (error);
7747463SRod.Evans@Sun.COM 
7757463SRod.Evans@Sun.COM 	return (1);
7767463SRod.Evans@Sun.COM }
7770Sstevel@tonic-gate 
7787463SRod.Evans@Sun.COM static uintptr_t
7797463SRod.Evans@Sun.COM /* ARGSUSED1 */
7807463SRod.Evans@Sun.COM array_process(Is_desc *isc, Ifl_desc *ifl, Ofl_desc *ofl)
7817463SRod.Evans@Sun.COM {
7827463SRod.Evans@Sun.COM 	Os_desc	*osp;
7837463SRod.Evans@Sun.COM 	Shdr	*shdr;
7847463SRod.Evans@Sun.COM 
7857463SRod.Evans@Sun.COM 	if ((isc == NULL) || ((osp = isc->is_osdesc) == NULL))
7860Sstevel@tonic-gate 		return (0);
7870Sstevel@tonic-gate 
7887463SRod.Evans@Sun.COM 	shdr = isc->is_shdr;
7897463SRod.Evans@Sun.COM 
7900Sstevel@tonic-gate 	if ((shdr->sh_type == SHT_FINI_ARRAY) &&
7917463SRod.Evans@Sun.COM 	    (ofl->ofl_osfiniarray == NULL))
7920Sstevel@tonic-gate 		ofl->ofl_osfiniarray = osp;
7930Sstevel@tonic-gate 	else if ((shdr->sh_type == SHT_INIT_ARRAY) &&
7947463SRod.Evans@Sun.COM 	    (ofl->ofl_osinitarray == NULL))
7950Sstevel@tonic-gate 		ofl->ofl_osinitarray = osp;
7960Sstevel@tonic-gate 	else if ((shdr->sh_type == SHT_PREINIT_ARRAY) &&
7977463SRod.Evans@Sun.COM 	    (ofl->ofl_ospreinitarray == NULL))
7980Sstevel@tonic-gate 		ofl->ofl_ospreinitarray = osp;
7990Sstevel@tonic-gate 
8000Sstevel@tonic-gate 	return (1);
8010Sstevel@tonic-gate }
8020Sstevel@tonic-gate 
8030Sstevel@tonic-gate /*
8040Sstevel@tonic-gate  * Process a SHT_SYMTAB_SHNDX section.
8050Sstevel@tonic-gate  */
8061618Srie static uintptr_t
8070Sstevel@tonic-gate process_sym_shndx(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
8080Sstevel@tonic-gate     Word ndx, int ident, Ofl_desc *ofl)
8090Sstevel@tonic-gate {
8100Sstevel@tonic-gate 	if (process_input(name, ifl, shdr, scn, ndx, ident, ofl) == S_ERROR)
8110Sstevel@tonic-gate 		return (S_ERROR);
8120Sstevel@tonic-gate 
8130Sstevel@tonic-gate 	/*
8140Sstevel@tonic-gate 	 * Have we already seen the related SYMTAB - if so verify it now.
8150Sstevel@tonic-gate 	 */
8160Sstevel@tonic-gate 	if (shdr->sh_link < ndx) {
8170Sstevel@tonic-gate 		Is_desc	*isp = ifl->ifl_isdesc[shdr->sh_link];
8180Sstevel@tonic-gate 
8190Sstevel@tonic-gate 		if ((isp == 0) || ((isp->is_shdr->sh_type != SHT_SYMTAB) &&
8200Sstevel@tonic-gate 		    (isp->is_shdr->sh_type != SHT_DYNSYM))) {
8211618Srie 			eprintf(ofl->ofl_lml, ERR_FATAL,
8229878SAli.Bahrami@Sun.COM 			    MSG_INTL(MSG_FIL_INVSHLINK), ifl->ifl_name,
8239878SAli.Bahrami@Sun.COM 			    EC_WORD(ndx), name, EC_XWORD(shdr->sh_link));
8240Sstevel@tonic-gate 			return (S_ERROR);
8250Sstevel@tonic-gate 		}
8260Sstevel@tonic-gate 		isp->is_symshndx = ifl->ifl_isdesc[ndx];
8270Sstevel@tonic-gate 	}
8280Sstevel@tonic-gate 	return (1);
8290Sstevel@tonic-gate }
8300Sstevel@tonic-gate 
8310Sstevel@tonic-gate /*
8320Sstevel@tonic-gate  * Final processing for SHT_SYMTAB_SHNDX section.
8330Sstevel@tonic-gate  */
8341618Srie static uintptr_t
8350Sstevel@tonic-gate /* ARGSUSED2 */
8360Sstevel@tonic-gate sym_shndx_process(Is_desc *isc, Ifl_desc *ifl, Ofl_desc *ofl)
8370Sstevel@tonic-gate {
8380Sstevel@tonic-gate 	if (isc->is_shdr->sh_link > isc->is_scnndx) {
8390Sstevel@tonic-gate 		Is_desc	*isp = ifl->ifl_isdesc[isc->is_shdr->sh_link];
8400Sstevel@tonic-gate 
8410Sstevel@tonic-gate 		if ((isp == 0) || ((isp->is_shdr->sh_type != SHT_SYMTAB) &&
8420Sstevel@tonic-gate 		    (isp->is_shdr->sh_type != SHT_DYNSYM))) {
8431618Srie 			eprintf(ofl->ofl_lml, ERR_FATAL,
8441618Srie 			    MSG_INTL(MSG_FIL_INVSHLINK), isc->is_file->ifl_name,
8459878SAli.Bahrami@Sun.COM 			    EC_WORD(isc->is_scnndx), isc->is_name,
8469878SAli.Bahrami@Sun.COM 			    EC_XWORD(isc->is_shdr->sh_link));
8470Sstevel@tonic-gate 			return (S_ERROR);
8480Sstevel@tonic-gate 		}
8490Sstevel@tonic-gate 		isp->is_symshndx = isc;
8500Sstevel@tonic-gate 	}
8510Sstevel@tonic-gate 	return (1);
8520Sstevel@tonic-gate }
8530Sstevel@tonic-gate 
8540Sstevel@tonic-gate /*
8550Sstevel@tonic-gate  * Process .dynamic section from a relocatable object.
8560Sstevel@tonic-gate  *
8570Sstevel@tonic-gate  * Note: That the .dynamic section is only considered interesting when
8580Sstevel@tonic-gate  *	 dlopen()ing a relocatable object (thus FLG_OF1_RELDYN can only get
8590Sstevel@tonic-gate  *	 set when libld is called from ld.so.1).
8600Sstevel@tonic-gate  */
8610Sstevel@tonic-gate /*ARGSUSED*/
8621618Srie static uintptr_t
8630Sstevel@tonic-gate process_rel_dynamic(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
8640Sstevel@tonic-gate     Word ndx, int ident, Ofl_desc *ofl)
8650Sstevel@tonic-gate {
8660Sstevel@tonic-gate 	Dyn		*dyn;
8670Sstevel@tonic-gate 	Elf_Scn		*strscn;
8680Sstevel@tonic-gate 	Elf_Data	*dp;
8690Sstevel@tonic-gate 	char		*str;
8700Sstevel@tonic-gate 
8710Sstevel@tonic-gate 	/*
8720Sstevel@tonic-gate 	 * Process .dynamic sections from relocatable objects ?
8730Sstevel@tonic-gate 	 */
8740Sstevel@tonic-gate 	if ((ofl->ofl_flags1 & FLG_OF1_RELDYN) == 0)
8750Sstevel@tonic-gate 		return (1);
8760Sstevel@tonic-gate 
8770Sstevel@tonic-gate 	/*
8780Sstevel@tonic-gate 	 * Find the string section associated with the .dynamic section.
8790Sstevel@tonic-gate 	 */
8800Sstevel@tonic-gate 	if ((strscn = elf_getscn(ifl->ifl_elf, shdr->sh_link)) == NULL) {
8811618Srie 		eprintf(ofl->ofl_lml, ERR_ELF, MSG_INTL(MSG_ELF_GETSCN),
8821618Srie 		    ifl->ifl_name);
8830Sstevel@tonic-gate 		ofl->ofl_flags |= FLG_OF_FATAL;
8840Sstevel@tonic-gate 		return (0);
8850Sstevel@tonic-gate 	}
8860Sstevel@tonic-gate 	dp = elf_getdata(strscn, NULL);
8870Sstevel@tonic-gate 	str = (char *)dp->d_buf;
8880Sstevel@tonic-gate 
8890Sstevel@tonic-gate 	/*
8900Sstevel@tonic-gate 	 * And get the .dynamic data
8910Sstevel@tonic-gate 	 */
8920Sstevel@tonic-gate 	dp = elf_getdata(scn, NULL);
8930Sstevel@tonic-gate 
8940Sstevel@tonic-gate 	for (dyn = (Dyn *)dp->d_buf; dyn->d_tag != DT_NULL; dyn++) {
8951109Srie 		Ifl_desc	*difl;
8960Sstevel@tonic-gate 
8970Sstevel@tonic-gate 		switch (dyn->d_tag) {
8980Sstevel@tonic-gate 		case DT_NEEDED:
8990Sstevel@tonic-gate 		case DT_USED:
9009131SRod.Evans@Sun.COM 			if (((difl = libld_calloc(1,
9019131SRod.Evans@Sun.COM 			    sizeof (Ifl_desc))) == NULL) ||
9029131SRod.Evans@Sun.COM 			    (aplist_append(&ofl->ofl_sos, difl,
9039131SRod.Evans@Sun.COM 			    AL_CNT_OFL_LIBS) == NULL))
9040Sstevel@tonic-gate 				return (S_ERROR);
9051109Srie 
9061109Srie 			difl->ifl_name = MSG_ORIG(MSG_STR_DYNAMIC);
9071109Srie 			difl->ifl_soname = str + (size_t)dyn->d_un.d_val;
9081109Srie 			difl->ifl_flags = FLG_IF_NEEDSTR;
9090Sstevel@tonic-gate 			break;
9100Sstevel@tonic-gate 		case DT_RPATH:
9110Sstevel@tonic-gate 		case DT_RUNPATH:
9120Sstevel@tonic-gate 			if ((ofl->ofl_rpath = add_string(ofl->ofl_rpath,
9130Sstevel@tonic-gate 			    (str + (size_t)dyn->d_un.d_val))) ==
9140Sstevel@tonic-gate 			    (const char *)S_ERROR)
9150Sstevel@tonic-gate 				return (S_ERROR);
9160Sstevel@tonic-gate 			break;
9174716Sab196087 		case DT_VERSYM:
9184716Sab196087 			/*
9194716Sab196087 			 * The Solaris ld does not put DT_VERSYM in the
9204716Sab196087 			 * dynamic section. If the object has DT_VERSYM,
9214716Sab196087 			 * then it must have been produced by the GNU ld,
9224716Sab196087 			 * and is using the GNU style of versioning.
9234716Sab196087 			 */
9244716Sab196087 			ifl->ifl_flags |= FLG_IF_GNUVER;
9254716Sab196087 			break;
9260Sstevel@tonic-gate 		}
9270Sstevel@tonic-gate 	}
9280Sstevel@tonic-gate 	return (1);
9290Sstevel@tonic-gate }
9300Sstevel@tonic-gate 
9310Sstevel@tonic-gate /*
9320Sstevel@tonic-gate  * Expand implicit references.  Dependencies can be specified in terms of the
9330Sstevel@tonic-gate  * $ORIGIN, $PLATFORM, $OSREL and $OSNAME tokens, either from their needed name,
9340Sstevel@tonic-gate  * or via a runpath.  In addition runpaths may also specify the $ISALIST token.
9350Sstevel@tonic-gate  *
9361109Srie  * Probably the most common reference to explicit dependencies (via -L) will be
9370Sstevel@tonic-gate  * sufficient to find any associated implicit dependencies, but just in case we
9380Sstevel@tonic-gate  * expand any occurrence of these known tokens here.
9390Sstevel@tonic-gate  *
9400Sstevel@tonic-gate  * Note, if any errors occur we simply return the original name.
9410Sstevel@tonic-gate  *
9420Sstevel@tonic-gate  * This code is remarkably similar to expand() in rtld/common/paths.c.
9430Sstevel@tonic-gate  */
9440Sstevel@tonic-gate static char		*platform = 0;
9450Sstevel@tonic-gate static size_t		platform_sz = 0;
9460Sstevel@tonic-gate static Isa_desc		*isa = 0;
9470Sstevel@tonic-gate static Uts_desc		*uts = 0;
9480Sstevel@tonic-gate 
9490Sstevel@tonic-gate static char *
9500Sstevel@tonic-gate expand(const char *parent, const char *name, char **next)
9510Sstevel@tonic-gate {
9520Sstevel@tonic-gate 	char		_name[PATH_MAX], *nptr, *_next;
9530Sstevel@tonic-gate 	const char	*optr;
9540Sstevel@tonic-gate 	size_t		nrem = PATH_MAX - 1;
9550Sstevel@tonic-gate 	int		expanded = 0, _expanded, isaflag = 0;
9560Sstevel@tonic-gate 
9570Sstevel@tonic-gate 	optr = name;
9580Sstevel@tonic-gate 	nptr = _name;
9590Sstevel@tonic-gate 
9600Sstevel@tonic-gate 	while (*optr) {
9610Sstevel@tonic-gate 		if (nrem == 0)
9620Sstevel@tonic-gate 			return ((char *)name);
9630Sstevel@tonic-gate 
9640Sstevel@tonic-gate 		if (*optr != '$') {
9650Sstevel@tonic-gate 			*nptr++ = *optr++, nrem--;
9660Sstevel@tonic-gate 			continue;
9670Sstevel@tonic-gate 		}
9680Sstevel@tonic-gate 
9690Sstevel@tonic-gate 		_expanded = 0;
9700Sstevel@tonic-gate 
9710Sstevel@tonic-gate 		if (strncmp(optr, MSG_ORIG(MSG_STR_ORIGIN),
9720Sstevel@tonic-gate 		    MSG_STR_ORIGIN_SIZE) == 0) {
9730Sstevel@tonic-gate 			char *eptr;
9740Sstevel@tonic-gate 
9750Sstevel@tonic-gate 			/*
9760Sstevel@tonic-gate 			 * For $ORIGIN, expansion is really just a concatenation
9770Sstevel@tonic-gate 			 * of the parents directory name.  For example, an
9781109Srie 			 * explicit dependency foo/bar/lib1.so with a dependency
9790Sstevel@tonic-gate 			 * on $ORIGIN/lib2.so would be expanded to
9800Sstevel@tonic-gate 			 * foo/bar/lib2.so.
9810Sstevel@tonic-gate 			 */
9820Sstevel@tonic-gate 			if ((eptr = strrchr(parent, '/')) == 0) {
9830Sstevel@tonic-gate 				*nptr++ = '.';
9840Sstevel@tonic-gate 				nrem--;
9850Sstevel@tonic-gate 			} else {
9860Sstevel@tonic-gate 				size_t	len = eptr - parent;
9870Sstevel@tonic-gate 
9880Sstevel@tonic-gate 				if (len >= nrem)
9890Sstevel@tonic-gate 					return ((char *)name);
9900Sstevel@tonic-gate 
9910Sstevel@tonic-gate 				(void) strncpy(nptr, parent, len);
9920Sstevel@tonic-gate 				nptr = nptr + len;
9930Sstevel@tonic-gate 				nrem -= len;
9940Sstevel@tonic-gate 			}
9950Sstevel@tonic-gate 			optr += MSG_STR_ORIGIN_SIZE;
9960Sstevel@tonic-gate 			expanded = _expanded = 1;
9970Sstevel@tonic-gate 
9980Sstevel@tonic-gate 		} else if (strncmp(optr, MSG_ORIG(MSG_STR_PLATFORM),
9990Sstevel@tonic-gate 		    MSG_STR_PLATFORM_SIZE) == 0) {
10000Sstevel@tonic-gate 			/*
10010Sstevel@tonic-gate 			 * Establish the platform from sysconf - like uname -i.
10020Sstevel@tonic-gate 			 */
10030Sstevel@tonic-gate 			if ((platform == 0) && (platform_sz == 0)) {
10040Sstevel@tonic-gate 				char	info[SYS_NMLN];
10050Sstevel@tonic-gate 				long	size;
10060Sstevel@tonic-gate 
10070Sstevel@tonic-gate 				size = sysinfo(SI_PLATFORM, info, SYS_NMLN);
10080Sstevel@tonic-gate 				if ((size != -1) &&
10090Sstevel@tonic-gate 				    (platform = libld_malloc((size_t)size))) {
10100Sstevel@tonic-gate 					(void) strcpy(platform, info);
10110Sstevel@tonic-gate 					platform_sz = (size_t)size - 1;
10120Sstevel@tonic-gate 				} else
10130Sstevel@tonic-gate 					platform_sz = 1;
10140Sstevel@tonic-gate 			}
10150Sstevel@tonic-gate 			if (platform != 0) {
10160Sstevel@tonic-gate 				if (platform_sz >= nrem)
10170Sstevel@tonic-gate 					return ((char *)name);
10180Sstevel@tonic-gate 
10190Sstevel@tonic-gate 				(void) strncpy(nptr, platform, platform_sz);
10200Sstevel@tonic-gate 				nptr = nptr + platform_sz;
10210Sstevel@tonic-gate 				nrem -= platform_sz;
10220Sstevel@tonic-gate 
10230Sstevel@tonic-gate 				optr += MSG_STR_PLATFORM_SIZE;
10240Sstevel@tonic-gate 				expanded = _expanded = 1;
10250Sstevel@tonic-gate 			}
10260Sstevel@tonic-gate 
10270Sstevel@tonic-gate 		} else if (strncmp(optr, MSG_ORIG(MSG_STR_OSNAME),
10280Sstevel@tonic-gate 		    MSG_STR_OSNAME_SIZE) == 0) {
10290Sstevel@tonic-gate 			/*
10300Sstevel@tonic-gate 			 * Establish the os name - like uname -s.
10310Sstevel@tonic-gate 			 */
10320Sstevel@tonic-gate 			if (uts == 0)
10330Sstevel@tonic-gate 				uts = conv_uts();
10340Sstevel@tonic-gate 
10350Sstevel@tonic-gate 			if (uts && uts->uts_osnamesz) {
10360Sstevel@tonic-gate 				if (uts->uts_osnamesz >= nrem)
10370Sstevel@tonic-gate 					return ((char *)name);
10380Sstevel@tonic-gate 
10390Sstevel@tonic-gate 				(void) strncpy(nptr, uts->uts_osname,
10400Sstevel@tonic-gate 				    uts->uts_osnamesz);
10410Sstevel@tonic-gate 				nptr = nptr + uts->uts_osnamesz;
10420Sstevel@tonic-gate 				nrem -= uts->uts_osnamesz;
10430Sstevel@tonic-gate 
10440Sstevel@tonic-gate 				optr += MSG_STR_OSNAME_SIZE;
10450Sstevel@tonic-gate 				expanded = _expanded = 1;
10460Sstevel@tonic-gate 			}
10470Sstevel@tonic-gate 
10480Sstevel@tonic-gate 		} else if (strncmp(optr, MSG_ORIG(MSG_STR_OSREL),
10490Sstevel@tonic-gate 		    MSG_STR_OSREL_SIZE) == 0) {
10500Sstevel@tonic-gate 			/*
10510Sstevel@tonic-gate 			 * Establish the os release - like uname -r.
10520Sstevel@tonic-gate 			 */
10530Sstevel@tonic-gate 			if (uts == 0)
10540Sstevel@tonic-gate 				uts = conv_uts();
10550Sstevel@tonic-gate 
10560Sstevel@tonic-gate 			if (uts && uts->uts_osrelsz) {
10570Sstevel@tonic-gate 				if (uts->uts_osrelsz >= nrem)
10580Sstevel@tonic-gate 					return ((char *)name);
10590Sstevel@tonic-gate 
10600Sstevel@tonic-gate 				(void) strncpy(nptr, uts->uts_osrel,
10610Sstevel@tonic-gate 				    uts->uts_osrelsz);
10620Sstevel@tonic-gate 				nptr = nptr + uts->uts_osrelsz;
10630Sstevel@tonic-gate 				nrem -= uts->uts_osrelsz;
10640Sstevel@tonic-gate 
10650Sstevel@tonic-gate 				optr += MSG_STR_OSREL_SIZE;
10660Sstevel@tonic-gate 				expanded = _expanded = 1;
10670Sstevel@tonic-gate 			}
10680Sstevel@tonic-gate 
10690Sstevel@tonic-gate 		} else if ((strncmp(optr, MSG_ORIG(MSG_STR_ISALIST),
10700Sstevel@tonic-gate 		    MSG_STR_ISALIST_SIZE) == 0) && next && (isaflag++ == 0)) {
10710Sstevel@tonic-gate 			/*
10720Sstevel@tonic-gate 			 * Establish instruction sets from sysconf.  Note that
10730Sstevel@tonic-gate 			 * this is only meaningful from runpaths.
10740Sstevel@tonic-gate 			 */
10750Sstevel@tonic-gate 			if (isa == 0)
10760Sstevel@tonic-gate 				isa = conv_isalist();
10770Sstevel@tonic-gate 
10780Sstevel@tonic-gate 			if (isa && isa->isa_listsz &&
10790Sstevel@tonic-gate 			    (nrem > isa->isa_opt->isa_namesz)) {
10800Sstevel@tonic-gate 				size_t		mlen, tlen, hlen = optr - name;
10810Sstevel@tonic-gate 				size_t		no;
10820Sstevel@tonic-gate 				char		*lptr;
10830Sstevel@tonic-gate 				Isa_opt		*opt = isa->isa_opt;
10840Sstevel@tonic-gate 
10850Sstevel@tonic-gate 				(void) strncpy(nptr, opt->isa_name,
10860Sstevel@tonic-gate 				    opt->isa_namesz);
10870Sstevel@tonic-gate 				nptr = nptr + opt->isa_namesz;
10880Sstevel@tonic-gate 				nrem -= opt->isa_namesz;
10890Sstevel@tonic-gate 
10900Sstevel@tonic-gate 				optr += MSG_STR_ISALIST_SIZE;
10910Sstevel@tonic-gate 				expanded = _expanded = 1;
10920Sstevel@tonic-gate 
10930Sstevel@tonic-gate 				tlen = strlen(optr);
10940Sstevel@tonic-gate 
10950Sstevel@tonic-gate 				/*
10960Sstevel@tonic-gate 				 * As ISALIST expands to a number of elements,
10970Sstevel@tonic-gate 				 * establish a new list to return to the caller.
10980Sstevel@tonic-gate 				 * This will contain the present path being
10990Sstevel@tonic-gate 				 * processed redefined for each isalist option,
11000Sstevel@tonic-gate 				 * plus the original remaining list entries.
11010Sstevel@tonic-gate 				 */
11020Sstevel@tonic-gate 				mlen = ((hlen + tlen) * (isa->isa_optno - 1)) +
11030Sstevel@tonic-gate 				    isa->isa_listsz - opt->isa_namesz;
11040Sstevel@tonic-gate 				if (*next)
11054716Sab196087 					mlen += strlen(*next);
11060Sstevel@tonic-gate 				if ((_next = lptr = libld_malloc(mlen)) == 0)
11070Sstevel@tonic-gate 					return (0);
11080Sstevel@tonic-gate 
11090Sstevel@tonic-gate 				for (no = 1, opt++; no < isa->isa_optno;
11100Sstevel@tonic-gate 				    no++, opt++) {
11110Sstevel@tonic-gate 					(void) strncpy(lptr, name, hlen);
11120Sstevel@tonic-gate 					lptr = lptr + hlen;
11130Sstevel@tonic-gate 					(void) strncpy(lptr, opt->isa_name,
11140Sstevel@tonic-gate 					    opt->isa_namesz);
11150Sstevel@tonic-gate 					lptr = lptr + opt->isa_namesz;
11160Sstevel@tonic-gate 					(void) strncpy(lptr, optr, tlen);
11170Sstevel@tonic-gate 					lptr = lptr + tlen;
11180Sstevel@tonic-gate 					*lptr++ = ':';
11190Sstevel@tonic-gate 				}
11200Sstevel@tonic-gate 				if (*next)
11210Sstevel@tonic-gate 					(void) strcpy(lptr, *next);
11220Sstevel@tonic-gate 				else
11230Sstevel@tonic-gate 					*--lptr = '\0';
11240Sstevel@tonic-gate 			}
11250Sstevel@tonic-gate 		}
11260Sstevel@tonic-gate 
11270Sstevel@tonic-gate 		/*
11280Sstevel@tonic-gate 		 * If no expansion occurred skip the $ and continue.
11290Sstevel@tonic-gate 		 */
11300Sstevel@tonic-gate 		if (_expanded == 0)
11310Sstevel@tonic-gate 			*nptr++ = *optr++, nrem--;
11320Sstevel@tonic-gate 	}
11330Sstevel@tonic-gate 
11340Sstevel@tonic-gate 	/*
11350Sstevel@tonic-gate 	 * If any ISALIST processing has occurred not only do we return the
11360Sstevel@tonic-gate 	 * expanded node we're presently working on, but we must also update the
11370Sstevel@tonic-gate 	 * remaining list so that it is effectively prepended with this node
11380Sstevel@tonic-gate 	 * expanded to all remaining isalist options.  Note that we can only
11390Sstevel@tonic-gate 	 * handle one ISALIST per node.  For more than one ISALIST to be
11400Sstevel@tonic-gate 	 * processed we'd need a better algorithm than above to replace the
11410Sstevel@tonic-gate 	 * newly generated list.  Whether we want to encourage the number of
11420Sstevel@tonic-gate 	 * pathname permutations this would provide is another question. So, for
11430Sstevel@tonic-gate 	 * now if more than one ISALIST is encountered we return the original
11440Sstevel@tonic-gate 	 * node untouched.
11450Sstevel@tonic-gate 	 */
11460Sstevel@tonic-gate 	if (isaflag) {
11470Sstevel@tonic-gate 		if (isaflag == 1)
11480Sstevel@tonic-gate 			*next = _next;
11490Sstevel@tonic-gate 		else
11500Sstevel@tonic-gate 			return ((char *)name);
11510Sstevel@tonic-gate 	}
11520Sstevel@tonic-gate 
11530Sstevel@tonic-gate 	*nptr = '\0';
11540Sstevel@tonic-gate 
11550Sstevel@tonic-gate 	if (expanded) {
11560Sstevel@tonic-gate 		if ((nptr = libld_malloc(strlen(_name) + 1)) == 0)
11570Sstevel@tonic-gate 			return ((char *)name);
11580Sstevel@tonic-gate 		(void) strcpy(nptr, _name);
11590Sstevel@tonic-gate 		return (nptr);
11600Sstevel@tonic-gate 	}
11610Sstevel@tonic-gate 	return ((char *)name);
11620Sstevel@tonic-gate }
11630Sstevel@tonic-gate 
11640Sstevel@tonic-gate /*
11654716Sab196087  * The Solaris ld does not put DT_VERSYM in the dynamic section, but the
11664716Sab196087  * GNU ld does, and it is used by the runtime linker to implement their
11674716Sab196087  * versioning scheme. Use this fact to determine if the sharable object
11684716Sab196087  * was produced by the GNU ld rather than the Solaris one, and to set
11694716Sab196087  * FLG_IF_GNUVER if so. This needs to be done before the symbols are
11704716Sab196087  * processed, since the answer determines whether we interpret the
11714716Sab196087  * symbols versions according to Solaris or GNU rules.
11724716Sab196087  */
11734716Sab196087 /*ARGSUSED*/
11744716Sab196087 static uintptr_t
11754716Sab196087 process_dynamic_isgnu(const char *name, Ifl_desc *ifl, Shdr *shdr,
11764716Sab196087     Elf_Scn *scn, Word ndx, int ident, Ofl_desc *ofl)
11774716Sab196087 {
11784716Sab196087 	Dyn		*dyn;
11794716Sab196087 	Elf_Data	*dp;
11807463SRod.Evans@Sun.COM 	uintptr_t	error;
11814716Sab196087 
11827463SRod.Evans@Sun.COM 	error = process_section(name, ifl, shdr, scn, ndx, ident, ofl);
11837463SRod.Evans@Sun.COM 	if ((error == 0) || (error == S_ERROR))
11847463SRod.Evans@Sun.COM 		return (error);
11854716Sab196087 
11864716Sab196087 	/* Get the .dynamic data */
11874716Sab196087 	dp = elf_getdata(scn, NULL);
11884716Sab196087 
11894716Sab196087 	for (dyn = (Dyn *)dp->d_buf; dyn->d_tag != DT_NULL; dyn++) {
11904716Sab196087 		if (dyn->d_tag == DT_VERSYM) {
11914716Sab196087 			ifl->ifl_flags |= FLG_IF_GNUVER;
11924716Sab196087 			break;
11934716Sab196087 		}
11944716Sab196087 	}
11954716Sab196087 	return (1);
11964716Sab196087 }
11974716Sab196087 
11984716Sab196087 /*
11990Sstevel@tonic-gate  * Process a dynamic section.  If we are processing an explicit shared object
12000Sstevel@tonic-gate  * then we need to determine if it has a recorded SONAME, if so, this name will
12010Sstevel@tonic-gate  * be recorded in the output file being generated as the NEEDED entry rather
12020Sstevel@tonic-gate  * than the shared objects filename itself.
12030Sstevel@tonic-gate  * If the mode of the link-edit indicates that no undefined symbols should
12040Sstevel@tonic-gate  * remain, then we also need to build up a list of any additional shared object
12050Sstevel@tonic-gate  * dependencies this object may have.  In this case save any NEEDED entries
12060Sstevel@tonic-gate  * together with any associated run-path specifications.  This information is
12070Sstevel@tonic-gate  * recorded on the `ofl_soneed' list and will be analyzed after all explicit
12080Sstevel@tonic-gate  * file processing has been completed (refer finish_libs()).
12090Sstevel@tonic-gate  */
12101618Srie static uintptr_t
12110Sstevel@tonic-gate process_dynamic(Is_desc *isc, Ifl_desc *ifl, Ofl_desc *ofl)
12120Sstevel@tonic-gate {
12130Sstevel@tonic-gate 	Dyn		*data, *dyn;
12140Sstevel@tonic-gate 	char		*str, *rpath = NULL;
12150Sstevel@tonic-gate 	const char	*soname, *needed;
12160Sstevel@tonic-gate 
12170Sstevel@tonic-gate 	data = (Dyn *)isc->is_indata->d_buf;
12180Sstevel@tonic-gate 	str = (char *)ifl->ifl_isdesc[isc->is_shdr->sh_link]->is_indata->d_buf;
12190Sstevel@tonic-gate 
12200Sstevel@tonic-gate 	/*
12210Sstevel@tonic-gate 	 * First loop through the dynamic section looking for a run path.
12220Sstevel@tonic-gate 	 */
12230Sstevel@tonic-gate 	if (ofl->ofl_flags & (FLG_OF_NOUNDEF | FLG_OF_SYMBOLIC)) {
12240Sstevel@tonic-gate 		for (dyn = data; dyn->d_tag != DT_NULL; dyn++) {
12250Sstevel@tonic-gate 			if ((dyn->d_tag != DT_RPATH) &&
12260Sstevel@tonic-gate 			    (dyn->d_tag != DT_RUNPATH))
12270Sstevel@tonic-gate 				continue;
12280Sstevel@tonic-gate 			if ((rpath = str + (size_t)dyn->d_un.d_val) == NULL)
12290Sstevel@tonic-gate 				continue;
12300Sstevel@tonic-gate 			break;
12310Sstevel@tonic-gate 		}
12320Sstevel@tonic-gate 	}
12330Sstevel@tonic-gate 
12340Sstevel@tonic-gate 	/*
12350Sstevel@tonic-gate 	 * Now look for any needed dependencies (which may use the rpath)
12360Sstevel@tonic-gate 	 * or a new SONAME.
12370Sstevel@tonic-gate 	 */
12380Sstevel@tonic-gate 	for (dyn = data; dyn->d_tag != DT_NULL; dyn++) {
12390Sstevel@tonic-gate 		if (dyn->d_tag == DT_SONAME) {
12400Sstevel@tonic-gate 			if ((soname = str + (size_t)dyn->d_un.d_val) == NULL)
12410Sstevel@tonic-gate 				continue;
12420Sstevel@tonic-gate 
12430Sstevel@tonic-gate 			/*
12440Sstevel@tonic-gate 			 * Update the input file structure with this new name.
12450Sstevel@tonic-gate 			 */
12460Sstevel@tonic-gate 			ifl->ifl_soname = soname;
12470Sstevel@tonic-gate 
12480Sstevel@tonic-gate 		} else if ((dyn->d_tag == DT_NEEDED) ||
12490Sstevel@tonic-gate 		    (dyn->d_tag == DT_USED)) {
12509131SRod.Evans@Sun.COM 			Sdf_desc	*sdf;
12519131SRod.Evans@Sun.COM 
12520Sstevel@tonic-gate 			if (!(ofl->ofl_flags &
12530Sstevel@tonic-gate 			    (FLG_OF_NOUNDEF | FLG_OF_SYMBOLIC)))
12540Sstevel@tonic-gate 				continue;
12550Sstevel@tonic-gate 			if ((needed = str + (size_t)dyn->d_un.d_val) == NULL)
12560Sstevel@tonic-gate 				continue;
12570Sstevel@tonic-gate 
12580Sstevel@tonic-gate 			/*
12590Sstevel@tonic-gate 			 * Determine if this needed entry is already recorded on
12600Sstevel@tonic-gate 			 * the shared object needed list, if not create a new
12610Sstevel@tonic-gate 			 * definition for later processing (see finish_libs()).
12620Sstevel@tonic-gate 			 */
12639131SRod.Evans@Sun.COM 			needed = expand(ifl->ifl_name, needed, NULL);
12640Sstevel@tonic-gate 
12659131SRod.Evans@Sun.COM 			if ((sdf = sdf_find(needed, ofl->ofl_soneed)) == NULL) {
12660Sstevel@tonic-gate 				if ((sdf = sdf_add(needed,
12670Sstevel@tonic-gate 				    &ofl->ofl_soneed)) == (Sdf_desc *)S_ERROR)
12680Sstevel@tonic-gate 					return (S_ERROR);
12690Sstevel@tonic-gate 				sdf->sdf_rfile = ifl->ifl_name;
12700Sstevel@tonic-gate 			}
12710Sstevel@tonic-gate 
12720Sstevel@tonic-gate 			/*
12730Sstevel@tonic-gate 			 * Record the runpath (Note that we take the first
12740Sstevel@tonic-gate 			 * runpath which is exactly what ld.so.1 would do during
12750Sstevel@tonic-gate 			 * its dependency processing).
12760Sstevel@tonic-gate 			 */
12770Sstevel@tonic-gate 			if (rpath && (sdf->sdf_rpath == 0))
12780Sstevel@tonic-gate 				sdf->sdf_rpath = rpath;
12790Sstevel@tonic-gate 
12800Sstevel@tonic-gate 		} else if (dyn->d_tag == DT_FLAGS_1) {
12810Sstevel@tonic-gate 			if (dyn->d_un.d_val & (DF_1_INITFIRST | DF_1_INTERPOSE))
12820Sstevel@tonic-gate 				ifl->ifl_flags &= ~FLG_IF_LAZYLD;
12830Sstevel@tonic-gate 			if (dyn->d_un.d_val & DF_1_DISPRELPND)
12840Sstevel@tonic-gate 				ifl->ifl_flags |= FLG_IF_DISPPEND;
12850Sstevel@tonic-gate 			if (dyn->d_un.d_val & DF_1_DISPRELDNE)
12860Sstevel@tonic-gate 				ifl->ifl_flags |= FLG_IF_DISPDONE;
12870Sstevel@tonic-gate 			if (dyn->d_un.d_val & DF_1_NODIRECT)
12880Sstevel@tonic-gate 				ifl->ifl_flags |= FLG_IF_NODIRECT;
12890Sstevel@tonic-gate 
12900Sstevel@tonic-gate 		} else if ((dyn->d_tag == DT_AUDIT) &&
12910Sstevel@tonic-gate 		    (ifl->ifl_flags & FLG_IF_NEEDED)) {
12920Sstevel@tonic-gate 			/*
12930Sstevel@tonic-gate 			 * Record audit string as DT_DEPAUDIT.
12940Sstevel@tonic-gate 			 */
12950Sstevel@tonic-gate 			if ((ofl->ofl_depaudit = add_string(ofl->ofl_depaudit,
12960Sstevel@tonic-gate 			    (str + (size_t)dyn->d_un.d_val))) ==
12970Sstevel@tonic-gate 			    (const char *)S_ERROR)
12980Sstevel@tonic-gate 				return (S_ERROR);
12990Sstevel@tonic-gate 
13000Sstevel@tonic-gate 		} else if (dyn->d_tag == DT_SUNW_RTLDINF) {
13010Sstevel@tonic-gate 			/*
13020Sstevel@tonic-gate 			 * If a library has the SUNW_RTLDINF .dynamic entry
13030Sstevel@tonic-gate 			 * then we must not permit lazyloading of this library.
13040Sstevel@tonic-gate 			 * This is because critical startup information (TLS
13050Sstevel@tonic-gate 			 * routines) are provided as part of these interfaces
13060Sstevel@tonic-gate 			 * and we must have them as part of process startup.
13070Sstevel@tonic-gate 			 */
13080Sstevel@tonic-gate 			ifl->ifl_flags &= ~FLG_IF_LAZYLD;
13090Sstevel@tonic-gate 		}
13100Sstevel@tonic-gate 	}
13110Sstevel@tonic-gate 
13120Sstevel@tonic-gate 	/*
13130Sstevel@tonic-gate 	 * Perform some SONAME sanity checks.
13140Sstevel@tonic-gate 	 */
13150Sstevel@tonic-gate 	if (ifl->ifl_flags & FLG_IF_NEEDED) {
13167463SRod.Evans@Sun.COM 		Ifl_desc	*sifl;
13179131SRod.Evans@Sun.COM 		Aliste		idx;
13180Sstevel@tonic-gate 
13190Sstevel@tonic-gate 		/*
13200Sstevel@tonic-gate 		 * Determine if anyone else will cause the same SONAME to be
13210Sstevel@tonic-gate 		 * used (this is either caused by two different files having the
13220Sstevel@tonic-gate 		 * same SONAME, or by one file SONAME actually matching another
13230Sstevel@tonic-gate 		 * file basename (if no SONAME is specified within a shared
13240Sstevel@tonic-gate 		 * library its basename will be used)). Probably rare, but some
13250Sstevel@tonic-gate 		 * idiot will do it.
13260Sstevel@tonic-gate 		 */
13279131SRod.Evans@Sun.COM 		for (APLIST_TRAVERSE(ofl->ofl_sos, idx, sifl)) {
13280Sstevel@tonic-gate 			if ((strcmp(ifl->ifl_soname, sifl->ifl_soname) == 0) &&
13290Sstevel@tonic-gate 			    (ifl != sifl)) {
13300Sstevel@tonic-gate 				const char	*hint, *iflb, *siflb;
13310Sstevel@tonic-gate 
13320Sstevel@tonic-gate 				/*
13330Sstevel@tonic-gate 				 * Determine the basename of each file. Perhaps
13340Sstevel@tonic-gate 				 * there are multiple copies of the same file
13350Sstevel@tonic-gate 				 * being brought in using different -L search
13360Sstevel@tonic-gate 				 * paths, and if so give an extra hint in the
13370Sstevel@tonic-gate 				 * error message.
13380Sstevel@tonic-gate 				 */
13390Sstevel@tonic-gate 				iflb = strrchr(ifl->ifl_name, '/');
13400Sstevel@tonic-gate 				if (iflb == NULL)
13410Sstevel@tonic-gate 					iflb = ifl->ifl_name;
13420Sstevel@tonic-gate 				else
13430Sstevel@tonic-gate 					iflb++;
13440Sstevel@tonic-gate 
13450Sstevel@tonic-gate 				siflb = strrchr(sifl->ifl_name, '/');
13460Sstevel@tonic-gate 				if (siflb == NULL)
13470Sstevel@tonic-gate 					siflb = sifl->ifl_name;
13480Sstevel@tonic-gate 				else
13490Sstevel@tonic-gate 					siflb++;
13500Sstevel@tonic-gate 
13510Sstevel@tonic-gate 				if (strcmp(iflb, siflb) == 0)
13520Sstevel@tonic-gate 					hint = MSG_INTL(MSG_REC_CNFLTHINT);
13530Sstevel@tonic-gate 				else
13540Sstevel@tonic-gate 					hint = MSG_ORIG(MSG_STR_EMPTY);
13550Sstevel@tonic-gate 
13561618Srie 				eprintf(ofl->ofl_lml, ERR_FATAL,
13571618Srie 				    MSG_INTL(MSG_REC_OBJCNFLT), sifl->ifl_name,
13581618Srie 				    ifl->ifl_name, sifl->ifl_soname, hint);
13590Sstevel@tonic-gate 				ofl->ofl_flags |= FLG_OF_FATAL;
13600Sstevel@tonic-gate 				return (0);
13610Sstevel@tonic-gate 			}
13620Sstevel@tonic-gate 		}
13630Sstevel@tonic-gate 
13640Sstevel@tonic-gate 		/*
13650Sstevel@tonic-gate 		 * If the SONAME is the same as the name the user wishes to
13660Sstevel@tonic-gate 		 * record when building a dynamic library (refer -h option),
13670Sstevel@tonic-gate 		 * we also have a name clash.
13680Sstevel@tonic-gate 		 */
13690Sstevel@tonic-gate 		if (ofl->ofl_soname &&
13700Sstevel@tonic-gate 		    (strcmp(ofl->ofl_soname, ifl->ifl_soname) == 0)) {
13711618Srie 			eprintf(ofl->ofl_lml, ERR_FATAL,
13721618Srie 			    MSG_INTL(MSG_REC_OPTCNFLT), ifl->ifl_name,
13737983SAli.Bahrami@Sun.COM 			    MSG_INTL(MSG_MARG_SONAME), ifl->ifl_soname);
13740Sstevel@tonic-gate 			ofl->ofl_flags |= FLG_OF_FATAL;
13750Sstevel@tonic-gate 			return (0);
13760Sstevel@tonic-gate 		}
13770Sstevel@tonic-gate 	}
13780Sstevel@tonic-gate 	return (1);
13790Sstevel@tonic-gate }
13800Sstevel@tonic-gate 
13810Sstevel@tonic-gate /*
13829085SAli.Bahrami@Sun.COM  * Process a progbits section from a relocatable object (ET_REL).
13839085SAli.Bahrami@Sun.COM  * This is used on non-amd64 objects to recognize .eh_frame sections.
13849085SAli.Bahrami@Sun.COM  */
13859085SAli.Bahrami@Sun.COM /*ARGSUSED1*/
13869085SAli.Bahrami@Sun.COM static uintptr_t
13879085SAli.Bahrami@Sun.COM process_progbits_final(Is_desc *isc, Ifl_desc *ifl, Ofl_desc *ofl)
13889085SAli.Bahrami@Sun.COM {
13899085SAli.Bahrami@Sun.COM 	if (isc->is_osdesc && (isc->is_flags & FLG_IS_EHFRAME) &&
13909085SAli.Bahrami@Sun.COM 	    (ld_unwind_register(isc->is_osdesc, ofl) == S_ERROR))
13919085SAli.Bahrami@Sun.COM 		return (S_ERROR);
13929085SAli.Bahrami@Sun.COM 
13939085SAli.Bahrami@Sun.COM 	return (1);
13949085SAli.Bahrami@Sun.COM }
13959085SAli.Bahrami@Sun.COM 
13969085SAli.Bahrami@Sun.COM /*
13977463SRod.Evans@Sun.COM  * Process a group section.
13987463SRod.Evans@Sun.COM  */
13997463SRod.Evans@Sun.COM static uintptr_t
14007463SRod.Evans@Sun.COM process_group(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
14017463SRod.Evans@Sun.COM     Word ndx, int ident, Ofl_desc *ofl)
14027463SRod.Evans@Sun.COM {
14037463SRod.Evans@Sun.COM 	uintptr_t	error;
14047463SRod.Evans@Sun.COM 
14057463SRod.Evans@Sun.COM 	error = process_section(name, ifl, shdr, scn, ndx, ident, ofl);
14067463SRod.Evans@Sun.COM 	if ((error == 0) || (error == S_ERROR))
14077463SRod.Evans@Sun.COM 		return (error);
14087463SRod.Evans@Sun.COM 
14097463SRod.Evans@Sun.COM 	/*
14107463SRod.Evans@Sun.COM 	 * Indicate that this input file has groups to process.  Groups are
14117463SRod.Evans@Sun.COM 	 * processed after all input sections have been processed.
14127463SRod.Evans@Sun.COM 	 */
14137463SRod.Evans@Sun.COM 	ifl->ifl_flags |= FLG_IS_GROUPS;
14147463SRod.Evans@Sun.COM 
14157463SRod.Evans@Sun.COM 	return (1);
14167463SRod.Evans@Sun.COM }
14177463SRod.Evans@Sun.COM 
14187463SRod.Evans@Sun.COM /*
14190Sstevel@tonic-gate  * Process a relocation entry. At this point all input sections from this
14200Sstevel@tonic-gate  * input file have been assigned an input section descriptor which is saved
14210Sstevel@tonic-gate  * in the `ifl_isdesc' array.
14220Sstevel@tonic-gate  */
14231618Srie static uintptr_t
14240Sstevel@tonic-gate rel_process(Is_desc *isc, Ifl_desc *ifl, Ofl_desc *ofl)
14250Sstevel@tonic-gate {
14260Sstevel@tonic-gate 	Word 	rndx;
14270Sstevel@tonic-gate 	Is_desc	*risc;
14280Sstevel@tonic-gate 	Os_desc	*osp;
14290Sstevel@tonic-gate 	Shdr	*shdr = isc->is_shdr;
14304734Sab196087 	Conv_inv_buf_t inv_buf;
14310Sstevel@tonic-gate 
14320Sstevel@tonic-gate 	/*
14330Sstevel@tonic-gate 	 * Make sure this is a valid relocation we can handle.
14340Sstevel@tonic-gate 	 */
14356206Sab196087 	if (shdr->sh_type != ld_targ.t_m.m_rel_sht_type) {
14361618Srie 		eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_FIL_INVALSEC),
14379878SAli.Bahrami@Sun.COM 		    ifl->ifl_name, EC_WORD(isc->is_scnndx), isc->is_name,
14389273SAli.Bahrami@Sun.COM 		    conv_sec_type(ifl->ifl_ehdr->e_ident[EI_OSABI],
14399273SAli.Bahrami@Sun.COM 		    ifl->ifl_ehdr->e_machine, shdr->sh_type, 0, &inv_buf));
14400Sstevel@tonic-gate 		ofl->ofl_flags |= FLG_OF_FATAL;
14410Sstevel@tonic-gate 		return (0);
14420Sstevel@tonic-gate 	}
14430Sstevel@tonic-gate 
14440Sstevel@tonic-gate 	/*
14450Sstevel@tonic-gate 	 * From the relocation section header information determine which
14460Sstevel@tonic-gate 	 * section needs the actual relocation.  Determine which output section
14470Sstevel@tonic-gate 	 * this input section has been assigned to and add to its relocation
14480Sstevel@tonic-gate 	 * list.  Note that the relocation section may be null if it is not
14490Sstevel@tonic-gate 	 * required (ie. .debug, .stabs, etc).
14500Sstevel@tonic-gate 	 */
14510Sstevel@tonic-gate 	rndx = shdr->sh_info;
14520Sstevel@tonic-gate 	if (rndx >= ifl->ifl_shnum) {
14530Sstevel@tonic-gate 		/*
14540Sstevel@tonic-gate 		 * Broken input file.
14550Sstevel@tonic-gate 		 */
14561618Srie 		eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_FIL_INVSHINFO),
14579878SAli.Bahrami@Sun.COM 		    ifl->ifl_name, EC_WORD(isc->is_scnndx), isc->is_name,
14589878SAli.Bahrami@Sun.COM 		    EC_XWORD(rndx));
14590Sstevel@tonic-gate 		ofl->ofl_flags |= FLG_OF_FATAL;
14600Sstevel@tonic-gate 		return (0);
14610Sstevel@tonic-gate 	}
14620Sstevel@tonic-gate 	if (rndx == 0) {
14639131SRod.Evans@Sun.COM 		if (aplist_append(&ofl->ofl_extrarels, isc,
14649131SRod.Evans@Sun.COM 		    AL_CNT_OFL_RELS) == NULL)
14650Sstevel@tonic-gate 			return (S_ERROR);
14669131SRod.Evans@Sun.COM 
14679131SRod.Evans@Sun.COM 	} else if ((risc = ifl->ifl_isdesc[rndx]) != NULL) {
14680Sstevel@tonic-gate 		/*
14690Sstevel@tonic-gate 		 * Discard relocations if they are against a section
14700Sstevel@tonic-gate 		 * which has been discarded.
14710Sstevel@tonic-gate 		 */
14720Sstevel@tonic-gate 		if (risc->is_flags & FLG_IS_DISCARD)
14730Sstevel@tonic-gate 			return (1);
14749131SRod.Evans@Sun.COM 
14759131SRod.Evans@Sun.COM 		if ((osp = risc->is_osdesc) == NULL) {
14760Sstevel@tonic-gate 			if (risc->is_shdr->sh_type == SHT_SUNW_move) {
14770Sstevel@tonic-gate 				/*
14789131SRod.Evans@Sun.COM 				 * This section is processed later in
14799131SRod.Evans@Sun.COM 				 * process_movereloc().
14800Sstevel@tonic-gate 				 */
14819131SRod.Evans@Sun.COM 				if (aplist_append(&ofl->ofl_ismoverel,
14829131SRod.Evans@Sun.COM 				    isc, AL_CNT_OFL_MOVE) == NULL)
14830Sstevel@tonic-gate 					return (S_ERROR);
14840Sstevel@tonic-gate 				return (1);
14850Sstevel@tonic-gate 			}
14861618Srie 			eprintf(ofl->ofl_lml, ERR_FATAL,
14871618Srie 			    MSG_INTL(MSG_FIL_INVRELOC1), ifl->ifl_name,
14889878SAli.Bahrami@Sun.COM 			    EC_WORD(isc->is_scnndx), isc->is_name,
14899878SAli.Bahrami@Sun.COM 			    EC_WORD(risc->is_scnndx), risc->is_name);
14900Sstevel@tonic-gate 			return (0);
14910Sstevel@tonic-gate 		}
14928608SAli.Bahrami@Sun.COM 		if (aplist_append(&osp->os_relisdescs, isc,
14938608SAli.Bahrami@Sun.COM 		    AL_CNT_OS_RELISDESCS) == NULL)
14940Sstevel@tonic-gate 			return (S_ERROR);
14950Sstevel@tonic-gate 	}
14960Sstevel@tonic-gate 	return (1);
14970Sstevel@tonic-gate }
14980Sstevel@tonic-gate 
14990Sstevel@tonic-gate /*
15000Sstevel@tonic-gate  * SHF_EXCLUDE flags is set for this section.
15010Sstevel@tonic-gate  */
15020Sstevel@tonic-gate static uintptr_t
15030Sstevel@tonic-gate process_exclude(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
15040Sstevel@tonic-gate     Word ndx, Ofl_desc *ofl)
15050Sstevel@tonic-gate {
15060Sstevel@tonic-gate 	/*
15070Sstevel@tonic-gate 	 * Sections SHT_SYMTAB and SHT_DYNDYM, even if SHF_EXCLUDE is on, might
15080Sstevel@tonic-gate 	 * be needed for ld processing.  These sections need to be in the
15090Sstevel@tonic-gate 	 * internal table.  Later it will be determined whether they can be
15100Sstevel@tonic-gate 	 * eliminated or not.
15110Sstevel@tonic-gate 	 */
15120Sstevel@tonic-gate 	if (shdr->sh_type == SHT_SYMTAB || shdr->sh_type == SHT_DYNSYM)
15130Sstevel@tonic-gate 		return (0);
15140Sstevel@tonic-gate 
15150Sstevel@tonic-gate 	/*
15160Sstevel@tonic-gate 	 * Other checks
15170Sstevel@tonic-gate 	 */
15180Sstevel@tonic-gate 	if (shdr->sh_flags & SHF_ALLOC) {
15190Sstevel@tonic-gate 		/*
15200Sstevel@tonic-gate 		 * A conflict, issue an warning message, and ignore the section.
15210Sstevel@tonic-gate 		 */
15221618Srie 		eprintf(ofl->ofl_lml, ERR_WARNING, MSG_INTL(MSG_FIL_EXCLUDE),
15239878SAli.Bahrami@Sun.COM 		    ifl->ifl_name, EC_WORD(ndx), name);
15240Sstevel@tonic-gate 		return (0);
15250Sstevel@tonic-gate 	}
15260Sstevel@tonic-gate 
15270Sstevel@tonic-gate 	/*
15280Sstevel@tonic-gate 	 * This sections is not going to the output file.
15290Sstevel@tonic-gate 	 */
15300Sstevel@tonic-gate 	return (process_section(name, ifl, shdr, scn, ndx, 0, ofl));
15310Sstevel@tonic-gate }
15320Sstevel@tonic-gate 
15330Sstevel@tonic-gate /*
15340Sstevel@tonic-gate  * Section processing state table.  `Initial' describes the required initial
15350Sstevel@tonic-gate  * procedure to be called (if any), `Final' describes the final processing
15360Sstevel@tonic-gate  * procedure (ie. things that can only be done when all required sections
15370Sstevel@tonic-gate  * have been collected).
15380Sstevel@tonic-gate  */
15399085SAli.Bahrami@Sun.COM typedef uintptr_t	(* initial_func_t)(const char *, Ifl_desc *, Shdr *,
15409085SAli.Bahrami@Sun.COM 			    Elf_Scn *, Word, int, Ofl_desc *);
15410Sstevel@tonic-gate 
15429085SAli.Bahrami@Sun.COM static initial_func_t Initial[SHT_NUM][2] = {
15430Sstevel@tonic-gate /*			ET_REL			ET_DYN			*/
15440Sstevel@tonic-gate 
15450Sstevel@tonic-gate /* SHT_NULL	*/	invalid_section,	invalid_section,
15460Sstevel@tonic-gate /* SHT_PROGBITS	*/	process_progbits,	process_progbits,
15470Sstevel@tonic-gate /* SHT_SYMTAB	*/	process_input,		process_input,
15480Sstevel@tonic-gate /* SHT_STRTAB	*/	process_strtab,		process_strtab,
15490Sstevel@tonic-gate /* SHT_RELA	*/	process_reloc,		process_reloc,
15500Sstevel@tonic-gate /* SHT_HASH	*/	invalid_section,	NULL,
15514716Sab196087 /* SHT_DYNAMIC	*/	process_rel_dynamic,	process_dynamic_isgnu,
15520Sstevel@tonic-gate /* SHT_NOTE	*/	process_section,	NULL,
15530Sstevel@tonic-gate /* SHT_NOBITS	*/	process_nobits,		process_nobits,
15540Sstevel@tonic-gate /* SHT_REL	*/	process_reloc,		process_reloc,
15550Sstevel@tonic-gate /* SHT_SHLIB	*/	process_section,	invalid_section,
15560Sstevel@tonic-gate /* SHT_DYNSYM	*/	invalid_section,	process_input,
15570Sstevel@tonic-gate /* SHT_UNKNOWN12 */	process_progbits,	process_progbits,
15580Sstevel@tonic-gate /* SHT_UNKNOWN13 */	process_progbits,	process_progbits,
15590Sstevel@tonic-gate /* SHT_INIT_ARRAY */	process_array,		NULL,
15600Sstevel@tonic-gate /* SHT_FINI_ARRAY */	process_array,		NULL,
15610Sstevel@tonic-gate /* SHT_PREINIT_ARRAY */	process_array,		NULL,
15627463SRod.Evans@Sun.COM /* SHT_GROUP */		process_group,		invalid_section,
15630Sstevel@tonic-gate /* SHT_SYMTAB_SHNDX */	process_sym_shndx,	NULL
15640Sstevel@tonic-gate };
15650Sstevel@tonic-gate 
15669085SAli.Bahrami@Sun.COM typedef uintptr_t	(* final_func_t)(Is_desc *, Ifl_desc *, Ofl_desc *);
15679085SAli.Bahrami@Sun.COM 
15689085SAli.Bahrami@Sun.COM static final_func_t Final[SHT_NUM][2] = {
15699085SAli.Bahrami@Sun.COM /*			ET_REL			ET_DYN			*/
15700Sstevel@tonic-gate 
15710Sstevel@tonic-gate /* SHT_NULL	*/	NULL,			NULL,
15729085SAli.Bahrami@Sun.COM /* SHT_PROGBITS	*/	process_progbits_final,	NULL,
15731618Srie /* SHT_SYMTAB	*/	ld_sym_process,		ld_sym_process,
15740Sstevel@tonic-gate /* SHT_STRTAB	*/	NULL,			NULL,
15750Sstevel@tonic-gate /* SHT_RELA	*/	rel_process,		NULL,
15760Sstevel@tonic-gate /* SHT_HASH	*/	NULL,			NULL,
15770Sstevel@tonic-gate /* SHT_DYNAMIC	*/	NULL,			process_dynamic,
15780Sstevel@tonic-gate /* SHT_NOTE	*/	NULL,			NULL,
15790Sstevel@tonic-gate /* SHT_NOBITS	*/	NULL,			NULL,
15800Sstevel@tonic-gate /* SHT_REL	*/	rel_process,		NULL,
15810Sstevel@tonic-gate /* SHT_SHLIB	*/	NULL,			NULL,
15821618Srie /* SHT_DYNSYM	*/	NULL,			ld_sym_process,
15830Sstevel@tonic-gate /* SHT_UNKNOWN12 */	NULL,			NULL,
15840Sstevel@tonic-gate /* SHT_UNKNOWN13 */	NULL,			NULL,
15857463SRod.Evans@Sun.COM /* SHT_INIT_ARRAY */	array_process,		NULL,
15867463SRod.Evans@Sun.COM /* SHT_FINI_ARRAY */	array_process,		NULL,
15877463SRod.Evans@Sun.COM /* SHT_PREINIT_ARRAY */	array_process,		NULL,
15880Sstevel@tonic-gate /* SHT_GROUP */		NULL,			NULL,
15890Sstevel@tonic-gate /* SHT_SYMTAB_SHNDX */	sym_shndx_process,	NULL
15900Sstevel@tonic-gate };
15910Sstevel@tonic-gate 
15920Sstevel@tonic-gate #define	MAXNDXSIZE	10
15930Sstevel@tonic-gate 
15940Sstevel@tonic-gate /*
15950Sstevel@tonic-gate  * Process an elf file.  Each section is compared against the section state
15960Sstevel@tonic-gate  * table to determine whether it should be processed (saved), ignored, or
15970Sstevel@tonic-gate  * is invalid for the type of input file being processed.
15980Sstevel@tonic-gate  */
15991618Srie static uintptr_t
16000Sstevel@tonic-gate process_elf(Ifl_desc *ifl, Elf *elf, Ofl_desc *ofl)
16010Sstevel@tonic-gate {
16020Sstevel@tonic-gate 	Elf_Scn		*scn;
16030Sstevel@tonic-gate 	Shdr		*shdr;
16047463SRod.Evans@Sun.COM 	Word		ndx, sndx, ordndx = 0, ordcnt = 0;
16059878SAli.Bahrami@Sun.COM 	char		*str, *name;
16060Sstevel@tonic-gate 	Word		row, column;
16070Sstevel@tonic-gate 	int		ident;
16080Sstevel@tonic-gate 	uintptr_t	error;
16093617Srie 	Is_desc		*vdfisp, *vndisp, *vsyisp, *sifisp, *capisp;
16100Sstevel@tonic-gate 	Sdf_desc	*sdf;
16110Sstevel@tonic-gate 
16120Sstevel@tonic-gate 	/*
16130Sstevel@tonic-gate 	 * First process the .shstrtab section so that later sections can
16140Sstevel@tonic-gate 	 * reference their name.
16150Sstevel@tonic-gate 	 */
16161618Srie 	ld_sup_file(ofl, ifl->ifl_name, elf_kind(elf), ifl->ifl_flags, elf);
16170Sstevel@tonic-gate 
16180Sstevel@tonic-gate 	sndx = ifl->ifl_shstrndx;
16190Sstevel@tonic-gate 	if ((scn = elf_getscn(elf, (size_t)sndx)) == NULL) {
16201618Srie 		eprintf(ofl->ofl_lml, ERR_ELF, MSG_INTL(MSG_ELF_GETSCN),
16211618Srie 		    ifl->ifl_name);
16220Sstevel@tonic-gate 		ofl->ofl_flags |= FLG_OF_FATAL;
16230Sstevel@tonic-gate 		return (0);
16240Sstevel@tonic-gate 	}
16250Sstevel@tonic-gate 	if ((shdr = elf_getshdr(scn)) == NULL) {
16261618Srie 		eprintf(ofl->ofl_lml, ERR_ELF, MSG_INTL(MSG_ELF_GETSHDR),
16271618Srie 		    ifl->ifl_name);
16280Sstevel@tonic-gate 		ofl->ofl_flags |= FLG_OF_FATAL;
16290Sstevel@tonic-gate 		return (0);
16300Sstevel@tonic-gate 	}
16310Sstevel@tonic-gate 	if ((name = elf_strptr(elf, (size_t)sndx, (size_t)shdr->sh_name)) ==
16320Sstevel@tonic-gate 	    NULL) {
16331618Srie 		eprintf(ofl->ofl_lml, ERR_ELF, MSG_INTL(MSG_ELF_STRPTR),
16341618Srie 		    ifl->ifl_name);
16350Sstevel@tonic-gate 		ofl->ofl_flags |= FLG_OF_FATAL;
16360Sstevel@tonic-gate 		return (0);
16370Sstevel@tonic-gate 	}
16380Sstevel@tonic-gate 
16392647Srie 	if (ld_sup_input_section(ofl, ifl, name, &shdr, sndx, scn,
16402647Srie 	    elf) == S_ERROR)
16410Sstevel@tonic-gate 		return (S_ERROR);
16420Sstevel@tonic-gate 
16430Sstevel@tonic-gate 	/*
16440Sstevel@tonic-gate 	 * Reset the name since the shdr->sh_name could have been changed as
16459878SAli.Bahrami@Sun.COM 	 * part of ld_sup_input_section().
16460Sstevel@tonic-gate 	 */
16479878SAli.Bahrami@Sun.COM 	if ((name = elf_strptr(elf, (size_t)sndx, (size_t)shdr->sh_name)) ==
16489878SAli.Bahrami@Sun.COM 	    NULL) {
16491618Srie 		eprintf(ofl->ofl_lml, ERR_ELF, MSG_INTL(MSG_ELF_STRPTR),
16501618Srie 		    ifl->ifl_name);
16510Sstevel@tonic-gate 		ofl->ofl_flags |= FLG_OF_FATAL;
16520Sstevel@tonic-gate 		return (0);
16530Sstevel@tonic-gate 	}
16540Sstevel@tonic-gate 
16550Sstevel@tonic-gate 	error = process_strtab(name, ifl, shdr, scn, sndx, FALSE, ofl);
16560Sstevel@tonic-gate 	if ((error == 0) || (error == S_ERROR))
16570Sstevel@tonic-gate 		return (error);
16580Sstevel@tonic-gate 	str = ifl->ifl_isdesc[sndx]->is_indata->d_buf;
16590Sstevel@tonic-gate 
16600Sstevel@tonic-gate 	/*
16610Sstevel@tonic-gate 	 * Determine the state table column from the input file type.  Note,
16620Sstevel@tonic-gate 	 * shared library sections are not added to the output section list.
16630Sstevel@tonic-gate 	 */
16640Sstevel@tonic-gate 	if (ifl->ifl_ehdr->e_type == ET_DYN) {
16650Sstevel@tonic-gate 		column = 1;
16660Sstevel@tonic-gate 		ofl->ofl_soscnt++;
16676206Sab196087 		ident = ld_targ.t_id.id_null;
16680Sstevel@tonic-gate 	} else {
16690Sstevel@tonic-gate 		column = 0;
16700Sstevel@tonic-gate 		ofl->ofl_objscnt++;
16716206Sab196087 		ident = ld_targ.t_id.id_unknown;
16720Sstevel@tonic-gate 	}
16730Sstevel@tonic-gate 
16741618Srie 	DBG_CALL(Dbg_file_generic(ofl->ofl_lml, ifl));
16750Sstevel@tonic-gate 	ndx = 0;
16760Sstevel@tonic-gate 	vdfisp = vndisp = vsyisp = sifisp = capisp = 0;
16770Sstevel@tonic-gate 	scn = NULL;
16780Sstevel@tonic-gate 	while (scn = elf_nextscn(elf, scn)) {
16790Sstevel@tonic-gate 		ndx++;
16807463SRod.Evans@Sun.COM 
16810Sstevel@tonic-gate 		/*
16820Sstevel@tonic-gate 		 * As we've already processed the .shstrtab don't do it again.
16830Sstevel@tonic-gate 		 */
16840Sstevel@tonic-gate 		if (ndx == sndx)
16850Sstevel@tonic-gate 			continue;
16860Sstevel@tonic-gate 
16870Sstevel@tonic-gate 		if ((shdr = elf_getshdr(scn)) == NULL) {
16881618Srie 			eprintf(ofl->ofl_lml, ERR_ELF,
16891618Srie 			    MSG_INTL(MSG_ELF_GETSHDR), ifl->ifl_name);
16900Sstevel@tonic-gate 			ofl->ofl_flags |= FLG_OF_FATAL;
16910Sstevel@tonic-gate 			return (0);
16920Sstevel@tonic-gate 		}
16930Sstevel@tonic-gate 		name = str + (size_t)(shdr->sh_name);
16940Sstevel@tonic-gate 
16952647Srie 		if (ld_sup_input_section(ofl, ifl, name, &shdr, ndx, scn,
16962647Srie 		    elf) == S_ERROR)
16970Sstevel@tonic-gate 			return (S_ERROR);
16980Sstevel@tonic-gate 
16990Sstevel@tonic-gate 		/*
17000Sstevel@tonic-gate 		 * Reset the name since the shdr->sh_name could have been
17019878SAli.Bahrami@Sun.COM 		 * changed as part of ld_sup_input_section().
17020Sstevel@tonic-gate 		 */
17039878SAli.Bahrami@Sun.COM 		name = str + (size_t)(shdr->sh_name);
17040Sstevel@tonic-gate 
17050Sstevel@tonic-gate 		row = shdr->sh_type;
17060Sstevel@tonic-gate 
17070Sstevel@tonic-gate 		/*
17080Sstevel@tonic-gate 		 * If the section has the SHF_EXCLUDE flag on, and we're not
17090Sstevel@tonic-gate 		 * generating a relocatable object, exclude the section.
17100Sstevel@tonic-gate 		 */
17110Sstevel@tonic-gate 		if (((shdr->sh_flags & SHF_EXCLUDE) != 0) &&
17120Sstevel@tonic-gate 		    ((ofl->ofl_flags & FLG_OF_RELOBJ) == 0)) {
17130Sstevel@tonic-gate 			if ((error = process_exclude(name, ifl, shdr, scn,
17140Sstevel@tonic-gate 			    ndx, ofl)) == S_ERROR)
17150Sstevel@tonic-gate 				return (S_ERROR);
17160Sstevel@tonic-gate 			if (error == 1)
17170Sstevel@tonic-gate 				continue;
17180Sstevel@tonic-gate 		}
17190Sstevel@tonic-gate 
17200Sstevel@tonic-gate 		/*
17210Sstevel@tonic-gate 		 * If this is a standard section type process it via the
17220Sstevel@tonic-gate 		 * appropriate action routine.
17230Sstevel@tonic-gate 		 */
17240Sstevel@tonic-gate 		if (row < SHT_NUM) {
17250Sstevel@tonic-gate 			if (Initial[row][column] != NULL) {
17260Sstevel@tonic-gate 				if (Initial[row][column](name, ifl, shdr, scn,
17270Sstevel@tonic-gate 				    ndx, ident, ofl) == S_ERROR)
17280Sstevel@tonic-gate 					return (S_ERROR);
17290Sstevel@tonic-gate 			}
17300Sstevel@tonic-gate 		} else {
17310Sstevel@tonic-gate 			/*
17320Sstevel@tonic-gate 			 * If this section is below SHT_LOSUNW then we don't
17330Sstevel@tonic-gate 			 * really know what to do with it, issue a warning
17340Sstevel@tonic-gate 			 * message but do the basic section processing anyway.
17350Sstevel@tonic-gate 			 */
17364734Sab196087 			if (row < (Word)SHT_LOSUNW) {
17374734Sab196087 				Conv_inv_buf_t inv_buf;
17384734Sab196087 
17391618Srie 				eprintf(ofl->ofl_lml, ERR_WARNING,
17401618Srie 				    MSG_INTL(MSG_FIL_INVALSEC), ifl->ifl_name,
17419878SAli.Bahrami@Sun.COM 				    EC_WORD(ndx), name, conv_sec_type(
17429273SAli.Bahrami@Sun.COM 				    ifl->ifl_ehdr->e_ident[EI_OSABI],
17439273SAli.Bahrami@Sun.COM 				    ifl->ifl_ehdr->e_machine,
17444734Sab196087 				    shdr->sh_type, 0, &inv_buf));
17454734Sab196087 			}
17460Sstevel@tonic-gate 
17470Sstevel@tonic-gate 			/*
17480Sstevel@tonic-gate 			 * Handle sections greater than SHT_LOSUNW.
17490Sstevel@tonic-gate 			 */
17500Sstevel@tonic-gate 			switch (row) {
17510Sstevel@tonic-gate 			case SHT_SUNW_dof:
17520Sstevel@tonic-gate 				if (process_section(name, ifl, shdr, scn,
17530Sstevel@tonic-gate 				    ndx, ident, ofl) == S_ERROR)
17540Sstevel@tonic-gate 					return (S_ERROR);
17550Sstevel@tonic-gate 				break;
17560Sstevel@tonic-gate 			case SHT_SUNW_cap:
17579085SAli.Bahrami@Sun.COM 				if (process_section(name, ifl, shdr, scn, ndx,
17589085SAli.Bahrami@Sun.COM 				    ld_targ.t_id.id_null, ofl) == S_ERROR)
17590Sstevel@tonic-gate 					return (S_ERROR);
17600Sstevel@tonic-gate 				capisp = ifl->ifl_isdesc[ndx];
17610Sstevel@tonic-gate 				break;
17620Sstevel@tonic-gate 			case SHT_SUNW_DEBUGSTR:
17630Sstevel@tonic-gate 			case SHT_SUNW_DEBUG:
17640Sstevel@tonic-gate 				if (process_debug(name, ifl, shdr, scn,
17650Sstevel@tonic-gate 				    ndx, ident, ofl) == S_ERROR)
17660Sstevel@tonic-gate 					return (S_ERROR);
17670Sstevel@tonic-gate 				break;
17680Sstevel@tonic-gate 			case SHT_SUNW_move:
17699085SAli.Bahrami@Sun.COM 				if (process_section(name, ifl, shdr, scn, ndx,
17709085SAli.Bahrami@Sun.COM 				    ld_targ.t_id.id_null, ofl) == S_ERROR)
17710Sstevel@tonic-gate 					return (S_ERROR);
17720Sstevel@tonic-gate 				break;
17730Sstevel@tonic-gate 			case SHT_SUNW_syminfo:
17749085SAli.Bahrami@Sun.COM 				if (process_section(name, ifl, shdr, scn, ndx,
17759085SAli.Bahrami@Sun.COM 				    ld_targ.t_id.id_null, ofl) == S_ERROR)
17760Sstevel@tonic-gate 					return (S_ERROR);
17770Sstevel@tonic-gate 				sifisp = ifl->ifl_isdesc[ndx];
17780Sstevel@tonic-gate 				break;
17790Sstevel@tonic-gate 			case SHT_SUNW_ANNOTATE:
17807463SRod.Evans@Sun.COM 				if (process_progbits(name, ifl, shdr, scn,
17817463SRod.Evans@Sun.COM 				    ndx, ident, ofl) == S_ERROR)
17827463SRod.Evans@Sun.COM 					return (S_ERROR);
17837463SRod.Evans@Sun.COM 				break;
17840Sstevel@tonic-gate 			case SHT_SUNW_COMDAT:
17850Sstevel@tonic-gate 				if (process_progbits(name, ifl, shdr, scn,
17860Sstevel@tonic-gate 				    ndx, ident, ofl) == S_ERROR)
17870Sstevel@tonic-gate 					return (S_ERROR);
17887463SRod.Evans@Sun.COM 				ifl->ifl_isdesc[ndx]->is_flags |= FLG_IS_COMDAT;
17890Sstevel@tonic-gate 				break;
17900Sstevel@tonic-gate 			case SHT_SUNW_verdef:
17919085SAli.Bahrami@Sun.COM 				if (process_section(name, ifl, shdr, scn, ndx,
17929085SAli.Bahrami@Sun.COM 				    ld_targ.t_id.id_null, ofl) == S_ERROR)
17930Sstevel@tonic-gate 					return (S_ERROR);
17940Sstevel@tonic-gate 				vdfisp = ifl->ifl_isdesc[ndx];
17950Sstevel@tonic-gate 				break;
17960Sstevel@tonic-gate 			case SHT_SUNW_verneed:
17979085SAli.Bahrami@Sun.COM 				if (process_section(name, ifl, shdr, scn, ndx,
17989085SAli.Bahrami@Sun.COM 				    ld_targ.t_id.id_null, ofl) == S_ERROR)
17990Sstevel@tonic-gate 					return (S_ERROR);
18000Sstevel@tonic-gate 				vndisp = ifl->ifl_isdesc[ndx];
18010Sstevel@tonic-gate 				break;
18020Sstevel@tonic-gate 			case SHT_SUNW_versym:
18039085SAli.Bahrami@Sun.COM 				if (process_section(name, ifl, shdr, scn, ndx,
18049085SAli.Bahrami@Sun.COM 				    ld_targ.t_id.id_null, ofl) == S_ERROR)
18050Sstevel@tonic-gate 					return (S_ERROR);
18060Sstevel@tonic-gate 				vsyisp = ifl->ifl_isdesc[ndx];
18070Sstevel@tonic-gate 				break;
18080Sstevel@tonic-gate 			case SHT_SPARC_GOTDATA:
18096206Sab196087 				/*
18106206Sab196087 				 * SHT_SPARC_GOTDATA (0x70000000) is in the
18116206Sab196087 				 * SHT_LOPROC - SHT_HIPROC range reserved
18126206Sab196087 				 * for processor-specific semantics. It is
18136206Sab196087 				 * only meaningful for sparc targets.
18146206Sab196087 				 */
18156206Sab196087 				if (ld_targ.t_m.m_mach !=
18166206Sab196087 				    LD_TARG_BYCLASS(EM_SPARC, EM_SPARCV9))
18176206Sab196087 					goto do_default;
18189085SAli.Bahrami@Sun.COM 				if (process_section(name, ifl, shdr, scn, ndx,
18199085SAli.Bahrami@Sun.COM 				    ld_targ.t_id.id_gotdata, ofl) == S_ERROR)
18200Sstevel@tonic-gate 					return (S_ERROR);
18210Sstevel@tonic-gate 				break;
18226206Sab196087 #if	defined(_ELF64)
18230Sstevel@tonic-gate 			case SHT_AMD64_UNWIND:
18246206Sab196087 				/*
18256206Sab196087 				 * SHT_AMD64_UNWIND (0x70000001) is in the
18266206Sab196087 				 * SHT_LOPROC - SHT_HIPROC range reserved
18276206Sab196087 				 * for processor-specific semantics. It is
18286206Sab196087 				 * only meaningful for amd64 targets.
18296206Sab196087 				 */
18306206Sab196087 				if (ld_targ.t_m.m_mach != EM_AMD64)
18316206Sab196087 					goto do_default;
18327463SRod.Evans@Sun.COM 
18336206Sab196087 				/*
18346206Sab196087 				 * Target is x86, so this really is
18356206Sab196087 				 * SHT_AMD64_UNWIND
18366206Sab196087 				 */
18370Sstevel@tonic-gate 				if (column == 0) {
18380Sstevel@tonic-gate 					/*
18390Sstevel@tonic-gate 					 * column == ET_REL
18400Sstevel@tonic-gate 					 */
18417463SRod.Evans@Sun.COM 					if (process_section(name, ifl, shdr,
18427463SRod.Evans@Sun.COM 					    scn, ndx, ld_targ.t_id.id_unwind,
18437463SRod.Evans@Sun.COM 					    ofl) == S_ERROR)
18440Sstevel@tonic-gate 						return (S_ERROR);
18459085SAli.Bahrami@Sun.COM 					ifl->ifl_isdesc[ndx]->is_flags |=
18469085SAli.Bahrami@Sun.COM 					    FLG_IS_EHFRAME;
18470Sstevel@tonic-gate 				}
18480Sstevel@tonic-gate 				break;
18490Sstevel@tonic-gate #endif
18500Sstevel@tonic-gate 			default:
18516206Sab196087 			do_default:
18529085SAli.Bahrami@Sun.COM 				if (process_section(name, ifl, shdr, scn, ndx,
18539085SAli.Bahrami@Sun.COM 				    ((ident == ld_targ.t_id.id_null) ?
18549085SAli.Bahrami@Sun.COM 				    ident : ld_targ.t_id.id_user), ofl) ==
18559085SAli.Bahrami@Sun.COM 				    S_ERROR)
18560Sstevel@tonic-gate 					return (S_ERROR);
18570Sstevel@tonic-gate 				break;
18580Sstevel@tonic-gate 			}
18590Sstevel@tonic-gate 		}
18607463SRod.Evans@Sun.COM 	}
18610Sstevel@tonic-gate 
18627463SRod.Evans@Sun.COM 	/*
18637463SRod.Evans@Sun.COM 	 * Now that all input sections have been analyzed, and prior to placing
18647463SRod.Evans@Sun.COM 	 * any input sections to their output sections, process any groups.
18657463SRod.Evans@Sun.COM 	 * Groups can contribute COMDAT items, which may get discarded as part
18667463SRod.Evans@Sun.COM 	 * of placement.  In addition, COMDAT names may require transformation
18677463SRod.Evans@Sun.COM 	 * to indicate different output section placement.
18687463SRod.Evans@Sun.COM 	 */
18697463SRod.Evans@Sun.COM 	if (ifl->ifl_flags & FLG_IS_GROUPS) {
18707463SRod.Evans@Sun.COM 		for (ndx = 1; ndx < ifl->ifl_shnum; ndx++) {
18717463SRod.Evans@Sun.COM 			Is_desc	*isp;
18727463SRod.Evans@Sun.COM 
18737463SRod.Evans@Sun.COM 			if (((isp = ifl->ifl_isdesc[ndx]) == NULL) ||
18747463SRod.Evans@Sun.COM 			    (isp->is_shdr->sh_type != SHT_GROUP))
18757463SRod.Evans@Sun.COM 				continue;
18767463SRod.Evans@Sun.COM 
18777463SRod.Evans@Sun.COM 			if (ld_group_process(isp, ofl) == S_ERROR)
18787463SRod.Evans@Sun.COM 				return (S_ERROR);
18790Sstevel@tonic-gate 		}
18800Sstevel@tonic-gate 	}
18810Sstevel@tonic-gate 
18820Sstevel@tonic-gate 	/*
18839615SAli.Bahrami@Sun.COM 	 * Now that all of the input sections have been processed, place
18849615SAli.Bahrami@Sun.COM 	 * them in the appropriate output sections.
18850Sstevel@tonic-gate 	 */
18867463SRod.Evans@Sun.COM 	for (ndx = 1; ndx < ifl->ifl_shnum; ndx++) {
18877463SRod.Evans@Sun.COM 		Is_desc	*isp;
18887463SRod.Evans@Sun.COM 
18897463SRod.Evans@Sun.COM 		if (((isp = ifl->ifl_isdesc[ndx]) == NULL) ||
18907463SRod.Evans@Sun.COM 		    ((isp->is_flags & FLG_IS_PLACE) == 0))
18917463SRod.Evans@Sun.COM 			continue;
18927463SRod.Evans@Sun.COM 
18937463SRod.Evans@Sun.COM 		/*
18947463SRod.Evans@Sun.COM 		 * Place all non-ordered sections within their appropriate
18957463SRod.Evans@Sun.COM 		 * output section.
18967463SRod.Evans@Sun.COM 		 */
18979615SAli.Bahrami@Sun.COM 		if ((isp->is_flags & FLG_IS_ORDERED) == 0) {
18987463SRod.Evans@Sun.COM 			if (ld_place_section(ofl, isp,
18999615SAli.Bahrami@Sun.COM 			    isp->is_keyident, NULL) == (Os_desc *)S_ERROR)
19007463SRod.Evans@Sun.COM 				return (S_ERROR);
19019615SAli.Bahrami@Sun.COM 			continue;
19027463SRod.Evans@Sun.COM 		}
19037463SRod.Evans@Sun.COM 
19047463SRod.Evans@Sun.COM 		/*
19059615SAli.Bahrami@Sun.COM 		 * Count the number of ordered sections and retain the first
19069615SAli.Bahrami@Sun.COM 		 * ordered section index. This will be used to optimize the
19079615SAli.Bahrami@Sun.COM 		 * ordered section loop that immediately follows this one.
19087463SRod.Evans@Sun.COM 		 */
19099615SAli.Bahrami@Sun.COM 		ordcnt++;
19109615SAli.Bahrami@Sun.COM 		if (ordndx == 0)
19119615SAli.Bahrami@Sun.COM 			ordndx = ndx;
19129615SAli.Bahrami@Sun.COM 	}
19139615SAli.Bahrami@Sun.COM 
19149615SAli.Bahrami@Sun.COM 	/*
19159615SAli.Bahrami@Sun.COM 	 * Having placed all the non-ordered sections, it is now
19169615SAli.Bahrami@Sun.COM 	 * safe to place SHF_ORDERED/SHF_LINK_ORDER sections.
19179615SAli.Bahrami@Sun.COM 	 */
19189615SAli.Bahrami@Sun.COM 	if (ifl->ifl_flags & FLG_IF_ORDERED) {
19199615SAli.Bahrami@Sun.COM 		for (ndx = ordndx; ndx < ifl->ifl_shnum; ndx++) {
19209615SAli.Bahrami@Sun.COM 			Is_desc	*isp;
19219615SAli.Bahrami@Sun.COM 
19229615SAli.Bahrami@Sun.COM 			if (((isp = ifl->ifl_isdesc[ndx]) == NULL) ||
19239615SAli.Bahrami@Sun.COM 			    ((isp->is_flags &
19249615SAli.Bahrami@Sun.COM 			    (FLG_IS_PLACE | FLG_IS_ORDERED)) !=
19259615SAli.Bahrami@Sun.COM 			    (FLG_IS_PLACE | FLG_IS_ORDERED)))
19269615SAli.Bahrami@Sun.COM 				continue;
19279615SAli.Bahrami@Sun.COM 
19289615SAli.Bahrami@Sun.COM 			/* ld_process_ordered() calls ld_place_section() */
19299615SAli.Bahrami@Sun.COM 			if (ld_process_ordered(ifl, ofl, ndx) == S_ERROR)
19309615SAli.Bahrami@Sun.COM 				return (S_ERROR);
19319615SAli.Bahrami@Sun.COM 
19329615SAli.Bahrami@Sun.COM 			/* If we've done them all, stop searching */
19339615SAli.Bahrami@Sun.COM 			if (--ordcnt == 0)
19349615SAli.Bahrami@Sun.COM 				break;
19357463SRod.Evans@Sun.COM 		}
19367463SRod.Evans@Sun.COM 	}
19377463SRod.Evans@Sun.COM 
19387463SRod.Evans@Sun.COM 	/*
19399615SAli.Bahrami@Sun.COM 	 * If this is a shared object explicitly specified on the command
19409615SAli.Bahrami@Sun.COM 	 * line (as opposed to being a dependency of such an object),
19419615SAli.Bahrami@Sun.COM 	 * determine if the user has specified a control definition. This
19429615SAli.Bahrami@Sun.COM 	 * descriptor may specify which version definitions can be used
19439615SAli.Bahrami@Sun.COM 	 * from this object. It may also update the dependency to USED and
19449615SAli.Bahrami@Sun.COM 	 * supply an alternative SONAME.
19450Sstevel@tonic-gate 	 */
19460Sstevel@tonic-gate 	sdf = 0;
19470Sstevel@tonic-gate 	if (column && (ifl->ifl_flags & FLG_IF_NEEDED)) {
19480Sstevel@tonic-gate 		const char	*base;
19490Sstevel@tonic-gate 
19500Sstevel@tonic-gate 		/*
19510Sstevel@tonic-gate 		 * Use the basename of the input file (typically this is the
19520Sstevel@tonic-gate 		 * compilation environment name, ie. libfoo.so).
19530Sstevel@tonic-gate 		 */
19540Sstevel@tonic-gate 		if ((base = strrchr(ifl->ifl_name, '/')) == NULL)
19550Sstevel@tonic-gate 			base = ifl->ifl_name;
19560Sstevel@tonic-gate 		else
19570Sstevel@tonic-gate 			base++;
19580Sstevel@tonic-gate 
19599131SRod.Evans@Sun.COM 		if ((sdf = sdf_find(base, ofl->ofl_socntl)) != NULL) {
19600Sstevel@tonic-gate 			sdf->sdf_file = ifl;
19610Sstevel@tonic-gate 			ifl->ifl_sdfdesc = sdf;
19620Sstevel@tonic-gate 		}
19630Sstevel@tonic-gate 	}
19640Sstevel@tonic-gate 
19650Sstevel@tonic-gate 	/*
19667833SRod.Evans@Sun.COM 	 * Process any hardware/software capabilities sections.  Only the
19677833SRod.Evans@Sun.COM 	 * capabilities for input relocatable objects are propagated.  If the
19687833SRod.Evans@Sun.COM 	 * relocatable objects don't contain any capabilities, any capability
19697833SRod.Evans@Sun.COM 	 * state that has already been gathered will prevail.
19700Sstevel@tonic-gate 	 */
19717833SRod.Evans@Sun.COM 	if (capisp)
19723617Srie 		process_cap(ifl, capisp, ofl);
19730Sstevel@tonic-gate 
19740Sstevel@tonic-gate 	/*
19750Sstevel@tonic-gate 	 * Process any version dependencies.  These will establish shared object
19760Sstevel@tonic-gate 	 * `needed' entries in the same manner as will be generated from the
19770Sstevel@tonic-gate 	 * .dynamic's NEEDED entries.
19780Sstevel@tonic-gate 	 */
19790Sstevel@tonic-gate 	if (vndisp && (ofl->ofl_flags & (FLG_OF_NOUNDEF | FLG_OF_SYMBOLIC)))
19801618Srie 		if (ld_vers_need_process(vndisp, ifl, ofl) == S_ERROR)
19810Sstevel@tonic-gate 			return (S_ERROR);
19820Sstevel@tonic-gate 
19830Sstevel@tonic-gate 	/*
19840Sstevel@tonic-gate 	 * Before processing any symbol resolution or relocations process any
19850Sstevel@tonic-gate 	 * version sections.
19860Sstevel@tonic-gate 	 */
19870Sstevel@tonic-gate 	if (vsyisp)
19881618Srie 		(void) ld_vers_sym_process(ofl->ofl_lml, vsyisp, ifl);
19890Sstevel@tonic-gate 
19900Sstevel@tonic-gate 	if (ifl->ifl_versym &&
19910Sstevel@tonic-gate 	    (vdfisp || (sdf && (sdf->sdf_flags & FLG_SDF_SELECT))))
19921618Srie 		if (ld_vers_def_process(vdfisp, ifl, ofl) == S_ERROR)
19930Sstevel@tonic-gate 			return (S_ERROR);
19940Sstevel@tonic-gate 
19950Sstevel@tonic-gate 	/*
19960Sstevel@tonic-gate 	 * Having collected the appropriate sections carry out any additional
19970Sstevel@tonic-gate 	 * processing if necessary.
19980Sstevel@tonic-gate 	 */
19990Sstevel@tonic-gate 	for (ndx = 0; ndx < ifl->ifl_shnum; ndx++) {
20007463SRod.Evans@Sun.COM 		Is_desc	*isp;
20010Sstevel@tonic-gate 
20020Sstevel@tonic-gate 		if ((isp = ifl->ifl_isdesc[ndx]) == 0)
20030Sstevel@tonic-gate 			continue;
20040Sstevel@tonic-gate 		row = isp->is_shdr->sh_type;
20050Sstevel@tonic-gate 
20060Sstevel@tonic-gate 		if ((isp->is_flags & FLG_IS_DISCARD) == 0)
20071618Srie 			ld_sup_section(ofl, isp->is_name, isp->is_shdr, ndx,
20081618Srie 			    isp->is_indata, elf);
20090Sstevel@tonic-gate 
20100Sstevel@tonic-gate 		/*
20119131SRod.Evans@Sun.COM 		 * If this is a SHT_SUNW_move section from a relocatable file,
20129131SRod.Evans@Sun.COM 		 * keep track of the section for later processing.
20130Sstevel@tonic-gate 		 */
20140Sstevel@tonic-gate 		if ((row == SHT_SUNW_move) && (column == 0)) {
20159131SRod.Evans@Sun.COM 			if (aplist_append(&(ofl->ofl_ismove), isp,
20169131SRod.Evans@Sun.COM 			    AL_CNT_OFL_MOVE) == NULL)
20170Sstevel@tonic-gate 				return (S_ERROR);
20180Sstevel@tonic-gate 		}
20190Sstevel@tonic-gate 
20200Sstevel@tonic-gate 		/*
20210Sstevel@tonic-gate 		 * If this is a standard section type process it via the
20220Sstevel@tonic-gate 		 * appropriate action routine.
20230Sstevel@tonic-gate 		 */
20240Sstevel@tonic-gate 		if (row < SHT_NUM) {
20257463SRod.Evans@Sun.COM 			if (Final[row][column] != NULL) {
20267463SRod.Evans@Sun.COM 				if (Final[row][column](isp, ifl,
20277463SRod.Evans@Sun.COM 				    ofl) == S_ERROR)
20280Sstevel@tonic-gate 					return (S_ERROR);
20297463SRod.Evans@Sun.COM 			}
20307463SRod.Evans@Sun.COM #if	defined(_ELF64)
20317463SRod.Evans@Sun.COM 		} else if ((row == SHT_AMD64_UNWIND) && (column == 0)) {
20327463SRod.Evans@Sun.COM 			Os_desc	*osp = isp->is_osdesc;
20337463SRod.Evans@Sun.COM 
20347463SRod.Evans@Sun.COM 			/*
20357463SRod.Evans@Sun.COM 			 * SHT_AMD64_UNWIND (0x70000001) is in the SHT_LOPROC -
20367463SRod.Evans@Sun.COM 			 * SHT_HIPROC range reserved for processor-specific
20377463SRod.Evans@Sun.COM 			 * semantics, and is only meaningful for amd64 targets.
20387463SRod.Evans@Sun.COM 			 *
20397463SRod.Evans@Sun.COM 			 * Only process unwind contents from relocatable
20407463SRod.Evans@Sun.COM 			 * objects.
20417463SRod.Evans@Sun.COM 			 */
20427463SRod.Evans@Sun.COM 			if (osp && (ld_targ.t_m.m_mach == EM_AMD64) &&
20439085SAli.Bahrami@Sun.COM 			    (ld_unwind_register(osp, ofl) == S_ERROR))
20447463SRod.Evans@Sun.COM 				return (S_ERROR);
20457463SRod.Evans@Sun.COM #endif
20460Sstevel@tonic-gate 		}
20470Sstevel@tonic-gate 	}
20480Sstevel@tonic-gate 
20490Sstevel@tonic-gate 	/*
20500Sstevel@tonic-gate 	 * After processing any symbol resolution, and if this dependency
20510Sstevel@tonic-gate 	 * indicates it contains symbols that can't be directly bound to,
20520Sstevel@tonic-gate 	 * set the symbols appropriately.
20530Sstevel@tonic-gate 	 */
20540Sstevel@tonic-gate 	if (sifisp && ((ifl->ifl_flags & (FLG_IF_NEEDED | FLG_IF_NODIRECT)) ==
20550Sstevel@tonic-gate 	    (FLG_IF_NEEDED | FLG_IF_NODIRECT)))
20561618Srie 		(void) ld_sym_nodirect(sifisp, ifl, ofl);
20570Sstevel@tonic-gate 
20580Sstevel@tonic-gate 	return (1);
20590Sstevel@tonic-gate }
20600Sstevel@tonic-gate 
20610Sstevel@tonic-gate /*
20620Sstevel@tonic-gate  * Process the current input file.  There are basically three types of files
20630Sstevel@tonic-gate  * that come through here:
20640Sstevel@tonic-gate  *
20650Sstevel@tonic-gate  *  o	files explicitly defined on the command line (ie. foo.o or bar.so),
20660Sstevel@tonic-gate  *	in this case only the `name' field is valid.
20670Sstevel@tonic-gate  *
20680Sstevel@tonic-gate  *  o	libraries determined from the -l command line option (ie. -lbar),
20690Sstevel@tonic-gate  *	in this case the `soname' field contains the basename of the located
20700Sstevel@tonic-gate  *	file.
20710Sstevel@tonic-gate  *
20720Sstevel@tonic-gate  * Any shared object specified via the above two conventions must be recorded
20730Sstevel@tonic-gate  * as a needed dependency.
20740Sstevel@tonic-gate  *
20750Sstevel@tonic-gate  *  o	libraries specified as dependencies of those libraries already obtained
20760Sstevel@tonic-gate  *	via the command line (ie. bar.so has a DT_NEEDED entry of fred.so.1),
20770Sstevel@tonic-gate  *	in this case the `soname' field contains either a full pathname (if the
20780Sstevel@tonic-gate  *	needed entry contained a `/'), or the basename of the located file.
20790Sstevel@tonic-gate  *	These libraries are processed to verify symbol binding but are not
20800Sstevel@tonic-gate  *	recorded as dependencies of the output file being generated.
20810Sstevel@tonic-gate  */
20820Sstevel@tonic-gate Ifl_desc *
20831618Srie ld_process_ifl(const char *name, const char *soname, int fd, Elf *elf,
20847359SRod.Evans@Sun.COM     Word flags, Ofl_desc *ofl, Rej_desc *rej)
20850Sstevel@tonic-gate {
20860Sstevel@tonic-gate 	Ifl_desc	*ifl;
20870Sstevel@tonic-gate 	Ehdr		*ehdr;
20880Sstevel@tonic-gate 	uintptr_t	error = 0;
20890Sstevel@tonic-gate 	struct stat	status;
20900Sstevel@tonic-gate 	Ar_desc		*adp;
20910Sstevel@tonic-gate 	Rej_desc	_rej;
20920Sstevel@tonic-gate 
20930Sstevel@tonic-gate 	/*
20940Sstevel@tonic-gate 	 * If this file was not extracted from an archive obtain its device
20950Sstevel@tonic-gate 	 * information.  This will be used to determine if the file has already
20960Sstevel@tonic-gate 	 * been processed (rather than simply comparing filenames, the device
20970Sstevel@tonic-gate 	 * information provides a quicker comparison and detects linked files).
20980Sstevel@tonic-gate 	 */
20998598SRod.Evans@Sun.COM 	if (fd && ((flags & FLG_IF_EXTRACT) == 0))
21000Sstevel@tonic-gate 		(void) fstat(fd, &status);
21010Sstevel@tonic-gate 	else {
21020Sstevel@tonic-gate 		status.st_dev = 0;
21030Sstevel@tonic-gate 		status.st_ino = 0;
21040Sstevel@tonic-gate 	}
21050Sstevel@tonic-gate 
21060Sstevel@tonic-gate 	switch (elf_kind(elf)) {
21070Sstevel@tonic-gate 	case ELF_K_AR:
21080Sstevel@tonic-gate 		/*
21090Sstevel@tonic-gate 		 * Determine if we've already come across this archive file.
21100Sstevel@tonic-gate 		 */
21110Sstevel@tonic-gate 		if (!(flags & FLG_IF_EXTRACT)) {
21129131SRod.Evans@Sun.COM 			Aliste	idx;
21139131SRod.Evans@Sun.COM 
21149131SRod.Evans@Sun.COM 			for (APLIST_TRAVERSE(ofl->ofl_ars, idx, adp)) {
21150Sstevel@tonic-gate 				if ((adp->ad_stdev != status.st_dev) ||
21160Sstevel@tonic-gate 				    (adp->ad_stino != status.st_ino))
21170Sstevel@tonic-gate 					continue;
21180Sstevel@tonic-gate 
21190Sstevel@tonic-gate 				/*
21200Sstevel@tonic-gate 				 * We've seen this file before so reuse the
21210Sstevel@tonic-gate 				 * original archive descriptor and discard the
21227359SRod.Evans@Sun.COM 				 * new elf descriptor.  Note that a file
21237359SRod.Evans@Sun.COM 				 * descriptor is unnecessary, as the file is
21247359SRod.Evans@Sun.COM 				 * already available in memory.
21250Sstevel@tonic-gate 				 */
21261618Srie 				DBG_CALL(Dbg_file_reuse(ofl->ofl_lml, name,
21271618Srie 				    adp->ad_name));
21280Sstevel@tonic-gate 				(void) elf_end(elf);
21297359SRod.Evans@Sun.COM 				return ((Ifl_desc *)ld_process_archive(name, -1,
21300Sstevel@tonic-gate 				    adp, ofl));
21310Sstevel@tonic-gate 			}
21320Sstevel@tonic-gate 		}
21330Sstevel@tonic-gate 
21340Sstevel@tonic-gate 		/*
21350Sstevel@tonic-gate 		 * As we haven't processed this file before establish a new
21360Sstevel@tonic-gate 		 * archive descriptor.
21370Sstevel@tonic-gate 		 */
21381618Srie 		adp = ld_ar_setup(name, elf, ofl);
21390Sstevel@tonic-gate 		if ((adp == 0) || (adp == (Ar_desc *)S_ERROR))
21400Sstevel@tonic-gate 			return ((Ifl_desc *)adp);
21410Sstevel@tonic-gate 		adp->ad_stdev = status.st_dev;
21420Sstevel@tonic-gate 		adp->ad_stino = status.st_ino;
21430Sstevel@tonic-gate 
21441618Srie 		ld_sup_file(ofl, name, ELF_K_AR, flags, elf);
21450Sstevel@tonic-gate 
21467359SRod.Evans@Sun.COM 		/*
21477359SRod.Evans@Sun.COM 		 * Indicate that the ELF descriptor no longer requires a file
21487359SRod.Evans@Sun.COM 		 * descriptor by reading the entire file.  The file is already
21497359SRod.Evans@Sun.COM 		 * read via the initial mmap(2) behind elf_begin(3elf), thus
21507359SRod.Evans@Sun.COM 		 * this operation is effectively a no-op.  However, a side-
21517359SRod.Evans@Sun.COM 		 * effect is that the internal file descriptor, maintained in
21527359SRod.Evans@Sun.COM 		 * the ELF descriptor, is set to -1.  This setting will not
21537359SRod.Evans@Sun.COM 		 * be compared with any file descriptor that is passed to
21547359SRod.Evans@Sun.COM 		 * elf_begin(), should this archive, or one of the archive
21557359SRod.Evans@Sun.COM 		 * members, be processed again from the command line or
21567359SRod.Evans@Sun.COM 		 * because of a -z rescan.
21577359SRod.Evans@Sun.COM 		 */
21587359SRod.Evans@Sun.COM 		if (elf_cntl(elf, ELF_C_FDREAD) == -1) {
21597359SRod.Evans@Sun.COM 			eprintf(ofl->ofl_lml, ERR_ELF, MSG_INTL(MSG_ELF_CNTL),
21607359SRod.Evans@Sun.COM 			    name);
21617359SRod.Evans@Sun.COM 			ofl->ofl_flags |= FLG_OF_FATAL;
21627359SRod.Evans@Sun.COM 			return (NULL);
21637359SRod.Evans@Sun.COM 		}
21647359SRod.Evans@Sun.COM 
21657359SRod.Evans@Sun.COM 		return ((Ifl_desc *)ld_process_archive(name, -1, adp, ofl));
21660Sstevel@tonic-gate 
21670Sstevel@tonic-gate 	case ELF_K_ELF:
21680Sstevel@tonic-gate 		/*
21690Sstevel@tonic-gate 		 * Obtain the elf header so that we can determine what type of
21700Sstevel@tonic-gate 		 * elf ELF_K_ELF file this is.
21710Sstevel@tonic-gate 		 */
21720Sstevel@tonic-gate 		if ((ehdr = elf_getehdr(elf)) == NULL) {
21730Sstevel@tonic-gate 			int	_class = gelf_getclass(elf);
21740Sstevel@tonic-gate 
21750Sstevel@tonic-gate 			/*
21760Sstevel@tonic-gate 			 * Failure could occur for a number of reasons at this
21770Sstevel@tonic-gate 			 * point.  Typically the files class is incorrect (ie.
21780Sstevel@tonic-gate 			 * user is building 64-bit but managed to pint at 32-bit
21790Sstevel@tonic-gate 			 * libraries).  However any number of elf errors can
21800Sstevel@tonic-gate 			 * also occur, such as from a truncated or corrupt file.
21810Sstevel@tonic-gate 			 * Here we try and get the best error message possible.
21820Sstevel@tonic-gate 			 */
21836206Sab196087 			if (ld_targ.t_m.m_class != _class) {
21840Sstevel@tonic-gate 				_rej.rej_type = SGS_REJ_CLASS;
21850Sstevel@tonic-gate 				_rej.rej_info = (uint_t)_class;
21860Sstevel@tonic-gate 			} else {
21870Sstevel@tonic-gate 				_rej.rej_type = SGS_REJ_STR;
21880Sstevel@tonic-gate 				_rej.rej_str = elf_errmsg(-1);
21890Sstevel@tonic-gate 			}
21900Sstevel@tonic-gate 			_rej.rej_name = name;
21916206Sab196087 			DBG_CALL(Dbg_file_rejected(ofl->ofl_lml, &_rej,
21926206Sab196087 			    ld_targ.t_m.m_mach));
21930Sstevel@tonic-gate 			if (rej->rej_type == 0) {
21940Sstevel@tonic-gate 				*rej = _rej;
21950Sstevel@tonic-gate 				rej->rej_name = strdup(_rej.rej_name);
21960Sstevel@tonic-gate 			}
21977359SRod.Evans@Sun.COM 			return (NULL);
21980Sstevel@tonic-gate 		}
21990Sstevel@tonic-gate 
22000Sstevel@tonic-gate 		/*
22010Sstevel@tonic-gate 		 * Determine if we've already come across this file.
22020Sstevel@tonic-gate 		 */
22030Sstevel@tonic-gate 		if (!(flags & FLG_IF_EXTRACT)) {
22049131SRod.Evans@Sun.COM 			APlist	*apl;
22059131SRod.Evans@Sun.COM 			Aliste	idx;
22060Sstevel@tonic-gate 
22070Sstevel@tonic-gate 			if (ehdr->e_type == ET_REL)
22089131SRod.Evans@Sun.COM 				apl = ofl->ofl_objs;
22090Sstevel@tonic-gate 			else
22109131SRod.Evans@Sun.COM 				apl = ofl->ofl_sos;
22110Sstevel@tonic-gate 
22120Sstevel@tonic-gate 			/*
22130Sstevel@tonic-gate 			 * Traverse the appropriate file list and determine if
22140Sstevel@tonic-gate 			 * a dev/inode match is found.
22150Sstevel@tonic-gate 			 */
22169131SRod.Evans@Sun.COM 			for (APLIST_TRAVERSE(apl, idx, ifl)) {
22170Sstevel@tonic-gate 				/*
22180Sstevel@tonic-gate 				 * Ifl_desc generated via -Nneed, therefore no
22190Sstevel@tonic-gate 				 * actual file behind it.
22200Sstevel@tonic-gate 				 */
22210Sstevel@tonic-gate 				if (ifl->ifl_flags & FLG_IF_NEEDSTR)
22220Sstevel@tonic-gate 					continue;
22230Sstevel@tonic-gate 
22240Sstevel@tonic-gate 				if ((ifl->ifl_stino != status.st_ino) ||
22250Sstevel@tonic-gate 				    (ifl->ifl_stdev != status.st_dev))
22260Sstevel@tonic-gate 					continue;
22270Sstevel@tonic-gate 
22280Sstevel@tonic-gate 				/*
22290Sstevel@tonic-gate 				 * Disregard (skip) this image.
22300Sstevel@tonic-gate 				 */
22311618Srie 				DBG_CALL(Dbg_file_skip(ofl->ofl_lml,
22321618Srie 				    ifl->ifl_name, name));
22330Sstevel@tonic-gate 				(void) elf_end(elf);
22340Sstevel@tonic-gate 
22350Sstevel@tonic-gate 				/*
22360Sstevel@tonic-gate 				 * If the file was explicitly defined on the
22370Sstevel@tonic-gate 				 * command line (this is always the case for
22380Sstevel@tonic-gate 				 * relocatable objects, and is true for shared
22390Sstevel@tonic-gate 				 * objects when they weren't specified via -l or
22400Sstevel@tonic-gate 				 * were dragged in as an implicit dependency),
22410Sstevel@tonic-gate 				 * then warn the user.
22420Sstevel@tonic-gate 				 */
22430Sstevel@tonic-gate 				if ((flags & FLG_IF_CMDLINE) ||
22440Sstevel@tonic-gate 				    (ifl->ifl_flags & FLG_IF_CMDLINE)) {
22450Sstevel@tonic-gate 					const char	*errmsg;
22460Sstevel@tonic-gate 
22470Sstevel@tonic-gate 					/*
22480Sstevel@tonic-gate 					 * Determine whether this is the same
22490Sstevel@tonic-gate 					 * file name as originally encountered
22500Sstevel@tonic-gate 					 * so as to provide the most
22510Sstevel@tonic-gate 					 * descriptive diagnostic.
22520Sstevel@tonic-gate 					 */
22534734Sab196087 					errmsg =
22544734Sab196087 					    (strcmp(name, ifl->ifl_name) == 0) ?
22554734Sab196087 					    MSG_INTL(MSG_FIL_MULINC_1) :
22564734Sab196087 					    MSG_INTL(MSG_FIL_MULINC_2);
22571618Srie 					eprintf(ofl->ofl_lml, ERR_WARNING,
22581618Srie 					    errmsg, name, ifl->ifl_name);
22590Sstevel@tonic-gate 				}
22600Sstevel@tonic-gate 				return (ifl);
22610Sstevel@tonic-gate 			}
22620Sstevel@tonic-gate 		}
22630Sstevel@tonic-gate 
22640Sstevel@tonic-gate 		/*
22650Sstevel@tonic-gate 		 * At this point, we know we need the file.  Establish an input
22660Sstevel@tonic-gate 		 * file descriptor and continue processing.
22670Sstevel@tonic-gate 		 */
22680Sstevel@tonic-gate 		ifl = ifl_setup(name, ehdr, elf, flags, ofl, rej);
22690Sstevel@tonic-gate 		if ((ifl == 0) || (ifl == (Ifl_desc *)S_ERROR))
22700Sstevel@tonic-gate 			return (ifl);
22710Sstevel@tonic-gate 		ifl->ifl_stdev = status.st_dev;
22720Sstevel@tonic-gate 		ifl->ifl_stino = status.st_ino;
22730Sstevel@tonic-gate 
22740Sstevel@tonic-gate 		/*
22750Sstevel@tonic-gate 		 * If -zignore is in effect, mark this file as a potential
22760Sstevel@tonic-gate 		 * candidate (the files use isn't actually determined until
22770Sstevel@tonic-gate 		 * symbol resolution and relocation processing are completed).
22780Sstevel@tonic-gate 		 */
22790Sstevel@tonic-gate 		if (ofl->ofl_flags1 & FLG_OF1_IGNORE)
22800Sstevel@tonic-gate 			ifl->ifl_flags |= FLG_IF_IGNORE;
22810Sstevel@tonic-gate 
22820Sstevel@tonic-gate 		switch (ehdr->e_type) {
22830Sstevel@tonic-gate 		case ET_REL:
22846206Sab196087 			(*ld_targ.t_mr.mr_mach_eflags)(ehdr, ofl);
22850Sstevel@tonic-gate 			error = process_elf(ifl, elf, ofl);
22860Sstevel@tonic-gate 			break;
22870Sstevel@tonic-gate 		case ET_DYN:
22880Sstevel@tonic-gate 			if ((ofl->ofl_flags & FLG_OF_STATIC) ||
22890Sstevel@tonic-gate 			    !(ofl->ofl_flags & FLG_OF_DYNLIBS)) {
22901618Srie 				eprintf(ofl->ofl_lml, ERR_FATAL,
22911618Srie 				    MSG_INTL(MSG_FIL_SOINSTAT), name);
22920Sstevel@tonic-gate 				ofl->ofl_flags |= FLG_OF_FATAL;
22937359SRod.Evans@Sun.COM 				return (NULL);
22940Sstevel@tonic-gate 			}
22950Sstevel@tonic-gate 
22960Sstevel@tonic-gate 			/*
22970Sstevel@tonic-gate 			 * Record any additional shared object information.
22980Sstevel@tonic-gate 			 * If no soname is specified (eg. this file was
22990Sstevel@tonic-gate 			 * derived from a explicit filename declaration on the
23000Sstevel@tonic-gate 			 * command line, ie. bar.so) use the pathname.
23010Sstevel@tonic-gate 			 * This entry may be overridden if the files dynamic
23020Sstevel@tonic-gate 			 * section specifies an DT_SONAME value.
23030Sstevel@tonic-gate 			 */
23040Sstevel@tonic-gate 			if (soname == NULL)
23050Sstevel@tonic-gate 				ifl->ifl_soname = ifl->ifl_name;
23060Sstevel@tonic-gate 			else
23070Sstevel@tonic-gate 				ifl->ifl_soname = soname;
23080Sstevel@tonic-gate 
23090Sstevel@tonic-gate 			/*
23100Sstevel@tonic-gate 			 * If direct bindings, lazy loading, or group
23110Sstevel@tonic-gate 			 * permissions need to be established, mark this object.
23120Sstevel@tonic-gate 			 */
23130Sstevel@tonic-gate 			if (ofl->ofl_flags1 & FLG_OF1_ZDIRECT)
23140Sstevel@tonic-gate 				ifl->ifl_flags |= FLG_IF_DIRECT;
23150Sstevel@tonic-gate 			if (ofl->ofl_flags1 & FLG_OF1_LAZYLD)
23160Sstevel@tonic-gate 				ifl->ifl_flags |= FLG_IF_LAZYLD;
23170Sstevel@tonic-gate 			if (ofl->ofl_flags1 & FLG_OF1_GRPPRM)
23180Sstevel@tonic-gate 				ifl->ifl_flags |= FLG_IF_GRPPRM;
23190Sstevel@tonic-gate 			error = process_elf(ifl, elf, ofl);
23200Sstevel@tonic-gate 
23210Sstevel@tonic-gate 			/*
23220Sstevel@tonic-gate 			 * At this point we know if this file will be
23230Sstevel@tonic-gate 			 * lazyloaded, or whether bindings to it must be direct.
23240Sstevel@tonic-gate 			 * In either case, a syminfo section is required.
23250Sstevel@tonic-gate 			 */
23260Sstevel@tonic-gate 			if (ifl->ifl_flags & (FLG_IF_LAZYLD | FLG_IF_DIRECT))
23270Sstevel@tonic-gate 				ofl->ofl_flags |= FLG_OF_SYMINFO;
23280Sstevel@tonic-gate 
23290Sstevel@tonic-gate 			break;
23300Sstevel@tonic-gate 		default:
23310Sstevel@tonic-gate 			(void) elf_errno();
23320Sstevel@tonic-gate 			_rej.rej_type = SGS_REJ_UNKFILE;
23330Sstevel@tonic-gate 			_rej.rej_name = name;
23346206Sab196087 			DBG_CALL(Dbg_file_rejected(ofl->ofl_lml, &_rej,
23356206Sab196087 			    ld_targ.t_m.m_mach));
23360Sstevel@tonic-gate 			if (rej->rej_type == 0) {
23370Sstevel@tonic-gate 				*rej = _rej;
23380Sstevel@tonic-gate 				rej->rej_name = strdup(_rej.rej_name);
23390Sstevel@tonic-gate 			}
23407359SRod.Evans@Sun.COM 			return (NULL);
23410Sstevel@tonic-gate 		}
23420Sstevel@tonic-gate 		break;
23430Sstevel@tonic-gate 	default:
23440Sstevel@tonic-gate 		(void) elf_errno();
23450Sstevel@tonic-gate 		_rej.rej_type = SGS_REJ_UNKFILE;
23460Sstevel@tonic-gate 		_rej.rej_name = name;
23476206Sab196087 		DBG_CALL(Dbg_file_rejected(ofl->ofl_lml, &_rej,
23486206Sab196087 		    ld_targ.t_m.m_mach));
23490Sstevel@tonic-gate 		if (rej->rej_type == 0) {
23500Sstevel@tonic-gate 			*rej = _rej;
23510Sstevel@tonic-gate 			rej->rej_name = strdup(_rej.rej_name);
23520Sstevel@tonic-gate 		}
23537359SRod.Evans@Sun.COM 		return (NULL);
23540Sstevel@tonic-gate 	}
23550Sstevel@tonic-gate 	if ((error == 0) || (error == S_ERROR))
23560Sstevel@tonic-gate 		return ((Ifl_desc *)error);
23570Sstevel@tonic-gate 	else
23580Sstevel@tonic-gate 		return (ifl);
23590Sstevel@tonic-gate }
23600Sstevel@tonic-gate 
23610Sstevel@tonic-gate /*
23620Sstevel@tonic-gate  * Having successfully opened a file, set up the necessary elf structures to
23630Sstevel@tonic-gate  * process it further.  This small section of processing is slightly different
23640Sstevel@tonic-gate  * from the elf initialization required to process a relocatable object from an
23652850Srie  * archive (see libs.c: ld_process_archive()).
23660Sstevel@tonic-gate  */
23670Sstevel@tonic-gate Ifl_desc *
23682978Srie ld_process_open(const char *opath, const char *ofile, int *fd, Ofl_desc *ofl,
23694716Sab196087     Word flags, Rej_desc *rej)
23700Sstevel@tonic-gate {
23712978Srie 	Elf		*elf;
23722978Srie 	const char	*npath = opath;
23732978Srie 	const char	*nfile = ofile;
23740Sstevel@tonic-gate 
23752978Srie 	if ((elf = elf_begin(*fd, ELF_C_READ, NULL)) == NULL) {
23762978Srie 		eprintf(ofl->ofl_lml, ERR_ELF, MSG_INTL(MSG_ELF_BEGIN), npath);
23770Sstevel@tonic-gate 		ofl->ofl_flags |= FLG_OF_FATAL;
23787359SRod.Evans@Sun.COM 		return (NULL);
23790Sstevel@tonic-gate 	}
23800Sstevel@tonic-gate 
23812978Srie 	/*
23822978Srie 	 * Determine whether the support library wishes to process this open.
23832978Srie 	 * The support library may return:
23842978Srie 	 *   .	a different ELF descriptor (in which case they should have
23852978Srie 	 *	closed the original)
23862978Srie 	 *   .	a different file descriptor (in which case they should have
23872978Srie 	 *	closed the original)
23882978Srie 	 *   .	a different path and file name (presumably associated with
23892978Srie 	 *	a different file descriptor)
23902978Srie 	 *
23912978Srie 	 * A file descriptor of -1, or and ELF descriptor of zero indicates
23922978Srie 	 * the file should be ignored.
23932978Srie 	 */
23942978Srie 	ld_sup_open(ofl, &npath, &nfile, fd, flags, &elf, NULL, 0,
23952978Srie 	    elf_kind(elf));
23962978Srie 
23972978Srie 	if ((*fd == -1) || (elf == NULL))
23987359SRod.Evans@Sun.COM 		return (NULL);
23992978Srie 
24002978Srie 	return (ld_process_ifl(npath, nfile, *fd, elf, flags, ofl, rej));
24010Sstevel@tonic-gate }
24020Sstevel@tonic-gate 
24030Sstevel@tonic-gate /*
24048598SRod.Evans@Sun.COM  * Having successfully mapped a file, set up the necessary elf structures to
24058598SRod.Evans@Sun.COM  * process it further.  This routine is patterned after ld_process_open() and
24068598SRod.Evans@Sun.COM  * is only called by ld.so.1(1) to process a relocatable object.
24078598SRod.Evans@Sun.COM  */
24088598SRod.Evans@Sun.COM Ifl_desc *
24098598SRod.Evans@Sun.COM ld_process_mem(const char *path, const char *file, char *addr, size_t size,
24108598SRod.Evans@Sun.COM     Ofl_desc *ofl, Rej_desc *rej)
24118598SRod.Evans@Sun.COM {
24128598SRod.Evans@Sun.COM 	Elf	*elf;
24138598SRod.Evans@Sun.COM 
24148598SRod.Evans@Sun.COM 	if ((elf = elf_memory(addr, size)) == NULL) {
24158598SRod.Evans@Sun.COM 		eprintf(ofl->ofl_lml, ERR_ELF, MSG_INTL(MSG_ELF_MEMORY), path);
24168598SRod.Evans@Sun.COM 		ofl->ofl_flags |= FLG_OF_FATAL;
24178598SRod.Evans@Sun.COM 		return (0);
24188598SRod.Evans@Sun.COM 	}
24198598SRod.Evans@Sun.COM 
24208598SRod.Evans@Sun.COM 	return (ld_process_ifl(path, file, 0, elf, 0, ofl, rej));
24218598SRod.Evans@Sun.COM }
24228598SRod.Evans@Sun.COM 
24238598SRod.Evans@Sun.COM /*
24240Sstevel@tonic-gate  * Process a required library (i.e. the dependency of a shared object).
24250Sstevel@tonic-gate  * Combine the directory and filename, check the resultant path size, and try
24260Sstevel@tonic-gate  * opening the pathname.
24270Sstevel@tonic-gate  */
24281618Srie static Ifl_desc *
24290Sstevel@tonic-gate process_req_lib(Sdf_desc *sdf, const char *dir, const char *file,
24307463SRod.Evans@Sun.COM     Ofl_desc *ofl, Rej_desc *rej)
24310Sstevel@tonic-gate {
24320Sstevel@tonic-gate 	size_t		dlen, plen;
24330Sstevel@tonic-gate 	int		fd;
24340Sstevel@tonic-gate 	char		path[PATH_MAX];
24350Sstevel@tonic-gate 	const char	*_dir = dir;
24360Sstevel@tonic-gate 
24370Sstevel@tonic-gate 	/*
24380Sstevel@tonic-gate 	 * Determine the sizes of the directory and filename to insure we don't
24390Sstevel@tonic-gate 	 * exceed our buffer.
24400Sstevel@tonic-gate 	 */
24410Sstevel@tonic-gate 	if ((dlen = strlen(dir)) == 0) {
24420Sstevel@tonic-gate 		_dir = MSG_ORIG(MSG_STR_DOT);
24430Sstevel@tonic-gate 		dlen = 1;
24440Sstevel@tonic-gate 	}
24450Sstevel@tonic-gate 	dlen++;
24460Sstevel@tonic-gate 	plen = dlen + strlen(file) + 1;
24470Sstevel@tonic-gate 	if (plen > PATH_MAX) {
24481618Srie 		eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_FIL_PTHTOLONG),
24491618Srie 		    _dir, file);
24500Sstevel@tonic-gate 		ofl->ofl_flags |= FLG_OF_FATAL;
24510Sstevel@tonic-gate 		return (0);
24520Sstevel@tonic-gate 	}
24530Sstevel@tonic-gate 
24540Sstevel@tonic-gate 	/*
24550Sstevel@tonic-gate 	 * Build the entire pathname and try and open the file.
24560Sstevel@tonic-gate 	 */
24570Sstevel@tonic-gate 	(void) strcpy(path, _dir);
24580Sstevel@tonic-gate 	(void) strcat(path, MSG_ORIG(MSG_STR_SLASH));
24590Sstevel@tonic-gate 	(void) strcat(path, file);
24601618Srie 	DBG_CALL(Dbg_libs_req(ofl->ofl_lml, sdf->sdf_name,
24611618Srie 	    sdf->sdf_rfile, path));
24620Sstevel@tonic-gate 
24630Sstevel@tonic-gate 	if ((fd = open(path, O_RDONLY)) == -1)
24640Sstevel@tonic-gate 		return (0);
24650Sstevel@tonic-gate 	else {
24660Sstevel@tonic-gate 		Ifl_desc	*ifl;
24670Sstevel@tonic-gate 		char		*_path;
24680Sstevel@tonic-gate 
24690Sstevel@tonic-gate 		if ((_path = libld_malloc(strlen(path) + 1)) == 0)
24700Sstevel@tonic-gate 			return ((Ifl_desc *)S_ERROR);
24710Sstevel@tonic-gate 		(void) strcpy(_path, path);
24724716Sab196087 		ifl = ld_process_open(_path, &_path[dlen], &fd, ofl, 0, rej);
24732978Srie 		if (fd != -1)
24742978Srie 			(void) close(fd);
24750Sstevel@tonic-gate 		return (ifl);
24760Sstevel@tonic-gate 	}
24770Sstevel@tonic-gate }
24780Sstevel@tonic-gate 
24790Sstevel@tonic-gate /*
24800Sstevel@tonic-gate  * Finish any library processing.  Walk the list of so's that have been listed
24810Sstevel@tonic-gate  * as "included" by shared objects we have previously processed.  Examine them,
24820Sstevel@tonic-gate  * without adding them as explicit dependents of this program, in order to
24830Sstevel@tonic-gate  * complete our symbol definition process.  The search path rules are:
24840Sstevel@tonic-gate  *
24850Sstevel@tonic-gate  *  o	use any user supplied paths, i.e. LD_LIBRARY_PATH and -L, then
24860Sstevel@tonic-gate  *
24870Sstevel@tonic-gate  *  o	use any RPATH defined within the parent shared object, then
24880Sstevel@tonic-gate  *
24890Sstevel@tonic-gate  *  o	use the default directories, i.e. LIBPATH or -YP.
24900Sstevel@tonic-gate  */
24910Sstevel@tonic-gate uintptr_t
24921618Srie ld_finish_libs(Ofl_desc *ofl)
24930Sstevel@tonic-gate {
24949131SRod.Evans@Sun.COM 	Aliste		idx1;
24950Sstevel@tonic-gate 	Sdf_desc	*sdf;
24960Sstevel@tonic-gate 	Rej_desc	rej = { 0 };
24970Sstevel@tonic-gate 
24980Sstevel@tonic-gate 	/*
24990Sstevel@tonic-gate 	 * Make sure we are back in dynamic mode.
25000Sstevel@tonic-gate 	 */
25010Sstevel@tonic-gate 	ofl->ofl_flags |= FLG_OF_DYNLIBS;
25020Sstevel@tonic-gate 
25039131SRod.Evans@Sun.COM 	for (APLIST_TRAVERSE(ofl->ofl_soneed, idx1, sdf)) {
25049131SRod.Evans@Sun.COM 		Aliste		idx2;
25052850Srie 		char		*path, *slash = NULL;
25060Sstevel@tonic-gate 		int		fd;
25070Sstevel@tonic-gate 		Ifl_desc	*ifl;
25082850Srie 		char		*file = (char *)sdf->sdf_name;
25090Sstevel@tonic-gate 
25100Sstevel@tonic-gate 		/*
25110Sstevel@tonic-gate 		 * See if this file has already been processed.  At the time
25120Sstevel@tonic-gate 		 * this implicit dependency was determined there may still have
25131109Srie 		 * been more explicit dependencies to process.  Note, if we ever
25140Sstevel@tonic-gate 		 * do parse the command line three times we would be able to
25151109Srie 		 * do all this checking when processing the dynamic section.
25160Sstevel@tonic-gate 		 */
25170Sstevel@tonic-gate 		if (sdf->sdf_file)
25180Sstevel@tonic-gate 			continue;
25190Sstevel@tonic-gate 
25209131SRod.Evans@Sun.COM 		for (APLIST_TRAVERSE(ofl->ofl_sos, idx2, ifl)) {
25210Sstevel@tonic-gate 			if (!(ifl->ifl_flags & FLG_IF_NEEDSTR) &&
25220Sstevel@tonic-gate 			    (strcmp(file, ifl->ifl_soname) == 0)) {
25230Sstevel@tonic-gate 				sdf->sdf_file = ifl;
25240Sstevel@tonic-gate 				break;
25250Sstevel@tonic-gate 			}
25260Sstevel@tonic-gate 		}
25270Sstevel@tonic-gate 		if (sdf->sdf_file)
25280Sstevel@tonic-gate 			continue;
25290Sstevel@tonic-gate 
25300Sstevel@tonic-gate 		/*
25312850Srie 		 * If the current path name element embeds a "/", then it's to
25322850Srie 		 * be taken "as is", with no searching involved.  Process all
25332850Srie 		 * "/" occurrences, so that we can deduce the base file name.
25340Sstevel@tonic-gate 		 */
25352850Srie 		for (path = file; *path; path++) {
25360Sstevel@tonic-gate 			if (*path == '/')
25372850Srie 				slash = path;
25382850Srie 		}
25392850Srie 		if (slash) {
25401618Srie 			DBG_CALL(Dbg_libs_req(ofl->ofl_lml, sdf->sdf_name,
25411618Srie 			    sdf->sdf_rfile, file));
25420Sstevel@tonic-gate 			if ((fd = open(file, O_RDONLY)) == -1) {
25431618Srie 				eprintf(ofl->ofl_lml, ERR_WARNING,
25441618Srie 				    MSG_INTL(MSG_FIL_NOTFOUND), file,
25451618Srie 				    sdf->sdf_rfile);
25460Sstevel@tonic-gate 			} else {
25470Sstevel@tonic-gate 				Rej_desc	_rej = { 0 };
25480Sstevel@tonic-gate 
25492978Srie 				ifl = ld_process_open(file, ++slash, &fd, ofl,
25504716Sab196087 				    0, &_rej);
25512978Srie 				if (fd != -1)
25522978Srie 					(void) close(fd);
25537359SRod.Evans@Sun.COM 				if (ifl == (Ifl_desc *)S_ERROR)
25547359SRod.Evans@Sun.COM 					return (S_ERROR);
25550Sstevel@tonic-gate 
25560Sstevel@tonic-gate 				if (_rej.rej_type) {
25574734Sab196087 					Conv_reject_desc_buf_t rej_buf;
25584734Sab196087 
25591618Srie 					eprintf(ofl->ofl_lml, ERR_WARNING,
25600Sstevel@tonic-gate 					    MSG_INTL(reject[_rej.rej_type]),
25610Sstevel@tonic-gate 					    _rej.rej_name ? rej.rej_name :
25620Sstevel@tonic-gate 					    MSG_INTL(MSG_STR_UNKNOWN),
25636206Sab196087 					    conv_reject_desc(&_rej, &rej_buf,
25646206Sab196087 					    ld_targ.t_m.m_mach));
25650Sstevel@tonic-gate 				} else
25660Sstevel@tonic-gate 					sdf->sdf_file = ifl;
25670Sstevel@tonic-gate 			}
25680Sstevel@tonic-gate 			continue;
25690Sstevel@tonic-gate 		}
25700Sstevel@tonic-gate 
25710Sstevel@tonic-gate 		/*
25720Sstevel@tonic-gate 		 * Now search for this file in any user defined directories.
25730Sstevel@tonic-gate 		 */
25749131SRod.Evans@Sun.COM 		for (APLIST_TRAVERSE(ofl->ofl_ulibdirs, idx2, path)) {
25750Sstevel@tonic-gate 			Rej_desc	_rej = { 0 };
25760Sstevel@tonic-gate 
25770Sstevel@tonic-gate 			ifl = process_req_lib(sdf, path, file, ofl, &_rej);
25780Sstevel@tonic-gate 			if (ifl == (Ifl_desc *)S_ERROR) {
25790Sstevel@tonic-gate 				return (S_ERROR);
25800Sstevel@tonic-gate 			}
25810Sstevel@tonic-gate 			if (_rej.rej_type) {
25820Sstevel@tonic-gate 				if (rej.rej_type == 0) {
25830Sstevel@tonic-gate 					rej = _rej;
25840Sstevel@tonic-gate 					rej.rej_name = strdup(_rej.rej_name);
25850Sstevel@tonic-gate 				}
25860Sstevel@tonic-gate 			}
25870Sstevel@tonic-gate 			if (ifl) {
25880Sstevel@tonic-gate 				sdf->sdf_file = ifl;
25890Sstevel@tonic-gate 				break;
25900Sstevel@tonic-gate 			}
25910Sstevel@tonic-gate 		}
25920Sstevel@tonic-gate 		if (sdf->sdf_file)
25930Sstevel@tonic-gate 			continue;
25940Sstevel@tonic-gate 
25950Sstevel@tonic-gate 		/*
25960Sstevel@tonic-gate 		 * Next use the local rules defined within the parent shared
25970Sstevel@tonic-gate 		 * object.
25980Sstevel@tonic-gate 		 */
25990Sstevel@tonic-gate 		if (sdf->sdf_rpath != NULL) {
26000Sstevel@tonic-gate 			char	*rpath, *next;
26010Sstevel@tonic-gate 
26020Sstevel@tonic-gate 			rpath = libld_malloc(strlen(sdf->sdf_rpath) + 1);
26030Sstevel@tonic-gate 			if (rpath == 0)
26040Sstevel@tonic-gate 				return (S_ERROR);
26050Sstevel@tonic-gate 			(void) strcpy(rpath, sdf->sdf_rpath);
26061618Srie 			DBG_CALL(Dbg_libs_path(ofl->ofl_lml, rpath,
26071618Srie 			    LA_SER_RUNPATH, sdf->sdf_rfile));
26080Sstevel@tonic-gate 			if ((path = strtok_r(rpath,
26090Sstevel@tonic-gate 			    MSG_ORIG(MSG_STR_COLON), &next)) != NULL) {
26100Sstevel@tonic-gate 				do {
26110Sstevel@tonic-gate 					Rej_desc	_rej = { 0 };
26120Sstevel@tonic-gate 
26130Sstevel@tonic-gate 					path = expand(sdf->sdf_rfile, path,
26140Sstevel@tonic-gate 					    &next);
26150Sstevel@tonic-gate 
26160Sstevel@tonic-gate 					ifl = process_req_lib(sdf, path,
26174716Sab196087 					    file, ofl, &_rej);
26180Sstevel@tonic-gate 					if (ifl == (Ifl_desc *)S_ERROR) {
26190Sstevel@tonic-gate 						return (S_ERROR);
26200Sstevel@tonic-gate 					}
26214716Sab196087 					if ((_rej.rej_type) &&
26224716Sab196087 					    (rej.rej_type == 0)) {
26234716Sab196087 						rej = _rej;
26244716Sab196087 						rej.rej_name =
26254716Sab196087 						    strdup(_rej.rej_name);
26260Sstevel@tonic-gate 					}
26270Sstevel@tonic-gate 					if (ifl) {
26280Sstevel@tonic-gate 						sdf->sdf_file = ifl;
26290Sstevel@tonic-gate 						break;
26300Sstevel@tonic-gate 					}
26310Sstevel@tonic-gate 				} while ((path = strtok_r(NULL,
26320Sstevel@tonic-gate 				    MSG_ORIG(MSG_STR_COLON), &next)) != NULL);
26330Sstevel@tonic-gate 			}
26340Sstevel@tonic-gate 		}
26350Sstevel@tonic-gate 		if (sdf->sdf_file)
26360Sstevel@tonic-gate 			continue;
26370Sstevel@tonic-gate 
26380Sstevel@tonic-gate 		/*
26390Sstevel@tonic-gate 		 * Finally try the default library search directories.
26400Sstevel@tonic-gate 		 */
26419131SRod.Evans@Sun.COM 		for (APLIST_TRAVERSE(ofl->ofl_dlibdirs, idx2, path)) {
26420Sstevel@tonic-gate 			Rej_desc	_rej = { 0 };
26430Sstevel@tonic-gate 
26440Sstevel@tonic-gate 			ifl = process_req_lib(sdf, path, file, ofl, &rej);
26450Sstevel@tonic-gate 			if (ifl == (Ifl_desc *)S_ERROR) {
26460Sstevel@tonic-gate 				return (S_ERROR);
26470Sstevel@tonic-gate 			}
26480Sstevel@tonic-gate 			if (_rej.rej_type) {
26490Sstevel@tonic-gate 				if (rej.rej_type == 0) {
26500Sstevel@tonic-gate 					rej = _rej;
26510Sstevel@tonic-gate 					rej.rej_name = strdup(_rej.rej_name);
26520Sstevel@tonic-gate 				}
26530Sstevel@tonic-gate 			}
26540Sstevel@tonic-gate 			if (ifl) {
26550Sstevel@tonic-gate 				sdf->sdf_file = ifl;
26560Sstevel@tonic-gate 				break;
26570Sstevel@tonic-gate 			}
26580Sstevel@tonic-gate 		}
26590Sstevel@tonic-gate 		if (sdf->sdf_file)
26600Sstevel@tonic-gate 			continue;
26610Sstevel@tonic-gate 
26620Sstevel@tonic-gate 		/*
26630Sstevel@tonic-gate 		 * If we've got this far we haven't found the shared object.
26640Sstevel@tonic-gate 		 * If an object was found, but was rejected for some reason,
26650Sstevel@tonic-gate 		 * print a diagnostic to that effect, otherwise generate a
26660Sstevel@tonic-gate 		 * generic "not found" diagnostic.
26670Sstevel@tonic-gate 		 */
26680Sstevel@tonic-gate 		if (rej.rej_type) {
26694734Sab196087 			Conv_reject_desc_buf_t rej_buf;
26704734Sab196087 
26711618Srie 			eprintf(ofl->ofl_lml, ERR_WARNING,
26721618Srie 			    MSG_INTL(reject[rej.rej_type]),
26730Sstevel@tonic-gate 			    rej.rej_name ? rej.rej_name :
26744734Sab196087 			    MSG_INTL(MSG_STR_UNKNOWN),
26756206Sab196087 			    conv_reject_desc(&rej, &rej_buf,
26766206Sab196087 			    ld_targ.t_m.m_mach));
26770Sstevel@tonic-gate 		} else {
26781618Srie 			eprintf(ofl->ofl_lml, ERR_WARNING,
26791618Srie 			    MSG_INTL(MSG_FIL_NOTFOUND), file, sdf->sdf_rfile);
26800Sstevel@tonic-gate 		}
26810Sstevel@tonic-gate 	}
26820Sstevel@tonic-gate 
26830Sstevel@tonic-gate 	/*
26840Sstevel@tonic-gate 	 * Finally, now that all objects have been input, make sure any version
26850Sstevel@tonic-gate 	 * requirements have been met.
26860Sstevel@tonic-gate 	 */
26871618Srie 	return (ld_vers_verify(ofl));
26880Sstevel@tonic-gate }
2689