xref: /titanic_51/usr/src/lib/libproc/common/Psymtab.c (revision 7e16fca05dfbcfd32c2ebc9e4d1abdac1cd8657c)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5ab9a77c7Sahl  * Common Development and Distribution License (the "License").
6ab9a77c7Sahl  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21ab9a77c7Sahl 
227c478bd9Sstevel@tonic-gate /*
23*7e16fca0SAli Bahrami  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
279acbbeafSnn35248 #include <assert.h>
287c478bd9Sstevel@tonic-gate #include <stdio.h>
297c478bd9Sstevel@tonic-gate #include <stdlib.h>
307c478bd9Sstevel@tonic-gate #include <stddef.h>
317c478bd9Sstevel@tonic-gate #include <unistd.h>
327c478bd9Sstevel@tonic-gate #include <ctype.h>
337c478bd9Sstevel@tonic-gate #include <fcntl.h>
347c478bd9Sstevel@tonic-gate #include <string.h>
357c478bd9Sstevel@tonic-gate #include <strings.h>
367c478bd9Sstevel@tonic-gate #include <memory.h>
377c478bd9Sstevel@tonic-gate #include <errno.h>
387c478bd9Sstevel@tonic-gate #include <dirent.h>
397c478bd9Sstevel@tonic-gate #include <signal.h>
407c478bd9Sstevel@tonic-gate #include <limits.h>
417c478bd9Sstevel@tonic-gate #include <libgen.h>
427c478bd9Sstevel@tonic-gate #include <sys/types.h>
437c478bd9Sstevel@tonic-gate #include <sys/stat.h>
447c478bd9Sstevel@tonic-gate #include <sys/systeminfo.h>
457c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate #include "libproc.h"
487c478bd9Sstevel@tonic-gate #include "Pcontrol.h"
497c478bd9Sstevel@tonic-gate #include "Putil.h"
50d51e9074Sab196087 #include "Psymtab_machelf.h"
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate static file_info_t *build_map_symtab(struct ps_prochandle *, map_info_t *);
537c478bd9Sstevel@tonic-gate static map_info_t *exec_map(struct ps_prochandle *);
547c478bd9Sstevel@tonic-gate static map_info_t *object_to_map(struct ps_prochandle *, Lmid_t, const char *);
557c478bd9Sstevel@tonic-gate static map_info_t *object_name_to_map(struct ps_prochandle *,
567c478bd9Sstevel@tonic-gate 	Lmid_t, const char *);
577c478bd9Sstevel@tonic-gate static GElf_Sym *sym_by_name(sym_tbl_t *, const char *, GElf_Sym *, uint_t *);
5830da1432Sahl static int read_ehdr32(struct ps_prochandle *, Elf32_Ehdr *, uint_t *,
5930da1432Sahl     uintptr_t);
607c478bd9Sstevel@tonic-gate #ifdef _LP64
6130da1432Sahl static int read_ehdr64(struct ps_prochandle *, Elf64_Ehdr *, uint_t *,
6230da1432Sahl     uintptr_t);
637c478bd9Sstevel@tonic-gate #endif
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate #define	DATA_TYPES	\
667c478bd9Sstevel@tonic-gate 	((1 << STT_OBJECT) | (1 << STT_FUNC) | \
677c478bd9Sstevel@tonic-gate 	(1 << STT_COMMON) | (1 << STT_TLS))
687c478bd9Sstevel@tonic-gate #define	IS_DATA_TYPE(tp)	(((1 << (tp)) & DATA_TYPES) != 0)
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate #define	MA_RWX	(MA_READ | MA_WRITE | MA_EXEC)
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate typedef enum {
737c478bd9Sstevel@tonic-gate 	PRO_NATURAL,
747c478bd9Sstevel@tonic-gate 	PRO_BYADDR,
757c478bd9Sstevel@tonic-gate 	PRO_BYNAME
767c478bd9Sstevel@tonic-gate } pr_order_t;
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate static int
797c478bd9Sstevel@tonic-gate addr_cmp(const void *aa, const void *bb)
807c478bd9Sstevel@tonic-gate {
817c478bd9Sstevel@tonic-gate 	uintptr_t a = *((uintptr_t *)aa);
827c478bd9Sstevel@tonic-gate 	uintptr_t b = *((uintptr_t *)bb);
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate 	if (a > b)
857c478bd9Sstevel@tonic-gate 		return (1);
867c478bd9Sstevel@tonic-gate 	if (a < b)
877c478bd9Sstevel@tonic-gate 		return (-1);
887c478bd9Sstevel@tonic-gate 	return (0);
897c478bd9Sstevel@tonic-gate }
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate /*
9212e72dbbSrh87107  * This function creates a list of addresses for a load object's sections.
9312e72dbbSrh87107  * The list is in ascending address order and alternates start address
9412e72dbbSrh87107  * then end address for each section we're interested in. The function
9512e72dbbSrh87107  * returns a pointer to the list, which must be freed by the caller.
9612e72dbbSrh87107  */
9712e72dbbSrh87107 static uintptr_t *
9812e72dbbSrh87107 get_saddrs(struct ps_prochandle *P, uintptr_t ehdr_start, uint_t *n)
9912e72dbbSrh87107 {
10012e72dbbSrh87107 	uintptr_t a, addr, *addrs, last = 0;
10112e72dbbSrh87107 	uint_t i, naddrs = 0, unordered = 0;
10212e72dbbSrh87107 
10312e72dbbSrh87107 	if (P->status.pr_dmodel == PR_MODEL_ILP32) {
10412e72dbbSrh87107 		Elf32_Ehdr ehdr;
10512e72dbbSrh87107 		Elf32_Phdr phdr;
10612e72dbbSrh87107 		uint_t phnum;
10712e72dbbSrh87107 
10812e72dbbSrh87107 		if (read_ehdr32(P, &ehdr, &phnum, ehdr_start) != 0)
10912e72dbbSrh87107 			return (NULL);
11012e72dbbSrh87107 
11112e72dbbSrh87107 		addrs = malloc(sizeof (uintptr_t) * phnum * 2);
11212e72dbbSrh87107 		a = ehdr_start + ehdr.e_phoff;
11312e72dbbSrh87107 		for (i = 0; i < phnum; i++, a += ehdr.e_phentsize) {
11412e72dbbSrh87107 			if (Pread(P, &phdr, sizeof (phdr), a) !=
11512e72dbbSrh87107 			    sizeof (phdr)) {
11612e72dbbSrh87107 				free(addrs);
11712e72dbbSrh87107 				return (NULL);
11812e72dbbSrh87107 			}
11912e72dbbSrh87107 			if (phdr.p_type != PT_LOAD || phdr.p_memsz == 0)
12012e72dbbSrh87107 				continue;
12112e72dbbSrh87107 
12212e72dbbSrh87107 			addr = phdr.p_vaddr;
12312e72dbbSrh87107 			if (ehdr.e_type == ET_DYN)
12412e72dbbSrh87107 				addr += ehdr_start;
12512e72dbbSrh87107 			if (last > addr)
12612e72dbbSrh87107 				unordered = 1;
12712e72dbbSrh87107 			addrs[naddrs++] = addr;
12812e72dbbSrh87107 			addrs[naddrs++] = last = addr + phdr.p_memsz - 1;
12912e72dbbSrh87107 		}
13012e72dbbSrh87107 #ifdef _LP64
13112e72dbbSrh87107 	} else {
13212e72dbbSrh87107 		Elf64_Ehdr ehdr;
13312e72dbbSrh87107 		Elf64_Phdr phdr;
13412e72dbbSrh87107 		uint_t phnum;
13512e72dbbSrh87107 
13612e72dbbSrh87107 		if (read_ehdr64(P, &ehdr, &phnum, ehdr_start) != 0)
13712e72dbbSrh87107 			return (NULL);
13812e72dbbSrh87107 
13912e72dbbSrh87107 		addrs = malloc(sizeof (uintptr_t) * phnum * 2);
14012e72dbbSrh87107 		a = ehdr_start + ehdr.e_phoff;
14112e72dbbSrh87107 		for (i = 0; i < phnum; i++, a += ehdr.e_phentsize) {
14212e72dbbSrh87107 			if (Pread(P, &phdr, sizeof (phdr), a) !=
14312e72dbbSrh87107 			    sizeof (phdr)) {
14412e72dbbSrh87107 				free(addrs);
14512e72dbbSrh87107 				return (NULL);
14612e72dbbSrh87107 			}
14712e72dbbSrh87107 			if (phdr.p_type != PT_LOAD || phdr.p_memsz == 0)
14812e72dbbSrh87107 				continue;
14912e72dbbSrh87107 
15012e72dbbSrh87107 			addr = phdr.p_vaddr;
15112e72dbbSrh87107 			if (ehdr.e_type == ET_DYN)
15212e72dbbSrh87107 				addr += ehdr_start;
15312e72dbbSrh87107 			if (last > addr)
15412e72dbbSrh87107 				unordered = 1;
15512e72dbbSrh87107 			addrs[naddrs++] = addr;
15612e72dbbSrh87107 			addrs[naddrs++] = last = addr + phdr.p_memsz - 1;
15712e72dbbSrh87107 		}
15812e72dbbSrh87107 #endif
15912e72dbbSrh87107 	}
16012e72dbbSrh87107 
16112e72dbbSrh87107 	if (unordered)
16212e72dbbSrh87107 		qsort(addrs, naddrs, sizeof (uintptr_t), addr_cmp);
16312e72dbbSrh87107 
16412e72dbbSrh87107 	*n = naddrs;
16512e72dbbSrh87107 	return (addrs);
16612e72dbbSrh87107 }
16712e72dbbSrh87107 
16812e72dbbSrh87107 /*
1697c478bd9Sstevel@tonic-gate  * Allocation function for a new file_info_t
1707c478bd9Sstevel@tonic-gate  */
171d7755b5aSrh87107 file_info_t *
1727c478bd9Sstevel@tonic-gate file_info_new(struct ps_prochandle *P, map_info_t *mptr)
1737c478bd9Sstevel@tonic-gate {
1747c478bd9Sstevel@tonic-gate 	file_info_t *fptr;
1757c478bd9Sstevel@tonic-gate 	map_info_t *mp;
176d7755b5aSrh87107 	uintptr_t mstart, mend, sstart, send;
177d7755b5aSrh87107 	uint_t i;
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate 	if ((fptr = calloc(1, sizeof (file_info_t))) == NULL)
1807c478bd9Sstevel@tonic-gate 		return (NULL);
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 	list_link(fptr, &P->file_head);
1837c478bd9Sstevel@tonic-gate 	(void) strcpy(fptr->file_pname, mptr->map_pmap.pr_mapname);
1847c478bd9Sstevel@tonic-gate 	mptr->map_file = fptr;
1857c478bd9Sstevel@tonic-gate 	fptr->file_ref = 1;
1867c478bd9Sstevel@tonic-gate 	fptr->file_fd = -1;
1877c478bd9Sstevel@tonic-gate 	P->num_files++;
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 	/*
1907c478bd9Sstevel@tonic-gate 	 * To figure out which map_info_t instances correspond to the mappings
19112e72dbbSrh87107 	 * for this load object we try to obtain the start and end address
19212e72dbbSrh87107 	 * for each section of our in-memory ELF image. If successful, we
1937c478bd9Sstevel@tonic-gate 	 * walk down the list of addresses and the list of map_info_t
1947c478bd9Sstevel@tonic-gate 	 * instances in lock step to correctly find the mappings that
1957c478bd9Sstevel@tonic-gate 	 * correspond to this load object.
1967c478bd9Sstevel@tonic-gate 	 */
19712e72dbbSrh87107 	if ((fptr->file_saddrs = get_saddrs(P, mptr->map_pmap.pr_vaddr,
19812e72dbbSrh87107 	    &fptr->file_nsaddrs)) == NULL)
1997c478bd9Sstevel@tonic-gate 		return (fptr);
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 	mp = P->mappings;
202d7755b5aSrh87107 	i = 0;
203d7755b5aSrh87107 	while (mp < P->mappings + P->map_count && i < fptr->file_nsaddrs) {
204d7755b5aSrh87107 
205d7755b5aSrh87107 		/* Calculate the start and end of the mapping and section */
206d7755b5aSrh87107 		mstart = mp->map_pmap.pr_vaddr;
207d7755b5aSrh87107 		mend = mp->map_pmap.pr_vaddr + mp->map_pmap.pr_size;
208d7755b5aSrh87107 		sstart = fptr->file_saddrs[i];
209d7755b5aSrh87107 		send = fptr->file_saddrs[i + 1];
210d7755b5aSrh87107 
211d7755b5aSrh87107 		if (mend <= sstart) {
212d7755b5aSrh87107 			/* This mapping is below the current section */
213d7755b5aSrh87107 			mp++;
214d7755b5aSrh87107 		} else if (mstart >= send) {
215d7755b5aSrh87107 			/* This mapping is above the current section */
216d7755b5aSrh87107 			i += 2;
217d7755b5aSrh87107 		} else {
218d7755b5aSrh87107 			/* This mapping overlaps the current section */
219d7755b5aSrh87107 			if (mp->map_file == NULL) {
220d7755b5aSrh87107 				dprintf("file_info_new: associating "
221d7755b5aSrh87107 				    "segment at %p\n",
222d7755b5aSrh87107 				    (void *)mp->map_pmap.pr_vaddr);
2237c478bd9Sstevel@tonic-gate 				mp->map_file = fptr;
2247c478bd9Sstevel@tonic-gate 				fptr->file_ref++;
2257c478bd9Sstevel@tonic-gate 			} else {
226d7755b5aSrh87107 				dprintf("file_info_new: segment at %p "
227d7755b5aSrh87107 				    "already associated with %s\n",
228d7755b5aSrh87107 				    (void *)mp->map_pmap.pr_vaddr,
229d7755b5aSrh87107 				    (mp == mptr ? "this file" :
230d7755b5aSrh87107 				    mp->map_file->file_pname));
231d7755b5aSrh87107 			}
2327c478bd9Sstevel@tonic-gate 			mp++;
2337c478bd9Sstevel@tonic-gate 		}
2347c478bd9Sstevel@tonic-gate 	}
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 	return (fptr);
2377c478bd9Sstevel@tonic-gate }
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate /*
2407c478bd9Sstevel@tonic-gate  * Deallocation function for a file_info_t
2417c478bd9Sstevel@tonic-gate  */
2427c478bd9Sstevel@tonic-gate static void
2437c478bd9Sstevel@tonic-gate file_info_free(struct ps_prochandle *P, file_info_t *fptr)
2447c478bd9Sstevel@tonic-gate {
2457c478bd9Sstevel@tonic-gate 	if (--fptr->file_ref == 0) {
2467c478bd9Sstevel@tonic-gate 		list_unlink(fptr);
2477c478bd9Sstevel@tonic-gate 		if (fptr->file_symtab.sym_elf) {
2487c478bd9Sstevel@tonic-gate 			(void) elf_end(fptr->file_symtab.sym_elf);
2497c478bd9Sstevel@tonic-gate 			free(fptr->file_symtab.sym_elfmem);
2507c478bd9Sstevel@tonic-gate 		}
2517c478bd9Sstevel@tonic-gate 		if (fptr->file_symtab.sym_byname)
2527c478bd9Sstevel@tonic-gate 			free(fptr->file_symtab.sym_byname);
2537c478bd9Sstevel@tonic-gate 		if (fptr->file_symtab.sym_byaddr)
2547c478bd9Sstevel@tonic-gate 			free(fptr->file_symtab.sym_byaddr);
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate 		if (fptr->file_dynsym.sym_elf) {
2577c478bd9Sstevel@tonic-gate 			(void) elf_end(fptr->file_dynsym.sym_elf);
2587c478bd9Sstevel@tonic-gate 			free(fptr->file_dynsym.sym_elfmem);
2597c478bd9Sstevel@tonic-gate 		}
2607c478bd9Sstevel@tonic-gate 		if (fptr->file_dynsym.sym_byname)
2617c478bd9Sstevel@tonic-gate 			free(fptr->file_dynsym.sym_byname);
2627c478bd9Sstevel@tonic-gate 		if (fptr->file_dynsym.sym_byaddr)
2637c478bd9Sstevel@tonic-gate 			free(fptr->file_dynsym.sym_byaddr);
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate 		if (fptr->file_lo)
2667c478bd9Sstevel@tonic-gate 			free(fptr->file_lo);
2677c478bd9Sstevel@tonic-gate 		if (fptr->file_lname)
2687c478bd9Sstevel@tonic-gate 			free(fptr->file_lname);
269186f7fbfSEdward Pilatowicz 		if (fptr->file_rname)
270186f7fbfSEdward Pilatowicz 			free(fptr->file_rname);
2717c478bd9Sstevel@tonic-gate 		if (fptr->file_elf)
2727c478bd9Sstevel@tonic-gate 			(void) elf_end(fptr->file_elf);
2737c478bd9Sstevel@tonic-gate 		if (fptr->file_elfmem != NULL)
2747c478bd9Sstevel@tonic-gate 			free(fptr->file_elfmem);
2757c478bd9Sstevel@tonic-gate 		if (fptr->file_fd >= 0)
2767c478bd9Sstevel@tonic-gate 			(void) close(fptr->file_fd);
2777c478bd9Sstevel@tonic-gate 		if (fptr->file_ctfp) {
2787c478bd9Sstevel@tonic-gate 			ctf_close(fptr->file_ctfp);
2797c478bd9Sstevel@tonic-gate 			free(fptr->file_ctf_buf);
2807c478bd9Sstevel@tonic-gate 		}
28112e72dbbSrh87107 		if (fptr->file_saddrs)
28212e72dbbSrh87107 			free(fptr->file_saddrs);
2837c478bd9Sstevel@tonic-gate 		free(fptr);
2847c478bd9Sstevel@tonic-gate 		P->num_files--;
2857c478bd9Sstevel@tonic-gate 	}
2867c478bd9Sstevel@tonic-gate }
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate /*
2897c478bd9Sstevel@tonic-gate  * Deallocation function for a map_info_t
2907c478bd9Sstevel@tonic-gate  */
2917c478bd9Sstevel@tonic-gate static void
2927c478bd9Sstevel@tonic-gate map_info_free(struct ps_prochandle *P, map_info_t *mptr)
2937c478bd9Sstevel@tonic-gate {
2947c478bd9Sstevel@tonic-gate 	file_info_t *fptr;
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate 	if ((fptr = mptr->map_file) != NULL) {
2977c478bd9Sstevel@tonic-gate 		if (fptr->file_map == mptr)
2987c478bd9Sstevel@tonic-gate 			fptr->file_map = NULL;
2997c478bd9Sstevel@tonic-gate 		file_info_free(P, fptr);
3007c478bd9Sstevel@tonic-gate 	}
3017c478bd9Sstevel@tonic-gate 	if (P->execname && mptr == P->map_exec) {
3027c478bd9Sstevel@tonic-gate 		free(P->execname);
3037c478bd9Sstevel@tonic-gate 		P->execname = NULL;
3047c478bd9Sstevel@tonic-gate 	}
3057c478bd9Sstevel@tonic-gate 	if (P->auxv && (mptr == P->map_exec || mptr == P->map_ldso)) {
3067c478bd9Sstevel@tonic-gate 		free(P->auxv);
3077c478bd9Sstevel@tonic-gate 		P->auxv = NULL;
3087c478bd9Sstevel@tonic-gate 		P->nauxv = 0;
3097c478bd9Sstevel@tonic-gate 	}
3107c478bd9Sstevel@tonic-gate 	if (mptr == P->map_exec)
3117c478bd9Sstevel@tonic-gate 		P->map_exec = NULL;
3127c478bd9Sstevel@tonic-gate 	if (mptr == P->map_ldso)
3137c478bd9Sstevel@tonic-gate 		P->map_ldso = NULL;
3147c478bd9Sstevel@tonic-gate }
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate /*
3177c478bd9Sstevel@tonic-gate  * Call-back function for librtld_db to iterate through all of its shared
3187c478bd9Sstevel@tonic-gate  * libraries.  We use this to get the load object names for the mappings.
3197c478bd9Sstevel@tonic-gate  */
3207c478bd9Sstevel@tonic-gate static int
3217c478bd9Sstevel@tonic-gate map_iter(const rd_loadobj_t *lop, void *cd)
3227c478bd9Sstevel@tonic-gate {
3237c478bd9Sstevel@tonic-gate 	char buf[PATH_MAX];
3247c478bd9Sstevel@tonic-gate 	struct ps_prochandle *P = cd;
3257c478bd9Sstevel@tonic-gate 	map_info_t *mptr;
3267c478bd9Sstevel@tonic-gate 	file_info_t *fptr;
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate 	dprintf("encountered rd object at %p\n", (void *)lop->rl_base);
3297c478bd9Sstevel@tonic-gate 
3309acbbeafSnn35248 	if ((mptr = Paddr2mptr(P, lop->rl_base)) == NULL) {
3319acbbeafSnn35248 		dprintf("map_iter: base address doesn't match any mapping\n");
3327c478bd9Sstevel@tonic-gate 		return (1); /* Base address does not match any mapping */
3339acbbeafSnn35248 	}
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate 	if ((fptr = mptr->map_file) == NULL &&
3369acbbeafSnn35248 	    (fptr = file_info_new(P, mptr)) == NULL) {
3379acbbeafSnn35248 		dprintf("map_iter: failed to allocate a new file_info_t\n");
3387c478bd9Sstevel@tonic-gate 		return (1); /* Failed to allocate a new file_info_t */
3399acbbeafSnn35248 	}
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 	if ((fptr->file_lo == NULL) &&
3427c478bd9Sstevel@tonic-gate 	    (fptr->file_lo = malloc(sizeof (rd_loadobj_t))) == NULL) {
3439acbbeafSnn35248 		dprintf("map_iter: failed to allocate rd_loadobj_t\n");
3447c478bd9Sstevel@tonic-gate 		file_info_free(P, fptr);
3457c478bd9Sstevel@tonic-gate 		return (1); /* Failed to allocate rd_loadobj_t */
3467c478bd9Sstevel@tonic-gate 	}
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate 	fptr->file_map = mptr;
3497c478bd9Sstevel@tonic-gate 	*fptr->file_lo = *lop;
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 	fptr->file_lo->rl_plt_base = fptr->file_plt_base;
3527c478bd9Sstevel@tonic-gate 	fptr->file_lo->rl_plt_size = fptr->file_plt_size;
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate 	if (fptr->file_lname) {
3557c478bd9Sstevel@tonic-gate 		free(fptr->file_lname);
3567c478bd9Sstevel@tonic-gate 		fptr->file_lname = NULL;
357186f7fbfSEdward Pilatowicz 		fptr->file_lbase = NULL;
358186f7fbfSEdward Pilatowicz 	}
359186f7fbfSEdward Pilatowicz 	if (fptr->file_rname) {
360186f7fbfSEdward Pilatowicz 		free(fptr->file_rname);
361186f7fbfSEdward Pilatowicz 		fptr->file_rname = NULL;
362186f7fbfSEdward Pilatowicz 		fptr->file_rbase = NULL;
3637c478bd9Sstevel@tonic-gate 	}
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate 	if (Pread_string(P, buf, sizeof (buf), lop->rl_nameaddr) > 0) {
3667c478bd9Sstevel@tonic-gate 		if ((fptr->file_lname = strdup(buf)) != NULL)
3677c478bd9Sstevel@tonic-gate 			fptr->file_lbase = basename(fptr->file_lname);
3689acbbeafSnn35248 	} else {
3699acbbeafSnn35248 		dprintf("map_iter: failed to read string at %p\n",
3709acbbeafSnn35248 		    (void *)lop->rl_nameaddr);
3717c478bd9Sstevel@tonic-gate 	}
3727c478bd9Sstevel@tonic-gate 
373186f7fbfSEdward Pilatowicz 	if ((Pfindmap(P, mptr, buf, sizeof (buf)) != NULL) &&
374186f7fbfSEdward Pilatowicz 	    ((fptr->file_rname = strdup(buf)) != NULL))
375186f7fbfSEdward Pilatowicz 		fptr->file_rbase = basename(fptr->file_rname);
376186f7fbfSEdward Pilatowicz 
3777c478bd9Sstevel@tonic-gate 	dprintf("loaded rd object %s lmid %lx\n",
378186f7fbfSEdward Pilatowicz 	    fptr->file_lname ? buf : "<NULL>", lop->rl_lmident);
3797c478bd9Sstevel@tonic-gate 	return (1);
3807c478bd9Sstevel@tonic-gate }
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate static void
3837c478bd9Sstevel@tonic-gate map_set(struct ps_prochandle *P, map_info_t *mptr, const char *lname)
3847c478bd9Sstevel@tonic-gate {
3857c478bd9Sstevel@tonic-gate 	file_info_t *fptr;
3864e4a6245SEdward Pilatowicz 	char buf[PATH_MAX];
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate 	if ((fptr = mptr->map_file) == NULL &&
3897c478bd9Sstevel@tonic-gate 	    (fptr = file_info_new(P, mptr)) == NULL)
3907c478bd9Sstevel@tonic-gate 		return; /* Failed to allocate a new file_info_t */
3917c478bd9Sstevel@tonic-gate 
3927c478bd9Sstevel@tonic-gate 	fptr->file_map = mptr;
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate 	if ((fptr->file_lo == NULL) &&
3957c478bd9Sstevel@tonic-gate 	    (fptr->file_lo = malloc(sizeof (rd_loadobj_t))) == NULL) {
3967c478bd9Sstevel@tonic-gate 		file_info_free(P, fptr);
3977c478bd9Sstevel@tonic-gate 		return; /* Failed to allocate rd_loadobj_t */
3987c478bd9Sstevel@tonic-gate 	}
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate 	(void) memset(fptr->file_lo, 0, sizeof (rd_loadobj_t));
4017c478bd9Sstevel@tonic-gate 	fptr->file_lo->rl_base = mptr->map_pmap.pr_vaddr;
4027c478bd9Sstevel@tonic-gate 	fptr->file_lo->rl_bend =
4037c478bd9Sstevel@tonic-gate 	    mptr->map_pmap.pr_vaddr + mptr->map_pmap.pr_size;
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate 	fptr->file_lo->rl_plt_base = fptr->file_plt_base;
4067c478bd9Sstevel@tonic-gate 	fptr->file_lo->rl_plt_size = fptr->file_plt_size;
4077c478bd9Sstevel@tonic-gate 
408186f7fbfSEdward Pilatowicz 	if ((fptr->file_lname == NULL) &&
4099acbbeafSnn35248 	    (fptr->file_lname = strdup(lname)) != NULL)
4107c478bd9Sstevel@tonic-gate 		fptr->file_lbase = basename(fptr->file_lname);
411186f7fbfSEdward Pilatowicz 
4124e4a6245SEdward Pilatowicz 	if ((Pfindmap(P, mptr, buf, sizeof (buf)) != NULL) &&
4134e4a6245SEdward Pilatowicz 	    ((fptr->file_rname = strdup(buf)) != NULL))
4144e4a6245SEdward Pilatowicz 		fptr->file_rbase = basename(fptr->file_rname);
4157c478bd9Sstevel@tonic-gate }
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate static void
4187c478bd9Sstevel@tonic-gate load_static_maps(struct ps_prochandle *P)
4197c478bd9Sstevel@tonic-gate {
4207c478bd9Sstevel@tonic-gate 	map_info_t *mptr;
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate 	/*
4237c478bd9Sstevel@tonic-gate 	 * Construct the map for the a.out.
4247c478bd9Sstevel@tonic-gate 	 */
4257c478bd9Sstevel@tonic-gate 	if ((mptr = object_name_to_map(P, PR_LMID_EVERY, PR_OBJ_EXEC)) != NULL)
4267c478bd9Sstevel@tonic-gate 		map_set(P, mptr, "a.out");
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate 	/*
4297c478bd9Sstevel@tonic-gate 	 * If the dynamic linker exists for this process,
4307c478bd9Sstevel@tonic-gate 	 * construct the map for it.
4317c478bd9Sstevel@tonic-gate 	 */
4327c478bd9Sstevel@tonic-gate 	if (Pgetauxval(P, AT_BASE) != -1L &&
4337c478bd9Sstevel@tonic-gate 	    (mptr = object_name_to_map(P, PR_LMID_EVERY, PR_OBJ_LDSO)) != NULL)
4347c478bd9Sstevel@tonic-gate 		map_set(P, mptr, "ld.so.1");
4357c478bd9Sstevel@tonic-gate }
4367c478bd9Sstevel@tonic-gate 
4377c478bd9Sstevel@tonic-gate /*
4387c478bd9Sstevel@tonic-gate  * Go through all the address space mappings, validating or updating
4397c478bd9Sstevel@tonic-gate  * the information already gathered, or gathering new information.
4407c478bd9Sstevel@tonic-gate  *
4417c478bd9Sstevel@tonic-gate  * This function is only called when we suspect that the mappings have changed
4427c478bd9Sstevel@tonic-gate  * because this is the first time we're calling it or because of rtld activity.
4437c478bd9Sstevel@tonic-gate  */
4447c478bd9Sstevel@tonic-gate void
4457c478bd9Sstevel@tonic-gate Pupdate_maps(struct ps_prochandle *P)
4467c478bd9Sstevel@tonic-gate {
4479acbbeafSnn35248 	char mapfile[PATH_MAX];
4487c478bd9Sstevel@tonic-gate 	int mapfd;
4497c478bd9Sstevel@tonic-gate 	struct stat statb;
4507c478bd9Sstevel@tonic-gate 	prmap_t *Pmap = NULL;
4517c478bd9Sstevel@tonic-gate 	prmap_t *pmap;
4527c478bd9Sstevel@tonic-gate 	ssize_t nmap;
4537c478bd9Sstevel@tonic-gate 	int i;
4547c478bd9Sstevel@tonic-gate 	uint_t oldmapcount;
4557c478bd9Sstevel@tonic-gate 	map_info_t *newmap, *newp;
4567c478bd9Sstevel@tonic-gate 	map_info_t *mptr;
4577c478bd9Sstevel@tonic-gate 
458a1b5e537Sbmc 	if (P->info_valid || P->state == PS_UNDEAD)
4597c478bd9Sstevel@tonic-gate 		return;
4607c478bd9Sstevel@tonic-gate 
4617c478bd9Sstevel@tonic-gate 	Preadauxvec(P);
4627c478bd9Sstevel@tonic-gate 
4639acbbeafSnn35248 	(void) snprintf(mapfile, sizeof (mapfile), "%s/%d/map",
4649acbbeafSnn35248 	    procfs_path, (int)P->pid);
4657c478bd9Sstevel@tonic-gate 	if ((mapfd = open(mapfile, O_RDONLY)) < 0 ||
4667c478bd9Sstevel@tonic-gate 	    fstat(mapfd, &statb) != 0 ||
4677c478bd9Sstevel@tonic-gate 	    statb.st_size < sizeof (prmap_t) ||
4687c478bd9Sstevel@tonic-gate 	    (Pmap = malloc(statb.st_size)) == NULL ||
4697c478bd9Sstevel@tonic-gate 	    (nmap = pread(mapfd, Pmap, statb.st_size, 0L)) <= 0 ||
4707c478bd9Sstevel@tonic-gate 	    (nmap /= sizeof (prmap_t)) == 0) {
4717c478bd9Sstevel@tonic-gate 		if (Pmap != NULL)
4727c478bd9Sstevel@tonic-gate 			free(Pmap);
4737c478bd9Sstevel@tonic-gate 		if (mapfd >= 0)
4747c478bd9Sstevel@tonic-gate 			(void) close(mapfd);
4757c478bd9Sstevel@tonic-gate 		Preset_maps(P);	/* utter failure; destroy tables */
4767c478bd9Sstevel@tonic-gate 		return;
4777c478bd9Sstevel@tonic-gate 	}
4787c478bd9Sstevel@tonic-gate 	(void) close(mapfd);
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate 	if ((newmap = calloc(1, nmap * sizeof (map_info_t))) == NULL)
4817c478bd9Sstevel@tonic-gate 		return;
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate 	/*
4847c478bd9Sstevel@tonic-gate 	 * We try to merge any file information we may have for existing
4857c478bd9Sstevel@tonic-gate 	 * mappings, to avoid having to rebuild the file info.
4867c478bd9Sstevel@tonic-gate 	 */
4877c478bd9Sstevel@tonic-gate 	mptr = P->mappings;
4887c478bd9Sstevel@tonic-gate 	pmap = Pmap;
4897c478bd9Sstevel@tonic-gate 	newp = newmap;
4907c478bd9Sstevel@tonic-gate 	oldmapcount = P->map_count;
4917c478bd9Sstevel@tonic-gate 	for (i = 0; i < nmap; i++, pmap++, newp++) {
4927c478bd9Sstevel@tonic-gate 
4937c478bd9Sstevel@tonic-gate 		if (oldmapcount == 0) {
4947c478bd9Sstevel@tonic-gate 			/*
4957c478bd9Sstevel@tonic-gate 			 * We've exhausted all the old mappings.  Every new
4967c478bd9Sstevel@tonic-gate 			 * mapping should be added.
4977c478bd9Sstevel@tonic-gate 			 */
4987c478bd9Sstevel@tonic-gate 			newp->map_pmap = *pmap;
4997c478bd9Sstevel@tonic-gate 
5007c478bd9Sstevel@tonic-gate 		} else if (pmap->pr_vaddr == mptr->map_pmap.pr_vaddr &&
5017c478bd9Sstevel@tonic-gate 		    pmap->pr_size == mptr->map_pmap.pr_size &&
5027c478bd9Sstevel@tonic-gate 		    pmap->pr_offset == mptr->map_pmap.pr_offset &&
5037c478bd9Sstevel@tonic-gate 		    (pmap->pr_mflags & ~(MA_BREAK | MA_STACK)) ==
5047c478bd9Sstevel@tonic-gate 		    (mptr->map_pmap.pr_mflags & ~(MA_BREAK | MA_STACK)) &&
5057c478bd9Sstevel@tonic-gate 		    pmap->pr_pagesize == mptr->map_pmap.pr_pagesize &&
5067c478bd9Sstevel@tonic-gate 		    pmap->pr_shmid == mptr->map_pmap.pr_shmid &&
5077c478bd9Sstevel@tonic-gate 		    strcmp(pmap->pr_mapname, mptr->map_pmap.pr_mapname) == 0) {
5087c478bd9Sstevel@tonic-gate 
5097c478bd9Sstevel@tonic-gate 			/*
5107c478bd9Sstevel@tonic-gate 			 * This mapping matches exactly.  Copy over the old
5117c478bd9Sstevel@tonic-gate 			 * mapping, taking care to get the latest flags.
5127c478bd9Sstevel@tonic-gate 			 * Make sure the associated file_info_t is updated
5137c478bd9Sstevel@tonic-gate 			 * appropriately.
5147c478bd9Sstevel@tonic-gate 			 */
5157c478bd9Sstevel@tonic-gate 			*newp = *mptr;
5167c478bd9Sstevel@tonic-gate 			if (P->map_exec == mptr)
5177c478bd9Sstevel@tonic-gate 				P->map_exec = newp;
5187c478bd9Sstevel@tonic-gate 			if (P->map_ldso == mptr)
5197c478bd9Sstevel@tonic-gate 				P->map_ldso = newp;
5207c478bd9Sstevel@tonic-gate 			newp->map_pmap.pr_mflags = pmap->pr_mflags;
5217c478bd9Sstevel@tonic-gate 			if (mptr->map_file != NULL &&
5227c478bd9Sstevel@tonic-gate 			    mptr->map_file->file_map == mptr)
5237c478bd9Sstevel@tonic-gate 				mptr->map_file->file_map = newp;
5247c478bd9Sstevel@tonic-gate 			oldmapcount--;
5257c478bd9Sstevel@tonic-gate 			mptr++;
5267c478bd9Sstevel@tonic-gate 
5277c478bd9Sstevel@tonic-gate 		} else if (pmap->pr_vaddr + pmap->pr_size >
5287c478bd9Sstevel@tonic-gate 		    mptr->map_pmap.pr_vaddr) {
5297c478bd9Sstevel@tonic-gate 
5307c478bd9Sstevel@tonic-gate 			/*
5317c478bd9Sstevel@tonic-gate 			 * The old mapping doesn't exist any more, remove it
5327c478bd9Sstevel@tonic-gate 			 * from the list.
5337c478bd9Sstevel@tonic-gate 			 */
5347c478bd9Sstevel@tonic-gate 			map_info_free(P, mptr);
5357c478bd9Sstevel@tonic-gate 			oldmapcount--;
5367c478bd9Sstevel@tonic-gate 			i--;
5377c478bd9Sstevel@tonic-gate 			newp--;
5387c478bd9Sstevel@tonic-gate 			pmap--;
5397c478bd9Sstevel@tonic-gate 			mptr++;
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate 		} else {
5427c478bd9Sstevel@tonic-gate 
5437c478bd9Sstevel@tonic-gate 			/*
5447c478bd9Sstevel@tonic-gate 			 * This is a new mapping, add it directly.
5457c478bd9Sstevel@tonic-gate 			 */
5467c478bd9Sstevel@tonic-gate 			newp->map_pmap = *pmap;
5477c478bd9Sstevel@tonic-gate 		}
5487c478bd9Sstevel@tonic-gate 	}
5497c478bd9Sstevel@tonic-gate 
5507c478bd9Sstevel@tonic-gate 	/*
5517c478bd9Sstevel@tonic-gate 	 * Free any old maps
5527c478bd9Sstevel@tonic-gate 	 */
5537c478bd9Sstevel@tonic-gate 	while (oldmapcount) {
5547c478bd9Sstevel@tonic-gate 		map_info_free(P, mptr);
5557c478bd9Sstevel@tonic-gate 		oldmapcount--;
5567c478bd9Sstevel@tonic-gate 		mptr++;
5577c478bd9Sstevel@tonic-gate 	}
5587c478bd9Sstevel@tonic-gate 
5597c478bd9Sstevel@tonic-gate 	free(Pmap);
5607c478bd9Sstevel@tonic-gate 	if (P->mappings != NULL)
5617c478bd9Sstevel@tonic-gate 		free(P->mappings);
5627c478bd9Sstevel@tonic-gate 	P->mappings = newmap;
5637c478bd9Sstevel@tonic-gate 	P->map_count = P->map_alloc = nmap;
5647c478bd9Sstevel@tonic-gate 	P->info_valid = 1;
5657c478bd9Sstevel@tonic-gate 
5667c478bd9Sstevel@tonic-gate 	/*
5677c478bd9Sstevel@tonic-gate 	 * Consult librtld_db to get the load object
5687c478bd9Sstevel@tonic-gate 	 * names for all of the shared libraries.
5697c478bd9Sstevel@tonic-gate 	 */
5707c478bd9Sstevel@tonic-gate 	if (P->rap != NULL)
5717c478bd9Sstevel@tonic-gate 		(void) rd_loadobj_iter(P->rap, map_iter, P);
5727c478bd9Sstevel@tonic-gate }
5737c478bd9Sstevel@tonic-gate 
5747c478bd9Sstevel@tonic-gate /*
5757c478bd9Sstevel@tonic-gate  * Update all of the mappings and rtld_db as if by Pupdate_maps(), and then
5767c478bd9Sstevel@tonic-gate  * forcibly cache all of the symbol tables associated with all object files.
5777c478bd9Sstevel@tonic-gate  */
5787c478bd9Sstevel@tonic-gate void
5797c478bd9Sstevel@tonic-gate Pupdate_syms(struct ps_prochandle *P)
5807c478bd9Sstevel@tonic-gate {
5814bfb3cb6Srh87107 	file_info_t *fptr;
5827c478bd9Sstevel@tonic-gate 	int i;
5837c478bd9Sstevel@tonic-gate 
5847c478bd9Sstevel@tonic-gate 	Pupdate_maps(P);
5857c478bd9Sstevel@tonic-gate 
5864bfb3cb6Srh87107 	for (i = 0, fptr = list_next(&P->file_head); i < P->num_files;
5874bfb3cb6Srh87107 	    i++, fptr = list_next(fptr)) {
5887c478bd9Sstevel@tonic-gate 		Pbuild_file_symtab(P, fptr);
5897c478bd9Sstevel@tonic-gate 		(void) Pbuild_file_ctf(P, fptr);
5907c478bd9Sstevel@tonic-gate 	}
5917c478bd9Sstevel@tonic-gate }
5927c478bd9Sstevel@tonic-gate 
5937c478bd9Sstevel@tonic-gate /*
5947c478bd9Sstevel@tonic-gate  * Return the librtld_db agent handle for the victim process.
5957c478bd9Sstevel@tonic-gate  * The handle will become invalid at the next successful exec() and the
5967c478bd9Sstevel@tonic-gate  * client (caller of proc_rd_agent()) must not use it beyond that point.
5977c478bd9Sstevel@tonic-gate  * If the process is already dead, we've already tried our best to
5987c478bd9Sstevel@tonic-gate  * create the agent during core file initialization.
5997c478bd9Sstevel@tonic-gate  */
6007c478bd9Sstevel@tonic-gate rd_agent_t *
6017c478bd9Sstevel@tonic-gate Prd_agent(struct ps_prochandle *P)
6027c478bd9Sstevel@tonic-gate {
6037c478bd9Sstevel@tonic-gate 	if (P->rap == NULL && P->state != PS_DEAD && P->state != PS_IDLE) {
6047c478bd9Sstevel@tonic-gate 		Pupdate_maps(P);
6057c478bd9Sstevel@tonic-gate 		if (P->num_files == 0)
6067c478bd9Sstevel@tonic-gate 			load_static_maps(P);
6077c478bd9Sstevel@tonic-gate 		rd_log(_libproc_debug);
6087c478bd9Sstevel@tonic-gate 		if ((P->rap = rd_new(P)) != NULL)
6097c478bd9Sstevel@tonic-gate 			(void) rd_loadobj_iter(P->rap, map_iter, P);
6107c478bd9Sstevel@tonic-gate 	}
6117c478bd9Sstevel@tonic-gate 	return (P->rap);
6127c478bd9Sstevel@tonic-gate }
6137c478bd9Sstevel@tonic-gate 
6147c478bd9Sstevel@tonic-gate /*
6157c478bd9Sstevel@tonic-gate  * Return the prmap_t structure containing 'addr', but only if it
6167c478bd9Sstevel@tonic-gate  * is in the dynamic linker's link map and is the text section.
6177c478bd9Sstevel@tonic-gate  */
6187c478bd9Sstevel@tonic-gate const prmap_t *
6197c478bd9Sstevel@tonic-gate Paddr_to_text_map(struct ps_prochandle *P, uintptr_t addr)
6207c478bd9Sstevel@tonic-gate {
6217c478bd9Sstevel@tonic-gate 	map_info_t *mptr;
6227c478bd9Sstevel@tonic-gate 
6237c478bd9Sstevel@tonic-gate 	if (!P->info_valid)
6247c478bd9Sstevel@tonic-gate 		Pupdate_maps(P);
6257c478bd9Sstevel@tonic-gate 
6267c478bd9Sstevel@tonic-gate 	if ((mptr = Paddr2mptr(P, addr)) != NULL) {
6277c478bd9Sstevel@tonic-gate 		file_info_t *fptr = build_map_symtab(P, mptr);
6287c478bd9Sstevel@tonic-gate 		const prmap_t *pmp = &mptr->map_pmap;
6297c478bd9Sstevel@tonic-gate 
630d7755b5aSrh87107 		/*
631d7755b5aSrh87107 		 * Assume that if rl_data_base is NULL, it means that no
632d7755b5aSrh87107 		 * data section was found for this load object, and that
633d7755b5aSrh87107 		 * a section must be text. Otherwise, a section will be
634d7755b5aSrh87107 		 * text unless it ends above the start of the data
635d7755b5aSrh87107 		 * section.
636d7755b5aSrh87107 		 */
6377c478bd9Sstevel@tonic-gate 		if (fptr != NULL && fptr->file_lo != NULL &&
638d7755b5aSrh87107 		    (fptr->file_lo->rl_data_base == NULL ||
639d7755b5aSrh87107 		    pmp->pr_vaddr + pmp->pr_size <
640d7755b5aSrh87107 		    fptr->file_lo->rl_data_base))
6417c478bd9Sstevel@tonic-gate 			return (pmp);
6427c478bd9Sstevel@tonic-gate 	}
6437c478bd9Sstevel@tonic-gate 
6447c478bd9Sstevel@tonic-gate 	return (NULL);
6457c478bd9Sstevel@tonic-gate }
6467c478bd9Sstevel@tonic-gate 
6477c478bd9Sstevel@tonic-gate /*
6487c478bd9Sstevel@tonic-gate  * Return the prmap_t structure containing 'addr' (no restrictions on
6497c478bd9Sstevel@tonic-gate  * the type of mapping).
6507c478bd9Sstevel@tonic-gate  */
6517c478bd9Sstevel@tonic-gate const prmap_t *
6527c478bd9Sstevel@tonic-gate Paddr_to_map(struct ps_prochandle *P, uintptr_t addr)
6537c478bd9Sstevel@tonic-gate {
6547c478bd9Sstevel@tonic-gate 	map_info_t *mptr;
6557c478bd9Sstevel@tonic-gate 
6567c478bd9Sstevel@tonic-gate 	if (!P->info_valid)
6577c478bd9Sstevel@tonic-gate 		Pupdate_maps(P);
6587c478bd9Sstevel@tonic-gate 
6597c478bd9Sstevel@tonic-gate 	if ((mptr = Paddr2mptr(P, addr)) != NULL)
6607c478bd9Sstevel@tonic-gate 		return (&mptr->map_pmap);
6617c478bd9Sstevel@tonic-gate 
6627c478bd9Sstevel@tonic-gate 	return (NULL);
6637c478bd9Sstevel@tonic-gate }
6647c478bd9Sstevel@tonic-gate 
6657c478bd9Sstevel@tonic-gate /*
6667c478bd9Sstevel@tonic-gate  * Convert a full or partial load object name to the prmap_t for its
6677c478bd9Sstevel@tonic-gate  * corresponding primary text mapping.
6687c478bd9Sstevel@tonic-gate  */
6697c478bd9Sstevel@tonic-gate const prmap_t *
6707c478bd9Sstevel@tonic-gate Plmid_to_map(struct ps_prochandle *P, Lmid_t lmid, const char *name)
6717c478bd9Sstevel@tonic-gate {
6727c478bd9Sstevel@tonic-gate 	map_info_t *mptr;
6737c478bd9Sstevel@tonic-gate 
6747c478bd9Sstevel@tonic-gate 	if (name == PR_OBJ_EVERY)
6757c478bd9Sstevel@tonic-gate 		return (NULL); /* A reasonable mistake */
6767c478bd9Sstevel@tonic-gate 
6777c478bd9Sstevel@tonic-gate 	if ((mptr = object_name_to_map(P, lmid, name)) != NULL)
6787c478bd9Sstevel@tonic-gate 		return (&mptr->map_pmap);
6797c478bd9Sstevel@tonic-gate 
6807c478bd9Sstevel@tonic-gate 	return (NULL);
6817c478bd9Sstevel@tonic-gate }
6827c478bd9Sstevel@tonic-gate 
6837c478bd9Sstevel@tonic-gate const prmap_t *
6847c478bd9Sstevel@tonic-gate Pname_to_map(struct ps_prochandle *P, const char *name)
6857c478bd9Sstevel@tonic-gate {
6867c478bd9Sstevel@tonic-gate 	return (Plmid_to_map(P, PR_LMID_EVERY, name));
6877c478bd9Sstevel@tonic-gate }
6887c478bd9Sstevel@tonic-gate 
6897c478bd9Sstevel@tonic-gate const rd_loadobj_t *
6907c478bd9Sstevel@tonic-gate Paddr_to_loadobj(struct ps_prochandle *P, uintptr_t addr)
6917c478bd9Sstevel@tonic-gate {
6927c478bd9Sstevel@tonic-gate 	map_info_t *mptr;
6937c478bd9Sstevel@tonic-gate 
6947c478bd9Sstevel@tonic-gate 	if (!P->info_valid)
6957c478bd9Sstevel@tonic-gate 		Pupdate_maps(P);
6967c478bd9Sstevel@tonic-gate 
6977c478bd9Sstevel@tonic-gate 	if ((mptr = Paddr2mptr(P, addr)) == NULL)
6987c478bd9Sstevel@tonic-gate 		return (NULL);
6997c478bd9Sstevel@tonic-gate 
7007c478bd9Sstevel@tonic-gate 	/*
7017c478bd9Sstevel@tonic-gate 	 * By building the symbol table, we implicitly bring the PLT
7027c478bd9Sstevel@tonic-gate 	 * information up to date in the load object.
7037c478bd9Sstevel@tonic-gate 	 */
7047c478bd9Sstevel@tonic-gate 	(void) build_map_symtab(P, mptr);
7057c478bd9Sstevel@tonic-gate 
7067c478bd9Sstevel@tonic-gate 	return (mptr->map_file->file_lo);
7077c478bd9Sstevel@tonic-gate }
7087c478bd9Sstevel@tonic-gate 
7097c478bd9Sstevel@tonic-gate const rd_loadobj_t *
7107c478bd9Sstevel@tonic-gate Plmid_to_loadobj(struct ps_prochandle *P, Lmid_t lmid, const char *name)
7117c478bd9Sstevel@tonic-gate {
7127c478bd9Sstevel@tonic-gate 	map_info_t *mptr;
7137c478bd9Sstevel@tonic-gate 
7147c478bd9Sstevel@tonic-gate 	if (name == PR_OBJ_EVERY)
7157c478bd9Sstevel@tonic-gate 		return (NULL);
7167c478bd9Sstevel@tonic-gate 
7177c478bd9Sstevel@tonic-gate 	if ((mptr = object_name_to_map(P, lmid, name)) == NULL)
7187c478bd9Sstevel@tonic-gate 		return (NULL);
7197c478bd9Sstevel@tonic-gate 
7207c478bd9Sstevel@tonic-gate 	/*
7217c478bd9Sstevel@tonic-gate 	 * By building the symbol table, we implicitly bring the PLT
7227c478bd9Sstevel@tonic-gate 	 * information up to date in the load object.
7237c478bd9Sstevel@tonic-gate 	 */
7247c478bd9Sstevel@tonic-gate 	(void) build_map_symtab(P, mptr);
7257c478bd9Sstevel@tonic-gate 
7267c478bd9Sstevel@tonic-gate 	return (mptr->map_file->file_lo);
7277c478bd9Sstevel@tonic-gate }
7287c478bd9Sstevel@tonic-gate 
7297c478bd9Sstevel@tonic-gate const rd_loadobj_t *
7307c478bd9Sstevel@tonic-gate Pname_to_loadobj(struct ps_prochandle *P, const char *name)
7317c478bd9Sstevel@tonic-gate {
7327c478bd9Sstevel@tonic-gate 	return (Plmid_to_loadobj(P, PR_LMID_EVERY, name));
7337c478bd9Sstevel@tonic-gate }
7347c478bd9Sstevel@tonic-gate 
7357c478bd9Sstevel@tonic-gate ctf_file_t *
7367c478bd9Sstevel@tonic-gate Pbuild_file_ctf(struct ps_prochandle *P, file_info_t *fptr)
7377c478bd9Sstevel@tonic-gate {
7387c478bd9Sstevel@tonic-gate 	ctf_sect_t ctdata, symtab, strtab;
7397c478bd9Sstevel@tonic-gate 	sym_tbl_t *symp;
7407c478bd9Sstevel@tonic-gate 	int err;
7417c478bd9Sstevel@tonic-gate 
7427c478bd9Sstevel@tonic-gate 	if (fptr->file_ctfp != NULL)
7437c478bd9Sstevel@tonic-gate 		return (fptr->file_ctfp);
7447c478bd9Sstevel@tonic-gate 
7457c478bd9Sstevel@tonic-gate 	Pbuild_file_symtab(P, fptr);
7467c478bd9Sstevel@tonic-gate 
7477c478bd9Sstevel@tonic-gate 	if (fptr->file_ctf_size == 0)
7487c478bd9Sstevel@tonic-gate 		return (NULL);
7497c478bd9Sstevel@tonic-gate 
7507c478bd9Sstevel@tonic-gate 	symp = fptr->file_ctf_dyn ? &fptr->file_dynsym : &fptr->file_symtab;
751d51e9074Sab196087 	if (symp->sym_data_pri == NULL)
7527c478bd9Sstevel@tonic-gate 		return (NULL);
7537c478bd9Sstevel@tonic-gate 
7547c478bd9Sstevel@tonic-gate 	/*
7557c478bd9Sstevel@tonic-gate 	 * The buffer may alread be allocated if this is a core file that
7567c478bd9Sstevel@tonic-gate 	 * contained CTF data for this file.
7577c478bd9Sstevel@tonic-gate 	 */
7587c478bd9Sstevel@tonic-gate 	if (fptr->file_ctf_buf == NULL) {
7597c478bd9Sstevel@tonic-gate 		fptr->file_ctf_buf = malloc(fptr->file_ctf_size);
7607c478bd9Sstevel@tonic-gate 		if (fptr->file_ctf_buf == NULL) {
7617c478bd9Sstevel@tonic-gate 			dprintf("failed to allocate ctf buffer\n");
7627c478bd9Sstevel@tonic-gate 			return (NULL);
7637c478bd9Sstevel@tonic-gate 		}
7647c478bd9Sstevel@tonic-gate 
7657c478bd9Sstevel@tonic-gate 		if (pread(fptr->file_fd, fptr->file_ctf_buf,
7667c478bd9Sstevel@tonic-gate 		    fptr->file_ctf_size, fptr->file_ctf_off) !=
7677c478bd9Sstevel@tonic-gate 		    fptr->file_ctf_size) {
7687c478bd9Sstevel@tonic-gate 			free(fptr->file_ctf_buf);
7697c478bd9Sstevel@tonic-gate 			fptr->file_ctf_buf = NULL;
7707c478bd9Sstevel@tonic-gate 			dprintf("failed to read ctf data\n");
7717c478bd9Sstevel@tonic-gate 			return (NULL);
7727c478bd9Sstevel@tonic-gate 		}
7737c478bd9Sstevel@tonic-gate 	}
7747c478bd9Sstevel@tonic-gate 
7757c478bd9Sstevel@tonic-gate 	ctdata.cts_name = ".SUNW_ctf";
7767c478bd9Sstevel@tonic-gate 	ctdata.cts_type = SHT_PROGBITS;
7777c478bd9Sstevel@tonic-gate 	ctdata.cts_flags = 0;
7787c478bd9Sstevel@tonic-gate 	ctdata.cts_data = fptr->file_ctf_buf;
7797c478bd9Sstevel@tonic-gate 	ctdata.cts_size = fptr->file_ctf_size;
7807c478bd9Sstevel@tonic-gate 	ctdata.cts_entsize = 1;
7817c478bd9Sstevel@tonic-gate 	ctdata.cts_offset = 0;
7827c478bd9Sstevel@tonic-gate 
7837c478bd9Sstevel@tonic-gate 	symtab.cts_name = fptr->file_ctf_dyn ? ".dynsym" : ".symtab";
784d51e9074Sab196087 	symtab.cts_type = symp->sym_hdr_pri.sh_type;
785d51e9074Sab196087 	symtab.cts_flags = symp->sym_hdr_pri.sh_flags;
786d51e9074Sab196087 	symtab.cts_data = symp->sym_data_pri->d_buf;
787d51e9074Sab196087 	symtab.cts_size = symp->sym_hdr_pri.sh_size;
788d51e9074Sab196087 	symtab.cts_entsize = symp->sym_hdr_pri.sh_entsize;
789d51e9074Sab196087 	symtab.cts_offset = symp->sym_hdr_pri.sh_offset;
7907c478bd9Sstevel@tonic-gate 
7917c478bd9Sstevel@tonic-gate 	strtab.cts_name = fptr->file_ctf_dyn ? ".dynstr" : ".strtab";
7927c478bd9Sstevel@tonic-gate 	strtab.cts_type = symp->sym_strhdr.sh_type;
7937c478bd9Sstevel@tonic-gate 	strtab.cts_flags = symp->sym_strhdr.sh_flags;
7947c478bd9Sstevel@tonic-gate 	strtab.cts_data = symp->sym_strs;
7957c478bd9Sstevel@tonic-gate 	strtab.cts_size = symp->sym_strhdr.sh_size;
7967c478bd9Sstevel@tonic-gate 	strtab.cts_entsize = symp->sym_strhdr.sh_entsize;
7977c478bd9Sstevel@tonic-gate 	strtab.cts_offset = symp->sym_strhdr.sh_offset;
7987c478bd9Sstevel@tonic-gate 
7997c478bd9Sstevel@tonic-gate 	fptr->file_ctfp = ctf_bufopen(&ctdata, &symtab, &strtab, &err);
8007c478bd9Sstevel@tonic-gate 	if (fptr->file_ctfp == NULL) {
8017c478bd9Sstevel@tonic-gate 		free(fptr->file_ctf_buf);
8027c478bd9Sstevel@tonic-gate 		fptr->file_ctf_buf = NULL;
8037c478bd9Sstevel@tonic-gate 		return (NULL);
8047c478bd9Sstevel@tonic-gate 	}
8057c478bd9Sstevel@tonic-gate 
8067c478bd9Sstevel@tonic-gate 	dprintf("loaded %lu bytes of CTF data for %s\n",
8077c478bd9Sstevel@tonic-gate 	    (ulong_t)fptr->file_ctf_size, fptr->file_pname);
8087c478bd9Sstevel@tonic-gate 
8097c478bd9Sstevel@tonic-gate 	return (fptr->file_ctfp);
8107c478bd9Sstevel@tonic-gate }
8117c478bd9Sstevel@tonic-gate 
8127c478bd9Sstevel@tonic-gate ctf_file_t *
8137c478bd9Sstevel@tonic-gate Paddr_to_ctf(struct ps_prochandle *P, uintptr_t addr)
8147c478bd9Sstevel@tonic-gate {
8157c478bd9Sstevel@tonic-gate 	map_info_t *mptr;
8167c478bd9Sstevel@tonic-gate 	file_info_t *fptr;
8177c478bd9Sstevel@tonic-gate 
8187c478bd9Sstevel@tonic-gate 	if (!P->info_valid)
8197c478bd9Sstevel@tonic-gate 		Pupdate_maps(P);
8207c478bd9Sstevel@tonic-gate 
8217c478bd9Sstevel@tonic-gate 	if ((mptr = Paddr2mptr(P, addr)) == NULL ||
8227c478bd9Sstevel@tonic-gate 	    (fptr = mptr->map_file) == NULL)
8237c478bd9Sstevel@tonic-gate 		return (NULL);
8247c478bd9Sstevel@tonic-gate 
8257c478bd9Sstevel@tonic-gate 	return (Pbuild_file_ctf(P, fptr));
8267c478bd9Sstevel@tonic-gate }
8277c478bd9Sstevel@tonic-gate 
8287c478bd9Sstevel@tonic-gate ctf_file_t *
8297c478bd9Sstevel@tonic-gate Plmid_to_ctf(struct ps_prochandle *P, Lmid_t lmid, const char *name)
8307c478bd9Sstevel@tonic-gate {
8317c478bd9Sstevel@tonic-gate 	map_info_t *mptr;
8327c478bd9Sstevel@tonic-gate 	file_info_t *fptr;
8337c478bd9Sstevel@tonic-gate 
8347c478bd9Sstevel@tonic-gate 	if (name == PR_OBJ_EVERY)
8357c478bd9Sstevel@tonic-gate 		return (NULL);
8367c478bd9Sstevel@tonic-gate 
8377c478bd9Sstevel@tonic-gate 	if ((mptr = object_name_to_map(P, lmid, name)) == NULL ||
8387c478bd9Sstevel@tonic-gate 	    (fptr = mptr->map_file) == NULL)
8397c478bd9Sstevel@tonic-gate 		return (NULL);
8407c478bd9Sstevel@tonic-gate 
8417c478bd9Sstevel@tonic-gate 	return (Pbuild_file_ctf(P, fptr));
8427c478bd9Sstevel@tonic-gate }
8437c478bd9Sstevel@tonic-gate 
8447c478bd9Sstevel@tonic-gate ctf_file_t *
8457c478bd9Sstevel@tonic-gate Pname_to_ctf(struct ps_prochandle *P, const char *name)
8467c478bd9Sstevel@tonic-gate {
8477c478bd9Sstevel@tonic-gate 	return (Plmid_to_ctf(P, PR_LMID_EVERY, name));
8487c478bd9Sstevel@tonic-gate }
8497c478bd9Sstevel@tonic-gate 
8507c478bd9Sstevel@tonic-gate /*
8517c478bd9Sstevel@tonic-gate  * If we're not a core file, re-read the /proc/<pid>/auxv file and store
8527c478bd9Sstevel@tonic-gate  * its contents in P->auxv.  In the case of a core file, we either
8537c478bd9Sstevel@tonic-gate  * initialized P->auxv in Pcore() from the NT_AUXV, or we don't have an
8547c478bd9Sstevel@tonic-gate  * auxv because the note was missing.
8557c478bd9Sstevel@tonic-gate  */
8567c478bd9Sstevel@tonic-gate void
8577c478bd9Sstevel@tonic-gate Preadauxvec(struct ps_prochandle *P)
8587c478bd9Sstevel@tonic-gate {
8597c478bd9Sstevel@tonic-gate 	char auxfile[64];
8607c478bd9Sstevel@tonic-gate 	struct stat statb;
8617c478bd9Sstevel@tonic-gate 	ssize_t naux;
8627c478bd9Sstevel@tonic-gate 	int fd;
8637c478bd9Sstevel@tonic-gate 
8647c478bd9Sstevel@tonic-gate 	if (P->state == PS_DEAD)
8657c478bd9Sstevel@tonic-gate 		return; /* Already read during Pgrab_core() */
8667c478bd9Sstevel@tonic-gate 	if (P->state == PS_IDLE)
8677c478bd9Sstevel@tonic-gate 		return; /* No aux vec for Pgrab_file() */
8687c478bd9Sstevel@tonic-gate 
8697c478bd9Sstevel@tonic-gate 	if (P->auxv != NULL) {
8707c478bd9Sstevel@tonic-gate 		free(P->auxv);
8717c478bd9Sstevel@tonic-gate 		P->auxv = NULL;
8727c478bd9Sstevel@tonic-gate 		P->nauxv = 0;
8737c478bd9Sstevel@tonic-gate 	}
8747c478bd9Sstevel@tonic-gate 
8759acbbeafSnn35248 	(void) snprintf(auxfile, sizeof (auxfile), "%s/%d/auxv",
8769acbbeafSnn35248 	    procfs_path, (int)P->pid);
8777c478bd9Sstevel@tonic-gate 	if ((fd = open(auxfile, O_RDONLY)) < 0)
8787c478bd9Sstevel@tonic-gate 		return;
8797c478bd9Sstevel@tonic-gate 
8807c478bd9Sstevel@tonic-gate 	if (fstat(fd, &statb) == 0 &&
8817c478bd9Sstevel@tonic-gate 	    statb.st_size >= sizeof (auxv_t) &&
8827c478bd9Sstevel@tonic-gate 	    (P->auxv = malloc(statb.st_size + sizeof (auxv_t))) != NULL) {
8837c478bd9Sstevel@tonic-gate 		if ((naux = read(fd, P->auxv, statb.st_size)) < 0 ||
8847c478bd9Sstevel@tonic-gate 		    (naux /= sizeof (auxv_t)) < 1) {
8857c478bd9Sstevel@tonic-gate 			free(P->auxv);
8867c478bd9Sstevel@tonic-gate 			P->auxv = NULL;
8877c478bd9Sstevel@tonic-gate 		} else {
8887c478bd9Sstevel@tonic-gate 			P->auxv[naux].a_type = AT_NULL;
8897c478bd9Sstevel@tonic-gate 			P->auxv[naux].a_un.a_val = 0L;
8907c478bd9Sstevel@tonic-gate 			P->nauxv = (int)naux;
8917c478bd9Sstevel@tonic-gate 		}
8927c478bd9Sstevel@tonic-gate 	}
8937c478bd9Sstevel@tonic-gate 
8947c478bd9Sstevel@tonic-gate 	(void) close(fd);
8957c478bd9Sstevel@tonic-gate }
8967c478bd9Sstevel@tonic-gate 
8977c478bd9Sstevel@tonic-gate /*
8987c478bd9Sstevel@tonic-gate  * Return a requested element from the process's aux vector.
8997c478bd9Sstevel@tonic-gate  * Return -1 on failure (this is adequate for our purposes).
9007c478bd9Sstevel@tonic-gate  */
9017c478bd9Sstevel@tonic-gate long
9027c478bd9Sstevel@tonic-gate Pgetauxval(struct ps_prochandle *P, int type)
9037c478bd9Sstevel@tonic-gate {
9047c478bd9Sstevel@tonic-gate 	auxv_t *auxv;
9057c478bd9Sstevel@tonic-gate 
9067c478bd9Sstevel@tonic-gate 	if (P->auxv == NULL)
9077c478bd9Sstevel@tonic-gate 		Preadauxvec(P);
9087c478bd9Sstevel@tonic-gate 
9097c478bd9Sstevel@tonic-gate 	if (P->auxv == NULL)
9107c478bd9Sstevel@tonic-gate 		return (-1);
9117c478bd9Sstevel@tonic-gate 
9127c478bd9Sstevel@tonic-gate 	for (auxv = P->auxv; auxv->a_type != AT_NULL; auxv++) {
9137c478bd9Sstevel@tonic-gate 		if (auxv->a_type == type)
9147c478bd9Sstevel@tonic-gate 			return (auxv->a_un.a_val);
9157c478bd9Sstevel@tonic-gate 	}
9167c478bd9Sstevel@tonic-gate 
9177c478bd9Sstevel@tonic-gate 	return (-1);
9187c478bd9Sstevel@tonic-gate }
9197c478bd9Sstevel@tonic-gate 
9207c478bd9Sstevel@tonic-gate /*
9217c478bd9Sstevel@tonic-gate  * Return a pointer to our internal copy of the process's aux vector.
9227c478bd9Sstevel@tonic-gate  * The caller should not hold on to this pointer across any libproc calls.
9237c478bd9Sstevel@tonic-gate  */
9247c478bd9Sstevel@tonic-gate const auxv_t *
9257c478bd9Sstevel@tonic-gate Pgetauxvec(struct ps_prochandle *P)
9267c478bd9Sstevel@tonic-gate {
9277c478bd9Sstevel@tonic-gate 	static const auxv_t empty = { AT_NULL, 0L };
9287c478bd9Sstevel@tonic-gate 
9297c478bd9Sstevel@tonic-gate 	if (P->auxv == NULL)
9307c478bd9Sstevel@tonic-gate 		Preadauxvec(P);
9317c478bd9Sstevel@tonic-gate 
9327c478bd9Sstevel@tonic-gate 	if (P->auxv == NULL)
9337c478bd9Sstevel@tonic-gate 		return (&empty);
9347c478bd9Sstevel@tonic-gate 
9357c478bd9Sstevel@tonic-gate 	return (P->auxv);
9367c478bd9Sstevel@tonic-gate }
9377c478bd9Sstevel@tonic-gate 
9387c478bd9Sstevel@tonic-gate /*
93912e72dbbSrh87107  * Return 1 if the given mapping corresponds to the given file_info_t's
94012e72dbbSrh87107  * load object; return 0 otherwise.
94112e72dbbSrh87107  */
94212e72dbbSrh87107 static int
94312e72dbbSrh87107 is_mapping_in_file(struct ps_prochandle *P, map_info_t *mptr, file_info_t *fptr)
94412e72dbbSrh87107 {
94512e72dbbSrh87107 	prmap_t *pmap = &mptr->map_pmap;
94612e72dbbSrh87107 	rd_loadobj_t *lop = fptr->file_lo;
94712e72dbbSrh87107 	uint_t i;
948d7755b5aSrh87107 	uintptr_t mstart, mend, sstart, send;
94912e72dbbSrh87107 
95012e72dbbSrh87107 	/*
95112e72dbbSrh87107 	 * We can get for free the start address of the text and data
95212e72dbbSrh87107 	 * sections of the load object. Start by seeing if the mapping
95312e72dbbSrh87107 	 * encloses either of these.
95412e72dbbSrh87107 	 */
95512e72dbbSrh87107 	if ((pmap->pr_vaddr <= lop->rl_base &&
95612e72dbbSrh87107 	    lop->rl_base < pmap->pr_vaddr + pmap->pr_size) ||
95712e72dbbSrh87107 	    (pmap->pr_vaddr <= lop->rl_data_base &&
95812e72dbbSrh87107 	    lop->rl_data_base < pmap->pr_vaddr + pmap->pr_size))
95912e72dbbSrh87107 		return (1);
96012e72dbbSrh87107 
96112e72dbbSrh87107 	/*
96212e72dbbSrh87107 	 * It's still possible that this mapping correponds to the load
96312e72dbbSrh87107 	 * object. Consider the example of a mapping whose start and end
96412e72dbbSrh87107 	 * addresses correspond to those of the load object's text section.
96512e72dbbSrh87107 	 * If the mapping splits, e.g. as a result of a segment demotion,
96612e72dbbSrh87107 	 * then although both mappings are still backed by the same section,
96712e72dbbSrh87107 	 * only one will be seen to enclose that section's start address.
96812e72dbbSrh87107 	 * Thus, to be rigorous, we ask not whether this mapping encloses
96912e72dbbSrh87107 	 * the start of a section, but whether there exists a section that
970d7755b5aSrh87107 	 * overlaps this mapping.
97112e72dbbSrh87107 	 *
97212e72dbbSrh87107 	 * If we don't already have the section addresses, and we successfully
97312e72dbbSrh87107 	 * get them, then we cache them in case we come here again.
97412e72dbbSrh87107 	 */
97512e72dbbSrh87107 	if (fptr->file_saddrs == NULL &&
97612e72dbbSrh87107 	    (fptr->file_saddrs = get_saddrs(P,
97712e72dbbSrh87107 	    fptr->file_map->map_pmap.pr_vaddr, &fptr->file_nsaddrs)) == NULL)
97812e72dbbSrh87107 		return (0);
979d7755b5aSrh87107 
980d7755b5aSrh87107 	mstart = mptr->map_pmap.pr_vaddr;
981d7755b5aSrh87107 	mend = mptr->map_pmap.pr_vaddr + mptr->map_pmap.pr_size;
98212e72dbbSrh87107 	for (i = 0; i < fptr->file_nsaddrs; i += 2) {
983d7755b5aSrh87107 		/* Does this section overlap the mapping? */
984d7755b5aSrh87107 		sstart = fptr->file_saddrs[i];
985d7755b5aSrh87107 		send = fptr->file_saddrs[i + 1];
986d7755b5aSrh87107 		if (!(mend <= sstart || mstart >= send))
98712e72dbbSrh87107 			return (1);
98812e72dbbSrh87107 	}
98912e72dbbSrh87107 
99012e72dbbSrh87107 	return (0);
99112e72dbbSrh87107 }
99212e72dbbSrh87107 
99312e72dbbSrh87107 /*
9947c478bd9Sstevel@tonic-gate  * Find or build the symbol table for the given mapping.
9957c478bd9Sstevel@tonic-gate  */
9967c478bd9Sstevel@tonic-gate static file_info_t *
9977c478bd9Sstevel@tonic-gate build_map_symtab(struct ps_prochandle *P, map_info_t *mptr)
9987c478bd9Sstevel@tonic-gate {
9997c478bd9Sstevel@tonic-gate 	prmap_t *pmap = &mptr->map_pmap;
10007c478bd9Sstevel@tonic-gate 	file_info_t *fptr;
10017c478bd9Sstevel@tonic-gate 	uint_t i;
10027c478bd9Sstevel@tonic-gate 
10037c478bd9Sstevel@tonic-gate 	if ((fptr = mptr->map_file) != NULL) {
10047c478bd9Sstevel@tonic-gate 		Pbuild_file_symtab(P, fptr);
10057c478bd9Sstevel@tonic-gate 		return (fptr);
10067c478bd9Sstevel@tonic-gate 	}
10077c478bd9Sstevel@tonic-gate 
10087c478bd9Sstevel@tonic-gate 	if (pmap->pr_mapname[0] == '\0')
10097c478bd9Sstevel@tonic-gate 		return (NULL);
10107c478bd9Sstevel@tonic-gate 
10117c478bd9Sstevel@tonic-gate 	/*
10127c478bd9Sstevel@tonic-gate 	 * Attempt to find a matching file.
10137c478bd9Sstevel@tonic-gate 	 * (A file can be mapped at several different addresses.)
10147c478bd9Sstevel@tonic-gate 	 */
10157c478bd9Sstevel@tonic-gate 	for (i = 0, fptr = list_next(&P->file_head); i < P->num_files;
10167c478bd9Sstevel@tonic-gate 	    i++, fptr = list_next(fptr)) {
10177c478bd9Sstevel@tonic-gate 		if (strcmp(fptr->file_pname, pmap->pr_mapname) == 0 &&
101812e72dbbSrh87107 		    fptr->file_lo && is_mapping_in_file(P, mptr, fptr)) {
10197c478bd9Sstevel@tonic-gate 			mptr->map_file = fptr;
10207c478bd9Sstevel@tonic-gate 			fptr->file_ref++;
10217c478bd9Sstevel@tonic-gate 			Pbuild_file_symtab(P, fptr);
10227c478bd9Sstevel@tonic-gate 			return (fptr);
10237c478bd9Sstevel@tonic-gate 		}
10247c478bd9Sstevel@tonic-gate 	}
10257c478bd9Sstevel@tonic-gate 
10267c478bd9Sstevel@tonic-gate 	/*
10277c478bd9Sstevel@tonic-gate 	 * If we need to create a new file_info structure, iterate
10287c478bd9Sstevel@tonic-gate 	 * through the load objects in order to attempt to connect
10297c478bd9Sstevel@tonic-gate 	 * this new file with its primary text mapping.  We again
10307c478bd9Sstevel@tonic-gate 	 * need to handle ld.so as a special case because we need
10317c478bd9Sstevel@tonic-gate 	 * to be able to bootstrap librtld_db.
10327c478bd9Sstevel@tonic-gate 	 */
10337c478bd9Sstevel@tonic-gate 	if ((fptr = file_info_new(P, mptr)) == NULL)
10347c478bd9Sstevel@tonic-gate 		return (NULL);
10357c478bd9Sstevel@tonic-gate 
10367c478bd9Sstevel@tonic-gate 	if (P->map_ldso != mptr) {
10377c478bd9Sstevel@tonic-gate 		if (P->rap != NULL)
10387c478bd9Sstevel@tonic-gate 			(void) rd_loadobj_iter(P->rap, map_iter, P);
10397c478bd9Sstevel@tonic-gate 		else
10407c478bd9Sstevel@tonic-gate 			(void) Prd_agent(P);
10417c478bd9Sstevel@tonic-gate 	} else {
10427c478bd9Sstevel@tonic-gate 		fptr->file_map = mptr;
10437c478bd9Sstevel@tonic-gate 	}
10447c478bd9Sstevel@tonic-gate 
10457c478bd9Sstevel@tonic-gate 	/*
10467c478bd9Sstevel@tonic-gate 	 * If librtld_db wasn't able to help us connect the file to a primary
10477c478bd9Sstevel@tonic-gate 	 * text mapping, set file_map to the current mapping because we require
10487c478bd9Sstevel@tonic-gate 	 * fptr->file_map to be set in Pbuild_file_symtab.  librtld_db may be
10497c478bd9Sstevel@tonic-gate 	 * unaware of what's going on in the rare case that a legitimate ELF
10507c478bd9Sstevel@tonic-gate 	 * file has been mmap(2)ed into the process address space *without*
10519acbbeafSnn35248 	 * the use of dlopen(3x).
10527c478bd9Sstevel@tonic-gate 	 */
10537c478bd9Sstevel@tonic-gate 	if (fptr->file_map == NULL)
10547c478bd9Sstevel@tonic-gate 		fptr->file_map = mptr;
10557c478bd9Sstevel@tonic-gate 
10567c478bd9Sstevel@tonic-gate 	Pbuild_file_symtab(P, fptr);
10577c478bd9Sstevel@tonic-gate 
10587c478bd9Sstevel@tonic-gate 	return (fptr);
10597c478bd9Sstevel@tonic-gate }
10607c478bd9Sstevel@tonic-gate 
10617c478bd9Sstevel@tonic-gate static int
106230da1432Sahl read_ehdr32(struct ps_prochandle *P, Elf32_Ehdr *ehdr, uint_t *phnum,
106330da1432Sahl     uintptr_t addr)
10647c478bd9Sstevel@tonic-gate {
10657c478bd9Sstevel@tonic-gate 	if (Pread(P, ehdr, sizeof (*ehdr), addr) != sizeof (*ehdr))
10667c478bd9Sstevel@tonic-gate 		return (-1);
10677c478bd9Sstevel@tonic-gate 
10687c478bd9Sstevel@tonic-gate 	if (ehdr->e_ident[EI_MAG0] != ELFMAG0 ||
10697c478bd9Sstevel@tonic-gate 	    ehdr->e_ident[EI_MAG1] != ELFMAG1 ||
10707c478bd9Sstevel@tonic-gate 	    ehdr->e_ident[EI_MAG2] != ELFMAG2 ||
10717c478bd9Sstevel@tonic-gate 	    ehdr->e_ident[EI_MAG3] != ELFMAG3 ||
10727c478bd9Sstevel@tonic-gate 	    ehdr->e_ident[EI_CLASS] != ELFCLASS32 ||
10737c478bd9Sstevel@tonic-gate #ifdef _BIG_ENDIAN
10747c478bd9Sstevel@tonic-gate 	    ehdr->e_ident[EI_DATA] != ELFDATA2MSB ||
10757c478bd9Sstevel@tonic-gate #else
10767c478bd9Sstevel@tonic-gate 	    ehdr->e_ident[EI_DATA] != ELFDATA2LSB ||
10777c478bd9Sstevel@tonic-gate #endif
10787c478bd9Sstevel@tonic-gate 	    ehdr->e_ident[EI_VERSION] != EV_CURRENT)
10797c478bd9Sstevel@tonic-gate 		return (-1);
10807c478bd9Sstevel@tonic-gate 
108130da1432Sahl 	if ((*phnum = ehdr->e_phnum) == PN_XNUM) {
108230da1432Sahl 		Elf32_Shdr shdr0;
108330da1432Sahl 
108430da1432Sahl 		if (ehdr->e_shoff == 0 || ehdr->e_shentsize < sizeof (shdr0) ||
108530da1432Sahl 		    Pread(P, &shdr0, sizeof (shdr0), addr + ehdr->e_shoff) !=
108630da1432Sahl 		    sizeof (shdr0))
108730da1432Sahl 			return (-1);
108830da1432Sahl 
108930da1432Sahl 		if (shdr0.sh_info != 0)
109030da1432Sahl 			*phnum = shdr0.sh_info;
109130da1432Sahl 	}
109230da1432Sahl 
10937c478bd9Sstevel@tonic-gate 	return (0);
10947c478bd9Sstevel@tonic-gate }
10957c478bd9Sstevel@tonic-gate 
10967c478bd9Sstevel@tonic-gate static int
10977c478bd9Sstevel@tonic-gate read_dynamic_phdr32(struct ps_prochandle *P, const Elf32_Ehdr *ehdr,
109830da1432Sahl     uint_t phnum, Elf32_Phdr *phdr, uintptr_t addr)
10997c478bd9Sstevel@tonic-gate {
11007c478bd9Sstevel@tonic-gate 	uint_t i;
11017c478bd9Sstevel@tonic-gate 
110230da1432Sahl 	for (i = 0; i < phnum; i++) {
11037c478bd9Sstevel@tonic-gate 		uintptr_t a = addr + ehdr->e_phoff + i * ehdr->e_phentsize;
11047c478bd9Sstevel@tonic-gate 		if (Pread(P, phdr, sizeof (*phdr), a) != sizeof (*phdr))
11057c478bd9Sstevel@tonic-gate 			return (-1);
11067c478bd9Sstevel@tonic-gate 
11077c478bd9Sstevel@tonic-gate 		if (phdr->p_type == PT_DYNAMIC)
11087c478bd9Sstevel@tonic-gate 			return (0);
11097c478bd9Sstevel@tonic-gate 	}
11107c478bd9Sstevel@tonic-gate 
11117c478bd9Sstevel@tonic-gate 	return (-1);
11127c478bd9Sstevel@tonic-gate }
11137c478bd9Sstevel@tonic-gate 
11147c478bd9Sstevel@tonic-gate #ifdef _LP64
11157c478bd9Sstevel@tonic-gate static int
111630da1432Sahl read_ehdr64(struct ps_prochandle *P, Elf64_Ehdr *ehdr, uint_t *phnum,
111730da1432Sahl     uintptr_t addr)
11187c478bd9Sstevel@tonic-gate {
11197c478bd9Sstevel@tonic-gate 	if (Pread(P, ehdr, sizeof (Elf64_Ehdr), addr) != sizeof (Elf64_Ehdr))
11207c478bd9Sstevel@tonic-gate 		return (-1);
11217c478bd9Sstevel@tonic-gate 
11227c478bd9Sstevel@tonic-gate 	if (ehdr->e_ident[EI_MAG0] != ELFMAG0 ||
11237c478bd9Sstevel@tonic-gate 	    ehdr->e_ident[EI_MAG1] != ELFMAG1 ||
11247c478bd9Sstevel@tonic-gate 	    ehdr->e_ident[EI_MAG2] != ELFMAG2 ||
11257c478bd9Sstevel@tonic-gate 	    ehdr->e_ident[EI_MAG3] != ELFMAG3 ||
11267c478bd9Sstevel@tonic-gate 	    ehdr->e_ident[EI_CLASS] != ELFCLASS64 ||
11277c478bd9Sstevel@tonic-gate #ifdef _BIG_ENDIAN
11287c478bd9Sstevel@tonic-gate 	    ehdr->e_ident[EI_DATA] != ELFDATA2MSB ||
11297c478bd9Sstevel@tonic-gate #else
11307c478bd9Sstevel@tonic-gate 	    ehdr->e_ident[EI_DATA] != ELFDATA2LSB ||
11317c478bd9Sstevel@tonic-gate #endif
11327c478bd9Sstevel@tonic-gate 	    ehdr->e_ident[EI_VERSION] != EV_CURRENT)
11337c478bd9Sstevel@tonic-gate 		return (-1);
11347c478bd9Sstevel@tonic-gate 
113530da1432Sahl 	if ((*phnum = ehdr->e_phnum) == PN_XNUM) {
113630da1432Sahl 		Elf64_Shdr shdr0;
113730da1432Sahl 
113830da1432Sahl 		if (ehdr->e_shoff == 0 || ehdr->e_shentsize < sizeof (shdr0) ||
113930da1432Sahl 		    Pread(P, &shdr0, sizeof (shdr0), addr + ehdr->e_shoff) !=
114030da1432Sahl 		    sizeof (shdr0))
114130da1432Sahl 			return (-1);
114230da1432Sahl 
114330da1432Sahl 		if (shdr0.sh_info != 0)
114430da1432Sahl 			*phnum = shdr0.sh_info;
114530da1432Sahl 	}
114630da1432Sahl 
11477c478bd9Sstevel@tonic-gate 	return (0);
11487c478bd9Sstevel@tonic-gate }
11497c478bd9Sstevel@tonic-gate 
11507c478bd9Sstevel@tonic-gate static int
11517c478bd9Sstevel@tonic-gate read_dynamic_phdr64(struct ps_prochandle *P, const Elf64_Ehdr *ehdr,
115230da1432Sahl     uint_t phnum, Elf64_Phdr *phdr, uintptr_t addr)
11537c478bd9Sstevel@tonic-gate {
11547c478bd9Sstevel@tonic-gate 	uint_t i;
11557c478bd9Sstevel@tonic-gate 
115630da1432Sahl 	for (i = 0; i < phnum; i++) {
11577c478bd9Sstevel@tonic-gate 		uintptr_t a = addr + ehdr->e_phoff + i * ehdr->e_phentsize;
11587c478bd9Sstevel@tonic-gate 		if (Pread(P, phdr, sizeof (*phdr), a) != sizeof (*phdr))
11597c478bd9Sstevel@tonic-gate 			return (-1);
11607c478bd9Sstevel@tonic-gate 
11617c478bd9Sstevel@tonic-gate 		if (phdr->p_type == PT_DYNAMIC)
11627c478bd9Sstevel@tonic-gate 			return (0);
11637c478bd9Sstevel@tonic-gate 	}
11647c478bd9Sstevel@tonic-gate 
11657c478bd9Sstevel@tonic-gate 	return (-1);
11667c478bd9Sstevel@tonic-gate }
11677c478bd9Sstevel@tonic-gate #endif	/* _LP64 */
11687c478bd9Sstevel@tonic-gate 
11697c478bd9Sstevel@tonic-gate /*
11707c478bd9Sstevel@tonic-gate  * The text segment for each load object contains the elf header and
11717c478bd9Sstevel@tonic-gate  * program headers. We can use this information to determine if the
11727c478bd9Sstevel@tonic-gate  * file that corresponds to the load object is the same file that
11737c478bd9Sstevel@tonic-gate  * was loaded into the process's address space. There can be a discrepency
11747c478bd9Sstevel@tonic-gate  * if a file is recompiled after the process is started or if the target
11757c478bd9Sstevel@tonic-gate  * represents a core file from a differently configured system -- two
11767c478bd9Sstevel@tonic-gate  * common examples. The DT_CHECKSUM entry in the dynamic section
11777c478bd9Sstevel@tonic-gate  * provides an easy method of comparison. It is important to note that
11787c478bd9Sstevel@tonic-gate  * the dynamic section usually lives in the data segment, but the meta
11797c478bd9Sstevel@tonic-gate  * data we use to find the dynamic section lives in the text segment so
11807c478bd9Sstevel@tonic-gate  * if either of those segments is absent we can't proceed.
11817c478bd9Sstevel@tonic-gate  *
11827c478bd9Sstevel@tonic-gate  * We're looking through the elf file for several items: the symbol tables
11837c478bd9Sstevel@tonic-gate  * (both dynsym and symtab), the procedure linkage table (PLT) base,
11847c478bd9Sstevel@tonic-gate  * size, and relocation base, and the CTF information. Most of this can
11857c478bd9Sstevel@tonic-gate  * be recovered from the loaded image of the file itself, the exceptions
11867c478bd9Sstevel@tonic-gate  * being the symtab and CTF data.
11877c478bd9Sstevel@tonic-gate  *
11887c478bd9Sstevel@tonic-gate  * First we try to open the file that we think corresponds to the load
11897c478bd9Sstevel@tonic-gate  * object, if the DT_CHECKSUM values match, we're all set, and can simply
11907c478bd9Sstevel@tonic-gate  * recover all the information we need from the file. If the values of
11917c478bd9Sstevel@tonic-gate  * DT_CHECKSUM don't match, or if we can't access the file for whatever
11927c478bd9Sstevel@tonic-gate  * reasaon, we fake up a elf file to use in its stead. If we can't read
11937c478bd9Sstevel@tonic-gate  * the elf data in the process's address space, we fall back to using
11947c478bd9Sstevel@tonic-gate  * the file even though it may give inaccurate information.
11957c478bd9Sstevel@tonic-gate  *
11967c478bd9Sstevel@tonic-gate  * The elf file that we fake up has to consist of sections for the
11977c478bd9Sstevel@tonic-gate  * dynsym, the PLT and the dynamic section. Note that in the case of a
11987c478bd9Sstevel@tonic-gate  * core file, we'll get the CTF data in the file_info_t later on from
11997c478bd9Sstevel@tonic-gate  * a section embedded the core file (if it's present).
12007c478bd9Sstevel@tonic-gate  *
12017c478bd9Sstevel@tonic-gate  * file_differs() conservatively looks for mismatched files, identifying
12027c478bd9Sstevel@tonic-gate  * a match when there is any ambiguity (since that's the legacy behavior).
12037c478bd9Sstevel@tonic-gate  */
12047c478bd9Sstevel@tonic-gate static int
12057c478bd9Sstevel@tonic-gate file_differs(struct ps_prochandle *P, Elf *elf, file_info_t *fptr)
12067c478bd9Sstevel@tonic-gate {
12077c478bd9Sstevel@tonic-gate 	Elf_Scn *scn;
12087c478bd9Sstevel@tonic-gate 	GElf_Shdr shdr;
12097c478bd9Sstevel@tonic-gate 	GElf_Dyn dyn;
12107c478bd9Sstevel@tonic-gate 	Elf_Data *data;
12117c478bd9Sstevel@tonic-gate 	uint_t i, ndyn;
12127c478bd9Sstevel@tonic-gate 	GElf_Xword cksum;
12137c478bd9Sstevel@tonic-gate 	uintptr_t addr;
12147c478bd9Sstevel@tonic-gate 
12157c478bd9Sstevel@tonic-gate 	if (fptr->file_map == NULL)
12167c478bd9Sstevel@tonic-gate 		return (0);
12177c478bd9Sstevel@tonic-gate 
12187c478bd9Sstevel@tonic-gate 	if ((Pcontent(P) & (CC_CONTENT_TEXT | CC_CONTENT_DATA)) !=
12197c478bd9Sstevel@tonic-gate 	    (CC_CONTENT_TEXT | CC_CONTENT_DATA))
12207c478bd9Sstevel@tonic-gate 		return (0);
12217c478bd9Sstevel@tonic-gate 
12227c478bd9Sstevel@tonic-gate 	/*
12237c478bd9Sstevel@tonic-gate 	 * First, we find the checksum value in the elf file.
12247c478bd9Sstevel@tonic-gate 	 */
12257c478bd9Sstevel@tonic-gate 	scn = NULL;
12267c478bd9Sstevel@tonic-gate 	while ((scn = elf_nextscn(elf, scn)) != NULL) {
12277c478bd9Sstevel@tonic-gate 		if (gelf_getshdr(scn, &shdr) != NULL &&
12287c478bd9Sstevel@tonic-gate 		    shdr.sh_type == SHT_DYNAMIC)
12297c478bd9Sstevel@tonic-gate 			goto found_shdr;
12307c478bd9Sstevel@tonic-gate 	}
12317c478bd9Sstevel@tonic-gate 	return (0);
12327c478bd9Sstevel@tonic-gate 
12337c478bd9Sstevel@tonic-gate found_shdr:
12347c478bd9Sstevel@tonic-gate 	if ((data = elf_getdata(scn, NULL)) == NULL)
12357c478bd9Sstevel@tonic-gate 		return (0);
12367c478bd9Sstevel@tonic-gate 
12377c478bd9Sstevel@tonic-gate 	if (P->status.pr_dmodel == PR_MODEL_ILP32)
12387c478bd9Sstevel@tonic-gate 		ndyn = shdr.sh_size / sizeof (Elf32_Dyn);
12397c478bd9Sstevel@tonic-gate #ifdef _LP64
12407c478bd9Sstevel@tonic-gate 	else if (P->status.pr_dmodel == PR_MODEL_LP64)
12417c478bd9Sstevel@tonic-gate 		ndyn = shdr.sh_size / sizeof (Elf64_Dyn);
12427c478bd9Sstevel@tonic-gate #endif
12437c478bd9Sstevel@tonic-gate 	else
12447c478bd9Sstevel@tonic-gate 		return (0);
12457c478bd9Sstevel@tonic-gate 
12467c478bd9Sstevel@tonic-gate 	for (i = 0; i < ndyn; i++) {
12477c478bd9Sstevel@tonic-gate 		if (gelf_getdyn(data, i, &dyn) != NULL &&
12487c478bd9Sstevel@tonic-gate 		    dyn.d_tag == DT_CHECKSUM)
12497c478bd9Sstevel@tonic-gate 			goto found_cksum;
12507c478bd9Sstevel@tonic-gate 	}
12519acbbeafSnn35248 
12529acbbeafSnn35248 	/*
12539acbbeafSnn35248 	 * The in-memory ELF has no DT_CHECKSUM section, but we will report it
12549acbbeafSnn35248 	 * as matching the file anyhow.
12559acbbeafSnn35248 	 */
12567c478bd9Sstevel@tonic-gate 	return (0);
12577c478bd9Sstevel@tonic-gate 
12587c478bd9Sstevel@tonic-gate found_cksum:
12597c478bd9Sstevel@tonic-gate 	cksum = dyn.d_un.d_val;
12607c478bd9Sstevel@tonic-gate 	dprintf("elf cksum value is %llx\n", (u_longlong_t)cksum);
12617c478bd9Sstevel@tonic-gate 
12627c478bd9Sstevel@tonic-gate 	/*
12637c478bd9Sstevel@tonic-gate 	 * Get the base of the text mapping that corresponds to this file.
12647c478bd9Sstevel@tonic-gate 	 */
12657c478bd9Sstevel@tonic-gate 	addr = fptr->file_map->map_pmap.pr_vaddr;
12667c478bd9Sstevel@tonic-gate 
12677c478bd9Sstevel@tonic-gate 	if (P->status.pr_dmodel == PR_MODEL_ILP32) {
12687c478bd9Sstevel@tonic-gate 		Elf32_Ehdr ehdr;
12697c478bd9Sstevel@tonic-gate 		Elf32_Phdr phdr;
12707c478bd9Sstevel@tonic-gate 		Elf32_Dyn dync, *dynp;
127130da1432Sahl 		uint_t phnum, i;
12727c478bd9Sstevel@tonic-gate 
127330da1432Sahl 		if (read_ehdr32(P, &ehdr, &phnum, addr) != 0 ||
127430da1432Sahl 		    read_dynamic_phdr32(P, &ehdr, phnum, &phdr, addr) != 0)
12757c478bd9Sstevel@tonic-gate 			return (0);
12767c478bd9Sstevel@tonic-gate 
12777c478bd9Sstevel@tonic-gate 		if (ehdr.e_type == ET_DYN)
12787c478bd9Sstevel@tonic-gate 			phdr.p_vaddr += addr;
12797c478bd9Sstevel@tonic-gate 		if ((dynp = malloc(phdr.p_filesz)) == NULL)
12807c478bd9Sstevel@tonic-gate 			return (0);
12817c478bd9Sstevel@tonic-gate 		dync.d_tag = DT_NULL;
12827c478bd9Sstevel@tonic-gate 		if (Pread(P, dynp, phdr.p_filesz, phdr.p_vaddr) !=
12837c478bd9Sstevel@tonic-gate 		    phdr.p_filesz) {
12847c478bd9Sstevel@tonic-gate 			free(dynp);
12857c478bd9Sstevel@tonic-gate 			return (0);
12867c478bd9Sstevel@tonic-gate 		}
12877c478bd9Sstevel@tonic-gate 
12887c478bd9Sstevel@tonic-gate 		for (i = 0; i < phdr.p_filesz / sizeof (Elf32_Dyn); i++) {
12897c478bd9Sstevel@tonic-gate 			if (dynp[i].d_tag == DT_CHECKSUM)
12907c478bd9Sstevel@tonic-gate 				dync = dynp[i];
12917c478bd9Sstevel@tonic-gate 		}
12927c478bd9Sstevel@tonic-gate 
12937c478bd9Sstevel@tonic-gate 		free(dynp);
12947c478bd9Sstevel@tonic-gate 
12957c478bd9Sstevel@tonic-gate 		if (dync.d_tag != DT_CHECKSUM)
12967c478bd9Sstevel@tonic-gate 			return (0);
12977c478bd9Sstevel@tonic-gate 
12987c478bd9Sstevel@tonic-gate 		dprintf("image cksum value is %llx\n",
12997c478bd9Sstevel@tonic-gate 		    (u_longlong_t)dync.d_un.d_val);
13007c478bd9Sstevel@tonic-gate 		return (dync.d_un.d_val != cksum);
13017c478bd9Sstevel@tonic-gate #ifdef _LP64
13027c478bd9Sstevel@tonic-gate 	} else if (P->status.pr_dmodel == PR_MODEL_LP64) {
13037c478bd9Sstevel@tonic-gate 		Elf64_Ehdr ehdr;
13047c478bd9Sstevel@tonic-gate 		Elf64_Phdr phdr;
13057c478bd9Sstevel@tonic-gate 		Elf64_Dyn dync, *dynp;
130630da1432Sahl 		uint_t phnum, i;
13077c478bd9Sstevel@tonic-gate 
130830da1432Sahl 		if (read_ehdr64(P, &ehdr, &phnum, addr) != 0 ||
130930da1432Sahl 		    read_dynamic_phdr64(P, &ehdr, phnum, &phdr, addr) != 0)
13107c478bd9Sstevel@tonic-gate 			return (0);
13117c478bd9Sstevel@tonic-gate 
13127c478bd9Sstevel@tonic-gate 		if (ehdr.e_type == ET_DYN)
13137c478bd9Sstevel@tonic-gate 			phdr.p_vaddr += addr;
13147c478bd9Sstevel@tonic-gate 		if ((dynp = malloc(phdr.p_filesz)) == NULL)
13157c478bd9Sstevel@tonic-gate 			return (0);
13167c478bd9Sstevel@tonic-gate 		dync.d_tag = DT_NULL;
13177c478bd9Sstevel@tonic-gate 		if (Pread(P, dynp, phdr.p_filesz, phdr.p_vaddr) !=
13187c478bd9Sstevel@tonic-gate 		    phdr.p_filesz) {
13197c478bd9Sstevel@tonic-gate 			free(dynp);
13207c478bd9Sstevel@tonic-gate 			return (0);
13217c478bd9Sstevel@tonic-gate 		}
13227c478bd9Sstevel@tonic-gate 
13237c478bd9Sstevel@tonic-gate 		for (i = 0; i < phdr.p_filesz / sizeof (Elf64_Dyn); i++) {
13247c478bd9Sstevel@tonic-gate 			if (dynp[i].d_tag == DT_CHECKSUM)
13257c478bd9Sstevel@tonic-gate 				dync = dynp[i];
13267c478bd9Sstevel@tonic-gate 		}
13277c478bd9Sstevel@tonic-gate 
13287c478bd9Sstevel@tonic-gate 		free(dynp);
13297c478bd9Sstevel@tonic-gate 
13307c478bd9Sstevel@tonic-gate 		if (dync.d_tag != DT_CHECKSUM)
13317c478bd9Sstevel@tonic-gate 			return (0);
13327c478bd9Sstevel@tonic-gate 
13337c478bd9Sstevel@tonic-gate 		dprintf("image cksum value is %llx\n",
13347c478bd9Sstevel@tonic-gate 		    (u_longlong_t)dync.d_un.d_val);
13357c478bd9Sstevel@tonic-gate 		return (dync.d_un.d_val != cksum);
13367c478bd9Sstevel@tonic-gate #endif	/* _LP64 */
13377c478bd9Sstevel@tonic-gate 	}
13387c478bd9Sstevel@tonic-gate 
13397c478bd9Sstevel@tonic-gate 	return (0);
13407c478bd9Sstevel@tonic-gate }
13417c478bd9Sstevel@tonic-gate 
1342d51e9074Sab196087 /*
1343d51e9074Sab196087  * Read data from the specified process and construct an in memory
1344d51e9074Sab196087  * image of an ELF file that represents it well enough to let
1345d51e9074Sab196087  * us probe it for information.
1346d51e9074Sab196087  */
13477c478bd9Sstevel@tonic-gate static Elf *
13487c478bd9Sstevel@tonic-gate fake_elf(struct ps_prochandle *P, file_info_t *fptr)
13497c478bd9Sstevel@tonic-gate {
13507c478bd9Sstevel@tonic-gate 	Elf *elf;
1351d51e9074Sab196087 	uintptr_t addr;
1352d51e9074Sab196087 	uint_t phnum;
13537c478bd9Sstevel@tonic-gate 
13547c478bd9Sstevel@tonic-gate 	if (fptr->file_map == NULL)
13557c478bd9Sstevel@tonic-gate 		return (NULL);
13567c478bd9Sstevel@tonic-gate 
13577c478bd9Sstevel@tonic-gate 	if ((Pcontent(P) & (CC_CONTENT_TEXT | CC_CONTENT_DATA)) !=
13587c478bd9Sstevel@tonic-gate 	    (CC_CONTENT_TEXT | CC_CONTENT_DATA))
13597c478bd9Sstevel@tonic-gate 		return (NULL);
13607c478bd9Sstevel@tonic-gate 
13617c478bd9Sstevel@tonic-gate 	addr = fptr->file_map->map_pmap.pr_vaddr;
13627c478bd9Sstevel@tonic-gate 
13637c478bd9Sstevel@tonic-gate 	if (P->status.pr_dmodel == PR_MODEL_ILP32) {
1364d51e9074Sab196087 		Elf32_Ehdr ehdr;
13657c478bd9Sstevel@tonic-gate 		Elf32_Phdr phdr;
13667c478bd9Sstevel@tonic-gate 
13679acbbeafSnn35248 		if ((read_ehdr32(P, &ehdr, &phnum, addr) != 0) ||
136830da1432Sahl 		    read_dynamic_phdr32(P, &ehdr, phnum, &phdr, addr) != 0)
13697c478bd9Sstevel@tonic-gate 			return (NULL);
13707c478bd9Sstevel@tonic-gate 
1371d51e9074Sab196087 		elf = fake_elf32(P, fptr, addr, &ehdr, phnum, &phdr);
13727c478bd9Sstevel@tonic-gate #ifdef _LP64
1373d51e9074Sab196087 	} else {
1374d51e9074Sab196087 		Elf64_Ehdr ehdr;
13757c478bd9Sstevel@tonic-gate 		Elf64_Phdr phdr;
13767c478bd9Sstevel@tonic-gate 
137730da1432Sahl 		if (read_ehdr64(P, &ehdr, &phnum, addr) != 0 ||
137830da1432Sahl 		    read_dynamic_phdr64(P, &ehdr, phnum, &phdr, addr) != 0)
13797c478bd9Sstevel@tonic-gate 			return (NULL);
13807c478bd9Sstevel@tonic-gate 
1381d51e9074Sab196087 		elf = fake_elf64(P, fptr, addr, &ehdr, phnum, &phdr);
1382d51e9074Sab196087 #endif
13837c478bd9Sstevel@tonic-gate 	}
13847c478bd9Sstevel@tonic-gate 
13857c478bd9Sstevel@tonic-gate 	return (elf);
13867c478bd9Sstevel@tonic-gate }
13877c478bd9Sstevel@tonic-gate 
13887c478bd9Sstevel@tonic-gate /*
13897c478bd9Sstevel@tonic-gate  * We wouldn't need these if qsort(3C) took an argument for the callback...
13907c478bd9Sstevel@tonic-gate  */
13917c478bd9Sstevel@tonic-gate static mutex_t sort_mtx = DEFAULTMUTEX;
13927c478bd9Sstevel@tonic-gate static char *sort_strs;
13937c478bd9Sstevel@tonic-gate static GElf_Sym *sort_syms;
13947c478bd9Sstevel@tonic-gate 
13957c478bd9Sstevel@tonic-gate int
13967c478bd9Sstevel@tonic-gate byaddr_cmp_common(GElf_Sym *a, char *aname, GElf_Sym *b, char *bname)
13977c478bd9Sstevel@tonic-gate {
13987c478bd9Sstevel@tonic-gate 	if (a->st_value < b->st_value)
13997c478bd9Sstevel@tonic-gate 		return (-1);
14007c478bd9Sstevel@tonic-gate 	if (a->st_value > b->st_value)
14017c478bd9Sstevel@tonic-gate 		return (1);
14027c478bd9Sstevel@tonic-gate 
14037c478bd9Sstevel@tonic-gate 	/*
14047c478bd9Sstevel@tonic-gate 	 * Prefer the function to the non-function.
14057c478bd9Sstevel@tonic-gate 	 */
14067c478bd9Sstevel@tonic-gate 	if (GELF_ST_TYPE(a->st_info) != GELF_ST_TYPE(b->st_info)) {
14077c478bd9Sstevel@tonic-gate 		if (GELF_ST_TYPE(a->st_info) == STT_FUNC)
14087c478bd9Sstevel@tonic-gate 			return (-1);
14097c478bd9Sstevel@tonic-gate 		if (GELF_ST_TYPE(b->st_info) == STT_FUNC)
14107c478bd9Sstevel@tonic-gate 			return (1);
14117c478bd9Sstevel@tonic-gate 	}
14127c478bd9Sstevel@tonic-gate 
14137c478bd9Sstevel@tonic-gate 	/*
14147c478bd9Sstevel@tonic-gate 	 * Prefer the weak or strong global symbol to the local symbol.
14157c478bd9Sstevel@tonic-gate 	 */
14167c478bd9Sstevel@tonic-gate 	if (GELF_ST_BIND(a->st_info) != GELF_ST_BIND(b->st_info)) {
14177c478bd9Sstevel@tonic-gate 		if (GELF_ST_BIND(b->st_info) == STB_LOCAL)
14187c478bd9Sstevel@tonic-gate 			return (-1);
14197c478bd9Sstevel@tonic-gate 		if (GELF_ST_BIND(a->st_info) == STB_LOCAL)
14207c478bd9Sstevel@tonic-gate 			return (1);
14217c478bd9Sstevel@tonic-gate 	}
14227c478bd9Sstevel@tonic-gate 
14237c478bd9Sstevel@tonic-gate 	/*
1424ab9a77c7Sahl 	 * Prefer the symbol that doesn't begin with a '$' since compilers and
1425ab9a77c7Sahl 	 * other symbol generators often use it as a prefix.
1426ab9a77c7Sahl 	 */
1427ab9a77c7Sahl 	if (*bname == '$')
1428ab9a77c7Sahl 		return (-1);
1429ab9a77c7Sahl 	if (*aname == '$')
1430ab9a77c7Sahl 		return (1);
1431ab9a77c7Sahl 
1432ab9a77c7Sahl 	/*
14337c478bd9Sstevel@tonic-gate 	 * Prefer the name with fewer leading underscores in the name.
14347c478bd9Sstevel@tonic-gate 	 */
14357c478bd9Sstevel@tonic-gate 	while (*aname == '_' && *bname == '_') {
14367c478bd9Sstevel@tonic-gate 		aname++;
14377c478bd9Sstevel@tonic-gate 		bname++;
14387c478bd9Sstevel@tonic-gate 	}
14397c478bd9Sstevel@tonic-gate 
14407c478bd9Sstevel@tonic-gate 	if (*bname == '_')
14417c478bd9Sstevel@tonic-gate 		return (-1);
14427c478bd9Sstevel@tonic-gate 	if (*aname == '_')
14437c478bd9Sstevel@tonic-gate 		return (1);
14447c478bd9Sstevel@tonic-gate 
14457c478bd9Sstevel@tonic-gate 	/*
14467c478bd9Sstevel@tonic-gate 	 * Prefer the symbol with the smaller size.
14477c478bd9Sstevel@tonic-gate 	 */
14487c478bd9Sstevel@tonic-gate 	if (a->st_size < b->st_size)
14497c478bd9Sstevel@tonic-gate 		return (-1);
14507c478bd9Sstevel@tonic-gate 	if (a->st_size > b->st_size)
14517c478bd9Sstevel@tonic-gate 		return (1);
14527c478bd9Sstevel@tonic-gate 
14537c478bd9Sstevel@tonic-gate 	/*
14547c478bd9Sstevel@tonic-gate 	 * All other factors being equal, fall back to lexicographic order.
14557c478bd9Sstevel@tonic-gate 	 */
14567c478bd9Sstevel@tonic-gate 	return (strcmp(aname, bname));
14577c478bd9Sstevel@tonic-gate }
14587c478bd9Sstevel@tonic-gate 
14597c478bd9Sstevel@tonic-gate static int
14607c478bd9Sstevel@tonic-gate byaddr_cmp(const void *aa, const void *bb)
14617c478bd9Sstevel@tonic-gate {
14627c478bd9Sstevel@tonic-gate 	GElf_Sym *a = &sort_syms[*(uint_t *)aa];
14637c478bd9Sstevel@tonic-gate 	GElf_Sym *b = &sort_syms[*(uint_t *)bb];
14647c478bd9Sstevel@tonic-gate 	char *aname = sort_strs + a->st_name;
14657c478bd9Sstevel@tonic-gate 	char *bname = sort_strs + b->st_name;
14667c478bd9Sstevel@tonic-gate 
14677c478bd9Sstevel@tonic-gate 	return (byaddr_cmp_common(a, aname, b, bname));
14687c478bd9Sstevel@tonic-gate }
14697c478bd9Sstevel@tonic-gate 
14707c478bd9Sstevel@tonic-gate static int
14717c478bd9Sstevel@tonic-gate byname_cmp(const void *aa, const void *bb)
14727c478bd9Sstevel@tonic-gate {
14737c478bd9Sstevel@tonic-gate 	GElf_Sym *a = &sort_syms[*(uint_t *)aa];
14747c478bd9Sstevel@tonic-gate 	GElf_Sym *b = &sort_syms[*(uint_t *)bb];
14757c478bd9Sstevel@tonic-gate 	char *aname = sort_strs + a->st_name;
14767c478bd9Sstevel@tonic-gate 	char *bname = sort_strs + b->st_name;
14777c478bd9Sstevel@tonic-gate 
14787c478bd9Sstevel@tonic-gate 	return (strcmp(aname, bname));
14797c478bd9Sstevel@tonic-gate }
14807c478bd9Sstevel@tonic-gate 
1481d51e9074Sab196087 /*
1482d51e9074Sab196087  * Given a symbol index, look up the corresponding symbol from the
1483d51e9074Sab196087  * given symbol table.
1484d51e9074Sab196087  *
1485d51e9074Sab196087  * This function allows the caller to treat the symbol table as a single
1486d51e9074Sab196087  * logical entity even though there may be 2 actual ELF symbol tables
1487d51e9074Sab196087  * involved. See the comments in Pcontrol.h for details.
1488d51e9074Sab196087  */
1489d51e9074Sab196087 static GElf_Sym *
1490d51e9074Sab196087 symtab_getsym(sym_tbl_t *symtab, int ndx, GElf_Sym *dst)
1491d51e9074Sab196087 {
1492d51e9074Sab196087 	/* If index is in range of primary symtab, look it up there */
1493d51e9074Sab196087 	if (ndx >= symtab->sym_symn_aux) {
1494d51e9074Sab196087 		return (gelf_getsym(symtab->sym_data_pri,
1495d51e9074Sab196087 		    ndx - symtab->sym_symn_aux, dst));
1496d51e9074Sab196087 	}
1497d51e9074Sab196087 
1498d51e9074Sab196087 	/* Not in primary: Look it up in the auxiliary symtab */
1499d51e9074Sab196087 	return (gelf_getsym(symtab->sym_data_aux, ndx, dst));
1500d51e9074Sab196087 }
1501d51e9074Sab196087 
15027c478bd9Sstevel@tonic-gate void
15037c478bd9Sstevel@tonic-gate optimize_symtab(sym_tbl_t *symtab)
15047c478bd9Sstevel@tonic-gate {
15057c478bd9Sstevel@tonic-gate 	GElf_Sym *symp, *syms;
15067c478bd9Sstevel@tonic-gate 	uint_t i, *indexa, *indexb;
15077c478bd9Sstevel@tonic-gate 	size_t symn, strsz, count;
15087c478bd9Sstevel@tonic-gate 
1509d51e9074Sab196087 	if (symtab == NULL || symtab->sym_data_pri == NULL ||
15107c478bd9Sstevel@tonic-gate 	    symtab->sym_byaddr != NULL)
15117c478bd9Sstevel@tonic-gate 		return;
15127c478bd9Sstevel@tonic-gate 
15137c478bd9Sstevel@tonic-gate 	symn = symtab->sym_symn;
15147c478bd9Sstevel@tonic-gate 	strsz = symtab->sym_strsz;
15157c478bd9Sstevel@tonic-gate 
15167c478bd9Sstevel@tonic-gate 	symp = syms = malloc(sizeof (GElf_Sym) * symn);
1517d51e9074Sab196087 	if (symp == NULL) {
1518d51e9074Sab196087 		dprintf("optimize_symtab: failed to malloc symbol array");
1519d51e9074Sab196087 		return;
1520d51e9074Sab196087 	}
15217c478bd9Sstevel@tonic-gate 
15227c478bd9Sstevel@tonic-gate 	/*
15237c478bd9Sstevel@tonic-gate 	 * First record all the symbols into a table and count up the ones
15247c478bd9Sstevel@tonic-gate 	 * that we're interested in. We mark symbols as invalid by setting
15257c478bd9Sstevel@tonic-gate 	 * the st_name to an illegal value.
15267c478bd9Sstevel@tonic-gate 	 */
15277c478bd9Sstevel@tonic-gate 	for (i = 0, count = 0; i < symn; i++, symp++) {
1528d51e9074Sab196087 		if (symtab_getsym(symtab, i, symp) != NULL &&
15297c478bd9Sstevel@tonic-gate 		    symp->st_name < strsz &&
15307c478bd9Sstevel@tonic-gate 		    IS_DATA_TYPE(GELF_ST_TYPE(symp->st_info)))
15317c478bd9Sstevel@tonic-gate 			count++;
15327c478bd9Sstevel@tonic-gate 		else
15337c478bd9Sstevel@tonic-gate 			symp->st_name = strsz;
15347c478bd9Sstevel@tonic-gate 	}
15357c478bd9Sstevel@tonic-gate 
15367c478bd9Sstevel@tonic-gate 	/*
15377c478bd9Sstevel@tonic-gate 	 * Allocate sufficient space for both tables and populate them
15387c478bd9Sstevel@tonic-gate 	 * with the same symbols we just counted.
15397c478bd9Sstevel@tonic-gate 	 */
15407c478bd9Sstevel@tonic-gate 	symtab->sym_count = count;
15417c478bd9Sstevel@tonic-gate 	indexa = symtab->sym_byaddr = calloc(sizeof (uint_t), count);
15427c478bd9Sstevel@tonic-gate 	indexb = symtab->sym_byname = calloc(sizeof (uint_t), count);
1543d51e9074Sab196087 	if (indexa == NULL || indexb == NULL) {
1544d51e9074Sab196087 		dprintf(
1545d51e9074Sab196087 		    "optimize_symtab: failed to malloc symbol index arrays");
1546d51e9074Sab196087 		symtab->sym_count = 0;
1547d51e9074Sab196087 		if (indexa != NULL) {	/* First alloc succeeded. Free it */
1548d51e9074Sab196087 			free(indexa);
1549d51e9074Sab196087 			symtab->sym_byaddr = NULL;
1550d51e9074Sab196087 		}
1551d51e9074Sab196087 		free(syms);
1552d51e9074Sab196087 		return;
1553d51e9074Sab196087 	}
15547c478bd9Sstevel@tonic-gate 	for (i = 0, symp = syms; i < symn; i++, symp++) {
15557c478bd9Sstevel@tonic-gate 		if (symp->st_name < strsz)
15567c478bd9Sstevel@tonic-gate 			*indexa++ = *indexb++ = i;
15577c478bd9Sstevel@tonic-gate 	}
15587c478bd9Sstevel@tonic-gate 
15597c478bd9Sstevel@tonic-gate 	/*
1560d7755b5aSrh87107 	 * Sort the two tables according to the appropriate criteria,
1561d7755b5aSrh87107 	 * unless the user has overridden this behaviour.
1562d7755b5aSrh87107 	 *
1563d7755b5aSrh87107 	 * An example where we might not sort the tables is the relatively
1564d7755b5aSrh87107 	 * unusual case of a process with very large symbol tables in which
1565d7755b5aSrh87107 	 * we perform few lookups. In such a case the total time would be
1566d7755b5aSrh87107 	 * dominated by the sort. It is difficult to determine a priori
1567d7755b5aSrh87107 	 * how many lookups an arbitrary client will perform, and
1568d7755b5aSrh87107 	 * hence whether the symbol tables should be sorted. We therefore
1569d7755b5aSrh87107 	 * sort the tables by default, but provide the user with a
1570d7755b5aSrh87107 	 * "chicken switch" in the form of the LIBPROC_NO_QSORT
1571d7755b5aSrh87107 	 * environment variable.
15727c478bd9Sstevel@tonic-gate 	 */
1573d7755b5aSrh87107 	if (!_libproc_no_qsort) {
15747c478bd9Sstevel@tonic-gate 		(void) mutex_lock(&sort_mtx);
15757c478bd9Sstevel@tonic-gate 		sort_strs = symtab->sym_strs;
15767c478bd9Sstevel@tonic-gate 		sort_syms = syms;
15777c478bd9Sstevel@tonic-gate 
15787c478bd9Sstevel@tonic-gate 		qsort(symtab->sym_byaddr, count, sizeof (uint_t), byaddr_cmp);
15797c478bd9Sstevel@tonic-gate 		qsort(symtab->sym_byname, count, sizeof (uint_t), byname_cmp);
15807c478bd9Sstevel@tonic-gate 
15817c478bd9Sstevel@tonic-gate 		sort_strs = NULL;
15827c478bd9Sstevel@tonic-gate 		sort_syms = NULL;
15837c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&sort_mtx);
1584d7755b5aSrh87107 	}
15857c478bd9Sstevel@tonic-gate 
15867c478bd9Sstevel@tonic-gate 	free(syms);
15877c478bd9Sstevel@tonic-gate }
15887c478bd9Sstevel@tonic-gate 
15897c478bd9Sstevel@tonic-gate /*
15907c478bd9Sstevel@tonic-gate  * Build the symbol table for the given mapped file.
15917c478bd9Sstevel@tonic-gate  */
15927c478bd9Sstevel@tonic-gate void
15937c478bd9Sstevel@tonic-gate Pbuild_file_symtab(struct ps_prochandle *P, file_info_t *fptr)
15947c478bd9Sstevel@tonic-gate {
15957c478bd9Sstevel@tonic-gate 	char objectfile[PATH_MAX];
15967c478bd9Sstevel@tonic-gate 	uint_t i;
15977c478bd9Sstevel@tonic-gate 
15987c478bd9Sstevel@tonic-gate 	GElf_Ehdr ehdr;
15997c478bd9Sstevel@tonic-gate 	GElf_Sym s;
16007c478bd9Sstevel@tonic-gate 
16017c478bd9Sstevel@tonic-gate 	Elf_Data *shdata;
16027c478bd9Sstevel@tonic-gate 	Elf_Scn *scn;
16037c478bd9Sstevel@tonic-gate 	Elf *elf;
160430da1432Sahl 	size_t nshdrs, shstrndx;
16057c478bd9Sstevel@tonic-gate 
16067c478bd9Sstevel@tonic-gate 	struct {
16077c478bd9Sstevel@tonic-gate 		GElf_Shdr c_shdr;
16087c478bd9Sstevel@tonic-gate 		Elf_Data *c_data;
16097c478bd9Sstevel@tonic-gate 		const char *c_name;
16107c478bd9Sstevel@tonic-gate 	} *cp, *cache = NULL, *dyn = NULL, *plt = NULL, *ctf = NULL;
16117c478bd9Sstevel@tonic-gate 
16127c478bd9Sstevel@tonic-gate 	if (fptr->file_init)
16137c478bd9Sstevel@tonic-gate 		return;	/* We've already processed this file */
16147c478bd9Sstevel@tonic-gate 
16157c478bd9Sstevel@tonic-gate 	/*
16167c478bd9Sstevel@tonic-gate 	 * Mark the file_info struct as having the symbol table initialized
16177c478bd9Sstevel@tonic-gate 	 * even if we fail below.  We tried once; we don't try again.
16187c478bd9Sstevel@tonic-gate 	 */
16197c478bd9Sstevel@tonic-gate 	fptr->file_init = 1;
16207c478bd9Sstevel@tonic-gate 
16217c478bd9Sstevel@tonic-gate 	if (elf_version(EV_CURRENT) == EV_NONE) {
16227c478bd9Sstevel@tonic-gate 		dprintf("libproc ELF version is more recent than libelf\n");
16237c478bd9Sstevel@tonic-gate 		return;
16247c478bd9Sstevel@tonic-gate 	}
16257c478bd9Sstevel@tonic-gate 
16267c478bd9Sstevel@tonic-gate 	if (P->state == PS_DEAD || P->state == PS_IDLE) {
1627186f7fbfSEdward Pilatowicz 		char *name;
16287c478bd9Sstevel@tonic-gate 		/*
16297c478bd9Sstevel@tonic-gate 		 * If we're a not live, we can't open files from the /proc
16307c478bd9Sstevel@tonic-gate 		 * object directory; we have only the mapping and file names
16317c478bd9Sstevel@tonic-gate 		 * to guide us.  We prefer the file_lname, but need to handle
16327c478bd9Sstevel@tonic-gate 		 * the case of it being NULL in order to bootstrap: we first
16337c478bd9Sstevel@tonic-gate 		 * come here during rd_new() when the only information we have
16347c478bd9Sstevel@tonic-gate 		 * is interpreter name associated with the AT_BASE mapping.
1635186f7fbfSEdward Pilatowicz 		 *
1636186f7fbfSEdward Pilatowicz 		 * Also, if the zone associated with the core file seems
1637186f7fbfSEdward Pilatowicz 		 * to exists on this machine we'll try to open the object
1638186f7fbfSEdward Pilatowicz 		 * file within the zone.
16397c478bd9Sstevel@tonic-gate 		 */
1640186f7fbfSEdward Pilatowicz 		if (fptr->file_rname != NULL)
1641186f7fbfSEdward Pilatowicz 			name = fptr->file_rname;
1642186f7fbfSEdward Pilatowicz 		else if (fptr->file_lname != NULL)
1643186f7fbfSEdward Pilatowicz 			name = fptr->file_lname;
1644186f7fbfSEdward Pilatowicz 		else
1645186f7fbfSEdward Pilatowicz 			name = fptr->file_pname;
1646186f7fbfSEdward Pilatowicz 		(void) strlcpy(objectfile, name, sizeof (objectfile));
16477c478bd9Sstevel@tonic-gate 	} else {
16487c478bd9Sstevel@tonic-gate 		(void) snprintf(objectfile, sizeof (objectfile),
16499acbbeafSnn35248 		    "%s/%d/object/%s",
16509acbbeafSnn35248 		    procfs_path, (int)P->pid, fptr->file_pname);
16517c478bd9Sstevel@tonic-gate 	}
16527c478bd9Sstevel@tonic-gate 
16537c478bd9Sstevel@tonic-gate 	/*
16547c478bd9Sstevel@tonic-gate 	 * Open the object file, create the elf file, and then get the elf
16557c478bd9Sstevel@tonic-gate 	 * header and .shstrtab data buffer so we can process sections by
16567c478bd9Sstevel@tonic-gate 	 * name. If anything goes wrong try to fake up an elf file from
16577c478bd9Sstevel@tonic-gate 	 * the in-core elf image.
16587c478bd9Sstevel@tonic-gate 	 */
16597c478bd9Sstevel@tonic-gate 	if ((fptr->file_fd = open(objectfile, O_RDONLY)) < 0) {
16607c478bd9Sstevel@tonic-gate 		dprintf("Pbuild_file_symtab: failed to open %s: %s\n",
16617c478bd9Sstevel@tonic-gate 		    objectfile, strerror(errno));
16627c478bd9Sstevel@tonic-gate 
16637c478bd9Sstevel@tonic-gate 		if ((elf = fake_elf(P, fptr)) == NULL ||
16647c478bd9Sstevel@tonic-gate 		    elf_kind(elf) != ELF_K_ELF ||
16657c478bd9Sstevel@tonic-gate 		    gelf_getehdr(elf, &ehdr) == NULL ||
166630da1432Sahl 		    elf_getshnum(elf, &nshdrs) == 0 ||
166730da1432Sahl 		    elf_getshstrndx(elf, &shstrndx) == 0 ||
166830da1432Sahl 		    (scn = elf_getscn(elf, shstrndx)) == NULL ||
16697c478bd9Sstevel@tonic-gate 		    (shdata = elf_getdata(scn, NULL)) == NULL) {
16707c478bd9Sstevel@tonic-gate 			dprintf("failed to fake up ELF file\n");
16717c478bd9Sstevel@tonic-gate 			return;
16727c478bd9Sstevel@tonic-gate 		}
16737c478bd9Sstevel@tonic-gate 
16747c478bd9Sstevel@tonic-gate 	} else if ((elf = elf_begin(fptr->file_fd, ELF_C_READ, NULL)) == NULL ||
16757c478bd9Sstevel@tonic-gate 	    elf_kind(elf) != ELF_K_ELF ||
16767c478bd9Sstevel@tonic-gate 	    gelf_getehdr(elf, &ehdr) == NULL ||
167730da1432Sahl 	    elf_getshnum(elf, &nshdrs) == 0 ||
167830da1432Sahl 	    elf_getshstrndx(elf, &shstrndx) == 0 ||
167930da1432Sahl 	    (scn = elf_getscn(elf, shstrndx)) == NULL ||
16807c478bd9Sstevel@tonic-gate 	    (shdata = elf_getdata(scn, NULL)) == NULL) {
16819acbbeafSnn35248 		int err = elf_errno();
16829acbbeafSnn35248 
16837c478bd9Sstevel@tonic-gate 		dprintf("failed to process ELF file %s: %s\n",
16849acbbeafSnn35248 		    objectfile, (err == 0) ? "<null>" : elf_errmsg(err));
16857c478bd9Sstevel@tonic-gate 
16867c478bd9Sstevel@tonic-gate 		if ((elf = fake_elf(P, fptr)) == NULL ||
16877c478bd9Sstevel@tonic-gate 		    elf_kind(elf) != ELF_K_ELF ||
16887c478bd9Sstevel@tonic-gate 		    gelf_getehdr(elf, &ehdr) == NULL ||
168930da1432Sahl 		    elf_getshnum(elf, &nshdrs) == 0 ||
169030da1432Sahl 		    elf_getshstrndx(elf, &shstrndx) == 0 ||
169130da1432Sahl 		    (scn = elf_getscn(elf, shstrndx)) == NULL ||
16927c478bd9Sstevel@tonic-gate 		    (shdata = elf_getdata(scn, NULL)) == NULL) {
16937c478bd9Sstevel@tonic-gate 			dprintf("failed to fake up ELF file\n");
16947c478bd9Sstevel@tonic-gate 			goto bad;
16957c478bd9Sstevel@tonic-gate 		}
16967c478bd9Sstevel@tonic-gate 
16977c478bd9Sstevel@tonic-gate 	} else if (file_differs(P, elf, fptr)) {
16987c478bd9Sstevel@tonic-gate 		Elf *newelf;
16997c478bd9Sstevel@tonic-gate 
17007c478bd9Sstevel@tonic-gate 		/*
17017c478bd9Sstevel@tonic-gate 		 * Before we get too excited about this elf file, we'll check
17027c478bd9Sstevel@tonic-gate 		 * its checksum value against the value we have in memory. If
17037c478bd9Sstevel@tonic-gate 		 * they don't agree, we try to fake up a new elf file and
17047c478bd9Sstevel@tonic-gate 		 * proceed with that instead.
17057c478bd9Sstevel@tonic-gate 		 */
17067c478bd9Sstevel@tonic-gate 
17077c478bd9Sstevel@tonic-gate 		dprintf("ELF file %s (%lx) doesn't match in-core image\n",
17087c478bd9Sstevel@tonic-gate 		    fptr->file_pname,
17097c478bd9Sstevel@tonic-gate 		    (ulong_t)fptr->file_map->map_pmap.pr_vaddr);
17107c478bd9Sstevel@tonic-gate 
17117c478bd9Sstevel@tonic-gate 		if ((newelf = fake_elf(P, fptr)) == NULL ||
17127c478bd9Sstevel@tonic-gate 		    elf_kind(newelf) != ELF_K_ELF ||
17137c478bd9Sstevel@tonic-gate 		    gelf_getehdr(newelf, &ehdr) == NULL ||
171430da1432Sahl 		    elf_getshnum(newelf, &nshdrs) == 0 ||
171530da1432Sahl 		    elf_getshstrndx(newelf, &shstrndx) == 0 ||
171630da1432Sahl 		    (scn = elf_getscn(newelf, shstrndx)) == NULL ||
17177c478bd9Sstevel@tonic-gate 		    (shdata = elf_getdata(scn, NULL)) == NULL) {
17187c478bd9Sstevel@tonic-gate 			dprintf("failed to fake up ELF file\n");
17197c478bd9Sstevel@tonic-gate 		} else {
17207c478bd9Sstevel@tonic-gate 			(void) elf_end(elf);
17217c478bd9Sstevel@tonic-gate 			elf = newelf;
17227c478bd9Sstevel@tonic-gate 
17237c478bd9Sstevel@tonic-gate 			dprintf("switched to faked up ELF file\n");
17247c478bd9Sstevel@tonic-gate 		}
17257c478bd9Sstevel@tonic-gate 	}
17267c478bd9Sstevel@tonic-gate 
172730da1432Sahl 	if ((cache = malloc(nshdrs * sizeof (*cache))) == NULL) {
17287c478bd9Sstevel@tonic-gate 		dprintf("failed to malloc section cache for %s\n", objectfile);
17297c478bd9Sstevel@tonic-gate 		goto bad;
17307c478bd9Sstevel@tonic-gate 	}
17317c478bd9Sstevel@tonic-gate 
17327c478bd9Sstevel@tonic-gate 	dprintf("processing ELF file %s\n", objectfile);
17337c478bd9Sstevel@tonic-gate 	fptr->file_class = ehdr.e_ident[EI_CLASS];
17347c478bd9Sstevel@tonic-gate 	fptr->file_etype = ehdr.e_type;
17357c478bd9Sstevel@tonic-gate 	fptr->file_elf = elf;
1736f957ecc1Svb160487 	fptr->file_shstrs = shdata->d_buf;
1737f957ecc1Svb160487 	fptr->file_shstrsz = shdata->d_size;
17387c478bd9Sstevel@tonic-gate 
17397c478bd9Sstevel@tonic-gate 	/*
17407c478bd9Sstevel@tonic-gate 	 * Iterate through each section, caching its section header, data
17417c478bd9Sstevel@tonic-gate 	 * pointer, and name.  We use this for handling sh_link values below.
17427c478bd9Sstevel@tonic-gate 	 */
17437c478bd9Sstevel@tonic-gate 	for (cp = cache + 1, scn = NULL; scn = elf_nextscn(elf, scn); cp++) {
17449acbbeafSnn35248 		if (gelf_getshdr(scn, &cp->c_shdr) == NULL) {
17459acbbeafSnn35248 			dprintf("Pbuild_file_symtab: Failed to get section "
17469acbbeafSnn35248 			    "header\n");
17477c478bd9Sstevel@tonic-gate 			goto bad; /* Failed to get section header */
17489acbbeafSnn35248 		}
17497c478bd9Sstevel@tonic-gate 
17509acbbeafSnn35248 		if ((cp->c_data = elf_getdata(scn, NULL)) == NULL) {
17519acbbeafSnn35248 			dprintf("Pbuild_file_symtab: Failed to get section "
17529acbbeafSnn35248 			    "data\n");
17537c478bd9Sstevel@tonic-gate 			goto bad; /* Failed to get section data */
17549acbbeafSnn35248 		}
17557c478bd9Sstevel@tonic-gate 
17569acbbeafSnn35248 		if (cp->c_shdr.sh_name >= shdata->d_size) {
17579acbbeafSnn35248 			dprintf("Pbuild_file_symtab: corrupt section name");
17587c478bd9Sstevel@tonic-gate 			goto bad; /* Corrupt section name */
17599acbbeafSnn35248 		}
17607c478bd9Sstevel@tonic-gate 
17617c478bd9Sstevel@tonic-gate 		cp->c_name = (const char *)shdata->d_buf + cp->c_shdr.sh_name;
17627c478bd9Sstevel@tonic-gate 	}
17637c478bd9Sstevel@tonic-gate 
17647c478bd9Sstevel@tonic-gate 	/*
17657c478bd9Sstevel@tonic-gate 	 * Now iterate through the section cache in order to locate info
1766d51e9074Sab196087 	 * for the .symtab, .dynsym, .SUNW_ldynsym, .dynamic, .plt,
1767d51e9074Sab196087 	 * and .SUNW_ctf sections:
17687c478bd9Sstevel@tonic-gate 	 */
176930da1432Sahl 	for (i = 1, cp = cache + 1; i < nshdrs; i++, cp++) {
17707c478bd9Sstevel@tonic-gate 		GElf_Shdr *shp = &cp->c_shdr;
17717c478bd9Sstevel@tonic-gate 
17727c478bd9Sstevel@tonic-gate 		if (shp->sh_type == SHT_SYMTAB || shp->sh_type == SHT_DYNSYM) {
17737c478bd9Sstevel@tonic-gate 			sym_tbl_t *symp = shp->sh_type == SHT_SYMTAB ?
17747c478bd9Sstevel@tonic-gate 			    &fptr->file_symtab : &fptr->file_dynsym;
17757c478bd9Sstevel@tonic-gate 			/*
17767c478bd9Sstevel@tonic-gate 			 * It's possible that the we already got the symbol
17777c478bd9Sstevel@tonic-gate 			 * table from the core file itself. Either the file
17787c478bd9Sstevel@tonic-gate 			 * differs in which case our faked up elf file will
17797c478bd9Sstevel@tonic-gate 			 * only contain the dynsym (not the symtab) or the
17807c478bd9Sstevel@tonic-gate 			 * file matches in which case we'll just be replacing
17817c478bd9Sstevel@tonic-gate 			 * the symbol table we pulled out of the core file
17827c478bd9Sstevel@tonic-gate 			 * with an equivalent one. In either case, this
17837c478bd9Sstevel@tonic-gate 			 * check isn't essential, but it's a good idea.
17847c478bd9Sstevel@tonic-gate 			 */
1785d51e9074Sab196087 			if (symp->sym_data_pri == NULL) {
17869acbbeafSnn35248 				dprintf("Symbol table found for %s\n",
17879acbbeafSnn35248 				    objectfile);
1788d51e9074Sab196087 				symp->sym_data_pri = cp->c_data;
1789d51e9074Sab196087 				symp->sym_symn +=
1790d51e9074Sab196087 				    shp->sh_size / shp->sh_entsize;
17917c478bd9Sstevel@tonic-gate 				symp->sym_strs =
17927c478bd9Sstevel@tonic-gate 				    cache[shp->sh_link].c_data->d_buf;
17937c478bd9Sstevel@tonic-gate 				symp->sym_strsz =
17947c478bd9Sstevel@tonic-gate 				    cache[shp->sh_link].c_data->d_size;
1795d51e9074Sab196087 				symp->sym_hdr_pri = cp->c_shdr;
17967c478bd9Sstevel@tonic-gate 				symp->sym_strhdr = cache[shp->sh_link].c_shdr;
17979acbbeafSnn35248 			} else {
17989acbbeafSnn35248 				dprintf("Symbol table already there for %s\n",
17999acbbeafSnn35248 				    objectfile);
18007c478bd9Sstevel@tonic-gate 			}
1801d51e9074Sab196087 		} else if (shp->sh_type == SHT_SUNW_LDYNSYM) {
1802d51e9074Sab196087 			/* .SUNW_ldynsym section is auxiliary to .dynsym */
1803d51e9074Sab196087 			if (fptr->file_dynsym.sym_data_aux == NULL) {
1804d51e9074Sab196087 				dprintf(".SUNW_ldynsym symbol table"
1805d51e9074Sab196087 				    " found for %s\n", objectfile);
1806d51e9074Sab196087 				fptr->file_dynsym.sym_data_aux = cp->c_data;
1807d51e9074Sab196087 				fptr->file_dynsym.sym_symn_aux =
1808d51e9074Sab196087 				    shp->sh_size / shp->sh_entsize;
1809d51e9074Sab196087 				fptr->file_dynsym.sym_symn +=
1810d51e9074Sab196087 				    fptr->file_dynsym.sym_symn_aux;
1811d51e9074Sab196087 				fptr->file_dynsym.sym_hdr_aux = cp->c_shdr;
1812d51e9074Sab196087 			} else {
1813d51e9074Sab196087 				dprintf(".SUNW_ldynsym symbol table already"
1814d51e9074Sab196087 				    " there for %s\n", objectfile);
1815d51e9074Sab196087 			}
18167c478bd9Sstevel@tonic-gate 		} else if (shp->sh_type == SHT_DYNAMIC) {
18177c478bd9Sstevel@tonic-gate 			dyn = cp;
18187c478bd9Sstevel@tonic-gate 		} else if (strcmp(cp->c_name, ".plt") == 0) {
18197c478bd9Sstevel@tonic-gate 			plt = cp;
18207c478bd9Sstevel@tonic-gate 		} else if (strcmp(cp->c_name, ".SUNW_ctf") == 0) {
18217c478bd9Sstevel@tonic-gate 			/*
18227c478bd9Sstevel@tonic-gate 			 * Skip over bogus CTF sections so they don't come back
18237c478bd9Sstevel@tonic-gate 			 * to haunt us later.
18247c478bd9Sstevel@tonic-gate 			 */
18257c478bd9Sstevel@tonic-gate 			if (shp->sh_link == 0 ||
182630da1432Sahl 			    shp->sh_link >= nshdrs ||
18277c478bd9Sstevel@tonic-gate 			    (cache[shp->sh_link].c_shdr.sh_type != SHT_DYNSYM &&
18287c478bd9Sstevel@tonic-gate 			    cache[shp->sh_link].c_shdr.sh_type != SHT_SYMTAB)) {
18297c478bd9Sstevel@tonic-gate 				dprintf("Bad sh_link %d for "
18307c478bd9Sstevel@tonic-gate 				    "CTF\n", shp->sh_link);
18317c478bd9Sstevel@tonic-gate 				continue;
18327c478bd9Sstevel@tonic-gate 			}
18337c478bd9Sstevel@tonic-gate 			ctf = cp;
18347c478bd9Sstevel@tonic-gate 		}
18357c478bd9Sstevel@tonic-gate 	}
18367c478bd9Sstevel@tonic-gate 
18377c478bd9Sstevel@tonic-gate 	/*
18387c478bd9Sstevel@tonic-gate 	 * At this point, we've found all the symbol tables we're ever going
18397c478bd9Sstevel@tonic-gate 	 * to find: the ones in the loop above and possibly the symtab that
18407c478bd9Sstevel@tonic-gate 	 * was included in the core file. Before we perform any lookups, we
18417c478bd9Sstevel@tonic-gate 	 * create sorted versions to optimize for lookups.
18427c478bd9Sstevel@tonic-gate 	 */
18437c478bd9Sstevel@tonic-gate 	optimize_symtab(&fptr->file_symtab);
18447c478bd9Sstevel@tonic-gate 	optimize_symtab(&fptr->file_dynsym);
18457c478bd9Sstevel@tonic-gate 
18467c478bd9Sstevel@tonic-gate 	/*
18477c478bd9Sstevel@tonic-gate 	 * Fill in the base address of the text mapping for shared libraries.
18487c478bd9Sstevel@tonic-gate 	 * This allows us to translate symbols before librtld_db is ready.
18497c478bd9Sstevel@tonic-gate 	 */
18507c478bd9Sstevel@tonic-gate 	if (fptr->file_etype == ET_DYN) {
18517c478bd9Sstevel@tonic-gate 		fptr->file_dyn_base = fptr->file_map->map_pmap.pr_vaddr -
18527c478bd9Sstevel@tonic-gate 		    fptr->file_map->map_pmap.pr_offset;
18539acbbeafSnn35248 		dprintf("setting file_dyn_base for %s to %lx\n",
18549acbbeafSnn35248 		    objectfile, (long)fptr->file_dyn_base);
18557c478bd9Sstevel@tonic-gate 	}
18567c478bd9Sstevel@tonic-gate 
18577c478bd9Sstevel@tonic-gate 	/*
18587c478bd9Sstevel@tonic-gate 	 * Record the CTF section information in the file info structure.
18597c478bd9Sstevel@tonic-gate 	 */
18607c478bd9Sstevel@tonic-gate 	if (ctf != NULL) {
18617c478bd9Sstevel@tonic-gate 		fptr->file_ctf_off = ctf->c_shdr.sh_offset;
18627c478bd9Sstevel@tonic-gate 		fptr->file_ctf_size = ctf->c_shdr.sh_size;
18637c478bd9Sstevel@tonic-gate 		if (ctf->c_shdr.sh_link != 0 &&
18647c478bd9Sstevel@tonic-gate 		    cache[ctf->c_shdr.sh_link].c_shdr.sh_type == SHT_DYNSYM)
18657c478bd9Sstevel@tonic-gate 			fptr->file_ctf_dyn = 1;
18667c478bd9Sstevel@tonic-gate 	}
18677c478bd9Sstevel@tonic-gate 
18687c478bd9Sstevel@tonic-gate 	if (fptr->file_lo == NULL)
18697c478bd9Sstevel@tonic-gate 		goto done; /* Nothing else to do if no load object info */
18707c478bd9Sstevel@tonic-gate 
18717c478bd9Sstevel@tonic-gate 	/*
18727c478bd9Sstevel@tonic-gate 	 * If the object is a shared library and we have a different rl_base
18737c478bd9Sstevel@tonic-gate 	 * value, reset file_dyn_base according to librtld_db's information.
18747c478bd9Sstevel@tonic-gate 	 */
18757c478bd9Sstevel@tonic-gate 	if (fptr->file_etype == ET_DYN &&
18767c478bd9Sstevel@tonic-gate 	    fptr->file_lo->rl_base != fptr->file_dyn_base) {
18779acbbeafSnn35248 		dprintf("resetting file_dyn_base for %s to %lx\n",
18789acbbeafSnn35248 		    objectfile, (long)fptr->file_lo->rl_base);
18797c478bd9Sstevel@tonic-gate 		fptr->file_dyn_base = fptr->file_lo->rl_base;
18807c478bd9Sstevel@tonic-gate 	}
18817c478bd9Sstevel@tonic-gate 
18827c478bd9Sstevel@tonic-gate 	/*
18837c478bd9Sstevel@tonic-gate 	 * Fill in the PLT information for this file if a PLT symbol is found.
18847c478bd9Sstevel@tonic-gate 	 */
18857c478bd9Sstevel@tonic-gate 	if (sym_by_name(&fptr->file_dynsym, "_PROCEDURE_LINKAGE_TABLE_", &s,
18867c478bd9Sstevel@tonic-gate 	    NULL) != NULL) {
18877c478bd9Sstevel@tonic-gate 		fptr->file_plt_base = s.st_value + fptr->file_dyn_base;
18887c478bd9Sstevel@tonic-gate 		fptr->file_plt_size = (plt != NULL) ? plt->c_shdr.sh_size : 0;
18897c478bd9Sstevel@tonic-gate 
18907c478bd9Sstevel@tonic-gate 		/*
18917c478bd9Sstevel@tonic-gate 		 * Bring the load object up to date; it is the only way the
18927c478bd9Sstevel@tonic-gate 		 * user has to access the PLT data. The PLT information in the
18937c478bd9Sstevel@tonic-gate 		 * rd_loadobj_t is not set in the call to map_iter() (the
18947c478bd9Sstevel@tonic-gate 		 * callback for rd_loadobj_iter) where we set file_lo.
18957c478bd9Sstevel@tonic-gate 		 */
18967c478bd9Sstevel@tonic-gate 		fptr->file_lo->rl_plt_base = fptr->file_plt_base;
18977c478bd9Sstevel@tonic-gate 		fptr->file_lo->rl_plt_size = fptr->file_plt_size;
18987c478bd9Sstevel@tonic-gate 
18997c478bd9Sstevel@tonic-gate 		dprintf("PLT found at %p, size = %lu\n",
19007c478bd9Sstevel@tonic-gate 		    (void *)fptr->file_plt_base, (ulong_t)fptr->file_plt_size);
19017c478bd9Sstevel@tonic-gate 	}
19027c478bd9Sstevel@tonic-gate 
19037c478bd9Sstevel@tonic-gate 	/*
19047c478bd9Sstevel@tonic-gate 	 * Fill in the PLT information.
19057c478bd9Sstevel@tonic-gate 	 */
19067c478bd9Sstevel@tonic-gate 	if (dyn != NULL) {
19077c478bd9Sstevel@tonic-gate 		uintptr_t dynaddr = dyn->c_shdr.sh_addr + fptr->file_dyn_base;
19087c478bd9Sstevel@tonic-gate 		size_t ndyn = dyn->c_shdr.sh_size / dyn->c_shdr.sh_entsize;
19097c478bd9Sstevel@tonic-gate 		GElf_Dyn d;
19107c478bd9Sstevel@tonic-gate 
19117c478bd9Sstevel@tonic-gate 		for (i = 0; i < ndyn; i++) {
19127c478bd9Sstevel@tonic-gate 			if (gelf_getdyn(dyn->c_data, i, &d) != NULL &&
19137c478bd9Sstevel@tonic-gate 			    d.d_tag == DT_JMPREL) {
19149acbbeafSnn35248 				dprintf("DT_JMPREL is %p\n",
19159acbbeafSnn35248 				    (void *)(uintptr_t)d.d_un.d_ptr);
19167c478bd9Sstevel@tonic-gate 				fptr->file_jmp_rel =
19177c478bd9Sstevel@tonic-gate 				    d.d_un.d_ptr + fptr->file_dyn_base;
19187c478bd9Sstevel@tonic-gate 				break;
19197c478bd9Sstevel@tonic-gate 			}
19207c478bd9Sstevel@tonic-gate 		}
19217c478bd9Sstevel@tonic-gate 
19227c478bd9Sstevel@tonic-gate 		dprintf("_DYNAMIC found at %p, %lu entries, DT_JMPREL = %p\n",
19237c478bd9Sstevel@tonic-gate 		    (void *)dynaddr, (ulong_t)ndyn, (void *)fptr->file_jmp_rel);
19247c478bd9Sstevel@tonic-gate 	}
19257c478bd9Sstevel@tonic-gate 
19267c478bd9Sstevel@tonic-gate done:
19277c478bd9Sstevel@tonic-gate 	free(cache);
19287c478bd9Sstevel@tonic-gate 	return;
19297c478bd9Sstevel@tonic-gate 
19307c478bd9Sstevel@tonic-gate bad:
19317c478bd9Sstevel@tonic-gate 	if (cache != NULL)
19327c478bd9Sstevel@tonic-gate 		free(cache);
19337c478bd9Sstevel@tonic-gate 
19347c478bd9Sstevel@tonic-gate 	(void) elf_end(elf);
19357c478bd9Sstevel@tonic-gate 	fptr->file_elf = NULL;
19367c478bd9Sstevel@tonic-gate 	if (fptr->file_elfmem != NULL) {
19377c478bd9Sstevel@tonic-gate 		free(fptr->file_elfmem);
19387c478bd9Sstevel@tonic-gate 		fptr->file_elfmem = NULL;
19397c478bd9Sstevel@tonic-gate 	}
19407c478bd9Sstevel@tonic-gate 	(void) close(fptr->file_fd);
19417c478bd9Sstevel@tonic-gate 	fptr->file_fd = -1;
19427c478bd9Sstevel@tonic-gate }
19437c478bd9Sstevel@tonic-gate 
19447c478bd9Sstevel@tonic-gate /*
19457c478bd9Sstevel@tonic-gate  * Given a process virtual address, return the map_info_t containing it.
19467c478bd9Sstevel@tonic-gate  * If none found, return NULL.
19477c478bd9Sstevel@tonic-gate  */
19487c478bd9Sstevel@tonic-gate map_info_t *
19497c478bd9Sstevel@tonic-gate Paddr2mptr(struct ps_prochandle *P, uintptr_t addr)
19507c478bd9Sstevel@tonic-gate {
19517c478bd9Sstevel@tonic-gate 	int lo = 0;
19527c478bd9Sstevel@tonic-gate 	int hi = P->map_count - 1;
19537c478bd9Sstevel@tonic-gate 	int mid;
19547c478bd9Sstevel@tonic-gate 	map_info_t *mp;
19557c478bd9Sstevel@tonic-gate 
19567c478bd9Sstevel@tonic-gate 	while (lo <= hi) {
19577c478bd9Sstevel@tonic-gate 
19587c478bd9Sstevel@tonic-gate 		mid = (lo + hi) / 2;
19597c478bd9Sstevel@tonic-gate 		mp = &P->mappings[mid];
19607c478bd9Sstevel@tonic-gate 
19617c478bd9Sstevel@tonic-gate 		/* check that addr is in [vaddr, vaddr + size) */
19627c478bd9Sstevel@tonic-gate 		if ((addr - mp->map_pmap.pr_vaddr) < mp->map_pmap.pr_size)
19637c478bd9Sstevel@tonic-gate 			return (mp);
19647c478bd9Sstevel@tonic-gate 
19657c478bd9Sstevel@tonic-gate 		if (addr < mp->map_pmap.pr_vaddr)
19667c478bd9Sstevel@tonic-gate 			hi = mid - 1;
19677c478bd9Sstevel@tonic-gate 		else
19687c478bd9Sstevel@tonic-gate 			lo = mid + 1;
19697c478bd9Sstevel@tonic-gate 	}
19707c478bd9Sstevel@tonic-gate 
19717c478bd9Sstevel@tonic-gate 	return (NULL);
19727c478bd9Sstevel@tonic-gate }
19737c478bd9Sstevel@tonic-gate 
19747c478bd9Sstevel@tonic-gate /*
19757c478bd9Sstevel@tonic-gate  * Return the map_info_t for the executable file.
19767c478bd9Sstevel@tonic-gate  * If not found, return NULL.
19777c478bd9Sstevel@tonic-gate  */
19787c478bd9Sstevel@tonic-gate static map_info_t *
19797c478bd9Sstevel@tonic-gate exec_map(struct ps_prochandle *P)
19807c478bd9Sstevel@tonic-gate {
19817c478bd9Sstevel@tonic-gate 	uint_t i;
19827c478bd9Sstevel@tonic-gate 	map_info_t *mptr;
19837c478bd9Sstevel@tonic-gate 	map_info_t *mold = NULL;
19847c478bd9Sstevel@tonic-gate 	file_info_t *fptr;
19857c478bd9Sstevel@tonic-gate 	uintptr_t base;
19867c478bd9Sstevel@tonic-gate 
19877c478bd9Sstevel@tonic-gate 	for (i = 0, mptr = P->mappings; i < P->map_count; i++, mptr++) {
19887c478bd9Sstevel@tonic-gate 		if (mptr->map_pmap.pr_mapname[0] == '\0')
19897c478bd9Sstevel@tonic-gate 			continue;
19907c478bd9Sstevel@tonic-gate 		if (strcmp(mptr->map_pmap.pr_mapname, "a.out") == 0) {
19917c478bd9Sstevel@tonic-gate 			if ((fptr = mptr->map_file) != NULL &&
19927c478bd9Sstevel@tonic-gate 			    fptr->file_lo != NULL) {
19937c478bd9Sstevel@tonic-gate 				base = fptr->file_lo->rl_base;
19947c478bd9Sstevel@tonic-gate 				if (base >= mptr->map_pmap.pr_vaddr &&
19957c478bd9Sstevel@tonic-gate 				    base < mptr->map_pmap.pr_vaddr +
19967c478bd9Sstevel@tonic-gate 				    mptr->map_pmap.pr_size)	/* text space */
19977c478bd9Sstevel@tonic-gate 					return (mptr);
19987c478bd9Sstevel@tonic-gate 				mold = mptr;	/* must be the data */
19997c478bd9Sstevel@tonic-gate 				continue;
20007c478bd9Sstevel@tonic-gate 			}
20017c478bd9Sstevel@tonic-gate 			/* This is a poor way to test for text space */
20027c478bd9Sstevel@tonic-gate 			if (!(mptr->map_pmap.pr_mflags & MA_EXEC) ||
20037c478bd9Sstevel@tonic-gate 			    (mptr->map_pmap.pr_mflags & MA_WRITE)) {
20047c478bd9Sstevel@tonic-gate 				mold = mptr;
20057c478bd9Sstevel@tonic-gate 				continue;
20067c478bd9Sstevel@tonic-gate 			}
20077c478bd9Sstevel@tonic-gate 			return (mptr);
20087c478bd9Sstevel@tonic-gate 		}
20097c478bd9Sstevel@tonic-gate 	}
20107c478bd9Sstevel@tonic-gate 
20117c478bd9Sstevel@tonic-gate 	return (mold);
20127c478bd9Sstevel@tonic-gate }
20137c478bd9Sstevel@tonic-gate 
20147c478bd9Sstevel@tonic-gate /*
20157c478bd9Sstevel@tonic-gate  * Given a shared object name, return the map_info_t for it.  If no matching
20167c478bd9Sstevel@tonic-gate  * object is found, return NULL.  Normally, the link maps contain the full
20177c478bd9Sstevel@tonic-gate  * object pathname, e.g. /usr/lib/libc.so.1.  We allow the object name to
20187c478bd9Sstevel@tonic-gate  * take one of the following forms:
20197c478bd9Sstevel@tonic-gate  *
20207c478bd9Sstevel@tonic-gate  * 1. An exact match (i.e. a full pathname): "/usr/lib/libc.so.1"
20217c478bd9Sstevel@tonic-gate  * 2. An exact basename match: "libc.so.1"
20227c478bd9Sstevel@tonic-gate  * 3. An initial basename match up to a '.' suffix: "libc.so" or "libc"
20237c478bd9Sstevel@tonic-gate  * 4. The literal string "a.out" is an alias for the executable mapping
20247c478bd9Sstevel@tonic-gate  *
20257c478bd9Sstevel@tonic-gate  * The third case is a convenience for callers and may not be necessary.
20267c478bd9Sstevel@tonic-gate  *
20277c478bd9Sstevel@tonic-gate  * As the exact same object name may be loaded on different link maps (see
20287c478bd9Sstevel@tonic-gate  * dlmopen(3DL)), we also allow the caller to resolve the object name by
20297c478bd9Sstevel@tonic-gate  * specifying a particular link map id.  If lmid is PR_LMID_EVERY, the
20307c478bd9Sstevel@tonic-gate  * first matching name will be returned, regardless of the link map id.
20317c478bd9Sstevel@tonic-gate  */
20327c478bd9Sstevel@tonic-gate static map_info_t *
20337c478bd9Sstevel@tonic-gate object_to_map(struct ps_prochandle *P, Lmid_t lmid, const char *objname)
20347c478bd9Sstevel@tonic-gate {
20357c478bd9Sstevel@tonic-gate 	map_info_t *mp;
20367c478bd9Sstevel@tonic-gate 	file_info_t *fp;
20377c478bd9Sstevel@tonic-gate 	size_t objlen;
20387c478bd9Sstevel@tonic-gate 	uint_t i;
20397c478bd9Sstevel@tonic-gate 
20407c478bd9Sstevel@tonic-gate 	/*
20419acbbeafSnn35248 	 * If we have no rtld_db, then always treat a request as one for all
20429acbbeafSnn35248 	 * link maps.
20439acbbeafSnn35248 	 */
20449acbbeafSnn35248 	if (P->rap == NULL)
20459acbbeafSnn35248 		lmid = PR_LMID_EVERY;
20469acbbeafSnn35248 
20479acbbeafSnn35248 	/*
20487c478bd9Sstevel@tonic-gate 	 * First pass: look for exact matches of the entire pathname or
20497c478bd9Sstevel@tonic-gate 	 * basename (cases 1 and 2 above):
20507c478bd9Sstevel@tonic-gate 	 */
20517c478bd9Sstevel@tonic-gate 	for (i = 0, mp = P->mappings; i < P->map_count; i++, mp++) {
20527c478bd9Sstevel@tonic-gate 
20537c478bd9Sstevel@tonic-gate 		if (mp->map_pmap.pr_mapname[0] == '\0' ||
2054186f7fbfSEdward Pilatowicz 		    (fp = mp->map_file) == NULL ||
2055186f7fbfSEdward Pilatowicz 		    ((fp->file_lname == NULL) && (fp->file_rname == NULL)))
20567c478bd9Sstevel@tonic-gate 			continue;
20577c478bd9Sstevel@tonic-gate 
20587c478bd9Sstevel@tonic-gate 		if (lmid != PR_LMID_EVERY &&
20597c478bd9Sstevel@tonic-gate 		    (fp->file_lo == NULL || lmid != fp->file_lo->rl_lmident))
20607c478bd9Sstevel@tonic-gate 			continue;
20617c478bd9Sstevel@tonic-gate 
20627c478bd9Sstevel@tonic-gate 		/*
20637c478bd9Sstevel@tonic-gate 		 * If we match, return the primary text mapping; otherwise
20647c478bd9Sstevel@tonic-gate 		 * just return the mapping we matched.
20657c478bd9Sstevel@tonic-gate 		 */
2066186f7fbfSEdward Pilatowicz 		if ((fp->file_lbase && strcmp(fp->file_lbase, objname) == 0) ||
2067186f7fbfSEdward Pilatowicz 		    (fp->file_rbase && strcmp(fp->file_rbase, objname) == 0) ||
2068186f7fbfSEdward Pilatowicz 		    (fp->file_lname && strcmp(fp->file_lname, objname) == 0) ||
2069186f7fbfSEdward Pilatowicz 		    (fp->file_rname && strcmp(fp->file_rname, objname) == 0))
20707c478bd9Sstevel@tonic-gate 			return (fp->file_map ? fp->file_map : mp);
20717c478bd9Sstevel@tonic-gate 	}
20727c478bd9Sstevel@tonic-gate 
20737c478bd9Sstevel@tonic-gate 	objlen = strlen(objname);
20747c478bd9Sstevel@tonic-gate 
20757c478bd9Sstevel@tonic-gate 	/*
20767c478bd9Sstevel@tonic-gate 	 * Second pass: look for partial matches (case 3 above):
20777c478bd9Sstevel@tonic-gate 	 */
20787c478bd9Sstevel@tonic-gate 	for (i = 0, mp = P->mappings; i < P->map_count; i++, mp++) {
20797c478bd9Sstevel@tonic-gate 
20807c478bd9Sstevel@tonic-gate 		if (mp->map_pmap.pr_mapname[0] == '\0' ||
2081186f7fbfSEdward Pilatowicz 		    (fp = mp->map_file) == NULL ||
2082186f7fbfSEdward Pilatowicz 		    ((fp->file_lname == NULL) && (fp->file_rname == NULL)))
20837c478bd9Sstevel@tonic-gate 			continue;
20847c478bd9Sstevel@tonic-gate 
20857c478bd9Sstevel@tonic-gate 		if (lmid != PR_LMID_EVERY &&
20867c478bd9Sstevel@tonic-gate 		    (fp->file_lo == NULL || lmid != fp->file_lo->rl_lmident))
20877c478bd9Sstevel@tonic-gate 			continue;
20887c478bd9Sstevel@tonic-gate 
20897c478bd9Sstevel@tonic-gate 		/*
20907c478bd9Sstevel@tonic-gate 		 * If we match, return the primary text mapping; otherwise
20917c478bd9Sstevel@tonic-gate 		 * just return the mapping we matched.
20927c478bd9Sstevel@tonic-gate 		 */
2093186f7fbfSEdward Pilatowicz 		if ((fp->file_lbase != NULL) &&
2094186f7fbfSEdward Pilatowicz 		    (strncmp(fp->file_lbase, objname, objlen) == 0) &&
2095186f7fbfSEdward Pilatowicz 		    (fp->file_lbase[objlen] == '.'))
2096186f7fbfSEdward Pilatowicz 			return (fp->file_map ? fp->file_map : mp);
2097186f7fbfSEdward Pilatowicz 		if ((fp->file_rbase != NULL) &&
2098186f7fbfSEdward Pilatowicz 		    (strncmp(fp->file_rbase, objname, objlen) == 0) &&
2099186f7fbfSEdward Pilatowicz 		    (fp->file_rbase[objlen] == '.'))
21007c478bd9Sstevel@tonic-gate 			return (fp->file_map ? fp->file_map : mp);
21017c478bd9Sstevel@tonic-gate 	}
21027c478bd9Sstevel@tonic-gate 
21037c478bd9Sstevel@tonic-gate 	/*
21047c478bd9Sstevel@tonic-gate 	 * One last check: we allow "a.out" to always alias the executable,
21057c478bd9Sstevel@tonic-gate 	 * assuming this name was not in use for something else.
21067c478bd9Sstevel@tonic-gate 	 */
21077c478bd9Sstevel@tonic-gate 	if ((lmid == PR_LMID_EVERY || lmid == LM_ID_BASE) &&
21087c478bd9Sstevel@tonic-gate 	    (strcmp(objname, "a.out") == 0))
21097c478bd9Sstevel@tonic-gate 		return (P->map_exec);
21107c478bd9Sstevel@tonic-gate 
21117c478bd9Sstevel@tonic-gate 	return (NULL);
21127c478bd9Sstevel@tonic-gate }
21137c478bd9Sstevel@tonic-gate 
21147c478bd9Sstevel@tonic-gate static map_info_t *
21157c478bd9Sstevel@tonic-gate object_name_to_map(struct ps_prochandle *P, Lmid_t lmid, const char *name)
21167c478bd9Sstevel@tonic-gate {
21177c478bd9Sstevel@tonic-gate 	map_info_t *mptr;
21187c478bd9Sstevel@tonic-gate 
21197c478bd9Sstevel@tonic-gate 	if (!P->info_valid)
21207c478bd9Sstevel@tonic-gate 		Pupdate_maps(P);
21217c478bd9Sstevel@tonic-gate 
21227c478bd9Sstevel@tonic-gate 	if (P->map_exec == NULL && ((mptr = Paddr2mptr(P,
21237c478bd9Sstevel@tonic-gate 	    Pgetauxval(P, AT_ENTRY))) != NULL || (mptr = exec_map(P)) != NULL))
21247c478bd9Sstevel@tonic-gate 		P->map_exec = mptr;
21257c478bd9Sstevel@tonic-gate 
21267c478bd9Sstevel@tonic-gate 	if (P->map_ldso == NULL && (mptr = Paddr2mptr(P,
21277c478bd9Sstevel@tonic-gate 	    Pgetauxval(P, AT_BASE))) != NULL)
21287c478bd9Sstevel@tonic-gate 		P->map_ldso = mptr;
21297c478bd9Sstevel@tonic-gate 
21307c478bd9Sstevel@tonic-gate 	if (name == PR_OBJ_EXEC)
21317c478bd9Sstevel@tonic-gate 		mptr = P->map_exec;
21327c478bd9Sstevel@tonic-gate 	else if (name == PR_OBJ_LDSO)
21337c478bd9Sstevel@tonic-gate 		mptr = P->map_ldso;
2134b45b146bSjhaslam 	else if (Prd_agent(P) != NULL || P->state == PS_IDLE)
21359acbbeafSnn35248 		mptr = object_to_map(P, lmid, name);
2136b45b146bSjhaslam 	else
2137b45b146bSjhaslam 		mptr = NULL;
21387c478bd9Sstevel@tonic-gate 
21397c478bd9Sstevel@tonic-gate 	return (mptr);
21407c478bd9Sstevel@tonic-gate }
21417c478bd9Sstevel@tonic-gate 
21427c478bd9Sstevel@tonic-gate /*
21437c478bd9Sstevel@tonic-gate  * When two symbols are found by address, decide which one is to be preferred.
21447c478bd9Sstevel@tonic-gate  */
21457c478bd9Sstevel@tonic-gate static GElf_Sym *
21467c478bd9Sstevel@tonic-gate sym_prefer(GElf_Sym *sym1, char *name1, GElf_Sym *sym2, char *name2)
21477c478bd9Sstevel@tonic-gate {
21487c478bd9Sstevel@tonic-gate 	/*
21497c478bd9Sstevel@tonic-gate 	 * Prefer the non-NULL symbol.
21507c478bd9Sstevel@tonic-gate 	 */
21517c478bd9Sstevel@tonic-gate 	if (sym1 == NULL)
21527c478bd9Sstevel@tonic-gate 		return (sym2);
21537c478bd9Sstevel@tonic-gate 	if (sym2 == NULL)
21547c478bd9Sstevel@tonic-gate 		return (sym1);
21557c478bd9Sstevel@tonic-gate 
21567c478bd9Sstevel@tonic-gate 	/*
21577c478bd9Sstevel@tonic-gate 	 * Defer to the sort ordering...
21587c478bd9Sstevel@tonic-gate 	 */
21597c478bd9Sstevel@tonic-gate 	return (byaddr_cmp_common(sym1, name1, sym2, name2) <= 0 ? sym1 : sym2);
21607c478bd9Sstevel@tonic-gate }
21617c478bd9Sstevel@tonic-gate 
21627c478bd9Sstevel@tonic-gate /*
2163d7755b5aSrh87107  * Use a binary search to do the work of sym_by_addr().
21647c478bd9Sstevel@tonic-gate  */
21657c478bd9Sstevel@tonic-gate static GElf_Sym *
2166d7755b5aSrh87107 sym_by_addr_binary(sym_tbl_t *symtab, GElf_Addr addr, GElf_Sym *symp,
2167d7755b5aSrh87107     uint_t *idp)
21687c478bd9Sstevel@tonic-gate {
21697c478bd9Sstevel@tonic-gate 	GElf_Sym sym, osym;
21707c478bd9Sstevel@tonic-gate 	uint_t i, oid, *byaddr = symtab->sym_byaddr;
21717c478bd9Sstevel@tonic-gate 	int min, max, mid, omid, found = 0;
21727c478bd9Sstevel@tonic-gate 
2173d51e9074Sab196087 	if (symtab->sym_data_pri == NULL || symtab->sym_count == 0)
21747c478bd9Sstevel@tonic-gate 		return (NULL);
21757c478bd9Sstevel@tonic-gate 
21767c478bd9Sstevel@tonic-gate 	min = 0;
21777c478bd9Sstevel@tonic-gate 	max = symtab->sym_count - 1;
21787c478bd9Sstevel@tonic-gate 	osym.st_value = 0;
21797c478bd9Sstevel@tonic-gate 
21807c478bd9Sstevel@tonic-gate 	/*
21817c478bd9Sstevel@tonic-gate 	 * We can't return when we've found a match, we have to continue
21827c478bd9Sstevel@tonic-gate 	 * searching for the closest matching symbol.
21837c478bd9Sstevel@tonic-gate 	 */
21847c478bd9Sstevel@tonic-gate 	while (min <= max) {
21857c478bd9Sstevel@tonic-gate 		mid = (max + min) / 2;
21867c478bd9Sstevel@tonic-gate 
21877c478bd9Sstevel@tonic-gate 		i = byaddr[mid];
2188d51e9074Sab196087 		(void) symtab_getsym(symtab, i, &sym);
21897c478bd9Sstevel@tonic-gate 
21907c478bd9Sstevel@tonic-gate 		if (addr >= sym.st_value &&
21917c478bd9Sstevel@tonic-gate 		    addr < sym.st_value + sym.st_size &&
21927c478bd9Sstevel@tonic-gate 		    (!found || sym.st_value > osym.st_value)) {
21937c478bd9Sstevel@tonic-gate 			osym = sym;
21947c478bd9Sstevel@tonic-gate 			omid = mid;
21957c478bd9Sstevel@tonic-gate 			oid = i;
21967c478bd9Sstevel@tonic-gate 			found = 1;
21977c478bd9Sstevel@tonic-gate 		}
21987c478bd9Sstevel@tonic-gate 
21997c478bd9Sstevel@tonic-gate 		if (addr < sym.st_value)
22007c478bd9Sstevel@tonic-gate 			max = mid - 1;
22017c478bd9Sstevel@tonic-gate 		else
22027c478bd9Sstevel@tonic-gate 			min = mid + 1;
22037c478bd9Sstevel@tonic-gate 	}
22047c478bd9Sstevel@tonic-gate 
22057c478bd9Sstevel@tonic-gate 	if (!found)
22067c478bd9Sstevel@tonic-gate 		return (NULL);
22077c478bd9Sstevel@tonic-gate 
22087c478bd9Sstevel@tonic-gate 	/*
22097c478bd9Sstevel@tonic-gate 	 * There may be many symbols with identical values so we walk
22107c478bd9Sstevel@tonic-gate 	 * backward in the byaddr table to find the best match.
22117c478bd9Sstevel@tonic-gate 	 */
22127c478bd9Sstevel@tonic-gate 	do {
22137c478bd9Sstevel@tonic-gate 		sym = osym;
22147c478bd9Sstevel@tonic-gate 		i = oid;
22157c478bd9Sstevel@tonic-gate 
22167c478bd9Sstevel@tonic-gate 		if (omid == 0)
22177c478bd9Sstevel@tonic-gate 			break;
22187c478bd9Sstevel@tonic-gate 
22197c478bd9Sstevel@tonic-gate 		oid = byaddr[--omid];
2220d51e9074Sab196087 		(void) symtab_getsym(symtab, oid, &osym);
22217c478bd9Sstevel@tonic-gate 	} while (addr >= osym.st_value &&
22227c478bd9Sstevel@tonic-gate 	    addr < sym.st_value + osym.st_size &&
22237c478bd9Sstevel@tonic-gate 	    osym.st_value == sym.st_value);
22247c478bd9Sstevel@tonic-gate 
22257c478bd9Sstevel@tonic-gate 	*symp = sym;
22267c478bd9Sstevel@tonic-gate 	if (idp != NULL)
22277c478bd9Sstevel@tonic-gate 		*idp = i;
22287c478bd9Sstevel@tonic-gate 	return (symp);
22297c478bd9Sstevel@tonic-gate }
22307c478bd9Sstevel@tonic-gate 
22317c478bd9Sstevel@tonic-gate /*
2232d7755b5aSrh87107  * Use a linear search to do the work of sym_by_addr().
22337c478bd9Sstevel@tonic-gate  */
22347c478bd9Sstevel@tonic-gate static GElf_Sym *
2235d7755b5aSrh87107 sym_by_addr_linear(sym_tbl_t *symtab, GElf_Addr addr, GElf_Sym *symbolp,
2236d7755b5aSrh87107     uint_t *idp)
2237d7755b5aSrh87107 {
2238d7755b5aSrh87107 	size_t symn = symtab->sym_symn;
2239d7755b5aSrh87107 	char *strs = symtab->sym_strs;
2240d7755b5aSrh87107 	GElf_Sym sym, *symp = NULL;
2241d7755b5aSrh87107 	GElf_Sym osym, *osymp = NULL;
2242d7755b5aSrh87107 	int i, id;
2243d7755b5aSrh87107 
2244d7755b5aSrh87107 	if (symtab->sym_data_pri == NULL || symn == 0 || strs == NULL)
2245d7755b5aSrh87107 		return (NULL);
2246d7755b5aSrh87107 
2247d7755b5aSrh87107 	for (i = 0; i < symn; i++) {
2248d7755b5aSrh87107 		if ((symp = symtab_getsym(symtab, i, &sym)) != NULL) {
2249d7755b5aSrh87107 			if (addr >= sym.st_value &&
2250d7755b5aSrh87107 			    addr < sym.st_value + sym.st_size) {
2251d7755b5aSrh87107 				if (osymp)
2252d7755b5aSrh87107 					symp = sym_prefer(
2253d7755b5aSrh87107 					    symp, strs + symp->st_name,
2254d7755b5aSrh87107 					    osymp, strs + osymp->st_name);
2255d7755b5aSrh87107 				if (symp != osymp) {
2256d7755b5aSrh87107 					osym = sym;
2257d7755b5aSrh87107 					osymp = &osym;
2258d7755b5aSrh87107 					id = i;
2259d7755b5aSrh87107 				}
2260d7755b5aSrh87107 			}
2261d7755b5aSrh87107 		}
2262d7755b5aSrh87107 	}
2263d7755b5aSrh87107 	if (osymp) {
2264d7755b5aSrh87107 		*symbolp = osym;
2265d7755b5aSrh87107 		if (idp)
2266d7755b5aSrh87107 			*idp = id;
2267d7755b5aSrh87107 		return (symbolp);
2268d7755b5aSrh87107 	}
2269d7755b5aSrh87107 	return (NULL);
2270d7755b5aSrh87107 }
2271d7755b5aSrh87107 
2272d7755b5aSrh87107 /*
2273d7755b5aSrh87107  * Look up a symbol by address in the specified symbol table.
2274d7755b5aSrh87107  * Adjustment to 'addr' must already have been made for the
2275d7755b5aSrh87107  * offset of the symbol if this is a dynamic library symbol table.
2276d7755b5aSrh87107  *
2277d7755b5aSrh87107  * Use a linear or a binary search depending on whether or not we
2278d7755b5aSrh87107  * chose to sort the table in optimize_symtab().
2279d7755b5aSrh87107  */
2280d7755b5aSrh87107 static GElf_Sym *
2281d7755b5aSrh87107 sym_by_addr(sym_tbl_t *symtab, GElf_Addr addr, GElf_Sym *symp, uint_t *idp)
2282d7755b5aSrh87107 {
2283d7755b5aSrh87107 	if (_libproc_no_qsort) {
2284d7755b5aSrh87107 		return (sym_by_addr_linear(symtab, addr, symp, idp));
2285d7755b5aSrh87107 	} else {
2286d7755b5aSrh87107 		return (sym_by_addr_binary(symtab, addr, symp, idp));
2287d7755b5aSrh87107 	}
2288d7755b5aSrh87107 }
2289d7755b5aSrh87107 
2290d7755b5aSrh87107 /*
2291d7755b5aSrh87107  * Use a binary search to do the work of sym_by_name().
2292d7755b5aSrh87107  */
2293d7755b5aSrh87107 static GElf_Sym *
2294d7755b5aSrh87107 sym_by_name_binary(sym_tbl_t *symtab, const char *name, GElf_Sym *symp,
2295d7755b5aSrh87107     uint_t *idp)
22967c478bd9Sstevel@tonic-gate {
22977c478bd9Sstevel@tonic-gate 	char *strs = symtab->sym_strs;
22987c478bd9Sstevel@tonic-gate 	uint_t i, *byname = symtab->sym_byname;
22997c478bd9Sstevel@tonic-gate 	int min, mid, max, cmp;
23007c478bd9Sstevel@tonic-gate 
2301d51e9074Sab196087 	if (symtab->sym_data_pri == NULL || strs == NULL ||
2302d51e9074Sab196087 	    symtab->sym_count == 0)
23037c478bd9Sstevel@tonic-gate 		return (NULL);
23047c478bd9Sstevel@tonic-gate 
23057c478bd9Sstevel@tonic-gate 	min = 0;
23067c478bd9Sstevel@tonic-gate 	max = symtab->sym_count - 1;
23077c478bd9Sstevel@tonic-gate 
23087c478bd9Sstevel@tonic-gate 	while (min <= max) {
23097c478bd9Sstevel@tonic-gate 		mid = (max + min) / 2;
23107c478bd9Sstevel@tonic-gate 
23117c478bd9Sstevel@tonic-gate 		i = byname[mid];
2312d51e9074Sab196087 		(void) symtab_getsym(symtab, i, symp);
23137c478bd9Sstevel@tonic-gate 
23147c478bd9Sstevel@tonic-gate 		if ((cmp = strcmp(name, strs + symp->st_name)) == 0) {
23157c478bd9Sstevel@tonic-gate 			if (idp != NULL)
23167c478bd9Sstevel@tonic-gate 				*idp = i;
23177c478bd9Sstevel@tonic-gate 			return (symp);
23187c478bd9Sstevel@tonic-gate 		}
23197c478bd9Sstevel@tonic-gate 
23207c478bd9Sstevel@tonic-gate 		if (cmp < 0)
23217c478bd9Sstevel@tonic-gate 			max = mid - 1;
23227c478bd9Sstevel@tonic-gate 		else
23237c478bd9Sstevel@tonic-gate 			min = mid + 1;
23247c478bd9Sstevel@tonic-gate 	}
23257c478bd9Sstevel@tonic-gate 
23267c478bd9Sstevel@tonic-gate 	return (NULL);
23277c478bd9Sstevel@tonic-gate }
23287c478bd9Sstevel@tonic-gate 
23297c478bd9Sstevel@tonic-gate /*
2330d7755b5aSrh87107  * Use a linear search to do the work of sym_by_name().
2331d7755b5aSrh87107  */
2332d7755b5aSrh87107 static GElf_Sym *
2333d7755b5aSrh87107 sym_by_name_linear(sym_tbl_t *symtab, const char *name, GElf_Sym *symp,
2334d7755b5aSrh87107     uint_t *idp)
2335d7755b5aSrh87107 {
2336d7755b5aSrh87107 	size_t symn = symtab->sym_symn;
2337d7755b5aSrh87107 	char *strs = symtab->sym_strs;
2338d7755b5aSrh87107 	int i;
2339d7755b5aSrh87107 
2340d7755b5aSrh87107 	if (symtab->sym_data_pri == NULL || symn == 0 || strs == NULL)
2341d7755b5aSrh87107 		return (NULL);
2342d7755b5aSrh87107 
2343d7755b5aSrh87107 	for (i = 0; i < symn; i++) {
2344d7755b5aSrh87107 		if (symtab_getsym(symtab, i, symp) &&
2345d7755b5aSrh87107 		    strcmp(name, strs + symp->st_name) == 0) {
2346d7755b5aSrh87107 			if (idp)
2347d7755b5aSrh87107 				*idp = i;
2348d7755b5aSrh87107 			return (symp);
2349d7755b5aSrh87107 		}
2350d7755b5aSrh87107 	}
2351d7755b5aSrh87107 
2352d7755b5aSrh87107 	return (NULL);
2353d7755b5aSrh87107 }
2354d7755b5aSrh87107 
2355d7755b5aSrh87107 /*
2356d7755b5aSrh87107  * Look up a symbol by name in the specified symbol table.
2357d7755b5aSrh87107  *
2358d7755b5aSrh87107  * Use a linear or a binary search depending on whether or not we
2359d7755b5aSrh87107  * chose to sort the table in optimize_symtab().
2360d7755b5aSrh87107  */
2361d7755b5aSrh87107 static GElf_Sym *
2362d7755b5aSrh87107 sym_by_name(sym_tbl_t *symtab, const char *name, GElf_Sym *symp, uint_t *idp)
2363d7755b5aSrh87107 {
2364d7755b5aSrh87107 	if (_libproc_no_qsort) {
2365d7755b5aSrh87107 		return (sym_by_name_linear(symtab, name, symp, idp));
2366d7755b5aSrh87107 	} else {
2367d7755b5aSrh87107 		return (sym_by_name_binary(symtab, name, symp, idp));
2368d7755b5aSrh87107 	}
2369d7755b5aSrh87107 }
2370d7755b5aSrh87107 
2371d7755b5aSrh87107 /*
23727c478bd9Sstevel@tonic-gate  * Search the process symbol tables looking for a symbol whose
23737c478bd9Sstevel@tonic-gate  * value to value+size contain the address specified by addr.
23747c478bd9Sstevel@tonic-gate  * Return values are:
23757c478bd9Sstevel@tonic-gate  *	sym_name_buffer containing the symbol name
23767c478bd9Sstevel@tonic-gate  *	GElf_Sym symbol table entry
23777c478bd9Sstevel@tonic-gate  *	prsyminfo_t ancillary symbol information
23787c478bd9Sstevel@tonic-gate  * Returns 0 on success, -1 on failure.
23797c478bd9Sstevel@tonic-gate  */
2380186f7fbfSEdward Pilatowicz static int
2381186f7fbfSEdward Pilatowicz i_Pxlookup_by_addr(
23827c478bd9Sstevel@tonic-gate 	struct ps_prochandle *P,
2383186f7fbfSEdward Pilatowicz 	int lmresolve,			/* use resolve linker object names */
23847c478bd9Sstevel@tonic-gate 	uintptr_t addr,			/* process address being sought */
23857c478bd9Sstevel@tonic-gate 	char *sym_name_buffer,		/* buffer for the symbol name */
23867c478bd9Sstevel@tonic-gate 	size_t bufsize,			/* size of sym_name_buffer */
23877c478bd9Sstevel@tonic-gate 	GElf_Sym *symbolp,		/* returned symbol table entry */
23887c478bd9Sstevel@tonic-gate 	prsyminfo_t *sip)		/* returned symbol info */
23897c478bd9Sstevel@tonic-gate {
23907c478bd9Sstevel@tonic-gate 	GElf_Sym	*symp;
23917c478bd9Sstevel@tonic-gate 	char		*name;
23927c478bd9Sstevel@tonic-gate 	GElf_Sym	sym1, *sym1p = NULL;
23937c478bd9Sstevel@tonic-gate 	GElf_Sym	sym2, *sym2p = NULL;
23947c478bd9Sstevel@tonic-gate 	char		*name1 = NULL;
23957c478bd9Sstevel@tonic-gate 	char		*name2 = NULL;
23967c478bd9Sstevel@tonic-gate 	uint_t		i1;
23977c478bd9Sstevel@tonic-gate 	uint_t		i2;
23987c478bd9Sstevel@tonic-gate 	map_info_t	*mptr;
23997c478bd9Sstevel@tonic-gate 	file_info_t	*fptr;
24007c478bd9Sstevel@tonic-gate 
24017c478bd9Sstevel@tonic-gate 	(void) Prd_agent(P);
24027c478bd9Sstevel@tonic-gate 
24037c478bd9Sstevel@tonic-gate 	if ((mptr = Paddr2mptr(P, addr)) == NULL ||	/* no such address */
24047c478bd9Sstevel@tonic-gate 	    (fptr = build_map_symtab(P, mptr)) == NULL || /* no mapped file */
24057c478bd9Sstevel@tonic-gate 	    fptr->file_elf == NULL)			/* not an ELF file */
24067c478bd9Sstevel@tonic-gate 		return (-1);
24077c478bd9Sstevel@tonic-gate 
24087c478bd9Sstevel@tonic-gate 	/*
24097c478bd9Sstevel@tonic-gate 	 * Adjust the address by the load object base address in
24107c478bd9Sstevel@tonic-gate 	 * case the address turns out to be in a shared library.
24117c478bd9Sstevel@tonic-gate 	 */
24127c478bd9Sstevel@tonic-gate 	addr -= fptr->file_dyn_base;
24137c478bd9Sstevel@tonic-gate 
24147c478bd9Sstevel@tonic-gate 	/*
24157c478bd9Sstevel@tonic-gate 	 * Search both symbol tables, symtab first, then dynsym.
24167c478bd9Sstevel@tonic-gate 	 */
24177c478bd9Sstevel@tonic-gate 	if ((sym1p = sym_by_addr(&fptr->file_symtab, addr, &sym1, &i1)) != NULL)
24187c478bd9Sstevel@tonic-gate 		name1 = fptr->file_symtab.sym_strs + sym1.st_name;
24197c478bd9Sstevel@tonic-gate 	if ((sym2p = sym_by_addr(&fptr->file_dynsym, addr, &sym2, &i2)) != NULL)
24207c478bd9Sstevel@tonic-gate 		name2 = fptr->file_dynsym.sym_strs + sym2.st_name;
24217c478bd9Sstevel@tonic-gate 
24227c478bd9Sstevel@tonic-gate 	if ((symp = sym_prefer(sym1p, name1, sym2p, name2)) == NULL)
24237c478bd9Sstevel@tonic-gate 		return (-1);
24247c478bd9Sstevel@tonic-gate 
24257c478bd9Sstevel@tonic-gate 	name = (symp == sym1p) ? name1 : name2;
24267c478bd9Sstevel@tonic-gate 	if (bufsize > 0) {
24277c478bd9Sstevel@tonic-gate 		(void) strncpy(sym_name_buffer, name, bufsize);
24287c478bd9Sstevel@tonic-gate 		sym_name_buffer[bufsize - 1] = '\0';
24297c478bd9Sstevel@tonic-gate 	}
24307c478bd9Sstevel@tonic-gate 
24317c478bd9Sstevel@tonic-gate 	*symbolp = *symp;
24327c478bd9Sstevel@tonic-gate 	if (sip != NULL) {
24337c478bd9Sstevel@tonic-gate 		sip->prs_name = bufsize == 0 ? NULL : sym_name_buffer;
2434186f7fbfSEdward Pilatowicz 		if (lmresolve && (fptr->file_rname != NULL))
2435186f7fbfSEdward Pilatowicz 			sip->prs_object = fptr->file_rbase;
2436186f7fbfSEdward Pilatowicz 		else
24377c478bd9Sstevel@tonic-gate 			sip->prs_object = fptr->file_lbase;
24387c478bd9Sstevel@tonic-gate 		sip->prs_id = (symp == sym1p) ? i1 : i2;
24397c478bd9Sstevel@tonic-gate 		sip->prs_table = (symp == sym1p) ? PR_SYMTAB : PR_DYNSYM;
24407c478bd9Sstevel@tonic-gate 		sip->prs_lmid = (fptr->file_lo == NULL) ? LM_ID_BASE :
24417c478bd9Sstevel@tonic-gate 		    fptr->file_lo->rl_lmident;
24427c478bd9Sstevel@tonic-gate 	}
24437c478bd9Sstevel@tonic-gate 
24447c478bd9Sstevel@tonic-gate 	if (GELF_ST_TYPE(symbolp->st_info) != STT_TLS)
24457c478bd9Sstevel@tonic-gate 		symbolp->st_value += fptr->file_dyn_base;
24467c478bd9Sstevel@tonic-gate 
24477c478bd9Sstevel@tonic-gate 	return (0);
24487c478bd9Sstevel@tonic-gate }
24497c478bd9Sstevel@tonic-gate 
24507c478bd9Sstevel@tonic-gate int
2451186f7fbfSEdward Pilatowicz Pxlookup_by_addr(struct ps_prochandle *P, uintptr_t addr, char *buf,
2452186f7fbfSEdward Pilatowicz     size_t bufsize, GElf_Sym *symp, prsyminfo_t *sip)
24537c478bd9Sstevel@tonic-gate {
2454186f7fbfSEdward Pilatowicz 	return (i_Pxlookup_by_addr(P, B_FALSE, addr, buf, bufsize, symp, sip));
2455186f7fbfSEdward Pilatowicz }
2456186f7fbfSEdward Pilatowicz 
2457186f7fbfSEdward Pilatowicz int
2458186f7fbfSEdward Pilatowicz Pxlookup_by_addr_resolved(struct ps_prochandle *P, uintptr_t addr, char *buf,
2459186f7fbfSEdward Pilatowicz     size_t bufsize, GElf_Sym *symp, prsyminfo_t *sip)
2460186f7fbfSEdward Pilatowicz {
2461186f7fbfSEdward Pilatowicz 	return (i_Pxlookup_by_addr(P, B_TRUE, addr, buf, bufsize, symp, sip));
2462186f7fbfSEdward Pilatowicz }
2463186f7fbfSEdward Pilatowicz 
2464186f7fbfSEdward Pilatowicz int
2465186f7fbfSEdward Pilatowicz Plookup_by_addr(struct ps_prochandle *P, uintptr_t addr, char *buf,
2466186f7fbfSEdward Pilatowicz     size_t size, GElf_Sym *symp)
2467186f7fbfSEdward Pilatowicz {
2468186f7fbfSEdward Pilatowicz 	return (i_Pxlookup_by_addr(P, B_FALSE, addr, buf, size, symp, NULL));
24697c478bd9Sstevel@tonic-gate }
24707c478bd9Sstevel@tonic-gate 
24717c478bd9Sstevel@tonic-gate /*
24727c478bd9Sstevel@tonic-gate  * Search the process symbol tables looking for a symbol whose name matches the
24737c478bd9Sstevel@tonic-gate  * specified name and whose object and link map optionally match the specified
24747c478bd9Sstevel@tonic-gate  * parameters.  On success, the function returns 0 and fills in the GElf_Sym
24757c478bd9Sstevel@tonic-gate  * symbol table entry.  On failure, -1 is returned.
24767c478bd9Sstevel@tonic-gate  */
24777c478bd9Sstevel@tonic-gate int
24787c478bd9Sstevel@tonic-gate Pxlookup_by_name(
24797c478bd9Sstevel@tonic-gate 	struct ps_prochandle *P,
24807c478bd9Sstevel@tonic-gate 	Lmid_t lmid,			/* link map to match, or -1 for any */
24817c478bd9Sstevel@tonic-gate 	const char *oname,		/* load object name */
24827c478bd9Sstevel@tonic-gate 	const char *sname,		/* symbol name */
24837c478bd9Sstevel@tonic-gate 	GElf_Sym *symp,			/* returned symbol table entry */
24847c478bd9Sstevel@tonic-gate 	prsyminfo_t *sip)		/* returned symbol info */
24857c478bd9Sstevel@tonic-gate {
24867c478bd9Sstevel@tonic-gate 	map_info_t *mptr;
24877c478bd9Sstevel@tonic-gate 	file_info_t *fptr;
24887c478bd9Sstevel@tonic-gate 	int cnt;
24897c478bd9Sstevel@tonic-gate 
24907c478bd9Sstevel@tonic-gate 	GElf_Sym sym;
24917c478bd9Sstevel@tonic-gate 	prsyminfo_t si;
24927c478bd9Sstevel@tonic-gate 	int rv = -1;
24937c478bd9Sstevel@tonic-gate 	uint_t id;
24947c478bd9Sstevel@tonic-gate 
24957c478bd9Sstevel@tonic-gate 	if (oname == PR_OBJ_EVERY) {
24967c478bd9Sstevel@tonic-gate 		/* create all the file_info_t's for all the mappings */
24977c478bd9Sstevel@tonic-gate 		(void) Prd_agent(P);
24987c478bd9Sstevel@tonic-gate 		cnt = P->num_files;
24997c478bd9Sstevel@tonic-gate 		fptr = list_next(&P->file_head);
25007c478bd9Sstevel@tonic-gate 	} else {
25017c478bd9Sstevel@tonic-gate 		cnt = 1;
25027c478bd9Sstevel@tonic-gate 		if ((mptr = object_name_to_map(P, lmid, oname)) == NULL ||
25037c478bd9Sstevel@tonic-gate 		    (fptr = build_map_symtab(P, mptr)) == NULL)
25047c478bd9Sstevel@tonic-gate 			return (-1);
25057c478bd9Sstevel@tonic-gate 	}
25067c478bd9Sstevel@tonic-gate 
25077c478bd9Sstevel@tonic-gate 	/*
25087c478bd9Sstevel@tonic-gate 	 * Iterate through the loaded object files and look for the symbol
25097c478bd9Sstevel@tonic-gate 	 * name in the .symtab and .dynsym of each.  If we encounter a match
25107c478bd9Sstevel@tonic-gate 	 * with SHN_UNDEF, keep looking in hopes of finding a better match.
25117c478bd9Sstevel@tonic-gate 	 * This means that a name such as "puts" will match the puts function
25127c478bd9Sstevel@tonic-gate 	 * in libc instead of matching the puts PLT entry in the a.out file.
25137c478bd9Sstevel@tonic-gate 	 */
25147c478bd9Sstevel@tonic-gate 	for (; cnt > 0; cnt--, fptr = list_next(fptr)) {
25157c478bd9Sstevel@tonic-gate 		Pbuild_file_symtab(P, fptr);
25167c478bd9Sstevel@tonic-gate 
25177c478bd9Sstevel@tonic-gate 		if (fptr->file_elf == NULL)
25187c478bd9Sstevel@tonic-gate 			continue;
25197c478bd9Sstevel@tonic-gate 
25207c478bd9Sstevel@tonic-gate 		if (lmid != PR_LMID_EVERY && fptr->file_lo != NULL &&
25217c478bd9Sstevel@tonic-gate 		    lmid != fptr->file_lo->rl_lmident)
25227c478bd9Sstevel@tonic-gate 			continue;
25237c478bd9Sstevel@tonic-gate 
2524d51e9074Sab196087 		if (fptr->file_symtab.sym_data_pri != NULL &&
25257c478bd9Sstevel@tonic-gate 		    sym_by_name(&fptr->file_symtab, sname, symp, &id)) {
25267c478bd9Sstevel@tonic-gate 			if (sip != NULL) {
25277c478bd9Sstevel@tonic-gate 				sip->prs_id = id;
25287c478bd9Sstevel@tonic-gate 				sip->prs_table = PR_SYMTAB;
25297c478bd9Sstevel@tonic-gate 				sip->prs_object = oname;
25307c478bd9Sstevel@tonic-gate 				sip->prs_name = sname;
25317c478bd9Sstevel@tonic-gate 				sip->prs_lmid = fptr->file_lo == NULL ?
25327c478bd9Sstevel@tonic-gate 				    LM_ID_BASE : fptr->file_lo->rl_lmident;
25337c478bd9Sstevel@tonic-gate 			}
2534d51e9074Sab196087 		} else if (fptr->file_dynsym.sym_data_pri != NULL &&
25357c478bd9Sstevel@tonic-gate 		    sym_by_name(&fptr->file_dynsym, sname, symp, &id)) {
25367c478bd9Sstevel@tonic-gate 			if (sip != NULL) {
25377c478bd9Sstevel@tonic-gate 				sip->prs_id = id;
25387c478bd9Sstevel@tonic-gate 				sip->prs_table = PR_DYNSYM;
25397c478bd9Sstevel@tonic-gate 				sip->prs_object = oname;
25407c478bd9Sstevel@tonic-gate 				sip->prs_name = sname;
25417c478bd9Sstevel@tonic-gate 				sip->prs_lmid = fptr->file_lo == NULL ?
25427c478bd9Sstevel@tonic-gate 				    LM_ID_BASE : fptr->file_lo->rl_lmident;
25437c478bd9Sstevel@tonic-gate 			}
25447c478bd9Sstevel@tonic-gate 		} else {
25457c478bd9Sstevel@tonic-gate 			continue;
25467c478bd9Sstevel@tonic-gate 		}
25477c478bd9Sstevel@tonic-gate 
25487c478bd9Sstevel@tonic-gate 		if (GELF_ST_TYPE(symp->st_info) != STT_TLS)
25497c478bd9Sstevel@tonic-gate 			symp->st_value += fptr->file_dyn_base;
25507c478bd9Sstevel@tonic-gate 
25517c478bd9Sstevel@tonic-gate 		if (symp->st_shndx != SHN_UNDEF)
25527c478bd9Sstevel@tonic-gate 			return (0);
25537c478bd9Sstevel@tonic-gate 
25547c478bd9Sstevel@tonic-gate 		if (rv != 0) {
25557c478bd9Sstevel@tonic-gate 			if (sip != NULL)
25567c478bd9Sstevel@tonic-gate 				si = *sip;
25577c478bd9Sstevel@tonic-gate 			sym = *symp;
25587c478bd9Sstevel@tonic-gate 			rv = 0;
25597c478bd9Sstevel@tonic-gate 		}
25607c478bd9Sstevel@tonic-gate 	}
25617c478bd9Sstevel@tonic-gate 
25627c478bd9Sstevel@tonic-gate 	if (rv == 0) {
25637c478bd9Sstevel@tonic-gate 		if (sip != NULL)
25647c478bd9Sstevel@tonic-gate 			*sip = si;
25657c478bd9Sstevel@tonic-gate 		*symp = sym;
25667c478bd9Sstevel@tonic-gate 	}
25677c478bd9Sstevel@tonic-gate 
25687c478bd9Sstevel@tonic-gate 	return (rv);
25697c478bd9Sstevel@tonic-gate }
25707c478bd9Sstevel@tonic-gate 
25717c478bd9Sstevel@tonic-gate /*
25727c478bd9Sstevel@tonic-gate  * Search the process symbol tables looking for a symbol whose name matches the
25737c478bd9Sstevel@tonic-gate  * specified name, but without any restriction on the link map id.
25747c478bd9Sstevel@tonic-gate  */
25757c478bd9Sstevel@tonic-gate int
25767c478bd9Sstevel@tonic-gate Plookup_by_name(struct ps_prochandle *P, const char *object,
25777c478bd9Sstevel@tonic-gate 	const char *symbol, GElf_Sym *symp)
25787c478bd9Sstevel@tonic-gate {
25797c478bd9Sstevel@tonic-gate 	return (Pxlookup_by_name(P, PR_LMID_EVERY, object, symbol, symp, NULL));
25807c478bd9Sstevel@tonic-gate }
25817c478bd9Sstevel@tonic-gate 
25827c478bd9Sstevel@tonic-gate /*
25837c478bd9Sstevel@tonic-gate  * Iterate over the process's address space mappings.
25847c478bd9Sstevel@tonic-gate  */
2585186f7fbfSEdward Pilatowicz static int
2586186f7fbfSEdward Pilatowicz i_Pmapping_iter(struct ps_prochandle *P, boolean_t lmresolve,
2587186f7fbfSEdward Pilatowicz     proc_map_f *func, void *cd)
25887c478bd9Sstevel@tonic-gate {
25897c478bd9Sstevel@tonic-gate 	map_info_t *mptr;
25907c478bd9Sstevel@tonic-gate 	file_info_t *fptr;
25917c478bd9Sstevel@tonic-gate 	char *object_name;
25927c478bd9Sstevel@tonic-gate 	int rc = 0;
25937c478bd9Sstevel@tonic-gate 	int i;
25947c478bd9Sstevel@tonic-gate 
25957c478bd9Sstevel@tonic-gate 	/* create all the file_info_t's for all the mappings */
25967c478bd9Sstevel@tonic-gate 	(void) Prd_agent(P);
25977c478bd9Sstevel@tonic-gate 
25987c478bd9Sstevel@tonic-gate 	for (i = 0, mptr = P->mappings; i < P->map_count; i++, mptr++) {
25997c478bd9Sstevel@tonic-gate 		if ((fptr = mptr->map_file) == NULL)
26007c478bd9Sstevel@tonic-gate 			object_name = NULL;
2601186f7fbfSEdward Pilatowicz 		else if (lmresolve && (fptr->file_rname != NULL))
2602186f7fbfSEdward Pilatowicz 			object_name = fptr->file_rname;
26037c478bd9Sstevel@tonic-gate 		else
26047c478bd9Sstevel@tonic-gate 			object_name = fptr->file_lname;
26057c478bd9Sstevel@tonic-gate 		if ((rc = func(cd, &mptr->map_pmap, object_name)) != 0)
26067c478bd9Sstevel@tonic-gate 			return (rc);
26077c478bd9Sstevel@tonic-gate 	}
26087c478bd9Sstevel@tonic-gate 	return (0);
26097c478bd9Sstevel@tonic-gate }
26107c478bd9Sstevel@tonic-gate 
2611186f7fbfSEdward Pilatowicz int
2612186f7fbfSEdward Pilatowicz Pmapping_iter(struct ps_prochandle *P, proc_map_f *func, void *cd)
2613186f7fbfSEdward Pilatowicz {
2614186f7fbfSEdward Pilatowicz 	return (i_Pmapping_iter(P, B_FALSE, func, cd));
2615186f7fbfSEdward Pilatowicz }
2616186f7fbfSEdward Pilatowicz 
2617186f7fbfSEdward Pilatowicz int
2618186f7fbfSEdward Pilatowicz Pmapping_iter_resolved(struct ps_prochandle *P, proc_map_f *func, void *cd)
2619186f7fbfSEdward Pilatowicz {
2620186f7fbfSEdward Pilatowicz 	return (i_Pmapping_iter(P, B_TRUE, func, cd));
2621186f7fbfSEdward Pilatowicz }
2622186f7fbfSEdward Pilatowicz 
26237c478bd9Sstevel@tonic-gate /*
26247c478bd9Sstevel@tonic-gate  * Iterate over the process's mapped objects.
26257c478bd9Sstevel@tonic-gate  */
2626186f7fbfSEdward Pilatowicz static int
2627186f7fbfSEdward Pilatowicz i_Pobject_iter(struct ps_prochandle *P, boolean_t lmresolve,
2628186f7fbfSEdward Pilatowicz     proc_map_f *func, void *cd)
26297c478bd9Sstevel@tonic-gate {
26307c478bd9Sstevel@tonic-gate 	map_info_t *mptr;
26317c478bd9Sstevel@tonic-gate 	file_info_t *fptr;
26327c478bd9Sstevel@tonic-gate 	uint_t cnt;
26337c478bd9Sstevel@tonic-gate 	int rc = 0;
26347c478bd9Sstevel@tonic-gate 
26357c478bd9Sstevel@tonic-gate 	(void) Prd_agent(P); /* create file_info_t's for all the mappings */
26367c478bd9Sstevel@tonic-gate 	Pupdate_maps(P);
26377c478bd9Sstevel@tonic-gate 
26387c478bd9Sstevel@tonic-gate 	for (cnt = P->num_files, fptr = list_next(&P->file_head);
26397c478bd9Sstevel@tonic-gate 	    cnt; cnt--, fptr = list_next(fptr)) {
2640186f7fbfSEdward Pilatowicz 		const char *lname;
26417c478bd9Sstevel@tonic-gate 
2642186f7fbfSEdward Pilatowicz 		if (lmresolve && (fptr->file_rname != NULL))
2643186f7fbfSEdward Pilatowicz 			lname = fptr->file_rname;
2644186f7fbfSEdward Pilatowicz 		else if (fptr->file_lname != NULL)
2645186f7fbfSEdward Pilatowicz 			lname = fptr->file_lname;
2646186f7fbfSEdward Pilatowicz 		else
2647186f7fbfSEdward Pilatowicz 			lname = "";
26487c478bd9Sstevel@tonic-gate 
26497c478bd9Sstevel@tonic-gate 		if ((mptr = fptr->file_map) == NULL)
26507c478bd9Sstevel@tonic-gate 			continue;
26517c478bd9Sstevel@tonic-gate 
26527c478bd9Sstevel@tonic-gate 		if ((rc = func(cd, &mptr->map_pmap, lname)) != 0)
26537c478bd9Sstevel@tonic-gate 			return (rc);
26547c478bd9Sstevel@tonic-gate 	}
26557c478bd9Sstevel@tonic-gate 	return (0);
26567c478bd9Sstevel@tonic-gate }
26577c478bd9Sstevel@tonic-gate 
2658186f7fbfSEdward Pilatowicz int
2659186f7fbfSEdward Pilatowicz Pobject_iter(struct ps_prochandle *P, proc_map_f *func, void *cd)
2660186f7fbfSEdward Pilatowicz {
2661186f7fbfSEdward Pilatowicz 	return (i_Pobject_iter(P, B_FALSE, func, cd));
2662186f7fbfSEdward Pilatowicz }
2663186f7fbfSEdward Pilatowicz 
2664186f7fbfSEdward Pilatowicz int
2665186f7fbfSEdward Pilatowicz Pobject_iter_resolved(struct ps_prochandle *P, proc_map_f *func, void *cd)
2666186f7fbfSEdward Pilatowicz {
2667186f7fbfSEdward Pilatowicz 	return (i_Pobject_iter(P, B_TRUE, func, cd));
2668186f7fbfSEdward Pilatowicz }
2669186f7fbfSEdward Pilatowicz 
2670186f7fbfSEdward Pilatowicz static char *
2671186f7fbfSEdward Pilatowicz i_Pobjname(struct ps_prochandle *P, boolean_t lmresolve, uintptr_t addr,
26727c478bd9Sstevel@tonic-gate 	char *buffer, size_t bufsize)
26737c478bd9Sstevel@tonic-gate {
26747c478bd9Sstevel@tonic-gate 	map_info_t *mptr;
26757c478bd9Sstevel@tonic-gate 	file_info_t *fptr;
26767c478bd9Sstevel@tonic-gate 
26777c478bd9Sstevel@tonic-gate 	/* create all the file_info_t's for all the mappings */
26787c478bd9Sstevel@tonic-gate 	(void) Prd_agent(P);
26797c478bd9Sstevel@tonic-gate 
2680186f7fbfSEdward Pilatowicz 	if ((mptr = Paddr2mptr(P, addr)) == NULL)
2681186f7fbfSEdward Pilatowicz 		return (NULL);
2682186f7fbfSEdward Pilatowicz 
2683186f7fbfSEdward Pilatowicz 	if (!lmresolve) {
2684186f7fbfSEdward Pilatowicz 		if (((fptr = mptr->map_file) == NULL) ||
2685186f7fbfSEdward Pilatowicz 		    (fptr->file_lname == NULL))
2686186f7fbfSEdward Pilatowicz 			return (NULL);
2687186f7fbfSEdward Pilatowicz 		(void) strlcpy(buffer, fptr->file_lname, bufsize);
26887c478bd9Sstevel@tonic-gate 		return (buffer);
26897c478bd9Sstevel@tonic-gate 	}
2690186f7fbfSEdward Pilatowicz 
2691186f7fbfSEdward Pilatowicz 	/* Check for a cached copy of the resolved path */
2692186f7fbfSEdward Pilatowicz 	if (Pfindmap(P, mptr, buffer, bufsize) != NULL)
2693186f7fbfSEdward Pilatowicz 		return (buffer);
2694186f7fbfSEdward Pilatowicz 
26957c478bd9Sstevel@tonic-gate 	return (NULL);
26967c478bd9Sstevel@tonic-gate }
26977c478bd9Sstevel@tonic-gate 
26987c478bd9Sstevel@tonic-gate /*
2699186f7fbfSEdward Pilatowicz  * Given a virtual address, return the name of the underlying
2700186f7fbfSEdward Pilatowicz  * mapped object (file) as provided by the dynamic linker.
2701186f7fbfSEdward Pilatowicz  * Return NULL if we can't find any name information for the object.
2702186f7fbfSEdward Pilatowicz  */
2703186f7fbfSEdward Pilatowicz char *
2704186f7fbfSEdward Pilatowicz Pobjname(struct ps_prochandle *P, uintptr_t addr,
2705186f7fbfSEdward Pilatowicz 	char *buffer, size_t bufsize)
2706186f7fbfSEdward Pilatowicz {
2707186f7fbfSEdward Pilatowicz 	return (i_Pobjname(P, B_FALSE, addr, buffer, bufsize));
2708186f7fbfSEdward Pilatowicz }
2709186f7fbfSEdward Pilatowicz 
2710186f7fbfSEdward Pilatowicz /*
2711186f7fbfSEdward Pilatowicz  * Given a virtual address, try to return a filesystem path to the
2712186f7fbfSEdward Pilatowicz  * underlying mapped object (file).  If we're in the global zone,
2713186f7fbfSEdward Pilatowicz  * this path could resolve to an object in another zone.  If we're
2714186f7fbfSEdward Pilatowicz  * unable return a valid filesystem path, we'll fall back to providing
2715186f7fbfSEdward Pilatowicz  * the mapped object (file) name provided by the dynamic linker in
2716186f7fbfSEdward Pilatowicz  * the target process (ie, the object reported by Pobjname()).
2717186f7fbfSEdward Pilatowicz  */
2718186f7fbfSEdward Pilatowicz char *
2719186f7fbfSEdward Pilatowicz Pobjname_resolved(struct ps_prochandle *P, uintptr_t addr,
2720186f7fbfSEdward Pilatowicz 	char *buffer, size_t bufsize)
2721186f7fbfSEdward Pilatowicz {
2722186f7fbfSEdward Pilatowicz 	return (i_Pobjname(P, B_TRUE, addr, buffer, bufsize));
2723186f7fbfSEdward Pilatowicz }
2724186f7fbfSEdward Pilatowicz 
2725186f7fbfSEdward Pilatowicz /*
27267c478bd9Sstevel@tonic-gate  * Given a virtual address, return the link map id of the underlying mapped
27277c478bd9Sstevel@tonic-gate  * object (file), as provided by the dynamic linker.  Return -1 on failure.
27287c478bd9Sstevel@tonic-gate  */
27297c478bd9Sstevel@tonic-gate int
27307c478bd9Sstevel@tonic-gate Plmid(struct ps_prochandle *P, uintptr_t addr, Lmid_t *lmidp)
27317c478bd9Sstevel@tonic-gate {
27327c478bd9Sstevel@tonic-gate 	map_info_t *mptr;
27337c478bd9Sstevel@tonic-gate 	file_info_t *fptr;
27347c478bd9Sstevel@tonic-gate 
27357c478bd9Sstevel@tonic-gate 	/* create all the file_info_t's for all the mappings */
27367c478bd9Sstevel@tonic-gate 	(void) Prd_agent(P);
27377c478bd9Sstevel@tonic-gate 
27387c478bd9Sstevel@tonic-gate 	if ((mptr = Paddr2mptr(P, addr)) != NULL &&
27397c478bd9Sstevel@tonic-gate 	    (fptr = mptr->map_file) != NULL && fptr->file_lo != NULL) {
27407c478bd9Sstevel@tonic-gate 		*lmidp = fptr->file_lo->rl_lmident;
27417c478bd9Sstevel@tonic-gate 		return (0);
27427c478bd9Sstevel@tonic-gate 	}
27437c478bd9Sstevel@tonic-gate 
27447c478bd9Sstevel@tonic-gate 	return (-1);
27457c478bd9Sstevel@tonic-gate }
27467c478bd9Sstevel@tonic-gate 
27477c478bd9Sstevel@tonic-gate /*
27487c478bd9Sstevel@tonic-gate  * Given an object name and optional lmid, iterate over the object's symbols.
27497c478bd9Sstevel@tonic-gate  * If which == PR_SYMTAB, search the normal symbol table.
27507c478bd9Sstevel@tonic-gate  * If which == PR_DYNSYM, search the dynamic symbol table.
27517c478bd9Sstevel@tonic-gate  */
27527c478bd9Sstevel@tonic-gate static int
27537c478bd9Sstevel@tonic-gate Psymbol_iter_com(struct ps_prochandle *P, Lmid_t lmid, const char *object_name,
27547c478bd9Sstevel@tonic-gate     int which, int mask, pr_order_t order, proc_xsym_f *func, void *cd)
27557c478bd9Sstevel@tonic-gate {
2756*7e16fca0SAli Bahrami #if STT_NUM != (STT_IFUNC + 1)
2757*7e16fca0SAli Bahrami #error "STT_NUM has grown. update Psymbol_iter_com()"
2758*7e16fca0SAli Bahrami #endif
2759*7e16fca0SAli Bahrami 
27607c478bd9Sstevel@tonic-gate 	GElf_Sym sym;
2761f957ecc1Svb160487 	GElf_Shdr shdr;
27627c478bd9Sstevel@tonic-gate 	map_info_t *mptr;
27637c478bd9Sstevel@tonic-gate 	file_info_t *fptr;
27647c478bd9Sstevel@tonic-gate 	sym_tbl_t *symtab;
27657c478bd9Sstevel@tonic-gate 	size_t symn;
27667c478bd9Sstevel@tonic-gate 	const char *strs;
27677c478bd9Sstevel@tonic-gate 	size_t strsz;
27687c478bd9Sstevel@tonic-gate 	prsyminfo_t si;
27697c478bd9Sstevel@tonic-gate 	int rv;
27707c478bd9Sstevel@tonic-gate 	uint_t *map, i, count, ndx;
27717c478bd9Sstevel@tonic-gate 
27727c478bd9Sstevel@tonic-gate 	if ((mptr = object_name_to_map(P, lmid, object_name)) == NULL)
27737c478bd9Sstevel@tonic-gate 		return (-1);
27747c478bd9Sstevel@tonic-gate 
27757c478bd9Sstevel@tonic-gate 	if ((fptr = build_map_symtab(P, mptr)) == NULL || /* no mapped file */
27767c478bd9Sstevel@tonic-gate 	    fptr->file_elf == NULL)			/* not an ELF file */
27777c478bd9Sstevel@tonic-gate 		return (-1);
27787c478bd9Sstevel@tonic-gate 
27797c478bd9Sstevel@tonic-gate 	/*
27807c478bd9Sstevel@tonic-gate 	 * Search the specified symbol table.
27817c478bd9Sstevel@tonic-gate 	 */
27827c478bd9Sstevel@tonic-gate 	switch (which) {
27837c478bd9Sstevel@tonic-gate 	case PR_SYMTAB:
27847c478bd9Sstevel@tonic-gate 		symtab = &fptr->file_symtab;
27857c478bd9Sstevel@tonic-gate 		si.prs_table = PR_SYMTAB;
27867c478bd9Sstevel@tonic-gate 		break;
27877c478bd9Sstevel@tonic-gate 	case PR_DYNSYM:
27887c478bd9Sstevel@tonic-gate 		symtab = &fptr->file_dynsym;
27897c478bd9Sstevel@tonic-gate 		si.prs_table = PR_DYNSYM;
27907c478bd9Sstevel@tonic-gate 		break;
27917c478bd9Sstevel@tonic-gate 	default:
27927c478bd9Sstevel@tonic-gate 		return (-1);
27937c478bd9Sstevel@tonic-gate 	}
27947c478bd9Sstevel@tonic-gate 
27957c478bd9Sstevel@tonic-gate 	si.prs_object = object_name;
27967c478bd9Sstevel@tonic-gate 	si.prs_lmid = fptr->file_lo == NULL ?
27977c478bd9Sstevel@tonic-gate 	    LM_ID_BASE : fptr->file_lo->rl_lmident;
27987c478bd9Sstevel@tonic-gate 
27997c478bd9Sstevel@tonic-gate 	symn = symtab->sym_symn;
28007c478bd9Sstevel@tonic-gate 	strs = symtab->sym_strs;
28017c478bd9Sstevel@tonic-gate 	strsz = symtab->sym_strsz;
28027c478bd9Sstevel@tonic-gate 
28037c478bd9Sstevel@tonic-gate 	switch (order) {
28047c478bd9Sstevel@tonic-gate 	case PRO_NATURAL:
28057c478bd9Sstevel@tonic-gate 		map = NULL;
28067c478bd9Sstevel@tonic-gate 		count = symn;
28077c478bd9Sstevel@tonic-gate 		break;
28087c478bd9Sstevel@tonic-gate 	case PRO_BYNAME:
28097c478bd9Sstevel@tonic-gate 		map = symtab->sym_byname;
28107c478bd9Sstevel@tonic-gate 		count = symtab->sym_count;
28117c478bd9Sstevel@tonic-gate 		break;
28127c478bd9Sstevel@tonic-gate 	case PRO_BYADDR:
28137c478bd9Sstevel@tonic-gate 		map = symtab->sym_byaddr;
28147c478bd9Sstevel@tonic-gate 		count = symtab->sym_count;
28157c478bd9Sstevel@tonic-gate 		break;
28167c478bd9Sstevel@tonic-gate 	default:
28177c478bd9Sstevel@tonic-gate 		return (-1);
28187c478bd9Sstevel@tonic-gate 	}
28197c478bd9Sstevel@tonic-gate 
2820d51e9074Sab196087 	if (symtab->sym_data_pri == NULL || strs == NULL || count == 0)
2821d51e9074Sab196087 		return (-1);
2822d51e9074Sab196087 
28237c478bd9Sstevel@tonic-gate 	rv = 0;
28247c478bd9Sstevel@tonic-gate 
28257c478bd9Sstevel@tonic-gate 	for (i = 0; i < count; i++) {
28267c478bd9Sstevel@tonic-gate 		ndx = map == NULL ? i : map[i];
2827d51e9074Sab196087 		if (symtab_getsym(symtab, ndx, &sym) != NULL) {
28287c478bd9Sstevel@tonic-gate 			uint_t s_bind, s_type, type;
28297c478bd9Sstevel@tonic-gate 
28307c478bd9Sstevel@tonic-gate 			if (sym.st_name >= strsz)	/* invalid st_name */
28317c478bd9Sstevel@tonic-gate 				continue;
28327c478bd9Sstevel@tonic-gate 
28337c478bd9Sstevel@tonic-gate 			s_bind = GELF_ST_BIND(sym.st_info);
28347c478bd9Sstevel@tonic-gate 			s_type = GELF_ST_TYPE(sym.st_info);
28357c478bd9Sstevel@tonic-gate 
28367c478bd9Sstevel@tonic-gate 			/*
28377c478bd9Sstevel@tonic-gate 			 * In case you haven't already guessed, this relies on
28387c478bd9Sstevel@tonic-gate 			 * the bitmask used in <libproc.h> for encoding symbol
28397c478bd9Sstevel@tonic-gate 			 * type and binding matching the order of STB and STT
2840*7e16fca0SAli Bahrami 			 * constants in <sys/elf.h>.  Changes to ELF must
2841*7e16fca0SAli Bahrami 			 * maintain binary compatibility, so I think this is
28427c478bd9Sstevel@tonic-gate 			 * reasonably fair game.
28437c478bd9Sstevel@tonic-gate 			 */
2844*7e16fca0SAli Bahrami 			if (s_bind < STB_NUM && s_type < STT_IFUNC) {
28457c478bd9Sstevel@tonic-gate 				type = (1 << (s_type + 8)) | (1 << s_bind);
28467c478bd9Sstevel@tonic-gate 				if ((type & ~mask) != 0)
28477c478bd9Sstevel@tonic-gate 					continue;
28487c478bd9Sstevel@tonic-gate 			} else
28497c478bd9Sstevel@tonic-gate 				continue; /* Invalid type or binding */
28507c478bd9Sstevel@tonic-gate 
28517c478bd9Sstevel@tonic-gate 			if (GELF_ST_TYPE(sym.st_info) != STT_TLS)
28527c478bd9Sstevel@tonic-gate 				sym.st_value += fptr->file_dyn_base;
28537c478bd9Sstevel@tonic-gate 
28547c478bd9Sstevel@tonic-gate 			si.prs_name = strs + sym.st_name;
2855f957ecc1Svb160487 
2856f957ecc1Svb160487 			/*
2857f957ecc1Svb160487 			 * If symbol's type is STT_SECTION, then try to lookup
2858f957ecc1Svb160487 			 * the name of the corresponding section.
2859f957ecc1Svb160487 			 */
2860f957ecc1Svb160487 			if (GELF_ST_TYPE(sym.st_info) == STT_SECTION &&
2861f957ecc1Svb160487 			    fptr->file_shstrs != NULL &&
2862f957ecc1Svb160487 			    gelf_getshdr(elf_getscn(fptr->file_elf,
2863f957ecc1Svb160487 			    sym.st_shndx), &shdr) != NULL &&
2864f957ecc1Svb160487 			    shdr.sh_name != 0 &&
2865f957ecc1Svb160487 			    shdr.sh_name < fptr->file_shstrsz)
2866f957ecc1Svb160487 				si.prs_name = fptr->file_shstrs + shdr.sh_name;
2867f957ecc1Svb160487 
28687c478bd9Sstevel@tonic-gate 			si.prs_id = ndx;
2869f957ecc1Svb160487 			if ((rv = func(cd, &sym, si.prs_name, &si)) != 0)
28707c478bd9Sstevel@tonic-gate 				break;
28717c478bd9Sstevel@tonic-gate 		}
28727c478bd9Sstevel@tonic-gate 	}
28737c478bd9Sstevel@tonic-gate 
28747c478bd9Sstevel@tonic-gate 	return (rv);
28757c478bd9Sstevel@tonic-gate }
28767c478bd9Sstevel@tonic-gate 
28777c478bd9Sstevel@tonic-gate int
28787c478bd9Sstevel@tonic-gate Pxsymbol_iter(struct ps_prochandle *P, Lmid_t lmid, const char *object_name,
28797c478bd9Sstevel@tonic-gate     int which, int mask, proc_xsym_f *func, void *cd)
28807c478bd9Sstevel@tonic-gate {
28817c478bd9Sstevel@tonic-gate 	return (Psymbol_iter_com(P, lmid, object_name, which, mask,
28827c478bd9Sstevel@tonic-gate 	    PRO_NATURAL, func, cd));
28837c478bd9Sstevel@tonic-gate }
28847c478bd9Sstevel@tonic-gate 
28857c478bd9Sstevel@tonic-gate int
28867c478bd9Sstevel@tonic-gate Psymbol_iter_by_lmid(struct ps_prochandle *P, Lmid_t lmid,
28877c478bd9Sstevel@tonic-gate     const char *object_name, int which, int mask, proc_sym_f *func, void *cd)
28887c478bd9Sstevel@tonic-gate {
28897c478bd9Sstevel@tonic-gate 	return (Psymbol_iter_com(P, lmid, object_name, which, mask,
28907c478bd9Sstevel@tonic-gate 	    PRO_NATURAL, (proc_xsym_f *)func, cd));
28917c478bd9Sstevel@tonic-gate }
28927c478bd9Sstevel@tonic-gate 
28937c478bd9Sstevel@tonic-gate int
28947c478bd9Sstevel@tonic-gate Psymbol_iter(struct ps_prochandle *P,
28957c478bd9Sstevel@tonic-gate     const char *object_name, int which, int mask, proc_sym_f *func, void *cd)
28967c478bd9Sstevel@tonic-gate {
28977c478bd9Sstevel@tonic-gate 	return (Psymbol_iter_com(P, PR_LMID_EVERY, object_name, which, mask,
28987c478bd9Sstevel@tonic-gate 	    PRO_NATURAL, (proc_xsym_f *)func, cd));
28997c478bd9Sstevel@tonic-gate }
29007c478bd9Sstevel@tonic-gate 
29017c478bd9Sstevel@tonic-gate int
29027c478bd9Sstevel@tonic-gate Psymbol_iter_by_addr(struct ps_prochandle *P,
29037c478bd9Sstevel@tonic-gate     const char *object_name, int which, int mask, proc_sym_f *func, void *cd)
29047c478bd9Sstevel@tonic-gate {
29057c478bd9Sstevel@tonic-gate 	return (Psymbol_iter_com(P, PR_LMID_EVERY, object_name, which, mask,
29067c478bd9Sstevel@tonic-gate 	    PRO_BYADDR, (proc_xsym_f *)func, cd));
29077c478bd9Sstevel@tonic-gate }
29087c478bd9Sstevel@tonic-gate 
29097c478bd9Sstevel@tonic-gate int
29107c478bd9Sstevel@tonic-gate Psymbol_iter_by_name(struct ps_prochandle *P,
29117c478bd9Sstevel@tonic-gate     const char *object_name, int which, int mask, proc_sym_f *func, void *cd)
29127c478bd9Sstevel@tonic-gate {
29137c478bd9Sstevel@tonic-gate 	return (Psymbol_iter_com(P, PR_LMID_EVERY, object_name, which, mask,
29147c478bd9Sstevel@tonic-gate 	    PRO_BYNAME, (proc_xsym_f *)func, cd));
29157c478bd9Sstevel@tonic-gate }
29167c478bd9Sstevel@tonic-gate 
29177c478bd9Sstevel@tonic-gate /*
29187c478bd9Sstevel@tonic-gate  * Get the platform string from the core file if we have it;
29197c478bd9Sstevel@tonic-gate  * just perform the system call for the caller if this is a live process.
29207c478bd9Sstevel@tonic-gate  */
29217c478bd9Sstevel@tonic-gate char *
29227c478bd9Sstevel@tonic-gate Pplatform(struct ps_prochandle *P, char *s, size_t n)
29237c478bd9Sstevel@tonic-gate {
29247c478bd9Sstevel@tonic-gate 	if (P->state == PS_IDLE) {
29257c478bd9Sstevel@tonic-gate 		errno = ENODATA;
29267c478bd9Sstevel@tonic-gate 		return (NULL);
29277c478bd9Sstevel@tonic-gate 	}
29287c478bd9Sstevel@tonic-gate 
29297c478bd9Sstevel@tonic-gate 	if (P->state == PS_DEAD) {
29307c478bd9Sstevel@tonic-gate 		if (P->core->core_platform == NULL) {
29317c478bd9Sstevel@tonic-gate 			errno = ENODATA;
29327c478bd9Sstevel@tonic-gate 			return (NULL);
29337c478bd9Sstevel@tonic-gate 		}
29347c478bd9Sstevel@tonic-gate 		(void) strncpy(s, P->core->core_platform, n - 1);
29357c478bd9Sstevel@tonic-gate 		s[n - 1] = '\0';
29367c478bd9Sstevel@tonic-gate 
29377c478bd9Sstevel@tonic-gate 	} else if (sysinfo(SI_PLATFORM, s, n) == -1)
29387c478bd9Sstevel@tonic-gate 		return (NULL);
29397c478bd9Sstevel@tonic-gate 
29407c478bd9Sstevel@tonic-gate 	return (s);
29417c478bd9Sstevel@tonic-gate }
29427c478bd9Sstevel@tonic-gate 
29437c478bd9Sstevel@tonic-gate /*
29447c478bd9Sstevel@tonic-gate  * Get the uname(2) information from the core file if we have it;
29457c478bd9Sstevel@tonic-gate  * just perform the system call for the caller if this is a live process.
29467c478bd9Sstevel@tonic-gate  */
29477c478bd9Sstevel@tonic-gate int
29487c478bd9Sstevel@tonic-gate Puname(struct ps_prochandle *P, struct utsname *u)
29497c478bd9Sstevel@tonic-gate {
29507c478bd9Sstevel@tonic-gate 	if (P->state == PS_IDLE) {
29517c478bd9Sstevel@tonic-gate 		errno = ENODATA;
29527c478bd9Sstevel@tonic-gate 		return (-1);
29537c478bd9Sstevel@tonic-gate 	}
29547c478bd9Sstevel@tonic-gate 
29557c478bd9Sstevel@tonic-gate 	if (P->state == PS_DEAD) {
29567c478bd9Sstevel@tonic-gate 		if (P->core->core_uts == NULL) {
29577c478bd9Sstevel@tonic-gate 			errno = ENODATA;
29587c478bd9Sstevel@tonic-gate 			return (-1);
29597c478bd9Sstevel@tonic-gate 		}
29607c478bd9Sstevel@tonic-gate 		(void) memcpy(u, P->core->core_uts, sizeof (struct utsname));
29617c478bd9Sstevel@tonic-gate 		return (0);
29627c478bd9Sstevel@tonic-gate 	}
29637c478bd9Sstevel@tonic-gate 	return (uname(u));
29647c478bd9Sstevel@tonic-gate }
29657c478bd9Sstevel@tonic-gate 
29667c478bd9Sstevel@tonic-gate /*
29677c478bd9Sstevel@tonic-gate  * Called from Pcreate(), Pgrab(), and Pfgrab_core() to initialize
29687c478bd9Sstevel@tonic-gate  * the symbol table heads in the new ps_prochandle.
29697c478bd9Sstevel@tonic-gate  */
29707c478bd9Sstevel@tonic-gate void
29717c478bd9Sstevel@tonic-gate Pinitsym(struct ps_prochandle *P)
29727c478bd9Sstevel@tonic-gate {
29737c478bd9Sstevel@tonic-gate 	P->num_files = 0;
29747c478bd9Sstevel@tonic-gate 	list_link(&P->file_head, NULL);
29757c478bd9Sstevel@tonic-gate }
29767c478bd9Sstevel@tonic-gate 
29777c478bd9Sstevel@tonic-gate /*
29787c478bd9Sstevel@tonic-gate  * Called from Prelease() to destroy the symbol tables.
29797c478bd9Sstevel@tonic-gate  * Must be called by the client after an exec() in the victim process.
29807c478bd9Sstevel@tonic-gate  */
29817c478bd9Sstevel@tonic-gate void
29827c478bd9Sstevel@tonic-gate Preset_maps(struct ps_prochandle *P)
29837c478bd9Sstevel@tonic-gate {
29847c478bd9Sstevel@tonic-gate 	int i;
29857c478bd9Sstevel@tonic-gate 
29867c478bd9Sstevel@tonic-gate 	if (P->rap != NULL) {
29877c478bd9Sstevel@tonic-gate 		rd_delete(P->rap);
29887c478bd9Sstevel@tonic-gate 		P->rap = NULL;
29897c478bd9Sstevel@tonic-gate 	}
29907c478bd9Sstevel@tonic-gate 
29917c478bd9Sstevel@tonic-gate 	if (P->execname != NULL) {
29927c478bd9Sstevel@tonic-gate 		free(P->execname);
29937c478bd9Sstevel@tonic-gate 		P->execname = NULL;
29947c478bd9Sstevel@tonic-gate 	}
29957c478bd9Sstevel@tonic-gate 
29967c478bd9Sstevel@tonic-gate 	if (P->auxv != NULL) {
29977c478bd9Sstevel@tonic-gate 		free(P->auxv);
29987c478bd9Sstevel@tonic-gate 		P->auxv = NULL;
29997c478bd9Sstevel@tonic-gate 		P->nauxv = 0;
30007c478bd9Sstevel@tonic-gate 	}
30017c478bd9Sstevel@tonic-gate 
30027c478bd9Sstevel@tonic-gate 	for (i = 0; i < P->map_count; i++)
30037c478bd9Sstevel@tonic-gate 		map_info_free(P, &P->mappings[i]);
30047c478bd9Sstevel@tonic-gate 
30057c478bd9Sstevel@tonic-gate 	if (P->mappings != NULL) {
30067c478bd9Sstevel@tonic-gate 		free(P->mappings);
30077c478bd9Sstevel@tonic-gate 		P->mappings = NULL;
30087c478bd9Sstevel@tonic-gate 	}
30097c478bd9Sstevel@tonic-gate 	P->map_count = P->map_alloc = 0;
30107c478bd9Sstevel@tonic-gate 
30117c478bd9Sstevel@tonic-gate 	P->info_valid = 0;
30127c478bd9Sstevel@tonic-gate }
30137c478bd9Sstevel@tonic-gate 
30147c478bd9Sstevel@tonic-gate typedef struct getenv_data {
30157c478bd9Sstevel@tonic-gate 	char *buf;
30167c478bd9Sstevel@tonic-gate 	size_t bufsize;
30177c478bd9Sstevel@tonic-gate 	const char *search;
30187c478bd9Sstevel@tonic-gate 	size_t searchlen;
30197c478bd9Sstevel@tonic-gate } getenv_data_t;
30207c478bd9Sstevel@tonic-gate 
30217c478bd9Sstevel@tonic-gate /*ARGSUSED*/
30227c478bd9Sstevel@tonic-gate static int
30237c478bd9Sstevel@tonic-gate getenv_func(void *data, struct ps_prochandle *P, uintptr_t addr,
30247c478bd9Sstevel@tonic-gate     const char *nameval)
30257c478bd9Sstevel@tonic-gate {
30267c478bd9Sstevel@tonic-gate 	getenv_data_t *d = data;
30277c478bd9Sstevel@tonic-gate 	size_t len;
30287c478bd9Sstevel@tonic-gate 
30297c478bd9Sstevel@tonic-gate 	if (nameval == NULL)
30307c478bd9Sstevel@tonic-gate 		return (0);
30317c478bd9Sstevel@tonic-gate 
30327c478bd9Sstevel@tonic-gate 	if (d->searchlen < strlen(nameval) &&
30337c478bd9Sstevel@tonic-gate 	    strncmp(nameval, d->search, d->searchlen) == 0 &&
30347c478bd9Sstevel@tonic-gate 	    nameval[d->searchlen] == '=') {
30357c478bd9Sstevel@tonic-gate 		len = MIN(strlen(nameval), d->bufsize - 1);
30367c478bd9Sstevel@tonic-gate 		(void) strncpy(d->buf, nameval, len);
30377c478bd9Sstevel@tonic-gate 		d->buf[len] = '\0';
30387c478bd9Sstevel@tonic-gate 		return (1);
30397c478bd9Sstevel@tonic-gate 	}
30407c478bd9Sstevel@tonic-gate 
30417c478bd9Sstevel@tonic-gate 	return (0);
30427c478bd9Sstevel@tonic-gate }
30437c478bd9Sstevel@tonic-gate 
30447c478bd9Sstevel@tonic-gate char *
30457c478bd9Sstevel@tonic-gate Pgetenv(struct ps_prochandle *P, const char *name, char *buf, size_t buflen)
30467c478bd9Sstevel@tonic-gate {
30477c478bd9Sstevel@tonic-gate 	getenv_data_t d;
30487c478bd9Sstevel@tonic-gate 
30497c478bd9Sstevel@tonic-gate 	d.buf = buf;
30507c478bd9Sstevel@tonic-gate 	d.bufsize = buflen;
30517c478bd9Sstevel@tonic-gate 	d.search = name;
30527c478bd9Sstevel@tonic-gate 	d.searchlen = strlen(name);
30537c478bd9Sstevel@tonic-gate 
30547c478bd9Sstevel@tonic-gate 	if (Penv_iter(P, getenv_func, &d) == 1) {
30557c478bd9Sstevel@tonic-gate 		char *equals = strchr(d.buf, '=');
30567c478bd9Sstevel@tonic-gate 
30577c478bd9Sstevel@tonic-gate 		if (equals != NULL) {
30587c478bd9Sstevel@tonic-gate 			(void) memmove(d.buf, equals + 1,
30597c478bd9Sstevel@tonic-gate 			    d.buf + buflen - equals - 1);
30607c478bd9Sstevel@tonic-gate 			d.buf[d.buf + buflen - equals] = '\0';
30617c478bd9Sstevel@tonic-gate 
30627c478bd9Sstevel@tonic-gate 			return (buf);
30637c478bd9Sstevel@tonic-gate 		}
30647c478bd9Sstevel@tonic-gate 	}
30657c478bd9Sstevel@tonic-gate 
30667c478bd9Sstevel@tonic-gate 	return (NULL);
30677c478bd9Sstevel@tonic-gate }
30687c478bd9Sstevel@tonic-gate 
30697c478bd9Sstevel@tonic-gate /* number of argument or environment pointers to read all at once */
30707c478bd9Sstevel@tonic-gate #define	NARG	100
30717c478bd9Sstevel@tonic-gate 
30727c478bd9Sstevel@tonic-gate int
30737c478bd9Sstevel@tonic-gate Penv_iter(struct ps_prochandle *P, proc_env_f *func, void *data)
30747c478bd9Sstevel@tonic-gate {
30757c478bd9Sstevel@tonic-gate 	const psinfo_t *psp;
30767c478bd9Sstevel@tonic-gate 	uintptr_t envpoff;
30777c478bd9Sstevel@tonic-gate 	GElf_Sym sym;
30787c478bd9Sstevel@tonic-gate 	int ret;
30797c478bd9Sstevel@tonic-gate 	char *buf, *nameval;
30807c478bd9Sstevel@tonic-gate 	size_t buflen;
30817c478bd9Sstevel@tonic-gate 
30827c478bd9Sstevel@tonic-gate 	int nenv = NARG;
30837c478bd9Sstevel@tonic-gate 	long envp[NARG];
30847c478bd9Sstevel@tonic-gate 
30857c478bd9Sstevel@tonic-gate 	/*
30867c478bd9Sstevel@tonic-gate 	 * Attempt to find the "_environ" variable in the process.
30877c478bd9Sstevel@tonic-gate 	 * Failing that, use the original value provided by Ppsinfo().
30887c478bd9Sstevel@tonic-gate 	 */
30897c478bd9Sstevel@tonic-gate 	if ((psp = Ppsinfo(P)) == NULL)
30907c478bd9Sstevel@tonic-gate 		return (-1);
30917c478bd9Sstevel@tonic-gate 
30927c478bd9Sstevel@tonic-gate 	envpoff = psp->pr_envp; /* Default if no _environ found */
30937c478bd9Sstevel@tonic-gate 
30947c478bd9Sstevel@tonic-gate 	if (Plookup_by_name(P, PR_OBJ_EXEC, "_environ", &sym) == 0) {
30957c478bd9Sstevel@tonic-gate 		if (P->status.pr_dmodel == PR_MODEL_NATIVE) {
30967c478bd9Sstevel@tonic-gate 			if (Pread(P, &envpoff, sizeof (envpoff),
30977c478bd9Sstevel@tonic-gate 			    sym.st_value) != sizeof (envpoff))
30987c478bd9Sstevel@tonic-gate 				envpoff = psp->pr_envp;
30997c478bd9Sstevel@tonic-gate 		} else if (P->status.pr_dmodel == PR_MODEL_ILP32) {
31007c478bd9Sstevel@tonic-gate 			uint32_t envpoff32;
31017c478bd9Sstevel@tonic-gate 
31027c478bd9Sstevel@tonic-gate 			if (Pread(P, &envpoff32, sizeof (envpoff32),
31037c478bd9Sstevel@tonic-gate 			    sym.st_value) != sizeof (envpoff32))
31047c478bd9Sstevel@tonic-gate 				envpoff = psp->pr_envp;
31057c478bd9Sstevel@tonic-gate 			else
31067c478bd9Sstevel@tonic-gate 				envpoff = envpoff32;
31077c478bd9Sstevel@tonic-gate 		}
31087c478bd9Sstevel@tonic-gate 	}
31097c478bd9Sstevel@tonic-gate 
31107c478bd9Sstevel@tonic-gate 	buflen = 128;
31117c478bd9Sstevel@tonic-gate 	buf = malloc(buflen);
31127c478bd9Sstevel@tonic-gate 
31137c478bd9Sstevel@tonic-gate 	ret = 0;
31147c478bd9Sstevel@tonic-gate 	for (;;) {
31157c478bd9Sstevel@tonic-gate 		uintptr_t envoff;
31167c478bd9Sstevel@tonic-gate 
31177c478bd9Sstevel@tonic-gate 		if (nenv == NARG) {
31187c478bd9Sstevel@tonic-gate 			(void) memset(envp, 0, sizeof (envp));
31197c478bd9Sstevel@tonic-gate 			if (P->status.pr_dmodel == PR_MODEL_NATIVE) {
31207c478bd9Sstevel@tonic-gate 				if (Pread(P, envp,
31217c478bd9Sstevel@tonic-gate 				    sizeof (envp), envpoff) <= 0) {
31227c478bd9Sstevel@tonic-gate 					ret = -1;
31237c478bd9Sstevel@tonic-gate 					break;
31247c478bd9Sstevel@tonic-gate 				}
31257c478bd9Sstevel@tonic-gate 			} else if (P->status.pr_dmodel == PR_MODEL_ILP32) {
31267c478bd9Sstevel@tonic-gate 				uint32_t e32[NARG];
31277c478bd9Sstevel@tonic-gate 				int i;
31287c478bd9Sstevel@tonic-gate 
31297c478bd9Sstevel@tonic-gate 				(void) memset(e32, 0, sizeof (e32));
31307c478bd9Sstevel@tonic-gate 				if (Pread(P, e32, sizeof (e32), envpoff) <= 0) {
31317c478bd9Sstevel@tonic-gate 					ret = -1;
31327c478bd9Sstevel@tonic-gate 					break;
31337c478bd9Sstevel@tonic-gate 				}
31347c478bd9Sstevel@tonic-gate 				for (i = 0; i < NARG; i++)
31357c478bd9Sstevel@tonic-gate 					envp[i] = e32[i];
31367c478bd9Sstevel@tonic-gate 			}
31377c478bd9Sstevel@tonic-gate 			nenv = 0;
31387c478bd9Sstevel@tonic-gate 		}
31397c478bd9Sstevel@tonic-gate 
31407c478bd9Sstevel@tonic-gate 		if ((envoff = envp[nenv++]) == NULL)
31417c478bd9Sstevel@tonic-gate 			break;
31427c478bd9Sstevel@tonic-gate 
31437c478bd9Sstevel@tonic-gate 		/*
31447c478bd9Sstevel@tonic-gate 		 * Attempt to read the string from the process.
31457c478bd9Sstevel@tonic-gate 		 */
31467c478bd9Sstevel@tonic-gate again:
31477c478bd9Sstevel@tonic-gate 		ret = Pread_string(P, buf, buflen, envoff);
31487c478bd9Sstevel@tonic-gate 
31497c478bd9Sstevel@tonic-gate 		if (ret <= 0) {
31507c478bd9Sstevel@tonic-gate 			nameval = NULL;
31517c478bd9Sstevel@tonic-gate 		} else if (ret == buflen - 1) {
31527c478bd9Sstevel@tonic-gate 			free(buf);
31537c478bd9Sstevel@tonic-gate 			/*
31547c478bd9Sstevel@tonic-gate 			 * Bail if we have a corrupted environment
31557c478bd9Sstevel@tonic-gate 			 */
31567c478bd9Sstevel@tonic-gate 			if (buflen >= ARG_MAX)
31577c478bd9Sstevel@tonic-gate 				return (-1);
31587c478bd9Sstevel@tonic-gate 			buflen *= 2;
31597c478bd9Sstevel@tonic-gate 			buf = malloc(buflen);
31607c478bd9Sstevel@tonic-gate 			goto again;
31617c478bd9Sstevel@tonic-gate 		} else {
31627c478bd9Sstevel@tonic-gate 			nameval = buf;
31637c478bd9Sstevel@tonic-gate 		}
31647c478bd9Sstevel@tonic-gate 
31657c478bd9Sstevel@tonic-gate 		if ((ret = func(data, P, envoff, nameval)) != 0)
31667c478bd9Sstevel@tonic-gate 			break;
31677c478bd9Sstevel@tonic-gate 
31687c478bd9Sstevel@tonic-gate 		envpoff += (P->status.pr_dmodel == PR_MODEL_LP64)? 8 : 4;
31697c478bd9Sstevel@tonic-gate 	}
31707c478bd9Sstevel@tonic-gate 
31717c478bd9Sstevel@tonic-gate 	free(buf);
31727c478bd9Sstevel@tonic-gate 
31737c478bd9Sstevel@tonic-gate 	return (ret);
31747c478bd9Sstevel@tonic-gate }
3175