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