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