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