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
  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
 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
 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
 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
 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
 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 *
 204 Ppsinfo_core(struct ps_prochandle *P, psinfo_t *psinfo, void *data)
 205 {
 206         return (&P->psinfo);
 207 }
 208 
 209 /*ARGSUSED*/
 210 static void
 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 *
 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
 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 *
 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
 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 *
 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
 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
 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
 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
 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
 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
 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
 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
 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
 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
 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
 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
 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
 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
 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
 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
 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
 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
 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
 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
 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
 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
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 right now ala what mdb does before using the xregs
1090  * data.
1091  */
1092 static int
1093 note_xreg(struct ps_prochandle *P, size_t nbytes)
1094 {
1095         core_info_t *core = P->data;
1096         lwp_info_t *lwp = core->core_lwp;
1097         prxregset_t *xregs;
1098         ssize_t sret;
1099 
1100         if (lwp == NULL || lwp->lwp_xregs != NULL)
1101                 return (0);     /* No lwp yet, already seen, or bad size */
1102 
1103         if ((xregs = malloc(nbytes)) == NULL)
1104                 return (-1);
1105 
1106         sret = read(P->asfd, xregs, nbytes);
1107         if (sret < 0 || (size_t)sret != nbytes) {
1108                 dprintf("Pgrab_core: failed to read NT_PRXREG\n");
1109                 free(xregs);
1110                 return (-1);
1111         }
1112 
1113         lwp->lwp_xregs = xregs;
1114         lwp->lwp_xregsize = nbytes;
1115         return (0);
1116 }
1117 
1118 #ifdef __sparc
1119 static int
1120 note_gwindows(struct ps_prochandle *P, size_t nbytes)
1121 {
1122         core_info_t *core = P->data;
1123         lwp_info_t *lwp = core->core_lwp;
1124 
1125         if (lwp == NULL || lwp->lwp_gwins != NULL || nbytes == 0)
1126                 return (0);     /* No lwp yet or already seen or no data */
1127 
1128         if ((lwp->lwp_gwins = malloc(sizeof (gwindows_t))) == NULL)
1129                 return (-1);
1130 
1131         /*
1132          * Since the amount of gwindows data varies with how many windows were
1133          * actually saved, we just read up to the minimum of the note size
1134          * and the size of the gwindows_t type.  It doesn't matter if the read
1135          * fails since we have to zero out gwindows first anyway.
1136          */
1137 #ifdef _LP64
1138         if (core->core_dmodel == PR_MODEL_ILP32) {
1139                 gwindows32_t g32;
1140 
1141                 (void) memset(&g32, 0, sizeof (g32));
1142                 (void) read(P->asfd, &g32, MIN(nbytes, sizeof (g32)));
1143                 gwindows_32_to_n(&g32, lwp->lwp_gwins);
1144 
1145         } else {
1146 #endif
1147                 (void) memset(lwp->lwp_gwins, 0, sizeof (gwindows_t));
1148                 (void) read(P->asfd, lwp->lwp_gwins,
1149                     MIN(nbytes, sizeof (gwindows_t)));
1150 #ifdef _LP64
1151         }
1152 #endif
1153         return (0);
1154 }
1155 
1156 #ifdef __sparcv9
1157 static int
1158 note_asrs(struct ps_prochandle *P, size_t nbytes)
1159 {
1160         core_info_t *core = P->data;
1161         lwp_info_t *lwp = core->core_lwp;
1162         int64_t *asrs;
1163 
1164         if (lwp == NULL || lwp->lwp_asrs != NULL || nbytes < sizeof (asrset_t))
1165                 return (0);     /* No lwp yet, already seen, or bad size */
1166 
1167         if ((asrs = malloc(sizeof (asrset_t))) == NULL)
1168                 return (-1);
1169 
1170         if (read(P->asfd, asrs, sizeof (asrset_t)) != sizeof (asrset_t)) {
1171                 dprintf("Pgrab_core: failed to read NT_ASRS\n");
1172                 free(asrs);
1173                 return (-1);
1174         }
1175 
1176         lwp->lwp_asrs = asrs;
1177         return (0);
1178 }
1179 #endif  /* __sparcv9 */
1180 #endif  /* __sparc */
1181 
1182 static int
1183 note_spymaster(struct ps_prochandle *P, size_t nbytes)
1184 {
1185 #ifdef _LP64
1186         core_info_t *core = P->data;
1187 
1188         if (core->core_dmodel == PR_MODEL_ILP32) {
1189                 psinfo32_t ps32;
1190 
1191                 if (nbytes < sizeof (psinfo32_t) ||
1192                     read(P->asfd, &ps32, sizeof (ps32)) != sizeof (ps32))
1193                         goto err;
1194 
1195                 psinfo_32_to_n(&ps32, &P->spymaster);
1196         } else
1197 #endif
1198         if (nbytes < sizeof (psinfo_t) || read(P->asfd,
1199             &P->spymaster, sizeof (psinfo_t)) != sizeof (psinfo_t))
1200                 goto err;
1201 
1202         dprintf("spymaster pr_fname = <%s>\n", P->psinfo.pr_fname);
1203         dprintf("spymaster pr_psargs = <%s>\n", P->psinfo.pr_psargs);
1204         dprintf("spymaster pr_wstat = 0x%x\n", P->psinfo.pr_wstat);
1205 
1206         return (0);
1207 
1208 err:
1209         dprintf("Pgrab_core: failed to read NT_SPYMASTER\n");
1210         return (-1);
1211 }
1212 
1213 static int
1214 note_upanic(struct ps_prochandle *P, size_t nbytes)
1215 {
1216         core_info_t *core = P->data;
1217         prupanic_t *pru;
1218 
1219         if (core->core_upanic != NULL)
1220                 return (0);
1221 
1222         if (sizeof (*pru) != nbytes) {
1223                 dprintf("Pgrab_core: NT_UPANIC changed size."
1224                     "  Need to handle a version change?\n");
1225                 return (-1);
1226         }
1227 
1228         if (nbytes != 0 && ((pru = malloc(nbytes)) != NULL)) {
1229                 if (read(P->asfd, pru, nbytes) != nbytes) {
1230                         dprintf("Pgrab_core: failed to read NT_UPANIC\n");
1231                         free(pru);
1232                         return (-1);
1233                 }
1234 
1235                 core->core_upanic = pru;
1236         }
1237 
1238         return (0);
1239 }
1240 
1241 /*ARGSUSED*/
1242 static int
1243 note_notsup(struct ps_prochandle *P, size_t nbytes)
1244 {
1245         dprintf("skipping unsupported note type of size %ld bytes\n",
1246             (ulong_t)nbytes);
1247         return (0);
1248 }
1249 
1250 /*
1251  * Populate a table of function pointers indexed by Note type with our
1252  * functions to process each type of core file note:
1253  */
1254 static int (*nhdlrs[])(struct ps_prochandle *, size_t) = {
1255         note_notsup,            /*  0   unassigned              */
1256 #ifdef __x86
1257         note_linux_prstatus,            /*  1   NT_PRSTATUS (old)       */
1258 #else
1259         note_notsup,            /*  1   NT_PRSTATUS (old)       */
1260 #endif
1261         note_notsup,            /*  2   NT_PRFPREG (old)        */
1262 #ifdef __x86
1263         note_linux_psinfo,              /*  3   NT_PRPSINFO (old)       */
1264 #else
1265         note_notsup,            /*  3   NT_PRPSINFO (old)       */
1266 #endif
1267         note_xreg,              /*  4   NT_PRXREG               */
1268         note_platform,          /*  5   NT_PLATFORM             */
1269         note_auxv,              /*  6   NT_AUXV                 */
1270 #ifdef __sparc
1271         note_gwindows,          /*  7   NT_GWINDOWS             */
1272 #ifdef __sparcv9
1273         note_asrs,              /*  8   NT_ASRS                 */
1274 #else
1275         note_notsup,            /*  8   NT_ASRS                 */
1276 #endif
1277 #else
1278         note_notsup,            /*  7   NT_GWINDOWS             */
1279         note_notsup,            /*  8   NT_ASRS                 */
1280 #endif
1281 #ifdef __x86
1282         note_ldt,               /*  9   NT_LDT                  */
1283 #else
1284         note_notsup,            /*  9   NT_LDT                  */
1285 #endif
1286         note_pstatus,           /* 10   NT_PSTATUS              */
1287         note_notsup,            /* 11   unassigned              */
1288         note_notsup,            /* 12   unassigned              */
1289         note_psinfo,            /* 13   NT_PSINFO               */
1290         note_cred,              /* 14   NT_PRCRED               */
1291         note_utsname,           /* 15   NT_UTSNAME              */
1292         note_lwpstatus,         /* 16   NT_LWPSTATUS            */
1293         note_lwpsinfo,          /* 17   NT_LWPSINFO             */
1294         note_priv,              /* 18   NT_PRPRIV               */
1295         note_priv_info,         /* 19   NT_PRPRIVINFO           */
1296         note_content,           /* 20   NT_CONTENT              */
1297         note_zonename,          /* 21   NT_ZONENAME             */
1298         note_fdinfo,            /* 22   NT_FDINFO               */
1299         note_spymaster,         /* 23   NT_SPYMASTER            */
1300         note_secflags,          /* 24   NT_SECFLAGS             */
1301         note_lwpname,           /* 25   NT_LWPNAME              */
1302         note_upanic             /* 26   NT_UPANIC               */
1303 };
1304 
1305 static void
1306 core_report_mapping(struct ps_prochandle *P, GElf_Phdr *php)
1307 {
1308         prkillinfo_t killinfo;
1309         siginfo_t *si = &killinfo.prk_info;
1310         char signame[SIG2STR_MAX], sig[64], info[64];
1311         void *addr = (void *)(uintptr_t)php->p_vaddr;
1312 
1313         const char *errfmt = "core file data for mapping at %p not saved: %s\n";
1314         const char *incfmt = "core file incomplete due to %s%s\n";
1315         const char *msgfmt = "mappings at and above %p are missing\n";
1316 
1317         if (!(php->p_flags & PF_SUNW_KILLED)) {
1318                 int err = 0;
1319 
1320                 (void) pread64(P->asfd, &err,
1321                     sizeof (err), (off64_t)php->p_offset);
1322 
1323                 Perror_printf(P, errfmt, addr, strerror(err));
1324                 dprintf(errfmt, addr, strerror(err));
1325                 return;
1326         }
1327 
1328         if (!(php->p_flags & PF_SUNW_SIGINFO))
1329                 return;
1330 
1331         (void) memset(&killinfo, 0, sizeof (killinfo));
1332 
1333         (void) pread64(P->asfd, &killinfo,
1334             sizeof (killinfo), (off64_t)php->p_offset);
1335 
1336         /*
1337          * While there is (or at least should be) only one segment that has
1338          * PF_SUNW_SIGINFO set, the signal information there is globally
1339          * useful (even if only to those debugging libproc consumers); we hang
1340          * the signal information gleaned here off of the ps_prochandle.
1341          */
1342         P->map_missing = php->p_vaddr;
1343         P->killinfo = killinfo.prk_info;
1344 
1345         if (sig2str(si->si_signo, signame) == -1) {
1346                 (void) snprintf(sig, sizeof (sig),
1347                     "<Unknown signal: 0x%x>, ", si->si_signo);
1348         } else {
1349                 (void) snprintf(sig, sizeof (sig), "SIG%s, ", signame);
1350         }
1351 
1352         if (si->si_code == SI_USER || si->si_code == SI_QUEUE) {
1353                 (void) snprintf(info, sizeof (info),
1354                     "pid=%d uid=%d zone=%d ctid=%d",
1355                     si->si_pid, si->si_uid, si->si_zoneid, si->si_ctid);
1356         } else {
1357                 (void) snprintf(info, sizeof (info),
1358                     "code=%d", si->si_code);
1359         }
1360 
1361         Perror_printf(P, incfmt, sig, info);
1362         Perror_printf(P, msgfmt, addr);
1363 
1364         dprintf(incfmt, sig, info);
1365         dprintf(msgfmt, addr);
1366 }
1367 
1368 /*
1369  * Add information on the address space mapping described by the given
1370  * PT_LOAD program header.  We fill in more information on the mapping later.
1371  */
1372 static int
1373 core_add_mapping(struct ps_prochandle *P, GElf_Phdr *php)
1374 {
1375         core_info_t *core = P->data;
1376         prmap_t pmap;
1377 
1378         dprintf("mapping base %llx filesz %llx memsz %llx offset %llx\n",
1379             (u_longlong_t)php->p_vaddr, (u_longlong_t)php->p_filesz,
1380             (u_longlong_t)php->p_memsz, (u_longlong_t)php->p_offset);
1381 
1382         pmap.pr_vaddr = (uintptr_t)php->p_vaddr;
1383         pmap.pr_size = php->p_memsz;
1384 
1385         /*
1386          * If Pgcore() or elfcore() fail to write a mapping, they will set
1387          * PF_SUNW_FAILURE in the Phdr and try to stash away the errno for us.
1388          */
1389         if (php->p_flags & PF_SUNW_FAILURE) {
1390                 core_report_mapping(P, php);
1391         } else if (php->p_filesz != 0 && php->p_offset >= core->core_size) {
1392                 Perror_printf(P, "core file may be corrupt -- data for mapping "
1393                     "at %p is missing\n", (void *)(uintptr_t)php->p_vaddr);
1394                 dprintf("core file may be corrupt -- data for mapping "
1395                     "at %p is missing\n", (void *)(uintptr_t)php->p_vaddr);
1396         }
1397 
1398         /*
1399          * The mapping name and offset will hopefully be filled in
1400          * by the librtld_db agent.  Unfortunately, if it isn't a
1401          * shared library mapping, this information is gone forever.
1402          */
1403         pmap.pr_mapname[0] = '\0';
1404         pmap.pr_offset = 0;
1405 
1406         pmap.pr_mflags = 0;
1407         if (php->p_flags & PF_R)
1408                 pmap.pr_mflags |= MA_READ;
1409         if (php->p_flags & PF_W)
1410                 pmap.pr_mflags |= MA_WRITE;
1411         if (php->p_flags & PF_X)
1412                 pmap.pr_mflags |= MA_EXEC;
1413 
1414         if (php->p_filesz == 0)
1415                 pmap.pr_mflags |= MA_RESERVED1;
1416 
1417         /*
1418          * At the time of adding this mapping, we just zero the pagesize.
1419          * Once we've processed more of the core file, we'll have the
1420          * pagesize from the auxv's AT_PAGESZ element and we can fill this in.
1421          */
1422         pmap.pr_pagesize = 0;
1423 
1424         /*
1425          * Unfortunately whether or not the mapping was a System V
1426          * shared memory segment is lost.  We use -1 to mark it as not shm.
1427          */
1428         pmap.pr_shmid = -1;
1429 
1430         return (Padd_mapping(P, php->p_offset, NULL, &pmap));
1431 }
1432 
1433 /*
1434  * Given a virtual address, name the mapping at that address using the
1435  * specified name, and return the map_info_t pointer.
1436  */
1437 static map_info_t *
1438 core_name_mapping(struct ps_prochandle *P, uintptr_t addr, const char *name)
1439 {
1440         map_info_t *mp = Paddr2mptr(P, addr);
1441 
1442         if (mp != NULL) {
1443                 (void) strncpy(mp->map_pmap.pr_mapname, name, PRMAPSZ);
1444                 mp->map_pmap.pr_mapname[PRMAPSZ - 1] = '\0';
1445         }
1446 
1447         return (mp);
1448 }
1449 
1450 /*
1451  * libproc uses libelf for all of its symbol table manipulation. This function
1452  * takes a symbol table and string table from a core file and places them
1453  * in a memory backed elf file.
1454  */
1455 static void
1456 fake_up_symtab(struct ps_prochandle *P, const elf_file_header_t *ehdr,
1457     GElf_Shdr *symtab, GElf_Shdr *strtab)
1458 {
1459         size_t size;
1460         off64_t off, base;
1461         map_info_t *mp;
1462         file_info_t *fp;
1463         Elf_Scn *scn;
1464         Elf_Data *data;
1465 
1466         if (symtab->sh_addr == 0 ||
1467             (mp = Paddr2mptr(P, symtab->sh_addr)) == NULL ||
1468             (fp = mp->map_file) == NULL) {
1469                 dprintf("fake_up_symtab: invalid section\n");
1470                 return;
1471         }
1472 
1473         if (fp->file_symtab.sym_data_pri != NULL) {
1474                 dprintf("Symbol table already loaded (sh_addr 0x%lx)\n",
1475                     (long)symtab->sh_addr);
1476                 return;
1477         }
1478 
1479         if (P->status.pr_dmodel == PR_MODEL_ILP32) {
1480                 struct {
1481                         Elf32_Ehdr ehdr;
1482                         Elf32_Shdr shdr[3];
1483                         char data[1];
1484                 } *b;
1485 
1486                 base = sizeof (b->ehdr) + sizeof (b->shdr);
1487                 size = base + symtab->sh_size + strtab->sh_size;
1488 
1489                 if ((b = calloc(1, size)) == NULL)
1490                         return;
1491 
1492                 (void) memcpy(b->ehdr.e_ident, ehdr->e_ident,
1493                     sizeof (ehdr->e_ident));
1494                 b->ehdr.e_type = ehdr->e_type;
1495                 b->ehdr.e_machine = ehdr->e_machine;
1496                 b->ehdr.e_version = ehdr->e_version;
1497                 b->ehdr.e_flags = ehdr->e_flags;
1498                 b->ehdr.e_ehsize = sizeof (b->ehdr);
1499                 b->ehdr.e_shoff = sizeof (b->ehdr);
1500                 b->ehdr.e_shentsize = sizeof (b->shdr[0]);
1501                 b->ehdr.e_shnum = 3;
1502                 off = 0;
1503 
1504                 b->shdr[1].sh_size = symtab->sh_size;
1505                 b->shdr[1].sh_type = SHT_SYMTAB;
1506                 b->shdr[1].sh_offset = off + base;
1507                 b->shdr[1].sh_entsize = sizeof (Elf32_Sym);
1508                 b->shdr[1].sh_link = 2;
1509                 b->shdr[1].sh_info =  symtab->sh_info;
1510                 b->shdr[1].sh_addralign = symtab->sh_addralign;
1511 
1512                 if (pread64(P->asfd, &b->data[off], b->shdr[1].sh_size,
1513                     symtab->sh_offset) != b->shdr[1].sh_size) {
1514                         dprintf("fake_up_symtab: pread of symtab[1] failed\n");
1515                         free(b);
1516                         return;
1517                 }
1518 
1519                 off += b->shdr[1].sh_size;
1520 
1521                 b->shdr[2].sh_flags = SHF_STRINGS;
1522                 b->shdr[2].sh_size = strtab->sh_size;
1523                 b->shdr[2].sh_type = SHT_STRTAB;
1524                 b->shdr[2].sh_offset = off + base;
1525                 b->shdr[2].sh_info =  strtab->sh_info;
1526                 b->shdr[2].sh_addralign = 1;
1527 
1528                 if (pread64(P->asfd, &b->data[off], b->shdr[2].sh_size,
1529                     strtab->sh_offset) != b->shdr[2].sh_size) {
1530                         dprintf("fake_up_symtab: pread of symtab[2] failed\n");
1531                         free(b);
1532                         return;
1533                 }
1534 
1535                 off += b->shdr[2].sh_size;
1536 
1537                 fp->file_symtab.sym_elf = elf_memory((char *)b, size);
1538                 if (fp->file_symtab.sym_elf == NULL) {
1539                         free(b);
1540                         return;
1541                 }
1542 
1543                 fp->file_symtab.sym_elfmem = b;
1544 #ifdef _LP64
1545         } else {
1546                 struct {
1547                         Elf64_Ehdr ehdr;
1548                         Elf64_Shdr shdr[3];
1549                         char data[1];
1550                 } *b;
1551 
1552                 base = sizeof (b->ehdr) + sizeof (b->shdr);
1553                 size = base + symtab->sh_size + strtab->sh_size;
1554 
1555                 if ((b = calloc(1, size)) == NULL)
1556                         return;
1557 
1558                 (void) memcpy(b->ehdr.e_ident, ehdr->e_ident,
1559                     sizeof (ehdr->e_ident));
1560                 b->ehdr.e_type = ehdr->e_type;
1561                 b->ehdr.e_machine = ehdr->e_machine;
1562                 b->ehdr.e_version = ehdr->e_version;
1563                 b->ehdr.e_flags = ehdr->e_flags;
1564                 b->ehdr.e_ehsize = sizeof (b->ehdr);
1565                 b->ehdr.e_shoff = sizeof (b->ehdr);
1566                 b->ehdr.e_shentsize = sizeof (b->shdr[0]);
1567                 b->ehdr.e_shnum = 3;
1568                 off = 0;
1569 
1570                 b->shdr[1].sh_size = symtab->sh_size;
1571                 b->shdr[1].sh_type = SHT_SYMTAB;
1572                 b->shdr[1].sh_offset = off + base;
1573                 b->shdr[1].sh_entsize = sizeof (Elf64_Sym);
1574                 b->shdr[1].sh_link = 2;
1575                 b->shdr[1].sh_info =  symtab->sh_info;
1576                 b->shdr[1].sh_addralign = symtab->sh_addralign;
1577 
1578                 if (pread64(P->asfd, &b->data[off], b->shdr[1].sh_size,
1579                     symtab->sh_offset) != b->shdr[1].sh_size) {
1580                         free(b);
1581                         return;
1582                 }
1583 
1584                 off += b->shdr[1].sh_size;
1585 
1586                 b->shdr[2].sh_flags = SHF_STRINGS;
1587                 b->shdr[2].sh_size = strtab->sh_size;
1588                 b->shdr[2].sh_type = SHT_STRTAB;
1589                 b->shdr[2].sh_offset = off + base;
1590                 b->shdr[2].sh_info =  strtab->sh_info;
1591                 b->shdr[2].sh_addralign = 1;
1592 
1593                 if (pread64(P->asfd, &b->data[off], b->shdr[2].sh_size,
1594                     strtab->sh_offset) != b->shdr[2].sh_size) {
1595                         free(b);
1596                         return;
1597                 }
1598 
1599                 off += b->shdr[2].sh_size;
1600 
1601                 fp->file_symtab.sym_elf = elf_memory((char *)b, size);
1602                 if (fp->file_symtab.sym_elf == NULL) {
1603                         free(b);
1604                         return;
1605                 }
1606 
1607                 fp->file_symtab.sym_elfmem = b;
1608 #endif
1609         }
1610 
1611         if ((scn = elf_getscn(fp->file_symtab.sym_elf, 1)) == NULL ||
1612             (fp->file_symtab.sym_data_pri = elf_getdata(scn, NULL)) == NULL ||
1613             (scn = elf_getscn(fp->file_symtab.sym_elf, 2)) == NULL ||
1614             (data = elf_getdata(scn, NULL)) == NULL) {
1615                 dprintf("fake_up_symtab: failed to get section data at %p\n",
1616                     (void *)scn);
1617                 goto err;
1618         }
1619 
1620         fp->file_symtab.sym_strs = data->d_buf;
1621         fp->file_symtab.sym_strsz = data->d_size;
1622         fp->file_symtab.sym_symn = symtab->sh_size / symtab->sh_entsize;
1623         fp->file_symtab.sym_hdr_pri = *symtab;
1624         fp->file_symtab.sym_strhdr = *strtab;
1625 
1626         optimize_symtab(&fp->file_symtab);
1627 
1628         return;
1629 err:
1630         (void) elf_end(fp->file_symtab.sym_elf);
1631         free(fp->file_symtab.sym_elfmem);
1632         fp->file_symtab.sym_elf = NULL;
1633         fp->file_symtab.sym_elfmem = NULL;
1634 }
1635 
1636 static void
1637 core_phdr_to_gelf(const Elf32_Phdr *src, GElf_Phdr *dst)
1638 {
1639         dst->p_type = src->p_type;
1640         dst->p_flags = src->p_flags;
1641         dst->p_offset = (Elf64_Off)src->p_offset;
1642         dst->p_vaddr = (Elf64_Addr)src->p_vaddr;
1643         dst->p_paddr = (Elf64_Addr)src->p_paddr;
1644         dst->p_filesz = (Elf64_Xword)src->p_filesz;
1645         dst->p_memsz = (Elf64_Xword)src->p_memsz;
1646         dst->p_align = (Elf64_Xword)src->p_align;
1647 }
1648 
1649 static void
1650 core_shdr_to_gelf(const Elf32_Shdr *src, GElf_Shdr *dst)
1651 {
1652         dst->sh_name = src->sh_name;
1653         dst->sh_type = src->sh_type;
1654         dst->sh_flags = (Elf64_Xword)src->sh_flags;
1655         dst->sh_addr = (Elf64_Addr)src->sh_addr;
1656         dst->sh_offset = (Elf64_Off)src->sh_offset;
1657         dst->sh_size = (Elf64_Xword)src->sh_size;
1658         dst->sh_link = src->sh_link;
1659         dst->sh_info = src->sh_info;
1660         dst->sh_addralign = (Elf64_Xword)src->sh_addralign;
1661         dst->sh_entsize = (Elf64_Xword)src->sh_entsize;
1662 }
1663 
1664 /*
1665  * Perform elf_begin on efp->e_fd and verify the ELF file's type and class.
1666  */
1667 static int
1668 core_elf_fdopen(elf_file_t *efp, GElf_Half type, int *perr)
1669 {
1670 #ifdef _BIG_ENDIAN
1671         uchar_t order = ELFDATA2MSB;
1672 #else
1673         uchar_t order = ELFDATA2LSB;
1674 #endif
1675         Elf32_Ehdr e32;
1676         int is_noelf = -1;
1677         int isa_err = 0;
1678 
1679         /*
1680          * Because 32-bit libelf cannot deal with large files, we need to read,
1681          * check, and convert the file header manually in case type == ET_CORE.
1682          */
1683         if (pread64(efp->e_fd, &e32, sizeof (e32), 0) != sizeof (e32)) {
1684                 if (perr != NULL)
1685                         *perr = G_FORMAT;
1686                 goto err;
1687         }
1688         if ((is_noelf = memcmp(&e32.e_ident[EI_MAG0], ELFMAG, SELFMAG)) != 0 ||
1689             e32.e_type != type || (isa_err = (e32.e_ident[EI_DATA] != order)) ||
1690             e32.e_version != EV_CURRENT) {
1691                 if (perr != NULL) {
1692                         if (is_noelf == 0 && isa_err) {
1693                                 *perr = G_ISAINVAL;
1694                         } else {
1695                                 *perr = G_FORMAT;
1696                         }
1697                 }
1698                 goto err;
1699         }
1700 
1701         /*
1702          * If the file is 64-bit and we are 32-bit, fail with G_LP64.  If the
1703          * file is 64-bit and we are 64-bit, re-read the header as a Elf64_Ehdr,
1704          * and convert it to a elf_file_header_t.  Otherwise, the file is
1705          * 32-bit, so convert e32 to a elf_file_header_t.
1706          */
1707         if (e32.e_ident[EI_CLASS] == ELFCLASS64) {
1708 #ifdef _LP64
1709                 Elf64_Ehdr e64;
1710 
1711                 if (pread64(efp->e_fd, &e64, sizeof (e64), 0) != sizeof (e64)) {
1712                         if (perr != NULL)
1713                                 *perr = G_FORMAT;
1714                         goto err;
1715                 }
1716 
1717                 (void) memcpy(efp->e_hdr.e_ident, e64.e_ident, EI_NIDENT);
1718                 efp->e_hdr.e_type = e64.e_type;
1719                 efp->e_hdr.e_machine = e64.e_machine;
1720                 efp->e_hdr.e_version = e64.e_version;
1721                 efp->e_hdr.e_entry = e64.e_entry;
1722                 efp->e_hdr.e_phoff = e64.e_phoff;
1723                 efp->e_hdr.e_shoff = e64.e_shoff;
1724                 efp->e_hdr.e_flags = e64.e_flags;
1725                 efp->e_hdr.e_ehsize = e64.e_ehsize;
1726                 efp->e_hdr.e_phentsize = e64.e_phentsize;
1727                 efp->e_hdr.e_phnum = (Elf64_Word)e64.e_phnum;
1728                 efp->e_hdr.e_shentsize = e64.e_shentsize;
1729                 efp->e_hdr.e_shnum = (Elf64_Word)e64.e_shnum;
1730                 efp->e_hdr.e_shstrndx = (Elf64_Word)e64.e_shstrndx;
1731 #else   /* _LP64 */
1732                 if (perr != NULL)
1733                         *perr = G_LP64;
1734                 goto err;
1735 #endif  /* _LP64 */
1736         } else {
1737                 (void) memcpy(efp->e_hdr.e_ident, e32.e_ident, EI_NIDENT);
1738                 efp->e_hdr.e_type = e32.e_type;
1739                 efp->e_hdr.e_machine = e32.e_machine;
1740                 efp->e_hdr.e_version = e32.e_version;
1741                 efp->e_hdr.e_entry = (Elf64_Addr)e32.e_entry;
1742                 efp->e_hdr.e_phoff = (Elf64_Off)e32.e_phoff;
1743                 efp->e_hdr.e_shoff = (Elf64_Off)e32.e_shoff;
1744                 efp->e_hdr.e_flags = e32.e_flags;
1745                 efp->e_hdr.e_ehsize = e32.e_ehsize;
1746                 efp->e_hdr.e_phentsize = e32.e_phentsize;
1747                 efp->e_hdr.e_phnum = (Elf64_Word)e32.e_phnum;
1748                 efp->e_hdr.e_shentsize = e32.e_shentsize;
1749                 efp->e_hdr.e_shnum = (Elf64_Word)e32.e_shnum;
1750                 efp->e_hdr.e_shstrndx = (Elf64_Word)e32.e_shstrndx;
1751         }
1752 
1753         /*
1754          * If the number of section headers or program headers or the section
1755          * header string table index would overflow their respective fields
1756          * in the ELF header, they're stored in the section header at index
1757          * zero. To simplify use elsewhere, we look for those sentinel values
1758          * here.
1759          */
1760         if ((efp->e_hdr.e_shnum == 0 && efp->e_hdr.e_shoff != 0) ||
1761             efp->e_hdr.e_shstrndx == SHN_XINDEX ||
1762             efp->e_hdr.e_phnum == PN_XNUM) {
1763                 GElf_Shdr shdr;
1764 
1765                 dprintf("extended ELF header\n");
1766 
1767                 if (efp->e_hdr.e_shoff == 0) {
1768                         if (perr != NULL)
1769                                 *perr = G_FORMAT;
1770                         goto err;
1771                 }
1772 
1773                 if (efp->e_hdr.e_ident[EI_CLASS] == ELFCLASS32) {
1774                         Elf32_Shdr shdr32;
1775 
1776                         if (pread64(efp->e_fd, &shdr32, sizeof (shdr32),
1777                             efp->e_hdr.e_shoff) != sizeof (shdr32)) {
1778                                 if (perr != NULL)
1779                                         *perr = G_FORMAT;
1780                                 goto err;
1781                         }
1782 
1783                         core_shdr_to_gelf(&shdr32, &shdr);
1784                 } else {
1785                         if (pread64(efp->e_fd, &shdr, sizeof (shdr),
1786                             efp->e_hdr.e_shoff) != sizeof (shdr)) {
1787                                 if (perr != NULL)
1788                                         *perr = G_FORMAT;
1789                                 goto err;
1790                         }
1791                 }
1792 
1793                 if (efp->e_hdr.e_shnum == 0) {
1794                         efp->e_hdr.e_shnum = shdr.sh_size;
1795                         dprintf("section header count %lu\n",
1796                             (ulong_t)shdr.sh_size);
1797                 }
1798 
1799                 if (efp->e_hdr.e_shstrndx == SHN_XINDEX) {
1800                         efp->e_hdr.e_shstrndx = shdr.sh_link;
1801                         dprintf("section string index %u\n", shdr.sh_link);
1802                 }
1803 
1804                 if (efp->e_hdr.e_phnum == PN_XNUM && shdr.sh_info != 0) {
1805                         efp->e_hdr.e_phnum = shdr.sh_info;
1806                         dprintf("program header count %u\n", shdr.sh_info);
1807                 }
1808 
1809         } else if (efp->e_hdr.e_phoff != 0) {
1810                 GElf_Phdr phdr;
1811                 uint64_t phnum;
1812 
1813                 /*
1814                  * It's possible this core file came from a system that
1815                  * accidentally truncated the e_phnum field without correctly
1816                  * using the extended format in the section header at index
1817                  * zero. We try to detect and correct that specific type of
1818                  * corruption by using the knowledge that the core dump
1819                  * routines usually place the data referenced by the first
1820                  * program header immediately after the last header element.
1821                  */
1822                 if (efp->e_hdr.e_ident[EI_CLASS] == ELFCLASS32) {
1823                         Elf32_Phdr phdr32;
1824 
1825                         if (pread64(efp->e_fd, &phdr32, sizeof (phdr32),
1826                             efp->e_hdr.e_phoff) != sizeof (phdr32)) {
1827                                 if (perr != NULL)
1828                                         *perr = G_FORMAT;
1829                                 goto err;
1830                         }
1831 
1832                         core_phdr_to_gelf(&phdr32, &phdr);
1833                 } else {
1834                         if (pread64(efp->e_fd, &phdr, sizeof (phdr),
1835                             efp->e_hdr.e_phoff) != sizeof (phdr)) {
1836                                 if (perr != NULL)
1837                                         *perr = G_FORMAT;
1838                                 goto err;
1839                         }
1840                 }
1841 
1842                 phnum = phdr.p_offset - efp->e_hdr.e_ehsize -
1843                     (uint64_t)efp->e_hdr.e_shnum * efp->e_hdr.e_shentsize;
1844                 phnum /= efp->e_hdr.e_phentsize;
1845 
1846                 if (phdr.p_offset != 0 && phnum != efp->e_hdr.e_phnum) {
1847                         dprintf("suspicious program header count %u %u\n",
1848                             (uint_t)phnum, efp->e_hdr.e_phnum);
1849 
1850                         /*
1851                          * If the new program header count we computed doesn't
1852                          * jive with count in the ELF header, we'll use the
1853                          * data that's there and hope for the best.
1854                          *
1855                          * If it does, it's also possible that the section
1856                          * header offset is incorrect; we'll check that and
1857                          * possibly try to fix it.
1858                          */
1859                         if (phnum <= INT_MAX &&
1860                             (uint16_t)phnum == efp->e_hdr.e_phnum) {
1861 
1862                                 if (efp->e_hdr.e_shoff == efp->e_hdr.e_phoff +
1863                                     efp->e_hdr.e_phentsize *
1864                                     (uint_t)efp->e_hdr.e_phnum) {
1865                                         efp->e_hdr.e_shoff =
1866                                             efp->e_hdr.e_phoff +
1867                                             efp->e_hdr.e_phentsize * phnum;
1868                                 }
1869 
1870                                 efp->e_hdr.e_phnum = (Elf64_Word)phnum;
1871                                 dprintf("using new program header count\n");
1872                         } else {
1873                                 dprintf("inconsistent program header count\n");
1874                         }
1875                 }
1876         }
1877 
1878         /*
1879          * The libelf implementation was never ported to be large-file aware.
1880          * This is typically not a problem for your average executable or
1881          * shared library, but a large 32-bit core file can exceed 2GB in size.
1882          * So if type is ET_CORE, we don't bother doing elf_begin; the code
1883          * in Pfgrab_core() below will do its own i/o and struct conversion.
1884          */
1885 
1886         if (type == ET_CORE) {
1887                 efp->e_elf = NULL;
1888                 return (0);
1889         }
1890 
1891         if ((efp->e_elf = elf_begin(efp->e_fd, ELF_C_READ, NULL)) == NULL) {
1892                 if (perr != NULL)
1893                         *perr = G_ELF;
1894                 goto err;
1895         }
1896 
1897         return (0);
1898 
1899 err:
1900         efp->e_elf = NULL;
1901         return (-1);
1902 }
1903 
1904 /*
1905  * Open the specified file and then do a core_elf_fdopen on it.
1906  */
1907 static int
1908 core_elf_open(elf_file_t *efp, const char *path, GElf_Half type, int *perr)
1909 {
1910         (void) memset(efp, 0, sizeof (elf_file_t));
1911 
1912         if ((efp->e_fd = open64(path, O_RDONLY)) >= 0) {
1913                 if (core_elf_fdopen(efp, type, perr) == 0)
1914                         return (0);
1915 
1916                 (void) close(efp->e_fd);
1917                 efp->e_fd = -1;
1918         }
1919 
1920         return (-1);
1921 }
1922 
1923 /*
1924  * Close the ELF handle and file descriptor.
1925  */
1926 static void
1927 core_elf_close(elf_file_t *efp)
1928 {
1929         if (efp->e_elf != NULL) {
1930                 (void) elf_end(efp->e_elf);
1931                 efp->e_elf = NULL;
1932         }
1933 
1934         if (efp->e_fd != -1) {
1935                 (void) close(efp->e_fd);
1936                 efp->e_fd = -1;
1937         }
1938 }
1939 
1940 /*
1941  * Given an ELF file for a statically linked executable, locate the likely
1942  * primary text section and fill in rl_base with its virtual address.
1943  */
1944 static map_info_t *
1945 core_find_text(struct ps_prochandle *P, Elf *elf, rd_loadobj_t *rlp)
1946 {
1947         GElf_Phdr phdr;
1948         uint_t i;
1949         size_t nphdrs;
1950 
1951         if (elf_getphdrnum(elf, &nphdrs) == -1)
1952                 return (NULL);
1953 
1954         for (i = 0; i < nphdrs; i++) {
1955                 if (gelf_getphdr(elf, i, &phdr) != NULL &&
1956                     phdr.p_type == PT_LOAD && (phdr.p_flags & PF_X)) {
1957                         rlp->rl_base = phdr.p_vaddr;
1958                         return (Paddr2mptr(P, rlp->rl_base));
1959                 }
1960         }
1961 
1962         return (NULL);
1963 }
1964 
1965 /*
1966  * Given an ELF file and the librtld_db structure corresponding to its primary
1967  * text mapping, deduce where its data segment was loaded and fill in
1968  * rl_data_base and prmap_t.pr_offset accordingly.
1969  */
1970 static map_info_t *
1971 core_find_data(struct ps_prochandle *P, Elf *elf, rd_loadobj_t *rlp)
1972 {
1973         GElf_Ehdr ehdr;
1974         GElf_Phdr phdr;
1975         map_info_t *mp;
1976         uint_t i, pagemask;
1977         size_t nphdrs;
1978 
1979         rlp->rl_data_base = (uintptr_t)NULL;
1980 
1981         /*
1982          * Find the first loadable, writeable Phdr and compute rl_data_base
1983          * as the virtual address at which is was loaded.
1984          */
1985         if (gelf_getehdr(elf, &ehdr) == NULL ||
1986             elf_getphdrnum(elf, &nphdrs) == -1)
1987                 return (NULL);
1988 
1989         for (i = 0; i < nphdrs; i++) {
1990                 if (gelf_getphdr(elf, i, &phdr) != NULL &&
1991                     phdr.p_type == PT_LOAD && (phdr.p_flags & PF_W)) {
1992                         rlp->rl_data_base = phdr.p_vaddr;
1993                         if (ehdr.e_type == ET_DYN)
1994                                 rlp->rl_data_base += rlp->rl_base;
1995                         break;
1996                 }
1997         }
1998 
1999         /*
2000          * If we didn't find an appropriate phdr or if the address we
2001          * computed has no mapping, return NULL.
2002          */
2003         if (rlp->rl_data_base == (uintptr_t)NULL ||
2004             (mp = Paddr2mptr(P, rlp->rl_data_base)) == NULL)
2005                 return (NULL);
2006 
2007         /*
2008          * It wouldn't be procfs-related code if we didn't make use of
2009          * unclean knowledge of segvn, even in userland ... the prmap_t's
2010          * pr_offset field will be the segvn offset from mmap(2)ing the
2011          * data section, which will be the file offset & PAGEMASK.
2012          */
2013         pagemask = ~(mp->map_pmap.pr_pagesize - 1);
2014         mp->map_pmap.pr_offset = phdr.p_offset & pagemask;
2015 
2016         return (mp);
2017 }
2018 
2019 /*
2020  * Librtld_db agent callback for iterating over load object mappings.
2021  * For each load object, we allocate a new file_info_t, perform naming,
2022  * and attempt to construct a symbol table for the load object.
2023  */
2024 static int
2025 core_iter_mapping(const rd_loadobj_t *rlp, struct ps_prochandle *P)
2026 {
2027         core_info_t *core = P->data;
2028         char lname[PATH_MAX], buf[PATH_MAX];
2029         file_info_t *fp;
2030         map_info_t *mp;
2031 
2032         if (Pread_string(P, lname, PATH_MAX, (off_t)rlp->rl_nameaddr) <= 0) {
2033                 dprintf("failed to read name %p\n", (void *)rlp->rl_nameaddr);
2034                 return (1); /* Keep going; forget this if we can't get a name */
2035         }
2036 
2037         dprintf("rd_loadobj name = \"%s\" rl_base = %p\n",
2038             lname, (void *)rlp->rl_base);
2039 
2040         if ((mp = Paddr2mptr(P, rlp->rl_base)) == NULL) {
2041                 dprintf("no mapping for %p\n", (void *)rlp->rl_base);
2042                 return (1); /* No mapping; advance to next mapping */
2043         }
2044 
2045         /*
2046          * Create a new file_info_t for this mapping, and therefore for
2047          * this load object.
2048          *
2049          * If there's an ELF header at the beginning of this mapping,
2050          * file_info_new() will try to use its section headers to
2051          * identify any other mappings that belong to this load object.
2052          */
2053         if ((fp = mp->map_file) == NULL &&
2054             (fp = file_info_new(P, mp)) == NULL) {
2055                 core->core_errno = errno;
2056                 dprintf("failed to malloc mapping data\n");
2057                 return (0); /* Abort */
2058         }
2059         fp->file_map = mp;
2060 
2061         /* Create a local copy of the load object representation */
2062         if ((fp->file_lo = calloc(1, sizeof (rd_loadobj_t))) == NULL) {
2063                 core->core_errno = errno;
2064                 dprintf("failed to malloc mapping data\n");
2065                 return (0); /* Abort */
2066         }
2067         *fp->file_lo = *rlp;
2068 
2069         if (lname[0] != '\0') {
2070                 /*
2071                  * Naming dance part 1: if we got a name from librtld_db, then
2072                  * copy this name to the prmap_t if it is unnamed.  If the
2073                  * file_info_t is unnamed, name it after the lname.
2074                  */
2075                 if (mp->map_pmap.pr_mapname[0] == '\0') {
2076                         (void) strncpy(mp->map_pmap.pr_mapname, lname, PRMAPSZ);
2077                         mp->map_pmap.pr_mapname[PRMAPSZ - 1] = '\0';
2078                 }
2079 
2080                 if (fp->file_lname == NULL)
2081                         fp->file_lname = strdup(lname);
2082 
2083         } else if (fp->file_lname == NULL &&
2084             mp->map_pmap.pr_mapname[0] != '\0') {
2085                 /*
2086                  * Naming dance part 2: if the mapping is named and the
2087                  * file_info_t is not, name the file after the mapping.
2088                  */
2089                 fp->file_lname = strdup(mp->map_pmap.pr_mapname);
2090         }
2091 
2092         if ((fp->file_rname == NULL) &&
2093             (Pfindmap(P, mp, buf, sizeof (buf)) != NULL))
2094                 fp->file_rname = strdup(buf);
2095 
2096         if (fp->file_lname != NULL)
2097                 fp->file_lbase = basename(fp->file_lname);
2098         if (fp->file_rname != NULL)
2099                 fp->file_rbase = basename(fp->file_rname);
2100 
2101         /* Associate the file and the mapping. */
2102         (void) strncpy(fp->file_pname, mp->map_pmap.pr_mapname, PRMAPSZ);
2103         fp->file_pname[PRMAPSZ - 1] = '\0';
2104 
2105         /*
2106          * If no section headers were available then we'll have to
2107          * identify this load object's other mappings with what we've
2108          * got: the start and end of the object's corresponding
2109          * address space.
2110          */
2111         if (fp->file_saddrs == NULL) {
2112                 for (mp = fp->file_map + 1; mp < P->mappings + P->map_count &&
2113                     mp->map_pmap.pr_vaddr < rlp->rl_bend; mp++) {
2114 
2115                         if (mp->map_file == NULL) {
2116                                 dprintf("core_iter_mapping %s: associating "
2117                                     "segment at %p\n",
2118                                     fp->file_pname,
2119                                     (void *)mp->map_pmap.pr_vaddr);
2120                                 mp->map_file = fp;
2121                                 fp->file_ref++;
2122                         } else {
2123                                 dprintf("core_iter_mapping %s: segment at "
2124                                     "%p already associated with %s\n",
2125                                     fp->file_pname,
2126                                     (void *)mp->map_pmap.pr_vaddr,
2127                                     (mp == fp->file_map ? "this file" :
2128                                     mp->map_file->file_pname));
2129                         }
2130                 }
2131         }
2132 
2133         /* Ensure that all this file's mappings are named. */
2134         for (mp = fp->file_map; mp < P->mappings + P->map_count &&
2135             mp->map_file == fp; mp++) {
2136                 if (mp->map_pmap.pr_mapname[0] == '\0' &&
2137                     !(mp->map_pmap.pr_mflags & MA_BREAK)) {
2138                         (void) strncpy(mp->map_pmap.pr_mapname, fp->file_pname,
2139                             PRMAPSZ);
2140                         mp->map_pmap.pr_mapname[PRMAPSZ - 1] = '\0';
2141                 }
2142         }
2143 
2144         /* Attempt to build a symbol table for this file. */
2145         Pbuild_file_symtab(P, fp);
2146         if (fp->file_elf == NULL)
2147                 dprintf("core_iter_mapping: no symtab for %s\n",
2148                     fp->file_pname);
2149 
2150         /* Locate the start of a data segment associated with this file. */
2151         if ((mp = core_find_data(P, fp->file_elf, fp->file_lo)) != NULL) {
2152                 dprintf("found data for %s at %p (pr_offset 0x%llx)\n",
2153                     fp->file_pname, (void *)fp->file_lo->rl_data_base,
2154                     mp->map_pmap.pr_offset);
2155         } else {
2156                 dprintf("core_iter_mapping: no data found for %s\n",
2157                     fp->file_pname);
2158         }
2159 
2160         return (1); /* Advance to next mapping */
2161 }
2162 
2163 /*
2164  * Callback function for Pfindexec().  In order to confirm a given pathname,
2165  * we verify that we can open it as an ELF file of type ET_EXEC or ET_DYN.
2166  */
2167 static int
2168 core_exec_open(const char *path, void *efp)
2169 {
2170         if (core_elf_open(efp, path, ET_EXEC, NULL) == 0)
2171                 return (1);
2172         if (core_elf_open(efp, path, ET_DYN, NULL) == 0)
2173                 return (1);
2174         return (0);
2175 }
2176 
2177 /*
2178  * Attempt to load any section headers found in the core file.  If present,
2179  * this will refer to non-loadable data added to the core file by the kernel
2180  * based on coreadm(8) settings, including CTF data and the symbol table.
2181  */
2182 static void
2183 core_load_shdrs(struct ps_prochandle *P, elf_file_t *efp)
2184 {
2185         GElf_Shdr *shp, *shdrs = NULL;
2186         char *shstrtab = NULL;
2187         ulong_t shstrtabsz;
2188         const char *name;
2189         map_info_t *mp;
2190 
2191         size_t nbytes;
2192         void *buf;
2193         int i;
2194 
2195         if (efp->e_hdr.e_shstrndx >= efp->e_hdr.e_shnum) {
2196                 dprintf("corrupt shstrndx (%u) exceeds shnum (%u)\n",
2197                     efp->e_hdr.e_shstrndx, efp->e_hdr.e_shnum);
2198                 return;
2199         }
2200 
2201         /*
2202          * Read the section header table from the core file and then iterate
2203          * over the section headers, converting each to a GElf_Shdr.
2204          */
2205         if ((shdrs = malloc(efp->e_hdr.e_shnum * sizeof (GElf_Shdr))) == NULL) {
2206                 dprintf("failed to malloc %u section headers: %s\n",
2207                     (uint_t)efp->e_hdr.e_shnum, strerror(errno));
2208                 return;
2209         }
2210 
2211         nbytes = efp->e_hdr.e_shnum * efp->e_hdr.e_shentsize;
2212         if ((buf = malloc(nbytes)) == NULL) {
2213                 dprintf("failed to malloc %d bytes: %s\n", (int)nbytes,
2214                     strerror(errno));
2215                 free(shdrs);
2216                 goto out;
2217         }
2218 
2219         if (pread64(efp->e_fd, buf, nbytes, efp->e_hdr.e_shoff) != nbytes) {
2220                 dprintf("failed to read section headers at off %lld: %s\n",
2221                     (longlong_t)efp->e_hdr.e_shoff, strerror(errno));
2222                 free(buf);
2223                 goto out;
2224         }
2225 
2226         for (i = 0; i < efp->e_hdr.e_shnum; i++) {
2227                 void *p = (uchar_t *)buf + efp->e_hdr.e_shentsize * i;
2228 
2229                 if (efp->e_hdr.e_ident[EI_CLASS] == ELFCLASS32)
2230                         core_shdr_to_gelf(p, &shdrs[i]);
2231                 else
2232                         (void) memcpy(&shdrs[i], p, sizeof (GElf_Shdr));
2233         }
2234 
2235         free(buf);
2236         buf = NULL;
2237 
2238         /*
2239          * Read the .shstrtab section from the core file, terminating it with
2240          * an extra \0 so that a corrupt section will not cause us to die.
2241          */
2242         shp = &shdrs[efp->e_hdr.e_shstrndx];
2243         shstrtabsz = shp->sh_size;
2244 
2245         if ((shstrtab = malloc(shstrtabsz + 1)) == NULL) {
2246                 dprintf("failed to allocate %lu bytes for shstrtab\n",
2247                     (ulong_t)shstrtabsz);
2248                 goto out;
2249         }
2250 
2251         if (pread64(efp->e_fd, shstrtab, shstrtabsz,
2252             shp->sh_offset) != shstrtabsz) {
2253                 dprintf("failed to read %lu bytes of shstrs at off %lld: %s\n",
2254                     shstrtabsz, (longlong_t)shp->sh_offset, strerror(errno));
2255                 goto out;
2256         }
2257 
2258         shstrtab[shstrtabsz] = '\0';
2259 
2260         /*
2261          * Now iterate over each section in the section header table, locating
2262          * sections of interest and initializing more of the ps_prochandle.
2263          */
2264         for (i = 0; i < efp->e_hdr.e_shnum; i++) {
2265                 shp = &shdrs[i];
2266                 name = shstrtab + shp->sh_name;
2267 
2268                 if (shp->sh_name >= shstrtabsz) {
2269                         dprintf("skipping section [%d]: corrupt sh_name\n", i);
2270                         continue;
2271                 }
2272 
2273                 if (shp->sh_link >= efp->e_hdr.e_shnum) {
2274                         dprintf("skipping section [%d]: corrupt sh_link\n", i);
2275                         continue;
2276                 }
2277 
2278                 dprintf("found section header %s (sh_addr 0x%llx)\n",
2279                     name, (u_longlong_t)shp->sh_addr);
2280 
2281                 if (strcmp(name, ".SUNW_ctf") == 0) {
2282                         if ((mp = Paddr2mptr(P, shp->sh_addr)) == NULL) {
2283                                 dprintf("no map at addr 0x%llx for %s [%d]\n",
2284                                     (u_longlong_t)shp->sh_addr, name, i);
2285                                 continue;
2286                         }
2287 
2288                         if (mp->map_file == NULL ||
2289                             mp->map_file->file_ctf_buf != NULL) {
2290                                 dprintf("no mapping file or duplicate buffer "
2291                                     "for %s [%d]\n", name, i);
2292                                 continue;
2293                         }
2294 
2295                         if ((buf = malloc(shp->sh_size)) == NULL ||
2296                             pread64(efp->e_fd, buf, shp->sh_size,
2297                             shp->sh_offset) != shp->sh_size) {
2298                                 dprintf("skipping section %s [%d]: %s\n",
2299                                     name, i, strerror(errno));
2300                                 free(buf);
2301                                 continue;
2302                         }
2303 
2304                         mp->map_file->file_ctf_size = shp->sh_size;
2305                         mp->map_file->file_ctf_buf = buf;
2306 
2307                         if (shdrs[shp->sh_link].sh_type == SHT_DYNSYM)
2308                                 mp->map_file->file_ctf_dyn = 1;
2309 
2310                 } else if (strcmp(name, ".symtab") == 0) {
2311                         fake_up_symtab(P, &efp->e_hdr,
2312                             shp, &shdrs[shp->sh_link]);
2313                 }
2314         }
2315 out:
2316         free(shstrtab);
2317         free(shdrs);
2318 }
2319 
2320 /*
2321  * Main engine for core file initialization: given an fd for the core file
2322  * and an optional pathname, construct the ps_prochandle.  The aout_path can
2323  * either be a suggested executable pathname, or a suggested directory to
2324  * use as a possible current working directory.
2325  */
2326 struct ps_prochandle *
2327 Pfgrab_core(int core_fd, const char *aout_path, int *perr)
2328 {
2329         struct ps_prochandle *P;
2330         core_info_t *core_info;
2331         map_info_t *stk_mp, *brk_mp;
2332         const char *execname;
2333         char *interp;
2334         int i, notes, pagesize;
2335         uintptr_t addr, base_addr;
2336         struct stat64 stbuf;
2337         void *phbuf, *php;
2338         size_t nbytes;
2339 #ifdef __x86
2340         boolean_t from_linux = B_FALSE;
2341 #endif
2342 
2343         elf_file_t aout;
2344         elf_file_t core;
2345 
2346         Elf_Scn *scn, *intp_scn = NULL;
2347         Elf_Data *dp;
2348 
2349         GElf_Phdr phdr, note_phdr;
2350         GElf_Shdr shdr;
2351         GElf_Xword nleft;
2352 
2353         if (elf_version(EV_CURRENT) == EV_NONE) {
2354                 dprintf("libproc ELF version is more recent than libelf\n");
2355                 *perr = G_ELF;
2356                 return (NULL);
2357         }
2358 
2359         aout.e_elf = NULL;
2360         aout.e_fd = -1;
2361 
2362         core.e_elf = NULL;
2363         core.e_fd = core_fd;
2364 
2365         /*
2366          * Allocate and initialize a ps_prochandle structure for the core.
2367          * There are several key pieces of initialization here:
2368          *
2369          * 1. The PS_DEAD state flag marks this prochandle as a core file.
2370          *    PS_DEAD also thus prevents all operations which require state
2371          *    to be PS_STOP from operating on this handle.
2372          *
2373          * 2. We keep the core file fd in P->asfd since the core file contains
2374          *    the remnants of the process address space.
2375          *
2376          * 3. We set the P->info_valid bit because all information about the
2377          *    core is determined by the end of this function; there is no need
2378          *    for proc_update_maps() to reload mappings at any later point.
2379          *
2380          * 4. The read/write ops vector uses our core_rw() function defined
2381          *    above to handle i/o requests.
2382          */
2383         if ((P = malloc(sizeof (struct ps_prochandle))) == NULL) {
2384                 *perr = G_STRANGE;
2385                 return (NULL);
2386         }
2387 
2388         (void) memset(P, 0, sizeof (struct ps_prochandle));
2389         (void) mutex_init(&P->proc_lock, USYNC_THREAD, NULL);
2390         P->state = PS_DEAD;
2391         P->pid = (pid_t)-1;
2392         P->asfd = core.e_fd;
2393         P->ctlfd = -1;
2394         P->statfd = -1;
2395         P->agentctlfd = -1;
2396         P->agentstatfd = -1;
2397         P->zoneroot = NULL;
2398         P->info_valid = 1;
2399         Pinit_ops(&P->ops, &P_core_ops);
2400 
2401         Pinitsym(P);
2402         Pinitfd(P);
2403 
2404         /*
2405          * Fstat and open the core file and make sure it is a valid ELF core.
2406          */
2407         if (fstat64(P->asfd, &stbuf) == -1) {
2408                 *perr = G_STRANGE;
2409                 goto err;
2410         }
2411 
2412         if (core_elf_fdopen(&core, ET_CORE, perr) == -1)
2413                 goto err;
2414 
2415         /*
2416          * Allocate and initialize a core_info_t to hang off the ps_prochandle
2417          * structure.  We keep all core-specific information in this structure.
2418          */
2419         if ((core_info = calloc(1, sizeof (core_info_t))) == NULL) {
2420                 *perr = G_STRANGE;
2421                 goto err;
2422         }
2423 
2424         P->data = core_info;
2425         list_create(&core_info->core_lwp_head, sizeof (lwp_info_t),
2426             offsetof(lwp_info_t, lwp_list));
2427         core_info->core_size = stbuf.st_size;
2428         /*
2429          * In the days before adjustable core file content, this was the
2430          * default core file content. For new core files, this value will
2431          * be overwritten by the NT_CONTENT note section.
2432          */
2433         core_info->core_content = CC_CONTENT_STACK | CC_CONTENT_HEAP |
2434             CC_CONTENT_DATA | CC_CONTENT_RODATA | CC_CONTENT_ANON |
2435             CC_CONTENT_SHANON;
2436 
2437         switch (core.e_hdr.e_ident[EI_CLASS]) {
2438         case ELFCLASS32:
2439                 core_info->core_dmodel = PR_MODEL_ILP32;
2440                 break;
2441         case ELFCLASS64:
2442                 core_info->core_dmodel = PR_MODEL_LP64;
2443                 break;
2444         default:
2445                 *perr = G_FORMAT;
2446                 goto err;
2447         }
2448         core_info->core_osabi = core.e_hdr.e_ident[EI_OSABI];
2449 
2450         /*
2451          * Because the core file may be a large file, we can't use libelf to
2452          * read the Phdrs.  We use e_phnum and e_phentsize to simplify things.
2453          */
2454         nbytes = core.e_hdr.e_phnum * core.e_hdr.e_phentsize;
2455 
2456         if ((phbuf = malloc(nbytes)) == NULL) {
2457                 *perr = G_STRANGE;
2458                 goto err;
2459         }
2460 
2461         if (pread64(core_fd, phbuf, nbytes, core.e_hdr.e_phoff) != nbytes) {
2462                 *perr = G_STRANGE;
2463                 free(phbuf);
2464                 goto err;
2465         }
2466 
2467         /*
2468          * Iterate through the program headers in the core file.
2469          * We're interested in two types of Phdrs: PT_NOTE (which
2470          * contains a set of saved /proc structures), and PT_LOAD (which
2471          * represents a memory mapping from the process's address space).
2472          * In the case of PT_NOTE, we're interested in the last PT_NOTE
2473          * in the core file; currently the first PT_NOTE (if present)
2474          * contains /proc structs in the pre-2.6 unstructured /proc format.
2475          */
2476         for (php = phbuf, notes = 0, i = 0; i < core.e_hdr.e_phnum; i++) {
2477                 if (core.e_hdr.e_ident[EI_CLASS] == ELFCLASS64)
2478                         (void) memcpy(&phdr, php, sizeof (GElf_Phdr));
2479                 else
2480                         core_phdr_to_gelf(php, &phdr);
2481 
2482                 switch (phdr.p_type) {
2483                 case PT_NOTE:
2484                         note_phdr = phdr;
2485                         notes++;
2486                         break;
2487 
2488                 case PT_LOAD:
2489                         if (core_add_mapping(P, &phdr) == -1) {
2490                                 *perr = G_STRANGE;
2491                                 free(phbuf);
2492                                 goto err;
2493                         }
2494                         break;
2495                 default:
2496                         dprintf("Pgrab_core: unknown phdr %d\n", phdr.p_type);
2497                         break;
2498                 }
2499 
2500                 php = (char *)php + core.e_hdr.e_phentsize;
2501         }
2502 
2503         free(phbuf);
2504 
2505         Psort_mappings(P);
2506 
2507         /*
2508          * If we couldn't find anything of type PT_NOTE, or only one PT_NOTE
2509          * was present, abort.  The core file is either corrupt or too old.
2510          */
2511         if (notes == 0 || (notes == 1 && core_info->core_osabi ==
2512             ELFOSABI_SOLARIS)) {
2513                 *perr = G_NOTE;
2514                 goto err;
2515         }
2516 
2517         /*
2518          * Advance the seek pointer to the start of the PT_NOTE data
2519          */
2520         if (lseek64(P->asfd, note_phdr.p_offset, SEEK_SET) == (off64_t)-1) {
2521                 dprintf("Pgrab_core: failed to lseek to PT_NOTE data\n");
2522                 *perr = G_STRANGE;
2523                 goto err;
2524         }
2525 
2526         /*
2527          * Now process the PT_NOTE structures.  Each one is preceded by
2528          * an Elf{32/64}_Nhdr structure describing its type and size.
2529          *
2530          *  +--------+
2531          *  | header |
2532          *  +--------+
2533          *  | name   |
2534          *  | ...    |
2535          *  +--------+
2536          *  | desc   |
2537          *  | ...    |
2538          *  +--------+
2539          */
2540         for (nleft = note_phdr.p_filesz; nleft > 0; ) {
2541                 Elf64_Nhdr nhdr;
2542                 off64_t off, namesz, descsz;
2543 
2544                 /*
2545                  * Although <sys/elf.h> defines both Elf32_Nhdr and Elf64_Nhdr
2546                  * as different types, they are both of the same content and
2547                  * size, so we don't need to worry about 32/64 conversion here.
2548                  */
2549                 if (read(P->asfd, &nhdr, sizeof (nhdr)) != sizeof (nhdr)) {
2550                         dprintf("Pgrab_core: failed to read ELF note header\n");
2551                         *perr = G_NOTE;
2552                         goto err;
2553                 }
2554 
2555                 /*
2556                  * According to the System V ABI, the amount of padding
2557                  * following the name field should align the description
2558                  * field on a 4 byte boundary for 32-bit binaries or on an 8
2559                  * byte boundary for 64-bit binaries. However, this change
2560                  * was not made correctly during the 64-bit port so all
2561                  * descriptions can assume only 4-byte alignment. We ignore
2562                  * the name field and the padding to 4-byte alignment.
2563                  */
2564                 namesz = P2ROUNDUP((off64_t)nhdr.n_namesz, (off64_t)4);
2565 
2566                 if (lseek64(P->asfd, namesz, SEEK_CUR) == (off64_t)-1) {
2567                         dprintf("failed to seek past name and padding\n");
2568                         *perr = G_STRANGE;
2569                         goto err;
2570                 }
2571 
2572                 dprintf("Note hdr n_type=%u n_namesz=%u n_descsz=%u\n",
2573                     nhdr.n_type, nhdr.n_namesz, nhdr.n_descsz);
2574 
2575                 off = lseek64(P->asfd, (off64_t)0L, SEEK_CUR);
2576 
2577                 /*
2578                  * Invoke the note handler function from our table
2579                  */
2580                 if (nhdr.n_type < sizeof (nhdlrs) / sizeof (nhdlrs[0])) {
2581                         if (nhdlrs[nhdr.n_type](P, nhdr.n_descsz) < 0) {
2582                                 dprintf("handler for type %d returned < 0",
2583                                     nhdr.n_type);
2584                                 *perr = G_NOTE;
2585                                 goto err;
2586                         }
2587                         /*
2588                          * The presence of either of these notes indicates that
2589                          * the dump was generated on Linux.
2590                          */
2591 #ifdef __x86
2592                         if (nhdr.n_type == NT_PRSTATUS ||
2593                             nhdr.n_type == NT_PRPSINFO)
2594                                 from_linux = B_TRUE;
2595 #endif
2596                 } else {
2597                         (void) note_notsup(P, nhdr.n_descsz);
2598                 }
2599 
2600                 /*
2601                  * Seek past the current note data to the next Elf_Nhdr
2602                  */
2603                 descsz = P2ROUNDUP((off64_t)nhdr.n_descsz, (off64_t)4);
2604                 if (lseek64(P->asfd, off + descsz, SEEK_SET) == (off64_t)-1) {
2605                         dprintf("Pgrab_core: failed to seek to next nhdr\n");
2606                         *perr = G_STRANGE;
2607                         goto err;
2608                 }
2609 
2610                 /*
2611                  * Subtract the size of the header and its data from what
2612                  * we have left to process.
2613                  */
2614                 nleft -= sizeof (nhdr) + namesz + descsz;
2615         }
2616 
2617 #ifdef __x86
2618         if (from_linux) {
2619                 size_t pid;
2620                 lwp_info_t *lwp;
2621 
2622                 P->status.pr_dmodel = core_info->core_dmodel;
2623 
2624                 pid = P->status.pr_pid;
2625 
2626                 for (lwp = list_head(&core_info->core_lwp_head); lwp != NULL;
2627                     lwp = list_next(&core_info->core_lwp_head, lwp)) {
2628                         dprintf("Linux thread with id %d\n", lwp->lwp_id);
2629 
2630                         /*
2631                          * In the case we don't have a valid psinfo (i.e. pid is
2632                          * 0, probably because of gdb creating the core) assume
2633                          * lowest pid count is the first thread (what if the
2634                          * next thread wraps the pid around?)
2635                          */
2636                         if (P->status.pr_pid == 0 &&
2637                             ((pid == 0 && lwp->lwp_id > 0) ||
2638                             (lwp->lwp_id < pid))) {
2639                                 pid = lwp->lwp_id;
2640                         }
2641                 }
2642 
2643                 if (P->status.pr_pid != pid) {
2644                         dprintf("No valid pid, setting to %ld\n", (ulong_t)pid);
2645                         P->status.pr_pid = pid;
2646                         P->psinfo.pr_pid = pid;
2647                 }
2648 
2649                 /*
2650                  * Consumers like mdb expect the first thread to actually have
2651                  * an id of 1, on linux that is actually the pid. Find the the
2652                  * thread with our process id, and set the id to 1
2653                  */
2654                 if ((lwp = lwpid2info(P, pid)) == NULL) {
2655                         dprintf("Couldn't find first thread\n");
2656                         *perr = G_STRANGE;
2657                         goto err;
2658                 }
2659 
2660                 dprintf("setting representative thread: %d\n", lwp->lwp_id);
2661 
2662                 lwp->lwp_id = 1;
2663                 lwp->lwp_status.pr_lwpid = 1;
2664 
2665                 /* set representative thread */
2666                 (void) memcpy(&P->status.pr_lwp, &lwp->lwp_status,
2667                     sizeof (P->status.pr_lwp));
2668         }
2669 #endif /* __x86 */
2670 
2671         if (nleft != 0) {
2672                 dprintf("Pgrab_core: note section malformed\n");
2673                 *perr = G_STRANGE;
2674                 goto err;
2675         }
2676 
2677         if ((pagesize = Pgetauxval(P, AT_PAGESZ)) == -1) {
2678                 pagesize = getpagesize();
2679                 dprintf("AT_PAGESZ missing; defaulting to %d\n", pagesize);
2680         }
2681 
2682         /*
2683          * Locate and label the mappings corresponding to the end of the
2684          * heap (MA_BREAK) and the base of the stack (MA_STACK).
2685          */
2686         if ((P->status.pr_brkbase != 0 || P->status.pr_brksize != 0) &&
2687             (brk_mp = Paddr2mptr(P, P->status.pr_brkbase +
2688             P->status.pr_brksize - 1)) != NULL)
2689                 brk_mp->map_pmap.pr_mflags |= MA_BREAK;
2690         else
2691                 brk_mp = NULL;
2692 
2693         if ((stk_mp = Paddr2mptr(P, P->status.pr_stkbase)) != NULL)
2694                 stk_mp->map_pmap.pr_mflags |= MA_STACK;
2695 
2696         /*
2697          * At this point, we have enough information to look for the
2698          * executable and open it: we have access to the auxv, a psinfo_t,
2699          * and the ability to read from mappings provided by the core file.
2700          */
2701         (void) Pfindexec(P, aout_path, core_exec_open, &aout);
2702         dprintf("P->execname = \"%s\"\n", P->execname ? P->execname : "NULL");
2703         execname = P->execname ? P->execname : "a.out";
2704 
2705         /*
2706          * Iterate through the sections, looking for the .dynamic and .interp
2707          * sections.  If we encounter them, remember their section pointers.
2708          */
2709         for (scn = NULL; (scn = elf_nextscn(aout.e_elf, scn)) != NULL; ) {
2710                 char *sname;
2711 
2712                 if ((gelf_getshdr(scn, &shdr) == NULL) ||
2713                     (sname = elf_strptr(aout.e_elf, aout.e_hdr.e_shstrndx,
2714                     (size_t)shdr.sh_name)) == NULL)
2715                         continue;
2716 
2717                 if (strcmp(sname, ".interp") == 0)
2718                         intp_scn = scn;
2719         }
2720 
2721         /*
2722          * Get the AT_BASE auxv element.  If this is missing (-1), then
2723          * we assume this is a statically-linked executable.
2724          */
2725         base_addr = Pgetauxval(P, AT_BASE);
2726 
2727         /*
2728          * In order to get librtld_db initialized, we'll need to identify
2729          * and name the mapping corresponding to the run-time linker.  The
2730          * AT_BASE auxv element tells us the address where it was mapped,
2731          * and the .interp section of the executable tells us its path.
2732          * If for some reason that doesn't pan out, just use ld.so.1.
2733          */
2734         if (intp_scn != NULL && (dp = elf_getdata(intp_scn, NULL)) != NULL &&
2735             dp->d_size != 0) {
2736                 dprintf(".interp = <%s>\n", (char *)dp->d_buf);
2737                 interp = dp->d_buf;
2738 
2739         } else if (base_addr != (uintptr_t)-1L) {
2740                 if (core_info->core_dmodel == PR_MODEL_LP64)
2741                         interp = "/usr/lib/64/ld.so.1";
2742                 else
2743                         interp = "/usr/lib/ld.so.1";
2744 
2745                 dprintf(".interp section is missing or could not be read; "
2746                     "defaulting to %s\n", interp);
2747         } else
2748                 dprintf("detected statically linked executable\n");
2749 
2750         /*
2751          * If we have an AT_BASE element, name the mapping at that address
2752          * using the interpreter pathname.  Name the corresponding data
2753          * mapping after the interpreter as well.
2754          */
2755         if (base_addr != (uintptr_t)-1L) {
2756                 elf_file_t intf;
2757 
2758                 P->map_ldso = core_name_mapping(P, base_addr, interp);
2759 
2760                 if (core_elf_open(&intf, interp, ET_DYN, NULL) == 0) {
2761                         rd_loadobj_t rl;
2762                         map_info_t *dmp;
2763 
2764                         rl.rl_base = base_addr;
2765                         dmp = core_find_data(P, intf.e_elf, &rl);
2766 
2767                         if (dmp != NULL) {
2768                                 dprintf("renamed data at %p to %s\n",
2769                                     (void *)rl.rl_data_base, interp);
2770                                 (void) strncpy(dmp->map_pmap.pr_mapname,
2771                                     interp, PRMAPSZ);
2772                                 dmp->map_pmap.pr_mapname[PRMAPSZ - 1] = '\0';
2773                         }
2774                 }
2775 
2776                 core_elf_close(&intf);
2777         }
2778 
2779         /*
2780          * If we have an AT_ENTRY element, name the mapping at that address
2781          * using the special name "a.out" just like /proc does.
2782          */
2783         if ((addr = Pgetauxval(P, AT_ENTRY)) != (uintptr_t)-1L)
2784                 P->map_exec = core_name_mapping(P, addr, "a.out");
2785 
2786         /*
2787          * If we're a statically linked executable (or we're on x86 and looking
2788          * at a Linux core dump), then just locate the executable's text and
2789          * data and name them after the executable.
2790          */
2791 #ifndef __x86
2792         if (base_addr == (uintptr_t)-1L) {
2793 #else
2794         if (base_addr == (uintptr_t)-1L || from_linux) {
2795 #endif
2796                 dprintf("looking for text and data: %s\n", execname);
2797                 map_info_t *tmp, *dmp;
2798                 file_info_t *fp;
2799                 rd_loadobj_t rl;
2800 
2801                 if ((tmp = core_find_text(P, aout.e_elf, &rl)) != NULL &&
2802                     (dmp = core_find_data(P, aout.e_elf, &rl)) != NULL) {
2803                         (void) strncpy(tmp->map_pmap.pr_mapname,
2804                             execname, PRMAPSZ);
2805                         tmp->map_pmap.pr_mapname[PRMAPSZ - 1] = '\0';
2806                         (void) strncpy(dmp->map_pmap.pr_mapname,
2807                             execname, PRMAPSZ);
2808                         dmp->map_pmap.pr_mapname[PRMAPSZ - 1] = '\0';
2809                 }
2810 
2811                 if ((P->map_exec = tmp) != NULL &&
2812                     (fp = malloc(sizeof (file_info_t))) != NULL) {
2813 
2814                         (void) memset(fp, 0, sizeof (file_info_t));
2815 
2816                         list_insert_head(&P->file_head, fp);
2817                         tmp->map_file = fp;
2818                         P->num_files++;
2819 
2820                         fp->file_ref = 1;
2821                         fp->file_fd = -1;
2822                         fp->file_dbgfile = -1;
2823 
2824                         fp->file_lo = malloc(sizeof (rd_loadobj_t));
2825                         fp->file_lname = strdup(execname);
2826 
2827                         if (fp->file_lo)
2828                                 *fp->file_lo = rl;
2829                         if (fp->file_lname)
2830                                 fp->file_lbase = basename(fp->file_lname);
2831                         if (fp->file_rname)
2832                                 fp->file_rbase = basename(fp->file_rname);
2833 
2834                         (void) strcpy(fp->file_pname,
2835                             P->mappings[0].map_pmap.pr_mapname);
2836                         fp->file_map = tmp;
2837 
2838                         Pbuild_file_symtab(P, fp);
2839 
2840                         if (dmp != NULL) {
2841                                 dmp->map_file = fp;
2842                                 fp->file_ref++;
2843                         }
2844                 }
2845         }
2846 
2847         core_elf_close(&aout);
2848 
2849         /*
2850          * We now have enough information to initialize librtld_db.
2851          * After it warms up, we can iterate through the load object chain
2852          * in the core, which will allow us to construct the file info
2853          * we need to provide symbol information for the other shared
2854          * libraries, and also to fill in the missing mapping names.
2855          */
2856         rd_log(_libproc_debug);
2857 
2858         if ((P->rap = rd_new(P)) != NULL) {
2859                 (void) rd_loadobj_iter(P->rap, (rl_iter_f *)
2860                     core_iter_mapping, P);
2861 
2862                 if (core_info->core_errno != 0) {
2863                         errno = core_info->core_errno;
2864                         *perr = G_STRANGE;
2865                         goto err;
2866                 }
2867         } else
2868                 dprintf("failed to initialize rtld_db agent\n");
2869 
2870         /*
2871          * If there are sections, load them and process the data from any
2872          * sections that we can use to annotate the file_info_t's.
2873          */
2874         core_load_shdrs(P, &core);
2875 
2876         /*
2877          * If we previously located a stack or break mapping, and they are
2878          * still anonymous, we now assume that they were MAP_ANON mappings.
2879          * If brk_mp turns out to now have a name, then the heap is still
2880          * sitting at the end of the executable's data+bss mapping: remove
2881          * the previous MA_BREAK setting to be consistent with /proc.
2882          */
2883         if (stk_mp != NULL && stk_mp->map_pmap.pr_mapname[0] == '\0')
2884                 stk_mp->map_pmap.pr_mflags |= MA_ANON;
2885         if (brk_mp != NULL && brk_mp->map_pmap.pr_mapname[0] == '\0')
2886                 brk_mp->map_pmap.pr_mflags |= MA_ANON;
2887         else if (brk_mp != NULL)
2888                 brk_mp->map_pmap.pr_mflags &= ~MA_BREAK;
2889 
2890         *perr = 0;
2891         return (P);
2892 
2893 err:
2894         Pfree(P);
2895         core_elf_close(&aout);
2896         return (NULL);
2897 }
2898 
2899 /*
2900  * Grab a core file using a pathname.  We just open it and call Pfgrab_core().
2901  */
2902 struct ps_prochandle *
2903 Pgrab_core(const char *core, const char *aout, int gflag, int *perr)
2904 {
2905         int fd, oflag = (gflag & PGRAB_RDONLY) ? O_RDONLY : O_RDWR;
2906 
2907         if ((fd = open64(core, oflag)) >= 0)
2908                 return (Pfgrab_core(fd, aout, perr));
2909 
2910         if (errno != ENOENT)
2911                 *perr = G_STRANGE;
2912         else
2913                 *perr = G_NOCORE;
2914 
2915         return (NULL);
2916 }
2917 
2918 int
2919 Pupanic(struct ps_prochandle *P, prupanic_t **pru)
2920 {
2921         core_info_t *core;
2922 
2923         if (P->state != PS_DEAD) {
2924                 errno = ENODATA;
2925                 return (-1);
2926         }
2927 
2928         core = P->data;
2929         if (core->core_upanic == NULL) {
2930                 errno = ENOENT;
2931                 return (-1);
2932         }
2933 
2934         if (core->core_upanic->pru_version != PRUPANIC_VERSION_1) {
2935                 errno = EINVAL;
2936                 return (-1);
2937         }
2938 
2939         if ((*pru = calloc(1, sizeof (prupanic_t))) == NULL)
2940                 return (-1);
2941         (void) memcpy(*pru, core->core_upanic, sizeof (prupanic_t));
2942 
2943         return (0);
2944 }
2945 
2946 void
2947 Pupanic_free(prupanic_t *pru)
2948 {
2949         free(pru);
2950 }