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