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