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