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