xref: /illumos-gate/usr/src/lib/libproc/common/Pcore.c (revision ed093b41)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 /*
26  * Copyright 2012 DEY Storage Systems, Inc.  All rights reserved.
27  * Copyright (c) 2018, Joyent, Inc. All rights reserved.
28  * Copyright (c) 2013 by Delphix. All rights reserved.
29  * Copyright 2015 Gary Mills
30  * Copyright 2020 OmniOS Community Edition (OmniOSce) Association.
31  * Copyright 2023 Oxide Computer Company
32  */
33 
34 #include <sys/types.h>
35 #include <sys/utsname.h>
36 #include <sys/sysmacros.h>
37 #include <sys/proc.h>
38 
39 #include <alloca.h>
40 #include <rtld_db.h>
41 #include <libgen.h>
42 #include <limits.h>
43 #include <string.h>
44 #include <stdlib.h>
45 #include <unistd.h>
46 #include <errno.h>
47 #include <gelf.h>
48 #include <stddef.h>
49 #include <signal.h>
50 
51 #include "libproc.h"
52 #include "Pcontrol.h"
53 #include "P32ton.h"
54 #include "Putil.h"
55 #include "proc_fd.h"
56 #ifdef __x86
57 #include "Pcore_linux.h"
58 #endif
59 
60 /*
61  * Pcore.c - Code to initialize a ps_prochandle from a core dump.  We
62  * allocate an additional structure to hold information from the core
63  * file, and attach this to the standard ps_prochandle in place of the
64  * ability to examine /proc/<pid>/ files.
65  */
66 
67 /*
68  * Basic i/o function for reading and writing from the process address space
69  * stored in the core file and associated shared libraries.  We compute the
70  * appropriate fd and offsets, and let the provided prw function do the rest.
71  */
72 static ssize_t
core_rw(struct ps_prochandle * P,void * buf,size_t n,uintptr_t addr,ssize_t (* prw)(int,void *,size_t,off64_t))73 core_rw(struct ps_prochandle *P, void *buf, size_t n, uintptr_t addr,
74     ssize_t (*prw)(int, void *, size_t, off64_t))
75 {
76 	ssize_t resid = n;
77 
78 	while (resid != 0) {
79 		map_info_t *mp = Paddr2mptr(P, addr);
80 
81 		uintptr_t mapoff;
82 		ssize_t len;
83 		off64_t off;
84 		int fd;
85 
86 		if (mp == NULL)
87 			break;	/* No mapping for this address */
88 
89 		if (mp->map_pmap.pr_mflags & MA_RESERVED1) {
90 			if (mp->map_file == NULL || mp->map_file->file_fd < 0)
91 				break;	/* No file or file not open */
92 
93 			fd = mp->map_file->file_fd;
94 		} else
95 			fd = P->asfd;
96 
97 		mapoff = addr - mp->map_pmap.pr_vaddr;
98 		len = MIN(resid, mp->map_pmap.pr_size - mapoff);
99 		off = mp->map_offset + mapoff;
100 
101 		if ((len = prw(fd, buf, len, off)) <= 0)
102 			break;
103 
104 		resid -= len;
105 		addr += len;
106 		buf = (char *)buf + len;
107 	}
108 
109 	/*
110 	 * Important: Be consistent with the behavior of i/o on the as file:
111 	 * writing to an invalid address yields EIO; reading from an invalid
112 	 * address falls through to returning success and zero bytes.
113 	 */
114 	if (resid == n && n != 0 && prw != pread64) {
115 		errno = EIO;
116 		return (-1);
117 	}
118 
119 	return (n - resid);
120 }
121 
122 /*ARGSUSED*/
123 static ssize_t
Pread_core(struct ps_prochandle * P,void * buf,size_t n,uintptr_t addr,void * data)124 Pread_core(struct ps_prochandle *P, void *buf, size_t n, uintptr_t addr,
125     void *data)
126 {
127 	return (core_rw(P, buf, n, addr, pread64));
128 }
129 
130 /*ARGSUSED*/
131 static ssize_t
Pwrite_core(struct ps_prochandle * P,const void * buf,size_t n,uintptr_t addr,void * data)132 Pwrite_core(struct ps_prochandle *P, const void *buf, size_t n, uintptr_t addr,
133     void *data)
134 {
135 	return (core_rw(P, (void *)buf, n, addr,
136 	    (ssize_t (*)(int, void *, size_t, off64_t)) pwrite64));
137 }
138 
139 /*ARGSUSED*/
140 static int
Pcred_core(struct ps_prochandle * P,prcred_t * pcrp,int ngroups,void * data)141 Pcred_core(struct ps_prochandle *P, prcred_t *pcrp, int ngroups, void *data)
142 {
143 	core_info_t *core = data;
144 
145 	if (core->core_cred != NULL) {
146 		/*
147 		 * Avoid returning more supplementary group data than the
148 		 * caller has allocated in their buffer.  We expect them to
149 		 * check pr_ngroups afterward and potentially call us again.
150 		 */
151 		ngroups = MIN(ngroups, core->core_cred->pr_ngroups);
152 
153 		(void) memcpy(pcrp, core->core_cred,
154 		    sizeof (prcred_t) + (ngroups - 1) * sizeof (gid_t));
155 
156 		return (0);
157 	}
158 
159 	errno = ENODATA;
160 	return (-1);
161 }
162 
163 /*ARGSUSED*/
164 static int
Psecflags_core(struct ps_prochandle * P,prsecflags_t ** psf,void * data)165 Psecflags_core(struct ps_prochandle *P, prsecflags_t **psf, void *data)
166 {
167 	core_info_t *core = data;
168 
169 	if (core->core_secflags == NULL) {
170 		errno = ENODATA;
171 		return (-1);
172 	}
173 
174 	if ((*psf = calloc(1, sizeof (prsecflags_t))) == NULL)
175 		return (-1);
176 
177 	(void) memcpy(*psf, core->core_secflags, sizeof (prsecflags_t));
178 
179 	return (0);
180 }
181 
182 /*ARGSUSED*/
183 static int
Ppriv_core(struct ps_prochandle * P,prpriv_t ** pprv,void * data)184 Ppriv_core(struct ps_prochandle *P, prpriv_t **pprv, void *data)
185 {
186 	core_info_t *core = data;
187 
188 	if (core->core_priv == NULL) {
189 		errno = ENODATA;
190 		return (-1);
191 	}
192 
193 	*pprv = malloc(core->core_priv_size);
194 	if (*pprv == NULL) {
195 		return (-1);
196 	}
197 
198 	(void) memcpy(*pprv, core->core_priv, core->core_priv_size);
199 	return (0);
200 }
201 
202 /*ARGSUSED*/
203 static const psinfo_t *
Ppsinfo_core(struct ps_prochandle * P,psinfo_t * psinfo,void * data)204 Ppsinfo_core(struct ps_prochandle *P, psinfo_t *psinfo, void *data)
205 {
206 	return (&P->psinfo);
207 }
208 
209 /*ARGSUSED*/
210 static void
Pfini_core(struct ps_prochandle * P,void * data)211 Pfini_core(struct ps_prochandle *P, void *data)
212 {
213 	core_info_t *core = data;
214 
215 	if (core != NULL) {
216 		extern void __priv_free_info(void *);
217 		lwp_info_t *lwp;
218 
219 		while ((lwp = list_remove_head(&core->core_lwp_head)) != NULL) {
220 #ifdef __sparc
221 			if (lwp->lwp_gwins != NULL)
222 				free(lwp->lwp_gwins);
223 			if (lwp->lwp_xregs != NULL)
224 				free(lwp->lwp_xregs);
225 			if (lwp->lwp_asrs != NULL)
226 				free(lwp->lwp_asrs);
227 #endif
228 			free(lwp);
229 		}
230 
231 		if (core->core_platform != NULL)
232 			free(core->core_platform);
233 		if (core->core_uts != NULL)
234 			free(core->core_uts);
235 		if (core->core_cred != NULL)
236 			free(core->core_cred);
237 		if (core->core_priv != NULL)
238 			free(core->core_priv);
239 		if (core->core_privinfo != NULL)
240 			__priv_free_info(core->core_privinfo);
241 		if (core->core_ppii != NULL)
242 			free(core->core_ppii);
243 		if (core->core_zonename != NULL)
244 			free(core->core_zonename);
245 		if (core->core_secflags != NULL)
246 			free(core->core_secflags);
247 		if (core->core_upanic != NULL)
248 			free(core->core_upanic);
249 #ifdef __x86
250 		if (core->core_ldt != NULL)
251 			free(core->core_ldt);
252 #endif
253 
254 		free(core);
255 	}
256 }
257 
258 /*ARGSUSED*/
259 static char *
Pplatform_core(struct ps_prochandle * P,char * s,size_t n,void * data)260 Pplatform_core(struct ps_prochandle *P, char *s, size_t n, void *data)
261 {
262 	core_info_t *core = data;
263 
264 	if (core->core_platform == NULL) {
265 		errno = ENODATA;
266 		return (NULL);
267 	}
268 	(void) strncpy(s, core->core_platform, n - 1);
269 	s[n - 1] = '\0';
270 	return (s);
271 }
272 
273 /*ARGSUSED*/
274 static int
Puname_core(struct ps_prochandle * P,struct utsname * u,void * data)275 Puname_core(struct ps_prochandle *P, struct utsname *u, void *data)
276 {
277 	core_info_t *core = data;
278 
279 	if (core->core_uts == NULL) {
280 		errno = ENODATA;
281 		return (-1);
282 	}
283 	(void) memcpy(u, core->core_uts, sizeof (struct utsname));
284 	return (0);
285 }
286 
287 /*ARGSUSED*/
288 static char *
Pzonename_core(struct ps_prochandle * P,char * s,size_t n,void * data)289 Pzonename_core(struct ps_prochandle *P, char *s, size_t n, void *data)
290 {
291 	core_info_t *core = data;
292 
293 	if (core->core_zonename == NULL) {
294 		errno = ENODATA;
295 		return (NULL);
296 	}
297 	(void) strlcpy(s, core->core_zonename, n);
298 	return (s);
299 }
300 
301 #ifdef __x86
302 /*ARGSUSED*/
303 static int
Pldt_core(struct ps_prochandle * P,struct ssd * pldt,int nldt,void * data)304 Pldt_core(struct ps_prochandle *P, struct ssd *pldt, int nldt, void *data)
305 {
306 	core_info_t *core = data;
307 
308 	if (pldt == NULL || nldt == 0)
309 		return (core->core_nldt);
310 
311 	if (core->core_ldt != NULL) {
312 		nldt = MIN(nldt, core->core_nldt);
313 
314 		(void) memcpy(pldt, core->core_ldt,
315 		    nldt * sizeof (struct ssd));
316 
317 		return (nldt);
318 	}
319 
320 	errno = ENODATA;
321 	return (-1);
322 }
323 #endif
324 
325 static const ps_ops_t P_core_ops = {
326 	.pop_pread	= Pread_core,
327 	.pop_pwrite	= Pwrite_core,
328 	.pop_cred	= Pcred_core,
329 	.pop_priv	= Ppriv_core,
330 	.pop_psinfo	= Ppsinfo_core,
331 	.pop_fini	= Pfini_core,
332 	.pop_platform	= Pplatform_core,
333 	.pop_uname	= Puname_core,
334 	.pop_zonename	= Pzonename_core,
335 	.pop_secflags	= Psecflags_core,
336 #ifdef __x86
337 	.pop_ldt	= Pldt_core
338 #endif
339 };
340 
341 /*
342  * Return the lwp_info_t for the given lwpid.  If no such lwpid has been
343  * encountered yet, allocate a new structure and return a pointer to it.
344  * Create a list of lwp_info_t structures sorted in decreasing lwp_id order.
345  */
346 static lwp_info_t *
lwpid2info(struct ps_prochandle * P,lwpid_t id)347 lwpid2info(struct ps_prochandle *P, lwpid_t id)
348 {
349 	core_info_t *core = P->data;
350 	lwp_info_t *lwp, *prev;
351 
352 	for (lwp = list_head(&core->core_lwp_head); lwp != NULL;
353 	    lwp = list_next(&core->core_lwp_head, lwp)) {
354 		if (lwp->lwp_id == id) {
355 			core->core_lwp = lwp;
356 			return (lwp);
357 		}
358 		if (lwp->lwp_id < id) {
359 			break;
360 		}
361 	}
362 
363 	prev = lwp;
364 	if ((lwp = calloc(1, sizeof (lwp_info_t))) == NULL)
365 		return (NULL);
366 
367 	list_insert_before(&core->core_lwp_head, prev, lwp);
368 	lwp->lwp_id = id;
369 
370 	core->core_lwp = lwp;
371 
372 	return (lwp);
373 }
374 
375 /*
376  * The core file itself contains a series of NOTE segments containing saved
377  * structures from /proc at the time the process died.  For each note we
378  * comprehend, we define a function to read it in from the core file,
379  * convert it to our native data model if necessary, and store it inside
380  * the ps_prochandle.  Each function is invoked by Pfgrab_core() with the
381  * seek pointer on P->asfd positioned appropriately.  We populate a table
382  * of pointers to these note functions below.
383  */
384 
385 static int
note_pstatus(struct ps_prochandle * P,size_t nbytes)386 note_pstatus(struct ps_prochandle *P, size_t nbytes)
387 {
388 #ifdef _LP64
389 	core_info_t *core = P->data;
390 
391 	if (core->core_dmodel == PR_MODEL_ILP32) {
392 		pstatus32_t ps32;
393 
394 		if (nbytes < sizeof (pstatus32_t) ||
395 		    read(P->asfd, &ps32, sizeof (ps32)) != sizeof (ps32))
396 			goto err;
397 
398 		pstatus_32_to_n(&ps32, &P->status);
399 
400 	} else
401 #endif
402 	if (nbytes < sizeof (pstatus_t) ||
403 	    read(P->asfd, &P->status, sizeof (pstatus_t)) != sizeof (pstatus_t))
404 		goto err;
405 
406 	P->orig_status = P->status;
407 	P->pid = P->status.pr_pid;
408 
409 	return (0);
410 
411 err:
412 	dprintf("Pgrab_core: failed to read NT_PSTATUS\n");
413 	return (-1);
414 }
415 
416 static int
note_lwpstatus(struct ps_prochandle * P,size_t nbytes)417 note_lwpstatus(struct ps_prochandle *P, size_t nbytes)
418 {
419 	lwp_info_t *lwp;
420 	lwpstatus_t lps;
421 
422 #ifdef _LP64
423 	core_info_t *core = P->data;
424 
425 	if (core->core_dmodel == PR_MODEL_ILP32) {
426 		lwpstatus32_t l32;
427 
428 		if (nbytes < sizeof (lwpstatus32_t) ||
429 		    read(P->asfd, &l32, sizeof (l32)) != sizeof (l32))
430 			goto err;
431 
432 		lwpstatus_32_to_n(&l32, &lps);
433 	} else
434 #endif
435 	if (nbytes < sizeof (lwpstatus_t) ||
436 	    read(P->asfd, &lps, sizeof (lps)) != sizeof (lps))
437 		goto err;
438 
439 	if ((lwp = lwpid2info(P, lps.pr_lwpid)) == NULL) {
440 		dprintf("Pgrab_core: failed to add NT_LWPSTATUS\n");
441 		return (-1);
442 	}
443 
444 	/*
445 	 * Erase a useless and confusing artifact of the kernel implementation:
446 	 * the lwps which did *not* create the core will show SIGKILL.  We can
447 	 * be assured this is bogus because SIGKILL can't produce core files.
448 	 */
449 	if (lps.pr_cursig == SIGKILL)
450 		lps.pr_cursig = 0;
451 
452 	(void) memcpy(&lwp->lwp_status, &lps, sizeof (lps));
453 	return (0);
454 
455 err:
456 	dprintf("Pgrab_core: failed to read NT_LWPSTATUS\n");
457 	return (-1);
458 }
459 
460 #ifdef __x86
461 
462 static void
lx_prpsinfo32_to_psinfo(lx_prpsinfo32_t * p32,psinfo_t * psinfo)463 lx_prpsinfo32_to_psinfo(lx_prpsinfo32_t *p32, psinfo_t *psinfo)
464 {
465 	psinfo->pr_flag = p32->pr_flag;
466 	psinfo->pr_pid = p32->pr_pid;
467 	psinfo->pr_ppid = p32->pr_ppid;
468 	psinfo->pr_uid = p32->pr_uid;
469 	psinfo->pr_gid = p32->pr_gid;
470 	psinfo->pr_sid = p32->pr_sid;
471 	psinfo->pr_pgid = p32->pr_pgrp;
472 
473 	(void) memcpy(psinfo->pr_fname, p32->pr_fname,
474 	    sizeof (psinfo->pr_fname));
475 	(void) memcpy(psinfo->pr_psargs, p32->pr_psargs,
476 	    sizeof (psinfo->pr_psargs));
477 }
478 
479 static void
lx_prpsinfo64_to_psinfo(lx_prpsinfo64_t * p64,psinfo_t * psinfo)480 lx_prpsinfo64_to_psinfo(lx_prpsinfo64_t *p64, psinfo_t *psinfo)
481 {
482 	psinfo->pr_flag = p64->pr_flag;
483 	psinfo->pr_pid = p64->pr_pid;
484 	psinfo->pr_ppid = p64->pr_ppid;
485 	psinfo->pr_uid = p64->pr_uid;
486 	psinfo->pr_gid = p64->pr_gid;
487 	psinfo->pr_sid = p64->pr_sid;
488 	psinfo->pr_pgid = p64->pr_pgrp;
489 	psinfo->pr_pgid = p64->pr_pgrp;
490 
491 	(void) memcpy(psinfo->pr_fname, p64->pr_fname,
492 	    sizeof (psinfo->pr_fname));
493 	(void) memcpy(psinfo->pr_psargs, p64->pr_psargs,
494 	    sizeof (psinfo->pr_psargs));
495 }
496 
497 static int
note_linux_psinfo(struct ps_prochandle * P,size_t nbytes)498 note_linux_psinfo(struct ps_prochandle *P, size_t nbytes)
499 {
500 	core_info_t *core = P->data;
501 	lx_prpsinfo32_t p32;
502 	lx_prpsinfo64_t p64;
503 
504 	if (core->core_dmodel == PR_MODEL_ILP32) {
505 		if (nbytes < sizeof (p32) ||
506 		    read(P->asfd, &p32, sizeof (p32)) != sizeof (p32))
507 			goto err;
508 
509 		lx_prpsinfo32_to_psinfo(&p32, &P->psinfo);
510 	} else {
511 		if (nbytes < sizeof (p64) ||
512 		    read(P->asfd, &p64, sizeof (p64)) != sizeof (p64))
513 			goto err;
514 
515 		lx_prpsinfo64_to_psinfo(&p64, &P->psinfo);
516 	}
517 
518 
519 	P->status.pr_pid = P->psinfo.pr_pid;
520 	P->status.pr_ppid = P->psinfo.pr_ppid;
521 	P->status.pr_pgid = P->psinfo.pr_pgid;
522 	P->status.pr_sid = P->psinfo.pr_sid;
523 
524 	P->psinfo.pr_nlwp = 0;
525 	P->status.pr_nlwp = 0;
526 
527 	return (0);
528 err:
529 	dprintf("Pgrab_core: failed to read NT_PSINFO\n");
530 	return (-1);
531 }
532 
533 static void
lx_prstatus64_to_lwp(lx_prstatus64_t * prs64,lwp_info_t * lwp)534 lx_prstatus64_to_lwp(lx_prstatus64_t *prs64, lwp_info_t *lwp)
535 {
536 	LTIME_TO_TIMESPEC(lwp->lwp_status.pr_utime, prs64->pr_utime);
537 	LTIME_TO_TIMESPEC(lwp->lwp_status.pr_stime, prs64->pr_stime);
538 
539 	lwp->lwp_status.pr_reg[REG_R15] = prs64->pr_reg.lxr_r15;
540 	lwp->lwp_status.pr_reg[REG_R14] = prs64->pr_reg.lxr_r14;
541 	lwp->lwp_status.pr_reg[REG_R13] = prs64->pr_reg.lxr_r13;
542 	lwp->lwp_status.pr_reg[REG_R12] = prs64->pr_reg.lxr_r12;
543 	lwp->lwp_status.pr_reg[REG_R11] = prs64->pr_reg.lxr_r11;
544 	lwp->lwp_status.pr_reg[REG_R10] = prs64->pr_reg.lxr_r10;
545 	lwp->lwp_status.pr_reg[REG_R9] = prs64->pr_reg.lxr_r9;
546 	lwp->lwp_status.pr_reg[REG_R8] = prs64->pr_reg.lxr_r8;
547 
548 	lwp->lwp_status.pr_reg[REG_RDI] = prs64->pr_reg.lxr_rdi;
549 	lwp->lwp_status.pr_reg[REG_RSI] = prs64->pr_reg.lxr_rsi;
550 	lwp->lwp_status.pr_reg[REG_RBP] = prs64->pr_reg.lxr_rbp;
551 	lwp->lwp_status.pr_reg[REG_RBX] = prs64->pr_reg.lxr_rbx;
552 	lwp->lwp_status.pr_reg[REG_RDX] = prs64->pr_reg.lxr_rdx;
553 	lwp->lwp_status.pr_reg[REG_RCX] = prs64->pr_reg.lxr_rcx;
554 	lwp->lwp_status.pr_reg[REG_RAX] = prs64->pr_reg.lxr_rax;
555 
556 	lwp->lwp_status.pr_reg[REG_RIP] = prs64->pr_reg.lxr_rip;
557 	lwp->lwp_status.pr_reg[REG_CS] = prs64->pr_reg.lxr_cs;
558 	lwp->lwp_status.pr_reg[REG_RSP] = prs64->pr_reg.lxr_rsp;
559 	lwp->lwp_status.pr_reg[REG_FS] = prs64->pr_reg.lxr_fs;
560 	lwp->lwp_status.pr_reg[REG_SS] = prs64->pr_reg.lxr_ss;
561 	lwp->lwp_status.pr_reg[REG_GS] = prs64->pr_reg.lxr_gs;
562 	lwp->lwp_status.pr_reg[REG_ES] = prs64->pr_reg.lxr_es;
563 	lwp->lwp_status.pr_reg[REG_DS] = prs64->pr_reg.lxr_ds;
564 
565 	lwp->lwp_status.pr_reg[REG_GSBASE] = prs64->pr_reg.lxr_gs_base;
566 	lwp->lwp_status.pr_reg[REG_FSBASE] = prs64->pr_reg.lxr_fs_base;
567 }
568 
569 static void
lx_prstatus32_to_lwp(lx_prstatus32_t * prs32,lwp_info_t * lwp)570 lx_prstatus32_to_lwp(lx_prstatus32_t *prs32, lwp_info_t *lwp)
571 {
572 	LTIME_TO_TIMESPEC(lwp->lwp_status.pr_utime, prs32->pr_utime);
573 	LTIME_TO_TIMESPEC(lwp->lwp_status.pr_stime, prs32->pr_stime);
574 
575 #ifdef __amd64
576 	lwp->lwp_status.pr_reg[REG_GS] = prs32->pr_reg.lxr_gs;
577 	lwp->lwp_status.pr_reg[REG_FS] = prs32->pr_reg.lxr_fs;
578 	lwp->lwp_status.pr_reg[REG_DS] = prs32->pr_reg.lxr_ds;
579 	lwp->lwp_status.pr_reg[REG_ES] = prs32->pr_reg.lxr_es;
580 	lwp->lwp_status.pr_reg[REG_RDI] = prs32->pr_reg.lxr_di;
581 	lwp->lwp_status.pr_reg[REG_RSI] = prs32->pr_reg.lxr_si;
582 	lwp->lwp_status.pr_reg[REG_RBP] = prs32->pr_reg.lxr_bp;
583 	lwp->lwp_status.pr_reg[REG_RBX] = prs32->pr_reg.lxr_bx;
584 	lwp->lwp_status.pr_reg[REG_RDX] = prs32->pr_reg.lxr_dx;
585 	lwp->lwp_status.pr_reg[REG_RCX] = prs32->pr_reg.lxr_cx;
586 	lwp->lwp_status.pr_reg[REG_RAX] = prs32->pr_reg.lxr_ax;
587 	lwp->lwp_status.pr_reg[REG_RIP] = prs32->pr_reg.lxr_ip;
588 	lwp->lwp_status.pr_reg[REG_CS] = prs32->pr_reg.lxr_cs;
589 	lwp->lwp_status.pr_reg[REG_RFL] = prs32->pr_reg.lxr_flags;
590 	lwp->lwp_status.pr_reg[REG_RSP] = prs32->pr_reg.lxr_sp;
591 	lwp->lwp_status.pr_reg[REG_SS] = prs32->pr_reg.lxr_ss;
592 #else /* __amd64 */
593 	lwp->lwp_status.pr_reg[EBX] = prs32->pr_reg.lxr_bx;
594 	lwp->lwp_status.pr_reg[ECX] = prs32->pr_reg.lxr_cx;
595 	lwp->lwp_status.pr_reg[EDX] = prs32->pr_reg.lxr_dx;
596 	lwp->lwp_status.pr_reg[ESI] = prs32->pr_reg.lxr_si;
597 	lwp->lwp_status.pr_reg[EDI] = prs32->pr_reg.lxr_di;
598 	lwp->lwp_status.pr_reg[EBP] = prs32->pr_reg.lxr_bp;
599 	lwp->lwp_status.pr_reg[EAX] = prs32->pr_reg.lxr_ax;
600 	lwp->lwp_status.pr_reg[EIP] = prs32->pr_reg.lxr_ip;
601 	lwp->lwp_status.pr_reg[UESP] = prs32->pr_reg.lxr_sp;
602 
603 	lwp->lwp_status.pr_reg[DS] = prs32->pr_reg.lxr_ds;
604 	lwp->lwp_status.pr_reg[ES] = prs32->pr_reg.lxr_es;
605 	lwp->lwp_status.pr_reg[FS] = prs32->pr_reg.lxr_fs;
606 	lwp->lwp_status.pr_reg[GS] = prs32->pr_reg.lxr_gs;
607 	lwp->lwp_status.pr_reg[CS] = prs32->pr_reg.lxr_cs;
608 	lwp->lwp_status.pr_reg[SS] = prs32->pr_reg.lxr_ss;
609 
610 	lwp->lwp_status.pr_reg[EFL] = prs32->pr_reg.lxr_flags;
611 #endif	/* !__amd64 */
612 }
613 
614 static int
note_linux_prstatus(struct ps_prochandle * P,size_t nbytes)615 note_linux_prstatus(struct ps_prochandle *P, size_t nbytes)
616 {
617 	core_info_t *core = P->data;
618 
619 	lx_prstatus64_t prs64;
620 	lx_prstatus32_t prs32;
621 	lwp_info_t *lwp;
622 	lwpid_t tid;
623 
624 	dprintf("looking for model %d, %ld/%ld\n", core->core_dmodel,
625 	    (ulong_t)nbytes, (ulong_t)sizeof (prs32));
626 	if (core->core_dmodel == PR_MODEL_ILP32) {
627 		if (nbytes < sizeof (prs32) ||
628 		    read(P->asfd, &prs32, sizeof (prs32)) != nbytes)
629 			goto err;
630 		tid = prs32.pr_pid;
631 	} else {
632 		if (nbytes < sizeof (prs64) ||
633 		    read(P->asfd, &prs64, sizeof (prs64)) != nbytes)
634 			goto err;
635 		tid = prs64.pr_pid;
636 	}
637 
638 	if ((lwp = lwpid2info(P, tid)) == NULL) {
639 		dprintf("Pgrab_core: failed to add lwpid2info "
640 		    "linux_prstatus\n");
641 		return (-1);
642 	}
643 
644 	P->psinfo.pr_nlwp++;
645 	P->status.pr_nlwp++;
646 
647 	lwp->lwp_status.pr_lwpid = tid;
648 
649 	if (core->core_dmodel == PR_MODEL_ILP32)
650 		lx_prstatus32_to_lwp(&prs32, lwp);
651 	else
652 		lx_prstatus64_to_lwp(&prs64, lwp);
653 
654 	return (0);
655 err:
656 	dprintf("Pgrab_core: failed to read NT_PRSTATUS\n");
657 	return (-1);
658 }
659 
660 #endif /* __x86 */
661 
662 static int
note_psinfo(struct ps_prochandle * P,size_t nbytes)663 note_psinfo(struct ps_prochandle *P, size_t nbytes)
664 {
665 #ifdef _LP64
666 	core_info_t *core = P->data;
667 
668 	if (core->core_dmodel == PR_MODEL_ILP32) {
669 		psinfo32_t ps32;
670 
671 		if (nbytes < sizeof (psinfo32_t) ||
672 		    read(P->asfd, &ps32, sizeof (ps32)) != sizeof (ps32))
673 			goto err;
674 
675 		psinfo_32_to_n(&ps32, &P->psinfo);
676 	} else
677 #endif
678 	if (nbytes < sizeof (psinfo_t) ||
679 	    read(P->asfd, &P->psinfo, sizeof (psinfo_t)) != sizeof (psinfo_t))
680 		goto err;
681 
682 	dprintf("pr_fname = <%s>\n", P->psinfo.pr_fname);
683 	dprintf("pr_psargs = <%s>\n", P->psinfo.pr_psargs);
684 	dprintf("pr_wstat = 0x%x\n", P->psinfo.pr_wstat);
685 
686 	return (0);
687 
688 err:
689 	dprintf("Pgrab_core: failed to read NT_PSINFO\n");
690 	return (-1);
691 }
692 
693 static int
note_lwpsinfo(struct ps_prochandle * P,size_t nbytes)694 note_lwpsinfo(struct ps_prochandle *P, size_t nbytes)
695 {
696 	lwp_info_t *lwp;
697 	lwpsinfo_t lps;
698 
699 #ifdef _LP64
700 	core_info_t *core = P->data;
701 
702 	if (core->core_dmodel == PR_MODEL_ILP32) {
703 		lwpsinfo32_t l32;
704 
705 		if (nbytes < sizeof (lwpsinfo32_t) ||
706 		    read(P->asfd, &l32, sizeof (l32)) != sizeof (l32))
707 			goto err;
708 
709 		lwpsinfo_32_to_n(&l32, &lps);
710 	} else
711 #endif
712 	if (nbytes < sizeof (lwpsinfo_t) ||
713 	    read(P->asfd, &lps, sizeof (lps)) != sizeof (lps))
714 		goto err;
715 
716 	if ((lwp = lwpid2info(P, lps.pr_lwpid)) == NULL) {
717 		dprintf("Pgrab_core: failed to add NT_LWPSINFO\n");
718 		return (-1);
719 	}
720 
721 	(void) memcpy(&lwp->lwp_psinfo, &lps, sizeof (lps));
722 	return (0);
723 
724 err:
725 	dprintf("Pgrab_core: failed to read NT_LWPSINFO\n");
726 	return (-1);
727 }
728 
729 static int
note_lwpname(struct ps_prochandle * P,size_t nbytes)730 note_lwpname(struct ps_prochandle *P, size_t nbytes)
731 {
732 	prlwpname_t name;
733 	lwp_info_t *lwp;
734 
735 	if (nbytes != sizeof (name) ||
736 	    read(P->asfd, &name, sizeof (name)) != sizeof (name))
737 		goto err;
738 
739 	if ((lwp = lwpid2info(P, name.pr_lwpid)) == NULL)
740 		goto err;
741 
742 	if (strlcpy(lwp->lwp_name, name.pr_lwpname,
743 	    sizeof (lwp->lwp_name)) >= sizeof (lwp->lwp_name)) {
744 		errno = ENAMETOOLONG;
745 		goto err;
746 	}
747 
748 	return (0);
749 
750 err:
751 	dprintf("Pgrab_core: failed to read NT_LWPNAME\n");
752 	return (-1);
753 }
754 
755 static int
note_fdinfo(struct ps_prochandle * P,size_t nbytes)756 note_fdinfo(struct ps_prochandle *P, size_t nbytes)
757 {
758 	prfdinfo_core_t prfd;
759 	fd_info_t *fip;
760 
761 	if ((nbytes < sizeof (prfd)) ||
762 	    (read(P->asfd, &prfd, sizeof (prfd)) != sizeof (prfd))) {
763 		dprintf("Pgrab_core: failed to read NT_FDINFO\n");
764 		return (-1);
765 	}
766 
767 	if ((fip = Pfd2info(P, prfd.pr_fd)) == NULL) {
768 		dprintf("Pgrab_core: failed to add NT_FDINFO\n");
769 		return (-1);
770 	}
771 	if (fip->fd_info == NULL) {
772 		if (proc_fdinfo_from_core(&prfd, &fip->fd_info) != 0) {
773 			dprintf("Pgrab_core: failed to convert NT_FDINFO\n");
774 			return (-1);
775 		}
776 	}
777 
778 	return (0);
779 }
780 
781 static int
note_platform(struct ps_prochandle * P,size_t nbytes)782 note_platform(struct ps_prochandle *P, size_t nbytes)
783 {
784 	core_info_t *core = P->data;
785 	char *plat;
786 
787 	if (core->core_platform != NULL)
788 		return (0);	/* Already seen */
789 
790 	if (nbytes != 0 && ((plat = malloc(nbytes + 1)) != NULL)) {
791 		if (read(P->asfd, plat, nbytes) != nbytes) {
792 			dprintf("Pgrab_core: failed to read NT_PLATFORM\n");
793 			free(plat);
794 			return (-1);
795 		}
796 		plat[nbytes - 1] = '\0';
797 		core->core_platform = plat;
798 	}
799 
800 	return (0);
801 }
802 
803 static int
note_secflags(struct ps_prochandle * P,size_t nbytes)804 note_secflags(struct ps_prochandle *P, size_t nbytes)
805 {
806 	core_info_t *core = P->data;
807 	prsecflags_t *psf;
808 
809 	if (core->core_secflags != NULL)
810 		return (0);	/* Already seen */
811 
812 	if (sizeof (*psf) != nbytes) {
813 		dprintf("Pgrab_core: NT_SECFLAGS changed size."
814 		    "  Need to handle a version change?\n");
815 		return (-1);
816 	}
817 
818 	if (nbytes != 0 && ((psf = malloc(nbytes)) != NULL)) {
819 		if (read(P->asfd, psf, nbytes) != nbytes) {
820 			dprintf("Pgrab_core: failed to read NT_SECFLAGS\n");
821 			free(psf);
822 			return (-1);
823 		}
824 
825 		core->core_secflags = psf;
826 	}
827 
828 	return (0);
829 }
830 
831 static int
note_utsname(struct ps_prochandle * P,size_t nbytes)832 note_utsname(struct ps_prochandle *P, size_t nbytes)
833 {
834 	core_info_t *core = P->data;
835 	size_t ubytes = sizeof (struct utsname);
836 	struct utsname *utsp;
837 
838 	if (core->core_uts != NULL || nbytes < ubytes)
839 		return (0);	/* Already seen or bad size */
840 
841 	if ((utsp = malloc(ubytes)) == NULL)
842 		return (-1);
843 
844 	if (read(P->asfd, utsp, ubytes) != ubytes) {
845 		dprintf("Pgrab_core: failed to read NT_UTSNAME\n");
846 		free(utsp);
847 		return (-1);
848 	}
849 
850 	if (_libproc_debug) {
851 		dprintf("uts.sysname = \"%s\"\n", utsp->sysname);
852 		dprintf("uts.nodename = \"%s\"\n", utsp->nodename);
853 		dprintf("uts.release = \"%s\"\n", utsp->release);
854 		dprintf("uts.version = \"%s\"\n", utsp->version);
855 		dprintf("uts.machine = \"%s\"\n", utsp->machine);
856 	}
857 
858 	core->core_uts = utsp;
859 	return (0);
860 }
861 
862 static int
note_content(struct ps_prochandle * P,size_t nbytes)863 note_content(struct ps_prochandle *P, size_t nbytes)
864 {
865 	core_info_t *core = P->data;
866 	core_content_t content;
867 
868 	if (sizeof (core->core_content) != nbytes)
869 		return (-1);
870 
871 	if (read(P->asfd, &content, sizeof (content)) != sizeof (content))
872 		return (-1);
873 
874 	core->core_content = content;
875 
876 	dprintf("core content = %llx\n", content);
877 
878 	return (0);
879 }
880 
881 static int
note_cred(struct ps_prochandle * P,size_t nbytes)882 note_cred(struct ps_prochandle *P, size_t nbytes)
883 {
884 	core_info_t *core = P->data;
885 	prcred_t *pcrp;
886 	int ngroups;
887 	const size_t min_size = sizeof (prcred_t) - sizeof (gid_t);
888 
889 	/*
890 	 * We allow for prcred_t notes that are actually smaller than a
891 	 * prcred_t since the last member isn't essential if there are
892 	 * no group memberships. This allows for more flexibility when it
893 	 * comes to slightly malformed -- but still valid -- notes.
894 	 */
895 	if (core->core_cred != NULL || nbytes < min_size)
896 		return (0);	/* Already seen or bad size */
897 
898 	ngroups = (nbytes - min_size) / sizeof (gid_t);
899 	nbytes = sizeof (prcred_t) + (ngroups - 1) * sizeof (gid_t);
900 
901 	if ((pcrp = malloc(nbytes)) == NULL)
902 		return (-1);
903 
904 	if (read(P->asfd, pcrp, nbytes) != nbytes) {
905 		dprintf("Pgrab_core: failed to read NT_PRCRED\n");
906 		free(pcrp);
907 		return (-1);
908 	}
909 
910 	if (pcrp->pr_ngroups > ngroups) {
911 		dprintf("pr_ngroups = %d; resetting to %d based on note size\n",
912 		    pcrp->pr_ngroups, ngroups);
913 		pcrp->pr_ngroups = ngroups;
914 	}
915 
916 	core->core_cred = pcrp;
917 	return (0);
918 }
919 
920 #ifdef __x86
921 static int
note_ldt(struct ps_prochandle * P,size_t nbytes)922 note_ldt(struct ps_prochandle *P, size_t nbytes)
923 {
924 	core_info_t *core = P->data;
925 	struct ssd *pldt;
926 	uint_t nldt;
927 
928 	if (core->core_ldt != NULL || nbytes < sizeof (struct ssd))
929 		return (0);	/* Already seen or bad size */
930 
931 	nldt = nbytes / sizeof (struct ssd);
932 	nbytes = nldt * sizeof (struct ssd);
933 
934 	if ((pldt = malloc(nbytes)) == NULL)
935 		return (-1);
936 
937 	if (read(P->asfd, pldt, nbytes) != nbytes) {
938 		dprintf("Pgrab_core: failed to read NT_LDT\n");
939 		free(pldt);
940 		return (-1);
941 	}
942 
943 	core->core_ldt = pldt;
944 	core->core_nldt = nldt;
945 	return (0);
946 }
947 #endif	/* __i386 */
948 
949 static int
note_priv(struct ps_prochandle * P,size_t nbytes)950 note_priv(struct ps_prochandle *P, size_t nbytes)
951 {
952 	core_info_t *core = P->data;
953 	prpriv_t *pprvp;
954 
955 	if (core->core_priv != NULL || nbytes < sizeof (prpriv_t))
956 		return (0);	/* Already seen or bad size */
957 
958 	if ((pprvp = malloc(nbytes)) == NULL)
959 		return (-1);
960 
961 	if (read(P->asfd, pprvp, nbytes) != nbytes) {
962 		dprintf("Pgrab_core: failed to read NT_PRPRIV\n");
963 		free(pprvp);
964 		return (-1);
965 	}
966 
967 	core->core_priv = pprvp;
968 	core->core_priv_size = nbytes;
969 	return (0);
970 }
971 
972 static int
note_priv_info(struct ps_prochandle * P,size_t nbytes)973 note_priv_info(struct ps_prochandle *P, size_t nbytes)
974 {
975 	core_info_t *core = P->data;
976 	extern void *__priv_parse_info();
977 	priv_impl_info_t *ppii;
978 
979 	if (core->core_privinfo != NULL ||
980 	    nbytes < sizeof (priv_impl_info_t))
981 		return (0);	/* Already seen or bad size */
982 
983 	if ((ppii = malloc(nbytes)) == NULL)
984 		return (-1);
985 
986 	if (read(P->asfd, ppii, nbytes) != nbytes ||
987 	    PRIV_IMPL_INFO_SIZE(ppii) != nbytes) {
988 		dprintf("Pgrab_core: failed to read NT_PRPRIVINFO\n");
989 		free(ppii);
990 		return (-1);
991 	}
992 
993 	core->core_privinfo = __priv_parse_info(ppii);
994 	core->core_ppii = ppii;
995 	return (0);
996 }
997 
998 static int
note_zonename(struct ps_prochandle * P,size_t nbytes)999 note_zonename(struct ps_prochandle *P, size_t nbytes)
1000 {
1001 	core_info_t *core = P->data;
1002 	char *zonename;
1003 
1004 	if (core->core_zonename != NULL)
1005 		return (0);	/* Already seen */
1006 
1007 	if (nbytes != 0) {
1008 		if ((zonename = malloc(nbytes)) == NULL)
1009 			return (-1);
1010 		if (read(P->asfd, zonename, nbytes) != nbytes) {
1011 			dprintf("Pgrab_core: failed to read NT_ZONENAME\n");
1012 			free(zonename);
1013 			return (-1);
1014 		}
1015 		zonename[nbytes - 1] = '\0';
1016 		core->core_zonename = zonename;
1017 	}
1018 
1019 	return (0);
1020 }
1021 
1022 static int
note_auxv(struct ps_prochandle * P,size_t nbytes)1023 note_auxv(struct ps_prochandle *P, size_t nbytes)
1024 {
1025 	size_t n, i;
1026 
1027 #ifdef _LP64
1028 	core_info_t *core = P->data;
1029 
1030 	if (core->core_dmodel == PR_MODEL_ILP32) {
1031 		auxv32_t *a32;
1032 
1033 		n = nbytes / sizeof (auxv32_t);
1034 		nbytes = n * sizeof (auxv32_t);
1035 		a32 = alloca(nbytes);
1036 
1037 		if (read(P->asfd, a32, nbytes) != nbytes) {
1038 			dprintf("Pgrab_core: failed to read NT_AUXV\n");
1039 			return (-1);
1040 		}
1041 
1042 		if ((P->auxv = malloc(sizeof (auxv_t) * (n + 1))) == NULL)
1043 			return (-1);
1044 
1045 		for (i = 0; i < n; i++)
1046 			auxv_32_to_n(&a32[i], &P->auxv[i]);
1047 
1048 	} else {
1049 #endif
1050 		n = nbytes / sizeof (auxv_t);
1051 		nbytes = n * sizeof (auxv_t);
1052 
1053 		if ((P->auxv = malloc(nbytes + sizeof (auxv_t))) == NULL)
1054 			return (-1);
1055 
1056 		if (read(P->asfd, P->auxv, nbytes) != nbytes) {
1057 			free(P->auxv);
1058 			P->auxv = NULL;
1059 			return (-1);
1060 		}
1061 #ifdef _LP64
1062 	}
1063 #endif
1064 
1065 	if (_libproc_debug) {
1066 		for (i = 0; i < n; i++) {
1067 			dprintf("P->auxv[%lu] = ( %d, 0x%lx )\n", (ulong_t)i,
1068 			    P->auxv[i].a_type, P->auxv[i].a_un.a_val);
1069 		}
1070 	}
1071 
1072 	/*
1073 	 * Defensive coding for loops which depend upon the auxv array being
1074 	 * terminated by an AT_NULL element; in each case, we've allocated
1075 	 * P->auxv to have an additional element which we force to be AT_NULL.
1076 	 */
1077 	P->auxv[n].a_type = AT_NULL;
1078 	P->auxv[n].a_un.a_val = 0L;
1079 	P->nauxv = (int)n;
1080 
1081 	return (0);
1082 }
1083 
1084 /*
1085  * The xregs are not a fixed size on all architectures (notably x86) and in
1086  * general the prxregset_t has become opaque to deal with this. This means that
1087  * validating the note itself can be a little more challenging. Especially as
1088  * this can change across time. In this case we require that our consumers
1089  * perform this validation.
1090  */
1091 static int
note_xreg(struct ps_prochandle * P,size_t nbytes)1092 note_xreg(struct ps_prochandle *P, size_t nbytes)
1093 {
1094 	core_info_t *core = P->data;
1095 	lwp_info_t *lwp = core->core_lwp;
1096 	prxregset_t *xregs;
1097 	ssize_t sret;
1098 
1099 	if (lwp == NULL || lwp->lwp_xregs != NULL)
1100 		return (0);	/* No lwp yet, already seen, or bad size */
1101 
1102 	if ((xregs = malloc(nbytes)) == NULL)
1103 		return (-1);
1104 
1105 	sret = read(P->asfd, xregs, nbytes);
1106 	if (sret < 0 || (size_t)sret != nbytes) {
1107 		dprintf("Pgrab_core: failed to read NT_PRXREG\n");
1108 		free(xregs);
1109 		return (-1);
1110 	}
1111 
1112 	lwp->lwp_xregs = xregs;
1113 	lwp->lwp_xregsize = nbytes;
1114 	return (0);
1115 }
1116 
1117 #ifdef __sparc
1118 static int
note_gwindows(struct ps_prochandle * P,size_t nbytes)1119 note_gwindows(struct ps_prochandle *P, size_t nbytes)
1120 {
1121 	core_info_t *core = P->data;
1122 	lwp_info_t *lwp = core->core_lwp;
1123 
1124 	if (lwp == NULL || lwp->lwp_gwins != NULL || nbytes == 0)
1125 		return (0);	/* No lwp yet or already seen or no data */
1126 
1127 	if ((lwp->lwp_gwins = malloc(sizeof (gwindows_t))) == NULL)
1128 		return (-1);
1129 
1130 	/*
1131 	 * Since the amount of gwindows data varies with how many windows were
1132 	 * actually saved, we just read up to the minimum of the note size
1133 	 * and the size of the gwindows_t type.  It doesn't matter if the read
1134 	 * fails since we have to zero out gwindows first anyway.
1135 	 */
1136 #ifdef _LP64
1137 	if (core->core_dmodel == PR_MODEL_ILP32) {
1138 		gwindows32_t g32;
1139 
1140 		(void) memset(&g32, 0, sizeof (g32));
1141 		(void) read(P->asfd, &g32, MIN(nbytes, sizeof (g32)));
1142 		gwindows_32_to_n(&g32, lwp->lwp_gwins);
1143 
1144 	} else {
1145 #endif
1146 		(void) memset(lwp->lwp_gwins, 0, sizeof (gwindows_t));
1147 		(void) read(P->asfd, lwp->lwp_gwins,
1148 		    MIN(nbytes, sizeof (gwindows_t)));
1149 #ifdef _LP64
1150 	}
1151 #endif
1152 	return (0);
1153 }
1154 
1155 #ifdef __sparcv9
1156 static int
note_asrs(struct ps_prochandle * P,size_t nbytes)1157 note_asrs(struct ps_prochandle *P, size_t nbytes)
1158 {
1159 	core_info_t *core = P->data;
1160 	lwp_info_t *lwp = core->core_lwp;
1161 	int64_t *asrs;
1162 
1163 	if (lwp == NULL || lwp->lwp_asrs != NULL || nbytes < sizeof (asrset_t))
1164 		return (0);	/* No lwp yet, already seen, or bad size */
1165 
1166 	if ((asrs = malloc(sizeof (asrset_t))) == NULL)
1167 		return (-1);
1168 
1169 	if (read(P->asfd, asrs, sizeof (asrset_t)) != sizeof (asrset_t)) {
1170 		dprintf("Pgrab_core: failed to read NT_ASRS\n");
1171 		free(asrs);
1172 		return (-1);
1173 	}
1174 
1175 	lwp->lwp_asrs = asrs;
1176 	return (0);
1177 }
1178 #endif	/* __sparcv9 */
1179 #endif	/* __sparc */
1180 
1181 static int
note_spymaster(struct ps_prochandle * P,size_t nbytes)1182 note_spymaster(struct ps_prochandle *P, size_t nbytes)
1183 {
1184 #ifdef _LP64
1185 	core_info_t *core = P->data;
1186 
1187 	if (core->core_dmodel == PR_MODEL_ILP32) {
1188 		psinfo32_t ps32;
1189 
1190 		if (nbytes < sizeof (psinfo32_t) ||
1191 		    read(P->asfd, &ps32, sizeof (ps32)) != sizeof (ps32))
1192 			goto err;
1193 
1194 		psinfo_32_to_n(&ps32, &P->spymaster);
1195 	} else
1196 #endif
1197 	if (nbytes < sizeof (psinfo_t) || read(P->asfd,
1198 	    &P->spymaster, sizeof (psinfo_t)) != sizeof (psinfo_t))
1199 		goto err;
1200 
1201 	dprintf("spymaster pr_fname = <%s>\n", P->psinfo.pr_fname);
1202 	dprintf("spymaster pr_psargs = <%s>\n", P->psinfo.pr_psargs);
1203 	dprintf("spymaster pr_wstat = 0x%x\n", P->psinfo.pr_wstat);
1204 
1205 	return (0);
1206 
1207 err:
1208 	dprintf("Pgrab_core: failed to read NT_SPYMASTER\n");
1209 	return (-1);
1210 }
1211 
1212 static int
note_upanic(struct ps_prochandle * P,size_t nbytes)1213 note_upanic(struct ps_prochandle *P, size_t nbytes)
1214 {
1215 	core_info_t *core = P->data;
1216 	prupanic_t *pru;
1217 
1218 	if (core->core_upanic != NULL)
1219 		return (0);
1220 
1221 	if (sizeof (*pru) != nbytes) {
1222 		dprintf("Pgrab_core: NT_UPANIC changed size."
1223 		    "  Need to handle a version change?\n");
1224 		return (-1);
1225 	}
1226 
1227 	if (nbytes != 0 && ((pru = malloc(nbytes)) != NULL)) {
1228 		if (read(P->asfd, pru, nbytes) != nbytes) {
1229 			dprintf("Pgrab_core: failed to read NT_UPANIC\n");
1230 			free(pru);
1231 			return (-1);
1232 		}
1233 
1234 		core->core_upanic = pru;
1235 	}
1236 
1237 	return (0);
1238 }
1239 
1240 /*ARGSUSED*/
1241 static int
note_notsup(struct ps_prochandle * P,size_t nbytes)1242 note_notsup(struct ps_prochandle *P, size_t nbytes)
1243 {
1244 	dprintf("skipping unsupported note type of size %ld bytes\n",
1245 	    (ulong_t)nbytes);
1246 	return (0);
1247 }
1248 
1249 /*
1250  * Populate a table of function pointers indexed by Note type with our
1251  * functions to process each type of core file note:
1252  */
1253 static int (*nhdlrs[])(struct ps_prochandle *, size_t) = {
1254 	note_notsup,		/*  0	unassigned		*/
1255 #ifdef __x86
1256 	note_linux_prstatus,		/*  1	NT_PRSTATUS (old)	*/
1257 #else
1258 	note_notsup,		/*  1	NT_PRSTATUS (old)	*/
1259 #endif
1260 	note_notsup,		/*  2	NT_PRFPREG (old)	*/
1261 #ifdef __x86
1262 	note_linux_psinfo,		/*  3	NT_PRPSINFO (old)	*/
1263 #else
1264 	note_notsup,		/*  3	NT_PRPSINFO (old)	*/
1265 #endif
1266 	note_xreg,		/*  4	NT_PRXREG		*/
1267 	note_platform,		/*  5	NT_PLATFORM		*/
1268 	note_auxv,		/*  6	NT_AUXV			*/
1269 #ifdef __sparc
1270 	note_gwindows,		/*  7	NT_GWINDOWS		*/
1271 #ifdef __sparcv9
1272 	note_asrs,		/*  8	NT_ASRS			*/
1273 #else
1274 	note_notsup,		/*  8	NT_ASRS			*/
1275 #endif
1276 #else
1277 	note_notsup,		/*  7	NT_GWINDOWS		*/
1278 	note_notsup,		/*  8	NT_ASRS			*/
1279 #endif
1280 #ifdef __x86
1281 	note_ldt,		/*  9	NT_LDT			*/
1282 #else
1283 	note_notsup,		/*  9	NT_LDT			*/
1284 #endif
1285 	note_pstatus,		/* 10	NT_PSTATUS		*/
1286 	note_notsup,		/* 11	unassigned		*/
1287 	note_notsup,		/* 12	unassigned		*/
1288 	note_psinfo,		/* 13	NT_PSINFO		*/
1289 	note_cred,		/* 14	NT_PRCRED		*/
1290 	note_utsname,		/* 15	NT_UTSNAME		*/
1291 	note_lwpstatus,		/* 16	NT_LWPSTATUS		*/
1292 	note_lwpsinfo,		/* 17	NT_LWPSINFO		*/
1293 	note_priv,		/* 18	NT_PRPRIV		*/
1294 	note_priv_info,		/* 19	NT_PRPRIVINFO		*/
1295 	note_content,		/* 20	NT_CONTENT		*/
1296 	note_zonename,		/* 21	NT_ZONENAME		*/
1297 	note_fdinfo,		/* 22	NT_FDINFO		*/
1298 	note_spymaster,		/* 23	NT_SPYMASTER		*/
1299 	note_secflags,		/* 24	NT_SECFLAGS		*/
1300 	note_lwpname,		/* 25	NT_LWPNAME		*/
1301 	note_upanic		/* 26	NT_UPANIC		*/
1302 };
1303 
1304 static void
core_report_mapping(struct ps_prochandle * P,GElf_Phdr * php)1305 core_report_mapping(struct ps_prochandle *P, GElf_Phdr *php)
1306 {
1307 	prkillinfo_t killinfo;
1308 	siginfo_t *si = &killinfo.prk_info;
1309 	char signame[SIG2STR_MAX], sig[64], info[64];
1310 	void *addr = (void *)(uintptr_t)php->p_vaddr;
1311 
1312 	const char *errfmt = "core file data for mapping at %p not saved: %s\n";
1313 	const char *incfmt = "core file incomplete due to %s%s\n";
1314 	const char *msgfmt = "mappings at and above %p are missing\n";
1315 
1316 	if (!(php->p_flags & PF_SUNW_KILLED)) {
1317 		int err = 0;
1318 
1319 		(void) pread64(P->asfd, &err,
1320 		    sizeof (err), (off64_t)php->p_offset);
1321 
1322 		Perror_printf(P, errfmt, addr, strerror(err));
1323 		dprintf(errfmt, addr, strerror(err));
1324 		return;
1325 	}
1326 
1327 	if (!(php->p_flags & PF_SUNW_SIGINFO))
1328 		return;
1329 
1330 	(void) memset(&killinfo, 0, sizeof (killinfo));
1331 
1332 	(void) pread64(P->asfd, &killinfo,
1333 	    sizeof (killinfo), (off64_t)php->p_offset);
1334 
1335 	/*
1336 	 * While there is (or at least should be) only one segment that has
1337 	 * PF_SUNW_SIGINFO set, the signal information there is globally
1338 	 * useful (even if only to those debugging libproc consumers); we hang
1339 	 * the signal information gleaned here off of the ps_prochandle.
1340 	 */
1341 	P->map_missing = php->p_vaddr;
1342 	P->killinfo = killinfo.prk_info;
1343 
1344 	if (sig2str(si->si_signo, signame) == -1) {
1345 		(void) snprintf(sig, sizeof (sig),
1346 		    "<Unknown signal: 0x%x>, ", si->si_signo);
1347 	} else {
1348 		(void) snprintf(sig, sizeof (sig), "SIG%s, ", signame);
1349 	}
1350 
1351 	if (si->si_code == SI_USER || si->si_code == SI_QUEUE) {
1352 		(void) snprintf(info, sizeof (info),
1353 		    "pid=%d uid=%d zone=%d ctid=%d",
1354 		    si->si_pid, si->si_uid, si->si_zoneid, si->si_ctid);
1355 	} else {
1356 		(void) snprintf(info, sizeof (info),
1357 		    "code=%d", si->si_code);
1358 	}
1359 
1360 	Perror_printf(P, incfmt, sig, info);
1361 	Perror_printf(P, msgfmt, addr);
1362 
1363 	dprintf(incfmt, sig, info);
1364 	dprintf(msgfmt, addr);
1365 }
1366 
1367 /*
1368  * Add information on the address space mapping described by the given
1369  * PT_LOAD program header.  We fill in more information on the mapping later.
1370  */
1371 static int
core_add_mapping(struct ps_prochandle * P,GElf_Phdr * php)1372 core_add_mapping(struct ps_prochandle *P, GElf_Phdr *php)
1373 {
1374 	core_info_t *core = P->data;
1375 	prmap_t pmap;
1376 
1377 	dprintf("mapping base %llx filesz %llx memsz %llx offset %llx\n",
1378 	    (u_longlong_t)php->p_vaddr, (u_longlong_t)php->p_filesz,
1379 	    (u_longlong_t)php->p_memsz, (u_longlong_t)php->p_offset);
1380 
1381 	pmap.pr_vaddr = (uintptr_t)php->p_vaddr;
1382 	pmap.pr_size = php->p_memsz;
1383 
1384 	/*
1385 	 * If Pgcore() or elfcore() fail to write a mapping, they will set
1386 	 * PF_SUNW_FAILURE in the Phdr and try to stash away the errno for us.
1387 	 */
1388 	if (php->p_flags & PF_SUNW_FAILURE) {
1389 		core_report_mapping(P, php);
1390 	} else if (php->p_filesz != 0 && php->p_offset >= core->core_size) {
1391 		Perror_printf(P, "core file may be corrupt -- data for mapping "
1392 		    "at %p is missing\n", (void *)(uintptr_t)php->p_vaddr);
1393 		dprintf("core file may be corrupt -- data for mapping "
1394 		    "at %p is missing\n", (void *)(uintptr_t)php->p_vaddr);
1395 	}
1396 
1397 	/*
1398 	 * The mapping name and offset will hopefully be filled in
1399 	 * by the librtld_db agent.  Unfortunately, if it isn't a
1400 	 * shared library mapping, this information is gone forever.
1401 	 */
1402 	pmap.pr_mapname[0] = '\0';
1403 	pmap.pr_offset = 0;
1404 
1405 	pmap.pr_mflags = 0;
1406 	if (php->p_flags & PF_R)
1407 		pmap.pr_mflags |= MA_READ;
1408 	if (php->p_flags & PF_W)
1409 		pmap.pr_mflags |= MA_WRITE;
1410 	if (php->p_flags & PF_X)
1411 		pmap.pr_mflags |= MA_EXEC;
1412 
1413 	if (php->p_filesz == 0)
1414 		pmap.pr_mflags |= MA_RESERVED1;
1415 
1416 	/*
1417 	 * At the time of adding this mapping, we just zero the pagesize.
1418 	 * Once we've processed more of the core file, we'll have the
1419 	 * pagesize from the auxv's AT_PAGESZ element and we can fill this in.
1420 	 */
1421 	pmap.pr_pagesize = 0;
1422 
1423 	/*
1424 	 * Unfortunately whether or not the mapping was a System V
1425 	 * shared memory segment is lost.  We use -1 to mark it as not shm.
1426 	 */
1427 	pmap.pr_shmid = -1;
1428 
1429 	return (Padd_mapping(P, php->p_offset, NULL, &pmap));
1430 }
1431 
1432 /*
1433  * Given a virtual address, name the mapping at that address using the
1434  * specified name, and return the map_info_t pointer.
1435  */
1436 static map_info_t *
core_name_mapping(struct ps_prochandle * P,uintptr_t addr,const char * name)1437 core_name_mapping(struct ps_prochandle *P, uintptr_t addr, const char *name)
1438 {
1439 	map_info_t *mp = Paddr2mptr(P, addr);
1440 
1441 	if (mp != NULL) {
1442 		(void) strncpy(mp->map_pmap.pr_mapname, name, PRMAPSZ);
1443 		mp->map_pmap.pr_mapname[PRMAPSZ - 1] = '\0';
1444 	}
1445 
1446 	return (mp);
1447 }
1448 
1449 /*
1450  * libproc uses libelf for all of its symbol table manipulation. This function
1451  * takes a symbol table and string table from a core file and places them
1452  * in a memory backed elf file.
1453  */
1454 static void
fake_up_symtab(struct ps_prochandle * P,const elf_file_header_t * ehdr,GElf_Shdr * symtab,GElf_Shdr * strtab)1455 fake_up_symtab(struct ps_prochandle *P, const elf_file_header_t *ehdr,
1456     GElf_Shdr *symtab, GElf_Shdr *strtab)
1457 {
1458 	size_t size;
1459 	off64_t off, base;
1460 	map_info_t *mp;
1461 	file_info_t *fp;
1462 	Elf_Scn *scn;
1463 	Elf_Data *data;
1464 
1465 	if (symtab->sh_addr == 0 ||
1466 	    (mp = Paddr2mptr(P, symtab->sh_addr)) == NULL ||
1467 	    (fp = mp->map_file) == NULL) {
1468 		dprintf("fake_up_symtab: invalid section\n");
1469 		return;
1470 	}
1471 
1472 	if (fp->file_symtab.sym_data_pri != NULL) {
1473 		dprintf("Symbol table already loaded (sh_addr 0x%lx)\n",
1474 		    (long)symtab->sh_addr);
1475 		return;
1476 	}
1477 
1478 	if (P->status.pr_dmodel == PR_MODEL_ILP32) {
1479 		struct {
1480 			Elf32_Ehdr ehdr;
1481 			Elf32_Shdr shdr[3];
1482 			char data[1];
1483 		} *b;
1484 
1485 		base = sizeof (b->ehdr) + sizeof (b->shdr);
1486 		size = base + symtab->sh_size + strtab->sh_size;
1487 
1488 		if ((b = calloc(1, size)) == NULL)
1489 			return;
1490 
1491 		(void) memcpy(b->ehdr.e_ident, ehdr->e_ident,
1492 		    sizeof (ehdr->e_ident));
1493 		b->ehdr.e_type = ehdr->e_type;
1494 		b->ehdr.e_machine = ehdr->e_machine;
1495 		b->ehdr.e_version = ehdr->e_version;
1496 		b->ehdr.e_flags = ehdr->e_flags;
1497 		b->ehdr.e_ehsize = sizeof (b->ehdr);
1498 		b->ehdr.e_shoff = sizeof (b->ehdr);
1499 		b->ehdr.e_shentsize = sizeof (b->shdr[0]);
1500 		b->ehdr.e_shnum = 3;
1501 		off = 0;
1502 
1503 		b->shdr[1].sh_size = symtab->sh_size;
1504 		b->shdr[1].sh_type = SHT_SYMTAB;
1505 		b->shdr[1].sh_offset = off + base;
1506 		b->shdr[1].sh_entsize = sizeof (Elf32_Sym);
1507 		b->shdr[1].sh_link = 2;
1508 		b->shdr[1].sh_info =  symtab->sh_info;
1509 		b->shdr[1].sh_addralign = symtab->sh_addralign;
1510 
1511 		if (pread64(P->asfd, &b->data[off], b->shdr[1].sh_size,
1512 		    symtab->sh_offset) != b->shdr[1].sh_size) {
1513 			dprintf("fake_up_symtab: pread of symtab[1] failed\n");
1514 			free(b);
1515 			return;
1516 		}
1517 
1518 		off += b->shdr[1].sh_size;
1519 
1520 		b->shdr[2].sh_flags = SHF_STRINGS;
1521 		b->shdr[2].sh_size = strtab->sh_size;
1522 		b->shdr[2].sh_type = SHT_STRTAB;
1523 		b->shdr[2].sh_offset = off + base;
1524 		b->shdr[2].sh_info =  strtab->sh_info;
1525 		b->shdr[2].sh_addralign = 1;
1526 
1527 		if (pread64(P->asfd, &b->data[off], b->shdr[2].sh_size,
1528 		    strtab->sh_offset) != b->shdr[2].sh_size) {
1529 			dprintf("fake_up_symtab: pread of symtab[2] failed\n");
1530 			free(b);
1531 			return;
1532 		}
1533 
1534 		off += b->shdr[2].sh_size;
1535 
1536 		fp->file_symtab.sym_elf = elf_memory((char *)b, size);
1537 		if (fp->file_symtab.sym_elf == NULL) {
1538 			free(b);
1539 			return;
1540 		}
1541 
1542 		fp->file_symtab.sym_elfmem = b;
1543 #ifdef _LP64
1544 	} else {
1545 		struct {
1546 			Elf64_Ehdr ehdr;
1547 			Elf64_Shdr shdr[3];
1548 			char data[1];
1549 		} *b;
1550 
1551 		base = sizeof (b->ehdr) + sizeof (b->shdr);
1552 		size = base + symtab->sh_size + strtab->sh_size;
1553 
1554 		if ((b = calloc(1, size)) == NULL)
1555 			return;
1556 
1557 		(void) memcpy(b->ehdr.e_ident, ehdr->e_ident,
1558 		    sizeof (ehdr->e_ident));
1559 		b->ehdr.e_type = ehdr->e_type;
1560 		b->ehdr.e_machine = ehdr->e_machine;
1561 		b->ehdr.e_version = ehdr->e_version;
1562 		b->ehdr.e_flags = ehdr->e_flags;
1563 		b->ehdr.e_ehsize = sizeof (b->ehdr);
1564 		b->ehdr.e_shoff = sizeof (b->ehdr);
1565 		b->ehdr.e_shentsize = sizeof (b->shdr[0]);
1566 		b->ehdr.e_shnum = 3;
1567 		off = 0;
1568 
1569 		b->shdr[1].sh_size = symtab->sh_size;
1570 		b->shdr[1].sh_type = SHT_SYMTAB;
1571 		b->shdr[1].sh_offset = off + base;
1572 		b->shdr[1].sh_entsize = sizeof (Elf64_Sym);
1573 		b->shdr[1].sh_link = 2;
1574 		b->shdr[1].sh_info =  symtab->sh_info;
1575 		b->shdr[1].sh_addralign = symtab->sh_addralign;
1576 
1577 		if (pread64(P->asfd, &b->data[off], b->shdr[1].sh_size,
1578 		    symtab->sh_offset) != b->shdr[1].sh_size) {
1579 			free(b);
1580 			return;
1581 		}
1582 
1583 		off += b->shdr[1].sh_size;
1584 
1585 		b->shdr[2].sh_flags = SHF_STRINGS;
1586 		b->shdr[2].sh_size = strtab->sh_size;
1587 		b->shdr[2].sh_type = SHT_STRTAB;
1588 		b->shdr[2].sh_offset = off + base;
1589 		b->shdr[2].sh_info =  strtab->sh_info;
1590 		b->shdr[2].sh_addralign = 1;
1591 
1592 		if (pread64(P->asfd, &b->data[off], b->shdr[2].sh_size,
1593 		    strtab->sh_offset) != b->shdr[2].sh_size) {
1594 			free(b);
1595 			return;
1596 		}
1597 
1598 		off += b->shdr[2].sh_size;
1599 
1600 		fp->file_symtab.sym_elf = elf_memory((char *)b, size);
1601 		if (fp->file_symtab.sym_elf == NULL) {
1602 			free(b);
1603 			return;
1604 		}
1605 
1606 		fp->file_symtab.sym_elfmem = b;
1607 #endif
1608 	}
1609 
1610 	if ((scn = elf_getscn(fp->file_symtab.sym_elf, 1)) == NULL ||
1611 	    (fp->file_symtab.sym_data_pri = elf_getdata(scn, NULL)) == NULL ||
1612 	    (scn = elf_getscn(fp->file_symtab.sym_elf, 2)) == NULL ||
1613 	    (data = elf_getdata(scn, NULL)) == NULL) {
1614 		dprintf("fake_up_symtab: failed to get section data at %p\n",
1615 		    (void *)scn);
1616 		goto err;
1617 	}
1618 
1619 	fp->file_symtab.sym_strs = data->d_buf;
1620 	fp->file_symtab.sym_strsz = data->d_size;
1621 	fp->file_symtab.sym_symn = symtab->sh_size / symtab->sh_entsize;
1622 	fp->file_symtab.sym_hdr_pri = *symtab;
1623 	fp->file_symtab.sym_strhdr = *strtab;
1624 
1625 	optimize_symtab(&fp->file_symtab);
1626 
1627 	return;
1628 err:
1629 	(void) elf_end(fp->file_symtab.sym_elf);
1630 	free(fp->file_symtab.sym_elfmem);
1631 	fp->file_symtab.sym_elf = NULL;
1632 	fp->file_symtab.sym_elfmem = NULL;
1633 }
1634 
1635 static void
core_phdr_to_gelf(const Elf32_Phdr * src,GElf_Phdr * dst)1636 core_phdr_to_gelf(const Elf32_Phdr *src, GElf_Phdr *dst)
1637 {
1638 	dst->p_type = src->p_type;
1639 	dst->p_flags = src->p_flags;
1640 	dst->p_offset = (Elf64_Off)src->p_offset;
1641 	dst->p_vaddr = (Elf64_Addr)src->p_vaddr;
1642 	dst->p_paddr = (Elf64_Addr)src->p_paddr;
1643 	dst->p_filesz = (Elf64_Xword)src->p_filesz;
1644 	dst->p_memsz = (Elf64_Xword)src->p_memsz;
1645 	dst->p_align = (Elf64_Xword)src->p_align;
1646 }
1647 
1648 static void
core_shdr_to_gelf(const Elf32_Shdr * src,GElf_Shdr * dst)1649 core_shdr_to_gelf(const Elf32_Shdr *src, GElf_Shdr *dst)
1650 {
1651 	dst->sh_name = src->sh_name;
1652 	dst->sh_type = src->sh_type;
1653 	dst->sh_flags = (Elf64_Xword)src->sh_flags;
1654 	dst->sh_addr = (Elf64_Addr)src->sh_addr;
1655 	dst->sh_offset = (Elf64_Off)src->sh_offset;
1656 	dst->sh_size = (Elf64_Xword)src->sh_size;
1657 	dst->sh_link = src->sh_link;
1658 	dst->sh_info = src->sh_info;
1659 	dst->sh_addralign = (Elf64_Xword)src->sh_addralign;
1660 	dst->sh_entsize = (Elf64_Xword)src->sh_entsize;
1661 }
1662 
1663 /*
1664  * Perform elf_begin on efp->e_fd and verify the ELF file's type and class.
1665  */
1666 static int
core_elf_fdopen(elf_file_t * efp,GElf_Half type,int * perr)1667 core_elf_fdopen(elf_file_t *efp, GElf_Half type, int *perr)
1668 {
1669 #ifdef _BIG_ENDIAN
1670 	uchar_t order = ELFDATA2MSB;
1671 #else
1672 	uchar_t order = ELFDATA2LSB;
1673 #endif
1674 	Elf32_Ehdr e32;
1675 	int is_noelf = -1;
1676 	int isa_err = 0;
1677 
1678 	/*
1679 	 * Because 32-bit libelf cannot deal with large files, we need to read,
1680 	 * check, and convert the file header manually in case type == ET_CORE.
1681 	 */
1682 	if (pread64(efp->e_fd, &e32, sizeof (e32), 0) != sizeof (e32)) {
1683 		if (perr != NULL)
1684 			*perr = G_FORMAT;
1685 		goto err;
1686 	}
1687 	if ((is_noelf = memcmp(&e32.e_ident[EI_MAG0], ELFMAG, SELFMAG)) != 0 ||
1688 	    e32.e_type != type || (isa_err = (e32.e_ident[EI_DATA] != order)) ||
1689 	    e32.e_version != EV_CURRENT) {
1690 		if (perr != NULL) {
1691 			if (is_noelf == 0 && isa_err) {
1692 				*perr = G_ISAINVAL;
1693 			} else {
1694 				*perr = G_FORMAT;
1695 			}
1696 		}
1697 		goto err;
1698 	}
1699 
1700 	/*
1701 	 * If the file is 64-bit and we are 32-bit, fail with G_LP64.  If the
1702 	 * file is 64-bit and we are 64-bit, re-read the header as a Elf64_Ehdr,
1703 	 * and convert it to a elf_file_header_t.  Otherwise, the file is
1704 	 * 32-bit, so convert e32 to a elf_file_header_t.
1705 	 */
1706 	if (e32.e_ident[EI_CLASS] == ELFCLASS64) {
1707 #ifdef _LP64
1708 		Elf64_Ehdr e64;
1709 
1710 		if (pread64(efp->e_fd, &e64, sizeof (e64), 0) != sizeof (e64)) {
1711 			if (perr != NULL)
1712 				*perr = G_FORMAT;
1713 			goto err;
1714 		}
1715 
1716 		(void) memcpy(efp->e_hdr.e_ident, e64.e_ident, EI_NIDENT);
1717 		efp->e_hdr.e_type = e64.e_type;
1718 		efp->e_hdr.e_machine = e64.e_machine;
1719 		efp->e_hdr.e_version = e64.e_version;
1720 		efp->e_hdr.e_entry = e64.e_entry;
1721 		efp->e_hdr.e_phoff = e64.e_phoff;
1722 		efp->e_hdr.e_shoff = e64.e_shoff;
1723 		efp->e_hdr.e_flags = e64.e_flags;
1724 		efp->e_hdr.e_ehsize = e64.e_ehsize;
1725 		efp->e_hdr.e_phentsize = e64.e_phentsize;
1726 		efp->e_hdr.e_phnum = (Elf64_Word)e64.e_phnum;
1727 		efp->e_hdr.e_shentsize = e64.e_shentsize;
1728 		efp->e_hdr.e_shnum = (Elf64_Word)e64.e_shnum;
1729 		efp->e_hdr.e_shstrndx = (Elf64_Word)e64.e_shstrndx;
1730 #else	/* _LP64 */
1731 		if (perr != NULL)
1732 			*perr = G_LP64;
1733 		goto err;
1734 #endif	/* _LP64 */
1735 	} else {
1736 		(void) memcpy(efp->e_hdr.e_ident, e32.e_ident, EI_NIDENT);
1737 		efp->e_hdr.e_type = e32.e_type;
1738 		efp->e_hdr.e_machine = e32.e_machine;
1739 		efp->e_hdr.e_version = e32.e_version;
1740 		efp->e_hdr.e_entry = (Elf64_Addr)e32.e_entry;
1741 		efp->e_hdr.e_phoff = (Elf64_Off)e32.e_phoff;
1742 		efp->e_hdr.e_shoff = (Elf64_Off)e32.e_shoff;
1743 		efp->e_hdr.e_flags = e32.e_flags;
1744 		efp->e_hdr.e_ehsize = e32.e_ehsize;
1745 		efp->e_hdr.e_phentsize = e32.e_phentsize;
1746 		efp->e_hdr.e_phnum = (Elf64_Word)e32.e_phnum;
1747 		efp->e_hdr.e_shentsize = e32.e_shentsize;
1748 		efp->e_hdr.e_shnum = (Elf64_Word)e32.e_shnum;
1749 		efp->e_hdr.e_shstrndx = (Elf64_Word)e32.e_shstrndx;
1750 	}
1751 
1752 	/*
1753 	 * If the number of section headers or program headers or the section
1754 	 * header string table index would overflow their respective fields
1755 	 * in the ELF header, they're stored in the section header at index
1756 	 * zero. To simplify use elsewhere, we look for those sentinel values
1757 	 * here.
1758 	 */
1759 	if ((efp->e_hdr.e_shnum == 0 && efp->e_hdr.e_shoff != 0) ||
1760 	    efp->e_hdr.e_shstrndx == SHN_XINDEX ||
1761 	    efp->e_hdr.e_phnum == PN_XNUM) {
1762 		GElf_Shdr shdr;
1763 
1764 		dprintf("extended ELF header\n");
1765 
1766 		if (efp->e_hdr.e_shoff == 0) {
1767 			if (perr != NULL)
1768 				*perr = G_FORMAT;
1769 			goto err;
1770 		}
1771 
1772 		if (efp->e_hdr.e_ident[EI_CLASS] == ELFCLASS32) {
1773 			Elf32_Shdr shdr32;
1774 
1775 			if (pread64(efp->e_fd, &shdr32, sizeof (shdr32),
1776 			    efp->e_hdr.e_shoff) != sizeof (shdr32)) {
1777 				if (perr != NULL)
1778 					*perr = G_FORMAT;
1779 				goto err;
1780 			}
1781 
1782 			core_shdr_to_gelf(&shdr32, &shdr);
1783 		} else {
1784 			if (pread64(efp->e_fd, &shdr, sizeof (shdr),
1785 			    efp->e_hdr.e_shoff) != sizeof (shdr)) {
1786 				if (perr != NULL)
1787 					*perr = G_FORMAT;
1788 				goto err;
1789 			}
1790 		}
1791 
1792 		if (efp->e_hdr.e_shnum == 0) {
1793 			efp->e_hdr.e_shnum = shdr.sh_size;
1794 			dprintf("section header count %lu\n",
1795 			    (ulong_t)shdr.sh_size);
1796 		}
1797 
1798 		if (efp->e_hdr.e_shstrndx == SHN_XINDEX) {
1799 			efp->e_hdr.e_shstrndx = shdr.sh_link;
1800 			dprintf("section string index %u\n", shdr.sh_link);
1801 		}
1802 
1803 		if (efp->e_hdr.e_phnum == PN_XNUM && shdr.sh_info != 0) {
1804 			efp->e_hdr.e_phnum = shdr.sh_info;
1805 			dprintf("program header count %u\n", shdr.sh_info);
1806 		}
1807 
1808 	} else if (efp->e_hdr.e_phoff != 0) {
1809 		GElf_Phdr phdr;
1810 		uint64_t phnum;
1811 
1812 		/*
1813 		 * It's possible this core file came from a system that
1814 		 * accidentally truncated the e_phnum field without correctly
1815 		 * using the extended format in the section header at index
1816 		 * zero. We try to detect and correct that specific type of
1817 		 * corruption by using the knowledge that the core dump
1818 		 * routines usually place the data referenced by the first
1819 		 * program header immediately after the last header element.
1820 		 */
1821 		if (efp->e_hdr.e_ident[EI_CLASS] == ELFCLASS32) {
1822 			Elf32_Phdr phdr32;
1823 
1824 			if (pread64(efp->e_fd, &phdr32, sizeof (phdr32),
1825 			    efp->e_hdr.e_phoff) != sizeof (phdr32)) {
1826 				if (perr != NULL)
1827 					*perr = G_FORMAT;
1828 				goto err;
1829 			}
1830 
1831 			core_phdr_to_gelf(&phdr32, &phdr);
1832 		} else {
1833 			if (pread64(efp->e_fd, &phdr, sizeof (phdr),
1834 			    efp->e_hdr.e_phoff) != sizeof (phdr)) {
1835 				if (perr != NULL)
1836 					*perr = G_FORMAT;
1837 				goto err;
1838 			}
1839 		}
1840 
1841 		phnum = phdr.p_offset - efp->e_hdr.e_ehsize -
1842 		    (uint64_t)efp->e_hdr.e_shnum * efp->e_hdr.e_shentsize;
1843 		phnum /= efp->e_hdr.e_phentsize;
1844 
1845 		if (phdr.p_offset != 0 && phnum != efp->e_hdr.e_phnum) {
1846 			dprintf("suspicious program header count %u %u\n",
1847 			    (uint_t)phnum, efp->e_hdr.e_phnum);
1848 
1849 			/*
1850 			 * If the new program header count we computed doesn't
1851 			 * jive with count in the ELF header, we'll use the
1852 			 * data that's there and hope for the best.
1853 			 *
1854 			 * If it does, it's also possible that the section
1855 			 * header offset is incorrect; we'll check that and
1856 			 * possibly try to fix it.
1857 			 */
1858 			if (phnum <= INT_MAX &&
1859 			    (uint16_t)phnum == efp->e_hdr.e_phnum) {
1860 
1861 				if (efp->e_hdr.e_shoff == efp->e_hdr.e_phoff +
1862 				    efp->e_hdr.e_phentsize *
1863 				    (uint_t)efp->e_hdr.e_phnum) {
1864 					efp->e_hdr.e_shoff =
1865 					    efp->e_hdr.e_phoff +
1866 					    efp->e_hdr.e_phentsize * phnum;
1867 				}
1868 
1869 				efp->e_hdr.e_phnum = (Elf64_Word)phnum;
1870 				dprintf("using new program header count\n");
1871 			} else {
1872 				dprintf("inconsistent program header count\n");
1873 			}
1874 		}
1875 	}
1876 
1877 	/*
1878 	 * The libelf implementation was never ported to be large-file aware.
1879 	 * This is typically not a problem for your average executable or
1880 	 * shared library, but a large 32-bit core file can exceed 2GB in size.
1881 	 * So if type is ET_CORE, we don't bother doing elf_begin; the code
1882 	 * in Pfgrab_core() below will do its own i/o and struct conversion.
1883 	 */
1884 
1885 	if (type == ET_CORE) {
1886 		efp->e_elf = NULL;
1887 		return (0);
1888 	}
1889 
1890 	if ((efp->e_elf = elf_begin(efp->e_fd, ELF_C_READ, NULL)) == NULL) {
1891 		if (perr != NULL)
1892 			*perr = G_ELF;
1893 		goto err;
1894 	}
1895 
1896 	return (0);
1897 
1898 err:
1899 	efp->e_elf = NULL;
1900 	return (-1);
1901 }
1902 
1903 /*
1904  * Open the specified file and then do a core_elf_fdopen on it.
1905  */
1906 static int
core_elf_open(elf_file_t * efp,const char * path,GElf_Half type,int * perr)1907 core_elf_open(elf_file_t *efp, const char *path, GElf_Half type, int *perr)
1908 {
1909 	(void) memset(efp, 0, sizeof (elf_file_t));
1910 
1911 	if ((efp->e_fd = open64(path, O_RDONLY)) >= 0) {
1912 		if (core_elf_fdopen(efp, type, perr) == 0)
1913 			return (0);
1914 
1915 		(void) close(efp->e_fd);
1916 		efp->e_fd = -1;
1917 	}
1918 
1919 	return (-1);
1920 }
1921 
1922 /*
1923  * Close the ELF handle and file descriptor.
1924  */
1925 static void
core_elf_close(elf_file_t * efp)1926 core_elf_close(elf_file_t *efp)
1927 {
1928 	if (efp->e_elf != NULL) {
1929 		(void) elf_end(efp->e_elf);
1930 		efp->e_elf = NULL;
1931 	}
1932 
1933 	if (efp->e_fd != -1) {
1934 		(void) close(efp->e_fd);
1935 		efp->e_fd = -1;
1936 	}
1937 }
1938 
1939 /*
1940  * Given an ELF file for a statically linked executable, locate the likely
1941  * primary text section and fill in rl_base with its virtual address.
1942  */
1943 static map_info_t *
core_find_text(struct ps_prochandle * P,Elf * elf,rd_loadobj_t * rlp)1944 core_find_text(struct ps_prochandle *P, Elf *elf, rd_loadobj_t *rlp)
1945 {
1946 	GElf_Phdr phdr;
1947 	uint_t i;
1948 	size_t nphdrs;
1949 
1950 	if (elf_getphdrnum(elf, &nphdrs) == -1)
1951 		return (NULL);
1952 
1953 	for (i = 0; i < nphdrs; i++) {
1954 		if (gelf_getphdr(elf, i, &phdr) != NULL &&
1955 		    phdr.p_type == PT_LOAD && (phdr.p_flags & PF_X)) {
1956 			rlp->rl_base = phdr.p_vaddr;
1957 			return (Paddr2mptr(P, rlp->rl_base));
1958 		}
1959 	}
1960 
1961 	return (NULL);
1962 }
1963 
1964 /*
1965  * Given an ELF file and the librtld_db structure corresponding to its primary
1966  * text mapping, deduce where its data segment was loaded and fill in
1967  * rl_data_base and prmap_t.pr_offset accordingly.
1968  */
1969 static map_info_t *
core_find_data(struct ps_prochandle * P,Elf * elf,rd_loadobj_t * rlp)1970 core_find_data(struct ps_prochandle *P, Elf *elf, rd_loadobj_t *rlp)
1971 {
1972 	GElf_Ehdr ehdr;
1973 	GElf_Phdr phdr;
1974 	map_info_t *mp;
1975 	uint_t i, pagemask;
1976 	size_t nphdrs;
1977 
1978 	rlp->rl_data_base = (uintptr_t)NULL;
1979 
1980 	/*
1981 	 * Find the first loadable, writeable Phdr and compute rl_data_base
1982 	 * as the virtual address at which is was loaded.
1983 	 */
1984 	if (gelf_getehdr(elf, &ehdr) == NULL ||
1985 	    elf_getphdrnum(elf, &nphdrs) == -1)
1986 		return (NULL);
1987 
1988 	for (i = 0; i < nphdrs; i++) {
1989 		if (gelf_getphdr(elf, i, &phdr) != NULL &&
1990 		    phdr.p_type == PT_LOAD && (phdr.p_flags & PF_W)) {
1991 			rlp->rl_data_base = phdr.p_vaddr;
1992 			if (ehdr.e_type == ET_DYN)
1993 				rlp->rl_data_base += rlp->rl_base;
1994 			break;
1995 		}
1996 	}
1997 
1998 	/*
1999 	 * If we didn't find an appropriate phdr or if the address we
2000 	 * computed has no mapping, return NULL.
2001 	 */
2002 	if (rlp->rl_data_base == (uintptr_t)NULL ||
2003 	    (mp = Paddr2mptr(P, rlp->rl_data_base)) == NULL)
2004 		return (NULL);
2005 
2006 	/*
2007 	 * It wouldn't be procfs-related code if we didn't make use of
2008 	 * unclean knowledge of segvn, even in userland ... the prmap_t's
2009 	 * pr_offset field will be the segvn offset from mmap(2)ing the
2010 	 * data section, which will be the file offset & PAGEMASK.
2011 	 */
2012 	pagemask = ~(mp->map_pmap.pr_pagesize - 1);
2013 	mp->map_pmap.pr_offset = phdr.p_offset & pagemask;
2014 
2015 	return (mp);
2016 }
2017 
2018 /*
2019  * Librtld_db agent callback for iterating over load object mappings.
2020  * For each load object, we allocate a new file_info_t, perform naming,
2021  * and attempt to construct a symbol table for the load object.
2022  */
2023 static int
core_iter_mapping(const rd_loadobj_t * rlp,struct ps_prochandle * P)2024 core_iter_mapping(const rd_loadobj_t *rlp, struct ps_prochandle *P)
2025 {
2026 	core_info_t *core = P->data;
2027 	char lname[PATH_MAX], buf[PATH_MAX];
2028 	file_info_t *fp;
2029 	map_info_t *mp;
2030 
2031 	if (Pread_string(P, lname, PATH_MAX, (off_t)rlp->rl_nameaddr) <= 0) {
2032 		dprintf("failed to read name %p\n", (void *)rlp->rl_nameaddr);
2033 		return (1); /* Keep going; forget this if we can't get a name */
2034 	}
2035 
2036 	dprintf("rd_loadobj name = \"%s\" rl_base = %p\n",
2037 	    lname, (void *)rlp->rl_base);
2038 
2039 	if ((mp = Paddr2mptr(P, rlp->rl_base)) == NULL) {
2040 		dprintf("no mapping for %p\n", (void *)rlp->rl_base);
2041 		return (1); /* No mapping; advance to next mapping */
2042 	}
2043 
2044 	/*
2045 	 * Create a new file_info_t for this mapping, and therefore for
2046 	 * this load object.
2047 	 *
2048 	 * If there's an ELF header at the beginning of this mapping,
2049 	 * file_info_new() will try to use its section headers to
2050 	 * identify any other mappings that belong to this load object.
2051 	 */
2052 	if ((fp = mp->map_file) == NULL &&
2053 	    (fp = file_info_new(P, mp)) == NULL) {
2054 		core->core_errno = errno;
2055 		dprintf("failed to malloc mapping data\n");
2056 		return (0); /* Abort */
2057 	}
2058 	fp->file_map = mp;
2059 
2060 	/* Create a local copy of the load object representation */
2061 	if ((fp->file_lo = calloc(1, sizeof (rd_loadobj_t))) == NULL) {
2062 		core->core_errno = errno;
2063 		dprintf("failed to malloc mapping data\n");
2064 		return (0); /* Abort */
2065 	}
2066 	*fp->file_lo = *rlp;
2067 
2068 	if (lname[0] != '\0') {
2069 		/*
2070 		 * Naming dance part 1: if we got a name from librtld_db, then
2071 		 * copy this name to the prmap_t if it is unnamed.  If the
2072 		 * file_info_t is unnamed, name it after the lname.
2073 		 */
2074 		if (mp->map_pmap.pr_mapname[0] == '\0') {
2075 			(void) strncpy(mp->map_pmap.pr_mapname, lname, PRMAPSZ);
2076 			mp->map_pmap.pr_mapname[PRMAPSZ - 1] = '\0';
2077 		}
2078 
2079 		if (fp->file_lname == NULL)
2080 			fp->file_lname = strdup(lname);
2081 
2082 	} else if (fp->file_lname == NULL &&
2083 	    mp->map_pmap.pr_mapname[0] != '\0') {
2084 		/*
2085 		 * Naming dance part 2: if the mapping is named and the
2086 		 * file_info_t is not, name the file after the mapping.
2087 		 */
2088 		fp->file_lname = strdup(mp->map_pmap.pr_mapname);
2089 	}
2090 
2091 	if ((fp->file_rname == NULL) &&
2092 	    (Pfindmap(P, mp, buf, sizeof (buf)) != NULL))
2093 		fp->file_rname = strdup(buf);
2094 
2095 	if (fp->file_lname != NULL)
2096 		fp->file_lbase = basename(fp->file_lname);
2097 	if (fp->file_rname != NULL)
2098 		fp->file_rbase = basename(fp->file_rname);
2099 
2100 	/* Associate the file and the mapping. */
2101 	(void) strncpy(fp->file_pname, mp->map_pmap.pr_mapname, PRMAPSZ);
2102 	fp->file_pname[PRMAPSZ - 1] = '\0';
2103 
2104 	/*
2105 	 * If no section headers were available then we'll have to
2106 	 * identify this load object's other mappings with what we've
2107 	 * got: the start and end of the object's corresponding
2108 	 * address space.
2109 	 */
2110 	if (fp->file_saddrs == NULL) {
2111 		for (mp = fp->file_map + 1; mp < P->mappings + P->map_count &&
2112 		    mp->map_pmap.pr_vaddr < rlp->rl_bend; mp++) {
2113 
2114 			if (mp->map_file == NULL) {
2115 				dprintf("core_iter_mapping %s: associating "
2116 				    "segment at %p\n",
2117 				    fp->file_pname,
2118 				    (void *)mp->map_pmap.pr_vaddr);
2119 				mp->map_file = fp;
2120 				fp->file_ref++;
2121 			} else {
2122 				dprintf("core_iter_mapping %s: segment at "
2123 				    "%p already associated with %s\n",
2124 				    fp->file_pname,
2125 				    (void *)mp->map_pmap.pr_vaddr,
2126 				    (mp == fp->file_map ? "this file" :
2127 				    mp->map_file->file_pname));
2128 			}
2129 		}
2130 	}
2131 
2132 	/* Ensure that all this file's mappings are named. */
2133 	for (mp = fp->file_map; mp < P->mappings + P->map_count &&
2134 	    mp->map_file == fp; mp++) {
2135 		if (mp->map_pmap.pr_mapname[0] == '\0' &&
2136 		    !(mp->map_pmap.pr_mflags & MA_BREAK)) {
2137 			(void) strncpy(mp->map_pmap.pr_mapname, fp->file_pname,
2138 			    PRMAPSZ);
2139 			mp->map_pmap.pr_mapname[PRMAPSZ - 1] = '\0';
2140 		}
2141 	}
2142 
2143 	/* Attempt to build a symbol table for this file. */
2144 	Pbuild_file_symtab(P, fp);
2145 	if (fp->file_elf == NULL)
2146 		dprintf("core_iter_mapping: no symtab for %s\n",
2147 		    fp->file_pname);
2148 
2149 	/* Locate the start of a data segment associated with this file. */
2150 	if ((mp = core_find_data(P, fp->file_elf, fp->file_lo)) != NULL) {
2151 		dprintf("found data for %s at %p (pr_offset 0x%llx)\n",
2152 		    fp->file_pname, (void *)fp->file_lo->rl_data_base,
2153 		    mp->map_pmap.pr_offset);
2154 	} else {
2155 		dprintf("core_iter_mapping: no data found for %s\n",
2156 		    fp->file_pname);
2157 	}
2158 
2159 	return (1); /* Advance to next mapping */
2160 }
2161 
2162 /*
2163  * Callback function for Pfindexec().  In order to confirm a given pathname,
2164  * we verify that we can open it as an ELF file of type ET_EXEC or ET_DYN.
2165  */
2166 static int
core_exec_open(const char * path,void * efp)2167 core_exec_open(const char *path, void *efp)
2168 {
2169 	if (core_elf_open(efp, path, ET_EXEC, NULL) == 0)
2170 		return (1);
2171 	if (core_elf_open(efp, path, ET_DYN, NULL) == 0)
2172 		return (1);
2173 	return (0);
2174 }
2175 
2176 /*
2177  * Attempt to load any section headers found in the core file.  If present,
2178  * this will refer to non-loadable data added to the core file by the kernel
2179  * based on coreadm(8) settings, including CTF data and the symbol table.
2180  */
2181 static void
core_load_shdrs(struct ps_prochandle * P,elf_file_t * efp)2182 core_load_shdrs(struct ps_prochandle *P, elf_file_t *efp)
2183 {
2184 	GElf_Shdr *shp, *shdrs = NULL;
2185 	char *shstrtab = NULL;
2186 	ulong_t shstrtabsz;
2187 	const char *name;
2188 	map_info_t *mp;
2189 
2190 	size_t nbytes;
2191 	void *buf;
2192 	int i;
2193 
2194 	if (efp->e_hdr.e_shstrndx >= efp->e_hdr.e_shnum) {
2195 		dprintf("corrupt shstrndx (%u) exceeds shnum (%u)\n",
2196 		    efp->e_hdr.e_shstrndx, efp->e_hdr.e_shnum);
2197 		return;
2198 	}
2199 
2200 	/*
2201 	 * Read the section header table from the core file and then iterate
2202 	 * over the section headers, converting each to a GElf_Shdr.
2203 	 */
2204 	if ((shdrs = malloc(efp->e_hdr.e_shnum * sizeof (GElf_Shdr))) == NULL) {
2205 		dprintf("failed to malloc %u section headers: %s\n",
2206 		    (uint_t)efp->e_hdr.e_shnum, strerror(errno));
2207 		return;
2208 	}
2209 
2210 	nbytes = efp->e_hdr.e_shnum * efp->e_hdr.e_shentsize;
2211 	if ((buf = malloc(nbytes)) == NULL) {
2212 		dprintf("failed to malloc %d bytes: %s\n", (int)nbytes,
2213 		    strerror(errno));
2214 		free(shdrs);
2215 		goto out;
2216 	}
2217 
2218 	if (pread64(efp->e_fd, buf, nbytes, efp->e_hdr.e_shoff) != nbytes) {
2219 		dprintf("failed to read section headers at off %lld: %s\n",
2220 		    (longlong_t)efp->e_hdr.e_shoff, strerror(errno));
2221 		free(buf);
2222 		goto out;
2223 	}
2224 
2225 	for (i = 0; i < efp->e_hdr.e_shnum; i++) {
2226 		void *p = (uchar_t *)buf + efp->e_hdr.e_shentsize * i;
2227 
2228 		if (efp->e_hdr.e_ident[EI_CLASS] == ELFCLASS32)
2229 			core_shdr_to_gelf(p, &shdrs[i]);
2230 		else
2231 			(void) memcpy(&shdrs[i], p, sizeof (GElf_Shdr));
2232 	}
2233 
2234 	free(buf);
2235 	buf = NULL;
2236 
2237 	/*
2238 	 * Read the .shstrtab section from the core file, terminating it with
2239 	 * an extra \0 so that a corrupt section will not cause us to die.
2240 	 */
2241 	shp = &shdrs[efp->e_hdr.e_shstrndx];
2242 	shstrtabsz = shp->sh_size;
2243 
2244 	if ((shstrtab = malloc(shstrtabsz + 1)) == NULL) {
2245 		dprintf("failed to allocate %lu bytes for shstrtab\n",
2246 		    (ulong_t)shstrtabsz);
2247 		goto out;
2248 	}
2249 
2250 	if (pread64(efp->e_fd, shstrtab, shstrtabsz,
2251 	    shp->sh_offset) != shstrtabsz) {
2252 		dprintf("failed to read %lu bytes of shstrs at off %lld: %s\n",
2253 		    shstrtabsz, (longlong_t)shp->sh_offset, strerror(errno));
2254 		goto out;
2255 	}
2256 
2257 	shstrtab[shstrtabsz] = '\0';
2258 
2259 	/*
2260 	 * Now iterate over each section in the section header table, locating
2261 	 * sections of interest and initializing more of the ps_prochandle.
2262 	 */
2263 	for (i = 0; i < efp->e_hdr.e_shnum; i++) {
2264 		shp = &shdrs[i];
2265 		name = shstrtab + shp->sh_name;
2266 
2267 		if (shp->sh_name >= shstrtabsz) {
2268 			dprintf("skipping section [%d]: corrupt sh_name\n", i);
2269 			continue;
2270 		}
2271 
2272 		if (shp->sh_link >= efp->e_hdr.e_shnum) {
2273 			dprintf("skipping section [%d]: corrupt sh_link\n", i);
2274 			continue;
2275 		}
2276 
2277 		dprintf("found section header %s (sh_addr 0x%llx)\n",
2278 		    name, (u_longlong_t)shp->sh_addr);
2279 
2280 		if (strcmp(name, ".SUNW_ctf") == 0) {
2281 			if ((mp = Paddr2mptr(P, shp->sh_addr)) == NULL) {
2282 				dprintf("no map at addr 0x%llx for %s [%d]\n",
2283 				    (u_longlong_t)shp->sh_addr, name, i);
2284 				continue;
2285 			}
2286 
2287 			if (mp->map_file == NULL ||
2288 			    mp->map_file->file_ctf_buf != NULL) {
2289 				dprintf("no mapping file or duplicate buffer "
2290 				    "for %s [%d]\n", name, i);
2291 				continue;
2292 			}
2293 
2294 			if ((buf = malloc(shp->sh_size)) == NULL ||
2295 			    pread64(efp->e_fd, buf, shp->sh_size,
2296 			    shp->sh_offset) != shp->sh_size) {
2297 				dprintf("skipping section %s [%d]: %s\n",
2298 				    name, i, strerror(errno));
2299 				free(buf);
2300 				continue;
2301 			}
2302 
2303 			mp->map_file->file_ctf_size = shp->sh_size;
2304 			mp->map_file->file_ctf_buf = buf;
2305 
2306 			if (shdrs[shp->sh_link].sh_type == SHT_DYNSYM)
2307 				mp->map_file->file_ctf_dyn = 1;
2308 
2309 		} else if (strcmp(name, ".symtab") == 0) {
2310 			fake_up_symtab(P, &efp->e_hdr,
2311 			    shp, &shdrs[shp->sh_link]);
2312 		}
2313 	}
2314 out:
2315 	free(shstrtab);
2316 	free(shdrs);
2317 }
2318 
2319 /*
2320  * Main engine for core file initialization: given an fd for the core file
2321  * and an optional pathname, construct the ps_prochandle.  The aout_path can
2322  * either be a suggested executable pathname, or a suggested directory to
2323  * use as a possible current working directory.
2324  */
2325 struct ps_prochandle *
Pfgrab_core(int core_fd,const char * aout_path,int * perr)2326 Pfgrab_core(int core_fd, const char *aout_path, int *perr)
2327 {
2328 	struct ps_prochandle *P;
2329 	core_info_t *core_info;
2330 	map_info_t *stk_mp, *brk_mp;
2331 	const char *execname;
2332 	char *interp;
2333 	int i, notes, pagesize;
2334 	uintptr_t addr, base_addr;
2335 	struct stat64 stbuf;
2336 	void *phbuf, *php;
2337 	size_t nbytes;
2338 #ifdef __x86
2339 	boolean_t from_linux = B_FALSE;
2340 #endif
2341 
2342 	elf_file_t aout;
2343 	elf_file_t core;
2344 
2345 	Elf_Scn *scn, *intp_scn = NULL;
2346 	Elf_Data *dp;
2347 
2348 	GElf_Phdr phdr, note_phdr;
2349 	GElf_Shdr shdr;
2350 	GElf_Xword nleft;
2351 
2352 	if (elf_version(EV_CURRENT) == EV_NONE) {
2353 		dprintf("libproc ELF version is more recent than libelf\n");
2354 		*perr = G_ELF;
2355 		return (NULL);
2356 	}
2357 
2358 	aout.e_elf = NULL;
2359 	aout.e_fd = -1;
2360 
2361 	core.e_elf = NULL;
2362 	core.e_fd = core_fd;
2363 
2364 	/*
2365 	 * Allocate and initialize a ps_prochandle structure for the core.
2366 	 * There are several key pieces of initialization here:
2367 	 *
2368 	 * 1. The PS_DEAD state flag marks this prochandle as a core file.
2369 	 *    PS_DEAD also thus prevents all operations which require state
2370 	 *    to be PS_STOP from operating on this handle.
2371 	 *
2372 	 * 2. We keep the core file fd in P->asfd since the core file contains
2373 	 *    the remnants of the process address space.
2374 	 *
2375 	 * 3. We set the P->info_valid bit because all information about the
2376 	 *    core is determined by the end of this function; there is no need
2377 	 *    for proc_update_maps() to reload mappings at any later point.
2378 	 *
2379 	 * 4. The read/write ops vector uses our core_rw() function defined
2380 	 *    above to handle i/o requests.
2381 	 */
2382 	if ((P = malloc(sizeof (struct ps_prochandle))) == NULL) {
2383 		*perr = G_STRANGE;
2384 		return (NULL);
2385 	}
2386 
2387 	(void) memset(P, 0, sizeof (struct ps_prochandle));
2388 	(void) mutex_init(&P->proc_lock, USYNC_THREAD, NULL);
2389 	P->state = PS_DEAD;
2390 	P->pid = (pid_t)-1;
2391 	P->asfd = core.e_fd;
2392 	P->ctlfd = -1;
2393 	P->statfd = -1;
2394 	P->agentctlfd = -1;
2395 	P->agentstatfd = -1;
2396 	P->zoneroot = NULL;
2397 	P->info_valid = 1;
2398 	Pinit_ops(&P->ops, &P_core_ops);
2399 
2400 	Pinitsym(P);
2401 	Pinitfd(P);
2402 
2403 	/*
2404 	 * Fstat and open the core file and make sure it is a valid ELF core.
2405 	 */
2406 	if (fstat64(P->asfd, &stbuf) == -1) {
2407 		*perr = G_STRANGE;
2408 		goto err;
2409 	}
2410 
2411 	if (core_elf_fdopen(&core, ET_CORE, perr) == -1)
2412 		goto err;
2413 
2414 	/*
2415 	 * Allocate and initialize a core_info_t to hang off the ps_prochandle
2416 	 * structure.  We keep all core-specific information in this structure.
2417 	 */
2418 	if ((core_info = calloc(1, sizeof (core_info_t))) == NULL) {
2419 		*perr = G_STRANGE;
2420 		goto err;
2421 	}
2422 
2423 	P->data = core_info;
2424 	list_create(&core_info->core_lwp_head, sizeof (lwp_info_t),
2425 	    offsetof(lwp_info_t, lwp_list));
2426 	core_info->core_size = stbuf.st_size;
2427 	/*
2428 	 * In the days before adjustable core file content, this was the
2429 	 * default core file content. For new core files, this value will
2430 	 * be overwritten by the NT_CONTENT note section.
2431 	 */
2432 	core_info->core_content = CC_CONTENT_STACK | CC_CONTENT_HEAP |
2433 	    CC_CONTENT_DATA | CC_CONTENT_RODATA | CC_CONTENT_ANON |
2434 	    CC_CONTENT_SHANON;
2435 
2436 	switch (core.e_hdr.e_ident[EI_CLASS]) {
2437 	case ELFCLASS32:
2438 		core_info->core_dmodel = PR_MODEL_ILP32;
2439 		break;
2440 	case ELFCLASS64:
2441 		core_info->core_dmodel = PR_MODEL_LP64;
2442 		break;
2443 	default:
2444 		*perr = G_FORMAT;
2445 		goto err;
2446 	}
2447 	core_info->core_osabi = core.e_hdr.e_ident[EI_OSABI];
2448 
2449 	/*
2450 	 * Because the core file may be a large file, we can't use libelf to
2451 	 * read the Phdrs.  We use e_phnum and e_phentsize to simplify things.
2452 	 */
2453 	nbytes = core.e_hdr.e_phnum * core.e_hdr.e_phentsize;
2454 
2455 	if ((phbuf = malloc(nbytes)) == NULL) {
2456 		*perr = G_STRANGE;
2457 		goto err;
2458 	}
2459 
2460 	if (pread64(core_fd, phbuf, nbytes, core.e_hdr.e_phoff) != nbytes) {
2461 		*perr = G_STRANGE;
2462 		free(phbuf);
2463 		goto err;
2464 	}
2465 
2466 	/*
2467 	 * Iterate through the program headers in the core file.
2468 	 * We're interested in two types of Phdrs: PT_NOTE (which
2469 	 * contains a set of saved /proc structures), and PT_LOAD (which
2470 	 * represents a memory mapping from the process's address space).
2471 	 * In the case of PT_NOTE, we're interested in the last PT_NOTE
2472 	 * in the core file; currently the first PT_NOTE (if present)
2473 	 * contains /proc structs in the pre-2.6 unstructured /proc format.
2474 	 */
2475 	for (php = phbuf, notes = 0, i = 0; i < core.e_hdr.e_phnum; i++) {
2476 		if (core.e_hdr.e_ident[EI_CLASS] == ELFCLASS64)
2477 			(void) memcpy(&phdr, php, sizeof (GElf_Phdr));
2478 		else
2479 			core_phdr_to_gelf(php, &phdr);
2480 
2481 		switch (phdr.p_type) {
2482 		case PT_NOTE:
2483 			note_phdr = phdr;
2484 			notes++;
2485 			break;
2486 
2487 		case PT_LOAD:
2488 			if (core_add_mapping(P, &phdr) == -1) {
2489 				*perr = G_STRANGE;
2490 				free(phbuf);
2491 				goto err;
2492 			}
2493 			break;
2494 		default:
2495 			dprintf("Pgrab_core: unknown phdr %d\n", phdr.p_type);
2496 			break;
2497 		}
2498 
2499 		php = (char *)php + core.e_hdr.e_phentsize;
2500 	}
2501 
2502 	free(phbuf);
2503 
2504 	Psort_mappings(P);
2505 
2506 	/*
2507 	 * If we couldn't find anything of type PT_NOTE, or only one PT_NOTE
2508 	 * was present, abort.  The core file is either corrupt or too old.
2509 	 */
2510 	if (notes == 0 || (notes == 1 && core_info->core_osabi ==
2511 	    ELFOSABI_SOLARIS)) {
2512 		*perr = G_NOTE;
2513 		goto err;
2514 	}
2515 
2516 	/*
2517 	 * Advance the seek pointer to the start of the PT_NOTE data
2518 	 */
2519 	if (lseek64(P->asfd, note_phdr.p_offset, SEEK_SET) == (off64_t)-1) {
2520 		dprintf("Pgrab_core: failed to lseek to PT_NOTE data\n");
2521 		*perr = G_STRANGE;
2522 		goto err;
2523 	}
2524 
2525 	/*
2526 	 * Now process the PT_NOTE structures.  Each one is preceded by
2527 	 * an Elf{32/64}_Nhdr structure describing its type and size.
2528 	 *
2529 	 *  +--------+
2530 	 *  | header |
2531 	 *  +--------+
2532 	 *  | name   |
2533 	 *  | ...    |
2534 	 *  +--------+
2535 	 *  | desc   |
2536 	 *  | ...    |
2537 	 *  +--------+
2538 	 */
2539 	for (nleft = note_phdr.p_filesz; nleft > 0; ) {
2540 		Elf64_Nhdr nhdr;
2541 		off64_t off, namesz, descsz;
2542 
2543 		/*
2544 		 * Although <sys/elf.h> defines both Elf32_Nhdr and Elf64_Nhdr
2545 		 * as different types, they are both of the same content and
2546 		 * size, so we don't need to worry about 32/64 conversion here.
2547 		 */
2548 		if (read(P->asfd, &nhdr, sizeof (nhdr)) != sizeof (nhdr)) {
2549 			dprintf("Pgrab_core: failed to read ELF note header\n");
2550 			*perr = G_NOTE;
2551 			goto err;
2552 		}
2553 
2554 		/*
2555 		 * According to the System V ABI, the amount of padding
2556 		 * following the name field should align the description
2557 		 * field on a 4 byte boundary for 32-bit binaries or on an 8
2558 		 * byte boundary for 64-bit binaries. However, this change
2559 		 * was not made correctly during the 64-bit port so all
2560 		 * descriptions can assume only 4-byte alignment. We ignore
2561 		 * the name field and the padding to 4-byte alignment.
2562 		 */
2563 		namesz = P2ROUNDUP((off64_t)nhdr.n_namesz, (off64_t)4);
2564 
2565 		if (lseek64(P->asfd, namesz, SEEK_CUR) == (off64_t)-1) {
2566 			dprintf("failed to seek past name and padding\n");
2567 			*perr = G_STRANGE;
2568 			goto err;
2569 		}
2570 
2571 		dprintf("Note hdr n_type=%u n_namesz=%u n_descsz=%u\n",
2572 		    nhdr.n_type, nhdr.n_namesz, nhdr.n_descsz);
2573 
2574 		off = lseek64(P->asfd, (off64_t)0L, SEEK_CUR);
2575 
2576 		/*
2577 		 * Invoke the note handler function from our table
2578 		 */
2579 		if (nhdr.n_type < sizeof (nhdlrs) / sizeof (nhdlrs[0])) {
2580 			if (nhdlrs[nhdr.n_type](P, nhdr.n_descsz) < 0) {
2581 				dprintf("handler for type %d returned < 0",
2582 				    nhdr.n_type);
2583 				*perr = G_NOTE;
2584 				goto err;
2585 			}
2586 			/*
2587 			 * The presence of either of these notes indicates that
2588 			 * the dump was generated on Linux.
2589 			 */
2590 #ifdef __x86
2591 			if (nhdr.n_type == NT_PRSTATUS ||
2592 			    nhdr.n_type == NT_PRPSINFO)
2593 				from_linux = B_TRUE;
2594 #endif
2595 		} else {
2596 			(void) note_notsup(P, nhdr.n_descsz);
2597 		}
2598 
2599 		/*
2600 		 * Seek past the current note data to the next Elf_Nhdr
2601 		 */
2602 		descsz = P2ROUNDUP((off64_t)nhdr.n_descsz, (off64_t)4);
2603 		if (lseek64(P->asfd, off + descsz, SEEK_SET) == (off64_t)-1) {
2604 			dprintf("Pgrab_core: failed to seek to next nhdr\n");
2605 			*perr = G_STRANGE;
2606 			goto err;
2607 		}
2608 
2609 		/*
2610 		 * Subtract the size of the header and its data from what
2611 		 * we have left to process.
2612 		 */
2613 		nleft -= sizeof (nhdr) + namesz + descsz;
2614 	}
2615 
2616 #ifdef __x86
2617 	if (from_linux) {
2618 		size_t pid;
2619 		lwp_info_t *lwp;
2620 
2621 		P->status.pr_dmodel = core_info->core_dmodel;
2622 
2623 		pid = P->status.pr_pid;
2624 
2625 		for (lwp = list_head(&core_info->core_lwp_head); lwp != NULL;
2626 		    lwp = list_next(&core_info->core_lwp_head, lwp)) {
2627 			dprintf("Linux thread with id %d\n", lwp->lwp_id);
2628 
2629 			/*
2630 			 * In the case we don't have a valid psinfo (i.e. pid is
2631 			 * 0, probably because of gdb creating the core) assume
2632 			 * lowest pid count is the first thread (what if the
2633 			 * next thread wraps the pid around?)
2634 			 */
2635 			if (P->status.pr_pid == 0 &&
2636 			    ((pid == 0 && lwp->lwp_id > 0) ||
2637 			    (lwp->lwp_id < pid))) {
2638 				pid = lwp->lwp_id;
2639 			}
2640 		}
2641 
2642 		if (P->status.pr_pid != pid) {
2643 			dprintf("No valid pid, setting to %ld\n", (ulong_t)pid);
2644 			P->status.pr_pid = pid;
2645 			P->psinfo.pr_pid = pid;
2646 		}
2647 
2648 		/*
2649 		 * Consumers like mdb expect the first thread to actually have
2650 		 * an id of 1, on linux that is actually the pid. Find the the
2651 		 * thread with our process id, and set the id to 1
2652 		 */
2653 		if ((lwp = lwpid2info(P, pid)) == NULL) {
2654 			dprintf("Couldn't find first thread\n");
2655 			*perr = G_STRANGE;
2656 			goto err;
2657 		}
2658 
2659 		dprintf("setting representative thread: %d\n", lwp->lwp_id);
2660 
2661 		lwp->lwp_id = 1;
2662 		lwp->lwp_status.pr_lwpid = 1;
2663 
2664 		/* set representative thread */
2665 		(void) memcpy(&P->status.pr_lwp, &lwp->lwp_status,
2666 		    sizeof (P->status.pr_lwp));
2667 	}
2668 #endif /* __x86 */
2669 
2670 	if (nleft != 0) {
2671 		dprintf("Pgrab_core: note section malformed\n");
2672 		*perr = G_STRANGE;
2673 		goto err;
2674 	}
2675 
2676 	if ((pagesize = Pgetauxval(P, AT_PAGESZ)) == -1) {
2677 		pagesize = getpagesize();
2678 		dprintf("AT_PAGESZ missing; defaulting to %d\n", pagesize);
2679 	}
2680 
2681 	/*
2682 	 * Locate and label the mappings corresponding to the end of the
2683 	 * heap (MA_BREAK) and the base of the stack (MA_STACK).
2684 	 */
2685 	if ((P->status.pr_brkbase != 0 || P->status.pr_brksize != 0) &&
2686 	    (brk_mp = Paddr2mptr(P, P->status.pr_brkbase +
2687 	    P->status.pr_brksize - 1)) != NULL)
2688 		brk_mp->map_pmap.pr_mflags |= MA_BREAK;
2689 	else
2690 		brk_mp = NULL;
2691 
2692 	if ((stk_mp = Paddr2mptr(P, P->status.pr_stkbase)) != NULL)
2693 		stk_mp->map_pmap.pr_mflags |= MA_STACK;
2694 
2695 	/*
2696 	 * At this point, we have enough information to look for the
2697 	 * executable and open it: we have access to the auxv, a psinfo_t,
2698 	 * and the ability to read from mappings provided by the core file.
2699 	 */
2700 	(void) Pfindexec(P, aout_path, core_exec_open, &aout);
2701 	dprintf("P->execname = \"%s\"\n", P->execname ? P->execname : "NULL");
2702 	execname = P->execname ? P->execname : "a.out";
2703 
2704 	/*
2705 	 * Iterate through the sections, looking for the .dynamic and .interp
2706 	 * sections.  If we encounter them, remember their section pointers.
2707 	 */
2708 	for (scn = NULL; (scn = elf_nextscn(aout.e_elf, scn)) != NULL; ) {
2709 		char *sname;
2710 
2711 		if ((gelf_getshdr(scn, &shdr) == NULL) ||
2712 		    (sname = elf_strptr(aout.e_elf, aout.e_hdr.e_shstrndx,
2713 		    (size_t)shdr.sh_name)) == NULL)
2714 			continue;
2715 
2716 		if (strcmp(sname, ".interp") == 0)
2717 			intp_scn = scn;
2718 	}
2719 
2720 	/*
2721 	 * Get the AT_BASE auxv element.  If this is missing (-1), then
2722 	 * we assume this is a statically-linked executable.
2723 	 */
2724 	base_addr = Pgetauxval(P, AT_BASE);
2725 
2726 	/*
2727 	 * In order to get librtld_db initialized, we'll need to identify
2728 	 * and name the mapping corresponding to the run-time linker.  The
2729 	 * AT_BASE auxv element tells us the address where it was mapped,
2730 	 * and the .interp section of the executable tells us its path.
2731 	 * If for some reason that doesn't pan out, just use ld.so.1.
2732 	 */
2733 	if (intp_scn != NULL && (dp = elf_getdata(intp_scn, NULL)) != NULL &&
2734 	    dp->d_size != 0) {
2735 		dprintf(".interp = <%s>\n", (char *)dp->d_buf);
2736 		interp = dp->d_buf;
2737 
2738 	} else if (base_addr != (uintptr_t)-1L) {
2739 		if (core_info->core_dmodel == PR_MODEL_LP64)
2740 			interp = "/usr/lib/64/ld.so.1";
2741 		else
2742 			interp = "/usr/lib/ld.so.1";
2743 
2744 		dprintf(".interp section is missing or could not be read; "
2745 		    "defaulting to %s\n", interp);
2746 	} else
2747 		dprintf("detected statically linked executable\n");
2748 
2749 	/*
2750 	 * If we have an AT_BASE element, name the mapping at that address
2751 	 * using the interpreter pathname.  Name the corresponding data
2752 	 * mapping after the interpreter as well.
2753 	 */
2754 	if (base_addr != (uintptr_t)-1L) {
2755 		elf_file_t intf;
2756 
2757 		P->map_ldso = core_name_mapping(P, base_addr, interp);
2758 
2759 		if (core_elf_open(&intf, interp, ET_DYN, NULL) == 0) {
2760 			rd_loadobj_t rl;
2761 			map_info_t *dmp;
2762 
2763 			rl.rl_base = base_addr;
2764 			dmp = core_find_data(P, intf.e_elf, &rl);
2765 
2766 			if (dmp != NULL) {
2767 				dprintf("renamed data at %p to %s\n",
2768 				    (void *)rl.rl_data_base, interp);
2769 				(void) strncpy(dmp->map_pmap.pr_mapname,
2770 				    interp, PRMAPSZ);
2771 				dmp->map_pmap.pr_mapname[PRMAPSZ - 1] = '\0';
2772 			}
2773 		}
2774 
2775 		core_elf_close(&intf);
2776 	}
2777 
2778 	/*
2779 	 * If we have an AT_ENTRY element, name the mapping at that address
2780 	 * using the special name "a.out" just like /proc does.
2781 	 */
2782 	if ((addr = Pgetauxval(P, AT_ENTRY)) != (uintptr_t)-1L)
2783 		P->map_exec = core_name_mapping(P, addr, "a.out");
2784 
2785 	/*
2786 	 * If we're a statically linked executable (or we're on x86 and looking
2787 	 * at a Linux core dump), then just locate the executable's text and
2788 	 * data and name them after the executable.
2789 	 */
2790 #ifndef __x86
2791 	if (base_addr == (uintptr_t)-1L) {
2792 #else
2793 	if (base_addr == (uintptr_t)-1L || from_linux) {
2794 #endif
2795 		dprintf("looking for text and data: %s\n", execname);
2796 		map_info_t *tmp, *dmp;
2797 		file_info_t *fp;
2798 		rd_loadobj_t rl;
2799 
2800 		if ((tmp = core_find_text(P, aout.e_elf, &rl)) != NULL &&
2801 		    (dmp = core_find_data(P, aout.e_elf, &rl)) != NULL) {
2802 			(void) strncpy(tmp->map_pmap.pr_mapname,
2803 			    execname, PRMAPSZ);
2804 			tmp->map_pmap.pr_mapname[PRMAPSZ - 1] = '\0';
2805 			(void) strncpy(dmp->map_pmap.pr_mapname,
2806 			    execname, PRMAPSZ);
2807 			dmp->map_pmap.pr_mapname[PRMAPSZ - 1] = '\0';
2808 		}
2809 
2810 		if ((P->map_exec = tmp) != NULL &&
2811 		    (fp = malloc(sizeof (file_info_t))) != NULL) {
2812 
2813 			(void) memset(fp, 0, sizeof (file_info_t));
2814 
2815 			list_insert_head(&P->file_head, fp);
2816 			tmp->map_file = fp;
2817 			P->num_files++;
2818 
2819 			fp->file_ref = 1;
2820 			fp->file_fd = -1;
2821 			fp->file_dbgfile = -1;
2822 
2823 			fp->file_lo = malloc(sizeof (rd_loadobj_t));
2824 			fp->file_lname = strdup(execname);
2825 
2826 			if (fp->file_lo)
2827 				*fp->file_lo = rl;
2828 			if (fp->file_lname)
2829 				fp->file_lbase = basename(fp->file_lname);
2830 			if (fp->file_rname)
2831 				fp->file_rbase = basename(fp->file_rname);
2832 
2833 			(void) strcpy(fp->file_pname,
2834 			    P->mappings[0].map_pmap.pr_mapname);
2835 			fp->file_map = tmp;
2836 
2837 			Pbuild_file_symtab(P, fp);
2838 
2839 			if (dmp != NULL) {
2840 				dmp->map_file = fp;
2841 				fp->file_ref++;
2842 			}
2843 		}
2844 	}
2845 
2846 	core_elf_close(&aout);
2847 
2848 	/*
2849 	 * We now have enough information to initialize librtld_db.
2850 	 * After it warms up, we can iterate through the load object chain
2851 	 * in the core, which will allow us to construct the file info
2852 	 * we need to provide symbol information for the other shared
2853 	 * libraries, and also to fill in the missing mapping names.
2854 	 */
2855 	rd_log(_libproc_debug);
2856 
2857 	if ((P->rap = rd_new(P)) != NULL) {
2858 		(void) rd_loadobj_iter(P->rap, (rl_iter_f *)
2859 		    core_iter_mapping, P);
2860 
2861 		if (core_info->core_errno != 0) {
2862 			errno = core_info->core_errno;
2863 			*perr = G_STRANGE;
2864 			goto err;
2865 		}
2866 	} else
2867 		dprintf("failed to initialize rtld_db agent\n");
2868 
2869 	/*
2870 	 * If there are sections, load them and process the data from any
2871 	 * sections that we can use to annotate the file_info_t's.
2872 	 */
2873 	core_load_shdrs(P, &core);
2874 
2875 	/*
2876 	 * If we previously located a stack or break mapping, and they are
2877 	 * still anonymous, we now assume that they were MAP_ANON mappings.
2878 	 * If brk_mp turns out to now have a name, then the heap is still
2879 	 * sitting at the end of the executable's data+bss mapping: remove
2880 	 * the previous MA_BREAK setting to be consistent with /proc.
2881 	 */
2882 	if (stk_mp != NULL && stk_mp->map_pmap.pr_mapname[0] == '\0')
2883 		stk_mp->map_pmap.pr_mflags |= MA_ANON;
2884 	if (brk_mp != NULL && brk_mp->map_pmap.pr_mapname[0] == '\0')
2885 		brk_mp->map_pmap.pr_mflags |= MA_ANON;
2886 	else if (brk_mp != NULL)
2887 		brk_mp->map_pmap.pr_mflags &= ~MA_BREAK;
2888 
2889 	*perr = 0;
2890 	return (P);
2891 
2892 err:
2893 	Pfree(P);
2894 	core_elf_close(&aout);
2895 	return (NULL);
2896 }
2897 
2898 /*
2899  * Grab a core file using a pathname.  We just open it and call Pfgrab_core().
2900  */
2901 struct ps_prochandle *
2902 Pgrab_core(const char *core, const char *aout, int gflag, int *perr)
2903 {
2904 	int fd, oflag = (gflag & PGRAB_RDONLY) ? O_RDONLY : O_RDWR;
2905 
2906 	if ((fd = open64(core, oflag)) >= 0)
2907 		return (Pfgrab_core(fd, aout, perr));
2908 
2909 	if (errno != ENOENT)
2910 		*perr = G_STRANGE;
2911 	else
2912 		*perr = G_NOCORE;
2913 
2914 	return (NULL);
2915 }
2916 
2917 int
2918 Pupanic(struct ps_prochandle *P, prupanic_t **pru)
2919 {
2920 	core_info_t *core;
2921 
2922 	if (P->state != PS_DEAD) {
2923 		errno = ENODATA;
2924 		return (-1);
2925 	}
2926 
2927 	core = P->data;
2928 	if (core->core_upanic == NULL) {
2929 		errno = ENOENT;
2930 		return (-1);
2931 	}
2932 
2933 	if (core->core_upanic->pru_version != PRUPANIC_VERSION_1) {
2934 		errno = EINVAL;
2935 		return (-1);
2936 	}
2937 
2938 	if ((*pru = calloc(1, sizeof (prupanic_t))) == NULL)
2939 		return (-1);
2940 	(void) memcpy(*pru, core->core_upanic, sizeof (prupanic_t));
2941 
2942 	return (0);
2943 }
2944 
2945 void
2946 Pupanic_free(prupanic_t *pru)
2947 {
2948 	free(pru);
2949 }
2950