xref: /illumos-gate/usr/src/cmd/sgs/libld/common/files.c (revision 8b80e8cb6855118d46f605e91b5ed4ce83417395)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  *	Copyright (c) 1988 AT&T
24  *	  All Rights Reserved
25  *
26  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
27  * Use is subject to license terms.
28  */
29 
30 /*
31  * Processing of relocatable objects and shared objects.
32  */
33 
34 #define	ELF_TARGET_AMD64
35 #define	ELF_TARGET_SPARC
36 
37 #include	<stdio.h>
38 #include	<string.h>
39 #include	<fcntl.h>
40 #include	<unistd.h>
41 #include	<link.h>
42 #include	<limits.h>
43 #include	<sys/stat.h>
44 #include	<sys/systeminfo.h>
45 #include	<debug.h>
46 #include	<msg.h>
47 #include	<_libld.h>
48 
49 /*
50  * Decide if we can link against this input file.
51  */
52 static int
53 ifl_verify(Ehdr *ehdr, Ofl_desc *ofl, Rej_desc *rej)
54 {
55 	/*
56 	 * Check the validity of the elf header information for compatibility
57 	 * with this machine and our own internal elf library.
58 	 */
59 	if ((ehdr->e_machine != ld_targ.t_m.m_mach) &&
60 	    ((ehdr->e_machine != ld_targ.t_m.m_machplus) &&
61 	    ((ehdr->e_flags & ld_targ.t_m.m_flagsplus) == 0))) {
62 		rej->rej_type = SGS_REJ_MACH;
63 		rej->rej_info = (uint_t)ehdr->e_machine;
64 		return (0);
65 	}
66 	if (ehdr->e_ident[EI_DATA] != ld_targ.t_m.m_data) {
67 		rej->rej_type = SGS_REJ_DATA;
68 		rej->rej_info = (uint_t)ehdr->e_ident[EI_DATA];
69 		return (0);
70 	}
71 	if (ehdr->e_version > ofl->ofl_dehdr->e_version) {
72 		rej->rej_type = SGS_REJ_VERSION;
73 		rej->rej_info = (uint_t)ehdr->e_version;
74 		return (0);
75 	}
76 	return (1);
77 }
78 
79 /*
80  * Check sanity of file header and allocate an infile descriptor
81  * for the file being processed.
82  */
83 static Ifl_desc *
84 ifl_setup(const char *name, Ehdr *ehdr, Elf *elf, Word flags, Ofl_desc *ofl,
85     Rej_desc *rej)
86 {
87 	Ifl_desc	*ifl;
88 	List		*list;
89 	Rej_desc	_rej = { 0 };
90 
91 	if (ifl_verify(ehdr, ofl, &_rej) == 0) {
92 		_rej.rej_name = name;
93 		DBG_CALL(Dbg_file_rejected(ofl->ofl_lml, &_rej,
94 		    ld_targ.t_m.m_mach));
95 		if (rej->rej_type == 0) {
96 			*rej = _rej;
97 			rej->rej_name = strdup(_rej.rej_name);
98 		}
99 		return (0);
100 	}
101 
102 	if ((ifl = libld_calloc(1, sizeof (Ifl_desc))) == 0)
103 		return ((Ifl_desc *)S_ERROR);
104 	ifl->ifl_name = name;
105 	ifl->ifl_ehdr = ehdr;
106 	ifl->ifl_elf = elf;
107 	ifl->ifl_flags = flags;
108 
109 	/*
110 	 * Is this file using 'extended Section Indexes'.  If so, use the
111 	 * e_shnum & e_shstrndx which can be found at:
112 	 *
113 	 *	e_shnum == Shdr[0].sh_size
114 	 *	e_shstrndx == Shdr[0].sh_link
115 	 */
116 	if ((ehdr->e_shnum == 0) && (ehdr->e_shoff != 0)) {
117 		Elf_Scn	*scn;
118 		Shdr	*shdr0;
119 
120 		if ((scn = elf_getscn(elf, 0)) == NULL) {
121 			eprintf(ofl->ofl_lml, ERR_ELF,
122 			    MSG_INTL(MSG_ELF_GETSCN), name);
123 			ofl->ofl_flags |= FLG_OF_FATAL;
124 			return ((Ifl_desc *)S_ERROR);
125 		}
126 		if ((shdr0 = elf_getshdr(scn)) == NULL) {
127 			eprintf(ofl->ofl_lml, ERR_ELF,
128 			    MSG_INTL(MSG_ELF_GETSHDR), name);
129 			ofl->ofl_flags |= FLG_OF_FATAL;
130 			return ((Ifl_desc *)S_ERROR);
131 		}
132 		ifl->ifl_shnum = (Word)shdr0->sh_size;
133 		if (ehdr->e_shstrndx == SHN_XINDEX)
134 			ifl->ifl_shstrndx = shdr0->sh_link;
135 		else
136 			ifl->ifl_shstrndx = ehdr->e_shstrndx;
137 	} else {
138 		ifl->ifl_shnum = ehdr->e_shnum;
139 		ifl->ifl_shstrndx = ehdr->e_shstrndx;
140 	}
141 
142 	if ((ifl->ifl_isdesc = libld_calloc(ifl->ifl_shnum,
143 	    sizeof (Is_desc *))) == 0)
144 		return ((Ifl_desc *)S_ERROR);
145 
146 	/*
147 	 * Record this new input file on the shared object or relocatable
148 	 * object input file list.
149 	 */
150 	if (ifl->ifl_ehdr->e_type == ET_DYN) {
151 		list = &ofl->ofl_sos;
152 	} else {
153 		list = &ofl->ofl_objs;
154 	}
155 
156 	if (list_appendc(list, ifl) == 0)
157 		return ((Ifl_desc *)S_ERROR);
158 	return (ifl);
159 }
160 
161 /*
162  * Process a generic section.  The appropriate section information is added
163  * to the files input descriptor list.
164  */
165 static uintptr_t
166 process_section(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
167     Word ndx, int ident, Ofl_desc *ofl)
168 {
169 	Is_desc	*isp;
170 
171 	/*
172 	 * Create a new input section descriptor.  If this is a NOBITS
173 	 * section elf_getdata() will still create a data buffer (the buffer
174 	 * will be null and the size will reflect the actual memory size).
175 	 */
176 	if ((isp = libld_calloc(sizeof (Is_desc), 1)) == 0)
177 		return (S_ERROR);
178 	isp->is_shdr = shdr;
179 	isp->is_file = ifl;
180 	isp->is_name = name;
181 	isp->is_scnndx = ndx;
182 	isp->is_flags = FLG_IS_EXTERNAL;
183 	isp->is_keyident = ident;
184 
185 	if ((isp->is_indata = elf_getdata(scn, NULL)) == NULL) {
186 		eprintf(ofl->ofl_lml, ERR_ELF, MSG_INTL(MSG_ELF_GETDATA),
187 		    ifl->ifl_name);
188 		ofl->ofl_flags |= FLG_OF_FATAL;
189 		return (0);
190 	}
191 
192 	if ((shdr->sh_flags & SHF_EXCLUDE) &&
193 	    ((ofl->ofl_flags & FLG_OF_RELOBJ) == 0)) {
194 		isp->is_flags |= FLG_IS_DISCARD;
195 	}
196 
197 	/*
198 	 * Add the new input section to the files input section list and
199 	 * flag whether the section needs placing in an output section.  This
200 	 * placement is deferred until all input section processing has been
201 	 * completed, as SHT_GROUP sections can provide information that will
202 	 * affect how other sections within the file should be placed.
203 	 */
204 	ifl->ifl_isdesc[ndx] = isp;
205 
206 	if (ident) {
207 		if (shdr->sh_flags & ALL_SHF_ORDER) {
208 			isp->is_flags |= FLG_IS_ORDERED;
209 			ifl->ifl_flags |= FLG_IF_ORDERED;
210 		}
211 		isp->is_flags |= FLG_IS_PLACE;
212 	}
213 	return (1);
214 }
215 
216 /*
217  * Determine the software capabilities of the object being built from the
218  * capabilities of the input relocatable objects.   One software capability
219  * is presently recognized, and represented with the following (sys/elf.h):
220  *
221  *   SF1_SUNW_FPKNWN	use/non-use of frame pointer is known, and
222  *   SF1_SUNW_FPUSED    the frame pointer is in use.
223  *
224  * The resolution of the present fame pointer state, and the capabilities
225  * provided by a new input relocatable object are:
226  *
227  *                              new input relocatable object
228  *
229  *      present      |  SF1_SUNW_FPKNWN  |  SF1_SUNW_FPKNWN  |    <unknown>
230  *       state       |  SF1_SUNW_FPUSED  |                   |
231  *  ---------------------------------------------------------------------------
232  *  SF1_SUNW_FPKNWN  |  SF1_SUNW_FPKNWN  |  SF1_SUNW_FPKNWN  |  SF1_SUNW_FPKNWN
233  *  SF1_SUNW_FPUSED  |  SF1_SUNW_FPUSED  |                   |  SF1_SUNW_FPUSED
234  *  ---------------------------------------------------------------------------
235  *  SF1_SUNW_FPKNWN  |  SF1_SUNW_FPKNWN  |  SF1_SUNW_FPKNWN  |  SF1_SUNW_FPKNWN
236  *                   |                   |                   |
237  *  ---------------------------------------------------------------------------
238  *     <unknown>     |  SF1_SUNW_FPKNWN  |  SF1_SUNW_FPKNWN  |    <unknown>
239  *                   |  SF1_SUNW_FPUSED  |                   |
240  */
241 static void
242 sf1_cap(Ofl_desc *ofl, Xword val, Ifl_desc *ifl, const char *name)
243 {
244 	Xword	badval;
245 
246 	/*
247 	 * If a mapfile has established definitions to override any input
248 	 * capabilities, ignore any new input capabilities.
249 	 */
250 	if (ofl->ofl_flags1 & FLG_OF1_OVSFCAP) {
251 		Dbg_cap_sec_entry(ofl->ofl_lml, DBG_CAP_IGNORE, CA_SUNW_SF_1,
252 		    val, ld_targ.t_m.m_mach);
253 		return;
254 	}
255 
256 #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));
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 (list_appendc(&osp->os_relisdescs, isc) == 0)
1372 			return (S_ERROR);
1373 	}
1374 	return (1);
1375 }
1376 
1377 /*
1378  * SHF_EXCLUDE flags is set for this section.
1379  */
1380 static uintptr_t
1381 process_exclude(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
1382     Word ndx, Ofl_desc *ofl)
1383 {
1384 	/*
1385 	 * Sections SHT_SYMTAB and SHT_DYNDYM, even if SHF_EXCLUDE is on, might
1386 	 * be needed for ld processing.  These sections need to be in the
1387 	 * internal table.  Later it will be determined whether they can be
1388 	 * eliminated or not.
1389 	 */
1390 	if (shdr->sh_type == SHT_SYMTAB || shdr->sh_type == SHT_DYNSYM)
1391 		return (0);
1392 
1393 	/*
1394 	 * Other checks
1395 	 */
1396 	if (shdr->sh_flags & SHF_ALLOC) {
1397 		/*
1398 		 * A conflict, issue an warning message, and ignore the section.
1399 		 */
1400 		eprintf(ofl->ofl_lml, ERR_WARNING, MSG_INTL(MSG_FIL_EXCLUDE),
1401 		    ifl->ifl_name, name);
1402 		return (0);
1403 	}
1404 
1405 	/*
1406 	 * This sections is not going to the output file.
1407 	 */
1408 	return (process_section(name, ifl, shdr, scn, ndx, 0, ofl));
1409 }
1410 
1411 /*
1412  * Section processing state table.  `Initial' describes the required initial
1413  * procedure to be called (if any), `Final' describes the final processing
1414  * procedure (ie. things that can only be done when all required sections
1415  * have been collected).
1416  */
1417 static uintptr_t (*Initial[SHT_NUM][2])() = {
1418 
1419 /*			ET_REL			ET_DYN			*/
1420 
1421 /* SHT_NULL	*/	invalid_section,	invalid_section,
1422 /* SHT_PROGBITS	*/	process_progbits,	process_progbits,
1423 /* SHT_SYMTAB	*/	process_input,		process_input,
1424 /* SHT_STRTAB	*/	process_strtab,		process_strtab,
1425 /* SHT_RELA	*/	process_reloc,		process_reloc,
1426 /* SHT_HASH	*/	invalid_section,	NULL,
1427 /* SHT_DYNAMIC	*/	process_rel_dynamic,	process_dynamic_isgnu,
1428 /* SHT_NOTE	*/	process_section,	NULL,
1429 /* SHT_NOBITS	*/	process_nobits,		process_nobits,
1430 /* SHT_REL	*/	process_reloc,		process_reloc,
1431 /* SHT_SHLIB	*/	process_section,	invalid_section,
1432 /* SHT_DYNSYM	*/	invalid_section,	process_input,
1433 /* SHT_UNKNOWN12 */	process_progbits,	process_progbits,
1434 /* SHT_UNKNOWN13 */	process_progbits,	process_progbits,
1435 /* SHT_INIT_ARRAY */	process_array,		NULL,
1436 /* SHT_FINI_ARRAY */	process_array,		NULL,
1437 /* SHT_PREINIT_ARRAY */	process_array,		NULL,
1438 /* SHT_GROUP */		process_group,		invalid_section,
1439 /* SHT_SYMTAB_SHNDX */	process_sym_shndx,	NULL
1440 };
1441 
1442 static uintptr_t (*Final[SHT_NUM][2])() = {
1443 
1444 /* SHT_NULL	*/	NULL,			NULL,
1445 /* SHT_PROGBITS	*/	NULL,			NULL,
1446 /* SHT_SYMTAB	*/	ld_sym_process,		ld_sym_process,
1447 /* SHT_STRTAB	*/	NULL,			NULL,
1448 /* SHT_RELA	*/	rel_process,		NULL,
1449 /* SHT_HASH	*/	NULL,			NULL,
1450 /* SHT_DYNAMIC	*/	NULL,			process_dynamic,
1451 /* SHT_NOTE	*/	NULL,			NULL,
1452 /* SHT_NOBITS	*/	NULL,			NULL,
1453 /* SHT_REL	*/	rel_process,		NULL,
1454 /* SHT_SHLIB	*/	NULL,			NULL,
1455 /* SHT_DYNSYM	*/	NULL,			ld_sym_process,
1456 /* SHT_UNKNOWN12 */	NULL,			NULL,
1457 /* SHT_UNKNOWN13 */	NULL,			NULL,
1458 /* SHT_INIT_ARRAY */	array_process,		NULL,
1459 /* SHT_FINI_ARRAY */	array_process,		NULL,
1460 /* SHT_PREINIT_ARRAY */	array_process,		NULL,
1461 /* SHT_GROUP */		NULL,			NULL,
1462 /* SHT_SYMTAB_SHNDX */	sym_shndx_process,	NULL
1463 };
1464 
1465 #define	MAXNDXSIZE	10
1466 
1467 /*
1468  * Process an elf file.  Each section is compared against the section state
1469  * table to determine whether it should be processed (saved), ignored, or
1470  * is invalid for the type of input file being processed.
1471  */
1472 static uintptr_t
1473 process_elf(Ifl_desc *ifl, Elf *elf, Ofl_desc *ofl)
1474 {
1475 	Elf_Scn		*scn;
1476 	Shdr		*shdr;
1477 	Word		ndx, sndx, ordndx = 0, ordcnt = 0;
1478 	char		*str, *name, _name[MAXNDXSIZE];
1479 	Word		row, column;
1480 	int		ident;
1481 	uintptr_t	error;
1482 	Is_desc		*vdfisp, *vndisp, *vsyisp, *sifisp, *capisp;
1483 	Sdf_desc	*sdf;
1484 
1485 	/*
1486 	 * First process the .shstrtab section so that later sections can
1487 	 * reference their name.
1488 	 */
1489 	ld_sup_file(ofl, ifl->ifl_name, elf_kind(elf), ifl->ifl_flags, elf);
1490 
1491 	sndx = ifl->ifl_shstrndx;
1492 	if ((scn = elf_getscn(elf, (size_t)sndx)) == NULL) {
1493 		eprintf(ofl->ofl_lml, ERR_ELF, MSG_INTL(MSG_ELF_GETSCN),
1494 		    ifl->ifl_name);
1495 		ofl->ofl_flags |= FLG_OF_FATAL;
1496 		return (0);
1497 	}
1498 	if ((shdr = elf_getshdr(scn)) == NULL) {
1499 		eprintf(ofl->ofl_lml, ERR_ELF, MSG_INTL(MSG_ELF_GETSHDR),
1500 		    ifl->ifl_name);
1501 		ofl->ofl_flags |= FLG_OF_FATAL;
1502 		return (0);
1503 	}
1504 	if ((name = elf_strptr(elf, (size_t)sndx, (size_t)shdr->sh_name)) ==
1505 	    NULL) {
1506 		eprintf(ofl->ofl_lml, ERR_ELF, MSG_INTL(MSG_ELF_STRPTR),
1507 		    ifl->ifl_name);
1508 		ofl->ofl_flags |= FLG_OF_FATAL;
1509 		return (0);
1510 	}
1511 
1512 	if (ld_sup_input_section(ofl, ifl, name, &shdr, sndx, scn,
1513 	    elf) == S_ERROR)
1514 		return (S_ERROR);
1515 
1516 	/*
1517 	 * Reset the name since the shdr->sh_name could have been changed as
1518 	 * part of ld_sup_input_section().  If there is no name, fabricate one
1519 	 * using the section index.
1520 	 */
1521 	if (shdr->sh_name == 0) {
1522 		(void) snprintf(_name, MAXNDXSIZE, MSG_ORIG(MSG_FMT_INDEX),
1523 		    EC_XWORD(sndx));
1524 		if ((name = libld_malloc(strlen(_name) + 1)) == 0)
1525 			return (S_ERROR);
1526 		(void) strcpy(name, _name);
1527 
1528 	} else if ((name = elf_strptr(elf, (size_t)sndx,
1529 	    (size_t)shdr->sh_name)) == NULL) {
1530 		eprintf(ofl->ofl_lml, ERR_ELF, MSG_INTL(MSG_ELF_STRPTR),
1531 		    ifl->ifl_name);
1532 		ofl->ofl_flags |= FLG_OF_FATAL;
1533 		return (0);
1534 	}
1535 
1536 	error = process_strtab(name, ifl, shdr, scn, sndx, FALSE, ofl);
1537 	if ((error == 0) || (error == S_ERROR))
1538 		return (error);
1539 	str = ifl->ifl_isdesc[sndx]->is_indata->d_buf;
1540 
1541 	/*
1542 	 * Determine the state table column from the input file type.  Note,
1543 	 * shared library sections are not added to the output section list.
1544 	 */
1545 	if (ifl->ifl_ehdr->e_type == ET_DYN) {
1546 		column = 1;
1547 		ofl->ofl_soscnt++;
1548 		ident = ld_targ.t_id.id_null;
1549 	} else {
1550 		column = 0;
1551 		ofl->ofl_objscnt++;
1552 		ident = ld_targ.t_id.id_unknown;
1553 	}
1554 
1555 	DBG_CALL(Dbg_file_generic(ofl->ofl_lml, ifl));
1556 	ndx = 0;
1557 	vdfisp = vndisp = vsyisp = sifisp = capisp = 0;
1558 	scn = NULL;
1559 	while (scn = elf_nextscn(elf, scn)) {
1560 		ndx++;
1561 
1562 		/*
1563 		 * As we've already processed the .shstrtab don't do it again.
1564 		 */
1565 		if (ndx == sndx)
1566 			continue;
1567 
1568 		if ((shdr = elf_getshdr(scn)) == NULL) {
1569 			eprintf(ofl->ofl_lml, ERR_ELF,
1570 			    MSG_INTL(MSG_ELF_GETSHDR), ifl->ifl_name);
1571 			ofl->ofl_flags |= FLG_OF_FATAL;
1572 			return (0);
1573 		}
1574 		name = str + (size_t)(shdr->sh_name);
1575 
1576 		if (ld_sup_input_section(ofl, ifl, name, &shdr, ndx, scn,
1577 		    elf) == S_ERROR)
1578 			return (S_ERROR);
1579 
1580 		/*
1581 		 * Reset the name since the shdr->sh_name could have been
1582 		 * changed as part of ld_sup_input_section().  If there is no
1583 		 * name, fabricate one using the section index.
1584 		 */
1585 		if (shdr->sh_name == 0) {
1586 			(void) snprintf(_name, MAXNDXSIZE,
1587 			    MSG_ORIG(MSG_FMT_INDEX), EC_XWORD(ndx));
1588 			if ((name = libld_malloc(strlen(_name) + 1)) == 0)
1589 				return (S_ERROR);
1590 			(void) strcpy(name, _name);
1591 		} else
1592 			name = str + (size_t)(shdr->sh_name);
1593 
1594 		row = shdr->sh_type;
1595 
1596 		/*
1597 		 * If the section has the SHF_EXCLUDE flag on, and we're not
1598 		 * generating a relocatable object, exclude the section.
1599 		 */
1600 		if (((shdr->sh_flags & SHF_EXCLUDE) != 0) &&
1601 		    ((ofl->ofl_flags & FLG_OF_RELOBJ) == 0)) {
1602 			if ((error = process_exclude(name, ifl, shdr, scn,
1603 			    ndx, ofl)) == S_ERROR)
1604 				return (S_ERROR);
1605 			if (error == 1)
1606 				continue;
1607 		}
1608 
1609 		/*
1610 		 * If this is a standard section type process it via the
1611 		 * appropriate action routine.
1612 		 */
1613 		if (row < SHT_NUM) {
1614 			if (Initial[row][column] != NULL) {
1615 				if (Initial[row][column](name, ifl, shdr, scn,
1616 				    ndx, ident, ofl) == S_ERROR)
1617 					return (S_ERROR);
1618 			}
1619 		} else {
1620 			/*
1621 			 * If this section is below SHT_LOSUNW then we don't
1622 			 * really know what to do with it, issue a warning
1623 			 * message but do the basic section processing anyway.
1624 			 */
1625 			if (row < (Word)SHT_LOSUNW) {
1626 				Conv_inv_buf_t inv_buf;
1627 
1628 				eprintf(ofl->ofl_lml, ERR_WARNING,
1629 				    MSG_INTL(MSG_FIL_INVALSEC), ifl->ifl_name,
1630 				    name,
1631 				    conv_sec_type(ifl->ifl_ehdr->e_machine,
1632 				    shdr->sh_type, 0, &inv_buf));
1633 			}
1634 
1635 			/*
1636 			 * Handle sections greater than SHT_LOSUNW.
1637 			 */
1638 			switch (row) {
1639 			case SHT_SUNW_dof:
1640 				if (process_section(name, ifl, shdr, scn,
1641 				    ndx, ident, ofl) == S_ERROR)
1642 					return (S_ERROR);
1643 				break;
1644 			case SHT_SUNW_cap:
1645 				if (process_section(name, ifl, shdr, scn,
1646 				    ndx, ld_targ.t_id.id_null, ofl) == S_ERROR)
1647 					return (S_ERROR);
1648 				capisp = ifl->ifl_isdesc[ndx];
1649 				break;
1650 			case SHT_SUNW_DEBUGSTR:
1651 			case SHT_SUNW_DEBUG:
1652 				if (process_debug(name, ifl, shdr, scn,
1653 				    ndx, ident, ofl) == S_ERROR)
1654 					return (S_ERROR);
1655 				break;
1656 			case SHT_SUNW_move:
1657 				if (process_section(name, ifl, shdr, scn,
1658 				    ndx, ld_targ.t_id.id_null, ofl) == S_ERROR)
1659 					return (S_ERROR);
1660 				break;
1661 			case SHT_SUNW_syminfo:
1662 				if (process_section(name, ifl, shdr, scn,
1663 				    ndx, ld_targ.t_id.id_null, ofl) == S_ERROR)
1664 					return (S_ERROR);
1665 				sifisp = ifl->ifl_isdesc[ndx];
1666 				break;
1667 			case SHT_SUNW_ANNOTATE:
1668 				if (process_progbits(name, ifl, shdr, scn,
1669 				    ndx, ident, ofl) == S_ERROR)
1670 					return (S_ERROR);
1671 				break;
1672 			case SHT_SUNW_COMDAT:
1673 				if (process_progbits(name, ifl, shdr, scn,
1674 				    ndx, ident, ofl) == S_ERROR)
1675 					return (S_ERROR);
1676 				ifl->ifl_isdesc[ndx]->is_flags |= FLG_IS_COMDAT;
1677 				break;
1678 			case SHT_SUNW_verdef:
1679 				if (process_section(name, ifl, shdr, scn,
1680 				    ndx, ld_targ.t_id.id_null, ofl) == S_ERROR)
1681 					return (S_ERROR);
1682 				vdfisp = ifl->ifl_isdesc[ndx];
1683 				break;
1684 			case SHT_SUNW_verneed:
1685 				if (process_section(name, ifl, shdr, scn,
1686 				    ndx, ld_targ.t_id.id_null, ofl) == S_ERROR)
1687 					return (S_ERROR);
1688 				vndisp = ifl->ifl_isdesc[ndx];
1689 				break;
1690 			case SHT_SUNW_versym:
1691 				if (process_section(name, ifl, shdr, scn,
1692 				    ndx, ld_targ.t_id.id_null, ofl) == S_ERROR)
1693 					return (S_ERROR);
1694 				vsyisp = ifl->ifl_isdesc[ndx];
1695 				break;
1696 			case SHT_SPARC_GOTDATA:
1697 				/*
1698 				 * SHT_SPARC_GOTDATA (0x70000000) is in the
1699 				 * SHT_LOPROC - SHT_HIPROC range reserved
1700 				 * for processor-specific semantics. It is
1701 				 * only meaningful for sparc targets.
1702 				 */
1703 				if (ld_targ.t_m.m_mach !=
1704 				    LD_TARG_BYCLASS(EM_SPARC, EM_SPARCV9))
1705 					goto do_default;
1706 				if (process_section(name, ifl, shdr, scn,
1707 				    ndx, ld_targ.t_id.id_gotdata, ofl) ==
1708 				    S_ERROR)
1709 					return (S_ERROR);
1710 				break;
1711 #if	defined(_ELF64)
1712 			case SHT_AMD64_UNWIND:
1713 				/*
1714 				 * SHT_AMD64_UNWIND (0x70000001) is in the
1715 				 * SHT_LOPROC - SHT_HIPROC range reserved
1716 				 * for processor-specific semantics. It is
1717 				 * only meaningful for amd64 targets.
1718 				 */
1719 				if (ld_targ.t_m.m_mach != EM_AMD64)
1720 					goto do_default;
1721 
1722 				/*
1723 				 * Target is x86, so this really is
1724 				 * SHT_AMD64_UNWIND
1725 				 */
1726 				if (column == 0) {
1727 					/*
1728 					 * column == ET_REL
1729 					 */
1730 					if (process_section(name, ifl, shdr,
1731 					    scn, ndx, ld_targ.t_id.id_unwind,
1732 					    ofl) == S_ERROR)
1733 						return (S_ERROR);
1734 				}
1735 				break;
1736 #endif
1737 			default:
1738 			do_default:
1739 				if (ident != ld_targ.t_id.id_null)
1740 					ident = ld_targ.t_id.id_user;
1741 				if (process_section(name, ifl, shdr, scn,
1742 				    ndx, ident, ofl) == S_ERROR)
1743 					return (S_ERROR);
1744 				break;
1745 			}
1746 		}
1747 	}
1748 
1749 	/*
1750 	 * Now that all input sections have been analyzed, and prior to placing
1751 	 * any input sections to their output sections, process any groups.
1752 	 * Groups can contribute COMDAT items, which may get discarded as part
1753 	 * of placement.  In addition, COMDAT names may require transformation
1754 	 * to indicate different output section placement.
1755 	 */
1756 	if (ifl->ifl_flags & FLG_IS_GROUPS) {
1757 		for (ndx = 1; ndx < ifl->ifl_shnum; ndx++) {
1758 			Is_desc	*isp;
1759 
1760 			if (((isp = ifl->ifl_isdesc[ndx]) == NULL) ||
1761 			    (isp->is_shdr->sh_type != SHT_GROUP))
1762 				continue;
1763 
1764 			if (ld_group_process(isp, ofl) == S_ERROR)
1765 				return (S_ERROR);
1766 		}
1767 	}
1768 
1769 	/*
1770 	 * Now that all of input sections have been processed, place them
1771 	 * in the appropriate output sections.
1772 	 */
1773 	for (ndx = 1; ndx < ifl->ifl_shnum; ndx++) {
1774 		Is_desc	*isp;
1775 		Shdr	*shdr;
1776 
1777 		if (((isp = ifl->ifl_isdesc[ndx]) == NULL) ||
1778 		    ((isp->is_flags & FLG_IS_PLACE) == 0))
1779 			continue;
1780 
1781 		shdr = isp->is_shdr;
1782 
1783 		/*
1784 		 * Place all non-ordered sections within their appropriate
1785 		 * output section.
1786 		 *
1787 		 * Ordered sections are sorted based on the relative ordering
1788 		 * of the section pointed to by the sh_info entry.  An ordered
1789 		 * section, whose sh_link points to itself, must also be placed
1790 		 * in the output image so as to control the ordered processing
1791 		 * that follows (see FLG_IF_ORDERED below).
1792 		 */
1793 		if (((isp->is_flags & FLG_IS_ORDERED) == 0) ||
1794 		    ((ndx == shdr->sh_link) &&
1795 		    (shdr->sh_flags & SHF_ORDERED))) {
1796 			if (ld_place_section(ofl, isp,
1797 			    isp->is_keyident, 0) == (Os_desc *)S_ERROR)
1798 				return (S_ERROR);
1799 		}
1800 
1801 		/*
1802 		 * If a section requires ordered processing, keep track of the
1803 		 * section index and count to optimize later section traversal.
1804 		 */
1805 		if (isp->is_flags & FLG_IS_ORDERED) {
1806 			ordcnt++;
1807 			if (ordndx == 0)
1808 				ordndx = ndx;
1809 		}
1810 	}
1811 
1812 	/*
1813 	 * Some sections have special ordering requirements, that are based off
1814 	 * of the section pointed to by their sh_info entry.  This controlling
1815 	 * section will have been placed (above), and thus any ordered sections
1816 	 * can now be processed.
1817 	 */
1818 	if (ifl->ifl_flags & FLG_IF_ORDERED) {
1819 		Word	cnt = 0;
1820 
1821 		for (ndx = ordndx;
1822 		    (ndx < ifl->ifl_shnum) && (cnt < ordcnt); ndx++) {
1823 			Is_desc	*isp;
1824 
1825 			if (((isp = ifl->ifl_isdesc[ndx]) == NULL) ||
1826 			    ((isp->is_flags & FLG_IS_ORDERED) == 0))
1827 				continue;
1828 
1829 			if (ld_process_ordered(ifl, ofl, ndx,
1830 			    ifl->ifl_shnum) == S_ERROR)
1831 				return (S_ERROR);
1832 		}
1833 	}
1834 
1835 	/*
1836 	 * If this is an explicit shared object determine if the user has
1837 	 * specified a control definition.  This descriptor may specify which
1838 	 * version definitions can be used from this object (it may also update
1839 	 * the dependency to USED and supply an alternative SONAME).
1840 	 */
1841 	sdf = 0;
1842 	if (column && (ifl->ifl_flags & FLG_IF_NEEDED)) {
1843 		const char	*base;
1844 
1845 		/*
1846 		 * Use the basename of the input file (typically this is the
1847 		 * compilation environment name, ie. libfoo.so).
1848 		 */
1849 		if ((base = strrchr(ifl->ifl_name, '/')) == NULL)
1850 			base = ifl->ifl_name;
1851 		else
1852 			base++;
1853 
1854 		if ((sdf = sdf_find(base, &ofl->ofl_socntl)) != 0) {
1855 			sdf->sdf_file = ifl;
1856 			ifl->ifl_sdfdesc = sdf;
1857 		}
1858 	}
1859 
1860 	/*
1861 	 * Process any hardware/software capabilities sections.  Only the
1862 	 * capabilities for input relocatable objects are propagated.  If the
1863 	 * relocatable objects don't contain any capabilities, any capability
1864 	 * state that has already been gathered will prevail.
1865 	 */
1866 	if (capisp)
1867 		process_cap(ifl, capisp, ofl);
1868 
1869 	/*
1870 	 * Process any version dependencies.  These will establish shared object
1871 	 * `needed' entries in the same manner as will be generated from the
1872 	 * .dynamic's NEEDED entries.
1873 	 */
1874 	if (vndisp && (ofl->ofl_flags & (FLG_OF_NOUNDEF | FLG_OF_SYMBOLIC)))
1875 		if (ld_vers_need_process(vndisp, ifl, ofl) == S_ERROR)
1876 			return (S_ERROR);
1877 
1878 	/*
1879 	 * Before processing any symbol resolution or relocations process any
1880 	 * version sections.
1881 	 */
1882 	if (vsyisp)
1883 		(void) ld_vers_sym_process(ofl->ofl_lml, vsyisp, ifl);
1884 
1885 	if (ifl->ifl_versym &&
1886 	    (vdfisp || (sdf && (sdf->sdf_flags & FLG_SDF_SELECT))))
1887 		if (ld_vers_def_process(vdfisp, ifl, ofl) == S_ERROR)
1888 			return (S_ERROR);
1889 
1890 	/*
1891 	 * Having collected the appropriate sections carry out any additional
1892 	 * processing if necessary.
1893 	 */
1894 	for (ndx = 0; ndx < ifl->ifl_shnum; ndx++) {
1895 		Is_desc	*isp;
1896 
1897 		if ((isp = ifl->ifl_isdesc[ndx]) == 0)
1898 			continue;
1899 		row = isp->is_shdr->sh_type;
1900 
1901 		if ((isp->is_flags & FLG_IS_DISCARD) == 0)
1902 			ld_sup_section(ofl, isp->is_name, isp->is_shdr, ndx,
1903 			    isp->is_indata, elf);
1904 
1905 		/*
1906 		 * If this is a ST_SUNW_move section from a
1907 		 * a relocatable file, keep the input section.
1908 		 */
1909 		if ((row == SHT_SUNW_move) && (column == 0)) {
1910 			if (list_appendc(&(ofl->ofl_ismove), isp) == 0)
1911 				return (S_ERROR);
1912 		}
1913 
1914 		/*
1915 		 * If this is a standard section type process it via the
1916 		 * appropriate action routine.
1917 		 */
1918 		if (row < SHT_NUM) {
1919 			if (Final[row][column] != NULL) {
1920 				if (Final[row][column](isp, ifl,
1921 				    ofl) == S_ERROR)
1922 					return (S_ERROR);
1923 			}
1924 #if	defined(_ELF64)
1925 		} else if ((row == SHT_AMD64_UNWIND) && (column == 0)) {
1926 			Os_desc	*osp = isp->is_osdesc;
1927 
1928 			/*
1929 			 * SHT_AMD64_UNWIND (0x70000001) is in the SHT_LOPROC -
1930 			 * SHT_HIPROC range reserved for processor-specific
1931 			 * semantics, and is only meaningful for amd64 targets.
1932 			 *
1933 			 * Only process unwind contents from relocatable
1934 			 * objects.
1935 			 */
1936 			if (osp && (ld_targ.t_m.m_mach == EM_AMD64) &&
1937 			    (ld_targ.t_uw.uw_append_unwind != NULL) &&
1938 			    ((*ld_targ.t_uw.uw_append_unwind)(osp, ofl) ==
1939 			    S_ERROR))
1940 				return (S_ERROR);
1941 #endif
1942 		}
1943 	}
1944 
1945 	/*
1946 	 * After processing any symbol resolution, and if this dependency
1947 	 * indicates it contains symbols that can't be directly bound to,
1948 	 * set the symbols appropriately.
1949 	 */
1950 	if (sifisp && ((ifl->ifl_flags & (FLG_IF_NEEDED | FLG_IF_NODIRECT)) ==
1951 	    (FLG_IF_NEEDED | FLG_IF_NODIRECT)))
1952 		(void) ld_sym_nodirect(sifisp, ifl, ofl);
1953 
1954 	return (1);
1955 }
1956 
1957 /*
1958  * Process the current input file.  There are basically three types of files
1959  * that come through here:
1960  *
1961  *  o	files explicitly defined on the command line (ie. foo.o or bar.so),
1962  *	in this case only the `name' field is valid.
1963  *
1964  *  o	libraries determined from the -l command line option (ie. -lbar),
1965  *	in this case the `soname' field contains the basename of the located
1966  *	file.
1967  *
1968  * Any shared object specified via the above two conventions must be recorded
1969  * as a needed dependency.
1970  *
1971  *  o	libraries specified as dependencies of those libraries already obtained
1972  *	via the command line (ie. bar.so has a DT_NEEDED entry of fred.so.1),
1973  *	in this case the `soname' field contains either a full pathname (if the
1974  *	needed entry contained a `/'), or the basename of the located file.
1975  *	These libraries are processed to verify symbol binding but are not
1976  *	recorded as dependencies of the output file being generated.
1977  */
1978 Ifl_desc *
1979 ld_process_ifl(const char *name, const char *soname, int fd, Elf *elf,
1980     Word flags, Ofl_desc *ofl, Rej_desc *rej)
1981 {
1982 	Ifl_desc	*ifl;
1983 	Ehdr		*ehdr;
1984 	Listnode	*lnp;
1985 	uintptr_t	error = 0;
1986 	struct stat	status;
1987 	Ar_desc		*adp;
1988 	Rej_desc	_rej;
1989 
1990 	/*
1991 	 * If this file was not extracted from an archive obtain its device
1992 	 * information.  This will be used to determine if the file has already
1993 	 * been processed (rather than simply comparing filenames, the device
1994 	 * information provides a quicker comparison and detects linked files).
1995 	 */
1996 	if (!(flags & FLG_IF_EXTRACT))
1997 		(void) fstat(fd, &status);
1998 	else {
1999 		status.st_dev = 0;
2000 		status.st_ino = 0;
2001 	}
2002 
2003 	switch (elf_kind(elf)) {
2004 	case ELF_K_AR:
2005 		/*
2006 		 * Determine if we've already come across this archive file.
2007 		 */
2008 		if (!(flags & FLG_IF_EXTRACT)) {
2009 			for (LIST_TRAVERSE(&ofl->ofl_ars, lnp, adp)) {
2010 				if ((adp->ad_stdev != status.st_dev) ||
2011 				    (adp->ad_stino != status.st_ino))
2012 					continue;
2013 
2014 				/*
2015 				 * We've seen this file before so reuse the
2016 				 * original archive descriptor and discard the
2017 				 * new elf descriptor.  Note that a file
2018 				 * descriptor is unnecessary, as the file is
2019 				 * already available in memory.
2020 				 */
2021 				DBG_CALL(Dbg_file_reuse(ofl->ofl_lml, name,
2022 				    adp->ad_name));
2023 				(void) elf_end(elf);
2024 				return ((Ifl_desc *)ld_process_archive(name, -1,
2025 				    adp, ofl));
2026 			}
2027 		}
2028 
2029 		/*
2030 		 * As we haven't processed this file before establish a new
2031 		 * archive descriptor.
2032 		 */
2033 		adp = ld_ar_setup(name, elf, ofl);
2034 		if ((adp == 0) || (adp == (Ar_desc *)S_ERROR))
2035 			return ((Ifl_desc *)adp);
2036 		adp->ad_stdev = status.st_dev;
2037 		adp->ad_stino = status.st_ino;
2038 
2039 		ld_sup_file(ofl, name, ELF_K_AR, flags, elf);
2040 
2041 		/*
2042 		 * Indicate that the ELF descriptor no longer requires a file
2043 		 * descriptor by reading the entire file.  The file is already
2044 		 * read via the initial mmap(2) behind elf_begin(3elf), thus
2045 		 * this operation is effectively a no-op.  However, a side-
2046 		 * effect is that the internal file descriptor, maintained in
2047 		 * the ELF descriptor, is set to -1.  This setting will not
2048 		 * be compared with any file descriptor that is passed to
2049 		 * elf_begin(), should this archive, or one of the archive
2050 		 * members, be processed again from the command line or
2051 		 * because of a -z rescan.
2052 		 */
2053 		if (elf_cntl(elf, ELF_C_FDREAD) == -1) {
2054 			eprintf(ofl->ofl_lml, ERR_ELF, MSG_INTL(MSG_ELF_CNTL),
2055 			    name);
2056 			ofl->ofl_flags |= FLG_OF_FATAL;
2057 			return (NULL);
2058 		}
2059 
2060 		return ((Ifl_desc *)ld_process_archive(name, -1, adp, ofl));
2061 
2062 	case ELF_K_ELF:
2063 		/*
2064 		 * Obtain the elf header so that we can determine what type of
2065 		 * elf ELF_K_ELF file this is.
2066 		 */
2067 		if ((ehdr = elf_getehdr(elf)) == NULL) {
2068 			int	_class = gelf_getclass(elf);
2069 
2070 			/*
2071 			 * Failure could occur for a number of reasons at this
2072 			 * point.  Typically the files class is incorrect (ie.
2073 			 * user is building 64-bit but managed to pint at 32-bit
2074 			 * libraries).  However any number of elf errors can
2075 			 * also occur, such as from a truncated or corrupt file.
2076 			 * Here we try and get the best error message possible.
2077 			 */
2078 			if (ld_targ.t_m.m_class != _class) {
2079 				_rej.rej_type = SGS_REJ_CLASS;
2080 				_rej.rej_info = (uint_t)_class;
2081 			} else {
2082 				_rej.rej_type = SGS_REJ_STR;
2083 				_rej.rej_str = elf_errmsg(-1);
2084 			}
2085 			_rej.rej_name = name;
2086 			DBG_CALL(Dbg_file_rejected(ofl->ofl_lml, &_rej,
2087 			    ld_targ.t_m.m_mach));
2088 			if (rej->rej_type == 0) {
2089 				*rej = _rej;
2090 				rej->rej_name = strdup(_rej.rej_name);
2091 			}
2092 			return (NULL);
2093 		}
2094 
2095 		/*
2096 		 * Determine if we've already come across this file.
2097 		 */
2098 		if (!(flags & FLG_IF_EXTRACT)) {
2099 			List	*lst;
2100 
2101 			if (ehdr->e_type == ET_REL)
2102 				lst = &ofl->ofl_objs;
2103 			else
2104 				lst = &ofl->ofl_sos;
2105 
2106 			/*
2107 			 * Traverse the appropriate file list and determine if
2108 			 * a dev/inode match is found.
2109 			 */
2110 			for (LIST_TRAVERSE(lst, lnp, ifl)) {
2111 				/*
2112 				 * Ifl_desc generated via -Nneed, therefore no
2113 				 * actual file behind it.
2114 				 */
2115 				if (ifl->ifl_flags & FLG_IF_NEEDSTR)
2116 					continue;
2117 
2118 				if ((ifl->ifl_stino != status.st_ino) ||
2119 				    (ifl->ifl_stdev != status.st_dev))
2120 					continue;
2121 
2122 				/*
2123 				 * Disregard (skip) this image.
2124 				 */
2125 				DBG_CALL(Dbg_file_skip(ofl->ofl_lml,
2126 				    ifl->ifl_name, name));
2127 				(void) elf_end(elf);
2128 
2129 				/*
2130 				 * If the file was explicitly defined on the
2131 				 * command line (this is always the case for
2132 				 * relocatable objects, and is true for shared
2133 				 * objects when they weren't specified via -l or
2134 				 * were dragged in as an implicit dependency),
2135 				 * then warn the user.
2136 				 */
2137 				if ((flags & FLG_IF_CMDLINE) ||
2138 				    (ifl->ifl_flags & FLG_IF_CMDLINE)) {
2139 					const char	*errmsg;
2140 
2141 					/*
2142 					 * Determine whether this is the same
2143 					 * file name as originally encountered
2144 					 * so as to provide the most
2145 					 * descriptive diagnostic.
2146 					 */
2147 					errmsg =
2148 					    (strcmp(name, ifl->ifl_name) == 0) ?
2149 					    MSG_INTL(MSG_FIL_MULINC_1) :
2150 					    MSG_INTL(MSG_FIL_MULINC_2);
2151 					eprintf(ofl->ofl_lml, ERR_WARNING,
2152 					    errmsg, name, ifl->ifl_name);
2153 				}
2154 				return (ifl);
2155 			}
2156 		}
2157 
2158 		/*
2159 		 * At this point, we know we need the file.  Establish an input
2160 		 * file descriptor and continue processing.
2161 		 */
2162 		ifl = ifl_setup(name, ehdr, elf, flags, ofl, rej);
2163 		if ((ifl == 0) || (ifl == (Ifl_desc *)S_ERROR))
2164 			return (ifl);
2165 		ifl->ifl_stdev = status.st_dev;
2166 		ifl->ifl_stino = status.st_ino;
2167 
2168 		/*
2169 		 * If -zignore is in effect, mark this file as a potential
2170 		 * candidate (the files use isn't actually determined until
2171 		 * symbol resolution and relocation processing are completed).
2172 		 */
2173 		if (ofl->ofl_flags1 & FLG_OF1_IGNORE)
2174 			ifl->ifl_flags |= FLG_IF_IGNORE;
2175 
2176 		switch (ehdr->e_type) {
2177 		case ET_REL:
2178 			(*ld_targ.t_mr.mr_mach_eflags)(ehdr, ofl);
2179 			error = process_elf(ifl, elf, ofl);
2180 			break;
2181 		case ET_DYN:
2182 			if ((ofl->ofl_flags & FLG_OF_STATIC) ||
2183 			    !(ofl->ofl_flags & FLG_OF_DYNLIBS)) {
2184 				eprintf(ofl->ofl_lml, ERR_FATAL,
2185 				    MSG_INTL(MSG_FIL_SOINSTAT), name);
2186 				ofl->ofl_flags |= FLG_OF_FATAL;
2187 				return (NULL);
2188 			}
2189 
2190 			/*
2191 			 * Record any additional shared object information.
2192 			 * If no soname is specified (eg. this file was
2193 			 * derived from a explicit filename declaration on the
2194 			 * command line, ie. bar.so) use the pathname.
2195 			 * This entry may be overridden if the files dynamic
2196 			 * section specifies an DT_SONAME value.
2197 			 */
2198 			if (soname == NULL)
2199 				ifl->ifl_soname = ifl->ifl_name;
2200 			else
2201 				ifl->ifl_soname = soname;
2202 
2203 			/*
2204 			 * If direct bindings, lazy loading, or group
2205 			 * permissions need to be established, mark this object.
2206 			 */
2207 			if (ofl->ofl_flags1 & FLG_OF1_ZDIRECT)
2208 				ifl->ifl_flags |= FLG_IF_DIRECT;
2209 			if (ofl->ofl_flags1 & FLG_OF1_LAZYLD)
2210 				ifl->ifl_flags |= FLG_IF_LAZYLD;
2211 			if (ofl->ofl_flags1 & FLG_OF1_GRPPRM)
2212 				ifl->ifl_flags |= FLG_IF_GRPPRM;
2213 			error = process_elf(ifl, elf, ofl);
2214 
2215 			/*
2216 			 * At this point we know if this file will be
2217 			 * lazyloaded, or whether bindings to it must be direct.
2218 			 * In either case, a syminfo section is required.
2219 			 */
2220 			if (ifl->ifl_flags & (FLG_IF_LAZYLD | FLG_IF_DIRECT))
2221 				ofl->ofl_flags |= FLG_OF_SYMINFO;
2222 
2223 			break;
2224 		default:
2225 			(void) elf_errno();
2226 			_rej.rej_type = SGS_REJ_UNKFILE;
2227 			_rej.rej_name = name;
2228 			DBG_CALL(Dbg_file_rejected(ofl->ofl_lml, &_rej,
2229 			    ld_targ.t_m.m_mach));
2230 			if (rej->rej_type == 0) {
2231 				*rej = _rej;
2232 				rej->rej_name = strdup(_rej.rej_name);
2233 			}
2234 			return (NULL);
2235 		}
2236 		break;
2237 	default:
2238 		(void) elf_errno();
2239 		_rej.rej_type = SGS_REJ_UNKFILE;
2240 		_rej.rej_name = name;
2241 		DBG_CALL(Dbg_file_rejected(ofl->ofl_lml, &_rej,
2242 		    ld_targ.t_m.m_mach));
2243 		if (rej->rej_type == 0) {
2244 			*rej = _rej;
2245 			rej->rej_name = strdup(_rej.rej_name);
2246 		}
2247 		return (NULL);
2248 	}
2249 	if ((error == 0) || (error == S_ERROR))
2250 		return ((Ifl_desc *)error);
2251 	else
2252 		return (ifl);
2253 }
2254 
2255 /*
2256  * Having successfully opened a file, set up the necessary elf structures to
2257  * process it further.  This small section of processing is slightly different
2258  * from the elf initialization required to process a relocatable object from an
2259  * archive (see libs.c: ld_process_archive()).
2260  */
2261 Ifl_desc *
2262 ld_process_open(const char *opath, const char *ofile, int *fd, Ofl_desc *ofl,
2263     Word flags, Rej_desc *rej)
2264 {
2265 	Elf		*elf;
2266 	const char	*npath = opath;
2267 	const char	*nfile = ofile;
2268 
2269 	if ((elf = elf_begin(*fd, ELF_C_READ, NULL)) == NULL) {
2270 		eprintf(ofl->ofl_lml, ERR_ELF, MSG_INTL(MSG_ELF_BEGIN), npath);
2271 		ofl->ofl_flags |= FLG_OF_FATAL;
2272 		return (NULL);
2273 	}
2274 
2275 	/*
2276 	 * Determine whether the support library wishes to process this open.
2277 	 * The support library may return:
2278 	 *   .	a different ELF descriptor (in which case they should have
2279 	 *	closed the original)
2280 	 *   .	a different file descriptor (in which case they should have
2281 	 *	closed the original)
2282 	 *   .	a different path and file name (presumably associated with
2283 	 *	a different file descriptor)
2284 	 *
2285 	 * A file descriptor of -1, or and ELF descriptor of zero indicates
2286 	 * the file should be ignored.
2287 	 */
2288 	ld_sup_open(ofl, &npath, &nfile, fd, flags, &elf, NULL, 0,
2289 	    elf_kind(elf));
2290 
2291 	if ((*fd == -1) || (elf == NULL))
2292 		return (NULL);
2293 
2294 	return (ld_process_ifl(npath, nfile, *fd, elf, flags, ofl, rej));
2295 }
2296 
2297 /*
2298  * Process a required library (i.e. the dependency of a shared object).
2299  * Combine the directory and filename, check the resultant path size, and try
2300  * opening the pathname.
2301  */
2302 static Ifl_desc *
2303 process_req_lib(Sdf_desc *sdf, const char *dir, const char *file,
2304     Ofl_desc *ofl, Rej_desc *rej)
2305 {
2306 	size_t		dlen, plen;
2307 	int		fd;
2308 	char		path[PATH_MAX];
2309 	const char	*_dir = dir;
2310 
2311 	/*
2312 	 * Determine the sizes of the directory and filename to insure we don't
2313 	 * exceed our buffer.
2314 	 */
2315 	if ((dlen = strlen(dir)) == 0) {
2316 		_dir = MSG_ORIG(MSG_STR_DOT);
2317 		dlen = 1;
2318 	}
2319 	dlen++;
2320 	plen = dlen + strlen(file) + 1;
2321 	if (plen > PATH_MAX) {
2322 		eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_FIL_PTHTOLONG),
2323 		    _dir, file);
2324 		ofl->ofl_flags |= FLG_OF_FATAL;
2325 		return (0);
2326 	}
2327 
2328 	/*
2329 	 * Build the entire pathname and try and open the file.
2330 	 */
2331 	(void) strcpy(path, _dir);
2332 	(void) strcat(path, MSG_ORIG(MSG_STR_SLASH));
2333 	(void) strcat(path, file);
2334 	DBG_CALL(Dbg_libs_req(ofl->ofl_lml, sdf->sdf_name,
2335 	    sdf->sdf_rfile, path));
2336 
2337 	if ((fd = open(path, O_RDONLY)) == -1)
2338 		return (0);
2339 	else {
2340 		Ifl_desc	*ifl;
2341 		char		*_path;
2342 
2343 		if ((_path = libld_malloc(strlen(path) + 1)) == 0)
2344 			return ((Ifl_desc *)S_ERROR);
2345 		(void) strcpy(_path, path);
2346 		ifl = ld_process_open(_path, &_path[dlen], &fd, ofl, 0, rej);
2347 		if (fd != -1)
2348 			(void) close(fd);
2349 		return (ifl);
2350 	}
2351 }
2352 
2353 /*
2354  * Finish any library processing.  Walk the list of so's that have been listed
2355  * as "included" by shared objects we have previously processed.  Examine them,
2356  * without adding them as explicit dependents of this program, in order to
2357  * complete our symbol definition process.  The search path rules are:
2358  *
2359  *  o	use any user supplied paths, i.e. LD_LIBRARY_PATH and -L, then
2360  *
2361  *  o	use any RPATH defined within the parent shared object, then
2362  *
2363  *  o	use the default directories, i.e. LIBPATH or -YP.
2364  */
2365 uintptr_t
2366 ld_finish_libs(Ofl_desc *ofl)
2367 {
2368 	Listnode	*lnp1;
2369 	Sdf_desc	*sdf;
2370 	Rej_desc	rej = { 0 };
2371 
2372 	/*
2373 	 * Make sure we are back in dynamic mode.
2374 	 */
2375 	ofl->ofl_flags |= FLG_OF_DYNLIBS;
2376 
2377 	for (LIST_TRAVERSE(&ofl->ofl_soneed, lnp1, sdf)) {
2378 		Listnode	*lnp2;
2379 		char		*path, *slash = NULL;
2380 		int		fd;
2381 		Ifl_desc	*ifl;
2382 		char		*file = (char *)sdf->sdf_name;
2383 
2384 		/*
2385 		 * See if this file has already been processed.  At the time
2386 		 * this implicit dependency was determined there may still have
2387 		 * been more explicit dependencies to process.  Note, if we ever
2388 		 * do parse the command line three times we would be able to
2389 		 * do all this checking when processing the dynamic section.
2390 		 */
2391 		if (sdf->sdf_file)
2392 			continue;
2393 
2394 		for (LIST_TRAVERSE(&ofl->ofl_sos, lnp2, ifl)) {
2395 			if (!(ifl->ifl_flags & FLG_IF_NEEDSTR) &&
2396 			    (strcmp(file, ifl->ifl_soname) == 0)) {
2397 				sdf->sdf_file = ifl;
2398 				break;
2399 			}
2400 		}
2401 		if (sdf->sdf_file)
2402 			continue;
2403 
2404 		/*
2405 		 * If the current path name element embeds a "/", then it's to
2406 		 * be taken "as is", with no searching involved.  Process all
2407 		 * "/" occurrences, so that we can deduce the base file name.
2408 		 */
2409 		for (path = file; *path; path++) {
2410 			if (*path == '/')
2411 				slash = path;
2412 		}
2413 		if (slash) {
2414 			DBG_CALL(Dbg_libs_req(ofl->ofl_lml, sdf->sdf_name,
2415 			    sdf->sdf_rfile, file));
2416 			if ((fd = open(file, O_RDONLY)) == -1) {
2417 				eprintf(ofl->ofl_lml, ERR_WARNING,
2418 				    MSG_INTL(MSG_FIL_NOTFOUND), file,
2419 				    sdf->sdf_rfile);
2420 			} else {
2421 				Rej_desc	_rej = { 0 };
2422 
2423 				ifl = ld_process_open(file, ++slash, &fd, ofl,
2424 				    0, &_rej);
2425 				if (fd != -1)
2426 					(void) close(fd);
2427 				if (ifl == (Ifl_desc *)S_ERROR)
2428 					return (S_ERROR);
2429 
2430 				if (_rej.rej_type) {
2431 					Conv_reject_desc_buf_t rej_buf;
2432 
2433 					eprintf(ofl->ofl_lml, ERR_WARNING,
2434 					    MSG_INTL(reject[_rej.rej_type]),
2435 					    _rej.rej_name ? rej.rej_name :
2436 					    MSG_INTL(MSG_STR_UNKNOWN),
2437 					    conv_reject_desc(&_rej, &rej_buf,
2438 					    ld_targ.t_m.m_mach));
2439 				} else
2440 					sdf->sdf_file = ifl;
2441 			}
2442 			continue;
2443 		}
2444 
2445 		/*
2446 		 * Now search for this file in any user defined directories.
2447 		 */
2448 		for (LIST_TRAVERSE(&ofl->ofl_ulibdirs, lnp2, path)) {
2449 			Rej_desc	_rej = { 0 };
2450 
2451 			ifl = process_req_lib(sdf, path, file, ofl, &_rej);
2452 			if (ifl == (Ifl_desc *)S_ERROR) {
2453 				return (S_ERROR);
2454 			}
2455 			if (_rej.rej_type) {
2456 				if (rej.rej_type == 0) {
2457 					rej = _rej;
2458 					rej.rej_name = strdup(_rej.rej_name);
2459 				}
2460 			}
2461 			if (ifl) {
2462 				sdf->sdf_file = ifl;
2463 				break;
2464 			}
2465 		}
2466 		if (sdf->sdf_file)
2467 			continue;
2468 
2469 		/*
2470 		 * Next use the local rules defined within the parent shared
2471 		 * object.
2472 		 */
2473 		if (sdf->sdf_rpath != NULL) {
2474 			char	*rpath, *next;
2475 
2476 			rpath = libld_malloc(strlen(sdf->sdf_rpath) + 1);
2477 			if (rpath == 0)
2478 				return (S_ERROR);
2479 			(void) strcpy(rpath, sdf->sdf_rpath);
2480 			DBG_CALL(Dbg_libs_path(ofl->ofl_lml, rpath,
2481 			    LA_SER_RUNPATH, sdf->sdf_rfile));
2482 			if ((path = strtok_r(rpath,
2483 			    MSG_ORIG(MSG_STR_COLON), &next)) != NULL) {
2484 				do {
2485 					Rej_desc	_rej = { 0 };
2486 
2487 					path = expand(sdf->sdf_rfile, path,
2488 					    &next);
2489 
2490 					ifl = process_req_lib(sdf, path,
2491 					    file, ofl, &_rej);
2492 					if (ifl == (Ifl_desc *)S_ERROR) {
2493 						return (S_ERROR);
2494 					}
2495 					if ((_rej.rej_type) &&
2496 					    (rej.rej_type == 0)) {
2497 						rej = _rej;
2498 						rej.rej_name =
2499 						    strdup(_rej.rej_name);
2500 					}
2501 					if (ifl) {
2502 						sdf->sdf_file = ifl;
2503 						break;
2504 					}
2505 				} while ((path = strtok_r(NULL,
2506 				    MSG_ORIG(MSG_STR_COLON), &next)) != NULL);
2507 			}
2508 		}
2509 		if (sdf->sdf_file)
2510 			continue;
2511 
2512 		/*
2513 		 * Finally try the default library search directories.
2514 		 */
2515 		for (LIST_TRAVERSE(&ofl->ofl_dlibdirs, lnp2, path)) {
2516 			Rej_desc	_rej = { 0 };
2517 
2518 			ifl = process_req_lib(sdf, path, file, ofl, &rej);
2519 			if (ifl == (Ifl_desc *)S_ERROR) {
2520 				return (S_ERROR);
2521 			}
2522 			if (_rej.rej_type) {
2523 				if (rej.rej_type == 0) {
2524 					rej = _rej;
2525 					rej.rej_name = strdup(_rej.rej_name);
2526 				}
2527 			}
2528 			if (ifl) {
2529 				sdf->sdf_file = ifl;
2530 				break;
2531 			}
2532 		}
2533 		if (sdf->sdf_file)
2534 			continue;
2535 
2536 		/*
2537 		 * If we've got this far we haven't found the shared object.
2538 		 * If an object was found, but was rejected for some reason,
2539 		 * print a diagnostic to that effect, otherwise generate a
2540 		 * generic "not found" diagnostic.
2541 		 */
2542 		if (rej.rej_type) {
2543 			Conv_reject_desc_buf_t rej_buf;
2544 
2545 			eprintf(ofl->ofl_lml, ERR_WARNING,
2546 			    MSG_INTL(reject[rej.rej_type]),
2547 			    rej.rej_name ? rej.rej_name :
2548 			    MSG_INTL(MSG_STR_UNKNOWN),
2549 			    conv_reject_desc(&rej, &rej_buf,
2550 			    ld_targ.t_m.m_mach));
2551 		} else {
2552 			eprintf(ofl->ofl_lml, ERR_WARNING,
2553 			    MSG_INTL(MSG_FIL_NOTFOUND), file, sdf->sdf_rfile);
2554 		}
2555 	}
2556 
2557 	/*
2558 	 * Finally, now that all objects have been input, make sure any version
2559 	 * requirements have been met.
2560 	 */
2561 	return (ld_vers_verify(ofl));
2562 }
2563