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