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