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