xref: /titanic_51/usr/src/uts/common/exec/elf/elf.c (revision 4bc0a2ef2b7ba50a7a717e7ddbf31472ad28e358)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 
31 #pragma ident	"%Z%%M%	%I%	%E% SMI"
32 
33 #include <sys/types.h>
34 #include <sys/param.h>
35 #include <sys/thread.h>
36 #include <sys/sysmacros.h>
37 #include <sys/signal.h>
38 #include <sys/cred.h>
39 #include <sys/user.h>
40 #include <sys/errno.h>
41 #include <sys/vnode.h>
42 #include <sys/mman.h>
43 #include <sys/kmem.h>
44 #include <sys/proc.h>
45 #include <sys/pathname.h>
46 #include <sys/cmn_err.h>
47 #include <sys/systm.h>
48 #include <sys/elf.h>
49 #include <sys/vmsystm.h>
50 #include <sys/debug.h>
51 #include <sys/auxv.h>
52 #include <sys/exec.h>
53 #include <sys/prsystm.h>
54 #include <vm/as.h>
55 #include <vm/rm.h>
56 #include <vm/seg.h>
57 #include <vm/seg_vn.h>
58 #include <sys/modctl.h>
59 #include <sys/systeminfo.h>
60 #include <sys/vmparam.h>
61 #include <sys/machelf.h>
62 #include <sys/shm_impl.h>
63 #include <sys/archsystm.h>
64 #include <sys/fasttrap.h>
65 #include "elf_impl.h"
66 
67 extern int at_flags;
68 
69 #define	ORIGIN_STR	"ORIGIN"
70 #define	ORIGIN_STR_SIZE	6
71 
72 static int getelfhead(vnode_t *, cred_t *, Ehdr *);
73 static int getelfphdr(vnode_t *, cred_t *, const Ehdr *, caddr_t *, ssize_t *);
74 static int getelfshdr(vnode_t *, cred_t *, const Ehdr *, caddr_t *, ssize_t *,
75     caddr_t *, ssize_t *);
76 static size_t elfsize(Ehdr *, caddr_t, uintptr_t *);
77 static int mapelfexec(vnode_t *, Ehdr *, caddr_t,
78     Phdr **, Phdr **, Phdr **, Phdr **, Phdr *,
79     caddr_t *, caddr_t *, intptr_t *, size_t, long *, size_t *);
80 
81 typedef enum {
82 	STR_CTF,
83 	STR_SYMTAB,
84 	STR_DYNSYM,
85 	STR_STRTAB,
86 	STR_DYNSTR,
87 	STR_SHSTRTAB,
88 	STR_NUM
89 } shstrtype_t;
90 
91 static const char *shstrtab_data[] = {
92 	".SUNW_ctf",
93 	".symtab",
94 	".dynsym",
95 	".strtab",
96 	".dynstr",
97 	".shstrtab"
98 };
99 
100 typedef struct shstrtab {
101 	int	sst_ndx[STR_NUM];
102 	int	sst_cur;
103 } shstrtab_t;
104 
105 static void
106 shstrtab_init(shstrtab_t *s)
107 {
108 	bzero(&s->sst_ndx, sizeof (s->sst_ndx));
109 	s->sst_cur = 1;
110 }
111 
112 static int
113 shstrtab_ndx(shstrtab_t *s, shstrtype_t type)
114 {
115 	int ret;
116 
117 	if ((ret = s->sst_ndx[type]) != 0)
118 		return (ret);
119 
120 	ret = s->sst_ndx[type] = s->sst_cur;
121 	s->sst_cur += strlen(shstrtab_data[type]) + 1;
122 
123 	return (ret);
124 }
125 
126 static size_t
127 shstrtab_size(const shstrtab_t *s)
128 {
129 	return (s->sst_cur);
130 }
131 
132 static void
133 shstrtab_dump(const shstrtab_t *s, char *buf)
134 {
135 	int i, ndx;
136 
137 	*buf = '\0';
138 	for (i = 0; i < STR_NUM; i++) {
139 		if ((ndx = s->sst_ndx[i]) != 0)
140 			(void) strcpy(buf + ndx, shstrtab_data[i]);
141 	}
142 }
143 
144 static int
145 dtrace_safe_phdr(Phdr *phdrp, struct uarg *args, uintptr_t base)
146 {
147 	ASSERT(phdrp->p_type == PT_SUNWDTRACE);
148 
149 	/*
150 	 * See the comment in fasttrap.h for information on how to safely
151 	 * update this program header.
152 	 */
153 	if (phdrp->p_memsz < PT_SUNWDTRACE_SIZE ||
154 	    (phdrp->p_flags & (PF_R | PF_W | PF_X)) != (PF_R | PF_W | PF_X))
155 		return (-1);
156 
157 	args->thrptr = phdrp->p_vaddr + base;
158 
159 	return (0);
160 }
161 
162 /*ARGSUSED*/
163 int
164 elfexec(vnode_t *vp, execa_t *uap, uarg_t *args, intpdata_t *idatap,
165     int level, long *execsz, int setid, caddr_t exec_file, cred_t *cred)
166 {
167 	caddr_t		phdrbase = NULL;
168 	caddr_t 	bssbase = 0;
169 	caddr_t 	brkbase = 0;
170 	size_t		brksize = 0;
171 	ssize_t		dlnsize;
172 	aux_entry_t	*aux;
173 	int		error;
174 	ssize_t		resid;
175 	int		fd = -1;
176 	intptr_t	voffset;
177 	Phdr	*dyphdr = NULL;
178 	Phdr	*stphdr = NULL;
179 	Phdr	*uphdr = NULL;
180 	Phdr	*junk = NULL;
181 	size_t		len;
182 	ssize_t		phdrsize;
183 	int		postfixsize = 0;
184 	int		i, hsize;
185 	Phdr		*phdrp;
186 	Phdr		*dataphdrp = NULL;
187 	Phdr		*dtrphdr;
188 	int		hasu = 0;
189 	int		hasauxv = 0;
190 	int		hasdy = 0;
191 
192 	struct proc *p = ttoproc(curthread);
193 	struct user *up = PTOU(p);
194 	struct bigwad {
195 		Ehdr	ehdr;
196 		aux_entry_t	elfargs[__KERN_NAUXV_IMPL];
197 		char		dl_name[MAXPATHLEN];
198 		char		pathbuf[MAXPATHLEN];
199 		struct vattr	vattr;
200 		struct execenv	exenv;
201 	} *bigwad;	/* kmem_alloc this behemoth so we don't blow stack */
202 	Ehdr	*ehdrp;
203 	char		*dlnp;
204 	char		*pathbufp;
205 	rlim64_t	limit;
206 	rlim64_t	roundlimit;
207 
208 	ASSERT(p->p_model == DATAMODEL_ILP32 || p->p_model == DATAMODEL_LP64);
209 
210 	bigwad = kmem_alloc(sizeof (struct bigwad), KM_SLEEP);
211 	ehdrp = &bigwad->ehdr;
212 	dlnp = bigwad->dl_name;
213 	pathbufp = bigwad->pathbuf;
214 
215 	/*
216 	 * Obtain ELF and program header information.
217 	 */
218 	if ((error = getelfhead(vp, CRED(), ehdrp)) != 0 ||
219 	    (error = getelfphdr(vp, CRED(), ehdrp, &phdrbase, &phdrsize)) != 0)
220 		goto out;
221 
222 	/*
223 	 * Put data model that we're exec-ing to into the args passed to
224 	 * exec_args(), so it will know what it is copying to on new stack.
225 	 * Now that we know whether we are exec-ing a 32-bit or 64-bit
226 	 * executable, we can set execsz with the appropriate NCARGS.
227 	 */
228 #ifdef	_LP64
229 	if (ehdrp->e_ident[EI_CLASS] == ELFCLASS32) {
230 		args->to_model = DATAMODEL_ILP32;
231 		*execsz = btopr(SINCR) + btopr(SSIZE) + btopr(NCARGS32-1);
232 	} else {
233 		args->to_model = DATAMODEL_LP64;
234 		args->stk_prot &= ~PROT_EXEC;
235 #if defined(__i386) || defined(__amd64)
236 		args->dat_prot &= ~PROT_EXEC;
237 #endif
238 		*execsz = btopr(SINCR) + btopr(SSIZE) + btopr(NCARGS64-1);
239 	}
240 #else	/* _LP64 */
241 	args->to_model = DATAMODEL_ILP32;
242 	*execsz = btopr(SINCR) + btopr(SSIZE) + btopr(NCARGS-1);
243 #endif	/* _LP64 */
244 
245 	/*
246 	 * Determine aux size now so that stack can be built
247 	 * in one shot (except actual copyout of aux image),
248 	 * determine any non-default stack protections,
249 	 * and still have this code be machine independent.
250 	 */
251 	hsize = ehdrp->e_phentsize;
252 	phdrp = (Phdr *)phdrbase;
253 	for (i = ehdrp->e_phnum; i > 0; i--) {
254 		switch (phdrp->p_type) {
255 		case PT_INTERP:
256 			hasauxv = hasdy = 1;
257 			break;
258 		case PT_PHDR:
259 			hasu = 1;
260 			break;
261 		case PT_SUNWSTACK:
262 			args->stk_prot = PROT_USER;
263 			if (phdrp->p_flags & PF_R)
264 				args->stk_prot |= PROT_READ;
265 			if (phdrp->p_flags & PF_W)
266 				args->stk_prot |= PROT_WRITE;
267 			if (phdrp->p_flags & PF_X)
268 				args->stk_prot |= PROT_EXEC;
269 			break;
270 		case PT_LOAD:
271 			dataphdrp = phdrp;
272 			break;
273 		}
274 		phdrp = (Phdr *)((caddr_t)phdrp + hsize);
275 	}
276 
277 	if (ehdrp->e_type != ET_EXEC) {
278 		dataphdrp = NULL;
279 		hasauxv = 1;
280 	}
281 
282 	/* Copy BSS permissions to args->dat_prot */
283 	if (dataphdrp != NULL) {
284 		args->dat_prot = PROT_USER;
285 		if (dataphdrp->p_flags & PF_R)
286 			args->dat_prot |= PROT_READ;
287 		if (dataphdrp->p_flags & PF_W)
288 			args->dat_prot |= PROT_WRITE;
289 		if (dataphdrp->p_flags & PF_X)
290 			args->dat_prot |= PROT_EXEC;
291 	}
292 
293 	/*
294 	 * If a auxvector will be required - reserve the space for
295 	 * it now.  This may be increased by exec_args if there are
296 	 * ISA-specific types (included in __KERN_NAUXV_IMPL).
297 	 */
298 	if (hasauxv) {
299 		/*
300 		 * If a AUX vector is being built - the base AUX
301 		 * entries are:
302 		 *
303 		 *	AT_BASE
304 		 *	AT_FLAGS
305 		 *	AT_PAGESZ
306 		 *	AT_SUN_LDSECURE
307 		 *	AT_SUN_HWCAP
308 		 *	AT_SUN_PLATFORM
309 		 *	AT_SUN_EXECNAME
310 		 *	AT_NULL
311 		 *
312 		 * total == 8
313 		 */
314 		if (hasdy && hasu) {
315 			/*
316 			 * Has PT_INTERP & PT_PHDR - the auxvectors that
317 			 * will be built are:
318 			 *
319 			 *	AT_PHDR
320 			 *	AT_PHENT
321 			 *	AT_PHNUM
322 			 *	AT_ENTRY
323 			 *	AT_LDDATA
324 			 *
325 			 * total = 5
326 			 */
327 			args->auxsize = (8 + 5) * sizeof (aux_entry_t);
328 		} else if (hasdy) {
329 			/*
330 			 * Has PT_INTERP but no PT_PHDR
331 			 *
332 			 *	AT_EXECFD
333 			 *	AT_LDDATA
334 			 *
335 			 * total = 2
336 			 */
337 			args->auxsize = (8 + 2) * sizeof (aux_entry_t);
338 		} else {
339 			args->auxsize = 8 * sizeof (aux_entry_t);
340 		}
341 	} else
342 		args->auxsize = 0;
343 
344 	aux = bigwad->elfargs;
345 	/*
346 	 * Move args to the user's stack.
347 	 */
348 	if ((error = exec_args(uap, args, idatap, (void **)&aux)) != 0) {
349 		if (error == -1) {
350 			error = ENOEXEC;
351 			goto bad;
352 		}
353 		goto out;
354 	}
355 
356 	/*
357 	 * If this is an ET_DYN executable (shared object),
358 	 * determine its memory size so that mapelfexec() can load it.
359 	 */
360 	if (ehdrp->e_type == ET_DYN)
361 		len = elfsize(ehdrp, phdrbase, NULL);
362 	else
363 		len = 0;
364 
365 	dtrphdr = NULL;
366 
367 	if ((error = mapelfexec(vp, ehdrp, phdrbase, &uphdr, &dyphdr, &stphdr,
368 	    &dtrphdr, dataphdrp, &bssbase, &brkbase, &voffset, len, execsz,
369 	    &brksize)) != 0)
370 		goto bad;
371 
372 	if (uphdr != NULL && dyphdr == NULL)
373 		goto bad;
374 
375 	if (dtrphdr != NULL && dtrace_safe_phdr(dtrphdr, args, voffset) != 0) {
376 		uprintf("%s: Bad DTrace phdr in %s\n", exec_file, exec_file);
377 		goto bad;
378 	}
379 
380 	if (dyphdr != NULL) {
381 		size_t		len;
382 		uintptr_t	lddata;
383 		char		*p;
384 		struct vnode	*nvp;
385 
386 		dlnsize = dyphdr->p_filesz;
387 
388 		if (dlnsize > MAXPATHLEN || dlnsize <= 0)
389 			goto bad;
390 
391 		/*
392 		 * Read in "interpreter" pathname.
393 		 */
394 		if ((error = vn_rdwr(UIO_READ, vp, dlnp, dyphdr->p_filesz,
395 		    (offset_t)dyphdr->p_offset, UIO_SYSSPACE, 0, (rlim64_t)0,
396 		    CRED(), &resid)) != 0) {
397 			uprintf("%s: Cannot obtain interpreter pathname\n",
398 			    exec_file);
399 			goto bad;
400 		}
401 
402 		if (resid != 0 || dlnp[dlnsize - 1] != '\0')
403 			goto bad;
404 
405 		/*
406 		 * Search for '$ORIGIN' token in interpreter path.
407 		 * If found, expand it.
408 		 */
409 		for (p = dlnp; p = strchr(p, '$'); ) {
410 			uint_t	len, curlen;
411 			char	*_ptr;
412 
413 			if (strncmp(++p, ORIGIN_STR, ORIGIN_STR_SIZE))
414 				continue;
415 
416 			curlen = 0;
417 			len = p - dlnp - 1;
418 			if (len) {
419 				bcopy(dlnp, pathbufp, len);
420 				curlen += len;
421 			}
422 			if (_ptr = strrchr(args->pathname, '/')) {
423 				len = _ptr - args->pathname;
424 				if ((curlen + len) > MAXPATHLEN)
425 					break;
426 
427 				bcopy(args->pathname, &pathbufp[curlen], len);
428 				curlen += len;
429 			} else {
430 				/*
431 				 * executable is a basename found in the
432 				 * current directory.  So - just substitue
433 				 * '.' for ORIGIN.
434 				 */
435 				pathbufp[curlen] = '.';
436 				curlen++;
437 			}
438 			p += ORIGIN_STR_SIZE;
439 			len = strlen(p);
440 
441 			if ((curlen + len) > MAXPATHLEN)
442 				break;
443 			bcopy(p, &pathbufp[curlen], len);
444 			curlen += len;
445 			pathbufp[curlen++] = '\0';
446 			bcopy(pathbufp, dlnp, curlen);
447 		}
448 
449 		/*
450 		 * /usr/lib/ld.so.1 is known to be a symlink to /lib/ld.so.1
451 		 * (and /usr/lib/64/ld.so.1 is a symlink to /lib/64/ld.so.1).
452 		 * Just in case /usr is not mounted, change it now.
453 		 */
454 		if (strcmp(dlnp, USR_LIB_RTLD) == 0)
455 			dlnp += 4;
456 		error = lookupname(dlnp, UIO_SYSSPACE, FOLLOW, NULLVPP, &nvp);
457 		if (error && dlnp != bigwad->dl_name) {
458 			/* new kernel, old user-level */
459 			error = lookupname(dlnp -= 4, UIO_SYSSPACE, FOLLOW,
460 				NULLVPP, &nvp);
461 		}
462 		if (error) {
463 			uprintf("%s: Cannot find %s\n", exec_file, dlnp);
464 			goto bad;
465 		}
466 
467 		/*
468 		 * Setup the "aux" vector.
469 		 */
470 		if (uphdr) {
471 			if (ehdrp->e_type == ET_DYN) {
472 				/* don't use the first page */
473 				bigwad->exenv.ex_brkbase = (caddr_t)PAGESIZE;
474 				bigwad->exenv.ex_bssbase = (caddr_t)PAGESIZE;
475 			} else {
476 				bigwad->exenv.ex_bssbase = bssbase;
477 				bigwad->exenv.ex_brkbase = brkbase;
478 			}
479 			bigwad->exenv.ex_brksize = brksize;
480 			bigwad->exenv.ex_magic = elfmagic;
481 			bigwad->exenv.ex_vp = vp;
482 			setexecenv(&bigwad->exenv);
483 
484 			ADDAUX(aux, AT_PHDR, uphdr->p_vaddr + voffset)
485 			ADDAUX(aux, AT_PHENT, ehdrp->e_phentsize)
486 			ADDAUX(aux, AT_PHNUM, ehdrp->e_phnum)
487 			ADDAUX(aux, AT_ENTRY, ehdrp->e_entry + voffset)
488 		} else {
489 			if ((error = execopen(&vp, &fd)) != 0) {
490 				VN_RELE(nvp);
491 				goto bad;
492 			}
493 
494 			ADDAUX(aux, AT_EXECFD, fd)
495 		}
496 
497 		if ((error = execpermissions(nvp, &bigwad->vattr, args)) != 0) {
498 			VN_RELE(nvp);
499 			uprintf("%s: Cannot execute %s\n", exec_file, dlnp);
500 			goto bad;
501 		}
502 
503 		/*
504 		 * Now obtain the ELF header along with the entire program
505 		 * header contained in "nvp".
506 		 */
507 		kmem_free(phdrbase, phdrsize);
508 		phdrbase = NULL;
509 		if ((error = getelfhead(nvp, CRED(), ehdrp)) != 0 ||
510 		    (error = getelfphdr(nvp, CRED(), ehdrp, &phdrbase,
511 		    &phdrsize)) != 0) {
512 			VN_RELE(nvp);
513 			uprintf("%s: Cannot read %s\n", exec_file, dlnp);
514 			goto bad;
515 		}
516 
517 		/*
518 		 * Determine memory size of the "interpreter's" loadable
519 		 * sections.  This size is then used to obtain the virtual
520 		 * address of a hole, in the user's address space, large
521 		 * enough to map the "interpreter".
522 		 */
523 		if ((len = elfsize(ehdrp, phdrbase, &lddata)) == 0) {
524 			VN_RELE(nvp);
525 			uprintf("%s: Nothing to load in %s\n", exec_file, dlnp);
526 			goto bad;
527 		}
528 
529 		dtrphdr = NULL;
530 
531 		error = mapelfexec(nvp, ehdrp, phdrbase, &junk, &junk, &junk,
532 		    &dtrphdr, NULL, NULL, NULL, &voffset, len, execsz, NULL);
533 		if (error || junk != NULL) {
534 			VN_RELE(nvp);
535 			uprintf("%s: Cannot map %s\n", exec_file, dlnp);
536 			goto bad;
537 		}
538 
539 		/*
540 		 * We use the DTrace program header to initialize the
541 		 * architecture-specific user per-LWP location. The dtrace
542 		 * fasttrap provider requires ready access to per-LWP scratch
543 		 * space. We assume that there is only one such program header
544 		 * in the interpreter.
545 		 */
546 		if (dtrphdr != NULL &&
547 		    dtrace_safe_phdr(dtrphdr, args, voffset) != 0) {
548 			VN_RELE(nvp);
549 			uprintf("%s: Bad DTrace phdr in %s\n", exec_file, dlnp);
550 			goto bad;
551 		}
552 
553 		VN_RELE(nvp);
554 		ADDAUX(aux, AT_SUN_LDDATA, voffset + lddata)
555 	}
556 
557 	if (hasauxv) {
558 		int auxf = AF_SUN_HWCAPVERIFY;
559 		/*
560 		 * Note: AT_SUN_PLATFORM was filled in via exec_args()
561 		 */
562 		ADDAUX(aux, AT_BASE, voffset)
563 		ADDAUX(aux, AT_FLAGS, at_flags)
564 		ADDAUX(aux, AT_PAGESZ, PAGESIZE)
565 		/*
566 		 * Linker flags. (security)
567 		 * p_flag not yet set at this time.
568 		 * We rely on gexec() to provide us with the information.
569 		 */
570 		ADDAUX(aux, AT_SUN_AUXFLAGS,
571 		    setid ? AF_SUN_SETUGID | auxf : auxf);
572 		/*
573 		 * Hardware capability flag word (performance hints)
574 		 * Used for choosing faster library routines.
575 		 * (Potentially different between 32-bit and 64-bit ABIs)
576 		 */
577 #if defined(_LP64)
578 		if (args->to_model == DATAMODEL_NATIVE)
579 			ADDAUX(aux, AT_SUN_HWCAP, auxv_hwcap)
580 		else
581 			ADDAUX(aux, AT_SUN_HWCAP, auxv_hwcap32)
582 #else
583 		ADDAUX(aux, AT_SUN_HWCAP, auxv_hwcap)
584 #endif
585 		ADDAUX(aux, AT_NULL, 0)
586 		postfixsize = (char *)aux - (char *)bigwad->elfargs;
587 		ASSERT(postfixsize == args->auxsize);
588 		ASSERT(postfixsize <= __KERN_NAUXV_IMPL * sizeof (aux_entry_t));
589 	}
590 
591 	/*
592 	 * For the 64-bit kernel, the limit is big enough that rounding it up
593 	 * to a page can overflow the 64-bit limit, so we check for btopr()
594 	 * overflowing here by comparing it with the unrounded limit in pages.
595 	 * If it hasn't overflowed, compare the exec size with the rounded up
596 	 * limit in pages.  Otherwise, just compare with the unrounded limit.
597 	 */
598 	limit = btop(p->p_vmem_ctl);
599 	roundlimit = btopr(p->p_vmem_ctl);
600 	if ((roundlimit > limit && *execsz > roundlimit) ||
601 	    (roundlimit < limit && *execsz > limit)) {
602 		mutex_enter(&p->p_lock);
603 		(void) rctl_action(rctlproc_legacy[RLIMIT_VMEM], p->p_rctls, p,
604 		    RCA_SAFE);
605 		mutex_exit(&p->p_lock);
606 		error = ENOMEM;
607 		goto bad;
608 	}
609 
610 	bzero(up->u_auxv, sizeof (up->u_auxv));
611 	if (postfixsize) {
612 		int num_auxv;
613 
614 		/*
615 		 * Copy the aux vector to the user stack.
616 		 */
617 		error = execpoststack(args, bigwad->elfargs, postfixsize);
618 		if (error)
619 			goto bad;
620 
621 		/*
622 		 * Copy auxv to the process's user structure for use by /proc.
623 		 */
624 		num_auxv = postfixsize / sizeof (aux_entry_t);
625 		ASSERT(num_auxv <= sizeof (up->u_auxv) / sizeof (auxv_t));
626 		aux = bigwad->elfargs;
627 		for (i = 0; i < num_auxv; i++) {
628 			up->u_auxv[i].a_type = aux[i].a_type;
629 			up->u_auxv[i].a_un.a_val = (aux_val_t)aux[i].a_un.a_val;
630 		}
631 	}
632 
633 	/*
634 	 * Pass back the starting address so we can set the program counter.
635 	 */
636 	args->entry = (uintptr_t)(ehdrp->e_entry + voffset);
637 
638 	if (!uphdr) {
639 		if (ehdrp->e_type == ET_DYN) {
640 			/*
641 			 * If we are executing a shared library which doesn't
642 			 * have a interpreter (probably ld.so.1) then
643 			 * we don't set the brkbase now.  Instead we
644 			 * delay it's setting until the first call
645 			 * via grow.c::brk().  This permits ld.so.1 to
646 			 * initialize brkbase to the tail of the executable it
647 			 * loads (which is where it needs to be).
648 			 */
649 			bigwad->exenv.ex_brkbase = (caddr_t)0;
650 			bigwad->exenv.ex_bssbase = (caddr_t)0;
651 			bigwad->exenv.ex_brksize = 0;
652 		} else {
653 			bigwad->exenv.ex_brkbase = brkbase;
654 			bigwad->exenv.ex_bssbase = bssbase;
655 			bigwad->exenv.ex_brksize = brksize;
656 		}
657 		bigwad->exenv.ex_magic = elfmagic;
658 		bigwad->exenv.ex_vp = vp;
659 		setexecenv(&bigwad->exenv);
660 	}
661 
662 	ASSERT(error == 0);
663 	goto out;
664 
665 bad:
666 	if (fd != -1)		/* did we open the a.out yet */
667 		(void) execclose(fd);
668 
669 	psignal(p, SIGKILL);
670 
671 	if (error == 0)
672 		error = ENOEXEC;
673 out:
674 	if (phdrbase != NULL)
675 		kmem_free(phdrbase, phdrsize);
676 	kmem_free(bigwad, sizeof (struct bigwad));
677 	return (error);
678 }
679 
680 /*
681  * Compute the memory size requirement for the ELF file.
682  */
683 static size_t
684 elfsize(Ehdr *ehdrp, caddr_t phdrbase, uintptr_t *lddata)
685 {
686 	size_t	len;
687 	Phdr	*phdrp = (Phdr *)phdrbase;
688 	int	hsize = ehdrp->e_phentsize;
689 	int	first = 1;
690 	int	dfirst = 1;	/* first data segment */
691 	uintptr_t loaddr = 0;
692 	uintptr_t hiaddr = 0;
693 	uintptr_t lo, hi;
694 	int	i;
695 
696 	for (i = ehdrp->e_phnum; i > 0; i--) {
697 		if (phdrp->p_type == PT_LOAD) {
698 			lo = phdrp->p_vaddr;
699 			hi = lo + phdrp->p_memsz;
700 			if (first) {
701 				loaddr = lo;
702 				hiaddr = hi;
703 				first = 0;
704 			} else {
705 				if (loaddr > lo)
706 					loaddr = lo;
707 				if (hiaddr < hi)
708 					hiaddr = hi;
709 			}
710 
711 			/*
712 			 * save the address of the first data segment
713 			 * of a object - used for the AT_SUNW_LDDATA
714 			 * aux entry.
715 			 */
716 			if ((lddata != NULL) && dfirst &&
717 			    (phdrp->p_flags & PF_W)) {
718 				*lddata = lo;
719 				dfirst = 0;
720 			}
721 		}
722 		phdrp = (Phdr *)((caddr_t)phdrp + hsize);
723 	}
724 
725 	len = hiaddr - (loaddr & PAGEMASK);
726 	len = roundup(len, PAGESIZE);
727 
728 	return (len);
729 }
730 
731 /*
732  * Read in the ELF header and program header table.
733  * SUSV3 requires:
734  *	ENOEXEC	File format is not recognized
735  *	EINVAL	Format recognized but execution not supported
736  */
737 static int
738 getelfhead(vnode_t *vp, cred_t *credp, Ehdr *ehdr)
739 {
740 	int error;
741 	ssize_t resid;
742 
743 	/*
744 	 * We got here by the first two bytes in ident,
745 	 * now read the entire ELF header.
746 	 */
747 	if ((error = vn_rdwr(UIO_READ, vp, (caddr_t)ehdr,
748 	    sizeof (Ehdr), (offset_t)0, UIO_SYSSPACE, 0,
749 	    (rlim64_t)0, credp, &resid)) != 0)
750 		return (error);
751 
752 	/*
753 	 * Since a separate version is compiled for handling 32-bit and
754 	 * 64-bit ELF executables on a 64-bit kernel, the 64-bit version
755 	 * doesn't need to be able to deal with 32-bit ELF files.
756 	 */
757 	if (resid != 0 ||
758 	    ehdr->e_ident[EI_MAG2] != ELFMAG2 ||
759 	    ehdr->e_ident[EI_MAG3] != ELFMAG3)
760 		return (ENOEXEC);
761 	if ((ehdr->e_type != ET_EXEC && ehdr->e_type != ET_DYN) ||
762 #if defined(_ILP32) || defined(_ELF32_COMPAT)
763 	    ehdr->e_ident[EI_CLASS] != ELFCLASS32 ||
764 #else
765 	    ehdr->e_ident[EI_CLASS] != ELFCLASS64 ||
766 #endif
767 	    !elfheadcheck(ehdr->e_ident[EI_DATA], ehdr->e_machine,
768 		ehdr->e_flags))
769 		return (EINVAL);
770 
771 	return (0);
772 }
773 
774 #ifdef _ELF32_COMPAT
775 extern size_t elf_nphdr_max;
776 #else
777 size_t elf_nphdr_max = 1000;
778 #endif
779 
780 static int
781 getelfphdr(vnode_t *vp, cred_t *credp, const Ehdr *ehdr,
782     caddr_t *phbasep, ssize_t *phsizep)
783 {
784 	ssize_t resid, minsize;
785 	int err;
786 
787 	/*
788 	 * Since we're going to be using e_phentsize to iterate down the
789 	 * array of program headers, it must be 8-byte aligned or else
790 	 * a we might cause a misaligned access. We use all members through
791 	 * p_flags on 32-bit ELF files and p_memsz on 64-bit ELF files so
792 	 * e_phentsize must be at least large enough to include those
793 	 * members.
794 	 */
795 #if !defined(_LP64) || defined(_ELF32_COMPAT)
796 	minsize = offsetof(Phdr, p_flags) + sizeof (((Phdr *)NULL)->p_flags);
797 #else
798 	minsize = offsetof(Phdr, p_memsz) + sizeof (((Phdr *)NULL)->p_memsz);
799 #endif
800 	if (ehdr->e_phentsize < minsize || (ehdr->e_phentsize & 3))
801 		return (EINVAL);
802 
803 	*phsizep = ehdr->e_phnum * ehdr->e_phentsize;
804 
805 	if (*phsizep > sizeof (Phdr) * elf_nphdr_max) {
806 		if ((*phbasep = kmem_alloc(*phsizep, KM_NOSLEEP)) == NULL)
807 			return (ENOMEM);
808 	} else {
809 		*phbasep = kmem_alloc(*phsizep, KM_SLEEP);
810 	}
811 
812 	if ((err = vn_rdwr(UIO_READ, vp, *phbasep, *phsizep,
813 	    (offset_t)ehdr->e_phoff, UIO_SYSSPACE, 0, (rlim64_t)0,
814 	    credp, &resid)) != 0) {
815 		kmem_free(*phbasep, *phsizep);
816 		*phbasep = NULL;
817 		return (err);
818 	}
819 
820 	return (0);
821 }
822 
823 #ifdef _ELF32_COMPAT
824 extern size_t elf_nshdr_max;
825 extern size_t elf_shstrtab_max;
826 #else
827 size_t elf_nshdr_max = 10000;
828 size_t elf_shstrtab_max = 100 * 1024;
829 #endif
830 
831 
832 static int
833 getelfshdr(vnode_t *vp, cred_t *credp, const Ehdr *ehdr,
834     caddr_t *shbasep, ssize_t *shsizep,
835     char **shstrbasep, ssize_t *shstrsizep)
836 {
837 	ssize_t resid, minsize;
838 	int err;
839 	Shdr *shdr;
840 
841 	/*
842 	 * Since we're going to be using e_shentsize to iterate down the
843 	 * array of section headers, it must be 8-byte aligned or else
844 	 * a we might cause a misaligned access. We use all members through
845 	 * sh_entsize (on both 32- and 64-bit ELF files) so e_shentsize
846 	 * must be at least large enough to include that member. The
847 	 * index of the string table section must be valid.
848 	 */
849 	minsize = offsetof(Shdr, sh_entsize) + sizeof (shdr->sh_entsize);
850 	if (ehdr->e_shentsize < minsize || (ehdr->e_shentsize & 3) ||
851 	    ehdr->e_shstrndx >= ehdr->e_shnum)
852 		return (EINVAL);
853 
854 	*shsizep = ehdr->e_shnum * ehdr->e_shentsize;
855 
856 	if (*shsizep > sizeof (Shdr) * elf_nshdr_max) {
857 		if ((*shbasep = kmem_alloc(*shsizep, KM_NOSLEEP)) == NULL)
858 			return (ENOMEM);
859 	} else {
860 		*shbasep = kmem_alloc(*shsizep, KM_SLEEP);
861 	}
862 
863 	if ((err = vn_rdwr(UIO_READ, vp, *shbasep, *shsizep,
864 	    (offset_t)ehdr->e_shoff, UIO_SYSSPACE, 0, (rlim64_t)0,
865 	    credp, &resid)) != 0) {
866 		kmem_free(*shbasep, *shsizep);
867 		return (err);
868 	}
869 
870 	/*
871 	 * Pull the section string table out of the vnode; fail if the size
872 	 * is zero.
873 	 */
874 	shdr = (Shdr *)(*shbasep + ehdr->e_shstrndx * ehdr->e_shentsize);
875 	if ((*shstrsizep = shdr->sh_size) == 0) {
876 		kmem_free(*shbasep, *shsizep);
877 		return (EINVAL);
878 	}
879 
880 	if (*shstrsizep > elf_shstrtab_max) {
881 		if ((*shstrbasep = kmem_alloc(*shstrsizep,
882 		    KM_NOSLEEP)) == NULL) {
883 			kmem_free(*shbasep, *shsizep);
884 			return (ENOMEM);
885 		}
886 	} else {
887 		*shstrbasep = kmem_alloc(*shstrsizep, KM_SLEEP);
888 	}
889 
890 	if ((err = vn_rdwr(UIO_READ, vp, *shstrbasep, *shstrsizep,
891 	    (offset_t)shdr->sh_offset, UIO_SYSSPACE, 0, (rlim64_t)0,
892 	    credp, &resid)) != 0) {
893 		kmem_free(*shbasep, *shsizep);
894 		kmem_free(*shstrbasep, *shstrsizep);
895 		return (err);
896 	}
897 
898 	/*
899 	 * Make sure the strtab is null-terminated to make sure we
900 	 * don't run off the end of the table.
901 	 */
902 	(*shstrbasep)[*shstrsizep - 1] = '\0';
903 
904 	return (0);
905 }
906 
907 static int
908 mapelfexec(
909 	vnode_t *vp,
910 	Ehdr *ehdr,
911 	caddr_t phdrbase,
912 	Phdr **uphdr,
913 	Phdr **dyphdr,
914 	Phdr **stphdr,
915 	Phdr **dtphdr,
916 	Phdr *dataphdrp,
917 	caddr_t *bssbase,
918 	caddr_t *brkbase,
919 	intptr_t *voffset,
920 	size_t len,
921 	long *execsz,
922 	size_t *brksize)
923 {
924 	Phdr *phdr;
925 	int i, prot, error;
926 	caddr_t addr;
927 	size_t zfodsz;
928 	int ptload = 0;
929 	int page;
930 	off_t offset;
931 	int hsize = ehdr->e_phentsize;
932 
933 	if (ehdr->e_type == ET_DYN) {
934 		/*
935 		 * Obtain the virtual address of a hole in the
936 		 * address space to map the "interpreter".
937 		 */
938 		map_addr(&addr, len, (offset_t)0, 1, 0);
939 		if (addr == NULL)
940 			return (ENOMEM);
941 		*voffset = (intptr_t)addr;
942 	} else {
943 		*voffset = 0;
944 	}
945 	phdr = (Phdr *)phdrbase;
946 	for (i = (int)ehdr->e_phnum; i > 0; i--) {
947 		switch (phdr->p_type) {
948 		case PT_LOAD:
949 			if ((*dyphdr != NULL) && (*uphdr == NULL))
950 				return (0);
951 
952 			ptload = 1;
953 			prot = PROT_USER;
954 			if (phdr->p_flags & PF_R)
955 				prot |= PROT_READ;
956 			if (phdr->p_flags & PF_W)
957 				prot |= PROT_WRITE;
958 			if (phdr->p_flags & PF_X)
959 				prot |= PROT_EXEC;
960 
961 			addr = (caddr_t)((uintptr_t)phdr->p_vaddr + *voffset);
962 			zfodsz = (size_t)phdr->p_memsz - phdr->p_filesz;
963 
964 			offset = phdr->p_offset;
965 			if (((uintptr_t)offset & PAGEOFFSET) ==
966 			    ((uintptr_t)addr & PAGEOFFSET) &&
967 				(!(vp->v_flag & VNOMAP))) {
968 				page = 1;
969 			} else {
970 				page = 0;
971 			}
972 
973 			if (curproc->p_brkpageszc != 0 && phdr == dataphdrp &&
974 			    (prot & PROT_WRITE)) {
975 				/*
976 				 * segvn only uses large pages for segments
977 				 * that have the requested large page size
978 				 * aligned base and size. To insure the part
979 				 * of bss that starts at heap large page size
980 				 * boundary gets mapped by large pages create
981 				 * 2 bss segvn segments which is accomplished
982 				 * by calling execmap twice. First execmap
983 				 * will create the bss segvn segment that is
984 				 * before the large page boundary and it will
985 				 * be mapped with base pages. If bss start is
986 				 * already large page aligned only 1 bss
987 				 * segment will be created. The second bss
988 				 * segment's size is large page size aligned
989 				 * so that segvn uses large pages for that
990 				 * segment and it also makes the heap that
991 				 * starts right after bss to start at large
992 				 * page boundary.
993 				 */
994 				uint_t	szc = curproc->p_brkpageszc;
995 				size_t pgsz = page_get_pagesize(szc);
996 				caddr_t zaddr = addr + phdr->p_filesz;
997 				size_t zlen = P2NPHASE((uintptr_t)zaddr, pgsz);
998 
999 				ASSERT(pgsz > PAGESIZE);
1000 
1001 				if (error = execmap(vp, addr, phdr->p_filesz,
1002 				    zlen, phdr->p_offset, prot, page, szc))
1003 					goto bad;
1004 				if (zfodsz > zlen) {
1005 					zfodsz -= zlen;
1006 					zaddr += zlen;
1007 					zlen = P2ROUNDUP(zfodsz, pgsz);
1008 					if (error = execmap(vp, zaddr, 0, zlen,
1009 					    phdr->p_offset, prot, page, szc))
1010 						goto bad;
1011 				}
1012 				if (brksize != NULL)
1013 					*brksize = zlen - zfodsz;
1014 			} else {
1015 				if (error = execmap(vp, addr, phdr->p_filesz,
1016 				    zfodsz, phdr->p_offset, prot, page, 0))
1017 					goto bad;
1018 			}
1019 
1020 			if (bssbase != NULL && addr >= *bssbase &&
1021 			    phdr == dataphdrp) {
1022 				*bssbase = addr + phdr->p_filesz;
1023 			}
1024 			if (brkbase != NULL && addr >= *brkbase) {
1025 				*brkbase = addr + phdr->p_memsz;
1026 			}
1027 
1028 			*execsz += btopr(phdr->p_memsz);
1029 			break;
1030 
1031 		case PT_INTERP:
1032 			if (ptload)
1033 				goto bad;
1034 			*dyphdr = phdr;
1035 			break;
1036 
1037 		case PT_SHLIB:
1038 			*stphdr = phdr;
1039 			break;
1040 
1041 		case PT_PHDR:
1042 			if (ptload)
1043 				goto bad;
1044 			*uphdr = phdr;
1045 			break;
1046 
1047 		case PT_NULL:
1048 		case PT_DYNAMIC:
1049 		case PT_NOTE:
1050 			break;
1051 
1052 		case PT_SUNWDTRACE:
1053 			if (dtphdr != NULL)
1054 				*dtphdr = phdr;
1055 			break;
1056 
1057 		default:
1058 			break;
1059 		}
1060 		phdr = (Phdr *)((caddr_t)phdr + hsize);
1061 	}
1062 	return (0);
1063 bad:
1064 	if (error == 0)
1065 		error = EINVAL;
1066 	return (error);
1067 }
1068 
1069 int
1070 elfnote(vnode_t *vp, offset_t *offsetp, int type, int descsz, void *desc,
1071     rlim64_t rlimit, cred_t *credp)
1072 {
1073 	Note note;
1074 	int error;
1075 
1076 	bzero(&note, sizeof (note));
1077 	bcopy("CORE", note.name, 4);
1078 	note.nhdr.n_type = type;
1079 	/*
1080 	 * The System V ABI states that n_namesz must be the length of the
1081 	 * string that follows the Nhdr structure including the terminating
1082 	 * null. The ABI also specifies that sufficient padding should be
1083 	 * included so that the description that follows the name string
1084 	 * begins on a 4- or 8-byte boundary for 32- and 64-bit binaries
1085 	 * respectively. However, since this change was not made correctly
1086 	 * at the time of the 64-bit port, both 32- and 64-bit binaries
1087 	 * descriptions are only guaranteed to begin on a 4-byte boundary.
1088 	 */
1089 	note.nhdr.n_namesz = 5;
1090 	note.nhdr.n_descsz = roundup(descsz, sizeof (Word));
1091 
1092 	if (error = core_write(vp, UIO_SYSSPACE, *offsetp, &note,
1093 	    sizeof (note), rlimit, credp))
1094 		return (error);
1095 
1096 	*offsetp += sizeof (note);
1097 
1098 	if (error = core_write(vp, UIO_SYSSPACE, *offsetp, desc,
1099 	    note.nhdr.n_descsz, rlimit, credp))
1100 		return (error);
1101 
1102 	*offsetp += note.nhdr.n_descsz;
1103 	return (0);
1104 }
1105 
1106 /*
1107  * Copy the section data from one vnode to the section of another vnode.
1108  */
1109 static void
1110 copy_scn(Shdr *src, vnode_t *src_vp, Shdr *dst, vnode_t *dst_vp, Off *doffset,
1111     void *buf, size_t size, cred_t *credp, rlim64_t rlimit)
1112 {
1113 	ssize_t resid;
1114 	size_t len, n = src->sh_size;
1115 	offset_t off = 0;
1116 
1117 	while (n != 0) {
1118 		len = MIN(size, n);
1119 		if (vn_rdwr(UIO_READ, src_vp, buf, len, src->sh_offset + off,
1120 		    UIO_SYSSPACE, 0, (rlim64_t)0, credp, &resid) != 0 ||
1121 		    resid >= len ||
1122 		    core_write(dst_vp, UIO_SYSSPACE, *doffset + off,
1123 		    buf, len - resid, rlimit, credp) != 0) {
1124 			dst->sh_size = 0;
1125 			dst->sh_offset = 0;
1126 			return;
1127 		}
1128 
1129 		ASSERT(n >= len - resid);
1130 
1131 		n -= len - resid;
1132 		off += len - resid;
1133 	}
1134 
1135 	*doffset += src->sh_size;
1136 }
1137 
1138 #ifdef _ELF32_COMPAT
1139 extern size_t elf_datasz_max;
1140 #else
1141 size_t elf_datasz_max = 1 * 1024 * 1024;
1142 #endif
1143 
1144 /*
1145  * This function processes mappings that correspond to load objects to
1146  * examine their respective sections for elfcore(). It's called once with
1147  * v set to NULL to count the number of sections that we're going to need
1148  * and then again with v set to some allocated buffer that we fill in with
1149  * all the section data.
1150  */
1151 static int
1152 process_scns(core_content_t content, proc_t *p, cred_t *credp, vnode_t *vp,
1153     Shdr *v, int nshdrs, rlim64_t rlimit, Off *doffsetp, int *nshdrsp)
1154 {
1155 	vnode_t *lastvp = NULL;
1156 	struct seg *seg;
1157 	int i, j;
1158 	void *data = NULL;
1159 	size_t datasz = 0;
1160 	shstrtab_t shstrtab;
1161 	struct as *as = p->p_as;
1162 	int error = 0;
1163 
1164 	if (v != NULL)
1165 		shstrtab_init(&shstrtab);
1166 
1167 	i = 1;
1168 	for (seg = AS_SEGFIRST(as); seg != NULL; seg = AS_SEGNEXT(as, seg)) {
1169 		uint_t prot;
1170 		vnode_t *mvp;
1171 		void *tmp = NULL;
1172 		caddr_t saddr = seg->s_base;
1173 		caddr_t naddr;
1174 		caddr_t eaddr;
1175 		size_t segsize;
1176 
1177 		Ehdr ehdr;
1178 		caddr_t shbase;
1179 		ssize_t shsize;
1180 		char *shstrbase;
1181 		ssize_t shstrsize;
1182 
1183 		Shdr *shdr;
1184 		const char *name;
1185 		size_t sz;
1186 		uintptr_t off;
1187 
1188 		int ctf_ndx = 0;
1189 		int symtab_ndx = 0;
1190 
1191 		/*
1192 		 * Since we're just looking for text segments of load
1193 		 * objects, we only care about the protection bits; we don't
1194 		 * care about the actual size of the segment so we use the
1195 		 * reserved size. If the segment's size is zero, there's
1196 		 * something fishy going on so we ignore this segment.
1197 		 */
1198 		if (seg->s_ops != &segvn_ops ||
1199 		    SEGOP_GETVP(seg, seg->s_base, &mvp) != 0 ||
1200 		    mvp == lastvp || mvp == NULL || mvp->v_type != VREG ||
1201 		    (segsize = pr_getsegsize(seg, 1)) == 0)
1202 			continue;
1203 
1204 		eaddr = saddr + segsize;
1205 		prot = pr_getprot(seg, 1, &tmp, &saddr, &naddr, eaddr);
1206 		pr_getprot_done(&tmp);
1207 
1208 		/*
1209 		 * Skip this segment unless the protection bits look like
1210 		 * what we'd expect for a text segment.
1211 		 */
1212 		if ((prot & (PROT_WRITE | PROT_EXEC)) != PROT_EXEC)
1213 			continue;
1214 
1215 		if (getelfhead(mvp, credp, &ehdr) != 0 ||
1216 		    getelfshdr(mvp, credp, &ehdr, &shbase, &shsize,
1217 		    &shstrbase, &shstrsize) != 0)
1218 			continue;
1219 
1220 		off = ehdr.e_shentsize;
1221 		for (j = 1; j < ehdr.e_shnum; j++, off += ehdr.e_shentsize) {
1222 			Shdr *symtab = NULL, *strtab;
1223 
1224 			shdr = (Shdr *)(shbase + off);
1225 
1226 			if (shdr->sh_name >= shstrsize)
1227 				continue;
1228 
1229 			name = shstrbase + shdr->sh_name;
1230 
1231 			if (strcmp(name, shstrtab_data[STR_CTF]) == 0) {
1232 				if ((content & CC_CONTENT_CTF) == 0 ||
1233 				    ctf_ndx != 0)
1234 					continue;
1235 
1236 				if (shdr->sh_link > 0 &&
1237 				    shdr->sh_link < ehdr.e_shnum) {
1238 					symtab = (Shdr *)(shbase +
1239 					    shdr->sh_link * ehdr.e_shentsize);
1240 				}
1241 
1242 				if (v != NULL && i < nshdrs - 1) {
1243 					if (shdr->sh_size > datasz &&
1244 					    shdr->sh_size <= elf_datasz_max) {
1245 						if (data != NULL)
1246 							kmem_free(data, datasz);
1247 
1248 						datasz = shdr->sh_size;
1249 						data = kmem_alloc(datasz,
1250 						    KM_SLEEP);
1251 					}
1252 
1253 					v[i].sh_name = shstrtab_ndx(&shstrtab,
1254 					    STR_CTF);
1255 					v[i].sh_addr = (Addr)(uintptr_t)saddr;
1256 					v[i].sh_type = SHT_PROGBITS;
1257 					v[i].sh_addralign = 4;
1258 					*doffsetp = roundup(*doffsetp,
1259 					    v[i].sh_addralign);
1260 					v[i].sh_offset = *doffsetp;
1261 					v[i].sh_size = shdr->sh_size;
1262 					if (symtab == NULL)  {
1263 						v[i].sh_link = 0;
1264 					} else if (symtab->sh_type ==
1265 					    SHT_SYMTAB &&
1266 					    symtab_ndx != 0) {
1267 						v[i].sh_link =
1268 						    symtab_ndx;
1269 					} else {
1270 						v[i].sh_link = i + 1;
1271 					}
1272 
1273 					copy_scn(shdr, mvp, &v[i], vp,
1274 					    doffsetp, data, datasz, credp,
1275 					    rlimit);
1276 				}
1277 
1278 				ctf_ndx = i++;
1279 
1280 				/*
1281 				 * We've already dumped the symtab.
1282 				 */
1283 				if (symtab != NULL &&
1284 				    symtab->sh_type == SHT_SYMTAB &&
1285 				    symtab_ndx != 0)
1286 					continue;
1287 
1288 			} else if (strcmp(name,
1289 			    shstrtab_data[STR_SYMTAB]) == 0) {
1290 				if ((content & CC_CONTENT_SYMTAB) == 0 ||
1291 				    symtab != 0)
1292 					continue;
1293 
1294 				symtab = shdr;
1295 			}
1296 
1297 			if (symtab != NULL) {
1298 				if ((symtab->sh_type != SHT_DYNSYM &&
1299 				    symtab->sh_type != SHT_SYMTAB) ||
1300 				    symtab->sh_link == 0 ||
1301 				    symtab->sh_link >= ehdr.e_shnum)
1302 					continue;
1303 
1304 				strtab = (Shdr *)(shbase +
1305 				    symtab->sh_link * ehdr.e_shentsize);
1306 
1307 				if (strtab->sh_type != SHT_STRTAB)
1308 					continue;
1309 
1310 				if (v != NULL && i < nshdrs - 2) {
1311 					sz = MAX(symtab->sh_size,
1312 					    strtab->sh_size);
1313 					if (sz > datasz &&
1314 					    sz <= elf_datasz_max) {
1315 						if (data != NULL)
1316 							kmem_free(data, datasz);
1317 
1318 						datasz = sz;
1319 						data = kmem_alloc(datasz,
1320 						    KM_SLEEP);
1321 					}
1322 
1323 					if (symtab->sh_type == SHT_DYNSYM) {
1324 						v[i].sh_name = shstrtab_ndx(
1325 						    &shstrtab, STR_DYNSYM);
1326 						v[i + 1].sh_name = shstrtab_ndx(
1327 						    &shstrtab, STR_DYNSTR);
1328 					} else {
1329 						v[i].sh_name = shstrtab_ndx(
1330 						    &shstrtab, STR_SYMTAB);
1331 						v[i + 1].sh_name = shstrtab_ndx(
1332 						    &shstrtab, STR_STRTAB);
1333 					}
1334 
1335 					v[i].sh_type = symtab->sh_type;
1336 					v[i].sh_addr = symtab->sh_addr;
1337 					if (ehdr.e_type == ET_DYN ||
1338 					    v[i].sh_addr == 0)
1339 						v[i].sh_addr +=
1340 						    (Addr)(uintptr_t)saddr;
1341 					v[i].sh_addralign =
1342 					    symtab->sh_addralign;
1343 					*doffsetp = roundup(*doffsetp,
1344 					    v[i].sh_addralign);
1345 					v[i].sh_offset = *doffsetp;
1346 					v[i].sh_size = symtab->sh_size;
1347 					v[i].sh_link = i + 1;
1348 					v[i].sh_entsize = symtab->sh_entsize;
1349 					v[i].sh_info = symtab->sh_info;
1350 
1351 					copy_scn(symtab, mvp, &v[i], vp,
1352 					    doffsetp, data, datasz, credp,
1353 					    rlimit);
1354 
1355 					v[i + 1].sh_type = SHT_STRTAB;
1356 					v[i + 1].sh_flags = SHF_STRINGS;
1357 					v[i + 1].sh_addr = symtab->sh_addr;
1358 					if (ehdr.e_type == ET_DYN ||
1359 					    v[i + 1].sh_addr == 0)
1360 						v[i + 1].sh_addr +=
1361 						    (Addr)(uintptr_t)saddr;
1362 					v[i + 1].sh_addralign =
1363 					    strtab->sh_addralign;
1364 					*doffsetp = roundup(*doffsetp,
1365 					    v[i + 1].sh_addralign);
1366 					v[i + 1].sh_offset = *doffsetp;
1367 					v[i + 1].sh_size = strtab->sh_size;
1368 
1369 					copy_scn(strtab, mvp, &v[i + 1], vp,
1370 					    doffsetp, data, datasz, credp,
1371 					    rlimit);
1372 				}
1373 
1374 				if (symtab->sh_type == SHT_SYMTAB)
1375 					symtab_ndx = i;
1376 				i += 2;
1377 			}
1378 		}
1379 
1380 		kmem_free(shstrbase, shstrsize);
1381 		kmem_free(shbase, shsize);
1382 
1383 		lastvp = mvp;
1384 	}
1385 
1386 	if (v == NULL) {
1387 		if (i == 1)
1388 			*nshdrsp = 0;
1389 		else
1390 			*nshdrsp = i + 1;
1391 		goto done;
1392 	}
1393 
1394 	if (i != nshdrs - 1) {
1395 		cmn_err(CE_WARN, "elfcore: core dump failed for "
1396 		    "process %d; address space is changing", p->p_pid);
1397 		error = EIO;
1398 		goto done;
1399 	}
1400 
1401 	v[i].sh_name = shstrtab_ndx(&shstrtab, STR_SHSTRTAB);
1402 	v[i].sh_size = shstrtab_size(&shstrtab);
1403 	v[i].sh_addralign = 1;
1404 	*doffsetp = roundup(*doffsetp, v[i].sh_addralign);
1405 	v[i].sh_offset = *doffsetp;
1406 	v[i].sh_flags = SHF_STRINGS;
1407 	v[i].sh_type = SHT_STRTAB;
1408 
1409 	if (v[i].sh_size > datasz) {
1410 		if (data != NULL)
1411 			kmem_free(data, datasz);
1412 
1413 		datasz = v[i].sh_size;
1414 		data = kmem_alloc(datasz,
1415 		    KM_SLEEP);
1416 	}
1417 
1418 	shstrtab_dump(&shstrtab, data);
1419 
1420 	if ((error = core_write(vp, UIO_SYSSPACE, *doffsetp,
1421 	    data, v[i].sh_size, rlimit, credp)) != 0)
1422 		goto done;
1423 
1424 	*doffsetp += v[i].sh_size;
1425 
1426 done:
1427 	if (data != NULL)
1428 		kmem_free(data, datasz);
1429 
1430 	return (error);
1431 }
1432 
1433 int
1434 elfcore(vnode_t *vp, proc_t *p, cred_t *credp, rlim64_t rlimit, int sig,
1435     core_content_t content)
1436 {
1437 	offset_t poffset, soffset;
1438 	Off doffset;
1439 	int error, i, nphdrs, nshdrs;
1440 	int overflow = 0;
1441 	struct seg *seg;
1442 	struct as *as = p->p_as;
1443 	union {
1444 		Ehdr ehdr;
1445 		Phdr phdr[1];
1446 		Shdr shdr[1];
1447 	} *bigwad;
1448 	size_t bigsize;
1449 	size_t phdrsz, shdrsz;
1450 	Ehdr *ehdr;
1451 	Phdr *v;
1452 	caddr_t brkbase;
1453 	size_t brksize;
1454 	caddr_t stkbase;
1455 	size_t stksize;
1456 	int ntries = 0;
1457 
1458 top:
1459 	/*
1460 	 * Make sure we have everything we need (registers, etc.).
1461 	 * All other lwps have already stopped and are in an orderly state.
1462 	 */
1463 	ASSERT(p == ttoproc(curthread));
1464 	prstop(0, 0);
1465 
1466 	AS_LOCK_ENTER(as, &as->a_lock, RW_WRITER);
1467 	nphdrs = prnsegs(as, 0) + 2;		/* two CORE note sections */
1468 
1469 	/*
1470 	 * Count the number of section headers we're going to need.
1471 	 */
1472 	nshdrs = 0;
1473 	if (content & (CC_CONTENT_CTF | CC_CONTENT_SYMTAB)) {
1474 		(void) process_scns(content, p, credp, NULL, NULL, NULL, 0,
1475 		    NULL, &nshdrs);
1476 	}
1477 	AS_LOCK_EXIT(as, &as->a_lock);
1478 
1479 	phdrsz = nphdrs * sizeof (Phdr);
1480 	shdrsz = nshdrs * sizeof (Shdr);
1481 
1482 	bigsize = MAX(sizeof (*bigwad), MAX(phdrsz, shdrsz));
1483 	bigwad = kmem_alloc(bigsize, KM_SLEEP);
1484 
1485 	ehdr = &bigwad->ehdr;
1486 	bzero(ehdr, sizeof (*ehdr));
1487 
1488 	ehdr->e_ident[EI_MAG0] = ELFMAG0;
1489 	ehdr->e_ident[EI_MAG1] = ELFMAG1;
1490 	ehdr->e_ident[EI_MAG2] = ELFMAG2;
1491 	ehdr->e_ident[EI_MAG3] = ELFMAG3;
1492 	ehdr->e_ident[EI_CLASS] = ELFCLASS;
1493 	ehdr->e_type = ET_CORE;
1494 
1495 #if !defined(_LP64) || defined(_ELF32_COMPAT)
1496 
1497 #if defined(__sparc)
1498 	ehdr->e_ident[EI_DATA] = ELFDATA2MSB;
1499 	ehdr->e_machine = EM_SPARC;
1500 #elif defined(__i386) || defined(__i386_COMPAT)
1501 	ehdr->e_ident[EI_DATA] = ELFDATA2LSB;
1502 	ehdr->e_machine = EM_386;
1503 #else
1504 #error "no recognized machine type is defined"
1505 #endif
1506 
1507 #else	/* !defined(_LP64) || defined(_ELF32_COMPAT) */
1508 
1509 #if defined(__sparc)
1510 	ehdr->e_ident[EI_DATA] = ELFDATA2MSB;
1511 	ehdr->e_machine = EM_SPARCV9;
1512 #elif defined(__amd64)
1513 	ehdr->e_ident[EI_DATA] = ELFDATA2LSB;
1514 	ehdr->e_machine = EM_AMD64;
1515 #else
1516 #error "no recognized 64-bit machine type is defined"
1517 #endif
1518 
1519 #endif	/* !defined(_LP64) || defined(_ELF32_COMPAT) */
1520 
1521 	ehdr->e_version = EV_CURRENT;
1522 	ehdr->e_phoff = sizeof (Ehdr);
1523 	ehdr->e_ehsize = sizeof (Ehdr);
1524 	ehdr->e_phentsize = sizeof (Phdr);
1525 	ehdr->e_phnum = (unsigned short)nphdrs;
1526 
1527 	if (nshdrs > 0) {
1528 		ehdr->e_shstrndx = (unsigned short)(nshdrs - 1);
1529 		ehdr->e_shentsize = sizeof (Shdr);
1530 		ehdr->e_shnum = (unsigned short)nshdrs;
1531 		ehdr->e_shoff = ehdr->e_phoff +
1532 		    ehdr->e_phentsize * ehdr->e_phnum;
1533 	}
1534 
1535 	if (error = core_write(vp, UIO_SYSSPACE, (offset_t)0, ehdr,
1536 	    sizeof (Ehdr), rlimit, credp))
1537 		goto done;
1538 
1539 	poffset = sizeof (Ehdr);
1540 	soffset = sizeof (Ehdr) + phdrsz;
1541 	doffset = sizeof (Ehdr) + phdrsz + shdrsz;
1542 
1543 	v = &bigwad->phdr[0];
1544 	bzero(v, phdrsz);
1545 
1546 	setup_old_note_header(&v[0], p);
1547 	v[0].p_offset = doffset = roundup(doffset, sizeof (Word));
1548 	doffset += v[0].p_filesz;
1549 
1550 	setup_note_header(&v[1], p);
1551 	v[1].p_offset = doffset = roundup(doffset, sizeof (Word));
1552 	doffset += v[1].p_filesz;
1553 
1554 	mutex_enter(&p->p_lock);
1555 
1556 	brkbase = p->p_brkbase;
1557 	brksize = p->p_brksize;
1558 
1559 	stkbase = p->p_usrstack - p->p_stksize;
1560 	stksize = p->p_stksize;
1561 
1562 	mutex_exit(&p->p_lock);
1563 
1564 	AS_LOCK_ENTER(as, &as->a_lock, RW_WRITER);
1565 	i = 2;
1566 	for (seg = AS_SEGFIRST(as); seg != NULL; seg = AS_SEGNEXT(as, seg)) {
1567 		caddr_t eaddr = seg->s_base + pr_getsegsize(seg, 0);
1568 		caddr_t saddr, naddr;
1569 		void *tmp = NULL;
1570 		extern struct seg_ops segspt_shmops;
1571 
1572 		for (saddr = seg->s_base; saddr < eaddr; saddr = naddr) {
1573 			uint_t prot;
1574 			size_t size;
1575 			int type;
1576 			vnode_t *mvp;
1577 
1578 			prot = pr_getprot(seg, 0, &tmp, &saddr, &naddr, eaddr);
1579 			prot &= PROT_READ | PROT_WRITE | PROT_EXEC;
1580 			if ((size = (size_t)(naddr - saddr)) == 0)
1581 				continue;
1582 			if (i == nphdrs) {
1583 				overflow++;
1584 				continue;
1585 			}
1586 			v[i].p_type = PT_LOAD;
1587 			v[i].p_vaddr = (Addr)(uintptr_t)saddr;
1588 			v[i].p_memsz = size;
1589 			if (prot & PROT_READ)
1590 				v[i].p_flags |= PF_R;
1591 			if (prot & PROT_WRITE)
1592 				v[i].p_flags |= PF_W;
1593 			if (prot & PROT_EXEC)
1594 				v[i].p_flags |= PF_X;
1595 
1596 			/*
1597 			 * Figure out which mappings to include in the core.
1598 			 */
1599 			type = SEGOP_GETTYPE(seg, saddr);
1600 
1601 			if (saddr == stkbase && size == stksize) {
1602 				if (!(content & CC_CONTENT_STACK))
1603 					goto exclude;
1604 
1605 			} else if (saddr == brkbase && size == brksize) {
1606 				if (!(content & CC_CONTENT_HEAP))
1607 					goto exclude;
1608 
1609 			} else if (seg->s_ops == &segspt_shmops) {
1610 				if (type & MAP_NORESERVE) {
1611 					if (!(content & CC_CONTENT_DISM))
1612 						goto exclude;
1613 				} else {
1614 					if (!(content & CC_CONTENT_ISM))
1615 						goto exclude;
1616 				}
1617 
1618 			} else if (seg->s_ops != &segvn_ops) {
1619 				goto exclude;
1620 
1621 			} else if (type & MAP_SHARED) {
1622 				if (shmgetid(p, saddr) != SHMID_NONE) {
1623 					if (!(content & CC_CONTENT_SHM))
1624 						goto exclude;
1625 
1626 				} else if (SEGOP_GETVP(seg, seg->s_base,
1627 				    &mvp) != 0 || mvp == NULL ||
1628 				    mvp->v_type != VREG) {
1629 					if (!(content & CC_CONTENT_SHANON))
1630 						goto exclude;
1631 
1632 				} else {
1633 					if (!(content & CC_CONTENT_SHFILE))
1634 						goto exclude;
1635 				}
1636 
1637 			} else if (SEGOP_GETVP(seg, seg->s_base, &mvp) != 0 ||
1638 			    mvp == NULL || mvp->v_type != VREG) {
1639 				if (!(content & CC_CONTENT_ANON))
1640 					goto exclude;
1641 
1642 			} else if (prot == (PROT_READ | PROT_EXEC)) {
1643 				if (!(content & CC_CONTENT_TEXT))
1644 					goto exclude;
1645 
1646 			} else if (prot == PROT_READ) {
1647 				if (!(content & CC_CONTENT_RODATA))
1648 					goto exclude;
1649 
1650 			} else {
1651 				if (!(content & CC_CONTENT_DATA))
1652 					goto exclude;
1653 			}
1654 
1655 			doffset = roundup(doffset, sizeof (Word));
1656 			v[i].p_offset = doffset;
1657 			v[i].p_filesz = size;
1658 			doffset += size;
1659 exclude:
1660 			i++;
1661 		}
1662 		ASSERT(tmp == NULL);
1663 	}
1664 	AS_LOCK_EXIT(as, &as->a_lock);
1665 
1666 	if (overflow || i != nphdrs) {
1667 		if (ntries++ == 0) {
1668 			kmem_free(bigwad, bigsize);
1669 			goto top;
1670 		}
1671 		cmn_err(CE_WARN, "elfcore: core dump failed for "
1672 		    "process %d; address space is changing", p->p_pid);
1673 		error = EIO;
1674 		goto done;
1675 	}
1676 
1677 	if ((error = core_write(vp, UIO_SYSSPACE, poffset,
1678 	    v, phdrsz, rlimit, credp)) != 0)
1679 		goto done;
1680 
1681 	if ((error = write_old_elfnotes(p, sig, vp, v[0].p_offset, rlimit,
1682 	    credp)) != 0)
1683 		goto done;
1684 
1685 	if ((error = write_elfnotes(p, sig, vp, v[1].p_offset, rlimit,
1686 	    credp, content)) != 0)
1687 		goto done;
1688 
1689 	for (i = 2; i < nphdrs; i++) {
1690 		if (v[i].p_filesz == 0)
1691 			continue;
1692 
1693 		/*
1694 		 * If dumping out this segment fails, rather than failing
1695 		 * the core dump entirely, we reset the size of the mapping
1696 		 * to zero to indicate that the data is absent from the core
1697 		 * file and or in the PF_SUNW_FAILURE flag to differentiate
1698 		 * this from mappings that were excluded due to the core file
1699 		 * content settings.
1700 		 */
1701 		if ((error = core_seg(p, vp, v[i].p_offset,
1702 		    (caddr_t)(uintptr_t)v[i].p_vaddr, v[i].p_filesz,
1703 		    rlimit, credp)) != 0) {
1704 
1705 			/*
1706 			 * Since the space reserved for the segment is now
1707 			 * unused, we stash the errno in the first four
1708 			 * bytes. This undocumented interface will let us
1709 			 * understand the nature of the failure.
1710 			 */
1711 			(void) core_write(vp, UIO_SYSSPACE, v[i].p_offset,
1712 			    &error, sizeof (error), rlimit, credp);
1713 
1714 			v[i].p_filesz = 0;
1715 			v[i].p_flags |= PF_SUNW_FAILURE;
1716 			if ((error = core_write(vp, UIO_SYSSPACE,
1717 			    poffset + sizeof (v[i]) * i, &v[i], sizeof (v[i]),
1718 			    rlimit, credp)) != 0)
1719 				goto done;
1720 		}
1721 	}
1722 
1723 	if (nshdrs > 0) {
1724 		bzero(&bigwad->shdr[0], shdrsz);
1725 
1726 		AS_LOCK_ENTER(as, &as->a_lock, RW_WRITER);
1727 		if ((error = process_scns(content, p, credp, vp,
1728 		    &bigwad->shdr[0], nshdrs, rlimit, &doffset, NULL)) != 0) {
1729 			AS_LOCK_EXIT(as, &as->a_lock);
1730 			goto done;
1731 		}
1732 		AS_LOCK_EXIT(as, &as->a_lock);
1733 
1734 
1735 		if ((error = core_write(vp, UIO_SYSSPACE, soffset,
1736 		    &bigwad->shdr[0], shdrsz, rlimit, credp)) != 0)
1737 			goto done;
1738 	}
1739 
1740 done:
1741 	kmem_free(bigwad, bigsize);
1742 	return (error);
1743 }
1744 
1745 #ifndef	_ELF32_COMPAT
1746 
1747 static struct execsw esw = {
1748 #ifdef	_LP64
1749 	elf64magicstr,
1750 #else	/* _LP64 */
1751 	elf32magicstr,
1752 #endif	/* _LP64 */
1753 	0,
1754 	5,
1755 	elfexec,
1756 	elfcore
1757 };
1758 
1759 static struct modlexec modlexec = {
1760 	&mod_execops, "exec module for elf", &esw
1761 };
1762 
1763 #ifdef	_LP64
1764 extern int elf32exec(vnode_t *vp, execa_t *uap, uarg_t *args,
1765 			intpdata_t *idatap, int level, long *execsz,
1766 			int setid, caddr_t exec_file, cred_t *cred);
1767 extern int elf32core(vnode_t *vp, proc_t *p, cred_t *credp,
1768 			rlim64_t rlimit, int sig, core_content_t content);
1769 
1770 static struct execsw esw32 = {
1771 	elf32magicstr,
1772 	0,
1773 	5,
1774 	elf32exec,
1775 	elf32core
1776 };
1777 
1778 static struct modlexec modlexec32 = {
1779 	&mod_execops, "32-bit exec module for elf", &esw32
1780 };
1781 #endif	/* _LP64 */
1782 
1783 static struct modlinkage modlinkage = {
1784 	MODREV_1,
1785 	(void *)&modlexec,
1786 #ifdef	_LP64
1787 	(void *)&modlexec32,
1788 #endif	/* _LP64 */
1789 	NULL
1790 };
1791 
1792 int
1793 _init(void)
1794 {
1795 	return (mod_install(&modlinkage));
1796 }
1797 
1798 int
1799 _fini(void)
1800 {
1801 	return (mod_remove(&modlinkage));
1802 }
1803 
1804 int
1805 _info(struct modinfo *modinfop)
1806 {
1807 	return (mod_info(&modlinkage, modinfop));
1808 }
1809 
1810 #endif	/* !_ELF32_COMPAT */
1811