xref: /illumos-gate/usr/src/uts/common/exec/elf/elf.c (revision c2aa8c918a0c67f7fd93724a31efac84968fc12c)
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 2007 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 		kmem_free(bigwad, sizeof (struct bigwad));
349 		return (BROP(p)->b_elfexec(vp, uap, args,
350 		    idatap, level + 1, execsz, setid, exec_file, cred,
351 		    brand_action));
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 		setid &= ~EXECSETID_SETID;
703 		ADDAUX(aux, AT_SUN_AUXFLAGS,
704 		    setid ? AF_SUN_SETUGID | auxf : auxf);
705 		/*
706 		 * Hardware capability flag word (performance hints)
707 		 * Used for choosing faster library routines.
708 		 * (Potentially different between 32-bit and 64-bit ABIs)
709 		 */
710 #if defined(_LP64)
711 		if (args->to_model == DATAMODEL_NATIVE)
712 			ADDAUX(aux, AT_SUN_HWCAP, auxv_hwcap)
713 		else
714 			ADDAUX(aux, AT_SUN_HWCAP, auxv_hwcap32)
715 #else
716 		ADDAUX(aux, AT_SUN_HWCAP, auxv_hwcap)
717 #endif
718 		if (branded) {
719 			/*
720 			 * Reserve space for the brand-private aux vectors,
721 			 * and record the user addr of that space.
722 			 */
723 			args->auxp_brand =
724 			    (char *)((char *)args->stackend +
725 			    ((char *)&aux->a_type -
726 			    (char *)bigwad->elfargs));
727 			ADDAUX(aux, AT_SUN_BRAND_AUX1, 0)
728 			ADDAUX(aux, AT_SUN_BRAND_AUX2, 0)
729 			ADDAUX(aux, AT_SUN_BRAND_AUX3, 0)
730 		}
731 
732 		ADDAUX(aux, AT_NULL, 0)
733 		postfixsize = (char *)aux - (char *)bigwad->elfargs;
734 		ASSERT(postfixsize == args->auxsize);
735 		ASSERT(postfixsize <= __KERN_NAUXV_IMPL * sizeof (aux_entry_t));
736 	}
737 
738 	/*
739 	 * For the 64-bit kernel, the limit is big enough that rounding it up
740 	 * to a page can overflow the 64-bit limit, so we check for btopr()
741 	 * overflowing here by comparing it with the unrounded limit in pages.
742 	 * If it hasn't overflowed, compare the exec size with the rounded up
743 	 * limit in pages.  Otherwise, just compare with the unrounded limit.
744 	 */
745 	limit = btop(p->p_vmem_ctl);
746 	roundlimit = btopr(p->p_vmem_ctl);
747 	if ((roundlimit > limit && *execsz > roundlimit) ||
748 	    (roundlimit < limit && *execsz > limit)) {
749 		mutex_enter(&p->p_lock);
750 		(void) rctl_action(rctlproc_legacy[RLIMIT_VMEM], p->p_rctls, p,
751 		    RCA_SAFE);
752 		mutex_exit(&p->p_lock);
753 		error = ENOMEM;
754 		goto bad;
755 	}
756 
757 	bzero(up->u_auxv, sizeof (up->u_auxv));
758 	if (postfixsize) {
759 		int num_auxv;
760 
761 		/*
762 		 * Copy the aux vector to the user stack.
763 		 */
764 		error = execpoststack(args, bigwad->elfargs, postfixsize);
765 		if (error)
766 			goto bad;
767 
768 		/*
769 		 * Copy auxv to the process's user structure for use by /proc.
770 		 * If this is a branded process, the brand's exec routine will
771 		 * copy it's private entries to the user structure later. It
772 		 * relies on the fact that the blank entries are at the end.
773 		 */
774 		num_auxv = postfixsize / sizeof (aux_entry_t);
775 		ASSERT(num_auxv <= sizeof (up->u_auxv) / sizeof (auxv_t));
776 		aux = bigwad->elfargs;
777 		for (i = 0; i < num_auxv; i++) {
778 			up->u_auxv[i].a_type = aux[i].a_type;
779 			up->u_auxv[i].a_un.a_val = (aux_val_t)aux[i].a_un.a_val;
780 		}
781 	}
782 
783 	/*
784 	 * Pass back the starting address so we can set the program counter.
785 	 */
786 	args->entry = (uintptr_t)(ehdrp->e_entry + voffset);
787 
788 	if (!uphdr) {
789 		if (ehdrp->e_type == ET_DYN) {
790 			/*
791 			 * If we are executing a shared library which doesn't
792 			 * have a interpreter (probably ld.so.1) then
793 			 * we don't set the brkbase now.  Instead we
794 			 * delay it's setting until the first call
795 			 * via grow.c::brk().  This permits ld.so.1 to
796 			 * initialize brkbase to the tail of the executable it
797 			 * loads (which is where it needs to be).
798 			 */
799 			bigwad->exenv.ex_brkbase = (caddr_t)0;
800 			bigwad->exenv.ex_bssbase = (caddr_t)0;
801 			bigwad->exenv.ex_brksize = 0;
802 		} else {
803 			bigwad->exenv.ex_brkbase = brkbase;
804 			bigwad->exenv.ex_bssbase = bssbase;
805 			bigwad->exenv.ex_brksize = brksize;
806 		}
807 		bigwad->exenv.ex_magic = elfmagic;
808 		bigwad->exenv.ex_vp = vp;
809 		setexecenv(&bigwad->exenv);
810 	}
811 
812 	ASSERT(error == 0);
813 	goto out;
814 
815 bad:
816 	if (fd != -1)		/* did we open the a.out yet */
817 		(void) execclose(fd);
818 
819 	psignal(p, SIGKILL);
820 
821 	if (error == 0)
822 		error = ENOEXEC;
823 out:
824 	if (phdrbase != NULL)
825 		kmem_free(phdrbase, phdrsize);
826 	kmem_free(bigwad, sizeof (struct bigwad));
827 	return (error);
828 }
829 
830 /*
831  * Compute the memory size requirement for the ELF file.
832  */
833 static size_t
834 elfsize(Ehdr *ehdrp, int nphdrs, caddr_t phdrbase, uintptr_t *lddata)
835 {
836 	size_t	len;
837 	Phdr	*phdrp = (Phdr *)phdrbase;
838 	int	hsize = ehdrp->e_phentsize;
839 	int	first = 1;
840 	int	dfirst = 1;	/* first data segment */
841 	uintptr_t loaddr = 0;
842 	uintptr_t hiaddr = 0;
843 	uintptr_t lo, hi;
844 	int	i;
845 
846 	for (i = nphdrs; i > 0; i--) {
847 		if (phdrp->p_type == PT_LOAD) {
848 			lo = phdrp->p_vaddr;
849 			hi = lo + phdrp->p_memsz;
850 			if (first) {
851 				loaddr = lo;
852 				hiaddr = hi;
853 				first = 0;
854 			} else {
855 				if (loaddr > lo)
856 					loaddr = lo;
857 				if (hiaddr < hi)
858 					hiaddr = hi;
859 			}
860 
861 			/*
862 			 * save the address of the first data segment
863 			 * of a object - used for the AT_SUNW_LDDATA
864 			 * aux entry.
865 			 */
866 			if ((lddata != NULL) && dfirst &&
867 			    (phdrp->p_flags & PF_W)) {
868 				*lddata = lo;
869 				dfirst = 0;
870 			}
871 		}
872 		phdrp = (Phdr *)((caddr_t)phdrp + hsize);
873 	}
874 
875 	len = hiaddr - (loaddr & PAGEMASK);
876 	len = roundup(len, PAGESIZE);
877 
878 	return (len);
879 }
880 
881 /*
882  * Read in the ELF header and program header table.
883  * SUSV3 requires:
884  *	ENOEXEC	File format is not recognized
885  *	EINVAL	Format recognized but execution not supported
886  */
887 static int
888 getelfhead(vnode_t *vp, cred_t *credp, Ehdr *ehdr, int *nshdrs, int *shstrndx,
889     int *nphdrs)
890 {
891 	int error;
892 	ssize_t resid;
893 
894 	/*
895 	 * We got here by the first two bytes in ident,
896 	 * now read the entire ELF header.
897 	 */
898 	if ((error = vn_rdwr(UIO_READ, vp, (caddr_t)ehdr,
899 	    sizeof (Ehdr), (offset_t)0, UIO_SYSSPACE, 0,
900 	    (rlim64_t)0, credp, &resid)) != 0)
901 		return (error);
902 
903 	/*
904 	 * Since a separate version is compiled for handling 32-bit and
905 	 * 64-bit ELF executables on a 64-bit kernel, the 64-bit version
906 	 * doesn't need to be able to deal with 32-bit ELF files.
907 	 */
908 	if (resid != 0 ||
909 	    ehdr->e_ident[EI_MAG2] != ELFMAG2 ||
910 	    ehdr->e_ident[EI_MAG3] != ELFMAG3)
911 		return (ENOEXEC);
912 
913 	if ((ehdr->e_type != ET_EXEC && ehdr->e_type != ET_DYN) ||
914 #if defined(_ILP32) || defined(_ELF32_COMPAT)
915 	    ehdr->e_ident[EI_CLASS] != ELFCLASS32 ||
916 #else
917 	    ehdr->e_ident[EI_CLASS] != ELFCLASS64 ||
918 #endif
919 	    !elfheadcheck(ehdr->e_ident[EI_DATA], ehdr->e_machine,
920 	    ehdr->e_flags))
921 		return (EINVAL);
922 
923 	*nshdrs = ehdr->e_shnum;
924 	*shstrndx = ehdr->e_shstrndx;
925 	*nphdrs = ehdr->e_phnum;
926 
927 	/*
928 	 * If e_shnum, e_shstrndx, or e_phnum is its sentinel value, we need
929 	 * to read in the section header at index zero to acces the true
930 	 * values for those fields.
931 	 */
932 	if ((*nshdrs == 0 && ehdr->e_shoff != 0) ||
933 	    *shstrndx == SHN_XINDEX || *nphdrs == PN_XNUM) {
934 		Shdr shdr;
935 
936 		if (ehdr->e_shoff == 0)
937 			return (EINVAL);
938 
939 		if ((error = vn_rdwr(UIO_READ, vp, (caddr_t)&shdr,
940 		    sizeof (shdr), (offset_t)ehdr->e_shoff, UIO_SYSSPACE, 0,
941 		    (rlim64_t)0, credp, &resid)) != 0)
942 			return (error);
943 
944 		if (*nshdrs == 0)
945 			*nshdrs = shdr.sh_size;
946 		if (*shstrndx == SHN_XINDEX)
947 			*shstrndx = shdr.sh_link;
948 		if (*nphdrs == PN_XNUM && shdr.sh_info != 0)
949 			*nphdrs = shdr.sh_info;
950 	}
951 
952 	return (0);
953 }
954 
955 #ifdef _ELF32_COMPAT
956 extern size_t elf_nphdr_max;
957 #else
958 size_t elf_nphdr_max = 1000;
959 #endif
960 
961 static int
962 getelfphdr(vnode_t *vp, cred_t *credp, const Ehdr *ehdr, int nphdrs,
963     caddr_t *phbasep, ssize_t *phsizep)
964 {
965 	ssize_t resid, minsize;
966 	int err;
967 
968 	/*
969 	 * Since we're going to be using e_phentsize to iterate down the
970 	 * array of program headers, it must be 8-byte aligned or else
971 	 * a we might cause a misaligned access. We use all members through
972 	 * p_flags on 32-bit ELF files and p_memsz on 64-bit ELF files so
973 	 * e_phentsize must be at least large enough to include those
974 	 * members.
975 	 */
976 #if !defined(_LP64) || defined(_ELF32_COMPAT)
977 	minsize = offsetof(Phdr, p_flags) + sizeof (((Phdr *)NULL)->p_flags);
978 #else
979 	minsize = offsetof(Phdr, p_memsz) + sizeof (((Phdr *)NULL)->p_memsz);
980 #endif
981 	if (ehdr->e_phentsize < minsize || (ehdr->e_phentsize & 3))
982 		return (EINVAL);
983 
984 	*phsizep = nphdrs * ehdr->e_phentsize;
985 
986 	if (*phsizep > sizeof (Phdr) * elf_nphdr_max) {
987 		if ((*phbasep = kmem_alloc(*phsizep, KM_NOSLEEP)) == NULL)
988 			return (ENOMEM);
989 	} else {
990 		*phbasep = kmem_alloc(*phsizep, KM_SLEEP);
991 	}
992 
993 	if ((err = vn_rdwr(UIO_READ, vp, *phbasep, *phsizep,
994 	    (offset_t)ehdr->e_phoff, UIO_SYSSPACE, 0, (rlim64_t)0,
995 	    credp, &resid)) != 0) {
996 		kmem_free(*phbasep, *phsizep);
997 		*phbasep = NULL;
998 		return (err);
999 	}
1000 
1001 	return (0);
1002 }
1003 
1004 #ifdef _ELF32_COMPAT
1005 extern size_t elf_nshdr_max;
1006 extern size_t elf_shstrtab_max;
1007 #else
1008 size_t elf_nshdr_max = 10000;
1009 size_t elf_shstrtab_max = 100 * 1024;
1010 #endif
1011 
1012 
1013 static int
1014 getelfshdr(vnode_t *vp, cred_t *credp, const Ehdr *ehdr,
1015     int nshdrs, int shstrndx, caddr_t *shbasep, ssize_t *shsizep,
1016     char **shstrbasep, ssize_t *shstrsizep)
1017 {
1018 	ssize_t resid, minsize;
1019 	int err;
1020 	Shdr *shdr;
1021 
1022 	/*
1023 	 * Since we're going to be using e_shentsize to iterate down the
1024 	 * array of section headers, it must be 8-byte aligned or else
1025 	 * a we might cause a misaligned access. We use all members through
1026 	 * sh_entsize (on both 32- and 64-bit ELF files) so e_shentsize
1027 	 * must be at least large enough to include that member. The index
1028 	 * of the string table section must also be valid.
1029 	 */
1030 	minsize = offsetof(Shdr, sh_entsize) + sizeof (shdr->sh_entsize);
1031 	if (ehdr->e_shentsize < minsize || (ehdr->e_shentsize & 3) ||
1032 	    shstrndx >= nshdrs)
1033 		return (EINVAL);
1034 
1035 	*shsizep = nshdrs * ehdr->e_shentsize;
1036 
1037 	if (*shsizep > sizeof (Shdr) * elf_nshdr_max) {
1038 		if ((*shbasep = kmem_alloc(*shsizep, KM_NOSLEEP)) == NULL)
1039 			return (ENOMEM);
1040 	} else {
1041 		*shbasep = kmem_alloc(*shsizep, KM_SLEEP);
1042 	}
1043 
1044 	if ((err = vn_rdwr(UIO_READ, vp, *shbasep, *shsizep,
1045 	    (offset_t)ehdr->e_shoff, UIO_SYSSPACE, 0, (rlim64_t)0,
1046 	    credp, &resid)) != 0) {
1047 		kmem_free(*shbasep, *shsizep);
1048 		return (err);
1049 	}
1050 
1051 	/*
1052 	 * Pull the section string table out of the vnode; fail if the size
1053 	 * is zero.
1054 	 */
1055 	shdr = (Shdr *)(*shbasep + shstrndx * ehdr->e_shentsize);
1056 	if ((*shstrsizep = shdr->sh_size) == 0) {
1057 		kmem_free(*shbasep, *shsizep);
1058 		return (EINVAL);
1059 	}
1060 
1061 	if (*shstrsizep > elf_shstrtab_max) {
1062 		if ((*shstrbasep = kmem_alloc(*shstrsizep,
1063 		    KM_NOSLEEP)) == NULL) {
1064 			kmem_free(*shbasep, *shsizep);
1065 			return (ENOMEM);
1066 		}
1067 	} else {
1068 		*shstrbasep = kmem_alloc(*shstrsizep, KM_SLEEP);
1069 	}
1070 
1071 	if ((err = vn_rdwr(UIO_READ, vp, *shstrbasep, *shstrsizep,
1072 	    (offset_t)shdr->sh_offset, UIO_SYSSPACE, 0, (rlim64_t)0,
1073 	    credp, &resid)) != 0) {
1074 		kmem_free(*shbasep, *shsizep);
1075 		kmem_free(*shstrbasep, *shstrsizep);
1076 		return (err);
1077 	}
1078 
1079 	/*
1080 	 * Make sure the strtab is null-terminated to make sure we
1081 	 * don't run off the end of the table.
1082 	 */
1083 	(*shstrbasep)[*shstrsizep - 1] = '\0';
1084 
1085 	return (0);
1086 }
1087 
1088 static int
1089 mapelfexec(
1090 	vnode_t *vp,
1091 	Ehdr *ehdr,
1092 	int nphdrs,
1093 	caddr_t phdrbase,
1094 	Phdr **uphdr,
1095 	Phdr **dyphdr,
1096 	Phdr **stphdr,
1097 	Phdr **dtphdr,
1098 	Phdr *dataphdrp,
1099 	caddr_t *bssbase,
1100 	caddr_t *brkbase,
1101 	intptr_t *voffset,
1102 	intptr_t *minaddr,
1103 	size_t len,
1104 	long *execsz,
1105 	size_t *brksize)
1106 {
1107 	Phdr *phdr;
1108 	int i, prot, error;
1109 	caddr_t addr;
1110 	size_t zfodsz;
1111 	int ptload = 0;
1112 	int page;
1113 	off_t offset;
1114 	int hsize = ehdr->e_phentsize;
1115 	caddr_t mintmp = (caddr_t)-1;
1116 	extern int use_brk_lpg;
1117 
1118 	if (ehdr->e_type == ET_DYN) {
1119 		/*
1120 		 * Obtain the virtual address of a hole in the
1121 		 * address space to map the "interpreter".
1122 		 */
1123 		map_addr(&addr, len, (offset_t)0, 1, 0);
1124 		if (addr == NULL)
1125 			return (ENOMEM);
1126 		*voffset = (intptr_t)addr;
1127 	} else {
1128 		*voffset = 0;
1129 	}
1130 	phdr = (Phdr *)phdrbase;
1131 	for (i = nphdrs; i > 0; i--) {
1132 		switch (phdr->p_type) {
1133 		case PT_LOAD:
1134 			if ((*dyphdr != NULL) && (*uphdr == NULL))
1135 				return (0);
1136 
1137 			ptload = 1;
1138 			prot = PROT_USER;
1139 			if (phdr->p_flags & PF_R)
1140 				prot |= PROT_READ;
1141 			if (phdr->p_flags & PF_W)
1142 				prot |= PROT_WRITE;
1143 			if (phdr->p_flags & PF_X)
1144 				prot |= PROT_EXEC;
1145 
1146 			addr = (caddr_t)((uintptr_t)phdr->p_vaddr + *voffset);
1147 
1148 			/*
1149 			 * Keep track of the segment with the lowest starting
1150 			 * address.
1151 			 */
1152 			if (addr < mintmp)
1153 				mintmp = addr;
1154 
1155 			zfodsz = (size_t)phdr->p_memsz - phdr->p_filesz;
1156 
1157 			offset = phdr->p_offset;
1158 			if (((uintptr_t)offset & PAGEOFFSET) ==
1159 			    ((uintptr_t)addr & PAGEOFFSET) &&
1160 			    (!(vp->v_flag & VNOMAP))) {
1161 				page = 1;
1162 			} else {
1163 				page = 0;
1164 			}
1165 
1166 			/*
1167 			 * Set the heap pagesize for OOB when the bss size
1168 			 * is known and use_brk_lpg is not 0.
1169 			 */
1170 			if (brksize != NULL && use_brk_lpg &&
1171 			    zfodsz != 0 && phdr == dataphdrp &&
1172 			    (prot & PROT_WRITE)) {
1173 				size_t tlen = P2NPHASE((uintptr_t)addr +
1174 				    phdr->p_filesz, PAGESIZE);
1175 
1176 				if (zfodsz > tlen) {
1177 					curproc->p_brkpageszc =
1178 					    page_szc(map_pgsz(MAPPGSZ_HEAP,
1179 					    curproc, addr + phdr->p_filesz +
1180 					    tlen, zfodsz - tlen, 0));
1181 				}
1182 			}
1183 
1184 			if (curproc->p_brkpageszc != 0 && phdr == dataphdrp &&
1185 			    (prot & PROT_WRITE)) {
1186 				uint_t	szc = curproc->p_brkpageszc;
1187 				size_t pgsz = page_get_pagesize(szc);
1188 				caddr_t ebss = addr + phdr->p_memsz;
1189 				size_t extra_zfodsz;
1190 
1191 				ASSERT(pgsz > PAGESIZE);
1192 
1193 				extra_zfodsz = P2NPHASE((uintptr_t)ebss, pgsz);
1194 
1195 				if (error = execmap(vp, addr, phdr->p_filesz,
1196 				    zfodsz + extra_zfodsz, phdr->p_offset,
1197 				    prot, page, szc))
1198 					goto bad;
1199 				if (brksize != NULL)
1200 					*brksize = extra_zfodsz;
1201 			} else {
1202 				if (error = execmap(vp, addr, phdr->p_filesz,
1203 				    zfodsz, phdr->p_offset, prot, page, 0))
1204 					goto bad;
1205 			}
1206 
1207 			if (bssbase != NULL && addr >= *bssbase &&
1208 			    phdr == dataphdrp) {
1209 				*bssbase = addr + phdr->p_filesz;
1210 			}
1211 			if (brkbase != NULL && addr >= *brkbase) {
1212 				*brkbase = addr + phdr->p_memsz;
1213 			}
1214 
1215 			*execsz += btopr(phdr->p_memsz);
1216 			break;
1217 
1218 		case PT_INTERP:
1219 			if (ptload)
1220 				goto bad;
1221 			*dyphdr = phdr;
1222 			break;
1223 
1224 		case PT_SHLIB:
1225 			*stphdr = phdr;
1226 			break;
1227 
1228 		case PT_PHDR:
1229 			if (ptload)
1230 				goto bad;
1231 			*uphdr = phdr;
1232 			break;
1233 
1234 		case PT_NULL:
1235 		case PT_DYNAMIC:
1236 		case PT_NOTE:
1237 			break;
1238 
1239 		case PT_SUNWDTRACE:
1240 			if (dtphdr != NULL)
1241 				*dtphdr = phdr;
1242 			break;
1243 
1244 		default:
1245 			break;
1246 		}
1247 		phdr = (Phdr *)((caddr_t)phdr + hsize);
1248 	}
1249 
1250 	if (minaddr != NULL) {
1251 		ASSERT(mintmp != (caddr_t)-1);
1252 		*minaddr = (intptr_t)mintmp;
1253 	}
1254 
1255 	return (0);
1256 bad:
1257 	if (error == 0)
1258 		error = EINVAL;
1259 	return (error);
1260 }
1261 
1262 int
1263 elfnote(vnode_t *vp, offset_t *offsetp, int type, int descsz, void *desc,
1264     rlim64_t rlimit, cred_t *credp)
1265 {
1266 	Note note;
1267 	int error;
1268 
1269 	bzero(&note, sizeof (note));
1270 	bcopy("CORE", note.name, 4);
1271 	note.nhdr.n_type = type;
1272 	/*
1273 	 * The System V ABI states that n_namesz must be the length of the
1274 	 * string that follows the Nhdr structure including the terminating
1275 	 * null. The ABI also specifies that sufficient padding should be
1276 	 * included so that the description that follows the name string
1277 	 * begins on a 4- or 8-byte boundary for 32- and 64-bit binaries
1278 	 * respectively. However, since this change was not made correctly
1279 	 * at the time of the 64-bit port, both 32- and 64-bit binaries
1280 	 * descriptions are only guaranteed to begin on a 4-byte boundary.
1281 	 */
1282 	note.nhdr.n_namesz = 5;
1283 	note.nhdr.n_descsz = roundup(descsz, sizeof (Word));
1284 
1285 	if (error = core_write(vp, UIO_SYSSPACE, *offsetp, &note,
1286 	    sizeof (note), rlimit, credp))
1287 		return (error);
1288 
1289 	*offsetp += sizeof (note);
1290 
1291 	if (error = core_write(vp, UIO_SYSSPACE, *offsetp, desc,
1292 	    note.nhdr.n_descsz, rlimit, credp))
1293 		return (error);
1294 
1295 	*offsetp += note.nhdr.n_descsz;
1296 	return (0);
1297 }
1298 
1299 /*
1300  * Copy the section data from one vnode to the section of another vnode.
1301  */
1302 static void
1303 copy_scn(Shdr *src, vnode_t *src_vp, Shdr *dst, vnode_t *dst_vp, Off *doffset,
1304     void *buf, size_t size, cred_t *credp, rlim64_t rlimit)
1305 {
1306 	ssize_t resid;
1307 	size_t len, n = src->sh_size;
1308 	offset_t off = 0;
1309 
1310 	while (n != 0) {
1311 		len = MIN(size, n);
1312 		if (vn_rdwr(UIO_READ, src_vp, buf, len, src->sh_offset + off,
1313 		    UIO_SYSSPACE, 0, (rlim64_t)0, credp, &resid) != 0 ||
1314 		    resid >= len ||
1315 		    core_write(dst_vp, UIO_SYSSPACE, *doffset + off,
1316 		    buf, len - resid, rlimit, credp) != 0) {
1317 			dst->sh_size = 0;
1318 			dst->sh_offset = 0;
1319 			return;
1320 		}
1321 
1322 		ASSERT(n >= len - resid);
1323 
1324 		n -= len - resid;
1325 		off += len - resid;
1326 	}
1327 
1328 	*doffset += src->sh_size;
1329 }
1330 
1331 #ifdef _ELF32_COMPAT
1332 extern size_t elf_datasz_max;
1333 #else
1334 size_t elf_datasz_max = 1 * 1024 * 1024;
1335 #endif
1336 
1337 /*
1338  * This function processes mappings that correspond to load objects to
1339  * examine their respective sections for elfcore(). It's called once with
1340  * v set to NULL to count the number of sections that we're going to need
1341  * and then again with v set to some allocated buffer that we fill in with
1342  * all the section data.
1343  */
1344 static int
1345 process_scns(core_content_t content, proc_t *p, cred_t *credp, vnode_t *vp,
1346     Shdr *v, int nv, rlim64_t rlimit, Off *doffsetp, int *nshdrsp)
1347 {
1348 	vnode_t *lastvp = NULL;
1349 	struct seg *seg;
1350 	int i, j;
1351 	void *data = NULL;
1352 	size_t datasz = 0;
1353 	shstrtab_t shstrtab;
1354 	struct as *as = p->p_as;
1355 	int error = 0;
1356 
1357 	if (v != NULL)
1358 		shstrtab_init(&shstrtab);
1359 
1360 	i = 1;
1361 	for (seg = AS_SEGFIRST(as); seg != NULL; seg = AS_SEGNEXT(as, seg)) {
1362 		uint_t prot;
1363 		vnode_t *mvp;
1364 		void *tmp = NULL;
1365 		caddr_t saddr = seg->s_base;
1366 		caddr_t naddr;
1367 		caddr_t eaddr;
1368 		size_t segsize;
1369 
1370 		Ehdr ehdr;
1371 		int nshdrs, shstrndx, nphdrs;
1372 		caddr_t shbase;
1373 		ssize_t shsize;
1374 		char *shstrbase;
1375 		ssize_t shstrsize;
1376 
1377 		Shdr *shdr;
1378 		const char *name;
1379 		size_t sz;
1380 		uintptr_t off;
1381 
1382 		int ctf_ndx = 0;
1383 		int symtab_ndx = 0;
1384 
1385 		/*
1386 		 * Since we're just looking for text segments of load
1387 		 * objects, we only care about the protection bits; we don't
1388 		 * care about the actual size of the segment so we use the
1389 		 * reserved size. If the segment's size is zero, there's
1390 		 * something fishy going on so we ignore this segment.
1391 		 */
1392 		if (seg->s_ops != &segvn_ops ||
1393 		    SEGOP_GETVP(seg, seg->s_base, &mvp) != 0 ||
1394 		    mvp == lastvp || mvp == NULL || mvp->v_type != VREG ||
1395 		    (segsize = pr_getsegsize(seg, 1)) == 0)
1396 			continue;
1397 
1398 		eaddr = saddr + segsize;
1399 		prot = pr_getprot(seg, 1, &tmp, &saddr, &naddr, eaddr);
1400 		pr_getprot_done(&tmp);
1401 
1402 		/*
1403 		 * Skip this segment unless the protection bits look like
1404 		 * what we'd expect for a text segment.
1405 		 */
1406 		if ((prot & (PROT_WRITE | PROT_EXEC)) != PROT_EXEC)
1407 			continue;
1408 
1409 		if (getelfhead(mvp, credp, &ehdr, &nshdrs, &shstrndx,
1410 		    &nphdrs) != 0 ||
1411 		    getelfshdr(mvp, credp, &ehdr, nshdrs, shstrndx,
1412 		    &shbase, &shsize, &shstrbase, &shstrsize) != 0)
1413 			continue;
1414 
1415 		off = ehdr.e_shentsize;
1416 		for (j = 1; j < nshdrs; j++, off += ehdr.e_shentsize) {
1417 			Shdr *symtab = NULL, *strtab;
1418 
1419 			shdr = (Shdr *)(shbase + off);
1420 
1421 			if (shdr->sh_name >= shstrsize)
1422 				continue;
1423 
1424 			name = shstrbase + shdr->sh_name;
1425 
1426 			if (strcmp(name, shstrtab_data[STR_CTF]) == 0) {
1427 				if ((content & CC_CONTENT_CTF) == 0 ||
1428 				    ctf_ndx != 0)
1429 					continue;
1430 
1431 				if (shdr->sh_link > 0 &&
1432 				    shdr->sh_link < nshdrs) {
1433 					symtab = (Shdr *)(shbase +
1434 					    shdr->sh_link * ehdr.e_shentsize);
1435 				}
1436 
1437 				if (v != NULL && i < nv - 1) {
1438 					if (shdr->sh_size > datasz &&
1439 					    shdr->sh_size <= elf_datasz_max) {
1440 						if (data != NULL)
1441 							kmem_free(data, datasz);
1442 
1443 						datasz = shdr->sh_size;
1444 						data = kmem_alloc(datasz,
1445 						    KM_SLEEP);
1446 					}
1447 
1448 					v[i].sh_name = shstrtab_ndx(&shstrtab,
1449 					    STR_CTF);
1450 					v[i].sh_addr = (Addr)(uintptr_t)saddr;
1451 					v[i].sh_type = SHT_PROGBITS;
1452 					v[i].sh_addralign = 4;
1453 					*doffsetp = roundup(*doffsetp,
1454 					    v[i].sh_addralign);
1455 					v[i].sh_offset = *doffsetp;
1456 					v[i].sh_size = shdr->sh_size;
1457 					if (symtab == NULL)  {
1458 						v[i].sh_link = 0;
1459 					} else if (symtab->sh_type ==
1460 					    SHT_SYMTAB &&
1461 					    symtab_ndx != 0) {
1462 						v[i].sh_link =
1463 						    symtab_ndx;
1464 					} else {
1465 						v[i].sh_link = i + 1;
1466 					}
1467 
1468 					copy_scn(shdr, mvp, &v[i], vp,
1469 					    doffsetp, data, datasz, credp,
1470 					    rlimit);
1471 				}
1472 
1473 				ctf_ndx = i++;
1474 
1475 				/*
1476 				 * We've already dumped the symtab.
1477 				 */
1478 				if (symtab != NULL &&
1479 				    symtab->sh_type == SHT_SYMTAB &&
1480 				    symtab_ndx != 0)
1481 					continue;
1482 
1483 			} else if (strcmp(name,
1484 			    shstrtab_data[STR_SYMTAB]) == 0) {
1485 				if ((content & CC_CONTENT_SYMTAB) == 0 ||
1486 				    symtab != 0)
1487 					continue;
1488 
1489 				symtab = shdr;
1490 			}
1491 
1492 			if (symtab != NULL) {
1493 				if ((symtab->sh_type != SHT_DYNSYM &&
1494 				    symtab->sh_type != SHT_SYMTAB) ||
1495 				    symtab->sh_link == 0 ||
1496 				    symtab->sh_link >= nshdrs)
1497 					continue;
1498 
1499 				strtab = (Shdr *)(shbase +
1500 				    symtab->sh_link * ehdr.e_shentsize);
1501 
1502 				if (strtab->sh_type != SHT_STRTAB)
1503 					continue;
1504 
1505 				if (v != NULL && i < nv - 2) {
1506 					sz = MAX(symtab->sh_size,
1507 					    strtab->sh_size);
1508 					if (sz > datasz &&
1509 					    sz <= elf_datasz_max) {
1510 						if (data != NULL)
1511 							kmem_free(data, datasz);
1512 
1513 						datasz = sz;
1514 						data = kmem_alloc(datasz,
1515 						    KM_SLEEP);
1516 					}
1517 
1518 					if (symtab->sh_type == SHT_DYNSYM) {
1519 						v[i].sh_name = shstrtab_ndx(
1520 						    &shstrtab, STR_DYNSYM);
1521 						v[i + 1].sh_name = shstrtab_ndx(
1522 						    &shstrtab, STR_DYNSTR);
1523 					} else {
1524 						v[i].sh_name = shstrtab_ndx(
1525 						    &shstrtab, STR_SYMTAB);
1526 						v[i + 1].sh_name = shstrtab_ndx(
1527 						    &shstrtab, STR_STRTAB);
1528 					}
1529 
1530 					v[i].sh_type = symtab->sh_type;
1531 					v[i].sh_addr = symtab->sh_addr;
1532 					if (ehdr.e_type == ET_DYN ||
1533 					    v[i].sh_addr == 0)
1534 						v[i].sh_addr +=
1535 						    (Addr)(uintptr_t)saddr;
1536 					v[i].sh_addralign =
1537 					    symtab->sh_addralign;
1538 					*doffsetp = roundup(*doffsetp,
1539 					    v[i].sh_addralign);
1540 					v[i].sh_offset = *doffsetp;
1541 					v[i].sh_size = symtab->sh_size;
1542 					v[i].sh_link = i + 1;
1543 					v[i].sh_entsize = symtab->sh_entsize;
1544 					v[i].sh_info = symtab->sh_info;
1545 
1546 					copy_scn(symtab, mvp, &v[i], vp,
1547 					    doffsetp, data, datasz, credp,
1548 					    rlimit);
1549 
1550 					v[i + 1].sh_type = SHT_STRTAB;
1551 					v[i + 1].sh_flags = SHF_STRINGS;
1552 					v[i + 1].sh_addr = symtab->sh_addr;
1553 					if (ehdr.e_type == ET_DYN ||
1554 					    v[i + 1].sh_addr == 0)
1555 						v[i + 1].sh_addr +=
1556 						    (Addr)(uintptr_t)saddr;
1557 					v[i + 1].sh_addralign =
1558 					    strtab->sh_addralign;
1559 					*doffsetp = roundup(*doffsetp,
1560 					    v[i + 1].sh_addralign);
1561 					v[i + 1].sh_offset = *doffsetp;
1562 					v[i + 1].sh_size = strtab->sh_size;
1563 
1564 					copy_scn(strtab, mvp, &v[i + 1], vp,
1565 					    doffsetp, data, datasz, credp,
1566 					    rlimit);
1567 				}
1568 
1569 				if (symtab->sh_type == SHT_SYMTAB)
1570 					symtab_ndx = i;
1571 				i += 2;
1572 			}
1573 		}
1574 
1575 		kmem_free(shstrbase, shstrsize);
1576 		kmem_free(shbase, shsize);
1577 
1578 		lastvp = mvp;
1579 	}
1580 
1581 	if (v == NULL) {
1582 		if (i == 1)
1583 			*nshdrsp = 0;
1584 		else
1585 			*nshdrsp = i + 1;
1586 		goto done;
1587 	}
1588 
1589 	if (i != nv - 1) {
1590 		cmn_err(CE_WARN, "elfcore: core dump failed for "
1591 		    "process %d; address space is changing", p->p_pid);
1592 		error = EIO;
1593 		goto done;
1594 	}
1595 
1596 	v[i].sh_name = shstrtab_ndx(&shstrtab, STR_SHSTRTAB);
1597 	v[i].sh_size = shstrtab_size(&shstrtab);
1598 	v[i].sh_addralign = 1;
1599 	*doffsetp = roundup(*doffsetp, v[i].sh_addralign);
1600 	v[i].sh_offset = *doffsetp;
1601 	v[i].sh_flags = SHF_STRINGS;
1602 	v[i].sh_type = SHT_STRTAB;
1603 
1604 	if (v[i].sh_size > datasz) {
1605 		if (data != NULL)
1606 			kmem_free(data, datasz);
1607 
1608 		datasz = v[i].sh_size;
1609 		data = kmem_alloc(datasz,
1610 		    KM_SLEEP);
1611 	}
1612 
1613 	shstrtab_dump(&shstrtab, data);
1614 
1615 	if ((error = core_write(vp, UIO_SYSSPACE, *doffsetp,
1616 	    data, v[i].sh_size, rlimit, credp)) != 0)
1617 		goto done;
1618 
1619 	*doffsetp += v[i].sh_size;
1620 
1621 done:
1622 	if (data != NULL)
1623 		kmem_free(data, datasz);
1624 
1625 	return (error);
1626 }
1627 
1628 int
1629 elfcore(vnode_t *vp, proc_t *p, cred_t *credp, rlim64_t rlimit, int sig,
1630     core_content_t content)
1631 {
1632 	offset_t poffset, soffset;
1633 	Off doffset;
1634 	int error, i, nphdrs, nshdrs;
1635 	int overflow = 0;
1636 	struct seg *seg;
1637 	struct as *as = p->p_as;
1638 	union {
1639 		Ehdr ehdr;
1640 		Phdr phdr[1];
1641 		Shdr shdr[1];
1642 	} *bigwad;
1643 	size_t bigsize;
1644 	size_t phdrsz, shdrsz;
1645 	Ehdr *ehdr;
1646 	Phdr *v;
1647 	caddr_t brkbase;
1648 	size_t brksize;
1649 	caddr_t stkbase;
1650 	size_t stksize;
1651 	int ntries = 0;
1652 
1653 top:
1654 	/*
1655 	 * Make sure we have everything we need (registers, etc.).
1656 	 * All other lwps have already stopped and are in an orderly state.
1657 	 */
1658 	ASSERT(p == ttoproc(curthread));
1659 	prstop(0, 0);
1660 
1661 	AS_LOCK_ENTER(as, &as->a_lock, RW_WRITER);
1662 	nphdrs = prnsegs(as, 0) + 2;		/* two CORE note sections */
1663 
1664 	/*
1665 	 * Count the number of section headers we're going to need.
1666 	 */
1667 	nshdrs = 0;
1668 	if (content & (CC_CONTENT_CTF | CC_CONTENT_SYMTAB)) {
1669 		(void) process_scns(content, p, credp, NULL, NULL, NULL, 0,
1670 		    NULL, &nshdrs);
1671 	}
1672 	AS_LOCK_EXIT(as, &as->a_lock);
1673 
1674 	ASSERT(nshdrs == 0 || nshdrs > 1);
1675 
1676 	/*
1677 	 * The core file contents may required zero section headers, but if
1678 	 * we overflow the 16 bits allotted to the program header count in
1679 	 * the ELF header, we'll need that program header at index zero.
1680 	 */
1681 	if (nshdrs == 0 && nphdrs >= PN_XNUM)
1682 		nshdrs = 1;
1683 
1684 	phdrsz = nphdrs * sizeof (Phdr);
1685 	shdrsz = nshdrs * sizeof (Shdr);
1686 
1687 	bigsize = MAX(sizeof (*bigwad), MAX(phdrsz, shdrsz));
1688 	bigwad = kmem_alloc(bigsize, KM_SLEEP);
1689 
1690 	ehdr = &bigwad->ehdr;
1691 	bzero(ehdr, sizeof (*ehdr));
1692 
1693 	ehdr->e_ident[EI_MAG0] = ELFMAG0;
1694 	ehdr->e_ident[EI_MAG1] = ELFMAG1;
1695 	ehdr->e_ident[EI_MAG2] = ELFMAG2;
1696 	ehdr->e_ident[EI_MAG3] = ELFMAG3;
1697 	ehdr->e_ident[EI_CLASS] = ELFCLASS;
1698 	ehdr->e_type = ET_CORE;
1699 
1700 #if !defined(_LP64) || defined(_ELF32_COMPAT)
1701 
1702 #if defined(__sparc)
1703 	ehdr->e_ident[EI_DATA] = ELFDATA2MSB;
1704 	ehdr->e_machine = EM_SPARC;
1705 #elif defined(__i386) || defined(__i386_COMPAT)
1706 	ehdr->e_ident[EI_DATA] = ELFDATA2LSB;
1707 	ehdr->e_machine = EM_386;
1708 #else
1709 #error "no recognized machine type is defined"
1710 #endif
1711 
1712 #else	/* !defined(_LP64) || defined(_ELF32_COMPAT) */
1713 
1714 #if defined(__sparc)
1715 	ehdr->e_ident[EI_DATA] = ELFDATA2MSB;
1716 	ehdr->e_machine = EM_SPARCV9;
1717 #elif defined(__amd64)
1718 	ehdr->e_ident[EI_DATA] = ELFDATA2LSB;
1719 	ehdr->e_machine = EM_AMD64;
1720 #else
1721 #error "no recognized 64-bit machine type is defined"
1722 #endif
1723 
1724 #endif	/* !defined(_LP64) || defined(_ELF32_COMPAT) */
1725 
1726 	/*
1727 	 * If the count of program headers or section headers or the index
1728 	 * of the section string table can't fit in the mere 16 bits
1729 	 * shortsightedly allotted to them in the ELF header, we use the
1730 	 * extended formats and put the real values in the section header
1731 	 * as index 0.
1732 	 */
1733 	ehdr->e_version = EV_CURRENT;
1734 	ehdr->e_ehsize = sizeof (Ehdr);
1735 
1736 	if (nphdrs >= PN_XNUM)
1737 		ehdr->e_phnum = PN_XNUM;
1738 	else
1739 		ehdr->e_phnum = (unsigned short)nphdrs;
1740 
1741 	ehdr->e_phoff = sizeof (Ehdr);
1742 	ehdr->e_phentsize = sizeof (Phdr);
1743 
1744 	if (nshdrs > 0) {
1745 		if (nshdrs >= SHN_LORESERVE)
1746 			ehdr->e_shnum = 0;
1747 		else
1748 			ehdr->e_shnum = (unsigned short)nshdrs;
1749 
1750 		if (nshdrs - 1 >= SHN_LORESERVE)
1751 			ehdr->e_shstrndx = SHN_XINDEX;
1752 		else
1753 			ehdr->e_shstrndx = (unsigned short)(nshdrs - 1);
1754 
1755 		ehdr->e_shoff = ehdr->e_phoff + ehdr->e_phentsize * nphdrs;
1756 		ehdr->e_shentsize = sizeof (Shdr);
1757 	}
1758 
1759 	if (error = core_write(vp, UIO_SYSSPACE, (offset_t)0, ehdr,
1760 	    sizeof (Ehdr), rlimit, credp))
1761 		goto done;
1762 
1763 	poffset = sizeof (Ehdr);
1764 	soffset = sizeof (Ehdr) + phdrsz;
1765 	doffset = sizeof (Ehdr) + phdrsz + shdrsz;
1766 
1767 	v = &bigwad->phdr[0];
1768 	bzero(v, phdrsz);
1769 
1770 	setup_old_note_header(&v[0], p);
1771 	v[0].p_offset = doffset = roundup(doffset, sizeof (Word));
1772 	doffset += v[0].p_filesz;
1773 
1774 	setup_note_header(&v[1], p);
1775 	v[1].p_offset = doffset = roundup(doffset, sizeof (Word));
1776 	doffset += v[1].p_filesz;
1777 
1778 	mutex_enter(&p->p_lock);
1779 
1780 	brkbase = p->p_brkbase;
1781 	brksize = p->p_brksize;
1782 
1783 	stkbase = p->p_usrstack - p->p_stksize;
1784 	stksize = p->p_stksize;
1785 
1786 	mutex_exit(&p->p_lock);
1787 
1788 	AS_LOCK_ENTER(as, &as->a_lock, RW_WRITER);
1789 	i = 2;
1790 	for (seg = AS_SEGFIRST(as); seg != NULL; seg = AS_SEGNEXT(as, seg)) {
1791 		caddr_t eaddr = seg->s_base + pr_getsegsize(seg, 0);
1792 		caddr_t saddr, naddr;
1793 		void *tmp = NULL;
1794 		extern struct seg_ops segspt_shmops;
1795 
1796 		for (saddr = seg->s_base; saddr < eaddr; saddr = naddr) {
1797 			uint_t prot;
1798 			size_t size;
1799 			int type;
1800 			vnode_t *mvp;
1801 
1802 			prot = pr_getprot(seg, 0, &tmp, &saddr, &naddr, eaddr);
1803 			prot &= PROT_READ | PROT_WRITE | PROT_EXEC;
1804 			if ((size = (size_t)(naddr - saddr)) == 0)
1805 				continue;
1806 			if (i == nphdrs) {
1807 				overflow++;
1808 				continue;
1809 			}
1810 			v[i].p_type = PT_LOAD;
1811 			v[i].p_vaddr = (Addr)(uintptr_t)saddr;
1812 			v[i].p_memsz = size;
1813 			if (prot & PROT_READ)
1814 				v[i].p_flags |= PF_R;
1815 			if (prot & PROT_WRITE)
1816 				v[i].p_flags |= PF_W;
1817 			if (prot & PROT_EXEC)
1818 				v[i].p_flags |= PF_X;
1819 
1820 			/*
1821 			 * Figure out which mappings to include in the core.
1822 			 */
1823 			type = SEGOP_GETTYPE(seg, saddr);
1824 
1825 			if (saddr == stkbase && size == stksize) {
1826 				if (!(content & CC_CONTENT_STACK))
1827 					goto exclude;
1828 
1829 			} else if (saddr == brkbase && size == brksize) {
1830 				if (!(content & CC_CONTENT_HEAP))
1831 					goto exclude;
1832 
1833 			} else if (seg->s_ops == &segspt_shmops) {
1834 				if (type & MAP_NORESERVE) {
1835 					if (!(content & CC_CONTENT_DISM))
1836 						goto exclude;
1837 				} else {
1838 					if (!(content & CC_CONTENT_ISM))
1839 						goto exclude;
1840 				}
1841 
1842 			} else if (seg->s_ops != &segvn_ops) {
1843 				goto exclude;
1844 
1845 			} else if (type & MAP_SHARED) {
1846 				if (shmgetid(p, saddr) != SHMID_NONE) {
1847 					if (!(content & CC_CONTENT_SHM))
1848 						goto exclude;
1849 
1850 				} else if (SEGOP_GETVP(seg, seg->s_base,
1851 				    &mvp) != 0 || mvp == NULL ||
1852 				    mvp->v_type != VREG) {
1853 					if (!(content & CC_CONTENT_SHANON))
1854 						goto exclude;
1855 
1856 				} else {
1857 					if (!(content & CC_CONTENT_SHFILE))
1858 						goto exclude;
1859 				}
1860 
1861 			} else if (SEGOP_GETVP(seg, seg->s_base, &mvp) != 0 ||
1862 			    mvp == NULL || mvp->v_type != VREG) {
1863 				if (!(content & CC_CONTENT_ANON))
1864 					goto exclude;
1865 
1866 			} else if (prot == (PROT_READ | PROT_EXEC)) {
1867 				if (!(content & CC_CONTENT_TEXT))
1868 					goto exclude;
1869 
1870 			} else if (prot == PROT_READ) {
1871 				if (!(content & CC_CONTENT_RODATA))
1872 					goto exclude;
1873 
1874 			} else {
1875 				if (!(content & CC_CONTENT_DATA))
1876 					goto exclude;
1877 			}
1878 
1879 			doffset = roundup(doffset, sizeof (Word));
1880 			v[i].p_offset = doffset;
1881 			v[i].p_filesz = size;
1882 			doffset += size;
1883 exclude:
1884 			i++;
1885 		}
1886 		ASSERT(tmp == NULL);
1887 	}
1888 	AS_LOCK_EXIT(as, &as->a_lock);
1889 
1890 	if (overflow || i != nphdrs) {
1891 		if (ntries++ == 0) {
1892 			kmem_free(bigwad, bigsize);
1893 			goto top;
1894 		}
1895 		cmn_err(CE_WARN, "elfcore: core dump failed for "
1896 		    "process %d; address space is changing", p->p_pid);
1897 		error = EIO;
1898 		goto done;
1899 	}
1900 
1901 	if ((error = core_write(vp, UIO_SYSSPACE, poffset,
1902 	    v, phdrsz, rlimit, credp)) != 0)
1903 		goto done;
1904 
1905 	if ((error = write_old_elfnotes(p, sig, vp, v[0].p_offset, rlimit,
1906 	    credp)) != 0)
1907 		goto done;
1908 
1909 	if ((error = write_elfnotes(p, sig, vp, v[1].p_offset, rlimit,
1910 	    credp, content)) != 0)
1911 		goto done;
1912 
1913 	for (i = 2; i < nphdrs; i++) {
1914 		if (v[i].p_filesz == 0)
1915 			continue;
1916 
1917 		/*
1918 		 * If dumping out this segment fails, rather than failing
1919 		 * the core dump entirely, we reset the size of the mapping
1920 		 * to zero to indicate that the data is absent from the core
1921 		 * file and or in the PF_SUNW_FAILURE flag to differentiate
1922 		 * this from mappings that were excluded due to the core file
1923 		 * content settings.
1924 		 */
1925 		if ((error = core_seg(p, vp, v[i].p_offset,
1926 		    (caddr_t)(uintptr_t)v[i].p_vaddr, v[i].p_filesz,
1927 		    rlimit, credp)) != 0) {
1928 
1929 			/*
1930 			 * Since the space reserved for the segment is now
1931 			 * unused, we stash the errno in the first four
1932 			 * bytes. This undocumented interface will let us
1933 			 * understand the nature of the failure.
1934 			 */
1935 			(void) core_write(vp, UIO_SYSSPACE, v[i].p_offset,
1936 			    &error, sizeof (error), rlimit, credp);
1937 
1938 			v[i].p_filesz = 0;
1939 			v[i].p_flags |= PF_SUNW_FAILURE;
1940 			if ((error = core_write(vp, UIO_SYSSPACE,
1941 			    poffset + sizeof (v[i]) * i, &v[i], sizeof (v[i]),
1942 			    rlimit, credp)) != 0)
1943 				goto done;
1944 		}
1945 	}
1946 
1947 	if (nshdrs > 0) {
1948 		bzero(&bigwad->shdr[0], shdrsz);
1949 
1950 		if (nshdrs >= SHN_LORESERVE)
1951 			bigwad->shdr[0].sh_size = nshdrs;
1952 
1953 		if (nshdrs - 1 >= SHN_LORESERVE)
1954 			bigwad->shdr[0].sh_link = nshdrs - 1;
1955 
1956 		if (nphdrs >= PN_XNUM)
1957 			bigwad->shdr[0].sh_info = nphdrs;
1958 
1959 		if (nshdrs > 1) {
1960 			AS_LOCK_ENTER(as, &as->a_lock, RW_WRITER);
1961 			if ((error = process_scns(content, p, credp, vp,
1962 			    &bigwad->shdr[0], nshdrs, rlimit, &doffset,
1963 			    NULL)) != 0) {
1964 				AS_LOCK_EXIT(as, &as->a_lock);
1965 				goto done;
1966 			}
1967 			AS_LOCK_EXIT(as, &as->a_lock);
1968 		}
1969 
1970 		if ((error = core_write(vp, UIO_SYSSPACE, soffset,
1971 		    &bigwad->shdr[0], shdrsz, rlimit, credp)) != 0)
1972 			goto done;
1973 	}
1974 
1975 done:
1976 	kmem_free(bigwad, bigsize);
1977 	return (error);
1978 }
1979 
1980 #ifndef	_ELF32_COMPAT
1981 
1982 static struct execsw esw = {
1983 #ifdef	_LP64
1984 	elf64magicstr,
1985 #else	/* _LP64 */
1986 	elf32magicstr,
1987 #endif	/* _LP64 */
1988 	0,
1989 	5,
1990 	elfexec,
1991 	elfcore
1992 };
1993 
1994 static struct modlexec modlexec = {
1995 	&mod_execops, "exec module for elf %I%", &esw
1996 };
1997 
1998 #ifdef	_LP64
1999 extern int elf32exec(vnode_t *vp, execa_t *uap, uarg_t *args,
2000 			intpdata_t *idatap, int level, long *execsz,
2001 			int setid, caddr_t exec_file, cred_t *cred,
2002 			int brand_action);
2003 extern int elf32core(vnode_t *vp, proc_t *p, cred_t *credp,
2004 			rlim64_t rlimit, int sig, core_content_t content);
2005 
2006 static struct execsw esw32 = {
2007 	elf32magicstr,
2008 	0,
2009 	5,
2010 	elf32exec,
2011 	elf32core
2012 };
2013 
2014 static struct modlexec modlexec32 = {
2015 	&mod_execops, "32-bit exec module for elf", &esw32
2016 };
2017 #endif	/* _LP64 */
2018 
2019 static struct modlinkage modlinkage = {
2020 	MODREV_1,
2021 	(void *)&modlexec,
2022 #ifdef	_LP64
2023 	(void *)&modlexec32,
2024 #endif	/* _LP64 */
2025 	NULL
2026 };
2027 
2028 int
2029 _init(void)
2030 {
2031 	return (mod_install(&modlinkage));
2032 }
2033 
2034 int
2035 _fini(void)
2036 {
2037 	return (mod_remove(&modlinkage));
2038 }
2039 
2040 int
2041 _info(struct modinfo *modinfop)
2042 {
2043 	return (mod_info(&modlinkage, modinfop));
2044 }
2045 
2046 #endif	/* !_ELF32_COMPAT */
2047