xref: /titanic_52/usr/src/cmd/sgs/libld/common/files.c (revision 3ed4a803a4d4b35e2773c194c9a6f4977687b542)
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 2009 Sun Microsystems, Inc.  All rights reserved.
27  * Use is subject to license terms.
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))) == 0)
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 			eprintf(ofl->ofl_lml, ERR_ELF,
121 			    MSG_INTL(MSG_ELF_GETSCN), name);
122 			ofl->ofl_flags |= FLG_OF_FATAL;
123 			return ((Ifl_desc *)S_ERROR);
124 		}
125 		if ((shdr0 = elf_getshdr(scn)) == NULL) {
126 			eprintf(ofl->ofl_lml, ERR_ELF,
127 			    MSG_INTL(MSG_ELF_GETSHDR), name);
128 			ofl->ofl_flags |= FLG_OF_FATAL;
129 			return ((Ifl_desc *)S_ERROR);
130 		}
131 		ifl->ifl_shnum = (Word)shdr0->sh_size;
132 		if (ehdr->e_shstrndx == SHN_XINDEX)
133 			ifl->ifl_shstrndx = shdr0->sh_link;
134 		else
135 			ifl->ifl_shstrndx = ehdr->e_shstrndx;
136 	} else {
137 		ifl->ifl_shnum = ehdr->e_shnum;
138 		ifl->ifl_shstrndx = ehdr->e_shstrndx;
139 	}
140 
141 	if ((ifl->ifl_isdesc = libld_calloc(ifl->ifl_shnum,
142 	    sizeof (Is_desc *))) == 0)
143 		return ((Ifl_desc *)S_ERROR);
144 
145 	/*
146 	 * Record this new input file on the shared object or relocatable
147 	 * object input file list.
148 	 */
149 	if (ifl->ifl_ehdr->e_type == ET_DYN) {
150 		if (aplist_append(&ofl->ofl_sos, ifl, AL_CNT_OFL_LIBS) == NULL)
151 			return ((Ifl_desc *)S_ERROR);
152 	} else {
153 		if (aplist_append(&ofl->ofl_objs, ifl, AL_CNT_OFL_OBJS) == NULL)
154 			return ((Ifl_desc *)S_ERROR);
155 	}
156 
157 	return (ifl);
158 }
159 
160 /*
161  * Process a generic section.  The appropriate section information is added
162  * to the files input descriptor list.
163  */
164 static uintptr_t
165 process_section(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
166     Word ndx, int ident, Ofl_desc *ofl)
167 {
168 	Is_desc	*isp;
169 
170 	/*
171 	 * Create a new input section descriptor.  If this is a NOBITS
172 	 * section elf_getdata() will still create a data buffer (the buffer
173 	 * will be null and the size will reflect the actual memory size).
174 	 */
175 	if ((isp = libld_calloc(sizeof (Is_desc), 1)) == 0)
176 		return (S_ERROR);
177 	isp->is_shdr = shdr;
178 	isp->is_file = ifl;
179 	isp->is_name = name;
180 	isp->is_scnndx = ndx;
181 	isp->is_flags = FLG_IS_EXTERNAL;
182 	isp->is_keyident = ident;
183 
184 	if ((isp->is_indata = elf_getdata(scn, NULL)) == NULL) {
185 		eprintf(ofl->ofl_lml, ERR_ELF, MSG_INTL(MSG_ELF_GETDATA),
186 		    ifl->ifl_name);
187 		ofl->ofl_flags |= FLG_OF_FATAL;
188 		return (0);
189 	}
190 
191 	if ((shdr->sh_flags & SHF_EXCLUDE) &&
192 	    ((ofl->ofl_flags & FLG_OF_RELOBJ) == 0)) {
193 		isp->is_flags |= FLG_IS_DISCARD;
194 	}
195 
196 	/*
197 	 * Add the new input section to the files input section list and
198 	 * flag whether the section needs placing in an output section.  This
199 	 * placement is deferred until all input section processing has been
200 	 * completed, as SHT_GROUP sections can provide information that will
201 	 * affect how other sections within the file should be placed.
202 	 */
203 	ifl->ifl_isdesc[ndx] = isp;
204 
205 	if (ident) {
206 		if (shdr->sh_flags & ALL_SHF_ORDER) {
207 			isp->is_flags |= FLG_IS_ORDERED;
208 			ifl->ifl_flags |= FLG_IF_ORDERED;
209 		}
210 		isp->is_flags |= FLG_IS_PLACE;
211 	}
212 	return (1);
213 }
214 
215 /*
216  * Determine the software capabilities of the object being built from the
217  * capabilities of the input relocatable objects.   One software capability
218  * is presently recognized, and represented with the following (sys/elf.h):
219  *
220  *   SF1_SUNW_FPKNWN	use/non-use of frame pointer is known, and
221  *   SF1_SUNW_FPUSED    the frame pointer is in use.
222  *
223  * The resolution of the present fame pointer state, and the capabilities
224  * provided by a new input relocatable object are:
225  *
226  *                              new input relocatable object
227  *
228  *      present      |  SF1_SUNW_FPKNWN  |  SF1_SUNW_FPKNWN  |    <unknown>
229  *       state       |  SF1_SUNW_FPUSED  |                   |
230  *  ---------------------------------------------------------------------------
231  *  SF1_SUNW_FPKNWN  |  SF1_SUNW_FPKNWN  |  SF1_SUNW_FPKNWN  |  SF1_SUNW_FPKNWN
232  *  SF1_SUNW_FPUSED  |  SF1_SUNW_FPUSED  |                   |  SF1_SUNW_FPUSED
233  *  ---------------------------------------------------------------------------
234  *  SF1_SUNW_FPKNWN  |  SF1_SUNW_FPKNWN  |  SF1_SUNW_FPKNWN  |  SF1_SUNW_FPKNWN
235  *                   |                   |                   |
236  *  ---------------------------------------------------------------------------
237  *     <unknown>     |  SF1_SUNW_FPKNWN  |  SF1_SUNW_FPKNWN  |    <unknown>
238  *                   |  SF1_SUNW_FPUSED  |                   |
239  */
240 static void
241 sf1_cap(Ofl_desc *ofl, Xword val, Ifl_desc *ifl, const char *name)
242 {
243 	Xword	badval;
244 
245 	/*
246 	 * If a mapfile has established definitions to override any input
247 	 * capabilities, ignore any new input capabilities.
248 	 */
249 	if (ofl->ofl_flags1 & FLG_OF1_OVSFCAP) {
250 		Dbg_cap_sec_entry(ofl->ofl_lml, DBG_CAP_IGNORE, CA_SUNW_SF_1,
251 		    val, ld_targ.t_m.m_mach);
252 		return;
253 	}
254 
255 #if	!defined(_ELF64)
256 	if (ifl->ifl_ehdr->e_type == ET_REL) {
257 		/*
258 		 * The SF1_SUNW_ADDR32 is only meaningful when building a 64-bit
259 		 * object.  Warn the user, and remove the setting, if we're
260 		 * building a 32-bit object.
261 		 */
262 		if (val & SF1_SUNW_ADDR32) {
263 			eprintf(ofl->ofl_lml, ERR_WARNING,
264 			    MSG_INTL(MSG_FIL_INADDR32SF1), ifl->ifl_name, 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 		eprintf(ofl->ofl_lml, ERR_WARNING, MSG_INTL(MSG_FIL_BADSF1),
282 		    ifl->ifl_name, name, EC_XWORD(badval));
283 		val &= SF1_SUNW_MASK;
284 	}
285 	if ((val & (SF1_SUNW_FPKNWN | SF1_SUNW_FPUSED)) == SF1_SUNW_FPUSED) {
286 		eprintf(ofl->ofl_lml, ERR_WARNING, MSG_INTL(MSG_FIL_BADSF1),
287 		    ifl->ifl_name, name, EC_XWORD(val));
288 		return;
289 	}
290 
291 	/*
292 	 * If the input file is not a relocatable object, then we're only here
293 	 * to warn the user of any questionable capabilities.
294 	 */
295 	if (ifl->ifl_ehdr->e_type != ET_REL) {
296 #if	defined(_ELF64)
297 		/*
298 		 * If we're building a 64-bit executable, and we come across a
299 		 * dependency that requires a restricted address space, then
300 		 * that dependencies requirement can only be satisfied if the
301 		 * executable triggers the restricted address space.  This is a
302 		 * warning rather than a fatal error, as the possibility exists
303 		 * that an appropriate dependency will be provided at runtime.
304 		 * The runtime linker will refuse to use this dependency.
305 		 */
306 		if ((val & SF1_SUNW_ADDR32) && (ofl->ofl_flags & FLG_OF_EXEC) &&
307 		    ((ofl->ofl_sfcap_1 & SF1_SUNW_ADDR32) == 0)) {
308 			eprintf(ofl->ofl_lml, ERR_WARNING,
309 			    MSG_INTL(MSG_FIL_EXADDR32SF1), ifl->ifl_name, name);
310 		}
311 #endif
312 		return;
313 	}
314 
315 	Dbg_cap_sec_entry(ofl->ofl_lml, DBG_CAP_OLD, CA_SUNW_SF_1,
316 	    ofl->ofl_sfcap_1, ld_targ.t_m.m_mach);
317 	Dbg_cap_sec_entry(ofl->ofl_lml, DBG_CAP_NEW, CA_SUNW_SF_1,
318 	    val, ld_targ.t_m.m_mach);
319 
320 	/*
321 	 * Determine the resolution of the present frame pointer and the
322 	 * new input relocatable objects frame pointer.
323 	 */
324 	if ((ofl->ofl_sfcap_1 & (SF1_SUNW_FPKNWN | SF1_SUNW_FPUSED)) ==
325 	    (SF1_SUNW_FPKNWN | SF1_SUNW_FPUSED)) {
326 		/*
327 		 * If the new relocatable object isn't using a frame pointer,
328 		 * reduce the present state to unused.
329 		 */
330 		if ((val & (SF1_SUNW_FPKNWN | SF1_SUNW_FPUSED)) !=
331 		    (SF1_SUNW_FPKNWN | SF1_SUNW_FPUSED))
332 			ofl->ofl_sfcap_1 &= ~SF1_SUNW_FPUSED;
333 
334 	} else if ((ofl->ofl_sfcap_1 & SF1_SUNW_FPKNWN) == 0) {
335 		/*
336 		 * If the present state is unknown, take the new relocatable
337 		 * object frame pointer usage.
338 		 */
339 		ofl->ofl_sfcap_1 = val;
340 	}
341 
342 	Dbg_cap_sec_entry(ofl->ofl_lml, DBG_CAP_RESOLVED, CA_SUNW_SF_1,
343 	    ofl->ofl_sfcap_1, ld_targ.t_m.m_mach);
344 }
345 
346 /*
347  * Determine the hardware capabilities of the object being built from the
348  * capabilities of the input relocatable objects.  There's really little to
349  * do here, other than to offer diagnostics, hardware capabilities are simply
350  * additive.
351  */
352 static void
353 hw1_cap(Ofl_desc *ofl, Xword val)
354 {
355 	/*
356 	 * If a mapfile has established definitions to override any input
357 	 * capabilities, ignore any new input capabilities.
358 	 */
359 	if (ofl->ofl_flags1 & FLG_OF1_OVHWCAP) {
360 		Dbg_cap_sec_entry(ofl->ofl_lml, DBG_CAP_IGNORE, CA_SUNW_HW_1,
361 		    val, ld_targ.t_m.m_mach);
362 		return;
363 	}
364 
365 	/*
366 	 * If this object doesn't specify any capabilities, ignore it, and
367 	 * leave the state as is.
368 	 */
369 	if (val == 0)
370 		return;
371 
372 	Dbg_cap_sec_entry(ofl->ofl_lml, DBG_CAP_OLD, CA_SUNW_HW_1,
373 	    ofl->ofl_hwcap_1, ld_targ.t_m.m_mach);
374 	Dbg_cap_sec_entry(ofl->ofl_lml, DBG_CAP_NEW, CA_SUNW_HW_1, val,
375 	    ld_targ.t_m.m_mach);
376 
377 	ofl->ofl_hwcap_1 |= val;
378 
379 	Dbg_cap_sec_entry(ofl->ofl_lml, DBG_CAP_RESOLVED, CA_SUNW_HW_1,
380 	    ofl->ofl_hwcap_1, ld_targ.t_m.m_mach);
381 }
382 
383 /*
384  * Process a hardware/software capabilities section.  Traverse the section
385  * updating the global capabilities variables as necessary.
386  */
387 static void
388 process_cap(Ifl_desc *ifl, Is_desc *cisp, Ofl_desc *ofl)
389 {
390 	Cap	*cdata;
391 	Word	ndx, cnum;
392 
393 	DBG_CALL(Dbg_cap_sec_title(ofl->ofl_lml, ifl->ifl_name));
394 
395 	/*
396 	 * The capabilities are supposed to be terminated with a CA_SUNW_NULL
397 	 * entry.  However, the compilers have been known to not follow this
398 	 * convention.  Use the section information to determine the number
399 	 * of capabilities, and skip any CA_SUNW_NULL entries.
400 	 */
401 	cdata = (Cap *)cisp->is_indata->d_buf;
402 	cnum = (Word)(cisp->is_shdr->sh_size / cisp->is_shdr->sh_entsize);
403 
404 	for (ndx = 0; ndx < cnum; cdata++, ndx++) {
405 		switch (cdata->c_tag) {
406 			case CA_SUNW_HW_1:
407 				/*
408 				 * Only the hardware capabilities that are
409 				 * defined in a relocatable object become part
410 				 * of the hardware capabilities in the output
411 				 * file.
412 				 */
413 				if (ifl->ifl_ehdr->e_type == ET_REL)
414 					hw1_cap(ofl, cdata->c_un.c_val);
415 				break;
416 			case CA_SUNW_SF_1:
417 				/*
418 				 * Only the software capabilities that are
419 				 * defined in a relocatable object become part
420 				 * of the software capabilities in the output
421 				 * file.  However, check the validity of the
422 				 * software capabilities of any dependencies.
423 				 */
424 				sf1_cap(ofl, cdata->c_un.c_val, ifl,
425 				    cisp->is_name);
426 				break;
427 			case CA_SUNW_NULL:
428 				break;
429 			default:
430 				eprintf(ofl->ofl_lml, ERR_WARNING,
431 				    MSG_INTL(MSG_FIL_UNKCAP),
432 				    ifl->ifl_name, cisp->is_name, cdata->c_tag);
433 		}
434 	}
435 }
436 
437 /*
438  * Simply process the section so that we have pointers to the data for use
439  * in later routines, however don't add the section to the output section
440  * list as we will be creating our own replacement sections later (ie.
441  * symtab and relocation).
442  */
443 static uintptr_t
444 /* ARGSUSED5 */
445 process_input(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
446     Word ndx, int ident, Ofl_desc *ofl)
447 {
448 	return (process_section(name, ifl, shdr, scn, ndx,
449 	    ld_targ.t_id.id_null, ofl));
450 }
451 
452 /*
453  * Keep a running count of relocation entries from input relocatable objects for
454  * sizing relocation buckets later.  If we're building an executable, save any
455  * relocations from shared objects to determine if any copy relocation symbol
456  * has a displacement relocation against it.
457  */
458 static uintptr_t
459 /* ARGSUSED5 */
460 process_reloc(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
461     Word ndx, int ident, Ofl_desc *ofl)
462 {
463 	if (process_section(name, ifl,
464 	    shdr, scn, ndx, ld_targ.t_id.id_null, ofl) == S_ERROR)
465 		return (S_ERROR);
466 
467 	if (ifl->ifl_ehdr->e_type == ET_REL) {
468 		if (shdr->sh_entsize && (shdr->sh_entsize <= shdr->sh_size))
469 			/* LINTED */
470 			ofl->ofl_relocincnt +=
471 			    (Word)(shdr->sh_size / shdr->sh_entsize);
472 	} else if (ofl->ofl_flags & FLG_OF_EXEC) {
473 		if (aplist_append(&ifl->ifl_relsect, ifl->ifl_isdesc[ndx],
474 		    AL_CNT_IFL_RELSECS) == NULL)
475 			return (S_ERROR);
476 	}
477 	return (1);
478 }
479 
480 
481 /*
482  * Process a string table section.  A valid section contains an initial and
483  * final null byte.
484  */
485 static uintptr_t
486 process_strtab(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
487     Word ndx, int ident, Ofl_desc *ofl)
488 {
489 	char		*data;
490 	size_t		size;
491 	Is_desc		*isp;
492 	uintptr_t	error;
493 
494 	/*
495 	 * Never include .stab.excl sections in any output file.
496 	 * If the -s flag has been specified strip any .stab sections.
497 	 */
498 	if (((ofl->ofl_flags & FLG_OF_STRIP) && ident &&
499 	    (strncmp(name, MSG_ORIG(MSG_SCN_STAB), MSG_SCN_STAB_SIZE) == 0)) ||
500 	    (strcmp(name, MSG_ORIG(MSG_SCN_STABEXCL)) == 0) && ident)
501 		return (1);
502 
503 	/*
504 	 * If we got here to process a .shstrtab or .dynstr table, `ident' will
505 	 * be null.  Otherwise make sure we don't have a .strtab section as this
506 	 * should not be added to the output section list either.
507 	 */
508 	if ((ident != ld_targ.t_id.id_null) &&
509 	    (strcmp(name, MSG_ORIG(MSG_SCN_STRTAB)) == 0))
510 		ident = ld_targ.t_id.id_null;
511 
512 	error = process_section(name, ifl, shdr, scn, ndx, ident, ofl);
513 	if ((error == 0) || (error == S_ERROR))
514 		return (error);
515 
516 	/*
517 	 * String tables should start and end with a NULL byte.  Note, it has
518 	 * been known for the assembler to create empty string tables, so check
519 	 * the size before attempting to verify the data itself.
520 	 */
521 	isp = ifl->ifl_isdesc[ndx];
522 	size = isp->is_indata->d_size;
523 	if (size) {
524 		data = isp->is_indata->d_buf;
525 		if (data[0] != '\0' || data[size - 1] != '\0')
526 			eprintf(ofl->ofl_lml, ERR_WARNING,
527 			    MSG_INTL(MSG_FIL_MALSTR), ifl->ifl_name, name);
528 	} else
529 		isp->is_indata->d_buf = (void *)MSG_ORIG(MSG_STR_EMPTY);
530 
531 	ifl->ifl_flags |= FLG_IF_HSTRTAB;
532 	return (1);
533 }
534 
535 /*
536  * Invalid sections produce a warning and are skipped.
537  */
538 static uintptr_t
539 /* ARGSUSED3 */
540 invalid_section(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
541     Word ndx, int ident, Ofl_desc *ofl)
542 {
543 	Conv_inv_buf_t inv_buf;
544 
545 	eprintf(ofl->ofl_lml, ERR_WARNING, MSG_INTL(MSG_FIL_INVALSEC),
546 	    ifl->ifl_name, name, conv_sec_type(ifl->ifl_ehdr->e_machine,
547 	    shdr->sh_type, 0, &inv_buf));
548 	return (1);
549 }
550 
551 /*
552  * Process a progbits section.
553  */
554 static uintptr_t
555 process_progbits(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
556     Word ndx, int ident, Ofl_desc *ofl)
557 {
558 	int		stab_index = 0;
559 	Word		is_flags = 0;
560 	uintptr_t	r;
561 
562 	/*
563 	 * Never include .stab.excl sections in any output file.
564 	 * If the -s flag has been specified strip any .stab sections.
565 	 */
566 	if (ident && (strncmp(name, MSG_ORIG(MSG_SCN_STAB),
567 	    MSG_SCN_STAB_SIZE) == 0)) {
568 		if ((ofl->ofl_flags & FLG_OF_STRIP) ||
569 		    (strcmp((name + MSG_SCN_STAB_SIZE),
570 		    MSG_ORIG(MSG_SCN_EXCL)) == 0))
571 			return (1);
572 
573 		if (strcmp((name + MSG_SCN_STAB_SIZE),
574 		    MSG_ORIG(MSG_SCN_INDEX)) == 0)
575 			stab_index = 1;
576 	}
577 
578 	if ((ofl->ofl_flags & FLG_OF_STRIP) && ident) {
579 		if ((strncmp(name, MSG_ORIG(MSG_SCN_DEBUG),
580 		    MSG_SCN_DEBUG_SIZE) == 0) ||
581 		    (strcmp(name, MSG_ORIG(MSG_SCN_LINE)) == 0))
582 			return (1);
583 	}
584 
585 	/*
586 	 * Update the ident to reflect the type of section we've got.
587 	 *
588 	 * If there is any .plt or .got section to generate we'll be creating
589 	 * our own version, so don't allow any input sections of these types to
590 	 * be added to the output section list (why a relocatable object would
591 	 * have a .plt or .got is a mystery, but stranger things have occurred).
592 	 *
593 	 * If there are any unwind sections, and this is a platform that uses
594 	 * SHT_PROGBITS for unwind sections, then set their ident to reflect
595 	 * that.
596 	 */
597 	if (ident) {
598 		if (shdr->sh_flags & SHF_TLS) {
599 			ident = ld_targ.t_id.id_tls;
600 		} else if ((shdr->sh_flags & ~ALL_SHF_IGNORE) ==
601 		    (SHF_ALLOC | SHF_EXECINSTR)) {
602 			ident = ld_targ.t_id.id_text;
603 		} else if (shdr->sh_flags & SHF_ALLOC) {
604 			int done = 0;
605 
606 			if (name[0] == '.') {
607 				switch (name[1]) {
608 				case 'e':
609 					if ((ld_targ.t_m.m_sht_unwind ==
610 					    SHT_PROGBITS) &&
611 					    (strcmp(name,
612 					    MSG_ORIG(MSG_SCN_EHFRAME)) == 0)) {
613 						ident = ld_targ.t_id.id_unwind;
614 						is_flags = FLG_IS_EHFRAME;
615 						done = 1;
616 					}
617 					break;
618 				case 'g':
619 					if (strcmp(name,
620 					    MSG_ORIG(MSG_SCN_GOT)) == 0) {
621 						ident = ld_targ.t_id.id_null;
622 						done = 1;
623 						break;
624 					}
625 					if ((ld_targ.t_m.m_sht_unwind ==
626 					    SHT_PROGBITS)&&
627 					    (strcmp(name,
628 					    MSG_ORIG(MSG_SCN_GCC_X_TBL)) ==
629 					    0)) {
630 						ident = ld_targ.t_id.id_unwind;
631 						done = 1;
632 						break;
633 					}
634 					break;
635 				case 'p':
636 					if (strcmp(name,
637 					    MSG_ORIG(MSG_SCN_PLT)) == 0) {
638 						ident = ld_targ.t_id.id_null;
639 						done = 1;
640 					}
641 					break;
642 				}
643 			}
644 			if (!done) {
645 				if (stab_index) {
646 					/*
647 					 * This is a work-around for x86
648 					 * compilers that have set SHF_ALLOC
649 					 * for the .stab.index section.
650 					 *
651 					 * Because of this, make sure that the
652 					 * .stab.index does not end up as the
653 					 * last section in the text segment.
654 					 * Older linkers can produce
655 					 * segmentation violations when they
656 					 * strip (ld -s) against a shared
657 					 * object whose last section in the
658 					 * text segment is a .stab.
659 					 */
660 					ident = ld_targ.t_id.id_interp;
661 				} else {
662 					ident = ld_targ.t_id.id_data;
663 				}
664 			}
665 		} else
666 			ident = ld_targ.t_id.id_note;
667 	}
668 
669 	r = process_section(name, ifl, shdr, scn, ndx, ident, ofl);
670 
671 	/*
672 	 * On success, process_section() creates an input section descriptor.
673 	 * Now that it exists, we can add any pending input section flags.
674 	 */
675 	if ((is_flags != 0) && (r == 1))
676 		ifl->ifl_isdesc[ndx]->is_flags |= is_flags;
677 
678 	return (r);
679 }
680 
681 /*
682  * Handles the SHT_SUNW_{DEBUG,DEBUGSTR) sections.
683  */
684 static uintptr_t
685 process_debug(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
686     Word ndx, int ident, Ofl_desc *ofl)
687 {
688 	/*
689 	 * Debug information is discarded when the 'ld -s' flag is invoked.
690 	 */
691 	if (ofl->ofl_flags & FLG_OF_STRIP) {
692 		return (1);
693 	}
694 	return (process_progbits(name, ifl, shdr, scn, ndx, ident, ofl));
695 }
696 
697 /*
698  * Process a nobits section.
699  */
700 static uintptr_t
701 process_nobits(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
702     Word ndx, int ident, Ofl_desc *ofl)
703 {
704 	if (ident) {
705 		if (shdr->sh_flags & SHF_TLS)
706 			ident = ld_targ.t_id.id_tlsbss;
707 #if	defined(_ELF64)
708 		else if ((shdr->sh_flags & SHF_AMD64_LARGE) &&
709 		    (ld_targ.t_m.m_mach == EM_AMD64))
710 			ident = ld_targ.t_id.id_lbss;
711 #endif
712 		else
713 			ident = ld_targ.t_id.id_bss;
714 	}
715 	return (process_section(name, ifl, shdr, scn, ndx, ident, ofl));
716 }
717 
718 /*
719  * Process a SHT_*_ARRAY section.
720  */
721 static uintptr_t
722 process_array(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
723     Word ndx, int ident, Ofl_desc *ofl)
724 {
725 	uintptr_t	error;
726 
727 	if (ident)
728 		ident = ld_targ.t_id.id_array;
729 
730 	error = process_section(name, ifl, shdr, scn, ndx, ident, ofl);
731 	if ((error == 0) || (error == S_ERROR))
732 		return (error);
733 
734 	return (1);
735 }
736 
737 static uintptr_t
738 /* ARGSUSED1 */
739 array_process(Is_desc *isc, Ifl_desc *ifl, Ofl_desc *ofl)
740 {
741 	Os_desc	*osp;
742 	Shdr	*shdr;
743 
744 	if ((isc == NULL) || ((osp = isc->is_osdesc) == NULL))
745 		return (0);
746 
747 	shdr = isc->is_shdr;
748 
749 	if ((shdr->sh_type == SHT_FINI_ARRAY) &&
750 	    (ofl->ofl_osfiniarray == NULL))
751 		ofl->ofl_osfiniarray = osp;
752 	else if ((shdr->sh_type == SHT_INIT_ARRAY) &&
753 	    (ofl->ofl_osinitarray == NULL))
754 		ofl->ofl_osinitarray = osp;
755 	else if ((shdr->sh_type == SHT_PREINIT_ARRAY) &&
756 	    (ofl->ofl_ospreinitarray == NULL))
757 		ofl->ofl_ospreinitarray = osp;
758 
759 	return (1);
760 }
761 
762 /*
763  * Process a SHT_SYMTAB_SHNDX section.
764  */
765 static uintptr_t
766 process_sym_shndx(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
767     Word ndx, int ident, Ofl_desc *ofl)
768 {
769 	if (process_input(name, ifl, shdr, scn, ndx, ident, ofl) == S_ERROR)
770 		return (S_ERROR);
771 
772 	/*
773 	 * Have we already seen the related SYMTAB - if so verify it now.
774 	 */
775 	if (shdr->sh_link < ndx) {
776 		Is_desc	*isp = ifl->ifl_isdesc[shdr->sh_link];
777 
778 		if ((isp == 0) || ((isp->is_shdr->sh_type != SHT_SYMTAB) &&
779 		    (isp->is_shdr->sh_type != SHT_DYNSYM))) {
780 			eprintf(ofl->ofl_lml, ERR_FATAL,
781 			    MSG_INTL(MSG_FIL_INVSHLINK), ifl->ifl_name, name,
782 			    EC_XWORD(shdr->sh_link));
783 			return (S_ERROR);
784 		}
785 		isp->is_symshndx = ifl->ifl_isdesc[ndx];
786 	}
787 	return (1);
788 }
789 
790 /*
791  * Final processing for SHT_SYMTAB_SHNDX section.
792  */
793 static uintptr_t
794 /* ARGSUSED2 */
795 sym_shndx_process(Is_desc *isc, Ifl_desc *ifl, Ofl_desc *ofl)
796 {
797 	if (isc->is_shdr->sh_link > isc->is_scnndx) {
798 		Is_desc	*isp = ifl->ifl_isdesc[isc->is_shdr->sh_link];
799 
800 		if ((isp == 0) || ((isp->is_shdr->sh_type != SHT_SYMTAB) &&
801 		    (isp->is_shdr->sh_type != SHT_DYNSYM))) {
802 			eprintf(ofl->ofl_lml, ERR_FATAL,
803 			    MSG_INTL(MSG_FIL_INVSHLINK), isc->is_file->ifl_name,
804 			    isc->is_name, EC_XWORD(isc->is_shdr->sh_link));
805 			return (S_ERROR);
806 		}
807 		isp->is_symshndx = isc;
808 	}
809 	return (1);
810 }
811 
812 /*
813  * Process .dynamic section from a relocatable object.
814  *
815  * Note: That the .dynamic section is only considered interesting when
816  *	 dlopen()ing a relocatable object (thus FLG_OF1_RELDYN can only get
817  *	 set when libld is called from ld.so.1).
818  */
819 /*ARGSUSED*/
820 static uintptr_t
821 process_rel_dynamic(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
822     Word ndx, int ident, Ofl_desc *ofl)
823 {
824 	Dyn		*dyn;
825 	Elf_Scn		*strscn;
826 	Elf_Data	*dp;
827 	char		*str;
828 
829 	/*
830 	 * Process .dynamic sections from relocatable objects ?
831 	 */
832 	if ((ofl->ofl_flags1 & FLG_OF1_RELDYN) == 0)
833 		return (1);
834 
835 	/*
836 	 * Find the string section associated with the .dynamic section.
837 	 */
838 	if ((strscn = elf_getscn(ifl->ifl_elf, shdr->sh_link)) == NULL) {
839 		eprintf(ofl->ofl_lml, ERR_ELF, MSG_INTL(MSG_ELF_GETSCN),
840 		    ifl->ifl_name);
841 		ofl->ofl_flags |= FLG_OF_FATAL;
842 		return (0);
843 	}
844 	dp = elf_getdata(strscn, NULL);
845 	str = (char *)dp->d_buf;
846 
847 	/*
848 	 * And get the .dynamic data
849 	 */
850 	dp = elf_getdata(scn, NULL);
851 
852 	for (dyn = (Dyn *)dp->d_buf; dyn->d_tag != DT_NULL; dyn++) {
853 		Ifl_desc	*difl;
854 
855 		switch (dyn->d_tag) {
856 		case DT_NEEDED:
857 		case DT_USED:
858 			if (((difl = libld_calloc(1,
859 			    sizeof (Ifl_desc))) == NULL) ||
860 			    (aplist_append(&ofl->ofl_sos, difl,
861 			    AL_CNT_OFL_LIBS) == NULL))
862 				return (S_ERROR);
863 
864 			difl->ifl_name = MSG_ORIG(MSG_STR_DYNAMIC);
865 			difl->ifl_soname = str + (size_t)dyn->d_un.d_val;
866 			difl->ifl_flags = FLG_IF_NEEDSTR;
867 			break;
868 		case DT_RPATH:
869 		case DT_RUNPATH:
870 			if ((ofl->ofl_rpath = add_string(ofl->ofl_rpath,
871 			    (str + (size_t)dyn->d_un.d_val))) ==
872 			    (const char *)S_ERROR)
873 				return (S_ERROR);
874 			break;
875 		case DT_VERSYM:
876 			/*
877 			 * The Solaris ld does not put DT_VERSYM in the
878 			 * dynamic section. If the object has DT_VERSYM,
879 			 * then it must have been produced by the GNU ld,
880 			 * and is using the GNU style of versioning.
881 			 */
882 			ifl->ifl_flags |= FLG_IF_GNUVER;
883 			break;
884 		}
885 	}
886 	return (1);
887 }
888 
889 /*
890  * Expand implicit references.  Dependencies can be specified in terms of the
891  * $ORIGIN, $PLATFORM, $OSREL and $OSNAME tokens, either from their needed name,
892  * or via a runpath.  In addition runpaths may also specify the $ISALIST token.
893  *
894  * Probably the most common reference to explicit dependencies (via -L) will be
895  * sufficient to find any associated implicit dependencies, but just in case we
896  * expand any occurrence of these known tokens here.
897  *
898  * Note, if any errors occur we simply return the original name.
899  *
900  * This code is remarkably similar to expand() in rtld/common/paths.c.
901  */
902 static char		*platform = 0;
903 static size_t		platform_sz = 0;
904 static Isa_desc		*isa = 0;
905 static Uts_desc		*uts = 0;
906 
907 static char *
908 expand(const char *parent, const char *name, char **next)
909 {
910 	char		_name[PATH_MAX], *nptr, *_next;
911 	const char	*optr;
912 	size_t		nrem = PATH_MAX - 1;
913 	int		expanded = 0, _expanded, isaflag = 0;
914 
915 	optr = name;
916 	nptr = _name;
917 
918 	while (*optr) {
919 		if (nrem == 0)
920 			return ((char *)name);
921 
922 		if (*optr != '$') {
923 			*nptr++ = *optr++, nrem--;
924 			continue;
925 		}
926 
927 		_expanded = 0;
928 
929 		if (strncmp(optr, MSG_ORIG(MSG_STR_ORIGIN),
930 		    MSG_STR_ORIGIN_SIZE) == 0) {
931 			char *eptr;
932 
933 			/*
934 			 * For $ORIGIN, expansion is really just a concatenation
935 			 * of the parents directory name.  For example, an
936 			 * explicit dependency foo/bar/lib1.so with a dependency
937 			 * on $ORIGIN/lib2.so would be expanded to
938 			 * foo/bar/lib2.so.
939 			 */
940 			if ((eptr = strrchr(parent, '/')) == 0) {
941 				*nptr++ = '.';
942 				nrem--;
943 			} else {
944 				size_t	len = eptr - parent;
945 
946 				if (len >= nrem)
947 					return ((char *)name);
948 
949 				(void) strncpy(nptr, parent, len);
950 				nptr = nptr + len;
951 				nrem -= len;
952 			}
953 			optr += MSG_STR_ORIGIN_SIZE;
954 			expanded = _expanded = 1;
955 
956 		} else if (strncmp(optr, MSG_ORIG(MSG_STR_PLATFORM),
957 		    MSG_STR_PLATFORM_SIZE) == 0) {
958 			/*
959 			 * Establish the platform from sysconf - like uname -i.
960 			 */
961 			if ((platform == 0) && (platform_sz == 0)) {
962 				char	info[SYS_NMLN];
963 				long	size;
964 
965 				size = sysinfo(SI_PLATFORM, info, SYS_NMLN);
966 				if ((size != -1) &&
967 				    (platform = libld_malloc((size_t)size))) {
968 					(void) strcpy(platform, info);
969 					platform_sz = (size_t)size - 1;
970 				} else
971 					platform_sz = 1;
972 			}
973 			if (platform != 0) {
974 				if (platform_sz >= nrem)
975 					return ((char *)name);
976 
977 				(void) strncpy(nptr, platform, platform_sz);
978 				nptr = nptr + platform_sz;
979 				nrem -= platform_sz;
980 
981 				optr += MSG_STR_PLATFORM_SIZE;
982 				expanded = _expanded = 1;
983 			}
984 
985 		} else if (strncmp(optr, MSG_ORIG(MSG_STR_OSNAME),
986 		    MSG_STR_OSNAME_SIZE) == 0) {
987 			/*
988 			 * Establish the os name - like uname -s.
989 			 */
990 			if (uts == 0)
991 				uts = conv_uts();
992 
993 			if (uts && uts->uts_osnamesz) {
994 				if (uts->uts_osnamesz >= nrem)
995 					return ((char *)name);
996 
997 				(void) strncpy(nptr, uts->uts_osname,
998 				    uts->uts_osnamesz);
999 				nptr = nptr + uts->uts_osnamesz;
1000 				nrem -= uts->uts_osnamesz;
1001 
1002 				optr += MSG_STR_OSNAME_SIZE;
1003 				expanded = _expanded = 1;
1004 			}
1005 
1006 		} else if (strncmp(optr, MSG_ORIG(MSG_STR_OSREL),
1007 		    MSG_STR_OSREL_SIZE) == 0) {
1008 			/*
1009 			 * Establish the os release - like uname -r.
1010 			 */
1011 			if (uts == 0)
1012 				uts = conv_uts();
1013 
1014 			if (uts && uts->uts_osrelsz) {
1015 				if (uts->uts_osrelsz >= nrem)
1016 					return ((char *)name);
1017 
1018 				(void) strncpy(nptr, uts->uts_osrel,
1019 				    uts->uts_osrelsz);
1020 				nptr = nptr + uts->uts_osrelsz;
1021 				nrem -= uts->uts_osrelsz;
1022 
1023 				optr += MSG_STR_OSREL_SIZE;
1024 				expanded = _expanded = 1;
1025 			}
1026 
1027 		} else if ((strncmp(optr, MSG_ORIG(MSG_STR_ISALIST),
1028 		    MSG_STR_ISALIST_SIZE) == 0) && next && (isaflag++ == 0)) {
1029 			/*
1030 			 * Establish instruction sets from sysconf.  Note that
1031 			 * this is only meaningful from runpaths.
1032 			 */
1033 			if (isa == 0)
1034 				isa = conv_isalist();
1035 
1036 			if (isa && isa->isa_listsz &&
1037 			    (nrem > isa->isa_opt->isa_namesz)) {
1038 				size_t		mlen, tlen, hlen = optr - name;
1039 				size_t		no;
1040 				char		*lptr;
1041 				Isa_opt		*opt = isa->isa_opt;
1042 
1043 				(void) strncpy(nptr, opt->isa_name,
1044 				    opt->isa_namesz);
1045 				nptr = nptr + opt->isa_namesz;
1046 				nrem -= opt->isa_namesz;
1047 
1048 				optr += MSG_STR_ISALIST_SIZE;
1049 				expanded = _expanded = 1;
1050 
1051 				tlen = strlen(optr);
1052 
1053 				/*
1054 				 * As ISALIST expands to a number of elements,
1055 				 * establish a new list to return to the caller.
1056 				 * This will contain the present path being
1057 				 * processed redefined for each isalist option,
1058 				 * plus the original remaining list entries.
1059 				 */
1060 				mlen = ((hlen + tlen) * (isa->isa_optno - 1)) +
1061 				    isa->isa_listsz - opt->isa_namesz;
1062 				if (*next)
1063 					mlen += strlen(*next);
1064 				if ((_next = lptr = libld_malloc(mlen)) == 0)
1065 					return (0);
1066 
1067 				for (no = 1, opt++; no < isa->isa_optno;
1068 				    no++, opt++) {
1069 					(void) strncpy(lptr, name, hlen);
1070 					lptr = lptr + hlen;
1071 					(void) strncpy(lptr, opt->isa_name,
1072 					    opt->isa_namesz);
1073 					lptr = lptr + opt->isa_namesz;
1074 					(void) strncpy(lptr, optr, tlen);
1075 					lptr = lptr + tlen;
1076 					*lptr++ = ':';
1077 				}
1078 				if (*next)
1079 					(void) strcpy(lptr, *next);
1080 				else
1081 					*--lptr = '\0';
1082 			}
1083 		}
1084 
1085 		/*
1086 		 * If no expansion occurred skip the $ and continue.
1087 		 */
1088 		if (_expanded == 0)
1089 			*nptr++ = *optr++, nrem--;
1090 	}
1091 
1092 	/*
1093 	 * If any ISALIST processing has occurred not only do we return the
1094 	 * expanded node we're presently working on, but we must also update the
1095 	 * remaining list so that it is effectively prepended with this node
1096 	 * expanded to all remaining isalist options.  Note that we can only
1097 	 * handle one ISALIST per node.  For more than one ISALIST to be
1098 	 * processed we'd need a better algorithm than above to replace the
1099 	 * newly generated list.  Whether we want to encourage the number of
1100 	 * pathname permutations this would provide is another question. So, for
1101 	 * now if more than one ISALIST is encountered we return the original
1102 	 * node untouched.
1103 	 */
1104 	if (isaflag) {
1105 		if (isaflag == 1)
1106 			*next = _next;
1107 		else
1108 			return ((char *)name);
1109 	}
1110 
1111 	*nptr = '\0';
1112 
1113 	if (expanded) {
1114 		if ((nptr = libld_malloc(strlen(_name) + 1)) == 0)
1115 			return ((char *)name);
1116 		(void) strcpy(nptr, _name);
1117 		return (nptr);
1118 	}
1119 	return ((char *)name);
1120 }
1121 
1122 /*
1123  * The Solaris ld does not put DT_VERSYM in the dynamic section, but the
1124  * GNU ld does, and it is used by the runtime linker to implement their
1125  * versioning scheme. Use this fact to determine if the sharable object
1126  * was produced by the GNU ld rather than the Solaris one, and to set
1127  * FLG_IF_GNUVER if so. This needs to be done before the symbols are
1128  * processed, since the answer determines whether we interpret the
1129  * symbols versions according to Solaris or GNU rules.
1130  */
1131 /*ARGSUSED*/
1132 static uintptr_t
1133 process_dynamic_isgnu(const char *name, Ifl_desc *ifl, Shdr *shdr,
1134     Elf_Scn *scn, Word ndx, int ident, Ofl_desc *ofl)
1135 {
1136 	Dyn		*dyn;
1137 	Elf_Data	*dp;
1138 	uintptr_t	error;
1139 
1140 	error = process_section(name, ifl, shdr, scn, ndx, ident, ofl);
1141 	if ((error == 0) || (error == S_ERROR))
1142 		return (error);
1143 
1144 	/* Get the .dynamic data */
1145 	dp = elf_getdata(scn, NULL);
1146 
1147 	for (dyn = (Dyn *)dp->d_buf; dyn->d_tag != DT_NULL; dyn++) {
1148 		if (dyn->d_tag == DT_VERSYM) {
1149 			ifl->ifl_flags |= FLG_IF_GNUVER;
1150 			break;
1151 		}
1152 	}
1153 	return (1);
1154 }
1155 
1156 /*
1157  * Process a dynamic section.  If we are processing an explicit shared object
1158  * then we need to determine if it has a recorded SONAME, if so, this name will
1159  * be recorded in the output file being generated as the NEEDED entry rather
1160  * than the shared objects filename itself.
1161  * If the mode of the link-edit indicates that no undefined symbols should
1162  * remain, then we also need to build up a list of any additional shared object
1163  * dependencies this object may have.  In this case save any NEEDED entries
1164  * together with any associated run-path specifications.  This information is
1165  * recorded on the `ofl_soneed' list and will be analyzed after all explicit
1166  * file processing has been completed (refer finish_libs()).
1167  */
1168 static uintptr_t
1169 process_dynamic(Is_desc *isc, Ifl_desc *ifl, Ofl_desc *ofl)
1170 {
1171 	Dyn		*data, *dyn;
1172 	char		*str, *rpath = NULL;
1173 	const char	*soname, *needed;
1174 
1175 	data = (Dyn *)isc->is_indata->d_buf;
1176 	str = (char *)ifl->ifl_isdesc[isc->is_shdr->sh_link]->is_indata->d_buf;
1177 
1178 	/*
1179 	 * First loop through the dynamic section looking for a run path.
1180 	 */
1181 	if (ofl->ofl_flags & (FLG_OF_NOUNDEF | FLG_OF_SYMBOLIC)) {
1182 		for (dyn = data; dyn->d_tag != DT_NULL; dyn++) {
1183 			if ((dyn->d_tag != DT_RPATH) &&
1184 			    (dyn->d_tag != DT_RUNPATH))
1185 				continue;
1186 			if ((rpath = str + (size_t)dyn->d_un.d_val) == NULL)
1187 				continue;
1188 			break;
1189 		}
1190 	}
1191 
1192 	/*
1193 	 * Now look for any needed dependencies (which may use the rpath)
1194 	 * or a new SONAME.
1195 	 */
1196 	for (dyn = data; dyn->d_tag != DT_NULL; dyn++) {
1197 		if (dyn->d_tag == DT_SONAME) {
1198 			if ((soname = str + (size_t)dyn->d_un.d_val) == NULL)
1199 				continue;
1200 
1201 			/*
1202 			 * Update the input file structure with this new name.
1203 			 */
1204 			ifl->ifl_soname = soname;
1205 
1206 		} else if ((dyn->d_tag == DT_NEEDED) ||
1207 		    (dyn->d_tag == DT_USED)) {
1208 			Sdf_desc	*sdf;
1209 
1210 			if (!(ofl->ofl_flags &
1211 			    (FLG_OF_NOUNDEF | FLG_OF_SYMBOLIC)))
1212 				continue;
1213 			if ((needed = str + (size_t)dyn->d_un.d_val) == NULL)
1214 				continue;
1215 
1216 			/*
1217 			 * Determine if this needed entry is already recorded on
1218 			 * the shared object needed list, if not create a new
1219 			 * definition for later processing (see finish_libs()).
1220 			 */
1221 			needed = expand(ifl->ifl_name, needed, NULL);
1222 
1223 			if ((sdf = sdf_find(needed, ofl->ofl_soneed)) == NULL) {
1224 				if ((sdf = sdf_add(needed,
1225 				    &ofl->ofl_soneed)) == (Sdf_desc *)S_ERROR)
1226 					return (S_ERROR);
1227 				sdf->sdf_rfile = ifl->ifl_name;
1228 			}
1229 
1230 			/*
1231 			 * Record the runpath (Note that we take the first
1232 			 * runpath which is exactly what ld.so.1 would do during
1233 			 * its dependency processing).
1234 			 */
1235 			if (rpath && (sdf->sdf_rpath == 0))
1236 				sdf->sdf_rpath = rpath;
1237 
1238 		} else if (dyn->d_tag == DT_FLAGS_1) {
1239 			if (dyn->d_un.d_val & (DF_1_INITFIRST | DF_1_INTERPOSE))
1240 				ifl->ifl_flags &= ~FLG_IF_LAZYLD;
1241 			if (dyn->d_un.d_val & DF_1_DISPRELPND)
1242 				ifl->ifl_flags |= FLG_IF_DISPPEND;
1243 			if (dyn->d_un.d_val & DF_1_DISPRELDNE)
1244 				ifl->ifl_flags |= FLG_IF_DISPDONE;
1245 			if (dyn->d_un.d_val & DF_1_NODIRECT)
1246 				ifl->ifl_flags |= FLG_IF_NODIRECT;
1247 
1248 		} else if ((dyn->d_tag == DT_AUDIT) &&
1249 		    (ifl->ifl_flags & FLG_IF_NEEDED)) {
1250 			/*
1251 			 * Record audit string as DT_DEPAUDIT.
1252 			 */
1253 			if ((ofl->ofl_depaudit = add_string(ofl->ofl_depaudit,
1254 			    (str + (size_t)dyn->d_un.d_val))) ==
1255 			    (const char *)S_ERROR)
1256 				return (S_ERROR);
1257 
1258 		} else if (dyn->d_tag == DT_SUNW_RTLDINF) {
1259 			/*
1260 			 * If a library has the SUNW_RTLDINF .dynamic entry
1261 			 * then we must not permit lazyloading of this library.
1262 			 * This is because critical startup information (TLS
1263 			 * routines) are provided as part of these interfaces
1264 			 * and we must have them as part of process startup.
1265 			 */
1266 			ifl->ifl_flags &= ~FLG_IF_LAZYLD;
1267 		}
1268 	}
1269 
1270 	/*
1271 	 * Perform some SONAME sanity checks.
1272 	 */
1273 	if (ifl->ifl_flags & FLG_IF_NEEDED) {
1274 		Ifl_desc	*sifl;
1275 		Aliste		idx;
1276 
1277 		/*
1278 		 * Determine if anyone else will cause the same SONAME to be
1279 		 * used (this is either caused by two different files having the
1280 		 * same SONAME, or by one file SONAME actually matching another
1281 		 * file basename (if no SONAME is specified within a shared
1282 		 * library its basename will be used)). Probably rare, but some
1283 		 * idiot will do it.
1284 		 */
1285 		for (APLIST_TRAVERSE(ofl->ofl_sos, idx, sifl)) {
1286 			if ((strcmp(ifl->ifl_soname, sifl->ifl_soname) == 0) &&
1287 			    (ifl != sifl)) {
1288 				const char	*hint, *iflb, *siflb;
1289 
1290 				/*
1291 				 * Determine the basename of each file. Perhaps
1292 				 * there are multiple copies of the same file
1293 				 * being brought in using different -L search
1294 				 * paths, and if so give an extra hint in the
1295 				 * error message.
1296 				 */
1297 				iflb = strrchr(ifl->ifl_name, '/');
1298 				if (iflb == NULL)
1299 					iflb = ifl->ifl_name;
1300 				else
1301 					iflb++;
1302 
1303 				siflb = strrchr(sifl->ifl_name, '/');
1304 				if (siflb == NULL)
1305 					siflb = sifl->ifl_name;
1306 				else
1307 					siflb++;
1308 
1309 				if (strcmp(iflb, siflb) == 0)
1310 					hint = MSG_INTL(MSG_REC_CNFLTHINT);
1311 				else
1312 					hint = MSG_ORIG(MSG_STR_EMPTY);
1313 
1314 				eprintf(ofl->ofl_lml, ERR_FATAL,
1315 				    MSG_INTL(MSG_REC_OBJCNFLT), sifl->ifl_name,
1316 				    ifl->ifl_name, sifl->ifl_soname, hint);
1317 				ofl->ofl_flags |= FLG_OF_FATAL;
1318 				return (0);
1319 			}
1320 		}
1321 
1322 		/*
1323 		 * If the SONAME is the same as the name the user wishes to
1324 		 * record when building a dynamic library (refer -h option),
1325 		 * we also have a name clash.
1326 		 */
1327 		if (ofl->ofl_soname &&
1328 		    (strcmp(ofl->ofl_soname, ifl->ifl_soname) == 0)) {
1329 			eprintf(ofl->ofl_lml, ERR_FATAL,
1330 			    MSG_INTL(MSG_REC_OPTCNFLT), ifl->ifl_name,
1331 			    MSG_INTL(MSG_MARG_SONAME), ifl->ifl_soname);
1332 			ofl->ofl_flags |= FLG_OF_FATAL;
1333 			return (0);
1334 		}
1335 	}
1336 	return (1);
1337 }
1338 
1339 /*
1340  * Process a progbits section from a relocatable object (ET_REL).
1341  * This is used on non-amd64 objects to recognize .eh_frame sections.
1342  */
1343 /*ARGSUSED1*/
1344 static uintptr_t
1345 process_progbits_final(Is_desc *isc, Ifl_desc *ifl, Ofl_desc *ofl)
1346 {
1347 	if (isc->is_osdesc && (isc->is_flags & FLG_IS_EHFRAME) &&
1348 	    (ld_unwind_register(isc->is_osdesc, ofl) == S_ERROR))
1349 		return (S_ERROR);
1350 
1351 	return (1);
1352 }
1353 
1354 /*
1355  * Process a group section.
1356  */
1357 static uintptr_t
1358 process_group(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
1359     Word ndx, int ident, Ofl_desc *ofl)
1360 {
1361 	uintptr_t	error;
1362 
1363 	error = process_section(name, ifl, shdr, scn, ndx, ident, ofl);
1364 	if ((error == 0) || (error == S_ERROR))
1365 		return (error);
1366 
1367 	/*
1368 	 * Indicate that this input file has groups to process.  Groups are
1369 	 * processed after all input sections have been processed.
1370 	 */
1371 	ifl->ifl_flags |= FLG_IS_GROUPS;
1372 
1373 	return (1);
1374 }
1375 
1376 /*
1377  * Process a relocation entry. At this point all input sections from this
1378  * input file have been assigned an input section descriptor which is saved
1379  * in the `ifl_isdesc' array.
1380  */
1381 static uintptr_t
1382 rel_process(Is_desc *isc, Ifl_desc *ifl, Ofl_desc *ofl)
1383 {
1384 	Word 	rndx;
1385 	Is_desc	*risc;
1386 	Os_desc	*osp;
1387 	Shdr	*shdr = isc->is_shdr;
1388 	Conv_inv_buf_t inv_buf;
1389 
1390 	/*
1391 	 * Make sure this is a valid relocation we can handle.
1392 	 */
1393 	if (shdr->sh_type != ld_targ.t_m.m_rel_sht_type) {
1394 		eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_FIL_INVALSEC),
1395 		    ifl->ifl_name, isc->is_name,
1396 		    conv_sec_type(ifl->ifl_ehdr->e_machine,
1397 		    shdr->sh_type, 0, &inv_buf));
1398 		ofl->ofl_flags |= FLG_OF_FATAL;
1399 		return (0);
1400 	}
1401 
1402 	/*
1403 	 * From the relocation section header information determine which
1404 	 * section needs the actual relocation.  Determine which output section
1405 	 * this input section has been assigned to and add to its relocation
1406 	 * list.  Note that the relocation section may be null if it is not
1407 	 * required (ie. .debug, .stabs, etc).
1408 	 */
1409 	rndx = shdr->sh_info;
1410 	if (rndx >= ifl->ifl_shnum) {
1411 		/*
1412 		 * Broken input file.
1413 		 */
1414 		eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_FIL_INVSHINFO),
1415 		    ifl->ifl_name, isc->is_name, EC_XWORD(rndx));
1416 		ofl->ofl_flags |= FLG_OF_FATAL;
1417 		return (0);
1418 	}
1419 	if (rndx == 0) {
1420 		if (aplist_append(&ofl->ofl_extrarels, isc,
1421 		    AL_CNT_OFL_RELS) == NULL)
1422 			return (S_ERROR);
1423 
1424 	} else if ((risc = ifl->ifl_isdesc[rndx]) != NULL) {
1425 		/*
1426 		 * Discard relocations if they are against a section
1427 		 * which has been discarded.
1428 		 */
1429 		if (risc->is_flags & FLG_IS_DISCARD)
1430 			return (1);
1431 
1432 		if ((osp = risc->is_osdesc) == NULL) {
1433 			if (risc->is_shdr->sh_type == SHT_SUNW_move) {
1434 				/*
1435 				 * This section is processed later in
1436 				 * process_movereloc().
1437 				 */
1438 				if (aplist_append(&ofl->ofl_ismoverel,
1439 				    isc, AL_CNT_OFL_MOVE) == NULL)
1440 					return (S_ERROR);
1441 				return (1);
1442 			}
1443 			eprintf(ofl->ofl_lml, ERR_FATAL,
1444 			    MSG_INTL(MSG_FIL_INVRELOC1), ifl->ifl_name,
1445 			    isc->is_name, risc->is_name);
1446 			return (0);
1447 		}
1448 		if (aplist_append(&osp->os_relisdescs, isc,
1449 		    AL_CNT_OS_RELISDESCS) == NULL)
1450 			return (S_ERROR);
1451 	}
1452 	return (1);
1453 }
1454 
1455 /*
1456  * SHF_EXCLUDE flags is set for this section.
1457  */
1458 static uintptr_t
1459 process_exclude(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
1460     Word ndx, Ofl_desc *ofl)
1461 {
1462 	/*
1463 	 * Sections SHT_SYMTAB and SHT_DYNDYM, even if SHF_EXCLUDE is on, might
1464 	 * be needed for ld processing.  These sections need to be in the
1465 	 * internal table.  Later it will be determined whether they can be
1466 	 * eliminated or not.
1467 	 */
1468 	if (shdr->sh_type == SHT_SYMTAB || shdr->sh_type == SHT_DYNSYM)
1469 		return (0);
1470 
1471 	/*
1472 	 * Other checks
1473 	 */
1474 	if (shdr->sh_flags & SHF_ALLOC) {
1475 		/*
1476 		 * A conflict, issue an warning message, and ignore the section.
1477 		 */
1478 		eprintf(ofl->ofl_lml, ERR_WARNING, MSG_INTL(MSG_FIL_EXCLUDE),
1479 		    ifl->ifl_name, name);
1480 		return (0);
1481 	}
1482 
1483 	/*
1484 	 * This sections is not going to the output file.
1485 	 */
1486 	return (process_section(name, ifl, shdr, scn, ndx, 0, ofl));
1487 }
1488 
1489 /*
1490  * Section processing state table.  `Initial' describes the required initial
1491  * procedure to be called (if any), `Final' describes the final processing
1492  * procedure (ie. things that can only be done when all required sections
1493  * have been collected).
1494  */
1495 typedef uintptr_t	(* initial_func_t)(const char *, Ifl_desc *, Shdr *,
1496 			    Elf_Scn *, Word, int, Ofl_desc *);
1497 
1498 static initial_func_t Initial[SHT_NUM][2] = {
1499 /*			ET_REL			ET_DYN			*/
1500 
1501 /* SHT_NULL	*/	invalid_section,	invalid_section,
1502 /* SHT_PROGBITS	*/	process_progbits,	process_progbits,
1503 /* SHT_SYMTAB	*/	process_input,		process_input,
1504 /* SHT_STRTAB	*/	process_strtab,		process_strtab,
1505 /* SHT_RELA	*/	process_reloc,		process_reloc,
1506 /* SHT_HASH	*/	invalid_section,	NULL,
1507 /* SHT_DYNAMIC	*/	process_rel_dynamic,	process_dynamic_isgnu,
1508 /* SHT_NOTE	*/	process_section,	NULL,
1509 /* SHT_NOBITS	*/	process_nobits,		process_nobits,
1510 /* SHT_REL	*/	process_reloc,		process_reloc,
1511 /* SHT_SHLIB	*/	process_section,	invalid_section,
1512 /* SHT_DYNSYM	*/	invalid_section,	process_input,
1513 /* SHT_UNKNOWN12 */	process_progbits,	process_progbits,
1514 /* SHT_UNKNOWN13 */	process_progbits,	process_progbits,
1515 /* SHT_INIT_ARRAY */	process_array,		NULL,
1516 /* SHT_FINI_ARRAY */	process_array,		NULL,
1517 /* SHT_PREINIT_ARRAY */	process_array,		NULL,
1518 /* SHT_GROUP */		process_group,		invalid_section,
1519 /* SHT_SYMTAB_SHNDX */	process_sym_shndx,	NULL
1520 };
1521 
1522 typedef uintptr_t	(* final_func_t)(Is_desc *, Ifl_desc *, Ofl_desc *);
1523 
1524 static final_func_t Final[SHT_NUM][2] = {
1525 /*			ET_REL			ET_DYN			*/
1526 
1527 /* SHT_NULL	*/	NULL,			NULL,
1528 /* SHT_PROGBITS	*/	process_progbits_final,	NULL,
1529 /* SHT_SYMTAB	*/	ld_sym_process,		ld_sym_process,
1530 /* SHT_STRTAB	*/	NULL,			NULL,
1531 /* SHT_RELA	*/	rel_process,		NULL,
1532 /* SHT_HASH	*/	NULL,			NULL,
1533 /* SHT_DYNAMIC	*/	NULL,			process_dynamic,
1534 /* SHT_NOTE	*/	NULL,			NULL,
1535 /* SHT_NOBITS	*/	NULL,			NULL,
1536 /* SHT_REL	*/	rel_process,		NULL,
1537 /* SHT_SHLIB	*/	NULL,			NULL,
1538 /* SHT_DYNSYM	*/	NULL,			ld_sym_process,
1539 /* SHT_UNKNOWN12 */	NULL,			NULL,
1540 /* SHT_UNKNOWN13 */	NULL,			NULL,
1541 /* SHT_INIT_ARRAY */	array_process,		NULL,
1542 /* SHT_FINI_ARRAY */	array_process,		NULL,
1543 /* SHT_PREINIT_ARRAY */	array_process,		NULL,
1544 /* SHT_GROUP */		NULL,			NULL,
1545 /* SHT_SYMTAB_SHNDX */	sym_shndx_process,	NULL
1546 };
1547 
1548 #define	MAXNDXSIZE	10
1549 
1550 /*
1551  * Process an elf file.  Each section is compared against the section state
1552  * table to determine whether it should be processed (saved), ignored, or
1553  * is invalid for the type of input file being processed.
1554  */
1555 static uintptr_t
1556 process_elf(Ifl_desc *ifl, Elf *elf, Ofl_desc *ofl)
1557 {
1558 	Elf_Scn		*scn;
1559 	Shdr		*shdr;
1560 	Word		ndx, sndx, ordndx = 0, ordcnt = 0;
1561 	char		*str, *name, _name[MAXNDXSIZE];
1562 	Word		row, column;
1563 	int		ident;
1564 	uintptr_t	error;
1565 	Is_desc		*vdfisp, *vndisp, *vsyisp, *sifisp, *capisp;
1566 	Sdf_desc	*sdf;
1567 
1568 	/*
1569 	 * First process the .shstrtab section so that later sections can
1570 	 * reference their name.
1571 	 */
1572 	ld_sup_file(ofl, ifl->ifl_name, elf_kind(elf), ifl->ifl_flags, elf);
1573 
1574 	sndx = ifl->ifl_shstrndx;
1575 	if ((scn = elf_getscn(elf, (size_t)sndx)) == NULL) {
1576 		eprintf(ofl->ofl_lml, ERR_ELF, MSG_INTL(MSG_ELF_GETSCN),
1577 		    ifl->ifl_name);
1578 		ofl->ofl_flags |= FLG_OF_FATAL;
1579 		return (0);
1580 	}
1581 	if ((shdr = elf_getshdr(scn)) == NULL) {
1582 		eprintf(ofl->ofl_lml, ERR_ELF, MSG_INTL(MSG_ELF_GETSHDR),
1583 		    ifl->ifl_name);
1584 		ofl->ofl_flags |= FLG_OF_FATAL;
1585 		return (0);
1586 	}
1587 	if ((name = elf_strptr(elf, (size_t)sndx, (size_t)shdr->sh_name)) ==
1588 	    NULL) {
1589 		eprintf(ofl->ofl_lml, ERR_ELF, MSG_INTL(MSG_ELF_STRPTR),
1590 		    ifl->ifl_name);
1591 		ofl->ofl_flags |= FLG_OF_FATAL;
1592 		return (0);
1593 	}
1594 
1595 	if (ld_sup_input_section(ofl, ifl, name, &shdr, sndx, scn,
1596 	    elf) == S_ERROR)
1597 		return (S_ERROR);
1598 
1599 	/*
1600 	 * Reset the name since the shdr->sh_name could have been changed as
1601 	 * part of ld_sup_input_section().  If there is no name, fabricate one
1602 	 * using the section index.
1603 	 */
1604 	if (shdr->sh_name == 0) {
1605 		(void) snprintf(_name, MAXNDXSIZE, MSG_ORIG(MSG_FMT_INDEX),
1606 		    EC_XWORD(sndx));
1607 		if ((name = libld_malloc(strlen(_name) + 1)) == 0)
1608 			return (S_ERROR);
1609 		(void) strcpy(name, _name);
1610 
1611 	} else if ((name = elf_strptr(elf, (size_t)sndx,
1612 	    (size_t)shdr->sh_name)) == NULL) {
1613 		eprintf(ofl->ofl_lml, ERR_ELF, MSG_INTL(MSG_ELF_STRPTR),
1614 		    ifl->ifl_name);
1615 		ofl->ofl_flags |= FLG_OF_FATAL;
1616 		return (0);
1617 	}
1618 
1619 	error = process_strtab(name, ifl, shdr, scn, sndx, FALSE, ofl);
1620 	if ((error == 0) || (error == S_ERROR))
1621 		return (error);
1622 	str = ifl->ifl_isdesc[sndx]->is_indata->d_buf;
1623 
1624 	/*
1625 	 * Determine the state table column from the input file type.  Note,
1626 	 * shared library sections are not added to the output section list.
1627 	 */
1628 	if (ifl->ifl_ehdr->e_type == ET_DYN) {
1629 		column = 1;
1630 		ofl->ofl_soscnt++;
1631 		ident = ld_targ.t_id.id_null;
1632 	} else {
1633 		column = 0;
1634 		ofl->ofl_objscnt++;
1635 		ident = ld_targ.t_id.id_unknown;
1636 	}
1637 
1638 	DBG_CALL(Dbg_file_generic(ofl->ofl_lml, ifl));
1639 	ndx = 0;
1640 	vdfisp = vndisp = vsyisp = sifisp = capisp = 0;
1641 	scn = NULL;
1642 	while (scn = elf_nextscn(elf, scn)) {
1643 		ndx++;
1644 
1645 		/*
1646 		 * As we've already processed the .shstrtab don't do it again.
1647 		 */
1648 		if (ndx == sndx)
1649 			continue;
1650 
1651 		if ((shdr = elf_getshdr(scn)) == NULL) {
1652 			eprintf(ofl->ofl_lml, ERR_ELF,
1653 			    MSG_INTL(MSG_ELF_GETSHDR), ifl->ifl_name);
1654 			ofl->ofl_flags |= FLG_OF_FATAL;
1655 			return (0);
1656 		}
1657 		name = str + (size_t)(shdr->sh_name);
1658 
1659 		if (ld_sup_input_section(ofl, ifl, name, &shdr, ndx, scn,
1660 		    elf) == S_ERROR)
1661 			return (S_ERROR);
1662 
1663 		/*
1664 		 * Reset the name since the shdr->sh_name could have been
1665 		 * changed as part of ld_sup_input_section().  If there is no
1666 		 * name, fabricate one using the section index.
1667 		 */
1668 		if (shdr->sh_name == 0) {
1669 			(void) snprintf(_name, MAXNDXSIZE,
1670 			    MSG_ORIG(MSG_FMT_INDEX), EC_XWORD(ndx));
1671 			if ((name = libld_malloc(strlen(_name) + 1)) == 0)
1672 				return (S_ERROR);
1673 			(void) strcpy(name, _name);
1674 		} else
1675 			name = str + (size_t)(shdr->sh_name);
1676 
1677 		row = shdr->sh_type;
1678 
1679 		/*
1680 		 * If the section has the SHF_EXCLUDE flag on, and we're not
1681 		 * generating a relocatable object, exclude the section.
1682 		 */
1683 		if (((shdr->sh_flags & SHF_EXCLUDE) != 0) &&
1684 		    ((ofl->ofl_flags & FLG_OF_RELOBJ) == 0)) {
1685 			if ((error = process_exclude(name, ifl, shdr, scn,
1686 			    ndx, ofl)) == S_ERROR)
1687 				return (S_ERROR);
1688 			if (error == 1)
1689 				continue;
1690 		}
1691 
1692 		/*
1693 		 * If this is a standard section type process it via the
1694 		 * appropriate action routine.
1695 		 */
1696 		if (row < SHT_NUM) {
1697 			if (Initial[row][column] != NULL) {
1698 				if (Initial[row][column](name, ifl, shdr, scn,
1699 				    ndx, ident, ofl) == S_ERROR)
1700 					return (S_ERROR);
1701 			}
1702 		} else {
1703 			/*
1704 			 * If this section is below SHT_LOSUNW then we don't
1705 			 * really know what to do with it, issue a warning
1706 			 * message but do the basic section processing anyway.
1707 			 */
1708 			if (row < (Word)SHT_LOSUNW) {
1709 				Conv_inv_buf_t inv_buf;
1710 
1711 				eprintf(ofl->ofl_lml, ERR_WARNING,
1712 				    MSG_INTL(MSG_FIL_INVALSEC), ifl->ifl_name,
1713 				    name,
1714 				    conv_sec_type(ifl->ifl_ehdr->e_machine,
1715 				    shdr->sh_type, 0, &inv_buf));
1716 			}
1717 
1718 			/*
1719 			 * Handle sections greater than SHT_LOSUNW.
1720 			 */
1721 			switch (row) {
1722 			case SHT_SUNW_dof:
1723 				if (process_section(name, ifl, shdr, scn,
1724 				    ndx, ident, ofl) == S_ERROR)
1725 					return (S_ERROR);
1726 				break;
1727 			case SHT_SUNW_cap:
1728 				if (process_section(name, ifl, shdr, scn, ndx,
1729 				    ld_targ.t_id.id_null, ofl) == S_ERROR)
1730 					return (S_ERROR);
1731 				capisp = ifl->ifl_isdesc[ndx];
1732 				break;
1733 			case SHT_SUNW_DEBUGSTR:
1734 			case SHT_SUNW_DEBUG:
1735 				if (process_debug(name, ifl, shdr, scn,
1736 				    ndx, ident, ofl) == S_ERROR)
1737 					return (S_ERROR);
1738 				break;
1739 			case SHT_SUNW_move:
1740 				if (process_section(name, ifl, shdr, scn, ndx,
1741 				    ld_targ.t_id.id_null, ofl) == S_ERROR)
1742 					return (S_ERROR);
1743 				break;
1744 			case SHT_SUNW_syminfo:
1745 				if (process_section(name, ifl, shdr, scn, ndx,
1746 				    ld_targ.t_id.id_null, ofl) == S_ERROR)
1747 					return (S_ERROR);
1748 				sifisp = ifl->ifl_isdesc[ndx];
1749 				break;
1750 			case SHT_SUNW_ANNOTATE:
1751 				if (process_progbits(name, ifl, shdr, scn,
1752 				    ndx, ident, ofl) == S_ERROR)
1753 					return (S_ERROR);
1754 				break;
1755 			case SHT_SUNW_COMDAT:
1756 				if (process_progbits(name, ifl, shdr, scn,
1757 				    ndx, ident, ofl) == S_ERROR)
1758 					return (S_ERROR);
1759 				ifl->ifl_isdesc[ndx]->is_flags |= FLG_IS_COMDAT;
1760 				break;
1761 			case SHT_SUNW_verdef:
1762 				if (process_section(name, ifl, shdr, scn, ndx,
1763 				    ld_targ.t_id.id_null, ofl) == S_ERROR)
1764 					return (S_ERROR);
1765 				vdfisp = ifl->ifl_isdesc[ndx];
1766 				break;
1767 			case SHT_SUNW_verneed:
1768 				if (process_section(name, ifl, shdr, scn, ndx,
1769 				    ld_targ.t_id.id_null, ofl) == S_ERROR)
1770 					return (S_ERROR);
1771 				vndisp = ifl->ifl_isdesc[ndx];
1772 				break;
1773 			case SHT_SUNW_versym:
1774 				if (process_section(name, ifl, shdr, scn, ndx,
1775 				    ld_targ.t_id.id_null, ofl) == S_ERROR)
1776 					return (S_ERROR);
1777 				vsyisp = ifl->ifl_isdesc[ndx];
1778 				break;
1779 			case SHT_SPARC_GOTDATA:
1780 				/*
1781 				 * SHT_SPARC_GOTDATA (0x70000000) is in the
1782 				 * SHT_LOPROC - SHT_HIPROC range reserved
1783 				 * for processor-specific semantics. It is
1784 				 * only meaningful for sparc targets.
1785 				 */
1786 				if (ld_targ.t_m.m_mach !=
1787 				    LD_TARG_BYCLASS(EM_SPARC, EM_SPARCV9))
1788 					goto do_default;
1789 				if (process_section(name, ifl, shdr, scn, ndx,
1790 				    ld_targ.t_id.id_gotdata, ofl) == S_ERROR)
1791 					return (S_ERROR);
1792 				break;
1793 #if	defined(_ELF64)
1794 			case SHT_AMD64_UNWIND:
1795 				/*
1796 				 * SHT_AMD64_UNWIND (0x70000001) is in the
1797 				 * SHT_LOPROC - SHT_HIPROC range reserved
1798 				 * for processor-specific semantics. It is
1799 				 * only meaningful for amd64 targets.
1800 				 */
1801 				if (ld_targ.t_m.m_mach != EM_AMD64)
1802 					goto do_default;
1803 
1804 				/*
1805 				 * Target is x86, so this really is
1806 				 * SHT_AMD64_UNWIND
1807 				 */
1808 				if (column == 0) {
1809 					/*
1810 					 * column == ET_REL
1811 					 */
1812 					if (process_section(name, ifl, shdr,
1813 					    scn, ndx, ld_targ.t_id.id_unwind,
1814 					    ofl) == S_ERROR)
1815 						return (S_ERROR);
1816 					ifl->ifl_isdesc[ndx]->is_flags |=
1817 					    FLG_IS_EHFRAME;
1818 				}
1819 				break;
1820 #endif
1821 			default:
1822 			do_default:
1823 				if (process_section(name, ifl, shdr, scn, ndx,
1824 				    ((ident == ld_targ.t_id.id_null) ?
1825 				    ident : ld_targ.t_id.id_user), ofl) ==
1826 				    S_ERROR)
1827 					return (S_ERROR);
1828 				break;
1829 			}
1830 		}
1831 	}
1832 
1833 	/*
1834 	 * Now that all input sections have been analyzed, and prior to placing
1835 	 * any input sections to their output sections, process any groups.
1836 	 * Groups can contribute COMDAT items, which may get discarded as part
1837 	 * of placement.  In addition, COMDAT names may require transformation
1838 	 * to indicate different output section placement.
1839 	 */
1840 	if (ifl->ifl_flags & FLG_IS_GROUPS) {
1841 		for (ndx = 1; ndx < ifl->ifl_shnum; ndx++) {
1842 			Is_desc	*isp;
1843 
1844 			if (((isp = ifl->ifl_isdesc[ndx]) == NULL) ||
1845 			    (isp->is_shdr->sh_type != SHT_GROUP))
1846 				continue;
1847 
1848 			if (ld_group_process(isp, ofl) == S_ERROR)
1849 				return (S_ERROR);
1850 		}
1851 	}
1852 
1853 	/*
1854 	 * Now that all of input sections have been processed, place them
1855 	 * in the appropriate output sections.
1856 	 */
1857 	for (ndx = 1; ndx < ifl->ifl_shnum; ndx++) {
1858 		Is_desc	*isp;
1859 		Shdr	*shdr;
1860 
1861 		if (((isp = ifl->ifl_isdesc[ndx]) == NULL) ||
1862 		    ((isp->is_flags & FLG_IS_PLACE) == 0))
1863 			continue;
1864 
1865 		shdr = isp->is_shdr;
1866 
1867 		/*
1868 		 * Place all non-ordered sections within their appropriate
1869 		 * output section.
1870 		 *
1871 		 * Ordered sections are sorted based on the relative ordering
1872 		 * of the section pointed to by the sh_info entry.  An ordered
1873 		 * section, whose sh_link points to itself, must also be placed
1874 		 * in the output image so as to control the ordered processing
1875 		 * that follows (see FLG_IF_ORDERED below).
1876 		 */
1877 		if (((isp->is_flags & FLG_IS_ORDERED) == 0) ||
1878 		    ((ndx == shdr->sh_link) &&
1879 		    (shdr->sh_flags & SHF_ORDERED))) {
1880 			if (ld_place_section(ofl, isp,
1881 			    isp->is_keyident, 0) == (Os_desc *)S_ERROR)
1882 				return (S_ERROR);
1883 		}
1884 
1885 		/*
1886 		 * If a section requires ordered processing, keep track of the
1887 		 * section index and count to optimize later section traversal.
1888 		 */
1889 		if (isp->is_flags & FLG_IS_ORDERED) {
1890 			ordcnt++;
1891 			if (ordndx == 0)
1892 				ordndx = ndx;
1893 		}
1894 	}
1895 
1896 	/*
1897 	 * Some sections have special ordering requirements, that are based off
1898 	 * of the section pointed to by their sh_info entry.  This controlling
1899 	 * section will have been placed (above), and thus any ordered sections
1900 	 * can now be processed.
1901 	 */
1902 	if (ifl->ifl_flags & FLG_IF_ORDERED) {
1903 		Word	cnt = 0;
1904 
1905 		for (ndx = ordndx;
1906 		    (ndx < ifl->ifl_shnum) && (cnt < ordcnt); ndx++) {
1907 			Is_desc	*isp;
1908 
1909 			if (((isp = ifl->ifl_isdesc[ndx]) == NULL) ||
1910 			    ((isp->is_flags & FLG_IS_ORDERED) == 0))
1911 				continue;
1912 
1913 			if (ld_process_ordered(ifl, ofl, ndx,
1914 			    ifl->ifl_shnum) == S_ERROR)
1915 				return (S_ERROR);
1916 		}
1917 	}
1918 
1919 	/*
1920 	 * If this is an explicit shared object determine if the user has
1921 	 * specified a control definition.  This descriptor may specify which
1922 	 * version definitions can be used from this object (it may also update
1923 	 * the dependency to USED and supply an alternative SONAME).
1924 	 */
1925 	sdf = 0;
1926 	if (column && (ifl->ifl_flags & FLG_IF_NEEDED)) {
1927 		const char	*base;
1928 
1929 		/*
1930 		 * Use the basename of the input file (typically this is the
1931 		 * compilation environment name, ie. libfoo.so).
1932 		 */
1933 		if ((base = strrchr(ifl->ifl_name, '/')) == NULL)
1934 			base = ifl->ifl_name;
1935 		else
1936 			base++;
1937 
1938 		if ((sdf = sdf_find(base, ofl->ofl_socntl)) != NULL) {
1939 			sdf->sdf_file = ifl;
1940 			ifl->ifl_sdfdesc = sdf;
1941 		}
1942 	}
1943 
1944 	/*
1945 	 * Process any hardware/software capabilities sections.  Only the
1946 	 * capabilities for input relocatable objects are propagated.  If the
1947 	 * relocatable objects don't contain any capabilities, any capability
1948 	 * state that has already been gathered will prevail.
1949 	 */
1950 	if (capisp)
1951 		process_cap(ifl, capisp, ofl);
1952 
1953 	/*
1954 	 * Process any version dependencies.  These will establish shared object
1955 	 * `needed' entries in the same manner as will be generated from the
1956 	 * .dynamic's NEEDED entries.
1957 	 */
1958 	if (vndisp && (ofl->ofl_flags & (FLG_OF_NOUNDEF | FLG_OF_SYMBOLIC)))
1959 		if (ld_vers_need_process(vndisp, ifl, ofl) == S_ERROR)
1960 			return (S_ERROR);
1961 
1962 	/*
1963 	 * Before processing any symbol resolution or relocations process any
1964 	 * version sections.
1965 	 */
1966 	if (vsyisp)
1967 		(void) ld_vers_sym_process(ofl->ofl_lml, vsyisp, ifl);
1968 
1969 	if (ifl->ifl_versym &&
1970 	    (vdfisp || (sdf && (sdf->sdf_flags & FLG_SDF_SELECT))))
1971 		if (ld_vers_def_process(vdfisp, ifl, ofl) == S_ERROR)
1972 			return (S_ERROR);
1973 
1974 	/*
1975 	 * Having collected the appropriate sections carry out any additional
1976 	 * processing if necessary.
1977 	 */
1978 	for (ndx = 0; ndx < ifl->ifl_shnum; ndx++) {
1979 		Is_desc	*isp;
1980 
1981 		if ((isp = ifl->ifl_isdesc[ndx]) == 0)
1982 			continue;
1983 		row = isp->is_shdr->sh_type;
1984 
1985 		if ((isp->is_flags & FLG_IS_DISCARD) == 0)
1986 			ld_sup_section(ofl, isp->is_name, isp->is_shdr, ndx,
1987 			    isp->is_indata, elf);
1988 
1989 		/*
1990 		 * If this is a SHT_SUNW_move section from a relocatable file,
1991 		 * keep track of the section for later processing.
1992 		 */
1993 		if ((row == SHT_SUNW_move) && (column == 0)) {
1994 			if (aplist_append(&(ofl->ofl_ismove), isp,
1995 			    AL_CNT_OFL_MOVE) == NULL)
1996 				return (S_ERROR);
1997 		}
1998 
1999 		/*
2000 		 * If this is a standard section type process it via the
2001 		 * appropriate action routine.
2002 		 */
2003 		if (row < SHT_NUM) {
2004 			if (Final[row][column] != NULL) {
2005 				if (Final[row][column](isp, ifl,
2006 				    ofl) == S_ERROR)
2007 					return (S_ERROR);
2008 			}
2009 #if	defined(_ELF64)
2010 		} else if ((row == SHT_AMD64_UNWIND) && (column == 0)) {
2011 			Os_desc	*osp = isp->is_osdesc;
2012 
2013 			/*
2014 			 * SHT_AMD64_UNWIND (0x70000001) is in the SHT_LOPROC -
2015 			 * SHT_HIPROC range reserved for processor-specific
2016 			 * semantics, and is only meaningful for amd64 targets.
2017 			 *
2018 			 * Only process unwind contents from relocatable
2019 			 * objects.
2020 			 */
2021 			if (osp && (ld_targ.t_m.m_mach == EM_AMD64) &&
2022 			    (ld_unwind_register(osp, ofl) == S_ERROR))
2023 				return (S_ERROR);
2024 #endif
2025 		}
2026 	}
2027 
2028 	/*
2029 	 * After processing any symbol resolution, and if this dependency
2030 	 * indicates it contains symbols that can't be directly bound to,
2031 	 * set the symbols appropriately.
2032 	 */
2033 	if (sifisp && ((ifl->ifl_flags & (FLG_IF_NEEDED | FLG_IF_NODIRECT)) ==
2034 	    (FLG_IF_NEEDED | FLG_IF_NODIRECT)))
2035 		(void) ld_sym_nodirect(sifisp, ifl, ofl);
2036 
2037 	return (1);
2038 }
2039 
2040 /*
2041  * Process the current input file.  There are basically three types of files
2042  * that come through here:
2043  *
2044  *  o	files explicitly defined on the command line (ie. foo.o or bar.so),
2045  *	in this case only the `name' field is valid.
2046  *
2047  *  o	libraries determined from the -l command line option (ie. -lbar),
2048  *	in this case the `soname' field contains the basename of the located
2049  *	file.
2050  *
2051  * Any shared object specified via the above two conventions must be recorded
2052  * as a needed dependency.
2053  *
2054  *  o	libraries specified as dependencies of those libraries already obtained
2055  *	via the command line (ie. bar.so has a DT_NEEDED entry of fred.so.1),
2056  *	in this case the `soname' field contains either a full pathname (if the
2057  *	needed entry contained a `/'), or the basename of the located file.
2058  *	These libraries are processed to verify symbol binding but are not
2059  *	recorded as dependencies of the output file being generated.
2060  */
2061 Ifl_desc *
2062 ld_process_ifl(const char *name, const char *soname, int fd, Elf *elf,
2063     Word flags, Ofl_desc *ofl, Rej_desc *rej)
2064 {
2065 	Ifl_desc	*ifl;
2066 	Ehdr		*ehdr;
2067 	uintptr_t	error = 0;
2068 	struct stat	status;
2069 	Ar_desc		*adp;
2070 	Rej_desc	_rej;
2071 
2072 	/*
2073 	 * If this file was not extracted from an archive obtain its device
2074 	 * information.  This will be used to determine if the file has already
2075 	 * been processed (rather than simply comparing filenames, the device
2076 	 * information provides a quicker comparison and detects linked files).
2077 	 */
2078 	if (fd && ((flags & FLG_IF_EXTRACT) == 0))
2079 		(void) fstat(fd, &status);
2080 	else {
2081 		status.st_dev = 0;
2082 		status.st_ino = 0;
2083 	}
2084 
2085 	switch (elf_kind(elf)) {
2086 	case ELF_K_AR:
2087 		/*
2088 		 * Determine if we've already come across this archive file.
2089 		 */
2090 		if (!(flags & FLG_IF_EXTRACT)) {
2091 			Aliste	idx;
2092 
2093 			for (APLIST_TRAVERSE(ofl->ofl_ars, idx, adp)) {
2094 				if ((adp->ad_stdev != status.st_dev) ||
2095 				    (adp->ad_stino != status.st_ino))
2096 					continue;
2097 
2098 				/*
2099 				 * We've seen this file before so reuse the
2100 				 * original archive descriptor and discard the
2101 				 * new elf descriptor.  Note that a file
2102 				 * descriptor is unnecessary, as the file is
2103 				 * already available in memory.
2104 				 */
2105 				DBG_CALL(Dbg_file_reuse(ofl->ofl_lml, name,
2106 				    adp->ad_name));
2107 				(void) elf_end(elf);
2108 				return ((Ifl_desc *)ld_process_archive(name, -1,
2109 				    adp, ofl));
2110 			}
2111 		}
2112 
2113 		/*
2114 		 * As we haven't processed this file before establish a new
2115 		 * archive descriptor.
2116 		 */
2117 		adp = ld_ar_setup(name, elf, ofl);
2118 		if ((adp == 0) || (adp == (Ar_desc *)S_ERROR))
2119 			return ((Ifl_desc *)adp);
2120 		adp->ad_stdev = status.st_dev;
2121 		adp->ad_stino = status.st_ino;
2122 
2123 		ld_sup_file(ofl, name, ELF_K_AR, flags, elf);
2124 
2125 		/*
2126 		 * Indicate that the ELF descriptor no longer requires a file
2127 		 * descriptor by reading the entire file.  The file is already
2128 		 * read via the initial mmap(2) behind elf_begin(3elf), thus
2129 		 * this operation is effectively a no-op.  However, a side-
2130 		 * effect is that the internal file descriptor, maintained in
2131 		 * the ELF descriptor, is set to -1.  This setting will not
2132 		 * be compared with any file descriptor that is passed to
2133 		 * elf_begin(), should this archive, or one of the archive
2134 		 * members, be processed again from the command line or
2135 		 * because of a -z rescan.
2136 		 */
2137 		if (elf_cntl(elf, ELF_C_FDREAD) == -1) {
2138 			eprintf(ofl->ofl_lml, ERR_ELF, MSG_INTL(MSG_ELF_CNTL),
2139 			    name);
2140 			ofl->ofl_flags |= FLG_OF_FATAL;
2141 			return (NULL);
2142 		}
2143 
2144 		return ((Ifl_desc *)ld_process_archive(name, -1, adp, ofl));
2145 
2146 	case ELF_K_ELF:
2147 		/*
2148 		 * Obtain the elf header so that we can determine what type of
2149 		 * elf ELF_K_ELF file this is.
2150 		 */
2151 		if ((ehdr = elf_getehdr(elf)) == NULL) {
2152 			int	_class = gelf_getclass(elf);
2153 
2154 			/*
2155 			 * Failure could occur for a number of reasons at this
2156 			 * point.  Typically the files class is incorrect (ie.
2157 			 * user is building 64-bit but managed to pint at 32-bit
2158 			 * libraries).  However any number of elf errors can
2159 			 * also occur, such as from a truncated or corrupt file.
2160 			 * Here we try and get the best error message possible.
2161 			 */
2162 			if (ld_targ.t_m.m_class != _class) {
2163 				_rej.rej_type = SGS_REJ_CLASS;
2164 				_rej.rej_info = (uint_t)_class;
2165 			} else {
2166 				_rej.rej_type = SGS_REJ_STR;
2167 				_rej.rej_str = elf_errmsg(-1);
2168 			}
2169 			_rej.rej_name = name;
2170 			DBG_CALL(Dbg_file_rejected(ofl->ofl_lml, &_rej,
2171 			    ld_targ.t_m.m_mach));
2172 			if (rej->rej_type == 0) {
2173 				*rej = _rej;
2174 				rej->rej_name = strdup(_rej.rej_name);
2175 			}
2176 			return (NULL);
2177 		}
2178 
2179 		/*
2180 		 * Determine if we've already come across this file.
2181 		 */
2182 		if (!(flags & FLG_IF_EXTRACT)) {
2183 			APlist	*apl;
2184 			Aliste	idx;
2185 
2186 			if (ehdr->e_type == ET_REL)
2187 				apl = ofl->ofl_objs;
2188 			else
2189 				apl = ofl->ofl_sos;
2190 
2191 			/*
2192 			 * Traverse the appropriate file list and determine if
2193 			 * a dev/inode match is found.
2194 			 */
2195 			for (APLIST_TRAVERSE(apl, idx, ifl)) {
2196 				/*
2197 				 * Ifl_desc generated via -Nneed, therefore no
2198 				 * actual file behind it.
2199 				 */
2200 				if (ifl->ifl_flags & FLG_IF_NEEDSTR)
2201 					continue;
2202 
2203 				if ((ifl->ifl_stino != status.st_ino) ||
2204 				    (ifl->ifl_stdev != status.st_dev))
2205 					continue;
2206 
2207 				/*
2208 				 * Disregard (skip) this image.
2209 				 */
2210 				DBG_CALL(Dbg_file_skip(ofl->ofl_lml,
2211 				    ifl->ifl_name, name));
2212 				(void) elf_end(elf);
2213 
2214 				/*
2215 				 * If the file was explicitly defined on the
2216 				 * command line (this is always the case for
2217 				 * relocatable objects, and is true for shared
2218 				 * objects when they weren't specified via -l or
2219 				 * were dragged in as an implicit dependency),
2220 				 * then warn the user.
2221 				 */
2222 				if ((flags & FLG_IF_CMDLINE) ||
2223 				    (ifl->ifl_flags & FLG_IF_CMDLINE)) {
2224 					const char	*errmsg;
2225 
2226 					/*
2227 					 * Determine whether this is the same
2228 					 * file name as originally encountered
2229 					 * so as to provide the most
2230 					 * descriptive diagnostic.
2231 					 */
2232 					errmsg =
2233 					    (strcmp(name, ifl->ifl_name) == 0) ?
2234 					    MSG_INTL(MSG_FIL_MULINC_1) :
2235 					    MSG_INTL(MSG_FIL_MULINC_2);
2236 					eprintf(ofl->ofl_lml, ERR_WARNING,
2237 					    errmsg, name, ifl->ifl_name);
2238 				}
2239 				return (ifl);
2240 			}
2241 		}
2242 
2243 		/*
2244 		 * At this point, we know we need the file.  Establish an input
2245 		 * file descriptor and continue processing.
2246 		 */
2247 		ifl = ifl_setup(name, ehdr, elf, flags, ofl, rej);
2248 		if ((ifl == 0) || (ifl == (Ifl_desc *)S_ERROR))
2249 			return (ifl);
2250 		ifl->ifl_stdev = status.st_dev;
2251 		ifl->ifl_stino = status.st_ino;
2252 
2253 		/*
2254 		 * If -zignore is in effect, mark this file as a potential
2255 		 * candidate (the files use isn't actually determined until
2256 		 * symbol resolution and relocation processing are completed).
2257 		 */
2258 		if (ofl->ofl_flags1 & FLG_OF1_IGNORE)
2259 			ifl->ifl_flags |= FLG_IF_IGNORE;
2260 
2261 		switch (ehdr->e_type) {
2262 		case ET_REL:
2263 			(*ld_targ.t_mr.mr_mach_eflags)(ehdr, ofl);
2264 			error = process_elf(ifl, elf, ofl);
2265 			break;
2266 		case ET_DYN:
2267 			if ((ofl->ofl_flags & FLG_OF_STATIC) ||
2268 			    !(ofl->ofl_flags & FLG_OF_DYNLIBS)) {
2269 				eprintf(ofl->ofl_lml, ERR_FATAL,
2270 				    MSG_INTL(MSG_FIL_SOINSTAT), name);
2271 				ofl->ofl_flags |= FLG_OF_FATAL;
2272 				return (NULL);
2273 			}
2274 
2275 			/*
2276 			 * Record any additional shared object information.
2277 			 * If no soname is specified (eg. this file was
2278 			 * derived from a explicit filename declaration on the
2279 			 * command line, ie. bar.so) use the pathname.
2280 			 * This entry may be overridden if the files dynamic
2281 			 * section specifies an DT_SONAME value.
2282 			 */
2283 			if (soname == NULL)
2284 				ifl->ifl_soname = ifl->ifl_name;
2285 			else
2286 				ifl->ifl_soname = soname;
2287 
2288 			/*
2289 			 * If direct bindings, lazy loading, or group
2290 			 * permissions need to be established, mark this object.
2291 			 */
2292 			if (ofl->ofl_flags1 & FLG_OF1_ZDIRECT)
2293 				ifl->ifl_flags |= FLG_IF_DIRECT;
2294 			if (ofl->ofl_flags1 & FLG_OF1_LAZYLD)
2295 				ifl->ifl_flags |= FLG_IF_LAZYLD;
2296 			if (ofl->ofl_flags1 & FLG_OF1_GRPPRM)
2297 				ifl->ifl_flags |= FLG_IF_GRPPRM;
2298 			error = process_elf(ifl, elf, ofl);
2299 
2300 			/*
2301 			 * At this point we know if this file will be
2302 			 * lazyloaded, or whether bindings to it must be direct.
2303 			 * In either case, a syminfo section is required.
2304 			 */
2305 			if (ifl->ifl_flags & (FLG_IF_LAZYLD | FLG_IF_DIRECT))
2306 				ofl->ofl_flags |= FLG_OF_SYMINFO;
2307 
2308 			break;
2309 		default:
2310 			(void) elf_errno();
2311 			_rej.rej_type = SGS_REJ_UNKFILE;
2312 			_rej.rej_name = name;
2313 			DBG_CALL(Dbg_file_rejected(ofl->ofl_lml, &_rej,
2314 			    ld_targ.t_m.m_mach));
2315 			if (rej->rej_type == 0) {
2316 				*rej = _rej;
2317 				rej->rej_name = strdup(_rej.rej_name);
2318 			}
2319 			return (NULL);
2320 		}
2321 		break;
2322 	default:
2323 		(void) elf_errno();
2324 		_rej.rej_type = SGS_REJ_UNKFILE;
2325 		_rej.rej_name = name;
2326 		DBG_CALL(Dbg_file_rejected(ofl->ofl_lml, &_rej,
2327 		    ld_targ.t_m.m_mach));
2328 		if (rej->rej_type == 0) {
2329 			*rej = _rej;
2330 			rej->rej_name = strdup(_rej.rej_name);
2331 		}
2332 		return (NULL);
2333 	}
2334 	if ((error == 0) || (error == S_ERROR))
2335 		return ((Ifl_desc *)error);
2336 	else
2337 		return (ifl);
2338 }
2339 
2340 /*
2341  * Having successfully opened a file, set up the necessary elf structures to
2342  * process it further.  This small section of processing is slightly different
2343  * from the elf initialization required to process a relocatable object from an
2344  * archive (see libs.c: ld_process_archive()).
2345  */
2346 Ifl_desc *
2347 ld_process_open(const char *opath, const char *ofile, int *fd, Ofl_desc *ofl,
2348     Word flags, Rej_desc *rej)
2349 {
2350 	Elf		*elf;
2351 	const char	*npath = opath;
2352 	const char	*nfile = ofile;
2353 
2354 	if ((elf = elf_begin(*fd, ELF_C_READ, NULL)) == NULL) {
2355 		eprintf(ofl->ofl_lml, ERR_ELF, MSG_INTL(MSG_ELF_BEGIN), npath);
2356 		ofl->ofl_flags |= FLG_OF_FATAL;
2357 		return (NULL);
2358 	}
2359 
2360 	/*
2361 	 * Determine whether the support library wishes to process this open.
2362 	 * The support library may return:
2363 	 *   .	a different ELF descriptor (in which case they should have
2364 	 *	closed the original)
2365 	 *   .	a different file descriptor (in which case they should have
2366 	 *	closed the original)
2367 	 *   .	a different path and file name (presumably associated with
2368 	 *	a different file descriptor)
2369 	 *
2370 	 * A file descriptor of -1, or and ELF descriptor of zero indicates
2371 	 * the file should be ignored.
2372 	 */
2373 	ld_sup_open(ofl, &npath, &nfile, fd, flags, &elf, NULL, 0,
2374 	    elf_kind(elf));
2375 
2376 	if ((*fd == -1) || (elf == NULL))
2377 		return (NULL);
2378 
2379 	return (ld_process_ifl(npath, nfile, *fd, elf, flags, ofl, rej));
2380 }
2381 
2382 /*
2383  * Having successfully mapped a file, set up the necessary elf structures to
2384  * process it further.  This routine is patterned after ld_process_open() and
2385  * is only called by ld.so.1(1) to process a relocatable object.
2386  */
2387 Ifl_desc *
2388 ld_process_mem(const char *path, const char *file, char *addr, size_t size,
2389     Ofl_desc *ofl, Rej_desc *rej)
2390 {
2391 	Elf	*elf;
2392 
2393 	if ((elf = elf_memory(addr, size)) == NULL) {
2394 		eprintf(ofl->ofl_lml, ERR_ELF, MSG_INTL(MSG_ELF_MEMORY), path);
2395 		ofl->ofl_flags |= FLG_OF_FATAL;
2396 		return (0);
2397 	}
2398 
2399 	return (ld_process_ifl(path, file, 0, elf, 0, ofl, rej));
2400 }
2401 
2402 /*
2403  * Process a required library (i.e. the dependency of a shared object).
2404  * Combine the directory and filename, check the resultant path size, and try
2405  * opening the pathname.
2406  */
2407 static Ifl_desc *
2408 process_req_lib(Sdf_desc *sdf, const char *dir, const char *file,
2409     Ofl_desc *ofl, Rej_desc *rej)
2410 {
2411 	size_t		dlen, plen;
2412 	int		fd;
2413 	char		path[PATH_MAX];
2414 	const char	*_dir = dir;
2415 
2416 	/*
2417 	 * Determine the sizes of the directory and filename to insure we don't
2418 	 * exceed our buffer.
2419 	 */
2420 	if ((dlen = strlen(dir)) == 0) {
2421 		_dir = MSG_ORIG(MSG_STR_DOT);
2422 		dlen = 1;
2423 	}
2424 	dlen++;
2425 	plen = dlen + strlen(file) + 1;
2426 	if (plen > PATH_MAX) {
2427 		eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_FIL_PTHTOLONG),
2428 		    _dir, file);
2429 		ofl->ofl_flags |= FLG_OF_FATAL;
2430 		return (0);
2431 	}
2432 
2433 	/*
2434 	 * Build the entire pathname and try and open the file.
2435 	 */
2436 	(void) strcpy(path, _dir);
2437 	(void) strcat(path, MSG_ORIG(MSG_STR_SLASH));
2438 	(void) strcat(path, file);
2439 	DBG_CALL(Dbg_libs_req(ofl->ofl_lml, sdf->sdf_name,
2440 	    sdf->sdf_rfile, path));
2441 
2442 	if ((fd = open(path, O_RDONLY)) == -1)
2443 		return (0);
2444 	else {
2445 		Ifl_desc	*ifl;
2446 		char		*_path;
2447 
2448 		if ((_path = libld_malloc(strlen(path) + 1)) == 0)
2449 			return ((Ifl_desc *)S_ERROR);
2450 		(void) strcpy(_path, path);
2451 		ifl = ld_process_open(_path, &_path[dlen], &fd, ofl, 0, rej);
2452 		if (fd != -1)
2453 			(void) close(fd);
2454 		return (ifl);
2455 	}
2456 }
2457 
2458 /*
2459  * Finish any library processing.  Walk the list of so's that have been listed
2460  * as "included" by shared objects we have previously processed.  Examine them,
2461  * without adding them as explicit dependents of this program, in order to
2462  * complete our symbol definition process.  The search path rules are:
2463  *
2464  *  o	use any user supplied paths, i.e. LD_LIBRARY_PATH and -L, then
2465  *
2466  *  o	use any RPATH defined within the parent shared object, then
2467  *
2468  *  o	use the default directories, i.e. LIBPATH or -YP.
2469  */
2470 uintptr_t
2471 ld_finish_libs(Ofl_desc *ofl)
2472 {
2473 	Aliste		idx1;
2474 	Sdf_desc	*sdf;
2475 	Rej_desc	rej = { 0 };
2476 
2477 	/*
2478 	 * Make sure we are back in dynamic mode.
2479 	 */
2480 	ofl->ofl_flags |= FLG_OF_DYNLIBS;
2481 
2482 	for (APLIST_TRAVERSE(ofl->ofl_soneed, idx1, sdf)) {
2483 		Aliste		idx2;
2484 		char		*path, *slash = NULL;
2485 		int		fd;
2486 		Ifl_desc	*ifl;
2487 		char		*file = (char *)sdf->sdf_name;
2488 
2489 		/*
2490 		 * See if this file has already been processed.  At the time
2491 		 * this implicit dependency was determined there may still have
2492 		 * been more explicit dependencies to process.  Note, if we ever
2493 		 * do parse the command line three times we would be able to
2494 		 * do all this checking when processing the dynamic section.
2495 		 */
2496 		if (sdf->sdf_file)
2497 			continue;
2498 
2499 		for (APLIST_TRAVERSE(ofl->ofl_sos, idx2, ifl)) {
2500 			if (!(ifl->ifl_flags & FLG_IF_NEEDSTR) &&
2501 			    (strcmp(file, ifl->ifl_soname) == 0)) {
2502 				sdf->sdf_file = ifl;
2503 				break;
2504 			}
2505 		}
2506 		if (sdf->sdf_file)
2507 			continue;
2508 
2509 		/*
2510 		 * If the current path name element embeds a "/", then it's to
2511 		 * be taken "as is", with no searching involved.  Process all
2512 		 * "/" occurrences, so that we can deduce the base file name.
2513 		 */
2514 		for (path = file; *path; path++) {
2515 			if (*path == '/')
2516 				slash = path;
2517 		}
2518 		if (slash) {
2519 			DBG_CALL(Dbg_libs_req(ofl->ofl_lml, sdf->sdf_name,
2520 			    sdf->sdf_rfile, file));
2521 			if ((fd = open(file, O_RDONLY)) == -1) {
2522 				eprintf(ofl->ofl_lml, ERR_WARNING,
2523 				    MSG_INTL(MSG_FIL_NOTFOUND), file,
2524 				    sdf->sdf_rfile);
2525 			} else {
2526 				Rej_desc	_rej = { 0 };
2527 
2528 				ifl = ld_process_open(file, ++slash, &fd, ofl,
2529 				    0, &_rej);
2530 				if (fd != -1)
2531 					(void) close(fd);
2532 				if (ifl == (Ifl_desc *)S_ERROR)
2533 					return (S_ERROR);
2534 
2535 				if (_rej.rej_type) {
2536 					Conv_reject_desc_buf_t rej_buf;
2537 
2538 					eprintf(ofl->ofl_lml, ERR_WARNING,
2539 					    MSG_INTL(reject[_rej.rej_type]),
2540 					    _rej.rej_name ? rej.rej_name :
2541 					    MSG_INTL(MSG_STR_UNKNOWN),
2542 					    conv_reject_desc(&_rej, &rej_buf,
2543 					    ld_targ.t_m.m_mach));
2544 				} else
2545 					sdf->sdf_file = ifl;
2546 			}
2547 			continue;
2548 		}
2549 
2550 		/*
2551 		 * Now search for this file in any user defined directories.
2552 		 */
2553 		for (APLIST_TRAVERSE(ofl->ofl_ulibdirs, idx2, path)) {
2554 			Rej_desc	_rej = { 0 };
2555 
2556 			ifl = process_req_lib(sdf, path, file, ofl, &_rej);
2557 			if (ifl == (Ifl_desc *)S_ERROR) {
2558 				return (S_ERROR);
2559 			}
2560 			if (_rej.rej_type) {
2561 				if (rej.rej_type == 0) {
2562 					rej = _rej;
2563 					rej.rej_name = strdup(_rej.rej_name);
2564 				}
2565 			}
2566 			if (ifl) {
2567 				sdf->sdf_file = ifl;
2568 				break;
2569 			}
2570 		}
2571 		if (sdf->sdf_file)
2572 			continue;
2573 
2574 		/*
2575 		 * Next use the local rules defined within the parent shared
2576 		 * object.
2577 		 */
2578 		if (sdf->sdf_rpath != NULL) {
2579 			char	*rpath, *next;
2580 
2581 			rpath = libld_malloc(strlen(sdf->sdf_rpath) + 1);
2582 			if (rpath == 0)
2583 				return (S_ERROR);
2584 			(void) strcpy(rpath, sdf->sdf_rpath);
2585 			DBG_CALL(Dbg_libs_path(ofl->ofl_lml, rpath,
2586 			    LA_SER_RUNPATH, sdf->sdf_rfile));
2587 			if ((path = strtok_r(rpath,
2588 			    MSG_ORIG(MSG_STR_COLON), &next)) != NULL) {
2589 				do {
2590 					Rej_desc	_rej = { 0 };
2591 
2592 					path = expand(sdf->sdf_rfile, path,
2593 					    &next);
2594 
2595 					ifl = process_req_lib(sdf, path,
2596 					    file, ofl, &_rej);
2597 					if (ifl == (Ifl_desc *)S_ERROR) {
2598 						return (S_ERROR);
2599 					}
2600 					if ((_rej.rej_type) &&
2601 					    (rej.rej_type == 0)) {
2602 						rej = _rej;
2603 						rej.rej_name =
2604 						    strdup(_rej.rej_name);
2605 					}
2606 					if (ifl) {
2607 						sdf->sdf_file = ifl;
2608 						break;
2609 					}
2610 				} while ((path = strtok_r(NULL,
2611 				    MSG_ORIG(MSG_STR_COLON), &next)) != NULL);
2612 			}
2613 		}
2614 		if (sdf->sdf_file)
2615 			continue;
2616 
2617 		/*
2618 		 * Finally try the default library search directories.
2619 		 */
2620 		for (APLIST_TRAVERSE(ofl->ofl_dlibdirs, idx2, path)) {
2621 			Rej_desc	_rej = { 0 };
2622 
2623 			ifl = process_req_lib(sdf, path, file, ofl, &rej);
2624 			if (ifl == (Ifl_desc *)S_ERROR) {
2625 				return (S_ERROR);
2626 			}
2627 			if (_rej.rej_type) {
2628 				if (rej.rej_type == 0) {
2629 					rej = _rej;
2630 					rej.rej_name = strdup(_rej.rej_name);
2631 				}
2632 			}
2633 			if (ifl) {
2634 				sdf->sdf_file = ifl;
2635 				break;
2636 			}
2637 		}
2638 		if (sdf->sdf_file)
2639 			continue;
2640 
2641 		/*
2642 		 * If we've got this far we haven't found the shared object.
2643 		 * If an object was found, but was rejected for some reason,
2644 		 * print a diagnostic to that effect, otherwise generate a
2645 		 * generic "not found" diagnostic.
2646 		 */
2647 		if (rej.rej_type) {
2648 			Conv_reject_desc_buf_t rej_buf;
2649 
2650 			eprintf(ofl->ofl_lml, ERR_WARNING,
2651 			    MSG_INTL(reject[rej.rej_type]),
2652 			    rej.rej_name ? rej.rej_name :
2653 			    MSG_INTL(MSG_STR_UNKNOWN),
2654 			    conv_reject_desc(&rej, &rej_buf,
2655 			    ld_targ.t_m.m_mach));
2656 		} else {
2657 			eprintf(ofl->ofl_lml, ERR_WARNING,
2658 			    MSG_INTL(MSG_FIL_NOTFOUND), file, sdf->sdf_rfile);
2659 		}
2660 	}
2661 
2662 	/*
2663 	 * Finally, now that all objects have been input, make sure any version
2664 	 * requirements have been met.
2665 	 */
2666 	return (ld_vers_verify(ofl));
2667 }
2668