xref: /titanic_44/usr/src/lib/libproc/common/Psymtab.c (revision 9acbbeaf2a1ffe5c14b244867d427714fab43c5c)
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 /*
23ab9a77c7Sahl  * Copyright 2006 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 
29*9acbbeafSnn35248 #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"
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate static file_info_t *build_map_symtab(struct ps_prochandle *, map_info_t *);
557c478bd9Sstevel@tonic-gate static map_info_t *exec_map(struct ps_prochandle *);
567c478bd9Sstevel@tonic-gate static map_info_t *object_to_map(struct ps_prochandle *, Lmid_t, const char *);
577c478bd9Sstevel@tonic-gate static map_info_t *object_name_to_map(struct ps_prochandle *,
587c478bd9Sstevel@tonic-gate 	Lmid_t, const char *);
597c478bd9Sstevel@tonic-gate static GElf_Sym *sym_by_name(sym_tbl_t *, const char *, GElf_Sym *, uint_t *);
6030da1432Sahl static int read_ehdr32(struct ps_prochandle *, Elf32_Ehdr *, uint_t *,
6130da1432Sahl     uintptr_t);
627c478bd9Sstevel@tonic-gate #ifdef _LP64
6330da1432Sahl static int read_ehdr64(struct ps_prochandle *, Elf64_Ehdr *, uint_t *,
6430da1432Sahl     uintptr_t);
657c478bd9Sstevel@tonic-gate #endif
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate #define	DATA_TYPES	\
687c478bd9Sstevel@tonic-gate 	((1 << STT_OBJECT) | (1 << STT_FUNC) | \
697c478bd9Sstevel@tonic-gate 	(1 << STT_COMMON) | (1 << STT_TLS))
707c478bd9Sstevel@tonic-gate #define	IS_DATA_TYPE(tp)	(((1 << (tp)) & DATA_TYPES) != 0)
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate #define	MA_RWX	(MA_READ | MA_WRITE | MA_EXEC)
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate typedef enum {
757c478bd9Sstevel@tonic-gate 	PRO_NATURAL,
767c478bd9Sstevel@tonic-gate 	PRO_BYADDR,
777c478bd9Sstevel@tonic-gate 	PRO_BYNAME
787c478bd9Sstevel@tonic-gate } pr_order_t;
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate static int
817c478bd9Sstevel@tonic-gate addr_cmp(const void *aa, const void *bb)
827c478bd9Sstevel@tonic-gate {
837c478bd9Sstevel@tonic-gate 	uintptr_t a = *((uintptr_t *)aa);
847c478bd9Sstevel@tonic-gate 	uintptr_t b = *((uintptr_t *)bb);
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate 	if (a > b)
877c478bd9Sstevel@tonic-gate 		return (1);
887c478bd9Sstevel@tonic-gate 	if (a < b)
897c478bd9Sstevel@tonic-gate 		return (-1);
907c478bd9Sstevel@tonic-gate 	return (0);
917c478bd9Sstevel@tonic-gate }
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate /*
947c478bd9Sstevel@tonic-gate  * Allocation function for a new file_info_t
957c478bd9Sstevel@tonic-gate  */
967c478bd9Sstevel@tonic-gate static file_info_t *
977c478bd9Sstevel@tonic-gate file_info_new(struct ps_prochandle *P, map_info_t *mptr)
987c478bd9Sstevel@tonic-gate {
997c478bd9Sstevel@tonic-gate 	file_info_t *fptr;
1007c478bd9Sstevel@tonic-gate 	map_info_t *mp;
1017c478bd9Sstevel@tonic-gate 	uintptr_t a, addr, *addrs, last = 0;
1027c478bd9Sstevel@tonic-gate 	uint_t i, j, naddrs = 0, unordered = 0;
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate 	if ((fptr = calloc(1, sizeof (file_info_t))) == NULL)
1057c478bd9Sstevel@tonic-gate 		return (NULL);
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate 	list_link(fptr, &P->file_head);
1087c478bd9Sstevel@tonic-gate 	(void) strcpy(fptr->file_pname, mptr->map_pmap.pr_mapname);
1097c478bd9Sstevel@tonic-gate 	mptr->map_file = fptr;
1107c478bd9Sstevel@tonic-gate 	fptr->file_ref = 1;
1117c478bd9Sstevel@tonic-gate 	fptr->file_fd = -1;
1127c478bd9Sstevel@tonic-gate 	P->num_files++;
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate 	/*
1157c478bd9Sstevel@tonic-gate 	 * To figure out which map_info_t instances correspond to the mappings
1167c478bd9Sstevel@tonic-gate 	 * for this load object, we look at the in-memory ELF image in the
1177c478bd9Sstevel@tonic-gate 	 * base mapping (usually the program text). We examine the program
1187c478bd9Sstevel@tonic-gate 	 * headers to find the addresses at the beginning and end of each
1197c478bd9Sstevel@tonic-gate 	 * section and store them in a list which we then sort. Finally, we
1207c478bd9Sstevel@tonic-gate 	 * walk down the list of addresses and the list of map_info_t
1217c478bd9Sstevel@tonic-gate 	 * instances in lock step to correctly find the mappings that
1227c478bd9Sstevel@tonic-gate 	 * correspond to this load object.
1237c478bd9Sstevel@tonic-gate 	 */
1247c478bd9Sstevel@tonic-gate 	if (P->status.pr_dmodel == PR_MODEL_ILP32) {
1257c478bd9Sstevel@tonic-gate 		Elf32_Ehdr ehdr;
1267c478bd9Sstevel@tonic-gate 		Elf32_Phdr phdr;
12730da1432Sahl 		uint_t phnum;
1287c478bd9Sstevel@tonic-gate 
12930da1432Sahl 		if (read_ehdr32(P, &ehdr, &phnum, mptr->map_pmap.pr_vaddr) != 0)
1307c478bd9Sstevel@tonic-gate 			return (fptr);
1317c478bd9Sstevel@tonic-gate 
13230da1432Sahl 		addrs = malloc(sizeof (uintptr_t) * phnum * 2);
1337c478bd9Sstevel@tonic-gate 		a = mptr->map_pmap.pr_vaddr + ehdr.e_phoff;
13430da1432Sahl 		for (i = 0; i < phnum; i++, a += ehdr.e_phentsize) {
1357c478bd9Sstevel@tonic-gate 			if (Pread(P, &phdr, sizeof (phdr), a) != sizeof (phdr))
1367c478bd9Sstevel@tonic-gate 				goto out;
1377c478bd9Sstevel@tonic-gate 			if (phdr.p_type != PT_LOAD || phdr.p_memsz == 0)
1387c478bd9Sstevel@tonic-gate 				continue;
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 			addr = phdr.p_vaddr;
1417c478bd9Sstevel@tonic-gate 			if (ehdr.e_type == ET_DYN)
1427c478bd9Sstevel@tonic-gate 				addr += mptr->map_pmap.pr_vaddr;
1437c478bd9Sstevel@tonic-gate 			if (last > addr)
1447c478bd9Sstevel@tonic-gate 				unordered = 1;
1457c478bd9Sstevel@tonic-gate 			addrs[naddrs++] = addr;
1467c478bd9Sstevel@tonic-gate 			addrs[naddrs++] = last = addr + phdr.p_memsz - 1;
1477c478bd9Sstevel@tonic-gate 		}
1487c478bd9Sstevel@tonic-gate #ifdef _LP64
1497c478bd9Sstevel@tonic-gate 	} else {
1507c478bd9Sstevel@tonic-gate 		Elf64_Ehdr ehdr;
1517c478bd9Sstevel@tonic-gate 		Elf64_Phdr phdr;
15230da1432Sahl 		uint_t phnum;
1537c478bd9Sstevel@tonic-gate 
15430da1432Sahl 		if (read_ehdr64(P, &ehdr, &phnum, mptr->map_pmap.pr_vaddr) != 0)
1557c478bd9Sstevel@tonic-gate 			return (fptr);
1567c478bd9Sstevel@tonic-gate 
15730da1432Sahl 		addrs = malloc(sizeof (uintptr_t) * phnum * 2);
1587c478bd9Sstevel@tonic-gate 		a = mptr->map_pmap.pr_vaddr + ehdr.e_phoff;
15930da1432Sahl 		for (i = 0; i < phnum; i++, a += ehdr.e_phentsize) {
1607c478bd9Sstevel@tonic-gate 			if (Pread(P, &phdr, sizeof (phdr), a) != sizeof (phdr))
1617c478bd9Sstevel@tonic-gate 				goto out;
1627c478bd9Sstevel@tonic-gate 			if (phdr.p_type != PT_LOAD || phdr.p_memsz == 0)
1637c478bd9Sstevel@tonic-gate 				continue;
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 			addr = phdr.p_vaddr;
1667c478bd9Sstevel@tonic-gate 			if (ehdr.e_type == ET_DYN)
1677c478bd9Sstevel@tonic-gate 				addr += mptr->map_pmap.pr_vaddr;
1687c478bd9Sstevel@tonic-gate 			if (last > addr)
1697c478bd9Sstevel@tonic-gate 				unordered = 1;
1707c478bd9Sstevel@tonic-gate 			addrs[naddrs++] = addr;
1717c478bd9Sstevel@tonic-gate 			addrs[naddrs++] = last = addr + phdr.p_memsz - 1;
1727c478bd9Sstevel@tonic-gate 		}
1737c478bd9Sstevel@tonic-gate #endif
1747c478bd9Sstevel@tonic-gate 	}
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 	if (unordered)
1777c478bd9Sstevel@tonic-gate 		qsort(addrs, naddrs, sizeof (uintptr_t), addr_cmp);
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 	i = j = 0;
1817c478bd9Sstevel@tonic-gate 	mp = P->mappings;
1827c478bd9Sstevel@tonic-gate 	while (j < P->map_count && i < naddrs) {
1837c478bd9Sstevel@tonic-gate 		addr = addrs[i];
1847c478bd9Sstevel@tonic-gate 		if (addr >= mp->map_pmap.pr_vaddr &&
1857c478bd9Sstevel@tonic-gate 		    addr < mp->map_pmap.pr_vaddr + mp->map_pmap.pr_size &&
1867c478bd9Sstevel@tonic-gate 		    mp->map_file == NULL) {
1877c478bd9Sstevel@tonic-gate 			mp->map_file = fptr;
1887c478bd9Sstevel@tonic-gate 			fptr->file_ref++;
1897c478bd9Sstevel@tonic-gate 		}
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 		if (addr < mp->map_pmap.pr_vaddr + mp->map_pmap.pr_size) {
1927c478bd9Sstevel@tonic-gate 			i++;
1937c478bd9Sstevel@tonic-gate 		} else {
1947c478bd9Sstevel@tonic-gate 			mp++;
1957c478bd9Sstevel@tonic-gate 			j++;
1967c478bd9Sstevel@tonic-gate 		}
1977c478bd9Sstevel@tonic-gate 	}
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate out:
2007c478bd9Sstevel@tonic-gate 	free(addrs);
2017c478bd9Sstevel@tonic-gate 	return (fptr);
2027c478bd9Sstevel@tonic-gate }
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate /*
2057c478bd9Sstevel@tonic-gate  * Deallocation function for a file_info_t
2067c478bd9Sstevel@tonic-gate  */
2077c478bd9Sstevel@tonic-gate static void
2087c478bd9Sstevel@tonic-gate file_info_free(struct ps_prochandle *P, file_info_t *fptr)
2097c478bd9Sstevel@tonic-gate {
2107c478bd9Sstevel@tonic-gate 	if (--fptr->file_ref == 0) {
2117c478bd9Sstevel@tonic-gate 		list_unlink(fptr);
2127c478bd9Sstevel@tonic-gate 		if (fptr->file_symtab.sym_elf) {
2137c478bd9Sstevel@tonic-gate 			(void) elf_end(fptr->file_symtab.sym_elf);
2147c478bd9Sstevel@tonic-gate 			free(fptr->file_symtab.sym_elfmem);
2157c478bd9Sstevel@tonic-gate 		}
2167c478bd9Sstevel@tonic-gate 		if (fptr->file_symtab.sym_byname)
2177c478bd9Sstevel@tonic-gate 			free(fptr->file_symtab.sym_byname);
2187c478bd9Sstevel@tonic-gate 		if (fptr->file_symtab.sym_byaddr)
2197c478bd9Sstevel@tonic-gate 			free(fptr->file_symtab.sym_byaddr);
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate 		if (fptr->file_dynsym.sym_elf) {
2227c478bd9Sstevel@tonic-gate 			(void) elf_end(fptr->file_dynsym.sym_elf);
2237c478bd9Sstevel@tonic-gate 			free(fptr->file_dynsym.sym_elfmem);
2247c478bd9Sstevel@tonic-gate 		}
2257c478bd9Sstevel@tonic-gate 		if (fptr->file_dynsym.sym_byname)
2267c478bd9Sstevel@tonic-gate 			free(fptr->file_dynsym.sym_byname);
2277c478bd9Sstevel@tonic-gate 		if (fptr->file_dynsym.sym_byaddr)
2287c478bd9Sstevel@tonic-gate 			free(fptr->file_dynsym.sym_byaddr);
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 		if (fptr->file_lo)
2317c478bd9Sstevel@tonic-gate 			free(fptr->file_lo);
2327c478bd9Sstevel@tonic-gate 		if (fptr->file_lname)
2337c478bd9Sstevel@tonic-gate 			free(fptr->file_lname);
2347c478bd9Sstevel@tonic-gate 		if (fptr->file_elf)
2357c478bd9Sstevel@tonic-gate 			(void) elf_end(fptr->file_elf);
2367c478bd9Sstevel@tonic-gate 		if (fptr->file_elfmem != NULL)
2377c478bd9Sstevel@tonic-gate 			free(fptr->file_elfmem);
2387c478bd9Sstevel@tonic-gate 		if (fptr->file_fd >= 0)
2397c478bd9Sstevel@tonic-gate 			(void) close(fptr->file_fd);
2407c478bd9Sstevel@tonic-gate 		if (fptr->file_ctfp) {
2417c478bd9Sstevel@tonic-gate 			ctf_close(fptr->file_ctfp);
2427c478bd9Sstevel@tonic-gate 			free(fptr->file_ctf_buf);
2437c478bd9Sstevel@tonic-gate 		}
2447c478bd9Sstevel@tonic-gate 		free(fptr);
2457c478bd9Sstevel@tonic-gate 		P->num_files--;
2467c478bd9Sstevel@tonic-gate 	}
2477c478bd9Sstevel@tonic-gate }
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate /*
2507c478bd9Sstevel@tonic-gate  * Deallocation function for a map_info_t
2517c478bd9Sstevel@tonic-gate  */
2527c478bd9Sstevel@tonic-gate static void
2537c478bd9Sstevel@tonic-gate map_info_free(struct ps_prochandle *P, map_info_t *mptr)
2547c478bd9Sstevel@tonic-gate {
2557c478bd9Sstevel@tonic-gate 	file_info_t *fptr;
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate 	if ((fptr = mptr->map_file) != NULL) {
2587c478bd9Sstevel@tonic-gate 		if (fptr->file_map == mptr)
2597c478bd9Sstevel@tonic-gate 			fptr->file_map = NULL;
2607c478bd9Sstevel@tonic-gate 		file_info_free(P, fptr);
2617c478bd9Sstevel@tonic-gate 	}
2627c478bd9Sstevel@tonic-gate 	if (P->execname && mptr == P->map_exec) {
2637c478bd9Sstevel@tonic-gate 		free(P->execname);
2647c478bd9Sstevel@tonic-gate 		P->execname = NULL;
2657c478bd9Sstevel@tonic-gate 	}
2667c478bd9Sstevel@tonic-gate 	if (P->auxv && (mptr == P->map_exec || mptr == P->map_ldso)) {
2677c478bd9Sstevel@tonic-gate 		free(P->auxv);
2687c478bd9Sstevel@tonic-gate 		P->auxv = NULL;
2697c478bd9Sstevel@tonic-gate 		P->nauxv = 0;
2707c478bd9Sstevel@tonic-gate 	}
2717c478bd9Sstevel@tonic-gate 	if (mptr == P->map_exec)
2727c478bd9Sstevel@tonic-gate 		P->map_exec = NULL;
2737c478bd9Sstevel@tonic-gate 	if (mptr == P->map_ldso)
2747c478bd9Sstevel@tonic-gate 		P->map_ldso = NULL;
2757c478bd9Sstevel@tonic-gate }
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate /*
2787c478bd9Sstevel@tonic-gate  * Call-back function for librtld_db to iterate through all of its shared
2797c478bd9Sstevel@tonic-gate  * libraries.  We use this to get the load object names for the mappings.
2807c478bd9Sstevel@tonic-gate  */
2817c478bd9Sstevel@tonic-gate static int
2827c478bd9Sstevel@tonic-gate map_iter(const rd_loadobj_t *lop, void *cd)
2837c478bd9Sstevel@tonic-gate {
2847c478bd9Sstevel@tonic-gate 	char buf[PATH_MAX];
2857c478bd9Sstevel@tonic-gate 	struct ps_prochandle *P = cd;
2867c478bd9Sstevel@tonic-gate 	map_info_t *mptr;
2877c478bd9Sstevel@tonic-gate 	file_info_t *fptr;
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate 	dprintf("encountered rd object at %p\n", (void *)lop->rl_base);
2907c478bd9Sstevel@tonic-gate 
291*9acbbeafSnn35248 	if ((mptr = Paddr2mptr(P, lop->rl_base)) == NULL) {
292*9acbbeafSnn35248 		dprintf("map_iter: base address doesn't match any mapping\n");
2937c478bd9Sstevel@tonic-gate 		return (1); /* Base address does not match any mapping */
294*9acbbeafSnn35248 	}
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate 	if ((fptr = mptr->map_file) == NULL &&
297*9acbbeafSnn35248 	    (fptr = file_info_new(P, mptr)) == NULL) {
298*9acbbeafSnn35248 		dprintf("map_iter: failed to allocate a new file_info_t\n");
2997c478bd9Sstevel@tonic-gate 		return (1); /* Failed to allocate a new file_info_t */
300*9acbbeafSnn35248 	}
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate 	if ((fptr->file_lo == NULL) &&
3037c478bd9Sstevel@tonic-gate 	    (fptr->file_lo = malloc(sizeof (rd_loadobj_t))) == NULL) {
304*9acbbeafSnn35248 		dprintf("map_iter: failed to allocate rd_loadobj_t\n");
3057c478bd9Sstevel@tonic-gate 		file_info_free(P, fptr);
3067c478bd9Sstevel@tonic-gate 		return (1); /* Failed to allocate rd_loadobj_t */
3077c478bd9Sstevel@tonic-gate 	}
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate 	fptr->file_map = mptr;
3107c478bd9Sstevel@tonic-gate 	*fptr->file_lo = *lop;
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate 	fptr->file_lo->rl_plt_base = fptr->file_plt_base;
3137c478bd9Sstevel@tonic-gate 	fptr->file_lo->rl_plt_size = fptr->file_plt_size;
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate 	if (fptr->file_lname) {
3167c478bd9Sstevel@tonic-gate 		free(fptr->file_lname);
3177c478bd9Sstevel@tonic-gate 		fptr->file_lname = NULL;
3187c478bd9Sstevel@tonic-gate 	}
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate 	if (Pread_string(P, buf, sizeof (buf), lop->rl_nameaddr) > 0) {
3217c478bd9Sstevel@tonic-gate 		if ((fptr->file_lname = strdup(buf)) != NULL)
3227c478bd9Sstevel@tonic-gate 			fptr->file_lbase = basename(fptr->file_lname);
323*9acbbeafSnn35248 	} else {
324*9acbbeafSnn35248 		dprintf("map_iter: failed to read string at %p\n",
325*9acbbeafSnn35248 		    (void *)lop->rl_nameaddr);
3267c478bd9Sstevel@tonic-gate 	}
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate 	dprintf("loaded rd object %s lmid %lx\n",
3297c478bd9Sstevel@tonic-gate 	    fptr->file_lname ? fptr->file_lname : "<NULL>", lop->rl_lmident);
3307c478bd9Sstevel@tonic-gate 	return (1);
3317c478bd9Sstevel@tonic-gate }
3327c478bd9Sstevel@tonic-gate 
3337c478bd9Sstevel@tonic-gate static void
3347c478bd9Sstevel@tonic-gate map_set(struct ps_prochandle *P, map_info_t *mptr, const char *lname)
3357c478bd9Sstevel@tonic-gate {
3367c478bd9Sstevel@tonic-gate 	file_info_t *fptr;
3377c478bd9Sstevel@tonic-gate 
3387c478bd9Sstevel@tonic-gate 	if ((fptr = mptr->map_file) == NULL &&
3397c478bd9Sstevel@tonic-gate 	    (fptr = file_info_new(P, mptr)) == NULL)
3407c478bd9Sstevel@tonic-gate 		return; /* Failed to allocate a new file_info_t */
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate 	fptr->file_map = mptr;
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate 	if ((fptr->file_lo == NULL) &&
3457c478bd9Sstevel@tonic-gate 	    (fptr->file_lo = malloc(sizeof (rd_loadobj_t))) == NULL) {
3467c478bd9Sstevel@tonic-gate 		file_info_free(P, fptr);
3477c478bd9Sstevel@tonic-gate 		return; /* Failed to allocate rd_loadobj_t */
3487c478bd9Sstevel@tonic-gate 	}
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate 	(void) memset(fptr->file_lo, 0, sizeof (rd_loadobj_t));
3517c478bd9Sstevel@tonic-gate 	fptr->file_lo->rl_base = mptr->map_pmap.pr_vaddr;
3527c478bd9Sstevel@tonic-gate 	fptr->file_lo->rl_bend =
3537c478bd9Sstevel@tonic-gate 	    mptr->map_pmap.pr_vaddr + mptr->map_pmap.pr_size;
3547c478bd9Sstevel@tonic-gate 
3557c478bd9Sstevel@tonic-gate 	fptr->file_lo->rl_plt_base = fptr->file_plt_base;
3567c478bd9Sstevel@tonic-gate 	fptr->file_lo->rl_plt_size = fptr->file_plt_size;
3577c478bd9Sstevel@tonic-gate 
358*9acbbeafSnn35248 	if (fptr->file_lname == NULL &&
359*9acbbeafSnn35248 	    (fptr->file_lname = strdup(lname)) != NULL)
3607c478bd9Sstevel@tonic-gate 		fptr->file_lbase = basename(fptr->file_lname);
3617c478bd9Sstevel@tonic-gate }
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate static void
3647c478bd9Sstevel@tonic-gate load_static_maps(struct ps_prochandle *P)
3657c478bd9Sstevel@tonic-gate {
3667c478bd9Sstevel@tonic-gate 	map_info_t *mptr;
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate 	/*
3697c478bd9Sstevel@tonic-gate 	 * Construct the map for the a.out.
3707c478bd9Sstevel@tonic-gate 	 */
3717c478bd9Sstevel@tonic-gate 	if ((mptr = object_name_to_map(P, PR_LMID_EVERY, PR_OBJ_EXEC)) != NULL)
3727c478bd9Sstevel@tonic-gate 		map_set(P, mptr, "a.out");
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate 	/*
3757c478bd9Sstevel@tonic-gate 	 * If the dynamic linker exists for this process,
3767c478bd9Sstevel@tonic-gate 	 * construct the map for it.
3777c478bd9Sstevel@tonic-gate 	 */
3787c478bd9Sstevel@tonic-gate 	if (Pgetauxval(P, AT_BASE) != -1L &&
3797c478bd9Sstevel@tonic-gate 	    (mptr = object_name_to_map(P, PR_LMID_EVERY, PR_OBJ_LDSO)) != NULL)
3807c478bd9Sstevel@tonic-gate 		map_set(P, mptr, "ld.so.1");
3817c478bd9Sstevel@tonic-gate }
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate /*
3847c478bd9Sstevel@tonic-gate  * Go through all the address space mappings, validating or updating
3857c478bd9Sstevel@tonic-gate  * the information already gathered, or gathering new information.
3867c478bd9Sstevel@tonic-gate  *
3877c478bd9Sstevel@tonic-gate  * This function is only called when we suspect that the mappings have changed
3887c478bd9Sstevel@tonic-gate  * because this is the first time we're calling it or because of rtld activity.
3897c478bd9Sstevel@tonic-gate  */
3907c478bd9Sstevel@tonic-gate void
3917c478bd9Sstevel@tonic-gate Pupdate_maps(struct ps_prochandle *P)
3927c478bd9Sstevel@tonic-gate {
393*9acbbeafSnn35248 	char mapfile[PATH_MAX];
3947c478bd9Sstevel@tonic-gate 	int mapfd;
3957c478bd9Sstevel@tonic-gate 	struct stat statb;
3967c478bd9Sstevel@tonic-gate 	prmap_t *Pmap = NULL;
3977c478bd9Sstevel@tonic-gate 	prmap_t *pmap;
3987c478bd9Sstevel@tonic-gate 	ssize_t nmap;
3997c478bd9Sstevel@tonic-gate 	int i;
4007c478bd9Sstevel@tonic-gate 	uint_t oldmapcount;
4017c478bd9Sstevel@tonic-gate 	map_info_t *newmap, *newp;
4027c478bd9Sstevel@tonic-gate 	map_info_t *mptr;
4037c478bd9Sstevel@tonic-gate 
404a1b5e537Sbmc 	if (P->info_valid || P->state == PS_UNDEAD)
4057c478bd9Sstevel@tonic-gate 		return;
4067c478bd9Sstevel@tonic-gate 
4077c478bd9Sstevel@tonic-gate 	Preadauxvec(P);
4087c478bd9Sstevel@tonic-gate 
409*9acbbeafSnn35248 	(void) snprintf(mapfile, sizeof (mapfile), "%s/%d/map",
410*9acbbeafSnn35248 	    procfs_path, (int)P->pid);
4117c478bd9Sstevel@tonic-gate 	if ((mapfd = open(mapfile, O_RDONLY)) < 0 ||
4127c478bd9Sstevel@tonic-gate 	    fstat(mapfd, &statb) != 0 ||
4137c478bd9Sstevel@tonic-gate 	    statb.st_size < sizeof (prmap_t) ||
4147c478bd9Sstevel@tonic-gate 	    (Pmap = malloc(statb.st_size)) == NULL ||
4157c478bd9Sstevel@tonic-gate 	    (nmap = pread(mapfd, Pmap, statb.st_size, 0L)) <= 0 ||
4167c478bd9Sstevel@tonic-gate 	    (nmap /= sizeof (prmap_t)) == 0) {
4177c478bd9Sstevel@tonic-gate 		if (Pmap != NULL)
4187c478bd9Sstevel@tonic-gate 			free(Pmap);
4197c478bd9Sstevel@tonic-gate 		if (mapfd >= 0)
4207c478bd9Sstevel@tonic-gate 			(void) close(mapfd);
4217c478bd9Sstevel@tonic-gate 		Preset_maps(P);	/* utter failure; destroy tables */
4227c478bd9Sstevel@tonic-gate 		return;
4237c478bd9Sstevel@tonic-gate 	}
4247c478bd9Sstevel@tonic-gate 	(void) close(mapfd);
4257c478bd9Sstevel@tonic-gate 
4267c478bd9Sstevel@tonic-gate 	if ((newmap = calloc(1, nmap * sizeof (map_info_t))) == NULL)
4277c478bd9Sstevel@tonic-gate 		return;
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate 	/*
4307c478bd9Sstevel@tonic-gate 	 * We try to merge any file information we may have for existing
4317c478bd9Sstevel@tonic-gate 	 * mappings, to avoid having to rebuild the file info.
4327c478bd9Sstevel@tonic-gate 	 */
4337c478bd9Sstevel@tonic-gate 	mptr = P->mappings;
4347c478bd9Sstevel@tonic-gate 	pmap = Pmap;
4357c478bd9Sstevel@tonic-gate 	newp = newmap;
4367c478bd9Sstevel@tonic-gate 	oldmapcount = P->map_count;
4377c478bd9Sstevel@tonic-gate 	for (i = 0; i < nmap; i++, pmap++, newp++) {
4387c478bd9Sstevel@tonic-gate 
4397c478bd9Sstevel@tonic-gate 		if (oldmapcount == 0) {
4407c478bd9Sstevel@tonic-gate 			/*
4417c478bd9Sstevel@tonic-gate 			 * We've exhausted all the old mappings.  Every new
4427c478bd9Sstevel@tonic-gate 			 * mapping should be added.
4437c478bd9Sstevel@tonic-gate 			 */
4447c478bd9Sstevel@tonic-gate 			newp->map_pmap = *pmap;
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate 		} else if (pmap->pr_vaddr == mptr->map_pmap.pr_vaddr &&
4477c478bd9Sstevel@tonic-gate 		    pmap->pr_size == mptr->map_pmap.pr_size &&
4487c478bd9Sstevel@tonic-gate 		    pmap->pr_offset == mptr->map_pmap.pr_offset &&
4497c478bd9Sstevel@tonic-gate 		    (pmap->pr_mflags & ~(MA_BREAK | MA_STACK)) ==
4507c478bd9Sstevel@tonic-gate 		    (mptr->map_pmap.pr_mflags & ~(MA_BREAK | MA_STACK)) &&
4517c478bd9Sstevel@tonic-gate 		    pmap->pr_pagesize == mptr->map_pmap.pr_pagesize &&
4527c478bd9Sstevel@tonic-gate 		    pmap->pr_shmid == mptr->map_pmap.pr_shmid &&
4537c478bd9Sstevel@tonic-gate 		    strcmp(pmap->pr_mapname, mptr->map_pmap.pr_mapname) == 0) {
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate 			/*
4567c478bd9Sstevel@tonic-gate 			 * This mapping matches exactly.  Copy over the old
4577c478bd9Sstevel@tonic-gate 			 * mapping, taking care to get the latest flags.
4587c478bd9Sstevel@tonic-gate 			 * Make sure the associated file_info_t is updated
4597c478bd9Sstevel@tonic-gate 			 * appropriately.
4607c478bd9Sstevel@tonic-gate 			 */
4617c478bd9Sstevel@tonic-gate 			*newp = *mptr;
4627c478bd9Sstevel@tonic-gate 			if (P->map_exec == mptr)
4637c478bd9Sstevel@tonic-gate 				P->map_exec = newp;
4647c478bd9Sstevel@tonic-gate 			if (P->map_ldso == mptr)
4657c478bd9Sstevel@tonic-gate 				P->map_ldso = newp;
4667c478bd9Sstevel@tonic-gate 			newp->map_pmap.pr_mflags = pmap->pr_mflags;
4677c478bd9Sstevel@tonic-gate 			if (mptr->map_file != NULL &&
4687c478bd9Sstevel@tonic-gate 			    mptr->map_file->file_map == mptr)
4697c478bd9Sstevel@tonic-gate 				mptr->map_file->file_map = newp;
4707c478bd9Sstevel@tonic-gate 			oldmapcount--;
4717c478bd9Sstevel@tonic-gate 			mptr++;
4727c478bd9Sstevel@tonic-gate 
4737c478bd9Sstevel@tonic-gate 		} else if (pmap->pr_vaddr + pmap->pr_size >
4747c478bd9Sstevel@tonic-gate 		    mptr->map_pmap.pr_vaddr) {
4757c478bd9Sstevel@tonic-gate 
4767c478bd9Sstevel@tonic-gate 			/*
4777c478bd9Sstevel@tonic-gate 			 * The old mapping doesn't exist any more, remove it
4787c478bd9Sstevel@tonic-gate 			 * from the list.
4797c478bd9Sstevel@tonic-gate 			 */
4807c478bd9Sstevel@tonic-gate 			map_info_free(P, mptr);
4817c478bd9Sstevel@tonic-gate 			oldmapcount--;
4827c478bd9Sstevel@tonic-gate 			i--;
4837c478bd9Sstevel@tonic-gate 			newp--;
4847c478bd9Sstevel@tonic-gate 			pmap--;
4857c478bd9Sstevel@tonic-gate 			mptr++;
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate 		} else {
4887c478bd9Sstevel@tonic-gate 
4897c478bd9Sstevel@tonic-gate 			/*
4907c478bd9Sstevel@tonic-gate 			 * This is a new mapping, add it directly.
4917c478bd9Sstevel@tonic-gate 			 */
4927c478bd9Sstevel@tonic-gate 			newp->map_pmap = *pmap;
4937c478bd9Sstevel@tonic-gate 		}
4947c478bd9Sstevel@tonic-gate 	}
4957c478bd9Sstevel@tonic-gate 
4967c478bd9Sstevel@tonic-gate 	/*
4977c478bd9Sstevel@tonic-gate 	 * Free any old maps
4987c478bd9Sstevel@tonic-gate 	 */
4997c478bd9Sstevel@tonic-gate 	while (oldmapcount) {
5007c478bd9Sstevel@tonic-gate 		map_info_free(P, mptr);
5017c478bd9Sstevel@tonic-gate 		oldmapcount--;
5027c478bd9Sstevel@tonic-gate 		mptr++;
5037c478bd9Sstevel@tonic-gate 	}
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate 	free(Pmap);
5067c478bd9Sstevel@tonic-gate 	if (P->mappings != NULL)
5077c478bd9Sstevel@tonic-gate 		free(P->mappings);
5087c478bd9Sstevel@tonic-gate 	P->mappings = newmap;
5097c478bd9Sstevel@tonic-gate 	P->map_count = P->map_alloc = nmap;
5107c478bd9Sstevel@tonic-gate 	P->info_valid = 1;
5117c478bd9Sstevel@tonic-gate 
5127c478bd9Sstevel@tonic-gate 	/*
5137c478bd9Sstevel@tonic-gate 	 * Consult librtld_db to get the load object
5147c478bd9Sstevel@tonic-gate 	 * names for all of the shared libraries.
5157c478bd9Sstevel@tonic-gate 	 */
5167c478bd9Sstevel@tonic-gate 	if (P->rap != NULL)
5177c478bd9Sstevel@tonic-gate 		(void) rd_loadobj_iter(P->rap, map_iter, P);
5187c478bd9Sstevel@tonic-gate }
5197c478bd9Sstevel@tonic-gate 
5207c478bd9Sstevel@tonic-gate /*
5217c478bd9Sstevel@tonic-gate  * Update all of the mappings and rtld_db as if by Pupdate_maps(), and then
5227c478bd9Sstevel@tonic-gate  * forcibly cache all of the symbol tables associated with all object files.
5237c478bd9Sstevel@tonic-gate  */
5247c478bd9Sstevel@tonic-gate void
5257c478bd9Sstevel@tonic-gate Pupdate_syms(struct ps_prochandle *P)
5267c478bd9Sstevel@tonic-gate {
5277c478bd9Sstevel@tonic-gate 	file_info_t *fptr = list_next(&P->file_head);
5287c478bd9Sstevel@tonic-gate 	int i;
5297c478bd9Sstevel@tonic-gate 
5307c478bd9Sstevel@tonic-gate 	Pupdate_maps(P);
5317c478bd9Sstevel@tonic-gate 
5327c478bd9Sstevel@tonic-gate 	for (i = 0; i < P->num_files; i++, fptr = list_next(fptr)) {
5337c478bd9Sstevel@tonic-gate 		Pbuild_file_symtab(P, fptr);
5347c478bd9Sstevel@tonic-gate 		(void) Pbuild_file_ctf(P, fptr);
5357c478bd9Sstevel@tonic-gate 	}
5367c478bd9Sstevel@tonic-gate }
5377c478bd9Sstevel@tonic-gate 
5387c478bd9Sstevel@tonic-gate /*
5397c478bd9Sstevel@tonic-gate  * Return the librtld_db agent handle for the victim process.
5407c478bd9Sstevel@tonic-gate  * The handle will become invalid at the next successful exec() and the
5417c478bd9Sstevel@tonic-gate  * client (caller of proc_rd_agent()) must not use it beyond that point.
5427c478bd9Sstevel@tonic-gate  * If the process is already dead, we've already tried our best to
5437c478bd9Sstevel@tonic-gate  * create the agent during core file initialization.
5447c478bd9Sstevel@tonic-gate  */
5457c478bd9Sstevel@tonic-gate rd_agent_t *
5467c478bd9Sstevel@tonic-gate Prd_agent(struct ps_prochandle *P)
5477c478bd9Sstevel@tonic-gate {
5487c478bd9Sstevel@tonic-gate 	if (P->rap == NULL && P->state != PS_DEAD && P->state != PS_IDLE) {
5497c478bd9Sstevel@tonic-gate 		Pupdate_maps(P);
5507c478bd9Sstevel@tonic-gate 		if (P->num_files == 0)
5517c478bd9Sstevel@tonic-gate 			load_static_maps(P);
5527c478bd9Sstevel@tonic-gate 		rd_log(_libproc_debug);
5537c478bd9Sstevel@tonic-gate 		if ((P->rap = rd_new(P)) != NULL)
5547c478bd9Sstevel@tonic-gate 			(void) rd_loadobj_iter(P->rap, map_iter, P);
5557c478bd9Sstevel@tonic-gate 	}
5567c478bd9Sstevel@tonic-gate 	return (P->rap);
5577c478bd9Sstevel@tonic-gate }
5587c478bd9Sstevel@tonic-gate 
5597c478bd9Sstevel@tonic-gate /*
5607c478bd9Sstevel@tonic-gate  * Return the prmap_t structure containing 'addr', but only if it
5617c478bd9Sstevel@tonic-gate  * is in the dynamic linker's link map and is the text section.
5627c478bd9Sstevel@tonic-gate  */
5637c478bd9Sstevel@tonic-gate const prmap_t *
5647c478bd9Sstevel@tonic-gate Paddr_to_text_map(struct ps_prochandle *P, uintptr_t addr)
5657c478bd9Sstevel@tonic-gate {
5667c478bd9Sstevel@tonic-gate 	map_info_t *mptr;
5677c478bd9Sstevel@tonic-gate 
5687c478bd9Sstevel@tonic-gate 	if (!P->info_valid)
5697c478bd9Sstevel@tonic-gate 		Pupdate_maps(P);
5707c478bd9Sstevel@tonic-gate 
5717c478bd9Sstevel@tonic-gate 	if ((mptr = Paddr2mptr(P, addr)) != NULL) {
5727c478bd9Sstevel@tonic-gate 		file_info_t *fptr = build_map_symtab(P, mptr);
5737c478bd9Sstevel@tonic-gate 		const prmap_t *pmp = &mptr->map_pmap;
5747c478bd9Sstevel@tonic-gate 
5757c478bd9Sstevel@tonic-gate 		if (fptr != NULL && fptr->file_lo != NULL &&
5767c478bd9Sstevel@tonic-gate 		    fptr->file_lo->rl_base >= pmp->pr_vaddr &&
5777c478bd9Sstevel@tonic-gate 		    fptr->file_lo->rl_base < pmp->pr_vaddr + pmp->pr_size)
5787c478bd9Sstevel@tonic-gate 			return (pmp);
5797c478bd9Sstevel@tonic-gate 	}
5807c478bd9Sstevel@tonic-gate 
5817c478bd9Sstevel@tonic-gate 	return (NULL);
5827c478bd9Sstevel@tonic-gate }
5837c478bd9Sstevel@tonic-gate 
5847c478bd9Sstevel@tonic-gate /*
5857c478bd9Sstevel@tonic-gate  * Return the prmap_t structure containing 'addr' (no restrictions on
5867c478bd9Sstevel@tonic-gate  * the type of mapping).
5877c478bd9Sstevel@tonic-gate  */
5887c478bd9Sstevel@tonic-gate const prmap_t *
5897c478bd9Sstevel@tonic-gate Paddr_to_map(struct ps_prochandle *P, uintptr_t addr)
5907c478bd9Sstevel@tonic-gate {
5917c478bd9Sstevel@tonic-gate 	map_info_t *mptr;
5927c478bd9Sstevel@tonic-gate 
5937c478bd9Sstevel@tonic-gate 	if (!P->info_valid)
5947c478bd9Sstevel@tonic-gate 		Pupdate_maps(P);
5957c478bd9Sstevel@tonic-gate 
5967c478bd9Sstevel@tonic-gate 	if ((mptr = Paddr2mptr(P, addr)) != NULL)
5977c478bd9Sstevel@tonic-gate 		return (&mptr->map_pmap);
5987c478bd9Sstevel@tonic-gate 
5997c478bd9Sstevel@tonic-gate 	return (NULL);
6007c478bd9Sstevel@tonic-gate }
6017c478bd9Sstevel@tonic-gate 
6027c478bd9Sstevel@tonic-gate /*
6037c478bd9Sstevel@tonic-gate  * Convert a full or partial load object name to the prmap_t for its
6047c478bd9Sstevel@tonic-gate  * corresponding primary text mapping.
6057c478bd9Sstevel@tonic-gate  */
6067c478bd9Sstevel@tonic-gate const prmap_t *
6077c478bd9Sstevel@tonic-gate Plmid_to_map(struct ps_prochandle *P, Lmid_t lmid, const char *name)
6087c478bd9Sstevel@tonic-gate {
6097c478bd9Sstevel@tonic-gate 	map_info_t *mptr;
6107c478bd9Sstevel@tonic-gate 
6117c478bd9Sstevel@tonic-gate 	if (name == PR_OBJ_EVERY)
6127c478bd9Sstevel@tonic-gate 		return (NULL); /* A reasonable mistake */
6137c478bd9Sstevel@tonic-gate 
6147c478bd9Sstevel@tonic-gate 	if ((mptr = object_name_to_map(P, lmid, name)) != NULL)
6157c478bd9Sstevel@tonic-gate 		return (&mptr->map_pmap);
6167c478bd9Sstevel@tonic-gate 
6177c478bd9Sstevel@tonic-gate 	return (NULL);
6187c478bd9Sstevel@tonic-gate }
6197c478bd9Sstevel@tonic-gate 
6207c478bd9Sstevel@tonic-gate const prmap_t *
6217c478bd9Sstevel@tonic-gate Pname_to_map(struct ps_prochandle *P, const char *name)
6227c478bd9Sstevel@tonic-gate {
6237c478bd9Sstevel@tonic-gate 	return (Plmid_to_map(P, PR_LMID_EVERY, name));
6247c478bd9Sstevel@tonic-gate }
6257c478bd9Sstevel@tonic-gate 
6267c478bd9Sstevel@tonic-gate const rd_loadobj_t *
6277c478bd9Sstevel@tonic-gate Paddr_to_loadobj(struct ps_prochandle *P, uintptr_t addr)
6287c478bd9Sstevel@tonic-gate {
6297c478bd9Sstevel@tonic-gate 	map_info_t *mptr;
6307c478bd9Sstevel@tonic-gate 
6317c478bd9Sstevel@tonic-gate 	if (!P->info_valid)
6327c478bd9Sstevel@tonic-gate 		Pupdate_maps(P);
6337c478bd9Sstevel@tonic-gate 
6347c478bd9Sstevel@tonic-gate 	if ((mptr = Paddr2mptr(P, addr)) == NULL)
6357c478bd9Sstevel@tonic-gate 		return (NULL);
6367c478bd9Sstevel@tonic-gate 
6377c478bd9Sstevel@tonic-gate 	/*
6387c478bd9Sstevel@tonic-gate 	 * By building the symbol table, we implicitly bring the PLT
6397c478bd9Sstevel@tonic-gate 	 * information up to date in the load object.
6407c478bd9Sstevel@tonic-gate 	 */
6417c478bd9Sstevel@tonic-gate 	(void) build_map_symtab(P, mptr);
6427c478bd9Sstevel@tonic-gate 
6437c478bd9Sstevel@tonic-gate 	return (mptr->map_file->file_lo);
6447c478bd9Sstevel@tonic-gate }
6457c478bd9Sstevel@tonic-gate 
6467c478bd9Sstevel@tonic-gate const rd_loadobj_t *
6477c478bd9Sstevel@tonic-gate Plmid_to_loadobj(struct ps_prochandle *P, Lmid_t lmid, const char *name)
6487c478bd9Sstevel@tonic-gate {
6497c478bd9Sstevel@tonic-gate 	map_info_t *mptr;
6507c478bd9Sstevel@tonic-gate 
6517c478bd9Sstevel@tonic-gate 	if (name == PR_OBJ_EVERY)
6527c478bd9Sstevel@tonic-gate 		return (NULL);
6537c478bd9Sstevel@tonic-gate 
6547c478bd9Sstevel@tonic-gate 	if ((mptr = object_name_to_map(P, lmid, name)) == NULL)
6557c478bd9Sstevel@tonic-gate 		return (NULL);
6567c478bd9Sstevel@tonic-gate 
6577c478bd9Sstevel@tonic-gate 	/*
6587c478bd9Sstevel@tonic-gate 	 * By building the symbol table, we implicitly bring the PLT
6597c478bd9Sstevel@tonic-gate 	 * information up to date in the load object.
6607c478bd9Sstevel@tonic-gate 	 */
6617c478bd9Sstevel@tonic-gate 	(void) build_map_symtab(P, mptr);
6627c478bd9Sstevel@tonic-gate 
6637c478bd9Sstevel@tonic-gate 	return (mptr->map_file->file_lo);
6647c478bd9Sstevel@tonic-gate }
6657c478bd9Sstevel@tonic-gate 
6667c478bd9Sstevel@tonic-gate const rd_loadobj_t *
6677c478bd9Sstevel@tonic-gate Pname_to_loadobj(struct ps_prochandle *P, const char *name)
6687c478bd9Sstevel@tonic-gate {
6697c478bd9Sstevel@tonic-gate 	return (Plmid_to_loadobj(P, PR_LMID_EVERY, name));
6707c478bd9Sstevel@tonic-gate }
6717c478bd9Sstevel@tonic-gate 
6727c478bd9Sstevel@tonic-gate ctf_file_t *
6737c478bd9Sstevel@tonic-gate Pbuild_file_ctf(struct ps_prochandle *P, file_info_t *fptr)
6747c478bd9Sstevel@tonic-gate {
6757c478bd9Sstevel@tonic-gate 	ctf_sect_t ctdata, symtab, strtab;
6767c478bd9Sstevel@tonic-gate 	sym_tbl_t *symp;
6777c478bd9Sstevel@tonic-gate 	int err;
6787c478bd9Sstevel@tonic-gate 
6797c478bd9Sstevel@tonic-gate 	if (fptr->file_ctfp != NULL)
6807c478bd9Sstevel@tonic-gate 		return (fptr->file_ctfp);
6817c478bd9Sstevel@tonic-gate 
6827c478bd9Sstevel@tonic-gate 	Pbuild_file_symtab(P, fptr);
6837c478bd9Sstevel@tonic-gate 
6847c478bd9Sstevel@tonic-gate 	if (fptr->file_ctf_size == 0)
6857c478bd9Sstevel@tonic-gate 		return (NULL);
6867c478bd9Sstevel@tonic-gate 
6877c478bd9Sstevel@tonic-gate 	symp = fptr->file_ctf_dyn ? &fptr->file_dynsym : &fptr->file_symtab;
6887c478bd9Sstevel@tonic-gate 	if (symp->sym_data == NULL)
6897c478bd9Sstevel@tonic-gate 		return (NULL);
6907c478bd9Sstevel@tonic-gate 
6917c478bd9Sstevel@tonic-gate 	/*
6927c478bd9Sstevel@tonic-gate 	 * The buffer may alread be allocated if this is a core file that
6937c478bd9Sstevel@tonic-gate 	 * contained CTF data for this file.
6947c478bd9Sstevel@tonic-gate 	 */
6957c478bd9Sstevel@tonic-gate 	if (fptr->file_ctf_buf == NULL) {
6967c478bd9Sstevel@tonic-gate 		fptr->file_ctf_buf = malloc(fptr->file_ctf_size);
6977c478bd9Sstevel@tonic-gate 		if (fptr->file_ctf_buf == NULL) {
6987c478bd9Sstevel@tonic-gate 			dprintf("failed to allocate ctf buffer\n");
6997c478bd9Sstevel@tonic-gate 			return (NULL);
7007c478bd9Sstevel@tonic-gate 		}
7017c478bd9Sstevel@tonic-gate 
7027c478bd9Sstevel@tonic-gate 		if (pread(fptr->file_fd, fptr->file_ctf_buf,
7037c478bd9Sstevel@tonic-gate 		    fptr->file_ctf_size, fptr->file_ctf_off) !=
7047c478bd9Sstevel@tonic-gate 		    fptr->file_ctf_size) {
7057c478bd9Sstevel@tonic-gate 			free(fptr->file_ctf_buf);
7067c478bd9Sstevel@tonic-gate 			fptr->file_ctf_buf = NULL;
7077c478bd9Sstevel@tonic-gate 			dprintf("failed to read ctf data\n");
7087c478bd9Sstevel@tonic-gate 			return (NULL);
7097c478bd9Sstevel@tonic-gate 		}
7107c478bd9Sstevel@tonic-gate 	}
7117c478bd9Sstevel@tonic-gate 
7127c478bd9Sstevel@tonic-gate 	ctdata.cts_name = ".SUNW_ctf";
7137c478bd9Sstevel@tonic-gate 	ctdata.cts_type = SHT_PROGBITS;
7147c478bd9Sstevel@tonic-gate 	ctdata.cts_flags = 0;
7157c478bd9Sstevel@tonic-gate 	ctdata.cts_data = fptr->file_ctf_buf;
7167c478bd9Sstevel@tonic-gate 	ctdata.cts_size = fptr->file_ctf_size;
7177c478bd9Sstevel@tonic-gate 	ctdata.cts_entsize = 1;
7187c478bd9Sstevel@tonic-gate 	ctdata.cts_offset = 0;
7197c478bd9Sstevel@tonic-gate 
7207c478bd9Sstevel@tonic-gate 	symtab.cts_name = fptr->file_ctf_dyn ? ".dynsym" : ".symtab";
7217c478bd9Sstevel@tonic-gate 	symtab.cts_type = symp->sym_hdr.sh_type;
7227c478bd9Sstevel@tonic-gate 	symtab.cts_flags = symp->sym_hdr.sh_flags;
7237c478bd9Sstevel@tonic-gate 	symtab.cts_data = symp->sym_data->d_buf;
7247c478bd9Sstevel@tonic-gate 	symtab.cts_size = symp->sym_hdr.sh_size;
7257c478bd9Sstevel@tonic-gate 	symtab.cts_entsize = symp->sym_hdr.sh_entsize;
7267c478bd9Sstevel@tonic-gate 	symtab.cts_offset = symp->sym_hdr.sh_offset;
7277c478bd9Sstevel@tonic-gate 
7287c478bd9Sstevel@tonic-gate 	strtab.cts_name = fptr->file_ctf_dyn ? ".dynstr" : ".strtab";
7297c478bd9Sstevel@tonic-gate 	strtab.cts_type = symp->sym_strhdr.sh_type;
7307c478bd9Sstevel@tonic-gate 	strtab.cts_flags = symp->sym_strhdr.sh_flags;
7317c478bd9Sstevel@tonic-gate 	strtab.cts_data = symp->sym_strs;
7327c478bd9Sstevel@tonic-gate 	strtab.cts_size = symp->sym_strhdr.sh_size;
7337c478bd9Sstevel@tonic-gate 	strtab.cts_entsize = symp->sym_strhdr.sh_entsize;
7347c478bd9Sstevel@tonic-gate 	strtab.cts_offset = symp->sym_strhdr.sh_offset;
7357c478bd9Sstevel@tonic-gate 
7367c478bd9Sstevel@tonic-gate 	fptr->file_ctfp = ctf_bufopen(&ctdata, &symtab, &strtab, &err);
7377c478bd9Sstevel@tonic-gate 	if (fptr->file_ctfp == NULL) {
7387c478bd9Sstevel@tonic-gate 		free(fptr->file_ctf_buf);
7397c478bd9Sstevel@tonic-gate 		fptr->file_ctf_buf = NULL;
7407c478bd9Sstevel@tonic-gate 		return (NULL);
7417c478bd9Sstevel@tonic-gate 	}
7427c478bd9Sstevel@tonic-gate 
7437c478bd9Sstevel@tonic-gate 	dprintf("loaded %lu bytes of CTF data for %s\n",
7447c478bd9Sstevel@tonic-gate 	    (ulong_t)fptr->file_ctf_size, fptr->file_pname);
7457c478bd9Sstevel@tonic-gate 
7467c478bd9Sstevel@tonic-gate 	return (fptr->file_ctfp);
7477c478bd9Sstevel@tonic-gate }
7487c478bd9Sstevel@tonic-gate 
7497c478bd9Sstevel@tonic-gate ctf_file_t *
7507c478bd9Sstevel@tonic-gate Paddr_to_ctf(struct ps_prochandle *P, uintptr_t addr)
7517c478bd9Sstevel@tonic-gate {
7527c478bd9Sstevel@tonic-gate 	map_info_t *mptr;
7537c478bd9Sstevel@tonic-gate 	file_info_t *fptr;
7547c478bd9Sstevel@tonic-gate 
7557c478bd9Sstevel@tonic-gate 	if (!P->info_valid)
7567c478bd9Sstevel@tonic-gate 		Pupdate_maps(P);
7577c478bd9Sstevel@tonic-gate 
7587c478bd9Sstevel@tonic-gate 	if ((mptr = Paddr2mptr(P, addr)) == NULL ||
7597c478bd9Sstevel@tonic-gate 	    (fptr = mptr->map_file) == NULL)
7607c478bd9Sstevel@tonic-gate 		return (NULL);
7617c478bd9Sstevel@tonic-gate 
7627c478bd9Sstevel@tonic-gate 	return (Pbuild_file_ctf(P, fptr));
7637c478bd9Sstevel@tonic-gate }
7647c478bd9Sstevel@tonic-gate 
7657c478bd9Sstevel@tonic-gate ctf_file_t *
7667c478bd9Sstevel@tonic-gate Plmid_to_ctf(struct ps_prochandle *P, Lmid_t lmid, const char *name)
7677c478bd9Sstevel@tonic-gate {
7687c478bd9Sstevel@tonic-gate 	map_info_t *mptr;
7697c478bd9Sstevel@tonic-gate 	file_info_t *fptr;
7707c478bd9Sstevel@tonic-gate 
7717c478bd9Sstevel@tonic-gate 	if (name == PR_OBJ_EVERY)
7727c478bd9Sstevel@tonic-gate 		return (NULL);
7737c478bd9Sstevel@tonic-gate 
7747c478bd9Sstevel@tonic-gate 	if ((mptr = object_name_to_map(P, lmid, name)) == NULL ||
7757c478bd9Sstevel@tonic-gate 	    (fptr = mptr->map_file) == NULL)
7767c478bd9Sstevel@tonic-gate 		return (NULL);
7777c478bd9Sstevel@tonic-gate 
7787c478bd9Sstevel@tonic-gate 	return (Pbuild_file_ctf(P, fptr));
7797c478bd9Sstevel@tonic-gate }
7807c478bd9Sstevel@tonic-gate 
7817c478bd9Sstevel@tonic-gate ctf_file_t *
7827c478bd9Sstevel@tonic-gate Pname_to_ctf(struct ps_prochandle *P, const char *name)
7837c478bd9Sstevel@tonic-gate {
7847c478bd9Sstevel@tonic-gate 	return (Plmid_to_ctf(P, PR_LMID_EVERY, name));
7857c478bd9Sstevel@tonic-gate }
7867c478bd9Sstevel@tonic-gate 
7877c478bd9Sstevel@tonic-gate /*
7887c478bd9Sstevel@tonic-gate  * If we're not a core file, re-read the /proc/<pid>/auxv file and store
7897c478bd9Sstevel@tonic-gate  * its contents in P->auxv.  In the case of a core file, we either
7907c478bd9Sstevel@tonic-gate  * initialized P->auxv in Pcore() from the NT_AUXV, or we don't have an
7917c478bd9Sstevel@tonic-gate  * auxv because the note was missing.
7927c478bd9Sstevel@tonic-gate  */
7937c478bd9Sstevel@tonic-gate void
7947c478bd9Sstevel@tonic-gate Preadauxvec(struct ps_prochandle *P)
7957c478bd9Sstevel@tonic-gate {
7967c478bd9Sstevel@tonic-gate 	char auxfile[64];
7977c478bd9Sstevel@tonic-gate 	struct stat statb;
7987c478bd9Sstevel@tonic-gate 	ssize_t naux;
7997c478bd9Sstevel@tonic-gate 	int fd;
8007c478bd9Sstevel@tonic-gate 
8017c478bd9Sstevel@tonic-gate 	if (P->state == PS_DEAD)
8027c478bd9Sstevel@tonic-gate 		return; /* Already read during Pgrab_core() */
8037c478bd9Sstevel@tonic-gate 	if (P->state == PS_IDLE)
8047c478bd9Sstevel@tonic-gate 		return; /* No aux vec for Pgrab_file() */
8057c478bd9Sstevel@tonic-gate 
8067c478bd9Sstevel@tonic-gate 	if (P->auxv != NULL) {
8077c478bd9Sstevel@tonic-gate 		free(P->auxv);
8087c478bd9Sstevel@tonic-gate 		P->auxv = NULL;
8097c478bd9Sstevel@tonic-gate 		P->nauxv = 0;
8107c478bd9Sstevel@tonic-gate 	}
8117c478bd9Sstevel@tonic-gate 
812*9acbbeafSnn35248 	(void) snprintf(auxfile, sizeof (auxfile), "%s/%d/auxv",
813*9acbbeafSnn35248 	    procfs_path, (int)P->pid);
8147c478bd9Sstevel@tonic-gate 	if ((fd = open(auxfile, O_RDONLY)) < 0)
8157c478bd9Sstevel@tonic-gate 		return;
8167c478bd9Sstevel@tonic-gate 
8177c478bd9Sstevel@tonic-gate 	if (fstat(fd, &statb) == 0 &&
8187c478bd9Sstevel@tonic-gate 	    statb.st_size >= sizeof (auxv_t) &&
8197c478bd9Sstevel@tonic-gate 	    (P->auxv = malloc(statb.st_size + sizeof (auxv_t))) != NULL) {
8207c478bd9Sstevel@tonic-gate 		if ((naux = read(fd, P->auxv, statb.st_size)) < 0 ||
8217c478bd9Sstevel@tonic-gate 		    (naux /= sizeof (auxv_t)) < 1) {
8227c478bd9Sstevel@tonic-gate 			free(P->auxv);
8237c478bd9Sstevel@tonic-gate 			P->auxv = NULL;
8247c478bd9Sstevel@tonic-gate 		} else {
8257c478bd9Sstevel@tonic-gate 			P->auxv[naux].a_type = AT_NULL;
8267c478bd9Sstevel@tonic-gate 			P->auxv[naux].a_un.a_val = 0L;
8277c478bd9Sstevel@tonic-gate 			P->nauxv = (int)naux;
8287c478bd9Sstevel@tonic-gate 		}
8297c478bd9Sstevel@tonic-gate 	}
8307c478bd9Sstevel@tonic-gate 
8317c478bd9Sstevel@tonic-gate 	(void) close(fd);
8327c478bd9Sstevel@tonic-gate }
8337c478bd9Sstevel@tonic-gate 
8347c478bd9Sstevel@tonic-gate /*
8357c478bd9Sstevel@tonic-gate  * Return a requested element from the process's aux vector.
8367c478bd9Sstevel@tonic-gate  * Return -1 on failure (this is adequate for our purposes).
8377c478bd9Sstevel@tonic-gate  */
8387c478bd9Sstevel@tonic-gate long
8397c478bd9Sstevel@tonic-gate Pgetauxval(struct ps_prochandle *P, int type)
8407c478bd9Sstevel@tonic-gate {
8417c478bd9Sstevel@tonic-gate 	auxv_t *auxv;
8427c478bd9Sstevel@tonic-gate 
8437c478bd9Sstevel@tonic-gate 	if (P->auxv == NULL)
8447c478bd9Sstevel@tonic-gate 		Preadauxvec(P);
8457c478bd9Sstevel@tonic-gate 
8467c478bd9Sstevel@tonic-gate 	if (P->auxv == NULL)
8477c478bd9Sstevel@tonic-gate 		return (-1);
8487c478bd9Sstevel@tonic-gate 
8497c478bd9Sstevel@tonic-gate 	for (auxv = P->auxv; auxv->a_type != AT_NULL; auxv++) {
8507c478bd9Sstevel@tonic-gate 		if (auxv->a_type == type)
8517c478bd9Sstevel@tonic-gate 			return (auxv->a_un.a_val);
8527c478bd9Sstevel@tonic-gate 	}
8537c478bd9Sstevel@tonic-gate 
8547c478bd9Sstevel@tonic-gate 	return (-1);
8557c478bd9Sstevel@tonic-gate }
8567c478bd9Sstevel@tonic-gate 
8577c478bd9Sstevel@tonic-gate /*
8587c478bd9Sstevel@tonic-gate  * Return a pointer to our internal copy of the process's aux vector.
8597c478bd9Sstevel@tonic-gate  * The caller should not hold on to this pointer across any libproc calls.
8607c478bd9Sstevel@tonic-gate  */
8617c478bd9Sstevel@tonic-gate const auxv_t *
8627c478bd9Sstevel@tonic-gate Pgetauxvec(struct ps_prochandle *P)
8637c478bd9Sstevel@tonic-gate {
8647c478bd9Sstevel@tonic-gate 	static const auxv_t empty = { AT_NULL, 0L };
8657c478bd9Sstevel@tonic-gate 
8667c478bd9Sstevel@tonic-gate 	if (P->auxv == NULL)
8677c478bd9Sstevel@tonic-gate 		Preadauxvec(P);
8687c478bd9Sstevel@tonic-gate 
8697c478bd9Sstevel@tonic-gate 	if (P->auxv == NULL)
8707c478bd9Sstevel@tonic-gate 		return (&empty);
8717c478bd9Sstevel@tonic-gate 
8727c478bd9Sstevel@tonic-gate 	return (P->auxv);
8737c478bd9Sstevel@tonic-gate }
8747c478bd9Sstevel@tonic-gate 
8757c478bd9Sstevel@tonic-gate /*
8767c478bd9Sstevel@tonic-gate  * Find or build the symbol table for the given mapping.
8777c478bd9Sstevel@tonic-gate  */
8787c478bd9Sstevel@tonic-gate static file_info_t *
8797c478bd9Sstevel@tonic-gate build_map_symtab(struct ps_prochandle *P, map_info_t *mptr)
8807c478bd9Sstevel@tonic-gate {
8817c478bd9Sstevel@tonic-gate 	prmap_t *pmap = &mptr->map_pmap;
8827c478bd9Sstevel@tonic-gate 	file_info_t *fptr;
8837c478bd9Sstevel@tonic-gate 	rd_loadobj_t *lop;
8847c478bd9Sstevel@tonic-gate 	uint_t i;
8857c478bd9Sstevel@tonic-gate 
8867c478bd9Sstevel@tonic-gate 	if ((fptr = mptr->map_file) != NULL) {
8877c478bd9Sstevel@tonic-gate 		Pbuild_file_symtab(P, fptr);
8887c478bd9Sstevel@tonic-gate 		return (fptr);
8897c478bd9Sstevel@tonic-gate 	}
8907c478bd9Sstevel@tonic-gate 
8917c478bd9Sstevel@tonic-gate 	if (pmap->pr_mapname[0] == '\0')
8927c478bd9Sstevel@tonic-gate 		return (NULL);
8937c478bd9Sstevel@tonic-gate 
8947c478bd9Sstevel@tonic-gate 	/*
8957c478bd9Sstevel@tonic-gate 	 * Attempt to find a matching file.
8967c478bd9Sstevel@tonic-gate 	 * (A file can be mapped at several different addresses.)
8977c478bd9Sstevel@tonic-gate 	 */
8987c478bd9Sstevel@tonic-gate 	for (i = 0, fptr = list_next(&P->file_head); i < P->num_files;
8997c478bd9Sstevel@tonic-gate 	    i++, fptr = list_next(fptr)) {
9007c478bd9Sstevel@tonic-gate 		if (strcmp(fptr->file_pname, pmap->pr_mapname) == 0 &&
9017c478bd9Sstevel@tonic-gate 		    (lop = fptr->file_lo) != NULL &&
9027c478bd9Sstevel@tonic-gate 		    ((pmap->pr_vaddr <= lop->rl_base &&
9037c478bd9Sstevel@tonic-gate 		    lop->rl_base < pmap->pr_vaddr + pmap->pr_size) ||
9047c478bd9Sstevel@tonic-gate 		    (pmap->pr_vaddr <= lop->rl_data_base &&
9057c478bd9Sstevel@tonic-gate 		    lop->rl_data_base < pmap->pr_vaddr + pmap->pr_size))) {
9067c478bd9Sstevel@tonic-gate 			mptr->map_file = fptr;
9077c478bd9Sstevel@tonic-gate 			fptr->file_ref++;
9087c478bd9Sstevel@tonic-gate 			Pbuild_file_symtab(P, fptr);
9097c478bd9Sstevel@tonic-gate 			return (fptr);
9107c478bd9Sstevel@tonic-gate 		}
9117c478bd9Sstevel@tonic-gate 	}
9127c478bd9Sstevel@tonic-gate 
9137c478bd9Sstevel@tonic-gate 	/*
9147c478bd9Sstevel@tonic-gate 	 * If we need to create a new file_info structure, iterate
9157c478bd9Sstevel@tonic-gate 	 * through the load objects in order to attempt to connect
9167c478bd9Sstevel@tonic-gate 	 * this new file with its primary text mapping.  We again
9177c478bd9Sstevel@tonic-gate 	 * need to handle ld.so as a special case because we need
9187c478bd9Sstevel@tonic-gate 	 * to be able to bootstrap librtld_db.
9197c478bd9Sstevel@tonic-gate 	 */
9207c478bd9Sstevel@tonic-gate 	if ((fptr = file_info_new(P, mptr)) == NULL)
9217c478bd9Sstevel@tonic-gate 		return (NULL);
9227c478bd9Sstevel@tonic-gate 
9237c478bd9Sstevel@tonic-gate 	if (P->map_ldso != mptr) {
9247c478bd9Sstevel@tonic-gate 		if (P->rap != NULL)
9257c478bd9Sstevel@tonic-gate 			(void) rd_loadobj_iter(P->rap, map_iter, P);
9267c478bd9Sstevel@tonic-gate 		else
9277c478bd9Sstevel@tonic-gate 			(void) Prd_agent(P);
9287c478bd9Sstevel@tonic-gate 	} else {
9297c478bd9Sstevel@tonic-gate 		fptr->file_map = mptr;
9307c478bd9Sstevel@tonic-gate 	}
9317c478bd9Sstevel@tonic-gate 
9327c478bd9Sstevel@tonic-gate 	/*
9337c478bd9Sstevel@tonic-gate 	 * If librtld_db wasn't able to help us connect the file to a primary
9347c478bd9Sstevel@tonic-gate 	 * text mapping, set file_map to the current mapping because we require
9357c478bd9Sstevel@tonic-gate 	 * fptr->file_map to be set in Pbuild_file_symtab.  librtld_db may be
9367c478bd9Sstevel@tonic-gate 	 * unaware of what's going on in the rare case that a legitimate ELF
9377c478bd9Sstevel@tonic-gate 	 * file has been mmap(2)ed into the process address space *without*
938*9acbbeafSnn35248 	 * the use of dlopen(3x).
9397c478bd9Sstevel@tonic-gate 	 */
9407c478bd9Sstevel@tonic-gate 	if (fptr->file_map == NULL)
9417c478bd9Sstevel@tonic-gate 		fptr->file_map = mptr;
9427c478bd9Sstevel@tonic-gate 
9437c478bd9Sstevel@tonic-gate 	Pbuild_file_symtab(P, fptr);
9447c478bd9Sstevel@tonic-gate 
9457c478bd9Sstevel@tonic-gate 	return (fptr);
9467c478bd9Sstevel@tonic-gate }
9477c478bd9Sstevel@tonic-gate 
9487c478bd9Sstevel@tonic-gate static int
94930da1432Sahl read_ehdr32(struct ps_prochandle *P, Elf32_Ehdr *ehdr, uint_t *phnum,
95030da1432Sahl     uintptr_t addr)
9517c478bd9Sstevel@tonic-gate {
9527c478bd9Sstevel@tonic-gate 	if (Pread(P, ehdr, sizeof (*ehdr), addr) != sizeof (*ehdr))
9537c478bd9Sstevel@tonic-gate 		return (-1);
9547c478bd9Sstevel@tonic-gate 
9557c478bd9Sstevel@tonic-gate 	if (ehdr->e_ident[EI_MAG0] != ELFMAG0 ||
9567c478bd9Sstevel@tonic-gate 	    ehdr->e_ident[EI_MAG1] != ELFMAG1 ||
9577c478bd9Sstevel@tonic-gate 	    ehdr->e_ident[EI_MAG2] != ELFMAG2 ||
9587c478bd9Sstevel@tonic-gate 	    ehdr->e_ident[EI_MAG3] != ELFMAG3 ||
9597c478bd9Sstevel@tonic-gate 	    ehdr->e_ident[EI_CLASS] != ELFCLASS32 ||
9607c478bd9Sstevel@tonic-gate #ifdef _BIG_ENDIAN
9617c478bd9Sstevel@tonic-gate 	    ehdr->e_ident[EI_DATA] != ELFDATA2MSB ||
9627c478bd9Sstevel@tonic-gate #else
9637c478bd9Sstevel@tonic-gate 	    ehdr->e_ident[EI_DATA] != ELFDATA2LSB ||
9647c478bd9Sstevel@tonic-gate #endif
9657c478bd9Sstevel@tonic-gate 	    ehdr->e_ident[EI_VERSION] != EV_CURRENT)
9667c478bd9Sstevel@tonic-gate 		return (-1);
9677c478bd9Sstevel@tonic-gate 
96830da1432Sahl 	if ((*phnum = ehdr->e_phnum) == PN_XNUM) {
96930da1432Sahl 		Elf32_Shdr shdr0;
97030da1432Sahl 
97130da1432Sahl 		if (ehdr->e_shoff == 0 || ehdr->e_shentsize < sizeof (shdr0) ||
97230da1432Sahl 		    Pread(P, &shdr0, sizeof (shdr0), addr + ehdr->e_shoff) !=
97330da1432Sahl 		    sizeof (shdr0))
97430da1432Sahl 			return (-1);
97530da1432Sahl 
97630da1432Sahl 		if (shdr0.sh_info != 0)
97730da1432Sahl 			*phnum = shdr0.sh_info;
97830da1432Sahl 	}
97930da1432Sahl 
9807c478bd9Sstevel@tonic-gate 	return (0);
9817c478bd9Sstevel@tonic-gate }
9827c478bd9Sstevel@tonic-gate 
9837c478bd9Sstevel@tonic-gate static int
9847c478bd9Sstevel@tonic-gate read_dynamic_phdr32(struct ps_prochandle *P, const Elf32_Ehdr *ehdr,
98530da1432Sahl     uint_t phnum, Elf32_Phdr *phdr, uintptr_t addr)
9867c478bd9Sstevel@tonic-gate {
9877c478bd9Sstevel@tonic-gate 	uint_t i;
9887c478bd9Sstevel@tonic-gate 
98930da1432Sahl 	for (i = 0; i < phnum; i++) {
9907c478bd9Sstevel@tonic-gate 		uintptr_t a = addr + ehdr->e_phoff + i * ehdr->e_phentsize;
9917c478bd9Sstevel@tonic-gate 		if (Pread(P, phdr, sizeof (*phdr), a) != sizeof (*phdr))
9927c478bd9Sstevel@tonic-gate 			return (-1);
9937c478bd9Sstevel@tonic-gate 
9947c478bd9Sstevel@tonic-gate 		if (phdr->p_type == PT_DYNAMIC)
9957c478bd9Sstevel@tonic-gate 			return (0);
9967c478bd9Sstevel@tonic-gate 	}
9977c478bd9Sstevel@tonic-gate 
9987c478bd9Sstevel@tonic-gate 	return (-1);
9997c478bd9Sstevel@tonic-gate }
10007c478bd9Sstevel@tonic-gate 
10017c478bd9Sstevel@tonic-gate #ifdef _LP64
10027c478bd9Sstevel@tonic-gate static int
100330da1432Sahl read_ehdr64(struct ps_prochandle *P, Elf64_Ehdr *ehdr, uint_t *phnum,
100430da1432Sahl     uintptr_t addr)
10057c478bd9Sstevel@tonic-gate {
10067c478bd9Sstevel@tonic-gate 	if (Pread(P, ehdr, sizeof (Elf64_Ehdr), addr) != sizeof (Elf64_Ehdr))
10077c478bd9Sstevel@tonic-gate 		return (-1);
10087c478bd9Sstevel@tonic-gate 
10097c478bd9Sstevel@tonic-gate 	if (ehdr->e_ident[EI_MAG0] != ELFMAG0 ||
10107c478bd9Sstevel@tonic-gate 	    ehdr->e_ident[EI_MAG1] != ELFMAG1 ||
10117c478bd9Sstevel@tonic-gate 	    ehdr->e_ident[EI_MAG2] != ELFMAG2 ||
10127c478bd9Sstevel@tonic-gate 	    ehdr->e_ident[EI_MAG3] != ELFMAG3 ||
10137c478bd9Sstevel@tonic-gate 	    ehdr->e_ident[EI_CLASS] != ELFCLASS64 ||
10147c478bd9Sstevel@tonic-gate #ifdef _BIG_ENDIAN
10157c478bd9Sstevel@tonic-gate 	    ehdr->e_ident[EI_DATA] != ELFDATA2MSB ||
10167c478bd9Sstevel@tonic-gate #else
10177c478bd9Sstevel@tonic-gate 	    ehdr->e_ident[EI_DATA] != ELFDATA2LSB ||
10187c478bd9Sstevel@tonic-gate #endif
10197c478bd9Sstevel@tonic-gate 	    ehdr->e_ident[EI_VERSION] != EV_CURRENT)
10207c478bd9Sstevel@tonic-gate 		return (-1);
10217c478bd9Sstevel@tonic-gate 
102230da1432Sahl 	if ((*phnum = ehdr->e_phnum) == PN_XNUM) {
102330da1432Sahl 		Elf64_Shdr shdr0;
102430da1432Sahl 
102530da1432Sahl 		if (ehdr->e_shoff == 0 || ehdr->e_shentsize < sizeof (shdr0) ||
102630da1432Sahl 		    Pread(P, &shdr0, sizeof (shdr0), addr + ehdr->e_shoff) !=
102730da1432Sahl 		    sizeof (shdr0))
102830da1432Sahl 			return (-1);
102930da1432Sahl 
103030da1432Sahl 		if (shdr0.sh_info != 0)
103130da1432Sahl 			*phnum = shdr0.sh_info;
103230da1432Sahl 	}
103330da1432Sahl 
10347c478bd9Sstevel@tonic-gate 	return (0);
10357c478bd9Sstevel@tonic-gate }
10367c478bd9Sstevel@tonic-gate 
10377c478bd9Sstevel@tonic-gate static int
10387c478bd9Sstevel@tonic-gate read_dynamic_phdr64(struct ps_prochandle *P, const Elf64_Ehdr *ehdr,
103930da1432Sahl     uint_t phnum, Elf64_Phdr *phdr, uintptr_t addr)
10407c478bd9Sstevel@tonic-gate {
10417c478bd9Sstevel@tonic-gate 	uint_t i;
10427c478bd9Sstevel@tonic-gate 
104330da1432Sahl 	for (i = 0; i < phnum; i++) {
10447c478bd9Sstevel@tonic-gate 		uintptr_t a = addr + ehdr->e_phoff + i * ehdr->e_phentsize;
10457c478bd9Sstevel@tonic-gate 		if (Pread(P, phdr, sizeof (*phdr), a) != sizeof (*phdr))
10467c478bd9Sstevel@tonic-gate 			return (-1);
10477c478bd9Sstevel@tonic-gate 
10487c478bd9Sstevel@tonic-gate 		if (phdr->p_type == PT_DYNAMIC)
10497c478bd9Sstevel@tonic-gate 			return (0);
10507c478bd9Sstevel@tonic-gate 	}
10517c478bd9Sstevel@tonic-gate 
10527c478bd9Sstevel@tonic-gate 	return (-1);
10537c478bd9Sstevel@tonic-gate }
10547c478bd9Sstevel@tonic-gate #endif	/* _LP64 */
10557c478bd9Sstevel@tonic-gate 
10567c478bd9Sstevel@tonic-gate /*
10577c478bd9Sstevel@tonic-gate  * The text segment for each load object contains the elf header and
10587c478bd9Sstevel@tonic-gate  * program headers. We can use this information to determine if the
10597c478bd9Sstevel@tonic-gate  * file that corresponds to the load object is the same file that
10607c478bd9Sstevel@tonic-gate  * was loaded into the process's address space. There can be a discrepency
10617c478bd9Sstevel@tonic-gate  * if a file is recompiled after the process is started or if the target
10627c478bd9Sstevel@tonic-gate  * represents a core file from a differently configured system -- two
10637c478bd9Sstevel@tonic-gate  * common examples. The DT_CHECKSUM entry in the dynamic section
10647c478bd9Sstevel@tonic-gate  * provides an easy method of comparison. It is important to note that
10657c478bd9Sstevel@tonic-gate  * the dynamic section usually lives in the data segment, but the meta
10667c478bd9Sstevel@tonic-gate  * data we use to find the dynamic section lives in the text segment so
10677c478bd9Sstevel@tonic-gate  * if either of those segments is absent we can't proceed.
10687c478bd9Sstevel@tonic-gate  *
10697c478bd9Sstevel@tonic-gate  * We're looking through the elf file for several items: the symbol tables
10707c478bd9Sstevel@tonic-gate  * (both dynsym and symtab), the procedure linkage table (PLT) base,
10717c478bd9Sstevel@tonic-gate  * size, and relocation base, and the CTF information. Most of this can
10727c478bd9Sstevel@tonic-gate  * be recovered from the loaded image of the file itself, the exceptions
10737c478bd9Sstevel@tonic-gate  * being the symtab and CTF data.
10747c478bd9Sstevel@tonic-gate  *
10757c478bd9Sstevel@tonic-gate  * First we try to open the file that we think corresponds to the load
10767c478bd9Sstevel@tonic-gate  * object, if the DT_CHECKSUM values match, we're all set, and can simply
10777c478bd9Sstevel@tonic-gate  * recover all the information we need from the file. If the values of
10787c478bd9Sstevel@tonic-gate  * DT_CHECKSUM don't match, or if we can't access the file for whatever
10797c478bd9Sstevel@tonic-gate  * reasaon, we fake up a elf file to use in its stead. If we can't read
10807c478bd9Sstevel@tonic-gate  * the elf data in the process's address space, we fall back to using
10817c478bd9Sstevel@tonic-gate  * the file even though it may give inaccurate information.
10827c478bd9Sstevel@tonic-gate  *
10837c478bd9Sstevel@tonic-gate  * The elf file that we fake up has to consist of sections for the
10847c478bd9Sstevel@tonic-gate  * dynsym, the PLT and the dynamic section. Note that in the case of a
10857c478bd9Sstevel@tonic-gate  * core file, we'll get the CTF data in the file_info_t later on from
10867c478bd9Sstevel@tonic-gate  * a section embedded the core file (if it's present).
10877c478bd9Sstevel@tonic-gate  *
10887c478bd9Sstevel@tonic-gate  * file_differs() conservatively looks for mismatched files, identifying
10897c478bd9Sstevel@tonic-gate  * a match when there is any ambiguity (since that's the legacy behavior).
10907c478bd9Sstevel@tonic-gate  */
10917c478bd9Sstevel@tonic-gate static int
10927c478bd9Sstevel@tonic-gate file_differs(struct ps_prochandle *P, Elf *elf, file_info_t *fptr)
10937c478bd9Sstevel@tonic-gate {
10947c478bd9Sstevel@tonic-gate 	Elf_Scn *scn;
10957c478bd9Sstevel@tonic-gate 	GElf_Shdr shdr;
10967c478bd9Sstevel@tonic-gate 	GElf_Dyn dyn;
10977c478bd9Sstevel@tonic-gate 	Elf_Data *data;
10987c478bd9Sstevel@tonic-gate 	uint_t i, ndyn;
10997c478bd9Sstevel@tonic-gate 	GElf_Xword cksum;
11007c478bd9Sstevel@tonic-gate 	uintptr_t addr;
11017c478bd9Sstevel@tonic-gate 
11027c478bd9Sstevel@tonic-gate 	if (fptr->file_map == NULL)
11037c478bd9Sstevel@tonic-gate 		return (0);
11047c478bd9Sstevel@tonic-gate 
11057c478bd9Sstevel@tonic-gate 	if ((Pcontent(P) & (CC_CONTENT_TEXT | CC_CONTENT_DATA)) !=
11067c478bd9Sstevel@tonic-gate 	    (CC_CONTENT_TEXT | CC_CONTENT_DATA))
11077c478bd9Sstevel@tonic-gate 		return (0);
11087c478bd9Sstevel@tonic-gate 
11097c478bd9Sstevel@tonic-gate 	/*
11107c478bd9Sstevel@tonic-gate 	 * First, we find the checksum value in the elf file.
11117c478bd9Sstevel@tonic-gate 	 */
11127c478bd9Sstevel@tonic-gate 	scn = NULL;
11137c478bd9Sstevel@tonic-gate 	while ((scn = elf_nextscn(elf, scn)) != NULL) {
11147c478bd9Sstevel@tonic-gate 		if (gelf_getshdr(scn, &shdr) != NULL &&
11157c478bd9Sstevel@tonic-gate 		    shdr.sh_type == SHT_DYNAMIC)
11167c478bd9Sstevel@tonic-gate 			goto found_shdr;
11177c478bd9Sstevel@tonic-gate 	}
11187c478bd9Sstevel@tonic-gate 	return (0);
11197c478bd9Sstevel@tonic-gate 
11207c478bd9Sstevel@tonic-gate found_shdr:
11217c478bd9Sstevel@tonic-gate 	if ((data = elf_getdata(scn, NULL)) == NULL)
11227c478bd9Sstevel@tonic-gate 		return (0);
11237c478bd9Sstevel@tonic-gate 
11247c478bd9Sstevel@tonic-gate 	if (P->status.pr_dmodel == PR_MODEL_ILP32)
11257c478bd9Sstevel@tonic-gate 		ndyn = shdr.sh_size / sizeof (Elf32_Dyn);
11267c478bd9Sstevel@tonic-gate #ifdef _LP64
11277c478bd9Sstevel@tonic-gate 	else if (P->status.pr_dmodel == PR_MODEL_LP64)
11287c478bd9Sstevel@tonic-gate 		ndyn = shdr.sh_size / sizeof (Elf64_Dyn);
11297c478bd9Sstevel@tonic-gate #endif
11307c478bd9Sstevel@tonic-gate 	else
11317c478bd9Sstevel@tonic-gate 		return (0);
11327c478bd9Sstevel@tonic-gate 
11337c478bd9Sstevel@tonic-gate 	for (i = 0; i < ndyn; i++) {
11347c478bd9Sstevel@tonic-gate 		if (gelf_getdyn(data, i, &dyn) != NULL &&
11357c478bd9Sstevel@tonic-gate 		    dyn.d_tag == DT_CHECKSUM)
11367c478bd9Sstevel@tonic-gate 			goto found_cksum;
11377c478bd9Sstevel@tonic-gate 	}
1138*9acbbeafSnn35248 
1139*9acbbeafSnn35248 	/*
1140*9acbbeafSnn35248 	 * The in-memory ELF has no DT_CHECKSUM section, but we will report it
1141*9acbbeafSnn35248 	 * as matching the file anyhow.
1142*9acbbeafSnn35248 	 */
11437c478bd9Sstevel@tonic-gate 	return (0);
11447c478bd9Sstevel@tonic-gate 
11457c478bd9Sstevel@tonic-gate found_cksum:
11467c478bd9Sstevel@tonic-gate 	cksum = dyn.d_un.d_val;
11477c478bd9Sstevel@tonic-gate 	dprintf("elf cksum value is %llx\n", (u_longlong_t)cksum);
11487c478bd9Sstevel@tonic-gate 
11497c478bd9Sstevel@tonic-gate 	/*
11507c478bd9Sstevel@tonic-gate 	 * Get the base of the text mapping that corresponds to this file.
11517c478bd9Sstevel@tonic-gate 	 */
11527c478bd9Sstevel@tonic-gate 	addr = fptr->file_map->map_pmap.pr_vaddr;
11537c478bd9Sstevel@tonic-gate 
11547c478bd9Sstevel@tonic-gate 	if (P->status.pr_dmodel == PR_MODEL_ILP32) {
11557c478bd9Sstevel@tonic-gate 		Elf32_Ehdr ehdr;
11567c478bd9Sstevel@tonic-gate 		Elf32_Phdr phdr;
11577c478bd9Sstevel@tonic-gate 		Elf32_Dyn dync, *dynp;
115830da1432Sahl 		uint_t phnum, i;
11597c478bd9Sstevel@tonic-gate 
116030da1432Sahl 		if (read_ehdr32(P, &ehdr, &phnum, addr) != 0 ||
116130da1432Sahl 		    read_dynamic_phdr32(P, &ehdr, phnum, &phdr, addr) != 0)
11627c478bd9Sstevel@tonic-gate 			return (0);
11637c478bd9Sstevel@tonic-gate 
11647c478bd9Sstevel@tonic-gate 		if (ehdr.e_type == ET_DYN)
11657c478bd9Sstevel@tonic-gate 			phdr.p_vaddr += addr;
11667c478bd9Sstevel@tonic-gate 		if ((dynp = malloc(phdr.p_filesz)) == NULL)
11677c478bd9Sstevel@tonic-gate 			return (0);
11687c478bd9Sstevel@tonic-gate 		dync.d_tag = DT_NULL;
11697c478bd9Sstevel@tonic-gate 		if (Pread(P, dynp, phdr.p_filesz, phdr.p_vaddr) !=
11707c478bd9Sstevel@tonic-gate 		    phdr.p_filesz) {
11717c478bd9Sstevel@tonic-gate 			free(dynp);
11727c478bd9Sstevel@tonic-gate 			return (0);
11737c478bd9Sstevel@tonic-gate 		}
11747c478bd9Sstevel@tonic-gate 
11757c478bd9Sstevel@tonic-gate 		for (i = 0; i < phdr.p_filesz / sizeof (Elf32_Dyn); i++) {
11767c478bd9Sstevel@tonic-gate 			if (dynp[i].d_tag == DT_CHECKSUM)
11777c478bd9Sstevel@tonic-gate 				dync = dynp[i];
11787c478bd9Sstevel@tonic-gate 		}
11797c478bd9Sstevel@tonic-gate 
11807c478bd9Sstevel@tonic-gate 		free(dynp);
11817c478bd9Sstevel@tonic-gate 
11827c478bd9Sstevel@tonic-gate 		if (dync.d_tag != DT_CHECKSUM)
11837c478bd9Sstevel@tonic-gate 			return (0);
11847c478bd9Sstevel@tonic-gate 
11857c478bd9Sstevel@tonic-gate 		dprintf("image cksum value is %llx\n",
11867c478bd9Sstevel@tonic-gate 		    (u_longlong_t)dync.d_un.d_val);
11877c478bd9Sstevel@tonic-gate 		return (dync.d_un.d_val != cksum);
11887c478bd9Sstevel@tonic-gate #ifdef _LP64
11897c478bd9Sstevel@tonic-gate 	} else if (P->status.pr_dmodel == PR_MODEL_LP64) {
11907c478bd9Sstevel@tonic-gate 		Elf64_Ehdr ehdr;
11917c478bd9Sstevel@tonic-gate 		Elf64_Phdr phdr;
11927c478bd9Sstevel@tonic-gate 		Elf64_Dyn dync, *dynp;
119330da1432Sahl 		uint_t phnum, i;
11947c478bd9Sstevel@tonic-gate 
119530da1432Sahl 		if (read_ehdr64(P, &ehdr, &phnum, addr) != 0 ||
119630da1432Sahl 		    read_dynamic_phdr64(P, &ehdr, phnum, &phdr, addr) != 0)
11977c478bd9Sstevel@tonic-gate 			return (0);
11987c478bd9Sstevel@tonic-gate 
11997c478bd9Sstevel@tonic-gate 		if (ehdr.e_type == ET_DYN)
12007c478bd9Sstevel@tonic-gate 			phdr.p_vaddr += addr;
12017c478bd9Sstevel@tonic-gate 		if ((dynp = malloc(phdr.p_filesz)) == NULL)
12027c478bd9Sstevel@tonic-gate 			return (0);
12037c478bd9Sstevel@tonic-gate 		dync.d_tag = DT_NULL;
12047c478bd9Sstevel@tonic-gate 		if (Pread(P, dynp, phdr.p_filesz, phdr.p_vaddr) !=
12057c478bd9Sstevel@tonic-gate 		    phdr.p_filesz) {
12067c478bd9Sstevel@tonic-gate 			free(dynp);
12077c478bd9Sstevel@tonic-gate 			return (0);
12087c478bd9Sstevel@tonic-gate 		}
12097c478bd9Sstevel@tonic-gate 
12107c478bd9Sstevel@tonic-gate 		for (i = 0; i < phdr.p_filesz / sizeof (Elf64_Dyn); i++) {
12117c478bd9Sstevel@tonic-gate 			if (dynp[i].d_tag == DT_CHECKSUM)
12127c478bd9Sstevel@tonic-gate 				dync = dynp[i];
12137c478bd9Sstevel@tonic-gate 		}
12147c478bd9Sstevel@tonic-gate 
12157c478bd9Sstevel@tonic-gate 		free(dynp);
12167c478bd9Sstevel@tonic-gate 
12177c478bd9Sstevel@tonic-gate 		if (dync.d_tag != DT_CHECKSUM)
12187c478bd9Sstevel@tonic-gate 			return (0);
12197c478bd9Sstevel@tonic-gate 
12207c478bd9Sstevel@tonic-gate 		dprintf("image cksum value is %llx\n",
12217c478bd9Sstevel@tonic-gate 		    (u_longlong_t)dync.d_un.d_val);
12227c478bd9Sstevel@tonic-gate 		return (dync.d_un.d_val != cksum);
12237c478bd9Sstevel@tonic-gate #endif	/* _LP64 */
12247c478bd9Sstevel@tonic-gate 	}
12257c478bd9Sstevel@tonic-gate 
12267c478bd9Sstevel@tonic-gate 	return (0);
12277c478bd9Sstevel@tonic-gate }
12287c478bd9Sstevel@tonic-gate 
12297c478bd9Sstevel@tonic-gate static Elf *
12307c478bd9Sstevel@tonic-gate fake_elf(struct ps_prochandle *P, file_info_t *fptr)
12317c478bd9Sstevel@tonic-gate {
12327c478bd9Sstevel@tonic-gate 	enum {
12337c478bd9Sstevel@tonic-gate 		DI_PLTGOT = 0,
12347c478bd9Sstevel@tonic-gate 		DI_JMPREL,
12357c478bd9Sstevel@tonic-gate 		DI_PLTRELSZ,
12367c478bd9Sstevel@tonic-gate 		DI_PLTREL,
12377c478bd9Sstevel@tonic-gate 		DI_SYMTAB,
12387c478bd9Sstevel@tonic-gate 		DI_HASH,
12397c478bd9Sstevel@tonic-gate 		DI_SYMENT,
12407c478bd9Sstevel@tonic-gate 		DI_STRTAB,
12417c478bd9Sstevel@tonic-gate 		DI_STRSZ,
12427c478bd9Sstevel@tonic-gate 		DI_NENT
12437c478bd9Sstevel@tonic-gate 	};
12447c478bd9Sstevel@tonic-gate 	uintptr_t addr;
12457c478bd9Sstevel@tonic-gate 	size_t size = 0;
12467c478bd9Sstevel@tonic-gate 	caddr_t elfdata = NULL;
12477c478bd9Sstevel@tonic-gate 	Elf *elf;
12487c478bd9Sstevel@tonic-gate 	Elf32_Word nchain;
12497c478bd9Sstevel@tonic-gate 	static char shstr[] = ".shstrtab\0.dynsym\0.dynstr\0.dynamic\0.plt";
12507c478bd9Sstevel@tonic-gate 
12517c478bd9Sstevel@tonic-gate 	if (fptr->file_map == NULL)
12527c478bd9Sstevel@tonic-gate 		return (NULL);
12537c478bd9Sstevel@tonic-gate 
12547c478bd9Sstevel@tonic-gate 	if ((Pcontent(P) & (CC_CONTENT_TEXT | CC_CONTENT_DATA)) !=
12557c478bd9Sstevel@tonic-gate 	    (CC_CONTENT_TEXT | CC_CONTENT_DATA))
12567c478bd9Sstevel@tonic-gate 		return (NULL);
12577c478bd9Sstevel@tonic-gate 
12587c478bd9Sstevel@tonic-gate 	addr = fptr->file_map->map_pmap.pr_vaddr;
12597c478bd9Sstevel@tonic-gate 
12607c478bd9Sstevel@tonic-gate 	/*
12617c478bd9Sstevel@tonic-gate 	 * We're building a in memory elf file that will let us use libelf
12627c478bd9Sstevel@tonic-gate 	 * for most of the work we need to later (e.g. symbol table lookups).
12637c478bd9Sstevel@tonic-gate 	 * We need sections for the dynsym, dynstr, and plt, and we need
12647c478bd9Sstevel@tonic-gate 	 * the program headers from the text section. The former is used in
12657c478bd9Sstevel@tonic-gate 	 * Pbuild_file_symtab(); the latter is used in several functions in
12667c478bd9Sstevel@tonic-gate 	 * Pcore.c to reconstruct the origin of each mapping from the load
12677c478bd9Sstevel@tonic-gate 	 * object that spawned it.
12687c478bd9Sstevel@tonic-gate 	 *
12697c478bd9Sstevel@tonic-gate 	 * Here are some useful pieces of elf trivia that will help
12707c478bd9Sstevel@tonic-gate 	 * to elucidate this code.
12717c478bd9Sstevel@tonic-gate 	 *
12727c478bd9Sstevel@tonic-gate 	 * All the information we need about the dynstr can be found in these
12737c478bd9Sstevel@tonic-gate 	 * two entries in the dynamic section:
12747c478bd9Sstevel@tonic-gate 	 *
12757c478bd9Sstevel@tonic-gate 	 *	DT_STRTAB	base of dynstr
12767c478bd9Sstevel@tonic-gate 	 *	DT_STRSZ	size of dynstr
12777c478bd9Sstevel@tonic-gate 	 *
12787c478bd9Sstevel@tonic-gate 	 * So deciphering the dynstr is pretty straightforward.
12797c478bd9Sstevel@tonic-gate 	 *
12807c478bd9Sstevel@tonic-gate 	 * The dynsym is a little trickier.
12817c478bd9Sstevel@tonic-gate 	 *
12827c478bd9Sstevel@tonic-gate 	 *	DT_SYMTAB	base of dynsym
12837c478bd9Sstevel@tonic-gate 	 *	DT_SYMENT	size of a dynstr entry (Elf{32,64}_Sym)
12847c478bd9Sstevel@tonic-gate 	 *	DT_HASH		base of hash table for dynamic lookups
12857c478bd9Sstevel@tonic-gate 	 *
12867c478bd9Sstevel@tonic-gate 	 * The DT_SYMTAB entry gives us any easy way of getting to the base
12877c478bd9Sstevel@tonic-gate 	 * of the dynsym, but getting the size involves rooting around in the
12887c478bd9Sstevel@tonic-gate 	 * dynamic lookup hash table. Here's the layout of the hash table:
12897c478bd9Sstevel@tonic-gate 	 *
12907c478bd9Sstevel@tonic-gate 	 *		+-------------------+
12917c478bd9Sstevel@tonic-gate 	 *		|	nbucket	    |	All values are of type
12927c478bd9Sstevel@tonic-gate 	 *		+-------------------+	Elf32_Word
12937c478bd9Sstevel@tonic-gate 	 *		|	nchain	    |
12947c478bd9Sstevel@tonic-gate 	 *		+-------------------+
12957c478bd9Sstevel@tonic-gate 	 *		|	bucket[0]   |
12967c478bd9Sstevel@tonic-gate 	 *		|	. . .	    |
12977c478bd9Sstevel@tonic-gate 	 *		| bucket[nbucket-1] |
12987c478bd9Sstevel@tonic-gate 	 *		+-------------------+
12997c478bd9Sstevel@tonic-gate 	 *		|	chain[0]    |
13007c478bd9Sstevel@tonic-gate 	 *		|	. . .	    |
13017c478bd9Sstevel@tonic-gate 	 *		|  chain[nchain-1]  |
13027c478bd9Sstevel@tonic-gate 	 *		+-------------------+
13037c478bd9Sstevel@tonic-gate 	 *	(figure 5-12 from the SYS V Generic ABI)
13047c478bd9Sstevel@tonic-gate 	 *
13057c478bd9Sstevel@tonic-gate 	 * Symbols names are hashed into a particular bucket which contains
13067c478bd9Sstevel@tonic-gate 	 * an index into the symbol table. Each entry in the symbol table
13077c478bd9Sstevel@tonic-gate 	 * has a corresponding entry in the chain table which tells the
13087c478bd9Sstevel@tonic-gate 	 * consumer where the next entry in the hash chain is. We can use
13097c478bd9Sstevel@tonic-gate 	 * the nchain field to find out the size of the dynsym.
13107c478bd9Sstevel@tonic-gate 	 *
13117c478bd9Sstevel@tonic-gate 	 * We can figure out the size of the .plt section, but it takes some
13127c478bd9Sstevel@tonic-gate 	 * doing. We need to use the following information:
13137c478bd9Sstevel@tonic-gate 	 *
13147c478bd9Sstevel@tonic-gate 	 *	DT_PLTGOT	base of the PLT
13157c478bd9Sstevel@tonic-gate 	 *	DT_JMPREL	base of the PLT's relocation section
13167c478bd9Sstevel@tonic-gate 	 *	DT_PLTRELSZ	size of the PLT's relocation section
13177c478bd9Sstevel@tonic-gate 	 *	DT_PLTREL	type of the PLT's relocation section
13187c478bd9Sstevel@tonic-gate 	 *
13197c478bd9Sstevel@tonic-gate 	 * We can use the relocation section to figure out the address of the
13207c478bd9Sstevel@tonic-gate 	 * last entry and subtract off the value of DT_PLTGOT to calculate
13217c478bd9Sstevel@tonic-gate 	 * the size of the PLT.
13227c478bd9Sstevel@tonic-gate 	 *
13237c478bd9Sstevel@tonic-gate 	 * For more information, check out the System V Generic ABI.
13247c478bd9Sstevel@tonic-gate 	 */
13257c478bd9Sstevel@tonic-gate 
13267c478bd9Sstevel@tonic-gate 	if (P->status.pr_dmodel == PR_MODEL_ILP32) {
13277c478bd9Sstevel@tonic-gate 		Elf32_Ehdr ehdr, *ep;
13287c478bd9Sstevel@tonic-gate 		Elf32_Phdr phdr;
13297c478bd9Sstevel@tonic-gate 		Elf32_Shdr *sp;
13307c478bd9Sstevel@tonic-gate 		Elf32_Dyn *dp;
13317c478bd9Sstevel@tonic-gate 		Elf32_Dyn *d[DI_NENT] = { 0 };
133230da1432Sahl 		uint_t phnum, i, dcount = 0;
13337c478bd9Sstevel@tonic-gate 		uint32_t off;
13347c478bd9Sstevel@tonic-gate 		size_t pltsz = 0, pltentsz;
13357c478bd9Sstevel@tonic-gate 
1336*9acbbeafSnn35248 		if ((read_ehdr32(P, &ehdr, &phnum, addr) != 0) ||
133730da1432Sahl 		    read_dynamic_phdr32(P, &ehdr, phnum, &phdr, addr) != 0)
13387c478bd9Sstevel@tonic-gate 			return (NULL);
13397c478bd9Sstevel@tonic-gate 
13407c478bd9Sstevel@tonic-gate 		if (ehdr.e_type == ET_DYN)
13417c478bd9Sstevel@tonic-gate 			phdr.p_vaddr += addr;
13427c478bd9Sstevel@tonic-gate 
13437c478bd9Sstevel@tonic-gate 		if ((dp = malloc(phdr.p_filesz)) == NULL)
13447c478bd9Sstevel@tonic-gate 			return (NULL);
13457c478bd9Sstevel@tonic-gate 
13467c478bd9Sstevel@tonic-gate 		if (Pread(P, dp, phdr.p_filesz, phdr.p_vaddr) !=
13477c478bd9Sstevel@tonic-gate 		    phdr.p_filesz) {
13487c478bd9Sstevel@tonic-gate 			free(dp);
13497c478bd9Sstevel@tonic-gate 			return (NULL);
13507c478bd9Sstevel@tonic-gate 		}
13517c478bd9Sstevel@tonic-gate 
1352*9acbbeafSnn35248 		/*
1353*9acbbeafSnn35248 		 * Allow librtld_db the opportunity to "fix" the program
1354*9acbbeafSnn35248 		 * headers, if it needs to, before we process them.
1355*9acbbeafSnn35248 		 */
1356*9acbbeafSnn35248 		if (P->rap != NULL && ehdr.e_type == ET_DYN) {
1357*9acbbeafSnn35248 			rd_fix_phdrs(P->rap, dp, phdr.p_filesz, addr);
1358*9acbbeafSnn35248 		}
1359*9acbbeafSnn35248 
13607c478bd9Sstevel@tonic-gate 		for (i = 0; i < phdr.p_filesz / sizeof (Elf32_Dyn); i++) {
13617c478bd9Sstevel@tonic-gate 			switch (dp[i].d_tag) {
13627c478bd9Sstevel@tonic-gate 			/*
13637c478bd9Sstevel@tonic-gate 			 * For the .plt section.
13647c478bd9Sstevel@tonic-gate 			 */
13657c478bd9Sstevel@tonic-gate 			case DT_PLTGOT:
13667c478bd9Sstevel@tonic-gate 				d[DI_PLTGOT] = &dp[i];
13677c478bd9Sstevel@tonic-gate 				continue;
13687c478bd9Sstevel@tonic-gate 			case DT_JMPREL:
13697c478bd9Sstevel@tonic-gate 				d[DI_JMPREL] = &dp[i];
13707c478bd9Sstevel@tonic-gate 				continue;
13717c478bd9Sstevel@tonic-gate 			case DT_PLTRELSZ:
13727c478bd9Sstevel@tonic-gate 				d[DI_PLTRELSZ] = &dp[i];
13737c478bd9Sstevel@tonic-gate 				continue;
13747c478bd9Sstevel@tonic-gate 			case DT_PLTREL:
13757c478bd9Sstevel@tonic-gate 				d[DI_PLTREL] = &dp[i];
13767c478bd9Sstevel@tonic-gate 				continue;
13777c478bd9Sstevel@tonic-gate 			default:
13787c478bd9Sstevel@tonic-gate 				continue;
13797c478bd9Sstevel@tonic-gate 
13807c478bd9Sstevel@tonic-gate 			/*
13817c478bd9Sstevel@tonic-gate 			 * For the .dynsym section.
13827c478bd9Sstevel@tonic-gate 			 */
13837c478bd9Sstevel@tonic-gate 			case DT_SYMTAB:
13847c478bd9Sstevel@tonic-gate 				d[DI_SYMTAB] = &dp[i];
13857c478bd9Sstevel@tonic-gate 				break;
13867c478bd9Sstevel@tonic-gate 			case DT_HASH:
13877c478bd9Sstevel@tonic-gate 				d[DI_HASH] = &dp[i];
13887c478bd9Sstevel@tonic-gate 				break;
13897c478bd9Sstevel@tonic-gate 			case DT_SYMENT:
13907c478bd9Sstevel@tonic-gate 				d[DI_SYMENT] = &dp[i];
13917c478bd9Sstevel@tonic-gate 				break;
13927c478bd9Sstevel@tonic-gate 
13937c478bd9Sstevel@tonic-gate 			/*
13947c478bd9Sstevel@tonic-gate 			 * For the .dynstr section.
13957c478bd9Sstevel@tonic-gate 			 */
13967c478bd9Sstevel@tonic-gate 			case DT_STRTAB:
13977c478bd9Sstevel@tonic-gate 				d[DI_STRTAB] = &dp[i];
13987c478bd9Sstevel@tonic-gate 				break;
13997c478bd9Sstevel@tonic-gate 			case DT_STRSZ:
14007c478bd9Sstevel@tonic-gate 				d[DI_STRSZ] = &dp[i];
14017c478bd9Sstevel@tonic-gate 				break;
14027c478bd9Sstevel@tonic-gate 			}
14037c478bd9Sstevel@tonic-gate 
14047c478bd9Sstevel@tonic-gate 			dcount++;
14057c478bd9Sstevel@tonic-gate 		}
14067c478bd9Sstevel@tonic-gate 
14077c478bd9Sstevel@tonic-gate 		/*
14087c478bd9Sstevel@tonic-gate 		 * We need all of those dynamic entries in order to put
14097c478bd9Sstevel@tonic-gate 		 * together a complete set of elf sections, but we'll
14107c478bd9Sstevel@tonic-gate 		 * let the PLT section slide if need be. The dynsym- and
14117c478bd9Sstevel@tonic-gate 		 * dynstr-related dynamic entries are mandatory in both
14127c478bd9Sstevel@tonic-gate 		 * executables and shared objects so if one of those is
14137c478bd9Sstevel@tonic-gate 		 * missing, we're in some trouble and should abort.
14147c478bd9Sstevel@tonic-gate 		 */
14157c478bd9Sstevel@tonic-gate 		if (dcount + 4 != DI_NENT) {
14167c478bd9Sstevel@tonic-gate 			dprintf("text section missing required dynamic "
14177c478bd9Sstevel@tonic-gate 			    "entries\n");
14187c478bd9Sstevel@tonic-gate 			return (NULL);
14197c478bd9Sstevel@tonic-gate 		}
14207c478bd9Sstevel@tonic-gate 
14217c478bd9Sstevel@tonic-gate 		if (ehdr.e_type == ET_DYN) {
14227c478bd9Sstevel@tonic-gate 			if (d[DI_PLTGOT] != NULL)
14237c478bd9Sstevel@tonic-gate 				d[DI_PLTGOT]->d_un.d_ptr += addr;
14247c478bd9Sstevel@tonic-gate 			if (d[DI_JMPREL] != NULL)
14257c478bd9Sstevel@tonic-gate 				d[DI_JMPREL]->d_un.d_ptr += addr;
14267c478bd9Sstevel@tonic-gate 			d[DI_SYMTAB]->d_un.d_ptr += addr;
14277c478bd9Sstevel@tonic-gate 			d[DI_HASH]->d_un.d_ptr += addr;
14287c478bd9Sstevel@tonic-gate 			d[DI_STRTAB]->d_un.d_ptr += addr;
14297c478bd9Sstevel@tonic-gate 		}
14307c478bd9Sstevel@tonic-gate 
14317c478bd9Sstevel@tonic-gate 		/* elf header */
14327c478bd9Sstevel@tonic-gate 		size = sizeof (Elf32_Ehdr);
14337c478bd9Sstevel@tonic-gate 
14347c478bd9Sstevel@tonic-gate 		/* program headers from in-core elf fragment */
143530da1432Sahl 		size += phnum * ehdr.e_phentsize;
14367c478bd9Sstevel@tonic-gate 
14377c478bd9Sstevel@tonic-gate 		/* unused shdr, and .shstrtab section */
14387c478bd9Sstevel@tonic-gate 		size += sizeof (Elf32_Shdr);
14397c478bd9Sstevel@tonic-gate 		size += sizeof (Elf32_Shdr);
14407c478bd9Sstevel@tonic-gate 		size += roundup(sizeof (shstr), 4);
14417c478bd9Sstevel@tonic-gate 
14427c478bd9Sstevel@tonic-gate 		/* .dynsym section */
14437c478bd9Sstevel@tonic-gate 		size += sizeof (Elf32_Shdr);
14447c478bd9Sstevel@tonic-gate 		if (Pread(P, &nchain, sizeof (nchain),
1445*9acbbeafSnn35248 		    d[DI_HASH]->d_un.d_ptr + 4) != sizeof (nchain)) {
1446*9acbbeafSnn35248 			dprintf("Pread of .dynsym at %lx failed\n",
1447*9acbbeafSnn35248 			    (long)(d[DI_HASH]->d_un.d_val + 4));
14487c478bd9Sstevel@tonic-gate 			goto bad32;
1449*9acbbeafSnn35248 		}
14507c478bd9Sstevel@tonic-gate 		size += sizeof (Elf32_Sym) * nchain;
14517c478bd9Sstevel@tonic-gate 
14527c478bd9Sstevel@tonic-gate 		/* .dynstr section */
14537c478bd9Sstevel@tonic-gate 		size += sizeof (Elf32_Shdr);
14547c478bd9Sstevel@tonic-gate 		size += roundup(d[DI_STRSZ]->d_un.d_val, 4);
14557c478bd9Sstevel@tonic-gate 
14567c478bd9Sstevel@tonic-gate 		/* .dynamic section */
14577c478bd9Sstevel@tonic-gate 		size += sizeof (Elf32_Shdr);
14587c478bd9Sstevel@tonic-gate 		size += roundup(phdr.p_filesz, 4);
14597c478bd9Sstevel@tonic-gate 
14607c478bd9Sstevel@tonic-gate 		/* .plt section */
14617c478bd9Sstevel@tonic-gate 		if (d[DI_PLTGOT] != NULL && d[DI_JMPREL] != NULL &&
14627c478bd9Sstevel@tonic-gate 		    d[DI_PLTRELSZ] != NULL && d[DI_PLTREL] != NULL) {
14637c478bd9Sstevel@tonic-gate 			uintptr_t penult, ult;
14647c478bd9Sstevel@tonic-gate 			uintptr_t jmprel = d[DI_JMPREL]->d_un.d_ptr;
14657c478bd9Sstevel@tonic-gate 			size_t pltrelsz = d[DI_PLTRELSZ]->d_un.d_val;
14667c478bd9Sstevel@tonic-gate 
14677c478bd9Sstevel@tonic-gate 			if (d[DI_PLTREL]->d_un.d_val == DT_RELA) {
14687c478bd9Sstevel@tonic-gate 				uint_t ndx = pltrelsz / sizeof (Elf32_Rela) - 2;
14697c478bd9Sstevel@tonic-gate 				Elf32_Rela r[2];
14707c478bd9Sstevel@tonic-gate 
14717c478bd9Sstevel@tonic-gate 				if (Pread(P, r, sizeof (r), jmprel +
1472*9acbbeafSnn35248 				    sizeof (r[0]) * ndx) != sizeof (r)) {
1473*9acbbeafSnn35248 					dprintf("Pread of DT_RELA failed\n");
14747c478bd9Sstevel@tonic-gate 					goto bad32;
1475*9acbbeafSnn35248 				}
14767c478bd9Sstevel@tonic-gate 
14777c478bd9Sstevel@tonic-gate 				penult = r[0].r_offset;
14787c478bd9Sstevel@tonic-gate 				ult = r[1].r_offset;
14797c478bd9Sstevel@tonic-gate 
14807c478bd9Sstevel@tonic-gate 			} else if (d[DI_PLTREL]->d_un.d_val == DT_REL) {
14817c478bd9Sstevel@tonic-gate 				uint_t ndx = pltrelsz / sizeof (Elf32_Rel) - 2;
14827c478bd9Sstevel@tonic-gate 				Elf32_Rel r[2];
14837c478bd9Sstevel@tonic-gate 
14847c478bd9Sstevel@tonic-gate 				if (Pread(P, r, sizeof (r), jmprel +
1485*9acbbeafSnn35248 				    sizeof (r[0]) * ndx) != sizeof (r)) {
1486*9acbbeafSnn35248 					dprintf("Pread of DT_REL failed\n");
14877c478bd9Sstevel@tonic-gate 					goto bad32;
1488*9acbbeafSnn35248 				}
14897c478bd9Sstevel@tonic-gate 
14907c478bd9Sstevel@tonic-gate 				penult = r[0].r_offset;
14917c478bd9Sstevel@tonic-gate 				ult = r[1].r_offset;
14927c478bd9Sstevel@tonic-gate 			} else {
1493*9acbbeafSnn35248 				dprintf(".plt: unknown jmprel value\n");
14947c478bd9Sstevel@tonic-gate 				goto bad32;
14957c478bd9Sstevel@tonic-gate 			}
14967c478bd9Sstevel@tonic-gate 
14977c478bd9Sstevel@tonic-gate 			pltentsz = ult - penult;
14987c478bd9Sstevel@tonic-gate 
14997c478bd9Sstevel@tonic-gate 			if (ehdr.e_type == ET_DYN)
15007c478bd9Sstevel@tonic-gate 				ult += addr;
15017c478bd9Sstevel@tonic-gate 
15027c478bd9Sstevel@tonic-gate 			pltsz = ult - d[DI_PLTGOT]->d_un.d_ptr + pltentsz;
15037c478bd9Sstevel@tonic-gate 
15047c478bd9Sstevel@tonic-gate 			size += sizeof (Elf32_Shdr);
15057c478bd9Sstevel@tonic-gate 			size += roundup(pltsz, 4);
15067c478bd9Sstevel@tonic-gate 		}
15077c478bd9Sstevel@tonic-gate 
15087c478bd9Sstevel@tonic-gate 		if ((elfdata = calloc(1, size)) == NULL)
15097c478bd9Sstevel@tonic-gate 			goto bad32;
15107c478bd9Sstevel@tonic-gate 
15117c478bd9Sstevel@tonic-gate 		/* LINTED - alignment */
15127c478bd9Sstevel@tonic-gate 		ep = (Elf32_Ehdr *)elfdata;
15137c478bd9Sstevel@tonic-gate 		(void) memcpy(ep, &ehdr, offsetof(Elf32_Ehdr, e_phoff));
15147c478bd9Sstevel@tonic-gate 
15157c478bd9Sstevel@tonic-gate 		ep->e_ehsize = sizeof (Elf32_Ehdr);
15167c478bd9Sstevel@tonic-gate 		ep->e_phoff = sizeof (Elf32_Ehdr);
15177c478bd9Sstevel@tonic-gate 		ep->e_phentsize = ehdr.e_phentsize;
151830da1432Sahl 		ep->e_phnum = phnum;
151930da1432Sahl 		ep->e_shoff = ep->e_phoff + phnum * ep->e_phentsize;
15207c478bd9Sstevel@tonic-gate 		ep->e_shentsize = sizeof (Elf32_Shdr);
15217c478bd9Sstevel@tonic-gate 		ep->e_shnum = (pltsz == 0) ? 5 : 6;
15227c478bd9Sstevel@tonic-gate 		ep->e_shstrndx = 1;
15237c478bd9Sstevel@tonic-gate 
15247c478bd9Sstevel@tonic-gate 		/* LINTED - alignment */
15257c478bd9Sstevel@tonic-gate 		sp = (Elf32_Shdr *)(elfdata + ep->e_shoff);
15267c478bd9Sstevel@tonic-gate 		off = ep->e_shoff + ep->e_shentsize * ep->e_shnum;
15277c478bd9Sstevel@tonic-gate 
15287c478bd9Sstevel@tonic-gate 		/*
15297c478bd9Sstevel@tonic-gate 		 * Copying the program headers directly from the process's
15307c478bd9Sstevel@tonic-gate 		 * address space is a little suspect, but since we only
15317c478bd9Sstevel@tonic-gate 		 * use them for their address and size values, this is fine.
15327c478bd9Sstevel@tonic-gate 		 */
153330da1432Sahl 		if (Pread(P, &elfdata[ep->e_phoff], phnum * ep->e_phentsize,
153430da1432Sahl 		    addr + ehdr.e_phoff) != phnum * ep->e_phentsize) {
15357c478bd9Sstevel@tonic-gate 			free(elfdata);
1536*9acbbeafSnn35248 			dprintf("failed to read program headers\n");
15377c478bd9Sstevel@tonic-gate 			goto bad32;
15387c478bd9Sstevel@tonic-gate 		}
15397c478bd9Sstevel@tonic-gate 
15407c478bd9Sstevel@tonic-gate 		/*
15417c478bd9Sstevel@tonic-gate 		 * The first elf section is always skipped.
15427c478bd9Sstevel@tonic-gate 		 */
15437c478bd9Sstevel@tonic-gate 		sp++;
15447c478bd9Sstevel@tonic-gate 
15457c478bd9Sstevel@tonic-gate 		/*
15467c478bd9Sstevel@tonic-gate 		 * Section Header[1]  sh_name: .shstrtab
15477c478bd9Sstevel@tonic-gate 		 */
15487c478bd9Sstevel@tonic-gate 		sp->sh_name = 0;
15497c478bd9Sstevel@tonic-gate 		sp->sh_type = SHT_STRTAB;
15507c478bd9Sstevel@tonic-gate 		sp->sh_flags = SHF_STRINGS;
15517c478bd9Sstevel@tonic-gate 		sp->sh_addr = 0;
15527c478bd9Sstevel@tonic-gate 		sp->sh_offset = off;
15537c478bd9Sstevel@tonic-gate 		sp->sh_size = sizeof (shstr);
15547c478bd9Sstevel@tonic-gate 		sp->sh_link = 0;
15557c478bd9Sstevel@tonic-gate 		sp->sh_info = 0;
15567c478bd9Sstevel@tonic-gate 		sp->sh_addralign = 1;
15577c478bd9Sstevel@tonic-gate 		sp->sh_entsize = 0;
15587c478bd9Sstevel@tonic-gate 
15597c478bd9Sstevel@tonic-gate 		(void) memcpy(&elfdata[off], shstr, sizeof (shstr));
15607c478bd9Sstevel@tonic-gate 		off += roundup(sp->sh_size, 4);
15617c478bd9Sstevel@tonic-gate 		sp++;
15627c478bd9Sstevel@tonic-gate 
15637c478bd9Sstevel@tonic-gate 		/*
15647c478bd9Sstevel@tonic-gate 		 * Section Header[2]  sh_name: .dynsym
15657c478bd9Sstevel@tonic-gate 		 */
15667c478bd9Sstevel@tonic-gate 		sp->sh_name = 10;
15677c478bd9Sstevel@tonic-gate 		sp->sh_type = SHT_DYNSYM;
15687c478bd9Sstevel@tonic-gate 		sp->sh_flags = SHF_ALLOC;
15697c478bd9Sstevel@tonic-gate 		sp->sh_addr = d[DI_SYMTAB]->d_un.d_ptr;
15707c478bd9Sstevel@tonic-gate 		if (ehdr.e_type == ET_DYN)
15717c478bd9Sstevel@tonic-gate 			sp->sh_addr -= addr;
15727c478bd9Sstevel@tonic-gate 		sp->sh_offset = off;
15737c478bd9Sstevel@tonic-gate 		sp->sh_size = nchain * sizeof (Elf32_Sym);
15747c478bd9Sstevel@tonic-gate 		sp->sh_link = 3;
15757c478bd9Sstevel@tonic-gate 		sp->sh_info = 1;
15767c478bd9Sstevel@tonic-gate 		sp->sh_addralign = 4;
15777c478bd9Sstevel@tonic-gate 		sp->sh_entsize = sizeof (Elf32_Sym);
15787c478bd9Sstevel@tonic-gate 
15797c478bd9Sstevel@tonic-gate 		if (Pread(P, &elfdata[off], sp->sh_size,
15807c478bd9Sstevel@tonic-gate 		    d[DI_SYMTAB]->d_un.d_ptr) != sp->sh_size) {
15817c478bd9Sstevel@tonic-gate 			free(elfdata);
1582*9acbbeafSnn35248 			dprintf("failed to read .dynsym at %lx\n",
1583*9acbbeafSnn35248 			    (long)d[DI_SYMTAB]->d_un.d_ptr);
15847c478bd9Sstevel@tonic-gate 			goto bad32;
15857c478bd9Sstevel@tonic-gate 		}
15867c478bd9Sstevel@tonic-gate 
15877c478bd9Sstevel@tonic-gate 		off += roundup(sp->sh_size, 4);
15887c478bd9Sstevel@tonic-gate 		sp++;
15897c478bd9Sstevel@tonic-gate 
15907c478bd9Sstevel@tonic-gate 		/*
15917c478bd9Sstevel@tonic-gate 		 * Section Header[3]  sh_name: .dynstr
15927c478bd9Sstevel@tonic-gate 		 */
15937c478bd9Sstevel@tonic-gate 		sp->sh_name = 18;
15947c478bd9Sstevel@tonic-gate 		sp->sh_type = SHT_STRTAB;
15957c478bd9Sstevel@tonic-gate 		sp->sh_flags = SHF_ALLOC | SHF_STRINGS;
15967c478bd9Sstevel@tonic-gate 		sp->sh_addr = d[DI_STRTAB]->d_un.d_ptr;
15977c478bd9Sstevel@tonic-gate 		if (ehdr.e_type == ET_DYN)
15987c478bd9Sstevel@tonic-gate 			sp->sh_addr -= addr;
15997c478bd9Sstevel@tonic-gate 		sp->sh_offset = off;
16007c478bd9Sstevel@tonic-gate 		sp->sh_size = d[DI_STRSZ]->d_un.d_val;
16017c478bd9Sstevel@tonic-gate 		sp->sh_link = 0;
16027c478bd9Sstevel@tonic-gate 		sp->sh_info = 0;
16037c478bd9Sstevel@tonic-gate 		sp->sh_addralign = 1;
16047c478bd9Sstevel@tonic-gate 		sp->sh_entsize = 0;
16057c478bd9Sstevel@tonic-gate 
16067c478bd9Sstevel@tonic-gate 		if (Pread(P, &elfdata[off], sp->sh_size,
16077c478bd9Sstevel@tonic-gate 		    d[DI_STRTAB]->d_un.d_ptr) != sp->sh_size) {
16087c478bd9Sstevel@tonic-gate 			free(elfdata);
1609*9acbbeafSnn35248 			dprintf("failed to read .dynstr\n");
16107c478bd9Sstevel@tonic-gate 			goto bad32;
16117c478bd9Sstevel@tonic-gate 		}
16127c478bd9Sstevel@tonic-gate 		off += roundup(sp->sh_size, 4);
16137c478bd9Sstevel@tonic-gate 		sp++;
16147c478bd9Sstevel@tonic-gate 
16157c478bd9Sstevel@tonic-gate 		/*
16167c478bd9Sstevel@tonic-gate 		 * Section Header[4]  sh_name: .dynamic
16177c478bd9Sstevel@tonic-gate 		 */
16187c478bd9Sstevel@tonic-gate 		sp->sh_name = 26;
16197c478bd9Sstevel@tonic-gate 		sp->sh_type = SHT_DYNAMIC;
16207c478bd9Sstevel@tonic-gate 		sp->sh_flags = SHF_WRITE | SHF_ALLOC;
16217c478bd9Sstevel@tonic-gate 		sp->sh_addr = phdr.p_vaddr;
16227c478bd9Sstevel@tonic-gate 		if (ehdr.e_type == ET_DYN)
16237c478bd9Sstevel@tonic-gate 			sp->sh_addr -= addr;
16247c478bd9Sstevel@tonic-gate 		sp->sh_offset = off;
16257c478bd9Sstevel@tonic-gate 		sp->sh_size = phdr.p_filesz;
16267c478bd9Sstevel@tonic-gate 		sp->sh_link = 3;
16277c478bd9Sstevel@tonic-gate 		sp->sh_info = 0;
16287c478bd9Sstevel@tonic-gate 		sp->sh_addralign = 4;
16297c478bd9Sstevel@tonic-gate 		sp->sh_entsize = sizeof (Elf32_Dyn);
16307c478bd9Sstevel@tonic-gate 
16317c478bd9Sstevel@tonic-gate 		(void) memcpy(&elfdata[off], dp, sp->sh_size);
16327c478bd9Sstevel@tonic-gate 		off += roundup(sp->sh_size, 4);
16337c478bd9Sstevel@tonic-gate 		sp++;
16347c478bd9Sstevel@tonic-gate 
16357c478bd9Sstevel@tonic-gate 		/*
16367c478bd9Sstevel@tonic-gate 		 * Section Header[5]  sh_name: .plt
16377c478bd9Sstevel@tonic-gate 		 */
16387c478bd9Sstevel@tonic-gate 		if (pltsz != 0) {
16397c478bd9Sstevel@tonic-gate 			sp->sh_name = 35;
16407c478bd9Sstevel@tonic-gate 			sp->sh_type = SHT_PROGBITS;
16417c478bd9Sstevel@tonic-gate 			sp->sh_flags = SHF_WRITE | SHF_ALLOC | SHF_EXECINSTR;
16427c478bd9Sstevel@tonic-gate 			sp->sh_addr = d[DI_PLTGOT]->d_un.d_ptr;
16437c478bd9Sstevel@tonic-gate 			if (ehdr.e_type == ET_DYN)
16447c478bd9Sstevel@tonic-gate 				sp->sh_addr -= addr;
16457c478bd9Sstevel@tonic-gate 			sp->sh_offset = off;
16467c478bd9Sstevel@tonic-gate 			sp->sh_size = pltsz;
16477c478bd9Sstevel@tonic-gate 			sp->sh_link = 0;
16487c478bd9Sstevel@tonic-gate 			sp->sh_info = 0;
16497c478bd9Sstevel@tonic-gate 			sp->sh_addralign = 4;
16507c478bd9Sstevel@tonic-gate 			sp->sh_entsize = pltentsz;
16517c478bd9Sstevel@tonic-gate 
16527c478bd9Sstevel@tonic-gate 			if (Pread(P, &elfdata[off], sp->sh_size,
16537c478bd9Sstevel@tonic-gate 			    d[DI_PLTGOT]->d_un.d_ptr) != sp->sh_size) {
16547c478bd9Sstevel@tonic-gate 				free(elfdata);
1655*9acbbeafSnn35248 				dprintf("failed to read .plt\n");
16567c478bd9Sstevel@tonic-gate 				goto bad32;
16577c478bd9Sstevel@tonic-gate 			}
16587c478bd9Sstevel@tonic-gate 			off += roundup(sp->sh_size, 4);
16597c478bd9Sstevel@tonic-gate 			sp++;
16607c478bd9Sstevel@tonic-gate 		}
16617c478bd9Sstevel@tonic-gate 
16627c478bd9Sstevel@tonic-gate 		free(dp);
16637c478bd9Sstevel@tonic-gate 		goto good;
16647c478bd9Sstevel@tonic-gate 
16657c478bd9Sstevel@tonic-gate bad32:
16667c478bd9Sstevel@tonic-gate 		free(dp);
16677c478bd9Sstevel@tonic-gate 		return (NULL);
16687c478bd9Sstevel@tonic-gate #ifdef _LP64
16697c478bd9Sstevel@tonic-gate 	} else if (P->status.pr_dmodel == PR_MODEL_LP64) {
16707c478bd9Sstevel@tonic-gate 		Elf64_Ehdr ehdr, *ep;
16717c478bd9Sstevel@tonic-gate 		Elf64_Phdr phdr;
16727c478bd9Sstevel@tonic-gate 		Elf64_Shdr *sp;
16737c478bd9Sstevel@tonic-gate 		Elf64_Dyn *dp;
16747c478bd9Sstevel@tonic-gate 		Elf64_Dyn *d[DI_NENT] = { 0 };
167530da1432Sahl 		uint_t phnum, i, dcount = 0;
16767c478bd9Sstevel@tonic-gate 		uint64_t off;
16777c478bd9Sstevel@tonic-gate 		size_t pltsz = 0, pltentsz;
16787c478bd9Sstevel@tonic-gate 
167930da1432Sahl 		if (read_ehdr64(P, &ehdr, &phnum, addr) != 0 ||
168030da1432Sahl 		    read_dynamic_phdr64(P, &ehdr, phnum, &phdr, addr) != 0)
16817c478bd9Sstevel@tonic-gate 			return (NULL);
16827c478bd9Sstevel@tonic-gate 
16837c478bd9Sstevel@tonic-gate 		if (ehdr.e_type == ET_DYN)
16847c478bd9Sstevel@tonic-gate 			phdr.p_vaddr += addr;
16857c478bd9Sstevel@tonic-gate 
16867c478bd9Sstevel@tonic-gate 		if ((dp = malloc(phdr.p_filesz)) == NULL)
16877c478bd9Sstevel@tonic-gate 			return (NULL);
16887c478bd9Sstevel@tonic-gate 
16897c478bd9Sstevel@tonic-gate 		if (Pread(P, dp, phdr.p_filesz, phdr.p_vaddr) !=
16907c478bd9Sstevel@tonic-gate 		    phdr.p_filesz) {
16917c478bd9Sstevel@tonic-gate 			free(dp);
16927c478bd9Sstevel@tonic-gate 			return (NULL);
16937c478bd9Sstevel@tonic-gate 		}
16947c478bd9Sstevel@tonic-gate 
16957c478bd9Sstevel@tonic-gate 		for (i = 0; i < phdr.p_filesz / sizeof (Elf64_Dyn); i++) {
16967c478bd9Sstevel@tonic-gate 			switch (dp[i].d_tag) {
16977c478bd9Sstevel@tonic-gate 			/*
16987c478bd9Sstevel@tonic-gate 			 * For the .plt section.
16997c478bd9Sstevel@tonic-gate 			 */
17007c478bd9Sstevel@tonic-gate 			case DT_PLTGOT:
17017c478bd9Sstevel@tonic-gate 				d[DI_PLTGOT] = &dp[i];
17027c478bd9Sstevel@tonic-gate 				continue;
17037c478bd9Sstevel@tonic-gate 			case DT_JMPREL:
17047c478bd9Sstevel@tonic-gate 				d[DI_JMPREL] = &dp[i];
17057c478bd9Sstevel@tonic-gate 				continue;
17067c478bd9Sstevel@tonic-gate 			case DT_PLTRELSZ:
17077c478bd9Sstevel@tonic-gate 				d[DI_PLTRELSZ] = &dp[i];
17087c478bd9Sstevel@tonic-gate 				continue;
17097c478bd9Sstevel@tonic-gate 			case DT_PLTREL:
17107c478bd9Sstevel@tonic-gate 				d[DI_PLTREL] = &dp[i];
17117c478bd9Sstevel@tonic-gate 				continue;
17127c478bd9Sstevel@tonic-gate 			default:
17137c478bd9Sstevel@tonic-gate 				continue;
17147c478bd9Sstevel@tonic-gate 
17157c478bd9Sstevel@tonic-gate 			/*
17167c478bd9Sstevel@tonic-gate 			 * For the .dynsym section.
17177c478bd9Sstevel@tonic-gate 			 */
17187c478bd9Sstevel@tonic-gate 			case DT_SYMTAB:
17197c478bd9Sstevel@tonic-gate 				d[DI_SYMTAB] = &dp[i];
17207c478bd9Sstevel@tonic-gate 				break;
17217c478bd9Sstevel@tonic-gate 			case DT_HASH:
17227c478bd9Sstevel@tonic-gate 				d[DI_HASH] = &dp[i];
17237c478bd9Sstevel@tonic-gate 				break;
17247c478bd9Sstevel@tonic-gate 			case DT_SYMENT:
17257c478bd9Sstevel@tonic-gate 				d[DI_SYMENT] = &dp[i];
17267c478bd9Sstevel@tonic-gate 				break;
17277c478bd9Sstevel@tonic-gate 
17287c478bd9Sstevel@tonic-gate 			/*
17297c478bd9Sstevel@tonic-gate 			 * For the .dynstr section.
17307c478bd9Sstevel@tonic-gate 			 */
17317c478bd9Sstevel@tonic-gate 			case DT_STRTAB:
17327c478bd9Sstevel@tonic-gate 				d[DI_STRTAB] = &dp[i];
17337c478bd9Sstevel@tonic-gate 				break;
17347c478bd9Sstevel@tonic-gate 			case DT_STRSZ:
17357c478bd9Sstevel@tonic-gate 				d[DI_STRSZ] = &dp[i];
17367c478bd9Sstevel@tonic-gate 				break;
17377c478bd9Sstevel@tonic-gate 			}
17387c478bd9Sstevel@tonic-gate 
17397c478bd9Sstevel@tonic-gate 			dcount++;
17407c478bd9Sstevel@tonic-gate 		}
17417c478bd9Sstevel@tonic-gate 
17427c478bd9Sstevel@tonic-gate 		/*
17437c478bd9Sstevel@tonic-gate 		 * We need all of those dynamic entries in order to put
17447c478bd9Sstevel@tonic-gate 		 * together a complete set of elf sections, but we'll
17457c478bd9Sstevel@tonic-gate 		 * let the PLT section slide if need be. The dynsym- and
17467c478bd9Sstevel@tonic-gate 		 * dynstr-related dynamic entries are mandatory in both
17477c478bd9Sstevel@tonic-gate 		 * executables and shared objects so if one of those is
17487c478bd9Sstevel@tonic-gate 		 * missing, we're in some trouble and should abort.
17497c478bd9Sstevel@tonic-gate 		 */
17507c478bd9Sstevel@tonic-gate 		if (dcount + 4 != DI_NENT) {
17517c478bd9Sstevel@tonic-gate 			dprintf("text section missing required dynamic "
17527c478bd9Sstevel@tonic-gate 			    "entries\n");
17537c478bd9Sstevel@tonic-gate 			return (NULL);
17547c478bd9Sstevel@tonic-gate 		}
17557c478bd9Sstevel@tonic-gate 
17567c478bd9Sstevel@tonic-gate 		if (ehdr.e_type == ET_DYN) {
17577c478bd9Sstevel@tonic-gate 			if (d[DI_PLTGOT] != NULL)
17587c478bd9Sstevel@tonic-gate 				d[DI_PLTGOT]->d_un.d_ptr += addr;
17597c478bd9Sstevel@tonic-gate 			if (d[DI_JMPREL] != NULL)
17607c478bd9Sstevel@tonic-gate 				d[DI_JMPREL]->d_un.d_ptr += addr;
17617c478bd9Sstevel@tonic-gate 			d[DI_SYMTAB]->d_un.d_ptr += addr;
17627c478bd9Sstevel@tonic-gate 			d[DI_HASH]->d_un.d_ptr += addr;
17637c478bd9Sstevel@tonic-gate 			d[DI_STRTAB]->d_un.d_ptr += addr;
17647c478bd9Sstevel@tonic-gate 		}
17657c478bd9Sstevel@tonic-gate 
17667c478bd9Sstevel@tonic-gate 		/* elf header */
17677c478bd9Sstevel@tonic-gate 		size = sizeof (Elf64_Ehdr);
17687c478bd9Sstevel@tonic-gate 
17697c478bd9Sstevel@tonic-gate 		/* program headers from in-core elf fragment */
177030da1432Sahl 		size += phnum * ehdr.e_phentsize;
17717c478bd9Sstevel@tonic-gate 
17727c478bd9Sstevel@tonic-gate 		/* unused shdr, and .shstrtab section */
17737c478bd9Sstevel@tonic-gate 		size += sizeof (Elf64_Shdr);
17747c478bd9Sstevel@tonic-gate 		size += sizeof (Elf64_Shdr);
17757c478bd9Sstevel@tonic-gate 		size += roundup(sizeof (shstr), 8);
17767c478bd9Sstevel@tonic-gate 
17777c478bd9Sstevel@tonic-gate 		/* .dynsym section */
17787c478bd9Sstevel@tonic-gate 		size += sizeof (Elf64_Shdr);
17797c478bd9Sstevel@tonic-gate 		if (Pread(P, &nchain, sizeof (nchain),
17807c478bd9Sstevel@tonic-gate 		    d[DI_HASH]->d_un.d_ptr + 4) != sizeof (nchain))
17817c478bd9Sstevel@tonic-gate 			goto bad64;
17827c478bd9Sstevel@tonic-gate 		size += sizeof (Elf64_Sym) * nchain;
17837c478bd9Sstevel@tonic-gate 
17847c478bd9Sstevel@tonic-gate 		/* .dynstr section */
17857c478bd9Sstevel@tonic-gate 		size += sizeof (Elf64_Shdr);
17867c478bd9Sstevel@tonic-gate 		size += roundup(d[DI_STRSZ]->d_un.d_val, 8);
17877c478bd9Sstevel@tonic-gate 
17887c478bd9Sstevel@tonic-gate 		/* .dynamic section */
17897c478bd9Sstevel@tonic-gate 		size += sizeof (Elf64_Shdr);
17907c478bd9Sstevel@tonic-gate 		size += roundup(phdr.p_filesz, 8);
17917c478bd9Sstevel@tonic-gate 
17927c478bd9Sstevel@tonic-gate 		/* .plt section */
17937c478bd9Sstevel@tonic-gate 		if (d[DI_PLTGOT] != NULL && d[DI_JMPREL] != NULL &&
17947c478bd9Sstevel@tonic-gate 		    d[DI_PLTRELSZ] != NULL && d[DI_PLTREL] != NULL) {
17957c478bd9Sstevel@tonic-gate 			uintptr_t penult, ult;
17967c478bd9Sstevel@tonic-gate 			uintptr_t jmprel = d[DI_JMPREL]->d_un.d_ptr;
17977c478bd9Sstevel@tonic-gate 			size_t pltrelsz = d[DI_PLTRELSZ]->d_un.d_val;
17987c478bd9Sstevel@tonic-gate 
17997c478bd9Sstevel@tonic-gate 			if (d[DI_PLTREL]->d_un.d_val == DT_RELA) {
18007c478bd9Sstevel@tonic-gate 				uint_t ndx = pltrelsz / sizeof (Elf64_Rela) - 2;
18017c478bd9Sstevel@tonic-gate 				Elf64_Rela r[2];
18027c478bd9Sstevel@tonic-gate 
18037c478bd9Sstevel@tonic-gate 				if (Pread(P, r, sizeof (r), jmprel +
1804*9acbbeafSnn35248 				    sizeof (r[0]) * ndx) != sizeof (r)) {
1805*9acbbeafSnn35248 					dprintf("Pread jmprel DT_RELA at %p "
1806*9acbbeafSnn35248 					    "failed\n",
1807*9acbbeafSnn35248 					    (void *)(jmprel +
1808*9acbbeafSnn35248 						sizeof (r[0]) * ndx));
18097c478bd9Sstevel@tonic-gate 					goto bad64;
1810*9acbbeafSnn35248 				}
18117c478bd9Sstevel@tonic-gate 
18127c478bd9Sstevel@tonic-gate 				penult = r[0].r_offset;
18137c478bd9Sstevel@tonic-gate 				ult = r[1].r_offset;
18147c478bd9Sstevel@tonic-gate 
18157c478bd9Sstevel@tonic-gate 			} else if (d[DI_PLTREL]->d_un.d_val == DT_REL) {
18167c478bd9Sstevel@tonic-gate 				uint_t ndx = pltrelsz / sizeof (Elf64_Rel) - 2;
18177c478bd9Sstevel@tonic-gate 				Elf64_Rel r[2];
18187c478bd9Sstevel@tonic-gate 
18197c478bd9Sstevel@tonic-gate 				if (Pread(P, r, sizeof (r), jmprel +
1820*9acbbeafSnn35248 				    sizeof (r[0]) * ndx) != sizeof (r)) {
1821*9acbbeafSnn35248 					dprintf("Pread jmprel DT_REL at %p "
1822*9acbbeafSnn35248 					    "failed\n",
1823*9acbbeafSnn35248 					    (void *)(jmprel +
1824*9acbbeafSnn35248 						sizeof (r[0]) * ndx));
18257c478bd9Sstevel@tonic-gate 					goto bad64;
1826*9acbbeafSnn35248 				}
18277c478bd9Sstevel@tonic-gate 
18287c478bd9Sstevel@tonic-gate 				penult = r[0].r_offset;
18297c478bd9Sstevel@tonic-gate 				ult = r[1].r_offset;
18307c478bd9Sstevel@tonic-gate 			} else {
1831*9acbbeafSnn35248 				dprintf("DT_PLTREL value %p unknown\n",
1832*9acbbeafSnn35248 				    (void *)d[DI_PLTREL]->d_un.d_ptr);
18337c478bd9Sstevel@tonic-gate 				goto bad64;
18347c478bd9Sstevel@tonic-gate 			}
18357c478bd9Sstevel@tonic-gate 
18367c478bd9Sstevel@tonic-gate 			pltentsz = ult - penult;
18377c478bd9Sstevel@tonic-gate 
18387c478bd9Sstevel@tonic-gate 			if (ehdr.e_type == ET_DYN)
18397c478bd9Sstevel@tonic-gate 				ult += addr;
18407c478bd9Sstevel@tonic-gate 
18417c478bd9Sstevel@tonic-gate 			pltsz = ult - d[DI_PLTGOT]->d_un.d_ptr + pltentsz;
18427c478bd9Sstevel@tonic-gate 
18437c478bd9Sstevel@tonic-gate 			size += sizeof (Elf64_Shdr);
18447c478bd9Sstevel@tonic-gate 			size += roundup(pltsz, 8);
18457c478bd9Sstevel@tonic-gate 		}
18467c478bd9Sstevel@tonic-gate 
18477c478bd9Sstevel@tonic-gate 		if ((elfdata = calloc(1, size)) == NULL)
18487c478bd9Sstevel@tonic-gate 			goto bad64;
18497c478bd9Sstevel@tonic-gate 
18507c478bd9Sstevel@tonic-gate 		/* LINTED - alignment */
18517c478bd9Sstevel@tonic-gate 		ep = (Elf64_Ehdr *)elfdata;
18527c478bd9Sstevel@tonic-gate 		(void) memcpy(ep, &ehdr, offsetof(Elf64_Ehdr, e_phoff));
18537c478bd9Sstevel@tonic-gate 
18547c478bd9Sstevel@tonic-gate 		ep->e_ehsize = sizeof (Elf64_Ehdr);
18557c478bd9Sstevel@tonic-gate 		ep->e_phoff = sizeof (Elf64_Ehdr);
18567c478bd9Sstevel@tonic-gate 		ep->e_phentsize = ehdr.e_phentsize;
185730da1432Sahl 		ep->e_phnum = phnum;
185830da1432Sahl 		ep->e_shoff = ep->e_phoff + phnum * ep->e_phentsize;
18597c478bd9Sstevel@tonic-gate 		ep->e_shentsize = sizeof (Elf64_Shdr);
18607c478bd9Sstevel@tonic-gate 		ep->e_shnum = (pltsz == 0) ? 5 : 6;
18617c478bd9Sstevel@tonic-gate 		ep->e_shstrndx = 1;
18627c478bd9Sstevel@tonic-gate 
18637c478bd9Sstevel@tonic-gate 		/* LINTED - alignment */
18647c478bd9Sstevel@tonic-gate 		sp = (Elf64_Shdr *)(elfdata + ep->e_shoff);
18657c478bd9Sstevel@tonic-gate 		off = ep->e_shoff + ep->e_shentsize * ep->e_shnum;
18667c478bd9Sstevel@tonic-gate 
18677c478bd9Sstevel@tonic-gate 		/*
18687c478bd9Sstevel@tonic-gate 		 * Copying the program headers directly from the process's
18697c478bd9Sstevel@tonic-gate 		 * address space is a little suspect, but since we only
18707c478bd9Sstevel@tonic-gate 		 * use them for their address and size values, this is fine.
18717c478bd9Sstevel@tonic-gate 		 */
187230da1432Sahl 		if (Pread(P, &elfdata[ep->e_phoff], phnum * ep->e_phentsize,
187330da1432Sahl 		    addr + ehdr.e_phoff) != phnum * ep->e_phentsize) {
18747c478bd9Sstevel@tonic-gate 			free(elfdata);
18757c478bd9Sstevel@tonic-gate 			goto bad64;
18767c478bd9Sstevel@tonic-gate 		}
18777c478bd9Sstevel@tonic-gate 
18787c478bd9Sstevel@tonic-gate 		/*
18797c478bd9Sstevel@tonic-gate 		 * The first elf section is always skipped.
18807c478bd9Sstevel@tonic-gate 		 */
18817c478bd9Sstevel@tonic-gate 		sp++;
18827c478bd9Sstevel@tonic-gate 
18837c478bd9Sstevel@tonic-gate 		/*
18847c478bd9Sstevel@tonic-gate 		 * Section Header[1]  sh_name: .shstrtab
18857c478bd9Sstevel@tonic-gate 		 */
18867c478bd9Sstevel@tonic-gate 		sp->sh_name = 0;
18877c478bd9Sstevel@tonic-gate 		sp->sh_type = SHT_STRTAB;
18887c478bd9Sstevel@tonic-gate 		sp->sh_flags = SHF_STRINGS;
18897c478bd9Sstevel@tonic-gate 		sp->sh_addr = 0;
18907c478bd9Sstevel@tonic-gate 		sp->sh_offset = off;
18917c478bd9Sstevel@tonic-gate 		sp->sh_size = sizeof (shstr);
18927c478bd9Sstevel@tonic-gate 		sp->sh_link = 0;
18937c478bd9Sstevel@tonic-gate 		sp->sh_info = 0;
18947c478bd9Sstevel@tonic-gate 		sp->sh_addralign = 1;
18957c478bd9Sstevel@tonic-gate 		sp->sh_entsize = 0;
18967c478bd9Sstevel@tonic-gate 
18977c478bd9Sstevel@tonic-gate 		(void) memcpy(&elfdata[off], shstr, sizeof (shstr));
18987c478bd9Sstevel@tonic-gate 		off += roundup(sp->sh_size, 8);
18997c478bd9Sstevel@tonic-gate 		sp++;
19007c478bd9Sstevel@tonic-gate 
19017c478bd9Sstevel@tonic-gate 		/*
19027c478bd9Sstevel@tonic-gate 		 * Section Header[2]  sh_name: .dynsym
19037c478bd9Sstevel@tonic-gate 		 */
19047c478bd9Sstevel@tonic-gate 		sp->sh_name = 10;
19057c478bd9Sstevel@tonic-gate 		sp->sh_type = SHT_DYNSYM;
19067c478bd9Sstevel@tonic-gate 		sp->sh_flags = SHF_ALLOC;
19077c478bd9Sstevel@tonic-gate 		sp->sh_addr = d[DI_SYMTAB]->d_un.d_ptr;
19087c478bd9Sstevel@tonic-gate 		if (ehdr.e_type == ET_DYN)
19097c478bd9Sstevel@tonic-gate 			sp->sh_addr -= addr;
19107c478bd9Sstevel@tonic-gate 		sp->sh_offset = off;
19117c478bd9Sstevel@tonic-gate 		sp->sh_size = nchain * sizeof (Elf64_Sym);
19127c478bd9Sstevel@tonic-gate 		sp->sh_link = 3;
19137c478bd9Sstevel@tonic-gate 		sp->sh_info = 1;
19147c478bd9Sstevel@tonic-gate 		sp->sh_addralign = 8;
19157c478bd9Sstevel@tonic-gate 		sp->sh_entsize = sizeof (Elf64_Sym);
19167c478bd9Sstevel@tonic-gate 
19177c478bd9Sstevel@tonic-gate 		if (Pread(P, &elfdata[off], sp->sh_size,
19187c478bd9Sstevel@tonic-gate 		    d[DI_SYMTAB]->d_un.d_ptr) != sp->sh_size) {
19197c478bd9Sstevel@tonic-gate 			free(elfdata);
19207c478bd9Sstevel@tonic-gate 			goto bad64;
19217c478bd9Sstevel@tonic-gate 		}
19227c478bd9Sstevel@tonic-gate 
19237c478bd9Sstevel@tonic-gate 		off += roundup(sp->sh_size, 8);
19247c478bd9Sstevel@tonic-gate 		sp++;
19257c478bd9Sstevel@tonic-gate 
19267c478bd9Sstevel@tonic-gate 		/*
19277c478bd9Sstevel@tonic-gate 		 * Section Header[3]  sh_name: .dynstr
19287c478bd9Sstevel@tonic-gate 		 */
19297c478bd9Sstevel@tonic-gate 		sp->sh_name = 18;
19307c478bd9Sstevel@tonic-gate 		sp->sh_type = SHT_STRTAB;
19317c478bd9Sstevel@tonic-gate 		sp->sh_flags = SHF_ALLOC | SHF_STRINGS;
19327c478bd9Sstevel@tonic-gate 		sp->sh_addr = d[DI_STRTAB]->d_un.d_ptr;
19337c478bd9Sstevel@tonic-gate 		if (ehdr.e_type == ET_DYN)
19347c478bd9Sstevel@tonic-gate 			sp->sh_addr -= addr;
19357c478bd9Sstevel@tonic-gate 		sp->sh_offset = off;
19367c478bd9Sstevel@tonic-gate 		sp->sh_size = d[DI_STRSZ]->d_un.d_val;
19377c478bd9Sstevel@tonic-gate 		sp->sh_link = 0;
19387c478bd9Sstevel@tonic-gate 		sp->sh_info = 0;
19397c478bd9Sstevel@tonic-gate 		sp->sh_addralign = 1;
19407c478bd9Sstevel@tonic-gate 		sp->sh_entsize = 0;
19417c478bd9Sstevel@tonic-gate 
19427c478bd9Sstevel@tonic-gate 		if (Pread(P, &elfdata[off], sp->sh_size,
19437c478bd9Sstevel@tonic-gate 		    d[DI_STRTAB]->d_un.d_ptr) != sp->sh_size) {
19447c478bd9Sstevel@tonic-gate 			free(elfdata);
19457c478bd9Sstevel@tonic-gate 			goto bad64;
19467c478bd9Sstevel@tonic-gate 		}
19477c478bd9Sstevel@tonic-gate 		off += roundup(sp->sh_size, 8);
19487c478bd9Sstevel@tonic-gate 		sp++;
19497c478bd9Sstevel@tonic-gate 
19507c478bd9Sstevel@tonic-gate 		/*
19517c478bd9Sstevel@tonic-gate 		 * Section Header[4]  sh_name: .dynamic
19527c478bd9Sstevel@tonic-gate 		 */
19537c478bd9Sstevel@tonic-gate 		sp->sh_name = 26;
19547c478bd9Sstevel@tonic-gate 		sp->sh_type = SHT_DYNAMIC;
19557c478bd9Sstevel@tonic-gate 		sp->sh_flags = SHF_WRITE | SHF_ALLOC;
19567c478bd9Sstevel@tonic-gate 		sp->sh_addr = phdr.p_vaddr;
19577c478bd9Sstevel@tonic-gate 		if (ehdr.e_type == ET_DYN)
19587c478bd9Sstevel@tonic-gate 			sp->sh_addr -= addr;
19597c478bd9Sstevel@tonic-gate 		sp->sh_offset = off;
19607c478bd9Sstevel@tonic-gate 		sp->sh_size = phdr.p_filesz;
19617c478bd9Sstevel@tonic-gate 		sp->sh_link = 3;
19627c478bd9Sstevel@tonic-gate 		sp->sh_info = 0;
19637c478bd9Sstevel@tonic-gate 		sp->sh_addralign = 8;
19647c478bd9Sstevel@tonic-gate 		sp->sh_entsize = sizeof (Elf64_Dyn);
19657c478bd9Sstevel@tonic-gate 
19667c478bd9Sstevel@tonic-gate 		(void) memcpy(&elfdata[off], dp, sp->sh_size);
19677c478bd9Sstevel@tonic-gate 		off += roundup(sp->sh_size, 8);
19687c478bd9Sstevel@tonic-gate 		sp++;
19697c478bd9Sstevel@tonic-gate 
19707c478bd9Sstevel@tonic-gate 		/*
19717c478bd9Sstevel@tonic-gate 		 * Section Header[5]  sh_name: .plt
19727c478bd9Sstevel@tonic-gate 		 */
19737c478bd9Sstevel@tonic-gate 		if (pltsz != 0) {
19747c478bd9Sstevel@tonic-gate 			sp->sh_name = 35;
19757c478bd9Sstevel@tonic-gate 			sp->sh_type = SHT_PROGBITS;
19767c478bd9Sstevel@tonic-gate 			sp->sh_flags = SHF_WRITE | SHF_ALLOC | SHF_EXECINSTR;
19777c478bd9Sstevel@tonic-gate 			sp->sh_addr = d[DI_PLTGOT]->d_un.d_ptr;
19787c478bd9Sstevel@tonic-gate 			if (ehdr.e_type == ET_DYN)
19797c478bd9Sstevel@tonic-gate 				sp->sh_addr -= addr;
19807c478bd9Sstevel@tonic-gate 			sp->sh_offset = off;
19817c478bd9Sstevel@tonic-gate 			sp->sh_size = pltsz;
19827c478bd9Sstevel@tonic-gate 			sp->sh_link = 0;
19837c478bd9Sstevel@tonic-gate 			sp->sh_info = 0;
19847c478bd9Sstevel@tonic-gate 			sp->sh_addralign = 8;
19857c478bd9Sstevel@tonic-gate 			sp->sh_entsize = pltentsz;
19867c478bd9Sstevel@tonic-gate 
19877c478bd9Sstevel@tonic-gate 			if (Pread(P, &elfdata[off], sp->sh_size,
19887c478bd9Sstevel@tonic-gate 			    d[DI_PLTGOT]->d_un.d_ptr) != sp->sh_size) {
19897c478bd9Sstevel@tonic-gate 				free(elfdata);
19907c478bd9Sstevel@tonic-gate 				goto bad64;
19917c478bd9Sstevel@tonic-gate 			}
19927c478bd9Sstevel@tonic-gate 			off += roundup(sp->sh_size, 8);
19937c478bd9Sstevel@tonic-gate 			sp++;
19947c478bd9Sstevel@tonic-gate 		}
19957c478bd9Sstevel@tonic-gate 
19967c478bd9Sstevel@tonic-gate 		free(dp);
19977c478bd9Sstevel@tonic-gate 		goto good;
19987c478bd9Sstevel@tonic-gate 
19997c478bd9Sstevel@tonic-gate bad64:
20007c478bd9Sstevel@tonic-gate 		free(dp);
20017c478bd9Sstevel@tonic-gate 		return (NULL);
20027c478bd9Sstevel@tonic-gate #endif	/* _LP64 */
20037c478bd9Sstevel@tonic-gate 	}
20047c478bd9Sstevel@tonic-gate good:
20057c478bd9Sstevel@tonic-gate 	if ((elf = elf_memory(elfdata, size)) == NULL) {
20067c478bd9Sstevel@tonic-gate 		free(elfdata);
20077c478bd9Sstevel@tonic-gate 		return (NULL);
20087c478bd9Sstevel@tonic-gate 	}
20097c478bd9Sstevel@tonic-gate 
20107c478bd9Sstevel@tonic-gate 	fptr->file_elfmem = elfdata;
20117c478bd9Sstevel@tonic-gate 
20127c478bd9Sstevel@tonic-gate 	return (elf);
20137c478bd9Sstevel@tonic-gate }
20147c478bd9Sstevel@tonic-gate 
20157c478bd9Sstevel@tonic-gate /*
20167c478bd9Sstevel@tonic-gate  * We wouldn't need these if qsort(3C) took an argument for the callback...
20177c478bd9Sstevel@tonic-gate  */
20187c478bd9Sstevel@tonic-gate static mutex_t sort_mtx = DEFAULTMUTEX;
20197c478bd9Sstevel@tonic-gate static char *sort_strs;
20207c478bd9Sstevel@tonic-gate static GElf_Sym *sort_syms;
20217c478bd9Sstevel@tonic-gate 
20227c478bd9Sstevel@tonic-gate int
20237c478bd9Sstevel@tonic-gate byaddr_cmp_common(GElf_Sym *a, char *aname, GElf_Sym *b, char *bname)
20247c478bd9Sstevel@tonic-gate {
20257c478bd9Sstevel@tonic-gate 	if (a->st_value < b->st_value)
20267c478bd9Sstevel@tonic-gate 		return (-1);
20277c478bd9Sstevel@tonic-gate 	if (a->st_value > b->st_value)
20287c478bd9Sstevel@tonic-gate 		return (1);
20297c478bd9Sstevel@tonic-gate 
20307c478bd9Sstevel@tonic-gate 	/*
20317c478bd9Sstevel@tonic-gate 	 * Prefer the function to the non-function.
20327c478bd9Sstevel@tonic-gate 	 */
20337c478bd9Sstevel@tonic-gate 	if (GELF_ST_TYPE(a->st_info) != GELF_ST_TYPE(b->st_info)) {
20347c478bd9Sstevel@tonic-gate 		if (GELF_ST_TYPE(a->st_info) == STT_FUNC)
20357c478bd9Sstevel@tonic-gate 			return (-1);
20367c478bd9Sstevel@tonic-gate 		if (GELF_ST_TYPE(b->st_info) == STT_FUNC)
20377c478bd9Sstevel@tonic-gate 			return (1);
20387c478bd9Sstevel@tonic-gate 	}
20397c478bd9Sstevel@tonic-gate 
20407c478bd9Sstevel@tonic-gate 	/*
20417c478bd9Sstevel@tonic-gate 	 * Prefer the weak or strong global symbol to the local symbol.
20427c478bd9Sstevel@tonic-gate 	 */
20437c478bd9Sstevel@tonic-gate 	if (GELF_ST_BIND(a->st_info) != GELF_ST_BIND(b->st_info)) {
20447c478bd9Sstevel@tonic-gate 		if (GELF_ST_BIND(b->st_info) == STB_LOCAL)
20457c478bd9Sstevel@tonic-gate 			return (-1);
20467c478bd9Sstevel@tonic-gate 		if (GELF_ST_BIND(a->st_info) == STB_LOCAL)
20477c478bd9Sstevel@tonic-gate 			return (1);
20487c478bd9Sstevel@tonic-gate 	}
20497c478bd9Sstevel@tonic-gate 
20507c478bd9Sstevel@tonic-gate 	/*
2051ab9a77c7Sahl 	 * Prefer the symbol that doesn't begin with a '$' since compilers and
2052ab9a77c7Sahl 	 * other symbol generators often use it as a prefix.
2053ab9a77c7Sahl 	 */
2054ab9a77c7Sahl 	if (*bname == '$')
2055ab9a77c7Sahl 		return (-1);
2056ab9a77c7Sahl 	if (*aname == '$')
2057ab9a77c7Sahl 		return (1);
2058ab9a77c7Sahl 
2059ab9a77c7Sahl 	/*
20607c478bd9Sstevel@tonic-gate 	 * Prefer the name with fewer leading underscores in the name.
20617c478bd9Sstevel@tonic-gate 	 */
20627c478bd9Sstevel@tonic-gate 	while (*aname == '_' && *bname == '_') {
20637c478bd9Sstevel@tonic-gate 		aname++;
20647c478bd9Sstevel@tonic-gate 		bname++;
20657c478bd9Sstevel@tonic-gate 	}
20667c478bd9Sstevel@tonic-gate 
20677c478bd9Sstevel@tonic-gate 	if (*bname == '_')
20687c478bd9Sstevel@tonic-gate 		return (-1);
20697c478bd9Sstevel@tonic-gate 	if (*aname == '_')
20707c478bd9Sstevel@tonic-gate 		return (1);
20717c478bd9Sstevel@tonic-gate 
20727c478bd9Sstevel@tonic-gate 	/*
20737c478bd9Sstevel@tonic-gate 	 * Prefer the symbol with the smaller size.
20747c478bd9Sstevel@tonic-gate 	 */
20757c478bd9Sstevel@tonic-gate 	if (a->st_size < b->st_size)
20767c478bd9Sstevel@tonic-gate 		return (-1);
20777c478bd9Sstevel@tonic-gate 	if (a->st_size > b->st_size)
20787c478bd9Sstevel@tonic-gate 		return (1);
20797c478bd9Sstevel@tonic-gate 
20807c478bd9Sstevel@tonic-gate 	/*
20817c478bd9Sstevel@tonic-gate 	 * All other factors being equal, fall back to lexicographic order.
20827c478bd9Sstevel@tonic-gate 	 */
20837c478bd9Sstevel@tonic-gate 	return (strcmp(aname, bname));
20847c478bd9Sstevel@tonic-gate }
20857c478bd9Sstevel@tonic-gate 
20867c478bd9Sstevel@tonic-gate static int
20877c478bd9Sstevel@tonic-gate byaddr_cmp(const void *aa, const void *bb)
20887c478bd9Sstevel@tonic-gate {
20897c478bd9Sstevel@tonic-gate 	GElf_Sym *a = &sort_syms[*(uint_t *)aa];
20907c478bd9Sstevel@tonic-gate 	GElf_Sym *b = &sort_syms[*(uint_t *)bb];
20917c478bd9Sstevel@tonic-gate 	char *aname = sort_strs + a->st_name;
20927c478bd9Sstevel@tonic-gate 	char *bname = sort_strs + b->st_name;
20937c478bd9Sstevel@tonic-gate 
20947c478bd9Sstevel@tonic-gate 	return (byaddr_cmp_common(a, aname, b, bname));
20957c478bd9Sstevel@tonic-gate }
20967c478bd9Sstevel@tonic-gate 
20977c478bd9Sstevel@tonic-gate static int
20987c478bd9Sstevel@tonic-gate byname_cmp(const void *aa, const void *bb)
20997c478bd9Sstevel@tonic-gate {
21007c478bd9Sstevel@tonic-gate 	GElf_Sym *a = &sort_syms[*(uint_t *)aa];
21017c478bd9Sstevel@tonic-gate 	GElf_Sym *b = &sort_syms[*(uint_t *)bb];
21027c478bd9Sstevel@tonic-gate 	char *aname = sort_strs + a->st_name;
21037c478bd9Sstevel@tonic-gate 	char *bname = sort_strs + b->st_name;
21047c478bd9Sstevel@tonic-gate 
21057c478bd9Sstevel@tonic-gate 	return (strcmp(aname, bname));
21067c478bd9Sstevel@tonic-gate }
21077c478bd9Sstevel@tonic-gate 
21087c478bd9Sstevel@tonic-gate void
21097c478bd9Sstevel@tonic-gate optimize_symtab(sym_tbl_t *symtab)
21107c478bd9Sstevel@tonic-gate {
21117c478bd9Sstevel@tonic-gate 	GElf_Sym *symp, *syms;
21127c478bd9Sstevel@tonic-gate 	uint_t i, *indexa, *indexb;
21137c478bd9Sstevel@tonic-gate 	Elf_Data *data;
21147c478bd9Sstevel@tonic-gate 	size_t symn, strsz, count;
21157c478bd9Sstevel@tonic-gate 
21167c478bd9Sstevel@tonic-gate 	if (symtab == NULL || symtab->sym_data == NULL ||
21177c478bd9Sstevel@tonic-gate 	    symtab->sym_byaddr != NULL)
21187c478bd9Sstevel@tonic-gate 		return;
21197c478bd9Sstevel@tonic-gate 
21207c478bd9Sstevel@tonic-gate 	data = symtab->sym_data;
21217c478bd9Sstevel@tonic-gate 	symn = symtab->sym_symn;
21227c478bd9Sstevel@tonic-gate 	strsz = symtab->sym_strsz;
21237c478bd9Sstevel@tonic-gate 
21247c478bd9Sstevel@tonic-gate 	symp = syms = malloc(sizeof (GElf_Sym) * symn);
21257c478bd9Sstevel@tonic-gate 
21267c478bd9Sstevel@tonic-gate 	/*
21277c478bd9Sstevel@tonic-gate 	 * First record all the symbols into a table and count up the ones
21287c478bd9Sstevel@tonic-gate 	 * that we're interested in. We mark symbols as invalid by setting
21297c478bd9Sstevel@tonic-gate 	 * the st_name to an illegal value.
21307c478bd9Sstevel@tonic-gate 	 */
21317c478bd9Sstevel@tonic-gate 	for (i = 0, count = 0; i < symn; i++, symp++) {
21327c478bd9Sstevel@tonic-gate 		if (gelf_getsym(data, i, symp) != NULL &&
21337c478bd9Sstevel@tonic-gate 		    symp->st_name < strsz &&
21347c478bd9Sstevel@tonic-gate 		    IS_DATA_TYPE(GELF_ST_TYPE(symp->st_info)))
21357c478bd9Sstevel@tonic-gate 			count++;
21367c478bd9Sstevel@tonic-gate 		else
21377c478bd9Sstevel@tonic-gate 			symp->st_name = strsz;
21387c478bd9Sstevel@tonic-gate 	}
21397c478bd9Sstevel@tonic-gate 
21407c478bd9Sstevel@tonic-gate 	/*
21417c478bd9Sstevel@tonic-gate 	 * Allocate sufficient space for both tables and populate them
21427c478bd9Sstevel@tonic-gate 	 * with the same symbols we just counted.
21437c478bd9Sstevel@tonic-gate 	 */
21447c478bd9Sstevel@tonic-gate 	symtab->sym_count = count;
21457c478bd9Sstevel@tonic-gate 	indexa = symtab->sym_byaddr = calloc(sizeof (uint_t), count);
21467c478bd9Sstevel@tonic-gate 	indexb = symtab->sym_byname = calloc(sizeof (uint_t), count);
21477c478bd9Sstevel@tonic-gate 
21487c478bd9Sstevel@tonic-gate 	for (i = 0, symp = syms; i < symn; i++, symp++) {
21497c478bd9Sstevel@tonic-gate 		if (symp->st_name < strsz)
21507c478bd9Sstevel@tonic-gate 			*indexa++ = *indexb++ = i;
21517c478bd9Sstevel@tonic-gate 	}
21527c478bd9Sstevel@tonic-gate 
21537c478bd9Sstevel@tonic-gate 	/*
21547c478bd9Sstevel@tonic-gate 	 * Sort the two tables according to the appropriate criteria.
21557c478bd9Sstevel@tonic-gate 	 */
21567c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&sort_mtx);
21577c478bd9Sstevel@tonic-gate 	sort_strs = symtab->sym_strs;
21587c478bd9Sstevel@tonic-gate 	sort_syms = syms;
21597c478bd9Sstevel@tonic-gate 
21607c478bd9Sstevel@tonic-gate 	qsort(symtab->sym_byaddr, count, sizeof (uint_t), byaddr_cmp);
21617c478bd9Sstevel@tonic-gate 	qsort(symtab->sym_byname, count, sizeof (uint_t), byname_cmp);
21627c478bd9Sstevel@tonic-gate 
21637c478bd9Sstevel@tonic-gate 	sort_strs = NULL;
21647c478bd9Sstevel@tonic-gate 	sort_syms = NULL;
21657c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&sort_mtx);
21667c478bd9Sstevel@tonic-gate 
21677c478bd9Sstevel@tonic-gate 	free(syms);
21687c478bd9Sstevel@tonic-gate }
21697c478bd9Sstevel@tonic-gate 
21707c478bd9Sstevel@tonic-gate /*
21717c478bd9Sstevel@tonic-gate  * Build the symbol table for the given mapped file.
21727c478bd9Sstevel@tonic-gate  */
21737c478bd9Sstevel@tonic-gate void
21747c478bd9Sstevel@tonic-gate Pbuild_file_symtab(struct ps_prochandle *P, file_info_t *fptr)
21757c478bd9Sstevel@tonic-gate {
21767c478bd9Sstevel@tonic-gate 	char objectfile[PATH_MAX];
21777c478bd9Sstevel@tonic-gate 	uint_t i;
21787c478bd9Sstevel@tonic-gate 
21797c478bd9Sstevel@tonic-gate 	GElf_Ehdr ehdr;
21807c478bd9Sstevel@tonic-gate 	GElf_Sym s;
21817c478bd9Sstevel@tonic-gate 
21827c478bd9Sstevel@tonic-gate 	Elf_Data *shdata;
21837c478bd9Sstevel@tonic-gate 	Elf_Scn *scn;
21847c478bd9Sstevel@tonic-gate 	Elf *elf;
218530da1432Sahl 	size_t nshdrs, shstrndx;
21867c478bd9Sstevel@tonic-gate 
21877c478bd9Sstevel@tonic-gate 	struct {
21887c478bd9Sstevel@tonic-gate 		GElf_Shdr c_shdr;
21897c478bd9Sstevel@tonic-gate 		Elf_Data *c_data;
21907c478bd9Sstevel@tonic-gate 		const char *c_name;
21917c478bd9Sstevel@tonic-gate 	} *cp, *cache = NULL, *dyn = NULL, *plt = NULL, *ctf = NULL;
21927c478bd9Sstevel@tonic-gate 
21937c478bd9Sstevel@tonic-gate 	if (fptr->file_init)
21947c478bd9Sstevel@tonic-gate 		return;	/* We've already processed this file */
21957c478bd9Sstevel@tonic-gate 
21967c478bd9Sstevel@tonic-gate 	/*
21977c478bd9Sstevel@tonic-gate 	 * Mark the file_info struct as having the symbol table initialized
21987c478bd9Sstevel@tonic-gate 	 * even if we fail below.  We tried once; we don't try again.
21997c478bd9Sstevel@tonic-gate 	 */
22007c478bd9Sstevel@tonic-gate 	fptr->file_init = 1;
22017c478bd9Sstevel@tonic-gate 
22027c478bd9Sstevel@tonic-gate 	if (elf_version(EV_CURRENT) == EV_NONE) {
22037c478bd9Sstevel@tonic-gate 		dprintf("libproc ELF version is more recent than libelf\n");
22047c478bd9Sstevel@tonic-gate 		return;
22057c478bd9Sstevel@tonic-gate 	}
22067c478bd9Sstevel@tonic-gate 
22077c478bd9Sstevel@tonic-gate 	if (P->state == PS_DEAD || P->state == PS_IDLE) {
22087c478bd9Sstevel@tonic-gate 		/*
22097c478bd9Sstevel@tonic-gate 		 * If we're a not live, we can't open files from the /proc
22107c478bd9Sstevel@tonic-gate 		 * object directory; we have only the mapping and file names
22117c478bd9Sstevel@tonic-gate 		 * to guide us.  We prefer the file_lname, but need to handle
22127c478bd9Sstevel@tonic-gate 		 * the case of it being NULL in order to bootstrap: we first
22137c478bd9Sstevel@tonic-gate 		 * come here during rd_new() when the only information we have
22147c478bd9Sstevel@tonic-gate 		 * is interpreter name associated with the AT_BASE mapping.
22157c478bd9Sstevel@tonic-gate 		 */
22167c478bd9Sstevel@tonic-gate 		(void) snprintf(objectfile, sizeof (objectfile), "%s",
22177c478bd9Sstevel@tonic-gate 		    fptr->file_lname ? fptr->file_lname : fptr->file_pname);
22187c478bd9Sstevel@tonic-gate 	} else {
22197c478bd9Sstevel@tonic-gate 		(void) snprintf(objectfile, sizeof (objectfile),
2220*9acbbeafSnn35248 		    "%s/%d/object/%s",
2221*9acbbeafSnn35248 		    procfs_path, (int)P->pid, fptr->file_pname);
22227c478bd9Sstevel@tonic-gate 	}
22237c478bd9Sstevel@tonic-gate 
22247c478bd9Sstevel@tonic-gate 	/*
22257c478bd9Sstevel@tonic-gate 	 * Open the object file, create the elf file, and then get the elf
22267c478bd9Sstevel@tonic-gate 	 * header and .shstrtab data buffer so we can process sections by
22277c478bd9Sstevel@tonic-gate 	 * name. If anything goes wrong try to fake up an elf file from
22287c478bd9Sstevel@tonic-gate 	 * the in-core elf image.
22297c478bd9Sstevel@tonic-gate 	 */
22307c478bd9Sstevel@tonic-gate 	if ((fptr->file_fd = open(objectfile, O_RDONLY)) < 0) {
22317c478bd9Sstevel@tonic-gate 		dprintf("Pbuild_file_symtab: failed to open %s: %s\n",
22327c478bd9Sstevel@tonic-gate 		    objectfile, strerror(errno));
22337c478bd9Sstevel@tonic-gate 
22347c478bd9Sstevel@tonic-gate 		if ((elf = fake_elf(P, fptr)) == NULL ||
22357c478bd9Sstevel@tonic-gate 		    elf_kind(elf) != ELF_K_ELF ||
22367c478bd9Sstevel@tonic-gate 		    gelf_getehdr(elf, &ehdr) == NULL ||
223730da1432Sahl 		    elf_getshnum(elf, &nshdrs) == 0 ||
223830da1432Sahl 		    elf_getshstrndx(elf, &shstrndx) == 0 ||
223930da1432Sahl 		    (scn = elf_getscn(elf, shstrndx)) == NULL ||
22407c478bd9Sstevel@tonic-gate 		    (shdata = elf_getdata(scn, NULL)) == NULL) {
22417c478bd9Sstevel@tonic-gate 			dprintf("failed to fake up ELF file\n");
22427c478bd9Sstevel@tonic-gate 			return;
22437c478bd9Sstevel@tonic-gate 		}
22447c478bd9Sstevel@tonic-gate 
22457c478bd9Sstevel@tonic-gate 	} else if ((elf = elf_begin(fptr->file_fd, ELF_C_READ, NULL)) == NULL ||
22467c478bd9Sstevel@tonic-gate 	    elf_kind(elf) != ELF_K_ELF ||
22477c478bd9Sstevel@tonic-gate 	    gelf_getehdr(elf, &ehdr) == NULL ||
224830da1432Sahl 	    elf_getshnum(elf, &nshdrs) == 0 ||
224930da1432Sahl 	    elf_getshstrndx(elf, &shstrndx) == 0 ||
225030da1432Sahl 	    (scn = elf_getscn(elf, shstrndx)) == NULL ||
22517c478bd9Sstevel@tonic-gate 	    (shdata = elf_getdata(scn, NULL)) == NULL) {
2252*9acbbeafSnn35248 		int err = elf_errno();
2253*9acbbeafSnn35248 
22547c478bd9Sstevel@tonic-gate 		dprintf("failed to process ELF file %s: %s\n",
2255*9acbbeafSnn35248 		    objectfile, (err == 0) ? "<null>" : elf_errmsg(err));
22567c478bd9Sstevel@tonic-gate 
22577c478bd9Sstevel@tonic-gate 		if ((elf = fake_elf(P, fptr)) == NULL ||
22587c478bd9Sstevel@tonic-gate 		    elf_kind(elf) != ELF_K_ELF ||
22597c478bd9Sstevel@tonic-gate 		    gelf_getehdr(elf, &ehdr) == NULL ||
226030da1432Sahl 		    elf_getshnum(elf, &nshdrs) == 0 ||
226130da1432Sahl 		    elf_getshstrndx(elf, &shstrndx) == 0 ||
226230da1432Sahl 		    (scn = elf_getscn(elf, shstrndx)) == NULL ||
22637c478bd9Sstevel@tonic-gate 		    (shdata = elf_getdata(scn, NULL)) == NULL) {
22647c478bd9Sstevel@tonic-gate 			dprintf("failed to fake up ELF file\n");
22657c478bd9Sstevel@tonic-gate 			goto bad;
22667c478bd9Sstevel@tonic-gate 		}
22677c478bd9Sstevel@tonic-gate 
22687c478bd9Sstevel@tonic-gate 	} else if (file_differs(P, elf, fptr)) {
22697c478bd9Sstevel@tonic-gate 		Elf *newelf;
22707c478bd9Sstevel@tonic-gate 
22717c478bd9Sstevel@tonic-gate 		/*
22727c478bd9Sstevel@tonic-gate 		 * Before we get too excited about this elf file, we'll check
22737c478bd9Sstevel@tonic-gate 		 * its checksum value against the value we have in memory. If
22747c478bd9Sstevel@tonic-gate 		 * they don't agree, we try to fake up a new elf file and
22757c478bd9Sstevel@tonic-gate 		 * proceed with that instead.
22767c478bd9Sstevel@tonic-gate 		 */
22777c478bd9Sstevel@tonic-gate 
22787c478bd9Sstevel@tonic-gate 		dprintf("ELF file %s (%lx) doesn't match in-core image\n",
22797c478bd9Sstevel@tonic-gate 		    fptr->file_pname,
22807c478bd9Sstevel@tonic-gate 		    (ulong_t)fptr->file_map->map_pmap.pr_vaddr);
22817c478bd9Sstevel@tonic-gate 
22827c478bd9Sstevel@tonic-gate 		if ((newelf = fake_elf(P, fptr)) == NULL ||
22837c478bd9Sstevel@tonic-gate 		    elf_kind(newelf) != ELF_K_ELF ||
22847c478bd9Sstevel@tonic-gate 		    gelf_getehdr(newelf, &ehdr) == NULL ||
228530da1432Sahl 		    elf_getshnum(newelf, &nshdrs) == 0 ||
228630da1432Sahl 		    elf_getshstrndx(newelf, &shstrndx) == 0 ||
228730da1432Sahl 		    (scn = elf_getscn(newelf, shstrndx)) == NULL ||
22887c478bd9Sstevel@tonic-gate 		    (shdata = elf_getdata(scn, NULL)) == NULL) {
22897c478bd9Sstevel@tonic-gate 			dprintf("failed to fake up ELF file\n");
22907c478bd9Sstevel@tonic-gate 		} else {
22917c478bd9Sstevel@tonic-gate 			(void) elf_end(elf);
22927c478bd9Sstevel@tonic-gate 			elf = newelf;
22937c478bd9Sstevel@tonic-gate 
22947c478bd9Sstevel@tonic-gate 			dprintf("switched to faked up ELF file\n");
22957c478bd9Sstevel@tonic-gate 		}
22967c478bd9Sstevel@tonic-gate 	}
22977c478bd9Sstevel@tonic-gate 
229830da1432Sahl 	if ((cache = malloc(nshdrs * sizeof (*cache))) == NULL) {
22997c478bd9Sstevel@tonic-gate 		dprintf("failed to malloc section cache for %s\n", objectfile);
23007c478bd9Sstevel@tonic-gate 		goto bad;
23017c478bd9Sstevel@tonic-gate 	}
23027c478bd9Sstevel@tonic-gate 
23037c478bd9Sstevel@tonic-gate 	dprintf("processing ELF file %s\n", objectfile);
23047c478bd9Sstevel@tonic-gate 	fptr->file_class = ehdr.e_ident[EI_CLASS];
23057c478bd9Sstevel@tonic-gate 	fptr->file_etype = ehdr.e_type;
23067c478bd9Sstevel@tonic-gate 	fptr->file_elf = elf;
2307f957ecc1Svb160487 	fptr->file_shstrs = shdata->d_buf;
2308f957ecc1Svb160487 	fptr->file_shstrsz = shdata->d_size;
23097c478bd9Sstevel@tonic-gate 
23107c478bd9Sstevel@tonic-gate 	/*
23117c478bd9Sstevel@tonic-gate 	 * Iterate through each section, caching its section header, data
23127c478bd9Sstevel@tonic-gate 	 * pointer, and name.  We use this for handling sh_link values below.
23137c478bd9Sstevel@tonic-gate 	 */
23147c478bd9Sstevel@tonic-gate 	for (cp = cache + 1, scn = NULL; scn = elf_nextscn(elf, scn); cp++) {
2315*9acbbeafSnn35248 		if (gelf_getshdr(scn, &cp->c_shdr) == NULL) {
2316*9acbbeafSnn35248 			dprintf("Pbuild_file_symtab: Failed to get section "
2317*9acbbeafSnn35248 			    "header\n");
23187c478bd9Sstevel@tonic-gate 			goto bad; /* Failed to get section header */
2319*9acbbeafSnn35248 		}
23207c478bd9Sstevel@tonic-gate 
2321*9acbbeafSnn35248 		if ((cp->c_data = elf_getdata(scn, NULL)) == NULL) {
2322*9acbbeafSnn35248 			dprintf("Pbuild_file_symtab: Failed to get section "
2323*9acbbeafSnn35248 			    "data\n");
23247c478bd9Sstevel@tonic-gate 			goto bad; /* Failed to get section data */
2325*9acbbeafSnn35248 		}
23267c478bd9Sstevel@tonic-gate 
2327*9acbbeafSnn35248 		if (cp->c_shdr.sh_name >= shdata->d_size) {
2328*9acbbeafSnn35248 			dprintf("Pbuild_file_symtab: corrupt section name");
23297c478bd9Sstevel@tonic-gate 			goto bad; /* Corrupt section name */
2330*9acbbeafSnn35248 		}
23317c478bd9Sstevel@tonic-gate 
23327c478bd9Sstevel@tonic-gate 		cp->c_name = (const char *)shdata->d_buf + cp->c_shdr.sh_name;
23337c478bd9Sstevel@tonic-gate 	}
23347c478bd9Sstevel@tonic-gate 
23357c478bd9Sstevel@tonic-gate 	/*
23367c478bd9Sstevel@tonic-gate 	 * Now iterate through the section cache in order to locate info
23377c478bd9Sstevel@tonic-gate 	 * for the .symtab, .dynsym, .dynamic, .plt, and .SUNW_ctf sections:
23387c478bd9Sstevel@tonic-gate 	 */
233930da1432Sahl 	for (i = 1, cp = cache + 1; i < nshdrs; i++, cp++) {
23407c478bd9Sstevel@tonic-gate 		GElf_Shdr *shp = &cp->c_shdr;
23417c478bd9Sstevel@tonic-gate 
23427c478bd9Sstevel@tonic-gate 		if (shp->sh_type == SHT_SYMTAB || shp->sh_type == SHT_DYNSYM) {
23437c478bd9Sstevel@tonic-gate 			sym_tbl_t *symp = shp->sh_type == SHT_SYMTAB ?
23447c478bd9Sstevel@tonic-gate 			    &fptr->file_symtab : &fptr->file_dynsym;
23457c478bd9Sstevel@tonic-gate 			/*
23467c478bd9Sstevel@tonic-gate 			 * It's possible that the we already got the symbol
23477c478bd9Sstevel@tonic-gate 			 * table from the core file itself. Either the file
23487c478bd9Sstevel@tonic-gate 			 * differs in which case our faked up elf file will
23497c478bd9Sstevel@tonic-gate 			 * only contain the dynsym (not the symtab) or the
23507c478bd9Sstevel@tonic-gate 			 * file matches in which case we'll just be replacing
23517c478bd9Sstevel@tonic-gate 			 * the symbol table we pulled out of the core file
23527c478bd9Sstevel@tonic-gate 			 * with an equivalent one. In either case, this
23537c478bd9Sstevel@tonic-gate 			 * check isn't essential, but it's a good idea.
23547c478bd9Sstevel@tonic-gate 			 */
23557c478bd9Sstevel@tonic-gate 			if (symp->sym_data == NULL) {
2356*9acbbeafSnn35248 				dprintf("Symbol table found for %s\n",
2357*9acbbeafSnn35248 				    objectfile);
23587c478bd9Sstevel@tonic-gate 				symp->sym_data = cp->c_data;
23597c478bd9Sstevel@tonic-gate 				symp->sym_symn = shp->sh_size / shp->sh_entsize;
23607c478bd9Sstevel@tonic-gate 				symp->sym_strs =
23617c478bd9Sstevel@tonic-gate 				    cache[shp->sh_link].c_data->d_buf;
23627c478bd9Sstevel@tonic-gate 				symp->sym_strsz =
23637c478bd9Sstevel@tonic-gate 				    cache[shp->sh_link].c_data->d_size;
23647c478bd9Sstevel@tonic-gate 				symp->sym_hdr = cp->c_shdr;
23657c478bd9Sstevel@tonic-gate 				symp->sym_strhdr = cache[shp->sh_link].c_shdr;
2366*9acbbeafSnn35248 			} else {
2367*9acbbeafSnn35248 				dprintf("Symbol table already there for %s\n",
2368*9acbbeafSnn35248 				    objectfile);
23697c478bd9Sstevel@tonic-gate 			}
23707c478bd9Sstevel@tonic-gate 
23717c478bd9Sstevel@tonic-gate 		} else if (shp->sh_type == SHT_DYNAMIC) {
23727c478bd9Sstevel@tonic-gate 			dyn = cp;
23737c478bd9Sstevel@tonic-gate 		} else if (strcmp(cp->c_name, ".plt") == 0) {
23747c478bd9Sstevel@tonic-gate 			plt = cp;
23757c478bd9Sstevel@tonic-gate 		} else if (strcmp(cp->c_name, ".SUNW_ctf") == 0) {
23767c478bd9Sstevel@tonic-gate 			/*
23777c478bd9Sstevel@tonic-gate 			 * Skip over bogus CTF sections so they don't come back
23787c478bd9Sstevel@tonic-gate 			 * to haunt us later.
23797c478bd9Sstevel@tonic-gate 			 */
23807c478bd9Sstevel@tonic-gate 			if (shp->sh_link == 0 ||
238130da1432Sahl 			    shp->sh_link >= nshdrs ||
23827c478bd9Sstevel@tonic-gate 			    (cache[shp->sh_link].c_shdr.sh_type != SHT_DYNSYM &&
23837c478bd9Sstevel@tonic-gate 			    cache[shp->sh_link].c_shdr.sh_type != SHT_SYMTAB)) {
23847c478bd9Sstevel@tonic-gate 				dprintf("Bad sh_link %d for "
23857c478bd9Sstevel@tonic-gate 				    "CTF\n", shp->sh_link);
23867c478bd9Sstevel@tonic-gate 				continue;
23877c478bd9Sstevel@tonic-gate 			}
23887c478bd9Sstevel@tonic-gate 			ctf = cp;
23897c478bd9Sstevel@tonic-gate 		}
23907c478bd9Sstevel@tonic-gate 	}
23917c478bd9Sstevel@tonic-gate 
23927c478bd9Sstevel@tonic-gate 	/*
23937c478bd9Sstevel@tonic-gate 	 * At this point, we've found all the symbol tables we're ever going
23947c478bd9Sstevel@tonic-gate 	 * to find: the ones in the loop above and possibly the symtab that
23957c478bd9Sstevel@tonic-gate 	 * was included in the core file. Before we perform any lookups, we
23967c478bd9Sstevel@tonic-gate 	 * create sorted versions to optimize for lookups.
23977c478bd9Sstevel@tonic-gate 	 */
23987c478bd9Sstevel@tonic-gate 	optimize_symtab(&fptr->file_symtab);
23997c478bd9Sstevel@tonic-gate 	optimize_symtab(&fptr->file_dynsym);
24007c478bd9Sstevel@tonic-gate 
24017c478bd9Sstevel@tonic-gate 	/*
24027c478bd9Sstevel@tonic-gate 	 * Fill in the base address of the text mapping for shared libraries.
24037c478bd9Sstevel@tonic-gate 	 * This allows us to translate symbols before librtld_db is ready.
24047c478bd9Sstevel@tonic-gate 	 */
24057c478bd9Sstevel@tonic-gate 	if (fptr->file_etype == ET_DYN) {
24067c478bd9Sstevel@tonic-gate 		fptr->file_dyn_base = fptr->file_map->map_pmap.pr_vaddr -
24077c478bd9Sstevel@tonic-gate 		    fptr->file_map->map_pmap.pr_offset;
2408*9acbbeafSnn35248 		dprintf("setting file_dyn_base for %s to %lx\n",
2409*9acbbeafSnn35248 		    objectfile, (long)fptr->file_dyn_base);
24107c478bd9Sstevel@tonic-gate 	}
24117c478bd9Sstevel@tonic-gate 
24127c478bd9Sstevel@tonic-gate 	/*
24137c478bd9Sstevel@tonic-gate 	 * Record the CTF section information in the file info structure.
24147c478bd9Sstevel@tonic-gate 	 */
24157c478bd9Sstevel@tonic-gate 	if (ctf != NULL) {
24167c478bd9Sstevel@tonic-gate 		fptr->file_ctf_off = ctf->c_shdr.sh_offset;
24177c478bd9Sstevel@tonic-gate 		fptr->file_ctf_size = ctf->c_shdr.sh_size;
24187c478bd9Sstevel@tonic-gate 		if (ctf->c_shdr.sh_link != 0 &&
24197c478bd9Sstevel@tonic-gate 		    cache[ctf->c_shdr.sh_link].c_shdr.sh_type == SHT_DYNSYM)
24207c478bd9Sstevel@tonic-gate 			fptr->file_ctf_dyn = 1;
24217c478bd9Sstevel@tonic-gate 	}
24227c478bd9Sstevel@tonic-gate 
24237c478bd9Sstevel@tonic-gate 	if (fptr->file_lo == NULL)
24247c478bd9Sstevel@tonic-gate 		goto done; /* Nothing else to do if no load object info */
24257c478bd9Sstevel@tonic-gate 
24267c478bd9Sstevel@tonic-gate 	/*
24277c478bd9Sstevel@tonic-gate 	 * If the object is a shared library and we have a different rl_base
24287c478bd9Sstevel@tonic-gate 	 * value, reset file_dyn_base according to librtld_db's information.
24297c478bd9Sstevel@tonic-gate 	 */
24307c478bd9Sstevel@tonic-gate 	if (fptr->file_etype == ET_DYN &&
24317c478bd9Sstevel@tonic-gate 	    fptr->file_lo->rl_base != fptr->file_dyn_base) {
2432*9acbbeafSnn35248 		dprintf("resetting file_dyn_base for %s to %lx\n",
2433*9acbbeafSnn35248 		    objectfile, (long)fptr->file_lo->rl_base);
24347c478bd9Sstevel@tonic-gate 		fptr->file_dyn_base = fptr->file_lo->rl_base;
24357c478bd9Sstevel@tonic-gate 	}
24367c478bd9Sstevel@tonic-gate 
24377c478bd9Sstevel@tonic-gate 	/*
24387c478bd9Sstevel@tonic-gate 	 * Fill in the PLT information for this file if a PLT symbol is found.
24397c478bd9Sstevel@tonic-gate 	 */
24407c478bd9Sstevel@tonic-gate 	if (sym_by_name(&fptr->file_dynsym, "_PROCEDURE_LINKAGE_TABLE_", &s,
24417c478bd9Sstevel@tonic-gate 	    NULL) != NULL) {
24427c478bd9Sstevel@tonic-gate 		fptr->file_plt_base = s.st_value + fptr->file_dyn_base;
24437c478bd9Sstevel@tonic-gate 		fptr->file_plt_size = (plt != NULL) ? plt->c_shdr.sh_size : 0;
24447c478bd9Sstevel@tonic-gate 
24457c478bd9Sstevel@tonic-gate 		/*
24467c478bd9Sstevel@tonic-gate 		 * Bring the load object up to date; it is the only way the
24477c478bd9Sstevel@tonic-gate 		 * user has to access the PLT data. The PLT information in the
24487c478bd9Sstevel@tonic-gate 		 * rd_loadobj_t is not set in the call to map_iter() (the
24497c478bd9Sstevel@tonic-gate 		 * callback for rd_loadobj_iter) where we set file_lo.
24507c478bd9Sstevel@tonic-gate 		 */
24517c478bd9Sstevel@tonic-gate 		fptr->file_lo->rl_plt_base = fptr->file_plt_base;
24527c478bd9Sstevel@tonic-gate 		fptr->file_lo->rl_plt_size = fptr->file_plt_size;
24537c478bd9Sstevel@tonic-gate 
24547c478bd9Sstevel@tonic-gate 		dprintf("PLT found at %p, size = %lu\n",
24557c478bd9Sstevel@tonic-gate 		    (void *)fptr->file_plt_base, (ulong_t)fptr->file_plt_size);
24567c478bd9Sstevel@tonic-gate 	}
24577c478bd9Sstevel@tonic-gate 
24587c478bd9Sstevel@tonic-gate 	/*
24597c478bd9Sstevel@tonic-gate 	 * Fill in the PLT information.
24607c478bd9Sstevel@tonic-gate 	 */
24617c478bd9Sstevel@tonic-gate 	if (dyn != NULL) {
24627c478bd9Sstevel@tonic-gate 		uintptr_t dynaddr = dyn->c_shdr.sh_addr + fptr->file_dyn_base;
24637c478bd9Sstevel@tonic-gate 		size_t ndyn = dyn->c_shdr.sh_size / dyn->c_shdr.sh_entsize;
24647c478bd9Sstevel@tonic-gate 		GElf_Dyn d;
24657c478bd9Sstevel@tonic-gate 
24667c478bd9Sstevel@tonic-gate 		for (i = 0; i < ndyn; i++) {
24677c478bd9Sstevel@tonic-gate 			if (gelf_getdyn(dyn->c_data, i, &d) != NULL &&
24687c478bd9Sstevel@tonic-gate 			    d.d_tag == DT_JMPREL) {
2469*9acbbeafSnn35248 				dprintf("DT_JMPREL is %p\n",
2470*9acbbeafSnn35248 				    (void *)(uintptr_t)d.d_un.d_ptr);
24717c478bd9Sstevel@tonic-gate 				fptr->file_jmp_rel =
24727c478bd9Sstevel@tonic-gate 				    d.d_un.d_ptr + fptr->file_dyn_base;
24737c478bd9Sstevel@tonic-gate 				break;
24747c478bd9Sstevel@tonic-gate 			}
24757c478bd9Sstevel@tonic-gate 		}
24767c478bd9Sstevel@tonic-gate 
24777c478bd9Sstevel@tonic-gate 		dprintf("_DYNAMIC found at %p, %lu entries, DT_JMPREL = %p\n",
24787c478bd9Sstevel@tonic-gate 		    (void *)dynaddr, (ulong_t)ndyn, (void *)fptr->file_jmp_rel);
24797c478bd9Sstevel@tonic-gate 	}
24807c478bd9Sstevel@tonic-gate 
24817c478bd9Sstevel@tonic-gate done:
24827c478bd9Sstevel@tonic-gate 	free(cache);
24837c478bd9Sstevel@tonic-gate 	return;
24847c478bd9Sstevel@tonic-gate 
24857c478bd9Sstevel@tonic-gate bad:
24867c478bd9Sstevel@tonic-gate 	if (cache != NULL)
24877c478bd9Sstevel@tonic-gate 		free(cache);
24887c478bd9Sstevel@tonic-gate 
24897c478bd9Sstevel@tonic-gate 	(void) elf_end(elf);
24907c478bd9Sstevel@tonic-gate 	fptr->file_elf = NULL;
24917c478bd9Sstevel@tonic-gate 	if (fptr->file_elfmem != NULL) {
24927c478bd9Sstevel@tonic-gate 		free(fptr->file_elfmem);
24937c478bd9Sstevel@tonic-gate 		fptr->file_elfmem = NULL;
24947c478bd9Sstevel@tonic-gate 	}
24957c478bd9Sstevel@tonic-gate 	(void) close(fptr->file_fd);
24967c478bd9Sstevel@tonic-gate 	fptr->file_fd = -1;
24977c478bd9Sstevel@tonic-gate }
24987c478bd9Sstevel@tonic-gate 
24997c478bd9Sstevel@tonic-gate /*
25007c478bd9Sstevel@tonic-gate  * Given a process virtual address, return the map_info_t containing it.
25017c478bd9Sstevel@tonic-gate  * If none found, return NULL.
25027c478bd9Sstevel@tonic-gate  */
25037c478bd9Sstevel@tonic-gate map_info_t *
25047c478bd9Sstevel@tonic-gate Paddr2mptr(struct ps_prochandle *P, uintptr_t addr)
25057c478bd9Sstevel@tonic-gate {
25067c478bd9Sstevel@tonic-gate 	int lo = 0;
25077c478bd9Sstevel@tonic-gate 	int hi = P->map_count - 1;
25087c478bd9Sstevel@tonic-gate 	int mid;
25097c478bd9Sstevel@tonic-gate 	map_info_t *mp;
25107c478bd9Sstevel@tonic-gate 
25117c478bd9Sstevel@tonic-gate 	while (lo <= hi) {
25127c478bd9Sstevel@tonic-gate 
25137c478bd9Sstevel@tonic-gate 		mid = (lo + hi) / 2;
25147c478bd9Sstevel@tonic-gate 		mp = &P->mappings[mid];
25157c478bd9Sstevel@tonic-gate 
25167c478bd9Sstevel@tonic-gate 		/* check that addr is in [vaddr, vaddr + size) */
25177c478bd9Sstevel@tonic-gate 		if ((addr - mp->map_pmap.pr_vaddr) < mp->map_pmap.pr_size)
25187c478bd9Sstevel@tonic-gate 			return (mp);
25197c478bd9Sstevel@tonic-gate 
25207c478bd9Sstevel@tonic-gate 		if (addr < mp->map_pmap.pr_vaddr)
25217c478bd9Sstevel@tonic-gate 			hi = mid - 1;
25227c478bd9Sstevel@tonic-gate 		else
25237c478bd9Sstevel@tonic-gate 			lo = mid + 1;
25247c478bd9Sstevel@tonic-gate 	}
25257c478bd9Sstevel@tonic-gate 
25267c478bd9Sstevel@tonic-gate 	return (NULL);
25277c478bd9Sstevel@tonic-gate }
25287c478bd9Sstevel@tonic-gate 
25297c478bd9Sstevel@tonic-gate /*
25307c478bd9Sstevel@tonic-gate  * Return the map_info_t for the executable file.
25317c478bd9Sstevel@tonic-gate  * If not found, return NULL.
25327c478bd9Sstevel@tonic-gate  */
25337c478bd9Sstevel@tonic-gate static map_info_t *
25347c478bd9Sstevel@tonic-gate exec_map(struct ps_prochandle *P)
25357c478bd9Sstevel@tonic-gate {
25367c478bd9Sstevel@tonic-gate 	uint_t i;
25377c478bd9Sstevel@tonic-gate 	map_info_t *mptr;
25387c478bd9Sstevel@tonic-gate 	map_info_t *mold = NULL;
25397c478bd9Sstevel@tonic-gate 	file_info_t *fptr;
25407c478bd9Sstevel@tonic-gate 	uintptr_t base;
25417c478bd9Sstevel@tonic-gate 
25427c478bd9Sstevel@tonic-gate 	for (i = 0, mptr = P->mappings; i < P->map_count; i++, mptr++) {
25437c478bd9Sstevel@tonic-gate 		if (mptr->map_pmap.pr_mapname[0] == '\0')
25447c478bd9Sstevel@tonic-gate 			continue;
25457c478bd9Sstevel@tonic-gate 		if (strcmp(mptr->map_pmap.pr_mapname, "a.out") == 0) {
25467c478bd9Sstevel@tonic-gate 			if ((fptr = mptr->map_file) != NULL &&
25477c478bd9Sstevel@tonic-gate 			    fptr->file_lo != NULL) {
25487c478bd9Sstevel@tonic-gate 				base = fptr->file_lo->rl_base;
25497c478bd9Sstevel@tonic-gate 				if (base >= mptr->map_pmap.pr_vaddr &&
25507c478bd9Sstevel@tonic-gate 				    base < mptr->map_pmap.pr_vaddr +
25517c478bd9Sstevel@tonic-gate 				    mptr->map_pmap.pr_size)	/* text space */
25527c478bd9Sstevel@tonic-gate 					return (mptr);
25537c478bd9Sstevel@tonic-gate 				mold = mptr;	/* must be the data */
25547c478bd9Sstevel@tonic-gate 				continue;
25557c478bd9Sstevel@tonic-gate 			}
25567c478bd9Sstevel@tonic-gate 			/* This is a poor way to test for text space */
25577c478bd9Sstevel@tonic-gate 			if (!(mptr->map_pmap.pr_mflags & MA_EXEC) ||
25587c478bd9Sstevel@tonic-gate 			    (mptr->map_pmap.pr_mflags & MA_WRITE)) {
25597c478bd9Sstevel@tonic-gate 				mold = mptr;
25607c478bd9Sstevel@tonic-gate 				continue;
25617c478bd9Sstevel@tonic-gate 			}
25627c478bd9Sstevel@tonic-gate 			return (mptr);
25637c478bd9Sstevel@tonic-gate 		}
25647c478bd9Sstevel@tonic-gate 	}
25657c478bd9Sstevel@tonic-gate 
25667c478bd9Sstevel@tonic-gate 	return (mold);
25677c478bd9Sstevel@tonic-gate }
25687c478bd9Sstevel@tonic-gate 
25697c478bd9Sstevel@tonic-gate /*
25707c478bd9Sstevel@tonic-gate  * Given a shared object name, return the map_info_t for it.  If no matching
25717c478bd9Sstevel@tonic-gate  * object is found, return NULL.  Normally, the link maps contain the full
25727c478bd9Sstevel@tonic-gate  * object pathname, e.g. /usr/lib/libc.so.1.  We allow the object name to
25737c478bd9Sstevel@tonic-gate  * take one of the following forms:
25747c478bd9Sstevel@tonic-gate  *
25757c478bd9Sstevel@tonic-gate  * 1. An exact match (i.e. a full pathname): "/usr/lib/libc.so.1"
25767c478bd9Sstevel@tonic-gate  * 2. An exact basename match: "libc.so.1"
25777c478bd9Sstevel@tonic-gate  * 3. An initial basename match up to a '.' suffix: "libc.so" or "libc"
25787c478bd9Sstevel@tonic-gate  * 4. The literal string "a.out" is an alias for the executable mapping
25797c478bd9Sstevel@tonic-gate  *
25807c478bd9Sstevel@tonic-gate  * The third case is a convenience for callers and may not be necessary.
25817c478bd9Sstevel@tonic-gate  *
25827c478bd9Sstevel@tonic-gate  * As the exact same object name may be loaded on different link maps (see
25837c478bd9Sstevel@tonic-gate  * dlmopen(3DL)), we also allow the caller to resolve the object name by
25847c478bd9Sstevel@tonic-gate  * specifying a particular link map id.  If lmid is PR_LMID_EVERY, the
25857c478bd9Sstevel@tonic-gate  * first matching name will be returned, regardless of the link map id.
25867c478bd9Sstevel@tonic-gate  */
25877c478bd9Sstevel@tonic-gate static map_info_t *
25887c478bd9Sstevel@tonic-gate object_to_map(struct ps_prochandle *P, Lmid_t lmid, const char *objname)
25897c478bd9Sstevel@tonic-gate {
25907c478bd9Sstevel@tonic-gate 	map_info_t *mp;
25917c478bd9Sstevel@tonic-gate 	file_info_t *fp;
25927c478bd9Sstevel@tonic-gate 	size_t objlen;
25937c478bd9Sstevel@tonic-gate 	uint_t i;
25947c478bd9Sstevel@tonic-gate 
25957c478bd9Sstevel@tonic-gate 	/*
2596*9acbbeafSnn35248 	 * If we have no rtld_db, then always treat a request as one for all
2597*9acbbeafSnn35248 	 * link maps.
2598*9acbbeafSnn35248 	 */
2599*9acbbeafSnn35248 	if (P->rap == NULL)
2600*9acbbeafSnn35248 		lmid = PR_LMID_EVERY;
2601*9acbbeafSnn35248 
2602*9acbbeafSnn35248 	/*
26037c478bd9Sstevel@tonic-gate 	 * First pass: look for exact matches of the entire pathname or
26047c478bd9Sstevel@tonic-gate 	 * basename (cases 1 and 2 above):
26057c478bd9Sstevel@tonic-gate 	 */
26067c478bd9Sstevel@tonic-gate 	for (i = 0, mp = P->mappings; i < P->map_count; i++, mp++) {
26077c478bd9Sstevel@tonic-gate 
26087c478bd9Sstevel@tonic-gate 		if (mp->map_pmap.pr_mapname[0] == '\0' ||
26097c478bd9Sstevel@tonic-gate 		    (fp = mp->map_file) == NULL || fp->file_lname == NULL)
26107c478bd9Sstevel@tonic-gate 			continue;
26117c478bd9Sstevel@tonic-gate 
26127c478bd9Sstevel@tonic-gate 		if (lmid != PR_LMID_EVERY &&
26137c478bd9Sstevel@tonic-gate 		    (fp->file_lo == NULL || lmid != fp->file_lo->rl_lmident))
26147c478bd9Sstevel@tonic-gate 			continue;
26157c478bd9Sstevel@tonic-gate 
26167c478bd9Sstevel@tonic-gate 		/*
26177c478bd9Sstevel@tonic-gate 		 * If we match, return the primary text mapping; otherwise
26187c478bd9Sstevel@tonic-gate 		 * just return the mapping we matched.
26197c478bd9Sstevel@tonic-gate 		 */
26207c478bd9Sstevel@tonic-gate 		if (strcmp(fp->file_lname, objname) == 0 ||
26217c478bd9Sstevel@tonic-gate 		    strcmp(fp->file_lbase, objname) == 0)
26227c478bd9Sstevel@tonic-gate 			return (fp->file_map ? fp->file_map : mp);
26237c478bd9Sstevel@tonic-gate 	}
26247c478bd9Sstevel@tonic-gate 
26257c478bd9Sstevel@tonic-gate 	objlen = strlen(objname);
26267c478bd9Sstevel@tonic-gate 
26277c478bd9Sstevel@tonic-gate 	/*
26287c478bd9Sstevel@tonic-gate 	 * Second pass: look for partial matches (case 3 above):
26297c478bd9Sstevel@tonic-gate 	 */
26307c478bd9Sstevel@tonic-gate 	for (i = 0, mp = P->mappings; i < P->map_count; i++, mp++) {
26317c478bd9Sstevel@tonic-gate 
26327c478bd9Sstevel@tonic-gate 		if (mp->map_pmap.pr_mapname[0] == '\0' ||
26337c478bd9Sstevel@tonic-gate 		    (fp = mp->map_file) == NULL || fp->file_lname == NULL)
26347c478bd9Sstevel@tonic-gate 			continue;
26357c478bd9Sstevel@tonic-gate 
26367c478bd9Sstevel@tonic-gate 		if (lmid != PR_LMID_EVERY &&
26377c478bd9Sstevel@tonic-gate 		    (fp->file_lo == NULL || lmid != fp->file_lo->rl_lmident))
26387c478bd9Sstevel@tonic-gate 			continue;
26397c478bd9Sstevel@tonic-gate 
26407c478bd9Sstevel@tonic-gate 		/*
26417c478bd9Sstevel@tonic-gate 		 * If we match, return the primary text mapping; otherwise
26427c478bd9Sstevel@tonic-gate 		 * just return the mapping we matched.
26437c478bd9Sstevel@tonic-gate 		 */
26447c478bd9Sstevel@tonic-gate 		if (strncmp(fp->file_lbase, objname, objlen) == 0 &&
26457c478bd9Sstevel@tonic-gate 		    fp->file_lbase[objlen] == '.')
26467c478bd9Sstevel@tonic-gate 			return (fp->file_map ? fp->file_map : mp);
26477c478bd9Sstevel@tonic-gate 	}
26487c478bd9Sstevel@tonic-gate 
26497c478bd9Sstevel@tonic-gate 	/*
26507c478bd9Sstevel@tonic-gate 	 * One last check: we allow "a.out" to always alias the executable,
26517c478bd9Sstevel@tonic-gate 	 * assuming this name was not in use for something else.
26527c478bd9Sstevel@tonic-gate 	 */
26537c478bd9Sstevel@tonic-gate 	if ((lmid == PR_LMID_EVERY || lmid == LM_ID_BASE) &&
26547c478bd9Sstevel@tonic-gate 	    (strcmp(objname, "a.out") == 0))
26557c478bd9Sstevel@tonic-gate 		return (P->map_exec);
26567c478bd9Sstevel@tonic-gate 
26577c478bd9Sstevel@tonic-gate 	return (NULL);
26587c478bd9Sstevel@tonic-gate }
26597c478bd9Sstevel@tonic-gate 
26607c478bd9Sstevel@tonic-gate static map_info_t *
26617c478bd9Sstevel@tonic-gate object_name_to_map(struct ps_prochandle *P, Lmid_t lmid, const char *name)
26627c478bd9Sstevel@tonic-gate {
26637c478bd9Sstevel@tonic-gate 	map_info_t *mptr;
26647c478bd9Sstevel@tonic-gate 
26657c478bd9Sstevel@tonic-gate 	if (!P->info_valid)
26667c478bd9Sstevel@tonic-gate 		Pupdate_maps(P);
26677c478bd9Sstevel@tonic-gate 
26687c478bd9Sstevel@tonic-gate 	if (P->map_exec == NULL && ((mptr = Paddr2mptr(P,
26697c478bd9Sstevel@tonic-gate 	    Pgetauxval(P, AT_ENTRY))) != NULL || (mptr = exec_map(P)) != NULL))
26707c478bd9Sstevel@tonic-gate 		P->map_exec = mptr;
26717c478bd9Sstevel@tonic-gate 
26727c478bd9Sstevel@tonic-gate 	if (P->map_ldso == NULL && (mptr = Paddr2mptr(P,
26737c478bd9Sstevel@tonic-gate 	    Pgetauxval(P, AT_BASE))) != NULL)
26747c478bd9Sstevel@tonic-gate 		P->map_ldso = mptr;
26757c478bd9Sstevel@tonic-gate 
26767c478bd9Sstevel@tonic-gate 	if (name == PR_OBJ_EXEC)
26777c478bd9Sstevel@tonic-gate 		mptr = P->map_exec;
26787c478bd9Sstevel@tonic-gate 	else if (name == PR_OBJ_LDSO)
26797c478bd9Sstevel@tonic-gate 		mptr = P->map_ldso;
26807c478bd9Sstevel@tonic-gate 	else
2681*9acbbeafSnn35248 		mptr = object_to_map(P, lmid, name);
26827c478bd9Sstevel@tonic-gate 
26837c478bd9Sstevel@tonic-gate 	return (mptr);
26847c478bd9Sstevel@tonic-gate }
26857c478bd9Sstevel@tonic-gate 
26867c478bd9Sstevel@tonic-gate /*
26877c478bd9Sstevel@tonic-gate  * When two symbols are found by address, decide which one is to be preferred.
26887c478bd9Sstevel@tonic-gate  */
26897c478bd9Sstevel@tonic-gate static GElf_Sym *
26907c478bd9Sstevel@tonic-gate sym_prefer(GElf_Sym *sym1, char *name1, GElf_Sym *sym2, char *name2)
26917c478bd9Sstevel@tonic-gate {
26927c478bd9Sstevel@tonic-gate 	/*
26937c478bd9Sstevel@tonic-gate 	 * Prefer the non-NULL symbol.
26947c478bd9Sstevel@tonic-gate 	 */
26957c478bd9Sstevel@tonic-gate 	if (sym1 == NULL)
26967c478bd9Sstevel@tonic-gate 		return (sym2);
26977c478bd9Sstevel@tonic-gate 	if (sym2 == NULL)
26987c478bd9Sstevel@tonic-gate 		return (sym1);
26997c478bd9Sstevel@tonic-gate 
27007c478bd9Sstevel@tonic-gate 	/*
27017c478bd9Sstevel@tonic-gate 	 * Defer to the sort ordering...
27027c478bd9Sstevel@tonic-gate 	 */
27037c478bd9Sstevel@tonic-gate 	return (byaddr_cmp_common(sym1, name1, sym2, name2) <= 0 ? sym1 : sym2);
27047c478bd9Sstevel@tonic-gate }
27057c478bd9Sstevel@tonic-gate 
27067c478bd9Sstevel@tonic-gate /*
27077c478bd9Sstevel@tonic-gate  * Look up a symbol by address in the specified symbol table.
27087c478bd9Sstevel@tonic-gate  * Adjustment to 'addr' must already have been made for the
27097c478bd9Sstevel@tonic-gate  * offset of the symbol if this is a dynamic library symbol table.
27107c478bd9Sstevel@tonic-gate  */
27117c478bd9Sstevel@tonic-gate static GElf_Sym *
27127c478bd9Sstevel@tonic-gate sym_by_addr(sym_tbl_t *symtab, GElf_Addr addr, GElf_Sym *symp, uint_t *idp)
27137c478bd9Sstevel@tonic-gate {
27147c478bd9Sstevel@tonic-gate 	Elf_Data *data = symtab->sym_data;
27157c478bd9Sstevel@tonic-gate 	GElf_Sym sym, osym;
27167c478bd9Sstevel@tonic-gate 	uint_t i, oid, *byaddr = symtab->sym_byaddr;
27177c478bd9Sstevel@tonic-gate 	int min, max, mid, omid, found = 0;
27187c478bd9Sstevel@tonic-gate 
27197c478bd9Sstevel@tonic-gate 	if (data == NULL)
27207c478bd9Sstevel@tonic-gate 		return (NULL);
27217c478bd9Sstevel@tonic-gate 
27227c478bd9Sstevel@tonic-gate 	min = 0;
27237c478bd9Sstevel@tonic-gate 	max = symtab->sym_count - 1;
27247c478bd9Sstevel@tonic-gate 	osym.st_value = 0;
27257c478bd9Sstevel@tonic-gate 
27267c478bd9Sstevel@tonic-gate 	/*
27277c478bd9Sstevel@tonic-gate 	 * We can't return when we've found a match, we have to continue
27287c478bd9Sstevel@tonic-gate 	 * searching for the closest matching symbol.
27297c478bd9Sstevel@tonic-gate 	 */
27307c478bd9Sstevel@tonic-gate 	while (min <= max) {
27317c478bd9Sstevel@tonic-gate 		mid = (max + min) / 2;
27327c478bd9Sstevel@tonic-gate 
27337c478bd9Sstevel@tonic-gate 		i = byaddr[mid];
27347c478bd9Sstevel@tonic-gate 		(void) gelf_getsym(data, i, &sym);
27357c478bd9Sstevel@tonic-gate 
27367c478bd9Sstevel@tonic-gate 		if (addr >= sym.st_value &&
27377c478bd9Sstevel@tonic-gate 		    addr < sym.st_value + sym.st_size &&
27387c478bd9Sstevel@tonic-gate 		    (!found || sym.st_value > osym.st_value)) {
27397c478bd9Sstevel@tonic-gate 			osym = sym;
27407c478bd9Sstevel@tonic-gate 			omid = mid;
27417c478bd9Sstevel@tonic-gate 			oid = i;
27427c478bd9Sstevel@tonic-gate 			found = 1;
27437c478bd9Sstevel@tonic-gate 		}
27447c478bd9Sstevel@tonic-gate 
27457c478bd9Sstevel@tonic-gate 		if (addr < sym.st_value)
27467c478bd9Sstevel@tonic-gate 			max = mid - 1;
27477c478bd9Sstevel@tonic-gate 		else
27487c478bd9Sstevel@tonic-gate 			min = mid + 1;
27497c478bd9Sstevel@tonic-gate 	}
27507c478bd9Sstevel@tonic-gate 
27517c478bd9Sstevel@tonic-gate 	if (!found)
27527c478bd9Sstevel@tonic-gate 		return (NULL);
27537c478bd9Sstevel@tonic-gate 
27547c478bd9Sstevel@tonic-gate 	/*
27557c478bd9Sstevel@tonic-gate 	 * There may be many symbols with identical values so we walk
27567c478bd9Sstevel@tonic-gate 	 * backward in the byaddr table to find the best match.
27577c478bd9Sstevel@tonic-gate 	 */
27587c478bd9Sstevel@tonic-gate 	do {
27597c478bd9Sstevel@tonic-gate 		sym = osym;
27607c478bd9Sstevel@tonic-gate 		i = oid;
27617c478bd9Sstevel@tonic-gate 
27627c478bd9Sstevel@tonic-gate 		if (omid == 0)
27637c478bd9Sstevel@tonic-gate 			break;
27647c478bd9Sstevel@tonic-gate 
27657c478bd9Sstevel@tonic-gate 		oid = byaddr[--omid];
27667c478bd9Sstevel@tonic-gate 		(void) gelf_getsym(data, oid, &osym);
27677c478bd9Sstevel@tonic-gate 	} while (addr >= osym.st_value &&
27687c478bd9Sstevel@tonic-gate 	    addr < sym.st_value + osym.st_size &&
27697c478bd9Sstevel@tonic-gate 	    osym.st_value == sym.st_value);
27707c478bd9Sstevel@tonic-gate 
27717c478bd9Sstevel@tonic-gate 	*symp = sym;
27727c478bd9Sstevel@tonic-gate 	if (idp != NULL)
27737c478bd9Sstevel@tonic-gate 		*idp = i;
27747c478bd9Sstevel@tonic-gate 	return (symp);
27757c478bd9Sstevel@tonic-gate }
27767c478bd9Sstevel@tonic-gate 
27777c478bd9Sstevel@tonic-gate /*
27787c478bd9Sstevel@tonic-gate  * Look up a symbol by name in the specified symbol table.
27797c478bd9Sstevel@tonic-gate  */
27807c478bd9Sstevel@tonic-gate static GElf_Sym *
27817c478bd9Sstevel@tonic-gate sym_by_name(sym_tbl_t *symtab, const char *name, GElf_Sym *symp, uint_t *idp)
27827c478bd9Sstevel@tonic-gate {
27837c478bd9Sstevel@tonic-gate 	Elf_Data *data = symtab->sym_data;
27847c478bd9Sstevel@tonic-gate 	char *strs = symtab->sym_strs;
27857c478bd9Sstevel@tonic-gate 	uint_t i, *byname = symtab->sym_byname;
27867c478bd9Sstevel@tonic-gate 	int min, mid, max, cmp;
27877c478bd9Sstevel@tonic-gate 
27887c478bd9Sstevel@tonic-gate 	if (data == NULL || strs == NULL)
27897c478bd9Sstevel@tonic-gate 		return (NULL);
27907c478bd9Sstevel@tonic-gate 
27917c478bd9Sstevel@tonic-gate 	min = 0;
27927c478bd9Sstevel@tonic-gate 	max = symtab->sym_count - 1;
27937c478bd9Sstevel@tonic-gate 
27947c478bd9Sstevel@tonic-gate 	while (min <= max) {
27957c478bd9Sstevel@tonic-gate 		mid = (max + min) / 2;
27967c478bd9Sstevel@tonic-gate 
27977c478bd9Sstevel@tonic-gate 		i = byname[mid];
27987c478bd9Sstevel@tonic-gate 		(void) gelf_getsym(data, i, symp);
27997c478bd9Sstevel@tonic-gate 
28007c478bd9Sstevel@tonic-gate 		if ((cmp = strcmp(name, strs + symp->st_name)) == 0) {
28017c478bd9Sstevel@tonic-gate 			if (idp != NULL)
28027c478bd9Sstevel@tonic-gate 				*idp = i;
28037c478bd9Sstevel@tonic-gate 			return (symp);
28047c478bd9Sstevel@tonic-gate 		}
28057c478bd9Sstevel@tonic-gate 
28067c478bd9Sstevel@tonic-gate 		if (cmp < 0)
28077c478bd9Sstevel@tonic-gate 			max = mid - 1;
28087c478bd9Sstevel@tonic-gate 		else
28097c478bd9Sstevel@tonic-gate 			min = mid + 1;
28107c478bd9Sstevel@tonic-gate 	}
28117c478bd9Sstevel@tonic-gate 
28127c478bd9Sstevel@tonic-gate 	return (NULL);
28137c478bd9Sstevel@tonic-gate }
28147c478bd9Sstevel@tonic-gate 
28157c478bd9Sstevel@tonic-gate /*
28167c478bd9Sstevel@tonic-gate  * Search the process symbol tables looking for a symbol whose
28177c478bd9Sstevel@tonic-gate  * value to value+size contain the address specified by addr.
28187c478bd9Sstevel@tonic-gate  * Return values are:
28197c478bd9Sstevel@tonic-gate  *	sym_name_buffer containing the symbol name
28207c478bd9Sstevel@tonic-gate  *	GElf_Sym symbol table entry
28217c478bd9Sstevel@tonic-gate  *	prsyminfo_t ancillary symbol information
28227c478bd9Sstevel@tonic-gate  * Returns 0 on success, -1 on failure.
28237c478bd9Sstevel@tonic-gate  */
28247c478bd9Sstevel@tonic-gate int
28257c478bd9Sstevel@tonic-gate Pxlookup_by_addr(
28267c478bd9Sstevel@tonic-gate 	struct ps_prochandle *P,
28277c478bd9Sstevel@tonic-gate 	uintptr_t addr,			/* process address being sought */
28287c478bd9Sstevel@tonic-gate 	char *sym_name_buffer,		/* buffer for the symbol name */
28297c478bd9Sstevel@tonic-gate 	size_t bufsize,			/* size of sym_name_buffer */
28307c478bd9Sstevel@tonic-gate 	GElf_Sym *symbolp,		/* returned symbol table entry */
28317c478bd9Sstevel@tonic-gate 	prsyminfo_t *sip)		/* returned symbol info */
28327c478bd9Sstevel@tonic-gate {
28337c478bd9Sstevel@tonic-gate 	GElf_Sym	*symp;
28347c478bd9Sstevel@tonic-gate 	char		*name;
28357c478bd9Sstevel@tonic-gate 	GElf_Sym	sym1, *sym1p = NULL;
28367c478bd9Sstevel@tonic-gate 	GElf_Sym	sym2, *sym2p = NULL;
28377c478bd9Sstevel@tonic-gate 	char		*name1 = NULL;
28387c478bd9Sstevel@tonic-gate 	char		*name2 = NULL;
28397c478bd9Sstevel@tonic-gate 	uint_t		i1;
28407c478bd9Sstevel@tonic-gate 	uint_t		i2;
28417c478bd9Sstevel@tonic-gate 	map_info_t	*mptr;
28427c478bd9Sstevel@tonic-gate 	file_info_t	*fptr;
28437c478bd9Sstevel@tonic-gate 
28447c478bd9Sstevel@tonic-gate 	(void) Prd_agent(P);
28457c478bd9Sstevel@tonic-gate 
28467c478bd9Sstevel@tonic-gate 	if ((mptr = Paddr2mptr(P, addr)) == NULL ||	/* no such address */
28477c478bd9Sstevel@tonic-gate 	    (fptr = build_map_symtab(P, mptr)) == NULL || /* no mapped file */
28487c478bd9Sstevel@tonic-gate 	    fptr->file_elf == NULL)			/* not an ELF file */
28497c478bd9Sstevel@tonic-gate 		return (-1);
28507c478bd9Sstevel@tonic-gate 
28517c478bd9Sstevel@tonic-gate 	/*
28527c478bd9Sstevel@tonic-gate 	 * Adjust the address by the load object base address in
28537c478bd9Sstevel@tonic-gate 	 * case the address turns out to be in a shared library.
28547c478bd9Sstevel@tonic-gate 	 */
28557c478bd9Sstevel@tonic-gate 	addr -= fptr->file_dyn_base;
28567c478bd9Sstevel@tonic-gate 
28577c478bd9Sstevel@tonic-gate 	/*
28587c478bd9Sstevel@tonic-gate 	 * Search both symbol tables, symtab first, then dynsym.
28597c478bd9Sstevel@tonic-gate 	 */
28607c478bd9Sstevel@tonic-gate 	if ((sym1p = sym_by_addr(&fptr->file_symtab, addr, &sym1, &i1)) != NULL)
28617c478bd9Sstevel@tonic-gate 		name1 = fptr->file_symtab.sym_strs + sym1.st_name;
28627c478bd9Sstevel@tonic-gate 	if ((sym2p = sym_by_addr(&fptr->file_dynsym, addr, &sym2, &i2)) != NULL)
28637c478bd9Sstevel@tonic-gate 		name2 = fptr->file_dynsym.sym_strs + sym2.st_name;
28647c478bd9Sstevel@tonic-gate 
28657c478bd9Sstevel@tonic-gate 	if ((symp = sym_prefer(sym1p, name1, sym2p, name2)) == NULL)
28667c478bd9Sstevel@tonic-gate 		return (-1);
28677c478bd9Sstevel@tonic-gate 
28687c478bd9Sstevel@tonic-gate 	name = (symp == sym1p) ? name1 : name2;
28697c478bd9Sstevel@tonic-gate 	if (bufsize > 0) {
28707c478bd9Sstevel@tonic-gate 		(void) strncpy(sym_name_buffer, name, bufsize);
28717c478bd9Sstevel@tonic-gate 		sym_name_buffer[bufsize - 1] = '\0';
28727c478bd9Sstevel@tonic-gate 	}
28737c478bd9Sstevel@tonic-gate 
28747c478bd9Sstevel@tonic-gate 	*symbolp = *symp;
28757c478bd9Sstevel@tonic-gate 	if (sip != NULL) {
28767c478bd9Sstevel@tonic-gate 		sip->prs_name = bufsize == 0 ? NULL : sym_name_buffer;
28777c478bd9Sstevel@tonic-gate 		sip->prs_object = fptr->file_lbase;
28787c478bd9Sstevel@tonic-gate 		sip->prs_id = (symp == sym1p) ? i1 : i2;
28797c478bd9Sstevel@tonic-gate 		sip->prs_table = (symp == sym1p) ? PR_SYMTAB : PR_DYNSYM;
28807c478bd9Sstevel@tonic-gate 		sip->prs_lmid = (fptr->file_lo == NULL) ? LM_ID_BASE :
28817c478bd9Sstevel@tonic-gate 		    fptr->file_lo->rl_lmident;
28827c478bd9Sstevel@tonic-gate 	}
28837c478bd9Sstevel@tonic-gate 
28847c478bd9Sstevel@tonic-gate 	if (GELF_ST_TYPE(symbolp->st_info) != STT_TLS)
28857c478bd9Sstevel@tonic-gate 		symbolp->st_value += fptr->file_dyn_base;
28867c478bd9Sstevel@tonic-gate 
28877c478bd9Sstevel@tonic-gate 	return (0);
28887c478bd9Sstevel@tonic-gate }
28897c478bd9Sstevel@tonic-gate 
28907c478bd9Sstevel@tonic-gate int
28917c478bd9Sstevel@tonic-gate Plookup_by_addr(struct ps_prochandle *P, uintptr_t addr, char *buf, size_t size,
28927c478bd9Sstevel@tonic-gate     GElf_Sym *symp)
28937c478bd9Sstevel@tonic-gate {
28947c478bd9Sstevel@tonic-gate 	return (Pxlookup_by_addr(P, addr, buf, size, symp, NULL));
28957c478bd9Sstevel@tonic-gate }
28967c478bd9Sstevel@tonic-gate 
28977c478bd9Sstevel@tonic-gate /*
28987c478bd9Sstevel@tonic-gate  * Search the process symbol tables looking for a symbol whose name matches the
28997c478bd9Sstevel@tonic-gate  * specified name and whose object and link map optionally match the specified
29007c478bd9Sstevel@tonic-gate  * parameters.  On success, the function returns 0 and fills in the GElf_Sym
29017c478bd9Sstevel@tonic-gate  * symbol table entry.  On failure, -1 is returned.
29027c478bd9Sstevel@tonic-gate  */
29037c478bd9Sstevel@tonic-gate int
29047c478bd9Sstevel@tonic-gate Pxlookup_by_name(
29057c478bd9Sstevel@tonic-gate 	struct ps_prochandle *P,
29067c478bd9Sstevel@tonic-gate 	Lmid_t lmid,			/* link map to match, or -1 for any */
29077c478bd9Sstevel@tonic-gate 	const char *oname,		/* load object name */
29087c478bd9Sstevel@tonic-gate 	const char *sname,		/* symbol name */
29097c478bd9Sstevel@tonic-gate 	GElf_Sym *symp,			/* returned symbol table entry */
29107c478bd9Sstevel@tonic-gate 	prsyminfo_t *sip)		/* returned symbol info */
29117c478bd9Sstevel@tonic-gate {
29127c478bd9Sstevel@tonic-gate 	map_info_t *mptr;
29137c478bd9Sstevel@tonic-gate 	file_info_t *fptr;
29147c478bd9Sstevel@tonic-gate 	int cnt;
29157c478bd9Sstevel@tonic-gate 
29167c478bd9Sstevel@tonic-gate 	GElf_Sym sym;
29177c478bd9Sstevel@tonic-gate 	prsyminfo_t si;
29187c478bd9Sstevel@tonic-gate 	int rv = -1;
29197c478bd9Sstevel@tonic-gate 	uint_t id;
29207c478bd9Sstevel@tonic-gate 
29217c478bd9Sstevel@tonic-gate 	if (oname == PR_OBJ_EVERY) {
29227c478bd9Sstevel@tonic-gate 		/* create all the file_info_t's for all the mappings */
29237c478bd9Sstevel@tonic-gate 		(void) Prd_agent(P);
29247c478bd9Sstevel@tonic-gate 		cnt = P->num_files;
29257c478bd9Sstevel@tonic-gate 		fptr = list_next(&P->file_head);
29267c478bd9Sstevel@tonic-gate 	} else {
29277c478bd9Sstevel@tonic-gate 		cnt = 1;
29287c478bd9Sstevel@tonic-gate 		if ((mptr = object_name_to_map(P, lmid, oname)) == NULL ||
29297c478bd9Sstevel@tonic-gate 		    (fptr = build_map_symtab(P, mptr)) == NULL)
29307c478bd9Sstevel@tonic-gate 			return (-1);
29317c478bd9Sstevel@tonic-gate 	}
29327c478bd9Sstevel@tonic-gate 
29337c478bd9Sstevel@tonic-gate 	/*
29347c478bd9Sstevel@tonic-gate 	 * Iterate through the loaded object files and look for the symbol
29357c478bd9Sstevel@tonic-gate 	 * name in the .symtab and .dynsym of each.  If we encounter a match
29367c478bd9Sstevel@tonic-gate 	 * with SHN_UNDEF, keep looking in hopes of finding a better match.
29377c478bd9Sstevel@tonic-gate 	 * This means that a name such as "puts" will match the puts function
29387c478bd9Sstevel@tonic-gate 	 * in libc instead of matching the puts PLT entry in the a.out file.
29397c478bd9Sstevel@tonic-gate 	 */
29407c478bd9Sstevel@tonic-gate 	for (; cnt > 0; cnt--, fptr = list_next(fptr)) {
29417c478bd9Sstevel@tonic-gate 		Pbuild_file_symtab(P, fptr);
29427c478bd9Sstevel@tonic-gate 
29437c478bd9Sstevel@tonic-gate 		if (fptr->file_elf == NULL)
29447c478bd9Sstevel@tonic-gate 			continue;
29457c478bd9Sstevel@tonic-gate 
29467c478bd9Sstevel@tonic-gate 		if (lmid != PR_LMID_EVERY && fptr->file_lo != NULL &&
29477c478bd9Sstevel@tonic-gate 		    lmid != fptr->file_lo->rl_lmident)
29487c478bd9Sstevel@tonic-gate 			continue;
29497c478bd9Sstevel@tonic-gate 
29507c478bd9Sstevel@tonic-gate 		if (fptr->file_symtab.sym_data != NULL &&
29517c478bd9Sstevel@tonic-gate 		    sym_by_name(&fptr->file_symtab, sname, symp, &id)) {
29527c478bd9Sstevel@tonic-gate 			if (sip != NULL) {
29537c478bd9Sstevel@tonic-gate 				sip->prs_id = id;
29547c478bd9Sstevel@tonic-gate 				sip->prs_table = PR_SYMTAB;
29557c478bd9Sstevel@tonic-gate 				sip->prs_object = oname;
29567c478bd9Sstevel@tonic-gate 				sip->prs_name = sname;
29577c478bd9Sstevel@tonic-gate 				sip->prs_lmid = fptr->file_lo == NULL ?
29587c478bd9Sstevel@tonic-gate 				    LM_ID_BASE : fptr->file_lo->rl_lmident;
29597c478bd9Sstevel@tonic-gate 			}
29607c478bd9Sstevel@tonic-gate 		} else if (fptr->file_dynsym.sym_data != NULL &&
29617c478bd9Sstevel@tonic-gate 		    sym_by_name(&fptr->file_dynsym, sname, symp, &id)) {
29627c478bd9Sstevel@tonic-gate 			if (sip != NULL) {
29637c478bd9Sstevel@tonic-gate 				sip->prs_id = id;
29647c478bd9Sstevel@tonic-gate 				sip->prs_table = PR_DYNSYM;
29657c478bd9Sstevel@tonic-gate 				sip->prs_object = oname;
29667c478bd9Sstevel@tonic-gate 				sip->prs_name = sname;
29677c478bd9Sstevel@tonic-gate 				sip->prs_lmid = fptr->file_lo == NULL ?
29687c478bd9Sstevel@tonic-gate 				    LM_ID_BASE : fptr->file_lo->rl_lmident;
29697c478bd9Sstevel@tonic-gate 			}
29707c478bd9Sstevel@tonic-gate 		} else {
29717c478bd9Sstevel@tonic-gate 			continue;
29727c478bd9Sstevel@tonic-gate 		}
29737c478bd9Sstevel@tonic-gate 
29747c478bd9Sstevel@tonic-gate 		if (GELF_ST_TYPE(symp->st_info) != STT_TLS)
29757c478bd9Sstevel@tonic-gate 			symp->st_value += fptr->file_dyn_base;
29767c478bd9Sstevel@tonic-gate 
29777c478bd9Sstevel@tonic-gate 		if (symp->st_shndx != SHN_UNDEF)
29787c478bd9Sstevel@tonic-gate 			return (0);
29797c478bd9Sstevel@tonic-gate 
29807c478bd9Sstevel@tonic-gate 		if (rv != 0) {
29817c478bd9Sstevel@tonic-gate 			if (sip != NULL)
29827c478bd9Sstevel@tonic-gate 				si = *sip;
29837c478bd9Sstevel@tonic-gate 			sym = *symp;
29847c478bd9Sstevel@tonic-gate 			rv = 0;
29857c478bd9Sstevel@tonic-gate 		}
29867c478bd9Sstevel@tonic-gate 	}
29877c478bd9Sstevel@tonic-gate 
29887c478bd9Sstevel@tonic-gate 	if (rv == 0) {
29897c478bd9Sstevel@tonic-gate 		if (sip != NULL)
29907c478bd9Sstevel@tonic-gate 			*sip = si;
29917c478bd9Sstevel@tonic-gate 		*symp = sym;
29927c478bd9Sstevel@tonic-gate 	}
29937c478bd9Sstevel@tonic-gate 
29947c478bd9Sstevel@tonic-gate 	return (rv);
29957c478bd9Sstevel@tonic-gate }
29967c478bd9Sstevel@tonic-gate 
29977c478bd9Sstevel@tonic-gate /*
29987c478bd9Sstevel@tonic-gate  * Search the process symbol tables looking for a symbol whose name matches the
29997c478bd9Sstevel@tonic-gate  * specified name, but without any restriction on the link map id.
30007c478bd9Sstevel@tonic-gate  */
30017c478bd9Sstevel@tonic-gate int
30027c478bd9Sstevel@tonic-gate Plookup_by_name(struct ps_prochandle *P, const char *object,
30037c478bd9Sstevel@tonic-gate 	const char *symbol, GElf_Sym *symp)
30047c478bd9Sstevel@tonic-gate {
30057c478bd9Sstevel@tonic-gate 	return (Pxlookup_by_name(P, PR_LMID_EVERY, object, symbol, symp, NULL));
30067c478bd9Sstevel@tonic-gate }
30077c478bd9Sstevel@tonic-gate 
30087c478bd9Sstevel@tonic-gate /*
30097c478bd9Sstevel@tonic-gate  * Iterate over the process's address space mappings.
30107c478bd9Sstevel@tonic-gate  */
30117c478bd9Sstevel@tonic-gate int
30127c478bd9Sstevel@tonic-gate Pmapping_iter(struct ps_prochandle *P, proc_map_f *func, void *cd)
30137c478bd9Sstevel@tonic-gate {
30147c478bd9Sstevel@tonic-gate 	map_info_t *mptr;
30157c478bd9Sstevel@tonic-gate 	file_info_t *fptr;
30167c478bd9Sstevel@tonic-gate 	char *object_name;
30177c478bd9Sstevel@tonic-gate 	int rc = 0;
30187c478bd9Sstevel@tonic-gate 	int i;
30197c478bd9Sstevel@tonic-gate 
30207c478bd9Sstevel@tonic-gate 	/* create all the file_info_t's for all the mappings */
30217c478bd9Sstevel@tonic-gate 	(void) Prd_agent(P);
30227c478bd9Sstevel@tonic-gate 
30237c478bd9Sstevel@tonic-gate 	for (i = 0, mptr = P->mappings; i < P->map_count; i++, mptr++) {
30247c478bd9Sstevel@tonic-gate 		if ((fptr = mptr->map_file) == NULL)
30257c478bd9Sstevel@tonic-gate 			object_name = NULL;
30267c478bd9Sstevel@tonic-gate 		else
30277c478bd9Sstevel@tonic-gate 			object_name = fptr->file_lname;
30287c478bd9Sstevel@tonic-gate 		if ((rc = func(cd, &mptr->map_pmap, object_name)) != 0)
30297c478bd9Sstevel@tonic-gate 			return (rc);
30307c478bd9Sstevel@tonic-gate 	}
30317c478bd9Sstevel@tonic-gate 	return (0);
30327c478bd9Sstevel@tonic-gate }
30337c478bd9Sstevel@tonic-gate 
30347c478bd9Sstevel@tonic-gate /*
30357c478bd9Sstevel@tonic-gate  * Iterate over the process's mapped objects.
30367c478bd9Sstevel@tonic-gate  */
30377c478bd9Sstevel@tonic-gate int
30387c478bd9Sstevel@tonic-gate Pobject_iter(struct ps_prochandle *P, proc_map_f *func, void *cd)
30397c478bd9Sstevel@tonic-gate {
30407c478bd9Sstevel@tonic-gate 	map_info_t *mptr;
30417c478bd9Sstevel@tonic-gate 	file_info_t *fptr;
30427c478bd9Sstevel@tonic-gate 	uint_t cnt;
30437c478bd9Sstevel@tonic-gate 	int rc = 0;
30447c478bd9Sstevel@tonic-gate 
30457c478bd9Sstevel@tonic-gate 	(void) Prd_agent(P); /* create file_info_t's for all the mappings */
30467c478bd9Sstevel@tonic-gate 	Pupdate_maps(P);
30477c478bd9Sstevel@tonic-gate 
30487c478bd9Sstevel@tonic-gate 	for (cnt = P->num_files, fptr = list_next(&P->file_head);
30497c478bd9Sstevel@tonic-gate 	    cnt; cnt--, fptr = list_next(fptr)) {
30507c478bd9Sstevel@tonic-gate 
30517c478bd9Sstevel@tonic-gate 		const char *lname = fptr->file_lname ? fptr->file_lname : "";
30527c478bd9Sstevel@tonic-gate 
30537c478bd9Sstevel@tonic-gate 		if ((mptr = fptr->file_map) == NULL)
30547c478bd9Sstevel@tonic-gate 			continue;
30557c478bd9Sstevel@tonic-gate 
30567c478bd9Sstevel@tonic-gate 		if ((rc = func(cd, &mptr->map_pmap, lname)) != 0)
30577c478bd9Sstevel@tonic-gate 			return (rc);
30587c478bd9Sstevel@tonic-gate 	}
30597c478bd9Sstevel@tonic-gate 	return (0);
30607c478bd9Sstevel@tonic-gate }
30617c478bd9Sstevel@tonic-gate 
30627c478bd9Sstevel@tonic-gate /*
30637c478bd9Sstevel@tonic-gate  * Given a virtual address, return the name of the underlying
30647c478bd9Sstevel@tonic-gate  * mapped object (file), as provided by the dynamic linker.
30657c478bd9Sstevel@tonic-gate  * Return NULL on failure (no underlying shared library).
30667c478bd9Sstevel@tonic-gate  */
30677c478bd9Sstevel@tonic-gate char *
30687c478bd9Sstevel@tonic-gate Pobjname(struct ps_prochandle *P, uintptr_t addr,
30697c478bd9Sstevel@tonic-gate 	char *buffer, size_t bufsize)
30707c478bd9Sstevel@tonic-gate {
30717c478bd9Sstevel@tonic-gate 	map_info_t *mptr;
30727c478bd9Sstevel@tonic-gate 	file_info_t *fptr;
30737c478bd9Sstevel@tonic-gate 
30747c478bd9Sstevel@tonic-gate 	/* create all the file_info_t's for all the mappings */
30757c478bd9Sstevel@tonic-gate 	(void) Prd_agent(P);
30767c478bd9Sstevel@tonic-gate 
30777c478bd9Sstevel@tonic-gate 	if ((mptr = Paddr2mptr(P, addr)) != NULL &&
30787c478bd9Sstevel@tonic-gate 	    (fptr = mptr->map_file) != NULL &&
30797c478bd9Sstevel@tonic-gate 	    fptr->file_lname != NULL) {
30807c478bd9Sstevel@tonic-gate 		(void) strncpy(buffer, fptr->file_lname, bufsize);
30817c478bd9Sstevel@tonic-gate 		if (strlen(fptr->file_lname) >= bufsize)
30827c478bd9Sstevel@tonic-gate 			buffer[bufsize-1] = '\0';
30837c478bd9Sstevel@tonic-gate 		return (buffer);
30847c478bd9Sstevel@tonic-gate 	}
30857c478bd9Sstevel@tonic-gate 	return (NULL);
30867c478bd9Sstevel@tonic-gate }
30877c478bd9Sstevel@tonic-gate 
30887c478bd9Sstevel@tonic-gate /*
30897c478bd9Sstevel@tonic-gate  * Given a virtual address, return the link map id of the underlying mapped
30907c478bd9Sstevel@tonic-gate  * object (file), as provided by the dynamic linker.  Return -1 on failure.
30917c478bd9Sstevel@tonic-gate  */
30927c478bd9Sstevel@tonic-gate int
30937c478bd9Sstevel@tonic-gate Plmid(struct ps_prochandle *P, uintptr_t addr, Lmid_t *lmidp)
30947c478bd9Sstevel@tonic-gate {
30957c478bd9Sstevel@tonic-gate 	map_info_t *mptr;
30967c478bd9Sstevel@tonic-gate 	file_info_t *fptr;
30977c478bd9Sstevel@tonic-gate 
30987c478bd9Sstevel@tonic-gate 	/* create all the file_info_t's for all the mappings */
30997c478bd9Sstevel@tonic-gate 	(void) Prd_agent(P);
31007c478bd9Sstevel@tonic-gate 
31017c478bd9Sstevel@tonic-gate 	if ((mptr = Paddr2mptr(P, addr)) != NULL &&
31027c478bd9Sstevel@tonic-gate 	    (fptr = mptr->map_file) != NULL && fptr->file_lo != NULL) {
31037c478bd9Sstevel@tonic-gate 		*lmidp = fptr->file_lo->rl_lmident;
31047c478bd9Sstevel@tonic-gate 		return (0);
31057c478bd9Sstevel@tonic-gate 	}
31067c478bd9Sstevel@tonic-gate 
31077c478bd9Sstevel@tonic-gate 	return (-1);
31087c478bd9Sstevel@tonic-gate }
31097c478bd9Sstevel@tonic-gate 
31107c478bd9Sstevel@tonic-gate /*
31117c478bd9Sstevel@tonic-gate  * Given an object name and optional lmid, iterate over the object's symbols.
31127c478bd9Sstevel@tonic-gate  * If which == PR_SYMTAB, search the normal symbol table.
31137c478bd9Sstevel@tonic-gate  * If which == PR_DYNSYM, search the dynamic symbol table.
31147c478bd9Sstevel@tonic-gate  */
31157c478bd9Sstevel@tonic-gate static int
31167c478bd9Sstevel@tonic-gate Psymbol_iter_com(struct ps_prochandle *P, Lmid_t lmid, const char *object_name,
31177c478bd9Sstevel@tonic-gate     int which, int mask, pr_order_t order, proc_xsym_f *func, void *cd)
31187c478bd9Sstevel@tonic-gate {
31197c478bd9Sstevel@tonic-gate 	GElf_Sym sym;
3120f957ecc1Svb160487 	GElf_Shdr shdr;
31217c478bd9Sstevel@tonic-gate 	map_info_t *mptr;
31227c478bd9Sstevel@tonic-gate 	file_info_t *fptr;
31237c478bd9Sstevel@tonic-gate 	sym_tbl_t *symtab;
31247c478bd9Sstevel@tonic-gate 	Elf_Data *data;
31257c478bd9Sstevel@tonic-gate 	size_t symn;
31267c478bd9Sstevel@tonic-gate 	const char *strs;
31277c478bd9Sstevel@tonic-gate 	size_t strsz;
31287c478bd9Sstevel@tonic-gate 	prsyminfo_t si;
31297c478bd9Sstevel@tonic-gate 	int rv;
31307c478bd9Sstevel@tonic-gate 	uint_t *map, i, count, ndx;
31317c478bd9Sstevel@tonic-gate 
31327c478bd9Sstevel@tonic-gate 	if ((mptr = object_name_to_map(P, lmid, object_name)) == NULL)
31337c478bd9Sstevel@tonic-gate 		return (-1);
31347c478bd9Sstevel@tonic-gate 
31357c478bd9Sstevel@tonic-gate 	if ((fptr = build_map_symtab(P, mptr)) == NULL || /* no mapped file */
31367c478bd9Sstevel@tonic-gate 	    fptr->file_elf == NULL)			/* not an ELF file */
31377c478bd9Sstevel@tonic-gate 		return (-1);
31387c478bd9Sstevel@tonic-gate 
31397c478bd9Sstevel@tonic-gate 	/*
31407c478bd9Sstevel@tonic-gate 	 * Search the specified symbol table.
31417c478bd9Sstevel@tonic-gate 	 */
31427c478bd9Sstevel@tonic-gate 	switch (which) {
31437c478bd9Sstevel@tonic-gate 	case PR_SYMTAB:
31447c478bd9Sstevel@tonic-gate 		symtab = &fptr->file_symtab;
31457c478bd9Sstevel@tonic-gate 		si.prs_table = PR_SYMTAB;
31467c478bd9Sstevel@tonic-gate 		break;
31477c478bd9Sstevel@tonic-gate 	case PR_DYNSYM:
31487c478bd9Sstevel@tonic-gate 		symtab = &fptr->file_dynsym;
31497c478bd9Sstevel@tonic-gate 		si.prs_table = PR_DYNSYM;
31507c478bd9Sstevel@tonic-gate 		break;
31517c478bd9Sstevel@tonic-gate 	default:
31527c478bd9Sstevel@tonic-gate 		return (-1);
31537c478bd9Sstevel@tonic-gate 	}
31547c478bd9Sstevel@tonic-gate 
31557c478bd9Sstevel@tonic-gate 	si.prs_object = object_name;
31567c478bd9Sstevel@tonic-gate 	si.prs_lmid = fptr->file_lo == NULL ?
31577c478bd9Sstevel@tonic-gate 	    LM_ID_BASE : fptr->file_lo->rl_lmident;
31587c478bd9Sstevel@tonic-gate 
31597c478bd9Sstevel@tonic-gate 	data = symtab->sym_data;
31607c478bd9Sstevel@tonic-gate 	symn = symtab->sym_symn;
31617c478bd9Sstevel@tonic-gate 	strs = symtab->sym_strs;
31627c478bd9Sstevel@tonic-gate 	strsz = symtab->sym_strsz;
31637c478bd9Sstevel@tonic-gate 
31647c478bd9Sstevel@tonic-gate 	if (data == NULL || strs == NULL)
31657c478bd9Sstevel@tonic-gate 		return (-1);
31667c478bd9Sstevel@tonic-gate 
31677c478bd9Sstevel@tonic-gate 	switch (order) {
31687c478bd9Sstevel@tonic-gate 	case PRO_NATURAL:
31697c478bd9Sstevel@tonic-gate 		map = NULL;
31707c478bd9Sstevel@tonic-gate 		count = symn;
31717c478bd9Sstevel@tonic-gate 		break;
31727c478bd9Sstevel@tonic-gate 	case PRO_BYNAME:
31737c478bd9Sstevel@tonic-gate 		map = symtab->sym_byname;
31747c478bd9Sstevel@tonic-gate 		count = symtab->sym_count;
31757c478bd9Sstevel@tonic-gate 		break;
31767c478bd9Sstevel@tonic-gate 	case PRO_BYADDR:
31777c478bd9Sstevel@tonic-gate 		map = symtab->sym_byaddr;
31787c478bd9Sstevel@tonic-gate 		count = symtab->sym_count;
31797c478bd9Sstevel@tonic-gate 		break;
31807c478bd9Sstevel@tonic-gate 	default:
31817c478bd9Sstevel@tonic-gate 		return (-1);
31827c478bd9Sstevel@tonic-gate 	}
31837c478bd9Sstevel@tonic-gate 
31847c478bd9Sstevel@tonic-gate 	rv = 0;
31857c478bd9Sstevel@tonic-gate 
31867c478bd9Sstevel@tonic-gate 	for (i = 0; i < count; i++) {
31877c478bd9Sstevel@tonic-gate 		ndx = map == NULL ? i : map[i];
31887c478bd9Sstevel@tonic-gate 		if (gelf_getsym(data, ndx, &sym) != NULL) {
31897c478bd9Sstevel@tonic-gate 			uint_t s_bind, s_type, type;
31907c478bd9Sstevel@tonic-gate 
31917c478bd9Sstevel@tonic-gate 			if (sym.st_name >= strsz)	/* invalid st_name */
31927c478bd9Sstevel@tonic-gate 				continue;
31937c478bd9Sstevel@tonic-gate 
31947c478bd9Sstevel@tonic-gate 			s_bind = GELF_ST_BIND(sym.st_info);
31957c478bd9Sstevel@tonic-gate 			s_type = GELF_ST_TYPE(sym.st_info);
31967c478bd9Sstevel@tonic-gate 
31977c478bd9Sstevel@tonic-gate 			/*
31987c478bd9Sstevel@tonic-gate 			 * In case you haven't already guessed, this relies on
31997c478bd9Sstevel@tonic-gate 			 * the bitmask used in <libproc.h> for encoding symbol
32007c478bd9Sstevel@tonic-gate 			 * type and binding matching the order of STB and STT
32017c478bd9Sstevel@tonic-gate 			 * constants in <sys/elf.h>.  ELF can't change without
32027c478bd9Sstevel@tonic-gate 			 * breaking binary compatibility, so I think this is
32037c478bd9Sstevel@tonic-gate 			 * reasonably fair game.
32047c478bd9Sstevel@tonic-gate 			 */
32057c478bd9Sstevel@tonic-gate 			if (s_bind < STB_NUM && s_type < STT_NUM) {
32067c478bd9Sstevel@tonic-gate 				type = (1 << (s_type + 8)) | (1 << s_bind);
32077c478bd9Sstevel@tonic-gate 				if ((type & ~mask) != 0)
32087c478bd9Sstevel@tonic-gate 					continue;
32097c478bd9Sstevel@tonic-gate 			} else
32107c478bd9Sstevel@tonic-gate 				continue; /* Invalid type or binding */
32117c478bd9Sstevel@tonic-gate 
32127c478bd9Sstevel@tonic-gate 			if (GELF_ST_TYPE(sym.st_info) != STT_TLS)
32137c478bd9Sstevel@tonic-gate 				sym.st_value += fptr->file_dyn_base;
32147c478bd9Sstevel@tonic-gate 
32157c478bd9Sstevel@tonic-gate 			si.prs_name = strs + sym.st_name;
3216f957ecc1Svb160487 
3217f957ecc1Svb160487 			/*
3218f957ecc1Svb160487 			 * If symbol's type is STT_SECTION, then try to lookup
3219f957ecc1Svb160487 			 * the name of the corresponding section.
3220f957ecc1Svb160487 			 */
3221f957ecc1Svb160487 			if (GELF_ST_TYPE(sym.st_info) == STT_SECTION &&
3222f957ecc1Svb160487 			    fptr->file_shstrs != NULL &&
3223f957ecc1Svb160487 			    gelf_getshdr(elf_getscn(fptr->file_elf,
3224f957ecc1Svb160487 			    sym.st_shndx), &shdr) != NULL &&
3225f957ecc1Svb160487 			    shdr.sh_name != 0 &&
3226f957ecc1Svb160487 			    shdr.sh_name < fptr->file_shstrsz)
3227f957ecc1Svb160487 				si.prs_name = fptr->file_shstrs + shdr.sh_name;
3228f957ecc1Svb160487 
32297c478bd9Sstevel@tonic-gate 			si.prs_id = ndx;
3230f957ecc1Svb160487 			if ((rv = func(cd, &sym, si.prs_name, &si)) != 0)
32317c478bd9Sstevel@tonic-gate 				break;
32327c478bd9Sstevel@tonic-gate 		}
32337c478bd9Sstevel@tonic-gate 	}
32347c478bd9Sstevel@tonic-gate 
32357c478bd9Sstevel@tonic-gate 	return (rv);
32367c478bd9Sstevel@tonic-gate }
32377c478bd9Sstevel@tonic-gate 
32387c478bd9Sstevel@tonic-gate int
32397c478bd9Sstevel@tonic-gate Pxsymbol_iter(struct ps_prochandle *P, Lmid_t lmid, const char *object_name,
32407c478bd9Sstevel@tonic-gate     int which, int mask, proc_xsym_f *func, void *cd)
32417c478bd9Sstevel@tonic-gate {
32427c478bd9Sstevel@tonic-gate 	return (Psymbol_iter_com(P, lmid, object_name, which, mask,
32437c478bd9Sstevel@tonic-gate 	    PRO_NATURAL, func, cd));
32447c478bd9Sstevel@tonic-gate }
32457c478bd9Sstevel@tonic-gate 
32467c478bd9Sstevel@tonic-gate int
32477c478bd9Sstevel@tonic-gate Psymbol_iter_by_lmid(struct ps_prochandle *P, Lmid_t lmid,
32487c478bd9Sstevel@tonic-gate     const char *object_name, int which, int mask, proc_sym_f *func, void *cd)
32497c478bd9Sstevel@tonic-gate {
32507c478bd9Sstevel@tonic-gate 	return (Psymbol_iter_com(P, lmid, object_name, which, mask,
32517c478bd9Sstevel@tonic-gate 	    PRO_NATURAL, (proc_xsym_f *)func, cd));
32527c478bd9Sstevel@tonic-gate }
32537c478bd9Sstevel@tonic-gate 
32547c478bd9Sstevel@tonic-gate int
32557c478bd9Sstevel@tonic-gate Psymbol_iter(struct ps_prochandle *P,
32567c478bd9Sstevel@tonic-gate     const char *object_name, int which, int mask, proc_sym_f *func, void *cd)
32577c478bd9Sstevel@tonic-gate {
32587c478bd9Sstevel@tonic-gate 	return (Psymbol_iter_com(P, PR_LMID_EVERY, object_name, which, mask,
32597c478bd9Sstevel@tonic-gate 	    PRO_NATURAL, (proc_xsym_f *)func, cd));
32607c478bd9Sstevel@tonic-gate }
32617c478bd9Sstevel@tonic-gate 
32627c478bd9Sstevel@tonic-gate int
32637c478bd9Sstevel@tonic-gate Psymbol_iter_by_addr(struct ps_prochandle *P,
32647c478bd9Sstevel@tonic-gate     const char *object_name, int which, int mask, proc_sym_f *func, void *cd)
32657c478bd9Sstevel@tonic-gate {
32667c478bd9Sstevel@tonic-gate 	return (Psymbol_iter_com(P, PR_LMID_EVERY, object_name, which, mask,
32677c478bd9Sstevel@tonic-gate 	    PRO_BYADDR, (proc_xsym_f *)func, cd));
32687c478bd9Sstevel@tonic-gate }
32697c478bd9Sstevel@tonic-gate 
32707c478bd9Sstevel@tonic-gate int
32717c478bd9Sstevel@tonic-gate Psymbol_iter_by_name(struct ps_prochandle *P,
32727c478bd9Sstevel@tonic-gate     const char *object_name, int which, int mask, proc_sym_f *func, void *cd)
32737c478bd9Sstevel@tonic-gate {
32747c478bd9Sstevel@tonic-gate 	return (Psymbol_iter_com(P, PR_LMID_EVERY, object_name, which, mask,
32757c478bd9Sstevel@tonic-gate 	    PRO_BYNAME, (proc_xsym_f *)func, cd));
32767c478bd9Sstevel@tonic-gate }
32777c478bd9Sstevel@tonic-gate 
32787c478bd9Sstevel@tonic-gate /*
32797c478bd9Sstevel@tonic-gate  * Get the platform string from the core file if we have it;
32807c478bd9Sstevel@tonic-gate  * just perform the system call for the caller if this is a live process.
32817c478bd9Sstevel@tonic-gate  */
32827c478bd9Sstevel@tonic-gate char *
32837c478bd9Sstevel@tonic-gate Pplatform(struct ps_prochandle *P, char *s, size_t n)
32847c478bd9Sstevel@tonic-gate {
32857c478bd9Sstevel@tonic-gate 	if (P->state == PS_IDLE) {
32867c478bd9Sstevel@tonic-gate 		errno = ENODATA;
32877c478bd9Sstevel@tonic-gate 		return (NULL);
32887c478bd9Sstevel@tonic-gate 	}
32897c478bd9Sstevel@tonic-gate 
32907c478bd9Sstevel@tonic-gate 	if (P->state == PS_DEAD) {
32917c478bd9Sstevel@tonic-gate 		if (P->core->core_platform == NULL) {
32927c478bd9Sstevel@tonic-gate 			errno = ENODATA;
32937c478bd9Sstevel@tonic-gate 			return (NULL);
32947c478bd9Sstevel@tonic-gate 		}
32957c478bd9Sstevel@tonic-gate 		(void) strncpy(s, P->core->core_platform, n - 1);
32967c478bd9Sstevel@tonic-gate 		s[n - 1] = '\0';
32977c478bd9Sstevel@tonic-gate 
32987c478bd9Sstevel@tonic-gate 	} else if (sysinfo(SI_PLATFORM, s, n) == -1)
32997c478bd9Sstevel@tonic-gate 		return (NULL);
33007c478bd9Sstevel@tonic-gate 
33017c478bd9Sstevel@tonic-gate 	return (s);
33027c478bd9Sstevel@tonic-gate }
33037c478bd9Sstevel@tonic-gate 
33047c478bd9Sstevel@tonic-gate /*
33057c478bd9Sstevel@tonic-gate  * Get the uname(2) information from the core file if we have it;
33067c478bd9Sstevel@tonic-gate  * just perform the system call for the caller if this is a live process.
33077c478bd9Sstevel@tonic-gate  */
33087c478bd9Sstevel@tonic-gate int
33097c478bd9Sstevel@tonic-gate Puname(struct ps_prochandle *P, struct utsname *u)
33107c478bd9Sstevel@tonic-gate {
33117c478bd9Sstevel@tonic-gate 	if (P->state == PS_IDLE) {
33127c478bd9Sstevel@tonic-gate 		errno = ENODATA;
33137c478bd9Sstevel@tonic-gate 		return (-1);
33147c478bd9Sstevel@tonic-gate 	}
33157c478bd9Sstevel@tonic-gate 
33167c478bd9Sstevel@tonic-gate 	if (P->state == PS_DEAD) {
33177c478bd9Sstevel@tonic-gate 		if (P->core->core_uts == NULL) {
33187c478bd9Sstevel@tonic-gate 			errno = ENODATA;
33197c478bd9Sstevel@tonic-gate 			return (-1);
33207c478bd9Sstevel@tonic-gate 		}
33217c478bd9Sstevel@tonic-gate 		(void) memcpy(u, P->core->core_uts, sizeof (struct utsname));
33227c478bd9Sstevel@tonic-gate 		return (0);
33237c478bd9Sstevel@tonic-gate 	}
33247c478bd9Sstevel@tonic-gate 	return (uname(u));
33257c478bd9Sstevel@tonic-gate }
33267c478bd9Sstevel@tonic-gate 
33277c478bd9Sstevel@tonic-gate /*
33287c478bd9Sstevel@tonic-gate  * Get the zone name from the core file if we have it; look up the
33297c478bd9Sstevel@tonic-gate  * name based on the zone id if this is a live process.
33307c478bd9Sstevel@tonic-gate  */
33317c478bd9Sstevel@tonic-gate char *
33327c478bd9Sstevel@tonic-gate Pzonename(struct ps_prochandle *P, char *s, size_t n)
33337c478bd9Sstevel@tonic-gate {
33347c478bd9Sstevel@tonic-gate 	if (P->state == PS_IDLE) {
33357c478bd9Sstevel@tonic-gate 		errno = ENODATA;
33367c478bd9Sstevel@tonic-gate 		return (NULL);
33377c478bd9Sstevel@tonic-gate 	}
33387c478bd9Sstevel@tonic-gate 
33397c478bd9Sstevel@tonic-gate 	if (P->state == PS_DEAD) {
33407c478bd9Sstevel@tonic-gate 		if (P->core->core_zonename == NULL) {
33417c478bd9Sstevel@tonic-gate 			errno = ENODATA;
33427c478bd9Sstevel@tonic-gate 			return (NULL);
33437c478bd9Sstevel@tonic-gate 		}
33447c478bd9Sstevel@tonic-gate 		(void) strlcpy(s, P->core->core_zonename, n);
33457c478bd9Sstevel@tonic-gate 	} else {
33467c478bd9Sstevel@tonic-gate 		if (getzonenamebyid(P->status.pr_zoneid, s, n) < 0)
33477c478bd9Sstevel@tonic-gate 			return (NULL);
33487c478bd9Sstevel@tonic-gate 		s[n - 1] = '\0';
33497c478bd9Sstevel@tonic-gate 	}
33507c478bd9Sstevel@tonic-gate 	return (s);
33517c478bd9Sstevel@tonic-gate }
33527c478bd9Sstevel@tonic-gate 
33537c478bd9Sstevel@tonic-gate /*
33547c478bd9Sstevel@tonic-gate  * Called from Pcreate(), Pgrab(), and Pfgrab_core() to initialize
33557c478bd9Sstevel@tonic-gate  * the symbol table heads in the new ps_prochandle.
33567c478bd9Sstevel@tonic-gate  */
33577c478bd9Sstevel@tonic-gate void
33587c478bd9Sstevel@tonic-gate Pinitsym(struct ps_prochandle *P)
33597c478bd9Sstevel@tonic-gate {
33607c478bd9Sstevel@tonic-gate 	P->num_files = 0;
33617c478bd9Sstevel@tonic-gate 	list_link(&P->file_head, NULL);
33627c478bd9Sstevel@tonic-gate }
33637c478bd9Sstevel@tonic-gate 
33647c478bd9Sstevel@tonic-gate /*
33657c478bd9Sstevel@tonic-gate  * Called from Prelease() to destroy the symbol tables.
33667c478bd9Sstevel@tonic-gate  * Must be called by the client after an exec() in the victim process.
33677c478bd9Sstevel@tonic-gate  */
33687c478bd9Sstevel@tonic-gate void
33697c478bd9Sstevel@tonic-gate Preset_maps(struct ps_prochandle *P)
33707c478bd9Sstevel@tonic-gate {
33717c478bd9Sstevel@tonic-gate 	int i;
33727c478bd9Sstevel@tonic-gate 
33737c478bd9Sstevel@tonic-gate 	if (P->rap != NULL) {
33747c478bd9Sstevel@tonic-gate 		rd_delete(P->rap);
33757c478bd9Sstevel@tonic-gate 		P->rap = NULL;
33767c478bd9Sstevel@tonic-gate 	}
33777c478bd9Sstevel@tonic-gate 
33787c478bd9Sstevel@tonic-gate 	if (P->execname != NULL) {
33797c478bd9Sstevel@tonic-gate 		free(P->execname);
33807c478bd9Sstevel@tonic-gate 		P->execname = NULL;
33817c478bd9Sstevel@tonic-gate 	}
33827c478bd9Sstevel@tonic-gate 
33837c478bd9Sstevel@tonic-gate 	if (P->auxv != NULL) {
33847c478bd9Sstevel@tonic-gate 		free(P->auxv);
33857c478bd9Sstevel@tonic-gate 		P->auxv = NULL;
33867c478bd9Sstevel@tonic-gate 		P->nauxv = 0;
33877c478bd9Sstevel@tonic-gate 	}
33887c478bd9Sstevel@tonic-gate 
33897c478bd9Sstevel@tonic-gate 	for (i = 0; i < P->map_count; i++)
33907c478bd9Sstevel@tonic-gate 		map_info_free(P, &P->mappings[i]);
33917c478bd9Sstevel@tonic-gate 
33927c478bd9Sstevel@tonic-gate 	if (P->mappings != NULL) {
33937c478bd9Sstevel@tonic-gate 		free(P->mappings);
33947c478bd9Sstevel@tonic-gate 		P->mappings = NULL;
33957c478bd9Sstevel@tonic-gate 	}
33967c478bd9Sstevel@tonic-gate 	P->map_count = P->map_alloc = 0;
33977c478bd9Sstevel@tonic-gate 
33987c478bd9Sstevel@tonic-gate 	P->info_valid = 0;
33997c478bd9Sstevel@tonic-gate }
34007c478bd9Sstevel@tonic-gate 
34017c478bd9Sstevel@tonic-gate typedef struct getenv_data {
34027c478bd9Sstevel@tonic-gate 	char *buf;
34037c478bd9Sstevel@tonic-gate 	size_t bufsize;
34047c478bd9Sstevel@tonic-gate 	const char *search;
34057c478bd9Sstevel@tonic-gate 	size_t searchlen;
34067c478bd9Sstevel@tonic-gate } getenv_data_t;
34077c478bd9Sstevel@tonic-gate 
34087c478bd9Sstevel@tonic-gate /*ARGSUSED*/
34097c478bd9Sstevel@tonic-gate static int
34107c478bd9Sstevel@tonic-gate getenv_func(void *data, struct ps_prochandle *P, uintptr_t addr,
34117c478bd9Sstevel@tonic-gate     const char *nameval)
34127c478bd9Sstevel@tonic-gate {
34137c478bd9Sstevel@tonic-gate 	getenv_data_t *d = data;
34147c478bd9Sstevel@tonic-gate 	size_t len;
34157c478bd9Sstevel@tonic-gate 
34167c478bd9Sstevel@tonic-gate 	if (nameval == NULL)
34177c478bd9Sstevel@tonic-gate 		return (0);
34187c478bd9Sstevel@tonic-gate 
34197c478bd9Sstevel@tonic-gate 	if (d->searchlen < strlen(nameval) &&
34207c478bd9Sstevel@tonic-gate 	    strncmp(nameval, d->search, d->searchlen) == 0 &&
34217c478bd9Sstevel@tonic-gate 	    nameval[d->searchlen] == '=') {
34227c478bd9Sstevel@tonic-gate 		len = MIN(strlen(nameval), d->bufsize - 1);
34237c478bd9Sstevel@tonic-gate 		(void) strncpy(d->buf, nameval, len);
34247c478bd9Sstevel@tonic-gate 		d->buf[len] = '\0';
34257c478bd9Sstevel@tonic-gate 		return (1);
34267c478bd9Sstevel@tonic-gate 	}
34277c478bd9Sstevel@tonic-gate 
34287c478bd9Sstevel@tonic-gate 	return (0);
34297c478bd9Sstevel@tonic-gate }
34307c478bd9Sstevel@tonic-gate 
34317c478bd9Sstevel@tonic-gate char *
34327c478bd9Sstevel@tonic-gate Pgetenv(struct ps_prochandle *P, const char *name, char *buf, size_t buflen)
34337c478bd9Sstevel@tonic-gate {
34347c478bd9Sstevel@tonic-gate 	getenv_data_t d;
34357c478bd9Sstevel@tonic-gate 
34367c478bd9Sstevel@tonic-gate 	d.buf = buf;
34377c478bd9Sstevel@tonic-gate 	d.bufsize = buflen;
34387c478bd9Sstevel@tonic-gate 	d.search = name;
34397c478bd9Sstevel@tonic-gate 	d.searchlen = strlen(name);
34407c478bd9Sstevel@tonic-gate 
34417c478bd9Sstevel@tonic-gate 	if (Penv_iter(P, getenv_func, &d) == 1) {
34427c478bd9Sstevel@tonic-gate 		char *equals = strchr(d.buf, '=');
34437c478bd9Sstevel@tonic-gate 
34447c478bd9Sstevel@tonic-gate 		if (equals != NULL) {
34457c478bd9Sstevel@tonic-gate 			(void) memmove(d.buf, equals + 1,
34467c478bd9Sstevel@tonic-gate 			    d.buf + buflen - equals - 1);
34477c478bd9Sstevel@tonic-gate 			d.buf[d.buf + buflen - equals] = '\0';
34487c478bd9Sstevel@tonic-gate 
34497c478bd9Sstevel@tonic-gate 			return (buf);
34507c478bd9Sstevel@tonic-gate 		}
34517c478bd9Sstevel@tonic-gate 	}
34527c478bd9Sstevel@tonic-gate 
34537c478bd9Sstevel@tonic-gate 	return (NULL);
34547c478bd9Sstevel@tonic-gate }
34557c478bd9Sstevel@tonic-gate 
34567c478bd9Sstevel@tonic-gate /* number of argument or environment pointers to read all at once */
34577c478bd9Sstevel@tonic-gate #define	NARG	100
34587c478bd9Sstevel@tonic-gate 
34597c478bd9Sstevel@tonic-gate int
34607c478bd9Sstevel@tonic-gate Penv_iter(struct ps_prochandle *P, proc_env_f *func, void *data)
34617c478bd9Sstevel@tonic-gate {
34627c478bd9Sstevel@tonic-gate 	const psinfo_t *psp;
34637c478bd9Sstevel@tonic-gate 	uintptr_t envpoff;
34647c478bd9Sstevel@tonic-gate 	GElf_Sym sym;
34657c478bd9Sstevel@tonic-gate 	int ret;
34667c478bd9Sstevel@tonic-gate 	char *buf, *nameval;
34677c478bd9Sstevel@tonic-gate 	size_t buflen;
34687c478bd9Sstevel@tonic-gate 
34697c478bd9Sstevel@tonic-gate 	int nenv = NARG;
34707c478bd9Sstevel@tonic-gate 	long envp[NARG];
34717c478bd9Sstevel@tonic-gate 
34727c478bd9Sstevel@tonic-gate 	/*
34737c478bd9Sstevel@tonic-gate 	 * Attempt to find the "_environ" variable in the process.
34747c478bd9Sstevel@tonic-gate 	 * Failing that, use the original value provided by Ppsinfo().
34757c478bd9Sstevel@tonic-gate 	 */
34767c478bd9Sstevel@tonic-gate 	if ((psp = Ppsinfo(P)) == NULL)
34777c478bd9Sstevel@tonic-gate 		return (-1);
34787c478bd9Sstevel@tonic-gate 
34797c478bd9Sstevel@tonic-gate 	envpoff = psp->pr_envp; /* Default if no _environ found */
34807c478bd9Sstevel@tonic-gate 
34817c478bd9Sstevel@tonic-gate 	if (Plookup_by_name(P, PR_OBJ_EXEC, "_environ", &sym) == 0) {
34827c478bd9Sstevel@tonic-gate 		if (P->status.pr_dmodel == PR_MODEL_NATIVE) {
34837c478bd9Sstevel@tonic-gate 			if (Pread(P, &envpoff, sizeof (envpoff),
34847c478bd9Sstevel@tonic-gate 			    sym.st_value) != sizeof (envpoff))
34857c478bd9Sstevel@tonic-gate 				envpoff = psp->pr_envp;
34867c478bd9Sstevel@tonic-gate 		} else if (P->status.pr_dmodel == PR_MODEL_ILP32) {
34877c478bd9Sstevel@tonic-gate 			uint32_t envpoff32;
34887c478bd9Sstevel@tonic-gate 
34897c478bd9Sstevel@tonic-gate 			if (Pread(P, &envpoff32, sizeof (envpoff32),
34907c478bd9Sstevel@tonic-gate 			    sym.st_value) != sizeof (envpoff32))
34917c478bd9Sstevel@tonic-gate 				envpoff = psp->pr_envp;
34927c478bd9Sstevel@tonic-gate 			else
34937c478bd9Sstevel@tonic-gate 				envpoff = envpoff32;
34947c478bd9Sstevel@tonic-gate 		}
34957c478bd9Sstevel@tonic-gate 	}
34967c478bd9Sstevel@tonic-gate 
34977c478bd9Sstevel@tonic-gate 	buflen = 128;
34987c478bd9Sstevel@tonic-gate 	buf = malloc(buflen);
34997c478bd9Sstevel@tonic-gate 
35007c478bd9Sstevel@tonic-gate 	ret = 0;
35017c478bd9Sstevel@tonic-gate 	for (;;) {
35027c478bd9Sstevel@tonic-gate 		uintptr_t envoff;
35037c478bd9Sstevel@tonic-gate 
35047c478bd9Sstevel@tonic-gate 		if (nenv == NARG) {
35057c478bd9Sstevel@tonic-gate 			(void) memset(envp, 0, sizeof (envp));
35067c478bd9Sstevel@tonic-gate 			if (P->status.pr_dmodel == PR_MODEL_NATIVE) {
35077c478bd9Sstevel@tonic-gate 				if (Pread(P, envp,
35087c478bd9Sstevel@tonic-gate 				    sizeof (envp), envpoff) <= 0) {
35097c478bd9Sstevel@tonic-gate 					ret = -1;
35107c478bd9Sstevel@tonic-gate 					break;
35117c478bd9Sstevel@tonic-gate 				}
35127c478bd9Sstevel@tonic-gate 			} else if (P->status.pr_dmodel == PR_MODEL_ILP32) {
35137c478bd9Sstevel@tonic-gate 				uint32_t e32[NARG];
35147c478bd9Sstevel@tonic-gate 				int i;
35157c478bd9Sstevel@tonic-gate 
35167c478bd9Sstevel@tonic-gate 				(void) memset(e32, 0, sizeof (e32));
35177c478bd9Sstevel@tonic-gate 				if (Pread(P, e32, sizeof (e32), envpoff) <= 0) {
35187c478bd9Sstevel@tonic-gate 					ret = -1;
35197c478bd9Sstevel@tonic-gate 					break;
35207c478bd9Sstevel@tonic-gate 				}
35217c478bd9Sstevel@tonic-gate 				for (i = 0; i < NARG; i++)
35227c478bd9Sstevel@tonic-gate 					envp[i] = e32[i];
35237c478bd9Sstevel@tonic-gate 			}
35247c478bd9Sstevel@tonic-gate 			nenv = 0;
35257c478bd9Sstevel@tonic-gate 		}
35267c478bd9Sstevel@tonic-gate 
35277c478bd9Sstevel@tonic-gate 		if ((envoff = envp[nenv++]) == NULL)
35287c478bd9Sstevel@tonic-gate 			break;
35297c478bd9Sstevel@tonic-gate 
35307c478bd9Sstevel@tonic-gate 		/*
35317c478bd9Sstevel@tonic-gate 		 * Attempt to read the string from the process.
35327c478bd9Sstevel@tonic-gate 		 */
35337c478bd9Sstevel@tonic-gate again:
35347c478bd9Sstevel@tonic-gate 		ret = Pread_string(P, buf, buflen, envoff);
35357c478bd9Sstevel@tonic-gate 
35367c478bd9Sstevel@tonic-gate 		if (ret <= 0) {
35377c478bd9Sstevel@tonic-gate 			nameval = NULL;
35387c478bd9Sstevel@tonic-gate 		} else if (ret == buflen - 1) {
35397c478bd9Sstevel@tonic-gate 			free(buf);
35407c478bd9Sstevel@tonic-gate 			/*
35417c478bd9Sstevel@tonic-gate 			 * Bail if we have a corrupted environment
35427c478bd9Sstevel@tonic-gate 			 */
35437c478bd9Sstevel@tonic-gate 			if (buflen >= ARG_MAX)
35447c478bd9Sstevel@tonic-gate 				return (-1);
35457c478bd9Sstevel@tonic-gate 			buflen *= 2;
35467c478bd9Sstevel@tonic-gate 			buf = malloc(buflen);
35477c478bd9Sstevel@tonic-gate 			goto again;
35487c478bd9Sstevel@tonic-gate 		} else {
35497c478bd9Sstevel@tonic-gate 			nameval = buf;
35507c478bd9Sstevel@tonic-gate 		}
35517c478bd9Sstevel@tonic-gate 
35527c478bd9Sstevel@tonic-gate 		if ((ret = func(data, P, envoff, nameval)) != 0)
35537c478bd9Sstevel@tonic-gate 			break;
35547c478bd9Sstevel@tonic-gate 
35557c478bd9Sstevel@tonic-gate 		envpoff += (P->status.pr_dmodel == PR_MODEL_LP64)? 8 : 4;
35567c478bd9Sstevel@tonic-gate 	}
35577c478bd9Sstevel@tonic-gate 
35587c478bd9Sstevel@tonic-gate 	free(buf);
35597c478bd9Sstevel@tonic-gate 
35607c478bd9Sstevel@tonic-gate 	return (ret);
35617c478bd9Sstevel@tonic-gate }
3562