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 2008 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 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 free(fptr->file_ctf_buf); 802 fptr->file_ctf_buf = NULL; 803 return (NULL); 804 } 805 806 dprintf("loaded %lu bytes of CTF data for %s\n", 807 (ulong_t)fptr->file_ctf_size, fptr->file_pname); 808 809 return (fptr->file_ctfp); 810 } 811 812 ctf_file_t * 813 Paddr_to_ctf(struct ps_prochandle *P, uintptr_t addr) 814 { 815 map_info_t *mptr; 816 file_info_t *fptr; 817 818 if (!P->info_valid) 819 Pupdate_maps(P); 820 821 if ((mptr = Paddr2mptr(P, addr)) == NULL || 822 (fptr = mptr->map_file) == NULL) 823 return (NULL); 824 825 return (Pbuild_file_ctf(P, fptr)); 826 } 827 828 ctf_file_t * 829 Plmid_to_ctf(struct ps_prochandle *P, Lmid_t lmid, const char *name) 830 { 831 map_info_t *mptr; 832 file_info_t *fptr; 833 834 if (name == PR_OBJ_EVERY) 835 return (NULL); 836 837 if ((mptr = object_name_to_map(P, lmid, name)) == NULL || 838 (fptr = mptr->map_file) == NULL) 839 return (NULL); 840 841 return (Pbuild_file_ctf(P, fptr)); 842 } 843 844 ctf_file_t * 845 Pname_to_ctf(struct ps_prochandle *P, const char *name) 846 { 847 return (Plmid_to_ctf(P, PR_LMID_EVERY, name)); 848 } 849 850 /* 851 * If we're not a core file, re-read the /proc/<pid>/auxv file and store 852 * its contents in P->auxv. In the case of a core file, we either 853 * initialized P->auxv in Pcore() from the NT_AUXV, or we don't have an 854 * auxv because the note was missing. 855 */ 856 void 857 Preadauxvec(struct ps_prochandle *P) 858 { 859 char auxfile[64]; 860 struct stat statb; 861 ssize_t naux; 862 int fd; 863 864 if (P->state == PS_DEAD) 865 return; /* Already read during Pgrab_core() */ 866 if (P->state == PS_IDLE) 867 return; /* No aux vec for Pgrab_file() */ 868 869 if (P->auxv != NULL) { 870 free(P->auxv); 871 P->auxv = NULL; 872 P->nauxv = 0; 873 } 874 875 (void) snprintf(auxfile, sizeof (auxfile), "%s/%d/auxv", 876 procfs_path, (int)P->pid); 877 if ((fd = open(auxfile, O_RDONLY)) < 0) 878 return; 879 880 if (fstat(fd, &statb) == 0 && 881 statb.st_size >= sizeof (auxv_t) && 882 (P->auxv = malloc(statb.st_size + sizeof (auxv_t))) != NULL) { 883 if ((naux = read(fd, P->auxv, statb.st_size)) < 0 || 884 (naux /= sizeof (auxv_t)) < 1) { 885 free(P->auxv); 886 P->auxv = NULL; 887 } else { 888 P->auxv[naux].a_type = AT_NULL; 889 P->auxv[naux].a_un.a_val = 0L; 890 P->nauxv = (int)naux; 891 } 892 } 893 894 (void) close(fd); 895 } 896 897 /* 898 * Return a requested element from the process's aux vector. 899 * Return -1 on failure (this is adequate for our purposes). 900 */ 901 long 902 Pgetauxval(struct ps_prochandle *P, int type) 903 { 904 auxv_t *auxv; 905 906 if (P->auxv == NULL) 907 Preadauxvec(P); 908 909 if (P->auxv == NULL) 910 return (-1); 911 912 for (auxv = P->auxv; auxv->a_type != AT_NULL; auxv++) { 913 if (auxv->a_type == type) 914 return (auxv->a_un.a_val); 915 } 916 917 return (-1); 918 } 919 920 /* 921 * Return a pointer to our internal copy of the process's aux vector. 922 * The caller should not hold on to this pointer across any libproc calls. 923 */ 924 const auxv_t * 925 Pgetauxvec(struct ps_prochandle *P) 926 { 927 static const auxv_t empty = { AT_NULL, 0L }; 928 929 if (P->auxv == NULL) 930 Preadauxvec(P); 931 932 if (P->auxv == NULL) 933 return (&empty); 934 935 return (P->auxv); 936 } 937 938 /* 939 * Return 1 if the given mapping corresponds to the given file_info_t's 940 * load object; return 0 otherwise. 941 */ 942 static int 943 is_mapping_in_file(struct ps_prochandle *P, map_info_t *mptr, file_info_t *fptr) 944 { 945 prmap_t *pmap = &mptr->map_pmap; 946 rd_loadobj_t *lop = fptr->file_lo; 947 uint_t i; 948 uintptr_t mstart, mend, sstart, send; 949 950 /* 951 * We can get for free the start address of the text and data 952 * sections of the load object. Start by seeing if the mapping 953 * encloses either of these. 954 */ 955 if ((pmap->pr_vaddr <= lop->rl_base && 956 lop->rl_base < pmap->pr_vaddr + pmap->pr_size) || 957 (pmap->pr_vaddr <= lop->rl_data_base && 958 lop->rl_data_base < pmap->pr_vaddr + pmap->pr_size)) 959 return (1); 960 961 /* 962 * It's still possible that this mapping correponds to the load 963 * object. Consider the example of a mapping whose start and end 964 * addresses correspond to those of the load object's text section. 965 * If the mapping splits, e.g. as a result of a segment demotion, 966 * then although both mappings are still backed by the same section, 967 * only one will be seen to enclose that section's start address. 968 * Thus, to be rigorous, we ask not whether this mapping encloses 969 * the start of a section, but whether there exists a section that 970 * overlaps this mapping. 971 * 972 * If we don't already have the section addresses, and we successfully 973 * get them, then we cache them in case we come here again. 974 */ 975 if (fptr->file_saddrs == NULL && 976 (fptr->file_saddrs = get_saddrs(P, 977 fptr->file_map->map_pmap.pr_vaddr, &fptr->file_nsaddrs)) == NULL) 978 return (0); 979 980 mstart = mptr->map_pmap.pr_vaddr; 981 mend = mptr->map_pmap.pr_vaddr + mptr->map_pmap.pr_size; 982 for (i = 0; i < fptr->file_nsaddrs; i += 2) { 983 /* Does this section overlap the mapping? */ 984 sstart = fptr->file_saddrs[i]; 985 send = fptr->file_saddrs[i + 1]; 986 if (!(mend <= sstart || mstart >= send)) 987 return (1); 988 } 989 990 return (0); 991 } 992 993 /* 994 * Find or build the symbol table for the given mapping. 995 */ 996 static file_info_t * 997 build_map_symtab(struct ps_prochandle *P, map_info_t *mptr) 998 { 999 prmap_t *pmap = &mptr->map_pmap; 1000 file_info_t *fptr; 1001 uint_t i; 1002 1003 if ((fptr = mptr->map_file) != NULL) { 1004 Pbuild_file_symtab(P, fptr); 1005 return (fptr); 1006 } 1007 1008 if (pmap->pr_mapname[0] == '\0') 1009 return (NULL); 1010 1011 /* 1012 * Attempt to find a matching file. 1013 * (A file can be mapped at several different addresses.) 1014 */ 1015 for (i = 0, fptr = list_next(&P->file_head); i < P->num_files; 1016 i++, fptr = list_next(fptr)) { 1017 if (strcmp(fptr->file_pname, pmap->pr_mapname) == 0 && 1018 fptr->file_lo && is_mapping_in_file(P, mptr, fptr)) { 1019 mptr->map_file = fptr; 1020 fptr->file_ref++; 1021 Pbuild_file_symtab(P, fptr); 1022 return (fptr); 1023 } 1024 } 1025 1026 /* 1027 * If we need to create a new file_info structure, iterate 1028 * through the load objects in order to attempt to connect 1029 * this new file with its primary text mapping. We again 1030 * need to handle ld.so as a special case because we need 1031 * to be able to bootstrap librtld_db. 1032 */ 1033 if ((fptr = file_info_new(P, mptr)) == NULL) 1034 return (NULL); 1035 1036 if (P->map_ldso != mptr) { 1037 if (P->rap != NULL) 1038 (void) rd_loadobj_iter(P->rap, map_iter, P); 1039 else 1040 (void) Prd_agent(P); 1041 } else { 1042 fptr->file_map = mptr; 1043 } 1044 1045 /* 1046 * If librtld_db wasn't able to help us connect the file to a primary 1047 * text mapping, set file_map to the current mapping because we require 1048 * fptr->file_map to be set in Pbuild_file_symtab. librtld_db may be 1049 * unaware of what's going on in the rare case that a legitimate ELF 1050 * file has been mmap(2)ed into the process address space *without* 1051 * the use of dlopen(3x). 1052 */ 1053 if (fptr->file_map == NULL) 1054 fptr->file_map = mptr; 1055 1056 Pbuild_file_symtab(P, fptr); 1057 1058 return (fptr); 1059 } 1060 1061 static int 1062 read_ehdr32(struct ps_prochandle *P, Elf32_Ehdr *ehdr, uint_t *phnum, 1063 uintptr_t addr) 1064 { 1065 if (Pread(P, ehdr, sizeof (*ehdr), addr) != sizeof (*ehdr)) 1066 return (-1); 1067 1068 if (ehdr->e_ident[EI_MAG0] != ELFMAG0 || 1069 ehdr->e_ident[EI_MAG1] != ELFMAG1 || 1070 ehdr->e_ident[EI_MAG2] != ELFMAG2 || 1071 ehdr->e_ident[EI_MAG3] != ELFMAG3 || 1072 ehdr->e_ident[EI_CLASS] != ELFCLASS32 || 1073 #ifdef _BIG_ENDIAN 1074 ehdr->e_ident[EI_DATA] != ELFDATA2MSB || 1075 #else 1076 ehdr->e_ident[EI_DATA] != ELFDATA2LSB || 1077 #endif 1078 ehdr->e_ident[EI_VERSION] != EV_CURRENT) 1079 return (-1); 1080 1081 if ((*phnum = ehdr->e_phnum) == PN_XNUM) { 1082 Elf32_Shdr shdr0; 1083 1084 if (ehdr->e_shoff == 0 || ehdr->e_shentsize < sizeof (shdr0) || 1085 Pread(P, &shdr0, sizeof (shdr0), addr + ehdr->e_shoff) != 1086 sizeof (shdr0)) 1087 return (-1); 1088 1089 if (shdr0.sh_info != 0) 1090 *phnum = shdr0.sh_info; 1091 } 1092 1093 return (0); 1094 } 1095 1096 static int 1097 read_dynamic_phdr32(struct ps_prochandle *P, const Elf32_Ehdr *ehdr, 1098 uint_t phnum, Elf32_Phdr *phdr, uintptr_t addr) 1099 { 1100 uint_t i; 1101 1102 for (i = 0; i < phnum; i++) { 1103 uintptr_t a = addr + ehdr->e_phoff + i * ehdr->e_phentsize; 1104 if (Pread(P, phdr, sizeof (*phdr), a) != sizeof (*phdr)) 1105 return (-1); 1106 1107 if (phdr->p_type == PT_DYNAMIC) 1108 return (0); 1109 } 1110 1111 return (-1); 1112 } 1113 1114 #ifdef _LP64 1115 static int 1116 read_ehdr64(struct ps_prochandle *P, Elf64_Ehdr *ehdr, uint_t *phnum, 1117 uintptr_t addr) 1118 { 1119 if (Pread(P, ehdr, sizeof (Elf64_Ehdr), addr) != sizeof (Elf64_Ehdr)) 1120 return (-1); 1121 1122 if (ehdr->e_ident[EI_MAG0] != ELFMAG0 || 1123 ehdr->e_ident[EI_MAG1] != ELFMAG1 || 1124 ehdr->e_ident[EI_MAG2] != ELFMAG2 || 1125 ehdr->e_ident[EI_MAG3] != ELFMAG3 || 1126 ehdr->e_ident[EI_CLASS] != ELFCLASS64 || 1127 #ifdef _BIG_ENDIAN 1128 ehdr->e_ident[EI_DATA] != ELFDATA2MSB || 1129 #else 1130 ehdr->e_ident[EI_DATA] != ELFDATA2LSB || 1131 #endif 1132 ehdr->e_ident[EI_VERSION] != EV_CURRENT) 1133 return (-1); 1134 1135 if ((*phnum = ehdr->e_phnum) == PN_XNUM) { 1136 Elf64_Shdr shdr0; 1137 1138 if (ehdr->e_shoff == 0 || ehdr->e_shentsize < sizeof (shdr0) || 1139 Pread(P, &shdr0, sizeof (shdr0), addr + ehdr->e_shoff) != 1140 sizeof (shdr0)) 1141 return (-1); 1142 1143 if (shdr0.sh_info != 0) 1144 *phnum = shdr0.sh_info; 1145 } 1146 1147 return (0); 1148 } 1149 1150 static int 1151 read_dynamic_phdr64(struct ps_prochandle *P, const Elf64_Ehdr *ehdr, 1152 uint_t phnum, Elf64_Phdr *phdr, uintptr_t addr) 1153 { 1154 uint_t i; 1155 1156 for (i = 0; i < phnum; i++) { 1157 uintptr_t a = addr + ehdr->e_phoff + i * ehdr->e_phentsize; 1158 if (Pread(P, phdr, sizeof (*phdr), a) != sizeof (*phdr)) 1159 return (-1); 1160 1161 if (phdr->p_type == PT_DYNAMIC) 1162 return (0); 1163 } 1164 1165 return (-1); 1166 } 1167 #endif /* _LP64 */ 1168 1169 /* 1170 * The text segment for each load object contains the elf header and 1171 * program headers. We can use this information to determine if the 1172 * file that corresponds to the load object is the same file that 1173 * was loaded into the process's address space. There can be a discrepency 1174 * if a file is recompiled after the process is started or if the target 1175 * represents a core file from a differently configured system -- two 1176 * common examples. The DT_CHECKSUM entry in the dynamic section 1177 * provides an easy method of comparison. It is important to note that 1178 * the dynamic section usually lives in the data segment, but the meta 1179 * data we use to find the dynamic section lives in the text segment so 1180 * if either of those segments is absent we can't proceed. 1181 * 1182 * We're looking through the elf file for several items: the symbol tables 1183 * (both dynsym and symtab), the procedure linkage table (PLT) base, 1184 * size, and relocation base, and the CTF information. Most of this can 1185 * be recovered from the loaded image of the file itself, the exceptions 1186 * being the symtab and CTF data. 1187 * 1188 * First we try to open the file that we think corresponds to the load 1189 * object, if the DT_CHECKSUM values match, we're all set, and can simply 1190 * recover all the information we need from the file. If the values of 1191 * DT_CHECKSUM don't match, or if we can't access the file for whatever 1192 * reasaon, we fake up a elf file to use in its stead. If we can't read 1193 * the elf data in the process's address space, we fall back to using 1194 * the file even though it may give inaccurate information. 1195 * 1196 * The elf file that we fake up has to consist of sections for the 1197 * dynsym, the PLT and the dynamic section. Note that in the case of a 1198 * core file, we'll get the CTF data in the file_info_t later on from 1199 * a section embedded the core file (if it's present). 1200 * 1201 * file_differs() conservatively looks for mismatched files, identifying 1202 * a match when there is any ambiguity (since that's the legacy behavior). 1203 */ 1204 static int 1205 file_differs(struct ps_prochandle *P, Elf *elf, file_info_t *fptr) 1206 { 1207 Elf_Scn *scn; 1208 GElf_Shdr shdr; 1209 GElf_Dyn dyn; 1210 Elf_Data *data; 1211 uint_t i, ndyn; 1212 GElf_Xword cksum; 1213 uintptr_t addr; 1214 1215 if (fptr->file_map == NULL) 1216 return (0); 1217 1218 if ((Pcontent(P) & (CC_CONTENT_TEXT | CC_CONTENT_DATA)) != 1219 (CC_CONTENT_TEXT | CC_CONTENT_DATA)) 1220 return (0); 1221 1222 /* 1223 * First, we find the checksum value in the elf file. 1224 */ 1225 scn = NULL; 1226 while ((scn = elf_nextscn(elf, scn)) != NULL) { 1227 if (gelf_getshdr(scn, &shdr) != NULL && 1228 shdr.sh_type == SHT_DYNAMIC) 1229 goto found_shdr; 1230 } 1231 return (0); 1232 1233 found_shdr: 1234 if ((data = elf_getdata(scn, NULL)) == NULL) 1235 return (0); 1236 1237 if (P->status.pr_dmodel == PR_MODEL_ILP32) 1238 ndyn = shdr.sh_size / sizeof (Elf32_Dyn); 1239 #ifdef _LP64 1240 else if (P->status.pr_dmodel == PR_MODEL_LP64) 1241 ndyn = shdr.sh_size / sizeof (Elf64_Dyn); 1242 #endif 1243 else 1244 return (0); 1245 1246 for (i = 0; i < ndyn; i++) { 1247 if (gelf_getdyn(data, i, &dyn) != NULL && 1248 dyn.d_tag == DT_CHECKSUM) 1249 goto found_cksum; 1250 } 1251 1252 /* 1253 * The in-memory ELF has no DT_CHECKSUM section, but we will report it 1254 * as matching the file anyhow. 1255 */ 1256 return (0); 1257 1258 found_cksum: 1259 cksum = dyn.d_un.d_val; 1260 dprintf("elf cksum value is %llx\n", (u_longlong_t)cksum); 1261 1262 /* 1263 * Get the base of the text mapping that corresponds to this file. 1264 */ 1265 addr = fptr->file_map->map_pmap.pr_vaddr; 1266 1267 if (P->status.pr_dmodel == PR_MODEL_ILP32) { 1268 Elf32_Ehdr ehdr; 1269 Elf32_Phdr phdr; 1270 Elf32_Dyn dync, *dynp; 1271 uint_t phnum, i; 1272 1273 if (read_ehdr32(P, &ehdr, &phnum, addr) != 0 || 1274 read_dynamic_phdr32(P, &ehdr, phnum, &phdr, addr) != 0) 1275 return (0); 1276 1277 if (ehdr.e_type == ET_DYN) 1278 phdr.p_vaddr += addr; 1279 if ((dynp = malloc(phdr.p_filesz)) == NULL) 1280 return (0); 1281 dync.d_tag = DT_NULL; 1282 if (Pread(P, dynp, phdr.p_filesz, phdr.p_vaddr) != 1283 phdr.p_filesz) { 1284 free(dynp); 1285 return (0); 1286 } 1287 1288 for (i = 0; i < phdr.p_filesz / sizeof (Elf32_Dyn); i++) { 1289 if (dynp[i].d_tag == DT_CHECKSUM) 1290 dync = dynp[i]; 1291 } 1292 1293 free(dynp); 1294 1295 if (dync.d_tag != DT_CHECKSUM) 1296 return (0); 1297 1298 dprintf("image cksum value is %llx\n", 1299 (u_longlong_t)dync.d_un.d_val); 1300 return (dync.d_un.d_val != cksum); 1301 #ifdef _LP64 1302 } else if (P->status.pr_dmodel == PR_MODEL_LP64) { 1303 Elf64_Ehdr ehdr; 1304 Elf64_Phdr phdr; 1305 Elf64_Dyn dync, *dynp; 1306 uint_t phnum, i; 1307 1308 if (read_ehdr64(P, &ehdr, &phnum, addr) != 0 || 1309 read_dynamic_phdr64(P, &ehdr, phnum, &phdr, addr) != 0) 1310 return (0); 1311 1312 if (ehdr.e_type == ET_DYN) 1313 phdr.p_vaddr += addr; 1314 if ((dynp = malloc(phdr.p_filesz)) == NULL) 1315 return (0); 1316 dync.d_tag = DT_NULL; 1317 if (Pread(P, dynp, phdr.p_filesz, phdr.p_vaddr) != 1318 phdr.p_filesz) { 1319 free(dynp); 1320 return (0); 1321 } 1322 1323 for (i = 0; i < phdr.p_filesz / sizeof (Elf64_Dyn); i++) { 1324 if (dynp[i].d_tag == DT_CHECKSUM) 1325 dync = dynp[i]; 1326 } 1327 1328 free(dynp); 1329 1330 if (dync.d_tag != DT_CHECKSUM) 1331 return (0); 1332 1333 dprintf("image cksum value is %llx\n", 1334 (u_longlong_t)dync.d_un.d_val); 1335 return (dync.d_un.d_val != cksum); 1336 #endif /* _LP64 */ 1337 } 1338 1339 return (0); 1340 } 1341 1342 /* 1343 * Read data from the specified process and construct an in memory 1344 * image of an ELF file that represents it well enough to let 1345 * us probe it for information. 1346 */ 1347 static Elf * 1348 fake_elf(struct ps_prochandle *P, file_info_t *fptr) 1349 { 1350 Elf *elf; 1351 uintptr_t addr; 1352 uint_t phnum; 1353 1354 if (fptr->file_map == NULL) 1355 return (NULL); 1356 1357 if ((Pcontent(P) & (CC_CONTENT_TEXT | CC_CONTENT_DATA)) != 1358 (CC_CONTENT_TEXT | CC_CONTENT_DATA)) 1359 return (NULL); 1360 1361 addr = fptr->file_map->map_pmap.pr_vaddr; 1362 1363 if (P->status.pr_dmodel == PR_MODEL_ILP32) { 1364 Elf32_Ehdr ehdr; 1365 Elf32_Phdr phdr; 1366 1367 if ((read_ehdr32(P, &ehdr, &phnum, addr) != 0) || 1368 read_dynamic_phdr32(P, &ehdr, phnum, &phdr, addr) != 0) 1369 return (NULL); 1370 1371 elf = fake_elf32(P, fptr, addr, &ehdr, phnum, &phdr); 1372 #ifdef _LP64 1373 } else { 1374 Elf64_Ehdr ehdr; 1375 Elf64_Phdr phdr; 1376 1377 if (read_ehdr64(P, &ehdr, &phnum, addr) != 0 || 1378 read_dynamic_phdr64(P, &ehdr, phnum, &phdr, addr) != 0) 1379 return (NULL); 1380 1381 elf = fake_elf64(P, fptr, addr, &ehdr, phnum, &phdr); 1382 #endif 1383 } 1384 1385 return (elf); 1386 } 1387 1388 /* 1389 * We wouldn't need these if qsort(3C) took an argument for the callback... 1390 */ 1391 static mutex_t sort_mtx = DEFAULTMUTEX; 1392 static char *sort_strs; 1393 static GElf_Sym *sort_syms; 1394 1395 int 1396 byaddr_cmp_common(GElf_Sym *a, char *aname, GElf_Sym *b, char *bname) 1397 { 1398 if (a->st_value < b->st_value) 1399 return (-1); 1400 if (a->st_value > b->st_value) 1401 return (1); 1402 1403 /* 1404 * Prefer the function to the non-function. 1405 */ 1406 if (GELF_ST_TYPE(a->st_info) != GELF_ST_TYPE(b->st_info)) { 1407 if (GELF_ST_TYPE(a->st_info) == STT_FUNC) 1408 return (-1); 1409 if (GELF_ST_TYPE(b->st_info) == STT_FUNC) 1410 return (1); 1411 } 1412 1413 /* 1414 * Prefer the weak or strong global symbol to the local symbol. 1415 */ 1416 if (GELF_ST_BIND(a->st_info) != GELF_ST_BIND(b->st_info)) { 1417 if (GELF_ST_BIND(b->st_info) == STB_LOCAL) 1418 return (-1); 1419 if (GELF_ST_BIND(a->st_info) == STB_LOCAL) 1420 return (1); 1421 } 1422 1423 /* 1424 * Prefer the symbol that doesn't begin with a '$' since compilers and 1425 * other symbol generators often use it as a prefix. 1426 */ 1427 if (*bname == '$') 1428 return (-1); 1429 if (*aname == '$') 1430 return (1); 1431 1432 /* 1433 * Prefer the name with fewer leading underscores in the name. 1434 */ 1435 while (*aname == '_' && *bname == '_') { 1436 aname++; 1437 bname++; 1438 } 1439 1440 if (*bname == '_') 1441 return (-1); 1442 if (*aname == '_') 1443 return (1); 1444 1445 /* 1446 * Prefer the symbol with the smaller size. 1447 */ 1448 if (a->st_size < b->st_size) 1449 return (-1); 1450 if (a->st_size > b->st_size) 1451 return (1); 1452 1453 /* 1454 * All other factors being equal, fall back to lexicographic order. 1455 */ 1456 return (strcmp(aname, bname)); 1457 } 1458 1459 static int 1460 byaddr_cmp(const void *aa, const void *bb) 1461 { 1462 GElf_Sym *a = &sort_syms[*(uint_t *)aa]; 1463 GElf_Sym *b = &sort_syms[*(uint_t *)bb]; 1464 char *aname = sort_strs + a->st_name; 1465 char *bname = sort_strs + b->st_name; 1466 1467 return (byaddr_cmp_common(a, aname, b, bname)); 1468 } 1469 1470 static int 1471 byname_cmp(const void *aa, const void *bb) 1472 { 1473 GElf_Sym *a = &sort_syms[*(uint_t *)aa]; 1474 GElf_Sym *b = &sort_syms[*(uint_t *)bb]; 1475 char *aname = sort_strs + a->st_name; 1476 char *bname = sort_strs + b->st_name; 1477 1478 return (strcmp(aname, bname)); 1479 } 1480 1481 /* 1482 * Given a symbol index, look up the corresponding symbol from the 1483 * given symbol table. 1484 * 1485 * This function allows the caller to treat the symbol table as a single 1486 * logical entity even though there may be 2 actual ELF symbol tables 1487 * involved. See the comments in Pcontrol.h for details. 1488 */ 1489 static GElf_Sym * 1490 symtab_getsym(sym_tbl_t *symtab, int ndx, GElf_Sym *dst) 1491 { 1492 /* If index is in range of primary symtab, look it up there */ 1493 if (ndx >= symtab->sym_symn_aux) { 1494 return (gelf_getsym(symtab->sym_data_pri, 1495 ndx - symtab->sym_symn_aux, dst)); 1496 } 1497 1498 /* Not in primary: Look it up in the auxiliary symtab */ 1499 return (gelf_getsym(symtab->sym_data_aux, ndx, dst)); 1500 } 1501 1502 void 1503 optimize_symtab(sym_tbl_t *symtab) 1504 { 1505 GElf_Sym *symp, *syms; 1506 uint_t i, *indexa, *indexb; 1507 size_t symn, strsz, count; 1508 1509 if (symtab == NULL || symtab->sym_data_pri == NULL || 1510 symtab->sym_byaddr != NULL) 1511 return; 1512 1513 symn = symtab->sym_symn; 1514 strsz = symtab->sym_strsz; 1515 1516 symp = syms = malloc(sizeof (GElf_Sym) * symn); 1517 if (symp == NULL) { 1518 dprintf("optimize_symtab: failed to malloc symbol array"); 1519 return; 1520 } 1521 1522 /* 1523 * First record all the symbols into a table and count up the ones 1524 * that we're interested in. We mark symbols as invalid by setting 1525 * the st_name to an illegal value. 1526 */ 1527 for (i = 0, count = 0; i < symn; i++, symp++) { 1528 if (symtab_getsym(symtab, i, symp) != NULL && 1529 symp->st_name < strsz && 1530 IS_DATA_TYPE(GELF_ST_TYPE(symp->st_info))) 1531 count++; 1532 else 1533 symp->st_name = strsz; 1534 } 1535 1536 /* 1537 * Allocate sufficient space for both tables and populate them 1538 * with the same symbols we just counted. 1539 */ 1540 symtab->sym_count = count; 1541 indexa = symtab->sym_byaddr = calloc(sizeof (uint_t), count); 1542 indexb = symtab->sym_byname = calloc(sizeof (uint_t), count); 1543 if (indexa == NULL || indexb == NULL) { 1544 dprintf( 1545 "optimize_symtab: failed to malloc symbol index arrays"); 1546 symtab->sym_count = 0; 1547 if (indexa != NULL) { /* First alloc succeeded. Free it */ 1548 free(indexa); 1549 symtab->sym_byaddr = NULL; 1550 } 1551 free(syms); 1552 return; 1553 } 1554 for (i = 0, symp = syms; i < symn; i++, symp++) { 1555 if (symp->st_name < strsz) 1556 *indexa++ = *indexb++ = i; 1557 } 1558 1559 /* 1560 * Sort the two tables according to the appropriate criteria, 1561 * unless the user has overridden this behaviour. 1562 * 1563 * An example where we might not sort the tables is the relatively 1564 * unusual case of a process with very large symbol tables in which 1565 * we perform few lookups. In such a case the total time would be 1566 * dominated by the sort. It is difficult to determine a priori 1567 * how many lookups an arbitrary client will perform, and 1568 * hence whether the symbol tables should be sorted. We therefore 1569 * sort the tables by default, but provide the user with a 1570 * "chicken switch" in the form of the LIBPROC_NO_QSORT 1571 * environment variable. 1572 */ 1573 if (!_libproc_no_qsort) { 1574 (void) mutex_lock(&sort_mtx); 1575 sort_strs = symtab->sym_strs; 1576 sort_syms = syms; 1577 1578 qsort(symtab->sym_byaddr, count, sizeof (uint_t), byaddr_cmp); 1579 qsort(symtab->sym_byname, count, sizeof (uint_t), byname_cmp); 1580 1581 sort_strs = NULL; 1582 sort_syms = NULL; 1583 (void) mutex_unlock(&sort_mtx); 1584 } 1585 1586 free(syms); 1587 } 1588 1589 /* 1590 * Build the symbol table for the given mapped file. 1591 */ 1592 void 1593 Pbuild_file_symtab(struct ps_prochandle *P, file_info_t *fptr) 1594 { 1595 char objectfile[PATH_MAX]; 1596 uint_t i; 1597 1598 GElf_Ehdr ehdr; 1599 GElf_Sym s; 1600 1601 Elf_Data *shdata; 1602 Elf_Scn *scn; 1603 Elf *elf; 1604 size_t nshdrs, shstrndx; 1605 1606 struct { 1607 GElf_Shdr c_shdr; 1608 Elf_Data *c_data; 1609 const char *c_name; 1610 } *cp, *cache = NULL, *dyn = NULL, *plt = NULL, *ctf = NULL; 1611 1612 if (fptr->file_init) 1613 return; /* We've already processed this file */ 1614 1615 /* 1616 * Mark the file_info struct as having the symbol table initialized 1617 * even if we fail below. We tried once; we don't try again. 1618 */ 1619 fptr->file_init = 1; 1620 1621 if (elf_version(EV_CURRENT) == EV_NONE) { 1622 dprintf("libproc ELF version is more recent than libelf\n"); 1623 return; 1624 } 1625 1626 if (P->state == PS_DEAD || P->state == PS_IDLE) { 1627 char *name; 1628 /* 1629 * If we're a not live, we can't open files from the /proc 1630 * object directory; we have only the mapping and file names 1631 * to guide us. We prefer the file_lname, but need to handle 1632 * the case of it being NULL in order to bootstrap: we first 1633 * come here during rd_new() when the only information we have 1634 * is interpreter name associated with the AT_BASE mapping. 1635 * 1636 * Also, if the zone associated with the core file seems 1637 * to exists on this machine we'll try to open the object 1638 * file within the zone. 1639 */ 1640 if (fptr->file_rname != NULL) 1641 name = fptr->file_rname; 1642 else if (fptr->file_lname != NULL) 1643 name = fptr->file_lname; 1644 else 1645 name = fptr->file_pname; 1646 (void) strlcpy(objectfile, name, sizeof (objectfile)); 1647 } else { 1648 (void) snprintf(objectfile, sizeof (objectfile), 1649 "%s/%d/object/%s", 1650 procfs_path, (int)P->pid, fptr->file_pname); 1651 } 1652 1653 /* 1654 * Open the object file, create the elf file, and then get the elf 1655 * header and .shstrtab data buffer so we can process sections by 1656 * name. If anything goes wrong try to fake up an elf file from 1657 * the in-core elf image. 1658 */ 1659 if ((fptr->file_fd = open(objectfile, O_RDONLY)) < 0) { 1660 dprintf("Pbuild_file_symtab: failed to open %s: %s\n", 1661 objectfile, strerror(errno)); 1662 1663 if ((elf = fake_elf(P, fptr)) == NULL || 1664 elf_kind(elf) != ELF_K_ELF || 1665 gelf_getehdr(elf, &ehdr) == NULL || 1666 elf_getshnum(elf, &nshdrs) == 0 || 1667 elf_getshstrndx(elf, &shstrndx) == 0 || 1668 (scn = elf_getscn(elf, shstrndx)) == NULL || 1669 (shdata = elf_getdata(scn, NULL)) == NULL) { 1670 dprintf("failed to fake up ELF file\n"); 1671 return; 1672 } 1673 1674 } else if ((elf = elf_begin(fptr->file_fd, ELF_C_READ, NULL)) == NULL || 1675 elf_kind(elf) != ELF_K_ELF || 1676 gelf_getehdr(elf, &ehdr) == NULL || 1677 elf_getshnum(elf, &nshdrs) == 0 || 1678 elf_getshstrndx(elf, &shstrndx) == 0 || 1679 (scn = elf_getscn(elf, shstrndx)) == NULL || 1680 (shdata = elf_getdata(scn, NULL)) == NULL) { 1681 int err = elf_errno(); 1682 1683 dprintf("failed to process ELF file %s: %s\n", 1684 objectfile, (err == 0) ? "<null>" : elf_errmsg(err)); 1685 1686 if ((elf = fake_elf(P, fptr)) == NULL || 1687 elf_kind(elf) != ELF_K_ELF || 1688 gelf_getehdr(elf, &ehdr) == NULL || 1689 elf_getshnum(elf, &nshdrs) == 0 || 1690 elf_getshstrndx(elf, &shstrndx) == 0 || 1691 (scn = elf_getscn(elf, shstrndx)) == NULL || 1692 (shdata = elf_getdata(scn, NULL)) == NULL) { 1693 dprintf("failed to fake up ELF file\n"); 1694 goto bad; 1695 } 1696 1697 } else if (file_differs(P, elf, fptr)) { 1698 Elf *newelf; 1699 1700 /* 1701 * Before we get too excited about this elf file, we'll check 1702 * its checksum value against the value we have in memory. If 1703 * they don't agree, we try to fake up a new elf file and 1704 * proceed with that instead. 1705 */ 1706 1707 dprintf("ELF file %s (%lx) doesn't match in-core image\n", 1708 fptr->file_pname, 1709 (ulong_t)fptr->file_map->map_pmap.pr_vaddr); 1710 1711 if ((newelf = fake_elf(P, fptr)) == NULL || 1712 elf_kind(newelf) != ELF_K_ELF || 1713 gelf_getehdr(newelf, &ehdr) == NULL || 1714 elf_getshnum(newelf, &nshdrs) == 0 || 1715 elf_getshstrndx(newelf, &shstrndx) == 0 || 1716 (scn = elf_getscn(newelf, shstrndx)) == NULL || 1717 (shdata = elf_getdata(scn, NULL)) == NULL) { 1718 dprintf("failed to fake up ELF file\n"); 1719 } else { 1720 (void) elf_end(elf); 1721 elf = newelf; 1722 1723 dprintf("switched to faked up ELF file\n"); 1724 } 1725 } 1726 1727 if ((cache = malloc(nshdrs * sizeof (*cache))) == NULL) { 1728 dprintf("failed to malloc section cache for %s\n", objectfile); 1729 goto bad; 1730 } 1731 1732 dprintf("processing ELF file %s\n", objectfile); 1733 fptr->file_class = ehdr.e_ident[EI_CLASS]; 1734 fptr->file_etype = ehdr.e_type; 1735 fptr->file_elf = elf; 1736 fptr->file_shstrs = shdata->d_buf; 1737 fptr->file_shstrsz = shdata->d_size; 1738 1739 /* 1740 * Iterate through each section, caching its section header, data 1741 * pointer, and name. We use this for handling sh_link values below. 1742 */ 1743 for (cp = cache + 1, scn = NULL; scn = elf_nextscn(elf, scn); cp++) { 1744 if (gelf_getshdr(scn, &cp->c_shdr) == NULL) { 1745 dprintf("Pbuild_file_symtab: Failed to get section " 1746 "header\n"); 1747 goto bad; /* Failed to get section header */ 1748 } 1749 1750 if ((cp->c_data = elf_getdata(scn, NULL)) == NULL) { 1751 dprintf("Pbuild_file_symtab: Failed to get section " 1752 "data\n"); 1753 goto bad; /* Failed to get section data */ 1754 } 1755 1756 if (cp->c_shdr.sh_name >= shdata->d_size) { 1757 dprintf("Pbuild_file_symtab: corrupt section name"); 1758 goto bad; /* Corrupt section name */ 1759 } 1760 1761 cp->c_name = (const char *)shdata->d_buf + cp->c_shdr.sh_name; 1762 } 1763 1764 /* 1765 * Now iterate through the section cache in order to locate info 1766 * for the .symtab, .dynsym, .SUNW_ldynsym, .dynamic, .plt, 1767 * and .SUNW_ctf sections: 1768 */ 1769 for (i = 1, cp = cache + 1; i < nshdrs; i++, cp++) { 1770 GElf_Shdr *shp = &cp->c_shdr; 1771 1772 if (shp->sh_type == SHT_SYMTAB || shp->sh_type == SHT_DYNSYM) { 1773 sym_tbl_t *symp = shp->sh_type == SHT_SYMTAB ? 1774 &fptr->file_symtab : &fptr->file_dynsym; 1775 /* 1776 * It's possible that the we already got the symbol 1777 * table from the core file itself. Either the file 1778 * differs in which case our faked up elf file will 1779 * only contain the dynsym (not the symtab) or the 1780 * file matches in which case we'll just be replacing 1781 * the symbol table we pulled out of the core file 1782 * with an equivalent one. In either case, this 1783 * check isn't essential, but it's a good idea. 1784 */ 1785 if (symp->sym_data_pri == NULL) { 1786 dprintf("Symbol table found for %s\n", 1787 objectfile); 1788 symp->sym_data_pri = cp->c_data; 1789 symp->sym_symn += 1790 shp->sh_size / shp->sh_entsize; 1791 symp->sym_strs = 1792 cache[shp->sh_link].c_data->d_buf; 1793 symp->sym_strsz = 1794 cache[shp->sh_link].c_data->d_size; 1795 symp->sym_hdr_pri = cp->c_shdr; 1796 symp->sym_strhdr = cache[shp->sh_link].c_shdr; 1797 } else { 1798 dprintf("Symbol table already there for %s\n", 1799 objectfile); 1800 } 1801 } else if (shp->sh_type == SHT_SUNW_LDYNSYM) { 1802 /* .SUNW_ldynsym section is auxiliary to .dynsym */ 1803 if (fptr->file_dynsym.sym_data_aux == NULL) { 1804 dprintf(".SUNW_ldynsym symbol table" 1805 " found for %s\n", objectfile); 1806 fptr->file_dynsym.sym_data_aux = cp->c_data; 1807 fptr->file_dynsym.sym_symn_aux = 1808 shp->sh_size / shp->sh_entsize; 1809 fptr->file_dynsym.sym_symn += 1810 fptr->file_dynsym.sym_symn_aux; 1811 fptr->file_dynsym.sym_hdr_aux = cp->c_shdr; 1812 } else { 1813 dprintf(".SUNW_ldynsym symbol table already" 1814 " there for %s\n", objectfile); 1815 } 1816 } else if (shp->sh_type == SHT_DYNAMIC) { 1817 dyn = cp; 1818 } else if (strcmp(cp->c_name, ".plt") == 0) { 1819 plt = cp; 1820 } else if (strcmp(cp->c_name, ".SUNW_ctf") == 0) { 1821 /* 1822 * Skip over bogus CTF sections so they don't come back 1823 * to haunt us later. 1824 */ 1825 if (shp->sh_link == 0 || 1826 shp->sh_link >= nshdrs || 1827 (cache[shp->sh_link].c_shdr.sh_type != SHT_DYNSYM && 1828 cache[shp->sh_link].c_shdr.sh_type != SHT_SYMTAB)) { 1829 dprintf("Bad sh_link %d for " 1830 "CTF\n", shp->sh_link); 1831 continue; 1832 } 1833 ctf = cp; 1834 } 1835 } 1836 1837 /* 1838 * At this point, we've found all the symbol tables we're ever going 1839 * to find: the ones in the loop above and possibly the symtab that 1840 * was included in the core file. Before we perform any lookups, we 1841 * create sorted versions to optimize for lookups. 1842 */ 1843 optimize_symtab(&fptr->file_symtab); 1844 optimize_symtab(&fptr->file_dynsym); 1845 1846 /* 1847 * Fill in the base address of the text mapping for shared libraries. 1848 * This allows us to translate symbols before librtld_db is ready. 1849 */ 1850 if (fptr->file_etype == ET_DYN) { 1851 fptr->file_dyn_base = fptr->file_map->map_pmap.pr_vaddr - 1852 fptr->file_map->map_pmap.pr_offset; 1853 dprintf("setting file_dyn_base for %s to %lx\n", 1854 objectfile, (long)fptr->file_dyn_base); 1855 } 1856 1857 /* 1858 * Record the CTF section information in the file info structure. 1859 */ 1860 if (ctf != NULL) { 1861 fptr->file_ctf_off = ctf->c_shdr.sh_offset; 1862 fptr->file_ctf_size = ctf->c_shdr.sh_size; 1863 if (ctf->c_shdr.sh_link != 0 && 1864 cache[ctf->c_shdr.sh_link].c_shdr.sh_type == SHT_DYNSYM) 1865 fptr->file_ctf_dyn = 1; 1866 } 1867 1868 if (fptr->file_lo == NULL) 1869 goto done; /* Nothing else to do if no load object info */ 1870 1871 /* 1872 * If the object is a shared library and we have a different rl_base 1873 * value, reset file_dyn_base according to librtld_db's information. 1874 */ 1875 if (fptr->file_etype == ET_DYN && 1876 fptr->file_lo->rl_base != fptr->file_dyn_base) { 1877 dprintf("resetting file_dyn_base for %s to %lx\n", 1878 objectfile, (long)fptr->file_lo->rl_base); 1879 fptr->file_dyn_base = fptr->file_lo->rl_base; 1880 } 1881 1882 /* 1883 * Fill in the PLT information for this file if a PLT symbol is found. 1884 */ 1885 if (sym_by_name(&fptr->file_dynsym, "_PROCEDURE_LINKAGE_TABLE_", &s, 1886 NULL) != NULL) { 1887 fptr->file_plt_base = s.st_value + fptr->file_dyn_base; 1888 fptr->file_plt_size = (plt != NULL) ? plt->c_shdr.sh_size : 0; 1889 1890 /* 1891 * Bring the load object up to date; it is the only way the 1892 * user has to access the PLT data. The PLT information in the 1893 * rd_loadobj_t is not set in the call to map_iter() (the 1894 * callback for rd_loadobj_iter) where we set file_lo. 1895 */ 1896 fptr->file_lo->rl_plt_base = fptr->file_plt_base; 1897 fptr->file_lo->rl_plt_size = fptr->file_plt_size; 1898 1899 dprintf("PLT found at %p, size = %lu\n", 1900 (void *)fptr->file_plt_base, (ulong_t)fptr->file_plt_size); 1901 } 1902 1903 /* 1904 * Fill in the PLT information. 1905 */ 1906 if (dyn != NULL) { 1907 uintptr_t dynaddr = dyn->c_shdr.sh_addr + fptr->file_dyn_base; 1908 size_t ndyn = dyn->c_shdr.sh_size / dyn->c_shdr.sh_entsize; 1909 GElf_Dyn d; 1910 1911 for (i = 0; i < ndyn; i++) { 1912 if (gelf_getdyn(dyn->c_data, i, &d) != NULL && 1913 d.d_tag == DT_JMPREL) { 1914 dprintf("DT_JMPREL is %p\n", 1915 (void *)(uintptr_t)d.d_un.d_ptr); 1916 fptr->file_jmp_rel = 1917 d.d_un.d_ptr + fptr->file_dyn_base; 1918 break; 1919 } 1920 } 1921 1922 dprintf("_DYNAMIC found at %p, %lu entries, DT_JMPREL = %p\n", 1923 (void *)dynaddr, (ulong_t)ndyn, (void *)fptr->file_jmp_rel); 1924 } 1925 1926 done: 1927 free(cache); 1928 return; 1929 1930 bad: 1931 if (cache != NULL) 1932 free(cache); 1933 1934 (void) elf_end(elf); 1935 fptr->file_elf = NULL; 1936 if (fptr->file_elfmem != NULL) { 1937 free(fptr->file_elfmem); 1938 fptr->file_elfmem = NULL; 1939 } 1940 (void) close(fptr->file_fd); 1941 fptr->file_fd = -1; 1942 } 1943 1944 /* 1945 * Given a process virtual address, return the map_info_t containing it. 1946 * If none found, return NULL. 1947 */ 1948 map_info_t * 1949 Paddr2mptr(struct ps_prochandle *P, uintptr_t addr) 1950 { 1951 int lo = 0; 1952 int hi = P->map_count - 1; 1953 int mid; 1954 map_info_t *mp; 1955 1956 while (lo <= hi) { 1957 1958 mid = (lo + hi) / 2; 1959 mp = &P->mappings[mid]; 1960 1961 /* check that addr is in [vaddr, vaddr + size) */ 1962 if ((addr - mp->map_pmap.pr_vaddr) < mp->map_pmap.pr_size) 1963 return (mp); 1964 1965 if (addr < mp->map_pmap.pr_vaddr) 1966 hi = mid - 1; 1967 else 1968 lo = mid + 1; 1969 } 1970 1971 return (NULL); 1972 } 1973 1974 /* 1975 * Return the map_info_t for the executable file. 1976 * If not found, return NULL. 1977 */ 1978 static map_info_t * 1979 exec_map(struct ps_prochandle *P) 1980 { 1981 uint_t i; 1982 map_info_t *mptr; 1983 map_info_t *mold = NULL; 1984 file_info_t *fptr; 1985 uintptr_t base; 1986 1987 for (i = 0, mptr = P->mappings; i < P->map_count; i++, mptr++) { 1988 if (mptr->map_pmap.pr_mapname[0] == '\0') 1989 continue; 1990 if (strcmp(mptr->map_pmap.pr_mapname, "a.out") == 0) { 1991 if ((fptr = mptr->map_file) != NULL && 1992 fptr->file_lo != NULL) { 1993 base = fptr->file_lo->rl_base; 1994 if (base >= mptr->map_pmap.pr_vaddr && 1995 base < mptr->map_pmap.pr_vaddr + 1996 mptr->map_pmap.pr_size) /* text space */ 1997 return (mptr); 1998 mold = mptr; /* must be the data */ 1999 continue; 2000 } 2001 /* This is a poor way to test for text space */ 2002 if (!(mptr->map_pmap.pr_mflags & MA_EXEC) || 2003 (mptr->map_pmap.pr_mflags & MA_WRITE)) { 2004 mold = mptr; 2005 continue; 2006 } 2007 return (mptr); 2008 } 2009 } 2010 2011 return (mold); 2012 } 2013 2014 /* 2015 * Given a shared object name, return the map_info_t for it. If no matching 2016 * object is found, return NULL. Normally, the link maps contain the full 2017 * object pathname, e.g. /usr/lib/libc.so.1. We allow the object name to 2018 * take one of the following forms: 2019 * 2020 * 1. An exact match (i.e. a full pathname): "/usr/lib/libc.so.1" 2021 * 2. An exact basename match: "libc.so.1" 2022 * 3. An initial basename match up to a '.' suffix: "libc.so" or "libc" 2023 * 4. The literal string "a.out" is an alias for the executable mapping 2024 * 2025 * The third case is a convenience for callers and may not be necessary. 2026 * 2027 * As the exact same object name may be loaded on different link maps (see 2028 * dlmopen(3DL)), we also allow the caller to resolve the object name by 2029 * specifying a particular link map id. If lmid is PR_LMID_EVERY, the 2030 * first matching name will be returned, regardless of the link map id. 2031 */ 2032 static map_info_t * 2033 object_to_map(struct ps_prochandle *P, Lmid_t lmid, const char *objname) 2034 { 2035 map_info_t *mp; 2036 file_info_t *fp; 2037 size_t objlen; 2038 uint_t i; 2039 2040 /* 2041 * If we have no rtld_db, then always treat a request as one for all 2042 * link maps. 2043 */ 2044 if (P->rap == NULL) 2045 lmid = PR_LMID_EVERY; 2046 2047 /* 2048 * First pass: look for exact matches of the entire pathname or 2049 * basename (cases 1 and 2 above): 2050 */ 2051 for (i = 0, mp = P->mappings; i < P->map_count; i++, mp++) { 2052 2053 if (mp->map_pmap.pr_mapname[0] == '\0' || 2054 (fp = mp->map_file) == NULL || 2055 ((fp->file_lname == NULL) && (fp->file_rname == NULL))) 2056 continue; 2057 2058 if (lmid != PR_LMID_EVERY && 2059 (fp->file_lo == NULL || lmid != fp->file_lo->rl_lmident)) 2060 continue; 2061 2062 /* 2063 * If we match, return the primary text mapping; otherwise 2064 * just return the mapping we matched. 2065 */ 2066 if ((fp->file_lbase && strcmp(fp->file_lbase, objname) == 0) || 2067 (fp->file_rbase && strcmp(fp->file_rbase, objname) == 0) || 2068 (fp->file_lname && strcmp(fp->file_lname, objname) == 0) || 2069 (fp->file_rname && strcmp(fp->file_rname, objname) == 0)) 2070 return (fp->file_map ? fp->file_map : mp); 2071 } 2072 2073 objlen = strlen(objname); 2074 2075 /* 2076 * Second pass: look for partial matches (case 3 above): 2077 */ 2078 for (i = 0, mp = P->mappings; i < P->map_count; i++, mp++) { 2079 2080 if (mp->map_pmap.pr_mapname[0] == '\0' || 2081 (fp = mp->map_file) == NULL || 2082 ((fp->file_lname == NULL) && (fp->file_rname == NULL))) 2083 continue; 2084 2085 if (lmid != PR_LMID_EVERY && 2086 (fp->file_lo == NULL || lmid != fp->file_lo->rl_lmident)) 2087 continue; 2088 2089 /* 2090 * If we match, return the primary text mapping; otherwise 2091 * just return the mapping we matched. 2092 */ 2093 if ((fp->file_lbase != NULL) && 2094 (strncmp(fp->file_lbase, objname, objlen) == 0) && 2095 (fp->file_lbase[objlen] == '.')) 2096 return (fp->file_map ? fp->file_map : mp); 2097 if ((fp->file_rbase != NULL) && 2098 (strncmp(fp->file_rbase, objname, objlen) == 0) && 2099 (fp->file_rbase[objlen] == '.')) 2100 return (fp->file_map ? fp->file_map : mp); 2101 } 2102 2103 /* 2104 * One last check: we allow "a.out" to always alias the executable, 2105 * assuming this name was not in use for something else. 2106 */ 2107 if ((lmid == PR_LMID_EVERY || lmid == LM_ID_BASE) && 2108 (strcmp(objname, "a.out") == 0)) 2109 return (P->map_exec); 2110 2111 return (NULL); 2112 } 2113 2114 static map_info_t * 2115 object_name_to_map(struct ps_prochandle *P, Lmid_t lmid, const char *name) 2116 { 2117 map_info_t *mptr; 2118 2119 if (!P->info_valid) 2120 Pupdate_maps(P); 2121 2122 if (P->map_exec == NULL && ((mptr = Paddr2mptr(P, 2123 Pgetauxval(P, AT_ENTRY))) != NULL || (mptr = exec_map(P)) != NULL)) 2124 P->map_exec = mptr; 2125 2126 if (P->map_ldso == NULL && (mptr = Paddr2mptr(P, 2127 Pgetauxval(P, AT_BASE))) != NULL) 2128 P->map_ldso = mptr; 2129 2130 if (name == PR_OBJ_EXEC) 2131 mptr = P->map_exec; 2132 else if (name == PR_OBJ_LDSO) 2133 mptr = P->map_ldso; 2134 else if (Prd_agent(P) != NULL || P->state == PS_IDLE) 2135 mptr = object_to_map(P, lmid, name); 2136 else 2137 mptr = NULL; 2138 2139 return (mptr); 2140 } 2141 2142 /* 2143 * When two symbols are found by address, decide which one is to be preferred. 2144 */ 2145 static GElf_Sym * 2146 sym_prefer(GElf_Sym *sym1, char *name1, GElf_Sym *sym2, char *name2) 2147 { 2148 /* 2149 * Prefer the non-NULL symbol. 2150 */ 2151 if (sym1 == NULL) 2152 return (sym2); 2153 if (sym2 == NULL) 2154 return (sym1); 2155 2156 /* 2157 * Defer to the sort ordering... 2158 */ 2159 return (byaddr_cmp_common(sym1, name1, sym2, name2) <= 0 ? sym1 : sym2); 2160 } 2161 2162 /* 2163 * Use a binary search to do the work of sym_by_addr(). 2164 */ 2165 static GElf_Sym * 2166 sym_by_addr_binary(sym_tbl_t *symtab, GElf_Addr addr, GElf_Sym *symp, 2167 uint_t *idp) 2168 { 2169 GElf_Sym sym, osym; 2170 uint_t i, oid, *byaddr = symtab->sym_byaddr; 2171 int min, max, mid, omid, found = 0; 2172 2173 if (symtab->sym_data_pri == NULL || symtab->sym_count == 0) 2174 return (NULL); 2175 2176 min = 0; 2177 max = symtab->sym_count - 1; 2178 osym.st_value = 0; 2179 2180 /* 2181 * We can't return when we've found a match, we have to continue 2182 * searching for the closest matching symbol. 2183 */ 2184 while (min <= max) { 2185 mid = (max + min) / 2; 2186 2187 i = byaddr[mid]; 2188 (void) symtab_getsym(symtab, i, &sym); 2189 2190 if (addr >= sym.st_value && 2191 addr < sym.st_value + sym.st_size && 2192 (!found || sym.st_value > osym.st_value)) { 2193 osym = sym; 2194 omid = mid; 2195 oid = i; 2196 found = 1; 2197 } 2198 2199 if (addr < sym.st_value) 2200 max = mid - 1; 2201 else 2202 min = mid + 1; 2203 } 2204 2205 if (!found) 2206 return (NULL); 2207 2208 /* 2209 * There may be many symbols with identical values so we walk 2210 * backward in the byaddr table to find the best match. 2211 */ 2212 do { 2213 sym = osym; 2214 i = oid; 2215 2216 if (omid == 0) 2217 break; 2218 2219 oid = byaddr[--omid]; 2220 (void) symtab_getsym(symtab, oid, &osym); 2221 } while (addr >= osym.st_value && 2222 addr < sym.st_value + osym.st_size && 2223 osym.st_value == sym.st_value); 2224 2225 *symp = sym; 2226 if (idp != NULL) 2227 *idp = i; 2228 return (symp); 2229 } 2230 2231 /* 2232 * Use a linear search to do the work of sym_by_addr(). 2233 */ 2234 static GElf_Sym * 2235 sym_by_addr_linear(sym_tbl_t *symtab, GElf_Addr addr, GElf_Sym *symbolp, 2236 uint_t *idp) 2237 { 2238 size_t symn = symtab->sym_symn; 2239 char *strs = symtab->sym_strs; 2240 GElf_Sym sym, *symp = NULL; 2241 GElf_Sym osym, *osymp = NULL; 2242 int i, id; 2243 2244 if (symtab->sym_data_pri == NULL || symn == 0 || strs == NULL) 2245 return (NULL); 2246 2247 for (i = 0; i < symn; i++) { 2248 if ((symp = symtab_getsym(symtab, i, &sym)) != NULL) { 2249 if (addr >= sym.st_value && 2250 addr < sym.st_value + sym.st_size) { 2251 if (osymp) 2252 symp = sym_prefer( 2253 symp, strs + symp->st_name, 2254 osymp, strs + osymp->st_name); 2255 if (symp != osymp) { 2256 osym = sym; 2257 osymp = &osym; 2258 id = i; 2259 } 2260 } 2261 } 2262 } 2263 if (osymp) { 2264 *symbolp = osym; 2265 if (idp) 2266 *idp = id; 2267 return (symbolp); 2268 } 2269 return (NULL); 2270 } 2271 2272 /* 2273 * Look up a symbol by address in the specified symbol table. 2274 * Adjustment to 'addr' must already have been made for the 2275 * offset of the symbol if this is a dynamic library symbol table. 2276 * 2277 * Use a linear or a binary search depending on whether or not we 2278 * chose to sort the table in optimize_symtab(). 2279 */ 2280 static GElf_Sym * 2281 sym_by_addr(sym_tbl_t *symtab, GElf_Addr addr, GElf_Sym *symp, uint_t *idp) 2282 { 2283 if (_libproc_no_qsort) { 2284 return (sym_by_addr_linear(symtab, addr, symp, idp)); 2285 } else { 2286 return (sym_by_addr_binary(symtab, addr, symp, idp)); 2287 } 2288 } 2289 2290 /* 2291 * Use a binary search to do the work of sym_by_name(). 2292 */ 2293 static GElf_Sym * 2294 sym_by_name_binary(sym_tbl_t *symtab, const char *name, GElf_Sym *symp, 2295 uint_t *idp) 2296 { 2297 char *strs = symtab->sym_strs; 2298 uint_t i, *byname = symtab->sym_byname; 2299 int min, mid, max, cmp; 2300 2301 if (symtab->sym_data_pri == NULL || strs == NULL || 2302 symtab->sym_count == 0) 2303 return (NULL); 2304 2305 min = 0; 2306 max = symtab->sym_count - 1; 2307 2308 while (min <= max) { 2309 mid = (max + min) / 2; 2310 2311 i = byname[mid]; 2312 (void) symtab_getsym(symtab, i, symp); 2313 2314 if ((cmp = strcmp(name, strs + symp->st_name)) == 0) { 2315 if (idp != NULL) 2316 *idp = i; 2317 return (symp); 2318 } 2319 2320 if (cmp < 0) 2321 max = mid - 1; 2322 else 2323 min = mid + 1; 2324 } 2325 2326 return (NULL); 2327 } 2328 2329 /* 2330 * Use a linear search to do the work of sym_by_name(). 2331 */ 2332 static GElf_Sym * 2333 sym_by_name_linear(sym_tbl_t *symtab, const char *name, GElf_Sym *symp, 2334 uint_t *idp) 2335 { 2336 size_t symn = symtab->sym_symn; 2337 char *strs = symtab->sym_strs; 2338 int i; 2339 2340 if (symtab->sym_data_pri == NULL || symn == 0 || strs == NULL) 2341 return (NULL); 2342 2343 for (i = 0; i < symn; i++) { 2344 if (symtab_getsym(symtab, i, symp) && 2345 strcmp(name, strs + symp->st_name) == 0) { 2346 if (idp) 2347 *idp = i; 2348 return (symp); 2349 } 2350 } 2351 2352 return (NULL); 2353 } 2354 2355 /* 2356 * Look up a symbol by name in the specified symbol table. 2357 * 2358 * Use a linear or a binary search depending on whether or not we 2359 * chose to sort the table in optimize_symtab(). 2360 */ 2361 static GElf_Sym * 2362 sym_by_name(sym_tbl_t *symtab, const char *name, GElf_Sym *symp, uint_t *idp) 2363 { 2364 if (_libproc_no_qsort) { 2365 return (sym_by_name_linear(symtab, name, symp, idp)); 2366 } else { 2367 return (sym_by_name_binary(symtab, name, symp, idp)); 2368 } 2369 } 2370 2371 /* 2372 * Search the process symbol tables looking for a symbol whose 2373 * value to value+size contain the address specified by addr. 2374 * Return values are: 2375 * sym_name_buffer containing the symbol name 2376 * GElf_Sym symbol table entry 2377 * prsyminfo_t ancillary symbol information 2378 * Returns 0 on success, -1 on failure. 2379 */ 2380 static int 2381 i_Pxlookup_by_addr( 2382 struct ps_prochandle *P, 2383 int lmresolve, /* use resolve linker object names */ 2384 uintptr_t addr, /* process address being sought */ 2385 char *sym_name_buffer, /* buffer for the symbol name */ 2386 size_t bufsize, /* size of sym_name_buffer */ 2387 GElf_Sym *symbolp, /* returned symbol table entry */ 2388 prsyminfo_t *sip) /* returned symbol info */ 2389 { 2390 GElf_Sym *symp; 2391 char *name; 2392 GElf_Sym sym1, *sym1p = NULL; 2393 GElf_Sym sym2, *sym2p = NULL; 2394 char *name1 = NULL; 2395 char *name2 = NULL; 2396 uint_t i1; 2397 uint_t i2; 2398 map_info_t *mptr; 2399 file_info_t *fptr; 2400 2401 (void) Prd_agent(P); 2402 2403 if ((mptr = Paddr2mptr(P, addr)) == NULL || /* no such address */ 2404 (fptr = build_map_symtab(P, mptr)) == NULL || /* no mapped file */ 2405 fptr->file_elf == NULL) /* not an ELF file */ 2406 return (-1); 2407 2408 /* 2409 * Adjust the address by the load object base address in 2410 * case the address turns out to be in a shared library. 2411 */ 2412 addr -= fptr->file_dyn_base; 2413 2414 /* 2415 * Search both symbol tables, symtab first, then dynsym. 2416 */ 2417 if ((sym1p = sym_by_addr(&fptr->file_symtab, addr, &sym1, &i1)) != NULL) 2418 name1 = fptr->file_symtab.sym_strs + sym1.st_name; 2419 if ((sym2p = sym_by_addr(&fptr->file_dynsym, addr, &sym2, &i2)) != NULL) 2420 name2 = fptr->file_dynsym.sym_strs + sym2.st_name; 2421 2422 if ((symp = sym_prefer(sym1p, name1, sym2p, name2)) == NULL) 2423 return (-1); 2424 2425 name = (symp == sym1p) ? name1 : name2; 2426 if (bufsize > 0) { 2427 (void) strncpy(sym_name_buffer, name, bufsize); 2428 sym_name_buffer[bufsize - 1] = '\0'; 2429 } 2430 2431 *symbolp = *symp; 2432 if (sip != NULL) { 2433 sip->prs_name = bufsize == 0 ? NULL : sym_name_buffer; 2434 if (lmresolve && (fptr->file_rname != NULL)) 2435 sip->prs_object = fptr->file_rbase; 2436 else 2437 sip->prs_object = fptr->file_lbase; 2438 sip->prs_id = (symp == sym1p) ? i1 : i2; 2439 sip->prs_table = (symp == sym1p) ? PR_SYMTAB : PR_DYNSYM; 2440 sip->prs_lmid = (fptr->file_lo == NULL) ? LM_ID_BASE : 2441 fptr->file_lo->rl_lmident; 2442 } 2443 2444 if (GELF_ST_TYPE(symbolp->st_info) != STT_TLS) 2445 symbolp->st_value += fptr->file_dyn_base; 2446 2447 return (0); 2448 } 2449 2450 int 2451 Pxlookup_by_addr(struct ps_prochandle *P, uintptr_t addr, char *buf, 2452 size_t bufsize, GElf_Sym *symp, prsyminfo_t *sip) 2453 { 2454 return (i_Pxlookup_by_addr(P, B_FALSE, addr, buf, bufsize, symp, sip)); 2455 } 2456 2457 int 2458 Pxlookup_by_addr_resolved(struct ps_prochandle *P, uintptr_t addr, char *buf, 2459 size_t bufsize, GElf_Sym *symp, prsyminfo_t *sip) 2460 { 2461 return (i_Pxlookup_by_addr(P, B_TRUE, addr, buf, bufsize, symp, sip)); 2462 } 2463 2464 int 2465 Plookup_by_addr(struct ps_prochandle *P, uintptr_t addr, char *buf, 2466 size_t size, GElf_Sym *symp) 2467 { 2468 return (i_Pxlookup_by_addr(P, B_FALSE, addr, buf, size, symp, NULL)); 2469 } 2470 2471 /* 2472 * Search the process symbol tables looking for a symbol whose name matches the 2473 * specified name and whose object and link map optionally match the specified 2474 * parameters. On success, the function returns 0 and fills in the GElf_Sym 2475 * symbol table entry. On failure, -1 is returned. 2476 */ 2477 int 2478 Pxlookup_by_name( 2479 struct ps_prochandle *P, 2480 Lmid_t lmid, /* link map to match, or -1 for any */ 2481 const char *oname, /* load object name */ 2482 const char *sname, /* symbol name */ 2483 GElf_Sym *symp, /* returned symbol table entry */ 2484 prsyminfo_t *sip) /* returned symbol info */ 2485 { 2486 map_info_t *mptr; 2487 file_info_t *fptr; 2488 int cnt; 2489 2490 GElf_Sym sym; 2491 prsyminfo_t si; 2492 int rv = -1; 2493 uint_t id; 2494 2495 if (oname == PR_OBJ_EVERY) { 2496 /* create all the file_info_t's for all the mappings */ 2497 (void) Prd_agent(P); 2498 cnt = P->num_files; 2499 fptr = list_next(&P->file_head); 2500 } else { 2501 cnt = 1; 2502 if ((mptr = object_name_to_map(P, lmid, oname)) == NULL || 2503 (fptr = build_map_symtab(P, mptr)) == NULL) 2504 return (-1); 2505 } 2506 2507 /* 2508 * Iterate through the loaded object files and look for the symbol 2509 * name in the .symtab and .dynsym of each. If we encounter a match 2510 * with SHN_UNDEF, keep looking in hopes of finding a better match. 2511 * This means that a name such as "puts" will match the puts function 2512 * in libc instead of matching the puts PLT entry in the a.out file. 2513 */ 2514 for (; cnt > 0; cnt--, fptr = list_next(fptr)) { 2515 Pbuild_file_symtab(P, fptr); 2516 2517 if (fptr->file_elf == NULL) 2518 continue; 2519 2520 if (lmid != PR_LMID_EVERY && fptr->file_lo != NULL && 2521 lmid != fptr->file_lo->rl_lmident) 2522 continue; 2523 2524 if (fptr->file_symtab.sym_data_pri != NULL && 2525 sym_by_name(&fptr->file_symtab, sname, symp, &id)) { 2526 if (sip != NULL) { 2527 sip->prs_id = id; 2528 sip->prs_table = PR_SYMTAB; 2529 sip->prs_object = oname; 2530 sip->prs_name = sname; 2531 sip->prs_lmid = fptr->file_lo == NULL ? 2532 LM_ID_BASE : fptr->file_lo->rl_lmident; 2533 } 2534 } else if (fptr->file_dynsym.sym_data_pri != NULL && 2535 sym_by_name(&fptr->file_dynsym, sname, symp, &id)) { 2536 if (sip != NULL) { 2537 sip->prs_id = id; 2538 sip->prs_table = PR_DYNSYM; 2539 sip->prs_object = oname; 2540 sip->prs_name = sname; 2541 sip->prs_lmid = fptr->file_lo == NULL ? 2542 LM_ID_BASE : fptr->file_lo->rl_lmident; 2543 } 2544 } else { 2545 continue; 2546 } 2547 2548 if (GELF_ST_TYPE(symp->st_info) != STT_TLS) 2549 symp->st_value += fptr->file_dyn_base; 2550 2551 if (symp->st_shndx != SHN_UNDEF) 2552 return (0); 2553 2554 if (rv != 0) { 2555 if (sip != NULL) 2556 si = *sip; 2557 sym = *symp; 2558 rv = 0; 2559 } 2560 } 2561 2562 if (rv == 0) { 2563 if (sip != NULL) 2564 *sip = si; 2565 *symp = sym; 2566 } 2567 2568 return (rv); 2569 } 2570 2571 /* 2572 * Search the process symbol tables looking for a symbol whose name matches the 2573 * specified name, but without any restriction on the link map id. 2574 */ 2575 int 2576 Plookup_by_name(struct ps_prochandle *P, const char *object, 2577 const char *symbol, GElf_Sym *symp) 2578 { 2579 return (Pxlookup_by_name(P, PR_LMID_EVERY, object, symbol, symp, NULL)); 2580 } 2581 2582 /* 2583 * Iterate over the process's address space mappings. 2584 */ 2585 static int 2586 i_Pmapping_iter(struct ps_prochandle *P, boolean_t lmresolve, 2587 proc_map_f *func, void *cd) 2588 { 2589 map_info_t *mptr; 2590 file_info_t *fptr; 2591 char *object_name; 2592 int rc = 0; 2593 int i; 2594 2595 /* create all the file_info_t's for all the mappings */ 2596 (void) Prd_agent(P); 2597 2598 for (i = 0, mptr = P->mappings; i < P->map_count; i++, mptr++) { 2599 if ((fptr = mptr->map_file) == NULL) 2600 object_name = NULL; 2601 else if (lmresolve && (fptr->file_rname != NULL)) 2602 object_name = fptr->file_rname; 2603 else 2604 object_name = fptr->file_lname; 2605 if ((rc = func(cd, &mptr->map_pmap, object_name)) != 0) 2606 return (rc); 2607 } 2608 return (0); 2609 } 2610 2611 int 2612 Pmapping_iter(struct ps_prochandle *P, proc_map_f *func, void *cd) 2613 { 2614 return (i_Pmapping_iter(P, B_FALSE, func, cd)); 2615 } 2616 2617 int 2618 Pmapping_iter_resolved(struct ps_prochandle *P, proc_map_f *func, void *cd) 2619 { 2620 return (i_Pmapping_iter(P, B_TRUE, func, cd)); 2621 } 2622 2623 /* 2624 * Iterate over the process's mapped objects. 2625 */ 2626 static int 2627 i_Pobject_iter(struct ps_prochandle *P, boolean_t lmresolve, 2628 proc_map_f *func, void *cd) 2629 { 2630 map_info_t *mptr; 2631 file_info_t *fptr; 2632 uint_t cnt; 2633 int rc = 0; 2634 2635 (void) Prd_agent(P); /* create file_info_t's for all the mappings */ 2636 Pupdate_maps(P); 2637 2638 for (cnt = P->num_files, fptr = list_next(&P->file_head); 2639 cnt; cnt--, fptr = list_next(fptr)) { 2640 const char *lname; 2641 2642 if (lmresolve && (fptr->file_rname != NULL)) 2643 lname = fptr->file_rname; 2644 else if (fptr->file_lname != NULL) 2645 lname = fptr->file_lname; 2646 else 2647 lname = ""; 2648 2649 if ((mptr = fptr->file_map) == NULL) 2650 continue; 2651 2652 if ((rc = func(cd, &mptr->map_pmap, lname)) != 0) 2653 return (rc); 2654 } 2655 return (0); 2656 } 2657 2658 int 2659 Pobject_iter(struct ps_prochandle *P, proc_map_f *func, void *cd) 2660 { 2661 return (i_Pobject_iter(P, B_FALSE, func, cd)); 2662 } 2663 2664 int 2665 Pobject_iter_resolved(struct ps_prochandle *P, proc_map_f *func, void *cd) 2666 { 2667 return (i_Pobject_iter(P, B_TRUE, func, cd)); 2668 } 2669 2670 static char * 2671 i_Pobjname(struct ps_prochandle *P, boolean_t lmresolve, uintptr_t addr, 2672 char *buffer, size_t bufsize) 2673 { 2674 map_info_t *mptr; 2675 file_info_t *fptr; 2676 2677 /* create all the file_info_t's for all the mappings */ 2678 (void) Prd_agent(P); 2679 2680 if ((mptr = Paddr2mptr(P, addr)) == NULL) 2681 return (NULL); 2682 2683 if (!lmresolve) { 2684 if (((fptr = mptr->map_file) == NULL) || 2685 (fptr->file_lname == NULL)) 2686 return (NULL); 2687 (void) strlcpy(buffer, fptr->file_lname, bufsize); 2688 return (buffer); 2689 } 2690 2691 /* Check for a cached copy of the resolved path */ 2692 if (Pfindmap(P, mptr, buffer, bufsize) != NULL) 2693 return (buffer); 2694 2695 return (NULL); 2696 } 2697 2698 /* 2699 * Given a virtual address, return the name of the underlying 2700 * mapped object (file) as provided by the dynamic linker. 2701 * Return NULL if we can't find any name information for the object. 2702 */ 2703 char * 2704 Pobjname(struct ps_prochandle *P, uintptr_t addr, 2705 char *buffer, size_t bufsize) 2706 { 2707 return (i_Pobjname(P, B_FALSE, addr, buffer, bufsize)); 2708 } 2709 2710 /* 2711 * Given a virtual address, try to return a filesystem path to the 2712 * underlying mapped object (file). If we're in the global zone, 2713 * this path could resolve to an object in another zone. If we're 2714 * unable return a valid filesystem path, we'll fall back to providing 2715 * the mapped object (file) name provided by the dynamic linker in 2716 * the target process (ie, the object reported by Pobjname()). 2717 */ 2718 char * 2719 Pobjname_resolved(struct ps_prochandle *P, uintptr_t addr, 2720 char *buffer, size_t bufsize) 2721 { 2722 return (i_Pobjname(P, B_TRUE, addr, buffer, bufsize)); 2723 } 2724 2725 /* 2726 * Given a virtual address, return the link map id of the underlying mapped 2727 * object (file), as provided by the dynamic linker. Return -1 on failure. 2728 */ 2729 int 2730 Plmid(struct ps_prochandle *P, uintptr_t addr, Lmid_t *lmidp) 2731 { 2732 map_info_t *mptr; 2733 file_info_t *fptr; 2734 2735 /* create all the file_info_t's for all the mappings */ 2736 (void) Prd_agent(P); 2737 2738 if ((mptr = Paddr2mptr(P, addr)) != NULL && 2739 (fptr = mptr->map_file) != NULL && fptr->file_lo != NULL) { 2740 *lmidp = fptr->file_lo->rl_lmident; 2741 return (0); 2742 } 2743 2744 return (-1); 2745 } 2746 2747 /* 2748 * Given an object name and optional lmid, iterate over the object's symbols. 2749 * If which == PR_SYMTAB, search the normal symbol table. 2750 * If which == PR_DYNSYM, search the dynamic symbol table. 2751 */ 2752 static int 2753 Psymbol_iter_com(struct ps_prochandle *P, Lmid_t lmid, const char *object_name, 2754 int which, int mask, pr_order_t order, proc_xsym_f *func, void *cd) 2755 { 2756 GElf_Sym sym; 2757 GElf_Shdr shdr; 2758 map_info_t *mptr; 2759 file_info_t *fptr; 2760 sym_tbl_t *symtab; 2761 size_t symn; 2762 const char *strs; 2763 size_t strsz; 2764 prsyminfo_t si; 2765 int rv; 2766 uint_t *map, i, count, ndx; 2767 2768 if ((mptr = object_name_to_map(P, lmid, object_name)) == NULL) 2769 return (-1); 2770 2771 if ((fptr = build_map_symtab(P, mptr)) == NULL || /* no mapped file */ 2772 fptr->file_elf == NULL) /* not an ELF file */ 2773 return (-1); 2774 2775 /* 2776 * Search the specified symbol table. 2777 */ 2778 switch (which) { 2779 case PR_SYMTAB: 2780 symtab = &fptr->file_symtab; 2781 si.prs_table = PR_SYMTAB; 2782 break; 2783 case PR_DYNSYM: 2784 symtab = &fptr->file_dynsym; 2785 si.prs_table = PR_DYNSYM; 2786 break; 2787 default: 2788 return (-1); 2789 } 2790 2791 si.prs_object = object_name; 2792 si.prs_lmid = fptr->file_lo == NULL ? 2793 LM_ID_BASE : fptr->file_lo->rl_lmident; 2794 2795 symn = symtab->sym_symn; 2796 strs = symtab->sym_strs; 2797 strsz = symtab->sym_strsz; 2798 2799 switch (order) { 2800 case PRO_NATURAL: 2801 map = NULL; 2802 count = symn; 2803 break; 2804 case PRO_BYNAME: 2805 map = symtab->sym_byname; 2806 count = symtab->sym_count; 2807 break; 2808 case PRO_BYADDR: 2809 map = symtab->sym_byaddr; 2810 count = symtab->sym_count; 2811 break; 2812 default: 2813 return (-1); 2814 } 2815 2816 if (symtab->sym_data_pri == NULL || strs == NULL || count == 0) 2817 return (-1); 2818 2819 rv = 0; 2820 2821 for (i = 0; i < count; i++) { 2822 ndx = map == NULL ? i : map[i]; 2823 if (symtab_getsym(symtab, ndx, &sym) != NULL) { 2824 uint_t s_bind, s_type, type; 2825 2826 if (sym.st_name >= strsz) /* invalid st_name */ 2827 continue; 2828 2829 s_bind = GELF_ST_BIND(sym.st_info); 2830 s_type = GELF_ST_TYPE(sym.st_info); 2831 2832 /* 2833 * In case you haven't already guessed, this relies on 2834 * the bitmask used in <libproc.h> for encoding symbol 2835 * type and binding matching the order of STB and STT 2836 * constants in <sys/elf.h>. ELF can't change without 2837 * breaking binary compatibility, so I think this is 2838 * reasonably fair game. 2839 */ 2840 if (s_bind < STB_NUM && s_type < STT_NUM) { 2841 type = (1 << (s_type + 8)) | (1 << s_bind); 2842 if ((type & ~mask) != 0) 2843 continue; 2844 } else 2845 continue; /* Invalid type or binding */ 2846 2847 if (GELF_ST_TYPE(sym.st_info) != STT_TLS) 2848 sym.st_value += fptr->file_dyn_base; 2849 2850 si.prs_name = strs + sym.st_name; 2851 2852 /* 2853 * If symbol's type is STT_SECTION, then try to lookup 2854 * the name of the corresponding section. 2855 */ 2856 if (GELF_ST_TYPE(sym.st_info) == STT_SECTION && 2857 fptr->file_shstrs != NULL && 2858 gelf_getshdr(elf_getscn(fptr->file_elf, 2859 sym.st_shndx), &shdr) != NULL && 2860 shdr.sh_name != 0 && 2861 shdr.sh_name < fptr->file_shstrsz) 2862 si.prs_name = fptr->file_shstrs + shdr.sh_name; 2863 2864 si.prs_id = ndx; 2865 if ((rv = func(cd, &sym, si.prs_name, &si)) != 0) 2866 break; 2867 } 2868 } 2869 2870 return (rv); 2871 } 2872 2873 int 2874 Pxsymbol_iter(struct ps_prochandle *P, Lmid_t lmid, const char *object_name, 2875 int which, int mask, proc_xsym_f *func, void *cd) 2876 { 2877 return (Psymbol_iter_com(P, lmid, object_name, which, mask, 2878 PRO_NATURAL, func, cd)); 2879 } 2880 2881 int 2882 Psymbol_iter_by_lmid(struct ps_prochandle *P, Lmid_t lmid, 2883 const char *object_name, int which, int mask, proc_sym_f *func, void *cd) 2884 { 2885 return (Psymbol_iter_com(P, lmid, object_name, which, mask, 2886 PRO_NATURAL, (proc_xsym_f *)func, cd)); 2887 } 2888 2889 int 2890 Psymbol_iter(struct ps_prochandle *P, 2891 const char *object_name, int which, int mask, proc_sym_f *func, void *cd) 2892 { 2893 return (Psymbol_iter_com(P, PR_LMID_EVERY, object_name, which, mask, 2894 PRO_NATURAL, (proc_xsym_f *)func, cd)); 2895 } 2896 2897 int 2898 Psymbol_iter_by_addr(struct ps_prochandle *P, 2899 const char *object_name, int which, int mask, proc_sym_f *func, void *cd) 2900 { 2901 return (Psymbol_iter_com(P, PR_LMID_EVERY, object_name, which, mask, 2902 PRO_BYADDR, (proc_xsym_f *)func, cd)); 2903 } 2904 2905 int 2906 Psymbol_iter_by_name(struct ps_prochandle *P, 2907 const char *object_name, int which, int mask, proc_sym_f *func, void *cd) 2908 { 2909 return (Psymbol_iter_com(P, PR_LMID_EVERY, object_name, which, mask, 2910 PRO_BYNAME, (proc_xsym_f *)func, cd)); 2911 } 2912 2913 /* 2914 * Get the platform string from the core file if we have it; 2915 * just perform the system call for the caller if this is a live process. 2916 */ 2917 char * 2918 Pplatform(struct ps_prochandle *P, char *s, size_t n) 2919 { 2920 if (P->state == PS_IDLE) { 2921 errno = ENODATA; 2922 return (NULL); 2923 } 2924 2925 if (P->state == PS_DEAD) { 2926 if (P->core->core_platform == NULL) { 2927 errno = ENODATA; 2928 return (NULL); 2929 } 2930 (void) strncpy(s, P->core->core_platform, n - 1); 2931 s[n - 1] = '\0'; 2932 2933 } else if (sysinfo(SI_PLATFORM, s, n) == -1) 2934 return (NULL); 2935 2936 return (s); 2937 } 2938 2939 /* 2940 * Get the uname(2) information from the core file if we have it; 2941 * just perform the system call for the caller if this is a live process. 2942 */ 2943 int 2944 Puname(struct ps_prochandle *P, struct utsname *u) 2945 { 2946 if (P->state == PS_IDLE) { 2947 errno = ENODATA; 2948 return (-1); 2949 } 2950 2951 if (P->state == PS_DEAD) { 2952 if (P->core->core_uts == NULL) { 2953 errno = ENODATA; 2954 return (-1); 2955 } 2956 (void) memcpy(u, P->core->core_uts, sizeof (struct utsname)); 2957 return (0); 2958 } 2959 return (uname(u)); 2960 } 2961 2962 /* 2963 * Called from Pcreate(), Pgrab(), and Pfgrab_core() to initialize 2964 * the symbol table heads in the new ps_prochandle. 2965 */ 2966 void 2967 Pinitsym(struct ps_prochandle *P) 2968 { 2969 P->num_files = 0; 2970 list_link(&P->file_head, NULL); 2971 } 2972 2973 /* 2974 * Called from Prelease() to destroy the symbol tables. 2975 * Must be called by the client after an exec() in the victim process. 2976 */ 2977 void 2978 Preset_maps(struct ps_prochandle *P) 2979 { 2980 int i; 2981 2982 if (P->rap != NULL) { 2983 rd_delete(P->rap); 2984 P->rap = NULL; 2985 } 2986 2987 if (P->execname != NULL) { 2988 free(P->execname); 2989 P->execname = NULL; 2990 } 2991 2992 if (P->auxv != NULL) { 2993 free(P->auxv); 2994 P->auxv = NULL; 2995 P->nauxv = 0; 2996 } 2997 2998 for (i = 0; i < P->map_count; i++) 2999 map_info_free(P, &P->mappings[i]); 3000 3001 if (P->mappings != NULL) { 3002 free(P->mappings); 3003 P->mappings = NULL; 3004 } 3005 P->map_count = P->map_alloc = 0; 3006 3007 P->info_valid = 0; 3008 } 3009 3010 typedef struct getenv_data { 3011 char *buf; 3012 size_t bufsize; 3013 const char *search; 3014 size_t searchlen; 3015 } getenv_data_t; 3016 3017 /*ARGSUSED*/ 3018 static int 3019 getenv_func(void *data, struct ps_prochandle *P, uintptr_t addr, 3020 const char *nameval) 3021 { 3022 getenv_data_t *d = data; 3023 size_t len; 3024 3025 if (nameval == NULL) 3026 return (0); 3027 3028 if (d->searchlen < strlen(nameval) && 3029 strncmp(nameval, d->search, d->searchlen) == 0 && 3030 nameval[d->searchlen] == '=') { 3031 len = MIN(strlen(nameval), d->bufsize - 1); 3032 (void) strncpy(d->buf, nameval, len); 3033 d->buf[len] = '\0'; 3034 return (1); 3035 } 3036 3037 return (0); 3038 } 3039 3040 char * 3041 Pgetenv(struct ps_prochandle *P, const char *name, char *buf, size_t buflen) 3042 { 3043 getenv_data_t d; 3044 3045 d.buf = buf; 3046 d.bufsize = buflen; 3047 d.search = name; 3048 d.searchlen = strlen(name); 3049 3050 if (Penv_iter(P, getenv_func, &d) == 1) { 3051 char *equals = strchr(d.buf, '='); 3052 3053 if (equals != NULL) { 3054 (void) memmove(d.buf, equals + 1, 3055 d.buf + buflen - equals - 1); 3056 d.buf[d.buf + buflen - equals] = '\0'; 3057 3058 return (buf); 3059 } 3060 } 3061 3062 return (NULL); 3063 } 3064 3065 /* number of argument or environment pointers to read all at once */ 3066 #define NARG 100 3067 3068 int 3069 Penv_iter(struct ps_prochandle *P, proc_env_f *func, void *data) 3070 { 3071 const psinfo_t *psp; 3072 uintptr_t envpoff; 3073 GElf_Sym sym; 3074 int ret; 3075 char *buf, *nameval; 3076 size_t buflen; 3077 3078 int nenv = NARG; 3079 long envp[NARG]; 3080 3081 /* 3082 * Attempt to find the "_environ" variable in the process. 3083 * Failing that, use the original value provided by Ppsinfo(). 3084 */ 3085 if ((psp = Ppsinfo(P)) == NULL) 3086 return (-1); 3087 3088 envpoff = psp->pr_envp; /* Default if no _environ found */ 3089 3090 if (Plookup_by_name(P, PR_OBJ_EXEC, "_environ", &sym) == 0) { 3091 if (P->status.pr_dmodel == PR_MODEL_NATIVE) { 3092 if (Pread(P, &envpoff, sizeof (envpoff), 3093 sym.st_value) != sizeof (envpoff)) 3094 envpoff = psp->pr_envp; 3095 } else if (P->status.pr_dmodel == PR_MODEL_ILP32) { 3096 uint32_t envpoff32; 3097 3098 if (Pread(P, &envpoff32, sizeof (envpoff32), 3099 sym.st_value) != sizeof (envpoff32)) 3100 envpoff = psp->pr_envp; 3101 else 3102 envpoff = envpoff32; 3103 } 3104 } 3105 3106 buflen = 128; 3107 buf = malloc(buflen); 3108 3109 ret = 0; 3110 for (;;) { 3111 uintptr_t envoff; 3112 3113 if (nenv == NARG) { 3114 (void) memset(envp, 0, sizeof (envp)); 3115 if (P->status.pr_dmodel == PR_MODEL_NATIVE) { 3116 if (Pread(P, envp, 3117 sizeof (envp), envpoff) <= 0) { 3118 ret = -1; 3119 break; 3120 } 3121 } else if (P->status.pr_dmodel == PR_MODEL_ILP32) { 3122 uint32_t e32[NARG]; 3123 int i; 3124 3125 (void) memset(e32, 0, sizeof (e32)); 3126 if (Pread(P, e32, sizeof (e32), envpoff) <= 0) { 3127 ret = -1; 3128 break; 3129 } 3130 for (i = 0; i < NARG; i++) 3131 envp[i] = e32[i]; 3132 } 3133 nenv = 0; 3134 } 3135 3136 if ((envoff = envp[nenv++]) == NULL) 3137 break; 3138 3139 /* 3140 * Attempt to read the string from the process. 3141 */ 3142 again: 3143 ret = Pread_string(P, buf, buflen, envoff); 3144 3145 if (ret <= 0) { 3146 nameval = NULL; 3147 } else if (ret == buflen - 1) { 3148 free(buf); 3149 /* 3150 * Bail if we have a corrupted environment 3151 */ 3152 if (buflen >= ARG_MAX) 3153 return (-1); 3154 buflen *= 2; 3155 buf = malloc(buflen); 3156 goto again; 3157 } else { 3158 nameval = buf; 3159 } 3160 3161 if ((ret = func(data, P, envoff, nameval)) != 0) 3162 break; 3163 3164 envpoff += (P->status.pr_dmodel == PR_MODEL_LP64)? 8 : 4; 3165 } 3166 3167 free(buf); 3168 3169 return (ret); 3170 } 3171