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