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