xref: /titanic_52/usr/src/cmd/sgs/libld/common/files.c (revision 88447a05f537aabe9a1bc3d5313f22581ec992a7)
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 input sections have been processed, place them
1857 	 * in the appropriate output sections.
1858 	 */
1859 	for (ndx = 1; ndx < ifl->ifl_shnum; ndx++) {
1860 		Is_desc	*isp;
1861 		Shdr	*shdr;
1862 
1863 		if (((isp = ifl->ifl_isdesc[ndx]) == NULL) ||
1864 		    ((isp->is_flags & FLG_IS_PLACE) == 0))
1865 			continue;
1866 
1867 		shdr = isp->is_shdr;
1868 
1869 		/*
1870 		 * Place all non-ordered sections within their appropriate
1871 		 * output section.
1872 		 *
1873 		 * Ordered sections are sorted based on the relative ordering
1874 		 * of the section pointed to by the sh_info entry.  An ordered
1875 		 * section, whose sh_link points to itself, must also be placed
1876 		 * in the output image so as to control the ordered processing
1877 		 * that follows (see FLG_IF_ORDERED below).
1878 		 */
1879 		if (((isp->is_flags & FLG_IS_ORDERED) == 0) ||
1880 		    ((ndx == shdr->sh_link) &&
1881 		    (shdr->sh_flags & SHF_ORDERED))) {
1882 			if (ld_place_section(ofl, isp,
1883 			    isp->is_keyident, 0) == (Os_desc *)S_ERROR)
1884 				return (S_ERROR);
1885 		}
1886 
1887 		/*
1888 		 * If a section requires ordered processing, keep track of the
1889 		 * section index and count to optimize later section traversal.
1890 		 */
1891 		if (isp->is_flags & FLG_IS_ORDERED) {
1892 			ordcnt++;
1893 			if (ordndx == 0)
1894 				ordndx = ndx;
1895 		}
1896 	}
1897 
1898 	/*
1899 	 * Some sections have special ordering requirements, that are based off
1900 	 * of the section pointed to by their sh_info entry.  This controlling
1901 	 * section will have been placed (above), and thus any ordered sections
1902 	 * can now be processed.
1903 	 */
1904 	if (ifl->ifl_flags & FLG_IF_ORDERED) {
1905 		Word	cnt = 0;
1906 
1907 		for (ndx = ordndx;
1908 		    (ndx < ifl->ifl_shnum) && (cnt < ordcnt); ndx++) {
1909 			Is_desc	*isp;
1910 
1911 			if (((isp = ifl->ifl_isdesc[ndx]) == NULL) ||
1912 			    ((isp->is_flags & FLG_IS_ORDERED) == 0))
1913 				continue;
1914 
1915 			if (ld_process_ordered(ifl, ofl, ndx) == S_ERROR)
1916 				return (S_ERROR);
1917 		}
1918 	}
1919 
1920 	/*
1921 	 * If this is an explicit shared object determine if the user has
1922 	 * specified a control definition.  This descriptor may specify which
1923 	 * version definitions can be used from this object (it may also update
1924 	 * the dependency to USED and supply an alternative SONAME).
1925 	 */
1926 	sdf = 0;
1927 	if (column && (ifl->ifl_flags & FLG_IF_NEEDED)) {
1928 		const char	*base;
1929 
1930 		/*
1931 		 * Use the basename of the input file (typically this is the
1932 		 * compilation environment name, ie. libfoo.so).
1933 		 */
1934 		if ((base = strrchr(ifl->ifl_name, '/')) == NULL)
1935 			base = ifl->ifl_name;
1936 		else
1937 			base++;
1938 
1939 		if ((sdf = sdf_find(base, ofl->ofl_socntl)) != NULL) {
1940 			sdf->sdf_file = ifl;
1941 			ifl->ifl_sdfdesc = sdf;
1942 		}
1943 	}
1944 
1945 	/*
1946 	 * Process any hardware/software capabilities sections.  Only the
1947 	 * capabilities for input relocatable objects are propagated.  If the
1948 	 * relocatable objects don't contain any capabilities, any capability
1949 	 * state that has already been gathered will prevail.
1950 	 */
1951 	if (capisp)
1952 		process_cap(ifl, capisp, ofl);
1953 
1954 	/*
1955 	 * Process any version dependencies.  These will establish shared object
1956 	 * `needed' entries in the same manner as will be generated from the
1957 	 * .dynamic's NEEDED entries.
1958 	 */
1959 	if (vndisp && (ofl->ofl_flags & (FLG_OF_NOUNDEF | FLG_OF_SYMBOLIC)))
1960 		if (ld_vers_need_process(vndisp, ifl, ofl) == S_ERROR)
1961 			return (S_ERROR);
1962 
1963 	/*
1964 	 * Before processing any symbol resolution or relocations process any
1965 	 * version sections.
1966 	 */
1967 	if (vsyisp)
1968 		(void) ld_vers_sym_process(ofl->ofl_lml, vsyisp, ifl);
1969 
1970 	if (ifl->ifl_versym &&
1971 	    (vdfisp || (sdf && (sdf->sdf_flags & FLG_SDF_SELECT))))
1972 		if (ld_vers_def_process(vdfisp, ifl, ofl) == S_ERROR)
1973 			return (S_ERROR);
1974 
1975 	/*
1976 	 * Having collected the appropriate sections carry out any additional
1977 	 * processing if necessary.
1978 	 */
1979 	for (ndx = 0; ndx < ifl->ifl_shnum; ndx++) {
1980 		Is_desc	*isp;
1981 
1982 		if ((isp = ifl->ifl_isdesc[ndx]) == 0)
1983 			continue;
1984 		row = isp->is_shdr->sh_type;
1985 
1986 		if ((isp->is_flags & FLG_IS_DISCARD) == 0)
1987 			ld_sup_section(ofl, isp->is_name, isp->is_shdr, ndx,
1988 			    isp->is_indata, elf);
1989 
1990 		/*
1991 		 * If this is a SHT_SUNW_move section from a relocatable file,
1992 		 * keep track of the section for later processing.
1993 		 */
1994 		if ((row == SHT_SUNW_move) && (column == 0)) {
1995 			if (aplist_append(&(ofl->ofl_ismove), isp,
1996 			    AL_CNT_OFL_MOVE) == NULL)
1997 				return (S_ERROR);
1998 		}
1999 
2000 		/*
2001 		 * If this is a standard section type process it via the
2002 		 * appropriate action routine.
2003 		 */
2004 		if (row < SHT_NUM) {
2005 			if (Final[row][column] != NULL) {
2006 				if (Final[row][column](isp, ifl,
2007 				    ofl) == S_ERROR)
2008 					return (S_ERROR);
2009 			}
2010 #if	defined(_ELF64)
2011 		} else if ((row == SHT_AMD64_UNWIND) && (column == 0)) {
2012 			Os_desc	*osp = isp->is_osdesc;
2013 
2014 			/*
2015 			 * SHT_AMD64_UNWIND (0x70000001) is in the SHT_LOPROC -
2016 			 * SHT_HIPROC range reserved for processor-specific
2017 			 * semantics, and is only meaningful for amd64 targets.
2018 			 *
2019 			 * Only process unwind contents from relocatable
2020 			 * objects.
2021 			 */
2022 			if (osp && (ld_targ.t_m.m_mach == EM_AMD64) &&
2023 			    (ld_unwind_register(osp, ofl) == S_ERROR))
2024 				return (S_ERROR);
2025 #endif
2026 		}
2027 	}
2028 
2029 	/*
2030 	 * After processing any symbol resolution, and if this dependency
2031 	 * indicates it contains symbols that can't be directly bound to,
2032 	 * set the symbols appropriately.
2033 	 */
2034 	if (sifisp && ((ifl->ifl_flags & (FLG_IF_NEEDED | FLG_IF_NODIRECT)) ==
2035 	    (FLG_IF_NEEDED | FLG_IF_NODIRECT)))
2036 		(void) ld_sym_nodirect(sifisp, ifl, ofl);
2037 
2038 	return (1);
2039 }
2040 
2041 /*
2042  * Process the current input file.  There are basically three types of files
2043  * that come through here:
2044  *
2045  *  o	files explicitly defined on the command line (ie. foo.o or bar.so),
2046  *	in this case only the `name' field is valid.
2047  *
2048  *  o	libraries determined from the -l command line option (ie. -lbar),
2049  *	in this case the `soname' field contains the basename of the located
2050  *	file.
2051  *
2052  * Any shared object specified via the above two conventions must be recorded
2053  * as a needed dependency.
2054  *
2055  *  o	libraries specified as dependencies of those libraries already obtained
2056  *	via the command line (ie. bar.so has a DT_NEEDED entry of fred.so.1),
2057  *	in this case the `soname' field contains either a full pathname (if the
2058  *	needed entry contained a `/'), or the basename of the located file.
2059  *	These libraries are processed to verify symbol binding but are not
2060  *	recorded as dependencies of the output file being generated.
2061  */
2062 Ifl_desc *
2063 ld_process_ifl(const char *name, const char *soname, int fd, Elf *elf,
2064     Word flags, Ofl_desc *ofl, Rej_desc *rej)
2065 {
2066 	Ifl_desc	*ifl;
2067 	Ehdr		*ehdr;
2068 	uintptr_t	error = 0;
2069 	struct stat	status;
2070 	Ar_desc		*adp;
2071 	Rej_desc	_rej;
2072 
2073 	/*
2074 	 * If this file was not extracted from an archive obtain its device
2075 	 * information.  This will be used to determine if the file has already
2076 	 * been processed (rather than simply comparing filenames, the device
2077 	 * information provides a quicker comparison and detects linked files).
2078 	 */
2079 	if (fd && ((flags & FLG_IF_EXTRACT) == 0))
2080 		(void) fstat(fd, &status);
2081 	else {
2082 		status.st_dev = 0;
2083 		status.st_ino = 0;
2084 	}
2085 
2086 	switch (elf_kind(elf)) {
2087 	case ELF_K_AR:
2088 		/*
2089 		 * Determine if we've already come across this archive file.
2090 		 */
2091 		if (!(flags & FLG_IF_EXTRACT)) {
2092 			Aliste	idx;
2093 
2094 			for (APLIST_TRAVERSE(ofl->ofl_ars, idx, adp)) {
2095 				if ((adp->ad_stdev != status.st_dev) ||
2096 				    (adp->ad_stino != status.st_ino))
2097 					continue;
2098 
2099 				/*
2100 				 * We've seen this file before so reuse the
2101 				 * original archive descriptor and discard the
2102 				 * new elf descriptor.  Note that a file
2103 				 * descriptor is unnecessary, as the file is
2104 				 * already available in memory.
2105 				 */
2106 				DBG_CALL(Dbg_file_reuse(ofl->ofl_lml, name,
2107 				    adp->ad_name));
2108 				(void) elf_end(elf);
2109 				return ((Ifl_desc *)ld_process_archive(name, -1,
2110 				    adp, ofl));
2111 			}
2112 		}
2113 
2114 		/*
2115 		 * As we haven't processed this file before establish a new
2116 		 * archive descriptor.
2117 		 */
2118 		adp = ld_ar_setup(name, elf, ofl);
2119 		if ((adp == 0) || (adp == (Ar_desc *)S_ERROR))
2120 			return ((Ifl_desc *)adp);
2121 		adp->ad_stdev = status.st_dev;
2122 		adp->ad_stino = status.st_ino;
2123 
2124 		ld_sup_file(ofl, name, ELF_K_AR, flags, elf);
2125 
2126 		/*
2127 		 * Indicate that the ELF descriptor no longer requires a file
2128 		 * descriptor by reading the entire file.  The file is already
2129 		 * read via the initial mmap(2) behind elf_begin(3elf), thus
2130 		 * this operation is effectively a no-op.  However, a side-
2131 		 * effect is that the internal file descriptor, maintained in
2132 		 * the ELF descriptor, is set to -1.  This setting will not
2133 		 * be compared with any file descriptor that is passed to
2134 		 * elf_begin(), should this archive, or one of the archive
2135 		 * members, be processed again from the command line or
2136 		 * because of a -z rescan.
2137 		 */
2138 		if (elf_cntl(elf, ELF_C_FDREAD) == -1) {
2139 			eprintf(ofl->ofl_lml, ERR_ELF, MSG_INTL(MSG_ELF_CNTL),
2140 			    name);
2141 			ofl->ofl_flags |= FLG_OF_FATAL;
2142 			return (NULL);
2143 		}
2144 
2145 		return ((Ifl_desc *)ld_process_archive(name, -1, adp, ofl));
2146 
2147 	case ELF_K_ELF:
2148 		/*
2149 		 * Obtain the elf header so that we can determine what type of
2150 		 * elf ELF_K_ELF file this is.
2151 		 */
2152 		if ((ehdr = elf_getehdr(elf)) == NULL) {
2153 			int	_class = gelf_getclass(elf);
2154 
2155 			/*
2156 			 * Failure could occur for a number of reasons at this
2157 			 * point.  Typically the files class is incorrect (ie.
2158 			 * user is building 64-bit but managed to pint at 32-bit
2159 			 * libraries).  However any number of elf errors can
2160 			 * also occur, such as from a truncated or corrupt file.
2161 			 * Here we try and get the best error message possible.
2162 			 */
2163 			if (ld_targ.t_m.m_class != _class) {
2164 				_rej.rej_type = SGS_REJ_CLASS;
2165 				_rej.rej_info = (uint_t)_class;
2166 			} else {
2167 				_rej.rej_type = SGS_REJ_STR;
2168 				_rej.rej_str = elf_errmsg(-1);
2169 			}
2170 			_rej.rej_name = name;
2171 			DBG_CALL(Dbg_file_rejected(ofl->ofl_lml, &_rej,
2172 			    ld_targ.t_m.m_mach));
2173 			if (rej->rej_type == 0) {
2174 				*rej = _rej;
2175 				rej->rej_name = strdup(_rej.rej_name);
2176 			}
2177 			return (NULL);
2178 		}
2179 
2180 		/*
2181 		 * Determine if we've already come across this file.
2182 		 */
2183 		if (!(flags & FLG_IF_EXTRACT)) {
2184 			APlist	*apl;
2185 			Aliste	idx;
2186 
2187 			if (ehdr->e_type == ET_REL)
2188 				apl = ofl->ofl_objs;
2189 			else
2190 				apl = ofl->ofl_sos;
2191 
2192 			/*
2193 			 * Traverse the appropriate file list and determine if
2194 			 * a dev/inode match is found.
2195 			 */
2196 			for (APLIST_TRAVERSE(apl, idx, ifl)) {
2197 				/*
2198 				 * Ifl_desc generated via -Nneed, therefore no
2199 				 * actual file behind it.
2200 				 */
2201 				if (ifl->ifl_flags & FLG_IF_NEEDSTR)
2202 					continue;
2203 
2204 				if ((ifl->ifl_stino != status.st_ino) ||
2205 				    (ifl->ifl_stdev != status.st_dev))
2206 					continue;
2207 
2208 				/*
2209 				 * Disregard (skip) this image.
2210 				 */
2211 				DBG_CALL(Dbg_file_skip(ofl->ofl_lml,
2212 				    ifl->ifl_name, name));
2213 				(void) elf_end(elf);
2214 
2215 				/*
2216 				 * If the file was explicitly defined on the
2217 				 * command line (this is always the case for
2218 				 * relocatable objects, and is true for shared
2219 				 * objects when they weren't specified via -l or
2220 				 * were dragged in as an implicit dependency),
2221 				 * then warn the user.
2222 				 */
2223 				if ((flags & FLG_IF_CMDLINE) ||
2224 				    (ifl->ifl_flags & FLG_IF_CMDLINE)) {
2225 					const char	*errmsg;
2226 
2227 					/*
2228 					 * Determine whether this is the same
2229 					 * file name as originally encountered
2230 					 * so as to provide the most
2231 					 * descriptive diagnostic.
2232 					 */
2233 					errmsg =
2234 					    (strcmp(name, ifl->ifl_name) == 0) ?
2235 					    MSG_INTL(MSG_FIL_MULINC_1) :
2236 					    MSG_INTL(MSG_FIL_MULINC_2);
2237 					eprintf(ofl->ofl_lml, ERR_WARNING,
2238 					    errmsg, name, ifl->ifl_name);
2239 				}
2240 				return (ifl);
2241 			}
2242 		}
2243 
2244 		/*
2245 		 * At this point, we know we need the file.  Establish an input
2246 		 * file descriptor and continue processing.
2247 		 */
2248 		ifl = ifl_setup(name, ehdr, elf, flags, ofl, rej);
2249 		if ((ifl == 0) || (ifl == (Ifl_desc *)S_ERROR))
2250 			return (ifl);
2251 		ifl->ifl_stdev = status.st_dev;
2252 		ifl->ifl_stino = status.st_ino;
2253 
2254 		/*
2255 		 * If -zignore is in effect, mark this file as a potential
2256 		 * candidate (the files use isn't actually determined until
2257 		 * symbol resolution and relocation processing are completed).
2258 		 */
2259 		if (ofl->ofl_flags1 & FLG_OF1_IGNORE)
2260 			ifl->ifl_flags |= FLG_IF_IGNORE;
2261 
2262 		switch (ehdr->e_type) {
2263 		case ET_REL:
2264 			(*ld_targ.t_mr.mr_mach_eflags)(ehdr, ofl);
2265 			error = process_elf(ifl, elf, ofl);
2266 			break;
2267 		case ET_DYN:
2268 			if ((ofl->ofl_flags & FLG_OF_STATIC) ||
2269 			    !(ofl->ofl_flags & FLG_OF_DYNLIBS)) {
2270 				eprintf(ofl->ofl_lml, ERR_FATAL,
2271 				    MSG_INTL(MSG_FIL_SOINSTAT), name);
2272 				ofl->ofl_flags |= FLG_OF_FATAL;
2273 				return (NULL);
2274 			}
2275 
2276 			/*
2277 			 * Record any additional shared object information.
2278 			 * If no soname is specified (eg. this file was
2279 			 * derived from a explicit filename declaration on the
2280 			 * command line, ie. bar.so) use the pathname.
2281 			 * This entry may be overridden if the files dynamic
2282 			 * section specifies an DT_SONAME value.
2283 			 */
2284 			if (soname == NULL)
2285 				ifl->ifl_soname = ifl->ifl_name;
2286 			else
2287 				ifl->ifl_soname = soname;
2288 
2289 			/*
2290 			 * If direct bindings, lazy loading, or group
2291 			 * permissions need to be established, mark this object.
2292 			 */
2293 			if (ofl->ofl_flags1 & FLG_OF1_ZDIRECT)
2294 				ifl->ifl_flags |= FLG_IF_DIRECT;
2295 			if (ofl->ofl_flags1 & FLG_OF1_LAZYLD)
2296 				ifl->ifl_flags |= FLG_IF_LAZYLD;
2297 			if (ofl->ofl_flags1 & FLG_OF1_GRPPRM)
2298 				ifl->ifl_flags |= FLG_IF_GRPPRM;
2299 			error = process_elf(ifl, elf, ofl);
2300 
2301 			/*
2302 			 * At this point we know if this file will be
2303 			 * lazyloaded, or whether bindings to it must be direct.
2304 			 * In either case, a syminfo section is required.
2305 			 */
2306 			if (ifl->ifl_flags & (FLG_IF_LAZYLD | FLG_IF_DIRECT))
2307 				ofl->ofl_flags |= FLG_OF_SYMINFO;
2308 
2309 			break;
2310 		default:
2311 			(void) elf_errno();
2312 			_rej.rej_type = SGS_REJ_UNKFILE;
2313 			_rej.rej_name = name;
2314 			DBG_CALL(Dbg_file_rejected(ofl->ofl_lml, &_rej,
2315 			    ld_targ.t_m.m_mach));
2316 			if (rej->rej_type == 0) {
2317 				*rej = _rej;
2318 				rej->rej_name = strdup(_rej.rej_name);
2319 			}
2320 			return (NULL);
2321 		}
2322 		break;
2323 	default:
2324 		(void) elf_errno();
2325 		_rej.rej_type = SGS_REJ_UNKFILE;
2326 		_rej.rej_name = name;
2327 		DBG_CALL(Dbg_file_rejected(ofl->ofl_lml, &_rej,
2328 		    ld_targ.t_m.m_mach));
2329 		if (rej->rej_type == 0) {
2330 			*rej = _rej;
2331 			rej->rej_name = strdup(_rej.rej_name);
2332 		}
2333 		return (NULL);
2334 	}
2335 	if ((error == 0) || (error == S_ERROR))
2336 		return ((Ifl_desc *)error);
2337 	else
2338 		return (ifl);
2339 }
2340 
2341 /*
2342  * Having successfully opened a file, set up the necessary elf structures to
2343  * process it further.  This small section of processing is slightly different
2344  * from the elf initialization required to process a relocatable object from an
2345  * archive (see libs.c: ld_process_archive()).
2346  */
2347 Ifl_desc *
2348 ld_process_open(const char *opath, const char *ofile, int *fd, Ofl_desc *ofl,
2349     Word flags, Rej_desc *rej)
2350 {
2351 	Elf		*elf;
2352 	const char	*npath = opath;
2353 	const char	*nfile = ofile;
2354 
2355 	if ((elf = elf_begin(*fd, ELF_C_READ, NULL)) == NULL) {
2356 		eprintf(ofl->ofl_lml, ERR_ELF, MSG_INTL(MSG_ELF_BEGIN), npath);
2357 		ofl->ofl_flags |= FLG_OF_FATAL;
2358 		return (NULL);
2359 	}
2360 
2361 	/*
2362 	 * Determine whether the support library wishes to process this open.
2363 	 * The support library may return:
2364 	 *   .	a different ELF descriptor (in which case they should have
2365 	 *	closed the original)
2366 	 *   .	a different file descriptor (in which case they should have
2367 	 *	closed the original)
2368 	 *   .	a different path and file name (presumably associated with
2369 	 *	a different file descriptor)
2370 	 *
2371 	 * A file descriptor of -1, or and ELF descriptor of zero indicates
2372 	 * the file should be ignored.
2373 	 */
2374 	ld_sup_open(ofl, &npath, &nfile, fd, flags, &elf, NULL, 0,
2375 	    elf_kind(elf));
2376 
2377 	if ((*fd == -1) || (elf == NULL))
2378 		return (NULL);
2379 
2380 	return (ld_process_ifl(npath, nfile, *fd, elf, flags, ofl, rej));
2381 }
2382 
2383 /*
2384  * Having successfully mapped a file, set up the necessary elf structures to
2385  * process it further.  This routine is patterned after ld_process_open() and
2386  * is only called by ld.so.1(1) to process a relocatable object.
2387  */
2388 Ifl_desc *
2389 ld_process_mem(const char *path, const char *file, char *addr, size_t size,
2390     Ofl_desc *ofl, Rej_desc *rej)
2391 {
2392 	Elf	*elf;
2393 
2394 	if ((elf = elf_memory(addr, size)) == NULL) {
2395 		eprintf(ofl->ofl_lml, ERR_ELF, MSG_INTL(MSG_ELF_MEMORY), path);
2396 		ofl->ofl_flags |= FLG_OF_FATAL;
2397 		return (0);
2398 	}
2399 
2400 	return (ld_process_ifl(path, file, 0, elf, 0, ofl, rej));
2401 }
2402 
2403 /*
2404  * Process a required library (i.e. the dependency of a shared object).
2405  * Combine the directory and filename, check the resultant path size, and try
2406  * opening the pathname.
2407  */
2408 static Ifl_desc *
2409 process_req_lib(Sdf_desc *sdf, const char *dir, const char *file,
2410     Ofl_desc *ofl, Rej_desc *rej)
2411 {
2412 	size_t		dlen, plen;
2413 	int		fd;
2414 	char		path[PATH_MAX];
2415 	const char	*_dir = dir;
2416 
2417 	/*
2418 	 * Determine the sizes of the directory and filename to insure we don't
2419 	 * exceed our buffer.
2420 	 */
2421 	if ((dlen = strlen(dir)) == 0) {
2422 		_dir = MSG_ORIG(MSG_STR_DOT);
2423 		dlen = 1;
2424 	}
2425 	dlen++;
2426 	plen = dlen + strlen(file) + 1;
2427 	if (plen > PATH_MAX) {
2428 		eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_FIL_PTHTOLONG),
2429 		    _dir, file);
2430 		ofl->ofl_flags |= FLG_OF_FATAL;
2431 		return (0);
2432 	}
2433 
2434 	/*
2435 	 * Build the entire pathname and try and open the file.
2436 	 */
2437 	(void) strcpy(path, _dir);
2438 	(void) strcat(path, MSG_ORIG(MSG_STR_SLASH));
2439 	(void) strcat(path, file);
2440 	DBG_CALL(Dbg_libs_req(ofl->ofl_lml, sdf->sdf_name,
2441 	    sdf->sdf_rfile, path));
2442 
2443 	if ((fd = open(path, O_RDONLY)) == -1)
2444 		return (0);
2445 	else {
2446 		Ifl_desc	*ifl;
2447 		char		*_path;
2448 
2449 		if ((_path = libld_malloc(strlen(path) + 1)) == 0)
2450 			return ((Ifl_desc *)S_ERROR);
2451 		(void) strcpy(_path, path);
2452 		ifl = ld_process_open(_path, &_path[dlen], &fd, ofl, 0, rej);
2453 		if (fd != -1)
2454 			(void) close(fd);
2455 		return (ifl);
2456 	}
2457 }
2458 
2459 /*
2460  * Finish any library processing.  Walk the list of so's that have been listed
2461  * as "included" by shared objects we have previously processed.  Examine them,
2462  * without adding them as explicit dependents of this program, in order to
2463  * complete our symbol definition process.  The search path rules are:
2464  *
2465  *  o	use any user supplied paths, i.e. LD_LIBRARY_PATH and -L, then
2466  *
2467  *  o	use any RPATH defined within the parent shared object, then
2468  *
2469  *  o	use the default directories, i.e. LIBPATH or -YP.
2470  */
2471 uintptr_t
2472 ld_finish_libs(Ofl_desc *ofl)
2473 {
2474 	Aliste		idx1;
2475 	Sdf_desc	*sdf;
2476 	Rej_desc	rej = { 0 };
2477 
2478 	/*
2479 	 * Make sure we are back in dynamic mode.
2480 	 */
2481 	ofl->ofl_flags |= FLG_OF_DYNLIBS;
2482 
2483 	for (APLIST_TRAVERSE(ofl->ofl_soneed, idx1, sdf)) {
2484 		Aliste		idx2;
2485 		char		*path, *slash = NULL;
2486 		int		fd;
2487 		Ifl_desc	*ifl;
2488 		char		*file = (char *)sdf->sdf_name;
2489 
2490 		/*
2491 		 * See if this file has already been processed.  At the time
2492 		 * this implicit dependency was determined there may still have
2493 		 * been more explicit dependencies to process.  Note, if we ever
2494 		 * do parse the command line three times we would be able to
2495 		 * do all this checking when processing the dynamic section.
2496 		 */
2497 		if (sdf->sdf_file)
2498 			continue;
2499 
2500 		for (APLIST_TRAVERSE(ofl->ofl_sos, idx2, ifl)) {
2501 			if (!(ifl->ifl_flags & FLG_IF_NEEDSTR) &&
2502 			    (strcmp(file, ifl->ifl_soname) == 0)) {
2503 				sdf->sdf_file = ifl;
2504 				break;
2505 			}
2506 		}
2507 		if (sdf->sdf_file)
2508 			continue;
2509 
2510 		/*
2511 		 * If the current path name element embeds a "/", then it's to
2512 		 * be taken "as is", with no searching involved.  Process all
2513 		 * "/" occurrences, so that we can deduce the base file name.
2514 		 */
2515 		for (path = file; *path; path++) {
2516 			if (*path == '/')
2517 				slash = path;
2518 		}
2519 		if (slash) {
2520 			DBG_CALL(Dbg_libs_req(ofl->ofl_lml, sdf->sdf_name,
2521 			    sdf->sdf_rfile, file));
2522 			if ((fd = open(file, O_RDONLY)) == -1) {
2523 				eprintf(ofl->ofl_lml, ERR_WARNING,
2524 				    MSG_INTL(MSG_FIL_NOTFOUND), file,
2525 				    sdf->sdf_rfile);
2526 			} else {
2527 				Rej_desc	_rej = { 0 };
2528 
2529 				ifl = ld_process_open(file, ++slash, &fd, ofl,
2530 				    0, &_rej);
2531 				if (fd != -1)
2532 					(void) close(fd);
2533 				if (ifl == (Ifl_desc *)S_ERROR)
2534 					return (S_ERROR);
2535 
2536 				if (_rej.rej_type) {
2537 					Conv_reject_desc_buf_t rej_buf;
2538 
2539 					eprintf(ofl->ofl_lml, ERR_WARNING,
2540 					    MSG_INTL(reject[_rej.rej_type]),
2541 					    _rej.rej_name ? rej.rej_name :
2542 					    MSG_INTL(MSG_STR_UNKNOWN),
2543 					    conv_reject_desc(&_rej, &rej_buf,
2544 					    ld_targ.t_m.m_mach));
2545 				} else
2546 					sdf->sdf_file = ifl;
2547 			}
2548 			continue;
2549 		}
2550 
2551 		/*
2552 		 * Now search for this file in any user defined directories.
2553 		 */
2554 		for (APLIST_TRAVERSE(ofl->ofl_ulibdirs, idx2, path)) {
2555 			Rej_desc	_rej = { 0 };
2556 
2557 			ifl = process_req_lib(sdf, path, file, ofl, &_rej);
2558 			if (ifl == (Ifl_desc *)S_ERROR) {
2559 				return (S_ERROR);
2560 			}
2561 			if (_rej.rej_type) {
2562 				if (rej.rej_type == 0) {
2563 					rej = _rej;
2564 					rej.rej_name = strdup(_rej.rej_name);
2565 				}
2566 			}
2567 			if (ifl) {
2568 				sdf->sdf_file = ifl;
2569 				break;
2570 			}
2571 		}
2572 		if (sdf->sdf_file)
2573 			continue;
2574 
2575 		/*
2576 		 * Next use the local rules defined within the parent shared
2577 		 * object.
2578 		 */
2579 		if (sdf->sdf_rpath != NULL) {
2580 			char	*rpath, *next;
2581 
2582 			rpath = libld_malloc(strlen(sdf->sdf_rpath) + 1);
2583 			if (rpath == 0)
2584 				return (S_ERROR);
2585 			(void) strcpy(rpath, sdf->sdf_rpath);
2586 			DBG_CALL(Dbg_libs_path(ofl->ofl_lml, rpath,
2587 			    LA_SER_RUNPATH, sdf->sdf_rfile));
2588 			if ((path = strtok_r(rpath,
2589 			    MSG_ORIG(MSG_STR_COLON), &next)) != NULL) {
2590 				do {
2591 					Rej_desc	_rej = { 0 };
2592 
2593 					path = expand(sdf->sdf_rfile, path,
2594 					    &next);
2595 
2596 					ifl = process_req_lib(sdf, path,
2597 					    file, ofl, &_rej);
2598 					if (ifl == (Ifl_desc *)S_ERROR) {
2599 						return (S_ERROR);
2600 					}
2601 					if ((_rej.rej_type) &&
2602 					    (rej.rej_type == 0)) {
2603 						rej = _rej;
2604 						rej.rej_name =
2605 						    strdup(_rej.rej_name);
2606 					}
2607 					if (ifl) {
2608 						sdf->sdf_file = ifl;
2609 						break;
2610 					}
2611 				} while ((path = strtok_r(NULL,
2612 				    MSG_ORIG(MSG_STR_COLON), &next)) != NULL);
2613 			}
2614 		}
2615 		if (sdf->sdf_file)
2616 			continue;
2617 
2618 		/*
2619 		 * Finally try the default library search directories.
2620 		 */
2621 		for (APLIST_TRAVERSE(ofl->ofl_dlibdirs, idx2, path)) {
2622 			Rej_desc	_rej = { 0 };
2623 
2624 			ifl = process_req_lib(sdf, path, file, ofl, &rej);
2625 			if (ifl == (Ifl_desc *)S_ERROR) {
2626 				return (S_ERROR);
2627 			}
2628 			if (_rej.rej_type) {
2629 				if (rej.rej_type == 0) {
2630 					rej = _rej;
2631 					rej.rej_name = strdup(_rej.rej_name);
2632 				}
2633 			}
2634 			if (ifl) {
2635 				sdf->sdf_file = ifl;
2636 				break;
2637 			}
2638 		}
2639 		if (sdf->sdf_file)
2640 			continue;
2641 
2642 		/*
2643 		 * If we've got this far we haven't found the shared object.
2644 		 * If an object was found, but was rejected for some reason,
2645 		 * print a diagnostic to that effect, otherwise generate a
2646 		 * generic "not found" diagnostic.
2647 		 */
2648 		if (rej.rej_type) {
2649 			Conv_reject_desc_buf_t rej_buf;
2650 
2651 			eprintf(ofl->ofl_lml, ERR_WARNING,
2652 			    MSG_INTL(reject[rej.rej_type]),
2653 			    rej.rej_name ? rej.rej_name :
2654 			    MSG_INTL(MSG_STR_UNKNOWN),
2655 			    conv_reject_desc(&rej, &rej_buf,
2656 			    ld_targ.t_m.m_mach));
2657 		} else {
2658 			eprintf(ofl->ofl_lml, ERR_WARNING,
2659 			    MSG_INTL(MSG_FIL_NOTFOUND), file, sdf->sdf_rfile);
2660 		}
2661 	}
2662 
2663 	/*
2664 	 * Finally, now that all objects have been input, make sure any version
2665 	 * requirements have been met.
2666 	 */
2667 	return (ld_vers_verify(ofl));
2668 }
2669