1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include <sys/types.h>
30*0Sstevel@tonic-gate #include <sys/modctl.h>
31*0Sstevel@tonic-gate #include <sys/kobj.h>
32*0Sstevel@tonic-gate #include <sys/kobj_impl.h>
33*0Sstevel@tonic-gate #include <sys/sysmacros.h>
34*0Sstevel@tonic-gate #include <sys/elf.h>
35*0Sstevel@tonic-gate #include <sys/task.h>
36*0Sstevel@tonic-gate 
37*0Sstevel@tonic-gate #include <unistd.h>
38*0Sstevel@tonic-gate #include <project.h>
39*0Sstevel@tonic-gate #include <strings.h>
40*0Sstevel@tonic-gate #include <stdlib.h>
41*0Sstevel@tonic-gate #include <libelf.h>
42*0Sstevel@tonic-gate #include <limits.h>
43*0Sstevel@tonic-gate #include <assert.h>
44*0Sstevel@tonic-gate #include <errno.h>
45*0Sstevel@tonic-gate #include <alloca.h>
46*0Sstevel@tonic-gate 
47*0Sstevel@tonic-gate #define	_POSIX_PTHREAD_SEMANTICS
48*0Sstevel@tonic-gate #include <dirent.h>
49*0Sstevel@tonic-gate #undef	_POSIX_PTHREAD_SEMANTICS
50*0Sstevel@tonic-gate 
51*0Sstevel@tonic-gate #include <dt_strtab.h>
52*0Sstevel@tonic-gate #include <dt_module.h>
53*0Sstevel@tonic-gate #include <dt_impl.h>
54*0Sstevel@tonic-gate 
55*0Sstevel@tonic-gate static const char *dt_module_strtab; /* active strtab for qsort callbacks */
56*0Sstevel@tonic-gate 
57*0Sstevel@tonic-gate static void
58*0Sstevel@tonic-gate dt_module_symhash_insert(dt_module_t *dmp, const char *name, uint_t id)
59*0Sstevel@tonic-gate {
60*0Sstevel@tonic-gate 	dt_sym_t *dsp = &dmp->dm_symchains[dmp->dm_symfree];
61*0Sstevel@tonic-gate 	uint_t h;
62*0Sstevel@tonic-gate 
63*0Sstevel@tonic-gate 	assert(dmp->dm_symfree < dmp->dm_nsymelems + 1);
64*0Sstevel@tonic-gate 
65*0Sstevel@tonic-gate 	dsp->ds_symid = id;
66*0Sstevel@tonic-gate 	h = dt_strtab_hash(name, NULL) % dmp->dm_nsymbuckets;
67*0Sstevel@tonic-gate 	dsp->ds_next = dmp->dm_symbuckets[h];
68*0Sstevel@tonic-gate 	dmp->dm_symbuckets[h] = dmp->dm_symfree++;
69*0Sstevel@tonic-gate }
70*0Sstevel@tonic-gate 
71*0Sstevel@tonic-gate static uint_t
72*0Sstevel@tonic-gate dt_module_syminit32(dt_module_t *dmp)
73*0Sstevel@tonic-gate {
74*0Sstevel@tonic-gate 	const Elf32_Sym *sym = dmp->dm_symtab.cts_data;
75*0Sstevel@tonic-gate 	const char *base = dmp->dm_strtab.cts_data;
76*0Sstevel@tonic-gate 	size_t ss_size = dmp->dm_strtab.cts_size;
77*0Sstevel@tonic-gate 	uint_t i, n = dmp->dm_nsymelems;
78*0Sstevel@tonic-gate 	uint_t asrsv = 0;
79*0Sstevel@tonic-gate 
80*0Sstevel@tonic-gate 	for (i = 0; i < n; i++, sym++) {
81*0Sstevel@tonic-gate 		const char *name = base + sym->st_name;
82*0Sstevel@tonic-gate 		uchar_t type = ELF32_ST_TYPE(sym->st_info);
83*0Sstevel@tonic-gate 
84*0Sstevel@tonic-gate 		if (type >= STT_NUM || type == STT_SECTION)
85*0Sstevel@tonic-gate 			continue; /* skip sections and unknown types */
86*0Sstevel@tonic-gate 
87*0Sstevel@tonic-gate 		if (sym->st_name == 0 || sym->st_name >= ss_size)
88*0Sstevel@tonic-gate 			continue; /* skip null or invalid names */
89*0Sstevel@tonic-gate 
90*0Sstevel@tonic-gate 		if (sym->st_value != 0 &&
91*0Sstevel@tonic-gate 		    (ELF32_ST_BIND(sym->st_info) != STB_LOCAL || sym->st_size))
92*0Sstevel@tonic-gate 			asrsv++; /* reserve space in the address map */
93*0Sstevel@tonic-gate 
94*0Sstevel@tonic-gate 		dt_module_symhash_insert(dmp, name, i);
95*0Sstevel@tonic-gate 	}
96*0Sstevel@tonic-gate 
97*0Sstevel@tonic-gate 	return (asrsv);
98*0Sstevel@tonic-gate }
99*0Sstevel@tonic-gate 
100*0Sstevel@tonic-gate static uint_t
101*0Sstevel@tonic-gate dt_module_syminit64(dt_module_t *dmp)
102*0Sstevel@tonic-gate {
103*0Sstevel@tonic-gate 	const Elf64_Sym *sym = dmp->dm_symtab.cts_data;
104*0Sstevel@tonic-gate 	const char *base = dmp->dm_strtab.cts_data;
105*0Sstevel@tonic-gate 	size_t ss_size = dmp->dm_strtab.cts_size;
106*0Sstevel@tonic-gate 	uint_t i, n = dmp->dm_nsymelems;
107*0Sstevel@tonic-gate 	uint_t asrsv = 0;
108*0Sstevel@tonic-gate 
109*0Sstevel@tonic-gate 	for (i = 0; i < n; i++, sym++) {
110*0Sstevel@tonic-gate 		const char *name = base + sym->st_name;
111*0Sstevel@tonic-gate 		uchar_t type = ELF64_ST_TYPE(sym->st_info);
112*0Sstevel@tonic-gate 
113*0Sstevel@tonic-gate 		if (type >= STT_NUM || type == STT_SECTION)
114*0Sstevel@tonic-gate 			continue; /* skip sections and unknown types */
115*0Sstevel@tonic-gate 
116*0Sstevel@tonic-gate 		if (sym->st_name == 0 || sym->st_name >= ss_size)
117*0Sstevel@tonic-gate 			continue; /* skip null or invalid names */
118*0Sstevel@tonic-gate 
119*0Sstevel@tonic-gate 		if (sym->st_value != 0 &&
120*0Sstevel@tonic-gate 		    (ELF64_ST_BIND(sym->st_info) != STB_LOCAL || sym->st_size))
121*0Sstevel@tonic-gate 			asrsv++; /* reserve space in the address map */
122*0Sstevel@tonic-gate 
123*0Sstevel@tonic-gate 		dt_module_symhash_insert(dmp, name, i);
124*0Sstevel@tonic-gate 	}
125*0Sstevel@tonic-gate 
126*0Sstevel@tonic-gate 	return (asrsv);
127*0Sstevel@tonic-gate }
128*0Sstevel@tonic-gate 
129*0Sstevel@tonic-gate /*
130*0Sstevel@tonic-gate  * Sort comparison function for 32-bit symbol address-to-name lookups.  We sort
131*0Sstevel@tonic-gate  * symbols by value.  If values are equal, we prefer the symbol that is
132*0Sstevel@tonic-gate  * non-zero sized, typed, not weak, or lexically first, in that order.
133*0Sstevel@tonic-gate  */
134*0Sstevel@tonic-gate static int
135*0Sstevel@tonic-gate dt_module_symcomp32(const void *lp, const void *rp)
136*0Sstevel@tonic-gate {
137*0Sstevel@tonic-gate 	Elf32_Sym *lhs = *((Elf32_Sym **)lp);
138*0Sstevel@tonic-gate 	Elf32_Sym *rhs = *((Elf32_Sym **)rp);
139*0Sstevel@tonic-gate 
140*0Sstevel@tonic-gate 	if (lhs->st_value != rhs->st_value)
141*0Sstevel@tonic-gate 		return (lhs->st_value > rhs->st_value ? 1 : -1);
142*0Sstevel@tonic-gate 
143*0Sstevel@tonic-gate 	if ((lhs->st_size == 0) != (rhs->st_size == 0))
144*0Sstevel@tonic-gate 		return (lhs->st_size == 0 ? 1 : -1);
145*0Sstevel@tonic-gate 
146*0Sstevel@tonic-gate 	if ((ELF32_ST_TYPE(lhs->st_info) == STT_NOTYPE) !=
147*0Sstevel@tonic-gate 	    (ELF32_ST_TYPE(rhs->st_info) == STT_NOTYPE))
148*0Sstevel@tonic-gate 		return (ELF32_ST_TYPE(lhs->st_info) == STT_NOTYPE ? 1 : -1);
149*0Sstevel@tonic-gate 
150*0Sstevel@tonic-gate 	if ((ELF32_ST_BIND(lhs->st_info) == STB_WEAK) !=
151*0Sstevel@tonic-gate 	    (ELF32_ST_BIND(rhs->st_info) == STB_WEAK))
152*0Sstevel@tonic-gate 		return (ELF32_ST_BIND(lhs->st_info) == STB_WEAK ? 1 : -1);
153*0Sstevel@tonic-gate 
154*0Sstevel@tonic-gate 	return (strcmp(dt_module_strtab + lhs->st_name,
155*0Sstevel@tonic-gate 	    dt_module_strtab + rhs->st_name));
156*0Sstevel@tonic-gate }
157*0Sstevel@tonic-gate 
158*0Sstevel@tonic-gate /*
159*0Sstevel@tonic-gate  * Sort comparison function for 64-bit symbol address-to-name lookups.  We sort
160*0Sstevel@tonic-gate  * symbols by value.  If values are equal, we prefer the symbol that is
161*0Sstevel@tonic-gate  * non-zero sized, typed, not weak, or lexically first, in that order.
162*0Sstevel@tonic-gate  */
163*0Sstevel@tonic-gate static int
164*0Sstevel@tonic-gate dt_module_symcomp64(const void *lp, const void *rp)
165*0Sstevel@tonic-gate {
166*0Sstevel@tonic-gate 	Elf64_Sym *lhs = *((Elf64_Sym **)lp);
167*0Sstevel@tonic-gate 	Elf64_Sym *rhs = *((Elf64_Sym **)rp);
168*0Sstevel@tonic-gate 
169*0Sstevel@tonic-gate 	if (lhs->st_value != rhs->st_value)
170*0Sstevel@tonic-gate 		return (lhs->st_value > rhs->st_value ? 1 : -1);
171*0Sstevel@tonic-gate 
172*0Sstevel@tonic-gate 	if ((lhs->st_size == 0) != (rhs->st_size == 0))
173*0Sstevel@tonic-gate 		return (lhs->st_size == 0 ? 1 : -1);
174*0Sstevel@tonic-gate 
175*0Sstevel@tonic-gate 	if ((ELF64_ST_TYPE(lhs->st_info) == STT_NOTYPE) !=
176*0Sstevel@tonic-gate 	    (ELF64_ST_TYPE(rhs->st_info) == STT_NOTYPE))
177*0Sstevel@tonic-gate 		return (ELF64_ST_TYPE(lhs->st_info) == STT_NOTYPE ? 1 : -1);
178*0Sstevel@tonic-gate 
179*0Sstevel@tonic-gate 	if ((ELF64_ST_BIND(lhs->st_info) == STB_WEAK) !=
180*0Sstevel@tonic-gate 	    (ELF64_ST_BIND(rhs->st_info) == STB_WEAK))
181*0Sstevel@tonic-gate 		return (ELF64_ST_BIND(lhs->st_info) == STB_WEAK ? 1 : -1);
182*0Sstevel@tonic-gate 
183*0Sstevel@tonic-gate 	return (strcmp(dt_module_strtab + lhs->st_name,
184*0Sstevel@tonic-gate 	    dt_module_strtab + rhs->st_name));
185*0Sstevel@tonic-gate }
186*0Sstevel@tonic-gate 
187*0Sstevel@tonic-gate static void
188*0Sstevel@tonic-gate dt_module_symsort32(dt_module_t *dmp)
189*0Sstevel@tonic-gate {
190*0Sstevel@tonic-gate 	Elf32_Sym *symtab = (Elf32_Sym *)dmp->dm_symtab.cts_data;
191*0Sstevel@tonic-gate 	Elf32_Sym **sympp = (Elf32_Sym **)dmp->dm_asmap;
192*0Sstevel@tonic-gate 	const dt_sym_t *dsp = dmp->dm_symchains + 1;
193*0Sstevel@tonic-gate 	uint_t i, n = dmp->dm_symfree;
194*0Sstevel@tonic-gate 
195*0Sstevel@tonic-gate 	for (i = 1; i < n; i++, dsp++) {
196*0Sstevel@tonic-gate 		Elf32_Sym *sym = symtab + dsp->ds_symid;
197*0Sstevel@tonic-gate 		if (sym->st_value != 0 &&
198*0Sstevel@tonic-gate 		    (ELF32_ST_BIND(sym->st_info) != STB_LOCAL || sym->st_size))
199*0Sstevel@tonic-gate 			*sympp++ = sym;
200*0Sstevel@tonic-gate 	}
201*0Sstevel@tonic-gate 
202*0Sstevel@tonic-gate 	dmp->dm_aslen = (uint_t)(sympp - (Elf32_Sym **)dmp->dm_asmap);
203*0Sstevel@tonic-gate 	assert(dmp->dm_aslen <= dmp->dm_asrsv);
204*0Sstevel@tonic-gate 
205*0Sstevel@tonic-gate 	dt_module_strtab = dmp->dm_strtab.cts_data;
206*0Sstevel@tonic-gate 	qsort(dmp->dm_asmap, dmp->dm_aslen,
207*0Sstevel@tonic-gate 	    sizeof (Elf32_Sym *), dt_module_symcomp32);
208*0Sstevel@tonic-gate 	dt_module_strtab = NULL;
209*0Sstevel@tonic-gate }
210*0Sstevel@tonic-gate 
211*0Sstevel@tonic-gate static void
212*0Sstevel@tonic-gate dt_module_symsort64(dt_module_t *dmp)
213*0Sstevel@tonic-gate {
214*0Sstevel@tonic-gate 	Elf64_Sym *symtab = (Elf64_Sym *)dmp->dm_symtab.cts_data;
215*0Sstevel@tonic-gate 	Elf64_Sym **sympp = (Elf64_Sym **)dmp->dm_asmap;
216*0Sstevel@tonic-gate 	const dt_sym_t *dsp = dmp->dm_symchains + 1;
217*0Sstevel@tonic-gate 	uint_t i, n = dmp->dm_symfree;
218*0Sstevel@tonic-gate 
219*0Sstevel@tonic-gate 	for (i = 1; i < n; i++, dsp++) {
220*0Sstevel@tonic-gate 		Elf64_Sym *sym = symtab + dsp->ds_symid;
221*0Sstevel@tonic-gate 		if (sym->st_value != 0 &&
222*0Sstevel@tonic-gate 		    (ELF64_ST_BIND(sym->st_info) != STB_LOCAL || sym->st_size))
223*0Sstevel@tonic-gate 			*sympp++ = sym;
224*0Sstevel@tonic-gate 	}
225*0Sstevel@tonic-gate 
226*0Sstevel@tonic-gate 	dmp->dm_aslen = (uint_t)(sympp - (Elf64_Sym **)dmp->dm_asmap);
227*0Sstevel@tonic-gate 	assert(dmp->dm_aslen <= dmp->dm_asrsv);
228*0Sstevel@tonic-gate 
229*0Sstevel@tonic-gate 	dt_module_strtab = dmp->dm_strtab.cts_data;
230*0Sstevel@tonic-gate 	qsort(dmp->dm_asmap, dmp->dm_aslen,
231*0Sstevel@tonic-gate 	    sizeof (Elf64_Sym *), dt_module_symcomp64);
232*0Sstevel@tonic-gate 	dt_module_strtab = NULL;
233*0Sstevel@tonic-gate }
234*0Sstevel@tonic-gate 
235*0Sstevel@tonic-gate static GElf_Sym *
236*0Sstevel@tonic-gate dt_module_symgelf32(const Elf32_Sym *src, GElf_Sym *dst)
237*0Sstevel@tonic-gate {
238*0Sstevel@tonic-gate 	if (dst != NULL) {
239*0Sstevel@tonic-gate 		dst->st_name = src->st_name;
240*0Sstevel@tonic-gate 		dst->st_info = src->st_info;
241*0Sstevel@tonic-gate 		dst->st_other = src->st_other;
242*0Sstevel@tonic-gate 		dst->st_shndx = src->st_shndx;
243*0Sstevel@tonic-gate 		dst->st_value = src->st_value;
244*0Sstevel@tonic-gate 		dst->st_size = src->st_size;
245*0Sstevel@tonic-gate 	}
246*0Sstevel@tonic-gate 
247*0Sstevel@tonic-gate 	return (dst);
248*0Sstevel@tonic-gate }
249*0Sstevel@tonic-gate 
250*0Sstevel@tonic-gate static GElf_Sym *
251*0Sstevel@tonic-gate dt_module_symgelf64(const Elf64_Sym *src, GElf_Sym *dst)
252*0Sstevel@tonic-gate {
253*0Sstevel@tonic-gate 	if (dst != NULL)
254*0Sstevel@tonic-gate 		bcopy(src, dst, sizeof (GElf_Sym));
255*0Sstevel@tonic-gate 
256*0Sstevel@tonic-gate 	return (dst);
257*0Sstevel@tonic-gate }
258*0Sstevel@tonic-gate 
259*0Sstevel@tonic-gate static GElf_Sym *
260*0Sstevel@tonic-gate dt_module_symname32(dt_module_t *dmp, const char *name,
261*0Sstevel@tonic-gate     GElf_Sym *symp, uint_t *idp)
262*0Sstevel@tonic-gate {
263*0Sstevel@tonic-gate 	const Elf32_Sym *symtab = dmp->dm_symtab.cts_data;
264*0Sstevel@tonic-gate 	const char *strtab = dmp->dm_strtab.cts_data;
265*0Sstevel@tonic-gate 
266*0Sstevel@tonic-gate 	const Elf32_Sym *sym;
267*0Sstevel@tonic-gate 	const dt_sym_t *dsp;
268*0Sstevel@tonic-gate 	uint_t i, h;
269*0Sstevel@tonic-gate 
270*0Sstevel@tonic-gate 	if (dmp->dm_nsymelems == 0)
271*0Sstevel@tonic-gate 		return (NULL);
272*0Sstevel@tonic-gate 
273*0Sstevel@tonic-gate 	h = dt_strtab_hash(name, NULL) % dmp->dm_nsymbuckets;
274*0Sstevel@tonic-gate 
275*0Sstevel@tonic-gate 	for (i = dmp->dm_symbuckets[h]; i != 0; i = dsp->ds_next) {
276*0Sstevel@tonic-gate 		dsp = &dmp->dm_symchains[i];
277*0Sstevel@tonic-gate 		sym = symtab + dsp->ds_symid;
278*0Sstevel@tonic-gate 
279*0Sstevel@tonic-gate 		if (strcmp(name, strtab + sym->st_name) == 0) {
280*0Sstevel@tonic-gate 			if (idp != NULL)
281*0Sstevel@tonic-gate 				*idp = dsp->ds_symid;
282*0Sstevel@tonic-gate 			return (dt_module_symgelf32(sym, symp));
283*0Sstevel@tonic-gate 		}
284*0Sstevel@tonic-gate 	}
285*0Sstevel@tonic-gate 
286*0Sstevel@tonic-gate 	return (NULL);
287*0Sstevel@tonic-gate }
288*0Sstevel@tonic-gate 
289*0Sstevel@tonic-gate static GElf_Sym *
290*0Sstevel@tonic-gate dt_module_symname64(dt_module_t *dmp, const char *name,
291*0Sstevel@tonic-gate     GElf_Sym *symp, uint_t *idp)
292*0Sstevel@tonic-gate {
293*0Sstevel@tonic-gate 	const Elf64_Sym *symtab = dmp->dm_symtab.cts_data;
294*0Sstevel@tonic-gate 	const char *strtab = dmp->dm_strtab.cts_data;
295*0Sstevel@tonic-gate 
296*0Sstevel@tonic-gate 	const Elf64_Sym *sym;
297*0Sstevel@tonic-gate 	const dt_sym_t *dsp;
298*0Sstevel@tonic-gate 	uint_t i, h;
299*0Sstevel@tonic-gate 
300*0Sstevel@tonic-gate 	if (dmp->dm_nsymelems == 0)
301*0Sstevel@tonic-gate 		return (NULL);
302*0Sstevel@tonic-gate 
303*0Sstevel@tonic-gate 	h = dt_strtab_hash(name, NULL) % dmp->dm_nsymbuckets;
304*0Sstevel@tonic-gate 
305*0Sstevel@tonic-gate 	for (i = dmp->dm_symbuckets[h]; i != 0; i = dsp->ds_next) {
306*0Sstevel@tonic-gate 		dsp = &dmp->dm_symchains[i];
307*0Sstevel@tonic-gate 		sym = symtab + dsp->ds_symid;
308*0Sstevel@tonic-gate 
309*0Sstevel@tonic-gate 		if (strcmp(name, strtab + sym->st_name) == 0) {
310*0Sstevel@tonic-gate 			if (idp != NULL)
311*0Sstevel@tonic-gate 				*idp = dsp->ds_symid;
312*0Sstevel@tonic-gate 			return (dt_module_symgelf64(sym, symp));
313*0Sstevel@tonic-gate 		}
314*0Sstevel@tonic-gate 	}
315*0Sstevel@tonic-gate 
316*0Sstevel@tonic-gate 	return (NULL);
317*0Sstevel@tonic-gate }
318*0Sstevel@tonic-gate 
319*0Sstevel@tonic-gate static GElf_Sym *
320*0Sstevel@tonic-gate dt_module_symaddr32(dt_module_t *dmp, GElf_Addr addr,
321*0Sstevel@tonic-gate     GElf_Sym *symp, uint_t *idp)
322*0Sstevel@tonic-gate {
323*0Sstevel@tonic-gate 	const Elf32_Sym **asmap = (const Elf32_Sym **)dmp->dm_asmap;
324*0Sstevel@tonic-gate 	const Elf32_Sym *symtab = dmp->dm_symtab.cts_data;
325*0Sstevel@tonic-gate 	const Elf32_Sym *sym;
326*0Sstevel@tonic-gate 
327*0Sstevel@tonic-gate 	uint_t i, mid, lo = 0, hi = dmp->dm_aslen - 1;
328*0Sstevel@tonic-gate 	Elf32_Addr v;
329*0Sstevel@tonic-gate 
330*0Sstevel@tonic-gate 	if (dmp->dm_aslen == 0)
331*0Sstevel@tonic-gate 		return (NULL);
332*0Sstevel@tonic-gate 
333*0Sstevel@tonic-gate 	while (hi - lo > 1) {
334*0Sstevel@tonic-gate 		mid = (lo + hi) / 2;
335*0Sstevel@tonic-gate 		if (addr >= asmap[mid]->st_value)
336*0Sstevel@tonic-gate 			lo = mid;
337*0Sstevel@tonic-gate 		else
338*0Sstevel@tonic-gate 			hi = mid;
339*0Sstevel@tonic-gate 	}
340*0Sstevel@tonic-gate 
341*0Sstevel@tonic-gate 	i = addr < asmap[hi]->st_value ? lo : hi;
342*0Sstevel@tonic-gate 	sym = asmap[i];
343*0Sstevel@tonic-gate 	v = sym->st_value;
344*0Sstevel@tonic-gate 
345*0Sstevel@tonic-gate 	/*
346*0Sstevel@tonic-gate 	 * If the previous entry has the same value, improve our choice.  The
347*0Sstevel@tonic-gate 	 * order of equal-valued symbols is determined by the comparison func.
348*0Sstevel@tonic-gate 	 */
349*0Sstevel@tonic-gate 	while (i-- != 0 && asmap[i]->st_value == v)
350*0Sstevel@tonic-gate 		sym = asmap[i];
351*0Sstevel@tonic-gate 
352*0Sstevel@tonic-gate 	if (addr - sym->st_value < MAX(sym->st_size, 1)) {
353*0Sstevel@tonic-gate 		if (idp != NULL)
354*0Sstevel@tonic-gate 			*idp = (uint_t)(sym - symtab);
355*0Sstevel@tonic-gate 		return (dt_module_symgelf32(sym, symp));
356*0Sstevel@tonic-gate 	}
357*0Sstevel@tonic-gate 
358*0Sstevel@tonic-gate 	return (NULL);
359*0Sstevel@tonic-gate }
360*0Sstevel@tonic-gate 
361*0Sstevel@tonic-gate static GElf_Sym *
362*0Sstevel@tonic-gate dt_module_symaddr64(dt_module_t *dmp, GElf_Addr addr,
363*0Sstevel@tonic-gate     GElf_Sym *symp, uint_t *idp)
364*0Sstevel@tonic-gate {
365*0Sstevel@tonic-gate 	const Elf64_Sym **asmap = (const Elf64_Sym **)dmp->dm_asmap;
366*0Sstevel@tonic-gate 	const Elf64_Sym *symtab = dmp->dm_symtab.cts_data;
367*0Sstevel@tonic-gate 	const Elf64_Sym *sym;
368*0Sstevel@tonic-gate 
369*0Sstevel@tonic-gate 	uint_t i, mid, lo = 0, hi = dmp->dm_aslen - 1;
370*0Sstevel@tonic-gate 	Elf64_Addr v;
371*0Sstevel@tonic-gate 
372*0Sstevel@tonic-gate 	if (dmp->dm_aslen == 0)
373*0Sstevel@tonic-gate 		return (NULL);
374*0Sstevel@tonic-gate 
375*0Sstevel@tonic-gate 	while (hi - lo > 1) {
376*0Sstevel@tonic-gate 		mid = (lo + hi) / 2;
377*0Sstevel@tonic-gate 		if (addr >= asmap[mid]->st_value)
378*0Sstevel@tonic-gate 			lo = mid;
379*0Sstevel@tonic-gate 		else
380*0Sstevel@tonic-gate 			hi = mid;
381*0Sstevel@tonic-gate 	}
382*0Sstevel@tonic-gate 
383*0Sstevel@tonic-gate 	i = addr < asmap[hi]->st_value ? lo : hi;
384*0Sstevel@tonic-gate 	sym = asmap[i];
385*0Sstevel@tonic-gate 	v = sym->st_value;
386*0Sstevel@tonic-gate 
387*0Sstevel@tonic-gate 	/*
388*0Sstevel@tonic-gate 	 * If the previous entry has the same value, improve our choice.  The
389*0Sstevel@tonic-gate 	 * order of equal-valued symbols is determined by the comparison func.
390*0Sstevel@tonic-gate 	 */
391*0Sstevel@tonic-gate 	while (i-- != 0 && asmap[i]->st_value == v)
392*0Sstevel@tonic-gate 		sym = asmap[i];
393*0Sstevel@tonic-gate 
394*0Sstevel@tonic-gate 	if (addr - sym->st_value < MAX(sym->st_size, 1)) {
395*0Sstevel@tonic-gate 		if (idp != NULL)
396*0Sstevel@tonic-gate 			*idp = (uint_t)(sym - symtab);
397*0Sstevel@tonic-gate 		return (dt_module_symgelf64(sym, symp));
398*0Sstevel@tonic-gate 	}
399*0Sstevel@tonic-gate 
400*0Sstevel@tonic-gate 	return (NULL);
401*0Sstevel@tonic-gate }
402*0Sstevel@tonic-gate 
403*0Sstevel@tonic-gate static const dt_modops_t dt_modops_32 = {
404*0Sstevel@tonic-gate 	dt_module_syminit32,
405*0Sstevel@tonic-gate 	dt_module_symsort32,
406*0Sstevel@tonic-gate 	dt_module_symname32,
407*0Sstevel@tonic-gate 	dt_module_symaddr32
408*0Sstevel@tonic-gate };
409*0Sstevel@tonic-gate 
410*0Sstevel@tonic-gate static const dt_modops_t dt_modops_64 = {
411*0Sstevel@tonic-gate 	dt_module_syminit64,
412*0Sstevel@tonic-gate 	dt_module_symsort64,
413*0Sstevel@tonic-gate 	dt_module_symname64,
414*0Sstevel@tonic-gate 	dt_module_symaddr64
415*0Sstevel@tonic-gate };
416*0Sstevel@tonic-gate 
417*0Sstevel@tonic-gate dt_module_t *
418*0Sstevel@tonic-gate dt_module_create(dtrace_hdl_t *dtp, const char *name)
419*0Sstevel@tonic-gate {
420*0Sstevel@tonic-gate 	uint_t h = dt_strtab_hash(name, NULL) % dtp->dt_modbuckets;
421*0Sstevel@tonic-gate 	dt_module_t *dmp;
422*0Sstevel@tonic-gate 
423*0Sstevel@tonic-gate 	for (dmp = dtp->dt_mods[h]; dmp != NULL; dmp = dmp->dm_next) {
424*0Sstevel@tonic-gate 		if (strcmp(dmp->dm_name, name) == 0)
425*0Sstevel@tonic-gate 			return (dmp);
426*0Sstevel@tonic-gate 	}
427*0Sstevel@tonic-gate 
428*0Sstevel@tonic-gate 	if ((dmp = malloc(sizeof (dt_module_t))) == NULL)
429*0Sstevel@tonic-gate 		return (NULL); /* caller must handle allocation failure */
430*0Sstevel@tonic-gate 
431*0Sstevel@tonic-gate 	bzero(dmp, sizeof (dt_module_t));
432*0Sstevel@tonic-gate 	(void) strlcpy(dmp->dm_name, name, sizeof (dmp->dm_name));
433*0Sstevel@tonic-gate 	dt_list_append(&dtp->dt_modlist, dmp);
434*0Sstevel@tonic-gate 	dmp->dm_next = dtp->dt_mods[h];
435*0Sstevel@tonic-gate 	dtp->dt_mods[h] = dmp;
436*0Sstevel@tonic-gate 	dtp->dt_nmods++;
437*0Sstevel@tonic-gate 
438*0Sstevel@tonic-gate 	if (dtp->dt_conf.dtc_ctfmodel == CTF_MODEL_LP64)
439*0Sstevel@tonic-gate 		dmp->dm_ops = &dt_modops_64;
440*0Sstevel@tonic-gate 	else
441*0Sstevel@tonic-gate 		dmp->dm_ops = &dt_modops_32;
442*0Sstevel@tonic-gate 
443*0Sstevel@tonic-gate 	return (dmp);
444*0Sstevel@tonic-gate }
445*0Sstevel@tonic-gate 
446*0Sstevel@tonic-gate dt_module_t *
447*0Sstevel@tonic-gate dt_module_lookup_by_name(dtrace_hdl_t *dtp, const char *name)
448*0Sstevel@tonic-gate {
449*0Sstevel@tonic-gate 	uint_t h = dt_strtab_hash(name, NULL) % dtp->dt_modbuckets;
450*0Sstevel@tonic-gate 	dt_module_t *dmp;
451*0Sstevel@tonic-gate 
452*0Sstevel@tonic-gate 	for (dmp = dtp->dt_mods[h]; dmp != NULL; dmp = dmp->dm_next) {
453*0Sstevel@tonic-gate 		if (strcmp(dmp->dm_name, name) == 0)
454*0Sstevel@tonic-gate 			return (dmp);
455*0Sstevel@tonic-gate 	}
456*0Sstevel@tonic-gate 
457*0Sstevel@tonic-gate 	return (NULL);
458*0Sstevel@tonic-gate }
459*0Sstevel@tonic-gate 
460*0Sstevel@tonic-gate /*ARGSUSED*/
461*0Sstevel@tonic-gate dt_module_t *
462*0Sstevel@tonic-gate dt_module_lookup_by_ctf(dtrace_hdl_t *dtp, ctf_file_t *ctfp)
463*0Sstevel@tonic-gate {
464*0Sstevel@tonic-gate 	return (ctfp ? ctf_getspecific(ctfp) : NULL);
465*0Sstevel@tonic-gate }
466*0Sstevel@tonic-gate 
467*0Sstevel@tonic-gate static int
468*0Sstevel@tonic-gate dt_module_load_sect(dtrace_hdl_t *dtp, dt_module_t *dmp, ctf_sect_t *ctsp)
469*0Sstevel@tonic-gate {
470*0Sstevel@tonic-gate 	const char *s;
471*0Sstevel@tonic-gate 	size_t shstrs;
472*0Sstevel@tonic-gate 	GElf_Shdr sh;
473*0Sstevel@tonic-gate 	Elf_Data *dp;
474*0Sstevel@tonic-gate 	Elf_Scn *sp;
475*0Sstevel@tonic-gate 
476*0Sstevel@tonic-gate 	if (elf_getshstrndx(dmp->dm_elf, &shstrs) == 0)
477*0Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_NOTLOADED));
478*0Sstevel@tonic-gate 
479*0Sstevel@tonic-gate 	for (sp = NULL; (sp = elf_nextscn(dmp->dm_elf, sp)) != NULL; ) {
480*0Sstevel@tonic-gate 		if (gelf_getshdr(sp, &sh) == NULL || sh.sh_type == SHT_NULL ||
481*0Sstevel@tonic-gate 		    (s = elf_strptr(dmp->dm_elf, shstrs, sh.sh_name)) == NULL)
482*0Sstevel@tonic-gate 			continue; /* skip any malformed sections */
483*0Sstevel@tonic-gate 
484*0Sstevel@tonic-gate 		if (sh.sh_type == ctsp->cts_type &&
485*0Sstevel@tonic-gate 		    sh.sh_entsize == ctsp->cts_entsize &&
486*0Sstevel@tonic-gate 		    strcmp(s, ctsp->cts_name) == 0)
487*0Sstevel@tonic-gate 			break; /* section matches specification */
488*0Sstevel@tonic-gate 	}
489*0Sstevel@tonic-gate 
490*0Sstevel@tonic-gate 	/*
491*0Sstevel@tonic-gate 	 * If the section isn't found, return success but leave cts_data set
492*0Sstevel@tonic-gate 	 * to NULL and cts_size set to zero for our caller.
493*0Sstevel@tonic-gate 	 */
494*0Sstevel@tonic-gate 	if (sp == NULL || (dp = elf_getdata(sp, NULL)) == NULL)
495*0Sstevel@tonic-gate 		return (0);
496*0Sstevel@tonic-gate 
497*0Sstevel@tonic-gate 	ctsp->cts_data = dp->d_buf;
498*0Sstevel@tonic-gate 	ctsp->cts_size = dp->d_size;
499*0Sstevel@tonic-gate 
500*0Sstevel@tonic-gate 	dt_dprintf("loaded %s [%s] (%lu bytes)\n",
501*0Sstevel@tonic-gate 	    dmp->dm_name, ctsp->cts_name, (ulong_t)ctsp->cts_size);
502*0Sstevel@tonic-gate 
503*0Sstevel@tonic-gate 	return (0);
504*0Sstevel@tonic-gate }
505*0Sstevel@tonic-gate 
506*0Sstevel@tonic-gate int
507*0Sstevel@tonic-gate dt_module_load(dtrace_hdl_t *dtp, dt_module_t *dmp)
508*0Sstevel@tonic-gate {
509*0Sstevel@tonic-gate 	if (dmp->dm_flags & DT_DM_LOADED)
510*0Sstevel@tonic-gate 		return (0); /* module is already loaded */
511*0Sstevel@tonic-gate 
512*0Sstevel@tonic-gate 	dmp->dm_ctdata.cts_name = ".SUNW_ctf";
513*0Sstevel@tonic-gate 	dmp->dm_ctdata.cts_type = SHT_PROGBITS;
514*0Sstevel@tonic-gate 	dmp->dm_ctdata.cts_flags = 0;
515*0Sstevel@tonic-gate 	dmp->dm_ctdata.cts_data = NULL;
516*0Sstevel@tonic-gate 	dmp->dm_ctdata.cts_size = 0;
517*0Sstevel@tonic-gate 	dmp->dm_ctdata.cts_entsize = 0;
518*0Sstevel@tonic-gate 	dmp->dm_ctdata.cts_offset = 0;
519*0Sstevel@tonic-gate 
520*0Sstevel@tonic-gate 	dmp->dm_symtab.cts_name = ".symtab";
521*0Sstevel@tonic-gate 	dmp->dm_symtab.cts_type = SHT_SYMTAB;
522*0Sstevel@tonic-gate 	dmp->dm_symtab.cts_flags = 0;
523*0Sstevel@tonic-gate 	dmp->dm_symtab.cts_data = NULL;
524*0Sstevel@tonic-gate 	dmp->dm_symtab.cts_size = 0;
525*0Sstevel@tonic-gate 	dmp->dm_symtab.cts_entsize = dmp->dm_ops == &dt_modops_64 ?
526*0Sstevel@tonic-gate 	    sizeof (Elf64_Sym) : sizeof (Elf32_Sym);
527*0Sstevel@tonic-gate 	dmp->dm_symtab.cts_offset = 0;
528*0Sstevel@tonic-gate 
529*0Sstevel@tonic-gate 	dmp->dm_strtab.cts_name = ".strtab";
530*0Sstevel@tonic-gate 	dmp->dm_strtab.cts_type = SHT_STRTAB;
531*0Sstevel@tonic-gate 	dmp->dm_strtab.cts_flags = 0;
532*0Sstevel@tonic-gate 	dmp->dm_strtab.cts_data = NULL;
533*0Sstevel@tonic-gate 	dmp->dm_strtab.cts_size = 0;
534*0Sstevel@tonic-gate 	dmp->dm_strtab.cts_entsize = 0;
535*0Sstevel@tonic-gate 	dmp->dm_strtab.cts_offset = 0;
536*0Sstevel@tonic-gate 
537*0Sstevel@tonic-gate 	/*
538*0Sstevel@tonic-gate 	 * Attempt to load the module's CTF section, symbol table section, and
539*0Sstevel@tonic-gate 	 * string table section.  Note that modules may not contain CTF data:
540*0Sstevel@tonic-gate 	 * this will result in a successful load_sect but data of size zero.
541*0Sstevel@tonic-gate 	 * We will then fail if dt_module_getctf() is called, as shown below.
542*0Sstevel@tonic-gate 	 */
543*0Sstevel@tonic-gate 	if (dt_module_load_sect(dtp, dmp, &dmp->dm_ctdata) == -1 ||
544*0Sstevel@tonic-gate 	    dt_module_load_sect(dtp, dmp, &dmp->dm_symtab) == -1 ||
545*0Sstevel@tonic-gate 	    dt_module_load_sect(dtp, dmp, &dmp->dm_strtab) == -1) {
546*0Sstevel@tonic-gate 		dt_module_unload(dtp, dmp);
547*0Sstevel@tonic-gate 		return (-1); /* dt_errno is set for us */
548*0Sstevel@tonic-gate 	}
549*0Sstevel@tonic-gate 
550*0Sstevel@tonic-gate 	/*
551*0Sstevel@tonic-gate 	 * Allocate the hash chains and hash buckets for symbol name lookup.
552*0Sstevel@tonic-gate 	 * This is relatively simple since the symbol table is of fixed size
553*0Sstevel@tonic-gate 	 * and is known in advance.  We allocate one extra element since we
554*0Sstevel@tonic-gate 	 * use element indices instead of pointers and zero is our sentinel.
555*0Sstevel@tonic-gate 	 */
556*0Sstevel@tonic-gate 	dmp->dm_nsymelems =
557*0Sstevel@tonic-gate 	    dmp->dm_symtab.cts_size / dmp->dm_symtab.cts_entsize;
558*0Sstevel@tonic-gate 
559*0Sstevel@tonic-gate 	dmp->dm_nsymbuckets = _dtrace_strbuckets;
560*0Sstevel@tonic-gate 	dmp->dm_symfree = 1;		/* first free element is index 1 */
561*0Sstevel@tonic-gate 
562*0Sstevel@tonic-gate 	dmp->dm_symbuckets = malloc(sizeof (uint_t) * dmp->dm_nsymbuckets);
563*0Sstevel@tonic-gate 	dmp->dm_symchains = malloc(sizeof (dt_sym_t) * dmp->dm_nsymelems + 1);
564*0Sstevel@tonic-gate 
565*0Sstevel@tonic-gate 	if (dmp->dm_symbuckets == NULL || dmp->dm_symchains == NULL) {
566*0Sstevel@tonic-gate 		dt_module_unload(dtp, dmp);
567*0Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_NOMEM));
568*0Sstevel@tonic-gate 	}
569*0Sstevel@tonic-gate 
570*0Sstevel@tonic-gate 	bzero(dmp->dm_symbuckets, sizeof (uint_t) * dmp->dm_nsymbuckets);
571*0Sstevel@tonic-gate 	bzero(dmp->dm_symchains, sizeof (dt_sym_t) * dmp->dm_nsymelems + 1);
572*0Sstevel@tonic-gate 
573*0Sstevel@tonic-gate 	/*
574*0Sstevel@tonic-gate 	 * Iterate over the symbol table data buffer and insert each symbol
575*0Sstevel@tonic-gate 	 * name into the name hash if the name and type are valid.  Then
576*0Sstevel@tonic-gate 	 * allocate the address map, fill it in, and sort it.
577*0Sstevel@tonic-gate 	 */
578*0Sstevel@tonic-gate 	dmp->dm_asrsv = dmp->dm_ops->do_syminit(dmp);
579*0Sstevel@tonic-gate 
580*0Sstevel@tonic-gate 	dt_dprintf("hashed %s [%s] (%u symbols)\n",
581*0Sstevel@tonic-gate 	    dmp->dm_name, dmp->dm_symtab.cts_name, dmp->dm_symfree - 1);
582*0Sstevel@tonic-gate 
583*0Sstevel@tonic-gate 	if ((dmp->dm_asmap = malloc(sizeof (void *) * dmp->dm_asrsv)) == NULL) {
584*0Sstevel@tonic-gate 		dt_module_unload(dtp, dmp);
585*0Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_NOMEM));
586*0Sstevel@tonic-gate 	}
587*0Sstevel@tonic-gate 
588*0Sstevel@tonic-gate 	dmp->dm_ops->do_symsort(dmp);
589*0Sstevel@tonic-gate 
590*0Sstevel@tonic-gate 	dt_dprintf("sorted %s [%s] (%u symbols)\n",
591*0Sstevel@tonic-gate 	    dmp->dm_name, dmp->dm_symtab.cts_name, dmp->dm_aslen);
592*0Sstevel@tonic-gate 
593*0Sstevel@tonic-gate 	dmp->dm_flags |= DT_DM_LOADED;
594*0Sstevel@tonic-gate 	return (0);
595*0Sstevel@tonic-gate }
596*0Sstevel@tonic-gate 
597*0Sstevel@tonic-gate ctf_file_t *
598*0Sstevel@tonic-gate dt_module_getctf(dtrace_hdl_t *dtp, dt_module_t *dmp)
599*0Sstevel@tonic-gate {
600*0Sstevel@tonic-gate 	const char *parent;
601*0Sstevel@tonic-gate 	dt_module_t *pmp;
602*0Sstevel@tonic-gate 	ctf_file_t *pfp;
603*0Sstevel@tonic-gate 	int model;
604*0Sstevel@tonic-gate 
605*0Sstevel@tonic-gate 	if (dmp->dm_ctfp != NULL || dt_module_load(dtp, dmp) != 0)
606*0Sstevel@tonic-gate 		return (dmp->dm_ctfp);
607*0Sstevel@tonic-gate 
608*0Sstevel@tonic-gate 	if (dmp->dm_ops == &dt_modops_64)
609*0Sstevel@tonic-gate 		model = CTF_MODEL_LP64;
610*0Sstevel@tonic-gate 	else
611*0Sstevel@tonic-gate 		model = CTF_MODEL_ILP32;
612*0Sstevel@tonic-gate 
613*0Sstevel@tonic-gate 	/*
614*0Sstevel@tonic-gate 	 * If the data model of the module does not match our program data
615*0Sstevel@tonic-gate 	 * model, then do not permit CTF from this module to be opened and
616*0Sstevel@tonic-gate 	 * returned to the compiler.  If we support mixed data models in the
617*0Sstevel@tonic-gate 	 * future for combined kernel/user tracing, this can be removed.
618*0Sstevel@tonic-gate 	 */
619*0Sstevel@tonic-gate 	if (dtp->dt_conf.dtc_ctfmodel != model) {
620*0Sstevel@tonic-gate 		(void) dt_set_errno(dtp, EDT_DATAMODEL);
621*0Sstevel@tonic-gate 		return (NULL);
622*0Sstevel@tonic-gate 	}
623*0Sstevel@tonic-gate 
624*0Sstevel@tonic-gate 	if (dmp->dm_ctdata.cts_size == 0) {
625*0Sstevel@tonic-gate 		(void) dt_set_errno(dtp, EDT_NOCTF);
626*0Sstevel@tonic-gate 		return (NULL);
627*0Sstevel@tonic-gate 	}
628*0Sstevel@tonic-gate 
629*0Sstevel@tonic-gate 	dmp->dm_ctfp = ctf_bufopen(&dmp->dm_ctdata,
630*0Sstevel@tonic-gate 	    &dmp->dm_symtab, &dmp->dm_strtab, &dtp->dt_ctferr);
631*0Sstevel@tonic-gate 
632*0Sstevel@tonic-gate 	if (dmp->dm_ctfp == NULL) {
633*0Sstevel@tonic-gate 		(void) dt_set_errno(dtp, EDT_CTF);
634*0Sstevel@tonic-gate 		return (NULL);
635*0Sstevel@tonic-gate 	}
636*0Sstevel@tonic-gate 
637*0Sstevel@tonic-gate 	(void) ctf_setmodel(dmp->dm_ctfp, model);
638*0Sstevel@tonic-gate 	ctf_setspecific(dmp->dm_ctfp, dmp);
639*0Sstevel@tonic-gate 
640*0Sstevel@tonic-gate 	if ((parent = ctf_parent_name(dmp->dm_ctfp)) != NULL) {
641*0Sstevel@tonic-gate 		if ((pmp = dt_module_create(dtp, parent)) == NULL ||
642*0Sstevel@tonic-gate 		    (pfp = dt_module_getctf(dtp, pmp)) == NULL) {
643*0Sstevel@tonic-gate 			if (pmp == NULL)
644*0Sstevel@tonic-gate 				(void) dt_set_errno(dtp, EDT_NOMEM);
645*0Sstevel@tonic-gate 			goto err;
646*0Sstevel@tonic-gate 		}
647*0Sstevel@tonic-gate 
648*0Sstevel@tonic-gate 		if (ctf_import(dmp->dm_ctfp, pfp) == CTF_ERR) {
649*0Sstevel@tonic-gate 			dtp->dt_ctferr = ctf_errno(dmp->dm_ctfp);
650*0Sstevel@tonic-gate 			(void) dt_set_errno(dtp, EDT_CTF);
651*0Sstevel@tonic-gate 			goto err;
652*0Sstevel@tonic-gate 		}
653*0Sstevel@tonic-gate 	}
654*0Sstevel@tonic-gate 
655*0Sstevel@tonic-gate 	dt_dprintf("loaded CTF container for %s (%p)\n",
656*0Sstevel@tonic-gate 	    dmp->dm_name, (void *)dmp->dm_ctfp);
657*0Sstevel@tonic-gate 
658*0Sstevel@tonic-gate 	return (dmp->dm_ctfp);
659*0Sstevel@tonic-gate 
660*0Sstevel@tonic-gate err:
661*0Sstevel@tonic-gate 	ctf_close(dmp->dm_ctfp);
662*0Sstevel@tonic-gate 	dmp->dm_ctfp = NULL;
663*0Sstevel@tonic-gate 	return (NULL);
664*0Sstevel@tonic-gate }
665*0Sstevel@tonic-gate 
666*0Sstevel@tonic-gate /*ARGSUSED*/
667*0Sstevel@tonic-gate void
668*0Sstevel@tonic-gate dt_module_unload(dtrace_hdl_t *dtp, dt_module_t *dmp)
669*0Sstevel@tonic-gate {
670*0Sstevel@tonic-gate 	ctf_close(dmp->dm_ctfp);
671*0Sstevel@tonic-gate 	dmp->dm_ctfp = NULL;
672*0Sstevel@tonic-gate 
673*0Sstevel@tonic-gate 	bzero(&dmp->dm_ctdata, sizeof (ctf_sect_t));
674*0Sstevel@tonic-gate 	bzero(&dmp->dm_symtab, sizeof (ctf_sect_t));
675*0Sstevel@tonic-gate 	bzero(&dmp->dm_strtab, sizeof (ctf_sect_t));
676*0Sstevel@tonic-gate 
677*0Sstevel@tonic-gate 	if (dmp->dm_symbuckets != NULL) {
678*0Sstevel@tonic-gate 		free(dmp->dm_symbuckets);
679*0Sstevel@tonic-gate 		dmp->dm_symbuckets = NULL;
680*0Sstevel@tonic-gate 	}
681*0Sstevel@tonic-gate 
682*0Sstevel@tonic-gate 	if (dmp->dm_symchains != NULL) {
683*0Sstevel@tonic-gate 		free(dmp->dm_symchains);
684*0Sstevel@tonic-gate 		dmp->dm_symchains = NULL;
685*0Sstevel@tonic-gate 	}
686*0Sstevel@tonic-gate 
687*0Sstevel@tonic-gate 	if (dmp->dm_asmap != NULL) {
688*0Sstevel@tonic-gate 		free(dmp->dm_asmap);
689*0Sstevel@tonic-gate 		dmp->dm_asmap = NULL;
690*0Sstevel@tonic-gate 	}
691*0Sstevel@tonic-gate 
692*0Sstevel@tonic-gate 	dmp->dm_symfree = 0;
693*0Sstevel@tonic-gate 	dmp->dm_nsymbuckets = 0;
694*0Sstevel@tonic-gate 	dmp->dm_nsymelems = 0;
695*0Sstevel@tonic-gate 	dmp->dm_asrsv = 0;
696*0Sstevel@tonic-gate 	dmp->dm_aslen = 0;
697*0Sstevel@tonic-gate 
698*0Sstevel@tonic-gate 	dmp->dm_text_va = NULL;
699*0Sstevel@tonic-gate 	dmp->dm_text_size = 0;
700*0Sstevel@tonic-gate 	dmp->dm_data_va = NULL;
701*0Sstevel@tonic-gate 	dmp->dm_data_size = 0;
702*0Sstevel@tonic-gate 	dmp->dm_bss_va = NULL;
703*0Sstevel@tonic-gate 	dmp->dm_bss_size = 0;
704*0Sstevel@tonic-gate 
705*0Sstevel@tonic-gate 	if (dmp->dm_extern != NULL) {
706*0Sstevel@tonic-gate 		dt_idhash_destroy(dmp->dm_extern);
707*0Sstevel@tonic-gate 		dmp->dm_extern = NULL;
708*0Sstevel@tonic-gate 	}
709*0Sstevel@tonic-gate 
710*0Sstevel@tonic-gate 	(void) elf_end(dmp->dm_elf);
711*0Sstevel@tonic-gate 	dmp->dm_elf = NULL;
712*0Sstevel@tonic-gate 
713*0Sstevel@tonic-gate 	dmp->dm_flags &= ~DT_DM_LOADED;
714*0Sstevel@tonic-gate }
715*0Sstevel@tonic-gate 
716*0Sstevel@tonic-gate void
717*0Sstevel@tonic-gate dt_module_destroy(dtrace_hdl_t *dtp, dt_module_t *dmp)
718*0Sstevel@tonic-gate {
719*0Sstevel@tonic-gate 	dt_list_delete(&dtp->dt_modlist, dmp);
720*0Sstevel@tonic-gate 	assert(dtp->dt_nmods != 0);
721*0Sstevel@tonic-gate 	dtp->dt_nmods--;
722*0Sstevel@tonic-gate 
723*0Sstevel@tonic-gate 	dt_module_unload(dtp, dmp);
724*0Sstevel@tonic-gate 	free(dmp);
725*0Sstevel@tonic-gate }
726*0Sstevel@tonic-gate 
727*0Sstevel@tonic-gate /*
728*0Sstevel@tonic-gate  * Insert a new external symbol reference into the specified module.  The new
729*0Sstevel@tonic-gate  * symbol will be marked as undefined and is assigned a symbol index beyond
730*0Sstevel@tonic-gate  * any existing cached symbols from this module.  We use the ident's di_data
731*0Sstevel@tonic-gate  * field to store a pointer to a copy of the dtrace_syminfo_t for this symbol.
732*0Sstevel@tonic-gate  */
733*0Sstevel@tonic-gate dt_ident_t *
734*0Sstevel@tonic-gate dt_module_extern(dtrace_hdl_t *dtp, dt_module_t *dmp,
735*0Sstevel@tonic-gate     const char *name, const dtrace_typeinfo_t *tip)
736*0Sstevel@tonic-gate {
737*0Sstevel@tonic-gate 	dtrace_syminfo_t *sip;
738*0Sstevel@tonic-gate 	dt_ident_t *idp;
739*0Sstevel@tonic-gate 	uint_t id;
740*0Sstevel@tonic-gate 
741*0Sstevel@tonic-gate 	if (dmp->dm_extern == NULL && (dmp->dm_extern = dt_idhash_create(
742*0Sstevel@tonic-gate 	    "extern", NULL, dmp->dm_nsymelems, UINT_MAX)) == NULL) {
743*0Sstevel@tonic-gate 		(void) dt_set_errno(dtp, EDT_NOMEM);
744*0Sstevel@tonic-gate 		return (NULL);
745*0Sstevel@tonic-gate 	}
746*0Sstevel@tonic-gate 
747*0Sstevel@tonic-gate 	if (dt_idhash_nextid(dmp->dm_extern, &id) == -1) {
748*0Sstevel@tonic-gate 		(void) dt_set_errno(dtp, EDT_SYMOFLOW);
749*0Sstevel@tonic-gate 		return (NULL);
750*0Sstevel@tonic-gate 	}
751*0Sstevel@tonic-gate 
752*0Sstevel@tonic-gate 	if ((sip = malloc(sizeof (dtrace_syminfo_t))) == NULL) {
753*0Sstevel@tonic-gate 		(void) dt_set_errno(dtp, EDT_NOMEM);
754*0Sstevel@tonic-gate 		return (NULL);
755*0Sstevel@tonic-gate 	}
756*0Sstevel@tonic-gate 
757*0Sstevel@tonic-gate 	idp = dt_idhash_insert(dmp->dm_extern, name, DT_IDENT_SYMBOL, 0, id,
758*0Sstevel@tonic-gate 	    _dtrace_symattr, 0, &dt_idops_thaw, NULL, dtp->dt_gen);
759*0Sstevel@tonic-gate 
760*0Sstevel@tonic-gate 	if (idp == NULL) {
761*0Sstevel@tonic-gate 		(void) dt_set_errno(dtp, EDT_NOMEM);
762*0Sstevel@tonic-gate 		free(sip);
763*0Sstevel@tonic-gate 		return (NULL);
764*0Sstevel@tonic-gate 	}
765*0Sstevel@tonic-gate 
766*0Sstevel@tonic-gate 	sip->dts_object = dmp->dm_name;
767*0Sstevel@tonic-gate 	sip->dts_name = idp->di_name;
768*0Sstevel@tonic-gate 	sip->dts_id = idp->di_id;
769*0Sstevel@tonic-gate 
770*0Sstevel@tonic-gate 	idp->di_data = sip;
771*0Sstevel@tonic-gate 	idp->di_ctfp = tip->dtt_ctfp;
772*0Sstevel@tonic-gate 	idp->di_type = tip->dtt_type;
773*0Sstevel@tonic-gate 
774*0Sstevel@tonic-gate 	return (idp);
775*0Sstevel@tonic-gate }
776*0Sstevel@tonic-gate 
777*0Sstevel@tonic-gate const char *
778*0Sstevel@tonic-gate dt_module_modelname(dt_module_t *dmp)
779*0Sstevel@tonic-gate {
780*0Sstevel@tonic-gate 	if (dmp->dm_ops == &dt_modops_64)
781*0Sstevel@tonic-gate 		return ("64-bit");
782*0Sstevel@tonic-gate 	else
783*0Sstevel@tonic-gate 		return ("32-bit");
784*0Sstevel@tonic-gate }
785*0Sstevel@tonic-gate 
786*0Sstevel@tonic-gate /*
787*0Sstevel@tonic-gate  * Update our module cache by adding an entry for the specified module 'name'.
788*0Sstevel@tonic-gate  * We create the dt_module_t and populate it using /system/object/<name>/.
789*0Sstevel@tonic-gate  */
790*0Sstevel@tonic-gate static void
791*0Sstevel@tonic-gate dt_module_update(dtrace_hdl_t *dtp, const char *name)
792*0Sstevel@tonic-gate {
793*0Sstevel@tonic-gate 	char fname[MAXPATHLEN];
794*0Sstevel@tonic-gate 	struct stat64 st;
795*0Sstevel@tonic-gate 	int fd, err, bits;
796*0Sstevel@tonic-gate 
797*0Sstevel@tonic-gate 	dt_module_t *dmp;
798*0Sstevel@tonic-gate 	const char *s;
799*0Sstevel@tonic-gate 	size_t shstrs;
800*0Sstevel@tonic-gate 	GElf_Shdr sh;
801*0Sstevel@tonic-gate 	Elf_Data *dp;
802*0Sstevel@tonic-gate 	Elf_Scn *sp;
803*0Sstevel@tonic-gate 
804*0Sstevel@tonic-gate 	(void) snprintf(fname, sizeof (fname),
805*0Sstevel@tonic-gate 	    "%s/%s/object", OBJFS_ROOT, name);
806*0Sstevel@tonic-gate 
807*0Sstevel@tonic-gate 	if ((fd = open(fname, O_RDONLY)) == -1 || fstat64(fd, &st) == -1 ||
808*0Sstevel@tonic-gate 	    (dmp = dt_module_create(dtp, name)) == NULL) {
809*0Sstevel@tonic-gate 		dt_dprintf("failed to open %s: %s\n", fname, strerror(errno));
810*0Sstevel@tonic-gate 		(void) close(fd);
811*0Sstevel@tonic-gate 		return;
812*0Sstevel@tonic-gate 	}
813*0Sstevel@tonic-gate 
814*0Sstevel@tonic-gate 	/*
815*0Sstevel@tonic-gate 	 * Since the module can unload out from under us (and /system/object
816*0Sstevel@tonic-gate 	 * will return ENOENT), tell libelf to cook the entire file now and
817*0Sstevel@tonic-gate 	 * then close the underlying file descriptor immediately.  If this
818*0Sstevel@tonic-gate 	 * succeeds, we know that we can continue safely using dmp->dm_elf.
819*0Sstevel@tonic-gate 	 */
820*0Sstevel@tonic-gate 	dmp->dm_elf = elf_begin(fd, ELF_C_READ, NULL);
821*0Sstevel@tonic-gate 	err = elf_cntl(dmp->dm_elf, ELF_C_FDREAD);
822*0Sstevel@tonic-gate 	(void) close(fd);
823*0Sstevel@tonic-gate 
824*0Sstevel@tonic-gate 	if (dmp->dm_elf == NULL || err == -1 ||
825*0Sstevel@tonic-gate 	    elf_getshstrndx(dmp->dm_elf, &shstrs) == 0) {
826*0Sstevel@tonic-gate 		dt_dprintf("failed to load %s: %s\n",
827*0Sstevel@tonic-gate 		    fname, elf_errmsg(elf_errno()));
828*0Sstevel@tonic-gate 		dt_module_destroy(dtp, dmp);
829*0Sstevel@tonic-gate 		return;
830*0Sstevel@tonic-gate 	}
831*0Sstevel@tonic-gate 
832*0Sstevel@tonic-gate 	switch (gelf_getclass(dmp->dm_elf)) {
833*0Sstevel@tonic-gate 	case ELFCLASS32:
834*0Sstevel@tonic-gate 		dmp->dm_ops = &dt_modops_32;
835*0Sstevel@tonic-gate 		bits = 32;
836*0Sstevel@tonic-gate 		break;
837*0Sstevel@tonic-gate 	case ELFCLASS64:
838*0Sstevel@tonic-gate 		dmp->dm_ops = &dt_modops_64;
839*0Sstevel@tonic-gate 		bits = 64;
840*0Sstevel@tonic-gate 		break;
841*0Sstevel@tonic-gate 	default:
842*0Sstevel@tonic-gate 		dt_dprintf("failed to load %s: unknown ELF class\n", fname);
843*0Sstevel@tonic-gate 		dt_module_destroy(dtp, dmp);
844*0Sstevel@tonic-gate 		return;
845*0Sstevel@tonic-gate 	}
846*0Sstevel@tonic-gate 
847*0Sstevel@tonic-gate 	/*
848*0Sstevel@tonic-gate 	 * Iterate over the section headers locating various sections of
849*0Sstevel@tonic-gate 	 * interest and use their attributes to flesh out the dt_module_t.
850*0Sstevel@tonic-gate 	 */
851*0Sstevel@tonic-gate 	for (sp = NULL; (sp = elf_nextscn(dmp->dm_elf, sp)) != NULL; ) {
852*0Sstevel@tonic-gate 		if (gelf_getshdr(sp, &sh) == NULL || sh.sh_type == SHT_NULL ||
853*0Sstevel@tonic-gate 		    (s = elf_strptr(dmp->dm_elf, shstrs, sh.sh_name)) == NULL)
854*0Sstevel@tonic-gate 			continue; /* skip any malformed sections */
855*0Sstevel@tonic-gate 
856*0Sstevel@tonic-gate 		if (strcmp(s, ".text") == 0) {
857*0Sstevel@tonic-gate 			dmp->dm_text_size = sh.sh_size;
858*0Sstevel@tonic-gate 			dmp->dm_text_va = sh.sh_addr;
859*0Sstevel@tonic-gate 		} else if (strcmp(s, ".data") == 0) {
860*0Sstevel@tonic-gate 			dmp->dm_data_size = sh.sh_size;
861*0Sstevel@tonic-gate 			dmp->dm_data_va = sh.sh_addr;
862*0Sstevel@tonic-gate 		} else if (strcmp(s, ".bss") == 0) {
863*0Sstevel@tonic-gate 			dmp->dm_bss_size = sh.sh_size;
864*0Sstevel@tonic-gate 			dmp->dm_bss_va = sh.sh_addr;
865*0Sstevel@tonic-gate 		} else if (strcmp(s, ".info") == 0 &&
866*0Sstevel@tonic-gate 		    (dp = elf_getdata(sp, NULL)) != NULL) {
867*0Sstevel@tonic-gate 			bcopy(dp->d_buf, &dmp->dm_info,
868*0Sstevel@tonic-gate 			    MIN(sh.sh_size, sizeof (dmp->dm_info)));
869*0Sstevel@tonic-gate 		} else if (strcmp(s, ".filename") == 0 &&
870*0Sstevel@tonic-gate 		    (dp = elf_getdata(sp, NULL)) != NULL) {
871*0Sstevel@tonic-gate 			(void) strlcpy(dmp->dm_file,
872*0Sstevel@tonic-gate 			    dp->d_buf, sizeof (dmp->dm_file));
873*0Sstevel@tonic-gate 		}
874*0Sstevel@tonic-gate 	}
875*0Sstevel@tonic-gate 
876*0Sstevel@tonic-gate 	dmp->dm_flags |= DT_DM_KERNEL;
877*0Sstevel@tonic-gate 	dmp->dm_modid = (int)OBJFS_MODID(st.st_ino);
878*0Sstevel@tonic-gate 
879*0Sstevel@tonic-gate 	if (dmp->dm_info.objfs_info_primary)
880*0Sstevel@tonic-gate 		dmp->dm_flags |= DT_DM_PRIMARY;
881*0Sstevel@tonic-gate 
882*0Sstevel@tonic-gate 	dt_dprintf("opened %d-bit module %s (%s) [%d]\n",
883*0Sstevel@tonic-gate 	    bits, dmp->dm_name, dmp->dm_file, dmp->dm_modid);
884*0Sstevel@tonic-gate }
885*0Sstevel@tonic-gate 
886*0Sstevel@tonic-gate /*
887*0Sstevel@tonic-gate  * Unload all the loaded modules and then refresh the module cache with the
888*0Sstevel@tonic-gate  * latest list of loaded modules and their address ranges.
889*0Sstevel@tonic-gate  */
890*0Sstevel@tonic-gate void
891*0Sstevel@tonic-gate dtrace_update(dtrace_hdl_t *dtp)
892*0Sstevel@tonic-gate {
893*0Sstevel@tonic-gate 	dt_module_t *dmp;
894*0Sstevel@tonic-gate 	DIR *dirp;
895*0Sstevel@tonic-gate 
896*0Sstevel@tonic-gate 	for (dmp = dt_list_next(&dtp->dt_modlist);
897*0Sstevel@tonic-gate 	    dmp != NULL; dmp = dt_list_next(dmp))
898*0Sstevel@tonic-gate 		dt_module_unload(dtp, dmp);
899*0Sstevel@tonic-gate 
900*0Sstevel@tonic-gate 	/*
901*0Sstevel@tonic-gate 	 * Open /system/object and attempt to create a libdtrace module for
902*0Sstevel@tonic-gate 	 * each kernel module that is loaded on the current system.
903*0Sstevel@tonic-gate 	 */
904*0Sstevel@tonic-gate 	if (!(dtp->dt_oflags & DTRACE_O_NOSYS) &&
905*0Sstevel@tonic-gate 	    (dirp = opendir(OBJFS_ROOT)) != NULL) {
906*0Sstevel@tonic-gate 		struct dirent *dp, *ep;
907*0Sstevel@tonic-gate 
908*0Sstevel@tonic-gate 		ep = alloca(sizeof (struct dirent) + PATH_MAX + 1);
909*0Sstevel@tonic-gate 		bzero(ep, sizeof (struct dirent) + PATH_MAX + 1);
910*0Sstevel@tonic-gate 
911*0Sstevel@tonic-gate 		while (readdir_r(dirp, ep, &dp) == 0 && dp != NULL) {
912*0Sstevel@tonic-gate 			if (dp->d_name[0] != '.')
913*0Sstevel@tonic-gate 				dt_module_update(dtp, dp->d_name);
914*0Sstevel@tonic-gate 		}
915*0Sstevel@tonic-gate 
916*0Sstevel@tonic-gate 		(void) closedir(dirp);
917*0Sstevel@tonic-gate 	}
918*0Sstevel@tonic-gate 
919*0Sstevel@tonic-gate 	/*
920*0Sstevel@tonic-gate 	 * Look up all the macro identifiers and set di_id to the latest value.
921*0Sstevel@tonic-gate 	 * This code collaborates with dt_lex.l on the use of di_id.  We will
922*0Sstevel@tonic-gate 	 * need to implement something fancier if we need to support non-ints.
923*0Sstevel@tonic-gate 	 */
924*0Sstevel@tonic-gate 	dt_idhash_lookup(dtp->dt_macros, "egid")->di_id = getegid();
925*0Sstevel@tonic-gate 	dt_idhash_lookup(dtp->dt_macros, "euid")->di_id = geteuid();
926*0Sstevel@tonic-gate 	dt_idhash_lookup(dtp->dt_macros, "gid")->di_id = getgid();
927*0Sstevel@tonic-gate 	dt_idhash_lookup(dtp->dt_macros, "pid")->di_id = getpid();
928*0Sstevel@tonic-gate 	dt_idhash_lookup(dtp->dt_macros, "pgid")->di_id = getpgid(0);
929*0Sstevel@tonic-gate 	dt_idhash_lookup(dtp->dt_macros, "ppid")->di_id = getppid();
930*0Sstevel@tonic-gate 	dt_idhash_lookup(dtp->dt_macros, "projid")->di_id = getprojid();
931*0Sstevel@tonic-gate 	dt_idhash_lookup(dtp->dt_macros, "sid")->di_id = getsid(0);
932*0Sstevel@tonic-gate 	dt_idhash_lookup(dtp->dt_macros, "taskid")->di_id = gettaskid();
933*0Sstevel@tonic-gate 	dt_idhash_lookup(dtp->dt_macros, "uid")->di_id = getuid();
934*0Sstevel@tonic-gate 
935*0Sstevel@tonic-gate 	/*
936*0Sstevel@tonic-gate 	 * Cache the pointers to the modules representing the base executable
937*0Sstevel@tonic-gate 	 * and the run-time linker in the dtrace client handle.  We should
938*0Sstevel@tonic-gate 	 * probably have a more generic way of inquiring as to their names.
939*0Sstevel@tonic-gate 	 */
940*0Sstevel@tonic-gate 	dtp->dt_exec = dt_module_lookup_by_name(dtp, "genunix");
941*0Sstevel@tonic-gate 	dtp->dt_rtld = dt_module_lookup_by_name(dtp, "krtld");
942*0Sstevel@tonic-gate 
943*0Sstevel@tonic-gate 	/*
944*0Sstevel@tonic-gate 	 * If this is the first time we are initializing the module list,
945*0Sstevel@tonic-gate 	 * remove the module for genunix from the module list and then move it
946*0Sstevel@tonic-gate 	 * to the front of the module list.  We do this so that type and symbol
947*0Sstevel@tonic-gate 	 * queries encounter genunix and thereby optimize for the common case
948*0Sstevel@tonic-gate 	 * in dtrace_lookup_by_name() and dtrace_lookup_by_type(), below.
949*0Sstevel@tonic-gate 	 */
950*0Sstevel@tonic-gate 	if (dtp->dt_exec != NULL &&
951*0Sstevel@tonic-gate 	    dtp->dt_cdefs == NULL && dtp->dt_ddefs == NULL) {
952*0Sstevel@tonic-gate 		dt_list_delete(&dtp->dt_modlist, dtp->dt_exec);
953*0Sstevel@tonic-gate 		dt_list_prepend(&dtp->dt_modlist, dtp->dt_exec);
954*0Sstevel@tonic-gate 	}
955*0Sstevel@tonic-gate }
956*0Sstevel@tonic-gate 
957*0Sstevel@tonic-gate static dt_module_t *
958*0Sstevel@tonic-gate dt_module_from_object(dtrace_hdl_t *dtp, const char *object)
959*0Sstevel@tonic-gate {
960*0Sstevel@tonic-gate 	int err = EDT_NOMOD;
961*0Sstevel@tonic-gate 	dt_module_t *dmp;
962*0Sstevel@tonic-gate 
963*0Sstevel@tonic-gate 	switch ((uintptr_t)object) {
964*0Sstevel@tonic-gate 	case (uintptr_t)DTRACE_OBJ_EXEC:
965*0Sstevel@tonic-gate 		dmp = dtp->dt_exec;
966*0Sstevel@tonic-gate 		break;
967*0Sstevel@tonic-gate 	case (uintptr_t)DTRACE_OBJ_RTLD:
968*0Sstevel@tonic-gate 		dmp = dtp->dt_rtld;
969*0Sstevel@tonic-gate 		break;
970*0Sstevel@tonic-gate 	case (uintptr_t)DTRACE_OBJ_CDEFS:
971*0Sstevel@tonic-gate 		dmp = dtp->dt_cdefs;
972*0Sstevel@tonic-gate 		break;
973*0Sstevel@tonic-gate 	case (uintptr_t)DTRACE_OBJ_DDEFS:
974*0Sstevel@tonic-gate 		dmp = dtp->dt_ddefs;
975*0Sstevel@tonic-gate 		break;
976*0Sstevel@tonic-gate 	default:
977*0Sstevel@tonic-gate 		dmp = dt_module_create(dtp, object);
978*0Sstevel@tonic-gate 		err = EDT_NOMEM;
979*0Sstevel@tonic-gate 	}
980*0Sstevel@tonic-gate 
981*0Sstevel@tonic-gate 	if (dmp == NULL)
982*0Sstevel@tonic-gate 		(void) dt_set_errno(dtp, err);
983*0Sstevel@tonic-gate 
984*0Sstevel@tonic-gate 	return (dmp);
985*0Sstevel@tonic-gate }
986*0Sstevel@tonic-gate 
987*0Sstevel@tonic-gate /*
988*0Sstevel@tonic-gate  * Exported interface to look up a symbol by name.  We return the GElf_Sym and
989*0Sstevel@tonic-gate  * complete symbol information for the matching symbol.
990*0Sstevel@tonic-gate  */
991*0Sstevel@tonic-gate int
992*0Sstevel@tonic-gate dtrace_lookup_by_name(dtrace_hdl_t *dtp, const char *object, const char *name,
993*0Sstevel@tonic-gate     GElf_Sym *symp, dtrace_syminfo_t *sip)
994*0Sstevel@tonic-gate {
995*0Sstevel@tonic-gate 	dt_module_t *dmp;
996*0Sstevel@tonic-gate 	dt_ident_t *idp;
997*0Sstevel@tonic-gate 	uint_t n, id;
998*0Sstevel@tonic-gate 	GElf_Sym sym;
999*0Sstevel@tonic-gate 
1000*0Sstevel@tonic-gate 	uint_t mask = 0; /* mask of dt_module flags to match */
1001*0Sstevel@tonic-gate 	uint_t bits = 0; /* flag bits that must be present */
1002*0Sstevel@tonic-gate 
1003*0Sstevel@tonic-gate 	if (object != DTRACE_OBJ_EVERY &&
1004*0Sstevel@tonic-gate 	    object != DTRACE_OBJ_KMODS &&
1005*0Sstevel@tonic-gate 	    object != DTRACE_OBJ_UMODS) {
1006*0Sstevel@tonic-gate 		if ((dmp = dt_module_from_object(dtp, object)) == NULL)
1007*0Sstevel@tonic-gate 			return (-1); /* dt_errno is set for us */
1008*0Sstevel@tonic-gate 
1009*0Sstevel@tonic-gate 		if (dt_module_load(dtp, dmp) == -1)
1010*0Sstevel@tonic-gate 			return (-1); /* dt_errno is set for us */
1011*0Sstevel@tonic-gate 		n = 1;
1012*0Sstevel@tonic-gate 
1013*0Sstevel@tonic-gate 	} else {
1014*0Sstevel@tonic-gate 		if (object == DTRACE_OBJ_KMODS)
1015*0Sstevel@tonic-gate 			mask = bits = DT_DM_KERNEL;
1016*0Sstevel@tonic-gate 		else if (object == DTRACE_OBJ_UMODS)
1017*0Sstevel@tonic-gate 			mask = DT_DM_KERNEL;
1018*0Sstevel@tonic-gate 
1019*0Sstevel@tonic-gate 		dmp = dt_list_next(&dtp->dt_modlist);
1020*0Sstevel@tonic-gate 		n = dtp->dt_nmods;
1021*0Sstevel@tonic-gate 	}
1022*0Sstevel@tonic-gate 
1023*0Sstevel@tonic-gate 	if (symp == NULL)
1024*0Sstevel@tonic-gate 		symp = &sym;
1025*0Sstevel@tonic-gate 
1026*0Sstevel@tonic-gate 	for (; n > 0; n--, dmp = dt_list_next(dmp)) {
1027*0Sstevel@tonic-gate 		if ((dmp->dm_flags & mask) != bits)
1028*0Sstevel@tonic-gate 			continue; /* failed to match required attributes */
1029*0Sstevel@tonic-gate 
1030*0Sstevel@tonic-gate 		if (dt_module_load(dtp, dmp) == -1)
1031*0Sstevel@tonic-gate 			continue; /* failed to load symbol table */
1032*0Sstevel@tonic-gate 
1033*0Sstevel@tonic-gate 		if (dmp->dm_ops->do_symname(dmp, name, symp, &id) != NULL) {
1034*0Sstevel@tonic-gate 			if (sip != NULL) {
1035*0Sstevel@tonic-gate 				sip->dts_object = dmp->dm_name;
1036*0Sstevel@tonic-gate 				sip->dts_name = (const char *)
1037*0Sstevel@tonic-gate 				    dmp->dm_strtab.cts_data + symp->st_name;
1038*0Sstevel@tonic-gate 				sip->dts_id = id;
1039*0Sstevel@tonic-gate 			}
1040*0Sstevel@tonic-gate 			return (0);
1041*0Sstevel@tonic-gate 		}
1042*0Sstevel@tonic-gate 
1043*0Sstevel@tonic-gate 		if (dmp->dm_extern != NULL &&
1044*0Sstevel@tonic-gate 		    (idp = dt_idhash_lookup(dmp->dm_extern, name)) != NULL) {
1045*0Sstevel@tonic-gate 			if (symp != &sym) {
1046*0Sstevel@tonic-gate 				symp->st_name = (uintptr_t)idp->di_name;
1047*0Sstevel@tonic-gate 				symp->st_info =
1048*0Sstevel@tonic-gate 				    GELF_ST_INFO(STB_GLOBAL, STT_NOTYPE);
1049*0Sstevel@tonic-gate 				symp->st_other = 0;
1050*0Sstevel@tonic-gate 				symp->st_shndx = SHN_UNDEF;
1051*0Sstevel@tonic-gate 				symp->st_value = 0;
1052*0Sstevel@tonic-gate 				symp->st_size =
1053*0Sstevel@tonic-gate 				    ctf_type_size(idp->di_ctfp, idp->di_type);
1054*0Sstevel@tonic-gate 			}
1055*0Sstevel@tonic-gate 
1056*0Sstevel@tonic-gate 			if (sip != NULL) {
1057*0Sstevel@tonic-gate 				sip->dts_object = dmp->dm_name;
1058*0Sstevel@tonic-gate 				sip->dts_name = idp->di_name;
1059*0Sstevel@tonic-gate 				sip->dts_id = idp->di_id;
1060*0Sstevel@tonic-gate 			}
1061*0Sstevel@tonic-gate 
1062*0Sstevel@tonic-gate 			return (0);
1063*0Sstevel@tonic-gate 		}
1064*0Sstevel@tonic-gate 	}
1065*0Sstevel@tonic-gate 
1066*0Sstevel@tonic-gate 	return (dt_set_errno(dtp, EDT_NOSYM));
1067*0Sstevel@tonic-gate }
1068*0Sstevel@tonic-gate 
1069*0Sstevel@tonic-gate /*
1070*0Sstevel@tonic-gate  * Exported interface to look up a symbol by address.  We return the GElf_Sym
1071*0Sstevel@tonic-gate  * and complete symbol information for the matching symbol.
1072*0Sstevel@tonic-gate  */
1073*0Sstevel@tonic-gate int
1074*0Sstevel@tonic-gate dtrace_lookup_by_addr(dtrace_hdl_t *dtp, GElf_Addr addr,
1075*0Sstevel@tonic-gate     GElf_Sym *symp, dtrace_syminfo_t *sip)
1076*0Sstevel@tonic-gate {
1077*0Sstevel@tonic-gate 	dt_module_t *dmp;
1078*0Sstevel@tonic-gate 	uint_t id;
1079*0Sstevel@tonic-gate 	const dtrace_vector_t *v = dtp->dt_vector;
1080*0Sstevel@tonic-gate 
1081*0Sstevel@tonic-gate 	if (v != NULL)
1082*0Sstevel@tonic-gate 		return (v->dtv_lookup_by_addr(dtp->dt_varg, addr, symp, sip));
1083*0Sstevel@tonic-gate 
1084*0Sstevel@tonic-gate 	for (dmp = dt_list_next(&dtp->dt_modlist); dmp != NULL;
1085*0Sstevel@tonic-gate 	    dmp = dt_list_next(dmp)) {
1086*0Sstevel@tonic-gate 		if (addr - dmp->dm_text_va < dmp->dm_text_size ||
1087*0Sstevel@tonic-gate 		    addr - dmp->dm_data_va < dmp->dm_data_size ||
1088*0Sstevel@tonic-gate 		    addr - dmp->dm_bss_va < dmp->dm_bss_size)
1089*0Sstevel@tonic-gate 			break;
1090*0Sstevel@tonic-gate 	}
1091*0Sstevel@tonic-gate 
1092*0Sstevel@tonic-gate 	if (dmp == NULL)
1093*0Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_NOSYMADDR));
1094*0Sstevel@tonic-gate 
1095*0Sstevel@tonic-gate 	if (dt_module_load(dtp, dmp) == -1)
1096*0Sstevel@tonic-gate 		return (-1); /* dt_errno is set for us */
1097*0Sstevel@tonic-gate 
1098*0Sstevel@tonic-gate 	if (symp != NULL) {
1099*0Sstevel@tonic-gate 		if (dmp->dm_ops->do_symaddr(dmp, addr, symp, &id) == NULL)
1100*0Sstevel@tonic-gate 			return (dt_set_errno(dtp, EDT_NOSYMADDR));
1101*0Sstevel@tonic-gate 	}
1102*0Sstevel@tonic-gate 
1103*0Sstevel@tonic-gate 	if (sip != NULL) {
1104*0Sstevel@tonic-gate 		sip->dts_object = dmp->dm_name;
1105*0Sstevel@tonic-gate 
1106*0Sstevel@tonic-gate 		if (symp != NULL) {
1107*0Sstevel@tonic-gate 			sip->dts_name = (const char *)
1108*0Sstevel@tonic-gate 			    dmp->dm_strtab.cts_data + symp->st_name;
1109*0Sstevel@tonic-gate 			sip->dts_id = id;
1110*0Sstevel@tonic-gate 		} else {
1111*0Sstevel@tonic-gate 			sip->dts_name = NULL;
1112*0Sstevel@tonic-gate 			sip->dts_id = 0;
1113*0Sstevel@tonic-gate 		}
1114*0Sstevel@tonic-gate 	}
1115*0Sstevel@tonic-gate 
1116*0Sstevel@tonic-gate 	return (0);
1117*0Sstevel@tonic-gate }
1118*0Sstevel@tonic-gate 
1119*0Sstevel@tonic-gate int
1120*0Sstevel@tonic-gate dtrace_lookup_by_type(dtrace_hdl_t *dtp, const char *object, const char *name,
1121*0Sstevel@tonic-gate     dtrace_typeinfo_t *tip)
1122*0Sstevel@tonic-gate {
1123*0Sstevel@tonic-gate 	dtrace_typeinfo_t ti;
1124*0Sstevel@tonic-gate 	dt_module_t *dmp;
1125*0Sstevel@tonic-gate 	int found = 0;
1126*0Sstevel@tonic-gate 	ctf_id_t id;
1127*0Sstevel@tonic-gate 	uint_t n;
1128*0Sstevel@tonic-gate 
1129*0Sstevel@tonic-gate 	uint_t mask = 0; /* mask of dt_module flags to match */
1130*0Sstevel@tonic-gate 	uint_t bits = 0; /* flag bits that must be present */
1131*0Sstevel@tonic-gate 
1132*0Sstevel@tonic-gate 	if (object != DTRACE_OBJ_EVERY &&
1133*0Sstevel@tonic-gate 	    object != DTRACE_OBJ_KMODS &&
1134*0Sstevel@tonic-gate 	    object != DTRACE_OBJ_UMODS) {
1135*0Sstevel@tonic-gate 		if ((dmp = dt_module_from_object(dtp, object)) == NULL)
1136*0Sstevel@tonic-gate 			return (-1); /* dt_errno is set for us */
1137*0Sstevel@tonic-gate 
1138*0Sstevel@tonic-gate 		if (dt_module_load(dtp, dmp) == -1)
1139*0Sstevel@tonic-gate 			return (-1); /* dt_errno is set for us */
1140*0Sstevel@tonic-gate 		n = 1;
1141*0Sstevel@tonic-gate 
1142*0Sstevel@tonic-gate 	} else {
1143*0Sstevel@tonic-gate 		if (object == DTRACE_OBJ_KMODS)
1144*0Sstevel@tonic-gate 			mask = bits = DT_DM_KERNEL;
1145*0Sstevel@tonic-gate 		else if (object == DTRACE_OBJ_UMODS)
1146*0Sstevel@tonic-gate 			mask = DT_DM_KERNEL;
1147*0Sstevel@tonic-gate 
1148*0Sstevel@tonic-gate 		dmp = dt_list_next(&dtp->dt_modlist);
1149*0Sstevel@tonic-gate 		n = dtp->dt_nmods;
1150*0Sstevel@tonic-gate 	}
1151*0Sstevel@tonic-gate 
1152*0Sstevel@tonic-gate 	if (tip == NULL)
1153*0Sstevel@tonic-gate 		tip = &ti;
1154*0Sstevel@tonic-gate 
1155*0Sstevel@tonic-gate 	for (; n > 0; n--, dmp = dt_list_next(dmp)) {
1156*0Sstevel@tonic-gate 		if ((dmp->dm_flags & mask) != bits)
1157*0Sstevel@tonic-gate 			continue; /* failed to match required attributes */
1158*0Sstevel@tonic-gate 
1159*0Sstevel@tonic-gate 		/*
1160*0Sstevel@tonic-gate 		 * If we can't load the CTF container, continue on to the next
1161*0Sstevel@tonic-gate 		 * module.  If our search was scoped to only one module (n = 1)
1162*0Sstevel@tonic-gate 		 * then return immediately, leaving dt_errno set to the error
1163*0Sstevel@tonic-gate 		 * from dt_module_getctf() on the module given by the caller.
1164*0Sstevel@tonic-gate 		 */
1165*0Sstevel@tonic-gate 		if (dt_module_getctf(dtp, dmp) == NULL) {
1166*0Sstevel@tonic-gate 			if (n == 1)
1167*0Sstevel@tonic-gate 				return (-1);
1168*0Sstevel@tonic-gate 			continue;
1169*0Sstevel@tonic-gate 		}
1170*0Sstevel@tonic-gate 
1171*0Sstevel@tonic-gate 		/*
1172*0Sstevel@tonic-gate 		 * Look up the type in the module's CTF container.  If our
1173*0Sstevel@tonic-gate 		 * match is a forward declaration tag, save this choice in
1174*0Sstevel@tonic-gate 		 * 'tip' and keep going in the hope that we will locate the
1175*0Sstevel@tonic-gate 		 * underlying structure definition.  Otherwise just return.
1176*0Sstevel@tonic-gate 		 */
1177*0Sstevel@tonic-gate 		if ((id = ctf_lookup_by_name(dmp->dm_ctfp, name)) != CTF_ERR) {
1178*0Sstevel@tonic-gate 			tip->dtt_object = dmp->dm_name;
1179*0Sstevel@tonic-gate 			tip->dtt_ctfp = dmp->dm_ctfp;
1180*0Sstevel@tonic-gate 			tip->dtt_type = id;
1181*0Sstevel@tonic-gate 
1182*0Sstevel@tonic-gate 			if (ctf_type_kind(dmp->dm_ctfp, ctf_type_resolve(
1183*0Sstevel@tonic-gate 			    dmp->dm_ctfp, id)) != CTF_K_FORWARD)
1184*0Sstevel@tonic-gate 				return (0);
1185*0Sstevel@tonic-gate 
1186*0Sstevel@tonic-gate 			found++;
1187*0Sstevel@tonic-gate 		}
1188*0Sstevel@tonic-gate 	}
1189*0Sstevel@tonic-gate 
1190*0Sstevel@tonic-gate 	if (found == 0)
1191*0Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_NOTYPE));
1192*0Sstevel@tonic-gate 
1193*0Sstevel@tonic-gate 	return (0);
1194*0Sstevel@tonic-gate }
1195*0Sstevel@tonic-gate 
1196*0Sstevel@tonic-gate int
1197*0Sstevel@tonic-gate dtrace_symbol_type(dtrace_hdl_t *dtp, const GElf_Sym *symp,
1198*0Sstevel@tonic-gate     const dtrace_syminfo_t *sip, dtrace_typeinfo_t *tip)
1199*0Sstevel@tonic-gate {
1200*0Sstevel@tonic-gate 	dt_module_t *dmp;
1201*0Sstevel@tonic-gate 
1202*0Sstevel@tonic-gate 	tip->dtt_object = NULL;
1203*0Sstevel@tonic-gate 	tip->dtt_ctfp = NULL;
1204*0Sstevel@tonic-gate 	tip->dtt_type = CTF_ERR;
1205*0Sstevel@tonic-gate 
1206*0Sstevel@tonic-gate 	if ((dmp = dt_module_lookup_by_name(dtp, sip->dts_object)) == NULL)
1207*0Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_NOMOD));
1208*0Sstevel@tonic-gate 
1209*0Sstevel@tonic-gate 	if (symp->st_shndx == SHN_UNDEF && dmp->dm_extern != NULL) {
1210*0Sstevel@tonic-gate 		dt_ident_t *idp =
1211*0Sstevel@tonic-gate 		    dt_idhash_lookup(dmp->dm_extern, sip->dts_name);
1212*0Sstevel@tonic-gate 
1213*0Sstevel@tonic-gate 		if (idp == NULL)
1214*0Sstevel@tonic-gate 			return (dt_set_errno(dtp, EDT_NOSYM));
1215*0Sstevel@tonic-gate 
1216*0Sstevel@tonic-gate 		tip->dtt_ctfp = idp->di_ctfp;
1217*0Sstevel@tonic-gate 		tip->dtt_type = idp->di_type;
1218*0Sstevel@tonic-gate 
1219*0Sstevel@tonic-gate 	} else if (GELF_ST_TYPE(symp->st_info) != STT_FUNC) {
1220*0Sstevel@tonic-gate 		if (dt_module_getctf(dtp, dmp) == NULL)
1221*0Sstevel@tonic-gate 			return (-1); /* errno is set for us */
1222*0Sstevel@tonic-gate 
1223*0Sstevel@tonic-gate 		tip->dtt_ctfp = dmp->dm_ctfp;
1224*0Sstevel@tonic-gate 		tip->dtt_type = ctf_lookup_by_symbol(dmp->dm_ctfp, sip->dts_id);
1225*0Sstevel@tonic-gate 
1226*0Sstevel@tonic-gate 		if (tip->dtt_type == CTF_ERR) {
1227*0Sstevel@tonic-gate 			dtp->dt_ctferr = ctf_errno(tip->dtt_ctfp);
1228*0Sstevel@tonic-gate 			return (dt_set_errno(dtp, EDT_CTF));
1229*0Sstevel@tonic-gate 		}
1230*0Sstevel@tonic-gate 
1231*0Sstevel@tonic-gate 	} else {
1232*0Sstevel@tonic-gate 		tip->dtt_ctfp = DT_FPTR_CTFP(dtp);
1233*0Sstevel@tonic-gate 		tip->dtt_type = DT_FPTR_TYPE(dtp);
1234*0Sstevel@tonic-gate 	}
1235*0Sstevel@tonic-gate 
1236*0Sstevel@tonic-gate 	tip->dtt_object = dmp->dm_name;
1237*0Sstevel@tonic-gate 	return (0);
1238*0Sstevel@tonic-gate }
1239*0Sstevel@tonic-gate 
1240*0Sstevel@tonic-gate static dtrace_objinfo_t *
1241*0Sstevel@tonic-gate dt_module_info(const dt_module_t *dmp, dtrace_objinfo_t *dto)
1242*0Sstevel@tonic-gate {
1243*0Sstevel@tonic-gate 	dto->dto_name = dmp->dm_name;
1244*0Sstevel@tonic-gate 	dto->dto_file = dmp->dm_file;
1245*0Sstevel@tonic-gate 	dto->dto_id = dmp->dm_modid;
1246*0Sstevel@tonic-gate 	dto->dto_flags = 0;
1247*0Sstevel@tonic-gate 
1248*0Sstevel@tonic-gate 	if (dmp->dm_flags & DT_DM_KERNEL)
1249*0Sstevel@tonic-gate 		dto->dto_flags |= DTRACE_OBJ_F_KERNEL;
1250*0Sstevel@tonic-gate 	if (dmp->dm_flags & DT_DM_PRIMARY)
1251*0Sstevel@tonic-gate 		dto->dto_flags |= DTRACE_OBJ_F_PRIMARY;
1252*0Sstevel@tonic-gate 
1253*0Sstevel@tonic-gate 	dto->dto_text_va = dmp->dm_text_va;
1254*0Sstevel@tonic-gate 	dto->dto_text_size = dmp->dm_text_size;
1255*0Sstevel@tonic-gate 	dto->dto_data_va = dmp->dm_data_va;
1256*0Sstevel@tonic-gate 	dto->dto_data_size = dmp->dm_data_size;
1257*0Sstevel@tonic-gate 	dto->dto_bss_va = dmp->dm_bss_va;
1258*0Sstevel@tonic-gate 	dto->dto_bss_size = dmp->dm_bss_size;
1259*0Sstevel@tonic-gate 
1260*0Sstevel@tonic-gate 	return (dto);
1261*0Sstevel@tonic-gate }
1262*0Sstevel@tonic-gate 
1263*0Sstevel@tonic-gate int
1264*0Sstevel@tonic-gate dtrace_object_iter(dtrace_hdl_t *dtp, dtrace_obj_f *func, void *data)
1265*0Sstevel@tonic-gate {
1266*0Sstevel@tonic-gate 	const dt_module_t *dmp = dt_list_next(&dtp->dt_modlist);
1267*0Sstevel@tonic-gate 	dtrace_objinfo_t dto;
1268*0Sstevel@tonic-gate 	int rv;
1269*0Sstevel@tonic-gate 
1270*0Sstevel@tonic-gate 	for (; dmp != NULL; dmp = dt_list_next(dmp)) {
1271*0Sstevel@tonic-gate 		if ((rv = (*func)(dtp, dt_module_info(dmp, &dto), data)) != 0)
1272*0Sstevel@tonic-gate 			return (rv);
1273*0Sstevel@tonic-gate 	}
1274*0Sstevel@tonic-gate 
1275*0Sstevel@tonic-gate 	return (0);
1276*0Sstevel@tonic-gate }
1277*0Sstevel@tonic-gate 
1278*0Sstevel@tonic-gate int
1279*0Sstevel@tonic-gate dtrace_object_info(dtrace_hdl_t *dtp, const char *object, dtrace_objinfo_t *dto)
1280*0Sstevel@tonic-gate {
1281*0Sstevel@tonic-gate 	dt_module_t *dmp;
1282*0Sstevel@tonic-gate 
1283*0Sstevel@tonic-gate 	if (object == DTRACE_OBJ_EVERY || object == DTRACE_OBJ_KMODS ||
1284*0Sstevel@tonic-gate 	    object == DTRACE_OBJ_UMODS || dto == NULL)
1285*0Sstevel@tonic-gate 		return (dt_set_errno(dtp, EINVAL));
1286*0Sstevel@tonic-gate 
1287*0Sstevel@tonic-gate 	if ((dmp = dt_module_from_object(dtp, object)) == NULL)
1288*0Sstevel@tonic-gate 		return (-1); /* dt_errno is set for us */
1289*0Sstevel@tonic-gate 
1290*0Sstevel@tonic-gate 	if (dt_module_load(dtp, dmp) == -1)
1291*0Sstevel@tonic-gate 		return (-1); /* dt_errno is set for us */
1292*0Sstevel@tonic-gate 
1293*0Sstevel@tonic-gate 	(void) dt_module_info(dmp, dto);
1294*0Sstevel@tonic-gate 	return (0);
1295*0Sstevel@tonic-gate }
1296