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