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