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