xref: /onnv-gate/usr/src/lib/libproc/common/Psymtab.c (revision 12968:4de81c4d427e)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51880Sahl  * Common Development and Distribution License (the "License").
61880Sahl  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
211880Sahl 
220Sstevel@tonic-gate /*
23*12968SRobert.Harris@Sun.COM  * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
262712Snn35248 #include <assert.h>
270Sstevel@tonic-gate #include <stdio.h>
280Sstevel@tonic-gate #include <stdlib.h>
290Sstevel@tonic-gate #include <stddef.h>
300Sstevel@tonic-gate #include <unistd.h>
310Sstevel@tonic-gate #include <ctype.h>
320Sstevel@tonic-gate #include <fcntl.h>
330Sstevel@tonic-gate #include <string.h>
340Sstevel@tonic-gate #include <strings.h>
350Sstevel@tonic-gate #include <memory.h>
360Sstevel@tonic-gate #include <errno.h>
370Sstevel@tonic-gate #include <dirent.h>
380Sstevel@tonic-gate #include <signal.h>
390Sstevel@tonic-gate #include <limits.h>
400Sstevel@tonic-gate #include <libgen.h>
410Sstevel@tonic-gate #include <sys/types.h>
420Sstevel@tonic-gate #include <sys/stat.h>
430Sstevel@tonic-gate #include <sys/systeminfo.h>
440Sstevel@tonic-gate #include <sys/sysmacros.h>
450Sstevel@tonic-gate 
460Sstevel@tonic-gate #include "libproc.h"
470Sstevel@tonic-gate #include "Pcontrol.h"
480Sstevel@tonic-gate #include "Putil.h"
493347Sab196087 #include "Psymtab_machelf.h"
500Sstevel@tonic-gate 
510Sstevel@tonic-gate static file_info_t *build_map_symtab(struct ps_prochandle *, map_info_t *);
520Sstevel@tonic-gate static map_info_t *exec_map(struct ps_prochandle *);
530Sstevel@tonic-gate static map_info_t *object_to_map(struct ps_prochandle *, Lmid_t, const char *);
540Sstevel@tonic-gate static map_info_t *object_name_to_map(struct ps_prochandle *,
550Sstevel@tonic-gate 	Lmid_t, const char *);
560Sstevel@tonic-gate static GElf_Sym *sym_by_name(sym_tbl_t *, const char *, GElf_Sym *, uint_t *);
57942Sahl static int read_ehdr32(struct ps_prochandle *, Elf32_Ehdr *, uint_t *,
58942Sahl     uintptr_t);
590Sstevel@tonic-gate #ifdef _LP64
60942Sahl static int read_ehdr64(struct ps_prochandle *, Elf64_Ehdr *, uint_t *,
61942Sahl     uintptr_t);
620Sstevel@tonic-gate #endif
630Sstevel@tonic-gate 
640Sstevel@tonic-gate #define	DATA_TYPES	\
650Sstevel@tonic-gate 	((1 << STT_OBJECT) | (1 << STT_FUNC) | \
660Sstevel@tonic-gate 	(1 << STT_COMMON) | (1 << STT_TLS))
670Sstevel@tonic-gate #define	IS_DATA_TYPE(tp)	(((1 << (tp)) & DATA_TYPES) != 0)
680Sstevel@tonic-gate 
690Sstevel@tonic-gate #define	MA_RWX	(MA_READ | MA_WRITE | MA_EXEC)
700Sstevel@tonic-gate 
710Sstevel@tonic-gate typedef enum {
720Sstevel@tonic-gate 	PRO_NATURAL,
730Sstevel@tonic-gate 	PRO_BYADDR,
740Sstevel@tonic-gate 	PRO_BYNAME
750Sstevel@tonic-gate } pr_order_t;
760Sstevel@tonic-gate 
770Sstevel@tonic-gate static int
addr_cmp(const void * aa,const void * bb)780Sstevel@tonic-gate addr_cmp(const void *aa, const void *bb)
790Sstevel@tonic-gate {
800Sstevel@tonic-gate 	uintptr_t a = *((uintptr_t *)aa);
810Sstevel@tonic-gate 	uintptr_t b = *((uintptr_t *)bb);
820Sstevel@tonic-gate 
830Sstevel@tonic-gate 	if (a > b)
840Sstevel@tonic-gate 		return (1);
850Sstevel@tonic-gate 	if (a < b)
860Sstevel@tonic-gate 		return (-1);
870Sstevel@tonic-gate 	return (0);
880Sstevel@tonic-gate }
890Sstevel@tonic-gate 
900Sstevel@tonic-gate /*
912915Srh87107  * This function creates a list of addresses for a load object's sections.
922915Srh87107  * The list is in ascending address order and alternates start address
932915Srh87107  * then end address for each section we're interested in. The function
942915Srh87107  * returns a pointer to the list, which must be freed by the caller.
952915Srh87107  */
962915Srh87107 static uintptr_t *
get_saddrs(struct ps_prochandle * P,uintptr_t ehdr_start,uint_t * n)972915Srh87107 get_saddrs(struct ps_prochandle *P, uintptr_t ehdr_start, uint_t *n)
982915Srh87107 {
992915Srh87107 	uintptr_t a, addr, *addrs, last = 0;
1002915Srh87107 	uint_t i, naddrs = 0, unordered = 0;
1012915Srh87107 
1022915Srh87107 	if (P->status.pr_dmodel == PR_MODEL_ILP32) {
1032915Srh87107 		Elf32_Ehdr ehdr;
1042915Srh87107 		Elf32_Phdr phdr;
1052915Srh87107 		uint_t phnum;
1062915Srh87107 
1072915Srh87107 		if (read_ehdr32(P, &ehdr, &phnum, ehdr_start) != 0)
1082915Srh87107 			return (NULL);
1092915Srh87107 
1102915Srh87107 		addrs = malloc(sizeof (uintptr_t) * phnum * 2);
1112915Srh87107 		a = ehdr_start + ehdr.e_phoff;
1122915Srh87107 		for (i = 0; i < phnum; i++, a += ehdr.e_phentsize) {
1132915Srh87107 			if (Pread(P, &phdr, sizeof (phdr), a) !=
1142915Srh87107 			    sizeof (phdr)) {
1152915Srh87107 				free(addrs);
1162915Srh87107 				return (NULL);
1172915Srh87107 			}
1182915Srh87107 			if (phdr.p_type != PT_LOAD || phdr.p_memsz == 0)
1192915Srh87107 				continue;
1202915Srh87107 
1212915Srh87107 			addr = phdr.p_vaddr;
1222915Srh87107 			if (ehdr.e_type == ET_DYN)
1232915Srh87107 				addr += ehdr_start;
1242915Srh87107 			if (last > addr)
1252915Srh87107 				unordered = 1;
1262915Srh87107 			addrs[naddrs++] = addr;
1272915Srh87107 			addrs[naddrs++] = last = addr + phdr.p_memsz - 1;
1282915Srh87107 		}
1292915Srh87107 #ifdef _LP64
1302915Srh87107 	} else {
1312915Srh87107 		Elf64_Ehdr ehdr;
1322915Srh87107 		Elf64_Phdr phdr;
1332915Srh87107 		uint_t phnum;
1342915Srh87107 
1352915Srh87107 		if (read_ehdr64(P, &ehdr, &phnum, ehdr_start) != 0)
1362915Srh87107 			return (NULL);
1372915Srh87107 
1382915Srh87107 		addrs = malloc(sizeof (uintptr_t) * phnum * 2);
1392915Srh87107 		a = ehdr_start + ehdr.e_phoff;
1402915Srh87107 		for (i = 0; i < phnum; i++, a += ehdr.e_phentsize) {
1412915Srh87107 			if (Pread(P, &phdr, sizeof (phdr), a) !=
1422915Srh87107 			    sizeof (phdr)) {
1432915Srh87107 				free(addrs);
1442915Srh87107 				return (NULL);
1452915Srh87107 			}
1462915Srh87107 			if (phdr.p_type != PT_LOAD || phdr.p_memsz == 0)
1472915Srh87107 				continue;
1482915Srh87107 
1492915Srh87107 			addr = phdr.p_vaddr;
1502915Srh87107 			if (ehdr.e_type == ET_DYN)
1512915Srh87107 				addr += ehdr_start;
1522915Srh87107 			if (last > addr)
1532915Srh87107 				unordered = 1;
1542915Srh87107 			addrs[naddrs++] = addr;
1552915Srh87107 			addrs[naddrs++] = last = addr + phdr.p_memsz - 1;
1562915Srh87107 		}
1572915Srh87107 #endif
1582915Srh87107 	}
1592915Srh87107 
1602915Srh87107 	if (unordered)
1612915Srh87107 		qsort(addrs, naddrs, sizeof (uintptr_t), addr_cmp);
1622915Srh87107 
1632915Srh87107 	*n = naddrs;
1642915Srh87107 	return (addrs);
1652915Srh87107 }
1662915Srh87107 
1672915Srh87107 /*
1680Sstevel@tonic-gate  * Allocation function for a new file_info_t
1690Sstevel@tonic-gate  */
1704753Srh87107 file_info_t *
file_info_new(struct ps_prochandle * P,map_info_t * mptr)1710Sstevel@tonic-gate file_info_new(struct ps_prochandle *P, map_info_t *mptr)
1720Sstevel@tonic-gate {
1730Sstevel@tonic-gate 	file_info_t *fptr;
1740Sstevel@tonic-gate 	map_info_t *mp;
1754753Srh87107 	uintptr_t mstart, mend, sstart, send;
1764753Srh87107 	uint_t i;
1770Sstevel@tonic-gate 
1780Sstevel@tonic-gate 	if ((fptr = calloc(1, sizeof (file_info_t))) == NULL)
1790Sstevel@tonic-gate 		return (NULL);
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate 	list_link(fptr, &P->file_head);
1820Sstevel@tonic-gate 	(void) strcpy(fptr->file_pname, mptr->map_pmap.pr_mapname);
1830Sstevel@tonic-gate 	mptr->map_file = fptr;
1840Sstevel@tonic-gate 	fptr->file_ref = 1;
1850Sstevel@tonic-gate 	fptr->file_fd = -1;
1860Sstevel@tonic-gate 	P->num_files++;
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate 	/*
1890Sstevel@tonic-gate 	 * To figure out which map_info_t instances correspond to the mappings
1902915Srh87107 	 * for this load object we try to obtain the start and end address
1912915Srh87107 	 * for each section of our in-memory ELF image. If successful, we
1920Sstevel@tonic-gate 	 * walk down the list of addresses and the list of map_info_t
1930Sstevel@tonic-gate 	 * instances in lock step to correctly find the mappings that
1940Sstevel@tonic-gate 	 * correspond to this load object.
1950Sstevel@tonic-gate 	 */
1962915Srh87107 	if ((fptr->file_saddrs = get_saddrs(P, mptr->map_pmap.pr_vaddr,
1972915Srh87107 	    &fptr->file_nsaddrs)) == NULL)
1982915Srh87107 		return (fptr);
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate 	mp = P->mappings;
2014753Srh87107 	i = 0;
2024753Srh87107 	while (mp < P->mappings + P->map_count && i < fptr->file_nsaddrs) {
2034753Srh87107 
2044753Srh87107 		/* Calculate the start and end of the mapping and section */
2054753Srh87107 		mstart = mp->map_pmap.pr_vaddr;
2064753Srh87107 		mend = mp->map_pmap.pr_vaddr + mp->map_pmap.pr_size;
2074753Srh87107 		sstart = fptr->file_saddrs[i];
2084753Srh87107 		send = fptr->file_saddrs[i + 1];
2094753Srh87107 
2104753Srh87107 		if (mend <= sstart) {
2114753Srh87107 			/* This mapping is below the current section */
2124753Srh87107 			mp++;
2134753Srh87107 		} else if (mstart >= send) {
2144753Srh87107 			/* This mapping is above the current section */
2154753Srh87107 			i += 2;
2160Sstevel@tonic-gate 		} else {
2174753Srh87107 			/* This mapping overlaps the current section */
2184753Srh87107 			if (mp->map_file == NULL) {
2194753Srh87107 				dprintf("file_info_new: associating "
2204753Srh87107 				    "segment at %p\n",
2214753Srh87107 				    (void *)mp->map_pmap.pr_vaddr);
2224753Srh87107 				mp->map_file = fptr;
2234753Srh87107 				fptr->file_ref++;
2244753Srh87107 			} else {
2254753Srh87107 				dprintf("file_info_new: segment at %p "
2264753Srh87107 				    "already associated with %s\n",
2274753Srh87107 				    (void *)mp->map_pmap.pr_vaddr,
2284753Srh87107 				    (mp == mptr ? "this file" :
2294753Srh87107 				    mp->map_file->file_pname));
2304753Srh87107 			}
2310Sstevel@tonic-gate 			mp++;
2320Sstevel@tonic-gate 		}
2330Sstevel@tonic-gate 	}
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate 	return (fptr);
2360Sstevel@tonic-gate }
2370Sstevel@tonic-gate 
2380Sstevel@tonic-gate /*
2390Sstevel@tonic-gate  * Deallocation function for a file_info_t
2400Sstevel@tonic-gate  */
2410Sstevel@tonic-gate static void
file_info_free(struct ps_prochandle * P,file_info_t * fptr)2420Sstevel@tonic-gate file_info_free(struct ps_prochandle *P, file_info_t *fptr)
2430Sstevel@tonic-gate {
2440Sstevel@tonic-gate 	if (--fptr->file_ref == 0) {
2450Sstevel@tonic-gate 		list_unlink(fptr);
2460Sstevel@tonic-gate 		if (fptr->file_symtab.sym_elf) {
2470Sstevel@tonic-gate 			(void) elf_end(fptr->file_symtab.sym_elf);
2480Sstevel@tonic-gate 			free(fptr->file_symtab.sym_elfmem);
2490Sstevel@tonic-gate 		}
2500Sstevel@tonic-gate 		if (fptr->file_symtab.sym_byname)
2510Sstevel@tonic-gate 			free(fptr->file_symtab.sym_byname);
2520Sstevel@tonic-gate 		if (fptr->file_symtab.sym_byaddr)
2530Sstevel@tonic-gate 			free(fptr->file_symtab.sym_byaddr);
2540Sstevel@tonic-gate 
2550Sstevel@tonic-gate 		if (fptr->file_dynsym.sym_elf) {
2560Sstevel@tonic-gate 			(void) elf_end(fptr->file_dynsym.sym_elf);
2570Sstevel@tonic-gate 			free(fptr->file_dynsym.sym_elfmem);
2580Sstevel@tonic-gate 		}
2590Sstevel@tonic-gate 		if (fptr->file_dynsym.sym_byname)
2600Sstevel@tonic-gate 			free(fptr->file_dynsym.sym_byname);
2610Sstevel@tonic-gate 		if (fptr->file_dynsym.sym_byaddr)
2620Sstevel@tonic-gate 			free(fptr->file_dynsym.sym_byaddr);
2630Sstevel@tonic-gate 
2640Sstevel@tonic-gate 		if (fptr->file_lo)
2650Sstevel@tonic-gate 			free(fptr->file_lo);
2660Sstevel@tonic-gate 		if (fptr->file_lname)
2670Sstevel@tonic-gate 			free(fptr->file_lname);
2687675SEdward.Pilatowicz@Sun.COM 		if (fptr->file_rname)
2697675SEdward.Pilatowicz@Sun.COM 			free(fptr->file_rname);
2700Sstevel@tonic-gate 		if (fptr->file_elf)
2710Sstevel@tonic-gate 			(void) elf_end(fptr->file_elf);
2720Sstevel@tonic-gate 		if (fptr->file_elfmem != NULL)
2730Sstevel@tonic-gate 			free(fptr->file_elfmem);
2740Sstevel@tonic-gate 		if (fptr->file_fd >= 0)
2750Sstevel@tonic-gate 			(void) close(fptr->file_fd);
2760Sstevel@tonic-gate 		if (fptr->file_ctfp) {
2770Sstevel@tonic-gate 			ctf_close(fptr->file_ctfp);
2780Sstevel@tonic-gate 			free(fptr->file_ctf_buf);
2790Sstevel@tonic-gate 		}
2802915Srh87107 		if (fptr->file_saddrs)
2812915Srh87107 			free(fptr->file_saddrs);
2820Sstevel@tonic-gate 		free(fptr);
2830Sstevel@tonic-gate 		P->num_files--;
2840Sstevel@tonic-gate 	}
2850Sstevel@tonic-gate }
2860Sstevel@tonic-gate 
2870Sstevel@tonic-gate /*
2880Sstevel@tonic-gate  * Deallocation function for a map_info_t
2890Sstevel@tonic-gate  */
2900Sstevel@tonic-gate static void
map_info_free(struct ps_prochandle * P,map_info_t * mptr)2910Sstevel@tonic-gate map_info_free(struct ps_prochandle *P, map_info_t *mptr)
2920Sstevel@tonic-gate {
2930Sstevel@tonic-gate 	file_info_t *fptr;
2940Sstevel@tonic-gate 
2950Sstevel@tonic-gate 	if ((fptr = mptr->map_file) != NULL) {
2960Sstevel@tonic-gate 		if (fptr->file_map == mptr)
2970Sstevel@tonic-gate 			fptr->file_map = NULL;
2980Sstevel@tonic-gate 		file_info_free(P, fptr);
2990Sstevel@tonic-gate 	}
3000Sstevel@tonic-gate 	if (P->execname && mptr == P->map_exec) {
3010Sstevel@tonic-gate 		free(P->execname);
3020Sstevel@tonic-gate 		P->execname = NULL;
3030Sstevel@tonic-gate 	}
3040Sstevel@tonic-gate 	if (P->auxv && (mptr == P->map_exec || mptr == P->map_ldso)) {
3050Sstevel@tonic-gate 		free(P->auxv);
3060Sstevel@tonic-gate 		P->auxv = NULL;
3070Sstevel@tonic-gate 		P->nauxv = 0;
3080Sstevel@tonic-gate 	}
3090Sstevel@tonic-gate 	if (mptr == P->map_exec)
3100Sstevel@tonic-gate 		P->map_exec = NULL;
3110Sstevel@tonic-gate 	if (mptr == P->map_ldso)
3120Sstevel@tonic-gate 		P->map_ldso = NULL;
3130Sstevel@tonic-gate }
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate /*
3160Sstevel@tonic-gate  * Call-back function for librtld_db to iterate through all of its shared
3170Sstevel@tonic-gate  * libraries.  We use this to get the load object names for the mappings.
3180Sstevel@tonic-gate  */
3190Sstevel@tonic-gate static int
map_iter(const rd_loadobj_t * lop,void * cd)3200Sstevel@tonic-gate map_iter(const rd_loadobj_t *lop, void *cd)
3210Sstevel@tonic-gate {
3220Sstevel@tonic-gate 	char buf[PATH_MAX];
3230Sstevel@tonic-gate 	struct ps_prochandle *P = cd;
3240Sstevel@tonic-gate 	map_info_t *mptr;
3250Sstevel@tonic-gate 	file_info_t *fptr;
3260Sstevel@tonic-gate 
3270Sstevel@tonic-gate 	dprintf("encountered rd object at %p\n", (void *)lop->rl_base);
3280Sstevel@tonic-gate 
3292712Snn35248 	if ((mptr = Paddr2mptr(P, lop->rl_base)) == NULL) {
3302712Snn35248 		dprintf("map_iter: base address doesn't match any mapping\n");
3310Sstevel@tonic-gate 		return (1); /* Base address does not match any mapping */
3322712Snn35248 	}
3330Sstevel@tonic-gate 
3340Sstevel@tonic-gate 	if ((fptr = mptr->map_file) == NULL &&
3352712Snn35248 	    (fptr = file_info_new(P, mptr)) == NULL) {
3362712Snn35248 		dprintf("map_iter: failed to allocate a new file_info_t\n");
3370Sstevel@tonic-gate 		return (1); /* Failed to allocate a new file_info_t */
3382712Snn35248 	}
3390Sstevel@tonic-gate 
3400Sstevel@tonic-gate 	if ((fptr->file_lo == NULL) &&
3410Sstevel@tonic-gate 	    (fptr->file_lo = malloc(sizeof (rd_loadobj_t))) == NULL) {
3422712Snn35248 		dprintf("map_iter: failed to allocate rd_loadobj_t\n");
3430Sstevel@tonic-gate 		file_info_free(P, fptr);
3440Sstevel@tonic-gate 		return (1); /* Failed to allocate rd_loadobj_t */
3450Sstevel@tonic-gate 	}
3460Sstevel@tonic-gate 
3470Sstevel@tonic-gate 	fptr->file_map = mptr;
3480Sstevel@tonic-gate 	*fptr->file_lo = *lop;
3490Sstevel@tonic-gate 
3500Sstevel@tonic-gate 	fptr->file_lo->rl_plt_base = fptr->file_plt_base;
3510Sstevel@tonic-gate 	fptr->file_lo->rl_plt_size = fptr->file_plt_size;
3520Sstevel@tonic-gate 
3530Sstevel@tonic-gate 	if (fptr->file_lname) {
3540Sstevel@tonic-gate 		free(fptr->file_lname);
3550Sstevel@tonic-gate 		fptr->file_lname = NULL;
3567675SEdward.Pilatowicz@Sun.COM 		fptr->file_lbase = NULL;
3577675SEdward.Pilatowicz@Sun.COM 	}
3587675SEdward.Pilatowicz@Sun.COM 	if (fptr->file_rname) {
3597675SEdward.Pilatowicz@Sun.COM 		free(fptr->file_rname);
3607675SEdward.Pilatowicz@Sun.COM 		fptr->file_rname = NULL;
3617675SEdward.Pilatowicz@Sun.COM 		fptr->file_rbase = NULL;
3620Sstevel@tonic-gate 	}
3630Sstevel@tonic-gate 
3640Sstevel@tonic-gate 	if (Pread_string(P, buf, sizeof (buf), lop->rl_nameaddr) > 0) {
3650Sstevel@tonic-gate 		if ((fptr->file_lname = strdup(buf)) != NULL)
3660Sstevel@tonic-gate 			fptr->file_lbase = basename(fptr->file_lname);
3672712Snn35248 	} else {
3682712Snn35248 		dprintf("map_iter: failed to read string at %p\n",
3692712Snn35248 		    (void *)lop->rl_nameaddr);
3700Sstevel@tonic-gate 	}
3710Sstevel@tonic-gate 
3727675SEdward.Pilatowicz@Sun.COM 	if ((Pfindmap(P, mptr, buf, sizeof (buf)) != NULL) &&
3737675SEdward.Pilatowicz@Sun.COM 	    ((fptr->file_rname = strdup(buf)) != NULL))
3747675SEdward.Pilatowicz@Sun.COM 		fptr->file_rbase = basename(fptr->file_rname);
3757675SEdward.Pilatowicz@Sun.COM 
3760Sstevel@tonic-gate 	dprintf("loaded rd object %s lmid %lx\n",
3777675SEdward.Pilatowicz@Sun.COM 	    fptr->file_lname ? buf : "<NULL>", lop->rl_lmident);
3780Sstevel@tonic-gate 	return (1);
3790Sstevel@tonic-gate }
3800Sstevel@tonic-gate 
3810Sstevel@tonic-gate static void
map_set(struct ps_prochandle * P,map_info_t * mptr,const char * lname)3820Sstevel@tonic-gate map_set(struct ps_prochandle *P, map_info_t *mptr, const char *lname)
3830Sstevel@tonic-gate {
3840Sstevel@tonic-gate 	file_info_t *fptr;
3857926SEdward.Pilatowicz@Sun.COM 	char buf[PATH_MAX];
3860Sstevel@tonic-gate 
3870Sstevel@tonic-gate 	if ((fptr = mptr->map_file) == NULL &&
3880Sstevel@tonic-gate 	    (fptr = file_info_new(P, mptr)) == NULL)
3890Sstevel@tonic-gate 		return; /* Failed to allocate a new file_info_t */
3900Sstevel@tonic-gate 
3910Sstevel@tonic-gate 	fptr->file_map = mptr;
3920Sstevel@tonic-gate 
3930Sstevel@tonic-gate 	if ((fptr->file_lo == NULL) &&
3940Sstevel@tonic-gate 	    (fptr->file_lo = malloc(sizeof (rd_loadobj_t))) == NULL) {
3950Sstevel@tonic-gate 		file_info_free(P, fptr);
3960Sstevel@tonic-gate 		return; /* Failed to allocate rd_loadobj_t */
3970Sstevel@tonic-gate 	}
3980Sstevel@tonic-gate 
3990Sstevel@tonic-gate 	(void) memset(fptr->file_lo, 0, sizeof (rd_loadobj_t));
4000Sstevel@tonic-gate 	fptr->file_lo->rl_base = mptr->map_pmap.pr_vaddr;
4010Sstevel@tonic-gate 	fptr->file_lo->rl_bend =
4022712Snn35248 	    mptr->map_pmap.pr_vaddr + mptr->map_pmap.pr_size;
4030Sstevel@tonic-gate 
4040Sstevel@tonic-gate 	fptr->file_lo->rl_plt_base = fptr->file_plt_base;
4050Sstevel@tonic-gate 	fptr->file_lo->rl_plt_size = fptr->file_plt_size;
4060Sstevel@tonic-gate 
4077675SEdward.Pilatowicz@Sun.COM 	if ((fptr->file_lname == NULL) &&
4082712Snn35248 	    (fptr->file_lname = strdup(lname)) != NULL)
4090Sstevel@tonic-gate 		fptr->file_lbase = basename(fptr->file_lname);
4107675SEdward.Pilatowicz@Sun.COM 
4117926SEdward.Pilatowicz@Sun.COM 	if ((Pfindmap(P, mptr, buf, sizeof (buf)) != NULL) &&
4127926SEdward.Pilatowicz@Sun.COM 	    ((fptr->file_rname = strdup(buf)) != NULL))
4137926SEdward.Pilatowicz@Sun.COM 		fptr->file_rbase = basename(fptr->file_rname);
4140Sstevel@tonic-gate }
4150Sstevel@tonic-gate 
4160Sstevel@tonic-gate static void
load_static_maps(struct ps_prochandle * P)4170Sstevel@tonic-gate load_static_maps(struct ps_prochandle *P)
4180Sstevel@tonic-gate {
4190Sstevel@tonic-gate 	map_info_t *mptr;
4200Sstevel@tonic-gate 
4210Sstevel@tonic-gate 	/*
4220Sstevel@tonic-gate 	 * Construct the map for the a.out.
4230Sstevel@tonic-gate 	 */
4240Sstevel@tonic-gate 	if ((mptr = object_name_to_map(P, PR_LMID_EVERY, PR_OBJ_EXEC)) != NULL)
4250Sstevel@tonic-gate 		map_set(P, mptr, "a.out");
4260Sstevel@tonic-gate 
4270Sstevel@tonic-gate 	/*
4280Sstevel@tonic-gate 	 * If the dynamic linker exists for this process,
4290Sstevel@tonic-gate 	 * construct the map for it.
4300Sstevel@tonic-gate 	 */
4310Sstevel@tonic-gate 	if (Pgetauxval(P, AT_BASE) != -1L &&
4320Sstevel@tonic-gate 	    (mptr = object_name_to_map(P, PR_LMID_EVERY, PR_OBJ_LDSO)) != NULL)
4330Sstevel@tonic-gate 		map_set(P, mptr, "ld.so.1");
4340Sstevel@tonic-gate }
4350Sstevel@tonic-gate 
4360Sstevel@tonic-gate /*
4370Sstevel@tonic-gate  * Go through all the address space mappings, validating or updating
4380Sstevel@tonic-gate  * the information already gathered, or gathering new information.
4390Sstevel@tonic-gate  *
4400Sstevel@tonic-gate  * This function is only called when we suspect that the mappings have changed
4410Sstevel@tonic-gate  * because this is the first time we're calling it or because of rtld activity.
4420Sstevel@tonic-gate  */
4430Sstevel@tonic-gate void
Pupdate_maps(struct ps_prochandle * P)4440Sstevel@tonic-gate Pupdate_maps(struct ps_prochandle *P)
4450Sstevel@tonic-gate {
4462712Snn35248 	char mapfile[PATH_MAX];
4470Sstevel@tonic-gate 	int mapfd;
4480Sstevel@tonic-gate 	struct stat statb;
4490Sstevel@tonic-gate 	prmap_t *Pmap = NULL;
4500Sstevel@tonic-gate 	prmap_t *pmap;
4510Sstevel@tonic-gate 	ssize_t nmap;
4520Sstevel@tonic-gate 	int i;
4530Sstevel@tonic-gate 	uint_t oldmapcount;
4540Sstevel@tonic-gate 	map_info_t *newmap, *newp;
4550Sstevel@tonic-gate 	map_info_t *mptr;
4560Sstevel@tonic-gate 
457457Sbmc 	if (P->info_valid || P->state == PS_UNDEAD)
4580Sstevel@tonic-gate 		return;
4590Sstevel@tonic-gate 
4600Sstevel@tonic-gate 	Preadauxvec(P);
4610Sstevel@tonic-gate 
4622712Snn35248 	(void) snprintf(mapfile, sizeof (mapfile), "%s/%d/map",
4632712Snn35248 	    procfs_path, (int)P->pid);
4640Sstevel@tonic-gate 	if ((mapfd = open(mapfile, O_RDONLY)) < 0 ||
4650Sstevel@tonic-gate 	    fstat(mapfd, &statb) != 0 ||
4660Sstevel@tonic-gate 	    statb.st_size < sizeof (prmap_t) ||
4670Sstevel@tonic-gate 	    (Pmap = malloc(statb.st_size)) == NULL ||
4680Sstevel@tonic-gate 	    (nmap = pread(mapfd, Pmap, statb.st_size, 0L)) <= 0 ||
4690Sstevel@tonic-gate 	    (nmap /= sizeof (prmap_t)) == 0) {
4700Sstevel@tonic-gate 		if (Pmap != NULL)
4710Sstevel@tonic-gate 			free(Pmap);
4720Sstevel@tonic-gate 		if (mapfd >= 0)
4730Sstevel@tonic-gate 			(void) close(mapfd);
4740Sstevel@tonic-gate 		Preset_maps(P);	/* utter failure; destroy tables */
4750Sstevel@tonic-gate 		return;
4760Sstevel@tonic-gate 	}
4770Sstevel@tonic-gate 	(void) close(mapfd);
4780Sstevel@tonic-gate 
4790Sstevel@tonic-gate 	if ((newmap = calloc(1, nmap * sizeof (map_info_t))) == NULL)
4800Sstevel@tonic-gate 		return;
4810Sstevel@tonic-gate 
4820Sstevel@tonic-gate 	/*
4830Sstevel@tonic-gate 	 * We try to merge any file information we may have for existing
4840Sstevel@tonic-gate 	 * mappings, to avoid having to rebuild the file info.
4850Sstevel@tonic-gate 	 */
4860Sstevel@tonic-gate 	mptr = P->mappings;
4870Sstevel@tonic-gate 	pmap = Pmap;
4880Sstevel@tonic-gate 	newp = newmap;
4890Sstevel@tonic-gate 	oldmapcount = P->map_count;
4900Sstevel@tonic-gate 	for (i = 0; i < nmap; i++, pmap++, newp++) {
4910Sstevel@tonic-gate 
4920Sstevel@tonic-gate 		if (oldmapcount == 0) {
4930Sstevel@tonic-gate 			/*
4940Sstevel@tonic-gate 			 * We've exhausted all the old mappings.  Every new
4950Sstevel@tonic-gate 			 * mapping should be added.
4960Sstevel@tonic-gate 			 */
4970Sstevel@tonic-gate 			newp->map_pmap = *pmap;
4980Sstevel@tonic-gate 
4990Sstevel@tonic-gate 		} else if (pmap->pr_vaddr == mptr->map_pmap.pr_vaddr &&
5000Sstevel@tonic-gate 		    pmap->pr_size == mptr->map_pmap.pr_size &&
5010Sstevel@tonic-gate 		    pmap->pr_offset == mptr->map_pmap.pr_offset &&
5020Sstevel@tonic-gate 		    (pmap->pr_mflags & ~(MA_BREAK | MA_STACK)) ==
5030Sstevel@tonic-gate 		    (mptr->map_pmap.pr_mflags & ~(MA_BREAK | MA_STACK)) &&
5040Sstevel@tonic-gate 		    pmap->pr_pagesize == mptr->map_pmap.pr_pagesize &&
5050Sstevel@tonic-gate 		    pmap->pr_shmid == mptr->map_pmap.pr_shmid &&
5060Sstevel@tonic-gate 		    strcmp(pmap->pr_mapname, mptr->map_pmap.pr_mapname) == 0) {
5070Sstevel@tonic-gate 
5080Sstevel@tonic-gate 			/*
5090Sstevel@tonic-gate 			 * This mapping matches exactly.  Copy over the old
5100Sstevel@tonic-gate 			 * mapping, taking care to get the latest flags.
5110Sstevel@tonic-gate 			 * Make sure the associated file_info_t is updated
5120Sstevel@tonic-gate 			 * appropriately.
5130Sstevel@tonic-gate 			 */
5140Sstevel@tonic-gate 			*newp = *mptr;
5150Sstevel@tonic-gate 			if (P->map_exec == mptr)
5160Sstevel@tonic-gate 				P->map_exec = newp;
5170Sstevel@tonic-gate 			if (P->map_ldso == mptr)
5180Sstevel@tonic-gate 				P->map_ldso = newp;
5190Sstevel@tonic-gate 			newp->map_pmap.pr_mflags = pmap->pr_mflags;
5200Sstevel@tonic-gate 			if (mptr->map_file != NULL &&
5210Sstevel@tonic-gate 			    mptr->map_file->file_map == mptr)
5220Sstevel@tonic-gate 				mptr->map_file->file_map = newp;
5230Sstevel@tonic-gate 			oldmapcount--;
5240Sstevel@tonic-gate 			mptr++;
5250Sstevel@tonic-gate 
5260Sstevel@tonic-gate 		} else if (pmap->pr_vaddr + pmap->pr_size >
5270Sstevel@tonic-gate 		    mptr->map_pmap.pr_vaddr) {
5280Sstevel@tonic-gate 
5290Sstevel@tonic-gate 			/*
5300Sstevel@tonic-gate 			 * The old mapping doesn't exist any more, remove it
5310Sstevel@tonic-gate 			 * from the list.
5320Sstevel@tonic-gate 			 */
5330Sstevel@tonic-gate 			map_info_free(P, mptr);
5340Sstevel@tonic-gate 			oldmapcount--;
5350Sstevel@tonic-gate 			i--;
5360Sstevel@tonic-gate 			newp--;
5370Sstevel@tonic-gate 			pmap--;
5380Sstevel@tonic-gate 			mptr++;
5390Sstevel@tonic-gate 
5400Sstevel@tonic-gate 		} else {
5410Sstevel@tonic-gate 
5420Sstevel@tonic-gate 			/*
5430Sstevel@tonic-gate 			 * This is a new mapping, add it directly.
5440Sstevel@tonic-gate 			 */
5450Sstevel@tonic-gate 			newp->map_pmap = *pmap;
5460Sstevel@tonic-gate 		}
5470Sstevel@tonic-gate 	}
5480Sstevel@tonic-gate 
5490Sstevel@tonic-gate 	/*
5500Sstevel@tonic-gate 	 * Free any old maps
5510Sstevel@tonic-gate 	 */
5520Sstevel@tonic-gate 	while (oldmapcount) {
5530Sstevel@tonic-gate 		map_info_free(P, mptr);
5540Sstevel@tonic-gate 		oldmapcount--;
5550Sstevel@tonic-gate 		mptr++;
5560Sstevel@tonic-gate 	}
5570Sstevel@tonic-gate 
5580Sstevel@tonic-gate 	free(Pmap);
5590Sstevel@tonic-gate 	if (P->mappings != NULL)
5600Sstevel@tonic-gate 		free(P->mappings);
5610Sstevel@tonic-gate 	P->mappings = newmap;
5620Sstevel@tonic-gate 	P->map_count = P->map_alloc = nmap;
5630Sstevel@tonic-gate 	P->info_valid = 1;
5640Sstevel@tonic-gate 
5650Sstevel@tonic-gate 	/*
5660Sstevel@tonic-gate 	 * Consult librtld_db to get the load object
5670Sstevel@tonic-gate 	 * names for all of the shared libraries.
5680Sstevel@tonic-gate 	 */
5690Sstevel@tonic-gate 	if (P->rap != NULL)
5700Sstevel@tonic-gate 		(void) rd_loadobj_iter(P->rap, map_iter, P);
5710Sstevel@tonic-gate }
5720Sstevel@tonic-gate 
5730Sstevel@tonic-gate /*
5740Sstevel@tonic-gate  * Update all of the mappings and rtld_db as if by Pupdate_maps(), and then
5750Sstevel@tonic-gate  * forcibly cache all of the symbol tables associated with all object files.
5760Sstevel@tonic-gate  */
5770Sstevel@tonic-gate void
Pupdate_syms(struct ps_prochandle * P)5780Sstevel@tonic-gate Pupdate_syms(struct ps_prochandle *P)
5790Sstevel@tonic-gate {
5807156Srh87107 	file_info_t *fptr;
5810Sstevel@tonic-gate 	int i;
5820Sstevel@tonic-gate 
5830Sstevel@tonic-gate 	Pupdate_maps(P);
5840Sstevel@tonic-gate 
5857156Srh87107 	for (i = 0, fptr = list_next(&P->file_head); i < P->num_files;
5867156Srh87107 	    i++, fptr = list_next(fptr)) {
5870Sstevel@tonic-gate 		Pbuild_file_symtab(P, fptr);
5880Sstevel@tonic-gate 		(void) Pbuild_file_ctf(P, fptr);
5890Sstevel@tonic-gate 	}
5900Sstevel@tonic-gate }
5910Sstevel@tonic-gate 
5920Sstevel@tonic-gate /*
5930Sstevel@tonic-gate  * Return the librtld_db agent handle for the victim process.
5940Sstevel@tonic-gate  * The handle will become invalid at the next successful exec() and the
5950Sstevel@tonic-gate  * client (caller of proc_rd_agent()) must not use it beyond that point.
5960Sstevel@tonic-gate  * If the process is already dead, we've already tried our best to
5970Sstevel@tonic-gate  * create the agent during core file initialization.
5980Sstevel@tonic-gate  */
5990Sstevel@tonic-gate rd_agent_t *
Prd_agent(struct ps_prochandle * P)6000Sstevel@tonic-gate Prd_agent(struct ps_prochandle *P)
6010Sstevel@tonic-gate {
6020Sstevel@tonic-gate 	if (P->rap == NULL && P->state != PS_DEAD && P->state != PS_IDLE) {
6030Sstevel@tonic-gate 		Pupdate_maps(P);
6040Sstevel@tonic-gate 		if (P->num_files == 0)
6050Sstevel@tonic-gate 			load_static_maps(P);
6060Sstevel@tonic-gate 		rd_log(_libproc_debug);
6070Sstevel@tonic-gate 		if ((P->rap = rd_new(P)) != NULL)
6080Sstevel@tonic-gate 			(void) rd_loadobj_iter(P->rap, map_iter, P);
6090Sstevel@tonic-gate 	}
6100Sstevel@tonic-gate 	return (P->rap);
6110Sstevel@tonic-gate }
6120Sstevel@tonic-gate 
6130Sstevel@tonic-gate /*
6140Sstevel@tonic-gate  * Return the prmap_t structure containing 'addr', but only if it
6150Sstevel@tonic-gate  * is in the dynamic linker's link map and is the text section.
6160Sstevel@tonic-gate  */
6170Sstevel@tonic-gate const prmap_t *
Paddr_to_text_map(struct ps_prochandle * P,uintptr_t addr)6180Sstevel@tonic-gate Paddr_to_text_map(struct ps_prochandle *P, uintptr_t addr)
6190Sstevel@tonic-gate {
6200Sstevel@tonic-gate 	map_info_t *mptr;
6210Sstevel@tonic-gate 
6220Sstevel@tonic-gate 	if (!P->info_valid)
6230Sstevel@tonic-gate 		Pupdate_maps(P);
6240Sstevel@tonic-gate 
6250Sstevel@tonic-gate 	if ((mptr = Paddr2mptr(P, addr)) != NULL) {
6260Sstevel@tonic-gate 		file_info_t *fptr = build_map_symtab(P, mptr);
6270Sstevel@tonic-gate 		const prmap_t *pmp = &mptr->map_pmap;
6280Sstevel@tonic-gate 
6294753Srh87107 		/*
6304753Srh87107 		 * Assume that if rl_data_base is NULL, it means that no
6314753Srh87107 		 * data section was found for this load object, and that
6324753Srh87107 		 * a section must be text. Otherwise, a section will be
6334753Srh87107 		 * text unless it ends above the start of the data
6344753Srh87107 		 * section.
6354753Srh87107 		 */
6360Sstevel@tonic-gate 		if (fptr != NULL && fptr->file_lo != NULL &&
6374753Srh87107 		    (fptr->file_lo->rl_data_base == NULL ||
638*12968SRobert.Harris@Sun.COM 		    pmp->pr_vaddr + pmp->pr_size <=
6394753Srh87107 		    fptr->file_lo->rl_data_base))
6400Sstevel@tonic-gate 			return (pmp);
6410Sstevel@tonic-gate 	}
6420Sstevel@tonic-gate 
6430Sstevel@tonic-gate 	return (NULL);
6440Sstevel@tonic-gate }
6450Sstevel@tonic-gate 
6460Sstevel@tonic-gate /*
6470Sstevel@tonic-gate  * Return the prmap_t structure containing 'addr' (no restrictions on
6480Sstevel@tonic-gate  * the type of mapping).
6490Sstevel@tonic-gate  */
6500Sstevel@tonic-gate const prmap_t *
Paddr_to_map(struct ps_prochandle * P,uintptr_t addr)6510Sstevel@tonic-gate Paddr_to_map(struct ps_prochandle *P, uintptr_t addr)
6520Sstevel@tonic-gate {
6530Sstevel@tonic-gate 	map_info_t *mptr;
6540Sstevel@tonic-gate 
6550Sstevel@tonic-gate 	if (!P->info_valid)
6560Sstevel@tonic-gate 		Pupdate_maps(P);
6570Sstevel@tonic-gate 
6580Sstevel@tonic-gate 	if ((mptr = Paddr2mptr(P, addr)) != NULL)
6590Sstevel@tonic-gate 		return (&mptr->map_pmap);
6600Sstevel@tonic-gate 
6610Sstevel@tonic-gate 	return (NULL);
6620Sstevel@tonic-gate }
6630Sstevel@tonic-gate 
6640Sstevel@tonic-gate /*
6650Sstevel@tonic-gate  * Convert a full or partial load object name to the prmap_t for its
6660Sstevel@tonic-gate  * corresponding primary text mapping.
6670Sstevel@tonic-gate  */
6680Sstevel@tonic-gate const prmap_t *
Plmid_to_map(struct ps_prochandle * P,Lmid_t lmid,const char * name)6690Sstevel@tonic-gate Plmid_to_map(struct ps_prochandle *P, Lmid_t lmid, const char *name)
6700Sstevel@tonic-gate {
6710Sstevel@tonic-gate 	map_info_t *mptr;
6720Sstevel@tonic-gate 
6730Sstevel@tonic-gate 	if (name == PR_OBJ_EVERY)
6740Sstevel@tonic-gate 		return (NULL); /* A reasonable mistake */
6750Sstevel@tonic-gate 
6760Sstevel@tonic-gate 	if ((mptr = object_name_to_map(P, lmid, name)) != NULL)
6770Sstevel@tonic-gate 		return (&mptr->map_pmap);
6780Sstevel@tonic-gate 
6790Sstevel@tonic-gate 	return (NULL);
6800Sstevel@tonic-gate }
6810Sstevel@tonic-gate 
6820Sstevel@tonic-gate const prmap_t *
Pname_to_map(struct ps_prochandle * P,const char * name)6830Sstevel@tonic-gate Pname_to_map(struct ps_prochandle *P, const char *name)
6840Sstevel@tonic-gate {
6850Sstevel@tonic-gate 	return (Plmid_to_map(P, PR_LMID_EVERY, name));
6860Sstevel@tonic-gate }
6870Sstevel@tonic-gate 
6880Sstevel@tonic-gate const rd_loadobj_t *
Paddr_to_loadobj(struct ps_prochandle * P,uintptr_t addr)6890Sstevel@tonic-gate Paddr_to_loadobj(struct ps_prochandle *P, uintptr_t addr)
6900Sstevel@tonic-gate {
6910Sstevel@tonic-gate 	map_info_t *mptr;
6920Sstevel@tonic-gate 
6930Sstevel@tonic-gate 	if (!P->info_valid)
6940Sstevel@tonic-gate 		Pupdate_maps(P);
6950Sstevel@tonic-gate 
6960Sstevel@tonic-gate 	if ((mptr = Paddr2mptr(P, addr)) == NULL)
6970Sstevel@tonic-gate 		return (NULL);
6980Sstevel@tonic-gate 
6990Sstevel@tonic-gate 	/*
7000Sstevel@tonic-gate 	 * By building the symbol table, we implicitly bring the PLT
7010Sstevel@tonic-gate 	 * information up to date in the load object.
7020Sstevel@tonic-gate 	 */
7030Sstevel@tonic-gate 	(void) build_map_symtab(P, mptr);
7040Sstevel@tonic-gate 
7050Sstevel@tonic-gate 	return (mptr->map_file->file_lo);
7060Sstevel@tonic-gate }
7070Sstevel@tonic-gate 
7080Sstevel@tonic-gate const rd_loadobj_t *
Plmid_to_loadobj(struct ps_prochandle * P,Lmid_t lmid,const char * name)7090Sstevel@tonic-gate Plmid_to_loadobj(struct ps_prochandle *P, Lmid_t lmid, const char *name)
7100Sstevel@tonic-gate {
7110Sstevel@tonic-gate 	map_info_t *mptr;
7120Sstevel@tonic-gate 
7130Sstevel@tonic-gate 	if (name == PR_OBJ_EVERY)
7140Sstevel@tonic-gate 		return (NULL);
7150Sstevel@tonic-gate 
7160Sstevel@tonic-gate 	if ((mptr = object_name_to_map(P, lmid, name)) == NULL)
7170Sstevel@tonic-gate 		return (NULL);
7180Sstevel@tonic-gate 
7190Sstevel@tonic-gate 	/*
7200Sstevel@tonic-gate 	 * By building the symbol table, we implicitly bring the PLT
7210Sstevel@tonic-gate 	 * information up to date in the load object.
7220Sstevel@tonic-gate 	 */
7230Sstevel@tonic-gate 	(void) build_map_symtab(P, mptr);
7240Sstevel@tonic-gate 
7250Sstevel@tonic-gate 	return (mptr->map_file->file_lo);
7260Sstevel@tonic-gate }
7270Sstevel@tonic-gate 
7280Sstevel@tonic-gate const rd_loadobj_t *
Pname_to_loadobj(struct ps_prochandle * P,const char * name)7290Sstevel@tonic-gate Pname_to_loadobj(struct ps_prochandle *P, const char *name)
7300Sstevel@tonic-gate {
7310Sstevel@tonic-gate 	return (Plmid_to_loadobj(P, PR_LMID_EVERY, name));
7320Sstevel@tonic-gate }
7330Sstevel@tonic-gate 
7340Sstevel@tonic-gate ctf_file_t *
Pbuild_file_ctf(struct ps_prochandle * P,file_info_t * fptr)7350Sstevel@tonic-gate Pbuild_file_ctf(struct ps_prochandle *P, file_info_t *fptr)
7360Sstevel@tonic-gate {
7370Sstevel@tonic-gate 	ctf_sect_t ctdata, symtab, strtab;
7380Sstevel@tonic-gate 	sym_tbl_t *symp;
7390Sstevel@tonic-gate 	int err;
7400Sstevel@tonic-gate 
7410Sstevel@tonic-gate 	if (fptr->file_ctfp != NULL)
7420Sstevel@tonic-gate 		return (fptr->file_ctfp);
7430Sstevel@tonic-gate 
7440Sstevel@tonic-gate 	Pbuild_file_symtab(P, fptr);
7450Sstevel@tonic-gate 
7460Sstevel@tonic-gate 	if (fptr->file_ctf_size == 0)
7470Sstevel@tonic-gate 		return (NULL);
7480Sstevel@tonic-gate 
7490Sstevel@tonic-gate 	symp = fptr->file_ctf_dyn ? &fptr->file_dynsym : &fptr->file_symtab;
7503347Sab196087 	if (symp->sym_data_pri == NULL)
7510Sstevel@tonic-gate 		return (NULL);
7520Sstevel@tonic-gate 
7530Sstevel@tonic-gate 	/*
7540Sstevel@tonic-gate 	 * The buffer may alread be allocated if this is a core file that
7550Sstevel@tonic-gate 	 * contained CTF data for this file.
7560Sstevel@tonic-gate 	 */
7570Sstevel@tonic-gate 	if (fptr->file_ctf_buf == NULL) {
7580Sstevel@tonic-gate 		fptr->file_ctf_buf = malloc(fptr->file_ctf_size);
7590Sstevel@tonic-gate 		if (fptr->file_ctf_buf == NULL) {
7600Sstevel@tonic-gate 			dprintf("failed to allocate ctf buffer\n");
7610Sstevel@tonic-gate 			return (NULL);
7620Sstevel@tonic-gate 		}
7630Sstevel@tonic-gate 
7640Sstevel@tonic-gate 		if (pread(fptr->file_fd, fptr->file_ctf_buf,
7650Sstevel@tonic-gate 		    fptr->file_ctf_size, fptr->file_ctf_off) !=
7660Sstevel@tonic-gate 		    fptr->file_ctf_size) {
7670Sstevel@tonic-gate 			free(fptr->file_ctf_buf);
7680Sstevel@tonic-gate 			fptr->file_ctf_buf = NULL;
7690Sstevel@tonic-gate 			dprintf("failed to read ctf data\n");
7700Sstevel@tonic-gate 			return (NULL);
7710Sstevel@tonic-gate 		}
7720Sstevel@tonic-gate 	}
7730Sstevel@tonic-gate 
7740Sstevel@tonic-gate 	ctdata.cts_name = ".SUNW_ctf";
7750Sstevel@tonic-gate 	ctdata.cts_type = SHT_PROGBITS;
7760Sstevel@tonic-gate 	ctdata.cts_flags = 0;
7770Sstevel@tonic-gate 	ctdata.cts_data = fptr->file_ctf_buf;
7780Sstevel@tonic-gate 	ctdata.cts_size = fptr->file_ctf_size;
7790Sstevel@tonic-gate 	ctdata.cts_entsize = 1;
7800Sstevel@tonic-gate 	ctdata.cts_offset = 0;
7810Sstevel@tonic-gate 
7820Sstevel@tonic-gate 	symtab.cts_name = fptr->file_ctf_dyn ? ".dynsym" : ".symtab";
7833347Sab196087 	symtab.cts_type = symp->sym_hdr_pri.sh_type;
7843347Sab196087 	symtab.cts_flags = symp->sym_hdr_pri.sh_flags;
7853347Sab196087 	symtab.cts_data = symp->sym_data_pri->d_buf;
7863347Sab196087 	symtab.cts_size = symp->sym_hdr_pri.sh_size;
7873347Sab196087 	symtab.cts_entsize = symp->sym_hdr_pri.sh_entsize;
7883347Sab196087 	symtab.cts_offset = symp->sym_hdr_pri.sh_offset;
7890Sstevel@tonic-gate 
7900Sstevel@tonic-gate 	strtab.cts_name = fptr->file_ctf_dyn ? ".dynstr" : ".strtab";
7910Sstevel@tonic-gate 	strtab.cts_type = symp->sym_strhdr.sh_type;
7920Sstevel@tonic-gate 	strtab.cts_flags = symp->sym_strhdr.sh_flags;
7930Sstevel@tonic-gate 	strtab.cts_data = symp->sym_strs;
7940Sstevel@tonic-gate 	strtab.cts_size = symp->sym_strhdr.sh_size;
7950Sstevel@tonic-gate 	strtab.cts_entsize = symp->sym_strhdr.sh_entsize;
7960Sstevel@tonic-gate 	strtab.cts_offset = symp->sym_strhdr.sh_offset;
7970Sstevel@tonic-gate 
7980Sstevel@tonic-gate 	fptr->file_ctfp = ctf_bufopen(&ctdata, &symtab, &strtab, &err);
7990Sstevel@tonic-gate 	if (fptr->file_ctfp == NULL) {
8009504SKrishnendu.Sadhukhan@Sun.COM 		dprintf("ctf_bufopen() failed, error code %d\n", err);
8010Sstevel@tonic-gate 		free(fptr->file_ctf_buf);
8020Sstevel@tonic-gate 		fptr->file_ctf_buf = NULL;
8030Sstevel@tonic-gate 		return (NULL);
8040Sstevel@tonic-gate 	}
8050Sstevel@tonic-gate 
8060Sstevel@tonic-gate 	dprintf("loaded %lu bytes of CTF data for %s\n",
8070Sstevel@tonic-gate 	    (ulong_t)fptr->file_ctf_size, fptr->file_pname);
8080Sstevel@tonic-gate 
8090Sstevel@tonic-gate 	return (fptr->file_ctfp);
8100Sstevel@tonic-gate }
8110Sstevel@tonic-gate 
8120Sstevel@tonic-gate ctf_file_t *
Paddr_to_ctf(struct ps_prochandle * P,uintptr_t addr)8130Sstevel@tonic-gate Paddr_to_ctf(struct ps_prochandle *P, uintptr_t addr)
8140Sstevel@tonic-gate {
8150Sstevel@tonic-gate 	map_info_t *mptr;
8160Sstevel@tonic-gate 	file_info_t *fptr;
8170Sstevel@tonic-gate 
8180Sstevel@tonic-gate 	if (!P->info_valid)
8190Sstevel@tonic-gate 		Pupdate_maps(P);
8200Sstevel@tonic-gate 
8210Sstevel@tonic-gate 	if ((mptr = Paddr2mptr(P, addr)) == NULL ||
8220Sstevel@tonic-gate 	    (fptr = mptr->map_file) == NULL)
8230Sstevel@tonic-gate 		return (NULL);
8240Sstevel@tonic-gate 
8250Sstevel@tonic-gate 	return (Pbuild_file_ctf(P, fptr));
8260Sstevel@tonic-gate }
8270Sstevel@tonic-gate 
8280Sstevel@tonic-gate ctf_file_t *
Plmid_to_ctf(struct ps_prochandle * P,Lmid_t lmid,const char * name)8290Sstevel@tonic-gate Plmid_to_ctf(struct ps_prochandle *P, Lmid_t lmid, const char *name)
8300Sstevel@tonic-gate {
8310Sstevel@tonic-gate 	map_info_t *mptr;
8320Sstevel@tonic-gate 	file_info_t *fptr;
8330Sstevel@tonic-gate 
8340Sstevel@tonic-gate 	if (name == PR_OBJ_EVERY)
8350Sstevel@tonic-gate 		return (NULL);
8360Sstevel@tonic-gate 
8370Sstevel@tonic-gate 	if ((mptr = object_name_to_map(P, lmid, name)) == NULL ||
8380Sstevel@tonic-gate 	    (fptr = mptr->map_file) == NULL)
8390Sstevel@tonic-gate 		return (NULL);
8400Sstevel@tonic-gate 
8410Sstevel@tonic-gate 	return (Pbuild_file_ctf(P, fptr));
8420Sstevel@tonic-gate }
8430Sstevel@tonic-gate 
8440Sstevel@tonic-gate ctf_file_t *
Pname_to_ctf(struct ps_prochandle * P,const char * name)8450Sstevel@tonic-gate Pname_to_ctf(struct ps_prochandle *P, const char *name)
8460Sstevel@tonic-gate {
8470Sstevel@tonic-gate 	return (Plmid_to_ctf(P, PR_LMID_EVERY, name));
8480Sstevel@tonic-gate }
8490Sstevel@tonic-gate 
8500Sstevel@tonic-gate /*
8510Sstevel@tonic-gate  * If we're not a core file, re-read the /proc/<pid>/auxv file and store
8520Sstevel@tonic-gate  * its contents in P->auxv.  In the case of a core file, we either
8530Sstevel@tonic-gate  * initialized P->auxv in Pcore() from the NT_AUXV, or we don't have an
8540Sstevel@tonic-gate  * auxv because the note was missing.
8550Sstevel@tonic-gate  */
8560Sstevel@tonic-gate void
Preadauxvec(struct ps_prochandle * P)8570Sstevel@tonic-gate Preadauxvec(struct ps_prochandle *P)
8580Sstevel@tonic-gate {
8590Sstevel@tonic-gate 	char auxfile[64];
8600Sstevel@tonic-gate 	struct stat statb;
8610Sstevel@tonic-gate 	ssize_t naux;
8620Sstevel@tonic-gate 	int fd;
8630Sstevel@tonic-gate 
8640Sstevel@tonic-gate 	if (P->state == PS_DEAD)
8650Sstevel@tonic-gate 		return; /* Already read during Pgrab_core() */
8660Sstevel@tonic-gate 	if (P->state == PS_IDLE)
8670Sstevel@tonic-gate 		return; /* No aux vec for Pgrab_file() */
8680Sstevel@tonic-gate 
8690Sstevel@tonic-gate 	if (P->auxv != NULL) {
8700Sstevel@tonic-gate 		free(P->auxv);
8710Sstevel@tonic-gate 		P->auxv = NULL;
8720Sstevel@tonic-gate 		P->nauxv = 0;
8730Sstevel@tonic-gate 	}
8740Sstevel@tonic-gate 
8752712Snn35248 	(void) snprintf(auxfile, sizeof (auxfile), "%s/%d/auxv",
8762712Snn35248 	    procfs_path, (int)P->pid);
8770Sstevel@tonic-gate 	if ((fd = open(auxfile, O_RDONLY)) < 0)
8780Sstevel@tonic-gate 		return;
8790Sstevel@tonic-gate 
8800Sstevel@tonic-gate 	if (fstat(fd, &statb) == 0 &&
8810Sstevel@tonic-gate 	    statb.st_size >= sizeof (auxv_t) &&
8820Sstevel@tonic-gate 	    (P->auxv = malloc(statb.st_size + sizeof (auxv_t))) != NULL) {
8830Sstevel@tonic-gate 		if ((naux = read(fd, P->auxv, statb.st_size)) < 0 ||
8840Sstevel@tonic-gate 		    (naux /= sizeof (auxv_t)) < 1) {
8850Sstevel@tonic-gate 			free(P->auxv);
8860Sstevel@tonic-gate 			P->auxv = NULL;
8870Sstevel@tonic-gate 		} else {
8880Sstevel@tonic-gate 			P->auxv[naux].a_type = AT_NULL;
8890Sstevel@tonic-gate 			P->auxv[naux].a_un.a_val = 0L;
8900Sstevel@tonic-gate 			P->nauxv = (int)naux;
8910Sstevel@tonic-gate 		}
8920Sstevel@tonic-gate 	}
8930Sstevel@tonic-gate 
8940Sstevel@tonic-gate 	(void) close(fd);
8950Sstevel@tonic-gate }
8960Sstevel@tonic-gate 
8970Sstevel@tonic-gate /*
8980Sstevel@tonic-gate  * Return a requested element from the process's aux vector.
8990Sstevel@tonic-gate  * Return -1 on failure (this is adequate for our purposes).
9000Sstevel@tonic-gate  */
9010Sstevel@tonic-gate long
Pgetauxval(struct ps_prochandle * P,int type)9020Sstevel@tonic-gate Pgetauxval(struct ps_prochandle *P, int type)
9030Sstevel@tonic-gate {
9040Sstevel@tonic-gate 	auxv_t *auxv;
9050Sstevel@tonic-gate 
9060Sstevel@tonic-gate 	if (P->auxv == NULL)
9070Sstevel@tonic-gate 		Preadauxvec(P);
9080Sstevel@tonic-gate 
9090Sstevel@tonic-gate 	if (P->auxv == NULL)
9100Sstevel@tonic-gate 		return (-1);
9110Sstevel@tonic-gate 
9120Sstevel@tonic-gate 	for (auxv = P->auxv; auxv->a_type != AT_NULL; auxv++) {
9130Sstevel@tonic-gate 		if (auxv->a_type == type)
9140Sstevel@tonic-gate 			return (auxv->a_un.a_val);
9150Sstevel@tonic-gate 	}
9160Sstevel@tonic-gate 
9170Sstevel@tonic-gate 	return (-1);
9180Sstevel@tonic-gate }
9190Sstevel@tonic-gate 
9200Sstevel@tonic-gate /*
9210Sstevel@tonic-gate  * Return a pointer to our internal copy of the process's aux vector.
9220Sstevel@tonic-gate  * The caller should not hold on to this pointer across any libproc calls.
9230Sstevel@tonic-gate  */
9240Sstevel@tonic-gate const auxv_t *
Pgetauxvec(struct ps_prochandle * P)9250Sstevel@tonic-gate Pgetauxvec(struct ps_prochandle *P)
9260Sstevel@tonic-gate {
9270Sstevel@tonic-gate 	static const auxv_t empty = { AT_NULL, 0L };
9280Sstevel@tonic-gate 
9290Sstevel@tonic-gate 	if (P->auxv == NULL)
9300Sstevel@tonic-gate 		Preadauxvec(P);
9310Sstevel@tonic-gate 
9320Sstevel@tonic-gate 	if (P->auxv == NULL)
9330Sstevel@tonic-gate 		return (&empty);
9340Sstevel@tonic-gate 
9350Sstevel@tonic-gate 	return (P->auxv);
9360Sstevel@tonic-gate }
9370Sstevel@tonic-gate 
9380Sstevel@tonic-gate /*
9392915Srh87107  * Return 1 if the given mapping corresponds to the given file_info_t's
9402915Srh87107  * load object; return 0 otherwise.
9412915Srh87107  */
9422915Srh87107 static int
is_mapping_in_file(struct ps_prochandle * P,map_info_t * mptr,file_info_t * fptr)9432915Srh87107 is_mapping_in_file(struct ps_prochandle *P, map_info_t *mptr, file_info_t *fptr)
9442915Srh87107 {
9452915Srh87107 	prmap_t *pmap = &mptr->map_pmap;
9462915Srh87107 	rd_loadobj_t *lop = fptr->file_lo;
9472915Srh87107 	uint_t i;
9484753Srh87107 	uintptr_t mstart, mend, sstart, send;
9492915Srh87107 
9502915Srh87107 	/*
9512915Srh87107 	 * We can get for free the start address of the text and data
9522915Srh87107 	 * sections of the load object. Start by seeing if the mapping
9532915Srh87107 	 * encloses either of these.
9542915Srh87107 	 */
9552915Srh87107 	if ((pmap->pr_vaddr <= lop->rl_base &&
9562915Srh87107 	    lop->rl_base < pmap->pr_vaddr + pmap->pr_size) ||
9572915Srh87107 	    (pmap->pr_vaddr <= lop->rl_data_base &&
9582915Srh87107 	    lop->rl_data_base < pmap->pr_vaddr + pmap->pr_size))
9592915Srh87107 		return (1);
9602915Srh87107 
9612915Srh87107 	/*
9622915Srh87107 	 * It's still possible that this mapping correponds to the load
9632915Srh87107 	 * object. Consider the example of a mapping whose start and end
9642915Srh87107 	 * addresses correspond to those of the load object's text section.
9652915Srh87107 	 * If the mapping splits, e.g. as a result of a segment demotion,
9662915Srh87107 	 * then although both mappings are still backed by the same section,
9672915Srh87107 	 * only one will be seen to enclose that section's start address.
9682915Srh87107 	 * Thus, to be rigorous, we ask not whether this mapping encloses
9692915Srh87107 	 * the start of a section, but whether there exists a section that
9704753Srh87107 	 * overlaps this mapping.
9712915Srh87107 	 *
9722915Srh87107 	 * If we don't already have the section addresses, and we successfully
9732915Srh87107 	 * get them, then we cache them in case we come here again.
9742915Srh87107 	 */
9752915Srh87107 	if (fptr->file_saddrs == NULL &&
9762915Srh87107 	    (fptr->file_saddrs = get_saddrs(P,
9772915Srh87107 	    fptr->file_map->map_pmap.pr_vaddr, &fptr->file_nsaddrs)) == NULL)
9782915Srh87107 		return (0);
9794753Srh87107 
9804753Srh87107 	mstart = mptr->map_pmap.pr_vaddr;
9814753Srh87107 	mend = mptr->map_pmap.pr_vaddr + mptr->map_pmap.pr_size;
9822915Srh87107 	for (i = 0; i < fptr->file_nsaddrs; i += 2) {
9834753Srh87107 		/* Does this section overlap the mapping? */
9844753Srh87107 		sstart = fptr->file_saddrs[i];
9854753Srh87107 		send = fptr->file_saddrs[i + 1];
9864753Srh87107 		if (!(mend <= sstart || mstart >= send))
9872915Srh87107 			return (1);
9882915Srh87107 	}
9892915Srh87107 
9902915Srh87107 	return (0);
9912915Srh87107 }
9922915Srh87107 
9932915Srh87107 /*
9940Sstevel@tonic-gate  * Find or build the symbol table for the given mapping.
9950Sstevel@tonic-gate  */
9960Sstevel@tonic-gate static file_info_t *
build_map_symtab(struct ps_prochandle * P,map_info_t * mptr)9970Sstevel@tonic-gate build_map_symtab(struct ps_prochandle *P, map_info_t *mptr)
9980Sstevel@tonic-gate {
9990Sstevel@tonic-gate 	prmap_t *pmap = &mptr->map_pmap;
10000Sstevel@tonic-gate 	file_info_t *fptr;
10010Sstevel@tonic-gate 	uint_t i;
10020Sstevel@tonic-gate 
10030Sstevel@tonic-gate 	if ((fptr = mptr->map_file) != NULL) {
10040Sstevel@tonic-gate 		Pbuild_file_symtab(P, fptr);
10050Sstevel@tonic-gate 		return (fptr);
10060Sstevel@tonic-gate 	}
10070Sstevel@tonic-gate 
10080Sstevel@tonic-gate 	if (pmap->pr_mapname[0] == '\0')
10090Sstevel@tonic-gate 		return (NULL);
10100Sstevel@tonic-gate 
10110Sstevel@tonic-gate 	/*
10120Sstevel@tonic-gate 	 * Attempt to find a matching file.
10130Sstevel@tonic-gate 	 * (A file can be mapped at several different addresses.)
10140Sstevel@tonic-gate 	 */
10150Sstevel@tonic-gate 	for (i = 0, fptr = list_next(&P->file_head); i < P->num_files;
10160Sstevel@tonic-gate 	    i++, fptr = list_next(fptr)) {
10170Sstevel@tonic-gate 		if (strcmp(fptr->file_pname, pmap->pr_mapname) == 0 &&
10182915Srh87107 		    fptr->file_lo && is_mapping_in_file(P, mptr, fptr)) {
10190Sstevel@tonic-gate 			mptr->map_file = fptr;
10200Sstevel@tonic-gate 			fptr->file_ref++;
10210Sstevel@tonic-gate 			Pbuild_file_symtab(P, fptr);
10220Sstevel@tonic-gate 			return (fptr);
10230Sstevel@tonic-gate 		}
10240Sstevel@tonic-gate 	}
10250Sstevel@tonic-gate 
10260Sstevel@tonic-gate 	/*
10270Sstevel@tonic-gate 	 * If we need to create a new file_info structure, iterate
10280Sstevel@tonic-gate 	 * through the load objects in order to attempt to connect
10290Sstevel@tonic-gate 	 * this new file with its primary text mapping.  We again
10300Sstevel@tonic-gate 	 * need to handle ld.so as a special case because we need
10310Sstevel@tonic-gate 	 * to be able to bootstrap librtld_db.
10320Sstevel@tonic-gate 	 */
10330Sstevel@tonic-gate 	if ((fptr = file_info_new(P, mptr)) == NULL)
10340Sstevel@tonic-gate 		return (NULL);
10350Sstevel@tonic-gate 
10360Sstevel@tonic-gate 	if (P->map_ldso != mptr) {
10370Sstevel@tonic-gate 		if (P->rap != NULL)
10380Sstevel@tonic-gate 			(void) rd_loadobj_iter(P->rap, map_iter, P);
10390Sstevel@tonic-gate 		else
10400Sstevel@tonic-gate 			(void) Prd_agent(P);
10410Sstevel@tonic-gate 	} else {
10420Sstevel@tonic-gate 		fptr->file_map = mptr;
10430Sstevel@tonic-gate 	}
10440Sstevel@tonic-gate 
10450Sstevel@tonic-gate 	/*
10460Sstevel@tonic-gate 	 * If librtld_db wasn't able to help us connect the file to a primary
10470Sstevel@tonic-gate 	 * text mapping, set file_map to the current mapping because we require
10480Sstevel@tonic-gate 	 * fptr->file_map to be set in Pbuild_file_symtab.  librtld_db may be
10490Sstevel@tonic-gate 	 * unaware of what's going on in the rare case that a legitimate ELF
10500Sstevel@tonic-gate 	 * file has been mmap(2)ed into the process address space *without*
10512712Snn35248 	 * the use of dlopen(3x).
10520Sstevel@tonic-gate 	 */
10530Sstevel@tonic-gate 	if (fptr->file_map == NULL)
10540Sstevel@tonic-gate 		fptr->file_map = mptr;
10550Sstevel@tonic-gate 
10560Sstevel@tonic-gate 	Pbuild_file_symtab(P, fptr);
10570Sstevel@tonic-gate 
10580Sstevel@tonic-gate 	return (fptr);
10590Sstevel@tonic-gate }
10600Sstevel@tonic-gate 
10610Sstevel@tonic-gate static int
read_ehdr32(struct ps_prochandle * P,Elf32_Ehdr * ehdr,uint_t * phnum,uintptr_t addr)1062942Sahl read_ehdr32(struct ps_prochandle *P, Elf32_Ehdr *ehdr, uint_t *phnum,
1063942Sahl     uintptr_t addr)
10640Sstevel@tonic-gate {
10650Sstevel@tonic-gate 	if (Pread(P, ehdr, sizeof (*ehdr), addr) != sizeof (*ehdr))
10660Sstevel@tonic-gate 		return (-1);
10670Sstevel@tonic-gate 
10680Sstevel@tonic-gate 	if (ehdr->e_ident[EI_MAG0] != ELFMAG0 ||
10690Sstevel@tonic-gate 	    ehdr->e_ident[EI_MAG1] != ELFMAG1 ||
10700Sstevel@tonic-gate 	    ehdr->e_ident[EI_MAG2] != ELFMAG2 ||
10710Sstevel@tonic-gate 	    ehdr->e_ident[EI_MAG3] != ELFMAG3 ||
10720Sstevel@tonic-gate 	    ehdr->e_ident[EI_CLASS] != ELFCLASS32 ||
10730Sstevel@tonic-gate #ifdef _BIG_ENDIAN
10740Sstevel@tonic-gate 	    ehdr->e_ident[EI_DATA] != ELFDATA2MSB ||
10750Sstevel@tonic-gate #else
10760Sstevel@tonic-gate 	    ehdr->e_ident[EI_DATA] != ELFDATA2LSB ||
10770Sstevel@tonic-gate #endif
10780Sstevel@tonic-gate 	    ehdr->e_ident[EI_VERSION] != EV_CURRENT)
10790Sstevel@tonic-gate 		return (-1);
10800Sstevel@tonic-gate 
1081942Sahl 	if ((*phnum = ehdr->e_phnum) == PN_XNUM) {
1082942Sahl 		Elf32_Shdr shdr0;
1083942Sahl 
1084942Sahl 		if (ehdr->e_shoff == 0 || ehdr->e_shentsize < sizeof (shdr0) ||
1085942Sahl 		    Pread(P, &shdr0, sizeof (shdr0), addr + ehdr->e_shoff) !=
1086942Sahl 		    sizeof (shdr0))
1087942Sahl 			return (-1);
1088942Sahl 
1089942Sahl 		if (shdr0.sh_info != 0)
1090942Sahl 			*phnum = shdr0.sh_info;
1091942Sahl 	}
1092942Sahl 
10930Sstevel@tonic-gate 	return (0);
10940Sstevel@tonic-gate }
10950Sstevel@tonic-gate 
10960Sstevel@tonic-gate static int
read_dynamic_phdr32(struct ps_prochandle * P,const Elf32_Ehdr * ehdr,uint_t phnum,Elf32_Phdr * phdr,uintptr_t addr)10970Sstevel@tonic-gate read_dynamic_phdr32(struct ps_prochandle *P, const Elf32_Ehdr *ehdr,
1098942Sahl     uint_t phnum, Elf32_Phdr *phdr, uintptr_t addr)
10990Sstevel@tonic-gate {
11000Sstevel@tonic-gate 	uint_t i;
11010Sstevel@tonic-gate 
1102942Sahl 	for (i = 0; i < phnum; i++) {
11030Sstevel@tonic-gate 		uintptr_t a = addr + ehdr->e_phoff + i * ehdr->e_phentsize;
11040Sstevel@tonic-gate 		if (Pread(P, phdr, sizeof (*phdr), a) != sizeof (*phdr))
11050Sstevel@tonic-gate 			return (-1);
11060Sstevel@tonic-gate 
11070Sstevel@tonic-gate 		if (phdr->p_type == PT_DYNAMIC)
11080Sstevel@tonic-gate 			return (0);
11090Sstevel@tonic-gate 	}
11100Sstevel@tonic-gate 
11110Sstevel@tonic-gate 	return (-1);
11120Sstevel@tonic-gate }
11130Sstevel@tonic-gate 
11140Sstevel@tonic-gate #ifdef _LP64
11150Sstevel@tonic-gate static int
read_ehdr64(struct ps_prochandle * P,Elf64_Ehdr * ehdr,uint_t * phnum,uintptr_t addr)1116942Sahl read_ehdr64(struct ps_prochandle *P, Elf64_Ehdr *ehdr, uint_t *phnum,
1117942Sahl     uintptr_t addr)
11180Sstevel@tonic-gate {
11190Sstevel@tonic-gate 	if (Pread(P, ehdr, sizeof (Elf64_Ehdr), addr) != sizeof (Elf64_Ehdr))
11200Sstevel@tonic-gate 		return (-1);
11210Sstevel@tonic-gate 
11220Sstevel@tonic-gate 	if (ehdr->e_ident[EI_MAG0] != ELFMAG0 ||
11230Sstevel@tonic-gate 	    ehdr->e_ident[EI_MAG1] != ELFMAG1 ||
11240Sstevel@tonic-gate 	    ehdr->e_ident[EI_MAG2] != ELFMAG2 ||
11250Sstevel@tonic-gate 	    ehdr->e_ident[EI_MAG3] != ELFMAG3 ||
11260Sstevel@tonic-gate 	    ehdr->e_ident[EI_CLASS] != ELFCLASS64 ||
11270Sstevel@tonic-gate #ifdef _BIG_ENDIAN
11280Sstevel@tonic-gate 	    ehdr->e_ident[EI_DATA] != ELFDATA2MSB ||
11290Sstevel@tonic-gate #else
11300Sstevel@tonic-gate 	    ehdr->e_ident[EI_DATA] != ELFDATA2LSB ||
11310Sstevel@tonic-gate #endif
11320Sstevel@tonic-gate 	    ehdr->e_ident[EI_VERSION] != EV_CURRENT)
11330Sstevel@tonic-gate 		return (-1);
11340Sstevel@tonic-gate 
1135942Sahl 	if ((*phnum = ehdr->e_phnum) == PN_XNUM) {
1136942Sahl 		Elf64_Shdr shdr0;
1137942Sahl 
1138942Sahl 		if (ehdr->e_shoff == 0 || ehdr->e_shentsize < sizeof (shdr0) ||
1139942Sahl 		    Pread(P, &shdr0, sizeof (shdr0), addr + ehdr->e_shoff) !=
1140942Sahl 		    sizeof (shdr0))
1141942Sahl 			return (-1);
1142942Sahl 
1143942Sahl 		if (shdr0.sh_info != 0)
1144942Sahl 			*phnum = shdr0.sh_info;
1145942Sahl 	}
1146942Sahl 
11470Sstevel@tonic-gate 	return (0);
11480Sstevel@tonic-gate }
11490Sstevel@tonic-gate 
11500Sstevel@tonic-gate static int
read_dynamic_phdr64(struct ps_prochandle * P,const Elf64_Ehdr * ehdr,uint_t phnum,Elf64_Phdr * phdr,uintptr_t addr)11510Sstevel@tonic-gate read_dynamic_phdr64(struct ps_prochandle *P, const Elf64_Ehdr *ehdr,
1152942Sahl     uint_t phnum, Elf64_Phdr *phdr, uintptr_t addr)
11530Sstevel@tonic-gate {
11540Sstevel@tonic-gate 	uint_t i;
11550Sstevel@tonic-gate 
1156942Sahl 	for (i = 0; i < phnum; i++) {
11570Sstevel@tonic-gate 		uintptr_t a = addr + ehdr->e_phoff + i * ehdr->e_phentsize;
11580Sstevel@tonic-gate 		if (Pread(P, phdr, sizeof (*phdr), a) != sizeof (*phdr))
11590Sstevel@tonic-gate 			return (-1);
11600Sstevel@tonic-gate 
11610Sstevel@tonic-gate 		if (phdr->p_type == PT_DYNAMIC)
11620Sstevel@tonic-gate 			return (0);
11630Sstevel@tonic-gate 	}
11640Sstevel@tonic-gate 
11650Sstevel@tonic-gate 	return (-1);
11660Sstevel@tonic-gate }
11670Sstevel@tonic-gate #endif	/* _LP64 */
11680Sstevel@tonic-gate 
11690Sstevel@tonic-gate /*
11700Sstevel@tonic-gate  * The text segment for each load object contains the elf header and
11710Sstevel@tonic-gate  * program headers. We can use this information to determine if the
11720Sstevel@tonic-gate  * file that corresponds to the load object is the same file that
11730Sstevel@tonic-gate  * was loaded into the process's address space. There can be a discrepency
11740Sstevel@tonic-gate  * if a file is recompiled after the process is started or if the target
11750Sstevel@tonic-gate  * represents a core file from a differently configured system -- two
11760Sstevel@tonic-gate  * common examples. The DT_CHECKSUM entry in the dynamic section
11770Sstevel@tonic-gate  * provides an easy method of comparison. It is important to note that
11780Sstevel@tonic-gate  * the dynamic section usually lives in the data segment, but the meta
11790Sstevel@tonic-gate  * data we use to find the dynamic section lives in the text segment so
11800Sstevel@tonic-gate  * if either of those segments is absent we can't proceed.
11810Sstevel@tonic-gate  *
11820Sstevel@tonic-gate  * We're looking through the elf file for several items: the symbol tables
11830Sstevel@tonic-gate  * (both dynsym and symtab), the procedure linkage table (PLT) base,
11840Sstevel@tonic-gate  * size, and relocation base, and the CTF information. Most of this can
11850Sstevel@tonic-gate  * be recovered from the loaded image of the file itself, the exceptions
11860Sstevel@tonic-gate  * being the symtab and CTF data.
11870Sstevel@tonic-gate  *
11880Sstevel@tonic-gate  * First we try to open the file that we think corresponds to the load
11890Sstevel@tonic-gate  * object, if the DT_CHECKSUM values match, we're all set, and can simply
11900Sstevel@tonic-gate  * recover all the information we need from the file. If the values of
11910Sstevel@tonic-gate  * DT_CHECKSUM don't match, or if we can't access the file for whatever
11920Sstevel@tonic-gate  * reasaon, we fake up a elf file to use in its stead. If we can't read
11930Sstevel@tonic-gate  * the elf data in the process's address space, we fall back to using
11940Sstevel@tonic-gate  * the file even though it may give inaccurate information.
11950Sstevel@tonic-gate  *
11960Sstevel@tonic-gate  * The elf file that we fake up has to consist of sections for the
11970Sstevel@tonic-gate  * dynsym, the PLT and the dynamic section. Note that in the case of a
11980Sstevel@tonic-gate  * core file, we'll get the CTF data in the file_info_t later on from
11990Sstevel@tonic-gate  * a section embedded the core file (if it's present).
12000Sstevel@tonic-gate  *
12010Sstevel@tonic-gate  * file_differs() conservatively looks for mismatched files, identifying
12020Sstevel@tonic-gate  * a match when there is any ambiguity (since that's the legacy behavior).
12030Sstevel@tonic-gate  */
12040Sstevel@tonic-gate static int
file_differs(struct ps_prochandle * P,Elf * elf,file_info_t * fptr)12050Sstevel@tonic-gate file_differs(struct ps_prochandle *P, Elf *elf, file_info_t *fptr)
12060Sstevel@tonic-gate {
12070Sstevel@tonic-gate 	Elf_Scn *scn;
12080Sstevel@tonic-gate 	GElf_Shdr shdr;
12090Sstevel@tonic-gate 	GElf_Dyn dyn;
12100Sstevel@tonic-gate 	Elf_Data *data;
12110Sstevel@tonic-gate 	uint_t i, ndyn;
12120Sstevel@tonic-gate 	GElf_Xword cksum;
12130Sstevel@tonic-gate 	uintptr_t addr;
12140Sstevel@tonic-gate 
12150Sstevel@tonic-gate 	if (fptr->file_map == NULL)
12160Sstevel@tonic-gate 		return (0);
12170Sstevel@tonic-gate 
12180Sstevel@tonic-gate 	if ((Pcontent(P) & (CC_CONTENT_TEXT | CC_CONTENT_DATA)) !=
12190Sstevel@tonic-gate 	    (CC_CONTENT_TEXT | CC_CONTENT_DATA))
12200Sstevel@tonic-gate 		return (0);
12210Sstevel@tonic-gate 
12220Sstevel@tonic-gate 	/*
12230Sstevel@tonic-gate 	 * First, we find the checksum value in the elf file.
12240Sstevel@tonic-gate 	 */
12250Sstevel@tonic-gate 	scn = NULL;
12260Sstevel@tonic-gate 	while ((scn = elf_nextscn(elf, scn)) != NULL) {
12270Sstevel@tonic-gate 		if (gelf_getshdr(scn, &shdr) != NULL &&
12280Sstevel@tonic-gate 		    shdr.sh_type == SHT_DYNAMIC)
12290Sstevel@tonic-gate 			goto found_shdr;
12300Sstevel@tonic-gate 	}
12310Sstevel@tonic-gate 	return (0);
12320Sstevel@tonic-gate 
12330Sstevel@tonic-gate found_shdr:
12340Sstevel@tonic-gate 	if ((data = elf_getdata(scn, NULL)) == NULL)
12350Sstevel@tonic-gate 		return (0);
12360Sstevel@tonic-gate 
12370Sstevel@tonic-gate 	if (P->status.pr_dmodel == PR_MODEL_ILP32)
12380Sstevel@tonic-gate 		ndyn = shdr.sh_size / sizeof (Elf32_Dyn);
12390Sstevel@tonic-gate #ifdef _LP64
12400Sstevel@tonic-gate 	else if (P->status.pr_dmodel == PR_MODEL_LP64)
12410Sstevel@tonic-gate 		ndyn = shdr.sh_size / sizeof (Elf64_Dyn);
12420Sstevel@tonic-gate #endif
12430Sstevel@tonic-gate 	else
12440Sstevel@tonic-gate 		return (0);
12450Sstevel@tonic-gate 
12460Sstevel@tonic-gate 	for (i = 0; i < ndyn; i++) {
12470Sstevel@tonic-gate 		if (gelf_getdyn(data, i, &dyn) != NULL &&
12480Sstevel@tonic-gate 		    dyn.d_tag == DT_CHECKSUM)
12490Sstevel@tonic-gate 			goto found_cksum;
12500Sstevel@tonic-gate 	}
12512712Snn35248 
12522712Snn35248 	/*
12532712Snn35248 	 * The in-memory ELF has no DT_CHECKSUM section, but we will report it
12542712Snn35248 	 * as matching the file anyhow.
12552712Snn35248 	 */
12560Sstevel@tonic-gate 	return (0);
12570Sstevel@tonic-gate 
12580Sstevel@tonic-gate found_cksum:
12590Sstevel@tonic-gate 	cksum = dyn.d_un.d_val;
12600Sstevel@tonic-gate 	dprintf("elf cksum value is %llx\n", (u_longlong_t)cksum);
12610Sstevel@tonic-gate 
12620Sstevel@tonic-gate 	/*
12630Sstevel@tonic-gate 	 * Get the base of the text mapping that corresponds to this file.
12640Sstevel@tonic-gate 	 */
12650Sstevel@tonic-gate 	addr = fptr->file_map->map_pmap.pr_vaddr;
12660Sstevel@tonic-gate 
12670Sstevel@tonic-gate 	if (P->status.pr_dmodel == PR_MODEL_ILP32) {
12680Sstevel@tonic-gate 		Elf32_Ehdr ehdr;
12690Sstevel@tonic-gate 		Elf32_Phdr phdr;
12700Sstevel@tonic-gate 		Elf32_Dyn dync, *dynp;
1271942Sahl 		uint_t phnum, i;
1272942Sahl 
1273942Sahl 		if (read_ehdr32(P, &ehdr, &phnum, addr) != 0 ||
1274942Sahl 		    read_dynamic_phdr32(P, &ehdr, phnum, &phdr, addr) != 0)
12750Sstevel@tonic-gate 			return (0);
12760Sstevel@tonic-gate 
12770Sstevel@tonic-gate 		if (ehdr.e_type == ET_DYN)
12780Sstevel@tonic-gate 			phdr.p_vaddr += addr;
12790Sstevel@tonic-gate 		if ((dynp = malloc(phdr.p_filesz)) == NULL)
12800Sstevel@tonic-gate 			return (0);
12810Sstevel@tonic-gate 		dync.d_tag = DT_NULL;
12820Sstevel@tonic-gate 		if (Pread(P, dynp, phdr.p_filesz, phdr.p_vaddr) !=
12830Sstevel@tonic-gate 		    phdr.p_filesz) {
12840Sstevel@tonic-gate 			free(dynp);
12850Sstevel@tonic-gate 			return (0);
12860Sstevel@tonic-gate 		}
12870Sstevel@tonic-gate 
12880Sstevel@tonic-gate 		for (i = 0; i < phdr.p_filesz / sizeof (Elf32_Dyn); i++) {
12890Sstevel@tonic-gate 			if (dynp[i].d_tag == DT_CHECKSUM)
12900Sstevel@tonic-gate 				dync = dynp[i];
12910Sstevel@tonic-gate 		}
12920Sstevel@tonic-gate 
12930Sstevel@tonic-gate 		free(dynp);
12940Sstevel@tonic-gate 
12950Sstevel@tonic-gate 		if (dync.d_tag != DT_CHECKSUM)
12960Sstevel@tonic-gate 			return (0);
12970Sstevel@tonic-gate 
12980Sstevel@tonic-gate 		dprintf("image cksum value is %llx\n",
12990Sstevel@tonic-gate 		    (u_longlong_t)dync.d_un.d_val);
13000Sstevel@tonic-gate 		return (dync.d_un.d_val != cksum);
13010Sstevel@tonic-gate #ifdef _LP64
13020Sstevel@tonic-gate 	} else if (P->status.pr_dmodel == PR_MODEL_LP64) {
13030Sstevel@tonic-gate 		Elf64_Ehdr ehdr;
13040Sstevel@tonic-gate 		Elf64_Phdr phdr;
13050Sstevel@tonic-gate 		Elf64_Dyn dync, *dynp;
1306942Sahl 		uint_t phnum, i;
1307942Sahl 
1308942Sahl 		if (read_ehdr64(P, &ehdr, &phnum, addr) != 0 ||
1309942Sahl 		    read_dynamic_phdr64(P, &ehdr, phnum, &phdr, addr) != 0)
13100Sstevel@tonic-gate 			return (0);
13110Sstevel@tonic-gate 
13120Sstevel@tonic-gate 		if (ehdr.e_type == ET_DYN)
13130Sstevel@tonic-gate 			phdr.p_vaddr += addr;
13140Sstevel@tonic-gate 		if ((dynp = malloc(phdr.p_filesz)) == NULL)
13150Sstevel@tonic-gate 			return (0);
13160Sstevel@tonic-gate 		dync.d_tag = DT_NULL;
13170Sstevel@tonic-gate 		if (Pread(P, dynp, phdr.p_filesz, phdr.p_vaddr) !=
13180Sstevel@tonic-gate 		    phdr.p_filesz) {
13190Sstevel@tonic-gate 			free(dynp);
13200Sstevel@tonic-gate 			return (0);
13210Sstevel@tonic-gate 		}
13220Sstevel@tonic-gate 
13230Sstevel@tonic-gate 		for (i = 0; i < phdr.p_filesz / sizeof (Elf64_Dyn); i++) {
13240Sstevel@tonic-gate 			if (dynp[i].d_tag == DT_CHECKSUM)
13250Sstevel@tonic-gate 				dync = dynp[i];
13260Sstevel@tonic-gate 		}
13270Sstevel@tonic-gate 
13280Sstevel@tonic-gate 		free(dynp);
13290Sstevel@tonic-gate 
13300Sstevel@tonic-gate 		if (dync.d_tag != DT_CHECKSUM)
13310Sstevel@tonic-gate 			return (0);
13320Sstevel@tonic-gate 
13330Sstevel@tonic-gate 		dprintf("image cksum value is %llx\n",
13340Sstevel@tonic-gate 		    (u_longlong_t)dync.d_un.d_val);
13350Sstevel@tonic-gate 		return (dync.d_un.d_val != cksum);
13360Sstevel@tonic-gate #endif	/* _LP64 */
13370Sstevel@tonic-gate 	}
13380Sstevel@tonic-gate 
13390Sstevel@tonic-gate 	return (0);
13400Sstevel@tonic-gate }
13410Sstevel@tonic-gate 
13423347Sab196087 /*
13433347Sab196087  * Read data from the specified process and construct an in memory
13443347Sab196087  * image of an ELF file that represents it well enough to let
13453347Sab196087  * us probe it for information.
13463347Sab196087  */
13470Sstevel@tonic-gate static Elf *
fake_elf(struct ps_prochandle * P,file_info_t * fptr)13480Sstevel@tonic-gate fake_elf(struct ps_prochandle *P, file_info_t *fptr)
13490Sstevel@tonic-gate {
13503347Sab196087 	Elf *elf;
13510Sstevel@tonic-gate 	uintptr_t addr;
13523347Sab196087 	uint_t phnum;
13530Sstevel@tonic-gate 
13540Sstevel@tonic-gate 	if (fptr->file_map == NULL)
13550Sstevel@tonic-gate 		return (NULL);
13560Sstevel@tonic-gate 
13570Sstevel@tonic-gate 	if ((Pcontent(P) & (CC_CONTENT_TEXT | CC_CONTENT_DATA)) !=
13580Sstevel@tonic-gate 	    (CC_CONTENT_TEXT | CC_CONTENT_DATA))
13590Sstevel@tonic-gate 		return (NULL);
13600Sstevel@tonic-gate 
13610Sstevel@tonic-gate 	addr = fptr->file_map->map_pmap.pr_vaddr;
13620Sstevel@tonic-gate 
13630Sstevel@tonic-gate 	if (P->status.pr_dmodel == PR_MODEL_ILP32) {
13643347Sab196087 		Elf32_Ehdr ehdr;
13650Sstevel@tonic-gate 		Elf32_Phdr phdr;
13660Sstevel@tonic-gate 
13672712Snn35248 		if ((read_ehdr32(P, &ehdr, &phnum, addr) != 0) ||
1368942Sahl 		    read_dynamic_phdr32(P, &ehdr, phnum, &phdr, addr) != 0)
13690Sstevel@tonic-gate 			return (NULL);
13700Sstevel@tonic-gate 
13713347Sab196087 		elf = fake_elf32(P, fptr, addr, &ehdr, phnum, &phdr);
13720Sstevel@tonic-gate #ifdef _LP64
13733347Sab196087 	} else {
13743347Sab196087 		Elf64_Ehdr ehdr;
13750Sstevel@tonic-gate 		Elf64_Phdr phdr;
13760Sstevel@tonic-gate 
1377942Sahl 		if (read_ehdr64(P, &ehdr, &phnum, addr) != 0 ||
1378942Sahl 		    read_dynamic_phdr64(P, &ehdr, phnum, &phdr, addr) != 0)
13790Sstevel@tonic-gate 			return (NULL);
13800Sstevel@tonic-gate 
13813347Sab196087 		elf = fake_elf64(P, fptr, addr, &ehdr, phnum, &phdr);
13823347Sab196087 #endif
13830Sstevel@tonic-gate 	}
13840Sstevel@tonic-gate 
13850Sstevel@tonic-gate 	return (elf);
13860Sstevel@tonic-gate }
13870Sstevel@tonic-gate 
13880Sstevel@tonic-gate /*
13890Sstevel@tonic-gate  * We wouldn't need these if qsort(3C) took an argument for the callback...
13900Sstevel@tonic-gate  */
13910Sstevel@tonic-gate static mutex_t sort_mtx = DEFAULTMUTEX;
13920Sstevel@tonic-gate static char *sort_strs;
13930Sstevel@tonic-gate static GElf_Sym *sort_syms;
13940Sstevel@tonic-gate 
13950Sstevel@tonic-gate int
byaddr_cmp_common(GElf_Sym * a,char * aname,GElf_Sym * b,char * bname)13960Sstevel@tonic-gate byaddr_cmp_common(GElf_Sym *a, char *aname, GElf_Sym *b, char *bname)
13970Sstevel@tonic-gate {
13980Sstevel@tonic-gate 	if (a->st_value < b->st_value)
13990Sstevel@tonic-gate 		return (-1);
14000Sstevel@tonic-gate 	if (a->st_value > b->st_value)
14010Sstevel@tonic-gate 		return (1);
14020Sstevel@tonic-gate 
14030Sstevel@tonic-gate 	/*
14040Sstevel@tonic-gate 	 * Prefer the function to the non-function.
14050Sstevel@tonic-gate 	 */
14060Sstevel@tonic-gate 	if (GELF_ST_TYPE(a->st_info) != GELF_ST_TYPE(b->st_info)) {
14070Sstevel@tonic-gate 		if (GELF_ST_TYPE(a->st_info) == STT_FUNC)
14080Sstevel@tonic-gate 			return (-1);
14090Sstevel@tonic-gate 		if (GELF_ST_TYPE(b->st_info) == STT_FUNC)
14100Sstevel@tonic-gate 			return (1);
14110Sstevel@tonic-gate 	}
14120Sstevel@tonic-gate 
14130Sstevel@tonic-gate 	/*
14140Sstevel@tonic-gate 	 * Prefer the weak or strong global symbol to the local symbol.
14150Sstevel@tonic-gate 	 */
14160Sstevel@tonic-gate 	if (GELF_ST_BIND(a->st_info) != GELF_ST_BIND(b->st_info)) {
14170Sstevel@tonic-gate 		if (GELF_ST_BIND(b->st_info) == STB_LOCAL)
14180Sstevel@tonic-gate 			return (-1);
14190Sstevel@tonic-gate 		if (GELF_ST_BIND(a->st_info) == STB_LOCAL)
14200Sstevel@tonic-gate 			return (1);
14210Sstevel@tonic-gate 	}
14220Sstevel@tonic-gate 
14230Sstevel@tonic-gate 	/*
14241880Sahl 	 * Prefer the symbol that doesn't begin with a '$' since compilers and
14251880Sahl 	 * other symbol generators often use it as a prefix.
14261880Sahl 	 */
14271880Sahl 	if (*bname == '$')
14281880Sahl 		return (-1);
14291880Sahl 	if (*aname == '$')
14301880Sahl 		return (1);
14311880Sahl 
14321880Sahl 	/*
14330Sstevel@tonic-gate 	 * Prefer the name with fewer leading underscores in the name.
14340Sstevel@tonic-gate 	 */
14350Sstevel@tonic-gate 	while (*aname == '_' && *bname == '_') {
14360Sstevel@tonic-gate 		aname++;
14370Sstevel@tonic-gate 		bname++;
14380Sstevel@tonic-gate 	}
14390Sstevel@tonic-gate 
14400Sstevel@tonic-gate 	if (*bname == '_')
14410Sstevel@tonic-gate 		return (-1);
14420Sstevel@tonic-gate 	if (*aname == '_')
14430Sstevel@tonic-gate 		return (1);
14440Sstevel@tonic-gate 
14450Sstevel@tonic-gate 	/*
14460Sstevel@tonic-gate 	 * Prefer the symbol with the smaller size.
14470Sstevel@tonic-gate 	 */
14480Sstevel@tonic-gate 	if (a->st_size < b->st_size)
14490Sstevel@tonic-gate 		return (-1);
14500Sstevel@tonic-gate 	if (a->st_size > b->st_size)
14510Sstevel@tonic-gate 		return (1);
14520Sstevel@tonic-gate 
14530Sstevel@tonic-gate 	/*
14540Sstevel@tonic-gate 	 * All other factors being equal, fall back to lexicographic order.
14550Sstevel@tonic-gate 	 */
14560Sstevel@tonic-gate 	return (strcmp(aname, bname));
14570Sstevel@tonic-gate }
14580Sstevel@tonic-gate 
14590Sstevel@tonic-gate static int
byaddr_cmp(const void * aa,const void * bb)14600Sstevel@tonic-gate byaddr_cmp(const void *aa, const void *bb)
14610Sstevel@tonic-gate {
14620Sstevel@tonic-gate 	GElf_Sym *a = &sort_syms[*(uint_t *)aa];
14630Sstevel@tonic-gate 	GElf_Sym *b = &sort_syms[*(uint_t *)bb];
14640Sstevel@tonic-gate 	char *aname = sort_strs + a->st_name;
14650Sstevel@tonic-gate 	char *bname = sort_strs + b->st_name;
14660Sstevel@tonic-gate 
14670Sstevel@tonic-gate 	return (byaddr_cmp_common(a, aname, b, bname));
14680Sstevel@tonic-gate }
14690Sstevel@tonic-gate 
14700Sstevel@tonic-gate static int
byname_cmp(const void * aa,const void * bb)14710Sstevel@tonic-gate byname_cmp(const void *aa, const void *bb)
14720Sstevel@tonic-gate {
14730Sstevel@tonic-gate 	GElf_Sym *a = &sort_syms[*(uint_t *)aa];
14740Sstevel@tonic-gate 	GElf_Sym *b = &sort_syms[*(uint_t *)bb];
14750Sstevel@tonic-gate 	char *aname = sort_strs + a->st_name;
14760Sstevel@tonic-gate 	char *bname = sort_strs + b->st_name;
14770Sstevel@tonic-gate 
14780Sstevel@tonic-gate 	return (strcmp(aname, bname));
14790Sstevel@tonic-gate }
14800Sstevel@tonic-gate 
14813347Sab196087 /*
14823347Sab196087  * Given a symbol index, look up the corresponding symbol from the
14833347Sab196087  * given symbol table.
14843347Sab196087  *
14853347Sab196087  * This function allows the caller to treat the symbol table as a single
14863347Sab196087  * logical entity even though there may be 2 actual ELF symbol tables
14873347Sab196087  * involved. See the comments in Pcontrol.h for details.
14883347Sab196087  */
14893347Sab196087 static GElf_Sym *
symtab_getsym(sym_tbl_t * symtab,int ndx,GElf_Sym * dst)14903347Sab196087 symtab_getsym(sym_tbl_t *symtab, int ndx, GElf_Sym *dst)
14913347Sab196087 {
14923347Sab196087 	/* If index is in range of primary symtab, look it up there */
14933347Sab196087 	if (ndx >= symtab->sym_symn_aux) {
14943347Sab196087 		return (gelf_getsym(symtab->sym_data_pri,
14953347Sab196087 		    ndx - symtab->sym_symn_aux, dst));
14963347Sab196087 	}
14973347Sab196087 
14983347Sab196087 	/* Not in primary: Look it up in the auxiliary symtab */
14993347Sab196087 	return (gelf_getsym(symtab->sym_data_aux, ndx, dst));
15003347Sab196087 }
15013347Sab196087 
15020Sstevel@tonic-gate void
optimize_symtab(sym_tbl_t * symtab)15030Sstevel@tonic-gate optimize_symtab(sym_tbl_t *symtab)
15040Sstevel@tonic-gate {
15050Sstevel@tonic-gate 	GElf_Sym *symp, *syms;
15060Sstevel@tonic-gate 	uint_t i, *indexa, *indexb;
15070Sstevel@tonic-gate 	size_t symn, strsz, count;
15080Sstevel@tonic-gate 
15093347Sab196087 	if (symtab == NULL || symtab->sym_data_pri == NULL ||
15100Sstevel@tonic-gate 	    symtab->sym_byaddr != NULL)
15110Sstevel@tonic-gate 		return;
15120Sstevel@tonic-gate 
15130Sstevel@tonic-gate 	symn = symtab->sym_symn;
15140Sstevel@tonic-gate 	strsz = symtab->sym_strsz;
15150Sstevel@tonic-gate 
15160Sstevel@tonic-gate 	symp = syms = malloc(sizeof (GElf_Sym) * symn);
15173347Sab196087 	if (symp == NULL) {
15183347Sab196087 		dprintf("optimize_symtab: failed to malloc symbol array");
15193347Sab196087 		return;
15203347Sab196087 	}
15210Sstevel@tonic-gate 
15220Sstevel@tonic-gate 	/*
15230Sstevel@tonic-gate 	 * First record all the symbols into a table and count up the ones
15240Sstevel@tonic-gate 	 * that we're interested in. We mark symbols as invalid by setting
15250Sstevel@tonic-gate 	 * the st_name to an illegal value.
15260Sstevel@tonic-gate 	 */
15270Sstevel@tonic-gate 	for (i = 0, count = 0; i < symn; i++, symp++) {
15283347Sab196087 		if (symtab_getsym(symtab, i, symp) != NULL &&
15290Sstevel@tonic-gate 		    symp->st_name < strsz &&
15300Sstevel@tonic-gate 		    IS_DATA_TYPE(GELF_ST_TYPE(symp->st_info)))
15310Sstevel@tonic-gate 			count++;
15320Sstevel@tonic-gate 		else
15330Sstevel@tonic-gate 			symp->st_name = strsz;
15340Sstevel@tonic-gate 	}
15350Sstevel@tonic-gate 
15360Sstevel@tonic-gate 	/*
15370Sstevel@tonic-gate 	 * Allocate sufficient space for both tables and populate them
15380Sstevel@tonic-gate 	 * with the same symbols we just counted.
15390Sstevel@tonic-gate 	 */
15400Sstevel@tonic-gate 	symtab->sym_count = count;
15410Sstevel@tonic-gate 	indexa = symtab->sym_byaddr = calloc(sizeof (uint_t), count);
15420Sstevel@tonic-gate 	indexb = symtab->sym_byname = calloc(sizeof (uint_t), count);
15433347Sab196087 	if (indexa == NULL || indexb == NULL) {
15443347Sab196087 		dprintf(
15453347Sab196087 		    "optimize_symtab: failed to malloc symbol index arrays");
15463347Sab196087 		symtab->sym_count = 0;
15473347Sab196087 		if (indexa != NULL) {	/* First alloc succeeded. Free it */
15483347Sab196087 			free(indexa);
15493347Sab196087 			symtab->sym_byaddr = NULL;
15503347Sab196087 		}
15513347Sab196087 		free(syms);
15523347Sab196087 		return;
15533347Sab196087 	}
15540Sstevel@tonic-gate 	for (i = 0, symp = syms; i < symn; i++, symp++) {
15550Sstevel@tonic-gate 		if (symp->st_name < strsz)
15560Sstevel@tonic-gate 			*indexa++ = *indexb++ = i;
15570Sstevel@tonic-gate 	}
15580Sstevel@tonic-gate 
15590Sstevel@tonic-gate 	/*
15604753Srh87107 	 * Sort the two tables according to the appropriate criteria,
15614753Srh87107 	 * unless the user has overridden this behaviour.
15624753Srh87107 	 *
15634753Srh87107 	 * An example where we might not sort the tables is the relatively
15644753Srh87107 	 * unusual case of a process with very large symbol tables in which
15654753Srh87107 	 * we perform few lookups. In such a case the total time would be
15664753Srh87107 	 * dominated by the sort. It is difficult to determine a priori
15674753Srh87107 	 * how many lookups an arbitrary client will perform, and
15684753Srh87107 	 * hence whether the symbol tables should be sorted. We therefore
15694753Srh87107 	 * sort the tables by default, but provide the user with a
15704753Srh87107 	 * "chicken switch" in the form of the LIBPROC_NO_QSORT
15714753Srh87107 	 * environment variable.
15720Sstevel@tonic-gate 	 */
15734753Srh87107 	if (!_libproc_no_qsort) {
15744753Srh87107 		(void) mutex_lock(&sort_mtx);
15754753Srh87107 		sort_strs = symtab->sym_strs;
15764753Srh87107 		sort_syms = syms;
15774753Srh87107 
15784753Srh87107 		qsort(symtab->sym_byaddr, count, sizeof (uint_t), byaddr_cmp);
15794753Srh87107 		qsort(symtab->sym_byname, count, sizeof (uint_t), byname_cmp);
15804753Srh87107 
15814753Srh87107 		sort_strs = NULL;
15824753Srh87107 		sort_syms = NULL;
15834753Srh87107 		(void) mutex_unlock(&sort_mtx);
15844753Srh87107 	}
15850Sstevel@tonic-gate 
15860Sstevel@tonic-gate 	free(syms);
15870Sstevel@tonic-gate }
15880Sstevel@tonic-gate 
158910201SEdward.Pilatowicz@Sun.COM 
159010201SEdward.Pilatowicz@Sun.COM static Elf *
build_fake_elf(struct ps_prochandle * P,file_info_t * fptr,GElf_Ehdr * ehdr,size_t * nshdrs,Elf_Data ** shdata)159110201SEdward.Pilatowicz@Sun.COM build_fake_elf(struct ps_prochandle *P, file_info_t *fptr, GElf_Ehdr *ehdr,
159210201SEdward.Pilatowicz@Sun.COM 	size_t *nshdrs, Elf_Data **shdata)
159310201SEdward.Pilatowicz@Sun.COM {
159410201SEdward.Pilatowicz@Sun.COM 	size_t shstrndx;
159510201SEdward.Pilatowicz@Sun.COM 	Elf_Scn *scn;
159610201SEdward.Pilatowicz@Sun.COM 	Elf *elf;
159710201SEdward.Pilatowicz@Sun.COM 
159810201SEdward.Pilatowicz@Sun.COM 	if ((elf = fake_elf(P, fptr)) == NULL ||
159910201SEdward.Pilatowicz@Sun.COM 	    elf_kind(elf) != ELF_K_ELF ||
160010201SEdward.Pilatowicz@Sun.COM 	    gelf_getehdr(elf, ehdr) == NULL ||
160110201SEdward.Pilatowicz@Sun.COM 	    elf_getshdrnum(elf, nshdrs) == -1 ||
160210201SEdward.Pilatowicz@Sun.COM 	    elf_getshdrstrndx(elf, &shstrndx) == -1 ||
160310201SEdward.Pilatowicz@Sun.COM 	    (scn = elf_getscn(elf, shstrndx)) == NULL ||
160410201SEdward.Pilatowicz@Sun.COM 	    (*shdata = elf_getdata(scn, NULL)) == NULL) {
160510201SEdward.Pilatowicz@Sun.COM 		if (elf != NULL)
160610201SEdward.Pilatowicz@Sun.COM 			(void) elf_end(elf);
160710201SEdward.Pilatowicz@Sun.COM 		dprintf("failed to fake up ELF file\n");
160810201SEdward.Pilatowicz@Sun.COM 		return (NULL);
160910201SEdward.Pilatowicz@Sun.COM 	}
161010201SEdward.Pilatowicz@Sun.COM 
161110201SEdward.Pilatowicz@Sun.COM 	return (elf);
161210201SEdward.Pilatowicz@Sun.COM }
161310201SEdward.Pilatowicz@Sun.COM 
16140Sstevel@tonic-gate /*
16150Sstevel@tonic-gate  * Build the symbol table for the given mapped file.
16160Sstevel@tonic-gate  */
16170Sstevel@tonic-gate void
Pbuild_file_symtab(struct ps_prochandle * P,file_info_t * fptr)16180Sstevel@tonic-gate Pbuild_file_symtab(struct ps_prochandle *P, file_info_t *fptr)
16190Sstevel@tonic-gate {
16200Sstevel@tonic-gate 	char objectfile[PATH_MAX];
16210Sstevel@tonic-gate 	uint_t i;
16220Sstevel@tonic-gate 
16230Sstevel@tonic-gate 	GElf_Ehdr ehdr;
16240Sstevel@tonic-gate 	GElf_Sym s;
16250Sstevel@tonic-gate 
16260Sstevel@tonic-gate 	Elf_Data *shdata;
16270Sstevel@tonic-gate 	Elf_Scn *scn;
16280Sstevel@tonic-gate 	Elf *elf;
1629942Sahl 	size_t nshdrs, shstrndx;
16300Sstevel@tonic-gate 
16310Sstevel@tonic-gate 	struct {
16320Sstevel@tonic-gate 		GElf_Shdr c_shdr;
16330Sstevel@tonic-gate 		Elf_Data *c_data;
16340Sstevel@tonic-gate 		const char *c_name;
16350Sstevel@tonic-gate 	} *cp, *cache = NULL, *dyn = NULL, *plt = NULL, *ctf = NULL;
16360Sstevel@tonic-gate 
16370Sstevel@tonic-gate 	if (fptr->file_init)
16380Sstevel@tonic-gate 		return;	/* We've already processed this file */
16390Sstevel@tonic-gate 
16400Sstevel@tonic-gate 	/*
16410Sstevel@tonic-gate 	 * Mark the file_info struct as having the symbol table initialized
16420Sstevel@tonic-gate 	 * even if we fail below.  We tried once; we don't try again.
16430Sstevel@tonic-gate 	 */
16440Sstevel@tonic-gate 	fptr->file_init = 1;
16450Sstevel@tonic-gate 
16460Sstevel@tonic-gate 	if (elf_version(EV_CURRENT) == EV_NONE) {
16470Sstevel@tonic-gate 		dprintf("libproc ELF version is more recent than libelf\n");
16480Sstevel@tonic-gate 		return;
16490Sstevel@tonic-gate 	}
16500Sstevel@tonic-gate 
16510Sstevel@tonic-gate 	if (P->state == PS_DEAD || P->state == PS_IDLE) {
16527675SEdward.Pilatowicz@Sun.COM 		char *name;
16530Sstevel@tonic-gate 		/*
16540Sstevel@tonic-gate 		 * If we're a not live, we can't open files from the /proc
16550Sstevel@tonic-gate 		 * object directory; we have only the mapping and file names
16560Sstevel@tonic-gate 		 * to guide us.  We prefer the file_lname, but need to handle
16570Sstevel@tonic-gate 		 * the case of it being NULL in order to bootstrap: we first
16580Sstevel@tonic-gate 		 * come here during rd_new() when the only information we have
16590Sstevel@tonic-gate 		 * is interpreter name associated with the AT_BASE mapping.
16607675SEdward.Pilatowicz@Sun.COM 		 *
16617675SEdward.Pilatowicz@Sun.COM 		 * Also, if the zone associated with the core file seems
16627675SEdward.Pilatowicz@Sun.COM 		 * to exists on this machine we'll try to open the object
16637675SEdward.Pilatowicz@Sun.COM 		 * file within the zone.
16640Sstevel@tonic-gate 		 */
16657675SEdward.Pilatowicz@Sun.COM 		if (fptr->file_rname != NULL)
16667675SEdward.Pilatowicz@Sun.COM 			name = fptr->file_rname;
16677675SEdward.Pilatowicz@Sun.COM 		else if (fptr->file_lname != NULL)
16687675SEdward.Pilatowicz@Sun.COM 			name = fptr->file_lname;
16697675SEdward.Pilatowicz@Sun.COM 		else
16707675SEdward.Pilatowicz@Sun.COM 			name = fptr->file_pname;
16717675SEdward.Pilatowicz@Sun.COM 		(void) strlcpy(objectfile, name, sizeof (objectfile));
16720Sstevel@tonic-gate 	} else {
16730Sstevel@tonic-gate 		(void) snprintf(objectfile, sizeof (objectfile),
16742712Snn35248 		    "%s/%d/object/%s",
16752712Snn35248 		    procfs_path, (int)P->pid, fptr->file_pname);
16760Sstevel@tonic-gate 	}
16770Sstevel@tonic-gate 
16780Sstevel@tonic-gate 	/*
16790Sstevel@tonic-gate 	 * Open the object file, create the elf file, and then get the elf
16800Sstevel@tonic-gate 	 * header and .shstrtab data buffer so we can process sections by
16810Sstevel@tonic-gate 	 * name. If anything goes wrong try to fake up an elf file from
16820Sstevel@tonic-gate 	 * the in-core elf image.
16830Sstevel@tonic-gate 	 */
168410201SEdward.Pilatowicz@Sun.COM 
168510201SEdward.Pilatowicz@Sun.COM 	if (_libproc_incore_elf) {
168610201SEdward.Pilatowicz@Sun.COM 		dprintf("Pbuild_file_symtab: using in-core data for: %s\n",
168710201SEdward.Pilatowicz@Sun.COM 		    fptr->file_pname);
168810201SEdward.Pilatowicz@Sun.COM 
168910201SEdward.Pilatowicz@Sun.COM 		if ((elf = build_fake_elf(P, fptr, &ehdr, &nshdrs, &shdata)) ==
169010201SEdward.Pilatowicz@Sun.COM 		    NULL)
169110201SEdward.Pilatowicz@Sun.COM 			return;
169210201SEdward.Pilatowicz@Sun.COM 
169310201SEdward.Pilatowicz@Sun.COM 	} else if ((fptr->file_fd = open(objectfile, O_RDONLY)) < 0) {
16940Sstevel@tonic-gate 		dprintf("Pbuild_file_symtab: failed to open %s: %s\n",
16950Sstevel@tonic-gate 		    objectfile, strerror(errno));
16960Sstevel@tonic-gate 
169710201SEdward.Pilatowicz@Sun.COM 		if ((elf = build_fake_elf(P, fptr, &ehdr, &nshdrs, &shdata)) ==
169810201SEdward.Pilatowicz@Sun.COM 		    NULL)
16990Sstevel@tonic-gate 			return;
17000Sstevel@tonic-gate 
17010Sstevel@tonic-gate 	} else if ((elf = elf_begin(fptr->file_fd, ELF_C_READ, NULL)) == NULL ||
17020Sstevel@tonic-gate 	    elf_kind(elf) != ELF_K_ELF ||
17030Sstevel@tonic-gate 	    gelf_getehdr(elf, &ehdr) == NULL ||
17049900SAli.Bahrami@Sun.COM 	    elf_getshdrnum(elf, &nshdrs) == -1 ||
17059900SAli.Bahrami@Sun.COM 	    elf_getshdrstrndx(elf, &shstrndx) == -1 ||
1706942Sahl 	    (scn = elf_getscn(elf, shstrndx)) == NULL ||
17070Sstevel@tonic-gate 	    (shdata = elf_getdata(scn, NULL)) == NULL) {
17082712Snn35248 		int err = elf_errno();
17092712Snn35248 
17100Sstevel@tonic-gate 		dprintf("failed to process ELF file %s: %s\n",
17112712Snn35248 		    objectfile, (err == 0) ? "<null>" : elf_errmsg(err));
171210201SEdward.Pilatowicz@Sun.COM 		(void) elf_end(elf);
171310201SEdward.Pilatowicz@Sun.COM 
171410201SEdward.Pilatowicz@Sun.COM 		if ((elf = build_fake_elf(P, fptr, &ehdr, &nshdrs, &shdata)) ==
171510201SEdward.Pilatowicz@Sun.COM 		    NULL)
171610201SEdward.Pilatowicz@Sun.COM 			return;
17170Sstevel@tonic-gate 
17180Sstevel@tonic-gate 	} else if (file_differs(P, elf, fptr)) {
17190Sstevel@tonic-gate 		Elf *newelf;
17200Sstevel@tonic-gate 
17210Sstevel@tonic-gate 		/*
17220Sstevel@tonic-gate 		 * Before we get too excited about this elf file, we'll check
17230Sstevel@tonic-gate 		 * its checksum value against the value we have in memory. If
17240Sstevel@tonic-gate 		 * they don't agree, we try to fake up a new elf file and
17250Sstevel@tonic-gate 		 * proceed with that instead.
17260Sstevel@tonic-gate 		 */
17270Sstevel@tonic-gate 		dprintf("ELF file %s (%lx) doesn't match in-core image\n",
17280Sstevel@tonic-gate 		    fptr->file_pname,
17290Sstevel@tonic-gate 		    (ulong_t)fptr->file_map->map_pmap.pr_vaddr);
17300Sstevel@tonic-gate 
173110201SEdward.Pilatowicz@Sun.COM 		if ((newelf = build_fake_elf(P, fptr, &ehdr, &nshdrs, &shdata))
173210201SEdward.Pilatowicz@Sun.COM 		    != NULL) {
17330Sstevel@tonic-gate 			(void) elf_end(elf);
17340Sstevel@tonic-gate 			elf = newelf;
17350Sstevel@tonic-gate 			dprintf("switched to faked up ELF file\n");
17360Sstevel@tonic-gate 		}
17370Sstevel@tonic-gate 	}
17380Sstevel@tonic-gate 
1739942Sahl 	if ((cache = malloc(nshdrs * sizeof (*cache))) == NULL) {
17400Sstevel@tonic-gate 		dprintf("failed to malloc section cache for %s\n", objectfile);
17410Sstevel@tonic-gate 		goto bad;
17420Sstevel@tonic-gate 	}
17430Sstevel@tonic-gate 
17440Sstevel@tonic-gate 	dprintf("processing ELF file %s\n", objectfile);
17450Sstevel@tonic-gate 	fptr->file_class = ehdr.e_ident[EI_CLASS];
17460Sstevel@tonic-gate 	fptr->file_etype = ehdr.e_type;
17470Sstevel@tonic-gate 	fptr->file_elf = elf;
17481160Svb160487 	fptr->file_shstrs = shdata->d_buf;
17491160Svb160487 	fptr->file_shstrsz = shdata->d_size;
17500Sstevel@tonic-gate 
17510Sstevel@tonic-gate 	/*
17520Sstevel@tonic-gate 	 * Iterate through each section, caching its section header, data
17530Sstevel@tonic-gate 	 * pointer, and name.  We use this for handling sh_link values below.
17540Sstevel@tonic-gate 	 */
17550Sstevel@tonic-gate 	for (cp = cache + 1, scn = NULL; scn = elf_nextscn(elf, scn); cp++) {
17562712Snn35248 		if (gelf_getshdr(scn, &cp->c_shdr) == NULL) {
17572712Snn35248 			dprintf("Pbuild_file_symtab: Failed to get section "
17582712Snn35248 			    "header\n");
17590Sstevel@tonic-gate 			goto bad; /* Failed to get section header */
17602712Snn35248 		}
17612712Snn35248 
17622712Snn35248 		if ((cp->c_data = elf_getdata(scn, NULL)) == NULL) {
17632712Snn35248 			dprintf("Pbuild_file_symtab: Failed to get section "
17642712Snn35248 			    "data\n");
17650Sstevel@tonic-gate 			goto bad; /* Failed to get section data */
17662712Snn35248 		}
17672712Snn35248 
17682712Snn35248 		if (cp->c_shdr.sh_name >= shdata->d_size) {
17692712Snn35248 			dprintf("Pbuild_file_symtab: corrupt section name");
17700Sstevel@tonic-gate 			goto bad; /* Corrupt section name */
17712712Snn35248 		}
17720Sstevel@tonic-gate 
17730Sstevel@tonic-gate 		cp->c_name = (const char *)shdata->d_buf + cp->c_shdr.sh_name;
17740Sstevel@tonic-gate 	}
17750Sstevel@tonic-gate 
17760Sstevel@tonic-gate 	/*
17770Sstevel@tonic-gate 	 * Now iterate through the section cache in order to locate info
17783347Sab196087 	 * for the .symtab, .dynsym, .SUNW_ldynsym, .dynamic, .plt,
17793347Sab196087 	 * and .SUNW_ctf sections:
17800Sstevel@tonic-gate 	 */
1781942Sahl 	for (i = 1, cp = cache + 1; i < nshdrs; i++, cp++) {
17820Sstevel@tonic-gate 		GElf_Shdr *shp = &cp->c_shdr;
17830Sstevel@tonic-gate 
17840Sstevel@tonic-gate 		if (shp->sh_type == SHT_SYMTAB || shp->sh_type == SHT_DYNSYM) {
17850Sstevel@tonic-gate 			sym_tbl_t *symp = shp->sh_type == SHT_SYMTAB ?
17860Sstevel@tonic-gate 			    &fptr->file_symtab : &fptr->file_dynsym;
17870Sstevel@tonic-gate 			/*
17880Sstevel@tonic-gate 			 * It's possible that the we already got the symbol
17890Sstevel@tonic-gate 			 * table from the core file itself. Either the file
17900Sstevel@tonic-gate 			 * differs in which case our faked up elf file will
17910Sstevel@tonic-gate 			 * only contain the dynsym (not the symtab) or the
17920Sstevel@tonic-gate 			 * file matches in which case we'll just be replacing
17930Sstevel@tonic-gate 			 * the symbol table we pulled out of the core file
17940Sstevel@tonic-gate 			 * with an equivalent one. In either case, this
17950Sstevel@tonic-gate 			 * check isn't essential, but it's a good idea.
17960Sstevel@tonic-gate 			 */
17973347Sab196087 			if (symp->sym_data_pri == NULL) {
17982712Snn35248 				dprintf("Symbol table found for %s\n",
17992712Snn35248 				    objectfile);
18003347Sab196087 				symp->sym_data_pri = cp->c_data;
18013347Sab196087 				symp->sym_symn +=
18023347Sab196087 				    shp->sh_size / shp->sh_entsize;
18030Sstevel@tonic-gate 				symp->sym_strs =
18040Sstevel@tonic-gate 				    cache[shp->sh_link].c_data->d_buf;
18050Sstevel@tonic-gate 				symp->sym_strsz =
18060Sstevel@tonic-gate 				    cache[shp->sh_link].c_data->d_size;
18073347Sab196087 				symp->sym_hdr_pri = cp->c_shdr;
18080Sstevel@tonic-gate 				symp->sym_strhdr = cache[shp->sh_link].c_shdr;
18092712Snn35248 			} else {
18102712Snn35248 				dprintf("Symbol table already there for %s\n",
18112712Snn35248 				    objectfile);
18120Sstevel@tonic-gate 			}
18133347Sab196087 		} else if (shp->sh_type == SHT_SUNW_LDYNSYM) {
18143347Sab196087 			/* .SUNW_ldynsym section is auxiliary to .dynsym */
18153347Sab196087 			if (fptr->file_dynsym.sym_data_aux == NULL) {
18163347Sab196087 				dprintf(".SUNW_ldynsym symbol table"
18173347Sab196087 				    " found for %s\n", objectfile);
18183347Sab196087 				fptr->file_dynsym.sym_data_aux = cp->c_data;
18193347Sab196087 				fptr->file_dynsym.sym_symn_aux =
18203347Sab196087 				    shp->sh_size / shp->sh_entsize;
18213347Sab196087 				fptr->file_dynsym.sym_symn +=
18223347Sab196087 				    fptr->file_dynsym.sym_symn_aux;
18233347Sab196087 				fptr->file_dynsym.sym_hdr_aux = cp->c_shdr;
18243347Sab196087 			} else {
18253347Sab196087 				dprintf(".SUNW_ldynsym symbol table already"
18263347Sab196087 				    " there for %s\n", objectfile);
18273347Sab196087 			}
18280Sstevel@tonic-gate 		} else if (shp->sh_type == SHT_DYNAMIC) {
18290Sstevel@tonic-gate 			dyn = cp;
18300Sstevel@tonic-gate 		} else if (strcmp(cp->c_name, ".plt") == 0) {
18310Sstevel@tonic-gate 			plt = cp;
18320Sstevel@tonic-gate 		} else if (strcmp(cp->c_name, ".SUNW_ctf") == 0) {
18330Sstevel@tonic-gate 			/*
18340Sstevel@tonic-gate 			 * Skip over bogus CTF sections so they don't come back
18350Sstevel@tonic-gate 			 * to haunt us later.
18360Sstevel@tonic-gate 			 */
18370Sstevel@tonic-gate 			if (shp->sh_link == 0 ||
1838942Sahl 			    shp->sh_link >= nshdrs ||
18390Sstevel@tonic-gate 			    (cache[shp->sh_link].c_shdr.sh_type != SHT_DYNSYM &&
18400Sstevel@tonic-gate 			    cache[shp->sh_link].c_shdr.sh_type != SHT_SYMTAB)) {
18410Sstevel@tonic-gate 				dprintf("Bad sh_link %d for "
18420Sstevel@tonic-gate 				    "CTF\n", shp->sh_link);
18430Sstevel@tonic-gate 				continue;
18440Sstevel@tonic-gate 			}
18450Sstevel@tonic-gate 			ctf = cp;
18460Sstevel@tonic-gate 		}
18470Sstevel@tonic-gate 	}
18480Sstevel@tonic-gate 
18490Sstevel@tonic-gate 	/*
18500Sstevel@tonic-gate 	 * At this point, we've found all the symbol tables we're ever going
18510Sstevel@tonic-gate 	 * to find: the ones in the loop above and possibly the symtab that
18520Sstevel@tonic-gate 	 * was included in the core file. Before we perform any lookups, we
18530Sstevel@tonic-gate 	 * create sorted versions to optimize for lookups.
18540Sstevel@tonic-gate 	 */
18550Sstevel@tonic-gate 	optimize_symtab(&fptr->file_symtab);
18560Sstevel@tonic-gate 	optimize_symtab(&fptr->file_dynsym);
18570Sstevel@tonic-gate 
18580Sstevel@tonic-gate 	/*
18590Sstevel@tonic-gate 	 * Fill in the base address of the text mapping for shared libraries.
18600Sstevel@tonic-gate 	 * This allows us to translate symbols before librtld_db is ready.
18610Sstevel@tonic-gate 	 */
18620Sstevel@tonic-gate 	if (fptr->file_etype == ET_DYN) {
18630Sstevel@tonic-gate 		fptr->file_dyn_base = fptr->file_map->map_pmap.pr_vaddr -
18640Sstevel@tonic-gate 		    fptr->file_map->map_pmap.pr_offset;
18652712Snn35248 		dprintf("setting file_dyn_base for %s to %lx\n",
18662712Snn35248 		    objectfile, (long)fptr->file_dyn_base);
18670Sstevel@tonic-gate 	}
18680Sstevel@tonic-gate 
18690Sstevel@tonic-gate 	/*
18700Sstevel@tonic-gate 	 * Record the CTF section information in the file info structure.
18710Sstevel@tonic-gate 	 */
18720Sstevel@tonic-gate 	if (ctf != NULL) {
18730Sstevel@tonic-gate 		fptr->file_ctf_off = ctf->c_shdr.sh_offset;
18740Sstevel@tonic-gate 		fptr->file_ctf_size = ctf->c_shdr.sh_size;
18750Sstevel@tonic-gate 		if (ctf->c_shdr.sh_link != 0 &&
18760Sstevel@tonic-gate 		    cache[ctf->c_shdr.sh_link].c_shdr.sh_type == SHT_DYNSYM)
18770Sstevel@tonic-gate 			fptr->file_ctf_dyn = 1;
18780Sstevel@tonic-gate 	}
18790Sstevel@tonic-gate 
18800Sstevel@tonic-gate 	if (fptr->file_lo == NULL)
18810Sstevel@tonic-gate 		goto done; /* Nothing else to do if no load object info */
18820Sstevel@tonic-gate 
18830Sstevel@tonic-gate 	/*
18840Sstevel@tonic-gate 	 * If the object is a shared library and we have a different rl_base
18850Sstevel@tonic-gate 	 * value, reset file_dyn_base according to librtld_db's information.
18860Sstevel@tonic-gate 	 */
18870Sstevel@tonic-gate 	if (fptr->file_etype == ET_DYN &&
18880Sstevel@tonic-gate 	    fptr->file_lo->rl_base != fptr->file_dyn_base) {
18892712Snn35248 		dprintf("resetting file_dyn_base for %s to %lx\n",
18902712Snn35248 		    objectfile, (long)fptr->file_lo->rl_base);
18910Sstevel@tonic-gate 		fptr->file_dyn_base = fptr->file_lo->rl_base;
18920Sstevel@tonic-gate 	}
18930Sstevel@tonic-gate 
18940Sstevel@tonic-gate 	/*
18950Sstevel@tonic-gate 	 * Fill in the PLT information for this file if a PLT symbol is found.
18960Sstevel@tonic-gate 	 */
18970Sstevel@tonic-gate 	if (sym_by_name(&fptr->file_dynsym, "_PROCEDURE_LINKAGE_TABLE_", &s,
18980Sstevel@tonic-gate 	    NULL) != NULL) {
18990Sstevel@tonic-gate 		fptr->file_plt_base = s.st_value + fptr->file_dyn_base;
19000Sstevel@tonic-gate 		fptr->file_plt_size = (plt != NULL) ? plt->c_shdr.sh_size : 0;
19010Sstevel@tonic-gate 
19020Sstevel@tonic-gate 		/*
19030Sstevel@tonic-gate 		 * Bring the load object up to date; it is the only way the
19040Sstevel@tonic-gate 		 * user has to access the PLT data. The PLT information in the
19050Sstevel@tonic-gate 		 * rd_loadobj_t is not set in the call to map_iter() (the
19060Sstevel@tonic-gate 		 * callback for rd_loadobj_iter) where we set file_lo.
19070Sstevel@tonic-gate 		 */
19080Sstevel@tonic-gate 		fptr->file_lo->rl_plt_base = fptr->file_plt_base;
19090Sstevel@tonic-gate 		fptr->file_lo->rl_plt_size = fptr->file_plt_size;
19100Sstevel@tonic-gate 
19110Sstevel@tonic-gate 		dprintf("PLT found at %p, size = %lu\n",
19120Sstevel@tonic-gate 		    (void *)fptr->file_plt_base, (ulong_t)fptr->file_plt_size);
19130Sstevel@tonic-gate 	}
19140Sstevel@tonic-gate 
19150Sstevel@tonic-gate 	/*
19160Sstevel@tonic-gate 	 * Fill in the PLT information.
19170Sstevel@tonic-gate 	 */
19180Sstevel@tonic-gate 	if (dyn != NULL) {
19190Sstevel@tonic-gate 		uintptr_t dynaddr = dyn->c_shdr.sh_addr + fptr->file_dyn_base;
19200Sstevel@tonic-gate 		size_t ndyn = dyn->c_shdr.sh_size / dyn->c_shdr.sh_entsize;
19210Sstevel@tonic-gate 		GElf_Dyn d;
19220Sstevel@tonic-gate 
19230Sstevel@tonic-gate 		for (i = 0; i < ndyn; i++) {
192410201SEdward.Pilatowicz@Sun.COM 			if (gelf_getdyn(dyn->c_data, i, &d) == NULL)
192510201SEdward.Pilatowicz@Sun.COM 				continue;
192610201SEdward.Pilatowicz@Sun.COM 
192710201SEdward.Pilatowicz@Sun.COM 			switch (d.d_tag) {
192810201SEdward.Pilatowicz@Sun.COM 			case DT_JMPREL:
19292712Snn35248 				dprintf("DT_JMPREL is %p\n",
19302712Snn35248 				    (void *)(uintptr_t)d.d_un.d_ptr);
19310Sstevel@tonic-gate 				fptr->file_jmp_rel =
19320Sstevel@tonic-gate 				    d.d_un.d_ptr + fptr->file_dyn_base;
19330Sstevel@tonic-gate 				break;
193410201SEdward.Pilatowicz@Sun.COM 			case DT_STRTAB:
193510201SEdward.Pilatowicz@Sun.COM 				dprintf("DT_STRTAB is %p\n",
193610201SEdward.Pilatowicz@Sun.COM 				    (void *)(uintptr_t)d.d_un.d_ptr);
193710201SEdward.Pilatowicz@Sun.COM 				break;
193810201SEdward.Pilatowicz@Sun.COM 			case DT_PLTGOT:
193910201SEdward.Pilatowicz@Sun.COM 				dprintf("DT_PLTGOT is %p\n",
194010201SEdward.Pilatowicz@Sun.COM 				    (void *)(uintptr_t)d.d_un.d_ptr);
194110201SEdward.Pilatowicz@Sun.COM 				break;
194210201SEdward.Pilatowicz@Sun.COM 			case DT_SUNW_SYMTAB:
194310201SEdward.Pilatowicz@Sun.COM 				dprintf("DT_SUNW_SYMTAB is %p\n",
194410201SEdward.Pilatowicz@Sun.COM 				    (void *)(uintptr_t)d.d_un.d_ptr);
194510201SEdward.Pilatowicz@Sun.COM 				break;
194610201SEdward.Pilatowicz@Sun.COM 			case DT_SYMTAB:
194710201SEdward.Pilatowicz@Sun.COM 				dprintf("DT_SYMTAB is %p\n",
194810201SEdward.Pilatowicz@Sun.COM 				    (void *)(uintptr_t)d.d_un.d_ptr);
194910201SEdward.Pilatowicz@Sun.COM 				break;
195010201SEdward.Pilatowicz@Sun.COM 			case DT_HASH:
195110201SEdward.Pilatowicz@Sun.COM 				dprintf("DT_HASH is %p\n",
195210201SEdward.Pilatowicz@Sun.COM 				    (void *)(uintptr_t)d.d_un.d_ptr);
195310201SEdward.Pilatowicz@Sun.COM 				break;
19540Sstevel@tonic-gate 			}
19550Sstevel@tonic-gate 		}
19560Sstevel@tonic-gate 
19570Sstevel@tonic-gate 		dprintf("_DYNAMIC found at %p, %lu entries, DT_JMPREL = %p\n",
19580Sstevel@tonic-gate 		    (void *)dynaddr, (ulong_t)ndyn, (void *)fptr->file_jmp_rel);
19590Sstevel@tonic-gate 	}
19600Sstevel@tonic-gate 
19610Sstevel@tonic-gate done:
19620Sstevel@tonic-gate 	free(cache);
19630Sstevel@tonic-gate 	return;
19640Sstevel@tonic-gate 
19650Sstevel@tonic-gate bad:
19660Sstevel@tonic-gate 	if (cache != NULL)
19670Sstevel@tonic-gate 		free(cache);
19680Sstevel@tonic-gate 
19690Sstevel@tonic-gate 	(void) elf_end(elf);
19700Sstevel@tonic-gate 	fptr->file_elf = NULL;
19710Sstevel@tonic-gate 	if (fptr->file_elfmem != NULL) {
19720Sstevel@tonic-gate 		free(fptr->file_elfmem);
19730Sstevel@tonic-gate 		fptr->file_elfmem = NULL;
19740Sstevel@tonic-gate 	}
19750Sstevel@tonic-gate 	(void) close(fptr->file_fd);
19760Sstevel@tonic-gate 	fptr->file_fd = -1;
19770Sstevel@tonic-gate }
19780Sstevel@tonic-gate 
19790Sstevel@tonic-gate /*
19800Sstevel@tonic-gate  * Given a process virtual address, return the map_info_t containing it.
19810Sstevel@tonic-gate  * If none found, return NULL.
19820Sstevel@tonic-gate  */
19830Sstevel@tonic-gate map_info_t *
Paddr2mptr(struct ps_prochandle * P,uintptr_t addr)19840Sstevel@tonic-gate Paddr2mptr(struct ps_prochandle *P, uintptr_t addr)
19850Sstevel@tonic-gate {
19860Sstevel@tonic-gate 	int lo = 0;
19870Sstevel@tonic-gate 	int hi = P->map_count - 1;
19880Sstevel@tonic-gate 	int mid;
19890Sstevel@tonic-gate 	map_info_t *mp;
19900Sstevel@tonic-gate 
19910Sstevel@tonic-gate 	while (lo <= hi) {
19920Sstevel@tonic-gate 
19930Sstevel@tonic-gate 		mid = (lo + hi) / 2;
19940Sstevel@tonic-gate 		mp = &P->mappings[mid];
19950Sstevel@tonic-gate 
19960Sstevel@tonic-gate 		/* check that addr is in [vaddr, vaddr + size) */
19970Sstevel@tonic-gate 		if ((addr - mp->map_pmap.pr_vaddr) < mp->map_pmap.pr_size)
19980Sstevel@tonic-gate 			return (mp);
19990Sstevel@tonic-gate 
20000Sstevel@tonic-gate 		if (addr < mp->map_pmap.pr_vaddr)
20010Sstevel@tonic-gate 			hi = mid - 1;
20020Sstevel@tonic-gate 		else
20030Sstevel@tonic-gate 			lo = mid + 1;
20040Sstevel@tonic-gate 	}
20050Sstevel@tonic-gate 
20060Sstevel@tonic-gate 	return (NULL);
20070Sstevel@tonic-gate }
20080Sstevel@tonic-gate 
20090Sstevel@tonic-gate /*
20100Sstevel@tonic-gate  * Return the map_info_t for the executable file.
20110Sstevel@tonic-gate  * If not found, return NULL.
20120Sstevel@tonic-gate  */
20130Sstevel@tonic-gate static map_info_t *
exec_map(struct ps_prochandle * P)20140Sstevel@tonic-gate exec_map(struct ps_prochandle *P)
20150Sstevel@tonic-gate {
20160Sstevel@tonic-gate 	uint_t i;
20170Sstevel@tonic-gate 	map_info_t *mptr;
20180Sstevel@tonic-gate 	map_info_t *mold = NULL;
20190Sstevel@tonic-gate 	file_info_t *fptr;
20200Sstevel@tonic-gate 	uintptr_t base;
20210Sstevel@tonic-gate 
20220Sstevel@tonic-gate 	for (i = 0, mptr = P->mappings; i < P->map_count; i++, mptr++) {
20230Sstevel@tonic-gate 		if (mptr->map_pmap.pr_mapname[0] == '\0')
20240Sstevel@tonic-gate 			continue;
20250Sstevel@tonic-gate 		if (strcmp(mptr->map_pmap.pr_mapname, "a.out") == 0) {
20260Sstevel@tonic-gate 			if ((fptr = mptr->map_file) != NULL &&
20270Sstevel@tonic-gate 			    fptr->file_lo != NULL) {
20280Sstevel@tonic-gate 				base = fptr->file_lo->rl_base;
20290Sstevel@tonic-gate 				if (base >= mptr->map_pmap.pr_vaddr &&
20300Sstevel@tonic-gate 				    base < mptr->map_pmap.pr_vaddr +
20310Sstevel@tonic-gate 				    mptr->map_pmap.pr_size)	/* text space */
20320Sstevel@tonic-gate 					return (mptr);
20330Sstevel@tonic-gate 				mold = mptr;	/* must be the data */
20340Sstevel@tonic-gate 				continue;
20350Sstevel@tonic-gate 			}
20360Sstevel@tonic-gate 			/* This is a poor way to test for text space */
20370Sstevel@tonic-gate 			if (!(mptr->map_pmap.pr_mflags & MA_EXEC) ||
20380Sstevel@tonic-gate 			    (mptr->map_pmap.pr_mflags & MA_WRITE)) {
20390Sstevel@tonic-gate 				mold = mptr;
20400Sstevel@tonic-gate 				continue;
20410Sstevel@tonic-gate 			}
20420Sstevel@tonic-gate 			return (mptr);
20430Sstevel@tonic-gate 		}
20440Sstevel@tonic-gate 	}
20450Sstevel@tonic-gate 
20460Sstevel@tonic-gate 	return (mold);
20470Sstevel@tonic-gate }
20480Sstevel@tonic-gate 
20490Sstevel@tonic-gate /*
20500Sstevel@tonic-gate  * Given a shared object name, return the map_info_t for it.  If no matching
20510Sstevel@tonic-gate  * object is found, return NULL.  Normally, the link maps contain the full
20520Sstevel@tonic-gate  * object pathname, e.g. /usr/lib/libc.so.1.  We allow the object name to
20530Sstevel@tonic-gate  * take one of the following forms:
20540Sstevel@tonic-gate  *
20550Sstevel@tonic-gate  * 1. An exact match (i.e. a full pathname): "/usr/lib/libc.so.1"
20560Sstevel@tonic-gate  * 2. An exact basename match: "libc.so.1"
20570Sstevel@tonic-gate  * 3. An initial basename match up to a '.' suffix: "libc.so" or "libc"
20580Sstevel@tonic-gate  * 4. The literal string "a.out" is an alias for the executable mapping
20590Sstevel@tonic-gate  *
20600Sstevel@tonic-gate  * The third case is a convenience for callers and may not be necessary.
20610Sstevel@tonic-gate  *
20620Sstevel@tonic-gate  * As the exact same object name may be loaded on different link maps (see
20630Sstevel@tonic-gate  * dlmopen(3DL)), we also allow the caller to resolve the object name by
20640Sstevel@tonic-gate  * specifying a particular link map id.  If lmid is PR_LMID_EVERY, the
20650Sstevel@tonic-gate  * first matching name will be returned, regardless of the link map id.
20660Sstevel@tonic-gate  */
20670Sstevel@tonic-gate static map_info_t *
object_to_map(struct ps_prochandle * P,Lmid_t lmid,const char * objname)20680Sstevel@tonic-gate object_to_map(struct ps_prochandle *P, Lmid_t lmid, const char *objname)
20690Sstevel@tonic-gate {
20700Sstevel@tonic-gate 	map_info_t *mp;
20710Sstevel@tonic-gate 	file_info_t *fp;
20720Sstevel@tonic-gate 	size_t objlen;
20730Sstevel@tonic-gate 	uint_t i;
20740Sstevel@tonic-gate 
20750Sstevel@tonic-gate 	/*
20762712Snn35248 	 * If we have no rtld_db, then always treat a request as one for all
20772712Snn35248 	 * link maps.
20782712Snn35248 	 */
20792712Snn35248 	if (P->rap == NULL)
20802712Snn35248 		lmid = PR_LMID_EVERY;
20812712Snn35248 
20822712Snn35248 	/*
20830Sstevel@tonic-gate 	 * First pass: look for exact matches of the entire pathname or
20840Sstevel@tonic-gate 	 * basename (cases 1 and 2 above):
20850Sstevel@tonic-gate 	 */
20860Sstevel@tonic-gate 	for (i = 0, mp = P->mappings; i < P->map_count; i++, mp++) {
20870Sstevel@tonic-gate 
20880Sstevel@tonic-gate 		if (mp->map_pmap.pr_mapname[0] == '\0' ||
20897675SEdward.Pilatowicz@Sun.COM 		    (fp = mp->map_file) == NULL ||
20907675SEdward.Pilatowicz@Sun.COM 		    ((fp->file_lname == NULL) && (fp->file_rname == NULL)))
20910Sstevel@tonic-gate 			continue;
20920Sstevel@tonic-gate 
20930Sstevel@tonic-gate 		if (lmid != PR_LMID_EVERY &&
20940Sstevel@tonic-gate 		    (fp->file_lo == NULL || lmid != fp->file_lo->rl_lmident))
20950Sstevel@tonic-gate 			continue;
20960Sstevel@tonic-gate 
20970Sstevel@tonic-gate 		/*
20980Sstevel@tonic-gate 		 * If we match, return the primary text mapping; otherwise
20990Sstevel@tonic-gate 		 * just return the mapping we matched.
21000Sstevel@tonic-gate 		 */
21017675SEdward.Pilatowicz@Sun.COM 		if ((fp->file_lbase && strcmp(fp->file_lbase, objname) == 0) ||
21027675SEdward.Pilatowicz@Sun.COM 		    (fp->file_rbase && strcmp(fp->file_rbase, objname) == 0) ||
21037675SEdward.Pilatowicz@Sun.COM 		    (fp->file_lname && strcmp(fp->file_lname, objname) == 0) ||
21047675SEdward.Pilatowicz@Sun.COM 		    (fp->file_rname && strcmp(fp->file_rname, objname) == 0))
21050Sstevel@tonic-gate 			return (fp->file_map ? fp->file_map : mp);
21060Sstevel@tonic-gate 	}
21070Sstevel@tonic-gate 
21080Sstevel@tonic-gate 	objlen = strlen(objname);
21090Sstevel@tonic-gate 
21100Sstevel@tonic-gate 	/*
21110Sstevel@tonic-gate 	 * Second pass: look for partial matches (case 3 above):
21120Sstevel@tonic-gate 	 */
21130Sstevel@tonic-gate 	for (i = 0, mp = P->mappings; i < P->map_count; i++, mp++) {
21140Sstevel@tonic-gate 
21150Sstevel@tonic-gate 		if (mp->map_pmap.pr_mapname[0] == '\0' ||
21167675SEdward.Pilatowicz@Sun.COM 		    (fp = mp->map_file) == NULL ||
21177675SEdward.Pilatowicz@Sun.COM 		    ((fp->file_lname == NULL) && (fp->file_rname == NULL)))
21180Sstevel@tonic-gate 			continue;
21190Sstevel@tonic-gate 
21200Sstevel@tonic-gate 		if (lmid != PR_LMID_EVERY &&
21210Sstevel@tonic-gate 		    (fp->file_lo == NULL || lmid != fp->file_lo->rl_lmident))
21220Sstevel@tonic-gate 			continue;
21230Sstevel@tonic-gate 
21240Sstevel@tonic-gate 		/*
21250Sstevel@tonic-gate 		 * If we match, return the primary text mapping; otherwise
21260Sstevel@tonic-gate 		 * just return the mapping we matched.
21270Sstevel@tonic-gate 		 */
21287675SEdward.Pilatowicz@Sun.COM 		if ((fp->file_lbase != NULL) &&
21297675SEdward.Pilatowicz@Sun.COM 		    (strncmp(fp->file_lbase, objname, objlen) == 0) &&
21307675SEdward.Pilatowicz@Sun.COM 		    (fp->file_lbase[objlen] == '.'))
21317675SEdward.Pilatowicz@Sun.COM 			return (fp->file_map ? fp->file_map : mp);
21327675SEdward.Pilatowicz@Sun.COM 		if ((fp->file_rbase != NULL) &&
21337675SEdward.Pilatowicz@Sun.COM 		    (strncmp(fp->file_rbase, objname, objlen) == 0) &&
21347675SEdward.Pilatowicz@Sun.COM 		    (fp->file_rbase[objlen] == '.'))
21350Sstevel@tonic-gate 			return (fp->file_map ? fp->file_map : mp);
21360Sstevel@tonic-gate 	}
21370Sstevel@tonic-gate 
21380Sstevel@tonic-gate 	/*
21390Sstevel@tonic-gate 	 * One last check: we allow "a.out" to always alias the executable,
21400Sstevel@tonic-gate 	 * assuming this name was not in use for something else.
21410Sstevel@tonic-gate 	 */
21420Sstevel@tonic-gate 	if ((lmid == PR_LMID_EVERY || lmid == LM_ID_BASE) &&
21430Sstevel@tonic-gate 	    (strcmp(objname, "a.out") == 0))
21440Sstevel@tonic-gate 		return (P->map_exec);
21450Sstevel@tonic-gate 
21460Sstevel@tonic-gate 	return (NULL);
21470Sstevel@tonic-gate }
21480Sstevel@tonic-gate 
21490Sstevel@tonic-gate static map_info_t *
object_name_to_map(struct ps_prochandle * P,Lmid_t lmid,const char * name)21500Sstevel@tonic-gate object_name_to_map(struct ps_prochandle *P, Lmid_t lmid, const char *name)
21510Sstevel@tonic-gate {
21520Sstevel@tonic-gate 	map_info_t *mptr;
21530Sstevel@tonic-gate 
21540Sstevel@tonic-gate 	if (!P->info_valid)
21550Sstevel@tonic-gate 		Pupdate_maps(P);
21560Sstevel@tonic-gate 
21570Sstevel@tonic-gate 	if (P->map_exec == NULL && ((mptr = Paddr2mptr(P,
21580Sstevel@tonic-gate 	    Pgetauxval(P, AT_ENTRY))) != NULL || (mptr = exec_map(P)) != NULL))
21590Sstevel@tonic-gate 		P->map_exec = mptr;
21600Sstevel@tonic-gate 
21610Sstevel@tonic-gate 	if (P->map_ldso == NULL && (mptr = Paddr2mptr(P,
21620Sstevel@tonic-gate 	    Pgetauxval(P, AT_BASE))) != NULL)
21630Sstevel@tonic-gate 		P->map_ldso = mptr;
21640Sstevel@tonic-gate 
21650Sstevel@tonic-gate 	if (name == PR_OBJ_EXEC)
21660Sstevel@tonic-gate 		mptr = P->map_exec;
21670Sstevel@tonic-gate 	else if (name == PR_OBJ_LDSO)
21680Sstevel@tonic-gate 		mptr = P->map_ldso;
21693181Sjhaslam 	else if (Prd_agent(P) != NULL || P->state == PS_IDLE)
21703181Sjhaslam 		mptr = object_to_map(P, lmid, name);
21712712Snn35248 	else
21723181Sjhaslam 		mptr = NULL;
21730Sstevel@tonic-gate 
21740Sstevel@tonic-gate 	return (mptr);
21750Sstevel@tonic-gate }
21760Sstevel@tonic-gate 
21770Sstevel@tonic-gate /*
21780Sstevel@tonic-gate  * When two symbols are found by address, decide which one is to be preferred.
21790Sstevel@tonic-gate  */
21800Sstevel@tonic-gate static GElf_Sym *
sym_prefer(GElf_Sym * sym1,char * name1,GElf_Sym * sym2,char * name2)21810Sstevel@tonic-gate sym_prefer(GElf_Sym *sym1, char *name1, GElf_Sym *sym2, char *name2)
21820Sstevel@tonic-gate {
21830Sstevel@tonic-gate 	/*
21840Sstevel@tonic-gate 	 * Prefer the non-NULL symbol.
21850Sstevel@tonic-gate 	 */
21860Sstevel@tonic-gate 	if (sym1 == NULL)
21870Sstevel@tonic-gate 		return (sym2);
21880Sstevel@tonic-gate 	if (sym2 == NULL)
21890Sstevel@tonic-gate 		return (sym1);
21900Sstevel@tonic-gate 
21910Sstevel@tonic-gate 	/*
21920Sstevel@tonic-gate 	 * Defer to the sort ordering...
21930Sstevel@tonic-gate 	 */
21940Sstevel@tonic-gate 	return (byaddr_cmp_common(sym1, name1, sym2, name2) <= 0 ? sym1 : sym2);
21950Sstevel@tonic-gate }
21960Sstevel@tonic-gate 
21970Sstevel@tonic-gate /*
21984753Srh87107  * Use a binary search to do the work of sym_by_addr().
21990Sstevel@tonic-gate  */
22000Sstevel@tonic-gate static GElf_Sym *
sym_by_addr_binary(sym_tbl_t * symtab,GElf_Addr addr,GElf_Sym * symp,uint_t * idp)22014753Srh87107 sym_by_addr_binary(sym_tbl_t *symtab, GElf_Addr addr, GElf_Sym *symp,
22024753Srh87107     uint_t *idp)
22030Sstevel@tonic-gate {
22040Sstevel@tonic-gate 	GElf_Sym sym, osym;
22050Sstevel@tonic-gate 	uint_t i, oid, *byaddr = symtab->sym_byaddr;
22060Sstevel@tonic-gate 	int min, max, mid, omid, found = 0;
22070Sstevel@tonic-gate 
22083347Sab196087 	if (symtab->sym_data_pri == NULL || symtab->sym_count == 0)
22090Sstevel@tonic-gate 		return (NULL);
22100Sstevel@tonic-gate 
22110Sstevel@tonic-gate 	min = 0;
22120Sstevel@tonic-gate 	max = symtab->sym_count - 1;
22130Sstevel@tonic-gate 	osym.st_value = 0;
22140Sstevel@tonic-gate 
22150Sstevel@tonic-gate 	/*
22160Sstevel@tonic-gate 	 * We can't return when we've found a match, we have to continue
22170Sstevel@tonic-gate 	 * searching for the closest matching symbol.
22180Sstevel@tonic-gate 	 */
22190Sstevel@tonic-gate 	while (min <= max) {
22200Sstevel@tonic-gate 		mid = (max + min) / 2;
22210Sstevel@tonic-gate 
22220Sstevel@tonic-gate 		i = byaddr[mid];
22233347Sab196087 		(void) symtab_getsym(symtab, i, &sym);
22240Sstevel@tonic-gate 
22250Sstevel@tonic-gate 		if (addr >= sym.st_value &&
22260Sstevel@tonic-gate 		    addr < sym.st_value + sym.st_size &&
22270Sstevel@tonic-gate 		    (!found || sym.st_value > osym.st_value)) {
22280Sstevel@tonic-gate 			osym = sym;
22290Sstevel@tonic-gate 			omid = mid;
22300Sstevel@tonic-gate 			oid = i;
22310Sstevel@tonic-gate 			found = 1;
22320Sstevel@tonic-gate 		}
22330Sstevel@tonic-gate 
22340Sstevel@tonic-gate 		if (addr < sym.st_value)
22350Sstevel@tonic-gate 			max = mid - 1;
22360Sstevel@tonic-gate 		else
22370Sstevel@tonic-gate 			min = mid + 1;
22380Sstevel@tonic-gate 	}
22390Sstevel@tonic-gate 
22400Sstevel@tonic-gate 	if (!found)
22410Sstevel@tonic-gate 		return (NULL);
22420Sstevel@tonic-gate 
22430Sstevel@tonic-gate 	/*
22440Sstevel@tonic-gate 	 * There may be many symbols with identical values so we walk
22450Sstevel@tonic-gate 	 * backward in the byaddr table to find the best match.
22460Sstevel@tonic-gate 	 */
22470Sstevel@tonic-gate 	do {
22480Sstevel@tonic-gate 		sym = osym;
22490Sstevel@tonic-gate 		i = oid;
22500Sstevel@tonic-gate 
22510Sstevel@tonic-gate 		if (omid == 0)
22520Sstevel@tonic-gate 			break;
22530Sstevel@tonic-gate 
22540Sstevel@tonic-gate 		oid = byaddr[--omid];
22553347Sab196087 		(void) symtab_getsym(symtab, oid, &osym);
22560Sstevel@tonic-gate 	} while (addr >= osym.st_value &&
22570Sstevel@tonic-gate 	    addr < sym.st_value + osym.st_size &&
22580Sstevel@tonic-gate 	    osym.st_value == sym.st_value);
22590Sstevel@tonic-gate 
22600Sstevel@tonic-gate 	*symp = sym;
22610Sstevel@tonic-gate 	if (idp != NULL)
22620Sstevel@tonic-gate 		*idp = i;
22630Sstevel@tonic-gate 	return (symp);
22640Sstevel@tonic-gate }
22650Sstevel@tonic-gate 
22660Sstevel@tonic-gate /*
22674753Srh87107  * Use a linear search to do the work of sym_by_addr().
22680Sstevel@tonic-gate  */
22690Sstevel@tonic-gate static GElf_Sym *
sym_by_addr_linear(sym_tbl_t * symtab,GElf_Addr addr,GElf_Sym * symbolp,uint_t * idp)22704753Srh87107 sym_by_addr_linear(sym_tbl_t *symtab, GElf_Addr addr, GElf_Sym *symbolp,
22714753Srh87107     uint_t *idp)
22724753Srh87107 {
22734753Srh87107 	size_t symn = symtab->sym_symn;
22744753Srh87107 	char *strs = symtab->sym_strs;
22754753Srh87107 	GElf_Sym sym, *symp = NULL;
22764753Srh87107 	GElf_Sym osym, *osymp = NULL;
22774753Srh87107 	int i, id;
22784753Srh87107 
22794753Srh87107 	if (symtab->sym_data_pri == NULL || symn == 0 || strs == NULL)
22804753Srh87107 		return (NULL);
22814753Srh87107 
22824753Srh87107 	for (i = 0; i < symn; i++) {
22834753Srh87107 		if ((symp = symtab_getsym(symtab, i, &sym)) != NULL) {
22844753Srh87107 			if (addr >= sym.st_value &&
22854753Srh87107 			    addr < sym.st_value + sym.st_size) {
22864753Srh87107 				if (osymp)
22874753Srh87107 					symp = sym_prefer(
22884753Srh87107 					    symp, strs + symp->st_name,
22894753Srh87107 					    osymp, strs + osymp->st_name);
22904753Srh87107 				if (symp != osymp) {
22914753Srh87107 					osym = sym;
22924753Srh87107 					osymp = &osym;
22934753Srh87107 					id = i;
22944753Srh87107 				}
22954753Srh87107 			}
22964753Srh87107 		}
22974753Srh87107 	}
22984753Srh87107 	if (osymp) {
22994753Srh87107 		*symbolp = osym;
23004753Srh87107 		if (idp)
23014753Srh87107 			*idp = id;
23024753Srh87107 		return (symbolp);
23034753Srh87107 	}
23044753Srh87107 	return (NULL);
23054753Srh87107 }
23064753Srh87107 
23074753Srh87107 /*
23084753Srh87107  * Look up a symbol by address in the specified symbol table.
23094753Srh87107  * Adjustment to 'addr' must already have been made for the
23104753Srh87107  * offset of the symbol if this is a dynamic library symbol table.
23114753Srh87107  *
23124753Srh87107  * Use a linear or a binary search depending on whether or not we
23134753Srh87107  * chose to sort the table in optimize_symtab().
23144753Srh87107  */
23154753Srh87107 static GElf_Sym *
sym_by_addr(sym_tbl_t * symtab,GElf_Addr addr,GElf_Sym * symp,uint_t * idp)23164753Srh87107 sym_by_addr(sym_tbl_t *symtab, GElf_Addr addr, GElf_Sym *symp, uint_t *idp)
23174753Srh87107 {
23184753Srh87107 	if (_libproc_no_qsort) {
23194753Srh87107 		return (sym_by_addr_linear(symtab, addr, symp, idp));
23204753Srh87107 	} else {
23214753Srh87107 		return (sym_by_addr_binary(symtab, addr, symp, idp));
23224753Srh87107 	}
23234753Srh87107 }
23244753Srh87107 
23254753Srh87107 /*
23264753Srh87107  * Use a binary search to do the work of sym_by_name().
23274753Srh87107  */
23284753Srh87107 static GElf_Sym *
sym_by_name_binary(sym_tbl_t * symtab,const char * name,GElf_Sym * symp,uint_t * idp)23294753Srh87107 sym_by_name_binary(sym_tbl_t *symtab, const char *name, GElf_Sym *symp,
23304753Srh87107     uint_t *idp)
23310Sstevel@tonic-gate {
23320Sstevel@tonic-gate 	char *strs = symtab->sym_strs;
23330Sstevel@tonic-gate 	uint_t i, *byname = symtab->sym_byname;
23340Sstevel@tonic-gate 	int min, mid, max, cmp;
23350Sstevel@tonic-gate 
23363347Sab196087 	if (symtab->sym_data_pri == NULL || strs == NULL ||
23373347Sab196087 	    symtab->sym_count == 0)
23380Sstevel@tonic-gate 		return (NULL);
23390Sstevel@tonic-gate 
23400Sstevel@tonic-gate 	min = 0;
23410Sstevel@tonic-gate 	max = symtab->sym_count - 1;
23420Sstevel@tonic-gate 
23430Sstevel@tonic-gate 	while (min <= max) {
23440Sstevel@tonic-gate 		mid = (max + min) / 2;
23450Sstevel@tonic-gate 
23460Sstevel@tonic-gate 		i = byname[mid];
23473347Sab196087 		(void) symtab_getsym(symtab, i, symp);
23480Sstevel@tonic-gate 
23490Sstevel@tonic-gate 		if ((cmp = strcmp(name, strs + symp->st_name)) == 0) {
23500Sstevel@tonic-gate 			if (idp != NULL)
23510Sstevel@tonic-gate 				*idp = i;
23520Sstevel@tonic-gate 			return (symp);
23530Sstevel@tonic-gate 		}
23540Sstevel@tonic-gate 
23550Sstevel@tonic-gate 		if (cmp < 0)
23560Sstevel@tonic-gate 			max = mid - 1;
23570Sstevel@tonic-gate 		else
23580Sstevel@tonic-gate 			min = mid + 1;
23590Sstevel@tonic-gate 	}
23600Sstevel@tonic-gate 
23610Sstevel@tonic-gate 	return (NULL);
23620Sstevel@tonic-gate }
23630Sstevel@tonic-gate 
23640Sstevel@tonic-gate /*
23654753Srh87107  * Use a linear search to do the work of sym_by_name().
23664753Srh87107  */
23674753Srh87107 static GElf_Sym *
sym_by_name_linear(sym_tbl_t * symtab,const char * name,GElf_Sym * symp,uint_t * idp)23684753Srh87107 sym_by_name_linear(sym_tbl_t *symtab, const char *name, GElf_Sym *symp,
23694753Srh87107     uint_t *idp)
23704753Srh87107 {
23714753Srh87107 	size_t symn = symtab->sym_symn;
23724753Srh87107 	char *strs = symtab->sym_strs;
23734753Srh87107 	int i;
23744753Srh87107 
23754753Srh87107 	if (symtab->sym_data_pri == NULL || symn == 0 || strs == NULL)
23764753Srh87107 		return (NULL);
23774753Srh87107 
23784753Srh87107 	for (i = 0; i < symn; i++) {
23794753Srh87107 		if (symtab_getsym(symtab, i, symp) &&
23804753Srh87107 		    strcmp(name, strs + symp->st_name) == 0) {
23814753Srh87107 			if (idp)
23824753Srh87107 				*idp = i;
23834753Srh87107 			return (symp);
23844753Srh87107 		}
23854753Srh87107 	}
23864753Srh87107 
23874753Srh87107 	return (NULL);
23884753Srh87107 }
23894753Srh87107 
23904753Srh87107 /*
23914753Srh87107  * Look up a symbol by name in the specified symbol table.
23924753Srh87107  *
23934753Srh87107  * Use a linear or a binary search depending on whether or not we
23944753Srh87107  * chose to sort the table in optimize_symtab().
23954753Srh87107  */
23964753Srh87107 static GElf_Sym *
sym_by_name(sym_tbl_t * symtab,const char * name,GElf_Sym * symp,uint_t * idp)23974753Srh87107 sym_by_name(sym_tbl_t *symtab, const char *name, GElf_Sym *symp, uint_t *idp)
23984753Srh87107 {
23994753Srh87107 	if (_libproc_no_qsort) {
24004753Srh87107 		return (sym_by_name_linear(symtab, name, symp, idp));
24014753Srh87107 	} else {
24024753Srh87107 		return (sym_by_name_binary(symtab, name, symp, idp));
24034753Srh87107 	}
24044753Srh87107 }
24054753Srh87107 
24064753Srh87107 /*
24070Sstevel@tonic-gate  * Search the process symbol tables looking for a symbol whose
24080Sstevel@tonic-gate  * value to value+size contain the address specified by addr.
24090Sstevel@tonic-gate  * Return values are:
24100Sstevel@tonic-gate  *	sym_name_buffer containing the symbol name
24110Sstevel@tonic-gate  *	GElf_Sym symbol table entry
24120Sstevel@tonic-gate  *	prsyminfo_t ancillary symbol information
24130Sstevel@tonic-gate  * Returns 0 on success, -1 on failure.
24140Sstevel@tonic-gate  */
24157675SEdward.Pilatowicz@Sun.COM static int
i_Pxlookup_by_addr(struct ps_prochandle * P,int lmresolve,uintptr_t addr,char * sym_name_buffer,size_t bufsize,GElf_Sym * symbolp,prsyminfo_t * sip)24167675SEdward.Pilatowicz@Sun.COM i_Pxlookup_by_addr(
24170Sstevel@tonic-gate 	struct ps_prochandle *P,
24187675SEdward.Pilatowicz@Sun.COM 	int lmresolve,			/* use resolve linker object names */
24190Sstevel@tonic-gate 	uintptr_t addr,			/* process address being sought */
24200Sstevel@tonic-gate 	char *sym_name_buffer,		/* buffer for the symbol name */
24210Sstevel@tonic-gate 	size_t bufsize,			/* size of sym_name_buffer */
24220Sstevel@tonic-gate 	GElf_Sym *symbolp,		/* returned symbol table entry */
24230Sstevel@tonic-gate 	prsyminfo_t *sip)		/* returned symbol info */
24240Sstevel@tonic-gate {
24250Sstevel@tonic-gate 	GElf_Sym	*symp;
24260Sstevel@tonic-gate 	char		*name;
24270Sstevel@tonic-gate 	GElf_Sym	sym1, *sym1p = NULL;
24280Sstevel@tonic-gate 	GElf_Sym	sym2, *sym2p = NULL;
24290Sstevel@tonic-gate 	char		*name1 = NULL;
24300Sstevel@tonic-gate 	char		*name2 = NULL;
24310Sstevel@tonic-gate 	uint_t		i1;
24320Sstevel@tonic-gate 	uint_t		i2;
24330Sstevel@tonic-gate 	map_info_t	*mptr;
24340Sstevel@tonic-gate 	file_info_t	*fptr;
24350Sstevel@tonic-gate 
24360Sstevel@tonic-gate 	(void) Prd_agent(P);
24370Sstevel@tonic-gate 
24380Sstevel@tonic-gate 	if ((mptr = Paddr2mptr(P, addr)) == NULL ||	/* no such address */
24390Sstevel@tonic-gate 	    (fptr = build_map_symtab(P, mptr)) == NULL || /* no mapped file */
24400Sstevel@tonic-gate 	    fptr->file_elf == NULL)			/* not an ELF file */
24410Sstevel@tonic-gate 		return (-1);
24420Sstevel@tonic-gate 
24430Sstevel@tonic-gate 	/*
24440Sstevel@tonic-gate 	 * Adjust the address by the load object base address in
24450Sstevel@tonic-gate 	 * case the address turns out to be in a shared library.
24460Sstevel@tonic-gate 	 */
24470Sstevel@tonic-gate 	addr -= fptr->file_dyn_base;
24480Sstevel@tonic-gate 
24490Sstevel@tonic-gate 	/*
24500Sstevel@tonic-gate 	 * Search both symbol tables, symtab first, then dynsym.
24510Sstevel@tonic-gate 	 */
24520Sstevel@tonic-gate 	if ((sym1p = sym_by_addr(&fptr->file_symtab, addr, &sym1, &i1)) != NULL)
24530Sstevel@tonic-gate 		name1 = fptr->file_symtab.sym_strs + sym1.st_name;
24540Sstevel@tonic-gate 	if ((sym2p = sym_by_addr(&fptr->file_dynsym, addr, &sym2, &i2)) != NULL)
24550Sstevel@tonic-gate 		name2 = fptr->file_dynsym.sym_strs + sym2.st_name;
24560Sstevel@tonic-gate 
24570Sstevel@tonic-gate 	if ((symp = sym_prefer(sym1p, name1, sym2p, name2)) == NULL)
24580Sstevel@tonic-gate 		return (-1);
24590Sstevel@tonic-gate 
24600Sstevel@tonic-gate 	name = (symp == sym1p) ? name1 : name2;
24610Sstevel@tonic-gate 	if (bufsize > 0) {
24620Sstevel@tonic-gate 		(void) strncpy(sym_name_buffer, name, bufsize);
24630Sstevel@tonic-gate 		sym_name_buffer[bufsize - 1] = '\0';
24640Sstevel@tonic-gate 	}
24650Sstevel@tonic-gate 
24660Sstevel@tonic-gate 	*symbolp = *symp;
24670Sstevel@tonic-gate 	if (sip != NULL) {
24680Sstevel@tonic-gate 		sip->prs_name = bufsize == 0 ? NULL : sym_name_buffer;
24697675SEdward.Pilatowicz@Sun.COM 		if (lmresolve && (fptr->file_rname != NULL))
24707675SEdward.Pilatowicz@Sun.COM 			sip->prs_object = fptr->file_rbase;
24717675SEdward.Pilatowicz@Sun.COM 		else
24727675SEdward.Pilatowicz@Sun.COM 			sip->prs_object = fptr->file_lbase;
24730Sstevel@tonic-gate 		sip->prs_id = (symp == sym1p) ? i1 : i2;
24740Sstevel@tonic-gate 		sip->prs_table = (symp == sym1p) ? PR_SYMTAB : PR_DYNSYM;
24750Sstevel@tonic-gate 		sip->prs_lmid = (fptr->file_lo == NULL) ? LM_ID_BASE :
24760Sstevel@tonic-gate 		    fptr->file_lo->rl_lmident;
24770Sstevel@tonic-gate 	}
24780Sstevel@tonic-gate 
24790Sstevel@tonic-gate 	if (GELF_ST_TYPE(symbolp->st_info) != STT_TLS)
24800Sstevel@tonic-gate 		symbolp->st_value += fptr->file_dyn_base;
24810Sstevel@tonic-gate 
24820Sstevel@tonic-gate 	return (0);
24830Sstevel@tonic-gate }
24840Sstevel@tonic-gate 
24850Sstevel@tonic-gate int
Pxlookup_by_addr(struct ps_prochandle * P,uintptr_t addr,char * buf,size_t bufsize,GElf_Sym * symp,prsyminfo_t * sip)24867675SEdward.Pilatowicz@Sun.COM Pxlookup_by_addr(struct ps_prochandle *P, uintptr_t addr, char *buf,
24877675SEdward.Pilatowicz@Sun.COM     size_t bufsize, GElf_Sym *symp, prsyminfo_t *sip)
24880Sstevel@tonic-gate {
24897675SEdward.Pilatowicz@Sun.COM 	return (i_Pxlookup_by_addr(P, B_FALSE, addr, buf, bufsize, symp, sip));
24907675SEdward.Pilatowicz@Sun.COM }
24917675SEdward.Pilatowicz@Sun.COM 
24927675SEdward.Pilatowicz@Sun.COM int
Pxlookup_by_addr_resolved(struct ps_prochandle * P,uintptr_t addr,char * buf,size_t bufsize,GElf_Sym * symp,prsyminfo_t * sip)24937675SEdward.Pilatowicz@Sun.COM Pxlookup_by_addr_resolved(struct ps_prochandle *P, uintptr_t addr, char *buf,
24947675SEdward.Pilatowicz@Sun.COM     size_t bufsize, GElf_Sym *symp, prsyminfo_t *sip)
24957675SEdward.Pilatowicz@Sun.COM {
24967675SEdward.Pilatowicz@Sun.COM 	return (i_Pxlookup_by_addr(P, B_TRUE, addr, buf, bufsize, symp, sip));
24977675SEdward.Pilatowicz@Sun.COM }
24987675SEdward.Pilatowicz@Sun.COM 
24997675SEdward.Pilatowicz@Sun.COM int
Plookup_by_addr(struct ps_prochandle * P,uintptr_t addr,char * buf,size_t size,GElf_Sym * symp)25007675SEdward.Pilatowicz@Sun.COM Plookup_by_addr(struct ps_prochandle *P, uintptr_t addr, char *buf,
25017675SEdward.Pilatowicz@Sun.COM     size_t size, GElf_Sym *symp)
25027675SEdward.Pilatowicz@Sun.COM {
25037675SEdward.Pilatowicz@Sun.COM 	return (i_Pxlookup_by_addr(P, B_FALSE, addr, buf, size, symp, NULL));
25040Sstevel@tonic-gate }
25050Sstevel@tonic-gate 
25060Sstevel@tonic-gate /*
25070Sstevel@tonic-gate  * Search the process symbol tables looking for a symbol whose name matches the
25080Sstevel@tonic-gate  * specified name and whose object and link map optionally match the specified
25090Sstevel@tonic-gate  * parameters.  On success, the function returns 0 and fills in the GElf_Sym
25100Sstevel@tonic-gate  * symbol table entry.  On failure, -1 is returned.
25110Sstevel@tonic-gate  */
25120Sstevel@tonic-gate int
Pxlookup_by_name(struct ps_prochandle * P,Lmid_t lmid,const char * oname,const char * sname,GElf_Sym * symp,prsyminfo_t * sip)25130Sstevel@tonic-gate Pxlookup_by_name(
25140Sstevel@tonic-gate 	struct ps_prochandle *P,
25150Sstevel@tonic-gate 	Lmid_t lmid,			/* link map to match, or -1 for any */
25160Sstevel@tonic-gate 	const char *oname,		/* load object name */
25170Sstevel@tonic-gate 	const char *sname,		/* symbol name */
25180Sstevel@tonic-gate 	GElf_Sym *symp,			/* returned symbol table entry */
25190Sstevel@tonic-gate 	prsyminfo_t *sip)		/* returned symbol info */
25200Sstevel@tonic-gate {
25210Sstevel@tonic-gate 	map_info_t *mptr;
25220Sstevel@tonic-gate 	file_info_t *fptr;
25230Sstevel@tonic-gate 	int cnt;
25240Sstevel@tonic-gate 
25250Sstevel@tonic-gate 	GElf_Sym sym;
25260Sstevel@tonic-gate 	prsyminfo_t si;
25270Sstevel@tonic-gate 	int rv = -1;
25280Sstevel@tonic-gate 	uint_t id;
25290Sstevel@tonic-gate 
25300Sstevel@tonic-gate 	if (oname == PR_OBJ_EVERY) {
25310Sstevel@tonic-gate 		/* create all the file_info_t's for all the mappings */
25320Sstevel@tonic-gate 		(void) Prd_agent(P);
25330Sstevel@tonic-gate 		cnt = P->num_files;
25340Sstevel@tonic-gate 		fptr = list_next(&P->file_head);
25350Sstevel@tonic-gate 	} else {
25360Sstevel@tonic-gate 		cnt = 1;
25370Sstevel@tonic-gate 		if ((mptr = object_name_to_map(P, lmid, oname)) == NULL ||
25380Sstevel@tonic-gate 		    (fptr = build_map_symtab(P, mptr)) == NULL)
25390Sstevel@tonic-gate 			return (-1);
25400Sstevel@tonic-gate 	}
25410Sstevel@tonic-gate 
25420Sstevel@tonic-gate 	/*
25430Sstevel@tonic-gate 	 * Iterate through the loaded object files and look for the symbol
25440Sstevel@tonic-gate 	 * name in the .symtab and .dynsym of each.  If we encounter a match
25450Sstevel@tonic-gate 	 * with SHN_UNDEF, keep looking in hopes of finding a better match.
25460Sstevel@tonic-gate 	 * This means that a name such as "puts" will match the puts function
25470Sstevel@tonic-gate 	 * in libc instead of matching the puts PLT entry in the a.out file.
25480Sstevel@tonic-gate 	 */
25490Sstevel@tonic-gate 	for (; cnt > 0; cnt--, fptr = list_next(fptr)) {
25500Sstevel@tonic-gate 		Pbuild_file_symtab(P, fptr);
25510Sstevel@tonic-gate 
25520Sstevel@tonic-gate 		if (fptr->file_elf == NULL)
25530Sstevel@tonic-gate 			continue;
25540Sstevel@tonic-gate 
25550Sstevel@tonic-gate 		if (lmid != PR_LMID_EVERY && fptr->file_lo != NULL &&
25560Sstevel@tonic-gate 		    lmid != fptr->file_lo->rl_lmident)
25570Sstevel@tonic-gate 			continue;
25580Sstevel@tonic-gate 
25593347Sab196087 		if (fptr->file_symtab.sym_data_pri != NULL &&
25600Sstevel@tonic-gate 		    sym_by_name(&fptr->file_symtab, sname, symp, &id)) {
25610Sstevel@tonic-gate 			if (sip != NULL) {
25620Sstevel@tonic-gate 				sip->prs_id = id;
25630Sstevel@tonic-gate 				sip->prs_table = PR_SYMTAB;
25640Sstevel@tonic-gate 				sip->prs_object = oname;
25650Sstevel@tonic-gate 				sip->prs_name = sname;
25660Sstevel@tonic-gate 				sip->prs_lmid = fptr->file_lo == NULL ?
25670Sstevel@tonic-gate 				    LM_ID_BASE : fptr->file_lo->rl_lmident;
25680Sstevel@tonic-gate 			}
25693347Sab196087 		} else if (fptr->file_dynsym.sym_data_pri != NULL &&
25700Sstevel@tonic-gate 		    sym_by_name(&fptr->file_dynsym, sname, symp, &id)) {
25710Sstevel@tonic-gate 			if (sip != NULL) {
25720Sstevel@tonic-gate 				sip->prs_id = id;
25730Sstevel@tonic-gate 				sip->prs_table = PR_DYNSYM;
25740Sstevel@tonic-gate 				sip->prs_object = oname;
25750Sstevel@tonic-gate 				sip->prs_name = sname;
25760Sstevel@tonic-gate 				sip->prs_lmid = fptr->file_lo == NULL ?
25770Sstevel@tonic-gate 				    LM_ID_BASE : fptr->file_lo->rl_lmident;
25780Sstevel@tonic-gate 			}
25790Sstevel@tonic-gate 		} else {
25800Sstevel@tonic-gate 			continue;
25810Sstevel@tonic-gate 		}
25820Sstevel@tonic-gate 
25830Sstevel@tonic-gate 		if (GELF_ST_TYPE(symp->st_info) != STT_TLS)
25840Sstevel@tonic-gate 			symp->st_value += fptr->file_dyn_base;
25850Sstevel@tonic-gate 
25860Sstevel@tonic-gate 		if (symp->st_shndx != SHN_UNDEF)
25870Sstevel@tonic-gate 			return (0);
25880Sstevel@tonic-gate 
25890Sstevel@tonic-gate 		if (rv != 0) {
25900Sstevel@tonic-gate 			if (sip != NULL)
25910Sstevel@tonic-gate 				si = *sip;
25920Sstevel@tonic-gate 			sym = *symp;
25930Sstevel@tonic-gate 			rv = 0;
25940Sstevel@tonic-gate 		}
25950Sstevel@tonic-gate 	}
25960Sstevel@tonic-gate 
25970Sstevel@tonic-gate 	if (rv == 0) {
25980Sstevel@tonic-gate 		if (sip != NULL)
25990Sstevel@tonic-gate 			*sip = si;
26000Sstevel@tonic-gate 		*symp = sym;
26010Sstevel@tonic-gate 	}
26020Sstevel@tonic-gate 
26030Sstevel@tonic-gate 	return (rv);
26040Sstevel@tonic-gate }
26050Sstevel@tonic-gate 
26060Sstevel@tonic-gate /*
26070Sstevel@tonic-gate  * Search the process symbol tables looking for a symbol whose name matches the
26080Sstevel@tonic-gate  * specified name, but without any restriction on the link map id.
26090Sstevel@tonic-gate  */
26100Sstevel@tonic-gate int
Plookup_by_name(struct ps_prochandle * P,const char * object,const char * symbol,GElf_Sym * symp)26110Sstevel@tonic-gate Plookup_by_name(struct ps_prochandle *P, const char *object,
26120Sstevel@tonic-gate 	const char *symbol, GElf_Sym *symp)
26130Sstevel@tonic-gate {
26140Sstevel@tonic-gate 	return (Pxlookup_by_name(P, PR_LMID_EVERY, object, symbol, symp, NULL));
26150Sstevel@tonic-gate }
26160Sstevel@tonic-gate 
26170Sstevel@tonic-gate /*
26180Sstevel@tonic-gate  * Iterate over the process's address space mappings.
26190Sstevel@tonic-gate  */
26207675SEdward.Pilatowicz@Sun.COM static int
i_Pmapping_iter(struct ps_prochandle * P,boolean_t lmresolve,proc_map_f * func,void * cd)26217675SEdward.Pilatowicz@Sun.COM i_Pmapping_iter(struct ps_prochandle *P, boolean_t lmresolve,
26227675SEdward.Pilatowicz@Sun.COM     proc_map_f *func, void *cd)
26230Sstevel@tonic-gate {
26240Sstevel@tonic-gate 	map_info_t *mptr;
26250Sstevel@tonic-gate 	file_info_t *fptr;
26260Sstevel@tonic-gate 	char *object_name;
26270Sstevel@tonic-gate 	int rc = 0;
26280Sstevel@tonic-gate 	int i;
26290Sstevel@tonic-gate 
26300Sstevel@tonic-gate 	/* create all the file_info_t's for all the mappings */
26310Sstevel@tonic-gate 	(void) Prd_agent(P);
26320Sstevel@tonic-gate 
26330Sstevel@tonic-gate 	for (i = 0, mptr = P->mappings; i < P->map_count; i++, mptr++) {
26340Sstevel@tonic-gate 		if ((fptr = mptr->map_file) == NULL)
26350Sstevel@tonic-gate 			object_name = NULL;
26367675SEdward.Pilatowicz@Sun.COM 		else if (lmresolve && (fptr->file_rname != NULL))
26377675SEdward.Pilatowicz@Sun.COM 			object_name = fptr->file_rname;
26380Sstevel@tonic-gate 		else
26390Sstevel@tonic-gate 			object_name = fptr->file_lname;
26400Sstevel@tonic-gate 		if ((rc = func(cd, &mptr->map_pmap, object_name)) != 0)
26410Sstevel@tonic-gate 			return (rc);
26420Sstevel@tonic-gate 	}
26430Sstevel@tonic-gate 	return (0);
26440Sstevel@tonic-gate }
26450Sstevel@tonic-gate 
26467675SEdward.Pilatowicz@Sun.COM int
Pmapping_iter(struct ps_prochandle * P,proc_map_f * func,void * cd)26477675SEdward.Pilatowicz@Sun.COM Pmapping_iter(struct ps_prochandle *P, proc_map_f *func, void *cd)
26487675SEdward.Pilatowicz@Sun.COM {
26497675SEdward.Pilatowicz@Sun.COM 	return (i_Pmapping_iter(P, B_FALSE, func, cd));
26507675SEdward.Pilatowicz@Sun.COM }
26517675SEdward.Pilatowicz@Sun.COM 
26527675SEdward.Pilatowicz@Sun.COM int
Pmapping_iter_resolved(struct ps_prochandle * P,proc_map_f * func,void * cd)26537675SEdward.Pilatowicz@Sun.COM Pmapping_iter_resolved(struct ps_prochandle *P, proc_map_f *func, void *cd)
26547675SEdward.Pilatowicz@Sun.COM {
26557675SEdward.Pilatowicz@Sun.COM 	return (i_Pmapping_iter(P, B_TRUE, func, cd));
26567675SEdward.Pilatowicz@Sun.COM }
26577675SEdward.Pilatowicz@Sun.COM 
26580Sstevel@tonic-gate /*
26590Sstevel@tonic-gate  * Iterate over the process's mapped objects.
26600Sstevel@tonic-gate  */
26617675SEdward.Pilatowicz@Sun.COM static int
i_Pobject_iter(struct ps_prochandle * P,boolean_t lmresolve,proc_map_f * func,void * cd)26627675SEdward.Pilatowicz@Sun.COM i_Pobject_iter(struct ps_prochandle *P, boolean_t lmresolve,
26637675SEdward.Pilatowicz@Sun.COM     proc_map_f *func, void *cd)
26640Sstevel@tonic-gate {
26650Sstevel@tonic-gate 	map_info_t *mptr;
26660Sstevel@tonic-gate 	file_info_t *fptr;
26670Sstevel@tonic-gate 	uint_t cnt;
26680Sstevel@tonic-gate 	int rc = 0;
26690Sstevel@tonic-gate 
26700Sstevel@tonic-gate 	(void) Prd_agent(P); /* create file_info_t's for all the mappings */
26710Sstevel@tonic-gate 	Pupdate_maps(P);
26720Sstevel@tonic-gate 
26730Sstevel@tonic-gate 	for (cnt = P->num_files, fptr = list_next(&P->file_head);
26740Sstevel@tonic-gate 	    cnt; cnt--, fptr = list_next(fptr)) {
26757675SEdward.Pilatowicz@Sun.COM 		const char *lname;
26767675SEdward.Pilatowicz@Sun.COM 
26777675SEdward.Pilatowicz@Sun.COM 		if (lmresolve && (fptr->file_rname != NULL))
26787675SEdward.Pilatowicz@Sun.COM 			lname = fptr->file_rname;
26797675SEdward.Pilatowicz@Sun.COM 		else if (fptr->file_lname != NULL)
26807675SEdward.Pilatowicz@Sun.COM 			lname = fptr->file_lname;
26817675SEdward.Pilatowicz@Sun.COM 		else
26827675SEdward.Pilatowicz@Sun.COM 			lname = "";
26830Sstevel@tonic-gate 
26840Sstevel@tonic-gate 		if ((mptr = fptr->file_map) == NULL)
26850Sstevel@tonic-gate 			continue;
26860Sstevel@tonic-gate 
26870Sstevel@tonic-gate 		if ((rc = func(cd, &mptr->map_pmap, lname)) != 0)
26880Sstevel@tonic-gate 			return (rc);
26899397SJonathan.Haslam@Sun.COM 
26909397SJonathan.Haslam@Sun.COM 		if (!P->info_valid)
26919397SJonathan.Haslam@Sun.COM 			Pupdate_maps(P);
26920Sstevel@tonic-gate 	}
26930Sstevel@tonic-gate 	return (0);
26940Sstevel@tonic-gate }
26950Sstevel@tonic-gate 
26967675SEdward.Pilatowicz@Sun.COM int
Pobject_iter(struct ps_prochandle * P,proc_map_f * func,void * cd)26977675SEdward.Pilatowicz@Sun.COM Pobject_iter(struct ps_prochandle *P, proc_map_f *func, void *cd)
26987675SEdward.Pilatowicz@Sun.COM {
26997675SEdward.Pilatowicz@Sun.COM 	return (i_Pobject_iter(P, B_FALSE, func, cd));
27007675SEdward.Pilatowicz@Sun.COM }
27017675SEdward.Pilatowicz@Sun.COM 
27027675SEdward.Pilatowicz@Sun.COM int
Pobject_iter_resolved(struct ps_prochandle * P,proc_map_f * func,void * cd)27037675SEdward.Pilatowicz@Sun.COM Pobject_iter_resolved(struct ps_prochandle *P, proc_map_f *func, void *cd)
27047675SEdward.Pilatowicz@Sun.COM {
27057675SEdward.Pilatowicz@Sun.COM 	return (i_Pobject_iter(P, B_TRUE, func, cd));
27067675SEdward.Pilatowicz@Sun.COM }
27077675SEdward.Pilatowicz@Sun.COM 
27087675SEdward.Pilatowicz@Sun.COM static char *
i_Pobjname(struct ps_prochandle * P,boolean_t lmresolve,uintptr_t addr,char * buffer,size_t bufsize)27097675SEdward.Pilatowicz@Sun.COM i_Pobjname(struct ps_prochandle *P, boolean_t lmresolve, uintptr_t addr,
27100Sstevel@tonic-gate 	char *buffer, size_t bufsize)
27110Sstevel@tonic-gate {
27120Sstevel@tonic-gate 	map_info_t *mptr;
27130Sstevel@tonic-gate 	file_info_t *fptr;
27140Sstevel@tonic-gate 
27150Sstevel@tonic-gate 	/* create all the file_info_t's for all the mappings */
27160Sstevel@tonic-gate 	(void) Prd_agent(P);
27170Sstevel@tonic-gate 
27187675SEdward.Pilatowicz@Sun.COM 	if ((mptr = Paddr2mptr(P, addr)) == NULL)
27197675SEdward.Pilatowicz@Sun.COM 		return (NULL);
27207675SEdward.Pilatowicz@Sun.COM 
27217675SEdward.Pilatowicz@Sun.COM 	if (!lmresolve) {
27227675SEdward.Pilatowicz@Sun.COM 		if (((fptr = mptr->map_file) == NULL) ||
27237675SEdward.Pilatowicz@Sun.COM 		    (fptr->file_lname == NULL))
27247675SEdward.Pilatowicz@Sun.COM 			return (NULL);
27257675SEdward.Pilatowicz@Sun.COM 		(void) strlcpy(buffer, fptr->file_lname, bufsize);
27260Sstevel@tonic-gate 		return (buffer);
27270Sstevel@tonic-gate 	}
27287675SEdward.Pilatowicz@Sun.COM 
27297675SEdward.Pilatowicz@Sun.COM 	/* Check for a cached copy of the resolved path */
27307675SEdward.Pilatowicz@Sun.COM 	if (Pfindmap(P, mptr, buffer, bufsize) != NULL)
27317675SEdward.Pilatowicz@Sun.COM 		return (buffer);
27327675SEdward.Pilatowicz@Sun.COM 
27330Sstevel@tonic-gate 	return (NULL);
27340Sstevel@tonic-gate }
27350Sstevel@tonic-gate 
27360Sstevel@tonic-gate /*
27377675SEdward.Pilatowicz@Sun.COM  * Given a virtual address, return the name of the underlying
27387675SEdward.Pilatowicz@Sun.COM  * mapped object (file) as provided by the dynamic linker.
27397675SEdward.Pilatowicz@Sun.COM  * Return NULL if we can't find any name information for the object.
27407675SEdward.Pilatowicz@Sun.COM  */
27417675SEdward.Pilatowicz@Sun.COM char *
Pobjname(struct ps_prochandle * P,uintptr_t addr,char * buffer,size_t bufsize)27427675SEdward.Pilatowicz@Sun.COM Pobjname(struct ps_prochandle *P, uintptr_t addr,
27437675SEdward.Pilatowicz@Sun.COM 	char *buffer, size_t bufsize)
27447675SEdward.Pilatowicz@Sun.COM {
27457675SEdward.Pilatowicz@Sun.COM 	return (i_Pobjname(P, B_FALSE, addr, buffer, bufsize));
27467675SEdward.Pilatowicz@Sun.COM }
27477675SEdward.Pilatowicz@Sun.COM 
27487675SEdward.Pilatowicz@Sun.COM /*
27497675SEdward.Pilatowicz@Sun.COM  * Given a virtual address, try to return a filesystem path to the
27507675SEdward.Pilatowicz@Sun.COM  * underlying mapped object (file).  If we're in the global zone,
27517675SEdward.Pilatowicz@Sun.COM  * this path could resolve to an object in another zone.  If we're
27527675SEdward.Pilatowicz@Sun.COM  * unable return a valid filesystem path, we'll fall back to providing
27537675SEdward.Pilatowicz@Sun.COM  * the mapped object (file) name provided by the dynamic linker in
27547675SEdward.Pilatowicz@Sun.COM  * the target process (ie, the object reported by Pobjname()).
27557675SEdward.Pilatowicz@Sun.COM  */
27567675SEdward.Pilatowicz@Sun.COM char *
Pobjname_resolved(struct ps_prochandle * P,uintptr_t addr,char * buffer,size_t bufsize)27577675SEdward.Pilatowicz@Sun.COM Pobjname_resolved(struct ps_prochandle *P, uintptr_t addr,
27587675SEdward.Pilatowicz@Sun.COM 	char *buffer, size_t bufsize)
27597675SEdward.Pilatowicz@Sun.COM {
27607675SEdward.Pilatowicz@Sun.COM 	return (i_Pobjname(P, B_TRUE, addr, buffer, bufsize));
27617675SEdward.Pilatowicz@Sun.COM }
27627675SEdward.Pilatowicz@Sun.COM 
27637675SEdward.Pilatowicz@Sun.COM /*
27640Sstevel@tonic-gate  * Given a virtual address, return the link map id of the underlying mapped
27650Sstevel@tonic-gate  * object (file), as provided by the dynamic linker.  Return -1 on failure.
27660Sstevel@tonic-gate  */
27670Sstevel@tonic-gate int
Plmid(struct ps_prochandle * P,uintptr_t addr,Lmid_t * lmidp)27680Sstevel@tonic-gate Plmid(struct ps_prochandle *P, uintptr_t addr, Lmid_t *lmidp)
27690Sstevel@tonic-gate {
27700Sstevel@tonic-gate 	map_info_t *mptr;
27710Sstevel@tonic-gate 	file_info_t *fptr;
27720Sstevel@tonic-gate 
27730Sstevel@tonic-gate 	/* create all the file_info_t's for all the mappings */
27740Sstevel@tonic-gate 	(void) Prd_agent(P);
27750Sstevel@tonic-gate 
27760Sstevel@tonic-gate 	if ((mptr = Paddr2mptr(P, addr)) != NULL &&
27770Sstevel@tonic-gate 	    (fptr = mptr->map_file) != NULL && fptr->file_lo != NULL) {
27780Sstevel@tonic-gate 		*lmidp = fptr->file_lo->rl_lmident;
27790Sstevel@tonic-gate 		return (0);
27800Sstevel@tonic-gate 	}
27810Sstevel@tonic-gate 
27820Sstevel@tonic-gate 	return (-1);
27830Sstevel@tonic-gate }
27840Sstevel@tonic-gate 
27850Sstevel@tonic-gate /*
27860Sstevel@tonic-gate  * Given an object name and optional lmid, iterate over the object's symbols.
27870Sstevel@tonic-gate  * If which == PR_SYMTAB, search the normal symbol table.
27880Sstevel@tonic-gate  * If which == PR_DYNSYM, search the dynamic symbol table.
27890Sstevel@tonic-gate  */
27900Sstevel@tonic-gate static int
Psymbol_iter_com(struct ps_prochandle * P,Lmid_t lmid,const char * object_name,int which,int mask,pr_order_t order,proc_xsym_f * func,void * cd)27910Sstevel@tonic-gate Psymbol_iter_com(struct ps_prochandle *P, Lmid_t lmid, const char *object_name,
27920Sstevel@tonic-gate     int which, int mask, pr_order_t order, proc_xsym_f *func, void *cd)
27930Sstevel@tonic-gate {
27949900SAli.Bahrami@Sun.COM #if STT_NUM != (STT_TLS + 1)
27959085SAli.Bahrami@Sun.COM #error "STT_NUM has grown. update Psymbol_iter_com()"
27969085SAli.Bahrami@Sun.COM #endif
27979085SAli.Bahrami@Sun.COM 
27980Sstevel@tonic-gate 	GElf_Sym sym;
27991160Svb160487 	GElf_Shdr shdr;
28000Sstevel@tonic-gate 	map_info_t *mptr;
28010Sstevel@tonic-gate 	file_info_t *fptr;
28020Sstevel@tonic-gate 	sym_tbl_t *symtab;
28030Sstevel@tonic-gate 	size_t symn;
28040Sstevel@tonic-gate 	const char *strs;
28050Sstevel@tonic-gate 	size_t strsz;
28060Sstevel@tonic-gate 	prsyminfo_t si;
28070Sstevel@tonic-gate 	int rv;
28080Sstevel@tonic-gate 	uint_t *map, i, count, ndx;
28090Sstevel@tonic-gate 
28100Sstevel@tonic-gate 	if ((mptr = object_name_to_map(P, lmid, object_name)) == NULL)
28110Sstevel@tonic-gate 		return (-1);
28120Sstevel@tonic-gate 
28130Sstevel@tonic-gate 	if ((fptr = build_map_symtab(P, mptr)) == NULL || /* no mapped file */
28140Sstevel@tonic-gate 	    fptr->file_elf == NULL)			/* not an ELF file */
28150Sstevel@tonic-gate 		return (-1);
28160Sstevel@tonic-gate 
28170Sstevel@tonic-gate 	/*
28180Sstevel@tonic-gate 	 * Search the specified symbol table.
28190Sstevel@tonic-gate 	 */
28200Sstevel@tonic-gate 	switch (which) {
28210Sstevel@tonic-gate 	case PR_SYMTAB:
28220Sstevel@tonic-gate 		symtab = &fptr->file_symtab;
28230Sstevel@tonic-gate 		si.prs_table = PR_SYMTAB;
28240Sstevel@tonic-gate 		break;
28250Sstevel@tonic-gate 	case PR_DYNSYM:
28260Sstevel@tonic-gate 		symtab = &fptr->file_dynsym;
28270Sstevel@tonic-gate 		si.prs_table = PR_DYNSYM;
28280Sstevel@tonic-gate 		break;
28290Sstevel@tonic-gate 	default:
28300Sstevel@tonic-gate 		return (-1);
28310Sstevel@tonic-gate 	}
28320Sstevel@tonic-gate 
28330Sstevel@tonic-gate 	si.prs_object = object_name;
28340Sstevel@tonic-gate 	si.prs_lmid = fptr->file_lo == NULL ?
28350Sstevel@tonic-gate 	    LM_ID_BASE : fptr->file_lo->rl_lmident;
28360Sstevel@tonic-gate 
28370Sstevel@tonic-gate 	symn = symtab->sym_symn;
28380Sstevel@tonic-gate 	strs = symtab->sym_strs;
28390Sstevel@tonic-gate 	strsz = symtab->sym_strsz;
28400Sstevel@tonic-gate 
28410Sstevel@tonic-gate 	switch (order) {
28420Sstevel@tonic-gate 	case PRO_NATURAL:
28430Sstevel@tonic-gate 		map = NULL;
28440Sstevel@tonic-gate 		count = symn;
28450Sstevel@tonic-gate 		break;
28460Sstevel@tonic-gate 	case PRO_BYNAME:
28470Sstevel@tonic-gate 		map = symtab->sym_byname;
28480Sstevel@tonic-gate 		count = symtab->sym_count;
28490Sstevel@tonic-gate 		break;
28500Sstevel@tonic-gate 	case PRO_BYADDR:
28510Sstevel@tonic-gate 		map = symtab->sym_byaddr;
28520Sstevel@tonic-gate 		count = symtab->sym_count;
28530Sstevel@tonic-gate 		break;
28540Sstevel@tonic-gate 	default:
28550Sstevel@tonic-gate 		return (-1);
28560Sstevel@tonic-gate 	}
28570Sstevel@tonic-gate 
28583347Sab196087 	if (symtab->sym_data_pri == NULL || strs == NULL || count == 0)
28593347Sab196087 		return (-1);
28603347Sab196087 
28610Sstevel@tonic-gate 	rv = 0;
28620Sstevel@tonic-gate 
28630Sstevel@tonic-gate 	for (i = 0; i < count; i++) {
28640Sstevel@tonic-gate 		ndx = map == NULL ? i : map[i];
28653347Sab196087 		if (symtab_getsym(symtab, ndx, &sym) != NULL) {
28660Sstevel@tonic-gate 			uint_t s_bind, s_type, type;
28670Sstevel@tonic-gate 
28680Sstevel@tonic-gate 			if (sym.st_name >= strsz)	/* invalid st_name */
28690Sstevel@tonic-gate 				continue;
28700Sstevel@tonic-gate 
28710Sstevel@tonic-gate 			s_bind = GELF_ST_BIND(sym.st_info);
28720Sstevel@tonic-gate 			s_type = GELF_ST_TYPE(sym.st_info);
28730Sstevel@tonic-gate 
28740Sstevel@tonic-gate 			/*
28750Sstevel@tonic-gate 			 * In case you haven't already guessed, this relies on
28760Sstevel@tonic-gate 			 * the bitmask used in <libproc.h> for encoding symbol
28770Sstevel@tonic-gate 			 * type and binding matching the order of STB and STT
28789085SAli.Bahrami@Sun.COM 			 * constants in <sys/elf.h>.  Changes to ELF must
28799085SAli.Bahrami@Sun.COM 			 * maintain binary compatibility, so I think this is
28800Sstevel@tonic-gate 			 * reasonably fair game.
28810Sstevel@tonic-gate 			 */
28829900SAli.Bahrami@Sun.COM 			if (s_bind < STB_NUM && s_type < STT_NUM) {
28830Sstevel@tonic-gate 				type = (1 << (s_type + 8)) | (1 << s_bind);
28840Sstevel@tonic-gate 				if ((type & ~mask) != 0)
28850Sstevel@tonic-gate 					continue;
28860Sstevel@tonic-gate 			} else
28870Sstevel@tonic-gate 				continue; /* Invalid type or binding */
28880Sstevel@tonic-gate 
28890Sstevel@tonic-gate 			if (GELF_ST_TYPE(sym.st_info) != STT_TLS)
28900Sstevel@tonic-gate 				sym.st_value += fptr->file_dyn_base;
28910Sstevel@tonic-gate 
28920Sstevel@tonic-gate 			si.prs_name = strs + sym.st_name;
28931160Svb160487 
28941160Svb160487 			/*
28951160Svb160487 			 * If symbol's type is STT_SECTION, then try to lookup
28961160Svb160487 			 * the name of the corresponding section.
28971160Svb160487 			 */
28981160Svb160487 			if (GELF_ST_TYPE(sym.st_info) == STT_SECTION &&
28991160Svb160487 			    fptr->file_shstrs != NULL &&
29001160Svb160487 			    gelf_getshdr(elf_getscn(fptr->file_elf,
29011160Svb160487 			    sym.st_shndx), &shdr) != NULL &&
29021160Svb160487 			    shdr.sh_name != 0 &&
29031160Svb160487 			    shdr.sh_name < fptr->file_shstrsz)
29041160Svb160487 				si.prs_name = fptr->file_shstrs + shdr.sh_name;
29051160Svb160487 
29060Sstevel@tonic-gate 			si.prs_id = ndx;
29071160Svb160487 			if ((rv = func(cd, &sym, si.prs_name, &si)) != 0)
29080Sstevel@tonic-gate 				break;
29090Sstevel@tonic-gate 		}
29100Sstevel@tonic-gate 	}
29110Sstevel@tonic-gate 
29120Sstevel@tonic-gate 	return (rv);
29130Sstevel@tonic-gate }
29140Sstevel@tonic-gate 
29150Sstevel@tonic-gate int
Pxsymbol_iter(struct ps_prochandle * P,Lmid_t lmid,const char * object_name,int which,int mask,proc_xsym_f * func,void * cd)29160Sstevel@tonic-gate Pxsymbol_iter(struct ps_prochandle *P, Lmid_t lmid, const char *object_name,
29170Sstevel@tonic-gate     int which, int mask, proc_xsym_f *func, void *cd)
29180Sstevel@tonic-gate {
29190Sstevel@tonic-gate 	return (Psymbol_iter_com(P, lmid, object_name, which, mask,
29200Sstevel@tonic-gate 	    PRO_NATURAL, func, cd));
29210Sstevel@tonic-gate }
29220Sstevel@tonic-gate 
29230Sstevel@tonic-gate int
Psymbol_iter_by_lmid(struct ps_prochandle * P,Lmid_t lmid,const char * object_name,int which,int mask,proc_sym_f * func,void * cd)29240Sstevel@tonic-gate Psymbol_iter_by_lmid(struct ps_prochandle *P, Lmid_t lmid,
29250Sstevel@tonic-gate     const char *object_name, int which, int mask, proc_sym_f *func, void *cd)
29260Sstevel@tonic-gate {
29270Sstevel@tonic-gate 	return (Psymbol_iter_com(P, lmid, object_name, which, mask,
29280Sstevel@tonic-gate 	    PRO_NATURAL, (proc_xsym_f *)func, cd));
29290Sstevel@tonic-gate }
29300Sstevel@tonic-gate 
29310Sstevel@tonic-gate int
Psymbol_iter(struct ps_prochandle * P,const char * object_name,int which,int mask,proc_sym_f * func,void * cd)29320Sstevel@tonic-gate Psymbol_iter(struct ps_prochandle *P,
29330Sstevel@tonic-gate     const char *object_name, int which, int mask, proc_sym_f *func, void *cd)
29340Sstevel@tonic-gate {
29350Sstevel@tonic-gate 	return (Psymbol_iter_com(P, PR_LMID_EVERY, object_name, which, mask,
29360Sstevel@tonic-gate 	    PRO_NATURAL, (proc_xsym_f *)func, cd));
29370Sstevel@tonic-gate }
29380Sstevel@tonic-gate 
29390Sstevel@tonic-gate int
Psymbol_iter_by_addr(struct ps_prochandle * P,const char * object_name,int which,int mask,proc_sym_f * func,void * cd)29400Sstevel@tonic-gate Psymbol_iter_by_addr(struct ps_prochandle *P,
29410Sstevel@tonic-gate     const char *object_name, int which, int mask, proc_sym_f *func, void *cd)
29420Sstevel@tonic-gate {
29430Sstevel@tonic-gate 	return (Psymbol_iter_com(P, PR_LMID_EVERY, object_name, which, mask,
29440Sstevel@tonic-gate 	    PRO_BYADDR, (proc_xsym_f *)func, cd));
29450Sstevel@tonic-gate }
29460Sstevel@tonic-gate 
29470Sstevel@tonic-gate int
Psymbol_iter_by_name(struct ps_prochandle * P,const char * object_name,int which,int mask,proc_sym_f * func,void * cd)29480Sstevel@tonic-gate Psymbol_iter_by_name(struct ps_prochandle *P,
29490Sstevel@tonic-gate     const char *object_name, int which, int mask, proc_sym_f *func, void *cd)
29500Sstevel@tonic-gate {
29510Sstevel@tonic-gate 	return (Psymbol_iter_com(P, PR_LMID_EVERY, object_name, which, mask,
29520Sstevel@tonic-gate 	    PRO_BYNAME, (proc_xsym_f *)func, cd));
29530Sstevel@tonic-gate }
29540Sstevel@tonic-gate 
29550Sstevel@tonic-gate /*
29560Sstevel@tonic-gate  * Get the platform string from the core file if we have it;
29570Sstevel@tonic-gate  * just perform the system call for the caller if this is a live process.
29580Sstevel@tonic-gate  */
29590Sstevel@tonic-gate char *
Pplatform(struct ps_prochandle * P,char * s,size_t n)29600Sstevel@tonic-gate Pplatform(struct ps_prochandle *P, char *s, size_t n)
29610Sstevel@tonic-gate {
29620Sstevel@tonic-gate 	if (P->state == PS_IDLE) {
29630Sstevel@tonic-gate 		errno = ENODATA;
29640Sstevel@tonic-gate 		return (NULL);
29650Sstevel@tonic-gate 	}
29660Sstevel@tonic-gate 
29670Sstevel@tonic-gate 	if (P->state == PS_DEAD) {
29680Sstevel@tonic-gate 		if (P->core->core_platform == NULL) {
29690Sstevel@tonic-gate 			errno = ENODATA;
29700Sstevel@tonic-gate 			return (NULL);
29710Sstevel@tonic-gate 		}
29720Sstevel@tonic-gate 		(void) strncpy(s, P->core->core_platform, n - 1);
29730Sstevel@tonic-gate 		s[n - 1] = '\0';
29740Sstevel@tonic-gate 
29750Sstevel@tonic-gate 	} else if (sysinfo(SI_PLATFORM, s, n) == -1)
29760Sstevel@tonic-gate 		return (NULL);
29770Sstevel@tonic-gate 
29780Sstevel@tonic-gate 	return (s);
29790Sstevel@tonic-gate }
29800Sstevel@tonic-gate 
29810Sstevel@tonic-gate /*
29820Sstevel@tonic-gate  * Get the uname(2) information from the core file if we have it;
29830Sstevel@tonic-gate  * just perform the system call for the caller if this is a live process.
29840Sstevel@tonic-gate  */
29850Sstevel@tonic-gate int
Puname(struct ps_prochandle * P,struct utsname * u)29860Sstevel@tonic-gate Puname(struct ps_prochandle *P, struct utsname *u)
29870Sstevel@tonic-gate {
29880Sstevel@tonic-gate 	if (P->state == PS_IDLE) {
29890Sstevel@tonic-gate 		errno = ENODATA;
29900Sstevel@tonic-gate 		return (-1);
29910Sstevel@tonic-gate 	}
29920Sstevel@tonic-gate 
29930Sstevel@tonic-gate 	if (P->state == PS_DEAD) {
29940Sstevel@tonic-gate 		if (P->core->core_uts == NULL) {
29950Sstevel@tonic-gate 			errno = ENODATA;
29960Sstevel@tonic-gate 			return (-1);
29970Sstevel@tonic-gate 		}
29980Sstevel@tonic-gate 		(void) memcpy(u, P->core->core_uts, sizeof (struct utsname));
29990Sstevel@tonic-gate 		return (0);
30000Sstevel@tonic-gate 	}
30010Sstevel@tonic-gate 	return (uname(u));
30020Sstevel@tonic-gate }
30030Sstevel@tonic-gate 
30040Sstevel@tonic-gate /*
30050Sstevel@tonic-gate  * Called from Pcreate(), Pgrab(), and Pfgrab_core() to initialize
30060Sstevel@tonic-gate  * the symbol table heads in the new ps_prochandle.
30070Sstevel@tonic-gate  */
30080Sstevel@tonic-gate void
Pinitsym(struct ps_prochandle * P)30090Sstevel@tonic-gate Pinitsym(struct ps_prochandle *P)
30100Sstevel@tonic-gate {
30110Sstevel@tonic-gate 	P->num_files = 0;
30120Sstevel@tonic-gate 	list_link(&P->file_head, NULL);
30130Sstevel@tonic-gate }
30140Sstevel@tonic-gate 
30150Sstevel@tonic-gate /*
30160Sstevel@tonic-gate  * Called from Prelease() to destroy the symbol tables.
30170Sstevel@tonic-gate  * Must be called by the client after an exec() in the victim process.
30180Sstevel@tonic-gate  */
30190Sstevel@tonic-gate void
Preset_maps(struct ps_prochandle * P)30200Sstevel@tonic-gate Preset_maps(struct ps_prochandle *P)
30210Sstevel@tonic-gate {
30220Sstevel@tonic-gate 	int i;
30230Sstevel@tonic-gate 
30240Sstevel@tonic-gate 	if (P->rap != NULL) {
30250Sstevel@tonic-gate 		rd_delete(P->rap);
30260Sstevel@tonic-gate 		P->rap = NULL;
30270Sstevel@tonic-gate 	}
30280Sstevel@tonic-gate 
30290Sstevel@tonic-gate 	if (P->execname != NULL) {
30300Sstevel@tonic-gate 		free(P->execname);
30310Sstevel@tonic-gate 		P->execname = NULL;
30320Sstevel@tonic-gate 	}
30330Sstevel@tonic-gate 
30340Sstevel@tonic-gate 	if (P->auxv != NULL) {
30350Sstevel@tonic-gate 		free(P->auxv);
30360Sstevel@tonic-gate 		P->auxv = NULL;
30370Sstevel@tonic-gate 		P->nauxv = 0;
30380Sstevel@tonic-gate 	}
30390Sstevel@tonic-gate 
30400Sstevel@tonic-gate 	for (i = 0; i < P->map_count; i++)
30410Sstevel@tonic-gate 		map_info_free(P, &P->mappings[i]);
30420Sstevel@tonic-gate 
30430Sstevel@tonic-gate 	if (P->mappings != NULL) {
30440Sstevel@tonic-gate 		free(P->mappings);
30450Sstevel@tonic-gate 		P->mappings = NULL;
30460Sstevel@tonic-gate 	}
30470Sstevel@tonic-gate 	P->map_count = P->map_alloc = 0;
30480Sstevel@tonic-gate 
30490Sstevel@tonic-gate 	P->info_valid = 0;
30500Sstevel@tonic-gate }
30510Sstevel@tonic-gate 
30520Sstevel@tonic-gate typedef struct getenv_data {
30530Sstevel@tonic-gate 	char *buf;
30540Sstevel@tonic-gate 	size_t bufsize;
30550Sstevel@tonic-gate 	const char *search;
30560Sstevel@tonic-gate 	size_t searchlen;
30570Sstevel@tonic-gate } getenv_data_t;
30580Sstevel@tonic-gate 
30590Sstevel@tonic-gate /*ARGSUSED*/
30600Sstevel@tonic-gate static int
getenv_func(void * data,struct ps_prochandle * P,uintptr_t addr,const char * nameval)30610Sstevel@tonic-gate getenv_func(void *data, struct ps_prochandle *P, uintptr_t addr,
30620Sstevel@tonic-gate     const char *nameval)
30630Sstevel@tonic-gate {
30640Sstevel@tonic-gate 	getenv_data_t *d = data;
30650Sstevel@tonic-gate 	size_t len;
30660Sstevel@tonic-gate 
30670Sstevel@tonic-gate 	if (nameval == NULL)
30680Sstevel@tonic-gate 		return (0);
30690Sstevel@tonic-gate 
30700Sstevel@tonic-gate 	if (d->searchlen < strlen(nameval) &&
30710Sstevel@tonic-gate 	    strncmp(nameval, d->search, d->searchlen) == 0 &&
30720Sstevel@tonic-gate 	    nameval[d->searchlen] == '=') {
30730Sstevel@tonic-gate 		len = MIN(strlen(nameval), d->bufsize - 1);
30740Sstevel@tonic-gate 		(void) strncpy(d->buf, nameval, len);
30750Sstevel@tonic-gate 		d->buf[len] = '\0';
30760Sstevel@tonic-gate 		return (1);
30770Sstevel@tonic-gate 	}
30780Sstevel@tonic-gate 
30790Sstevel@tonic-gate 	return (0);
30800Sstevel@tonic-gate }
30810Sstevel@tonic-gate 
30820Sstevel@tonic-gate char *
Pgetenv(struct ps_prochandle * P,const char * name,char * buf,size_t buflen)30830Sstevel@tonic-gate Pgetenv(struct ps_prochandle *P, const char *name, char *buf, size_t buflen)
30840Sstevel@tonic-gate {
30850Sstevel@tonic-gate 	getenv_data_t d;
30860Sstevel@tonic-gate 
30870Sstevel@tonic-gate 	d.buf = buf;
30880Sstevel@tonic-gate 	d.bufsize = buflen;
30890Sstevel@tonic-gate 	d.search = name;
30900Sstevel@tonic-gate 	d.searchlen = strlen(name);
30910Sstevel@tonic-gate 
30920Sstevel@tonic-gate 	if (Penv_iter(P, getenv_func, &d) == 1) {
30930Sstevel@tonic-gate 		char *equals = strchr(d.buf, '=');
30940Sstevel@tonic-gate 
30950Sstevel@tonic-gate 		if (equals != NULL) {
30960Sstevel@tonic-gate 			(void) memmove(d.buf, equals + 1,
30970Sstevel@tonic-gate 			    d.buf + buflen - equals - 1);
30980Sstevel@tonic-gate 			d.buf[d.buf + buflen - equals] = '\0';
30990Sstevel@tonic-gate 
31000Sstevel@tonic-gate 			return (buf);
31010Sstevel@tonic-gate 		}
31020Sstevel@tonic-gate 	}
31030Sstevel@tonic-gate 
31040Sstevel@tonic-gate 	return (NULL);
31050Sstevel@tonic-gate }
31060Sstevel@tonic-gate 
31070Sstevel@tonic-gate /* number of argument or environment pointers to read all at once */
31080Sstevel@tonic-gate #define	NARG	100
31090Sstevel@tonic-gate 
31100Sstevel@tonic-gate int
Penv_iter(struct ps_prochandle * P,proc_env_f * func,void * data)31110Sstevel@tonic-gate Penv_iter(struct ps_prochandle *P, proc_env_f *func, void *data)
31120Sstevel@tonic-gate {
31130Sstevel@tonic-gate 	const psinfo_t *psp;
31140Sstevel@tonic-gate 	uintptr_t envpoff;
31150Sstevel@tonic-gate 	GElf_Sym sym;
31160Sstevel@tonic-gate 	int ret;
31170Sstevel@tonic-gate 	char *buf, *nameval;
31180Sstevel@tonic-gate 	size_t buflen;
31190Sstevel@tonic-gate 
31200Sstevel@tonic-gate 	int nenv = NARG;
31210Sstevel@tonic-gate 	long envp[NARG];
31220Sstevel@tonic-gate 
31230Sstevel@tonic-gate 	/*
31240Sstevel@tonic-gate 	 * Attempt to find the "_environ" variable in the process.
31250Sstevel@tonic-gate 	 * Failing that, use the original value provided by Ppsinfo().
31260Sstevel@tonic-gate 	 */
31270Sstevel@tonic-gate 	if ((psp = Ppsinfo(P)) == NULL)
31280Sstevel@tonic-gate 		return (-1);
31290Sstevel@tonic-gate 
31300Sstevel@tonic-gate 	envpoff = psp->pr_envp; /* Default if no _environ found */
31310Sstevel@tonic-gate 
31320Sstevel@tonic-gate 	if (Plookup_by_name(P, PR_OBJ_EXEC, "_environ", &sym) == 0) {
31330Sstevel@tonic-gate 		if (P->status.pr_dmodel == PR_MODEL_NATIVE) {
31340Sstevel@tonic-gate 			if (Pread(P, &envpoff, sizeof (envpoff),
31350Sstevel@tonic-gate 			    sym.st_value) != sizeof (envpoff))
31360Sstevel@tonic-gate 				envpoff = psp->pr_envp;
31370Sstevel@tonic-gate 		} else if (P->status.pr_dmodel == PR_MODEL_ILP32) {
31380Sstevel@tonic-gate 			uint32_t envpoff32;
31390Sstevel@tonic-gate 
31400Sstevel@tonic-gate 			if (Pread(P, &envpoff32, sizeof (envpoff32),
31410Sstevel@tonic-gate 			    sym.st_value) != sizeof (envpoff32))
31420Sstevel@tonic-gate 				envpoff = psp->pr_envp;
31430Sstevel@tonic-gate 			else
31440Sstevel@tonic-gate 				envpoff = envpoff32;
31450Sstevel@tonic-gate 		}
31460Sstevel@tonic-gate 	}
31470Sstevel@tonic-gate 
31480Sstevel@tonic-gate 	buflen = 128;
31490Sstevel@tonic-gate 	buf = malloc(buflen);
31500Sstevel@tonic-gate 
31510Sstevel@tonic-gate 	ret = 0;
31520Sstevel@tonic-gate 	for (;;) {
31530Sstevel@tonic-gate 		uintptr_t envoff;
31540Sstevel@tonic-gate 
31550Sstevel@tonic-gate 		if (nenv == NARG) {
31560Sstevel@tonic-gate 			(void) memset(envp, 0, sizeof (envp));
31570Sstevel@tonic-gate 			if (P->status.pr_dmodel == PR_MODEL_NATIVE) {
31580Sstevel@tonic-gate 				if (Pread(P, envp,
31590Sstevel@tonic-gate 				    sizeof (envp), envpoff) <= 0) {
31600Sstevel@tonic-gate 					ret = -1;
31610Sstevel@tonic-gate 					break;
31620Sstevel@tonic-gate 				}
31630Sstevel@tonic-gate 			} else if (P->status.pr_dmodel == PR_MODEL_ILP32) {
31640Sstevel@tonic-gate 				uint32_t e32[NARG];
31650Sstevel@tonic-gate 				int i;
31660Sstevel@tonic-gate 
31670Sstevel@tonic-gate 				(void) memset(e32, 0, sizeof (e32));
31680Sstevel@tonic-gate 				if (Pread(P, e32, sizeof (e32), envpoff) <= 0) {
31690Sstevel@tonic-gate 					ret = -1;
31700Sstevel@tonic-gate 					break;
31710Sstevel@tonic-gate 				}
31720Sstevel@tonic-gate 				for (i = 0; i < NARG; i++)
31730Sstevel@tonic-gate 					envp[i] = e32[i];
31740Sstevel@tonic-gate 			}
31750Sstevel@tonic-gate 			nenv = 0;
31760Sstevel@tonic-gate 		}
31770Sstevel@tonic-gate 
31780Sstevel@tonic-gate 		if ((envoff = envp[nenv++]) == NULL)
31790Sstevel@tonic-gate 			break;
31800Sstevel@tonic-gate 
31810Sstevel@tonic-gate 		/*
31820Sstevel@tonic-gate 		 * Attempt to read the string from the process.
31830Sstevel@tonic-gate 		 */
31840Sstevel@tonic-gate again:
31850Sstevel@tonic-gate 		ret = Pread_string(P, buf, buflen, envoff);
31860Sstevel@tonic-gate 
31870Sstevel@tonic-gate 		if (ret <= 0) {
31880Sstevel@tonic-gate 			nameval = NULL;
31890Sstevel@tonic-gate 		} else if (ret == buflen - 1) {
31900Sstevel@tonic-gate 			free(buf);
31910Sstevel@tonic-gate 			/*
31920Sstevel@tonic-gate 			 * Bail if we have a corrupted environment
31930Sstevel@tonic-gate 			 */
31940Sstevel@tonic-gate 			if (buflen >= ARG_MAX)
31950Sstevel@tonic-gate 				return (-1);
31960Sstevel@tonic-gate 			buflen *= 2;
31970Sstevel@tonic-gate 			buf = malloc(buflen);
31980Sstevel@tonic-gate 			goto again;
31990Sstevel@tonic-gate 		} else {
32000Sstevel@tonic-gate 			nameval = buf;
32010Sstevel@tonic-gate 		}
32020Sstevel@tonic-gate 
32030Sstevel@tonic-gate 		if ((ret = func(data, P, envoff, nameval)) != 0)
32040Sstevel@tonic-gate 			break;
32050Sstevel@tonic-gate 
32060Sstevel@tonic-gate 		envpoff += (P->status.pr_dmodel == PR_MODEL_LP64)? 8 : 4;
32070Sstevel@tonic-gate 	}
32080Sstevel@tonic-gate 
32090Sstevel@tonic-gate 	free(buf);
32100Sstevel@tonic-gate 
32110Sstevel@tonic-gate 	return (ret);
32120Sstevel@tonic-gate }
3213