10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 51880Sahl * Common Development and Distribution License (the "License"). 61880Sahl * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 211880Sahl 220Sstevel@tonic-gate /* 237156Srh87107 * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 272712Snn35248 #include <assert.h> 280Sstevel@tonic-gate #include <stdio.h> 290Sstevel@tonic-gate #include <stdlib.h> 300Sstevel@tonic-gate #include <stddef.h> 310Sstevel@tonic-gate #include <unistd.h> 320Sstevel@tonic-gate #include <ctype.h> 330Sstevel@tonic-gate #include <fcntl.h> 340Sstevel@tonic-gate #include <string.h> 350Sstevel@tonic-gate #include <strings.h> 360Sstevel@tonic-gate #include <memory.h> 370Sstevel@tonic-gate #include <errno.h> 380Sstevel@tonic-gate #include <dirent.h> 390Sstevel@tonic-gate #include <signal.h> 400Sstevel@tonic-gate #include <limits.h> 410Sstevel@tonic-gate #include <libgen.h> 420Sstevel@tonic-gate #include <sys/types.h> 430Sstevel@tonic-gate #include <sys/stat.h> 440Sstevel@tonic-gate #include <sys/systeminfo.h> 450Sstevel@tonic-gate #include <sys/sysmacros.h> 460Sstevel@tonic-gate 470Sstevel@tonic-gate #include "libproc.h" 480Sstevel@tonic-gate #include "Pcontrol.h" 490Sstevel@tonic-gate #include "Putil.h" 503347Sab196087 #include "Psymtab_machelf.h" 510Sstevel@tonic-gate 520Sstevel@tonic-gate static file_info_t *build_map_symtab(struct ps_prochandle *, map_info_t *); 530Sstevel@tonic-gate static map_info_t *exec_map(struct ps_prochandle *); 540Sstevel@tonic-gate static map_info_t *object_to_map(struct ps_prochandle *, Lmid_t, const char *); 550Sstevel@tonic-gate static map_info_t *object_name_to_map(struct ps_prochandle *, 560Sstevel@tonic-gate Lmid_t, const char *); 570Sstevel@tonic-gate static GElf_Sym *sym_by_name(sym_tbl_t *, const char *, GElf_Sym *, uint_t *); 58942Sahl static int read_ehdr32(struct ps_prochandle *, Elf32_Ehdr *, uint_t *, 59942Sahl uintptr_t); 600Sstevel@tonic-gate #ifdef _LP64 61942Sahl static int read_ehdr64(struct ps_prochandle *, Elf64_Ehdr *, uint_t *, 62942Sahl uintptr_t); 630Sstevel@tonic-gate #endif 640Sstevel@tonic-gate 650Sstevel@tonic-gate #define DATA_TYPES \ 660Sstevel@tonic-gate ((1 << STT_OBJECT) | (1 << STT_FUNC) | \ 670Sstevel@tonic-gate (1 << STT_COMMON) | (1 << STT_TLS)) 680Sstevel@tonic-gate #define IS_DATA_TYPE(tp) (((1 << (tp)) & DATA_TYPES) != 0) 690Sstevel@tonic-gate 700Sstevel@tonic-gate #define MA_RWX (MA_READ | MA_WRITE | MA_EXEC) 710Sstevel@tonic-gate 720Sstevel@tonic-gate typedef enum { 730Sstevel@tonic-gate PRO_NATURAL, 740Sstevel@tonic-gate PRO_BYADDR, 750Sstevel@tonic-gate PRO_BYNAME 760Sstevel@tonic-gate } pr_order_t; 770Sstevel@tonic-gate 780Sstevel@tonic-gate static int 790Sstevel@tonic-gate addr_cmp(const void *aa, const void *bb) 800Sstevel@tonic-gate { 810Sstevel@tonic-gate uintptr_t a = *((uintptr_t *)aa); 820Sstevel@tonic-gate uintptr_t b = *((uintptr_t *)bb); 830Sstevel@tonic-gate 840Sstevel@tonic-gate if (a > b) 850Sstevel@tonic-gate return (1); 860Sstevel@tonic-gate if (a < b) 870Sstevel@tonic-gate return (-1); 880Sstevel@tonic-gate return (0); 890Sstevel@tonic-gate } 900Sstevel@tonic-gate 910Sstevel@tonic-gate /* 922915Srh87107 * This function creates a list of addresses for a load object's sections. 932915Srh87107 * The list is in ascending address order and alternates start address 942915Srh87107 * then end address for each section we're interested in. The function 952915Srh87107 * returns a pointer to the list, which must be freed by the caller. 962915Srh87107 */ 972915Srh87107 static uintptr_t * 982915Srh87107 get_saddrs(struct ps_prochandle *P, uintptr_t ehdr_start, uint_t *n) 992915Srh87107 { 1002915Srh87107 uintptr_t a, addr, *addrs, last = 0; 1012915Srh87107 uint_t i, naddrs = 0, unordered = 0; 1022915Srh87107 1032915Srh87107 if (P->status.pr_dmodel == PR_MODEL_ILP32) { 1042915Srh87107 Elf32_Ehdr ehdr; 1052915Srh87107 Elf32_Phdr phdr; 1062915Srh87107 uint_t phnum; 1072915Srh87107 1082915Srh87107 if (read_ehdr32(P, &ehdr, &phnum, ehdr_start) != 0) 1092915Srh87107 return (NULL); 1102915Srh87107 1112915Srh87107 addrs = malloc(sizeof (uintptr_t) * phnum * 2); 1122915Srh87107 a = ehdr_start + ehdr.e_phoff; 1132915Srh87107 for (i = 0; i < phnum; i++, a += ehdr.e_phentsize) { 1142915Srh87107 if (Pread(P, &phdr, sizeof (phdr), a) != 1152915Srh87107 sizeof (phdr)) { 1162915Srh87107 free(addrs); 1172915Srh87107 return (NULL); 1182915Srh87107 } 1192915Srh87107 if (phdr.p_type != PT_LOAD || phdr.p_memsz == 0) 1202915Srh87107 continue; 1212915Srh87107 1222915Srh87107 addr = phdr.p_vaddr; 1232915Srh87107 if (ehdr.e_type == ET_DYN) 1242915Srh87107 addr += ehdr_start; 1252915Srh87107 if (last > addr) 1262915Srh87107 unordered = 1; 1272915Srh87107 addrs[naddrs++] = addr; 1282915Srh87107 addrs[naddrs++] = last = addr + phdr.p_memsz - 1; 1292915Srh87107 } 1302915Srh87107 #ifdef _LP64 1312915Srh87107 } else { 1322915Srh87107 Elf64_Ehdr ehdr; 1332915Srh87107 Elf64_Phdr phdr; 1342915Srh87107 uint_t phnum; 1352915Srh87107 1362915Srh87107 if (read_ehdr64(P, &ehdr, &phnum, ehdr_start) != 0) 1372915Srh87107 return (NULL); 1382915Srh87107 1392915Srh87107 addrs = malloc(sizeof (uintptr_t) * phnum * 2); 1402915Srh87107 a = ehdr_start + ehdr.e_phoff; 1412915Srh87107 for (i = 0; i < phnum; i++, a += ehdr.e_phentsize) { 1422915Srh87107 if (Pread(P, &phdr, sizeof (phdr), a) != 1432915Srh87107 sizeof (phdr)) { 1442915Srh87107 free(addrs); 1452915Srh87107 return (NULL); 1462915Srh87107 } 1472915Srh87107 if (phdr.p_type != PT_LOAD || phdr.p_memsz == 0) 1482915Srh87107 continue; 1492915Srh87107 1502915Srh87107 addr = phdr.p_vaddr; 1512915Srh87107 if (ehdr.e_type == ET_DYN) 1522915Srh87107 addr += ehdr_start; 1532915Srh87107 if (last > addr) 1542915Srh87107 unordered = 1; 1552915Srh87107 addrs[naddrs++] = addr; 1562915Srh87107 addrs[naddrs++] = last = addr + phdr.p_memsz - 1; 1572915Srh87107 } 1582915Srh87107 #endif 1592915Srh87107 } 1602915Srh87107 1612915Srh87107 if (unordered) 1622915Srh87107 qsort(addrs, naddrs, sizeof (uintptr_t), addr_cmp); 1632915Srh87107 1642915Srh87107 *n = naddrs; 1652915Srh87107 return (addrs); 1662915Srh87107 } 1672915Srh87107 1682915Srh87107 /* 1690Sstevel@tonic-gate * Allocation function for a new file_info_t 1700Sstevel@tonic-gate */ 1714753Srh87107 file_info_t * 1720Sstevel@tonic-gate file_info_new(struct ps_prochandle *P, map_info_t *mptr) 1730Sstevel@tonic-gate { 1740Sstevel@tonic-gate file_info_t *fptr; 1750Sstevel@tonic-gate map_info_t *mp; 1764753Srh87107 uintptr_t mstart, mend, sstart, send; 1774753Srh87107 uint_t i; 1780Sstevel@tonic-gate 1790Sstevel@tonic-gate if ((fptr = calloc(1, sizeof (file_info_t))) == NULL) 1800Sstevel@tonic-gate return (NULL); 1810Sstevel@tonic-gate 1820Sstevel@tonic-gate list_link(fptr, &P->file_head); 1830Sstevel@tonic-gate (void) strcpy(fptr->file_pname, mptr->map_pmap.pr_mapname); 1840Sstevel@tonic-gate mptr->map_file = fptr; 1850Sstevel@tonic-gate fptr->file_ref = 1; 1860Sstevel@tonic-gate fptr->file_fd = -1; 1870Sstevel@tonic-gate P->num_files++; 1880Sstevel@tonic-gate 1890Sstevel@tonic-gate /* 1900Sstevel@tonic-gate * To figure out which map_info_t instances correspond to the mappings 1912915Srh87107 * for this load object we try to obtain the start and end address 1922915Srh87107 * for each section of our in-memory ELF image. If successful, we 1930Sstevel@tonic-gate * walk down the list of addresses and the list of map_info_t 1940Sstevel@tonic-gate * instances in lock step to correctly find the mappings that 1950Sstevel@tonic-gate * correspond to this load object. 1960Sstevel@tonic-gate */ 1972915Srh87107 if ((fptr->file_saddrs = get_saddrs(P, mptr->map_pmap.pr_vaddr, 1982915Srh87107 &fptr->file_nsaddrs)) == NULL) 1992915Srh87107 return (fptr); 2000Sstevel@tonic-gate 2010Sstevel@tonic-gate mp = P->mappings; 2024753Srh87107 i = 0; 2034753Srh87107 while (mp < P->mappings + P->map_count && i < fptr->file_nsaddrs) { 2044753Srh87107 2054753Srh87107 /* Calculate the start and end of the mapping and section */ 2064753Srh87107 mstart = mp->map_pmap.pr_vaddr; 2074753Srh87107 mend = mp->map_pmap.pr_vaddr + mp->map_pmap.pr_size; 2084753Srh87107 sstart = fptr->file_saddrs[i]; 2094753Srh87107 send = fptr->file_saddrs[i + 1]; 2104753Srh87107 2114753Srh87107 if (mend <= sstart) { 2124753Srh87107 /* This mapping is below the current section */ 2134753Srh87107 mp++; 2144753Srh87107 } else if (mstart >= send) { 2154753Srh87107 /* This mapping is above the current section */ 2164753Srh87107 i += 2; 2170Sstevel@tonic-gate } else { 2184753Srh87107 /* This mapping overlaps the current section */ 2194753Srh87107 if (mp->map_file == NULL) { 2204753Srh87107 dprintf("file_info_new: associating " 2214753Srh87107 "segment at %p\n", 2224753Srh87107 (void *)mp->map_pmap.pr_vaddr); 2234753Srh87107 mp->map_file = fptr; 2244753Srh87107 fptr->file_ref++; 2254753Srh87107 } else { 2264753Srh87107 dprintf("file_info_new: segment at %p " 2274753Srh87107 "already associated with %s\n", 2284753Srh87107 (void *)mp->map_pmap.pr_vaddr, 2294753Srh87107 (mp == mptr ? "this file" : 2304753Srh87107 mp->map_file->file_pname)); 2314753Srh87107 } 2320Sstevel@tonic-gate mp++; 2330Sstevel@tonic-gate } 2340Sstevel@tonic-gate } 2350Sstevel@tonic-gate 2360Sstevel@tonic-gate return (fptr); 2370Sstevel@tonic-gate } 2380Sstevel@tonic-gate 2390Sstevel@tonic-gate /* 2400Sstevel@tonic-gate * Deallocation function for a file_info_t 2410Sstevel@tonic-gate */ 2420Sstevel@tonic-gate static void 2430Sstevel@tonic-gate file_info_free(struct ps_prochandle *P, file_info_t *fptr) 2440Sstevel@tonic-gate { 2450Sstevel@tonic-gate if (--fptr->file_ref == 0) { 2460Sstevel@tonic-gate list_unlink(fptr); 2470Sstevel@tonic-gate if (fptr->file_symtab.sym_elf) { 2480Sstevel@tonic-gate (void) elf_end(fptr->file_symtab.sym_elf); 2490Sstevel@tonic-gate free(fptr->file_symtab.sym_elfmem); 2500Sstevel@tonic-gate } 2510Sstevel@tonic-gate if (fptr->file_symtab.sym_byname) 2520Sstevel@tonic-gate free(fptr->file_symtab.sym_byname); 2530Sstevel@tonic-gate if (fptr->file_symtab.sym_byaddr) 2540Sstevel@tonic-gate free(fptr->file_symtab.sym_byaddr); 2550Sstevel@tonic-gate 2560Sstevel@tonic-gate if (fptr->file_dynsym.sym_elf) { 2570Sstevel@tonic-gate (void) elf_end(fptr->file_dynsym.sym_elf); 2580Sstevel@tonic-gate free(fptr->file_dynsym.sym_elfmem); 2590Sstevel@tonic-gate } 2600Sstevel@tonic-gate if (fptr->file_dynsym.sym_byname) 2610Sstevel@tonic-gate free(fptr->file_dynsym.sym_byname); 2620Sstevel@tonic-gate if (fptr->file_dynsym.sym_byaddr) 2630Sstevel@tonic-gate free(fptr->file_dynsym.sym_byaddr); 2640Sstevel@tonic-gate 2650Sstevel@tonic-gate if (fptr->file_lo) 2660Sstevel@tonic-gate free(fptr->file_lo); 2670Sstevel@tonic-gate if (fptr->file_lname) 2680Sstevel@tonic-gate free(fptr->file_lname); 269*7675SEdward.Pilatowicz@Sun.COM if (fptr->file_rname) 270*7675SEdward.Pilatowicz@Sun.COM free(fptr->file_rname); 2710Sstevel@tonic-gate if (fptr->file_elf) 2720Sstevel@tonic-gate (void) elf_end(fptr->file_elf); 2730Sstevel@tonic-gate if (fptr->file_elfmem != NULL) 2740Sstevel@tonic-gate free(fptr->file_elfmem); 2750Sstevel@tonic-gate if (fptr->file_fd >= 0) 2760Sstevel@tonic-gate (void) close(fptr->file_fd); 2770Sstevel@tonic-gate if (fptr->file_ctfp) { 2780Sstevel@tonic-gate ctf_close(fptr->file_ctfp); 2790Sstevel@tonic-gate free(fptr->file_ctf_buf); 2800Sstevel@tonic-gate } 2812915Srh87107 if (fptr->file_saddrs) 2822915Srh87107 free(fptr->file_saddrs); 2830Sstevel@tonic-gate free(fptr); 2840Sstevel@tonic-gate P->num_files--; 2850Sstevel@tonic-gate } 2860Sstevel@tonic-gate } 2870Sstevel@tonic-gate 2880Sstevel@tonic-gate /* 2890Sstevel@tonic-gate * Deallocation function for a map_info_t 2900Sstevel@tonic-gate */ 2910Sstevel@tonic-gate static void 2920Sstevel@tonic-gate map_info_free(struct ps_prochandle *P, map_info_t *mptr) 2930Sstevel@tonic-gate { 2940Sstevel@tonic-gate file_info_t *fptr; 2950Sstevel@tonic-gate 2960Sstevel@tonic-gate if ((fptr = mptr->map_file) != NULL) { 2970Sstevel@tonic-gate if (fptr->file_map == mptr) 2980Sstevel@tonic-gate fptr->file_map = NULL; 2990Sstevel@tonic-gate file_info_free(P, fptr); 3000Sstevel@tonic-gate } 3010Sstevel@tonic-gate if (P->execname && mptr == P->map_exec) { 3020Sstevel@tonic-gate free(P->execname); 3030Sstevel@tonic-gate P->execname = NULL; 3040Sstevel@tonic-gate } 3050Sstevel@tonic-gate if (P->auxv && (mptr == P->map_exec || mptr == P->map_ldso)) { 3060Sstevel@tonic-gate free(P->auxv); 3070Sstevel@tonic-gate P->auxv = NULL; 3080Sstevel@tonic-gate P->nauxv = 0; 3090Sstevel@tonic-gate } 3100Sstevel@tonic-gate if (mptr == P->map_exec) 3110Sstevel@tonic-gate P->map_exec = NULL; 3120Sstevel@tonic-gate if (mptr == P->map_ldso) 3130Sstevel@tonic-gate P->map_ldso = NULL; 3140Sstevel@tonic-gate } 3150Sstevel@tonic-gate 3160Sstevel@tonic-gate /* 3170Sstevel@tonic-gate * Call-back function for librtld_db to iterate through all of its shared 3180Sstevel@tonic-gate * libraries. We use this to get the load object names for the mappings. 3190Sstevel@tonic-gate */ 3200Sstevel@tonic-gate static int 3210Sstevel@tonic-gate map_iter(const rd_loadobj_t *lop, void *cd) 3220Sstevel@tonic-gate { 3230Sstevel@tonic-gate char buf[PATH_MAX]; 3240Sstevel@tonic-gate struct ps_prochandle *P = cd; 3250Sstevel@tonic-gate map_info_t *mptr; 3260Sstevel@tonic-gate file_info_t *fptr; 3270Sstevel@tonic-gate 3280Sstevel@tonic-gate dprintf("encountered rd object at %p\n", (void *)lop->rl_base); 3290Sstevel@tonic-gate 3302712Snn35248 if ((mptr = Paddr2mptr(P, lop->rl_base)) == NULL) { 3312712Snn35248 dprintf("map_iter: base address doesn't match any mapping\n"); 3320Sstevel@tonic-gate return (1); /* Base address does not match any mapping */ 3332712Snn35248 } 3340Sstevel@tonic-gate 3350Sstevel@tonic-gate if ((fptr = mptr->map_file) == NULL && 3362712Snn35248 (fptr = file_info_new(P, mptr)) == NULL) { 3372712Snn35248 dprintf("map_iter: failed to allocate a new file_info_t\n"); 3380Sstevel@tonic-gate return (1); /* Failed to allocate a new file_info_t */ 3392712Snn35248 } 3400Sstevel@tonic-gate 3410Sstevel@tonic-gate if ((fptr->file_lo == NULL) && 3420Sstevel@tonic-gate (fptr->file_lo = malloc(sizeof (rd_loadobj_t))) == NULL) { 3432712Snn35248 dprintf("map_iter: failed to allocate rd_loadobj_t\n"); 3440Sstevel@tonic-gate file_info_free(P, fptr); 3450Sstevel@tonic-gate return (1); /* Failed to allocate rd_loadobj_t */ 3460Sstevel@tonic-gate } 3470Sstevel@tonic-gate 3480Sstevel@tonic-gate fptr->file_map = mptr; 3490Sstevel@tonic-gate *fptr->file_lo = *lop; 3500Sstevel@tonic-gate 3510Sstevel@tonic-gate fptr->file_lo->rl_plt_base = fptr->file_plt_base; 3520Sstevel@tonic-gate fptr->file_lo->rl_plt_size = fptr->file_plt_size; 3530Sstevel@tonic-gate 3540Sstevel@tonic-gate if (fptr->file_lname) { 3550Sstevel@tonic-gate free(fptr->file_lname); 3560Sstevel@tonic-gate fptr->file_lname = NULL; 357*7675SEdward.Pilatowicz@Sun.COM fptr->file_lbase = NULL; 358*7675SEdward.Pilatowicz@Sun.COM } 359*7675SEdward.Pilatowicz@Sun.COM if (fptr->file_rname) { 360*7675SEdward.Pilatowicz@Sun.COM free(fptr->file_rname); 361*7675SEdward.Pilatowicz@Sun.COM fptr->file_rname = NULL; 362*7675SEdward.Pilatowicz@Sun.COM fptr->file_rbase = NULL; 3630Sstevel@tonic-gate } 3640Sstevel@tonic-gate 3650Sstevel@tonic-gate if (Pread_string(P, buf, sizeof (buf), lop->rl_nameaddr) > 0) { 3660Sstevel@tonic-gate if ((fptr->file_lname = strdup(buf)) != NULL) 3670Sstevel@tonic-gate fptr->file_lbase = basename(fptr->file_lname); 3682712Snn35248 } else { 3692712Snn35248 dprintf("map_iter: failed to read string at %p\n", 3702712Snn35248 (void *)lop->rl_nameaddr); 3710Sstevel@tonic-gate } 3720Sstevel@tonic-gate 373*7675SEdward.Pilatowicz@Sun.COM if ((Pfindmap(P, mptr, buf, sizeof (buf)) != NULL) && 374*7675SEdward.Pilatowicz@Sun.COM ((fptr->file_rname = strdup(buf)) != NULL)) 375*7675SEdward.Pilatowicz@Sun.COM fptr->file_rbase = basename(fptr->file_rname); 376*7675SEdward.Pilatowicz@Sun.COM 3770Sstevel@tonic-gate dprintf("loaded rd object %s lmid %lx\n", 378*7675SEdward.Pilatowicz@Sun.COM fptr->file_lname ? buf : "<NULL>", lop->rl_lmident); 3790Sstevel@tonic-gate return (1); 3800Sstevel@tonic-gate } 3810Sstevel@tonic-gate 3820Sstevel@tonic-gate static void 3830Sstevel@tonic-gate map_set(struct ps_prochandle *P, map_info_t *mptr, const char *lname) 3840Sstevel@tonic-gate { 3850Sstevel@tonic-gate file_info_t *fptr; 3860Sstevel@tonic-gate 3870Sstevel@tonic-gate if ((fptr = mptr->map_file) == NULL && 3880Sstevel@tonic-gate (fptr = file_info_new(P, mptr)) == NULL) 3890Sstevel@tonic-gate return; /* Failed to allocate a new file_info_t */ 3900Sstevel@tonic-gate 3910Sstevel@tonic-gate fptr->file_map = mptr; 3920Sstevel@tonic-gate 3930Sstevel@tonic-gate if ((fptr->file_lo == NULL) && 3940Sstevel@tonic-gate (fptr->file_lo = malloc(sizeof (rd_loadobj_t))) == NULL) { 3950Sstevel@tonic-gate file_info_free(P, fptr); 3960Sstevel@tonic-gate return; /* Failed to allocate rd_loadobj_t */ 3970Sstevel@tonic-gate } 3980Sstevel@tonic-gate 3990Sstevel@tonic-gate (void) memset(fptr->file_lo, 0, sizeof (rd_loadobj_t)); 4000Sstevel@tonic-gate fptr->file_lo->rl_base = mptr->map_pmap.pr_vaddr; 4010Sstevel@tonic-gate fptr->file_lo->rl_bend = 4022712Snn35248 mptr->map_pmap.pr_vaddr + mptr->map_pmap.pr_size; 4030Sstevel@tonic-gate 4040Sstevel@tonic-gate fptr->file_lo->rl_plt_base = fptr->file_plt_base; 4050Sstevel@tonic-gate fptr->file_lo->rl_plt_size = fptr->file_plt_size; 4060Sstevel@tonic-gate 407*7675SEdward.Pilatowicz@Sun.COM if ((fptr->file_lname == NULL) && 4082712Snn35248 (fptr->file_lname = strdup(lname)) != NULL) 4090Sstevel@tonic-gate fptr->file_lbase = basename(fptr->file_lname); 410*7675SEdward.Pilatowicz@Sun.COM 411*7675SEdward.Pilatowicz@Sun.COM /* 412*7675SEdward.Pilatowicz@Sun.COM * Don't bother to set file_rname/file_rbase since lname is really 413*7675SEdward.Pilatowicz@Sun.COM * just a token name (either "a.out" or "ld.so.1") and not a real 414*7675SEdward.Pilatowicz@Sun.COM * filesystem object path. 415*7675SEdward.Pilatowicz@Sun.COM */ 4160Sstevel@tonic-gate } 4170Sstevel@tonic-gate 4180Sstevel@tonic-gate static void 4190Sstevel@tonic-gate load_static_maps(struct ps_prochandle *P) 4200Sstevel@tonic-gate { 4210Sstevel@tonic-gate map_info_t *mptr; 4220Sstevel@tonic-gate 4230Sstevel@tonic-gate /* 4240Sstevel@tonic-gate * Construct the map for the a.out. 4250Sstevel@tonic-gate */ 4260Sstevel@tonic-gate if ((mptr = object_name_to_map(P, PR_LMID_EVERY, PR_OBJ_EXEC)) != NULL) 4270Sstevel@tonic-gate map_set(P, mptr, "a.out"); 4280Sstevel@tonic-gate 4290Sstevel@tonic-gate /* 4300Sstevel@tonic-gate * If the dynamic linker exists for this process, 4310Sstevel@tonic-gate * construct the map for it. 4320Sstevel@tonic-gate */ 4330Sstevel@tonic-gate if (Pgetauxval(P, AT_BASE) != -1L && 4340Sstevel@tonic-gate (mptr = object_name_to_map(P, PR_LMID_EVERY, PR_OBJ_LDSO)) != NULL) 4350Sstevel@tonic-gate map_set(P, mptr, "ld.so.1"); 4360Sstevel@tonic-gate } 4370Sstevel@tonic-gate 4380Sstevel@tonic-gate /* 4390Sstevel@tonic-gate * Go through all the address space mappings, validating or updating 4400Sstevel@tonic-gate * the information already gathered, or gathering new information. 4410Sstevel@tonic-gate * 4420Sstevel@tonic-gate * This function is only called when we suspect that the mappings have changed 4430Sstevel@tonic-gate * because this is the first time we're calling it or because of rtld activity. 4440Sstevel@tonic-gate */ 4450Sstevel@tonic-gate void 4460Sstevel@tonic-gate Pupdate_maps(struct ps_prochandle *P) 4470Sstevel@tonic-gate { 4482712Snn35248 char mapfile[PATH_MAX]; 4490Sstevel@tonic-gate int mapfd; 4500Sstevel@tonic-gate struct stat statb; 4510Sstevel@tonic-gate prmap_t *Pmap = NULL; 4520Sstevel@tonic-gate prmap_t *pmap; 4530Sstevel@tonic-gate ssize_t nmap; 4540Sstevel@tonic-gate int i; 4550Sstevel@tonic-gate uint_t oldmapcount; 4560Sstevel@tonic-gate map_info_t *newmap, *newp; 4570Sstevel@tonic-gate map_info_t *mptr; 4580Sstevel@tonic-gate 459457Sbmc if (P->info_valid || P->state == PS_UNDEAD) 4600Sstevel@tonic-gate return; 4610Sstevel@tonic-gate 4620Sstevel@tonic-gate Preadauxvec(P); 4630Sstevel@tonic-gate 4642712Snn35248 (void) snprintf(mapfile, sizeof (mapfile), "%s/%d/map", 4652712Snn35248 procfs_path, (int)P->pid); 4660Sstevel@tonic-gate if ((mapfd = open(mapfile, O_RDONLY)) < 0 || 4670Sstevel@tonic-gate fstat(mapfd, &statb) != 0 || 4680Sstevel@tonic-gate statb.st_size < sizeof (prmap_t) || 4690Sstevel@tonic-gate (Pmap = malloc(statb.st_size)) == NULL || 4700Sstevel@tonic-gate (nmap = pread(mapfd, Pmap, statb.st_size, 0L)) <= 0 || 4710Sstevel@tonic-gate (nmap /= sizeof (prmap_t)) == 0) { 4720Sstevel@tonic-gate if (Pmap != NULL) 4730Sstevel@tonic-gate free(Pmap); 4740Sstevel@tonic-gate if (mapfd >= 0) 4750Sstevel@tonic-gate (void) close(mapfd); 4760Sstevel@tonic-gate Preset_maps(P); /* utter failure; destroy tables */ 4770Sstevel@tonic-gate return; 4780Sstevel@tonic-gate } 4790Sstevel@tonic-gate (void) close(mapfd); 4800Sstevel@tonic-gate 4810Sstevel@tonic-gate if ((newmap = calloc(1, nmap * sizeof (map_info_t))) == NULL) 4820Sstevel@tonic-gate return; 4830Sstevel@tonic-gate 4840Sstevel@tonic-gate /* 4850Sstevel@tonic-gate * We try to merge any file information we may have for existing 4860Sstevel@tonic-gate * mappings, to avoid having to rebuild the file info. 4870Sstevel@tonic-gate */ 4880Sstevel@tonic-gate mptr = P->mappings; 4890Sstevel@tonic-gate pmap = Pmap; 4900Sstevel@tonic-gate newp = newmap; 4910Sstevel@tonic-gate oldmapcount = P->map_count; 4920Sstevel@tonic-gate for (i = 0; i < nmap; i++, pmap++, newp++) { 4930Sstevel@tonic-gate 4940Sstevel@tonic-gate if (oldmapcount == 0) { 4950Sstevel@tonic-gate /* 4960Sstevel@tonic-gate * We've exhausted all the old mappings. Every new 4970Sstevel@tonic-gate * mapping should be added. 4980Sstevel@tonic-gate */ 4990Sstevel@tonic-gate newp->map_pmap = *pmap; 5000Sstevel@tonic-gate 5010Sstevel@tonic-gate } else if (pmap->pr_vaddr == mptr->map_pmap.pr_vaddr && 5020Sstevel@tonic-gate pmap->pr_size == mptr->map_pmap.pr_size && 5030Sstevel@tonic-gate pmap->pr_offset == mptr->map_pmap.pr_offset && 5040Sstevel@tonic-gate (pmap->pr_mflags & ~(MA_BREAK | MA_STACK)) == 5050Sstevel@tonic-gate (mptr->map_pmap.pr_mflags & ~(MA_BREAK | MA_STACK)) && 5060Sstevel@tonic-gate pmap->pr_pagesize == mptr->map_pmap.pr_pagesize && 5070Sstevel@tonic-gate pmap->pr_shmid == mptr->map_pmap.pr_shmid && 5080Sstevel@tonic-gate strcmp(pmap->pr_mapname, mptr->map_pmap.pr_mapname) == 0) { 5090Sstevel@tonic-gate 5100Sstevel@tonic-gate /* 5110Sstevel@tonic-gate * This mapping matches exactly. Copy over the old 5120Sstevel@tonic-gate * mapping, taking care to get the latest flags. 5130Sstevel@tonic-gate * Make sure the associated file_info_t is updated 5140Sstevel@tonic-gate * appropriately. 5150Sstevel@tonic-gate */ 5160Sstevel@tonic-gate *newp = *mptr; 5170Sstevel@tonic-gate if (P->map_exec == mptr) 5180Sstevel@tonic-gate P->map_exec = newp; 5190Sstevel@tonic-gate if (P->map_ldso == mptr) 5200Sstevel@tonic-gate P->map_ldso = newp; 5210Sstevel@tonic-gate newp->map_pmap.pr_mflags = pmap->pr_mflags; 5220Sstevel@tonic-gate if (mptr->map_file != NULL && 5230Sstevel@tonic-gate mptr->map_file->file_map == mptr) 5240Sstevel@tonic-gate mptr->map_file->file_map = newp; 5250Sstevel@tonic-gate oldmapcount--; 5260Sstevel@tonic-gate mptr++; 5270Sstevel@tonic-gate 5280Sstevel@tonic-gate } else if (pmap->pr_vaddr + pmap->pr_size > 5290Sstevel@tonic-gate mptr->map_pmap.pr_vaddr) { 5300Sstevel@tonic-gate 5310Sstevel@tonic-gate /* 5320Sstevel@tonic-gate * The old mapping doesn't exist any more, remove it 5330Sstevel@tonic-gate * from the list. 5340Sstevel@tonic-gate */ 5350Sstevel@tonic-gate map_info_free(P, mptr); 5360Sstevel@tonic-gate oldmapcount--; 5370Sstevel@tonic-gate i--; 5380Sstevel@tonic-gate newp--; 5390Sstevel@tonic-gate pmap--; 5400Sstevel@tonic-gate mptr++; 5410Sstevel@tonic-gate 5420Sstevel@tonic-gate } else { 5430Sstevel@tonic-gate 5440Sstevel@tonic-gate /* 5450Sstevel@tonic-gate * This is a new mapping, add it directly. 5460Sstevel@tonic-gate */ 5470Sstevel@tonic-gate newp->map_pmap = *pmap; 5480Sstevel@tonic-gate } 5490Sstevel@tonic-gate } 5500Sstevel@tonic-gate 5510Sstevel@tonic-gate /* 5520Sstevel@tonic-gate * Free any old maps 5530Sstevel@tonic-gate */ 5540Sstevel@tonic-gate while (oldmapcount) { 5550Sstevel@tonic-gate map_info_free(P, mptr); 5560Sstevel@tonic-gate oldmapcount--; 5570Sstevel@tonic-gate mptr++; 5580Sstevel@tonic-gate } 5590Sstevel@tonic-gate 5600Sstevel@tonic-gate free(Pmap); 5610Sstevel@tonic-gate if (P->mappings != NULL) 5620Sstevel@tonic-gate free(P->mappings); 5630Sstevel@tonic-gate P->mappings = newmap; 5640Sstevel@tonic-gate P->map_count = P->map_alloc = nmap; 5650Sstevel@tonic-gate P->info_valid = 1; 5660Sstevel@tonic-gate 5670Sstevel@tonic-gate /* 5680Sstevel@tonic-gate * Consult librtld_db to get the load object 5690Sstevel@tonic-gate * names for all of the shared libraries. 5700Sstevel@tonic-gate */ 5710Sstevel@tonic-gate if (P->rap != NULL) 5720Sstevel@tonic-gate (void) rd_loadobj_iter(P->rap, map_iter, P); 5730Sstevel@tonic-gate } 5740Sstevel@tonic-gate 5750Sstevel@tonic-gate /* 5760Sstevel@tonic-gate * Update all of the mappings and rtld_db as if by Pupdate_maps(), and then 5770Sstevel@tonic-gate * forcibly cache all of the symbol tables associated with all object files. 5780Sstevel@tonic-gate */ 5790Sstevel@tonic-gate void 5800Sstevel@tonic-gate Pupdate_syms(struct ps_prochandle *P) 5810Sstevel@tonic-gate { 5827156Srh87107 file_info_t *fptr; 5830Sstevel@tonic-gate int i; 5840Sstevel@tonic-gate 5850Sstevel@tonic-gate Pupdate_maps(P); 5860Sstevel@tonic-gate 5877156Srh87107 for (i = 0, fptr = list_next(&P->file_head); i < P->num_files; 5887156Srh87107 i++, fptr = list_next(fptr)) { 5890Sstevel@tonic-gate Pbuild_file_symtab(P, fptr); 5900Sstevel@tonic-gate (void) Pbuild_file_ctf(P, fptr); 5910Sstevel@tonic-gate } 5920Sstevel@tonic-gate } 5930Sstevel@tonic-gate 5940Sstevel@tonic-gate /* 5950Sstevel@tonic-gate * Return the librtld_db agent handle for the victim process. 5960Sstevel@tonic-gate * The handle will become invalid at the next successful exec() and the 5970Sstevel@tonic-gate * client (caller of proc_rd_agent()) must not use it beyond that point. 5980Sstevel@tonic-gate * If the process is already dead, we've already tried our best to 5990Sstevel@tonic-gate * create the agent during core file initialization. 6000Sstevel@tonic-gate */ 6010Sstevel@tonic-gate rd_agent_t * 6020Sstevel@tonic-gate Prd_agent(struct ps_prochandle *P) 6030Sstevel@tonic-gate { 6040Sstevel@tonic-gate if (P->rap == NULL && P->state != PS_DEAD && P->state != PS_IDLE) { 6050Sstevel@tonic-gate Pupdate_maps(P); 6060Sstevel@tonic-gate if (P->num_files == 0) 6070Sstevel@tonic-gate load_static_maps(P); 6080Sstevel@tonic-gate rd_log(_libproc_debug); 6090Sstevel@tonic-gate if ((P->rap = rd_new(P)) != NULL) 6100Sstevel@tonic-gate (void) rd_loadobj_iter(P->rap, map_iter, P); 6110Sstevel@tonic-gate } 6120Sstevel@tonic-gate return (P->rap); 6130Sstevel@tonic-gate } 6140Sstevel@tonic-gate 6150Sstevel@tonic-gate /* 6160Sstevel@tonic-gate * Return the prmap_t structure containing 'addr', but only if it 6170Sstevel@tonic-gate * is in the dynamic linker's link map and is the text section. 6180Sstevel@tonic-gate */ 6190Sstevel@tonic-gate const prmap_t * 6200Sstevel@tonic-gate Paddr_to_text_map(struct ps_prochandle *P, uintptr_t addr) 6210Sstevel@tonic-gate { 6220Sstevel@tonic-gate map_info_t *mptr; 6230Sstevel@tonic-gate 6240Sstevel@tonic-gate if (!P->info_valid) 6250Sstevel@tonic-gate Pupdate_maps(P); 6260Sstevel@tonic-gate 6270Sstevel@tonic-gate if ((mptr = Paddr2mptr(P, addr)) != NULL) { 6280Sstevel@tonic-gate file_info_t *fptr = build_map_symtab(P, mptr); 6290Sstevel@tonic-gate const prmap_t *pmp = &mptr->map_pmap; 6300Sstevel@tonic-gate 6314753Srh87107 /* 6324753Srh87107 * Assume that if rl_data_base is NULL, it means that no 6334753Srh87107 * data section was found for this load object, and that 6344753Srh87107 * a section must be text. Otherwise, a section will be 6354753Srh87107 * text unless it ends above the start of the data 6364753Srh87107 * section. 6374753Srh87107 */ 6380Sstevel@tonic-gate if (fptr != NULL && fptr->file_lo != NULL && 6394753Srh87107 (fptr->file_lo->rl_data_base == NULL || 6404753Srh87107 pmp->pr_vaddr + pmp->pr_size < 6414753Srh87107 fptr->file_lo->rl_data_base)) 6420Sstevel@tonic-gate return (pmp); 6430Sstevel@tonic-gate } 6440Sstevel@tonic-gate 6450Sstevel@tonic-gate return (NULL); 6460Sstevel@tonic-gate } 6470Sstevel@tonic-gate 6480Sstevel@tonic-gate /* 6490Sstevel@tonic-gate * Return the prmap_t structure containing 'addr' (no restrictions on 6500Sstevel@tonic-gate * the type of mapping). 6510Sstevel@tonic-gate */ 6520Sstevel@tonic-gate const prmap_t * 6530Sstevel@tonic-gate Paddr_to_map(struct ps_prochandle *P, uintptr_t addr) 6540Sstevel@tonic-gate { 6550Sstevel@tonic-gate map_info_t *mptr; 6560Sstevel@tonic-gate 6570Sstevel@tonic-gate if (!P->info_valid) 6580Sstevel@tonic-gate Pupdate_maps(P); 6590Sstevel@tonic-gate 6600Sstevel@tonic-gate if ((mptr = Paddr2mptr(P, addr)) != NULL) 6610Sstevel@tonic-gate return (&mptr->map_pmap); 6620Sstevel@tonic-gate 6630Sstevel@tonic-gate return (NULL); 6640Sstevel@tonic-gate } 6650Sstevel@tonic-gate 6660Sstevel@tonic-gate /* 6670Sstevel@tonic-gate * Convert a full or partial load object name to the prmap_t for its 6680Sstevel@tonic-gate * corresponding primary text mapping. 6690Sstevel@tonic-gate */ 6700Sstevel@tonic-gate const prmap_t * 6710Sstevel@tonic-gate Plmid_to_map(struct ps_prochandle *P, Lmid_t lmid, const char *name) 6720Sstevel@tonic-gate { 6730Sstevel@tonic-gate map_info_t *mptr; 6740Sstevel@tonic-gate 6750Sstevel@tonic-gate if (name == PR_OBJ_EVERY) 6760Sstevel@tonic-gate return (NULL); /* A reasonable mistake */ 6770Sstevel@tonic-gate 6780Sstevel@tonic-gate if ((mptr = object_name_to_map(P, lmid, name)) != NULL) 6790Sstevel@tonic-gate return (&mptr->map_pmap); 6800Sstevel@tonic-gate 6810Sstevel@tonic-gate return (NULL); 6820Sstevel@tonic-gate } 6830Sstevel@tonic-gate 6840Sstevel@tonic-gate const prmap_t * 6850Sstevel@tonic-gate Pname_to_map(struct ps_prochandle *P, const char *name) 6860Sstevel@tonic-gate { 6870Sstevel@tonic-gate return (Plmid_to_map(P, PR_LMID_EVERY, name)); 6880Sstevel@tonic-gate } 6890Sstevel@tonic-gate 6900Sstevel@tonic-gate const rd_loadobj_t * 6910Sstevel@tonic-gate Paddr_to_loadobj(struct ps_prochandle *P, uintptr_t addr) 6920Sstevel@tonic-gate { 6930Sstevel@tonic-gate map_info_t *mptr; 6940Sstevel@tonic-gate 6950Sstevel@tonic-gate if (!P->info_valid) 6960Sstevel@tonic-gate Pupdate_maps(P); 6970Sstevel@tonic-gate 6980Sstevel@tonic-gate if ((mptr = Paddr2mptr(P, addr)) == NULL) 6990Sstevel@tonic-gate return (NULL); 7000Sstevel@tonic-gate 7010Sstevel@tonic-gate /* 7020Sstevel@tonic-gate * By building the symbol table, we implicitly bring the PLT 7030Sstevel@tonic-gate * information up to date in the load object. 7040Sstevel@tonic-gate */ 7050Sstevel@tonic-gate (void) build_map_symtab(P, mptr); 7060Sstevel@tonic-gate 7070Sstevel@tonic-gate return (mptr->map_file->file_lo); 7080Sstevel@tonic-gate } 7090Sstevel@tonic-gate 7100Sstevel@tonic-gate const rd_loadobj_t * 7110Sstevel@tonic-gate Plmid_to_loadobj(struct ps_prochandle *P, Lmid_t lmid, const char *name) 7120Sstevel@tonic-gate { 7130Sstevel@tonic-gate map_info_t *mptr; 7140Sstevel@tonic-gate 7150Sstevel@tonic-gate if (name == PR_OBJ_EVERY) 7160Sstevel@tonic-gate return (NULL); 7170Sstevel@tonic-gate 7180Sstevel@tonic-gate if ((mptr = object_name_to_map(P, lmid, name)) == NULL) 7190Sstevel@tonic-gate return (NULL); 7200Sstevel@tonic-gate 7210Sstevel@tonic-gate /* 7220Sstevel@tonic-gate * By building the symbol table, we implicitly bring the PLT 7230Sstevel@tonic-gate * information up to date in the load object. 7240Sstevel@tonic-gate */ 7250Sstevel@tonic-gate (void) build_map_symtab(P, mptr); 7260Sstevel@tonic-gate 7270Sstevel@tonic-gate return (mptr->map_file->file_lo); 7280Sstevel@tonic-gate } 7290Sstevel@tonic-gate 7300Sstevel@tonic-gate const rd_loadobj_t * 7310Sstevel@tonic-gate Pname_to_loadobj(struct ps_prochandle *P, const char *name) 7320Sstevel@tonic-gate { 7330Sstevel@tonic-gate return (Plmid_to_loadobj(P, PR_LMID_EVERY, name)); 7340Sstevel@tonic-gate } 7350Sstevel@tonic-gate 7360Sstevel@tonic-gate ctf_file_t * 7370Sstevel@tonic-gate Pbuild_file_ctf(struct ps_prochandle *P, file_info_t *fptr) 7380Sstevel@tonic-gate { 7390Sstevel@tonic-gate ctf_sect_t ctdata, symtab, strtab; 7400Sstevel@tonic-gate sym_tbl_t *symp; 7410Sstevel@tonic-gate int err; 7420Sstevel@tonic-gate 7430Sstevel@tonic-gate if (fptr->file_ctfp != NULL) 7440Sstevel@tonic-gate return (fptr->file_ctfp); 7450Sstevel@tonic-gate 7460Sstevel@tonic-gate Pbuild_file_symtab(P, fptr); 7470Sstevel@tonic-gate 7480Sstevel@tonic-gate if (fptr->file_ctf_size == 0) 7490Sstevel@tonic-gate return (NULL); 7500Sstevel@tonic-gate 7510Sstevel@tonic-gate symp = fptr->file_ctf_dyn ? &fptr->file_dynsym : &fptr->file_symtab; 7523347Sab196087 if (symp->sym_data_pri == NULL) 7530Sstevel@tonic-gate return (NULL); 7540Sstevel@tonic-gate 7550Sstevel@tonic-gate /* 7560Sstevel@tonic-gate * The buffer may alread be allocated if this is a core file that 7570Sstevel@tonic-gate * contained CTF data for this file. 7580Sstevel@tonic-gate */ 7590Sstevel@tonic-gate if (fptr->file_ctf_buf == NULL) { 7600Sstevel@tonic-gate fptr->file_ctf_buf = malloc(fptr->file_ctf_size); 7610Sstevel@tonic-gate if (fptr->file_ctf_buf == NULL) { 7620Sstevel@tonic-gate dprintf("failed to allocate ctf buffer\n"); 7630Sstevel@tonic-gate return (NULL); 7640Sstevel@tonic-gate } 7650Sstevel@tonic-gate 7660Sstevel@tonic-gate if (pread(fptr->file_fd, fptr->file_ctf_buf, 7670Sstevel@tonic-gate fptr->file_ctf_size, fptr->file_ctf_off) != 7680Sstevel@tonic-gate fptr->file_ctf_size) { 7690Sstevel@tonic-gate free(fptr->file_ctf_buf); 7700Sstevel@tonic-gate fptr->file_ctf_buf = NULL; 7710Sstevel@tonic-gate dprintf("failed to read ctf data\n"); 7720Sstevel@tonic-gate return (NULL); 7730Sstevel@tonic-gate } 7740Sstevel@tonic-gate } 7750Sstevel@tonic-gate 7760Sstevel@tonic-gate ctdata.cts_name = ".SUNW_ctf"; 7770Sstevel@tonic-gate ctdata.cts_type = SHT_PROGBITS; 7780Sstevel@tonic-gate ctdata.cts_flags = 0; 7790Sstevel@tonic-gate ctdata.cts_data = fptr->file_ctf_buf; 7800Sstevel@tonic-gate ctdata.cts_size = fptr->file_ctf_size; 7810Sstevel@tonic-gate ctdata.cts_entsize = 1; 7820Sstevel@tonic-gate ctdata.cts_offset = 0; 7830Sstevel@tonic-gate 7840Sstevel@tonic-gate symtab.cts_name = fptr->file_ctf_dyn ? ".dynsym" : ".symtab"; 7853347Sab196087 symtab.cts_type = symp->sym_hdr_pri.sh_type; 7863347Sab196087 symtab.cts_flags = symp->sym_hdr_pri.sh_flags; 7873347Sab196087 symtab.cts_data = symp->sym_data_pri->d_buf; 7883347Sab196087 symtab.cts_size = symp->sym_hdr_pri.sh_size; 7893347Sab196087 symtab.cts_entsize = symp->sym_hdr_pri.sh_entsize; 7903347Sab196087 symtab.cts_offset = symp->sym_hdr_pri.sh_offset; 7910Sstevel@tonic-gate 7920Sstevel@tonic-gate strtab.cts_name = fptr->file_ctf_dyn ? ".dynstr" : ".strtab"; 7930Sstevel@tonic-gate strtab.cts_type = symp->sym_strhdr.sh_type; 7940Sstevel@tonic-gate strtab.cts_flags = symp->sym_strhdr.sh_flags; 7950Sstevel@tonic-gate strtab.cts_data = symp->sym_strs; 7960Sstevel@tonic-gate strtab.cts_size = symp->sym_strhdr.sh_size; 7970Sstevel@tonic-gate strtab.cts_entsize = symp->sym_strhdr.sh_entsize; 7980Sstevel@tonic-gate strtab.cts_offset = symp->sym_strhdr.sh_offset; 7990Sstevel@tonic-gate 8000Sstevel@tonic-gate fptr->file_ctfp = ctf_bufopen(&ctdata, &symtab, &strtab, &err); 8010Sstevel@tonic-gate if (fptr->file_ctfp == NULL) { 8020Sstevel@tonic-gate free(fptr->file_ctf_buf); 8030Sstevel@tonic-gate fptr->file_ctf_buf = NULL; 8040Sstevel@tonic-gate return (NULL); 8050Sstevel@tonic-gate } 8060Sstevel@tonic-gate 8070Sstevel@tonic-gate dprintf("loaded %lu bytes of CTF data for %s\n", 8080Sstevel@tonic-gate (ulong_t)fptr->file_ctf_size, fptr->file_pname); 8090Sstevel@tonic-gate 8100Sstevel@tonic-gate return (fptr->file_ctfp); 8110Sstevel@tonic-gate } 8120Sstevel@tonic-gate 8130Sstevel@tonic-gate ctf_file_t * 8140Sstevel@tonic-gate Paddr_to_ctf(struct ps_prochandle *P, uintptr_t addr) 8150Sstevel@tonic-gate { 8160Sstevel@tonic-gate map_info_t *mptr; 8170Sstevel@tonic-gate file_info_t *fptr; 8180Sstevel@tonic-gate 8190Sstevel@tonic-gate if (!P->info_valid) 8200Sstevel@tonic-gate Pupdate_maps(P); 8210Sstevel@tonic-gate 8220Sstevel@tonic-gate if ((mptr = Paddr2mptr(P, addr)) == NULL || 8230Sstevel@tonic-gate (fptr = mptr->map_file) == NULL) 8240Sstevel@tonic-gate return (NULL); 8250Sstevel@tonic-gate 8260Sstevel@tonic-gate return (Pbuild_file_ctf(P, fptr)); 8270Sstevel@tonic-gate } 8280Sstevel@tonic-gate 8290Sstevel@tonic-gate ctf_file_t * 8300Sstevel@tonic-gate Plmid_to_ctf(struct ps_prochandle *P, Lmid_t lmid, const char *name) 8310Sstevel@tonic-gate { 8320Sstevel@tonic-gate map_info_t *mptr; 8330Sstevel@tonic-gate file_info_t *fptr; 8340Sstevel@tonic-gate 8350Sstevel@tonic-gate if (name == PR_OBJ_EVERY) 8360Sstevel@tonic-gate return (NULL); 8370Sstevel@tonic-gate 8380Sstevel@tonic-gate if ((mptr = object_name_to_map(P, lmid, name)) == NULL || 8390Sstevel@tonic-gate (fptr = mptr->map_file) == NULL) 8400Sstevel@tonic-gate return (NULL); 8410Sstevel@tonic-gate 8420Sstevel@tonic-gate return (Pbuild_file_ctf(P, fptr)); 8430Sstevel@tonic-gate } 8440Sstevel@tonic-gate 8450Sstevel@tonic-gate ctf_file_t * 8460Sstevel@tonic-gate Pname_to_ctf(struct ps_prochandle *P, const char *name) 8470Sstevel@tonic-gate { 8480Sstevel@tonic-gate return (Plmid_to_ctf(P, PR_LMID_EVERY, name)); 8490Sstevel@tonic-gate } 8500Sstevel@tonic-gate 8510Sstevel@tonic-gate /* 8520Sstevel@tonic-gate * If we're not a core file, re-read the /proc/<pid>/auxv file and store 8530Sstevel@tonic-gate * its contents in P->auxv. In the case of a core file, we either 8540Sstevel@tonic-gate * initialized P->auxv in Pcore() from the NT_AUXV, or we don't have an 8550Sstevel@tonic-gate * auxv because the note was missing. 8560Sstevel@tonic-gate */ 8570Sstevel@tonic-gate void 8580Sstevel@tonic-gate Preadauxvec(struct ps_prochandle *P) 8590Sstevel@tonic-gate { 8600Sstevel@tonic-gate char auxfile[64]; 8610Sstevel@tonic-gate struct stat statb; 8620Sstevel@tonic-gate ssize_t naux; 8630Sstevel@tonic-gate int fd; 8640Sstevel@tonic-gate 8650Sstevel@tonic-gate if (P->state == PS_DEAD) 8660Sstevel@tonic-gate return; /* Already read during Pgrab_core() */ 8670Sstevel@tonic-gate if (P->state == PS_IDLE) 8680Sstevel@tonic-gate return; /* No aux vec for Pgrab_file() */ 8690Sstevel@tonic-gate 8700Sstevel@tonic-gate if (P->auxv != NULL) { 8710Sstevel@tonic-gate free(P->auxv); 8720Sstevel@tonic-gate P->auxv = NULL; 8730Sstevel@tonic-gate P->nauxv = 0; 8740Sstevel@tonic-gate } 8750Sstevel@tonic-gate 8762712Snn35248 (void) snprintf(auxfile, sizeof (auxfile), "%s/%d/auxv", 8772712Snn35248 procfs_path, (int)P->pid); 8780Sstevel@tonic-gate if ((fd = open(auxfile, O_RDONLY)) < 0) 8790Sstevel@tonic-gate return; 8800Sstevel@tonic-gate 8810Sstevel@tonic-gate if (fstat(fd, &statb) == 0 && 8820Sstevel@tonic-gate statb.st_size >= sizeof (auxv_t) && 8830Sstevel@tonic-gate (P->auxv = malloc(statb.st_size + sizeof (auxv_t))) != NULL) { 8840Sstevel@tonic-gate if ((naux = read(fd, P->auxv, statb.st_size)) < 0 || 8850Sstevel@tonic-gate (naux /= sizeof (auxv_t)) < 1) { 8860Sstevel@tonic-gate free(P->auxv); 8870Sstevel@tonic-gate P->auxv = NULL; 8880Sstevel@tonic-gate } else { 8890Sstevel@tonic-gate P->auxv[naux].a_type = AT_NULL; 8900Sstevel@tonic-gate P->auxv[naux].a_un.a_val = 0L; 8910Sstevel@tonic-gate P->nauxv = (int)naux; 8920Sstevel@tonic-gate } 8930Sstevel@tonic-gate } 8940Sstevel@tonic-gate 8950Sstevel@tonic-gate (void) close(fd); 8960Sstevel@tonic-gate } 8970Sstevel@tonic-gate 8980Sstevel@tonic-gate /* 8990Sstevel@tonic-gate * Return a requested element from the process's aux vector. 9000Sstevel@tonic-gate * Return -1 on failure (this is adequate for our purposes). 9010Sstevel@tonic-gate */ 9020Sstevel@tonic-gate long 9030Sstevel@tonic-gate Pgetauxval(struct ps_prochandle *P, int type) 9040Sstevel@tonic-gate { 9050Sstevel@tonic-gate auxv_t *auxv; 9060Sstevel@tonic-gate 9070Sstevel@tonic-gate if (P->auxv == NULL) 9080Sstevel@tonic-gate Preadauxvec(P); 9090Sstevel@tonic-gate 9100Sstevel@tonic-gate if (P->auxv == NULL) 9110Sstevel@tonic-gate return (-1); 9120Sstevel@tonic-gate 9130Sstevel@tonic-gate for (auxv = P->auxv; auxv->a_type != AT_NULL; auxv++) { 9140Sstevel@tonic-gate if (auxv->a_type == type) 9150Sstevel@tonic-gate return (auxv->a_un.a_val); 9160Sstevel@tonic-gate } 9170Sstevel@tonic-gate 9180Sstevel@tonic-gate return (-1); 9190Sstevel@tonic-gate } 9200Sstevel@tonic-gate 9210Sstevel@tonic-gate /* 9220Sstevel@tonic-gate * Return a pointer to our internal copy of the process's aux vector. 9230Sstevel@tonic-gate * The caller should not hold on to this pointer across any libproc calls. 9240Sstevel@tonic-gate */ 9250Sstevel@tonic-gate const auxv_t * 9260Sstevel@tonic-gate Pgetauxvec(struct ps_prochandle *P) 9270Sstevel@tonic-gate { 9280Sstevel@tonic-gate static const auxv_t empty = { AT_NULL, 0L }; 9290Sstevel@tonic-gate 9300Sstevel@tonic-gate if (P->auxv == NULL) 9310Sstevel@tonic-gate Preadauxvec(P); 9320Sstevel@tonic-gate 9330Sstevel@tonic-gate if (P->auxv == NULL) 9340Sstevel@tonic-gate return (&empty); 9350Sstevel@tonic-gate 9360Sstevel@tonic-gate return (P->auxv); 9370Sstevel@tonic-gate } 9380Sstevel@tonic-gate 9390Sstevel@tonic-gate /* 9402915Srh87107 * Return 1 if the given mapping corresponds to the given file_info_t's 9412915Srh87107 * load object; return 0 otherwise. 9422915Srh87107 */ 9432915Srh87107 static int 9442915Srh87107 is_mapping_in_file(struct ps_prochandle *P, map_info_t *mptr, file_info_t *fptr) 9452915Srh87107 { 9462915Srh87107 prmap_t *pmap = &mptr->map_pmap; 9472915Srh87107 rd_loadobj_t *lop = fptr->file_lo; 9482915Srh87107 uint_t i; 9494753Srh87107 uintptr_t mstart, mend, sstart, send; 9502915Srh87107 9512915Srh87107 /* 9522915Srh87107 * We can get for free the start address of the text and data 9532915Srh87107 * sections of the load object. Start by seeing if the mapping 9542915Srh87107 * encloses either of these. 9552915Srh87107 */ 9562915Srh87107 if ((pmap->pr_vaddr <= lop->rl_base && 9572915Srh87107 lop->rl_base < pmap->pr_vaddr + pmap->pr_size) || 9582915Srh87107 (pmap->pr_vaddr <= lop->rl_data_base && 9592915Srh87107 lop->rl_data_base < pmap->pr_vaddr + pmap->pr_size)) 9602915Srh87107 return (1); 9612915Srh87107 9622915Srh87107 /* 9632915Srh87107 * It's still possible that this mapping correponds to the load 9642915Srh87107 * object. Consider the example of a mapping whose start and end 9652915Srh87107 * addresses correspond to those of the load object's text section. 9662915Srh87107 * If the mapping splits, e.g. as a result of a segment demotion, 9672915Srh87107 * then although both mappings are still backed by the same section, 9682915Srh87107 * only one will be seen to enclose that section's start address. 9692915Srh87107 * Thus, to be rigorous, we ask not whether this mapping encloses 9702915Srh87107 * the start of a section, but whether there exists a section that 9714753Srh87107 * overlaps this mapping. 9722915Srh87107 * 9732915Srh87107 * If we don't already have the section addresses, and we successfully 9742915Srh87107 * get them, then we cache them in case we come here again. 9752915Srh87107 */ 9762915Srh87107 if (fptr->file_saddrs == NULL && 9772915Srh87107 (fptr->file_saddrs = get_saddrs(P, 9782915Srh87107 fptr->file_map->map_pmap.pr_vaddr, &fptr->file_nsaddrs)) == NULL) 9792915Srh87107 return (0); 9804753Srh87107 9814753Srh87107 mstart = mptr->map_pmap.pr_vaddr; 9824753Srh87107 mend = mptr->map_pmap.pr_vaddr + mptr->map_pmap.pr_size; 9832915Srh87107 for (i = 0; i < fptr->file_nsaddrs; i += 2) { 9844753Srh87107 /* Does this section overlap the mapping? */ 9854753Srh87107 sstart = fptr->file_saddrs[i]; 9864753Srh87107 send = fptr->file_saddrs[i + 1]; 9874753Srh87107 if (!(mend <= sstart || mstart >= send)) 9882915Srh87107 return (1); 9892915Srh87107 } 9902915Srh87107 9912915Srh87107 return (0); 9922915Srh87107 } 9932915Srh87107 9942915Srh87107 /* 9950Sstevel@tonic-gate * Find or build the symbol table for the given mapping. 9960Sstevel@tonic-gate */ 9970Sstevel@tonic-gate static file_info_t * 9980Sstevel@tonic-gate build_map_symtab(struct ps_prochandle *P, map_info_t *mptr) 9990Sstevel@tonic-gate { 10000Sstevel@tonic-gate prmap_t *pmap = &mptr->map_pmap; 10010Sstevel@tonic-gate file_info_t *fptr; 10020Sstevel@tonic-gate uint_t i; 10030Sstevel@tonic-gate 10040Sstevel@tonic-gate if ((fptr = mptr->map_file) != NULL) { 10050Sstevel@tonic-gate Pbuild_file_symtab(P, fptr); 10060Sstevel@tonic-gate return (fptr); 10070Sstevel@tonic-gate } 10080Sstevel@tonic-gate 10090Sstevel@tonic-gate if (pmap->pr_mapname[0] == '\0') 10100Sstevel@tonic-gate return (NULL); 10110Sstevel@tonic-gate 10120Sstevel@tonic-gate /* 10130Sstevel@tonic-gate * Attempt to find a matching file. 10140Sstevel@tonic-gate * (A file can be mapped at several different addresses.) 10150Sstevel@tonic-gate */ 10160Sstevel@tonic-gate for (i = 0, fptr = list_next(&P->file_head); i < P->num_files; 10170Sstevel@tonic-gate i++, fptr = list_next(fptr)) { 10180Sstevel@tonic-gate if (strcmp(fptr->file_pname, pmap->pr_mapname) == 0 && 10192915Srh87107 fptr->file_lo && is_mapping_in_file(P, mptr, fptr)) { 10200Sstevel@tonic-gate mptr->map_file = fptr; 10210Sstevel@tonic-gate fptr->file_ref++; 10220Sstevel@tonic-gate Pbuild_file_symtab(P, fptr); 10230Sstevel@tonic-gate return (fptr); 10240Sstevel@tonic-gate } 10250Sstevel@tonic-gate } 10260Sstevel@tonic-gate 10270Sstevel@tonic-gate /* 10280Sstevel@tonic-gate * If we need to create a new file_info structure, iterate 10290Sstevel@tonic-gate * through the load objects in order to attempt to connect 10300Sstevel@tonic-gate * this new file with its primary text mapping. We again 10310Sstevel@tonic-gate * need to handle ld.so as a special case because we need 10320Sstevel@tonic-gate * to be able to bootstrap librtld_db. 10330Sstevel@tonic-gate */ 10340Sstevel@tonic-gate if ((fptr = file_info_new(P, mptr)) == NULL) 10350Sstevel@tonic-gate return (NULL); 10360Sstevel@tonic-gate 10370Sstevel@tonic-gate if (P->map_ldso != mptr) { 10380Sstevel@tonic-gate if (P->rap != NULL) 10390Sstevel@tonic-gate (void) rd_loadobj_iter(P->rap, map_iter, P); 10400Sstevel@tonic-gate else 10410Sstevel@tonic-gate (void) Prd_agent(P); 10420Sstevel@tonic-gate } else { 10430Sstevel@tonic-gate fptr->file_map = mptr; 10440Sstevel@tonic-gate } 10450Sstevel@tonic-gate 10460Sstevel@tonic-gate /* 10470Sstevel@tonic-gate * If librtld_db wasn't able to help us connect the file to a primary 10480Sstevel@tonic-gate * text mapping, set file_map to the current mapping because we require 10490Sstevel@tonic-gate * fptr->file_map to be set in Pbuild_file_symtab. librtld_db may be 10500Sstevel@tonic-gate * unaware of what's going on in the rare case that a legitimate ELF 10510Sstevel@tonic-gate * file has been mmap(2)ed into the process address space *without* 10522712Snn35248 * the use of dlopen(3x). 10530Sstevel@tonic-gate */ 10540Sstevel@tonic-gate if (fptr->file_map == NULL) 10550Sstevel@tonic-gate fptr->file_map = mptr; 10560Sstevel@tonic-gate 10570Sstevel@tonic-gate Pbuild_file_symtab(P, fptr); 10580Sstevel@tonic-gate 10590Sstevel@tonic-gate return (fptr); 10600Sstevel@tonic-gate } 10610Sstevel@tonic-gate 10620Sstevel@tonic-gate static int 1063942Sahl read_ehdr32(struct ps_prochandle *P, Elf32_Ehdr *ehdr, uint_t *phnum, 1064942Sahl uintptr_t addr) 10650Sstevel@tonic-gate { 10660Sstevel@tonic-gate if (Pread(P, ehdr, sizeof (*ehdr), addr) != sizeof (*ehdr)) 10670Sstevel@tonic-gate return (-1); 10680Sstevel@tonic-gate 10690Sstevel@tonic-gate if (ehdr->e_ident[EI_MAG0] != ELFMAG0 || 10700Sstevel@tonic-gate ehdr->e_ident[EI_MAG1] != ELFMAG1 || 10710Sstevel@tonic-gate ehdr->e_ident[EI_MAG2] != ELFMAG2 || 10720Sstevel@tonic-gate ehdr->e_ident[EI_MAG3] != ELFMAG3 || 10730Sstevel@tonic-gate ehdr->e_ident[EI_CLASS] != ELFCLASS32 || 10740Sstevel@tonic-gate #ifdef _BIG_ENDIAN 10750Sstevel@tonic-gate ehdr->e_ident[EI_DATA] != ELFDATA2MSB || 10760Sstevel@tonic-gate #else 10770Sstevel@tonic-gate ehdr->e_ident[EI_DATA] != ELFDATA2LSB || 10780Sstevel@tonic-gate #endif 10790Sstevel@tonic-gate ehdr->e_ident[EI_VERSION] != EV_CURRENT) 10800Sstevel@tonic-gate return (-1); 10810Sstevel@tonic-gate 1082942Sahl if ((*phnum = ehdr->e_phnum) == PN_XNUM) { 1083942Sahl Elf32_Shdr shdr0; 1084942Sahl 1085942Sahl if (ehdr->e_shoff == 0 || ehdr->e_shentsize < sizeof (shdr0) || 1086942Sahl Pread(P, &shdr0, sizeof (shdr0), addr + ehdr->e_shoff) != 1087942Sahl sizeof (shdr0)) 1088942Sahl return (-1); 1089942Sahl 1090942Sahl if (shdr0.sh_info != 0) 1091942Sahl *phnum = shdr0.sh_info; 1092942Sahl } 1093942Sahl 10940Sstevel@tonic-gate return (0); 10950Sstevel@tonic-gate } 10960Sstevel@tonic-gate 10970Sstevel@tonic-gate static int 10980Sstevel@tonic-gate read_dynamic_phdr32(struct ps_prochandle *P, const Elf32_Ehdr *ehdr, 1099942Sahl uint_t phnum, Elf32_Phdr *phdr, uintptr_t addr) 11000Sstevel@tonic-gate { 11010Sstevel@tonic-gate uint_t i; 11020Sstevel@tonic-gate 1103942Sahl for (i = 0; i < phnum; i++) { 11040Sstevel@tonic-gate uintptr_t a = addr + ehdr->e_phoff + i * ehdr->e_phentsize; 11050Sstevel@tonic-gate if (Pread(P, phdr, sizeof (*phdr), a) != sizeof (*phdr)) 11060Sstevel@tonic-gate return (-1); 11070Sstevel@tonic-gate 11080Sstevel@tonic-gate if (phdr->p_type == PT_DYNAMIC) 11090Sstevel@tonic-gate return (0); 11100Sstevel@tonic-gate } 11110Sstevel@tonic-gate 11120Sstevel@tonic-gate return (-1); 11130Sstevel@tonic-gate } 11140Sstevel@tonic-gate 11150Sstevel@tonic-gate #ifdef _LP64 11160Sstevel@tonic-gate static int 1117942Sahl read_ehdr64(struct ps_prochandle *P, Elf64_Ehdr *ehdr, uint_t *phnum, 1118942Sahl uintptr_t addr) 11190Sstevel@tonic-gate { 11200Sstevel@tonic-gate if (Pread(P, ehdr, sizeof (Elf64_Ehdr), addr) != sizeof (Elf64_Ehdr)) 11210Sstevel@tonic-gate return (-1); 11220Sstevel@tonic-gate 11230Sstevel@tonic-gate if (ehdr->e_ident[EI_MAG0] != ELFMAG0 || 11240Sstevel@tonic-gate ehdr->e_ident[EI_MAG1] != ELFMAG1 || 11250Sstevel@tonic-gate ehdr->e_ident[EI_MAG2] != ELFMAG2 || 11260Sstevel@tonic-gate ehdr->e_ident[EI_MAG3] != ELFMAG3 || 11270Sstevel@tonic-gate ehdr->e_ident[EI_CLASS] != ELFCLASS64 || 11280Sstevel@tonic-gate #ifdef _BIG_ENDIAN 11290Sstevel@tonic-gate ehdr->e_ident[EI_DATA] != ELFDATA2MSB || 11300Sstevel@tonic-gate #else 11310Sstevel@tonic-gate ehdr->e_ident[EI_DATA] != ELFDATA2LSB || 11320Sstevel@tonic-gate #endif 11330Sstevel@tonic-gate ehdr->e_ident[EI_VERSION] != EV_CURRENT) 11340Sstevel@tonic-gate return (-1); 11350Sstevel@tonic-gate 1136942Sahl if ((*phnum = ehdr->e_phnum) == PN_XNUM) { 1137942Sahl Elf64_Shdr shdr0; 1138942Sahl 1139942Sahl if (ehdr->e_shoff == 0 || ehdr->e_shentsize < sizeof (shdr0) || 1140942Sahl Pread(P, &shdr0, sizeof (shdr0), addr + ehdr->e_shoff) != 1141942Sahl sizeof (shdr0)) 1142942Sahl return (-1); 1143942Sahl 1144942Sahl if (shdr0.sh_info != 0) 1145942Sahl *phnum = shdr0.sh_info; 1146942Sahl } 1147942Sahl 11480Sstevel@tonic-gate return (0); 11490Sstevel@tonic-gate } 11500Sstevel@tonic-gate 11510Sstevel@tonic-gate static int 11520Sstevel@tonic-gate read_dynamic_phdr64(struct ps_prochandle *P, const Elf64_Ehdr *ehdr, 1153942Sahl uint_t phnum, Elf64_Phdr *phdr, uintptr_t addr) 11540Sstevel@tonic-gate { 11550Sstevel@tonic-gate uint_t i; 11560Sstevel@tonic-gate 1157942Sahl for (i = 0; i < phnum; i++) { 11580Sstevel@tonic-gate uintptr_t a = addr + ehdr->e_phoff + i * ehdr->e_phentsize; 11590Sstevel@tonic-gate if (Pread(P, phdr, sizeof (*phdr), a) != sizeof (*phdr)) 11600Sstevel@tonic-gate return (-1); 11610Sstevel@tonic-gate 11620Sstevel@tonic-gate if (phdr->p_type == PT_DYNAMIC) 11630Sstevel@tonic-gate return (0); 11640Sstevel@tonic-gate } 11650Sstevel@tonic-gate 11660Sstevel@tonic-gate return (-1); 11670Sstevel@tonic-gate } 11680Sstevel@tonic-gate #endif /* _LP64 */ 11690Sstevel@tonic-gate 11700Sstevel@tonic-gate /* 11710Sstevel@tonic-gate * The text segment for each load object contains the elf header and 11720Sstevel@tonic-gate * program headers. We can use this information to determine if the 11730Sstevel@tonic-gate * file that corresponds to the load object is the same file that 11740Sstevel@tonic-gate * was loaded into the process's address space. There can be a discrepency 11750Sstevel@tonic-gate * if a file is recompiled after the process is started or if the target 11760Sstevel@tonic-gate * represents a core file from a differently configured system -- two 11770Sstevel@tonic-gate * common examples. The DT_CHECKSUM entry in the dynamic section 11780Sstevel@tonic-gate * provides an easy method of comparison. It is important to note that 11790Sstevel@tonic-gate * the dynamic section usually lives in the data segment, but the meta 11800Sstevel@tonic-gate * data we use to find the dynamic section lives in the text segment so 11810Sstevel@tonic-gate * if either of those segments is absent we can't proceed. 11820Sstevel@tonic-gate * 11830Sstevel@tonic-gate * We're looking through the elf file for several items: the symbol tables 11840Sstevel@tonic-gate * (both dynsym and symtab), the procedure linkage table (PLT) base, 11850Sstevel@tonic-gate * size, and relocation base, and the CTF information. Most of this can 11860Sstevel@tonic-gate * be recovered from the loaded image of the file itself, the exceptions 11870Sstevel@tonic-gate * being the symtab and CTF data. 11880Sstevel@tonic-gate * 11890Sstevel@tonic-gate * First we try to open the file that we think corresponds to the load 11900Sstevel@tonic-gate * object, if the DT_CHECKSUM values match, we're all set, and can simply 11910Sstevel@tonic-gate * recover all the information we need from the file. If the values of 11920Sstevel@tonic-gate * DT_CHECKSUM don't match, or if we can't access the file for whatever 11930Sstevel@tonic-gate * reasaon, we fake up a elf file to use in its stead. If we can't read 11940Sstevel@tonic-gate * the elf data in the process's address space, we fall back to using 11950Sstevel@tonic-gate * the file even though it may give inaccurate information. 11960Sstevel@tonic-gate * 11970Sstevel@tonic-gate * The elf file that we fake up has to consist of sections for the 11980Sstevel@tonic-gate * dynsym, the PLT and the dynamic section. Note that in the case of a 11990Sstevel@tonic-gate * core file, we'll get the CTF data in the file_info_t later on from 12000Sstevel@tonic-gate * a section embedded the core file (if it's present). 12010Sstevel@tonic-gate * 12020Sstevel@tonic-gate * file_differs() conservatively looks for mismatched files, identifying 12030Sstevel@tonic-gate * a match when there is any ambiguity (since that's the legacy behavior). 12040Sstevel@tonic-gate */ 12050Sstevel@tonic-gate static int 12060Sstevel@tonic-gate file_differs(struct ps_prochandle *P, Elf *elf, file_info_t *fptr) 12070Sstevel@tonic-gate { 12080Sstevel@tonic-gate Elf_Scn *scn; 12090Sstevel@tonic-gate GElf_Shdr shdr; 12100Sstevel@tonic-gate GElf_Dyn dyn; 12110Sstevel@tonic-gate Elf_Data *data; 12120Sstevel@tonic-gate uint_t i, ndyn; 12130Sstevel@tonic-gate GElf_Xword cksum; 12140Sstevel@tonic-gate uintptr_t addr; 12150Sstevel@tonic-gate 12160Sstevel@tonic-gate if (fptr->file_map == NULL) 12170Sstevel@tonic-gate return (0); 12180Sstevel@tonic-gate 12190Sstevel@tonic-gate if ((Pcontent(P) & (CC_CONTENT_TEXT | CC_CONTENT_DATA)) != 12200Sstevel@tonic-gate (CC_CONTENT_TEXT | CC_CONTENT_DATA)) 12210Sstevel@tonic-gate return (0); 12220Sstevel@tonic-gate 12230Sstevel@tonic-gate /* 12240Sstevel@tonic-gate * First, we find the checksum value in the elf file. 12250Sstevel@tonic-gate */ 12260Sstevel@tonic-gate scn = NULL; 12270Sstevel@tonic-gate while ((scn = elf_nextscn(elf, scn)) != NULL) { 12280Sstevel@tonic-gate if (gelf_getshdr(scn, &shdr) != NULL && 12290Sstevel@tonic-gate shdr.sh_type == SHT_DYNAMIC) 12300Sstevel@tonic-gate goto found_shdr; 12310Sstevel@tonic-gate } 12320Sstevel@tonic-gate return (0); 12330Sstevel@tonic-gate 12340Sstevel@tonic-gate found_shdr: 12350Sstevel@tonic-gate if ((data = elf_getdata(scn, NULL)) == NULL) 12360Sstevel@tonic-gate return (0); 12370Sstevel@tonic-gate 12380Sstevel@tonic-gate if (P->status.pr_dmodel == PR_MODEL_ILP32) 12390Sstevel@tonic-gate ndyn = shdr.sh_size / sizeof (Elf32_Dyn); 12400Sstevel@tonic-gate #ifdef _LP64 12410Sstevel@tonic-gate else if (P->status.pr_dmodel == PR_MODEL_LP64) 12420Sstevel@tonic-gate ndyn = shdr.sh_size / sizeof (Elf64_Dyn); 12430Sstevel@tonic-gate #endif 12440Sstevel@tonic-gate else 12450Sstevel@tonic-gate return (0); 12460Sstevel@tonic-gate 12470Sstevel@tonic-gate for (i = 0; i < ndyn; i++) { 12480Sstevel@tonic-gate if (gelf_getdyn(data, i, &dyn) != NULL && 12490Sstevel@tonic-gate dyn.d_tag == DT_CHECKSUM) 12500Sstevel@tonic-gate goto found_cksum; 12510Sstevel@tonic-gate } 12522712Snn35248 12532712Snn35248 /* 12542712Snn35248 * The in-memory ELF has no DT_CHECKSUM section, but we will report it 12552712Snn35248 * as matching the file anyhow. 12562712Snn35248 */ 12570Sstevel@tonic-gate return (0); 12580Sstevel@tonic-gate 12590Sstevel@tonic-gate found_cksum: 12600Sstevel@tonic-gate cksum = dyn.d_un.d_val; 12610Sstevel@tonic-gate dprintf("elf cksum value is %llx\n", (u_longlong_t)cksum); 12620Sstevel@tonic-gate 12630Sstevel@tonic-gate /* 12640Sstevel@tonic-gate * Get the base of the text mapping that corresponds to this file. 12650Sstevel@tonic-gate */ 12660Sstevel@tonic-gate addr = fptr->file_map->map_pmap.pr_vaddr; 12670Sstevel@tonic-gate 12680Sstevel@tonic-gate if (P->status.pr_dmodel == PR_MODEL_ILP32) { 12690Sstevel@tonic-gate Elf32_Ehdr ehdr; 12700Sstevel@tonic-gate Elf32_Phdr phdr; 12710Sstevel@tonic-gate Elf32_Dyn dync, *dynp; 1272942Sahl uint_t phnum, i; 1273942Sahl 1274942Sahl if (read_ehdr32(P, &ehdr, &phnum, addr) != 0 || 1275942Sahl read_dynamic_phdr32(P, &ehdr, phnum, &phdr, addr) != 0) 12760Sstevel@tonic-gate return (0); 12770Sstevel@tonic-gate 12780Sstevel@tonic-gate if (ehdr.e_type == ET_DYN) 12790Sstevel@tonic-gate phdr.p_vaddr += addr; 12800Sstevel@tonic-gate if ((dynp = malloc(phdr.p_filesz)) == NULL) 12810Sstevel@tonic-gate return (0); 12820Sstevel@tonic-gate dync.d_tag = DT_NULL; 12830Sstevel@tonic-gate if (Pread(P, dynp, phdr.p_filesz, phdr.p_vaddr) != 12840Sstevel@tonic-gate phdr.p_filesz) { 12850Sstevel@tonic-gate free(dynp); 12860Sstevel@tonic-gate return (0); 12870Sstevel@tonic-gate } 12880Sstevel@tonic-gate 12890Sstevel@tonic-gate for (i = 0; i < phdr.p_filesz / sizeof (Elf32_Dyn); i++) { 12900Sstevel@tonic-gate if (dynp[i].d_tag == DT_CHECKSUM) 12910Sstevel@tonic-gate dync = dynp[i]; 12920Sstevel@tonic-gate } 12930Sstevel@tonic-gate 12940Sstevel@tonic-gate free(dynp); 12950Sstevel@tonic-gate 12960Sstevel@tonic-gate if (dync.d_tag != DT_CHECKSUM) 12970Sstevel@tonic-gate return (0); 12980Sstevel@tonic-gate 12990Sstevel@tonic-gate dprintf("image cksum value is %llx\n", 13000Sstevel@tonic-gate (u_longlong_t)dync.d_un.d_val); 13010Sstevel@tonic-gate return (dync.d_un.d_val != cksum); 13020Sstevel@tonic-gate #ifdef _LP64 13030Sstevel@tonic-gate } else if (P->status.pr_dmodel == PR_MODEL_LP64) { 13040Sstevel@tonic-gate Elf64_Ehdr ehdr; 13050Sstevel@tonic-gate Elf64_Phdr phdr; 13060Sstevel@tonic-gate Elf64_Dyn dync, *dynp; 1307942Sahl uint_t phnum, i; 1308942Sahl 1309942Sahl if (read_ehdr64(P, &ehdr, &phnum, addr) != 0 || 1310942Sahl read_dynamic_phdr64(P, &ehdr, phnum, &phdr, addr) != 0) 13110Sstevel@tonic-gate return (0); 13120Sstevel@tonic-gate 13130Sstevel@tonic-gate if (ehdr.e_type == ET_DYN) 13140Sstevel@tonic-gate phdr.p_vaddr += addr; 13150Sstevel@tonic-gate if ((dynp = malloc(phdr.p_filesz)) == NULL) 13160Sstevel@tonic-gate return (0); 13170Sstevel@tonic-gate dync.d_tag = DT_NULL; 13180Sstevel@tonic-gate if (Pread(P, dynp, phdr.p_filesz, phdr.p_vaddr) != 13190Sstevel@tonic-gate phdr.p_filesz) { 13200Sstevel@tonic-gate free(dynp); 13210Sstevel@tonic-gate return (0); 13220Sstevel@tonic-gate } 13230Sstevel@tonic-gate 13240Sstevel@tonic-gate for (i = 0; i < phdr.p_filesz / sizeof (Elf64_Dyn); i++) { 13250Sstevel@tonic-gate if (dynp[i].d_tag == DT_CHECKSUM) 13260Sstevel@tonic-gate dync = dynp[i]; 13270Sstevel@tonic-gate } 13280Sstevel@tonic-gate 13290Sstevel@tonic-gate free(dynp); 13300Sstevel@tonic-gate 13310Sstevel@tonic-gate if (dync.d_tag != DT_CHECKSUM) 13320Sstevel@tonic-gate return (0); 13330Sstevel@tonic-gate 13340Sstevel@tonic-gate dprintf("image cksum value is %llx\n", 13350Sstevel@tonic-gate (u_longlong_t)dync.d_un.d_val); 13360Sstevel@tonic-gate return (dync.d_un.d_val != cksum); 13370Sstevel@tonic-gate #endif /* _LP64 */ 13380Sstevel@tonic-gate } 13390Sstevel@tonic-gate 13400Sstevel@tonic-gate return (0); 13410Sstevel@tonic-gate } 13420Sstevel@tonic-gate 13433347Sab196087 /* 13443347Sab196087 * Read data from the specified process and construct an in memory 13453347Sab196087 * image of an ELF file that represents it well enough to let 13463347Sab196087 * us probe it for information. 13473347Sab196087 */ 13480Sstevel@tonic-gate static Elf * 13490Sstevel@tonic-gate fake_elf(struct ps_prochandle *P, file_info_t *fptr) 13500Sstevel@tonic-gate { 13513347Sab196087 Elf *elf; 13520Sstevel@tonic-gate uintptr_t addr; 13533347Sab196087 uint_t phnum; 13540Sstevel@tonic-gate 13550Sstevel@tonic-gate if (fptr->file_map == NULL) 13560Sstevel@tonic-gate return (NULL); 13570Sstevel@tonic-gate 13580Sstevel@tonic-gate if ((Pcontent(P) & (CC_CONTENT_TEXT | CC_CONTENT_DATA)) != 13590Sstevel@tonic-gate (CC_CONTENT_TEXT | CC_CONTENT_DATA)) 13600Sstevel@tonic-gate return (NULL); 13610Sstevel@tonic-gate 13620Sstevel@tonic-gate addr = fptr->file_map->map_pmap.pr_vaddr; 13630Sstevel@tonic-gate 13640Sstevel@tonic-gate if (P->status.pr_dmodel == PR_MODEL_ILP32) { 13653347Sab196087 Elf32_Ehdr ehdr; 13660Sstevel@tonic-gate Elf32_Phdr phdr; 13670Sstevel@tonic-gate 13682712Snn35248 if ((read_ehdr32(P, &ehdr, &phnum, addr) != 0) || 1369942Sahl read_dynamic_phdr32(P, &ehdr, phnum, &phdr, addr) != 0) 13700Sstevel@tonic-gate return (NULL); 13710Sstevel@tonic-gate 13723347Sab196087 elf = fake_elf32(P, fptr, addr, &ehdr, phnum, &phdr); 13730Sstevel@tonic-gate #ifdef _LP64 13743347Sab196087 } else { 13753347Sab196087 Elf64_Ehdr ehdr; 13760Sstevel@tonic-gate Elf64_Phdr phdr; 13770Sstevel@tonic-gate 1378942Sahl if (read_ehdr64(P, &ehdr, &phnum, addr) != 0 || 1379942Sahl read_dynamic_phdr64(P, &ehdr, phnum, &phdr, addr) != 0) 13800Sstevel@tonic-gate return (NULL); 13810Sstevel@tonic-gate 13823347Sab196087 elf = fake_elf64(P, fptr, addr, &ehdr, phnum, &phdr); 13833347Sab196087 #endif 13840Sstevel@tonic-gate } 13850Sstevel@tonic-gate 13860Sstevel@tonic-gate return (elf); 13870Sstevel@tonic-gate } 13880Sstevel@tonic-gate 13890Sstevel@tonic-gate /* 13900Sstevel@tonic-gate * We wouldn't need these if qsort(3C) took an argument for the callback... 13910Sstevel@tonic-gate */ 13920Sstevel@tonic-gate static mutex_t sort_mtx = DEFAULTMUTEX; 13930Sstevel@tonic-gate static char *sort_strs; 13940Sstevel@tonic-gate static GElf_Sym *sort_syms; 13950Sstevel@tonic-gate 13960Sstevel@tonic-gate int 13970Sstevel@tonic-gate byaddr_cmp_common(GElf_Sym *a, char *aname, GElf_Sym *b, char *bname) 13980Sstevel@tonic-gate { 13990Sstevel@tonic-gate if (a->st_value < b->st_value) 14000Sstevel@tonic-gate return (-1); 14010Sstevel@tonic-gate if (a->st_value > b->st_value) 14020Sstevel@tonic-gate return (1); 14030Sstevel@tonic-gate 14040Sstevel@tonic-gate /* 14050Sstevel@tonic-gate * Prefer the function to the non-function. 14060Sstevel@tonic-gate */ 14070Sstevel@tonic-gate if (GELF_ST_TYPE(a->st_info) != GELF_ST_TYPE(b->st_info)) { 14080Sstevel@tonic-gate if (GELF_ST_TYPE(a->st_info) == STT_FUNC) 14090Sstevel@tonic-gate return (-1); 14100Sstevel@tonic-gate if (GELF_ST_TYPE(b->st_info) == STT_FUNC) 14110Sstevel@tonic-gate return (1); 14120Sstevel@tonic-gate } 14130Sstevel@tonic-gate 14140Sstevel@tonic-gate /* 14150Sstevel@tonic-gate * Prefer the weak or strong global symbol to the local symbol. 14160Sstevel@tonic-gate */ 14170Sstevel@tonic-gate if (GELF_ST_BIND(a->st_info) != GELF_ST_BIND(b->st_info)) { 14180Sstevel@tonic-gate if (GELF_ST_BIND(b->st_info) == STB_LOCAL) 14190Sstevel@tonic-gate return (-1); 14200Sstevel@tonic-gate if (GELF_ST_BIND(a->st_info) == STB_LOCAL) 14210Sstevel@tonic-gate return (1); 14220Sstevel@tonic-gate } 14230Sstevel@tonic-gate 14240Sstevel@tonic-gate /* 14251880Sahl * Prefer the symbol that doesn't begin with a '$' since compilers and 14261880Sahl * other symbol generators often use it as a prefix. 14271880Sahl */ 14281880Sahl if (*bname == '$') 14291880Sahl return (-1); 14301880Sahl if (*aname == '$') 14311880Sahl return (1); 14321880Sahl 14331880Sahl /* 14340Sstevel@tonic-gate * Prefer the name with fewer leading underscores in the name. 14350Sstevel@tonic-gate */ 14360Sstevel@tonic-gate while (*aname == '_' && *bname == '_') { 14370Sstevel@tonic-gate aname++; 14380Sstevel@tonic-gate bname++; 14390Sstevel@tonic-gate } 14400Sstevel@tonic-gate 14410Sstevel@tonic-gate if (*bname == '_') 14420Sstevel@tonic-gate return (-1); 14430Sstevel@tonic-gate if (*aname == '_') 14440Sstevel@tonic-gate return (1); 14450Sstevel@tonic-gate 14460Sstevel@tonic-gate /* 14470Sstevel@tonic-gate * Prefer the symbol with the smaller size. 14480Sstevel@tonic-gate */ 14490Sstevel@tonic-gate if (a->st_size < b->st_size) 14500Sstevel@tonic-gate return (-1); 14510Sstevel@tonic-gate if (a->st_size > b->st_size) 14520Sstevel@tonic-gate return (1); 14530Sstevel@tonic-gate 14540Sstevel@tonic-gate /* 14550Sstevel@tonic-gate * All other factors being equal, fall back to lexicographic order. 14560Sstevel@tonic-gate */ 14570Sstevel@tonic-gate return (strcmp(aname, bname)); 14580Sstevel@tonic-gate } 14590Sstevel@tonic-gate 14600Sstevel@tonic-gate static int 14610Sstevel@tonic-gate byaddr_cmp(const void *aa, const void *bb) 14620Sstevel@tonic-gate { 14630Sstevel@tonic-gate GElf_Sym *a = &sort_syms[*(uint_t *)aa]; 14640Sstevel@tonic-gate GElf_Sym *b = &sort_syms[*(uint_t *)bb]; 14650Sstevel@tonic-gate char *aname = sort_strs + a->st_name; 14660Sstevel@tonic-gate char *bname = sort_strs + b->st_name; 14670Sstevel@tonic-gate 14680Sstevel@tonic-gate return (byaddr_cmp_common(a, aname, b, bname)); 14690Sstevel@tonic-gate } 14700Sstevel@tonic-gate 14710Sstevel@tonic-gate static int 14720Sstevel@tonic-gate byname_cmp(const void *aa, const void *bb) 14730Sstevel@tonic-gate { 14740Sstevel@tonic-gate GElf_Sym *a = &sort_syms[*(uint_t *)aa]; 14750Sstevel@tonic-gate GElf_Sym *b = &sort_syms[*(uint_t *)bb]; 14760Sstevel@tonic-gate char *aname = sort_strs + a->st_name; 14770Sstevel@tonic-gate char *bname = sort_strs + b->st_name; 14780Sstevel@tonic-gate 14790Sstevel@tonic-gate return (strcmp(aname, bname)); 14800Sstevel@tonic-gate } 14810Sstevel@tonic-gate 14823347Sab196087 /* 14833347Sab196087 * Given a symbol index, look up the corresponding symbol from the 14843347Sab196087 * given symbol table. 14853347Sab196087 * 14863347Sab196087 * This function allows the caller to treat the symbol table as a single 14873347Sab196087 * logical entity even though there may be 2 actual ELF symbol tables 14883347Sab196087 * involved. See the comments in Pcontrol.h for details. 14893347Sab196087 */ 14903347Sab196087 static GElf_Sym * 14913347Sab196087 symtab_getsym(sym_tbl_t *symtab, int ndx, GElf_Sym *dst) 14923347Sab196087 { 14933347Sab196087 /* If index is in range of primary symtab, look it up there */ 14943347Sab196087 if (ndx >= symtab->sym_symn_aux) { 14953347Sab196087 return (gelf_getsym(symtab->sym_data_pri, 14963347Sab196087 ndx - symtab->sym_symn_aux, dst)); 14973347Sab196087 } 14983347Sab196087 14993347Sab196087 /* Not in primary: Look it up in the auxiliary symtab */ 15003347Sab196087 return (gelf_getsym(symtab->sym_data_aux, ndx, dst)); 15013347Sab196087 } 15023347Sab196087 15030Sstevel@tonic-gate void 15040Sstevel@tonic-gate optimize_symtab(sym_tbl_t *symtab) 15050Sstevel@tonic-gate { 15060Sstevel@tonic-gate GElf_Sym *symp, *syms; 15070Sstevel@tonic-gate uint_t i, *indexa, *indexb; 15080Sstevel@tonic-gate size_t symn, strsz, count; 15090Sstevel@tonic-gate 15103347Sab196087 if (symtab == NULL || symtab->sym_data_pri == NULL || 15110Sstevel@tonic-gate symtab->sym_byaddr != NULL) 15120Sstevel@tonic-gate return; 15130Sstevel@tonic-gate 15140Sstevel@tonic-gate symn = symtab->sym_symn; 15150Sstevel@tonic-gate strsz = symtab->sym_strsz; 15160Sstevel@tonic-gate 15170Sstevel@tonic-gate symp = syms = malloc(sizeof (GElf_Sym) * symn); 15183347Sab196087 if (symp == NULL) { 15193347Sab196087 dprintf("optimize_symtab: failed to malloc symbol array"); 15203347Sab196087 return; 15213347Sab196087 } 15220Sstevel@tonic-gate 15230Sstevel@tonic-gate /* 15240Sstevel@tonic-gate * First record all the symbols into a table and count up the ones 15250Sstevel@tonic-gate * that we're interested in. We mark symbols as invalid by setting 15260Sstevel@tonic-gate * the st_name to an illegal value. 15270Sstevel@tonic-gate */ 15280Sstevel@tonic-gate for (i = 0, count = 0; i < symn; i++, symp++) { 15293347Sab196087 if (symtab_getsym(symtab, i, symp) != NULL && 15300Sstevel@tonic-gate symp->st_name < strsz && 15310Sstevel@tonic-gate IS_DATA_TYPE(GELF_ST_TYPE(symp->st_info))) 15320Sstevel@tonic-gate count++; 15330Sstevel@tonic-gate else 15340Sstevel@tonic-gate symp->st_name = strsz; 15350Sstevel@tonic-gate } 15360Sstevel@tonic-gate 15370Sstevel@tonic-gate /* 15380Sstevel@tonic-gate * Allocate sufficient space for both tables and populate them 15390Sstevel@tonic-gate * with the same symbols we just counted. 15400Sstevel@tonic-gate */ 15410Sstevel@tonic-gate symtab->sym_count = count; 15420Sstevel@tonic-gate indexa = symtab->sym_byaddr = calloc(sizeof (uint_t), count); 15430Sstevel@tonic-gate indexb = symtab->sym_byname = calloc(sizeof (uint_t), count); 15443347Sab196087 if (indexa == NULL || indexb == NULL) { 15453347Sab196087 dprintf( 15463347Sab196087 "optimize_symtab: failed to malloc symbol index arrays"); 15473347Sab196087 symtab->sym_count = 0; 15483347Sab196087 if (indexa != NULL) { /* First alloc succeeded. Free it */ 15493347Sab196087 free(indexa); 15503347Sab196087 symtab->sym_byaddr = NULL; 15513347Sab196087 } 15523347Sab196087 free(syms); 15533347Sab196087 return; 15543347Sab196087 } 15550Sstevel@tonic-gate for (i = 0, symp = syms; i < symn; i++, symp++) { 15560Sstevel@tonic-gate if (symp->st_name < strsz) 15570Sstevel@tonic-gate *indexa++ = *indexb++ = i; 15580Sstevel@tonic-gate } 15590Sstevel@tonic-gate 15600Sstevel@tonic-gate /* 15614753Srh87107 * Sort the two tables according to the appropriate criteria, 15624753Srh87107 * unless the user has overridden this behaviour. 15634753Srh87107 * 15644753Srh87107 * An example where we might not sort the tables is the relatively 15654753Srh87107 * unusual case of a process with very large symbol tables in which 15664753Srh87107 * we perform few lookups. In such a case the total time would be 15674753Srh87107 * dominated by the sort. It is difficult to determine a priori 15684753Srh87107 * how many lookups an arbitrary client will perform, and 15694753Srh87107 * hence whether the symbol tables should be sorted. We therefore 15704753Srh87107 * sort the tables by default, but provide the user with a 15714753Srh87107 * "chicken switch" in the form of the LIBPROC_NO_QSORT 15724753Srh87107 * environment variable. 15730Sstevel@tonic-gate */ 15744753Srh87107 if (!_libproc_no_qsort) { 15754753Srh87107 (void) mutex_lock(&sort_mtx); 15764753Srh87107 sort_strs = symtab->sym_strs; 15774753Srh87107 sort_syms = syms; 15784753Srh87107 15794753Srh87107 qsort(symtab->sym_byaddr, count, sizeof (uint_t), byaddr_cmp); 15804753Srh87107 qsort(symtab->sym_byname, count, sizeof (uint_t), byname_cmp); 15814753Srh87107 15824753Srh87107 sort_strs = NULL; 15834753Srh87107 sort_syms = NULL; 15844753Srh87107 (void) mutex_unlock(&sort_mtx); 15854753Srh87107 } 15860Sstevel@tonic-gate 15870Sstevel@tonic-gate free(syms); 15880Sstevel@tonic-gate } 15890Sstevel@tonic-gate 15900Sstevel@tonic-gate /* 15910Sstevel@tonic-gate * Build the symbol table for the given mapped file. 15920Sstevel@tonic-gate */ 15930Sstevel@tonic-gate void 15940Sstevel@tonic-gate Pbuild_file_symtab(struct ps_prochandle *P, file_info_t *fptr) 15950Sstevel@tonic-gate { 15960Sstevel@tonic-gate char objectfile[PATH_MAX]; 15970Sstevel@tonic-gate uint_t i; 15980Sstevel@tonic-gate 15990Sstevel@tonic-gate GElf_Ehdr ehdr; 16000Sstevel@tonic-gate GElf_Sym s; 16010Sstevel@tonic-gate 16020Sstevel@tonic-gate Elf_Data *shdata; 16030Sstevel@tonic-gate Elf_Scn *scn; 16040Sstevel@tonic-gate Elf *elf; 1605942Sahl size_t nshdrs, shstrndx; 16060Sstevel@tonic-gate 16070Sstevel@tonic-gate struct { 16080Sstevel@tonic-gate GElf_Shdr c_shdr; 16090Sstevel@tonic-gate Elf_Data *c_data; 16100Sstevel@tonic-gate const char *c_name; 16110Sstevel@tonic-gate } *cp, *cache = NULL, *dyn = NULL, *plt = NULL, *ctf = NULL; 16120Sstevel@tonic-gate 16130Sstevel@tonic-gate if (fptr->file_init) 16140Sstevel@tonic-gate return; /* We've already processed this file */ 16150Sstevel@tonic-gate 16160Sstevel@tonic-gate /* 16170Sstevel@tonic-gate * Mark the file_info struct as having the symbol table initialized 16180Sstevel@tonic-gate * even if we fail below. We tried once; we don't try again. 16190Sstevel@tonic-gate */ 16200Sstevel@tonic-gate fptr->file_init = 1; 16210Sstevel@tonic-gate 16220Sstevel@tonic-gate if (elf_version(EV_CURRENT) == EV_NONE) { 16230Sstevel@tonic-gate dprintf("libproc ELF version is more recent than libelf\n"); 16240Sstevel@tonic-gate return; 16250Sstevel@tonic-gate } 16260Sstevel@tonic-gate 16270Sstevel@tonic-gate if (P->state == PS_DEAD || P->state == PS_IDLE) { 1628*7675SEdward.Pilatowicz@Sun.COM char *name; 16290Sstevel@tonic-gate /* 16300Sstevel@tonic-gate * If we're a not live, we can't open files from the /proc 16310Sstevel@tonic-gate * object directory; we have only the mapping and file names 16320Sstevel@tonic-gate * to guide us. We prefer the file_lname, but need to handle 16330Sstevel@tonic-gate * the case of it being NULL in order to bootstrap: we first 16340Sstevel@tonic-gate * come here during rd_new() when the only information we have 16350Sstevel@tonic-gate * is interpreter name associated with the AT_BASE mapping. 1636*7675SEdward.Pilatowicz@Sun.COM * 1637*7675SEdward.Pilatowicz@Sun.COM * Also, if the zone associated with the core file seems 1638*7675SEdward.Pilatowicz@Sun.COM * to exists on this machine we'll try to open the object 1639*7675SEdward.Pilatowicz@Sun.COM * file within the zone. 16400Sstevel@tonic-gate */ 1641*7675SEdward.Pilatowicz@Sun.COM if (fptr->file_rname != NULL) 1642*7675SEdward.Pilatowicz@Sun.COM name = fptr->file_rname; 1643*7675SEdward.Pilatowicz@Sun.COM else if (fptr->file_lname != NULL) 1644*7675SEdward.Pilatowicz@Sun.COM name = fptr->file_lname; 1645*7675SEdward.Pilatowicz@Sun.COM else 1646*7675SEdward.Pilatowicz@Sun.COM name = fptr->file_pname; 1647*7675SEdward.Pilatowicz@Sun.COM (void) strlcpy(objectfile, name, sizeof (objectfile)); 16480Sstevel@tonic-gate } else { 16490Sstevel@tonic-gate (void) snprintf(objectfile, sizeof (objectfile), 16502712Snn35248 "%s/%d/object/%s", 16512712Snn35248 procfs_path, (int)P->pid, fptr->file_pname); 16520Sstevel@tonic-gate } 16530Sstevel@tonic-gate 16540Sstevel@tonic-gate /* 16550Sstevel@tonic-gate * Open the object file, create the elf file, and then get the elf 16560Sstevel@tonic-gate * header and .shstrtab data buffer so we can process sections by 16570Sstevel@tonic-gate * name. If anything goes wrong try to fake up an elf file from 16580Sstevel@tonic-gate * the in-core elf image. 16590Sstevel@tonic-gate */ 16600Sstevel@tonic-gate if ((fptr->file_fd = open(objectfile, O_RDONLY)) < 0) { 16610Sstevel@tonic-gate dprintf("Pbuild_file_symtab: failed to open %s: %s\n", 16620Sstevel@tonic-gate objectfile, strerror(errno)); 16630Sstevel@tonic-gate 16640Sstevel@tonic-gate if ((elf = fake_elf(P, fptr)) == NULL || 16650Sstevel@tonic-gate elf_kind(elf) != ELF_K_ELF || 16660Sstevel@tonic-gate gelf_getehdr(elf, &ehdr) == NULL || 1667942Sahl elf_getshnum(elf, &nshdrs) == 0 || 1668942Sahl elf_getshstrndx(elf, &shstrndx) == 0 || 1669942Sahl (scn = elf_getscn(elf, shstrndx)) == NULL || 16700Sstevel@tonic-gate (shdata = elf_getdata(scn, NULL)) == NULL) { 16710Sstevel@tonic-gate dprintf("failed to fake up ELF file\n"); 16720Sstevel@tonic-gate return; 16730Sstevel@tonic-gate } 16740Sstevel@tonic-gate 16750Sstevel@tonic-gate } else if ((elf = elf_begin(fptr->file_fd, ELF_C_READ, NULL)) == NULL || 16760Sstevel@tonic-gate elf_kind(elf) != ELF_K_ELF || 16770Sstevel@tonic-gate gelf_getehdr(elf, &ehdr) == NULL || 1678942Sahl elf_getshnum(elf, &nshdrs) == 0 || 1679942Sahl elf_getshstrndx(elf, &shstrndx) == 0 || 1680942Sahl (scn = elf_getscn(elf, shstrndx)) == NULL || 16810Sstevel@tonic-gate (shdata = elf_getdata(scn, NULL)) == NULL) { 16822712Snn35248 int err = elf_errno(); 16832712Snn35248 16840Sstevel@tonic-gate dprintf("failed to process ELF file %s: %s\n", 16852712Snn35248 objectfile, (err == 0) ? "<null>" : elf_errmsg(err)); 16860Sstevel@tonic-gate 16870Sstevel@tonic-gate if ((elf = fake_elf(P, fptr)) == NULL || 16880Sstevel@tonic-gate elf_kind(elf) != ELF_K_ELF || 16890Sstevel@tonic-gate gelf_getehdr(elf, &ehdr) == NULL || 1690942Sahl elf_getshnum(elf, &nshdrs) == 0 || 1691942Sahl elf_getshstrndx(elf, &shstrndx) == 0 || 1692942Sahl (scn = elf_getscn(elf, shstrndx)) == NULL || 16930Sstevel@tonic-gate (shdata = elf_getdata(scn, NULL)) == NULL) { 16940Sstevel@tonic-gate dprintf("failed to fake up ELF file\n"); 16950Sstevel@tonic-gate goto bad; 16960Sstevel@tonic-gate } 16970Sstevel@tonic-gate 16980Sstevel@tonic-gate } else if (file_differs(P, elf, fptr)) { 16990Sstevel@tonic-gate Elf *newelf; 17000Sstevel@tonic-gate 17010Sstevel@tonic-gate /* 17020Sstevel@tonic-gate * Before we get too excited about this elf file, we'll check 17030Sstevel@tonic-gate * its checksum value against the value we have in memory. If 17040Sstevel@tonic-gate * they don't agree, we try to fake up a new elf file and 17050Sstevel@tonic-gate * proceed with that instead. 17060Sstevel@tonic-gate */ 17070Sstevel@tonic-gate 17080Sstevel@tonic-gate dprintf("ELF file %s (%lx) doesn't match in-core image\n", 17090Sstevel@tonic-gate fptr->file_pname, 17100Sstevel@tonic-gate (ulong_t)fptr->file_map->map_pmap.pr_vaddr); 17110Sstevel@tonic-gate 17120Sstevel@tonic-gate if ((newelf = fake_elf(P, fptr)) == NULL || 17130Sstevel@tonic-gate elf_kind(newelf) != ELF_K_ELF || 17140Sstevel@tonic-gate gelf_getehdr(newelf, &ehdr) == NULL || 1715942Sahl elf_getshnum(newelf, &nshdrs) == 0 || 1716942Sahl elf_getshstrndx(newelf, &shstrndx) == 0 || 1717942Sahl (scn = elf_getscn(newelf, shstrndx)) == NULL || 17180Sstevel@tonic-gate (shdata = elf_getdata(scn, NULL)) == NULL) { 17190Sstevel@tonic-gate dprintf("failed to fake up ELF file\n"); 17200Sstevel@tonic-gate } else { 17210Sstevel@tonic-gate (void) elf_end(elf); 17220Sstevel@tonic-gate elf = newelf; 17230Sstevel@tonic-gate 17240Sstevel@tonic-gate dprintf("switched to faked up ELF file\n"); 17250Sstevel@tonic-gate } 17260Sstevel@tonic-gate } 17270Sstevel@tonic-gate 1728942Sahl if ((cache = malloc(nshdrs * sizeof (*cache))) == NULL) { 17290Sstevel@tonic-gate dprintf("failed to malloc section cache for %s\n", objectfile); 17300Sstevel@tonic-gate goto bad; 17310Sstevel@tonic-gate } 17320Sstevel@tonic-gate 17330Sstevel@tonic-gate dprintf("processing ELF file %s\n", objectfile); 17340Sstevel@tonic-gate fptr->file_class = ehdr.e_ident[EI_CLASS]; 17350Sstevel@tonic-gate fptr->file_etype = ehdr.e_type; 17360Sstevel@tonic-gate fptr->file_elf = elf; 17371160Svb160487 fptr->file_shstrs = shdata->d_buf; 17381160Svb160487 fptr->file_shstrsz = shdata->d_size; 17390Sstevel@tonic-gate 17400Sstevel@tonic-gate /* 17410Sstevel@tonic-gate * Iterate through each section, caching its section header, data 17420Sstevel@tonic-gate * pointer, and name. We use this for handling sh_link values below. 17430Sstevel@tonic-gate */ 17440Sstevel@tonic-gate for (cp = cache + 1, scn = NULL; scn = elf_nextscn(elf, scn); cp++) { 17452712Snn35248 if (gelf_getshdr(scn, &cp->c_shdr) == NULL) { 17462712Snn35248 dprintf("Pbuild_file_symtab: Failed to get section " 17472712Snn35248 "header\n"); 17480Sstevel@tonic-gate goto bad; /* Failed to get section header */ 17492712Snn35248 } 17502712Snn35248 17512712Snn35248 if ((cp->c_data = elf_getdata(scn, NULL)) == NULL) { 17522712Snn35248 dprintf("Pbuild_file_symtab: Failed to get section " 17532712Snn35248 "data\n"); 17540Sstevel@tonic-gate goto bad; /* Failed to get section data */ 17552712Snn35248 } 17562712Snn35248 17572712Snn35248 if (cp->c_shdr.sh_name >= shdata->d_size) { 17582712Snn35248 dprintf("Pbuild_file_symtab: corrupt section name"); 17590Sstevel@tonic-gate goto bad; /* Corrupt section name */ 17602712Snn35248 } 17610Sstevel@tonic-gate 17620Sstevel@tonic-gate cp->c_name = (const char *)shdata->d_buf + cp->c_shdr.sh_name; 17630Sstevel@tonic-gate } 17640Sstevel@tonic-gate 17650Sstevel@tonic-gate /* 17660Sstevel@tonic-gate * Now iterate through the section cache in order to locate info 17673347Sab196087 * for the .symtab, .dynsym, .SUNW_ldynsym, .dynamic, .plt, 17683347Sab196087 * and .SUNW_ctf sections: 17690Sstevel@tonic-gate */ 1770942Sahl for (i = 1, cp = cache + 1; i < nshdrs; i++, cp++) { 17710Sstevel@tonic-gate GElf_Shdr *shp = &cp->c_shdr; 17720Sstevel@tonic-gate 17730Sstevel@tonic-gate if (shp->sh_type == SHT_SYMTAB || shp->sh_type == SHT_DYNSYM) { 17740Sstevel@tonic-gate sym_tbl_t *symp = shp->sh_type == SHT_SYMTAB ? 17750Sstevel@tonic-gate &fptr->file_symtab : &fptr->file_dynsym; 17760Sstevel@tonic-gate /* 17770Sstevel@tonic-gate * It's possible that the we already got the symbol 17780Sstevel@tonic-gate * table from the core file itself. Either the file 17790Sstevel@tonic-gate * differs in which case our faked up elf file will 17800Sstevel@tonic-gate * only contain the dynsym (not the symtab) or the 17810Sstevel@tonic-gate * file matches in which case we'll just be replacing 17820Sstevel@tonic-gate * the symbol table we pulled out of the core file 17830Sstevel@tonic-gate * with an equivalent one. In either case, this 17840Sstevel@tonic-gate * check isn't essential, but it's a good idea. 17850Sstevel@tonic-gate */ 17863347Sab196087 if (symp->sym_data_pri == NULL) { 17872712Snn35248 dprintf("Symbol table found for %s\n", 17882712Snn35248 objectfile); 17893347Sab196087 symp->sym_data_pri = cp->c_data; 17903347Sab196087 symp->sym_symn += 17913347Sab196087 shp->sh_size / shp->sh_entsize; 17920Sstevel@tonic-gate symp->sym_strs = 17930Sstevel@tonic-gate cache[shp->sh_link].c_data->d_buf; 17940Sstevel@tonic-gate symp->sym_strsz = 17950Sstevel@tonic-gate cache[shp->sh_link].c_data->d_size; 17963347Sab196087 symp->sym_hdr_pri = cp->c_shdr; 17970Sstevel@tonic-gate symp->sym_strhdr = cache[shp->sh_link].c_shdr; 17982712Snn35248 } else { 17992712Snn35248 dprintf("Symbol table already there for %s\n", 18002712Snn35248 objectfile); 18010Sstevel@tonic-gate } 18023347Sab196087 } else if (shp->sh_type == SHT_SUNW_LDYNSYM) { 18033347Sab196087 /* .SUNW_ldynsym section is auxiliary to .dynsym */ 18043347Sab196087 if (fptr->file_dynsym.sym_data_aux == NULL) { 18053347Sab196087 dprintf(".SUNW_ldynsym symbol table" 18063347Sab196087 " found for %s\n", objectfile); 18073347Sab196087 fptr->file_dynsym.sym_data_aux = cp->c_data; 18083347Sab196087 fptr->file_dynsym.sym_symn_aux = 18093347Sab196087 shp->sh_size / shp->sh_entsize; 18103347Sab196087 fptr->file_dynsym.sym_symn += 18113347Sab196087 fptr->file_dynsym.sym_symn_aux; 18123347Sab196087 fptr->file_dynsym.sym_hdr_aux = cp->c_shdr; 18133347Sab196087 } else { 18143347Sab196087 dprintf(".SUNW_ldynsym symbol table already" 18153347Sab196087 " there for %s\n", objectfile); 18163347Sab196087 } 18170Sstevel@tonic-gate } else if (shp->sh_type == SHT_DYNAMIC) { 18180Sstevel@tonic-gate dyn = cp; 18190Sstevel@tonic-gate } else if (strcmp(cp->c_name, ".plt") == 0) { 18200Sstevel@tonic-gate plt = cp; 18210Sstevel@tonic-gate } else if (strcmp(cp->c_name, ".SUNW_ctf") == 0) { 18220Sstevel@tonic-gate /* 18230Sstevel@tonic-gate * Skip over bogus CTF sections so they don't come back 18240Sstevel@tonic-gate * to haunt us later. 18250Sstevel@tonic-gate */ 18260Sstevel@tonic-gate if (shp->sh_link == 0 || 1827942Sahl shp->sh_link >= nshdrs || 18280Sstevel@tonic-gate (cache[shp->sh_link].c_shdr.sh_type != SHT_DYNSYM && 18290Sstevel@tonic-gate cache[shp->sh_link].c_shdr.sh_type != SHT_SYMTAB)) { 18300Sstevel@tonic-gate dprintf("Bad sh_link %d for " 18310Sstevel@tonic-gate "CTF\n", shp->sh_link); 18320Sstevel@tonic-gate continue; 18330Sstevel@tonic-gate } 18340Sstevel@tonic-gate ctf = cp; 18350Sstevel@tonic-gate } 18360Sstevel@tonic-gate } 18370Sstevel@tonic-gate 18380Sstevel@tonic-gate /* 18390Sstevel@tonic-gate * At this point, we've found all the symbol tables we're ever going 18400Sstevel@tonic-gate * to find: the ones in the loop above and possibly the symtab that 18410Sstevel@tonic-gate * was included in the core file. Before we perform any lookups, we 18420Sstevel@tonic-gate * create sorted versions to optimize for lookups. 18430Sstevel@tonic-gate */ 18440Sstevel@tonic-gate optimize_symtab(&fptr->file_symtab); 18450Sstevel@tonic-gate optimize_symtab(&fptr->file_dynsym); 18460Sstevel@tonic-gate 18470Sstevel@tonic-gate /* 18480Sstevel@tonic-gate * Fill in the base address of the text mapping for shared libraries. 18490Sstevel@tonic-gate * This allows us to translate symbols before librtld_db is ready. 18500Sstevel@tonic-gate */ 18510Sstevel@tonic-gate if (fptr->file_etype == ET_DYN) { 18520Sstevel@tonic-gate fptr->file_dyn_base = fptr->file_map->map_pmap.pr_vaddr - 18530Sstevel@tonic-gate fptr->file_map->map_pmap.pr_offset; 18542712Snn35248 dprintf("setting file_dyn_base for %s to %lx\n", 18552712Snn35248 objectfile, (long)fptr->file_dyn_base); 18560Sstevel@tonic-gate } 18570Sstevel@tonic-gate 18580Sstevel@tonic-gate /* 18590Sstevel@tonic-gate * Record the CTF section information in the file info structure. 18600Sstevel@tonic-gate */ 18610Sstevel@tonic-gate if (ctf != NULL) { 18620Sstevel@tonic-gate fptr->file_ctf_off = ctf->c_shdr.sh_offset; 18630Sstevel@tonic-gate fptr->file_ctf_size = ctf->c_shdr.sh_size; 18640Sstevel@tonic-gate if (ctf->c_shdr.sh_link != 0 && 18650Sstevel@tonic-gate cache[ctf->c_shdr.sh_link].c_shdr.sh_type == SHT_DYNSYM) 18660Sstevel@tonic-gate fptr->file_ctf_dyn = 1; 18670Sstevel@tonic-gate } 18680Sstevel@tonic-gate 18690Sstevel@tonic-gate if (fptr->file_lo == NULL) 18700Sstevel@tonic-gate goto done; /* Nothing else to do if no load object info */ 18710Sstevel@tonic-gate 18720Sstevel@tonic-gate /* 18730Sstevel@tonic-gate * If the object is a shared library and we have a different rl_base 18740Sstevel@tonic-gate * value, reset file_dyn_base according to librtld_db's information. 18750Sstevel@tonic-gate */ 18760Sstevel@tonic-gate if (fptr->file_etype == ET_DYN && 18770Sstevel@tonic-gate fptr->file_lo->rl_base != fptr->file_dyn_base) { 18782712Snn35248 dprintf("resetting file_dyn_base for %s to %lx\n", 18792712Snn35248 objectfile, (long)fptr->file_lo->rl_base); 18800Sstevel@tonic-gate fptr->file_dyn_base = fptr->file_lo->rl_base; 18810Sstevel@tonic-gate } 18820Sstevel@tonic-gate 18830Sstevel@tonic-gate /* 18840Sstevel@tonic-gate * Fill in the PLT information for this file if a PLT symbol is found. 18850Sstevel@tonic-gate */ 18860Sstevel@tonic-gate if (sym_by_name(&fptr->file_dynsym, "_PROCEDURE_LINKAGE_TABLE_", &s, 18870Sstevel@tonic-gate NULL) != NULL) { 18880Sstevel@tonic-gate fptr->file_plt_base = s.st_value + fptr->file_dyn_base; 18890Sstevel@tonic-gate fptr->file_plt_size = (plt != NULL) ? plt->c_shdr.sh_size : 0; 18900Sstevel@tonic-gate 18910Sstevel@tonic-gate /* 18920Sstevel@tonic-gate * Bring the load object up to date; it is the only way the 18930Sstevel@tonic-gate * user has to access the PLT data. The PLT information in the 18940Sstevel@tonic-gate * rd_loadobj_t is not set in the call to map_iter() (the 18950Sstevel@tonic-gate * callback for rd_loadobj_iter) where we set file_lo. 18960Sstevel@tonic-gate */ 18970Sstevel@tonic-gate fptr->file_lo->rl_plt_base = fptr->file_plt_base; 18980Sstevel@tonic-gate fptr->file_lo->rl_plt_size = fptr->file_plt_size; 18990Sstevel@tonic-gate 19000Sstevel@tonic-gate dprintf("PLT found at %p, size = %lu\n", 19010Sstevel@tonic-gate (void *)fptr->file_plt_base, (ulong_t)fptr->file_plt_size); 19020Sstevel@tonic-gate } 19030Sstevel@tonic-gate 19040Sstevel@tonic-gate /* 19050Sstevel@tonic-gate * Fill in the PLT information. 19060Sstevel@tonic-gate */ 19070Sstevel@tonic-gate if (dyn != NULL) { 19080Sstevel@tonic-gate uintptr_t dynaddr = dyn->c_shdr.sh_addr + fptr->file_dyn_base; 19090Sstevel@tonic-gate size_t ndyn = dyn->c_shdr.sh_size / dyn->c_shdr.sh_entsize; 19100Sstevel@tonic-gate GElf_Dyn d; 19110Sstevel@tonic-gate 19120Sstevel@tonic-gate for (i = 0; i < ndyn; i++) { 19130Sstevel@tonic-gate if (gelf_getdyn(dyn->c_data, i, &d) != NULL && 19140Sstevel@tonic-gate d.d_tag == DT_JMPREL) { 19152712Snn35248 dprintf("DT_JMPREL is %p\n", 19162712Snn35248 (void *)(uintptr_t)d.d_un.d_ptr); 19170Sstevel@tonic-gate fptr->file_jmp_rel = 19180Sstevel@tonic-gate d.d_un.d_ptr + fptr->file_dyn_base; 19190Sstevel@tonic-gate break; 19200Sstevel@tonic-gate } 19210Sstevel@tonic-gate } 19220Sstevel@tonic-gate 19230Sstevel@tonic-gate dprintf("_DYNAMIC found at %p, %lu entries, DT_JMPREL = %p\n", 19240Sstevel@tonic-gate (void *)dynaddr, (ulong_t)ndyn, (void *)fptr->file_jmp_rel); 19250Sstevel@tonic-gate } 19260Sstevel@tonic-gate 19270Sstevel@tonic-gate done: 19280Sstevel@tonic-gate free(cache); 19290Sstevel@tonic-gate return; 19300Sstevel@tonic-gate 19310Sstevel@tonic-gate bad: 19320Sstevel@tonic-gate if (cache != NULL) 19330Sstevel@tonic-gate free(cache); 19340Sstevel@tonic-gate 19350Sstevel@tonic-gate (void) elf_end(elf); 19360Sstevel@tonic-gate fptr->file_elf = NULL; 19370Sstevel@tonic-gate if (fptr->file_elfmem != NULL) { 19380Sstevel@tonic-gate free(fptr->file_elfmem); 19390Sstevel@tonic-gate fptr->file_elfmem = NULL; 19400Sstevel@tonic-gate } 19410Sstevel@tonic-gate (void) close(fptr->file_fd); 19420Sstevel@tonic-gate fptr->file_fd = -1; 19430Sstevel@tonic-gate } 19440Sstevel@tonic-gate 19450Sstevel@tonic-gate /* 19460Sstevel@tonic-gate * Given a process virtual address, return the map_info_t containing it. 19470Sstevel@tonic-gate * If none found, return NULL. 19480Sstevel@tonic-gate */ 19490Sstevel@tonic-gate map_info_t * 19500Sstevel@tonic-gate Paddr2mptr(struct ps_prochandle *P, uintptr_t addr) 19510Sstevel@tonic-gate { 19520Sstevel@tonic-gate int lo = 0; 19530Sstevel@tonic-gate int hi = P->map_count - 1; 19540Sstevel@tonic-gate int mid; 19550Sstevel@tonic-gate map_info_t *mp; 19560Sstevel@tonic-gate 19570Sstevel@tonic-gate while (lo <= hi) { 19580Sstevel@tonic-gate 19590Sstevel@tonic-gate mid = (lo + hi) / 2; 19600Sstevel@tonic-gate mp = &P->mappings[mid]; 19610Sstevel@tonic-gate 19620Sstevel@tonic-gate /* check that addr is in [vaddr, vaddr + size) */ 19630Sstevel@tonic-gate if ((addr - mp->map_pmap.pr_vaddr) < mp->map_pmap.pr_size) 19640Sstevel@tonic-gate return (mp); 19650Sstevel@tonic-gate 19660Sstevel@tonic-gate if (addr < mp->map_pmap.pr_vaddr) 19670Sstevel@tonic-gate hi = mid - 1; 19680Sstevel@tonic-gate else 19690Sstevel@tonic-gate lo = mid + 1; 19700Sstevel@tonic-gate } 19710Sstevel@tonic-gate 19720Sstevel@tonic-gate return (NULL); 19730Sstevel@tonic-gate } 19740Sstevel@tonic-gate 19750Sstevel@tonic-gate /* 19760Sstevel@tonic-gate * Return the map_info_t for the executable file. 19770Sstevel@tonic-gate * If not found, return NULL. 19780Sstevel@tonic-gate */ 19790Sstevel@tonic-gate static map_info_t * 19800Sstevel@tonic-gate exec_map(struct ps_prochandle *P) 19810Sstevel@tonic-gate { 19820Sstevel@tonic-gate uint_t i; 19830Sstevel@tonic-gate map_info_t *mptr; 19840Sstevel@tonic-gate map_info_t *mold = NULL; 19850Sstevel@tonic-gate file_info_t *fptr; 19860Sstevel@tonic-gate uintptr_t base; 19870Sstevel@tonic-gate 19880Sstevel@tonic-gate for (i = 0, mptr = P->mappings; i < P->map_count; i++, mptr++) { 19890Sstevel@tonic-gate if (mptr->map_pmap.pr_mapname[0] == '\0') 19900Sstevel@tonic-gate continue; 19910Sstevel@tonic-gate if (strcmp(mptr->map_pmap.pr_mapname, "a.out") == 0) { 19920Sstevel@tonic-gate if ((fptr = mptr->map_file) != NULL && 19930Sstevel@tonic-gate fptr->file_lo != NULL) { 19940Sstevel@tonic-gate base = fptr->file_lo->rl_base; 19950Sstevel@tonic-gate if (base >= mptr->map_pmap.pr_vaddr && 19960Sstevel@tonic-gate base < mptr->map_pmap.pr_vaddr + 19970Sstevel@tonic-gate mptr->map_pmap.pr_size) /* text space */ 19980Sstevel@tonic-gate return (mptr); 19990Sstevel@tonic-gate mold = mptr; /* must be the data */ 20000Sstevel@tonic-gate continue; 20010Sstevel@tonic-gate } 20020Sstevel@tonic-gate /* This is a poor way to test for text space */ 20030Sstevel@tonic-gate if (!(mptr->map_pmap.pr_mflags & MA_EXEC) || 20040Sstevel@tonic-gate (mptr->map_pmap.pr_mflags & MA_WRITE)) { 20050Sstevel@tonic-gate mold = mptr; 20060Sstevel@tonic-gate continue; 20070Sstevel@tonic-gate } 20080Sstevel@tonic-gate return (mptr); 20090Sstevel@tonic-gate } 20100Sstevel@tonic-gate } 20110Sstevel@tonic-gate 20120Sstevel@tonic-gate return (mold); 20130Sstevel@tonic-gate } 20140Sstevel@tonic-gate 20150Sstevel@tonic-gate /* 20160Sstevel@tonic-gate * Given a shared object name, return the map_info_t for it. If no matching 20170Sstevel@tonic-gate * object is found, return NULL. Normally, the link maps contain the full 20180Sstevel@tonic-gate * object pathname, e.g. /usr/lib/libc.so.1. We allow the object name to 20190Sstevel@tonic-gate * take one of the following forms: 20200Sstevel@tonic-gate * 20210Sstevel@tonic-gate * 1. An exact match (i.e. a full pathname): "/usr/lib/libc.so.1" 20220Sstevel@tonic-gate * 2. An exact basename match: "libc.so.1" 20230Sstevel@tonic-gate * 3. An initial basename match up to a '.' suffix: "libc.so" or "libc" 20240Sstevel@tonic-gate * 4. The literal string "a.out" is an alias for the executable mapping 20250Sstevel@tonic-gate * 20260Sstevel@tonic-gate * The third case is a convenience for callers and may not be necessary. 20270Sstevel@tonic-gate * 20280Sstevel@tonic-gate * As the exact same object name may be loaded on different link maps (see 20290Sstevel@tonic-gate * dlmopen(3DL)), we also allow the caller to resolve the object name by 20300Sstevel@tonic-gate * specifying a particular link map id. If lmid is PR_LMID_EVERY, the 20310Sstevel@tonic-gate * first matching name will be returned, regardless of the link map id. 20320Sstevel@tonic-gate */ 20330Sstevel@tonic-gate static map_info_t * 20340Sstevel@tonic-gate object_to_map(struct ps_prochandle *P, Lmid_t lmid, const char *objname) 20350Sstevel@tonic-gate { 20360Sstevel@tonic-gate map_info_t *mp; 20370Sstevel@tonic-gate file_info_t *fp; 20380Sstevel@tonic-gate size_t objlen; 20390Sstevel@tonic-gate uint_t i; 20400Sstevel@tonic-gate 20410Sstevel@tonic-gate /* 20422712Snn35248 * If we have no rtld_db, then always treat a request as one for all 20432712Snn35248 * link maps. 20442712Snn35248 */ 20452712Snn35248 if (P->rap == NULL) 20462712Snn35248 lmid = PR_LMID_EVERY; 20472712Snn35248 20482712Snn35248 /* 20490Sstevel@tonic-gate * First pass: look for exact matches of the entire pathname or 20500Sstevel@tonic-gate * basename (cases 1 and 2 above): 20510Sstevel@tonic-gate */ 20520Sstevel@tonic-gate for (i = 0, mp = P->mappings; i < P->map_count; i++, mp++) { 20530Sstevel@tonic-gate 20540Sstevel@tonic-gate if (mp->map_pmap.pr_mapname[0] == '\0' || 2055*7675SEdward.Pilatowicz@Sun.COM (fp = mp->map_file) == NULL || 2056*7675SEdward.Pilatowicz@Sun.COM ((fp->file_lname == NULL) && (fp->file_rname == NULL))) 20570Sstevel@tonic-gate continue; 20580Sstevel@tonic-gate 20590Sstevel@tonic-gate if (lmid != PR_LMID_EVERY && 20600Sstevel@tonic-gate (fp->file_lo == NULL || lmid != fp->file_lo->rl_lmident)) 20610Sstevel@tonic-gate continue; 20620Sstevel@tonic-gate 20630Sstevel@tonic-gate /* 20640Sstevel@tonic-gate * If we match, return the primary text mapping; otherwise 20650Sstevel@tonic-gate * just return the mapping we matched. 20660Sstevel@tonic-gate */ 2067*7675SEdward.Pilatowicz@Sun.COM if ((fp->file_lbase && strcmp(fp->file_lbase, objname) == 0) || 2068*7675SEdward.Pilatowicz@Sun.COM (fp->file_rbase && strcmp(fp->file_rbase, objname) == 0) || 2069*7675SEdward.Pilatowicz@Sun.COM (fp->file_lname && strcmp(fp->file_lname, objname) == 0) || 2070*7675SEdward.Pilatowicz@Sun.COM (fp->file_rname && strcmp(fp->file_rname, objname) == 0)) 20710Sstevel@tonic-gate return (fp->file_map ? fp->file_map : mp); 20720Sstevel@tonic-gate } 20730Sstevel@tonic-gate 20740Sstevel@tonic-gate objlen = strlen(objname); 20750Sstevel@tonic-gate 20760Sstevel@tonic-gate /* 20770Sstevel@tonic-gate * Second pass: look for partial matches (case 3 above): 20780Sstevel@tonic-gate */ 20790Sstevel@tonic-gate for (i = 0, mp = P->mappings; i < P->map_count; i++, mp++) { 20800Sstevel@tonic-gate 20810Sstevel@tonic-gate if (mp->map_pmap.pr_mapname[0] == '\0' || 2082*7675SEdward.Pilatowicz@Sun.COM (fp = mp->map_file) == NULL || 2083*7675SEdward.Pilatowicz@Sun.COM ((fp->file_lname == NULL) && (fp->file_rname == NULL))) 20840Sstevel@tonic-gate continue; 20850Sstevel@tonic-gate 20860Sstevel@tonic-gate if (lmid != PR_LMID_EVERY && 20870Sstevel@tonic-gate (fp->file_lo == NULL || lmid != fp->file_lo->rl_lmident)) 20880Sstevel@tonic-gate continue; 20890Sstevel@tonic-gate 20900Sstevel@tonic-gate /* 20910Sstevel@tonic-gate * If we match, return the primary text mapping; otherwise 20920Sstevel@tonic-gate * just return the mapping we matched. 20930Sstevel@tonic-gate */ 2094*7675SEdward.Pilatowicz@Sun.COM if ((fp->file_lbase != NULL) && 2095*7675SEdward.Pilatowicz@Sun.COM (strncmp(fp->file_lbase, objname, objlen) == 0) && 2096*7675SEdward.Pilatowicz@Sun.COM (fp->file_lbase[objlen] == '.')) 2097*7675SEdward.Pilatowicz@Sun.COM return (fp->file_map ? fp->file_map : mp); 2098*7675SEdward.Pilatowicz@Sun.COM if ((fp->file_rbase != NULL) && 2099*7675SEdward.Pilatowicz@Sun.COM (strncmp(fp->file_rbase, objname, objlen) == 0) && 2100*7675SEdward.Pilatowicz@Sun.COM (fp->file_rbase[objlen] == '.')) 21010Sstevel@tonic-gate return (fp->file_map ? fp->file_map : mp); 21020Sstevel@tonic-gate } 21030Sstevel@tonic-gate 21040Sstevel@tonic-gate /* 21050Sstevel@tonic-gate * One last check: we allow "a.out" to always alias the executable, 21060Sstevel@tonic-gate * assuming this name was not in use for something else. 21070Sstevel@tonic-gate */ 21080Sstevel@tonic-gate if ((lmid == PR_LMID_EVERY || lmid == LM_ID_BASE) && 21090Sstevel@tonic-gate (strcmp(objname, "a.out") == 0)) 21100Sstevel@tonic-gate return (P->map_exec); 21110Sstevel@tonic-gate 21120Sstevel@tonic-gate return (NULL); 21130Sstevel@tonic-gate } 21140Sstevel@tonic-gate 21150Sstevel@tonic-gate static map_info_t * 21160Sstevel@tonic-gate object_name_to_map(struct ps_prochandle *P, Lmid_t lmid, const char *name) 21170Sstevel@tonic-gate { 21180Sstevel@tonic-gate map_info_t *mptr; 21190Sstevel@tonic-gate 21200Sstevel@tonic-gate if (!P->info_valid) 21210Sstevel@tonic-gate Pupdate_maps(P); 21220Sstevel@tonic-gate 21230Sstevel@tonic-gate if (P->map_exec == NULL && ((mptr = Paddr2mptr(P, 21240Sstevel@tonic-gate Pgetauxval(P, AT_ENTRY))) != NULL || (mptr = exec_map(P)) != NULL)) 21250Sstevel@tonic-gate P->map_exec = mptr; 21260Sstevel@tonic-gate 21270Sstevel@tonic-gate if (P->map_ldso == NULL && (mptr = Paddr2mptr(P, 21280Sstevel@tonic-gate Pgetauxval(P, AT_BASE))) != NULL) 21290Sstevel@tonic-gate P->map_ldso = mptr; 21300Sstevel@tonic-gate 21310Sstevel@tonic-gate if (name == PR_OBJ_EXEC) 21320Sstevel@tonic-gate mptr = P->map_exec; 21330Sstevel@tonic-gate else if (name == PR_OBJ_LDSO) 21340Sstevel@tonic-gate mptr = P->map_ldso; 21353181Sjhaslam else if (Prd_agent(P) != NULL || P->state == PS_IDLE) 21363181Sjhaslam mptr = object_to_map(P, lmid, name); 21372712Snn35248 else 21383181Sjhaslam mptr = NULL; 21390Sstevel@tonic-gate 21400Sstevel@tonic-gate return (mptr); 21410Sstevel@tonic-gate } 21420Sstevel@tonic-gate 21430Sstevel@tonic-gate /* 21440Sstevel@tonic-gate * When two symbols are found by address, decide which one is to be preferred. 21450Sstevel@tonic-gate */ 21460Sstevel@tonic-gate static GElf_Sym * 21470Sstevel@tonic-gate sym_prefer(GElf_Sym *sym1, char *name1, GElf_Sym *sym2, char *name2) 21480Sstevel@tonic-gate { 21490Sstevel@tonic-gate /* 21500Sstevel@tonic-gate * Prefer the non-NULL symbol. 21510Sstevel@tonic-gate */ 21520Sstevel@tonic-gate if (sym1 == NULL) 21530Sstevel@tonic-gate return (sym2); 21540Sstevel@tonic-gate if (sym2 == NULL) 21550Sstevel@tonic-gate return (sym1); 21560Sstevel@tonic-gate 21570Sstevel@tonic-gate /* 21580Sstevel@tonic-gate * Defer to the sort ordering... 21590Sstevel@tonic-gate */ 21600Sstevel@tonic-gate return (byaddr_cmp_common(sym1, name1, sym2, name2) <= 0 ? sym1 : sym2); 21610Sstevel@tonic-gate } 21620Sstevel@tonic-gate 21630Sstevel@tonic-gate /* 21644753Srh87107 * Use a binary search to do the work of sym_by_addr(). 21650Sstevel@tonic-gate */ 21660Sstevel@tonic-gate static GElf_Sym * 21674753Srh87107 sym_by_addr_binary(sym_tbl_t *symtab, GElf_Addr addr, GElf_Sym *symp, 21684753Srh87107 uint_t *idp) 21690Sstevel@tonic-gate { 21700Sstevel@tonic-gate GElf_Sym sym, osym; 21710Sstevel@tonic-gate uint_t i, oid, *byaddr = symtab->sym_byaddr; 21720Sstevel@tonic-gate int min, max, mid, omid, found = 0; 21730Sstevel@tonic-gate 21743347Sab196087 if (symtab->sym_data_pri == NULL || symtab->sym_count == 0) 21750Sstevel@tonic-gate return (NULL); 21760Sstevel@tonic-gate 21770Sstevel@tonic-gate min = 0; 21780Sstevel@tonic-gate max = symtab->sym_count - 1; 21790Sstevel@tonic-gate osym.st_value = 0; 21800Sstevel@tonic-gate 21810Sstevel@tonic-gate /* 21820Sstevel@tonic-gate * We can't return when we've found a match, we have to continue 21830Sstevel@tonic-gate * searching for the closest matching symbol. 21840Sstevel@tonic-gate */ 21850Sstevel@tonic-gate while (min <= max) { 21860Sstevel@tonic-gate mid = (max + min) / 2; 21870Sstevel@tonic-gate 21880Sstevel@tonic-gate i = byaddr[mid]; 21893347Sab196087 (void) symtab_getsym(symtab, i, &sym); 21900Sstevel@tonic-gate 21910Sstevel@tonic-gate if (addr >= sym.st_value && 21920Sstevel@tonic-gate addr < sym.st_value + sym.st_size && 21930Sstevel@tonic-gate (!found || sym.st_value > osym.st_value)) { 21940Sstevel@tonic-gate osym = sym; 21950Sstevel@tonic-gate omid = mid; 21960Sstevel@tonic-gate oid = i; 21970Sstevel@tonic-gate found = 1; 21980Sstevel@tonic-gate } 21990Sstevel@tonic-gate 22000Sstevel@tonic-gate if (addr < sym.st_value) 22010Sstevel@tonic-gate max = mid - 1; 22020Sstevel@tonic-gate else 22030Sstevel@tonic-gate min = mid + 1; 22040Sstevel@tonic-gate } 22050Sstevel@tonic-gate 22060Sstevel@tonic-gate if (!found) 22070Sstevel@tonic-gate return (NULL); 22080Sstevel@tonic-gate 22090Sstevel@tonic-gate /* 22100Sstevel@tonic-gate * There may be many symbols with identical values so we walk 22110Sstevel@tonic-gate * backward in the byaddr table to find the best match. 22120Sstevel@tonic-gate */ 22130Sstevel@tonic-gate do { 22140Sstevel@tonic-gate sym = osym; 22150Sstevel@tonic-gate i = oid; 22160Sstevel@tonic-gate 22170Sstevel@tonic-gate if (omid == 0) 22180Sstevel@tonic-gate break; 22190Sstevel@tonic-gate 22200Sstevel@tonic-gate oid = byaddr[--omid]; 22213347Sab196087 (void) symtab_getsym(symtab, oid, &osym); 22220Sstevel@tonic-gate } while (addr >= osym.st_value && 22230Sstevel@tonic-gate addr < sym.st_value + osym.st_size && 22240Sstevel@tonic-gate osym.st_value == sym.st_value); 22250Sstevel@tonic-gate 22260Sstevel@tonic-gate *symp = sym; 22270Sstevel@tonic-gate if (idp != NULL) 22280Sstevel@tonic-gate *idp = i; 22290Sstevel@tonic-gate return (symp); 22300Sstevel@tonic-gate } 22310Sstevel@tonic-gate 22320Sstevel@tonic-gate /* 22334753Srh87107 * Use a linear search to do the work of sym_by_addr(). 22340Sstevel@tonic-gate */ 22350Sstevel@tonic-gate static GElf_Sym * 22364753Srh87107 sym_by_addr_linear(sym_tbl_t *symtab, GElf_Addr addr, GElf_Sym *symbolp, 22374753Srh87107 uint_t *idp) 22384753Srh87107 { 22394753Srh87107 size_t symn = symtab->sym_symn; 22404753Srh87107 char *strs = symtab->sym_strs; 22414753Srh87107 GElf_Sym sym, *symp = NULL; 22424753Srh87107 GElf_Sym osym, *osymp = NULL; 22434753Srh87107 int i, id; 22444753Srh87107 22454753Srh87107 if (symtab->sym_data_pri == NULL || symn == 0 || strs == NULL) 22464753Srh87107 return (NULL); 22474753Srh87107 22484753Srh87107 for (i = 0; i < symn; i++) { 22494753Srh87107 if ((symp = symtab_getsym(symtab, i, &sym)) != NULL) { 22504753Srh87107 if (addr >= sym.st_value && 22514753Srh87107 addr < sym.st_value + sym.st_size) { 22524753Srh87107 if (osymp) 22534753Srh87107 symp = sym_prefer( 22544753Srh87107 symp, strs + symp->st_name, 22554753Srh87107 osymp, strs + osymp->st_name); 22564753Srh87107 if (symp != osymp) { 22574753Srh87107 osym = sym; 22584753Srh87107 osymp = &osym; 22594753Srh87107 id = i; 22604753Srh87107 } 22614753Srh87107 } 22624753Srh87107 } 22634753Srh87107 } 22644753Srh87107 if (osymp) { 22654753Srh87107 *symbolp = osym; 22664753Srh87107 if (idp) 22674753Srh87107 *idp = id; 22684753Srh87107 return (symbolp); 22694753Srh87107 } 22704753Srh87107 return (NULL); 22714753Srh87107 } 22724753Srh87107 22734753Srh87107 /* 22744753Srh87107 * Look up a symbol by address in the specified symbol table. 22754753Srh87107 * Adjustment to 'addr' must already have been made for the 22764753Srh87107 * offset of the symbol if this is a dynamic library symbol table. 22774753Srh87107 * 22784753Srh87107 * Use a linear or a binary search depending on whether or not we 22794753Srh87107 * chose to sort the table in optimize_symtab(). 22804753Srh87107 */ 22814753Srh87107 static GElf_Sym * 22824753Srh87107 sym_by_addr(sym_tbl_t *symtab, GElf_Addr addr, GElf_Sym *symp, uint_t *idp) 22834753Srh87107 { 22844753Srh87107 if (_libproc_no_qsort) { 22854753Srh87107 return (sym_by_addr_linear(symtab, addr, symp, idp)); 22864753Srh87107 } else { 22874753Srh87107 return (sym_by_addr_binary(symtab, addr, symp, idp)); 22884753Srh87107 } 22894753Srh87107 } 22904753Srh87107 22914753Srh87107 /* 22924753Srh87107 * Use a binary search to do the work of sym_by_name(). 22934753Srh87107 */ 22944753Srh87107 static GElf_Sym * 22954753Srh87107 sym_by_name_binary(sym_tbl_t *symtab, const char *name, GElf_Sym *symp, 22964753Srh87107 uint_t *idp) 22970Sstevel@tonic-gate { 22980Sstevel@tonic-gate char *strs = symtab->sym_strs; 22990Sstevel@tonic-gate uint_t i, *byname = symtab->sym_byname; 23000Sstevel@tonic-gate int min, mid, max, cmp; 23010Sstevel@tonic-gate 23023347Sab196087 if (symtab->sym_data_pri == NULL || strs == NULL || 23033347Sab196087 symtab->sym_count == 0) 23040Sstevel@tonic-gate return (NULL); 23050Sstevel@tonic-gate 23060Sstevel@tonic-gate min = 0; 23070Sstevel@tonic-gate max = symtab->sym_count - 1; 23080Sstevel@tonic-gate 23090Sstevel@tonic-gate while (min <= max) { 23100Sstevel@tonic-gate mid = (max + min) / 2; 23110Sstevel@tonic-gate 23120Sstevel@tonic-gate i = byname[mid]; 23133347Sab196087 (void) symtab_getsym(symtab, i, symp); 23140Sstevel@tonic-gate 23150Sstevel@tonic-gate if ((cmp = strcmp(name, strs + symp->st_name)) == 0) { 23160Sstevel@tonic-gate if (idp != NULL) 23170Sstevel@tonic-gate *idp = i; 23180Sstevel@tonic-gate return (symp); 23190Sstevel@tonic-gate } 23200Sstevel@tonic-gate 23210Sstevel@tonic-gate if (cmp < 0) 23220Sstevel@tonic-gate max = mid - 1; 23230Sstevel@tonic-gate else 23240Sstevel@tonic-gate min = mid + 1; 23250Sstevel@tonic-gate } 23260Sstevel@tonic-gate 23270Sstevel@tonic-gate return (NULL); 23280Sstevel@tonic-gate } 23290Sstevel@tonic-gate 23300Sstevel@tonic-gate /* 23314753Srh87107 * Use a linear search to do the work of sym_by_name(). 23324753Srh87107 */ 23334753Srh87107 static GElf_Sym * 23344753Srh87107 sym_by_name_linear(sym_tbl_t *symtab, const char *name, GElf_Sym *symp, 23354753Srh87107 uint_t *idp) 23364753Srh87107 { 23374753Srh87107 size_t symn = symtab->sym_symn; 23384753Srh87107 char *strs = symtab->sym_strs; 23394753Srh87107 int i; 23404753Srh87107 23414753Srh87107 if (symtab->sym_data_pri == NULL || symn == 0 || strs == NULL) 23424753Srh87107 return (NULL); 23434753Srh87107 23444753Srh87107 for (i = 0; i < symn; i++) { 23454753Srh87107 if (symtab_getsym(symtab, i, symp) && 23464753Srh87107 strcmp(name, strs + symp->st_name) == 0) { 23474753Srh87107 if (idp) 23484753Srh87107 *idp = i; 23494753Srh87107 return (symp); 23504753Srh87107 } 23514753Srh87107 } 23524753Srh87107 23534753Srh87107 return (NULL); 23544753Srh87107 } 23554753Srh87107 23564753Srh87107 /* 23574753Srh87107 * Look up a symbol by name in the specified symbol table. 23584753Srh87107 * 23594753Srh87107 * Use a linear or a binary search depending on whether or not we 23604753Srh87107 * chose to sort the table in optimize_symtab(). 23614753Srh87107 */ 23624753Srh87107 static GElf_Sym * 23634753Srh87107 sym_by_name(sym_tbl_t *symtab, const char *name, GElf_Sym *symp, uint_t *idp) 23644753Srh87107 { 23654753Srh87107 if (_libproc_no_qsort) { 23664753Srh87107 return (sym_by_name_linear(symtab, name, symp, idp)); 23674753Srh87107 } else { 23684753Srh87107 return (sym_by_name_binary(symtab, name, symp, idp)); 23694753Srh87107 } 23704753Srh87107 } 23714753Srh87107 23724753Srh87107 /* 23730Sstevel@tonic-gate * Search the process symbol tables looking for a symbol whose 23740Sstevel@tonic-gate * value to value+size contain the address specified by addr. 23750Sstevel@tonic-gate * Return values are: 23760Sstevel@tonic-gate * sym_name_buffer containing the symbol name 23770Sstevel@tonic-gate * GElf_Sym symbol table entry 23780Sstevel@tonic-gate * prsyminfo_t ancillary symbol information 23790Sstevel@tonic-gate * Returns 0 on success, -1 on failure. 23800Sstevel@tonic-gate */ 2381*7675SEdward.Pilatowicz@Sun.COM static int 2382*7675SEdward.Pilatowicz@Sun.COM i_Pxlookup_by_addr( 23830Sstevel@tonic-gate struct ps_prochandle *P, 2384*7675SEdward.Pilatowicz@Sun.COM int lmresolve, /* use resolve linker object names */ 23850Sstevel@tonic-gate uintptr_t addr, /* process address being sought */ 23860Sstevel@tonic-gate char *sym_name_buffer, /* buffer for the symbol name */ 23870Sstevel@tonic-gate size_t bufsize, /* size of sym_name_buffer */ 23880Sstevel@tonic-gate GElf_Sym *symbolp, /* returned symbol table entry */ 23890Sstevel@tonic-gate prsyminfo_t *sip) /* returned symbol info */ 23900Sstevel@tonic-gate { 23910Sstevel@tonic-gate GElf_Sym *symp; 23920Sstevel@tonic-gate char *name; 23930Sstevel@tonic-gate GElf_Sym sym1, *sym1p = NULL; 23940Sstevel@tonic-gate GElf_Sym sym2, *sym2p = NULL; 23950Sstevel@tonic-gate char *name1 = NULL; 23960Sstevel@tonic-gate char *name2 = NULL; 23970Sstevel@tonic-gate uint_t i1; 23980Sstevel@tonic-gate uint_t i2; 23990Sstevel@tonic-gate map_info_t *mptr; 24000Sstevel@tonic-gate file_info_t *fptr; 24010Sstevel@tonic-gate 24020Sstevel@tonic-gate (void) Prd_agent(P); 24030Sstevel@tonic-gate 24040Sstevel@tonic-gate if ((mptr = Paddr2mptr(P, addr)) == NULL || /* no such address */ 24050Sstevel@tonic-gate (fptr = build_map_symtab(P, mptr)) == NULL || /* no mapped file */ 24060Sstevel@tonic-gate fptr->file_elf == NULL) /* not an ELF file */ 24070Sstevel@tonic-gate return (-1); 24080Sstevel@tonic-gate 24090Sstevel@tonic-gate /* 24100Sstevel@tonic-gate * Adjust the address by the load object base address in 24110Sstevel@tonic-gate * case the address turns out to be in a shared library. 24120Sstevel@tonic-gate */ 24130Sstevel@tonic-gate addr -= fptr->file_dyn_base; 24140Sstevel@tonic-gate 24150Sstevel@tonic-gate /* 24160Sstevel@tonic-gate * Search both symbol tables, symtab first, then dynsym. 24170Sstevel@tonic-gate */ 24180Sstevel@tonic-gate if ((sym1p = sym_by_addr(&fptr->file_symtab, addr, &sym1, &i1)) != NULL) 24190Sstevel@tonic-gate name1 = fptr->file_symtab.sym_strs + sym1.st_name; 24200Sstevel@tonic-gate if ((sym2p = sym_by_addr(&fptr->file_dynsym, addr, &sym2, &i2)) != NULL) 24210Sstevel@tonic-gate name2 = fptr->file_dynsym.sym_strs + sym2.st_name; 24220Sstevel@tonic-gate 24230Sstevel@tonic-gate if ((symp = sym_prefer(sym1p, name1, sym2p, name2)) == NULL) 24240Sstevel@tonic-gate return (-1); 24250Sstevel@tonic-gate 24260Sstevel@tonic-gate name = (symp == sym1p) ? name1 : name2; 24270Sstevel@tonic-gate if (bufsize > 0) { 24280Sstevel@tonic-gate (void) strncpy(sym_name_buffer, name, bufsize); 24290Sstevel@tonic-gate sym_name_buffer[bufsize - 1] = '\0'; 24300Sstevel@tonic-gate } 24310Sstevel@tonic-gate 24320Sstevel@tonic-gate *symbolp = *symp; 24330Sstevel@tonic-gate if (sip != NULL) { 24340Sstevel@tonic-gate sip->prs_name = bufsize == 0 ? NULL : sym_name_buffer; 2435*7675SEdward.Pilatowicz@Sun.COM if (lmresolve && (fptr->file_rname != NULL)) 2436*7675SEdward.Pilatowicz@Sun.COM sip->prs_object = fptr->file_rbase; 2437*7675SEdward.Pilatowicz@Sun.COM else 2438*7675SEdward.Pilatowicz@Sun.COM sip->prs_object = fptr->file_lbase; 24390Sstevel@tonic-gate sip->prs_id = (symp == sym1p) ? i1 : i2; 24400Sstevel@tonic-gate sip->prs_table = (symp == sym1p) ? PR_SYMTAB : PR_DYNSYM; 24410Sstevel@tonic-gate sip->prs_lmid = (fptr->file_lo == NULL) ? LM_ID_BASE : 24420Sstevel@tonic-gate fptr->file_lo->rl_lmident; 24430Sstevel@tonic-gate } 24440Sstevel@tonic-gate 24450Sstevel@tonic-gate if (GELF_ST_TYPE(symbolp->st_info) != STT_TLS) 24460Sstevel@tonic-gate symbolp->st_value += fptr->file_dyn_base; 24470Sstevel@tonic-gate 24480Sstevel@tonic-gate return (0); 24490Sstevel@tonic-gate } 24500Sstevel@tonic-gate 24510Sstevel@tonic-gate int 2452*7675SEdward.Pilatowicz@Sun.COM Pxlookup_by_addr(struct ps_prochandle *P, uintptr_t addr, char *buf, 2453*7675SEdward.Pilatowicz@Sun.COM size_t bufsize, GElf_Sym *symp, prsyminfo_t *sip) 24540Sstevel@tonic-gate { 2455*7675SEdward.Pilatowicz@Sun.COM return (i_Pxlookup_by_addr(P, B_FALSE, addr, buf, bufsize, symp, sip)); 2456*7675SEdward.Pilatowicz@Sun.COM } 2457*7675SEdward.Pilatowicz@Sun.COM 2458*7675SEdward.Pilatowicz@Sun.COM int 2459*7675SEdward.Pilatowicz@Sun.COM Pxlookup_by_addr_resolved(struct ps_prochandle *P, uintptr_t addr, char *buf, 2460*7675SEdward.Pilatowicz@Sun.COM size_t bufsize, GElf_Sym *symp, prsyminfo_t *sip) 2461*7675SEdward.Pilatowicz@Sun.COM { 2462*7675SEdward.Pilatowicz@Sun.COM return (i_Pxlookup_by_addr(P, B_TRUE, addr, buf, bufsize, symp, sip)); 2463*7675SEdward.Pilatowicz@Sun.COM } 2464*7675SEdward.Pilatowicz@Sun.COM 2465*7675SEdward.Pilatowicz@Sun.COM int 2466*7675SEdward.Pilatowicz@Sun.COM Plookup_by_addr(struct ps_prochandle *P, uintptr_t addr, char *buf, 2467*7675SEdward.Pilatowicz@Sun.COM size_t size, GElf_Sym *symp) 2468*7675SEdward.Pilatowicz@Sun.COM { 2469*7675SEdward.Pilatowicz@Sun.COM return (i_Pxlookup_by_addr(P, B_FALSE, addr, buf, size, symp, NULL)); 24700Sstevel@tonic-gate } 24710Sstevel@tonic-gate 24720Sstevel@tonic-gate /* 24730Sstevel@tonic-gate * Search the process symbol tables looking for a symbol whose name matches the 24740Sstevel@tonic-gate * specified name and whose object and link map optionally match the specified 24750Sstevel@tonic-gate * parameters. On success, the function returns 0 and fills in the GElf_Sym 24760Sstevel@tonic-gate * symbol table entry. On failure, -1 is returned. 24770Sstevel@tonic-gate */ 24780Sstevel@tonic-gate int 24790Sstevel@tonic-gate Pxlookup_by_name( 24800Sstevel@tonic-gate struct ps_prochandle *P, 24810Sstevel@tonic-gate Lmid_t lmid, /* link map to match, or -1 for any */ 24820Sstevel@tonic-gate const char *oname, /* load object name */ 24830Sstevel@tonic-gate const char *sname, /* symbol name */ 24840Sstevel@tonic-gate GElf_Sym *symp, /* returned symbol table entry */ 24850Sstevel@tonic-gate prsyminfo_t *sip) /* returned symbol info */ 24860Sstevel@tonic-gate { 24870Sstevel@tonic-gate map_info_t *mptr; 24880Sstevel@tonic-gate file_info_t *fptr; 24890Sstevel@tonic-gate int cnt; 24900Sstevel@tonic-gate 24910Sstevel@tonic-gate GElf_Sym sym; 24920Sstevel@tonic-gate prsyminfo_t si; 24930Sstevel@tonic-gate int rv = -1; 24940Sstevel@tonic-gate uint_t id; 24950Sstevel@tonic-gate 24960Sstevel@tonic-gate if (oname == PR_OBJ_EVERY) { 24970Sstevel@tonic-gate /* create all the file_info_t's for all the mappings */ 24980Sstevel@tonic-gate (void) Prd_agent(P); 24990Sstevel@tonic-gate cnt = P->num_files; 25000Sstevel@tonic-gate fptr = list_next(&P->file_head); 25010Sstevel@tonic-gate } else { 25020Sstevel@tonic-gate cnt = 1; 25030Sstevel@tonic-gate if ((mptr = object_name_to_map(P, lmid, oname)) == NULL || 25040Sstevel@tonic-gate (fptr = build_map_symtab(P, mptr)) == NULL) 25050Sstevel@tonic-gate return (-1); 25060Sstevel@tonic-gate } 25070Sstevel@tonic-gate 25080Sstevel@tonic-gate /* 25090Sstevel@tonic-gate * Iterate through the loaded object files and look for the symbol 25100Sstevel@tonic-gate * name in the .symtab and .dynsym of each. If we encounter a match 25110Sstevel@tonic-gate * with SHN_UNDEF, keep looking in hopes of finding a better match. 25120Sstevel@tonic-gate * This means that a name such as "puts" will match the puts function 25130Sstevel@tonic-gate * in libc instead of matching the puts PLT entry in the a.out file. 25140Sstevel@tonic-gate */ 25150Sstevel@tonic-gate for (; cnt > 0; cnt--, fptr = list_next(fptr)) { 25160Sstevel@tonic-gate Pbuild_file_symtab(P, fptr); 25170Sstevel@tonic-gate 25180Sstevel@tonic-gate if (fptr->file_elf == NULL) 25190Sstevel@tonic-gate continue; 25200Sstevel@tonic-gate 25210Sstevel@tonic-gate if (lmid != PR_LMID_EVERY && fptr->file_lo != NULL && 25220Sstevel@tonic-gate lmid != fptr->file_lo->rl_lmident) 25230Sstevel@tonic-gate continue; 25240Sstevel@tonic-gate 25253347Sab196087 if (fptr->file_symtab.sym_data_pri != NULL && 25260Sstevel@tonic-gate sym_by_name(&fptr->file_symtab, sname, symp, &id)) { 25270Sstevel@tonic-gate if (sip != NULL) { 25280Sstevel@tonic-gate sip->prs_id = id; 25290Sstevel@tonic-gate sip->prs_table = PR_SYMTAB; 25300Sstevel@tonic-gate sip->prs_object = oname; 25310Sstevel@tonic-gate sip->prs_name = sname; 25320Sstevel@tonic-gate sip->prs_lmid = fptr->file_lo == NULL ? 25330Sstevel@tonic-gate LM_ID_BASE : fptr->file_lo->rl_lmident; 25340Sstevel@tonic-gate } 25353347Sab196087 } else if (fptr->file_dynsym.sym_data_pri != NULL && 25360Sstevel@tonic-gate sym_by_name(&fptr->file_dynsym, sname, symp, &id)) { 25370Sstevel@tonic-gate if (sip != NULL) { 25380Sstevel@tonic-gate sip->prs_id = id; 25390Sstevel@tonic-gate sip->prs_table = PR_DYNSYM; 25400Sstevel@tonic-gate sip->prs_object = oname; 25410Sstevel@tonic-gate sip->prs_name = sname; 25420Sstevel@tonic-gate sip->prs_lmid = fptr->file_lo == NULL ? 25430Sstevel@tonic-gate LM_ID_BASE : fptr->file_lo->rl_lmident; 25440Sstevel@tonic-gate } 25450Sstevel@tonic-gate } else { 25460Sstevel@tonic-gate continue; 25470Sstevel@tonic-gate } 25480Sstevel@tonic-gate 25490Sstevel@tonic-gate if (GELF_ST_TYPE(symp->st_info) != STT_TLS) 25500Sstevel@tonic-gate symp->st_value += fptr->file_dyn_base; 25510Sstevel@tonic-gate 25520Sstevel@tonic-gate if (symp->st_shndx != SHN_UNDEF) 25530Sstevel@tonic-gate return (0); 25540Sstevel@tonic-gate 25550Sstevel@tonic-gate if (rv != 0) { 25560Sstevel@tonic-gate if (sip != NULL) 25570Sstevel@tonic-gate si = *sip; 25580Sstevel@tonic-gate sym = *symp; 25590Sstevel@tonic-gate rv = 0; 25600Sstevel@tonic-gate } 25610Sstevel@tonic-gate } 25620Sstevel@tonic-gate 25630Sstevel@tonic-gate if (rv == 0) { 25640Sstevel@tonic-gate if (sip != NULL) 25650Sstevel@tonic-gate *sip = si; 25660Sstevel@tonic-gate *symp = sym; 25670Sstevel@tonic-gate } 25680Sstevel@tonic-gate 25690Sstevel@tonic-gate return (rv); 25700Sstevel@tonic-gate } 25710Sstevel@tonic-gate 25720Sstevel@tonic-gate /* 25730Sstevel@tonic-gate * Search the process symbol tables looking for a symbol whose name matches the 25740Sstevel@tonic-gate * specified name, but without any restriction on the link map id. 25750Sstevel@tonic-gate */ 25760Sstevel@tonic-gate int 25770Sstevel@tonic-gate Plookup_by_name(struct ps_prochandle *P, const char *object, 25780Sstevel@tonic-gate const char *symbol, GElf_Sym *symp) 25790Sstevel@tonic-gate { 25800Sstevel@tonic-gate return (Pxlookup_by_name(P, PR_LMID_EVERY, object, symbol, symp, NULL)); 25810Sstevel@tonic-gate } 25820Sstevel@tonic-gate 25830Sstevel@tonic-gate /* 25840Sstevel@tonic-gate * Iterate over the process's address space mappings. 25850Sstevel@tonic-gate */ 2586*7675SEdward.Pilatowicz@Sun.COM static int 2587*7675SEdward.Pilatowicz@Sun.COM i_Pmapping_iter(struct ps_prochandle *P, boolean_t lmresolve, 2588*7675SEdward.Pilatowicz@Sun.COM proc_map_f *func, void *cd) 25890Sstevel@tonic-gate { 25900Sstevel@tonic-gate map_info_t *mptr; 25910Sstevel@tonic-gate file_info_t *fptr; 25920Sstevel@tonic-gate char *object_name; 25930Sstevel@tonic-gate int rc = 0; 25940Sstevel@tonic-gate int i; 25950Sstevel@tonic-gate 25960Sstevel@tonic-gate /* create all the file_info_t's for all the mappings */ 25970Sstevel@tonic-gate (void) Prd_agent(P); 25980Sstevel@tonic-gate 25990Sstevel@tonic-gate for (i = 0, mptr = P->mappings; i < P->map_count; i++, mptr++) { 26000Sstevel@tonic-gate if ((fptr = mptr->map_file) == NULL) 26010Sstevel@tonic-gate object_name = NULL; 2602*7675SEdward.Pilatowicz@Sun.COM else if (lmresolve && (fptr->file_rname != NULL)) 2603*7675SEdward.Pilatowicz@Sun.COM object_name = fptr->file_rname; 26040Sstevel@tonic-gate else 26050Sstevel@tonic-gate object_name = fptr->file_lname; 26060Sstevel@tonic-gate if ((rc = func(cd, &mptr->map_pmap, object_name)) != 0) 26070Sstevel@tonic-gate return (rc); 26080Sstevel@tonic-gate } 26090Sstevel@tonic-gate return (0); 26100Sstevel@tonic-gate } 26110Sstevel@tonic-gate 2612*7675SEdward.Pilatowicz@Sun.COM int 2613*7675SEdward.Pilatowicz@Sun.COM Pmapping_iter(struct ps_prochandle *P, proc_map_f *func, void *cd) 2614*7675SEdward.Pilatowicz@Sun.COM { 2615*7675SEdward.Pilatowicz@Sun.COM return (i_Pmapping_iter(P, B_FALSE, func, cd)); 2616*7675SEdward.Pilatowicz@Sun.COM } 2617*7675SEdward.Pilatowicz@Sun.COM 2618*7675SEdward.Pilatowicz@Sun.COM int 2619*7675SEdward.Pilatowicz@Sun.COM Pmapping_iter_resolved(struct ps_prochandle *P, proc_map_f *func, void *cd) 2620*7675SEdward.Pilatowicz@Sun.COM { 2621*7675SEdward.Pilatowicz@Sun.COM return (i_Pmapping_iter(P, B_TRUE, func, cd)); 2622*7675SEdward.Pilatowicz@Sun.COM } 2623*7675SEdward.Pilatowicz@Sun.COM 26240Sstevel@tonic-gate /* 26250Sstevel@tonic-gate * Iterate over the process's mapped objects. 26260Sstevel@tonic-gate */ 2627*7675SEdward.Pilatowicz@Sun.COM static int 2628*7675SEdward.Pilatowicz@Sun.COM i_Pobject_iter(struct ps_prochandle *P, boolean_t lmresolve, 2629*7675SEdward.Pilatowicz@Sun.COM proc_map_f *func, void *cd) 26300Sstevel@tonic-gate { 26310Sstevel@tonic-gate map_info_t *mptr; 26320Sstevel@tonic-gate file_info_t *fptr; 26330Sstevel@tonic-gate uint_t cnt; 26340Sstevel@tonic-gate int rc = 0; 26350Sstevel@tonic-gate 26360Sstevel@tonic-gate (void) Prd_agent(P); /* create file_info_t's for all the mappings */ 26370Sstevel@tonic-gate Pupdate_maps(P); 26380Sstevel@tonic-gate 26390Sstevel@tonic-gate for (cnt = P->num_files, fptr = list_next(&P->file_head); 26400Sstevel@tonic-gate cnt; cnt--, fptr = list_next(fptr)) { 2641*7675SEdward.Pilatowicz@Sun.COM const char *lname; 2642*7675SEdward.Pilatowicz@Sun.COM 2643*7675SEdward.Pilatowicz@Sun.COM if (lmresolve && (fptr->file_rname != NULL)) 2644*7675SEdward.Pilatowicz@Sun.COM lname = fptr->file_rname; 2645*7675SEdward.Pilatowicz@Sun.COM else if (fptr->file_lname != NULL) 2646*7675SEdward.Pilatowicz@Sun.COM lname = fptr->file_lname; 2647*7675SEdward.Pilatowicz@Sun.COM else 2648*7675SEdward.Pilatowicz@Sun.COM lname = ""; 26490Sstevel@tonic-gate 26500Sstevel@tonic-gate if ((mptr = fptr->file_map) == NULL) 26510Sstevel@tonic-gate continue; 26520Sstevel@tonic-gate 26530Sstevel@tonic-gate if ((rc = func(cd, &mptr->map_pmap, lname)) != 0) 26540Sstevel@tonic-gate return (rc); 26550Sstevel@tonic-gate } 26560Sstevel@tonic-gate return (0); 26570Sstevel@tonic-gate } 26580Sstevel@tonic-gate 2659*7675SEdward.Pilatowicz@Sun.COM int 2660*7675SEdward.Pilatowicz@Sun.COM Pobject_iter(struct ps_prochandle *P, proc_map_f *func, void *cd) 2661*7675SEdward.Pilatowicz@Sun.COM { 2662*7675SEdward.Pilatowicz@Sun.COM return (i_Pobject_iter(P, B_FALSE, func, cd)); 2663*7675SEdward.Pilatowicz@Sun.COM } 2664*7675SEdward.Pilatowicz@Sun.COM 2665*7675SEdward.Pilatowicz@Sun.COM int 2666*7675SEdward.Pilatowicz@Sun.COM Pobject_iter_resolved(struct ps_prochandle *P, proc_map_f *func, void *cd) 2667*7675SEdward.Pilatowicz@Sun.COM { 2668*7675SEdward.Pilatowicz@Sun.COM return (i_Pobject_iter(P, B_TRUE, func, cd)); 2669*7675SEdward.Pilatowicz@Sun.COM } 2670*7675SEdward.Pilatowicz@Sun.COM 2671*7675SEdward.Pilatowicz@Sun.COM static char * 2672*7675SEdward.Pilatowicz@Sun.COM i_Pobjname(struct ps_prochandle *P, boolean_t lmresolve, uintptr_t addr, 26730Sstevel@tonic-gate char *buffer, size_t bufsize) 26740Sstevel@tonic-gate { 26750Sstevel@tonic-gate map_info_t *mptr; 26760Sstevel@tonic-gate file_info_t *fptr; 26770Sstevel@tonic-gate 26780Sstevel@tonic-gate /* create all the file_info_t's for all the mappings */ 26790Sstevel@tonic-gate (void) Prd_agent(P); 26800Sstevel@tonic-gate 2681*7675SEdward.Pilatowicz@Sun.COM if ((mptr = Paddr2mptr(P, addr)) == NULL) 2682*7675SEdward.Pilatowicz@Sun.COM return (NULL); 2683*7675SEdward.Pilatowicz@Sun.COM 2684*7675SEdward.Pilatowicz@Sun.COM if (!lmresolve) { 2685*7675SEdward.Pilatowicz@Sun.COM if (((fptr = mptr->map_file) == NULL) || 2686*7675SEdward.Pilatowicz@Sun.COM (fptr->file_lname == NULL)) 2687*7675SEdward.Pilatowicz@Sun.COM return (NULL); 2688*7675SEdward.Pilatowicz@Sun.COM (void) strlcpy(buffer, fptr->file_lname, bufsize); 26890Sstevel@tonic-gate return (buffer); 26900Sstevel@tonic-gate } 2691*7675SEdward.Pilatowicz@Sun.COM 2692*7675SEdward.Pilatowicz@Sun.COM /* Check for a cached copy of the resolved path */ 2693*7675SEdward.Pilatowicz@Sun.COM if (Pfindmap(P, mptr, buffer, bufsize) != NULL) 2694*7675SEdward.Pilatowicz@Sun.COM return (buffer); 2695*7675SEdward.Pilatowicz@Sun.COM 26960Sstevel@tonic-gate return (NULL); 26970Sstevel@tonic-gate } 26980Sstevel@tonic-gate 26990Sstevel@tonic-gate /* 2700*7675SEdward.Pilatowicz@Sun.COM * Given a virtual address, return the name of the underlying 2701*7675SEdward.Pilatowicz@Sun.COM * mapped object (file) as provided by the dynamic linker. 2702*7675SEdward.Pilatowicz@Sun.COM * Return NULL if we can't find any name information for the object. 2703*7675SEdward.Pilatowicz@Sun.COM */ 2704*7675SEdward.Pilatowicz@Sun.COM char * 2705*7675SEdward.Pilatowicz@Sun.COM Pobjname(struct ps_prochandle *P, uintptr_t addr, 2706*7675SEdward.Pilatowicz@Sun.COM char *buffer, size_t bufsize) 2707*7675SEdward.Pilatowicz@Sun.COM { 2708*7675SEdward.Pilatowicz@Sun.COM return (i_Pobjname(P, B_FALSE, addr, buffer, bufsize)); 2709*7675SEdward.Pilatowicz@Sun.COM } 2710*7675SEdward.Pilatowicz@Sun.COM 2711*7675SEdward.Pilatowicz@Sun.COM /* 2712*7675SEdward.Pilatowicz@Sun.COM * Given a virtual address, try to return a filesystem path to the 2713*7675SEdward.Pilatowicz@Sun.COM * underlying mapped object (file). If we're in the global zone, 2714*7675SEdward.Pilatowicz@Sun.COM * this path could resolve to an object in another zone. If we're 2715*7675SEdward.Pilatowicz@Sun.COM * unable return a valid filesystem path, we'll fall back to providing 2716*7675SEdward.Pilatowicz@Sun.COM * the mapped object (file) name provided by the dynamic linker in 2717*7675SEdward.Pilatowicz@Sun.COM * the target process (ie, the object reported by Pobjname()). 2718*7675SEdward.Pilatowicz@Sun.COM */ 2719*7675SEdward.Pilatowicz@Sun.COM char * 2720*7675SEdward.Pilatowicz@Sun.COM Pobjname_resolved(struct ps_prochandle *P, uintptr_t addr, 2721*7675SEdward.Pilatowicz@Sun.COM char *buffer, size_t bufsize) 2722*7675SEdward.Pilatowicz@Sun.COM { 2723*7675SEdward.Pilatowicz@Sun.COM return (i_Pobjname(P, B_TRUE, addr, buffer, bufsize)); 2724*7675SEdward.Pilatowicz@Sun.COM } 2725*7675SEdward.Pilatowicz@Sun.COM 2726*7675SEdward.Pilatowicz@Sun.COM /* 27270Sstevel@tonic-gate * Given a virtual address, return the link map id of the underlying mapped 27280Sstevel@tonic-gate * object (file), as provided by the dynamic linker. Return -1 on failure. 27290Sstevel@tonic-gate */ 27300Sstevel@tonic-gate int 27310Sstevel@tonic-gate Plmid(struct ps_prochandle *P, uintptr_t addr, Lmid_t *lmidp) 27320Sstevel@tonic-gate { 27330Sstevel@tonic-gate map_info_t *mptr; 27340Sstevel@tonic-gate file_info_t *fptr; 27350Sstevel@tonic-gate 27360Sstevel@tonic-gate /* create all the file_info_t's for all the mappings */ 27370Sstevel@tonic-gate (void) Prd_agent(P); 27380Sstevel@tonic-gate 27390Sstevel@tonic-gate if ((mptr = Paddr2mptr(P, addr)) != NULL && 27400Sstevel@tonic-gate (fptr = mptr->map_file) != NULL && fptr->file_lo != NULL) { 27410Sstevel@tonic-gate *lmidp = fptr->file_lo->rl_lmident; 27420Sstevel@tonic-gate return (0); 27430Sstevel@tonic-gate } 27440Sstevel@tonic-gate 27450Sstevel@tonic-gate return (-1); 27460Sstevel@tonic-gate } 27470Sstevel@tonic-gate 27480Sstevel@tonic-gate /* 27490Sstevel@tonic-gate * Given an object name and optional lmid, iterate over the object's symbols. 27500Sstevel@tonic-gate * If which == PR_SYMTAB, search the normal symbol table. 27510Sstevel@tonic-gate * If which == PR_DYNSYM, search the dynamic symbol table. 27520Sstevel@tonic-gate */ 27530Sstevel@tonic-gate static int 27540Sstevel@tonic-gate Psymbol_iter_com(struct ps_prochandle *P, Lmid_t lmid, const char *object_name, 27550Sstevel@tonic-gate int which, int mask, pr_order_t order, proc_xsym_f *func, void *cd) 27560Sstevel@tonic-gate { 27570Sstevel@tonic-gate GElf_Sym sym; 27581160Svb160487 GElf_Shdr shdr; 27590Sstevel@tonic-gate map_info_t *mptr; 27600Sstevel@tonic-gate file_info_t *fptr; 27610Sstevel@tonic-gate sym_tbl_t *symtab; 27620Sstevel@tonic-gate size_t symn; 27630Sstevel@tonic-gate const char *strs; 27640Sstevel@tonic-gate size_t strsz; 27650Sstevel@tonic-gate prsyminfo_t si; 27660Sstevel@tonic-gate int rv; 27670Sstevel@tonic-gate uint_t *map, i, count, ndx; 27680Sstevel@tonic-gate 27690Sstevel@tonic-gate if ((mptr = object_name_to_map(P, lmid, object_name)) == NULL) 27700Sstevel@tonic-gate return (-1); 27710Sstevel@tonic-gate 27720Sstevel@tonic-gate if ((fptr = build_map_symtab(P, mptr)) == NULL || /* no mapped file */ 27730Sstevel@tonic-gate fptr->file_elf == NULL) /* not an ELF file */ 27740Sstevel@tonic-gate return (-1); 27750Sstevel@tonic-gate 27760Sstevel@tonic-gate /* 27770Sstevel@tonic-gate * Search the specified symbol table. 27780Sstevel@tonic-gate */ 27790Sstevel@tonic-gate switch (which) { 27800Sstevel@tonic-gate case PR_SYMTAB: 27810Sstevel@tonic-gate symtab = &fptr->file_symtab; 27820Sstevel@tonic-gate si.prs_table = PR_SYMTAB; 27830Sstevel@tonic-gate break; 27840Sstevel@tonic-gate case PR_DYNSYM: 27850Sstevel@tonic-gate symtab = &fptr->file_dynsym; 27860Sstevel@tonic-gate si.prs_table = PR_DYNSYM; 27870Sstevel@tonic-gate break; 27880Sstevel@tonic-gate default: 27890Sstevel@tonic-gate return (-1); 27900Sstevel@tonic-gate } 27910Sstevel@tonic-gate 27920Sstevel@tonic-gate si.prs_object = object_name; 27930Sstevel@tonic-gate si.prs_lmid = fptr->file_lo == NULL ? 27940Sstevel@tonic-gate LM_ID_BASE : fptr->file_lo->rl_lmident; 27950Sstevel@tonic-gate 27960Sstevel@tonic-gate symn = symtab->sym_symn; 27970Sstevel@tonic-gate strs = symtab->sym_strs; 27980Sstevel@tonic-gate strsz = symtab->sym_strsz; 27990Sstevel@tonic-gate 28000Sstevel@tonic-gate switch (order) { 28010Sstevel@tonic-gate case PRO_NATURAL: 28020Sstevel@tonic-gate map = NULL; 28030Sstevel@tonic-gate count = symn; 28040Sstevel@tonic-gate break; 28050Sstevel@tonic-gate case PRO_BYNAME: 28060Sstevel@tonic-gate map = symtab->sym_byname; 28070Sstevel@tonic-gate count = symtab->sym_count; 28080Sstevel@tonic-gate break; 28090Sstevel@tonic-gate case PRO_BYADDR: 28100Sstevel@tonic-gate map = symtab->sym_byaddr; 28110Sstevel@tonic-gate count = symtab->sym_count; 28120Sstevel@tonic-gate break; 28130Sstevel@tonic-gate default: 28140Sstevel@tonic-gate return (-1); 28150Sstevel@tonic-gate } 28160Sstevel@tonic-gate 28173347Sab196087 if (symtab->sym_data_pri == NULL || strs == NULL || count == 0) 28183347Sab196087 return (-1); 28193347Sab196087 28200Sstevel@tonic-gate rv = 0; 28210Sstevel@tonic-gate 28220Sstevel@tonic-gate for (i = 0; i < count; i++) { 28230Sstevel@tonic-gate ndx = map == NULL ? i : map[i]; 28243347Sab196087 if (symtab_getsym(symtab, ndx, &sym) != NULL) { 28250Sstevel@tonic-gate uint_t s_bind, s_type, type; 28260Sstevel@tonic-gate 28270Sstevel@tonic-gate if (sym.st_name >= strsz) /* invalid st_name */ 28280Sstevel@tonic-gate continue; 28290Sstevel@tonic-gate 28300Sstevel@tonic-gate s_bind = GELF_ST_BIND(sym.st_info); 28310Sstevel@tonic-gate s_type = GELF_ST_TYPE(sym.st_info); 28320Sstevel@tonic-gate 28330Sstevel@tonic-gate /* 28340Sstevel@tonic-gate * In case you haven't already guessed, this relies on 28350Sstevel@tonic-gate * the bitmask used in <libproc.h> for encoding symbol 28360Sstevel@tonic-gate * type and binding matching the order of STB and STT 28370Sstevel@tonic-gate * constants in <sys/elf.h>. ELF can't change without 28380Sstevel@tonic-gate * breaking binary compatibility, so I think this is 28390Sstevel@tonic-gate * reasonably fair game. 28400Sstevel@tonic-gate */ 28410Sstevel@tonic-gate if (s_bind < STB_NUM && s_type < STT_NUM) { 28420Sstevel@tonic-gate type = (1 << (s_type + 8)) | (1 << s_bind); 28430Sstevel@tonic-gate if ((type & ~mask) != 0) 28440Sstevel@tonic-gate continue; 28450Sstevel@tonic-gate } else 28460Sstevel@tonic-gate continue; /* Invalid type or binding */ 28470Sstevel@tonic-gate 28480Sstevel@tonic-gate if (GELF_ST_TYPE(sym.st_info) != STT_TLS) 28490Sstevel@tonic-gate sym.st_value += fptr->file_dyn_base; 28500Sstevel@tonic-gate 28510Sstevel@tonic-gate si.prs_name = strs + sym.st_name; 28521160Svb160487 28531160Svb160487 /* 28541160Svb160487 * If symbol's type is STT_SECTION, then try to lookup 28551160Svb160487 * the name of the corresponding section. 28561160Svb160487 */ 28571160Svb160487 if (GELF_ST_TYPE(sym.st_info) == STT_SECTION && 28581160Svb160487 fptr->file_shstrs != NULL && 28591160Svb160487 gelf_getshdr(elf_getscn(fptr->file_elf, 28601160Svb160487 sym.st_shndx), &shdr) != NULL && 28611160Svb160487 shdr.sh_name != 0 && 28621160Svb160487 shdr.sh_name < fptr->file_shstrsz) 28631160Svb160487 si.prs_name = fptr->file_shstrs + shdr.sh_name; 28641160Svb160487 28650Sstevel@tonic-gate si.prs_id = ndx; 28661160Svb160487 if ((rv = func(cd, &sym, si.prs_name, &si)) != 0) 28670Sstevel@tonic-gate break; 28680Sstevel@tonic-gate } 28690Sstevel@tonic-gate } 28700Sstevel@tonic-gate 28710Sstevel@tonic-gate return (rv); 28720Sstevel@tonic-gate } 28730Sstevel@tonic-gate 28740Sstevel@tonic-gate int 28750Sstevel@tonic-gate Pxsymbol_iter(struct ps_prochandle *P, Lmid_t lmid, const char *object_name, 28760Sstevel@tonic-gate int which, int mask, proc_xsym_f *func, void *cd) 28770Sstevel@tonic-gate { 28780Sstevel@tonic-gate return (Psymbol_iter_com(P, lmid, object_name, which, mask, 28790Sstevel@tonic-gate PRO_NATURAL, func, cd)); 28800Sstevel@tonic-gate } 28810Sstevel@tonic-gate 28820Sstevel@tonic-gate int 28830Sstevel@tonic-gate Psymbol_iter_by_lmid(struct ps_prochandle *P, Lmid_t lmid, 28840Sstevel@tonic-gate const char *object_name, int which, int mask, proc_sym_f *func, void *cd) 28850Sstevel@tonic-gate { 28860Sstevel@tonic-gate return (Psymbol_iter_com(P, lmid, object_name, which, mask, 28870Sstevel@tonic-gate PRO_NATURAL, (proc_xsym_f *)func, cd)); 28880Sstevel@tonic-gate } 28890Sstevel@tonic-gate 28900Sstevel@tonic-gate int 28910Sstevel@tonic-gate Psymbol_iter(struct ps_prochandle *P, 28920Sstevel@tonic-gate const char *object_name, int which, int mask, proc_sym_f *func, void *cd) 28930Sstevel@tonic-gate { 28940Sstevel@tonic-gate return (Psymbol_iter_com(P, PR_LMID_EVERY, object_name, which, mask, 28950Sstevel@tonic-gate PRO_NATURAL, (proc_xsym_f *)func, cd)); 28960Sstevel@tonic-gate } 28970Sstevel@tonic-gate 28980Sstevel@tonic-gate int 28990Sstevel@tonic-gate Psymbol_iter_by_addr(struct ps_prochandle *P, 29000Sstevel@tonic-gate const char *object_name, int which, int mask, proc_sym_f *func, void *cd) 29010Sstevel@tonic-gate { 29020Sstevel@tonic-gate return (Psymbol_iter_com(P, PR_LMID_EVERY, object_name, which, mask, 29030Sstevel@tonic-gate PRO_BYADDR, (proc_xsym_f *)func, cd)); 29040Sstevel@tonic-gate } 29050Sstevel@tonic-gate 29060Sstevel@tonic-gate int 29070Sstevel@tonic-gate Psymbol_iter_by_name(struct ps_prochandle *P, 29080Sstevel@tonic-gate const char *object_name, int which, int mask, proc_sym_f *func, void *cd) 29090Sstevel@tonic-gate { 29100Sstevel@tonic-gate return (Psymbol_iter_com(P, PR_LMID_EVERY, object_name, which, mask, 29110Sstevel@tonic-gate PRO_BYNAME, (proc_xsym_f *)func, cd)); 29120Sstevel@tonic-gate } 29130Sstevel@tonic-gate 29140Sstevel@tonic-gate /* 29150Sstevel@tonic-gate * Get the platform string from the core file if we have it; 29160Sstevel@tonic-gate * just perform the system call for the caller if this is a live process. 29170Sstevel@tonic-gate */ 29180Sstevel@tonic-gate char * 29190Sstevel@tonic-gate Pplatform(struct ps_prochandle *P, char *s, size_t n) 29200Sstevel@tonic-gate { 29210Sstevel@tonic-gate if (P->state == PS_IDLE) { 29220Sstevel@tonic-gate errno = ENODATA; 29230Sstevel@tonic-gate return (NULL); 29240Sstevel@tonic-gate } 29250Sstevel@tonic-gate 29260Sstevel@tonic-gate if (P->state == PS_DEAD) { 29270Sstevel@tonic-gate if (P->core->core_platform == NULL) { 29280Sstevel@tonic-gate errno = ENODATA; 29290Sstevel@tonic-gate return (NULL); 29300Sstevel@tonic-gate } 29310Sstevel@tonic-gate (void) strncpy(s, P->core->core_platform, n - 1); 29320Sstevel@tonic-gate s[n - 1] = '\0'; 29330Sstevel@tonic-gate 29340Sstevel@tonic-gate } else if (sysinfo(SI_PLATFORM, s, n) == -1) 29350Sstevel@tonic-gate return (NULL); 29360Sstevel@tonic-gate 29370Sstevel@tonic-gate return (s); 29380Sstevel@tonic-gate } 29390Sstevel@tonic-gate 29400Sstevel@tonic-gate /* 29410Sstevel@tonic-gate * Get the uname(2) information from the core file if we have it; 29420Sstevel@tonic-gate * just perform the system call for the caller if this is a live process. 29430Sstevel@tonic-gate */ 29440Sstevel@tonic-gate int 29450Sstevel@tonic-gate Puname(struct ps_prochandle *P, struct utsname *u) 29460Sstevel@tonic-gate { 29470Sstevel@tonic-gate if (P->state == PS_IDLE) { 29480Sstevel@tonic-gate errno = ENODATA; 29490Sstevel@tonic-gate return (-1); 29500Sstevel@tonic-gate } 29510Sstevel@tonic-gate 29520Sstevel@tonic-gate if (P->state == PS_DEAD) { 29530Sstevel@tonic-gate if (P->core->core_uts == NULL) { 29540Sstevel@tonic-gate errno = ENODATA; 29550Sstevel@tonic-gate return (-1); 29560Sstevel@tonic-gate } 29570Sstevel@tonic-gate (void) memcpy(u, P->core->core_uts, sizeof (struct utsname)); 29580Sstevel@tonic-gate return (0); 29590Sstevel@tonic-gate } 29600Sstevel@tonic-gate return (uname(u)); 29610Sstevel@tonic-gate } 29620Sstevel@tonic-gate 29630Sstevel@tonic-gate /* 29640Sstevel@tonic-gate * Called from Pcreate(), Pgrab(), and Pfgrab_core() to initialize 29650Sstevel@tonic-gate * the symbol table heads in the new ps_prochandle. 29660Sstevel@tonic-gate */ 29670Sstevel@tonic-gate void 29680Sstevel@tonic-gate Pinitsym(struct ps_prochandle *P) 29690Sstevel@tonic-gate { 29700Sstevel@tonic-gate P->num_files = 0; 29710Sstevel@tonic-gate list_link(&P->file_head, NULL); 29720Sstevel@tonic-gate } 29730Sstevel@tonic-gate 29740Sstevel@tonic-gate /* 29750Sstevel@tonic-gate * Called from Prelease() to destroy the symbol tables. 29760Sstevel@tonic-gate * Must be called by the client after an exec() in the victim process. 29770Sstevel@tonic-gate */ 29780Sstevel@tonic-gate void 29790Sstevel@tonic-gate Preset_maps(struct ps_prochandle *P) 29800Sstevel@tonic-gate { 29810Sstevel@tonic-gate int i; 29820Sstevel@tonic-gate 29830Sstevel@tonic-gate if (P->rap != NULL) { 29840Sstevel@tonic-gate rd_delete(P->rap); 29850Sstevel@tonic-gate P->rap = NULL; 29860Sstevel@tonic-gate } 29870Sstevel@tonic-gate 29880Sstevel@tonic-gate if (P->execname != NULL) { 29890Sstevel@tonic-gate free(P->execname); 29900Sstevel@tonic-gate P->execname = NULL; 29910Sstevel@tonic-gate } 29920Sstevel@tonic-gate 29930Sstevel@tonic-gate if (P->auxv != NULL) { 29940Sstevel@tonic-gate free(P->auxv); 29950Sstevel@tonic-gate P->auxv = NULL; 29960Sstevel@tonic-gate P->nauxv = 0; 29970Sstevel@tonic-gate } 29980Sstevel@tonic-gate 29990Sstevel@tonic-gate for (i = 0; i < P->map_count; i++) 30000Sstevel@tonic-gate map_info_free(P, &P->mappings[i]); 30010Sstevel@tonic-gate 30020Sstevel@tonic-gate if (P->mappings != NULL) { 30030Sstevel@tonic-gate free(P->mappings); 30040Sstevel@tonic-gate P->mappings = NULL; 30050Sstevel@tonic-gate } 30060Sstevel@tonic-gate P->map_count = P->map_alloc = 0; 30070Sstevel@tonic-gate 30080Sstevel@tonic-gate P->info_valid = 0; 30090Sstevel@tonic-gate } 30100Sstevel@tonic-gate 30110Sstevel@tonic-gate typedef struct getenv_data { 30120Sstevel@tonic-gate char *buf; 30130Sstevel@tonic-gate size_t bufsize; 30140Sstevel@tonic-gate const char *search; 30150Sstevel@tonic-gate size_t searchlen; 30160Sstevel@tonic-gate } getenv_data_t; 30170Sstevel@tonic-gate 30180Sstevel@tonic-gate /*ARGSUSED*/ 30190Sstevel@tonic-gate static int 30200Sstevel@tonic-gate getenv_func(void *data, struct ps_prochandle *P, uintptr_t addr, 30210Sstevel@tonic-gate const char *nameval) 30220Sstevel@tonic-gate { 30230Sstevel@tonic-gate getenv_data_t *d = data; 30240Sstevel@tonic-gate size_t len; 30250Sstevel@tonic-gate 30260Sstevel@tonic-gate if (nameval == NULL) 30270Sstevel@tonic-gate return (0); 30280Sstevel@tonic-gate 30290Sstevel@tonic-gate if (d->searchlen < strlen(nameval) && 30300Sstevel@tonic-gate strncmp(nameval, d->search, d->searchlen) == 0 && 30310Sstevel@tonic-gate nameval[d->searchlen] == '=') { 30320Sstevel@tonic-gate len = MIN(strlen(nameval), d->bufsize - 1); 30330Sstevel@tonic-gate (void) strncpy(d->buf, nameval, len); 30340Sstevel@tonic-gate d->buf[len] = '\0'; 30350Sstevel@tonic-gate return (1); 30360Sstevel@tonic-gate } 30370Sstevel@tonic-gate 30380Sstevel@tonic-gate return (0); 30390Sstevel@tonic-gate } 30400Sstevel@tonic-gate 30410Sstevel@tonic-gate char * 30420Sstevel@tonic-gate Pgetenv(struct ps_prochandle *P, const char *name, char *buf, size_t buflen) 30430Sstevel@tonic-gate { 30440Sstevel@tonic-gate getenv_data_t d; 30450Sstevel@tonic-gate 30460Sstevel@tonic-gate d.buf = buf; 30470Sstevel@tonic-gate d.bufsize = buflen; 30480Sstevel@tonic-gate d.search = name; 30490Sstevel@tonic-gate d.searchlen = strlen(name); 30500Sstevel@tonic-gate 30510Sstevel@tonic-gate if (Penv_iter(P, getenv_func, &d) == 1) { 30520Sstevel@tonic-gate char *equals = strchr(d.buf, '='); 30530Sstevel@tonic-gate 30540Sstevel@tonic-gate if (equals != NULL) { 30550Sstevel@tonic-gate (void) memmove(d.buf, equals + 1, 30560Sstevel@tonic-gate d.buf + buflen - equals - 1); 30570Sstevel@tonic-gate d.buf[d.buf + buflen - equals] = '\0'; 30580Sstevel@tonic-gate 30590Sstevel@tonic-gate return (buf); 30600Sstevel@tonic-gate } 30610Sstevel@tonic-gate } 30620Sstevel@tonic-gate 30630Sstevel@tonic-gate return (NULL); 30640Sstevel@tonic-gate } 30650Sstevel@tonic-gate 30660Sstevel@tonic-gate /* number of argument or environment pointers to read all at once */ 30670Sstevel@tonic-gate #define NARG 100 30680Sstevel@tonic-gate 30690Sstevel@tonic-gate int 30700Sstevel@tonic-gate Penv_iter(struct ps_prochandle *P, proc_env_f *func, void *data) 30710Sstevel@tonic-gate { 30720Sstevel@tonic-gate const psinfo_t *psp; 30730Sstevel@tonic-gate uintptr_t envpoff; 30740Sstevel@tonic-gate GElf_Sym sym; 30750Sstevel@tonic-gate int ret; 30760Sstevel@tonic-gate char *buf, *nameval; 30770Sstevel@tonic-gate size_t buflen; 30780Sstevel@tonic-gate 30790Sstevel@tonic-gate int nenv = NARG; 30800Sstevel@tonic-gate long envp[NARG]; 30810Sstevel@tonic-gate 30820Sstevel@tonic-gate /* 30830Sstevel@tonic-gate * Attempt to find the "_environ" variable in the process. 30840Sstevel@tonic-gate * Failing that, use the original value provided by Ppsinfo(). 30850Sstevel@tonic-gate */ 30860Sstevel@tonic-gate if ((psp = Ppsinfo(P)) == NULL) 30870Sstevel@tonic-gate return (-1); 30880Sstevel@tonic-gate 30890Sstevel@tonic-gate envpoff = psp->pr_envp; /* Default if no _environ found */ 30900Sstevel@tonic-gate 30910Sstevel@tonic-gate if (Plookup_by_name(P, PR_OBJ_EXEC, "_environ", &sym) == 0) { 30920Sstevel@tonic-gate if (P->status.pr_dmodel == PR_MODEL_NATIVE) { 30930Sstevel@tonic-gate if (Pread(P, &envpoff, sizeof (envpoff), 30940Sstevel@tonic-gate sym.st_value) != sizeof (envpoff)) 30950Sstevel@tonic-gate envpoff = psp->pr_envp; 30960Sstevel@tonic-gate } else if (P->status.pr_dmodel == PR_MODEL_ILP32) { 30970Sstevel@tonic-gate uint32_t envpoff32; 30980Sstevel@tonic-gate 30990Sstevel@tonic-gate if (Pread(P, &envpoff32, sizeof (envpoff32), 31000Sstevel@tonic-gate sym.st_value) != sizeof (envpoff32)) 31010Sstevel@tonic-gate envpoff = psp->pr_envp; 31020Sstevel@tonic-gate else 31030Sstevel@tonic-gate envpoff = envpoff32; 31040Sstevel@tonic-gate } 31050Sstevel@tonic-gate } 31060Sstevel@tonic-gate 31070Sstevel@tonic-gate buflen = 128; 31080Sstevel@tonic-gate buf = malloc(buflen); 31090Sstevel@tonic-gate 31100Sstevel@tonic-gate ret = 0; 31110Sstevel@tonic-gate for (;;) { 31120Sstevel@tonic-gate uintptr_t envoff; 31130Sstevel@tonic-gate 31140Sstevel@tonic-gate if (nenv == NARG) { 31150Sstevel@tonic-gate (void) memset(envp, 0, sizeof (envp)); 31160Sstevel@tonic-gate if (P->status.pr_dmodel == PR_MODEL_NATIVE) { 31170Sstevel@tonic-gate if (Pread(P, envp, 31180Sstevel@tonic-gate sizeof (envp), envpoff) <= 0) { 31190Sstevel@tonic-gate ret = -1; 31200Sstevel@tonic-gate break; 31210Sstevel@tonic-gate } 31220Sstevel@tonic-gate } else if (P->status.pr_dmodel == PR_MODEL_ILP32) { 31230Sstevel@tonic-gate uint32_t e32[NARG]; 31240Sstevel@tonic-gate int i; 31250Sstevel@tonic-gate 31260Sstevel@tonic-gate (void) memset(e32, 0, sizeof (e32)); 31270Sstevel@tonic-gate if (Pread(P, e32, sizeof (e32), envpoff) <= 0) { 31280Sstevel@tonic-gate ret = -1; 31290Sstevel@tonic-gate break; 31300Sstevel@tonic-gate } 31310Sstevel@tonic-gate for (i = 0; i < NARG; i++) 31320Sstevel@tonic-gate envp[i] = e32[i]; 31330Sstevel@tonic-gate } 31340Sstevel@tonic-gate nenv = 0; 31350Sstevel@tonic-gate } 31360Sstevel@tonic-gate 31370Sstevel@tonic-gate if ((envoff = envp[nenv++]) == NULL) 31380Sstevel@tonic-gate break; 31390Sstevel@tonic-gate 31400Sstevel@tonic-gate /* 31410Sstevel@tonic-gate * Attempt to read the string from the process. 31420Sstevel@tonic-gate */ 31430Sstevel@tonic-gate again: 31440Sstevel@tonic-gate ret = Pread_string(P, buf, buflen, envoff); 31450Sstevel@tonic-gate 31460Sstevel@tonic-gate if (ret <= 0) { 31470Sstevel@tonic-gate nameval = NULL; 31480Sstevel@tonic-gate } else if (ret == buflen - 1) { 31490Sstevel@tonic-gate free(buf); 31500Sstevel@tonic-gate /* 31510Sstevel@tonic-gate * Bail if we have a corrupted environment 31520Sstevel@tonic-gate */ 31530Sstevel@tonic-gate if (buflen >= ARG_MAX) 31540Sstevel@tonic-gate return (-1); 31550Sstevel@tonic-gate buflen *= 2; 31560Sstevel@tonic-gate buf = malloc(buflen); 31570Sstevel@tonic-gate goto again; 31580Sstevel@tonic-gate } else { 31590Sstevel@tonic-gate nameval = buf; 31600Sstevel@tonic-gate } 31610Sstevel@tonic-gate 31620Sstevel@tonic-gate if ((ret = func(data, P, envoff, nameval)) != 0) 31630Sstevel@tonic-gate break; 31640Sstevel@tonic-gate 31650Sstevel@tonic-gate envpoff += (P->status.pr_dmodel == PR_MODEL_LP64)? 8 : 4; 31660Sstevel@tonic-gate } 31670Sstevel@tonic-gate 31680Sstevel@tonic-gate free(buf); 31690Sstevel@tonic-gate 31700Sstevel@tonic-gate return (ret); 31710Sstevel@tonic-gate } 3172