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