xref: /illumos-gate/usr/src/lib/libproc/common/Pcore.c (revision 628e3cbed6489fa1db545d8524a06cd6535af456)
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 2008 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 	    fp->file_symtab.sym_data_pri != NULL) {
815 		dprintf("fake_up_symtab: invalid section\n");
816 		return;
817 	}
818 
819 	if (P->status.pr_dmodel == PR_MODEL_ILP32) {
820 		struct {
821 			Elf32_Ehdr ehdr;
822 			Elf32_Shdr shdr[3];
823 			char data[1];
824 		} *b;
825 
826 		base = sizeof (b->ehdr) + sizeof (b->shdr);
827 		size = base + symtab->sh_size + strtab->sh_size;
828 
829 		if ((b = calloc(1, size)) == NULL)
830 			return;
831 
832 		(void) memcpy(b->ehdr.e_ident, ehdr->e_ident,
833 		    sizeof (ehdr->e_ident));
834 		b->ehdr.e_type = ehdr->e_type;
835 		b->ehdr.e_machine = ehdr->e_machine;
836 		b->ehdr.e_version = ehdr->e_version;
837 		b->ehdr.e_flags = ehdr->e_flags;
838 		b->ehdr.e_ehsize = sizeof (b->ehdr);
839 		b->ehdr.e_shoff = sizeof (b->ehdr);
840 		b->ehdr.e_shentsize = sizeof (b->shdr[0]);
841 		b->ehdr.e_shnum = 3;
842 		off = 0;
843 
844 		b->shdr[1].sh_size = symtab->sh_size;
845 		b->shdr[1].sh_type = SHT_SYMTAB;
846 		b->shdr[1].sh_offset = off + base;
847 		b->shdr[1].sh_entsize = sizeof (Elf32_Sym);
848 		b->shdr[1].sh_link = 2;
849 		b->shdr[1].sh_info =  symtab->sh_info;
850 		b->shdr[1].sh_addralign = symtab->sh_addralign;
851 
852 		if (pread64(P->asfd, &b->data[off], b->shdr[1].sh_size,
853 		    symtab->sh_offset) != b->shdr[1].sh_size) {
854 			dprintf("fake_up_symtab: pread of symtab[1] failed\n");
855 			free(b);
856 			return;
857 		}
858 
859 		off += b->shdr[1].sh_size;
860 
861 		b->shdr[2].sh_flags = SHF_STRINGS;
862 		b->shdr[2].sh_size = strtab->sh_size;
863 		b->shdr[2].sh_type = SHT_STRTAB;
864 		b->shdr[2].sh_offset = off + base;
865 		b->shdr[2].sh_info =  strtab->sh_info;
866 		b->shdr[2].sh_addralign = 1;
867 
868 		if (pread64(P->asfd, &b->data[off], b->shdr[2].sh_size,
869 		    strtab->sh_offset) != b->shdr[2].sh_size) {
870 			dprintf("fake_up_symtab: pread of symtab[2] failed\n");
871 			free(b);
872 			return;
873 		}
874 
875 		off += b->shdr[2].sh_size;
876 
877 		fp->file_symtab.sym_elf = elf_memory((char *)b, size);
878 		if (fp->file_symtab.sym_elf == NULL) {
879 			free(b);
880 			return;
881 		}
882 
883 		fp->file_symtab.sym_elfmem = b;
884 #ifdef _LP64
885 	} else {
886 		struct {
887 			Elf64_Ehdr ehdr;
888 			Elf64_Shdr shdr[3];
889 			char data[1];
890 		} *b;
891 
892 		base = sizeof (b->ehdr) + sizeof (b->shdr);
893 		size = base + symtab->sh_size + strtab->sh_size;
894 
895 		if ((b = calloc(1, size)) == NULL)
896 			return;
897 
898 		(void) memcpy(b->ehdr.e_ident, ehdr->e_ident,
899 		    sizeof (ehdr->e_ident));
900 		b->ehdr.e_type = ehdr->e_type;
901 		b->ehdr.e_machine = ehdr->e_machine;
902 		b->ehdr.e_version = ehdr->e_version;
903 		b->ehdr.e_flags = ehdr->e_flags;
904 		b->ehdr.e_ehsize = sizeof (b->ehdr);
905 		b->ehdr.e_shoff = sizeof (b->ehdr);
906 		b->ehdr.e_shentsize = sizeof (b->shdr[0]);
907 		b->ehdr.e_shnum = 3;
908 		off = 0;
909 
910 		b->shdr[1].sh_size = symtab->sh_size;
911 		b->shdr[1].sh_type = SHT_SYMTAB;
912 		b->shdr[1].sh_offset = off + base;
913 		b->shdr[1].sh_entsize = sizeof (Elf64_Sym);
914 		b->shdr[1].sh_link = 2;
915 		b->shdr[1].sh_info =  symtab->sh_info;
916 		b->shdr[1].sh_addralign = symtab->sh_addralign;
917 
918 		if (pread64(P->asfd, &b->data[off], b->shdr[1].sh_size,
919 		    symtab->sh_offset) != b->shdr[1].sh_size) {
920 			free(b);
921 			return;
922 		}
923 
924 		off += b->shdr[1].sh_size;
925 
926 		b->shdr[2].sh_flags = SHF_STRINGS;
927 		b->shdr[2].sh_size = strtab->sh_size;
928 		b->shdr[2].sh_type = SHT_STRTAB;
929 		b->shdr[2].sh_offset = off + base;
930 		b->shdr[2].sh_info =  strtab->sh_info;
931 		b->shdr[2].sh_addralign = 1;
932 
933 		if (pread64(P->asfd, &b->data[off], b->shdr[2].sh_size,
934 		    strtab->sh_offset) != b->shdr[2].sh_size) {
935 			free(b);
936 			return;
937 		}
938 
939 		off += b->shdr[2].sh_size;
940 
941 		fp->file_symtab.sym_elf = elf_memory((char *)b, size);
942 		if (fp->file_symtab.sym_elf == NULL) {
943 			free(b);
944 			return;
945 		}
946 
947 		fp->file_symtab.sym_elfmem = b;
948 #endif
949 	}
950 
951 	if ((scn = elf_getscn(fp->file_symtab.sym_elf, 1)) == NULL ||
952 	    (fp->file_symtab.sym_data_pri = elf_getdata(scn, NULL)) == NULL ||
953 	    (scn = elf_getscn(fp->file_symtab.sym_elf, 2)) == NULL ||
954 	    (data = elf_getdata(scn, NULL)) == NULL) {
955 		dprintf("fake_up_symtab: failed to get section data at %p\n",
956 		    (void *)scn);
957 		goto err;
958 	}
959 
960 	fp->file_symtab.sym_strs = data->d_buf;
961 	fp->file_symtab.sym_strsz = data->d_size;
962 	fp->file_symtab.sym_symn = symtab->sh_size / symtab->sh_entsize;
963 	fp->file_symtab.sym_hdr_pri = *symtab;
964 	fp->file_symtab.sym_strhdr = *strtab;
965 
966 	optimize_symtab(&fp->file_symtab);
967 
968 	return;
969 err:
970 	(void) elf_end(fp->file_symtab.sym_elf);
971 	free(fp->file_symtab.sym_elfmem);
972 	fp->file_symtab.sym_elf = NULL;
973 	fp->file_symtab.sym_elfmem = NULL;
974 }
975 
976 static void
977 core_phdr_to_gelf(const Elf32_Phdr *src, GElf_Phdr *dst)
978 {
979 	dst->p_type = src->p_type;
980 	dst->p_flags = src->p_flags;
981 	dst->p_offset = (Elf64_Off)src->p_offset;
982 	dst->p_vaddr = (Elf64_Addr)src->p_vaddr;
983 	dst->p_paddr = (Elf64_Addr)src->p_paddr;
984 	dst->p_filesz = (Elf64_Xword)src->p_filesz;
985 	dst->p_memsz = (Elf64_Xword)src->p_memsz;
986 	dst->p_align = (Elf64_Xword)src->p_align;
987 }
988 
989 static void
990 core_shdr_to_gelf(const Elf32_Shdr *src, GElf_Shdr *dst)
991 {
992 	dst->sh_name = src->sh_name;
993 	dst->sh_type = src->sh_type;
994 	dst->sh_flags = (Elf64_Xword)src->sh_flags;
995 	dst->sh_addr = (Elf64_Addr)src->sh_addr;
996 	dst->sh_offset = (Elf64_Off)src->sh_offset;
997 	dst->sh_size = (Elf64_Xword)src->sh_size;
998 	dst->sh_link = src->sh_link;
999 	dst->sh_info = src->sh_info;
1000 	dst->sh_addralign = (Elf64_Xword)src->sh_addralign;
1001 	dst->sh_entsize = (Elf64_Xword)src->sh_entsize;
1002 }
1003 
1004 /*
1005  * Perform elf_begin on efp->e_fd and verify the ELF file's type and class.
1006  */
1007 static int
1008 core_elf_fdopen(elf_file_t *efp, GElf_Half type, int *perr)
1009 {
1010 #ifdef _BIG_ENDIAN
1011 	uchar_t order = ELFDATA2MSB;
1012 #else
1013 	uchar_t order = ELFDATA2LSB;
1014 #endif
1015 	Elf32_Ehdr e32;
1016 	int is_noelf = -1;
1017 	int isa_err = 0;
1018 
1019 	/*
1020 	 * Because 32-bit libelf cannot deal with large files, we need to read,
1021 	 * check, and convert the file header manually in case type == ET_CORE.
1022 	 */
1023 	if (pread64(efp->e_fd, &e32, sizeof (e32), 0) != sizeof (e32)) {
1024 		if (perr != NULL)
1025 			*perr = G_FORMAT;
1026 		goto err;
1027 	}
1028 	if ((is_noelf = memcmp(&e32.e_ident[EI_MAG0], ELFMAG, SELFMAG)) != 0 ||
1029 	    e32.e_type != type || (isa_err = (e32.e_ident[EI_DATA] != order)) ||
1030 	    e32.e_version != EV_CURRENT) {
1031 		if (perr != NULL) {
1032 			if (is_noelf == 0 && isa_err) {
1033 				*perr = G_ISAINVAL;
1034 			} else {
1035 				*perr = G_FORMAT;
1036 			}
1037 		}
1038 		goto err;
1039 	}
1040 
1041 	/*
1042 	 * If the file is 64-bit and we are 32-bit, fail with G_LP64.  If the
1043 	 * file is 64-bit and we are 64-bit, re-read the header as a Elf64_Ehdr,
1044 	 * and convert it to a elf_file_header_t.  Otherwise, the file is
1045 	 * 32-bit, so convert e32 to a elf_file_header_t.
1046 	 */
1047 	if (e32.e_ident[EI_CLASS] == ELFCLASS64) {
1048 #ifdef _LP64
1049 		Elf64_Ehdr e64;
1050 
1051 		if (pread64(efp->e_fd, &e64, sizeof (e64), 0) != sizeof (e64)) {
1052 			if (perr != NULL)
1053 				*perr = G_FORMAT;
1054 			goto err;
1055 		}
1056 
1057 		(void) memcpy(efp->e_hdr.e_ident, e64.e_ident, EI_NIDENT);
1058 		efp->e_hdr.e_type = e64.e_type;
1059 		efp->e_hdr.e_machine = e64.e_machine;
1060 		efp->e_hdr.e_version = e64.e_version;
1061 		efp->e_hdr.e_entry = e64.e_entry;
1062 		efp->e_hdr.e_phoff = e64.e_phoff;
1063 		efp->e_hdr.e_shoff = e64.e_shoff;
1064 		efp->e_hdr.e_flags = e64.e_flags;
1065 		efp->e_hdr.e_ehsize = e64.e_ehsize;
1066 		efp->e_hdr.e_phentsize = e64.e_phentsize;
1067 		efp->e_hdr.e_phnum = (Elf64_Word)e64.e_phnum;
1068 		efp->e_hdr.e_shentsize = e64.e_shentsize;
1069 		efp->e_hdr.e_shnum = (Elf64_Word)e64.e_shnum;
1070 		efp->e_hdr.e_shstrndx = (Elf64_Word)e64.e_shstrndx;
1071 #else	/* _LP64 */
1072 		if (perr != NULL)
1073 			*perr = G_LP64;
1074 		goto err;
1075 #endif	/* _LP64 */
1076 	} else {
1077 		(void) memcpy(efp->e_hdr.e_ident, e32.e_ident, EI_NIDENT);
1078 		efp->e_hdr.e_type = e32.e_type;
1079 		efp->e_hdr.e_machine = e32.e_machine;
1080 		efp->e_hdr.e_version = e32.e_version;
1081 		efp->e_hdr.e_entry = (Elf64_Addr)e32.e_entry;
1082 		efp->e_hdr.e_phoff = (Elf64_Off)e32.e_phoff;
1083 		efp->e_hdr.e_shoff = (Elf64_Off)e32.e_shoff;
1084 		efp->e_hdr.e_flags = e32.e_flags;
1085 		efp->e_hdr.e_ehsize = e32.e_ehsize;
1086 		efp->e_hdr.e_phentsize = e32.e_phentsize;
1087 		efp->e_hdr.e_phnum = (Elf64_Word)e32.e_phnum;
1088 		efp->e_hdr.e_shentsize = e32.e_shentsize;
1089 		efp->e_hdr.e_shnum = (Elf64_Word)e32.e_shnum;
1090 		efp->e_hdr.e_shstrndx = (Elf64_Word)e32.e_shstrndx;
1091 	}
1092 
1093 	/*
1094 	 * If the number of section headers or program headers or the section
1095 	 * header string table index would overflow their respective fields
1096 	 * in the ELF header, they're stored in the section header at index
1097 	 * zero. To simplify use elsewhere, we look for those sentinel values
1098 	 * here.
1099 	 */
1100 	if ((efp->e_hdr.e_shnum == 0 && efp->e_hdr.e_shoff != 0) ||
1101 	    efp->e_hdr.e_shstrndx == SHN_XINDEX ||
1102 	    efp->e_hdr.e_phnum == PN_XNUM) {
1103 		GElf_Shdr shdr;
1104 
1105 		dprintf("extended ELF header\n");
1106 
1107 		if (efp->e_hdr.e_shoff == 0) {
1108 			if (perr != NULL)
1109 				*perr = G_FORMAT;
1110 			goto err;
1111 		}
1112 
1113 		if (efp->e_hdr.e_ident[EI_CLASS] == ELFCLASS32) {
1114 			Elf32_Shdr shdr32;
1115 
1116 			if (pread64(efp->e_fd, &shdr32, sizeof (shdr32),
1117 			    efp->e_hdr.e_shoff) != sizeof (shdr32)) {
1118 				if (perr != NULL)
1119 					*perr = G_FORMAT;
1120 				goto err;
1121 			}
1122 
1123 			core_shdr_to_gelf(&shdr32, &shdr);
1124 		} else {
1125 			if (pread64(efp->e_fd, &shdr, sizeof (shdr),
1126 			    efp->e_hdr.e_shoff) != sizeof (shdr)) {
1127 				if (perr != NULL)
1128 					*perr = G_FORMAT;
1129 				goto err;
1130 			}
1131 		}
1132 
1133 		if (efp->e_hdr.e_shnum == 0) {
1134 			efp->e_hdr.e_shnum = shdr.sh_size;
1135 			dprintf("section header count %lu\n",
1136 			    (ulong_t)shdr.sh_size);
1137 		}
1138 
1139 		if (efp->e_hdr.e_shstrndx == SHN_XINDEX) {
1140 			efp->e_hdr.e_shstrndx = shdr.sh_link;
1141 			dprintf("section string index %u\n", shdr.sh_link);
1142 		}
1143 
1144 		if (efp->e_hdr.e_phnum == PN_XNUM && shdr.sh_info != 0) {
1145 			efp->e_hdr.e_phnum = shdr.sh_info;
1146 			dprintf("program header count %u\n", shdr.sh_info);
1147 		}
1148 
1149 	} else if (efp->e_hdr.e_phoff != 0) {
1150 		GElf_Phdr phdr;
1151 		uint64_t phnum;
1152 
1153 		/*
1154 		 * It's possible this core file came from a system that
1155 		 * accidentally truncated the e_phnum field without correctly
1156 		 * using the extended format in the section header at index
1157 		 * zero. We try to detect and correct that specific type of
1158 		 * corruption by using the knowledge that the core dump
1159 		 * routines usually place the data referenced by the first
1160 		 * program header immediately after the last header element.
1161 		 */
1162 		if (efp->e_hdr.e_ident[EI_CLASS] == ELFCLASS32) {
1163 			Elf32_Phdr phdr32;
1164 
1165 			if (pread64(efp->e_fd, &phdr32, sizeof (phdr32),
1166 			    efp->e_hdr.e_phoff) != sizeof (phdr32)) {
1167 				if (perr != NULL)
1168 					*perr = G_FORMAT;
1169 				goto err;
1170 			}
1171 
1172 			core_phdr_to_gelf(&phdr32, &phdr);
1173 		} else {
1174 			if (pread64(efp->e_fd, &phdr, sizeof (phdr),
1175 			    efp->e_hdr.e_phoff) != sizeof (phdr)) {
1176 				if (perr != NULL)
1177 					*perr = G_FORMAT;
1178 				goto err;
1179 			}
1180 		}
1181 
1182 		phnum = phdr.p_offset - efp->e_hdr.e_ehsize -
1183 		    (uint64_t)efp->e_hdr.e_shnum * efp->e_hdr.e_shentsize;
1184 		phnum /= efp->e_hdr.e_phentsize;
1185 
1186 		if (phdr.p_offset != 0 && phnum != efp->e_hdr.e_phnum) {
1187 			dprintf("suspicious program header count %u %u\n",
1188 			    (uint_t)phnum, efp->e_hdr.e_phnum);
1189 
1190 			/*
1191 			 * If the new program header count we computed doesn't
1192 			 * jive with count in the ELF header, we'll use the
1193 			 * data that's there and hope for the best.
1194 			 *
1195 			 * If it does, it's also possible that the section
1196 			 * header offset is incorrect; we'll check that and
1197 			 * possibly try to fix it.
1198 			 */
1199 			if (phnum <= INT_MAX &&
1200 			    (uint16_t)phnum == efp->e_hdr.e_phnum) {
1201 
1202 				if (efp->e_hdr.e_shoff == efp->e_hdr.e_phoff +
1203 				    efp->e_hdr.e_phentsize *
1204 				    (uint_t)efp->e_hdr.e_phnum) {
1205 					efp->e_hdr.e_shoff =
1206 					    efp->e_hdr.e_phoff +
1207 					    efp->e_hdr.e_phentsize * phnum;
1208 				}
1209 
1210 				efp->e_hdr.e_phnum = (Elf64_Word)phnum;
1211 				dprintf("using new program header count\n");
1212 			} else {
1213 				dprintf("inconsistent program header count\n");
1214 			}
1215 		}
1216 	}
1217 
1218 	/*
1219 	 * The libelf implementation was never ported to be large-file aware.
1220 	 * This is typically not a problem for your average executable or
1221 	 * shared library, but a large 32-bit core file can exceed 2GB in size.
1222 	 * So if type is ET_CORE, we don't bother doing elf_begin; the code
1223 	 * in Pfgrab_core() below will do its own i/o and struct conversion.
1224 	 */
1225 
1226 	if (type == ET_CORE) {
1227 		efp->e_elf = NULL;
1228 		return (0);
1229 	}
1230 
1231 	if ((efp->e_elf = elf_begin(efp->e_fd, ELF_C_READ, NULL)) == NULL) {
1232 		if (perr != NULL)
1233 			*perr = G_ELF;
1234 		goto err;
1235 	}
1236 
1237 	return (0);
1238 
1239 err:
1240 	efp->e_elf = NULL;
1241 	return (-1);
1242 }
1243 
1244 /*
1245  * Open the specified file and then do a core_elf_fdopen on it.
1246  */
1247 static int
1248 core_elf_open(elf_file_t *efp, const char *path, GElf_Half type, int *perr)
1249 {
1250 	(void) memset(efp, 0, sizeof (elf_file_t));
1251 
1252 	if ((efp->e_fd = open64(path, O_RDONLY)) >= 0) {
1253 		if (core_elf_fdopen(efp, type, perr) == 0)
1254 			return (0);
1255 
1256 		(void) close(efp->e_fd);
1257 		efp->e_fd = -1;
1258 	}
1259 
1260 	return (-1);
1261 }
1262 
1263 /*
1264  * Close the ELF handle and file descriptor.
1265  */
1266 static void
1267 core_elf_close(elf_file_t *efp)
1268 {
1269 	if (efp->e_elf != NULL) {
1270 		(void) elf_end(efp->e_elf);
1271 		efp->e_elf = NULL;
1272 	}
1273 
1274 	if (efp->e_fd != -1) {
1275 		(void) close(efp->e_fd);
1276 		efp->e_fd = -1;
1277 	}
1278 }
1279 
1280 /*
1281  * Given an ELF file for a statically linked executable, locate the likely
1282  * primary text section and fill in rl_base with its virtual address.
1283  */
1284 static map_info_t *
1285 core_find_text(struct ps_prochandle *P, Elf *elf, rd_loadobj_t *rlp)
1286 {
1287 	GElf_Phdr phdr;
1288 	uint_t i;
1289 	size_t nphdrs;
1290 
1291 	if (elf_getphnum(elf, &nphdrs) == 0)
1292 		return (NULL);
1293 
1294 	for (i = 0; i < nphdrs; i++) {
1295 		if (gelf_getphdr(elf, i, &phdr) != NULL &&
1296 		    phdr.p_type == PT_LOAD && (phdr.p_flags & PF_X)) {
1297 			rlp->rl_base = phdr.p_vaddr;
1298 			return (Paddr2mptr(P, rlp->rl_base));
1299 		}
1300 	}
1301 
1302 	return (NULL);
1303 }
1304 
1305 /*
1306  * Given an ELF file and the librtld_db structure corresponding to its primary
1307  * text mapping, deduce where its data segment was loaded and fill in
1308  * rl_data_base and prmap_t.pr_offset accordingly.
1309  */
1310 static map_info_t *
1311 core_find_data(struct ps_prochandle *P, Elf *elf, rd_loadobj_t *rlp)
1312 {
1313 	GElf_Ehdr ehdr;
1314 	GElf_Phdr phdr;
1315 	map_info_t *mp;
1316 	uint_t i, pagemask;
1317 	size_t nphdrs;
1318 
1319 	rlp->rl_data_base = NULL;
1320 
1321 	/*
1322 	 * Find the first loadable, writeable Phdr and compute rl_data_base
1323 	 * as the virtual address at which is was loaded.
1324 	 */
1325 	if (gelf_getehdr(elf, &ehdr) == NULL ||
1326 	    elf_getphnum(elf, &nphdrs) == 0)
1327 		return (NULL);
1328 
1329 	for (i = 0; i < nphdrs; i++) {
1330 		if (gelf_getphdr(elf, i, &phdr) != NULL &&
1331 		    phdr.p_type == PT_LOAD && (phdr.p_flags & PF_W)) {
1332 			rlp->rl_data_base = phdr.p_vaddr;
1333 			if (ehdr.e_type == ET_DYN)
1334 				rlp->rl_data_base += rlp->rl_base;
1335 			break;
1336 		}
1337 	}
1338 
1339 	/*
1340 	 * If we didn't find an appropriate phdr or if the address we
1341 	 * computed has no mapping, return NULL.
1342 	 */
1343 	if (rlp->rl_data_base == NULL ||
1344 	    (mp = Paddr2mptr(P, rlp->rl_data_base)) == NULL)
1345 		return (NULL);
1346 
1347 	/*
1348 	 * It wouldn't be procfs-related code if we didn't make use of
1349 	 * unclean knowledge of segvn, even in userland ... the prmap_t's
1350 	 * pr_offset field will be the segvn offset from mmap(2)ing the
1351 	 * data section, which will be the file offset & PAGEMASK.
1352 	 */
1353 	pagemask = ~(mp->map_pmap.pr_pagesize - 1);
1354 	mp->map_pmap.pr_offset = phdr.p_offset & pagemask;
1355 
1356 	return (mp);
1357 }
1358 
1359 /*
1360  * Librtld_db agent callback for iterating over load object mappings.
1361  * For each load object, we allocate a new file_info_t, perform naming,
1362  * and attempt to construct a symbol table for the load object.
1363  */
1364 static int
1365 core_iter_mapping(const rd_loadobj_t *rlp, struct ps_prochandle *P)
1366 {
1367 	char lname[PATH_MAX], buf[PATH_MAX];
1368 	file_info_t *fp;
1369 	map_info_t *mp;
1370 
1371 	if (Pread_string(P, lname, PATH_MAX, (off_t)rlp->rl_nameaddr) <= 0) {
1372 		dprintf("failed to read name %p\n", (void *)rlp->rl_nameaddr);
1373 		return (1); /* Keep going; forget this if we can't get a name */
1374 	}
1375 
1376 	dprintf("rd_loadobj name = \"%s\" rl_base = %p\n",
1377 	    lname, (void *)rlp->rl_base);
1378 
1379 	if ((mp = Paddr2mptr(P, rlp->rl_base)) == NULL) {
1380 		dprintf("no mapping for %p\n", (void *)rlp->rl_base);
1381 		return (1); /* No mapping; advance to next mapping */
1382 	}
1383 
1384 	/*
1385 	 * Create a new file_info_t for this mapping, and therefore for
1386 	 * this load object.
1387 	 *
1388 	 * If there's an ELF header at the beginning of this mapping,
1389 	 * file_info_new() will try to use its section headers to
1390 	 * identify any other mappings that belong to this load object.
1391 	 */
1392 	if ((fp = mp->map_file) == NULL &&
1393 	    (fp = file_info_new(P, mp)) == NULL) {
1394 		P->core->core_errno = errno;
1395 		dprintf("failed to malloc mapping data\n");
1396 		return (0); /* Abort */
1397 	}
1398 	fp->file_map = mp;
1399 
1400 	/* Create a local copy of the load object representation */
1401 	if ((fp->file_lo = calloc(1, sizeof (rd_loadobj_t))) == NULL) {
1402 		P->core->core_errno = errno;
1403 		dprintf("failed to malloc mapping data\n");
1404 		return (0); /* Abort */
1405 	}
1406 	*fp->file_lo = *rlp;
1407 
1408 	if (lname[0] != '\0') {
1409 		/*
1410 		 * Naming dance part 1: if we got a name from librtld_db, then
1411 		 * copy this name to the prmap_t if it is unnamed.  If the
1412 		 * file_info_t is unnamed, name it after the lname.
1413 		 */
1414 		if (mp->map_pmap.pr_mapname[0] == '\0') {
1415 			(void) strncpy(mp->map_pmap.pr_mapname, lname, PRMAPSZ);
1416 			mp->map_pmap.pr_mapname[PRMAPSZ - 1] = '\0';
1417 		}
1418 
1419 		if (fp->file_lname == NULL)
1420 			fp->file_lname = strdup(lname);
1421 
1422 	} else if (fp->file_lname == NULL &&
1423 	    mp->map_pmap.pr_mapname[0] != '\0') {
1424 		/*
1425 		 * Naming dance part 2: if the mapping is named and the
1426 		 * file_info_t is not, name the file after the mapping.
1427 		 */
1428 		fp->file_lname = strdup(mp->map_pmap.pr_mapname);
1429 	}
1430 
1431 	if ((fp->file_rname == NULL) &&
1432 	    (Pfindmap(P, mp, buf, sizeof (buf)) != NULL))
1433 		fp->file_rname = strdup(buf);
1434 
1435 	if (fp->file_lname != NULL)
1436 		fp->file_lbase = basename(fp->file_lname);
1437 	if (fp->file_rname != NULL)
1438 		fp->file_rbase = basename(fp->file_rname);
1439 
1440 	/* Associate the file and the mapping. */
1441 	(void) strncpy(fp->file_pname, mp->map_pmap.pr_mapname, PRMAPSZ);
1442 	fp->file_pname[PRMAPSZ - 1] = '\0';
1443 
1444 	/*
1445 	 * If no section headers were available then we'll have to
1446 	 * identify this load object's other mappings with what we've
1447 	 * got: the start and end of the object's corresponding
1448 	 * address space.
1449 	 */
1450 	if (fp->file_saddrs == NULL) {
1451 		for (mp = fp->file_map + 1; mp < P->mappings + P->map_count &&
1452 		    mp->map_pmap.pr_vaddr < rlp->rl_bend; mp++) {
1453 
1454 			if (mp->map_file == NULL) {
1455 				dprintf("core_iter_mapping %s: associating "
1456 				    "segment at %p\n",
1457 				    fp->file_pname,
1458 				    (void *)mp->map_pmap.pr_vaddr);
1459 				mp->map_file = fp;
1460 				fp->file_ref++;
1461 			} else {
1462 				dprintf("core_iter_mapping %s: segment at "
1463 				    "%p already associated with %s\n",
1464 				    fp->file_pname,
1465 				    (void *)mp->map_pmap.pr_vaddr,
1466 				    (mp == fp->file_map ? "this file" :
1467 				    mp->map_file->file_pname));
1468 			}
1469 		}
1470 	}
1471 
1472 	/* Ensure that all this file's mappings are named. */
1473 	for (mp = fp->file_map; mp < P->mappings + P->map_count &&
1474 	    mp->map_file == fp; mp++) {
1475 		if (mp->map_pmap.pr_mapname[0] == '\0' &&
1476 		    !(mp->map_pmap.pr_mflags & MA_BREAK)) {
1477 			(void) strncpy(mp->map_pmap.pr_mapname, fp->file_pname,
1478 			    PRMAPSZ);
1479 			mp->map_pmap.pr_mapname[PRMAPSZ - 1] = '\0';
1480 		}
1481 	}
1482 
1483 	/* Attempt to build a symbol table for this file. */
1484 	Pbuild_file_symtab(P, fp);
1485 	if (fp->file_elf == NULL)
1486 		dprintf("core_iter_mapping: no symtab for %s\n",
1487 		    fp->file_pname);
1488 
1489 	/* Locate the start of a data segment associated with this file. */
1490 	if ((mp = core_find_data(P, fp->file_elf, fp->file_lo)) != NULL) {
1491 		dprintf("found data for %s at %p (pr_offset 0x%llx)\n",
1492 		    fp->file_pname, (void *)fp->file_lo->rl_data_base,
1493 		    mp->map_pmap.pr_offset);
1494 	} else {
1495 		dprintf("core_iter_mapping: no data found for %s\n",
1496 		    fp->file_pname);
1497 	}
1498 
1499 	return (1); /* Advance to next mapping */
1500 }
1501 
1502 /*
1503  * Callback function for Pfindexec().  In order to confirm a given pathname,
1504  * we verify that we can open it as an ELF file of type ET_EXEC or ET_DYN.
1505  */
1506 static int
1507 core_exec_open(const char *path, void *efp)
1508 {
1509 	if (core_elf_open(efp, path, ET_EXEC, NULL) == 0)
1510 		return (1);
1511 	if (core_elf_open(efp, path, ET_DYN, NULL) == 0)
1512 		return (1);
1513 	return (0);
1514 }
1515 
1516 /*
1517  * Attempt to load any section headers found in the core file.  If present,
1518  * this will refer to non-loadable data added to the core file by the kernel
1519  * based on coreadm(1M) settings, including CTF data and the symbol table.
1520  */
1521 static void
1522 core_load_shdrs(struct ps_prochandle *P, elf_file_t *efp)
1523 {
1524 	GElf_Shdr *shp, *shdrs = NULL;
1525 	char *shstrtab = NULL;
1526 	ulong_t shstrtabsz;
1527 	const char *name;
1528 	map_info_t *mp;
1529 
1530 	size_t nbytes;
1531 	void *buf;
1532 	int i;
1533 
1534 	if (efp->e_hdr.e_shstrndx >= efp->e_hdr.e_shnum) {
1535 		dprintf("corrupt shstrndx (%u) exceeds shnum (%u)\n",
1536 		    efp->e_hdr.e_shstrndx, efp->e_hdr.e_shnum);
1537 		return;
1538 	}
1539 
1540 	/*
1541 	 * Read the section header table from the core file and then iterate
1542 	 * over the section headers, converting each to a GElf_Shdr.
1543 	 */
1544 	if ((shdrs = malloc(efp->e_hdr.e_shnum * sizeof (GElf_Shdr))) == NULL) {
1545 		dprintf("failed to malloc %u section headers: %s\n",
1546 		    (uint_t)efp->e_hdr.e_shnum, strerror(errno));
1547 		return;
1548 	}
1549 
1550 	nbytes = efp->e_hdr.e_shnum * efp->e_hdr.e_shentsize;
1551 	if ((buf = malloc(nbytes)) == NULL) {
1552 		dprintf("failed to malloc %d bytes: %s\n", (int)nbytes,
1553 		    strerror(errno));
1554 		free(shdrs);
1555 		goto out;
1556 	}
1557 
1558 	if (pread64(efp->e_fd, buf, nbytes, efp->e_hdr.e_shoff) != nbytes) {
1559 		dprintf("failed to read section headers at off %lld: %s\n",
1560 		    (longlong_t)efp->e_hdr.e_shoff, strerror(errno));
1561 		free(buf);
1562 		goto out;
1563 	}
1564 
1565 	for (i = 0; i < efp->e_hdr.e_shnum; i++) {
1566 		void *p = (uchar_t *)buf + efp->e_hdr.e_shentsize * i;
1567 
1568 		if (efp->e_hdr.e_ident[EI_CLASS] == ELFCLASS32)
1569 			core_shdr_to_gelf(p, &shdrs[i]);
1570 		else
1571 			(void) memcpy(&shdrs[i], p, sizeof (GElf_Shdr));
1572 	}
1573 
1574 	free(buf);
1575 	buf = NULL;
1576 
1577 	/*
1578 	 * Read the .shstrtab section from the core file, terminating it with
1579 	 * an extra \0 so that a corrupt section will not cause us to die.
1580 	 */
1581 	shp = &shdrs[efp->e_hdr.e_shstrndx];
1582 	shstrtabsz = shp->sh_size;
1583 
1584 	if ((shstrtab = malloc(shstrtabsz + 1)) == NULL) {
1585 		dprintf("failed to allocate %lu bytes for shstrtab\n",
1586 		    (ulong_t)shstrtabsz);
1587 		goto out;
1588 	}
1589 
1590 	if (pread64(efp->e_fd, shstrtab, shstrtabsz,
1591 	    shp->sh_offset) != shstrtabsz) {
1592 		dprintf("failed to read %lu bytes of shstrs at off %lld: %s\n",
1593 		    shstrtabsz, (longlong_t)shp->sh_offset, strerror(errno));
1594 		goto out;
1595 	}
1596 
1597 	shstrtab[shstrtabsz] = '\0';
1598 
1599 	/*
1600 	 * Now iterate over each section in the section header table, locating
1601 	 * sections of interest and initializing more of the ps_prochandle.
1602 	 */
1603 	for (i = 0; i < efp->e_hdr.e_shnum; i++) {
1604 		shp = &shdrs[i];
1605 		name = shstrtab + shp->sh_name;
1606 
1607 		if (shp->sh_name >= shstrtabsz) {
1608 			dprintf("skipping section [%d]: corrupt sh_name\n", i);
1609 			continue;
1610 		}
1611 
1612 		if (shp->sh_link >= efp->e_hdr.e_shnum) {
1613 			dprintf("skipping section [%d]: corrupt sh_link\n", i);
1614 			continue;
1615 		}
1616 
1617 		dprintf("found section header %s (sh_addr 0x%llx)\n",
1618 		    name, (u_longlong_t)shp->sh_addr);
1619 
1620 		if (strcmp(name, ".SUNW_ctf") == 0) {
1621 			if ((mp = Paddr2mptr(P, shp->sh_addr)) == NULL) {
1622 				dprintf("no map at addr 0x%llx for %s [%d]\n",
1623 				    (u_longlong_t)shp->sh_addr, name, i);
1624 				continue;
1625 			}
1626 
1627 			if (mp->map_file == NULL ||
1628 			    mp->map_file->file_ctf_buf != NULL) {
1629 				dprintf("no mapping file or duplicate buffer "
1630 				    "for %s [%d]\n", name, i);
1631 				continue;
1632 			}
1633 
1634 			if ((buf = malloc(shp->sh_size)) == NULL ||
1635 			    pread64(efp->e_fd, buf, shp->sh_size,
1636 			    shp->sh_offset) != shp->sh_size) {
1637 				dprintf("skipping section %s [%d]: %s\n",
1638 				    name, i, strerror(errno));
1639 				free(buf);
1640 				continue;
1641 			}
1642 
1643 			mp->map_file->file_ctf_size = shp->sh_size;
1644 			mp->map_file->file_ctf_buf = buf;
1645 
1646 			if (shdrs[shp->sh_link].sh_type == SHT_DYNSYM)
1647 				mp->map_file->file_ctf_dyn = 1;
1648 
1649 		} else if (strcmp(name, ".symtab") == 0) {
1650 			fake_up_symtab(P, &efp->e_hdr,
1651 			    shp, &shdrs[shp->sh_link]);
1652 		}
1653 	}
1654 out:
1655 	free(shstrtab);
1656 	free(shdrs);
1657 }
1658 
1659 /*
1660  * Main engine for core file initialization: given an fd for the core file
1661  * and an optional pathname, construct the ps_prochandle.  The aout_path can
1662  * either be a suggested executable pathname, or a suggested directory to
1663  * use as a possible current working directory.
1664  */
1665 struct ps_prochandle *
1666 Pfgrab_core(int core_fd, const char *aout_path, int *perr)
1667 {
1668 	struct ps_prochandle *P;
1669 	map_info_t *stk_mp, *brk_mp;
1670 	const char *execname;
1671 	char *interp;
1672 	int i, notes, pagesize;
1673 	uintptr_t addr, base_addr;
1674 	struct stat64 stbuf;
1675 	void *phbuf, *php;
1676 	size_t nbytes;
1677 
1678 	elf_file_t aout;
1679 	elf_file_t core;
1680 
1681 	Elf_Scn *scn, *intp_scn = NULL;
1682 	Elf_Data *dp;
1683 
1684 	GElf_Phdr phdr, note_phdr;
1685 	GElf_Shdr shdr;
1686 	GElf_Xword nleft;
1687 
1688 	if (elf_version(EV_CURRENT) == EV_NONE) {
1689 		dprintf("libproc ELF version is more recent than libelf\n");
1690 		*perr = G_ELF;
1691 		return (NULL);
1692 	}
1693 
1694 	aout.e_elf = NULL;
1695 	aout.e_fd = -1;
1696 
1697 	core.e_elf = NULL;
1698 	core.e_fd = core_fd;
1699 
1700 	/*
1701 	 * Allocate and initialize a ps_prochandle structure for the core.
1702 	 * There are several key pieces of initialization here:
1703 	 *
1704 	 * 1. The PS_DEAD state flag marks this prochandle as a core file.
1705 	 *    PS_DEAD also thus prevents all operations which require state
1706 	 *    to be PS_STOP from operating on this handle.
1707 	 *
1708 	 * 2. We keep the core file fd in P->asfd since the core file contains
1709 	 *    the remnants of the process address space.
1710 	 *
1711 	 * 3. We set the P->info_valid bit because all information about the
1712 	 *    core is determined by the end of this function; there is no need
1713 	 *    for proc_update_maps() to reload mappings at any later point.
1714 	 *
1715 	 * 4. The read/write ops vector uses our core_rw() function defined
1716 	 *    above to handle i/o requests.
1717 	 */
1718 	if ((P = malloc(sizeof (struct ps_prochandle))) == NULL) {
1719 		*perr = G_STRANGE;
1720 		return (NULL);
1721 	}
1722 
1723 	(void) memset(P, 0, sizeof (struct ps_prochandle));
1724 	(void) mutex_init(&P->proc_lock, USYNC_THREAD, NULL);
1725 	P->state = PS_DEAD;
1726 	P->pid = (pid_t)-1;
1727 	P->asfd = core.e_fd;
1728 	P->ctlfd = -1;
1729 	P->statfd = -1;
1730 	P->agentctlfd = -1;
1731 	P->agentstatfd = -1;
1732 	P->zoneroot = NULL;
1733 	P->info_valid = 1;
1734 	P->ops = &P_core_ops;
1735 
1736 	Pinitsym(P);
1737 
1738 	/*
1739 	 * Fstat and open the core file and make sure it is a valid ELF core.
1740 	 */
1741 	if (fstat64(P->asfd, &stbuf) == -1) {
1742 		*perr = G_STRANGE;
1743 		goto err;
1744 	}
1745 
1746 	if (core_elf_fdopen(&core, ET_CORE, perr) == -1)
1747 		goto err;
1748 
1749 	/*
1750 	 * Allocate and initialize a core_info_t to hang off the ps_prochandle
1751 	 * structure.  We keep all core-specific information in this structure.
1752 	 */
1753 	if ((P->core = calloc(1, sizeof (core_info_t))) == NULL) {
1754 		*perr = G_STRANGE;
1755 		goto err;
1756 	}
1757 
1758 	list_link(&P->core->core_lwp_head, NULL);
1759 	P->core->core_size = stbuf.st_size;
1760 	/*
1761 	 * In the days before adjustable core file content, this was the
1762 	 * default core file content. For new core files, this value will
1763 	 * be overwritten by the NT_CONTENT note section.
1764 	 */
1765 	P->core->core_content = CC_CONTENT_STACK | CC_CONTENT_HEAP |
1766 	    CC_CONTENT_DATA | CC_CONTENT_RODATA | CC_CONTENT_ANON |
1767 	    CC_CONTENT_SHANON;
1768 
1769 	switch (core.e_hdr.e_ident[EI_CLASS]) {
1770 	case ELFCLASS32:
1771 		P->core->core_dmodel = PR_MODEL_ILP32;
1772 		break;
1773 	case ELFCLASS64:
1774 		P->core->core_dmodel = PR_MODEL_LP64;
1775 		break;
1776 	default:
1777 		*perr = G_FORMAT;
1778 		goto err;
1779 	}
1780 
1781 	/*
1782 	 * Because the core file may be a large file, we can't use libelf to
1783 	 * read the Phdrs.  We use e_phnum and e_phentsize to simplify things.
1784 	 */
1785 	nbytes = core.e_hdr.e_phnum * core.e_hdr.e_phentsize;
1786 
1787 	if ((phbuf = malloc(nbytes)) == NULL) {
1788 		*perr = G_STRANGE;
1789 		goto err;
1790 	}
1791 
1792 	if (pread64(core_fd, phbuf, nbytes, core.e_hdr.e_phoff) != nbytes) {
1793 		*perr = G_STRANGE;
1794 		free(phbuf);
1795 		goto err;
1796 	}
1797 
1798 	/*
1799 	 * Iterate through the program headers in the core file.
1800 	 * We're interested in two types of Phdrs: PT_NOTE (which
1801 	 * contains a set of saved /proc structures), and PT_LOAD (which
1802 	 * represents a memory mapping from the process's address space).
1803 	 * In the case of PT_NOTE, we're interested in the last PT_NOTE
1804 	 * in the core file; currently the first PT_NOTE (if present)
1805 	 * contains /proc structs in the pre-2.6 unstructured /proc format.
1806 	 */
1807 	for (php = phbuf, notes = 0, i = 0; i < core.e_hdr.e_phnum; i++) {
1808 		if (core.e_hdr.e_ident[EI_CLASS] == ELFCLASS64)
1809 			(void) memcpy(&phdr, php, sizeof (GElf_Phdr));
1810 		else
1811 			core_phdr_to_gelf(php, &phdr);
1812 
1813 		switch (phdr.p_type) {
1814 		case PT_NOTE:
1815 			note_phdr = phdr;
1816 			notes++;
1817 			break;
1818 
1819 		case PT_LOAD:
1820 			if (core_add_mapping(P, &phdr) == -1) {
1821 				*perr = G_STRANGE;
1822 				free(phbuf);
1823 				goto err;
1824 			}
1825 			break;
1826 		}
1827 
1828 		php = (char *)php + core.e_hdr.e_phentsize;
1829 	}
1830 
1831 	free(phbuf);
1832 
1833 	Psort_mappings(P);
1834 
1835 	/*
1836 	 * If we couldn't find anything of type PT_NOTE, or only one PT_NOTE
1837 	 * was present, abort.  The core file is either corrupt or too old.
1838 	 */
1839 	if (notes == 0 || notes == 1) {
1840 		*perr = G_NOTE;
1841 		goto err;
1842 	}
1843 
1844 	/*
1845 	 * Advance the seek pointer to the start of the PT_NOTE data
1846 	 */
1847 	if (lseek64(P->asfd, note_phdr.p_offset, SEEK_SET) == (off64_t)-1) {
1848 		dprintf("Pgrab_core: failed to lseek to PT_NOTE data\n");
1849 		*perr = G_STRANGE;
1850 		goto err;
1851 	}
1852 
1853 	/*
1854 	 * Now process the PT_NOTE structures.  Each one is preceded by
1855 	 * an Elf{32/64}_Nhdr structure describing its type and size.
1856 	 *
1857 	 *  +--------+
1858 	 *  | header |
1859 	 *  +--------+
1860 	 *  | name   |
1861 	 *  | ...    |
1862 	 *  +--------+
1863 	 *  | desc   |
1864 	 *  | ...    |
1865 	 *  +--------+
1866 	 */
1867 	for (nleft = note_phdr.p_filesz; nleft > 0; ) {
1868 		Elf64_Nhdr nhdr;
1869 		off64_t off, namesz;
1870 
1871 		/*
1872 		 * Although <sys/elf.h> defines both Elf32_Nhdr and Elf64_Nhdr
1873 		 * as different types, they are both of the same content and
1874 		 * size, so we don't need to worry about 32/64 conversion here.
1875 		 */
1876 		if (read(P->asfd, &nhdr, sizeof (nhdr)) != sizeof (nhdr)) {
1877 			dprintf("Pgrab_core: failed to read ELF note header\n");
1878 			*perr = G_NOTE;
1879 			goto err;
1880 		}
1881 
1882 		/*
1883 		 * According to the System V ABI, the amount of padding
1884 		 * following the name field should align the description
1885 		 * field on a 4 byte boundary for 32-bit binaries or on an 8
1886 		 * byte boundary for 64-bit binaries. However, this change
1887 		 * was not made correctly during the 64-bit port so all
1888 		 * descriptions can assume only 4-byte alignment. We ignore
1889 		 * the name field and the padding to 4-byte alignment.
1890 		 */
1891 		namesz = P2ROUNDUP((off64_t)nhdr.n_namesz, (off64_t)4);
1892 		if (lseek64(P->asfd, namesz, SEEK_CUR) == (off64_t)-1) {
1893 			dprintf("failed to seek past name and padding\n");
1894 			*perr = G_STRANGE;
1895 			goto err;
1896 		}
1897 
1898 		dprintf("Note hdr n_type=%u n_namesz=%u n_descsz=%u\n",
1899 		    nhdr.n_type, nhdr.n_namesz, nhdr.n_descsz);
1900 
1901 		off = lseek64(P->asfd, (off64_t)0L, SEEK_CUR);
1902 
1903 		/*
1904 		 * Invoke the note handler function from our table
1905 		 */
1906 		if (nhdr.n_type < sizeof (nhdlrs) / sizeof (nhdlrs[0])) {
1907 			if (nhdlrs[nhdr.n_type](P, nhdr.n_descsz) < 0) {
1908 				*perr = G_NOTE;
1909 				goto err;
1910 			}
1911 		} else
1912 			(void) note_notsup(P, nhdr.n_descsz);
1913 
1914 		/*
1915 		 * Seek past the current note data to the next Elf_Nhdr
1916 		 */
1917 		if (lseek64(P->asfd, off + nhdr.n_descsz,
1918 		    SEEK_SET) == (off64_t)-1) {
1919 			dprintf("Pgrab_core: failed to seek to next nhdr\n");
1920 			*perr = G_STRANGE;
1921 			goto err;
1922 		}
1923 
1924 		/*
1925 		 * Subtract the size of the header and its data from what
1926 		 * we have left to process.
1927 		 */
1928 		nleft -= sizeof (nhdr) + namesz + nhdr.n_descsz;
1929 	}
1930 
1931 	if (nleft != 0) {
1932 		dprintf("Pgrab_core: note section malformed\n");
1933 		*perr = G_STRANGE;
1934 		goto err;
1935 	}
1936 
1937 	if ((pagesize = Pgetauxval(P, AT_PAGESZ)) == -1) {
1938 		pagesize = getpagesize();
1939 		dprintf("AT_PAGESZ missing; defaulting to %d\n", pagesize);
1940 	}
1941 
1942 	/*
1943 	 * Locate and label the mappings corresponding to the end of the
1944 	 * heap (MA_BREAK) and the base of the stack (MA_STACK).
1945 	 */
1946 	if ((P->status.pr_brkbase != 0 || P->status.pr_brksize != 0) &&
1947 	    (brk_mp = Paddr2mptr(P, P->status.pr_brkbase +
1948 	    P->status.pr_brksize - 1)) != NULL)
1949 		brk_mp->map_pmap.pr_mflags |= MA_BREAK;
1950 	else
1951 		brk_mp = NULL;
1952 
1953 	if ((stk_mp = Paddr2mptr(P, P->status.pr_stkbase)) != NULL)
1954 		stk_mp->map_pmap.pr_mflags |= MA_STACK;
1955 
1956 	/*
1957 	 * At this point, we have enough information to look for the
1958 	 * executable and open it: we have access to the auxv, a psinfo_t,
1959 	 * and the ability to read from mappings provided by the core file.
1960 	 */
1961 	(void) Pfindexec(P, aout_path, core_exec_open, &aout);
1962 	dprintf("P->execname = \"%s\"\n", P->execname ? P->execname : "NULL");
1963 	execname = P->execname ? P->execname : "a.out";
1964 
1965 	/*
1966 	 * Iterate through the sections, looking for the .dynamic and .interp
1967 	 * sections.  If we encounter them, remember their section pointers.
1968 	 */
1969 	for (scn = NULL; (scn = elf_nextscn(aout.e_elf, scn)) != NULL; ) {
1970 		char *sname;
1971 
1972 		if ((gelf_getshdr(scn, &shdr) == NULL) ||
1973 		    (sname = elf_strptr(aout.e_elf, aout.e_hdr.e_shstrndx,
1974 		    (size_t)shdr.sh_name)) == NULL)
1975 			continue;
1976 
1977 		if (strcmp(sname, ".interp") == 0)
1978 			intp_scn = scn;
1979 	}
1980 
1981 	/*
1982 	 * Get the AT_BASE auxv element.  If this is missing (-1), then
1983 	 * we assume this is a statically-linked executable.
1984 	 */
1985 	base_addr = Pgetauxval(P, AT_BASE);
1986 
1987 	/*
1988 	 * In order to get librtld_db initialized, we'll need to identify
1989 	 * and name the mapping corresponding to the run-time linker.  The
1990 	 * AT_BASE auxv element tells us the address where it was mapped,
1991 	 * and the .interp section of the executable tells us its path.
1992 	 * If for some reason that doesn't pan out, just use ld.so.1.
1993 	 */
1994 	if (intp_scn != NULL && (dp = elf_getdata(intp_scn, NULL)) != NULL &&
1995 	    dp->d_size != 0) {
1996 		dprintf(".interp = <%s>\n", (char *)dp->d_buf);
1997 		interp = dp->d_buf;
1998 
1999 	} else if (base_addr != (uintptr_t)-1L) {
2000 		if (P->core->core_dmodel == PR_MODEL_LP64)
2001 			interp = "/usr/lib/64/ld.so.1";
2002 		else
2003 			interp = "/usr/lib/ld.so.1";
2004 
2005 		dprintf(".interp section is missing or could not be read; "
2006 		    "defaulting to %s\n", interp);
2007 	} else
2008 		dprintf("detected statically linked executable\n");
2009 
2010 	/*
2011 	 * If we have an AT_BASE element, name the mapping at that address
2012 	 * using the interpreter pathname.  Name the corresponding data
2013 	 * mapping after the interpreter as well.
2014 	 */
2015 	if (base_addr != (uintptr_t)-1L) {
2016 		elf_file_t intf;
2017 
2018 		P->map_ldso = core_name_mapping(P, base_addr, interp);
2019 
2020 		if (core_elf_open(&intf, interp, ET_DYN, NULL) == 0) {
2021 			rd_loadobj_t rl;
2022 			map_info_t *dmp;
2023 
2024 			rl.rl_base = base_addr;
2025 			dmp = core_find_data(P, intf.e_elf, &rl);
2026 
2027 			if (dmp != NULL) {
2028 				dprintf("renamed data at %p to %s\n",
2029 				    (void *)rl.rl_data_base, interp);
2030 				(void) strncpy(dmp->map_pmap.pr_mapname,
2031 				    interp, PRMAPSZ);
2032 				dmp->map_pmap.pr_mapname[PRMAPSZ - 1] = '\0';
2033 			}
2034 		}
2035 
2036 		core_elf_close(&intf);
2037 	}
2038 
2039 	/*
2040 	 * If we have an AT_ENTRY element, name the mapping at that address
2041 	 * using the special name "a.out" just like /proc does.
2042 	 */
2043 	if ((addr = Pgetauxval(P, AT_ENTRY)) != (uintptr_t)-1L)
2044 		P->map_exec = core_name_mapping(P, addr, "a.out");
2045 
2046 	/*
2047 	 * If we're a statically linked executable, then just locate the
2048 	 * executable's text and data and name them after the executable.
2049 	 */
2050 	if (base_addr == (uintptr_t)-1L) {
2051 		map_info_t *tmp, *dmp;
2052 		file_info_t *fp;
2053 		rd_loadobj_t rl;
2054 
2055 		if ((tmp = core_find_text(P, aout.e_elf, &rl)) != NULL &&
2056 		    (dmp = core_find_data(P, aout.e_elf, &rl)) != NULL) {
2057 			(void) strncpy(tmp->map_pmap.pr_mapname,
2058 			    execname, PRMAPSZ);
2059 			tmp->map_pmap.pr_mapname[PRMAPSZ - 1] = '\0';
2060 			(void) strncpy(dmp->map_pmap.pr_mapname,
2061 			    execname, PRMAPSZ);
2062 			dmp->map_pmap.pr_mapname[PRMAPSZ - 1] = '\0';
2063 		}
2064 
2065 		if ((P->map_exec = tmp) != NULL &&
2066 		    (fp = malloc(sizeof (file_info_t))) != NULL) {
2067 
2068 			(void) memset(fp, 0, sizeof (file_info_t));
2069 
2070 			list_link(fp, &P->file_head);
2071 			tmp->map_file = fp;
2072 			P->num_files++;
2073 
2074 			fp->file_ref = 1;
2075 			fp->file_fd = -1;
2076 
2077 			fp->file_lo = malloc(sizeof (rd_loadobj_t));
2078 			fp->file_lname = strdup(execname);
2079 
2080 			if (fp->file_lo)
2081 				*fp->file_lo = rl;
2082 			if (fp->file_lname)
2083 				fp->file_lbase = basename(fp->file_lname);
2084 			if (fp->file_rname)
2085 				fp->file_rbase = basename(fp->file_rname);
2086 
2087 			(void) strcpy(fp->file_pname,
2088 			    P->mappings[0].map_pmap.pr_mapname);
2089 			fp->file_map = tmp;
2090 
2091 			Pbuild_file_symtab(P, fp);
2092 
2093 			if (dmp != NULL) {
2094 				dmp->map_file = fp;
2095 				fp->file_ref++;
2096 			}
2097 		}
2098 	}
2099 
2100 	core_elf_close(&aout);
2101 
2102 	/*
2103 	 * We now have enough information to initialize librtld_db.
2104 	 * After it warms up, we can iterate through the load object chain
2105 	 * in the core, which will allow us to construct the file info
2106 	 * we need to provide symbol information for the other shared
2107 	 * libraries, and also to fill in the missing mapping names.
2108 	 */
2109 	rd_log(_libproc_debug);
2110 
2111 	if ((P->rap = rd_new(P)) != NULL) {
2112 		(void) rd_loadobj_iter(P->rap, (rl_iter_f *)
2113 		    core_iter_mapping, P);
2114 
2115 		if (P->core->core_errno != 0) {
2116 			errno = P->core->core_errno;
2117 			*perr = G_STRANGE;
2118 			goto err;
2119 		}
2120 	} else
2121 		dprintf("failed to initialize rtld_db agent\n");
2122 
2123 	/*
2124 	 * If there are sections, load them and process the data from any
2125 	 * sections that we can use to annotate the file_info_t's.
2126 	 */
2127 	core_load_shdrs(P, &core);
2128 
2129 	/*
2130 	 * If we previously located a stack or break mapping, and they are
2131 	 * still anonymous, we now assume that they were MAP_ANON mappings.
2132 	 * If brk_mp turns out to now have a name, then the heap is still
2133 	 * sitting at the end of the executable's data+bss mapping: remove
2134 	 * the previous MA_BREAK setting to be consistent with /proc.
2135 	 */
2136 	if (stk_mp != NULL && stk_mp->map_pmap.pr_mapname[0] == '\0')
2137 		stk_mp->map_pmap.pr_mflags |= MA_ANON;
2138 	if (brk_mp != NULL && brk_mp->map_pmap.pr_mapname[0] == '\0')
2139 		brk_mp->map_pmap.pr_mflags |= MA_ANON;
2140 	else if (brk_mp != NULL)
2141 		brk_mp->map_pmap.pr_mflags &= ~MA_BREAK;
2142 
2143 	*perr = 0;
2144 	return (P);
2145 
2146 err:
2147 	Pfree(P);
2148 	core_elf_close(&aout);
2149 	return (NULL);
2150 }
2151 
2152 /*
2153  * Grab a core file using a pathname.  We just open it and call Pfgrab_core().
2154  */
2155 struct ps_prochandle *
2156 Pgrab_core(const char *core, const char *aout, int gflag, int *perr)
2157 {
2158 	int fd, oflag = (gflag & PGRAB_RDONLY) ? O_RDONLY : O_RDWR;
2159 
2160 	if ((fd = open64(core, oflag)) >= 0)
2161 		return (Pfgrab_core(fd, aout, perr));
2162 
2163 	if (errno != ENOENT)
2164 		*perr = G_STRANGE;
2165 	else
2166 		*perr = G_NOCORE;
2167 
2168 	return (NULL);
2169 }
2170