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