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