xref: /freebsd/cddl/contrib/opensolaris/lib/libdtrace/common/dt_module.c (revision e8e8c939350bdf3c228a411caa9660c607c27a11)
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 /*
23  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24  */
25 /*
26  * Copyright (c) 2013, Joyent, Inc.  All rights reserved.
27  */
28 
29 #include <sys/types.h>
30 #ifdef illumos
31 #include <sys/modctl.h>
32 #include <sys/kobj.h>
33 #include <sys/kobj_impl.h>
34 #include <sys/sysmacros.h>
35 #include <sys/elf.h>
36 #include <sys/task.h>
37 #else
38 #include <sys/param.h>
39 #include <sys/linker.h>
40 #include <sys/module.h>
41 #include <sys/stat.h>
42 #endif
43 
44 #include <unistd.h>
45 #ifdef illumos
46 #include <project.h>
47 #endif
48 #include <strings.h>
49 #include <stdlib.h>
50 #include <libelf.h>
51 #include <limits.h>
52 #include <assert.h>
53 #include <errno.h>
54 #include <dirent.h>
55 #ifndef illumos
56 #include <fcntl.h>
57 #include <libproc_compat.h>
58 #endif
59 
60 #include <dt_strtab.h>
61 #include <dt_module.h>
62 #include <dt_impl.h>
63 
64 static const char *dt_module_strtab; /* active strtab for qsort callbacks */
65 
66 static void
67 dt_module_symhash_insert(dt_module_t *dmp, const char *name, uint_t id)
68 {
69 	dt_sym_t *dsp = &dmp->dm_symchains[dmp->dm_symfree];
70 	uint_t h;
71 
72 	assert(dmp->dm_symfree < dmp->dm_nsymelems + 1);
73 
74 	dsp->ds_symid = id;
75 	h = dt_strtab_hash(name, NULL) % dmp->dm_nsymbuckets;
76 	dsp->ds_next = dmp->dm_symbuckets[h];
77 	dmp->dm_symbuckets[h] = dmp->dm_symfree++;
78 }
79 
80 static uint_t
81 dt_module_syminit32(dt_module_t *dmp)
82 {
83 #if STT_NUM != (STT_TLS + 1)
84 #error "STT_NUM has grown. update dt_module_syminit32()"
85 #endif
86 
87 	Elf32_Sym *sym = dmp->dm_symtab.cts_data;
88 	const char *base = dmp->dm_strtab.cts_data;
89 	size_t ss_size = dmp->dm_strtab.cts_size;
90 	uint_t i, n = dmp->dm_nsymelems;
91 	uint_t asrsv = 0;
92 
93 #if defined(__FreeBSD__)
94 	GElf_Ehdr ehdr;
95 	int is_elf_obj;
96 
97 	gelf_getehdr(dmp->dm_elf, &ehdr);
98 	is_elf_obj = (ehdr.e_type == ET_REL);
99 #endif
100 
101 	for (i = 0; i < n; i++, sym++) {
102 		const char *name = base + sym->st_name;
103 		uchar_t type = ELF32_ST_TYPE(sym->st_info);
104 
105 		if (type >= STT_NUM || type == STT_SECTION)
106 			continue; /* skip sections and unknown types */
107 
108 		if (sym->st_name == 0 || sym->st_name >= ss_size)
109 			continue; /* skip null or invalid names */
110 
111 		if (sym->st_value != 0 &&
112 		    (ELF32_ST_BIND(sym->st_info) != STB_LOCAL || sym->st_size)) {
113 			asrsv++; /* reserve space in the address map */
114 
115 #if defined(__FreeBSD__)
116 			sym->st_value += (Elf_Addr) dmp->dm_reloc_offset;
117 			if (is_elf_obj && sym->st_shndx != SHN_UNDEF &&
118 			    sym->st_shndx < ehdr.e_shnum)
119 				sym->st_value +=
120 				    dmp->dm_sec_offsets[sym->st_shndx];
121 #endif
122 		}
123 
124 		dt_module_symhash_insert(dmp, name, i);
125 	}
126 
127 	return (asrsv);
128 }
129 
130 static uint_t
131 dt_module_syminit64(dt_module_t *dmp)
132 {
133 #if STT_NUM != (STT_TLS + 1)
134 #error "STT_NUM has grown. update dt_module_syminit64()"
135 #endif
136 
137 	Elf64_Sym *sym = dmp->dm_symtab.cts_data;
138 	const char *base = dmp->dm_strtab.cts_data;
139 	size_t ss_size = dmp->dm_strtab.cts_size;
140 	uint_t i, n = dmp->dm_nsymelems;
141 	uint_t asrsv = 0;
142 
143 #if defined(__FreeBSD__)
144 	GElf_Ehdr ehdr;
145 	int is_elf_obj;
146 
147 	gelf_getehdr(dmp->dm_elf, &ehdr);
148 	is_elf_obj = (ehdr.e_type == ET_REL);
149 #endif
150 
151 	for (i = 0; i < n; i++, sym++) {
152 		const char *name = base + sym->st_name;
153 		uchar_t type = ELF64_ST_TYPE(sym->st_info);
154 
155 		if (type >= STT_NUM || type == STT_SECTION)
156 			continue; /* skip sections and unknown types */
157 
158 		if (sym->st_name == 0 || sym->st_name >= ss_size)
159 			continue; /* skip null or invalid names */
160 
161 		if (sym->st_value != 0 &&
162 		    (ELF64_ST_BIND(sym->st_info) != STB_LOCAL || sym->st_size)) {
163 			asrsv++; /* reserve space in the address map */
164 #if defined(__FreeBSD__)
165 			sym->st_value += (Elf_Addr) dmp->dm_reloc_offset;
166 			if (is_elf_obj && sym->st_shndx != SHN_UNDEF &&
167 			    sym->st_shndx < ehdr.e_shnum)
168 				sym->st_value +=
169 				    dmp->dm_sec_offsets[sym->st_shndx];
170 #endif
171 		}
172 
173 		dt_module_symhash_insert(dmp, name, i);
174 	}
175 
176 	return (asrsv);
177 }
178 
179 /*
180  * Sort comparison function for 32-bit symbol address-to-name lookups.  We sort
181  * symbols by value.  If values are equal, we prefer the symbol that is
182  * non-zero sized, typed, not weak, or lexically first, in that order.
183  */
184 static int
185 dt_module_symcomp32(const void *lp, const void *rp)
186 {
187 	Elf32_Sym *lhs = *((Elf32_Sym **)lp);
188 	Elf32_Sym *rhs = *((Elf32_Sym **)rp);
189 
190 	if (lhs->st_value != rhs->st_value)
191 		return (lhs->st_value > rhs->st_value ? 1 : -1);
192 
193 	if ((lhs->st_size == 0) != (rhs->st_size == 0))
194 		return (lhs->st_size == 0 ? 1 : -1);
195 
196 	if ((ELF32_ST_TYPE(lhs->st_info) == STT_NOTYPE) !=
197 	    (ELF32_ST_TYPE(rhs->st_info) == STT_NOTYPE))
198 		return (ELF32_ST_TYPE(lhs->st_info) == STT_NOTYPE ? 1 : -1);
199 
200 	if ((ELF32_ST_BIND(lhs->st_info) == STB_WEAK) !=
201 	    (ELF32_ST_BIND(rhs->st_info) == STB_WEAK))
202 		return (ELF32_ST_BIND(lhs->st_info) == STB_WEAK ? 1 : -1);
203 
204 	return (strcmp(dt_module_strtab + lhs->st_name,
205 	    dt_module_strtab + rhs->st_name));
206 }
207 
208 /*
209  * Sort comparison function for 64-bit symbol address-to-name lookups.  We sort
210  * symbols by value.  If values are equal, we prefer the symbol that is
211  * non-zero sized, typed, not weak, or lexically first, in that order.
212  */
213 static int
214 dt_module_symcomp64(const void *lp, const void *rp)
215 {
216 	Elf64_Sym *lhs = *((Elf64_Sym **)lp);
217 	Elf64_Sym *rhs = *((Elf64_Sym **)rp);
218 
219 	if (lhs->st_value != rhs->st_value)
220 		return (lhs->st_value > rhs->st_value ? 1 : -1);
221 
222 	if ((lhs->st_size == 0) != (rhs->st_size == 0))
223 		return (lhs->st_size == 0 ? 1 : -1);
224 
225 	if ((ELF64_ST_TYPE(lhs->st_info) == STT_NOTYPE) !=
226 	    (ELF64_ST_TYPE(rhs->st_info) == STT_NOTYPE))
227 		return (ELF64_ST_TYPE(lhs->st_info) == STT_NOTYPE ? 1 : -1);
228 
229 	if ((ELF64_ST_BIND(lhs->st_info) == STB_WEAK) !=
230 	    (ELF64_ST_BIND(rhs->st_info) == STB_WEAK))
231 		return (ELF64_ST_BIND(lhs->st_info) == STB_WEAK ? 1 : -1);
232 
233 	return (strcmp(dt_module_strtab + lhs->st_name,
234 	    dt_module_strtab + rhs->st_name));
235 }
236 
237 static void
238 dt_module_symsort32(dt_module_t *dmp)
239 {
240 	Elf32_Sym *symtab = (Elf32_Sym *)dmp->dm_symtab.cts_data;
241 	Elf32_Sym **sympp = (Elf32_Sym **)dmp->dm_asmap;
242 	const dt_sym_t *dsp = dmp->dm_symchains + 1;
243 	uint_t i, n = dmp->dm_symfree;
244 
245 	for (i = 1; i < n; i++, dsp++) {
246 		Elf32_Sym *sym = symtab + dsp->ds_symid;
247 		if (sym->st_value != 0 &&
248 		    (ELF32_ST_BIND(sym->st_info) != STB_LOCAL || sym->st_size))
249 			*sympp++ = sym;
250 	}
251 
252 	dmp->dm_aslen = (uint_t)(sympp - (Elf32_Sym **)dmp->dm_asmap);
253 	assert(dmp->dm_aslen <= dmp->dm_asrsv);
254 
255 	dt_module_strtab = dmp->dm_strtab.cts_data;
256 	qsort(dmp->dm_asmap, dmp->dm_aslen,
257 	    sizeof (Elf32_Sym *), dt_module_symcomp32);
258 	dt_module_strtab = NULL;
259 }
260 
261 static void
262 dt_module_symsort64(dt_module_t *dmp)
263 {
264 	Elf64_Sym *symtab = (Elf64_Sym *)dmp->dm_symtab.cts_data;
265 	Elf64_Sym **sympp = (Elf64_Sym **)dmp->dm_asmap;
266 	const dt_sym_t *dsp = dmp->dm_symchains + 1;
267 	uint_t i, n = dmp->dm_symfree;
268 
269 	for (i = 1; i < n; i++, dsp++) {
270 		Elf64_Sym *sym = symtab + dsp->ds_symid;
271 		if (sym->st_value != 0 &&
272 		    (ELF64_ST_BIND(sym->st_info) != STB_LOCAL || sym->st_size))
273 			*sympp++ = sym;
274 	}
275 
276 	dmp->dm_aslen = (uint_t)(sympp - (Elf64_Sym **)dmp->dm_asmap);
277 	assert(dmp->dm_aslen <= dmp->dm_asrsv);
278 
279 	dt_module_strtab = dmp->dm_strtab.cts_data;
280 	qsort(dmp->dm_asmap, dmp->dm_aslen,
281 	    sizeof (Elf64_Sym *), dt_module_symcomp64);
282 	dt_module_strtab = NULL;
283 }
284 
285 static GElf_Sym *
286 dt_module_symgelf32(const Elf32_Sym *src, GElf_Sym *dst)
287 {
288 	if (dst != NULL) {
289 		dst->st_name = src->st_name;
290 		dst->st_info = src->st_info;
291 		dst->st_other = src->st_other;
292 		dst->st_shndx = src->st_shndx;
293 		dst->st_value = src->st_value;
294 		dst->st_size = src->st_size;
295 	}
296 
297 	return (dst);
298 }
299 
300 static GElf_Sym *
301 dt_module_symgelf64(const Elf64_Sym *src, GElf_Sym *dst)
302 {
303 	if (dst != NULL)
304 		bcopy(src, dst, sizeof (GElf_Sym));
305 
306 	return (dst);
307 }
308 
309 static GElf_Sym *
310 dt_module_symname32(dt_module_t *dmp, const char *name,
311     GElf_Sym *symp, uint_t *idp)
312 {
313 	const Elf32_Sym *symtab = dmp->dm_symtab.cts_data;
314 	const char *strtab = dmp->dm_strtab.cts_data;
315 
316 	const Elf32_Sym *sym;
317 	const dt_sym_t *dsp;
318 	uint_t i, h;
319 
320 	if (dmp->dm_nsymelems == 0)
321 		return (NULL);
322 
323 	h = dt_strtab_hash(name, NULL) % dmp->dm_nsymbuckets;
324 
325 	for (i = dmp->dm_symbuckets[h]; i != 0; i = dsp->ds_next) {
326 		dsp = &dmp->dm_symchains[i];
327 		sym = symtab + dsp->ds_symid;
328 
329 		if (strcmp(name, strtab + sym->st_name) == 0) {
330 			if (idp != NULL)
331 				*idp = dsp->ds_symid;
332 			return (dt_module_symgelf32(sym, symp));
333 		}
334 	}
335 
336 	return (NULL);
337 }
338 
339 static GElf_Sym *
340 dt_module_symname64(dt_module_t *dmp, const char *name,
341     GElf_Sym *symp, uint_t *idp)
342 {
343 	const Elf64_Sym *symtab = dmp->dm_symtab.cts_data;
344 	const char *strtab = dmp->dm_strtab.cts_data;
345 
346 	const Elf64_Sym *sym;
347 	const dt_sym_t *dsp;
348 	uint_t i, h;
349 
350 	if (dmp->dm_nsymelems == 0)
351 		return (NULL);
352 
353 	h = dt_strtab_hash(name, NULL) % dmp->dm_nsymbuckets;
354 
355 	for (i = dmp->dm_symbuckets[h]; i != 0; i = dsp->ds_next) {
356 		dsp = &dmp->dm_symchains[i];
357 		sym = symtab + dsp->ds_symid;
358 
359 		if (strcmp(name, strtab + sym->st_name) == 0) {
360 			if (idp != NULL)
361 				*idp = dsp->ds_symid;
362 			return (dt_module_symgelf64(sym, symp));
363 		}
364 	}
365 
366 	return (NULL);
367 }
368 
369 static GElf_Sym *
370 dt_module_symaddr32(dt_module_t *dmp, GElf_Addr addr,
371     GElf_Sym *symp, uint_t *idp)
372 {
373 	const Elf32_Sym **asmap = (const Elf32_Sym **)dmp->dm_asmap;
374 	const Elf32_Sym *symtab = dmp->dm_symtab.cts_data;
375 	const Elf32_Sym *sym;
376 
377 	uint_t i, mid, lo = 0, hi = dmp->dm_aslen - 1;
378 	Elf32_Addr v;
379 
380 	if (dmp->dm_aslen == 0)
381 		return (NULL);
382 
383 	while (hi - lo > 1) {
384 		mid = (lo + hi) / 2;
385 		if (addr >= asmap[mid]->st_value)
386 			lo = mid;
387 		else
388 			hi = mid;
389 	}
390 
391 	i = addr < asmap[hi]->st_value ? lo : hi;
392 	sym = asmap[i];
393 	v = sym->st_value;
394 
395 	/*
396 	 * If the previous entry has the same value, improve our choice.  The
397 	 * order of equal-valued symbols is determined by the comparison func.
398 	 */
399 	while (i-- != 0 && asmap[i]->st_value == v)
400 		sym = asmap[i];
401 
402 	if (addr - sym->st_value < MAX(sym->st_size, 1)) {
403 		if (idp != NULL)
404 			*idp = (uint_t)(sym - symtab);
405 		return (dt_module_symgelf32(sym, symp));
406 	}
407 
408 	return (NULL);
409 }
410 
411 static GElf_Sym *
412 dt_module_symaddr64(dt_module_t *dmp, GElf_Addr addr,
413     GElf_Sym *symp, uint_t *idp)
414 {
415 	const Elf64_Sym **asmap = (const Elf64_Sym **)dmp->dm_asmap;
416 	const Elf64_Sym *symtab = dmp->dm_symtab.cts_data;
417 	const Elf64_Sym *sym;
418 
419 	uint_t i, mid, lo = 0, hi = dmp->dm_aslen - 1;
420 	Elf64_Addr v;
421 
422 	if (dmp->dm_aslen == 0)
423 		return (NULL);
424 
425 	while (hi - lo > 1) {
426 		mid = (lo + hi) / 2;
427 		if (addr >= asmap[mid]->st_value)
428 			lo = mid;
429 		else
430 			hi = mid;
431 	}
432 
433 	i = addr < asmap[hi]->st_value ? lo : hi;
434 	sym = asmap[i];
435 	v = sym->st_value;
436 
437 	/*
438 	 * If the previous entry has the same value, improve our choice.  The
439 	 * order of equal-valued symbols is determined by the comparison func.
440 	 */
441 	while (i-- != 0 && asmap[i]->st_value == v)
442 		sym = asmap[i];
443 
444 	if (addr - sym->st_value < MAX(sym->st_size, 1)) {
445 		if (idp != NULL)
446 			*idp = (uint_t)(sym - symtab);
447 		return (dt_module_symgelf64(sym, symp));
448 	}
449 
450 	return (NULL);
451 }
452 
453 static const dt_modops_t dt_modops_32 = {
454 	dt_module_syminit32,
455 	dt_module_symsort32,
456 	dt_module_symname32,
457 	dt_module_symaddr32
458 };
459 
460 static const dt_modops_t dt_modops_64 = {
461 	dt_module_syminit64,
462 	dt_module_symsort64,
463 	dt_module_symname64,
464 	dt_module_symaddr64
465 };
466 
467 dt_module_t *
468 dt_module_create(dtrace_hdl_t *dtp, const char *name)
469 {
470 	long pid;
471 	char *eptr;
472 	dt_ident_t *idp;
473 	uint_t h = dt_strtab_hash(name, NULL) % dtp->dt_modbuckets;
474 	dt_module_t *dmp;
475 
476 	for (dmp = dtp->dt_mods[h]; dmp != NULL; dmp = dmp->dm_next) {
477 		if (strcmp(dmp->dm_name, name) == 0)
478 			return (dmp);
479 	}
480 
481 	if ((dmp = malloc(sizeof (dt_module_t))) == NULL)
482 		return (NULL); /* caller must handle allocation failure */
483 
484 	bzero(dmp, sizeof (dt_module_t));
485 	(void) strlcpy(dmp->dm_name, name, sizeof (dmp->dm_name));
486 	dt_list_append(&dtp->dt_modlist, dmp);
487 	dmp->dm_next = dtp->dt_mods[h];
488 	dtp->dt_mods[h] = dmp;
489 	dtp->dt_nmods++;
490 
491 	if (dtp->dt_conf.dtc_ctfmodel == CTF_MODEL_LP64)
492 		dmp->dm_ops = &dt_modops_64;
493 	else
494 		dmp->dm_ops = &dt_modops_32;
495 
496 	/*
497 	 * Modules for userland processes are special. They always refer to a
498 	 * specific process and have a copy of their CTF data from a specific
499 	 * instant in time. Any dt_module_t that begins with 'pid' is a module
500 	 * for a specific process, much like how any probe description that
501 	 * begins with 'pid' is special. pid123 refers to process 123. A module
502 	 * that is just 'pid' refers specifically to pid$target. This is
503 	 * generally done as D does not currently allow for macros to be
504 	 * evaluated when working with types.
505 	 */
506 	if (strncmp(dmp->dm_name, "pid", 3) == 0) {
507 		errno = 0;
508 		if (dmp->dm_name[3] == '\0') {
509 			idp = dt_idhash_lookup(dtp->dt_macros, "target");
510 			if (idp != NULL && idp->di_id != 0)
511 				dmp->dm_pid = idp->di_id;
512 		} else {
513 			pid = strtol(dmp->dm_name + 3, &eptr, 10);
514 			if (errno == 0 && *eptr == '\0')
515 				dmp->dm_pid = (pid_t)pid;
516 			else
517 				dt_dprintf("encountered malformed pid "
518 				    "module: %s\n", dmp->dm_name);
519 		}
520 	}
521 
522 	return (dmp);
523 }
524 
525 dt_module_t *
526 dt_module_lookup_by_name(dtrace_hdl_t *dtp, const char *name)
527 {
528 	uint_t h = dt_strtab_hash(name, NULL) % dtp->dt_modbuckets;
529 	dt_module_t *dmp;
530 
531 	for (dmp = dtp->dt_mods[h]; dmp != NULL; dmp = dmp->dm_next) {
532 		if (strcmp(dmp->dm_name, name) == 0)
533 			return (dmp);
534 	}
535 
536 	return (NULL);
537 }
538 
539 /*ARGSUSED*/
540 dt_module_t *
541 dt_module_lookup_by_ctf(dtrace_hdl_t *dtp, ctf_file_t *ctfp)
542 {
543 	return (ctfp ? ctf_getspecific(ctfp) : NULL);
544 }
545 
546 #ifdef __FreeBSD__
547 dt_kmodule_t *
548 dt_kmodule_lookup(dtrace_hdl_t *dtp, const char *name)
549 {
550 	uint_t h = dt_strtab_hash(name, NULL) % dtp->dt_modbuckets;
551 	dt_kmodule_t *dkmp;
552 
553 	for (dkmp = dtp->dt_kmods[h]; dkmp != NULL; dkmp = dkmp->dkm_next) {
554 		if (strcmp(dkmp->dkm_name, name) == 0)
555 			return (dkmp);
556 	}
557 
558 	return (NULL);
559 }
560 #endif
561 
562 static int
563 dt_module_load_sect(dtrace_hdl_t *dtp, dt_module_t *dmp, ctf_sect_t *ctsp)
564 {
565 	const char *s;
566 	size_t shstrs;
567 	GElf_Shdr sh;
568 	Elf_Data *dp;
569 	Elf_Scn *sp;
570 
571 	if (elf_getshdrstrndx(dmp->dm_elf, &shstrs) == -1)
572 		return (dt_set_errno(dtp, EDT_NOTLOADED));
573 
574 	for (sp = NULL; (sp = elf_nextscn(dmp->dm_elf, sp)) != NULL; ) {
575 		if (gelf_getshdr(sp, &sh) == NULL || sh.sh_type == SHT_NULL ||
576 		    (s = elf_strptr(dmp->dm_elf, shstrs, sh.sh_name)) == NULL)
577 			continue; /* skip any malformed sections */
578 
579 		if (sh.sh_type == ctsp->cts_type &&
580 		    sh.sh_entsize == ctsp->cts_entsize &&
581 		    strcmp(s, ctsp->cts_name) == 0)
582 			break; /* section matches specification */
583 	}
584 
585 	/*
586 	 * If the section isn't found, return success but leave cts_data set
587 	 * to NULL and cts_size set to zero for our caller.
588 	 */
589 	if (sp == NULL || (dp = elf_getdata(sp, NULL)) == NULL)
590 		return (0);
591 
592 #ifdef illumos
593 	ctsp->cts_data = dp->d_buf;
594 #else
595 	if ((ctsp->cts_data = malloc(dp->d_size)) == NULL)
596 		return (0);
597 	memcpy(ctsp->cts_data, dp->d_buf, dp->d_size);
598 #endif
599 	ctsp->cts_size = dp->d_size;
600 
601 	dt_dprintf("loaded %s [%s] (%lu bytes)\n",
602 	    dmp->dm_name, ctsp->cts_name, (ulong_t)ctsp->cts_size);
603 
604 	return (0);
605 }
606 
607 typedef struct dt_module_cb_arg {
608 	struct ps_prochandle *dpa_proc;
609 	dtrace_hdl_t *dpa_dtp;
610 	dt_module_t *dpa_dmp;
611 	uint_t dpa_count;
612 } dt_module_cb_arg_t;
613 
614 /* ARGSUSED */
615 static int
616 dt_module_load_proc_count(void *arg, const prmap_t *prmap, const char *obj)
617 {
618 	ctf_file_t *fp;
619 	dt_module_cb_arg_t *dcp = arg;
620 
621 	/* Try to grab a ctf container if it exists */
622 	fp = Pname_to_ctf(dcp->dpa_proc, obj);
623 	if (fp != NULL)
624 		dcp->dpa_count++;
625 	return (0);
626 }
627 
628 /* ARGSUSED */
629 static int
630 dt_module_load_proc_build(void *arg, const prmap_t *prmap, const char *obj)
631 {
632 	ctf_file_t *fp;
633 	char buf[MAXPATHLEN], *p;
634 	dt_module_cb_arg_t *dcp = arg;
635 	int count = dcp->dpa_count;
636 	Lmid_t lmid;
637 
638 	fp = Pname_to_ctf(dcp->dpa_proc, obj);
639 	if (fp == NULL)
640 		return (0);
641 	fp = ctf_dup(fp);
642 	if (fp == NULL)
643 		return (0);
644 	dcp->dpa_dmp->dm_libctfp[count] = fp;
645 	/*
646 	 * While it'd be nice to simply use objname here, because of our prior
647 	 * actions we'll always get a resolved object name to its on disk file.
648 	 * Like the pid provider, we need to tell a bit of a lie here. The type
649 	 * that the user thinks of is in terms of the libraries they requested,
650 	 * eg. libc.so.1, they don't care about the fact that it's
651 	 * libc_hwcap.so.1.
652 	 */
653 	(void) Pobjname(dcp->dpa_proc, prmap->pr_vaddr, buf, sizeof (buf));
654 	if ((p = strrchr(buf, '/')) == NULL)
655 		p = buf;
656 	else
657 		p++;
658 
659 	/*
660 	 * If for some reason we can't find a link map id for this module, which
661 	 * would be really quite weird. We instead just say the link map id is
662 	 * zero.
663 	 */
664 	if (Plmid(dcp->dpa_proc, prmap->pr_vaddr, &lmid) != 0)
665 		lmid = 0;
666 
667 	if (lmid == 0)
668 		dcp->dpa_dmp->dm_libctfn[count] = strdup(p);
669 	else
670 		(void) asprintf(&dcp->dpa_dmp->dm_libctfn[count],
671 		    "LM%x`%s", lmid, p);
672 	if (dcp->dpa_dmp->dm_libctfn[count] == NULL)
673 		return (1);
674 	ctf_setspecific(fp, dcp->dpa_dmp);
675 	dcp->dpa_count++;
676 	return (0);
677 }
678 
679 /*
680  * We've been asked to load data that belongs to another process. As such we're
681  * going to pgrab it at this instant, load everything that we might ever care
682  * about, and then drive on. The reason for this is that the process that we're
683  * interested in might be changing. As long as we have grabbed it, then this
684  * can't be a problem for us.
685  *
686  * For now, we're actually going to punt on most things and just try to get CTF
687  * data, nothing else. Basically this is only useful as a source of type
688  * information, we can't go and do the stacktrace lookups, etc.
689  */
690 static int
691 dt_module_load_proc(dtrace_hdl_t *dtp, dt_module_t *dmp)
692 {
693 	struct ps_prochandle *p;
694 	dt_module_cb_arg_t arg;
695 
696 	/*
697 	 * Note that on success we do not release this hold. We must hold this
698 	 * for our life time.
699 	 */
700 	p = dt_proc_grab(dtp, dmp->dm_pid, 0, PGRAB_RDONLY | PGRAB_FORCE);
701 	if (p == NULL) {
702 		dt_dprintf("failed to grab pid: %d\n", (int)dmp->dm_pid);
703 		return (dt_set_errno(dtp, EDT_CANTLOAD));
704 	}
705 	dt_proc_lock(dtp, p);
706 
707 	arg.dpa_proc = p;
708 	arg.dpa_dtp = dtp;
709 	arg.dpa_dmp = dmp;
710 	arg.dpa_count = 0;
711 	if (Pobject_iter_resolved(p, dt_module_load_proc_count, &arg) != 0) {
712 		dt_dprintf("failed to iterate objects\n");
713 		dt_proc_release(dtp, p);
714 		return (dt_set_errno(dtp, EDT_CANTLOAD));
715 	}
716 
717 	if (arg.dpa_count == 0) {
718 		dt_dprintf("no ctf data present\n");
719 		dt_proc_unlock(dtp, p);
720 		dt_proc_release(dtp, p);
721 		return (dt_set_errno(dtp, EDT_CANTLOAD));
722 	}
723 
724 	dmp->dm_libctfp = malloc(sizeof (ctf_file_t *) * arg.dpa_count);
725 	if (dmp->dm_libctfp == NULL) {
726 		dt_proc_unlock(dtp, p);
727 		dt_proc_release(dtp, p);
728 		return (dt_set_errno(dtp, EDT_NOMEM));
729 	}
730 	bzero(dmp->dm_libctfp, sizeof (ctf_file_t *) * arg.dpa_count);
731 
732 	dmp->dm_libctfn = malloc(sizeof (char *) * arg.dpa_count);
733 	if (dmp->dm_libctfn == NULL) {
734 		free(dmp->dm_libctfp);
735 		dt_proc_unlock(dtp, p);
736 		dt_proc_release(dtp, p);
737 		return (dt_set_errno(dtp, EDT_NOMEM));
738 	}
739 	bzero(dmp->dm_libctfn, sizeof (char *) * arg.dpa_count);
740 
741 	dmp->dm_nctflibs = arg.dpa_count;
742 
743 	arg.dpa_count = 0;
744 	if (Pobject_iter_resolved(p, dt_module_load_proc_build, &arg) != 0) {
745 		dt_proc_unlock(dtp, p);
746 		dt_module_unload(dtp, dmp);
747 		dt_proc_release(dtp, p);
748 		return (dt_set_errno(dtp, EDT_CANTLOAD));
749 	}
750 	assert(arg.dpa_count == dmp->dm_nctflibs);
751 	dt_dprintf("loaded %d ctf modules for pid %d\n", arg.dpa_count,
752 	    (int)dmp->dm_pid);
753 
754 	dt_proc_unlock(dtp, p);
755 	dt_proc_release(dtp, p);
756 	dmp->dm_flags |= DT_DM_LOADED;
757 
758 	return (0);
759 }
760 
761 int
762 dt_module_load(dtrace_hdl_t *dtp, dt_module_t *dmp)
763 {
764 	if (dmp->dm_flags & DT_DM_LOADED)
765 		return (0); /* module is already loaded */
766 
767 	if (dmp->dm_pid != 0)
768 		return (dt_module_load_proc(dtp, dmp));
769 
770 	dmp->dm_ctdata.cts_name = ".SUNW_ctf";
771 	dmp->dm_ctdata.cts_type = SHT_PROGBITS;
772 	dmp->dm_ctdata.cts_flags = 0;
773 	dmp->dm_ctdata.cts_data = NULL;
774 	dmp->dm_ctdata.cts_size = 0;
775 	dmp->dm_ctdata.cts_entsize = 0;
776 	dmp->dm_ctdata.cts_offset = 0;
777 
778 	dmp->dm_symtab.cts_name = ".symtab";
779 	dmp->dm_symtab.cts_type = SHT_SYMTAB;
780 	dmp->dm_symtab.cts_flags = 0;
781 	dmp->dm_symtab.cts_data = NULL;
782 	dmp->dm_symtab.cts_size = 0;
783 	dmp->dm_symtab.cts_entsize = dmp->dm_ops == &dt_modops_64 ?
784 	    sizeof (Elf64_Sym) : sizeof (Elf32_Sym);
785 	dmp->dm_symtab.cts_offset = 0;
786 
787 	dmp->dm_strtab.cts_name = ".strtab";
788 	dmp->dm_strtab.cts_type = SHT_STRTAB;
789 	dmp->dm_strtab.cts_flags = 0;
790 	dmp->dm_strtab.cts_data = NULL;
791 	dmp->dm_strtab.cts_size = 0;
792 	dmp->dm_strtab.cts_entsize = 0;
793 	dmp->dm_strtab.cts_offset = 0;
794 
795 	/*
796 	 * Attempt to load the module's CTF section, symbol table section, and
797 	 * string table section.  Note that modules may not contain CTF data:
798 	 * this will result in a successful load_sect but data of size zero.
799 	 * We will then fail if dt_module_getctf() is called, as shown below.
800 	 */
801 	if (dt_module_load_sect(dtp, dmp, &dmp->dm_ctdata) == -1 ||
802 	    dt_module_load_sect(dtp, dmp, &dmp->dm_symtab) == -1 ||
803 	    dt_module_load_sect(dtp, dmp, &dmp->dm_strtab) == -1) {
804 		dt_module_unload(dtp, dmp);
805 		return (-1); /* dt_errno is set for us */
806 	}
807 
808 	/*
809 	 * Allocate the hash chains and hash buckets for symbol name lookup.
810 	 * This is relatively simple since the symbol table is of fixed size
811 	 * and is known in advance.  We allocate one extra element since we
812 	 * use element indices instead of pointers and zero is our sentinel.
813 	 */
814 	dmp->dm_nsymelems =
815 	    dmp->dm_symtab.cts_size / dmp->dm_symtab.cts_entsize;
816 
817 	dmp->dm_nsymbuckets = _dtrace_strbuckets;
818 	dmp->dm_symfree = 1;		/* first free element is index 1 */
819 
820 	dmp->dm_symbuckets = malloc(sizeof (uint_t) * dmp->dm_nsymbuckets);
821 	dmp->dm_symchains = malloc(sizeof (dt_sym_t) * dmp->dm_nsymelems + 1);
822 
823 	if (dmp->dm_symbuckets == NULL || dmp->dm_symchains == NULL) {
824 		dt_module_unload(dtp, dmp);
825 		return (dt_set_errno(dtp, EDT_NOMEM));
826 	}
827 
828 	bzero(dmp->dm_symbuckets, sizeof (uint_t) * dmp->dm_nsymbuckets);
829 	bzero(dmp->dm_symchains, sizeof (dt_sym_t) * dmp->dm_nsymelems + 1);
830 
831 	/*
832 	 * Iterate over the symbol table data buffer and insert each symbol
833 	 * name into the name hash if the name and type are valid.  Then
834 	 * allocate the address map, fill it in, and sort it.
835 	 */
836 	dmp->dm_asrsv = dmp->dm_ops->do_syminit(dmp);
837 
838 	dt_dprintf("hashed %s [%s] (%u symbols)\n",
839 	    dmp->dm_name, dmp->dm_symtab.cts_name, dmp->dm_symfree - 1);
840 
841 	if ((dmp->dm_asmap = malloc(sizeof (void *) * dmp->dm_asrsv)) == NULL) {
842 		dt_module_unload(dtp, dmp);
843 		return (dt_set_errno(dtp, EDT_NOMEM));
844 	}
845 
846 	dmp->dm_ops->do_symsort(dmp);
847 
848 	dt_dprintf("sorted %s [%s] (%u symbols)\n",
849 	    dmp->dm_name, dmp->dm_symtab.cts_name, dmp->dm_aslen);
850 
851 	dmp->dm_flags |= DT_DM_LOADED;
852 	return (0);
853 }
854 
855 int
856 dt_module_hasctf(dtrace_hdl_t *dtp, dt_module_t *dmp)
857 {
858 	if (dmp->dm_pid != 0 && dmp->dm_nctflibs > 0)
859 		return (1);
860 	return (dt_module_getctf(dtp, dmp) != NULL);
861 }
862 
863 ctf_file_t *
864 dt_module_getctf(dtrace_hdl_t *dtp, dt_module_t *dmp)
865 {
866 	const char *parent;
867 	dt_module_t *pmp;
868 	ctf_file_t *pfp;
869 	int model;
870 
871 	if (dmp->dm_ctfp != NULL || dt_module_load(dtp, dmp) != 0)
872 		return (dmp->dm_ctfp);
873 
874 	if (dmp->dm_ops == &dt_modops_64)
875 		model = CTF_MODEL_LP64;
876 	else
877 		model = CTF_MODEL_ILP32;
878 
879 	/*
880 	 * If the data model of the module does not match our program data
881 	 * model, then do not permit CTF from this module to be opened and
882 	 * returned to the compiler.  If we support mixed data models in the
883 	 * future for combined kernel/user tracing, this can be removed.
884 	 */
885 	if (dtp->dt_conf.dtc_ctfmodel != model) {
886 		(void) dt_set_errno(dtp, EDT_DATAMODEL);
887 		return (NULL);
888 	}
889 
890 	if (dmp->dm_ctdata.cts_size == 0) {
891 		(void) dt_set_errno(dtp, EDT_NOCTF);
892 		return (NULL);
893 	}
894 
895 	dmp->dm_ctfp = ctf_bufopen(&dmp->dm_ctdata,
896 	    &dmp->dm_symtab, &dmp->dm_strtab, &dtp->dt_ctferr);
897 
898 	if (dmp->dm_ctfp == NULL) {
899 		(void) dt_set_errno(dtp, EDT_CTF);
900 		return (NULL);
901 	}
902 
903 	(void) ctf_setmodel(dmp->dm_ctfp, model);
904 	ctf_setspecific(dmp->dm_ctfp, dmp);
905 
906 	if ((parent = ctf_parent_name(dmp->dm_ctfp)) != NULL) {
907 		if ((pmp = dt_module_create(dtp, parent)) == NULL ||
908 		    (pfp = dt_module_getctf(dtp, pmp)) == NULL) {
909 			if (pmp == NULL)
910 				(void) dt_set_errno(dtp, EDT_NOMEM);
911 			goto err;
912 		}
913 
914 		if (ctf_import(dmp->dm_ctfp, pfp) == CTF_ERR) {
915 			dtp->dt_ctferr = ctf_errno(dmp->dm_ctfp);
916 			(void) dt_set_errno(dtp, EDT_CTF);
917 			goto err;
918 		}
919 	}
920 
921 	dt_dprintf("loaded CTF container for %s (%p)\n",
922 	    dmp->dm_name, (void *)dmp->dm_ctfp);
923 
924 	return (dmp->dm_ctfp);
925 
926 err:
927 	ctf_close(dmp->dm_ctfp);
928 	dmp->dm_ctfp = NULL;
929 	return (NULL);
930 }
931 
932 /*ARGSUSED*/
933 void
934 dt_module_unload(dtrace_hdl_t *dtp, dt_module_t *dmp)
935 {
936 	int i;
937 
938 	ctf_close(dmp->dm_ctfp);
939 	dmp->dm_ctfp = NULL;
940 
941 #ifndef illumos
942 	if (dmp->dm_ctdata.cts_data != NULL) {
943 		free(dmp->dm_ctdata.cts_data);
944 	}
945 	if (dmp->dm_symtab.cts_data != NULL) {
946 		free(dmp->dm_symtab.cts_data);
947 	}
948 	if (dmp->dm_strtab.cts_data != NULL) {
949 		free(dmp->dm_strtab.cts_data);
950 	}
951 #endif
952 
953 	if (dmp->dm_libctfp != NULL) {
954 		for (i = 0; i < dmp->dm_nctflibs; i++) {
955 			ctf_close(dmp->dm_libctfp[i]);
956 			free(dmp->dm_libctfn[i]);
957 		}
958 		free(dmp->dm_libctfp);
959 		free(dmp->dm_libctfn);
960 		dmp->dm_libctfp = NULL;
961 		dmp->dm_nctflibs = 0;
962 	}
963 
964 	bzero(&dmp->dm_ctdata, sizeof (ctf_sect_t));
965 	bzero(&dmp->dm_symtab, sizeof (ctf_sect_t));
966 	bzero(&dmp->dm_strtab, sizeof (ctf_sect_t));
967 
968 	if (dmp->dm_symbuckets != NULL) {
969 		free(dmp->dm_symbuckets);
970 		dmp->dm_symbuckets = NULL;
971 	}
972 
973 	if (dmp->dm_symchains != NULL) {
974 		free(dmp->dm_symchains);
975 		dmp->dm_symchains = NULL;
976 	}
977 
978 	if (dmp->dm_asmap != NULL) {
979 		free(dmp->dm_asmap);
980 		dmp->dm_asmap = NULL;
981 	}
982 #if defined(__FreeBSD__)
983 	if (dmp->dm_sec_offsets != NULL) {
984 		free(dmp->dm_sec_offsets);
985 		dmp->dm_sec_offsets = NULL;
986 	}
987 #endif
988 	dmp->dm_symfree = 0;
989 	dmp->dm_nsymbuckets = 0;
990 	dmp->dm_nsymelems = 0;
991 	dmp->dm_asrsv = 0;
992 	dmp->dm_aslen = 0;
993 
994 	dmp->dm_text_va = 0;
995 	dmp->dm_text_size = 0;
996 	dmp->dm_data_va = 0;
997 	dmp->dm_data_size = 0;
998 	dmp->dm_bss_va = 0;
999 	dmp->dm_bss_size = 0;
1000 
1001 	if (dmp->dm_extern != NULL) {
1002 		dt_idhash_destroy(dmp->dm_extern);
1003 		dmp->dm_extern = NULL;
1004 	}
1005 
1006 	(void) elf_end(dmp->dm_elf);
1007 	dmp->dm_elf = NULL;
1008 
1009 	dmp->dm_pid = 0;
1010 
1011 	dmp->dm_flags &= ~DT_DM_LOADED;
1012 }
1013 
1014 void
1015 dt_module_destroy(dtrace_hdl_t *dtp, dt_module_t *dmp)
1016 {
1017 	uint_t h = dt_strtab_hash(dmp->dm_name, NULL) % dtp->dt_modbuckets;
1018 	dt_module_t **dmpp = &dtp->dt_mods[h];
1019 
1020 	dt_list_delete(&dtp->dt_modlist, dmp);
1021 	assert(dtp->dt_nmods != 0);
1022 	dtp->dt_nmods--;
1023 
1024 	/*
1025 	 * Now remove this module from its hash chain.  We expect to always
1026 	 * find the module on its hash chain, so in this loop we assert that
1027 	 * we don't run off the end of the list.
1028 	 */
1029 	while (*dmpp != dmp) {
1030 		dmpp = &((*dmpp)->dm_next);
1031 		assert(*dmpp != NULL);
1032 	}
1033 
1034 	*dmpp = dmp->dm_next;
1035 
1036 	dt_module_unload(dtp, dmp);
1037 	free(dmp);
1038 }
1039 
1040 /*
1041  * Insert a new external symbol reference into the specified module.  The new
1042  * symbol will be marked as undefined and is assigned a symbol index beyond
1043  * any existing cached symbols from this module.  We use the ident's di_data
1044  * field to store a pointer to a copy of the dtrace_syminfo_t for this symbol.
1045  */
1046 dt_ident_t *
1047 dt_module_extern(dtrace_hdl_t *dtp, dt_module_t *dmp,
1048     const char *name, const dtrace_typeinfo_t *tip)
1049 {
1050 	dtrace_syminfo_t *sip;
1051 	dt_ident_t *idp;
1052 	uint_t id;
1053 
1054 	if (dmp->dm_extern == NULL && (dmp->dm_extern = dt_idhash_create(
1055 	    "extern", NULL, dmp->dm_nsymelems, UINT_MAX)) == NULL) {
1056 		(void) dt_set_errno(dtp, EDT_NOMEM);
1057 		return (NULL);
1058 	}
1059 
1060 	if (dt_idhash_nextid(dmp->dm_extern, &id) == -1) {
1061 		(void) dt_set_errno(dtp, EDT_SYMOFLOW);
1062 		return (NULL);
1063 	}
1064 
1065 	if ((sip = malloc(sizeof (dtrace_syminfo_t))) == NULL) {
1066 		(void) dt_set_errno(dtp, EDT_NOMEM);
1067 		return (NULL);
1068 	}
1069 
1070 	idp = dt_idhash_insert(dmp->dm_extern, name, DT_IDENT_SYMBOL, 0, id,
1071 	    _dtrace_symattr, 0, &dt_idops_thaw, NULL, dtp->dt_gen);
1072 
1073 	if (idp == NULL) {
1074 		(void) dt_set_errno(dtp, EDT_NOMEM);
1075 		free(sip);
1076 		return (NULL);
1077 	}
1078 
1079 	sip->dts_object = dmp->dm_name;
1080 	sip->dts_name = idp->di_name;
1081 	sip->dts_id = idp->di_id;
1082 
1083 	idp->di_data = sip;
1084 	idp->di_ctfp = tip->dtt_ctfp;
1085 	idp->di_type = tip->dtt_type;
1086 
1087 	return (idp);
1088 }
1089 
1090 const char *
1091 dt_module_modelname(dt_module_t *dmp)
1092 {
1093 	if (dmp->dm_ops == &dt_modops_64)
1094 		return ("64-bit");
1095 	else
1096 		return ("32-bit");
1097 }
1098 
1099 /* ARGSUSED */
1100 int
1101 dt_module_getlibid(dtrace_hdl_t *dtp, dt_module_t *dmp, const ctf_file_t *fp)
1102 {
1103 	int i;
1104 
1105 	for (i = 0; i < dmp->dm_nctflibs; i++) {
1106 		if (dmp->dm_libctfp[i] == fp)
1107 			return (i);
1108 	}
1109 
1110 	return (-1);
1111 }
1112 
1113 /* ARGSUSED */
1114 ctf_file_t *
1115 dt_module_getctflib(dtrace_hdl_t *dtp, dt_module_t *dmp, const char *name)
1116 {
1117 	int i;
1118 
1119 	for (i = 0; i < dmp->dm_nctflibs; i++) {
1120 		if (strcmp(dmp->dm_libctfn[i], name) == 0)
1121 			return (dmp->dm_libctfp[i]);
1122 	}
1123 
1124 	return (NULL);
1125 }
1126 
1127 /*
1128  * Update our module cache by adding an entry for the specified module 'name'.
1129  * We create the dt_module_t and populate it using /system/object/<name>/.
1130  *
1131  * On FreeBSD, the module name is passed as the full module file name,
1132  * including the path.
1133  */
1134 static void
1135 #ifdef illumos
1136 dt_module_update(dtrace_hdl_t *dtp, const char *name)
1137 #else
1138 dt_module_update(dtrace_hdl_t *dtp, struct kld_file_stat *k_stat)
1139 #endif
1140 {
1141 	char fname[MAXPATHLEN];
1142 	struct stat64 st;
1143 	int fd, err, bits;
1144 #ifdef __FreeBSD__
1145 	struct module_stat ms;
1146 	dt_kmodule_t *dkmp;
1147 	uint_t h;
1148 	int modid;
1149 #endif
1150 
1151 	dt_module_t *dmp;
1152 	const char *s;
1153 	size_t shstrs;
1154 	GElf_Shdr sh;
1155 	Elf_Data *dp;
1156 	Elf_Scn *sp;
1157 
1158 #ifdef illumos
1159 	(void) snprintf(fname, sizeof (fname),
1160 	    "%s/%s/object", OBJFS_ROOT, name);
1161 #else
1162 	GElf_Ehdr ehdr;
1163 	GElf_Phdr ph;
1164 	char name[MAXPATHLEN];
1165 	uintptr_t mapbase, alignmask;
1166 	int i = 0;
1167 	int is_elf_obj;
1168 
1169 	(void) strlcpy(name, k_stat->name, sizeof(name));
1170 	(void) strlcpy(fname, k_stat->pathname, sizeof(fname));
1171 #endif
1172 
1173 	if ((fd = open(fname, O_RDONLY)) == -1 || fstat64(fd, &st) == -1 ||
1174 	    (dmp = dt_module_create(dtp, name)) == NULL) {
1175 		dt_dprintf("failed to open %s: %s\n", fname, strerror(errno));
1176 		(void) close(fd);
1177 		return;
1178 	}
1179 
1180 	/*
1181 	 * Since the module can unload out from under us (and /system/object
1182 	 * will return ENOENT), tell libelf to cook the entire file now and
1183 	 * then close the underlying file descriptor immediately.  If this
1184 	 * succeeds, we know that we can continue safely using dmp->dm_elf.
1185 	 */
1186 	dmp->dm_elf = elf_begin(fd, ELF_C_READ, NULL);
1187 	err = elf_cntl(dmp->dm_elf, ELF_C_FDREAD);
1188 	(void) close(fd);
1189 
1190 	if (dmp->dm_elf == NULL || err == -1 ||
1191 	    elf_getshdrstrndx(dmp->dm_elf, &shstrs) == -1) {
1192 		dt_dprintf("failed to load %s: %s\n",
1193 		    fname, elf_errmsg(elf_errno()));
1194 		dt_module_destroy(dtp, dmp);
1195 		return;
1196 	}
1197 
1198 	switch (gelf_getclass(dmp->dm_elf)) {
1199 	case ELFCLASS32:
1200 		dmp->dm_ops = &dt_modops_32;
1201 		bits = 32;
1202 		break;
1203 	case ELFCLASS64:
1204 		dmp->dm_ops = &dt_modops_64;
1205 		bits = 64;
1206 		break;
1207 	default:
1208 		dt_dprintf("failed to load %s: unknown ELF class\n", fname);
1209 		dt_module_destroy(dtp, dmp);
1210 		return;
1211 	}
1212 #if defined(__FreeBSD__)
1213 	mapbase = (uintptr_t)k_stat->address;
1214 	gelf_getehdr(dmp->dm_elf, &ehdr);
1215 	is_elf_obj = (ehdr.e_type == ET_REL);
1216 	if (is_elf_obj) {
1217 		dmp->dm_sec_offsets =
1218 		    malloc(ehdr.e_shnum * sizeof(*dmp->dm_sec_offsets));
1219 		if (dmp->dm_sec_offsets == NULL) {
1220 			dt_dprintf("failed to allocate memory\n");
1221 			dt_module_destroy(dtp, dmp);
1222 			return;
1223 		}
1224 	}
1225 #endif
1226 	/*
1227 	 * Iterate over the section headers locating various sections of
1228 	 * interest and use their attributes to flesh out the dt_module_t.
1229 	 */
1230 	for (sp = NULL; (sp = elf_nextscn(dmp->dm_elf, sp)) != NULL; ) {
1231 		if (gelf_getshdr(sp, &sh) == NULL || sh.sh_type == SHT_NULL ||
1232 		    (s = elf_strptr(dmp->dm_elf, shstrs, sh.sh_name)) == NULL)
1233 			continue; /* skip any malformed sections */
1234 #if defined(__FreeBSD__)
1235 		if (sh.sh_size == 0)
1236 			continue;
1237 		if (sh.sh_type == SHT_PROGBITS || sh.sh_type == SHT_NOBITS) {
1238 			alignmask = sh.sh_addralign - 1;
1239 			mapbase += alignmask;
1240 			mapbase &= ~alignmask;
1241 			sh.sh_addr = mapbase;
1242 			if (is_elf_obj)
1243 				dmp->dm_sec_offsets[elf_ndxscn(sp)] = sh.sh_addr;
1244 			mapbase += sh.sh_size;
1245 		}
1246 #endif
1247 		if (strcmp(s, ".text") == 0) {
1248 			dmp->dm_text_size = sh.sh_size;
1249 			dmp->dm_text_va = sh.sh_addr;
1250 		} else if (strcmp(s, ".data") == 0) {
1251 			dmp->dm_data_size = sh.sh_size;
1252 			dmp->dm_data_va = sh.sh_addr;
1253 		} else if (strcmp(s, ".bss") == 0) {
1254 			dmp->dm_bss_size = sh.sh_size;
1255 			dmp->dm_bss_va = sh.sh_addr;
1256 		} else if (strcmp(s, ".info") == 0 &&
1257 		    (dp = elf_getdata(sp, NULL)) != NULL) {
1258 			bcopy(dp->d_buf, &dmp->dm_info,
1259 			    MIN(sh.sh_size, sizeof (dmp->dm_info)));
1260 		} else if (strcmp(s, ".filename") == 0 &&
1261 		    (dp = elf_getdata(sp, NULL)) != NULL) {
1262 			(void) strlcpy(dmp->dm_file,
1263 			    dp->d_buf, sizeof (dmp->dm_file));
1264 		}
1265 	}
1266 
1267 	dmp->dm_flags |= DT_DM_KERNEL;
1268 #ifdef illumos
1269 	dmp->dm_modid = (int)OBJFS_MODID(st.st_ino);
1270 #else
1271 	/*
1272 	 * Include .rodata and special sections into .text.
1273 	 * This depends on default section layout produced by GNU ld
1274 	 * for ELF objects and libraries:
1275 	 * [Text][R/O data][R/W data][Dynamic][BSS][Non loadable]
1276 	 */
1277 	dmp->dm_text_size = dmp->dm_data_va - dmp->dm_text_va;
1278 #if defined(__i386__)
1279 	/*
1280 	 * Find the first load section and figure out the relocation
1281 	 * offset for the symbols. The kernel module will not need
1282 	 * relocation, but the kernel linker modules will.
1283 	 */
1284 	for (i = 0; gelf_getphdr(dmp->dm_elf, i, &ph) != NULL; i++) {
1285 		if (ph.p_type == PT_LOAD) {
1286 			dmp->dm_reloc_offset = k_stat->address - ph.p_vaddr;
1287 			break;
1288 		}
1289 	}
1290 #endif
1291 #endif /* illumos */
1292 
1293 	if (dmp->dm_info.objfs_info_primary)
1294 		dmp->dm_flags |= DT_DM_PRIMARY;
1295 
1296 #ifdef __FreeBSD__
1297 	ms.version = sizeof(ms);
1298 	for (modid = kldfirstmod(k_stat->id); modid > 0;
1299 	    modid = modnext(modid)) {
1300 		if (modstat(modid, &ms) != 0) {
1301 			dt_dprintf("modstat failed for id %d in %s: %s\n",
1302 			    modid, k_stat->name, strerror(errno));
1303 			continue;
1304 		}
1305 		if (dt_kmodule_lookup(dtp, ms.name) != NULL)
1306 			continue;
1307 
1308 		dkmp = malloc(sizeof (*dkmp));
1309 		if (dkmp == NULL) {
1310 			dt_dprintf("failed to allocate memory\n");
1311 			dt_module_destroy(dtp, dmp);
1312 			return;
1313 		}
1314 
1315 		h = dt_strtab_hash(ms.name, NULL) % dtp->dt_modbuckets;
1316 		dkmp->dkm_next = dtp->dt_kmods[h];
1317 		dkmp->dkm_name = strdup(ms.name);
1318 		dkmp->dkm_module = dmp;
1319 		dtp->dt_kmods[h] = dkmp;
1320 	}
1321 #endif
1322 
1323 	dt_dprintf("opened %d-bit module %s (%s) [%d]\n",
1324 	    bits, dmp->dm_name, dmp->dm_file, dmp->dm_modid);
1325 }
1326 
1327 /*
1328  * Unload all the loaded modules and then refresh the module cache with the
1329  * latest list of loaded modules and their address ranges.
1330  */
1331 void
1332 dtrace_update(dtrace_hdl_t *dtp)
1333 {
1334 	dt_module_t *dmp;
1335 	DIR *dirp;
1336 #if defined(__FreeBSD__)
1337 	int fileid;
1338 #endif
1339 
1340 	for (dmp = dt_list_next(&dtp->dt_modlist);
1341 	    dmp != NULL; dmp = dt_list_next(dmp))
1342 		dt_module_unload(dtp, dmp);
1343 
1344 #ifdef illumos
1345 	/*
1346 	 * Open /system/object and attempt to create a libdtrace module for
1347 	 * each kernel module that is loaded on the current system.
1348 	 */
1349 	if (!(dtp->dt_oflags & DTRACE_O_NOSYS) &&
1350 	    (dirp = opendir(OBJFS_ROOT)) != NULL) {
1351 		struct dirent *dp;
1352 
1353 		while ((dp = readdir(dirp)) != NULL) {
1354 			if (dp->d_name[0] != '.')
1355 				dt_module_update(dtp, dp->d_name);
1356 		}
1357 
1358 		(void) closedir(dirp);
1359 	}
1360 #elif defined(__FreeBSD__)
1361 	/*
1362 	 * Use FreeBSD's kernel loader interface to discover what kernel
1363 	 * modules are loaded and create a libdtrace module for each one.
1364 	 */
1365 	for (fileid = kldnext(0); fileid > 0; fileid = kldnext(fileid)) {
1366 		struct kld_file_stat k_stat;
1367 		k_stat.version = sizeof(k_stat);
1368 		if (kldstat(fileid, &k_stat) == 0)
1369 			dt_module_update(dtp, &k_stat);
1370 	}
1371 #endif
1372 
1373 	/*
1374 	 * Look up all the macro identifiers and set di_id to the latest value.
1375 	 * This code collaborates with dt_lex.l on the use of di_id.  We will
1376 	 * need to implement something fancier if we need to support non-ints.
1377 	 */
1378 	dt_idhash_lookup(dtp->dt_macros, "egid")->di_id = getegid();
1379 	dt_idhash_lookup(dtp->dt_macros, "euid")->di_id = geteuid();
1380 	dt_idhash_lookup(dtp->dt_macros, "gid")->di_id = getgid();
1381 	dt_idhash_lookup(dtp->dt_macros, "pid")->di_id = getpid();
1382 	dt_idhash_lookup(dtp->dt_macros, "pgid")->di_id = getpgid(0);
1383 	dt_idhash_lookup(dtp->dt_macros, "ppid")->di_id = getppid();
1384 #ifdef illumos
1385 	dt_idhash_lookup(dtp->dt_macros, "projid")->di_id = getprojid();
1386 #endif
1387 	dt_idhash_lookup(dtp->dt_macros, "sid")->di_id = getsid(0);
1388 #ifdef illumos
1389 	dt_idhash_lookup(dtp->dt_macros, "taskid")->di_id = gettaskid();
1390 #endif
1391 	dt_idhash_lookup(dtp->dt_macros, "uid")->di_id = getuid();
1392 
1393 	/*
1394 	 * Cache the pointers to the modules representing the base executable
1395 	 * and the run-time linker in the dtrace client handle. Note that on
1396 	 * x86 krtld is folded into unix, so if we don't find it, use unix
1397 	 * instead.
1398 	 */
1399 	dtp->dt_exec = dt_module_lookup_by_name(dtp, "genunix");
1400 	dtp->dt_rtld = dt_module_lookup_by_name(dtp, "krtld");
1401 	if (dtp->dt_rtld == NULL)
1402 		dtp->dt_rtld = dt_module_lookup_by_name(dtp, "unix");
1403 
1404 	/*
1405 	 * If this is the first time we are initializing the module list,
1406 	 * remove the module for genunix from the module list and then move it
1407 	 * to the front of the module list.  We do this so that type and symbol
1408 	 * queries encounter genunix and thereby optimize for the common case
1409 	 * in dtrace_lookup_by_name() and dtrace_lookup_by_type(), below.
1410 	 */
1411 	if (dtp->dt_exec != NULL &&
1412 	    dtp->dt_cdefs == NULL && dtp->dt_ddefs == NULL) {
1413 		dt_list_delete(&dtp->dt_modlist, dtp->dt_exec);
1414 		dt_list_prepend(&dtp->dt_modlist, dtp->dt_exec);
1415 	}
1416 }
1417 
1418 static dt_module_t *
1419 dt_module_from_object(dtrace_hdl_t *dtp, const char *object)
1420 {
1421 	int err = EDT_NOMOD;
1422 	dt_module_t *dmp;
1423 
1424 	switch ((uintptr_t)object) {
1425 	case (uintptr_t)DTRACE_OBJ_EXEC:
1426 		dmp = dtp->dt_exec;
1427 		break;
1428 	case (uintptr_t)DTRACE_OBJ_RTLD:
1429 		dmp = dtp->dt_rtld;
1430 		break;
1431 	case (uintptr_t)DTRACE_OBJ_CDEFS:
1432 		dmp = dtp->dt_cdefs;
1433 		break;
1434 	case (uintptr_t)DTRACE_OBJ_DDEFS:
1435 		dmp = dtp->dt_ddefs;
1436 		break;
1437 	default:
1438 		dmp = dt_module_create(dtp, object);
1439 		err = EDT_NOMEM;
1440 	}
1441 
1442 	if (dmp == NULL)
1443 		(void) dt_set_errno(dtp, err);
1444 
1445 	return (dmp);
1446 }
1447 
1448 /*
1449  * Exported interface to look up a symbol by name.  We return the GElf_Sym and
1450  * complete symbol information for the matching symbol.
1451  */
1452 int
1453 dtrace_lookup_by_name(dtrace_hdl_t *dtp, const char *object, const char *name,
1454     GElf_Sym *symp, dtrace_syminfo_t *sip)
1455 {
1456 	dt_module_t *dmp;
1457 	dt_ident_t *idp;
1458 	uint_t n, id;
1459 	GElf_Sym sym;
1460 
1461 	uint_t mask = 0; /* mask of dt_module flags to match */
1462 	uint_t bits = 0; /* flag bits that must be present */
1463 
1464 	if (object != DTRACE_OBJ_EVERY &&
1465 	    object != DTRACE_OBJ_KMODS &&
1466 	    object != DTRACE_OBJ_UMODS) {
1467 		if ((dmp = dt_module_from_object(dtp, object)) == NULL)
1468 			return (-1); /* dt_errno is set for us */
1469 
1470 		if (dt_module_load(dtp, dmp) == -1)
1471 			return (-1); /* dt_errno is set for us */
1472 		n = 1;
1473 
1474 	} else {
1475 		if (object == DTRACE_OBJ_KMODS)
1476 			mask = bits = DT_DM_KERNEL;
1477 		else if (object == DTRACE_OBJ_UMODS)
1478 			mask = DT_DM_KERNEL;
1479 
1480 		dmp = dt_list_next(&dtp->dt_modlist);
1481 		n = dtp->dt_nmods;
1482 	}
1483 
1484 	if (symp == NULL)
1485 		symp = &sym;
1486 
1487 	for (; n > 0; n--, dmp = dt_list_next(dmp)) {
1488 		if ((dmp->dm_flags & mask) != bits)
1489 			continue; /* failed to match required attributes */
1490 
1491 		if (dt_module_load(dtp, dmp) == -1)
1492 			continue; /* failed to load symbol table */
1493 
1494 		if (dmp->dm_ops->do_symname(dmp, name, symp, &id) != NULL) {
1495 			if (sip != NULL) {
1496 				sip->dts_object = dmp->dm_name;
1497 				sip->dts_name = (const char *)
1498 				    dmp->dm_strtab.cts_data + symp->st_name;
1499 				sip->dts_id = id;
1500 			}
1501 			return (0);
1502 		}
1503 
1504 		if (dmp->dm_extern != NULL &&
1505 		    (idp = dt_idhash_lookup(dmp->dm_extern, name)) != NULL) {
1506 			if (symp != &sym) {
1507 				symp->st_name = (uintptr_t)idp->di_name;
1508 				symp->st_info =
1509 				    GELF_ST_INFO(STB_GLOBAL, STT_NOTYPE);
1510 				symp->st_other = 0;
1511 				symp->st_shndx = SHN_UNDEF;
1512 				symp->st_value = 0;
1513 				symp->st_size =
1514 				    ctf_type_size(idp->di_ctfp, idp->di_type);
1515 			}
1516 
1517 			if (sip != NULL) {
1518 				sip->dts_object = dmp->dm_name;
1519 				sip->dts_name = idp->di_name;
1520 				sip->dts_id = idp->di_id;
1521 			}
1522 
1523 			return (0);
1524 		}
1525 	}
1526 
1527 	return (dt_set_errno(dtp, EDT_NOSYM));
1528 }
1529 
1530 /*
1531  * Exported interface to look up a symbol by address.  We return the GElf_Sym
1532  * and complete symbol information for the matching symbol.
1533  */
1534 int
1535 dtrace_lookup_by_addr(dtrace_hdl_t *dtp, GElf_Addr addr,
1536     GElf_Sym *symp, dtrace_syminfo_t *sip)
1537 {
1538 	dt_module_t *dmp;
1539 	uint_t id;
1540 	const dtrace_vector_t *v = dtp->dt_vector;
1541 
1542 	if (v != NULL)
1543 		return (v->dtv_lookup_by_addr(dtp->dt_varg, addr, symp, sip));
1544 
1545 	for (dmp = dt_list_next(&dtp->dt_modlist); dmp != NULL;
1546 	    dmp = dt_list_next(dmp)) {
1547 		if (addr - dmp->dm_text_va < dmp->dm_text_size ||
1548 		    addr - dmp->dm_data_va < dmp->dm_data_size ||
1549 		    addr - dmp->dm_bss_va < dmp->dm_bss_size)
1550 			break;
1551 	}
1552 
1553 	if (dmp == NULL)
1554 		return (dt_set_errno(dtp, EDT_NOSYMADDR));
1555 
1556 	if (dt_module_load(dtp, dmp) == -1)
1557 		return (-1); /* dt_errno is set for us */
1558 
1559 	if (symp != NULL) {
1560 		if (dmp->dm_ops->do_symaddr(dmp, addr, symp, &id) == NULL)
1561 			return (dt_set_errno(dtp, EDT_NOSYMADDR));
1562 	}
1563 
1564 	if (sip != NULL) {
1565 		sip->dts_object = dmp->dm_name;
1566 
1567 		if (symp != NULL) {
1568 			sip->dts_name = (const char *)
1569 			    dmp->dm_strtab.cts_data + symp->st_name;
1570 			sip->dts_id = id;
1571 		} else {
1572 			sip->dts_name = NULL;
1573 			sip->dts_id = 0;
1574 		}
1575 	}
1576 
1577 	return (0);
1578 }
1579 
1580 int
1581 dtrace_lookup_by_type(dtrace_hdl_t *dtp, const char *object, const char *name,
1582     dtrace_typeinfo_t *tip)
1583 {
1584 	dtrace_typeinfo_t ti;
1585 	dt_module_t *dmp;
1586 	int found = 0;
1587 	ctf_id_t id;
1588 	uint_t n, i;
1589 	int justone;
1590 	ctf_file_t *fp;
1591 	char *buf, *p, *q;
1592 
1593 	uint_t mask = 0; /* mask of dt_module flags to match */
1594 	uint_t bits = 0; /* flag bits that must be present */
1595 
1596 	if (object != DTRACE_OBJ_EVERY &&
1597 	    object != DTRACE_OBJ_KMODS &&
1598 	    object != DTRACE_OBJ_UMODS) {
1599 		if ((dmp = dt_module_from_object(dtp, object)) == NULL)
1600 			return (-1); /* dt_errno is set for us */
1601 
1602 		if (dt_module_load(dtp, dmp) == -1)
1603 			return (-1); /* dt_errno is set for us */
1604 		n = 1;
1605 		justone = 1;
1606 	} else {
1607 		if (object == DTRACE_OBJ_KMODS)
1608 			mask = bits = DT_DM_KERNEL;
1609 		else if (object == DTRACE_OBJ_UMODS)
1610 			mask = DT_DM_KERNEL;
1611 
1612 		dmp = dt_list_next(&dtp->dt_modlist);
1613 		n = dtp->dt_nmods;
1614 		justone = 0;
1615 	}
1616 
1617 	if (tip == NULL)
1618 		tip = &ti;
1619 
1620 	for (; n > 0; n--, dmp = dt_list_next(dmp)) {
1621 		if ((dmp->dm_flags & mask) != bits)
1622 			continue; /* failed to match required attributes */
1623 
1624 		/*
1625 		 * If we can't load the CTF container, continue on to the next
1626 		 * module.  If our search was scoped to only one module then
1627 		 * return immediately leaving dt_errno unmodified.
1628 		 */
1629 		if (dt_module_hasctf(dtp, dmp) == 0) {
1630 			if (justone)
1631 				return (-1);
1632 			continue;
1633 		}
1634 
1635 		/*
1636 		 * Look up the type in the module's CTF container.  If our
1637 		 * match is a forward declaration tag, save this choice in
1638 		 * 'tip' and keep going in the hope that we will locate the
1639 		 * underlying structure definition.  Otherwise just return.
1640 		 */
1641 		if (dmp->dm_pid == 0) {
1642 			id = ctf_lookup_by_name(dmp->dm_ctfp, name);
1643 			fp = dmp->dm_ctfp;
1644 		} else {
1645 			if ((p = strchr(name, '`')) != NULL) {
1646 				buf = strdup(name);
1647 				if (buf == NULL)
1648 					return (dt_set_errno(dtp, EDT_NOMEM));
1649 				p = strchr(buf, '`');
1650 				if ((q = strchr(p + 1, '`')) != NULL)
1651 					p = q;
1652 				*p = '\0';
1653 				fp = dt_module_getctflib(dtp, dmp, buf);
1654 				if (fp == NULL || (id = ctf_lookup_by_name(fp,
1655 				    p + 1)) == CTF_ERR)
1656 					id = CTF_ERR;
1657 				free(buf);
1658 			} else {
1659 				for (i = 0; i < dmp->dm_nctflibs; i++) {
1660 					fp = dmp->dm_libctfp[i];
1661 					id = ctf_lookup_by_name(fp, name);
1662 					if (id != CTF_ERR)
1663 						break;
1664 				}
1665 			}
1666 		}
1667 		if (id != CTF_ERR) {
1668 			tip->dtt_object = dmp->dm_name;
1669 			tip->dtt_ctfp = fp;
1670 			tip->dtt_type = id;
1671 			if (ctf_type_kind(fp, ctf_type_resolve(fp, id)) !=
1672 			    CTF_K_FORWARD)
1673 				return (0);
1674 
1675 			found++;
1676 		}
1677 	}
1678 
1679 	if (found == 0)
1680 		return (dt_set_errno(dtp, EDT_NOTYPE));
1681 
1682 	return (0);
1683 }
1684 
1685 int
1686 dtrace_symbol_type(dtrace_hdl_t *dtp, const GElf_Sym *symp,
1687     const dtrace_syminfo_t *sip, dtrace_typeinfo_t *tip)
1688 {
1689 	dt_module_t *dmp;
1690 
1691 	tip->dtt_object = NULL;
1692 	tip->dtt_ctfp = NULL;
1693 	tip->dtt_type = CTF_ERR;
1694 	tip->dtt_flags = 0;
1695 
1696 	if ((dmp = dt_module_lookup_by_name(dtp, sip->dts_object)) == NULL)
1697 		return (dt_set_errno(dtp, EDT_NOMOD));
1698 
1699 	if (symp->st_shndx == SHN_UNDEF && dmp->dm_extern != NULL) {
1700 		dt_ident_t *idp =
1701 		    dt_idhash_lookup(dmp->dm_extern, sip->dts_name);
1702 
1703 		if (idp == NULL)
1704 			return (dt_set_errno(dtp, EDT_NOSYM));
1705 
1706 		tip->dtt_ctfp = idp->di_ctfp;
1707 		tip->dtt_type = idp->di_type;
1708 
1709 	} else if (GELF_ST_TYPE(symp->st_info) != STT_FUNC) {
1710 		if (dt_module_getctf(dtp, dmp) == NULL)
1711 			return (-1); /* errno is set for us */
1712 
1713 		tip->dtt_ctfp = dmp->dm_ctfp;
1714 		tip->dtt_type = ctf_lookup_by_symbol(dmp->dm_ctfp, sip->dts_id);
1715 
1716 		if (tip->dtt_type == CTF_ERR) {
1717 			dtp->dt_ctferr = ctf_errno(tip->dtt_ctfp);
1718 			return (dt_set_errno(dtp, EDT_CTF));
1719 		}
1720 
1721 	} else {
1722 		tip->dtt_ctfp = DT_FPTR_CTFP(dtp);
1723 		tip->dtt_type = DT_FPTR_TYPE(dtp);
1724 	}
1725 
1726 	tip->dtt_object = dmp->dm_name;
1727 	return (0);
1728 }
1729 
1730 static dtrace_objinfo_t *
1731 dt_module_info(const dt_module_t *dmp, dtrace_objinfo_t *dto)
1732 {
1733 	dto->dto_name = dmp->dm_name;
1734 	dto->dto_file = dmp->dm_file;
1735 	dto->dto_id = dmp->dm_modid;
1736 	dto->dto_flags = 0;
1737 
1738 	if (dmp->dm_flags & DT_DM_KERNEL)
1739 		dto->dto_flags |= DTRACE_OBJ_F_KERNEL;
1740 	if (dmp->dm_flags & DT_DM_PRIMARY)
1741 		dto->dto_flags |= DTRACE_OBJ_F_PRIMARY;
1742 
1743 	dto->dto_text_va = dmp->dm_text_va;
1744 	dto->dto_text_size = dmp->dm_text_size;
1745 	dto->dto_data_va = dmp->dm_data_va;
1746 	dto->dto_data_size = dmp->dm_data_size;
1747 	dto->dto_bss_va = dmp->dm_bss_va;
1748 	dto->dto_bss_size = dmp->dm_bss_size;
1749 
1750 	return (dto);
1751 }
1752 
1753 int
1754 dtrace_object_iter(dtrace_hdl_t *dtp, dtrace_obj_f *func, void *data)
1755 {
1756 	const dt_module_t *dmp = dt_list_next(&dtp->dt_modlist);
1757 	dtrace_objinfo_t dto;
1758 	int rv;
1759 
1760 	for (; dmp != NULL; dmp = dt_list_next(dmp)) {
1761 		if ((rv = (*func)(dtp, dt_module_info(dmp, &dto), data)) != 0)
1762 			return (rv);
1763 	}
1764 
1765 	return (0);
1766 }
1767 
1768 int
1769 dtrace_object_info(dtrace_hdl_t *dtp, const char *object, dtrace_objinfo_t *dto)
1770 {
1771 	dt_module_t *dmp;
1772 
1773 	if (object == DTRACE_OBJ_EVERY || object == DTRACE_OBJ_KMODS ||
1774 	    object == DTRACE_OBJ_UMODS || dto == NULL)
1775 		return (dt_set_errno(dtp, EINVAL));
1776 
1777 	if ((dmp = dt_module_from_object(dtp, object)) == NULL)
1778 		return (-1); /* dt_errno is set for us */
1779 
1780 	if (dt_module_load(dtp, dmp) == -1)
1781 		return (-1); /* dt_errno is set for us */
1782 
1783 	(void) dt_module_info(dmp, dto);
1784 	return (0);
1785 }
1786