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