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 /*
  23  * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
  24  * Copyright (c) 2013, Joyent, Inc. All rights reserved.
  25  */
  26 
  27 #include <assert.h>
  28 #include <stdio.h>
  29 #include <stdlib.h>
  30 #include <stddef.h>
  31 #include <unistd.h>
  32 #include <ctype.h>
  33 #include <fcntl.h>
  34 #include <string.h>
  35 #include <strings.h>
  36 #include <memory.h>
  37 #include <errno.h>
  38 #include <dirent.h>
  39 #include <signal.h>
  40 #include <limits.h>
  41 #include <libgen.h>
  42 #include <sys/types.h>
  43 #include <sys/stat.h>
  44 #include <sys/systeminfo.h>
  45 #include <sys/sysmacros.h>
  46 
  47 #include "libproc.h"
  48 #include "Pcontrol.h"
  49 #include "Putil.h"
  50 #include "Psymtab_machelf.h"
  51 
  52 static file_info_t *build_map_symtab(struct ps_prochandle *, map_info_t *);
  53 static map_info_t *exec_map(struct ps_prochandle *);
  54 static map_info_t *object_to_map(struct ps_prochandle *, Lmid_t, const char *);
  55 static map_info_t *object_name_to_map(struct ps_prochandle *,
  56         Lmid_t, const char *);
  57 static GElf_Sym *sym_by_name(sym_tbl_t *, const char *, GElf_Sym *, uint_t *);
  58 static int read_ehdr32(struct ps_prochandle *, Elf32_Ehdr *, uint_t *,
  59     uintptr_t);
  60 #ifdef _LP64
  61 static int read_ehdr64(struct ps_prochandle *, Elf64_Ehdr *, uint_t *,
  62     uintptr_t);
  63 #endif
  64 
  65 #define DATA_TYPES      \
  66         ((1 << STT_OBJECT) | (1 << STT_FUNC) | \
  67         (1 << STT_COMMON) | (1 << STT_TLS))
  68 #define IS_DATA_TYPE(tp)        (((1 << (tp)) & DATA_TYPES) != 0)
  69 
  70 #define MA_RWX  (MA_READ | MA_WRITE | MA_EXEC)
  71 
  72 typedef enum {
  73         PRO_NATURAL,
  74         PRO_BYADDR,
  75         PRO_BYNAME
  76 } pr_order_t;
  77 
  78 static int
  79 addr_cmp(const void *aa, const void *bb)
  80 {
  81         uintptr_t a = *((uintptr_t *)aa);
  82         uintptr_t b = *((uintptr_t *)bb);
  83 
  84         if (a > b)
  85                 return (1);
  86         if (a < b)
  87                 return (-1);
  88         return (0);
  89 }
  90 
  91 /*
  92  * This function creates a list of addresses for a load object's sections.
  93  * The list is in ascending address order and alternates start address
  94  * then end address for each section we're interested in. The function
  95  * returns a pointer to the list, which must be freed by the caller.
  96  */
  97 static uintptr_t *
  98 get_saddrs(struct ps_prochandle *P, uintptr_t ehdr_start, uint_t *n)
  99 {
 100         uintptr_t a, addr, *addrs, last = 0;
 101         uint_t i, naddrs = 0, unordered = 0;
 102 
 103         if (P->status.pr_dmodel == PR_MODEL_ILP32) {
 104                 Elf32_Ehdr ehdr;
 105                 Elf32_Phdr phdr;
 106                 uint_t phnum;
 107 
 108                 if (read_ehdr32(P, &ehdr, &phnum, ehdr_start) != 0)
 109                         return (NULL);
 110 
 111                 addrs = malloc(sizeof (uintptr_t) * phnum * 2);
 112                 a = ehdr_start + ehdr.e_phoff;
 113                 for (i = 0; i < phnum; i++, a += ehdr.e_phentsize) {
 114                         if (Pread(P, &phdr, sizeof (phdr), a) !=
 115                             sizeof (phdr)) {
 116                                 free(addrs);
 117                                 return (NULL);
 118                         }
 119                         if (phdr.p_type != PT_LOAD || phdr.p_memsz == 0)
 120                                 continue;
 121 
 122                         addr = phdr.p_vaddr;
 123                         if (ehdr.e_type == ET_DYN)
 124                                 addr += ehdr_start;
 125                         if (last > addr)
 126                                 unordered = 1;
 127                         addrs[naddrs++] = addr;
 128                         addrs[naddrs++] = last = addr + phdr.p_memsz - 1;
 129                 }
 130 #ifdef _LP64
 131         } else {
 132                 Elf64_Ehdr ehdr;
 133                 Elf64_Phdr phdr;
 134                 uint_t phnum;
 135 
 136                 if (read_ehdr64(P, &ehdr, &phnum, ehdr_start) != 0)
 137                         return (NULL);
 138 
 139                 addrs = malloc(sizeof (uintptr_t) * phnum * 2);
 140                 a = ehdr_start + ehdr.e_phoff;
 141                 for (i = 0; i < phnum; i++, a += ehdr.e_phentsize) {
 142                         if (Pread(P, &phdr, sizeof (phdr), a) !=
 143                             sizeof (phdr)) {
 144                                 free(addrs);
 145                                 return (NULL);
 146                         }
 147                         if (phdr.p_type != PT_LOAD || phdr.p_memsz == 0)
 148                                 continue;
 149 
 150                         addr = phdr.p_vaddr;
 151                         if (ehdr.e_type == ET_DYN)
 152                                 addr += ehdr_start;
 153                         if (last > addr)
 154                                 unordered = 1;
 155                         addrs[naddrs++] = addr;
 156                         addrs[naddrs++] = last = addr + phdr.p_memsz - 1;
 157                 }
 158 #endif
 159         }
 160 
 161         if (unordered)
 162                 qsort(addrs, naddrs, sizeof (uintptr_t), addr_cmp);
 163 
 164         *n = naddrs;
 165         return (addrs);
 166 }
 167 
 168 /*
 169  * Allocation function for a new file_info_t
 170  */
 171 file_info_t *
 172 file_info_new(struct ps_prochandle *P, map_info_t *mptr)
 173 {
 174         file_info_t *fptr;
 175         map_info_t *mp;
 176         uintptr_t mstart, mend, sstart, send;
 177         uint_t i;
 178 
 179         if ((fptr = calloc(1, sizeof (file_info_t))) == NULL)
 180                 return (NULL);
 181 
 182         list_link(fptr, &P->file_head);
 183         (void) strcpy(fptr->file_pname, mptr->map_pmap.pr_mapname);
 184         mptr->map_file = fptr;
 185         fptr->file_ref = 1;
 186         fptr->file_fd = -1;
 187         P->num_files++;
 188 
 189         /*
 190          * To figure out which map_info_t instances correspond to the mappings
 191          * for this load object we try to obtain the start and end address
 192          * for each section of our in-memory ELF image. If successful, we
 193          * walk down the list of addresses and the list of map_info_t
 194          * instances in lock step to correctly find the mappings that
 195          * correspond to this load object.
 196          */
 197         if ((fptr->file_saddrs = get_saddrs(P, mptr->map_pmap.pr_vaddr,
 198             &fptr->file_nsaddrs)) == NULL)
 199                 return (fptr);
 200 
 201         mp = P->mappings;
 202         i = 0;
 203         while (mp < P->mappings + P->map_count && i < fptr->file_nsaddrs) {
 204 
 205                 /* Calculate the start and end of the mapping and section */
 206                 mstart = mp->map_pmap.pr_vaddr;
 207                 mend = mp->map_pmap.pr_vaddr + mp->map_pmap.pr_size;
 208                 sstart = fptr->file_saddrs[i];
 209                 send = fptr->file_saddrs[i + 1];
 210 
 211                 if (mend <= sstart) {
 212                         /* This mapping is below the current section */
 213                         mp++;
 214                 } else if (mstart >= send) {
 215                         /* This mapping is above the current section */
 216                         i += 2;
 217                 } else {
 218                         /* This mapping overlaps the current section */
 219                         if (mp->map_file == NULL) {
 220                                 dprintf("file_info_new: associating "
 221                                     "segment at %p\n",
 222                                     (void *)mp->map_pmap.pr_vaddr);
 223                                 mp->map_file = fptr;
 224                                 fptr->file_ref++;
 225                         } else {
 226                                 dprintf("file_info_new: segment at %p "
 227                                     "already associated with %s\n",
 228                                     (void *)mp->map_pmap.pr_vaddr,
 229                                     (mp == mptr ? "this file" :
 230                                     mp->map_file->file_pname));
 231                         }
 232                         mp++;
 233                 }
 234         }
 235 
 236         return (fptr);
 237 }
 238 
 239 /*
 240  * Deallocation function for a file_info_t
 241  */
 242 static void
 243 file_info_free(struct ps_prochandle *P, file_info_t *fptr)
 244 {
 245         if (--fptr->file_ref == 0) {
 246                 list_unlink(fptr);
 247                 if (fptr->file_symtab.sym_elf) {
 248                         (void) elf_end(fptr->file_symtab.sym_elf);
 249                         free(fptr->file_symtab.sym_elfmem);
 250                 }
 251                 if (fptr->file_symtab.sym_byname)
 252                         free(fptr->file_symtab.sym_byname);
 253                 if (fptr->file_symtab.sym_byaddr)
 254                         free(fptr->file_symtab.sym_byaddr);
 255 
 256                 if (fptr->file_dynsym.sym_elf) {
 257                         (void) elf_end(fptr->file_dynsym.sym_elf);
 258                         free(fptr->file_dynsym.sym_elfmem);
 259                 }
 260                 if (fptr->file_dynsym.sym_byname)
 261                         free(fptr->file_dynsym.sym_byname);
 262                 if (fptr->file_dynsym.sym_byaddr)
 263                         free(fptr->file_dynsym.sym_byaddr);
 264 
 265                 if (fptr->file_lo)
 266                         free(fptr->file_lo);
 267                 if (fptr->file_lname)
 268                         free(fptr->file_lname);
 269                 if (fptr->file_rname)
 270                         free(fptr->file_rname);
 271                 if (fptr->file_elf)
 272                         (void) elf_end(fptr->file_elf);
 273                 if (fptr->file_elfmem != NULL)
 274                         free(fptr->file_elfmem);
 275                 if (fptr->file_fd >= 0)
 276                         (void) close(fptr->file_fd);
 277                 if (fptr->file_ctfp) {
 278                         ctf_close(fptr->file_ctfp);
 279                         free(fptr->file_ctf_buf);
 280                 }
 281                 if (fptr->file_saddrs)
 282                         free(fptr->file_saddrs);
 283                 free(fptr);
 284                 P->num_files--;
 285         }
 286 }
 287 
 288 /*
 289  * Deallocation function for a map_info_t
 290  */
 291 static void
 292 map_info_free(struct ps_prochandle *P, map_info_t *mptr)
 293 {
 294         file_info_t *fptr;
 295 
 296         if ((fptr = mptr->map_file) != NULL) {
 297                 if (fptr->file_map == mptr)
 298                         fptr->file_map = NULL;
 299                 file_info_free(P, fptr);
 300         }
 301         if (P->execname && mptr == P->map_exec) {
 302                 free(P->execname);
 303                 P->execname = NULL;
 304         }
 305         if (P->auxv && (mptr == P->map_exec || mptr == P->map_ldso)) {
 306                 free(P->auxv);
 307                 P->auxv = NULL;
 308                 P->nauxv = 0;
 309         }
 310         if (mptr == P->map_exec)
 311                 P->map_exec = NULL;
 312         if (mptr == P->map_ldso)
 313                 P->map_ldso = NULL;
 314 }
 315 
 316 /*
 317  * Call-back function for librtld_db to iterate through all of its shared
 318  * libraries.  We use this to get the load object names for the mappings.
 319  */
 320 static int
 321 map_iter(const rd_loadobj_t *lop, void *cd)
 322 {
 323         char buf[PATH_MAX];
 324         struct ps_prochandle *P = cd;
 325         map_info_t *mptr;
 326         file_info_t *fptr;
 327 
 328         dprintf("encountered rd object at %p\n", (void *)lop->rl_base);
 329 
 330         if ((mptr = Paddr2mptr(P, lop->rl_base)) == NULL) {
 331                 dprintf("map_iter: base address doesn't match any mapping\n");
 332                 return (1); /* Base address does not match any mapping */
 333         }
 334 
 335         if ((fptr = mptr->map_file) == NULL &&
 336             (fptr = file_info_new(P, mptr)) == NULL) {
 337                 dprintf("map_iter: failed to allocate a new file_info_t\n");
 338                 return (1); /* Failed to allocate a new file_info_t */
 339         }
 340 
 341         if ((fptr->file_lo == NULL) &&
 342             (fptr->file_lo = malloc(sizeof (rd_loadobj_t))) == NULL) {
 343                 dprintf("map_iter: failed to allocate rd_loadobj_t\n");
 344                 file_info_free(P, fptr);
 345                 return (1); /* Failed to allocate rd_loadobj_t */
 346         }
 347 
 348         fptr->file_map = mptr;
 349         *fptr->file_lo = *lop;
 350 
 351         fptr->file_lo->rl_plt_base = fptr->file_plt_base;
 352         fptr->file_lo->rl_plt_size = fptr->file_plt_size;
 353 
 354         if (fptr->file_lname) {
 355                 free(fptr->file_lname);
 356                 fptr->file_lname = NULL;
 357                 fptr->file_lbase = NULL;
 358         }
 359         if (fptr->file_rname) {
 360                 free(fptr->file_rname);
 361                 fptr->file_rname = NULL;
 362                 fptr->file_rbase = NULL;
 363         }
 364 
 365         if (Pread_string(P, buf, sizeof (buf), lop->rl_nameaddr) > 0) {
 366                 if ((fptr->file_lname = strdup(buf)) != NULL)
 367                         fptr->file_lbase = basename(fptr->file_lname);
 368         } else {
 369                 dprintf("map_iter: failed to read string at %p\n",
 370                     (void *)lop->rl_nameaddr);
 371         }
 372 
 373         if ((Pfindmap(P, mptr, buf, sizeof (buf)) != NULL) &&
 374             ((fptr->file_rname = strdup(buf)) != NULL))
 375                 fptr->file_rbase = basename(fptr->file_rname);
 376 
 377         dprintf("loaded rd object %s lmid %lx\n",
 378             fptr->file_lname ? buf : "<NULL>", lop->rl_lmident);
 379         return (1);
 380 }
 381 
 382 static void
 383 map_set(struct ps_prochandle *P, map_info_t *mptr, const char *lname)
 384 {
 385         file_info_t *fptr;
 386         char buf[PATH_MAX];
 387 
 388         if ((fptr = mptr->map_file) == NULL &&
 389             (fptr = file_info_new(P, mptr)) == NULL)
 390                 return; /* Failed to allocate a new file_info_t */
 391 
 392         fptr->file_map = mptr;
 393 
 394         if ((fptr->file_lo == NULL) &&
 395             (fptr->file_lo = malloc(sizeof (rd_loadobj_t))) == NULL) {
 396                 file_info_free(P, fptr);
 397                 return; /* Failed to allocate rd_loadobj_t */
 398         }
 399 
 400         (void) memset(fptr->file_lo, 0, sizeof (rd_loadobj_t));
 401         fptr->file_lo->rl_base = mptr->map_pmap.pr_vaddr;
 402         fptr->file_lo->rl_bend =
 403             mptr->map_pmap.pr_vaddr + mptr->map_pmap.pr_size;
 404 
 405         fptr->file_lo->rl_plt_base = fptr->file_plt_base;
 406         fptr->file_lo->rl_plt_size = fptr->file_plt_size;
 407 
 408         if ((fptr->file_lname == NULL) &&
 409             (fptr->file_lname = strdup(lname)) != NULL)
 410                 fptr->file_lbase = basename(fptr->file_lname);
 411 
 412         if ((Pfindmap(P, mptr, buf, sizeof (buf)) != NULL) &&
 413             ((fptr->file_rname = strdup(buf)) != NULL))
 414                 fptr->file_rbase = basename(fptr->file_rname);
 415 }
 416 
 417 static void
 418 load_static_maps(struct ps_prochandle *P)
 419 {
 420         map_info_t *mptr;
 421 
 422         /*
 423          * Construct the map for the a.out.
 424          */
 425         if ((mptr = object_name_to_map(P, PR_LMID_EVERY, PR_OBJ_EXEC)) != NULL)
 426                 map_set(P, mptr, "a.out");
 427 
 428         /*
 429          * If the dynamic linker exists for this process,
 430          * construct the map for it.
 431          */
 432         if (Pgetauxval(P, AT_BASE) != -1L &&
 433             (mptr = object_name_to_map(P, PR_LMID_EVERY, PR_OBJ_LDSO)) != NULL)
 434                 map_set(P, mptr, "ld.so.1");
 435 }
 436 
 437 /*
 438  * Go through all the address space mappings, validating or updating
 439  * the information already gathered, or gathering new information.
 440  *
 441  * This function is only called when we suspect that the mappings have changed
 442  * because this is the first time we're calling it or because of rtld activity.
 443  */
 444 void
 445 Pupdate_maps(struct ps_prochandle *P)
 446 {
 447         char mapfile[PATH_MAX];
 448         int mapfd;
 449         struct stat statb;
 450         prmap_t *Pmap = NULL;
 451         prmap_t *pmap;
 452         ssize_t nmap;
 453         int i;
 454         uint_t oldmapcount;
 455         map_info_t *newmap, *newp;
 456         map_info_t *mptr;
 457 
 458         if (P->info_valid || P->state == PS_UNDEAD)
 459                 return;
 460 
 461         Preadauxvec(P);
 462 
 463         (void) snprintf(mapfile, sizeof (mapfile), "%s/%d/map",
 464             procfs_path, (int)P->pid);
 465         if ((mapfd = open(mapfile, O_RDONLY)) < 0 ||
 466             fstat(mapfd, &statb) != 0 ||
 467             statb.st_size < sizeof (prmap_t) ||
 468             (Pmap = malloc(statb.st_size)) == NULL ||
 469             (nmap = pread(mapfd, Pmap, statb.st_size, 0L)) <= 0 ||
 470             (nmap /= sizeof (prmap_t)) == 0) {
 471                 if (Pmap != NULL)
 472                         free(Pmap);
 473                 if (mapfd >= 0)
 474                         (void) close(mapfd);
 475                 Preset_maps(P); /* utter failure; destroy tables */
 476                 return;
 477         }
 478         (void) close(mapfd);
 479 
 480         if ((newmap = calloc(1, nmap * sizeof (map_info_t))) == NULL)
 481                 return;
 482 
 483         /*
 484          * We try to merge any file information we may have for existing
 485          * mappings, to avoid having to rebuild the file info.
 486          */
 487         mptr = P->mappings;
 488         pmap = Pmap;
 489         newp = newmap;
 490         oldmapcount = P->map_count;
 491         for (i = 0; i < nmap; i++, pmap++, newp++) {
 492 
 493                 if (oldmapcount == 0) {
 494                         /*
 495                          * We've exhausted all the old mappings.  Every new
 496                          * mapping should be added.
 497                          */
 498                         newp->map_pmap = *pmap;
 499 
 500                 } else if (pmap->pr_vaddr == mptr->map_pmap.pr_vaddr &&
 501                     pmap->pr_size == mptr->map_pmap.pr_size &&
 502                     pmap->pr_offset == mptr->map_pmap.pr_offset &&
 503                     (pmap->pr_mflags & ~(MA_BREAK | MA_STACK)) ==
 504                     (mptr->map_pmap.pr_mflags & ~(MA_BREAK | MA_STACK)) &&
 505                     pmap->pr_pagesize == mptr->map_pmap.pr_pagesize &&
 506                     pmap->pr_shmid == mptr->map_pmap.pr_shmid &&
 507                     strcmp(pmap->pr_mapname, mptr->map_pmap.pr_mapname) == 0) {
 508 
 509                         /*
 510                          * This mapping matches exactly.  Copy over the old
 511                          * mapping, taking care to get the latest flags.
 512                          * Make sure the associated file_info_t is updated
 513                          * appropriately.
 514                          */
 515                         *newp = *mptr;
 516                         if (P->map_exec == mptr)
 517                                 P->map_exec = newp;
 518                         if (P->map_ldso == mptr)
 519                                 P->map_ldso = newp;
 520                         newp->map_pmap.pr_mflags = pmap->pr_mflags;
 521                         if (mptr->map_file != NULL &&
 522                             mptr->map_file->file_map == mptr)
 523                                 mptr->map_file->file_map = newp;
 524                         oldmapcount--;
 525                         mptr++;
 526 
 527                 } else if (pmap->pr_vaddr + pmap->pr_size >
 528                     mptr->map_pmap.pr_vaddr) {
 529 
 530                         /*
 531                          * The old mapping doesn't exist any more, remove it
 532                          * from the list.
 533                          */
 534                         map_info_free(P, mptr);
 535                         oldmapcount--;
 536                         i--;
 537                         newp--;
 538                         pmap--;
 539                         mptr++;
 540 
 541                 } else {
 542 
 543                         /*
 544                          * This is a new mapping, add it directly.
 545                          */
 546                         newp->map_pmap = *pmap;
 547                 }
 548         }
 549 
 550         /*
 551          * Free any old maps
 552          */
 553         while (oldmapcount) {
 554                 map_info_free(P, mptr);
 555                 oldmapcount--;
 556                 mptr++;
 557         }
 558 
 559         free(Pmap);
 560         if (P->mappings != NULL)
 561                 free(P->mappings);
 562         P->mappings = newmap;
 563         P->map_count = P->map_alloc = nmap;
 564         P->info_valid = 1;
 565 
 566         /*
 567          * Consult librtld_db to get the load object
 568          * names for all of the shared libraries.
 569          */
 570         if (P->rap != NULL)
 571                 (void) rd_loadobj_iter(P->rap, map_iter, P);
 572 }
 573 
 574 /*
 575  * Update all of the mappings and rtld_db as if by Pupdate_maps(), and then
 576  * forcibly cache all of the symbol tables associated with all object files.
 577  */
 578 void
 579 Pupdate_syms(struct ps_prochandle *P)
 580 {
 581         file_info_t *fptr;
 582         int i;
 583 
 584         Pupdate_maps(P);
 585 
 586         for (i = 0, fptr = list_next(&P->file_head); i < P->num_files;
 587             i++, fptr = list_next(fptr)) {
 588                 Pbuild_file_symtab(P, fptr);
 589                 (void) Pbuild_file_ctf(P, fptr);
 590         }
 591 }
 592 
 593 /*
 594  * Return the librtld_db agent handle for the victim process.
 595  * The handle will become invalid at the next successful exec() and the
 596  * client (caller of proc_rd_agent()) must not use it beyond that point.
 597  * If the process is already dead, we've already tried our best to
 598  * create the agent during core file initialization.
 599  */
 600 rd_agent_t *
 601 Prd_agent(struct ps_prochandle *P)
 602 {
 603         if (P->rap == NULL && P->state != PS_DEAD && P->state != PS_IDLE) {
 604                 Pupdate_maps(P);
 605                 if (P->num_files == 0)
 606                         load_static_maps(P);
 607                 rd_log(_libproc_debug);
 608                 if ((P->rap = rd_new(P)) != NULL)
 609                         (void) rd_loadobj_iter(P->rap, map_iter, P);
 610         }
 611         return (P->rap);
 612 }
 613 
 614 /*
 615  * Return the prmap_t structure containing 'addr', but only if it
 616  * is in the dynamic linker's link map and is the text section.
 617  */
 618 const prmap_t *
 619 Paddr_to_text_map(struct ps_prochandle *P, uintptr_t addr)
 620 {
 621         map_info_t *mptr;
 622 
 623         if (!P->info_valid)
 624                 Pupdate_maps(P);
 625 
 626         if ((mptr = Paddr2mptr(P, addr)) != NULL) {
 627                 file_info_t *fptr = build_map_symtab(P, mptr);
 628                 const prmap_t *pmp = &mptr->map_pmap;
 629 
 630                 /*
 631                  * Assume that if rl_data_base is NULL, it means that no
 632                  * data section was found for this load object, and that
 633                  * a section must be text. Otherwise, a section will be
 634                  * text unless it ends above the start of the data
 635                  * section.
 636                  */
 637                 if (fptr != NULL && fptr->file_lo != NULL &&
 638                     (fptr->file_lo->rl_data_base == NULL ||
 639                     pmp->pr_vaddr + pmp->pr_size <=
 640                     fptr->file_lo->rl_data_base))
 641                         return (pmp);
 642         }
 643 
 644         return (NULL);
 645 }
 646 
 647 /*
 648  * Return the prmap_t structure containing 'addr' (no restrictions on
 649  * the type of mapping).
 650  */
 651 const prmap_t *
 652 Paddr_to_map(struct ps_prochandle *P, uintptr_t addr)
 653 {
 654         map_info_t *mptr;
 655 
 656         if (!P->info_valid)
 657                 Pupdate_maps(P);
 658 
 659         if ((mptr = Paddr2mptr(P, addr)) != NULL)
 660                 return (&mptr->map_pmap);
 661 
 662         return (NULL);
 663 }
 664 
 665 /*
 666  * Convert a full or partial load object name to the prmap_t for its
 667  * corresponding primary text mapping.
 668  */
 669 const prmap_t *
 670 Plmid_to_map(struct ps_prochandle *P, Lmid_t lmid, const char *name)
 671 {
 672         map_info_t *mptr;
 673 
 674         if (name == PR_OBJ_EVERY)
 675                 return (NULL); /* A reasonable mistake */
 676 
 677         if ((mptr = object_name_to_map(P, lmid, name)) != NULL)
 678                 return (&mptr->map_pmap);
 679 
 680         return (NULL);
 681 }
 682 
 683 const prmap_t *
 684 Pname_to_map(struct ps_prochandle *P, const char *name)
 685 {
 686         return (Plmid_to_map(P, PR_LMID_EVERY, name));
 687 }
 688 
 689 const rd_loadobj_t *
 690 Paddr_to_loadobj(struct ps_prochandle *P, uintptr_t addr)
 691 {
 692         map_info_t *mptr;
 693 
 694         if (!P->info_valid)
 695                 Pupdate_maps(P);
 696 
 697         if ((mptr = Paddr2mptr(P, addr)) == NULL)
 698                 return (NULL);
 699 
 700         /*
 701          * By building the symbol table, we implicitly bring the PLT
 702          * information up to date in the load object.
 703          */
 704         (void) build_map_symtab(P, mptr);
 705 
 706         return (mptr->map_file->file_lo);
 707 }
 708 
 709 const rd_loadobj_t *
 710 Plmid_to_loadobj(struct ps_prochandle *P, Lmid_t lmid, const char *name)
 711 {
 712         map_info_t *mptr;
 713 
 714         if (name == PR_OBJ_EVERY)
 715                 return (NULL);
 716 
 717         if ((mptr = object_name_to_map(P, lmid, name)) == NULL)
 718                 return (NULL);
 719 
 720         /*
 721          * By building the symbol table, we implicitly bring the PLT
 722          * information up to date in the load object.
 723          */
 724         (void) build_map_symtab(P, mptr);
 725 
 726         return (mptr->map_file->file_lo);
 727 }
 728 
 729 const rd_loadobj_t *
 730 Pname_to_loadobj(struct ps_prochandle *P, const char *name)
 731 {
 732         return (Plmid_to_loadobj(P, PR_LMID_EVERY, name));
 733 }
 734 
 735 ctf_file_t *
 736 Pbuild_file_ctf(struct ps_prochandle *P, file_info_t *fptr)
 737 {
 738         ctf_sect_t ctdata, symtab, strtab;
 739         sym_tbl_t *symp;
 740         int err;
 741 
 742         if (fptr->file_ctfp != NULL)
 743                 return (fptr->file_ctfp);
 744 
 745         Pbuild_file_symtab(P, fptr);
 746 
 747         if (fptr->file_ctf_size == 0)
 748                 return (NULL);
 749 
 750         symp = fptr->file_ctf_dyn ? &fptr->file_dynsym : &fptr->file_symtab;
 751         if (symp->sym_data_pri == NULL)
 752                 return (NULL);
 753 
 754         /*
 755          * The buffer may alread be allocated if this is a core file that
 756          * contained CTF data for this file.
 757          */
 758         if (fptr->file_ctf_buf == NULL) {
 759                 fptr->file_ctf_buf = malloc(fptr->file_ctf_size);
 760                 if (fptr->file_ctf_buf == NULL) {
 761                         dprintf("failed to allocate ctf buffer\n");
 762                         return (NULL);
 763                 }
 764 
 765                 if (pread(fptr->file_fd, fptr->file_ctf_buf,
 766                     fptr->file_ctf_size, fptr->file_ctf_off) !=
 767                     fptr->file_ctf_size) {
 768                         free(fptr->file_ctf_buf);
 769                         fptr->file_ctf_buf = NULL;
 770                         dprintf("failed to read ctf data\n");
 771                         return (NULL);
 772                 }
 773         }
 774 
 775         ctdata.cts_name = ".SUNW_ctf";
 776         ctdata.cts_type = SHT_PROGBITS;
 777         ctdata.cts_flags = 0;
 778         ctdata.cts_data = fptr->file_ctf_buf;
 779         ctdata.cts_size = fptr->file_ctf_size;
 780         ctdata.cts_entsize = 1;
 781         ctdata.cts_offset = 0;
 782 
 783         symtab.cts_name = fptr->file_ctf_dyn ? ".dynsym" : ".symtab";
 784         symtab.cts_type = symp->sym_hdr_pri.sh_type;
 785         symtab.cts_flags = symp->sym_hdr_pri.sh_flags;
 786         symtab.cts_data = symp->sym_data_pri->d_buf;
 787         symtab.cts_size = symp->sym_hdr_pri.sh_size;
 788         symtab.cts_entsize = symp->sym_hdr_pri.sh_entsize;
 789         symtab.cts_offset = symp->sym_hdr_pri.sh_offset;
 790 
 791         strtab.cts_name = fptr->file_ctf_dyn ? ".dynstr" : ".strtab";
 792         strtab.cts_type = symp->sym_strhdr.sh_type;
 793         strtab.cts_flags = symp->sym_strhdr.sh_flags;
 794         strtab.cts_data = symp->sym_strs;
 795         strtab.cts_size = symp->sym_strhdr.sh_size;
 796         strtab.cts_entsize = symp->sym_strhdr.sh_entsize;
 797         strtab.cts_offset = symp->sym_strhdr.sh_offset;
 798 
 799         fptr->file_ctfp = ctf_bufopen(&ctdata, &symtab, &strtab, &err);
 800         if (fptr->file_ctfp == NULL) {
 801                 dprintf("ctf_bufopen() failed, error code %d\n", err);
 802                 free(fptr->file_ctf_buf);
 803                 fptr->file_ctf_buf = NULL;
 804                 return (NULL);
 805         }
 806 
 807         dprintf("loaded %lu bytes of CTF data for %s\n",
 808             (ulong_t)fptr->file_ctf_size, fptr->file_pname);
 809 
 810         return (fptr->file_ctfp);
 811 }
 812 
 813 ctf_file_t *
 814 Paddr_to_ctf(struct ps_prochandle *P, uintptr_t addr)
 815 {
 816         map_info_t *mptr;
 817         file_info_t *fptr;
 818 
 819         if (!P->info_valid)
 820                 Pupdate_maps(P);
 821 
 822         if ((mptr = Paddr2mptr(P, addr)) == NULL ||
 823             (fptr = mptr->map_file) == NULL)
 824                 return (NULL);
 825 
 826         return (Pbuild_file_ctf(P, fptr));
 827 }
 828 
 829 ctf_file_t *
 830 Plmid_to_ctf(struct ps_prochandle *P, Lmid_t lmid, const char *name)
 831 {
 832         map_info_t *mptr;
 833         file_info_t *fptr;
 834 
 835         if (name == PR_OBJ_EVERY)
 836                 return (NULL);
 837 
 838         if ((mptr = object_name_to_map(P, lmid, name)) == NULL ||
 839             (fptr = mptr->map_file) == NULL)
 840                 return (NULL);
 841 
 842         return (Pbuild_file_ctf(P, fptr));
 843 }
 844 
 845 ctf_file_t *
 846 Pname_to_ctf(struct ps_prochandle *P, const char *name)
 847 {
 848         return (Plmid_to_ctf(P, PR_LMID_EVERY, name));
 849 }
 850 
 851 /*
 852  * If we're not a core file, re-read the /proc/<pid>/auxv file and store
 853  * its contents in P->auxv.  In the case of a core file, we either
 854  * initialized P->auxv in Pcore() from the NT_AUXV, or we don't have an
 855  * auxv because the note was missing.
 856  */
 857 void
 858 Preadauxvec(struct ps_prochandle *P)
 859 {
 860         char auxfile[64];
 861         struct stat statb;
 862         ssize_t naux;
 863         int fd;
 864 
 865         if (P->state == PS_DEAD)
 866                 return; /* Already read during Pgrab_core() */
 867         if (P->state == PS_IDLE)
 868                 return; /* No aux vec for Pgrab_file() */
 869 
 870         if (P->auxv != NULL) {
 871                 free(P->auxv);
 872                 P->auxv = NULL;
 873                 P->nauxv = 0;
 874         }
 875 
 876         (void) snprintf(auxfile, sizeof (auxfile), "%s/%d/auxv",
 877             procfs_path, (int)P->pid);
 878         if ((fd = open(auxfile, O_RDONLY)) < 0)
 879                 return;
 880 
 881         if (fstat(fd, &statb) == 0 &&
 882             statb.st_size >= sizeof (auxv_t) &&
 883             (P->auxv = malloc(statb.st_size + sizeof (auxv_t))) != NULL) {
 884                 if ((naux = read(fd, P->auxv, statb.st_size)) < 0 ||
 885                     (naux /= sizeof (auxv_t)) < 1) {
 886                         free(P->auxv);
 887                         P->auxv = NULL;
 888                 } else {
 889                         P->auxv[naux].a_type = AT_NULL;
 890                         P->auxv[naux].a_un.a_val = 0L;
 891                         P->nauxv = (int)naux;
 892                 }
 893         }
 894 
 895         (void) close(fd);
 896 }
 897 
 898 /*
 899  * Return a requested element from the process's aux vector.
 900  * Return -1 on failure (this is adequate for our purposes).
 901  */
 902 long
 903 Pgetauxval(struct ps_prochandle *P, int type)
 904 {
 905         auxv_t *auxv;
 906 
 907         if (P->auxv == NULL)
 908                 Preadauxvec(P);
 909 
 910         if (P->auxv == NULL)
 911                 return (-1);
 912 
 913         for (auxv = P->auxv; auxv->a_type != AT_NULL; auxv++) {
 914                 if (auxv->a_type == type)
 915                         return (auxv->a_un.a_val);
 916         }
 917 
 918         return (-1);
 919 }
 920 
 921 /*
 922  * Return a pointer to our internal copy of the process's aux vector.
 923  * The caller should not hold on to this pointer across any libproc calls.
 924  */
 925 const auxv_t *
 926 Pgetauxvec(struct ps_prochandle *P)
 927 {
 928         static const auxv_t empty = { AT_NULL, 0L };
 929 
 930         if (P->auxv == NULL)
 931                 Preadauxvec(P);
 932 
 933         if (P->auxv == NULL)
 934                 return (&empty);
 935 
 936         return (P->auxv);
 937 }
 938 
 939 /*
 940  * Return 1 if the given mapping corresponds to the given file_info_t's
 941  * load object; return 0 otherwise.
 942  */
 943 static int
 944 is_mapping_in_file(struct ps_prochandle *P, map_info_t *mptr, file_info_t *fptr)
 945 {
 946         prmap_t *pmap = &mptr->map_pmap;
 947         rd_loadobj_t *lop = fptr->file_lo;
 948         uint_t i;
 949         uintptr_t mstart, mend, sstart, send;
 950 
 951         /*
 952          * We can get for free the start address of the text and data
 953          * sections of the load object. Start by seeing if the mapping
 954          * encloses either of these.
 955          */
 956         if ((pmap->pr_vaddr <= lop->rl_base &&
 957             lop->rl_base < pmap->pr_vaddr + pmap->pr_size) ||
 958             (pmap->pr_vaddr <= lop->rl_data_base &&
 959             lop->rl_data_base < pmap->pr_vaddr + pmap->pr_size))
 960                 return (1);
 961 
 962         /*
 963          * It's still possible that this mapping correponds to the load
 964          * object. Consider the example of a mapping whose start and end
 965          * addresses correspond to those of the load object's text section.
 966          * If the mapping splits, e.g. as a result of a segment demotion,
 967          * then although both mappings are still backed by the same section,
 968          * only one will be seen to enclose that section's start address.
 969          * Thus, to be rigorous, we ask not whether this mapping encloses
 970          * the start of a section, but whether there exists a section that
 971          * overlaps this mapping.
 972          *
 973          * If we don't already have the section addresses, and we successfully
 974          * get them, then we cache them in case we come here again.
 975          */
 976         if (fptr->file_saddrs == NULL &&
 977             (fptr->file_saddrs = get_saddrs(P,
 978             fptr->file_map->map_pmap.pr_vaddr, &fptr->file_nsaddrs)) == NULL)
 979                 return (0);
 980 
 981         mstart = mptr->map_pmap.pr_vaddr;
 982         mend = mptr->map_pmap.pr_vaddr + mptr->map_pmap.pr_size;
 983         for (i = 0; i < fptr->file_nsaddrs; i += 2) {
 984                 /* Does this section overlap the mapping? */
 985                 sstart = fptr->file_saddrs[i];
 986                 send = fptr->file_saddrs[i + 1];
 987                 if (!(mend <= sstart || mstart >= send))
 988                         return (1);
 989         }
 990 
 991         return (0);
 992 }
 993 
 994 /*
 995  * Find or build the symbol table for the given mapping.
 996  */
 997 static file_info_t *
 998 build_map_symtab(struct ps_prochandle *P, map_info_t *mptr)
 999 {
1000         prmap_t *pmap = &mptr->map_pmap;
1001         file_info_t *fptr;
1002         uint_t i;
1003 
1004         if ((fptr = mptr->map_file) != NULL) {
1005                 Pbuild_file_symtab(P, fptr);
1006                 return (fptr);
1007         }
1008 
1009         if (pmap->pr_mapname[0] == '\0')
1010                 return (NULL);
1011 
1012         /*
1013          * Attempt to find a matching file.
1014          * (A file can be mapped at several different addresses.)
1015          */
1016         for (i = 0, fptr = list_next(&P->file_head); i < P->num_files;
1017             i++, fptr = list_next(fptr)) {
1018                 if (strcmp(fptr->file_pname, pmap->pr_mapname) == 0 &&
1019                     fptr->file_lo && is_mapping_in_file(P, mptr, fptr)) {
1020                         mptr->map_file = fptr;
1021                         fptr->file_ref++;
1022                         Pbuild_file_symtab(P, fptr);
1023                         return (fptr);
1024                 }
1025         }
1026 
1027         /*
1028          * If we need to create a new file_info structure, iterate
1029          * through the load objects in order to attempt to connect
1030          * this new file with its primary text mapping.  We again
1031          * need to handle ld.so as a special case because we need
1032          * to be able to bootstrap librtld_db.
1033          */
1034         if ((fptr = file_info_new(P, mptr)) == NULL)
1035                 return (NULL);
1036 
1037         if (P->map_ldso != mptr) {
1038                 if (P->rap != NULL)
1039                         (void) rd_loadobj_iter(P->rap, map_iter, P);
1040                 else
1041                         (void) Prd_agent(P);
1042         } else {
1043                 fptr->file_map = mptr;
1044         }
1045 
1046         /*
1047          * If librtld_db wasn't able to help us connect the file to a primary
1048          * text mapping, set file_map to the current mapping because we require
1049          * fptr->file_map to be set in Pbuild_file_symtab.  librtld_db may be
1050          * unaware of what's going on in the rare case that a legitimate ELF
1051          * file has been mmap(2)ed into the process address space *without*
1052          * the use of dlopen(3x).
1053          */
1054         if (fptr->file_map == NULL)
1055                 fptr->file_map = mptr;
1056 
1057         Pbuild_file_symtab(P, fptr);
1058 
1059         return (fptr);
1060 }
1061 
1062 static int
1063 read_ehdr32(struct ps_prochandle *P, Elf32_Ehdr *ehdr, uint_t *phnum,
1064     uintptr_t addr)
1065 {
1066         if (Pread(P, ehdr, sizeof (*ehdr), addr) != sizeof (*ehdr))
1067                 return (-1);
1068 
1069         if (ehdr->e_ident[EI_MAG0] != ELFMAG0 ||
1070             ehdr->e_ident[EI_MAG1] != ELFMAG1 ||
1071             ehdr->e_ident[EI_MAG2] != ELFMAG2 ||
1072             ehdr->e_ident[EI_MAG3] != ELFMAG3 ||
1073             ehdr->e_ident[EI_CLASS] != ELFCLASS32 ||
1074 #ifdef _BIG_ENDIAN
1075             ehdr->e_ident[EI_DATA] != ELFDATA2MSB ||
1076 #else
1077             ehdr->e_ident[EI_DATA] != ELFDATA2LSB ||
1078 #endif
1079             ehdr->e_ident[EI_VERSION] != EV_CURRENT)
1080                 return (-1);
1081 
1082         if ((*phnum = ehdr->e_phnum) == PN_XNUM) {
1083                 Elf32_Shdr shdr0;
1084 
1085                 if (ehdr->e_shoff == 0 || ehdr->e_shentsize < sizeof (shdr0) ||
1086                     Pread(P, &shdr0, sizeof (shdr0), addr + ehdr->e_shoff) !=
1087                     sizeof (shdr0))
1088                         return (-1);
1089 
1090                 if (shdr0.sh_info != 0)
1091                         *phnum = shdr0.sh_info;
1092         }
1093 
1094         return (0);
1095 }
1096 
1097 static int
1098 read_dynamic_phdr32(struct ps_prochandle *P, const Elf32_Ehdr *ehdr,
1099     uint_t phnum, Elf32_Phdr *phdr, uintptr_t addr)
1100 {
1101         uint_t i;
1102 
1103         for (i = 0; i < phnum; i++) {
1104                 uintptr_t a = addr + ehdr->e_phoff + i * ehdr->e_phentsize;
1105                 if (Pread(P, phdr, sizeof (*phdr), a) != sizeof (*phdr))
1106                         return (-1);
1107 
1108                 if (phdr->p_type == PT_DYNAMIC)
1109                         return (0);
1110         }
1111 
1112         return (-1);
1113 }
1114 
1115 #ifdef _LP64
1116 static int
1117 read_ehdr64(struct ps_prochandle *P, Elf64_Ehdr *ehdr, uint_t *phnum,
1118     uintptr_t addr)
1119 {
1120         if (Pread(P, ehdr, sizeof (Elf64_Ehdr), addr) != sizeof (Elf64_Ehdr))
1121                 return (-1);
1122 
1123         if (ehdr->e_ident[EI_MAG0] != ELFMAG0 ||
1124             ehdr->e_ident[EI_MAG1] != ELFMAG1 ||
1125             ehdr->e_ident[EI_MAG2] != ELFMAG2 ||
1126             ehdr->e_ident[EI_MAG3] != ELFMAG3 ||
1127             ehdr->e_ident[EI_CLASS] != ELFCLASS64 ||
1128 #ifdef _BIG_ENDIAN
1129             ehdr->e_ident[EI_DATA] != ELFDATA2MSB ||
1130 #else
1131             ehdr->e_ident[EI_DATA] != ELFDATA2LSB ||
1132 #endif
1133             ehdr->e_ident[EI_VERSION] != EV_CURRENT)
1134                 return (-1);
1135 
1136         if ((*phnum = ehdr->e_phnum) == PN_XNUM) {
1137                 Elf64_Shdr shdr0;
1138 
1139                 if (ehdr->e_shoff == 0 || ehdr->e_shentsize < sizeof (shdr0) ||
1140                     Pread(P, &shdr0, sizeof (shdr0), addr + ehdr->e_shoff) !=
1141                     sizeof (shdr0))
1142                         return (-1);
1143 
1144                 if (shdr0.sh_info != 0)
1145                         *phnum = shdr0.sh_info;
1146         }
1147 
1148         return (0);
1149 }
1150 
1151 static int
1152 read_dynamic_phdr64(struct ps_prochandle *P, const Elf64_Ehdr *ehdr,
1153     uint_t phnum, Elf64_Phdr *phdr, uintptr_t addr)
1154 {
1155         uint_t i;
1156 
1157         for (i = 0; i < phnum; i++) {
1158                 uintptr_t a = addr + ehdr->e_phoff + i * ehdr->e_phentsize;
1159                 if (Pread(P, phdr, sizeof (*phdr), a) != sizeof (*phdr))
1160                         return (-1);
1161 
1162                 if (phdr->p_type == PT_DYNAMIC)
1163                         return (0);
1164         }
1165 
1166         return (-1);
1167 }
1168 #endif  /* _LP64 */
1169 
1170 /*
1171  * The text segment for each load object contains the elf header and
1172  * program headers. We can use this information to determine if the
1173  * file that corresponds to the load object is the same file that
1174  * was loaded into the process's address space. There can be a discrepency
1175  * if a file is recompiled after the process is started or if the target
1176  * represents a core file from a differently configured system -- two
1177  * common examples. The DT_CHECKSUM entry in the dynamic section
1178  * provides an easy method of comparison. It is important to note that
1179  * the dynamic section usually lives in the data segment, but the meta
1180  * data we use to find the dynamic section lives in the text segment so
1181  * if either of those segments is absent we can't proceed.
1182  *
1183  * We're looking through the elf file for several items: the symbol tables
1184  * (both dynsym and symtab), the procedure linkage table (PLT) base,
1185  * size, and relocation base, and the CTF information. Most of this can
1186  * be recovered from the loaded image of the file itself, the exceptions
1187  * being the symtab and CTF data.
1188  *
1189  * First we try to open the file that we think corresponds to the load
1190  * object, if the DT_CHECKSUM values match, we're all set, and can simply
1191  * recover all the information we need from the file. If the values of
1192  * DT_CHECKSUM don't match, or if we can't access the file for whatever
1193  * reasaon, we fake up a elf file to use in its stead. If we can't read
1194  * the elf data in the process's address space, we fall back to using
1195  * the file even though it may give inaccurate information.
1196  *
1197  * The elf file that we fake up has to consist of sections for the
1198  * dynsym, the PLT and the dynamic section. Note that in the case of a
1199  * core file, we'll get the CTF data in the file_info_t later on from
1200  * a section embedded the core file (if it's present).
1201  *
1202  * file_differs() conservatively looks for mismatched files, identifying
1203  * a match when there is any ambiguity (since that's the legacy behavior).
1204  */
1205 static int
1206 file_differs(struct ps_prochandle *P, Elf *elf, file_info_t *fptr)
1207 {
1208         Elf_Scn *scn;
1209         GElf_Shdr shdr;
1210         GElf_Dyn dyn;
1211         Elf_Data *data;
1212         uint_t i, ndyn;
1213         GElf_Xword cksum;
1214         uintptr_t addr;
1215 
1216         if (fptr->file_map == NULL)
1217                 return (0);
1218 
1219         if ((Pcontent(P) & (CC_CONTENT_TEXT | CC_CONTENT_DATA)) !=
1220             (CC_CONTENT_TEXT | CC_CONTENT_DATA))
1221                 return (0);
1222 
1223         /*
1224          * First, we find the checksum value in the elf file.
1225          */
1226         scn = NULL;
1227         while ((scn = elf_nextscn(elf, scn)) != NULL) {
1228                 if (gelf_getshdr(scn, &shdr) != NULL &&
1229                     shdr.sh_type == SHT_DYNAMIC)
1230                         goto found_shdr;
1231         }
1232         return (0);
1233 
1234 found_shdr:
1235         if ((data = elf_getdata(scn, NULL)) == NULL)
1236                 return (0);
1237 
1238         if (P->status.pr_dmodel == PR_MODEL_ILP32)
1239                 ndyn = shdr.sh_size / sizeof (Elf32_Dyn);
1240 #ifdef _LP64
1241         else if (P->status.pr_dmodel == PR_MODEL_LP64)
1242                 ndyn = shdr.sh_size / sizeof (Elf64_Dyn);
1243 #endif
1244         else
1245                 return (0);
1246 
1247         for (i = 0; i < ndyn; i++) {
1248                 if (gelf_getdyn(data, i, &dyn) != NULL &&
1249                     dyn.d_tag == DT_CHECKSUM)
1250                         goto found_cksum;
1251         }
1252 
1253         /*
1254          * The in-memory ELF has no DT_CHECKSUM section, but we will report it
1255          * as matching the file anyhow.
1256          */
1257         return (0);
1258 
1259 found_cksum:
1260         cksum = dyn.d_un.d_val;
1261         dprintf("elf cksum value is %llx\n", (u_longlong_t)cksum);
1262 
1263         /*
1264          * Get the base of the text mapping that corresponds to this file.
1265          */
1266         addr = fptr->file_map->map_pmap.pr_vaddr;
1267 
1268         if (P->status.pr_dmodel == PR_MODEL_ILP32) {
1269                 Elf32_Ehdr ehdr;
1270                 Elf32_Phdr phdr;
1271                 Elf32_Dyn dync, *dynp;
1272                 uint_t phnum, i;
1273 
1274                 if (read_ehdr32(P, &ehdr, &phnum, addr) != 0 ||
1275                     read_dynamic_phdr32(P, &ehdr, phnum, &phdr, addr) != 0)
1276                         return (0);
1277 
1278                 if (ehdr.e_type == ET_DYN)
1279                         phdr.p_vaddr += addr;
1280                 if ((dynp = malloc(phdr.p_filesz)) == NULL)
1281                         return (0);
1282                 dync.d_tag = DT_NULL;
1283                 if (Pread(P, dynp, phdr.p_filesz, phdr.p_vaddr) !=
1284                     phdr.p_filesz) {
1285                         free(dynp);
1286                         return (0);
1287                 }
1288 
1289                 for (i = 0; i < phdr.p_filesz / sizeof (Elf32_Dyn); i++) {
1290                         if (dynp[i].d_tag == DT_CHECKSUM)
1291                                 dync = dynp[i];
1292                 }
1293 
1294                 free(dynp);
1295 
1296                 if (dync.d_tag != DT_CHECKSUM)
1297                         return (0);
1298 
1299                 dprintf("image cksum value is %llx\n",
1300                     (u_longlong_t)dync.d_un.d_val);
1301                 return (dync.d_un.d_val != cksum);
1302 #ifdef _LP64
1303         } else if (P->status.pr_dmodel == PR_MODEL_LP64) {
1304                 Elf64_Ehdr ehdr;
1305                 Elf64_Phdr phdr;
1306                 Elf64_Dyn dync, *dynp;
1307                 uint_t phnum, i;
1308 
1309                 if (read_ehdr64(P, &ehdr, &phnum, addr) != 0 ||
1310                     read_dynamic_phdr64(P, &ehdr, phnum, &phdr, addr) != 0)
1311                         return (0);
1312 
1313                 if (ehdr.e_type == ET_DYN)
1314                         phdr.p_vaddr += addr;
1315                 if ((dynp = malloc(phdr.p_filesz)) == NULL)
1316                         return (0);
1317                 dync.d_tag = DT_NULL;
1318                 if (Pread(P, dynp, phdr.p_filesz, phdr.p_vaddr) !=
1319                     phdr.p_filesz) {
1320                         free(dynp);
1321                         return (0);
1322                 }
1323 
1324                 for (i = 0; i < phdr.p_filesz / sizeof (Elf64_Dyn); i++) {
1325                         if (dynp[i].d_tag == DT_CHECKSUM)
1326                                 dync = dynp[i];
1327                 }
1328 
1329                 free(dynp);
1330 
1331                 if (dync.d_tag != DT_CHECKSUM)
1332                         return (0);
1333 
1334                 dprintf("image cksum value is %llx\n",
1335                     (u_longlong_t)dync.d_un.d_val);
1336                 return (dync.d_un.d_val != cksum);
1337 #endif  /* _LP64 */
1338         }
1339 
1340         return (0);
1341 }
1342 
1343 /*
1344  * Read data from the specified process and construct an in memory
1345  * image of an ELF file that represents it well enough to let
1346  * us probe it for information.
1347  */
1348 static Elf *
1349 fake_elf(struct ps_prochandle *P, file_info_t *fptr)
1350 {
1351         Elf *elf;
1352         uintptr_t addr;
1353         uint_t phnum;
1354 
1355         if (fptr->file_map == NULL)
1356                 return (NULL);
1357 
1358         if ((Pcontent(P) & (CC_CONTENT_TEXT | CC_CONTENT_DATA)) !=
1359             (CC_CONTENT_TEXT | CC_CONTENT_DATA))
1360                 return (NULL);
1361 
1362         addr = fptr->file_map->map_pmap.pr_vaddr;
1363 
1364         if (P->status.pr_dmodel == PR_MODEL_ILP32) {
1365                 Elf32_Ehdr ehdr;
1366                 Elf32_Phdr phdr;
1367 
1368                 if ((read_ehdr32(P, &ehdr, &phnum, addr) != 0) ||
1369                     read_dynamic_phdr32(P, &ehdr, phnum, &phdr, addr) != 0)
1370                         return (NULL);
1371 
1372                 elf = fake_elf32(P, fptr, addr, &ehdr, phnum, &phdr);
1373 #ifdef _LP64
1374         } else {
1375                 Elf64_Ehdr ehdr;
1376                 Elf64_Phdr phdr;
1377 
1378                 if (read_ehdr64(P, &ehdr, &phnum, addr) != 0 ||
1379                     read_dynamic_phdr64(P, &ehdr, phnum, &phdr, addr) != 0)
1380                         return (NULL);
1381 
1382                 elf = fake_elf64(P, fptr, addr, &ehdr, phnum, &phdr);
1383 #endif
1384         }
1385 
1386         return (elf);
1387 }
1388 
1389 /*
1390  * We wouldn't need these if qsort(3C) took an argument for the callback...
1391  */
1392 static mutex_t sort_mtx = DEFAULTMUTEX;
1393 static char *sort_strs;
1394 static GElf_Sym *sort_syms;
1395 
1396 int
1397 byaddr_cmp_common(GElf_Sym *a, char *aname, GElf_Sym *b, char *bname)
1398 {
1399         if (a->st_value < b->st_value)
1400                 return (-1);
1401         if (a->st_value > b->st_value)
1402                 return (1);
1403 
1404         /*
1405          * Prefer the function to the non-function.
1406          */
1407         if (GELF_ST_TYPE(a->st_info) != GELF_ST_TYPE(b->st_info)) {
1408                 if (GELF_ST_TYPE(a->st_info) == STT_FUNC)
1409                         return (-1);
1410                 if (GELF_ST_TYPE(b->st_info) == STT_FUNC)
1411                         return (1);
1412         }
1413 
1414         /*
1415          * Prefer the weak or strong global symbol to the local symbol.
1416          */
1417         if (GELF_ST_BIND(a->st_info) != GELF_ST_BIND(b->st_info)) {
1418                 if (GELF_ST_BIND(b->st_info) == STB_LOCAL)
1419                         return (-1);
1420                 if (GELF_ST_BIND(a->st_info) == STB_LOCAL)
1421                         return (1);
1422         }
1423 
1424         /*
1425          * Prefer the symbol that doesn't begin with a '$' since compilers and
1426          * other symbol generators often use it as a prefix.
1427          */
1428         if (*bname == '$')
1429                 return (-1);
1430         if (*aname == '$')
1431                 return (1);
1432 
1433         /*
1434          * Prefer the name with fewer leading underscores in the name.
1435          */
1436         while (*aname == '_' && *bname == '_') {
1437                 aname++;
1438                 bname++;
1439         }
1440 
1441         if (*bname == '_')
1442                 return (-1);
1443         if (*aname == '_')
1444                 return (1);
1445 
1446         /*
1447          * Prefer the symbol with the smaller size.
1448          */
1449         if (a->st_size < b->st_size)
1450                 return (-1);
1451         if (a->st_size > b->st_size)
1452                 return (1);
1453 
1454         /*
1455          * All other factors being equal, fall back to lexicographic order.
1456          */
1457         return (strcmp(aname, bname));
1458 }
1459 
1460 static int
1461 byaddr_cmp(const void *aa, const void *bb)
1462 {
1463         GElf_Sym *a = &sort_syms[*(uint_t *)aa];
1464         GElf_Sym *b = &sort_syms[*(uint_t *)bb];
1465         char *aname = sort_strs + a->st_name;
1466         char *bname = sort_strs + b->st_name;
1467 
1468         return (byaddr_cmp_common(a, aname, b, bname));
1469 }
1470 
1471 static int
1472 byname_cmp(const void *aa, const void *bb)
1473 {
1474         GElf_Sym *a = &sort_syms[*(uint_t *)aa];
1475         GElf_Sym *b = &sort_syms[*(uint_t *)bb];
1476         char *aname = sort_strs + a->st_name;
1477         char *bname = sort_strs + b->st_name;
1478 
1479         return (strcmp(aname, bname));
1480 }
1481 
1482 /*
1483  * Given a symbol index, look up the corresponding symbol from the
1484  * given symbol table.
1485  *
1486  * This function allows the caller to treat the symbol table as a single
1487  * logical entity even though there may be 2 actual ELF symbol tables
1488  * involved. See the comments in Pcontrol.h for details.
1489  */
1490 static GElf_Sym *
1491 symtab_getsym(sym_tbl_t *symtab, int ndx, GElf_Sym *dst)
1492 {
1493         /* If index is in range of primary symtab, look it up there */
1494         if (ndx >= symtab->sym_symn_aux) {
1495                 return (gelf_getsym(symtab->sym_data_pri,
1496                     ndx - symtab->sym_symn_aux, dst));
1497         }
1498 
1499         /* Not in primary: Look it up in the auxiliary symtab */
1500         return (gelf_getsym(symtab->sym_data_aux, ndx, dst));
1501 }
1502 
1503 void
1504 optimize_symtab(sym_tbl_t *symtab)
1505 {
1506         GElf_Sym *symp, *syms;
1507         uint_t i, *indexa, *indexb;
1508         size_t symn, strsz, count;
1509 
1510         if (symtab == NULL || symtab->sym_data_pri == NULL ||
1511             symtab->sym_byaddr != NULL)
1512                 return;
1513 
1514         symn = symtab->sym_symn;
1515         strsz = symtab->sym_strsz;
1516 
1517         symp = syms = malloc(sizeof (GElf_Sym) * symn);
1518         if (symp == NULL) {
1519                 dprintf("optimize_symtab: failed to malloc symbol array");
1520                 return;
1521         }
1522 
1523         /*
1524          * First record all the symbols into a table and count up the ones
1525          * that we're interested in. We mark symbols as invalid by setting
1526          * the st_name to an illegal value.
1527          */
1528         for (i = 0, count = 0; i < symn; i++, symp++) {
1529                 if (symtab_getsym(symtab, i, symp) != NULL &&
1530                     symp->st_name < strsz &&
1531                     IS_DATA_TYPE(GELF_ST_TYPE(symp->st_info)))
1532                         count++;
1533                 else
1534                         symp->st_name = strsz;
1535         }
1536 
1537         /*
1538          * Allocate sufficient space for both tables and populate them
1539          * with the same symbols we just counted.
1540          */
1541         symtab->sym_count = count;
1542         indexa = symtab->sym_byaddr = calloc(sizeof (uint_t), count);
1543         indexb = symtab->sym_byname = calloc(sizeof (uint_t), count);
1544         if (indexa == NULL || indexb == NULL) {
1545                 dprintf(
1546                     "optimize_symtab: failed to malloc symbol index arrays");
1547                 symtab->sym_count = 0;
1548                 if (indexa != NULL) {   /* First alloc succeeded. Free it */
1549                         free(indexa);
1550                         symtab->sym_byaddr = NULL;
1551                 }
1552                 free(syms);
1553                 return;
1554         }
1555         for (i = 0, symp = syms; i < symn; i++, symp++) {
1556                 if (symp->st_name < strsz)
1557                         *indexa++ = *indexb++ = i;
1558         }
1559 
1560         /*
1561          * Sort the two tables according to the appropriate criteria,
1562          * unless the user has overridden this behaviour.
1563          *
1564          * An example where we might not sort the tables is the relatively
1565          * unusual case of a process with very large symbol tables in which
1566          * we perform few lookups. In such a case the total time would be
1567          * dominated by the sort. It is difficult to determine a priori
1568          * how many lookups an arbitrary client will perform, and
1569          * hence whether the symbol tables should be sorted. We therefore
1570          * sort the tables by default, but provide the user with a
1571          * "chicken switch" in the form of the LIBPROC_NO_QSORT
1572          * environment variable.
1573          */
1574         if (!_libproc_no_qsort) {
1575                 (void) mutex_lock(&sort_mtx);
1576                 sort_strs = symtab->sym_strs;
1577                 sort_syms = syms;
1578 
1579                 qsort(symtab->sym_byaddr, count, sizeof (uint_t), byaddr_cmp);
1580                 qsort(symtab->sym_byname, count, sizeof (uint_t), byname_cmp);
1581 
1582                 sort_strs = NULL;
1583                 sort_syms = NULL;
1584                 (void) mutex_unlock(&sort_mtx);
1585         }
1586 
1587         free(syms);
1588 }
1589 
1590 
1591 static Elf *
1592 build_fake_elf(struct ps_prochandle *P, file_info_t *fptr, GElf_Ehdr *ehdr,
1593         size_t *nshdrs, Elf_Data **shdata)
1594 {
1595         size_t shstrndx;
1596         Elf_Scn *scn;
1597         Elf *elf;
1598 
1599         if ((elf = fake_elf(P, fptr)) == NULL ||
1600             elf_kind(elf) != ELF_K_ELF ||
1601             gelf_getehdr(elf, ehdr) == NULL ||
1602             elf_getshdrnum(elf, nshdrs) == -1 ||
1603             elf_getshdrstrndx(elf, &shstrndx) == -1 ||
1604             (scn = elf_getscn(elf, shstrndx)) == NULL ||
1605             (*shdata = elf_getdata(scn, NULL)) == NULL) {
1606                 if (elf != NULL)
1607                         (void) elf_end(elf);
1608                 dprintf("failed to fake up ELF file\n");
1609                 return (NULL);
1610         }
1611 
1612         return (elf);
1613 }
1614 
1615 /*
1616  * Build the symbol table for the given mapped file.
1617  */
1618 void
1619 Pbuild_file_symtab(struct ps_prochandle *P, file_info_t *fptr)
1620 {
1621         char objectfile[PATH_MAX];
1622         uint_t i;
1623 
1624         GElf_Ehdr ehdr;
1625         GElf_Sym s;
1626 
1627         Elf_Data *shdata;
1628         Elf_Scn *scn;
1629         Elf *elf;
1630         size_t nshdrs, shstrndx;
1631 
1632         struct {
1633                 GElf_Shdr c_shdr;
1634                 Elf_Data *c_data;
1635                 const char *c_name;
1636         } *cp, *cache = NULL, *dyn = NULL, *plt = NULL, *ctf = NULL;
1637 
1638         if (fptr->file_init)
1639                 return; /* We've already processed this file */
1640 
1641         /*
1642          * Mark the file_info struct as having the symbol table initialized
1643          * even if we fail below.  We tried once; we don't try again.
1644          */
1645         fptr->file_init = 1;
1646 
1647         if (elf_version(EV_CURRENT) == EV_NONE) {
1648                 dprintf("libproc ELF version is more recent than libelf\n");
1649                 return;
1650         }
1651 
1652         if (P->state == PS_DEAD || P->state == PS_IDLE) {
1653                 char *name;
1654                 /*
1655                  * If we're a not live, we can't open files from the /proc
1656                  * object directory; we have only the mapping and file names
1657                  * to guide us.  We prefer the file_lname, but need to handle
1658                  * the case of it being NULL in order to bootstrap: we first
1659                  * come here during rd_new() when the only information we have
1660                  * is interpreter name associated with the AT_BASE mapping.
1661                  *
1662                  * Also, if the zone associated with the core file seems
1663                  * to exists on this machine we'll try to open the object
1664                  * file within the zone.
1665                  */
1666                 if (fptr->file_rname != NULL)
1667                         name = fptr->file_rname;
1668                 else if (fptr->file_lname != NULL)
1669                         name = fptr->file_lname;
1670                 else
1671                         name = fptr->file_pname;
1672                 (void) strlcpy(objectfile, name, sizeof (objectfile));
1673         } else {
1674                 (void) snprintf(objectfile, sizeof (objectfile),
1675                     "%s/%d/object/%s",
1676                     procfs_path, (int)P->pid, fptr->file_pname);
1677         }
1678 
1679         /*
1680          * Open the object file, create the elf file, and then get the elf
1681          * header and .shstrtab data buffer so we can process sections by
1682          * name. If anything goes wrong try to fake up an elf file from
1683          * the in-core elf image.
1684          */
1685 
1686         if (_libproc_incore_elf) {
1687                 dprintf("Pbuild_file_symtab: using in-core data for: %s\n",
1688                     fptr->file_pname);
1689 
1690                 if ((elf = build_fake_elf(P, fptr, &ehdr, &nshdrs, &shdata)) ==
1691                     NULL)
1692                         return;
1693 
1694         } else if ((fptr->file_fd = open(objectfile, O_RDONLY)) < 0) {
1695                 dprintf("Pbuild_file_symtab: failed to open %s: %s\n",
1696                     objectfile, strerror(errno));
1697 
1698                 if ((elf = build_fake_elf(P, fptr, &ehdr, &nshdrs, &shdata)) ==
1699                     NULL)
1700                         return;
1701 
1702         } else if ((elf = elf_begin(fptr->file_fd, ELF_C_READ, NULL)) == NULL ||
1703             elf_kind(elf) != ELF_K_ELF ||
1704             gelf_getehdr(elf, &ehdr) == NULL ||
1705             elf_getshdrnum(elf, &nshdrs) == -1 ||
1706             elf_getshdrstrndx(elf, &shstrndx) == -1 ||
1707             (scn = elf_getscn(elf, shstrndx)) == NULL ||
1708             (shdata = elf_getdata(scn, NULL)) == NULL) {
1709                 int err = elf_errno();
1710 
1711                 dprintf("failed to process ELF file %s: %s\n",
1712                     objectfile, (err == 0) ? "<null>" : elf_errmsg(err));
1713                 (void) elf_end(elf);
1714 
1715                 if ((elf = build_fake_elf(P, fptr, &ehdr, &nshdrs, &shdata)) ==
1716                     NULL)
1717                         return;
1718 
1719         } else if (file_differs(P, elf, fptr)) {
1720                 Elf *newelf;
1721 
1722                 /*
1723                  * Before we get too excited about this elf file, we'll check
1724                  * its checksum value against the value we have in memory. If
1725                  * they don't agree, we try to fake up a new elf file and
1726                  * proceed with that instead.
1727                  */
1728                 dprintf("ELF file %s (%lx) doesn't match in-core image\n",
1729                     fptr->file_pname,
1730                     (ulong_t)fptr->file_map->map_pmap.pr_vaddr);
1731 
1732                 if ((newelf = build_fake_elf(P, fptr, &ehdr, &nshdrs, &shdata))
1733                     != NULL) {
1734                         (void) elf_end(elf);
1735                         elf = newelf;
1736                         dprintf("switched to faked up ELF file\n");
1737 
1738                         /*
1739                          * Check to see if the file that we just discovered
1740                          * to be an imposter matches the execname that was
1741                          * determined by Pfindexec().  If it does, we (clearly)
1742                          * don't have the right binary, and we zero out
1743                          * execname before anyone gets hurt.
1744                          */
1745                         if (fptr->file_rname != NULL && P->execname != NULL &&
1746                             strcmp(fptr->file_rname, P->execname) == 0) {
1747                                 dprintf("file/in-core image mismatch was "
1748                                     "on P->execname; discarding\n");
1749                                 free(P->execname);
1750                                 P->execname = NULL;
1751                         }
1752                 }
1753         }
1754 
1755         if ((cache = malloc(nshdrs * sizeof (*cache))) == NULL) {
1756                 dprintf("failed to malloc section cache for %s\n", objectfile);
1757                 goto bad;
1758         }
1759 
1760         dprintf("processing ELF file %s\n", objectfile);
1761         fptr->file_class = ehdr.e_ident[EI_CLASS];
1762         fptr->file_etype = ehdr.e_type;
1763         fptr->file_elf = elf;
1764         fptr->file_shstrs = shdata->d_buf;
1765         fptr->file_shstrsz = shdata->d_size;
1766 
1767         /*
1768          * Iterate through each section, caching its section header, data
1769          * pointer, and name.  We use this for handling sh_link values below.
1770          */
1771         for (cp = cache + 1, scn = NULL; scn = elf_nextscn(elf, scn); cp++) {
1772                 if (gelf_getshdr(scn, &cp->c_shdr) == NULL) {
1773                         dprintf("Pbuild_file_symtab: Failed to get section "
1774                             "header\n");
1775                         goto bad; /* Failed to get section header */
1776                 }
1777 
1778                 if ((cp->c_data = elf_getdata(scn, NULL)) == NULL) {
1779                         dprintf("Pbuild_file_symtab: Failed to get section "
1780                             "data\n");
1781                         goto bad; /* Failed to get section data */
1782                 }
1783 
1784                 if (cp->c_shdr.sh_name >= shdata->d_size) {
1785                         dprintf("Pbuild_file_symtab: corrupt section name");
1786                         goto bad; /* Corrupt section name */
1787                 }
1788 
1789                 cp->c_name = (const char *)shdata->d_buf + cp->c_shdr.sh_name;
1790         }
1791 
1792         /*
1793          * Now iterate through the section cache in order to locate info
1794          * for the .symtab, .dynsym, .SUNW_ldynsym, .dynamic, .plt,
1795          * and .SUNW_ctf sections:
1796          */
1797         for (i = 1, cp = cache + 1; i < nshdrs; i++, cp++) {
1798                 GElf_Shdr *shp = &cp->c_shdr;
1799 
1800                 if (shp->sh_type == SHT_SYMTAB || shp->sh_type == SHT_DYNSYM) {
1801                         sym_tbl_t *symp = shp->sh_type == SHT_SYMTAB ?
1802                             &fptr->file_symtab : &fptr->file_dynsym;
1803                         /*
1804                          * It's possible that the we already got the symbol
1805                          * table from the core file itself. Either the file
1806                          * differs in which case our faked up elf file will
1807                          * only contain the dynsym (not the symtab) or the
1808                          * file matches in which case we'll just be replacing
1809                          * the symbol table we pulled out of the core file
1810                          * with an equivalent one. In either case, this
1811                          * check isn't essential, but it's a good idea.
1812                          */
1813                         if (symp->sym_data_pri == NULL) {
1814                                 dprintf("Symbol table found for %s\n",
1815                                     objectfile);
1816                                 symp->sym_data_pri = cp->c_data;
1817                                 symp->sym_symn +=
1818                                     shp->sh_size / shp->sh_entsize;
1819                                 symp->sym_strs =
1820                                     cache[shp->sh_link].c_data->d_buf;
1821                                 symp->sym_strsz =
1822                                     cache[shp->sh_link].c_data->d_size;
1823                                 symp->sym_hdr_pri = cp->c_shdr;
1824                                 symp->sym_strhdr = cache[shp->sh_link].c_shdr;
1825                         } else {
1826                                 dprintf("Symbol table already there for %s\n",
1827                                     objectfile);
1828                         }
1829                 } else if (shp->sh_type == SHT_SUNW_LDYNSYM) {
1830                         /* .SUNW_ldynsym section is auxiliary to .dynsym */
1831                         if (fptr->file_dynsym.sym_data_aux == NULL) {
1832                                 dprintf(".SUNW_ldynsym symbol table"
1833                                     " found for %s\n", objectfile);
1834                                 fptr->file_dynsym.sym_data_aux = cp->c_data;
1835                                 fptr->file_dynsym.sym_symn_aux =
1836                                     shp->sh_size / shp->sh_entsize;
1837                                 fptr->file_dynsym.sym_symn +=
1838                                     fptr->file_dynsym.sym_symn_aux;
1839                                 fptr->file_dynsym.sym_hdr_aux = cp->c_shdr;
1840                         } else {
1841                                 dprintf(".SUNW_ldynsym symbol table already"
1842                                     " there for %s\n", objectfile);
1843                         }
1844                 } else if (shp->sh_type == SHT_DYNAMIC) {
1845                         dyn = cp;
1846                 } else if (strcmp(cp->c_name, ".plt") == 0) {
1847                         plt = cp;
1848                 } else if (strcmp(cp->c_name, ".SUNW_ctf") == 0) {
1849                         /*
1850                          * Skip over bogus CTF sections so they don't come back
1851                          * to haunt us later.
1852                          */
1853                         if (shp->sh_link == 0 ||
1854                             shp->sh_link >= nshdrs ||
1855                             (cache[shp->sh_link].c_shdr.sh_type != SHT_DYNSYM &&
1856                             cache[shp->sh_link].c_shdr.sh_type != SHT_SYMTAB)) {
1857                                 dprintf("Bad sh_link %d for "
1858                                     "CTF\n", shp->sh_link);
1859                                 continue;
1860                         }
1861                         ctf = cp;
1862                 }
1863         }
1864 
1865         /*
1866          * At this point, we've found all the symbol tables we're ever going
1867          * to find: the ones in the loop above and possibly the symtab that
1868          * was included in the core file. Before we perform any lookups, we
1869          * create sorted versions to optimize for lookups.
1870          */
1871         optimize_symtab(&fptr->file_symtab);
1872         optimize_symtab(&fptr->file_dynsym);
1873 
1874         /*
1875          * Fill in the base address of the text mapping for shared libraries.
1876          * This allows us to translate symbols before librtld_db is ready.
1877          */
1878         if (fptr->file_etype == ET_DYN) {
1879                 fptr->file_dyn_base = fptr->file_map->map_pmap.pr_vaddr -
1880                     fptr->file_map->map_pmap.pr_offset;
1881                 dprintf("setting file_dyn_base for %s to %lx\n",
1882                     objectfile, (long)fptr->file_dyn_base);
1883         }
1884 
1885         /*
1886          * Record the CTF section information in the file info structure.
1887          */
1888         if (ctf != NULL) {
1889                 fptr->file_ctf_off = ctf->c_shdr.sh_offset;
1890                 fptr->file_ctf_size = ctf->c_shdr.sh_size;
1891                 if (ctf->c_shdr.sh_link != 0 &&
1892                     cache[ctf->c_shdr.sh_link].c_shdr.sh_type == SHT_DYNSYM)
1893                         fptr->file_ctf_dyn = 1;
1894         }
1895 
1896         if (fptr->file_lo == NULL)
1897                 goto done; /* Nothing else to do if no load object info */
1898 
1899         /*
1900          * If the object is a shared library and we have a different rl_base
1901          * value, reset file_dyn_base according to librtld_db's information.
1902          */
1903         if (fptr->file_etype == ET_DYN &&
1904             fptr->file_lo->rl_base != fptr->file_dyn_base) {
1905                 dprintf("resetting file_dyn_base for %s to %lx\n",
1906                     objectfile, (long)fptr->file_lo->rl_base);
1907                 fptr->file_dyn_base = fptr->file_lo->rl_base;
1908         }
1909 
1910         /*
1911          * Fill in the PLT information for this file if a PLT symbol is found.
1912          */
1913         if (sym_by_name(&fptr->file_dynsym, "_PROCEDURE_LINKAGE_TABLE_", &s,
1914             NULL) != NULL) {
1915                 fptr->file_plt_base = s.st_value + fptr->file_dyn_base;
1916                 fptr->file_plt_size = (plt != NULL) ? plt->c_shdr.sh_size : 0;
1917 
1918                 /*
1919                  * Bring the load object up to date; it is the only way the
1920                  * user has to access the PLT data. The PLT information in the
1921                  * rd_loadobj_t is not set in the call to map_iter() (the
1922                  * callback for rd_loadobj_iter) where we set file_lo.
1923                  */
1924                 fptr->file_lo->rl_plt_base = fptr->file_plt_base;
1925                 fptr->file_lo->rl_plt_size = fptr->file_plt_size;
1926 
1927                 dprintf("PLT found at %p, size = %lu\n",
1928                     (void *)fptr->file_plt_base, (ulong_t)fptr->file_plt_size);
1929         }
1930 
1931         /*
1932          * Fill in the PLT information.
1933          */
1934         if (dyn != NULL) {
1935                 uintptr_t dynaddr = dyn->c_shdr.sh_addr + fptr->file_dyn_base;
1936                 size_t ndyn = dyn->c_shdr.sh_size / dyn->c_shdr.sh_entsize;
1937                 GElf_Dyn d;
1938 
1939                 for (i = 0; i < ndyn; i++) {
1940                         if (gelf_getdyn(dyn->c_data, i, &d) == NULL)
1941                                 continue;
1942 
1943                         switch (d.d_tag) {
1944                         case DT_JMPREL:
1945                                 dprintf("DT_JMPREL is %p\n",
1946                                     (void *)(uintptr_t)d.d_un.d_ptr);
1947                                 fptr->file_jmp_rel =
1948                                     d.d_un.d_ptr + fptr->file_dyn_base;
1949                                 break;
1950                         case DT_STRTAB:
1951                                 dprintf("DT_STRTAB is %p\n",
1952                                     (void *)(uintptr_t)d.d_un.d_ptr);
1953                                 break;
1954                         case DT_PLTGOT:
1955                                 dprintf("DT_PLTGOT is %p\n",
1956                                     (void *)(uintptr_t)d.d_un.d_ptr);
1957                                 break;
1958                         case DT_SUNW_SYMTAB:
1959                                 dprintf("DT_SUNW_SYMTAB is %p\n",
1960                                     (void *)(uintptr_t)d.d_un.d_ptr);
1961                                 break;
1962                         case DT_SYMTAB:
1963                                 dprintf("DT_SYMTAB is %p\n",
1964                                     (void *)(uintptr_t)d.d_un.d_ptr);
1965                                 break;
1966                         case DT_HASH:
1967                                 dprintf("DT_HASH is %p\n",
1968                                     (void *)(uintptr_t)d.d_un.d_ptr);
1969                                 break;
1970                         }
1971                 }
1972 
1973                 dprintf("_DYNAMIC found at %p, %lu entries, DT_JMPREL = %p\n",
1974                     (void *)dynaddr, (ulong_t)ndyn, (void *)fptr->file_jmp_rel);
1975         }
1976 
1977 done:
1978         free(cache);
1979         return;
1980 
1981 bad:
1982         if (cache != NULL)
1983                 free(cache);
1984 
1985         (void) elf_end(elf);
1986         fptr->file_elf = NULL;
1987         if (fptr->file_elfmem != NULL) {
1988                 free(fptr->file_elfmem);
1989                 fptr->file_elfmem = NULL;
1990         }
1991         (void) close(fptr->file_fd);
1992         fptr->file_fd = -1;
1993 }
1994 
1995 /*
1996  * Given a process virtual address, return the map_info_t containing it.
1997  * If none found, return NULL.
1998  */
1999 map_info_t *
2000 Paddr2mptr(struct ps_prochandle *P, uintptr_t addr)
2001 {
2002         int lo = 0;
2003         int hi = P->map_count - 1;
2004         int mid;
2005         map_info_t *mp;
2006 
2007         while (lo <= hi) {
2008 
2009                 mid = (lo + hi) / 2;
2010                 mp = &P->mappings[mid];
2011 
2012                 /* check that addr is in [vaddr, vaddr + size) */
2013                 if ((addr - mp->map_pmap.pr_vaddr) < mp->map_pmap.pr_size)
2014                         return (mp);
2015 
2016                 if (addr < mp->map_pmap.pr_vaddr)
2017                         hi = mid - 1;
2018                 else
2019                         lo = mid + 1;
2020         }
2021 
2022         return (NULL);
2023 }
2024 
2025 /*
2026  * Return the map_info_t for the executable file.
2027  * If not found, return NULL.
2028  */
2029 static map_info_t *
2030 exec_map(struct ps_prochandle *P)
2031 {
2032         uint_t i;
2033         map_info_t *mptr;
2034         map_info_t *mold = NULL;
2035         file_info_t *fptr;
2036         uintptr_t base;
2037 
2038         for (i = 0, mptr = P->mappings; i < P->map_count; i++, mptr++) {
2039                 if (mptr->map_pmap.pr_mapname[0] == '\0')
2040                         continue;
2041                 if (strcmp(mptr->map_pmap.pr_mapname, "a.out") == 0) {
2042                         if ((fptr = mptr->map_file) != NULL &&
2043                             fptr->file_lo != NULL) {
2044                                 base = fptr->file_lo->rl_base;
2045                                 if (base >= mptr->map_pmap.pr_vaddr &&
2046                                     base < mptr->map_pmap.pr_vaddr +
2047                                     mptr->map_pmap.pr_size)  /* text space */
2048                                         return (mptr);
2049                                 mold = mptr;    /* must be the data */
2050                                 continue;
2051                         }
2052                         /* This is a poor way to test for text space */
2053                         if (!(mptr->map_pmap.pr_mflags & MA_EXEC) ||
2054                             (mptr->map_pmap.pr_mflags & MA_WRITE)) {
2055                                 mold = mptr;
2056                                 continue;
2057                         }
2058                         return (mptr);
2059                 }
2060         }
2061 
2062         return (mold);
2063 }
2064 
2065 /*
2066  * Given a shared object name, return the map_info_t for it.  If no matching
2067  * object is found, return NULL.  Normally, the link maps contain the full
2068  * object pathname, e.g. /usr/lib/libc.so.1.  We allow the object name to
2069  * take one of the following forms:
2070  *
2071  * 1. An exact match (i.e. a full pathname): "/usr/lib/libc.so.1"
2072  * 2. An exact basename match: "libc.so.1"
2073  * 3. An initial basename match up to a '.' suffix: "libc.so" or "libc"
2074  * 4. The literal string "a.out" is an alias for the executable mapping
2075  *
2076  * The third case is a convenience for callers and may not be necessary.
2077  *
2078  * As the exact same object name may be loaded on different link maps (see
2079  * dlmopen(3DL)), we also allow the caller to resolve the object name by
2080  * specifying a particular link map id.  If lmid is PR_LMID_EVERY, the
2081  * first matching name will be returned, regardless of the link map id.
2082  */
2083 static map_info_t *
2084 object_to_map(struct ps_prochandle *P, Lmid_t lmid, const char *objname)
2085 {
2086         map_info_t *mp;
2087         file_info_t *fp;
2088         size_t objlen;
2089         uint_t i;
2090 
2091         /*
2092          * If we have no rtld_db, then always treat a request as one for all
2093          * link maps.
2094          */
2095         if (P->rap == NULL)
2096                 lmid = PR_LMID_EVERY;
2097 
2098         /*
2099          * First pass: look for exact matches of the entire pathname or
2100          * basename (cases 1 and 2 above):
2101          */
2102         for (i = 0, mp = P->mappings; i < P->map_count; i++, mp++) {
2103 
2104                 if (mp->map_pmap.pr_mapname[0] == '\0' ||
2105                     (fp = mp->map_file) == NULL ||
2106                     ((fp->file_lname == NULL) && (fp->file_rname == NULL)))
2107                         continue;
2108 
2109                 if (lmid != PR_LMID_EVERY &&
2110                     (fp->file_lo == NULL || lmid != fp->file_lo->rl_lmident))
2111                         continue;
2112 
2113                 /*
2114                  * If we match, return the primary text mapping; otherwise
2115                  * just return the mapping we matched.
2116                  */
2117                 if ((fp->file_lbase && strcmp(fp->file_lbase, objname) == 0) ||
2118                     (fp->file_rbase && strcmp(fp->file_rbase, objname) == 0) ||
2119                     (fp->file_lname && strcmp(fp->file_lname, objname) == 0) ||
2120                     (fp->file_rname && strcmp(fp->file_rname, objname) == 0))
2121                         return (fp->file_map ? fp->file_map : mp);
2122         }
2123 
2124         objlen = strlen(objname);
2125 
2126         /*
2127          * Second pass: look for partial matches (case 3 above):
2128          */
2129         for (i = 0, mp = P->mappings; i < P->map_count; i++, mp++) {
2130 
2131                 if (mp->map_pmap.pr_mapname[0] == '\0' ||
2132                     (fp = mp->map_file) == NULL ||
2133                     ((fp->file_lname == NULL) && (fp->file_rname == NULL)))
2134                         continue;
2135 
2136                 if (lmid != PR_LMID_EVERY &&
2137                     (fp->file_lo == NULL || lmid != fp->file_lo->rl_lmident))
2138                         continue;
2139 
2140                 /*
2141                  * If we match, return the primary text mapping; otherwise
2142                  * just return the mapping we matched.
2143                  */
2144                 if ((fp->file_lbase != NULL) &&
2145                     (strncmp(fp->file_lbase, objname, objlen) == 0) &&
2146                     (fp->file_lbase[objlen] == '.'))
2147                         return (fp->file_map ? fp->file_map : mp);
2148                 if ((fp->file_rbase != NULL) &&
2149                     (strncmp(fp->file_rbase, objname, objlen) == 0) &&
2150                     (fp->file_rbase[objlen] == '.'))
2151                         return (fp->file_map ? fp->file_map : mp);
2152         }
2153 
2154         /*
2155          * One last check: we allow "a.out" to always alias the executable,
2156          * assuming this name was not in use for something else.
2157          */
2158         if ((lmid == PR_LMID_EVERY || lmid == LM_ID_BASE) &&
2159             (strcmp(objname, "a.out") == 0))
2160                 return (P->map_exec);
2161 
2162         return (NULL);
2163 }
2164 
2165 static map_info_t *
2166 object_name_to_map(struct ps_prochandle *P, Lmid_t lmid, const char *name)
2167 {
2168         map_info_t *mptr;
2169 
2170         if (!P->info_valid)
2171                 Pupdate_maps(P);
2172 
2173         if (P->map_exec == NULL && ((mptr = Paddr2mptr(P,
2174             Pgetauxval(P, AT_ENTRY))) != NULL || (mptr = exec_map(P)) != NULL))
2175                 P->map_exec = mptr;
2176 
2177         if (P->map_ldso == NULL && (mptr = Paddr2mptr(P,
2178             Pgetauxval(P, AT_BASE))) != NULL)
2179                 P->map_ldso = mptr;
2180 
2181         if (name == PR_OBJ_EXEC)
2182                 mptr = P->map_exec;
2183         else if (name == PR_OBJ_LDSO)
2184                 mptr = P->map_ldso;
2185         else if (Prd_agent(P) != NULL || P->state == PS_IDLE)
2186                 mptr = object_to_map(P, lmid, name);
2187         else
2188                 mptr = NULL;
2189 
2190         return (mptr);
2191 }
2192 
2193 /*
2194  * When two symbols are found by address, decide which one is to be preferred.
2195  */
2196 static GElf_Sym *
2197 sym_prefer(GElf_Sym *sym1, char *name1, GElf_Sym *sym2, char *name2)
2198 {
2199         /*
2200          * Prefer the non-NULL symbol.
2201          */
2202         if (sym1 == NULL)
2203                 return (sym2);
2204         if (sym2 == NULL)
2205                 return (sym1);
2206 
2207         /*
2208          * Defer to the sort ordering...
2209          */
2210         return (byaddr_cmp_common(sym1, name1, sym2, name2) <= 0 ? sym1 : sym2);
2211 }
2212 
2213 /*
2214  * Use a binary search to do the work of sym_by_addr().
2215  */
2216 static GElf_Sym *
2217 sym_by_addr_binary(sym_tbl_t *symtab, GElf_Addr addr, GElf_Sym *symp,
2218     uint_t *idp)
2219 {
2220         GElf_Sym sym, osym;
2221         uint_t i, oid, *byaddr = symtab->sym_byaddr;
2222         int min, max, mid, omid, found = 0;
2223 
2224         if (symtab->sym_data_pri == NULL || symtab->sym_count == 0)
2225                 return (NULL);
2226 
2227         min = 0;
2228         max = symtab->sym_count - 1;
2229         osym.st_value = 0;
2230 
2231         /*
2232          * We can't return when we've found a match, we have to continue
2233          * searching for the closest matching symbol.
2234          */
2235         while (min <= max) {
2236                 mid = (max + min) / 2;
2237 
2238                 i = byaddr[mid];
2239                 (void) symtab_getsym(symtab, i, &sym);
2240 
2241                 if (addr >= sym.st_value &&
2242                     addr < sym.st_value + sym.st_size &&
2243                     (!found || sym.st_value > osym.st_value)) {
2244                         osym = sym;
2245                         omid = mid;
2246                         oid = i;
2247                         found = 1;
2248                 }
2249 
2250                 if (addr < sym.st_value)
2251                         max = mid - 1;
2252                 else
2253                         min = mid + 1;
2254         }
2255 
2256         if (!found)
2257                 return (NULL);
2258 
2259         /*
2260          * There may be many symbols with identical values so we walk
2261          * backward in the byaddr table to find the best match.
2262          */
2263         do {
2264                 sym = osym;
2265                 i = oid;
2266 
2267                 if (omid == 0)
2268                         break;
2269 
2270                 oid = byaddr[--omid];
2271                 (void) symtab_getsym(symtab, oid, &osym);
2272         } while (addr >= osym.st_value &&
2273             addr < sym.st_value + osym.st_size &&
2274             osym.st_value == sym.st_value);
2275 
2276         *symp = sym;
2277         if (idp != NULL)
2278                 *idp = i;
2279         return (symp);
2280 }
2281 
2282 /*
2283  * Use a linear search to do the work of sym_by_addr().
2284  */
2285 static GElf_Sym *
2286 sym_by_addr_linear(sym_tbl_t *symtab, GElf_Addr addr, GElf_Sym *symbolp,
2287     uint_t *idp)
2288 {
2289         size_t symn = symtab->sym_symn;
2290         char *strs = symtab->sym_strs;
2291         GElf_Sym sym, *symp = NULL;
2292         GElf_Sym osym, *osymp = NULL;
2293         int i, id;
2294 
2295         if (symtab->sym_data_pri == NULL || symn == 0 || strs == NULL)
2296                 return (NULL);
2297 
2298         for (i = 0; i < symn; i++) {
2299                 if ((symp = symtab_getsym(symtab, i, &sym)) != NULL) {
2300                         if (addr >= sym.st_value &&
2301                             addr < sym.st_value + sym.st_size) {
2302                                 if (osymp)
2303                                         symp = sym_prefer(
2304                                             symp, strs + symp->st_name,
2305                                             osymp, strs + osymp->st_name);
2306                                 if (symp != osymp) {
2307                                         osym = sym;
2308                                         osymp = &osym;
2309                                         id = i;
2310                                 }
2311                         }
2312                 }
2313         }
2314         if (osymp) {
2315                 *symbolp = osym;
2316                 if (idp)
2317                         *idp = id;
2318                 return (symbolp);
2319         }
2320         return (NULL);
2321 }
2322 
2323 /*
2324  * Look up a symbol by address in the specified symbol table.
2325  * Adjustment to 'addr' must already have been made for the
2326  * offset of the symbol if this is a dynamic library symbol table.
2327  *
2328  * Use a linear or a binary search depending on whether or not we
2329  * chose to sort the table in optimize_symtab().
2330  */
2331 static GElf_Sym *
2332 sym_by_addr(sym_tbl_t *symtab, GElf_Addr addr, GElf_Sym *symp, uint_t *idp)
2333 {
2334         if (_libproc_no_qsort) {
2335                 return (sym_by_addr_linear(symtab, addr, symp, idp));
2336         } else {
2337                 return (sym_by_addr_binary(symtab, addr, symp, idp));
2338         }
2339 }
2340 
2341 /*
2342  * Use a binary search to do the work of sym_by_name().
2343  */
2344 static GElf_Sym *
2345 sym_by_name_binary(sym_tbl_t *symtab, const char *name, GElf_Sym *symp,
2346     uint_t *idp)
2347 {
2348         char *strs = symtab->sym_strs;
2349         uint_t i, *byname = symtab->sym_byname;
2350         int min, mid, max, cmp;
2351 
2352         if (symtab->sym_data_pri == NULL || strs == NULL ||
2353             symtab->sym_count == 0)
2354                 return (NULL);
2355 
2356         min = 0;
2357         max = symtab->sym_count - 1;
2358 
2359         while (min <= max) {
2360                 mid = (max + min) / 2;
2361 
2362                 i = byname[mid];
2363                 (void) symtab_getsym(symtab, i, symp);
2364 
2365                 if ((cmp = strcmp(name, strs + symp->st_name)) == 0) {
2366                         if (idp != NULL)
2367                                 *idp = i;
2368                         return (symp);
2369                 }
2370 
2371                 if (cmp < 0)
2372                         max = mid - 1;
2373                 else
2374                         min = mid + 1;
2375         }
2376 
2377         return (NULL);
2378 }
2379 
2380 /*
2381  * Use a linear search to do the work of sym_by_name().
2382  */
2383 static GElf_Sym *
2384 sym_by_name_linear(sym_tbl_t *symtab, const char *name, GElf_Sym *symp,
2385     uint_t *idp)
2386 {
2387         size_t symn = symtab->sym_symn;
2388         char *strs = symtab->sym_strs;
2389         int i;
2390 
2391         if (symtab->sym_data_pri == NULL || symn == 0 || strs == NULL)
2392                 return (NULL);
2393 
2394         for (i = 0; i < symn; i++) {
2395                 if (symtab_getsym(symtab, i, symp) &&
2396                     strcmp(name, strs + symp->st_name) == 0) {
2397                         if (idp)
2398                                 *idp = i;
2399                         return (symp);
2400                 }
2401         }
2402 
2403         return (NULL);
2404 }
2405 
2406 /*
2407  * Look up a symbol by name in the specified symbol table.
2408  *
2409  * Use a linear or a binary search depending on whether or not we
2410  * chose to sort the table in optimize_symtab().
2411  */
2412 static GElf_Sym *
2413 sym_by_name(sym_tbl_t *symtab, const char *name, GElf_Sym *symp, uint_t *idp)
2414 {
2415         if (_libproc_no_qsort) {
2416                 return (sym_by_name_linear(symtab, name, symp, idp));
2417         } else {
2418                 return (sym_by_name_binary(symtab, name, symp, idp));
2419         }
2420 }
2421 
2422 /*
2423  * Search the process symbol tables looking for a symbol whose
2424  * value to value+size contain the address specified by addr.
2425  * Return values are:
2426  *      sym_name_buffer containing the symbol name
2427  *      GElf_Sym symbol table entry
2428  *      prsyminfo_t ancillary symbol information
2429  * Returns 0 on success, -1 on failure.
2430  */
2431 static int
2432 i_Pxlookup_by_addr(
2433         struct ps_prochandle *P,
2434         int lmresolve,                  /* use resolve linker object names */
2435         uintptr_t addr,                 /* process address being sought */
2436         char *sym_name_buffer,          /* buffer for the symbol name */
2437         size_t bufsize,                 /* size of sym_name_buffer */
2438         GElf_Sym *symbolp,              /* returned symbol table entry */
2439         prsyminfo_t *sip)               /* returned symbol info */
2440 {
2441         GElf_Sym        *symp;
2442         char            *name;
2443         GElf_Sym        sym1, *sym1p = NULL;
2444         GElf_Sym        sym2, *sym2p = NULL;
2445         char            *name1 = NULL;
2446         char            *name2 = NULL;
2447         uint_t          i1;
2448         uint_t          i2;
2449         map_info_t      *mptr;
2450         file_info_t     *fptr;
2451 
2452         (void) Prd_agent(P);
2453 
2454         if ((mptr = Paddr2mptr(P, addr)) == NULL ||     /* no such address */
2455             (fptr = build_map_symtab(P, mptr)) == NULL || /* no mapped file */
2456             fptr->file_elf == NULL)                  /* not an ELF file */
2457                 return (-1);
2458 
2459         /*
2460          * Adjust the address by the load object base address in
2461          * case the address turns out to be in a shared library.
2462          */
2463         addr -= fptr->file_dyn_base;
2464 
2465         /*
2466          * Search both symbol tables, symtab first, then dynsym.
2467          */
2468         if ((sym1p = sym_by_addr(&fptr->file_symtab, addr, &sym1, &i1)) != NULL)
2469                 name1 = fptr->file_symtab.sym_strs + sym1.st_name;
2470         if ((sym2p = sym_by_addr(&fptr->file_dynsym, addr, &sym2, &i2)) != NULL)
2471                 name2 = fptr->file_dynsym.sym_strs + sym2.st_name;
2472 
2473         if ((symp = sym_prefer(sym1p, name1, sym2p, name2)) == NULL)
2474                 return (-1);
2475 
2476         name = (symp == sym1p) ? name1 : name2;
2477         if (bufsize > 0) {
2478                 (void) strncpy(sym_name_buffer, name, bufsize);
2479                 sym_name_buffer[bufsize - 1] = '\0';
2480         }
2481 
2482         *symbolp = *symp;
2483         if (sip != NULL) {
2484                 sip->prs_name = bufsize == 0 ? NULL : sym_name_buffer;
2485                 if (lmresolve && (fptr->file_rname != NULL))
2486                         sip->prs_object = fptr->file_rbase;
2487                 else
2488                         sip->prs_object = fptr->file_lbase;
2489                 sip->prs_id = (symp == sym1p) ? i1 : i2;
2490                 sip->prs_table = (symp == sym1p) ? PR_SYMTAB : PR_DYNSYM;
2491                 sip->prs_lmid = (fptr->file_lo == NULL) ? LM_ID_BASE :
2492                     fptr->file_lo->rl_lmident;
2493         }
2494 
2495         if (GELF_ST_TYPE(symbolp->st_info) != STT_TLS)
2496                 symbolp->st_value += fptr->file_dyn_base;
2497 
2498         return (0);
2499 }
2500 
2501 int
2502 Pxlookup_by_addr(struct ps_prochandle *P, uintptr_t addr, char *buf,
2503     size_t bufsize, GElf_Sym *symp, prsyminfo_t *sip)
2504 {
2505         return (i_Pxlookup_by_addr(P, B_FALSE, addr, buf, bufsize, symp, sip));
2506 }
2507 
2508 int
2509 Pxlookup_by_addr_resolved(struct ps_prochandle *P, uintptr_t addr, char *buf,
2510     size_t bufsize, GElf_Sym *symp, prsyminfo_t *sip)
2511 {
2512         return (i_Pxlookup_by_addr(P, B_TRUE, addr, buf, bufsize, symp, sip));
2513 }
2514 
2515 int
2516 Plookup_by_addr(struct ps_prochandle *P, uintptr_t addr, char *buf,
2517     size_t size, GElf_Sym *symp)
2518 {
2519         return (i_Pxlookup_by_addr(P, B_FALSE, addr, buf, size, symp, NULL));
2520 }
2521 
2522 /*
2523  * Search the process symbol tables looking for a symbol whose name matches the
2524  * specified name and whose object and link map optionally match the specified
2525  * parameters.  On success, the function returns 0 and fills in the GElf_Sym
2526  * symbol table entry.  On failure, -1 is returned.
2527  */
2528 int
2529 Pxlookup_by_name(
2530         struct ps_prochandle *P,
2531         Lmid_t lmid,                    /* link map to match, or -1 for any */
2532         const char *oname,              /* load object name */
2533         const char *sname,              /* symbol name */
2534         GElf_Sym *symp,                 /* returned symbol table entry */
2535         prsyminfo_t *sip)               /* returned symbol info */
2536 {
2537         map_info_t *mptr;
2538         file_info_t *fptr;
2539         int cnt;
2540 
2541         GElf_Sym sym;
2542         prsyminfo_t si;
2543         int rv = -1;
2544         uint_t id;
2545 
2546         if (oname == PR_OBJ_EVERY) {
2547                 /* create all the file_info_t's for all the mappings */
2548                 (void) Prd_agent(P);
2549                 cnt = P->num_files;
2550                 fptr = list_next(&P->file_head);
2551         } else {
2552                 cnt = 1;
2553                 if ((mptr = object_name_to_map(P, lmid, oname)) == NULL ||
2554                     (fptr = build_map_symtab(P, mptr)) == NULL)
2555                         return (-1);
2556         }
2557 
2558         /*
2559          * Iterate through the loaded object files and look for the symbol
2560          * name in the .symtab and .dynsym of each.  If we encounter a match
2561          * with SHN_UNDEF, keep looking in hopes of finding a better match.
2562          * This means that a name such as "puts" will match the puts function
2563          * in libc instead of matching the puts PLT entry in the a.out file.
2564          */
2565         for (; cnt > 0; cnt--, fptr = list_next(fptr)) {
2566                 Pbuild_file_symtab(P, fptr);
2567 
2568                 if (fptr->file_elf == NULL)
2569                         continue;
2570 
2571                 if (lmid != PR_LMID_EVERY && fptr->file_lo != NULL &&
2572                     lmid != fptr->file_lo->rl_lmident)
2573                         continue;
2574 
2575                 if (fptr->file_symtab.sym_data_pri != NULL &&
2576                     sym_by_name(&fptr->file_symtab, sname, symp, &id)) {
2577                         if (sip != NULL) {
2578                                 sip->prs_id = id;
2579                                 sip->prs_table = PR_SYMTAB;
2580                                 sip->prs_object = oname;
2581                                 sip->prs_name = sname;
2582                                 sip->prs_lmid = fptr->file_lo == NULL ?
2583                                     LM_ID_BASE : fptr->file_lo->rl_lmident;
2584                         }
2585                 } else if (fptr->file_dynsym.sym_data_pri != NULL &&
2586                     sym_by_name(&fptr->file_dynsym, sname, symp, &id)) {
2587                         if (sip != NULL) {
2588                                 sip->prs_id = id;
2589                                 sip->prs_table = PR_DYNSYM;
2590                                 sip->prs_object = oname;
2591                                 sip->prs_name = sname;
2592                                 sip->prs_lmid = fptr->file_lo == NULL ?
2593                                     LM_ID_BASE : fptr->file_lo->rl_lmident;
2594                         }
2595                 } else {
2596                         continue;
2597                 }
2598 
2599                 if (GELF_ST_TYPE(symp->st_info) != STT_TLS)
2600                         symp->st_value += fptr->file_dyn_base;
2601 
2602                 if (symp->st_shndx != SHN_UNDEF)
2603                         return (0);
2604 
2605                 if (rv != 0) {
2606                         if (sip != NULL)
2607                                 si = *sip;
2608                         sym = *symp;
2609                         rv = 0;
2610                 }
2611         }
2612 
2613         if (rv == 0) {
2614                 if (sip != NULL)
2615                         *sip = si;
2616                 *symp = sym;
2617         }
2618 
2619         return (rv);
2620 }
2621 
2622 /*
2623  * Search the process symbol tables looking for a symbol whose name matches the
2624  * specified name, but without any restriction on the link map id.
2625  */
2626 int
2627 Plookup_by_name(struct ps_prochandle *P, const char *object,
2628         const char *symbol, GElf_Sym *symp)
2629 {
2630         return (Pxlookup_by_name(P, PR_LMID_EVERY, object, symbol, symp, NULL));
2631 }
2632 
2633 /*
2634  * Iterate over the process's address space mappings.
2635  */
2636 static int
2637 i_Pmapping_iter(struct ps_prochandle *P, boolean_t lmresolve,
2638     proc_map_f *func, void *cd)
2639 {
2640         map_info_t *mptr;
2641         file_info_t *fptr;
2642         char *object_name;
2643         int rc = 0;
2644         int i;
2645 
2646         /* create all the file_info_t's for all the mappings */
2647         (void) Prd_agent(P);
2648 
2649         for (i = 0, mptr = P->mappings; i < P->map_count; i++, mptr++) {
2650                 if ((fptr = mptr->map_file) == NULL)
2651                         object_name = NULL;
2652                 else if (lmresolve && (fptr->file_rname != NULL))
2653                         object_name = fptr->file_rname;
2654                 else
2655                         object_name = fptr->file_lname;
2656                 if ((rc = func(cd, &mptr->map_pmap, object_name)) != 0)
2657                         return (rc);
2658         }
2659         return (0);
2660 }
2661 
2662 int
2663 Pmapping_iter(struct ps_prochandle *P, proc_map_f *func, void *cd)
2664 {
2665         return (i_Pmapping_iter(P, B_FALSE, func, cd));
2666 }
2667 
2668 int
2669 Pmapping_iter_resolved(struct ps_prochandle *P, proc_map_f *func, void *cd)
2670 {
2671         return (i_Pmapping_iter(P, B_TRUE, func, cd));
2672 }
2673 
2674 /*
2675  * Iterate over the process's mapped objects.
2676  */
2677 static int
2678 i_Pobject_iter(struct ps_prochandle *P, boolean_t lmresolve,
2679     proc_map_f *func, void *cd)
2680 {
2681         map_info_t *mptr;
2682         file_info_t *fptr;
2683         uint_t cnt;
2684         int rc = 0;
2685 
2686         (void) Prd_agent(P); /* create file_info_t's for all the mappings */
2687         Pupdate_maps(P);
2688 
2689         for (cnt = P->num_files, fptr = list_next(&P->file_head);
2690             cnt; cnt--, fptr = list_next(fptr)) {
2691                 const char *lname;
2692 
2693                 if (lmresolve && (fptr->file_rname != NULL))
2694                         lname = fptr->file_rname;
2695                 else if (fptr->file_lname != NULL)
2696                         lname = fptr->file_lname;
2697                 else
2698                         lname = "";
2699 
2700                 if ((mptr = fptr->file_map) == NULL)
2701                         continue;
2702 
2703                 if ((rc = func(cd, &mptr->map_pmap, lname)) != 0)
2704                         return (rc);
2705 
2706                 if (!P->info_valid)
2707                         Pupdate_maps(P);
2708         }
2709         return (0);
2710 }
2711 
2712 int
2713 Pobject_iter(struct ps_prochandle *P, proc_map_f *func, void *cd)
2714 {
2715         return (i_Pobject_iter(P, B_FALSE, func, cd));
2716 }
2717 
2718 int
2719 Pobject_iter_resolved(struct ps_prochandle *P, proc_map_f *func, void *cd)
2720 {
2721         return (i_Pobject_iter(P, B_TRUE, func, cd));
2722 }
2723 
2724 static char *
2725 i_Pobjname(struct ps_prochandle *P, boolean_t lmresolve, uintptr_t addr,
2726         char *buffer, size_t bufsize)
2727 {
2728         map_info_t *mptr;
2729         file_info_t *fptr;
2730 
2731         /* create all the file_info_t's for all the mappings */
2732         (void) Prd_agent(P);
2733 
2734         if ((mptr = Paddr2mptr(P, addr)) == NULL)
2735                 return (NULL);
2736 
2737         if (!lmresolve) {
2738                 if (((fptr = mptr->map_file) == NULL) ||
2739                     (fptr->file_lname == NULL))
2740                         return (NULL);
2741                 (void) strlcpy(buffer, fptr->file_lname, bufsize);
2742                 return (buffer);
2743         }
2744 
2745         /* Check for a cached copy of the resolved path */
2746         if (Pfindmap(P, mptr, buffer, bufsize) != NULL)
2747                 return (buffer);
2748 
2749         return (NULL);
2750 }
2751 
2752 /*
2753  * Given a virtual address, return the name of the underlying
2754  * mapped object (file) as provided by the dynamic linker.
2755  * Return NULL if we can't find any name information for the object.
2756  */
2757 char *
2758 Pobjname(struct ps_prochandle *P, uintptr_t addr,
2759         char *buffer, size_t bufsize)
2760 {
2761         return (i_Pobjname(P, B_FALSE, addr, buffer, bufsize));
2762 }
2763 
2764 /*
2765  * Given a virtual address, try to return a filesystem path to the
2766  * underlying mapped object (file).  If we're in the global zone,
2767  * this path could resolve to an object in another zone.  If we're
2768  * unable return a valid filesystem path, we'll fall back to providing
2769  * the mapped object (file) name provided by the dynamic linker in
2770  * the target process (ie, the object reported by Pobjname()).
2771  */
2772 char *
2773 Pobjname_resolved(struct ps_prochandle *P, uintptr_t addr,
2774         char *buffer, size_t bufsize)
2775 {
2776         return (i_Pobjname(P, B_TRUE, addr, buffer, bufsize));
2777 }
2778 
2779 /*
2780  * Given a virtual address, return the link map id of the underlying mapped
2781  * object (file), as provided by the dynamic linker.  Return -1 on failure.
2782  */
2783 int
2784 Plmid(struct ps_prochandle *P, uintptr_t addr, Lmid_t *lmidp)
2785 {
2786         map_info_t *mptr;
2787         file_info_t *fptr;
2788 
2789         /* create all the file_info_t's for all the mappings */
2790         (void) Prd_agent(P);
2791 
2792         if ((mptr = Paddr2mptr(P, addr)) != NULL &&
2793             (fptr = mptr->map_file) != NULL && fptr->file_lo != NULL) {
2794                 *lmidp = fptr->file_lo->rl_lmident;
2795                 return (0);
2796         }
2797 
2798         return (-1);
2799 }
2800 
2801 /*
2802  * Given an object name and optional lmid, iterate over the object's symbols.
2803  * If which == PR_SYMTAB, search the normal symbol table.
2804  * If which == PR_DYNSYM, search the dynamic symbol table.
2805  */
2806 static int
2807 Psymbol_iter_com(struct ps_prochandle *P, Lmid_t lmid, const char *object_name,
2808     int which, int mask, pr_order_t order, proc_xsym_f *func, void *cd)
2809 {
2810 #if STT_NUM != (STT_TLS + 1)
2811 #error "STT_NUM has grown. update Psymbol_iter_com()"
2812 #endif
2813 
2814         GElf_Sym sym;
2815         GElf_Shdr shdr;
2816         map_info_t *mptr;
2817         file_info_t *fptr;
2818         sym_tbl_t *symtab;
2819         size_t symn;
2820         const char *strs;
2821         size_t strsz;
2822         prsyminfo_t si;
2823         int rv;
2824         uint_t *map, i, count, ndx;
2825 
2826         if ((mptr = object_name_to_map(P, lmid, object_name)) == NULL)
2827                 return (-1);
2828 
2829         if ((fptr = build_map_symtab(P, mptr)) == NULL || /* no mapped file */
2830             fptr->file_elf == NULL)                  /* not an ELF file */
2831                 return (-1);
2832 
2833         /*
2834          * Search the specified symbol table.
2835          */
2836         switch (which) {
2837         case PR_SYMTAB:
2838                 symtab = &fptr->file_symtab;
2839                 si.prs_table = PR_SYMTAB;
2840                 break;
2841         case PR_DYNSYM:
2842                 symtab = &fptr->file_dynsym;
2843                 si.prs_table = PR_DYNSYM;
2844                 break;
2845         default:
2846                 return (-1);
2847         }
2848 
2849         si.prs_object = object_name;
2850         si.prs_lmid = fptr->file_lo == NULL ?
2851             LM_ID_BASE : fptr->file_lo->rl_lmident;
2852 
2853         symn = symtab->sym_symn;
2854         strs = symtab->sym_strs;
2855         strsz = symtab->sym_strsz;
2856 
2857         switch (order) {
2858         case PRO_NATURAL:
2859                 map = NULL;
2860                 count = symn;
2861                 break;
2862         case PRO_BYNAME:
2863                 map = symtab->sym_byname;
2864                 count = symtab->sym_count;
2865                 break;
2866         case PRO_BYADDR:
2867                 map = symtab->sym_byaddr;
2868                 count = symtab->sym_count;
2869                 break;
2870         default:
2871                 return (-1);
2872         }
2873 
2874         if (symtab->sym_data_pri == NULL || strs == NULL || count == 0)
2875                 return (-1);
2876 
2877         rv = 0;
2878 
2879         for (i = 0; i < count; i++) {
2880                 ndx = map == NULL ? i : map[i];
2881                 if (symtab_getsym(symtab, ndx, &sym) != NULL) {
2882                         uint_t s_bind, s_type, type;
2883 
2884                         if (sym.st_name >= strsz)    /* invalid st_name */
2885                                 continue;
2886 
2887                         s_bind = GELF_ST_BIND(sym.st_info);
2888                         s_type = GELF_ST_TYPE(sym.st_info);
2889 
2890                         /*
2891                          * In case you haven't already guessed, this relies on
2892                          * the bitmask used in <libproc.h> for encoding symbol
2893                          * type and binding matching the order of STB and STT
2894                          * constants in <sys/elf.h>.  Changes to ELF must
2895                          * maintain binary compatibility, so I think this is
2896                          * reasonably fair game.
2897                          */
2898                         if (s_bind < STB_NUM && s_type < STT_NUM) {
2899                                 type = (1 << (s_type + 8)) | (1 << s_bind);
2900                                 if ((type & ~mask) != 0)
2901                                         continue;
2902                         } else
2903                                 continue; /* Invalid type or binding */
2904 
2905                         if (GELF_ST_TYPE(sym.st_info) != STT_TLS)
2906                                 sym.st_value += fptr->file_dyn_base;
2907 
2908                         si.prs_name = strs + sym.st_name;
2909 
2910                         /*
2911                          * If symbol's type is STT_SECTION, then try to lookup
2912                          * the name of the corresponding section.
2913                          */
2914                         if (GELF_ST_TYPE(sym.st_info) == STT_SECTION &&
2915                             fptr->file_shstrs != NULL &&
2916                             gelf_getshdr(elf_getscn(fptr->file_elf,
2917                             sym.st_shndx), &shdr) != NULL &&
2918                             shdr.sh_name != 0 &&
2919                             shdr.sh_name < fptr->file_shstrsz)
2920                                 si.prs_name = fptr->file_shstrs + shdr.sh_name;
2921 
2922                         si.prs_id = ndx;
2923                         if ((rv = func(cd, &sym, si.prs_name, &si)) != 0)
2924                                 break;
2925                 }
2926         }
2927 
2928         return (rv);
2929 }
2930 
2931 int
2932 Pxsymbol_iter(struct ps_prochandle *P, Lmid_t lmid, const char *object_name,
2933     int which, int mask, proc_xsym_f *func, void *cd)
2934 {
2935         return (Psymbol_iter_com(P, lmid, object_name, which, mask,
2936             PRO_NATURAL, func, cd));
2937 }
2938 
2939 int
2940 Psymbol_iter_by_lmid(struct ps_prochandle *P, Lmid_t lmid,
2941     const char *object_name, int which, int mask, proc_sym_f *func, void *cd)
2942 {
2943         return (Psymbol_iter_com(P, lmid, object_name, which, mask,
2944             PRO_NATURAL, (proc_xsym_f *)func, cd));
2945 }
2946 
2947 int
2948 Psymbol_iter(struct ps_prochandle *P,
2949     const char *object_name, int which, int mask, proc_sym_f *func, void *cd)
2950 {
2951         return (Psymbol_iter_com(P, PR_LMID_EVERY, object_name, which, mask,
2952             PRO_NATURAL, (proc_xsym_f *)func, cd));
2953 }
2954 
2955 int
2956 Psymbol_iter_by_addr(struct ps_prochandle *P,
2957     const char *object_name, int which, int mask, proc_sym_f *func, void *cd)
2958 {
2959         return (Psymbol_iter_com(P, PR_LMID_EVERY, object_name, which, mask,
2960             PRO_BYADDR, (proc_xsym_f *)func, cd));
2961 }
2962 
2963 int
2964 Psymbol_iter_by_name(struct ps_prochandle *P,
2965     const char *object_name, int which, int mask, proc_sym_f *func, void *cd)
2966 {
2967         return (Psymbol_iter_com(P, PR_LMID_EVERY, object_name, which, mask,
2968             PRO_BYNAME, (proc_xsym_f *)func, cd));
2969 }
2970 
2971 /*
2972  * Get the platform string from the core file if we have it;
2973  * just perform the system call for the caller if this is a live process.
2974  */
2975 char *
2976 Pplatform(struct ps_prochandle *P, char *s, size_t n)
2977 {
2978         if (P->state == PS_IDLE) {
2979                 errno = ENODATA;
2980                 return (NULL);
2981         }
2982 
2983         if (P->state == PS_DEAD) {
2984                 if (P->core->core_platform == NULL) {
2985                         errno = ENODATA;
2986                         return (NULL);
2987                 }
2988                 (void) strncpy(s, P->core->core_platform, n - 1);
2989                 s[n - 1] = '\0';
2990 
2991         } else if (sysinfo(SI_PLATFORM, s, n) == -1)
2992                 return (NULL);
2993 
2994         return (s);
2995 }
2996 
2997 /*
2998  * Get the uname(2) information from the core file if we have it;
2999  * just perform the system call for the caller if this is a live process.
3000  */
3001 int
3002 Puname(struct ps_prochandle *P, struct utsname *u)
3003 {
3004         if (P->state == PS_IDLE) {
3005                 errno = ENODATA;
3006                 return (-1);
3007         }
3008 
3009         if (P->state == PS_DEAD) {
3010                 if (P->core->core_uts == NULL) {
3011                         errno = ENODATA;
3012                         return (-1);
3013                 }
3014                 (void) memcpy(u, P->core->core_uts, sizeof (struct utsname));
3015                 return (0);
3016         }
3017         return (uname(u));
3018 }
3019 
3020 /*
3021  * Called from Pcreate(), Pgrab(), and Pfgrab_core() to initialize
3022  * the symbol table heads in the new ps_prochandle.
3023  */
3024 void
3025 Pinitsym(struct ps_prochandle *P)
3026 {
3027         P->num_files = 0;
3028         list_link(&P->file_head, NULL);
3029 }
3030 
3031 /*
3032  * Called from Prelease() to destroy the symbol tables.
3033  * Must be called by the client after an exec() in the victim process.
3034  */
3035 void
3036 Preset_maps(struct ps_prochandle *P)
3037 {
3038         int i;
3039 
3040         if (P->rap != NULL) {
3041                 rd_delete(P->rap);
3042                 P->rap = NULL;
3043         }
3044 
3045         if (P->execname != NULL) {
3046                 free(P->execname);
3047                 P->execname = NULL;
3048         }
3049 
3050         if (P->auxv != NULL) {
3051                 free(P->auxv);
3052                 P->auxv = NULL;
3053                 P->nauxv = 0;
3054         }
3055 
3056         for (i = 0; i < P->map_count; i++)
3057                 map_info_free(P, &P->mappings[i]);
3058 
3059         if (P->mappings != NULL) {
3060                 free(P->mappings);
3061                 P->mappings = NULL;
3062         }
3063         P->map_count = P->map_alloc = 0;
3064 
3065         P->info_valid = 0;
3066 }
3067 
3068 typedef struct getenv_data {
3069         char *buf;
3070         size_t bufsize;
3071         const char *search;
3072         size_t searchlen;
3073 } getenv_data_t;
3074 
3075 /*ARGSUSED*/
3076 static int
3077 getenv_func(void *data, struct ps_prochandle *P, uintptr_t addr,
3078     const char *nameval)
3079 {
3080         getenv_data_t *d = data;
3081         size_t len;
3082 
3083         if (nameval == NULL)
3084                 return (0);
3085 
3086         if (d->searchlen < strlen(nameval) &&
3087             strncmp(nameval, d->search, d->searchlen) == 0 &&
3088             nameval[d->searchlen] == '=') {
3089                 len = MIN(strlen(nameval), d->bufsize - 1);
3090                 (void) strncpy(d->buf, nameval, len);
3091                 d->buf[len] = '\0';
3092                 return (1);
3093         }
3094 
3095         return (0);
3096 }
3097 
3098 char *
3099 Pgetenv(struct ps_prochandle *P, const char *name, char *buf, size_t buflen)
3100 {
3101         getenv_data_t d;
3102 
3103         d.buf = buf;
3104         d.bufsize = buflen;
3105         d.search = name;
3106         d.searchlen = strlen(name);
3107 
3108         if (Penv_iter(P, getenv_func, &d) == 1) {
3109                 char *equals = strchr(d.buf, '=');
3110 
3111                 if (equals != NULL) {
3112                         (void) memmove(d.buf, equals + 1,
3113                             d.buf + buflen - equals - 1);
3114                         d.buf[d.buf + buflen - equals] = '\0';
3115 
3116                         return (buf);
3117                 }
3118         }
3119 
3120         return (NULL);
3121 }
3122 
3123 /* number of argument or environment pointers to read all at once */
3124 #define NARG    100
3125 
3126 int
3127 Penv_iter(struct ps_prochandle *P, proc_env_f *func, void *data)
3128 {
3129         const psinfo_t *psp;
3130         uintptr_t envpoff;
3131         GElf_Sym sym;
3132         int ret;
3133         char *buf, *nameval;
3134         size_t buflen;
3135 
3136         int nenv = NARG;
3137         long envp[NARG];
3138 
3139         /*
3140          * Attempt to find the "_environ" variable in the process.
3141          * Failing that, use the original value provided by Ppsinfo().
3142          */
3143         if ((psp = Ppsinfo(P)) == NULL)
3144                 return (-1);
3145 
3146         envpoff = psp->pr_envp; /* Default if no _environ found */
3147 
3148         if (Plookup_by_name(P, PR_OBJ_EXEC, "_environ", &sym) == 0) {
3149                 if (P->status.pr_dmodel == PR_MODEL_NATIVE) {
3150                         if (Pread(P, &envpoff, sizeof (envpoff),
3151                             sym.st_value) != sizeof (envpoff))
3152                                 envpoff = psp->pr_envp;
3153                 } else if (P->status.pr_dmodel == PR_MODEL_ILP32) {
3154                         uint32_t envpoff32;
3155 
3156                         if (Pread(P, &envpoff32, sizeof (envpoff32),
3157                             sym.st_value) != sizeof (envpoff32))
3158                                 envpoff = psp->pr_envp;
3159                         else
3160                                 envpoff = envpoff32;
3161                 }
3162         }
3163 
3164         buflen = 128;
3165         buf = malloc(buflen);
3166 
3167         ret = 0;
3168         for (;;) {
3169                 uintptr_t envoff;
3170 
3171                 if (nenv == NARG) {
3172                         (void) memset(envp, 0, sizeof (envp));
3173                         if (P->status.pr_dmodel == PR_MODEL_NATIVE) {
3174                                 if (Pread(P, envp,
3175                                     sizeof (envp), envpoff) <= 0) {
3176                                         ret = -1;
3177                                         break;
3178                                 }
3179                         } else if (P->status.pr_dmodel == PR_MODEL_ILP32) {
3180                                 uint32_t e32[NARG];
3181                                 int i;
3182 
3183                                 (void) memset(e32, 0, sizeof (e32));
3184                                 if (Pread(P, e32, sizeof (e32), envpoff) <= 0) {
3185                                         ret = -1;
3186                                         break;
3187                                 }
3188                                 for (i = 0; i < NARG; i++)
3189                                         envp[i] = e32[i];
3190                         }
3191                         nenv = 0;
3192                 }
3193 
3194                 if ((envoff = envp[nenv++]) == NULL)
3195                         break;
3196 
3197                 /*
3198                  * Attempt to read the string from the process.
3199                  */
3200 again:
3201                 ret = Pread_string(P, buf, buflen, envoff);
3202 
3203                 if (ret <= 0) {
3204                         nameval = NULL;
3205                 } else if (ret == buflen - 1) {
3206                         free(buf);
3207                         /*
3208                          * Bail if we have a corrupted environment
3209                          */
3210                         if (buflen >= ARG_MAX)
3211                                 return (-1);
3212                         buflen *= 2;
3213                         buf = malloc(buflen);
3214                         goto again;
3215                 } else {
3216                         nameval = buf;
3217                 }
3218 
3219                 if ((ret = func(data, P, envoff, nameval)) != 0)
3220                         break;
3221 
3222                 envpoff += (P->status.pr_dmodel == PR_MODEL_LP64)? 8 : 4;
3223         }
3224 
3225         free(buf);
3226 
3227         return (ret);
3228 }