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  *
261618Srie  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
270Sstevel@tonic-gate  * Use is subject to license terms.
280Sstevel@tonic-gate  */
290Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
300Sstevel@tonic-gate 
310Sstevel@tonic-gate /*
320Sstevel@tonic-gate  * Processing of relocatable objects and shared objects.
330Sstevel@tonic-gate  */
340Sstevel@tonic-gate #include	<stdio.h>
350Sstevel@tonic-gate #include	<string.h>
360Sstevel@tonic-gate #include	<fcntl.h>
370Sstevel@tonic-gate #include	<unistd.h>
380Sstevel@tonic-gate #include	<link.h>
390Sstevel@tonic-gate #include	<limits.h>
400Sstevel@tonic-gate #include	<sys/stat.h>
410Sstevel@tonic-gate #include	<sys/systeminfo.h>
420Sstevel@tonic-gate #include	<debug.h>
430Sstevel@tonic-gate #include	<msg.h>
440Sstevel@tonic-gate #include	<_libld.h>
450Sstevel@tonic-gate 
460Sstevel@tonic-gate /*
470Sstevel@tonic-gate  * Decide if we can link against this input file.
480Sstevel@tonic-gate  */
490Sstevel@tonic-gate static int
500Sstevel@tonic-gate ifl_verify(Ehdr * ehdr, Ofl_desc * ofl, Rej_desc * rej)
510Sstevel@tonic-gate {
520Sstevel@tonic-gate 	/*
530Sstevel@tonic-gate 	 * Check the validity of the elf header information for compatibility
540Sstevel@tonic-gate 	 * with this machine and our own internal elf library.
550Sstevel@tonic-gate 	 */
560Sstevel@tonic-gate 	if ((ehdr->e_machine != M_MACH) &&
570Sstevel@tonic-gate 	    ((ehdr->e_machine != M_MACHPLUS) &&
580Sstevel@tonic-gate 	    ((ehdr->e_flags & M_FLAGSPLUS) == 0))) {
590Sstevel@tonic-gate 		rej->rej_type = SGS_REJ_MACH;
600Sstevel@tonic-gate 		rej->rej_info = (uint_t)ehdr->e_machine;
610Sstevel@tonic-gate 		return (0);
620Sstevel@tonic-gate 	}
630Sstevel@tonic-gate 	if (ehdr->e_ident[EI_DATA] != M_DATA) {
640Sstevel@tonic-gate 		rej->rej_type = SGS_REJ_DATA;
650Sstevel@tonic-gate 		rej->rej_info = (uint_t)ehdr->e_ident[EI_DATA];
660Sstevel@tonic-gate 		return (0);
670Sstevel@tonic-gate 	}
681618Srie 	if (ehdr->e_version > ofl->ofl_dehdr->e_version) {
690Sstevel@tonic-gate 		rej->rej_type = SGS_REJ_VERSION;
700Sstevel@tonic-gate 		rej->rej_info = (uint_t)ehdr->e_version;
710Sstevel@tonic-gate 		return (0);
720Sstevel@tonic-gate 	}
730Sstevel@tonic-gate 	return (1);
740Sstevel@tonic-gate }
750Sstevel@tonic-gate 
760Sstevel@tonic-gate /*
770Sstevel@tonic-gate  * Check sanity of file header and allocate an infile descriptor
780Sstevel@tonic-gate  * for the file being processed.
790Sstevel@tonic-gate  */
801618Srie static Ifl_desc *
810Sstevel@tonic-gate ifl_setup(const char *name, Ehdr *ehdr, Elf *elf, Half flags, Ofl_desc *ofl,
820Sstevel@tonic-gate     Rej_desc *rej)
830Sstevel@tonic-gate {
840Sstevel@tonic-gate 	Ifl_desc	*ifl;
850Sstevel@tonic-gate 	List		*list;
860Sstevel@tonic-gate 	Rej_desc	_rej = { 0 };
870Sstevel@tonic-gate 
880Sstevel@tonic-gate 	if (ifl_verify(ehdr, ofl, &_rej) == 0) {
890Sstevel@tonic-gate 		_rej.rej_name = name;
901618Srie 		DBG_CALL(Dbg_file_rejected(ofl->ofl_lml, &_rej));
910Sstevel@tonic-gate 		if (rej->rej_type == 0) {
920Sstevel@tonic-gate 			*rej = _rej;
930Sstevel@tonic-gate 			rej->rej_name = strdup(_rej.rej_name);
940Sstevel@tonic-gate 		}
950Sstevel@tonic-gate 		return (0);
960Sstevel@tonic-gate 	}
970Sstevel@tonic-gate 
980Sstevel@tonic-gate 	if ((ifl = libld_calloc(1, sizeof (Ifl_desc))) == 0)
990Sstevel@tonic-gate 		return ((Ifl_desc *)S_ERROR);
1000Sstevel@tonic-gate 	ifl->ifl_name = name;
1010Sstevel@tonic-gate 	ifl->ifl_ehdr = ehdr;
1020Sstevel@tonic-gate 	ifl->ifl_elf = elf;
1030Sstevel@tonic-gate 	ifl->ifl_flags = flags;
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate 	/*
1060Sstevel@tonic-gate 	 * Is this file using 'extended Section Indexes'.  If so, use the
1070Sstevel@tonic-gate 	 * e_shnum & e_shstrndx which can be found at:
1080Sstevel@tonic-gate 	 *
1090Sstevel@tonic-gate 	 *	e_shnum == Shdr[0].sh_size
1100Sstevel@tonic-gate 	 *	e_shstrndx == Shdr[0].sh_link
1110Sstevel@tonic-gate 	 */
1120Sstevel@tonic-gate 	if ((ehdr->e_shnum == 0) && (ehdr->e_shoff != 0)) {
1130Sstevel@tonic-gate 		Elf_Scn	*scn;
1140Sstevel@tonic-gate 		Shdr	*shdr0;
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate 		if ((scn = elf_getscn(elf, 0)) == NULL) {
1171618Srie 			eprintf(ofl->ofl_lml, ERR_ELF,
1181618Srie 			    MSG_INTL(MSG_ELF_GETSCN), name);
1190Sstevel@tonic-gate 			ofl->ofl_flags |= FLG_OF_FATAL;
1200Sstevel@tonic-gate 			return ((Ifl_desc *)S_ERROR);
1210Sstevel@tonic-gate 		}
1220Sstevel@tonic-gate 		if ((shdr0 = elf_getshdr(scn)) == NULL) {
1231618Srie 			eprintf(ofl->ofl_lml, ERR_ELF,
1241618Srie 			    MSG_INTL(MSG_ELF_GETSHDR), name);
1250Sstevel@tonic-gate 			ofl->ofl_flags |= FLG_OF_FATAL;
1260Sstevel@tonic-gate 			return ((Ifl_desc *)S_ERROR);
1270Sstevel@tonic-gate 		}
1280Sstevel@tonic-gate 		ifl->ifl_shnum = (Word)shdr0->sh_size;
1290Sstevel@tonic-gate 		if (ehdr->e_shstrndx == SHN_XINDEX)
1300Sstevel@tonic-gate 			ifl->ifl_shstrndx = shdr0->sh_link;
1310Sstevel@tonic-gate 		else
1320Sstevel@tonic-gate 			ifl->ifl_shstrndx = ehdr->e_shstrndx;
1330Sstevel@tonic-gate 	} else {
1340Sstevel@tonic-gate 		ifl->ifl_shnum = ehdr->e_shnum;
1350Sstevel@tonic-gate 		ifl->ifl_shstrndx = ehdr->e_shstrndx;
1360Sstevel@tonic-gate 	}
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate 	if ((ifl->ifl_isdesc = libld_calloc(ifl->ifl_shnum,
1390Sstevel@tonic-gate 	    sizeof (Is_desc *))) == 0)
1400Sstevel@tonic-gate 		return ((Ifl_desc *)S_ERROR);
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate 	/*
1430Sstevel@tonic-gate 	 * Record this new input file on the shared object or relocatable
1440Sstevel@tonic-gate 	 * object input file list.
1450Sstevel@tonic-gate 	 */
1460Sstevel@tonic-gate 	if (ifl->ifl_ehdr->e_type == ET_DYN) {
1470Sstevel@tonic-gate 		list = &ofl->ofl_sos;
1480Sstevel@tonic-gate 	} else {
1490Sstevel@tonic-gate 		list = &ofl->ofl_objs;
1500Sstevel@tonic-gate 	}
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate 	if (list_appendc(list, ifl) == 0)
1530Sstevel@tonic-gate 		return ((Ifl_desc *)S_ERROR);
1540Sstevel@tonic-gate 	return (ifl);
1550Sstevel@tonic-gate }
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate /*
1580Sstevel@tonic-gate  * Process a generic section.  The appropriate section information is added
1590Sstevel@tonic-gate  * to the files input descriptor list.
1600Sstevel@tonic-gate  */
1611618Srie static uintptr_t
1620Sstevel@tonic-gate process_section(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
1630Sstevel@tonic-gate     Word ndx, int ident, Ofl_desc *ofl)
1640Sstevel@tonic-gate {
1650Sstevel@tonic-gate 	Is_desc	*isp;
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate 	/*
1680Sstevel@tonic-gate 	 * Create a new input section descriptor.  If this is a NOBITS
1690Sstevel@tonic-gate 	 * section elf_getdata() will still create a data buffer (the buffer
1700Sstevel@tonic-gate 	 * will be null and the size will reflect the actual memory size).
1710Sstevel@tonic-gate 	 */
1720Sstevel@tonic-gate 	if ((isp = libld_calloc(sizeof (Is_desc), 1)) == 0)
1730Sstevel@tonic-gate 		return (S_ERROR);
1740Sstevel@tonic-gate 	isp->is_shdr = shdr;
1750Sstevel@tonic-gate 	isp->is_file = ifl;
1760Sstevel@tonic-gate 	isp->is_name = name;
1770Sstevel@tonic-gate 	isp->is_scnndx = ndx;
178574Sseizo 	isp->is_flags = FLG_IS_EXTERNAL;
1790Sstevel@tonic-gate 	/* LINTED */
1800Sstevel@tonic-gate 	isp->is_key = (Half)ident;
1810Sstevel@tonic-gate 	if ((isp->is_indata = elf_getdata(scn, NULL)) == NULL) {
1821618Srie 		eprintf(ofl->ofl_lml, ERR_ELF, MSG_INTL(MSG_ELF_GETDATA),
1831618Srie 		    ifl->ifl_name);
1840Sstevel@tonic-gate 		ofl->ofl_flags |= FLG_OF_FATAL;
1850Sstevel@tonic-gate 		return (0);
1860Sstevel@tonic-gate 	}
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate 	if ((shdr->sh_flags & SHF_EXCLUDE) &&
1890Sstevel@tonic-gate 	    ((ofl->ofl_flags & FLG_OF_RELOBJ) == 0)) {
1900Sstevel@tonic-gate 		isp->is_flags |= FLG_IS_DISCARD;
1910Sstevel@tonic-gate 	}
1920Sstevel@tonic-gate 
1930Sstevel@tonic-gate 	/*
1940Sstevel@tonic-gate 	 * Add the new input section to the files input section list and
1950Sstevel@tonic-gate 	 * to the output section list (some sections like .strtab and
1960Sstevel@tonic-gate 	 * .shstrtab are not added to the output section list).
1970Sstevel@tonic-gate 	 *
1981618Srie 	 * If the section has the SHF_ORDERED flag on, do the ld_place_section()
1990Sstevel@tonic-gate 	 * after all input sections from this file are read in.
2000Sstevel@tonic-gate 	 */
2010Sstevel@tonic-gate 	ifl->ifl_isdesc[ndx] = isp;
2020Sstevel@tonic-gate 	if (ident && (shdr->sh_flags & ALL_SHF_ORDER) == 0)
2031618Srie 		return ((uintptr_t)ld_place_section(ofl, isp, ident, 0));
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate 	if (ident && (shdr->sh_flags & ALL_SHF_ORDER)) {
2060Sstevel@tonic-gate 		isp->is_flags |= FLG_IS_ORDERED;
2070Sstevel@tonic-gate 		isp->is_ident = ident;
2081618Srie 
2090Sstevel@tonic-gate 		if ((ndx != 0) && (ndx == shdr->sh_link) &&
2102647Srie 		    (shdr->sh_flags & SHF_ORDERED)) {
2111618Srie 			return ((uintptr_t)ld_place_section(ofl, isp,
2121618Srie 			    ident, 0));
2132647Srie 		}
2140Sstevel@tonic-gate 	}
2150Sstevel@tonic-gate 	return (1);
2160Sstevel@tonic-gate }
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate /*
2190Sstevel@tonic-gate  * Determine the software capabilities of the object being built from the
2200Sstevel@tonic-gate  * capabilities of the input relocatable objects.   One software capability
2210Sstevel@tonic-gate  * is presently recognized, and represented with the following (sys/elf.h):
2220Sstevel@tonic-gate  *
2230Sstevel@tonic-gate  *   SF1_SUNW_FPKNWN	use/non-use of frame pointer is known, and
2240Sstevel@tonic-gate  *   SF1_SUNW_FPUSED    the frame pointer is in use.
2250Sstevel@tonic-gate  *
2260Sstevel@tonic-gate  * The resolution of the present fame pointer state, and the capabilities
2270Sstevel@tonic-gate  * provided by a new input relocatable object are:
2280Sstevel@tonic-gate  *
2290Sstevel@tonic-gate  *                              new input relocatable object
2300Sstevel@tonic-gate  *
2310Sstevel@tonic-gate  *      present      |  SF1_SUNW_FPKNWN  |  SF1_SUNW_FPKNWN  |    <unknown>
2320Sstevel@tonic-gate  *       state       |  SF1_SUNW_FPUSED  |                   |
2330Sstevel@tonic-gate  *  ---------------------------------------------------------------------------
2340Sstevel@tonic-gate  *  SF1_SUNW_FPKNWN  |  SF1_SUNW_FPKNWN  |  SF1_SUNW_FPKNWN  |  SF1_SUNW_FPKNWN
2350Sstevel@tonic-gate  *  SF1_SUNW_FPUSED  |  SF1_SUNW_FPUSED  |                   |  SF1_SUNW_FPUSED
2360Sstevel@tonic-gate  *  ---------------------------------------------------------------------------
2370Sstevel@tonic-gate  *  SF1_SUNW_FPKNWN  |  SF1_SUNW_FPKNWN  |  SF1_SUNW_FPKNWN  |  SF1_SUNW_FPKNWN
2380Sstevel@tonic-gate  *                   |                   |                   |
2390Sstevel@tonic-gate  *  ---------------------------------------------------------------------------
2400Sstevel@tonic-gate  *     <unknown>     |  SF1_SUNW_FPKNWN  |  SF1_SUNW_FPKNWN  |    <unknown>
2410Sstevel@tonic-gate  *                   |  SF1_SUNW_FPUSED  |                   |
2420Sstevel@tonic-gate  */
2430Sstevel@tonic-gate static void
2440Sstevel@tonic-gate sf1_cap(Ofl_desc *ofl, Xword val, Ifl_desc *ifl, const char *name)
2450Sstevel@tonic-gate {
2460Sstevel@tonic-gate 	Xword	badval;
2470Sstevel@tonic-gate 
2480Sstevel@tonic-gate 	/*
2490Sstevel@tonic-gate 	 * If a mapfile has established definitions to override any input
2500Sstevel@tonic-gate 	 * capabilities, ignore any new input capabilities.
2510Sstevel@tonic-gate 	 */
2520Sstevel@tonic-gate 	if (ofl->ofl_flags1 & FLG_OF1_OVSFCAP) {
2531618Srie 		Dbg_cap_sec_entry(ofl->ofl_lml, DBG_CAP_IGNORE, CA_SUNW_SF_1,
2541618Srie 		    val, M_MACH);
2550Sstevel@tonic-gate 		return;
2560Sstevel@tonic-gate 	}
2570Sstevel@tonic-gate 
2580Sstevel@tonic-gate 	/*
2590Sstevel@tonic-gate 	 * If this object doesn't specify any capabilities, ignore it, and
2600Sstevel@tonic-gate 	 * leave the state as is.
2610Sstevel@tonic-gate 	 */
2620Sstevel@tonic-gate 	if (val == 0)
2630Sstevel@tonic-gate 		return;
2640Sstevel@tonic-gate 
2650Sstevel@tonic-gate 	/*
2660Sstevel@tonic-gate 	 * Make sure we only accept known software capabilities.  Note, that
2670Sstevel@tonic-gate 	 * an F1_SUNW_FPUSED by itself is viewed as bad practice.
2680Sstevel@tonic-gate 	 */
2690Sstevel@tonic-gate 	if ((badval = (val & ~SF1_SUNW_MASK)) != 0) {
2701618Srie 		eprintf(ofl->ofl_lml, ERR_WARNING, MSG_INTL(MSG_FIL_BADSF1),
2710Sstevel@tonic-gate 		    ifl->ifl_name, name, EC_XWORD(badval));
2720Sstevel@tonic-gate 		val &= SF1_SUNW_MASK;
2730Sstevel@tonic-gate 	}
2740Sstevel@tonic-gate 	if (val == SF1_SUNW_FPUSED) {
2751618Srie 		eprintf(ofl->ofl_lml, ERR_WARNING, MSG_INTL(MSG_FIL_BADSF1),
2760Sstevel@tonic-gate 		    ifl->ifl_name, name, EC_XWORD(val));
2770Sstevel@tonic-gate 		return;
2780Sstevel@tonic-gate 	}
2790Sstevel@tonic-gate 
2801618Srie 	Dbg_cap_sec_entry(ofl->ofl_lml, DBG_CAP_OLD, CA_SUNW_SF_1,
2811618Srie 	    ofl->ofl_sfcap_1, M_MACH);
2821618Srie 	Dbg_cap_sec_entry(ofl->ofl_lml, DBG_CAP_NEW, CA_SUNW_SF_1,
2831618Srie 	    val, M_MACH);
2840Sstevel@tonic-gate 
2850Sstevel@tonic-gate 	/*
2860Sstevel@tonic-gate 	 * Determine the resolution of the present frame pointer and the
2870Sstevel@tonic-gate 	 * new input relocatable objects frame pointer.
2880Sstevel@tonic-gate 	 */
2890Sstevel@tonic-gate 	if ((ofl->ofl_sfcap_1 & (SF1_SUNW_FPKNWN | SF1_SUNW_FPUSED)) ==
2900Sstevel@tonic-gate 	    (SF1_SUNW_FPKNWN | SF1_SUNW_FPUSED)) {
2910Sstevel@tonic-gate 		/*
2920Sstevel@tonic-gate 		 * If the new relocatable object isn't using a frame pointer,
2930Sstevel@tonic-gate 		 * reduce the present state to unused.
2940Sstevel@tonic-gate 		 */
2950Sstevel@tonic-gate 		if ((val & (SF1_SUNW_FPKNWN | SF1_SUNW_FPUSED)) !=
2960Sstevel@tonic-gate 		    (SF1_SUNW_FPKNWN | SF1_SUNW_FPUSED))
2970Sstevel@tonic-gate 			ofl->ofl_sfcap_1 &= ~SF1_SUNW_FPUSED;
2980Sstevel@tonic-gate 
2990Sstevel@tonic-gate 	} else if ((ofl->ofl_sfcap_1 & SF1_SUNW_FPKNWN) == 0) {
3000Sstevel@tonic-gate 		/*
3010Sstevel@tonic-gate 		 * If the present state is unknown, take the new relocatable
3020Sstevel@tonic-gate 		 * object frame pointer usage.
3030Sstevel@tonic-gate 		 */
3040Sstevel@tonic-gate 		ofl->ofl_sfcap_1 = val;
3050Sstevel@tonic-gate 	}
3060Sstevel@tonic-gate 
3071618Srie 	Dbg_cap_sec_entry(ofl->ofl_lml, DBG_CAP_RESOLVED, CA_SUNW_SF_1,
3081618Srie 	    ofl->ofl_sfcap_1, M_MACH);
3090Sstevel@tonic-gate }
3100Sstevel@tonic-gate 
3110Sstevel@tonic-gate /*
3120Sstevel@tonic-gate  * Determine the hardware capabilities of the object being built from the
3130Sstevel@tonic-gate  * capabilities of the input relocatable objects.  There's really little to
3140Sstevel@tonic-gate  * do here, other than to offer diagnostics, hardware capabilities are simply
3150Sstevel@tonic-gate  * additive.
3160Sstevel@tonic-gate  */
3170Sstevel@tonic-gate static void
3180Sstevel@tonic-gate hw1_cap(Ofl_desc *ofl, Xword val)
3190Sstevel@tonic-gate {
3200Sstevel@tonic-gate 	/*
3210Sstevel@tonic-gate 	 * If a mapfile has established definitions to override any input
3220Sstevel@tonic-gate 	 * capabilities, ignore any new input capabilities.
3230Sstevel@tonic-gate 	 */
3240Sstevel@tonic-gate 	if (ofl->ofl_flags1 & FLG_OF1_OVHWCAP) {
3251618Srie 		Dbg_cap_sec_entry(ofl->ofl_lml, DBG_CAP_IGNORE, CA_SUNW_HW_1,
3261618Srie 		    val, M_MACH);
3270Sstevel@tonic-gate 		return;
3280Sstevel@tonic-gate 	}
3290Sstevel@tonic-gate 
3300Sstevel@tonic-gate 	/*
3310Sstevel@tonic-gate 	 * If this object doesn't specify any capabilities, ignore it, and
3320Sstevel@tonic-gate 	 * leave the state as is.
3330Sstevel@tonic-gate 	 */
3340Sstevel@tonic-gate 	if (val == 0)
3350Sstevel@tonic-gate 		return;
3360Sstevel@tonic-gate 
3371618Srie 	Dbg_cap_sec_entry(ofl->ofl_lml, DBG_CAP_OLD, CA_SUNW_HW_1,
3381618Srie 	    ofl->ofl_hwcap_1, M_MACH);
3391618Srie 	Dbg_cap_sec_entry(ofl->ofl_lml, DBG_CAP_NEW, CA_SUNW_HW_1, val, M_MACH);
3400Sstevel@tonic-gate 
3410Sstevel@tonic-gate 	ofl->ofl_hwcap_1 |= val;
3420Sstevel@tonic-gate 
3431618Srie 	Dbg_cap_sec_entry(ofl->ofl_lml, DBG_CAP_RESOLVED, CA_SUNW_HW_1,
3441618Srie 	    ofl->ofl_hwcap_1, M_MACH);
3450Sstevel@tonic-gate }
3460Sstevel@tonic-gate 
3470Sstevel@tonic-gate /*
3480Sstevel@tonic-gate  * Process a hardware/software capabilities section.  Traverse the section
3490Sstevel@tonic-gate  * updating the global capabilities variables as necessary.
3500Sstevel@tonic-gate  */
3510Sstevel@tonic-gate static void
3520Sstevel@tonic-gate process_cap(const char *name, Ifl_desc *ifl, Is_desc *cisp, Ofl_desc *ofl)
3530Sstevel@tonic-gate {
3541618Srie 	Cap	*cdata;
3550Sstevel@tonic-gate 
3561824Srie 	DBG_CALL(Dbg_cap_sec_title(ofl));
3570Sstevel@tonic-gate 
3580Sstevel@tonic-gate 	for (cdata = (Cap *)cisp->is_indata->d_buf;
3590Sstevel@tonic-gate 	    cdata->c_tag != CA_SUNW_NULL; cdata++) {
3600Sstevel@tonic-gate 		switch (cdata->c_tag) {
3610Sstevel@tonic-gate 			case CA_SUNW_HW_1:
3620Sstevel@tonic-gate 				hw1_cap(ofl, cdata->c_un.c_val);
3630Sstevel@tonic-gate 				break;
3640Sstevel@tonic-gate 			case CA_SUNW_SF_1:
3650Sstevel@tonic-gate 				sf1_cap(ofl, cdata->c_un.c_val, ifl, name);
3660Sstevel@tonic-gate 				break;
3670Sstevel@tonic-gate 			default:
3681618Srie 				eprintf(ofl->ofl_lml, ERR_WARNING,
3691618Srie 				    MSG_INTL(MSG_FIL_UNKCAP),
3700Sstevel@tonic-gate 				    ifl->ifl_name, name, cdata->c_tag);
3710Sstevel@tonic-gate 		}
3720Sstevel@tonic-gate 	}
3730Sstevel@tonic-gate }
3740Sstevel@tonic-gate 
3750Sstevel@tonic-gate /*
3760Sstevel@tonic-gate  * Simply process the section so that we have pointers to the data for use
3770Sstevel@tonic-gate  * in later routines, however don't add the section to the output section
3780Sstevel@tonic-gate  * list as we will be creating our own replacement sections later (ie.
3790Sstevel@tonic-gate  * symtab and relocation).
3800Sstevel@tonic-gate  */
3811618Srie static uintptr_t
3820Sstevel@tonic-gate /* ARGSUSED5 */
3830Sstevel@tonic-gate process_input(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
3840Sstevel@tonic-gate 	Word ndx, int ident, Ofl_desc *ofl)
3850Sstevel@tonic-gate {
3860Sstevel@tonic-gate 	return (process_section(name, ifl, shdr, scn, ndx, M_ID_NULL, ofl));
3870Sstevel@tonic-gate }
3880Sstevel@tonic-gate 
3890Sstevel@tonic-gate /*
3900Sstevel@tonic-gate  * Keep a running count of relocation entries from input relocatable objects for
3910Sstevel@tonic-gate  * sizing relocation buckets later.  If we're building an executable, save any
3920Sstevel@tonic-gate  * relocations from shared objects to determine if any copy relocation symbol
3930Sstevel@tonic-gate  * has a displacement relocation against it.
3940Sstevel@tonic-gate  */
3951618Srie static uintptr_t
3960Sstevel@tonic-gate /* ARGSUSED5 */
3970Sstevel@tonic-gate process_reloc(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
3980Sstevel@tonic-gate 	Word ndx, int ident, Ofl_desc *ofl)
3990Sstevel@tonic-gate {
4000Sstevel@tonic-gate 	if (process_section(name, ifl,
4010Sstevel@tonic-gate 	    shdr, scn, ndx, M_ID_NULL, ofl) == S_ERROR)
4020Sstevel@tonic-gate 		return (S_ERROR);
4030Sstevel@tonic-gate 
4040Sstevel@tonic-gate 	if (ifl->ifl_ehdr->e_type == ET_REL) {
4050Sstevel@tonic-gate 		if (shdr->sh_entsize && (shdr->sh_entsize <= shdr->sh_size))
4060Sstevel@tonic-gate 			/* LINTED */
4070Sstevel@tonic-gate 			ofl->ofl_relocincnt +=
4080Sstevel@tonic-gate 			    (Word)(shdr->sh_size / shdr->sh_entsize);
4090Sstevel@tonic-gate 	} else if (ofl->ofl_flags & FLG_OF_EXEC) {
4100Sstevel@tonic-gate 		if (list_appendc(&ifl->ifl_relsect, ifl->ifl_isdesc[ndx]) == 0)
4110Sstevel@tonic-gate 			return (S_ERROR);
4120Sstevel@tonic-gate 	}
4130Sstevel@tonic-gate 	return (1);
4140Sstevel@tonic-gate }
4150Sstevel@tonic-gate 
4160Sstevel@tonic-gate 
4170Sstevel@tonic-gate /*
4180Sstevel@tonic-gate  * Process a string table section.  A valid section contains an initial and
4190Sstevel@tonic-gate  * final null byte.
4200Sstevel@tonic-gate  */
4211618Srie static uintptr_t
4220Sstevel@tonic-gate process_strtab(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
4230Sstevel@tonic-gate 	Word ndx, int ident, Ofl_desc *ofl)
4240Sstevel@tonic-gate {
4250Sstevel@tonic-gate 	char		*data;
4260Sstevel@tonic-gate 	size_t		size;
4270Sstevel@tonic-gate 	Is_desc		*isp;
4280Sstevel@tonic-gate 	uintptr_t	error;
4290Sstevel@tonic-gate 
4300Sstevel@tonic-gate 	/*
4310Sstevel@tonic-gate 	 * Never include .stab.excl sections in any output file.
4320Sstevel@tonic-gate 	 * If the -s flag has been specified strip any .stab sections.
4330Sstevel@tonic-gate 	 */
4340Sstevel@tonic-gate 	if (((ofl->ofl_flags & FLG_OF_STRIP) && ident &&
4350Sstevel@tonic-gate 	    (strncmp(name, MSG_ORIG(MSG_SCN_STAB), MSG_SCN_STAB_SIZE) == 0)) ||
4360Sstevel@tonic-gate 	    (strcmp(name, MSG_ORIG(MSG_SCN_STABEXCL)) == 0) && ident)
4370Sstevel@tonic-gate 		return (1);
4380Sstevel@tonic-gate 
4390Sstevel@tonic-gate 	/*
4400Sstevel@tonic-gate 	 * If we got here to process a .shstrtab or .dynstr table, `ident' will
4410Sstevel@tonic-gate 	 * be null.  Otherwise make sure we don't have a .strtab section as this
4420Sstevel@tonic-gate 	 * should not be added to the output section list either.
4430Sstevel@tonic-gate 	 */
4440Sstevel@tonic-gate 	if ((ident != M_ID_NULL) &&
4450Sstevel@tonic-gate 	    (strcmp(name, MSG_ORIG(MSG_SCN_STRTAB)) == 0))
4460Sstevel@tonic-gate 		ident = M_ID_NULL;
4470Sstevel@tonic-gate 
4480Sstevel@tonic-gate 	error = process_section(name, ifl, shdr, scn, ndx, ident, ofl);
4490Sstevel@tonic-gate 	if ((error == 0) || (error == S_ERROR))
4500Sstevel@tonic-gate 		return (error);
4510Sstevel@tonic-gate 
4520Sstevel@tonic-gate 	/*
4530Sstevel@tonic-gate 	 * String tables should start and end with a NULL byte.  Note, it has
4540Sstevel@tonic-gate 	 * been known for the assembler to create empty string tables, so check
4550Sstevel@tonic-gate 	 * the size before attempting to verify the data itself.
4560Sstevel@tonic-gate 	 */
4570Sstevel@tonic-gate 	isp = ifl->ifl_isdesc[ndx];
4580Sstevel@tonic-gate 	size = isp->is_indata->d_size;
4590Sstevel@tonic-gate 	if (size) {
4600Sstevel@tonic-gate 		data = isp->is_indata->d_buf;
4610Sstevel@tonic-gate 		if (data[0] != '\0' || data[size - 1] != '\0')
4621618Srie 			eprintf(ofl->ofl_lml, ERR_WARNING,
4631618Srie 			    MSG_INTL(MSG_FIL_MALSTR), ifl->ifl_name, name);
4640Sstevel@tonic-gate 	} else
4650Sstevel@tonic-gate 		isp->is_indata->d_buf = (void *)MSG_ORIG(MSG_STR_EMPTY);
4660Sstevel@tonic-gate 
4670Sstevel@tonic-gate 	ifl->ifl_flags |= FLG_IF_HSTRTAB;
4680Sstevel@tonic-gate 	return (1);
4690Sstevel@tonic-gate }
4700Sstevel@tonic-gate 
4710Sstevel@tonic-gate /*
4720Sstevel@tonic-gate  * Invalid sections produce a warning and are skipped.
4730Sstevel@tonic-gate  */
4741618Srie static uintptr_t
4750Sstevel@tonic-gate /* ARGSUSED3 */
4760Sstevel@tonic-gate invalid_section(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
4770Sstevel@tonic-gate     Word ndx, int ident, Ofl_desc *ofl)
4780Sstevel@tonic-gate {
4791618Srie 	eprintf(ofl->ofl_lml, ERR_WARNING, MSG_INTL(MSG_FIL_INVALSEC),
4801618Srie 	    ifl->ifl_name, name, conv_sec_type(ifl->ifl_ehdr->e_machine,
4811976Sab196087 	    shdr->sh_type, 0));
4820Sstevel@tonic-gate 	return (1);
4830Sstevel@tonic-gate }
4840Sstevel@tonic-gate 
4850Sstevel@tonic-gate /*
4860Sstevel@tonic-gate  * Process a progbits section.
4870Sstevel@tonic-gate  */
4881618Srie static uintptr_t
4890Sstevel@tonic-gate process_progbits(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
4900Sstevel@tonic-gate     Word ndx, int ident, Ofl_desc *ofl)
4910Sstevel@tonic-gate {
4920Sstevel@tonic-gate 	int stab_index = 0;
4930Sstevel@tonic-gate 
4940Sstevel@tonic-gate 	/*
4950Sstevel@tonic-gate 	 * Never include .stab.excl sections in any output file.
4960Sstevel@tonic-gate 	 * If the -s flag has been specified strip any .stab sections.
4970Sstevel@tonic-gate 	 */
4980Sstevel@tonic-gate 	if (ident && (strncmp(name, MSG_ORIG(MSG_SCN_STAB),
4990Sstevel@tonic-gate 	    MSG_SCN_STAB_SIZE) == 0)) {
5000Sstevel@tonic-gate 		if ((ofl->ofl_flags & FLG_OF_STRIP) ||
5010Sstevel@tonic-gate 		    (strcmp((name + MSG_SCN_STAB_SIZE),
5020Sstevel@tonic-gate 			MSG_ORIG(MSG_SCN_EXCL)) == 0))
5030Sstevel@tonic-gate 			return (1);
5040Sstevel@tonic-gate 
5050Sstevel@tonic-gate 		if (strcmp((name + MSG_SCN_STAB_SIZE),
5060Sstevel@tonic-gate 		    MSG_ORIG(MSG_SCN_INDEX)) == 0)
5070Sstevel@tonic-gate 			stab_index = 1;
5080Sstevel@tonic-gate 	}
5090Sstevel@tonic-gate 
5100Sstevel@tonic-gate 	if ((ofl->ofl_flags & FLG_OF_STRIP) && ident) {
5110Sstevel@tonic-gate 		if ((strncmp(name, MSG_ORIG(MSG_SCN_DEBUG),
5120Sstevel@tonic-gate 		    MSG_SCN_DEBUG_SIZE) == 0) ||
5130Sstevel@tonic-gate 		    (strcmp(name, MSG_ORIG(MSG_SCN_LINE)) == 0))
5140Sstevel@tonic-gate 			return (1);
5150Sstevel@tonic-gate 	}
5160Sstevel@tonic-gate 
5170Sstevel@tonic-gate 	/*
5180Sstevel@tonic-gate 	 * Update the ident to reflect the type of section we've got.
5190Sstevel@tonic-gate 	 *
5200Sstevel@tonic-gate 	 * If there is any .plt or .got section to generate we'll be creating
5210Sstevel@tonic-gate 	 * our own version, so don't allow any input sections of these types to
5220Sstevel@tonic-gate 	 * be added to the output section list (why a relocatable object would
5230Sstevel@tonic-gate 	 * have a .plt or .got is a mystery, but stranger things have occurred).
5240Sstevel@tonic-gate 	 */
5250Sstevel@tonic-gate 	if (ident) {
5260Sstevel@tonic-gate 		if (shdr->sh_flags & SHF_TLS)
5270Sstevel@tonic-gate 			ident = M_ID_TLS;
5280Sstevel@tonic-gate 		else if ((shdr->sh_flags & ~ALL_SHF_IGNORE) ==
5290Sstevel@tonic-gate 		    (SHF_ALLOC | SHF_EXECINSTR))
5300Sstevel@tonic-gate 			ident = M_ID_TEXT;
5310Sstevel@tonic-gate 		else if (shdr->sh_flags & SHF_ALLOC) {
5320Sstevel@tonic-gate 			if ((strcmp(name, MSG_ORIG(MSG_SCN_PLT)) == 0) ||
5330Sstevel@tonic-gate 			    (strcmp(name, MSG_ORIG(MSG_SCN_GOT)) == 0))
5340Sstevel@tonic-gate 				ident = M_ID_NULL;
5350Sstevel@tonic-gate 			else if (stab_index) {
5360Sstevel@tonic-gate 				/*
5370Sstevel@tonic-gate 				 * This is a work-around for x86 compilers that
5380Sstevel@tonic-gate 				 * have set SHF_ALLOC for the .stab.index
5390Sstevel@tonic-gate 				 * section.
5400Sstevel@tonic-gate 				 *
5410Sstevel@tonic-gate 				 * Because of this, make sure that the
5420Sstevel@tonic-gate 				 * .stab.index does not end up as the last
5430Sstevel@tonic-gate 				 * section in the text segment.  Older linkers
5440Sstevel@tonic-gate 				 * can produce segmentation violations when they
5450Sstevel@tonic-gate 				 * strip (ld -s) against a shared object whose
5460Sstevel@tonic-gate 				 * last section in the text segment is a .stab.
5470Sstevel@tonic-gate 				 */
5480Sstevel@tonic-gate 				ident = M_ID_INTERP;
5490Sstevel@tonic-gate 			} else
5500Sstevel@tonic-gate 				ident = M_ID_DATA;
5510Sstevel@tonic-gate 		} else
5520Sstevel@tonic-gate 			ident = M_ID_NOTE;
5530Sstevel@tonic-gate 	}
5540Sstevel@tonic-gate 	return (process_section(name, ifl, shdr, scn, ndx, ident, ofl));
5550Sstevel@tonic-gate }
5560Sstevel@tonic-gate 
5570Sstevel@tonic-gate /*
5580Sstevel@tonic-gate  * Handles the SHT_SUNW_{DEBUG,DEBUGSTR) sections.
5590Sstevel@tonic-gate  */
5601618Srie static uintptr_t
5610Sstevel@tonic-gate process_debug(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
5620Sstevel@tonic-gate     Word ndx, int ident, Ofl_desc *ofl)
5630Sstevel@tonic-gate {
5640Sstevel@tonic-gate 	/*
5650Sstevel@tonic-gate 	 * Debug information is discarded when the 'ld -s' flag is invoked.
5660Sstevel@tonic-gate 	 */
5670Sstevel@tonic-gate 	if (ofl->ofl_flags & FLG_OF_STRIP) {
5680Sstevel@tonic-gate 		return (1);
5690Sstevel@tonic-gate 	}
5700Sstevel@tonic-gate 	return (process_progbits(name, ifl, shdr, scn, ndx, ident, ofl));
5710Sstevel@tonic-gate }
5720Sstevel@tonic-gate 
5730Sstevel@tonic-gate /*
5740Sstevel@tonic-gate  * Process a nobits section.
5750Sstevel@tonic-gate  */
5761618Srie static uintptr_t
5770Sstevel@tonic-gate process_nobits(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
5780Sstevel@tonic-gate     Word ndx, int ident, Ofl_desc *ofl)
5790Sstevel@tonic-gate {
5800Sstevel@tonic-gate 	if (ident) {
5810Sstevel@tonic-gate 		if (shdr->sh_flags & SHF_TLS)
5820Sstevel@tonic-gate 			ident = M_ID_TLSBSS;
583574Sseizo #if	(defined(__i386) || defined(__amd64)) && defined(_ELF64)
584574Sseizo 		else if (shdr->sh_flags & SHF_AMD64_LARGE)
585574Sseizo 			ident = M_ID_LBSS;
586574Sseizo #endif
5870Sstevel@tonic-gate 		else
5880Sstevel@tonic-gate 			ident = M_ID_BSS;
5890Sstevel@tonic-gate 	}
5900Sstevel@tonic-gate 	return (process_section(name, ifl, shdr, scn, ndx, ident, ofl));
5910Sstevel@tonic-gate }
5920Sstevel@tonic-gate 
5930Sstevel@tonic-gate /*
5940Sstevel@tonic-gate  * Process a SHT_*_ARRAY section.
5950Sstevel@tonic-gate  */
5961618Srie static uintptr_t
5970Sstevel@tonic-gate process_array(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
5980Sstevel@tonic-gate     Word ndx, int ident, Ofl_desc *ofl)
5990Sstevel@tonic-gate {
6000Sstevel@tonic-gate 	Os_desc	*osp;
6010Sstevel@tonic-gate 	Is_desc	*isp;
6020Sstevel@tonic-gate 
6030Sstevel@tonic-gate 	if (ident)
6040Sstevel@tonic-gate 		ident = M_ID_ARRAY;
6050Sstevel@tonic-gate 
6060Sstevel@tonic-gate 	if (process_section(name, ifl, shdr, scn, ndx, ident, ofl) == S_ERROR)
6070Sstevel@tonic-gate 		return (S_ERROR);
6080Sstevel@tonic-gate 
6090Sstevel@tonic-gate 	if (((isp = ifl->ifl_isdesc[ndx]) == 0) ||
6100Sstevel@tonic-gate 	    ((osp = isp->is_osdesc) == 0))
6110Sstevel@tonic-gate 		return (0);
6120Sstevel@tonic-gate 
6130Sstevel@tonic-gate 	if ((shdr->sh_type == SHT_FINI_ARRAY) &&
6140Sstevel@tonic-gate 	    (ofl->ofl_osfiniarray == 0))
6150Sstevel@tonic-gate 		ofl->ofl_osfiniarray = osp;
6160Sstevel@tonic-gate 	else if ((shdr->sh_type == SHT_INIT_ARRAY) &&
6170Sstevel@tonic-gate 	    (ofl->ofl_osinitarray == 0))
6180Sstevel@tonic-gate 		ofl->ofl_osinitarray = osp;
6190Sstevel@tonic-gate 	else if ((shdr->sh_type == SHT_PREINIT_ARRAY) &&
6200Sstevel@tonic-gate 	    (ofl->ofl_ospreinitarray == 0))
6210Sstevel@tonic-gate 		ofl->ofl_ospreinitarray = osp;
6220Sstevel@tonic-gate 
6230Sstevel@tonic-gate 	return (1);
6240Sstevel@tonic-gate }
6250Sstevel@tonic-gate 
6260Sstevel@tonic-gate /*
6270Sstevel@tonic-gate  * Process a SHT_SYMTAB_SHNDX section.
6280Sstevel@tonic-gate  */
6291618Srie static uintptr_t
6300Sstevel@tonic-gate process_sym_shndx(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
6310Sstevel@tonic-gate     Word ndx, int ident, Ofl_desc *ofl)
6320Sstevel@tonic-gate {
6330Sstevel@tonic-gate 	if (process_input(name, ifl, shdr, scn, ndx, ident, ofl) == S_ERROR)
6340Sstevel@tonic-gate 		return (S_ERROR);
6350Sstevel@tonic-gate 
6360Sstevel@tonic-gate 	/*
6370Sstevel@tonic-gate 	 * Have we already seen the related SYMTAB - if so verify it now.
6380Sstevel@tonic-gate 	 */
6390Sstevel@tonic-gate 	if (shdr->sh_link < ndx) {
6400Sstevel@tonic-gate 		Is_desc	*isp = ifl->ifl_isdesc[shdr->sh_link];
6410Sstevel@tonic-gate 
6420Sstevel@tonic-gate 		if ((isp == 0) || ((isp->is_shdr->sh_type != SHT_SYMTAB) &&
6430Sstevel@tonic-gate 		    (isp->is_shdr->sh_type != SHT_DYNSYM))) {
6441618Srie 			eprintf(ofl->ofl_lml, ERR_FATAL,
6451618Srie 			    MSG_INTL(MSG_FIL_INVSHLINK), ifl->ifl_name, name,
6461618Srie 			    EC_XWORD(shdr->sh_link));
6470Sstevel@tonic-gate 			return (S_ERROR);
6480Sstevel@tonic-gate 		}
6490Sstevel@tonic-gate 		isp->is_symshndx = ifl->ifl_isdesc[ndx];
6500Sstevel@tonic-gate 	}
6510Sstevel@tonic-gate 	return (1);
6520Sstevel@tonic-gate }
6530Sstevel@tonic-gate 
6540Sstevel@tonic-gate /*
6550Sstevel@tonic-gate  * Final processing for SHT_SYMTAB_SHNDX section.
6560Sstevel@tonic-gate  */
6571618Srie static uintptr_t
6580Sstevel@tonic-gate /* ARGSUSED2 */
6590Sstevel@tonic-gate sym_shndx_process(Is_desc *isc, Ifl_desc *ifl, Ofl_desc *ofl)
6600Sstevel@tonic-gate {
6610Sstevel@tonic-gate 	if (isc->is_shdr->sh_link > isc->is_scnndx) {
6620Sstevel@tonic-gate 		Is_desc	*isp = ifl->ifl_isdesc[isc->is_shdr->sh_link];
6630Sstevel@tonic-gate 
6640Sstevel@tonic-gate 		if ((isp == 0) || ((isp->is_shdr->sh_type != SHT_SYMTAB) &&
6650Sstevel@tonic-gate 		    (isp->is_shdr->sh_type != SHT_DYNSYM))) {
6661618Srie 			eprintf(ofl->ofl_lml, ERR_FATAL,
6671618Srie 			    MSG_INTL(MSG_FIL_INVSHLINK), isc->is_file->ifl_name,
6681618Srie 			    isc->is_name, EC_XWORD(isc->is_shdr->sh_link));
6690Sstevel@tonic-gate 			return (S_ERROR);
6700Sstevel@tonic-gate 		}
6710Sstevel@tonic-gate 		isp->is_symshndx = isc;
6720Sstevel@tonic-gate 	}
6730Sstevel@tonic-gate 	return (1);
6740Sstevel@tonic-gate }
6750Sstevel@tonic-gate 
6760Sstevel@tonic-gate /*
6770Sstevel@tonic-gate  * Process .dynamic section from a relocatable object.
6780Sstevel@tonic-gate  *
6790Sstevel@tonic-gate  * Note: That the .dynamic section is only considered interesting when
6800Sstevel@tonic-gate  *	 dlopen()ing a relocatable object (thus FLG_OF1_RELDYN can only get
6810Sstevel@tonic-gate  *	 set when libld is called from ld.so.1).
6820Sstevel@tonic-gate  */
6830Sstevel@tonic-gate /*ARGSUSED*/
6841618Srie static uintptr_t
6850Sstevel@tonic-gate process_rel_dynamic(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
6860Sstevel@tonic-gate     Word ndx, int ident, Ofl_desc *ofl)
6870Sstevel@tonic-gate {
6880Sstevel@tonic-gate 	Dyn		*dyn;
6890Sstevel@tonic-gate 	Elf_Scn		*strscn;
6900Sstevel@tonic-gate 	Elf_Data	*dp;
6910Sstevel@tonic-gate 	char		*str;
6920Sstevel@tonic-gate 
6930Sstevel@tonic-gate 	/*
6940Sstevel@tonic-gate 	 * Process .dynamic sections from relocatable objects ?
6950Sstevel@tonic-gate 	 */
6960Sstevel@tonic-gate 	if ((ofl->ofl_flags1 & FLG_OF1_RELDYN) == 0)
6970Sstevel@tonic-gate 		return (1);
6980Sstevel@tonic-gate 
6990Sstevel@tonic-gate 	/*
7000Sstevel@tonic-gate 	 * Find the string section associated with the .dynamic section.
7010Sstevel@tonic-gate 	 */
7020Sstevel@tonic-gate 	if ((strscn = elf_getscn(ifl->ifl_elf, shdr->sh_link)) == NULL) {
7031618Srie 		eprintf(ofl->ofl_lml, ERR_ELF, MSG_INTL(MSG_ELF_GETSCN),
7041618Srie 		    ifl->ifl_name);
7050Sstevel@tonic-gate 		ofl->ofl_flags |= FLG_OF_FATAL;
7060Sstevel@tonic-gate 		return (0);
7070Sstevel@tonic-gate 	}
7080Sstevel@tonic-gate 	dp = elf_getdata(strscn, NULL);
7090Sstevel@tonic-gate 	str = (char *)dp->d_buf;
7100Sstevel@tonic-gate 
7110Sstevel@tonic-gate 	/*
7120Sstevel@tonic-gate 	 * And get the .dynamic data
7130Sstevel@tonic-gate 	 */
7140Sstevel@tonic-gate 	dp = elf_getdata(scn, NULL);
7150Sstevel@tonic-gate 
7160Sstevel@tonic-gate 	for (dyn = (Dyn *)dp->d_buf; dyn->d_tag != DT_NULL; dyn++) {
7171109Srie 		Ifl_desc	*difl;
7180Sstevel@tonic-gate 
7190Sstevel@tonic-gate 		switch (dyn->d_tag) {
7200Sstevel@tonic-gate 		case DT_NEEDED:
7210Sstevel@tonic-gate 		case DT_USED:
7221109Srie 			if (((difl =
7231109Srie 			    libld_calloc(1, sizeof (Ifl_desc))) == 0) ||
7241109Srie 			    (list_appendc(&ofl->ofl_sos, difl) == 0))
7250Sstevel@tonic-gate 				return (S_ERROR);
7261109Srie 
7271109Srie 			difl->ifl_name = MSG_ORIG(MSG_STR_DYNAMIC);
7281109Srie 			difl->ifl_soname = str + (size_t)dyn->d_un.d_val;
7291109Srie 			difl->ifl_flags = FLG_IF_NEEDSTR;
7300Sstevel@tonic-gate 			break;
7310Sstevel@tonic-gate 		case DT_RPATH:
7320Sstevel@tonic-gate 		case DT_RUNPATH:
7330Sstevel@tonic-gate 			if ((ofl->ofl_rpath = add_string(ofl->ofl_rpath,
7340Sstevel@tonic-gate 			    (str + (size_t)dyn->d_un.d_val))) ==
7350Sstevel@tonic-gate 			    (const char *)S_ERROR)
7360Sstevel@tonic-gate 				return (S_ERROR);
7370Sstevel@tonic-gate 			break;
7380Sstevel@tonic-gate 		}
7390Sstevel@tonic-gate 	}
7400Sstevel@tonic-gate 	return (1);
7410Sstevel@tonic-gate }
7420Sstevel@tonic-gate 
7430Sstevel@tonic-gate /*
7440Sstevel@tonic-gate  * Expand implicit references.  Dependencies can be specified in terms of the
7450Sstevel@tonic-gate  * $ORIGIN, $PLATFORM, $OSREL and $OSNAME tokens, either from their needed name,
7460Sstevel@tonic-gate  * or via a runpath.  In addition runpaths may also specify the $ISALIST token.
7470Sstevel@tonic-gate  *
7481109Srie  * Probably the most common reference to explicit dependencies (via -L) will be
7490Sstevel@tonic-gate  * sufficient to find any associated implicit dependencies, but just in case we
7500Sstevel@tonic-gate  * expand any occurrence of these known tokens here.
7510Sstevel@tonic-gate  *
7520Sstevel@tonic-gate  * Note, if any errors occur we simply return the original name.
7530Sstevel@tonic-gate  *
7540Sstevel@tonic-gate  * This code is remarkably similar to expand() in rtld/common/paths.c.
7550Sstevel@tonic-gate  */
7560Sstevel@tonic-gate static char		*platform = 0;
7570Sstevel@tonic-gate static size_t		platform_sz = 0;
7580Sstevel@tonic-gate static Isa_desc		*isa = 0;
7590Sstevel@tonic-gate static Uts_desc		*uts = 0;
7600Sstevel@tonic-gate 
7610Sstevel@tonic-gate static char *
7620Sstevel@tonic-gate expand(const char *parent, const char *name, char **next)
7630Sstevel@tonic-gate {
7640Sstevel@tonic-gate 	char		_name[PATH_MAX], *nptr, *_next;
7650Sstevel@tonic-gate 	const char	*optr;
7660Sstevel@tonic-gate 	size_t		nrem = PATH_MAX - 1;
7670Sstevel@tonic-gate 	int		expanded = 0, _expanded, isaflag = 0;
7680Sstevel@tonic-gate 
7690Sstevel@tonic-gate 	optr = name;
7700Sstevel@tonic-gate 	nptr = _name;
7710Sstevel@tonic-gate 
7720Sstevel@tonic-gate 	while (*optr) {
7730Sstevel@tonic-gate 		if (nrem == 0)
7740Sstevel@tonic-gate 			return ((char *)name);
7750Sstevel@tonic-gate 
7760Sstevel@tonic-gate 		if (*optr != '$') {
7770Sstevel@tonic-gate 			*nptr++ = *optr++, nrem--;
7780Sstevel@tonic-gate 			continue;
7790Sstevel@tonic-gate 		}
7800Sstevel@tonic-gate 
7810Sstevel@tonic-gate 		_expanded = 0;
7820Sstevel@tonic-gate 
7830Sstevel@tonic-gate 		if (strncmp(optr, MSG_ORIG(MSG_STR_ORIGIN),
7840Sstevel@tonic-gate 		    MSG_STR_ORIGIN_SIZE) == 0) {
7850Sstevel@tonic-gate 			char *eptr;
7860Sstevel@tonic-gate 
7870Sstevel@tonic-gate 			/*
7880Sstevel@tonic-gate 			 * For $ORIGIN, expansion is really just a concatenation
7890Sstevel@tonic-gate 			 * of the parents directory name.  For example, an
7901109Srie 			 * explicit dependency foo/bar/lib1.so with a dependency
7910Sstevel@tonic-gate 			 * on $ORIGIN/lib2.so would be expanded to
7920Sstevel@tonic-gate 			 * foo/bar/lib2.so.
7930Sstevel@tonic-gate 			 */
7940Sstevel@tonic-gate 			if ((eptr = strrchr(parent, '/')) == 0) {
7950Sstevel@tonic-gate 				*nptr++ = '.';
7960Sstevel@tonic-gate 				nrem--;
7970Sstevel@tonic-gate 			} else {
7980Sstevel@tonic-gate 				size_t	len = eptr - parent;
7990Sstevel@tonic-gate 
8000Sstevel@tonic-gate 				if (len >= nrem)
8010Sstevel@tonic-gate 					return ((char *)name);
8020Sstevel@tonic-gate 
8030Sstevel@tonic-gate 				(void) strncpy(nptr, parent, len);
8040Sstevel@tonic-gate 				nptr = nptr + len;
8050Sstevel@tonic-gate 				nrem -= len;
8060Sstevel@tonic-gate 			}
8070Sstevel@tonic-gate 			optr += MSG_STR_ORIGIN_SIZE;
8080Sstevel@tonic-gate 			expanded = _expanded = 1;
8090Sstevel@tonic-gate 
8100Sstevel@tonic-gate 		} else if (strncmp(optr, MSG_ORIG(MSG_STR_PLATFORM),
8110Sstevel@tonic-gate 		    MSG_STR_PLATFORM_SIZE) == 0) {
8120Sstevel@tonic-gate 			/*
8130Sstevel@tonic-gate 			 * Establish the platform from sysconf - like uname -i.
8140Sstevel@tonic-gate 			 */
8150Sstevel@tonic-gate 			if ((platform == 0) && (platform_sz == 0)) {
8160Sstevel@tonic-gate 				char	info[SYS_NMLN];
8170Sstevel@tonic-gate 				long	size;
8180Sstevel@tonic-gate 
8190Sstevel@tonic-gate 				size = sysinfo(SI_PLATFORM, info, SYS_NMLN);
8200Sstevel@tonic-gate 				if ((size != -1) &&
8210Sstevel@tonic-gate 				    (platform = libld_malloc((size_t)size))) {
8220Sstevel@tonic-gate 					(void) strcpy(platform, info);
8230Sstevel@tonic-gate 					platform_sz = (size_t)size - 1;
8240Sstevel@tonic-gate 				} else
8250Sstevel@tonic-gate 					platform_sz = 1;
8260Sstevel@tonic-gate 			}
8270Sstevel@tonic-gate 			if (platform != 0) {
8280Sstevel@tonic-gate 				if (platform_sz >= nrem)
8290Sstevel@tonic-gate 					return ((char *)name);
8300Sstevel@tonic-gate 
8310Sstevel@tonic-gate 				(void) strncpy(nptr, platform, platform_sz);
8320Sstevel@tonic-gate 				nptr = nptr + platform_sz;
8330Sstevel@tonic-gate 				nrem -= platform_sz;
8340Sstevel@tonic-gate 
8350Sstevel@tonic-gate 				optr += MSG_STR_PLATFORM_SIZE;
8360Sstevel@tonic-gate 				expanded = _expanded = 1;
8370Sstevel@tonic-gate 			}
8380Sstevel@tonic-gate 
8390Sstevel@tonic-gate 		} else if (strncmp(optr, MSG_ORIG(MSG_STR_OSNAME),
8400Sstevel@tonic-gate 		    MSG_STR_OSNAME_SIZE) == 0) {
8410Sstevel@tonic-gate 			/*
8420Sstevel@tonic-gate 			 * Establish the os name - like uname -s.
8430Sstevel@tonic-gate 			 */
8440Sstevel@tonic-gate 			if (uts == 0)
8450Sstevel@tonic-gate 				uts = conv_uts();
8460Sstevel@tonic-gate 
8470Sstevel@tonic-gate 			if (uts && uts->uts_osnamesz) {
8480Sstevel@tonic-gate 				if (uts->uts_osnamesz >= nrem)
8490Sstevel@tonic-gate 					return ((char *)name);
8500Sstevel@tonic-gate 
8510Sstevel@tonic-gate 				(void) strncpy(nptr, uts->uts_osname,
8520Sstevel@tonic-gate 				    uts->uts_osnamesz);
8530Sstevel@tonic-gate 				nptr = nptr + uts->uts_osnamesz;
8540Sstevel@tonic-gate 				nrem -= uts->uts_osnamesz;
8550Sstevel@tonic-gate 
8560Sstevel@tonic-gate 				optr += MSG_STR_OSNAME_SIZE;
8570Sstevel@tonic-gate 				expanded = _expanded = 1;
8580Sstevel@tonic-gate 			}
8590Sstevel@tonic-gate 
8600Sstevel@tonic-gate 		} else if (strncmp(optr, MSG_ORIG(MSG_STR_OSREL),
8610Sstevel@tonic-gate 		    MSG_STR_OSREL_SIZE) == 0) {
8620Sstevel@tonic-gate 			/*
8630Sstevel@tonic-gate 			 * Establish the os release - like uname -r.
8640Sstevel@tonic-gate 			 */
8650Sstevel@tonic-gate 			if (uts == 0)
8660Sstevel@tonic-gate 				uts = conv_uts();
8670Sstevel@tonic-gate 
8680Sstevel@tonic-gate 			if (uts && uts->uts_osrelsz) {
8690Sstevel@tonic-gate 				if (uts->uts_osrelsz >= nrem)
8700Sstevel@tonic-gate 					return ((char *)name);
8710Sstevel@tonic-gate 
8720Sstevel@tonic-gate 				(void) strncpy(nptr, uts->uts_osrel,
8730Sstevel@tonic-gate 				    uts->uts_osrelsz);
8740Sstevel@tonic-gate 				nptr = nptr + uts->uts_osrelsz;
8750Sstevel@tonic-gate 				nrem -= uts->uts_osrelsz;
8760Sstevel@tonic-gate 
8770Sstevel@tonic-gate 				optr += MSG_STR_OSREL_SIZE;
8780Sstevel@tonic-gate 				expanded = _expanded = 1;
8790Sstevel@tonic-gate 			}
8800Sstevel@tonic-gate 
8810Sstevel@tonic-gate 		} else if ((strncmp(optr, MSG_ORIG(MSG_STR_ISALIST),
8820Sstevel@tonic-gate 		    MSG_STR_ISALIST_SIZE) == 0) && next && (isaflag++ == 0)) {
8830Sstevel@tonic-gate 			/*
8840Sstevel@tonic-gate 			 * Establish instruction sets from sysconf.  Note that
8850Sstevel@tonic-gate 			 * this is only meaningful from runpaths.
8860Sstevel@tonic-gate 			 */
8870Sstevel@tonic-gate 			if (isa == 0)
8880Sstevel@tonic-gate 				isa = conv_isalist();
8890Sstevel@tonic-gate 
8900Sstevel@tonic-gate 			if (isa && isa->isa_listsz &&
8910Sstevel@tonic-gate 			    (nrem > isa->isa_opt->isa_namesz)) {
8920Sstevel@tonic-gate 				size_t		mlen, tlen, hlen = optr - name;
8930Sstevel@tonic-gate 				size_t		no;
8940Sstevel@tonic-gate 				char		*lptr;
8950Sstevel@tonic-gate 				Isa_opt		*opt = isa->isa_opt;
8960Sstevel@tonic-gate 
8970Sstevel@tonic-gate 				(void) strncpy(nptr, opt->isa_name,
8980Sstevel@tonic-gate 				    opt->isa_namesz);
8990Sstevel@tonic-gate 				nptr = nptr + opt->isa_namesz;
9000Sstevel@tonic-gate 				nrem -= opt->isa_namesz;
9010Sstevel@tonic-gate 
9020Sstevel@tonic-gate 				optr += MSG_STR_ISALIST_SIZE;
9030Sstevel@tonic-gate 				expanded = _expanded = 1;
9040Sstevel@tonic-gate 
9050Sstevel@tonic-gate 				tlen = strlen(optr);
9060Sstevel@tonic-gate 
9070Sstevel@tonic-gate 				/*
9080Sstevel@tonic-gate 				 * As ISALIST expands to a number of elements,
9090Sstevel@tonic-gate 				 * establish a new list to return to the caller.
9100Sstevel@tonic-gate 				 * This will contain the present path being
9110Sstevel@tonic-gate 				 * processed redefined for each isalist option,
9120Sstevel@tonic-gate 				 * plus the original remaining list entries.
9130Sstevel@tonic-gate 				 */
9140Sstevel@tonic-gate 				mlen = ((hlen + tlen) * (isa->isa_optno - 1)) +
9150Sstevel@tonic-gate 				    isa->isa_listsz - opt->isa_namesz;
9160Sstevel@tonic-gate 				if (*next)
9170Sstevel@tonic-gate 				    mlen += strlen(*next);
9180Sstevel@tonic-gate 				if ((_next = lptr = libld_malloc(mlen)) == 0)
9190Sstevel@tonic-gate 					return (0);
9200Sstevel@tonic-gate 
9210Sstevel@tonic-gate 				for (no = 1, opt++; no < isa->isa_optno;
9220Sstevel@tonic-gate 				    no++, opt++) {
9230Sstevel@tonic-gate 					(void) strncpy(lptr, name, hlen);
9240Sstevel@tonic-gate 					lptr = lptr + hlen;
9250Sstevel@tonic-gate 					(void) strncpy(lptr, opt->isa_name,
9260Sstevel@tonic-gate 					    opt->isa_namesz);
9270Sstevel@tonic-gate 					lptr = lptr + opt->isa_namesz;
9280Sstevel@tonic-gate 					(void) strncpy(lptr, optr, tlen);
9290Sstevel@tonic-gate 					lptr = lptr + tlen;
9300Sstevel@tonic-gate 					*lptr++ = ':';
9310Sstevel@tonic-gate 				}
9320Sstevel@tonic-gate 				if (*next)
9330Sstevel@tonic-gate 					(void) strcpy(lptr, *next);
9340Sstevel@tonic-gate 				else
9350Sstevel@tonic-gate 					*--lptr = '\0';
9360Sstevel@tonic-gate 			}
9370Sstevel@tonic-gate 		}
9380Sstevel@tonic-gate 
9390Sstevel@tonic-gate 		/*
9400Sstevel@tonic-gate 		 * If no expansion occurred skip the $ and continue.
9410Sstevel@tonic-gate 		 */
9420Sstevel@tonic-gate 		if (_expanded == 0)
9430Sstevel@tonic-gate 			*nptr++ = *optr++, nrem--;
9440Sstevel@tonic-gate 	}
9450Sstevel@tonic-gate 
9460Sstevel@tonic-gate 	/*
9470Sstevel@tonic-gate 	 * If any ISALIST processing has occurred not only do we return the
9480Sstevel@tonic-gate 	 * expanded node we're presently working on, but we must also update the
9490Sstevel@tonic-gate 	 * remaining list so that it is effectively prepended with this node
9500Sstevel@tonic-gate 	 * expanded to all remaining isalist options.  Note that we can only
9510Sstevel@tonic-gate 	 * handle one ISALIST per node.  For more than one ISALIST to be
9520Sstevel@tonic-gate 	 * processed we'd need a better algorithm than above to replace the
9530Sstevel@tonic-gate 	 * newly generated list.  Whether we want to encourage the number of
9540Sstevel@tonic-gate 	 * pathname permutations this would provide is another question. So, for
9550Sstevel@tonic-gate 	 * now if more than one ISALIST is encountered we return the original
9560Sstevel@tonic-gate 	 * node untouched.
9570Sstevel@tonic-gate 	 */
9580Sstevel@tonic-gate 	if (isaflag) {
9590Sstevel@tonic-gate 		if (isaflag == 1)
9600Sstevel@tonic-gate 			*next = _next;
9610Sstevel@tonic-gate 		else
9620Sstevel@tonic-gate 			return ((char *)name);
9630Sstevel@tonic-gate 	}
9640Sstevel@tonic-gate 
9650Sstevel@tonic-gate 	*nptr = '\0';
9660Sstevel@tonic-gate 
9670Sstevel@tonic-gate 	if (expanded) {
9680Sstevel@tonic-gate 		if ((nptr = libld_malloc(strlen(_name) + 1)) == 0)
9690Sstevel@tonic-gate 			return ((char *)name);
9700Sstevel@tonic-gate 		(void) strcpy(nptr, _name);
9710Sstevel@tonic-gate 		return (nptr);
9720Sstevel@tonic-gate 	}
9730Sstevel@tonic-gate 	return ((char *)name);
9740Sstevel@tonic-gate }
9750Sstevel@tonic-gate 
9760Sstevel@tonic-gate /*
9770Sstevel@tonic-gate  * Process a dynamic section.  If we are processing an explicit shared object
9780Sstevel@tonic-gate  * then we need to determine if it has a recorded SONAME, if so, this name will
9790Sstevel@tonic-gate  * be recorded in the output file being generated as the NEEDED entry rather
9800Sstevel@tonic-gate  * than the shared objects filename itself.
9810Sstevel@tonic-gate  * If the mode of the link-edit indicates that no undefined symbols should
9820Sstevel@tonic-gate  * remain, then we also need to build up a list of any additional shared object
9830Sstevel@tonic-gate  * dependencies this object may have.  In this case save any NEEDED entries
9840Sstevel@tonic-gate  * together with any associated run-path specifications.  This information is
9850Sstevel@tonic-gate  * recorded on the `ofl_soneed' list and will be analyzed after all explicit
9860Sstevel@tonic-gate  * file processing has been completed (refer finish_libs()).
9870Sstevel@tonic-gate  */
9881618Srie static uintptr_t
9890Sstevel@tonic-gate process_dynamic(Is_desc *isc, Ifl_desc *ifl, Ofl_desc *ofl)
9900Sstevel@tonic-gate {
9910Sstevel@tonic-gate 	Dyn		*data, *dyn;
9920Sstevel@tonic-gate 	char		*str, *rpath = NULL;
9930Sstevel@tonic-gate 	const char	*soname, *needed;
9940Sstevel@tonic-gate 	Sdf_desc	*sdf;
9950Sstevel@tonic-gate 	Listnode	*lnp;
9960Sstevel@tonic-gate 
9970Sstevel@tonic-gate 	data = (Dyn *)isc->is_indata->d_buf;
9980Sstevel@tonic-gate 	str = (char *)ifl->ifl_isdesc[isc->is_shdr->sh_link]->is_indata->d_buf;
9990Sstevel@tonic-gate 
10000Sstevel@tonic-gate 	/*
10010Sstevel@tonic-gate 	 * First loop through the dynamic section looking for a run path.
10020Sstevel@tonic-gate 	 */
10030Sstevel@tonic-gate 	if (ofl->ofl_flags & (FLG_OF_NOUNDEF | FLG_OF_SYMBOLIC)) {
10040Sstevel@tonic-gate 		for (dyn = data; dyn->d_tag != DT_NULL; dyn++) {
10050Sstevel@tonic-gate 			if ((dyn->d_tag != DT_RPATH) &&
10060Sstevel@tonic-gate 			    (dyn->d_tag != DT_RUNPATH))
10070Sstevel@tonic-gate 				continue;
10080Sstevel@tonic-gate 			if ((rpath = str + (size_t)dyn->d_un.d_val) == NULL)
10090Sstevel@tonic-gate 				continue;
10100Sstevel@tonic-gate 			break;
10110Sstevel@tonic-gate 		}
10120Sstevel@tonic-gate 	}
10130Sstevel@tonic-gate 
10140Sstevel@tonic-gate 	/*
10150Sstevel@tonic-gate 	 * Now look for any needed dependencies (which may use the rpath)
10160Sstevel@tonic-gate 	 * or a new SONAME.
10170Sstevel@tonic-gate 	 */
10180Sstevel@tonic-gate 	for (dyn = data; dyn->d_tag != DT_NULL; dyn++) {
10190Sstevel@tonic-gate 		if (dyn->d_tag == DT_SONAME) {
10200Sstevel@tonic-gate 			if ((soname = str + (size_t)dyn->d_un.d_val) == NULL)
10210Sstevel@tonic-gate 				continue;
10220Sstevel@tonic-gate 
10230Sstevel@tonic-gate 			/*
10240Sstevel@tonic-gate 			 * Update the input file structure with this new name.
10250Sstevel@tonic-gate 			 */
10260Sstevel@tonic-gate 			ifl->ifl_soname = soname;
10270Sstevel@tonic-gate 
10280Sstevel@tonic-gate 		} else if ((dyn->d_tag == DT_NEEDED) ||
10290Sstevel@tonic-gate 		    (dyn->d_tag == DT_USED)) {
10300Sstevel@tonic-gate 			if (!(ofl->ofl_flags &
10310Sstevel@tonic-gate 			    (FLG_OF_NOUNDEF | FLG_OF_SYMBOLIC)))
10320Sstevel@tonic-gate 				continue;
10330Sstevel@tonic-gate 			if ((needed = str + (size_t)dyn->d_un.d_val) == NULL)
10340Sstevel@tonic-gate 				continue;
10350Sstevel@tonic-gate 
10360Sstevel@tonic-gate 			/*
10370Sstevel@tonic-gate 			 * Determine if this needed entry is already recorded on
10380Sstevel@tonic-gate 			 * the shared object needed list, if not create a new
10390Sstevel@tonic-gate 			 * definition for later processing (see finish_libs()).
10400Sstevel@tonic-gate 			 */
10410Sstevel@tonic-gate 			needed = expand(ifl->ifl_name, needed, (char **)0);
10420Sstevel@tonic-gate 
10430Sstevel@tonic-gate 			if ((sdf = sdf_find(needed, &ofl->ofl_soneed)) == 0) {
10440Sstevel@tonic-gate 				if ((sdf = sdf_add(needed,
10450Sstevel@tonic-gate 				    &ofl->ofl_soneed)) == (Sdf_desc *)S_ERROR)
10460Sstevel@tonic-gate 					return (S_ERROR);
10470Sstevel@tonic-gate 				sdf->sdf_rfile = ifl->ifl_name;
10480Sstevel@tonic-gate 			}
10490Sstevel@tonic-gate 
10500Sstevel@tonic-gate 			/*
10510Sstevel@tonic-gate 			 * Record the runpath (Note that we take the first
10520Sstevel@tonic-gate 			 * runpath which is exactly what ld.so.1 would do during
10530Sstevel@tonic-gate 			 * its dependency processing).
10540Sstevel@tonic-gate 			 */
10550Sstevel@tonic-gate 			if (rpath && (sdf->sdf_rpath == 0))
10560Sstevel@tonic-gate 				sdf->sdf_rpath = rpath;
10570Sstevel@tonic-gate 
10580Sstevel@tonic-gate 		} else if (dyn->d_tag == DT_FLAGS_1) {
10590Sstevel@tonic-gate 			if (dyn->d_un.d_val & (DF_1_INITFIRST | DF_1_INTERPOSE))
10600Sstevel@tonic-gate 				ifl->ifl_flags &= ~FLG_IF_LAZYLD;
10610Sstevel@tonic-gate 			if (dyn->d_un.d_val & DF_1_DISPRELPND)
10620Sstevel@tonic-gate 				ifl->ifl_flags |= FLG_IF_DISPPEND;
10630Sstevel@tonic-gate 			if (dyn->d_un.d_val & DF_1_DISPRELDNE)
10640Sstevel@tonic-gate 				ifl->ifl_flags |= FLG_IF_DISPDONE;
10650Sstevel@tonic-gate 			if (dyn->d_un.d_val & DF_1_NODIRECT)
10660Sstevel@tonic-gate 				ifl->ifl_flags |= FLG_IF_NODIRECT;
10670Sstevel@tonic-gate 
10680Sstevel@tonic-gate 		} else if ((dyn->d_tag == DT_AUDIT) &&
10690Sstevel@tonic-gate 		    (ifl->ifl_flags & FLG_IF_NEEDED)) {
10700Sstevel@tonic-gate 			/*
10710Sstevel@tonic-gate 			 * Record audit string as DT_DEPAUDIT.
10720Sstevel@tonic-gate 			 */
10730Sstevel@tonic-gate 			if ((ofl->ofl_depaudit = add_string(ofl->ofl_depaudit,
10740Sstevel@tonic-gate 			    (str + (size_t)dyn->d_un.d_val))) ==
10750Sstevel@tonic-gate 			    (const char *)S_ERROR)
10760Sstevel@tonic-gate 				return (S_ERROR);
10770Sstevel@tonic-gate 
10780Sstevel@tonic-gate 		} else if (dyn->d_tag == DT_SUNW_RTLDINF) {
10790Sstevel@tonic-gate 			/*
10800Sstevel@tonic-gate 			 * If a library has the SUNW_RTLDINF .dynamic entry
10810Sstevel@tonic-gate 			 * then we must not permit lazyloading of this library.
10820Sstevel@tonic-gate 			 * This is because critical startup information (TLS
10830Sstevel@tonic-gate 			 * routines) are provided as part of these interfaces
10840Sstevel@tonic-gate 			 * and we must have them as part of process startup.
10850Sstevel@tonic-gate 			 */
10860Sstevel@tonic-gate 			ifl->ifl_flags &= ~FLG_IF_LAZYLD;
10870Sstevel@tonic-gate 		}
10880Sstevel@tonic-gate 	}
10890Sstevel@tonic-gate 
10900Sstevel@tonic-gate 	/*
10910Sstevel@tonic-gate 	 * Perform some SONAME sanity checks.
10920Sstevel@tonic-gate 	 */
10930Sstevel@tonic-gate 	if (ifl->ifl_flags & FLG_IF_NEEDED) {
10940Sstevel@tonic-gate 		Ifl_desc *	sifl;
10950Sstevel@tonic-gate 
10960Sstevel@tonic-gate 		/*
10970Sstevel@tonic-gate 		 * Determine if anyone else will cause the same SONAME to be
10980Sstevel@tonic-gate 		 * used (this is either caused by two different files having the
10990Sstevel@tonic-gate 		 * same SONAME, or by one file SONAME actually matching another
11000Sstevel@tonic-gate 		 * file basename (if no SONAME is specified within a shared
11010Sstevel@tonic-gate 		 * library its basename will be used)). Probably rare, but some
11020Sstevel@tonic-gate 		 * idiot will do it.
11030Sstevel@tonic-gate 		 */
11040Sstevel@tonic-gate 		for (LIST_TRAVERSE(&ofl->ofl_sos, lnp, sifl)) {
11050Sstevel@tonic-gate 			if ((strcmp(ifl->ifl_soname, sifl->ifl_soname) == 0) &&
11060Sstevel@tonic-gate 			    (ifl != sifl)) {
11070Sstevel@tonic-gate 				const char	*hint, *iflb, *siflb;
11080Sstevel@tonic-gate 
11090Sstevel@tonic-gate 				/*
11100Sstevel@tonic-gate 				 * Determine the basename of each file. Perhaps
11110Sstevel@tonic-gate 				 * there are multiple copies of the same file
11120Sstevel@tonic-gate 				 * being brought in using different -L search
11130Sstevel@tonic-gate 				 * paths, and if so give an extra hint in the
11140Sstevel@tonic-gate 				 * error message.
11150Sstevel@tonic-gate 				 */
11160Sstevel@tonic-gate 				iflb = strrchr(ifl->ifl_name, '/');
11170Sstevel@tonic-gate 				if (iflb == NULL)
11180Sstevel@tonic-gate 					iflb = ifl->ifl_name;
11190Sstevel@tonic-gate 				else
11200Sstevel@tonic-gate 					iflb++;
11210Sstevel@tonic-gate 
11220Sstevel@tonic-gate 				siflb = strrchr(sifl->ifl_name, '/');
11230Sstevel@tonic-gate 				if (siflb == NULL)
11240Sstevel@tonic-gate 					siflb = sifl->ifl_name;
11250Sstevel@tonic-gate 				else
11260Sstevel@tonic-gate 					siflb++;
11270Sstevel@tonic-gate 
11280Sstevel@tonic-gate 				if (strcmp(iflb, siflb) == 0)
11290Sstevel@tonic-gate 					hint = MSG_INTL(MSG_REC_CNFLTHINT);
11300Sstevel@tonic-gate 				else
11310Sstevel@tonic-gate 					hint = MSG_ORIG(MSG_STR_EMPTY);
11320Sstevel@tonic-gate 
11331618Srie 				eprintf(ofl->ofl_lml, ERR_FATAL,
11341618Srie 				    MSG_INTL(MSG_REC_OBJCNFLT), sifl->ifl_name,
11351618Srie 				    ifl->ifl_name, sifl->ifl_soname, hint);
11360Sstevel@tonic-gate 				ofl->ofl_flags |= FLG_OF_FATAL;
11370Sstevel@tonic-gate 				return (0);
11380Sstevel@tonic-gate 			}
11390Sstevel@tonic-gate 		}
11400Sstevel@tonic-gate 
11410Sstevel@tonic-gate 		/*
11420Sstevel@tonic-gate 		 * If the SONAME is the same as the name the user wishes to
11430Sstevel@tonic-gate 		 * record when building a dynamic library (refer -h option),
11440Sstevel@tonic-gate 		 * we also have a name clash.
11450Sstevel@tonic-gate 		 */
11460Sstevel@tonic-gate 		if (ofl->ofl_soname &&
11470Sstevel@tonic-gate 		    (strcmp(ofl->ofl_soname, ifl->ifl_soname) == 0)) {
11481618Srie 			eprintf(ofl->ofl_lml, ERR_FATAL,
11491618Srie 			    MSG_INTL(MSG_REC_OPTCNFLT), ifl->ifl_name,
11501618Srie 			    ifl->ifl_soname);
11510Sstevel@tonic-gate 			ofl->ofl_flags |= FLG_OF_FATAL;
11520Sstevel@tonic-gate 			return (0);
11530Sstevel@tonic-gate 		}
11540Sstevel@tonic-gate 	}
11550Sstevel@tonic-gate 	return (1);
11560Sstevel@tonic-gate }
11570Sstevel@tonic-gate 
11580Sstevel@tonic-gate /*
11590Sstevel@tonic-gate  * Process a relocation entry. At this point all input sections from this
11600Sstevel@tonic-gate  * input file have been assigned an input section descriptor which is saved
11610Sstevel@tonic-gate  * in the `ifl_isdesc' array.
11620Sstevel@tonic-gate  */
11631618Srie static uintptr_t
11640Sstevel@tonic-gate rel_process(Is_desc *isc, Ifl_desc *ifl, Ofl_desc *ofl)
11650Sstevel@tonic-gate {
11660Sstevel@tonic-gate 	Word 	rndx;
11670Sstevel@tonic-gate 	Is_desc	*risc;
11680Sstevel@tonic-gate 	Os_desc	*osp;
11690Sstevel@tonic-gate 	Shdr	*shdr = isc->is_shdr;
11700Sstevel@tonic-gate 
11710Sstevel@tonic-gate 	/*
11720Sstevel@tonic-gate 	 * Make sure this is a valid relocation we can handle.
11730Sstevel@tonic-gate 	 */
11740Sstevel@tonic-gate 	if (shdr->sh_type != M_REL_SHT_TYPE) {
11751618Srie 		eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_FIL_INVALSEC),
11761618Srie 		    ifl->ifl_name, isc->is_name,
11771976Sab196087 		    conv_sec_type(ifl->ifl_ehdr->e_machine, shdr->sh_type, 0));
11780Sstevel@tonic-gate 		ofl->ofl_flags |= FLG_OF_FATAL;
11790Sstevel@tonic-gate 		return (0);
11800Sstevel@tonic-gate 	}
11810Sstevel@tonic-gate 
11820Sstevel@tonic-gate 	/*
11830Sstevel@tonic-gate 	 * From the relocation section header information determine which
11840Sstevel@tonic-gate 	 * section needs the actual relocation.  Determine which output section
11850Sstevel@tonic-gate 	 * this input section has been assigned to and add to its relocation
11860Sstevel@tonic-gate 	 * list.  Note that the relocation section may be null if it is not
11870Sstevel@tonic-gate 	 * required (ie. .debug, .stabs, etc).
11880Sstevel@tonic-gate 	 */
11890Sstevel@tonic-gate 	rndx = shdr->sh_info;
11900Sstevel@tonic-gate 	if (rndx >= ifl->ifl_shnum) {
11910Sstevel@tonic-gate 		/*
11920Sstevel@tonic-gate 		 * Broken input file.
11930Sstevel@tonic-gate 		 */
11941618Srie 		eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_FIL_INVSHINFO),
11950Sstevel@tonic-gate 			ifl->ifl_name, isc->is_name, EC_XWORD(rndx));
11960Sstevel@tonic-gate 		ofl->ofl_flags |= FLG_OF_FATAL;
11970Sstevel@tonic-gate 		return (0);
11980Sstevel@tonic-gate 	}
11990Sstevel@tonic-gate 	if (rndx == 0) {
12000Sstevel@tonic-gate 		if (list_appendc(&ofl->ofl_extrarels, isc) == 0)
12010Sstevel@tonic-gate 			return (S_ERROR);
12020Sstevel@tonic-gate 	} else if ((risc = ifl->ifl_isdesc[rndx]) != 0) {
12030Sstevel@tonic-gate 		/*
12040Sstevel@tonic-gate 		 * Discard relocations if they are against a section
12050Sstevel@tonic-gate 		 * which has been discarded.
12060Sstevel@tonic-gate 		 */
12070Sstevel@tonic-gate 		if (risc->is_flags & FLG_IS_DISCARD)
12080Sstevel@tonic-gate 			return (1);
12090Sstevel@tonic-gate 		if ((osp = risc->is_osdesc) == 0) {
12100Sstevel@tonic-gate 			if (risc->is_shdr->sh_type == SHT_SUNW_move) {
12110Sstevel@tonic-gate 				/*
12120Sstevel@tonic-gate 				 * This section is processed later
12130Sstevel@tonic-gate 				 * in sunwmove_preprocess() and
12140Sstevel@tonic-gate 				 * reloc_init().
12150Sstevel@tonic-gate 				 */
12160Sstevel@tonic-gate 				if (list_appendc(&ofl->ofl_mvrelisdescs,
12170Sstevel@tonic-gate 				    isc) == 0)
12180Sstevel@tonic-gate 					return (S_ERROR);
12190Sstevel@tonic-gate 				return (1);
12200Sstevel@tonic-gate 			}
12211618Srie 			eprintf(ofl->ofl_lml, ERR_FATAL,
12221618Srie 			    MSG_INTL(MSG_FIL_INVRELOC1), ifl->ifl_name,
12231618Srie 			    isc->is_name, risc->is_name);
12240Sstevel@tonic-gate 			return (0);
12250Sstevel@tonic-gate 		}
12260Sstevel@tonic-gate 		if (list_appendc(&osp->os_relisdescs, isc) == 0)
12270Sstevel@tonic-gate 			return (S_ERROR);
12280Sstevel@tonic-gate 	}
12290Sstevel@tonic-gate 	return (1);
12300Sstevel@tonic-gate }
12310Sstevel@tonic-gate 
12320Sstevel@tonic-gate /*
12330Sstevel@tonic-gate  * SHF_EXCLUDE flags is set for this section.
12340Sstevel@tonic-gate  */
12350Sstevel@tonic-gate static uintptr_t
12360Sstevel@tonic-gate process_exclude(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
12370Sstevel@tonic-gate     Word ndx, Ofl_desc *ofl)
12380Sstevel@tonic-gate {
12390Sstevel@tonic-gate 	/*
12400Sstevel@tonic-gate 	 * Sections SHT_SYMTAB and SHT_DYNDYM, even if SHF_EXCLUDE is on, might
12410Sstevel@tonic-gate 	 * be needed for ld processing.  These sections need to be in the
12420Sstevel@tonic-gate 	 * internal table.  Later it will be determined whether they can be
12430Sstevel@tonic-gate 	 * eliminated or not.
12440Sstevel@tonic-gate 	 */
12450Sstevel@tonic-gate 	if (shdr->sh_type == SHT_SYMTAB || shdr->sh_type == SHT_DYNSYM)
12460Sstevel@tonic-gate 		return (0);
12470Sstevel@tonic-gate 
12480Sstevel@tonic-gate 	/*
12490Sstevel@tonic-gate 	 * Other checks
12500Sstevel@tonic-gate 	 */
12510Sstevel@tonic-gate 	if (shdr->sh_flags & SHF_ALLOC) {
12520Sstevel@tonic-gate 		/*
12530Sstevel@tonic-gate 		 * A conflict, issue an warning message, and ignore the section.
12540Sstevel@tonic-gate 		 */
12551618Srie 		eprintf(ofl->ofl_lml, ERR_WARNING, MSG_INTL(MSG_FIL_EXCLUDE),
12560Sstevel@tonic-gate 		    ifl->ifl_name, name);
12570Sstevel@tonic-gate 		return (0);
12580Sstevel@tonic-gate 	}
12590Sstevel@tonic-gate 
12600Sstevel@tonic-gate 	/*
12610Sstevel@tonic-gate 	 * This sections is not going to the output file.
12620Sstevel@tonic-gate 	 */
12630Sstevel@tonic-gate 	return (process_section(name, ifl, shdr, scn, ndx, 0, ofl));
12640Sstevel@tonic-gate }
12650Sstevel@tonic-gate 
12661618Srie #if	(defined(__i386) || defined(__amd64)) && defined(_ELF64)
12671618Srie 
12681618Srie static uintptr_t
12691618Srie process_amd64_unwind(const char *name, Ifl_desc *ifl, Shdr *shdr,
12701618Srie     Elf_Scn *scn, Word ndx, int ident, Ofl_desc *ofl)
12711618Srie {
12721618Srie 	Os_desc		*osp, *eosp;
12731618Srie 	Is_desc		*isp;
12741618Srie 	Listnode	*lnp;
12751618Srie 
12761618Srie 	if (process_section(name, ifl, shdr, scn, ndx, ident, ofl) == S_ERROR)
12771618Srie 		return (S_ERROR);
12781618Srie 
12791618Srie 	/*
12801618Srie 	 * When producing a relocatable object - just collect the sections.
12811618Srie 	 */
12821618Srie 	if (ofl->ofl_flags & FLG_OF_RELOBJ)
12831618Srie 		return (1);
12841618Srie 
12851618Srie 	/*
12861618Srie 	 * If producing a executable or shared library, keep track of all the
12871618Srie 	 * output UNWIND sections to allow the creation of the appropriate
12881618Srie 	 * frame_hdr information.
12891618Srie 	 *
12901618Srie 	 * If the section hasn't been placed in the output file, then there's
12911618Srie 	 * nothing for us to do.
12921618Srie 	 */
12931618Srie 	if (((isp = ifl->ifl_isdesc[ndx]) == 0) ||
12941618Srie 	    ((osp = isp->is_osdesc) == 0))
12951618Srie 		return (1);
12961618Srie 
12971618Srie 	/*
12981618Srie 	 * Check to see if this output section is already on the list, and if
12991618Srie 	 * not, add it.
13001618Srie 	 */
13011618Srie 	for (LIST_TRAVERSE(&ofl->ofl_unwind, lnp, eosp))
13021618Srie 		if (osp == eosp)
13031618Srie 		    return (1);
13041618Srie 
13051618Srie 	if (list_appendc(&ofl->ofl_unwind, osp) == 0)
13061618Srie 		return (S_ERROR);
13071618Srie 
13081618Srie 	return (1);
13091618Srie }
13101618Srie 
13111618Srie #endif
13121618Srie 
13130Sstevel@tonic-gate /*
13140Sstevel@tonic-gate  * Section processing state table.  `Initial' describes the required initial
13150Sstevel@tonic-gate  * procedure to be called (if any), `Final' describes the final processing
13160Sstevel@tonic-gate  * procedure (ie. things that can only be done when all required sections
13170Sstevel@tonic-gate  * have been collected).
13180Sstevel@tonic-gate  */
13190Sstevel@tonic-gate static uintptr_t (*Initial[SHT_NUM][2])() = {
13200Sstevel@tonic-gate 
13210Sstevel@tonic-gate /*			ET_REL			ET_DYN			*/
13220Sstevel@tonic-gate 
13230Sstevel@tonic-gate /* SHT_NULL	*/	invalid_section,	invalid_section,
13240Sstevel@tonic-gate /* SHT_PROGBITS	*/	process_progbits,	process_progbits,
13250Sstevel@tonic-gate /* SHT_SYMTAB	*/	process_input,		process_input,
13260Sstevel@tonic-gate /* SHT_STRTAB	*/	process_strtab,		process_strtab,
13270Sstevel@tonic-gate /* SHT_RELA	*/	process_reloc,		process_reloc,
13280Sstevel@tonic-gate /* SHT_HASH	*/	invalid_section,	NULL,
13290Sstevel@tonic-gate /* SHT_DYNAMIC	*/	process_rel_dynamic,	process_section,
13300Sstevel@tonic-gate /* SHT_NOTE	*/	process_section,	NULL,
13310Sstevel@tonic-gate /* SHT_NOBITS	*/	process_nobits,		process_nobits,
13320Sstevel@tonic-gate /* SHT_REL	*/	process_reloc,		process_reloc,
13330Sstevel@tonic-gate /* SHT_SHLIB	*/	process_section,	invalid_section,
13340Sstevel@tonic-gate /* SHT_DYNSYM	*/	invalid_section,	process_input,
13350Sstevel@tonic-gate /* SHT_UNKNOWN12 */	process_progbits,	process_progbits,
13360Sstevel@tonic-gate /* SHT_UNKNOWN13 */	process_progbits,	process_progbits,
13370Sstevel@tonic-gate /* SHT_INIT_ARRAY */	process_array,		NULL,
13380Sstevel@tonic-gate /* SHT_FINI_ARRAY */	process_array,		NULL,
13390Sstevel@tonic-gate /* SHT_PREINIT_ARRAY */	process_array,		NULL,
13400Sstevel@tonic-gate /* SHT_GROUP */		process_section,	invalid_section,
13410Sstevel@tonic-gate /* SHT_SYMTAB_SHNDX */	process_sym_shndx,	NULL
13420Sstevel@tonic-gate };
13430Sstevel@tonic-gate 
13440Sstevel@tonic-gate static uintptr_t (*Final[SHT_NUM][2])() = {
13450Sstevel@tonic-gate 
13460Sstevel@tonic-gate /* SHT_NULL	*/	NULL,			NULL,
13470Sstevel@tonic-gate /* SHT_PROGBITS	*/	NULL,			NULL,
13481618Srie /* SHT_SYMTAB	*/	ld_sym_process,		ld_sym_process,
13490Sstevel@tonic-gate /* SHT_STRTAB	*/	NULL,			NULL,
13500Sstevel@tonic-gate /* SHT_RELA	*/	rel_process,		NULL,
13510Sstevel@tonic-gate /* SHT_HASH	*/	NULL,			NULL,
13520Sstevel@tonic-gate /* SHT_DYNAMIC	*/	NULL,			process_dynamic,
13530Sstevel@tonic-gate /* SHT_NOTE	*/	NULL,			NULL,
13540Sstevel@tonic-gate /* SHT_NOBITS	*/	NULL,			NULL,
13550Sstevel@tonic-gate /* SHT_REL	*/	rel_process,		NULL,
13560Sstevel@tonic-gate /* SHT_SHLIB	*/	NULL,			NULL,
13571618Srie /* SHT_DYNSYM	*/	NULL,			ld_sym_process,
13580Sstevel@tonic-gate /* SHT_UNKNOWN12 */	NULL,			NULL,
13590Sstevel@tonic-gate /* SHT_UNKNOWN13 */	NULL,			NULL,
13600Sstevel@tonic-gate /* SHT_INIT_ARRAY */	NULL,			NULL,
13610Sstevel@tonic-gate /* SHT_FINI_ARRAY */	NULL,			NULL,
13620Sstevel@tonic-gate /* SHT_PREINIT_ARRAY */	NULL,			NULL,
13630Sstevel@tonic-gate /* SHT_GROUP */		NULL,			NULL,
13640Sstevel@tonic-gate /* SHT_SYMTAB_SHNDX */	sym_shndx_process,	NULL
13650Sstevel@tonic-gate };
13660Sstevel@tonic-gate 
13670Sstevel@tonic-gate #define	MAXNDXSIZE	10
13680Sstevel@tonic-gate 
13690Sstevel@tonic-gate /*
13700Sstevel@tonic-gate  * Process an elf file.  Each section is compared against the section state
13710Sstevel@tonic-gate  * table to determine whether it should be processed (saved), ignored, or
13720Sstevel@tonic-gate  * is invalid for the type of input file being processed.
13730Sstevel@tonic-gate  */
13741618Srie static uintptr_t
13750Sstevel@tonic-gate process_elf(Ifl_desc *ifl, Elf *elf, Ofl_desc *ofl)
13760Sstevel@tonic-gate {
13770Sstevel@tonic-gate 	Elf_Scn		*scn;
13780Sstevel@tonic-gate 	Shdr		*shdr;
13790Sstevel@tonic-gate 	Word		ndx, sndx;
13800Sstevel@tonic-gate 	char		*str, *name, _name[MAXNDXSIZE];
13810Sstevel@tonic-gate 	Word		row, column;
13820Sstevel@tonic-gate 	int		ident;
13830Sstevel@tonic-gate 	uintptr_t	error;
13840Sstevel@tonic-gate 	Is_desc		*vdfisp, *vndisp, *vsyisp, *sifisp, * capisp;
13850Sstevel@tonic-gate 	Sdf_desc	*sdf;
13860Sstevel@tonic-gate 	Word		ordered_shndx = 0; /* index to first ordered section */
13870Sstevel@tonic-gate 	Word		ordered_cnt = 0;
13880Sstevel@tonic-gate 
13890Sstevel@tonic-gate 	/*
13900Sstevel@tonic-gate 	 * First process the .shstrtab section so that later sections can
13910Sstevel@tonic-gate 	 * reference their name.
13920Sstevel@tonic-gate 	 */
13931618Srie 	ld_sup_file(ofl, ifl->ifl_name, elf_kind(elf), ifl->ifl_flags, elf);
13940Sstevel@tonic-gate 
13950Sstevel@tonic-gate 	sndx = ifl->ifl_shstrndx;
13960Sstevel@tonic-gate 	if ((scn = elf_getscn(elf, (size_t)sndx)) == NULL) {
13971618Srie 		eprintf(ofl->ofl_lml, ERR_ELF, MSG_INTL(MSG_ELF_GETSCN),
13981618Srie 		    ifl->ifl_name);
13990Sstevel@tonic-gate 		ofl->ofl_flags |= FLG_OF_FATAL;
14000Sstevel@tonic-gate 		return (0);
14010Sstevel@tonic-gate 	}
14020Sstevel@tonic-gate 	if ((shdr = elf_getshdr(scn)) == NULL) {
14031618Srie 		eprintf(ofl->ofl_lml, ERR_ELF, MSG_INTL(MSG_ELF_GETSHDR),
14041618Srie 		    ifl->ifl_name);
14050Sstevel@tonic-gate 		ofl->ofl_flags |= FLG_OF_FATAL;
14060Sstevel@tonic-gate 		return (0);
14070Sstevel@tonic-gate 	}
14080Sstevel@tonic-gate 	if ((name = elf_strptr(elf, (size_t)sndx, (size_t)shdr->sh_name)) ==
14090Sstevel@tonic-gate 	    NULL) {
14101618Srie 		eprintf(ofl->ofl_lml, ERR_ELF, MSG_INTL(MSG_ELF_STRPTR),
14111618Srie 		    ifl->ifl_name);
14120Sstevel@tonic-gate 		ofl->ofl_flags |= FLG_OF_FATAL;
14130Sstevel@tonic-gate 		return (0);
14140Sstevel@tonic-gate 	}
14150Sstevel@tonic-gate 
14162647Srie 	if (ld_sup_input_section(ofl, ifl, name, &shdr, sndx, scn,
14172647Srie 	    elf) == S_ERROR)
14180Sstevel@tonic-gate 		return (S_ERROR);
14190Sstevel@tonic-gate 
14200Sstevel@tonic-gate 	/*
14210Sstevel@tonic-gate 	 * Reset the name since the shdr->sh_name could have been changed as
14221618Srie 	 * part of ld_sup_input_section().  If there is no name, fabricate one
14230Sstevel@tonic-gate 	 * using the section index.
14240Sstevel@tonic-gate 	 */
14250Sstevel@tonic-gate 	if (shdr->sh_name == 0) {
14260Sstevel@tonic-gate 		(void) snprintf(_name, MAXNDXSIZE, MSG_ORIG(MSG_FMT_INDEX),
14270Sstevel@tonic-gate 		    EC_XWORD(sndx));
14280Sstevel@tonic-gate 		if ((name = libld_malloc(strlen(_name) + 1)) == 0)
14290Sstevel@tonic-gate 			return (S_ERROR);
14300Sstevel@tonic-gate 		(void) strcpy(name, _name);
14310Sstevel@tonic-gate 
14320Sstevel@tonic-gate 	} else if ((name = elf_strptr(elf, (size_t)sndx,
14330Sstevel@tonic-gate 	    (size_t)shdr->sh_name)) == NULL) {
14341618Srie 		eprintf(ofl->ofl_lml, ERR_ELF, MSG_INTL(MSG_ELF_STRPTR),
14351618Srie 		    ifl->ifl_name);
14360Sstevel@tonic-gate 		ofl->ofl_flags |= FLG_OF_FATAL;
14370Sstevel@tonic-gate 		return (0);
14380Sstevel@tonic-gate 	}
14390Sstevel@tonic-gate 
14400Sstevel@tonic-gate 	error = process_strtab(name, ifl, shdr, scn, sndx, FALSE, ofl);
14410Sstevel@tonic-gate 	if ((error == 0) || (error == S_ERROR))
14420Sstevel@tonic-gate 		return (error);
14430Sstevel@tonic-gate 	str = ifl->ifl_isdesc[sndx]->is_indata->d_buf;
14440Sstevel@tonic-gate 
14450Sstevel@tonic-gate 	/*
14460Sstevel@tonic-gate 	 * Determine the state table column from the input file type.  Note,
14470Sstevel@tonic-gate 	 * shared library sections are not added to the output section list.
14480Sstevel@tonic-gate 	 */
14490Sstevel@tonic-gate 	if (ifl->ifl_ehdr->e_type == ET_DYN) {
14500Sstevel@tonic-gate 		column = 1;
14510Sstevel@tonic-gate 		ofl->ofl_soscnt++;
14520Sstevel@tonic-gate 		ident = M_ID_NULL;
14530Sstevel@tonic-gate 	} else {
14540Sstevel@tonic-gate 		column = 0;
14550Sstevel@tonic-gate 		ofl->ofl_objscnt++;
14560Sstevel@tonic-gate 		ident = M_ID_UNKNOWN;
14570Sstevel@tonic-gate 	}
14580Sstevel@tonic-gate 
14591618Srie 	DBG_CALL(Dbg_file_generic(ofl->ofl_lml, ifl));
14600Sstevel@tonic-gate 	ndx = 0;
14610Sstevel@tonic-gate 	vdfisp = vndisp = vsyisp = sifisp = capisp = 0;
14620Sstevel@tonic-gate 	scn = NULL;
14630Sstevel@tonic-gate 	while (scn = elf_nextscn(elf, scn)) {
14640Sstevel@tonic-gate 		ndx++;
14650Sstevel@tonic-gate 		/*
14660Sstevel@tonic-gate 		 * As we've already processed the .shstrtab don't do it again.
14670Sstevel@tonic-gate 		 */
14680Sstevel@tonic-gate 		if (ndx == sndx)
14690Sstevel@tonic-gate 			continue;
14700Sstevel@tonic-gate 
14710Sstevel@tonic-gate 		if ((shdr = elf_getshdr(scn)) == NULL) {
14721618Srie 			eprintf(ofl->ofl_lml, ERR_ELF,
14731618Srie 			    MSG_INTL(MSG_ELF_GETSHDR), ifl->ifl_name);
14740Sstevel@tonic-gate 			ofl->ofl_flags |= FLG_OF_FATAL;
14750Sstevel@tonic-gate 			return (0);
14760Sstevel@tonic-gate 		}
14770Sstevel@tonic-gate 		name = str + (size_t)(shdr->sh_name);
14780Sstevel@tonic-gate 
14792647Srie 		if (ld_sup_input_section(ofl, ifl, name, &shdr, ndx, scn,
14802647Srie 		    elf) == S_ERROR)
14810Sstevel@tonic-gate 			return (S_ERROR);
14820Sstevel@tonic-gate 
14830Sstevel@tonic-gate 		/*
14840Sstevel@tonic-gate 		 * Reset the name since the shdr->sh_name could have been
14851618Srie 		 * changed as part of ld_sup_input_section().  If there is no
14861618Srie 		 * name, fabricate one using the section index.
14870Sstevel@tonic-gate 		 */
14880Sstevel@tonic-gate 		if (shdr->sh_name == 0) {
14890Sstevel@tonic-gate 			(void) snprintf(_name, MAXNDXSIZE,
14900Sstevel@tonic-gate 			    MSG_ORIG(MSG_FMT_INDEX), EC_XWORD(ndx));
14910Sstevel@tonic-gate 			if ((name = libld_malloc(strlen(_name) + 1)) == 0)
14920Sstevel@tonic-gate 				return (S_ERROR);
14930Sstevel@tonic-gate 			(void) strcpy(name, _name);
14940Sstevel@tonic-gate 		} else
14950Sstevel@tonic-gate 			name = str + (size_t)(shdr->sh_name);
14960Sstevel@tonic-gate 
14970Sstevel@tonic-gate 		row = shdr->sh_type;
14980Sstevel@tonic-gate 
14990Sstevel@tonic-gate 		/*
15000Sstevel@tonic-gate 		 * If the section has the SHF_EXCLUDE flag on, and we're not
15010Sstevel@tonic-gate 		 * generating a relocatable object, exclude the section.
15020Sstevel@tonic-gate 		 */
15030Sstevel@tonic-gate 		if (((shdr->sh_flags & SHF_EXCLUDE) != 0) &&
15040Sstevel@tonic-gate 		    ((ofl->ofl_flags & FLG_OF_RELOBJ) == 0)) {
15050Sstevel@tonic-gate 			if ((error = process_exclude(name, ifl, shdr, scn,
15060Sstevel@tonic-gate 			    ndx, ofl)) == S_ERROR)
15070Sstevel@tonic-gate 				return (S_ERROR);
15080Sstevel@tonic-gate 			if (error == 1)
15090Sstevel@tonic-gate 				continue;
15100Sstevel@tonic-gate 		}
15110Sstevel@tonic-gate 
15120Sstevel@tonic-gate 		/*
15130Sstevel@tonic-gate 		 * If this is a standard section type process it via the
15140Sstevel@tonic-gate 		 * appropriate action routine.
15150Sstevel@tonic-gate 		 */
15160Sstevel@tonic-gate 		if (row < SHT_NUM) {
15170Sstevel@tonic-gate 			if (Initial[row][column] != NULL) {
15180Sstevel@tonic-gate 				if (Initial[row][column](name, ifl, shdr, scn,
15190Sstevel@tonic-gate 				    ndx, ident, ofl) == S_ERROR)
15200Sstevel@tonic-gate 					return (S_ERROR);
15210Sstevel@tonic-gate 			}
15220Sstevel@tonic-gate 		} else {
15230Sstevel@tonic-gate 			/*
15240Sstevel@tonic-gate 			 * If this section is below SHT_LOSUNW then we don't
15250Sstevel@tonic-gate 			 * really know what to do with it, issue a warning
15260Sstevel@tonic-gate 			 * message but do the basic section processing anyway.
15270Sstevel@tonic-gate 			 */
15280Sstevel@tonic-gate 			if (row < (Word)SHT_LOSUNW)
15291618Srie 				eprintf(ofl->ofl_lml, ERR_WARNING,
15301618Srie 				    MSG_INTL(MSG_FIL_INVALSEC), ifl->ifl_name,
15311618Srie 				    name,
15321618Srie 				    conv_sec_type(ifl->ifl_ehdr->e_machine,
15331976Sab196087 				    shdr->sh_type, 0));
15340Sstevel@tonic-gate 
15350Sstevel@tonic-gate 			/*
15360Sstevel@tonic-gate 			 * Handle sections greater than SHT_LOSUNW.
15370Sstevel@tonic-gate 			 */
15380Sstevel@tonic-gate 			switch (row) {
15390Sstevel@tonic-gate 			case SHT_SUNW_dof:
15400Sstevel@tonic-gate 				if (process_section(name, ifl, shdr, scn,
15410Sstevel@tonic-gate 				    ndx, ident, ofl) == S_ERROR)
15420Sstevel@tonic-gate 					return (S_ERROR);
15430Sstevel@tonic-gate 				break;
15440Sstevel@tonic-gate 			case SHT_SUNW_cap:
15450Sstevel@tonic-gate 				if (process_section(name, ifl, shdr, scn,
15460Sstevel@tonic-gate 				    ndx, M_ID_NULL, ofl) == S_ERROR)
15470Sstevel@tonic-gate 					return (S_ERROR);
15480Sstevel@tonic-gate 				capisp = ifl->ifl_isdesc[ndx];
15490Sstevel@tonic-gate 				break;
15500Sstevel@tonic-gate 			case SHT_SUNW_DEBUGSTR:
15510Sstevel@tonic-gate 			case SHT_SUNW_DEBUG:
15520Sstevel@tonic-gate 				if (process_debug(name, ifl, shdr, scn,
15530Sstevel@tonic-gate 				    ndx, ident, ofl) == S_ERROR)
15540Sstevel@tonic-gate 					return (S_ERROR);
15550Sstevel@tonic-gate 				break;
15560Sstevel@tonic-gate 			case SHT_SUNW_move:
15570Sstevel@tonic-gate 				if (process_section(name, ifl, shdr, scn,
15580Sstevel@tonic-gate 				    ndx, M_ID_NULL, ofl) == S_ERROR)
15590Sstevel@tonic-gate 					return (S_ERROR);
15600Sstevel@tonic-gate 				break;
15610Sstevel@tonic-gate 			case SHT_SUNW_syminfo:
15620Sstevel@tonic-gate 				if (process_section(name, ifl, shdr, scn,
15630Sstevel@tonic-gate 				    ndx, M_ID_NULL, ofl) == S_ERROR)
15640Sstevel@tonic-gate 					return (S_ERROR);
15650Sstevel@tonic-gate 				sifisp = ifl->ifl_isdesc[ndx];
15660Sstevel@tonic-gate 				break;
15670Sstevel@tonic-gate 			case SHT_SUNW_ANNOTATE:
15680Sstevel@tonic-gate 			case SHT_SUNW_COMDAT:
15690Sstevel@tonic-gate 				if (process_progbits(name, ifl, shdr, scn,
15700Sstevel@tonic-gate 				    ndx, ident, ofl) == S_ERROR)
15710Sstevel@tonic-gate 					return (S_ERROR);
15720Sstevel@tonic-gate 				break;
15730Sstevel@tonic-gate 			case SHT_SUNW_verdef:
15740Sstevel@tonic-gate 				if (process_section(name, ifl, shdr, scn,
15750Sstevel@tonic-gate 				    ndx, M_ID_NULL, ofl) == S_ERROR)
15760Sstevel@tonic-gate 					return (S_ERROR);
15770Sstevel@tonic-gate 				vdfisp = ifl->ifl_isdesc[ndx];
15780Sstevel@tonic-gate 				break;
15790Sstevel@tonic-gate 			case SHT_SUNW_verneed:
15800Sstevel@tonic-gate 				if (process_section(name, ifl, shdr, scn,
15810Sstevel@tonic-gate 				    ndx, M_ID_NULL, ofl) == S_ERROR)
15820Sstevel@tonic-gate 					return (S_ERROR);
15830Sstevel@tonic-gate 				vndisp = ifl->ifl_isdesc[ndx];
15840Sstevel@tonic-gate 				break;
15850Sstevel@tonic-gate 			case SHT_SUNW_versym:
15860Sstevel@tonic-gate 				if (process_section(name, ifl, shdr, scn,
15870Sstevel@tonic-gate 				    ndx, M_ID_NULL, ofl) == S_ERROR)
15880Sstevel@tonic-gate 					return (S_ERROR);
15890Sstevel@tonic-gate 				vsyisp = ifl->ifl_isdesc[ndx];
15900Sstevel@tonic-gate 				break;
15910Sstevel@tonic-gate #if	defined(sparc)
15920Sstevel@tonic-gate 			case SHT_SPARC_GOTDATA:
15930Sstevel@tonic-gate 				if (process_section(name, ifl, shdr, scn,
15940Sstevel@tonic-gate 				    ndx, M_ID_GOTDATA, ofl) == S_ERROR)
15950Sstevel@tonic-gate 					return (S_ERROR);
15960Sstevel@tonic-gate 				break;
15970Sstevel@tonic-gate #endif
15980Sstevel@tonic-gate #if	(defined(__i386) || defined(__amd64)) && defined(_ELF64)
15990Sstevel@tonic-gate 			case SHT_AMD64_UNWIND:
16000Sstevel@tonic-gate 				if (column == 0) {
16010Sstevel@tonic-gate 					/*
16020Sstevel@tonic-gate 					 * column == ET_REL
16030Sstevel@tonic-gate 					 */
16040Sstevel@tonic-gate 					if (process_amd64_unwind(name, ifl,
16051618Srie 					    shdr, scn, ndx, M_ID_UNWIND,
16060Sstevel@tonic-gate 					    ofl) == S_ERROR)
16070Sstevel@tonic-gate 						return (S_ERROR);
16080Sstevel@tonic-gate 				}
16090Sstevel@tonic-gate 				break;
16100Sstevel@tonic-gate #endif
16110Sstevel@tonic-gate 			default:
16120Sstevel@tonic-gate 				if (ident != M_ID_NULL)
16130Sstevel@tonic-gate 					ident = M_ID_USER;
16140Sstevel@tonic-gate 				if (process_section(name, ifl, shdr, scn,
16150Sstevel@tonic-gate 				    ndx, ident, ofl) == S_ERROR)
16160Sstevel@tonic-gate 					return (S_ERROR);
16170Sstevel@tonic-gate 				break;
16180Sstevel@tonic-gate 			}
16190Sstevel@tonic-gate 		}
16200Sstevel@tonic-gate 
16210Sstevel@tonic-gate 		/*
16220Sstevel@tonic-gate 		 * If we have any sections that require ORDERED processing,
16230Sstevel@tonic-gate 		 * remember the index of the first ordered section.  This let's
16240Sstevel@tonic-gate 		 * us know if we need an ORDERED place_section pass, and if so,
16250Sstevel@tonic-gate 		 * where to start.
16260Sstevel@tonic-gate 		 */
16270Sstevel@tonic-gate 		if (ifl->ifl_isdesc[ndx] &&
16280Sstevel@tonic-gate 		    (ifl->ifl_isdesc[ndx]->is_shdr->sh_flags & ALL_SHF_ORDER)) {
16290Sstevel@tonic-gate 			ordered_cnt++;
16300Sstevel@tonic-gate 			if (ordered_shndx == 0)
16310Sstevel@tonic-gate 				ordered_shndx = ndx;
16320Sstevel@tonic-gate 		}
16330Sstevel@tonic-gate 	}
16340Sstevel@tonic-gate 
16350Sstevel@tonic-gate 	/*
16360Sstevel@tonic-gate 	 * Now that all of sections have been placed, scan through any sections
16370Sstevel@tonic-gate 	 * which have special ordering requirements and place them now.
16380Sstevel@tonic-gate 	 */
16390Sstevel@tonic-gate 	if (ordered_shndx) {
16400Sstevel@tonic-gate 		Word	cnt;
16410Sstevel@tonic-gate 
16420Sstevel@tonic-gate 		for (ndx = ordered_shndx, cnt = 0;
16430Sstevel@tonic-gate 		    (ndx < ifl->ifl_shnum) && (cnt < ordered_cnt); ndx++) {
16440Sstevel@tonic-gate 			Is_desc	*isp;
16450Sstevel@tonic-gate 			/* LINTED */
16460Sstevel@tonic-gate 			Os_desc	*osp;
16470Sstevel@tonic-gate 
16480Sstevel@tonic-gate 			if (((isp = ifl->ifl_isdesc[ndx]) == 0) ||
16490Sstevel@tonic-gate 			    ((isp->is_shdr->sh_flags & ALL_SHF_ORDER) == 0))
16500Sstevel@tonic-gate 				continue;
16510Sstevel@tonic-gate 
16520Sstevel@tonic-gate 			/*
16530Sstevel@tonic-gate 			 * If this is an ordered section, process it.
16540Sstevel@tonic-gate 			 */
16550Sstevel@tonic-gate 			cnt++;
16561618Srie 			if ((osp = (Os_desc *)ld_process_ordered(ifl, ofl, ndx,
16570Sstevel@tonic-gate 			    ifl->ifl_shnum)) == (Os_desc *)S_ERROR)
16580Sstevel@tonic-gate 				return (S_ERROR);
16590Sstevel@tonic-gate 
16600Sstevel@tonic-gate #if	(defined(__i386) || defined(__amd64)) && defined(_ELF64)
16610Sstevel@tonic-gate 			/*
16620Sstevel@tonic-gate 			 * If this section is 'ordered' then it was not
16630Sstevel@tonic-gate 			 * caught in the previous 'place_section' operation.
16640Sstevel@tonic-gate 			 *
16650Sstevel@tonic-gate 			 * So - now that we have a OSP section for
16660Sstevel@tonic-gate 			 * the unwind info - record it.
16670Sstevel@tonic-gate 			 */
16680Sstevel@tonic-gate 			if (osp &&
16690Sstevel@tonic-gate 			    (osp->os_shdr->sh_type == SHT_AMD64_UNWIND) &&
16700Sstevel@tonic-gate 			    (append_amd64_unwind(osp, ofl) == S_ERROR))
16710Sstevel@tonic-gate 				return (S_ERROR);
16720Sstevel@tonic-gate #endif
16730Sstevel@tonic-gate 		}
16740Sstevel@tonic-gate 	}
16750Sstevel@tonic-gate 
16760Sstevel@tonic-gate 	/*
16771109Srie 	 * If this is an explicit shared object determine if the user has
16780Sstevel@tonic-gate 	 * specified a control definition.  This descriptor may specify which
16790Sstevel@tonic-gate 	 * version definitions can be used from this object (it may also update
16800Sstevel@tonic-gate 	 * the dependency to USED and supply an alternative SONAME).
16810Sstevel@tonic-gate 	 */
16820Sstevel@tonic-gate 	sdf = 0;
16830Sstevel@tonic-gate 	if (column && (ifl->ifl_flags & FLG_IF_NEEDED)) {
16840Sstevel@tonic-gate 		const char	*base;
16850Sstevel@tonic-gate 
16860Sstevel@tonic-gate 		/*
16870Sstevel@tonic-gate 		 * Use the basename of the input file (typically this is the
16880Sstevel@tonic-gate 		 * compilation environment name, ie. libfoo.so).
16890Sstevel@tonic-gate 		 */
16900Sstevel@tonic-gate 		if ((base = strrchr(ifl->ifl_name, '/')) == NULL)
16910Sstevel@tonic-gate 			base = ifl->ifl_name;
16920Sstevel@tonic-gate 		else
16930Sstevel@tonic-gate 			base++;
16940Sstevel@tonic-gate 
16950Sstevel@tonic-gate 		if ((sdf = sdf_find(base, &ofl->ofl_socntl)) != 0) {
16960Sstevel@tonic-gate 			sdf->sdf_file = ifl;
16970Sstevel@tonic-gate 			ifl->ifl_sdfdesc = sdf;
16980Sstevel@tonic-gate 		}
16990Sstevel@tonic-gate 	}
17000Sstevel@tonic-gate 
17010Sstevel@tonic-gate 	/*
17020Sstevel@tonic-gate 	 * Process any hardware/software capabilities sections.  Only propagate
17030Sstevel@tonic-gate 	 * capabilities for input relocatable objects.  If the object doesn't
17040Sstevel@tonic-gate 	 * contain any capabilities, any capability state that has already been
17050Sstevel@tonic-gate 	 * gathered will prevail.
17060Sstevel@tonic-gate 	 */
17070Sstevel@tonic-gate 	if (capisp && (ifl->ifl_ehdr->e_type == ET_REL))
17080Sstevel@tonic-gate 		process_cap(name, ifl, capisp, ofl);
17090Sstevel@tonic-gate 
17100Sstevel@tonic-gate 	/*
17110Sstevel@tonic-gate 	 * Process any version dependencies.  These will establish shared object
17120Sstevel@tonic-gate 	 * `needed' entries in the same manner as will be generated from the
17130Sstevel@tonic-gate 	 * .dynamic's NEEDED entries.
17140Sstevel@tonic-gate 	 */
17150Sstevel@tonic-gate 	if (vndisp && (ofl->ofl_flags & (FLG_OF_NOUNDEF | FLG_OF_SYMBOLIC)))
17161618Srie 		if (ld_vers_need_process(vndisp, ifl, ofl) == S_ERROR)
17170Sstevel@tonic-gate 			return (S_ERROR);
17180Sstevel@tonic-gate 
17190Sstevel@tonic-gate 	/*
17200Sstevel@tonic-gate 	 * Before processing any symbol resolution or relocations process any
17210Sstevel@tonic-gate 	 * version sections.
17220Sstevel@tonic-gate 	 */
17230Sstevel@tonic-gate 	if (vsyisp)
17241618Srie 		(void) ld_vers_sym_process(ofl->ofl_lml, vsyisp, ifl);
17250Sstevel@tonic-gate 
17260Sstevel@tonic-gate 	if (ifl->ifl_versym &&
17270Sstevel@tonic-gate 	    (vdfisp || (sdf && (sdf->sdf_flags & FLG_SDF_SELECT))))
17281618Srie 		if (ld_vers_def_process(vdfisp, ifl, ofl) == S_ERROR)
17290Sstevel@tonic-gate 			return (S_ERROR);
17300Sstevel@tonic-gate 
17310Sstevel@tonic-gate 	/*
17320Sstevel@tonic-gate 	 * Having collected the appropriate sections carry out any additional
17330Sstevel@tonic-gate 	 * processing if necessary.
17340Sstevel@tonic-gate 	 */
17350Sstevel@tonic-gate 	for (ndx = 0; ndx < ifl->ifl_shnum; ndx++) {
17360Sstevel@tonic-gate 		Is_desc *	isp;
17370Sstevel@tonic-gate 
17380Sstevel@tonic-gate 		if ((isp = ifl->ifl_isdesc[ndx]) == 0)
17390Sstevel@tonic-gate 			continue;
17400Sstevel@tonic-gate 		row = isp->is_shdr->sh_type;
17410Sstevel@tonic-gate 
17420Sstevel@tonic-gate 		if ((isp->is_flags & FLG_IS_DISCARD) == 0)
17431618Srie 			ld_sup_section(ofl, isp->is_name, isp->is_shdr, ndx,
17441618Srie 			    isp->is_indata, elf);
17450Sstevel@tonic-gate 
17460Sstevel@tonic-gate 		/*
17470Sstevel@tonic-gate 		 * If this is a ST_SUNW_move section from a
17480Sstevel@tonic-gate 		 * a relocatable file, keep the input section.
17490Sstevel@tonic-gate 		 */
17500Sstevel@tonic-gate 		if ((row == SHT_SUNW_move) && (column == 0)) {
17510Sstevel@tonic-gate 			if (list_appendc(&(ofl->ofl_ismove), isp) == 0)
17520Sstevel@tonic-gate 				return (S_ERROR);
17530Sstevel@tonic-gate 		}
17540Sstevel@tonic-gate 
17550Sstevel@tonic-gate 		/*
17560Sstevel@tonic-gate 		 * If this is a standard section type process it via the
17570Sstevel@tonic-gate 		 * appropriate action routine.
17580Sstevel@tonic-gate 		 */
17590Sstevel@tonic-gate 		if (row < SHT_NUM) {
17600Sstevel@tonic-gate 			if (Final[row][column] != NULL)
17610Sstevel@tonic-gate 				if (Final[row][column](isp, ifl, ofl) ==
17620Sstevel@tonic-gate 				    S_ERROR)
17630Sstevel@tonic-gate 					return (S_ERROR);
17640Sstevel@tonic-gate 		}
17650Sstevel@tonic-gate 	}
17660Sstevel@tonic-gate 
17670Sstevel@tonic-gate 	/*
17680Sstevel@tonic-gate 	 * After processing any symbol resolution, and if this dependency
17690Sstevel@tonic-gate 	 * indicates it contains symbols that can't be directly bound to,
17700Sstevel@tonic-gate 	 * set the symbols appropriately.
17710Sstevel@tonic-gate 	 */
17720Sstevel@tonic-gate 	if (sifisp && ((ifl->ifl_flags & (FLG_IF_NEEDED | FLG_IF_NODIRECT)) ==
17730Sstevel@tonic-gate 	    (FLG_IF_NEEDED | FLG_IF_NODIRECT)))
17741618Srie 		(void) ld_sym_nodirect(sifisp, ifl, ofl);
17750Sstevel@tonic-gate 
17760Sstevel@tonic-gate 	return (1);
17770Sstevel@tonic-gate }
17780Sstevel@tonic-gate 
17790Sstevel@tonic-gate /*
17800Sstevel@tonic-gate  * Process the current input file.  There are basically three types of files
17810Sstevel@tonic-gate  * that come through here:
17820Sstevel@tonic-gate  *
17830Sstevel@tonic-gate  *  o	files explicitly defined on the command line (ie. foo.o or bar.so),
17840Sstevel@tonic-gate  *	in this case only the `name' field is valid.
17850Sstevel@tonic-gate  *
17860Sstevel@tonic-gate  *  o	libraries determined from the -l command line option (ie. -lbar),
17870Sstevel@tonic-gate  *	in this case the `soname' field contains the basename of the located
17880Sstevel@tonic-gate  *	file.
17890Sstevel@tonic-gate  *
17900Sstevel@tonic-gate  * Any shared object specified via the above two conventions must be recorded
17910Sstevel@tonic-gate  * as a needed dependency.
17920Sstevel@tonic-gate  *
17930Sstevel@tonic-gate  *  o	libraries specified as dependencies of those libraries already obtained
17940Sstevel@tonic-gate  *	via the command line (ie. bar.so has a DT_NEEDED entry of fred.so.1),
17950Sstevel@tonic-gate  *	in this case the `soname' field contains either a full pathname (if the
17960Sstevel@tonic-gate  *	needed entry contained a `/'), or the basename of the located file.
17970Sstevel@tonic-gate  *	These libraries are processed to verify symbol binding but are not
17980Sstevel@tonic-gate  *	recorded as dependencies of the output file being generated.
17990Sstevel@tonic-gate  */
18000Sstevel@tonic-gate Ifl_desc *
18011618Srie ld_process_ifl(const char *name, const char *soname, int fd, Elf *elf,
18020Sstevel@tonic-gate     Half flags, Ofl_desc * ofl, Rej_desc * rej)
18030Sstevel@tonic-gate {
18040Sstevel@tonic-gate 	Ifl_desc	*ifl;
18050Sstevel@tonic-gate 	Ehdr		*ehdr;
18060Sstevel@tonic-gate 	Listnode	*lnp;
18070Sstevel@tonic-gate 	uintptr_t	error = 0;
18080Sstevel@tonic-gate 	struct stat	status;
18090Sstevel@tonic-gate 	Ar_desc		*adp;
18100Sstevel@tonic-gate 	Rej_desc	_rej;
18110Sstevel@tonic-gate 
18120Sstevel@tonic-gate 	/*
18130Sstevel@tonic-gate 	 * If this file was not extracted from an archive obtain its device
18140Sstevel@tonic-gate 	 * information.  This will be used to determine if the file has already
18150Sstevel@tonic-gate 	 * been processed (rather than simply comparing filenames, the device
18160Sstevel@tonic-gate 	 * information provides a quicker comparison and detects linked files).
18170Sstevel@tonic-gate 	 */
18180Sstevel@tonic-gate 	if (!(flags & FLG_IF_EXTRACT))
18190Sstevel@tonic-gate 		(void) fstat(fd, &status);
18200Sstevel@tonic-gate 	else {
18210Sstevel@tonic-gate 		status.st_dev = 0;
18220Sstevel@tonic-gate 		status.st_ino = 0;
18230Sstevel@tonic-gate 	}
18240Sstevel@tonic-gate 
18250Sstevel@tonic-gate 	switch (elf_kind(elf)) {
18260Sstevel@tonic-gate 	case ELF_K_AR:
18270Sstevel@tonic-gate 		/*
18280Sstevel@tonic-gate 		 * Determine if we've already come across this archive file.
18290Sstevel@tonic-gate 		 */
18300Sstevel@tonic-gate 		if (!(flags & FLG_IF_EXTRACT)) {
18310Sstevel@tonic-gate 			for (LIST_TRAVERSE(&ofl->ofl_ars, lnp, adp)) {
18320Sstevel@tonic-gate 				if ((adp->ad_stdev != status.st_dev) ||
18330Sstevel@tonic-gate 				    (adp->ad_stino != status.st_ino))
18340Sstevel@tonic-gate 					continue;
18350Sstevel@tonic-gate 
18360Sstevel@tonic-gate 				/*
18370Sstevel@tonic-gate 				 * We've seen this file before so reuse the
18380Sstevel@tonic-gate 				 * original archive descriptor and discard the
18390Sstevel@tonic-gate 				 * new elf descriptor.
18400Sstevel@tonic-gate 				 */
18411618Srie 				DBG_CALL(Dbg_file_reuse(ofl->ofl_lml, name,
18421618Srie 				    adp->ad_name));
18430Sstevel@tonic-gate 				(void) elf_end(elf);
18441618Srie 				return ((Ifl_desc *)ld_process_archive(name, fd,
18450Sstevel@tonic-gate 				    adp, ofl));
18460Sstevel@tonic-gate 			}
18470Sstevel@tonic-gate 		}
18480Sstevel@tonic-gate 
18490Sstevel@tonic-gate 		/*
18500Sstevel@tonic-gate 		 * As we haven't processed this file before establish a new
18510Sstevel@tonic-gate 		 * archive descriptor.
18520Sstevel@tonic-gate 		 */
18531618Srie 		adp = ld_ar_setup(name, elf, ofl);
18540Sstevel@tonic-gate 		if ((adp == 0) || (adp == (Ar_desc *)S_ERROR))
18550Sstevel@tonic-gate 			return ((Ifl_desc *)adp);
18560Sstevel@tonic-gate 		adp->ad_stdev = status.st_dev;
18570Sstevel@tonic-gate 		adp->ad_stino = status.st_ino;
18580Sstevel@tonic-gate 
18591618Srie 		ld_sup_file(ofl, name, ELF_K_AR, flags, elf);
18600Sstevel@tonic-gate 
18611618Srie 		return ((Ifl_desc *)ld_process_archive(name, fd, adp, ofl));
18620Sstevel@tonic-gate 
18630Sstevel@tonic-gate 	case ELF_K_ELF:
18640Sstevel@tonic-gate 		/*
18650Sstevel@tonic-gate 		 * Obtain the elf header so that we can determine what type of
18660Sstevel@tonic-gate 		 * elf ELF_K_ELF file this is.
18670Sstevel@tonic-gate 		 */
18680Sstevel@tonic-gate 		if ((ehdr = elf_getehdr(elf)) == NULL) {
18690Sstevel@tonic-gate 			int	_class = gelf_getclass(elf);
18700Sstevel@tonic-gate 
18710Sstevel@tonic-gate 			/*
18720Sstevel@tonic-gate 			 * Failure could occur for a number of reasons at this
18730Sstevel@tonic-gate 			 * point.  Typically the files class is incorrect (ie.
18740Sstevel@tonic-gate 			 * user is building 64-bit but managed to pint at 32-bit
18750Sstevel@tonic-gate 			 * libraries).  However any number of elf errors can
18760Sstevel@tonic-gate 			 * also occur, such as from a truncated or corrupt file.
18770Sstevel@tonic-gate 			 * Here we try and get the best error message possible.
18780Sstevel@tonic-gate 			 */
18790Sstevel@tonic-gate 			if (M_CLASS != _class) {
18800Sstevel@tonic-gate 				_rej.rej_type = SGS_REJ_CLASS;
18810Sstevel@tonic-gate 				_rej.rej_info = (uint_t)_class;
18820Sstevel@tonic-gate 			} else {
18830Sstevel@tonic-gate 				_rej.rej_type = SGS_REJ_STR;
18840Sstevel@tonic-gate 				_rej.rej_str = elf_errmsg(-1);
18850Sstevel@tonic-gate 			}
18860Sstevel@tonic-gate 			_rej.rej_name = name;
18871618Srie 			DBG_CALL(Dbg_file_rejected(ofl->ofl_lml, &_rej));
18880Sstevel@tonic-gate 			if (rej->rej_type == 0) {
18890Sstevel@tonic-gate 				*rej = _rej;
18900Sstevel@tonic-gate 				rej->rej_name = strdup(_rej.rej_name);
18910Sstevel@tonic-gate 			}
18920Sstevel@tonic-gate 			return (0);
18930Sstevel@tonic-gate 		}
18940Sstevel@tonic-gate 
18950Sstevel@tonic-gate 		/*
18960Sstevel@tonic-gate 		 * Determine if we've already come across this file.
18970Sstevel@tonic-gate 		 */
18980Sstevel@tonic-gate 		if (!(flags & FLG_IF_EXTRACT)) {
18990Sstevel@tonic-gate 			List *	lst;
19000Sstevel@tonic-gate 
19010Sstevel@tonic-gate 			if (ehdr->e_type == ET_REL)
19020Sstevel@tonic-gate 				lst = &ofl->ofl_objs;
19030Sstevel@tonic-gate 			else
19040Sstevel@tonic-gate 				lst = &ofl->ofl_sos;
19050Sstevel@tonic-gate 
19060Sstevel@tonic-gate 			/*
19070Sstevel@tonic-gate 			 * Traverse the appropriate file list and determine if
19080Sstevel@tonic-gate 			 * a dev/inode match is found.
19090Sstevel@tonic-gate 			 */
19100Sstevel@tonic-gate 			for (LIST_TRAVERSE(lst, lnp, ifl)) {
19110Sstevel@tonic-gate 				/*
19120Sstevel@tonic-gate 				 * Ifl_desc generated via -Nneed, therefore no
19130Sstevel@tonic-gate 				 * actual file behind it.
19140Sstevel@tonic-gate 				 */
19150Sstevel@tonic-gate 				if (ifl->ifl_flags & FLG_IF_NEEDSTR)
19160Sstevel@tonic-gate 					continue;
19170Sstevel@tonic-gate 
19180Sstevel@tonic-gate 				if ((ifl->ifl_stino != status.st_ino) ||
19190Sstevel@tonic-gate 				    (ifl->ifl_stdev != status.st_dev))
19200Sstevel@tonic-gate 					continue;
19210Sstevel@tonic-gate 
19220Sstevel@tonic-gate 				/*
19230Sstevel@tonic-gate 				 * Disregard (skip) this image.
19240Sstevel@tonic-gate 				 */
19251618Srie 				DBG_CALL(Dbg_file_skip(ofl->ofl_lml,
19261618Srie 				    ifl->ifl_name, name));
19270Sstevel@tonic-gate 				(void) elf_end(elf);
19280Sstevel@tonic-gate 
19290Sstevel@tonic-gate 				/*
19300Sstevel@tonic-gate 				 * If the file was explicitly defined on the
19310Sstevel@tonic-gate 				 * command line (this is always the case for
19320Sstevel@tonic-gate 				 * relocatable objects, and is true for shared
19330Sstevel@tonic-gate 				 * objects when they weren't specified via -l or
19340Sstevel@tonic-gate 				 * were dragged in as an implicit dependency),
19350Sstevel@tonic-gate 				 * then warn the user.
19360Sstevel@tonic-gate 				 */
19370Sstevel@tonic-gate 				if ((flags & FLG_IF_CMDLINE) ||
19380Sstevel@tonic-gate 				    (ifl->ifl_flags & FLG_IF_CMDLINE)) {
19390Sstevel@tonic-gate 					const char	*errmsg;
19400Sstevel@tonic-gate 
19410Sstevel@tonic-gate 					/*
19420Sstevel@tonic-gate 					 * Determine whether this is the same
19430Sstevel@tonic-gate 					 * file name as originally encountered
19440Sstevel@tonic-gate 					 * so as to provide the most
19450Sstevel@tonic-gate 					 * descriptive diagnostic.
19460Sstevel@tonic-gate 					 */
19470Sstevel@tonic-gate 					if (strcmp(name, ifl->ifl_name) == 0)
19480Sstevel@tonic-gate 					    errmsg = MSG_INTL(MSG_FIL_MULINC_1);
19490Sstevel@tonic-gate 					else
19500Sstevel@tonic-gate 					    errmsg = MSG_INTL(MSG_FIL_MULINC_2);
19510Sstevel@tonic-gate 
19521618Srie 					eprintf(ofl->ofl_lml, ERR_WARNING,
19531618Srie 					    errmsg, name, ifl->ifl_name);
19540Sstevel@tonic-gate 				}
19550Sstevel@tonic-gate 				return (ifl);
19560Sstevel@tonic-gate 			}
19570Sstevel@tonic-gate 		}
19580Sstevel@tonic-gate 
19590Sstevel@tonic-gate 		/*
19600Sstevel@tonic-gate 		 * At this point, we know we need the file.  Establish an input
19610Sstevel@tonic-gate 		 * file descriptor and continue processing.
19620Sstevel@tonic-gate 		 */
19630Sstevel@tonic-gate 		ifl = ifl_setup(name, ehdr, elf, flags, ofl, rej);
19640Sstevel@tonic-gate 		if ((ifl == 0) || (ifl == (Ifl_desc *)S_ERROR))
19650Sstevel@tonic-gate 			return (ifl);
19660Sstevel@tonic-gate 		ifl->ifl_stdev = status.st_dev;
19670Sstevel@tonic-gate 		ifl->ifl_stino = status.st_ino;
19680Sstevel@tonic-gate 
19690Sstevel@tonic-gate 		/*
19700Sstevel@tonic-gate 		 * If -zignore is in effect, mark this file as a potential
19710Sstevel@tonic-gate 		 * candidate (the files use isn't actually determined until
19720Sstevel@tonic-gate 		 * symbol resolution and relocation processing are completed).
19730Sstevel@tonic-gate 		 */
19740Sstevel@tonic-gate 		if (ofl->ofl_flags1 & FLG_OF1_IGNORE)
19750Sstevel@tonic-gate 			ifl->ifl_flags |= FLG_IF_IGNORE;
19760Sstevel@tonic-gate 
19770Sstevel@tonic-gate 		switch (ehdr->e_type) {
19780Sstevel@tonic-gate 		case ET_REL:
19791618Srie 			ld_mach_eflags(ehdr, ofl);
19800Sstevel@tonic-gate 			error = process_elf(ifl, elf, ofl);
19810Sstevel@tonic-gate 			break;
19820Sstevel@tonic-gate 		case ET_DYN:
19830Sstevel@tonic-gate 			if ((ofl->ofl_flags & FLG_OF_STATIC) ||
19840Sstevel@tonic-gate 			    !(ofl->ofl_flags & FLG_OF_DYNLIBS)) {
19851618Srie 				eprintf(ofl->ofl_lml, ERR_FATAL,
19861618Srie 				    MSG_INTL(MSG_FIL_SOINSTAT), name);
19870Sstevel@tonic-gate 				ofl->ofl_flags |= FLG_OF_FATAL;
19880Sstevel@tonic-gate 				return (0);
19890Sstevel@tonic-gate 			}
19900Sstevel@tonic-gate 
19910Sstevel@tonic-gate 			/*
19920Sstevel@tonic-gate 			 * Record any additional shared object information.
19930Sstevel@tonic-gate 			 * If no soname is specified (eg. this file was
19940Sstevel@tonic-gate 			 * derived from a explicit filename declaration on the
19950Sstevel@tonic-gate 			 * command line, ie. bar.so) use the pathname.
19960Sstevel@tonic-gate 			 * This entry may be overridden if the files dynamic
19970Sstevel@tonic-gate 			 * section specifies an DT_SONAME value.
19980Sstevel@tonic-gate 			 */
19990Sstevel@tonic-gate 			if (soname == NULL)
20000Sstevel@tonic-gate 				ifl->ifl_soname = ifl->ifl_name;
20010Sstevel@tonic-gate 			else
20020Sstevel@tonic-gate 				ifl->ifl_soname = soname;
20030Sstevel@tonic-gate 
20040Sstevel@tonic-gate 			/*
20050Sstevel@tonic-gate 			 * If direct bindings, lazy loading, or group
20060Sstevel@tonic-gate 			 * permissions need to be established, mark this object.
20070Sstevel@tonic-gate 			 */
20080Sstevel@tonic-gate 			if (ofl->ofl_flags1 & FLG_OF1_ZDIRECT)
20090Sstevel@tonic-gate 				ifl->ifl_flags |= FLG_IF_DIRECT;
20100Sstevel@tonic-gate 			if (ofl->ofl_flags1 & FLG_OF1_LAZYLD)
20110Sstevel@tonic-gate 				ifl->ifl_flags |= FLG_IF_LAZYLD;
20120Sstevel@tonic-gate 			if (ofl->ofl_flags1 & FLG_OF1_GRPPRM)
20130Sstevel@tonic-gate 				ifl->ifl_flags |= FLG_IF_GRPPRM;
20140Sstevel@tonic-gate 			error = process_elf(ifl, elf, ofl);
20150Sstevel@tonic-gate 
20160Sstevel@tonic-gate 			/*
20170Sstevel@tonic-gate 			 * At this point we know if this file will be
20180Sstevel@tonic-gate 			 * lazyloaded, or whether bindings to it must be direct.
20190Sstevel@tonic-gate 			 * In either case, a syminfo section is required.
20200Sstevel@tonic-gate 			 */
20210Sstevel@tonic-gate 			if (ifl->ifl_flags & (FLG_IF_LAZYLD | FLG_IF_DIRECT))
20220Sstevel@tonic-gate 				ofl->ofl_flags |= FLG_OF_SYMINFO;
20230Sstevel@tonic-gate 
20240Sstevel@tonic-gate 			break;
20250Sstevel@tonic-gate 		default:
20260Sstevel@tonic-gate 			(void) elf_errno();
20270Sstevel@tonic-gate 			_rej.rej_type = SGS_REJ_UNKFILE;
20280Sstevel@tonic-gate 			_rej.rej_name = name;
20291618Srie 			DBG_CALL(Dbg_file_rejected(ofl->ofl_lml, &_rej));
20300Sstevel@tonic-gate 			if (rej->rej_type == 0) {
20310Sstevel@tonic-gate 				*rej = _rej;
20320Sstevel@tonic-gate 				rej->rej_name = strdup(_rej.rej_name);
20330Sstevel@tonic-gate 			}
20340Sstevel@tonic-gate 			return (0);
20350Sstevel@tonic-gate 		}
20360Sstevel@tonic-gate 		break;
20370Sstevel@tonic-gate 	default:
20380Sstevel@tonic-gate 		(void) elf_errno();
20390Sstevel@tonic-gate 		_rej.rej_type = SGS_REJ_UNKFILE;
20400Sstevel@tonic-gate 		_rej.rej_name = name;
20411618Srie 		DBG_CALL(Dbg_file_rejected(ofl->ofl_lml, &_rej));
20420Sstevel@tonic-gate 		if (rej->rej_type == 0) {
20430Sstevel@tonic-gate 			*rej = _rej;
20440Sstevel@tonic-gate 			rej->rej_name = strdup(_rej.rej_name);
20450Sstevel@tonic-gate 		}
20460Sstevel@tonic-gate 		return (0);
20470Sstevel@tonic-gate 	}
20480Sstevel@tonic-gate 	if ((error == 0) || (error == S_ERROR))
20490Sstevel@tonic-gate 		return ((Ifl_desc *)error);
20500Sstevel@tonic-gate 	else
20510Sstevel@tonic-gate 		return (ifl);
20520Sstevel@tonic-gate }
20530Sstevel@tonic-gate 
20540Sstevel@tonic-gate /*
20550Sstevel@tonic-gate  * Having successfully opened a file, set up the necessary elf structures to
20560Sstevel@tonic-gate  * process it further.  This small section of processing is slightly different
20570Sstevel@tonic-gate  * from the elf initialization required to process a relocatable object from an
20582850Srie  * archive (see libs.c: ld_process_archive()).
20590Sstevel@tonic-gate  */
20600Sstevel@tonic-gate Ifl_desc *
2061*2978Srie ld_process_open(const char *opath, const char *ofile, int *fd, Ofl_desc *ofl,
20622850Srie     Half flags, Rej_desc *rej)
20630Sstevel@tonic-gate {
2064*2978Srie 	Elf		*elf;
2065*2978Srie 	const char	*npath = opath;
2066*2978Srie 	const char	*nfile = ofile;
20670Sstevel@tonic-gate 
2068*2978Srie 	if ((elf = elf_begin(*fd, ELF_C_READ, NULL)) == NULL) {
2069*2978Srie 		eprintf(ofl->ofl_lml, ERR_ELF, MSG_INTL(MSG_ELF_BEGIN), npath);
20700Sstevel@tonic-gate 		ofl->ofl_flags |= FLG_OF_FATAL;
20710Sstevel@tonic-gate 		return (0);
20720Sstevel@tonic-gate 	}
20730Sstevel@tonic-gate 
2074*2978Srie 	/*
2075*2978Srie 	 * Determine whether the support library wishes to process this open.
2076*2978Srie 	 * The support library may return:
2077*2978Srie 	 *   .	a different ELF descriptor (in which case they should have
2078*2978Srie 	 *	closed the original)
2079*2978Srie 	 *   .	a different file descriptor (in which case they should have
2080*2978Srie 	 *	closed the original)
2081*2978Srie 	 *   .	a different path and file name (presumably associated with
2082*2978Srie 	 *	a different file descriptor)
2083*2978Srie 	 *
2084*2978Srie 	 * A file descriptor of -1, or and ELF descriptor of zero indicates
2085*2978Srie 	 * the file should be ignored.
2086*2978Srie 	 */
2087*2978Srie 	ld_sup_open(ofl, &npath, &nfile, fd, flags, &elf, NULL, 0,
2088*2978Srie 	    elf_kind(elf));
2089*2978Srie 
2090*2978Srie 	if ((*fd == -1) || (elf == NULL))
2091*2978Srie 		return (0);
2092*2978Srie 
2093*2978Srie 	return (ld_process_ifl(npath, nfile, *fd, elf, flags, ofl, rej));
20940Sstevel@tonic-gate }
20950Sstevel@tonic-gate 
20960Sstevel@tonic-gate /*
20970Sstevel@tonic-gate  * Process a required library (i.e. the dependency of a shared object).
20980Sstevel@tonic-gate  * Combine the directory and filename, check the resultant path size, and try
20990Sstevel@tonic-gate  * opening the pathname.
21000Sstevel@tonic-gate  */
21011618Srie static Ifl_desc *
21020Sstevel@tonic-gate process_req_lib(Sdf_desc *sdf, const char *dir, const char *file,
21030Sstevel@tonic-gate     Ofl_desc * ofl, Rej_desc * rej)
21040Sstevel@tonic-gate {
21050Sstevel@tonic-gate 	size_t		dlen, plen;
21060Sstevel@tonic-gate 	int		fd;
21070Sstevel@tonic-gate 	char		path[PATH_MAX];
21080Sstevel@tonic-gate 	const char	*_dir = dir;
21090Sstevel@tonic-gate 
21100Sstevel@tonic-gate 	/*
21110Sstevel@tonic-gate 	 * Determine the sizes of the directory and filename to insure we don't
21120Sstevel@tonic-gate 	 * exceed our buffer.
21130Sstevel@tonic-gate 	 */
21140Sstevel@tonic-gate 	if ((dlen = strlen(dir)) == 0) {
21150Sstevel@tonic-gate 		_dir = MSG_ORIG(MSG_STR_DOT);
21160Sstevel@tonic-gate 		dlen = 1;
21170Sstevel@tonic-gate 	}
21180Sstevel@tonic-gate 	dlen++;
21190Sstevel@tonic-gate 	plen = dlen + strlen(file) + 1;
21200Sstevel@tonic-gate 	if (plen > PATH_MAX) {
21211618Srie 		eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_FIL_PTHTOLONG),
21221618Srie 		    _dir, file);
21230Sstevel@tonic-gate 		ofl->ofl_flags |= FLG_OF_FATAL;
21240Sstevel@tonic-gate 		return (0);
21250Sstevel@tonic-gate 	}
21260Sstevel@tonic-gate 
21270Sstevel@tonic-gate 	/*
21280Sstevel@tonic-gate 	 * Build the entire pathname and try and open the file.
21290Sstevel@tonic-gate 	 */
21300Sstevel@tonic-gate 	(void) strcpy(path, _dir);
21310Sstevel@tonic-gate 	(void) strcat(path, MSG_ORIG(MSG_STR_SLASH));
21320Sstevel@tonic-gate 	(void) strcat(path, file);
21331618Srie 	DBG_CALL(Dbg_libs_req(ofl->ofl_lml, sdf->sdf_name,
21341618Srie 	    sdf->sdf_rfile, path));
21350Sstevel@tonic-gate 
21360Sstevel@tonic-gate 	if ((fd = open(path, O_RDONLY)) == -1)
21370Sstevel@tonic-gate 		return (0);
21380Sstevel@tonic-gate 	else {
21390Sstevel@tonic-gate 		Ifl_desc	*ifl;
21400Sstevel@tonic-gate 		char		*_path;
21410Sstevel@tonic-gate 
21420Sstevel@tonic-gate 		if ((_path = libld_malloc(strlen(path) + 1)) == 0)
21430Sstevel@tonic-gate 			return ((Ifl_desc *)S_ERROR);
21440Sstevel@tonic-gate 		(void) strcpy(_path, path);
2145*2978Srie 		ifl = ld_process_open(_path, &_path[dlen], &fd, ofl, NULL, rej);
2146*2978Srie 		if (fd != -1)
2147*2978Srie 			(void) close(fd);
21480Sstevel@tonic-gate 		return (ifl);
21490Sstevel@tonic-gate 	}
21500Sstevel@tonic-gate }
21510Sstevel@tonic-gate 
21520Sstevel@tonic-gate /*
21530Sstevel@tonic-gate  * Finish any library processing.  Walk the list of so's that have been listed
21540Sstevel@tonic-gate  * as "included" by shared objects we have previously processed.  Examine them,
21550Sstevel@tonic-gate  * without adding them as explicit dependents of this program, in order to
21560Sstevel@tonic-gate  * complete our symbol definition process.  The search path rules are:
21570Sstevel@tonic-gate  *
21580Sstevel@tonic-gate  *  o	use any user supplied paths, i.e. LD_LIBRARY_PATH and -L, then
21590Sstevel@tonic-gate  *
21600Sstevel@tonic-gate  *  o	use any RPATH defined within the parent shared object, then
21610Sstevel@tonic-gate  *
21620Sstevel@tonic-gate  *  o	use the default directories, i.e. LIBPATH or -YP.
21630Sstevel@tonic-gate  */
21640Sstevel@tonic-gate uintptr_t
21651618Srie ld_finish_libs(Ofl_desc *ofl)
21660Sstevel@tonic-gate {
21670Sstevel@tonic-gate 	Listnode	*lnp1;
21680Sstevel@tonic-gate 	Sdf_desc	*sdf;
21690Sstevel@tonic-gate 	Rej_desc	rej = { 0 };
21700Sstevel@tonic-gate 
21710Sstevel@tonic-gate 	/*
21720Sstevel@tonic-gate 	 * Make sure we are back in dynamic mode.
21730Sstevel@tonic-gate 	 */
21740Sstevel@tonic-gate 	ofl->ofl_flags |= FLG_OF_DYNLIBS;
21750Sstevel@tonic-gate 
21760Sstevel@tonic-gate 	for (LIST_TRAVERSE(&ofl->ofl_soneed, lnp1, sdf)) {
21770Sstevel@tonic-gate 		Listnode	*lnp2;
21782850Srie 		char		*path, *slash = NULL;
21790Sstevel@tonic-gate 		int		fd;
21800Sstevel@tonic-gate 		Ifl_desc	*ifl;
21812850Srie 		char		*file = (char *)sdf->sdf_name;
21820Sstevel@tonic-gate 
21830Sstevel@tonic-gate 		/*
21840Sstevel@tonic-gate 		 * See if this file has already been processed.  At the time
21850Sstevel@tonic-gate 		 * this implicit dependency was determined there may still have
21861109Srie 		 * been more explicit dependencies to process.  Note, if we ever
21870Sstevel@tonic-gate 		 * do parse the command line three times we would be able to
21881109Srie 		 * do all this checking when processing the dynamic section.
21890Sstevel@tonic-gate 		 */
21900Sstevel@tonic-gate 		if (sdf->sdf_file)
21910Sstevel@tonic-gate 			continue;
21920Sstevel@tonic-gate 
21930Sstevel@tonic-gate 		for (LIST_TRAVERSE(&ofl->ofl_sos, lnp2, ifl)) {
21940Sstevel@tonic-gate 			if (!(ifl->ifl_flags & FLG_IF_NEEDSTR) &&
21950Sstevel@tonic-gate 			    (strcmp(file, ifl->ifl_soname) == 0)) {
21960Sstevel@tonic-gate 				sdf->sdf_file = ifl;
21970Sstevel@tonic-gate 				break;
21980Sstevel@tonic-gate 			}
21990Sstevel@tonic-gate 		}
22000Sstevel@tonic-gate 		if (sdf->sdf_file)
22010Sstevel@tonic-gate 			continue;
22020Sstevel@tonic-gate 
22030Sstevel@tonic-gate 		/*
22042850Srie 		 * If the current path name element embeds a "/", then it's to
22052850Srie 		 * be taken "as is", with no searching involved.  Process all
22062850Srie 		 * "/" occurrences, so that we can deduce the base file name.
22070Sstevel@tonic-gate 		 */
22082850Srie 		for (path = file; *path; path++) {
22090Sstevel@tonic-gate 			if (*path == '/')
22102850Srie 				slash = path;
22112850Srie 		}
22122850Srie 		if (slash) {
22131618Srie 			DBG_CALL(Dbg_libs_req(ofl->ofl_lml, sdf->sdf_name,
22141618Srie 			    sdf->sdf_rfile, file));
22150Sstevel@tonic-gate 			if ((fd = open(file, O_RDONLY)) == -1) {
22161618Srie 				eprintf(ofl->ofl_lml, ERR_WARNING,
22171618Srie 				    MSG_INTL(MSG_FIL_NOTFOUND), file,
22181618Srie 				    sdf->sdf_rfile);
22190Sstevel@tonic-gate 			} else {
22200Sstevel@tonic-gate 				Rej_desc	_rej = { 0 };
22210Sstevel@tonic-gate 
2222*2978Srie 				ifl = ld_process_open(file, ++slash, &fd, ofl,
22232850Srie 				    NULL, &_rej);
2224*2978Srie 				if (fd != -1)
2225*2978Srie 					(void) close(fd);
22260Sstevel@tonic-gate 
22270Sstevel@tonic-gate 				if (ifl == (Ifl_desc *)S_ERROR) {
22280Sstevel@tonic-gate 					return (S_ERROR);
22290Sstevel@tonic-gate 				}
22300Sstevel@tonic-gate 				if (_rej.rej_type) {
22311618Srie 					eprintf(ofl->ofl_lml, ERR_WARNING,
22320Sstevel@tonic-gate 					    MSG_INTL(reject[_rej.rej_type]),
22330Sstevel@tonic-gate 					    _rej.rej_name ? rej.rej_name :
22340Sstevel@tonic-gate 					    MSG_INTL(MSG_STR_UNKNOWN),
22351618Srie 					    conv_reject_desc(&_rej));
22360Sstevel@tonic-gate 				} else
22370Sstevel@tonic-gate 					sdf->sdf_file = ifl;
22380Sstevel@tonic-gate 			}
22390Sstevel@tonic-gate 			continue;
22400Sstevel@tonic-gate 		}
22410Sstevel@tonic-gate 
22420Sstevel@tonic-gate 		/*
22430Sstevel@tonic-gate 		 * Now search for this file in any user defined directories.
22440Sstevel@tonic-gate 		 */
22450Sstevel@tonic-gate 		for (LIST_TRAVERSE(&ofl->ofl_ulibdirs, lnp2, path)) {
22460Sstevel@tonic-gate 			Rej_desc	_rej = { 0 };
22470Sstevel@tonic-gate 
22480Sstevel@tonic-gate 			ifl = process_req_lib(sdf, path, file, ofl, &_rej);
22490Sstevel@tonic-gate 			if (ifl == (Ifl_desc *)S_ERROR) {
22500Sstevel@tonic-gate 				return (S_ERROR);
22510Sstevel@tonic-gate 			}
22520Sstevel@tonic-gate 			if (_rej.rej_type) {
22530Sstevel@tonic-gate 				if (rej.rej_type == 0) {
22540Sstevel@tonic-gate 					rej = _rej;
22550Sstevel@tonic-gate 					rej.rej_name = strdup(_rej.rej_name);
22560Sstevel@tonic-gate 				}
22570Sstevel@tonic-gate 			}
22580Sstevel@tonic-gate 			if (ifl) {
22590Sstevel@tonic-gate 				sdf->sdf_file = ifl;
22600Sstevel@tonic-gate 				break;
22610Sstevel@tonic-gate 			}
22620Sstevel@tonic-gate 		}
22630Sstevel@tonic-gate 		if (sdf->sdf_file)
22640Sstevel@tonic-gate 			continue;
22650Sstevel@tonic-gate 
22660Sstevel@tonic-gate 		/*
22670Sstevel@tonic-gate 		 * Next use the local rules defined within the parent shared
22680Sstevel@tonic-gate 		 * object.
22690Sstevel@tonic-gate 		 */
22700Sstevel@tonic-gate 		if (sdf->sdf_rpath != NULL) {
22710Sstevel@tonic-gate 			char	*rpath, *next;
22720Sstevel@tonic-gate 
22730Sstevel@tonic-gate 			rpath = libld_malloc(strlen(sdf->sdf_rpath) + 1);
22740Sstevel@tonic-gate 			if (rpath == 0)
22750Sstevel@tonic-gate 				return (S_ERROR);
22760Sstevel@tonic-gate 			(void) strcpy(rpath, sdf->sdf_rpath);
22771618Srie 			DBG_CALL(Dbg_libs_path(ofl->ofl_lml, rpath,
22781618Srie 			    LA_SER_RUNPATH, sdf->sdf_rfile));
22790Sstevel@tonic-gate 			if ((path = strtok_r(rpath,
22800Sstevel@tonic-gate 			    MSG_ORIG(MSG_STR_COLON), &next)) != NULL) {
22810Sstevel@tonic-gate 				do {
22820Sstevel@tonic-gate 					Rej_desc	_rej = { 0 };
22830Sstevel@tonic-gate 
22840Sstevel@tonic-gate 					path = expand(sdf->sdf_rfile, path,
22850Sstevel@tonic-gate 					    &next);
22860Sstevel@tonic-gate 
22870Sstevel@tonic-gate 					ifl = process_req_lib(sdf, path,
22880Sstevel@tonic-gate 						file, ofl, &_rej);
22890Sstevel@tonic-gate 					if (ifl == (Ifl_desc *)S_ERROR) {
22900Sstevel@tonic-gate 						return (S_ERROR);
22910Sstevel@tonic-gate 					}
22920Sstevel@tonic-gate 					if (_rej.rej_type) {
22930Sstevel@tonic-gate 						if (rej.rej_type == 0) {
22940Sstevel@tonic-gate 						    rej = _rej;
22950Sstevel@tonic-gate 						    rej.rej_name =
22960Sstevel@tonic-gate 							strdup(_rej.rej_name);
22970Sstevel@tonic-gate 						}
22980Sstevel@tonic-gate 					}
22990Sstevel@tonic-gate 					if (ifl) {
23000Sstevel@tonic-gate 						sdf->sdf_file = ifl;
23010Sstevel@tonic-gate 						break;
23020Sstevel@tonic-gate 					}
23030Sstevel@tonic-gate 				} while ((path = strtok_r(NULL,
23040Sstevel@tonic-gate 				    MSG_ORIG(MSG_STR_COLON), &next)) != NULL);
23050Sstevel@tonic-gate 			}
23060Sstevel@tonic-gate 		}
23070Sstevel@tonic-gate 		if (sdf->sdf_file)
23080Sstevel@tonic-gate 			continue;
23090Sstevel@tonic-gate 
23100Sstevel@tonic-gate 		/*
23110Sstevel@tonic-gate 		 * Finally try the default library search directories.
23120Sstevel@tonic-gate 		 */
23130Sstevel@tonic-gate 		for (LIST_TRAVERSE(&ofl->ofl_dlibdirs, lnp2, path)) {
23140Sstevel@tonic-gate 			Rej_desc	_rej = { 0 };
23150Sstevel@tonic-gate 
23160Sstevel@tonic-gate 			ifl = process_req_lib(sdf, path, file, ofl, &rej);
23170Sstevel@tonic-gate 			if (ifl == (Ifl_desc *)S_ERROR) {
23180Sstevel@tonic-gate 				return (S_ERROR);
23190Sstevel@tonic-gate 			}
23200Sstevel@tonic-gate 			if (_rej.rej_type) {
23210Sstevel@tonic-gate 				if (rej.rej_type == 0) {
23220Sstevel@tonic-gate 					rej = _rej;
23230Sstevel@tonic-gate 					rej.rej_name = strdup(_rej.rej_name);
23240Sstevel@tonic-gate 				}
23250Sstevel@tonic-gate 			}
23260Sstevel@tonic-gate 			if (ifl) {
23270Sstevel@tonic-gate 				sdf->sdf_file = ifl;
23280Sstevel@tonic-gate 				break;
23290Sstevel@tonic-gate 			}
23300Sstevel@tonic-gate 		}
23310Sstevel@tonic-gate 		if (sdf->sdf_file)
23320Sstevel@tonic-gate 			continue;
23330Sstevel@tonic-gate 
23340Sstevel@tonic-gate 		/*
23350Sstevel@tonic-gate 		 * If we've got this far we haven't found the shared object.
23360Sstevel@tonic-gate 		 * If an object was found, but was rejected for some reason,
23370Sstevel@tonic-gate 		 * print a diagnostic to that effect, otherwise generate a
23380Sstevel@tonic-gate 		 * generic "not found" diagnostic.
23390Sstevel@tonic-gate 		 */
23400Sstevel@tonic-gate 		if (rej.rej_type) {
23411618Srie 			eprintf(ofl->ofl_lml, ERR_WARNING,
23421618Srie 			    MSG_INTL(reject[rej.rej_type]),
23430Sstevel@tonic-gate 			    rej.rej_name ? rej.rej_name :
23441618Srie 			    MSG_INTL(MSG_STR_UNKNOWN), conv_reject_desc(&rej));
23450Sstevel@tonic-gate 		} else {
23461618Srie 			eprintf(ofl->ofl_lml, ERR_WARNING,
23471618Srie 			    MSG_INTL(MSG_FIL_NOTFOUND), file, sdf->sdf_rfile);
23480Sstevel@tonic-gate 		}
23490Sstevel@tonic-gate 	}
23500Sstevel@tonic-gate 
23510Sstevel@tonic-gate 	/*
23520Sstevel@tonic-gate 	 * Finally, now that all objects have been input, make sure any version
23530Sstevel@tonic-gate 	 * requirements have been met.
23540Sstevel@tonic-gate 	 */
23551618Srie 	return (ld_vers_verify(ofl));
23560Sstevel@tonic-gate }
2357