xref: /titanic_41/usr/src/cmd/sgs/rtld/common/setup.c (revision 5203bc321053fb87d7073c7640548fab73634793)
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) 1992, 2010, Oracle and/or its affiliates. All rights reserved.
24  */
25 
26 /*
27  *	Copyright (c) 1988 AT&T
28  *	  All Rights Reserved
29  */
30 
31 /*
32  * Run time linker common setup.
33  *
34  * Called from _setup to get the process going at startup.
35  */
36 
37 #include	<stdlib.h>
38 #include	<fcntl.h>
39 #include	<stdio.h>
40 #include	<sys/types.h>
41 #include	<sys/stat.h>
42 #include	<sys/mman.h>
43 #include	<string.h>
44 #include	<unistd.h>
45 #include	<dlfcn.h>
46 #include	<sys/sysconfig.h>
47 #include	<sys/auxv.h>
48 #include	<debug.h>
49 #include	<conv.h>
50 #include	"_rtld.h"
51 #include	"_audit.h"
52 #include	"_elf.h"
53 #include	"_a.out.h"
54 #include	"msg.h"
55 
56 
57 extern int	_end, _edata, _etext;
58 extern void	_init(void);
59 extern int	_brk_unlocked(void *);
60 
61 #ifndef	SGS_PRE_UNIFIED_PROCESS
62 /* needed for _brk_unlocked() */
63 void *_nd = &_end;
64 #endif
65 
66 /*
67  * Counters that are incremented every time an object is mapped/unmapped.
68  *
69  * Note that exec() will usually map 2 objects before we receive control,
70  * but this can be 1 if ld.so.1 is executed directly. We count one of these
71  * here, and add another as necessary in setup().
72  */
73 u_longlong_t	cnt_map = 1;
74 u_longlong_t	cnt_unmap = 0;
75 
76 
77 /*
78  * Define for the executable's interpreter.
79  * Usually it is ld.so.1, but for the first release of ICL binaries
80  * it is libc.so.1.  We keep this information so that we don't end
81  * up mapping libc twice if it is the interpreter.
82  */
83 static Interp _interp;
84 
85 /*
86  * LD_PRELOAD objects.
87  */
88 static int
89 preload(const char *str, Rt_map *mlmp, Rt_map **clmp)
90 {
91 	Alist		*palp = NULL;
92 	char		*objs, *ptr, *next;
93 	Word		lmflags = lml_main.lm_flags;
94 	int		lddstub;
95 
96 	DBG_CALL(Dbg_util_nl(&lml_main, DBG_NL_STD));
97 
98 	if ((objs = strdup(str)) == NULL)
99 		return (0);
100 
101 	/*
102 	 * Determine if we've been called from lddstub.
103 	 */
104 	lddstub = (lmflags & LML_FLG_TRC_ENABLE) &&
105 	    (FLAGS1(*clmp) & FL1_RT_LDDSTUB);
106 
107 	ptr = strtok_r(objs, MSG_ORIG(MSG_STR_DELIMIT), &next);
108 	do {
109 		Rt_map	*nlmp = NULL;
110 		uint_t	flags;
111 
112 		DBG_CALL(Dbg_file_preload(&lml_main, ptr));
113 
114 		/*
115 		 * Establish the flags for loading each object.  If we're
116 		 * called via lddstub, then the first preloaded object is the
117 		 * object being inspected by ldd(1).  This object should not be
118 		 * marked as an interposer, as this object is intended to act
119 		 * as the target object of the process.
120 		 */
121 		if (lddstub)
122 			flags = FLG_RT_PRELOAD;
123 		else
124 			flags = (FLG_RT_PRELOAD | FLG_RT_OBJINTPO);
125 
126 		/*
127 		 * If this a secure application, then preload errors are
128 		 * reduced to warnings, as the errors are non-fatal.
129 		 */
130 		if (rtld_flags & RT_FL_SECURE)
131 			rtld_flags2 |= RT_FL2_FTL2WARN;
132 		if (expand_paths(*clmp, ptr, &palp, AL_CNT_NEEDED,
133 		    PD_FLG_EXTLOAD, 0) != 0)
134 			nlmp = load_one(&lml_main, ALIST_OFF_DATA, palp, *clmp,
135 			    MODE(mlmp), flags, 0, NULL);
136 		remove_plist(&palp, 0);
137 		if (rtld_flags & RT_FL_SECURE)
138 			rtld_flags2 &= ~RT_FL2_FTL2WARN;
139 		if (nlmp && (bind_one(*clmp, nlmp, BND_NEEDED) == 0))
140 			nlmp = NULL;
141 
142 		if (lddstub && nlmp) {
143 			lddstub = 0;
144 
145 			/*
146 			 * Fabricate a binding between the target shared object
147 			 * and lddstub so that the target object isn't called
148 			 * out from unused() processing.
149 			 */
150 			if (lmflags &
151 			    (LML_FLG_TRC_UNREF | LML_FLG_TRC_UNUSED)) {
152 				if (bind_one(*clmp, nlmp, BND_REFER) == 0)
153 					nlmp = NULL;
154 			}
155 
156 			/*
157 			 * By identifying lddstub as the caller, several
158 			 * confusing ldd() diagnostics get suppressed.  These
159 			 * diagnostics would reveal how the target shared object
160 			 * was found from lddstub.  Now that the real target is
161 			 * loaded, identify the target as the caller so that all
162 			 * ldd() diagnostics are enabled for subsequent objects.
163 			 */
164 			if (nlmp)
165 				*clmp = nlmp;
166 		}
167 
168 		/*
169 		 * If no error occurred with loading this object, indicate that
170 		 * this link-map list contains an interposer.
171 		 */
172 		if (nlmp == NULL) {
173 			if ((lmflags & LML_FLG_TRC_ENABLE) ||
174 			    (rtld_flags & RT_FL_SECURE))
175 				continue;
176 			else
177 				return (0);
178 		}
179 		if (flags & FLG_RT_OBJINTPO)
180 			lml_main.lm_flags |= LML_FLG_INTRPOSE;
181 
182 	} while ((ptr = strtok_r(NULL,
183 	    MSG_ORIG(MSG_STR_DELIMIT), &next)) != NULL);
184 
185 	free(palp);
186 	free(objs);
187 	return (1);
188 }
189 
190 Rt_map *
191 setup(char **envp, auxv_t *auxv, Word _flags, char *_platform, int _syspagsz,
192     char *_rtldname, ulong_t ld_base, ulong_t interp_base, int fd, Phdr *phdr,
193     char *execname, char **argv, uid_t uid, uid_t euid, gid_t gid, gid_t egid,
194     void *aoutdyn, int auxflags, uint_t hwcap_1)
195 {
196 	Rt_map			*rlmp, *mlmp, *clmp, **tobj = NULL;
197 	Ehdr			*ehdr;
198 	rtld_stat_t		status;
199 	int			features = 0, ldsoexec = 0;
200 	size_t			eaddr, esize;
201 	char			*str, *argvname;
202 	Word			lmflags;
203 	mmapobj_result_t	*mpp;
204 	Fdesc			fdr = { 0 }, fdm = { 0 };
205 	Rej_desc		rej = { 0 };
206 	APlist			*ealp = NULL;
207 
208 	/*
209 	 * Now that ld.so has relocated itself, initialize our own 'environ' so
210 	 * as to establish an address suitable for any libc requirements.
211 	 */
212 	_environ = (char **)((ulong_t)auxv - sizeof (char *));
213 	_init();
214 	_environ = envp;
215 
216 	/*
217 	 * Establish a base time.  Total time diagnostics start from entering
218 	 * ld.so.1 here, however the base time is reset each time the ld.so.1
219 	 * is re-entered.  Note also, there will be a large time associated
220 	 * with the first diagnostic from ld.so.1, as bootstrapping ld.so.1
221 	 * and establishing the liblddbg infrastructure takes some time.
222 	 */
223 	(void) gettimeofday(&DBG_TOTALTIME, NULL);
224 	DBG_DELTATIME = DBG_TOTALTIME;
225 
226 	/*
227 	 * Determine how ld.so.1 has been executed.
228 	 */
229 	if ((fd == -1) && (phdr == NULL)) {
230 		/*
231 		 * If we received neither the AT_EXECFD nor the AT_PHDR aux
232 		 * vector, ld.so.1 must have been invoked directly from the
233 		 * command line.
234 		 */
235 		ldsoexec = 1;
236 
237 		/*
238 		 * AT_SUN_EXECNAME provides the most precise name, if it is
239 		 * available, otherwise fall back to argv[0].  At this time,
240 		 * there is no process name.
241 		 */
242 		if (execname)
243 			rtldname = execname;
244 		else if (argv[0])
245 			rtldname = argv[0];
246 		else
247 			rtldname = (char *)MSG_INTL(MSG_STR_UNKNOWN);
248 	} else {
249 		/*
250 		 * Otherwise, we have a standard process.  AT_SUN_EXECNAME
251 		 * provides the most precise name, if it is available,
252 		 * otherwise fall back to argv[0].  Provided the application
253 		 * is already mapped, the process is the application, so
254 		 * simplify the application name for use in any diagnostics.
255 		 */
256 		if (execname)
257 			argvname = execname;
258 		else if (argv[0])
259 			argvname = execname = argv[0];
260 		else
261 			argvname = execname = (char *)MSG_INTL(MSG_STR_UNKNOWN);
262 
263 		if (fd == -1) {
264 			if ((str = strrchr(argvname, '/')) != NULL)
265 				procname = ++str;
266 			else
267 				procname = argvname;
268 		}
269 
270 		/*
271 		 * At this point, we don't know the runtime linkers full path
272 		 * name.  The _rtldname passed to us is the SONAME of the
273 		 * runtime linker, which is typically /lib/ld.so.1 no matter
274 		 * what the full path is.   Use this for now, we'll reset the
275 		 * runtime linkers name once the application is analyzed.
276 		 */
277 		if (_rtldname) {
278 			if ((str = strrchr(_rtldname, '/')) != NULL)
279 				rtldname = ++str;
280 			else
281 				rtldname = _rtldname;
282 		} else
283 			rtldname = (char *)MSG_INTL(MSG_STR_UNKNOWN);
284 
285 		/* exec() brought in two objects for us. Count the second one */
286 		cnt_map++;
287 	}
288 
289 	/*
290 	 * Initialize any global variables.
291 	 */
292 	at_flags = _flags;
293 
294 	if ((org_scapset->sc_plat = _platform) != NULL)
295 		org_scapset->sc_platsz = strlen(_platform);
296 
297 	if (org_scapset->sc_plat == NULL)
298 		platform_name(org_scapset);
299 	if (org_scapset->sc_mach == NULL)
300 		machine_name(org_scapset);
301 
302 	/*
303 	 * If pagesize is unspecified find its value.
304 	 */
305 	if ((syspagsz = _syspagsz) == 0)
306 		syspagsz = _sysconfig(_CONFIG_PAGESIZE);
307 
308 	/*
309 	 * Add the unused portion of the last data page to the free space list.
310 	 * The page size must be set before doing this.  Here, _end refers to
311 	 * the end of the runtime linkers bss.  Note that we do not use the
312 	 * unused data pages from any included .so's to supplement this free
313 	 * space as badly behaved .os's may corrupt this data space, and in so
314 	 * doing ruin our data.
315 	 */
316 	eaddr = S_DROUND((size_t)&_end);
317 	esize = eaddr % syspagsz;
318 	if (esize) {
319 		esize = syspagsz - esize;
320 		addfree((void *)eaddr, esize);
321 	}
322 
323 	/*
324 	 * Establish initial link-map list flags, and link-map list alists.
325 	 */
326 	if (alist_append(&lml_main.lm_lists, NULL, sizeof (Lm_cntl),
327 	    AL_CNT_LMLISTS) == NULL)
328 		return (0);
329 	lml_main.lm_flags |= LML_FLG_BASELM;
330 	lml_main.lm_lmid = LM_ID_BASE;
331 	lml_main.lm_lmidstr = (char *)MSG_ORIG(MSG_LMID_BASE);
332 
333 	if (alist_append(&lml_rtld.lm_lists, NULL, sizeof (Lm_cntl),
334 	    AL_CNT_LMLISTS) == NULL)
335 		return (0);
336 	lml_rtld.lm_flags |= (LML_FLG_RTLDLM | LML_FLG_NOAUDIT |
337 	    LML_FLG_HOLDLOCK);
338 	lml_rtld.lm_lmid = LM_ID_LDSO;
339 	lml_rtld.lm_lmidstr = (char *)MSG_ORIG(MSG_LMID_LDSO);
340 
341 	/*
342 	 * Determine whether we have a secure executable.
343 	 */
344 	security(uid, euid, gid, egid, auxflags);
345 
346 	/*
347 	 * Make an initial pass of environment variables to pick off those
348 	 * related to locale processing.  At the same time, collect and save
349 	 * any LD_XXXX variables for later processing.  Note that this later
350 	 * processing will be skipped if ld.so.1 is invoked from the command
351 	 * line with -e LD_NOENVIRON.
352 	 */
353 	if (envp && (readenv_user((const char **)envp, &ealp) == 1))
354 		return (0);
355 
356 	/*
357 	 * If ld.so.1 has been invoked directly, process its arguments.
358 	 */
359 	if (ldsoexec) {
360 		/*
361 		 * Process any arguments that are specific to ld.so.1, and
362 		 * reorganize the process stack to effectively remove ld.so.1
363 		 * from the stack.  Reinitialize the environment pointer, as
364 		 * this pointer may have been shifted after skipping ld.so.1's
365 		 * arguments.
366 		 */
367 		if (rtld_getopt(argv, &envp, &auxv, &(lml_main.lm_flags),
368 		    &(lml_main.lm_tflags), (aoutdyn != 0)) == 1) {
369 			eprintf(&lml_main, ERR_NONE, MSG_INTL(MSG_USG_BADOPT));
370 			return (0);
371 		}
372 		_environ = envp;
373 
374 		/*
375 		 * Open the object that ld.so.1 is to execute.
376 		 */
377 		argvname = execname = argv[0];
378 
379 		if ((fd = open(argvname, O_RDONLY)) == -1) {
380 			int	err = errno;
381 			eprintf(&lml_main, ERR_FATAL, MSG_INTL(MSG_SYS_OPEN),
382 			    argvname, strerror(err));
383 			return (0);
384 		}
385 	}
386 
387 	/*
388 	 * Having processed any ld.so.1 command line options, return to process
389 	 * any LD_XXXX environment variables.
390 	 */
391 	if (ealp) {
392 		if (((rtld_flags & RT_FL_NOENVIRON) == 0) &&
393 		    (procenv_user(ealp, &(lml_main.lm_flags),
394 		    &(lml_main.lm_tflags), (aoutdyn != 0)) == 1))
395 			return (0);
396 		free(ealp);
397 	}
398 
399 	/*
400 	 * Initialize a hardware capability descriptor for use in comparing
401 	 * each loaded object.  The aux vector must provide AF_SUN_HWCAPVERIFY,
402 	 * as prior to this setting any hardware capabilities that were found
403 	 * could not be relied upon.
404 	 */
405 	if (auxflags & AF_SUN_HWCAPVERIFY) {
406 		rtld_flags2 |= RT_FL2_HWCAP;
407 		org_scapset->sc_hw_1 = (Xword)hwcap_1;
408 	}
409 
410 	/*
411 	 * Create a mapping descriptor for ld.so.1.  We can determine our
412 	 * two segments information from known symbols.
413 	 */
414 	if ((mpp = calloc(2, sizeof (mmapobj_result_t))) == NULL)
415 		return (0);
416 	mpp[0].mr_addr = (caddr_t)M_PTRUNC(ld_base);
417 	mpp[0].mr_msize = (caddr_t)&_etext - mpp[0].mr_addr;
418 	mpp[0].mr_fsize = mpp[0].mr_msize;
419 	mpp[0].mr_prot = (PROT_READ | PROT_EXEC);
420 
421 	mpp[1].mr_addr = (caddr_t)M_PTRUNC((uintptr_t)&r_debug);
422 	mpp[1].mr_msize = (caddr_t)&_end - mpp[1].mr_addr;
423 	mpp[1].mr_fsize = (caddr_t)&_edata - mpp[1].mr_addr;
424 	mpp[1].mr_prot = (PROT_READ | PROT_WRITE | PROT_EXEC);
425 
426 	if ((fdr.fd_nname = stravl_insert(_rtldname, 0, 0, 0)) == NULL)
427 		return (0);
428 	if ((rlmp = elf_new_lmp(&lml_rtld, ALIST_OFF_DATA, &fdr,
429 	    (Addr)mpp->mr_addr, (size_t)((uintptr_t)eaddr - (uintptr_t)ld_base),
430 	    NULL, NULL)) == NULL)
431 		return (0);
432 
433 	MMAPS(rlmp) = mpp;
434 	MMAPCNT(rlmp) = 2;
435 	PADSTART(rlmp) = (ulong_t)mpp[0].mr_addr;
436 	PADIMLEN(rlmp) = (ulong_t)mpp[0].mr_addr + (ulong_t)mpp[1].mr_addr +
437 	    (ulong_t)mpp[1].mr_msize;
438 
439 	MODE(rlmp) |= (RTLD_LAZY | RTLD_NODELETE | RTLD_GLOBAL | RTLD_WORLD);
440 	FLAGS(rlmp) |= (FLG_RT_ANALYZED | FLG_RT_RELOCED | FLG_RT_INITDONE |
441 	    FLG_RT_INITCLCT | FLG_RT_FINICLCT | FLG_RT_MODESET);
442 
443 	/*
444 	 * Initialize the runtime linkers information.
445 	 */
446 	interp = &_interp;
447 	interp->i_name = (char *)rtldname;
448 	interp->i_faddr = (caddr_t)ADDR(rlmp);
449 	ldso_plt_init(rlmp);
450 
451 	/*
452 	 * Map in the file, if exec has not already done so, or if the file
453 	 * was passed as an argument to an explicit execution of ld.so.1 from
454 	 * the command line.
455 	 */
456 	if (fd != -1) {
457 		/*
458 		 * Map the file.  Once the object is mapped we no longer need
459 		 * the file descriptor.
460 		 */
461 		(void) rtld_fstat(fd, &status);
462 		fdm.fd_oname = argvname;
463 		fdm.fd_ftp = map_obj(&lml_main, &fdm, status.st_size, argvname,
464 		    fd, &rej);
465 		(void) close(fd);
466 
467 		if (fdm.fd_ftp == NULL) {
468 			Conv_reject_desc_buf_t rej_buf;
469 
470 			eprintf(&lml_main, ERR_FATAL,
471 			    MSG_INTL(err_reject[rej.rej_type]), argvname,
472 			    conv_reject_desc(&rej, &rej_buf, M_MACH));
473 			return (0);
474 		}
475 
476 		/*
477 		 * Finish processing the loading of the file.
478 		 */
479 		if ((fdm.fd_nname = stravl_insert(argvname, 0, 0, 0)) == NULL)
480 			return (0);
481 		fdm.fd_dev = status.st_dev;
482 		fdm.fd_ino = status.st_ino;
483 
484 		if ((mlmp = load_file(&lml_main, ALIST_OFF_DATA, &fdm,
485 		    NULL)) == NULL)
486 			return (0);
487 
488 		/*
489 		 * We now have a process name for error diagnostics.
490 		 */
491 		if ((str = strrchr(argvname, '/')) != NULL)
492 			procname = ++str;
493 		else
494 			procname = argvname;
495 
496 		if (ldsoexec) {
497 			mmapobj_result_t	*mpp = MMAPS(mlmp);
498 			uint_t			mnum, mapnum = MMAPCNT(mlmp);
499 			void			*brkbase = NULL;
500 
501 			/*
502 			 * Since ld.so.1 was the primary executed object - the
503 			 * brk() base has not yet been initialized, we need to
504 			 * initialize it.  For an executable, initialize it to
505 			 * the end of the object.  For a shared object (ET_DYN)
506 			 * initialize it to the first page in memory.
507 			 */
508 			for (mnum = 0; mnum < mapnum; mnum++, mpp++)
509 				brkbase = mpp->mr_addr + mpp->mr_msize;
510 
511 			if (brkbase == NULL)
512 				brkbase = (void *)syspagsz;
513 
514 			if (_brk_unlocked(brkbase) == -1) {
515 				int	err = errno;
516 
517 				eprintf(&lml_main, ERR_FATAL,
518 				    MSG_INTL(MSG_SYS_BRK), argvname,
519 				    strerror(err));
520 				return (0);
521 			}
522 		}
523 	} else {
524 		/*
525 		 * Set up function ptr and arguments according to the type
526 		 * of file class the executable is. (Currently only supported
527 		 * types are ELF and a.out format.)  Then create a link map
528 		 * for the executable.
529 		 */
530 		if (aoutdyn) {
531 #ifdef A_OUT
532 			mmapobj_result_t	*mpp;
533 
534 			/*
535 			 * Create a mapping structure sufficient to describe
536 			 * a single two segments.  The ADDR() of the a.out is
537 			 * established as 0, which is required but the AOUT
538 			 * relocation code.
539 			 */
540 			if ((mpp =
541 			    calloc(sizeof (mmapobj_result_t), 2)) == NULL)
542 				return (0);
543 
544 			if ((fdm.fd_nname =
545 			    stravl_insert(execname, 0, 0, 0)) == NULL)
546 				return (0);
547 			if ((mlmp = aout_new_lmp(&lml_main, ALIST_OFF_DATA,
548 			    &fdm, 0, 0, aoutdyn, NULL)) == NULL)
549 				return (0);
550 
551 			/*
552 			 * Establish the true mapping information for the a.out.
553 			 */
554 			if (aout_get_mmap(&lml_main, mpp)) {
555 				free(mpp);
556 				return (0);
557 			}
558 
559 			MSIZE(mlmp) =
560 			    (size_t)(mpp[1].mr_addr + mpp[1].mr_msize) -
561 			    S_ALIGN((size_t)mpp[0].mr_addr, syspagsz);
562 			MMAPS(mlmp) = mpp;
563 			MMAPCNT(mlmp) = 2;
564 			PADSTART(mlmp) = (ulong_t)mpp->mr_addr;
565 			PADIMLEN(mlmp) = mpp->mr_msize;
566 
567 			/*
568 			 * Disable any object configuration cache (BCP apps
569 			 * bring in sbcp which can benefit from any object
570 			 * cache, but both the app and sbcp can't use the same
571 			 * objects).
572 			 */
573 			rtld_flags |= RT_FL_NOOBJALT;
574 
575 			/*
576 			 * Make sure no-direct bindings are in effect.
577 			 */
578 			lml_main.lm_tflags |= LML_TFLG_NODIRECT;
579 #else
580 			eprintf(&lml_main, ERR_FATAL,
581 			    MSG_INTL(MSG_ERR_REJ_UNKFILE), argvname);
582 			return (0);
583 #endif
584 		} else if (phdr) {
585 			Phdr			*pptr;
586 			Off			i_offset = 0;
587 			Addr			base = 0;
588 			ulong_t			phsize;
589 			mmapobj_result_t	*mpp, *fmpp, *hmpp = NULL;
590 			uint_t			mapnum = 0;
591 			int			i;
592 			size_t			msize;
593 
594 			/*
595 			 * Using the executables phdr address determine the base
596 			 * address of the input file.  NOTE, this assumes the
597 			 * program headers and elf header are part of the same
598 			 * mapped segment.  Although this has held for many
599 			 * years now, it might be more flexible if the kernel
600 			 * gave use the ELF headers start address, rather than
601 			 * the Program headers.
602 			 *
603 			 * Determine from the ELF header if we're been called
604 			 * from a shared object or dynamic executable.  If the
605 			 * latter, then any addresses within the object are used
606 			 * as is.  Addresses within shared objects must be added
607 			 * to the process's base address.
608 			 */
609 			ehdr = (Ehdr *)((Addr)phdr - phdr->p_offset);
610 			phsize = ehdr->e_phentsize;
611 			if (ehdr->e_type == ET_DYN)
612 				base = (Addr)ehdr;
613 
614 			/*
615 			 * Allocate a mapping array to retain mapped segment
616 			 * information.
617 			 */
618 			if ((fmpp = mpp = calloc(ehdr->e_phnum,
619 			    sizeof (mmapobj_result_t))) == NULL)
620 				return (0);
621 
622 			/*
623 			 * Extract the needed information from the segment
624 			 * headers.
625 			 */
626 			for (i = 0, pptr = phdr; i < ehdr->e_phnum; i++) {
627 				if (pptr->p_type == PT_INTERP) {
628 					i_offset = pptr->p_offset;
629 					interp->i_faddr =
630 					    (caddr_t)interp_base;
631 				}
632 				if ((pptr->p_type == PT_LOAD) &&
633 				    (pptr->p_filesz || pptr->p_memsz)) {
634 					int	perm = (PROT_READ | PROT_EXEC);
635 					size_t	off;
636 
637 					if (i_offset && pptr->p_filesz &&
638 					    (i_offset >= pptr->p_offset) &&
639 					    (i_offset <=
640 					    (pptr->p_memsz + pptr->p_offset))) {
641 						interp->i_name = (char *)
642 						    pptr->p_vaddr + i_offset -
643 						    pptr->p_offset + base;
644 						i_offset = 0;
645 					}
646 
647 					if (pptr->p_flags & PF_W)
648 						perm |= PROT_WRITE;
649 
650 					/*
651 					 * Retain segments mapping info.  Round
652 					 * each segment to a page boundary, as
653 					 * this insures addresses are suitable
654 					 * for mprotect() if required.
655 					 */
656 					off = pptr->p_vaddr + base;
657 					if (hmpp == NULL) {
658 						hmpp = mpp;
659 						mpp->mr_addr = (caddr_t)ehdr;
660 					} else
661 						mpp->mr_addr = (caddr_t)off;
662 
663 					off -= (size_t)(uintptr_t)mpp->mr_addr;
664 					mpp->mr_msize = pptr->p_memsz + off;
665 					mpp->mr_fsize = pptr->p_filesz + off;
666 					mpp->mr_prot = perm;
667 
668 					mpp++, mapnum++;
669 				}
670 
671 				pptr = (Phdr *)((ulong_t)pptr + phsize);
672 			}
673 
674 			mpp--;
675 			msize = (size_t)(mpp->mr_addr + mpp->mr_msize) -
676 			    S_ALIGN((size_t)fmpp->mr_addr, syspagsz);
677 
678 			if ((fdm.fd_nname =
679 			    stravl_insert(execname, 0, 0, 0)) == NULL)
680 				return (0);
681 			if ((mlmp = elf_new_lmp(&lml_main, ALIST_OFF_DATA, &fdm,
682 			    (Addr)hmpp->mr_addr, msize, NULL, NULL)) == NULL)
683 				return (0);
684 
685 			MMAPS(mlmp) = fmpp;
686 			MMAPCNT(mlmp) = mapnum;
687 			PADSTART(mlmp) = (ulong_t)fmpp->mr_addr;
688 			PADIMLEN(mlmp) = (ulong_t)fmpp->mr_addr +
689 			    (ulong_t)mpp->mr_addr + (ulong_t)mpp->mr_msize;
690 		}
691 	}
692 
693 	/*
694 	 * Establish the interpretors name as that defined within the initial
695 	 * object (executable).  This provides for ORIGIN processing of ld.so.1
696 	 * dependencies.  Note, the NAME() of the object remains that which was
697 	 * passed to us as the SONAME on execution.
698 	 */
699 	if (ldsoexec == 0) {
700 		size_t	len = strlen(interp->i_name);
701 
702 		if (expand(&interp->i_name, &len, 0, 0,
703 		    (PD_TKN_ISALIST | PD_TKN_CAP), rlmp) & PD_TKN_RESOLVED)
704 			fdr.fd_flags |= FLG_FD_RESOLVED;
705 	}
706 	fdr.fd_pname = interp->i_name;
707 	(void) fullpath(rlmp, &fdr);
708 
709 	/*
710 	 * The runtime linker acts as a filtee for various dl*() functions that
711 	 * are defined in libc (and libdl).  Make sure this standard name for
712 	 * the runtime linker is also registered in the FullPathNode AVL tree.
713 	 */
714 	(void) fpavl_insert(&lml_rtld, rlmp, _rtldname, 0);
715 
716 	/*
717 	 * Having established the true runtime linkers name, simplify the name
718 	 * for error diagnostics.
719 	 */
720 	if ((str = strrchr(PATHNAME(rlmp), '/')) != NULL)
721 		rtldname = ++str;
722 	else
723 		rtldname = PATHNAME(rlmp);
724 
725 	/*
726 	 * Expand the fullpath name of the application.  This typically occurs
727 	 * as a part of loading an object, but as the kernel probably mapped
728 	 * it in, complete this processing now.
729 	 */
730 	(void) fullpath(mlmp, 0);
731 
732 	/*
733 	 * Some troublesome programs will change the value of argv[0].  Dupping
734 	 * the process string protects us, and insures the string is left in
735 	 * any core files.
736 	 */
737 	if ((str = (char *)strdup(procname)) == NULL)
738 		return (0);
739 	procname = str;
740 
741 	FLAGS(mlmp) |= (FLG_RT_ISMAIN | FLG_RT_MODESET);
742 	FLAGS1(mlmp) |= FL1_RT_USED;
743 
744 	/*
745 	 * It's the responsibility of MAIN(crt0) to call it's _init and _fini
746 	 * section, therefore null out any INIT/FINI so that this object isn't
747 	 * collected during tsort processing.  And, if the application has no
748 	 * initarray or finiarray we can economize on establishing bindings.
749 	 */
750 	INIT(mlmp) = FINI(mlmp) = NULL;
751 	if ((INITARRAY(mlmp) == NULL) && (FINIARRAY(mlmp) == NULL))
752 		FLAGS1(mlmp) |= FL1_RT_NOINIFIN;
753 
754 	/*
755 	 * Identify lddstub if necessary.
756 	 */
757 	if (lml_main.lm_flags & LML_FLG_TRC_LDDSTUB)
758 		FLAGS1(mlmp) |= FL1_RT_LDDSTUB;
759 
760 	/*
761 	 * Retain our argument information for use in dlinfo.
762 	 */
763 	argsinfo.dla_argv = argv--;
764 	argsinfo.dla_argc = (long)*argv;
765 	argsinfo.dla_envp = envp;
766 	argsinfo.dla_auxv = auxv;
767 
768 	(void) enter(0);
769 
770 	/*
771 	 * Add our two main link-maps to the dynlm_list
772 	 */
773 	if (aplist_append(&dynlm_list, &lml_main, AL_CNT_DYNLIST) == NULL)
774 		return (0);
775 
776 	if (aplist_append(&dynlm_list, &lml_rtld, AL_CNT_DYNLIST) == NULL)
777 		return (0);
778 
779 	/*
780 	 * Reset the link-map counts for both lists.  The init count is used to
781 	 * track how many objects have pending init sections, this gets incre-
782 	 * mented each time an object is relocated.  Since ld.so.1 relocates
783 	 * itself, it's init count will remain zero.
784 	 * The object count is used to track how many objects have pending fini
785 	 * sections, as ld.so.1 handles its own fini we can zero its count.
786 	 */
787 	lml_main.lm_obj = 1;
788 	lml_rtld.lm_obj = 0;
789 
790 	/*
791 	 * Initialize debugger information structure.  Some parts of this
792 	 * structure were initialized statically.
793 	 */
794 	r_debug.rtd_rdebug.r_map = (Link_map *)lml_main.lm_head;
795 	r_debug.rtd_rdebug.r_ldsomap = (Link_map *)lml_rtld.lm_head;
796 	r_debug.rtd_rdebug.r_ldbase = r_debug.rtd_rdebug.r_ldsomap->l_addr;
797 	r_debug.rtd_dynlmlst = &dynlm_list;
798 
799 	/*
800 	 * Determine the dev/inode information for the executable to complete
801 	 * load_so() checking for those who might dlopen(a.out).
802 	 */
803 	if (rtld_stat(PATHNAME(mlmp), &status) == 0) {
804 		STDEV(mlmp) = status.st_dev;
805 		STINO(mlmp) = status.st_ino;
806 	}
807 
808 	/*
809 	 * Initialize any configuration information.
810 	 */
811 	if (!(rtld_flags & RT_FL_NOCFG)) {
812 		if ((features = elf_config(mlmp, (aoutdyn != 0))) == -1)
813 			return (0);
814 	}
815 
816 #if	defined(_ELF64)
817 	/*
818 	 * If this is a 64-bit process, determine whether this process has
819 	 * restricted the process address space to 32-bits.  Any dependencies
820 	 * that are restricted to a 32-bit address space can only be loaded if
821 	 * the executable has established this requirement.
822 	 */
823 	if (CAPSET(mlmp).sc_sf_1 & SF1_SUNW_ADDR32)
824 		rtld_flags2 |= RT_FL2_ADDR32;
825 #endif
826 	/*
827 	 * Establish any alternative capabilities, and validate this object
828 	 * if it defines it's own capabilities information.
829 	 */
830 	if (cap_alternative() == 0)
831 		return (0);
832 
833 	if (cap_check_lmp(mlmp, &rej) == 0) {
834 		if (lml_main.lm_flags & LML_FLG_TRC_ENABLE) {
835 			/* LINTED */
836 			(void) printf(MSG_INTL(ldd_warn[rej.rej_type]),
837 			    NAME(mlmp), rej.rej_str);
838 		} else {
839 			/* LINTED */
840 			eprintf(&lml_main, ERR_FATAL,
841 			    MSG_INTL(err_reject[rej.rej_type]),
842 			    NAME(mlmp), rej.rej_str);
843 			return (0);
844 		}
845 	}
846 
847 	/*
848 	 * Establish the modes of the initial object.  These modes are
849 	 * propagated to any preloaded objects and explicit shared library
850 	 * dependencies.
851 	 *
852 	 * If we're generating a configuration file using crle(1), remove
853 	 * any RTLD_NOW use, as we don't want to trigger any relocation proc-
854 	 * essing during crle(1)'s first past (this would just be unnecessary
855 	 * overhead).  Any filters are explicitly loaded, and thus RTLD_NOW is
856 	 * not required to trigger filter loading.
857 	 *
858 	 * Note, RTLD_NOW may have been established during analysis of the
859 	 * application had the application been built -z now.
860 	 */
861 	MODE(mlmp) |= (RTLD_NODELETE | RTLD_GLOBAL | RTLD_WORLD);
862 
863 	if (rtld_flags & RT_FL_CONFGEN) {
864 		MODE(mlmp) |= RTLD_CONFGEN;
865 		MODE(mlmp) &= ~RTLD_NOW;
866 		rtld_flags2 &= ~RT_FL2_BINDNOW;
867 	}
868 
869 	if ((MODE(mlmp) & RTLD_NOW) == 0) {
870 		if (rtld_flags2 & RT_FL2_BINDNOW)
871 			MODE(mlmp) |= RTLD_NOW;
872 		else
873 			MODE(mlmp) |= RTLD_LAZY;
874 	}
875 
876 	/*
877 	 * If debugging was requested initialize things now that any cache has
878 	 * been established.  A user can specify LD_DEBUG=help to discover the
879 	 * list of debugging tokens available without running the application.
880 	 * However, don't allow this setting from a configuration file.
881 	 *
882 	 * Note, to prevent recursion issues caused by loading and binding the
883 	 * debugging libraries themselves, a local debugging descriptor is
884 	 * initialized.  Once the debugging setup has completed, this local
885 	 * descriptor is copied to the global descriptor which effectively
886 	 * enables diagnostic output.
887 	 *
888 	 * Ignore any debugging request if we're being monitored by a process
889 	 * that expects the old getpid() initialization handshake.
890 	 */
891 	if ((rpl_debug || prm_debug) && ((rtld_flags & RT_FL_DEBUGGER) == 0)) {
892 		Dbg_desc	_dbg_desc = {0};
893 		struct timeval	total = DBG_TOTALTIME;
894 		struct timeval	delta = DBG_DELTATIME;
895 
896 		if (rpl_debug) {
897 			if (dbg_setup(rpl_debug, &_dbg_desc) == 0)
898 				return (0);
899 			if (_dbg_desc.d_extra & DBG_E_HELP_EXIT)
900 				rtldexit(&lml_main, 0);
901 		}
902 		if (prm_debug)
903 			(void) dbg_setup(prm_debug, &_dbg_desc);
904 
905 		*dbg_desc = _dbg_desc;
906 		DBG_TOTALTIME = total;
907 		DBG_DELTATIME = delta;
908 	}
909 
910 	/*
911 	 * Now that debugging is enabled generate any diagnostics from any
912 	 * previous events.
913 	 */
914 	if (DBG_ENABLED) {
915 		DBG_CALL(Dbg_cap_val(&lml_main, org_scapset, alt_scapset,
916 		    M_MACH));
917 		DBG_CALL(Dbg_file_config_dis(&lml_main, config->c_name,
918 		    features));
919 
920 		DBG_CALL(Dbg_file_ldso(rlmp, envp, auxv,
921 		    LIST(rlmp)->lm_lmidstr, ALIST_OFF_DATA));
922 
923 		if (THIS_IS_ELF(mlmp)) {
924 			DBG_CALL(Dbg_file_elf(&lml_main, PATHNAME(mlmp),
925 			    ADDR(mlmp), MSIZE(mlmp), LIST(mlmp)->lm_lmidstr,
926 			    ALIST_OFF_DATA));
927 		} else {
928 			DBG_CALL(Dbg_file_aout(&lml_main, PATHNAME(mlmp),
929 			    ADDR(mlmp), MSIZE(mlmp), LIST(mlmp)->lm_lmidstr,
930 			    ALIST_OFF_DATA));
931 		}
932 	}
933 
934 	/*
935 	 * Enable auditing.
936 	 */
937 	if (rpl_audit || prm_audit || profile_lib) {
938 		int		ndx;
939 		const char	*aud[3];
940 
941 		aud[0] = rpl_audit;
942 		aud[1] = prm_audit;
943 		aud[2] = profile_lib;
944 
945 		/*
946 		 * Any global auditing (set using LD_AUDIT or LD_PROFILE) that
947 		 * can't be established is non-fatal.
948 		 */
949 		if ((auditors = calloc(1, sizeof (Audit_desc))) == NULL)
950 			return (0);
951 
952 		for (ndx = 0; ndx < 3; ndx++) {
953 			if (aud[ndx]) {
954 				if ((auditors->ad_name =
955 				    strdup(aud[ndx])) == NULL)
956 					return (0);
957 				rtld_flags2 |= RT_FL2_FTL2WARN;
958 				(void) audit_setup(mlmp, auditors,
959 				    PD_FLG_EXTLOAD, NULL);
960 				rtld_flags2 &= ~RT_FL2_FTL2WARN;
961 			}
962 		}
963 		lml_main.lm_tflags |= auditors->ad_flags;
964 	}
965 	if (AUDITORS(mlmp)) {
966 		/*
967 		 * Any object required auditing (set with a DT_DEPAUDIT dynamic
968 		 * entry) that can't be established is fatal.
969 		 */
970 		if (FLAGS1(mlmp) & FL1_RT_GLOBAUD) {
971 			/*
972 			 * If this object requires global auditing, use the
973 			 * local auditing information to set the global
974 			 * auditing descriptor.  The effect is that a
975 			 * DT_DEPAUDIT act as an LD_AUDIT.
976 			 */
977 			if ((auditors == NULL) && ((auditors = calloc(1,
978 			    sizeof (Audit_desc))) == NULL))
979 				return (0);
980 
981 			auditors->ad_name = AUDITORS(mlmp)->ad_name;
982 			if (audit_setup(mlmp, auditors, 0, NULL) == 0)
983 				return (0);
984 			lml_main.lm_tflags |= auditors->ad_flags;
985 
986 			/*
987 			 * Clear the local auditor information.
988 			 */
989 			free((void *) AUDITORS(mlmp));
990 			AUDITORS(mlmp) = NULL;
991 
992 		} else {
993 			/*
994 			 * Establish any local auditing.
995 			 */
996 			if (audit_setup(mlmp, AUDITORS(mlmp), 0, NULL) == 0)
997 				return (0);
998 
999 			AFLAGS(mlmp) |= AUDITORS(mlmp)->ad_flags;
1000 			lml_main.lm_flags |= LML_FLG_LOCAUDIT;
1001 		}
1002 	}
1003 
1004 	/*
1005 	 * Explicitly add the initial object and ld.so.1 to those objects being
1006 	 * audited.  Note, although the ld.so.1 link-map isn't auditable,
1007 	 * establish a cookie for ld.so.1 as this may be bound to via the
1008 	 * dl*() family.
1009 	 */
1010 	if ((lml_main.lm_tflags | AFLAGS(mlmp)) & LML_TFLG_AUD_MASK) {
1011 		if (((audit_objopen(mlmp, mlmp) == 0) ||
1012 		    (audit_objopen(mlmp, rlmp) == 0)) &&
1013 		    (AFLAGS(mlmp) & LML_TFLG_AUD_MASK))
1014 			return (0);
1015 	}
1016 
1017 	/*
1018 	 * Map in any preloadable shared objects.  Establish the caller as the
1019 	 * head of the main link-map list.  In the case of being exercised from
1020 	 * lddstub, the caller gets reassigned to the first target shared object
1021 	 * so as to provide intuitive diagnostics from ldd().
1022 	 *
1023 	 * Note, it is valid to preload a 4.x shared object with a 5.0
1024 	 * executable (or visa-versa), as this functionality is required by
1025 	 * ldd(1).
1026 	 */
1027 	clmp = mlmp;
1028 	if (rpl_preload && (preload(rpl_preload, mlmp, &clmp) == 0))
1029 		return (0);
1030 	if (prm_preload && (preload(prm_preload, mlmp, &clmp) == 0))
1031 		return (0);
1032 
1033 	/*
1034 	 * Load all dependent (needed) objects.
1035 	 */
1036 	if (analyze_lmc(&lml_main, ALIST_OFF_DATA, mlmp, NULL) == NULL)
1037 		return (0);
1038 
1039 	/*
1040 	 * Relocate all the dependencies we've just added.
1041 	 *
1042 	 * If this process has been established via crle(1), the environment
1043 	 * variable LD_CONFGEN will have been set.  crle(1) may create this
1044 	 * process twice.  The first time crle only needs to gather dependency
1045 	 * information.  The second time, is to dldump() the images.
1046 	 *
1047 	 * If we're only gathering dependencies, relocation is unnecessary.
1048 	 * As crle(1) may be building an arbitrary family of objects, they may
1049 	 * not fully relocate either.  Hence the relocation phase is not carried
1050 	 * out now, but will be called by crle(1) once all objects have been
1051 	 * loaded.
1052 	 */
1053 	if ((rtld_flags & RT_FL_CONFGEN) == 0) {
1054 
1055 		DBG_CALL(Dbg_util_nl(&lml_main, DBG_NL_STD));
1056 
1057 		if (relocate_lmc(&lml_main, ALIST_OFF_DATA, mlmp,
1058 		    mlmp, NULL) == 0)
1059 			return (0);
1060 
1061 		/*
1062 		 * Inform the debuggers that basic process initialization is
1063 		 * complete, and that the state of ld.so.1 (link-map lists,
1064 		 * etc.) is stable.  This handshake enables the debugger to
1065 		 * initialize themselves, and consequently allows the user to
1066 		 * set break points in .init code.
1067 		 *
1068 		 * Most new debuggers use librtld_db to monitor activity events.
1069 		 * Older debuggers indicated their presence by setting the
1070 		 * DT_DEBUG entry in the dynamic executable (see elf_new_lm()).
1071 		 * In this case, getpid() is called so that the debugger can
1072 		 * catch the system call.  This old mechanism has some
1073 		 * restrictions, as getpid() should not be called prior to
1074 		 * basic process initialization being completed.  This
1075 		 * restriction has become increasingly difficult to maintain,
1076 		 * as the use of auditors, LD_DEBUG, and the initialization
1077 		 * handshake with libc can result in "premature" getpid()
1078 		 * calls.  The use of this getpid() handshake is expected to
1079 		 * disappear at some point in the future, and there is intent
1080 		 * to work towards that goal.
1081 		 */
1082 		rd_event(&lml_main, RD_DLACTIVITY, RT_CONSISTENT);
1083 		rd_event(&lml_rtld, RD_DLACTIVITY, RT_CONSISTENT);
1084 
1085 		if (rtld_flags & RT_FL_DEBUGGER) {
1086 			r_debug.rtd_rdebug.r_flags |= RD_FL_ODBG;
1087 			(void) getpid();
1088 		}
1089 	}
1090 
1091 	/*
1092 	 * Indicate preinit activity, and call any auditing routines.  These
1093 	 * routines are called before initializing any threads via libc, or
1094 	 * before collecting the complete set of .inits on the primary link-map.
1095 	 * Although most libc interfaces are encapsulated in local routines
1096 	 * within libc, they have been known to escape (ie. call a .plt).  As
1097 	 * the appcert auditor uses preinit as a trigger to establish some
1098 	 * external interfaces to the main link-maps libc, we need to activate
1099 	 * this trigger before exercising any code within libc.  Additionally,
1100 	 * I wouldn't put it past an auditor to add additional objects to the
1101 	 * primary link-map.  Hence, we collect .inits after the audit call.
1102 	 */
1103 	rd_event(&lml_main, RD_PREINIT, 0);
1104 
1105 	if ((lml_main.lm_tflags | AFLAGS(mlmp)) & LML_TFLG_AUD_ACTIVITY)
1106 		audit_activity(mlmp, LA_ACT_CONSISTENT);
1107 	if ((lml_main.lm_tflags | AFLAGS(mlmp)) & LML_TFLG_AUD_PREINIT)
1108 		audit_preinit(mlmp);
1109 
1110 	/*
1111 	 * If we're creating initial configuration information, we're done
1112 	 * now that the auditing step has been called.
1113 	 */
1114 	if (rtld_flags & RT_FL_CONFGEN) {
1115 		leave(LIST(mlmp), 0);
1116 		return (mlmp);
1117 	}
1118 
1119 	/*
1120 	 * Sort the .init sections of all objects we've added.  If we're
1121 	 * tracing we only need to execute this under ldd(1) with the -i or -u
1122 	 * options.
1123 	 */
1124 	lmflags = lml_main.lm_flags;
1125 	if (((lmflags & LML_FLG_TRC_ENABLE) == 0) ||
1126 	    (lmflags & (LML_FLG_TRC_INIT | LML_FLG_TRC_UNREF))) {
1127 		if ((tobj = tsort(mlmp, LIST(mlmp)->lm_init,
1128 		    RT_SORT_REV)) == (Rt_map **)S_ERROR)
1129 			return (0);
1130 	}
1131 
1132 	/*
1133 	 * If we are tracing we're done.  This is the one legitimate use of a
1134 	 * direct call to rtldexit() rather than return, as we don't want to
1135 	 * return and jump to the application.
1136 	 */
1137 	if (lmflags & LML_FLG_TRC_ENABLE) {
1138 		unused(&lml_main);
1139 		rtldexit(&lml_main, 0);
1140 	}
1141 
1142 	/*
1143 	 * Check if this instance of the linker should have a primary link
1144 	 * map.  This flag allows multiple copies of the -same- -version-
1145 	 * of the linker (and libc) to run in the same address space.
1146 	 *
1147 	 * Without this flag we only support one copy of the linker in a
1148 	 * process because by default the linker will always try to
1149 	 * initialize at one primary link map  The copy of libc which is
1150 	 * initialized on a primary link map will initialize global TLS
1151 	 * data which can be shared with other copies of libc in the
1152 	 * process.  The problem is that if there is more than one copy
1153 	 * of the linker, only one copy should link libc onto a primary
1154 	 * link map, otherwise libc will attempt to re-initialize global
1155 	 * TLS data.  So when a copy of the linker is loaded with this
1156 	 * flag set, it will not initialize any primary link maps since
1157 	 * presumably another copy of the linker will do this.
1158 	 *
1159 	 * Note that this flag only allows multiple copies of the -same-
1160 	 * -version- of the linker (and libc) to coexist.  This approach
1161 	 * will not work if we are trying to load different versions of
1162 	 * the linker and libc into the same process.  The reason for
1163 	 * this is that the format of the global TLS data may not be
1164 	 * the same for different versions of libc.  In this case each
1165 	 * different version of libc must have it's own primary link map
1166 	 * and be able to maintain it's own TLS data.  The only way this
1167 	 * can be done is by carefully managing TLS pointers on transitions
1168 	 * between code associated with each of the different linkers.
1169 	 * Note that this is actually what is done for processes in lx
1170 	 * branded zones.  Although in the lx branded zone case, the
1171 	 * other linker and libc are actually gld and glibc.  But the
1172 	 * same general TLS management mechanism used by the lx brand
1173 	 * would apply to any attempts to run multiple versions of the
1174 	 * solaris linker and libc in a single process.
1175 	 */
1176 	if (auxflags & AF_SUN_NOPLM)
1177 		rtld_flags2 |= RT_FL2_NOPLM;
1178 
1179 	/*
1180 	 * Establish any static TLS for this primary link-map.  Note, regardless
1181 	 * of whether TLS is available, an initial handshake occurs with libc to
1182 	 * indicate we're processing the primary link-map.  Having identified
1183 	 * the primary link-map, initialize threads.
1184 	 */
1185 	if (rt_get_extern(&lml_main, mlmp) == 0)
1186 		return (0);
1187 
1188 	if ((rtld_flags2 & RT_FL2_NOPLM) == 0) {
1189 		if (tls_statmod(&lml_main, mlmp) == 0)
1190 			return (0);
1191 		rt_thr_init(&lml_main);
1192 		rtld_flags2 |= RT_FL2_PLMSETUP;
1193 	} else {
1194 		rt_thr_init(&lml_main);
1195 	}
1196 
1197 	rtld_flags |= RT_FL_APPLIC;
1198 
1199 	/*
1200 	 * Fire all dependencies .init sections.  Identify any unused
1201 	 * dependencies, and leave the runtime linker - effectively calling
1202 	 * the dynamic executables entry point.
1203 	 */
1204 	call_array(PREINITARRAY(mlmp), (uint_t)PREINITARRAYSZ(mlmp), mlmp,
1205 	    SHT_PREINIT_ARRAY);
1206 
1207 	if (tobj)
1208 		call_init(tobj, DBG_INIT_SORT);
1209 
1210 	rd_event(&lml_main, RD_POSTINIT, 0);
1211 
1212 	unused(&lml_main);
1213 
1214 	DBG_CALL(Dbg_util_call_main(mlmp));
1215 
1216 	rtld_flags |= RT_FL_OPERATION;
1217 	leave(LIST(mlmp), 0);
1218 
1219 	return (mlmp);
1220 }
1221