xref: /titanic_51/usr/src/cmd/sgs/rtld/common/setup.c (revision 672986541be54a7a471bb088e60780c37e371d7e)
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 2007 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 /*
33  * Run time linker common setup.
34  *
35  * Called from _setup to get the process going at startup.
36  */
37 #include	"_synonyms.h"
38 
39 #include	<stdlib.h>
40 #include	<fcntl.h>
41 #include	<stdio.h>
42 #include	<sys/types.h>
43 #include	<sys/stat.h>
44 #include	<sys/mman.h>
45 #include	<string.h>
46 #include	<stdio.h>
47 #include	<unistd.h>
48 #include	<dlfcn.h>
49 #include	<sys/sysconfig.h>
50 #include	<sys/auxv.h>
51 #include	<debug.h>
52 #include	<conv.h>
53 #include	"_rtld.h"
54 #include	"_audit.h"
55 #include	"_elf.h"
56 #include	"_a.out.h"
57 #include	"msg.h"
58 
59 
60 extern int	_end, _edata, _etext;
61 extern void	_init(void);
62 extern int	_brk_unlocked(void *);
63 
64 #ifndef	SGS_PRE_UNIFIED_PROCESS
65 /* needed for _brk_unlocked() */
66 void *_nd = &_end;
67 #endif
68 
69 /*
70  * Define for the executable's interpreter.
71  * Usually it is ld.so.1, but for the first release of ICL binaries
72  * it is libc.so.1.  We keep this information so that we don't end
73  * up mapping libc twice if it is the interpreter.
74  */
75 static Interp _interp;
76 
77 
78 static int
79 preload(const char *str, Rt_map *lmp)
80 {
81 	Rt_map		*clmp = lmp;
82 	char		*objs, *ptr, *next;
83 	Word		lmflags = lml_main.lm_flags;
84 	uint_t		flags;
85 
86 	DBG_CALL(Dbg_util_nl(&lml_main, DBG_NL_STD));
87 
88 	if ((objs = strdup(str)) == 0)
89 		return (0);
90 
91 	/*
92 	 * Establish the flags for loading each object.  If we're called via
93 	 * lddstub, then the first shared object is the object being inspected
94 	 * by ldd(1).  This object should not be marked as an interposer, as
95 	 * it is intended to act like the first object of the process.
96 	 */
97 	if ((lmflags & LML_FLG_TRC_ENABLE) && (FLAGS1(lmp) & FL1_RT_LDDSTUB))
98 		flags = FLG_RT_PRELOAD;
99 	else
100 		flags = (FLG_RT_PRELOAD | FLG_RT_OBJINTPO);
101 
102 	ptr = strtok_r(objs, MSG_ORIG(MSG_STR_DELIMIT), &next);
103 	do {
104 		Pnode	*pnp;
105 		Rt_map	*nlmp = 0;
106 
107 		DBG_CALL(Dbg_file_preload(&lml_main, ptr));
108 
109 		/*
110 		 * If this a secure application, then preload errors are
111 		 * reduced to warnings, as the errors are non-fatal.
112 		 */
113 		if (rtld_flags & RT_FL_SECURE)
114 			rtld_flags2 |= RT_FL2_FTL2WARN;
115 		if ((pnp = expand_paths(clmp, ptr, PN_SER_EXTLOAD, 0)) != 0)
116 			nlmp = load_one(&lml_main, ALO_DATA, pnp, clmp,
117 			MODE(lmp), flags, 0);
118 		if (pnp)
119 			remove_pnode(pnp);
120 		if (rtld_flags & RT_FL_SECURE)
121 			rtld_flags2 &= ~RT_FL2_FTL2WARN;
122 		if (nlmp && (bind_one(clmp, nlmp, BND_NEEDED) == 0))
123 			nlmp = 0;
124 
125 		/*
126 		 * Establish state for the next preloadable object.  If no
127 		 * error occurred with loading this object, indicate that this
128 		 * link-map list contains an interposer.
129 		 */
130 		flags |= FLG_RT_OBJINTPO;
131 		if (nlmp == 0) {
132 			if ((lmflags & LML_FLG_TRC_ENABLE) ||
133 			    (rtld_flags & RT_FL_SECURE))
134 				continue;
135 			else
136 				return (0);
137 		}
138 		lml_main.lm_flags |= LML_FLG_INTRPOSE;
139 
140 		/*
141 		 * If we're tracing shared objects via lddstub, establish a
142 		 * binding between the initial shared object and lddstub so that
143 		 * the shared object isn't called out from unused() processing.
144 		 * After the first object is loaded increment the caller to the
145 		 * initial preloaded object to provide intuitive ldd -v and -s
146 		 * diagnostics
147 		 */
148 		if ((lmflags & LML_FLG_TRC_ENABLE) &&
149 		    (FLAGS1(lmp) & FL1_RT_LDDSTUB)) {
150 			if ((lmp == clmp) && (lmflags &
151 			    (LML_FLG_TRC_UNREF | LML_FLG_TRC_UNUSED))) {
152 				if (bind_one(clmp, nlmp, BND_REFER) == 0)
153 					continue;
154 			}
155 			clmp = (Rt_map *)NEXT(lmp);
156 		}
157 
158 	} while ((ptr = strtok_r(NULL,
159 	    MSG_ORIG(MSG_STR_DELIMIT), &next)) != NULL);
160 
161 	free(objs);
162 	return (1);
163 }
164 
165 Rt_map *
166 setup(char **envp, auxv_t *auxv, Word _flags, char *_platform, int _syspagsz,
167     char *_rtldname, Dyn *dyn_ptr, ulong_t ld_base, ulong_t interp_base, int fd,
168     Phdr *phdr, char *execname, char **argv, int dz_fd, uid_t uid,
169     uid_t euid, gid_t gid, gid_t egid, void *aoutdyn, int auxflags,
170     uint_t hwcap_1)
171 {
172 	Rt_map		*rlmp, *mlmp, **tobj = 0;
173 	Ehdr		*ehdr;
174 	struct stat	status;
175 	int		features = 0, ldsoexec = 0;
176 	size_t		eaddr, esize;
177 	char		*str, *argvname;
178 	Mmap		*mmaps;
179 	Word		lmflags;
180 
181 	/*
182 	 * Now that ld.so has relocated itself, initialize our own 'environ' so
183 	 * as to establish an address suitable for libc's hardware mul/div
184 	 * magic (libc/sparc/crt/hwmuldiv.o).
185 	 */
186 	_environ = (char **)((ulong_t)auxv - sizeof (char *));
187 	_init();
188 	_environ = envp;
189 
190 	/*
191 	 * Far the most common application execution revolves around appending
192 	 * the application name to the users PATH definition, thus a full name
193 	 * is passed to exec() which will in turn be returned via
194 	 * AT_SUN_EXECNAME.  Applications may also be invoked from the current
195 	 * working directory, or via a relative name.
196 	 *
197 	 * Determine whether the kernel has supplied a AT_SUN_EXECNAME aux
198 	 * vector.  This vector points to the full pathname, on the stack, of
199 	 * the object that started the process.  If this is null, then
200 	 * AT_SUN_EXECNAME isn't supported (if the pathname exceeded the system
201 	 * limit (PATH_MAX) the exec would have failed).  This flag is used to
202 	 * determine whether we can call resolvepath().
203 	 */
204 	if (execname)
205 		rtld_flags |= RT_FL_EXECNAME;
206 
207 	/*
208 	 * Determine how ld.so.1 has been executed.
209 	 */
210 	if ((fd == -1) && (phdr == 0)) {
211 		/*
212 		 * If we received neither the AT_EXECFD nor the AT_PHDR aux
213 		 * vector, ld.so.1 must have been invoked directly from the
214 		 * command line.
215 		 */
216 		ldsoexec = 1;
217 
218 		/*
219 		 * AT_SUN_EXECNAME provides the most precise name, if it is
220 		 * available, otherwise fall back to argv[0].  At this time,
221 		 * there is no process name.
222 		 */
223 		if (execname)
224 			rtldname = execname;
225 		else if (argv[0])
226 			rtldname = argv[0];
227 		else
228 			rtldname = (char *)MSG_INTL(MSG_STR_UNKNOWN);
229 	} else {
230 		/*
231 		 * Otherwise, we have a standard process.  AT_SUN_EXECNAME
232 		 * provides the most precise name, if it is available,
233 		 * otherwise fall back to argv[0].  Provided the application
234 		 * is already mapped, the process is the application, so
235 		 * simplify the application name for use in any diagnostics.
236 		 */
237 		if (execname)
238 			argvname = execname;
239 		else if (argv[0])
240 			argvname = execname = argv[0];
241 		else
242 			argvname = execname = (char *)MSG_INTL(MSG_STR_UNKNOWN);
243 
244 		if (fd == -1) {
245 			if ((str = strrchr(argvname, '/')) != 0)
246 				procname = ++str;
247 			else
248 				procname = argvname;
249 		}
250 
251 		/*
252 		 * At this point, we don't know the runtime linkers full path
253 		 * name.  The _rtldname passed to us is the SONAME of the
254 		 * runtime linker, which is typically /lib/ld.so.1 no matter
255 		 * what the full path is.   Use this for now, we'll reset the
256 		 * runtime linkers name once the application is analyzed.
257 		 */
258 		if (_rtldname) {
259 			if ((str = strrchr(_rtldname, '/')) != 0)
260 				rtldname = ++str;
261 			else
262 				rtldname = _rtldname;
263 		} else
264 			rtldname = (char *)MSG_INTL(MSG_STR_UNKNOWN);
265 	}
266 
267 	/*
268 	 * Initialize any global variables.
269 	 */
270 	at_flags = _flags;
271 	if (dz_fd != FD_UNAVAIL)
272 		dz_init(dz_fd);
273 	platform = _platform;
274 
275 	/*
276 	 * If pagesize is unspecified find its value.
277 	 */
278 	if ((syspagsz = _syspagsz) == 0)
279 		syspagsz = _sysconfig(_CONFIG_PAGESIZE);
280 	fmap_setup();
281 
282 	/*
283 	 * Add the unused portion of the last data page to the free space list.
284 	 * The page size must be set before doing this.  Here, _end refers to
285 	 * the end of the runtime linkers bss.  Note that we do not use the
286 	 * unused data pages from any included .so's to supplement this free
287 	 * space as badly behaved .os's may corrupt this data space, and in so
288 	 * doing ruin our data.
289 	 */
290 	eaddr = S_DROUND((size_t)&_end);
291 	esize = eaddr % syspagsz;
292 	if (esize) {
293 		esize = syspagsz - esize;
294 		addfree((void *)eaddr, esize);
295 	}
296 
297 	/*
298 	 * Establish initial link-map list flags, and link-map list alists.
299 	 */
300 	if (alist_append(&lml_main.lm_lists, 0, sizeof (Lm_cntl),
301 	    AL_CNT_LMLISTS) == 0)
302 		return (0);
303 	lml_main.lm_flags |= LML_FLG_BASELM;
304 	lml_main.lm_lmid = LM_ID_BASE;
305 	lml_main.lm_lmidstr = (char *)MSG_ORIG(MSG_LMID_BASE);
306 
307 	if (alist_append(&lml_rtld.lm_lists, 0, sizeof (Lm_cntl),
308 	    AL_CNT_LMLISTS) == 0)
309 		return (0);
310 	lml_rtld.lm_flags |= (LML_FLG_RTLDLM | LML_FLG_NOAUDIT |
311 	    LML_FLG_HOLDLOCK);
312 	lml_rtld.lm_lmid = LM_ID_LDSO;
313 	lml_rtld.lm_lmidstr = (char *)MSG_ORIG(MSG_LMID_LDSO);
314 
315 	/*
316 	 * Determine whether we have a secure executable.
317 	 */
318 	security(uid, euid, gid, egid, auxflags);
319 
320 	/*
321 	 * Initialize a hardware capability descriptor for use in comparing
322 	 * each loaded object.
323 	 */
324 #ifdef	AT_SUN_AUXFLAGS
325 	if (auxflags & AF_SUN_HWCAPVERIFY) {
326 		rtld_flags2 |= RT_FL2_HWCAP;
327 		hwcap = (ulong_t)hwcap_1;
328 	}
329 #endif
330 	/*
331 	 * Look for environment strings (allows things like LD_NOAUDIT to be
332 	 * established, although debugging isn't enabled until later).
333 	 */
334 	if ((readenv_user((const char **)envp, &(lml_main.lm_flags),
335 	    &(lml_main.lm_tflags), (aoutdyn != 0))) == 1)
336 		return (0);
337 
338 	/*
339 	 * Create a mapping descriptor for ld.so.1.  We can determine our
340 	 * two segments information from known symbols.
341 	 */
342 	if ((mmaps = calloc(3, sizeof (Mmap))) == 0)
343 		return (0);
344 	mmaps[0].m_vaddr = (caddr_t)M_PTRUNC(ld_base);
345 	mmaps[0].m_msize = (size_t)((caddr_t)&_etext - mmaps[0].m_vaddr);
346 	mmaps[0].m_fsize = mmaps[0].m_msize;
347 	mmaps[0].m_perm = (PROT_READ | PROT_EXEC);
348 	mmaps[1].m_vaddr = (caddr_t)M_PTRUNC((ulong_t)&r_debug);
349 	mmaps[1].m_msize = (size_t)((caddr_t)&_end - mmaps[1].m_vaddr);
350 	mmaps[1].m_fsize = (size_t)((caddr_t)&_edata - mmaps[1].m_vaddr);
351 	mmaps[1].m_perm = (PROT_READ | PROT_WRITE | PROT_EXEC);
352 
353 	/*
354 	 * Create a link map structure for ld.so.1.
355 	 */
356 	if ((rlmp = elf_new_lm(&lml_rtld, _rtldname, rtldname, dyn_ptr, ld_base,
357 	    (ulong_t)&_etext, ALO_DATA, (ulong_t)(eaddr - ld_base), 0, ld_base,
358 	    (ulong_t)(eaddr - ld_base), mmaps, 2)) == 0) {
359 		return (0);
360 	}
361 
362 	MODE(rlmp) |= (RTLD_LAZY | RTLD_NODELETE | RTLD_GLOBAL | RTLD_WORLD);
363 	FLAGS(rlmp) |= (FLG_RT_ANALYZED | FLG_RT_RELOCED | FLG_RT_INITDONE |
364 		FLG_RT_INITCLCT | FLG_RT_FINICLCT | FLG_RT_MODESET);
365 
366 	/*
367 	 * Initialize the runtime linkers information.
368 	 */
369 	interp = &_interp;
370 	interp->i_name = NAME(rlmp);
371 	interp->i_faddr = (caddr_t)ADDR(rlmp);
372 	ldso_plt_init(rlmp);
373 
374 	/*
375 	 * If ld.so.1 has been invoked directly, process its arguments.
376 	 */
377 	if (ldsoexec) {
378 		/*
379 		 * Process any arguments that are specific to ld.so.1, and
380 		 * reorganize the process stack to effectively remove ld.so.1
381 		 * from it.  Reinitialize the environment pointer, as this may
382 		 * have been shifted after skipping ld.so.1's arguments.
383 		 */
384 		if (rtld_getopt(argv, &envp, &auxv, &(lml_main.lm_flags),
385 		    &(lml_main.lm_tflags), (aoutdyn != 0)) == 1) {
386 			eprintf(&lml_main, ERR_NONE, MSG_INTL(MSG_USG_BADOPT));
387 			return (0);
388 		}
389 		_environ = envp;
390 
391 		/*
392 		 * Open the object that ld.so.1 is to execute.
393 		 */
394 		argvname = execname = argv[0];
395 
396 		if ((fd = open(argvname, O_RDONLY)) == -1) {
397 			int	err = errno;
398 			eprintf(&lml_main, ERR_FATAL, MSG_INTL(MSG_SYS_OPEN),
399 			    argvname, strerror(err));
400 			return (0);
401 		}
402 	}
403 
404 	/*
405 	 * Map in the file, if exec has not already done so.  If it has,
406 	 * simply create a new link map structure for the executable.
407 	 */
408 	if (fd != -1) {
409 		Rej_desc	rej;
410 		Fct		*ftp;
411 
412 		/*
413 		 * Find out what type of object we have.
414 		 */
415 		(void) fstat(fd, &status);
416 		if ((ftp = are_u_this(&rej, fd, &status, argvname)) == 0) {
417 			eprintf(&lml_main, ERR_FATAL,
418 			    MSG_INTL(err_reject[rej.rej_type]), argvname,
419 			    conv_reject_desc(&rej));
420 			return (0);
421 		}
422 
423 		/*
424 		 * Map in object.
425 		 */
426 		if ((mlmp = (ftp->fct_map_so)(&lml_main, ALO_DATA, execname,
427 		    argvname, fd)) == 0)
428 			return (0);
429 
430 		/*
431 		 * We now have a process name for error diagnostics.
432 		 */
433 		if ((str = strrchr(argvname, '/')) != 0)
434 			procname = ++str;
435 		else
436 			procname = argvname;
437 
438 		if (ldsoexec) {
439 			Addr	brkbase = 0;
440 
441 			/*
442 			 * Since ld.so.1 was the primary executed object - the
443 			 * brk() base has not yet been initialized, we need to
444 			 * initialize it.  For an executable, initialize it to
445 			 * the end of the object.  For a shared object (ET_DYN)
446 			 * initialize it to the first page in memory.
447 			 */
448 			ehdr = (Ehdr *)ADDR(mlmp);
449 
450 			if ((FCT(mlmp) == &elf_fct) &&
451 			    (ehdr->e_type == ET_EXEC)) {
452 				int	i;
453 				Phdr *	_phdr = (Phdr *)((uintptr_t)ADDR(mlmp) +
454 					ehdr->e_phoff);
455 
456 				/*
457 				 * We scan the program headers to find the tail
458 				 * of the memory image.  We can't use MSIZE()
459 				 * since that's already been page aligned.
460 				 */
461 				for (i = 0; i < ehdr->e_phnum; i++, _phdr++) {
462 					if (_phdr->p_type == PT_LOAD)
463 						brkbase = _phdr->p_vaddr +
464 							_phdr->p_memsz;
465 				}
466 			}
467 
468 			if (!brkbase)
469 				brkbase = syspagsz;
470 
471 			if (_brk_unlocked((void *)brkbase) == -1) {
472 				int	err = errno;
473 				eprintf(&lml_main, ERR_FATAL,
474 				    MSG_INTL(MSG_SYS_BRK), argvname,
475 				    strerror(err));
476 			}
477 		}
478 
479 		/*
480 		 * The object has now been mmaped, we no longer need the file
481 		 * descriptor.
482 		 */
483 		(void) close(fd);
484 
485 	} else {
486 		/*
487 		 * Set up function ptr and arguments according to the type
488 		 * of file class the executable is. (Currently only supported
489 		 * types are ELF and a.out format.)  Then create a link map
490 		 * for the executable.
491 		 */
492 		if (aoutdyn) {
493 #ifdef A_OUT
494 			if ((mlmp = aout_new_lm(&lml_main, execname, argvname,
495 			    aoutdyn, 0, 0, ALO_DATA)) == 0)
496 				return (0);
497 
498 			/*
499 			 * Set the memory size.  Note, we only know the end of
500 			 * text, and although we could find the _end by looking
501 			 * up the symbol, this may not be present.  We should
502 			 * set ADDR to MAIN_BASE, but presently all the a.out
503 			 * relocation code assumes ADDR is 0 for the dynamic
504 			 * executable. (these data items are only used for
505 			 * dladdr(3x), and there aren't many a.out dladdr(3x)
506 			 * users to warrant spending much time on this :-).
507 			 */
508 			MSIZE(mlmp) = MAIN_BASE + ETEXT(mlmp);
509 
510 			/*
511 			 * Disable any object configuration cache (BCP apps
512 			 * bring in sbcp which can benefit from any object
513 			 * cache, but both the app and sbcp can't use the same
514 			 * objects).
515 			 */
516 			rtld_flags |= RT_FL_NOOBJALT;
517 
518 			/*
519 			 * Make sure no-direct bindings are in effect.
520 			 */
521 			lml_main.lm_tflags |= LML_TFLG_NODIRECT;
522 #else
523 			eprintf(&lml_main, ERR_FATAL,
524 			    MSG_INTL(MSG_ERR_REJ_UNKFILE), argvname);
525 			return (0);
526 #endif
527 		} else if (phdr) {
528 			Phdr		*pptr, *firstptr = 0, *lastptr;
529 			Phdr		*tlsphdr = 0, *unwindphdr = 0;
530 			Dyn		*dyn = 0;
531 			Cap		*cap = 0;
532 			Off		i_offset = 0;
533 			Addr		base = 0;
534 			ulong_t		memsize, phsize, entry, etext;
535 			uint_t		mmapcnt = 0;
536 			int		i;
537 
538 			/*
539 			 * Using the executables phdr address determine the base
540 			 * address of the input file.  NOTE, this assumes the
541 			 * program headers and elf header are part of the same
542 			 * mapped segment.  Although this has held for many
543 			 * years now, it might be more flexible if the kernel
544 			 * gave use the ELF headers start address, rather than
545 			 * the Program headers.
546 			 *
547 			 * Determine from the ELF header if we're been called
548 			 * from a shared object or dynamic executable.  If the
549 			 * latter, then any addresses within the object are used
550 			 * as is.  Addresses within shared objects must be added
551 			 * to the process's base address.
552 			 */
553 			ehdr = (Ehdr *)((Addr)phdr - phdr->p_offset);
554 			phsize = ehdr->e_phentsize;
555 			if (ehdr->e_type == ET_DYN)
556 				base = (Addr)ehdr;
557 
558 			/*
559 			 * Allocate a mapping array to retain mapped segment
560 			 * information.
561 			 */
562 			if ((mmaps = calloc(ehdr->e_phnum, sizeof (Mmap))) == 0)
563 				return (0);
564 
565 			/*
566 			 * Extract the needed information from the segment
567 			 * headers.
568 			 */
569 			for (i = 0, pptr = phdr; i < ehdr->e_phnum; i++) {
570 				if (pptr->p_type == PT_INTERP) {
571 					i_offset = pptr->p_offset;
572 					interp->i_faddr =
573 					    (caddr_t)interp_base;
574 				}
575 				if ((pptr->p_type == PT_LOAD) &&
576 				    (pptr->p_filesz || pptr->p_memsz)) {
577 					int	perm = (PROT_READ | PROT_EXEC);
578 					size_t	off;
579 
580 					if (!firstptr)
581 						firstptr = pptr;
582 					lastptr = pptr;
583 					if (i_offset && pptr->p_filesz &&
584 					    (i_offset >= pptr->p_offset) &&
585 					    (i_offset <=
586 					    (pptr->p_memsz + pptr->p_offset))) {
587 						interp->i_name = (char *)
588 						    pptr->p_vaddr + i_offset -
589 						    pptr->p_offset + base;
590 						i_offset = 0;
591 					}
592 					if ((pptr->p_flags &
593 					    (PF_R | PF_W)) == PF_R)
594 						etext = pptr->p_vaddr +
595 						    pptr->p_memsz + base;
596 					else
597 						perm |= PROT_WRITE;
598 
599 					/*
600 					 * Retain segments mapping info.  Round
601 					 * each segment to a page boundary, as
602 					 * this insures addresses are suitable
603 					 * for mprotect() if required.
604 					 */
605 					off = pptr->p_vaddr + base;
606 					mmaps[mmapcnt].m_vaddr =
607 					    (caddr_t)M_PTRUNC(off);
608 					off -= (size_t)mmaps[mmapcnt].m_vaddr;
609 					mmaps[mmapcnt].m_msize =
610 					    pptr->p_memsz + off;
611 					mmaps[mmapcnt].m_fsize =
612 					    pptr->p_filesz + off;
613 					mmaps[mmapcnt].m_perm = perm;
614 					mmapcnt++;
615 
616 				} else if (pptr->p_type == PT_DYNAMIC) {
617 					dyn = (Dyn *)(pptr->p_vaddr + base);
618 				} else if ((pptr->p_type == PT_TLS) &&
619 				    pptr->p_memsz) {
620 					tlsphdr = pptr;
621 				} else if (pptr->p_type == PT_SUNW_UNWIND) {
622 					unwindphdr = pptr;
623 				} else if (pptr->p_type == PT_SUNWCAP) {
624 					cap = (Cap *)(pptr->p_vaddr + base);
625 				}
626 				pptr = (Phdr *)((ulong_t)pptr + phsize);
627 			}
628 
629 
630 			memsize = (lastptr->p_vaddr + lastptr->p_memsz) -
631 				S_ALIGN(firstptr->p_vaddr, syspagsz);
632 
633 			entry = ehdr->e_entry;
634 			if (ehdr->e_type == ET_DYN)
635 				entry += (ulong_t)ehdr;
636 
637 			if ((mlmp = elf_new_lm(&lml_main, execname, argvname,
638 			    dyn, (Addr)ehdr, etext, ALO_DATA, memsize, entry,
639 			    (ulong_t)ehdr, memsize, mmaps, mmapcnt)) == 0) {
640 				return (0);
641 			}
642 			if (tlsphdr &&
643 			    (tls_assign(&lml_main, mlmp, tlsphdr) == 0))
644 				return (0);
645 
646 			if (unwindphdr)
647 				PTUNWIND(mlmp) = unwindphdr;
648 
649 			if (cap)
650 				cap_assign(cap, mlmp);
651 		}
652 	}
653 
654 	/*
655 	 * Establish the interpretors name as that defined within the initial
656 	 * object (executable).  This provides for ORIGIN processing of ld.so.1
657 	 * dependencies.
658 	 */
659 	if (ldsoexec == 0) {
660 		size_t	len = strlen(interp->i_name);
661 		(void) expand(&interp->i_name, &len, 0, 0,
662 		    (PN_TKN_ISALIST | PN_TKN_HWCAP), rlmp);
663 	}
664 	PATHNAME(rlmp) = interp->i_name;
665 
666 	if (FLAGS1(rlmp) & FL1_RT_RELATIVE)
667 		(void) fullpath(rlmp, 0);
668 	else
669 		ORIGNAME(rlmp) = PATHNAME(rlmp) = NAME(rlmp);
670 
671 	/*
672 	 * Having established the true runtime linkers name, simplify the name
673 	 * for error diagnostics.
674 	 */
675 	if ((str = strrchr(PATHNAME(rlmp), '/')) != 0)
676 		rtldname = ++str;
677 	else
678 		rtldname = PATHNAME(rlmp);
679 
680 	/*
681 	 * Expand the fullpath name of the application.  This typically occurs
682 	 * as a part of loading an object, but as the kernel probably mapped
683 	 * it in, complete this processing now.
684 	 */
685 	if (FLAGS1(mlmp) & FL1_RT_RELATIVE)
686 		(void) fullpath(mlmp, 0);
687 
688 	/*
689 	 * Some troublesome programs will change the value of argv[0].  Dupping
690 	 * the process string protects us, and insures the string is left in
691 	 * any core files.
692 	 */
693 	if ((str = (char *)strdup(procname)) == 0)
694 		return (0);
695 	procname = str;
696 
697 	/*
698 	 * If the kernel has provided hardware capabilities information, and
699 	 * the executable contains hardware capabilities information, make
700 	 * sure it's a valid object.
701 	 */
702 	if ((rtld_flags2 & RT_FL2_HWCAP) && HWCAP(mlmp)) {
703 		ulong_t	mhwcap;
704 
705 		if ((mhwcap = (HWCAP(mlmp) & ~hwcap)) != 0) {
706 			const char	*str = conv_cap_val_hw1(mhwcap, M_MACH);
707 
708 			if (lml_main.lm_flags & LML_FLG_TRC_ENABLE) {
709 				(void) printf(MSG_INTL(MSG_LDD_GEN_HWCAP_1),
710 				    NAME(mlmp), str);
711 			} else {
712 				eprintf(&lml_main, ERR_FATAL,
713 				    MSG_INTL(MSG_GEN_BADHWCAP_1), str);
714 				return (0);
715 			}
716 		}
717 	}
718 
719 	FLAGS(mlmp) |= (FLG_RT_ISMAIN | FLG_RT_MODESET);
720 	FLAGS1(mlmp) |= FL1_RT_USED;
721 
722 	/*
723 	 * It's the responsibility of MAIN(crt0) to call it's _init and _fini
724 	 * section, therefore null out any INIT/FINI so that this object isn't
725 	 * collected during tsort processing.  And, if the application has no
726 	 * initarray or finiarray we can economize on establishing bindings.
727 	 */
728 	INIT(mlmp) = FINI(mlmp) = 0;
729 	if ((INITARRAY(mlmp) == 0) && (FINIARRAY(mlmp) == 0))
730 		FLAGS1(mlmp) |= FL1_RT_NOINIFIN;
731 
732 	/*
733 	 * Identify lddstub if necessary.
734 	 */
735 	if (lml_main.lm_flags & LML_FLG_TRC_LDDSTUB)
736 		FLAGS1(mlmp) |= FL1_RT_LDDSTUB;
737 
738 	/*
739 	 * Retain our argument information for use in dlinfo.
740 	 */
741 	argsinfo.dla_argv = argv--;
742 	argsinfo.dla_argc = (long)*argv;
743 	argsinfo.dla_envp = envp;
744 	argsinfo.dla_auxv = auxv;
745 
746 	(void) enter();
747 
748 	/*
749 	 * Add our two main link-maps to the dynlm_list
750 	 */
751 	if (list_append(&dynlm_list, &lml_main) == 0)
752 		return (0);
753 
754 	if (list_append(&dynlm_list, &lml_rtld) == 0)
755 		return (0);
756 
757 	/*
758 	 * Reset the link-map counts for both lists.  The init count is used to
759 	 * track how many objects have pending init sections, this gets incre-
760 	 * mented each time an object is relocated.  Since ld.so.1 relocates
761 	 * itself, it's init count will remain zero.
762 	 * The object count is used to track how many objects have pending fini
763 	 * sections, as ld.so.1 handles its own fini we can zero its count.
764 	 */
765 	lml_main.lm_obj = 1;
766 	lml_rtld.lm_obj = 0;
767 
768 	/*
769 	 * Initialize debugger information structure.  Some parts of this
770 	 * structure were initialized statically.
771 	 */
772 	r_debug.rtd_rdebug.r_map = (Link_map *)lml_main.lm_head;
773 	r_debug.rtd_rdebug.r_ldsomap = (Link_map *)lml_rtld.lm_head;
774 	r_debug.rtd_rdebug.r_ldbase = r_debug.rtd_rdebug.r_ldsomap->l_addr;
775 	r_debug.rtd_dynlmlst = &dynlm_list;
776 
777 	if (platform)
778 		platform_sz = strlen(platform);
779 
780 	/*
781 	 * Determine the dev/inode information for the executable to complete
782 	 * load_so() checking for those who might dlopen(a.out).
783 	 */
784 	if ((FLAGS1(mlmp) & FL1_RT_RELATIVE) &&
785 	    (stat(PATHNAME(mlmp), &status) == 0)) {
786 		STDEV(mlmp) = status.st_dev;
787 		STINO(mlmp) = status.st_ino;
788 	}
789 
790 	/*
791 	 * Initialize any configuration information.
792 	 */
793 	if (!(rtld_flags & RT_FL_NOCFG)) {
794 		if ((features = elf_config(mlmp, (aoutdyn != 0))) == -1)
795 			return (0);
796 	}
797 
798 	/*
799 	 * Establish the modes of the initial object.  These modes are
800 	 * propagated to any preloaded objects and explicit shared library
801 	 * dependencies.  Note, RTLD_NOW may have been established during
802 	 * analysis of the application had it been built -z now.
803 	 */
804 	MODE(mlmp) |= (RTLD_NODELETE | RTLD_GLOBAL | RTLD_WORLD);
805 	if (rtld_flags & RT_FL_CONFGEN)
806 		MODE(mlmp) |= RTLD_CONFGEN;
807 	if ((MODE(mlmp) & RTLD_NOW) == 0) {
808 		if (rtld_flags2 & RT_FL2_BINDNOW)
809 			MODE(mlmp) |= RTLD_NOW;
810 		else
811 			MODE(mlmp) |= RTLD_LAZY;
812 	}
813 
814 	/*
815 	 * If debugging was requested initialize things now that any cache has
816 	 * been established.  A user can specify LD_DEBUG=help to discover the
817 	 * list of debugging tokens available without running the application.
818 	 * However, don't allow this setting from a configuration file.
819 	 *
820 	 * Note, to prevent recursion issues caused by loading and binding the
821 	 * debugging libraries themselves, a local debugging descriptor is
822 	 * initialized.  Once the debugging setup has completed, this local
823 	 * descriptor is copied to the global descriptor which effectively
824 	 * enables diagnostic output.
825 	 */
826 	if (rpl_debug || prm_debug) {
827 		Dbg_desc	_dbg_desc = {0, 0, 0};
828 
829 		if (rpl_debug) {
830 			uintptr_t	ret;
831 
832 			if ((ret = dbg_setup(rpl_debug, &_dbg_desc)) == S_ERROR)
833 				return (0);
834 			if (ret == 0)
835 				rtldexit(&lml_main, 0);
836 		}
837 		if (prm_debug)
838 			(void) dbg_setup(prm_debug, &_dbg_desc);
839 
840 		*dbg_desc = _dbg_desc;
841 	}
842 
843 	/*
844 	 * Now that debugging is enabled generate any diagnostics from any
845 	 * previous events.
846 	 */
847 	if (hwcap)
848 		DBG_CALL(Dbg_cap_val_hw1(&lml_main, hwcap, M_MACH));
849 	if (features)
850 		DBG_CALL(Dbg_file_config_dis(&lml_main, config->c_name,
851 		    features));
852 
853 	if (DBG_ENABLED) {
854 		DBG_CALL(Dbg_file_ldso(rlmp, envp, auxv,
855 		    LIST(rlmp)->lm_lmidstr, ALO_DATA));
856 
857 		if (FCT(mlmp) == &elf_fct) {
858 			DBG_CALL(Dbg_file_elf(&lml_main, PATHNAME(mlmp),
859 			    (ulong_t)DYN(mlmp), ADDR(mlmp), MSIZE(mlmp),
860 			    ENTRY(mlmp), LIST(mlmp)->lm_lmidstr, ALO_DATA));
861 		} else {
862 			DBG_CALL(Dbg_file_aout(&lml_main, PATHNAME(mlmp),
863 			    (ulong_t)AOUTDYN(mlmp), (ulong_t)ADDR(mlmp),
864 			    (ulong_t)MSIZE(mlmp), LIST(mlmp)->lm_lmidstr,
865 			    ALO_DATA));
866 		}
867 	}
868 
869 	/*
870 	 * Enable auditing.
871 	 */
872 	if (rpl_audit || prm_audit || profile_lib) {
873 		int		ndx;
874 		const char	*aud[3];
875 
876 		aud[0] = rpl_audit;
877 		aud[1] = prm_audit;
878 		aud[2] = profile_lib;
879 
880 		/*
881 		 * Any global auditing (set using LD_AUDIT or LD_PROFILE) that
882 		 * can't be established is non-fatal.
883 		 */
884 		if ((auditors = calloc(1, sizeof (Audit_desc))) == 0)
885 			return (0);
886 
887 		for (ndx = 0; ndx < 3; ndx++) {
888 			if (aud[ndx]) {
889 				if ((auditors->ad_name = strdup(aud[ndx])) == 0)
890 					return (0);
891 				rtld_flags2 |= RT_FL2_FTL2WARN;
892 				(void) audit_setup(mlmp, auditors,
893 				    PN_SER_EXTLOAD);
894 				rtld_flags2 &= ~RT_FL2_FTL2WARN;
895 			}
896 		}
897 		lml_main.lm_tflags |= auditors->ad_flags;
898 	}
899 	if (AUDITORS(mlmp)) {
900 		/*
901 		 * Any object required auditing (set with a DT_DEPAUDIT dynamic
902 		 * entry) that can't be established is fatal.
903 		 */
904 		if (audit_setup(mlmp, AUDITORS(mlmp), 0) == 0)
905 			return (0);
906 
907 		FLAGS1(mlmp) |= AUDITORS(mlmp)->ad_flags;
908 		lml_main.lm_flags |= LML_FLG_LOCAUDIT;
909 	}
910 
911 	/*
912 	 * Explicitly add the initial object and ld.so.1 to those objects being
913 	 * audited.  Note, although the ld.so.1 link-map isn't auditable,
914 	 * establish a cookie for ld.so.1 as this may be bound to via the
915 	 * dl*() family.
916 	 */
917 	if ((lml_main.lm_tflags | FLAGS1(mlmp)) & LML_TFLG_AUD_MASK) {
918 		if (((audit_objopen(mlmp, mlmp) == 0) ||
919 		    (audit_objopen(mlmp, rlmp) == 0)) &&
920 		    (FLAGS1(mlmp) & LML_TFLG_AUD_MASK))
921 			return (0);
922 	}
923 
924 	/*
925 	 * Map in any preloadable shared objects.  Note, it is valid to preload
926 	 * a 4.x shared object with a 5.0 executable (or visa-versa), as this
927 	 * functionality is required by ldd(1).
928 	 */
929 	if (rpl_preload && (preload(rpl_preload, mlmp) == 0))
930 		return (0);
931 	if (prm_preload && (preload(prm_preload, mlmp) == 0))
932 		return (0);
933 
934 	/*
935 	 * Load all dependent (needed) objects.
936 	 */
937 	if (analyze_lmc(&lml_main, ALO_DATA, mlmp) == 0)
938 		return (0);
939 
940 	/*
941 	 * Relocate all the dependencies we've just added.
942 	 *
943 	 * If this process has been established via crle(1), the environment
944 	 * variable LD_CONFGEN will have been set.  crle(1) may create this
945 	 * process twice.  The first time crle only needs to gather dependency
946 	 * information.  The second time, is to dldump() the images.
947 	 *
948 	 * If we're only gathering dependencies, relocation is unnecessary.
949 	 * As crle(1) may be building an arbitrary family of objects, they may
950 	 * not fully relocate either.  Hence the relocation phase is not carried
951 	 * out now, but will be called by crle(1) once all objects have been
952 	 * loaded.
953 	 */
954 	if ((rtld_flags & RT_FL_CONFGEN) == 0) {
955 
956 		DBG_CALL(Dbg_util_nl(&lml_main, DBG_NL_STD));
957 
958 		if (relocate_lmc(&lml_main, ALO_DATA, mlmp, mlmp) == 0)
959 			return (0);
960 
961 		/*
962 		 * Inform the debuggers we're here and stable.  Newer debuggers
963 		 * can indicate their presence by setting the DT_DEBUG entry in
964 		 * the dynamic executable (see elf_new_lm()).  In this case call
965 		 * getpid() so the debugger can catch the system call.  This
966 		 * handshake allows the debugger to initialize, and consequently
967 		 * allows the user to set break points in .init code.
968 		 */
969 		rd_event(&lml_main, RD_DLACTIVITY, RT_CONSISTENT);
970 		rd_event(&lml_rtld, RD_DLACTIVITY, RT_CONSISTENT);
971 
972 		if (rtld_flags & RT_FL_DEBUGGER) {
973 			r_debug.rtd_rdebug.r_flags |= RD_FL_ODBG;
974 			(void) getpid();
975 		}
976 	}
977 
978 	/*
979 	 * Indicate preinit activity, and call any auditing routines.  These
980 	 * routines are called before initializing any threads via libc, or
981 	 * before collecting the complete set of .inits on the primary link-map.
982 	 * Although most libc interfaces are encapsulated in local routines
983 	 * within libc, they have been known to escape (ie. call a .plt).  As
984 	 * the appcert auditor uses preinit as a trigger to establish some
985 	 * external interfaces to the main link-maps libc, we need to activate
986 	 * this trigger before exercising any code within libc.  Additionally,
987 	 * I wouldn't put it past an auditor to add additional objects to the
988 	 * primary link-map.  Hence, we collect .inits after the audit call.
989 	 */
990 	rd_event(&lml_main, RD_PREINIT, 0);
991 
992 	if ((lml_main.lm_tflags | FLAGS1(mlmp)) & LML_TFLG_AUD_ACTIVITY)
993 		audit_activity(mlmp, LA_ACT_CONSISTENT);
994 	if ((lml_main.lm_tflags | FLAGS1(mlmp)) & LML_TFLG_AUD_PREINIT)
995 		audit_preinit(mlmp);
996 
997 	/*
998 	 * If we're creating initial configuration information, we're done
999 	 * now that the auditing step has been called.
1000 	 */
1001 	if (rtld_flags & RT_FL_CONFGEN) {
1002 		leave(LIST(mlmp));
1003 		return (mlmp);
1004 	}
1005 
1006 	/*
1007 	 * Sort the .init sections of all objects we've added.  If we're
1008 	 * tracing we only need to execute this under ldd(1) with the -i or -u
1009 	 * options.
1010 	 */
1011 	lmflags = lml_main.lm_flags;
1012 	if (((lmflags & LML_FLG_TRC_ENABLE) == 0) ||
1013 	    (lmflags & (LML_FLG_TRC_INIT | LML_FLG_TRC_UNREF))) {
1014 		if ((tobj = tsort(mlmp, LIST(mlmp)->lm_init,
1015 		    RT_SORT_REV)) == (Rt_map **)S_ERROR)
1016 			return (0);
1017 	}
1018 
1019 	/*
1020 	 * If we are tracing we're done.  This is the one legitimate use of a
1021 	 * direct call to rtldexit() rather than return, as we don't want to
1022 	 * return and jump to the application.
1023 	 */
1024 	if (lmflags & LML_FLG_TRC_ENABLE) {
1025 		unused(&lml_main);
1026 		rtldexit(&lml_main, 0);
1027 	}
1028 
1029 	/*
1030 	 * Establish any static TLS for this primary link-map.  Note, regardless
1031 	 * of whether TLS is available, an initial handshake occurs with libc to
1032 	 * indicate we're processing the primary link-map.  Having identified
1033 	 * the primary link-map, initialize threads.
1034 	 */
1035 	if (rt_get_extern(&lml_main, mlmp) == 0)
1036 		return (0);
1037 	if (tls_statmod(&lml_main, mlmp) == 0)
1038 		return (0);
1039 
1040 	rt_thr_init(&lml_main);
1041 
1042 	rtld_flags2 |= RT_FL2_PLMSETUP;
1043 	rtld_flags |= RT_FL_APPLIC;
1044 
1045 	/*
1046 	 * Fire all dependencies .init sections.  Identify any unused
1047 	 * dependencies, and leave the runtime linker - effectively calling
1048 	 * the dynamic executables entry point.
1049 	 */
1050 	call_array(PREINITARRAY(mlmp), (uint_t)PREINITARRAYSZ(mlmp), mlmp,
1051 		SHT_PREINIT_ARRAY);
1052 
1053 	if (tobj)
1054 		call_init(tobj, DBG_INIT_SORT);
1055 
1056 	rd_event(&lml_main, RD_POSTINIT, 0);
1057 
1058 	unused(&lml_main);
1059 
1060 	DBG_CALL(Dbg_util_call_main(mlmp));
1061 
1062 	leave(LIST(mlmp));
1063 
1064 	return (mlmp);
1065 }
1066