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