xref: /titanic_51/usr/src/cmd/sgs/libld/common/files.c (revision 16ab6e0b56ccd36f9a870ff0b87e64c55599a6f0)
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 	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 *	_ifl;
708 
709 		switch (dyn->d_tag) {
710 		case DT_NEEDED:
711 		case DT_USED:
712 			if ((_ifl = libld_calloc(1, sizeof (Ifl_desc))) == 0)
713 				return (S_ERROR);
714 			_ifl->ifl_name = MSG_ORIG(MSG_STR_DYNAMIC);
715 			_ifl->ifl_soname = str + (size_t)dyn->d_un.d_val;
716 			_ifl->ifl_flags = FLG_IF_NEEDSTR;
717 			if (list_appendc(&ofl->ofl_sos, _ifl) == 0)
718 				return (S_ERROR);
719 			break;
720 		case DT_RPATH:
721 		case DT_RUNPATH:
722 			if ((ofl->ofl_rpath = add_string(ofl->ofl_rpath,
723 			    (str + (size_t)dyn->d_un.d_val))) ==
724 			    (const char *)S_ERROR)
725 				return (S_ERROR);
726 			break;
727 		}
728 	}
729 	return (1);
730 }
731 
732 /*
733  * Expand implicit references.  Dependencies can be specified in terms of the
734  * $ORIGIN, $PLATFORM, $OSREL and $OSNAME tokens, either from their needed name,
735  * or via a runpath.  In addition runpaths may also specify the $ISALIST token.
736  *
737  * Probably the most common reference to explict dependencies (via -L) will be
738  * sufficient to find any associated implicit dependencies, but just in case we
739  * expand any occurrence of these known tokens here.
740  *
741  * Note, if any errors occur we simply return the original name.
742  *
743  * This code is remarkably similar to expand() in rtld/common/paths.c.
744  */
745 static char		*platform = 0;
746 static size_t		platform_sz = 0;
747 static Isa_desc		*isa = 0;
748 static Uts_desc		*uts = 0;
749 
750 static char *
751 expand(const char *parent, const char *name, char **next)
752 {
753 	char		_name[PATH_MAX], *nptr, *_next;
754 	const char	*optr;
755 	size_t		nrem = PATH_MAX - 1;
756 	int		expanded = 0, _expanded, isaflag = 0;
757 
758 	optr = name;
759 	nptr = _name;
760 
761 	while (*optr) {
762 		if (nrem == 0)
763 			return ((char *)name);
764 
765 		if (*optr != '$') {
766 			*nptr++ = *optr++, nrem--;
767 			continue;
768 		}
769 
770 		_expanded = 0;
771 
772 		if (strncmp(optr, MSG_ORIG(MSG_STR_ORIGIN),
773 		    MSG_STR_ORIGIN_SIZE) == 0) {
774 			char *eptr;
775 
776 			/*
777 			 * For $ORIGIN, expansion is really just a concatenation
778 			 * of the parents directory name.  For example, an
779 			 * explict dependency foo/bar/lib1.so with a dependency
780 			 * on $ORIGIN/lib2.so would be expanded to
781 			 * foo/bar/lib2.so.
782 			 */
783 			if ((eptr = strrchr(parent, '/')) == 0) {
784 				*nptr++ = '.';
785 				nrem--;
786 			} else {
787 				size_t	len = eptr - parent;
788 
789 				if (len >= nrem)
790 					return ((char *)name);
791 
792 				(void) strncpy(nptr, parent, len);
793 				nptr = nptr + len;
794 				nrem -= len;
795 			}
796 			optr += MSG_STR_ORIGIN_SIZE;
797 			expanded = _expanded = 1;
798 
799 		} else if (strncmp(optr, MSG_ORIG(MSG_STR_PLATFORM),
800 		    MSG_STR_PLATFORM_SIZE) == 0) {
801 			/*
802 			 * Establish the platform from sysconf - like uname -i.
803 			 */
804 			if ((platform == 0) && (platform_sz == 0)) {
805 				char	info[SYS_NMLN];
806 				long	size;
807 
808 				size = sysinfo(SI_PLATFORM, info, SYS_NMLN);
809 				if ((size != -1) &&
810 				    (platform = libld_malloc((size_t)size))) {
811 					(void) strcpy(platform, info);
812 					platform_sz = (size_t)size - 1;
813 				} else
814 					platform_sz = 1;
815 			}
816 			if (platform != 0) {
817 				if (platform_sz >= nrem)
818 					return ((char *)name);
819 
820 				(void) strncpy(nptr, platform, platform_sz);
821 				nptr = nptr + platform_sz;
822 				nrem -= platform_sz;
823 
824 				optr += MSG_STR_PLATFORM_SIZE;
825 				expanded = _expanded = 1;
826 			}
827 
828 		} else if (strncmp(optr, MSG_ORIG(MSG_STR_OSNAME),
829 		    MSG_STR_OSNAME_SIZE) == 0) {
830 			/*
831 			 * Establish the os name - like uname -s.
832 			 */
833 			if (uts == 0)
834 				uts = conv_uts();
835 
836 			if (uts && uts->uts_osnamesz) {
837 				if (uts->uts_osnamesz >= nrem)
838 					return ((char *)name);
839 
840 				(void) strncpy(nptr, uts->uts_osname,
841 				    uts->uts_osnamesz);
842 				nptr = nptr + uts->uts_osnamesz;
843 				nrem -= uts->uts_osnamesz;
844 
845 				optr += MSG_STR_OSNAME_SIZE;
846 				expanded = _expanded = 1;
847 			}
848 
849 		} else if (strncmp(optr, MSG_ORIG(MSG_STR_OSREL),
850 		    MSG_STR_OSREL_SIZE) == 0) {
851 			/*
852 			 * Establish the os release - like uname -r.
853 			 */
854 			if (uts == 0)
855 				uts = conv_uts();
856 
857 			if (uts && uts->uts_osrelsz) {
858 				if (uts->uts_osrelsz >= nrem)
859 					return ((char *)name);
860 
861 				(void) strncpy(nptr, uts->uts_osrel,
862 				    uts->uts_osrelsz);
863 				nptr = nptr + uts->uts_osrelsz;
864 				nrem -= uts->uts_osrelsz;
865 
866 				optr += MSG_STR_OSREL_SIZE;
867 				expanded = _expanded = 1;
868 			}
869 
870 		} else if ((strncmp(optr, MSG_ORIG(MSG_STR_ISALIST),
871 		    MSG_STR_ISALIST_SIZE) == 0) && next && (isaflag++ == 0)) {
872 			/*
873 			 * Establish instruction sets from sysconf.  Note that
874 			 * this is only meaningful from runpaths.
875 			 */
876 			if (isa == 0)
877 				isa = conv_isalist();
878 
879 			if (isa && isa->isa_listsz &&
880 			    (nrem > isa->isa_opt->isa_namesz)) {
881 				size_t		mlen, tlen, hlen = optr - name;
882 				size_t		no;
883 				char		*lptr;
884 				Isa_opt		*opt = isa->isa_opt;
885 
886 				(void) strncpy(nptr, opt->isa_name,
887 				    opt->isa_namesz);
888 				nptr = nptr + opt->isa_namesz;
889 				nrem -= opt->isa_namesz;
890 
891 				optr += MSG_STR_ISALIST_SIZE;
892 				expanded = _expanded = 1;
893 
894 				tlen = strlen(optr);
895 
896 				/*
897 				 * As ISALIST expands to a number of elements,
898 				 * establish a new list to return to the caller.
899 				 * This will contain the present path being
900 				 * processed redefined for each isalist option,
901 				 * plus the original remaining list entries.
902 				 */
903 				mlen = ((hlen + tlen) * (isa->isa_optno - 1)) +
904 				    isa->isa_listsz - opt->isa_namesz;
905 				if (*next)
906 				    mlen += strlen(*next);
907 				if ((_next = lptr = libld_malloc(mlen)) == 0)
908 					return (0);
909 
910 				for (no = 1, opt++; no < isa->isa_optno;
911 				    no++, opt++) {
912 					(void) strncpy(lptr, name, hlen);
913 					lptr = lptr + hlen;
914 					(void) strncpy(lptr, opt->isa_name,
915 					    opt->isa_namesz);
916 					lptr = lptr + opt->isa_namesz;
917 					(void) strncpy(lptr, optr, tlen);
918 					lptr = lptr + tlen;
919 					*lptr++ = ':';
920 				}
921 				if (*next)
922 					(void) strcpy(lptr, *next);
923 				else
924 					*--lptr = '\0';
925 			}
926 		}
927 
928 		/*
929 		 * If no expansion occurred skip the $ and continue.
930 		 */
931 		if (_expanded == 0)
932 			*nptr++ = *optr++, nrem--;
933 	}
934 
935 	/*
936 	 * If any ISALIST processing has occurred not only do we return the
937 	 * expanded node we're presently working on, but we must also update the
938 	 * remaining list so that it is effectively prepended with this node
939 	 * expanded to all remaining isalist options.  Note that we can only
940 	 * handle one ISALIST per node.  For more than one ISALIST to be
941 	 * processed we'd need a better algorithm than above to replace the
942 	 * newly generated list.  Whether we want to encourage the number of
943 	 * pathname permutations this would provide is another question. So, for
944 	 * now if more than one ISALIST is encountered we return the original
945 	 * node untouched.
946 	 */
947 	if (isaflag) {
948 		if (isaflag == 1)
949 			*next = _next;
950 		else
951 			return ((char *)name);
952 	}
953 
954 	*nptr = '\0';
955 
956 	if (expanded) {
957 		if ((nptr = libld_malloc(strlen(_name) + 1)) == 0)
958 			return ((char *)name);
959 		(void) strcpy(nptr, _name);
960 		return (nptr);
961 	}
962 	return ((char *)name);
963 }
964 
965 /*
966  * Process a dynamic section.  If we are processing an explicit shared object
967  * then we need to determine if it has a recorded SONAME, if so, this name will
968  * be recorded in the output file being generated as the NEEDED entry rather
969  * than the shared objects filename itself.
970  * If the mode of the link-edit indicates that no undefined symbols should
971  * remain, then we also need to build up a list of any additional shared object
972  * dependencies this object may have.  In this case save any NEEDED entries
973  * together with any associated run-path specifications.  This information is
974  * recorded on the `ofl_soneed' list and will be analyzed after all explicit
975  * file processing has been completed (refer finish_libs()).
976  */
977 uintptr_t
978 process_dynamic(Is_desc *isc, Ifl_desc *ifl, Ofl_desc *ofl)
979 {
980 	Dyn		*data, *dyn;
981 	char		*str, *rpath = NULL;
982 	const char	*soname, *needed;
983 	Sdf_desc	*sdf;
984 	Listnode	*lnp;
985 
986 	data = (Dyn *)isc->is_indata->d_buf;
987 	str = (char *)ifl->ifl_isdesc[isc->is_shdr->sh_link]->is_indata->d_buf;
988 
989 	/*
990 	 * First loop through the dynamic section looking for a run path.
991 	 */
992 	if (ofl->ofl_flags & (FLG_OF_NOUNDEF | FLG_OF_SYMBOLIC)) {
993 		for (dyn = data; dyn->d_tag != DT_NULL; dyn++) {
994 			if ((dyn->d_tag != DT_RPATH) &&
995 			    (dyn->d_tag != DT_RUNPATH))
996 				continue;
997 			if ((rpath = str + (size_t)dyn->d_un.d_val) == NULL)
998 				continue;
999 			break;
1000 		}
1001 	}
1002 
1003 	/*
1004 	 * Now look for any needed dependencies (which may use the rpath)
1005 	 * or a new SONAME.
1006 	 */
1007 	for (dyn = data; dyn->d_tag != DT_NULL; dyn++) {
1008 		if (dyn->d_tag == DT_SONAME) {
1009 			if ((soname = str + (size_t)dyn->d_un.d_val) == NULL)
1010 				continue;
1011 
1012 			/*
1013 			 * Update the input file structure with this new name.
1014 			 */
1015 			ifl->ifl_soname = soname;
1016 
1017 		} else if ((dyn->d_tag == DT_NEEDED) ||
1018 		    (dyn->d_tag == DT_USED)) {
1019 			if (!(ofl->ofl_flags &
1020 			    (FLG_OF_NOUNDEF | FLG_OF_SYMBOLIC)))
1021 				continue;
1022 			if ((needed = str + (size_t)dyn->d_un.d_val) == NULL)
1023 				continue;
1024 
1025 			/*
1026 			 * Determine if this needed entry is already recorded on
1027 			 * the shared object needed list, if not create a new
1028 			 * definition for later processing (see finish_libs()).
1029 			 */
1030 			needed = expand(ifl->ifl_name, needed, (char **)0);
1031 
1032 			if ((sdf = sdf_find(needed, &ofl->ofl_soneed)) == 0) {
1033 				if ((sdf = sdf_add(needed,
1034 				    &ofl->ofl_soneed)) == (Sdf_desc *)S_ERROR)
1035 					return (S_ERROR);
1036 				sdf->sdf_rfile = ifl->ifl_name;
1037 			}
1038 
1039 			/*
1040 			 * Record the runpath (Note that we take the first
1041 			 * runpath which is exactly what ld.so.1 would do during
1042 			 * its dependency processing).
1043 			 */
1044 			if (rpath && (sdf->sdf_rpath == 0))
1045 				sdf->sdf_rpath = rpath;
1046 
1047 		} else if (dyn->d_tag == DT_FLAGS_1) {
1048 			if (dyn->d_un.d_val & (DF_1_INITFIRST | DF_1_INTERPOSE))
1049 				ifl->ifl_flags &= ~FLG_IF_LAZYLD;
1050 			if (dyn->d_un.d_val & DF_1_DISPRELPND)
1051 				ifl->ifl_flags |= FLG_IF_DISPPEND;
1052 			if (dyn->d_un.d_val & DF_1_DISPRELDNE)
1053 				ifl->ifl_flags |= FLG_IF_DISPDONE;
1054 			if (dyn->d_un.d_val & DF_1_NODIRECT)
1055 				ifl->ifl_flags |= FLG_IF_NODIRECT;
1056 
1057 		} else if ((dyn->d_tag == DT_AUDIT) &&
1058 		    (ifl->ifl_flags & FLG_IF_NEEDED)) {
1059 			/*
1060 			 * Record audit string as DT_DEPAUDIT.
1061 			 */
1062 			if ((ofl->ofl_depaudit = add_string(ofl->ofl_depaudit,
1063 			    (str + (size_t)dyn->d_un.d_val))) ==
1064 			    (const char *)S_ERROR)
1065 				return (S_ERROR);
1066 
1067 		} else if (dyn->d_tag == DT_SUNW_RTLDINF) {
1068 			/*
1069 			 * If a library has the SUNW_RTLDINF .dynamic entry
1070 			 * then we must not permit lazyloading of this library.
1071 			 * This is because critical startup information (TLS
1072 			 * routines) are provided as part of these interfaces
1073 			 * and we must have them as part of process startup.
1074 			 */
1075 			ifl->ifl_flags &= ~FLG_IF_LAZYLD;
1076 		}
1077 	}
1078 
1079 	/*
1080 	 * Perform some SONAME sanity checks.
1081 	 */
1082 	if (ifl->ifl_flags & FLG_IF_NEEDED) {
1083 		Ifl_desc *	sifl;
1084 
1085 		/*
1086 		 * Determine if anyone else will cause the same SONAME to be
1087 		 * used (this is either caused by two different files having the
1088 		 * same SONAME, or by one file SONAME actually matching another
1089 		 * file basename (if no SONAME is specified within a shared
1090 		 * library its basename will be used)). Probably rare, but some
1091 		 * idiot will do it.
1092 		 */
1093 		for (LIST_TRAVERSE(&ofl->ofl_sos, lnp, sifl)) {
1094 			if ((strcmp(ifl->ifl_soname, sifl->ifl_soname) == 0) &&
1095 			    (ifl != sifl)) {
1096 				const char	*hint, *iflb, *siflb;
1097 
1098 				/*
1099 				 * Determine the basename of each file. Perhaps
1100 				 * there are multiple copies of the same file
1101 				 * being brought in using different -L search
1102 				 * paths, and if so give an extra hint in the
1103 				 * error message.
1104 				 */
1105 				iflb = strrchr(ifl->ifl_name, '/');
1106 				if (iflb == NULL)
1107 					iflb = ifl->ifl_name;
1108 				else
1109 					iflb++;
1110 
1111 				siflb = strrchr(sifl->ifl_name, '/');
1112 				if (siflb == NULL)
1113 					siflb = sifl->ifl_name;
1114 				else
1115 					siflb++;
1116 
1117 				if (strcmp(iflb, siflb) == 0)
1118 					hint = MSG_INTL(MSG_REC_CNFLTHINT);
1119 				else
1120 					hint = MSG_ORIG(MSG_STR_EMPTY);
1121 
1122 				eprintf(ERR_FATAL, MSG_INTL(MSG_REC_OBJCNFLT),
1123 				    sifl->ifl_name, ifl->ifl_name,
1124 				    sifl->ifl_soname, hint);
1125 				ofl->ofl_flags |= FLG_OF_FATAL;
1126 				return (0);
1127 			}
1128 		}
1129 
1130 		/*
1131 		 * If the SONAME is the same as the name the user wishes to
1132 		 * record when building a dynamic library (refer -h option),
1133 		 * we also have a name clash.
1134 		 */
1135 		if (ofl->ofl_soname &&
1136 		    (strcmp(ofl->ofl_soname, ifl->ifl_soname) == 0)) {
1137 			eprintf(ERR_FATAL, MSG_INTL(MSG_REC_OPTCNFLT),
1138 			    ifl->ifl_name, ifl->ifl_soname);
1139 			ofl->ofl_flags |= FLG_OF_FATAL;
1140 			return (0);
1141 		}
1142 	}
1143 	return (1);
1144 }
1145 
1146 
1147 /*
1148  * Process a relocation entry. At this point all input sections from this
1149  * input file have been assigned an input section descriptor which is saved
1150  * in the `ifl_isdesc' array.
1151  */
1152 uintptr_t
1153 rel_process(Is_desc *isc, Ifl_desc *ifl, Ofl_desc *ofl)
1154 {
1155 	Word 	rndx;
1156 	Is_desc	*risc;
1157 	Os_desc	*osp;
1158 	Shdr	*shdr = isc->is_shdr;
1159 
1160 	/*
1161 	 * Make sure this is a valid relocation we can handle.
1162 	 */
1163 	if (shdr->sh_type != M_REL_SHT_TYPE) {
1164 		eprintf(ERR_FATAL, MSG_INTL(MSG_FIL_INVALSEC), ifl->ifl_name,
1165 		    isc->is_name, conv_sectyp_str(ofl->ofl_e_machine,
1166 		    (unsigned)shdr->sh_type));
1167 		ofl->ofl_flags |= FLG_OF_FATAL;
1168 		return (0);
1169 	}
1170 
1171 	/*
1172 	 * From the relocation section header information determine which
1173 	 * section needs the actual relocation.  Determine which output section
1174 	 * this input section has been assigned to and add to its relocation
1175 	 * list.  Note that the relocation section may be null if it is not
1176 	 * required (ie. .debug, .stabs, etc).
1177 	 */
1178 	rndx = shdr->sh_info;
1179 	if (rndx >= ifl->ifl_shnum) {
1180 		/*
1181 		 * Broken input file.
1182 		 */
1183 		eprintf(ERR_FATAL, MSG_INTL(MSG_FIL_INVSHINFO),
1184 			ifl->ifl_name, isc->is_name, EC_XWORD(rndx));
1185 		ofl->ofl_flags |= FLG_OF_FATAL;
1186 		return (0);
1187 	}
1188 	if (rndx == 0) {
1189 		if (list_appendc(&ofl->ofl_extrarels, isc) == 0)
1190 			return (S_ERROR);
1191 	} else if ((risc = ifl->ifl_isdesc[rndx]) != 0) {
1192 		/*
1193 		 * Discard relocations if they are against a section
1194 		 * which has been discarded.
1195 		 */
1196 		if (risc->is_flags & FLG_IS_DISCARD)
1197 			return (1);
1198 		if ((osp = risc->is_osdesc) == 0) {
1199 			if (risc->is_shdr->sh_type == SHT_SUNW_move) {
1200 				/*
1201 				 * This section is processed later
1202 				 * in sunwmove_preprocess() and
1203 				 * reloc_init().
1204 				 */
1205 				if (list_appendc(&ofl->ofl_mvrelisdescs,
1206 				    isc) == 0)
1207 					return (S_ERROR);
1208 				return (1);
1209 			}
1210 			eprintf(ERR_FATAL, MSG_INTL(MSG_FIL_INVRELOC1),
1211 				ifl->ifl_name, isc->is_name, risc->is_name);
1212 			return (0);
1213 		}
1214 		if (list_appendc(&osp->os_relisdescs, isc) == 0)
1215 			return (S_ERROR);
1216 	}
1217 	return (1);
1218 }
1219 
1220 /*
1221  * SHF_EXCLUDE flags is set for this section.
1222  */
1223 static uintptr_t
1224 process_exclude(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
1225     Word ndx, Ofl_desc *ofl)
1226 {
1227 	/*
1228 	 * Sections SHT_SYMTAB and SHT_DYNDYM, even if SHF_EXCLUDE is on, might
1229 	 * be needed for ld processing.  These sections need to be in the
1230 	 * internal table.  Later it will be determined whether they can be
1231 	 * eliminated or not.
1232 	 */
1233 	if (shdr->sh_type == SHT_SYMTAB || shdr->sh_type == SHT_DYNSYM)
1234 		return (0);
1235 
1236 	/*
1237 	 * Other checks
1238 	 */
1239 	if (shdr->sh_flags & SHF_ALLOC) {
1240 		/*
1241 		 * A conflict, issue an warning message, and ignore the section.
1242 		 */
1243 		eprintf(ERR_WARNING, MSG_INTL(MSG_FIL_EXCLUDE),
1244 		    ifl->ifl_name, name);
1245 		return (0);
1246 	}
1247 
1248 	/*
1249 	 * This sections is not going to the output file.
1250 	 */
1251 	return (process_section(name, ifl, shdr, scn, ndx, 0, ofl));
1252 }
1253 
1254 /*
1255  * Section processing state table.  `Initial' describes the required initial
1256  * procedure to be called (if any), `Final' describes the final processing
1257  * procedure (ie. things that can only be done when all required sections
1258  * have been collected).
1259  */
1260 static uintptr_t (*Initial[SHT_NUM][2])() = {
1261 
1262 /*			ET_REL			ET_DYN			*/
1263 
1264 /* SHT_NULL	*/	invalid_section,	invalid_section,
1265 /* SHT_PROGBITS	*/	process_progbits,	process_progbits,
1266 /* SHT_SYMTAB	*/	process_input,		process_input,
1267 /* SHT_STRTAB	*/	process_strtab,		process_strtab,
1268 /* SHT_RELA	*/	process_reloc,		process_reloc,
1269 /* SHT_HASH	*/	invalid_section,	NULL,
1270 /* SHT_DYNAMIC	*/	process_rel_dynamic,	process_section,
1271 /* SHT_NOTE	*/	process_section,	NULL,
1272 /* SHT_NOBITS	*/	process_nobits,		process_nobits,
1273 /* SHT_REL	*/	process_reloc,		process_reloc,
1274 /* SHT_SHLIB	*/	process_section,	invalid_section,
1275 /* SHT_DYNSYM	*/	invalid_section,	process_input,
1276 /* SHT_UNKNOWN12 */	process_progbits,	process_progbits,
1277 /* SHT_UNKNOWN13 */	process_progbits,	process_progbits,
1278 /* SHT_INIT_ARRAY */	process_array,		NULL,
1279 /* SHT_FINI_ARRAY */	process_array,		NULL,
1280 /* SHT_PREINIT_ARRAY */	process_array,		NULL,
1281 /* SHT_GROUP */		process_section,	invalid_section,
1282 /* SHT_SYMTAB_SHNDX */	process_sym_shndx,	NULL
1283 };
1284 
1285 static uintptr_t (*Final[SHT_NUM][2])() = {
1286 
1287 /* SHT_NULL	*/	NULL,			NULL,
1288 /* SHT_PROGBITS	*/	NULL,			NULL,
1289 /* SHT_SYMTAB	*/	sym_process,		sym_process,
1290 /* SHT_STRTAB	*/	NULL,			NULL,
1291 /* SHT_RELA	*/	rel_process,		NULL,
1292 /* SHT_HASH	*/	NULL,			NULL,
1293 /* SHT_DYNAMIC	*/	NULL,			process_dynamic,
1294 /* SHT_NOTE	*/	NULL,			NULL,
1295 /* SHT_NOBITS	*/	NULL,			NULL,
1296 /* SHT_REL	*/	rel_process,		NULL,
1297 /* SHT_SHLIB	*/	NULL,			NULL,
1298 /* SHT_DYNSYM	*/	NULL,			sym_process,
1299 /* SHT_UNKNOWN12 */	NULL,			NULL,
1300 /* SHT_UNKNOWN13 */	NULL,			NULL,
1301 /* SHT_INIT_ARRAY */	NULL,			NULL,
1302 /* SHT_FINI_ARRAY */	NULL,			NULL,
1303 /* SHT_PREINIT_ARRAY */	NULL,			NULL,
1304 /* SHT_GROUP */		NULL,			NULL,
1305 /* SHT_SYMTAB_SHNDX */	sym_shndx_process,	NULL
1306 };
1307 
1308 #define	MAXNDXSIZE	10
1309 
1310 /*
1311  * Process an elf file.  Each section is compared against the section state
1312  * table to determine whether it should be processed (saved), ignored, or
1313  * is invalid for the type of input file being processed.
1314  */
1315 uintptr_t
1316 process_elf(Ifl_desc *ifl, Elf *elf, Ofl_desc *ofl)
1317 {
1318 	Elf_Scn		*scn;
1319 	Shdr		*shdr;
1320 	Word		ndx, sndx;
1321 	char		*str, *name, _name[MAXNDXSIZE];
1322 	Word		row, column;
1323 	int		ident;
1324 	uintptr_t	error;
1325 	Is_desc		*vdfisp, *vndisp, *vsyisp, *sifisp, * capisp;
1326 	Sdf_desc	*sdf;
1327 	Word		ordered_shndx = 0; /* index to first ordered section */
1328 	Word		ordered_cnt = 0;
1329 
1330 	/*
1331 	 * First process the .shstrtab section so that later sections can
1332 	 * reference their name.
1333 	 */
1334 	lds_file(ifl->ifl_name, elf_kind(elf), ifl->ifl_flags, elf);
1335 
1336 	sndx = ifl->ifl_shstrndx;
1337 	if ((scn = elf_getscn(elf, (size_t)sndx)) == NULL) {
1338 		eprintf(ERR_ELF, MSG_INTL(MSG_ELF_GETSCN), ifl->ifl_name);
1339 		ofl->ofl_flags |= FLG_OF_FATAL;
1340 		return (0);
1341 	}
1342 	if ((shdr = elf_getshdr(scn)) == NULL) {
1343 		eprintf(ERR_ELF, MSG_INTL(MSG_ELF_GETSHDR), ifl->ifl_name);
1344 		ofl->ofl_flags |= FLG_OF_FATAL;
1345 		return (0);
1346 	}
1347 	if ((name = elf_strptr(elf, (size_t)sndx, (size_t)shdr->sh_name)) ==
1348 	    NULL) {
1349 		eprintf(ERR_ELF, MSG_INTL(MSG_ELF_STRPTR), ifl->ifl_name);
1350 		ofl->ofl_flags |= FLG_OF_FATAL;
1351 		return (0);
1352 	}
1353 
1354 	if (lds_input_section(name, &shdr, sndx, ifl->ifl_name,
1355 	    scn, elf, ofl) == S_ERROR)
1356 		return (S_ERROR);
1357 
1358 	/*
1359 	 * Reset the name since the shdr->sh_name could have been changed as
1360 	 * part of lds_input_section().  If there is no name, fabricate one
1361 	 * using the section index.
1362 	 */
1363 	if (shdr->sh_name == 0) {
1364 		(void) snprintf(_name, MAXNDXSIZE, MSG_ORIG(MSG_FMT_INDEX),
1365 		    EC_XWORD(sndx));
1366 		if ((name = libld_malloc(strlen(_name) + 1)) == 0)
1367 			return (S_ERROR);
1368 		(void) strcpy(name, _name);
1369 
1370 	} else if ((name = elf_strptr(elf, (size_t)sndx,
1371 	    (size_t)shdr->sh_name)) == NULL) {
1372 		eprintf(ERR_ELF, MSG_INTL(MSG_ELF_STRPTR), ifl->ifl_name);
1373 		ofl->ofl_flags |= FLG_OF_FATAL;
1374 		return (0);
1375 	}
1376 
1377 	error = process_strtab(name, ifl, shdr, scn, sndx, FALSE, ofl);
1378 	if ((error == 0) || (error == S_ERROR))
1379 		return (error);
1380 	str = ifl->ifl_isdesc[sndx]->is_indata->d_buf;
1381 
1382 	/*
1383 	 * Determine the state table column from the input file type.  Note,
1384 	 * shared library sections are not added to the output section list.
1385 	 */
1386 	if (ifl->ifl_ehdr->e_type == ET_DYN) {
1387 		column = 1;
1388 		ofl->ofl_soscnt++;
1389 		ident = M_ID_NULL;
1390 	} else {
1391 		column = 0;
1392 		ofl->ofl_objscnt++;
1393 		ident = M_ID_UNKNOWN;
1394 	}
1395 
1396 	DBG_CALL(Dbg_file_generic(ifl));
1397 	ndx = 0;
1398 	vdfisp = vndisp = vsyisp = sifisp = capisp = 0;
1399 	scn = NULL;
1400 	while (scn = elf_nextscn(elf, scn)) {
1401 		ndx++;
1402 		/*
1403 		 * As we've already processed the .shstrtab don't do it again.
1404 		 */
1405 		if (ndx == sndx)
1406 			continue;
1407 
1408 		if ((shdr = elf_getshdr(scn)) == NULL) {
1409 			eprintf(ERR_ELF, MSG_INTL(MSG_ELF_GETSHDR),
1410 			    ifl->ifl_name);
1411 			ofl->ofl_flags |= FLG_OF_FATAL;
1412 			return (0);
1413 		}
1414 		name = str + (size_t)(shdr->sh_name);
1415 
1416 		if (lds_input_section(name, &shdr, ndx, ifl->ifl_name, scn,
1417 		    elf, ofl) == S_ERROR)
1418 			return (S_ERROR);
1419 
1420 		/*
1421 		 * Reset the name since the shdr->sh_name could have been
1422 		 * changed as part of lds_input_section().  If there is no name,
1423 		 * fabricate one using the section index.
1424 		 */
1425 		if (shdr->sh_name == 0) {
1426 			(void) snprintf(_name, MAXNDXSIZE,
1427 			    MSG_ORIG(MSG_FMT_INDEX), EC_XWORD(ndx));
1428 			if ((name = libld_malloc(strlen(_name) + 1)) == 0)
1429 				return (S_ERROR);
1430 			(void) strcpy(name, _name);
1431 		} else
1432 			name = str + (size_t)(shdr->sh_name);
1433 
1434 		row = shdr->sh_type;
1435 
1436 		/*
1437 		 * If the section has the SHF_EXCLUDE flag on, and we're not
1438 		 * generating a relocatable object, exclude the section.
1439 		 */
1440 		if (((shdr->sh_flags & SHF_EXCLUDE) != 0) &&
1441 		    ((ofl->ofl_flags & FLG_OF_RELOBJ) == 0)) {
1442 			if ((error = process_exclude(name, ifl, shdr, scn,
1443 			    ndx, ofl)) == S_ERROR)
1444 				return (S_ERROR);
1445 			if (error == 1)
1446 				continue;
1447 		}
1448 
1449 		/*
1450 		 * If this is a standard section type process it via the
1451 		 * appropriate action routine.
1452 		 */
1453 		if (row < SHT_NUM) {
1454 			if (Initial[row][column] != NULL) {
1455 				if (Initial[row][column](name, ifl, shdr, scn,
1456 				    ndx, ident, ofl) == S_ERROR)
1457 					return (S_ERROR);
1458 			}
1459 		} else {
1460 			/*
1461 			 * If this section is below SHT_LOSUNW then we don't
1462 			 * really know what to do with it, issue a warning
1463 			 * message but do the basic section processing anyway.
1464 			 */
1465 			if (row < (Word)SHT_LOSUNW)
1466 				eprintf(ERR_WARNING, MSG_INTL(MSG_FIL_INVALSEC),
1467 				    ifl->ifl_name, name,
1468 				    conv_sectyp_str(ofl->ofl_e_machine,
1469 				    (unsigned)shdr->sh_type));
1470 
1471 			/*
1472 			 * Handle sections greater than SHT_LOSUNW.
1473 			 */
1474 			switch (row) {
1475 			case SHT_SUNW_dof:
1476 				if (process_section(name, ifl, shdr, scn,
1477 				    ndx, ident, ofl) == S_ERROR)
1478 					return (S_ERROR);
1479 				break;
1480 			case SHT_SUNW_cap:
1481 				if (process_section(name, ifl, shdr, scn,
1482 				    ndx, M_ID_NULL, ofl) == S_ERROR)
1483 					return (S_ERROR);
1484 				capisp = ifl->ifl_isdesc[ndx];
1485 				break;
1486 			case SHT_SUNW_DEBUGSTR:
1487 			case SHT_SUNW_DEBUG:
1488 				if (process_debug(name, ifl, shdr, scn,
1489 				    ndx, ident, ofl) == S_ERROR)
1490 					return (S_ERROR);
1491 				break;
1492 			case SHT_SUNW_move:
1493 				if (process_section(name, ifl, shdr, scn,
1494 				    ndx, M_ID_NULL, ofl) == S_ERROR)
1495 					return (S_ERROR);
1496 				break;
1497 			case SHT_SUNW_syminfo:
1498 				if (process_section(name, ifl, shdr, scn,
1499 				    ndx, M_ID_NULL, ofl) == S_ERROR)
1500 					return (S_ERROR);
1501 				sifisp = ifl->ifl_isdesc[ndx];
1502 				break;
1503 			case SHT_SUNW_ANNOTATE:
1504 			case SHT_SUNW_COMDAT:
1505 				if (process_progbits(name, ifl, shdr, scn,
1506 				    ndx, ident, ofl) == S_ERROR)
1507 					return (S_ERROR);
1508 				break;
1509 			case SHT_SUNW_verdef:
1510 				if (process_section(name, ifl, shdr, scn,
1511 				    ndx, M_ID_NULL, ofl) == S_ERROR)
1512 					return (S_ERROR);
1513 				vdfisp = ifl->ifl_isdesc[ndx];
1514 				break;
1515 			case SHT_SUNW_verneed:
1516 				if (process_section(name, ifl, shdr, scn,
1517 				    ndx, M_ID_NULL, ofl) == S_ERROR)
1518 					return (S_ERROR);
1519 				vndisp = ifl->ifl_isdesc[ndx];
1520 				break;
1521 			case SHT_SUNW_versym:
1522 				if (process_section(name, ifl, shdr, scn,
1523 				    ndx, M_ID_NULL, ofl) == S_ERROR)
1524 					return (S_ERROR);
1525 				vsyisp = ifl->ifl_isdesc[ndx];
1526 				break;
1527 #if	defined(sparc)
1528 			case SHT_SPARC_GOTDATA:
1529 				if (process_section(name, ifl, shdr, scn,
1530 				    ndx, M_ID_GOTDATA, ofl) == S_ERROR)
1531 					return (S_ERROR);
1532 				break;
1533 #endif
1534 #if	(defined(__i386) || defined(__amd64)) && defined(_ELF64)
1535 			case SHT_AMD64_UNWIND:
1536 				if (column == 0) {
1537 					/*
1538 					 * column == ET_REL
1539 					 */
1540 					if (process_amd64_unwind(name, ifl,
1541 					    shdr, scn, ndx, M_ID_NULL,
1542 					    ofl) == S_ERROR)
1543 						return (S_ERROR);
1544 				}
1545 				break;
1546 #endif
1547 			default:
1548 				if (ident != M_ID_NULL)
1549 					ident = M_ID_USER;
1550 				if (process_section(name, ifl, shdr, scn,
1551 				    ndx, ident, ofl) == S_ERROR)
1552 					return (S_ERROR);
1553 				break;
1554 			}
1555 		}
1556 
1557 		/*
1558 		 * If we have any sections that require ORDERED processing,
1559 		 * remember the index of the first ordered section.  This let's
1560 		 * us know if we need an ORDERED place_section pass, and if so,
1561 		 * where to start.
1562 		 */
1563 		if (ifl->ifl_isdesc[ndx] &&
1564 		    (ifl->ifl_isdesc[ndx]->is_shdr->sh_flags & ALL_SHF_ORDER)) {
1565 			ordered_cnt++;
1566 			if (ordered_shndx == 0)
1567 				ordered_shndx = ndx;
1568 		}
1569 	}
1570 
1571 	/*
1572 	 * Now that all of sections have been placed, scan through any sections
1573 	 * which have special ordering requirements and place them now.
1574 	 */
1575 	if (ordered_shndx) {
1576 		Word	cnt;
1577 
1578 		for (ndx = ordered_shndx, cnt = 0;
1579 		    (ndx < ifl->ifl_shnum) && (cnt < ordered_cnt); ndx++) {
1580 			Is_desc	*isp;
1581 			/* LINTED */
1582 			Os_desc	*osp;
1583 
1584 			if (((isp = ifl->ifl_isdesc[ndx]) == 0) ||
1585 			    ((isp->is_shdr->sh_flags & ALL_SHF_ORDER) == 0))
1586 				continue;
1587 
1588 			/*
1589 			 * If this is an ordered section, process it.
1590 			 */
1591 			cnt++;
1592 			if ((osp = (Os_desc *)process_ordered(ifl, ofl, ndx,
1593 			    ifl->ifl_shnum)) == (Os_desc *)S_ERROR)
1594 				return (S_ERROR);
1595 
1596 #if	(defined(__i386) || defined(__amd64)) && defined(_ELF64)
1597 			/*
1598 			 * If this section is 'ordered' then it was not
1599 			 * caught in the previous 'place_section' operation.
1600 			 *
1601 			 * So - now that we have a OSP section for
1602 			 * the unwind info - record it.
1603 			 */
1604 			if (osp &&
1605 			    (osp->os_shdr->sh_type == SHT_AMD64_UNWIND) &&
1606 			    (append_amd64_unwind(osp, ofl) == S_ERROR))
1607 				return (S_ERROR);
1608 #endif
1609 		}
1610 	}
1611 
1612 	/*
1613 	 * If this is an explict shared object determine if the user has
1614 	 * specified a control definition.  This descriptor may specify which
1615 	 * version definitions can be used from this object (it may also update
1616 	 * the dependency to USED and supply an alternative SONAME).
1617 	 */
1618 	sdf = 0;
1619 	if (column && (ifl->ifl_flags & FLG_IF_NEEDED)) {
1620 		const char	*base;
1621 
1622 		/*
1623 		 * Use the basename of the input file (typically this is the
1624 		 * compilation environment name, ie. libfoo.so).
1625 		 */
1626 		if ((base = strrchr(ifl->ifl_name, '/')) == NULL)
1627 			base = ifl->ifl_name;
1628 		else
1629 			base++;
1630 
1631 		if ((sdf = sdf_find(base, &ofl->ofl_socntl)) != 0) {
1632 			sdf->sdf_file = ifl;
1633 			ifl->ifl_sdfdesc = sdf;
1634 		}
1635 	}
1636 
1637 	/*
1638 	 * Process any hardware/software capabilities sections.  Only propagate
1639 	 * capabilities for input relocatable objects.  If the object doesn't
1640 	 * contain any capabilities, any capability state that has already been
1641 	 * gathered will prevail.
1642 	 */
1643 	if (capisp && (ifl->ifl_ehdr->e_type == ET_REL))
1644 		process_cap(name, ifl, capisp, ofl);
1645 
1646 	/*
1647 	 * Process any version dependencies.  These will establish shared object
1648 	 * `needed' entries in the same manner as will be generated from the
1649 	 * .dynamic's NEEDED entries.
1650 	 */
1651 	if (vndisp && (ofl->ofl_flags & (FLG_OF_NOUNDEF | FLG_OF_SYMBOLIC)))
1652 		if (vers_need_process(vndisp, ifl, ofl) == S_ERROR)
1653 			return (S_ERROR);
1654 
1655 	/*
1656 	 * Before processing any symbol resolution or relocations process any
1657 	 * version sections.
1658 	 */
1659 	if (vsyisp)
1660 		(void) vers_sym_process(vsyisp, ifl);
1661 
1662 	if (ifl->ifl_versym &&
1663 	    (vdfisp || (sdf && (sdf->sdf_flags & FLG_SDF_SELECT))))
1664 		if (vers_def_process(vdfisp, ifl, ofl) == S_ERROR)
1665 			return (S_ERROR);
1666 
1667 	/*
1668 	 * Having collected the appropriate sections carry out any additional
1669 	 * processing if necessary.
1670 	 */
1671 	for (ndx = 0; ndx < ifl->ifl_shnum; ndx++) {
1672 		Is_desc *	isp;
1673 
1674 		if ((isp = ifl->ifl_isdesc[ndx]) == 0)
1675 			continue;
1676 		row = isp->is_shdr->sh_type;
1677 
1678 		if ((isp->is_flags & FLG_IS_DISCARD) == 0)
1679 			lds_section(isp->is_name, isp->is_shdr, ndx,
1680 				isp->is_indata, elf);
1681 
1682 		/*
1683 		 * If this is a ST_SUNW_move section from a
1684 		 * a relocatable file, keep the input section.
1685 		 */
1686 		if ((row == SHT_SUNW_move) && (column == 0)) {
1687 			if (list_appendc(&(ofl->ofl_ismove), isp) == 0)
1688 				return (S_ERROR);
1689 		}
1690 
1691 		/*
1692 		 * If this is a standard section type process it via the
1693 		 * appropriate action routine.
1694 		 */
1695 		if (row < SHT_NUM) {
1696 			if (Final[row][column] != NULL)
1697 				if (Final[row][column](isp, ifl, ofl) ==
1698 				    S_ERROR)
1699 					return (S_ERROR);
1700 		}
1701 	}
1702 
1703 	/*
1704 	 * After processing any symbol resolution, and if this dependency
1705 	 * indicates it contains symbols that can't be directly bound to,
1706 	 * set the symbols appropriately.
1707 	 */
1708 	if (sifisp && ((ifl->ifl_flags & (FLG_IF_NEEDED | FLG_IF_NODIRECT)) ==
1709 	    (FLG_IF_NEEDED | FLG_IF_NODIRECT)))
1710 		(void) sym_nodirect(sifisp, ifl, ofl);
1711 
1712 	return (1);
1713 }
1714 
1715 /*
1716  * Process the current input file.  There are basically three types of files
1717  * that come through here:
1718  *
1719  *  o	files explicitly defined on the command line (ie. foo.o or bar.so),
1720  *	in this case only the `name' field is valid.
1721  *
1722  *  o	libraries determined from the -l command line option (ie. -lbar),
1723  *	in this case the `soname' field contains the basename of the located
1724  *	file.
1725  *
1726  * Any shared object specified via the above two conventions must be recorded
1727  * as a needed dependency.
1728  *
1729  *  o	libraries specified as dependencies of those libraries already obtained
1730  *	via the command line (ie. bar.so has a DT_NEEDED entry of fred.so.1),
1731  *	in this case the `soname' field contains either a full pathname (if the
1732  *	needed entry contained a `/'), or the basename of the located file.
1733  *	These libraries are processed to verify symbol binding but are not
1734  *	recorded as dependencies of the output file being generated.
1735  */
1736 Ifl_desc *
1737 process_ifl(const char *name, const char *soname, int fd, Elf *elf,
1738     Half flags, Ofl_desc * ofl, Rej_desc * rej)
1739 {
1740 	Ifl_desc	*ifl;
1741 	Ehdr		*ehdr;
1742 	Listnode	*lnp;
1743 	uintptr_t	error = 0;
1744 	struct stat	status;
1745 	Ar_desc		*adp;
1746 	Rej_desc	_rej;
1747 
1748 	/*
1749 	 * If this file was not extracted from an archive obtain its device
1750 	 * information.  This will be used to determine if the file has already
1751 	 * been processed (rather than simply comparing filenames, the device
1752 	 * information provides a quicker comparison and detects linked files).
1753 	 */
1754 	if (!(flags & FLG_IF_EXTRACT))
1755 		(void) fstat(fd, &status);
1756 	else {
1757 		status.st_dev = 0;
1758 		status.st_ino = 0;
1759 	}
1760 
1761 	switch (elf_kind(elf)) {
1762 	case ELF_K_AR:
1763 		/*
1764 		 * Determine if we've already come across this archive file.
1765 		 */
1766 		if (!(flags & FLG_IF_EXTRACT)) {
1767 			for (LIST_TRAVERSE(&ofl->ofl_ars, lnp, adp)) {
1768 				if ((adp->ad_stdev != status.st_dev) ||
1769 				    (adp->ad_stino != status.st_ino))
1770 					continue;
1771 
1772 				/*
1773 				 * We've seen this file before so reuse the
1774 				 * original archive descriptor and discard the
1775 				 * new elf descriptor.
1776 				 */
1777 				DBG_CALL(Dbg_file_reuse(name, adp->ad_name));
1778 				(void) elf_end(elf);
1779 				return ((Ifl_desc *)process_archive(name, fd,
1780 				    adp, ofl));
1781 			}
1782 		}
1783 
1784 		/*
1785 		 * As we haven't processed this file before establish a new
1786 		 * archive descriptor.
1787 		 */
1788 		adp = ar_setup(name, elf, ofl);
1789 		if ((adp == 0) || (adp == (Ar_desc *)S_ERROR))
1790 			return ((Ifl_desc *)adp);
1791 		adp->ad_stdev = status.st_dev;
1792 		adp->ad_stino = status.st_ino;
1793 
1794 		lds_file(name, ELF_K_AR, flags, elf);
1795 
1796 		return ((Ifl_desc *)process_archive(name, fd, adp, ofl));
1797 
1798 	case ELF_K_ELF:
1799 		/*
1800 		 * Obtain the elf header so that we can determine what type of
1801 		 * elf ELF_K_ELF file this is.
1802 		 */
1803 		if ((ehdr = elf_getehdr(elf)) == NULL) {
1804 			int	_class = gelf_getclass(elf);
1805 
1806 			/*
1807 			 * Failure could occur for a number of reasons at this
1808 			 * point.  Typically the files class is incorrect (ie.
1809 			 * user is building 64-bit but managed to pint at 32-bit
1810 			 * libraries).  However any number of elf errors can
1811 			 * also occur, such as from a truncated or corrupt file.
1812 			 * Here we try and get the best error message possible.
1813 			 */
1814 			if (M_CLASS != _class) {
1815 				_rej.rej_type = SGS_REJ_CLASS;
1816 				_rej.rej_info = (uint_t)_class;
1817 			} else {
1818 				_rej.rej_type = SGS_REJ_STR;
1819 				_rej.rej_str = elf_errmsg(-1);
1820 			}
1821 			_rej.rej_name = name;
1822 			DBG_CALL(Dbg_file_rejected(&_rej));
1823 			if (rej->rej_type == 0) {
1824 				*rej = _rej;
1825 				rej->rej_name = strdup(_rej.rej_name);
1826 			}
1827 			return (0);
1828 		}
1829 
1830 		/*
1831 		 * Determine if we've already come across this file.
1832 		 */
1833 		if (!(flags & FLG_IF_EXTRACT)) {
1834 			List *	lst;
1835 
1836 			if (ehdr->e_type == ET_REL)
1837 				lst = &ofl->ofl_objs;
1838 			else
1839 				lst = &ofl->ofl_sos;
1840 
1841 			/*
1842 			 * Traverse the appropriate file list and determine if
1843 			 * a dev/inode match is found.
1844 			 */
1845 			for (LIST_TRAVERSE(lst, lnp, ifl)) {
1846 				/*
1847 				 * Ifl_desc generated via -Nneed, therefore no
1848 				 * actual file behind it.
1849 				 */
1850 				if (ifl->ifl_flags & FLG_IF_NEEDSTR)
1851 					continue;
1852 
1853 				if ((ifl->ifl_stino != status.st_ino) ||
1854 				    (ifl->ifl_stdev != status.st_dev))
1855 					continue;
1856 
1857 				/*
1858 				 * Disregard (skip) this image.
1859 				 */
1860 				DBG_CALL(Dbg_file_skip(name, ifl->ifl_name));
1861 				(void) elf_end(elf);
1862 
1863 				/*
1864 				 * If the file was explicitly defined on the
1865 				 * command line (this is always the case for
1866 				 * relocatable objects, and is true for shared
1867 				 * objects when they weren't specified via -l or
1868 				 * were dragged in as an implicit dependency),
1869 				 * then warn the user.
1870 				 */
1871 				if ((flags & FLG_IF_CMDLINE) ||
1872 				    (ifl->ifl_flags & FLG_IF_CMDLINE)) {
1873 					const char	*errmsg;
1874 
1875 					/*
1876 					 * Determine whether this is the same
1877 					 * file name as originally encountered
1878 					 * so as to provide the most
1879 					 * descriptive diagnostic.
1880 					 */
1881 					if (strcmp(name, ifl->ifl_name) == 0)
1882 					    errmsg = MSG_INTL(MSG_FIL_MULINC_1);
1883 					else
1884 					    errmsg = MSG_INTL(MSG_FIL_MULINC_2);
1885 
1886 					eprintf(ERR_WARNING, errmsg, name,
1887 					    ifl->ifl_name);
1888 				}
1889 				return (ifl);
1890 			}
1891 		}
1892 
1893 		/*
1894 		 * At this point, we know we need the file.  Establish an input
1895 		 * file descriptor and continue processing.
1896 		 */
1897 		ifl = ifl_setup(name, ehdr, elf, flags, ofl, rej);
1898 		if ((ifl == 0) || (ifl == (Ifl_desc *)S_ERROR))
1899 			return (ifl);
1900 		ifl->ifl_stdev = status.st_dev;
1901 		ifl->ifl_stino = status.st_ino;
1902 
1903 		/*
1904 		 * If -zignore is in effect, mark this file as a potential
1905 		 * candidate (the files use isn't actually determined until
1906 		 * symbol resolution and relocation processing are completed).
1907 		 */
1908 		if (ofl->ofl_flags1 & FLG_OF1_IGNORE)
1909 			ifl->ifl_flags |= FLG_IF_IGNORE;
1910 
1911 		switch (ehdr->e_type) {
1912 		case ET_REL:
1913 			mach_eflags(ehdr, ofl);
1914 			error = process_elf(ifl, elf, ofl);
1915 			break;
1916 		case ET_DYN:
1917 			if ((ofl->ofl_flags & FLG_OF_STATIC) ||
1918 			    !(ofl->ofl_flags & FLG_OF_DYNLIBS)) {
1919 				eprintf(ERR_FATAL, MSG_INTL(MSG_FIL_SOINSTAT),
1920 				    name);
1921 				ofl->ofl_flags |= FLG_OF_FATAL;
1922 				return (0);
1923 			}
1924 
1925 			/*
1926 			 * Record any additional shared object information.
1927 			 * If no soname is specified (eg. this file was
1928 			 * derived from a explicit filename declaration on the
1929 			 * command line, ie. bar.so) use the pathname.
1930 			 * This entry may be overridden if the files dynamic
1931 			 * section specifies an DT_SONAME value.
1932 			 */
1933 			if (soname == NULL)
1934 				ifl->ifl_soname = ifl->ifl_name;
1935 			else
1936 				ifl->ifl_soname = soname;
1937 
1938 			/*
1939 			 * If direct bindings, lazy loading, or group
1940 			 * permissions need to be established, mark this object.
1941 			 */
1942 			if (ofl->ofl_flags1 & FLG_OF1_ZDIRECT)
1943 				ifl->ifl_flags |= FLG_IF_DIRECT;
1944 			if (ofl->ofl_flags1 & FLG_OF1_LAZYLD)
1945 				ifl->ifl_flags |= FLG_IF_LAZYLD;
1946 			if (ofl->ofl_flags1 & FLG_OF1_GRPPRM)
1947 				ifl->ifl_flags |= FLG_IF_GRPPRM;
1948 			error = process_elf(ifl, elf, ofl);
1949 
1950 			/*
1951 			 * At this point we know if this file will be
1952 			 * lazyloaded, or whether bindings to it must be direct.
1953 			 * In either case, a syminfo section is required.
1954 			 */
1955 			if (ifl->ifl_flags & (FLG_IF_LAZYLD | FLG_IF_DIRECT))
1956 				ofl->ofl_flags |= FLG_OF_SYMINFO;
1957 
1958 			break;
1959 		default:
1960 			(void) elf_errno();
1961 			_rej.rej_type = SGS_REJ_UNKFILE;
1962 			_rej.rej_name = name;
1963 			DBG_CALL(Dbg_file_rejected(&_rej));
1964 			if (rej->rej_type == 0) {
1965 				*rej = _rej;
1966 				rej->rej_name = strdup(_rej.rej_name);
1967 			}
1968 			return (0);
1969 		}
1970 		break;
1971 	default:
1972 		(void) elf_errno();
1973 		_rej.rej_type = SGS_REJ_UNKFILE;
1974 		_rej.rej_name = name;
1975 		DBG_CALL(Dbg_file_rejected(&_rej));
1976 		if (rej->rej_type == 0) {
1977 			*rej = _rej;
1978 			rej->rej_name = strdup(_rej.rej_name);
1979 		}
1980 		return (0);
1981 	}
1982 	if ((error == 0) || (error == S_ERROR))
1983 		return ((Ifl_desc *)error);
1984 	else
1985 		return (ifl);
1986 }
1987 
1988 /*
1989  * Having successfully opened a file, set up the necessary elf structures to
1990  * process it further.  This small section of processing is slightly different
1991  * from the elf initialization required to process a relocatable object from an
1992  * archive (see libs.c: process_archive()).
1993  */
1994 Ifl_desc *
1995 process_open(const char *path, size_t dlen, int fd, Ofl_desc *ofl, Half flags,
1996     Rej_desc * rej)
1997 {
1998 	Elf *	elf;
1999 
2000 	if ((elf = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
2001 		eprintf(ERR_ELF, MSG_INTL(MSG_ELF_BEGIN), path);
2002 		ofl->ofl_flags |= FLG_OF_FATAL;
2003 		return (0);
2004 	}
2005 
2006 	return (process_ifl(&path[0], &path[dlen], fd, elf, flags, ofl, rej));
2007 }
2008 
2009 /*
2010  * Process a required library (i.e. the dependency of a shared object).
2011  * Combine the directory and filename, check the resultant path size, and try
2012  * opening the pathname.
2013  */
2014 Ifl_desc *
2015 process_req_lib(Sdf_desc *sdf, const char *dir, const char *file,
2016     Ofl_desc * ofl, Rej_desc * rej)
2017 {
2018 	size_t		dlen, plen;
2019 	int		fd;
2020 	char		path[PATH_MAX];
2021 	const char	*_dir = dir;
2022 
2023 	/*
2024 	 * Determine the sizes of the directory and filename to insure we don't
2025 	 * exceed our buffer.
2026 	 */
2027 	if ((dlen = strlen(dir)) == 0) {
2028 		_dir = MSG_ORIG(MSG_STR_DOT);
2029 		dlen = 1;
2030 	}
2031 	dlen++;
2032 	plen = dlen + strlen(file) + 1;
2033 	if (plen > PATH_MAX) {
2034 		eprintf(ERR_FATAL, MSG_INTL(MSG_FIL_PTHTOLONG), _dir, file);
2035 		ofl->ofl_flags |= FLG_OF_FATAL;
2036 		return (0);
2037 	}
2038 
2039 	/*
2040 	 * Build the entire pathname and try and open the file.
2041 	 */
2042 	(void) strcpy(path, _dir);
2043 	(void) strcat(path, MSG_ORIG(MSG_STR_SLASH));
2044 	(void) strcat(path, file);
2045 	DBG_CALL(Dbg_libs_req(sdf->sdf_name, sdf->sdf_rfile, path));
2046 
2047 	if ((fd = open(path, O_RDONLY)) == -1)
2048 		return (0);
2049 	else {
2050 		Ifl_desc	*ifl;
2051 		char		*_path;
2052 
2053 		if ((_path = libld_malloc(strlen(path) + 1)) == 0)
2054 			return ((Ifl_desc *)S_ERROR);
2055 		(void) strcpy(_path, path);
2056 		ifl = process_open(_path, dlen, fd, ofl, NULL, rej);
2057 		(void) close(fd);
2058 		return (ifl);
2059 	}
2060 }
2061 
2062 /*
2063  * Finish any library processing.  Walk the list of so's that have been listed
2064  * as "included" by shared objects we have previously processed.  Examine them,
2065  * without adding them as explicit dependents of this program, in order to
2066  * complete our symbol definition process.  The search path rules are:
2067  *
2068  *  o	use any user supplied paths, i.e. LD_LIBRARY_PATH and -L, then
2069  *
2070  *  o	use any RPATH defined within the parent shared object, then
2071  *
2072  *  o	use the default directories, i.e. LIBPATH or -YP.
2073  */
2074 uintptr_t
2075 finish_libs(Ofl_desc *ofl)
2076 {
2077 	Listnode	*lnp1;
2078 	Sdf_desc	*sdf;
2079 	Rej_desc	rej = { 0 };
2080 
2081 	/*
2082 	 * Make sure we are back in dynamic mode.
2083 	 */
2084 	ofl->ofl_flags |= FLG_OF_DYNLIBS;
2085 
2086 	for (LIST_TRAVERSE(&ofl->ofl_soneed, lnp1, sdf)) {
2087 		Listnode	*lnp2;
2088 		const char	*path;
2089 		int		fd;
2090 		Ifl_desc	*ifl;
2091 		const char	*file = sdf->sdf_name;
2092 
2093 		/*
2094 		 * See if this file has already been processed.  At the time
2095 		 * this implicit dependency was determined there may still have
2096 		 * been more explict dependencies to process.  (Note, if we ever
2097 		 * do parse the command line three times we would be able to
2098 		 * do all this checking when processing the dynamic section).
2099 		 */
2100 		if (sdf->sdf_file)
2101 			continue;
2102 
2103 		for (LIST_TRAVERSE(&ofl->ofl_sos, lnp2, ifl)) {
2104 			if (!(ifl->ifl_flags & FLG_IF_NEEDSTR) &&
2105 			    (strcmp(file, ifl->ifl_soname) == 0)) {
2106 				sdf->sdf_file = ifl;
2107 				break;
2108 			}
2109 		}
2110 		if (sdf->sdf_file)
2111 			continue;
2112 
2113 		/*
2114 		 * If the current element embeds a "/", then it's to be taken
2115 		 * "as is", with no searching involved.
2116 		 */
2117 		for (path = file; *path; path++)
2118 			if (*path == '/')
2119 				break;
2120 		if (*path) {
2121 			DBG_CALL(Dbg_libs_req(sdf->sdf_name, sdf->sdf_rfile,
2122 			    file));
2123 			if ((fd = open(file, O_RDONLY)) == -1) {
2124 				eprintf(ERR_WARNING, MSG_INTL(MSG_FIL_NOTFOUND),
2125 				    file, sdf->sdf_rfile);
2126 			} else {
2127 				Rej_desc	_rej = { 0 };
2128 
2129 				ifl = process_open(file, sizeof (file) + 1,
2130 				    fd, ofl, NULL, &_rej);
2131 				(void) close(fd);
2132 
2133 				if (ifl == (Ifl_desc *)S_ERROR) {
2134 					return (S_ERROR);
2135 				}
2136 				if (_rej.rej_type) {
2137 					eprintf(ERR_WARNING,
2138 					    MSG_INTL(reject[_rej.rej_type]),
2139 					    _rej.rej_name ? rej.rej_name :
2140 					    MSG_INTL(MSG_STR_UNKNOWN),
2141 					    conv_reject_str(&_rej));
2142 				} else
2143 					sdf->sdf_file = ifl;
2144 			}
2145 			continue;
2146 		}
2147 
2148 		/*
2149 		 * Now search for this file in any user defined directories.
2150 		 */
2151 		for (LIST_TRAVERSE(&ofl->ofl_ulibdirs, lnp2, path)) {
2152 			Rej_desc	_rej = { 0 };
2153 
2154 			ifl = process_req_lib(sdf, path, file, ofl, &_rej);
2155 			if (ifl == (Ifl_desc *)S_ERROR) {
2156 				return (S_ERROR);
2157 			}
2158 			if (_rej.rej_type) {
2159 				if (rej.rej_type == 0) {
2160 					rej = _rej;
2161 					rej.rej_name = strdup(_rej.rej_name);
2162 				}
2163 			}
2164 			if (ifl) {
2165 				sdf->sdf_file = ifl;
2166 				break;
2167 			}
2168 		}
2169 		if (sdf->sdf_file)
2170 			continue;
2171 
2172 		/*
2173 		 * Next use the local rules defined within the parent shared
2174 		 * object.
2175 		 */
2176 		if (sdf->sdf_rpath != NULL) {
2177 			char	*rpath, *next;
2178 
2179 			rpath = libld_malloc(strlen(sdf->sdf_rpath) + 1);
2180 			if (rpath == 0)
2181 				return (S_ERROR);
2182 			(void) strcpy(rpath, sdf->sdf_rpath);
2183 			DBG_CALL(Dbg_libs_path(rpath, LA_SER_RUNPATH,
2184 			    sdf->sdf_rfile));
2185 			if ((path = strtok_r(rpath,
2186 			    MSG_ORIG(MSG_STR_COLON), &next)) != NULL) {
2187 				do {
2188 					Rej_desc	_rej = { 0 };
2189 
2190 					path = expand(sdf->sdf_rfile, path,
2191 					    &next);
2192 
2193 					ifl = process_req_lib(sdf, path,
2194 						file, ofl, &_rej);
2195 					if (ifl == (Ifl_desc *)S_ERROR) {
2196 						return (S_ERROR);
2197 					}
2198 					if (_rej.rej_type) {
2199 						if (rej.rej_type == 0) {
2200 						    rej = _rej;
2201 						    rej.rej_name =
2202 							strdup(_rej.rej_name);
2203 						}
2204 					}
2205 					if (ifl) {
2206 						sdf->sdf_file = ifl;
2207 						break;
2208 					}
2209 				} while ((path = strtok_r(NULL,
2210 				    MSG_ORIG(MSG_STR_COLON), &next)) != NULL);
2211 			}
2212 		}
2213 		if (sdf->sdf_file)
2214 			continue;
2215 
2216 		/*
2217 		 * Finally try the default library search directories.
2218 		 */
2219 		for (LIST_TRAVERSE(&ofl->ofl_dlibdirs, lnp2, path)) {
2220 			Rej_desc	_rej = { 0 };
2221 
2222 			ifl = process_req_lib(sdf, path, file, ofl, &rej);
2223 			if (ifl == (Ifl_desc *)S_ERROR) {
2224 				return (S_ERROR);
2225 			}
2226 			if (_rej.rej_type) {
2227 				if (rej.rej_type == 0) {
2228 					rej = _rej;
2229 					rej.rej_name = strdup(_rej.rej_name);
2230 				}
2231 			}
2232 			if (ifl) {
2233 				sdf->sdf_file = ifl;
2234 				break;
2235 			}
2236 		}
2237 		if (sdf->sdf_file)
2238 			continue;
2239 
2240 		/*
2241 		 * If we've got this far we haven't found the shared object.
2242 		 * If an object was found, but was rejected for some reason,
2243 		 * print a diagnostic to that effect, otherwise generate a
2244 		 * generic "not found" diagnostic.
2245 		 */
2246 		if (rej.rej_type) {
2247 			eprintf(ERR_WARNING, MSG_INTL(reject[rej.rej_type]),
2248 			    rej.rej_name ? rej.rej_name :
2249 			    MSG_INTL(MSG_STR_UNKNOWN), conv_reject_str(&rej));
2250 		} else {
2251 			eprintf(ERR_WARNING, MSG_INTL(MSG_FIL_NOTFOUND), file,
2252 			    sdf->sdf_rfile);
2253 		}
2254 	}
2255 
2256 	/*
2257 	 * Finally, now that all objects have been input, make sure any version
2258 	 * requirements have been met.
2259 	 */
2260 	return (vers_verify(ofl));
2261 }
2262