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