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