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