xref: /titanic_52/usr/src/cmd/sgs/rtld/common/elf.c (revision d2d5cf7c5d909b74a88d499283e24750a9a52c5d)
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 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*
28  *	Copyright (c) 1988 AT&T
29  *	  All Rights Reserved
30  */
31 
32 /*
33  * Object file dependent support for ELF objects.
34  */
35 
36 #include	<stdio.h>
37 #include	<sys/procfs.h>
38 #include	<sys/mman.h>
39 #include	<sys/debug.h>
40 #include	<string.h>
41 #include	<limits.h>
42 #include	<dlfcn.h>
43 #include	<debug.h>
44 #include	<conv.h>
45 #include	"_rtld.h"
46 #include	"_audit.h"
47 #include	"_elf.h"
48 #include	"msg.h"
49 
50 /*
51  * Default and secure dependency search paths.
52  */
53 static Pnode		elf_dflt_dirs[] = {
54 #if	defined(_ELF64)
55 #ifndef	SGS_PRE_UNIFIED_PROCESS
56 	{ MSG_ORIG(MSG_PTH_LIB_64),		0,	MSG_PTH_LIB_64_SIZE,
57 		LA_SER_DEFAULT,			0,	&elf_dflt_dirs[1] },
58 #endif
59 	{ MSG_ORIG(MSG_PTH_USRLIB_64),		0,	MSG_PTH_USRLIB_64_SIZE,
60 		LA_SER_DEFAULT,			0, 0 }
61 #else
62 #ifndef	SGS_PRE_UNIFIED_PROCESS
63 	{ MSG_ORIG(MSG_PTH_LIB),		0,	MSG_PTH_LIB_SIZE,
64 		LA_SER_DEFAULT,			0,	&elf_dflt_dirs[1] },
65 #endif
66 	{ MSG_ORIG(MSG_PTH_USRLIB),		0,	MSG_PTH_USRLIB_SIZE,
67 		LA_SER_DEFAULT,			0, 0 }
68 #endif
69 };
70 
71 static Pnode		elf_secure_dirs[] = {
72 #if	defined(_ELF64)
73 #ifndef	SGS_PRE_UNIFIED_PROCESS
74 	{ MSG_ORIG(MSG_PTH_LIBSE_64),		0,	MSG_PTH_LIBSE_64_SIZE,
75 		LA_SER_SECURE,			0,	&elf_secure_dirs[1] },
76 #endif
77 	{ MSG_ORIG(MSG_PTH_USRLIBSE_64),	0,
78 		MSG_PTH_USRLIBSE_64_SIZE,
79 		LA_SER_SECURE,			0, 0 }
80 #else
81 #ifndef	SGS_PRE_UNIFIED_PROCESS
82 	{ MSG_ORIG(MSG_PTH_LIBSE),		0,	MSG_PTH_LIBSE_SIZE,
83 		LA_SER_SECURE,			0,	&elf_secure_dirs[1] },
84 #endif
85 	{ MSG_ORIG(MSG_PTH_USRLIBSE),		0,	MSG_PTH_USRLIBSE_SIZE,
86 		LA_SER_SECURE,			0, 0 }
87 #endif
88 };
89 
90 /*
91  * Defines for local functions.
92  */
93 static Pnode	*elf_fix_name(const char *, Rt_map *, uint_t);
94 static int	elf_are_u(Rej_desc *);
95 static void	elf_dladdr(ulong_t, Rt_map *, Dl_info *, void **, int);
96 static ulong_t	elf_entry_pt(void);
97 static char	*elf_get_so(const char *, const char *);
98 static Rt_map	*elf_map_so(Lm_list *, Aliste, const char *, const char *,
99 		    int, int *);
100 static int	elf_needed(Lm_list *, Aliste, Rt_map *, int *);
101 static void	elf_unmap_so(Rt_map *);
102 static int	elf_verify_vers(const char *, Rt_map *, Rt_map *);
103 
104 /*
105  * Functions and data accessed through indirect pointers.
106  */
107 Fct elf_fct = {
108 	elf_are_u,
109 	elf_entry_pt,
110 	elf_map_so,
111 	elf_unmap_so,
112 	elf_needed,
113 	lookup_sym,
114 	elf_reloc,
115 	elf_dflt_dirs,
116 	elf_secure_dirs,
117 	elf_fix_name,
118 	elf_get_so,
119 	elf_dladdr,
120 	dlsym_handle,
121 	elf_verify_vers,
122 	elf_set_prot
123 };
124 
125 
126 /*
127  * Redefine NEEDED name if necessary.
128  */
129 static Pnode *
130 elf_fix_name(const char *name, Rt_map *clmp, uint_t orig)
131 {
132 	/*
133 	 * For ABI compliance, if we are asked for ld.so.1, then really give
134 	 * them libsys.so.1 (the SONAME of libsys.so.1 is ld.so.1).
135 	 */
136 	if (((*name == '/') &&
137 	/* BEGIN CSTYLED */
138 #if	defined(_ELF64)
139 	    (strcmp(name, MSG_ORIG(MSG_PTH_RTLD_64)) == 0)) ||
140 #else
141 	    (strcmp(name, MSG_ORIG(MSG_PTH_RTLD)) == 0)) ||
142 #endif
143 	    (strcmp(name, MSG_ORIG(MSG_FIL_RTLD)) == 0)) {
144 		/* END CSTYLED */
145 		Pnode	*pnp;
146 
147 		DBG_CALL(Dbg_file_fixname(LIST(clmp), name,
148 		    MSG_ORIG(MSG_PTH_LIBSYS)));
149 		if (((pnp = calloc(sizeof (Pnode), 1)) == 0) ||
150 		    ((pnp->p_name = strdup(MSG_ORIG(MSG_PTH_LIBSYS))) == 0)) {
151 			if (pnp)
152 				free(pnp);
153 			return (0);
154 		}
155 		pnp->p_len = MSG_PTH_LIBSYS_SIZE;
156 		return (pnp);
157 	}
158 
159 	return (expand_paths(clmp, name, orig, 0));
160 }
161 
162 /*
163  * Determine if we have been given an ELF file and if so determine if the file
164  * is compatible.  Returns 1 if true, else 0 and sets the reject descriptor
165  * with associated error information.
166  */
167 static int
168 elf_are_u(Rej_desc *rej)
169 {
170 	Ehdr	*ehdr;
171 
172 	/*
173 	 * Determine if we're an elf file.  If not simply return, we don't set
174 	 * any rejection information as this test allows use to scroll through
175 	 * the objects we support (ELF, AOUT).
176 	 */
177 	if (fmap->fm_fsize < sizeof (Ehdr) ||
178 	    fmap->fm_maddr[EI_MAG0] != ELFMAG0 ||
179 	    fmap->fm_maddr[EI_MAG1] != ELFMAG1 ||
180 	    fmap->fm_maddr[EI_MAG2] != ELFMAG2 ||
181 	    fmap->fm_maddr[EI_MAG3] != ELFMAG3) {
182 		return (0);
183 	}
184 
185 	/*
186 	 * Check class and encoding.
187 	 */
188 	/* LINTED */
189 	ehdr = (Ehdr *)fmap->fm_maddr;
190 	if (ehdr->e_ident[EI_CLASS] != M_CLASS) {
191 		rej->rej_type = SGS_REJ_CLASS;
192 		rej->rej_info = (uint_t)ehdr->e_ident[EI_CLASS];
193 		return (0);
194 	}
195 	if (ehdr->e_ident[EI_DATA] != M_DATA) {
196 		rej->rej_type = SGS_REJ_DATA;
197 		rej->rej_info = (uint_t)ehdr->e_ident[EI_DATA];
198 		return (0);
199 	}
200 	if ((ehdr->e_type != ET_REL) && (ehdr->e_type != ET_EXEC) &&
201 	    (ehdr->e_type != ET_DYN)) {
202 		rej->rej_type = SGS_REJ_TYPE;
203 		rej->rej_info = (uint_t)ehdr->e_type;
204 		return (0);
205 	}
206 
207 	/*
208 	 * Verify machine specific flags, and hardware capability requirements.
209 	 */
210 	if ((elf_mach_flags_check(rej, ehdr) == 0) ||
211 	    (cap_check(rej, ehdr) == 0))
212 		return (0);
213 
214 	/*
215 	 * Verify ELF version.  ??? is this too restrictive ???
216 	 */
217 	if (ehdr->e_version > EV_CURRENT) {
218 		rej->rej_type = SGS_REJ_VERSION;
219 		rej->rej_info = (uint_t)ehdr->e_version;
220 		return (0);
221 	}
222 	return (1);
223 }
224 
225 /*
226  * The runtime linker employs lazy loading to provide the libraries needed for
227  * debugging, preloading .o's and dldump().  As these are seldom used, the
228  * standard startup of ld.so.1 doesn't initialize all the information necessary
229  * to perform plt relocation on ld.so.1's link-map.  The first time lazy loading
230  * is called we get here to perform these initializations:
231  *
232  *  o	elf_needed() is called to set up the DYNINFO() indexes for each lazy
233  *	dependency.  Typically, for all other objects, this is called during
234  *	analyze_so(), but as ld.so.1 is set-contained we skip this processing.
235  *
236  *  o	For intel, ld.so.1's JMPSLOT relocations need relative updates. These
237  *	are by default skipped thus delaying all relative relocation processing
238  * 	on every invocation of ld.so.1.
239  */
240 int
241 elf_rtld_load()
242 {
243 	Lm_list	*lml = &lml_rtld;
244 	Rt_map	*lmp = lml->lm_head;
245 
246 	if (lml->lm_flags & LML_FLG_PLTREL)
247 		return (1);
248 
249 	/*
250 	 * As we need to refer to the DYNINFO() information, insure that it has
251 	 * been initialized.
252 	 */
253 	if (elf_needed(lml, ALIST_OFF_DATA, lmp, NULL) == 0)
254 		return (0);
255 
256 #if	defined(__i386)
257 	/*
258 	 * This is a kludge to give ld.so.1 a performance benefit on i386.
259 	 * It's based around two factors.
260 	 *
261 	 *  o	JMPSLOT relocations (PLT's) actually need a relative relocation
262 	 *	applied to the GOT entry so that they can find PLT0.
263 	 *
264 	 *  o	ld.so.1 does not exercise *any* PLT's before it has made a call
265 	 *	to elf_lazy_load().  This is because all dynamic dependencies
266 	 * 	are recorded as lazy dependencies.
267 	 */
268 	(void) elf_reloc_relacount((ulong_t)JMPREL(lmp),
269 	    (ulong_t)(PLTRELSZ(lmp) / RELENT(lmp)), (ulong_t)RELENT(lmp),
270 	    (ulong_t)ADDR(lmp));
271 #endif
272 
273 	lml->lm_flags |= LML_FLG_PLTREL;
274 	return (1);
275 }
276 
277 /*
278  * Lazy load an object.
279  */
280 Rt_map *
281 elf_lazy_load(Rt_map *clmp, Slookup *slp, uint_t ndx, const char *sym,
282     int *in_nfavl)
283 {
284 	Rt_map		*nlmp, *hlmp;
285 	Dyninfo		*dip = &DYNINFO(clmp)[ndx], *pdip;
286 	uint_t		flags = 0;
287 	Pnode		*pnp;
288 	const char	*name;
289 	Lm_list		*lml = LIST(clmp);
290 	Lm_cntl		*lmc;
291 	Aliste		lmco;
292 
293 	/*
294 	 * If this dependency has already been processed, we're done.
295 	 */
296 	if (((nlmp = (Rt_map *)dip->di_info) != 0) ||
297 	    (dip->di_flags & FLG_DI_LDD_DONE))
298 		return (nlmp);
299 
300 	/*
301 	 * If we're running under ldd(1), indicate that this dependency has been
302 	 * processed (see test above).  It doesn't matter whether the object is
303 	 * successfully loaded or not, this flag simply ensures that we don't
304 	 * repeatedly attempt to load an object that has already failed to load.
305 	 * To do so would create multiple failure diagnostics for the same
306 	 * object under ldd(1).
307 	 */
308 	if (lml->lm_flags & LML_FLG_TRC_ENABLE)
309 		dip->di_flags |= FLG_DI_LDD_DONE;
310 
311 	/*
312 	 * Determine the initial dependency name.
313 	 */
314 	name = STRTAB(clmp) + DYN(clmp)[ndx].d_un.d_val;
315 	DBG_CALL(Dbg_file_lazyload(clmp, name, sym));
316 
317 	/*
318 	 * If this object needs to establish its own group, make sure a handle
319 	 * is created.
320 	 */
321 	if (dip->di_flags & FLG_DI_GROUP)
322 		flags |= (FLG_RT_SETGROUP | FLG_RT_HANDLE);
323 
324 	/*
325 	 * Lazy dependencies are identified as DT_NEEDED entries with a
326 	 * DF_P1_LAZYLOAD flag in the previous DT_POSFLAG_1 element.  The
327 	 * dynamic information element that corresponds to the DT_POSFLAG_1
328 	 * entry is free, and thus used to store the present entrance
329 	 * identifier.  This identifier is used to prevent multiple attempts to
330 	 * load a failed lazy loadable dependency within the same runtime linker
331 	 * operation.  However, future attempts to reload this dependency are
332 	 * still possible.
333 	 */
334 	if (ndx && (pdip = dip - 1) && (pdip->di_flags & FLG_DI_POSFLAG1))
335 		pdip->di_info = (void *)slp->sl_id;
336 
337 	/*
338 	 * Expand the requested name if necessary.
339 	 */
340 	if ((pnp = elf_fix_name(name, clmp, 0)) == 0)
341 		return (0);
342 
343 	/*
344 	 * Provided the object on the head of the link-map has completed its
345 	 * relocation, create a new link-map control list for this request.
346 	 */
347 	hlmp = lml->lm_head;
348 	if (FLAGS(hlmp) & FLG_RT_RELOCED) {
349 		if ((lmc = alist_append(&lml->lm_lists, 0, sizeof (Lm_cntl),
350 		    AL_CNT_LMLISTS)) == 0) {
351 			remove_pnode(pnp);
352 			return (0);
353 		}
354 		lmco = (Aliste)((char *)lmc - (char *)lml->lm_lists);
355 	} else {
356 		lmc = 0;
357 		lmco = ALIST_OFF_DATA;
358 	}
359 
360 	/*
361 	 * Load the associated object.
362 	 */
363 	dip->di_info = nlmp =
364 	    load_one(lml, lmco, pnp, clmp, MODE(clmp), flags, 0, in_nfavl);
365 
366 	/*
367 	 * Remove any expanded pathname infrastructure.  Reduce the pending lazy
368 	 * dependency count of the caller, together with the link-map lists
369 	 * count of objects that still have lazy dependencies pending.
370 	 */
371 	remove_pnode(pnp);
372 	if (--LAZY(clmp) == 0)
373 		LIST(clmp)->lm_lazy--;
374 
375 	/*
376 	 * Finish processing the objects associated with this request, and
377 	 * create an association between the caller and this dependency.
378 	 */
379 	if (nlmp && ((bind_one(clmp, nlmp, BND_NEEDED) == 0) ||
380 	    (analyze_lmc(lml, lmco, nlmp, in_nfavl) == 0) ||
381 	    (relocate_lmc(lml, lmco, clmp, nlmp, in_nfavl) == 0)))
382 		dip->di_info = nlmp = 0;
383 
384 	/*
385 	 * If this lazyload has failed, and we've created a new link-map
386 	 * control list to which this request has added objects, then remove
387 	 * all the objects that have been associated to this request.
388 	 */
389 	if ((nlmp == 0) && lmc && lmc->lc_head)
390 		remove_lmc(lml, clmp, lmc, lmco, name);
391 
392 	/*
393 	 * Finally, remove any link-map control list that was created.
394 	 */
395 	if (lmc)
396 		remove_cntl(lml, lmco);
397 
398 	/*
399 	 * If this lazy loading failed, record the fact, and bump the lazy
400 	 * counts.
401 	 */
402 	if (nlmp == 0) {
403 		dip->di_flags |= FLG_DI_LAZYFAIL;
404 		if (LAZY(clmp)++ == 0)
405 			LIST(clmp)->lm_lazy++;
406 	}
407 
408 	return (nlmp);
409 }
410 
411 /*
412  * Return the entry point of the ELF executable.
413  */
414 static ulong_t
415 elf_entry_pt(void)
416 {
417 	return (ENTRY(lml_main.lm_head));
418 }
419 
420 /*
421  * Unmap a given ELF shared object from the address space.
422  */
423 static void
424 elf_unmap_so(Rt_map *lmp)
425 {
426 	caddr_t	addr;
427 	size_t	size;
428 	Mmap	*mmaps;
429 
430 	/*
431 	 * If this link map represents a relocatable object concatenation, then
432 	 * the image was simply generated in allocated memory.  Free the memory.
433 	 *
434 	 * Note: the memory was originally allocated in the libelf:_elf_outmap
435 	 * routine and would normally have been free'd in elf_outsync(), but
436 	 * because we 'interpose' on that routine the memory  wasn't free'd at
437 	 * that time.
438 	 */
439 	if (FLAGS(lmp) & FLG_RT_IMGALLOC) {
440 		free((void *)ADDR(lmp));
441 		return;
442 	}
443 
444 	/*
445 	 * If padding was enabled via rtld_db, then we have at least one page
446 	 * in front of the image - and possibly a trailing page.
447 	 * Unmap the front page first:
448 	 */
449 	if (PADSTART(lmp) != ADDR(lmp)) {
450 		addr = (caddr_t)M_PTRUNC(PADSTART(lmp));
451 		size = ADDR(lmp) - (ulong_t)addr;
452 		(void) munmap(addr, size);
453 	}
454 
455 	/*
456 	 * Unmap any trailing padding.
457 	 */
458 	if (M_PROUND((PADSTART(lmp) + PADIMLEN(lmp))) >
459 	    M_PROUND(ADDR(lmp) + MSIZE(lmp))) {
460 		addr = (caddr_t)M_PROUND(ADDR(lmp) + MSIZE(lmp));
461 		size = M_PROUND(PADSTART(lmp) + PADIMLEN(lmp)) - (ulong_t)addr;
462 		(void) munmap(addr, size);
463 	}
464 
465 	/*
466 	 * Unmmap all mapped segments.
467 	 */
468 	for (mmaps = MMAPS(lmp); mmaps->m_vaddr; mmaps++)
469 		(void) munmap(mmaps->m_vaddr, mmaps->m_msize);
470 }
471 
472 /*
473  * Determine if a dependency requires a particular version and if so verify
474  * that the version exists in the dependency.
475  */
476 static int
477 elf_verify_vers(const char *name, Rt_map *clmp, Rt_map *nlmp)
478 {
479 	Verneed		*vnd = VERNEED(clmp);
480 	int		_num, num = VERNEEDNUM(clmp);
481 	char		*cstrs = (char *)STRTAB(clmp);
482 	Lm_list		*lml = LIST(clmp);
483 
484 	/*
485 	 * Traverse the callers version needed information and determine if any
486 	 * specific versions are required from the dependency.
487 	 */
488 	DBG_CALL(Dbg_ver_need_title(LIST(clmp), NAME(clmp)));
489 	for (_num = 1; _num <= num; _num++,
490 	    vnd = (Verneed *)((Xword)vnd + vnd->vn_next)) {
491 		Half		cnt = vnd->vn_cnt;
492 		Vernaux		*vnap;
493 		char		*nstrs, *need;
494 
495 		/*
496 		 * Determine if a needed entry matches this dependency.
497 		 */
498 		need = (char *)(cstrs + vnd->vn_file);
499 		if (strcmp(name, need) != 0)
500 			continue;
501 
502 		if ((lml->lm_flags & LML_FLG_TRC_VERBOSE) &&
503 		    ((FLAGS1(clmp) & FL1_RT_LDDSTUB) == 0))
504 			(void) printf(MSG_INTL(MSG_LDD_VER_FIND), name);
505 
506 		/*
507 		 * Validate that each version required actually exists in the
508 		 * dependency.
509 		 */
510 		nstrs = (char *)STRTAB(nlmp);
511 
512 		for (vnap = (Vernaux *)((Xword)vnd + vnd->vn_aux); cnt;
513 		    cnt--, vnap = (Vernaux *)((Xword)vnap + vnap->vna_next)) {
514 			char		*version, *define;
515 			Verdef		*vdf = VERDEF(nlmp);
516 			ulong_t		_num, num = VERDEFNUM(nlmp);
517 			int		found = 0;
518 
519 			/*
520 			 * Skip validation of versions that are marked
521 			 * INFO. This optimization is used for versions
522 			 * that are inherited by another version. Verification
523 			 * of the inheriting version is sufficient.
524 			 *
525 			 * Such versions are recorded in the object for the
526 			 * benefit of VERSYM entries that refer to them. This
527 			 * provides a purely diagnositic benefit.
528 			 */
529 			if (vnap->vna_flags & VER_FLG_INFO)
530 				continue;
531 
532 			version = (char *)(cstrs + vnap->vna_name);
533 			DBG_CALL(Dbg_ver_need_entry(lml, 0, need, version));
534 
535 			for (_num = 1; _num <= num; _num++,
536 			    vdf = (Verdef *)((Xword)vdf + vdf->vd_next)) {
537 				Verdaux		*vdap;
538 
539 				if (vnap->vna_hash != vdf->vd_hash)
540 					continue;
541 
542 				vdap = (Verdaux *)((Xword)vdf + vdf->vd_aux);
543 				define = (char *)(nstrs + vdap->vda_name);
544 				if (strcmp(version, define) != 0)
545 					continue;
546 
547 				found++;
548 				break;
549 			}
550 
551 			/*
552 			 * If we're being traced print out any matched version
553 			 * when the verbose (-v) option is in effect.  Always
554 			 * print any unmatched versions.
555 			 */
556 			if (lml->lm_flags & LML_FLG_TRC_ENABLE) {
557 				/* BEGIN CSTYLED */
558 				if (found) {
559 				    if (!(lml->lm_flags & LML_FLG_TRC_VERBOSE))
560 					continue;
561 
562 				    (void) printf(MSG_ORIG(MSG_LDD_VER_FOUND),
563 					need, version, NAME(nlmp));
564 				} else {
565 				    if (rtld_flags & RT_FL_SILENCERR)
566 					continue;
567 
568 				    (void) printf(MSG_INTL(MSG_LDD_VER_NFOUND),
569 					need, version);
570 				}
571 				/* END CSTYLED */
572 				continue;
573 			}
574 
575 			/*
576 			 * If the version hasn't been found then this is a
577 			 * candidate for a fatal error condition.  Weak
578 			 * version definition requirements are silently
579 			 * ignored.  Also, if the image inspected for a version
580 			 * definition has no versioning recorded at all then
581 			 * silently ignore this (this provides better backward
582 			 * compatibility to old images created prior to
583 			 * versioning being available).  Both of these skipped
584 			 * diagnostics are available under tracing (see above).
585 			 */
586 			if ((found == 0) && (num != 0) &&
587 			    (!(vnap->vna_flags & VER_FLG_WEAK))) {
588 				eprintf(lml, ERR_FATAL,
589 				    MSG_INTL(MSG_VER_NFOUND), need, version,
590 				    NAME(clmp));
591 				return (0);
592 			}
593 		}
594 	}
595 	DBG_CALL(Dbg_util_nl(lml, DBG_NL_STD));
596 	return (1);
597 }
598 
599 /*
600  * Search through the dynamic section for DT_NEEDED entries and perform one
601  * of two functions.  If only the first argument is specified then load the
602  * defined shared object, otherwise add the link map representing the defined
603  * link map the the dlopen list.
604  */
605 static int
606 elf_needed(Lm_list *lml, Aliste lmco, Rt_map *clmp, int *in_nfavl)
607 {
608 	Dyn		*dyn, *pdyn;
609 	ulong_t		ndx = 0;
610 	uint_t		lazy, flags;
611 	Word		lmflags = lml->lm_flags;
612 	Word		lmtflags = lml->lm_tflags;
613 
614 	/*
615 	 * Process each shared object on needed list.
616 	 */
617 	if (DYN(clmp) == 0)
618 		return (1);
619 
620 	for (dyn = (Dyn *)DYN(clmp), pdyn = NULL; dyn->d_tag != DT_NULL;
621 	    pdyn = dyn++, ndx++) {
622 		Dyninfo	*dip = &DYNINFO(clmp)[ndx];
623 		Rt_map	*nlmp = 0;
624 		char	*name;
625 		int	silent = 0;
626 		Pnode	*pnp;
627 
628 		switch (dyn->d_tag) {
629 		case DT_POSFLAG_1:
630 			dip->di_flags |= FLG_DI_POSFLAG1;
631 			continue;
632 		case DT_NEEDED:
633 		case DT_USED:
634 			lazy = flags = 0;
635 			dip->di_flags |= FLG_DI_NEEDED;
636 
637 			if (pdyn && (pdyn->d_tag == DT_POSFLAG_1)) {
638 				if ((pdyn->d_un.d_val & DF_P1_LAZYLOAD) &&
639 				    ((lmtflags & LML_TFLG_NOLAZYLD) == 0)) {
640 					dip->di_flags |= FLG_DI_LAZY;
641 					lazy = 1;
642 				}
643 				if (pdyn->d_un.d_val & DF_P1_GROUPPERM) {
644 					dip->di_flags |= FLG_DI_GROUP;
645 					flags =
646 					    (FLG_RT_SETGROUP | FLG_RT_HANDLE);
647 				}
648 			}
649 
650 			name = (char *)STRTAB(clmp) + dyn->d_un.d_val;
651 
652 			/*
653 			 * NOTE, libc.so.1 can't be lazy loaded.  Although a
654 			 * lazy position flag won't be produced when a RTLDINFO
655 			 * .dynamic entry is found (introduced with the UPM in
656 			 * Solaris 10), it was possible to mark libc for lazy
657 			 * loading on previous releases.  To reduce the overhead
658 			 * of testing for this occurrence, only carry out this
659 			 * check for the first object on the link-map list
660 			 * (there aren't many applications built without libc).
661 			 */
662 			if (lazy && (lml->lm_head == clmp) &&
663 			    (strcmp(name, MSG_ORIG(MSG_FIL_LIBC)) == 0))
664 				lazy = 0;
665 
666 			/*
667 			 * Don't bring in lazy loaded objects yet unless we've
668 			 * been asked to attempt to load all available objects
669 			 * (crle(1) sets LD_FLAGS=loadavail).  Even under
670 			 * RTLD_NOW we don't process this - RTLD_NOW will cause
671 			 * relocation processing which in turn might trigger
672 			 * lazy loading, but its possible that the object has a
673 			 * lazy loaded file with no bindings (i.e., it should
674 			 * never have been a dependency in the first place).
675 			 */
676 			if (lazy) {
677 				if ((lmflags & LML_FLG_LOADAVAIL) == 0) {
678 					LAZY(clmp)++;
679 					lazy = flags = 0;
680 					continue;
681 				}
682 
683 				/*
684 				 * Silence any error messages - see description
685 				 * under elf_lookup_filtee().
686 				 */
687 				if ((rtld_flags & RT_FL_SILENCERR) == 0) {
688 					rtld_flags |= RT_FL_SILENCERR;
689 					silent = 1;
690 				}
691 			}
692 			break;
693 		case DT_AUXILIARY:
694 			dip->di_flags |= FLG_DI_AUXFLTR;
695 			continue;
696 		case DT_SUNW_AUXILIARY:
697 			dip->di_flags |= (FLG_DI_AUXFLTR | FLG_DI_SYMFLTR);
698 			continue;
699 		case DT_FILTER:
700 			dip->di_flags |= FLG_DI_STDFLTR;
701 			continue;
702 		case DT_SUNW_FILTER:
703 			dip->di_flags |= (FLG_DI_STDFLTR | FLG_DI_SYMFLTR);
704 			continue;
705 		default:
706 			continue;
707 		}
708 
709 		DBG_CALL(Dbg_file_needed(clmp, name));
710 
711 		/*
712 		 * If we're running under ldd(1), indicate that this dependency
713 		 * has been processed.  It doesn't matter whether the object is
714 		 * successfully loaded or not, this flag simply ensures that we
715 		 * don't repeatedly attempt to load an object that has already
716 		 * failed to load.  To do so would create multiple failure
717 		 * diagnostics for the same object under ldd(1).
718 		 */
719 		if (lml->lm_flags & LML_FLG_TRC_ENABLE)
720 			dip->di_flags |= FLG_DI_LDD_DONE;
721 
722 		/*
723 		 * Establish the objects name, load it and establish a binding
724 		 * with the caller.
725 		 */
726 		if (((pnp = elf_fix_name(name, clmp, 0)) == 0) || ((nlmp =
727 		    load_one(lml, lmco, pnp, clmp, MODE(clmp), flags, 0,
728 		    in_nfavl)) == 0) || (bind_one(clmp, nlmp, BND_NEEDED) == 0))
729 			nlmp = 0;
730 
731 		/*
732 		 * Clean up any infrastructure, including the removal of the
733 		 * error suppression state, if it had been previously set in
734 		 * this routine.
735 		 */
736 		if (pnp)
737 			remove_pnode(pnp);
738 		if (silent)
739 			rtld_flags &= ~RT_FL_SILENCERR;
740 
741 		if ((dip->di_info = (void *)nlmp) == 0) {
742 			/*
743 			 * If the object could not be mapped, continue if error
744 			 * suppression is established or we're here with ldd(1).
745 			 */
746 			if ((MODE(clmp) & RTLD_CONFGEN) || (lmflags &
747 			    (LML_FLG_LOADAVAIL | LML_FLG_TRC_ENABLE)))
748 				continue;
749 			else
750 				return (0);
751 		}
752 	}
753 
754 	if (LAZY(clmp))
755 		lml->lm_lazy++;
756 
757 	return (1);
758 }
759 
760 static int
761 elf_map_check(Lm_list *lml, const char *name, caddr_t vaddr, Off size)
762 {
763 	prmap_t		*maps, *_maps;
764 	int		pfd, num, _num;
765 	caddr_t		eaddr = vaddr + size;
766 	int		err;
767 
768 	/*
769 	 * If memory reservations have been established for alternative objects
770 	 * determine if this object falls within the reservation, if it does no
771 	 * further checking is required.
772 	 */
773 	if (rtld_flags & RT_FL_MEMRESV) {
774 		Rtc_head	*head = (Rtc_head *)config->c_bgn;
775 
776 		if ((vaddr >= (caddr_t)(uintptr_t)head->ch_resbgn) &&
777 		    (eaddr <= (caddr_t)(uintptr_t)head->ch_resend))
778 			return (0);
779 	}
780 
781 	/*
782 	 * Determine the mappings presently in use by this process.
783 	 */
784 	if ((pfd = pr_open(lml)) == FD_UNAVAIL)
785 		return (1);
786 
787 	if (ioctl(pfd, PIOCNMAP, (void *)&num) == -1) {
788 		err = errno;
789 		eprintf(lml, ERR_FATAL, MSG_INTL(MSG_SYS_PROC), name,
790 		    strerror(err));
791 		return (1);
792 	}
793 
794 	if ((maps = malloc((num + 1) * sizeof (prmap_t))) == 0)
795 		return (1);
796 
797 	if (ioctl(pfd, PIOCMAP, (void *)maps) == -1) {
798 		err = errno;
799 		eprintf(lml, ERR_FATAL, MSG_INTL(MSG_SYS_PROC), name,
800 		    strerror(err));
801 		free(maps);
802 		return (1);
803 	}
804 
805 	/*
806 	 * Determine if the supplied address clashes with any of the present
807 	 * process mappings.
808 	 */
809 	for (_num = 0, _maps = maps; _num < num; _num++, _maps++) {
810 		caddr_t		_eaddr = _maps->pr_vaddr + _maps->pr_size;
811 		Rt_map		*lmp;
812 		const char	*str;
813 
814 		if ((eaddr < _maps->pr_vaddr) || (vaddr >= _eaddr))
815 			continue;
816 
817 		/*
818 		 * We have a memory clash.  See if one of the known dynamic
819 		 * dependency mappings represents this space so as to provide
820 		 * the user a more meaningful message.
821 		 */
822 		if ((lmp = _caller(vaddr, 0)) != 0)
823 			str = NAME(lmp);
824 		else
825 			str = MSG_INTL(MSG_STR_UNKNOWN);
826 
827 		eprintf(lml, ERR_FATAL, MSG_INTL(MSG_GEN_MAPINUSE), name,
828 		    EC_NATPTR(vaddr), EC_OFF(size), str);
829 		return (1);
830 	}
831 	free(maps);
832 	return (0);
833 }
834 
835 /*
836  * Obtain a memory reservation.  On newer systems, both MAP_ANON and MAP_ALIGN
837  * are used to obtained an aligned reservation from anonymous memory.  If
838  * MAP_ANON isn't available, then MAP_ALIGN isn't either, so obtain a standard
839  * reservation using the file as backing.
840  */
841 static Am_ret
842 elf_map_reserve(Lm_list *lml, const char *name, caddr_t *maddr, Off msize,
843     int mperm, int fd, Xword align)
844 {
845 	Am_ret	amret;
846 	int	mflag = MAP_PRIVATE | MAP_NORESERVE;
847 
848 #if defined(MAP_ALIGN)
849 	if ((rtld_flags2 & RT_FL2_NOMALIGN) == 0) {
850 		mflag |= MAP_ALIGN;
851 		*maddr = (caddr_t)align;
852 	}
853 #endif
854 	if ((amret = anon_map(lml, maddr, msize, PROT_NONE, mflag)) == AM_ERROR)
855 		return (amret);
856 
857 	if (amret == AM_OK)
858 		return (AM_OK);
859 
860 	/*
861 	 * If an anonymous memory request failed (which should only be the
862 	 * case if it is unsupported on the system we're running on), establish
863 	 * the initial mapping directly from the file.
864 	 */
865 	*maddr = 0;
866 	if ((*maddr = mmap(*maddr, msize, mperm, MAP_PRIVATE,
867 	    fd, 0)) == MAP_FAILED) {
868 		int	err = errno;
869 		eprintf(lml, ERR_FATAL, MSG_INTL(MSG_SYS_MMAP), name,
870 		    strerror(err));
871 		return (AM_ERROR);
872 	}
873 	return (AM_NOSUP);
874 }
875 
876 static void *
877 elf_map_textdata(caddr_t addr, Off flen, int mperm, int phdr_mperm, int mflag,
878     int fd, Off foff)
879 {
880 #if	defined(MAP_TEXT) && defined(MAP_INITDATA)
881 	static int	notd = 0;
882 
883 	/*
884 	 * If MAP_TEXT and MAP_INITDATA are available, select the appropriate
885 	 * flag.
886 	 */
887 	if (notd == 0) {
888 		if ((phdr_mperm & (PROT_WRITE | PROT_EXEC)) == PROT_EXEC)
889 			mflag |= MAP_TEXT;
890 		else
891 			mflag |= MAP_INITDATA;
892 	}
893 #endif
894 	if (mmap((caddr_t)addr, flen, mperm, mflag, fd, foff) != MAP_FAILED)
895 		return (0);
896 
897 #if	defined(MAP_TEXT) && defined(MAP_INITDATA)
898 	if ((notd == 0) && (errno == EINVAL)) {
899 		/*
900 		 * MAP_TEXT and MAP_INITDATA may not be supported on this
901 		 * platform, try again without.
902 		 */
903 		notd = 1;
904 		mflag &= ~(MAP_TEXT | MAP_INITDATA);
905 
906 		return (mmap((caddr_t)addr, flen, mperm, mflag, fd, foff));
907 	}
908 #endif
909 	return (MAP_FAILED);
910 }
911 
912 /*
913  * Map in a file.
914  */
915 static caddr_t
916 elf_map_it(
917 	Lm_list		*lml,		/* link-map list */
918 	const char	*name,		/* actual name stored for pathname */
919 	Off		fsize,		/* total mapping claim of the file */
920 	Ehdr		*ehdr,		/* ELF header of file */
921 	Phdr		*fphdr,		/* first loadable Phdr */
922 	Phdr		*lphdr,		/* last loadable Phdr */
923 	Phdr		**rrphdr,	/* return first Phdr in reservation */
924 	caddr_t		*rraddr,	/* return start of reservation */
925 	Off		*rrsize,	/* return total size of reservation */
926 	int		fixed,		/* image is resolved to a fixed addr */
927 	int		fd,		/* images file descriptor */
928 	Xword		align,		/* image segments maximum alignment */
929 	Mmap		*mmaps,		/* mmap information array and */
930 	uint_t		*mmapcnt)	/* 	mapping count */
931 {
932 	caddr_t		raddr;		/* reservation address */
933 	Off		rsize;		/* reservation size */
934 	Phdr		*phdr;		/* working program header poiner */
935 	caddr_t		maddr;		/* working mmap address */
936 	caddr_t		faddr;		/* working file address */
937 	size_t		padsize;	/* object padding requirement */
938 	size_t		padpsize = 0;	/* padding size rounded to next page */
939 	size_t		padmsize = 0;	/* padding size rounded for alignment */
940 	int		skipfseg;	/* skip mapping first segment */
941 	int		mperm;		/* segment permissions */
942 	Am_ret		amret = AM_NOSUP;
943 
944 	/*
945 	 * If padding is required extend both the front and rear of the image.
946 	 * To insure the image itself is mapped at the correct alignment the
947 	 * initial padding is rounded up to the nearest page.  Once the image is
948 	 * mapped the excess can be pruned to the nearest page required for the
949 	 * actual padding itself.
950 	 */
951 	if ((padsize = r_debug.rtd_objpad) != 0) {
952 		padpsize = M_PROUND(padsize);
953 		if (fixed)
954 			padmsize = padpsize;
955 		else
956 			padmsize = S_ROUND(padsize, align);
957 	}
958 
959 	/*
960 	 * Determine the initial permissions used to map in the first segment.
961 	 * If this segments memsz is greater that its filesz then the difference
962 	 * must be zeroed.  Make sure this segment is writable.
963 	 */
964 	mperm = 0;
965 	if (fphdr->p_flags & PF_R)
966 		mperm |= PROT_READ;
967 	if (fphdr->p_flags & PF_X)
968 		mperm |= PROT_EXEC;
969 	if ((fphdr->p_flags & PF_W) || (fphdr->p_memsz > fphdr->p_filesz))
970 		mperm |= PROT_WRITE;
971 
972 	/*
973 	 * Determine whether or not to let system reserve address space based on
974 	 * whether this is a dynamic executable (addresses in object are fixed)
975 	 * or a shared object (addresses in object are relative to the objects'
976 	 * base).
977 	 */
978 	if (fixed) {
979 		/*
980 		 * Determine the reservation address and size, and insure that
981 		 * this reservation isn't already in use.
982 		 */
983 		faddr = maddr = (caddr_t)M_PTRUNC((ulong_t)fphdr->p_vaddr);
984 		raddr = maddr - padpsize;
985 		rsize = fsize + padpsize + padsize;
986 
987 		if (lml_main.lm_head) {
988 			if (elf_map_check(lml, name, raddr, rsize) != 0)
989 				return (0);
990 		}
991 
992 		/*
993 		 * As this is a fixed image, all segments must be individually
994 		 * mapped.
995 		 */
996 		skipfseg = 0;
997 
998 	} else {
999 		size_t	esize;
1000 
1001 		/*
1002 		 * If this isn't a fixed image, reserve enough address space for
1003 		 * the entire image to be mapped.  The amount of reservation is
1004 		 * the range between the beginning of the first, and end of the
1005 		 * last loadable segment, together with any padding, plus the
1006 		 * alignment of the first segment.
1007 		 *
1008 		 * The optimal reservation is made as a no-reserve mapping from
1009 		 * anonymous memory.  Each segment is then mapped into this
1010 		 * reservation.  If the anonymous mapping capability isn't
1011 		 * available, the reservation is obtained from the file itself.
1012 		 * In this case the first segment of the image is mapped as part
1013 		 * of the reservation, thus only the following segments need to
1014 		 * be remapped.
1015 		 */
1016 		rsize = fsize + padmsize + padsize;
1017 		if ((amret = elf_map_reserve(lml, name, &raddr, rsize, mperm,
1018 		    fd, align)) == AM_ERROR)
1019 			return (0);
1020 		maddr = raddr + padmsize;
1021 		faddr = (caddr_t)S_ROUND((Off)maddr, align);
1022 
1023 		/*
1024 		 * If this reservation has been obtained from anonymous memory,
1025 		 * then all segments must be individually mapped.  Otherwise,
1026 		 * the first segment heads the reservation.
1027 		 */
1028 		if (amret == AM_OK)
1029 			skipfseg = 0;
1030 		else
1031 			skipfseg = 1;
1032 
1033 		/*
1034 		 * For backward compatibility (where MAP_ALIGN isn't available),
1035 		 * insure the alignment of the reservation is adequate for this
1036 		 * object, and if not remap the object to obtain the correct
1037 		 * alignment.
1038 		 */
1039 		if (faddr != maddr) {
1040 			(void) munmap(raddr, rsize);
1041 
1042 			rsize += align;
1043 			if ((amret = elf_map_reserve(lml, name, &raddr, rsize,
1044 			    mperm, fd, align)) == AM_ERROR)
1045 				return (0);
1046 
1047 			maddr = faddr = (caddr_t)S_ROUND((Off)(raddr +
1048 			    padpsize), align);
1049 
1050 			esize = maddr - raddr + padpsize;
1051 
1052 			/*
1053 			 * As ths image has been realigned, the first segment
1054 			 * of the file needs to be remapped to its correct
1055 			 * location.
1056 			 */
1057 			skipfseg = 0;
1058 		} else
1059 			esize = padmsize - padpsize;
1060 
1061 		/*
1062 		 * If this reservation included padding, remove any excess for
1063 		 * the start of the image (the padding was adjusted to insure
1064 		 * the image was aligned appropriately).
1065 		 */
1066 		if (esize) {
1067 			(void) munmap(raddr, esize);
1068 			raddr += esize;
1069 			rsize -= esize;
1070 		}
1071 	}
1072 
1073 	/*
1074 	 * At this point we know the initial location of the image, and its
1075 	 * size.  Pass these back to the caller for inclusion in the link-map
1076 	 * that will eventually be created.
1077 	 */
1078 	*rraddr = raddr;
1079 	*rrsize = rsize;
1080 
1081 	/*
1082 	 * The first loadable segment is now pointed to by maddr.  This segment
1083 	 * will eventually contain the elf header and program headers, so reset
1084 	 * the program header.  Pass this  back to the caller for inclusion in
1085 	 * the link-map so it can be used for later unmapping operations.
1086 	 */
1087 	/* LINTED */
1088 	*rrphdr = (Phdr *)((char *)maddr + ehdr->e_phoff);
1089 
1090 	/*
1091 	 * If padding is required at the front of the image, obtain that now.
1092 	 * Note, if we've already obtained a reservation from anonymous memory
1093 	 * then this reservation will already include suitable padding.
1094 	 * Otherwise this reservation is backed by the file, or in the case of
1095 	 * a fixed image, doesn't yet exist.  Map the padding so that it is
1096 	 * suitably protected (PROT_NONE), and insure the first segment of the
1097 	 * file is mapped to its correct location.
1098 	 */
1099 	if (padsize) {
1100 		if (amret == AM_NOSUP) {
1101 			if (dz_map(lml, raddr, padpsize, PROT_NONE,
1102 			    (MAP_PRIVATE | MAP_FIXED | MAP_NORESERVE)) ==
1103 			    MAP_FAILED)
1104 				return (0);
1105 
1106 			skipfseg = 0;
1107 		}
1108 		rsize -= padpsize;
1109 	}
1110 
1111 	/*
1112 	 * Map individual segments.  For a fixed image, these will each be
1113 	 * unique mappings.  For a reservation these will fill in the
1114 	 * reservation.
1115 	 */
1116 	for (phdr = fphdr; phdr <= lphdr;
1117 	    phdr = (Phdr *)((Off)phdr + ehdr->e_phentsize)) {
1118 		caddr_t	addr;
1119 		Off	mlen, flen;
1120 		size_t	size;
1121 
1122 		/*
1123 		 * Skip non-loadable segments or segments that don't occupy
1124 		 * any memory.
1125 		 */
1126 		if ((phdr->p_type != PT_LOAD) || (phdr->p_memsz == 0))
1127 			continue;
1128 
1129 		/*
1130 		 * Establish this segments address relative to our base.
1131 		 */
1132 		addr = (caddr_t)M_PTRUNC((ulong_t)(phdr->p_vaddr +
1133 		    (fixed ? 0 : faddr)));
1134 
1135 		/*
1136 		 * Determine the mapping protection from the segment attributes.
1137 		 * Also determine the etext address from the last loadable
1138 		 * segment which has permissions but no write access.
1139 		 */
1140 		mperm = 0;
1141 		if (phdr->p_flags) {
1142 			if (phdr->p_flags & PF_R)
1143 				mperm |= PROT_READ;
1144 			if (phdr->p_flags & PF_X)
1145 				mperm |= PROT_EXEC;
1146 			if (phdr->p_flags & PF_W)
1147 				mperm |= PROT_WRITE;
1148 			else
1149 				fmap->fm_etext = phdr->p_vaddr + phdr->p_memsz +
1150 				    (ulong_t)(fixed ? 0 : faddr);
1151 		}
1152 
1153 		/*
1154 		 * Determine the type of mapping required.
1155 		 */
1156 		if ((phdr->p_filesz == 0) && (phdr->p_flags == 0)) {
1157 			/*
1158 			 * If this segment has no backing file and no flags
1159 			 * specified, then it defines a reservation.  At this
1160 			 * point all standard loadable segments will have been
1161 			 * processed.  The segment reservation is mapped
1162 			 * directly from /dev/null.
1163 			 */
1164 			if (nu_map(lml, (caddr_t)addr, phdr->p_memsz, PROT_NONE,
1165 			    MAP_FIXED | MAP_PRIVATE) == MAP_FAILED)
1166 				return (0);
1167 
1168 			mlen = phdr->p_memsz;
1169 			flen = 0;
1170 
1171 		} else if (phdr->p_filesz == 0) {
1172 			/*
1173 			 * If this segment has no backing file then it defines a
1174 			 * nobits segment and is mapped directly from /dev/zero.
1175 			 */
1176 			if (dz_map(lml, (caddr_t)addr, phdr->p_memsz, mperm,
1177 			    MAP_FIXED | MAP_PRIVATE) == MAP_FAILED)
1178 				return (0);
1179 
1180 			mlen = phdr->p_memsz;
1181 			flen = 0;
1182 
1183 		} else {
1184 			Off	foff;
1185 
1186 			/*
1187 			 * This mapping originates from the file.  Determine the
1188 			 * file offset to which the mapping will be directed
1189 			 * (must be aligned) and how much to map (might be more
1190 			 * than the file in the case of .bss).
1191 			 */
1192 			foff = M_PTRUNC((ulong_t)phdr->p_offset);
1193 			mlen = phdr->p_memsz + (phdr->p_offset - foff);
1194 			flen = phdr->p_filesz + (phdr->p_offset - foff);
1195 
1196 			/*
1197 			 * If this is a non-fixed, non-anonymous mapping, and no
1198 			 * padding is involved, then the first loadable segment
1199 			 * is already part of the initial reservation.  In this
1200 			 * case there is no need to remap this segment.
1201 			 */
1202 			if ((skipfseg == 0) || (phdr != fphdr)) {
1203 				int phdr_mperm = mperm;
1204 				/*
1205 				 * If this segments memsz is greater that its
1206 				 * filesz then the difference must be zeroed.
1207 				 * Make sure this segment is writable.
1208 				 */
1209 				if (phdr->p_memsz > phdr->p_filesz)
1210 					mperm |= PROT_WRITE;
1211 
1212 				if (elf_map_textdata((caddr_t)addr, flen,
1213 				    mperm, phdr_mperm,
1214 				    (MAP_FIXED | MAP_PRIVATE), fd, foff) ==
1215 				    MAP_FAILED) {
1216 					int	err = errno;
1217 					eprintf(lml, ERR_FATAL,
1218 					    MSG_INTL(MSG_SYS_MMAP), name,
1219 					    strerror(err));
1220 					return (0);
1221 				}
1222 			}
1223 
1224 			/*
1225 			 * If the memory occupancy of the segment overflows the
1226 			 * definition in the file, we need to "zero out" the end
1227 			 * of the mapping we've established, and if necessary,
1228 			 * map some more space from /dev/zero.  Note, zero'ed
1229 			 * memory must end on a double word boundary to satisfy
1230 			 * zero().
1231 			 */
1232 			if (phdr->p_memsz > phdr->p_filesz) {
1233 				caddr_t	zaddr;
1234 				size_t	zlen, zplen;
1235 				Off	fend;
1236 
1237 				foff = (Off)(phdr->p_vaddr + phdr->p_filesz +
1238 				    (fixed ? 0 : faddr));
1239 				zaddr = (caddr_t)M_PROUND(foff);
1240 				zplen = (size_t)(zaddr - foff);
1241 
1242 				fend = (Off)S_DROUND((size_t)(phdr->p_vaddr +
1243 				    phdr->p_memsz + (fixed ? 0 : faddr)));
1244 				zlen = (size_t)(fend - foff);
1245 
1246 				/*
1247 				 * Determine whether the number of bytes that
1248 				 * must be zero'ed overflow to the next page.
1249 				 * If not, simply clear the exact bytes
1250 				 * (filesz to memsz) from this page.  Otherwise,
1251 				 * clear the remaining bytes of this page, and
1252 				 * map an following pages from /dev/zero.
1253 				 */
1254 				if (zlen < zplen)
1255 					zero((caddr_t)foff, (long)zlen);
1256 				else {
1257 					zero((caddr_t)foff, (long)zplen);
1258 
1259 					if ((zlen = (fend - (Off)zaddr)) > 0) {
1260 						if (dz_map(lml, zaddr, zlen,
1261 						    mperm,
1262 						    MAP_FIXED | MAP_PRIVATE) ==
1263 						    MAP_FAILED)
1264 							return (0);
1265 					}
1266 				}
1267 			}
1268 		}
1269 
1270 		/*
1271 		 * Unmap anything from the last mapping address to this one and
1272 		 * update the mapping claim pointer.
1273 		 */
1274 		if ((fixed == 0) && ((size = addr - maddr) != 0)) {
1275 			(void) munmap(maddr, size);
1276 			rsize -= size;
1277 		}
1278 
1279 		/*
1280 		 * Retain this segments mapping information.
1281 		 */
1282 		mmaps[*mmapcnt].m_vaddr = addr;
1283 		mmaps[*mmapcnt].m_msize = mlen;
1284 		mmaps[*mmapcnt].m_fsize = flen;
1285 		mmaps[*mmapcnt].m_perm = mperm;
1286 		(*mmapcnt)++;
1287 
1288 		maddr = addr + M_PROUND(mlen);
1289 		rsize -= M_PROUND(mlen);
1290 	}
1291 
1292 	/*
1293 	 * If padding is required at the end of the image, obtain that now.
1294 	 * Note, if we've already obtained a reservation from anonymous memory
1295 	 * then this reservation will already include suitable padding.
1296 	 */
1297 	if (padsize) {
1298 		if (amret == AM_NOSUP) {
1299 			/*
1300 			 * maddr is currently page aligned from the last segment
1301 			 * mapping.
1302 			 */
1303 			if (dz_map(lml, maddr, padsize, PROT_NONE,
1304 			    (MAP_PRIVATE | MAP_FIXED | MAP_NORESERVE)) ==
1305 			    MAP_FAILED)
1306 				return (0);
1307 		}
1308 		maddr += padsize;
1309 		rsize -= padsize;
1310 	}
1311 
1312 	/*
1313 	 * Unmap any final reservation.
1314 	 */
1315 	if ((fixed == 0) && (rsize != 0))
1316 		(void) munmap(maddr, rsize);
1317 
1318 	return (faddr);
1319 }
1320 
1321 /*
1322  * A null symbol interpretor.  Used if a filter has no associated filtees.
1323  */
1324 /* ARGSUSED0 */
1325 static Sym *
1326 elf_null_find_sym(Slookup *slp, Rt_map **dlmp, uint_t *binfo, int *in_nfavl)
1327 {
1328 	return ((Sym *)0);
1329 }
1330 
1331 /*
1332  * Disable filtee use.
1333  */
1334 static void
1335 elf_disable_filtee(Rt_map *lmp, Dyninfo *dip)
1336 {
1337 	dip->di_info = 0;
1338 
1339 	if ((dip->di_flags & FLG_DI_SYMFLTR) == 0) {
1340 		/*
1341 		 * If this is an object filter, free the filtee's duplication.
1342 		 */
1343 		if (OBJFLTRNDX(lmp) != FLTR_DISABLED) {
1344 			free(REFNAME(lmp));
1345 			REFNAME(lmp) = (char *)0;
1346 			OBJFLTRNDX(lmp) = FLTR_DISABLED;
1347 
1348 			/*
1349 			 * Indicate that this filtee is no longer available.
1350 			 */
1351 			if (dip->di_flags & FLG_DI_STDFLTR)
1352 				SYMINTP(lmp) = elf_null_find_sym;
1353 
1354 		}
1355 	} else if (dip->di_flags & FLG_DI_STDFLTR) {
1356 		/*
1357 		 * Indicate that this standard filtee is no longer available.
1358 		 */
1359 		if (SYMSFLTRCNT(lmp))
1360 			SYMSFLTRCNT(lmp)--;
1361 	} else {
1362 		/*
1363 		 * Indicate that this auxiliary filtee is no longer available.
1364 		 */
1365 		if (SYMAFLTRCNT(lmp))
1366 			SYMAFLTRCNT(lmp)--;
1367 	}
1368 	dip->di_flags &= ~MSK_DI_FILTER;
1369 }
1370 
1371 /*
1372  * Find symbol interpreter - filters.
1373  * This function is called when the symbols from a shared object should
1374  * be resolved from the shared objects filtees instead of from within itself.
1375  *
1376  * A symbol name of 0 is used to trigger filtee loading.
1377  */
1378 static Sym *
1379 _elf_lookup_filtee(Slookup *slp, Rt_map **dlmp, uint_t *binfo, uint_t ndx,
1380     int *in_nfavl)
1381 {
1382 	const char	*name = slp->sl_name, *filtees;
1383 	Rt_map		*clmp = slp->sl_cmap;
1384 	Rt_map		*ilmp = slp->sl_imap;
1385 	Pnode		*pnp, **pnpp;
1386 	int		any;
1387 	Dyninfo		*dip = &DYNINFO(ilmp)[ndx];
1388 	Lm_list		*lml = LIST(ilmp);
1389 
1390 	/*
1391 	 * Indicate that the filter has been used.  If a binding already exists
1392 	 * to the caller, indicate that this object is referenced.  This insures
1393 	 * we don't generate false unreferenced diagnostics from ldd -u/U or
1394 	 * debugging.  Don't create a binding regardless, as this filter may
1395 	 * have been dlopen()'ed.
1396 	 */
1397 	if (name && (ilmp != clmp)) {
1398 		Word	tracing = (LIST(clmp)->lm_flags &
1399 		    (LML_FLG_TRC_UNREF | LML_FLG_TRC_UNUSED));
1400 
1401 		if (tracing || DBG_ENABLED) {
1402 			Bnd_desc 	*bdp;
1403 			Aliste		idx;
1404 
1405 			FLAGS1(ilmp) |= FL1_RT_USED;
1406 
1407 			if ((tracing & LML_FLG_TRC_UNREF) || DBG_ENABLED) {
1408 				for (APLIST_TRAVERSE(CALLERS(ilmp), idx, bdp)) {
1409 					if (bdp->b_caller == clmp) {
1410 						bdp->b_flags |= BND_REFER;
1411 						break;
1412 					}
1413 				}
1414 			}
1415 		}
1416 	}
1417 
1418 	/*
1419 	 * If this is the first call to process this filter, establish the
1420 	 * filtee list.  If a configuration file exists, determine if any
1421 	 * filtee associations for this filter, and its filtee reference, are
1422 	 * defined.  Otherwise, process the filtee reference.  Any token
1423 	 * expansion is also completed at this point (i.e., $PLATFORM).
1424 	 */
1425 	filtees = (char *)STRTAB(ilmp) + DYN(ilmp)[ndx].d_un.d_val;
1426 	if (dip->di_info == 0) {
1427 		if (rtld_flags2 & RT_FL2_FLTCFG)
1428 			dip->di_info = elf_config_flt(lml, PATHNAME(ilmp),
1429 			    filtees);
1430 
1431 		if (dip->di_info == 0) {
1432 			DBG_CALL(Dbg_file_filter(lml, NAME(ilmp), filtees, 0));
1433 			if ((lml->lm_flags &
1434 			    (LML_FLG_TRC_VERBOSE | LML_FLG_TRC_SEARCH)) &&
1435 			    ((FLAGS1(ilmp) & FL1_RT_LDDSTUB) == 0))
1436 				(void) printf(MSG_INTL(MSG_LDD_FIL_FILTER),
1437 				    NAME(ilmp), filtees);
1438 
1439 			if ((dip->di_info = (void *)expand_paths(ilmp,
1440 			    filtees, 0, 0)) == 0) {
1441 				elf_disable_filtee(ilmp, dip);
1442 				return ((Sym *)0);
1443 			}
1444 		}
1445 	}
1446 
1447 	/*
1448 	 * Traverse the filtee list, dlopen()'ing any objects specified and
1449 	 * using their group handle to lookup the symbol.
1450 	 */
1451 	for (any = 0, pnpp = (Pnode **)&(dip->di_info), pnp = *pnpp; pnp;
1452 	    pnpp = &pnp->p_next, pnp = *pnpp) {
1453 		int	mode;
1454 		Grp_hdl	*ghp;
1455 		Rt_map	*nlmp = 0;
1456 
1457 		if (pnp->p_len == 0)
1458 			continue;
1459 
1460 		/*
1461 		 * Establish the mode of the filtee from the filter.  As filtees
1462 		 * are loaded via a dlopen(), make sure that RTLD_GROUP is set
1463 		 * and the filtees aren't global.  It would be nice to have
1464 		 * RTLD_FIRST used here also, but as filters got out long before
1465 		 * RTLD_FIRST was introduced it's a little too late now.
1466 		 */
1467 		mode = MODE(ilmp) | RTLD_GROUP;
1468 		mode &= ~RTLD_GLOBAL;
1469 
1470 		/*
1471 		 * Insure that any auxiliary filter can locate symbols from its
1472 		 * caller.
1473 		 */
1474 		if (dip->di_flags & FLG_DI_AUXFLTR)
1475 			mode |= RTLD_PARENT;
1476 
1477 		/*
1478 		 * Process any hardware capability directory.  Establish a new
1479 		 * link-map control list from which to analyze any newly added
1480 		 * objects.
1481 		 */
1482 		if ((pnp->p_info == 0) && (pnp->p_orig & PN_TKN_HWCAP)) {
1483 			Lm_cntl	*lmc;
1484 			Aliste	lmco;
1485 
1486 			if (FLAGS(lml->lm_head) & FLG_RT_RELOCED) {
1487 				if ((lmc = alist_append(&lml->lm_lists, 0,
1488 				    sizeof (Lm_cntl), AL_CNT_LMLISTS)) == 0)
1489 					return ((Sym *)0);
1490 				lmco = (Aliste)((char *)lmc -
1491 				    (char *)lml->lm_lists);
1492 			} else {
1493 				lmc = 0;
1494 				lmco = ALIST_OFF_DATA;
1495 			}
1496 
1497 			pnp = hwcap_filtees(pnpp, lmco, lmc, dip, ilmp, filtees,
1498 			    mode, (FLG_RT_HANDLE | FLG_RT_HWCAP), in_nfavl);
1499 
1500 			/*
1501 			 * Now that any hardware capability objects have been
1502 			 * processed, remove any link-map control list.
1503 			 */
1504 			if (lmc)
1505 				remove_cntl(lml, lmco);
1506 		}
1507 
1508 		if (pnp->p_len == 0)
1509 			continue;
1510 
1511 		/*
1512 		 * Process an individual filtee.
1513 		 */
1514 		if (pnp->p_info == 0) {
1515 			const char	*filtee = pnp->p_name;
1516 			int		audit = 0;
1517 
1518 			DBG_CALL(Dbg_file_filtee(lml, NAME(ilmp), filtee, 0));
1519 
1520 			ghp = 0;
1521 
1522 			/*
1523 			 * Determine if the reference link map is already
1524 			 * loaded.  As an optimization compare the filtee with
1525 			 * our interpretor.  The most common filter is
1526 			 * libdl.so.1, which is a filter on ld.so.1.
1527 			 */
1528 #if	defined(_ELF64)
1529 			if (strcmp(filtee, MSG_ORIG(MSG_PTH_RTLD_64)) == 0) {
1530 #else
1531 			if (strcmp(filtee, MSG_ORIG(MSG_PTH_RTLD)) == 0) {
1532 #endif
1533 				/*
1534 				 * Create an association between ld.so.1 and the
1535 				 * filter.  As an optimization, a handle for
1536 				 * ld.so.1 itself (required for the dlopen()
1537 				 * family filtering mechanism) shouldn't search
1538 				 * any dependencies of ld.so.1.  Omitting
1539 				 * GPD_ADDEPS prevents the addition of any
1540 				 * ld.so.1 dependencies to this handle.
1541 				 */
1542 				nlmp = lml_rtld.lm_head;
1543 				if ((ghp = hdl_create(&lml_rtld, nlmp, ilmp,
1544 				    (GPH_LDSO | GPH_FIRST | GPH_FILTEE),
1545 				    (GPD_DLSYM | GPD_RELOC), GPD_PARENT)) == 0)
1546 					nlmp = 0;
1547 
1548 				/*
1549 				 * Establish the filter handle to prevent any
1550 				 * recursion.
1551 				 */
1552 				if (nlmp && ghp)
1553 					pnp->p_info = (void *)ghp;
1554 
1555 				/*
1556 				 * Audit the filter/filtee established.  Ignore
1557 				 * any return from the auditor, as we can't
1558 				 * allow ignore filtering to ld.so.1, otherwise
1559 				 * nothing is going to work.
1560 				 */
1561 				if (nlmp && ((lml->lm_tflags | FLAGS1(ilmp)) &
1562 				    LML_TFLG_AUD_OBJFILTER))
1563 					(void) audit_objfilter(ilmp, filtees,
1564 					    nlmp, 0);
1565 
1566 			} else {
1567 				Rej_desc	rej = { 0 };
1568 				Lm_cntl		*lmc;
1569 				Aliste		lmco;
1570 
1571 				/*
1572 				 * Establish a new link-map control list from
1573 				 * which to analyze any newly added objects.
1574 				 */
1575 				if (FLAGS(lml->lm_head) & FLG_RT_RELOCED) {
1576 					if ((lmc =
1577 					    alist_append(&lml->lm_lists, 0,
1578 					    sizeof (Lm_cntl),
1579 					    AL_CNT_LMLISTS)) == 0)
1580 						return ((Sym *)0);
1581 					lmco = (Aliste)((char *)lmc -
1582 					    (char *)lml->lm_lists);
1583 				} else {
1584 					lmc = 0;
1585 					lmco = ALIST_OFF_DATA;
1586 				}
1587 
1588 				/*
1589 				 * Load the filtee.  Note, an auditor can
1590 				 * provide an alternative name.
1591 				 */
1592 				if ((nlmp = load_path(lml, lmco, &(pnp->p_name),
1593 				    ilmp, mode, FLG_RT_HANDLE, &ghp, 0,
1594 				    &rej, in_nfavl)) == 0) {
1595 					file_notfound(LIST(ilmp), filtee, ilmp,
1596 					    FLG_RT_HANDLE, &rej);
1597 					remove_rej(&rej);
1598 				}
1599 				filtee = pnp->p_name;
1600 
1601 				/*
1602 				 * Establish the filter handle to prevent any
1603 				 * recursion.
1604 				 */
1605 				if (nlmp && ghp) {
1606 					ghp->gh_flags |= GPH_FILTEE;
1607 					pnp->p_info = (void *)ghp;
1608 
1609 					FLAGS1(nlmp) |= FL1_RT_USED;
1610 				}
1611 
1612 				/*
1613 				 * Audit the filter/filtee established.  A
1614 				 * return of 0 indicates the auditor wishes to
1615 				 * ignore this filtee.
1616 				 */
1617 				if (nlmp && ((lml->lm_tflags | FLAGS1(ilmp)) &
1618 				    LML_TFLG_AUD_OBJFILTER)) {
1619 					if (audit_objfilter(ilmp, filtees,
1620 					    nlmp, 0) == 0) {
1621 						audit = 1;
1622 						nlmp = 0;
1623 					}
1624 				}
1625 
1626 				/*
1627 				 * Finish processing the objects associated with
1628 				 * this request.  Create an association between
1629 				 * this object and the originating filter to
1630 				 * provide sufficient information to tear down
1631 				 * this filtee if necessary.
1632 				 */
1633 				if (nlmp && ghp && ((analyze_lmc(lml, lmco,
1634 				    nlmp, in_nfavl) == 0) || (relocate_lmc(lml,
1635 				    lmco, ilmp, nlmp, in_nfavl) == 0)))
1636 					nlmp = 0;
1637 
1638 				/*
1639 				 * If the filtee has been successfully
1640 				 * processed, then create an association
1641 				 * between the filter and filtee.  This
1642 				 * association provides sufficient information
1643 				 * to tear down the filter and filtee if
1644 				 * necessary.
1645 				 */
1646 				DBG_CALL(Dbg_file_hdl_title(DBG_HDL_ADD));
1647 				if (nlmp && ghp &&
1648 				    (hdl_add(ghp, ilmp, GPD_FILTER) == 0))
1649 					nlmp = 0;
1650 
1651 				/*
1652 				 * If this filtee loading has failed, and we've
1653 				 * created a new link-map control list to which
1654 				 * this request has added objects, then remove
1655 				 * all the objects that have been associated to
1656 				 * this request.
1657 				 */
1658 				if ((nlmp == 0) && lmc && lmc->lc_head)
1659 					remove_lmc(lml, clmp, lmc, lmco, name);
1660 
1661 				/*
1662 				 * Remove any link-map control list that was
1663 				 * created.
1664 				 */
1665 				if (lmc)
1666 					remove_cntl(lml, lmco);
1667 			}
1668 
1669 			/*
1670 			 * Generate a diagnostic if the filtee couldn't be
1671 			 * loaded, null out the pnode entry, and continue
1672 			 * the search.  Otherwise, retain this group handle
1673 			 * for future symbol searches.
1674 			 */
1675 			if (nlmp == 0) {
1676 				DBG_CALL(Dbg_file_filtee(lml, 0, filtee,
1677 				    audit));
1678 
1679 				pnp->p_info = 0;
1680 				pnp->p_len = 0;
1681 				continue;
1682 			}
1683 		}
1684 
1685 		ghp = (Grp_hdl *)pnp->p_info;
1686 
1687 		/*
1688 		 * If we're just here to trigger filtee loading skip the symbol
1689 		 * lookup so we'll continue looking for additional filtees.
1690 		 */
1691 		if (name) {
1692 			Grp_desc	*gdp;
1693 			Sym		*sym = 0;
1694 			Aliste		idx;
1695 			Slookup		sl = *slp;
1696 
1697 			sl.sl_flags |= LKUP_FIRST;
1698 			any++;
1699 
1700 			/*
1701 			 * Look for the symbol in the handles dependencies.
1702 			 */
1703 			for (ALIST_TRAVERSE(ghp->gh_depends, idx, gdp)) {
1704 				if ((gdp->gd_flags & GPD_DLSYM) == 0)
1705 					continue;
1706 
1707 				/*
1708 				 * If our parent is a dependency don't look at
1709 				 * it (otherwise we are in a recursive loop).
1710 				 * This situation can occur with auxiliary
1711 				 * filters if the filtee has a dependency on the
1712 				 * filter.  This dependency isn't necessary as
1713 				 * auxiliary filters are opened RTLD_PARENT, but
1714 				 * users may still unknowingly add an explicit
1715 				 * dependency to the parent.
1716 				 */
1717 				if ((sl.sl_imap = gdp->gd_depend) == ilmp)
1718 					continue;
1719 
1720 				if (((sym = SYMINTP(sl.sl_imap)(&sl, dlmp,
1721 				    binfo, in_nfavl)) != 0) ||
1722 				    (ghp->gh_flags & GPH_FIRST))
1723 					break;
1724 			}
1725 
1726 			/*
1727 			 * If a symbol has been found, indicate the binding
1728 			 * and return the symbol.
1729 			 */
1730 			if (sym) {
1731 				*binfo |= DBG_BINFO_FILTEE;
1732 				return (sym);
1733 			}
1734 		}
1735 
1736 		/*
1737 		 * If this object is tagged to terminate filtee processing we're
1738 		 * done.
1739 		 */
1740 		if (FLAGS1(ghp->gh_ownlmp) & FL1_RT_ENDFILTE)
1741 			break;
1742 	}
1743 
1744 	/*
1745 	 * If we're just here to trigger filtee loading then we're done.
1746 	 */
1747 	if (name == 0)
1748 		return ((Sym *)0);
1749 
1750 	/*
1751 	 * If no filtees have been found for a filter, clean up any Pnode
1752 	 * structures and disable their search completely.  For auxiliary
1753 	 * filters we can reselect the symbol search function so that we never
1754 	 * enter this routine again for this object.  For standard filters we
1755 	 * use the null symbol routine.
1756 	 */
1757 	if (any == 0) {
1758 		remove_pnode((Pnode *)dip->di_info);
1759 		elf_disable_filtee(ilmp, dip);
1760 		return ((Sym *)0);
1761 	}
1762 
1763 	return ((Sym *)0);
1764 }
1765 
1766 /*
1767  * Focal point for disabling error messages for auxiliary filters.  As an
1768  * auxiliary filter allows for filtee use, but provides a fallback should a
1769  * filtee not exist (or fail to load), any errors generated as a consequence of
1770  * trying to load the filtees are typically suppressed.  Setting RT_FL_SILENCERR
1771  * suppresses errors generated by eprint(), but insures a debug diagnostic is
1772  * produced.  ldd(1) employs printf(), and here, the selection of whether to
1773  * print a diagnostic in regards to auxiliary filters is a little more complex.
1774  *
1775  *   .	The determination of whether to produce an ldd message, or a fatal
1776  *	error message is driven by LML_FLG_TRC_ENABLE.
1777  *   .	More detailed ldd messages may also be driven off of LML_FLG_TRC_WARN,
1778  *	(ldd -d/-r), LML_FLG_TRC_VERBOSE (ldd -v), LML_FLG_TRC_SEARCH (ldd -s),
1779  *	and LML_FLG_TRC_UNREF/LML_FLG_TRC_UNUSED (ldd -U/-u).
1780  *
1781  *   .	If the calling object is lddstub, then several classes of message are
1782  *	suppressed.  The user isn't trying to diagnose lddstub, this is simply
1783  *	a stub executable employed to preload a user specified library against.
1784  *
1785  *   .	If RT_FL_SILENCERR is in effect then any generic ldd() messages should
1786  *	be suppressed.  All detailed ldd messages should still be produced.
1787  */
1788 Sym *
1789 elf_lookup_filtee(Slookup *slp, Rt_map **dlmp, uint_t *binfo, uint_t ndx,
1790     int *in_nfavl)
1791 {
1792 	Sym	*sym;
1793 	Dyninfo	*dip = &DYNINFO(slp->sl_imap)[ndx];
1794 	int	silent = 0;
1795 
1796 	/*
1797 	 * Make sure this entry is still acting as a filter.  We may have tried
1798 	 * to process this previously, and disabled it if the filtee couldn't
1799 	 * be processed.  However, other entries may provide different filtees
1800 	 * that are yet to be completed.
1801 	 */
1802 	if (dip->di_flags == 0)
1803 		return ((Sym *)0);
1804 
1805 	/*
1806 	 * Indicate whether an error message is required should this filtee not
1807 	 * be found, based on the type of filter.
1808 	 */
1809 	if ((dip->di_flags & FLG_DI_AUXFLTR) &&
1810 	    ((rtld_flags & (RT_FL_WARNFLTR | RT_FL_SILENCERR)) == 0)) {
1811 		rtld_flags |= RT_FL_SILENCERR;
1812 		silent = 1;
1813 	}
1814 
1815 	sym = _elf_lookup_filtee(slp, dlmp, binfo, ndx, in_nfavl);
1816 
1817 	if (silent)
1818 		rtld_flags &= ~RT_FL_SILENCERR;
1819 
1820 	return (sym);
1821 }
1822 
1823 /*
1824  * Compute the elf hash value (as defined in the ELF access library).
1825  * The form of the hash table is:
1826  *
1827  *	|--------------|
1828  *	| # of buckets |
1829  *	|--------------|
1830  *	| # of chains  |
1831  *	|--------------|
1832  *	|   bucket[]   |
1833  *	|--------------|
1834  *	|   chain[]    |
1835  *	|--------------|
1836  */
1837 ulong_t
1838 elf_hash(const char *name)
1839 {
1840 	uint_t	hval = 0;
1841 
1842 	while (*name) {
1843 		uint_t	g;
1844 		hval = (hval << 4) + *name++;
1845 		if ((g = (hval & 0xf0000000)) != 0)
1846 			hval ^= g >> 24;
1847 		hval &= ~g;
1848 	}
1849 	return ((ulong_t)hval);
1850 }
1851 
1852 /*
1853  * If flag argument has LKUP_SPEC set, we treat undefined symbols of type
1854  * function specially in the executable - if they have a value, even though
1855  * undefined, we use that value.  This allows us to associate all references
1856  * to a function's address to a single place in the process: the plt entry
1857  * for that function in the executable.  Calls to lookup from plt binding
1858  * routines do NOT set LKUP_SPEC in the flag.
1859  */
1860 Sym *
1861 elf_find_sym(Slookup *slp, Rt_map **dlmp, uint_t *binfo, int *in_nfavl)
1862 {
1863 	const char	*name = slp->sl_name;
1864 	Rt_map		*ilmp = slp->sl_imap;
1865 	ulong_t		hash = slp->sl_hash;
1866 	uint_t		ndx, htmp, buckets, *chainptr;
1867 	Sym		*sym, *symtabptr;
1868 	char		*strtabptr, *strtabname;
1869 	uint_t		flags1;
1870 	Syminfo		*sip;
1871 
1872 	/*
1873 	 * If we're only here to establish a symbols index, skip the diagnostic
1874 	 * used to trace a symbol search.
1875 	 */
1876 	if ((slp->sl_flags & LKUP_SYMNDX) == 0)
1877 		DBG_CALL(Dbg_syms_lookup(ilmp, name, MSG_ORIG(MSG_STR_ELF)));
1878 
1879 	if (HASH(ilmp) == 0)
1880 		return ((Sym *)0);
1881 
1882 	buckets = HASH(ilmp)[0];
1883 	/* LINTED */
1884 	htmp = (uint_t)hash % buckets;
1885 
1886 	/*
1887 	 * Get the first symbol on hash chain and initialize the string
1888 	 * and symbol table pointers.
1889 	 */
1890 	if ((ndx = HASH(ilmp)[htmp + 2]) == 0)
1891 		return ((Sym *)0);
1892 
1893 	chainptr = HASH(ilmp) + 2 + buckets;
1894 	strtabptr = STRTAB(ilmp);
1895 	symtabptr = SYMTAB(ilmp);
1896 
1897 	while (ndx) {
1898 		sym = symtabptr + ndx;
1899 		strtabname = strtabptr + sym->st_name;
1900 
1901 		/*
1902 		 * Compare the symbol found with the name required.  If the
1903 		 * names don't match continue with the next hash entry.
1904 		 */
1905 		if ((*strtabname++ != *name) || strcmp(strtabname, &name[1])) {
1906 			if ((ndx = chainptr[ndx]) != 0)
1907 				continue;
1908 			return ((Sym *)0);
1909 		}
1910 
1911 		/*
1912 		 * The Solaris ld does not put DT_VERSYM in the dynamic
1913 		 * section, but the GNU ld does. The GNU runtime linker
1914 		 * interprets the top bit of the 16-bit Versym value
1915 		 * (0x8000) as the "hidden" bit. If this bit is set,
1916 		 * the linker is supposed to act as if that symbol does
1917 		 * not exist. The hidden bit supports their versioning
1918 		 * scheme, which allows multiple incompatible functions
1919 		 * with the same name to exist at different versions
1920 		 * within an object. The Solaris linker does not support this
1921 		 * mechanism, or the model of interface evolution that
1922 		 * it allows, but we honor the hidden bit in GNU ld
1923 		 * produced objects in order to interoperate with them.
1924 		 */
1925 		if ((VERSYM(ilmp) != NULL) &&
1926 		    ((VERSYM(ilmp)[ndx] & 0x8000) != 0)) {
1927 			DBG_CALL(Dbg_syms_ignore_gnuver(ilmp, name,
1928 			    ndx, VERSYM(ilmp)[ndx]));
1929 			if ((ndx = chainptr[ndx]) != 0)
1930 				continue;
1931 			return ((Sym *)0);
1932 		}
1933 
1934 		/*
1935 		 * If we're only here to establish a symbols index, we're done.
1936 		 */
1937 		if (slp->sl_flags & LKUP_SYMNDX)
1938 			return (sym);
1939 
1940 		/*
1941 		 * If we find a match and the symbol is defined, return the
1942 		 * symbol pointer and the link map in which it was found.
1943 		 */
1944 		if (sym->st_shndx != SHN_UNDEF) {
1945 			*dlmp = ilmp;
1946 			*binfo |= DBG_BINFO_FOUND;
1947 			if ((FLAGS(ilmp) & FLG_RT_OBJINTPO) ||
1948 			    ((FLAGS(ilmp) & FLG_RT_SYMINTPO) &&
1949 			    is_sym_interposer(ilmp, sym)))
1950 				*binfo |= DBG_BINFO_INTERPOSE;
1951 			break;
1952 
1953 		/*
1954 		 * If we find a match and the symbol is undefined, the
1955 		 * symbol type is a function, and the value of the symbol
1956 		 * is non zero, then this is a special case.  This allows
1957 		 * the resolution of a function address to the plt[] entry.
1958 		 * See SPARC ABI, Dynamic Linking, Function Addresses for
1959 		 * more details.
1960 		 */
1961 		} else if ((slp->sl_flags & LKUP_SPEC) &&
1962 		    (FLAGS(ilmp) & FLG_RT_ISMAIN) && (sym->st_value != 0) &&
1963 		    (ELF_ST_TYPE(sym->st_info) == STT_FUNC)) {
1964 			*dlmp = ilmp;
1965 			*binfo |= (DBG_BINFO_FOUND | DBG_BINFO_PLTADDR);
1966 			if ((FLAGS(ilmp) & FLG_RT_OBJINTPO) ||
1967 			    ((FLAGS(ilmp) & FLG_RT_SYMINTPO) &&
1968 			    is_sym_interposer(ilmp, sym)))
1969 				*binfo |= DBG_BINFO_INTERPOSE;
1970 			return (sym);
1971 		}
1972 
1973 		/*
1974 		 * Undefined symbol.
1975 		 */
1976 		return ((Sym *)0);
1977 	}
1978 
1979 	/*
1980 	 * We've found a match.  Determine if the defining object contains
1981 	 * symbol binding information.
1982 	 */
1983 	if ((sip = SYMINFO(ilmp)) != 0)
1984 		sip += ndx;
1985 
1986 	/*
1987 	 * If this definition is a singleton, and we haven't followed a default
1988 	 * symbol search knowing that we're looking for a singleton (presumably
1989 	 * because the symbol definition has been changed since the referring
1990 	 * object was built), then reject this binding so that the caller can
1991 	 * fall back to a standard symbol search.
1992 	 */
1993 	if ((ELF_ST_VISIBILITY(sym->st_other) == STV_SINGLETON) &&
1994 	    (((slp->sl_flags & LKUP_STANDARD) == 0) ||
1995 	    (((slp->sl_flags & LKUP_SINGLETON) == 0) &&
1996 	    (LIST(ilmp)->lm_flags & LML_FLG_GROUPSEXIST)))) {
1997 		DBG_CALL(Dbg_bind_reject(slp->sl_cmap, ilmp, name,
1998 		    DBG_BNDREJ_SINGLE));
1999 		*binfo |= BINFO_REJSINGLE;
2000 		*binfo &= ~DBG_BINFO_MSK;
2001 		return ((Sym *)0);
2002 	}
2003 
2004 	/*
2005 	 * If this is a direct binding request, but the symbol definition has
2006 	 * disabled directly binding to it (presumably because the symbol
2007 	 * definition has been changed since the referring object was built),
2008 	 * indicate this failure so that the caller can fall back to a standard
2009 	 * symbol search.
2010 	 */
2011 	if (sip && (slp->sl_flags & LKUP_DIRECT) &&
2012 	    (sip->si_flags & SYMINFO_FLG_NOEXTDIRECT)) {
2013 		DBG_CALL(Dbg_bind_reject(slp->sl_cmap, ilmp, name,
2014 		    DBG_BNDREJ_NODIR));
2015 		*binfo |= BINFO_REJDIRECT;
2016 		*binfo &= ~DBG_BINFO_MSK;
2017 		return ((Sym *)0);
2018 	}
2019 
2020 	/*
2021 	 * Determine whether this object is acting as a filter.
2022 	 */
2023 	if (((flags1 = FLAGS1(ilmp)) & MSK_RT_FILTER) == 0)
2024 		return (sym);
2025 
2026 	/*
2027 	 * Determine if this object offers per-symbol filtering, and if so,
2028 	 * whether this symbol references a filtee.
2029 	 */
2030 	if (sip && (flags1 & (FL1_RT_SYMSFLTR | FL1_RT_SYMAFLTR))) {
2031 		/*
2032 		 * If this is a standard filter reference, and no standard
2033 		 * filtees remain to be inspected, we're done.  If this is an
2034 		 * auxiliary filter reference, and no auxiliary filtees remain,
2035 		 * we'll fall through in case any object filtering is available.
2036 		 */
2037 		if ((sip->si_flags & SYMINFO_FLG_FILTER) &&
2038 		    (SYMSFLTRCNT(ilmp) == 0))
2039 			return ((Sym *)0);
2040 
2041 		if ((sip->si_flags & SYMINFO_FLG_FILTER) ||
2042 		    ((sip->si_flags & SYMINFO_FLG_AUXILIARY) &&
2043 		    SYMAFLTRCNT(ilmp))) {
2044 			Sym	*fsym;
2045 
2046 			/*
2047 			 * This symbol has an associated filtee.  Lookup the
2048 			 * symbol in the filtee, and if it is found return it.
2049 			 * If the symbol doesn't exist, and this is a standard
2050 			 * filter, return an error, otherwise fall through to
2051 			 * catch any object filtering that may be available.
2052 			 */
2053 			if ((fsym = elf_lookup_filtee(slp, dlmp, binfo,
2054 			    sip->si_boundto, in_nfavl)) != 0)
2055 				return (fsym);
2056 			if (sip->si_flags & SYMINFO_FLG_FILTER)
2057 				return ((Sym *)0);
2058 		}
2059 	}
2060 
2061 	/*
2062 	 * Determine if this object provides global filtering.
2063 	 */
2064 	if (flags1 & (FL1_RT_OBJSFLTR | FL1_RT_OBJAFLTR)) {
2065 		Sym	*fsym;
2066 
2067 		if (OBJFLTRNDX(ilmp) != FLTR_DISABLED) {
2068 			/*
2069 			 * This object has an associated filtee.  Lookup the
2070 			 * symbol in the filtee, and if it is found return it.
2071 			 * If the symbol doesn't exist, and this is a standard
2072 			 * filter, return and error, otherwise return the symbol
2073 			 * within the filter itself.
2074 			 */
2075 			if ((fsym = elf_lookup_filtee(slp, dlmp, binfo,
2076 			    OBJFLTRNDX(ilmp), in_nfavl)) != 0)
2077 				return (fsym);
2078 		}
2079 
2080 		if (flags1 & FL1_RT_OBJSFLTR)
2081 			return ((Sym *)0);
2082 	}
2083 	return (sym);
2084 }
2085 
2086 /*
2087  * Create a new Rt_map structure for an ELF object and initialize
2088  * all values.
2089  */
2090 Rt_map *
2091 elf_new_lm(Lm_list *lml, const char *pname, const char *oname, Dyn *ld,
2092     ulong_t addr, ulong_t etext, Aliste lmco, ulong_t msize, ulong_t entry,
2093     ulong_t paddr, ulong_t padimsize, Mmap *mmaps, uint_t mmapcnt,
2094     int *in_nfavl)
2095 {
2096 	Rt_map		*lmp;
2097 	ulong_t		base, fltr = 0, audit = 0, cfile = 0, crle = 0;
2098 	Xword		rpath = 0;
2099 	Ehdr		*ehdr = (Ehdr *)addr;
2100 
2101 	DBG_CALL(Dbg_file_elf(lml, pname, (ulong_t)ld, addr, msize, entry,
2102 	    lml->lm_lmidstr, lmco));
2103 
2104 	/*
2105 	 * Allocate space for the link-map and private elf information.  Once
2106 	 * these are allocated and initialized, we can use remove_so(0, lmp) to
2107 	 * tear down the link-map should any failures occur.
2108 	 */
2109 	if ((lmp = calloc(sizeof (Rt_map), 1)) == 0)
2110 		return (0);
2111 	if ((ELFPRV(lmp) = calloc(sizeof (Rt_elfp), 1)) == 0) {
2112 		free(lmp);
2113 		return (0);
2114 	}
2115 
2116 	/*
2117 	 * All fields not filled in were set to 0 by calloc.
2118 	 */
2119 	ORIGNAME(lmp) = PATHNAME(lmp) = NAME(lmp) = (char *)pname;
2120 	DYN(lmp) = ld;
2121 	ADDR(lmp) = addr;
2122 	MSIZE(lmp) = msize;
2123 	ENTRY(lmp) = (Addr)entry;
2124 	SYMINTP(lmp) = elf_find_sym;
2125 	ETEXT(lmp) = etext;
2126 	FCT(lmp) = &elf_fct;
2127 	LIST(lmp) = lml;
2128 	PADSTART(lmp) = paddr;
2129 	PADIMLEN(lmp) = padimsize;
2130 	THREADID(lmp) = rt_thr_self();
2131 	OBJFLTRNDX(lmp) = FLTR_DISABLED;
2132 	SORTVAL(lmp) = -1;
2133 
2134 	MMAPS(lmp) = mmaps;
2135 	MMAPCNT(lmp) = mmapcnt;
2136 	ASSERT(mmapcnt != 0);
2137 
2138 	/*
2139 	 * If this is a shared object, add the base address to each address.
2140 	 * if this is an executable, use address as is.
2141 	 */
2142 	if (ehdr->e_type == ET_EXEC) {
2143 		base = 0;
2144 		FLAGS(lmp) |= FLG_RT_FIXED;
2145 	} else
2146 		base = addr;
2147 
2148 	/*
2149 	 * Fill in rest of the link map entries with information from the file's
2150 	 * dynamic structure.
2151 	 */
2152 	if (ld) {
2153 		uint_t		dynndx = 0;
2154 		Xword		pltpadsz = 0;
2155 		Rti_desc	*rti;
2156 
2157 		/* CSTYLED */
2158 		for ( ; ld->d_tag != DT_NULL; ++ld, dynndx++) {
2159 			switch ((Xword)ld->d_tag) {
2160 			case DT_SYMTAB:
2161 				SYMTAB(lmp) = (void *)(ld->d_un.d_ptr + base);
2162 				break;
2163 			case DT_SUNW_SYMTAB:
2164 				SUNWSYMTAB(lmp) =
2165 				    (void *)(ld->d_un.d_ptr + base);
2166 				break;
2167 			case DT_SUNW_SYMSZ:
2168 				SUNWSYMSZ(lmp) = ld->d_un.d_val;
2169 				break;
2170 			case DT_STRTAB:
2171 				STRTAB(lmp) = (void *)(ld->d_un.d_ptr + base);
2172 				break;
2173 			case DT_SYMENT:
2174 				SYMENT(lmp) = ld->d_un.d_val;
2175 				break;
2176 			case DT_FEATURE_1:
2177 				ld->d_un.d_val |= DTF_1_PARINIT;
2178 				if (ld->d_un.d_val & DTF_1_CONFEXP)
2179 					crle = 1;
2180 				break;
2181 			case DT_MOVESZ:
2182 				MOVESZ(lmp) = ld->d_un.d_val;
2183 				FLAGS(lmp) |= FLG_RT_MOVE;
2184 				break;
2185 			case DT_MOVEENT:
2186 				MOVEENT(lmp) = ld->d_un.d_val;
2187 				break;
2188 			case DT_MOVETAB:
2189 				MOVETAB(lmp) = (void *)(ld->d_un.d_ptr + base);
2190 				break;
2191 			case DT_REL:
2192 			case DT_RELA:
2193 				/*
2194 				 * At this time, ld.so. can only handle one
2195 				 * type of relocation per object.
2196 				 */
2197 				REL(lmp) = (void *)(ld->d_un.d_ptr + base);
2198 				break;
2199 			case DT_RELSZ:
2200 			case DT_RELASZ:
2201 				RELSZ(lmp) = ld->d_un.d_val;
2202 				break;
2203 			case DT_RELENT:
2204 			case DT_RELAENT:
2205 				RELENT(lmp) = ld->d_un.d_val;
2206 				break;
2207 			case DT_RELCOUNT:
2208 			case DT_RELACOUNT:
2209 				RELACOUNT(lmp) = (uint_t)ld->d_un.d_val;
2210 				break;
2211 			case DT_TEXTREL:
2212 				FLAGS1(lmp) |= FL1_RT_TEXTREL;
2213 				break;
2214 			case DT_HASH:
2215 				HASH(lmp) = (uint_t *)(ld->d_un.d_ptr + base);
2216 				break;
2217 			case DT_PLTGOT:
2218 				PLTGOT(lmp) = (uint_t *)(ld->d_un.d_ptr + base);
2219 				break;
2220 			case DT_PLTRELSZ:
2221 				PLTRELSZ(lmp) = ld->d_un.d_val;
2222 				break;
2223 			case DT_JMPREL:
2224 				JMPREL(lmp) = (void *)(ld->d_un.d_ptr + base);
2225 				break;
2226 			case DT_INIT:
2227 				if (ld->d_un.d_ptr != NULL)
2228 					INIT(lmp) =
2229 					    (void (*)())(ld->d_un.d_ptr + base);
2230 				break;
2231 			case DT_FINI:
2232 				if (ld->d_un.d_ptr != NULL)
2233 					FINI(lmp) =
2234 					    (void (*)())(ld->d_un.d_ptr + base);
2235 				break;
2236 			case DT_INIT_ARRAY:
2237 				INITARRAY(lmp) = (Addr *)(ld->d_un.d_ptr +
2238 				    base);
2239 				break;
2240 			case DT_INIT_ARRAYSZ:
2241 				INITARRAYSZ(lmp) = (uint_t)ld->d_un.d_val;
2242 				break;
2243 			case DT_FINI_ARRAY:
2244 				FINIARRAY(lmp) = (Addr *)(ld->d_un.d_ptr +
2245 				    base);
2246 				break;
2247 			case DT_FINI_ARRAYSZ:
2248 				FINIARRAYSZ(lmp) = (uint_t)ld->d_un.d_val;
2249 				break;
2250 			case DT_PREINIT_ARRAY:
2251 				PREINITARRAY(lmp) = (Addr *)(ld->d_un.d_ptr +
2252 				    base);
2253 				break;
2254 			case DT_PREINIT_ARRAYSZ:
2255 				PREINITARRAYSZ(lmp) = (uint_t)ld->d_un.d_val;
2256 				break;
2257 			case DT_RPATH:
2258 			case DT_RUNPATH:
2259 				rpath = ld->d_un.d_val;
2260 				break;
2261 			case DT_FILTER:
2262 				fltr = ld->d_un.d_val;
2263 				OBJFLTRNDX(lmp) = dynndx;
2264 				FLAGS1(lmp) |= FL1_RT_OBJSFLTR;
2265 				break;
2266 			case DT_AUXILIARY:
2267 				if (!(rtld_flags & RT_FL_NOAUXFLTR)) {
2268 					fltr = ld->d_un.d_val;
2269 					OBJFLTRNDX(lmp) = dynndx;
2270 				}
2271 				FLAGS1(lmp) |= FL1_RT_OBJAFLTR;
2272 				break;
2273 			case DT_SUNW_FILTER:
2274 				SYMSFLTRCNT(lmp)++;
2275 				FLAGS1(lmp) |= FL1_RT_SYMSFLTR;
2276 				break;
2277 			case DT_SUNW_AUXILIARY:
2278 				if (!(rtld_flags & RT_FL_NOAUXFLTR)) {
2279 					SYMAFLTRCNT(lmp)++;
2280 				}
2281 				FLAGS1(lmp) |= FL1_RT_SYMAFLTR;
2282 				break;
2283 			case DT_DEPAUDIT:
2284 				if (!(rtld_flags & RT_FL_NOAUDIT))
2285 					audit = ld->d_un.d_val;
2286 				break;
2287 			case DT_CONFIG:
2288 				cfile = ld->d_un.d_val;
2289 				break;
2290 			case DT_DEBUG:
2291 				/*
2292 				 * DT_DEBUG entries are only created in
2293 				 * dynamic objects that require an interpretor
2294 				 * (ie. all dynamic executables and some shared
2295 				 * objects), and provide for a hand-shake with
2296 				 * debuggers.  This entry is initialized to
2297 				 * zero by the link-editor.  If a debugger has
2298 				 * us and updated this entry set the debugger
2299 				 * flag, and finish initializing the debugging
2300 				 * structure (see setup() also).  Switch off any
2301 				 * configuration object use as most debuggers
2302 				 * can't handle fixed dynamic executables as
2303 				 * dependencies, and we can't handle requests
2304 				 * like object padding for alternative objects.
2305 				 */
2306 				if (ld->d_un.d_ptr)
2307 					rtld_flags |=
2308 					    (RT_FL_DEBUGGER | RT_FL_NOOBJALT);
2309 				ld->d_un.d_ptr = (Addr)&r_debug;
2310 				break;
2311 			case DT_VERNEED:
2312 				VERNEED(lmp) = (Verneed *)(ld->d_un.d_ptr +
2313 				    base);
2314 				break;
2315 			case DT_VERNEEDNUM:
2316 				/* LINTED */
2317 				VERNEEDNUM(lmp) = (int)ld->d_un.d_val;
2318 				break;
2319 			case DT_VERDEF:
2320 				VERDEF(lmp) = (Verdef *)(ld->d_un.d_ptr + base);
2321 				break;
2322 			case DT_VERDEFNUM:
2323 				/* LINTED */
2324 				VERDEFNUM(lmp) = (int)ld->d_un.d_val;
2325 				break;
2326 			case DT_VERSYM:
2327 				/*
2328 				 * The Solaris ld does not produce DT_VERSYM,
2329 				 * but the GNU ld does, in order to support
2330 				 * their style of versioning, which differs
2331 				 * from ours in some ways, while using the
2332 				 * same data structures. The presence of
2333 				 * DT_VERSYM therefore means that GNU
2334 				 * versioning rules apply to the given file.
2335 				 * If DT_VERSYM is not present, then Solaris
2336 				 * versioning rules apply.
2337 				 */
2338 				VERSYM(lmp) = (Versym *)(ld->d_un.d_ptr + base);
2339 				break;
2340 			case DT_BIND_NOW:
2341 				if ((rtld_flags2 & RT_FL2_BINDLAZY) == 0) {
2342 					MODE(lmp) |= RTLD_NOW;
2343 					MODE(lmp) &= ~RTLD_LAZY;
2344 				}
2345 				break;
2346 			case DT_FLAGS:
2347 				FLAGS2(lmp) |= FL2_RT_DTFLAGS;
2348 				if (ld->d_un.d_val & DF_SYMBOLIC)
2349 					FLAGS1(lmp) |= FL1_RT_SYMBOLIC;
2350 				if (ld->d_un.d_val & DF_TEXTREL)
2351 					FLAGS1(lmp) |= FL1_RT_TEXTREL;
2352 				if ((ld->d_un.d_val & DF_BIND_NOW) &&
2353 				    ((rtld_flags2 & RT_FL2_BINDLAZY) == 0)) {
2354 					MODE(lmp) |= RTLD_NOW;
2355 					MODE(lmp) &= ~RTLD_LAZY;
2356 				}
2357 				/*
2358 				 * Capture any static TLS use, and enforce that
2359 				 * this object be non-deletable.
2360 				 */
2361 				if (ld->d_un.d_val & DF_STATIC_TLS) {
2362 					FLAGS1(lmp) |= FL1_RT_TLSSTAT;
2363 					MODE(lmp) |= RTLD_NODELETE;
2364 				}
2365 				break;
2366 			case DT_FLAGS_1:
2367 				if (ld->d_un.d_val & DF_1_DISPRELPND)
2368 					FLAGS1(lmp) |= FL1_RT_DISPREL;
2369 				if (ld->d_un.d_val & DF_1_GROUP)
2370 					FLAGS(lmp) |=
2371 					    (FLG_RT_SETGROUP | FLG_RT_HANDLE);
2372 				if ((ld->d_un.d_val & DF_1_NOW) &&
2373 				    ((rtld_flags2 & RT_FL2_BINDLAZY) == 0)) {
2374 					MODE(lmp) |= RTLD_NOW;
2375 					MODE(lmp) &= ~RTLD_LAZY;
2376 				}
2377 				if (ld->d_un.d_val & DF_1_NODELETE)
2378 					MODE(lmp) |= RTLD_NODELETE;
2379 				if (ld->d_un.d_val & DF_1_INITFIRST)
2380 					FLAGS(lmp) |= FLG_RT_INITFRST;
2381 				if (ld->d_un.d_val & DF_1_NOOPEN)
2382 					FLAGS(lmp) |= FLG_RT_NOOPEN;
2383 				if (ld->d_un.d_val & DF_1_LOADFLTR)
2384 					FLAGS(lmp) |= FLG_RT_LOADFLTR;
2385 				if (ld->d_un.d_val & DF_1_NODUMP)
2386 					FLAGS(lmp) |= FLG_RT_NODUMP;
2387 				if (ld->d_un.d_val & DF_1_CONFALT)
2388 					crle = 1;
2389 				if (ld->d_un.d_val & DF_1_DIRECT)
2390 					FLAGS1(lmp) |= FL1_RT_DIRECT;
2391 				if (ld->d_un.d_val & DF_1_NODEFLIB)
2392 					FLAGS1(lmp) |= FL1_RT_NODEFLIB;
2393 				if (ld->d_un.d_val & DF_1_ENDFILTEE)
2394 					FLAGS1(lmp) |= FL1_RT_ENDFILTE;
2395 				if (ld->d_un.d_val & DF_1_TRANS)
2396 					FLAGS(lmp) |= FLG_RT_TRANS;
2397 #ifndef	EXPAND_RELATIVE
2398 				if (ld->d_un.d_val & DF_1_ORIGIN)
2399 					FLAGS1(lmp) |= FL1_RT_RELATIVE;
2400 #endif
2401 				/*
2402 				 * Global auditing is only meaningful when
2403 				 * specified by the initiating object of the
2404 				 * process - typically the dynamic executable.
2405 				 * If this is the initiaiting object, its link-
2406 				 * map will not yet have been added to the
2407 				 * link-map list, and consequently the link-map
2408 				 * list is empty.  (see setup()).
2409 				 */
2410 				if (ld->d_un.d_val & DF_1_GLOBAUDIT) {
2411 					if (lml_main.lm_head == 0)
2412 						FLAGS1(lmp) |= FL1_RT_GLOBAUD;
2413 					else
2414 						DBG_CALL(Dbg_audit_ignore(lmp));
2415 				}
2416 
2417 				/*
2418 				 * If this object identifies itself as an
2419 				 * interposer, but relocation processing has
2420 				 * already started, then demote it.  It's too
2421 				 * late to guarantee complete interposition.
2422 				 */
2423 				/* BEGIN CSTYLED */
2424 				if (ld->d_un.d_val &
2425 				    (DF_1_INTERPOSE | DF_1_SYMINTPOSE)) {
2426 				    if (lml->lm_flags & LML_FLG_STARTREL) {
2427 					DBG_CALL(Dbg_util_intoolate(lmp));
2428 					if (lml->lm_flags & LML_FLG_TRC_ENABLE)
2429 					    (void) printf(
2430 						MSG_INTL(MSG_LDD_REL_ERR2),
2431 						NAME(lmp));
2432 				    } else if (ld->d_un.d_val & DF_1_INTERPOSE)
2433 					FLAGS(lmp) |= FLG_RT_OBJINTPO;
2434 				    else
2435 					FLAGS(lmp) |= FLG_RT_SYMINTPO;
2436 				}
2437 				/* END CSTYLED */
2438 				break;
2439 			case DT_SYMINFO:
2440 				SYMINFO(lmp) = (Syminfo *)(ld->d_un.d_ptr +
2441 				    base);
2442 				break;
2443 			case DT_SYMINENT:
2444 				SYMINENT(lmp) = ld->d_un.d_val;
2445 				break;
2446 			case DT_PLTPAD:
2447 				PLTPAD(lmp) = (void *)(ld->d_un.d_ptr + base);
2448 				break;
2449 			case DT_PLTPADSZ:
2450 				pltpadsz = ld->d_un.d_val;
2451 				break;
2452 			case DT_SUNW_RTLDINF:
2453 				/*
2454 				 * Maintain a list of RTLDINFO structures.
2455 				 * Typically, libc is the only supplier, and
2456 				 * only one structure is provided.  However,
2457 				 * multiple suppliers and multiple structures
2458 				 * are supported.  For example, one structure
2459 				 * may provide thread_init, and another
2460 				 * structure may provide atexit reservations.
2461 				 */
2462 				if ((rti = alist_append(&lml->lm_rti, 0,
2463 				    sizeof (Rti_desc), AL_CNT_RTLDINFO)) == 0) {
2464 					remove_so(0, lmp);
2465 					return (0);
2466 				}
2467 				rti->rti_lmp = lmp;
2468 				rti->rti_info = (void *)(ld->d_un.d_ptr + base);
2469 				break;
2470 			case DT_SUNW_SORTENT:
2471 				SUNWSORTENT(lmp) = ld->d_un.d_val;
2472 				break;
2473 			case DT_SUNW_SYMSORT:
2474 				SUNWSYMSORT(lmp) =
2475 				    (void *)(ld->d_un.d_ptr + base);
2476 				break;
2477 			case DT_SUNW_SYMSORTSZ:
2478 				SUNWSYMSORTSZ(lmp) = ld->d_un.d_val;
2479 				break;
2480 			case DT_DEPRECATED_SPARC_REGISTER:
2481 			case M_DT_REGISTER:
2482 				FLAGS(lmp) |= FLG_RT_REGSYMS;
2483 				break;
2484 			}
2485 		}
2486 
2487 		if (PLTPAD(lmp)) {
2488 			if (pltpadsz == (Xword)0)
2489 				PLTPAD(lmp) = 0;
2490 			else
2491 				PLTPADEND(lmp) = (void *)((Addr)PLTPAD(lmp) +
2492 				    pltpadsz);
2493 		}
2494 
2495 		/*
2496 		 * Allocate a Dynamic Info structure.
2497 		 */
2498 		if ((DYNINFO(lmp) = calloc((size_t)dynndx,
2499 		    sizeof (Dyninfo))) == 0) {
2500 			remove_so(0, lmp);
2501 			return (0);
2502 		}
2503 		DYNINFOCNT(lmp) = dynndx;
2504 	}
2505 
2506 	/*
2507 	 * A dynsym contains only global functions. We want to have
2508 	 * a version of it that also includes local functions, so that
2509 	 * dladdr() will be able to report names for local functions
2510 	 * when used to generate a stack trace for a stripped file.
2511 	 * This version of the dynsym is provided via DT_SUNW_SYMTAB.
2512 	 *
2513 	 * In producing DT_SUNW_SYMTAB, ld uses a non-obvious trick
2514 	 * in order to avoid having to have two copies of the global
2515 	 * symbols held in DT_SYMTAB: The local symbols are placed in
2516 	 * a separate section than the globals in the dynsym, but the
2517 	 * linker conspires to put the data for these two sections adjacent
2518 	 * to each other. DT_SUNW_SYMTAB points at the top of the local
2519 	 * symbols, and DT_SUNW_SYMSZ is the combined length of both tables.
2520 	 *
2521 	 * If the two sections are not adjacent, then something went wrong
2522 	 * at link time. We use ASSERT to kill the process if this is
2523 	 * a debug build. In a production build, we will silently ignore
2524 	 * the presence of the .ldynsym and proceed. We can detect this
2525 	 * situation by checking to see that DT_SYMTAB lies in
2526 	 * the range given by DT_SUNW_SYMTAB/DT_SUNW_SYMSZ.
2527 	 */
2528 	if ((SUNWSYMTAB(lmp) != NULL) &&
2529 	    (((char *)SYMTAB(lmp) <= (char *)SUNWSYMTAB(lmp)) ||
2530 	    (((char *)SYMTAB(lmp) >=
2531 	    (SUNWSYMSZ(lmp) + (char *)SUNWSYMTAB(lmp)))))) {
2532 		ASSERT(0);
2533 		SUNWSYMTAB(lmp) = NULL;
2534 		SUNWSYMSZ(lmp) = 0;
2535 	}
2536 
2537 	/*
2538 	 * If configuration file use hasn't been disabled, and a configuration
2539 	 * file hasn't already been set via an environment variable, see if any
2540 	 * application specific configuration file is specified.  An LD_CONFIG
2541 	 * setting is used first, but if this image was generated via crle(1)
2542 	 * then a default configuration file is a fall-back.
2543 	 */
2544 	if ((!(rtld_flags & RT_FL_NOCFG)) && (config->c_name == 0)) {
2545 		if (cfile)
2546 			config->c_name = (const char *)(cfile +
2547 			    (char *)STRTAB(lmp));
2548 		else if (crle) {
2549 			rtld_flags |= RT_FL_CONFAPP;
2550 #ifndef	EXPAND_RELATIVE
2551 			FLAGS1(lmp) |= FL1_RT_RELATIVE;
2552 #endif
2553 		}
2554 	}
2555 
2556 	if (rpath)
2557 		RPATH(lmp) = (char *)(rpath + (char *)STRTAB(lmp));
2558 	if (fltr) {
2559 		/*
2560 		 * If this object is a global filter, duplicate the filtee
2561 		 * string name(s) so that REFNAME() is available in core files.
2562 		 * This cludge was useful for debuggers at one point, but only
2563 		 * when the filtee name was an individual full path.
2564 		 */
2565 		if ((REFNAME(lmp) = strdup(fltr + (char *)STRTAB(lmp))) == 0) {
2566 			remove_so(0, lmp);
2567 			return (0);
2568 		}
2569 	}
2570 
2571 	if (rtld_flags & RT_FL_RELATIVE)
2572 		FLAGS1(lmp) |= FL1_RT_RELATIVE;
2573 
2574 	/*
2575 	 * For Intel ABI compatibility.  It's possible that a JMPREL can be
2576 	 * specified without any other relocations (e.g. a dynamic executable
2577 	 * normally only contains .plt relocations).  If this is the case then
2578 	 * no REL, RELSZ or RELENT will have been created.  For us to be able
2579 	 * to traverse the .plt relocations under LD_BIND_NOW we need to know
2580 	 * the RELENT for these relocations.  Refer to elf_reloc() for more
2581 	 * details.
2582 	 */
2583 	if (!RELENT(lmp) && JMPREL(lmp))
2584 		RELENT(lmp) = sizeof (Rel);
2585 
2586 	/*
2587 	 * Establish any per-object auditing.  If we're establishing `main's
2588 	 * link-map its too early to go searching for audit objects so just
2589 	 * hold the object name for later (see setup()).
2590 	 */
2591 	if (audit) {
2592 		char	*cp = audit + (char *)STRTAB(lmp);
2593 
2594 		if (*cp) {
2595 			if (((AUDITORS(lmp) =
2596 			    calloc(1, sizeof (Audit_desc))) == 0) ||
2597 			    ((AUDITORS(lmp)->ad_name = strdup(cp)) == 0)) {
2598 				remove_so(0, lmp);
2599 				return (0);
2600 			}
2601 			if (lml_main.lm_head) {
2602 				if (audit_setup(lmp, AUDITORS(lmp), 0,
2603 				    in_nfavl) == 0) {
2604 					remove_so(0, lmp);
2605 					return (0);
2606 				}
2607 				FLAGS1(lmp) |= AUDITORS(lmp)->ad_flags;
2608 				lml->lm_flags |= LML_FLG_LOCAUDIT;
2609 			}
2610 		}
2611 	}
2612 
2613 	if ((CONDVAR(lmp) = rt_cond_create()) == 0) {
2614 		remove_so(0, lmp);
2615 		return (0);
2616 	}
2617 	if (oname && ((append_alias(lmp, oname, 0)) == 0)) {
2618 		remove_so(0, lmp);
2619 		return (0);
2620 	}
2621 
2622 	/*
2623 	 * Add the mapped object to the end of the link map list.
2624 	 */
2625 	lm_append(lml, lmco, lmp);
2626 	return (lmp);
2627 }
2628 
2629 /*
2630  * Assign hardware/software capabilities.
2631  */
2632 void
2633 cap_assign(Cap *cap, Rt_map *lmp)
2634 {
2635 	while (cap->c_tag != CA_SUNW_NULL) {
2636 		switch (cap->c_tag) {
2637 		case CA_SUNW_HW_1:
2638 			HWCAP(lmp) = cap->c_un.c_val;
2639 			break;
2640 		case CA_SUNW_SF_1:
2641 			SFCAP(lmp) = cap->c_un.c_val;
2642 		}
2643 		cap++;
2644 	}
2645 }
2646 
2647 /*
2648  * Map in an ELF object.
2649  * Takes an open file descriptor for the object to map and its pathname; returns
2650  * a pointer to a Rt_map structure for this object, or 0 on error.
2651  */
2652 static Rt_map *
2653 elf_map_so(Lm_list *lml, Aliste lmco, const char *pname, const char *oname,
2654     int fd, int *in_nfavl)
2655 {
2656 	int		i; 		/* general temporary */
2657 	Off		memsize = 0;	/* total memory size of pathname */
2658 	Off		mentry;		/* entry point */
2659 	Ehdr		*ehdr;		/* ELF header of ld.so */
2660 	Phdr		*phdr;		/* first Phdr in file */
2661 	Phdr		*phdr0;		/* Saved first Phdr in file */
2662 	Phdr		*pptr;		/* working Phdr */
2663 	Phdr		*fph = 0;	/* first loadable Phdr */
2664 	Phdr		*lph;		/* last loadable Phdr */
2665 	Phdr		*lfph = 0;	/* last loadable (filesz != 0) Phdr */
2666 	Phdr		*lmph = 0;	/* last loadable (memsz != 0) Phdr */
2667 	Phdr		*tlph = 0;	/* program header for PT_TLS */
2668 	Phdr		*unwindph = 0;	/* program header for PT_SUNW_UNWIND */
2669 	Cap		*cap = 0;	/* program header for SUNWCAP */
2670 	Dyn		*mld = 0;	/* DYNAMIC structure for pathname */
2671 	size_t		size;		/* size of elf and program headers */
2672 	caddr_t		faddr = 0;	/* mapping address of pathname */
2673 	Rt_map		*lmp;		/* link map created */
2674 	caddr_t		paddr;		/* start of padded image */
2675 	Off		plen;		/* size of image including padding */
2676 	Half		etype;
2677 	int		fixed;
2678 	Mmap		*mmaps;
2679 	uint_t		mmapcnt = 0;
2680 	Xword		align = 0;
2681 
2682 	/* LINTED */
2683 	ehdr = (Ehdr *)fmap->fm_maddr;
2684 
2685 	/*
2686 	 * If this a relocatable object then special processing is required.
2687 	 */
2688 	if ((etype = ehdr->e_type) == ET_REL)
2689 		return (elf_obj_file(lml, lmco, pname, fd));
2690 
2691 	/*
2692 	 * If this isn't a dynamic executable or shared object we can't process
2693 	 * it.  If this is a dynamic executable then all addresses are fixed.
2694 	 */
2695 	if (etype == ET_EXEC) {
2696 		fixed = 1;
2697 	} else if (etype == ET_DYN) {
2698 		fixed = 0;
2699 	} else {
2700 		Conv_inv_buf_t inv_buf;
2701 
2702 		eprintf(lml, ERR_ELF, MSG_INTL(MSG_GEN_BADTYPE), pname,
2703 		    conv_ehdr_type(etype, 0, &inv_buf));
2704 		return (0);
2705 	}
2706 
2707 	/*
2708 	 * If our original mapped page was not large enough to hold all the
2709 	 * program headers remap them.
2710 	 */
2711 	size = (size_t)((char *)ehdr->e_phoff +
2712 	    (ehdr->e_phnum * ehdr->e_phentsize));
2713 	if (size > fmap->fm_fsize) {
2714 		eprintf(lml, ERR_FATAL, MSG_INTL(MSG_GEN_CORTRUNC), pname);
2715 		return (0);
2716 	}
2717 	if (size > fmap->fm_msize) {
2718 		fmap_setup();
2719 		if ((fmap->fm_maddr = mmap(fmap->fm_maddr, size, PROT_READ,
2720 		    fmap->fm_mflags, fd, 0)) == MAP_FAILED) {
2721 			int	err = errno;
2722 			eprintf(lml, ERR_FATAL, MSG_INTL(MSG_SYS_MMAP), pname,
2723 			    strerror(err));
2724 			return (0);
2725 		}
2726 		fmap->fm_msize = size;
2727 		/* LINTED */
2728 		ehdr = (Ehdr *)fmap->fm_maddr;
2729 	}
2730 	/* LINTED */
2731 	phdr0 = phdr = (Phdr *)((char *)ehdr + ehdr->e_ehsize);
2732 
2733 	/*
2734 	 * Get entry point.
2735 	 */
2736 	mentry = ehdr->e_entry;
2737 
2738 	/*
2739 	 * Point at program headers and perform some basic validation.
2740 	 */
2741 	for (i = 0, pptr = phdr; i < (int)ehdr->e_phnum; i++,
2742 	    pptr = (Phdr *)((Off)pptr + ehdr->e_phentsize)) {
2743 		if (pptr->p_type == PT_LOAD) {
2744 
2745 			if (fph == 0) {
2746 				fph = pptr;
2747 			/* LINTED argument lph is initialized in first pass */
2748 			} else if (pptr->p_vaddr <= lph->p_vaddr) {
2749 				eprintf(lml, ERR_ELF,
2750 				    MSG_INTL(MSG_GEN_INVPRGHDR), pname);
2751 				return (0);
2752 			}
2753 
2754 			lph = pptr;
2755 
2756 			if (pptr->p_memsz)
2757 				lmph = pptr;
2758 			if (pptr->p_filesz)
2759 				lfph = pptr;
2760 			if (pptr->p_align > align)
2761 				align = pptr->p_align;
2762 
2763 		} else if (pptr->p_type == PT_DYNAMIC) {
2764 			mld = (Dyn *)(pptr->p_vaddr);
2765 		} else if ((pptr->p_type == PT_TLS) && pptr->p_memsz) {
2766 			tlph = pptr;
2767 		} else if (pptr->p_type == PT_SUNWCAP) {
2768 			cap = (Cap *)(pptr->p_vaddr);
2769 		} else if (pptr->p_type == PT_SUNW_UNWIND) {
2770 			unwindph = pptr;
2771 		}
2772 	}
2773 
2774 #if defined(MAP_ALIGN)
2775 	/*
2776 	 * Make sure the maximum page alignment is a power of 2 >= the default
2777 	 * segment alignment, for use with MAP_ALIGN.
2778 	 */
2779 	align = S_ROUND(align, M_SEGM_ALIGN);
2780 #endif
2781 
2782 	/*
2783 	 * We'd better have at least one loadable segment, together with some
2784 	 * specified file and memory size.
2785 	 */
2786 	if ((fph == 0) || (lmph == 0) || (lfph == 0)) {
2787 		eprintf(lml, ERR_ELF, MSG_INTL(MSG_GEN_NOLOADSEG), pname);
2788 		return (0);
2789 	}
2790 
2791 	/*
2792 	 * Check that the files size accounts for the loadable sections
2793 	 * we're going to map in (failure to do this may cause spurious
2794 	 * bus errors if we're given a truncated file).
2795 	 */
2796 	if (fmap->fm_fsize < ((size_t)lfph->p_offset + lfph->p_filesz)) {
2797 		eprintf(lml, ERR_FATAL, MSG_INTL(MSG_GEN_CORTRUNC), pname);
2798 		return (0);
2799 	}
2800 
2801 	/*
2802 	 * Memsize must be page rounded so that if we add object padding
2803 	 * at the end it will start at the beginning of a page.
2804 	 */
2805 	plen = memsize = M_PROUND((lmph->p_vaddr + lmph->p_memsz) -
2806 	    M_PTRUNC((ulong_t)fph->p_vaddr));
2807 
2808 	/*
2809 	 * Determine if an existing mapping is acceptable.
2810 	 */
2811 	if (interp && (lml->lm_flags & LML_FLG_BASELM) &&
2812 	    (strcmp(pname, interp->i_name) == 0)) {
2813 		/*
2814 		 * If this is the interpreter then it has already been mapped
2815 		 * and we have the address so don't map it again.  Note that
2816 		 * the common occurrence of a reference to the interpretor
2817 		 * (libdl -> ld.so.1) will have been caught during filter
2818 		 * initialization (see elf_lookup_filtee()).  However, some
2819 		 * ELF implementations are known to record libc.so.1 as the
2820 		 * interpretor, and thus this test catches this behavior.
2821 		 */
2822 		paddr = faddr = interp->i_faddr;
2823 
2824 	} else if ((fixed == 0) && (r_debug.rtd_objpad == 0) &&
2825 	    (memsize <= fmap->fm_msize) && ((fph->p_flags & PF_W) == 0) &&
2826 	    (fph == lph) && (fph->p_filesz == fph->p_memsz) &&
2827 	    (((Xword)fmap->fm_maddr % align) == 0)) {
2828 		size_t	rsize;
2829 
2830 		/*
2831 		 * If the file contains a single segment, and the mapping
2832 		 * required has already been established from the initial fmap
2833 		 * mapping, then we don't need to do anything more.  Reset the
2834 		 * fmap address so that any later files start a new fmap.  This
2835 		 * is really an optimization for filters, such as libdl.so,
2836 		 * libthread, etc. that are constructed to be a single text
2837 		 * segment.
2838 		 */
2839 		paddr = faddr = fmap->fm_maddr;
2840 
2841 		/*
2842 		 * Free any unused mapping by assigning the fmap buffer to the
2843 		 * unused region.  fmap_setup() will unmap this area and
2844 		 * establish defaults for future mappings.
2845 		 */
2846 		rsize = M_PROUND(fph->p_filesz);
2847 		fmap->fm_maddr += rsize;
2848 		fmap->fm_msize -= rsize;
2849 		fmap_setup();
2850 	}
2851 
2852 	/*
2853 	 * Allocate a mapping array to retain mapped segment information.
2854 	 */
2855 	if ((mmaps = calloc(ehdr->e_phnum, sizeof (Mmap))) == 0)
2856 		return (0);
2857 
2858 	/*
2859 	 * If we're reusing an existing mapping determine the objects etext
2860 	 * address.  Otherwise map the file (which will calculate the etext
2861 	 * address as part of the mapping process).
2862 	 */
2863 	if (faddr) {
2864 		caddr_t	base;
2865 
2866 		if (fixed)
2867 			base = 0;
2868 		else
2869 			base = faddr;
2870 
2871 		/* LINTED */
2872 		phdr0 = phdr = (Phdr *)((char *)faddr + ehdr->e_ehsize);
2873 
2874 		for (i = 0, pptr = phdr; i < (int)ehdr->e_phnum; i++,
2875 		    pptr = (Phdr *)((Off)pptr + ehdr->e_phentsize)) {
2876 			if (pptr->p_type != PT_LOAD)
2877 				continue;
2878 
2879 			mmaps[mmapcnt].m_vaddr = (pptr->p_vaddr + base);
2880 			mmaps[mmapcnt].m_msize = pptr->p_memsz;
2881 			mmaps[mmapcnt].m_fsize = pptr->p_filesz;
2882 			mmaps[mmapcnt].m_perm = (PROT_READ | PROT_EXEC);
2883 			mmapcnt++;
2884 
2885 			if (!(pptr->p_flags & PF_W)) {
2886 				fmap->fm_etext = (ulong_t)pptr->p_vaddr +
2887 				    (ulong_t)pptr->p_memsz +
2888 				    (ulong_t)(fixed ? 0 : faddr);
2889 			}
2890 		}
2891 	} else {
2892 		/*
2893 		 * Map the file.
2894 		 */
2895 		if (!(faddr = elf_map_it(lml, pname, memsize, ehdr, fph, lph,
2896 		    &phdr, &paddr, &plen, fixed, fd, align, mmaps, &mmapcnt)))
2897 			return (0);
2898 	}
2899 
2900 	/*
2901 	 * Calculate absolute base addresses and entry points.
2902 	 */
2903 	if (!fixed) {
2904 		if (mld)
2905 			/* LINTED */
2906 			mld = (Dyn *)((Off)mld + faddr);
2907 		if (cap)
2908 			/* LINTED */
2909 			cap = (Cap *)((Off)cap + faddr);
2910 		mentry += (Off)faddr;
2911 	}
2912 
2913 	/*
2914 	 * Create new link map structure for newly mapped shared object.
2915 	 */
2916 	if (!(lmp = elf_new_lm(lml, pname, oname, mld, (ulong_t)faddr,
2917 	    fmap->fm_etext, lmco, memsize, mentry, (ulong_t)paddr, plen, mmaps,
2918 	    mmapcnt, in_nfavl))) {
2919 		(void) munmap((caddr_t)faddr, memsize);
2920 		return (0);
2921 	}
2922 
2923 	/*
2924 	 * Start the system loading in the ELF information we'll be processing.
2925 	 */
2926 	if (REL(lmp)) {
2927 		(void) madvise((void *)ADDR(lmp), (uintptr_t)REL(lmp) +
2928 		    (uintptr_t)RELSZ(lmp) - (uintptr_t)ADDR(lmp),
2929 		    MADV_WILLNEED);
2930 	}
2931 
2932 	/*
2933 	 * If this shared object contains any special segments, record them.
2934 	 */
2935 	if (tlph && (tls_assign(lml, lmp, (phdr + (tlph - phdr0))) == 0)) {
2936 		remove_so(lml, lmp);
2937 		return (0);
2938 	}
2939 
2940 	if (unwindph)
2941 		PTUNWIND(lmp) = phdr + (unwindph - phdr0);
2942 
2943 	if (cap)
2944 		cap_assign(cap, lmp);
2945 
2946 	return (lmp);
2947 }
2948 
2949 /*
2950  * Function to correct protection settings.  Segments are all mapped initially
2951  * with permissions as given in the segment header.  We need to turn on write
2952  * permissions on a text segment if there are any relocations against that
2953  * segment, and them turn write permission back off again before returning
2954  * control to the user.  This function turns the permission on or off depending
2955  * on the value of the argument.
2956  */
2957 int
2958 elf_set_prot(Rt_map *lmp, int permission)
2959 {
2960 	Mmap	*mmaps;
2961 
2962 	/*
2963 	 * If this is an allocated image (ie. a relocatable object) we can't
2964 	 * mprotect() anything.
2965 	 */
2966 	if (FLAGS(lmp) & FLG_RT_IMGALLOC)
2967 		return (1);
2968 
2969 	DBG_CALL(Dbg_file_prot(lmp, permission));
2970 
2971 	for (mmaps = MMAPS(lmp); mmaps->m_vaddr; mmaps++) {
2972 		if (mmaps->m_perm & PROT_WRITE)
2973 			continue;
2974 
2975 		if (mprotect(mmaps->m_vaddr, mmaps->m_msize,
2976 		    (mmaps->m_perm | permission)) == -1) {
2977 			int	err = errno;
2978 			eprintf(LIST(lmp), ERR_FATAL, MSG_INTL(MSG_SYS_MPROT),
2979 			    NAME(lmp), strerror(err));
2980 			return (0);
2981 		}
2982 	}
2983 	return (1);
2984 }
2985 
2986 /*
2987  * Build full pathname of shared object from given directory name and filename.
2988  */
2989 static char *
2990 elf_get_so(const char *dir, const char *file)
2991 {
2992 	static char	pname[PATH_MAX];
2993 
2994 	(void) snprintf(pname, PATH_MAX, MSG_ORIG(MSG_FMT_PATH), dir, file);
2995 	return (pname);
2996 }
2997 
2998 /*
2999  * The copy relocation is recorded in a copy structure which will be applied
3000  * after all other relocations are carried out.  This provides for copying data
3001  * that must be relocated itself (ie. pointers in shared objects).  This
3002  * structure also provides a means of binding RTLD_GROUP dependencies to any
3003  * copy relocations that have been taken from any group members.
3004  *
3005  * If the size of the .bss area available for the copy information is not the
3006  * same as the source of the data inform the user if we're under ldd(1) control
3007  * (this checking was only established in 5.3, so by only issuing an error via
3008  * ldd(1) we maintain the standard set by previous releases).
3009  */
3010 int
3011 elf_copy_reloc(char *name, Sym *rsym, Rt_map *rlmp, void *radd, Sym *dsym,
3012     Rt_map *dlmp, const void *dadd)
3013 {
3014 	Rel_copy	rc;
3015 	Lm_list		*lml = LIST(rlmp);
3016 
3017 	rc.r_name = name;
3018 	rc.r_rsym = rsym;		/* the new reference symbol and its */
3019 	rc.r_rlmp = rlmp;		/*	associated link-map */
3020 	rc.r_dlmp = dlmp;		/* the defining link-map */
3021 	rc.r_dsym = dsym;		/* the original definition */
3022 	rc.r_radd = radd;
3023 	rc.r_dadd = dadd;
3024 
3025 	if (rsym->st_size > dsym->st_size)
3026 		rc.r_size = (size_t)dsym->st_size;
3027 	else
3028 		rc.r_size = (size_t)rsym->st_size;
3029 
3030 	if (alist_append(&COPY_R(dlmp), &rc, sizeof (Rel_copy),
3031 	    AL_CNT_COPYREL) == 0) {
3032 		if (!(lml->lm_flags & LML_FLG_TRC_WARN))
3033 			return (0);
3034 		else
3035 			return (1);
3036 	}
3037 	if (!(FLAGS1(dlmp) & FL1_RT_COPYTOOK)) {
3038 		if (aplist_append(&COPY_S(rlmp), dlmp,
3039 		    AL_CNT_COPYREL) == NULL) {
3040 			if (!(lml->lm_flags & LML_FLG_TRC_WARN))
3041 				return (0);
3042 			else
3043 				return (1);
3044 		}
3045 		FLAGS1(dlmp) |= FL1_RT_COPYTOOK;
3046 	}
3047 
3048 	/*
3049 	 * If we are tracing (ldd), warn the user if
3050 	 *	1) the size from the reference symbol differs from the
3051 	 *	   copy definition. We can only copy as much data as the
3052 	 *	   reference (dynamic executables) entry allows.
3053 	 *	2) the copy definition has STV_PROTECTED visibility.
3054 	 */
3055 	if (lml->lm_flags & LML_FLG_TRC_WARN) {
3056 		if (rsym->st_size != dsym->st_size) {
3057 			(void) printf(MSG_INTL(MSG_LDD_CPY_SIZDIF),
3058 			    _conv_reloc_type(M_R_COPY), demangle(name),
3059 			    NAME(rlmp), EC_XWORD(rsym->st_size),
3060 			    NAME(dlmp), EC_XWORD(dsym->st_size));
3061 			if (rsym->st_size > dsym->st_size)
3062 				(void) printf(MSG_INTL(MSG_LDD_CPY_INSDATA),
3063 				    NAME(dlmp));
3064 			else
3065 				(void) printf(MSG_INTL(MSG_LDD_CPY_DATRUNC),
3066 				    NAME(rlmp));
3067 		}
3068 
3069 		if (ELF_ST_VISIBILITY(dsym->st_other) == STV_PROTECTED) {
3070 			(void) printf(MSG_INTL(MSG_LDD_CPY_PROT),
3071 			    _conv_reloc_type(M_R_COPY), demangle(name),
3072 			    NAME(dlmp));
3073 		}
3074 	}
3075 
3076 	DBG_CALL(Dbg_reloc_apply_val(lml, ELF_DBG_RTLD, (Xword)radd,
3077 	    (Xword)rc.r_size));
3078 	return (1);
3079 }
3080 
3081 /*
3082  * Determine the symbol location of an address within a link-map.  Look for
3083  * the nearest symbol (whose value is less than or equal to the required
3084  * address).  This is the object specific part of dladdr().
3085  */
3086 static void
3087 elf_dladdr(ulong_t addr, Rt_map *lmp, Dl_info *dlip, void **info, int flags)
3088 {
3089 	ulong_t		ndx, cnt, base, _value;
3090 	Sym		*sym, *_sym = NULL;
3091 	const char	*str;
3092 	int		_flags;
3093 	uint_t		*dynaddr_ndx;
3094 	uint_t		dynaddr_n = 0;
3095 	ulong_t		value;
3096 
3097 	/*
3098 	 * If SUNWSYMTAB() is non-NULL, then it sees a special version of
3099 	 * the dynsym that starts with any local function symbols that exist in
3100 	 * the library and then moves to the data held in SYMTAB(). In this
3101 	 * case, SUNWSYMSZ tells us how long the symbol table is. The
3102 	 * availability of local function symbols will enhance the results
3103 	 * we can provide.
3104 	 *
3105 	 * If SUNWSYMTAB() is non-NULL, then there might also be a
3106 	 * SUNWSYMSORT() vector associated with it. SUNWSYMSORT() contains
3107 	 * an array of indices into SUNWSYMTAB, sorted by increasing
3108 	 * address. We can use this to do an O(log N) search instead of a
3109 	 * brute force search.
3110 	 *
3111 	 * If SUNWSYMTAB() is NULL, then SYMTAB() references a dynsym that
3112 	 * contains only global symbols. In that case, the length of
3113 	 * the symbol table comes from the nchain field of the related
3114 	 * symbol lookup hash table.
3115 	 */
3116 	str = STRTAB(lmp);
3117 	if (SUNWSYMSZ(lmp) == NULL) {
3118 		sym = SYMTAB(lmp);
3119 		/*
3120 		 * If we don't have a .hash table there are no symbols
3121 		 * to look at.
3122 		 */
3123 		if (HASH(lmp) == 0)
3124 			return;
3125 		cnt = HASH(lmp)[1];
3126 	} else {
3127 		sym = SUNWSYMTAB(lmp);
3128 		cnt = SUNWSYMSZ(lmp) / SYMENT(lmp);
3129 		dynaddr_ndx = SUNWSYMSORT(lmp);
3130 		if (dynaddr_ndx != NULL)
3131 			dynaddr_n = SUNWSYMSORTSZ(lmp) / SUNWSORTENT(lmp);
3132 	}
3133 
3134 	if (FLAGS(lmp) & FLG_RT_FIXED)
3135 		base = 0;
3136 	else
3137 		base = ADDR(lmp);
3138 
3139 	if (dynaddr_n > 0) {		/* Binary search */
3140 		long	low = 0, low_bnd;
3141 		long	high = dynaddr_n - 1, high_bnd;
3142 		long	mid;
3143 		Sym	*mid_sym;
3144 
3145 		/*
3146 		 * Note that SUNWSYMSORT only contains symbols types that
3147 		 * supply memory addresses, so there's no need to check and
3148 		 * filter out any other types.
3149 		 */
3150 		low_bnd = low;
3151 		high_bnd = high;
3152 		while (low <= high) {
3153 			mid = (low + high) / 2;
3154 			mid_sym = &sym[dynaddr_ndx[mid]];
3155 			value = mid_sym->st_value + base;
3156 			if (addr < value) {
3157 				if ((sym[dynaddr_ndx[high]].st_value + base) >=
3158 				    addr)
3159 					high_bnd = high;
3160 				high = mid - 1;
3161 			} else if (addr > value) {
3162 				if ((sym[dynaddr_ndx[low]].st_value + base) <=
3163 				    addr)
3164 					low_bnd = low;
3165 				low = mid + 1;
3166 			} else {
3167 				_sym = mid_sym;
3168 				_value = value;
3169 				break;
3170 			}
3171 		}
3172 		/*
3173 		 * If the above didn't find it exactly, then we must
3174 		 * return the closest symbol with a value that doesn't
3175 		 * exceed the one we are looking for. If that symbol exists,
3176 		 * it will lie in the range bounded by low_bnd and
3177 		 * high_bnd. This is a linear search, but a short one.
3178 		 */
3179 		if (_sym == NULL) {
3180 			for (mid = low_bnd; mid <= high_bnd; mid++) {
3181 				mid_sym = &sym[dynaddr_ndx[mid]];
3182 				value = mid_sym->st_value + base;
3183 				if (addr >= value) {
3184 					_sym = mid_sym;
3185 					_value = value;
3186 				} else {
3187 					break;
3188 				}
3189 			}
3190 		}
3191 	} else {			/* Linear search */
3192 		for (_value = 0, sym++, ndx = 1; ndx < cnt; ndx++, sym++) {
3193 			/*
3194 			 * Skip expected symbol types that are not functions
3195 			 * or data:
3196 			 *	- A symbol table starts with an undefined symbol
3197 			 *		in slot 0. If we are using SUNWSYMTAB(),
3198 			 *		there will be a second undefined symbol
3199 			 *		right before the globals.
3200 			 *	- The local part of SUNWSYMTAB() contains a
3201 			 *		series of function symbols. Each section
3202 			 *		starts with an initial STT_FILE symbol.
3203 			 */
3204 			if ((sym->st_shndx == SHN_UNDEF) ||
3205 			    (ELF_ST_TYPE(sym->st_info) == STT_FILE))
3206 				continue;
3207 
3208 			value = sym->st_value + base;
3209 			if (value > addr)
3210 				continue;
3211 			if (value < _value)
3212 				continue;
3213 
3214 			_sym = sym;
3215 			_value = value;
3216 
3217 			/*
3218 			 * Note, because we accept local and global symbols
3219 			 * we could find a section symbol that matches the
3220 			 * associated address, which means that the symbol
3221 			 * name will be null.  In this case continue the
3222 			 * search in case we can find a global symbol of
3223 			 * the same value.
3224 			 */
3225 			if ((value == addr) &&
3226 			    (ELF_ST_TYPE(sym->st_info) != STT_SECTION))
3227 				break;
3228 		}
3229 	}
3230 
3231 	_flags = flags & RTLD_DL_MASK;
3232 	if (_sym) {
3233 		if (_flags == RTLD_DL_SYMENT)
3234 			*info = (void *)_sym;
3235 		else if (_flags == RTLD_DL_LINKMAP)
3236 			*info = (void *)lmp;
3237 
3238 		dlip->dli_sname = str + _sym->st_name;
3239 		dlip->dli_saddr = (void *)_value;
3240 	} else {
3241 		/*
3242 		 * addr lies between the beginning of the mapped segment and
3243 		 * the first global symbol. We have no symbol to return
3244 		 * and the caller requires one. We use _START_, the base
3245 		 * address of the mapping.
3246 		 */
3247 
3248 		if (_flags == RTLD_DL_SYMENT) {
3249 			/*
3250 			 * An actual symbol struct is needed, so we
3251 			 * construct one for _START_. To do this in a
3252 			 * fully accurate way requires a different symbol
3253 			 * for each mapped segment. This requires the
3254 			 * use of dynamic memory and a mutex. That's too much
3255 			 * plumbing for a fringe case of limited importance.
3256 			 *
3257 			 * Fortunately, we can simplify:
3258 			 *    - Only the st_size and st_info fields are useful
3259 			 *	outside of the linker internals. The others
3260 			 *	reference things that outside code cannot see,
3261 			 *	and can be set to 0.
3262 			 *    - It's just a label and there is no size
3263 			 *	to report. So, the size should be 0.
3264 			 * This means that only st_info needs a non-zero
3265 			 * (constant) value. A static struct will suffice.
3266 			 * It must be const (readonly) so the caller can't
3267 			 * change its meaning for subsequent callers.
3268 			 */
3269 			static const Sym fsym = { 0, 0, 0,
3270 				ELF_ST_INFO(STB_LOCAL, STT_OBJECT) };
3271 			*info = (void *) &fsym;
3272 		}
3273 
3274 		dlip->dli_sname = MSG_ORIG(MSG_SYM_START);
3275 		dlip->dli_saddr = (void *) ADDR(lmp);
3276 	}
3277 }
3278 
3279 static void
3280 elf_lazy_cleanup(APlist *alp)
3281 {
3282 	Rt_map	*lmp;
3283 	Aliste	idx;
3284 
3285 	/*
3286 	 * Cleanup any link-maps added to this dynamic list and free it.
3287 	 */
3288 	for (APLIST_TRAVERSE(alp, idx, lmp))
3289 		FLAGS(lmp) &= ~FLG_RT_TMPLIST;
3290 	free(alp);
3291 }
3292 
3293 /*
3294  * This routine is called as a last fall-back to search for a symbol from a
3295  * standard relocation.  To maintain lazy loadings goal of reducing the number
3296  * of objects mapped, any symbol search is first carried out using the objects
3297  * that already exist in the process (either on a link-map list or handle).
3298  * If a symbol can't be found, and lazy dependencies are still pending, this
3299  * routine loads the dependencies in an attempt to locate the symbol.
3300  *
3301  * Only new objects are inspected as we will have already inspected presently
3302  * loaded objects before calling this routine.  However, a new object may not
3303  * be new - although the di_lmp might be zero, the object may have been mapped
3304  * as someone elses dependency.  Thus there's a possibility of some symbol
3305  * search duplication.
3306  */
3307 Sym *
3308 elf_lazy_find_sym(Slookup *slp, Rt_map **_lmp, uint_t *binfo, int *in_nfavl)
3309 {
3310 	Sym		*sym = 0;
3311 	APlist		*alist = NULL;
3312 	Aliste		idx;
3313 	Rt_map		*lmp1, *lmp = slp->sl_imap;
3314 	const char	*name = slp->sl_name;
3315 
3316 	/*
3317 	 * Generate a local list of new objects to process.  This list can grow
3318 	 * as each object supplies its own lazy dependencies.
3319 	 */
3320 	if (aplist_append(&alist, lmp, AL_CNT_LAZYFIND) == NULL)
3321 		return (NULL);
3322 	FLAGS(lmp) |= FLG_RT_TMPLIST;
3323 
3324 	for (APLIST_TRAVERSE(alist, idx, lmp1)) {
3325 		uint_t	cnt = 0;
3326 		Slookup	sl = *slp;
3327 		Dyninfo	*dip, *pdip;
3328 
3329 		/*
3330 		 * Discard any relocation index from further symbol searches.
3331 		 * This index will have already been used to trigger any
3332 		 * necessary lazy-loads, and it might be because one of these
3333 		 * lazy loads have failed that we're here performing this
3334 		 * fallback.  By removing the relocation index we don't try
3335 		 * and perform the same failed lazy loading activity again.
3336 		 */
3337 		sl.sl_rsymndx = 0;
3338 
3339 		/*
3340 		 * Loop through the lazy DT_NEEDED entries examining each object
3341 		 * for the required symbol.  If the symbol is not found, the
3342 		 * object is in turn added to the local alist, so that the
3343 		 * objects lazy DT_NEEDED entries can be examined.
3344 		 */
3345 		lmp = lmp1;
3346 		for (dip = DYNINFO(lmp), pdip = NULL; cnt < DYNINFOCNT(lmp);
3347 		    cnt++, pdip = dip++) {
3348 			Rt_map *nlmp;
3349 
3350 			if (((dip->di_flags & FLG_DI_LAZY) == 0) ||
3351 			    dip->di_info)
3352 				continue;
3353 
3354 			/*
3355 			 * If this object has already failed to lazy load, and
3356 			 * we're still processing the same runtime linker
3357 			 * operation that produced the failure, don't bother
3358 			 * to try and load the object again.
3359 			 */
3360 			if ((dip->di_flags & FLG_DI_LAZYFAIL) && pdip &&
3361 			    (pdip->di_flags & FLG_DI_POSFLAG1)) {
3362 				if (pdip->di_info == (void *)ld_entry_cnt)
3363 					continue;
3364 
3365 				dip->di_flags &= ~FLG_DI_LAZYFAIL;
3366 				pdip->di_info = NULL;
3367 			}
3368 
3369 			/*
3370 			 * Try loading this lazy dependency.  If the object
3371 			 * can't be loaded, consider this non-fatal and continue
3372 			 * the search.  Lazy loaded dependencies need not exist
3373 			 * and their loading should only turn out to be fatal
3374 			 * if they are required to satisfy a relocation.
3375 			 *
3376 			 * If the file is already loaded and relocated we must
3377 			 * still inspect it for symbols, even though it might
3378 			 * have already been searched.  This lazy load operation
3379 			 * might have promoted the permissions of the object,
3380 			 * and thus made the object applicable for this symbol
3381 			 * search, whereas before the object might have been
3382 			 * skipped.
3383 			 */
3384 			if ((nlmp = elf_lazy_load(lmp, &sl, cnt,
3385 			    name, in_nfavl)) == 0)
3386 				continue;
3387 
3388 			/*
3389 			 * If this object isn't yet a part of the dynamic list
3390 			 * then inspect it for the symbol.  If the symbol isn't
3391 			 * found add the object to the dynamic list so that we
3392 			 * can inspect its dependencies.
3393 			 */
3394 			if (FLAGS(nlmp) & FLG_RT_TMPLIST)
3395 				continue;
3396 
3397 			sl.sl_imap = nlmp;
3398 			if (sym = LM_LOOKUP_SYM(sl.sl_cmap)(&sl, _lmp,
3399 			    binfo, in_nfavl))
3400 				break;
3401 
3402 			/*
3403 			 * Some dlsym() operations are already traversing a
3404 			 * link-map (dlopen(0)), and thus there's no need to
3405 			 * build our own dynamic dependency list.
3406 			 */
3407 			if ((sl.sl_flags & LKUP_NODESCENT) == 0) {
3408 				if (aplist_append(&alist, nlmp,
3409 				    AL_CNT_LAZYFIND) == 0) {
3410 					elf_lazy_cleanup(alist);
3411 					return (0);
3412 				}
3413 				FLAGS(nlmp) |= FLG_RT_TMPLIST;
3414 			}
3415 		}
3416 		if (sym)
3417 			break;
3418 	}
3419 
3420 	elf_lazy_cleanup(alist);
3421 	return (sym);
3422 }
3423 
3424 /*
3425  * Warning message for bad r_offset.
3426  */
3427 void
3428 elf_reloc_bad(Rt_map *lmp, void *rel, uchar_t rtype, ulong_t roffset,
3429     ulong_t rsymndx)
3430 {
3431 	const char	*name = (char *)0;
3432 	Lm_list		*lml = LIST(lmp);
3433 	int		trace;
3434 
3435 	if ((lml->lm_flags & LML_FLG_TRC_ENABLE) &&
3436 	    (((rtld_flags & RT_FL_SILENCERR) == 0) ||
3437 	    (lml->lm_flags & LML_FLG_TRC_VERBOSE)))
3438 		trace = 1;
3439 	else
3440 		trace = 0;
3441 
3442 	if ((trace == 0) && (DBG_ENABLED == 0))
3443 		return;
3444 
3445 	if (rsymndx) {
3446 		Sym	*symref = (Sym *)((ulong_t)SYMTAB(lmp) +
3447 		    (rsymndx * SYMENT(lmp)));
3448 
3449 		if (ELF_ST_BIND(symref->st_info) != STB_LOCAL)
3450 			name = (char *)(STRTAB(lmp) + symref->st_name);
3451 	}
3452 
3453 	if (name == 0)
3454 		name = MSG_ORIG(MSG_STR_EMPTY);
3455 
3456 	if (trace) {
3457 		const char *rstr;
3458 
3459 		rstr = _conv_reloc_type((uint_t)rtype);
3460 		(void) printf(MSG_INTL(MSG_LDD_REL_ERR1), rstr, name,
3461 		    EC_ADDR(roffset));
3462 		return;
3463 	}
3464 
3465 	Dbg_reloc_error(lml, ELF_DBG_RTLD, M_MACH, M_REL_SHT_TYPE, rel, name);
3466 }
3467 
3468 /*
3469  * Resolve a static TLS relocation.
3470  */
3471 long
3472 elf_static_tls(Rt_map *lmp, Sym *sym, void *rel, uchar_t rtype, char *name,
3473     ulong_t roffset, long value)
3474 {
3475 	Lm_list	*lml = LIST(lmp);
3476 
3477 	/*
3478 	 * Relocations against a static TLS block have limited support once
3479 	 * process initialization has completed.  Any error condition should be
3480 	 * discovered by testing for DF_STATIC_TLS as part of loading an object,
3481 	 * however individual relocations are tested in case the dynamic flag
3482 	 * had not been set when this object was built.
3483 	 */
3484 	if (PTTLS(lmp) == 0) {
3485 		DBG_CALL(Dbg_reloc_in(lml, ELF_DBG_RTLD, M_MACH,
3486 		    M_REL_SHT_TYPE, rel, NULL, name));
3487 		eprintf(lml, ERR_FATAL, MSG_INTL(MSG_REL_BADTLS),
3488 		    _conv_reloc_type((uint_t)rtype), NAME(lmp),
3489 		    name ? demangle(name) : MSG_INTL(MSG_STR_UNKNOWN));
3490 		return (0);
3491 	}
3492 
3493 	/*
3494 	 * If no static TLS has been set aside for this object, determine if
3495 	 * any can be obtained.  Enforce that any object using static TLS is
3496 	 * non-deletable.
3497 	 */
3498 	if (TLSSTATOFF(lmp) == 0) {
3499 		FLAGS1(lmp) |= FL1_RT_TLSSTAT;
3500 		MODE(lmp) |= RTLD_NODELETE;
3501 
3502 		if (tls_assign(lml, lmp, PTTLS(lmp)) == 0) {
3503 			DBG_CALL(Dbg_reloc_in(lml, ELF_DBG_RTLD, M_MACH,
3504 			    M_REL_SHT_TYPE, rel, NULL, name));
3505 			eprintf(lml, ERR_FATAL, MSG_INTL(MSG_REL_BADTLS),
3506 			    _conv_reloc_type((uint_t)rtype), NAME(lmp),
3507 			    name ? demangle(name) : MSG_INTL(MSG_STR_UNKNOWN));
3508 			return (0);
3509 		}
3510 	}
3511 
3512 	/*
3513 	 * Typically, a static TLS offset is maintained as a symbols value.
3514 	 * For local symbols that are not apart of the dynamic symbol table,
3515 	 * the TLS relocation points to a section symbol, and the static TLS
3516 	 * offset was deposited in the associated GOT table.  Make sure the GOT
3517 	 * is cleared, so that the value isn't reused in do_reloc().
3518 	 */
3519 	if (ELF_ST_BIND(sym->st_info) == STB_LOCAL) {
3520 		if ((ELF_ST_TYPE(sym->st_info) == STT_SECTION)) {
3521 			value = *(long *)roffset;
3522 			*(long *)roffset = 0;
3523 		} else {
3524 			value = sym->st_value;
3525 		}
3526 	}
3527 	return (-(TLSSTATOFF(lmp) - value));
3528 }
3529 
3530 /*
3531  * If the symbol is not found and the reference was not to a weak symbol, report
3532  * an error.  Weak references may be unresolved.
3533  */
3534 int
3535 elf_reloc_error(Rt_map *lmp, const char *name, void *rel, uint_t binfo)
3536 {
3537 	Lm_list	*lml = LIST(lmp);
3538 
3539 	/*
3540 	 * Under crle(1), relocation failures are ignored.
3541 	 */
3542 	if (lml->lm_flags & LML_FLG_IGNRELERR)
3543 		return (1);
3544 
3545 	/*
3546 	 * Under ldd(1), unresolved references are reported.  However, if the
3547 	 * original reference is EXTERN or PARENT these references are ignored
3548 	 * unless ldd's -p option is in effect.
3549 	 */
3550 	if (lml->lm_flags & LML_FLG_TRC_WARN) {
3551 		if (((binfo & DBG_BINFO_REF_MSK) == 0) ||
3552 		    ((lml->lm_flags & LML_FLG_TRC_NOPAREXT) != 0)) {
3553 			(void) printf(MSG_INTL(MSG_LDD_SYM_NFOUND),
3554 			    demangle(name), NAME(lmp));
3555 		}
3556 		return (1);
3557 	}
3558 
3559 	/*
3560 	 * Otherwise, the unresolved references is fatal.
3561 	 */
3562 	DBG_CALL(Dbg_reloc_in(lml, ELF_DBG_RTLD, M_MACH, M_REL_SHT_TYPE, rel,
3563 	    NULL, name));
3564 	eprintf(lml, ERR_FATAL, MSG_INTL(MSG_REL_NOSYM), NAME(lmp),
3565 	    demangle(name));
3566 
3567 	return (0);
3568 }
3569