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