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