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