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