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