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