xref: /illumos-gate/usr/src/lib/libproc/common/Pcore.c (revision a7a845e4)
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  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 /*
26  * Copyright 2012 DEY Storage Systems, Inc.  All rights reserved.
27  * Copyright (c) 2013, Joyent, Inc. All rights reserved.
28  */
29 
30 #include <sys/types.h>
31 #include <sys/utsname.h>
32 #include <sys/sysmacros.h>
33 #include <sys/proc.h>
34 
35 #include <alloca.h>
36 #include <rtld_db.h>
37 #include <libgen.h>
38 #include <limits.h>
39 #include <string.h>
40 #include <stdlib.h>
41 #include <unistd.h>
42 #include <errno.h>
43 #include <gelf.h>
44 #include <stddef.h>
45 #include <signal.h>
46 
47 #include "libproc.h"
48 #include "Pcontrol.h"
49 #include "P32ton.h"
50 #include "Putil.h"
51 
52 /*
53  * Pcore.c - Code to initialize a ps_prochandle from a core dump.  We
54  * allocate an additional structure to hold information from the core
55  * file, and attach this to the standard ps_prochandle in place of the
56  * ability to examine /proc/<pid>/ files.
57  */
58 
59 /*
60  * Basic i/o function for reading and writing from the process address space
61  * stored in the core file and associated shared libraries.  We compute the
62  * appropriate fd and offsets, and let the provided prw function do the rest.
63  */
64 static ssize_t
65 core_rw(struct ps_prochandle *P, void *buf, size_t n, uintptr_t addr,
66     ssize_t (*prw)(int, void *, size_t, off64_t))
67 {
68 	ssize_t resid = n;
69 
70 	while (resid != 0) {
71 		map_info_t *mp = Paddr2mptr(P, addr);
72 
73 		uintptr_t mapoff;
74 		ssize_t len;
75 		off64_t off;
76 		int fd;
77 
78 		if (mp == NULL)
79 			break;	/* No mapping for this address */
80 
81 		if (mp->map_pmap.pr_mflags & MA_RESERVED1) {
82 			if (mp->map_file == NULL || mp->map_file->file_fd < 0)
83 				break;	/* No file or file not open */
84 
85 			fd = mp->map_file->file_fd;
86 		} else
87 			fd = P->asfd;
88 
89 		mapoff = addr - mp->map_pmap.pr_vaddr;
90 		len = MIN(resid, mp->map_pmap.pr_size - mapoff);
91 		off = mp->map_offset + mapoff;
92 
93 		if ((len = prw(fd, buf, len, off)) <= 0)
94 			break;
95 
96 		resid -= len;
97 		addr += len;
98 		buf = (char *)buf + len;
99 	}
100 
101 	/*
102 	 * Important: Be consistent with the behavior of i/o on the as file:
103 	 * writing to an invalid address yields EIO; reading from an invalid
104 	 * address falls through to returning success and zero bytes.
105 	 */
106 	if (resid == n && n != 0 && prw != pread64) {
107 		errno = EIO;
108 		return (-1);
109 	}
110 
111 	return (n - resid);
112 }
113 
114 static ssize_t
115 Pread_core(struct ps_prochandle *P, void *buf, size_t n, uintptr_t addr)
116 {
117 	return (core_rw(P, buf, n, addr, pread64));
118 }
119 
120 static ssize_t
121 Pwrite_core(struct ps_prochandle *P, const void *buf, size_t n, uintptr_t addr)
122 {
123 	return (core_rw(P, (void *)buf, n, addr,
124 	    (ssize_t (*)(int, void *, size_t, off64_t)) pwrite64));
125 }
126 
127 static const ps_rwops_t P_core_ops = { Pread_core, Pwrite_core };
128 
129 /*
130  * Return the lwp_info_t for the given lwpid.  If no such lwpid has been
131  * encountered yet, allocate a new structure and return a pointer to it.
132  * Create a list of lwp_info_t structures sorted in decreasing lwp_id order.
133  */
134 static lwp_info_t *
135 lwpid2info(struct ps_prochandle *P, lwpid_t id)
136 {
137 	lwp_info_t *lwp = list_next(&P->core->core_lwp_head);
138 	lwp_info_t *next;
139 	uint_t i;
140 
141 	for (i = 0; i < P->core->core_nlwp; i++, lwp = list_next(lwp)) {
142 		if (lwp->lwp_id == id) {
143 			P->core->core_lwp = lwp;
144 			return (lwp);
145 		}
146 		if (lwp->lwp_id < id) {
147 			break;
148 		}
149 	}
150 
151 	next = lwp;
152 	if ((lwp = calloc(1, sizeof (lwp_info_t))) == NULL)
153 		return (NULL);
154 
155 	list_link(lwp, next);
156 	lwp->lwp_id = id;
157 
158 	P->core->core_lwp = lwp;
159 	P->core->core_nlwp++;
160 
161 	return (lwp);
162 }
163 
164 /*
165  * The core file itself contains a series of NOTE segments containing saved
166  * structures from /proc at the time the process died.  For each note we
167  * comprehend, we define a function to read it in from the core file,
168  * convert it to our native data model if necessary, and store it inside
169  * the ps_prochandle.  Each function is invoked by Pfgrab_core() with the
170  * seek pointer on P->asfd positioned appropriately.  We populate a table
171  * of pointers to these note functions below.
172  */
173 
174 static int
175 note_pstatus(struct ps_prochandle *P, size_t nbytes)
176 {
177 #ifdef _LP64
178 	if (P->core->core_dmodel == PR_MODEL_ILP32) {
179 		pstatus32_t ps32;
180 
181 		if (nbytes < sizeof (pstatus32_t) ||
182 		    read(P->asfd, &ps32, sizeof (ps32)) != sizeof (ps32))
183 			goto err;
184 
185 		pstatus_32_to_n(&ps32, &P->status);
186 
187 	} else
188 #endif
189 	if (nbytes < sizeof (pstatus_t) ||
190 	    read(P->asfd, &P->status, sizeof (pstatus_t)) != sizeof (pstatus_t))
191 		goto err;
192 
193 	P->orig_status = P->status;
194 	P->pid = P->status.pr_pid;
195 
196 	return (0);
197 
198 err:
199 	dprintf("Pgrab_core: failed to read NT_PSTATUS\n");
200 	return (-1);
201 }
202 
203 static int
204 note_lwpstatus(struct ps_prochandle *P, size_t nbytes)
205 {
206 	lwp_info_t *lwp;
207 	lwpstatus_t lps;
208 
209 #ifdef _LP64
210 	if (P->core->core_dmodel == PR_MODEL_ILP32) {
211 		lwpstatus32_t l32;
212 
213 		if (nbytes < sizeof (lwpstatus32_t) ||
214 		    read(P->asfd, &l32, sizeof (l32)) != sizeof (l32))
215 			goto err;
216 
217 		lwpstatus_32_to_n(&l32, &lps);
218 	} else
219 #endif
220 	if (nbytes < sizeof (lwpstatus_t) ||
221 	    read(P->asfd, &lps, sizeof (lps)) != sizeof (lps))
222 		goto err;
223 
224 	if ((lwp = lwpid2info(P, lps.pr_lwpid)) == NULL) {
225 		dprintf("Pgrab_core: failed to add NT_LWPSTATUS\n");
226 		return (-1);
227 	}
228 
229 	/*
230 	 * Erase a useless and confusing artifact of the kernel implementation:
231 	 * the lwps which did *not* create the core will show SIGKILL.  We can
232 	 * be assured this is bogus because SIGKILL can't produce core files.
233 	 */
234 	if (lps.pr_cursig == SIGKILL)
235 		lps.pr_cursig = 0;
236 
237 	(void) memcpy(&lwp->lwp_status, &lps, sizeof (lps));
238 	return (0);
239 
240 err:
241 	dprintf("Pgrab_core: failed to read NT_LWPSTATUS\n");
242 	return (-1);
243 }
244 
245 static int
246 note_psinfo(struct ps_prochandle *P, size_t nbytes)
247 {
248 #ifdef _LP64
249 	if (P->core->core_dmodel == PR_MODEL_ILP32) {
250 		psinfo32_t ps32;
251 
252 		if (nbytes < sizeof (psinfo32_t) ||
253 		    read(P->asfd, &ps32, sizeof (ps32)) != sizeof (ps32))
254 			goto err;
255 
256 		psinfo_32_to_n(&ps32, &P->psinfo);
257 	} else
258 #endif
259 	if (nbytes < sizeof (psinfo_t) ||
260 	    read(P->asfd, &P->psinfo, sizeof (psinfo_t)) != sizeof (psinfo_t))
261 		goto err;
262 
263 	dprintf("pr_fname = <%s>\n", P->psinfo.pr_fname);
264 	dprintf("pr_psargs = <%s>\n", P->psinfo.pr_psargs);
265 	dprintf("pr_wstat = 0x%x\n", P->psinfo.pr_wstat);
266 
267 	return (0);
268 
269 err:
270 	dprintf("Pgrab_core: failed to read NT_PSINFO\n");
271 	return (-1);
272 }
273 
274 static int
275 note_lwpsinfo(struct ps_prochandle *P, size_t nbytes)
276 {
277 	lwp_info_t *lwp;
278 	lwpsinfo_t lps;
279 
280 #ifdef _LP64
281 	if (P->core->core_dmodel == PR_MODEL_ILP32) {
282 		lwpsinfo32_t l32;
283 
284 		if (nbytes < sizeof (lwpsinfo32_t) ||
285 		    read(P->asfd, &l32, sizeof (l32)) != sizeof (l32))
286 			goto err;
287 
288 		lwpsinfo_32_to_n(&l32, &lps);
289 	} else
290 #endif
291 	if (nbytes < sizeof (lwpsinfo_t) ||
292 	    read(P->asfd, &lps, sizeof (lps)) != sizeof (lps))
293 		goto err;
294 
295 	if ((lwp = lwpid2info(P, lps.pr_lwpid)) == NULL) {
296 		dprintf("Pgrab_core: failed to add NT_LWPSINFO\n");
297 		return (-1);
298 	}
299 
300 	(void) memcpy(&lwp->lwp_psinfo, &lps, sizeof (lps));
301 	return (0);
302 
303 err:
304 	dprintf("Pgrab_core: failed to read NT_LWPSINFO\n");
305 	return (-1);
306 }
307 
308 static int
309 note_fdinfo(struct ps_prochandle *P, size_t nbytes)
310 {
311 	prfdinfo_t prfd;
312 	fd_info_t *fip;
313 
314 	if ((nbytes < sizeof (prfd)) ||
315 	    (read(P->asfd, &prfd, sizeof (prfd)) != sizeof (prfd))) {
316 		dprintf("Pgrab_core: failed to read NT_FDINFO\n");
317 		return (-1);
318 	}
319 
320 	if ((fip = Pfd2info(P, prfd.pr_fd)) == NULL) {
321 		dprintf("Pgrab_core: failed to add NT_FDINFO\n");
322 		return (-1);
323 	}
324 	(void) memcpy(&fip->fd_info, &prfd, sizeof (prfd));
325 	return (0);
326 }
327 
328 static int
329 note_platform(struct ps_prochandle *P, size_t nbytes)
330 {
331 	char *plat;
332 
333 	if (P->core->core_platform != NULL)
334 		return (0);	/* Already seen */
335 
336 	if (nbytes != 0 && ((plat = malloc(nbytes + 1)) != NULL)) {
337 		if (read(P->asfd, plat, nbytes) != nbytes) {
338 			dprintf("Pgrab_core: failed to read NT_PLATFORM\n");
339 			free(plat);
340 			return (-1);
341 		}
342 		plat[nbytes - 1] = '\0';
343 		P->core->core_platform = plat;
344 	}
345 
346 	return (0);
347 }
348 
349 static int
350 note_utsname(struct ps_prochandle *P, size_t nbytes)
351 {
352 	size_t ubytes = sizeof (struct utsname);
353 	struct utsname *utsp;
354 
355 	if (P->core->core_uts != NULL || nbytes < ubytes)
356 		return (0);	/* Already seen or bad size */
357 
358 	if ((utsp = malloc(ubytes)) == NULL)
359 		return (-1);
360 
361 	if (read(P->asfd, utsp, ubytes) != ubytes) {
362 		dprintf("Pgrab_core: failed to read NT_UTSNAME\n");
363 		free(utsp);
364 		return (-1);
365 	}
366 
367 	if (_libproc_debug) {
368 		dprintf("uts.sysname = \"%s\"\n", utsp->sysname);
369 		dprintf("uts.nodename = \"%s\"\n", utsp->nodename);
370 		dprintf("uts.release = \"%s\"\n", utsp->release);
371 		dprintf("uts.version = \"%s\"\n", utsp->version);
372 		dprintf("uts.machine = \"%s\"\n", utsp->machine);
373 	}
374 
375 	P->core->core_uts = utsp;
376 	return (0);
377 }
378 
379 static int
380 note_content(struct ps_prochandle *P, size_t nbytes)
381 {
382 	core_content_t content;
383 
384 	if (sizeof (P->core->core_content) != nbytes)
385 		return (-1);
386 
387 	if (read(P->asfd, &content, sizeof (content)) != sizeof (content))
388 		return (-1);
389 
390 	P->core->core_content = content;
391 
392 	dprintf("core content = %llx\n", content);
393 
394 	return (0);
395 }
396 
397 static int
398 note_cred(struct ps_prochandle *P, size_t nbytes)
399 {
400 	prcred_t *pcrp;
401 	int ngroups;
402 	const size_t min_size = sizeof (prcred_t) - sizeof (gid_t);
403 
404 	/*
405 	 * We allow for prcred_t notes that are actually smaller than a
406 	 * prcred_t since the last member isn't essential if there are
407 	 * no group memberships. This allows for more flexibility when it
408 	 * comes to slightly malformed -- but still valid -- notes.
409 	 */
410 	if (P->core->core_cred != NULL || nbytes < min_size)
411 		return (0);	/* Already seen or bad size */
412 
413 	ngroups = (nbytes - min_size) / sizeof (gid_t);
414 	nbytes = sizeof (prcred_t) + (ngroups - 1) * sizeof (gid_t);
415 
416 	if ((pcrp = malloc(nbytes)) == NULL)
417 		return (-1);
418 
419 	if (read(P->asfd, pcrp, nbytes) != nbytes) {
420 		dprintf("Pgrab_core: failed to read NT_PRCRED\n");
421 		free(pcrp);
422 		return (-1);
423 	}
424 
425 	if (pcrp->pr_ngroups > ngroups) {
426 		dprintf("pr_ngroups = %d; resetting to %d based on note size\n",
427 		    pcrp->pr_ngroups, ngroups);
428 		pcrp->pr_ngroups = ngroups;
429 	}
430 
431 	P->core->core_cred = pcrp;
432 	return (0);
433 }
434 
435 #if defined(__i386) || defined(__amd64)
436 static int
437 note_ldt(struct ps_prochandle *P, size_t nbytes)
438 {
439 	struct ssd *pldt;
440 	uint_t nldt;
441 
442 	if (P->core->core_ldt != NULL || nbytes < sizeof (struct ssd))
443 		return (0);	/* Already seen or bad size */
444 
445 	nldt = nbytes / sizeof (struct ssd);
446 	nbytes = nldt * sizeof (struct ssd);
447 
448 	if ((pldt = malloc(nbytes)) == NULL)
449 		return (-1);
450 
451 	if (read(P->asfd, pldt, nbytes) != nbytes) {
452 		dprintf("Pgrab_core: failed to read NT_LDT\n");
453 		free(pldt);
454 		return (-1);
455 	}
456 
457 	P->core->core_ldt = pldt;
458 	P->core->core_nldt = nldt;
459 	return (0);
460 }
461 #endif	/* __i386 */
462 
463 static int
464 note_priv(struct ps_prochandle *P, size_t nbytes)
465 {
466 	prpriv_t *pprvp;
467 
468 	if (P->core->core_priv != NULL || nbytes < sizeof (prpriv_t))
469 		return (0);	/* Already seen or bad size */
470 
471 	if ((pprvp = malloc(nbytes)) == NULL)
472 		return (-1);
473 
474 	if (read(P->asfd, pprvp, nbytes) != nbytes) {
475 		dprintf("Pgrab_core: failed to read NT_PRPRIV\n");
476 		free(pprvp);
477 		return (-1);
478 	}
479 
480 	P->core->core_priv = pprvp;
481 	P->core->core_priv_size = nbytes;
482 	return (0);
483 }
484 
485 static int
486 note_priv_info(struct ps_prochandle *P, size_t nbytes)
487 {
488 	extern void *__priv_parse_info();
489 	priv_impl_info_t *ppii;
490 
491 	if (P->core->core_privinfo != NULL ||
492 	    nbytes < sizeof (priv_impl_info_t))
493 		return (0);	/* Already seen or bad size */
494 
495 	if ((ppii = malloc(nbytes)) == NULL)
496 		return (-1);
497 
498 	if (read(P->asfd, ppii, nbytes) != nbytes ||
499 	    PRIV_IMPL_INFO_SIZE(ppii) != nbytes) {
500 		dprintf("Pgrab_core: failed to read NT_PRPRIVINFO\n");
501 		free(ppii);
502 		return (-1);
503 	}
504 
505 	P->core->core_privinfo = __priv_parse_info(ppii);
506 	P->core->core_ppii = ppii;
507 	return (0);
508 }
509 
510 static int
511 note_zonename(struct ps_prochandle *P, size_t nbytes)
512 {
513 	char *zonename;
514 
515 	if (P->core->core_zonename != NULL)
516 		return (0);	/* Already seen */
517 
518 	if (nbytes != 0) {
519 		if ((zonename = malloc(nbytes)) == NULL)
520 			return (-1);
521 		if (read(P->asfd, zonename, nbytes) != nbytes) {
522 			dprintf("Pgrab_core: failed to read NT_ZONENAME\n");
523 			free(zonename);
524 			return (-1);
525 		}
526 		zonename[nbytes - 1] = '\0';
527 		P->core->core_zonename = zonename;
528 	}
529 
530 	return (0);
531 }
532 
533 static int
534 note_auxv(struct ps_prochandle *P, size_t nbytes)
535 {
536 	size_t n, i;
537 
538 #ifdef _LP64
539 	if (P->core->core_dmodel == PR_MODEL_ILP32) {
540 		auxv32_t *a32;
541 
542 		n = nbytes / sizeof (auxv32_t);
543 		nbytes = n * sizeof (auxv32_t);
544 		a32 = alloca(nbytes);
545 
546 		if (read(P->asfd, a32, nbytes) != nbytes) {
547 			dprintf("Pgrab_core: failed to read NT_AUXV\n");
548 			return (-1);
549 		}
550 
551 		if ((P->auxv = malloc(sizeof (auxv_t) * (n + 1))) == NULL)
552 			return (-1);
553 
554 		for (i = 0; i < n; i++)
555 			auxv_32_to_n(&a32[i], &P->auxv[i]);
556 
557 	} else {
558 #endif
559 		n = nbytes / sizeof (auxv_t);
560 		nbytes = n * sizeof (auxv_t);
561 
562 		if ((P->auxv = malloc(nbytes + sizeof (auxv_t))) == NULL)
563 			return (-1);
564 
565 		if (read(P->asfd, P->auxv, nbytes) != nbytes) {
566 			free(P->auxv);
567 			P->auxv = NULL;
568 			return (-1);
569 		}
570 #ifdef _LP64
571 	}
572 #endif
573 
574 	if (_libproc_debug) {
575 		for (i = 0; i < n; i++) {
576 			dprintf("P->auxv[%lu] = ( %d, 0x%lx )\n", (ulong_t)i,
577 			    P->auxv[i].a_type, P->auxv[i].a_un.a_val);
578 		}
579 	}
580 
581 	/*
582 	 * Defensive coding for loops which depend upon the auxv array being
583 	 * terminated by an AT_NULL element; in each case, we've allocated
584 	 * P->auxv to have an additional element which we force to be AT_NULL.
585 	 */
586 	P->auxv[n].a_type = AT_NULL;
587 	P->auxv[n].a_un.a_val = 0L;
588 	P->nauxv = (int)n;
589 
590 	return (0);
591 }
592 
593 #ifdef __sparc
594 static int
595 note_xreg(struct ps_prochandle *P, size_t nbytes)
596 {
597 	lwp_info_t *lwp = P->core->core_lwp;
598 	size_t xbytes = sizeof (prxregset_t);
599 	prxregset_t *xregs;
600 
601 	if (lwp == NULL || lwp->lwp_xregs != NULL || nbytes < xbytes)
602 		return (0);	/* No lwp yet, already seen, or bad size */
603 
604 	if ((xregs = malloc(xbytes)) == NULL)
605 		return (-1);
606 
607 	if (read(P->asfd, xregs, xbytes) != xbytes) {
608 		dprintf("Pgrab_core: failed to read NT_PRXREG\n");
609 		free(xregs);
610 		return (-1);
611 	}
612 
613 	lwp->lwp_xregs = xregs;
614 	return (0);
615 }
616 
617 static int
618 note_gwindows(struct ps_prochandle *P, size_t nbytes)
619 {
620 	lwp_info_t *lwp = P->core->core_lwp;
621 
622 	if (lwp == NULL || lwp->lwp_gwins != NULL || nbytes == 0)
623 		return (0);	/* No lwp yet or already seen or no data */
624 
625 	if ((lwp->lwp_gwins = malloc(sizeof (gwindows_t))) == NULL)
626 		return (-1);
627 
628 	/*
629 	 * Since the amount of gwindows data varies with how many windows were
630 	 * actually saved, we just read up to the minimum of the note size
631 	 * and the size of the gwindows_t type.  It doesn't matter if the read
632 	 * fails since we have to zero out gwindows first anyway.
633 	 */
634 #ifdef _LP64
635 	if (P->core->core_dmodel == PR_MODEL_ILP32) {
636 		gwindows32_t g32;
637 
638 		(void) memset(&g32, 0, sizeof (g32));
639 		(void) read(P->asfd, &g32, MIN(nbytes, sizeof (g32)));
640 		gwindows_32_to_n(&g32, lwp->lwp_gwins);
641 
642 	} else {
643 #endif
644 		(void) memset(lwp->lwp_gwins, 0, sizeof (gwindows_t));
645 		(void) read(P->asfd, lwp->lwp_gwins,
646 		    MIN(nbytes, sizeof (gwindows_t)));
647 #ifdef _LP64
648 	}
649 #endif
650 	return (0);
651 }
652 
653 #ifdef __sparcv9
654 static int
655 note_asrs(struct ps_prochandle *P, size_t nbytes)
656 {
657 	lwp_info_t *lwp = P->core->core_lwp;
658 	int64_t *asrs;
659 
660 	if (lwp == NULL || lwp->lwp_asrs != NULL || nbytes < sizeof (asrset_t))
661 		return (0);	/* No lwp yet, already seen, or bad size */
662 
663 	if ((asrs = malloc(sizeof (asrset_t))) == NULL)
664 		return (-1);
665 
666 	if (read(P->asfd, asrs, sizeof (asrset_t)) != sizeof (asrset_t)) {
667 		dprintf("Pgrab_core: failed to read NT_ASRS\n");
668 		free(asrs);
669 		return (-1);
670 	}
671 
672 	lwp->lwp_asrs = asrs;
673 	return (0);
674 }
675 #endif	/* __sparcv9 */
676 #endif	/* __sparc */
677 
678 static int
679 note_spymaster(struct ps_prochandle *P, size_t nbytes)
680 {
681 #ifdef _LP64
682 	if (P->core->core_dmodel == PR_MODEL_ILP32) {
683 		psinfo32_t ps32;
684 
685 		if (nbytes < sizeof (psinfo32_t) ||
686 		    read(P->asfd, &ps32, sizeof (ps32)) != sizeof (ps32))
687 			goto err;
688 
689 		psinfo_32_to_n(&ps32, &P->spymaster);
690 	} else
691 #endif
692 	if (nbytes < sizeof (psinfo_t) || read(P->asfd,
693 	    &P->spymaster, sizeof (psinfo_t)) != sizeof (psinfo_t))
694 		goto err;
695 
696 	dprintf("spymaster pr_fname = <%s>\n", P->psinfo.pr_fname);
697 	dprintf("spymaster pr_psargs = <%s>\n", P->psinfo.pr_psargs);
698 	dprintf("spymaster pr_wstat = 0x%x\n", P->psinfo.pr_wstat);
699 
700 	return (0);
701 
702 err:
703 	dprintf("Pgrab_core: failed to read NT_SPYMASTER\n");
704 	return (-1);
705 }
706 
707 /*ARGSUSED*/
708 static int
709 note_notsup(struct ps_prochandle *P, size_t nbytes)
710 {
711 	dprintf("skipping unsupported note type\n");
712 	return (0);
713 }
714 
715 /*
716  * Populate a table of function pointers indexed by Note type with our
717  * functions to process each type of core file note:
718  */
719 static int (*nhdlrs[])(struct ps_prochandle *, size_t) = {
720 	note_notsup,		/*  0	unassigned		*/
721 	note_notsup,		/*  1	NT_PRSTATUS (old)	*/
722 	note_notsup,		/*  2	NT_PRFPREG (old)	*/
723 	note_notsup,		/*  3	NT_PRPSINFO (old)	*/
724 #ifdef __sparc
725 	note_xreg,		/*  4	NT_PRXREG		*/
726 #else
727 	note_notsup,		/*  4	NT_PRXREG		*/
728 #endif
729 	note_platform,		/*  5	NT_PLATFORM		*/
730 	note_auxv,		/*  6	NT_AUXV			*/
731 #ifdef __sparc
732 	note_gwindows,		/*  7	NT_GWINDOWS		*/
733 #ifdef __sparcv9
734 	note_asrs,		/*  8	NT_ASRS			*/
735 #else
736 	note_notsup,		/*  8	NT_ASRS			*/
737 #endif
738 #else
739 	note_notsup,		/*  7	NT_GWINDOWS		*/
740 	note_notsup,		/*  8	NT_ASRS			*/
741 #endif
742 #if defined(__i386) || defined(__amd64)
743 	note_ldt,		/*  9	NT_LDT			*/
744 #else
745 	note_notsup,		/*  9	NT_LDT			*/
746 #endif
747 	note_pstatus,		/* 10	NT_PSTATUS		*/
748 	note_notsup,		/* 11	unassigned		*/
749 	note_notsup,		/* 12	unassigned		*/
750 	note_psinfo,		/* 13	NT_PSINFO		*/
751 	note_cred,		/* 14	NT_PRCRED		*/
752 	note_utsname,		/* 15	NT_UTSNAME		*/
753 	note_lwpstatus,		/* 16	NT_LWPSTATUS		*/
754 	note_lwpsinfo,		/* 17	NT_LWPSINFO		*/
755 	note_priv,		/* 18	NT_PRPRIV		*/
756 	note_priv_info,		/* 19	NT_PRPRIVINFO		*/
757 	note_content,		/* 20	NT_CONTENT		*/
758 	note_zonename,		/* 21	NT_ZONENAME		*/
759 	note_fdinfo,		/* 22	NT_FDINFO		*/
760 	note_spymaster,		/* 23	NT_SPYMASTER		*/
761 };
762 
763 static void
764 core_report_mapping(struct ps_prochandle *P, GElf_Phdr *php)
765 {
766 	prkillinfo_t killinfo;
767 	siginfo_t *si = &killinfo.prk_info;
768 	char signame[SIG2STR_MAX], sig[64], info[64];
769 	void *addr = (void *)(uintptr_t)php->p_vaddr;
770 
771 	const char *errfmt = "core file data for mapping at %p not saved: %s\n";
772 	const char *incfmt = "core file incomplete due to %s%s\n";
773 	const char *msgfmt = "mappings at and above %p are missing\n";
774 
775 	if (!(php->p_flags & PF_SUNW_KILLED)) {
776 		int err = 0;
777 
778 		(void) pread64(P->asfd, &err,
779 		    sizeof (err), (off64_t)php->p_offset);
780 
781 		Perror_printf(P, errfmt, addr, strerror(err));
782 		dprintf(errfmt, addr, strerror(err));
783 		return;
784 	}
785 
786 	if (!(php->p_flags & PF_SUNW_SIGINFO))
787 		return;
788 
789 	(void) memset(&killinfo, 0, sizeof (killinfo));
790 
791 	(void) pread64(P->asfd, &killinfo,
792 	    sizeof (killinfo), (off64_t)php->p_offset);
793 
794 	/*
795 	 * While there is (or at least should be) only one segment that has
796 	 * PF_SUNW_SIGINFO set, the signal information there is globally
797 	 * useful (even if only to those debugging libproc consumers); we hang
798 	 * the signal information gleaned here off of the ps_prochandle.
799 	 */
800 	P->map_missing = php->p_vaddr;
801 	P->killinfo = killinfo.prk_info;
802 
803 	if (sig2str(si->si_signo, signame) == -1) {
804 		(void) snprintf(sig, sizeof (sig),
805 		    "<Unknown signal: 0x%x>, ", si->si_signo);
806 	} else {
807 		(void) snprintf(sig, sizeof (sig), "SIG%s, ", signame);
808 	}
809 
810 	if (si->si_code == SI_USER || si->si_code == SI_QUEUE) {
811 		(void) snprintf(info, sizeof (info),
812 		    "pid=%d uid=%d zone=%d ctid=%d",
813 		    si->si_pid, si->si_uid, si->si_zoneid, si->si_ctid);
814 	} else {
815 		(void) snprintf(info, sizeof (info),
816 		    "code=%d", si->si_code);
817 	}
818 
819 	Perror_printf(P, incfmt, sig, info);
820 	Perror_printf(P, msgfmt, addr);
821 
822 	dprintf(incfmt, sig, info);
823 	dprintf(msgfmt, addr);
824 }
825 
826 /*
827  * Add information on the address space mapping described by the given
828  * PT_LOAD program header.  We fill in more information on the mapping later.
829  */
830 static int
831 core_add_mapping(struct ps_prochandle *P, GElf_Phdr *php)
832 {
833 	prmap_t pmap;
834 
835 	dprintf("mapping base %llx filesz %llu memsz %llu offset %llu\n",
836 	    (u_longlong_t)php->p_vaddr, (u_longlong_t)php->p_filesz,
837 	    (u_longlong_t)php->p_memsz, (u_longlong_t)php->p_offset);
838 
839 	pmap.pr_vaddr = (uintptr_t)php->p_vaddr;
840 	pmap.pr_size = php->p_memsz;
841 
842 	/*
843 	 * If Pgcore() or elfcore() fail to write a mapping, they will set
844 	 * PF_SUNW_FAILURE in the Phdr and try to stash away the errno for us.
845 	 */
846 	if (php->p_flags & PF_SUNW_FAILURE) {
847 		core_report_mapping(P, php);
848 	} else if (php->p_filesz != 0 && php->p_offset >= P->core->core_size) {
849 		Perror_printf(P, "core file may be corrupt -- data for mapping "
850 		    "at %p is missing\n", (void *)(uintptr_t)php->p_vaddr);
851 		dprintf("core file may be corrupt -- data for mapping "
852 		    "at %p is missing\n", (void *)(uintptr_t)php->p_vaddr);
853 	}
854 
855 	/*
856 	 * The mapping name and offset will hopefully be filled in
857 	 * by the librtld_db agent.  Unfortunately, if it isn't a
858 	 * shared library mapping, this information is gone forever.
859 	 */
860 	pmap.pr_mapname[0] = '\0';
861 	pmap.pr_offset = 0;
862 
863 	pmap.pr_mflags = 0;
864 	if (php->p_flags & PF_R)
865 		pmap.pr_mflags |= MA_READ;
866 	if (php->p_flags & PF_W)
867 		pmap.pr_mflags |= MA_WRITE;
868 	if (php->p_flags & PF_X)
869 		pmap.pr_mflags |= MA_EXEC;
870 
871 	if (php->p_filesz == 0)
872 		pmap.pr_mflags |= MA_RESERVED1;
873 
874 	/*
875 	 * At the time of adding this mapping, we just zero the pagesize.
876 	 * Once we've processed more of the core file, we'll have the
877 	 * pagesize from the auxv's AT_PAGESZ element and we can fill this in.
878 	 */
879 	pmap.pr_pagesize = 0;
880 
881 	/*
882 	 * Unfortunately whether or not the mapping was a System V
883 	 * shared memory segment is lost.  We use -1 to mark it as not shm.
884 	 */
885 	pmap.pr_shmid = -1;
886 
887 	return (Padd_mapping(P, php->p_offset, NULL, &pmap));
888 }
889 
890 /*
891  * Given a virtual address, name the mapping at that address using the
892  * specified name, and return the map_info_t pointer.
893  */
894 static map_info_t *
895 core_name_mapping(struct ps_prochandle *P, uintptr_t addr, const char *name)
896 {
897 	map_info_t *mp = Paddr2mptr(P, addr);
898 
899 	if (mp != NULL) {
900 		(void) strncpy(mp->map_pmap.pr_mapname, name, PRMAPSZ);
901 		mp->map_pmap.pr_mapname[PRMAPSZ - 1] = '\0';
902 	}
903 
904 	return (mp);
905 }
906 
907 /*
908  * libproc uses libelf for all of its symbol table manipulation. This function
909  * takes a symbol table and string table from a core file and places them
910  * in a memory backed elf file.
911  */
912 static void
913 fake_up_symtab(struct ps_prochandle *P, const elf_file_header_t *ehdr,
914     GElf_Shdr *symtab, GElf_Shdr *strtab)
915 {
916 	size_t size;
917 	off64_t off, base;
918 	map_info_t *mp;
919 	file_info_t *fp;
920 	Elf_Scn *scn;
921 	Elf_Data *data;
922 
923 	if (symtab->sh_addr == 0 ||
924 	    (mp = Paddr2mptr(P, symtab->sh_addr)) == NULL ||
925 	    (fp = mp->map_file) == NULL) {
926 		dprintf("fake_up_symtab: invalid section\n");
927 		return;
928 	}
929 
930 	if (fp->file_symtab.sym_data_pri != NULL) {
931 		dprintf("Symbol table already loaded (sh_addr 0x%lx)\n",
932 		    (long)symtab->sh_addr);
933 		return;
934 	}
935 
936 	if (P->status.pr_dmodel == PR_MODEL_ILP32) {
937 		struct {
938 			Elf32_Ehdr ehdr;
939 			Elf32_Shdr shdr[3];
940 			char data[1];
941 		} *b;
942 
943 		base = sizeof (b->ehdr) + sizeof (b->shdr);
944 		size = base + symtab->sh_size + strtab->sh_size;
945 
946 		if ((b = calloc(1, size)) == NULL)
947 			return;
948 
949 		(void) memcpy(b->ehdr.e_ident, ehdr->e_ident,
950 		    sizeof (ehdr->e_ident));
951 		b->ehdr.e_type = ehdr->e_type;
952 		b->ehdr.e_machine = ehdr->e_machine;
953 		b->ehdr.e_version = ehdr->e_version;
954 		b->ehdr.e_flags = ehdr->e_flags;
955 		b->ehdr.e_ehsize = sizeof (b->ehdr);
956 		b->ehdr.e_shoff = sizeof (b->ehdr);
957 		b->ehdr.e_shentsize = sizeof (b->shdr[0]);
958 		b->ehdr.e_shnum = 3;
959 		off = 0;
960 
961 		b->shdr[1].sh_size = symtab->sh_size;
962 		b->shdr[1].sh_type = SHT_SYMTAB;
963 		b->shdr[1].sh_offset = off + base;
964 		b->shdr[1].sh_entsize = sizeof (Elf32_Sym);
965 		b->shdr[1].sh_link = 2;
966 		b->shdr[1].sh_info =  symtab->sh_info;
967 		b->shdr[1].sh_addralign = symtab->sh_addralign;
968 
969 		if (pread64(P->asfd, &b->data[off], b->shdr[1].sh_size,
970 		    symtab->sh_offset) != b->shdr[1].sh_size) {
971 			dprintf("fake_up_symtab: pread of symtab[1] failed\n");
972 			free(b);
973 			return;
974 		}
975 
976 		off += b->shdr[1].sh_size;
977 
978 		b->shdr[2].sh_flags = SHF_STRINGS;
979 		b->shdr[2].sh_size = strtab->sh_size;
980 		b->shdr[2].sh_type = SHT_STRTAB;
981 		b->shdr[2].sh_offset = off + base;
982 		b->shdr[2].sh_info =  strtab->sh_info;
983 		b->shdr[2].sh_addralign = 1;
984 
985 		if (pread64(P->asfd, &b->data[off], b->shdr[2].sh_size,
986 		    strtab->sh_offset) != b->shdr[2].sh_size) {
987 			dprintf("fake_up_symtab: pread of symtab[2] failed\n");
988 			free(b);
989 			return;
990 		}
991 
992 		off += b->shdr[2].sh_size;
993 
994 		fp->file_symtab.sym_elf = elf_memory((char *)b, size);
995 		if (fp->file_symtab.sym_elf == NULL) {
996 			free(b);
997 			return;
998 		}
999 
1000 		fp->file_symtab.sym_elfmem = b;
1001 #ifdef _LP64
1002 	} else {
1003 		struct {
1004 			Elf64_Ehdr ehdr;
1005 			Elf64_Shdr shdr[3];
1006 			char data[1];
1007 		} *b;
1008 
1009 		base = sizeof (b->ehdr) + sizeof (b->shdr);
1010 		size = base + symtab->sh_size + strtab->sh_size;
1011 
1012 		if ((b = calloc(1, size)) == NULL)
1013 			return;
1014 
1015 		(void) memcpy(b->ehdr.e_ident, ehdr->e_ident,
1016 		    sizeof (ehdr->e_ident));
1017 		b->ehdr.e_type = ehdr->e_type;
1018 		b->ehdr.e_machine = ehdr->e_machine;
1019 		b->ehdr.e_version = ehdr->e_version;
1020 		b->ehdr.e_flags = ehdr->e_flags;
1021 		b->ehdr.e_ehsize = sizeof (b->ehdr);
1022 		b->ehdr.e_shoff = sizeof (b->ehdr);
1023 		b->ehdr.e_shentsize = sizeof (b->shdr[0]);
1024 		b->ehdr.e_shnum = 3;
1025 		off = 0;
1026 
1027 		b->shdr[1].sh_size = symtab->sh_size;
1028 		b->shdr[1].sh_type = SHT_SYMTAB;
1029 		b->shdr[1].sh_offset = off + base;
1030 		b->shdr[1].sh_entsize = sizeof (Elf64_Sym);
1031 		b->shdr[1].sh_link = 2;
1032 		b->shdr[1].sh_info =  symtab->sh_info;
1033 		b->shdr[1].sh_addralign = symtab->sh_addralign;
1034 
1035 		if (pread64(P->asfd, &b->data[off], b->shdr[1].sh_size,
1036 		    symtab->sh_offset) != b->shdr[1].sh_size) {
1037 			free(b);
1038 			return;
1039 		}
1040 
1041 		off += b->shdr[1].sh_size;
1042 
1043 		b->shdr[2].sh_flags = SHF_STRINGS;
1044 		b->shdr[2].sh_size = strtab->sh_size;
1045 		b->shdr[2].sh_type = SHT_STRTAB;
1046 		b->shdr[2].sh_offset = off + base;
1047 		b->shdr[2].sh_info =  strtab->sh_info;
1048 		b->shdr[2].sh_addralign = 1;
1049 
1050 		if (pread64(P->asfd, &b->data[off], b->shdr[2].sh_size,
1051 		    strtab->sh_offset) != b->shdr[2].sh_size) {
1052 			free(b);
1053 			return;
1054 		}
1055 
1056 		off += b->shdr[2].sh_size;
1057 
1058 		fp->file_symtab.sym_elf = elf_memory((char *)b, size);
1059 		if (fp->file_symtab.sym_elf == NULL) {
1060 			free(b);
1061 			return;
1062 		}
1063 
1064 		fp->file_symtab.sym_elfmem = b;
1065 #endif
1066 	}
1067 
1068 	if ((scn = elf_getscn(fp->file_symtab.sym_elf, 1)) == NULL ||
1069 	    (fp->file_symtab.sym_data_pri = elf_getdata(scn, NULL)) == NULL ||
1070 	    (scn = elf_getscn(fp->file_symtab.sym_elf, 2)) == NULL ||
1071 	    (data = elf_getdata(scn, NULL)) == NULL) {
1072 		dprintf("fake_up_symtab: failed to get section data at %p\n",
1073 		    (void *)scn);
1074 		goto err;
1075 	}
1076 
1077 	fp->file_symtab.sym_strs = data->d_buf;
1078 	fp->file_symtab.sym_strsz = data->d_size;
1079 	fp->file_symtab.sym_symn = symtab->sh_size / symtab->sh_entsize;
1080 	fp->file_symtab.sym_hdr_pri = *symtab;
1081 	fp->file_symtab.sym_strhdr = *strtab;
1082 
1083 	optimize_symtab(&fp->file_symtab);
1084 
1085 	return;
1086 err:
1087 	(void) elf_end(fp->file_symtab.sym_elf);
1088 	free(fp->file_symtab.sym_elfmem);
1089 	fp->file_symtab.sym_elf = NULL;
1090 	fp->file_symtab.sym_elfmem = NULL;
1091 }
1092 
1093 static void
1094 core_phdr_to_gelf(const Elf32_Phdr *src, GElf_Phdr *dst)
1095 {
1096 	dst->p_type = src->p_type;
1097 	dst->p_flags = src->p_flags;
1098 	dst->p_offset = (Elf64_Off)src->p_offset;
1099 	dst->p_vaddr = (Elf64_Addr)src->p_vaddr;
1100 	dst->p_paddr = (Elf64_Addr)src->p_paddr;
1101 	dst->p_filesz = (Elf64_Xword)src->p_filesz;
1102 	dst->p_memsz = (Elf64_Xword)src->p_memsz;
1103 	dst->p_align = (Elf64_Xword)src->p_align;
1104 }
1105 
1106 static void
1107 core_shdr_to_gelf(const Elf32_Shdr *src, GElf_Shdr *dst)
1108 {
1109 	dst->sh_name = src->sh_name;
1110 	dst->sh_type = src->sh_type;
1111 	dst->sh_flags = (Elf64_Xword)src->sh_flags;
1112 	dst->sh_addr = (Elf64_Addr)src->sh_addr;
1113 	dst->sh_offset = (Elf64_Off)src->sh_offset;
1114 	dst->sh_size = (Elf64_Xword)src->sh_size;
1115 	dst->sh_link = src->sh_link;
1116 	dst->sh_info = src->sh_info;
1117 	dst->sh_addralign = (Elf64_Xword)src->sh_addralign;
1118 	dst->sh_entsize = (Elf64_Xword)src->sh_entsize;
1119 }
1120 
1121 /*
1122  * Perform elf_begin on efp->e_fd and verify the ELF file's type and class.
1123  */
1124 static int
1125 core_elf_fdopen(elf_file_t *efp, GElf_Half type, int *perr)
1126 {
1127 #ifdef _BIG_ENDIAN
1128 	uchar_t order = ELFDATA2MSB;
1129 #else
1130 	uchar_t order = ELFDATA2LSB;
1131 #endif
1132 	Elf32_Ehdr e32;
1133 	int is_noelf = -1;
1134 	int isa_err = 0;
1135 
1136 	/*
1137 	 * Because 32-bit libelf cannot deal with large files, we need to read,
1138 	 * check, and convert the file header manually in case type == ET_CORE.
1139 	 */
1140 	if (pread64(efp->e_fd, &e32, sizeof (e32), 0) != sizeof (e32)) {
1141 		if (perr != NULL)
1142 			*perr = G_FORMAT;
1143 		goto err;
1144 	}
1145 	if ((is_noelf = memcmp(&e32.e_ident[EI_MAG0], ELFMAG, SELFMAG)) != 0 ||
1146 	    e32.e_type != type || (isa_err = (e32.e_ident[EI_DATA] != order)) ||
1147 	    e32.e_version != EV_CURRENT) {
1148 		if (perr != NULL) {
1149 			if (is_noelf == 0 && isa_err) {
1150 				*perr = G_ISAINVAL;
1151 			} else {
1152 				*perr = G_FORMAT;
1153 			}
1154 		}
1155 		goto err;
1156 	}
1157 
1158 	/*
1159 	 * If the file is 64-bit and we are 32-bit, fail with G_LP64.  If the
1160 	 * file is 64-bit and we are 64-bit, re-read the header as a Elf64_Ehdr,
1161 	 * and convert it to a elf_file_header_t.  Otherwise, the file is
1162 	 * 32-bit, so convert e32 to a elf_file_header_t.
1163 	 */
1164 	if (e32.e_ident[EI_CLASS] == ELFCLASS64) {
1165 #ifdef _LP64
1166 		Elf64_Ehdr e64;
1167 
1168 		if (pread64(efp->e_fd, &e64, sizeof (e64), 0) != sizeof (e64)) {
1169 			if (perr != NULL)
1170 				*perr = G_FORMAT;
1171 			goto err;
1172 		}
1173 
1174 		(void) memcpy(efp->e_hdr.e_ident, e64.e_ident, EI_NIDENT);
1175 		efp->e_hdr.e_type = e64.e_type;
1176 		efp->e_hdr.e_machine = e64.e_machine;
1177 		efp->e_hdr.e_version = e64.e_version;
1178 		efp->e_hdr.e_entry = e64.e_entry;
1179 		efp->e_hdr.e_phoff = e64.e_phoff;
1180 		efp->e_hdr.e_shoff = e64.e_shoff;
1181 		efp->e_hdr.e_flags = e64.e_flags;
1182 		efp->e_hdr.e_ehsize = e64.e_ehsize;
1183 		efp->e_hdr.e_phentsize = e64.e_phentsize;
1184 		efp->e_hdr.e_phnum = (Elf64_Word)e64.e_phnum;
1185 		efp->e_hdr.e_shentsize = e64.e_shentsize;
1186 		efp->e_hdr.e_shnum = (Elf64_Word)e64.e_shnum;
1187 		efp->e_hdr.e_shstrndx = (Elf64_Word)e64.e_shstrndx;
1188 #else	/* _LP64 */
1189 		if (perr != NULL)
1190 			*perr = G_LP64;
1191 		goto err;
1192 #endif	/* _LP64 */
1193 	} else {
1194 		(void) memcpy(efp->e_hdr.e_ident, e32.e_ident, EI_NIDENT);
1195 		efp->e_hdr.e_type = e32.e_type;
1196 		efp->e_hdr.e_machine = e32.e_machine;
1197 		efp->e_hdr.e_version = e32.e_version;
1198 		efp->e_hdr.e_entry = (Elf64_Addr)e32.e_entry;
1199 		efp->e_hdr.e_phoff = (Elf64_Off)e32.e_phoff;
1200 		efp->e_hdr.e_shoff = (Elf64_Off)e32.e_shoff;
1201 		efp->e_hdr.e_flags = e32.e_flags;
1202 		efp->e_hdr.e_ehsize = e32.e_ehsize;
1203 		efp->e_hdr.e_phentsize = e32.e_phentsize;
1204 		efp->e_hdr.e_phnum = (Elf64_Word)e32.e_phnum;
1205 		efp->e_hdr.e_shentsize = e32.e_shentsize;
1206 		efp->e_hdr.e_shnum = (Elf64_Word)e32.e_shnum;
1207 		efp->e_hdr.e_shstrndx = (Elf64_Word)e32.e_shstrndx;
1208 	}
1209 
1210 	/*
1211 	 * If the number of section headers or program headers or the section
1212 	 * header string table index would overflow their respective fields
1213 	 * in the ELF header, they're stored in the section header at index
1214 	 * zero. To simplify use elsewhere, we look for those sentinel values
1215 	 * here.
1216 	 */
1217 	if ((efp->e_hdr.e_shnum == 0 && efp->e_hdr.e_shoff != 0) ||
1218 	    efp->e_hdr.e_shstrndx == SHN_XINDEX ||
1219 	    efp->e_hdr.e_phnum == PN_XNUM) {
1220 		GElf_Shdr shdr;
1221 
1222 		dprintf("extended ELF header\n");
1223 
1224 		if (efp->e_hdr.e_shoff == 0) {
1225 			if (perr != NULL)
1226 				*perr = G_FORMAT;
1227 			goto err;
1228 		}
1229 
1230 		if (efp->e_hdr.e_ident[EI_CLASS] == ELFCLASS32) {
1231 			Elf32_Shdr shdr32;
1232 
1233 			if (pread64(efp->e_fd, &shdr32, sizeof (shdr32),
1234 			    efp->e_hdr.e_shoff) != sizeof (shdr32)) {
1235 				if (perr != NULL)
1236 					*perr = G_FORMAT;
1237 				goto err;
1238 			}
1239 
1240 			core_shdr_to_gelf(&shdr32, &shdr);
1241 		} else {
1242 			if (pread64(efp->e_fd, &shdr, sizeof (shdr),
1243 			    efp->e_hdr.e_shoff) != sizeof (shdr)) {
1244 				if (perr != NULL)
1245 					*perr = G_FORMAT;
1246 				goto err;
1247 			}
1248 		}
1249 
1250 		if (efp->e_hdr.e_shnum == 0) {
1251 			efp->e_hdr.e_shnum = shdr.sh_size;
1252 			dprintf("section header count %lu\n",
1253 			    (ulong_t)shdr.sh_size);
1254 		}
1255 
1256 		if (efp->e_hdr.e_shstrndx == SHN_XINDEX) {
1257 			efp->e_hdr.e_shstrndx = shdr.sh_link;
1258 			dprintf("section string index %u\n", shdr.sh_link);
1259 		}
1260 
1261 		if (efp->e_hdr.e_phnum == PN_XNUM && shdr.sh_info != 0) {
1262 			efp->e_hdr.e_phnum = shdr.sh_info;
1263 			dprintf("program header count %u\n", shdr.sh_info);
1264 		}
1265 
1266 	} else if (efp->e_hdr.e_phoff != 0) {
1267 		GElf_Phdr phdr;
1268 		uint64_t phnum;
1269 
1270 		/*
1271 		 * It's possible this core file came from a system that
1272 		 * accidentally truncated the e_phnum field without correctly
1273 		 * using the extended format in the section header at index
1274 		 * zero. We try to detect and correct that specific type of
1275 		 * corruption by using the knowledge that the core dump
1276 		 * routines usually place the data referenced by the first
1277 		 * program header immediately after the last header element.
1278 		 */
1279 		if (efp->e_hdr.e_ident[EI_CLASS] == ELFCLASS32) {
1280 			Elf32_Phdr phdr32;
1281 
1282 			if (pread64(efp->e_fd, &phdr32, sizeof (phdr32),
1283 			    efp->e_hdr.e_phoff) != sizeof (phdr32)) {
1284 				if (perr != NULL)
1285 					*perr = G_FORMAT;
1286 				goto err;
1287 			}
1288 
1289 			core_phdr_to_gelf(&phdr32, &phdr);
1290 		} else {
1291 			if (pread64(efp->e_fd, &phdr, sizeof (phdr),
1292 			    efp->e_hdr.e_phoff) != sizeof (phdr)) {
1293 				if (perr != NULL)
1294 					*perr = G_FORMAT;
1295 				goto err;
1296 			}
1297 		}
1298 
1299 		phnum = phdr.p_offset - efp->e_hdr.e_ehsize -
1300 		    (uint64_t)efp->e_hdr.e_shnum * efp->e_hdr.e_shentsize;
1301 		phnum /= efp->e_hdr.e_phentsize;
1302 
1303 		if (phdr.p_offset != 0 && phnum != efp->e_hdr.e_phnum) {
1304 			dprintf("suspicious program header count %u %u\n",
1305 			    (uint_t)phnum, efp->e_hdr.e_phnum);
1306 
1307 			/*
1308 			 * If the new program header count we computed doesn't
1309 			 * jive with count in the ELF header, we'll use the
1310 			 * data that's there and hope for the best.
1311 			 *
1312 			 * If it does, it's also possible that the section
1313 			 * header offset is incorrect; we'll check that and
1314 			 * possibly try to fix it.
1315 			 */
1316 			if (phnum <= INT_MAX &&
1317 			    (uint16_t)phnum == efp->e_hdr.e_phnum) {
1318 
1319 				if (efp->e_hdr.e_shoff == efp->e_hdr.e_phoff +
1320 				    efp->e_hdr.e_phentsize *
1321 				    (uint_t)efp->e_hdr.e_phnum) {
1322 					efp->e_hdr.e_shoff =
1323 					    efp->e_hdr.e_phoff +
1324 					    efp->e_hdr.e_phentsize * phnum;
1325 				}
1326 
1327 				efp->e_hdr.e_phnum = (Elf64_Word)phnum;
1328 				dprintf("using new program header count\n");
1329 			} else {
1330 				dprintf("inconsistent program header count\n");
1331 			}
1332 		}
1333 	}
1334 
1335 	/*
1336 	 * The libelf implementation was never ported to be large-file aware.
1337 	 * This is typically not a problem for your average executable or
1338 	 * shared library, but a large 32-bit core file can exceed 2GB in size.
1339 	 * So if type is ET_CORE, we don't bother doing elf_begin; the code
1340 	 * in Pfgrab_core() below will do its own i/o and struct conversion.
1341 	 */
1342 
1343 	if (type == ET_CORE) {
1344 		efp->e_elf = NULL;
1345 		return (0);
1346 	}
1347 
1348 	if ((efp->e_elf = elf_begin(efp->e_fd, ELF_C_READ, NULL)) == NULL) {
1349 		if (perr != NULL)
1350 			*perr = G_ELF;
1351 		goto err;
1352 	}
1353 
1354 	return (0);
1355 
1356 err:
1357 	efp->e_elf = NULL;
1358 	return (-1);
1359 }
1360 
1361 /*
1362  * Open the specified file and then do a core_elf_fdopen on it.
1363  */
1364 static int
1365 core_elf_open(elf_file_t *efp, const char *path, GElf_Half type, int *perr)
1366 {
1367 	(void) memset(efp, 0, sizeof (elf_file_t));
1368 
1369 	if ((efp->e_fd = open64(path, O_RDONLY)) >= 0) {
1370 		if (core_elf_fdopen(efp, type, perr) == 0)
1371 			return (0);
1372 
1373 		(void) close(efp->e_fd);
1374 		efp->e_fd = -1;
1375 	}
1376 
1377 	return (-1);
1378 }
1379 
1380 /*
1381  * Close the ELF handle and file descriptor.
1382  */
1383 static void
1384 core_elf_close(elf_file_t *efp)
1385 {
1386 	if (efp->e_elf != NULL) {
1387 		(void) elf_end(efp->e_elf);
1388 		efp->e_elf = NULL;
1389 	}
1390 
1391 	if (efp->e_fd != -1) {
1392 		(void) close(efp->e_fd);
1393 		efp->e_fd = -1;
1394 	}
1395 }
1396 
1397 /*
1398  * Given an ELF file for a statically linked executable, locate the likely
1399  * primary text section and fill in rl_base with its virtual address.
1400  */
1401 static map_info_t *
1402 core_find_text(struct ps_prochandle *P, Elf *elf, rd_loadobj_t *rlp)
1403 {
1404 	GElf_Phdr phdr;
1405 	uint_t i;
1406 	size_t nphdrs;
1407 
1408 	if (elf_getphdrnum(elf, &nphdrs) == -1)
1409 		return (NULL);
1410 
1411 	for (i = 0; i < nphdrs; i++) {
1412 		if (gelf_getphdr(elf, i, &phdr) != NULL &&
1413 		    phdr.p_type == PT_LOAD && (phdr.p_flags & PF_X)) {
1414 			rlp->rl_base = phdr.p_vaddr;
1415 			return (Paddr2mptr(P, rlp->rl_base));
1416 		}
1417 	}
1418 
1419 	return (NULL);
1420 }
1421 
1422 /*
1423  * Given an ELF file and the librtld_db structure corresponding to its primary
1424  * text mapping, deduce where its data segment was loaded and fill in
1425  * rl_data_base and prmap_t.pr_offset accordingly.
1426  */
1427 static map_info_t *
1428 core_find_data(struct ps_prochandle *P, Elf *elf, rd_loadobj_t *rlp)
1429 {
1430 	GElf_Ehdr ehdr;
1431 	GElf_Phdr phdr;
1432 	map_info_t *mp;
1433 	uint_t i, pagemask;
1434 	size_t nphdrs;
1435 
1436 	rlp->rl_data_base = NULL;
1437 
1438 	/*
1439 	 * Find the first loadable, writeable Phdr and compute rl_data_base
1440 	 * as the virtual address at which is was loaded.
1441 	 */
1442 	if (gelf_getehdr(elf, &ehdr) == NULL ||
1443 	    elf_getphdrnum(elf, &nphdrs) == -1)
1444 		return (NULL);
1445 
1446 	for (i = 0; i < nphdrs; i++) {
1447 		if (gelf_getphdr(elf, i, &phdr) != NULL &&
1448 		    phdr.p_type == PT_LOAD && (phdr.p_flags & PF_W)) {
1449 			rlp->rl_data_base = phdr.p_vaddr;
1450 			if (ehdr.e_type == ET_DYN)
1451 				rlp->rl_data_base += rlp->rl_base;
1452 			break;
1453 		}
1454 	}
1455 
1456 	/*
1457 	 * If we didn't find an appropriate phdr or if the address we
1458 	 * computed has no mapping, return NULL.
1459 	 */
1460 	if (rlp->rl_data_base == NULL ||
1461 	    (mp = Paddr2mptr(P, rlp->rl_data_base)) == NULL)
1462 		return (NULL);
1463 
1464 	/*
1465 	 * It wouldn't be procfs-related code if we didn't make use of
1466 	 * unclean knowledge of segvn, even in userland ... the prmap_t's
1467 	 * pr_offset field will be the segvn offset from mmap(2)ing the
1468 	 * data section, which will be the file offset & PAGEMASK.
1469 	 */
1470 	pagemask = ~(mp->map_pmap.pr_pagesize - 1);
1471 	mp->map_pmap.pr_offset = phdr.p_offset & pagemask;
1472 
1473 	return (mp);
1474 }
1475 
1476 /*
1477  * Librtld_db agent callback for iterating over load object mappings.
1478  * For each load object, we allocate a new file_info_t, perform naming,
1479  * and attempt to construct a symbol table for the load object.
1480  */
1481 static int
1482 core_iter_mapping(const rd_loadobj_t *rlp, struct ps_prochandle *P)
1483 {
1484 	char lname[PATH_MAX], buf[PATH_MAX];
1485 	file_info_t *fp;
1486 	map_info_t *mp;
1487 
1488 	if (Pread_string(P, lname, PATH_MAX, (off_t)rlp->rl_nameaddr) <= 0) {
1489 		dprintf("failed to read name %p\n", (void *)rlp->rl_nameaddr);
1490 		return (1); /* Keep going; forget this if we can't get a name */
1491 	}
1492 
1493 	dprintf("rd_loadobj name = \"%s\" rl_base = %p\n",
1494 	    lname, (void *)rlp->rl_base);
1495 
1496 	if ((mp = Paddr2mptr(P, rlp->rl_base)) == NULL) {
1497 		dprintf("no mapping for %p\n", (void *)rlp->rl_base);
1498 		return (1); /* No mapping; advance to next mapping */
1499 	}
1500 
1501 	/*
1502 	 * Create a new file_info_t for this mapping, and therefore for
1503 	 * this load object.
1504 	 *
1505 	 * If there's an ELF header at the beginning of this mapping,
1506 	 * file_info_new() will try to use its section headers to
1507 	 * identify any other mappings that belong to this load object.
1508 	 */
1509 	if ((fp = mp->map_file) == NULL &&
1510 	    (fp = file_info_new(P, mp)) == NULL) {
1511 		P->core->core_errno = errno;
1512 		dprintf("failed to malloc mapping data\n");
1513 		return (0); /* Abort */
1514 	}
1515 	fp->file_map = mp;
1516 
1517 	/* Create a local copy of the load object representation */
1518 	if ((fp->file_lo = calloc(1, sizeof (rd_loadobj_t))) == NULL) {
1519 		P->core->core_errno = errno;
1520 		dprintf("failed to malloc mapping data\n");
1521 		return (0); /* Abort */
1522 	}
1523 	*fp->file_lo = *rlp;
1524 
1525 	if (lname[0] != '\0') {
1526 		/*
1527 		 * Naming dance part 1: if we got a name from librtld_db, then
1528 		 * copy this name to the prmap_t if it is unnamed.  If the
1529 		 * file_info_t is unnamed, name it after the lname.
1530 		 */
1531 		if (mp->map_pmap.pr_mapname[0] == '\0') {
1532 			(void) strncpy(mp->map_pmap.pr_mapname, lname, PRMAPSZ);
1533 			mp->map_pmap.pr_mapname[PRMAPSZ - 1] = '\0';
1534 		}
1535 
1536 		if (fp->file_lname == NULL)
1537 			fp->file_lname = strdup(lname);
1538 
1539 	} else if (fp->file_lname == NULL &&
1540 	    mp->map_pmap.pr_mapname[0] != '\0') {
1541 		/*
1542 		 * Naming dance part 2: if the mapping is named and the
1543 		 * file_info_t is not, name the file after the mapping.
1544 		 */
1545 		fp->file_lname = strdup(mp->map_pmap.pr_mapname);
1546 	}
1547 
1548 	if ((fp->file_rname == NULL) &&
1549 	    (Pfindmap(P, mp, buf, sizeof (buf)) != NULL))
1550 		fp->file_rname = strdup(buf);
1551 
1552 	if (fp->file_lname != NULL)
1553 		fp->file_lbase = basename(fp->file_lname);
1554 	if (fp->file_rname != NULL)
1555 		fp->file_rbase = basename(fp->file_rname);
1556 
1557 	/* Associate the file and the mapping. */
1558 	(void) strncpy(fp->file_pname, mp->map_pmap.pr_mapname, PRMAPSZ);
1559 	fp->file_pname[PRMAPSZ - 1] = '\0';
1560 
1561 	/*
1562 	 * If no section headers were available then we'll have to
1563 	 * identify this load object's other mappings with what we've
1564 	 * got: the start and end of the object's corresponding
1565 	 * address space.
1566 	 */
1567 	if (fp->file_saddrs == NULL) {
1568 		for (mp = fp->file_map + 1; mp < P->mappings + P->map_count &&
1569 		    mp->map_pmap.pr_vaddr < rlp->rl_bend; mp++) {
1570 
1571 			if (mp->map_file == NULL) {
1572 				dprintf("core_iter_mapping %s: associating "
1573 				    "segment at %p\n",
1574 				    fp->file_pname,
1575 				    (void *)mp->map_pmap.pr_vaddr);
1576 				mp->map_file = fp;
1577 				fp->file_ref++;
1578 			} else {
1579 				dprintf("core_iter_mapping %s: segment at "
1580 				    "%p already associated with %s\n",
1581 				    fp->file_pname,
1582 				    (void *)mp->map_pmap.pr_vaddr,
1583 				    (mp == fp->file_map ? "this file" :
1584 				    mp->map_file->file_pname));
1585 			}
1586 		}
1587 	}
1588 
1589 	/* Ensure that all this file's mappings are named. */
1590 	for (mp = fp->file_map; mp < P->mappings + P->map_count &&
1591 	    mp->map_file == fp; mp++) {
1592 		if (mp->map_pmap.pr_mapname[0] == '\0' &&
1593 		    !(mp->map_pmap.pr_mflags & MA_BREAK)) {
1594 			(void) strncpy(mp->map_pmap.pr_mapname, fp->file_pname,
1595 			    PRMAPSZ);
1596 			mp->map_pmap.pr_mapname[PRMAPSZ - 1] = '\0';
1597 		}
1598 	}
1599 
1600 	/* Attempt to build a symbol table for this file. */
1601 	Pbuild_file_symtab(P, fp);
1602 	if (fp->file_elf == NULL)
1603 		dprintf("core_iter_mapping: no symtab for %s\n",
1604 		    fp->file_pname);
1605 
1606 	/* Locate the start of a data segment associated with this file. */
1607 	if ((mp = core_find_data(P, fp->file_elf, fp->file_lo)) != NULL) {
1608 		dprintf("found data for %s at %p (pr_offset 0x%llx)\n",
1609 		    fp->file_pname, (void *)fp->file_lo->rl_data_base,
1610 		    mp->map_pmap.pr_offset);
1611 	} else {
1612 		dprintf("core_iter_mapping: no data found for %s\n",
1613 		    fp->file_pname);
1614 	}
1615 
1616 	return (1); /* Advance to next mapping */
1617 }
1618 
1619 /*
1620  * Callback function for Pfindexec().  In order to confirm a given pathname,
1621  * we verify that we can open it as an ELF file of type ET_EXEC or ET_DYN.
1622  */
1623 static int
1624 core_exec_open(const char *path, void *efp)
1625 {
1626 	if (core_elf_open(efp, path, ET_EXEC, NULL) == 0)
1627 		return (1);
1628 	if (core_elf_open(efp, path, ET_DYN, NULL) == 0)
1629 		return (1);
1630 	return (0);
1631 }
1632 
1633 /*
1634  * Attempt to load any section headers found in the core file.  If present,
1635  * this will refer to non-loadable data added to the core file by the kernel
1636  * based on coreadm(1M) settings, including CTF data and the symbol table.
1637  */
1638 static void
1639 core_load_shdrs(struct ps_prochandle *P, elf_file_t *efp)
1640 {
1641 	GElf_Shdr *shp, *shdrs = NULL;
1642 	char *shstrtab = NULL;
1643 	ulong_t shstrtabsz;
1644 	const char *name;
1645 	map_info_t *mp;
1646 
1647 	size_t nbytes;
1648 	void *buf;
1649 	int i;
1650 
1651 	if (efp->e_hdr.e_shstrndx >= efp->e_hdr.e_shnum) {
1652 		dprintf("corrupt shstrndx (%u) exceeds shnum (%u)\n",
1653 		    efp->e_hdr.e_shstrndx, efp->e_hdr.e_shnum);
1654 		return;
1655 	}
1656 
1657 	/*
1658 	 * Read the section header table from the core file and then iterate
1659 	 * over the section headers, converting each to a GElf_Shdr.
1660 	 */
1661 	if ((shdrs = malloc(efp->e_hdr.e_shnum * sizeof (GElf_Shdr))) == NULL) {
1662 		dprintf("failed to malloc %u section headers: %s\n",
1663 		    (uint_t)efp->e_hdr.e_shnum, strerror(errno));
1664 		return;
1665 	}
1666 
1667 	nbytes = efp->e_hdr.e_shnum * efp->e_hdr.e_shentsize;
1668 	if ((buf = malloc(nbytes)) == NULL) {
1669 		dprintf("failed to malloc %d bytes: %s\n", (int)nbytes,
1670 		    strerror(errno));
1671 		free(shdrs);
1672 		goto out;
1673 	}
1674 
1675 	if (pread64(efp->e_fd, buf, nbytes, efp->e_hdr.e_shoff) != nbytes) {
1676 		dprintf("failed to read section headers at off %lld: %s\n",
1677 		    (longlong_t)efp->e_hdr.e_shoff, strerror(errno));
1678 		free(buf);
1679 		goto out;
1680 	}
1681 
1682 	for (i = 0; i < efp->e_hdr.e_shnum; i++) {
1683 		void *p = (uchar_t *)buf + efp->e_hdr.e_shentsize * i;
1684 
1685 		if (efp->e_hdr.e_ident[EI_CLASS] == ELFCLASS32)
1686 			core_shdr_to_gelf(p, &shdrs[i]);
1687 		else
1688 			(void) memcpy(&shdrs[i], p, sizeof (GElf_Shdr));
1689 	}
1690 
1691 	free(buf);
1692 	buf = NULL;
1693 
1694 	/*
1695 	 * Read the .shstrtab section from the core file, terminating it with
1696 	 * an extra \0 so that a corrupt section will not cause us to die.
1697 	 */
1698 	shp = &shdrs[efp->e_hdr.e_shstrndx];
1699 	shstrtabsz = shp->sh_size;
1700 
1701 	if ((shstrtab = malloc(shstrtabsz + 1)) == NULL) {
1702 		dprintf("failed to allocate %lu bytes for shstrtab\n",
1703 		    (ulong_t)shstrtabsz);
1704 		goto out;
1705 	}
1706 
1707 	if (pread64(efp->e_fd, shstrtab, shstrtabsz,
1708 	    shp->sh_offset) != shstrtabsz) {
1709 		dprintf("failed to read %lu bytes of shstrs at off %lld: %s\n",
1710 		    shstrtabsz, (longlong_t)shp->sh_offset, strerror(errno));
1711 		goto out;
1712 	}
1713 
1714 	shstrtab[shstrtabsz] = '\0';
1715 
1716 	/*
1717 	 * Now iterate over each section in the section header table, locating
1718 	 * sections of interest and initializing more of the ps_prochandle.
1719 	 */
1720 	for (i = 0; i < efp->e_hdr.e_shnum; i++) {
1721 		shp = &shdrs[i];
1722 		name = shstrtab + shp->sh_name;
1723 
1724 		if (shp->sh_name >= shstrtabsz) {
1725 			dprintf("skipping section [%d]: corrupt sh_name\n", i);
1726 			continue;
1727 		}
1728 
1729 		if (shp->sh_link >= efp->e_hdr.e_shnum) {
1730 			dprintf("skipping section [%d]: corrupt sh_link\n", i);
1731 			continue;
1732 		}
1733 
1734 		dprintf("found section header %s (sh_addr 0x%llx)\n",
1735 		    name, (u_longlong_t)shp->sh_addr);
1736 
1737 		if (strcmp(name, ".SUNW_ctf") == 0) {
1738 			if ((mp = Paddr2mptr(P, shp->sh_addr)) == NULL) {
1739 				dprintf("no map at addr 0x%llx for %s [%d]\n",
1740 				    (u_longlong_t)shp->sh_addr, name, i);
1741 				continue;
1742 			}
1743 
1744 			if (mp->map_file == NULL ||
1745 			    mp->map_file->file_ctf_buf != NULL) {
1746 				dprintf("no mapping file or duplicate buffer "
1747 				    "for %s [%d]\n", name, i);
1748 				continue;
1749 			}
1750 
1751 			if ((buf = malloc(shp->sh_size)) == NULL ||
1752 			    pread64(efp->e_fd, buf, shp->sh_size,
1753 			    shp->sh_offset) != shp->sh_size) {
1754 				dprintf("skipping section %s [%d]: %s\n",
1755 				    name, i, strerror(errno));
1756 				free(buf);
1757 				continue;
1758 			}
1759 
1760 			mp->map_file->file_ctf_size = shp->sh_size;
1761 			mp->map_file->file_ctf_buf = buf;
1762 
1763 			if (shdrs[shp->sh_link].sh_type == SHT_DYNSYM)
1764 				mp->map_file->file_ctf_dyn = 1;
1765 
1766 		} else if (strcmp(name, ".symtab") == 0) {
1767 			fake_up_symtab(P, &efp->e_hdr,
1768 			    shp, &shdrs[shp->sh_link]);
1769 		}
1770 	}
1771 out:
1772 	free(shstrtab);
1773 	free(shdrs);
1774 }
1775 
1776 /*
1777  * Main engine for core file initialization: given an fd for the core file
1778  * and an optional pathname, construct the ps_prochandle.  The aout_path can
1779  * either be a suggested executable pathname, or a suggested directory to
1780  * use as a possible current working directory.
1781  */
1782 struct ps_prochandle *
1783 Pfgrab_core(int core_fd, const char *aout_path, int *perr)
1784 {
1785 	struct ps_prochandle *P;
1786 	map_info_t *stk_mp, *brk_mp;
1787 	const char *execname;
1788 	char *interp;
1789 	int i, notes, pagesize;
1790 	uintptr_t addr, base_addr;
1791 	struct stat64 stbuf;
1792 	void *phbuf, *php;
1793 	size_t nbytes;
1794 
1795 	elf_file_t aout;
1796 	elf_file_t core;
1797 
1798 	Elf_Scn *scn, *intp_scn = NULL;
1799 	Elf_Data *dp;
1800 
1801 	GElf_Phdr phdr, note_phdr;
1802 	GElf_Shdr shdr;
1803 	GElf_Xword nleft;
1804 
1805 	if (elf_version(EV_CURRENT) == EV_NONE) {
1806 		dprintf("libproc ELF version is more recent than libelf\n");
1807 		*perr = G_ELF;
1808 		return (NULL);
1809 	}
1810 
1811 	aout.e_elf = NULL;
1812 	aout.e_fd = -1;
1813 
1814 	core.e_elf = NULL;
1815 	core.e_fd = core_fd;
1816 
1817 	/*
1818 	 * Allocate and initialize a ps_prochandle structure for the core.
1819 	 * There are several key pieces of initialization here:
1820 	 *
1821 	 * 1. The PS_DEAD state flag marks this prochandle as a core file.
1822 	 *    PS_DEAD also thus prevents all operations which require state
1823 	 *    to be PS_STOP from operating on this handle.
1824 	 *
1825 	 * 2. We keep the core file fd in P->asfd since the core file contains
1826 	 *    the remnants of the process address space.
1827 	 *
1828 	 * 3. We set the P->info_valid bit because all information about the
1829 	 *    core is determined by the end of this function; there is no need
1830 	 *    for proc_update_maps() to reload mappings at any later point.
1831 	 *
1832 	 * 4. The read/write ops vector uses our core_rw() function defined
1833 	 *    above to handle i/o requests.
1834 	 */
1835 	if ((P = malloc(sizeof (struct ps_prochandle))) == NULL) {
1836 		*perr = G_STRANGE;
1837 		return (NULL);
1838 	}
1839 
1840 	(void) memset(P, 0, sizeof (struct ps_prochandle));
1841 	(void) mutex_init(&P->proc_lock, USYNC_THREAD, NULL);
1842 	P->state = PS_DEAD;
1843 	P->pid = (pid_t)-1;
1844 	P->asfd = core.e_fd;
1845 	P->ctlfd = -1;
1846 	P->statfd = -1;
1847 	P->agentctlfd = -1;
1848 	P->agentstatfd = -1;
1849 	P->zoneroot = NULL;
1850 	P->info_valid = 1;
1851 	P->ops = &P_core_ops;
1852 
1853 	Pinitsym(P);
1854 
1855 	/*
1856 	 * Fstat and open the core file and make sure it is a valid ELF core.
1857 	 */
1858 	if (fstat64(P->asfd, &stbuf) == -1) {
1859 		*perr = G_STRANGE;
1860 		goto err;
1861 	}
1862 
1863 	if (core_elf_fdopen(&core, ET_CORE, perr) == -1)
1864 		goto err;
1865 
1866 	/*
1867 	 * Allocate and initialize a core_info_t to hang off the ps_prochandle
1868 	 * structure.  We keep all core-specific information in this structure.
1869 	 */
1870 	if ((P->core = calloc(1, sizeof (core_info_t))) == NULL) {
1871 		*perr = G_STRANGE;
1872 		goto err;
1873 	}
1874 
1875 	list_link(&P->core->core_lwp_head, NULL);
1876 	P->core->core_size = stbuf.st_size;
1877 	/*
1878 	 * In the days before adjustable core file content, this was the
1879 	 * default core file content. For new core files, this value will
1880 	 * be overwritten by the NT_CONTENT note section.
1881 	 */
1882 	P->core->core_content = CC_CONTENT_STACK | CC_CONTENT_HEAP |
1883 	    CC_CONTENT_DATA | CC_CONTENT_RODATA | CC_CONTENT_ANON |
1884 	    CC_CONTENT_SHANON;
1885 
1886 	switch (core.e_hdr.e_ident[EI_CLASS]) {
1887 	case ELFCLASS32:
1888 		P->core->core_dmodel = PR_MODEL_ILP32;
1889 		break;
1890 	case ELFCLASS64:
1891 		P->core->core_dmodel = PR_MODEL_LP64;
1892 		break;
1893 	default:
1894 		*perr = G_FORMAT;
1895 		goto err;
1896 	}
1897 
1898 	/*
1899 	 * Because the core file may be a large file, we can't use libelf to
1900 	 * read the Phdrs.  We use e_phnum and e_phentsize to simplify things.
1901 	 */
1902 	nbytes = core.e_hdr.e_phnum * core.e_hdr.e_phentsize;
1903 
1904 	if ((phbuf = malloc(nbytes)) == NULL) {
1905 		*perr = G_STRANGE;
1906 		goto err;
1907 	}
1908 
1909 	if (pread64(core_fd, phbuf, nbytes, core.e_hdr.e_phoff) != nbytes) {
1910 		*perr = G_STRANGE;
1911 		free(phbuf);
1912 		goto err;
1913 	}
1914 
1915 	/*
1916 	 * Iterate through the program headers in the core file.
1917 	 * We're interested in two types of Phdrs: PT_NOTE (which
1918 	 * contains a set of saved /proc structures), and PT_LOAD (which
1919 	 * represents a memory mapping from the process's address space).
1920 	 * In the case of PT_NOTE, we're interested in the last PT_NOTE
1921 	 * in the core file; currently the first PT_NOTE (if present)
1922 	 * contains /proc structs in the pre-2.6 unstructured /proc format.
1923 	 */
1924 	for (php = phbuf, notes = 0, i = 0; i < core.e_hdr.e_phnum; i++) {
1925 		if (core.e_hdr.e_ident[EI_CLASS] == ELFCLASS64)
1926 			(void) memcpy(&phdr, php, sizeof (GElf_Phdr));
1927 		else
1928 			core_phdr_to_gelf(php, &phdr);
1929 
1930 		switch (phdr.p_type) {
1931 		case PT_NOTE:
1932 			note_phdr = phdr;
1933 			notes++;
1934 			break;
1935 
1936 		case PT_LOAD:
1937 			if (core_add_mapping(P, &phdr) == -1) {
1938 				*perr = G_STRANGE;
1939 				free(phbuf);
1940 				goto err;
1941 			}
1942 			break;
1943 		}
1944 
1945 		php = (char *)php + core.e_hdr.e_phentsize;
1946 	}
1947 
1948 	free(phbuf);
1949 
1950 	Psort_mappings(P);
1951 
1952 	/*
1953 	 * If we couldn't find anything of type PT_NOTE, or only one PT_NOTE
1954 	 * was present, abort.  The core file is either corrupt or too old.
1955 	 */
1956 	if (notes == 0 || notes == 1) {
1957 		*perr = G_NOTE;
1958 		goto err;
1959 	}
1960 
1961 	/*
1962 	 * Advance the seek pointer to the start of the PT_NOTE data
1963 	 */
1964 	if (lseek64(P->asfd, note_phdr.p_offset, SEEK_SET) == (off64_t)-1) {
1965 		dprintf("Pgrab_core: failed to lseek to PT_NOTE data\n");
1966 		*perr = G_STRANGE;
1967 		goto err;
1968 	}
1969 
1970 	/*
1971 	 * Now process the PT_NOTE structures.  Each one is preceded by
1972 	 * an Elf{32/64}_Nhdr structure describing its type and size.
1973 	 *
1974 	 *  +--------+
1975 	 *  | header |
1976 	 *  +--------+
1977 	 *  | name   |
1978 	 *  | ...    |
1979 	 *  +--------+
1980 	 *  | desc   |
1981 	 *  | ...    |
1982 	 *  +--------+
1983 	 */
1984 	for (nleft = note_phdr.p_filesz; nleft > 0; ) {
1985 		Elf64_Nhdr nhdr;
1986 		off64_t off, namesz;
1987 
1988 		/*
1989 		 * Although <sys/elf.h> defines both Elf32_Nhdr and Elf64_Nhdr
1990 		 * as different types, they are both of the same content and
1991 		 * size, so we don't need to worry about 32/64 conversion here.
1992 		 */
1993 		if (read(P->asfd, &nhdr, sizeof (nhdr)) != sizeof (nhdr)) {
1994 			dprintf("Pgrab_core: failed to read ELF note header\n");
1995 			*perr = G_NOTE;
1996 			goto err;
1997 		}
1998 
1999 		/*
2000 		 * According to the System V ABI, the amount of padding
2001 		 * following the name field should align the description
2002 		 * field on a 4 byte boundary for 32-bit binaries or on an 8
2003 		 * byte boundary for 64-bit binaries. However, this change
2004 		 * was not made correctly during the 64-bit port so all
2005 		 * descriptions can assume only 4-byte alignment. We ignore
2006 		 * the name field and the padding to 4-byte alignment.
2007 		 */
2008 		namesz = P2ROUNDUP((off64_t)nhdr.n_namesz, (off64_t)4);
2009 		if (lseek64(P->asfd, namesz, SEEK_CUR) == (off64_t)-1) {
2010 			dprintf("failed to seek past name and padding\n");
2011 			*perr = G_STRANGE;
2012 			goto err;
2013 		}
2014 
2015 		dprintf("Note hdr n_type=%u n_namesz=%u n_descsz=%u\n",
2016 		    nhdr.n_type, nhdr.n_namesz, nhdr.n_descsz);
2017 
2018 		off = lseek64(P->asfd, (off64_t)0L, SEEK_CUR);
2019 
2020 		/*
2021 		 * Invoke the note handler function from our table
2022 		 */
2023 		if (nhdr.n_type < sizeof (nhdlrs) / sizeof (nhdlrs[0])) {
2024 			if (nhdlrs[nhdr.n_type](P, nhdr.n_descsz) < 0) {
2025 				*perr = G_NOTE;
2026 				goto err;
2027 			}
2028 		} else
2029 			(void) note_notsup(P, nhdr.n_descsz);
2030 
2031 		/*
2032 		 * Seek past the current note data to the next Elf_Nhdr
2033 		 */
2034 		if (lseek64(P->asfd, off + nhdr.n_descsz,
2035 		    SEEK_SET) == (off64_t)-1) {
2036 			dprintf("Pgrab_core: failed to seek to next nhdr\n");
2037 			*perr = G_STRANGE;
2038 			goto err;
2039 		}
2040 
2041 		/*
2042 		 * Subtract the size of the header and its data from what
2043 		 * we have left to process.
2044 		 */
2045 		nleft -= sizeof (nhdr) + namesz + nhdr.n_descsz;
2046 	}
2047 
2048 	if (nleft != 0) {
2049 		dprintf("Pgrab_core: note section malformed\n");
2050 		*perr = G_STRANGE;
2051 		goto err;
2052 	}
2053 
2054 	if ((pagesize = Pgetauxval(P, AT_PAGESZ)) == -1) {
2055 		pagesize = getpagesize();
2056 		dprintf("AT_PAGESZ missing; defaulting to %d\n", pagesize);
2057 	}
2058 
2059 	/*
2060 	 * Locate and label the mappings corresponding to the end of the
2061 	 * heap (MA_BREAK) and the base of the stack (MA_STACK).
2062 	 */
2063 	if ((P->status.pr_brkbase != 0 || P->status.pr_brksize != 0) &&
2064 	    (brk_mp = Paddr2mptr(P, P->status.pr_brkbase +
2065 	    P->status.pr_brksize - 1)) != NULL)
2066 		brk_mp->map_pmap.pr_mflags |= MA_BREAK;
2067 	else
2068 		brk_mp = NULL;
2069 
2070 	if ((stk_mp = Paddr2mptr(P, P->status.pr_stkbase)) != NULL)
2071 		stk_mp->map_pmap.pr_mflags |= MA_STACK;
2072 
2073 	/*
2074 	 * At this point, we have enough information to look for the
2075 	 * executable and open it: we have access to the auxv, a psinfo_t,
2076 	 * and the ability to read from mappings provided by the core file.
2077 	 */
2078 	(void) Pfindexec(P, aout_path, core_exec_open, &aout);
2079 	dprintf("P->execname = \"%s\"\n", P->execname ? P->execname : "NULL");
2080 	execname = P->execname ? P->execname : "a.out";
2081 
2082 	/*
2083 	 * Iterate through the sections, looking for the .dynamic and .interp
2084 	 * sections.  If we encounter them, remember their section pointers.
2085 	 */
2086 	for (scn = NULL; (scn = elf_nextscn(aout.e_elf, scn)) != NULL; ) {
2087 		char *sname;
2088 
2089 		if ((gelf_getshdr(scn, &shdr) == NULL) ||
2090 		    (sname = elf_strptr(aout.e_elf, aout.e_hdr.e_shstrndx,
2091 		    (size_t)shdr.sh_name)) == NULL)
2092 			continue;
2093 
2094 		if (strcmp(sname, ".interp") == 0)
2095 			intp_scn = scn;
2096 	}
2097 
2098 	/*
2099 	 * Get the AT_BASE auxv element.  If this is missing (-1), then
2100 	 * we assume this is a statically-linked executable.
2101 	 */
2102 	base_addr = Pgetauxval(P, AT_BASE);
2103 
2104 	/*
2105 	 * In order to get librtld_db initialized, we'll need to identify
2106 	 * and name the mapping corresponding to the run-time linker.  The
2107 	 * AT_BASE auxv element tells us the address where it was mapped,
2108 	 * and the .interp section of the executable tells us its path.
2109 	 * If for some reason that doesn't pan out, just use ld.so.1.
2110 	 */
2111 	if (intp_scn != NULL && (dp = elf_getdata(intp_scn, NULL)) != NULL &&
2112 	    dp->d_size != 0) {
2113 		dprintf(".interp = <%s>\n", (char *)dp->d_buf);
2114 		interp = dp->d_buf;
2115 
2116 	} else if (base_addr != (uintptr_t)-1L) {
2117 		if (P->core->core_dmodel == PR_MODEL_LP64)
2118 			interp = "/usr/lib/64/ld.so.1";
2119 		else
2120 			interp = "/usr/lib/ld.so.1";
2121 
2122 		dprintf(".interp section is missing or could not be read; "
2123 		    "defaulting to %s\n", interp);
2124 	} else
2125 		dprintf("detected statically linked executable\n");
2126 
2127 	/*
2128 	 * If we have an AT_BASE element, name the mapping at that address
2129 	 * using the interpreter pathname.  Name the corresponding data
2130 	 * mapping after the interpreter as well.
2131 	 */
2132 	if (base_addr != (uintptr_t)-1L) {
2133 		elf_file_t intf;
2134 
2135 		P->map_ldso = core_name_mapping(P, base_addr, interp);
2136 
2137 		if (core_elf_open(&intf, interp, ET_DYN, NULL) == 0) {
2138 			rd_loadobj_t rl;
2139 			map_info_t *dmp;
2140 
2141 			rl.rl_base = base_addr;
2142 			dmp = core_find_data(P, intf.e_elf, &rl);
2143 
2144 			if (dmp != NULL) {
2145 				dprintf("renamed data at %p to %s\n",
2146 				    (void *)rl.rl_data_base, interp);
2147 				(void) strncpy(dmp->map_pmap.pr_mapname,
2148 				    interp, PRMAPSZ);
2149 				dmp->map_pmap.pr_mapname[PRMAPSZ - 1] = '\0';
2150 			}
2151 		}
2152 
2153 		core_elf_close(&intf);
2154 	}
2155 
2156 	/*
2157 	 * If we have an AT_ENTRY element, name the mapping at that address
2158 	 * using the special name "a.out" just like /proc does.
2159 	 */
2160 	if ((addr = Pgetauxval(P, AT_ENTRY)) != (uintptr_t)-1L)
2161 		P->map_exec = core_name_mapping(P, addr, "a.out");
2162 
2163 	/*
2164 	 * If we're a statically linked executable, then just locate the
2165 	 * executable's text and data and name them after the executable.
2166 	 */
2167 	if (base_addr == (uintptr_t)-1L) {
2168 		map_info_t *tmp, *dmp;
2169 		file_info_t *fp;
2170 		rd_loadobj_t rl;
2171 
2172 		if ((tmp = core_find_text(P, aout.e_elf, &rl)) != NULL &&
2173 		    (dmp = core_find_data(P, aout.e_elf, &rl)) != NULL) {
2174 			(void) strncpy(tmp->map_pmap.pr_mapname,
2175 			    execname, PRMAPSZ);
2176 			tmp->map_pmap.pr_mapname[PRMAPSZ - 1] = '\0';
2177 			(void) strncpy(dmp->map_pmap.pr_mapname,
2178 			    execname, PRMAPSZ);
2179 			dmp->map_pmap.pr_mapname[PRMAPSZ - 1] = '\0';
2180 		}
2181 
2182 		if ((P->map_exec = tmp) != NULL &&
2183 		    (fp = malloc(sizeof (file_info_t))) != NULL) {
2184 
2185 			(void) memset(fp, 0, sizeof (file_info_t));
2186 
2187 			list_link(fp, &P->file_head);
2188 			tmp->map_file = fp;
2189 			P->num_files++;
2190 
2191 			fp->file_ref = 1;
2192 			fp->file_fd = -1;
2193 
2194 			fp->file_lo = malloc(sizeof (rd_loadobj_t));
2195 			fp->file_lname = strdup(execname);
2196 
2197 			if (fp->file_lo)
2198 				*fp->file_lo = rl;
2199 			if (fp->file_lname)
2200 				fp->file_lbase = basename(fp->file_lname);
2201 			if (fp->file_rname)
2202 				fp->file_rbase = basename(fp->file_rname);
2203 
2204 			(void) strcpy(fp->file_pname,
2205 			    P->mappings[0].map_pmap.pr_mapname);
2206 			fp->file_map = tmp;
2207 
2208 			Pbuild_file_symtab(P, fp);
2209 
2210 			if (dmp != NULL) {
2211 				dmp->map_file = fp;
2212 				fp->file_ref++;
2213 			}
2214 		}
2215 	}
2216 
2217 	core_elf_close(&aout);
2218 
2219 	/*
2220 	 * We now have enough information to initialize librtld_db.
2221 	 * After it warms up, we can iterate through the load object chain
2222 	 * in the core, which will allow us to construct the file info
2223 	 * we need to provide symbol information for the other shared
2224 	 * libraries, and also to fill in the missing mapping names.
2225 	 */
2226 	rd_log(_libproc_debug);
2227 
2228 	if ((P->rap = rd_new(P)) != NULL) {
2229 		(void) rd_loadobj_iter(P->rap, (rl_iter_f *)
2230 		    core_iter_mapping, P);
2231 
2232 		if (P->core->core_errno != 0) {
2233 			errno = P->core->core_errno;
2234 			*perr = G_STRANGE;
2235 			goto err;
2236 		}
2237 	} else
2238 		dprintf("failed to initialize rtld_db agent\n");
2239 
2240 	/*
2241 	 * If there are sections, load them and process the data from any
2242 	 * sections that we can use to annotate the file_info_t's.
2243 	 */
2244 	core_load_shdrs(P, &core);
2245 
2246 	/*
2247 	 * If we previously located a stack or break mapping, and they are
2248 	 * still anonymous, we now assume that they were MAP_ANON mappings.
2249 	 * If brk_mp turns out to now have a name, then the heap is still
2250 	 * sitting at the end of the executable's data+bss mapping: remove
2251 	 * the previous MA_BREAK setting to be consistent with /proc.
2252 	 */
2253 	if (stk_mp != NULL && stk_mp->map_pmap.pr_mapname[0] == '\0')
2254 		stk_mp->map_pmap.pr_mflags |= MA_ANON;
2255 	if (brk_mp != NULL && brk_mp->map_pmap.pr_mapname[0] == '\0')
2256 		brk_mp->map_pmap.pr_mflags |= MA_ANON;
2257 	else if (brk_mp != NULL)
2258 		brk_mp->map_pmap.pr_mflags &= ~MA_BREAK;
2259 
2260 	*perr = 0;
2261 	return (P);
2262 
2263 err:
2264 	Pfree(P);
2265 	core_elf_close(&aout);
2266 	return (NULL);
2267 }
2268 
2269 /*
2270  * Grab a core file using a pathname.  We just open it and call Pfgrab_core().
2271  */
2272 struct ps_prochandle *
2273 Pgrab_core(const char *core, const char *aout, int gflag, int *perr)
2274 {
2275 	int fd, oflag = (gflag & PGRAB_RDONLY) ? O_RDONLY : O_RDWR;
2276 
2277 	if ((fd = open64(core, oflag)) >= 0)
2278 		return (Pfgrab_core(fd, aout, perr));
2279 
2280 	if (errno != ENOENT)
2281 		*perr = G_STRANGE;
2282 	else
2283 		*perr = G_NOCORE;
2284 
2285 	return (NULL);
2286 }
2287