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