xref: /onnv-gate/usr/src/lib/libdtrace/common/dt_module.c (revision 871:f917b2ed7d21)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
230Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include <sys/types.h>
300Sstevel@tonic-gate #include <sys/modctl.h>
310Sstevel@tonic-gate #include <sys/kobj.h>
320Sstevel@tonic-gate #include <sys/kobj_impl.h>
330Sstevel@tonic-gate #include <sys/sysmacros.h>
340Sstevel@tonic-gate #include <sys/elf.h>
350Sstevel@tonic-gate #include <sys/task.h>
360Sstevel@tonic-gate 
370Sstevel@tonic-gate #include <unistd.h>
380Sstevel@tonic-gate #include <project.h>
390Sstevel@tonic-gate #include <strings.h>
400Sstevel@tonic-gate #include <stdlib.h>
410Sstevel@tonic-gate #include <libelf.h>
420Sstevel@tonic-gate #include <limits.h>
430Sstevel@tonic-gate #include <assert.h>
440Sstevel@tonic-gate #include <errno.h>
450Sstevel@tonic-gate #include <dirent.h>
460Sstevel@tonic-gate 
470Sstevel@tonic-gate #include <dt_strtab.h>
480Sstevel@tonic-gate #include <dt_module.h>
490Sstevel@tonic-gate #include <dt_impl.h>
500Sstevel@tonic-gate 
510Sstevel@tonic-gate static const char *dt_module_strtab; /* active strtab for qsort callbacks */
520Sstevel@tonic-gate 
530Sstevel@tonic-gate static void
540Sstevel@tonic-gate dt_module_symhash_insert(dt_module_t *dmp, const char *name, uint_t id)
550Sstevel@tonic-gate {
560Sstevel@tonic-gate 	dt_sym_t *dsp = &dmp->dm_symchains[dmp->dm_symfree];
570Sstevel@tonic-gate 	uint_t h;
580Sstevel@tonic-gate 
590Sstevel@tonic-gate 	assert(dmp->dm_symfree < dmp->dm_nsymelems + 1);
600Sstevel@tonic-gate 
610Sstevel@tonic-gate 	dsp->ds_symid = id;
620Sstevel@tonic-gate 	h = dt_strtab_hash(name, NULL) % dmp->dm_nsymbuckets;
630Sstevel@tonic-gate 	dsp->ds_next = dmp->dm_symbuckets[h];
640Sstevel@tonic-gate 	dmp->dm_symbuckets[h] = dmp->dm_symfree++;
650Sstevel@tonic-gate }
660Sstevel@tonic-gate 
670Sstevel@tonic-gate static uint_t
680Sstevel@tonic-gate dt_module_syminit32(dt_module_t *dmp)
690Sstevel@tonic-gate {
700Sstevel@tonic-gate 	const Elf32_Sym *sym = dmp->dm_symtab.cts_data;
710Sstevel@tonic-gate 	const char *base = dmp->dm_strtab.cts_data;
720Sstevel@tonic-gate 	size_t ss_size = dmp->dm_strtab.cts_size;
730Sstevel@tonic-gate 	uint_t i, n = dmp->dm_nsymelems;
740Sstevel@tonic-gate 	uint_t asrsv = 0;
750Sstevel@tonic-gate 
760Sstevel@tonic-gate 	for (i = 0; i < n; i++, sym++) {
770Sstevel@tonic-gate 		const char *name = base + sym->st_name;
780Sstevel@tonic-gate 		uchar_t type = ELF32_ST_TYPE(sym->st_info);
790Sstevel@tonic-gate 
800Sstevel@tonic-gate 		if (type >= STT_NUM || type == STT_SECTION)
810Sstevel@tonic-gate 			continue; /* skip sections and unknown types */
820Sstevel@tonic-gate 
830Sstevel@tonic-gate 		if (sym->st_name == 0 || sym->st_name >= ss_size)
840Sstevel@tonic-gate 			continue; /* skip null or invalid names */
850Sstevel@tonic-gate 
860Sstevel@tonic-gate 		if (sym->st_value != 0 &&
870Sstevel@tonic-gate 		    (ELF32_ST_BIND(sym->st_info) != STB_LOCAL || sym->st_size))
880Sstevel@tonic-gate 			asrsv++; /* reserve space in the address map */
890Sstevel@tonic-gate 
900Sstevel@tonic-gate 		dt_module_symhash_insert(dmp, name, i);
910Sstevel@tonic-gate 	}
920Sstevel@tonic-gate 
930Sstevel@tonic-gate 	return (asrsv);
940Sstevel@tonic-gate }
950Sstevel@tonic-gate 
960Sstevel@tonic-gate static uint_t
970Sstevel@tonic-gate dt_module_syminit64(dt_module_t *dmp)
980Sstevel@tonic-gate {
990Sstevel@tonic-gate 	const Elf64_Sym *sym = dmp->dm_symtab.cts_data;
1000Sstevel@tonic-gate 	const char *base = dmp->dm_strtab.cts_data;
1010Sstevel@tonic-gate 	size_t ss_size = dmp->dm_strtab.cts_size;
1020Sstevel@tonic-gate 	uint_t i, n = dmp->dm_nsymelems;
1030Sstevel@tonic-gate 	uint_t asrsv = 0;
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate 	for (i = 0; i < n; i++, sym++) {
1060Sstevel@tonic-gate 		const char *name = base + sym->st_name;
1070Sstevel@tonic-gate 		uchar_t type = ELF64_ST_TYPE(sym->st_info);
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate 		if (type >= STT_NUM || type == STT_SECTION)
1100Sstevel@tonic-gate 			continue; /* skip sections and unknown types */
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate 		if (sym->st_name == 0 || sym->st_name >= ss_size)
1130Sstevel@tonic-gate 			continue; /* skip null or invalid names */
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate 		if (sym->st_value != 0 &&
1160Sstevel@tonic-gate 		    (ELF64_ST_BIND(sym->st_info) != STB_LOCAL || sym->st_size))
1170Sstevel@tonic-gate 			asrsv++; /* reserve space in the address map */
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate 		dt_module_symhash_insert(dmp, name, i);
1200Sstevel@tonic-gate 	}
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate 	return (asrsv);
1230Sstevel@tonic-gate }
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate /*
1260Sstevel@tonic-gate  * Sort comparison function for 32-bit symbol address-to-name lookups.  We sort
1270Sstevel@tonic-gate  * symbols by value.  If values are equal, we prefer the symbol that is
1280Sstevel@tonic-gate  * non-zero sized, typed, not weak, or lexically first, in that order.
1290Sstevel@tonic-gate  */
1300Sstevel@tonic-gate static int
1310Sstevel@tonic-gate dt_module_symcomp32(const void *lp, const void *rp)
1320Sstevel@tonic-gate {
1330Sstevel@tonic-gate 	Elf32_Sym *lhs = *((Elf32_Sym **)lp);
1340Sstevel@tonic-gate 	Elf32_Sym *rhs = *((Elf32_Sym **)rp);
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate 	if (lhs->st_value != rhs->st_value)
1370Sstevel@tonic-gate 		return (lhs->st_value > rhs->st_value ? 1 : -1);
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate 	if ((lhs->st_size == 0) != (rhs->st_size == 0))
1400Sstevel@tonic-gate 		return (lhs->st_size == 0 ? 1 : -1);
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate 	if ((ELF32_ST_TYPE(lhs->st_info) == STT_NOTYPE) !=
1430Sstevel@tonic-gate 	    (ELF32_ST_TYPE(rhs->st_info) == STT_NOTYPE))
1440Sstevel@tonic-gate 		return (ELF32_ST_TYPE(lhs->st_info) == STT_NOTYPE ? 1 : -1);
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate 	if ((ELF32_ST_BIND(lhs->st_info) == STB_WEAK) !=
1470Sstevel@tonic-gate 	    (ELF32_ST_BIND(rhs->st_info) == STB_WEAK))
1480Sstevel@tonic-gate 		return (ELF32_ST_BIND(lhs->st_info) == STB_WEAK ? 1 : -1);
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate 	return (strcmp(dt_module_strtab + lhs->st_name,
1510Sstevel@tonic-gate 	    dt_module_strtab + rhs->st_name));
1520Sstevel@tonic-gate }
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate /*
1550Sstevel@tonic-gate  * Sort comparison function for 64-bit symbol address-to-name lookups.  We sort
1560Sstevel@tonic-gate  * symbols by value.  If values are equal, we prefer the symbol that is
1570Sstevel@tonic-gate  * non-zero sized, typed, not weak, or lexically first, in that order.
1580Sstevel@tonic-gate  */
1590Sstevel@tonic-gate static int
1600Sstevel@tonic-gate dt_module_symcomp64(const void *lp, const void *rp)
1610Sstevel@tonic-gate {
1620Sstevel@tonic-gate 	Elf64_Sym *lhs = *((Elf64_Sym **)lp);
1630Sstevel@tonic-gate 	Elf64_Sym *rhs = *((Elf64_Sym **)rp);
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate 	if (lhs->st_value != rhs->st_value)
1660Sstevel@tonic-gate 		return (lhs->st_value > rhs->st_value ? 1 : -1);
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate 	if ((lhs->st_size == 0) != (rhs->st_size == 0))
1690Sstevel@tonic-gate 		return (lhs->st_size == 0 ? 1 : -1);
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate 	if ((ELF64_ST_TYPE(lhs->st_info) == STT_NOTYPE) !=
1720Sstevel@tonic-gate 	    (ELF64_ST_TYPE(rhs->st_info) == STT_NOTYPE))
1730Sstevel@tonic-gate 		return (ELF64_ST_TYPE(lhs->st_info) == STT_NOTYPE ? 1 : -1);
1740Sstevel@tonic-gate 
1750Sstevel@tonic-gate 	if ((ELF64_ST_BIND(lhs->st_info) == STB_WEAK) !=
1760Sstevel@tonic-gate 	    (ELF64_ST_BIND(rhs->st_info) == STB_WEAK))
1770Sstevel@tonic-gate 		return (ELF64_ST_BIND(lhs->st_info) == STB_WEAK ? 1 : -1);
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate 	return (strcmp(dt_module_strtab + lhs->st_name,
1800Sstevel@tonic-gate 	    dt_module_strtab + rhs->st_name));
1810Sstevel@tonic-gate }
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate static void
1840Sstevel@tonic-gate dt_module_symsort32(dt_module_t *dmp)
1850Sstevel@tonic-gate {
1860Sstevel@tonic-gate 	Elf32_Sym *symtab = (Elf32_Sym *)dmp->dm_symtab.cts_data;
1870Sstevel@tonic-gate 	Elf32_Sym **sympp = (Elf32_Sym **)dmp->dm_asmap;
1880Sstevel@tonic-gate 	const dt_sym_t *dsp = dmp->dm_symchains + 1;
1890Sstevel@tonic-gate 	uint_t i, n = dmp->dm_symfree;
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate 	for (i = 1; i < n; i++, dsp++) {
1920Sstevel@tonic-gate 		Elf32_Sym *sym = symtab + dsp->ds_symid;
1930Sstevel@tonic-gate 		if (sym->st_value != 0 &&
1940Sstevel@tonic-gate 		    (ELF32_ST_BIND(sym->st_info) != STB_LOCAL || sym->st_size))
1950Sstevel@tonic-gate 			*sympp++ = sym;
1960Sstevel@tonic-gate 	}
1970Sstevel@tonic-gate 
1980Sstevel@tonic-gate 	dmp->dm_aslen = (uint_t)(sympp - (Elf32_Sym **)dmp->dm_asmap);
1990Sstevel@tonic-gate 	assert(dmp->dm_aslen <= dmp->dm_asrsv);
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate 	dt_module_strtab = dmp->dm_strtab.cts_data;
2020Sstevel@tonic-gate 	qsort(dmp->dm_asmap, dmp->dm_aslen,
2030Sstevel@tonic-gate 	    sizeof (Elf32_Sym *), dt_module_symcomp32);
2040Sstevel@tonic-gate 	dt_module_strtab = NULL;
2050Sstevel@tonic-gate }
2060Sstevel@tonic-gate 
2070Sstevel@tonic-gate static void
2080Sstevel@tonic-gate dt_module_symsort64(dt_module_t *dmp)
2090Sstevel@tonic-gate {
2100Sstevel@tonic-gate 	Elf64_Sym *symtab = (Elf64_Sym *)dmp->dm_symtab.cts_data;
2110Sstevel@tonic-gate 	Elf64_Sym **sympp = (Elf64_Sym **)dmp->dm_asmap;
2120Sstevel@tonic-gate 	const dt_sym_t *dsp = dmp->dm_symchains + 1;
2130Sstevel@tonic-gate 	uint_t i, n = dmp->dm_symfree;
2140Sstevel@tonic-gate 
2150Sstevel@tonic-gate 	for (i = 1; i < n; i++, dsp++) {
2160Sstevel@tonic-gate 		Elf64_Sym *sym = symtab + dsp->ds_symid;
2170Sstevel@tonic-gate 		if (sym->st_value != 0 &&
2180Sstevel@tonic-gate 		    (ELF64_ST_BIND(sym->st_info) != STB_LOCAL || sym->st_size))
2190Sstevel@tonic-gate 			*sympp++ = sym;
2200Sstevel@tonic-gate 	}
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate 	dmp->dm_aslen = (uint_t)(sympp - (Elf64_Sym **)dmp->dm_asmap);
2230Sstevel@tonic-gate 	assert(dmp->dm_aslen <= dmp->dm_asrsv);
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate 	dt_module_strtab = dmp->dm_strtab.cts_data;
2260Sstevel@tonic-gate 	qsort(dmp->dm_asmap, dmp->dm_aslen,
2270Sstevel@tonic-gate 	    sizeof (Elf64_Sym *), dt_module_symcomp64);
2280Sstevel@tonic-gate 	dt_module_strtab = NULL;
2290Sstevel@tonic-gate }
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate static GElf_Sym *
2320Sstevel@tonic-gate dt_module_symgelf32(const Elf32_Sym *src, GElf_Sym *dst)
2330Sstevel@tonic-gate {
2340Sstevel@tonic-gate 	if (dst != NULL) {
2350Sstevel@tonic-gate 		dst->st_name = src->st_name;
2360Sstevel@tonic-gate 		dst->st_info = src->st_info;
2370Sstevel@tonic-gate 		dst->st_other = src->st_other;
2380Sstevel@tonic-gate 		dst->st_shndx = src->st_shndx;
2390Sstevel@tonic-gate 		dst->st_value = src->st_value;
2400Sstevel@tonic-gate 		dst->st_size = src->st_size;
2410Sstevel@tonic-gate 	}
2420Sstevel@tonic-gate 
2430Sstevel@tonic-gate 	return (dst);
2440Sstevel@tonic-gate }
2450Sstevel@tonic-gate 
2460Sstevel@tonic-gate static GElf_Sym *
2470Sstevel@tonic-gate dt_module_symgelf64(const Elf64_Sym *src, GElf_Sym *dst)
2480Sstevel@tonic-gate {
2490Sstevel@tonic-gate 	if (dst != NULL)
2500Sstevel@tonic-gate 		bcopy(src, dst, sizeof (GElf_Sym));
2510Sstevel@tonic-gate 
2520Sstevel@tonic-gate 	return (dst);
2530Sstevel@tonic-gate }
2540Sstevel@tonic-gate 
2550Sstevel@tonic-gate static GElf_Sym *
2560Sstevel@tonic-gate dt_module_symname32(dt_module_t *dmp, const char *name,
2570Sstevel@tonic-gate     GElf_Sym *symp, uint_t *idp)
2580Sstevel@tonic-gate {
2590Sstevel@tonic-gate 	const Elf32_Sym *symtab = dmp->dm_symtab.cts_data;
2600Sstevel@tonic-gate 	const char *strtab = dmp->dm_strtab.cts_data;
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate 	const Elf32_Sym *sym;
2630Sstevel@tonic-gate 	const dt_sym_t *dsp;
2640Sstevel@tonic-gate 	uint_t i, h;
2650Sstevel@tonic-gate 
2660Sstevel@tonic-gate 	if (dmp->dm_nsymelems == 0)
2670Sstevel@tonic-gate 		return (NULL);
2680Sstevel@tonic-gate 
2690Sstevel@tonic-gate 	h = dt_strtab_hash(name, NULL) % dmp->dm_nsymbuckets;
2700Sstevel@tonic-gate 
2710Sstevel@tonic-gate 	for (i = dmp->dm_symbuckets[h]; i != 0; i = dsp->ds_next) {
2720Sstevel@tonic-gate 		dsp = &dmp->dm_symchains[i];
2730Sstevel@tonic-gate 		sym = symtab + dsp->ds_symid;
2740Sstevel@tonic-gate 
2750Sstevel@tonic-gate 		if (strcmp(name, strtab + sym->st_name) == 0) {
2760Sstevel@tonic-gate 			if (idp != NULL)
2770Sstevel@tonic-gate 				*idp = dsp->ds_symid;
2780Sstevel@tonic-gate 			return (dt_module_symgelf32(sym, symp));
2790Sstevel@tonic-gate 		}
2800Sstevel@tonic-gate 	}
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate 	return (NULL);
2830Sstevel@tonic-gate }
2840Sstevel@tonic-gate 
2850Sstevel@tonic-gate static GElf_Sym *
2860Sstevel@tonic-gate dt_module_symname64(dt_module_t *dmp, const char *name,
2870Sstevel@tonic-gate     GElf_Sym *symp, uint_t *idp)
2880Sstevel@tonic-gate {
2890Sstevel@tonic-gate 	const Elf64_Sym *symtab = dmp->dm_symtab.cts_data;
2900Sstevel@tonic-gate 	const char *strtab = dmp->dm_strtab.cts_data;
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate 	const Elf64_Sym *sym;
2930Sstevel@tonic-gate 	const dt_sym_t *dsp;
2940Sstevel@tonic-gate 	uint_t i, h;
2950Sstevel@tonic-gate 
2960Sstevel@tonic-gate 	if (dmp->dm_nsymelems == 0)
2970Sstevel@tonic-gate 		return (NULL);
2980Sstevel@tonic-gate 
2990Sstevel@tonic-gate 	h = dt_strtab_hash(name, NULL) % dmp->dm_nsymbuckets;
3000Sstevel@tonic-gate 
3010Sstevel@tonic-gate 	for (i = dmp->dm_symbuckets[h]; i != 0; i = dsp->ds_next) {
3020Sstevel@tonic-gate 		dsp = &dmp->dm_symchains[i];
3030Sstevel@tonic-gate 		sym = symtab + dsp->ds_symid;
3040Sstevel@tonic-gate 
3050Sstevel@tonic-gate 		if (strcmp(name, strtab + sym->st_name) == 0) {
3060Sstevel@tonic-gate 			if (idp != NULL)
3070Sstevel@tonic-gate 				*idp = dsp->ds_symid;
3080Sstevel@tonic-gate 			return (dt_module_symgelf64(sym, symp));
3090Sstevel@tonic-gate 		}
3100Sstevel@tonic-gate 	}
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate 	return (NULL);
3130Sstevel@tonic-gate }
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate static GElf_Sym *
3160Sstevel@tonic-gate dt_module_symaddr32(dt_module_t *dmp, GElf_Addr addr,
3170Sstevel@tonic-gate     GElf_Sym *symp, uint_t *idp)
3180Sstevel@tonic-gate {
3190Sstevel@tonic-gate 	const Elf32_Sym **asmap = (const Elf32_Sym **)dmp->dm_asmap;
3200Sstevel@tonic-gate 	const Elf32_Sym *symtab = dmp->dm_symtab.cts_data;
3210Sstevel@tonic-gate 	const Elf32_Sym *sym;
3220Sstevel@tonic-gate 
3230Sstevel@tonic-gate 	uint_t i, mid, lo = 0, hi = dmp->dm_aslen - 1;
3240Sstevel@tonic-gate 	Elf32_Addr v;
3250Sstevel@tonic-gate 
3260Sstevel@tonic-gate 	if (dmp->dm_aslen == 0)
3270Sstevel@tonic-gate 		return (NULL);
3280Sstevel@tonic-gate 
3290Sstevel@tonic-gate 	while (hi - lo > 1) {
3300Sstevel@tonic-gate 		mid = (lo + hi) / 2;
3310Sstevel@tonic-gate 		if (addr >= asmap[mid]->st_value)
3320Sstevel@tonic-gate 			lo = mid;
3330Sstevel@tonic-gate 		else
3340Sstevel@tonic-gate 			hi = mid;
3350Sstevel@tonic-gate 	}
3360Sstevel@tonic-gate 
3370Sstevel@tonic-gate 	i = addr < asmap[hi]->st_value ? lo : hi;
3380Sstevel@tonic-gate 	sym = asmap[i];
3390Sstevel@tonic-gate 	v = sym->st_value;
3400Sstevel@tonic-gate 
3410Sstevel@tonic-gate 	/*
3420Sstevel@tonic-gate 	 * If the previous entry has the same value, improve our choice.  The
3430Sstevel@tonic-gate 	 * order of equal-valued symbols is determined by the comparison func.
3440Sstevel@tonic-gate 	 */
3450Sstevel@tonic-gate 	while (i-- != 0 && asmap[i]->st_value == v)
3460Sstevel@tonic-gate 		sym = asmap[i];
3470Sstevel@tonic-gate 
3480Sstevel@tonic-gate 	if (addr - sym->st_value < MAX(sym->st_size, 1)) {
3490Sstevel@tonic-gate 		if (idp != NULL)
3500Sstevel@tonic-gate 			*idp = (uint_t)(sym - symtab);
3510Sstevel@tonic-gate 		return (dt_module_symgelf32(sym, symp));
3520Sstevel@tonic-gate 	}
3530Sstevel@tonic-gate 
3540Sstevel@tonic-gate 	return (NULL);
3550Sstevel@tonic-gate }
3560Sstevel@tonic-gate 
3570Sstevel@tonic-gate static GElf_Sym *
3580Sstevel@tonic-gate dt_module_symaddr64(dt_module_t *dmp, GElf_Addr addr,
3590Sstevel@tonic-gate     GElf_Sym *symp, uint_t *idp)
3600Sstevel@tonic-gate {
3610Sstevel@tonic-gate 	const Elf64_Sym **asmap = (const Elf64_Sym **)dmp->dm_asmap;
3620Sstevel@tonic-gate 	const Elf64_Sym *symtab = dmp->dm_symtab.cts_data;
3630Sstevel@tonic-gate 	const Elf64_Sym *sym;
3640Sstevel@tonic-gate 
3650Sstevel@tonic-gate 	uint_t i, mid, lo = 0, hi = dmp->dm_aslen - 1;
3660Sstevel@tonic-gate 	Elf64_Addr v;
3670Sstevel@tonic-gate 
3680Sstevel@tonic-gate 	if (dmp->dm_aslen == 0)
3690Sstevel@tonic-gate 		return (NULL);
3700Sstevel@tonic-gate 
3710Sstevel@tonic-gate 	while (hi - lo > 1) {
3720Sstevel@tonic-gate 		mid = (lo + hi) / 2;
3730Sstevel@tonic-gate 		if (addr >= asmap[mid]->st_value)
3740Sstevel@tonic-gate 			lo = mid;
3750Sstevel@tonic-gate 		else
3760Sstevel@tonic-gate 			hi = mid;
3770Sstevel@tonic-gate 	}
3780Sstevel@tonic-gate 
3790Sstevel@tonic-gate 	i = addr < asmap[hi]->st_value ? lo : hi;
3800Sstevel@tonic-gate 	sym = asmap[i];
3810Sstevel@tonic-gate 	v = sym->st_value;
3820Sstevel@tonic-gate 
3830Sstevel@tonic-gate 	/*
3840Sstevel@tonic-gate 	 * If the previous entry has the same value, improve our choice.  The
3850Sstevel@tonic-gate 	 * order of equal-valued symbols is determined by the comparison func.
3860Sstevel@tonic-gate 	 */
3870Sstevel@tonic-gate 	while (i-- != 0 && asmap[i]->st_value == v)
3880Sstevel@tonic-gate 		sym = asmap[i];
3890Sstevel@tonic-gate 
3900Sstevel@tonic-gate 	if (addr - sym->st_value < MAX(sym->st_size, 1)) {
3910Sstevel@tonic-gate 		if (idp != NULL)
3920Sstevel@tonic-gate 			*idp = (uint_t)(sym - symtab);
3930Sstevel@tonic-gate 		return (dt_module_symgelf64(sym, symp));
3940Sstevel@tonic-gate 	}
3950Sstevel@tonic-gate 
3960Sstevel@tonic-gate 	return (NULL);
3970Sstevel@tonic-gate }
3980Sstevel@tonic-gate 
3990Sstevel@tonic-gate static const dt_modops_t dt_modops_32 = {
4000Sstevel@tonic-gate 	dt_module_syminit32,
4010Sstevel@tonic-gate 	dt_module_symsort32,
4020Sstevel@tonic-gate 	dt_module_symname32,
4030Sstevel@tonic-gate 	dt_module_symaddr32
4040Sstevel@tonic-gate };
4050Sstevel@tonic-gate 
4060Sstevel@tonic-gate static const dt_modops_t dt_modops_64 = {
4070Sstevel@tonic-gate 	dt_module_syminit64,
4080Sstevel@tonic-gate 	dt_module_symsort64,
4090Sstevel@tonic-gate 	dt_module_symname64,
4100Sstevel@tonic-gate 	dt_module_symaddr64
4110Sstevel@tonic-gate };
4120Sstevel@tonic-gate 
4130Sstevel@tonic-gate dt_module_t *
4140Sstevel@tonic-gate dt_module_create(dtrace_hdl_t *dtp, const char *name)
4150Sstevel@tonic-gate {
4160Sstevel@tonic-gate 	uint_t h = dt_strtab_hash(name, NULL) % dtp->dt_modbuckets;
4170Sstevel@tonic-gate 	dt_module_t *dmp;
4180Sstevel@tonic-gate 
4190Sstevel@tonic-gate 	for (dmp = dtp->dt_mods[h]; dmp != NULL; dmp = dmp->dm_next) {
4200Sstevel@tonic-gate 		if (strcmp(dmp->dm_name, name) == 0)
4210Sstevel@tonic-gate 			return (dmp);
4220Sstevel@tonic-gate 	}
4230Sstevel@tonic-gate 
4240Sstevel@tonic-gate 	if ((dmp = malloc(sizeof (dt_module_t))) == NULL)
4250Sstevel@tonic-gate 		return (NULL); /* caller must handle allocation failure */
4260Sstevel@tonic-gate 
4270Sstevel@tonic-gate 	bzero(dmp, sizeof (dt_module_t));
4280Sstevel@tonic-gate 	(void) strlcpy(dmp->dm_name, name, sizeof (dmp->dm_name));
4290Sstevel@tonic-gate 	dt_list_append(&dtp->dt_modlist, dmp);
4300Sstevel@tonic-gate 	dmp->dm_next = dtp->dt_mods[h];
4310Sstevel@tonic-gate 	dtp->dt_mods[h] = dmp;
4320Sstevel@tonic-gate 	dtp->dt_nmods++;
4330Sstevel@tonic-gate 
4340Sstevel@tonic-gate 	if (dtp->dt_conf.dtc_ctfmodel == CTF_MODEL_LP64)
4350Sstevel@tonic-gate 		dmp->dm_ops = &dt_modops_64;
4360Sstevel@tonic-gate 	else
4370Sstevel@tonic-gate 		dmp->dm_ops = &dt_modops_32;
4380Sstevel@tonic-gate 
4390Sstevel@tonic-gate 	return (dmp);
4400Sstevel@tonic-gate }
4410Sstevel@tonic-gate 
4420Sstevel@tonic-gate dt_module_t *
4430Sstevel@tonic-gate dt_module_lookup_by_name(dtrace_hdl_t *dtp, const char *name)
4440Sstevel@tonic-gate {
4450Sstevel@tonic-gate 	uint_t h = dt_strtab_hash(name, NULL) % dtp->dt_modbuckets;
4460Sstevel@tonic-gate 	dt_module_t *dmp;
4470Sstevel@tonic-gate 
4480Sstevel@tonic-gate 	for (dmp = dtp->dt_mods[h]; dmp != NULL; dmp = dmp->dm_next) {
4490Sstevel@tonic-gate 		if (strcmp(dmp->dm_name, name) == 0)
4500Sstevel@tonic-gate 			return (dmp);
4510Sstevel@tonic-gate 	}
4520Sstevel@tonic-gate 
4530Sstevel@tonic-gate 	return (NULL);
4540Sstevel@tonic-gate }
4550Sstevel@tonic-gate 
4560Sstevel@tonic-gate /*ARGSUSED*/
4570Sstevel@tonic-gate dt_module_t *
4580Sstevel@tonic-gate dt_module_lookup_by_ctf(dtrace_hdl_t *dtp, ctf_file_t *ctfp)
4590Sstevel@tonic-gate {
4600Sstevel@tonic-gate 	return (ctfp ? ctf_getspecific(ctfp) : NULL);
4610Sstevel@tonic-gate }
4620Sstevel@tonic-gate 
4630Sstevel@tonic-gate static int
4640Sstevel@tonic-gate dt_module_load_sect(dtrace_hdl_t *dtp, dt_module_t *dmp, ctf_sect_t *ctsp)
4650Sstevel@tonic-gate {
4660Sstevel@tonic-gate 	const char *s;
4670Sstevel@tonic-gate 	size_t shstrs;
4680Sstevel@tonic-gate 	GElf_Shdr sh;
4690Sstevel@tonic-gate 	Elf_Data *dp;
4700Sstevel@tonic-gate 	Elf_Scn *sp;
4710Sstevel@tonic-gate 
4720Sstevel@tonic-gate 	if (elf_getshstrndx(dmp->dm_elf, &shstrs) == 0)
4730Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_NOTLOADED));
4740Sstevel@tonic-gate 
4750Sstevel@tonic-gate 	for (sp = NULL; (sp = elf_nextscn(dmp->dm_elf, sp)) != NULL; ) {
4760Sstevel@tonic-gate 		if (gelf_getshdr(sp, &sh) == NULL || sh.sh_type == SHT_NULL ||
4770Sstevel@tonic-gate 		    (s = elf_strptr(dmp->dm_elf, shstrs, sh.sh_name)) == NULL)
4780Sstevel@tonic-gate 			continue; /* skip any malformed sections */
4790Sstevel@tonic-gate 
4800Sstevel@tonic-gate 		if (sh.sh_type == ctsp->cts_type &&
4810Sstevel@tonic-gate 		    sh.sh_entsize == ctsp->cts_entsize &&
4820Sstevel@tonic-gate 		    strcmp(s, ctsp->cts_name) == 0)
4830Sstevel@tonic-gate 			break; /* section matches specification */
4840Sstevel@tonic-gate 	}
4850Sstevel@tonic-gate 
4860Sstevel@tonic-gate 	/*
4870Sstevel@tonic-gate 	 * If the section isn't found, return success but leave cts_data set
4880Sstevel@tonic-gate 	 * to NULL and cts_size set to zero for our caller.
4890Sstevel@tonic-gate 	 */
4900Sstevel@tonic-gate 	if (sp == NULL || (dp = elf_getdata(sp, NULL)) == NULL)
4910Sstevel@tonic-gate 		return (0);
4920Sstevel@tonic-gate 
4930Sstevel@tonic-gate 	ctsp->cts_data = dp->d_buf;
4940Sstevel@tonic-gate 	ctsp->cts_size = dp->d_size;
4950Sstevel@tonic-gate 
4960Sstevel@tonic-gate 	dt_dprintf("loaded %s [%s] (%lu bytes)\n",
4970Sstevel@tonic-gate 	    dmp->dm_name, ctsp->cts_name, (ulong_t)ctsp->cts_size);
4980Sstevel@tonic-gate 
4990Sstevel@tonic-gate 	return (0);
5000Sstevel@tonic-gate }
5010Sstevel@tonic-gate 
5020Sstevel@tonic-gate int
5030Sstevel@tonic-gate dt_module_load(dtrace_hdl_t *dtp, dt_module_t *dmp)
5040Sstevel@tonic-gate {
5050Sstevel@tonic-gate 	if (dmp->dm_flags & DT_DM_LOADED)
5060Sstevel@tonic-gate 		return (0); /* module is already loaded */
5070Sstevel@tonic-gate 
5080Sstevel@tonic-gate 	dmp->dm_ctdata.cts_name = ".SUNW_ctf";
5090Sstevel@tonic-gate 	dmp->dm_ctdata.cts_type = SHT_PROGBITS;
5100Sstevel@tonic-gate 	dmp->dm_ctdata.cts_flags = 0;
5110Sstevel@tonic-gate 	dmp->dm_ctdata.cts_data = NULL;
5120Sstevel@tonic-gate 	dmp->dm_ctdata.cts_size = 0;
5130Sstevel@tonic-gate 	dmp->dm_ctdata.cts_entsize = 0;
5140Sstevel@tonic-gate 	dmp->dm_ctdata.cts_offset = 0;
5150Sstevel@tonic-gate 
5160Sstevel@tonic-gate 	dmp->dm_symtab.cts_name = ".symtab";
5170Sstevel@tonic-gate 	dmp->dm_symtab.cts_type = SHT_SYMTAB;
5180Sstevel@tonic-gate 	dmp->dm_symtab.cts_flags = 0;
5190Sstevel@tonic-gate 	dmp->dm_symtab.cts_data = NULL;
5200Sstevel@tonic-gate 	dmp->dm_symtab.cts_size = 0;
5210Sstevel@tonic-gate 	dmp->dm_symtab.cts_entsize = dmp->dm_ops == &dt_modops_64 ?
5220Sstevel@tonic-gate 	    sizeof (Elf64_Sym) : sizeof (Elf32_Sym);
5230Sstevel@tonic-gate 	dmp->dm_symtab.cts_offset = 0;
5240Sstevel@tonic-gate 
5250Sstevel@tonic-gate 	dmp->dm_strtab.cts_name = ".strtab";
5260Sstevel@tonic-gate 	dmp->dm_strtab.cts_type = SHT_STRTAB;
5270Sstevel@tonic-gate 	dmp->dm_strtab.cts_flags = 0;
5280Sstevel@tonic-gate 	dmp->dm_strtab.cts_data = NULL;
5290Sstevel@tonic-gate 	dmp->dm_strtab.cts_size = 0;
5300Sstevel@tonic-gate 	dmp->dm_strtab.cts_entsize = 0;
5310Sstevel@tonic-gate 	dmp->dm_strtab.cts_offset = 0;
5320Sstevel@tonic-gate 
5330Sstevel@tonic-gate 	/*
5340Sstevel@tonic-gate 	 * Attempt to load the module's CTF section, symbol table section, and
5350Sstevel@tonic-gate 	 * string table section.  Note that modules may not contain CTF data:
5360Sstevel@tonic-gate 	 * this will result in a successful load_sect but data of size zero.
5370Sstevel@tonic-gate 	 * We will then fail if dt_module_getctf() is called, as shown below.
5380Sstevel@tonic-gate 	 */
5390Sstevel@tonic-gate 	if (dt_module_load_sect(dtp, dmp, &dmp->dm_ctdata) == -1 ||
5400Sstevel@tonic-gate 	    dt_module_load_sect(dtp, dmp, &dmp->dm_symtab) == -1 ||
5410Sstevel@tonic-gate 	    dt_module_load_sect(dtp, dmp, &dmp->dm_strtab) == -1) {
5420Sstevel@tonic-gate 		dt_module_unload(dtp, dmp);
5430Sstevel@tonic-gate 		return (-1); /* dt_errno is set for us */
5440Sstevel@tonic-gate 	}
5450Sstevel@tonic-gate 
5460Sstevel@tonic-gate 	/*
5470Sstevel@tonic-gate 	 * Allocate the hash chains and hash buckets for symbol name lookup.
5480Sstevel@tonic-gate 	 * This is relatively simple since the symbol table is of fixed size
5490Sstevel@tonic-gate 	 * and is known in advance.  We allocate one extra element since we
5500Sstevel@tonic-gate 	 * use element indices instead of pointers and zero is our sentinel.
5510Sstevel@tonic-gate 	 */
5520Sstevel@tonic-gate 	dmp->dm_nsymelems =
5530Sstevel@tonic-gate 	    dmp->dm_symtab.cts_size / dmp->dm_symtab.cts_entsize;
5540Sstevel@tonic-gate 
5550Sstevel@tonic-gate 	dmp->dm_nsymbuckets = _dtrace_strbuckets;
5560Sstevel@tonic-gate 	dmp->dm_symfree = 1;		/* first free element is index 1 */
5570Sstevel@tonic-gate 
5580Sstevel@tonic-gate 	dmp->dm_symbuckets = malloc(sizeof (uint_t) * dmp->dm_nsymbuckets);
5590Sstevel@tonic-gate 	dmp->dm_symchains = malloc(sizeof (dt_sym_t) * dmp->dm_nsymelems + 1);
5600Sstevel@tonic-gate 
5610Sstevel@tonic-gate 	if (dmp->dm_symbuckets == NULL || dmp->dm_symchains == NULL) {
5620Sstevel@tonic-gate 		dt_module_unload(dtp, dmp);
5630Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_NOMEM));
5640Sstevel@tonic-gate 	}
5650Sstevel@tonic-gate 
5660Sstevel@tonic-gate 	bzero(dmp->dm_symbuckets, sizeof (uint_t) * dmp->dm_nsymbuckets);
5670Sstevel@tonic-gate 	bzero(dmp->dm_symchains, sizeof (dt_sym_t) * dmp->dm_nsymelems + 1);
5680Sstevel@tonic-gate 
5690Sstevel@tonic-gate 	/*
5700Sstevel@tonic-gate 	 * Iterate over the symbol table data buffer and insert each symbol
5710Sstevel@tonic-gate 	 * name into the name hash if the name and type are valid.  Then
5720Sstevel@tonic-gate 	 * allocate the address map, fill it in, and sort it.
5730Sstevel@tonic-gate 	 */
5740Sstevel@tonic-gate 	dmp->dm_asrsv = dmp->dm_ops->do_syminit(dmp);
5750Sstevel@tonic-gate 
5760Sstevel@tonic-gate 	dt_dprintf("hashed %s [%s] (%u symbols)\n",
5770Sstevel@tonic-gate 	    dmp->dm_name, dmp->dm_symtab.cts_name, dmp->dm_symfree - 1);
5780Sstevel@tonic-gate 
5790Sstevel@tonic-gate 	if ((dmp->dm_asmap = malloc(sizeof (void *) * dmp->dm_asrsv)) == NULL) {
5800Sstevel@tonic-gate 		dt_module_unload(dtp, dmp);
5810Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_NOMEM));
5820Sstevel@tonic-gate 	}
5830Sstevel@tonic-gate 
5840Sstevel@tonic-gate 	dmp->dm_ops->do_symsort(dmp);
5850Sstevel@tonic-gate 
5860Sstevel@tonic-gate 	dt_dprintf("sorted %s [%s] (%u symbols)\n",
5870Sstevel@tonic-gate 	    dmp->dm_name, dmp->dm_symtab.cts_name, dmp->dm_aslen);
5880Sstevel@tonic-gate 
5890Sstevel@tonic-gate 	dmp->dm_flags |= DT_DM_LOADED;
5900Sstevel@tonic-gate 	return (0);
5910Sstevel@tonic-gate }
5920Sstevel@tonic-gate 
5930Sstevel@tonic-gate ctf_file_t *
5940Sstevel@tonic-gate dt_module_getctf(dtrace_hdl_t *dtp, dt_module_t *dmp)
5950Sstevel@tonic-gate {
5960Sstevel@tonic-gate 	const char *parent;
5970Sstevel@tonic-gate 	dt_module_t *pmp;
5980Sstevel@tonic-gate 	ctf_file_t *pfp;
5990Sstevel@tonic-gate 	int model;
6000Sstevel@tonic-gate 
6010Sstevel@tonic-gate 	if (dmp->dm_ctfp != NULL || dt_module_load(dtp, dmp) != 0)
6020Sstevel@tonic-gate 		return (dmp->dm_ctfp);
6030Sstevel@tonic-gate 
6040Sstevel@tonic-gate 	if (dmp->dm_ops == &dt_modops_64)
6050Sstevel@tonic-gate 		model = CTF_MODEL_LP64;
6060Sstevel@tonic-gate 	else
6070Sstevel@tonic-gate 		model = CTF_MODEL_ILP32;
6080Sstevel@tonic-gate 
6090Sstevel@tonic-gate 	/*
6100Sstevel@tonic-gate 	 * If the data model of the module does not match our program data
6110Sstevel@tonic-gate 	 * model, then do not permit CTF from this module to be opened and
6120Sstevel@tonic-gate 	 * returned to the compiler.  If we support mixed data models in the
6130Sstevel@tonic-gate 	 * future for combined kernel/user tracing, this can be removed.
6140Sstevel@tonic-gate 	 */
6150Sstevel@tonic-gate 	if (dtp->dt_conf.dtc_ctfmodel != model) {
6160Sstevel@tonic-gate 		(void) dt_set_errno(dtp, EDT_DATAMODEL);
6170Sstevel@tonic-gate 		return (NULL);
6180Sstevel@tonic-gate 	}
6190Sstevel@tonic-gate 
6200Sstevel@tonic-gate 	if (dmp->dm_ctdata.cts_size == 0) {
6210Sstevel@tonic-gate 		(void) dt_set_errno(dtp, EDT_NOCTF);
6220Sstevel@tonic-gate 		return (NULL);
6230Sstevel@tonic-gate 	}
6240Sstevel@tonic-gate 
6250Sstevel@tonic-gate 	dmp->dm_ctfp = ctf_bufopen(&dmp->dm_ctdata,
6260Sstevel@tonic-gate 	    &dmp->dm_symtab, &dmp->dm_strtab, &dtp->dt_ctferr);
6270Sstevel@tonic-gate 
6280Sstevel@tonic-gate 	if (dmp->dm_ctfp == NULL) {
6290Sstevel@tonic-gate 		(void) dt_set_errno(dtp, EDT_CTF);
6300Sstevel@tonic-gate 		return (NULL);
6310Sstevel@tonic-gate 	}
6320Sstevel@tonic-gate 
6330Sstevel@tonic-gate 	(void) ctf_setmodel(dmp->dm_ctfp, model);
6340Sstevel@tonic-gate 	ctf_setspecific(dmp->dm_ctfp, dmp);
6350Sstevel@tonic-gate 
6360Sstevel@tonic-gate 	if ((parent = ctf_parent_name(dmp->dm_ctfp)) != NULL) {
6370Sstevel@tonic-gate 		if ((pmp = dt_module_create(dtp, parent)) == NULL ||
6380Sstevel@tonic-gate 		    (pfp = dt_module_getctf(dtp, pmp)) == NULL) {
6390Sstevel@tonic-gate 			if (pmp == NULL)
6400Sstevel@tonic-gate 				(void) dt_set_errno(dtp, EDT_NOMEM);
6410Sstevel@tonic-gate 			goto err;
6420Sstevel@tonic-gate 		}
6430Sstevel@tonic-gate 
6440Sstevel@tonic-gate 		if (ctf_import(dmp->dm_ctfp, pfp) == CTF_ERR) {
6450Sstevel@tonic-gate 			dtp->dt_ctferr = ctf_errno(dmp->dm_ctfp);
6460Sstevel@tonic-gate 			(void) dt_set_errno(dtp, EDT_CTF);
6470Sstevel@tonic-gate 			goto err;
6480Sstevel@tonic-gate 		}
6490Sstevel@tonic-gate 	}
6500Sstevel@tonic-gate 
6510Sstevel@tonic-gate 	dt_dprintf("loaded CTF container for %s (%p)\n",
6520Sstevel@tonic-gate 	    dmp->dm_name, (void *)dmp->dm_ctfp);
6530Sstevel@tonic-gate 
6540Sstevel@tonic-gate 	return (dmp->dm_ctfp);
6550Sstevel@tonic-gate 
6560Sstevel@tonic-gate err:
6570Sstevel@tonic-gate 	ctf_close(dmp->dm_ctfp);
6580Sstevel@tonic-gate 	dmp->dm_ctfp = NULL;
6590Sstevel@tonic-gate 	return (NULL);
6600Sstevel@tonic-gate }
6610Sstevel@tonic-gate 
6620Sstevel@tonic-gate /*ARGSUSED*/
6630Sstevel@tonic-gate void
6640Sstevel@tonic-gate dt_module_unload(dtrace_hdl_t *dtp, dt_module_t *dmp)
6650Sstevel@tonic-gate {
6660Sstevel@tonic-gate 	ctf_close(dmp->dm_ctfp);
6670Sstevel@tonic-gate 	dmp->dm_ctfp = NULL;
6680Sstevel@tonic-gate 
6690Sstevel@tonic-gate 	bzero(&dmp->dm_ctdata, sizeof (ctf_sect_t));
6700Sstevel@tonic-gate 	bzero(&dmp->dm_symtab, sizeof (ctf_sect_t));
6710Sstevel@tonic-gate 	bzero(&dmp->dm_strtab, sizeof (ctf_sect_t));
6720Sstevel@tonic-gate 
6730Sstevel@tonic-gate 	if (dmp->dm_symbuckets != NULL) {
6740Sstevel@tonic-gate 		free(dmp->dm_symbuckets);
6750Sstevel@tonic-gate 		dmp->dm_symbuckets = NULL;
6760Sstevel@tonic-gate 	}
6770Sstevel@tonic-gate 
6780Sstevel@tonic-gate 	if (dmp->dm_symchains != NULL) {
6790Sstevel@tonic-gate 		free(dmp->dm_symchains);
6800Sstevel@tonic-gate 		dmp->dm_symchains = NULL;
6810Sstevel@tonic-gate 	}
6820Sstevel@tonic-gate 
6830Sstevel@tonic-gate 	if (dmp->dm_asmap != NULL) {
6840Sstevel@tonic-gate 		free(dmp->dm_asmap);
6850Sstevel@tonic-gate 		dmp->dm_asmap = NULL;
6860Sstevel@tonic-gate 	}
6870Sstevel@tonic-gate 
6880Sstevel@tonic-gate 	dmp->dm_symfree = 0;
6890Sstevel@tonic-gate 	dmp->dm_nsymbuckets = 0;
6900Sstevel@tonic-gate 	dmp->dm_nsymelems = 0;
6910Sstevel@tonic-gate 	dmp->dm_asrsv = 0;
6920Sstevel@tonic-gate 	dmp->dm_aslen = 0;
6930Sstevel@tonic-gate 
6940Sstevel@tonic-gate 	dmp->dm_text_va = NULL;
6950Sstevel@tonic-gate 	dmp->dm_text_size = 0;
6960Sstevel@tonic-gate 	dmp->dm_data_va = NULL;
6970Sstevel@tonic-gate 	dmp->dm_data_size = 0;
6980Sstevel@tonic-gate 	dmp->dm_bss_va = NULL;
6990Sstevel@tonic-gate 	dmp->dm_bss_size = 0;
7000Sstevel@tonic-gate 
7010Sstevel@tonic-gate 	if (dmp->dm_extern != NULL) {
7020Sstevel@tonic-gate 		dt_idhash_destroy(dmp->dm_extern);
7030Sstevel@tonic-gate 		dmp->dm_extern = NULL;
7040Sstevel@tonic-gate 	}
7050Sstevel@tonic-gate 
7060Sstevel@tonic-gate 	(void) elf_end(dmp->dm_elf);
7070Sstevel@tonic-gate 	dmp->dm_elf = NULL;
7080Sstevel@tonic-gate 
7090Sstevel@tonic-gate 	dmp->dm_flags &= ~DT_DM_LOADED;
7100Sstevel@tonic-gate }
7110Sstevel@tonic-gate 
7120Sstevel@tonic-gate void
7130Sstevel@tonic-gate dt_module_destroy(dtrace_hdl_t *dtp, dt_module_t *dmp)
7140Sstevel@tonic-gate {
7150Sstevel@tonic-gate 	dt_list_delete(&dtp->dt_modlist, dmp);
7160Sstevel@tonic-gate 	assert(dtp->dt_nmods != 0);
7170Sstevel@tonic-gate 	dtp->dt_nmods--;
7180Sstevel@tonic-gate 
7190Sstevel@tonic-gate 	dt_module_unload(dtp, dmp);
7200Sstevel@tonic-gate 	free(dmp);
7210Sstevel@tonic-gate }
7220Sstevel@tonic-gate 
7230Sstevel@tonic-gate /*
7240Sstevel@tonic-gate  * Insert a new external symbol reference into the specified module.  The new
7250Sstevel@tonic-gate  * symbol will be marked as undefined and is assigned a symbol index beyond
7260Sstevel@tonic-gate  * any existing cached symbols from this module.  We use the ident's di_data
7270Sstevel@tonic-gate  * field to store a pointer to a copy of the dtrace_syminfo_t for this symbol.
7280Sstevel@tonic-gate  */
7290Sstevel@tonic-gate dt_ident_t *
7300Sstevel@tonic-gate dt_module_extern(dtrace_hdl_t *dtp, dt_module_t *dmp,
7310Sstevel@tonic-gate     const char *name, const dtrace_typeinfo_t *tip)
7320Sstevel@tonic-gate {
7330Sstevel@tonic-gate 	dtrace_syminfo_t *sip;
7340Sstevel@tonic-gate 	dt_ident_t *idp;
7350Sstevel@tonic-gate 	uint_t id;
7360Sstevel@tonic-gate 
7370Sstevel@tonic-gate 	if (dmp->dm_extern == NULL && (dmp->dm_extern = dt_idhash_create(
7380Sstevel@tonic-gate 	    "extern", NULL, dmp->dm_nsymelems, UINT_MAX)) == NULL) {
7390Sstevel@tonic-gate 		(void) dt_set_errno(dtp, EDT_NOMEM);
7400Sstevel@tonic-gate 		return (NULL);
7410Sstevel@tonic-gate 	}
7420Sstevel@tonic-gate 
7430Sstevel@tonic-gate 	if (dt_idhash_nextid(dmp->dm_extern, &id) == -1) {
7440Sstevel@tonic-gate 		(void) dt_set_errno(dtp, EDT_SYMOFLOW);
7450Sstevel@tonic-gate 		return (NULL);
7460Sstevel@tonic-gate 	}
7470Sstevel@tonic-gate 
7480Sstevel@tonic-gate 	if ((sip = malloc(sizeof (dtrace_syminfo_t))) == NULL) {
7490Sstevel@tonic-gate 		(void) dt_set_errno(dtp, EDT_NOMEM);
7500Sstevel@tonic-gate 		return (NULL);
7510Sstevel@tonic-gate 	}
7520Sstevel@tonic-gate 
7530Sstevel@tonic-gate 	idp = dt_idhash_insert(dmp->dm_extern, name, DT_IDENT_SYMBOL, 0, id,
7540Sstevel@tonic-gate 	    _dtrace_symattr, 0, &dt_idops_thaw, NULL, dtp->dt_gen);
7550Sstevel@tonic-gate 
7560Sstevel@tonic-gate 	if (idp == NULL) {
7570Sstevel@tonic-gate 		(void) dt_set_errno(dtp, EDT_NOMEM);
7580Sstevel@tonic-gate 		free(sip);
7590Sstevel@tonic-gate 		return (NULL);
7600Sstevel@tonic-gate 	}
7610Sstevel@tonic-gate 
7620Sstevel@tonic-gate 	sip->dts_object = dmp->dm_name;
7630Sstevel@tonic-gate 	sip->dts_name = idp->di_name;
7640Sstevel@tonic-gate 	sip->dts_id = idp->di_id;
7650Sstevel@tonic-gate 
7660Sstevel@tonic-gate 	idp->di_data = sip;
7670Sstevel@tonic-gate 	idp->di_ctfp = tip->dtt_ctfp;
7680Sstevel@tonic-gate 	idp->di_type = tip->dtt_type;
7690Sstevel@tonic-gate 
7700Sstevel@tonic-gate 	return (idp);
7710Sstevel@tonic-gate }
7720Sstevel@tonic-gate 
7730Sstevel@tonic-gate const char *
7740Sstevel@tonic-gate dt_module_modelname(dt_module_t *dmp)
7750Sstevel@tonic-gate {
7760Sstevel@tonic-gate 	if (dmp->dm_ops == &dt_modops_64)
7770Sstevel@tonic-gate 		return ("64-bit");
7780Sstevel@tonic-gate 	else
7790Sstevel@tonic-gate 		return ("32-bit");
7800Sstevel@tonic-gate }
7810Sstevel@tonic-gate 
7820Sstevel@tonic-gate /*
7830Sstevel@tonic-gate  * Update our module cache by adding an entry for the specified module 'name'.
7840Sstevel@tonic-gate  * We create the dt_module_t and populate it using /system/object/<name>/.
7850Sstevel@tonic-gate  */
7860Sstevel@tonic-gate static void
7870Sstevel@tonic-gate dt_module_update(dtrace_hdl_t *dtp, const char *name)
7880Sstevel@tonic-gate {
7890Sstevel@tonic-gate 	char fname[MAXPATHLEN];
7900Sstevel@tonic-gate 	struct stat64 st;
7910Sstevel@tonic-gate 	int fd, err, bits;
7920Sstevel@tonic-gate 
7930Sstevel@tonic-gate 	dt_module_t *dmp;
7940Sstevel@tonic-gate 	const char *s;
7950Sstevel@tonic-gate 	size_t shstrs;
7960Sstevel@tonic-gate 	GElf_Shdr sh;
7970Sstevel@tonic-gate 	Elf_Data *dp;
7980Sstevel@tonic-gate 	Elf_Scn *sp;
7990Sstevel@tonic-gate 
8000Sstevel@tonic-gate 	(void) snprintf(fname, sizeof (fname),
8010Sstevel@tonic-gate 	    "%s/%s/object", OBJFS_ROOT, name);
8020Sstevel@tonic-gate 
8030Sstevel@tonic-gate 	if ((fd = open(fname, O_RDONLY)) == -1 || fstat64(fd, &st) == -1 ||
8040Sstevel@tonic-gate 	    (dmp = dt_module_create(dtp, name)) == NULL) {
8050Sstevel@tonic-gate 		dt_dprintf("failed to open %s: %s\n", fname, strerror(errno));
8060Sstevel@tonic-gate 		(void) close(fd);
8070Sstevel@tonic-gate 		return;
8080Sstevel@tonic-gate 	}
8090Sstevel@tonic-gate 
8100Sstevel@tonic-gate 	/*
8110Sstevel@tonic-gate 	 * Since the module can unload out from under us (and /system/object
8120Sstevel@tonic-gate 	 * will return ENOENT), tell libelf to cook the entire file now and
8130Sstevel@tonic-gate 	 * then close the underlying file descriptor immediately.  If this
8140Sstevel@tonic-gate 	 * succeeds, we know that we can continue safely using dmp->dm_elf.
8150Sstevel@tonic-gate 	 */
8160Sstevel@tonic-gate 	dmp->dm_elf = elf_begin(fd, ELF_C_READ, NULL);
8170Sstevel@tonic-gate 	err = elf_cntl(dmp->dm_elf, ELF_C_FDREAD);
8180Sstevel@tonic-gate 	(void) close(fd);
8190Sstevel@tonic-gate 
8200Sstevel@tonic-gate 	if (dmp->dm_elf == NULL || err == -1 ||
8210Sstevel@tonic-gate 	    elf_getshstrndx(dmp->dm_elf, &shstrs) == 0) {
8220Sstevel@tonic-gate 		dt_dprintf("failed to load %s: %s\n",
8230Sstevel@tonic-gate 		    fname, elf_errmsg(elf_errno()));
8240Sstevel@tonic-gate 		dt_module_destroy(dtp, dmp);
8250Sstevel@tonic-gate 		return;
8260Sstevel@tonic-gate 	}
8270Sstevel@tonic-gate 
8280Sstevel@tonic-gate 	switch (gelf_getclass(dmp->dm_elf)) {
8290Sstevel@tonic-gate 	case ELFCLASS32:
8300Sstevel@tonic-gate 		dmp->dm_ops = &dt_modops_32;
8310Sstevel@tonic-gate 		bits = 32;
8320Sstevel@tonic-gate 		break;
8330Sstevel@tonic-gate 	case ELFCLASS64:
8340Sstevel@tonic-gate 		dmp->dm_ops = &dt_modops_64;
8350Sstevel@tonic-gate 		bits = 64;
8360Sstevel@tonic-gate 		break;
8370Sstevel@tonic-gate 	default:
8380Sstevel@tonic-gate 		dt_dprintf("failed to load %s: unknown ELF class\n", fname);
8390Sstevel@tonic-gate 		dt_module_destroy(dtp, dmp);
8400Sstevel@tonic-gate 		return;
8410Sstevel@tonic-gate 	}
8420Sstevel@tonic-gate 
8430Sstevel@tonic-gate 	/*
8440Sstevel@tonic-gate 	 * Iterate over the section headers locating various sections of
8450Sstevel@tonic-gate 	 * interest and use their attributes to flesh out the dt_module_t.
8460Sstevel@tonic-gate 	 */
8470Sstevel@tonic-gate 	for (sp = NULL; (sp = elf_nextscn(dmp->dm_elf, sp)) != NULL; ) {
8480Sstevel@tonic-gate 		if (gelf_getshdr(sp, &sh) == NULL || sh.sh_type == SHT_NULL ||
8490Sstevel@tonic-gate 		    (s = elf_strptr(dmp->dm_elf, shstrs, sh.sh_name)) == NULL)
8500Sstevel@tonic-gate 			continue; /* skip any malformed sections */
8510Sstevel@tonic-gate 
8520Sstevel@tonic-gate 		if (strcmp(s, ".text") == 0) {
8530Sstevel@tonic-gate 			dmp->dm_text_size = sh.sh_size;
8540Sstevel@tonic-gate 			dmp->dm_text_va = sh.sh_addr;
8550Sstevel@tonic-gate 		} else if (strcmp(s, ".data") == 0) {
8560Sstevel@tonic-gate 			dmp->dm_data_size = sh.sh_size;
8570Sstevel@tonic-gate 			dmp->dm_data_va = sh.sh_addr;
8580Sstevel@tonic-gate 		} else if (strcmp(s, ".bss") == 0) {
8590Sstevel@tonic-gate 			dmp->dm_bss_size = sh.sh_size;
8600Sstevel@tonic-gate 			dmp->dm_bss_va = sh.sh_addr;
8610Sstevel@tonic-gate 		} else if (strcmp(s, ".info") == 0 &&
8620Sstevel@tonic-gate 		    (dp = elf_getdata(sp, NULL)) != NULL) {
8630Sstevel@tonic-gate 			bcopy(dp->d_buf, &dmp->dm_info,
8640Sstevel@tonic-gate 			    MIN(sh.sh_size, sizeof (dmp->dm_info)));
8650Sstevel@tonic-gate 		} else if (strcmp(s, ".filename") == 0 &&
8660Sstevel@tonic-gate 		    (dp = elf_getdata(sp, NULL)) != NULL) {
8670Sstevel@tonic-gate 			(void) strlcpy(dmp->dm_file,
8680Sstevel@tonic-gate 			    dp->d_buf, sizeof (dmp->dm_file));
8690Sstevel@tonic-gate 		}
8700Sstevel@tonic-gate 	}
8710Sstevel@tonic-gate 
8720Sstevel@tonic-gate 	dmp->dm_flags |= DT_DM_KERNEL;
8730Sstevel@tonic-gate 	dmp->dm_modid = (int)OBJFS_MODID(st.st_ino);
8740Sstevel@tonic-gate 
8750Sstevel@tonic-gate 	if (dmp->dm_info.objfs_info_primary)
8760Sstevel@tonic-gate 		dmp->dm_flags |= DT_DM_PRIMARY;
8770Sstevel@tonic-gate 
8780Sstevel@tonic-gate 	dt_dprintf("opened %d-bit module %s (%s) [%d]\n",
8790Sstevel@tonic-gate 	    bits, dmp->dm_name, dmp->dm_file, dmp->dm_modid);
8800Sstevel@tonic-gate }
8810Sstevel@tonic-gate 
8820Sstevel@tonic-gate /*
8830Sstevel@tonic-gate  * Unload all the loaded modules and then refresh the module cache with the
8840Sstevel@tonic-gate  * latest list of loaded modules and their address ranges.
8850Sstevel@tonic-gate  */
8860Sstevel@tonic-gate void
8870Sstevel@tonic-gate dtrace_update(dtrace_hdl_t *dtp)
8880Sstevel@tonic-gate {
8890Sstevel@tonic-gate 	dt_module_t *dmp;
8900Sstevel@tonic-gate 	DIR *dirp;
8910Sstevel@tonic-gate 
8920Sstevel@tonic-gate 	for (dmp = dt_list_next(&dtp->dt_modlist);
8930Sstevel@tonic-gate 	    dmp != NULL; dmp = dt_list_next(dmp))
8940Sstevel@tonic-gate 		dt_module_unload(dtp, dmp);
8950Sstevel@tonic-gate 
8960Sstevel@tonic-gate 	/*
8970Sstevel@tonic-gate 	 * Open /system/object and attempt to create a libdtrace module for
8980Sstevel@tonic-gate 	 * each kernel module that is loaded on the current system.
8990Sstevel@tonic-gate 	 */
9000Sstevel@tonic-gate 	if (!(dtp->dt_oflags & DTRACE_O_NOSYS) &&
9010Sstevel@tonic-gate 	    (dirp = opendir(OBJFS_ROOT)) != NULL) {
902*871Scasper 		struct dirent *dp;
9030Sstevel@tonic-gate 
904*871Scasper 		while ((dp = readdir(dirp)) != NULL) {
9050Sstevel@tonic-gate 			if (dp->d_name[0] != '.')
9060Sstevel@tonic-gate 				dt_module_update(dtp, dp->d_name);
9070Sstevel@tonic-gate 		}
9080Sstevel@tonic-gate 
9090Sstevel@tonic-gate 		(void) closedir(dirp);
9100Sstevel@tonic-gate 	}
9110Sstevel@tonic-gate 
9120Sstevel@tonic-gate 	/*
9130Sstevel@tonic-gate 	 * Look up all the macro identifiers and set di_id to the latest value.
9140Sstevel@tonic-gate 	 * This code collaborates with dt_lex.l on the use of di_id.  We will
9150Sstevel@tonic-gate 	 * need to implement something fancier if we need to support non-ints.
9160Sstevel@tonic-gate 	 */
9170Sstevel@tonic-gate 	dt_idhash_lookup(dtp->dt_macros, "egid")->di_id = getegid();
9180Sstevel@tonic-gate 	dt_idhash_lookup(dtp->dt_macros, "euid")->di_id = geteuid();
9190Sstevel@tonic-gate 	dt_idhash_lookup(dtp->dt_macros, "gid")->di_id = getgid();
9200Sstevel@tonic-gate 	dt_idhash_lookup(dtp->dt_macros, "pid")->di_id = getpid();
9210Sstevel@tonic-gate 	dt_idhash_lookup(dtp->dt_macros, "pgid")->di_id = getpgid(0);
9220Sstevel@tonic-gate 	dt_idhash_lookup(dtp->dt_macros, "ppid")->di_id = getppid();
9230Sstevel@tonic-gate 	dt_idhash_lookup(dtp->dt_macros, "projid")->di_id = getprojid();
9240Sstevel@tonic-gate 	dt_idhash_lookup(dtp->dt_macros, "sid")->di_id = getsid(0);
9250Sstevel@tonic-gate 	dt_idhash_lookup(dtp->dt_macros, "taskid")->di_id = gettaskid();
9260Sstevel@tonic-gate 	dt_idhash_lookup(dtp->dt_macros, "uid")->di_id = getuid();
9270Sstevel@tonic-gate 
9280Sstevel@tonic-gate 	/*
9290Sstevel@tonic-gate 	 * Cache the pointers to the modules representing the base executable
9300Sstevel@tonic-gate 	 * and the run-time linker in the dtrace client handle.  We should
9310Sstevel@tonic-gate 	 * probably have a more generic way of inquiring as to their names.
9320Sstevel@tonic-gate 	 */
9330Sstevel@tonic-gate 	dtp->dt_exec = dt_module_lookup_by_name(dtp, "genunix");
9340Sstevel@tonic-gate 	dtp->dt_rtld = dt_module_lookup_by_name(dtp, "krtld");
9350Sstevel@tonic-gate 
9360Sstevel@tonic-gate 	/*
9370Sstevel@tonic-gate 	 * If this is the first time we are initializing the module list,
9380Sstevel@tonic-gate 	 * remove the module for genunix from the module list and then move it
9390Sstevel@tonic-gate 	 * to the front of the module list.  We do this so that type and symbol
9400Sstevel@tonic-gate 	 * queries encounter genunix and thereby optimize for the common case
9410Sstevel@tonic-gate 	 * in dtrace_lookup_by_name() and dtrace_lookup_by_type(), below.
9420Sstevel@tonic-gate 	 */
9430Sstevel@tonic-gate 	if (dtp->dt_exec != NULL &&
9440Sstevel@tonic-gate 	    dtp->dt_cdefs == NULL && dtp->dt_ddefs == NULL) {
9450Sstevel@tonic-gate 		dt_list_delete(&dtp->dt_modlist, dtp->dt_exec);
9460Sstevel@tonic-gate 		dt_list_prepend(&dtp->dt_modlist, dtp->dt_exec);
9470Sstevel@tonic-gate 	}
9480Sstevel@tonic-gate }
9490Sstevel@tonic-gate 
9500Sstevel@tonic-gate static dt_module_t *
9510Sstevel@tonic-gate dt_module_from_object(dtrace_hdl_t *dtp, const char *object)
9520Sstevel@tonic-gate {
9530Sstevel@tonic-gate 	int err = EDT_NOMOD;
9540Sstevel@tonic-gate 	dt_module_t *dmp;
9550Sstevel@tonic-gate 
9560Sstevel@tonic-gate 	switch ((uintptr_t)object) {
9570Sstevel@tonic-gate 	case (uintptr_t)DTRACE_OBJ_EXEC:
9580Sstevel@tonic-gate 		dmp = dtp->dt_exec;
9590Sstevel@tonic-gate 		break;
9600Sstevel@tonic-gate 	case (uintptr_t)DTRACE_OBJ_RTLD:
9610Sstevel@tonic-gate 		dmp = dtp->dt_rtld;
9620Sstevel@tonic-gate 		break;
9630Sstevel@tonic-gate 	case (uintptr_t)DTRACE_OBJ_CDEFS:
9640Sstevel@tonic-gate 		dmp = dtp->dt_cdefs;
9650Sstevel@tonic-gate 		break;
9660Sstevel@tonic-gate 	case (uintptr_t)DTRACE_OBJ_DDEFS:
9670Sstevel@tonic-gate 		dmp = dtp->dt_ddefs;
9680Sstevel@tonic-gate 		break;
9690Sstevel@tonic-gate 	default:
9700Sstevel@tonic-gate 		dmp = dt_module_create(dtp, object);
9710Sstevel@tonic-gate 		err = EDT_NOMEM;
9720Sstevel@tonic-gate 	}
9730Sstevel@tonic-gate 
9740Sstevel@tonic-gate 	if (dmp == NULL)
9750Sstevel@tonic-gate 		(void) dt_set_errno(dtp, err);
9760Sstevel@tonic-gate 
9770Sstevel@tonic-gate 	return (dmp);
9780Sstevel@tonic-gate }
9790Sstevel@tonic-gate 
9800Sstevel@tonic-gate /*
9810Sstevel@tonic-gate  * Exported interface to look up a symbol by name.  We return the GElf_Sym and
9820Sstevel@tonic-gate  * complete symbol information for the matching symbol.
9830Sstevel@tonic-gate  */
9840Sstevel@tonic-gate int
9850Sstevel@tonic-gate dtrace_lookup_by_name(dtrace_hdl_t *dtp, const char *object, const char *name,
9860Sstevel@tonic-gate     GElf_Sym *symp, dtrace_syminfo_t *sip)
9870Sstevel@tonic-gate {
9880Sstevel@tonic-gate 	dt_module_t *dmp;
9890Sstevel@tonic-gate 	dt_ident_t *idp;
9900Sstevel@tonic-gate 	uint_t n, id;
9910Sstevel@tonic-gate 	GElf_Sym sym;
9920Sstevel@tonic-gate 
9930Sstevel@tonic-gate 	uint_t mask = 0; /* mask of dt_module flags to match */
9940Sstevel@tonic-gate 	uint_t bits = 0; /* flag bits that must be present */
9950Sstevel@tonic-gate 
9960Sstevel@tonic-gate 	if (object != DTRACE_OBJ_EVERY &&
9970Sstevel@tonic-gate 	    object != DTRACE_OBJ_KMODS &&
9980Sstevel@tonic-gate 	    object != DTRACE_OBJ_UMODS) {
9990Sstevel@tonic-gate 		if ((dmp = dt_module_from_object(dtp, object)) == NULL)
10000Sstevel@tonic-gate 			return (-1); /* dt_errno is set for us */
10010Sstevel@tonic-gate 
10020Sstevel@tonic-gate 		if (dt_module_load(dtp, dmp) == -1)
10030Sstevel@tonic-gate 			return (-1); /* dt_errno is set for us */
10040Sstevel@tonic-gate 		n = 1;
10050Sstevel@tonic-gate 
10060Sstevel@tonic-gate 	} else {
10070Sstevel@tonic-gate 		if (object == DTRACE_OBJ_KMODS)
10080Sstevel@tonic-gate 			mask = bits = DT_DM_KERNEL;
10090Sstevel@tonic-gate 		else if (object == DTRACE_OBJ_UMODS)
10100Sstevel@tonic-gate 			mask = DT_DM_KERNEL;
10110Sstevel@tonic-gate 
10120Sstevel@tonic-gate 		dmp = dt_list_next(&dtp->dt_modlist);
10130Sstevel@tonic-gate 		n = dtp->dt_nmods;
10140Sstevel@tonic-gate 	}
10150Sstevel@tonic-gate 
10160Sstevel@tonic-gate 	if (symp == NULL)
10170Sstevel@tonic-gate 		symp = &sym;
10180Sstevel@tonic-gate 
10190Sstevel@tonic-gate 	for (; n > 0; n--, dmp = dt_list_next(dmp)) {
10200Sstevel@tonic-gate 		if ((dmp->dm_flags & mask) != bits)
10210Sstevel@tonic-gate 			continue; /* failed to match required attributes */
10220Sstevel@tonic-gate 
10230Sstevel@tonic-gate 		if (dt_module_load(dtp, dmp) == -1)
10240Sstevel@tonic-gate 			continue; /* failed to load symbol table */
10250Sstevel@tonic-gate 
10260Sstevel@tonic-gate 		if (dmp->dm_ops->do_symname(dmp, name, symp, &id) != NULL) {
10270Sstevel@tonic-gate 			if (sip != NULL) {
10280Sstevel@tonic-gate 				sip->dts_object = dmp->dm_name;
10290Sstevel@tonic-gate 				sip->dts_name = (const char *)
10300Sstevel@tonic-gate 				    dmp->dm_strtab.cts_data + symp->st_name;
10310Sstevel@tonic-gate 				sip->dts_id = id;
10320Sstevel@tonic-gate 			}
10330Sstevel@tonic-gate 			return (0);
10340Sstevel@tonic-gate 		}
10350Sstevel@tonic-gate 
10360Sstevel@tonic-gate 		if (dmp->dm_extern != NULL &&
10370Sstevel@tonic-gate 		    (idp = dt_idhash_lookup(dmp->dm_extern, name)) != NULL) {
10380Sstevel@tonic-gate 			if (symp != &sym) {
10390Sstevel@tonic-gate 				symp->st_name = (uintptr_t)idp->di_name;
10400Sstevel@tonic-gate 				symp->st_info =
10410Sstevel@tonic-gate 				    GELF_ST_INFO(STB_GLOBAL, STT_NOTYPE);
10420Sstevel@tonic-gate 				symp->st_other = 0;
10430Sstevel@tonic-gate 				symp->st_shndx = SHN_UNDEF;
10440Sstevel@tonic-gate 				symp->st_value = 0;
10450Sstevel@tonic-gate 				symp->st_size =
10460Sstevel@tonic-gate 				    ctf_type_size(idp->di_ctfp, idp->di_type);
10470Sstevel@tonic-gate 			}
10480Sstevel@tonic-gate 
10490Sstevel@tonic-gate 			if (sip != NULL) {
10500Sstevel@tonic-gate 				sip->dts_object = dmp->dm_name;
10510Sstevel@tonic-gate 				sip->dts_name = idp->di_name;
10520Sstevel@tonic-gate 				sip->dts_id = idp->di_id;
10530Sstevel@tonic-gate 			}
10540Sstevel@tonic-gate 
10550Sstevel@tonic-gate 			return (0);
10560Sstevel@tonic-gate 		}
10570Sstevel@tonic-gate 	}
10580Sstevel@tonic-gate 
10590Sstevel@tonic-gate 	return (dt_set_errno(dtp, EDT_NOSYM));
10600Sstevel@tonic-gate }
10610Sstevel@tonic-gate 
10620Sstevel@tonic-gate /*
10630Sstevel@tonic-gate  * Exported interface to look up a symbol by address.  We return the GElf_Sym
10640Sstevel@tonic-gate  * and complete symbol information for the matching symbol.
10650Sstevel@tonic-gate  */
10660Sstevel@tonic-gate int
10670Sstevel@tonic-gate dtrace_lookup_by_addr(dtrace_hdl_t *dtp, GElf_Addr addr,
10680Sstevel@tonic-gate     GElf_Sym *symp, dtrace_syminfo_t *sip)
10690Sstevel@tonic-gate {
10700Sstevel@tonic-gate 	dt_module_t *dmp;
10710Sstevel@tonic-gate 	uint_t id;
10720Sstevel@tonic-gate 	const dtrace_vector_t *v = dtp->dt_vector;
10730Sstevel@tonic-gate 
10740Sstevel@tonic-gate 	if (v != NULL)
10750Sstevel@tonic-gate 		return (v->dtv_lookup_by_addr(dtp->dt_varg, addr, symp, sip));
10760Sstevel@tonic-gate 
10770Sstevel@tonic-gate 	for (dmp = dt_list_next(&dtp->dt_modlist); dmp != NULL;
10780Sstevel@tonic-gate 	    dmp = dt_list_next(dmp)) {
10790Sstevel@tonic-gate 		if (addr - dmp->dm_text_va < dmp->dm_text_size ||
10800Sstevel@tonic-gate 		    addr - dmp->dm_data_va < dmp->dm_data_size ||
10810Sstevel@tonic-gate 		    addr - dmp->dm_bss_va < dmp->dm_bss_size)
10820Sstevel@tonic-gate 			break;
10830Sstevel@tonic-gate 	}
10840Sstevel@tonic-gate 
10850Sstevel@tonic-gate 	if (dmp == NULL)
10860Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_NOSYMADDR));
10870Sstevel@tonic-gate 
10880Sstevel@tonic-gate 	if (dt_module_load(dtp, dmp) == -1)
10890Sstevel@tonic-gate 		return (-1); /* dt_errno is set for us */
10900Sstevel@tonic-gate 
10910Sstevel@tonic-gate 	if (symp != NULL) {
10920Sstevel@tonic-gate 		if (dmp->dm_ops->do_symaddr(dmp, addr, symp, &id) == NULL)
10930Sstevel@tonic-gate 			return (dt_set_errno(dtp, EDT_NOSYMADDR));
10940Sstevel@tonic-gate 	}
10950Sstevel@tonic-gate 
10960Sstevel@tonic-gate 	if (sip != NULL) {
10970Sstevel@tonic-gate 		sip->dts_object = dmp->dm_name;
10980Sstevel@tonic-gate 
10990Sstevel@tonic-gate 		if (symp != NULL) {
11000Sstevel@tonic-gate 			sip->dts_name = (const char *)
11010Sstevel@tonic-gate 			    dmp->dm_strtab.cts_data + symp->st_name;
11020Sstevel@tonic-gate 			sip->dts_id = id;
11030Sstevel@tonic-gate 		} else {
11040Sstevel@tonic-gate 			sip->dts_name = NULL;
11050Sstevel@tonic-gate 			sip->dts_id = 0;
11060Sstevel@tonic-gate 		}
11070Sstevel@tonic-gate 	}
11080Sstevel@tonic-gate 
11090Sstevel@tonic-gate 	return (0);
11100Sstevel@tonic-gate }
11110Sstevel@tonic-gate 
11120Sstevel@tonic-gate int
11130Sstevel@tonic-gate dtrace_lookup_by_type(dtrace_hdl_t *dtp, const char *object, const char *name,
11140Sstevel@tonic-gate     dtrace_typeinfo_t *tip)
11150Sstevel@tonic-gate {
11160Sstevel@tonic-gate 	dtrace_typeinfo_t ti;
11170Sstevel@tonic-gate 	dt_module_t *dmp;
11180Sstevel@tonic-gate 	int found = 0;
11190Sstevel@tonic-gate 	ctf_id_t id;
11200Sstevel@tonic-gate 	uint_t n;
11210Sstevel@tonic-gate 
11220Sstevel@tonic-gate 	uint_t mask = 0; /* mask of dt_module flags to match */
11230Sstevel@tonic-gate 	uint_t bits = 0; /* flag bits that must be present */
11240Sstevel@tonic-gate 
11250Sstevel@tonic-gate 	if (object != DTRACE_OBJ_EVERY &&
11260Sstevel@tonic-gate 	    object != DTRACE_OBJ_KMODS &&
11270Sstevel@tonic-gate 	    object != DTRACE_OBJ_UMODS) {
11280Sstevel@tonic-gate 		if ((dmp = dt_module_from_object(dtp, object)) == NULL)
11290Sstevel@tonic-gate 			return (-1); /* dt_errno is set for us */
11300Sstevel@tonic-gate 
11310Sstevel@tonic-gate 		if (dt_module_load(dtp, dmp) == -1)
11320Sstevel@tonic-gate 			return (-1); /* dt_errno is set for us */
11330Sstevel@tonic-gate 		n = 1;
11340Sstevel@tonic-gate 
11350Sstevel@tonic-gate 	} else {
11360Sstevel@tonic-gate 		if (object == DTRACE_OBJ_KMODS)
11370Sstevel@tonic-gate 			mask = bits = DT_DM_KERNEL;
11380Sstevel@tonic-gate 		else if (object == DTRACE_OBJ_UMODS)
11390Sstevel@tonic-gate 			mask = DT_DM_KERNEL;
11400Sstevel@tonic-gate 
11410Sstevel@tonic-gate 		dmp = dt_list_next(&dtp->dt_modlist);
11420Sstevel@tonic-gate 		n = dtp->dt_nmods;
11430Sstevel@tonic-gate 	}
11440Sstevel@tonic-gate 
11450Sstevel@tonic-gate 	if (tip == NULL)
11460Sstevel@tonic-gate 		tip = &ti;
11470Sstevel@tonic-gate 
11480Sstevel@tonic-gate 	for (; n > 0; n--, dmp = dt_list_next(dmp)) {
11490Sstevel@tonic-gate 		if ((dmp->dm_flags & mask) != bits)
11500Sstevel@tonic-gate 			continue; /* failed to match required attributes */
11510Sstevel@tonic-gate 
11520Sstevel@tonic-gate 		/*
11530Sstevel@tonic-gate 		 * If we can't load the CTF container, continue on to the next
11540Sstevel@tonic-gate 		 * module.  If our search was scoped to only one module (n = 1)
11550Sstevel@tonic-gate 		 * then return immediately, leaving dt_errno set to the error
11560Sstevel@tonic-gate 		 * from dt_module_getctf() on the module given by the caller.
11570Sstevel@tonic-gate 		 */
11580Sstevel@tonic-gate 		if (dt_module_getctf(dtp, dmp) == NULL) {
11590Sstevel@tonic-gate 			if (n == 1)
11600Sstevel@tonic-gate 				return (-1);
11610Sstevel@tonic-gate 			continue;
11620Sstevel@tonic-gate 		}
11630Sstevel@tonic-gate 
11640Sstevel@tonic-gate 		/*
11650Sstevel@tonic-gate 		 * Look up the type in the module's CTF container.  If our
11660Sstevel@tonic-gate 		 * match is a forward declaration tag, save this choice in
11670Sstevel@tonic-gate 		 * 'tip' and keep going in the hope that we will locate the
11680Sstevel@tonic-gate 		 * underlying structure definition.  Otherwise just return.
11690Sstevel@tonic-gate 		 */
11700Sstevel@tonic-gate 		if ((id = ctf_lookup_by_name(dmp->dm_ctfp, name)) != CTF_ERR) {
11710Sstevel@tonic-gate 			tip->dtt_object = dmp->dm_name;
11720Sstevel@tonic-gate 			tip->dtt_ctfp = dmp->dm_ctfp;
11730Sstevel@tonic-gate 			tip->dtt_type = id;
11740Sstevel@tonic-gate 
11750Sstevel@tonic-gate 			if (ctf_type_kind(dmp->dm_ctfp, ctf_type_resolve(
11760Sstevel@tonic-gate 			    dmp->dm_ctfp, id)) != CTF_K_FORWARD)
11770Sstevel@tonic-gate 				return (0);
11780Sstevel@tonic-gate 
11790Sstevel@tonic-gate 			found++;
11800Sstevel@tonic-gate 		}
11810Sstevel@tonic-gate 	}
11820Sstevel@tonic-gate 
11830Sstevel@tonic-gate 	if (found == 0)
11840Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_NOTYPE));
11850Sstevel@tonic-gate 
11860Sstevel@tonic-gate 	return (0);
11870Sstevel@tonic-gate }
11880Sstevel@tonic-gate 
11890Sstevel@tonic-gate int
11900Sstevel@tonic-gate dtrace_symbol_type(dtrace_hdl_t *dtp, const GElf_Sym *symp,
11910Sstevel@tonic-gate     const dtrace_syminfo_t *sip, dtrace_typeinfo_t *tip)
11920Sstevel@tonic-gate {
11930Sstevel@tonic-gate 	dt_module_t *dmp;
11940Sstevel@tonic-gate 
11950Sstevel@tonic-gate 	tip->dtt_object = NULL;
11960Sstevel@tonic-gate 	tip->dtt_ctfp = NULL;
11970Sstevel@tonic-gate 	tip->dtt_type = CTF_ERR;
11980Sstevel@tonic-gate 
11990Sstevel@tonic-gate 	if ((dmp = dt_module_lookup_by_name(dtp, sip->dts_object)) == NULL)
12000Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_NOMOD));
12010Sstevel@tonic-gate 
12020Sstevel@tonic-gate 	if (symp->st_shndx == SHN_UNDEF && dmp->dm_extern != NULL) {
12030Sstevel@tonic-gate 		dt_ident_t *idp =
12040Sstevel@tonic-gate 		    dt_idhash_lookup(dmp->dm_extern, sip->dts_name);
12050Sstevel@tonic-gate 
12060Sstevel@tonic-gate 		if (idp == NULL)
12070Sstevel@tonic-gate 			return (dt_set_errno(dtp, EDT_NOSYM));
12080Sstevel@tonic-gate 
12090Sstevel@tonic-gate 		tip->dtt_ctfp = idp->di_ctfp;
12100Sstevel@tonic-gate 		tip->dtt_type = idp->di_type;
12110Sstevel@tonic-gate 
12120Sstevel@tonic-gate 	} else if (GELF_ST_TYPE(symp->st_info) != STT_FUNC) {
12130Sstevel@tonic-gate 		if (dt_module_getctf(dtp, dmp) == NULL)
12140Sstevel@tonic-gate 			return (-1); /* errno is set for us */
12150Sstevel@tonic-gate 
12160Sstevel@tonic-gate 		tip->dtt_ctfp = dmp->dm_ctfp;
12170Sstevel@tonic-gate 		tip->dtt_type = ctf_lookup_by_symbol(dmp->dm_ctfp, sip->dts_id);
12180Sstevel@tonic-gate 
12190Sstevel@tonic-gate 		if (tip->dtt_type == CTF_ERR) {
12200Sstevel@tonic-gate 			dtp->dt_ctferr = ctf_errno(tip->dtt_ctfp);
12210Sstevel@tonic-gate 			return (dt_set_errno(dtp, EDT_CTF));
12220Sstevel@tonic-gate 		}
12230Sstevel@tonic-gate 
12240Sstevel@tonic-gate 	} else {
12250Sstevel@tonic-gate 		tip->dtt_ctfp = DT_FPTR_CTFP(dtp);
12260Sstevel@tonic-gate 		tip->dtt_type = DT_FPTR_TYPE(dtp);
12270Sstevel@tonic-gate 	}
12280Sstevel@tonic-gate 
12290Sstevel@tonic-gate 	tip->dtt_object = dmp->dm_name;
12300Sstevel@tonic-gate 	return (0);
12310Sstevel@tonic-gate }
12320Sstevel@tonic-gate 
12330Sstevel@tonic-gate static dtrace_objinfo_t *
12340Sstevel@tonic-gate dt_module_info(const dt_module_t *dmp, dtrace_objinfo_t *dto)
12350Sstevel@tonic-gate {
12360Sstevel@tonic-gate 	dto->dto_name = dmp->dm_name;
12370Sstevel@tonic-gate 	dto->dto_file = dmp->dm_file;
12380Sstevel@tonic-gate 	dto->dto_id = dmp->dm_modid;
12390Sstevel@tonic-gate 	dto->dto_flags = 0;
12400Sstevel@tonic-gate 
12410Sstevel@tonic-gate 	if (dmp->dm_flags & DT_DM_KERNEL)
12420Sstevel@tonic-gate 		dto->dto_flags |= DTRACE_OBJ_F_KERNEL;
12430Sstevel@tonic-gate 	if (dmp->dm_flags & DT_DM_PRIMARY)
12440Sstevel@tonic-gate 		dto->dto_flags |= DTRACE_OBJ_F_PRIMARY;
12450Sstevel@tonic-gate 
12460Sstevel@tonic-gate 	dto->dto_text_va = dmp->dm_text_va;
12470Sstevel@tonic-gate 	dto->dto_text_size = dmp->dm_text_size;
12480Sstevel@tonic-gate 	dto->dto_data_va = dmp->dm_data_va;
12490Sstevel@tonic-gate 	dto->dto_data_size = dmp->dm_data_size;
12500Sstevel@tonic-gate 	dto->dto_bss_va = dmp->dm_bss_va;
12510Sstevel@tonic-gate 	dto->dto_bss_size = dmp->dm_bss_size;
12520Sstevel@tonic-gate 
12530Sstevel@tonic-gate 	return (dto);
12540Sstevel@tonic-gate }
12550Sstevel@tonic-gate 
12560Sstevel@tonic-gate int
12570Sstevel@tonic-gate dtrace_object_iter(dtrace_hdl_t *dtp, dtrace_obj_f *func, void *data)
12580Sstevel@tonic-gate {
12590Sstevel@tonic-gate 	const dt_module_t *dmp = dt_list_next(&dtp->dt_modlist);
12600Sstevel@tonic-gate 	dtrace_objinfo_t dto;
12610Sstevel@tonic-gate 	int rv;
12620Sstevel@tonic-gate 
12630Sstevel@tonic-gate 	for (; dmp != NULL; dmp = dt_list_next(dmp)) {
12640Sstevel@tonic-gate 		if ((rv = (*func)(dtp, dt_module_info(dmp, &dto), data)) != 0)
12650Sstevel@tonic-gate 			return (rv);
12660Sstevel@tonic-gate 	}
12670Sstevel@tonic-gate 
12680Sstevel@tonic-gate 	return (0);
12690Sstevel@tonic-gate }
12700Sstevel@tonic-gate 
12710Sstevel@tonic-gate int
12720Sstevel@tonic-gate dtrace_object_info(dtrace_hdl_t *dtp, const char *object, dtrace_objinfo_t *dto)
12730Sstevel@tonic-gate {
12740Sstevel@tonic-gate 	dt_module_t *dmp;
12750Sstevel@tonic-gate 
12760Sstevel@tonic-gate 	if (object == DTRACE_OBJ_EVERY || object == DTRACE_OBJ_KMODS ||
12770Sstevel@tonic-gate 	    object == DTRACE_OBJ_UMODS || dto == NULL)
12780Sstevel@tonic-gate 		return (dt_set_errno(dtp, EINVAL));
12790Sstevel@tonic-gate 
12800Sstevel@tonic-gate 	if ((dmp = dt_module_from_object(dtp, object)) == NULL)
12810Sstevel@tonic-gate 		return (-1); /* dt_errno is set for us */
12820Sstevel@tonic-gate 
12830Sstevel@tonic-gate 	if (dt_module_load(dtp, dmp) == -1)
12840Sstevel@tonic-gate 		return (-1); /* dt_errno is set for us */
12850Sstevel@tonic-gate 
12860Sstevel@tonic-gate 	(void) dt_module_info(dmp, dto);
12870Sstevel@tonic-gate 	return (0);
12880Sstevel@tonic-gate }
1289