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