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