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