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