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 50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 70Sstevel@tonic-gate * with the License. 80Sstevel@tonic-gate * 90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 110Sstevel@tonic-gate * See the License for the specific language governing permissions 120Sstevel@tonic-gate * and limitations under the License. 130Sstevel@tonic-gate * 140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 190Sstevel@tonic-gate * 200Sstevel@tonic-gate * CDDL HEADER END 210Sstevel@tonic-gate */ 220Sstevel@tonic-gate /* 230Sstevel@tonic-gate * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 270Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 280Sstevel@tonic-gate 290Sstevel@tonic-gate #include <stdio.h> 300Sstevel@tonic-gate #include <stdlib.h> 310Sstevel@tonic-gate #include <stddef.h> 320Sstevel@tonic-gate #include <unistd.h> 330Sstevel@tonic-gate #include <ctype.h> 340Sstevel@tonic-gate #include <fcntl.h> 350Sstevel@tonic-gate #include <string.h> 360Sstevel@tonic-gate #include <strings.h> 370Sstevel@tonic-gate #include <memory.h> 380Sstevel@tonic-gate #include <errno.h> 390Sstevel@tonic-gate #include <dirent.h> 400Sstevel@tonic-gate #include <signal.h> 410Sstevel@tonic-gate #include <limits.h> 420Sstevel@tonic-gate #include <libgen.h> 430Sstevel@tonic-gate #include <zone.h> 440Sstevel@tonic-gate #include <sys/types.h> 450Sstevel@tonic-gate #include <sys/stat.h> 460Sstevel@tonic-gate #include <sys/systeminfo.h> 470Sstevel@tonic-gate #include <sys/sysmacros.h> 480Sstevel@tonic-gate 490Sstevel@tonic-gate #include "libproc.h" 500Sstevel@tonic-gate #include "Pcontrol.h" 510Sstevel@tonic-gate #include "Putil.h" 520Sstevel@tonic-gate 530Sstevel@tonic-gate static file_info_t *build_map_symtab(struct ps_prochandle *, map_info_t *); 540Sstevel@tonic-gate static map_info_t *exec_map(struct ps_prochandle *); 550Sstevel@tonic-gate static map_info_t *object_to_map(struct ps_prochandle *, Lmid_t, const char *); 560Sstevel@tonic-gate static map_info_t *object_name_to_map(struct ps_prochandle *, 570Sstevel@tonic-gate Lmid_t, const char *); 580Sstevel@tonic-gate static GElf_Sym *sym_by_name(sym_tbl_t *, const char *, GElf_Sym *, uint_t *); 590Sstevel@tonic-gate static int read_ehdr32(struct ps_prochandle *, Elf32_Ehdr *, uintptr_t); 600Sstevel@tonic-gate #ifdef _LP64 610Sstevel@tonic-gate static int read_ehdr64(struct ps_prochandle *, Elf64_Ehdr *, uintptr_t); 620Sstevel@tonic-gate #endif 630Sstevel@tonic-gate 640Sstevel@tonic-gate #define DATA_TYPES \ 650Sstevel@tonic-gate ((1 << STT_OBJECT) | (1 << STT_FUNC) | \ 660Sstevel@tonic-gate (1 << STT_COMMON) | (1 << STT_TLS)) 670Sstevel@tonic-gate #define IS_DATA_TYPE(tp) (((1 << (tp)) & DATA_TYPES) != 0) 680Sstevel@tonic-gate 690Sstevel@tonic-gate #define MA_RWX (MA_READ | MA_WRITE | MA_EXEC) 700Sstevel@tonic-gate 710Sstevel@tonic-gate typedef enum { 720Sstevel@tonic-gate PRO_NATURAL, 730Sstevel@tonic-gate PRO_BYADDR, 740Sstevel@tonic-gate PRO_BYNAME 750Sstevel@tonic-gate } pr_order_t; 760Sstevel@tonic-gate 770Sstevel@tonic-gate static int 780Sstevel@tonic-gate addr_cmp(const void *aa, const void *bb) 790Sstevel@tonic-gate { 800Sstevel@tonic-gate uintptr_t a = *((uintptr_t *)aa); 810Sstevel@tonic-gate uintptr_t b = *((uintptr_t *)bb); 820Sstevel@tonic-gate 830Sstevel@tonic-gate if (a > b) 840Sstevel@tonic-gate return (1); 850Sstevel@tonic-gate if (a < b) 860Sstevel@tonic-gate return (-1); 870Sstevel@tonic-gate return (0); 880Sstevel@tonic-gate } 890Sstevel@tonic-gate 900Sstevel@tonic-gate /* 910Sstevel@tonic-gate * Allocation function for a new file_info_t 920Sstevel@tonic-gate */ 930Sstevel@tonic-gate static file_info_t * 940Sstevel@tonic-gate file_info_new(struct ps_prochandle *P, map_info_t *mptr) 950Sstevel@tonic-gate { 960Sstevel@tonic-gate file_info_t *fptr; 970Sstevel@tonic-gate map_info_t *mp; 980Sstevel@tonic-gate uintptr_t a, addr, *addrs, last = 0; 990Sstevel@tonic-gate uint_t i, j, naddrs = 0, unordered = 0; 1000Sstevel@tonic-gate 1010Sstevel@tonic-gate if ((fptr = calloc(1, sizeof (file_info_t))) == NULL) 1020Sstevel@tonic-gate return (NULL); 1030Sstevel@tonic-gate 1040Sstevel@tonic-gate list_link(fptr, &P->file_head); 1050Sstevel@tonic-gate (void) strcpy(fptr->file_pname, mptr->map_pmap.pr_mapname); 1060Sstevel@tonic-gate mptr->map_file = fptr; 1070Sstevel@tonic-gate fptr->file_ref = 1; 1080Sstevel@tonic-gate fptr->file_fd = -1; 1090Sstevel@tonic-gate P->num_files++; 1100Sstevel@tonic-gate 1110Sstevel@tonic-gate /* 1120Sstevel@tonic-gate * To figure out which map_info_t instances correspond to the mappings 1130Sstevel@tonic-gate * for this load object, we look at the in-memory ELF image in the 1140Sstevel@tonic-gate * base mapping (usually the program text). We examine the program 1150Sstevel@tonic-gate * headers to find the addresses at the beginning and end of each 1160Sstevel@tonic-gate * section and store them in a list which we then sort. Finally, we 1170Sstevel@tonic-gate * walk down the list of addresses and the list of map_info_t 1180Sstevel@tonic-gate * instances in lock step to correctly find the mappings that 1190Sstevel@tonic-gate * correspond to this load object. 1200Sstevel@tonic-gate */ 1210Sstevel@tonic-gate if (P->status.pr_dmodel == PR_MODEL_ILP32) { 1220Sstevel@tonic-gate Elf32_Ehdr ehdr; 1230Sstevel@tonic-gate Elf32_Phdr phdr; 1240Sstevel@tonic-gate 1250Sstevel@tonic-gate if (read_ehdr32(P, &ehdr, mptr->map_pmap.pr_vaddr) != 0) 1260Sstevel@tonic-gate return (fptr); 1270Sstevel@tonic-gate 1280Sstevel@tonic-gate addrs = malloc(sizeof (uintptr_t) * ehdr.e_phnum * 2); 1290Sstevel@tonic-gate a = mptr->map_pmap.pr_vaddr + ehdr.e_phoff; 1300Sstevel@tonic-gate for (i = 0; i < ehdr.e_phnum; i++, a += ehdr.e_phentsize) { 1310Sstevel@tonic-gate if (Pread(P, &phdr, sizeof (phdr), a) != sizeof (phdr)) 1320Sstevel@tonic-gate goto out; 1330Sstevel@tonic-gate if (phdr.p_type != PT_LOAD || phdr.p_memsz == 0) 1340Sstevel@tonic-gate continue; 1350Sstevel@tonic-gate 1360Sstevel@tonic-gate addr = phdr.p_vaddr; 1370Sstevel@tonic-gate if (ehdr.e_type == ET_DYN) 1380Sstevel@tonic-gate addr += mptr->map_pmap.pr_vaddr; 1390Sstevel@tonic-gate if (last > addr) 1400Sstevel@tonic-gate unordered = 1; 1410Sstevel@tonic-gate addrs[naddrs++] = addr; 1420Sstevel@tonic-gate addrs[naddrs++] = last = addr + phdr.p_memsz - 1; 1430Sstevel@tonic-gate } 1440Sstevel@tonic-gate #ifdef _LP64 1450Sstevel@tonic-gate } else { 1460Sstevel@tonic-gate Elf64_Ehdr ehdr; 1470Sstevel@tonic-gate Elf64_Phdr phdr; 1480Sstevel@tonic-gate 1490Sstevel@tonic-gate if (read_ehdr64(P, &ehdr, mptr->map_pmap.pr_vaddr) != 0) 1500Sstevel@tonic-gate return (fptr); 1510Sstevel@tonic-gate 1520Sstevel@tonic-gate addrs = malloc(sizeof (uintptr_t) * ehdr.e_phnum * 2); 1530Sstevel@tonic-gate a = mptr->map_pmap.pr_vaddr + ehdr.e_phoff; 1540Sstevel@tonic-gate for (i = 0; i < ehdr.e_phnum; i++, a += ehdr.e_phentsize) { 1550Sstevel@tonic-gate if (Pread(P, &phdr, sizeof (phdr), a) != sizeof (phdr)) 1560Sstevel@tonic-gate goto out; 1570Sstevel@tonic-gate if (phdr.p_type != PT_LOAD || phdr.p_memsz == 0) 1580Sstevel@tonic-gate continue; 1590Sstevel@tonic-gate 1600Sstevel@tonic-gate addr = phdr.p_vaddr; 1610Sstevel@tonic-gate if (ehdr.e_type == ET_DYN) 1620Sstevel@tonic-gate addr += mptr->map_pmap.pr_vaddr; 1630Sstevel@tonic-gate if (last > addr) 1640Sstevel@tonic-gate unordered = 1; 1650Sstevel@tonic-gate addrs[naddrs++] = addr; 1660Sstevel@tonic-gate addrs[naddrs++] = last = addr + phdr.p_memsz - 1; 1670Sstevel@tonic-gate } 1680Sstevel@tonic-gate #endif 1690Sstevel@tonic-gate } 1700Sstevel@tonic-gate 1710Sstevel@tonic-gate if (unordered) 1720Sstevel@tonic-gate qsort(addrs, naddrs, sizeof (uintptr_t), addr_cmp); 1730Sstevel@tonic-gate 1740Sstevel@tonic-gate 1750Sstevel@tonic-gate i = j = 0; 1760Sstevel@tonic-gate mp = P->mappings; 1770Sstevel@tonic-gate while (j < P->map_count && i < naddrs) { 1780Sstevel@tonic-gate addr = addrs[i]; 1790Sstevel@tonic-gate if (addr >= mp->map_pmap.pr_vaddr && 1800Sstevel@tonic-gate addr < mp->map_pmap.pr_vaddr + mp->map_pmap.pr_size && 1810Sstevel@tonic-gate mp->map_file == NULL) { 1820Sstevel@tonic-gate mp->map_file = fptr; 1830Sstevel@tonic-gate fptr->file_ref++; 1840Sstevel@tonic-gate } 1850Sstevel@tonic-gate 1860Sstevel@tonic-gate if (addr < mp->map_pmap.pr_vaddr + mp->map_pmap.pr_size) { 1870Sstevel@tonic-gate i++; 1880Sstevel@tonic-gate } else { 1890Sstevel@tonic-gate mp++; 1900Sstevel@tonic-gate j++; 1910Sstevel@tonic-gate } 1920Sstevel@tonic-gate } 1930Sstevel@tonic-gate 1940Sstevel@tonic-gate out: 1950Sstevel@tonic-gate free(addrs); 1960Sstevel@tonic-gate return (fptr); 1970Sstevel@tonic-gate } 1980Sstevel@tonic-gate 1990Sstevel@tonic-gate /* 2000Sstevel@tonic-gate * Deallocation function for a file_info_t 2010Sstevel@tonic-gate */ 2020Sstevel@tonic-gate static void 2030Sstevel@tonic-gate file_info_free(struct ps_prochandle *P, file_info_t *fptr) 2040Sstevel@tonic-gate { 2050Sstevel@tonic-gate if (--fptr->file_ref == 0) { 2060Sstevel@tonic-gate list_unlink(fptr); 2070Sstevel@tonic-gate if (fptr->file_symtab.sym_elf) { 2080Sstevel@tonic-gate (void) elf_end(fptr->file_symtab.sym_elf); 2090Sstevel@tonic-gate free(fptr->file_symtab.sym_elfmem); 2100Sstevel@tonic-gate } 2110Sstevel@tonic-gate if (fptr->file_symtab.sym_byname) 2120Sstevel@tonic-gate free(fptr->file_symtab.sym_byname); 2130Sstevel@tonic-gate if (fptr->file_symtab.sym_byaddr) 2140Sstevel@tonic-gate free(fptr->file_symtab.sym_byaddr); 2150Sstevel@tonic-gate 2160Sstevel@tonic-gate if (fptr->file_dynsym.sym_elf) { 2170Sstevel@tonic-gate (void) elf_end(fptr->file_dynsym.sym_elf); 2180Sstevel@tonic-gate free(fptr->file_dynsym.sym_elfmem); 2190Sstevel@tonic-gate } 2200Sstevel@tonic-gate if (fptr->file_dynsym.sym_byname) 2210Sstevel@tonic-gate free(fptr->file_dynsym.sym_byname); 2220Sstevel@tonic-gate if (fptr->file_dynsym.sym_byaddr) 2230Sstevel@tonic-gate free(fptr->file_dynsym.sym_byaddr); 2240Sstevel@tonic-gate 2250Sstevel@tonic-gate if (fptr->file_lo) 2260Sstevel@tonic-gate free(fptr->file_lo); 2270Sstevel@tonic-gate if (fptr->file_lname) 2280Sstevel@tonic-gate free(fptr->file_lname); 2290Sstevel@tonic-gate if (fptr->file_elf) 2300Sstevel@tonic-gate (void) elf_end(fptr->file_elf); 2310Sstevel@tonic-gate if (fptr->file_elfmem != NULL) 2320Sstevel@tonic-gate free(fptr->file_elfmem); 2330Sstevel@tonic-gate if (fptr->file_fd >= 0) 2340Sstevel@tonic-gate (void) close(fptr->file_fd); 2350Sstevel@tonic-gate if (fptr->file_ctfp) { 2360Sstevel@tonic-gate ctf_close(fptr->file_ctfp); 2370Sstevel@tonic-gate free(fptr->file_ctf_buf); 2380Sstevel@tonic-gate } 2390Sstevel@tonic-gate free(fptr); 2400Sstevel@tonic-gate P->num_files--; 2410Sstevel@tonic-gate } 2420Sstevel@tonic-gate } 2430Sstevel@tonic-gate 2440Sstevel@tonic-gate /* 2450Sstevel@tonic-gate * Deallocation function for a map_info_t 2460Sstevel@tonic-gate */ 2470Sstevel@tonic-gate static void 2480Sstevel@tonic-gate map_info_free(struct ps_prochandle *P, map_info_t *mptr) 2490Sstevel@tonic-gate { 2500Sstevel@tonic-gate file_info_t *fptr; 2510Sstevel@tonic-gate 2520Sstevel@tonic-gate if ((fptr = mptr->map_file) != NULL) { 2530Sstevel@tonic-gate if (fptr->file_map == mptr) 2540Sstevel@tonic-gate fptr->file_map = NULL; 2550Sstevel@tonic-gate file_info_free(P, fptr); 2560Sstevel@tonic-gate } 2570Sstevel@tonic-gate if (P->execname && mptr == P->map_exec) { 2580Sstevel@tonic-gate free(P->execname); 2590Sstevel@tonic-gate P->execname = NULL; 2600Sstevel@tonic-gate } 2610Sstevel@tonic-gate if (P->auxv && (mptr == P->map_exec || mptr == P->map_ldso)) { 2620Sstevel@tonic-gate free(P->auxv); 2630Sstevel@tonic-gate P->auxv = NULL; 2640Sstevel@tonic-gate P->nauxv = 0; 2650Sstevel@tonic-gate } 2660Sstevel@tonic-gate if (mptr == P->map_exec) 2670Sstevel@tonic-gate P->map_exec = NULL; 2680Sstevel@tonic-gate if (mptr == P->map_ldso) 2690Sstevel@tonic-gate P->map_ldso = NULL; 2700Sstevel@tonic-gate } 2710Sstevel@tonic-gate 2720Sstevel@tonic-gate /* 2730Sstevel@tonic-gate * Call-back function for librtld_db to iterate through all of its shared 2740Sstevel@tonic-gate * libraries. We use this to get the load object names for the mappings. 2750Sstevel@tonic-gate */ 2760Sstevel@tonic-gate static int 2770Sstevel@tonic-gate map_iter(const rd_loadobj_t *lop, void *cd) 2780Sstevel@tonic-gate { 2790Sstevel@tonic-gate char buf[PATH_MAX]; 2800Sstevel@tonic-gate struct ps_prochandle *P = cd; 2810Sstevel@tonic-gate map_info_t *mptr; 2820Sstevel@tonic-gate file_info_t *fptr; 2830Sstevel@tonic-gate 2840Sstevel@tonic-gate dprintf("encountered rd object at %p\n", (void *)lop->rl_base); 2850Sstevel@tonic-gate 2860Sstevel@tonic-gate if ((mptr = Paddr2mptr(P, lop->rl_base)) == NULL) 2870Sstevel@tonic-gate return (1); /* Base address does not match any mapping */ 2880Sstevel@tonic-gate 2890Sstevel@tonic-gate if ((fptr = mptr->map_file) == NULL && 2900Sstevel@tonic-gate (fptr = file_info_new(P, mptr)) == NULL) 2910Sstevel@tonic-gate return (1); /* Failed to allocate a new file_info_t */ 2920Sstevel@tonic-gate 2930Sstevel@tonic-gate if ((fptr->file_lo == NULL) && 2940Sstevel@tonic-gate (fptr->file_lo = malloc(sizeof (rd_loadobj_t))) == NULL) { 2950Sstevel@tonic-gate file_info_free(P, fptr); 2960Sstevel@tonic-gate return (1); /* Failed to allocate rd_loadobj_t */ 2970Sstevel@tonic-gate } 2980Sstevel@tonic-gate 2990Sstevel@tonic-gate fptr->file_map = mptr; 3000Sstevel@tonic-gate *fptr->file_lo = *lop; 3010Sstevel@tonic-gate 3020Sstevel@tonic-gate fptr->file_lo->rl_plt_base = fptr->file_plt_base; 3030Sstevel@tonic-gate fptr->file_lo->rl_plt_size = fptr->file_plt_size; 3040Sstevel@tonic-gate 3050Sstevel@tonic-gate if (fptr->file_lname) { 3060Sstevel@tonic-gate free(fptr->file_lname); 3070Sstevel@tonic-gate fptr->file_lname = NULL; 3080Sstevel@tonic-gate } 3090Sstevel@tonic-gate 3100Sstevel@tonic-gate if (Pread_string(P, buf, sizeof (buf), lop->rl_nameaddr) > 0) { 3110Sstevel@tonic-gate if ((fptr->file_lname = strdup(buf)) != NULL) 3120Sstevel@tonic-gate fptr->file_lbase = basename(fptr->file_lname); 3130Sstevel@tonic-gate } 3140Sstevel@tonic-gate 3150Sstevel@tonic-gate dprintf("loaded rd object %s lmid %lx\n", 3160Sstevel@tonic-gate fptr->file_lname ? fptr->file_lname : "<NULL>", lop->rl_lmident); 3170Sstevel@tonic-gate return (1); 3180Sstevel@tonic-gate } 3190Sstevel@tonic-gate 3200Sstevel@tonic-gate static void 3210Sstevel@tonic-gate map_set(struct ps_prochandle *P, map_info_t *mptr, const char *lname) 3220Sstevel@tonic-gate { 3230Sstevel@tonic-gate file_info_t *fptr; 3240Sstevel@tonic-gate 3250Sstevel@tonic-gate if ((fptr = mptr->map_file) == NULL && 3260Sstevel@tonic-gate (fptr = file_info_new(P, mptr)) == NULL) 3270Sstevel@tonic-gate return; /* Failed to allocate a new file_info_t */ 3280Sstevel@tonic-gate 3290Sstevel@tonic-gate fptr->file_map = mptr; 3300Sstevel@tonic-gate 3310Sstevel@tonic-gate if ((fptr->file_lo == NULL) && 3320Sstevel@tonic-gate (fptr->file_lo = malloc(sizeof (rd_loadobj_t))) == NULL) { 3330Sstevel@tonic-gate file_info_free(P, fptr); 3340Sstevel@tonic-gate return; /* Failed to allocate rd_loadobj_t */ 3350Sstevel@tonic-gate } 3360Sstevel@tonic-gate 3370Sstevel@tonic-gate (void) memset(fptr->file_lo, 0, sizeof (rd_loadobj_t)); 3380Sstevel@tonic-gate fptr->file_lo->rl_base = mptr->map_pmap.pr_vaddr; 3390Sstevel@tonic-gate fptr->file_lo->rl_bend = 3400Sstevel@tonic-gate mptr->map_pmap.pr_vaddr + mptr->map_pmap.pr_size; 3410Sstevel@tonic-gate 3420Sstevel@tonic-gate fptr->file_lo->rl_plt_base = fptr->file_plt_base; 3430Sstevel@tonic-gate fptr->file_lo->rl_plt_size = fptr->file_plt_size; 3440Sstevel@tonic-gate 3450Sstevel@tonic-gate if (fptr->file_lname) { 3460Sstevel@tonic-gate free(fptr->file_lname); 3470Sstevel@tonic-gate fptr->file_lname = NULL; 3480Sstevel@tonic-gate } 3490Sstevel@tonic-gate 3500Sstevel@tonic-gate if ((fptr->file_lname = strdup(lname)) != NULL) 3510Sstevel@tonic-gate fptr->file_lbase = basename(fptr->file_lname); 3520Sstevel@tonic-gate } 3530Sstevel@tonic-gate 3540Sstevel@tonic-gate static void 3550Sstevel@tonic-gate load_static_maps(struct ps_prochandle *P) 3560Sstevel@tonic-gate { 3570Sstevel@tonic-gate map_info_t *mptr; 3580Sstevel@tonic-gate 3590Sstevel@tonic-gate /* 3600Sstevel@tonic-gate * Construct the map for the a.out. 3610Sstevel@tonic-gate */ 3620Sstevel@tonic-gate if ((mptr = object_name_to_map(P, PR_LMID_EVERY, PR_OBJ_EXEC)) != NULL) 3630Sstevel@tonic-gate map_set(P, mptr, "a.out"); 3640Sstevel@tonic-gate 3650Sstevel@tonic-gate /* 3660Sstevel@tonic-gate * If the dynamic linker exists for this process, 3670Sstevel@tonic-gate * construct the map for it. 3680Sstevel@tonic-gate */ 3690Sstevel@tonic-gate if (Pgetauxval(P, AT_BASE) != -1L && 3700Sstevel@tonic-gate (mptr = object_name_to_map(P, PR_LMID_EVERY, PR_OBJ_LDSO)) != NULL) 3710Sstevel@tonic-gate map_set(P, mptr, "ld.so.1"); 3720Sstevel@tonic-gate } 3730Sstevel@tonic-gate 3740Sstevel@tonic-gate /* 3750Sstevel@tonic-gate * Go through all the address space mappings, validating or updating 3760Sstevel@tonic-gate * the information already gathered, or gathering new information. 3770Sstevel@tonic-gate * 3780Sstevel@tonic-gate * This function is only called when we suspect that the mappings have changed 3790Sstevel@tonic-gate * because this is the first time we're calling it or because of rtld activity. 3800Sstevel@tonic-gate */ 3810Sstevel@tonic-gate void 3820Sstevel@tonic-gate Pupdate_maps(struct ps_prochandle *P) 3830Sstevel@tonic-gate { 3840Sstevel@tonic-gate char mapfile[64]; 3850Sstevel@tonic-gate int mapfd; 3860Sstevel@tonic-gate struct stat statb; 3870Sstevel@tonic-gate prmap_t *Pmap = NULL; 3880Sstevel@tonic-gate prmap_t *pmap; 3890Sstevel@tonic-gate ssize_t nmap; 3900Sstevel@tonic-gate int i; 3910Sstevel@tonic-gate uint_t oldmapcount; 3920Sstevel@tonic-gate map_info_t *newmap, *newp; 3930Sstevel@tonic-gate map_info_t *mptr; 3940Sstevel@tonic-gate 395*457Sbmc if (P->info_valid || P->state == PS_UNDEAD) 3960Sstevel@tonic-gate return; 3970Sstevel@tonic-gate 3980Sstevel@tonic-gate Preadauxvec(P); 3990Sstevel@tonic-gate 4000Sstevel@tonic-gate (void) sprintf(mapfile, "/proc/%d/map", (int)P->pid); 4010Sstevel@tonic-gate if ((mapfd = open(mapfile, O_RDONLY)) < 0 || 4020Sstevel@tonic-gate fstat(mapfd, &statb) != 0 || 4030Sstevel@tonic-gate statb.st_size < sizeof (prmap_t) || 4040Sstevel@tonic-gate (Pmap = malloc(statb.st_size)) == NULL || 4050Sstevel@tonic-gate (nmap = pread(mapfd, Pmap, statb.st_size, 0L)) <= 0 || 4060Sstevel@tonic-gate (nmap /= sizeof (prmap_t)) == 0) { 4070Sstevel@tonic-gate if (Pmap != NULL) 4080Sstevel@tonic-gate free(Pmap); 4090Sstevel@tonic-gate if (mapfd >= 0) 4100Sstevel@tonic-gate (void) close(mapfd); 4110Sstevel@tonic-gate Preset_maps(P); /* utter failure; destroy tables */ 4120Sstevel@tonic-gate return; 4130Sstevel@tonic-gate } 4140Sstevel@tonic-gate (void) close(mapfd); 4150Sstevel@tonic-gate 4160Sstevel@tonic-gate if ((newmap = calloc(1, nmap * sizeof (map_info_t))) == NULL) 4170Sstevel@tonic-gate return; 4180Sstevel@tonic-gate 4190Sstevel@tonic-gate /* 4200Sstevel@tonic-gate * We try to merge any file information we may have for existing 4210Sstevel@tonic-gate * mappings, to avoid having to rebuild the file info. 4220Sstevel@tonic-gate */ 4230Sstevel@tonic-gate mptr = P->mappings; 4240Sstevel@tonic-gate pmap = Pmap; 4250Sstevel@tonic-gate newp = newmap; 4260Sstevel@tonic-gate oldmapcount = P->map_count; 4270Sstevel@tonic-gate for (i = 0; i < nmap; i++, pmap++, newp++) { 4280Sstevel@tonic-gate 4290Sstevel@tonic-gate if (oldmapcount == 0) { 4300Sstevel@tonic-gate /* 4310Sstevel@tonic-gate * We've exhausted all the old mappings. Every new 4320Sstevel@tonic-gate * mapping should be added. 4330Sstevel@tonic-gate */ 4340Sstevel@tonic-gate newp->map_pmap = *pmap; 4350Sstevel@tonic-gate 4360Sstevel@tonic-gate } else if (pmap->pr_vaddr == mptr->map_pmap.pr_vaddr && 4370Sstevel@tonic-gate pmap->pr_size == mptr->map_pmap.pr_size && 4380Sstevel@tonic-gate pmap->pr_offset == mptr->map_pmap.pr_offset && 4390Sstevel@tonic-gate (pmap->pr_mflags & ~(MA_BREAK | MA_STACK)) == 4400Sstevel@tonic-gate (mptr->map_pmap.pr_mflags & ~(MA_BREAK | MA_STACK)) && 4410Sstevel@tonic-gate pmap->pr_pagesize == mptr->map_pmap.pr_pagesize && 4420Sstevel@tonic-gate pmap->pr_shmid == mptr->map_pmap.pr_shmid && 4430Sstevel@tonic-gate strcmp(pmap->pr_mapname, mptr->map_pmap.pr_mapname) == 0) { 4440Sstevel@tonic-gate 4450Sstevel@tonic-gate /* 4460Sstevel@tonic-gate * This mapping matches exactly. Copy over the old 4470Sstevel@tonic-gate * mapping, taking care to get the latest flags. 4480Sstevel@tonic-gate * Make sure the associated file_info_t is updated 4490Sstevel@tonic-gate * appropriately. 4500Sstevel@tonic-gate */ 4510Sstevel@tonic-gate *newp = *mptr; 4520Sstevel@tonic-gate if (P->map_exec == mptr) 4530Sstevel@tonic-gate P->map_exec = newp; 4540Sstevel@tonic-gate if (P->map_ldso == mptr) 4550Sstevel@tonic-gate P->map_ldso = newp; 4560Sstevel@tonic-gate newp->map_pmap.pr_mflags = pmap->pr_mflags; 4570Sstevel@tonic-gate if (mptr->map_file != NULL && 4580Sstevel@tonic-gate mptr->map_file->file_map == mptr) 4590Sstevel@tonic-gate mptr->map_file->file_map = newp; 4600Sstevel@tonic-gate oldmapcount--; 4610Sstevel@tonic-gate mptr++; 4620Sstevel@tonic-gate 4630Sstevel@tonic-gate } else if (pmap->pr_vaddr + pmap->pr_size > 4640Sstevel@tonic-gate mptr->map_pmap.pr_vaddr) { 4650Sstevel@tonic-gate 4660Sstevel@tonic-gate /* 4670Sstevel@tonic-gate * The old mapping doesn't exist any more, remove it 4680Sstevel@tonic-gate * from the list. 4690Sstevel@tonic-gate */ 4700Sstevel@tonic-gate map_info_free(P, mptr); 4710Sstevel@tonic-gate oldmapcount--; 4720Sstevel@tonic-gate i--; 4730Sstevel@tonic-gate newp--; 4740Sstevel@tonic-gate pmap--; 4750Sstevel@tonic-gate mptr++; 4760Sstevel@tonic-gate 4770Sstevel@tonic-gate } else { 4780Sstevel@tonic-gate 4790Sstevel@tonic-gate /* 4800Sstevel@tonic-gate * This is a new mapping, add it directly. 4810Sstevel@tonic-gate */ 4820Sstevel@tonic-gate newp->map_pmap = *pmap; 4830Sstevel@tonic-gate } 4840Sstevel@tonic-gate } 4850Sstevel@tonic-gate 4860Sstevel@tonic-gate /* 4870Sstevel@tonic-gate * Free any old maps 4880Sstevel@tonic-gate */ 4890Sstevel@tonic-gate while (oldmapcount) { 4900Sstevel@tonic-gate map_info_free(P, mptr); 4910Sstevel@tonic-gate oldmapcount--; 4920Sstevel@tonic-gate mptr++; 4930Sstevel@tonic-gate } 4940Sstevel@tonic-gate 4950Sstevel@tonic-gate free(Pmap); 4960Sstevel@tonic-gate if (P->mappings != NULL) 4970Sstevel@tonic-gate free(P->mappings); 4980Sstevel@tonic-gate P->mappings = newmap; 4990Sstevel@tonic-gate P->map_count = P->map_alloc = nmap; 5000Sstevel@tonic-gate P->info_valid = 1; 5010Sstevel@tonic-gate 5020Sstevel@tonic-gate /* 5030Sstevel@tonic-gate * Consult librtld_db to get the load object 5040Sstevel@tonic-gate * names for all of the shared libraries. 5050Sstevel@tonic-gate */ 5060Sstevel@tonic-gate if (P->rap != NULL) 5070Sstevel@tonic-gate (void) rd_loadobj_iter(P->rap, map_iter, P); 5080Sstevel@tonic-gate } 5090Sstevel@tonic-gate 5100Sstevel@tonic-gate /* 5110Sstevel@tonic-gate * Update all of the mappings and rtld_db as if by Pupdate_maps(), and then 5120Sstevel@tonic-gate * forcibly cache all of the symbol tables associated with all object files. 5130Sstevel@tonic-gate */ 5140Sstevel@tonic-gate void 5150Sstevel@tonic-gate Pupdate_syms(struct ps_prochandle *P) 5160Sstevel@tonic-gate { 5170Sstevel@tonic-gate file_info_t *fptr = list_next(&P->file_head); 5180Sstevel@tonic-gate int i; 5190Sstevel@tonic-gate 5200Sstevel@tonic-gate Pupdate_maps(P); 5210Sstevel@tonic-gate 5220Sstevel@tonic-gate for (i = 0; i < P->num_files; i++, fptr = list_next(fptr)) { 5230Sstevel@tonic-gate Pbuild_file_symtab(P, fptr); 5240Sstevel@tonic-gate (void) Pbuild_file_ctf(P, fptr); 5250Sstevel@tonic-gate } 5260Sstevel@tonic-gate } 5270Sstevel@tonic-gate 5280Sstevel@tonic-gate /* 5290Sstevel@tonic-gate * Return the librtld_db agent handle for the victim process. 5300Sstevel@tonic-gate * The handle will become invalid at the next successful exec() and the 5310Sstevel@tonic-gate * client (caller of proc_rd_agent()) must not use it beyond that point. 5320Sstevel@tonic-gate * If the process is already dead, we've already tried our best to 5330Sstevel@tonic-gate * create the agent during core file initialization. 5340Sstevel@tonic-gate */ 5350Sstevel@tonic-gate rd_agent_t * 5360Sstevel@tonic-gate Prd_agent(struct ps_prochandle *P) 5370Sstevel@tonic-gate { 5380Sstevel@tonic-gate if (P->rap == NULL && P->state != PS_DEAD && P->state != PS_IDLE) { 5390Sstevel@tonic-gate Pupdate_maps(P); 5400Sstevel@tonic-gate if (P->num_files == 0) 5410Sstevel@tonic-gate load_static_maps(P); 5420Sstevel@tonic-gate rd_log(_libproc_debug); 5430Sstevel@tonic-gate if ((P->rap = rd_new(P)) != NULL) 5440Sstevel@tonic-gate (void) rd_loadobj_iter(P->rap, map_iter, P); 5450Sstevel@tonic-gate } 5460Sstevel@tonic-gate return (P->rap); 5470Sstevel@tonic-gate } 5480Sstevel@tonic-gate 5490Sstevel@tonic-gate /* 5500Sstevel@tonic-gate * Return the prmap_t structure containing 'addr', but only if it 5510Sstevel@tonic-gate * is in the dynamic linker's link map and is the text section. 5520Sstevel@tonic-gate */ 5530Sstevel@tonic-gate const prmap_t * 5540Sstevel@tonic-gate Paddr_to_text_map(struct ps_prochandle *P, uintptr_t addr) 5550Sstevel@tonic-gate { 5560Sstevel@tonic-gate map_info_t *mptr; 5570Sstevel@tonic-gate 5580Sstevel@tonic-gate if (!P->info_valid) 5590Sstevel@tonic-gate Pupdate_maps(P); 5600Sstevel@tonic-gate 5610Sstevel@tonic-gate if ((mptr = Paddr2mptr(P, addr)) != NULL) { 5620Sstevel@tonic-gate file_info_t *fptr = build_map_symtab(P, mptr); 5630Sstevel@tonic-gate const prmap_t *pmp = &mptr->map_pmap; 5640Sstevel@tonic-gate 5650Sstevel@tonic-gate if (fptr != NULL && fptr->file_lo != NULL && 5660Sstevel@tonic-gate fptr->file_lo->rl_base >= pmp->pr_vaddr && 5670Sstevel@tonic-gate fptr->file_lo->rl_base < pmp->pr_vaddr + pmp->pr_size) 5680Sstevel@tonic-gate return (pmp); 5690Sstevel@tonic-gate } 5700Sstevel@tonic-gate 5710Sstevel@tonic-gate return (NULL); 5720Sstevel@tonic-gate } 5730Sstevel@tonic-gate 5740Sstevel@tonic-gate /* 5750Sstevel@tonic-gate * Return the prmap_t structure containing 'addr' (no restrictions on 5760Sstevel@tonic-gate * the type of mapping). 5770Sstevel@tonic-gate */ 5780Sstevel@tonic-gate const prmap_t * 5790Sstevel@tonic-gate Paddr_to_map(struct ps_prochandle *P, uintptr_t addr) 5800Sstevel@tonic-gate { 5810Sstevel@tonic-gate map_info_t *mptr; 5820Sstevel@tonic-gate 5830Sstevel@tonic-gate if (!P->info_valid) 5840Sstevel@tonic-gate Pupdate_maps(P); 5850Sstevel@tonic-gate 5860Sstevel@tonic-gate if ((mptr = Paddr2mptr(P, addr)) != NULL) 5870Sstevel@tonic-gate return (&mptr->map_pmap); 5880Sstevel@tonic-gate 5890Sstevel@tonic-gate return (NULL); 5900Sstevel@tonic-gate } 5910Sstevel@tonic-gate 5920Sstevel@tonic-gate /* 5930Sstevel@tonic-gate * Convert a full or partial load object name to the prmap_t for its 5940Sstevel@tonic-gate * corresponding primary text mapping. 5950Sstevel@tonic-gate */ 5960Sstevel@tonic-gate const prmap_t * 5970Sstevel@tonic-gate Plmid_to_map(struct ps_prochandle *P, Lmid_t lmid, const char *name) 5980Sstevel@tonic-gate { 5990Sstevel@tonic-gate map_info_t *mptr; 6000Sstevel@tonic-gate 6010Sstevel@tonic-gate if (name == PR_OBJ_EVERY) 6020Sstevel@tonic-gate return (NULL); /* A reasonable mistake */ 6030Sstevel@tonic-gate 6040Sstevel@tonic-gate if ((mptr = object_name_to_map(P, lmid, name)) != NULL) 6050Sstevel@tonic-gate return (&mptr->map_pmap); 6060Sstevel@tonic-gate 6070Sstevel@tonic-gate return (NULL); 6080Sstevel@tonic-gate } 6090Sstevel@tonic-gate 6100Sstevel@tonic-gate const prmap_t * 6110Sstevel@tonic-gate Pname_to_map(struct ps_prochandle *P, const char *name) 6120Sstevel@tonic-gate { 6130Sstevel@tonic-gate return (Plmid_to_map(P, PR_LMID_EVERY, name)); 6140Sstevel@tonic-gate } 6150Sstevel@tonic-gate 6160Sstevel@tonic-gate const rd_loadobj_t * 6170Sstevel@tonic-gate Paddr_to_loadobj(struct ps_prochandle *P, uintptr_t addr) 6180Sstevel@tonic-gate { 6190Sstevel@tonic-gate map_info_t *mptr; 6200Sstevel@tonic-gate 6210Sstevel@tonic-gate if (!P->info_valid) 6220Sstevel@tonic-gate Pupdate_maps(P); 6230Sstevel@tonic-gate 6240Sstevel@tonic-gate if ((mptr = Paddr2mptr(P, addr)) == NULL) 6250Sstevel@tonic-gate return (NULL); 6260Sstevel@tonic-gate 6270Sstevel@tonic-gate /* 6280Sstevel@tonic-gate * By building the symbol table, we implicitly bring the PLT 6290Sstevel@tonic-gate * information up to date in the load object. 6300Sstevel@tonic-gate */ 6310Sstevel@tonic-gate (void) build_map_symtab(P, mptr); 6320Sstevel@tonic-gate 6330Sstevel@tonic-gate return (mptr->map_file->file_lo); 6340Sstevel@tonic-gate } 6350Sstevel@tonic-gate 6360Sstevel@tonic-gate const rd_loadobj_t * 6370Sstevel@tonic-gate Plmid_to_loadobj(struct ps_prochandle *P, Lmid_t lmid, const char *name) 6380Sstevel@tonic-gate { 6390Sstevel@tonic-gate map_info_t *mptr; 6400Sstevel@tonic-gate 6410Sstevel@tonic-gate if (name == PR_OBJ_EVERY) 6420Sstevel@tonic-gate return (NULL); 6430Sstevel@tonic-gate 6440Sstevel@tonic-gate if ((mptr = object_name_to_map(P, lmid, name)) == NULL) 6450Sstevel@tonic-gate return (NULL); 6460Sstevel@tonic-gate 6470Sstevel@tonic-gate /* 6480Sstevel@tonic-gate * By building the symbol table, we implicitly bring the PLT 6490Sstevel@tonic-gate * information up to date in the load object. 6500Sstevel@tonic-gate */ 6510Sstevel@tonic-gate (void) build_map_symtab(P, mptr); 6520Sstevel@tonic-gate 6530Sstevel@tonic-gate return (mptr->map_file->file_lo); 6540Sstevel@tonic-gate } 6550Sstevel@tonic-gate 6560Sstevel@tonic-gate const rd_loadobj_t * 6570Sstevel@tonic-gate Pname_to_loadobj(struct ps_prochandle *P, const char *name) 6580Sstevel@tonic-gate { 6590Sstevel@tonic-gate return (Plmid_to_loadobj(P, PR_LMID_EVERY, name)); 6600Sstevel@tonic-gate } 6610Sstevel@tonic-gate 6620Sstevel@tonic-gate ctf_file_t * 6630Sstevel@tonic-gate Pbuild_file_ctf(struct ps_prochandle *P, file_info_t *fptr) 6640Sstevel@tonic-gate { 6650Sstevel@tonic-gate ctf_sect_t ctdata, symtab, strtab; 6660Sstevel@tonic-gate sym_tbl_t *symp; 6670Sstevel@tonic-gate int err; 6680Sstevel@tonic-gate 6690Sstevel@tonic-gate if (fptr->file_ctfp != NULL) 6700Sstevel@tonic-gate return (fptr->file_ctfp); 6710Sstevel@tonic-gate 6720Sstevel@tonic-gate Pbuild_file_symtab(P, fptr); 6730Sstevel@tonic-gate 6740Sstevel@tonic-gate if (fptr->file_ctf_size == 0) 6750Sstevel@tonic-gate return (NULL); 6760Sstevel@tonic-gate 6770Sstevel@tonic-gate symp = fptr->file_ctf_dyn ? &fptr->file_dynsym : &fptr->file_symtab; 6780Sstevel@tonic-gate if (symp->sym_data == NULL) 6790Sstevel@tonic-gate return (NULL); 6800Sstevel@tonic-gate 6810Sstevel@tonic-gate /* 6820Sstevel@tonic-gate * The buffer may alread be allocated if this is a core file that 6830Sstevel@tonic-gate * contained CTF data for this file. 6840Sstevel@tonic-gate */ 6850Sstevel@tonic-gate if (fptr->file_ctf_buf == NULL) { 6860Sstevel@tonic-gate fptr->file_ctf_buf = malloc(fptr->file_ctf_size); 6870Sstevel@tonic-gate if (fptr->file_ctf_buf == NULL) { 6880Sstevel@tonic-gate dprintf("failed to allocate ctf buffer\n"); 6890Sstevel@tonic-gate return (NULL); 6900Sstevel@tonic-gate } 6910Sstevel@tonic-gate 6920Sstevel@tonic-gate if (pread(fptr->file_fd, fptr->file_ctf_buf, 6930Sstevel@tonic-gate fptr->file_ctf_size, fptr->file_ctf_off) != 6940Sstevel@tonic-gate fptr->file_ctf_size) { 6950Sstevel@tonic-gate free(fptr->file_ctf_buf); 6960Sstevel@tonic-gate fptr->file_ctf_buf = NULL; 6970Sstevel@tonic-gate dprintf("failed to read ctf data\n"); 6980Sstevel@tonic-gate return (NULL); 6990Sstevel@tonic-gate } 7000Sstevel@tonic-gate } 7010Sstevel@tonic-gate 7020Sstevel@tonic-gate ctdata.cts_name = ".SUNW_ctf"; 7030Sstevel@tonic-gate ctdata.cts_type = SHT_PROGBITS; 7040Sstevel@tonic-gate ctdata.cts_flags = 0; 7050Sstevel@tonic-gate ctdata.cts_data = fptr->file_ctf_buf; 7060Sstevel@tonic-gate ctdata.cts_size = fptr->file_ctf_size; 7070Sstevel@tonic-gate ctdata.cts_entsize = 1; 7080Sstevel@tonic-gate ctdata.cts_offset = 0; 7090Sstevel@tonic-gate 7100Sstevel@tonic-gate symtab.cts_name = fptr->file_ctf_dyn ? ".dynsym" : ".symtab"; 7110Sstevel@tonic-gate symtab.cts_type = symp->sym_hdr.sh_type; 7120Sstevel@tonic-gate symtab.cts_flags = symp->sym_hdr.sh_flags; 7130Sstevel@tonic-gate symtab.cts_data = symp->sym_data->d_buf; 7140Sstevel@tonic-gate symtab.cts_size = symp->sym_hdr.sh_size; 7150Sstevel@tonic-gate symtab.cts_entsize = symp->sym_hdr.sh_entsize; 7160Sstevel@tonic-gate symtab.cts_offset = symp->sym_hdr.sh_offset; 7170Sstevel@tonic-gate 7180Sstevel@tonic-gate strtab.cts_name = fptr->file_ctf_dyn ? ".dynstr" : ".strtab"; 7190Sstevel@tonic-gate strtab.cts_type = symp->sym_strhdr.sh_type; 7200Sstevel@tonic-gate strtab.cts_flags = symp->sym_strhdr.sh_flags; 7210Sstevel@tonic-gate strtab.cts_data = symp->sym_strs; 7220Sstevel@tonic-gate strtab.cts_size = symp->sym_strhdr.sh_size; 7230Sstevel@tonic-gate strtab.cts_entsize = symp->sym_strhdr.sh_entsize; 7240Sstevel@tonic-gate strtab.cts_offset = symp->sym_strhdr.sh_offset; 7250Sstevel@tonic-gate 7260Sstevel@tonic-gate fptr->file_ctfp = ctf_bufopen(&ctdata, &symtab, &strtab, &err); 7270Sstevel@tonic-gate if (fptr->file_ctfp == NULL) { 7280Sstevel@tonic-gate free(fptr->file_ctf_buf); 7290Sstevel@tonic-gate fptr->file_ctf_buf = NULL; 7300Sstevel@tonic-gate return (NULL); 7310Sstevel@tonic-gate } 7320Sstevel@tonic-gate 7330Sstevel@tonic-gate dprintf("loaded %lu bytes of CTF data for %s\n", 7340Sstevel@tonic-gate (ulong_t)fptr->file_ctf_size, fptr->file_pname); 7350Sstevel@tonic-gate 7360Sstevel@tonic-gate return (fptr->file_ctfp); 7370Sstevel@tonic-gate } 7380Sstevel@tonic-gate 7390Sstevel@tonic-gate ctf_file_t * 7400Sstevel@tonic-gate Paddr_to_ctf(struct ps_prochandle *P, uintptr_t addr) 7410Sstevel@tonic-gate { 7420Sstevel@tonic-gate map_info_t *mptr; 7430Sstevel@tonic-gate file_info_t *fptr; 7440Sstevel@tonic-gate 7450Sstevel@tonic-gate if (!P->info_valid) 7460Sstevel@tonic-gate Pupdate_maps(P); 7470Sstevel@tonic-gate 7480Sstevel@tonic-gate if ((mptr = Paddr2mptr(P, addr)) == NULL || 7490Sstevel@tonic-gate (fptr = mptr->map_file) == NULL) 7500Sstevel@tonic-gate return (NULL); 7510Sstevel@tonic-gate 7520Sstevel@tonic-gate return (Pbuild_file_ctf(P, fptr)); 7530Sstevel@tonic-gate } 7540Sstevel@tonic-gate 7550Sstevel@tonic-gate ctf_file_t * 7560Sstevel@tonic-gate Plmid_to_ctf(struct ps_prochandle *P, Lmid_t lmid, const char *name) 7570Sstevel@tonic-gate { 7580Sstevel@tonic-gate map_info_t *mptr; 7590Sstevel@tonic-gate file_info_t *fptr; 7600Sstevel@tonic-gate 7610Sstevel@tonic-gate if (name == PR_OBJ_EVERY) 7620Sstevel@tonic-gate return (NULL); 7630Sstevel@tonic-gate 7640Sstevel@tonic-gate if ((mptr = object_name_to_map(P, lmid, name)) == NULL || 7650Sstevel@tonic-gate (fptr = mptr->map_file) == NULL) 7660Sstevel@tonic-gate return (NULL); 7670Sstevel@tonic-gate 7680Sstevel@tonic-gate return (Pbuild_file_ctf(P, fptr)); 7690Sstevel@tonic-gate } 7700Sstevel@tonic-gate 7710Sstevel@tonic-gate ctf_file_t * 7720Sstevel@tonic-gate Pname_to_ctf(struct ps_prochandle *P, const char *name) 7730Sstevel@tonic-gate { 7740Sstevel@tonic-gate return (Plmid_to_ctf(P, PR_LMID_EVERY, name)); 7750Sstevel@tonic-gate } 7760Sstevel@tonic-gate 7770Sstevel@tonic-gate /* 7780Sstevel@tonic-gate * If we're not a core file, re-read the /proc/<pid>/auxv file and store 7790Sstevel@tonic-gate * its contents in P->auxv. In the case of a core file, we either 7800Sstevel@tonic-gate * initialized P->auxv in Pcore() from the NT_AUXV, or we don't have an 7810Sstevel@tonic-gate * auxv because the note was missing. 7820Sstevel@tonic-gate */ 7830Sstevel@tonic-gate void 7840Sstevel@tonic-gate Preadauxvec(struct ps_prochandle *P) 7850Sstevel@tonic-gate { 7860Sstevel@tonic-gate char auxfile[64]; 7870Sstevel@tonic-gate struct stat statb; 7880Sstevel@tonic-gate ssize_t naux; 7890Sstevel@tonic-gate int fd; 7900Sstevel@tonic-gate 7910Sstevel@tonic-gate if (P->state == PS_DEAD) 7920Sstevel@tonic-gate return; /* Already read during Pgrab_core() */ 7930Sstevel@tonic-gate if (P->state == PS_IDLE) 7940Sstevel@tonic-gate return; /* No aux vec for Pgrab_file() */ 7950Sstevel@tonic-gate 7960Sstevel@tonic-gate if (P->auxv != NULL) { 7970Sstevel@tonic-gate free(P->auxv); 7980Sstevel@tonic-gate P->auxv = NULL; 7990Sstevel@tonic-gate P->nauxv = 0; 8000Sstevel@tonic-gate } 8010Sstevel@tonic-gate 8020Sstevel@tonic-gate (void) sprintf(auxfile, "/proc/%d/auxv", (int)P->pid); 8030Sstevel@tonic-gate if ((fd = open(auxfile, O_RDONLY)) < 0) 8040Sstevel@tonic-gate return; 8050Sstevel@tonic-gate 8060Sstevel@tonic-gate if (fstat(fd, &statb) == 0 && 8070Sstevel@tonic-gate statb.st_size >= sizeof (auxv_t) && 8080Sstevel@tonic-gate (P->auxv = malloc(statb.st_size + sizeof (auxv_t))) != NULL) { 8090Sstevel@tonic-gate if ((naux = read(fd, P->auxv, statb.st_size)) < 0 || 8100Sstevel@tonic-gate (naux /= sizeof (auxv_t)) < 1) { 8110Sstevel@tonic-gate free(P->auxv); 8120Sstevel@tonic-gate P->auxv = NULL; 8130Sstevel@tonic-gate } else { 8140Sstevel@tonic-gate P->auxv[naux].a_type = AT_NULL; 8150Sstevel@tonic-gate P->auxv[naux].a_un.a_val = 0L; 8160Sstevel@tonic-gate P->nauxv = (int)naux; 8170Sstevel@tonic-gate } 8180Sstevel@tonic-gate } 8190Sstevel@tonic-gate 8200Sstevel@tonic-gate (void) close(fd); 8210Sstevel@tonic-gate } 8220Sstevel@tonic-gate 8230Sstevel@tonic-gate /* 8240Sstevel@tonic-gate * Return a requested element from the process's aux vector. 8250Sstevel@tonic-gate * Return -1 on failure (this is adequate for our purposes). 8260Sstevel@tonic-gate */ 8270Sstevel@tonic-gate long 8280Sstevel@tonic-gate Pgetauxval(struct ps_prochandle *P, int type) 8290Sstevel@tonic-gate { 8300Sstevel@tonic-gate auxv_t *auxv; 8310Sstevel@tonic-gate 8320Sstevel@tonic-gate if (P->auxv == NULL) 8330Sstevel@tonic-gate Preadauxvec(P); 8340Sstevel@tonic-gate 8350Sstevel@tonic-gate if (P->auxv == NULL) 8360Sstevel@tonic-gate return (-1); 8370Sstevel@tonic-gate 8380Sstevel@tonic-gate for (auxv = P->auxv; auxv->a_type != AT_NULL; auxv++) { 8390Sstevel@tonic-gate if (auxv->a_type == type) 8400Sstevel@tonic-gate return (auxv->a_un.a_val); 8410Sstevel@tonic-gate } 8420Sstevel@tonic-gate 8430Sstevel@tonic-gate return (-1); 8440Sstevel@tonic-gate } 8450Sstevel@tonic-gate 8460Sstevel@tonic-gate /* 8470Sstevel@tonic-gate * Return a pointer to our internal copy of the process's aux vector. 8480Sstevel@tonic-gate * The caller should not hold on to this pointer across any libproc calls. 8490Sstevel@tonic-gate */ 8500Sstevel@tonic-gate const auxv_t * 8510Sstevel@tonic-gate Pgetauxvec(struct ps_prochandle *P) 8520Sstevel@tonic-gate { 8530Sstevel@tonic-gate static const auxv_t empty = { AT_NULL, 0L }; 8540Sstevel@tonic-gate 8550Sstevel@tonic-gate if (P->auxv == NULL) 8560Sstevel@tonic-gate Preadauxvec(P); 8570Sstevel@tonic-gate 8580Sstevel@tonic-gate if (P->auxv == NULL) 8590Sstevel@tonic-gate return (&empty); 8600Sstevel@tonic-gate 8610Sstevel@tonic-gate return (P->auxv); 8620Sstevel@tonic-gate } 8630Sstevel@tonic-gate 8640Sstevel@tonic-gate /* 8650Sstevel@tonic-gate * Find or build the symbol table for the given mapping. 8660Sstevel@tonic-gate */ 8670Sstevel@tonic-gate static file_info_t * 8680Sstevel@tonic-gate build_map_symtab(struct ps_prochandle *P, map_info_t *mptr) 8690Sstevel@tonic-gate { 8700Sstevel@tonic-gate prmap_t *pmap = &mptr->map_pmap; 8710Sstevel@tonic-gate file_info_t *fptr; 8720Sstevel@tonic-gate rd_loadobj_t *lop; 8730Sstevel@tonic-gate uint_t i; 8740Sstevel@tonic-gate 8750Sstevel@tonic-gate if ((fptr = mptr->map_file) != NULL) { 8760Sstevel@tonic-gate Pbuild_file_symtab(P, fptr); 8770Sstevel@tonic-gate return (fptr); 8780Sstevel@tonic-gate } 8790Sstevel@tonic-gate 8800Sstevel@tonic-gate if (pmap->pr_mapname[0] == '\0') 8810Sstevel@tonic-gate return (NULL); 8820Sstevel@tonic-gate 8830Sstevel@tonic-gate /* 8840Sstevel@tonic-gate * Attempt to find a matching file. 8850Sstevel@tonic-gate * (A file can be mapped at several different addresses.) 8860Sstevel@tonic-gate */ 8870Sstevel@tonic-gate for (i = 0, fptr = list_next(&P->file_head); i < P->num_files; 8880Sstevel@tonic-gate i++, fptr = list_next(fptr)) { 8890Sstevel@tonic-gate if (strcmp(fptr->file_pname, pmap->pr_mapname) == 0 && 8900Sstevel@tonic-gate (lop = fptr->file_lo) != NULL && 8910Sstevel@tonic-gate ((pmap->pr_vaddr <= lop->rl_base && 8920Sstevel@tonic-gate lop->rl_base < pmap->pr_vaddr + pmap->pr_size) || 8930Sstevel@tonic-gate (pmap->pr_vaddr <= lop->rl_data_base && 8940Sstevel@tonic-gate lop->rl_data_base < pmap->pr_vaddr + pmap->pr_size))) { 8950Sstevel@tonic-gate mptr->map_file = fptr; 8960Sstevel@tonic-gate fptr->file_ref++; 8970Sstevel@tonic-gate Pbuild_file_symtab(P, fptr); 8980Sstevel@tonic-gate return (fptr); 8990Sstevel@tonic-gate } 9000Sstevel@tonic-gate } 9010Sstevel@tonic-gate 9020Sstevel@tonic-gate /* 9030Sstevel@tonic-gate * If we need to create a new file_info structure, iterate 9040Sstevel@tonic-gate * through the load objects in order to attempt to connect 9050Sstevel@tonic-gate * this new file with its primary text mapping. We again 9060Sstevel@tonic-gate * need to handle ld.so as a special case because we need 9070Sstevel@tonic-gate * to be able to bootstrap librtld_db. 9080Sstevel@tonic-gate */ 9090Sstevel@tonic-gate if ((fptr = file_info_new(P, mptr)) == NULL) 9100Sstevel@tonic-gate return (NULL); 9110Sstevel@tonic-gate 9120Sstevel@tonic-gate if (P->map_ldso != mptr) { 9130Sstevel@tonic-gate if (P->rap != NULL) 9140Sstevel@tonic-gate (void) rd_loadobj_iter(P->rap, map_iter, P); 9150Sstevel@tonic-gate else 9160Sstevel@tonic-gate (void) Prd_agent(P); 9170Sstevel@tonic-gate } else { 9180Sstevel@tonic-gate fptr->file_map = mptr; 9190Sstevel@tonic-gate } 9200Sstevel@tonic-gate 9210Sstevel@tonic-gate /* 9220Sstevel@tonic-gate * If librtld_db wasn't able to help us connect the file to a primary 9230Sstevel@tonic-gate * text mapping, set file_map to the current mapping because we require 9240Sstevel@tonic-gate * fptr->file_map to be set in Pbuild_file_symtab. librtld_db may be 9250Sstevel@tonic-gate * unaware of what's going on in the rare case that a legitimate ELF 9260Sstevel@tonic-gate * file has been mmap(2)ed into the process address space *without* 9270Sstevel@tonic-gate * the use of dlopen(3x). Why would this happen? See pwdx ... :) 9280Sstevel@tonic-gate */ 9290Sstevel@tonic-gate if (fptr->file_map == NULL) 9300Sstevel@tonic-gate fptr->file_map = mptr; 9310Sstevel@tonic-gate 9320Sstevel@tonic-gate Pbuild_file_symtab(P, fptr); 9330Sstevel@tonic-gate 9340Sstevel@tonic-gate return (fptr); 9350Sstevel@tonic-gate } 9360Sstevel@tonic-gate 9370Sstevel@tonic-gate static int 9380Sstevel@tonic-gate read_ehdr32(struct ps_prochandle *P, Elf32_Ehdr *ehdr, uintptr_t addr) 9390Sstevel@tonic-gate { 9400Sstevel@tonic-gate if (Pread(P, ehdr, sizeof (*ehdr), addr) != sizeof (*ehdr)) 9410Sstevel@tonic-gate return (-1); 9420Sstevel@tonic-gate 9430Sstevel@tonic-gate if (ehdr->e_ident[EI_MAG0] != ELFMAG0 || 9440Sstevel@tonic-gate ehdr->e_ident[EI_MAG1] != ELFMAG1 || 9450Sstevel@tonic-gate ehdr->e_ident[EI_MAG2] != ELFMAG2 || 9460Sstevel@tonic-gate ehdr->e_ident[EI_MAG3] != ELFMAG3 || 9470Sstevel@tonic-gate ehdr->e_ident[EI_CLASS] != ELFCLASS32 || 9480Sstevel@tonic-gate #ifdef _BIG_ENDIAN 9490Sstevel@tonic-gate ehdr->e_ident[EI_DATA] != ELFDATA2MSB || 9500Sstevel@tonic-gate #else 9510Sstevel@tonic-gate ehdr->e_ident[EI_DATA] != ELFDATA2LSB || 9520Sstevel@tonic-gate #endif 9530Sstevel@tonic-gate ehdr->e_ident[EI_VERSION] != EV_CURRENT) 9540Sstevel@tonic-gate return (-1); 9550Sstevel@tonic-gate 9560Sstevel@tonic-gate return (0); 9570Sstevel@tonic-gate } 9580Sstevel@tonic-gate 9590Sstevel@tonic-gate static int 9600Sstevel@tonic-gate read_dynamic_phdr32(struct ps_prochandle *P, const Elf32_Ehdr *ehdr, 9610Sstevel@tonic-gate Elf32_Phdr *phdr, uintptr_t addr) 9620Sstevel@tonic-gate { 9630Sstevel@tonic-gate uint_t i; 9640Sstevel@tonic-gate 9650Sstevel@tonic-gate for (i = 0; i < ehdr->e_phnum; i++) { 9660Sstevel@tonic-gate uintptr_t a = addr + ehdr->e_phoff + i * ehdr->e_phentsize; 9670Sstevel@tonic-gate if (Pread(P, phdr, sizeof (*phdr), a) != sizeof (*phdr)) 9680Sstevel@tonic-gate return (-1); 9690Sstevel@tonic-gate 9700Sstevel@tonic-gate if (phdr->p_type == PT_DYNAMIC) 9710Sstevel@tonic-gate return (0); 9720Sstevel@tonic-gate } 9730Sstevel@tonic-gate 9740Sstevel@tonic-gate return (-1); 9750Sstevel@tonic-gate } 9760Sstevel@tonic-gate 9770Sstevel@tonic-gate #ifdef _LP64 9780Sstevel@tonic-gate static int 9790Sstevel@tonic-gate read_ehdr64(struct ps_prochandle *P, Elf64_Ehdr *ehdr, uintptr_t addr) 9800Sstevel@tonic-gate { 9810Sstevel@tonic-gate if (Pread(P, ehdr, sizeof (Elf64_Ehdr), addr) != sizeof (Elf64_Ehdr)) 9820Sstevel@tonic-gate return (-1); 9830Sstevel@tonic-gate 9840Sstevel@tonic-gate if (ehdr->e_ident[EI_MAG0] != ELFMAG0 || 9850Sstevel@tonic-gate ehdr->e_ident[EI_MAG1] != ELFMAG1 || 9860Sstevel@tonic-gate ehdr->e_ident[EI_MAG2] != ELFMAG2 || 9870Sstevel@tonic-gate ehdr->e_ident[EI_MAG3] != ELFMAG3 || 9880Sstevel@tonic-gate ehdr->e_ident[EI_CLASS] != ELFCLASS64 || 9890Sstevel@tonic-gate #ifdef _BIG_ENDIAN 9900Sstevel@tonic-gate ehdr->e_ident[EI_DATA] != ELFDATA2MSB || 9910Sstevel@tonic-gate #else 9920Sstevel@tonic-gate ehdr->e_ident[EI_DATA] != ELFDATA2LSB || 9930Sstevel@tonic-gate #endif 9940Sstevel@tonic-gate ehdr->e_ident[EI_VERSION] != EV_CURRENT) 9950Sstevel@tonic-gate return (-1); 9960Sstevel@tonic-gate 9970Sstevel@tonic-gate return (0); 9980Sstevel@tonic-gate } 9990Sstevel@tonic-gate 10000Sstevel@tonic-gate static int 10010Sstevel@tonic-gate read_dynamic_phdr64(struct ps_prochandle *P, const Elf64_Ehdr *ehdr, 10020Sstevel@tonic-gate Elf64_Phdr *phdr, uintptr_t addr) 10030Sstevel@tonic-gate { 10040Sstevel@tonic-gate uint_t i; 10050Sstevel@tonic-gate 10060Sstevel@tonic-gate for (i = 0; i < ehdr->e_phnum; i++) { 10070Sstevel@tonic-gate uintptr_t a = addr + ehdr->e_phoff + i * ehdr->e_phentsize; 10080Sstevel@tonic-gate if (Pread(P, phdr, sizeof (*phdr), a) != sizeof (*phdr)) 10090Sstevel@tonic-gate return (-1); 10100Sstevel@tonic-gate 10110Sstevel@tonic-gate if (phdr->p_type == PT_DYNAMIC) 10120Sstevel@tonic-gate return (0); 10130Sstevel@tonic-gate } 10140Sstevel@tonic-gate 10150Sstevel@tonic-gate return (-1); 10160Sstevel@tonic-gate } 10170Sstevel@tonic-gate #endif /* _LP64 */ 10180Sstevel@tonic-gate 10190Sstevel@tonic-gate /* 10200Sstevel@tonic-gate * The text segment for each load object contains the elf header and 10210Sstevel@tonic-gate * program headers. We can use this information to determine if the 10220Sstevel@tonic-gate * file that corresponds to the load object is the same file that 10230Sstevel@tonic-gate * was loaded into the process's address space. There can be a discrepency 10240Sstevel@tonic-gate * if a file is recompiled after the process is started or if the target 10250Sstevel@tonic-gate * represents a core file from a differently configured system -- two 10260Sstevel@tonic-gate * common examples. The DT_CHECKSUM entry in the dynamic section 10270Sstevel@tonic-gate * provides an easy method of comparison. It is important to note that 10280Sstevel@tonic-gate * the dynamic section usually lives in the data segment, but the meta 10290Sstevel@tonic-gate * data we use to find the dynamic section lives in the text segment so 10300Sstevel@tonic-gate * if either of those segments is absent we can't proceed. 10310Sstevel@tonic-gate * 10320Sstevel@tonic-gate * We're looking through the elf file for several items: the symbol tables 10330Sstevel@tonic-gate * (both dynsym and symtab), the procedure linkage table (PLT) base, 10340Sstevel@tonic-gate * size, and relocation base, and the CTF information. Most of this can 10350Sstevel@tonic-gate * be recovered from the loaded image of the file itself, the exceptions 10360Sstevel@tonic-gate * being the symtab and CTF data. 10370Sstevel@tonic-gate * 10380Sstevel@tonic-gate * First we try to open the file that we think corresponds to the load 10390Sstevel@tonic-gate * object, if the DT_CHECKSUM values match, we're all set, and can simply 10400Sstevel@tonic-gate * recover all the information we need from the file. If the values of 10410Sstevel@tonic-gate * DT_CHECKSUM don't match, or if we can't access the file for whatever 10420Sstevel@tonic-gate * reasaon, we fake up a elf file to use in its stead. If we can't read 10430Sstevel@tonic-gate * the elf data in the process's address space, we fall back to using 10440Sstevel@tonic-gate * the file even though it may give inaccurate information. 10450Sstevel@tonic-gate * 10460Sstevel@tonic-gate * The elf file that we fake up has to consist of sections for the 10470Sstevel@tonic-gate * dynsym, the PLT and the dynamic section. Note that in the case of a 10480Sstevel@tonic-gate * core file, we'll get the CTF data in the file_info_t later on from 10490Sstevel@tonic-gate * a section embedded the core file (if it's present). 10500Sstevel@tonic-gate * 10510Sstevel@tonic-gate * file_differs() conservatively looks for mismatched files, identifying 10520Sstevel@tonic-gate * a match when there is any ambiguity (since that's the legacy behavior). 10530Sstevel@tonic-gate */ 10540Sstevel@tonic-gate static int 10550Sstevel@tonic-gate file_differs(struct ps_prochandle *P, Elf *elf, file_info_t *fptr) 10560Sstevel@tonic-gate { 10570Sstevel@tonic-gate Elf_Scn *scn; 10580Sstevel@tonic-gate GElf_Shdr shdr; 10590Sstevel@tonic-gate GElf_Dyn dyn; 10600Sstevel@tonic-gate Elf_Data *data; 10610Sstevel@tonic-gate uint_t i, ndyn; 10620Sstevel@tonic-gate GElf_Xword cksum; 10630Sstevel@tonic-gate uintptr_t addr; 10640Sstevel@tonic-gate 10650Sstevel@tonic-gate if (fptr->file_map == NULL) 10660Sstevel@tonic-gate return (0); 10670Sstevel@tonic-gate 10680Sstevel@tonic-gate if ((Pcontent(P) & (CC_CONTENT_TEXT | CC_CONTENT_DATA)) != 10690Sstevel@tonic-gate (CC_CONTENT_TEXT | CC_CONTENT_DATA)) 10700Sstevel@tonic-gate return (0); 10710Sstevel@tonic-gate 10720Sstevel@tonic-gate /* 10730Sstevel@tonic-gate * First, we find the checksum value in the elf file. 10740Sstevel@tonic-gate */ 10750Sstevel@tonic-gate scn = NULL; 10760Sstevel@tonic-gate while ((scn = elf_nextscn(elf, scn)) != NULL) { 10770Sstevel@tonic-gate if (gelf_getshdr(scn, &shdr) != NULL && 10780Sstevel@tonic-gate shdr.sh_type == SHT_DYNAMIC) 10790Sstevel@tonic-gate goto found_shdr; 10800Sstevel@tonic-gate } 10810Sstevel@tonic-gate return (0); 10820Sstevel@tonic-gate 10830Sstevel@tonic-gate found_shdr: 10840Sstevel@tonic-gate if ((data = elf_getdata(scn, NULL)) == NULL) 10850Sstevel@tonic-gate return (0); 10860Sstevel@tonic-gate 10870Sstevel@tonic-gate if (P->status.pr_dmodel == PR_MODEL_ILP32) 10880Sstevel@tonic-gate ndyn = shdr.sh_size / sizeof (Elf32_Dyn); 10890Sstevel@tonic-gate #ifdef _LP64 10900Sstevel@tonic-gate else if (P->status.pr_dmodel == PR_MODEL_LP64) 10910Sstevel@tonic-gate ndyn = shdr.sh_size / sizeof (Elf64_Dyn); 10920Sstevel@tonic-gate #endif 10930Sstevel@tonic-gate else 10940Sstevel@tonic-gate return (0); 10950Sstevel@tonic-gate 10960Sstevel@tonic-gate for (i = 0; i < ndyn; i++) { 10970Sstevel@tonic-gate if (gelf_getdyn(data, i, &dyn) != NULL && 10980Sstevel@tonic-gate dyn.d_tag == DT_CHECKSUM) 10990Sstevel@tonic-gate goto found_cksum; 11000Sstevel@tonic-gate } 11010Sstevel@tonic-gate return (0); 11020Sstevel@tonic-gate 11030Sstevel@tonic-gate found_cksum: 11040Sstevel@tonic-gate cksum = dyn.d_un.d_val; 11050Sstevel@tonic-gate dprintf("elf cksum value is %llx\n", (u_longlong_t)cksum); 11060Sstevel@tonic-gate 11070Sstevel@tonic-gate /* 11080Sstevel@tonic-gate * Get the base of the text mapping that corresponds to this file. 11090Sstevel@tonic-gate */ 11100Sstevel@tonic-gate addr = fptr->file_map->map_pmap.pr_vaddr; 11110Sstevel@tonic-gate 11120Sstevel@tonic-gate if (P->status.pr_dmodel == PR_MODEL_ILP32) { 11130Sstevel@tonic-gate Elf32_Ehdr ehdr; 11140Sstevel@tonic-gate Elf32_Phdr phdr; 11150Sstevel@tonic-gate Elf32_Dyn dync, *dynp; 11160Sstevel@tonic-gate uint_t i; 11170Sstevel@tonic-gate 11180Sstevel@tonic-gate if (read_ehdr32(P, &ehdr, addr) != 0 || 11190Sstevel@tonic-gate read_dynamic_phdr32(P, &ehdr, &phdr, addr) != 0) 11200Sstevel@tonic-gate return (0); 11210Sstevel@tonic-gate 11220Sstevel@tonic-gate if (ehdr.e_type == ET_DYN) 11230Sstevel@tonic-gate phdr.p_vaddr += addr; 11240Sstevel@tonic-gate if ((dynp = malloc(phdr.p_filesz)) == NULL) 11250Sstevel@tonic-gate return (0); 11260Sstevel@tonic-gate dync.d_tag = DT_NULL; 11270Sstevel@tonic-gate if (Pread(P, dynp, phdr.p_filesz, phdr.p_vaddr) != 11280Sstevel@tonic-gate phdr.p_filesz) { 11290Sstevel@tonic-gate free(dynp); 11300Sstevel@tonic-gate return (0); 11310Sstevel@tonic-gate } 11320Sstevel@tonic-gate 11330Sstevel@tonic-gate for (i = 0; i < phdr.p_filesz / sizeof (Elf32_Dyn); i++) { 11340Sstevel@tonic-gate if (dynp[i].d_tag == DT_CHECKSUM) 11350Sstevel@tonic-gate dync = dynp[i]; 11360Sstevel@tonic-gate } 11370Sstevel@tonic-gate 11380Sstevel@tonic-gate free(dynp); 11390Sstevel@tonic-gate 11400Sstevel@tonic-gate if (dync.d_tag != DT_CHECKSUM) 11410Sstevel@tonic-gate return (0); 11420Sstevel@tonic-gate 11430Sstevel@tonic-gate dprintf("image cksum value is %llx\n", 11440Sstevel@tonic-gate (u_longlong_t)dync.d_un.d_val); 11450Sstevel@tonic-gate return (dync.d_un.d_val != cksum); 11460Sstevel@tonic-gate #ifdef _LP64 11470Sstevel@tonic-gate } else if (P->status.pr_dmodel == PR_MODEL_LP64) { 11480Sstevel@tonic-gate Elf64_Ehdr ehdr; 11490Sstevel@tonic-gate Elf64_Phdr phdr; 11500Sstevel@tonic-gate Elf64_Dyn dync, *dynp; 11510Sstevel@tonic-gate uint_t i; 11520Sstevel@tonic-gate 11530Sstevel@tonic-gate if (read_ehdr64(P, &ehdr, addr) != 0 || 11540Sstevel@tonic-gate read_dynamic_phdr64(P, &ehdr, &phdr, addr) != 0) 11550Sstevel@tonic-gate return (0); 11560Sstevel@tonic-gate 11570Sstevel@tonic-gate if (ehdr.e_type == ET_DYN) 11580Sstevel@tonic-gate phdr.p_vaddr += addr; 11590Sstevel@tonic-gate if ((dynp = malloc(phdr.p_filesz)) == NULL) 11600Sstevel@tonic-gate return (0); 11610Sstevel@tonic-gate dync.d_tag = DT_NULL; 11620Sstevel@tonic-gate if (Pread(P, dynp, phdr.p_filesz, phdr.p_vaddr) != 11630Sstevel@tonic-gate phdr.p_filesz) { 11640Sstevel@tonic-gate free(dynp); 11650Sstevel@tonic-gate return (0); 11660Sstevel@tonic-gate } 11670Sstevel@tonic-gate 11680Sstevel@tonic-gate for (i = 0; i < phdr.p_filesz / sizeof (Elf64_Dyn); i++) { 11690Sstevel@tonic-gate if (dynp[i].d_tag == DT_CHECKSUM) 11700Sstevel@tonic-gate dync = dynp[i]; 11710Sstevel@tonic-gate } 11720Sstevel@tonic-gate 11730Sstevel@tonic-gate free(dynp); 11740Sstevel@tonic-gate 11750Sstevel@tonic-gate if (dync.d_tag != DT_CHECKSUM) 11760Sstevel@tonic-gate return (0); 11770Sstevel@tonic-gate 11780Sstevel@tonic-gate dprintf("image cksum value is %llx\n", 11790Sstevel@tonic-gate (u_longlong_t)dync.d_un.d_val); 11800Sstevel@tonic-gate return (dync.d_un.d_val != cksum); 11810Sstevel@tonic-gate #endif /* _LP64 */ 11820Sstevel@tonic-gate } 11830Sstevel@tonic-gate 11840Sstevel@tonic-gate return (0); 11850Sstevel@tonic-gate } 11860Sstevel@tonic-gate 11870Sstevel@tonic-gate static Elf * 11880Sstevel@tonic-gate fake_elf(struct ps_prochandle *P, file_info_t *fptr) 11890Sstevel@tonic-gate { 11900Sstevel@tonic-gate enum { 11910Sstevel@tonic-gate DI_PLTGOT = 0, 11920Sstevel@tonic-gate DI_JMPREL, 11930Sstevel@tonic-gate DI_PLTRELSZ, 11940Sstevel@tonic-gate DI_PLTREL, 11950Sstevel@tonic-gate DI_SYMTAB, 11960Sstevel@tonic-gate DI_HASH, 11970Sstevel@tonic-gate DI_SYMENT, 11980Sstevel@tonic-gate DI_STRTAB, 11990Sstevel@tonic-gate DI_STRSZ, 12000Sstevel@tonic-gate DI_NENT 12010Sstevel@tonic-gate }; 12020Sstevel@tonic-gate uintptr_t addr; 12030Sstevel@tonic-gate size_t size = 0; 12040Sstevel@tonic-gate caddr_t elfdata = NULL; 12050Sstevel@tonic-gate Elf *elf; 12060Sstevel@tonic-gate Elf32_Word nchain; 12070Sstevel@tonic-gate static char shstr[] = ".shstrtab\0.dynsym\0.dynstr\0.dynamic\0.plt"; 12080Sstevel@tonic-gate 12090Sstevel@tonic-gate if (fptr->file_map == NULL) 12100Sstevel@tonic-gate return (NULL); 12110Sstevel@tonic-gate 12120Sstevel@tonic-gate if ((Pcontent(P) & (CC_CONTENT_TEXT | CC_CONTENT_DATA)) != 12130Sstevel@tonic-gate (CC_CONTENT_TEXT | CC_CONTENT_DATA)) 12140Sstevel@tonic-gate return (NULL); 12150Sstevel@tonic-gate 12160Sstevel@tonic-gate addr = fptr->file_map->map_pmap.pr_vaddr; 12170Sstevel@tonic-gate 12180Sstevel@tonic-gate /* 12190Sstevel@tonic-gate * We're building a in memory elf file that will let us use libelf 12200Sstevel@tonic-gate * for most of the work we need to later (e.g. symbol table lookups). 12210Sstevel@tonic-gate * We need sections for the dynsym, dynstr, and plt, and we need 12220Sstevel@tonic-gate * the program headers from the text section. The former is used in 12230Sstevel@tonic-gate * Pbuild_file_symtab(); the latter is used in several functions in 12240Sstevel@tonic-gate * Pcore.c to reconstruct the origin of each mapping from the load 12250Sstevel@tonic-gate * object that spawned it. 12260Sstevel@tonic-gate * 12270Sstevel@tonic-gate * Here are some useful pieces of elf trivia that will help 12280Sstevel@tonic-gate * to elucidate this code. 12290Sstevel@tonic-gate * 12300Sstevel@tonic-gate * All the information we need about the dynstr can be found in these 12310Sstevel@tonic-gate * two entries in the dynamic section: 12320Sstevel@tonic-gate * 12330Sstevel@tonic-gate * DT_STRTAB base of dynstr 12340Sstevel@tonic-gate * DT_STRSZ size of dynstr 12350Sstevel@tonic-gate * 12360Sstevel@tonic-gate * So deciphering the dynstr is pretty straightforward. 12370Sstevel@tonic-gate * 12380Sstevel@tonic-gate * The dynsym is a little trickier. 12390Sstevel@tonic-gate * 12400Sstevel@tonic-gate * DT_SYMTAB base of dynsym 12410Sstevel@tonic-gate * DT_SYMENT size of a dynstr entry (Elf{32,64}_Sym) 12420Sstevel@tonic-gate * DT_HASH base of hash table for dynamic lookups 12430Sstevel@tonic-gate * 12440Sstevel@tonic-gate * The DT_SYMTAB entry gives us any easy way of getting to the base 12450Sstevel@tonic-gate * of the dynsym, but getting the size involves rooting around in the 12460Sstevel@tonic-gate * dynamic lookup hash table. Here's the layout of the hash table: 12470Sstevel@tonic-gate * 12480Sstevel@tonic-gate * +-------------------+ 12490Sstevel@tonic-gate * | nbucket | All values are of type 12500Sstevel@tonic-gate * +-------------------+ Elf32_Word 12510Sstevel@tonic-gate * | nchain | 12520Sstevel@tonic-gate * +-------------------+ 12530Sstevel@tonic-gate * | bucket[0] | 12540Sstevel@tonic-gate * | . . . | 12550Sstevel@tonic-gate * | bucket[nbucket-1] | 12560Sstevel@tonic-gate * +-------------------+ 12570Sstevel@tonic-gate * | chain[0] | 12580Sstevel@tonic-gate * | . . . | 12590Sstevel@tonic-gate * | chain[nchain-1] | 12600Sstevel@tonic-gate * +-------------------+ 12610Sstevel@tonic-gate * (figure 5-12 from the SYS V Generic ABI) 12620Sstevel@tonic-gate * 12630Sstevel@tonic-gate * Symbols names are hashed into a particular bucket which contains 12640Sstevel@tonic-gate * an index into the symbol table. Each entry in the symbol table 12650Sstevel@tonic-gate * has a corresponding entry in the chain table which tells the 12660Sstevel@tonic-gate * consumer where the next entry in the hash chain is. We can use 12670Sstevel@tonic-gate * the nchain field to find out the size of the dynsym. 12680Sstevel@tonic-gate * 12690Sstevel@tonic-gate * We can figure out the size of the .plt section, but it takes some 12700Sstevel@tonic-gate * doing. We need to use the following information: 12710Sstevel@tonic-gate * 12720Sstevel@tonic-gate * DT_PLTGOT base of the PLT 12730Sstevel@tonic-gate * DT_JMPREL base of the PLT's relocation section 12740Sstevel@tonic-gate * DT_PLTRELSZ size of the PLT's relocation section 12750Sstevel@tonic-gate * DT_PLTREL type of the PLT's relocation section 12760Sstevel@tonic-gate * 12770Sstevel@tonic-gate * We can use the relocation section to figure out the address of the 12780Sstevel@tonic-gate * last entry and subtract off the value of DT_PLTGOT to calculate 12790Sstevel@tonic-gate * the size of the PLT. 12800Sstevel@tonic-gate * 12810Sstevel@tonic-gate * For more information, check out the System V Generic ABI. 12820Sstevel@tonic-gate */ 12830Sstevel@tonic-gate 12840Sstevel@tonic-gate if (P->status.pr_dmodel == PR_MODEL_ILP32) { 12850Sstevel@tonic-gate Elf32_Ehdr ehdr, *ep; 12860Sstevel@tonic-gate Elf32_Phdr phdr; 12870Sstevel@tonic-gate Elf32_Shdr *sp; 12880Sstevel@tonic-gate Elf32_Dyn *dp; 12890Sstevel@tonic-gate Elf32_Dyn *d[DI_NENT] = { 0 }; 12900Sstevel@tonic-gate uint_t i, dcount = 0; 12910Sstevel@tonic-gate uint32_t off; 12920Sstevel@tonic-gate size_t pltsz = 0, pltentsz; 12930Sstevel@tonic-gate 12940Sstevel@tonic-gate if (read_ehdr32(P, &ehdr, addr) != 0 || 12950Sstevel@tonic-gate read_dynamic_phdr32(P, &ehdr, &phdr, addr) != 0) 12960Sstevel@tonic-gate return (NULL); 12970Sstevel@tonic-gate 12980Sstevel@tonic-gate if (ehdr.e_type == ET_DYN) 12990Sstevel@tonic-gate phdr.p_vaddr += addr; 13000Sstevel@tonic-gate 13010Sstevel@tonic-gate if ((dp = malloc(phdr.p_filesz)) == NULL) 13020Sstevel@tonic-gate return (NULL); 13030Sstevel@tonic-gate 13040Sstevel@tonic-gate if (Pread(P, dp, phdr.p_filesz, phdr.p_vaddr) != 13050Sstevel@tonic-gate phdr.p_filesz) { 13060Sstevel@tonic-gate free(dp); 13070Sstevel@tonic-gate return (NULL); 13080Sstevel@tonic-gate } 13090Sstevel@tonic-gate 13100Sstevel@tonic-gate for (i = 0; i < phdr.p_filesz / sizeof (Elf32_Dyn); i++) { 13110Sstevel@tonic-gate switch (dp[i].d_tag) { 13120Sstevel@tonic-gate /* 13130Sstevel@tonic-gate * For the .plt section. 13140Sstevel@tonic-gate */ 13150Sstevel@tonic-gate case DT_PLTGOT: 13160Sstevel@tonic-gate d[DI_PLTGOT] = &dp[i]; 13170Sstevel@tonic-gate continue; 13180Sstevel@tonic-gate case DT_JMPREL: 13190Sstevel@tonic-gate d[DI_JMPREL] = &dp[i]; 13200Sstevel@tonic-gate continue; 13210Sstevel@tonic-gate case DT_PLTRELSZ: 13220Sstevel@tonic-gate d[DI_PLTRELSZ] = &dp[i]; 13230Sstevel@tonic-gate continue; 13240Sstevel@tonic-gate case DT_PLTREL: 13250Sstevel@tonic-gate d[DI_PLTREL] = &dp[i]; 13260Sstevel@tonic-gate continue; 13270Sstevel@tonic-gate default: 13280Sstevel@tonic-gate continue; 13290Sstevel@tonic-gate 13300Sstevel@tonic-gate /* 13310Sstevel@tonic-gate * For the .dynsym section. 13320Sstevel@tonic-gate */ 13330Sstevel@tonic-gate case DT_SYMTAB: 13340Sstevel@tonic-gate d[DI_SYMTAB] = &dp[i]; 13350Sstevel@tonic-gate break; 13360Sstevel@tonic-gate case DT_HASH: 13370Sstevel@tonic-gate d[DI_HASH] = &dp[i]; 13380Sstevel@tonic-gate break; 13390Sstevel@tonic-gate case DT_SYMENT: 13400Sstevel@tonic-gate d[DI_SYMENT] = &dp[i]; 13410Sstevel@tonic-gate break; 13420Sstevel@tonic-gate 13430Sstevel@tonic-gate /* 13440Sstevel@tonic-gate * For the .dynstr section. 13450Sstevel@tonic-gate */ 13460Sstevel@tonic-gate case DT_STRTAB: 13470Sstevel@tonic-gate d[DI_STRTAB] = &dp[i]; 13480Sstevel@tonic-gate break; 13490Sstevel@tonic-gate case DT_STRSZ: 13500Sstevel@tonic-gate d[DI_STRSZ] = &dp[i]; 13510Sstevel@tonic-gate break; 13520Sstevel@tonic-gate } 13530Sstevel@tonic-gate 13540Sstevel@tonic-gate dcount++; 13550Sstevel@tonic-gate } 13560Sstevel@tonic-gate 13570Sstevel@tonic-gate /* 13580Sstevel@tonic-gate * We need all of those dynamic entries in order to put 13590Sstevel@tonic-gate * together a complete set of elf sections, but we'll 13600Sstevel@tonic-gate * let the PLT section slide if need be. The dynsym- and 13610Sstevel@tonic-gate * dynstr-related dynamic entries are mandatory in both 13620Sstevel@tonic-gate * executables and shared objects so if one of those is 13630Sstevel@tonic-gate * missing, we're in some trouble and should abort. 13640Sstevel@tonic-gate */ 13650Sstevel@tonic-gate if (dcount + 4 != DI_NENT) { 13660Sstevel@tonic-gate dprintf("text section missing required dynamic " 13670Sstevel@tonic-gate "entries\n"); 13680Sstevel@tonic-gate return (NULL); 13690Sstevel@tonic-gate } 13700Sstevel@tonic-gate 13710Sstevel@tonic-gate if (ehdr.e_type == ET_DYN) { 13720Sstevel@tonic-gate if (d[DI_PLTGOT] != NULL) 13730Sstevel@tonic-gate d[DI_PLTGOT]->d_un.d_ptr += addr; 13740Sstevel@tonic-gate if (d[DI_JMPREL] != NULL) 13750Sstevel@tonic-gate d[DI_JMPREL]->d_un.d_ptr += addr; 13760Sstevel@tonic-gate d[DI_SYMTAB]->d_un.d_ptr += addr; 13770Sstevel@tonic-gate d[DI_HASH]->d_un.d_ptr += addr; 13780Sstevel@tonic-gate d[DI_STRTAB]->d_un.d_ptr += addr; 13790Sstevel@tonic-gate } 13800Sstevel@tonic-gate 13810Sstevel@tonic-gate /* elf header */ 13820Sstevel@tonic-gate size = sizeof (Elf32_Ehdr); 13830Sstevel@tonic-gate 13840Sstevel@tonic-gate /* program headers from in-core elf fragment */ 13850Sstevel@tonic-gate size += ehdr.e_phnum * ehdr.e_phentsize; 13860Sstevel@tonic-gate 13870Sstevel@tonic-gate /* unused shdr, and .shstrtab section */ 13880Sstevel@tonic-gate size += sizeof (Elf32_Shdr); 13890Sstevel@tonic-gate size += sizeof (Elf32_Shdr); 13900Sstevel@tonic-gate size += roundup(sizeof (shstr), 4); 13910Sstevel@tonic-gate 13920Sstevel@tonic-gate /* .dynsym section */ 13930Sstevel@tonic-gate size += sizeof (Elf32_Shdr); 13940Sstevel@tonic-gate if (Pread(P, &nchain, sizeof (nchain), 13950Sstevel@tonic-gate d[DI_HASH]->d_un.d_ptr + 4) != sizeof (nchain)) 13960Sstevel@tonic-gate goto bad32; 13970Sstevel@tonic-gate size += sizeof (Elf32_Sym) * nchain; 13980Sstevel@tonic-gate 13990Sstevel@tonic-gate /* .dynstr section */ 14000Sstevel@tonic-gate size += sizeof (Elf32_Shdr); 14010Sstevel@tonic-gate size += roundup(d[DI_STRSZ]->d_un.d_val, 4); 14020Sstevel@tonic-gate 14030Sstevel@tonic-gate /* .dynamic section */ 14040Sstevel@tonic-gate size += sizeof (Elf32_Shdr); 14050Sstevel@tonic-gate size += roundup(phdr.p_filesz, 4); 14060Sstevel@tonic-gate 14070Sstevel@tonic-gate /* .plt section */ 14080Sstevel@tonic-gate if (d[DI_PLTGOT] != NULL && d[DI_JMPREL] != NULL && 14090Sstevel@tonic-gate d[DI_PLTRELSZ] != NULL && d[DI_PLTREL] != NULL) { 14100Sstevel@tonic-gate uintptr_t penult, ult; 14110Sstevel@tonic-gate uintptr_t jmprel = d[DI_JMPREL]->d_un.d_ptr; 14120Sstevel@tonic-gate size_t pltrelsz = d[DI_PLTRELSZ]->d_un.d_val; 14130Sstevel@tonic-gate 14140Sstevel@tonic-gate if (d[DI_PLTREL]->d_un.d_val == DT_RELA) { 14150Sstevel@tonic-gate uint_t ndx = pltrelsz / sizeof (Elf32_Rela) - 2; 14160Sstevel@tonic-gate Elf32_Rela r[2]; 14170Sstevel@tonic-gate 14180Sstevel@tonic-gate if (Pread(P, r, sizeof (r), jmprel + 14190Sstevel@tonic-gate sizeof (r[0]) * ndx) != sizeof (r)) 14200Sstevel@tonic-gate goto bad32; 14210Sstevel@tonic-gate 14220Sstevel@tonic-gate penult = r[0].r_offset; 14230Sstevel@tonic-gate ult = r[1].r_offset; 14240Sstevel@tonic-gate 14250Sstevel@tonic-gate } else if (d[DI_PLTREL]->d_un.d_val == DT_REL) { 14260Sstevel@tonic-gate uint_t ndx = pltrelsz / sizeof (Elf32_Rel) - 2; 14270Sstevel@tonic-gate Elf32_Rel r[2]; 14280Sstevel@tonic-gate 14290Sstevel@tonic-gate if (Pread(P, r, sizeof (r), jmprel + 14300Sstevel@tonic-gate sizeof (r[0]) * ndx) != sizeof (r)) 14310Sstevel@tonic-gate goto bad32; 14320Sstevel@tonic-gate 14330Sstevel@tonic-gate penult = r[0].r_offset; 14340Sstevel@tonic-gate ult = r[1].r_offset; 14350Sstevel@tonic-gate } else { 14360Sstevel@tonic-gate goto bad32; 14370Sstevel@tonic-gate } 14380Sstevel@tonic-gate 14390Sstevel@tonic-gate pltentsz = ult - penult; 14400Sstevel@tonic-gate 14410Sstevel@tonic-gate if (ehdr.e_type == ET_DYN) 14420Sstevel@tonic-gate ult += addr; 14430Sstevel@tonic-gate 14440Sstevel@tonic-gate pltsz = ult - d[DI_PLTGOT]->d_un.d_ptr + pltentsz; 14450Sstevel@tonic-gate 14460Sstevel@tonic-gate size += sizeof (Elf32_Shdr); 14470Sstevel@tonic-gate size += roundup(pltsz, 4); 14480Sstevel@tonic-gate } 14490Sstevel@tonic-gate 14500Sstevel@tonic-gate if ((elfdata = calloc(1, size)) == NULL) 14510Sstevel@tonic-gate goto bad32; 14520Sstevel@tonic-gate 14530Sstevel@tonic-gate /* LINTED - alignment */ 14540Sstevel@tonic-gate ep = (Elf32_Ehdr *)elfdata; 14550Sstevel@tonic-gate (void) memcpy(ep, &ehdr, offsetof(Elf32_Ehdr, e_phoff)); 14560Sstevel@tonic-gate 14570Sstevel@tonic-gate ep->e_ehsize = sizeof (Elf32_Ehdr); 14580Sstevel@tonic-gate ep->e_phoff = sizeof (Elf32_Ehdr); 14590Sstevel@tonic-gate ep->e_phentsize = ehdr.e_phentsize; 14600Sstevel@tonic-gate ep->e_phnum = ehdr.e_phnum; 14610Sstevel@tonic-gate ep->e_shoff = ep->e_phoff + ep->e_phnum * ep->e_phentsize; 14620Sstevel@tonic-gate ep->e_shentsize = sizeof (Elf32_Shdr); 14630Sstevel@tonic-gate ep->e_shnum = (pltsz == 0) ? 5 : 6; 14640Sstevel@tonic-gate ep->e_shstrndx = 1; 14650Sstevel@tonic-gate 14660Sstevel@tonic-gate /* LINTED - alignment */ 14670Sstevel@tonic-gate sp = (Elf32_Shdr *)(elfdata + ep->e_shoff); 14680Sstevel@tonic-gate off = ep->e_shoff + ep->e_shentsize * ep->e_shnum; 14690Sstevel@tonic-gate 14700Sstevel@tonic-gate /* 14710Sstevel@tonic-gate * Copying the program headers directly from the process's 14720Sstevel@tonic-gate * address space is a little suspect, but since we only 14730Sstevel@tonic-gate * use them for their address and size values, this is fine. 14740Sstevel@tonic-gate */ 14750Sstevel@tonic-gate if (Pread(P, &elfdata[ep->e_phoff], 14760Sstevel@tonic-gate ep->e_phnum * ep->e_phentsize, addr + ehdr.e_phoff) != 14770Sstevel@tonic-gate ep->e_phnum * ep->e_phentsize) { 14780Sstevel@tonic-gate free(elfdata); 14790Sstevel@tonic-gate goto bad32; 14800Sstevel@tonic-gate } 14810Sstevel@tonic-gate 14820Sstevel@tonic-gate /* 14830Sstevel@tonic-gate * The first elf section is always skipped. 14840Sstevel@tonic-gate */ 14850Sstevel@tonic-gate sp++; 14860Sstevel@tonic-gate 14870Sstevel@tonic-gate /* 14880Sstevel@tonic-gate * Section Header[1] sh_name: .shstrtab 14890Sstevel@tonic-gate */ 14900Sstevel@tonic-gate sp->sh_name = 0; 14910Sstevel@tonic-gate sp->sh_type = SHT_STRTAB; 14920Sstevel@tonic-gate sp->sh_flags = SHF_STRINGS; 14930Sstevel@tonic-gate sp->sh_addr = 0; 14940Sstevel@tonic-gate sp->sh_offset = off; 14950Sstevel@tonic-gate sp->sh_size = sizeof (shstr); 14960Sstevel@tonic-gate sp->sh_link = 0; 14970Sstevel@tonic-gate sp->sh_info = 0; 14980Sstevel@tonic-gate sp->sh_addralign = 1; 14990Sstevel@tonic-gate sp->sh_entsize = 0; 15000Sstevel@tonic-gate 15010Sstevel@tonic-gate (void) memcpy(&elfdata[off], shstr, sizeof (shstr)); 15020Sstevel@tonic-gate off += roundup(sp->sh_size, 4); 15030Sstevel@tonic-gate sp++; 15040Sstevel@tonic-gate 15050Sstevel@tonic-gate /* 15060Sstevel@tonic-gate * Section Header[2] sh_name: .dynsym 15070Sstevel@tonic-gate */ 15080Sstevel@tonic-gate sp->sh_name = 10; 15090Sstevel@tonic-gate sp->sh_type = SHT_DYNSYM; 15100Sstevel@tonic-gate sp->sh_flags = SHF_ALLOC; 15110Sstevel@tonic-gate sp->sh_addr = d[DI_SYMTAB]->d_un.d_ptr; 15120Sstevel@tonic-gate if (ehdr.e_type == ET_DYN) 15130Sstevel@tonic-gate sp->sh_addr -= addr; 15140Sstevel@tonic-gate sp->sh_offset = off; 15150Sstevel@tonic-gate sp->sh_size = nchain * sizeof (Elf32_Sym); 15160Sstevel@tonic-gate sp->sh_link = 3; 15170Sstevel@tonic-gate sp->sh_info = 1; 15180Sstevel@tonic-gate sp->sh_addralign = 4; 15190Sstevel@tonic-gate sp->sh_entsize = sizeof (Elf32_Sym); 15200Sstevel@tonic-gate 15210Sstevel@tonic-gate if (Pread(P, &elfdata[off], sp->sh_size, 15220Sstevel@tonic-gate d[DI_SYMTAB]->d_un.d_ptr) != sp->sh_size) { 15230Sstevel@tonic-gate free(elfdata); 15240Sstevel@tonic-gate goto bad32; 15250Sstevel@tonic-gate } 15260Sstevel@tonic-gate 15270Sstevel@tonic-gate off += roundup(sp->sh_size, 4); 15280Sstevel@tonic-gate sp++; 15290Sstevel@tonic-gate 15300Sstevel@tonic-gate /* 15310Sstevel@tonic-gate * Section Header[3] sh_name: .dynstr 15320Sstevel@tonic-gate */ 15330Sstevel@tonic-gate sp->sh_name = 18; 15340Sstevel@tonic-gate sp->sh_type = SHT_STRTAB; 15350Sstevel@tonic-gate sp->sh_flags = SHF_ALLOC | SHF_STRINGS; 15360Sstevel@tonic-gate sp->sh_addr = d[DI_STRTAB]->d_un.d_ptr; 15370Sstevel@tonic-gate if (ehdr.e_type == ET_DYN) 15380Sstevel@tonic-gate sp->sh_addr -= addr; 15390Sstevel@tonic-gate sp->sh_offset = off; 15400Sstevel@tonic-gate sp->sh_size = d[DI_STRSZ]->d_un.d_val; 15410Sstevel@tonic-gate sp->sh_link = 0; 15420Sstevel@tonic-gate sp->sh_info = 0; 15430Sstevel@tonic-gate sp->sh_addralign = 1; 15440Sstevel@tonic-gate sp->sh_entsize = 0; 15450Sstevel@tonic-gate 15460Sstevel@tonic-gate if (Pread(P, &elfdata[off], sp->sh_size, 15470Sstevel@tonic-gate d[DI_STRTAB]->d_un.d_ptr) != sp->sh_size) { 15480Sstevel@tonic-gate free(elfdata); 15490Sstevel@tonic-gate goto bad32; 15500Sstevel@tonic-gate } 15510Sstevel@tonic-gate off += roundup(sp->sh_size, 4); 15520Sstevel@tonic-gate sp++; 15530Sstevel@tonic-gate 15540Sstevel@tonic-gate /* 15550Sstevel@tonic-gate * Section Header[4] sh_name: .dynamic 15560Sstevel@tonic-gate */ 15570Sstevel@tonic-gate sp->sh_name = 26; 15580Sstevel@tonic-gate sp->sh_type = SHT_DYNAMIC; 15590Sstevel@tonic-gate sp->sh_flags = SHF_WRITE | SHF_ALLOC; 15600Sstevel@tonic-gate sp->sh_addr = phdr.p_vaddr; 15610Sstevel@tonic-gate if (ehdr.e_type == ET_DYN) 15620Sstevel@tonic-gate sp->sh_addr -= addr; 15630Sstevel@tonic-gate sp->sh_offset = off; 15640Sstevel@tonic-gate sp->sh_size = phdr.p_filesz; 15650Sstevel@tonic-gate sp->sh_link = 3; 15660Sstevel@tonic-gate sp->sh_info = 0; 15670Sstevel@tonic-gate sp->sh_addralign = 4; 15680Sstevel@tonic-gate sp->sh_entsize = sizeof (Elf32_Dyn); 15690Sstevel@tonic-gate 15700Sstevel@tonic-gate (void) memcpy(&elfdata[off], dp, sp->sh_size); 15710Sstevel@tonic-gate off += roundup(sp->sh_size, 4); 15720Sstevel@tonic-gate sp++; 15730Sstevel@tonic-gate 15740Sstevel@tonic-gate /* 15750Sstevel@tonic-gate * Section Header[5] sh_name: .plt 15760Sstevel@tonic-gate */ 15770Sstevel@tonic-gate if (pltsz != 0) { 15780Sstevel@tonic-gate sp->sh_name = 35; 15790Sstevel@tonic-gate sp->sh_type = SHT_PROGBITS; 15800Sstevel@tonic-gate sp->sh_flags = SHF_WRITE | SHF_ALLOC | SHF_EXECINSTR; 15810Sstevel@tonic-gate sp->sh_addr = d[DI_PLTGOT]->d_un.d_ptr; 15820Sstevel@tonic-gate if (ehdr.e_type == ET_DYN) 15830Sstevel@tonic-gate sp->sh_addr -= addr; 15840Sstevel@tonic-gate sp->sh_offset = off; 15850Sstevel@tonic-gate sp->sh_size = pltsz; 15860Sstevel@tonic-gate sp->sh_link = 0; 15870Sstevel@tonic-gate sp->sh_info = 0; 15880Sstevel@tonic-gate sp->sh_addralign = 4; 15890Sstevel@tonic-gate sp->sh_entsize = pltentsz; 15900Sstevel@tonic-gate 15910Sstevel@tonic-gate if (Pread(P, &elfdata[off], sp->sh_size, 15920Sstevel@tonic-gate d[DI_PLTGOT]->d_un.d_ptr) != sp->sh_size) { 15930Sstevel@tonic-gate free(elfdata); 15940Sstevel@tonic-gate goto bad32; 15950Sstevel@tonic-gate } 15960Sstevel@tonic-gate off += roundup(sp->sh_size, 4); 15970Sstevel@tonic-gate sp++; 15980Sstevel@tonic-gate } 15990Sstevel@tonic-gate 16000Sstevel@tonic-gate free(dp); 16010Sstevel@tonic-gate goto good; 16020Sstevel@tonic-gate 16030Sstevel@tonic-gate bad32: 16040Sstevel@tonic-gate free(dp); 16050Sstevel@tonic-gate return (NULL); 16060Sstevel@tonic-gate #ifdef _LP64 16070Sstevel@tonic-gate } else if (P->status.pr_dmodel == PR_MODEL_LP64) { 16080Sstevel@tonic-gate Elf64_Ehdr ehdr, *ep; 16090Sstevel@tonic-gate Elf64_Phdr phdr; 16100Sstevel@tonic-gate Elf64_Shdr *sp; 16110Sstevel@tonic-gate Elf64_Dyn *dp; 16120Sstevel@tonic-gate Elf64_Dyn *d[DI_NENT] = { 0 }; 16130Sstevel@tonic-gate uint_t i, dcount = 0; 16140Sstevel@tonic-gate uint64_t off; 16150Sstevel@tonic-gate size_t pltsz = 0, pltentsz; 16160Sstevel@tonic-gate 16170Sstevel@tonic-gate if (read_ehdr64(P, &ehdr, addr) != 0 || 16180Sstevel@tonic-gate read_dynamic_phdr64(P, &ehdr, &phdr, addr) != 0) 16190Sstevel@tonic-gate return (NULL); 16200Sstevel@tonic-gate 16210Sstevel@tonic-gate if (ehdr.e_type == ET_DYN) 16220Sstevel@tonic-gate phdr.p_vaddr += addr; 16230Sstevel@tonic-gate 16240Sstevel@tonic-gate if ((dp = malloc(phdr.p_filesz)) == NULL) 16250Sstevel@tonic-gate return (NULL); 16260Sstevel@tonic-gate 16270Sstevel@tonic-gate if (Pread(P, dp, phdr.p_filesz, phdr.p_vaddr) != 16280Sstevel@tonic-gate phdr.p_filesz) { 16290Sstevel@tonic-gate free(dp); 16300Sstevel@tonic-gate return (NULL); 16310Sstevel@tonic-gate } 16320Sstevel@tonic-gate 16330Sstevel@tonic-gate for (i = 0; i < phdr.p_filesz / sizeof (Elf64_Dyn); i++) { 16340Sstevel@tonic-gate switch (dp[i].d_tag) { 16350Sstevel@tonic-gate /* 16360Sstevel@tonic-gate * For the .plt section. 16370Sstevel@tonic-gate */ 16380Sstevel@tonic-gate case DT_PLTGOT: 16390Sstevel@tonic-gate d[DI_PLTGOT] = &dp[i]; 16400Sstevel@tonic-gate continue; 16410Sstevel@tonic-gate case DT_JMPREL: 16420Sstevel@tonic-gate d[DI_JMPREL] = &dp[i]; 16430Sstevel@tonic-gate continue; 16440Sstevel@tonic-gate case DT_PLTRELSZ: 16450Sstevel@tonic-gate d[DI_PLTRELSZ] = &dp[i]; 16460Sstevel@tonic-gate continue; 16470Sstevel@tonic-gate case DT_PLTREL: 16480Sstevel@tonic-gate d[DI_PLTREL] = &dp[i]; 16490Sstevel@tonic-gate continue; 16500Sstevel@tonic-gate default: 16510Sstevel@tonic-gate continue; 16520Sstevel@tonic-gate 16530Sstevel@tonic-gate /* 16540Sstevel@tonic-gate * For the .dynsym section. 16550Sstevel@tonic-gate */ 16560Sstevel@tonic-gate case DT_SYMTAB: 16570Sstevel@tonic-gate d[DI_SYMTAB] = &dp[i]; 16580Sstevel@tonic-gate break; 16590Sstevel@tonic-gate case DT_HASH: 16600Sstevel@tonic-gate d[DI_HASH] = &dp[i]; 16610Sstevel@tonic-gate break; 16620Sstevel@tonic-gate case DT_SYMENT: 16630Sstevel@tonic-gate d[DI_SYMENT] = &dp[i]; 16640Sstevel@tonic-gate break; 16650Sstevel@tonic-gate 16660Sstevel@tonic-gate /* 16670Sstevel@tonic-gate * For the .dynstr section. 16680Sstevel@tonic-gate */ 16690Sstevel@tonic-gate case DT_STRTAB: 16700Sstevel@tonic-gate d[DI_STRTAB] = &dp[i]; 16710Sstevel@tonic-gate break; 16720Sstevel@tonic-gate case DT_STRSZ: 16730Sstevel@tonic-gate d[DI_STRSZ] = &dp[i]; 16740Sstevel@tonic-gate break; 16750Sstevel@tonic-gate } 16760Sstevel@tonic-gate 16770Sstevel@tonic-gate dcount++; 16780Sstevel@tonic-gate } 16790Sstevel@tonic-gate 16800Sstevel@tonic-gate /* 16810Sstevel@tonic-gate * We need all of those dynamic entries in order to put 16820Sstevel@tonic-gate * together a complete set of elf sections, but we'll 16830Sstevel@tonic-gate * let the PLT section slide if need be. The dynsym- and 16840Sstevel@tonic-gate * dynstr-related dynamic entries are mandatory in both 16850Sstevel@tonic-gate * executables and shared objects so if one of those is 16860Sstevel@tonic-gate * missing, we're in some trouble and should abort. 16870Sstevel@tonic-gate */ 16880Sstevel@tonic-gate if (dcount + 4 != DI_NENT) { 16890Sstevel@tonic-gate dprintf("text section missing required dynamic " 16900Sstevel@tonic-gate "entries\n"); 16910Sstevel@tonic-gate return (NULL); 16920Sstevel@tonic-gate } 16930Sstevel@tonic-gate 16940Sstevel@tonic-gate if (ehdr.e_type == ET_DYN) { 16950Sstevel@tonic-gate if (d[DI_PLTGOT] != NULL) 16960Sstevel@tonic-gate d[DI_PLTGOT]->d_un.d_ptr += addr; 16970Sstevel@tonic-gate if (d[DI_JMPREL] != NULL) 16980Sstevel@tonic-gate d[DI_JMPREL]->d_un.d_ptr += addr; 16990Sstevel@tonic-gate d[DI_SYMTAB]->d_un.d_ptr += addr; 17000Sstevel@tonic-gate d[DI_HASH]->d_un.d_ptr += addr; 17010Sstevel@tonic-gate d[DI_STRTAB]->d_un.d_ptr += addr; 17020Sstevel@tonic-gate } 17030Sstevel@tonic-gate 17040Sstevel@tonic-gate /* elf header */ 17050Sstevel@tonic-gate size = sizeof (Elf64_Ehdr); 17060Sstevel@tonic-gate 17070Sstevel@tonic-gate /* program headers from in-core elf fragment */ 17080Sstevel@tonic-gate size += ehdr.e_phnum * ehdr.e_phentsize; 17090Sstevel@tonic-gate 17100Sstevel@tonic-gate /* unused shdr, and .shstrtab section */ 17110Sstevel@tonic-gate size += sizeof (Elf64_Shdr); 17120Sstevel@tonic-gate size += sizeof (Elf64_Shdr); 17130Sstevel@tonic-gate size += roundup(sizeof (shstr), 8); 17140Sstevel@tonic-gate 17150Sstevel@tonic-gate /* .dynsym section */ 17160Sstevel@tonic-gate size += sizeof (Elf64_Shdr); 17170Sstevel@tonic-gate if (Pread(P, &nchain, sizeof (nchain), 17180Sstevel@tonic-gate d[DI_HASH]->d_un.d_ptr + 4) != sizeof (nchain)) 17190Sstevel@tonic-gate goto bad64; 17200Sstevel@tonic-gate size += sizeof (Elf64_Sym) * nchain; 17210Sstevel@tonic-gate 17220Sstevel@tonic-gate /* .dynstr section */ 17230Sstevel@tonic-gate size += sizeof (Elf64_Shdr); 17240Sstevel@tonic-gate size += roundup(d[DI_STRSZ]->d_un.d_val, 8); 17250Sstevel@tonic-gate 17260Sstevel@tonic-gate /* .dynamic section */ 17270Sstevel@tonic-gate size += sizeof (Elf64_Shdr); 17280Sstevel@tonic-gate size += roundup(phdr.p_filesz, 8); 17290Sstevel@tonic-gate 17300Sstevel@tonic-gate /* .plt section */ 17310Sstevel@tonic-gate if (d[DI_PLTGOT] != NULL && d[DI_JMPREL] != NULL && 17320Sstevel@tonic-gate d[DI_PLTRELSZ] != NULL && d[DI_PLTREL] != NULL) { 17330Sstevel@tonic-gate uintptr_t penult, ult; 17340Sstevel@tonic-gate uintptr_t jmprel = d[DI_JMPREL]->d_un.d_ptr; 17350Sstevel@tonic-gate size_t pltrelsz = d[DI_PLTRELSZ]->d_un.d_val; 17360Sstevel@tonic-gate 17370Sstevel@tonic-gate if (d[DI_PLTREL]->d_un.d_val == DT_RELA) { 17380Sstevel@tonic-gate uint_t ndx = pltrelsz / sizeof (Elf64_Rela) - 2; 17390Sstevel@tonic-gate Elf64_Rela r[2]; 17400Sstevel@tonic-gate 17410Sstevel@tonic-gate if (Pread(P, r, sizeof (r), jmprel + 17420Sstevel@tonic-gate sizeof (r[0]) * ndx) != sizeof (r)) 17430Sstevel@tonic-gate goto bad64; 17440Sstevel@tonic-gate 17450Sstevel@tonic-gate penult = r[0].r_offset; 17460Sstevel@tonic-gate ult = r[1].r_offset; 17470Sstevel@tonic-gate 17480Sstevel@tonic-gate } else if (d[DI_PLTREL]->d_un.d_val == DT_REL) { 17490Sstevel@tonic-gate uint_t ndx = pltrelsz / sizeof (Elf64_Rel) - 2; 17500Sstevel@tonic-gate Elf64_Rel r[2]; 17510Sstevel@tonic-gate 17520Sstevel@tonic-gate if (Pread(P, r, sizeof (r), jmprel + 17530Sstevel@tonic-gate sizeof (r[0]) * ndx) != sizeof (r)) 17540Sstevel@tonic-gate goto bad64; 17550Sstevel@tonic-gate 17560Sstevel@tonic-gate penult = r[0].r_offset; 17570Sstevel@tonic-gate ult = r[1].r_offset; 17580Sstevel@tonic-gate } else { 17590Sstevel@tonic-gate goto bad64; 17600Sstevel@tonic-gate } 17610Sstevel@tonic-gate 17620Sstevel@tonic-gate pltentsz = ult - penult; 17630Sstevel@tonic-gate 17640Sstevel@tonic-gate if (ehdr.e_type == ET_DYN) 17650Sstevel@tonic-gate ult += addr; 17660Sstevel@tonic-gate 17670Sstevel@tonic-gate pltsz = ult - d[DI_PLTGOT]->d_un.d_ptr + pltentsz; 17680Sstevel@tonic-gate 17690Sstevel@tonic-gate size += sizeof (Elf64_Shdr); 17700Sstevel@tonic-gate size += roundup(pltsz, 8); 17710Sstevel@tonic-gate } 17720Sstevel@tonic-gate 17730Sstevel@tonic-gate if ((elfdata = calloc(1, size)) == NULL) 17740Sstevel@tonic-gate goto bad64; 17750Sstevel@tonic-gate 17760Sstevel@tonic-gate /* LINTED - alignment */ 17770Sstevel@tonic-gate ep = (Elf64_Ehdr *)elfdata; 17780Sstevel@tonic-gate (void) memcpy(ep, &ehdr, offsetof(Elf64_Ehdr, e_phoff)); 17790Sstevel@tonic-gate 17800Sstevel@tonic-gate ep->e_ehsize = sizeof (Elf64_Ehdr); 17810Sstevel@tonic-gate ep->e_phoff = sizeof (Elf64_Ehdr); 17820Sstevel@tonic-gate ep->e_phentsize = ehdr.e_phentsize; 17830Sstevel@tonic-gate ep->e_phnum = ehdr.e_phnum; 17840Sstevel@tonic-gate ep->e_shoff = ep->e_phoff + ep->e_phnum * ep->e_phentsize; 17850Sstevel@tonic-gate ep->e_shentsize = sizeof (Elf64_Shdr); 17860Sstevel@tonic-gate ep->e_shnum = (pltsz == 0) ? 5 : 6; 17870Sstevel@tonic-gate ep->e_shstrndx = 1; 17880Sstevel@tonic-gate 17890Sstevel@tonic-gate /* LINTED - alignment */ 17900Sstevel@tonic-gate sp = (Elf64_Shdr *)(elfdata + ep->e_shoff); 17910Sstevel@tonic-gate off = ep->e_shoff + ep->e_shentsize * ep->e_shnum; 17920Sstevel@tonic-gate 17930Sstevel@tonic-gate /* 17940Sstevel@tonic-gate * Copying the program headers directly from the process's 17950Sstevel@tonic-gate * address space is a little suspect, but since we only 17960Sstevel@tonic-gate * use them for their address and size values, this is fine. 17970Sstevel@tonic-gate */ 17980Sstevel@tonic-gate if (Pread(P, &elfdata[ep->e_phoff], 17990Sstevel@tonic-gate ep->e_phnum * ep->e_phentsize, addr + ehdr.e_phoff) != 18000Sstevel@tonic-gate ep->e_phnum * ep->e_phentsize) { 18010Sstevel@tonic-gate free(elfdata); 18020Sstevel@tonic-gate goto bad64; 18030Sstevel@tonic-gate } 18040Sstevel@tonic-gate 18050Sstevel@tonic-gate /* 18060Sstevel@tonic-gate * The first elf section is always skipped. 18070Sstevel@tonic-gate */ 18080Sstevel@tonic-gate sp++; 18090Sstevel@tonic-gate 18100Sstevel@tonic-gate /* 18110Sstevel@tonic-gate * Section Header[1] sh_name: .shstrtab 18120Sstevel@tonic-gate */ 18130Sstevel@tonic-gate sp->sh_name = 0; 18140Sstevel@tonic-gate sp->sh_type = SHT_STRTAB; 18150Sstevel@tonic-gate sp->sh_flags = SHF_STRINGS; 18160Sstevel@tonic-gate sp->sh_addr = 0; 18170Sstevel@tonic-gate sp->sh_offset = off; 18180Sstevel@tonic-gate sp->sh_size = sizeof (shstr); 18190Sstevel@tonic-gate sp->sh_link = 0; 18200Sstevel@tonic-gate sp->sh_info = 0; 18210Sstevel@tonic-gate sp->sh_addralign = 1; 18220Sstevel@tonic-gate sp->sh_entsize = 0; 18230Sstevel@tonic-gate 18240Sstevel@tonic-gate (void) memcpy(&elfdata[off], shstr, sizeof (shstr)); 18250Sstevel@tonic-gate off += roundup(sp->sh_size, 8); 18260Sstevel@tonic-gate sp++; 18270Sstevel@tonic-gate 18280Sstevel@tonic-gate /* 18290Sstevel@tonic-gate * Section Header[2] sh_name: .dynsym 18300Sstevel@tonic-gate */ 18310Sstevel@tonic-gate sp->sh_name = 10; 18320Sstevel@tonic-gate sp->sh_type = SHT_DYNSYM; 18330Sstevel@tonic-gate sp->sh_flags = SHF_ALLOC; 18340Sstevel@tonic-gate sp->sh_addr = d[DI_SYMTAB]->d_un.d_ptr; 18350Sstevel@tonic-gate if (ehdr.e_type == ET_DYN) 18360Sstevel@tonic-gate sp->sh_addr -= addr; 18370Sstevel@tonic-gate sp->sh_offset = off; 18380Sstevel@tonic-gate sp->sh_size = nchain * sizeof (Elf64_Sym); 18390Sstevel@tonic-gate sp->sh_link = 3; 18400Sstevel@tonic-gate sp->sh_info = 1; 18410Sstevel@tonic-gate sp->sh_addralign = 8; 18420Sstevel@tonic-gate sp->sh_entsize = sizeof (Elf64_Sym); 18430Sstevel@tonic-gate 18440Sstevel@tonic-gate if (Pread(P, &elfdata[off], sp->sh_size, 18450Sstevel@tonic-gate d[DI_SYMTAB]->d_un.d_ptr) != sp->sh_size) { 18460Sstevel@tonic-gate free(elfdata); 18470Sstevel@tonic-gate goto bad64; 18480Sstevel@tonic-gate } 18490Sstevel@tonic-gate 18500Sstevel@tonic-gate off += roundup(sp->sh_size, 8); 18510Sstevel@tonic-gate sp++; 18520Sstevel@tonic-gate 18530Sstevel@tonic-gate /* 18540Sstevel@tonic-gate * Section Header[3] sh_name: .dynstr 18550Sstevel@tonic-gate */ 18560Sstevel@tonic-gate sp->sh_name = 18; 18570Sstevel@tonic-gate sp->sh_type = SHT_STRTAB; 18580Sstevel@tonic-gate sp->sh_flags = SHF_ALLOC | SHF_STRINGS; 18590Sstevel@tonic-gate sp->sh_addr = d[DI_STRTAB]->d_un.d_ptr; 18600Sstevel@tonic-gate if (ehdr.e_type == ET_DYN) 18610Sstevel@tonic-gate sp->sh_addr -= addr; 18620Sstevel@tonic-gate sp->sh_offset = off; 18630Sstevel@tonic-gate sp->sh_size = d[DI_STRSZ]->d_un.d_val; 18640Sstevel@tonic-gate sp->sh_link = 0; 18650Sstevel@tonic-gate sp->sh_info = 0; 18660Sstevel@tonic-gate sp->sh_addralign = 1; 18670Sstevel@tonic-gate sp->sh_entsize = 0; 18680Sstevel@tonic-gate 18690Sstevel@tonic-gate if (Pread(P, &elfdata[off], sp->sh_size, 18700Sstevel@tonic-gate d[DI_STRTAB]->d_un.d_ptr) != sp->sh_size) { 18710Sstevel@tonic-gate free(elfdata); 18720Sstevel@tonic-gate goto bad64; 18730Sstevel@tonic-gate } 18740Sstevel@tonic-gate off += roundup(sp->sh_size, 8); 18750Sstevel@tonic-gate sp++; 18760Sstevel@tonic-gate 18770Sstevel@tonic-gate /* 18780Sstevel@tonic-gate * Section Header[4] sh_name: .dynamic 18790Sstevel@tonic-gate */ 18800Sstevel@tonic-gate sp->sh_name = 26; 18810Sstevel@tonic-gate sp->sh_type = SHT_DYNAMIC; 18820Sstevel@tonic-gate sp->sh_flags = SHF_WRITE | SHF_ALLOC; 18830Sstevel@tonic-gate sp->sh_addr = phdr.p_vaddr; 18840Sstevel@tonic-gate if (ehdr.e_type == ET_DYN) 18850Sstevel@tonic-gate sp->sh_addr -= addr; 18860Sstevel@tonic-gate sp->sh_offset = off; 18870Sstevel@tonic-gate sp->sh_size = phdr.p_filesz; 18880Sstevel@tonic-gate sp->sh_link = 3; 18890Sstevel@tonic-gate sp->sh_info = 0; 18900Sstevel@tonic-gate sp->sh_addralign = 8; 18910Sstevel@tonic-gate sp->sh_entsize = sizeof (Elf64_Dyn); 18920Sstevel@tonic-gate 18930Sstevel@tonic-gate (void) memcpy(&elfdata[off], dp, sp->sh_size); 18940Sstevel@tonic-gate off += roundup(sp->sh_size, 8); 18950Sstevel@tonic-gate sp++; 18960Sstevel@tonic-gate 18970Sstevel@tonic-gate /* 18980Sstevel@tonic-gate * Section Header[5] sh_name: .plt 18990Sstevel@tonic-gate */ 19000Sstevel@tonic-gate if (pltsz != 0) { 19010Sstevel@tonic-gate sp->sh_name = 35; 19020Sstevel@tonic-gate sp->sh_type = SHT_PROGBITS; 19030Sstevel@tonic-gate sp->sh_flags = SHF_WRITE | SHF_ALLOC | SHF_EXECINSTR; 19040Sstevel@tonic-gate sp->sh_addr = d[DI_PLTGOT]->d_un.d_ptr; 19050Sstevel@tonic-gate if (ehdr.e_type == ET_DYN) 19060Sstevel@tonic-gate sp->sh_addr -= addr; 19070Sstevel@tonic-gate sp->sh_offset = off; 19080Sstevel@tonic-gate sp->sh_size = pltsz; 19090Sstevel@tonic-gate sp->sh_link = 0; 19100Sstevel@tonic-gate sp->sh_info = 0; 19110Sstevel@tonic-gate sp->sh_addralign = 8; 19120Sstevel@tonic-gate sp->sh_entsize = pltentsz; 19130Sstevel@tonic-gate 19140Sstevel@tonic-gate if (Pread(P, &elfdata[off], sp->sh_size, 19150Sstevel@tonic-gate d[DI_PLTGOT]->d_un.d_ptr) != sp->sh_size) { 19160Sstevel@tonic-gate free(elfdata); 19170Sstevel@tonic-gate goto bad64; 19180Sstevel@tonic-gate } 19190Sstevel@tonic-gate off += roundup(sp->sh_size, 8); 19200Sstevel@tonic-gate sp++; 19210Sstevel@tonic-gate } 19220Sstevel@tonic-gate 19230Sstevel@tonic-gate free(dp); 19240Sstevel@tonic-gate goto good; 19250Sstevel@tonic-gate 19260Sstevel@tonic-gate bad64: 19270Sstevel@tonic-gate free(dp); 19280Sstevel@tonic-gate return (NULL); 19290Sstevel@tonic-gate #endif /* _LP64 */ 19300Sstevel@tonic-gate } 19310Sstevel@tonic-gate good: 19320Sstevel@tonic-gate if ((elf = elf_memory(elfdata, size)) == NULL) { 19330Sstevel@tonic-gate free(elfdata); 19340Sstevel@tonic-gate return (NULL); 19350Sstevel@tonic-gate } 19360Sstevel@tonic-gate 19370Sstevel@tonic-gate fptr->file_elfmem = elfdata; 19380Sstevel@tonic-gate 19390Sstevel@tonic-gate return (elf); 19400Sstevel@tonic-gate } 19410Sstevel@tonic-gate 19420Sstevel@tonic-gate /* 19430Sstevel@tonic-gate * We wouldn't need these if qsort(3C) took an argument for the callback... 19440Sstevel@tonic-gate */ 19450Sstevel@tonic-gate static mutex_t sort_mtx = DEFAULTMUTEX; 19460Sstevel@tonic-gate static char *sort_strs; 19470Sstevel@tonic-gate static GElf_Sym *sort_syms; 19480Sstevel@tonic-gate 19490Sstevel@tonic-gate int 19500Sstevel@tonic-gate byaddr_cmp_common(GElf_Sym *a, char *aname, GElf_Sym *b, char *bname) 19510Sstevel@tonic-gate { 19520Sstevel@tonic-gate if (a->st_value < b->st_value) 19530Sstevel@tonic-gate return (-1); 19540Sstevel@tonic-gate if (a->st_value > b->st_value) 19550Sstevel@tonic-gate return (1); 19560Sstevel@tonic-gate 19570Sstevel@tonic-gate /* 19580Sstevel@tonic-gate * Prefer the function to the non-function. 19590Sstevel@tonic-gate */ 19600Sstevel@tonic-gate if (GELF_ST_TYPE(a->st_info) != GELF_ST_TYPE(b->st_info)) { 19610Sstevel@tonic-gate if (GELF_ST_TYPE(a->st_info) == STT_FUNC) 19620Sstevel@tonic-gate return (-1); 19630Sstevel@tonic-gate if (GELF_ST_TYPE(b->st_info) == STT_FUNC) 19640Sstevel@tonic-gate return (1); 19650Sstevel@tonic-gate } 19660Sstevel@tonic-gate 19670Sstevel@tonic-gate /* 19680Sstevel@tonic-gate * Prefer the weak or strong global symbol to the local symbol. 19690Sstevel@tonic-gate */ 19700Sstevel@tonic-gate if (GELF_ST_BIND(a->st_info) != GELF_ST_BIND(b->st_info)) { 19710Sstevel@tonic-gate if (GELF_ST_BIND(b->st_info) == STB_LOCAL) 19720Sstevel@tonic-gate return (-1); 19730Sstevel@tonic-gate if (GELF_ST_BIND(a->st_info) == STB_LOCAL) 19740Sstevel@tonic-gate return (1); 19750Sstevel@tonic-gate } 19760Sstevel@tonic-gate 19770Sstevel@tonic-gate /* 19780Sstevel@tonic-gate * Prefer the name with fewer leading underscores in the name. 19790Sstevel@tonic-gate */ 19800Sstevel@tonic-gate while (*aname == '_' && *bname == '_') { 19810Sstevel@tonic-gate aname++; 19820Sstevel@tonic-gate bname++; 19830Sstevel@tonic-gate } 19840Sstevel@tonic-gate 19850Sstevel@tonic-gate if (*bname == '_') 19860Sstevel@tonic-gate return (-1); 19870Sstevel@tonic-gate if (*aname == '_') 19880Sstevel@tonic-gate return (1); 19890Sstevel@tonic-gate 19900Sstevel@tonic-gate /* 19910Sstevel@tonic-gate * Prefer the symbol with the smaller size. 19920Sstevel@tonic-gate */ 19930Sstevel@tonic-gate if (a->st_size < b->st_size) 19940Sstevel@tonic-gate return (-1); 19950Sstevel@tonic-gate if (a->st_size > b->st_size) 19960Sstevel@tonic-gate return (1); 19970Sstevel@tonic-gate 19980Sstevel@tonic-gate /* 19990Sstevel@tonic-gate * All other factors being equal, fall back to lexicographic order. 20000Sstevel@tonic-gate */ 20010Sstevel@tonic-gate return (strcmp(aname, bname)); 20020Sstevel@tonic-gate } 20030Sstevel@tonic-gate 20040Sstevel@tonic-gate static int 20050Sstevel@tonic-gate byaddr_cmp(const void *aa, const void *bb) 20060Sstevel@tonic-gate { 20070Sstevel@tonic-gate GElf_Sym *a = &sort_syms[*(uint_t *)aa]; 20080Sstevel@tonic-gate GElf_Sym *b = &sort_syms[*(uint_t *)bb]; 20090Sstevel@tonic-gate char *aname = sort_strs + a->st_name; 20100Sstevel@tonic-gate char *bname = sort_strs + b->st_name; 20110Sstevel@tonic-gate 20120Sstevel@tonic-gate return (byaddr_cmp_common(a, aname, b, bname)); 20130Sstevel@tonic-gate } 20140Sstevel@tonic-gate 20150Sstevel@tonic-gate static int 20160Sstevel@tonic-gate byname_cmp(const void *aa, const void *bb) 20170Sstevel@tonic-gate { 20180Sstevel@tonic-gate GElf_Sym *a = &sort_syms[*(uint_t *)aa]; 20190Sstevel@tonic-gate GElf_Sym *b = &sort_syms[*(uint_t *)bb]; 20200Sstevel@tonic-gate char *aname = sort_strs + a->st_name; 20210Sstevel@tonic-gate char *bname = sort_strs + b->st_name; 20220Sstevel@tonic-gate 20230Sstevel@tonic-gate return (strcmp(aname, bname)); 20240Sstevel@tonic-gate } 20250Sstevel@tonic-gate 20260Sstevel@tonic-gate void 20270Sstevel@tonic-gate optimize_symtab(sym_tbl_t *symtab) 20280Sstevel@tonic-gate { 20290Sstevel@tonic-gate GElf_Sym *symp, *syms; 20300Sstevel@tonic-gate uint_t i, *indexa, *indexb; 20310Sstevel@tonic-gate Elf_Data *data; 20320Sstevel@tonic-gate size_t symn, strsz, count; 20330Sstevel@tonic-gate 20340Sstevel@tonic-gate if (symtab == NULL || symtab->sym_data == NULL || 20350Sstevel@tonic-gate symtab->sym_byaddr != NULL) 20360Sstevel@tonic-gate return; 20370Sstevel@tonic-gate 20380Sstevel@tonic-gate data = symtab->sym_data; 20390Sstevel@tonic-gate symn = symtab->sym_symn; 20400Sstevel@tonic-gate strsz = symtab->sym_strsz; 20410Sstevel@tonic-gate 20420Sstevel@tonic-gate symp = syms = malloc(sizeof (GElf_Sym) * symn); 20430Sstevel@tonic-gate 20440Sstevel@tonic-gate /* 20450Sstevel@tonic-gate * First record all the symbols into a table and count up the ones 20460Sstevel@tonic-gate * that we're interested in. We mark symbols as invalid by setting 20470Sstevel@tonic-gate * the st_name to an illegal value. 20480Sstevel@tonic-gate */ 20490Sstevel@tonic-gate for (i = 0, count = 0; i < symn; i++, symp++) { 20500Sstevel@tonic-gate if (gelf_getsym(data, i, symp) != NULL && 20510Sstevel@tonic-gate symp->st_name < strsz && 20520Sstevel@tonic-gate IS_DATA_TYPE(GELF_ST_TYPE(symp->st_info))) 20530Sstevel@tonic-gate count++; 20540Sstevel@tonic-gate else 20550Sstevel@tonic-gate symp->st_name = strsz; 20560Sstevel@tonic-gate } 20570Sstevel@tonic-gate 20580Sstevel@tonic-gate /* 20590Sstevel@tonic-gate * Allocate sufficient space for both tables and populate them 20600Sstevel@tonic-gate * with the same symbols we just counted. 20610Sstevel@tonic-gate */ 20620Sstevel@tonic-gate symtab->sym_count = count; 20630Sstevel@tonic-gate indexa = symtab->sym_byaddr = calloc(sizeof (uint_t), count); 20640Sstevel@tonic-gate indexb = symtab->sym_byname = calloc(sizeof (uint_t), count); 20650Sstevel@tonic-gate 20660Sstevel@tonic-gate for (i = 0, symp = syms; i < symn; i++, symp++) { 20670Sstevel@tonic-gate if (symp->st_name < strsz) 20680Sstevel@tonic-gate *indexa++ = *indexb++ = i; 20690Sstevel@tonic-gate } 20700Sstevel@tonic-gate 20710Sstevel@tonic-gate /* 20720Sstevel@tonic-gate * Sort the two tables according to the appropriate criteria. 20730Sstevel@tonic-gate */ 20740Sstevel@tonic-gate (void) mutex_lock(&sort_mtx); 20750Sstevel@tonic-gate sort_strs = symtab->sym_strs; 20760Sstevel@tonic-gate sort_syms = syms; 20770Sstevel@tonic-gate 20780Sstevel@tonic-gate qsort(symtab->sym_byaddr, count, sizeof (uint_t), byaddr_cmp); 20790Sstevel@tonic-gate qsort(symtab->sym_byname, count, sizeof (uint_t), byname_cmp); 20800Sstevel@tonic-gate 20810Sstevel@tonic-gate sort_strs = NULL; 20820Sstevel@tonic-gate sort_syms = NULL; 20830Sstevel@tonic-gate (void) mutex_unlock(&sort_mtx); 20840Sstevel@tonic-gate 20850Sstevel@tonic-gate free(syms); 20860Sstevel@tonic-gate } 20870Sstevel@tonic-gate 20880Sstevel@tonic-gate /* 20890Sstevel@tonic-gate * Build the symbol table for the given mapped file. 20900Sstevel@tonic-gate */ 20910Sstevel@tonic-gate void 20920Sstevel@tonic-gate Pbuild_file_symtab(struct ps_prochandle *P, file_info_t *fptr) 20930Sstevel@tonic-gate { 20940Sstevel@tonic-gate char objectfile[PATH_MAX]; 20950Sstevel@tonic-gate uint_t i; 20960Sstevel@tonic-gate 20970Sstevel@tonic-gate GElf_Ehdr ehdr; 20980Sstevel@tonic-gate GElf_Sym s; 20990Sstevel@tonic-gate 21000Sstevel@tonic-gate Elf_Data *shdata; 21010Sstevel@tonic-gate Elf_Scn *scn; 21020Sstevel@tonic-gate Elf *elf; 21030Sstevel@tonic-gate 21040Sstevel@tonic-gate struct { 21050Sstevel@tonic-gate GElf_Shdr c_shdr; 21060Sstevel@tonic-gate Elf_Data *c_data; 21070Sstevel@tonic-gate const char *c_name; 21080Sstevel@tonic-gate } *cp, *cache = NULL, *dyn = NULL, *plt = NULL, *ctf = NULL; 21090Sstevel@tonic-gate 21100Sstevel@tonic-gate if (fptr->file_init) 21110Sstevel@tonic-gate return; /* We've already processed this file */ 21120Sstevel@tonic-gate 21130Sstevel@tonic-gate /* 21140Sstevel@tonic-gate * Mark the file_info struct as having the symbol table initialized 21150Sstevel@tonic-gate * even if we fail below. We tried once; we don't try again. 21160Sstevel@tonic-gate */ 21170Sstevel@tonic-gate fptr->file_init = 1; 21180Sstevel@tonic-gate 21190Sstevel@tonic-gate if (elf_version(EV_CURRENT) == EV_NONE) { 21200Sstevel@tonic-gate dprintf("libproc ELF version is more recent than libelf\n"); 21210Sstevel@tonic-gate return; 21220Sstevel@tonic-gate } 21230Sstevel@tonic-gate 21240Sstevel@tonic-gate if (P->state == PS_DEAD || P->state == PS_IDLE) { 21250Sstevel@tonic-gate /* 21260Sstevel@tonic-gate * If we're a not live, we can't open files from the /proc 21270Sstevel@tonic-gate * object directory; we have only the mapping and file names 21280Sstevel@tonic-gate * to guide us. We prefer the file_lname, but need to handle 21290Sstevel@tonic-gate * the case of it being NULL in order to bootstrap: we first 21300Sstevel@tonic-gate * come here during rd_new() when the only information we have 21310Sstevel@tonic-gate * is interpreter name associated with the AT_BASE mapping. 21320Sstevel@tonic-gate */ 21330Sstevel@tonic-gate (void) snprintf(objectfile, sizeof (objectfile), "%s", 21340Sstevel@tonic-gate fptr->file_lname ? fptr->file_lname : fptr->file_pname); 21350Sstevel@tonic-gate } else { 21360Sstevel@tonic-gate (void) snprintf(objectfile, sizeof (objectfile), 21370Sstevel@tonic-gate "/proc/%d/object/%s", (int)P->pid, fptr->file_pname); 21380Sstevel@tonic-gate } 21390Sstevel@tonic-gate 21400Sstevel@tonic-gate /* 21410Sstevel@tonic-gate * Open the object file, create the elf file, and then get the elf 21420Sstevel@tonic-gate * header and .shstrtab data buffer so we can process sections by 21430Sstevel@tonic-gate * name. If anything goes wrong try to fake up an elf file from 21440Sstevel@tonic-gate * the in-core elf image. 21450Sstevel@tonic-gate */ 21460Sstevel@tonic-gate if ((fptr->file_fd = open(objectfile, O_RDONLY)) < 0) { 21470Sstevel@tonic-gate dprintf("Pbuild_file_symtab: failed to open %s: %s\n", 21480Sstevel@tonic-gate objectfile, strerror(errno)); 21490Sstevel@tonic-gate 21500Sstevel@tonic-gate if ((elf = fake_elf(P, fptr)) == NULL || 21510Sstevel@tonic-gate elf_kind(elf) != ELF_K_ELF || 21520Sstevel@tonic-gate gelf_getehdr(elf, &ehdr) == NULL || 21530Sstevel@tonic-gate (scn = elf_getscn(elf, ehdr.e_shstrndx)) == NULL || 21540Sstevel@tonic-gate (shdata = elf_getdata(scn, NULL)) == NULL) { 21550Sstevel@tonic-gate dprintf("failed to fake up ELF file\n"); 21560Sstevel@tonic-gate return; 21570Sstevel@tonic-gate } 21580Sstevel@tonic-gate 21590Sstevel@tonic-gate } else if ((elf = elf_begin(fptr->file_fd, ELF_C_READ, NULL)) == NULL || 21600Sstevel@tonic-gate elf_kind(elf) != ELF_K_ELF || 21610Sstevel@tonic-gate gelf_getehdr(elf, &ehdr) == NULL || 21620Sstevel@tonic-gate (scn = elf_getscn(elf, ehdr.e_shstrndx)) == NULL || 21630Sstevel@tonic-gate (shdata = elf_getdata(scn, NULL)) == NULL) { 21640Sstevel@tonic-gate dprintf("failed to process ELF file %s: %s\n", 21650Sstevel@tonic-gate objectfile, elf_errmsg(elf_errno())); 21660Sstevel@tonic-gate 21670Sstevel@tonic-gate if ((elf = fake_elf(P, fptr)) == NULL || 21680Sstevel@tonic-gate elf_kind(elf) != ELF_K_ELF || 21690Sstevel@tonic-gate gelf_getehdr(elf, &ehdr) == NULL || 21700Sstevel@tonic-gate (scn = elf_getscn(elf, ehdr.e_shstrndx)) == NULL || 21710Sstevel@tonic-gate (shdata = elf_getdata(scn, NULL)) == NULL) { 21720Sstevel@tonic-gate dprintf("failed to fake up ELF file\n"); 21730Sstevel@tonic-gate goto bad; 21740Sstevel@tonic-gate } 21750Sstevel@tonic-gate 21760Sstevel@tonic-gate } else if (file_differs(P, elf, fptr)) { 21770Sstevel@tonic-gate Elf *newelf; 21780Sstevel@tonic-gate 21790Sstevel@tonic-gate /* 21800Sstevel@tonic-gate * Before we get too excited about this elf file, we'll check 21810Sstevel@tonic-gate * its checksum value against the value we have in memory. If 21820Sstevel@tonic-gate * they don't agree, we try to fake up a new elf file and 21830Sstevel@tonic-gate * proceed with that instead. 21840Sstevel@tonic-gate */ 21850Sstevel@tonic-gate 21860Sstevel@tonic-gate dprintf("ELF file %s (%lx) doesn't match in-core image\n", 21870Sstevel@tonic-gate fptr->file_pname, 21880Sstevel@tonic-gate (ulong_t)fptr->file_map->map_pmap.pr_vaddr); 21890Sstevel@tonic-gate 21900Sstevel@tonic-gate if ((newelf = fake_elf(P, fptr)) == NULL || 21910Sstevel@tonic-gate elf_kind(newelf) != ELF_K_ELF || 21920Sstevel@tonic-gate gelf_getehdr(newelf, &ehdr) == NULL || 21930Sstevel@tonic-gate (scn = elf_getscn(newelf, ehdr.e_shstrndx)) == NULL || 21940Sstevel@tonic-gate (shdata = elf_getdata(scn, NULL)) == NULL) { 21950Sstevel@tonic-gate dprintf("failed to fake up ELF file\n"); 21960Sstevel@tonic-gate } else { 21970Sstevel@tonic-gate (void) elf_end(elf); 21980Sstevel@tonic-gate elf = newelf; 21990Sstevel@tonic-gate 22000Sstevel@tonic-gate dprintf("switched to faked up ELF file\n"); 22010Sstevel@tonic-gate } 22020Sstevel@tonic-gate } 22030Sstevel@tonic-gate 22040Sstevel@tonic-gate if ((cache = malloc(ehdr.e_shnum * sizeof (*cache))) == NULL) { 22050Sstevel@tonic-gate dprintf("failed to malloc section cache for %s\n", objectfile); 22060Sstevel@tonic-gate goto bad; 22070Sstevel@tonic-gate } 22080Sstevel@tonic-gate 22090Sstevel@tonic-gate dprintf("processing ELF file %s\n", objectfile); 22100Sstevel@tonic-gate fptr->file_class = ehdr.e_ident[EI_CLASS]; 22110Sstevel@tonic-gate fptr->file_etype = ehdr.e_type; 22120Sstevel@tonic-gate fptr->file_elf = elf; 22130Sstevel@tonic-gate 22140Sstevel@tonic-gate /* 22150Sstevel@tonic-gate * Iterate through each section, caching its section header, data 22160Sstevel@tonic-gate * pointer, and name. We use this for handling sh_link values below. 22170Sstevel@tonic-gate */ 22180Sstevel@tonic-gate for (cp = cache + 1, scn = NULL; scn = elf_nextscn(elf, scn); cp++) { 22190Sstevel@tonic-gate if (gelf_getshdr(scn, &cp->c_shdr) == NULL) 22200Sstevel@tonic-gate goto bad; /* Failed to get section header */ 22210Sstevel@tonic-gate 22220Sstevel@tonic-gate if ((cp->c_data = elf_getdata(scn, NULL)) == NULL) 22230Sstevel@tonic-gate goto bad; /* Failed to get section data */ 22240Sstevel@tonic-gate 22250Sstevel@tonic-gate if (cp->c_shdr.sh_name >= shdata->d_size) 22260Sstevel@tonic-gate goto bad; /* Corrupt section name */ 22270Sstevel@tonic-gate 22280Sstevel@tonic-gate cp->c_name = (const char *)shdata->d_buf + cp->c_shdr.sh_name; 22290Sstevel@tonic-gate } 22300Sstevel@tonic-gate 22310Sstevel@tonic-gate /* 22320Sstevel@tonic-gate * Now iterate through the section cache in order to locate info 22330Sstevel@tonic-gate * for the .symtab, .dynsym, .dynamic, .plt, and .SUNW_ctf sections: 22340Sstevel@tonic-gate */ 22350Sstevel@tonic-gate for (i = 1, cp = cache + 1; i < ehdr.e_shnum; i++, cp++) { 22360Sstevel@tonic-gate GElf_Shdr *shp = &cp->c_shdr; 22370Sstevel@tonic-gate 22380Sstevel@tonic-gate if (shp->sh_type == SHT_SYMTAB || shp->sh_type == SHT_DYNSYM) { 22390Sstevel@tonic-gate sym_tbl_t *symp = shp->sh_type == SHT_SYMTAB ? 22400Sstevel@tonic-gate &fptr->file_symtab : &fptr->file_dynsym; 22410Sstevel@tonic-gate 22420Sstevel@tonic-gate /* 22430Sstevel@tonic-gate * It's possible that the we already got the symbol 22440Sstevel@tonic-gate * table from the core file itself. Either the file 22450Sstevel@tonic-gate * differs in which case our faked up elf file will 22460Sstevel@tonic-gate * only contain the dynsym (not the symtab) or the 22470Sstevel@tonic-gate * file matches in which case we'll just be replacing 22480Sstevel@tonic-gate * the symbol table we pulled out of the core file 22490Sstevel@tonic-gate * with an equivalent one. In either case, this 22500Sstevel@tonic-gate * check isn't essential, but it's a good idea. 22510Sstevel@tonic-gate */ 22520Sstevel@tonic-gate if (symp->sym_data == NULL) { 22530Sstevel@tonic-gate symp->sym_data = cp->c_data; 22540Sstevel@tonic-gate symp->sym_symn = shp->sh_size / shp->sh_entsize; 22550Sstevel@tonic-gate symp->sym_strs = 22560Sstevel@tonic-gate cache[shp->sh_link].c_data->d_buf; 22570Sstevel@tonic-gate symp->sym_strsz = 22580Sstevel@tonic-gate cache[shp->sh_link].c_data->d_size; 22590Sstevel@tonic-gate symp->sym_hdr = cp->c_shdr; 22600Sstevel@tonic-gate symp->sym_strhdr = cache[shp->sh_link].c_shdr; 22610Sstevel@tonic-gate } 22620Sstevel@tonic-gate 22630Sstevel@tonic-gate } else if (shp->sh_type == SHT_DYNAMIC) { 22640Sstevel@tonic-gate dyn = cp; 22650Sstevel@tonic-gate 22660Sstevel@tonic-gate } else if (strcmp(cp->c_name, ".plt") == 0) { 22670Sstevel@tonic-gate plt = cp; 22680Sstevel@tonic-gate 22690Sstevel@tonic-gate } else if (strcmp(cp->c_name, ".SUNW_ctf") == 0) { 22700Sstevel@tonic-gate /* 22710Sstevel@tonic-gate * Skip over bogus CTF sections so they don't come back 22720Sstevel@tonic-gate * to haunt us later. 22730Sstevel@tonic-gate */ 22740Sstevel@tonic-gate if (shp->sh_link == 0 || 22750Sstevel@tonic-gate shp->sh_link > ehdr.e_shnum || 22760Sstevel@tonic-gate (cache[shp->sh_link].c_shdr.sh_type != SHT_DYNSYM && 22770Sstevel@tonic-gate cache[shp->sh_link].c_shdr.sh_type != SHT_SYMTAB)) { 22780Sstevel@tonic-gate dprintf("Bad sh_link %d for " 22790Sstevel@tonic-gate "CTF\n", shp->sh_link); 22800Sstevel@tonic-gate continue; 22810Sstevel@tonic-gate } 22820Sstevel@tonic-gate ctf = cp; 22830Sstevel@tonic-gate } 22840Sstevel@tonic-gate } 22850Sstevel@tonic-gate 22860Sstevel@tonic-gate /* 22870Sstevel@tonic-gate * At this point, we've found all the symbol tables we're ever going 22880Sstevel@tonic-gate * to find: the ones in the loop above and possibly the symtab that 22890Sstevel@tonic-gate * was included in the core file. Before we perform any lookups, we 22900Sstevel@tonic-gate * create sorted versions to optimize for lookups. 22910Sstevel@tonic-gate */ 22920Sstevel@tonic-gate optimize_symtab(&fptr->file_symtab); 22930Sstevel@tonic-gate optimize_symtab(&fptr->file_dynsym); 22940Sstevel@tonic-gate 22950Sstevel@tonic-gate /* 22960Sstevel@tonic-gate * Fill in the base address of the text mapping for shared libraries. 22970Sstevel@tonic-gate * This allows us to translate symbols before librtld_db is ready. 22980Sstevel@tonic-gate */ 22990Sstevel@tonic-gate if (fptr->file_etype == ET_DYN) { 23000Sstevel@tonic-gate fptr->file_dyn_base = fptr->file_map->map_pmap.pr_vaddr - 23010Sstevel@tonic-gate fptr->file_map->map_pmap.pr_offset; 23020Sstevel@tonic-gate dprintf("setting file_dyn_base for %s to %p\n", 23030Sstevel@tonic-gate objectfile, (void *)fptr->file_dyn_base); 23040Sstevel@tonic-gate } 23050Sstevel@tonic-gate 23060Sstevel@tonic-gate /* 23070Sstevel@tonic-gate * Record the CTF section information in the file info structure. 23080Sstevel@tonic-gate */ 23090Sstevel@tonic-gate if (ctf != NULL) { 23100Sstevel@tonic-gate fptr->file_ctf_off = ctf->c_shdr.sh_offset; 23110Sstevel@tonic-gate fptr->file_ctf_size = ctf->c_shdr.sh_size; 23120Sstevel@tonic-gate if (ctf->c_shdr.sh_link != 0 && 23130Sstevel@tonic-gate cache[ctf->c_shdr.sh_link].c_shdr.sh_type == SHT_DYNSYM) 23140Sstevel@tonic-gate fptr->file_ctf_dyn = 1; 23150Sstevel@tonic-gate } 23160Sstevel@tonic-gate 23170Sstevel@tonic-gate if (fptr->file_lo == NULL) 23180Sstevel@tonic-gate goto done; /* Nothing else to do if no load object info */ 23190Sstevel@tonic-gate 23200Sstevel@tonic-gate /* 23210Sstevel@tonic-gate * If the object is a shared library and we have a different rl_base 23220Sstevel@tonic-gate * value, reset file_dyn_base according to librtld_db's information. 23230Sstevel@tonic-gate */ 23240Sstevel@tonic-gate if (fptr->file_etype == ET_DYN && 23250Sstevel@tonic-gate fptr->file_lo->rl_base != fptr->file_dyn_base) { 23260Sstevel@tonic-gate dprintf("resetting file_dyn_base for %s to %p\n", 23270Sstevel@tonic-gate objectfile, (void *)fptr->file_lo->rl_base); 23280Sstevel@tonic-gate fptr->file_dyn_base = fptr->file_lo->rl_base; 23290Sstevel@tonic-gate } 23300Sstevel@tonic-gate 23310Sstevel@tonic-gate /* 23320Sstevel@tonic-gate * Fill in the PLT information for this file if a PLT symbol is found. 23330Sstevel@tonic-gate */ 23340Sstevel@tonic-gate if (sym_by_name(&fptr->file_dynsym, "_PROCEDURE_LINKAGE_TABLE_", &s, 23350Sstevel@tonic-gate NULL) != NULL) { 23360Sstevel@tonic-gate fptr->file_plt_base = s.st_value + fptr->file_dyn_base; 23370Sstevel@tonic-gate fptr->file_plt_size = (plt != NULL) ? plt->c_shdr.sh_size : 0; 23380Sstevel@tonic-gate 23390Sstevel@tonic-gate /* 23400Sstevel@tonic-gate * Bring the load object up to date; it is the only way the 23410Sstevel@tonic-gate * user has to access the PLT data. The PLT information in the 23420Sstevel@tonic-gate * rd_loadobj_t is not set in the call to map_iter() (the 23430Sstevel@tonic-gate * callback for rd_loadobj_iter) where we set file_lo. 23440Sstevel@tonic-gate */ 23450Sstevel@tonic-gate fptr->file_lo->rl_plt_base = fptr->file_plt_base; 23460Sstevel@tonic-gate fptr->file_lo->rl_plt_size = fptr->file_plt_size; 23470Sstevel@tonic-gate 23480Sstevel@tonic-gate dprintf("PLT found at %p, size = %lu\n", 23490Sstevel@tonic-gate (void *)fptr->file_plt_base, (ulong_t)fptr->file_plt_size); 23500Sstevel@tonic-gate } 23510Sstevel@tonic-gate 23520Sstevel@tonic-gate /* 23530Sstevel@tonic-gate * Fill in the PLT information. 23540Sstevel@tonic-gate */ 23550Sstevel@tonic-gate if (dyn != NULL) { 23560Sstevel@tonic-gate uintptr_t dynaddr = dyn->c_shdr.sh_addr + fptr->file_dyn_base; 23570Sstevel@tonic-gate size_t ndyn = dyn->c_shdr.sh_size / dyn->c_shdr.sh_entsize; 23580Sstevel@tonic-gate GElf_Dyn d; 23590Sstevel@tonic-gate 23600Sstevel@tonic-gate for (i = 0; i < ndyn; i++) { 23610Sstevel@tonic-gate if (gelf_getdyn(dyn->c_data, i, &d) != NULL && 23620Sstevel@tonic-gate d.d_tag == DT_JMPREL) { 23630Sstevel@tonic-gate fptr->file_jmp_rel = 23640Sstevel@tonic-gate d.d_un.d_ptr + fptr->file_dyn_base; 23650Sstevel@tonic-gate break; 23660Sstevel@tonic-gate } 23670Sstevel@tonic-gate } 23680Sstevel@tonic-gate 23690Sstevel@tonic-gate dprintf("_DYNAMIC found at %p, %lu entries, DT_JMPREL = %p\n", 23700Sstevel@tonic-gate (void *)dynaddr, (ulong_t)ndyn, (void *)fptr->file_jmp_rel); 23710Sstevel@tonic-gate } 23720Sstevel@tonic-gate 23730Sstevel@tonic-gate done: 23740Sstevel@tonic-gate free(cache); 23750Sstevel@tonic-gate return; 23760Sstevel@tonic-gate 23770Sstevel@tonic-gate bad: 23780Sstevel@tonic-gate if (cache != NULL) 23790Sstevel@tonic-gate free(cache); 23800Sstevel@tonic-gate 23810Sstevel@tonic-gate (void) elf_end(elf); 23820Sstevel@tonic-gate fptr->file_elf = NULL; 23830Sstevel@tonic-gate if (fptr->file_elfmem != NULL) { 23840Sstevel@tonic-gate free(fptr->file_elfmem); 23850Sstevel@tonic-gate fptr->file_elfmem = NULL; 23860Sstevel@tonic-gate } 23870Sstevel@tonic-gate (void) close(fptr->file_fd); 23880Sstevel@tonic-gate fptr->file_fd = -1; 23890Sstevel@tonic-gate } 23900Sstevel@tonic-gate 23910Sstevel@tonic-gate /* 23920Sstevel@tonic-gate * Given a process virtual address, return the map_info_t containing it. 23930Sstevel@tonic-gate * If none found, return NULL. 23940Sstevel@tonic-gate */ 23950Sstevel@tonic-gate map_info_t * 23960Sstevel@tonic-gate Paddr2mptr(struct ps_prochandle *P, uintptr_t addr) 23970Sstevel@tonic-gate { 23980Sstevel@tonic-gate int lo = 0; 23990Sstevel@tonic-gate int hi = P->map_count - 1; 24000Sstevel@tonic-gate int mid; 24010Sstevel@tonic-gate map_info_t *mp; 24020Sstevel@tonic-gate 24030Sstevel@tonic-gate while (lo <= hi) { 24040Sstevel@tonic-gate 24050Sstevel@tonic-gate mid = (lo + hi) / 2; 24060Sstevel@tonic-gate mp = &P->mappings[mid]; 24070Sstevel@tonic-gate 24080Sstevel@tonic-gate /* check that addr is in [vaddr, vaddr + size) */ 24090Sstevel@tonic-gate if ((addr - mp->map_pmap.pr_vaddr) < mp->map_pmap.pr_size) 24100Sstevel@tonic-gate return (mp); 24110Sstevel@tonic-gate 24120Sstevel@tonic-gate if (addr < mp->map_pmap.pr_vaddr) 24130Sstevel@tonic-gate hi = mid - 1; 24140Sstevel@tonic-gate else 24150Sstevel@tonic-gate lo = mid + 1; 24160Sstevel@tonic-gate } 24170Sstevel@tonic-gate 24180Sstevel@tonic-gate return (NULL); 24190Sstevel@tonic-gate } 24200Sstevel@tonic-gate 24210Sstevel@tonic-gate /* 24220Sstevel@tonic-gate * Return the map_info_t for the executable file. 24230Sstevel@tonic-gate * If not found, return NULL. 24240Sstevel@tonic-gate */ 24250Sstevel@tonic-gate static map_info_t * 24260Sstevel@tonic-gate exec_map(struct ps_prochandle *P) 24270Sstevel@tonic-gate { 24280Sstevel@tonic-gate uint_t i; 24290Sstevel@tonic-gate map_info_t *mptr; 24300Sstevel@tonic-gate map_info_t *mold = NULL; 24310Sstevel@tonic-gate file_info_t *fptr; 24320Sstevel@tonic-gate uintptr_t base; 24330Sstevel@tonic-gate 24340Sstevel@tonic-gate for (i = 0, mptr = P->mappings; i < P->map_count; i++, mptr++) { 24350Sstevel@tonic-gate if (mptr->map_pmap.pr_mapname[0] == '\0') 24360Sstevel@tonic-gate continue; 24370Sstevel@tonic-gate if (strcmp(mptr->map_pmap.pr_mapname, "a.out") == 0) { 24380Sstevel@tonic-gate if ((fptr = mptr->map_file) != NULL && 24390Sstevel@tonic-gate fptr->file_lo != NULL) { 24400Sstevel@tonic-gate base = fptr->file_lo->rl_base; 24410Sstevel@tonic-gate if (base >= mptr->map_pmap.pr_vaddr && 24420Sstevel@tonic-gate base < mptr->map_pmap.pr_vaddr + 24430Sstevel@tonic-gate mptr->map_pmap.pr_size) /* text space */ 24440Sstevel@tonic-gate return (mptr); 24450Sstevel@tonic-gate mold = mptr; /* must be the data */ 24460Sstevel@tonic-gate continue; 24470Sstevel@tonic-gate } 24480Sstevel@tonic-gate /* This is a poor way to test for text space */ 24490Sstevel@tonic-gate if (!(mptr->map_pmap.pr_mflags & MA_EXEC) || 24500Sstevel@tonic-gate (mptr->map_pmap.pr_mflags & MA_WRITE)) { 24510Sstevel@tonic-gate mold = mptr; 24520Sstevel@tonic-gate continue; 24530Sstevel@tonic-gate } 24540Sstevel@tonic-gate return (mptr); 24550Sstevel@tonic-gate } 24560Sstevel@tonic-gate } 24570Sstevel@tonic-gate 24580Sstevel@tonic-gate return (mold); 24590Sstevel@tonic-gate } 24600Sstevel@tonic-gate 24610Sstevel@tonic-gate /* 24620Sstevel@tonic-gate * Given a shared object name, return the map_info_t for it. If no matching 24630Sstevel@tonic-gate * object is found, return NULL. Normally, the link maps contain the full 24640Sstevel@tonic-gate * object pathname, e.g. /usr/lib/libc.so.1. We allow the object name to 24650Sstevel@tonic-gate * take one of the following forms: 24660Sstevel@tonic-gate * 24670Sstevel@tonic-gate * 1. An exact match (i.e. a full pathname): "/usr/lib/libc.so.1" 24680Sstevel@tonic-gate * 2. An exact basename match: "libc.so.1" 24690Sstevel@tonic-gate * 3. An initial basename match up to a '.' suffix: "libc.so" or "libc" 24700Sstevel@tonic-gate * 4. The literal string "a.out" is an alias for the executable mapping 24710Sstevel@tonic-gate * 24720Sstevel@tonic-gate * The third case is a convenience for callers and may not be necessary. 24730Sstevel@tonic-gate * 24740Sstevel@tonic-gate * As the exact same object name may be loaded on different link maps (see 24750Sstevel@tonic-gate * dlmopen(3DL)), we also allow the caller to resolve the object name by 24760Sstevel@tonic-gate * specifying a particular link map id. If lmid is PR_LMID_EVERY, the 24770Sstevel@tonic-gate * first matching name will be returned, regardless of the link map id. 24780Sstevel@tonic-gate */ 24790Sstevel@tonic-gate static map_info_t * 24800Sstevel@tonic-gate object_to_map(struct ps_prochandle *P, Lmid_t lmid, const char *objname) 24810Sstevel@tonic-gate { 24820Sstevel@tonic-gate map_info_t *mp; 24830Sstevel@tonic-gate file_info_t *fp; 24840Sstevel@tonic-gate size_t objlen; 24850Sstevel@tonic-gate uint_t i; 24860Sstevel@tonic-gate 24870Sstevel@tonic-gate /* 24880Sstevel@tonic-gate * First pass: look for exact matches of the entire pathname or 24890Sstevel@tonic-gate * basename (cases 1 and 2 above): 24900Sstevel@tonic-gate */ 24910Sstevel@tonic-gate for (i = 0, mp = P->mappings; i < P->map_count; i++, mp++) { 24920Sstevel@tonic-gate 24930Sstevel@tonic-gate if (mp->map_pmap.pr_mapname[0] == '\0' || 24940Sstevel@tonic-gate (fp = mp->map_file) == NULL || fp->file_lname == NULL) 24950Sstevel@tonic-gate continue; 24960Sstevel@tonic-gate 24970Sstevel@tonic-gate if (lmid != PR_LMID_EVERY && 24980Sstevel@tonic-gate (fp->file_lo == NULL || lmid != fp->file_lo->rl_lmident)) 24990Sstevel@tonic-gate continue; 25000Sstevel@tonic-gate 25010Sstevel@tonic-gate /* 25020Sstevel@tonic-gate * If we match, return the primary text mapping; otherwise 25030Sstevel@tonic-gate * just return the mapping we matched. 25040Sstevel@tonic-gate */ 25050Sstevel@tonic-gate if (strcmp(fp->file_lname, objname) == 0 || 25060Sstevel@tonic-gate strcmp(fp->file_lbase, objname) == 0) 25070Sstevel@tonic-gate return (fp->file_map ? fp->file_map : mp); 25080Sstevel@tonic-gate } 25090Sstevel@tonic-gate 25100Sstevel@tonic-gate objlen = strlen(objname); 25110Sstevel@tonic-gate 25120Sstevel@tonic-gate /* 25130Sstevel@tonic-gate * Second pass: look for partial matches (case 3 above): 25140Sstevel@tonic-gate */ 25150Sstevel@tonic-gate for (i = 0, mp = P->mappings; i < P->map_count; i++, mp++) { 25160Sstevel@tonic-gate 25170Sstevel@tonic-gate if (mp->map_pmap.pr_mapname[0] == '\0' || 25180Sstevel@tonic-gate (fp = mp->map_file) == NULL || fp->file_lname == NULL) 25190Sstevel@tonic-gate continue; 25200Sstevel@tonic-gate 25210Sstevel@tonic-gate if (lmid != PR_LMID_EVERY && 25220Sstevel@tonic-gate (fp->file_lo == NULL || lmid != fp->file_lo->rl_lmident)) 25230Sstevel@tonic-gate continue; 25240Sstevel@tonic-gate 25250Sstevel@tonic-gate /* 25260Sstevel@tonic-gate * If we match, return the primary text mapping; otherwise 25270Sstevel@tonic-gate * just return the mapping we matched. 25280Sstevel@tonic-gate */ 25290Sstevel@tonic-gate if (strncmp(fp->file_lbase, objname, objlen) == 0 && 25300Sstevel@tonic-gate fp->file_lbase[objlen] == '.') 25310Sstevel@tonic-gate return (fp->file_map ? fp->file_map : mp); 25320Sstevel@tonic-gate } 25330Sstevel@tonic-gate 25340Sstevel@tonic-gate /* 25350Sstevel@tonic-gate * One last check: we allow "a.out" to always alias the executable, 25360Sstevel@tonic-gate * assuming this name was not in use for something else. 25370Sstevel@tonic-gate */ 25380Sstevel@tonic-gate if ((lmid == PR_LMID_EVERY || lmid == LM_ID_BASE) && 25390Sstevel@tonic-gate (strcmp(objname, "a.out") == 0)) 25400Sstevel@tonic-gate return (P->map_exec); 25410Sstevel@tonic-gate 25420Sstevel@tonic-gate return (NULL); 25430Sstevel@tonic-gate } 25440Sstevel@tonic-gate 25450Sstevel@tonic-gate static map_info_t * 25460Sstevel@tonic-gate object_name_to_map(struct ps_prochandle *P, Lmid_t lmid, const char *name) 25470Sstevel@tonic-gate { 25480Sstevel@tonic-gate map_info_t *mptr; 25490Sstevel@tonic-gate 25500Sstevel@tonic-gate if (!P->info_valid) 25510Sstevel@tonic-gate Pupdate_maps(P); 25520Sstevel@tonic-gate 25530Sstevel@tonic-gate if (P->map_exec == NULL && ((mptr = Paddr2mptr(P, 25540Sstevel@tonic-gate Pgetauxval(P, AT_ENTRY))) != NULL || (mptr = exec_map(P)) != NULL)) 25550Sstevel@tonic-gate P->map_exec = mptr; 25560Sstevel@tonic-gate 25570Sstevel@tonic-gate if (P->map_ldso == NULL && (mptr = Paddr2mptr(P, 25580Sstevel@tonic-gate Pgetauxval(P, AT_BASE))) != NULL) 25590Sstevel@tonic-gate P->map_ldso = mptr; 25600Sstevel@tonic-gate 25610Sstevel@tonic-gate if (name == PR_OBJ_EXEC) 25620Sstevel@tonic-gate mptr = P->map_exec; 25630Sstevel@tonic-gate else if (name == PR_OBJ_LDSO) 25640Sstevel@tonic-gate mptr = P->map_ldso; 25650Sstevel@tonic-gate else if (Prd_agent(P) != NULL || P->state == PS_IDLE) 25660Sstevel@tonic-gate mptr = object_to_map(P, lmid, name); 25670Sstevel@tonic-gate else 25680Sstevel@tonic-gate mptr = NULL; 25690Sstevel@tonic-gate 25700Sstevel@tonic-gate return (mptr); 25710Sstevel@tonic-gate } 25720Sstevel@tonic-gate 25730Sstevel@tonic-gate /* 25740Sstevel@tonic-gate * When two symbols are found by address, decide which one is to be preferred. 25750Sstevel@tonic-gate */ 25760Sstevel@tonic-gate static GElf_Sym * 25770Sstevel@tonic-gate sym_prefer(GElf_Sym *sym1, char *name1, GElf_Sym *sym2, char *name2) 25780Sstevel@tonic-gate { 25790Sstevel@tonic-gate /* 25800Sstevel@tonic-gate * Prefer the non-NULL symbol. 25810Sstevel@tonic-gate */ 25820Sstevel@tonic-gate if (sym1 == NULL) 25830Sstevel@tonic-gate return (sym2); 25840Sstevel@tonic-gate if (sym2 == NULL) 25850Sstevel@tonic-gate return (sym1); 25860Sstevel@tonic-gate 25870Sstevel@tonic-gate /* 25880Sstevel@tonic-gate * Defer to the sort ordering... 25890Sstevel@tonic-gate */ 25900Sstevel@tonic-gate return (byaddr_cmp_common(sym1, name1, sym2, name2) <= 0 ? sym1 : sym2); 25910Sstevel@tonic-gate } 25920Sstevel@tonic-gate 25930Sstevel@tonic-gate /* 25940Sstevel@tonic-gate * Look up a symbol by address in the specified symbol table. 25950Sstevel@tonic-gate * Adjustment to 'addr' must already have been made for the 25960Sstevel@tonic-gate * offset of the symbol if this is a dynamic library symbol table. 25970Sstevel@tonic-gate */ 25980Sstevel@tonic-gate static GElf_Sym * 25990Sstevel@tonic-gate sym_by_addr(sym_tbl_t *symtab, GElf_Addr addr, GElf_Sym *symp, uint_t *idp) 26000Sstevel@tonic-gate { 26010Sstevel@tonic-gate Elf_Data *data = symtab->sym_data; 26020Sstevel@tonic-gate GElf_Sym sym, osym; 26030Sstevel@tonic-gate uint_t i, oid, *byaddr = symtab->sym_byaddr; 26040Sstevel@tonic-gate int min, max, mid, omid, found = 0; 26050Sstevel@tonic-gate 26060Sstevel@tonic-gate if (data == NULL) 26070Sstevel@tonic-gate return (NULL); 26080Sstevel@tonic-gate 26090Sstevel@tonic-gate min = 0; 26100Sstevel@tonic-gate max = symtab->sym_count - 1; 26110Sstevel@tonic-gate osym.st_value = 0; 26120Sstevel@tonic-gate 26130Sstevel@tonic-gate /* 26140Sstevel@tonic-gate * We can't return when we've found a match, we have to continue 26150Sstevel@tonic-gate * searching for the closest matching symbol. 26160Sstevel@tonic-gate */ 26170Sstevel@tonic-gate while (min <= max) { 26180Sstevel@tonic-gate mid = (max + min) / 2; 26190Sstevel@tonic-gate 26200Sstevel@tonic-gate i = byaddr[mid]; 26210Sstevel@tonic-gate (void) gelf_getsym(data, i, &sym); 26220Sstevel@tonic-gate 26230Sstevel@tonic-gate if (addr >= sym.st_value && 26240Sstevel@tonic-gate addr < sym.st_value + sym.st_size && 26250Sstevel@tonic-gate (!found || sym.st_value > osym.st_value)) { 26260Sstevel@tonic-gate osym = sym; 26270Sstevel@tonic-gate omid = mid; 26280Sstevel@tonic-gate oid = i; 26290Sstevel@tonic-gate found = 1; 26300Sstevel@tonic-gate } 26310Sstevel@tonic-gate 26320Sstevel@tonic-gate if (addr < sym.st_value) 26330Sstevel@tonic-gate max = mid - 1; 26340Sstevel@tonic-gate else 26350Sstevel@tonic-gate min = mid + 1; 26360Sstevel@tonic-gate } 26370Sstevel@tonic-gate 26380Sstevel@tonic-gate if (!found) 26390Sstevel@tonic-gate return (NULL); 26400Sstevel@tonic-gate 26410Sstevel@tonic-gate /* 26420Sstevel@tonic-gate * There may be many symbols with identical values so we walk 26430Sstevel@tonic-gate * backward in the byaddr table to find the best match. 26440Sstevel@tonic-gate */ 26450Sstevel@tonic-gate do { 26460Sstevel@tonic-gate sym = osym; 26470Sstevel@tonic-gate i = oid; 26480Sstevel@tonic-gate 26490Sstevel@tonic-gate if (omid == 0) 26500Sstevel@tonic-gate break; 26510Sstevel@tonic-gate 26520Sstevel@tonic-gate oid = byaddr[--omid]; 26530Sstevel@tonic-gate (void) gelf_getsym(data, oid, &osym); 26540Sstevel@tonic-gate } while (addr >= osym.st_value && 26550Sstevel@tonic-gate addr < sym.st_value + osym.st_size && 26560Sstevel@tonic-gate osym.st_value == sym.st_value); 26570Sstevel@tonic-gate 26580Sstevel@tonic-gate *symp = sym; 26590Sstevel@tonic-gate if (idp != NULL) 26600Sstevel@tonic-gate *idp = i; 26610Sstevel@tonic-gate return (symp); 26620Sstevel@tonic-gate } 26630Sstevel@tonic-gate 26640Sstevel@tonic-gate /* 26650Sstevel@tonic-gate * Look up a symbol by name in the specified symbol table. 26660Sstevel@tonic-gate */ 26670Sstevel@tonic-gate static GElf_Sym * 26680Sstevel@tonic-gate sym_by_name(sym_tbl_t *symtab, const char *name, GElf_Sym *symp, uint_t *idp) 26690Sstevel@tonic-gate { 26700Sstevel@tonic-gate Elf_Data *data = symtab->sym_data; 26710Sstevel@tonic-gate char *strs = symtab->sym_strs; 26720Sstevel@tonic-gate uint_t i, *byname = symtab->sym_byname; 26730Sstevel@tonic-gate int min, mid, max, cmp; 26740Sstevel@tonic-gate 26750Sstevel@tonic-gate if (data == NULL || strs == NULL) 26760Sstevel@tonic-gate return (NULL); 26770Sstevel@tonic-gate 26780Sstevel@tonic-gate min = 0; 26790Sstevel@tonic-gate max = symtab->sym_count - 1; 26800Sstevel@tonic-gate 26810Sstevel@tonic-gate while (min <= max) { 26820Sstevel@tonic-gate mid = (max + min) / 2; 26830Sstevel@tonic-gate 26840Sstevel@tonic-gate i = byname[mid]; 26850Sstevel@tonic-gate (void) gelf_getsym(data, i, symp); 26860Sstevel@tonic-gate 26870Sstevel@tonic-gate if ((cmp = strcmp(name, strs + symp->st_name)) == 0) { 26880Sstevel@tonic-gate if (idp != NULL) 26890Sstevel@tonic-gate *idp = i; 26900Sstevel@tonic-gate return (symp); 26910Sstevel@tonic-gate } 26920Sstevel@tonic-gate 26930Sstevel@tonic-gate if (cmp < 0) 26940Sstevel@tonic-gate max = mid - 1; 26950Sstevel@tonic-gate else 26960Sstevel@tonic-gate min = mid + 1; 26970Sstevel@tonic-gate } 26980Sstevel@tonic-gate 26990Sstevel@tonic-gate return (NULL); 27000Sstevel@tonic-gate } 27010Sstevel@tonic-gate 27020Sstevel@tonic-gate /* 27030Sstevel@tonic-gate * Search the process symbol tables looking for a symbol whose 27040Sstevel@tonic-gate * value to value+size contain the address specified by addr. 27050Sstevel@tonic-gate * Return values are: 27060Sstevel@tonic-gate * sym_name_buffer containing the symbol name 27070Sstevel@tonic-gate * GElf_Sym symbol table entry 27080Sstevel@tonic-gate * prsyminfo_t ancillary symbol information 27090Sstevel@tonic-gate * Returns 0 on success, -1 on failure. 27100Sstevel@tonic-gate */ 27110Sstevel@tonic-gate int 27120Sstevel@tonic-gate Pxlookup_by_addr( 27130Sstevel@tonic-gate struct ps_prochandle *P, 27140Sstevel@tonic-gate uintptr_t addr, /* process address being sought */ 27150Sstevel@tonic-gate char *sym_name_buffer, /* buffer for the symbol name */ 27160Sstevel@tonic-gate size_t bufsize, /* size of sym_name_buffer */ 27170Sstevel@tonic-gate GElf_Sym *symbolp, /* returned symbol table entry */ 27180Sstevel@tonic-gate prsyminfo_t *sip) /* returned symbol info */ 27190Sstevel@tonic-gate { 27200Sstevel@tonic-gate GElf_Sym *symp; 27210Sstevel@tonic-gate char *name; 27220Sstevel@tonic-gate GElf_Sym sym1, *sym1p = NULL; 27230Sstevel@tonic-gate GElf_Sym sym2, *sym2p = NULL; 27240Sstevel@tonic-gate char *name1 = NULL; 27250Sstevel@tonic-gate char *name2 = NULL; 27260Sstevel@tonic-gate uint_t i1; 27270Sstevel@tonic-gate uint_t i2; 27280Sstevel@tonic-gate map_info_t *mptr; 27290Sstevel@tonic-gate file_info_t *fptr; 27300Sstevel@tonic-gate 27310Sstevel@tonic-gate (void) Prd_agent(P); 27320Sstevel@tonic-gate 27330Sstevel@tonic-gate if ((mptr = Paddr2mptr(P, addr)) == NULL || /* no such address */ 27340Sstevel@tonic-gate (fptr = build_map_symtab(P, mptr)) == NULL || /* no mapped file */ 27350Sstevel@tonic-gate fptr->file_elf == NULL) /* not an ELF file */ 27360Sstevel@tonic-gate return (-1); 27370Sstevel@tonic-gate 27380Sstevel@tonic-gate /* 27390Sstevel@tonic-gate * Adjust the address by the load object base address in 27400Sstevel@tonic-gate * case the address turns out to be in a shared library. 27410Sstevel@tonic-gate */ 27420Sstevel@tonic-gate addr -= fptr->file_dyn_base; 27430Sstevel@tonic-gate 27440Sstevel@tonic-gate /* 27450Sstevel@tonic-gate * Search both symbol tables, symtab first, then dynsym. 27460Sstevel@tonic-gate */ 27470Sstevel@tonic-gate if ((sym1p = sym_by_addr(&fptr->file_symtab, addr, &sym1, &i1)) != NULL) 27480Sstevel@tonic-gate name1 = fptr->file_symtab.sym_strs + sym1.st_name; 27490Sstevel@tonic-gate if ((sym2p = sym_by_addr(&fptr->file_dynsym, addr, &sym2, &i2)) != NULL) 27500Sstevel@tonic-gate name2 = fptr->file_dynsym.sym_strs + sym2.st_name; 27510Sstevel@tonic-gate 27520Sstevel@tonic-gate if ((symp = sym_prefer(sym1p, name1, sym2p, name2)) == NULL) 27530Sstevel@tonic-gate return (-1); 27540Sstevel@tonic-gate 27550Sstevel@tonic-gate name = (symp == sym1p) ? name1 : name2; 27560Sstevel@tonic-gate if (bufsize > 0) { 27570Sstevel@tonic-gate (void) strncpy(sym_name_buffer, name, bufsize); 27580Sstevel@tonic-gate sym_name_buffer[bufsize - 1] = '\0'; 27590Sstevel@tonic-gate } 27600Sstevel@tonic-gate 27610Sstevel@tonic-gate *symbolp = *symp; 27620Sstevel@tonic-gate if (sip != NULL) { 27630Sstevel@tonic-gate sip->prs_name = bufsize == 0 ? NULL : sym_name_buffer; 27640Sstevel@tonic-gate sip->prs_object = fptr->file_lbase; 27650Sstevel@tonic-gate sip->prs_id = (symp == sym1p) ? i1 : i2; 27660Sstevel@tonic-gate sip->prs_table = (symp == sym1p) ? PR_SYMTAB : PR_DYNSYM; 27670Sstevel@tonic-gate sip->prs_lmid = (fptr->file_lo == NULL) ? LM_ID_BASE : 27680Sstevel@tonic-gate fptr->file_lo->rl_lmident; 27690Sstevel@tonic-gate } 27700Sstevel@tonic-gate 27710Sstevel@tonic-gate if (GELF_ST_TYPE(symbolp->st_info) != STT_TLS) 27720Sstevel@tonic-gate symbolp->st_value += fptr->file_dyn_base; 27730Sstevel@tonic-gate 27740Sstevel@tonic-gate return (0); 27750Sstevel@tonic-gate } 27760Sstevel@tonic-gate 27770Sstevel@tonic-gate int 27780Sstevel@tonic-gate Plookup_by_addr(struct ps_prochandle *P, uintptr_t addr, char *buf, size_t size, 27790Sstevel@tonic-gate GElf_Sym *symp) 27800Sstevel@tonic-gate { 27810Sstevel@tonic-gate return (Pxlookup_by_addr(P, addr, buf, size, symp, NULL)); 27820Sstevel@tonic-gate } 27830Sstevel@tonic-gate 27840Sstevel@tonic-gate /* 27850Sstevel@tonic-gate * Search the process symbol tables looking for a symbol whose name matches the 27860Sstevel@tonic-gate * specified name and whose object and link map optionally match the specified 27870Sstevel@tonic-gate * parameters. On success, the function returns 0 and fills in the GElf_Sym 27880Sstevel@tonic-gate * symbol table entry. On failure, -1 is returned. 27890Sstevel@tonic-gate */ 27900Sstevel@tonic-gate int 27910Sstevel@tonic-gate Pxlookup_by_name( 27920Sstevel@tonic-gate struct ps_prochandle *P, 27930Sstevel@tonic-gate Lmid_t lmid, /* link map to match, or -1 for any */ 27940Sstevel@tonic-gate const char *oname, /* load object name */ 27950Sstevel@tonic-gate const char *sname, /* symbol name */ 27960Sstevel@tonic-gate GElf_Sym *symp, /* returned symbol table entry */ 27970Sstevel@tonic-gate prsyminfo_t *sip) /* returned symbol info */ 27980Sstevel@tonic-gate { 27990Sstevel@tonic-gate map_info_t *mptr; 28000Sstevel@tonic-gate file_info_t *fptr; 28010Sstevel@tonic-gate int cnt; 28020Sstevel@tonic-gate 28030Sstevel@tonic-gate GElf_Sym sym; 28040Sstevel@tonic-gate prsyminfo_t si; 28050Sstevel@tonic-gate int rv = -1; 28060Sstevel@tonic-gate uint_t id; 28070Sstevel@tonic-gate 28080Sstevel@tonic-gate if (oname == PR_OBJ_EVERY) { 28090Sstevel@tonic-gate /* create all the file_info_t's for all the mappings */ 28100Sstevel@tonic-gate (void) Prd_agent(P); 28110Sstevel@tonic-gate cnt = P->num_files; 28120Sstevel@tonic-gate fptr = list_next(&P->file_head); 28130Sstevel@tonic-gate } else { 28140Sstevel@tonic-gate cnt = 1; 28150Sstevel@tonic-gate if ((mptr = object_name_to_map(P, lmid, oname)) == NULL || 28160Sstevel@tonic-gate (fptr = build_map_symtab(P, mptr)) == NULL) 28170Sstevel@tonic-gate return (-1); 28180Sstevel@tonic-gate } 28190Sstevel@tonic-gate 28200Sstevel@tonic-gate /* 28210Sstevel@tonic-gate * Iterate through the loaded object files and look for the symbol 28220Sstevel@tonic-gate * name in the .symtab and .dynsym of each. If we encounter a match 28230Sstevel@tonic-gate * with SHN_UNDEF, keep looking in hopes of finding a better match. 28240Sstevel@tonic-gate * This means that a name such as "puts" will match the puts function 28250Sstevel@tonic-gate * in libc instead of matching the puts PLT entry in the a.out file. 28260Sstevel@tonic-gate */ 28270Sstevel@tonic-gate for (; cnt > 0; cnt--, fptr = list_next(fptr)) { 28280Sstevel@tonic-gate Pbuild_file_symtab(P, fptr); 28290Sstevel@tonic-gate 28300Sstevel@tonic-gate if (fptr->file_elf == NULL) 28310Sstevel@tonic-gate continue; 28320Sstevel@tonic-gate 28330Sstevel@tonic-gate if (lmid != PR_LMID_EVERY && fptr->file_lo != NULL && 28340Sstevel@tonic-gate lmid != fptr->file_lo->rl_lmident) 28350Sstevel@tonic-gate continue; 28360Sstevel@tonic-gate 28370Sstevel@tonic-gate if (fptr->file_symtab.sym_data != NULL && 28380Sstevel@tonic-gate sym_by_name(&fptr->file_symtab, sname, symp, &id)) { 28390Sstevel@tonic-gate if (sip != NULL) { 28400Sstevel@tonic-gate sip->prs_id = id; 28410Sstevel@tonic-gate sip->prs_table = PR_SYMTAB; 28420Sstevel@tonic-gate sip->prs_object = oname; 28430Sstevel@tonic-gate sip->prs_name = sname; 28440Sstevel@tonic-gate sip->prs_lmid = fptr->file_lo == NULL ? 28450Sstevel@tonic-gate LM_ID_BASE : fptr->file_lo->rl_lmident; 28460Sstevel@tonic-gate } 28470Sstevel@tonic-gate } else if (fptr->file_dynsym.sym_data != NULL && 28480Sstevel@tonic-gate sym_by_name(&fptr->file_dynsym, sname, symp, &id)) { 28490Sstevel@tonic-gate if (sip != NULL) { 28500Sstevel@tonic-gate sip->prs_id = id; 28510Sstevel@tonic-gate sip->prs_table = PR_DYNSYM; 28520Sstevel@tonic-gate sip->prs_object = oname; 28530Sstevel@tonic-gate sip->prs_name = sname; 28540Sstevel@tonic-gate sip->prs_lmid = fptr->file_lo == NULL ? 28550Sstevel@tonic-gate LM_ID_BASE : fptr->file_lo->rl_lmident; 28560Sstevel@tonic-gate } 28570Sstevel@tonic-gate } else { 28580Sstevel@tonic-gate continue; 28590Sstevel@tonic-gate } 28600Sstevel@tonic-gate 28610Sstevel@tonic-gate if (GELF_ST_TYPE(symp->st_info) != STT_TLS) 28620Sstevel@tonic-gate symp->st_value += fptr->file_dyn_base; 28630Sstevel@tonic-gate 28640Sstevel@tonic-gate if (symp->st_shndx != SHN_UNDEF) 28650Sstevel@tonic-gate return (0); 28660Sstevel@tonic-gate 28670Sstevel@tonic-gate if (rv != 0) { 28680Sstevel@tonic-gate if (sip != NULL) 28690Sstevel@tonic-gate si = *sip; 28700Sstevel@tonic-gate sym = *symp; 28710Sstevel@tonic-gate rv = 0; 28720Sstevel@tonic-gate } 28730Sstevel@tonic-gate } 28740Sstevel@tonic-gate 28750Sstevel@tonic-gate if (rv == 0) { 28760Sstevel@tonic-gate if (sip != NULL) 28770Sstevel@tonic-gate *sip = si; 28780Sstevel@tonic-gate *symp = sym; 28790Sstevel@tonic-gate } 28800Sstevel@tonic-gate 28810Sstevel@tonic-gate return (rv); 28820Sstevel@tonic-gate } 28830Sstevel@tonic-gate 28840Sstevel@tonic-gate /* 28850Sstevel@tonic-gate * Search the process symbol tables looking for a symbol whose name matches the 28860Sstevel@tonic-gate * specified name, but without any restriction on the link map id. 28870Sstevel@tonic-gate */ 28880Sstevel@tonic-gate int 28890Sstevel@tonic-gate Plookup_by_name(struct ps_prochandle *P, const char *object, 28900Sstevel@tonic-gate const char *symbol, GElf_Sym *symp) 28910Sstevel@tonic-gate { 28920Sstevel@tonic-gate return (Pxlookup_by_name(P, PR_LMID_EVERY, object, symbol, symp, NULL)); 28930Sstevel@tonic-gate } 28940Sstevel@tonic-gate 28950Sstevel@tonic-gate /* 28960Sstevel@tonic-gate * Iterate over the process's address space mappings. 28970Sstevel@tonic-gate */ 28980Sstevel@tonic-gate int 28990Sstevel@tonic-gate Pmapping_iter(struct ps_prochandle *P, proc_map_f *func, void *cd) 29000Sstevel@tonic-gate { 29010Sstevel@tonic-gate map_info_t *mptr; 29020Sstevel@tonic-gate file_info_t *fptr; 29030Sstevel@tonic-gate char *object_name; 29040Sstevel@tonic-gate int rc = 0; 29050Sstevel@tonic-gate int i; 29060Sstevel@tonic-gate 29070Sstevel@tonic-gate /* create all the file_info_t's for all the mappings */ 29080Sstevel@tonic-gate (void) Prd_agent(P); 29090Sstevel@tonic-gate 29100Sstevel@tonic-gate for (i = 0, mptr = P->mappings; i < P->map_count; i++, mptr++) { 29110Sstevel@tonic-gate if ((fptr = mptr->map_file) == NULL) 29120Sstevel@tonic-gate object_name = NULL; 29130Sstevel@tonic-gate else 29140Sstevel@tonic-gate object_name = fptr->file_lname; 29150Sstevel@tonic-gate if ((rc = func(cd, &mptr->map_pmap, object_name)) != 0) 29160Sstevel@tonic-gate return (rc); 29170Sstevel@tonic-gate } 29180Sstevel@tonic-gate return (0); 29190Sstevel@tonic-gate } 29200Sstevel@tonic-gate 29210Sstevel@tonic-gate /* 29220Sstevel@tonic-gate * Iterate over the process's mapped objects. 29230Sstevel@tonic-gate */ 29240Sstevel@tonic-gate int 29250Sstevel@tonic-gate Pobject_iter(struct ps_prochandle *P, proc_map_f *func, void *cd) 29260Sstevel@tonic-gate { 29270Sstevel@tonic-gate map_info_t *mptr; 29280Sstevel@tonic-gate file_info_t *fptr; 29290Sstevel@tonic-gate uint_t cnt; 29300Sstevel@tonic-gate int rc = 0; 29310Sstevel@tonic-gate 29320Sstevel@tonic-gate (void) Prd_agent(P); /* create file_info_t's for all the mappings */ 29330Sstevel@tonic-gate Pupdate_maps(P); 29340Sstevel@tonic-gate 29350Sstevel@tonic-gate for (cnt = P->num_files, fptr = list_next(&P->file_head); 29360Sstevel@tonic-gate cnt; cnt--, fptr = list_next(fptr)) { 29370Sstevel@tonic-gate 29380Sstevel@tonic-gate const char *lname = fptr->file_lname ? fptr->file_lname : ""; 29390Sstevel@tonic-gate 29400Sstevel@tonic-gate if ((mptr = fptr->file_map) == NULL) 29410Sstevel@tonic-gate continue; 29420Sstevel@tonic-gate 29430Sstevel@tonic-gate if ((rc = func(cd, &mptr->map_pmap, lname)) != 0) 29440Sstevel@tonic-gate return (rc); 29450Sstevel@tonic-gate } 29460Sstevel@tonic-gate return (0); 29470Sstevel@tonic-gate } 29480Sstevel@tonic-gate 29490Sstevel@tonic-gate /* 29500Sstevel@tonic-gate * Given a virtual address, return the name of the underlying 29510Sstevel@tonic-gate * mapped object (file), as provided by the dynamic linker. 29520Sstevel@tonic-gate * Return NULL on failure (no underlying shared library). 29530Sstevel@tonic-gate */ 29540Sstevel@tonic-gate char * 29550Sstevel@tonic-gate Pobjname(struct ps_prochandle *P, uintptr_t addr, 29560Sstevel@tonic-gate char *buffer, size_t bufsize) 29570Sstevel@tonic-gate { 29580Sstevel@tonic-gate map_info_t *mptr; 29590Sstevel@tonic-gate file_info_t *fptr; 29600Sstevel@tonic-gate 29610Sstevel@tonic-gate /* create all the file_info_t's for all the mappings */ 29620Sstevel@tonic-gate (void) Prd_agent(P); 29630Sstevel@tonic-gate 29640Sstevel@tonic-gate if ((mptr = Paddr2mptr(P, addr)) != NULL && 29650Sstevel@tonic-gate (fptr = mptr->map_file) != NULL && 29660Sstevel@tonic-gate fptr->file_lname != NULL) { 29670Sstevel@tonic-gate (void) strncpy(buffer, fptr->file_lname, bufsize); 29680Sstevel@tonic-gate if (strlen(fptr->file_lname) >= bufsize) 29690Sstevel@tonic-gate buffer[bufsize-1] = '\0'; 29700Sstevel@tonic-gate return (buffer); 29710Sstevel@tonic-gate } 29720Sstevel@tonic-gate return (NULL); 29730Sstevel@tonic-gate } 29740Sstevel@tonic-gate 29750Sstevel@tonic-gate /* 29760Sstevel@tonic-gate * Given a virtual address, return the link map id of the underlying mapped 29770Sstevel@tonic-gate * object (file), as provided by the dynamic linker. Return -1 on failure. 29780Sstevel@tonic-gate */ 29790Sstevel@tonic-gate int 29800Sstevel@tonic-gate Plmid(struct ps_prochandle *P, uintptr_t addr, Lmid_t *lmidp) 29810Sstevel@tonic-gate { 29820Sstevel@tonic-gate map_info_t *mptr; 29830Sstevel@tonic-gate file_info_t *fptr; 29840Sstevel@tonic-gate 29850Sstevel@tonic-gate /* create all the file_info_t's for all the mappings */ 29860Sstevel@tonic-gate (void) Prd_agent(P); 29870Sstevel@tonic-gate 29880Sstevel@tonic-gate if ((mptr = Paddr2mptr(P, addr)) != NULL && 29890Sstevel@tonic-gate (fptr = mptr->map_file) != NULL && fptr->file_lo != NULL) { 29900Sstevel@tonic-gate *lmidp = fptr->file_lo->rl_lmident; 29910Sstevel@tonic-gate return (0); 29920Sstevel@tonic-gate } 29930Sstevel@tonic-gate 29940Sstevel@tonic-gate return (-1); 29950Sstevel@tonic-gate } 29960Sstevel@tonic-gate 29970Sstevel@tonic-gate /* 29980Sstevel@tonic-gate * Given an object name and optional lmid, iterate over the object's symbols. 29990Sstevel@tonic-gate * If which == PR_SYMTAB, search the normal symbol table. 30000Sstevel@tonic-gate * If which == PR_DYNSYM, search the dynamic symbol table. 30010Sstevel@tonic-gate */ 30020Sstevel@tonic-gate static int 30030Sstevel@tonic-gate Psymbol_iter_com(struct ps_prochandle *P, Lmid_t lmid, const char *object_name, 30040Sstevel@tonic-gate int which, int mask, pr_order_t order, proc_xsym_f *func, void *cd) 30050Sstevel@tonic-gate { 30060Sstevel@tonic-gate GElf_Sym sym; 30070Sstevel@tonic-gate map_info_t *mptr; 30080Sstevel@tonic-gate file_info_t *fptr; 30090Sstevel@tonic-gate sym_tbl_t *symtab; 30100Sstevel@tonic-gate Elf_Data *data; 30110Sstevel@tonic-gate size_t symn; 30120Sstevel@tonic-gate const char *strs; 30130Sstevel@tonic-gate size_t strsz; 30140Sstevel@tonic-gate prsyminfo_t si; 30150Sstevel@tonic-gate int rv; 30160Sstevel@tonic-gate uint_t *map, i, count, ndx; 30170Sstevel@tonic-gate 30180Sstevel@tonic-gate if ((mptr = object_name_to_map(P, lmid, object_name)) == NULL) 30190Sstevel@tonic-gate return (-1); 30200Sstevel@tonic-gate 30210Sstevel@tonic-gate if ((fptr = build_map_symtab(P, mptr)) == NULL || /* no mapped file */ 30220Sstevel@tonic-gate fptr->file_elf == NULL) /* not an ELF file */ 30230Sstevel@tonic-gate return (-1); 30240Sstevel@tonic-gate 30250Sstevel@tonic-gate /* 30260Sstevel@tonic-gate * Search the specified symbol table. 30270Sstevel@tonic-gate */ 30280Sstevel@tonic-gate switch (which) { 30290Sstevel@tonic-gate case PR_SYMTAB: 30300Sstevel@tonic-gate symtab = &fptr->file_symtab; 30310Sstevel@tonic-gate si.prs_table = PR_SYMTAB; 30320Sstevel@tonic-gate break; 30330Sstevel@tonic-gate case PR_DYNSYM: 30340Sstevel@tonic-gate symtab = &fptr->file_dynsym; 30350Sstevel@tonic-gate si.prs_table = PR_DYNSYM; 30360Sstevel@tonic-gate break; 30370Sstevel@tonic-gate default: 30380Sstevel@tonic-gate return (-1); 30390Sstevel@tonic-gate } 30400Sstevel@tonic-gate 30410Sstevel@tonic-gate si.prs_object = object_name; 30420Sstevel@tonic-gate si.prs_lmid = fptr->file_lo == NULL ? 30430Sstevel@tonic-gate LM_ID_BASE : fptr->file_lo->rl_lmident; 30440Sstevel@tonic-gate 30450Sstevel@tonic-gate data = symtab->sym_data; 30460Sstevel@tonic-gate symn = symtab->sym_symn; 30470Sstevel@tonic-gate strs = symtab->sym_strs; 30480Sstevel@tonic-gate strsz = symtab->sym_strsz; 30490Sstevel@tonic-gate 30500Sstevel@tonic-gate if (data == NULL || strs == NULL) 30510Sstevel@tonic-gate return (-1); 30520Sstevel@tonic-gate 30530Sstevel@tonic-gate switch (order) { 30540Sstevel@tonic-gate case PRO_NATURAL: 30550Sstevel@tonic-gate map = NULL; 30560Sstevel@tonic-gate count = symn; 30570Sstevel@tonic-gate break; 30580Sstevel@tonic-gate case PRO_BYNAME: 30590Sstevel@tonic-gate map = symtab->sym_byname; 30600Sstevel@tonic-gate count = symtab->sym_count; 30610Sstevel@tonic-gate break; 30620Sstevel@tonic-gate case PRO_BYADDR: 30630Sstevel@tonic-gate map = symtab->sym_byaddr; 30640Sstevel@tonic-gate count = symtab->sym_count; 30650Sstevel@tonic-gate break; 30660Sstevel@tonic-gate default: 30670Sstevel@tonic-gate return (-1); 30680Sstevel@tonic-gate } 30690Sstevel@tonic-gate 30700Sstevel@tonic-gate rv = 0; 30710Sstevel@tonic-gate 30720Sstevel@tonic-gate for (i = 0; i < count; i++) { 30730Sstevel@tonic-gate ndx = map == NULL ? i : map[i]; 30740Sstevel@tonic-gate if (gelf_getsym(data, ndx, &sym) != NULL) { 30750Sstevel@tonic-gate uint_t s_bind, s_type, type; 30760Sstevel@tonic-gate 30770Sstevel@tonic-gate if (sym.st_name >= strsz) /* invalid st_name */ 30780Sstevel@tonic-gate continue; 30790Sstevel@tonic-gate 30800Sstevel@tonic-gate s_bind = GELF_ST_BIND(sym.st_info); 30810Sstevel@tonic-gate s_type = GELF_ST_TYPE(sym.st_info); 30820Sstevel@tonic-gate 30830Sstevel@tonic-gate /* 30840Sstevel@tonic-gate * In case you haven't already guessed, this relies on 30850Sstevel@tonic-gate * the bitmask used in <libproc.h> for encoding symbol 30860Sstevel@tonic-gate * type and binding matching the order of STB and STT 30870Sstevel@tonic-gate * constants in <sys/elf.h>. ELF can't change without 30880Sstevel@tonic-gate * breaking binary compatibility, so I think this is 30890Sstevel@tonic-gate * reasonably fair game. 30900Sstevel@tonic-gate */ 30910Sstevel@tonic-gate if (s_bind < STB_NUM && s_type < STT_NUM) { 30920Sstevel@tonic-gate type = (1 << (s_type + 8)) | (1 << s_bind); 30930Sstevel@tonic-gate if ((type & ~mask) != 0) 30940Sstevel@tonic-gate continue; 30950Sstevel@tonic-gate } else 30960Sstevel@tonic-gate continue; /* Invalid type or binding */ 30970Sstevel@tonic-gate 30980Sstevel@tonic-gate if (GELF_ST_TYPE(sym.st_info) != STT_TLS) 30990Sstevel@tonic-gate sym.st_value += fptr->file_dyn_base; 31000Sstevel@tonic-gate 31010Sstevel@tonic-gate si.prs_name = strs + sym.st_name; 31020Sstevel@tonic-gate si.prs_id = ndx; 31030Sstevel@tonic-gate if ((rv = func(cd, &sym, strs + sym.st_name, &si)) != 0) 31040Sstevel@tonic-gate break; 31050Sstevel@tonic-gate } 31060Sstevel@tonic-gate } 31070Sstevel@tonic-gate 31080Sstevel@tonic-gate return (rv); 31090Sstevel@tonic-gate } 31100Sstevel@tonic-gate 31110Sstevel@tonic-gate int 31120Sstevel@tonic-gate Pxsymbol_iter(struct ps_prochandle *P, Lmid_t lmid, const char *object_name, 31130Sstevel@tonic-gate int which, int mask, proc_xsym_f *func, void *cd) 31140Sstevel@tonic-gate { 31150Sstevel@tonic-gate return (Psymbol_iter_com(P, lmid, object_name, which, mask, 31160Sstevel@tonic-gate PRO_NATURAL, func, cd)); 31170Sstevel@tonic-gate } 31180Sstevel@tonic-gate 31190Sstevel@tonic-gate int 31200Sstevel@tonic-gate Psymbol_iter_by_lmid(struct ps_prochandle *P, Lmid_t lmid, 31210Sstevel@tonic-gate const char *object_name, int which, int mask, proc_sym_f *func, void *cd) 31220Sstevel@tonic-gate { 31230Sstevel@tonic-gate return (Psymbol_iter_com(P, lmid, object_name, which, mask, 31240Sstevel@tonic-gate PRO_NATURAL, (proc_xsym_f *)func, cd)); 31250Sstevel@tonic-gate } 31260Sstevel@tonic-gate 31270Sstevel@tonic-gate int 31280Sstevel@tonic-gate Psymbol_iter(struct ps_prochandle *P, 31290Sstevel@tonic-gate const char *object_name, int which, int mask, proc_sym_f *func, void *cd) 31300Sstevel@tonic-gate { 31310Sstevel@tonic-gate return (Psymbol_iter_com(P, PR_LMID_EVERY, object_name, which, mask, 31320Sstevel@tonic-gate PRO_NATURAL, (proc_xsym_f *)func, cd)); 31330Sstevel@tonic-gate } 31340Sstevel@tonic-gate 31350Sstevel@tonic-gate int 31360Sstevel@tonic-gate Psymbol_iter_by_addr(struct ps_prochandle *P, 31370Sstevel@tonic-gate const char *object_name, int which, int mask, proc_sym_f *func, void *cd) 31380Sstevel@tonic-gate { 31390Sstevel@tonic-gate return (Psymbol_iter_com(P, PR_LMID_EVERY, object_name, which, mask, 31400Sstevel@tonic-gate PRO_BYADDR, (proc_xsym_f *)func, cd)); 31410Sstevel@tonic-gate } 31420Sstevel@tonic-gate 31430Sstevel@tonic-gate int 31440Sstevel@tonic-gate Psymbol_iter_by_name(struct ps_prochandle *P, 31450Sstevel@tonic-gate const char *object_name, int which, int mask, proc_sym_f *func, void *cd) 31460Sstevel@tonic-gate { 31470Sstevel@tonic-gate return (Psymbol_iter_com(P, PR_LMID_EVERY, object_name, which, mask, 31480Sstevel@tonic-gate PRO_BYNAME, (proc_xsym_f *)func, cd)); 31490Sstevel@tonic-gate } 31500Sstevel@tonic-gate 31510Sstevel@tonic-gate /* 31520Sstevel@tonic-gate * Get the platform string from the core file if we have it; 31530Sstevel@tonic-gate * just perform the system call for the caller if this is a live process. 31540Sstevel@tonic-gate */ 31550Sstevel@tonic-gate char * 31560Sstevel@tonic-gate Pplatform(struct ps_prochandle *P, char *s, size_t n) 31570Sstevel@tonic-gate { 31580Sstevel@tonic-gate if (P->state == PS_IDLE) { 31590Sstevel@tonic-gate errno = ENODATA; 31600Sstevel@tonic-gate return (NULL); 31610Sstevel@tonic-gate } 31620Sstevel@tonic-gate 31630Sstevel@tonic-gate if (P->state == PS_DEAD) { 31640Sstevel@tonic-gate if (P->core->core_platform == NULL) { 31650Sstevel@tonic-gate errno = ENODATA; 31660Sstevel@tonic-gate return (NULL); 31670Sstevel@tonic-gate } 31680Sstevel@tonic-gate (void) strncpy(s, P->core->core_platform, n - 1); 31690Sstevel@tonic-gate s[n - 1] = '\0'; 31700Sstevel@tonic-gate 31710Sstevel@tonic-gate } else if (sysinfo(SI_PLATFORM, s, n) == -1) 31720Sstevel@tonic-gate return (NULL); 31730Sstevel@tonic-gate 31740Sstevel@tonic-gate return (s); 31750Sstevel@tonic-gate } 31760Sstevel@tonic-gate 31770Sstevel@tonic-gate /* 31780Sstevel@tonic-gate * Get the uname(2) information from the core file if we have it; 31790Sstevel@tonic-gate * just perform the system call for the caller if this is a live process. 31800Sstevel@tonic-gate */ 31810Sstevel@tonic-gate int 31820Sstevel@tonic-gate Puname(struct ps_prochandle *P, struct utsname *u) 31830Sstevel@tonic-gate { 31840Sstevel@tonic-gate if (P->state == PS_IDLE) { 31850Sstevel@tonic-gate errno = ENODATA; 31860Sstevel@tonic-gate return (-1); 31870Sstevel@tonic-gate } 31880Sstevel@tonic-gate 31890Sstevel@tonic-gate if (P->state == PS_DEAD) { 31900Sstevel@tonic-gate if (P->core->core_uts == NULL) { 31910Sstevel@tonic-gate errno = ENODATA; 31920Sstevel@tonic-gate return (-1); 31930Sstevel@tonic-gate } 31940Sstevel@tonic-gate (void) memcpy(u, P->core->core_uts, sizeof (struct utsname)); 31950Sstevel@tonic-gate return (0); 31960Sstevel@tonic-gate } 31970Sstevel@tonic-gate return (uname(u)); 31980Sstevel@tonic-gate } 31990Sstevel@tonic-gate 32000Sstevel@tonic-gate /* 32010Sstevel@tonic-gate * Get the zone name from the core file if we have it; look up the 32020Sstevel@tonic-gate * name based on the zone id if this is a live process. 32030Sstevel@tonic-gate */ 32040Sstevel@tonic-gate char * 32050Sstevel@tonic-gate Pzonename(struct ps_prochandle *P, char *s, size_t n) 32060Sstevel@tonic-gate { 32070Sstevel@tonic-gate if (P->state == PS_IDLE) { 32080Sstevel@tonic-gate errno = ENODATA; 32090Sstevel@tonic-gate return (NULL); 32100Sstevel@tonic-gate } 32110Sstevel@tonic-gate 32120Sstevel@tonic-gate if (P->state == PS_DEAD) { 32130Sstevel@tonic-gate if (P->core->core_zonename == NULL) { 32140Sstevel@tonic-gate errno = ENODATA; 32150Sstevel@tonic-gate return (NULL); 32160Sstevel@tonic-gate } 32170Sstevel@tonic-gate (void) strlcpy(s, P->core->core_zonename, n); 32180Sstevel@tonic-gate } else { 32190Sstevel@tonic-gate if (getzonenamebyid(P->status.pr_zoneid, s, n) < 0) 32200Sstevel@tonic-gate return (NULL); 32210Sstevel@tonic-gate s[n - 1] = '\0'; 32220Sstevel@tonic-gate } 32230Sstevel@tonic-gate return (s); 32240Sstevel@tonic-gate } 32250Sstevel@tonic-gate 32260Sstevel@tonic-gate /* 32270Sstevel@tonic-gate * Called from Pcreate(), Pgrab(), and Pfgrab_core() to initialize 32280Sstevel@tonic-gate * the symbol table heads in the new ps_prochandle. 32290Sstevel@tonic-gate */ 32300Sstevel@tonic-gate void 32310Sstevel@tonic-gate Pinitsym(struct ps_prochandle *P) 32320Sstevel@tonic-gate { 32330Sstevel@tonic-gate P->num_files = 0; 32340Sstevel@tonic-gate list_link(&P->file_head, NULL); 32350Sstevel@tonic-gate } 32360Sstevel@tonic-gate 32370Sstevel@tonic-gate /* 32380Sstevel@tonic-gate * Called from Prelease() to destroy the symbol tables. 32390Sstevel@tonic-gate * Must be called by the client after an exec() in the victim process. 32400Sstevel@tonic-gate */ 32410Sstevel@tonic-gate void 32420Sstevel@tonic-gate Preset_maps(struct ps_prochandle *P) 32430Sstevel@tonic-gate { 32440Sstevel@tonic-gate int i; 32450Sstevel@tonic-gate 32460Sstevel@tonic-gate if (P->rap != NULL) { 32470Sstevel@tonic-gate rd_delete(P->rap); 32480Sstevel@tonic-gate P->rap = NULL; 32490Sstevel@tonic-gate } 32500Sstevel@tonic-gate 32510Sstevel@tonic-gate if (P->execname != NULL) { 32520Sstevel@tonic-gate free(P->execname); 32530Sstevel@tonic-gate P->execname = NULL; 32540Sstevel@tonic-gate } 32550Sstevel@tonic-gate 32560Sstevel@tonic-gate if (P->auxv != NULL) { 32570Sstevel@tonic-gate free(P->auxv); 32580Sstevel@tonic-gate P->auxv = NULL; 32590Sstevel@tonic-gate P->nauxv = 0; 32600Sstevel@tonic-gate } 32610Sstevel@tonic-gate 32620Sstevel@tonic-gate for (i = 0; i < P->map_count; i++) 32630Sstevel@tonic-gate map_info_free(P, &P->mappings[i]); 32640Sstevel@tonic-gate 32650Sstevel@tonic-gate if (P->mappings != NULL) { 32660Sstevel@tonic-gate free(P->mappings); 32670Sstevel@tonic-gate P->mappings = NULL; 32680Sstevel@tonic-gate } 32690Sstevel@tonic-gate P->map_count = P->map_alloc = 0; 32700Sstevel@tonic-gate 32710Sstevel@tonic-gate P->info_valid = 0; 32720Sstevel@tonic-gate } 32730Sstevel@tonic-gate 32740Sstevel@tonic-gate typedef struct getenv_data { 32750Sstevel@tonic-gate char *buf; 32760Sstevel@tonic-gate size_t bufsize; 32770Sstevel@tonic-gate const char *search; 32780Sstevel@tonic-gate size_t searchlen; 32790Sstevel@tonic-gate } getenv_data_t; 32800Sstevel@tonic-gate 32810Sstevel@tonic-gate /*ARGSUSED*/ 32820Sstevel@tonic-gate static int 32830Sstevel@tonic-gate getenv_func(void *data, struct ps_prochandle *P, uintptr_t addr, 32840Sstevel@tonic-gate const char *nameval) 32850Sstevel@tonic-gate { 32860Sstevel@tonic-gate getenv_data_t *d = data; 32870Sstevel@tonic-gate size_t len; 32880Sstevel@tonic-gate 32890Sstevel@tonic-gate if (nameval == NULL) 32900Sstevel@tonic-gate return (0); 32910Sstevel@tonic-gate 32920Sstevel@tonic-gate if (d->searchlen < strlen(nameval) && 32930Sstevel@tonic-gate strncmp(nameval, d->search, d->searchlen) == 0 && 32940Sstevel@tonic-gate nameval[d->searchlen] == '=') { 32950Sstevel@tonic-gate len = MIN(strlen(nameval), d->bufsize - 1); 32960Sstevel@tonic-gate (void) strncpy(d->buf, nameval, len); 32970Sstevel@tonic-gate d->buf[len] = '\0'; 32980Sstevel@tonic-gate return (1); 32990Sstevel@tonic-gate } 33000Sstevel@tonic-gate 33010Sstevel@tonic-gate return (0); 33020Sstevel@tonic-gate } 33030Sstevel@tonic-gate 33040Sstevel@tonic-gate char * 33050Sstevel@tonic-gate Pgetenv(struct ps_prochandle *P, const char *name, char *buf, size_t buflen) 33060Sstevel@tonic-gate { 33070Sstevel@tonic-gate getenv_data_t d; 33080Sstevel@tonic-gate 33090Sstevel@tonic-gate d.buf = buf; 33100Sstevel@tonic-gate d.bufsize = buflen; 33110Sstevel@tonic-gate d.search = name; 33120Sstevel@tonic-gate d.searchlen = strlen(name); 33130Sstevel@tonic-gate 33140Sstevel@tonic-gate if (Penv_iter(P, getenv_func, &d) == 1) { 33150Sstevel@tonic-gate char *equals = strchr(d.buf, '='); 33160Sstevel@tonic-gate 33170Sstevel@tonic-gate if (equals != NULL) { 33180Sstevel@tonic-gate (void) memmove(d.buf, equals + 1, 33190Sstevel@tonic-gate d.buf + buflen - equals - 1); 33200Sstevel@tonic-gate d.buf[d.buf + buflen - equals] = '\0'; 33210Sstevel@tonic-gate 33220Sstevel@tonic-gate return (buf); 33230Sstevel@tonic-gate } 33240Sstevel@tonic-gate } 33250Sstevel@tonic-gate 33260Sstevel@tonic-gate return (NULL); 33270Sstevel@tonic-gate } 33280Sstevel@tonic-gate 33290Sstevel@tonic-gate /* number of argument or environment pointers to read all at once */ 33300Sstevel@tonic-gate #define NARG 100 33310Sstevel@tonic-gate 33320Sstevel@tonic-gate int 33330Sstevel@tonic-gate Penv_iter(struct ps_prochandle *P, proc_env_f *func, void *data) 33340Sstevel@tonic-gate { 33350Sstevel@tonic-gate const psinfo_t *psp; 33360Sstevel@tonic-gate uintptr_t envpoff; 33370Sstevel@tonic-gate GElf_Sym sym; 33380Sstevel@tonic-gate int ret; 33390Sstevel@tonic-gate char *buf, *nameval; 33400Sstevel@tonic-gate size_t buflen; 33410Sstevel@tonic-gate 33420Sstevel@tonic-gate int nenv = NARG; 33430Sstevel@tonic-gate long envp[NARG]; 33440Sstevel@tonic-gate 33450Sstevel@tonic-gate /* 33460Sstevel@tonic-gate * Attempt to find the "_environ" variable in the process. 33470Sstevel@tonic-gate * Failing that, use the original value provided by Ppsinfo(). 33480Sstevel@tonic-gate */ 33490Sstevel@tonic-gate if ((psp = Ppsinfo(P)) == NULL) 33500Sstevel@tonic-gate return (-1); 33510Sstevel@tonic-gate 33520Sstevel@tonic-gate envpoff = psp->pr_envp; /* Default if no _environ found */ 33530Sstevel@tonic-gate 33540Sstevel@tonic-gate if (Plookup_by_name(P, PR_OBJ_EXEC, "_environ", &sym) == 0) { 33550Sstevel@tonic-gate if (P->status.pr_dmodel == PR_MODEL_NATIVE) { 33560Sstevel@tonic-gate if (Pread(P, &envpoff, sizeof (envpoff), 33570Sstevel@tonic-gate sym.st_value) != sizeof (envpoff)) 33580Sstevel@tonic-gate envpoff = psp->pr_envp; 33590Sstevel@tonic-gate } else if (P->status.pr_dmodel == PR_MODEL_ILP32) { 33600Sstevel@tonic-gate uint32_t envpoff32; 33610Sstevel@tonic-gate 33620Sstevel@tonic-gate if (Pread(P, &envpoff32, sizeof (envpoff32), 33630Sstevel@tonic-gate sym.st_value) != sizeof (envpoff32)) 33640Sstevel@tonic-gate envpoff = psp->pr_envp; 33650Sstevel@tonic-gate else 33660Sstevel@tonic-gate envpoff = envpoff32; 33670Sstevel@tonic-gate } 33680Sstevel@tonic-gate } 33690Sstevel@tonic-gate 33700Sstevel@tonic-gate buflen = 128; 33710Sstevel@tonic-gate buf = malloc(buflen); 33720Sstevel@tonic-gate 33730Sstevel@tonic-gate ret = 0; 33740Sstevel@tonic-gate for (;;) { 33750Sstevel@tonic-gate uintptr_t envoff; 33760Sstevel@tonic-gate 33770Sstevel@tonic-gate if (nenv == NARG) { 33780Sstevel@tonic-gate (void) memset(envp, 0, sizeof (envp)); 33790Sstevel@tonic-gate if (P->status.pr_dmodel == PR_MODEL_NATIVE) { 33800Sstevel@tonic-gate if (Pread(P, envp, 33810Sstevel@tonic-gate sizeof (envp), envpoff) <= 0) { 33820Sstevel@tonic-gate ret = -1; 33830Sstevel@tonic-gate break; 33840Sstevel@tonic-gate } 33850Sstevel@tonic-gate } else if (P->status.pr_dmodel == PR_MODEL_ILP32) { 33860Sstevel@tonic-gate uint32_t e32[NARG]; 33870Sstevel@tonic-gate int i; 33880Sstevel@tonic-gate 33890Sstevel@tonic-gate (void) memset(e32, 0, sizeof (e32)); 33900Sstevel@tonic-gate if (Pread(P, e32, sizeof (e32), envpoff) <= 0) { 33910Sstevel@tonic-gate ret = -1; 33920Sstevel@tonic-gate break; 33930Sstevel@tonic-gate } 33940Sstevel@tonic-gate for (i = 0; i < NARG; i++) 33950Sstevel@tonic-gate envp[i] = e32[i]; 33960Sstevel@tonic-gate } 33970Sstevel@tonic-gate nenv = 0; 33980Sstevel@tonic-gate } 33990Sstevel@tonic-gate 34000Sstevel@tonic-gate if ((envoff = envp[nenv++]) == NULL) 34010Sstevel@tonic-gate break; 34020Sstevel@tonic-gate 34030Sstevel@tonic-gate /* 34040Sstevel@tonic-gate * Attempt to read the string from the process. 34050Sstevel@tonic-gate */ 34060Sstevel@tonic-gate again: 34070Sstevel@tonic-gate ret = Pread_string(P, buf, buflen, envoff); 34080Sstevel@tonic-gate 34090Sstevel@tonic-gate if (ret <= 0) { 34100Sstevel@tonic-gate nameval = NULL; 34110Sstevel@tonic-gate } else if (ret == buflen - 1) { 34120Sstevel@tonic-gate free(buf); 34130Sstevel@tonic-gate /* 34140Sstevel@tonic-gate * Bail if we have a corrupted environment 34150Sstevel@tonic-gate */ 34160Sstevel@tonic-gate if (buflen >= ARG_MAX) 34170Sstevel@tonic-gate return (-1); 34180Sstevel@tonic-gate buflen *= 2; 34190Sstevel@tonic-gate buf = malloc(buflen); 34200Sstevel@tonic-gate goto again; 34210Sstevel@tonic-gate } else { 34220Sstevel@tonic-gate nameval = buf; 34230Sstevel@tonic-gate } 34240Sstevel@tonic-gate 34250Sstevel@tonic-gate if ((ret = func(data, P, envoff, nameval)) != 0) 34260Sstevel@tonic-gate break; 34270Sstevel@tonic-gate 34280Sstevel@tonic-gate envpoff += (P->status.pr_dmodel == PR_MODEL_LP64)? 8 : 4; 34290Sstevel@tonic-gate } 34300Sstevel@tonic-gate 34310Sstevel@tonic-gate free(buf); 34320Sstevel@tonic-gate 34330Sstevel@tonic-gate return (ret); 34340Sstevel@tonic-gate } 3435