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