xref: /titanic_52/usr/src/lib/libproc/common/Psymtab.c (revision 4bfb3cb6ef3fb38c53dd286090e19e92ab8a45fb)
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*4bfb3cb6Srh87107  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
299acbbeafSnn35248 #include <assert.h>
307c478bd9Sstevel@tonic-gate #include <stdio.h>
317c478bd9Sstevel@tonic-gate #include <stdlib.h>
327c478bd9Sstevel@tonic-gate #include <stddef.h>
337c478bd9Sstevel@tonic-gate #include <unistd.h>
347c478bd9Sstevel@tonic-gate #include <ctype.h>
357c478bd9Sstevel@tonic-gate #include <fcntl.h>
367c478bd9Sstevel@tonic-gate #include <string.h>
377c478bd9Sstevel@tonic-gate #include <strings.h>
387c478bd9Sstevel@tonic-gate #include <memory.h>
397c478bd9Sstevel@tonic-gate #include <errno.h>
407c478bd9Sstevel@tonic-gate #include <dirent.h>
417c478bd9Sstevel@tonic-gate #include <signal.h>
427c478bd9Sstevel@tonic-gate #include <limits.h>
437c478bd9Sstevel@tonic-gate #include <libgen.h>
447c478bd9Sstevel@tonic-gate #include <zone.h>
457c478bd9Sstevel@tonic-gate #include <sys/types.h>
467c478bd9Sstevel@tonic-gate #include <sys/stat.h>
477c478bd9Sstevel@tonic-gate #include <sys/systeminfo.h>
487c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate #include "libproc.h"
517c478bd9Sstevel@tonic-gate #include "Pcontrol.h"
527c478bd9Sstevel@tonic-gate #include "Putil.h"
53d51e9074Sab196087 #include "Psymtab_machelf.h"
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate static file_info_t *build_map_symtab(struct ps_prochandle *, map_info_t *);
567c478bd9Sstevel@tonic-gate static map_info_t *exec_map(struct ps_prochandle *);
577c478bd9Sstevel@tonic-gate static map_info_t *object_to_map(struct ps_prochandle *, Lmid_t, const char *);
587c478bd9Sstevel@tonic-gate static map_info_t *object_name_to_map(struct ps_prochandle *,
597c478bd9Sstevel@tonic-gate 	Lmid_t, const char *);
607c478bd9Sstevel@tonic-gate static GElf_Sym *sym_by_name(sym_tbl_t *, const char *, GElf_Sym *, uint_t *);
6130da1432Sahl static int read_ehdr32(struct ps_prochandle *, Elf32_Ehdr *, uint_t *,
6230da1432Sahl     uintptr_t);
637c478bd9Sstevel@tonic-gate #ifdef _LP64
6430da1432Sahl static int read_ehdr64(struct ps_prochandle *, Elf64_Ehdr *, uint_t *,
6530da1432Sahl     uintptr_t);
667c478bd9Sstevel@tonic-gate #endif
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate #define	DATA_TYPES	\
697c478bd9Sstevel@tonic-gate 	((1 << STT_OBJECT) | (1 << STT_FUNC) | \
707c478bd9Sstevel@tonic-gate 	(1 << STT_COMMON) | (1 << STT_TLS))
717c478bd9Sstevel@tonic-gate #define	IS_DATA_TYPE(tp)	(((1 << (tp)) & DATA_TYPES) != 0)
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate #define	MA_RWX	(MA_READ | MA_WRITE | MA_EXEC)
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate typedef enum {
767c478bd9Sstevel@tonic-gate 	PRO_NATURAL,
777c478bd9Sstevel@tonic-gate 	PRO_BYADDR,
787c478bd9Sstevel@tonic-gate 	PRO_BYNAME
797c478bd9Sstevel@tonic-gate } pr_order_t;
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate static int
827c478bd9Sstevel@tonic-gate addr_cmp(const void *aa, const void *bb)
837c478bd9Sstevel@tonic-gate {
847c478bd9Sstevel@tonic-gate 	uintptr_t a = *((uintptr_t *)aa);
857c478bd9Sstevel@tonic-gate 	uintptr_t b = *((uintptr_t *)bb);
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate 	if (a > b)
887c478bd9Sstevel@tonic-gate 		return (1);
897c478bd9Sstevel@tonic-gate 	if (a < b)
907c478bd9Sstevel@tonic-gate 		return (-1);
917c478bd9Sstevel@tonic-gate 	return (0);
927c478bd9Sstevel@tonic-gate }
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate /*
9512e72dbbSrh87107  * This function creates a list of addresses for a load object's sections.
9612e72dbbSrh87107  * The list is in ascending address order and alternates start address
9712e72dbbSrh87107  * then end address for each section we're interested in. The function
9812e72dbbSrh87107  * returns a pointer to the list, which must be freed by the caller.
9912e72dbbSrh87107  */
10012e72dbbSrh87107 static uintptr_t *
10112e72dbbSrh87107 get_saddrs(struct ps_prochandle *P, uintptr_t ehdr_start, uint_t *n)
10212e72dbbSrh87107 {
10312e72dbbSrh87107 	uintptr_t a, addr, *addrs, last = 0;
10412e72dbbSrh87107 	uint_t i, naddrs = 0, unordered = 0;
10512e72dbbSrh87107 
10612e72dbbSrh87107 	if (P->status.pr_dmodel == PR_MODEL_ILP32) {
10712e72dbbSrh87107 		Elf32_Ehdr ehdr;
10812e72dbbSrh87107 		Elf32_Phdr phdr;
10912e72dbbSrh87107 		uint_t phnum;
11012e72dbbSrh87107 
11112e72dbbSrh87107 		if (read_ehdr32(P, &ehdr, &phnum, ehdr_start) != 0)
11212e72dbbSrh87107 			return (NULL);
11312e72dbbSrh87107 
11412e72dbbSrh87107 		addrs = malloc(sizeof (uintptr_t) * phnum * 2);
11512e72dbbSrh87107 		a = ehdr_start + ehdr.e_phoff;
11612e72dbbSrh87107 		for (i = 0; i < phnum; i++, a += ehdr.e_phentsize) {
11712e72dbbSrh87107 			if (Pread(P, &phdr, sizeof (phdr), a) !=
11812e72dbbSrh87107 			    sizeof (phdr)) {
11912e72dbbSrh87107 				free(addrs);
12012e72dbbSrh87107 				return (NULL);
12112e72dbbSrh87107 			}
12212e72dbbSrh87107 			if (phdr.p_type != PT_LOAD || phdr.p_memsz == 0)
12312e72dbbSrh87107 				continue;
12412e72dbbSrh87107 
12512e72dbbSrh87107 			addr = phdr.p_vaddr;
12612e72dbbSrh87107 			if (ehdr.e_type == ET_DYN)
12712e72dbbSrh87107 				addr += ehdr_start;
12812e72dbbSrh87107 			if (last > addr)
12912e72dbbSrh87107 				unordered = 1;
13012e72dbbSrh87107 			addrs[naddrs++] = addr;
13112e72dbbSrh87107 			addrs[naddrs++] = last = addr + phdr.p_memsz - 1;
13212e72dbbSrh87107 		}
13312e72dbbSrh87107 #ifdef _LP64
13412e72dbbSrh87107 	} else {
13512e72dbbSrh87107 		Elf64_Ehdr ehdr;
13612e72dbbSrh87107 		Elf64_Phdr phdr;
13712e72dbbSrh87107 		uint_t phnum;
13812e72dbbSrh87107 
13912e72dbbSrh87107 		if (read_ehdr64(P, &ehdr, &phnum, ehdr_start) != 0)
14012e72dbbSrh87107 			return (NULL);
14112e72dbbSrh87107 
14212e72dbbSrh87107 		addrs = malloc(sizeof (uintptr_t) * phnum * 2);
14312e72dbbSrh87107 		a = ehdr_start + ehdr.e_phoff;
14412e72dbbSrh87107 		for (i = 0; i < phnum; i++, a += ehdr.e_phentsize) {
14512e72dbbSrh87107 			if (Pread(P, &phdr, sizeof (phdr), a) !=
14612e72dbbSrh87107 			    sizeof (phdr)) {
14712e72dbbSrh87107 				free(addrs);
14812e72dbbSrh87107 				return (NULL);
14912e72dbbSrh87107 			}
15012e72dbbSrh87107 			if (phdr.p_type != PT_LOAD || phdr.p_memsz == 0)
15112e72dbbSrh87107 				continue;
15212e72dbbSrh87107 
15312e72dbbSrh87107 			addr = phdr.p_vaddr;
15412e72dbbSrh87107 			if (ehdr.e_type == ET_DYN)
15512e72dbbSrh87107 				addr += ehdr_start;
15612e72dbbSrh87107 			if (last > addr)
15712e72dbbSrh87107 				unordered = 1;
15812e72dbbSrh87107 			addrs[naddrs++] = addr;
15912e72dbbSrh87107 			addrs[naddrs++] = last = addr + phdr.p_memsz - 1;
16012e72dbbSrh87107 		}
16112e72dbbSrh87107 #endif
16212e72dbbSrh87107 	}
16312e72dbbSrh87107 
16412e72dbbSrh87107 	if (unordered)
16512e72dbbSrh87107 		qsort(addrs, naddrs, sizeof (uintptr_t), addr_cmp);
16612e72dbbSrh87107 
16712e72dbbSrh87107 	*n = naddrs;
16812e72dbbSrh87107 	return (addrs);
16912e72dbbSrh87107 }
17012e72dbbSrh87107 
17112e72dbbSrh87107 /*
1727c478bd9Sstevel@tonic-gate  * Allocation function for a new file_info_t
1737c478bd9Sstevel@tonic-gate  */
174d7755b5aSrh87107 file_info_t *
1757c478bd9Sstevel@tonic-gate file_info_new(struct ps_prochandle *P, map_info_t *mptr)
1767c478bd9Sstevel@tonic-gate {
1777c478bd9Sstevel@tonic-gate 	file_info_t *fptr;
1787c478bd9Sstevel@tonic-gate 	map_info_t *mp;
179d7755b5aSrh87107 	uintptr_t mstart, mend, sstart, send;
180d7755b5aSrh87107 	uint_t i;
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 	if ((fptr = calloc(1, sizeof (file_info_t))) == NULL)
1837c478bd9Sstevel@tonic-gate 		return (NULL);
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate 	list_link(fptr, &P->file_head);
1867c478bd9Sstevel@tonic-gate 	(void) strcpy(fptr->file_pname, mptr->map_pmap.pr_mapname);
1877c478bd9Sstevel@tonic-gate 	mptr->map_file = fptr;
1887c478bd9Sstevel@tonic-gate 	fptr->file_ref = 1;
1897c478bd9Sstevel@tonic-gate 	fptr->file_fd = -1;
1907c478bd9Sstevel@tonic-gate 	P->num_files++;
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 	/*
1937c478bd9Sstevel@tonic-gate 	 * To figure out which map_info_t instances correspond to the mappings
19412e72dbbSrh87107 	 * for this load object we try to obtain the start and end address
19512e72dbbSrh87107 	 * for each section of our in-memory ELF image. If successful, we
1967c478bd9Sstevel@tonic-gate 	 * walk down the list of addresses and the list of map_info_t
1977c478bd9Sstevel@tonic-gate 	 * instances in lock step to correctly find the mappings that
1987c478bd9Sstevel@tonic-gate 	 * correspond to this load object.
1997c478bd9Sstevel@tonic-gate 	 */
20012e72dbbSrh87107 	if ((fptr->file_saddrs = get_saddrs(P, mptr->map_pmap.pr_vaddr,
20112e72dbbSrh87107 	    &fptr->file_nsaddrs)) == NULL)
2027c478bd9Sstevel@tonic-gate 		return (fptr);
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 	mp = P->mappings;
205d7755b5aSrh87107 	i = 0;
206d7755b5aSrh87107 	while (mp < P->mappings + P->map_count && i < fptr->file_nsaddrs) {
207d7755b5aSrh87107 
208d7755b5aSrh87107 		/* Calculate the start and end of the mapping and section */
209d7755b5aSrh87107 		mstart = mp->map_pmap.pr_vaddr;
210d7755b5aSrh87107 		mend = mp->map_pmap.pr_vaddr + mp->map_pmap.pr_size;
211d7755b5aSrh87107 		sstart = fptr->file_saddrs[i];
212d7755b5aSrh87107 		send = fptr->file_saddrs[i + 1];
213d7755b5aSrh87107 
214d7755b5aSrh87107 		if (mend <= sstart) {
215d7755b5aSrh87107 			/* This mapping is below the current section */
216d7755b5aSrh87107 			mp++;
217d7755b5aSrh87107 		} else if (mstart >= send) {
218d7755b5aSrh87107 			/* This mapping is above the current section */
219d7755b5aSrh87107 			i += 2;
220d7755b5aSrh87107 		} else {
221d7755b5aSrh87107 			/* This mapping overlaps the current section */
222d7755b5aSrh87107 			if (mp->map_file == NULL) {
223d7755b5aSrh87107 				dprintf("file_info_new: associating "
224d7755b5aSrh87107 				    "segment at %p\n",
225d7755b5aSrh87107 				    (void *)mp->map_pmap.pr_vaddr);
2267c478bd9Sstevel@tonic-gate 				mp->map_file = fptr;
2277c478bd9Sstevel@tonic-gate 				fptr->file_ref++;
2287c478bd9Sstevel@tonic-gate 			} else {
229d7755b5aSrh87107 				dprintf("file_info_new: segment at %p "
230d7755b5aSrh87107 				    "already associated with %s\n",
231d7755b5aSrh87107 				    (void *)mp->map_pmap.pr_vaddr,
232d7755b5aSrh87107 				    (mp == mptr ? "this file" :
233d7755b5aSrh87107 				    mp->map_file->file_pname));
234d7755b5aSrh87107 			}
2357c478bd9Sstevel@tonic-gate 			mp++;
2367c478bd9Sstevel@tonic-gate 		}
2377c478bd9Sstevel@tonic-gate 	}
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate 	return (fptr);
2407c478bd9Sstevel@tonic-gate }
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate /*
2437c478bd9Sstevel@tonic-gate  * Deallocation function for a file_info_t
2447c478bd9Sstevel@tonic-gate  */
2457c478bd9Sstevel@tonic-gate static void
2467c478bd9Sstevel@tonic-gate file_info_free(struct ps_prochandle *P, file_info_t *fptr)
2477c478bd9Sstevel@tonic-gate {
2487c478bd9Sstevel@tonic-gate 	if (--fptr->file_ref == 0) {
2497c478bd9Sstevel@tonic-gate 		list_unlink(fptr);
2507c478bd9Sstevel@tonic-gate 		if (fptr->file_symtab.sym_elf) {
2517c478bd9Sstevel@tonic-gate 			(void) elf_end(fptr->file_symtab.sym_elf);
2527c478bd9Sstevel@tonic-gate 			free(fptr->file_symtab.sym_elfmem);
2537c478bd9Sstevel@tonic-gate 		}
2547c478bd9Sstevel@tonic-gate 		if (fptr->file_symtab.sym_byname)
2557c478bd9Sstevel@tonic-gate 			free(fptr->file_symtab.sym_byname);
2567c478bd9Sstevel@tonic-gate 		if (fptr->file_symtab.sym_byaddr)
2577c478bd9Sstevel@tonic-gate 			free(fptr->file_symtab.sym_byaddr);
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate 		if (fptr->file_dynsym.sym_elf) {
2607c478bd9Sstevel@tonic-gate 			(void) elf_end(fptr->file_dynsym.sym_elf);
2617c478bd9Sstevel@tonic-gate 			free(fptr->file_dynsym.sym_elfmem);
2627c478bd9Sstevel@tonic-gate 		}
2637c478bd9Sstevel@tonic-gate 		if (fptr->file_dynsym.sym_byname)
2647c478bd9Sstevel@tonic-gate 			free(fptr->file_dynsym.sym_byname);
2657c478bd9Sstevel@tonic-gate 		if (fptr->file_dynsym.sym_byaddr)
2667c478bd9Sstevel@tonic-gate 			free(fptr->file_dynsym.sym_byaddr);
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate 		if (fptr->file_lo)
2697c478bd9Sstevel@tonic-gate 			free(fptr->file_lo);
2707c478bd9Sstevel@tonic-gate 		if (fptr->file_lname)
2717c478bd9Sstevel@tonic-gate 			free(fptr->file_lname);
2727c478bd9Sstevel@tonic-gate 		if (fptr->file_elf)
2737c478bd9Sstevel@tonic-gate 			(void) elf_end(fptr->file_elf);
2747c478bd9Sstevel@tonic-gate 		if (fptr->file_elfmem != NULL)
2757c478bd9Sstevel@tonic-gate 			free(fptr->file_elfmem);
2767c478bd9Sstevel@tonic-gate 		if (fptr->file_fd >= 0)
2777c478bd9Sstevel@tonic-gate 			(void) close(fptr->file_fd);
2787c478bd9Sstevel@tonic-gate 		if (fptr->file_ctfp) {
2797c478bd9Sstevel@tonic-gate 			ctf_close(fptr->file_ctfp);
2807c478bd9Sstevel@tonic-gate 			free(fptr->file_ctf_buf);
2817c478bd9Sstevel@tonic-gate 		}
28212e72dbbSrh87107 		if (fptr->file_saddrs)
28312e72dbbSrh87107 			free(fptr->file_saddrs);
2847c478bd9Sstevel@tonic-gate 		free(fptr);
2857c478bd9Sstevel@tonic-gate 		P->num_files--;
2867c478bd9Sstevel@tonic-gate 	}
2877c478bd9Sstevel@tonic-gate }
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate /*
2907c478bd9Sstevel@tonic-gate  * Deallocation function for a map_info_t
2917c478bd9Sstevel@tonic-gate  */
2927c478bd9Sstevel@tonic-gate static void
2937c478bd9Sstevel@tonic-gate map_info_free(struct ps_prochandle *P, map_info_t *mptr)
2947c478bd9Sstevel@tonic-gate {
2957c478bd9Sstevel@tonic-gate 	file_info_t *fptr;
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate 	if ((fptr = mptr->map_file) != NULL) {
2987c478bd9Sstevel@tonic-gate 		if (fptr->file_map == mptr)
2997c478bd9Sstevel@tonic-gate 			fptr->file_map = NULL;
3007c478bd9Sstevel@tonic-gate 		file_info_free(P, fptr);
3017c478bd9Sstevel@tonic-gate 	}
3027c478bd9Sstevel@tonic-gate 	if (P->execname && mptr == P->map_exec) {
3037c478bd9Sstevel@tonic-gate 		free(P->execname);
3047c478bd9Sstevel@tonic-gate 		P->execname = NULL;
3057c478bd9Sstevel@tonic-gate 	}
3067c478bd9Sstevel@tonic-gate 	if (P->auxv && (mptr == P->map_exec || mptr == P->map_ldso)) {
3077c478bd9Sstevel@tonic-gate 		free(P->auxv);
3087c478bd9Sstevel@tonic-gate 		P->auxv = NULL;
3097c478bd9Sstevel@tonic-gate 		P->nauxv = 0;
3107c478bd9Sstevel@tonic-gate 	}
3117c478bd9Sstevel@tonic-gate 	if (mptr == P->map_exec)
3127c478bd9Sstevel@tonic-gate 		P->map_exec = NULL;
3137c478bd9Sstevel@tonic-gate 	if (mptr == P->map_ldso)
3147c478bd9Sstevel@tonic-gate 		P->map_ldso = NULL;
3157c478bd9Sstevel@tonic-gate }
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate /*
3187c478bd9Sstevel@tonic-gate  * Call-back function for librtld_db to iterate through all of its shared
3197c478bd9Sstevel@tonic-gate  * libraries.  We use this to get the load object names for the mappings.
3207c478bd9Sstevel@tonic-gate  */
3217c478bd9Sstevel@tonic-gate static int
3227c478bd9Sstevel@tonic-gate map_iter(const rd_loadobj_t *lop, void *cd)
3237c478bd9Sstevel@tonic-gate {
3247c478bd9Sstevel@tonic-gate 	char buf[PATH_MAX];
3257c478bd9Sstevel@tonic-gate 	struct ps_prochandle *P = cd;
3267c478bd9Sstevel@tonic-gate 	map_info_t *mptr;
3277c478bd9Sstevel@tonic-gate 	file_info_t *fptr;
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate 	dprintf("encountered rd object at %p\n", (void *)lop->rl_base);
3307c478bd9Sstevel@tonic-gate 
3319acbbeafSnn35248 	if ((mptr = Paddr2mptr(P, lop->rl_base)) == NULL) {
3329acbbeafSnn35248 		dprintf("map_iter: base address doesn't match any mapping\n");
3337c478bd9Sstevel@tonic-gate 		return (1); /* Base address does not match any mapping */
3349acbbeafSnn35248 	}
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate 	if ((fptr = mptr->map_file) == NULL &&
3379acbbeafSnn35248 	    (fptr = file_info_new(P, mptr)) == NULL) {
3389acbbeafSnn35248 		dprintf("map_iter: failed to allocate a new file_info_t\n");
3397c478bd9Sstevel@tonic-gate 		return (1); /* Failed to allocate a new file_info_t */
3409acbbeafSnn35248 	}
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate 	if ((fptr->file_lo == NULL) &&
3437c478bd9Sstevel@tonic-gate 	    (fptr->file_lo = malloc(sizeof (rd_loadobj_t))) == NULL) {
3449acbbeafSnn35248 		dprintf("map_iter: failed to allocate rd_loadobj_t\n");
3457c478bd9Sstevel@tonic-gate 		file_info_free(P, fptr);
3467c478bd9Sstevel@tonic-gate 		return (1); /* Failed to allocate rd_loadobj_t */
3477c478bd9Sstevel@tonic-gate 	}
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate 	fptr->file_map = mptr;
3507c478bd9Sstevel@tonic-gate 	*fptr->file_lo = *lop;
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate 	fptr->file_lo->rl_plt_base = fptr->file_plt_base;
3537c478bd9Sstevel@tonic-gate 	fptr->file_lo->rl_plt_size = fptr->file_plt_size;
3547c478bd9Sstevel@tonic-gate 
3557c478bd9Sstevel@tonic-gate 	if (fptr->file_lname) {
3567c478bd9Sstevel@tonic-gate 		free(fptr->file_lname);
3577c478bd9Sstevel@tonic-gate 		fptr->file_lname = NULL;
3587c478bd9Sstevel@tonic-gate 	}
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate 	if (Pread_string(P, buf, sizeof (buf), lop->rl_nameaddr) > 0) {
3617c478bd9Sstevel@tonic-gate 		if ((fptr->file_lname = strdup(buf)) != NULL)
3627c478bd9Sstevel@tonic-gate 			fptr->file_lbase = basename(fptr->file_lname);
3639acbbeafSnn35248 	} else {
3649acbbeafSnn35248 		dprintf("map_iter: failed to read string at %p\n",
3659acbbeafSnn35248 		    (void *)lop->rl_nameaddr);
3667c478bd9Sstevel@tonic-gate 	}
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate 	dprintf("loaded rd object %s lmid %lx\n",
3697c478bd9Sstevel@tonic-gate 	    fptr->file_lname ? fptr->file_lname : "<NULL>", lop->rl_lmident);
3707c478bd9Sstevel@tonic-gate 	return (1);
3717c478bd9Sstevel@tonic-gate }
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate static void
3747c478bd9Sstevel@tonic-gate map_set(struct ps_prochandle *P, map_info_t *mptr, const char *lname)
3757c478bd9Sstevel@tonic-gate {
3767c478bd9Sstevel@tonic-gate 	file_info_t *fptr;
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate 	if ((fptr = mptr->map_file) == NULL &&
3797c478bd9Sstevel@tonic-gate 	    (fptr = file_info_new(P, mptr)) == NULL)
3807c478bd9Sstevel@tonic-gate 		return; /* Failed to allocate a new file_info_t */
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate 	fptr->file_map = mptr;
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate 	if ((fptr->file_lo == NULL) &&
3857c478bd9Sstevel@tonic-gate 	    (fptr->file_lo = malloc(sizeof (rd_loadobj_t))) == NULL) {
3867c478bd9Sstevel@tonic-gate 		file_info_free(P, fptr);
3877c478bd9Sstevel@tonic-gate 		return; /* Failed to allocate rd_loadobj_t */
3887c478bd9Sstevel@tonic-gate 	}
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate 	(void) memset(fptr->file_lo, 0, sizeof (rd_loadobj_t));
3917c478bd9Sstevel@tonic-gate 	fptr->file_lo->rl_base = mptr->map_pmap.pr_vaddr;
3927c478bd9Sstevel@tonic-gate 	fptr->file_lo->rl_bend =
3937c478bd9Sstevel@tonic-gate 	    mptr->map_pmap.pr_vaddr + mptr->map_pmap.pr_size;
3947c478bd9Sstevel@tonic-gate 
3957c478bd9Sstevel@tonic-gate 	fptr->file_lo->rl_plt_base = fptr->file_plt_base;
3967c478bd9Sstevel@tonic-gate 	fptr->file_lo->rl_plt_size = fptr->file_plt_size;
3977c478bd9Sstevel@tonic-gate 
3989acbbeafSnn35248 	if (fptr->file_lname == NULL &&
3999acbbeafSnn35248 	    (fptr->file_lname = strdup(lname)) != NULL)
4007c478bd9Sstevel@tonic-gate 		fptr->file_lbase = basename(fptr->file_lname);
4017c478bd9Sstevel@tonic-gate }
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate static void
4047c478bd9Sstevel@tonic-gate load_static_maps(struct ps_prochandle *P)
4057c478bd9Sstevel@tonic-gate {
4067c478bd9Sstevel@tonic-gate 	map_info_t *mptr;
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate 	/*
4097c478bd9Sstevel@tonic-gate 	 * Construct the map for the a.out.
4107c478bd9Sstevel@tonic-gate 	 */
4117c478bd9Sstevel@tonic-gate 	if ((mptr = object_name_to_map(P, PR_LMID_EVERY, PR_OBJ_EXEC)) != NULL)
4127c478bd9Sstevel@tonic-gate 		map_set(P, mptr, "a.out");
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate 	/*
4157c478bd9Sstevel@tonic-gate 	 * If the dynamic linker exists for this process,
4167c478bd9Sstevel@tonic-gate 	 * construct the map for it.
4177c478bd9Sstevel@tonic-gate 	 */
4187c478bd9Sstevel@tonic-gate 	if (Pgetauxval(P, AT_BASE) != -1L &&
4197c478bd9Sstevel@tonic-gate 	    (mptr = object_name_to_map(P, PR_LMID_EVERY, PR_OBJ_LDSO)) != NULL)
4207c478bd9Sstevel@tonic-gate 		map_set(P, mptr, "ld.so.1");
4217c478bd9Sstevel@tonic-gate }
4227c478bd9Sstevel@tonic-gate 
4237c478bd9Sstevel@tonic-gate /*
4247c478bd9Sstevel@tonic-gate  * Go through all the address space mappings, validating or updating
4257c478bd9Sstevel@tonic-gate  * the information already gathered, or gathering new information.
4267c478bd9Sstevel@tonic-gate  *
4277c478bd9Sstevel@tonic-gate  * This function is only called when we suspect that the mappings have changed
4287c478bd9Sstevel@tonic-gate  * because this is the first time we're calling it or because of rtld activity.
4297c478bd9Sstevel@tonic-gate  */
4307c478bd9Sstevel@tonic-gate void
4317c478bd9Sstevel@tonic-gate Pupdate_maps(struct ps_prochandle *P)
4327c478bd9Sstevel@tonic-gate {
4339acbbeafSnn35248 	char mapfile[PATH_MAX];
4347c478bd9Sstevel@tonic-gate 	int mapfd;
4357c478bd9Sstevel@tonic-gate 	struct stat statb;
4367c478bd9Sstevel@tonic-gate 	prmap_t *Pmap = NULL;
4377c478bd9Sstevel@tonic-gate 	prmap_t *pmap;
4387c478bd9Sstevel@tonic-gate 	ssize_t nmap;
4397c478bd9Sstevel@tonic-gate 	int i;
4407c478bd9Sstevel@tonic-gate 	uint_t oldmapcount;
4417c478bd9Sstevel@tonic-gate 	map_info_t *newmap, *newp;
4427c478bd9Sstevel@tonic-gate 	map_info_t *mptr;
4437c478bd9Sstevel@tonic-gate 
444a1b5e537Sbmc 	if (P->info_valid || P->state == PS_UNDEAD)
4457c478bd9Sstevel@tonic-gate 		return;
4467c478bd9Sstevel@tonic-gate 
4477c478bd9Sstevel@tonic-gate 	Preadauxvec(P);
4487c478bd9Sstevel@tonic-gate 
4499acbbeafSnn35248 	(void) snprintf(mapfile, sizeof (mapfile), "%s/%d/map",
4509acbbeafSnn35248 	    procfs_path, (int)P->pid);
4517c478bd9Sstevel@tonic-gate 	if ((mapfd = open(mapfile, O_RDONLY)) < 0 ||
4527c478bd9Sstevel@tonic-gate 	    fstat(mapfd, &statb) != 0 ||
4537c478bd9Sstevel@tonic-gate 	    statb.st_size < sizeof (prmap_t) ||
4547c478bd9Sstevel@tonic-gate 	    (Pmap = malloc(statb.st_size)) == NULL ||
4557c478bd9Sstevel@tonic-gate 	    (nmap = pread(mapfd, Pmap, statb.st_size, 0L)) <= 0 ||
4567c478bd9Sstevel@tonic-gate 	    (nmap /= sizeof (prmap_t)) == 0) {
4577c478bd9Sstevel@tonic-gate 		if (Pmap != NULL)
4587c478bd9Sstevel@tonic-gate 			free(Pmap);
4597c478bd9Sstevel@tonic-gate 		if (mapfd >= 0)
4607c478bd9Sstevel@tonic-gate 			(void) close(mapfd);
4617c478bd9Sstevel@tonic-gate 		Preset_maps(P);	/* utter failure; destroy tables */
4627c478bd9Sstevel@tonic-gate 		return;
4637c478bd9Sstevel@tonic-gate 	}
4647c478bd9Sstevel@tonic-gate 	(void) close(mapfd);
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate 	if ((newmap = calloc(1, nmap * sizeof (map_info_t))) == NULL)
4677c478bd9Sstevel@tonic-gate 		return;
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate 	/*
4707c478bd9Sstevel@tonic-gate 	 * We try to merge any file information we may have for existing
4717c478bd9Sstevel@tonic-gate 	 * mappings, to avoid having to rebuild the file info.
4727c478bd9Sstevel@tonic-gate 	 */
4737c478bd9Sstevel@tonic-gate 	mptr = P->mappings;
4747c478bd9Sstevel@tonic-gate 	pmap = Pmap;
4757c478bd9Sstevel@tonic-gate 	newp = newmap;
4767c478bd9Sstevel@tonic-gate 	oldmapcount = P->map_count;
4777c478bd9Sstevel@tonic-gate 	for (i = 0; i < nmap; i++, pmap++, newp++) {
4787c478bd9Sstevel@tonic-gate 
4797c478bd9Sstevel@tonic-gate 		if (oldmapcount == 0) {
4807c478bd9Sstevel@tonic-gate 			/*
4817c478bd9Sstevel@tonic-gate 			 * We've exhausted all the old mappings.  Every new
4827c478bd9Sstevel@tonic-gate 			 * mapping should be added.
4837c478bd9Sstevel@tonic-gate 			 */
4847c478bd9Sstevel@tonic-gate 			newp->map_pmap = *pmap;
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate 		} else if (pmap->pr_vaddr == mptr->map_pmap.pr_vaddr &&
4877c478bd9Sstevel@tonic-gate 		    pmap->pr_size == mptr->map_pmap.pr_size &&
4887c478bd9Sstevel@tonic-gate 		    pmap->pr_offset == mptr->map_pmap.pr_offset &&
4897c478bd9Sstevel@tonic-gate 		    (pmap->pr_mflags & ~(MA_BREAK | MA_STACK)) ==
4907c478bd9Sstevel@tonic-gate 		    (mptr->map_pmap.pr_mflags & ~(MA_BREAK | MA_STACK)) &&
4917c478bd9Sstevel@tonic-gate 		    pmap->pr_pagesize == mptr->map_pmap.pr_pagesize &&
4927c478bd9Sstevel@tonic-gate 		    pmap->pr_shmid == mptr->map_pmap.pr_shmid &&
4937c478bd9Sstevel@tonic-gate 		    strcmp(pmap->pr_mapname, mptr->map_pmap.pr_mapname) == 0) {
4947c478bd9Sstevel@tonic-gate 
4957c478bd9Sstevel@tonic-gate 			/*
4967c478bd9Sstevel@tonic-gate 			 * This mapping matches exactly.  Copy over the old
4977c478bd9Sstevel@tonic-gate 			 * mapping, taking care to get the latest flags.
4987c478bd9Sstevel@tonic-gate 			 * Make sure the associated file_info_t is updated
4997c478bd9Sstevel@tonic-gate 			 * appropriately.
5007c478bd9Sstevel@tonic-gate 			 */
5017c478bd9Sstevel@tonic-gate 			*newp = *mptr;
5027c478bd9Sstevel@tonic-gate 			if (P->map_exec == mptr)
5037c478bd9Sstevel@tonic-gate 				P->map_exec = newp;
5047c478bd9Sstevel@tonic-gate 			if (P->map_ldso == mptr)
5057c478bd9Sstevel@tonic-gate 				P->map_ldso = newp;
5067c478bd9Sstevel@tonic-gate 			newp->map_pmap.pr_mflags = pmap->pr_mflags;
5077c478bd9Sstevel@tonic-gate 			if (mptr->map_file != NULL &&
5087c478bd9Sstevel@tonic-gate 			    mptr->map_file->file_map == mptr)
5097c478bd9Sstevel@tonic-gate 				mptr->map_file->file_map = newp;
5107c478bd9Sstevel@tonic-gate 			oldmapcount--;
5117c478bd9Sstevel@tonic-gate 			mptr++;
5127c478bd9Sstevel@tonic-gate 
5137c478bd9Sstevel@tonic-gate 		} else if (pmap->pr_vaddr + pmap->pr_size >
5147c478bd9Sstevel@tonic-gate 		    mptr->map_pmap.pr_vaddr) {
5157c478bd9Sstevel@tonic-gate 
5167c478bd9Sstevel@tonic-gate 			/*
5177c478bd9Sstevel@tonic-gate 			 * The old mapping doesn't exist any more, remove it
5187c478bd9Sstevel@tonic-gate 			 * from the list.
5197c478bd9Sstevel@tonic-gate 			 */
5207c478bd9Sstevel@tonic-gate 			map_info_free(P, mptr);
5217c478bd9Sstevel@tonic-gate 			oldmapcount--;
5227c478bd9Sstevel@tonic-gate 			i--;
5237c478bd9Sstevel@tonic-gate 			newp--;
5247c478bd9Sstevel@tonic-gate 			pmap--;
5257c478bd9Sstevel@tonic-gate 			mptr++;
5267c478bd9Sstevel@tonic-gate 
5277c478bd9Sstevel@tonic-gate 		} else {
5287c478bd9Sstevel@tonic-gate 
5297c478bd9Sstevel@tonic-gate 			/*
5307c478bd9Sstevel@tonic-gate 			 * This is a new mapping, add it directly.
5317c478bd9Sstevel@tonic-gate 			 */
5327c478bd9Sstevel@tonic-gate 			newp->map_pmap = *pmap;
5337c478bd9Sstevel@tonic-gate 		}
5347c478bd9Sstevel@tonic-gate 	}
5357c478bd9Sstevel@tonic-gate 
5367c478bd9Sstevel@tonic-gate 	/*
5377c478bd9Sstevel@tonic-gate 	 * Free any old maps
5387c478bd9Sstevel@tonic-gate 	 */
5397c478bd9Sstevel@tonic-gate 	while (oldmapcount) {
5407c478bd9Sstevel@tonic-gate 		map_info_free(P, mptr);
5417c478bd9Sstevel@tonic-gate 		oldmapcount--;
5427c478bd9Sstevel@tonic-gate 		mptr++;
5437c478bd9Sstevel@tonic-gate 	}
5447c478bd9Sstevel@tonic-gate 
5457c478bd9Sstevel@tonic-gate 	free(Pmap);
5467c478bd9Sstevel@tonic-gate 	if (P->mappings != NULL)
5477c478bd9Sstevel@tonic-gate 		free(P->mappings);
5487c478bd9Sstevel@tonic-gate 	P->mappings = newmap;
5497c478bd9Sstevel@tonic-gate 	P->map_count = P->map_alloc = nmap;
5507c478bd9Sstevel@tonic-gate 	P->info_valid = 1;
5517c478bd9Sstevel@tonic-gate 
5527c478bd9Sstevel@tonic-gate 	/*
5537c478bd9Sstevel@tonic-gate 	 * Consult librtld_db to get the load object
5547c478bd9Sstevel@tonic-gate 	 * names for all of the shared libraries.
5557c478bd9Sstevel@tonic-gate 	 */
5567c478bd9Sstevel@tonic-gate 	if (P->rap != NULL)
5577c478bd9Sstevel@tonic-gate 		(void) rd_loadobj_iter(P->rap, map_iter, P);
5587c478bd9Sstevel@tonic-gate }
5597c478bd9Sstevel@tonic-gate 
5607c478bd9Sstevel@tonic-gate /*
5617c478bd9Sstevel@tonic-gate  * Update all of the mappings and rtld_db as if by Pupdate_maps(), and then
5627c478bd9Sstevel@tonic-gate  * forcibly cache all of the symbol tables associated with all object files.
5637c478bd9Sstevel@tonic-gate  */
5647c478bd9Sstevel@tonic-gate void
5657c478bd9Sstevel@tonic-gate Pupdate_syms(struct ps_prochandle *P)
5667c478bd9Sstevel@tonic-gate {
567*4bfb3cb6Srh87107 	file_info_t *fptr;
5687c478bd9Sstevel@tonic-gate 	int i;
5697c478bd9Sstevel@tonic-gate 
5707c478bd9Sstevel@tonic-gate 	Pupdate_maps(P);
5717c478bd9Sstevel@tonic-gate 
572*4bfb3cb6Srh87107 	for (i = 0, fptr = list_next(&P->file_head); i < P->num_files;
573*4bfb3cb6Srh87107 	    i++, fptr = list_next(fptr)) {
5747c478bd9Sstevel@tonic-gate 		Pbuild_file_symtab(P, fptr);
5757c478bd9Sstevel@tonic-gate 		(void) Pbuild_file_ctf(P, fptr);
5767c478bd9Sstevel@tonic-gate 	}
5777c478bd9Sstevel@tonic-gate }
5787c478bd9Sstevel@tonic-gate 
5797c478bd9Sstevel@tonic-gate /*
5807c478bd9Sstevel@tonic-gate  * Return the librtld_db agent handle for the victim process.
5817c478bd9Sstevel@tonic-gate  * The handle will become invalid at the next successful exec() and the
5827c478bd9Sstevel@tonic-gate  * client (caller of proc_rd_agent()) must not use it beyond that point.
5837c478bd9Sstevel@tonic-gate  * If the process is already dead, we've already tried our best to
5847c478bd9Sstevel@tonic-gate  * create the agent during core file initialization.
5857c478bd9Sstevel@tonic-gate  */
5867c478bd9Sstevel@tonic-gate rd_agent_t *
5877c478bd9Sstevel@tonic-gate Prd_agent(struct ps_prochandle *P)
5887c478bd9Sstevel@tonic-gate {
5897c478bd9Sstevel@tonic-gate 	if (P->rap == NULL && P->state != PS_DEAD && P->state != PS_IDLE) {
5907c478bd9Sstevel@tonic-gate 		Pupdate_maps(P);
5917c478bd9Sstevel@tonic-gate 		if (P->num_files == 0)
5927c478bd9Sstevel@tonic-gate 			load_static_maps(P);
5937c478bd9Sstevel@tonic-gate 		rd_log(_libproc_debug);
5947c478bd9Sstevel@tonic-gate 		if ((P->rap = rd_new(P)) != NULL)
5957c478bd9Sstevel@tonic-gate 			(void) rd_loadobj_iter(P->rap, map_iter, P);
5967c478bd9Sstevel@tonic-gate 	}
5977c478bd9Sstevel@tonic-gate 	return (P->rap);
5987c478bd9Sstevel@tonic-gate }
5997c478bd9Sstevel@tonic-gate 
6007c478bd9Sstevel@tonic-gate /*
6017c478bd9Sstevel@tonic-gate  * Return the prmap_t structure containing 'addr', but only if it
6027c478bd9Sstevel@tonic-gate  * is in the dynamic linker's link map and is the text section.
6037c478bd9Sstevel@tonic-gate  */
6047c478bd9Sstevel@tonic-gate const prmap_t *
6057c478bd9Sstevel@tonic-gate Paddr_to_text_map(struct ps_prochandle *P, uintptr_t addr)
6067c478bd9Sstevel@tonic-gate {
6077c478bd9Sstevel@tonic-gate 	map_info_t *mptr;
6087c478bd9Sstevel@tonic-gate 
6097c478bd9Sstevel@tonic-gate 	if (!P->info_valid)
6107c478bd9Sstevel@tonic-gate 		Pupdate_maps(P);
6117c478bd9Sstevel@tonic-gate 
6127c478bd9Sstevel@tonic-gate 	if ((mptr = Paddr2mptr(P, addr)) != NULL) {
6137c478bd9Sstevel@tonic-gate 		file_info_t *fptr = build_map_symtab(P, mptr);
6147c478bd9Sstevel@tonic-gate 		const prmap_t *pmp = &mptr->map_pmap;
6157c478bd9Sstevel@tonic-gate 
616d7755b5aSrh87107 		/*
617d7755b5aSrh87107 		 * Assume that if rl_data_base is NULL, it means that no
618d7755b5aSrh87107 		 * data section was found for this load object, and that
619d7755b5aSrh87107 		 * a section must be text. Otherwise, a section will be
620d7755b5aSrh87107 		 * text unless it ends above the start of the data
621d7755b5aSrh87107 		 * section.
622d7755b5aSrh87107 		 */
6237c478bd9Sstevel@tonic-gate 		if (fptr != NULL && fptr->file_lo != NULL &&
624d7755b5aSrh87107 		    (fptr->file_lo->rl_data_base == NULL ||
625d7755b5aSrh87107 		    pmp->pr_vaddr + pmp->pr_size <
626d7755b5aSrh87107 		    fptr->file_lo->rl_data_base))
6277c478bd9Sstevel@tonic-gate 			return (pmp);
6287c478bd9Sstevel@tonic-gate 	}
6297c478bd9Sstevel@tonic-gate 
6307c478bd9Sstevel@tonic-gate 	return (NULL);
6317c478bd9Sstevel@tonic-gate }
6327c478bd9Sstevel@tonic-gate 
6337c478bd9Sstevel@tonic-gate /*
6347c478bd9Sstevel@tonic-gate  * Return the prmap_t structure containing 'addr' (no restrictions on
6357c478bd9Sstevel@tonic-gate  * the type of mapping).
6367c478bd9Sstevel@tonic-gate  */
6377c478bd9Sstevel@tonic-gate const prmap_t *
6387c478bd9Sstevel@tonic-gate Paddr_to_map(struct ps_prochandle *P, uintptr_t addr)
6397c478bd9Sstevel@tonic-gate {
6407c478bd9Sstevel@tonic-gate 	map_info_t *mptr;
6417c478bd9Sstevel@tonic-gate 
6427c478bd9Sstevel@tonic-gate 	if (!P->info_valid)
6437c478bd9Sstevel@tonic-gate 		Pupdate_maps(P);
6447c478bd9Sstevel@tonic-gate 
6457c478bd9Sstevel@tonic-gate 	if ((mptr = Paddr2mptr(P, addr)) != NULL)
6467c478bd9Sstevel@tonic-gate 		return (&mptr->map_pmap);
6477c478bd9Sstevel@tonic-gate 
6487c478bd9Sstevel@tonic-gate 	return (NULL);
6497c478bd9Sstevel@tonic-gate }
6507c478bd9Sstevel@tonic-gate 
6517c478bd9Sstevel@tonic-gate /*
6527c478bd9Sstevel@tonic-gate  * Convert a full or partial load object name to the prmap_t for its
6537c478bd9Sstevel@tonic-gate  * corresponding primary text mapping.
6547c478bd9Sstevel@tonic-gate  */
6557c478bd9Sstevel@tonic-gate const prmap_t *
6567c478bd9Sstevel@tonic-gate Plmid_to_map(struct ps_prochandle *P, Lmid_t lmid, const char *name)
6577c478bd9Sstevel@tonic-gate {
6587c478bd9Sstevel@tonic-gate 	map_info_t *mptr;
6597c478bd9Sstevel@tonic-gate 
6607c478bd9Sstevel@tonic-gate 	if (name == PR_OBJ_EVERY)
6617c478bd9Sstevel@tonic-gate 		return (NULL); /* A reasonable mistake */
6627c478bd9Sstevel@tonic-gate 
6637c478bd9Sstevel@tonic-gate 	if ((mptr = object_name_to_map(P, lmid, name)) != NULL)
6647c478bd9Sstevel@tonic-gate 		return (&mptr->map_pmap);
6657c478bd9Sstevel@tonic-gate 
6667c478bd9Sstevel@tonic-gate 	return (NULL);
6677c478bd9Sstevel@tonic-gate }
6687c478bd9Sstevel@tonic-gate 
6697c478bd9Sstevel@tonic-gate const prmap_t *
6707c478bd9Sstevel@tonic-gate Pname_to_map(struct ps_prochandle *P, const char *name)
6717c478bd9Sstevel@tonic-gate {
6727c478bd9Sstevel@tonic-gate 	return (Plmid_to_map(P, PR_LMID_EVERY, name));
6737c478bd9Sstevel@tonic-gate }
6747c478bd9Sstevel@tonic-gate 
6757c478bd9Sstevel@tonic-gate const rd_loadobj_t *
6767c478bd9Sstevel@tonic-gate Paddr_to_loadobj(struct ps_prochandle *P, uintptr_t addr)
6777c478bd9Sstevel@tonic-gate {
6787c478bd9Sstevel@tonic-gate 	map_info_t *mptr;
6797c478bd9Sstevel@tonic-gate 
6807c478bd9Sstevel@tonic-gate 	if (!P->info_valid)
6817c478bd9Sstevel@tonic-gate 		Pupdate_maps(P);
6827c478bd9Sstevel@tonic-gate 
6837c478bd9Sstevel@tonic-gate 	if ((mptr = Paddr2mptr(P, addr)) == NULL)
6847c478bd9Sstevel@tonic-gate 		return (NULL);
6857c478bd9Sstevel@tonic-gate 
6867c478bd9Sstevel@tonic-gate 	/*
6877c478bd9Sstevel@tonic-gate 	 * By building the symbol table, we implicitly bring the PLT
6887c478bd9Sstevel@tonic-gate 	 * information up to date in the load object.
6897c478bd9Sstevel@tonic-gate 	 */
6907c478bd9Sstevel@tonic-gate 	(void) build_map_symtab(P, mptr);
6917c478bd9Sstevel@tonic-gate 
6927c478bd9Sstevel@tonic-gate 	return (mptr->map_file->file_lo);
6937c478bd9Sstevel@tonic-gate }
6947c478bd9Sstevel@tonic-gate 
6957c478bd9Sstevel@tonic-gate const rd_loadobj_t *
6967c478bd9Sstevel@tonic-gate Plmid_to_loadobj(struct ps_prochandle *P, Lmid_t lmid, const char *name)
6977c478bd9Sstevel@tonic-gate {
6987c478bd9Sstevel@tonic-gate 	map_info_t *mptr;
6997c478bd9Sstevel@tonic-gate 
7007c478bd9Sstevel@tonic-gate 	if (name == PR_OBJ_EVERY)
7017c478bd9Sstevel@tonic-gate 		return (NULL);
7027c478bd9Sstevel@tonic-gate 
7037c478bd9Sstevel@tonic-gate 	if ((mptr = object_name_to_map(P, lmid, name)) == NULL)
7047c478bd9Sstevel@tonic-gate 		return (NULL);
7057c478bd9Sstevel@tonic-gate 
7067c478bd9Sstevel@tonic-gate 	/*
7077c478bd9Sstevel@tonic-gate 	 * By building the symbol table, we implicitly bring the PLT
7087c478bd9Sstevel@tonic-gate 	 * information up to date in the load object.
7097c478bd9Sstevel@tonic-gate 	 */
7107c478bd9Sstevel@tonic-gate 	(void) build_map_symtab(P, mptr);
7117c478bd9Sstevel@tonic-gate 
7127c478bd9Sstevel@tonic-gate 	return (mptr->map_file->file_lo);
7137c478bd9Sstevel@tonic-gate }
7147c478bd9Sstevel@tonic-gate 
7157c478bd9Sstevel@tonic-gate const rd_loadobj_t *
7167c478bd9Sstevel@tonic-gate Pname_to_loadobj(struct ps_prochandle *P, const char *name)
7177c478bd9Sstevel@tonic-gate {
7187c478bd9Sstevel@tonic-gate 	return (Plmid_to_loadobj(P, PR_LMID_EVERY, name));
7197c478bd9Sstevel@tonic-gate }
7207c478bd9Sstevel@tonic-gate 
7217c478bd9Sstevel@tonic-gate ctf_file_t *
7227c478bd9Sstevel@tonic-gate Pbuild_file_ctf(struct ps_prochandle *P, file_info_t *fptr)
7237c478bd9Sstevel@tonic-gate {
7247c478bd9Sstevel@tonic-gate 	ctf_sect_t ctdata, symtab, strtab;
7257c478bd9Sstevel@tonic-gate 	sym_tbl_t *symp;
7267c478bd9Sstevel@tonic-gate 	int err;
7277c478bd9Sstevel@tonic-gate 
7287c478bd9Sstevel@tonic-gate 	if (fptr->file_ctfp != NULL)
7297c478bd9Sstevel@tonic-gate 		return (fptr->file_ctfp);
7307c478bd9Sstevel@tonic-gate 
7317c478bd9Sstevel@tonic-gate 	Pbuild_file_symtab(P, fptr);
7327c478bd9Sstevel@tonic-gate 
7337c478bd9Sstevel@tonic-gate 	if (fptr->file_ctf_size == 0)
7347c478bd9Sstevel@tonic-gate 		return (NULL);
7357c478bd9Sstevel@tonic-gate 
7367c478bd9Sstevel@tonic-gate 	symp = fptr->file_ctf_dyn ? &fptr->file_dynsym : &fptr->file_symtab;
737d51e9074Sab196087 	if (symp->sym_data_pri == NULL)
7387c478bd9Sstevel@tonic-gate 		return (NULL);
7397c478bd9Sstevel@tonic-gate 
7407c478bd9Sstevel@tonic-gate 	/*
7417c478bd9Sstevel@tonic-gate 	 * The buffer may alread be allocated if this is a core file that
7427c478bd9Sstevel@tonic-gate 	 * contained CTF data for this file.
7437c478bd9Sstevel@tonic-gate 	 */
7447c478bd9Sstevel@tonic-gate 	if (fptr->file_ctf_buf == NULL) {
7457c478bd9Sstevel@tonic-gate 		fptr->file_ctf_buf = malloc(fptr->file_ctf_size);
7467c478bd9Sstevel@tonic-gate 		if (fptr->file_ctf_buf == NULL) {
7477c478bd9Sstevel@tonic-gate 			dprintf("failed to allocate ctf buffer\n");
7487c478bd9Sstevel@tonic-gate 			return (NULL);
7497c478bd9Sstevel@tonic-gate 		}
7507c478bd9Sstevel@tonic-gate 
7517c478bd9Sstevel@tonic-gate 		if (pread(fptr->file_fd, fptr->file_ctf_buf,
7527c478bd9Sstevel@tonic-gate 		    fptr->file_ctf_size, fptr->file_ctf_off) !=
7537c478bd9Sstevel@tonic-gate 		    fptr->file_ctf_size) {
7547c478bd9Sstevel@tonic-gate 			free(fptr->file_ctf_buf);
7557c478bd9Sstevel@tonic-gate 			fptr->file_ctf_buf = NULL;
7567c478bd9Sstevel@tonic-gate 			dprintf("failed to read ctf data\n");
7577c478bd9Sstevel@tonic-gate 			return (NULL);
7587c478bd9Sstevel@tonic-gate 		}
7597c478bd9Sstevel@tonic-gate 	}
7607c478bd9Sstevel@tonic-gate 
7617c478bd9Sstevel@tonic-gate 	ctdata.cts_name = ".SUNW_ctf";
7627c478bd9Sstevel@tonic-gate 	ctdata.cts_type = SHT_PROGBITS;
7637c478bd9Sstevel@tonic-gate 	ctdata.cts_flags = 0;
7647c478bd9Sstevel@tonic-gate 	ctdata.cts_data = fptr->file_ctf_buf;
7657c478bd9Sstevel@tonic-gate 	ctdata.cts_size = fptr->file_ctf_size;
7667c478bd9Sstevel@tonic-gate 	ctdata.cts_entsize = 1;
7677c478bd9Sstevel@tonic-gate 	ctdata.cts_offset = 0;
7687c478bd9Sstevel@tonic-gate 
7697c478bd9Sstevel@tonic-gate 	symtab.cts_name = fptr->file_ctf_dyn ? ".dynsym" : ".symtab";
770d51e9074Sab196087 	symtab.cts_type = symp->sym_hdr_pri.sh_type;
771d51e9074Sab196087 	symtab.cts_flags = symp->sym_hdr_pri.sh_flags;
772d51e9074Sab196087 	symtab.cts_data = symp->sym_data_pri->d_buf;
773d51e9074Sab196087 	symtab.cts_size = symp->sym_hdr_pri.sh_size;
774d51e9074Sab196087 	symtab.cts_entsize = symp->sym_hdr_pri.sh_entsize;
775d51e9074Sab196087 	symtab.cts_offset = symp->sym_hdr_pri.sh_offset;
7767c478bd9Sstevel@tonic-gate 
7777c478bd9Sstevel@tonic-gate 	strtab.cts_name = fptr->file_ctf_dyn ? ".dynstr" : ".strtab";
7787c478bd9Sstevel@tonic-gate 	strtab.cts_type = symp->sym_strhdr.sh_type;
7797c478bd9Sstevel@tonic-gate 	strtab.cts_flags = symp->sym_strhdr.sh_flags;
7807c478bd9Sstevel@tonic-gate 	strtab.cts_data = symp->sym_strs;
7817c478bd9Sstevel@tonic-gate 	strtab.cts_size = symp->sym_strhdr.sh_size;
7827c478bd9Sstevel@tonic-gate 	strtab.cts_entsize = symp->sym_strhdr.sh_entsize;
7837c478bd9Sstevel@tonic-gate 	strtab.cts_offset = symp->sym_strhdr.sh_offset;
7847c478bd9Sstevel@tonic-gate 
7857c478bd9Sstevel@tonic-gate 	fptr->file_ctfp = ctf_bufopen(&ctdata, &symtab, &strtab, &err);
7867c478bd9Sstevel@tonic-gate 	if (fptr->file_ctfp == NULL) {
7877c478bd9Sstevel@tonic-gate 		free(fptr->file_ctf_buf);
7887c478bd9Sstevel@tonic-gate 		fptr->file_ctf_buf = NULL;
7897c478bd9Sstevel@tonic-gate 		return (NULL);
7907c478bd9Sstevel@tonic-gate 	}
7917c478bd9Sstevel@tonic-gate 
7927c478bd9Sstevel@tonic-gate 	dprintf("loaded %lu bytes of CTF data for %s\n",
7937c478bd9Sstevel@tonic-gate 	    (ulong_t)fptr->file_ctf_size, fptr->file_pname);
7947c478bd9Sstevel@tonic-gate 
7957c478bd9Sstevel@tonic-gate 	return (fptr->file_ctfp);
7967c478bd9Sstevel@tonic-gate }
7977c478bd9Sstevel@tonic-gate 
7987c478bd9Sstevel@tonic-gate ctf_file_t *
7997c478bd9Sstevel@tonic-gate Paddr_to_ctf(struct ps_prochandle *P, uintptr_t addr)
8007c478bd9Sstevel@tonic-gate {
8017c478bd9Sstevel@tonic-gate 	map_info_t *mptr;
8027c478bd9Sstevel@tonic-gate 	file_info_t *fptr;
8037c478bd9Sstevel@tonic-gate 
8047c478bd9Sstevel@tonic-gate 	if (!P->info_valid)
8057c478bd9Sstevel@tonic-gate 		Pupdate_maps(P);
8067c478bd9Sstevel@tonic-gate 
8077c478bd9Sstevel@tonic-gate 	if ((mptr = Paddr2mptr(P, addr)) == NULL ||
8087c478bd9Sstevel@tonic-gate 	    (fptr = mptr->map_file) == NULL)
8097c478bd9Sstevel@tonic-gate 		return (NULL);
8107c478bd9Sstevel@tonic-gate 
8117c478bd9Sstevel@tonic-gate 	return (Pbuild_file_ctf(P, fptr));
8127c478bd9Sstevel@tonic-gate }
8137c478bd9Sstevel@tonic-gate 
8147c478bd9Sstevel@tonic-gate ctf_file_t *
8157c478bd9Sstevel@tonic-gate Plmid_to_ctf(struct ps_prochandle *P, Lmid_t lmid, const char *name)
8167c478bd9Sstevel@tonic-gate {
8177c478bd9Sstevel@tonic-gate 	map_info_t *mptr;
8187c478bd9Sstevel@tonic-gate 	file_info_t *fptr;
8197c478bd9Sstevel@tonic-gate 
8207c478bd9Sstevel@tonic-gate 	if (name == PR_OBJ_EVERY)
8217c478bd9Sstevel@tonic-gate 		return (NULL);
8227c478bd9Sstevel@tonic-gate 
8237c478bd9Sstevel@tonic-gate 	if ((mptr = object_name_to_map(P, lmid, name)) == NULL ||
8247c478bd9Sstevel@tonic-gate 	    (fptr = mptr->map_file) == NULL)
8257c478bd9Sstevel@tonic-gate 		return (NULL);
8267c478bd9Sstevel@tonic-gate 
8277c478bd9Sstevel@tonic-gate 	return (Pbuild_file_ctf(P, fptr));
8287c478bd9Sstevel@tonic-gate }
8297c478bd9Sstevel@tonic-gate 
8307c478bd9Sstevel@tonic-gate ctf_file_t *
8317c478bd9Sstevel@tonic-gate Pname_to_ctf(struct ps_prochandle *P, const char *name)
8327c478bd9Sstevel@tonic-gate {
8337c478bd9Sstevel@tonic-gate 	return (Plmid_to_ctf(P, PR_LMID_EVERY, name));
8347c478bd9Sstevel@tonic-gate }
8357c478bd9Sstevel@tonic-gate 
8367c478bd9Sstevel@tonic-gate /*
8377c478bd9Sstevel@tonic-gate  * If we're not a core file, re-read the /proc/<pid>/auxv file and store
8387c478bd9Sstevel@tonic-gate  * its contents in P->auxv.  In the case of a core file, we either
8397c478bd9Sstevel@tonic-gate  * initialized P->auxv in Pcore() from the NT_AUXV, or we don't have an
8407c478bd9Sstevel@tonic-gate  * auxv because the note was missing.
8417c478bd9Sstevel@tonic-gate  */
8427c478bd9Sstevel@tonic-gate void
8437c478bd9Sstevel@tonic-gate Preadauxvec(struct ps_prochandle *P)
8447c478bd9Sstevel@tonic-gate {
8457c478bd9Sstevel@tonic-gate 	char auxfile[64];
8467c478bd9Sstevel@tonic-gate 	struct stat statb;
8477c478bd9Sstevel@tonic-gate 	ssize_t naux;
8487c478bd9Sstevel@tonic-gate 	int fd;
8497c478bd9Sstevel@tonic-gate 
8507c478bd9Sstevel@tonic-gate 	if (P->state == PS_DEAD)
8517c478bd9Sstevel@tonic-gate 		return; /* Already read during Pgrab_core() */
8527c478bd9Sstevel@tonic-gate 	if (P->state == PS_IDLE)
8537c478bd9Sstevel@tonic-gate 		return; /* No aux vec for Pgrab_file() */
8547c478bd9Sstevel@tonic-gate 
8557c478bd9Sstevel@tonic-gate 	if (P->auxv != NULL) {
8567c478bd9Sstevel@tonic-gate 		free(P->auxv);
8577c478bd9Sstevel@tonic-gate 		P->auxv = NULL;
8587c478bd9Sstevel@tonic-gate 		P->nauxv = 0;
8597c478bd9Sstevel@tonic-gate 	}
8607c478bd9Sstevel@tonic-gate 
8619acbbeafSnn35248 	(void) snprintf(auxfile, sizeof (auxfile), "%s/%d/auxv",
8629acbbeafSnn35248 	    procfs_path, (int)P->pid);
8637c478bd9Sstevel@tonic-gate 	if ((fd = open(auxfile, O_RDONLY)) < 0)
8647c478bd9Sstevel@tonic-gate 		return;
8657c478bd9Sstevel@tonic-gate 
8667c478bd9Sstevel@tonic-gate 	if (fstat(fd, &statb) == 0 &&
8677c478bd9Sstevel@tonic-gate 	    statb.st_size >= sizeof (auxv_t) &&
8687c478bd9Sstevel@tonic-gate 	    (P->auxv = malloc(statb.st_size + sizeof (auxv_t))) != NULL) {
8697c478bd9Sstevel@tonic-gate 		if ((naux = read(fd, P->auxv, statb.st_size)) < 0 ||
8707c478bd9Sstevel@tonic-gate 		    (naux /= sizeof (auxv_t)) < 1) {
8717c478bd9Sstevel@tonic-gate 			free(P->auxv);
8727c478bd9Sstevel@tonic-gate 			P->auxv = NULL;
8737c478bd9Sstevel@tonic-gate 		} else {
8747c478bd9Sstevel@tonic-gate 			P->auxv[naux].a_type = AT_NULL;
8757c478bd9Sstevel@tonic-gate 			P->auxv[naux].a_un.a_val = 0L;
8767c478bd9Sstevel@tonic-gate 			P->nauxv = (int)naux;
8777c478bd9Sstevel@tonic-gate 		}
8787c478bd9Sstevel@tonic-gate 	}
8797c478bd9Sstevel@tonic-gate 
8807c478bd9Sstevel@tonic-gate 	(void) close(fd);
8817c478bd9Sstevel@tonic-gate }
8827c478bd9Sstevel@tonic-gate 
8837c478bd9Sstevel@tonic-gate /*
8847c478bd9Sstevel@tonic-gate  * Return a requested element from the process's aux vector.
8857c478bd9Sstevel@tonic-gate  * Return -1 on failure (this is adequate for our purposes).
8867c478bd9Sstevel@tonic-gate  */
8877c478bd9Sstevel@tonic-gate long
8887c478bd9Sstevel@tonic-gate Pgetauxval(struct ps_prochandle *P, int type)
8897c478bd9Sstevel@tonic-gate {
8907c478bd9Sstevel@tonic-gate 	auxv_t *auxv;
8917c478bd9Sstevel@tonic-gate 
8927c478bd9Sstevel@tonic-gate 	if (P->auxv == NULL)
8937c478bd9Sstevel@tonic-gate 		Preadauxvec(P);
8947c478bd9Sstevel@tonic-gate 
8957c478bd9Sstevel@tonic-gate 	if (P->auxv == NULL)
8967c478bd9Sstevel@tonic-gate 		return (-1);
8977c478bd9Sstevel@tonic-gate 
8987c478bd9Sstevel@tonic-gate 	for (auxv = P->auxv; auxv->a_type != AT_NULL; auxv++) {
8997c478bd9Sstevel@tonic-gate 		if (auxv->a_type == type)
9007c478bd9Sstevel@tonic-gate 			return (auxv->a_un.a_val);
9017c478bd9Sstevel@tonic-gate 	}
9027c478bd9Sstevel@tonic-gate 
9037c478bd9Sstevel@tonic-gate 	return (-1);
9047c478bd9Sstevel@tonic-gate }
9057c478bd9Sstevel@tonic-gate 
9067c478bd9Sstevel@tonic-gate /*
9077c478bd9Sstevel@tonic-gate  * Return a pointer to our internal copy of the process's aux vector.
9087c478bd9Sstevel@tonic-gate  * The caller should not hold on to this pointer across any libproc calls.
9097c478bd9Sstevel@tonic-gate  */
9107c478bd9Sstevel@tonic-gate const auxv_t *
9117c478bd9Sstevel@tonic-gate Pgetauxvec(struct ps_prochandle *P)
9127c478bd9Sstevel@tonic-gate {
9137c478bd9Sstevel@tonic-gate 	static const auxv_t empty = { AT_NULL, 0L };
9147c478bd9Sstevel@tonic-gate 
9157c478bd9Sstevel@tonic-gate 	if (P->auxv == NULL)
9167c478bd9Sstevel@tonic-gate 		Preadauxvec(P);
9177c478bd9Sstevel@tonic-gate 
9187c478bd9Sstevel@tonic-gate 	if (P->auxv == NULL)
9197c478bd9Sstevel@tonic-gate 		return (&empty);
9207c478bd9Sstevel@tonic-gate 
9217c478bd9Sstevel@tonic-gate 	return (P->auxv);
9227c478bd9Sstevel@tonic-gate }
9237c478bd9Sstevel@tonic-gate 
9247c478bd9Sstevel@tonic-gate /*
92512e72dbbSrh87107  * Return 1 if the given mapping corresponds to the given file_info_t's
92612e72dbbSrh87107  * load object; return 0 otherwise.
92712e72dbbSrh87107  */
92812e72dbbSrh87107 static int
92912e72dbbSrh87107 is_mapping_in_file(struct ps_prochandle *P, map_info_t *mptr, file_info_t *fptr)
93012e72dbbSrh87107 {
93112e72dbbSrh87107 	prmap_t *pmap = &mptr->map_pmap;
93212e72dbbSrh87107 	rd_loadobj_t *lop = fptr->file_lo;
93312e72dbbSrh87107 	uint_t i;
934d7755b5aSrh87107 	uintptr_t mstart, mend, sstart, send;
93512e72dbbSrh87107 
93612e72dbbSrh87107 	/*
93712e72dbbSrh87107 	 * We can get for free the start address of the text and data
93812e72dbbSrh87107 	 * sections of the load object. Start by seeing if the mapping
93912e72dbbSrh87107 	 * encloses either of these.
94012e72dbbSrh87107 	 */
94112e72dbbSrh87107 	if ((pmap->pr_vaddr <= lop->rl_base &&
94212e72dbbSrh87107 	    lop->rl_base < pmap->pr_vaddr + pmap->pr_size) ||
94312e72dbbSrh87107 	    (pmap->pr_vaddr <= lop->rl_data_base &&
94412e72dbbSrh87107 	    lop->rl_data_base < pmap->pr_vaddr + pmap->pr_size))
94512e72dbbSrh87107 		return (1);
94612e72dbbSrh87107 
94712e72dbbSrh87107 	/*
94812e72dbbSrh87107 	 * It's still possible that this mapping correponds to the load
94912e72dbbSrh87107 	 * object. Consider the example of a mapping whose start and end
95012e72dbbSrh87107 	 * addresses correspond to those of the load object's text section.
95112e72dbbSrh87107 	 * If the mapping splits, e.g. as a result of a segment demotion,
95212e72dbbSrh87107 	 * then although both mappings are still backed by the same section,
95312e72dbbSrh87107 	 * only one will be seen to enclose that section's start address.
95412e72dbbSrh87107 	 * Thus, to be rigorous, we ask not whether this mapping encloses
95512e72dbbSrh87107 	 * the start of a section, but whether there exists a section that
956d7755b5aSrh87107 	 * overlaps this mapping.
95712e72dbbSrh87107 	 *
95812e72dbbSrh87107 	 * If we don't already have the section addresses, and we successfully
95912e72dbbSrh87107 	 * get them, then we cache them in case we come here again.
96012e72dbbSrh87107 	 */
96112e72dbbSrh87107 	if (fptr->file_saddrs == NULL &&
96212e72dbbSrh87107 	    (fptr->file_saddrs = get_saddrs(P,
96312e72dbbSrh87107 	    fptr->file_map->map_pmap.pr_vaddr, &fptr->file_nsaddrs)) == NULL)
96412e72dbbSrh87107 		return (0);
965d7755b5aSrh87107 
966d7755b5aSrh87107 	mstart = mptr->map_pmap.pr_vaddr;
967d7755b5aSrh87107 	mend = mptr->map_pmap.pr_vaddr + mptr->map_pmap.pr_size;
96812e72dbbSrh87107 	for (i = 0; i < fptr->file_nsaddrs; i += 2) {
969d7755b5aSrh87107 		/* Does this section overlap the mapping? */
970d7755b5aSrh87107 		sstart = fptr->file_saddrs[i];
971d7755b5aSrh87107 		send = fptr->file_saddrs[i + 1];
972d7755b5aSrh87107 		if (!(mend <= sstart || mstart >= send))
97312e72dbbSrh87107 			return (1);
97412e72dbbSrh87107 	}
97512e72dbbSrh87107 
97612e72dbbSrh87107 	return (0);
97712e72dbbSrh87107 }
97812e72dbbSrh87107 
97912e72dbbSrh87107 /*
9807c478bd9Sstevel@tonic-gate  * Find or build the symbol table for the given mapping.
9817c478bd9Sstevel@tonic-gate  */
9827c478bd9Sstevel@tonic-gate static file_info_t *
9837c478bd9Sstevel@tonic-gate build_map_symtab(struct ps_prochandle *P, map_info_t *mptr)
9847c478bd9Sstevel@tonic-gate {
9857c478bd9Sstevel@tonic-gate 	prmap_t *pmap = &mptr->map_pmap;
9867c478bd9Sstevel@tonic-gate 	file_info_t *fptr;
9877c478bd9Sstevel@tonic-gate 	uint_t i;
9887c478bd9Sstevel@tonic-gate 
9897c478bd9Sstevel@tonic-gate 	if ((fptr = mptr->map_file) != NULL) {
9907c478bd9Sstevel@tonic-gate 		Pbuild_file_symtab(P, fptr);
9917c478bd9Sstevel@tonic-gate 		return (fptr);
9927c478bd9Sstevel@tonic-gate 	}
9937c478bd9Sstevel@tonic-gate 
9947c478bd9Sstevel@tonic-gate 	if (pmap->pr_mapname[0] == '\0')
9957c478bd9Sstevel@tonic-gate 		return (NULL);
9967c478bd9Sstevel@tonic-gate 
9977c478bd9Sstevel@tonic-gate 	/*
9987c478bd9Sstevel@tonic-gate 	 * Attempt to find a matching file.
9997c478bd9Sstevel@tonic-gate 	 * (A file can be mapped at several different addresses.)
10007c478bd9Sstevel@tonic-gate 	 */
10017c478bd9Sstevel@tonic-gate 	for (i = 0, fptr = list_next(&P->file_head); i < P->num_files;
10027c478bd9Sstevel@tonic-gate 	    i++, fptr = list_next(fptr)) {
10037c478bd9Sstevel@tonic-gate 		if (strcmp(fptr->file_pname, pmap->pr_mapname) == 0 &&
100412e72dbbSrh87107 		    fptr->file_lo && is_mapping_in_file(P, mptr, fptr)) {
10057c478bd9Sstevel@tonic-gate 			mptr->map_file = fptr;
10067c478bd9Sstevel@tonic-gate 			fptr->file_ref++;
10077c478bd9Sstevel@tonic-gate 			Pbuild_file_symtab(P, fptr);
10087c478bd9Sstevel@tonic-gate 			return (fptr);
10097c478bd9Sstevel@tonic-gate 		}
10107c478bd9Sstevel@tonic-gate 	}
10117c478bd9Sstevel@tonic-gate 
10127c478bd9Sstevel@tonic-gate 	/*
10137c478bd9Sstevel@tonic-gate 	 * If we need to create a new file_info structure, iterate
10147c478bd9Sstevel@tonic-gate 	 * through the load objects in order to attempt to connect
10157c478bd9Sstevel@tonic-gate 	 * this new file with its primary text mapping.  We again
10167c478bd9Sstevel@tonic-gate 	 * need to handle ld.so as a special case because we need
10177c478bd9Sstevel@tonic-gate 	 * to be able to bootstrap librtld_db.
10187c478bd9Sstevel@tonic-gate 	 */
10197c478bd9Sstevel@tonic-gate 	if ((fptr = file_info_new(P, mptr)) == NULL)
10207c478bd9Sstevel@tonic-gate 		return (NULL);
10217c478bd9Sstevel@tonic-gate 
10227c478bd9Sstevel@tonic-gate 	if (P->map_ldso != mptr) {
10237c478bd9Sstevel@tonic-gate 		if (P->rap != NULL)
10247c478bd9Sstevel@tonic-gate 			(void) rd_loadobj_iter(P->rap, map_iter, P);
10257c478bd9Sstevel@tonic-gate 		else
10267c478bd9Sstevel@tonic-gate 			(void) Prd_agent(P);
10277c478bd9Sstevel@tonic-gate 	} else {
10287c478bd9Sstevel@tonic-gate 		fptr->file_map = mptr;
10297c478bd9Sstevel@tonic-gate 	}
10307c478bd9Sstevel@tonic-gate 
10317c478bd9Sstevel@tonic-gate 	/*
10327c478bd9Sstevel@tonic-gate 	 * If librtld_db wasn't able to help us connect the file to a primary
10337c478bd9Sstevel@tonic-gate 	 * text mapping, set file_map to the current mapping because we require
10347c478bd9Sstevel@tonic-gate 	 * fptr->file_map to be set in Pbuild_file_symtab.  librtld_db may be
10357c478bd9Sstevel@tonic-gate 	 * unaware of what's going on in the rare case that a legitimate ELF
10367c478bd9Sstevel@tonic-gate 	 * file has been mmap(2)ed into the process address space *without*
10379acbbeafSnn35248 	 * the use of dlopen(3x).
10387c478bd9Sstevel@tonic-gate 	 */
10397c478bd9Sstevel@tonic-gate 	if (fptr->file_map == NULL)
10407c478bd9Sstevel@tonic-gate 		fptr->file_map = mptr;
10417c478bd9Sstevel@tonic-gate 
10427c478bd9Sstevel@tonic-gate 	Pbuild_file_symtab(P, fptr);
10437c478bd9Sstevel@tonic-gate 
10447c478bd9Sstevel@tonic-gate 	return (fptr);
10457c478bd9Sstevel@tonic-gate }
10467c478bd9Sstevel@tonic-gate 
10477c478bd9Sstevel@tonic-gate static int
104830da1432Sahl read_ehdr32(struct ps_prochandle *P, Elf32_Ehdr *ehdr, uint_t *phnum,
104930da1432Sahl     uintptr_t addr)
10507c478bd9Sstevel@tonic-gate {
10517c478bd9Sstevel@tonic-gate 	if (Pread(P, ehdr, sizeof (*ehdr), addr) != sizeof (*ehdr))
10527c478bd9Sstevel@tonic-gate 		return (-1);
10537c478bd9Sstevel@tonic-gate 
10547c478bd9Sstevel@tonic-gate 	if (ehdr->e_ident[EI_MAG0] != ELFMAG0 ||
10557c478bd9Sstevel@tonic-gate 	    ehdr->e_ident[EI_MAG1] != ELFMAG1 ||
10567c478bd9Sstevel@tonic-gate 	    ehdr->e_ident[EI_MAG2] != ELFMAG2 ||
10577c478bd9Sstevel@tonic-gate 	    ehdr->e_ident[EI_MAG3] != ELFMAG3 ||
10587c478bd9Sstevel@tonic-gate 	    ehdr->e_ident[EI_CLASS] != ELFCLASS32 ||
10597c478bd9Sstevel@tonic-gate #ifdef _BIG_ENDIAN
10607c478bd9Sstevel@tonic-gate 	    ehdr->e_ident[EI_DATA] != ELFDATA2MSB ||
10617c478bd9Sstevel@tonic-gate #else
10627c478bd9Sstevel@tonic-gate 	    ehdr->e_ident[EI_DATA] != ELFDATA2LSB ||
10637c478bd9Sstevel@tonic-gate #endif
10647c478bd9Sstevel@tonic-gate 	    ehdr->e_ident[EI_VERSION] != EV_CURRENT)
10657c478bd9Sstevel@tonic-gate 		return (-1);
10667c478bd9Sstevel@tonic-gate 
106730da1432Sahl 	if ((*phnum = ehdr->e_phnum) == PN_XNUM) {
106830da1432Sahl 		Elf32_Shdr shdr0;
106930da1432Sahl 
107030da1432Sahl 		if (ehdr->e_shoff == 0 || ehdr->e_shentsize < sizeof (shdr0) ||
107130da1432Sahl 		    Pread(P, &shdr0, sizeof (shdr0), addr + ehdr->e_shoff) !=
107230da1432Sahl 		    sizeof (shdr0))
107330da1432Sahl 			return (-1);
107430da1432Sahl 
107530da1432Sahl 		if (shdr0.sh_info != 0)
107630da1432Sahl 			*phnum = shdr0.sh_info;
107730da1432Sahl 	}
107830da1432Sahl 
10797c478bd9Sstevel@tonic-gate 	return (0);
10807c478bd9Sstevel@tonic-gate }
10817c478bd9Sstevel@tonic-gate 
10827c478bd9Sstevel@tonic-gate static int
10837c478bd9Sstevel@tonic-gate read_dynamic_phdr32(struct ps_prochandle *P, const Elf32_Ehdr *ehdr,
108430da1432Sahl     uint_t phnum, Elf32_Phdr *phdr, uintptr_t addr)
10857c478bd9Sstevel@tonic-gate {
10867c478bd9Sstevel@tonic-gate 	uint_t i;
10877c478bd9Sstevel@tonic-gate 
108830da1432Sahl 	for (i = 0; i < phnum; i++) {
10897c478bd9Sstevel@tonic-gate 		uintptr_t a = addr + ehdr->e_phoff + i * ehdr->e_phentsize;
10907c478bd9Sstevel@tonic-gate 		if (Pread(P, phdr, sizeof (*phdr), a) != sizeof (*phdr))
10917c478bd9Sstevel@tonic-gate 			return (-1);
10927c478bd9Sstevel@tonic-gate 
10937c478bd9Sstevel@tonic-gate 		if (phdr->p_type == PT_DYNAMIC)
10947c478bd9Sstevel@tonic-gate 			return (0);
10957c478bd9Sstevel@tonic-gate 	}
10967c478bd9Sstevel@tonic-gate 
10977c478bd9Sstevel@tonic-gate 	return (-1);
10987c478bd9Sstevel@tonic-gate }
10997c478bd9Sstevel@tonic-gate 
11007c478bd9Sstevel@tonic-gate #ifdef _LP64
11017c478bd9Sstevel@tonic-gate static int
110230da1432Sahl read_ehdr64(struct ps_prochandle *P, Elf64_Ehdr *ehdr, uint_t *phnum,
110330da1432Sahl     uintptr_t addr)
11047c478bd9Sstevel@tonic-gate {
11057c478bd9Sstevel@tonic-gate 	if (Pread(P, ehdr, sizeof (Elf64_Ehdr), addr) != sizeof (Elf64_Ehdr))
11067c478bd9Sstevel@tonic-gate 		return (-1);
11077c478bd9Sstevel@tonic-gate 
11087c478bd9Sstevel@tonic-gate 	if (ehdr->e_ident[EI_MAG0] != ELFMAG0 ||
11097c478bd9Sstevel@tonic-gate 	    ehdr->e_ident[EI_MAG1] != ELFMAG1 ||
11107c478bd9Sstevel@tonic-gate 	    ehdr->e_ident[EI_MAG2] != ELFMAG2 ||
11117c478bd9Sstevel@tonic-gate 	    ehdr->e_ident[EI_MAG3] != ELFMAG3 ||
11127c478bd9Sstevel@tonic-gate 	    ehdr->e_ident[EI_CLASS] != ELFCLASS64 ||
11137c478bd9Sstevel@tonic-gate #ifdef _BIG_ENDIAN
11147c478bd9Sstevel@tonic-gate 	    ehdr->e_ident[EI_DATA] != ELFDATA2MSB ||
11157c478bd9Sstevel@tonic-gate #else
11167c478bd9Sstevel@tonic-gate 	    ehdr->e_ident[EI_DATA] != ELFDATA2LSB ||
11177c478bd9Sstevel@tonic-gate #endif
11187c478bd9Sstevel@tonic-gate 	    ehdr->e_ident[EI_VERSION] != EV_CURRENT)
11197c478bd9Sstevel@tonic-gate 		return (-1);
11207c478bd9Sstevel@tonic-gate 
112130da1432Sahl 	if ((*phnum = ehdr->e_phnum) == PN_XNUM) {
112230da1432Sahl 		Elf64_Shdr shdr0;
112330da1432Sahl 
112430da1432Sahl 		if (ehdr->e_shoff == 0 || ehdr->e_shentsize < sizeof (shdr0) ||
112530da1432Sahl 		    Pread(P, &shdr0, sizeof (shdr0), addr + ehdr->e_shoff) !=
112630da1432Sahl 		    sizeof (shdr0))
112730da1432Sahl 			return (-1);
112830da1432Sahl 
112930da1432Sahl 		if (shdr0.sh_info != 0)
113030da1432Sahl 			*phnum = shdr0.sh_info;
113130da1432Sahl 	}
113230da1432Sahl 
11337c478bd9Sstevel@tonic-gate 	return (0);
11347c478bd9Sstevel@tonic-gate }
11357c478bd9Sstevel@tonic-gate 
11367c478bd9Sstevel@tonic-gate static int
11377c478bd9Sstevel@tonic-gate read_dynamic_phdr64(struct ps_prochandle *P, const Elf64_Ehdr *ehdr,
113830da1432Sahl     uint_t phnum, Elf64_Phdr *phdr, uintptr_t addr)
11397c478bd9Sstevel@tonic-gate {
11407c478bd9Sstevel@tonic-gate 	uint_t i;
11417c478bd9Sstevel@tonic-gate 
114230da1432Sahl 	for (i = 0; i < phnum; i++) {
11437c478bd9Sstevel@tonic-gate 		uintptr_t a = addr + ehdr->e_phoff + i * ehdr->e_phentsize;
11447c478bd9Sstevel@tonic-gate 		if (Pread(P, phdr, sizeof (*phdr), a) != sizeof (*phdr))
11457c478bd9Sstevel@tonic-gate 			return (-1);
11467c478bd9Sstevel@tonic-gate 
11477c478bd9Sstevel@tonic-gate 		if (phdr->p_type == PT_DYNAMIC)
11487c478bd9Sstevel@tonic-gate 			return (0);
11497c478bd9Sstevel@tonic-gate 	}
11507c478bd9Sstevel@tonic-gate 
11517c478bd9Sstevel@tonic-gate 	return (-1);
11527c478bd9Sstevel@tonic-gate }
11537c478bd9Sstevel@tonic-gate #endif	/* _LP64 */
11547c478bd9Sstevel@tonic-gate 
11557c478bd9Sstevel@tonic-gate /*
11567c478bd9Sstevel@tonic-gate  * The text segment for each load object contains the elf header and
11577c478bd9Sstevel@tonic-gate  * program headers. We can use this information to determine if the
11587c478bd9Sstevel@tonic-gate  * file that corresponds to the load object is the same file that
11597c478bd9Sstevel@tonic-gate  * was loaded into the process's address space. There can be a discrepency
11607c478bd9Sstevel@tonic-gate  * if a file is recompiled after the process is started or if the target
11617c478bd9Sstevel@tonic-gate  * represents a core file from a differently configured system -- two
11627c478bd9Sstevel@tonic-gate  * common examples. The DT_CHECKSUM entry in the dynamic section
11637c478bd9Sstevel@tonic-gate  * provides an easy method of comparison. It is important to note that
11647c478bd9Sstevel@tonic-gate  * the dynamic section usually lives in the data segment, but the meta
11657c478bd9Sstevel@tonic-gate  * data we use to find the dynamic section lives in the text segment so
11667c478bd9Sstevel@tonic-gate  * if either of those segments is absent we can't proceed.
11677c478bd9Sstevel@tonic-gate  *
11687c478bd9Sstevel@tonic-gate  * We're looking through the elf file for several items: the symbol tables
11697c478bd9Sstevel@tonic-gate  * (both dynsym and symtab), the procedure linkage table (PLT) base,
11707c478bd9Sstevel@tonic-gate  * size, and relocation base, and the CTF information. Most of this can
11717c478bd9Sstevel@tonic-gate  * be recovered from the loaded image of the file itself, the exceptions
11727c478bd9Sstevel@tonic-gate  * being the symtab and CTF data.
11737c478bd9Sstevel@tonic-gate  *
11747c478bd9Sstevel@tonic-gate  * First we try to open the file that we think corresponds to the load
11757c478bd9Sstevel@tonic-gate  * object, if the DT_CHECKSUM values match, we're all set, and can simply
11767c478bd9Sstevel@tonic-gate  * recover all the information we need from the file. If the values of
11777c478bd9Sstevel@tonic-gate  * DT_CHECKSUM don't match, or if we can't access the file for whatever
11787c478bd9Sstevel@tonic-gate  * reasaon, we fake up a elf file to use in its stead. If we can't read
11797c478bd9Sstevel@tonic-gate  * the elf data in the process's address space, we fall back to using
11807c478bd9Sstevel@tonic-gate  * the file even though it may give inaccurate information.
11817c478bd9Sstevel@tonic-gate  *
11827c478bd9Sstevel@tonic-gate  * The elf file that we fake up has to consist of sections for the
11837c478bd9Sstevel@tonic-gate  * dynsym, the PLT and the dynamic section. Note that in the case of a
11847c478bd9Sstevel@tonic-gate  * core file, we'll get the CTF data in the file_info_t later on from
11857c478bd9Sstevel@tonic-gate  * a section embedded the core file (if it's present).
11867c478bd9Sstevel@tonic-gate  *
11877c478bd9Sstevel@tonic-gate  * file_differs() conservatively looks for mismatched files, identifying
11887c478bd9Sstevel@tonic-gate  * a match when there is any ambiguity (since that's the legacy behavior).
11897c478bd9Sstevel@tonic-gate  */
11907c478bd9Sstevel@tonic-gate static int
11917c478bd9Sstevel@tonic-gate file_differs(struct ps_prochandle *P, Elf *elf, file_info_t *fptr)
11927c478bd9Sstevel@tonic-gate {
11937c478bd9Sstevel@tonic-gate 	Elf_Scn *scn;
11947c478bd9Sstevel@tonic-gate 	GElf_Shdr shdr;
11957c478bd9Sstevel@tonic-gate 	GElf_Dyn dyn;
11967c478bd9Sstevel@tonic-gate 	Elf_Data *data;
11977c478bd9Sstevel@tonic-gate 	uint_t i, ndyn;
11987c478bd9Sstevel@tonic-gate 	GElf_Xword cksum;
11997c478bd9Sstevel@tonic-gate 	uintptr_t addr;
12007c478bd9Sstevel@tonic-gate 
12017c478bd9Sstevel@tonic-gate 	if (fptr->file_map == NULL)
12027c478bd9Sstevel@tonic-gate 		return (0);
12037c478bd9Sstevel@tonic-gate 
12047c478bd9Sstevel@tonic-gate 	if ((Pcontent(P) & (CC_CONTENT_TEXT | CC_CONTENT_DATA)) !=
12057c478bd9Sstevel@tonic-gate 	    (CC_CONTENT_TEXT | CC_CONTENT_DATA))
12067c478bd9Sstevel@tonic-gate 		return (0);
12077c478bd9Sstevel@tonic-gate 
12087c478bd9Sstevel@tonic-gate 	/*
12097c478bd9Sstevel@tonic-gate 	 * First, we find the checksum value in the elf file.
12107c478bd9Sstevel@tonic-gate 	 */
12117c478bd9Sstevel@tonic-gate 	scn = NULL;
12127c478bd9Sstevel@tonic-gate 	while ((scn = elf_nextscn(elf, scn)) != NULL) {
12137c478bd9Sstevel@tonic-gate 		if (gelf_getshdr(scn, &shdr) != NULL &&
12147c478bd9Sstevel@tonic-gate 		    shdr.sh_type == SHT_DYNAMIC)
12157c478bd9Sstevel@tonic-gate 			goto found_shdr;
12167c478bd9Sstevel@tonic-gate 	}
12177c478bd9Sstevel@tonic-gate 	return (0);
12187c478bd9Sstevel@tonic-gate 
12197c478bd9Sstevel@tonic-gate found_shdr:
12207c478bd9Sstevel@tonic-gate 	if ((data = elf_getdata(scn, NULL)) == NULL)
12217c478bd9Sstevel@tonic-gate 		return (0);
12227c478bd9Sstevel@tonic-gate 
12237c478bd9Sstevel@tonic-gate 	if (P->status.pr_dmodel == PR_MODEL_ILP32)
12247c478bd9Sstevel@tonic-gate 		ndyn = shdr.sh_size / sizeof (Elf32_Dyn);
12257c478bd9Sstevel@tonic-gate #ifdef _LP64
12267c478bd9Sstevel@tonic-gate 	else if (P->status.pr_dmodel == PR_MODEL_LP64)
12277c478bd9Sstevel@tonic-gate 		ndyn = shdr.sh_size / sizeof (Elf64_Dyn);
12287c478bd9Sstevel@tonic-gate #endif
12297c478bd9Sstevel@tonic-gate 	else
12307c478bd9Sstevel@tonic-gate 		return (0);
12317c478bd9Sstevel@tonic-gate 
12327c478bd9Sstevel@tonic-gate 	for (i = 0; i < ndyn; i++) {
12337c478bd9Sstevel@tonic-gate 		if (gelf_getdyn(data, i, &dyn) != NULL &&
12347c478bd9Sstevel@tonic-gate 		    dyn.d_tag == DT_CHECKSUM)
12357c478bd9Sstevel@tonic-gate 			goto found_cksum;
12367c478bd9Sstevel@tonic-gate 	}
12379acbbeafSnn35248 
12389acbbeafSnn35248 	/*
12399acbbeafSnn35248 	 * The in-memory ELF has no DT_CHECKSUM section, but we will report it
12409acbbeafSnn35248 	 * as matching the file anyhow.
12419acbbeafSnn35248 	 */
12427c478bd9Sstevel@tonic-gate 	return (0);
12437c478bd9Sstevel@tonic-gate 
12447c478bd9Sstevel@tonic-gate found_cksum:
12457c478bd9Sstevel@tonic-gate 	cksum = dyn.d_un.d_val;
12467c478bd9Sstevel@tonic-gate 	dprintf("elf cksum value is %llx\n", (u_longlong_t)cksum);
12477c478bd9Sstevel@tonic-gate 
12487c478bd9Sstevel@tonic-gate 	/*
12497c478bd9Sstevel@tonic-gate 	 * Get the base of the text mapping that corresponds to this file.
12507c478bd9Sstevel@tonic-gate 	 */
12517c478bd9Sstevel@tonic-gate 	addr = fptr->file_map->map_pmap.pr_vaddr;
12527c478bd9Sstevel@tonic-gate 
12537c478bd9Sstevel@tonic-gate 	if (P->status.pr_dmodel == PR_MODEL_ILP32) {
12547c478bd9Sstevel@tonic-gate 		Elf32_Ehdr ehdr;
12557c478bd9Sstevel@tonic-gate 		Elf32_Phdr phdr;
12567c478bd9Sstevel@tonic-gate 		Elf32_Dyn dync, *dynp;
125730da1432Sahl 		uint_t phnum, i;
12587c478bd9Sstevel@tonic-gate 
125930da1432Sahl 		if (read_ehdr32(P, &ehdr, &phnum, addr) != 0 ||
126030da1432Sahl 		    read_dynamic_phdr32(P, &ehdr, phnum, &phdr, addr) != 0)
12617c478bd9Sstevel@tonic-gate 			return (0);
12627c478bd9Sstevel@tonic-gate 
12637c478bd9Sstevel@tonic-gate 		if (ehdr.e_type == ET_DYN)
12647c478bd9Sstevel@tonic-gate 			phdr.p_vaddr += addr;
12657c478bd9Sstevel@tonic-gate 		if ((dynp = malloc(phdr.p_filesz)) == NULL)
12667c478bd9Sstevel@tonic-gate 			return (0);
12677c478bd9Sstevel@tonic-gate 		dync.d_tag = DT_NULL;
12687c478bd9Sstevel@tonic-gate 		if (Pread(P, dynp, phdr.p_filesz, phdr.p_vaddr) !=
12697c478bd9Sstevel@tonic-gate 		    phdr.p_filesz) {
12707c478bd9Sstevel@tonic-gate 			free(dynp);
12717c478bd9Sstevel@tonic-gate 			return (0);
12727c478bd9Sstevel@tonic-gate 		}
12737c478bd9Sstevel@tonic-gate 
12747c478bd9Sstevel@tonic-gate 		for (i = 0; i < phdr.p_filesz / sizeof (Elf32_Dyn); i++) {
12757c478bd9Sstevel@tonic-gate 			if (dynp[i].d_tag == DT_CHECKSUM)
12767c478bd9Sstevel@tonic-gate 				dync = dynp[i];
12777c478bd9Sstevel@tonic-gate 		}
12787c478bd9Sstevel@tonic-gate 
12797c478bd9Sstevel@tonic-gate 		free(dynp);
12807c478bd9Sstevel@tonic-gate 
12817c478bd9Sstevel@tonic-gate 		if (dync.d_tag != DT_CHECKSUM)
12827c478bd9Sstevel@tonic-gate 			return (0);
12837c478bd9Sstevel@tonic-gate 
12847c478bd9Sstevel@tonic-gate 		dprintf("image cksum value is %llx\n",
12857c478bd9Sstevel@tonic-gate 		    (u_longlong_t)dync.d_un.d_val);
12867c478bd9Sstevel@tonic-gate 		return (dync.d_un.d_val != cksum);
12877c478bd9Sstevel@tonic-gate #ifdef _LP64
12887c478bd9Sstevel@tonic-gate 	} else if (P->status.pr_dmodel == PR_MODEL_LP64) {
12897c478bd9Sstevel@tonic-gate 		Elf64_Ehdr ehdr;
12907c478bd9Sstevel@tonic-gate 		Elf64_Phdr phdr;
12917c478bd9Sstevel@tonic-gate 		Elf64_Dyn dync, *dynp;
129230da1432Sahl 		uint_t phnum, i;
12937c478bd9Sstevel@tonic-gate 
129430da1432Sahl 		if (read_ehdr64(P, &ehdr, &phnum, addr) != 0 ||
129530da1432Sahl 		    read_dynamic_phdr64(P, &ehdr, phnum, &phdr, addr) != 0)
12967c478bd9Sstevel@tonic-gate 			return (0);
12977c478bd9Sstevel@tonic-gate 
12987c478bd9Sstevel@tonic-gate 		if (ehdr.e_type == ET_DYN)
12997c478bd9Sstevel@tonic-gate 			phdr.p_vaddr += addr;
13007c478bd9Sstevel@tonic-gate 		if ((dynp = malloc(phdr.p_filesz)) == NULL)
13017c478bd9Sstevel@tonic-gate 			return (0);
13027c478bd9Sstevel@tonic-gate 		dync.d_tag = DT_NULL;
13037c478bd9Sstevel@tonic-gate 		if (Pread(P, dynp, phdr.p_filesz, phdr.p_vaddr) !=
13047c478bd9Sstevel@tonic-gate 		    phdr.p_filesz) {
13057c478bd9Sstevel@tonic-gate 			free(dynp);
13067c478bd9Sstevel@tonic-gate 			return (0);
13077c478bd9Sstevel@tonic-gate 		}
13087c478bd9Sstevel@tonic-gate 
13097c478bd9Sstevel@tonic-gate 		for (i = 0; i < phdr.p_filesz / sizeof (Elf64_Dyn); i++) {
13107c478bd9Sstevel@tonic-gate 			if (dynp[i].d_tag == DT_CHECKSUM)
13117c478bd9Sstevel@tonic-gate 				dync = dynp[i];
13127c478bd9Sstevel@tonic-gate 		}
13137c478bd9Sstevel@tonic-gate 
13147c478bd9Sstevel@tonic-gate 		free(dynp);
13157c478bd9Sstevel@tonic-gate 
13167c478bd9Sstevel@tonic-gate 		if (dync.d_tag != DT_CHECKSUM)
13177c478bd9Sstevel@tonic-gate 			return (0);
13187c478bd9Sstevel@tonic-gate 
13197c478bd9Sstevel@tonic-gate 		dprintf("image cksum value is %llx\n",
13207c478bd9Sstevel@tonic-gate 		    (u_longlong_t)dync.d_un.d_val);
13217c478bd9Sstevel@tonic-gate 		return (dync.d_un.d_val != cksum);
13227c478bd9Sstevel@tonic-gate #endif	/* _LP64 */
13237c478bd9Sstevel@tonic-gate 	}
13247c478bd9Sstevel@tonic-gate 
13257c478bd9Sstevel@tonic-gate 	return (0);
13267c478bd9Sstevel@tonic-gate }
13277c478bd9Sstevel@tonic-gate 
1328d51e9074Sab196087 /*
1329d51e9074Sab196087  * Read data from the specified process and construct an in memory
1330d51e9074Sab196087  * image of an ELF file that represents it well enough to let
1331d51e9074Sab196087  * us probe it for information.
1332d51e9074Sab196087  */
13337c478bd9Sstevel@tonic-gate static Elf *
13347c478bd9Sstevel@tonic-gate fake_elf(struct ps_prochandle *P, file_info_t *fptr)
13357c478bd9Sstevel@tonic-gate {
13367c478bd9Sstevel@tonic-gate 	Elf *elf;
1337d51e9074Sab196087 	uintptr_t addr;
1338d51e9074Sab196087 	uint_t phnum;
13397c478bd9Sstevel@tonic-gate 
13407c478bd9Sstevel@tonic-gate 	if (fptr->file_map == NULL)
13417c478bd9Sstevel@tonic-gate 		return (NULL);
13427c478bd9Sstevel@tonic-gate 
13437c478bd9Sstevel@tonic-gate 	if ((Pcontent(P) & (CC_CONTENT_TEXT | CC_CONTENT_DATA)) !=
13447c478bd9Sstevel@tonic-gate 	    (CC_CONTENT_TEXT | CC_CONTENT_DATA))
13457c478bd9Sstevel@tonic-gate 		return (NULL);
13467c478bd9Sstevel@tonic-gate 
13477c478bd9Sstevel@tonic-gate 	addr = fptr->file_map->map_pmap.pr_vaddr;
13487c478bd9Sstevel@tonic-gate 
13497c478bd9Sstevel@tonic-gate 	if (P->status.pr_dmodel == PR_MODEL_ILP32) {
1350d51e9074Sab196087 		Elf32_Ehdr ehdr;
13517c478bd9Sstevel@tonic-gate 		Elf32_Phdr phdr;
13527c478bd9Sstevel@tonic-gate 
13539acbbeafSnn35248 		if ((read_ehdr32(P, &ehdr, &phnum, addr) != 0) ||
135430da1432Sahl 		    read_dynamic_phdr32(P, &ehdr, phnum, &phdr, addr) != 0)
13557c478bd9Sstevel@tonic-gate 			return (NULL);
13567c478bd9Sstevel@tonic-gate 
1357d51e9074Sab196087 		elf = fake_elf32(P, fptr, addr, &ehdr, phnum, &phdr);
13587c478bd9Sstevel@tonic-gate #ifdef _LP64
1359d51e9074Sab196087 	} else {
1360d51e9074Sab196087 		Elf64_Ehdr ehdr;
13617c478bd9Sstevel@tonic-gate 		Elf64_Phdr phdr;
13627c478bd9Sstevel@tonic-gate 
136330da1432Sahl 		if (read_ehdr64(P, &ehdr, &phnum, addr) != 0 ||
136430da1432Sahl 		    read_dynamic_phdr64(P, &ehdr, phnum, &phdr, addr) != 0)
13657c478bd9Sstevel@tonic-gate 			return (NULL);
13667c478bd9Sstevel@tonic-gate 
1367d51e9074Sab196087 		elf = fake_elf64(P, fptr, addr, &ehdr, phnum, &phdr);
1368d51e9074Sab196087 #endif
13697c478bd9Sstevel@tonic-gate 	}
13707c478bd9Sstevel@tonic-gate 
13717c478bd9Sstevel@tonic-gate 	return (elf);
13727c478bd9Sstevel@tonic-gate }
13737c478bd9Sstevel@tonic-gate 
13747c478bd9Sstevel@tonic-gate /*
13757c478bd9Sstevel@tonic-gate  * We wouldn't need these if qsort(3C) took an argument for the callback...
13767c478bd9Sstevel@tonic-gate  */
13777c478bd9Sstevel@tonic-gate static mutex_t sort_mtx = DEFAULTMUTEX;
13787c478bd9Sstevel@tonic-gate static char *sort_strs;
13797c478bd9Sstevel@tonic-gate static GElf_Sym *sort_syms;
13807c478bd9Sstevel@tonic-gate 
13817c478bd9Sstevel@tonic-gate int
13827c478bd9Sstevel@tonic-gate byaddr_cmp_common(GElf_Sym *a, char *aname, GElf_Sym *b, char *bname)
13837c478bd9Sstevel@tonic-gate {
13847c478bd9Sstevel@tonic-gate 	if (a->st_value < b->st_value)
13857c478bd9Sstevel@tonic-gate 		return (-1);
13867c478bd9Sstevel@tonic-gate 	if (a->st_value > b->st_value)
13877c478bd9Sstevel@tonic-gate 		return (1);
13887c478bd9Sstevel@tonic-gate 
13897c478bd9Sstevel@tonic-gate 	/*
13907c478bd9Sstevel@tonic-gate 	 * Prefer the function to the non-function.
13917c478bd9Sstevel@tonic-gate 	 */
13927c478bd9Sstevel@tonic-gate 	if (GELF_ST_TYPE(a->st_info) != GELF_ST_TYPE(b->st_info)) {
13937c478bd9Sstevel@tonic-gate 		if (GELF_ST_TYPE(a->st_info) == STT_FUNC)
13947c478bd9Sstevel@tonic-gate 			return (-1);
13957c478bd9Sstevel@tonic-gate 		if (GELF_ST_TYPE(b->st_info) == STT_FUNC)
13967c478bd9Sstevel@tonic-gate 			return (1);
13977c478bd9Sstevel@tonic-gate 	}
13987c478bd9Sstevel@tonic-gate 
13997c478bd9Sstevel@tonic-gate 	/*
14007c478bd9Sstevel@tonic-gate 	 * Prefer the weak or strong global symbol to the local symbol.
14017c478bd9Sstevel@tonic-gate 	 */
14027c478bd9Sstevel@tonic-gate 	if (GELF_ST_BIND(a->st_info) != GELF_ST_BIND(b->st_info)) {
14037c478bd9Sstevel@tonic-gate 		if (GELF_ST_BIND(b->st_info) == STB_LOCAL)
14047c478bd9Sstevel@tonic-gate 			return (-1);
14057c478bd9Sstevel@tonic-gate 		if (GELF_ST_BIND(a->st_info) == STB_LOCAL)
14067c478bd9Sstevel@tonic-gate 			return (1);
14077c478bd9Sstevel@tonic-gate 	}
14087c478bd9Sstevel@tonic-gate 
14097c478bd9Sstevel@tonic-gate 	/*
1410ab9a77c7Sahl 	 * Prefer the symbol that doesn't begin with a '$' since compilers and
1411ab9a77c7Sahl 	 * other symbol generators often use it as a prefix.
1412ab9a77c7Sahl 	 */
1413ab9a77c7Sahl 	if (*bname == '$')
1414ab9a77c7Sahl 		return (-1);
1415ab9a77c7Sahl 	if (*aname == '$')
1416ab9a77c7Sahl 		return (1);
1417ab9a77c7Sahl 
1418ab9a77c7Sahl 	/*
14197c478bd9Sstevel@tonic-gate 	 * Prefer the name with fewer leading underscores in the name.
14207c478bd9Sstevel@tonic-gate 	 */
14217c478bd9Sstevel@tonic-gate 	while (*aname == '_' && *bname == '_') {
14227c478bd9Sstevel@tonic-gate 		aname++;
14237c478bd9Sstevel@tonic-gate 		bname++;
14247c478bd9Sstevel@tonic-gate 	}
14257c478bd9Sstevel@tonic-gate 
14267c478bd9Sstevel@tonic-gate 	if (*bname == '_')
14277c478bd9Sstevel@tonic-gate 		return (-1);
14287c478bd9Sstevel@tonic-gate 	if (*aname == '_')
14297c478bd9Sstevel@tonic-gate 		return (1);
14307c478bd9Sstevel@tonic-gate 
14317c478bd9Sstevel@tonic-gate 	/*
14327c478bd9Sstevel@tonic-gate 	 * Prefer the symbol with the smaller size.
14337c478bd9Sstevel@tonic-gate 	 */
14347c478bd9Sstevel@tonic-gate 	if (a->st_size < b->st_size)
14357c478bd9Sstevel@tonic-gate 		return (-1);
14367c478bd9Sstevel@tonic-gate 	if (a->st_size > b->st_size)
14377c478bd9Sstevel@tonic-gate 		return (1);
14387c478bd9Sstevel@tonic-gate 
14397c478bd9Sstevel@tonic-gate 	/*
14407c478bd9Sstevel@tonic-gate 	 * All other factors being equal, fall back to lexicographic order.
14417c478bd9Sstevel@tonic-gate 	 */
14427c478bd9Sstevel@tonic-gate 	return (strcmp(aname, bname));
14437c478bd9Sstevel@tonic-gate }
14447c478bd9Sstevel@tonic-gate 
14457c478bd9Sstevel@tonic-gate static int
14467c478bd9Sstevel@tonic-gate byaddr_cmp(const void *aa, const void *bb)
14477c478bd9Sstevel@tonic-gate {
14487c478bd9Sstevel@tonic-gate 	GElf_Sym *a = &sort_syms[*(uint_t *)aa];
14497c478bd9Sstevel@tonic-gate 	GElf_Sym *b = &sort_syms[*(uint_t *)bb];
14507c478bd9Sstevel@tonic-gate 	char *aname = sort_strs + a->st_name;
14517c478bd9Sstevel@tonic-gate 	char *bname = sort_strs + b->st_name;
14527c478bd9Sstevel@tonic-gate 
14537c478bd9Sstevel@tonic-gate 	return (byaddr_cmp_common(a, aname, b, bname));
14547c478bd9Sstevel@tonic-gate }
14557c478bd9Sstevel@tonic-gate 
14567c478bd9Sstevel@tonic-gate static int
14577c478bd9Sstevel@tonic-gate byname_cmp(const void *aa, const void *bb)
14587c478bd9Sstevel@tonic-gate {
14597c478bd9Sstevel@tonic-gate 	GElf_Sym *a = &sort_syms[*(uint_t *)aa];
14607c478bd9Sstevel@tonic-gate 	GElf_Sym *b = &sort_syms[*(uint_t *)bb];
14617c478bd9Sstevel@tonic-gate 	char *aname = sort_strs + a->st_name;
14627c478bd9Sstevel@tonic-gate 	char *bname = sort_strs + b->st_name;
14637c478bd9Sstevel@tonic-gate 
14647c478bd9Sstevel@tonic-gate 	return (strcmp(aname, bname));
14657c478bd9Sstevel@tonic-gate }
14667c478bd9Sstevel@tonic-gate 
1467d51e9074Sab196087 /*
1468d51e9074Sab196087  * Given a symbol index, look up the corresponding symbol from the
1469d51e9074Sab196087  * given symbol table.
1470d51e9074Sab196087  *
1471d51e9074Sab196087  * This function allows the caller to treat the symbol table as a single
1472d51e9074Sab196087  * logical entity even though there may be 2 actual ELF symbol tables
1473d51e9074Sab196087  * involved. See the comments in Pcontrol.h for details.
1474d51e9074Sab196087  */
1475d51e9074Sab196087 static GElf_Sym *
1476d51e9074Sab196087 symtab_getsym(sym_tbl_t *symtab, int ndx, GElf_Sym *dst)
1477d51e9074Sab196087 {
1478d51e9074Sab196087 	/* If index is in range of primary symtab, look it up there */
1479d51e9074Sab196087 	if (ndx >= symtab->sym_symn_aux) {
1480d51e9074Sab196087 		return (gelf_getsym(symtab->sym_data_pri,
1481d51e9074Sab196087 		    ndx - symtab->sym_symn_aux, dst));
1482d51e9074Sab196087 	}
1483d51e9074Sab196087 
1484d51e9074Sab196087 	/* Not in primary: Look it up in the auxiliary symtab */
1485d51e9074Sab196087 	return (gelf_getsym(symtab->sym_data_aux, ndx, dst));
1486d51e9074Sab196087 }
1487d51e9074Sab196087 
14887c478bd9Sstevel@tonic-gate void
14897c478bd9Sstevel@tonic-gate optimize_symtab(sym_tbl_t *symtab)
14907c478bd9Sstevel@tonic-gate {
14917c478bd9Sstevel@tonic-gate 	GElf_Sym *symp, *syms;
14927c478bd9Sstevel@tonic-gate 	uint_t i, *indexa, *indexb;
14937c478bd9Sstevel@tonic-gate 	size_t symn, strsz, count;
14947c478bd9Sstevel@tonic-gate 
1495d51e9074Sab196087 	if (symtab == NULL || symtab->sym_data_pri == NULL ||
14967c478bd9Sstevel@tonic-gate 	    symtab->sym_byaddr != NULL)
14977c478bd9Sstevel@tonic-gate 		return;
14987c478bd9Sstevel@tonic-gate 
14997c478bd9Sstevel@tonic-gate 	symn = symtab->sym_symn;
15007c478bd9Sstevel@tonic-gate 	strsz = symtab->sym_strsz;
15017c478bd9Sstevel@tonic-gate 
15027c478bd9Sstevel@tonic-gate 	symp = syms = malloc(sizeof (GElf_Sym) * symn);
1503d51e9074Sab196087 	if (symp == NULL) {
1504d51e9074Sab196087 		dprintf("optimize_symtab: failed to malloc symbol array");
1505d51e9074Sab196087 		return;
1506d51e9074Sab196087 	}
15077c478bd9Sstevel@tonic-gate 
15087c478bd9Sstevel@tonic-gate 	/*
15097c478bd9Sstevel@tonic-gate 	 * First record all the symbols into a table and count up the ones
15107c478bd9Sstevel@tonic-gate 	 * that we're interested in. We mark symbols as invalid by setting
15117c478bd9Sstevel@tonic-gate 	 * the st_name to an illegal value.
15127c478bd9Sstevel@tonic-gate 	 */
15137c478bd9Sstevel@tonic-gate 	for (i = 0, count = 0; i < symn; i++, symp++) {
1514d51e9074Sab196087 		if (symtab_getsym(symtab, i, symp) != NULL &&
15157c478bd9Sstevel@tonic-gate 		    symp->st_name < strsz &&
15167c478bd9Sstevel@tonic-gate 		    IS_DATA_TYPE(GELF_ST_TYPE(symp->st_info)))
15177c478bd9Sstevel@tonic-gate 			count++;
15187c478bd9Sstevel@tonic-gate 		else
15197c478bd9Sstevel@tonic-gate 			symp->st_name = strsz;
15207c478bd9Sstevel@tonic-gate 	}
15217c478bd9Sstevel@tonic-gate 
15227c478bd9Sstevel@tonic-gate 	/*
15237c478bd9Sstevel@tonic-gate 	 * Allocate sufficient space for both tables and populate them
15247c478bd9Sstevel@tonic-gate 	 * with the same symbols we just counted.
15257c478bd9Sstevel@tonic-gate 	 */
15267c478bd9Sstevel@tonic-gate 	symtab->sym_count = count;
15277c478bd9Sstevel@tonic-gate 	indexa = symtab->sym_byaddr = calloc(sizeof (uint_t), count);
15287c478bd9Sstevel@tonic-gate 	indexb = symtab->sym_byname = calloc(sizeof (uint_t), count);
1529d51e9074Sab196087 	if (indexa == NULL || indexb == NULL) {
1530d51e9074Sab196087 		dprintf(
1531d51e9074Sab196087 		    "optimize_symtab: failed to malloc symbol index arrays");
1532d51e9074Sab196087 		symtab->sym_count = 0;
1533d51e9074Sab196087 		if (indexa != NULL) {	/* First alloc succeeded. Free it */
1534d51e9074Sab196087 			free(indexa);
1535d51e9074Sab196087 			symtab->sym_byaddr = NULL;
1536d51e9074Sab196087 		}
1537d51e9074Sab196087 		free(syms);
1538d51e9074Sab196087 		return;
1539d51e9074Sab196087 	}
15407c478bd9Sstevel@tonic-gate 	for (i = 0, symp = syms; i < symn; i++, symp++) {
15417c478bd9Sstevel@tonic-gate 		if (symp->st_name < strsz)
15427c478bd9Sstevel@tonic-gate 			*indexa++ = *indexb++ = i;
15437c478bd9Sstevel@tonic-gate 	}
15447c478bd9Sstevel@tonic-gate 
15457c478bd9Sstevel@tonic-gate 	/*
1546d7755b5aSrh87107 	 * Sort the two tables according to the appropriate criteria,
1547d7755b5aSrh87107 	 * unless the user has overridden this behaviour.
1548d7755b5aSrh87107 	 *
1549d7755b5aSrh87107 	 * An example where we might not sort the tables is the relatively
1550d7755b5aSrh87107 	 * unusual case of a process with very large symbol tables in which
1551d7755b5aSrh87107 	 * we perform few lookups. In such a case the total time would be
1552d7755b5aSrh87107 	 * dominated by the sort. It is difficult to determine a priori
1553d7755b5aSrh87107 	 * how many lookups an arbitrary client will perform, and
1554d7755b5aSrh87107 	 * hence whether the symbol tables should be sorted. We therefore
1555d7755b5aSrh87107 	 * sort the tables by default, but provide the user with a
1556d7755b5aSrh87107 	 * "chicken switch" in the form of the LIBPROC_NO_QSORT
1557d7755b5aSrh87107 	 * environment variable.
15587c478bd9Sstevel@tonic-gate 	 */
1559d7755b5aSrh87107 	if (!_libproc_no_qsort) {
15607c478bd9Sstevel@tonic-gate 		(void) mutex_lock(&sort_mtx);
15617c478bd9Sstevel@tonic-gate 		sort_strs = symtab->sym_strs;
15627c478bd9Sstevel@tonic-gate 		sort_syms = syms;
15637c478bd9Sstevel@tonic-gate 
15647c478bd9Sstevel@tonic-gate 		qsort(symtab->sym_byaddr, count, sizeof (uint_t), byaddr_cmp);
15657c478bd9Sstevel@tonic-gate 		qsort(symtab->sym_byname, count, sizeof (uint_t), byname_cmp);
15667c478bd9Sstevel@tonic-gate 
15677c478bd9Sstevel@tonic-gate 		sort_strs = NULL;
15687c478bd9Sstevel@tonic-gate 		sort_syms = NULL;
15697c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&sort_mtx);
1570d7755b5aSrh87107 	}
15717c478bd9Sstevel@tonic-gate 
15727c478bd9Sstevel@tonic-gate 	free(syms);
15737c478bd9Sstevel@tonic-gate }
15747c478bd9Sstevel@tonic-gate 
15757c478bd9Sstevel@tonic-gate /*
15767c478bd9Sstevel@tonic-gate  * Build the symbol table for the given mapped file.
15777c478bd9Sstevel@tonic-gate  */
15787c478bd9Sstevel@tonic-gate void
15797c478bd9Sstevel@tonic-gate Pbuild_file_symtab(struct ps_prochandle *P, file_info_t *fptr)
15807c478bd9Sstevel@tonic-gate {
15817c478bd9Sstevel@tonic-gate 	char objectfile[PATH_MAX];
15827c478bd9Sstevel@tonic-gate 	uint_t i;
15837c478bd9Sstevel@tonic-gate 
15847c478bd9Sstevel@tonic-gate 	GElf_Ehdr ehdr;
15857c478bd9Sstevel@tonic-gate 	GElf_Sym s;
15867c478bd9Sstevel@tonic-gate 
15877c478bd9Sstevel@tonic-gate 	Elf_Data *shdata;
15887c478bd9Sstevel@tonic-gate 	Elf_Scn *scn;
15897c478bd9Sstevel@tonic-gate 	Elf *elf;
159030da1432Sahl 	size_t nshdrs, shstrndx;
15917c478bd9Sstevel@tonic-gate 
15927c478bd9Sstevel@tonic-gate 	struct {
15937c478bd9Sstevel@tonic-gate 		GElf_Shdr c_shdr;
15947c478bd9Sstevel@tonic-gate 		Elf_Data *c_data;
15957c478bd9Sstevel@tonic-gate 		const char *c_name;
15967c478bd9Sstevel@tonic-gate 	} *cp, *cache = NULL, *dyn = NULL, *plt = NULL, *ctf = NULL;
15977c478bd9Sstevel@tonic-gate 
15987c478bd9Sstevel@tonic-gate 	if (fptr->file_init)
15997c478bd9Sstevel@tonic-gate 		return;	/* We've already processed this file */
16007c478bd9Sstevel@tonic-gate 
16017c478bd9Sstevel@tonic-gate 	/*
16027c478bd9Sstevel@tonic-gate 	 * Mark the file_info struct as having the symbol table initialized
16037c478bd9Sstevel@tonic-gate 	 * even if we fail below.  We tried once; we don't try again.
16047c478bd9Sstevel@tonic-gate 	 */
16057c478bd9Sstevel@tonic-gate 	fptr->file_init = 1;
16067c478bd9Sstevel@tonic-gate 
16077c478bd9Sstevel@tonic-gate 	if (elf_version(EV_CURRENT) == EV_NONE) {
16087c478bd9Sstevel@tonic-gate 		dprintf("libproc ELF version is more recent than libelf\n");
16097c478bd9Sstevel@tonic-gate 		return;
16107c478bd9Sstevel@tonic-gate 	}
16117c478bd9Sstevel@tonic-gate 
16127c478bd9Sstevel@tonic-gate 	if (P->state == PS_DEAD || P->state == PS_IDLE) {
16137c478bd9Sstevel@tonic-gate 		/*
16147c478bd9Sstevel@tonic-gate 		 * If we're a not live, we can't open files from the /proc
16157c478bd9Sstevel@tonic-gate 		 * object directory; we have only the mapping and file names
16167c478bd9Sstevel@tonic-gate 		 * to guide us.  We prefer the file_lname, but need to handle
16177c478bd9Sstevel@tonic-gate 		 * the case of it being NULL in order to bootstrap: we first
16187c478bd9Sstevel@tonic-gate 		 * come here during rd_new() when the only information we have
16197c478bd9Sstevel@tonic-gate 		 * is interpreter name associated with the AT_BASE mapping.
16207c478bd9Sstevel@tonic-gate 		 */
16217c478bd9Sstevel@tonic-gate 		(void) snprintf(objectfile, sizeof (objectfile), "%s",
16227c478bd9Sstevel@tonic-gate 		    fptr->file_lname ? fptr->file_lname : fptr->file_pname);
16237c478bd9Sstevel@tonic-gate 	} else {
16247c478bd9Sstevel@tonic-gate 		(void) snprintf(objectfile, sizeof (objectfile),
16259acbbeafSnn35248 		    "%s/%d/object/%s",
16269acbbeafSnn35248 		    procfs_path, (int)P->pid, fptr->file_pname);
16277c478bd9Sstevel@tonic-gate 	}
16287c478bd9Sstevel@tonic-gate 
16297c478bd9Sstevel@tonic-gate 	/*
16307c478bd9Sstevel@tonic-gate 	 * Open the object file, create the elf file, and then get the elf
16317c478bd9Sstevel@tonic-gate 	 * header and .shstrtab data buffer so we can process sections by
16327c478bd9Sstevel@tonic-gate 	 * name. If anything goes wrong try to fake up an elf file from
16337c478bd9Sstevel@tonic-gate 	 * the in-core elf image.
16347c478bd9Sstevel@tonic-gate 	 */
16357c478bd9Sstevel@tonic-gate 	if ((fptr->file_fd = open(objectfile, O_RDONLY)) < 0) {
16367c478bd9Sstevel@tonic-gate 		dprintf("Pbuild_file_symtab: failed to open %s: %s\n",
16377c478bd9Sstevel@tonic-gate 		    objectfile, strerror(errno));
16387c478bd9Sstevel@tonic-gate 
16397c478bd9Sstevel@tonic-gate 		if ((elf = fake_elf(P, fptr)) == NULL ||
16407c478bd9Sstevel@tonic-gate 		    elf_kind(elf) != ELF_K_ELF ||
16417c478bd9Sstevel@tonic-gate 		    gelf_getehdr(elf, &ehdr) == NULL ||
164230da1432Sahl 		    elf_getshnum(elf, &nshdrs) == 0 ||
164330da1432Sahl 		    elf_getshstrndx(elf, &shstrndx) == 0 ||
164430da1432Sahl 		    (scn = elf_getscn(elf, shstrndx)) == NULL ||
16457c478bd9Sstevel@tonic-gate 		    (shdata = elf_getdata(scn, NULL)) == NULL) {
16467c478bd9Sstevel@tonic-gate 			dprintf("failed to fake up ELF file\n");
16477c478bd9Sstevel@tonic-gate 			return;
16487c478bd9Sstevel@tonic-gate 		}
16497c478bd9Sstevel@tonic-gate 
16507c478bd9Sstevel@tonic-gate 	} else if ((elf = elf_begin(fptr->file_fd, ELF_C_READ, NULL)) == NULL ||
16517c478bd9Sstevel@tonic-gate 	    elf_kind(elf) != ELF_K_ELF ||
16527c478bd9Sstevel@tonic-gate 	    gelf_getehdr(elf, &ehdr) == NULL ||
165330da1432Sahl 	    elf_getshnum(elf, &nshdrs) == 0 ||
165430da1432Sahl 	    elf_getshstrndx(elf, &shstrndx) == 0 ||
165530da1432Sahl 	    (scn = elf_getscn(elf, shstrndx)) == NULL ||
16567c478bd9Sstevel@tonic-gate 	    (shdata = elf_getdata(scn, NULL)) == NULL) {
16579acbbeafSnn35248 		int err = elf_errno();
16589acbbeafSnn35248 
16597c478bd9Sstevel@tonic-gate 		dprintf("failed to process ELF file %s: %s\n",
16609acbbeafSnn35248 		    objectfile, (err == 0) ? "<null>" : elf_errmsg(err));
16617c478bd9Sstevel@tonic-gate 
16627c478bd9Sstevel@tonic-gate 		if ((elf = fake_elf(P, fptr)) == NULL ||
16637c478bd9Sstevel@tonic-gate 		    elf_kind(elf) != ELF_K_ELF ||
16647c478bd9Sstevel@tonic-gate 		    gelf_getehdr(elf, &ehdr) == NULL ||
166530da1432Sahl 		    elf_getshnum(elf, &nshdrs) == 0 ||
166630da1432Sahl 		    elf_getshstrndx(elf, &shstrndx) == 0 ||
166730da1432Sahl 		    (scn = elf_getscn(elf, shstrndx)) == NULL ||
16687c478bd9Sstevel@tonic-gate 		    (shdata = elf_getdata(scn, NULL)) == NULL) {
16697c478bd9Sstevel@tonic-gate 			dprintf("failed to fake up ELF file\n");
16707c478bd9Sstevel@tonic-gate 			goto bad;
16717c478bd9Sstevel@tonic-gate 		}
16727c478bd9Sstevel@tonic-gate 
16737c478bd9Sstevel@tonic-gate 	} else if (file_differs(P, elf, fptr)) {
16747c478bd9Sstevel@tonic-gate 		Elf *newelf;
16757c478bd9Sstevel@tonic-gate 
16767c478bd9Sstevel@tonic-gate 		/*
16777c478bd9Sstevel@tonic-gate 		 * Before we get too excited about this elf file, we'll check
16787c478bd9Sstevel@tonic-gate 		 * its checksum value against the value we have in memory. If
16797c478bd9Sstevel@tonic-gate 		 * they don't agree, we try to fake up a new elf file and
16807c478bd9Sstevel@tonic-gate 		 * proceed with that instead.
16817c478bd9Sstevel@tonic-gate 		 */
16827c478bd9Sstevel@tonic-gate 
16837c478bd9Sstevel@tonic-gate 		dprintf("ELF file %s (%lx) doesn't match in-core image\n",
16847c478bd9Sstevel@tonic-gate 		    fptr->file_pname,
16857c478bd9Sstevel@tonic-gate 		    (ulong_t)fptr->file_map->map_pmap.pr_vaddr);
16867c478bd9Sstevel@tonic-gate 
16877c478bd9Sstevel@tonic-gate 		if ((newelf = fake_elf(P, fptr)) == NULL ||
16887c478bd9Sstevel@tonic-gate 		    elf_kind(newelf) != ELF_K_ELF ||
16897c478bd9Sstevel@tonic-gate 		    gelf_getehdr(newelf, &ehdr) == NULL ||
169030da1432Sahl 		    elf_getshnum(newelf, &nshdrs) == 0 ||
169130da1432Sahl 		    elf_getshstrndx(newelf, &shstrndx) == 0 ||
169230da1432Sahl 		    (scn = elf_getscn(newelf, shstrndx)) == NULL ||
16937c478bd9Sstevel@tonic-gate 		    (shdata = elf_getdata(scn, NULL)) == NULL) {
16947c478bd9Sstevel@tonic-gate 			dprintf("failed to fake up ELF file\n");
16957c478bd9Sstevel@tonic-gate 		} else {
16967c478bd9Sstevel@tonic-gate 			(void) elf_end(elf);
16977c478bd9Sstevel@tonic-gate 			elf = newelf;
16987c478bd9Sstevel@tonic-gate 
16997c478bd9Sstevel@tonic-gate 			dprintf("switched to faked up ELF file\n");
17007c478bd9Sstevel@tonic-gate 		}
17017c478bd9Sstevel@tonic-gate 	}
17027c478bd9Sstevel@tonic-gate 
170330da1432Sahl 	if ((cache = malloc(nshdrs * sizeof (*cache))) == NULL) {
17047c478bd9Sstevel@tonic-gate 		dprintf("failed to malloc section cache for %s\n", objectfile);
17057c478bd9Sstevel@tonic-gate 		goto bad;
17067c478bd9Sstevel@tonic-gate 	}
17077c478bd9Sstevel@tonic-gate 
17087c478bd9Sstevel@tonic-gate 	dprintf("processing ELF file %s\n", objectfile);
17097c478bd9Sstevel@tonic-gate 	fptr->file_class = ehdr.e_ident[EI_CLASS];
17107c478bd9Sstevel@tonic-gate 	fptr->file_etype = ehdr.e_type;
17117c478bd9Sstevel@tonic-gate 	fptr->file_elf = elf;
1712f957ecc1Svb160487 	fptr->file_shstrs = shdata->d_buf;
1713f957ecc1Svb160487 	fptr->file_shstrsz = shdata->d_size;
17147c478bd9Sstevel@tonic-gate 
17157c478bd9Sstevel@tonic-gate 	/*
17167c478bd9Sstevel@tonic-gate 	 * Iterate through each section, caching its section header, data
17177c478bd9Sstevel@tonic-gate 	 * pointer, and name.  We use this for handling sh_link values below.
17187c478bd9Sstevel@tonic-gate 	 */
17197c478bd9Sstevel@tonic-gate 	for (cp = cache + 1, scn = NULL; scn = elf_nextscn(elf, scn); cp++) {
17209acbbeafSnn35248 		if (gelf_getshdr(scn, &cp->c_shdr) == NULL) {
17219acbbeafSnn35248 			dprintf("Pbuild_file_symtab: Failed to get section "
17229acbbeafSnn35248 			    "header\n");
17237c478bd9Sstevel@tonic-gate 			goto bad; /* Failed to get section header */
17249acbbeafSnn35248 		}
17257c478bd9Sstevel@tonic-gate 
17269acbbeafSnn35248 		if ((cp->c_data = elf_getdata(scn, NULL)) == NULL) {
17279acbbeafSnn35248 			dprintf("Pbuild_file_symtab: Failed to get section "
17289acbbeafSnn35248 			    "data\n");
17297c478bd9Sstevel@tonic-gate 			goto bad; /* Failed to get section data */
17309acbbeafSnn35248 		}
17317c478bd9Sstevel@tonic-gate 
17329acbbeafSnn35248 		if (cp->c_shdr.sh_name >= shdata->d_size) {
17339acbbeafSnn35248 			dprintf("Pbuild_file_symtab: corrupt section name");
17347c478bd9Sstevel@tonic-gate 			goto bad; /* Corrupt section name */
17359acbbeafSnn35248 		}
17367c478bd9Sstevel@tonic-gate 
17377c478bd9Sstevel@tonic-gate 		cp->c_name = (const char *)shdata->d_buf + cp->c_shdr.sh_name;
17387c478bd9Sstevel@tonic-gate 	}
17397c478bd9Sstevel@tonic-gate 
17407c478bd9Sstevel@tonic-gate 	/*
17417c478bd9Sstevel@tonic-gate 	 * Now iterate through the section cache in order to locate info
1742d51e9074Sab196087 	 * for the .symtab, .dynsym, .SUNW_ldynsym, .dynamic, .plt,
1743d51e9074Sab196087 	 * and .SUNW_ctf sections:
17447c478bd9Sstevel@tonic-gate 	 */
174530da1432Sahl 	for (i = 1, cp = cache + 1; i < nshdrs; i++, cp++) {
17467c478bd9Sstevel@tonic-gate 		GElf_Shdr *shp = &cp->c_shdr;
17477c478bd9Sstevel@tonic-gate 
17487c478bd9Sstevel@tonic-gate 		if (shp->sh_type == SHT_SYMTAB || shp->sh_type == SHT_DYNSYM) {
17497c478bd9Sstevel@tonic-gate 			sym_tbl_t *symp = shp->sh_type == SHT_SYMTAB ?
17507c478bd9Sstevel@tonic-gate 			    &fptr->file_symtab : &fptr->file_dynsym;
17517c478bd9Sstevel@tonic-gate 			/*
17527c478bd9Sstevel@tonic-gate 			 * It's possible that the we already got the symbol
17537c478bd9Sstevel@tonic-gate 			 * table from the core file itself. Either the file
17547c478bd9Sstevel@tonic-gate 			 * differs in which case our faked up elf file will
17557c478bd9Sstevel@tonic-gate 			 * only contain the dynsym (not the symtab) or the
17567c478bd9Sstevel@tonic-gate 			 * file matches in which case we'll just be replacing
17577c478bd9Sstevel@tonic-gate 			 * the symbol table we pulled out of the core file
17587c478bd9Sstevel@tonic-gate 			 * with an equivalent one. In either case, this
17597c478bd9Sstevel@tonic-gate 			 * check isn't essential, but it's a good idea.
17607c478bd9Sstevel@tonic-gate 			 */
1761d51e9074Sab196087 			if (symp->sym_data_pri == NULL) {
17629acbbeafSnn35248 				dprintf("Symbol table found for %s\n",
17639acbbeafSnn35248 				    objectfile);
1764d51e9074Sab196087 				symp->sym_data_pri = cp->c_data;
1765d51e9074Sab196087 				symp->sym_symn +=
1766d51e9074Sab196087 				    shp->sh_size / shp->sh_entsize;
17677c478bd9Sstevel@tonic-gate 				symp->sym_strs =
17687c478bd9Sstevel@tonic-gate 				    cache[shp->sh_link].c_data->d_buf;
17697c478bd9Sstevel@tonic-gate 				symp->sym_strsz =
17707c478bd9Sstevel@tonic-gate 				    cache[shp->sh_link].c_data->d_size;
1771d51e9074Sab196087 				symp->sym_hdr_pri = cp->c_shdr;
17727c478bd9Sstevel@tonic-gate 				symp->sym_strhdr = cache[shp->sh_link].c_shdr;
17739acbbeafSnn35248 			} else {
17749acbbeafSnn35248 				dprintf("Symbol table already there for %s\n",
17759acbbeafSnn35248 				    objectfile);
17767c478bd9Sstevel@tonic-gate 			}
1777d51e9074Sab196087 		} else if (shp->sh_type == SHT_SUNW_LDYNSYM) {
1778d51e9074Sab196087 			/* .SUNW_ldynsym section is auxiliary to .dynsym */
1779d51e9074Sab196087 			if (fptr->file_dynsym.sym_data_aux == NULL) {
1780d51e9074Sab196087 				dprintf(".SUNW_ldynsym symbol table"
1781d51e9074Sab196087 				    " found for %s\n", objectfile);
1782d51e9074Sab196087 				fptr->file_dynsym.sym_data_aux = cp->c_data;
1783d51e9074Sab196087 				fptr->file_dynsym.sym_symn_aux =
1784d51e9074Sab196087 				    shp->sh_size / shp->sh_entsize;
1785d51e9074Sab196087 				fptr->file_dynsym.sym_symn +=
1786d51e9074Sab196087 				    fptr->file_dynsym.sym_symn_aux;
1787d51e9074Sab196087 				fptr->file_dynsym.sym_hdr_aux = cp->c_shdr;
1788d51e9074Sab196087 			} else {
1789d51e9074Sab196087 				dprintf(".SUNW_ldynsym symbol table already"
1790d51e9074Sab196087 				    " there for %s\n", objectfile);
1791d51e9074Sab196087 			}
17927c478bd9Sstevel@tonic-gate 		} else if (shp->sh_type == SHT_DYNAMIC) {
17937c478bd9Sstevel@tonic-gate 			dyn = cp;
17947c478bd9Sstevel@tonic-gate 		} else if (strcmp(cp->c_name, ".plt") == 0) {
17957c478bd9Sstevel@tonic-gate 			plt = cp;
17967c478bd9Sstevel@tonic-gate 		} else if (strcmp(cp->c_name, ".SUNW_ctf") == 0) {
17977c478bd9Sstevel@tonic-gate 			/*
17987c478bd9Sstevel@tonic-gate 			 * Skip over bogus CTF sections so they don't come back
17997c478bd9Sstevel@tonic-gate 			 * to haunt us later.
18007c478bd9Sstevel@tonic-gate 			 */
18017c478bd9Sstevel@tonic-gate 			if (shp->sh_link == 0 ||
180230da1432Sahl 			    shp->sh_link >= nshdrs ||
18037c478bd9Sstevel@tonic-gate 			    (cache[shp->sh_link].c_shdr.sh_type != SHT_DYNSYM &&
18047c478bd9Sstevel@tonic-gate 			    cache[shp->sh_link].c_shdr.sh_type != SHT_SYMTAB)) {
18057c478bd9Sstevel@tonic-gate 				dprintf("Bad sh_link %d for "
18067c478bd9Sstevel@tonic-gate 				    "CTF\n", shp->sh_link);
18077c478bd9Sstevel@tonic-gate 				continue;
18087c478bd9Sstevel@tonic-gate 			}
18097c478bd9Sstevel@tonic-gate 			ctf = cp;
18107c478bd9Sstevel@tonic-gate 		}
18117c478bd9Sstevel@tonic-gate 	}
18127c478bd9Sstevel@tonic-gate 
18137c478bd9Sstevel@tonic-gate 	/*
18147c478bd9Sstevel@tonic-gate 	 * At this point, we've found all the symbol tables we're ever going
18157c478bd9Sstevel@tonic-gate 	 * to find: the ones in the loop above and possibly the symtab that
18167c478bd9Sstevel@tonic-gate 	 * was included in the core file. Before we perform any lookups, we
18177c478bd9Sstevel@tonic-gate 	 * create sorted versions to optimize for lookups.
18187c478bd9Sstevel@tonic-gate 	 */
18197c478bd9Sstevel@tonic-gate 	optimize_symtab(&fptr->file_symtab);
18207c478bd9Sstevel@tonic-gate 	optimize_symtab(&fptr->file_dynsym);
18217c478bd9Sstevel@tonic-gate 
18227c478bd9Sstevel@tonic-gate 	/*
18237c478bd9Sstevel@tonic-gate 	 * Fill in the base address of the text mapping for shared libraries.
18247c478bd9Sstevel@tonic-gate 	 * This allows us to translate symbols before librtld_db is ready.
18257c478bd9Sstevel@tonic-gate 	 */
18267c478bd9Sstevel@tonic-gate 	if (fptr->file_etype == ET_DYN) {
18277c478bd9Sstevel@tonic-gate 		fptr->file_dyn_base = fptr->file_map->map_pmap.pr_vaddr -
18287c478bd9Sstevel@tonic-gate 		    fptr->file_map->map_pmap.pr_offset;
18299acbbeafSnn35248 		dprintf("setting file_dyn_base for %s to %lx\n",
18309acbbeafSnn35248 		    objectfile, (long)fptr->file_dyn_base);
18317c478bd9Sstevel@tonic-gate 	}
18327c478bd9Sstevel@tonic-gate 
18337c478bd9Sstevel@tonic-gate 	/*
18347c478bd9Sstevel@tonic-gate 	 * Record the CTF section information in the file info structure.
18357c478bd9Sstevel@tonic-gate 	 */
18367c478bd9Sstevel@tonic-gate 	if (ctf != NULL) {
18377c478bd9Sstevel@tonic-gate 		fptr->file_ctf_off = ctf->c_shdr.sh_offset;
18387c478bd9Sstevel@tonic-gate 		fptr->file_ctf_size = ctf->c_shdr.sh_size;
18397c478bd9Sstevel@tonic-gate 		if (ctf->c_shdr.sh_link != 0 &&
18407c478bd9Sstevel@tonic-gate 		    cache[ctf->c_shdr.sh_link].c_shdr.sh_type == SHT_DYNSYM)
18417c478bd9Sstevel@tonic-gate 			fptr->file_ctf_dyn = 1;
18427c478bd9Sstevel@tonic-gate 	}
18437c478bd9Sstevel@tonic-gate 
18447c478bd9Sstevel@tonic-gate 	if (fptr->file_lo == NULL)
18457c478bd9Sstevel@tonic-gate 		goto done; /* Nothing else to do if no load object info */
18467c478bd9Sstevel@tonic-gate 
18477c478bd9Sstevel@tonic-gate 	/*
18487c478bd9Sstevel@tonic-gate 	 * If the object is a shared library and we have a different rl_base
18497c478bd9Sstevel@tonic-gate 	 * value, reset file_dyn_base according to librtld_db's information.
18507c478bd9Sstevel@tonic-gate 	 */
18517c478bd9Sstevel@tonic-gate 	if (fptr->file_etype == ET_DYN &&
18527c478bd9Sstevel@tonic-gate 	    fptr->file_lo->rl_base != fptr->file_dyn_base) {
18539acbbeafSnn35248 		dprintf("resetting file_dyn_base for %s to %lx\n",
18549acbbeafSnn35248 		    objectfile, (long)fptr->file_lo->rl_base);
18557c478bd9Sstevel@tonic-gate 		fptr->file_dyn_base = fptr->file_lo->rl_base;
18567c478bd9Sstevel@tonic-gate 	}
18577c478bd9Sstevel@tonic-gate 
18587c478bd9Sstevel@tonic-gate 	/*
18597c478bd9Sstevel@tonic-gate 	 * Fill in the PLT information for this file if a PLT symbol is found.
18607c478bd9Sstevel@tonic-gate 	 */
18617c478bd9Sstevel@tonic-gate 	if (sym_by_name(&fptr->file_dynsym, "_PROCEDURE_LINKAGE_TABLE_", &s,
18627c478bd9Sstevel@tonic-gate 	    NULL) != NULL) {
18637c478bd9Sstevel@tonic-gate 		fptr->file_plt_base = s.st_value + fptr->file_dyn_base;
18647c478bd9Sstevel@tonic-gate 		fptr->file_plt_size = (plt != NULL) ? plt->c_shdr.sh_size : 0;
18657c478bd9Sstevel@tonic-gate 
18667c478bd9Sstevel@tonic-gate 		/*
18677c478bd9Sstevel@tonic-gate 		 * Bring the load object up to date; it is the only way the
18687c478bd9Sstevel@tonic-gate 		 * user has to access the PLT data. The PLT information in the
18697c478bd9Sstevel@tonic-gate 		 * rd_loadobj_t is not set in the call to map_iter() (the
18707c478bd9Sstevel@tonic-gate 		 * callback for rd_loadobj_iter) where we set file_lo.
18717c478bd9Sstevel@tonic-gate 		 */
18727c478bd9Sstevel@tonic-gate 		fptr->file_lo->rl_plt_base = fptr->file_plt_base;
18737c478bd9Sstevel@tonic-gate 		fptr->file_lo->rl_plt_size = fptr->file_plt_size;
18747c478bd9Sstevel@tonic-gate 
18757c478bd9Sstevel@tonic-gate 		dprintf("PLT found at %p, size = %lu\n",
18767c478bd9Sstevel@tonic-gate 		    (void *)fptr->file_plt_base, (ulong_t)fptr->file_plt_size);
18777c478bd9Sstevel@tonic-gate 	}
18787c478bd9Sstevel@tonic-gate 
18797c478bd9Sstevel@tonic-gate 	/*
18807c478bd9Sstevel@tonic-gate 	 * Fill in the PLT information.
18817c478bd9Sstevel@tonic-gate 	 */
18827c478bd9Sstevel@tonic-gate 	if (dyn != NULL) {
18837c478bd9Sstevel@tonic-gate 		uintptr_t dynaddr = dyn->c_shdr.sh_addr + fptr->file_dyn_base;
18847c478bd9Sstevel@tonic-gate 		size_t ndyn = dyn->c_shdr.sh_size / dyn->c_shdr.sh_entsize;
18857c478bd9Sstevel@tonic-gate 		GElf_Dyn d;
18867c478bd9Sstevel@tonic-gate 
18877c478bd9Sstevel@tonic-gate 		for (i = 0; i < ndyn; i++) {
18887c478bd9Sstevel@tonic-gate 			if (gelf_getdyn(dyn->c_data, i, &d) != NULL &&
18897c478bd9Sstevel@tonic-gate 			    d.d_tag == DT_JMPREL) {
18909acbbeafSnn35248 				dprintf("DT_JMPREL is %p\n",
18919acbbeafSnn35248 				    (void *)(uintptr_t)d.d_un.d_ptr);
18927c478bd9Sstevel@tonic-gate 				fptr->file_jmp_rel =
18937c478bd9Sstevel@tonic-gate 				    d.d_un.d_ptr + fptr->file_dyn_base;
18947c478bd9Sstevel@tonic-gate 				break;
18957c478bd9Sstevel@tonic-gate 			}
18967c478bd9Sstevel@tonic-gate 		}
18977c478bd9Sstevel@tonic-gate 
18987c478bd9Sstevel@tonic-gate 		dprintf("_DYNAMIC found at %p, %lu entries, DT_JMPREL = %p\n",
18997c478bd9Sstevel@tonic-gate 		    (void *)dynaddr, (ulong_t)ndyn, (void *)fptr->file_jmp_rel);
19007c478bd9Sstevel@tonic-gate 	}
19017c478bd9Sstevel@tonic-gate 
19027c478bd9Sstevel@tonic-gate done:
19037c478bd9Sstevel@tonic-gate 	free(cache);
19047c478bd9Sstevel@tonic-gate 	return;
19057c478bd9Sstevel@tonic-gate 
19067c478bd9Sstevel@tonic-gate bad:
19077c478bd9Sstevel@tonic-gate 	if (cache != NULL)
19087c478bd9Sstevel@tonic-gate 		free(cache);
19097c478bd9Sstevel@tonic-gate 
19107c478bd9Sstevel@tonic-gate 	(void) elf_end(elf);
19117c478bd9Sstevel@tonic-gate 	fptr->file_elf = NULL;
19127c478bd9Sstevel@tonic-gate 	if (fptr->file_elfmem != NULL) {
19137c478bd9Sstevel@tonic-gate 		free(fptr->file_elfmem);
19147c478bd9Sstevel@tonic-gate 		fptr->file_elfmem = NULL;
19157c478bd9Sstevel@tonic-gate 	}
19167c478bd9Sstevel@tonic-gate 	(void) close(fptr->file_fd);
19177c478bd9Sstevel@tonic-gate 	fptr->file_fd = -1;
19187c478bd9Sstevel@tonic-gate }
19197c478bd9Sstevel@tonic-gate 
19207c478bd9Sstevel@tonic-gate /*
19217c478bd9Sstevel@tonic-gate  * Given a process virtual address, return the map_info_t containing it.
19227c478bd9Sstevel@tonic-gate  * If none found, return NULL.
19237c478bd9Sstevel@tonic-gate  */
19247c478bd9Sstevel@tonic-gate map_info_t *
19257c478bd9Sstevel@tonic-gate Paddr2mptr(struct ps_prochandle *P, uintptr_t addr)
19267c478bd9Sstevel@tonic-gate {
19277c478bd9Sstevel@tonic-gate 	int lo = 0;
19287c478bd9Sstevel@tonic-gate 	int hi = P->map_count - 1;
19297c478bd9Sstevel@tonic-gate 	int mid;
19307c478bd9Sstevel@tonic-gate 	map_info_t *mp;
19317c478bd9Sstevel@tonic-gate 
19327c478bd9Sstevel@tonic-gate 	while (lo <= hi) {
19337c478bd9Sstevel@tonic-gate 
19347c478bd9Sstevel@tonic-gate 		mid = (lo + hi) / 2;
19357c478bd9Sstevel@tonic-gate 		mp = &P->mappings[mid];
19367c478bd9Sstevel@tonic-gate 
19377c478bd9Sstevel@tonic-gate 		/* check that addr is in [vaddr, vaddr + size) */
19387c478bd9Sstevel@tonic-gate 		if ((addr - mp->map_pmap.pr_vaddr) < mp->map_pmap.pr_size)
19397c478bd9Sstevel@tonic-gate 			return (mp);
19407c478bd9Sstevel@tonic-gate 
19417c478bd9Sstevel@tonic-gate 		if (addr < mp->map_pmap.pr_vaddr)
19427c478bd9Sstevel@tonic-gate 			hi = mid - 1;
19437c478bd9Sstevel@tonic-gate 		else
19447c478bd9Sstevel@tonic-gate 			lo = mid + 1;
19457c478bd9Sstevel@tonic-gate 	}
19467c478bd9Sstevel@tonic-gate 
19477c478bd9Sstevel@tonic-gate 	return (NULL);
19487c478bd9Sstevel@tonic-gate }
19497c478bd9Sstevel@tonic-gate 
19507c478bd9Sstevel@tonic-gate /*
19517c478bd9Sstevel@tonic-gate  * Return the map_info_t for the executable file.
19527c478bd9Sstevel@tonic-gate  * If not found, return NULL.
19537c478bd9Sstevel@tonic-gate  */
19547c478bd9Sstevel@tonic-gate static map_info_t *
19557c478bd9Sstevel@tonic-gate exec_map(struct ps_prochandle *P)
19567c478bd9Sstevel@tonic-gate {
19577c478bd9Sstevel@tonic-gate 	uint_t i;
19587c478bd9Sstevel@tonic-gate 	map_info_t *mptr;
19597c478bd9Sstevel@tonic-gate 	map_info_t *mold = NULL;
19607c478bd9Sstevel@tonic-gate 	file_info_t *fptr;
19617c478bd9Sstevel@tonic-gate 	uintptr_t base;
19627c478bd9Sstevel@tonic-gate 
19637c478bd9Sstevel@tonic-gate 	for (i = 0, mptr = P->mappings; i < P->map_count; i++, mptr++) {
19647c478bd9Sstevel@tonic-gate 		if (mptr->map_pmap.pr_mapname[0] == '\0')
19657c478bd9Sstevel@tonic-gate 			continue;
19667c478bd9Sstevel@tonic-gate 		if (strcmp(mptr->map_pmap.pr_mapname, "a.out") == 0) {
19677c478bd9Sstevel@tonic-gate 			if ((fptr = mptr->map_file) != NULL &&
19687c478bd9Sstevel@tonic-gate 			    fptr->file_lo != NULL) {
19697c478bd9Sstevel@tonic-gate 				base = fptr->file_lo->rl_base;
19707c478bd9Sstevel@tonic-gate 				if (base >= mptr->map_pmap.pr_vaddr &&
19717c478bd9Sstevel@tonic-gate 				    base < mptr->map_pmap.pr_vaddr +
19727c478bd9Sstevel@tonic-gate 				    mptr->map_pmap.pr_size)	/* text space */
19737c478bd9Sstevel@tonic-gate 					return (mptr);
19747c478bd9Sstevel@tonic-gate 				mold = mptr;	/* must be the data */
19757c478bd9Sstevel@tonic-gate 				continue;
19767c478bd9Sstevel@tonic-gate 			}
19777c478bd9Sstevel@tonic-gate 			/* This is a poor way to test for text space */
19787c478bd9Sstevel@tonic-gate 			if (!(mptr->map_pmap.pr_mflags & MA_EXEC) ||
19797c478bd9Sstevel@tonic-gate 			    (mptr->map_pmap.pr_mflags & MA_WRITE)) {
19807c478bd9Sstevel@tonic-gate 				mold = mptr;
19817c478bd9Sstevel@tonic-gate 				continue;
19827c478bd9Sstevel@tonic-gate 			}
19837c478bd9Sstevel@tonic-gate 			return (mptr);
19847c478bd9Sstevel@tonic-gate 		}
19857c478bd9Sstevel@tonic-gate 	}
19867c478bd9Sstevel@tonic-gate 
19877c478bd9Sstevel@tonic-gate 	return (mold);
19887c478bd9Sstevel@tonic-gate }
19897c478bd9Sstevel@tonic-gate 
19907c478bd9Sstevel@tonic-gate /*
19917c478bd9Sstevel@tonic-gate  * Given a shared object name, return the map_info_t for it.  If no matching
19927c478bd9Sstevel@tonic-gate  * object is found, return NULL.  Normally, the link maps contain the full
19937c478bd9Sstevel@tonic-gate  * object pathname, e.g. /usr/lib/libc.so.1.  We allow the object name to
19947c478bd9Sstevel@tonic-gate  * take one of the following forms:
19957c478bd9Sstevel@tonic-gate  *
19967c478bd9Sstevel@tonic-gate  * 1. An exact match (i.e. a full pathname): "/usr/lib/libc.so.1"
19977c478bd9Sstevel@tonic-gate  * 2. An exact basename match: "libc.so.1"
19987c478bd9Sstevel@tonic-gate  * 3. An initial basename match up to a '.' suffix: "libc.so" or "libc"
19997c478bd9Sstevel@tonic-gate  * 4. The literal string "a.out" is an alias for the executable mapping
20007c478bd9Sstevel@tonic-gate  *
20017c478bd9Sstevel@tonic-gate  * The third case is a convenience for callers and may not be necessary.
20027c478bd9Sstevel@tonic-gate  *
20037c478bd9Sstevel@tonic-gate  * As the exact same object name may be loaded on different link maps (see
20047c478bd9Sstevel@tonic-gate  * dlmopen(3DL)), we also allow the caller to resolve the object name by
20057c478bd9Sstevel@tonic-gate  * specifying a particular link map id.  If lmid is PR_LMID_EVERY, the
20067c478bd9Sstevel@tonic-gate  * first matching name will be returned, regardless of the link map id.
20077c478bd9Sstevel@tonic-gate  */
20087c478bd9Sstevel@tonic-gate static map_info_t *
20097c478bd9Sstevel@tonic-gate object_to_map(struct ps_prochandle *P, Lmid_t lmid, const char *objname)
20107c478bd9Sstevel@tonic-gate {
20117c478bd9Sstevel@tonic-gate 	map_info_t *mp;
20127c478bd9Sstevel@tonic-gate 	file_info_t *fp;
20137c478bd9Sstevel@tonic-gate 	size_t objlen;
20147c478bd9Sstevel@tonic-gate 	uint_t i;
20157c478bd9Sstevel@tonic-gate 
20167c478bd9Sstevel@tonic-gate 	/*
20179acbbeafSnn35248 	 * If we have no rtld_db, then always treat a request as one for all
20189acbbeafSnn35248 	 * link maps.
20199acbbeafSnn35248 	 */
20209acbbeafSnn35248 	if (P->rap == NULL)
20219acbbeafSnn35248 		lmid = PR_LMID_EVERY;
20229acbbeafSnn35248 
20239acbbeafSnn35248 	/*
20247c478bd9Sstevel@tonic-gate 	 * First pass: look for exact matches of the entire pathname or
20257c478bd9Sstevel@tonic-gate 	 * basename (cases 1 and 2 above):
20267c478bd9Sstevel@tonic-gate 	 */
20277c478bd9Sstevel@tonic-gate 	for (i = 0, mp = P->mappings; i < P->map_count; i++, mp++) {
20287c478bd9Sstevel@tonic-gate 
20297c478bd9Sstevel@tonic-gate 		if (mp->map_pmap.pr_mapname[0] == '\0' ||
20307c478bd9Sstevel@tonic-gate 		    (fp = mp->map_file) == NULL || fp->file_lname == NULL)
20317c478bd9Sstevel@tonic-gate 			continue;
20327c478bd9Sstevel@tonic-gate 
20337c478bd9Sstevel@tonic-gate 		if (lmid != PR_LMID_EVERY &&
20347c478bd9Sstevel@tonic-gate 		    (fp->file_lo == NULL || lmid != fp->file_lo->rl_lmident))
20357c478bd9Sstevel@tonic-gate 			continue;
20367c478bd9Sstevel@tonic-gate 
20377c478bd9Sstevel@tonic-gate 		/*
20387c478bd9Sstevel@tonic-gate 		 * If we match, return the primary text mapping; otherwise
20397c478bd9Sstevel@tonic-gate 		 * just return the mapping we matched.
20407c478bd9Sstevel@tonic-gate 		 */
20417c478bd9Sstevel@tonic-gate 		if (strcmp(fp->file_lname, objname) == 0 ||
20427c478bd9Sstevel@tonic-gate 		    strcmp(fp->file_lbase, objname) == 0)
20437c478bd9Sstevel@tonic-gate 			return (fp->file_map ? fp->file_map : mp);
20447c478bd9Sstevel@tonic-gate 	}
20457c478bd9Sstevel@tonic-gate 
20467c478bd9Sstevel@tonic-gate 	objlen = strlen(objname);
20477c478bd9Sstevel@tonic-gate 
20487c478bd9Sstevel@tonic-gate 	/*
20497c478bd9Sstevel@tonic-gate 	 * Second pass: look for partial matches (case 3 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' ||
20547c478bd9Sstevel@tonic-gate 		    (fp = mp->map_file) == NULL || fp->file_lname == NULL)
20557c478bd9Sstevel@tonic-gate 			continue;
20567c478bd9Sstevel@tonic-gate 
20577c478bd9Sstevel@tonic-gate 		if (lmid != PR_LMID_EVERY &&
20587c478bd9Sstevel@tonic-gate 		    (fp->file_lo == NULL || lmid != fp->file_lo->rl_lmident))
20597c478bd9Sstevel@tonic-gate 			continue;
20607c478bd9Sstevel@tonic-gate 
20617c478bd9Sstevel@tonic-gate 		/*
20627c478bd9Sstevel@tonic-gate 		 * If we match, return the primary text mapping; otherwise
20637c478bd9Sstevel@tonic-gate 		 * just return the mapping we matched.
20647c478bd9Sstevel@tonic-gate 		 */
20657c478bd9Sstevel@tonic-gate 		if (strncmp(fp->file_lbase, objname, objlen) == 0 &&
20667c478bd9Sstevel@tonic-gate 		    fp->file_lbase[objlen] == '.')
20677c478bd9Sstevel@tonic-gate 			return (fp->file_map ? fp->file_map : mp);
20687c478bd9Sstevel@tonic-gate 	}
20697c478bd9Sstevel@tonic-gate 
20707c478bd9Sstevel@tonic-gate 	/*
20717c478bd9Sstevel@tonic-gate 	 * One last check: we allow "a.out" to always alias the executable,
20727c478bd9Sstevel@tonic-gate 	 * assuming this name was not in use for something else.
20737c478bd9Sstevel@tonic-gate 	 */
20747c478bd9Sstevel@tonic-gate 	if ((lmid == PR_LMID_EVERY || lmid == LM_ID_BASE) &&
20757c478bd9Sstevel@tonic-gate 	    (strcmp(objname, "a.out") == 0))
20767c478bd9Sstevel@tonic-gate 		return (P->map_exec);
20777c478bd9Sstevel@tonic-gate 
20787c478bd9Sstevel@tonic-gate 	return (NULL);
20797c478bd9Sstevel@tonic-gate }
20807c478bd9Sstevel@tonic-gate 
20817c478bd9Sstevel@tonic-gate static map_info_t *
20827c478bd9Sstevel@tonic-gate object_name_to_map(struct ps_prochandle *P, Lmid_t lmid, const char *name)
20837c478bd9Sstevel@tonic-gate {
20847c478bd9Sstevel@tonic-gate 	map_info_t *mptr;
20857c478bd9Sstevel@tonic-gate 
20867c478bd9Sstevel@tonic-gate 	if (!P->info_valid)
20877c478bd9Sstevel@tonic-gate 		Pupdate_maps(P);
20887c478bd9Sstevel@tonic-gate 
20897c478bd9Sstevel@tonic-gate 	if (P->map_exec == NULL && ((mptr = Paddr2mptr(P,
20907c478bd9Sstevel@tonic-gate 	    Pgetauxval(P, AT_ENTRY))) != NULL || (mptr = exec_map(P)) != NULL))
20917c478bd9Sstevel@tonic-gate 		P->map_exec = mptr;
20927c478bd9Sstevel@tonic-gate 
20937c478bd9Sstevel@tonic-gate 	if (P->map_ldso == NULL && (mptr = Paddr2mptr(P,
20947c478bd9Sstevel@tonic-gate 	    Pgetauxval(P, AT_BASE))) != NULL)
20957c478bd9Sstevel@tonic-gate 		P->map_ldso = mptr;
20967c478bd9Sstevel@tonic-gate 
20977c478bd9Sstevel@tonic-gate 	if (name == PR_OBJ_EXEC)
20987c478bd9Sstevel@tonic-gate 		mptr = P->map_exec;
20997c478bd9Sstevel@tonic-gate 	else if (name == PR_OBJ_LDSO)
21007c478bd9Sstevel@tonic-gate 		mptr = P->map_ldso;
2101b45b146bSjhaslam 	else if (Prd_agent(P) != NULL || P->state == PS_IDLE)
21029acbbeafSnn35248 		mptr = object_to_map(P, lmid, name);
2103b45b146bSjhaslam 	else
2104b45b146bSjhaslam 		mptr = NULL;
21057c478bd9Sstevel@tonic-gate 
21067c478bd9Sstevel@tonic-gate 	return (mptr);
21077c478bd9Sstevel@tonic-gate }
21087c478bd9Sstevel@tonic-gate 
21097c478bd9Sstevel@tonic-gate /*
21107c478bd9Sstevel@tonic-gate  * When two symbols are found by address, decide which one is to be preferred.
21117c478bd9Sstevel@tonic-gate  */
21127c478bd9Sstevel@tonic-gate static GElf_Sym *
21137c478bd9Sstevel@tonic-gate sym_prefer(GElf_Sym *sym1, char *name1, GElf_Sym *sym2, char *name2)
21147c478bd9Sstevel@tonic-gate {
21157c478bd9Sstevel@tonic-gate 	/*
21167c478bd9Sstevel@tonic-gate 	 * Prefer the non-NULL symbol.
21177c478bd9Sstevel@tonic-gate 	 */
21187c478bd9Sstevel@tonic-gate 	if (sym1 == NULL)
21197c478bd9Sstevel@tonic-gate 		return (sym2);
21207c478bd9Sstevel@tonic-gate 	if (sym2 == NULL)
21217c478bd9Sstevel@tonic-gate 		return (sym1);
21227c478bd9Sstevel@tonic-gate 
21237c478bd9Sstevel@tonic-gate 	/*
21247c478bd9Sstevel@tonic-gate 	 * Defer to the sort ordering...
21257c478bd9Sstevel@tonic-gate 	 */
21267c478bd9Sstevel@tonic-gate 	return (byaddr_cmp_common(sym1, name1, sym2, name2) <= 0 ? sym1 : sym2);
21277c478bd9Sstevel@tonic-gate }
21287c478bd9Sstevel@tonic-gate 
21297c478bd9Sstevel@tonic-gate /*
2130d7755b5aSrh87107  * Use a binary search to do the work of sym_by_addr().
21317c478bd9Sstevel@tonic-gate  */
21327c478bd9Sstevel@tonic-gate static GElf_Sym *
2133d7755b5aSrh87107 sym_by_addr_binary(sym_tbl_t *symtab, GElf_Addr addr, GElf_Sym *symp,
2134d7755b5aSrh87107     uint_t *idp)
21357c478bd9Sstevel@tonic-gate {
21367c478bd9Sstevel@tonic-gate 	GElf_Sym sym, osym;
21377c478bd9Sstevel@tonic-gate 	uint_t i, oid, *byaddr = symtab->sym_byaddr;
21387c478bd9Sstevel@tonic-gate 	int min, max, mid, omid, found = 0;
21397c478bd9Sstevel@tonic-gate 
2140d51e9074Sab196087 	if (symtab->sym_data_pri == NULL || symtab->sym_count == 0)
21417c478bd9Sstevel@tonic-gate 		return (NULL);
21427c478bd9Sstevel@tonic-gate 
21437c478bd9Sstevel@tonic-gate 	min = 0;
21447c478bd9Sstevel@tonic-gate 	max = symtab->sym_count - 1;
21457c478bd9Sstevel@tonic-gate 	osym.st_value = 0;
21467c478bd9Sstevel@tonic-gate 
21477c478bd9Sstevel@tonic-gate 	/*
21487c478bd9Sstevel@tonic-gate 	 * We can't return when we've found a match, we have to continue
21497c478bd9Sstevel@tonic-gate 	 * searching for the closest matching symbol.
21507c478bd9Sstevel@tonic-gate 	 */
21517c478bd9Sstevel@tonic-gate 	while (min <= max) {
21527c478bd9Sstevel@tonic-gate 		mid = (max + min) / 2;
21537c478bd9Sstevel@tonic-gate 
21547c478bd9Sstevel@tonic-gate 		i = byaddr[mid];
2155d51e9074Sab196087 		(void) symtab_getsym(symtab, i, &sym);
21567c478bd9Sstevel@tonic-gate 
21577c478bd9Sstevel@tonic-gate 		if (addr >= sym.st_value &&
21587c478bd9Sstevel@tonic-gate 		    addr < sym.st_value + sym.st_size &&
21597c478bd9Sstevel@tonic-gate 		    (!found || sym.st_value > osym.st_value)) {
21607c478bd9Sstevel@tonic-gate 			osym = sym;
21617c478bd9Sstevel@tonic-gate 			omid = mid;
21627c478bd9Sstevel@tonic-gate 			oid = i;
21637c478bd9Sstevel@tonic-gate 			found = 1;
21647c478bd9Sstevel@tonic-gate 		}
21657c478bd9Sstevel@tonic-gate 
21667c478bd9Sstevel@tonic-gate 		if (addr < sym.st_value)
21677c478bd9Sstevel@tonic-gate 			max = mid - 1;
21687c478bd9Sstevel@tonic-gate 		else
21697c478bd9Sstevel@tonic-gate 			min = mid + 1;
21707c478bd9Sstevel@tonic-gate 	}
21717c478bd9Sstevel@tonic-gate 
21727c478bd9Sstevel@tonic-gate 	if (!found)
21737c478bd9Sstevel@tonic-gate 		return (NULL);
21747c478bd9Sstevel@tonic-gate 
21757c478bd9Sstevel@tonic-gate 	/*
21767c478bd9Sstevel@tonic-gate 	 * There may be many symbols with identical values so we walk
21777c478bd9Sstevel@tonic-gate 	 * backward in the byaddr table to find the best match.
21787c478bd9Sstevel@tonic-gate 	 */
21797c478bd9Sstevel@tonic-gate 	do {
21807c478bd9Sstevel@tonic-gate 		sym = osym;
21817c478bd9Sstevel@tonic-gate 		i = oid;
21827c478bd9Sstevel@tonic-gate 
21837c478bd9Sstevel@tonic-gate 		if (omid == 0)
21847c478bd9Sstevel@tonic-gate 			break;
21857c478bd9Sstevel@tonic-gate 
21867c478bd9Sstevel@tonic-gate 		oid = byaddr[--omid];
2187d51e9074Sab196087 		(void) symtab_getsym(symtab, oid, &osym);
21887c478bd9Sstevel@tonic-gate 	} while (addr >= osym.st_value &&
21897c478bd9Sstevel@tonic-gate 	    addr < sym.st_value + osym.st_size &&
21907c478bd9Sstevel@tonic-gate 	    osym.st_value == sym.st_value);
21917c478bd9Sstevel@tonic-gate 
21927c478bd9Sstevel@tonic-gate 	*symp = sym;
21937c478bd9Sstevel@tonic-gate 	if (idp != NULL)
21947c478bd9Sstevel@tonic-gate 		*idp = i;
21957c478bd9Sstevel@tonic-gate 	return (symp);
21967c478bd9Sstevel@tonic-gate }
21977c478bd9Sstevel@tonic-gate 
21987c478bd9Sstevel@tonic-gate /*
2199d7755b5aSrh87107  * Use a linear search to do the work of sym_by_addr().
22007c478bd9Sstevel@tonic-gate  */
22017c478bd9Sstevel@tonic-gate static GElf_Sym *
2202d7755b5aSrh87107 sym_by_addr_linear(sym_tbl_t *symtab, GElf_Addr addr, GElf_Sym *symbolp,
2203d7755b5aSrh87107     uint_t *idp)
2204d7755b5aSrh87107 {
2205d7755b5aSrh87107 	size_t symn = symtab->sym_symn;
2206d7755b5aSrh87107 	char *strs = symtab->sym_strs;
2207d7755b5aSrh87107 	GElf_Sym sym, *symp = NULL;
2208d7755b5aSrh87107 	GElf_Sym osym, *osymp = NULL;
2209d7755b5aSrh87107 	int i, id;
2210d7755b5aSrh87107 
2211d7755b5aSrh87107 	if (symtab->sym_data_pri == NULL || symn == 0 || strs == NULL)
2212d7755b5aSrh87107 		return (NULL);
2213d7755b5aSrh87107 
2214d7755b5aSrh87107 	for (i = 0; i < symn; i++) {
2215d7755b5aSrh87107 		if ((symp = symtab_getsym(symtab, i, &sym)) != NULL) {
2216d7755b5aSrh87107 			if (addr >= sym.st_value &&
2217d7755b5aSrh87107 			    addr < sym.st_value + sym.st_size) {
2218d7755b5aSrh87107 				if (osymp)
2219d7755b5aSrh87107 					symp = sym_prefer(
2220d7755b5aSrh87107 					    symp, strs + symp->st_name,
2221d7755b5aSrh87107 					    osymp, strs + osymp->st_name);
2222d7755b5aSrh87107 				if (symp != osymp) {
2223d7755b5aSrh87107 					osym = sym;
2224d7755b5aSrh87107 					osymp = &osym;
2225d7755b5aSrh87107 					id = i;
2226d7755b5aSrh87107 				}
2227d7755b5aSrh87107 			}
2228d7755b5aSrh87107 		}
2229d7755b5aSrh87107 	}
2230d7755b5aSrh87107 	if (osymp) {
2231d7755b5aSrh87107 		*symbolp = osym;
2232d7755b5aSrh87107 		if (idp)
2233d7755b5aSrh87107 			*idp = id;
2234d7755b5aSrh87107 		return (symbolp);
2235d7755b5aSrh87107 	}
2236d7755b5aSrh87107 	return (NULL);
2237d7755b5aSrh87107 }
2238d7755b5aSrh87107 
2239d7755b5aSrh87107 /*
2240d7755b5aSrh87107  * Look up a symbol by address in the specified symbol table.
2241d7755b5aSrh87107  * Adjustment to 'addr' must already have been made for the
2242d7755b5aSrh87107  * offset of the symbol if this is a dynamic library symbol table.
2243d7755b5aSrh87107  *
2244d7755b5aSrh87107  * Use a linear or a binary search depending on whether or not we
2245d7755b5aSrh87107  * chose to sort the table in optimize_symtab().
2246d7755b5aSrh87107  */
2247d7755b5aSrh87107 static GElf_Sym *
2248d7755b5aSrh87107 sym_by_addr(sym_tbl_t *symtab, GElf_Addr addr, GElf_Sym *symp, uint_t *idp)
2249d7755b5aSrh87107 {
2250d7755b5aSrh87107 	if (_libproc_no_qsort) {
2251d7755b5aSrh87107 		return (sym_by_addr_linear(symtab, addr, symp, idp));
2252d7755b5aSrh87107 	} else {
2253d7755b5aSrh87107 		return (sym_by_addr_binary(symtab, addr, symp, idp));
2254d7755b5aSrh87107 	}
2255d7755b5aSrh87107 }
2256d7755b5aSrh87107 
2257d7755b5aSrh87107 /*
2258d7755b5aSrh87107  * Use a binary search to do the work of sym_by_name().
2259d7755b5aSrh87107  */
2260d7755b5aSrh87107 static GElf_Sym *
2261d7755b5aSrh87107 sym_by_name_binary(sym_tbl_t *symtab, const char *name, GElf_Sym *symp,
2262d7755b5aSrh87107     uint_t *idp)
22637c478bd9Sstevel@tonic-gate {
22647c478bd9Sstevel@tonic-gate 	char *strs = symtab->sym_strs;
22657c478bd9Sstevel@tonic-gate 	uint_t i, *byname = symtab->sym_byname;
22667c478bd9Sstevel@tonic-gate 	int min, mid, max, cmp;
22677c478bd9Sstevel@tonic-gate 
2268d51e9074Sab196087 	if (symtab->sym_data_pri == NULL || strs == NULL ||
2269d51e9074Sab196087 	    symtab->sym_count == 0)
22707c478bd9Sstevel@tonic-gate 		return (NULL);
22717c478bd9Sstevel@tonic-gate 
22727c478bd9Sstevel@tonic-gate 	min = 0;
22737c478bd9Sstevel@tonic-gate 	max = symtab->sym_count - 1;
22747c478bd9Sstevel@tonic-gate 
22757c478bd9Sstevel@tonic-gate 	while (min <= max) {
22767c478bd9Sstevel@tonic-gate 		mid = (max + min) / 2;
22777c478bd9Sstevel@tonic-gate 
22787c478bd9Sstevel@tonic-gate 		i = byname[mid];
2279d51e9074Sab196087 		(void) symtab_getsym(symtab, i, symp);
22807c478bd9Sstevel@tonic-gate 
22817c478bd9Sstevel@tonic-gate 		if ((cmp = strcmp(name, strs + symp->st_name)) == 0) {
22827c478bd9Sstevel@tonic-gate 			if (idp != NULL)
22837c478bd9Sstevel@tonic-gate 				*idp = i;
22847c478bd9Sstevel@tonic-gate 			return (symp);
22857c478bd9Sstevel@tonic-gate 		}
22867c478bd9Sstevel@tonic-gate 
22877c478bd9Sstevel@tonic-gate 		if (cmp < 0)
22887c478bd9Sstevel@tonic-gate 			max = mid - 1;
22897c478bd9Sstevel@tonic-gate 		else
22907c478bd9Sstevel@tonic-gate 			min = mid + 1;
22917c478bd9Sstevel@tonic-gate 	}
22927c478bd9Sstevel@tonic-gate 
22937c478bd9Sstevel@tonic-gate 	return (NULL);
22947c478bd9Sstevel@tonic-gate }
22957c478bd9Sstevel@tonic-gate 
22967c478bd9Sstevel@tonic-gate /*
2297d7755b5aSrh87107  * Use a linear search to do the work of sym_by_name().
2298d7755b5aSrh87107  */
2299d7755b5aSrh87107 static GElf_Sym *
2300d7755b5aSrh87107 sym_by_name_linear(sym_tbl_t *symtab, const char *name, GElf_Sym *symp,
2301d7755b5aSrh87107     uint_t *idp)
2302d7755b5aSrh87107 {
2303d7755b5aSrh87107 	size_t symn = symtab->sym_symn;
2304d7755b5aSrh87107 	char *strs = symtab->sym_strs;
2305d7755b5aSrh87107 	int i;
2306d7755b5aSrh87107 
2307d7755b5aSrh87107 	if (symtab->sym_data_pri == NULL || symn == 0 || strs == NULL)
2308d7755b5aSrh87107 		return (NULL);
2309d7755b5aSrh87107 
2310d7755b5aSrh87107 	for (i = 0; i < symn; i++) {
2311d7755b5aSrh87107 		if (symtab_getsym(symtab, i, symp) &&
2312d7755b5aSrh87107 		    strcmp(name, strs + symp->st_name) == 0) {
2313d7755b5aSrh87107 			if (idp)
2314d7755b5aSrh87107 				*idp = i;
2315d7755b5aSrh87107 			return (symp);
2316d7755b5aSrh87107 		}
2317d7755b5aSrh87107 	}
2318d7755b5aSrh87107 
2319d7755b5aSrh87107 	return (NULL);
2320d7755b5aSrh87107 }
2321d7755b5aSrh87107 
2322d7755b5aSrh87107 /*
2323d7755b5aSrh87107  * Look up a symbol by name in the specified symbol table.
2324d7755b5aSrh87107  *
2325d7755b5aSrh87107  * Use a linear or a binary search depending on whether or not we
2326d7755b5aSrh87107  * chose to sort the table in optimize_symtab().
2327d7755b5aSrh87107  */
2328d7755b5aSrh87107 static GElf_Sym *
2329d7755b5aSrh87107 sym_by_name(sym_tbl_t *symtab, const char *name, GElf_Sym *symp, uint_t *idp)
2330d7755b5aSrh87107 {
2331d7755b5aSrh87107 	if (_libproc_no_qsort) {
2332d7755b5aSrh87107 		return (sym_by_name_linear(symtab, name, symp, idp));
2333d7755b5aSrh87107 	} else {
2334d7755b5aSrh87107 		return (sym_by_name_binary(symtab, name, symp, idp));
2335d7755b5aSrh87107 	}
2336d7755b5aSrh87107 }
2337d7755b5aSrh87107 
2338d7755b5aSrh87107 /*
23397c478bd9Sstevel@tonic-gate  * Search the process symbol tables looking for a symbol whose
23407c478bd9Sstevel@tonic-gate  * value to value+size contain the address specified by addr.
23417c478bd9Sstevel@tonic-gate  * Return values are:
23427c478bd9Sstevel@tonic-gate  *	sym_name_buffer containing the symbol name
23437c478bd9Sstevel@tonic-gate  *	GElf_Sym symbol table entry
23447c478bd9Sstevel@tonic-gate  *	prsyminfo_t ancillary symbol information
23457c478bd9Sstevel@tonic-gate  * Returns 0 on success, -1 on failure.
23467c478bd9Sstevel@tonic-gate  */
23477c478bd9Sstevel@tonic-gate int
23487c478bd9Sstevel@tonic-gate Pxlookup_by_addr(
23497c478bd9Sstevel@tonic-gate 	struct ps_prochandle *P,
23507c478bd9Sstevel@tonic-gate 	uintptr_t addr,			/* process address being sought */
23517c478bd9Sstevel@tonic-gate 	char *sym_name_buffer,		/* buffer for the symbol name */
23527c478bd9Sstevel@tonic-gate 	size_t bufsize,			/* size of sym_name_buffer */
23537c478bd9Sstevel@tonic-gate 	GElf_Sym *symbolp,		/* returned symbol table entry */
23547c478bd9Sstevel@tonic-gate 	prsyminfo_t *sip)		/* returned symbol info */
23557c478bd9Sstevel@tonic-gate {
23567c478bd9Sstevel@tonic-gate 	GElf_Sym	*symp;
23577c478bd9Sstevel@tonic-gate 	char		*name;
23587c478bd9Sstevel@tonic-gate 	GElf_Sym	sym1, *sym1p = NULL;
23597c478bd9Sstevel@tonic-gate 	GElf_Sym	sym2, *sym2p = NULL;
23607c478bd9Sstevel@tonic-gate 	char		*name1 = NULL;
23617c478bd9Sstevel@tonic-gate 	char		*name2 = NULL;
23627c478bd9Sstevel@tonic-gate 	uint_t		i1;
23637c478bd9Sstevel@tonic-gate 	uint_t		i2;
23647c478bd9Sstevel@tonic-gate 	map_info_t	*mptr;
23657c478bd9Sstevel@tonic-gate 	file_info_t	*fptr;
23667c478bd9Sstevel@tonic-gate 
23677c478bd9Sstevel@tonic-gate 	(void) Prd_agent(P);
23687c478bd9Sstevel@tonic-gate 
23697c478bd9Sstevel@tonic-gate 	if ((mptr = Paddr2mptr(P, addr)) == NULL ||	/* no such address */
23707c478bd9Sstevel@tonic-gate 	    (fptr = build_map_symtab(P, mptr)) == NULL || /* no mapped file */
23717c478bd9Sstevel@tonic-gate 	    fptr->file_elf == NULL)			/* not an ELF file */
23727c478bd9Sstevel@tonic-gate 		return (-1);
23737c478bd9Sstevel@tonic-gate 
23747c478bd9Sstevel@tonic-gate 	/*
23757c478bd9Sstevel@tonic-gate 	 * Adjust the address by the load object base address in
23767c478bd9Sstevel@tonic-gate 	 * case the address turns out to be in a shared library.
23777c478bd9Sstevel@tonic-gate 	 */
23787c478bd9Sstevel@tonic-gate 	addr -= fptr->file_dyn_base;
23797c478bd9Sstevel@tonic-gate 
23807c478bd9Sstevel@tonic-gate 	/*
23817c478bd9Sstevel@tonic-gate 	 * Search both symbol tables, symtab first, then dynsym.
23827c478bd9Sstevel@tonic-gate 	 */
23837c478bd9Sstevel@tonic-gate 	if ((sym1p = sym_by_addr(&fptr->file_symtab, addr, &sym1, &i1)) != NULL)
23847c478bd9Sstevel@tonic-gate 		name1 = fptr->file_symtab.sym_strs + sym1.st_name;
23857c478bd9Sstevel@tonic-gate 	if ((sym2p = sym_by_addr(&fptr->file_dynsym, addr, &sym2, &i2)) != NULL)
23867c478bd9Sstevel@tonic-gate 		name2 = fptr->file_dynsym.sym_strs + sym2.st_name;
23877c478bd9Sstevel@tonic-gate 
23887c478bd9Sstevel@tonic-gate 	if ((symp = sym_prefer(sym1p, name1, sym2p, name2)) == NULL)
23897c478bd9Sstevel@tonic-gate 		return (-1);
23907c478bd9Sstevel@tonic-gate 
23917c478bd9Sstevel@tonic-gate 	name = (symp == sym1p) ? name1 : name2;
23927c478bd9Sstevel@tonic-gate 	if (bufsize > 0) {
23937c478bd9Sstevel@tonic-gate 		(void) strncpy(sym_name_buffer, name, bufsize);
23947c478bd9Sstevel@tonic-gate 		sym_name_buffer[bufsize - 1] = '\0';
23957c478bd9Sstevel@tonic-gate 	}
23967c478bd9Sstevel@tonic-gate 
23977c478bd9Sstevel@tonic-gate 	*symbolp = *symp;
23987c478bd9Sstevel@tonic-gate 	if (sip != NULL) {
23997c478bd9Sstevel@tonic-gate 		sip->prs_name = bufsize == 0 ? NULL : sym_name_buffer;
24007c478bd9Sstevel@tonic-gate 		sip->prs_object = fptr->file_lbase;
24017c478bd9Sstevel@tonic-gate 		sip->prs_id = (symp == sym1p) ? i1 : i2;
24027c478bd9Sstevel@tonic-gate 		sip->prs_table = (symp == sym1p) ? PR_SYMTAB : PR_DYNSYM;
24037c478bd9Sstevel@tonic-gate 		sip->prs_lmid = (fptr->file_lo == NULL) ? LM_ID_BASE :
24047c478bd9Sstevel@tonic-gate 		    fptr->file_lo->rl_lmident;
24057c478bd9Sstevel@tonic-gate 	}
24067c478bd9Sstevel@tonic-gate 
24077c478bd9Sstevel@tonic-gate 	if (GELF_ST_TYPE(symbolp->st_info) != STT_TLS)
24087c478bd9Sstevel@tonic-gate 		symbolp->st_value += fptr->file_dyn_base;
24097c478bd9Sstevel@tonic-gate 
24107c478bd9Sstevel@tonic-gate 	return (0);
24117c478bd9Sstevel@tonic-gate }
24127c478bd9Sstevel@tonic-gate 
24137c478bd9Sstevel@tonic-gate int
24147c478bd9Sstevel@tonic-gate Plookup_by_addr(struct ps_prochandle *P, uintptr_t addr, char *buf, size_t size,
24157c478bd9Sstevel@tonic-gate     GElf_Sym *symp)
24167c478bd9Sstevel@tonic-gate {
24177c478bd9Sstevel@tonic-gate 	return (Pxlookup_by_addr(P, addr, buf, size, symp, NULL));
24187c478bd9Sstevel@tonic-gate }
24197c478bd9Sstevel@tonic-gate 
24207c478bd9Sstevel@tonic-gate /*
24217c478bd9Sstevel@tonic-gate  * Search the process symbol tables looking for a symbol whose name matches the
24227c478bd9Sstevel@tonic-gate  * specified name and whose object and link map optionally match the specified
24237c478bd9Sstevel@tonic-gate  * parameters.  On success, the function returns 0 and fills in the GElf_Sym
24247c478bd9Sstevel@tonic-gate  * symbol table entry.  On failure, -1 is returned.
24257c478bd9Sstevel@tonic-gate  */
24267c478bd9Sstevel@tonic-gate int
24277c478bd9Sstevel@tonic-gate Pxlookup_by_name(
24287c478bd9Sstevel@tonic-gate 	struct ps_prochandle *P,
24297c478bd9Sstevel@tonic-gate 	Lmid_t lmid,			/* link map to match, or -1 for any */
24307c478bd9Sstevel@tonic-gate 	const char *oname,		/* load object name */
24317c478bd9Sstevel@tonic-gate 	const char *sname,		/* symbol name */
24327c478bd9Sstevel@tonic-gate 	GElf_Sym *symp,			/* returned symbol table entry */
24337c478bd9Sstevel@tonic-gate 	prsyminfo_t *sip)		/* returned symbol info */
24347c478bd9Sstevel@tonic-gate {
24357c478bd9Sstevel@tonic-gate 	map_info_t *mptr;
24367c478bd9Sstevel@tonic-gate 	file_info_t *fptr;
24377c478bd9Sstevel@tonic-gate 	int cnt;
24387c478bd9Sstevel@tonic-gate 
24397c478bd9Sstevel@tonic-gate 	GElf_Sym sym;
24407c478bd9Sstevel@tonic-gate 	prsyminfo_t si;
24417c478bd9Sstevel@tonic-gate 	int rv = -1;
24427c478bd9Sstevel@tonic-gate 	uint_t id;
24437c478bd9Sstevel@tonic-gate 
24447c478bd9Sstevel@tonic-gate 	if (oname == PR_OBJ_EVERY) {
24457c478bd9Sstevel@tonic-gate 		/* create all the file_info_t's for all the mappings */
24467c478bd9Sstevel@tonic-gate 		(void) Prd_agent(P);
24477c478bd9Sstevel@tonic-gate 		cnt = P->num_files;
24487c478bd9Sstevel@tonic-gate 		fptr = list_next(&P->file_head);
24497c478bd9Sstevel@tonic-gate 	} else {
24507c478bd9Sstevel@tonic-gate 		cnt = 1;
24517c478bd9Sstevel@tonic-gate 		if ((mptr = object_name_to_map(P, lmid, oname)) == NULL ||
24527c478bd9Sstevel@tonic-gate 		    (fptr = build_map_symtab(P, mptr)) == NULL)
24537c478bd9Sstevel@tonic-gate 			return (-1);
24547c478bd9Sstevel@tonic-gate 	}
24557c478bd9Sstevel@tonic-gate 
24567c478bd9Sstevel@tonic-gate 	/*
24577c478bd9Sstevel@tonic-gate 	 * Iterate through the loaded object files and look for the symbol
24587c478bd9Sstevel@tonic-gate 	 * name in the .symtab and .dynsym of each.  If we encounter a match
24597c478bd9Sstevel@tonic-gate 	 * with SHN_UNDEF, keep looking in hopes of finding a better match.
24607c478bd9Sstevel@tonic-gate 	 * This means that a name such as "puts" will match the puts function
24617c478bd9Sstevel@tonic-gate 	 * in libc instead of matching the puts PLT entry in the a.out file.
24627c478bd9Sstevel@tonic-gate 	 */
24637c478bd9Sstevel@tonic-gate 	for (; cnt > 0; cnt--, fptr = list_next(fptr)) {
24647c478bd9Sstevel@tonic-gate 		Pbuild_file_symtab(P, fptr);
24657c478bd9Sstevel@tonic-gate 
24667c478bd9Sstevel@tonic-gate 		if (fptr->file_elf == NULL)
24677c478bd9Sstevel@tonic-gate 			continue;
24687c478bd9Sstevel@tonic-gate 
24697c478bd9Sstevel@tonic-gate 		if (lmid != PR_LMID_EVERY && fptr->file_lo != NULL &&
24707c478bd9Sstevel@tonic-gate 		    lmid != fptr->file_lo->rl_lmident)
24717c478bd9Sstevel@tonic-gate 			continue;
24727c478bd9Sstevel@tonic-gate 
2473d51e9074Sab196087 		if (fptr->file_symtab.sym_data_pri != NULL &&
24747c478bd9Sstevel@tonic-gate 		    sym_by_name(&fptr->file_symtab, sname, symp, &id)) {
24757c478bd9Sstevel@tonic-gate 			if (sip != NULL) {
24767c478bd9Sstevel@tonic-gate 				sip->prs_id = id;
24777c478bd9Sstevel@tonic-gate 				sip->prs_table = PR_SYMTAB;
24787c478bd9Sstevel@tonic-gate 				sip->prs_object = oname;
24797c478bd9Sstevel@tonic-gate 				sip->prs_name = sname;
24807c478bd9Sstevel@tonic-gate 				sip->prs_lmid = fptr->file_lo == NULL ?
24817c478bd9Sstevel@tonic-gate 				    LM_ID_BASE : fptr->file_lo->rl_lmident;
24827c478bd9Sstevel@tonic-gate 			}
2483d51e9074Sab196087 		} else if (fptr->file_dynsym.sym_data_pri != NULL &&
24847c478bd9Sstevel@tonic-gate 		    sym_by_name(&fptr->file_dynsym, sname, symp, &id)) {
24857c478bd9Sstevel@tonic-gate 			if (sip != NULL) {
24867c478bd9Sstevel@tonic-gate 				sip->prs_id = id;
24877c478bd9Sstevel@tonic-gate 				sip->prs_table = PR_DYNSYM;
24887c478bd9Sstevel@tonic-gate 				sip->prs_object = oname;
24897c478bd9Sstevel@tonic-gate 				sip->prs_name = sname;
24907c478bd9Sstevel@tonic-gate 				sip->prs_lmid = fptr->file_lo == NULL ?
24917c478bd9Sstevel@tonic-gate 				    LM_ID_BASE : fptr->file_lo->rl_lmident;
24927c478bd9Sstevel@tonic-gate 			}
24937c478bd9Sstevel@tonic-gate 		} else {
24947c478bd9Sstevel@tonic-gate 			continue;
24957c478bd9Sstevel@tonic-gate 		}
24967c478bd9Sstevel@tonic-gate 
24977c478bd9Sstevel@tonic-gate 		if (GELF_ST_TYPE(symp->st_info) != STT_TLS)
24987c478bd9Sstevel@tonic-gate 			symp->st_value += fptr->file_dyn_base;
24997c478bd9Sstevel@tonic-gate 
25007c478bd9Sstevel@tonic-gate 		if (symp->st_shndx != SHN_UNDEF)
25017c478bd9Sstevel@tonic-gate 			return (0);
25027c478bd9Sstevel@tonic-gate 
25037c478bd9Sstevel@tonic-gate 		if (rv != 0) {
25047c478bd9Sstevel@tonic-gate 			if (sip != NULL)
25057c478bd9Sstevel@tonic-gate 				si = *sip;
25067c478bd9Sstevel@tonic-gate 			sym = *symp;
25077c478bd9Sstevel@tonic-gate 			rv = 0;
25087c478bd9Sstevel@tonic-gate 		}
25097c478bd9Sstevel@tonic-gate 	}
25107c478bd9Sstevel@tonic-gate 
25117c478bd9Sstevel@tonic-gate 	if (rv == 0) {
25127c478bd9Sstevel@tonic-gate 		if (sip != NULL)
25137c478bd9Sstevel@tonic-gate 			*sip = si;
25147c478bd9Sstevel@tonic-gate 		*symp = sym;
25157c478bd9Sstevel@tonic-gate 	}
25167c478bd9Sstevel@tonic-gate 
25177c478bd9Sstevel@tonic-gate 	return (rv);
25187c478bd9Sstevel@tonic-gate }
25197c478bd9Sstevel@tonic-gate 
25207c478bd9Sstevel@tonic-gate /*
25217c478bd9Sstevel@tonic-gate  * Search the process symbol tables looking for a symbol whose name matches the
25227c478bd9Sstevel@tonic-gate  * specified name, but without any restriction on the link map id.
25237c478bd9Sstevel@tonic-gate  */
25247c478bd9Sstevel@tonic-gate int
25257c478bd9Sstevel@tonic-gate Plookup_by_name(struct ps_prochandle *P, const char *object,
25267c478bd9Sstevel@tonic-gate 	const char *symbol, GElf_Sym *symp)
25277c478bd9Sstevel@tonic-gate {
25287c478bd9Sstevel@tonic-gate 	return (Pxlookup_by_name(P, PR_LMID_EVERY, object, symbol, symp, NULL));
25297c478bd9Sstevel@tonic-gate }
25307c478bd9Sstevel@tonic-gate 
25317c478bd9Sstevel@tonic-gate /*
25327c478bd9Sstevel@tonic-gate  * Iterate over the process's address space mappings.
25337c478bd9Sstevel@tonic-gate  */
25347c478bd9Sstevel@tonic-gate int
25357c478bd9Sstevel@tonic-gate Pmapping_iter(struct ps_prochandle *P, proc_map_f *func, void *cd)
25367c478bd9Sstevel@tonic-gate {
25377c478bd9Sstevel@tonic-gate 	map_info_t *mptr;
25387c478bd9Sstevel@tonic-gate 	file_info_t *fptr;
25397c478bd9Sstevel@tonic-gate 	char *object_name;
25407c478bd9Sstevel@tonic-gate 	int rc = 0;
25417c478bd9Sstevel@tonic-gate 	int i;
25427c478bd9Sstevel@tonic-gate 
25437c478bd9Sstevel@tonic-gate 	/* create all the file_info_t's for all the mappings */
25447c478bd9Sstevel@tonic-gate 	(void) Prd_agent(P);
25457c478bd9Sstevel@tonic-gate 
25467c478bd9Sstevel@tonic-gate 	for (i = 0, mptr = P->mappings; i < P->map_count; i++, mptr++) {
25477c478bd9Sstevel@tonic-gate 		if ((fptr = mptr->map_file) == NULL)
25487c478bd9Sstevel@tonic-gate 			object_name = NULL;
25497c478bd9Sstevel@tonic-gate 		else
25507c478bd9Sstevel@tonic-gate 			object_name = fptr->file_lname;
25517c478bd9Sstevel@tonic-gate 		if ((rc = func(cd, &mptr->map_pmap, object_name)) != 0)
25527c478bd9Sstevel@tonic-gate 			return (rc);
25537c478bd9Sstevel@tonic-gate 	}
25547c478bd9Sstevel@tonic-gate 	return (0);
25557c478bd9Sstevel@tonic-gate }
25567c478bd9Sstevel@tonic-gate 
25577c478bd9Sstevel@tonic-gate /*
25587c478bd9Sstevel@tonic-gate  * Iterate over the process's mapped objects.
25597c478bd9Sstevel@tonic-gate  */
25607c478bd9Sstevel@tonic-gate int
25617c478bd9Sstevel@tonic-gate Pobject_iter(struct ps_prochandle *P, proc_map_f *func, void *cd)
25627c478bd9Sstevel@tonic-gate {
25637c478bd9Sstevel@tonic-gate 	map_info_t *mptr;
25647c478bd9Sstevel@tonic-gate 	file_info_t *fptr;
25657c478bd9Sstevel@tonic-gate 	uint_t cnt;
25667c478bd9Sstevel@tonic-gate 	int rc = 0;
25677c478bd9Sstevel@tonic-gate 
25687c478bd9Sstevel@tonic-gate 	(void) Prd_agent(P); /* create file_info_t's for all the mappings */
25697c478bd9Sstevel@tonic-gate 	Pupdate_maps(P);
25707c478bd9Sstevel@tonic-gate 
25717c478bd9Sstevel@tonic-gate 	for (cnt = P->num_files, fptr = list_next(&P->file_head);
25727c478bd9Sstevel@tonic-gate 	    cnt; cnt--, fptr = list_next(fptr)) {
25737c478bd9Sstevel@tonic-gate 
25747c478bd9Sstevel@tonic-gate 		const char *lname = fptr->file_lname ? fptr->file_lname : "";
25757c478bd9Sstevel@tonic-gate 
25767c478bd9Sstevel@tonic-gate 		if ((mptr = fptr->file_map) == NULL)
25777c478bd9Sstevel@tonic-gate 			continue;
25787c478bd9Sstevel@tonic-gate 
25797c478bd9Sstevel@tonic-gate 		if ((rc = func(cd, &mptr->map_pmap, lname)) != 0)
25807c478bd9Sstevel@tonic-gate 			return (rc);
25817c478bd9Sstevel@tonic-gate 	}
25827c478bd9Sstevel@tonic-gate 	return (0);
25837c478bd9Sstevel@tonic-gate }
25847c478bd9Sstevel@tonic-gate 
25857c478bd9Sstevel@tonic-gate /*
25867c478bd9Sstevel@tonic-gate  * Given a virtual address, return the name of the underlying
25877c478bd9Sstevel@tonic-gate  * mapped object (file), as provided by the dynamic linker.
25887c478bd9Sstevel@tonic-gate  * Return NULL on failure (no underlying shared library).
25897c478bd9Sstevel@tonic-gate  */
25907c478bd9Sstevel@tonic-gate char *
25917c478bd9Sstevel@tonic-gate Pobjname(struct ps_prochandle *P, uintptr_t addr,
25927c478bd9Sstevel@tonic-gate 	char *buffer, size_t bufsize)
25937c478bd9Sstevel@tonic-gate {
25947c478bd9Sstevel@tonic-gate 	map_info_t *mptr;
25957c478bd9Sstevel@tonic-gate 	file_info_t *fptr;
25967c478bd9Sstevel@tonic-gate 
25977c478bd9Sstevel@tonic-gate 	/* create all the file_info_t's for all the mappings */
25987c478bd9Sstevel@tonic-gate 	(void) Prd_agent(P);
25997c478bd9Sstevel@tonic-gate 
26007c478bd9Sstevel@tonic-gate 	if ((mptr = Paddr2mptr(P, addr)) != NULL &&
26017c478bd9Sstevel@tonic-gate 	    (fptr = mptr->map_file) != NULL &&
26027c478bd9Sstevel@tonic-gate 	    fptr->file_lname != NULL) {
26037c478bd9Sstevel@tonic-gate 		(void) strncpy(buffer, fptr->file_lname, bufsize);
26047c478bd9Sstevel@tonic-gate 		if (strlen(fptr->file_lname) >= bufsize)
26057c478bd9Sstevel@tonic-gate 			buffer[bufsize-1] = '\0';
26067c478bd9Sstevel@tonic-gate 		return (buffer);
26077c478bd9Sstevel@tonic-gate 	}
26087c478bd9Sstevel@tonic-gate 	return (NULL);
26097c478bd9Sstevel@tonic-gate }
26107c478bd9Sstevel@tonic-gate 
26117c478bd9Sstevel@tonic-gate /*
26127c478bd9Sstevel@tonic-gate  * Given a virtual address, return the link map id of the underlying mapped
26137c478bd9Sstevel@tonic-gate  * object (file), as provided by the dynamic linker.  Return -1 on failure.
26147c478bd9Sstevel@tonic-gate  */
26157c478bd9Sstevel@tonic-gate int
26167c478bd9Sstevel@tonic-gate Plmid(struct ps_prochandle *P, uintptr_t addr, Lmid_t *lmidp)
26177c478bd9Sstevel@tonic-gate {
26187c478bd9Sstevel@tonic-gate 	map_info_t *mptr;
26197c478bd9Sstevel@tonic-gate 	file_info_t *fptr;
26207c478bd9Sstevel@tonic-gate 
26217c478bd9Sstevel@tonic-gate 	/* create all the file_info_t's for all the mappings */
26227c478bd9Sstevel@tonic-gate 	(void) Prd_agent(P);
26237c478bd9Sstevel@tonic-gate 
26247c478bd9Sstevel@tonic-gate 	if ((mptr = Paddr2mptr(P, addr)) != NULL &&
26257c478bd9Sstevel@tonic-gate 	    (fptr = mptr->map_file) != NULL && fptr->file_lo != NULL) {
26267c478bd9Sstevel@tonic-gate 		*lmidp = fptr->file_lo->rl_lmident;
26277c478bd9Sstevel@tonic-gate 		return (0);
26287c478bd9Sstevel@tonic-gate 	}
26297c478bd9Sstevel@tonic-gate 
26307c478bd9Sstevel@tonic-gate 	return (-1);
26317c478bd9Sstevel@tonic-gate }
26327c478bd9Sstevel@tonic-gate 
26337c478bd9Sstevel@tonic-gate /*
26347c478bd9Sstevel@tonic-gate  * Given an object name and optional lmid, iterate over the object's symbols.
26357c478bd9Sstevel@tonic-gate  * If which == PR_SYMTAB, search the normal symbol table.
26367c478bd9Sstevel@tonic-gate  * If which == PR_DYNSYM, search the dynamic symbol table.
26377c478bd9Sstevel@tonic-gate  */
26387c478bd9Sstevel@tonic-gate static int
26397c478bd9Sstevel@tonic-gate Psymbol_iter_com(struct ps_prochandle *P, Lmid_t lmid, const char *object_name,
26407c478bd9Sstevel@tonic-gate     int which, int mask, pr_order_t order, proc_xsym_f *func, void *cd)
26417c478bd9Sstevel@tonic-gate {
26427c478bd9Sstevel@tonic-gate 	GElf_Sym sym;
2643f957ecc1Svb160487 	GElf_Shdr shdr;
26447c478bd9Sstevel@tonic-gate 	map_info_t *mptr;
26457c478bd9Sstevel@tonic-gate 	file_info_t *fptr;
26467c478bd9Sstevel@tonic-gate 	sym_tbl_t *symtab;
26477c478bd9Sstevel@tonic-gate 	size_t symn;
26487c478bd9Sstevel@tonic-gate 	const char *strs;
26497c478bd9Sstevel@tonic-gate 	size_t strsz;
26507c478bd9Sstevel@tonic-gate 	prsyminfo_t si;
26517c478bd9Sstevel@tonic-gate 	int rv;
26527c478bd9Sstevel@tonic-gate 	uint_t *map, i, count, ndx;
26537c478bd9Sstevel@tonic-gate 
26547c478bd9Sstevel@tonic-gate 	if ((mptr = object_name_to_map(P, lmid, object_name)) == NULL)
26557c478bd9Sstevel@tonic-gate 		return (-1);
26567c478bd9Sstevel@tonic-gate 
26577c478bd9Sstevel@tonic-gate 	if ((fptr = build_map_symtab(P, mptr)) == NULL || /* no mapped file */
26587c478bd9Sstevel@tonic-gate 	    fptr->file_elf == NULL)			/* not an ELF file */
26597c478bd9Sstevel@tonic-gate 		return (-1);
26607c478bd9Sstevel@tonic-gate 
26617c478bd9Sstevel@tonic-gate 	/*
26627c478bd9Sstevel@tonic-gate 	 * Search the specified symbol table.
26637c478bd9Sstevel@tonic-gate 	 */
26647c478bd9Sstevel@tonic-gate 	switch (which) {
26657c478bd9Sstevel@tonic-gate 	case PR_SYMTAB:
26667c478bd9Sstevel@tonic-gate 		symtab = &fptr->file_symtab;
26677c478bd9Sstevel@tonic-gate 		si.prs_table = PR_SYMTAB;
26687c478bd9Sstevel@tonic-gate 		break;
26697c478bd9Sstevel@tonic-gate 	case PR_DYNSYM:
26707c478bd9Sstevel@tonic-gate 		symtab = &fptr->file_dynsym;
26717c478bd9Sstevel@tonic-gate 		si.prs_table = PR_DYNSYM;
26727c478bd9Sstevel@tonic-gate 		break;
26737c478bd9Sstevel@tonic-gate 	default:
26747c478bd9Sstevel@tonic-gate 		return (-1);
26757c478bd9Sstevel@tonic-gate 	}
26767c478bd9Sstevel@tonic-gate 
26777c478bd9Sstevel@tonic-gate 	si.prs_object = object_name;
26787c478bd9Sstevel@tonic-gate 	si.prs_lmid = fptr->file_lo == NULL ?
26797c478bd9Sstevel@tonic-gate 	    LM_ID_BASE : fptr->file_lo->rl_lmident;
26807c478bd9Sstevel@tonic-gate 
26817c478bd9Sstevel@tonic-gate 	symn = symtab->sym_symn;
26827c478bd9Sstevel@tonic-gate 	strs = symtab->sym_strs;
26837c478bd9Sstevel@tonic-gate 	strsz = symtab->sym_strsz;
26847c478bd9Sstevel@tonic-gate 
26857c478bd9Sstevel@tonic-gate 	switch (order) {
26867c478bd9Sstevel@tonic-gate 	case PRO_NATURAL:
26877c478bd9Sstevel@tonic-gate 		map = NULL;
26887c478bd9Sstevel@tonic-gate 		count = symn;
26897c478bd9Sstevel@tonic-gate 		break;
26907c478bd9Sstevel@tonic-gate 	case PRO_BYNAME:
26917c478bd9Sstevel@tonic-gate 		map = symtab->sym_byname;
26927c478bd9Sstevel@tonic-gate 		count = symtab->sym_count;
26937c478bd9Sstevel@tonic-gate 		break;
26947c478bd9Sstevel@tonic-gate 	case PRO_BYADDR:
26957c478bd9Sstevel@tonic-gate 		map = symtab->sym_byaddr;
26967c478bd9Sstevel@tonic-gate 		count = symtab->sym_count;
26977c478bd9Sstevel@tonic-gate 		break;
26987c478bd9Sstevel@tonic-gate 	default:
26997c478bd9Sstevel@tonic-gate 		return (-1);
27007c478bd9Sstevel@tonic-gate 	}
27017c478bd9Sstevel@tonic-gate 
2702d51e9074Sab196087 	if (symtab->sym_data_pri == NULL || strs == NULL || count == 0)
2703d51e9074Sab196087 		return (-1);
2704d51e9074Sab196087 
27057c478bd9Sstevel@tonic-gate 	rv = 0;
27067c478bd9Sstevel@tonic-gate 
27077c478bd9Sstevel@tonic-gate 	for (i = 0; i < count; i++) {
27087c478bd9Sstevel@tonic-gate 		ndx = map == NULL ? i : map[i];
2709d51e9074Sab196087 		if (symtab_getsym(symtab, ndx, &sym) != NULL) {
27107c478bd9Sstevel@tonic-gate 			uint_t s_bind, s_type, type;
27117c478bd9Sstevel@tonic-gate 
27127c478bd9Sstevel@tonic-gate 			if (sym.st_name >= strsz)	/* invalid st_name */
27137c478bd9Sstevel@tonic-gate 				continue;
27147c478bd9Sstevel@tonic-gate 
27157c478bd9Sstevel@tonic-gate 			s_bind = GELF_ST_BIND(sym.st_info);
27167c478bd9Sstevel@tonic-gate 			s_type = GELF_ST_TYPE(sym.st_info);
27177c478bd9Sstevel@tonic-gate 
27187c478bd9Sstevel@tonic-gate 			/*
27197c478bd9Sstevel@tonic-gate 			 * In case you haven't already guessed, this relies on
27207c478bd9Sstevel@tonic-gate 			 * the bitmask used in <libproc.h> for encoding symbol
27217c478bd9Sstevel@tonic-gate 			 * type and binding matching the order of STB and STT
27227c478bd9Sstevel@tonic-gate 			 * constants in <sys/elf.h>.  ELF can't change without
27237c478bd9Sstevel@tonic-gate 			 * breaking binary compatibility, so I think this is
27247c478bd9Sstevel@tonic-gate 			 * reasonably fair game.
27257c478bd9Sstevel@tonic-gate 			 */
27267c478bd9Sstevel@tonic-gate 			if (s_bind < STB_NUM && s_type < STT_NUM) {
27277c478bd9Sstevel@tonic-gate 				type = (1 << (s_type + 8)) | (1 << s_bind);
27287c478bd9Sstevel@tonic-gate 				if ((type & ~mask) != 0)
27297c478bd9Sstevel@tonic-gate 					continue;
27307c478bd9Sstevel@tonic-gate 			} else
27317c478bd9Sstevel@tonic-gate 				continue; /* Invalid type or binding */
27327c478bd9Sstevel@tonic-gate 
27337c478bd9Sstevel@tonic-gate 			if (GELF_ST_TYPE(sym.st_info) != STT_TLS)
27347c478bd9Sstevel@tonic-gate 				sym.st_value += fptr->file_dyn_base;
27357c478bd9Sstevel@tonic-gate 
27367c478bd9Sstevel@tonic-gate 			si.prs_name = strs + sym.st_name;
2737f957ecc1Svb160487 
2738f957ecc1Svb160487 			/*
2739f957ecc1Svb160487 			 * If symbol's type is STT_SECTION, then try to lookup
2740f957ecc1Svb160487 			 * the name of the corresponding section.
2741f957ecc1Svb160487 			 */
2742f957ecc1Svb160487 			if (GELF_ST_TYPE(sym.st_info) == STT_SECTION &&
2743f957ecc1Svb160487 			    fptr->file_shstrs != NULL &&
2744f957ecc1Svb160487 			    gelf_getshdr(elf_getscn(fptr->file_elf,
2745f957ecc1Svb160487 			    sym.st_shndx), &shdr) != NULL &&
2746f957ecc1Svb160487 			    shdr.sh_name != 0 &&
2747f957ecc1Svb160487 			    shdr.sh_name < fptr->file_shstrsz)
2748f957ecc1Svb160487 				si.prs_name = fptr->file_shstrs + shdr.sh_name;
2749f957ecc1Svb160487 
27507c478bd9Sstevel@tonic-gate 			si.prs_id = ndx;
2751f957ecc1Svb160487 			if ((rv = func(cd, &sym, si.prs_name, &si)) != 0)
27527c478bd9Sstevel@tonic-gate 				break;
27537c478bd9Sstevel@tonic-gate 		}
27547c478bd9Sstevel@tonic-gate 	}
27557c478bd9Sstevel@tonic-gate 
27567c478bd9Sstevel@tonic-gate 	return (rv);
27577c478bd9Sstevel@tonic-gate }
27587c478bd9Sstevel@tonic-gate 
27597c478bd9Sstevel@tonic-gate int
27607c478bd9Sstevel@tonic-gate Pxsymbol_iter(struct ps_prochandle *P, Lmid_t lmid, const char *object_name,
27617c478bd9Sstevel@tonic-gate     int which, int mask, proc_xsym_f *func, void *cd)
27627c478bd9Sstevel@tonic-gate {
27637c478bd9Sstevel@tonic-gate 	return (Psymbol_iter_com(P, lmid, object_name, which, mask,
27647c478bd9Sstevel@tonic-gate 	    PRO_NATURAL, func, cd));
27657c478bd9Sstevel@tonic-gate }
27667c478bd9Sstevel@tonic-gate 
27677c478bd9Sstevel@tonic-gate int
27687c478bd9Sstevel@tonic-gate Psymbol_iter_by_lmid(struct ps_prochandle *P, Lmid_t lmid,
27697c478bd9Sstevel@tonic-gate     const char *object_name, int which, int mask, proc_sym_f *func, void *cd)
27707c478bd9Sstevel@tonic-gate {
27717c478bd9Sstevel@tonic-gate 	return (Psymbol_iter_com(P, lmid, object_name, which, mask,
27727c478bd9Sstevel@tonic-gate 	    PRO_NATURAL, (proc_xsym_f *)func, cd));
27737c478bd9Sstevel@tonic-gate }
27747c478bd9Sstevel@tonic-gate 
27757c478bd9Sstevel@tonic-gate int
27767c478bd9Sstevel@tonic-gate Psymbol_iter(struct ps_prochandle *P,
27777c478bd9Sstevel@tonic-gate     const char *object_name, int which, int mask, proc_sym_f *func, void *cd)
27787c478bd9Sstevel@tonic-gate {
27797c478bd9Sstevel@tonic-gate 	return (Psymbol_iter_com(P, PR_LMID_EVERY, object_name, which, mask,
27807c478bd9Sstevel@tonic-gate 	    PRO_NATURAL, (proc_xsym_f *)func, cd));
27817c478bd9Sstevel@tonic-gate }
27827c478bd9Sstevel@tonic-gate 
27837c478bd9Sstevel@tonic-gate int
27847c478bd9Sstevel@tonic-gate Psymbol_iter_by_addr(struct ps_prochandle *P,
27857c478bd9Sstevel@tonic-gate     const char *object_name, int which, int mask, proc_sym_f *func, void *cd)
27867c478bd9Sstevel@tonic-gate {
27877c478bd9Sstevel@tonic-gate 	return (Psymbol_iter_com(P, PR_LMID_EVERY, object_name, which, mask,
27887c478bd9Sstevel@tonic-gate 	    PRO_BYADDR, (proc_xsym_f *)func, cd));
27897c478bd9Sstevel@tonic-gate }
27907c478bd9Sstevel@tonic-gate 
27917c478bd9Sstevel@tonic-gate int
27927c478bd9Sstevel@tonic-gate Psymbol_iter_by_name(struct ps_prochandle *P,
27937c478bd9Sstevel@tonic-gate     const char *object_name, int which, int mask, proc_sym_f *func, void *cd)
27947c478bd9Sstevel@tonic-gate {
27957c478bd9Sstevel@tonic-gate 	return (Psymbol_iter_com(P, PR_LMID_EVERY, object_name, which, mask,
27967c478bd9Sstevel@tonic-gate 	    PRO_BYNAME, (proc_xsym_f *)func, cd));
27977c478bd9Sstevel@tonic-gate }
27987c478bd9Sstevel@tonic-gate 
27997c478bd9Sstevel@tonic-gate /*
28007c478bd9Sstevel@tonic-gate  * Get the platform string from the core file if we have it;
28017c478bd9Sstevel@tonic-gate  * just perform the system call for the caller if this is a live process.
28027c478bd9Sstevel@tonic-gate  */
28037c478bd9Sstevel@tonic-gate char *
28047c478bd9Sstevel@tonic-gate Pplatform(struct ps_prochandle *P, char *s, size_t n)
28057c478bd9Sstevel@tonic-gate {
28067c478bd9Sstevel@tonic-gate 	if (P->state == PS_IDLE) {
28077c478bd9Sstevel@tonic-gate 		errno = ENODATA;
28087c478bd9Sstevel@tonic-gate 		return (NULL);
28097c478bd9Sstevel@tonic-gate 	}
28107c478bd9Sstevel@tonic-gate 
28117c478bd9Sstevel@tonic-gate 	if (P->state == PS_DEAD) {
28127c478bd9Sstevel@tonic-gate 		if (P->core->core_platform == NULL) {
28137c478bd9Sstevel@tonic-gate 			errno = ENODATA;
28147c478bd9Sstevel@tonic-gate 			return (NULL);
28157c478bd9Sstevel@tonic-gate 		}
28167c478bd9Sstevel@tonic-gate 		(void) strncpy(s, P->core->core_platform, n - 1);
28177c478bd9Sstevel@tonic-gate 		s[n - 1] = '\0';
28187c478bd9Sstevel@tonic-gate 
28197c478bd9Sstevel@tonic-gate 	} else if (sysinfo(SI_PLATFORM, s, n) == -1)
28207c478bd9Sstevel@tonic-gate 		return (NULL);
28217c478bd9Sstevel@tonic-gate 
28227c478bd9Sstevel@tonic-gate 	return (s);
28237c478bd9Sstevel@tonic-gate }
28247c478bd9Sstevel@tonic-gate 
28257c478bd9Sstevel@tonic-gate /*
28267c478bd9Sstevel@tonic-gate  * Get the uname(2) information from the core file if we have it;
28277c478bd9Sstevel@tonic-gate  * just perform the system call for the caller if this is a live process.
28287c478bd9Sstevel@tonic-gate  */
28297c478bd9Sstevel@tonic-gate int
28307c478bd9Sstevel@tonic-gate Puname(struct ps_prochandle *P, struct utsname *u)
28317c478bd9Sstevel@tonic-gate {
28327c478bd9Sstevel@tonic-gate 	if (P->state == PS_IDLE) {
28337c478bd9Sstevel@tonic-gate 		errno = ENODATA;
28347c478bd9Sstevel@tonic-gate 		return (-1);
28357c478bd9Sstevel@tonic-gate 	}
28367c478bd9Sstevel@tonic-gate 
28377c478bd9Sstevel@tonic-gate 	if (P->state == PS_DEAD) {
28387c478bd9Sstevel@tonic-gate 		if (P->core->core_uts == NULL) {
28397c478bd9Sstevel@tonic-gate 			errno = ENODATA;
28407c478bd9Sstevel@tonic-gate 			return (-1);
28417c478bd9Sstevel@tonic-gate 		}
28427c478bd9Sstevel@tonic-gate 		(void) memcpy(u, P->core->core_uts, sizeof (struct utsname));
28437c478bd9Sstevel@tonic-gate 		return (0);
28447c478bd9Sstevel@tonic-gate 	}
28457c478bd9Sstevel@tonic-gate 	return (uname(u));
28467c478bd9Sstevel@tonic-gate }
28477c478bd9Sstevel@tonic-gate 
28487c478bd9Sstevel@tonic-gate /*
28497c478bd9Sstevel@tonic-gate  * Get the zone name from the core file if we have it; look up the
28507c478bd9Sstevel@tonic-gate  * name based on the zone id if this is a live process.
28517c478bd9Sstevel@tonic-gate  */
28527c478bd9Sstevel@tonic-gate char *
28537c478bd9Sstevel@tonic-gate Pzonename(struct ps_prochandle *P, char *s, size_t n)
28547c478bd9Sstevel@tonic-gate {
28557c478bd9Sstevel@tonic-gate 	if (P->state == PS_IDLE) {
28567c478bd9Sstevel@tonic-gate 		errno = ENODATA;
28577c478bd9Sstevel@tonic-gate 		return (NULL);
28587c478bd9Sstevel@tonic-gate 	}
28597c478bd9Sstevel@tonic-gate 
28607c478bd9Sstevel@tonic-gate 	if (P->state == PS_DEAD) {
28617c478bd9Sstevel@tonic-gate 		if (P->core->core_zonename == NULL) {
28627c478bd9Sstevel@tonic-gate 			errno = ENODATA;
28637c478bd9Sstevel@tonic-gate 			return (NULL);
28647c478bd9Sstevel@tonic-gate 		}
28657c478bd9Sstevel@tonic-gate 		(void) strlcpy(s, P->core->core_zonename, n);
28667c478bd9Sstevel@tonic-gate 	} else {
28677c478bd9Sstevel@tonic-gate 		if (getzonenamebyid(P->status.pr_zoneid, s, n) < 0)
28687c478bd9Sstevel@tonic-gate 			return (NULL);
28697c478bd9Sstevel@tonic-gate 		s[n - 1] = '\0';
28707c478bd9Sstevel@tonic-gate 	}
28717c478bd9Sstevel@tonic-gate 	return (s);
28727c478bd9Sstevel@tonic-gate }
28737c478bd9Sstevel@tonic-gate 
28747c478bd9Sstevel@tonic-gate /*
28757c478bd9Sstevel@tonic-gate  * Called from Pcreate(), Pgrab(), and Pfgrab_core() to initialize
28767c478bd9Sstevel@tonic-gate  * the symbol table heads in the new ps_prochandle.
28777c478bd9Sstevel@tonic-gate  */
28787c478bd9Sstevel@tonic-gate void
28797c478bd9Sstevel@tonic-gate Pinitsym(struct ps_prochandle *P)
28807c478bd9Sstevel@tonic-gate {
28817c478bd9Sstevel@tonic-gate 	P->num_files = 0;
28827c478bd9Sstevel@tonic-gate 	list_link(&P->file_head, NULL);
28837c478bd9Sstevel@tonic-gate }
28847c478bd9Sstevel@tonic-gate 
28857c478bd9Sstevel@tonic-gate /*
28867c478bd9Sstevel@tonic-gate  * Called from Prelease() to destroy the symbol tables.
28877c478bd9Sstevel@tonic-gate  * Must be called by the client after an exec() in the victim process.
28887c478bd9Sstevel@tonic-gate  */
28897c478bd9Sstevel@tonic-gate void
28907c478bd9Sstevel@tonic-gate Preset_maps(struct ps_prochandle *P)
28917c478bd9Sstevel@tonic-gate {
28927c478bd9Sstevel@tonic-gate 	int i;
28937c478bd9Sstevel@tonic-gate 
28947c478bd9Sstevel@tonic-gate 	if (P->rap != NULL) {
28957c478bd9Sstevel@tonic-gate 		rd_delete(P->rap);
28967c478bd9Sstevel@tonic-gate 		P->rap = NULL;
28977c478bd9Sstevel@tonic-gate 	}
28987c478bd9Sstevel@tonic-gate 
28997c478bd9Sstevel@tonic-gate 	if (P->execname != NULL) {
29007c478bd9Sstevel@tonic-gate 		free(P->execname);
29017c478bd9Sstevel@tonic-gate 		P->execname = NULL;
29027c478bd9Sstevel@tonic-gate 	}
29037c478bd9Sstevel@tonic-gate 
29047c478bd9Sstevel@tonic-gate 	if (P->auxv != NULL) {
29057c478bd9Sstevel@tonic-gate 		free(P->auxv);
29067c478bd9Sstevel@tonic-gate 		P->auxv = NULL;
29077c478bd9Sstevel@tonic-gate 		P->nauxv = 0;
29087c478bd9Sstevel@tonic-gate 	}
29097c478bd9Sstevel@tonic-gate 
29107c478bd9Sstevel@tonic-gate 	for (i = 0; i < P->map_count; i++)
29117c478bd9Sstevel@tonic-gate 		map_info_free(P, &P->mappings[i]);
29127c478bd9Sstevel@tonic-gate 
29137c478bd9Sstevel@tonic-gate 	if (P->mappings != NULL) {
29147c478bd9Sstevel@tonic-gate 		free(P->mappings);
29157c478bd9Sstevel@tonic-gate 		P->mappings = NULL;
29167c478bd9Sstevel@tonic-gate 	}
29177c478bd9Sstevel@tonic-gate 	P->map_count = P->map_alloc = 0;
29187c478bd9Sstevel@tonic-gate 
29197c478bd9Sstevel@tonic-gate 	P->info_valid = 0;
29207c478bd9Sstevel@tonic-gate }
29217c478bd9Sstevel@tonic-gate 
29227c478bd9Sstevel@tonic-gate typedef struct getenv_data {
29237c478bd9Sstevel@tonic-gate 	char *buf;
29247c478bd9Sstevel@tonic-gate 	size_t bufsize;
29257c478bd9Sstevel@tonic-gate 	const char *search;
29267c478bd9Sstevel@tonic-gate 	size_t searchlen;
29277c478bd9Sstevel@tonic-gate } getenv_data_t;
29287c478bd9Sstevel@tonic-gate 
29297c478bd9Sstevel@tonic-gate /*ARGSUSED*/
29307c478bd9Sstevel@tonic-gate static int
29317c478bd9Sstevel@tonic-gate getenv_func(void *data, struct ps_prochandle *P, uintptr_t addr,
29327c478bd9Sstevel@tonic-gate     const char *nameval)
29337c478bd9Sstevel@tonic-gate {
29347c478bd9Sstevel@tonic-gate 	getenv_data_t *d = data;
29357c478bd9Sstevel@tonic-gate 	size_t len;
29367c478bd9Sstevel@tonic-gate 
29377c478bd9Sstevel@tonic-gate 	if (nameval == NULL)
29387c478bd9Sstevel@tonic-gate 		return (0);
29397c478bd9Sstevel@tonic-gate 
29407c478bd9Sstevel@tonic-gate 	if (d->searchlen < strlen(nameval) &&
29417c478bd9Sstevel@tonic-gate 	    strncmp(nameval, d->search, d->searchlen) == 0 &&
29427c478bd9Sstevel@tonic-gate 	    nameval[d->searchlen] == '=') {
29437c478bd9Sstevel@tonic-gate 		len = MIN(strlen(nameval), d->bufsize - 1);
29447c478bd9Sstevel@tonic-gate 		(void) strncpy(d->buf, nameval, len);
29457c478bd9Sstevel@tonic-gate 		d->buf[len] = '\0';
29467c478bd9Sstevel@tonic-gate 		return (1);
29477c478bd9Sstevel@tonic-gate 	}
29487c478bd9Sstevel@tonic-gate 
29497c478bd9Sstevel@tonic-gate 	return (0);
29507c478bd9Sstevel@tonic-gate }
29517c478bd9Sstevel@tonic-gate 
29527c478bd9Sstevel@tonic-gate char *
29537c478bd9Sstevel@tonic-gate Pgetenv(struct ps_prochandle *P, const char *name, char *buf, size_t buflen)
29547c478bd9Sstevel@tonic-gate {
29557c478bd9Sstevel@tonic-gate 	getenv_data_t d;
29567c478bd9Sstevel@tonic-gate 
29577c478bd9Sstevel@tonic-gate 	d.buf = buf;
29587c478bd9Sstevel@tonic-gate 	d.bufsize = buflen;
29597c478bd9Sstevel@tonic-gate 	d.search = name;
29607c478bd9Sstevel@tonic-gate 	d.searchlen = strlen(name);
29617c478bd9Sstevel@tonic-gate 
29627c478bd9Sstevel@tonic-gate 	if (Penv_iter(P, getenv_func, &d) == 1) {
29637c478bd9Sstevel@tonic-gate 		char *equals = strchr(d.buf, '=');
29647c478bd9Sstevel@tonic-gate 
29657c478bd9Sstevel@tonic-gate 		if (equals != NULL) {
29667c478bd9Sstevel@tonic-gate 			(void) memmove(d.buf, equals + 1,
29677c478bd9Sstevel@tonic-gate 			    d.buf + buflen - equals - 1);
29687c478bd9Sstevel@tonic-gate 			d.buf[d.buf + buflen - equals] = '\0';
29697c478bd9Sstevel@tonic-gate 
29707c478bd9Sstevel@tonic-gate 			return (buf);
29717c478bd9Sstevel@tonic-gate 		}
29727c478bd9Sstevel@tonic-gate 	}
29737c478bd9Sstevel@tonic-gate 
29747c478bd9Sstevel@tonic-gate 	return (NULL);
29757c478bd9Sstevel@tonic-gate }
29767c478bd9Sstevel@tonic-gate 
29777c478bd9Sstevel@tonic-gate /* number of argument or environment pointers to read all at once */
29787c478bd9Sstevel@tonic-gate #define	NARG	100
29797c478bd9Sstevel@tonic-gate 
29807c478bd9Sstevel@tonic-gate int
29817c478bd9Sstevel@tonic-gate Penv_iter(struct ps_prochandle *P, proc_env_f *func, void *data)
29827c478bd9Sstevel@tonic-gate {
29837c478bd9Sstevel@tonic-gate 	const psinfo_t *psp;
29847c478bd9Sstevel@tonic-gate 	uintptr_t envpoff;
29857c478bd9Sstevel@tonic-gate 	GElf_Sym sym;
29867c478bd9Sstevel@tonic-gate 	int ret;
29877c478bd9Sstevel@tonic-gate 	char *buf, *nameval;
29887c478bd9Sstevel@tonic-gate 	size_t buflen;
29897c478bd9Sstevel@tonic-gate 
29907c478bd9Sstevel@tonic-gate 	int nenv = NARG;
29917c478bd9Sstevel@tonic-gate 	long envp[NARG];
29927c478bd9Sstevel@tonic-gate 
29937c478bd9Sstevel@tonic-gate 	/*
29947c478bd9Sstevel@tonic-gate 	 * Attempt to find the "_environ" variable in the process.
29957c478bd9Sstevel@tonic-gate 	 * Failing that, use the original value provided by Ppsinfo().
29967c478bd9Sstevel@tonic-gate 	 */
29977c478bd9Sstevel@tonic-gate 	if ((psp = Ppsinfo(P)) == NULL)
29987c478bd9Sstevel@tonic-gate 		return (-1);
29997c478bd9Sstevel@tonic-gate 
30007c478bd9Sstevel@tonic-gate 	envpoff = psp->pr_envp; /* Default if no _environ found */
30017c478bd9Sstevel@tonic-gate 
30027c478bd9Sstevel@tonic-gate 	if (Plookup_by_name(P, PR_OBJ_EXEC, "_environ", &sym) == 0) {
30037c478bd9Sstevel@tonic-gate 		if (P->status.pr_dmodel == PR_MODEL_NATIVE) {
30047c478bd9Sstevel@tonic-gate 			if (Pread(P, &envpoff, sizeof (envpoff),
30057c478bd9Sstevel@tonic-gate 			    sym.st_value) != sizeof (envpoff))
30067c478bd9Sstevel@tonic-gate 				envpoff = psp->pr_envp;
30077c478bd9Sstevel@tonic-gate 		} else if (P->status.pr_dmodel == PR_MODEL_ILP32) {
30087c478bd9Sstevel@tonic-gate 			uint32_t envpoff32;
30097c478bd9Sstevel@tonic-gate 
30107c478bd9Sstevel@tonic-gate 			if (Pread(P, &envpoff32, sizeof (envpoff32),
30117c478bd9Sstevel@tonic-gate 			    sym.st_value) != sizeof (envpoff32))
30127c478bd9Sstevel@tonic-gate 				envpoff = psp->pr_envp;
30137c478bd9Sstevel@tonic-gate 			else
30147c478bd9Sstevel@tonic-gate 				envpoff = envpoff32;
30157c478bd9Sstevel@tonic-gate 		}
30167c478bd9Sstevel@tonic-gate 	}
30177c478bd9Sstevel@tonic-gate 
30187c478bd9Sstevel@tonic-gate 	buflen = 128;
30197c478bd9Sstevel@tonic-gate 	buf = malloc(buflen);
30207c478bd9Sstevel@tonic-gate 
30217c478bd9Sstevel@tonic-gate 	ret = 0;
30227c478bd9Sstevel@tonic-gate 	for (;;) {
30237c478bd9Sstevel@tonic-gate 		uintptr_t envoff;
30247c478bd9Sstevel@tonic-gate 
30257c478bd9Sstevel@tonic-gate 		if (nenv == NARG) {
30267c478bd9Sstevel@tonic-gate 			(void) memset(envp, 0, sizeof (envp));
30277c478bd9Sstevel@tonic-gate 			if (P->status.pr_dmodel == PR_MODEL_NATIVE) {
30287c478bd9Sstevel@tonic-gate 				if (Pread(P, envp,
30297c478bd9Sstevel@tonic-gate 				    sizeof (envp), envpoff) <= 0) {
30307c478bd9Sstevel@tonic-gate 					ret = -1;
30317c478bd9Sstevel@tonic-gate 					break;
30327c478bd9Sstevel@tonic-gate 				}
30337c478bd9Sstevel@tonic-gate 			} else if (P->status.pr_dmodel == PR_MODEL_ILP32) {
30347c478bd9Sstevel@tonic-gate 				uint32_t e32[NARG];
30357c478bd9Sstevel@tonic-gate 				int i;
30367c478bd9Sstevel@tonic-gate 
30377c478bd9Sstevel@tonic-gate 				(void) memset(e32, 0, sizeof (e32));
30387c478bd9Sstevel@tonic-gate 				if (Pread(P, e32, sizeof (e32), envpoff) <= 0) {
30397c478bd9Sstevel@tonic-gate 					ret = -1;
30407c478bd9Sstevel@tonic-gate 					break;
30417c478bd9Sstevel@tonic-gate 				}
30427c478bd9Sstevel@tonic-gate 				for (i = 0; i < NARG; i++)
30437c478bd9Sstevel@tonic-gate 					envp[i] = e32[i];
30447c478bd9Sstevel@tonic-gate 			}
30457c478bd9Sstevel@tonic-gate 			nenv = 0;
30467c478bd9Sstevel@tonic-gate 		}
30477c478bd9Sstevel@tonic-gate 
30487c478bd9Sstevel@tonic-gate 		if ((envoff = envp[nenv++]) == NULL)
30497c478bd9Sstevel@tonic-gate 			break;
30507c478bd9Sstevel@tonic-gate 
30517c478bd9Sstevel@tonic-gate 		/*
30527c478bd9Sstevel@tonic-gate 		 * Attempt to read the string from the process.
30537c478bd9Sstevel@tonic-gate 		 */
30547c478bd9Sstevel@tonic-gate again:
30557c478bd9Sstevel@tonic-gate 		ret = Pread_string(P, buf, buflen, envoff);
30567c478bd9Sstevel@tonic-gate 
30577c478bd9Sstevel@tonic-gate 		if (ret <= 0) {
30587c478bd9Sstevel@tonic-gate 			nameval = NULL;
30597c478bd9Sstevel@tonic-gate 		} else if (ret == buflen - 1) {
30607c478bd9Sstevel@tonic-gate 			free(buf);
30617c478bd9Sstevel@tonic-gate 			/*
30627c478bd9Sstevel@tonic-gate 			 * Bail if we have a corrupted environment
30637c478bd9Sstevel@tonic-gate 			 */
30647c478bd9Sstevel@tonic-gate 			if (buflen >= ARG_MAX)
30657c478bd9Sstevel@tonic-gate 				return (-1);
30667c478bd9Sstevel@tonic-gate 			buflen *= 2;
30677c478bd9Sstevel@tonic-gate 			buf = malloc(buflen);
30687c478bd9Sstevel@tonic-gate 			goto again;
30697c478bd9Sstevel@tonic-gate 		} else {
30707c478bd9Sstevel@tonic-gate 			nameval = buf;
30717c478bd9Sstevel@tonic-gate 		}
30727c478bd9Sstevel@tonic-gate 
30737c478bd9Sstevel@tonic-gate 		if ((ret = func(data, P, envoff, nameval)) != 0)
30747c478bd9Sstevel@tonic-gate 			break;
30757c478bd9Sstevel@tonic-gate 
30767c478bd9Sstevel@tonic-gate 		envpoff += (P->status.pr_dmodel == PR_MODEL_LP64)? 8 : 4;
30777c478bd9Sstevel@tonic-gate 	}
30787c478bd9Sstevel@tonic-gate 
30797c478bd9Sstevel@tonic-gate 	free(buf);
30807c478bd9Sstevel@tonic-gate 
30817c478bd9Sstevel@tonic-gate 	return (ret);
30827c478bd9Sstevel@tonic-gate }
3083