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