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