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