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