files.c revision 11827:d7ef53deac3f
1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21
22/*
23 *	Copyright (c) 1988 AT&T
24 *	  All Rights Reserved
25 *
26 * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
27 * Use is subject to license terms.
28 */
29
30/*
31 * Processing of relocatable objects and shared objects.
32 */
33
34#define	ELF_TARGET_AMD64
35#define	ELF_TARGET_SPARC
36
37#include	<stdio.h>
38#include	<string.h>
39#include	<fcntl.h>
40#include	<unistd.h>
41#include	<link.h>
42#include	<limits.h>
43#include	<sys/stat.h>
44#include	<sys/systeminfo.h>
45#include	<debug.h>
46#include	<msg.h>
47#include	<_libld.h>
48
49/*
50 * Decide if we can link against this input file.
51 */
52static int
53ifl_verify(Ehdr *ehdr, Ofl_desc *ofl, Rej_desc *rej)
54{
55	/*
56	 * Check the validity of the elf header information for compatibility
57	 * with this machine and our own internal elf library.
58	 */
59	if ((ehdr->e_machine != ld_targ.t_m.m_mach) &&
60	    ((ehdr->e_machine != ld_targ.t_m.m_machplus) &&
61	    ((ehdr->e_flags & ld_targ.t_m.m_flagsplus) == 0))) {
62		rej->rej_type = SGS_REJ_MACH;
63		rej->rej_info = (uint_t)ehdr->e_machine;
64		return (0);
65	}
66	if (ehdr->e_ident[EI_DATA] != ld_targ.t_m.m_data) {
67		rej->rej_type = SGS_REJ_DATA;
68		rej->rej_info = (uint_t)ehdr->e_ident[EI_DATA];
69		return (0);
70	}
71	if (ehdr->e_version > ofl->ofl_dehdr->e_version) {
72		rej->rej_type = SGS_REJ_VERSION;
73		rej->rej_info = (uint_t)ehdr->e_version;
74		return (0);
75	}
76	return (1);
77}
78
79/*
80 * Check sanity of file header and allocate an infile descriptor
81 * for the file being processed.
82 */
83static Ifl_desc *
84ifl_setup(const char *name, Ehdr *ehdr, Elf *elf, Word flags, Ofl_desc *ofl,
85    Rej_desc *rej)
86{
87	Ifl_desc	*ifl;
88	Rej_desc	_rej = { 0 };
89
90	if (ifl_verify(ehdr, ofl, &_rej) == 0) {
91		_rej.rej_name = name;
92		DBG_CALL(Dbg_file_rejected(ofl->ofl_lml, &_rej,
93		    ld_targ.t_m.m_mach));
94		if (rej->rej_type == 0) {
95			*rej = _rej;
96			rej->rej_name = strdup(_rej.rej_name);
97		}
98		return (0);
99	}
100
101	if ((ifl = libld_calloc(1, sizeof (Ifl_desc))) == NULL)
102		return ((Ifl_desc *)S_ERROR);
103	ifl->ifl_name = name;
104	ifl->ifl_ehdr = ehdr;
105	ifl->ifl_elf = elf;
106	ifl->ifl_flags = flags;
107
108	/*
109	 * Is this file using 'extended Section Indexes'.  If so, use the
110	 * e_shnum & e_shstrndx which can be found at:
111	 *
112	 *	e_shnum == Shdr[0].sh_size
113	 *	e_shstrndx == Shdr[0].sh_link
114	 */
115	if ((ehdr->e_shnum == 0) && (ehdr->e_shoff != 0)) {
116		Elf_Scn	*scn;
117		Shdr	*shdr0;
118
119		if ((scn = elf_getscn(elf, 0)) == NULL) {
120			eprintf(ofl->ofl_lml, ERR_ELF,
121			    MSG_INTL(MSG_ELF_GETSCN), name);
122			ofl->ofl_flags |= FLG_OF_FATAL;
123			return ((Ifl_desc *)S_ERROR);
124		}
125		if ((shdr0 = elf_getshdr(scn)) == NULL) {
126			eprintf(ofl->ofl_lml, ERR_ELF,
127			    MSG_INTL(MSG_ELF_GETSHDR), name);
128			ofl->ofl_flags |= FLG_OF_FATAL;
129			return ((Ifl_desc *)S_ERROR);
130		}
131		ifl->ifl_shnum = (Word)shdr0->sh_size;
132		if (ehdr->e_shstrndx == SHN_XINDEX)
133			ifl->ifl_shstrndx = shdr0->sh_link;
134		else
135			ifl->ifl_shstrndx = ehdr->e_shstrndx;
136	} else {
137		ifl->ifl_shnum = ehdr->e_shnum;
138		ifl->ifl_shstrndx = ehdr->e_shstrndx;
139	}
140
141	if ((ifl->ifl_isdesc = libld_calloc(ifl->ifl_shnum,
142	    sizeof (Is_desc *))) == NULL)
143		return ((Ifl_desc *)S_ERROR);
144
145	/*
146	 * Record this new input file on the shared object or relocatable
147	 * object input file list.
148	 */
149	if (ifl->ifl_ehdr->e_type == ET_DYN) {
150		if (aplist_append(&ofl->ofl_sos, ifl, AL_CNT_OFL_LIBS) == NULL)
151			return ((Ifl_desc *)S_ERROR);
152	} else {
153		if (aplist_append(&ofl->ofl_objs, ifl, AL_CNT_OFL_OBJS) == NULL)
154			return ((Ifl_desc *)S_ERROR);
155	}
156
157	return (ifl);
158}
159
160/*
161 * Process a generic section.  The appropriate section information is added
162 * to the files input descriptor list.
163 */
164static uintptr_t
165process_section(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
166    Word ndx, int ident, Ofl_desc *ofl)
167{
168	Is_desc	*isp;
169
170	/*
171	 * Create a new input section descriptor.  If this is a NOBITS
172	 * section elf_getdata() will still create a data buffer (the buffer
173	 * will be null and the size will reflect the actual memory size).
174	 */
175	if ((isp = libld_calloc(sizeof (Is_desc), 1)) == NULL)
176		return (S_ERROR);
177	isp->is_shdr = shdr;
178	isp->is_file = ifl;
179	isp->is_name = name;
180	isp->is_scnndx = ndx;
181	isp->is_flags = FLG_IS_EXTERNAL;
182	isp->is_keyident = ident;
183
184	if ((isp->is_indata = elf_getdata(scn, NULL)) == NULL) {
185		eprintf(ofl->ofl_lml, ERR_ELF, MSG_INTL(MSG_ELF_GETDATA),
186		    ifl->ifl_name);
187		ofl->ofl_flags |= FLG_OF_FATAL;
188		return (0);
189	}
190
191	if ((shdr->sh_flags & SHF_EXCLUDE) &&
192	    ((ofl->ofl_flags & FLG_OF_RELOBJ) == 0)) {
193		isp->is_flags |= FLG_IS_DISCARD;
194	}
195
196	/*
197	 * Add the new input section to the files input section list and
198	 * flag whether the section needs placing in an output section.  This
199	 * placement is deferred until all input section processing has been
200	 * completed, as SHT_GROUP sections can provide information that will
201	 * affect how other sections within the file should be placed.
202	 */
203	ifl->ifl_isdesc[ndx] = isp;
204
205	if (ident) {
206		if (shdr->sh_flags & ALL_SHF_ORDER) {
207			isp->is_flags |= FLG_IS_ORDERED;
208			ifl->ifl_flags |= FLG_IF_ORDERED;
209		}
210		isp->is_flags |= FLG_IS_PLACE;
211	}
212	return (1);
213}
214
215/*
216 * Determine the software capabilities of the object being built from the
217 * capabilities of the input relocatable objects.   One software capability
218 * is presently recognized, and represented with the following (sys/elf.h):
219 *
220 *   SF1_SUNW_FPKNWN	use/non-use of frame pointer is known, and
221 *   SF1_SUNW_FPUSED    the frame pointer is in use.
222 *
223 * The resolution of the present fame pointer state, and the capabilities
224 * provided by a new input relocatable object are:
225 *
226 *                              new input relocatable object
227 *
228 *      present      |  SF1_SUNW_FPKNWN  |  SF1_SUNW_FPKNWN  |    <unknown>
229 *       state       |  SF1_SUNW_FPUSED  |                   |
230 *  ---------------------------------------------------------------------------
231 *  SF1_SUNW_FPKNWN  |  SF1_SUNW_FPKNWN  |  SF1_SUNW_FPKNWN  |  SF1_SUNW_FPKNWN
232 *  SF1_SUNW_FPUSED  |  SF1_SUNW_FPUSED  |                   |  SF1_SUNW_FPUSED
233 *  ---------------------------------------------------------------------------
234 *  SF1_SUNW_FPKNWN  |  SF1_SUNW_FPKNWN  |  SF1_SUNW_FPKNWN  |  SF1_SUNW_FPKNWN
235 *                   |                   |                   |
236 *  ---------------------------------------------------------------------------
237 *     <unknown>     |  SF1_SUNW_FPKNWN  |  SF1_SUNW_FPKNWN  |    <unknown>
238 *                   |  SF1_SUNW_FPUSED  |                   |
239 */
240static void
241sf1_cap(Ofl_desc *ofl, Xword val, Ifl_desc *ifl, Is_desc *cisp)
242{
243#define	FP_FLAGS	(SF1_SUNW_FPKNWN | SF1_SUNW_FPUSED)
244
245	Xword	badval;
246
247	/*
248	 * If a mapfile has established definitions to override any object
249	 * capabilities, ignore any new object capabilities.
250	 */
251	if (ofl->ofl_flags1 & FLG_OF1_OVSFCAP1) {
252		DBG_CALL(Dbg_cap_val_entry(ofl->ofl_lml, DBG_STATE_IGNORED,
253		    CA_SUNW_SF_1, val, ld_targ.t_m.m_mach));
254		return;
255	}
256
257#if	!defined(_ELF64)
258	if (ifl && (ifl->ifl_ehdr->e_type == ET_REL)) {
259		/*
260		 * The SF1_SUNW_ADDR32 is only meaningful when building a 64-bit
261		 * object.  Warn the user, and remove the setting, if we're
262		 * building a 32-bit object.
263		 */
264		if (val & SF1_SUNW_ADDR32) {
265			eprintf(ofl->ofl_lml, ERR_WARNING,
266			    MSG_INTL(MSG_FIL_INADDR32SF1), ifl->ifl_name,
267			    EC_WORD(cisp->is_scnndx), cisp->is_name);
268			val &= ~SF1_SUNW_ADDR32;
269		}
270	}
271#endif
272	/*
273	 * If this object doesn't specify any capabilities, ignore it, and
274	 * leave the state as is.
275	 */
276	if (val == 0)
277		return;
278
279	/*
280	 * Make sure we only accept known software capabilities.  Note, that
281	 * an F1_SUNW_FPUSED by itself is viewed as bad practice.
282	 */
283	if ((badval = (val & ~SF1_SUNW_MASK)) != 0) {
284		eprintf(ofl->ofl_lml, ERR_WARNING, MSG_INTL(MSG_FIL_BADSF1),
285		    ifl->ifl_name, EC_WORD(cisp->is_scnndx), cisp->is_name,
286		    EC_XWORD(badval));
287		val &= SF1_SUNW_MASK;
288	}
289	if ((val & FP_FLAGS) == SF1_SUNW_FPUSED) {
290		eprintf(ofl->ofl_lml, ERR_WARNING, MSG_INTL(MSG_FIL_BADSF1),
291		    ifl->ifl_name, EC_WORD(cisp->is_scnndx), cisp->is_name,
292		    EC_XWORD(val));
293		return;
294	}
295
296	/*
297	 * If the input file is not a relocatable object, then we're only here
298	 * to warn the user of any questionable capabilities.
299	 */
300	if (ifl->ifl_ehdr->e_type != ET_REL) {
301#if	defined(_ELF64)
302		/*
303		 * If we're building a 64-bit executable, and we come across a
304		 * dependency that requires a restricted address space, then
305		 * that dependencies requirement can only be satisfied if the
306		 * executable triggers the restricted address space.  This is a
307		 * warning rather than a fatal error, as the possibility exists
308		 * that an appropriate dependency will be provided at runtime.
309		 * The runtime linker will refuse to use this dependency.
310		 */
311		if ((val & SF1_SUNW_ADDR32) && (ofl->ofl_flags & FLG_OF_EXEC) &&
312		    ((ofl->ofl_ocapset.oc_sf_1.cm_val &
313		    SF1_SUNW_ADDR32) == 0)) {
314			eprintf(ofl->ofl_lml, ERR_WARNING,
315			    MSG_INTL(MSG_FIL_EXADDR32SF1), ifl->ifl_name,
316			    EC_WORD(cisp->is_scnndx), cisp->is_name);
317		}
318#endif
319		return;
320	}
321
322	if (DBG_ENABLED) {
323		Dbg_cap_val_entry(ofl->ofl_lml, DBG_STATE_CURRENT, CA_SUNW_SF_1,
324		    ofl->ofl_ocapset.oc_sf_1.cm_val, ld_targ.t_m.m_mach);
325		Dbg_cap_val_entry(ofl->ofl_lml, DBG_STATE_NEW, CA_SUNW_SF_1,
326		    val, ld_targ.t_m.m_mach);
327	}
328
329	/*
330	 * Determine the resolution of the present frame pointer and the
331	 * new input relocatable objects frame pointer.
332	 */
333	if ((ofl->ofl_ocapset.oc_sf_1.cm_val & FP_FLAGS) == FP_FLAGS) {
334		/*
335		 * If the new relocatable object isn't using a frame pointer,
336		 * reduce the present state to unused.
337		 */
338		if ((val & FP_FLAGS) != FP_FLAGS)
339			ofl->ofl_ocapset.oc_sf_1.cm_val &= ~SF1_SUNW_FPUSED;
340
341		/*
342		 * Having processed the frame pointer bits, remove them from
343		 * the value so they don't get OR'd in below.
344		 */
345		val &= ~FP_FLAGS;
346
347	} else if ((ofl->ofl_ocapset.oc_sf_1.cm_val & SF1_SUNW_FPKNWN) == 0) {
348		/*
349		 * If the present frame pointer state is unknown, mask it out
350		 * and allow the values from the new relocatable object
351		 * to overwrite them.
352		 */
353		ofl->ofl_ocapset.oc_sf_1.cm_val &= ~FP_FLAGS;
354	} else {
355		/* Do not take the frame pointer flags from the object */
356		val &= ~FP_FLAGS;
357	}
358
359	ofl->ofl_ocapset.oc_sf_1.cm_val |= val;
360
361	DBG_CALL(Dbg_cap_val_entry(ofl->ofl_lml, DBG_STATE_RESOLVED,
362	    CA_SUNW_SF_1, ofl->ofl_ocapset.oc_sf_1.cm_val, ld_targ.t_m.m_mach));
363
364#undef FP_FLAGS
365}
366
367/*
368 * Determine the hardware capabilities of the object being built from the
369 * capabilities of the input relocatable objects.  There's really little to
370 * do here, other than to offer diagnostics, hardware capabilities are simply
371 * additive.
372 */
373static void
374hw_cap(Ofl_desc *ofl, Xword tag, Xword val)
375{
376	elfcap_mask_t	*hwcap;
377	ofl_flag_t	flags1;
378
379	if (tag == CA_SUNW_HW_1) {
380		hwcap = &ofl->ofl_ocapset.oc_hw_1.cm_val;
381		flags1 = FLG_OF1_OVHWCAP1;
382	} else {
383		hwcap = &ofl->ofl_ocapset.oc_hw_2.cm_val;
384		flags1 = FLG_OF1_OVHWCAP2;
385	}
386
387	/*
388	 * If a mapfile has established definitions to override any object
389	 * capabilities, ignore any new object capabilities.
390	 */
391	if (ofl->ofl_flags1 & flags1) {
392		DBG_CALL(Dbg_cap_val_entry(ofl->ofl_lml, DBG_STATE_IGNORED,
393		    tag, val, ld_targ.t_m.m_mach));
394		return;
395	}
396
397	/*
398	 * If this object doesn't specify any capabilities, ignore it, and
399	 * leave the state as is.
400	 */
401	if (val == 0)
402		return;
403
404	if (DBG_ENABLED) {
405		Dbg_cap_val_entry(ofl->ofl_lml, DBG_STATE_CURRENT, CA_SUNW_HW_1,
406		    ofl->ofl_ocapset.oc_hw_1.cm_val, ld_targ.t_m.m_mach);
407		Dbg_cap_val_entry(ofl->ofl_lml, DBG_STATE_NEW, CA_SUNW_HW_1,
408		    val, ld_targ.t_m.m_mach);
409	}
410
411	*hwcap |= val;
412
413	DBG_CALL(Dbg_cap_val_entry(ofl->ofl_lml, DBG_STATE_RESOLVED, tag,
414	    *hwcap, ld_targ.t_m.m_mach));
415}
416
417/*
418 * Promote a machine capability or platform capability to the output file.
419 * Multiple instances of these names can be defined.
420 */
421static void
422str_cap(Ofl_desc *ofl, char *pstr, ofl_flag_t flags, Xword tag, Caplist *list)
423{
424	Capstr		*capstr;
425	Aliste		idx;
426	Boolean		found = FALSE;
427
428	/*
429	 * If a mapfile has established definitions to override this capability,
430	 * ignore any new capability.
431	 */
432	if (ofl->ofl_flags1 & flags) {
433		DBG_CALL(Dbg_cap_ptr_entry(ofl->ofl_lml, DBG_STATE_IGNORED,
434		    tag, pstr));
435		return;
436	}
437
438	for (ALIST_TRAVERSE(list->cl_val, idx, capstr)) {
439		DBG_CALL(Dbg_cap_ptr_entry(ofl->ofl_lml,
440		    DBG_STATE_CURRENT, tag, capstr->cs_str));
441		if (strcmp(capstr->cs_str, pstr) == 0)
442			found = TRUE;
443	}
444
445	DBG_CALL(Dbg_cap_ptr_entry(ofl->ofl_lml, DBG_STATE_NEW, tag, pstr));
446
447	if (found == FALSE) {
448		if ((capstr = alist_append(&list->cl_val, NULL,
449		    sizeof (Capstr), AL_CNT_CAP_NAMES)) == NULL) {
450			ofl->ofl_flags |= FLG_OF_FATAL;
451			return;
452		}
453		capstr->cs_str = pstr;
454	}
455
456	if (DBG_ENABLED) {
457		for (ALIST_TRAVERSE(list->cl_val, idx, capstr)) {
458			DBG_CALL(Dbg_cap_ptr_entry(ofl->ofl_lml,
459			    DBG_STATE_RESOLVED, tag, capstr->cs_str));
460		}
461	}
462}
463
464/*
465 * Promote a capability identifier to the output file.  A capability group can
466 * only have one identifier, and thus only the first identifier seen from any
467 * input relocatable objects is retained.  An explicit user defined identifier,
468 * rather than an an identifier fabricated by ld(1) with -z symbcap processing,
469 * takes precedence.  Note, a user may have defined an identifier via a mapfile,
470 * in which case the mapfile identifier is retained.
471 */
472static void
473id_cap(Ofl_desc *ofl, char *pstr, oc_flag_t flags)
474{
475	Objcapset	*ocapset = &ofl->ofl_ocapset;
476
477	if (ocapset->oc_id.cs_str) {
478		DBG_CALL(Dbg_cap_ptr_entry(ofl->ofl_lml, DBG_STATE_CURRENT,
479		    CA_SUNW_ID, ocapset->oc_id.cs_str));
480
481		if ((ocapset->oc_flags & FLG_OCS_USRDEFID) ||
482		    ((flags & FLG_OCS_USRDEFID) == 0)) {
483			DBG_CALL(Dbg_cap_ptr_entry(ofl->ofl_lml,
484			    DBG_STATE_IGNORED, CA_SUNW_ID, pstr));
485			return;
486		}
487	}
488
489	DBG_CALL(Dbg_cap_ptr_entry(ofl->ofl_lml, DBG_STATE_NEW,
490	    CA_SUNW_ID, pstr));
491
492	ocapset->oc_id.cs_str = pstr;
493	ocapset->oc_flags |= flags;
494
495	DBG_CALL(Dbg_cap_ptr_entry(ofl->ofl_lml, DBG_STATE_RESOLVED,
496	    CA_SUNW_ID, pstr));
497}
498
499/*
500 * Promote a capabilities group to the object capabilities.  This catches a
501 * corner case.  An object capabilities file can be converted to symbol
502 * capabilities with -z symbolcap.  However, if the user has indicated that all
503 * the symbols should be demoted, we'd be left with a symbol capabilities file,
504 * with no associated symbols.  Catch this case by promoting the symbol
505 * capabilities back to object capabilities.
506 */
507void
508ld_cap_move_symtoobj(Ofl_desc *ofl)
509{
510	Cap_group	*cgp;
511	Aliste		idx1;
512
513	for (APLIST_TRAVERSE(ofl->ofl_capgroups, idx1, cgp)) {
514		Objcapset	*scapset = &cgp->cg_set;
515		Capstr		*capstr;
516		Aliste		idx2;
517
518		if (scapset->oc_id.cs_str) {
519			if (scapset->oc_flags & FLG_OCS_USRDEFID)
520				id_cap(ofl, scapset->oc_id.cs_str,
521				    scapset->oc_flags);
522		}
523		if (scapset->oc_plat.cl_val) {
524			for (ALIST_TRAVERSE(scapset->oc_plat.cl_val, idx2,
525			    capstr)) {
526				str_cap(ofl, capstr->cs_str, FLG_OF1_OVPLATCAP,
527				    CA_SUNW_PLAT, &ofl->ofl_ocapset.oc_plat);
528			}
529		}
530		if (scapset->oc_mach.cl_val) {
531			for (ALIST_TRAVERSE(scapset->oc_mach.cl_val, idx2,
532			    capstr)) {
533				str_cap(ofl, capstr->cs_str, FLG_OF1_OVMACHCAP,
534				    CA_SUNW_MACH, &ofl->ofl_ocapset.oc_mach);
535			}
536		}
537		if (scapset->oc_hw_2.cm_val)
538			hw_cap(ofl, CA_SUNW_HW_2, scapset->oc_hw_2.cm_val);
539
540		if (scapset->oc_hw_1.cm_val)
541			hw_cap(ofl, CA_SUNW_HW_1, scapset->oc_hw_1.cm_val);
542
543		if (scapset->oc_sf_1.cm_val)
544			sf1_cap(ofl, scapset->oc_sf_1.cm_val, NULL, NULL);
545	}
546}
547
548/*
549 * Determine whether a capabilities group already exists that describes this
550 * new capabilities group.
551 *
552 * Note, a capability group identifier, CA_SUNW_ID, isn't used as part of the
553 * comparison.  This attribute simply assigns a diagnostic name to the group,
554 * and in the case of multiple identifiers, the first will be taken.
555 */
556static Cap_group *
557get_cap_group(Objcapset *ocapset, Word cnum, Ofl_desc *ofl, Is_desc *isp)
558{
559	Aliste		idx;
560	Cap_group	*cgp;
561	Word		ccnum = cnum;
562
563	/*
564	 * If the new capabilities contains a CA_SUNW_ID, drop the count of the
565	 * number of comparable items.
566	 */
567	if (ocapset->oc_id.cs_str)
568		ccnum--;
569
570	/*
571	 * Traverse the existing symbols capabilities groups.
572	 */
573	for (APLIST_TRAVERSE(ofl->ofl_capgroups, idx, cgp)) {
574		Word	onum = cgp->cg_num;
575		Alist	*calp, *oalp;
576
577		if (cgp->cg_set.oc_id.cs_str)
578			onum--;
579
580		if (onum != ccnum)
581			continue;
582
583		if (cgp->cg_set.oc_hw_1.cm_val != ocapset->oc_hw_1.cm_val)
584			continue;
585		if (cgp->cg_set.oc_sf_1.cm_val != ocapset->oc_sf_1.cm_val)
586			continue;
587		if (cgp->cg_set.oc_hw_2.cm_val != ocapset->oc_hw_2.cm_val)
588			continue;
589
590		calp = cgp->cg_set.oc_plat.cl_val;
591		oalp = ocapset->oc_plat.cl_val;
592		if ((calp == NULL) && oalp)
593			continue;
594		if (calp && ((oalp == NULL) || cap_names_match(calp, oalp)))
595			continue;
596
597		calp = cgp->cg_set.oc_mach.cl_val;
598		oalp = ocapset->oc_mach.cl_val;
599		if ((calp == NULL) && oalp)
600			continue;
601		if (calp && ((oalp == NULL) || cap_names_match(calp, oalp)))
602			continue;
603
604		/*
605		 * If a matching group is found, then this new group has
606		 * already been supplied by a previous file, and hence the
607		 * existing group can be used.  Record this new input section,
608		 * from which we can also derive the input file name, on the
609		 * existing groups input sections.
610		 */
611		if (aplist_append(&(cgp->cg_secs), isp,
612		    AL_CNT_CAP_SECS) == NULL)
613			return (NULL);
614		return (cgp);
615	}
616
617	/*
618	 * If a capabilities group is not found, create a new one.
619	 */
620	if (((cgp = libld_calloc(sizeof (Cap_group), 1)) == NULL) ||
621	    (aplist_append(&(ofl->ofl_capgroups), cgp,
622	    AL_CNT_CAP_DESCS) == NULL))
623		return (NULL);
624
625	/*
626	 * If we're converting object capabilities to symbol capabilities and
627	 * no CA_SUNW_ID is defined, fabricate one.  This identifier is appended
628	 * to all symbol names that are converted into capabilities symbols,
629	 * see ld_sym_process().
630	 */
631	if ((isp->is_file->ifl_flags & FLG_IF_OTOSCAP) &&
632	    (ocapset->oc_id.cs_str == NULL)) {
633		size_t	len;
634
635		/*
636		 * Create an identifier using the group number together with a
637		 * default template.  We allocate a buffer large enough for any
638		 * possible number of items (way more than we need).
639		 */
640		len = MSG_STR_CAPGROUPID_SIZE + CONV_INV_BUFSIZE;
641		if ((ocapset->oc_id.cs_str = libld_malloc(len)) == NULL)
642			return (NULL);
643
644		(void) snprintf(ocapset->oc_id.cs_str, len,
645		    MSG_ORIG(MSG_STR_CAPGROUPID),
646		    aplist_nitems(ofl->ofl_capgroups));
647		cnum++;
648	}
649
650	cgp->cg_set = *ocapset;
651	cgp->cg_num = cnum;
652
653	/*
654	 * Null the callers alist's as they've effectively been transferred
655	 * to this new Cap_group.
656	 */
657	ocapset->oc_plat.cl_val = ocapset->oc_mach.cl_val = NULL;
658
659	/*
660	 * Keep track of which input section, and hence input file, established
661	 * this group.
662	 */
663	if (aplist_append(&(cgp->cg_secs), isp, AL_CNT_CAP_SECS) == NULL)
664		return (NULL);
665
666	/*
667	 * Keep track of the number of symbol capabilities entries that will be
668	 * required in the output file.  Each group requires a terminating
669	 * CA_SUNW_NULL.
670	 */
671	ofl->ofl_capsymcnt += (cnum + 1);
672	return (cgp);
673}
674
675/*
676 * Capture symbol capability family information.  This data structure is focal
677 * in maintaining all symbol capability relationships, and provides for the
678 * eventual creation of a capabilities information section, and possibly a
679 * capabilities chain section.
680 *
681 * Capabilities families are lead by a CAPINFO_SUNW_GLOB symbol.  This symbol
682 * provides the visible global symbol that is referenced by all external
683 * callers.  This symbol may have aliases.  For example, a weak/global symbol
684 * pair, such as memcpy()/_memcpy() may lead the same capabilities family.
685 * Each family contains one or more local symbol members.  These members provide
686 * the capabilities specific functions, and are associated to a capabilities
687 * group.  For example, the capability members memcpy%sun4u and memcpy%sun4v
688 * might be associated with the memcpy() capability family.
689 *
690 * This routine is called when a relocatable object that provides object
691 * capabilities is transformed into a symbol capabilities object, using the
692 * -z symbolcap option.
693 *
694 * This routine is also called to collect the SUNW_capinfo section information
695 * of a relocatable object that contains symbol capability definitions.
696 */
697uintptr_t
698ld_cap_add_family(Ofl_desc *ofl, Sym_desc *lsdp, Sym_desc *csdp, Cap_group *cgp,
699    APlist **csyms)
700{
701	Cap_avlnode	qcav, *cav;
702	avl_tree_t	*avlt;
703	avl_index_t	where = 0;
704	Cap_sym		*mcsp;
705	Aliste		idx;
706
707	/*
708	 * Make sure the capability families have an initialized AVL tree.
709	 */
710	if ((avlt = ofl->ofl_capfamilies) == NULL) {
711		if ((avlt = libld_calloc(sizeof (avl_tree_t), 1)) == NULL)
712			return (S_ERROR);
713		avl_create(avlt, &ld_sym_avl_comp, sizeof (Cap_avlnode),
714		    SGSOFFSETOF(Cap_avlnode, cn_symavlnode.sav_node));
715		ofl->ofl_capfamilies = avlt;
716
717		/*
718		 * When creating a dynamic object, capability family members
719		 * are maintained in a .SUNW_capchain, the first entry of
720		 * which is the version number of the chain.
721		 */
722		ofl->ofl_capchaincnt = 1;
723	}
724
725	/*
726	 * Determine whether a family already exists, and if not, create one
727	 * using the lead family symbol.
728	 */
729	qcav.cn_symavlnode.sav_hash = (Word)elf_hash(lsdp->sd_name);
730	qcav.cn_symavlnode.sav_name = lsdp->sd_name;
731
732	if ((cav = avl_find(avlt, &qcav, &where)) == NULL) {
733		if ((cav = libld_calloc(sizeof (Cap_avlnode), 1)) == NULL)
734			return (S_ERROR);
735		cav->cn_symavlnode.sav_hash = qcav.cn_symavlnode.sav_hash;
736		cav->cn_symavlnode.sav_name = qcav.cn_symavlnode.sav_name;
737		cav->cn_symavlnode.sav_sdp = lsdp;
738
739		avl_insert(avlt, cav, where);
740
741		/*
742		 * When creating a dynamic object, capability family members
743		 * are maintained in a .SUNW_capchain, each family starts with
744		 * this lead symbol, and is terminated with a 0 element.
745		 */
746		ofl->ofl_capchaincnt += 2;
747	}
748
749	/*
750	 * If no group information is provided then this request is to add a
751	 * lead capability symbol, or lead symbol alias.  If this is the lead
752	 * symbol there's nothing more to do.  Otherwise save the alias.
753	 */
754	if (cgp == NULL) {
755		if ((lsdp != csdp) && (aplist_append(&cav->cn_aliases, csdp,
756		    AL_CNT_CAP_ALIASES) == NULL))
757			return (S_ERROR);
758
759		return (0);
760	}
761
762	/*
763	 * Determine whether a member of the same group as this new member is
764	 * already defined within this family.  If so, we have a multiply
765	 * defined symbol.
766	 */
767	for (APLIST_TRAVERSE(cav->cn_members, idx, mcsp)) {
768		Sym_desc	*msdp;
769
770		if (cgp != mcsp->cs_group)
771			continue;
772
773		/*
774		 * Diagnose that a multiple symbol definition exists.
775		 */
776		msdp = mcsp->cs_sdp;
777
778		eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_CAP_MULDEF),
779		    demangle(lsdp->sd_name));
780		eprintf(ofl->ofl_lml, ERR_NONE, MSG_INTL(MSG_CAP_MULDEFSYMS),
781		    msdp->sd_file->ifl_name, msdp->sd_name,
782		    csdp->sd_file->ifl_name, csdp->sd_name);
783		ofl->ofl_flags |= FLG_OF_FATAL;
784	}
785
786	/*
787	 * Add this capabilities symbol member to the family.
788	 */
789	if (((mcsp = libld_malloc(sizeof (Cap_sym))) == NULL) ||
790	    (aplist_append(&cav->cn_members, mcsp, AL_CNT_CAP_MEMS) == NULL))
791		return (S_ERROR);
792
793	mcsp->cs_sdp = csdp;
794	mcsp->cs_group = cgp;
795
796	/*
797	 * When creating a dynamic object, capability family members are
798	 * maintained in a .SUNW_capchain.  Account for this family member.
799	 */
800	ofl->ofl_capchaincnt++;
801
802	/*
803	 * If this input file is undergoing object capabilities to symbol
804	 * capabilities conversion, then this member is a new local symbol
805	 * that has been generated from an original global symbol.  Keep track
806	 * of this symbol so that the output file symbol table can be populated
807	 * with these new symbol entries.
808	 */
809	if (csyms && (aplist_append(csyms, mcsp, AL_CNT_CAP_SYMS) == NULL))
810		return (S_ERROR);
811
812	return (0);
813}
814
815/*
816 * Process a SHT_SUNW_cap capabilities section.
817 */
818static uintptr_t
819process_cap(Ofl_desc *ofl, Ifl_desc *ifl, Is_desc *cisp)
820{
821	Objcapset	ocapset = { 0 };
822	Cap_desc	*cdp;
823	Cap		*data, *cdata;
824	char		*strs;
825	Word		ndx, cnum;
826	int		objcapndx, descapndx, symcapndx;
827	int		nulls, capstrs = 0;
828
829	/*
830	 * Determine the capabilities data and size.
831	 */
832	cdata = (Cap *)cisp->is_indata->d_buf;
833	cnum = (Word)(cisp->is_shdr->sh_size / cisp->is_shdr->sh_entsize);
834
835	if ((cdata == NULL) || (cnum == 0))
836		return (0);
837
838	DBG_CALL(Dbg_cap_sec_title(ofl->ofl_lml, ifl->ifl_name));
839
840	/*
841	 * Traverse the section to determine what capabilities groups are
842	 * available.
843	 *
844	 * A capabilities section can contain one or more, CA_SUNW_NULL
845	 * terminated groups.
846	 *
847	 *  -	The first group defines the object capabilities.
848	 *  -	Additional groups define symbol capabilities.
849	 *  -	Since the initial group is always reserved for object
850	 *	capabilities, any object with symbol capabilities must also
851	 *	have an object capabilities group.  If the object has no object
852	 *	capabilities, an empty object group is defined, consisting of a
853	 *	CA_SUNW_NULL element in index [0].
854	 *  -	If any capabilities require references to a named string, then
855	 *	the section header sh_info points to the associated string
856	 *	table.
857	 *  -	If an object contains symbol capability groups, then the
858	 *	section header sh_link points to the associated capinfo table.
859	 */
860	objcapndx = 0;
861	descapndx = symcapndx = -1;
862	nulls = 0;
863
864	for (ndx = 0, data = cdata; ndx < cnum; ndx++, data++) {
865		switch (data->c_tag) {
866		case CA_SUNW_NULL:
867			/*
868			 * If this is the first CA_SUNW_NULL entry, and no
869			 * capabilities group has been found, then this object
870			 * does not define any object capabilities.
871			 */
872			if (nulls++ == 0) {
873				if (ndx == 0)
874					objcapndx = -1;
875			} else if ((symcapndx == -1) && (descapndx != -1))
876				symcapndx = descapndx;
877
878			break;
879
880		case CA_SUNW_PLAT:
881		case CA_SUNW_MACH:
882		case CA_SUNW_ID:
883			capstrs++;
884			/* FALLTHROUGH */
885
886		case CA_SUNW_HW_1:
887		case CA_SUNW_SF_1:
888		case CA_SUNW_HW_2:
889			/*
890			 * If this is the start of a new group, save it.
891			 */
892			if (descapndx == -1)
893				descapndx = ndx;
894			break;
895
896		default:
897			eprintf(ofl->ofl_lml, ERR_WARNING,
898			    MSG_INTL(MSG_FIL_UNKCAP), ifl->ifl_name,
899			    EC_WORD(cisp->is_scnndx), cisp->is_name,
900			    data->c_tag);
901		}
902	}
903
904	/*
905	 * If a string capabilities entry has been found, the capabilities
906	 * section must reference the associated string table.
907	 */
908	if (capstrs) {
909		Word	info = cisp->is_shdr->sh_info;
910
911		if ((info == 0) || (info > ifl->ifl_shnum)) {
912			eprintf(ofl->ofl_lml, ERR_FATAL,
913			    MSG_INTL(MSG_FIL_INVSHINFO), ifl->ifl_name,
914			    EC_WORD(cisp->is_scnndx), cisp->is_name,
915			    EC_XWORD(info));
916			ofl->ofl_flags |= FLG_OF_FATAL;
917			return (S_ERROR);
918		}
919		strs = (char *)ifl->ifl_isdesc[info]->is_indata->d_buf;
920	}
921
922	/*
923	 * The processing of capabilities groups is as follows:
924	 *
925	 *  -	if a relocatable object provides only object capabilities, and
926	 *	the -z symbolcap option is in effect, then the object
927	 *	capabilities are transformed into symbol capabilities and the
928	 *	symbol capabilities are carried over to the output file.
929	 *  -	in all other cases, any capabilities present in an input
930	 *	relocatable object are carried from the input object to the
931	 *	output without any transformation or conversion.
932	 *
933	 * Capture any object capabilities that are to be carried over to the
934	 * output file.
935	 */
936	if ((objcapndx == 0) &&
937	    ((symcapndx != -1) || ((ofl->ofl_flags & FLG_OF_OTOSCAP) == 0))) {
938		for (ndx = 0, data = cdata; ndx < cnum; ndx++, data++) {
939			/*
940			 * Object capabilities end at the first null.
941			 */
942			if (data->c_tag == CA_SUNW_NULL)
943				break;
944
945			/*
946			 * Only the object software capabilities that are
947			 * defined in a relocatable object become part of the
948			 * object software capabilities in the output file.
949			 * However, check the validity of any object software
950			 * capabilities of any dependencies.
951			 */
952			if (data->c_tag == CA_SUNW_SF_1) {
953				sf1_cap(ofl, data->c_un.c_val, ifl, cisp);
954				continue;
955			}
956
957			/*
958			 * The remaining capability types must come from a
959			 * relocatable object in order to contribute to the
960			 * output.
961			 */
962			if (ifl->ifl_ehdr->e_type != ET_REL)
963				continue;
964
965			switch (data->c_tag) {
966			case CA_SUNW_HW_1:
967			case CA_SUNW_HW_2:
968				hw_cap(ofl, data->c_tag, data->c_un.c_val);
969				break;
970
971			case CA_SUNW_PLAT:
972				str_cap(ofl, strs + data->c_un.c_ptr,
973				    FLG_OF1_OVPLATCAP, CA_SUNW_PLAT,
974				    &ofl->ofl_ocapset.oc_plat);
975				break;
976
977			case CA_SUNW_MACH:
978				str_cap(ofl, strs + data->c_un.c_ptr,
979				    FLG_OF1_OVMACHCAP, CA_SUNW_MACH,
980				    &ofl->ofl_ocapset.oc_mach);
981				break;
982
983			case CA_SUNW_ID:
984				id_cap(ofl, strs + data->c_un.c_ptr,
985				    FLG_OCS_USRDEFID);
986				break;
987
988			default:
989				assert(0);	/* Unknown capability type */
990			}
991		}
992
993		/*
994		 * If there are no symbol capabilities, or this objects
995		 * capabilities aren't being transformed into a symbol
996		 * capabilities, then we're done.
997		 */
998		if ((symcapndx == -1) &&
999		    ((ofl->ofl_flags & FLG_OF_OTOSCAP) == 0))
1000			return (1);
1001	}
1002
1003	/*
1004	 * If these capabilities don't originate from a relocatable object
1005	 * there's no further processing required.
1006	 */
1007	if (ifl->ifl_ehdr->e_type != ET_REL)
1008		return (1);
1009
1010	/*
1011	 * If this object only defines an object capabilities group, and the
1012	 * -z symbolcap option is in effect, then all global function symbols
1013	 * and initialized global data symbols are renamed and assigned to the
1014	 * transformed symbol capabilities group.
1015	 */
1016	if ((objcapndx == 0) &&
1017	    (symcapndx == -1) && (ofl->ofl_flags & FLG_OF_OTOSCAP))
1018		ifl->ifl_flags |= FLG_IF_OTOSCAP;
1019
1020	/*
1021	 * Allocate a capabilities descriptor to collect the capabilities data
1022	 * for this input file.  Allocate a mirror of the raw capabilities data
1023	 * that points to the individual symbol capabilities groups.  An APlist
1024	 * is used, although it will be sparsely populated, as the list provides
1025	 * a convenient mechanism for traversal later.
1026	 */
1027	if (((cdp = libld_calloc(sizeof (Cap_desc), 1)) == NULL) ||
1028	    (aplist_append(&(cdp->ca_groups), NULL, cnum) == NULL))
1029		return (S_ERROR);
1030
1031	/*
1032	 * Clear the allocated APlist data array, and assign the number of
1033	 * items as the total number of array items.
1034	 */
1035	(void) memset(&cdp->ca_groups->apl_data[0], 0,
1036	    (cnum * sizeof (void *)));
1037	cdp->ca_groups->apl_nitems = cnum;
1038
1039	ifl->ifl_caps = cdp;
1040
1041	/*
1042	 * Traverse the capabilities data, unpacking the data into a
1043	 * capabilities set.  Process each capabilities set as a unique group.
1044	 */
1045	descapndx = -1;
1046	nulls = 0;
1047
1048	for (ndx = 0, data = cdata; ndx < cnum; ndx++, data++) {
1049		Capstr	*capstr;
1050
1051		switch (data->c_tag) {
1052		case CA_SUNW_NULL:
1053			nulls++;
1054
1055			/*
1056			 * Process the capabilities group that this null entry
1057			 * terminates.  The capabilities group that is returned
1058			 * will either point to this file's data, or to a
1059			 * matching capabilities group that has already been
1060			 * processed.
1061			 *
1062			 * Note, if this object defines object capabilities,
1063			 * the first group descriptor points to these object
1064			 * capabilities.  It is only necessary to save this
1065			 * descriptor when object capabilities are being
1066			 * transformed into symbol capabilities (-z symbolcap).
1067			 */
1068			if (descapndx != -1) {
1069				if ((nulls > 1) ||
1070				    (ifl->ifl_flags & FLG_IF_OTOSCAP)) {
1071					APlist	*alp = cdp->ca_groups;
1072
1073					if ((alp->apl_data[descapndx] =
1074					    get_cap_group(&ocapset,
1075					    (ndx - descapndx), ofl,
1076					    cisp)) == NULL)
1077						return (S_ERROR);
1078				}
1079
1080				/*
1081				 * Clean up the capabilities data in preparation
1082				 * for processing additional groups.  If the
1083				 * collected capabilities strings were used to
1084				 * establish a new output group, they will have
1085				 * been saved in get_cap_group().  If these
1086				 * descriptors still exist, then an existing
1087				 * descriptor has been used to associate with
1088				 * this file, and these string descriptors can
1089				 * be freed.
1090				 */
1091				ocapset.oc_hw_1.cm_val =
1092				    ocapset.oc_sf_1.cm_val =
1093				    ocapset.oc_hw_2.cm_val = 0;
1094				if (ocapset.oc_plat.cl_val) {
1095					free((void *)ocapset.oc_plat.cl_val);
1096					ocapset.oc_plat.cl_val = NULL;
1097				}
1098				if (ocapset.oc_mach.cl_val) {
1099					free((void *)ocapset.oc_mach.cl_val);
1100					ocapset.oc_mach.cl_val = NULL;
1101				}
1102				descapndx = -1;
1103			}
1104			continue;
1105
1106		case CA_SUNW_HW_1:
1107			ocapset.oc_hw_1.cm_val = data->c_un.c_val;
1108			DBG_CALL(Dbg_cap_val_entry(ofl->ofl_lml,
1109			    DBG_STATE_ORIGINAL, CA_SUNW_HW_1,
1110			    ocapset.oc_hw_1.cm_val, ld_targ.t_m.m_mach));
1111			break;
1112
1113		case CA_SUNW_SF_1:
1114			ocapset.oc_sf_1.cm_val = data->c_un.c_val;
1115			DBG_CALL(Dbg_cap_val_entry(ofl->ofl_lml,
1116			    DBG_STATE_ORIGINAL, CA_SUNW_SF_1,
1117			    ocapset.oc_sf_1.cm_val, ld_targ.t_m.m_mach));
1118			break;
1119
1120		case CA_SUNW_HW_2:
1121			ocapset.oc_hw_2.cm_val = data->c_un.c_val;
1122			DBG_CALL(Dbg_cap_val_entry(ofl->ofl_lml,
1123			    DBG_STATE_ORIGINAL, CA_SUNW_HW_2,
1124			    ocapset.oc_hw_2.cm_val, ld_targ.t_m.m_mach));
1125			break;
1126
1127		case CA_SUNW_PLAT:
1128			if ((capstr = alist_append(&ocapset.oc_plat.cl_val,
1129			    NULL, sizeof (Capstr), AL_CNT_CAP_NAMES)) == NULL)
1130				return (S_ERROR);
1131			capstr->cs_str = strs + data->c_un.c_ptr;
1132			DBG_CALL(Dbg_cap_ptr_entry(ofl->ofl_lml,
1133			    DBG_STATE_ORIGINAL, CA_SUNW_PLAT, capstr->cs_str));
1134			break;
1135
1136		case CA_SUNW_MACH:
1137			if ((capstr = alist_append(&ocapset.oc_mach.cl_val,
1138			    NULL, sizeof (Capstr), AL_CNT_CAP_NAMES)) == NULL)
1139				return (S_ERROR);
1140			capstr->cs_str = strs + data->c_un.c_ptr;
1141			DBG_CALL(Dbg_cap_ptr_entry(ofl->ofl_lml,
1142			    DBG_STATE_ORIGINAL, CA_SUNW_MACH, capstr->cs_str));
1143			break;
1144
1145		case CA_SUNW_ID:
1146			ocapset.oc_id.cs_str = strs + data->c_un.c_ptr;
1147			DBG_CALL(Dbg_cap_ptr_entry(ofl->ofl_lml,
1148			    DBG_STATE_ORIGINAL, CA_SUNW_ID,
1149			    ocapset.oc_id.cs_str));
1150			break;
1151		}
1152
1153		/*
1154		 * Save the start of this new group.
1155		 */
1156		if (descapndx == -1)
1157			descapndx = ndx;
1158	}
1159	return (1);
1160}
1161
1162/*
1163 * Capture any symbol capabilities symbols.  An object file that contains symbol
1164 * capabilities has an associated .SUNW_capinfo section.  This section
1165 * identifies which symbols are associated to which capabilities, together with
1166 * their associated lead symbol.  Each of these symbol pairs are recorded for
1167 * processing later.
1168 */
1169static uintptr_t
1170process_capinfo(Ofl_desc *ofl, Ifl_desc *ifl, Is_desc *isp)
1171{
1172	Cap_desc	*cdp = ifl->ifl_caps;
1173	Capinfo		*capinfo = isp->is_indata->d_buf;
1174	Shdr		*shdr = isp->is_shdr;
1175	Word		cndx, capinfonum;
1176
1177	capinfonum = (Word)(shdr->sh_size / shdr->sh_entsize);
1178
1179	if ((cdp == NULL) || (capinfo == NULL) || (capinfonum == 0))
1180		return (0);
1181
1182	for (cndx = 1, capinfo++; cndx < capinfonum; cndx++, capinfo++) {
1183		Sym_desc	*sdp, *lsdp;
1184		Word		lndx;
1185		uchar_t		gndx;
1186
1187		if ((gndx = (uchar_t)ELF_C_GROUP(*capinfo)) == 0)
1188			continue;
1189		lndx = (Word)ELF_C_SYM(*capinfo);
1190
1191		/*
1192		 * Catch any anomalies.  A capabilities symbol should be valid,
1193		 * and the capabilities lead symbol should also be global.
1194		 * Note, ld(1) -z symbolcap would create local capabilities
1195		 * symbols, but we don't enforce this so as to give the
1196		 * compilation environment a little more freedom.
1197		 */
1198		if ((sdp = ifl->ifl_oldndx[cndx]) == NULL) {
1199			eprintf(ofl->ofl_lml, ERR_WARNING,
1200			    MSG_INTL(MSG_CAPINFO_INVALSYM), ifl->ifl_name,
1201			    EC_WORD(isp->is_scnndx), isp->is_name, cndx,
1202			    MSG_INTL(MSG_STR_UNKNOWN));
1203			continue;
1204		}
1205		if ((lndx == 0) || (lndx >= ifl->ifl_symscnt) ||
1206		    ((lsdp = ifl->ifl_oldndx[lndx]) == NULL) ||
1207		    (ELF_ST_BIND(lsdp->sd_sym->st_info) != STB_GLOBAL)) {
1208			eprintf(ofl->ofl_lml, ERR_WARNING,
1209			    MSG_INTL(MSG_CAPINFO_INVALLEAD), ifl->ifl_name,
1210			    EC_WORD(isp->is_scnndx), isp->is_name, cndx, lsdp ?
1211			    demangle(lsdp->sd_name) : MSG_INTL(MSG_STR_UNKNOWN),
1212			    lndx);
1213			continue;
1214		}
1215
1216		/*
1217		 * Indicate that this is a capabilities symbol.
1218		 */
1219		sdp->sd_flags |= FLG_SY_CAP;
1220
1221		/*
1222		 * Save any global capability symbols.  Global capability
1223		 * symbols are identified with a CAPINFO_SUNW_GLOB group id.
1224		 * The lead symbol for this global capability symbol is either
1225		 * the symbol itself, or an alias.
1226		 */
1227		if (gndx == CAPINFO_SUNW_GLOB) {
1228			if (ld_cap_add_family(ofl, lsdp, sdp,
1229			    NULL, NULL) == S_ERROR)
1230				return (S_ERROR);
1231			continue;
1232		}
1233
1234		/*
1235		 * Track the number of non-global capabilities symbols, as these
1236		 * are used to size any symbol tables.  If we're generating a
1237		 * dynamic object, this symbol will be added to the dynamic
1238		 * symbol table, therefore ensure there is space in the dynamic
1239		 * string table.
1240		 */
1241		ofl->ofl_caploclcnt++;
1242		if (((ofl->ofl_flags & FLG_OF_RELOBJ) == 0) &&
1243		    (st_insert(ofl->ofl_dynstrtab, sdp->sd_name) == -1))
1244			return (S_ERROR);
1245
1246		/*
1247		 * As we're tracking this local symbol as a capabilities symbol,
1248		 * reduce the local symbol count to compensate.
1249		 */
1250		ofl->ofl_locscnt--;
1251
1252		/*
1253		 * Determine whether the associated lead symbol indicates
1254		 * NODYNSORT.  If so, remove this local entry from the
1255		 * SUNW_dynsort section too.  NODYNSORT tagging can only be
1256		 * obtained from a mapfile symbol definition, and thus any
1257		 * global definition that has this tagging has already been
1258		 * instantiated and this instance resolved to it.
1259		 */
1260		if (lsdp->sd_flags & FLG_SY_NODYNSORT) {
1261			Sym	*lsym = lsdp->sd_sym;
1262			uchar_t ltype = ELF_ST_TYPE(lsym->st_info);
1263
1264			DYNSORT_COUNT(lsdp, lsym, ltype, --);
1265			lsdp->sd_flags |= FLG_SY_NODYNSORT;
1266		}
1267
1268		/*
1269		 * Track this family member, together with its associated group.
1270		 */
1271		if (ld_cap_add_family(ofl, lsdp, sdp,
1272		    cdp->ca_groups->apl_data[gndx], NULL) == S_ERROR)
1273			return (S_ERROR);
1274	}
1275
1276	return (0);
1277}
1278
1279/*
1280 * Simply process the section so that we have pointers to the data for use
1281 * in later routines, however don't add the section to the output section
1282 * list as we will be creating our own replacement sections later (ie.
1283 * symtab and relocation).
1284 */
1285static uintptr_t
1286/* ARGSUSED5 */
1287process_input(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
1288    Word ndx, int ident, Ofl_desc *ofl)
1289{
1290	return (process_section(name, ifl, shdr, scn, ndx,
1291	    ld_targ.t_id.id_null, ofl));
1292}
1293
1294/*
1295 * Keep a running count of relocation entries from input relocatable objects for
1296 * sizing relocation buckets later.  If we're building an executable, save any
1297 * relocations from shared objects to determine if any copy relocation symbol
1298 * has a displacement relocation against it.
1299 */
1300static uintptr_t
1301/* ARGSUSED5 */
1302process_reloc(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
1303    Word ndx, int ident, Ofl_desc *ofl)
1304{
1305	if (process_section(name, ifl,
1306	    shdr, scn, ndx, ld_targ.t_id.id_null, ofl) == S_ERROR)
1307		return (S_ERROR);
1308
1309	if (ifl->ifl_ehdr->e_type == ET_REL) {
1310		if (shdr->sh_entsize && (shdr->sh_entsize <= shdr->sh_size))
1311			/* LINTED */
1312			ofl->ofl_relocincnt +=
1313			    (Word)(shdr->sh_size / shdr->sh_entsize);
1314	} else if (ofl->ofl_flags & FLG_OF_EXEC) {
1315		if (aplist_append(&ifl->ifl_relsect, ifl->ifl_isdesc[ndx],
1316		    AL_CNT_IFL_RELSECS) == NULL)
1317			return (S_ERROR);
1318	}
1319	return (1);
1320}
1321
1322/*
1323 * Process a string table section.  A valid section contains an initial and
1324 * final null byte.
1325 */
1326static uintptr_t
1327process_strtab(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
1328    Word ndx, int ident, Ofl_desc *ofl)
1329{
1330	char		*data;
1331	size_t		size;
1332	Is_desc		*isp;
1333	uintptr_t	error;
1334
1335	/*
1336	 * Never include .stab.excl sections in any output file.
1337	 * If the -s flag has been specified strip any .stab sections.
1338	 */
1339	if (((ofl->ofl_flags & FLG_OF_STRIP) && ident &&
1340	    (strncmp(name, MSG_ORIG(MSG_SCN_STAB), MSG_SCN_STAB_SIZE) == 0)) ||
1341	    (strcmp(name, MSG_ORIG(MSG_SCN_STABEXCL)) == 0) && ident)
1342		return (1);
1343
1344	/*
1345	 * If we got here to process a .shstrtab or .dynstr table, `ident' will
1346	 * be null.  Otherwise make sure we don't have a .strtab section as this
1347	 * should not be added to the output section list either.
1348	 */
1349	if ((ident != ld_targ.t_id.id_null) &&
1350	    (strcmp(name, MSG_ORIG(MSG_SCN_STRTAB)) == 0))
1351		ident = ld_targ.t_id.id_null;
1352
1353	error = process_section(name, ifl, shdr, scn, ndx, ident, ofl);
1354	if ((error == 0) || (error == S_ERROR))
1355		return (error);
1356
1357	/*
1358	 * String tables should start and end with a NULL byte.  Note, it has
1359	 * been known for the assembler to create empty string tables, so check
1360	 * the size before attempting to verify the data itself.
1361	 */
1362	isp = ifl->ifl_isdesc[ndx];
1363	size = isp->is_indata->d_size;
1364	if (size) {
1365		data = isp->is_indata->d_buf;
1366		if (data[0] != '\0' || data[size - 1] != '\0')
1367			eprintf(ofl->ofl_lml, ERR_WARNING,
1368			    MSG_INTL(MSG_FIL_MALSTR), ifl->ifl_name,
1369			    EC_WORD(isp->is_scnndx), name);
1370	} else
1371		isp->is_indata->d_buf = (void *)MSG_ORIG(MSG_STR_EMPTY);
1372
1373	ifl->ifl_flags |= FLG_IF_HSTRTAB;
1374	return (1);
1375}
1376
1377/*
1378 * Invalid sections produce a warning and are skipped.
1379 */
1380static uintptr_t
1381/* ARGSUSED3 */
1382invalid_section(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
1383    Word ndx, int ident, Ofl_desc *ofl)
1384{
1385	Conv_inv_buf_t inv_buf;
1386
1387	eprintf(ofl->ofl_lml, ERR_WARNING, MSG_INTL(MSG_FIL_INVALSEC),
1388	    ifl->ifl_name, EC_WORD(ndx), name,
1389	    conv_sec_type(ifl->ifl_ehdr->e_ident[EI_OSABI],
1390	    ifl->ifl_ehdr->e_machine, shdr->sh_type, 0, &inv_buf));
1391	return (1);
1392}
1393
1394/*
1395 * Compare an input section name to a given string, taking the ELF '%'
1396 * section naming convention into account. If an input section name
1397 * contains a '%' character, the '%' and all following characters are
1398 * ignored in the comparison.
1399 *
1400 * entry:
1401 *	is_name - Name of input section
1402 *	match_name - Name to compare to
1403 *	match_len - strlen(match_name)
1404 *
1405 * exit:
1406 *	Returns True (1) if the names match, and False (0) otherwise.
1407 */
1408inline static int
1409is_name_cmp(const char *is_name, const char *match_name, size_t match_len)
1410{
1411	/*
1412	 * If the start of is_name is not a match for name,
1413	 * the match fails.
1414	 */
1415	if (strncmp(is_name, match_name, match_len) != 0)
1416		return (0);
1417
1418	/*
1419	 * The prefix matched. The next character must be either '%', or
1420	 * NULL, in order for a match to be true.
1421	 */
1422	is_name += match_len;
1423	return ((*is_name == '\0') || (*is_name == '%'));
1424}
1425
1426/*
1427 * Process a progbits section.
1428 */
1429static uintptr_t
1430process_progbits(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
1431    Word ndx, int ident, Ofl_desc *ofl)
1432{
1433	int		stab_index = 0;
1434	Word		is_flags = 0;
1435	uintptr_t	r;
1436
1437	/*
1438	 * Never include .stab.excl sections in any output file.
1439	 * If the -s flag has been specified strip any .stab sections.
1440	 */
1441	if (ident && (strncmp(name, MSG_ORIG(MSG_SCN_STAB),
1442	    MSG_SCN_STAB_SIZE) == 0)) {
1443		if ((ofl->ofl_flags & FLG_OF_STRIP) ||
1444		    (strcmp((name + MSG_SCN_STAB_SIZE),
1445		    MSG_ORIG(MSG_SCN_EXCL)) == 0))
1446			return (1);
1447
1448		if (strcmp((name + MSG_SCN_STAB_SIZE),
1449		    MSG_ORIG(MSG_SCN_INDEX)) == 0)
1450			stab_index = 1;
1451	}
1452
1453	if ((ofl->ofl_flags & FLG_OF_STRIP) && ident) {
1454		if ((strncmp(name, MSG_ORIG(MSG_SCN_DEBUG),
1455		    MSG_SCN_DEBUG_SIZE) == 0) ||
1456		    (strcmp(name, MSG_ORIG(MSG_SCN_LINE)) == 0))
1457			return (1);
1458	}
1459
1460	/*
1461	 * Update the ident to reflect the type of section we've got.
1462	 *
1463	 * If there is any .plt or .got section to generate we'll be creating
1464	 * our own version, so don't allow any input sections of these types to
1465	 * be added to the output section list (why a relocatable object would
1466	 * have a .plt or .got is a mystery, but stranger things have occurred).
1467	 *
1468	 * If there are any unwind sections, and this is a platform that uses
1469	 * SHT_PROGBITS for unwind sections, then set their ident to reflect
1470	 * that.
1471	 */
1472	if (ident) {
1473		if (shdr->sh_flags & SHF_TLS) {
1474			ident = ld_targ.t_id.id_tls;
1475		} else if ((shdr->sh_flags & ~ALL_SHF_IGNORE) ==
1476		    (SHF_ALLOC | SHF_EXECINSTR)) {
1477			ident = ld_targ.t_id.id_text;
1478		} else if (shdr->sh_flags & SHF_ALLOC) {
1479			int done = 0;
1480
1481			if (name[0] == '.') {
1482				switch (name[1]) {
1483				case 'e':
1484					if ((ld_targ.t_m.m_sht_unwind ==
1485					    SHT_PROGBITS) &&
1486					    is_name_cmp(name,
1487					    MSG_ORIG(MSG_SCN_EHFRAME),
1488					    MSG_SCN_EHFRAME_SIZE)) {
1489						ident = ld_targ.t_id.id_unwind;
1490						is_flags = FLG_IS_EHFRAME;
1491						done = 1;
1492					}
1493					break;
1494				case 'g':
1495					if (is_name_cmp(name,
1496					    MSG_ORIG(MSG_SCN_GOT),
1497					    MSG_SCN_GOT_SIZE)) {
1498						ident = ld_targ.t_id.id_null;
1499						done = 1;
1500						break;
1501					}
1502					if ((ld_targ.t_m.m_sht_unwind ==
1503					    SHT_PROGBITS) &&
1504					    is_name_cmp(name,
1505					    MSG_ORIG(MSG_SCN_GCC_X_TBL),
1506					    MSG_SCN_GCC_X_TBL_SIZE)) {
1507						ident = ld_targ.t_id.id_unwind;
1508						done = 1;
1509						break;
1510					}
1511					break;
1512				case 'p':
1513					if (is_name_cmp(name,
1514					    MSG_ORIG(MSG_SCN_PLT),
1515					    MSG_SCN_PLT_SIZE)) {
1516						ident = ld_targ.t_id.id_null;
1517						done = 1;
1518					}
1519					break;
1520				}
1521			}
1522			if (!done) {
1523				if (stab_index) {
1524					/*
1525					 * This is a work-around for x86
1526					 * compilers that have set SHF_ALLOC
1527					 * for the .stab.index section.
1528					 *
1529					 * Because of this, make sure that the
1530					 * .stab.index does not end up as the
1531					 * last section in the text segment.
1532					 * Older linkers can produce
1533					 * segmentation violations when they
1534					 * strip (ld -s) against a shared
1535					 * object whose last section in the
1536					 * text segment is a .stab.
1537					 */
1538					ident = ld_targ.t_id.id_interp;
1539				} else {
1540					ident = ld_targ.t_id.id_data;
1541				}
1542			}
1543		} else
1544			ident = ld_targ.t_id.id_note;
1545	}
1546
1547	r = process_section(name, ifl, shdr, scn, ndx, ident, ofl);
1548
1549	/*
1550	 * On success, process_section() creates an input section descriptor.
1551	 * Now that it exists, we can add any pending input section flags.
1552	 */
1553	if ((is_flags != 0) && (r == 1))
1554		ifl->ifl_isdesc[ndx]->is_flags |= is_flags;
1555
1556	return (r);
1557}
1558
1559/*
1560 * Handles the SHT_SUNW_{DEBUG,DEBUGSTR) sections.
1561 */
1562static uintptr_t
1563process_debug(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
1564    Word ndx, int ident, Ofl_desc *ofl)
1565{
1566	/*
1567	 * Debug information is discarded when the 'ld -s' flag is invoked.
1568	 */
1569	if (ofl->ofl_flags & FLG_OF_STRIP) {
1570		return (1);
1571	}
1572	return (process_progbits(name, ifl, shdr, scn, ndx, ident, ofl));
1573}
1574
1575/*
1576 * Process a nobits section.
1577 */
1578static uintptr_t
1579process_nobits(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
1580    Word ndx, int ident, Ofl_desc *ofl)
1581{
1582	if (ident) {
1583		if (shdr->sh_flags & SHF_TLS)
1584			ident = ld_targ.t_id.id_tlsbss;
1585#if	defined(_ELF64)
1586		else if ((shdr->sh_flags & SHF_AMD64_LARGE) &&
1587		    (ld_targ.t_m.m_mach == EM_AMD64))
1588			ident = ld_targ.t_id.id_lbss;
1589#endif
1590		else
1591			ident = ld_targ.t_id.id_bss;
1592	}
1593	return (process_section(name, ifl, shdr, scn, ndx, ident, ofl));
1594}
1595
1596/*
1597 * Process a SHT_*_ARRAY section.
1598 */
1599static uintptr_t
1600process_array(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
1601    Word ndx, int ident, Ofl_desc *ofl)
1602{
1603	uintptr_t	error;
1604
1605	if (ident)
1606		ident = ld_targ.t_id.id_array;
1607
1608	error = process_section(name, ifl, shdr, scn, ndx, ident, ofl);
1609	if ((error == 0) || (error == S_ERROR))
1610		return (error);
1611
1612	return (1);
1613}
1614
1615static uintptr_t
1616/* ARGSUSED1 */
1617array_process(Is_desc *isc, Ifl_desc *ifl, Ofl_desc *ofl)
1618{
1619	Os_desc	*osp;
1620	Shdr	*shdr;
1621
1622	if ((isc == NULL) || ((osp = isc->is_osdesc) == NULL))
1623		return (0);
1624
1625	shdr = isc->is_shdr;
1626
1627	if ((shdr->sh_type == SHT_FINI_ARRAY) &&
1628	    (ofl->ofl_osfiniarray == NULL))
1629		ofl->ofl_osfiniarray = osp;
1630	else if ((shdr->sh_type == SHT_INIT_ARRAY) &&
1631	    (ofl->ofl_osinitarray == NULL))
1632		ofl->ofl_osinitarray = osp;
1633	else if ((shdr->sh_type == SHT_PREINIT_ARRAY) &&
1634	    (ofl->ofl_ospreinitarray == NULL))
1635		ofl->ofl_ospreinitarray = osp;
1636
1637	return (1);
1638}
1639
1640/*
1641 * Process a SHT_SYMTAB_SHNDX section.
1642 */
1643static uintptr_t
1644process_sym_shndx(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
1645    Word ndx, int ident, Ofl_desc *ofl)
1646{
1647	if (process_input(name, ifl, shdr, scn, ndx, ident, ofl) == S_ERROR)
1648		return (S_ERROR);
1649
1650	/*
1651	 * Have we already seen the related SYMTAB - if so verify it now.
1652	 */
1653	if (shdr->sh_link < ndx) {
1654		Is_desc	*isp = ifl->ifl_isdesc[shdr->sh_link];
1655
1656		if ((isp == NULL) || ((isp->is_shdr->sh_type != SHT_SYMTAB) &&
1657		    (isp->is_shdr->sh_type != SHT_DYNSYM))) {
1658			eprintf(ofl->ofl_lml, ERR_FATAL,
1659			    MSG_INTL(MSG_FIL_INVSHLINK), ifl->ifl_name,
1660			    EC_WORD(ndx), name, EC_XWORD(shdr->sh_link));
1661			return (S_ERROR);
1662		}
1663		isp->is_symshndx = ifl->ifl_isdesc[ndx];
1664	}
1665	return (1);
1666}
1667
1668/*
1669 * Final processing for SHT_SYMTAB_SHNDX section.
1670 */
1671static uintptr_t
1672/* ARGSUSED2 */
1673sym_shndx_process(Is_desc *isc, Ifl_desc *ifl, Ofl_desc *ofl)
1674{
1675	if (isc->is_shdr->sh_link > isc->is_scnndx) {
1676		Is_desc	*isp = ifl->ifl_isdesc[isc->is_shdr->sh_link];
1677
1678		if ((isp == NULL) || ((isp->is_shdr->sh_type != SHT_SYMTAB) &&
1679		    (isp->is_shdr->sh_type != SHT_DYNSYM))) {
1680			eprintf(ofl->ofl_lml, ERR_FATAL,
1681			    MSG_INTL(MSG_FIL_INVSHLINK), isc->is_file->ifl_name,
1682			    EC_WORD(isc->is_scnndx), isc->is_name,
1683			    EC_XWORD(isc->is_shdr->sh_link));
1684			return (S_ERROR);
1685		}
1686		isp->is_symshndx = isc;
1687	}
1688	return (1);
1689}
1690
1691/*
1692 * Process .dynamic section from a relocatable object.
1693 *
1694 * Note: That the .dynamic section is only considered interesting when
1695 *	 dlopen()ing a relocatable object (thus FLG_OF1_RELDYN can only get
1696 *	 set when libld is called from ld.so.1).
1697 */
1698/*ARGSUSED*/
1699static uintptr_t
1700process_rel_dynamic(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
1701    Word ndx, int ident, Ofl_desc *ofl)
1702{
1703	Dyn		*dyn;
1704	Elf_Scn		*strscn;
1705	Elf_Data	*dp;
1706	char		*str;
1707
1708	/*
1709	 * Process .dynamic sections from relocatable objects ?
1710	 */
1711	if ((ofl->ofl_flags1 & FLG_OF1_RELDYN) == 0)
1712		return (1);
1713
1714	/*
1715	 * Find the string section associated with the .dynamic section.
1716	 */
1717	if ((strscn = elf_getscn(ifl->ifl_elf, shdr->sh_link)) == NULL) {
1718		eprintf(ofl->ofl_lml, ERR_ELF, MSG_INTL(MSG_ELF_GETSCN),
1719		    ifl->ifl_name);
1720		ofl->ofl_flags |= FLG_OF_FATAL;
1721		return (0);
1722	}
1723	dp = elf_getdata(strscn, NULL);
1724	str = (char *)dp->d_buf;
1725
1726	/*
1727	 * And get the .dynamic data
1728	 */
1729	dp = elf_getdata(scn, NULL);
1730
1731	for (dyn = (Dyn *)dp->d_buf; dyn->d_tag != DT_NULL; dyn++) {
1732		Ifl_desc	*difl;
1733
1734		switch (dyn->d_tag) {
1735		case DT_NEEDED:
1736		case DT_USED:
1737			if (((difl = libld_calloc(1,
1738			    sizeof (Ifl_desc))) == NULL) ||
1739			    (aplist_append(&ofl->ofl_sos, difl,
1740			    AL_CNT_OFL_LIBS) == NULL))
1741				return (S_ERROR);
1742
1743			difl->ifl_name = MSG_ORIG(MSG_STR_DYNAMIC);
1744			difl->ifl_soname = str + (size_t)dyn->d_un.d_val;
1745			difl->ifl_flags = FLG_IF_NEEDSTR;
1746			break;
1747		case DT_RPATH:
1748		case DT_RUNPATH:
1749			if ((ofl->ofl_rpath = add_string(ofl->ofl_rpath,
1750			    (str + (size_t)dyn->d_un.d_val))) ==
1751			    (const char *)S_ERROR)
1752				return (S_ERROR);
1753			break;
1754		case DT_VERSYM:
1755			/*
1756			 * The Solaris ld does not put DT_VERSYM in the
1757			 * dynamic section. If the object has DT_VERSYM,
1758			 * then it must have been produced by the GNU ld,
1759			 * and is using the GNU style of versioning.
1760			 */
1761			ifl->ifl_flags |= FLG_IF_GNUVER;
1762			break;
1763		}
1764	}
1765	return (1);
1766}
1767
1768/*
1769 * Expand implicit references.  Dependencies can be specified in terms of the
1770 * $ORIGIN, $MACHINE, $PLATFORM, $OSREL and $OSNAME tokens, either from their
1771 * needed name, or via a runpath.  In addition runpaths may also specify the
1772 * $ISALIST token.
1773 *
1774 * Probably the most common reference to explicit dependencies (via -L) will be
1775 * sufficient to find any associated implicit dependencies, but just in case we
1776 * expand any occurrence of these known tokens here.
1777 *
1778 * Note, if any errors occur we simply return the original name.
1779 *
1780 * This code is remarkably similar to expand() in rtld/common/paths.c.
1781 */
1782static char		*machine = NULL;
1783static size_t		machine_sz = 0;
1784static char		*platform = NULL;
1785static size_t		platform_sz = 0;
1786static Isa_desc		*isa = NULL;
1787static Uts_desc		*uts = NULL;
1788
1789static char *
1790expand(const char *parent, const char *name, char **next)
1791{
1792	char		_name[PATH_MAX], *nptr, *_next;
1793	const char	*optr;
1794	size_t		nrem = PATH_MAX - 1;
1795	int		expanded = 0, _expanded, isaflag = 0;
1796
1797	optr = name;
1798	nptr = _name;
1799
1800	while (*optr) {
1801		if (nrem == 0)
1802			return ((char *)name);
1803
1804		if (*optr != '$') {
1805			*nptr++ = *optr++, nrem--;
1806			continue;
1807		}
1808
1809		_expanded = 0;
1810
1811		if (strncmp(optr, MSG_ORIG(MSG_STR_ORIGIN),
1812		    MSG_STR_ORIGIN_SIZE) == 0) {
1813			char *eptr;
1814
1815			/*
1816			 * For $ORIGIN, expansion is really just a concatenation
1817			 * of the parents directory name.  For example, an
1818			 * explicit dependency foo/bar/lib1.so with a dependency
1819			 * on $ORIGIN/lib2.so would be expanded to
1820			 * foo/bar/lib2.so.
1821			 */
1822			if ((eptr = strrchr(parent, '/')) == NULL) {
1823				*nptr++ = '.';
1824				nrem--;
1825			} else {
1826				size_t	len = eptr - parent;
1827
1828				if (len >= nrem)
1829					return ((char *)name);
1830
1831				(void) strncpy(nptr, parent, len);
1832				nptr = nptr + len;
1833				nrem -= len;
1834			}
1835			optr += MSG_STR_ORIGIN_SIZE;
1836			expanded = _expanded = 1;
1837
1838		} else if (strncmp(optr, MSG_ORIG(MSG_STR_MACHINE),
1839		    MSG_STR_MACHINE_SIZE) == 0) {
1840			/*
1841			 * Establish the machine from sysconf - like uname -i.
1842			 */
1843			if ((machine == NULL) && (machine_sz == 0)) {
1844				char	info[SYS_NMLN];
1845				long	size;
1846
1847				size = sysinfo(SI_MACHINE, info, SYS_NMLN);
1848				if ((size != -1) &&
1849				    (machine = libld_malloc((size_t)size))) {
1850					(void) strcpy(machine, info);
1851					machine_sz = (size_t)size - 1;
1852				} else
1853					machine_sz = 1;
1854			}
1855			if (machine) {
1856				if (machine_sz >= nrem)
1857					return ((char *)name);
1858
1859				(void) strncpy(nptr, machine, machine_sz);
1860				nptr = nptr + machine_sz;
1861				nrem -= machine_sz;
1862
1863				optr += MSG_STR_MACHINE_SIZE;
1864				expanded = _expanded = 1;
1865			}
1866
1867		} else if (strncmp(optr, MSG_ORIG(MSG_STR_PLATFORM),
1868		    MSG_STR_PLATFORM_SIZE) == 0) {
1869			/*
1870			 * Establish the platform from sysconf - like uname -i.
1871			 */
1872			if ((platform == NULL) && (platform_sz == 0)) {
1873				char	info[SYS_NMLN];
1874				long	size;
1875
1876				size = sysinfo(SI_PLATFORM, info, SYS_NMLN);
1877				if ((size != -1) &&
1878				    (platform = libld_malloc((size_t)size))) {
1879					(void) strcpy(platform, info);
1880					platform_sz = (size_t)size - 1;
1881				} else
1882					platform_sz = 1;
1883			}
1884			if (platform) {
1885				if (platform_sz >= nrem)
1886					return ((char *)name);
1887
1888				(void) strncpy(nptr, platform, platform_sz);
1889				nptr = nptr + platform_sz;
1890				nrem -= platform_sz;
1891
1892				optr += MSG_STR_PLATFORM_SIZE;
1893				expanded = _expanded = 1;
1894			}
1895
1896		} else if (strncmp(optr, MSG_ORIG(MSG_STR_OSNAME),
1897		    MSG_STR_OSNAME_SIZE) == 0) {
1898			/*
1899			 * Establish the os name - like uname -s.
1900			 */
1901			if (uts == NULL)
1902				uts = conv_uts();
1903
1904			if (uts && uts->uts_osnamesz) {
1905				if (uts->uts_osnamesz >= nrem)
1906					return ((char *)name);
1907
1908				(void) strncpy(nptr, uts->uts_osname,
1909				    uts->uts_osnamesz);
1910				nptr = nptr + uts->uts_osnamesz;
1911				nrem -= uts->uts_osnamesz;
1912
1913				optr += MSG_STR_OSNAME_SIZE;
1914				expanded = _expanded = 1;
1915			}
1916
1917		} else if (strncmp(optr, MSG_ORIG(MSG_STR_OSREL),
1918		    MSG_STR_OSREL_SIZE) == 0) {
1919			/*
1920			 * Establish the os release - like uname -r.
1921			 */
1922			if (uts == NULL)
1923				uts = conv_uts();
1924
1925			if (uts && uts->uts_osrelsz) {
1926				if (uts->uts_osrelsz >= nrem)
1927					return ((char *)name);
1928
1929				(void) strncpy(nptr, uts->uts_osrel,
1930				    uts->uts_osrelsz);
1931				nptr = nptr + uts->uts_osrelsz;
1932				nrem -= uts->uts_osrelsz;
1933
1934				optr += MSG_STR_OSREL_SIZE;
1935				expanded = _expanded = 1;
1936			}
1937
1938		} else if ((strncmp(optr, MSG_ORIG(MSG_STR_ISALIST),
1939		    MSG_STR_ISALIST_SIZE) == 0) && next && (isaflag++ == 0)) {
1940			/*
1941			 * Establish instruction sets from sysconf.  Note that
1942			 * this is only meaningful from runpaths.
1943			 */
1944			if (isa == NULL)
1945				isa = conv_isalist();
1946
1947			if (isa && isa->isa_listsz &&
1948			    (nrem > isa->isa_opt->isa_namesz)) {
1949				size_t		mlen, tlen, hlen = optr - name;
1950				size_t		no;
1951				char		*lptr;
1952				Isa_opt		*opt = isa->isa_opt;
1953
1954				(void) strncpy(nptr, opt->isa_name,
1955				    opt->isa_namesz);
1956				nptr = nptr + opt->isa_namesz;
1957				nrem -= opt->isa_namesz;
1958
1959				optr += MSG_STR_ISALIST_SIZE;
1960				expanded = _expanded = 1;
1961
1962				tlen = strlen(optr);
1963
1964				/*
1965				 * As ISALIST expands to a number of elements,
1966				 * establish a new list to return to the caller.
1967				 * This will contain the present path being
1968				 * processed redefined for each isalist option,
1969				 * plus the original remaining list entries.
1970				 */
1971				mlen = ((hlen + tlen) * (isa->isa_optno - 1)) +
1972				    isa->isa_listsz - opt->isa_namesz;
1973				if (*next)
1974					mlen += strlen(*next);
1975				if ((_next = lptr = libld_malloc(mlen)) == NULL)
1976					return (0);
1977
1978				for (no = 1, opt++; no < isa->isa_optno;
1979				    no++, opt++) {
1980					(void) strncpy(lptr, name, hlen);
1981					lptr = lptr + hlen;
1982					(void) strncpy(lptr, opt->isa_name,
1983					    opt->isa_namesz);
1984					lptr = lptr + opt->isa_namesz;
1985					(void) strncpy(lptr, optr, tlen);
1986					lptr = lptr + tlen;
1987					*lptr++ = ':';
1988				}
1989				if (*next)
1990					(void) strcpy(lptr, *next);
1991				else
1992					*--lptr = '\0';
1993			}
1994		}
1995
1996		/*
1997		 * If no expansion occurred skip the $ and continue.
1998		 */
1999		if (_expanded == 0)
2000			*nptr++ = *optr++, nrem--;
2001	}
2002
2003	/*
2004	 * If any ISALIST processing has occurred not only do we return the
2005	 * expanded node we're presently working on, but we must also update the
2006	 * remaining list so that it is effectively prepended with this node
2007	 * expanded to all remaining isalist options.  Note that we can only
2008	 * handle one ISALIST per node.  For more than one ISALIST to be
2009	 * processed we'd need a better algorithm than above to replace the
2010	 * newly generated list.  Whether we want to encourage the number of
2011	 * pathname permutations this would provide is another question. So, for
2012	 * now if more than one ISALIST is encountered we return the original
2013	 * node untouched.
2014	 */
2015	if (isaflag) {
2016		if (isaflag == 1)
2017			*next = _next;
2018		else
2019			return ((char *)name);
2020	}
2021
2022	*nptr = '\0';
2023
2024	if (expanded) {
2025		if ((nptr = libld_malloc(strlen(_name) + 1)) == NULL)
2026			return ((char *)name);
2027		(void) strcpy(nptr, _name);
2028		return (nptr);
2029	}
2030	return ((char *)name);
2031}
2032
2033/*
2034 * The Solaris ld does not put DT_VERSYM in the dynamic section, but the
2035 * GNU ld does, and it is used by the runtime linker to implement their
2036 * versioning scheme. Use this fact to determine if the sharable object
2037 * was produced by the GNU ld rather than the Solaris one, and to set
2038 * FLG_IF_GNUVER if so. This needs to be done before the symbols are
2039 * processed, since the answer determines whether we interpret the
2040 * symbols versions according to Solaris or GNU rules.
2041 */
2042/*ARGSUSED*/
2043static uintptr_t
2044process_dynamic_isgnu(const char *name, Ifl_desc *ifl, Shdr *shdr,
2045    Elf_Scn *scn, Word ndx, int ident, Ofl_desc *ofl)
2046{
2047	Dyn		*dyn;
2048	Elf_Data	*dp;
2049	uintptr_t	error;
2050
2051	error = process_section(name, ifl, shdr, scn, ndx, ident, ofl);
2052	if ((error == 0) || (error == S_ERROR))
2053		return (error);
2054
2055	/* Get the .dynamic data */
2056	dp = elf_getdata(scn, NULL);
2057
2058	for (dyn = (Dyn *)dp->d_buf; dyn->d_tag != DT_NULL; dyn++) {
2059		if (dyn->d_tag == DT_VERSYM) {
2060			ifl->ifl_flags |= FLG_IF_GNUVER;
2061			break;
2062		}
2063	}
2064	return (1);
2065}
2066
2067/*
2068 * Process a dynamic section.  If we are processing an explicit shared object
2069 * then we need to determine if it has a recorded SONAME, if so, this name will
2070 * be recorded in the output file being generated as the NEEDED entry rather
2071 * than the shared objects filename itself.
2072 * If the mode of the link-edit indicates that no undefined symbols should
2073 * remain, then we also need to build up a list of any additional shared object
2074 * dependencies this object may have.  In this case save any NEEDED entries
2075 * together with any associated run-path specifications.  This information is
2076 * recorded on the `ofl_soneed' list and will be analyzed after all explicit
2077 * file processing has been completed (refer finish_libs()).
2078 */
2079static uintptr_t
2080process_dynamic(Is_desc *isc, Ifl_desc *ifl, Ofl_desc *ofl)
2081{
2082	Dyn		*data, *dyn;
2083	char		*str, *rpath = NULL;
2084	const char	*soname, *needed;
2085
2086	data = (Dyn *)isc->is_indata->d_buf;
2087	str = (char *)ifl->ifl_isdesc[isc->is_shdr->sh_link]->is_indata->d_buf;
2088
2089	/*
2090	 * First loop through the dynamic section looking for a run path.
2091	 */
2092	if (ofl->ofl_flags & (FLG_OF_NOUNDEF | FLG_OF_SYMBOLIC)) {
2093		for (dyn = data; dyn->d_tag != DT_NULL; dyn++) {
2094			if ((dyn->d_tag != DT_RPATH) &&
2095			    (dyn->d_tag != DT_RUNPATH))
2096				continue;
2097			if ((rpath = str + (size_t)dyn->d_un.d_val) == NULL)
2098				continue;
2099			break;
2100		}
2101	}
2102
2103	/*
2104	 * Now look for any needed dependencies (which may use the rpath)
2105	 * or a new SONAME.
2106	 */
2107	for (dyn = data; dyn->d_tag != DT_NULL; dyn++) {
2108		if (dyn->d_tag == DT_SONAME) {
2109			if ((soname = str + (size_t)dyn->d_un.d_val) == NULL)
2110				continue;
2111
2112			/*
2113			 * Update the input file structure with this new name.
2114			 */
2115			ifl->ifl_soname = soname;
2116
2117		} else if ((dyn->d_tag == DT_NEEDED) ||
2118		    (dyn->d_tag == DT_USED)) {
2119			Sdf_desc	*sdf;
2120
2121			if (!(ofl->ofl_flags &
2122			    (FLG_OF_NOUNDEF | FLG_OF_SYMBOLIC)))
2123				continue;
2124			if ((needed = str + (size_t)dyn->d_un.d_val) == NULL)
2125				continue;
2126
2127			/*
2128			 * Determine if this needed entry is already recorded on
2129			 * the shared object needed list, if not create a new
2130			 * definition for later processing (see finish_libs()).
2131			 */
2132			needed = expand(ifl->ifl_name, needed, NULL);
2133
2134			if ((sdf = sdf_find(needed, ofl->ofl_soneed)) == NULL) {
2135				if ((sdf = sdf_add(needed,
2136				    &ofl->ofl_soneed)) == (Sdf_desc *)S_ERROR)
2137					return (S_ERROR);
2138				sdf->sdf_rfile = ifl->ifl_name;
2139			}
2140
2141			/*
2142			 * Record the runpath (Note that we take the first
2143			 * runpath which is exactly what ld.so.1 would do during
2144			 * its dependency processing).
2145			 */
2146			if (rpath && (sdf->sdf_rpath == NULL))
2147				sdf->sdf_rpath = rpath;
2148
2149		} else if (dyn->d_tag == DT_FLAGS_1) {
2150			if (dyn->d_un.d_val & (DF_1_INITFIRST | DF_1_INTERPOSE))
2151				ifl->ifl_flags &= ~FLG_IF_LAZYLD;
2152			if (dyn->d_un.d_val & DF_1_DISPRELPND)
2153				ifl->ifl_flags |= FLG_IF_DISPPEND;
2154			if (dyn->d_un.d_val & DF_1_DISPRELDNE)
2155				ifl->ifl_flags |= FLG_IF_DISPDONE;
2156			if (dyn->d_un.d_val & DF_1_NODIRECT)
2157				ifl->ifl_flags |= FLG_IF_NODIRECT;
2158
2159		} else if ((dyn->d_tag == DT_AUDIT) &&
2160		    (ifl->ifl_flags & FLG_IF_NEEDED)) {
2161			/*
2162			 * Record audit string as DT_DEPAUDIT.
2163			 */
2164			if ((ofl->ofl_depaudit = add_string(ofl->ofl_depaudit,
2165			    (str + (size_t)dyn->d_un.d_val))) ==
2166			    (const char *)S_ERROR)
2167				return (S_ERROR);
2168
2169		} else if (dyn->d_tag == DT_SUNW_RTLDINF) {
2170			/*
2171			 * If a library has the SUNW_RTLDINF .dynamic entry
2172			 * then we must not permit lazyloading of this library.
2173			 * This is because critical startup information (TLS
2174			 * routines) are provided as part of these interfaces
2175			 * and we must have them as part of process startup.
2176			 */
2177			ifl->ifl_flags &= ~FLG_IF_LAZYLD;
2178		}
2179	}
2180
2181	/*
2182	 * Perform some SONAME sanity checks.
2183	 */
2184	if (ifl->ifl_flags & FLG_IF_NEEDED) {
2185		Ifl_desc	*sifl;
2186		Aliste		idx;
2187
2188		/*
2189		 * Determine if anyone else will cause the same SONAME to be
2190		 * used (this is either caused by two different files having the
2191		 * same SONAME, or by one file SONAME actually matching another
2192		 * file basename (if no SONAME is specified within a shared
2193		 * library its basename will be used)). Probably rare, but some
2194		 * idiot will do it.
2195		 */
2196		for (APLIST_TRAVERSE(ofl->ofl_sos, idx, sifl)) {
2197			if ((strcmp(ifl->ifl_soname, sifl->ifl_soname) == 0) &&
2198			    (ifl != sifl)) {
2199				const char	*hint, *iflb, *siflb;
2200
2201				/*
2202				 * Determine the basename of each file. Perhaps
2203				 * there are multiple copies of the same file
2204				 * being brought in using different -L search
2205				 * paths, and if so give an extra hint in the
2206				 * error message.
2207				 */
2208				iflb = strrchr(ifl->ifl_name, '/');
2209				if (iflb == NULL)
2210					iflb = ifl->ifl_name;
2211				else
2212					iflb++;
2213
2214				siflb = strrchr(sifl->ifl_name, '/');
2215				if (siflb == NULL)
2216					siflb = sifl->ifl_name;
2217				else
2218					siflb++;
2219
2220				if (strcmp(iflb, siflb) == 0)
2221					hint = MSG_INTL(MSG_REC_CNFLTHINT);
2222				else
2223					hint = MSG_ORIG(MSG_STR_EMPTY);
2224
2225				eprintf(ofl->ofl_lml, ERR_FATAL,
2226				    MSG_INTL(MSG_REC_OBJCNFLT), sifl->ifl_name,
2227				    ifl->ifl_name, sifl->ifl_soname, hint);
2228				ofl->ofl_flags |= FLG_OF_FATAL;
2229				return (0);
2230			}
2231		}
2232
2233		/*
2234		 * If the SONAME is the same as the name the user wishes to
2235		 * record when building a dynamic library (refer -h option),
2236		 * we also have a name clash.
2237		 */
2238		if (ofl->ofl_soname &&
2239		    (strcmp(ofl->ofl_soname, ifl->ifl_soname) == 0)) {
2240			eprintf(ofl->ofl_lml, ERR_FATAL,
2241			    MSG_INTL(MSG_REC_OPTCNFLT), ifl->ifl_name,
2242			    MSG_INTL(MSG_MARG_SONAME), ifl->ifl_soname);
2243			ofl->ofl_flags |= FLG_OF_FATAL;
2244			return (0);
2245		}
2246	}
2247	return (1);
2248}
2249
2250/*
2251 * Process a progbits section from a relocatable object (ET_REL).
2252 * This is used on non-amd64 objects to recognize .eh_frame sections.
2253 */
2254/*ARGSUSED1*/
2255static uintptr_t
2256process_progbits_final(Is_desc *isc, Ifl_desc *ifl, Ofl_desc *ofl)
2257{
2258	if (isc->is_osdesc && (isc->is_flags & FLG_IS_EHFRAME) &&
2259	    (ld_unwind_register(isc->is_osdesc, ofl) == S_ERROR))
2260		return (S_ERROR);
2261
2262	return (1);
2263}
2264
2265/*
2266 * Process a group section.
2267 */
2268static uintptr_t
2269process_group(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
2270    Word ndx, int ident, Ofl_desc *ofl)
2271{
2272	uintptr_t	error;
2273
2274	error = process_section(name, ifl, shdr, scn, ndx, ident, ofl);
2275	if ((error == 0) || (error == S_ERROR))
2276		return (error);
2277
2278	/*
2279	 * Indicate that this input file has groups to process.  Groups are
2280	 * processed after all input sections have been processed.
2281	 */
2282	ifl->ifl_flags |= FLG_IS_GROUPS;
2283
2284	return (1);
2285}
2286
2287/*
2288 * Process a relocation entry. At this point all input sections from this
2289 * input file have been assigned an input section descriptor which is saved
2290 * in the `ifl_isdesc' array.
2291 */
2292static uintptr_t
2293rel_process(Is_desc *isc, Ifl_desc *ifl, Ofl_desc *ofl)
2294{
2295	Word 	rndx;
2296	Is_desc	*risc;
2297	Os_desc	*osp;
2298	Shdr	*shdr = isc->is_shdr;
2299	Conv_inv_buf_t inv_buf;
2300
2301	/*
2302	 * Make sure this is a valid relocation we can handle.
2303	 */
2304	if (shdr->sh_type != ld_targ.t_m.m_rel_sht_type) {
2305		eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_FIL_INVALSEC),
2306		    ifl->ifl_name, EC_WORD(isc->is_scnndx), isc->is_name,
2307		    conv_sec_type(ifl->ifl_ehdr->e_ident[EI_OSABI],
2308		    ifl->ifl_ehdr->e_machine, shdr->sh_type, 0, &inv_buf));
2309		ofl->ofl_flags |= FLG_OF_FATAL;
2310		return (0);
2311	}
2312
2313	/*
2314	 * From the relocation section header information determine which
2315	 * section needs the actual relocation.  Determine which output section
2316	 * this input section has been assigned to and add to its relocation
2317	 * list.  Note that the relocation section may be null if it is not
2318	 * required (ie. .debug, .stabs, etc).
2319	 */
2320	rndx = shdr->sh_info;
2321	if (rndx >= ifl->ifl_shnum) {
2322		/*
2323		 * Broken input file.
2324		 */
2325		eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_FIL_INVSHINFO),
2326		    ifl->ifl_name, EC_WORD(isc->is_scnndx), isc->is_name,
2327		    EC_XWORD(rndx));
2328		ofl->ofl_flags |= FLG_OF_FATAL;
2329		return (0);
2330	}
2331	if (rndx == 0) {
2332		if (aplist_append(&ofl->ofl_extrarels, isc,
2333		    AL_CNT_OFL_RELS) == NULL)
2334			return (S_ERROR);
2335
2336	} else if ((risc = ifl->ifl_isdesc[rndx]) != NULL) {
2337		/*
2338		 * Discard relocations if they are against a section
2339		 * which has been discarded.
2340		 */
2341		if (risc->is_flags & FLG_IS_DISCARD)
2342			return (1);
2343
2344		if ((osp = risc->is_osdesc) == NULL) {
2345			if (risc->is_shdr->sh_type == SHT_SUNW_move) {
2346				/*
2347				 * This section is processed later in
2348				 * process_movereloc().
2349				 */
2350				if (aplist_append(&ofl->ofl_ismoverel,
2351				    isc, AL_CNT_OFL_MOVE) == NULL)
2352					return (S_ERROR);
2353				return (1);
2354			}
2355			eprintf(ofl->ofl_lml, ERR_FATAL,
2356			    MSG_INTL(MSG_FIL_INVRELOC1), ifl->ifl_name,
2357			    EC_WORD(isc->is_scnndx), isc->is_name,
2358			    EC_WORD(risc->is_scnndx), risc->is_name);
2359			return (0);
2360		}
2361		if (aplist_append(&osp->os_relisdescs, isc,
2362		    AL_CNT_OS_RELISDESCS) == NULL)
2363			return (S_ERROR);
2364	}
2365	return (1);
2366}
2367
2368/*
2369 * SHF_EXCLUDE flags is set for this section.
2370 */
2371static uintptr_t
2372process_exclude(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
2373    Word ndx, Ofl_desc *ofl)
2374{
2375	/*
2376	 * Sections SHT_SYMTAB and SHT_DYNDYM, even if SHF_EXCLUDE is on, might
2377	 * be needed for ld processing.  These sections need to be in the
2378	 * internal table.  Later it will be determined whether they can be
2379	 * eliminated or not.
2380	 */
2381	if (shdr->sh_type == SHT_SYMTAB || shdr->sh_type == SHT_DYNSYM)
2382		return (0);
2383
2384	/*
2385	 * Other checks
2386	 */
2387	if (shdr->sh_flags & SHF_ALLOC) {
2388		/*
2389		 * A conflict, issue an warning message, and ignore the section.
2390		 */
2391		eprintf(ofl->ofl_lml, ERR_WARNING, MSG_INTL(MSG_FIL_EXCLUDE),
2392		    ifl->ifl_name, EC_WORD(ndx), name);
2393		return (0);
2394	}
2395
2396	/*
2397	 * This sections is not going to the output file.
2398	 */
2399	return (process_section(name, ifl, shdr, scn, ndx, 0, ofl));
2400}
2401
2402/*
2403 * Section processing state table.  `Initial' describes the required initial
2404 * procedure to be called (if any), `Final' describes the final processing
2405 * procedure (ie. things that can only be done when all required sections
2406 * have been collected).
2407 */
2408typedef uintptr_t	(* initial_func_t)(const char *, Ifl_desc *, Shdr *,
2409			    Elf_Scn *, Word, int, Ofl_desc *);
2410
2411static initial_func_t Initial[SHT_NUM][2] = {
2412/*			ET_REL			ET_DYN			*/
2413
2414/* SHT_NULL	*/	invalid_section,	invalid_section,
2415/* SHT_PROGBITS	*/	process_progbits,	process_progbits,
2416/* SHT_SYMTAB	*/	process_input,		process_input,
2417/* SHT_STRTAB	*/	process_strtab,		process_strtab,
2418/* SHT_RELA	*/	process_reloc,		process_reloc,
2419/* SHT_HASH	*/	invalid_section,	NULL,
2420/* SHT_DYNAMIC	*/	process_rel_dynamic,	process_dynamic_isgnu,
2421/* SHT_NOTE	*/	process_section,	NULL,
2422/* SHT_NOBITS	*/	process_nobits,		process_nobits,
2423/* SHT_REL	*/	process_reloc,		process_reloc,
2424/* SHT_SHLIB	*/	process_section,	invalid_section,
2425/* SHT_DYNSYM	*/	invalid_section,	process_input,
2426/* SHT_UNKNOWN12 */	process_progbits,	process_progbits,
2427/* SHT_UNKNOWN13 */	process_progbits,	process_progbits,
2428/* SHT_INIT_ARRAY */	process_array,		NULL,
2429/* SHT_FINI_ARRAY */	process_array,		NULL,
2430/* SHT_PREINIT_ARRAY */	process_array,		NULL,
2431/* SHT_GROUP */		process_group,		invalid_section,
2432/* SHT_SYMTAB_SHNDX */	process_sym_shndx,	NULL
2433};
2434
2435typedef uintptr_t	(* final_func_t)(Is_desc *, Ifl_desc *, Ofl_desc *);
2436
2437static final_func_t Final[SHT_NUM][2] = {
2438/*			ET_REL			ET_DYN			*/
2439
2440/* SHT_NULL	*/	NULL,			NULL,
2441/* SHT_PROGBITS	*/	process_progbits_final,	NULL,
2442/* SHT_SYMTAB	*/	ld_sym_process,		ld_sym_process,
2443/* SHT_STRTAB	*/	NULL,			NULL,
2444/* SHT_RELA	*/	rel_process,		NULL,
2445/* SHT_HASH	*/	NULL,			NULL,
2446/* SHT_DYNAMIC	*/	NULL,			process_dynamic,
2447/* SHT_NOTE	*/	NULL,			NULL,
2448/* SHT_NOBITS	*/	NULL,			NULL,
2449/* SHT_REL	*/	rel_process,		NULL,
2450/* SHT_SHLIB	*/	NULL,			NULL,
2451/* SHT_DYNSYM	*/	NULL,			ld_sym_process,
2452/* SHT_UNKNOWN12 */	NULL,			NULL,
2453/* SHT_UNKNOWN13 */	NULL,			NULL,
2454/* SHT_INIT_ARRAY */	array_process,		NULL,
2455/* SHT_FINI_ARRAY */	array_process,		NULL,
2456/* SHT_PREINIT_ARRAY */	array_process,		NULL,
2457/* SHT_GROUP */		NULL,			NULL,
2458/* SHT_SYMTAB_SHNDX */	sym_shndx_process,	NULL
2459};
2460
2461#define	MAXNDXSIZE	10
2462
2463/*
2464 * Process an elf file.  Each section is compared against the section state
2465 * table to determine whether it should be processed (saved), ignored, or
2466 * is invalid for the type of input file being processed.
2467 */
2468static uintptr_t
2469process_elf(Ifl_desc *ifl, Elf *elf, Ofl_desc *ofl)
2470{
2471	Elf_Scn		*scn;
2472	Shdr		*shdr;
2473	Word		ndx, sndx, ordndx = 0, ordcnt = 0;
2474	char		*str, *name;
2475	Word		row, column;
2476	int		ident;
2477	uintptr_t	error;
2478	Is_desc		*vdfisp, *vndisp, *vsyisp, *sifisp;
2479	Is_desc		*capinfoisp, *capisp;
2480	Sdf_desc	*sdf;
2481	Place_path_info	path_info_buf, *path_info;
2482
2483	/*
2484	 * Path information buffer used by ld_place_section() and related
2485	 * routines. This information is used to evaluate entrance criteria
2486	 * with non-empty file matching lists (ec_files).
2487	 */
2488	path_info = ld_place_path_info_init(ofl, ifl, &path_info_buf);
2489
2490	/*
2491	 * First process the .shstrtab section so that later sections can
2492	 * reference their name.
2493	 */
2494	ld_sup_file(ofl, ifl->ifl_name, elf_kind(elf), ifl->ifl_flags, elf);
2495
2496	sndx = ifl->ifl_shstrndx;
2497	if ((scn = elf_getscn(elf, (size_t)sndx)) == NULL) {
2498		eprintf(ofl->ofl_lml, ERR_ELF, MSG_INTL(MSG_ELF_GETSCN),
2499		    ifl->ifl_name);
2500		ofl->ofl_flags |= FLG_OF_FATAL;
2501		return (0);
2502	}
2503	if ((shdr = elf_getshdr(scn)) == NULL) {
2504		eprintf(ofl->ofl_lml, ERR_ELF, MSG_INTL(MSG_ELF_GETSHDR),
2505		    ifl->ifl_name);
2506		ofl->ofl_flags |= FLG_OF_FATAL;
2507		return (0);
2508	}
2509	if ((name = elf_strptr(elf, (size_t)sndx, (size_t)shdr->sh_name)) ==
2510	    NULL) {
2511		eprintf(ofl->ofl_lml, ERR_ELF, MSG_INTL(MSG_ELF_STRPTR),
2512		    ifl->ifl_name);
2513		ofl->ofl_flags |= FLG_OF_FATAL;
2514		return (0);
2515	}
2516
2517	if (ld_sup_input_section(ofl, ifl, name, &shdr, sndx, scn,
2518	    elf) == S_ERROR)
2519		return (S_ERROR);
2520
2521	/*
2522	 * Reset the name since the shdr->sh_name could have been changed as
2523	 * part of ld_sup_input_section().
2524	 */
2525	if ((name = elf_strptr(elf, (size_t)sndx, (size_t)shdr->sh_name)) ==
2526	    NULL) {
2527		eprintf(ofl->ofl_lml, ERR_ELF, MSG_INTL(MSG_ELF_STRPTR),
2528		    ifl->ifl_name);
2529		ofl->ofl_flags |= FLG_OF_FATAL;
2530		return (0);
2531	}
2532
2533	error = process_strtab(name, ifl, shdr, scn, sndx, FALSE, ofl);
2534	if ((error == 0) || (error == S_ERROR))
2535		return (error);
2536	str = ifl->ifl_isdesc[sndx]->is_indata->d_buf;
2537
2538	/*
2539	 * Determine the state table column from the input file type.  Note,
2540	 * shared library sections are not added to the output section list.
2541	 */
2542	if (ifl->ifl_ehdr->e_type == ET_DYN) {
2543		column = 1;
2544		ofl->ofl_soscnt++;
2545		ident = ld_targ.t_id.id_null;
2546	} else {
2547		column = 0;
2548		ofl->ofl_objscnt++;
2549		ident = ld_targ.t_id.id_unknown;
2550	}
2551
2552	DBG_CALL(Dbg_file_generic(ofl->ofl_lml, ifl));
2553	ndx = 0;
2554	vdfisp = vndisp = vsyisp = sifisp = capinfoisp = capisp = NULL;
2555	scn = NULL;
2556	while (scn = elf_nextscn(elf, scn)) {
2557		ndx++;
2558
2559		/*
2560		 * As we've already processed the .shstrtab don't do it again.
2561		 */
2562		if (ndx == sndx)
2563			continue;
2564
2565		if ((shdr = elf_getshdr(scn)) == NULL) {
2566			eprintf(ofl->ofl_lml, ERR_ELF,
2567			    MSG_INTL(MSG_ELF_GETSHDR), ifl->ifl_name);
2568			ofl->ofl_flags |= FLG_OF_FATAL;
2569			return (0);
2570		}
2571		name = str + (size_t)(shdr->sh_name);
2572
2573		if (ld_sup_input_section(ofl, ifl, name, &shdr, ndx, scn,
2574		    elf) == S_ERROR)
2575			return (S_ERROR);
2576
2577		/*
2578		 * Reset the name since the shdr->sh_name could have been
2579		 * changed as part of ld_sup_input_section().
2580		 */
2581		name = str + (size_t)(shdr->sh_name);
2582
2583		row = shdr->sh_type;
2584
2585		/*
2586		 * If the section has the SHF_EXCLUDE flag on, and we're not
2587		 * generating a relocatable object, exclude the section.
2588		 */
2589		if (((shdr->sh_flags & SHF_EXCLUDE) != 0) &&
2590		    ((ofl->ofl_flags & FLG_OF_RELOBJ) == 0)) {
2591			if ((error = process_exclude(name, ifl, shdr, scn,
2592			    ndx, ofl)) == S_ERROR)
2593				return (S_ERROR);
2594			if (error == 1)
2595				continue;
2596		}
2597
2598		/*
2599		 * If this is a standard section type process it via the
2600		 * appropriate action routine.
2601		 */
2602		if (row < SHT_NUM) {
2603			if (Initial[row][column] != NULL) {
2604				if (Initial[row][column](name, ifl, shdr, scn,
2605				    ndx, ident, ofl) == S_ERROR)
2606					return (S_ERROR);
2607			}
2608		} else {
2609			/*
2610			 * If this section is below SHT_LOSUNW then we don't
2611			 * really know what to do with it, issue a warning
2612			 * message but do the basic section processing anyway.
2613			 */
2614			if (row < (Word)SHT_LOSUNW) {
2615				Conv_inv_buf_t inv_buf;
2616
2617				eprintf(ofl->ofl_lml, ERR_WARNING,
2618				    MSG_INTL(MSG_FIL_INVALSEC), ifl->ifl_name,
2619				    EC_WORD(ndx), name, conv_sec_type(
2620				    ifl->ifl_ehdr->e_ident[EI_OSABI],
2621				    ifl->ifl_ehdr->e_machine,
2622				    shdr->sh_type, 0, &inv_buf));
2623			}
2624
2625			/*
2626			 * Handle sections greater than SHT_LOSUNW.
2627			 */
2628			switch (row) {
2629			case SHT_SUNW_dof:
2630				if (process_section(name, ifl, shdr, scn,
2631				    ndx, ident, ofl) == S_ERROR)
2632					return (S_ERROR);
2633				break;
2634			case SHT_SUNW_cap:
2635				if (process_section(name, ifl, shdr, scn, ndx,
2636				    ld_targ.t_id.id_null, ofl) == S_ERROR)
2637					return (S_ERROR);
2638				capisp = ifl->ifl_isdesc[ndx];
2639				break;
2640			case SHT_SUNW_capinfo:
2641				if (process_section(name, ifl, shdr, scn, ndx,
2642				    ld_targ.t_id.id_null, ofl) == S_ERROR)
2643					return (S_ERROR);
2644				capinfoisp = ifl->ifl_isdesc[ndx];
2645				break;
2646			case SHT_SUNW_DEBUGSTR:
2647			case SHT_SUNW_DEBUG:
2648				if (process_debug(name, ifl, shdr, scn,
2649				    ndx, ident, ofl) == S_ERROR)
2650					return (S_ERROR);
2651				break;
2652			case SHT_SUNW_move:
2653				if (process_section(name, ifl, shdr, scn, ndx,
2654				    ld_targ.t_id.id_null, ofl) == S_ERROR)
2655					return (S_ERROR);
2656				break;
2657			case SHT_SUNW_syminfo:
2658				if (process_section(name, ifl, shdr, scn, ndx,
2659				    ld_targ.t_id.id_null, ofl) == S_ERROR)
2660					return (S_ERROR);
2661				sifisp = ifl->ifl_isdesc[ndx];
2662				break;
2663			case SHT_SUNW_ANNOTATE:
2664				if (process_progbits(name, ifl, shdr, scn,
2665				    ndx, ident, ofl) == S_ERROR)
2666					return (S_ERROR);
2667				break;
2668			case SHT_SUNW_COMDAT:
2669				if (process_progbits(name, ifl, shdr, scn,
2670				    ndx, ident, ofl) == S_ERROR)
2671					return (S_ERROR);
2672				ifl->ifl_isdesc[ndx]->is_flags |= FLG_IS_COMDAT;
2673				break;
2674			case SHT_SUNW_verdef:
2675				if (process_section(name, ifl, shdr, scn, ndx,
2676				    ld_targ.t_id.id_null, ofl) == S_ERROR)
2677					return (S_ERROR);
2678				vdfisp = ifl->ifl_isdesc[ndx];
2679				break;
2680			case SHT_SUNW_verneed:
2681				if (process_section(name, ifl, shdr, scn, ndx,
2682				    ld_targ.t_id.id_null, ofl) == S_ERROR)
2683					return (S_ERROR);
2684				vndisp = ifl->ifl_isdesc[ndx];
2685				break;
2686			case SHT_SUNW_versym:
2687				if (process_section(name, ifl, shdr, scn, ndx,
2688				    ld_targ.t_id.id_null, ofl) == S_ERROR)
2689					return (S_ERROR);
2690				vsyisp = ifl->ifl_isdesc[ndx];
2691				break;
2692			case SHT_SPARC_GOTDATA:
2693				/*
2694				 * SHT_SPARC_GOTDATA (0x70000000) is in the
2695				 * SHT_LOPROC - SHT_HIPROC range reserved
2696				 * for processor-specific semantics. It is
2697				 * only meaningful for sparc targets.
2698				 */
2699				if (ld_targ.t_m.m_mach !=
2700				    LD_TARG_BYCLASS(EM_SPARC, EM_SPARCV9))
2701					goto do_default;
2702				if (process_section(name, ifl, shdr, scn, ndx,
2703				    ld_targ.t_id.id_gotdata, ofl) == S_ERROR)
2704					return (S_ERROR);
2705				break;
2706#if	defined(_ELF64)
2707			case SHT_AMD64_UNWIND:
2708				/*
2709				 * SHT_AMD64_UNWIND (0x70000001) is in the
2710				 * SHT_LOPROC - SHT_HIPROC range reserved
2711				 * for processor-specific semantics. It is
2712				 * only meaningful for amd64 targets.
2713				 */
2714				if (ld_targ.t_m.m_mach != EM_AMD64)
2715					goto do_default;
2716
2717				/*
2718				 * Target is x86, so this really is
2719				 * SHT_AMD64_UNWIND
2720				 */
2721				if (column == 0) {
2722					/*
2723					 * column == ET_REL
2724					 */
2725					if (process_section(name, ifl, shdr,
2726					    scn, ndx, ld_targ.t_id.id_unwind,
2727					    ofl) == S_ERROR)
2728						return (S_ERROR);
2729					ifl->ifl_isdesc[ndx]->is_flags |=
2730					    FLG_IS_EHFRAME;
2731				}
2732				break;
2733#endif
2734			default:
2735			do_default:
2736				if (process_section(name, ifl, shdr, scn, ndx,
2737				    ((ident == ld_targ.t_id.id_null) ?
2738				    ident : ld_targ.t_id.id_user), ofl) ==
2739				    S_ERROR)
2740					return (S_ERROR);
2741				break;
2742			}
2743		}
2744	}
2745
2746	/*
2747	 * Now that all input sections have been analyzed, and prior to placing
2748	 * any input sections to their output sections, process any groups.
2749	 * Groups can contribute COMDAT items, which may get discarded as part
2750	 * of placement.  In addition, COMDAT names may require transformation
2751	 * to indicate different output section placement.
2752	 */
2753	if (ifl->ifl_flags & FLG_IS_GROUPS) {
2754		for (ndx = 1; ndx < ifl->ifl_shnum; ndx++) {
2755			Is_desc	*isp;
2756
2757			if (((isp = ifl->ifl_isdesc[ndx]) == NULL) ||
2758			    (isp->is_shdr->sh_type != SHT_GROUP))
2759				continue;
2760
2761			if (ld_group_process(isp, ofl) == S_ERROR)
2762				return (S_ERROR);
2763		}
2764	}
2765
2766	/*
2767	 * Now that all of the input sections have been processed, place
2768	 * them in the appropriate output sections.
2769	 */
2770	for (ndx = 1; ndx < ifl->ifl_shnum; ndx++) {
2771		Is_desc	*isp;
2772
2773		if (((isp = ifl->ifl_isdesc[ndx]) == NULL) ||
2774		    ((isp->is_flags & FLG_IS_PLACE) == 0))
2775			continue;
2776
2777		/*
2778		 * Place all non-ordered sections within their appropriate
2779		 * output section.
2780		 */
2781		if ((isp->is_flags & FLG_IS_ORDERED) == 0) {
2782			if (ld_place_section(ofl, isp, path_info,
2783			    isp->is_keyident, NULL) == (Os_desc *)S_ERROR)
2784				return (S_ERROR);
2785			continue;
2786		}
2787
2788		/*
2789		 * Count the number of ordered sections and retain the first
2790		 * ordered section index. This will be used to optimize the
2791		 * ordered section loop that immediately follows this one.
2792		 */
2793		ordcnt++;
2794		if (ordndx == 0)
2795			ordndx = ndx;
2796	}
2797
2798	/*
2799	 * Having placed all the non-ordered sections, it is now
2800	 * safe to place SHF_ORDERED/SHF_LINK_ORDER sections.
2801	 */
2802	if (ifl->ifl_flags & FLG_IF_ORDERED) {
2803		for (ndx = ordndx; ndx < ifl->ifl_shnum; ndx++) {
2804			Is_desc	*isp;
2805
2806			if (((isp = ifl->ifl_isdesc[ndx]) == NULL) ||
2807			    ((isp->is_flags &
2808			    (FLG_IS_PLACE | FLG_IS_ORDERED)) !=
2809			    (FLG_IS_PLACE | FLG_IS_ORDERED)))
2810				continue;
2811
2812			/* ld_process_ordered() calls ld_place_section() */
2813			if (ld_process_ordered(ofl, ifl, path_info, ndx) ==
2814			    S_ERROR)
2815				return (S_ERROR);
2816
2817			/* If we've done them all, stop searching */
2818			if (--ordcnt == 0)
2819				break;
2820		}
2821	}
2822
2823	/*
2824	 * If this is a shared object explicitly specified on the command
2825	 * line (as opposed to being a dependency of such an object),
2826	 * determine if the user has specified a control definition. This
2827	 * descriptor may specify which version definitions can be used
2828	 * from this object. It may also update the dependency to USED and
2829	 * supply an alternative SONAME.
2830	 */
2831	sdf = NULL;
2832	if (column && (ifl->ifl_flags & FLG_IF_NEEDED)) {
2833		const char	*base;
2834
2835		/*
2836		 * Use the basename of the input file (typically this is the
2837		 * compilation environment name, ie. libfoo.so).
2838		 */
2839		if ((base = strrchr(ifl->ifl_name, '/')) == NULL)
2840			base = ifl->ifl_name;
2841		else
2842			base++;
2843
2844		if ((sdf = sdf_find(base, ofl->ofl_socntl)) != NULL) {
2845			sdf->sdf_file = ifl;
2846			ifl->ifl_sdfdesc = sdf;
2847		}
2848	}
2849
2850	/*
2851	 * Before symbol processing, process any capabilities.  Capabilities
2852	 * can reference a string table, which is why this processing is
2853	 * carried out after the initial section processing.  Capabilities,
2854	 * together with -z symbolcap, can require the conversion of global
2855	 * symbols to local symbols.
2856	 */
2857	if (capisp && (process_cap(ofl, ifl, capisp) == S_ERROR))
2858		return (S_ERROR);
2859
2860	/*
2861	 * Process any version dependencies.  These will establish shared object
2862	 * `needed' entries in the same manner as will be generated from the
2863	 * .dynamic's NEEDED entries.
2864	 */
2865	if (vndisp && (ofl->ofl_flags & (FLG_OF_NOUNDEF | FLG_OF_SYMBOLIC)))
2866		if (ld_vers_need_process(vndisp, ifl, ofl) == S_ERROR)
2867			return (S_ERROR);
2868
2869	/*
2870	 * Before processing any symbol resolution or relocations process any
2871	 * version sections.
2872	 */
2873	if (vsyisp)
2874		(void) ld_vers_sym_process(ofl->ofl_lml, vsyisp, ifl);
2875
2876	if (ifl->ifl_versym &&
2877	    (vdfisp || (sdf && (sdf->sdf_flags & FLG_SDF_SELECT))))
2878		if (ld_vers_def_process(vdfisp, ifl, ofl) == S_ERROR)
2879			return (S_ERROR);
2880
2881	/*
2882	 * Having collected the appropriate sections carry out any additional
2883	 * processing if necessary.
2884	 */
2885	for (ndx = 0; ndx < ifl->ifl_shnum; ndx++) {
2886		Is_desc	*isp;
2887
2888		if ((isp = ifl->ifl_isdesc[ndx]) == NULL)
2889			continue;
2890		row = isp->is_shdr->sh_type;
2891
2892		if ((isp->is_flags & FLG_IS_DISCARD) == 0)
2893			ld_sup_section(ofl, isp->is_name, isp->is_shdr, ndx,
2894			    isp->is_indata, elf);
2895
2896		/*
2897		 * If this is a SHT_SUNW_move section from a relocatable file,
2898		 * keep track of the section for later processing.
2899		 */
2900		if ((row == SHT_SUNW_move) && (column == 0)) {
2901			if (aplist_append(&(ofl->ofl_ismove), isp,
2902			    AL_CNT_OFL_MOVE) == NULL)
2903				return (S_ERROR);
2904		}
2905
2906		/*
2907		 * If this is a standard section type process it via the
2908		 * appropriate action routine.
2909		 */
2910		if (row < SHT_NUM) {
2911			if (Final[row][column] != NULL) {
2912				if (Final[row][column](isp, ifl,
2913				    ofl) == S_ERROR)
2914					return (S_ERROR);
2915			}
2916#if	defined(_ELF64)
2917		} else if ((row == SHT_AMD64_UNWIND) && (column == 0)) {
2918			Os_desc	*osp = isp->is_osdesc;
2919
2920			/*
2921			 * SHT_AMD64_UNWIND (0x70000001) is in the SHT_LOPROC -
2922			 * SHT_HIPROC range reserved for processor-specific
2923			 * semantics, and is only meaningful for amd64 targets.
2924			 *
2925			 * Only process unwind contents from relocatable
2926			 * objects.
2927			 */
2928			if (osp && (ld_targ.t_m.m_mach == EM_AMD64) &&
2929			    (ld_unwind_register(osp, ofl) == S_ERROR))
2930				return (S_ERROR);
2931#endif
2932		}
2933	}
2934
2935	/*
2936	 * Following symbol processing, if this relocatable object input file
2937	 * provides symbol capabilities, tag the associated symbols so that
2938	 * the symbols can be re-assigned to the new capabilities symbol
2939	 * section that will be created for the output file.
2940	 */
2941	if (capinfoisp && (ifl->ifl_ehdr->e_type == ET_REL) &&
2942	    (process_capinfo(ofl, ifl, capinfoisp) == S_ERROR))
2943		return (S_ERROR);
2944
2945	/*
2946	 * After processing any symbol resolution, and if this dependency
2947	 * indicates it contains symbols that can't be directly bound to,
2948	 * set the symbols appropriately.
2949	 */
2950	if (sifisp && ((ifl->ifl_flags & (FLG_IF_NEEDED | FLG_IF_NODIRECT)) ==
2951	    (FLG_IF_NEEDED | FLG_IF_NODIRECT)))
2952		(void) ld_sym_nodirect(sifisp, ifl, ofl);
2953
2954	return (1);
2955}
2956
2957/*
2958 * Process the current input file.  There are basically three types of files
2959 * that come through here:
2960 *
2961 *  -	files explicitly defined on the command line (ie. foo.o or bar.so),
2962 *	in this case only the `name' field is valid.
2963 *
2964 *  -	libraries determined from the -l command line option (ie. -lbar),
2965 *	in this case the `soname' field contains the basename of the located
2966 *	file.
2967 *
2968 * Any shared object specified via the above two conventions must be recorded
2969 * as a needed dependency.
2970 *
2971 *  -	libraries specified as dependencies of those libraries already obtained
2972 *	via the command line (ie. bar.so has a DT_NEEDED entry of fred.so.1),
2973 *	in this case the `soname' field contains either a full pathname (if the
2974 *	needed entry contained a `/'), or the basename of the located file.
2975 *	These libraries are processed to verify symbol binding but are not
2976 *	recorded as dependencies of the output file being generated.
2977 */
2978Ifl_desc *
2979ld_process_ifl(const char *name, const char *soname, int fd, Elf *elf,
2980    Word flags, Ofl_desc *ofl, Rej_desc *rej)
2981{
2982	Ifl_desc	*ifl;
2983	Ehdr		*ehdr;
2984	uintptr_t	error = 0;
2985	struct stat	status;
2986	Ar_desc		*adp;
2987	Rej_desc	_rej;
2988
2989	/*
2990	 * If this file was not extracted from an archive obtain its device
2991	 * information.  This will be used to determine if the file has already
2992	 * been processed (rather than simply comparing filenames, the device
2993	 * information provides a quicker comparison and detects linked files).
2994	 */
2995	if (fd && ((flags & FLG_IF_EXTRACT) == 0))
2996		(void) fstat(fd, &status);
2997	else {
2998		status.st_dev = 0;
2999		status.st_ino = 0;
3000	}
3001
3002	switch (elf_kind(elf)) {
3003	case ELF_K_AR:
3004		/*
3005		 * Determine if we've already come across this archive file.
3006		 */
3007		if (!(flags & FLG_IF_EXTRACT)) {
3008			Aliste	idx;
3009
3010			for (APLIST_TRAVERSE(ofl->ofl_ars, idx, adp)) {
3011				if ((adp->ad_stdev != status.st_dev) ||
3012				    (adp->ad_stino != status.st_ino))
3013					continue;
3014
3015				/*
3016				 * We've seen this file before so reuse the
3017				 * original archive descriptor and discard the
3018				 * new elf descriptor.  Note that a file
3019				 * descriptor is unnecessary, as the file is
3020				 * already available in memory.
3021				 */
3022				DBG_CALL(Dbg_file_reuse(ofl->ofl_lml, name,
3023				    adp->ad_name));
3024				(void) elf_end(elf);
3025				return ((Ifl_desc *)ld_process_archive(name, -1,
3026				    adp, ofl));
3027			}
3028		}
3029
3030		/*
3031		 * As we haven't processed this file before establish a new
3032		 * archive descriptor.
3033		 */
3034		adp = ld_ar_setup(name, elf, ofl);
3035		if ((adp == NULL) || (adp == (Ar_desc *)S_ERROR))
3036			return ((Ifl_desc *)adp);
3037		adp->ad_stdev = status.st_dev;
3038		adp->ad_stino = status.st_ino;
3039
3040		ld_sup_file(ofl, name, ELF_K_AR, flags, elf);
3041
3042		/*
3043		 * Indicate that the ELF descriptor no longer requires a file
3044		 * descriptor by reading the entire file.  The file is already
3045		 * read via the initial mmap(2) behind elf_begin(3elf), thus
3046		 * this operation is effectively a no-op.  However, a side-
3047		 * effect is that the internal file descriptor, maintained in
3048		 * the ELF descriptor, is set to -1.  This setting will not
3049		 * be compared with any file descriptor that is passed to
3050		 * elf_begin(), should this archive, or one of the archive
3051		 * members, be processed again from the command line or
3052		 * because of a -z rescan.
3053		 */
3054		if (elf_cntl(elf, ELF_C_FDREAD) == -1) {
3055			eprintf(ofl->ofl_lml, ERR_ELF, MSG_INTL(MSG_ELF_CNTL),
3056			    name);
3057			ofl->ofl_flags |= FLG_OF_FATAL;
3058			return (NULL);
3059		}
3060
3061		return ((Ifl_desc *)ld_process_archive(name, -1, adp, ofl));
3062
3063	case ELF_K_ELF:
3064		/*
3065		 * Obtain the elf header so that we can determine what type of
3066		 * elf ELF_K_ELF file this is.
3067		 */
3068		if ((ehdr = elf_getehdr(elf)) == NULL) {
3069			int	_class = gelf_getclass(elf);
3070
3071			/*
3072			 * Failure could occur for a number of reasons at this
3073			 * point.  Typically the files class is incorrect (ie.
3074			 * user is building 64-bit but managed to pint at 32-bit
3075			 * libraries).  However any number of elf errors can
3076			 * also occur, such as from a truncated or corrupt file.
3077			 * Here we try and get the best error message possible.
3078			 */
3079			if (ld_targ.t_m.m_class != _class) {
3080				_rej.rej_type = SGS_REJ_CLASS;
3081				_rej.rej_info = (uint_t)_class;
3082			} else {
3083				_rej.rej_type = SGS_REJ_STR;
3084				_rej.rej_str = elf_errmsg(-1);
3085			}
3086			_rej.rej_name = name;
3087			DBG_CALL(Dbg_file_rejected(ofl->ofl_lml, &_rej,
3088			    ld_targ.t_m.m_mach));
3089			if (rej->rej_type == 0) {
3090				*rej = _rej;
3091				rej->rej_name = strdup(_rej.rej_name);
3092			}
3093			return (NULL);
3094		}
3095
3096		/*
3097		 * Determine if we've already come across this file.
3098		 */
3099		if (!(flags & FLG_IF_EXTRACT)) {
3100			APlist	*apl;
3101			Aliste	idx;
3102
3103			if (ehdr->e_type == ET_REL)
3104				apl = ofl->ofl_objs;
3105			else
3106				apl = ofl->ofl_sos;
3107
3108			/*
3109			 * Traverse the appropriate file list and determine if
3110			 * a dev/inode match is found.
3111			 */
3112			for (APLIST_TRAVERSE(apl, idx, ifl)) {
3113				/*
3114				 * Ifl_desc generated via -Nneed, therefore no
3115				 * actual file behind it.
3116				 */
3117				if (ifl->ifl_flags & FLG_IF_NEEDSTR)
3118					continue;
3119
3120				if ((ifl->ifl_stino != status.st_ino) ||
3121				    (ifl->ifl_stdev != status.st_dev))
3122					continue;
3123
3124				/*
3125				 * Disregard (skip) this image.
3126				 */
3127				DBG_CALL(Dbg_file_skip(ofl->ofl_lml,
3128				    ifl->ifl_name, name));
3129				(void) elf_end(elf);
3130
3131				/*
3132				 * If the file was explicitly defined on the
3133				 * command line (this is always the case for
3134				 * relocatable objects, and is true for shared
3135				 * objects when they weren't specified via -l or
3136				 * were dragged in as an implicit dependency),
3137				 * then warn the user.
3138				 */
3139				if ((flags & FLG_IF_CMDLINE) ||
3140				    (ifl->ifl_flags & FLG_IF_CMDLINE)) {
3141					const char	*errmsg;
3142
3143					/*
3144					 * Determine whether this is the same
3145					 * file name as originally encountered
3146					 * so as to provide the most
3147					 * descriptive diagnostic.
3148					 */
3149					errmsg =
3150					    (strcmp(name, ifl->ifl_name) == 0) ?
3151					    MSG_INTL(MSG_FIL_MULINC_1) :
3152					    MSG_INTL(MSG_FIL_MULINC_2);
3153					eprintf(ofl->ofl_lml, ERR_WARNING,
3154					    errmsg, name, ifl->ifl_name);
3155				}
3156				return (ifl);
3157			}
3158		}
3159
3160		/*
3161		 * At this point, we know we need the file.  Establish an input
3162		 * file descriptor and continue processing.
3163		 */
3164		ifl = ifl_setup(name, ehdr, elf, flags, ofl, rej);
3165		if ((ifl == NULL) || (ifl == (Ifl_desc *)S_ERROR))
3166			return (ifl);
3167		ifl->ifl_stdev = status.st_dev;
3168		ifl->ifl_stino = status.st_ino;
3169
3170		/*
3171		 * If -zignore is in effect, mark this file as a potential
3172		 * candidate (the files use isn't actually determined until
3173		 * symbol resolution and relocation processing are completed).
3174		 */
3175		if (ofl->ofl_flags1 & FLG_OF1_IGNORE)
3176			ifl->ifl_flags |= FLG_IF_IGNORE;
3177
3178		switch (ehdr->e_type) {
3179		case ET_REL:
3180			(*ld_targ.t_mr.mr_mach_eflags)(ehdr, ofl);
3181			error = process_elf(ifl, elf, ofl);
3182			break;
3183		case ET_DYN:
3184			if ((ofl->ofl_flags & FLG_OF_STATIC) ||
3185			    !(ofl->ofl_flags & FLG_OF_DYNLIBS)) {
3186				eprintf(ofl->ofl_lml, ERR_FATAL,
3187				    MSG_INTL(MSG_FIL_SOINSTAT), name);
3188				ofl->ofl_flags |= FLG_OF_FATAL;
3189				return (NULL);
3190			}
3191
3192			/*
3193			 * Record any additional shared object information.
3194			 * If no soname is specified (eg. this file was
3195			 * derived from a explicit filename declaration on the
3196			 * command line, ie. bar.so) use the pathname.
3197			 * This entry may be overridden if the files dynamic
3198			 * section specifies an DT_SONAME value.
3199			 */
3200			if (soname == NULL)
3201				ifl->ifl_soname = ifl->ifl_name;
3202			else
3203				ifl->ifl_soname = soname;
3204
3205			/*
3206			 * If direct bindings, lazy loading, or group
3207			 * permissions need to be established, mark this object.
3208			 */
3209			if (ofl->ofl_flags1 & FLG_OF1_ZDIRECT)
3210				ifl->ifl_flags |= FLG_IF_DIRECT;
3211			if (ofl->ofl_flags1 & FLG_OF1_LAZYLD)
3212				ifl->ifl_flags |= FLG_IF_LAZYLD;
3213			if (ofl->ofl_flags1 & FLG_OF1_GRPPRM)
3214				ifl->ifl_flags |= FLG_IF_GRPPRM;
3215			error = process_elf(ifl, elf, ofl);
3216
3217			/*
3218			 * At this point we know if this file will be
3219			 * lazyloaded, or whether bindings to it must be direct.
3220			 * In either case, a syminfo section is required.
3221			 */
3222			if (ifl->ifl_flags & (FLG_IF_LAZYLD | FLG_IF_DIRECT))
3223				ofl->ofl_flags |= FLG_OF_SYMINFO;
3224
3225			break;
3226		default:
3227			(void) elf_errno();
3228			_rej.rej_type = SGS_REJ_UNKFILE;
3229			_rej.rej_name = name;
3230			DBG_CALL(Dbg_file_rejected(ofl->ofl_lml, &_rej,
3231			    ld_targ.t_m.m_mach));
3232			if (rej->rej_type == 0) {
3233				*rej = _rej;
3234				rej->rej_name = strdup(_rej.rej_name);
3235			}
3236			return (NULL);
3237		}
3238		break;
3239	default:
3240		(void) elf_errno();
3241		_rej.rej_type = SGS_REJ_UNKFILE;
3242		_rej.rej_name = name;
3243		DBG_CALL(Dbg_file_rejected(ofl->ofl_lml, &_rej,
3244		    ld_targ.t_m.m_mach));
3245		if (rej->rej_type == 0) {
3246			*rej = _rej;
3247			rej->rej_name = strdup(_rej.rej_name);
3248		}
3249		return (NULL);
3250	}
3251	if ((error == 0) || (error == S_ERROR))
3252		return ((Ifl_desc *)error);
3253	else
3254		return (ifl);
3255}
3256
3257/*
3258 * Having successfully opened a file, set up the necessary elf structures to
3259 * process it further.  This small section of processing is slightly different
3260 * from the elf initialization required to process a relocatable object from an
3261 * archive (see libs.c: ld_process_archive()).
3262 */
3263Ifl_desc *
3264ld_process_open(const char *opath, const char *ofile, int *fd, Ofl_desc *ofl,
3265    Word flags, Rej_desc *rej)
3266{
3267	Elf		*elf;
3268	const char	*npath = opath;
3269	const char	*nfile = ofile;
3270
3271	if ((elf = elf_begin(*fd, ELF_C_READ, NULL)) == NULL) {
3272		eprintf(ofl->ofl_lml, ERR_ELF, MSG_INTL(MSG_ELF_BEGIN), npath);
3273		ofl->ofl_flags |= FLG_OF_FATAL;
3274		return (NULL);
3275	}
3276
3277	/*
3278	 * Determine whether the support library wishes to process this open.
3279	 * The support library may return:
3280	 *   .	a different ELF descriptor (in which case they should have
3281	 *	closed the original)
3282	 *   .	a different file descriptor (in which case they should have
3283	 *	closed the original)
3284	 *   .	a different path and file name (presumably associated with
3285	 *	a different file descriptor)
3286	 *
3287	 * A file descriptor of -1, or and ELF descriptor of zero indicates
3288	 * the file should be ignored.
3289	 */
3290	ld_sup_open(ofl, &npath, &nfile, fd, flags, &elf, NULL, 0,
3291	    elf_kind(elf));
3292
3293	if ((*fd == -1) || (elf == NULL))
3294		return (NULL);
3295
3296	return (ld_process_ifl(npath, nfile, *fd, elf, flags, ofl, rej));
3297}
3298
3299/*
3300 * Having successfully mapped a file, set up the necessary elf structures to
3301 * process it further.  This routine is patterned after ld_process_open() and
3302 * is only called by ld.so.1(1) to process a relocatable object.
3303 */
3304Ifl_desc *
3305ld_process_mem(const char *path, const char *file, char *addr, size_t size,
3306    Ofl_desc *ofl, Rej_desc *rej)
3307{
3308	Elf	*elf;
3309
3310	if ((elf = elf_memory(addr, size)) == NULL) {
3311		eprintf(ofl->ofl_lml, ERR_ELF, MSG_INTL(MSG_ELF_MEMORY), path);
3312		ofl->ofl_flags |= FLG_OF_FATAL;
3313		return (0);
3314	}
3315
3316	return (ld_process_ifl(path, file, 0, elf, 0, ofl, rej));
3317}
3318
3319/*
3320 * Process a required library (i.e. the dependency of a shared object).
3321 * Combine the directory and filename, check the resultant path size, and try
3322 * opening the pathname.
3323 */
3324static Ifl_desc *
3325process_req_lib(Sdf_desc *sdf, const char *dir, const char *file,
3326    Ofl_desc *ofl, Rej_desc *rej)
3327{
3328	size_t		dlen, plen;
3329	int		fd;
3330	char		path[PATH_MAX];
3331	const char	*_dir = dir;
3332
3333	/*
3334	 * Determine the sizes of the directory and filename to insure we don't
3335	 * exceed our buffer.
3336	 */
3337	if ((dlen = strlen(dir)) == 0) {
3338		_dir = MSG_ORIG(MSG_STR_DOT);
3339		dlen = 1;
3340	}
3341	dlen++;
3342	plen = dlen + strlen(file) + 1;
3343	if (plen > PATH_MAX) {
3344		eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_FIL_PTHTOLONG),
3345		    _dir, file);
3346		ofl->ofl_flags |= FLG_OF_FATAL;
3347		return (0);
3348	}
3349
3350	/*
3351	 * Build the entire pathname and try and open the file.
3352	 */
3353	(void) strcpy(path, _dir);
3354	(void) strcat(path, MSG_ORIG(MSG_STR_SLASH));
3355	(void) strcat(path, file);
3356	DBG_CALL(Dbg_libs_req(ofl->ofl_lml, sdf->sdf_name,
3357	    sdf->sdf_rfile, path));
3358
3359	if ((fd = open(path, O_RDONLY)) == -1)
3360		return (0);
3361	else {
3362		Ifl_desc	*ifl;
3363		char		*_path;
3364
3365		if ((_path = libld_malloc(strlen(path) + 1)) == NULL)
3366			return ((Ifl_desc *)S_ERROR);
3367		(void) strcpy(_path, path);
3368		ifl = ld_process_open(_path, &_path[dlen], &fd, ofl, 0, rej);
3369		if (fd != -1)
3370			(void) close(fd);
3371		return (ifl);
3372	}
3373}
3374
3375/*
3376 * Finish any library processing.  Walk the list of so's that have been listed
3377 * as "included" by shared objects we have previously processed.  Examine them,
3378 * without adding them as explicit dependents of this program, in order to
3379 * complete our symbol definition process.  The search path rules are:
3380 *
3381 *  -	use any user supplied paths, i.e. LD_LIBRARY_PATH and -L, then
3382 *
3383 *  -	use any RPATH defined within the parent shared object, then
3384 *
3385 *  -	use the default directories, i.e. LIBPATH or -YP.
3386 */
3387uintptr_t
3388ld_finish_libs(Ofl_desc *ofl)
3389{
3390	Aliste		idx1;
3391	Sdf_desc	*sdf;
3392	Rej_desc	rej = { 0 };
3393
3394	/*
3395	 * Make sure we are back in dynamic mode.
3396	 */
3397	ofl->ofl_flags |= FLG_OF_DYNLIBS;
3398
3399	for (APLIST_TRAVERSE(ofl->ofl_soneed, idx1, sdf)) {
3400		Aliste		idx2;
3401		char		*path, *slash = NULL;
3402		int		fd;
3403		Ifl_desc	*ifl;
3404		char		*file = (char *)sdf->sdf_name;
3405
3406		/*
3407		 * See if this file has already been processed.  At the time
3408		 * this implicit dependency was determined there may still have
3409		 * been more explicit dependencies to process.  Note, if we ever
3410		 * do parse the command line three times we would be able to
3411		 * do all this checking when processing the dynamic section.
3412		 */
3413		if (sdf->sdf_file)
3414			continue;
3415
3416		for (APLIST_TRAVERSE(ofl->ofl_sos, idx2, ifl)) {
3417			if (!(ifl->ifl_flags & FLG_IF_NEEDSTR) &&
3418			    (strcmp(file, ifl->ifl_soname) == 0)) {
3419				sdf->sdf_file = ifl;
3420				break;
3421			}
3422		}
3423		if (sdf->sdf_file)
3424			continue;
3425
3426		/*
3427		 * If the current path name element embeds a "/", then it's to
3428		 * be taken "as is", with no searching involved.  Process all
3429		 * "/" occurrences, so that we can deduce the base file name.
3430		 */
3431		for (path = file; *path; path++) {
3432			if (*path == '/')
3433				slash = path;
3434		}
3435		if (slash) {
3436			DBG_CALL(Dbg_libs_req(ofl->ofl_lml, sdf->sdf_name,
3437			    sdf->sdf_rfile, file));
3438			if ((fd = open(file, O_RDONLY)) == -1) {
3439				eprintf(ofl->ofl_lml, ERR_WARNING,
3440				    MSG_INTL(MSG_FIL_NOTFOUND), file,
3441				    sdf->sdf_rfile);
3442			} else {
3443				Rej_desc	_rej = { 0 };
3444
3445				ifl = ld_process_open(file, ++slash, &fd, ofl,
3446				    0, &_rej);
3447				if (fd != -1)
3448					(void) close(fd);
3449				if (ifl == (Ifl_desc *)S_ERROR)
3450					return (S_ERROR);
3451
3452				if (_rej.rej_type) {
3453					Conv_reject_desc_buf_t rej_buf;
3454
3455					eprintf(ofl->ofl_lml, ERR_WARNING,
3456					    MSG_INTL(reject[_rej.rej_type]),
3457					    _rej.rej_name ? rej.rej_name :
3458					    MSG_INTL(MSG_STR_UNKNOWN),
3459					    conv_reject_desc(&_rej, &rej_buf,
3460					    ld_targ.t_m.m_mach));
3461				} else
3462					sdf->sdf_file = ifl;
3463			}
3464			continue;
3465		}
3466
3467		/*
3468		 * Now search for this file in any user defined directories.
3469		 */
3470		for (APLIST_TRAVERSE(ofl->ofl_ulibdirs, idx2, path)) {
3471			Rej_desc	_rej = { 0 };
3472
3473			ifl = process_req_lib(sdf, path, file, ofl, &_rej);
3474			if (ifl == (Ifl_desc *)S_ERROR) {
3475				return (S_ERROR);
3476			}
3477			if (_rej.rej_type) {
3478				if (rej.rej_type == 0) {
3479					rej = _rej;
3480					rej.rej_name = strdup(_rej.rej_name);
3481				}
3482			}
3483			if (ifl) {
3484				sdf->sdf_file = ifl;
3485				break;
3486			}
3487		}
3488		if (sdf->sdf_file)
3489			continue;
3490
3491		/*
3492		 * Next use the local rules defined within the parent shared
3493		 * object.
3494		 */
3495		if (sdf->sdf_rpath != NULL) {
3496			char	*rpath, *next;
3497
3498			rpath = libld_malloc(strlen(sdf->sdf_rpath) + 1);
3499			if (rpath == NULL)
3500				return (S_ERROR);
3501			(void) strcpy(rpath, sdf->sdf_rpath);
3502			DBG_CALL(Dbg_libs_path(ofl->ofl_lml, rpath,
3503			    LA_SER_RUNPATH, sdf->sdf_rfile));
3504			if ((path = strtok_r(rpath,
3505			    MSG_ORIG(MSG_STR_COLON), &next)) != NULL) {
3506				do {
3507					Rej_desc	_rej = { 0 };
3508
3509					path = expand(sdf->sdf_rfile, path,
3510					    &next);
3511
3512					ifl = process_req_lib(sdf, path,
3513					    file, ofl, &_rej);
3514					if (ifl == (Ifl_desc *)S_ERROR) {
3515						return (S_ERROR);
3516					}
3517					if ((_rej.rej_type) &&
3518					    (rej.rej_type == 0)) {
3519						rej = _rej;
3520						rej.rej_name =
3521						    strdup(_rej.rej_name);
3522					}
3523					if (ifl) {
3524						sdf->sdf_file = ifl;
3525						break;
3526					}
3527				} while ((path = strtok_r(NULL,
3528				    MSG_ORIG(MSG_STR_COLON), &next)) != NULL);
3529			}
3530		}
3531		if (sdf->sdf_file)
3532			continue;
3533
3534		/*
3535		 * Finally try the default library search directories.
3536		 */
3537		for (APLIST_TRAVERSE(ofl->ofl_dlibdirs, idx2, path)) {
3538			Rej_desc	_rej = { 0 };
3539
3540			ifl = process_req_lib(sdf, path, file, ofl, &rej);
3541			if (ifl == (Ifl_desc *)S_ERROR) {
3542				return (S_ERROR);
3543			}
3544			if (_rej.rej_type) {
3545				if (rej.rej_type == 0) {
3546					rej = _rej;
3547					rej.rej_name = strdup(_rej.rej_name);
3548				}
3549			}
3550			if (ifl) {
3551				sdf->sdf_file = ifl;
3552				break;
3553			}
3554		}
3555		if (sdf->sdf_file)
3556			continue;
3557
3558		/*
3559		 * If we've got this far we haven't found the shared object.
3560		 * If an object was found, but was rejected for some reason,
3561		 * print a diagnostic to that effect, otherwise generate a
3562		 * generic "not found" diagnostic.
3563		 */
3564		if (rej.rej_type) {
3565			Conv_reject_desc_buf_t rej_buf;
3566
3567			eprintf(ofl->ofl_lml, ERR_WARNING,
3568			    MSG_INTL(reject[rej.rej_type]),
3569			    rej.rej_name ? rej.rej_name :
3570			    MSG_INTL(MSG_STR_UNKNOWN),
3571			    conv_reject_desc(&rej, &rej_buf,
3572			    ld_targ.t_m.m_mach));
3573		} else {
3574			eprintf(ofl->ofl_lml, ERR_WARNING,
3575			    MSG_INTL(MSG_FIL_NOTFOUND), file, sdf->sdf_rfile);
3576		}
3577	}
3578
3579	/*
3580	 * Finally, now that all objects have been input, make sure any version
3581	 * requirements have been met.
3582	 */
3583	return (ld_vers_verify(ofl));
3584}
3585