xref: /titanic_41/usr/src/cmd/sgs/libld/common/files.c (revision f3312ec0e8acbd249df97358fb8c3ca92f4e089c)
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_IF_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_IF_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 group information has been processed, we can safely validate
2825 	 * that nothing is fishy about the section COMDAT description.  We
2826 	 * need to do this prior to placing the section (where any
2827 	 * SHT_SUNW_COMDAT sections will be restored to being PROGBITS)
2828 	 */
2829 	ld_comdat_validate(ofl, ifl);
2830 
2831 	/*
2832 	 * Now that all of the input sections have been processed, place
2833 	 * them in the appropriate output sections.
2834 	 */
2835 	for (ndx = 1; ndx < ifl->ifl_shnum; ndx++) {
2836 		Is_desc	*isp;
2837 
2838 		if (((isp = ifl->ifl_isdesc[ndx]) == NULL) ||
2839 		    ((isp->is_flags & FLG_IS_PLACE) == 0))
2840 			continue;
2841 
2842 		/*
2843 		 * Place all non-ordered sections within their appropriate
2844 		 * output section.
2845 		 */
2846 		if ((isp->is_flags & FLG_IS_ORDERED) == 0) {
2847 			if (ld_place_section(ofl, isp, path_info,
2848 			    isp->is_keyident, NULL) == (Os_desc *)S_ERROR)
2849 				return (S_ERROR);
2850 			continue;
2851 		}
2852 
2853 		/*
2854 		 * Count the number of ordered sections and retain the first
2855 		 * ordered section index. This will be used to optimize the
2856 		 * ordered section loop that immediately follows this one.
2857 		 */
2858 		ordcnt++;
2859 		if (ordndx == 0)
2860 			ordndx = ndx;
2861 	}
2862 
2863 	/*
2864 	 * Having placed all the non-ordered sections, it is now
2865 	 * safe to place SHF_ORDERED/SHF_LINK_ORDER sections.
2866 	 */
2867 	if (ifl->ifl_flags & FLG_IF_ORDERED) {
2868 		for (ndx = ordndx; ndx < ifl->ifl_shnum; ndx++) {
2869 			Is_desc	*isp;
2870 
2871 			if (((isp = ifl->ifl_isdesc[ndx]) == NULL) ||
2872 			    ((isp->is_flags &
2873 			    (FLG_IS_PLACE | FLG_IS_ORDERED)) !=
2874 			    (FLG_IS_PLACE | FLG_IS_ORDERED)))
2875 				continue;
2876 
2877 			/* ld_process_ordered() calls ld_place_section() */
2878 			if (ld_process_ordered(ofl, ifl, path_info, ndx) ==
2879 			    S_ERROR)
2880 				return (S_ERROR);
2881 
2882 			/* If we've done them all, stop searching */
2883 			if (--ordcnt == 0)
2884 				break;
2885 		}
2886 	}
2887 
2888 	/*
2889 	 * If this is a shared object explicitly specified on the command
2890 	 * line (as opposed to being a dependency of such an object),
2891 	 * determine if the user has specified a control definition. This
2892 	 * descriptor may specify which version definitions can be used
2893 	 * from this object. It may also update the dependency to USED and
2894 	 * supply an alternative SONAME.
2895 	 */
2896 	sdf = NULL;
2897 	if (column && (ifl->ifl_flags & FLG_IF_NEEDED)) {
2898 		const char	*base;
2899 
2900 		/*
2901 		 * Use the basename of the input file (typically this is the
2902 		 * compilation environment name, ie. libfoo.so).
2903 		 */
2904 		if ((base = strrchr(ifl->ifl_name, '/')) == NULL)
2905 			base = ifl->ifl_name;
2906 		else
2907 			base++;
2908 
2909 		if ((sdf = sdf_find(base, ofl->ofl_socntl)) != NULL) {
2910 			sdf->sdf_file = ifl;
2911 			ifl->ifl_sdfdesc = sdf;
2912 		}
2913 	}
2914 
2915 	/*
2916 	 * Before symbol processing, process any capabilities.  Capabilities
2917 	 * can reference a string table, which is why this processing is
2918 	 * carried out after the initial section processing.  Capabilities,
2919 	 * together with -z symbolcap, can require the conversion of global
2920 	 * symbols to local symbols.
2921 	 */
2922 	if (capisp && (process_cap(ofl, ifl, capisp) == S_ERROR))
2923 		return (S_ERROR);
2924 
2925 	/*
2926 	 * Process any version dependencies.  These will establish shared object
2927 	 * `needed' entries in the same manner as will be generated from the
2928 	 * .dynamic's NEEDED entries.
2929 	 */
2930 	if (vndisp && ((ofl->ofl_flags & (FLG_OF_NOUNDEF | FLG_OF_SYMBOLIC)) ||
2931 	    OFL_GUIDANCE(ofl, FLG_OFG_NO_DEFS)))
2932 		if (ld_vers_need_process(vndisp, ifl, ofl) == S_ERROR)
2933 			return (S_ERROR);
2934 
2935 	/*
2936 	 * Before processing any symbol resolution or relocations process any
2937 	 * version sections.
2938 	 */
2939 	if (vsyisp)
2940 		(void) ld_vers_sym_process(ofl, vsyisp, ifl);
2941 
2942 	if (ifl->ifl_versym &&
2943 	    (vdfisp || (sdf && (sdf->sdf_flags & FLG_SDF_SELECT))))
2944 		if (ld_vers_def_process(vdfisp, ifl, ofl) == S_ERROR)
2945 			return (S_ERROR);
2946 
2947 	/*
2948 	 * Having collected the appropriate sections carry out any additional
2949 	 * processing if necessary.
2950 	 */
2951 	for (ndx = 0; ndx < ifl->ifl_shnum; ndx++) {
2952 		Is_desc	*isp;
2953 
2954 		if ((isp = ifl->ifl_isdesc[ndx]) == NULL)
2955 			continue;
2956 		row = isp->is_shdr->sh_type;
2957 
2958 		if ((isp->is_flags & FLG_IS_DISCARD) == 0)
2959 			ld_sup_section(ofl, isp->is_name, isp->is_shdr, ndx,
2960 			    isp->is_indata, elf);
2961 
2962 		/*
2963 		 * If this is a SHT_SUNW_move section from a relocatable file,
2964 		 * keep track of the section for later processing.
2965 		 */
2966 		if ((row == SHT_SUNW_move) && (column == 0)) {
2967 			if (aplist_append(&(ofl->ofl_ismove), isp,
2968 			    AL_CNT_OFL_MOVE) == NULL)
2969 				return (S_ERROR);
2970 		}
2971 
2972 		/*
2973 		 * If this is a standard section type process it via the
2974 		 * appropriate action routine.
2975 		 */
2976 		if (row < SHT_NUM) {
2977 			if (Final[row][column] != NULL) {
2978 				if (Final[row][column](isp, ifl,
2979 				    ofl) == S_ERROR)
2980 					return (S_ERROR);
2981 			}
2982 #if	defined(_ELF64)
2983 		} else if ((row == SHT_AMD64_UNWIND) && (column == 0)) {
2984 			Os_desc	*osp = isp->is_osdesc;
2985 
2986 			/*
2987 			 * SHT_AMD64_UNWIND (0x70000001) is in the SHT_LOPROC -
2988 			 * SHT_HIPROC range reserved for processor-specific
2989 			 * semantics, and is only meaningful for amd64 targets.
2990 			 *
2991 			 * Only process unwind contents from relocatable
2992 			 * objects.
2993 			 */
2994 			if (osp && (ld_targ.t_m.m_mach == EM_AMD64) &&
2995 			    (ld_unwind_register(osp, ofl) == S_ERROR))
2996 				return (S_ERROR);
2997 #endif
2998 		}
2999 	}
3000 
3001 	/*
3002 	 * Following symbol processing, if this relocatable object input file
3003 	 * provides symbol capabilities, tag the associated symbols so that
3004 	 * the symbols can be re-assigned to the new capabilities symbol
3005 	 * section that will be created for the output file.
3006 	 */
3007 	if (capinfoisp && (ifl->ifl_ehdr->e_type == ET_REL) &&
3008 	    (process_capinfo(ofl, ifl, capinfoisp) == S_ERROR))
3009 		return (S_ERROR);
3010 
3011 	/*
3012 	 * After processing any symbol resolution, and if this dependency
3013 	 * indicates it contains symbols that can't be directly bound to,
3014 	 * set the symbols appropriately.
3015 	 */
3016 	if (sifisp && ((ifl->ifl_flags & (FLG_IF_NEEDED | FLG_IF_NODIRECT)) ==
3017 	    (FLG_IF_NEEDED | FLG_IF_NODIRECT)))
3018 		(void) ld_sym_nodirect(sifisp, ifl, ofl);
3019 
3020 	return (1);
3021 }
3022 
3023 /*
3024  * Process the current input file.  There are basically three types of files
3025  * that come through here:
3026  *
3027  *  -	files explicitly defined on the command line (ie. foo.o or bar.so),
3028  *	in this case only the `name' field is valid.
3029  *
3030  *  -	libraries determined from the -l command line option (ie. -lbar),
3031  *	in this case the `soname' field contains the basename of the located
3032  *	file.
3033  *
3034  * Any shared object specified via the above two conventions must be recorded
3035  * as a needed dependency.
3036  *
3037  *  -	libraries specified as dependencies of those libraries already obtained
3038  *	via the command line (ie. bar.so has a DT_NEEDED entry of fred.so.1),
3039  *	in this case the `soname' field contains either a full pathname (if the
3040  *	needed entry contained a `/'), or the basename of the located file.
3041  *	These libraries are processed to verify symbol binding but are not
3042  *	recorded as dependencies of the output file being generated.
3043  *
3044  * entry:
3045  *	name - File name
3046  *	soname - SONAME for needed sharable library, as described above
3047  *	fd - Open file descriptor
3048  *	elf - Open ELF handle
3049  *	flags - FLG_IF_ flags applicable to file
3050  *	ofl - Output file descriptor
3051  *	rej - Rejection descriptor used to record rejection reason
3052  *	ifl_ret - NULL, or address of pointer to receive reference to
3053  *		resulting input descriptor for file. If ifl_ret is non-NULL,
3054  *		the file cannot be an archive or it will be rejected.
3055  *
3056  * exit:
3057  *	If a error occurs in examining the file, S_ERROR is returned.
3058  *	If the file can be examined, but is not suitable, *rej is updated,
3059  *	and 0 is returned. If the file is acceptable, 1 is returned, and if
3060  *	ifl_ret is non-NULL, *ifl_ret is set to contain the pointer to the
3061  *	resulting input descriptor.
3062  */
3063 uintptr_t
3064 ld_process_ifl(const char *name, const char *soname, int fd, Elf *elf,
3065     Word flags, Ofl_desc *ofl, Rej_desc *rej, Ifl_desc **ifl_ret)
3066 {
3067 	Ifl_desc	*ifl;
3068 	Ehdr		*ehdr;
3069 	uintptr_t	error = 0;
3070 	struct stat	status;
3071 	Ar_desc		*adp;
3072 	Rej_desc	_rej;
3073 
3074 	/*
3075 	 * If this file was not extracted from an archive obtain its device
3076 	 * information.  This will be used to determine if the file has already
3077 	 * been processed (rather than simply comparing filenames, the device
3078 	 * information provides a quicker comparison and detects linked files).
3079 	 */
3080 	if (fd && ((flags & FLG_IF_EXTRACT) == 0))
3081 		(void) fstat(fd, &status);
3082 	else {
3083 		status.st_dev = 0;
3084 		status.st_ino = 0;
3085 	}
3086 
3087 	switch (elf_kind(elf)) {
3088 	case ELF_K_AR:
3089 		/*
3090 		 * If the caller has supplied a non-NULL ifl_ret, then
3091 		 * we cannot process archives, for there will be no
3092 		 * input file descriptor for us to return. In this case,
3093 		 * reject the attempt.
3094 		 */
3095 		if (ifl_ret != NULL) {
3096 			_rej.rej_type = SGS_REJ_ARCHIVE;
3097 			_rej.rej_name = name;
3098 			DBG_CALL(Dbg_file_rejected(ofl->ofl_lml, &_rej,
3099 			    ld_targ.t_m.m_mach));
3100 			if (rej->rej_type == 0) {
3101 				*rej = _rej;
3102 				rej->rej_name = strdup(_rej.rej_name);
3103 			}
3104 			return (0);
3105 		}
3106 
3107 		/*
3108 		 * Determine if we've already come across this archive file.
3109 		 */
3110 		if (!(flags & FLG_IF_EXTRACT)) {
3111 			Aliste	idx;
3112 
3113 			for (APLIST_TRAVERSE(ofl->ofl_ars, idx, adp)) {
3114 				if ((adp->ad_stdev != status.st_dev) ||
3115 				    (adp->ad_stino != status.st_ino))
3116 					continue;
3117 
3118 				/*
3119 				 * We've seen this file before so reuse the
3120 				 * original archive descriptor and discard the
3121 				 * new elf descriptor.  Note that a file
3122 				 * descriptor is unnecessary, as the file is
3123 				 * already available in memory.
3124 				 */
3125 				DBG_CALL(Dbg_file_reuse(ofl->ofl_lml, name,
3126 				    adp->ad_name));
3127 				(void) elf_end(elf);
3128 				if (!ld_process_archive(name, -1, adp, ofl))
3129 					return (S_ERROR);
3130 				return (1);
3131 			}
3132 		}
3133 
3134 		/*
3135 		 * As we haven't processed this file before establish a new
3136 		 * archive descriptor.
3137 		 */
3138 		adp = ld_ar_setup(name, elf, ofl);
3139 		if ((adp == NULL) || (adp == (Ar_desc *)S_ERROR))
3140 			return ((uintptr_t)adp);
3141 		adp->ad_stdev = status.st_dev;
3142 		adp->ad_stino = status.st_ino;
3143 
3144 		ld_sup_file(ofl, name, ELF_K_AR, flags, elf);
3145 
3146 		/*
3147 		 * Indicate that the ELF descriptor no longer requires a file
3148 		 * descriptor by reading the entire file.  The file is already
3149 		 * read via the initial mmap(2) behind elf_begin(3elf), thus
3150 		 * this operation is effectively a no-op.  However, a side-
3151 		 * effect is that the internal file descriptor, maintained in
3152 		 * the ELF descriptor, is set to -1.  This setting will not
3153 		 * be compared with any file descriptor that is passed to
3154 		 * elf_begin(), should this archive, or one of the archive
3155 		 * members, be processed again from the command line or
3156 		 * because of a -z rescan.
3157 		 */
3158 		if (elf_cntl(elf, ELF_C_FDREAD) == -1) {
3159 			ld_eprintf(ofl, ERR_ELF, MSG_INTL(MSG_ELF_CNTL),
3160 			    name);
3161 			return (0);
3162 		}
3163 
3164 		if (!ld_process_archive(name, -1, adp, ofl))
3165 			return (S_ERROR);
3166 		return (1);
3167 
3168 	case ELF_K_ELF:
3169 		/*
3170 		 * Obtain the elf header so that we can determine what type of
3171 		 * elf ELF_K_ELF file this is.
3172 		 */
3173 		if ((ehdr = elf_getehdr(elf)) == NULL) {
3174 			int	_class = gelf_getclass(elf);
3175 
3176 			/*
3177 			 * This can fail for a number of reasons. Typically
3178 			 * the object class is incorrect (ie. user is building
3179 			 * 64-bit but managed to point at 32-bit libraries).
3180 			 * Other ELF errors can include a truncated or corrupt
3181 			 * file. Try to get the best error message possible.
3182 			 */
3183 			if (ld_targ.t_m.m_class != _class) {
3184 				_rej.rej_type = SGS_REJ_CLASS;
3185 				_rej.rej_info = (uint_t)_class;
3186 			} else {
3187 				_rej.rej_type = SGS_REJ_STR;
3188 				_rej.rej_str = elf_errmsg(-1);
3189 			}
3190 			_rej.rej_name = name;
3191 			DBG_CALL(Dbg_file_rejected(ofl->ofl_lml, &_rej,
3192 			    ld_targ.t_m.m_mach));
3193 			if (rej->rej_type == 0) {
3194 				*rej = _rej;
3195 				rej->rej_name = strdup(_rej.rej_name);
3196 			}
3197 			return (0);
3198 		}
3199 
3200 		/*
3201 		 * Determine if we've already come across this file.
3202 		 */
3203 		if (!(flags & FLG_IF_EXTRACT)) {
3204 			APlist	*apl;
3205 			Aliste	idx;
3206 
3207 			if (ehdr->e_type == ET_REL)
3208 				apl = ofl->ofl_objs;
3209 			else
3210 				apl = ofl->ofl_sos;
3211 
3212 			/*
3213 			 * Traverse the appropriate file list and determine if
3214 			 * a dev/inode match is found.
3215 			 */
3216 			for (APLIST_TRAVERSE(apl, idx, ifl)) {
3217 				/*
3218 				 * Ifl_desc generated via -Nneed, therefore no
3219 				 * actual file behind it.
3220 				 */
3221 				if (ifl->ifl_flags & FLG_IF_NEEDSTR)
3222 					continue;
3223 
3224 				if ((ifl->ifl_stino != status.st_ino) ||
3225 				    (ifl->ifl_stdev != status.st_dev))
3226 					continue;
3227 
3228 				/*
3229 				 * Disregard (skip) this image.
3230 				 */
3231 				DBG_CALL(Dbg_file_skip(ofl->ofl_lml,
3232 				    ifl->ifl_name, name));
3233 				(void) elf_end(elf);
3234 
3235 				/*
3236 				 * If the file was explicitly defined on the
3237 				 * command line (this is always the case for
3238 				 * relocatable objects, and is true for shared
3239 				 * objects when they weren't specified via -l or
3240 				 * were dragged in as an implicit dependency),
3241 				 * then warn the user.
3242 				 */
3243 				if ((flags & FLG_IF_CMDLINE) ||
3244 				    (ifl->ifl_flags & FLG_IF_CMDLINE)) {
3245 					const char	*errmsg;
3246 
3247 					/*
3248 					 * Determine whether this is the same
3249 					 * file name as originally encountered
3250 					 * so as to provide the most
3251 					 * descriptive diagnostic.
3252 					 */
3253 					errmsg =
3254 					    (strcmp(name, ifl->ifl_name) == 0) ?
3255 					    MSG_INTL(MSG_FIL_MULINC_1) :
3256 					    MSG_INTL(MSG_FIL_MULINC_2);
3257 					ld_eprintf(ofl, ERR_WARNING,
3258 					    errmsg, name, ifl->ifl_name);
3259 				}
3260 				if (ifl_ret)
3261 					*ifl_ret = ifl;
3262 				return (1);
3263 			}
3264 		}
3265 
3266 		/*
3267 		 * At this point, we know we need the file.  Establish an input
3268 		 * file descriptor and continue processing.
3269 		 */
3270 		ifl = ifl_setup(name, ehdr, elf, flags, ofl, rej);
3271 		if ((ifl == NULL) || (ifl == (Ifl_desc *)S_ERROR))
3272 			return ((uintptr_t)ifl);
3273 		ifl->ifl_stdev = status.st_dev;
3274 		ifl->ifl_stino = status.st_ino;
3275 
3276 		/*
3277 		 * If -zignore is in effect, mark this file as a potential
3278 		 * candidate (the files use isn't actually determined until
3279 		 * symbol resolution and relocation processing are completed).
3280 		 */
3281 		if (ofl->ofl_flags1 & FLG_OF1_IGNORE)
3282 			ifl->ifl_flags |= FLG_IF_IGNORE;
3283 
3284 		switch (ehdr->e_type) {
3285 		case ET_REL:
3286 			(*ld_targ.t_mr.mr_mach_eflags)(ehdr, ofl);
3287 			error = process_elf(ifl, elf, ofl);
3288 			break;
3289 		case ET_DYN:
3290 			if ((ofl->ofl_flags & FLG_OF_STATIC) ||
3291 			    !(ofl->ofl_flags & FLG_OF_DYNLIBS)) {
3292 				ld_eprintf(ofl, ERR_FATAL,
3293 				    MSG_INTL(MSG_FIL_SOINSTAT), name);
3294 				return (0);
3295 			}
3296 
3297 			/*
3298 			 * Record any additional shared object information.
3299 			 * If no soname is specified (eg. this file was
3300 			 * derived from a explicit filename declaration on the
3301 			 * command line, ie. bar.so) use the pathname.
3302 			 * This entry may be overridden if the files dynamic
3303 			 * section specifies an DT_SONAME value.
3304 			 */
3305 			if (soname == NULL)
3306 				ifl->ifl_soname = ifl->ifl_name;
3307 			else
3308 				ifl->ifl_soname = soname;
3309 
3310 			/*
3311 			 * If direct bindings, lazy loading, group permissions,
3312 			 * or deferred dependencies need to be established, mark
3313 			 * this object.
3314 			 */
3315 			if (ofl->ofl_flags1 & FLG_OF1_ZDIRECT)
3316 				ifl->ifl_flags |= FLG_IF_DIRECT;
3317 			if (ofl->ofl_flags1 & FLG_OF1_LAZYLD)
3318 				ifl->ifl_flags |= FLG_IF_LAZYLD;
3319 			if (ofl->ofl_flags1 & FLG_OF1_GRPPRM)
3320 				ifl->ifl_flags |= FLG_IF_GRPPRM;
3321 			if (ofl->ofl_flags1 & FLG_OF1_DEFERRED)
3322 				ifl->ifl_flags |=
3323 				    (FLG_IF_LAZYLD | FLG_IF_DEFERRED);
3324 
3325 			error = process_elf(ifl, elf, ofl);
3326 
3327 			/*
3328 			 * Determine whether this dependency requires a syminfo.
3329 			 */
3330 			if (ifl->ifl_flags & MSK_IF_SYMINFO)
3331 				ofl->ofl_flags |= FLG_OF_SYMINFO;
3332 
3333 			/*
3334 			 * Guidance: Use -z lazyload/nolazyload.
3335 			 * libc is exempt from this advice, because it cannot
3336 			 * be lazy loaded, and requests to do so are ignored.
3337 			 */
3338 			if (OFL_GUIDANCE(ofl, FLG_OFG_NO_LAZY) &&
3339 			    ((ifl->ifl_flags & FLG_IF_RTLDINF) == 0)) {
3340 				ld_eprintf(ofl, ERR_GUIDANCE,
3341 				    MSG_INTL(MSG_GUIDE_LAZYLOAD));
3342 				ofl->ofl_guideflags |= FLG_OFG_NO_LAZY;
3343 			}
3344 
3345 			/*
3346 			 * Guidance: Use -B direct/nodirect or
3347 			 * -z direct/nodirect.
3348 			 */
3349 			if (OFL_GUIDANCE(ofl, FLG_OFG_NO_DB)) {
3350 				ld_eprintf(ofl, ERR_GUIDANCE,
3351 				    MSG_INTL(MSG_GUIDE_DIRECT));
3352 				ofl->ofl_guideflags |= FLG_OFG_NO_DB;
3353 			}
3354 
3355 			break;
3356 		default:
3357 			(void) elf_errno();
3358 			_rej.rej_type = SGS_REJ_UNKFILE;
3359 			_rej.rej_name = name;
3360 			DBG_CALL(Dbg_file_rejected(ofl->ofl_lml, &_rej,
3361 			    ld_targ.t_m.m_mach));
3362 			if (rej->rej_type == 0) {
3363 				*rej = _rej;
3364 				rej->rej_name = strdup(_rej.rej_name);
3365 			}
3366 			return (0);
3367 		}
3368 		break;
3369 	default:
3370 		(void) elf_errno();
3371 		_rej.rej_type = SGS_REJ_UNKFILE;
3372 		_rej.rej_name = name;
3373 		DBG_CALL(Dbg_file_rejected(ofl->ofl_lml, &_rej,
3374 		    ld_targ.t_m.m_mach));
3375 		if (rej->rej_type == 0) {
3376 			*rej = _rej;
3377 			rej->rej_name = strdup(_rej.rej_name);
3378 		}
3379 		return (0);
3380 	}
3381 	if ((error == 0) || (error == S_ERROR))
3382 		return (error);
3383 
3384 	if (ifl_ret)
3385 		*ifl_ret = ifl;
3386 	return (1);
3387 }
3388 
3389 /*
3390  * Having successfully opened a file, set up the necessary elf structures to
3391  * process it further.  This small section of processing is slightly different
3392  * from the elf initialization required to process a relocatable object from an
3393  * archive (see libs.c: ld_process_archive()).
3394  */
3395 uintptr_t
3396 ld_process_open(const char *opath, const char *ofile, int *fd, Ofl_desc *ofl,
3397     Word flags, Rej_desc *rej, Ifl_desc **ifl_ret)
3398 {
3399 	Elf		*elf;
3400 	const char	*npath = opath;
3401 	const char	*nfile = ofile;
3402 
3403 	if ((elf = elf_begin(*fd, ELF_C_READ, NULL)) == NULL) {
3404 		ld_eprintf(ofl, ERR_ELF, MSG_INTL(MSG_ELF_BEGIN), npath);
3405 		return (0);
3406 	}
3407 
3408 	/*
3409 	 * Determine whether the support library wishes to process this open.
3410 	 * The support library may return:
3411 	 *   .	a different ELF descriptor (in which case they should have
3412 	 *	closed the original)
3413 	 *   .	a different file descriptor (in which case they should have
3414 	 *	closed the original)
3415 	 *   .	a different path and file name (presumably associated with
3416 	 *	a different file descriptor)
3417 	 *
3418 	 * A file descriptor of -1, or and ELF descriptor of zero indicates
3419 	 * the file should be ignored.
3420 	 */
3421 	ld_sup_open(ofl, &npath, &nfile, fd, flags, &elf, NULL, 0,
3422 	    elf_kind(elf));
3423 
3424 	if ((*fd == -1) || (elf == NULL))
3425 		return (0);
3426 
3427 	return (ld_process_ifl(npath, nfile, *fd, elf, flags, ofl, rej,
3428 	    ifl_ret));
3429 }
3430 
3431 /*
3432  * Having successfully mapped a file, set up the necessary elf structures to
3433  * process it further.  This routine is patterned after ld_process_open() and
3434  * is only called by ld.so.1(1) to process a relocatable object.
3435  */
3436 Ifl_desc *
3437 ld_process_mem(const char *path, const char *file, char *addr, size_t size,
3438     Ofl_desc *ofl, Rej_desc *rej)
3439 {
3440 	Elf		*elf;
3441 	uintptr_t	open_ret;
3442 	Ifl_desc	*ifl;
3443 
3444 	if ((elf = elf_memory(addr, size)) == NULL) {
3445 		ld_eprintf(ofl, ERR_ELF, MSG_INTL(MSG_ELF_MEMORY), path);
3446 		return (0);
3447 	}
3448 
3449 	open_ret = ld_process_ifl(path, file, 0, elf, 0, ofl, rej, &ifl);
3450 	if (open_ret != 1)
3451 		return ((Ifl_desc *) open_ret);
3452 	return (ifl);
3453 }
3454 
3455 /*
3456  * Process a required library (i.e. the dependency of a shared object).
3457  * Combine the directory and filename, check the resultant path size, and try
3458  * opening the pathname.
3459  */
3460 static Ifl_desc *
3461 process_req_lib(Sdf_desc *sdf, const char *dir, const char *file,
3462     Ofl_desc *ofl, Rej_desc *rej)
3463 {
3464 	size_t		dlen, plen;
3465 	int		fd;
3466 	char		path[PATH_MAX];
3467 	const char	*_dir = dir;
3468 
3469 	/*
3470 	 * Determine the sizes of the directory and filename to insure we don't
3471 	 * exceed our buffer.
3472 	 */
3473 	if ((dlen = strlen(dir)) == 0) {
3474 		_dir = MSG_ORIG(MSG_STR_DOT);
3475 		dlen = 1;
3476 	}
3477 	dlen++;
3478 	plen = dlen + strlen(file) + 1;
3479 	if (plen > PATH_MAX) {
3480 		ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_FIL_PTHTOLONG),
3481 		    _dir, file);
3482 		return (0);
3483 	}
3484 
3485 	/*
3486 	 * Build the entire pathname and try and open the file.
3487 	 */
3488 	(void) strcpy(path, _dir);
3489 	(void) strcat(path, MSG_ORIG(MSG_STR_SLASH));
3490 	(void) strcat(path, file);
3491 	DBG_CALL(Dbg_libs_req(ofl->ofl_lml, sdf->sdf_name,
3492 	    sdf->sdf_rfile, path));
3493 
3494 	if ((fd = open(path, O_RDONLY)) == -1)
3495 		return (0);
3496 	else {
3497 		uintptr_t	open_ret;
3498 		Ifl_desc	*ifl;
3499 		char		*_path;
3500 
3501 		if ((_path = libld_malloc(strlen(path) + 1)) == NULL)
3502 			return ((Ifl_desc *)S_ERROR);
3503 		(void) strcpy(_path, path);
3504 		open_ret = ld_process_open(_path, &_path[dlen], &fd, ofl,
3505 		    0, rej, &ifl);
3506 		if (fd != -1)
3507 			(void) close(fd);
3508 		if (open_ret != 1)
3509 			return ((Ifl_desc *)open_ret);
3510 		return (ifl);
3511 	}
3512 }
3513 
3514 /*
3515  * Finish any library processing.  Walk the list of so's that have been listed
3516  * as "included" by shared objects we have previously processed.  Examine them,
3517  * without adding them as explicit dependents of this program, in order to
3518  * complete our symbol definition process.  The search path rules are:
3519  *
3520  *  -	use any user supplied paths, i.e. LD_LIBRARY_PATH and -L, then
3521  *
3522  *  -	use any RPATH defined within the parent shared object, then
3523  *
3524  *  -	use the default directories, i.e. LIBPATH or -YP.
3525  */
3526 uintptr_t
3527 ld_finish_libs(Ofl_desc *ofl)
3528 {
3529 	Aliste		idx1;
3530 	Sdf_desc	*sdf;
3531 	Rej_desc	rej = { 0 };
3532 
3533 	/*
3534 	 * Make sure we are back in dynamic mode.
3535 	 */
3536 	ofl->ofl_flags |= FLG_OF_DYNLIBS;
3537 
3538 	for (APLIST_TRAVERSE(ofl->ofl_soneed, idx1, sdf)) {
3539 		Aliste		idx2;
3540 		char		*path, *slash = NULL;
3541 		int		fd;
3542 		Ifl_desc	*ifl;
3543 		char		*file = (char *)sdf->sdf_name;
3544 
3545 		/*
3546 		 * See if this file has already been processed.  At the time
3547 		 * this implicit dependency was determined there may still have
3548 		 * been more explicit dependencies to process.  Note, if we ever
3549 		 * do parse the command line three times we would be able to
3550 		 * do all this checking when processing the dynamic section.
3551 		 */
3552 		if (sdf->sdf_file)
3553 			continue;
3554 
3555 		for (APLIST_TRAVERSE(ofl->ofl_sos, idx2, ifl)) {
3556 			if (!(ifl->ifl_flags & FLG_IF_NEEDSTR) &&
3557 			    (strcmp(file, ifl->ifl_soname) == 0)) {
3558 				sdf->sdf_file = ifl;
3559 				break;
3560 			}
3561 		}
3562 		if (sdf->sdf_file)
3563 			continue;
3564 
3565 		/*
3566 		 * If the current path name element embeds a "/", then it's to
3567 		 * be taken "as is", with no searching involved.  Process all
3568 		 * "/" occurrences, so that we can deduce the base file name.
3569 		 */
3570 		for (path = file; *path; path++) {
3571 			if (*path == '/')
3572 				slash = path;
3573 		}
3574 		if (slash) {
3575 			DBG_CALL(Dbg_libs_req(ofl->ofl_lml, sdf->sdf_name,
3576 			    sdf->sdf_rfile, file));
3577 			if ((fd = open(file, O_RDONLY)) == -1) {
3578 				ld_eprintf(ofl, ERR_WARNING,
3579 				    MSG_INTL(MSG_FIL_NOTFOUND), file,
3580 				    sdf->sdf_rfile);
3581 			} else {
3582 				uintptr_t	open_ret;
3583 				Rej_desc	_rej = { 0 };
3584 
3585 				open_ret = ld_process_open(file, ++slash,
3586 				    &fd, ofl, 0, &_rej, &ifl);
3587 				if (fd != -1)
3588 					(void) close(fd);
3589 				if (open_ret == S_ERROR)
3590 					return (S_ERROR);
3591 
3592 				if (_rej.rej_type) {
3593 					Conv_reject_desc_buf_t rej_buf;
3594 
3595 					ld_eprintf(ofl, ERR_WARNING,
3596 					    MSG_INTL(reject[_rej.rej_type]),
3597 					    _rej.rej_name ? rej.rej_name :
3598 					    MSG_INTL(MSG_STR_UNKNOWN),
3599 					    conv_reject_desc(&_rej, &rej_buf,
3600 					    ld_targ.t_m.m_mach));
3601 				} else
3602 					sdf->sdf_file = ifl;
3603 			}
3604 			continue;
3605 		}
3606 
3607 		/*
3608 		 * Now search for this file in any user defined directories.
3609 		 */
3610 		for (APLIST_TRAVERSE(ofl->ofl_ulibdirs, idx2, path)) {
3611 			Rej_desc	_rej = { 0 };
3612 
3613 			ifl = process_req_lib(sdf, path, file, ofl, &_rej);
3614 			if (ifl == (Ifl_desc *)S_ERROR) {
3615 				return (S_ERROR);
3616 			}
3617 			if (_rej.rej_type) {
3618 				if (rej.rej_type == 0) {
3619 					rej = _rej;
3620 					rej.rej_name = strdup(_rej.rej_name);
3621 				}
3622 			}
3623 			if (ifl) {
3624 				sdf->sdf_file = ifl;
3625 				break;
3626 			}
3627 		}
3628 		if (sdf->sdf_file)
3629 			continue;
3630 
3631 		/*
3632 		 * Next use the local rules defined within the parent shared
3633 		 * object.
3634 		 */
3635 		if (sdf->sdf_rpath != NULL) {
3636 			char	*rpath, *next;
3637 
3638 			rpath = libld_malloc(strlen(sdf->sdf_rpath) + 1);
3639 			if (rpath == NULL)
3640 				return (S_ERROR);
3641 			(void) strcpy(rpath, sdf->sdf_rpath);
3642 			DBG_CALL(Dbg_libs_path(ofl->ofl_lml, rpath,
3643 			    LA_SER_RUNPATH, sdf->sdf_rfile));
3644 			if ((path = strtok_r(rpath,
3645 			    MSG_ORIG(MSG_STR_COLON), &next)) != NULL) {
3646 				do {
3647 					Rej_desc	_rej = { 0 };
3648 
3649 					path = expand(sdf->sdf_rfile, path,
3650 					    &next);
3651 
3652 					ifl = process_req_lib(sdf, path,
3653 					    file, ofl, &_rej);
3654 					if (ifl == (Ifl_desc *)S_ERROR) {
3655 						return (S_ERROR);
3656 					}
3657 					if ((_rej.rej_type) &&
3658 					    (rej.rej_type == 0)) {
3659 						rej = _rej;
3660 						rej.rej_name =
3661 						    strdup(_rej.rej_name);
3662 					}
3663 					if (ifl) {
3664 						sdf->sdf_file = ifl;
3665 						break;
3666 					}
3667 				} while ((path = strtok_r(NULL,
3668 				    MSG_ORIG(MSG_STR_COLON), &next)) != NULL);
3669 			}
3670 		}
3671 		if (sdf->sdf_file)
3672 			continue;
3673 
3674 		/*
3675 		 * Finally try the default library search directories.
3676 		 */
3677 		for (APLIST_TRAVERSE(ofl->ofl_dlibdirs, idx2, path)) {
3678 			Rej_desc	_rej = { 0 };
3679 
3680 			ifl = process_req_lib(sdf, path, file, ofl, &rej);
3681 			if (ifl == (Ifl_desc *)S_ERROR) {
3682 				return (S_ERROR);
3683 			}
3684 			if (_rej.rej_type) {
3685 				if (rej.rej_type == 0) {
3686 					rej = _rej;
3687 					rej.rej_name = strdup(_rej.rej_name);
3688 				}
3689 			}
3690 			if (ifl) {
3691 				sdf->sdf_file = ifl;
3692 				break;
3693 			}
3694 		}
3695 		if (sdf->sdf_file)
3696 			continue;
3697 
3698 		/*
3699 		 * If we've got this far we haven't found the shared object.
3700 		 * If an object was found, but was rejected for some reason,
3701 		 * print a diagnostic to that effect, otherwise generate a
3702 		 * generic "not found" diagnostic.
3703 		 */
3704 		if (rej.rej_type) {
3705 			Conv_reject_desc_buf_t rej_buf;
3706 
3707 			ld_eprintf(ofl, ERR_WARNING,
3708 			    MSG_INTL(reject[rej.rej_type]),
3709 			    rej.rej_name ? rej.rej_name :
3710 			    MSG_INTL(MSG_STR_UNKNOWN),
3711 			    conv_reject_desc(&rej, &rej_buf,
3712 			    ld_targ.t_m.m_mach));
3713 		} else {
3714 			ld_eprintf(ofl, ERR_WARNING,
3715 			    MSG_INTL(MSG_FIL_NOTFOUND), file, sdf->sdf_rfile);
3716 		}
3717 	}
3718 
3719 	/*
3720 	 * Finally, now that all objects have been input, make sure any version
3721 	 * requirements have been met.
3722 	 */
3723 	return (ld_vers_verify(ofl));
3724 }
3725