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