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_unlock(dtp, p);
715 		dt_proc_release(dtp, p);
716 		return (dt_set_errno(dtp, EDT_CANTLOAD));
717 	}
718 
719 	if (arg.dpa_count == 0) {
720 		dt_dprintf("no ctf data present\n");
721 		dt_proc_unlock(dtp, p);
722 		dt_proc_release(dtp, p);
723 		return (dt_set_errno(dtp, EDT_CANTLOAD));
724 	}
725 
726 	dmp->dm_libctfp = calloc(arg.dpa_count, sizeof (ctf_file_t *));
727 	if (dmp->dm_libctfp == NULL) {
728 		dt_proc_unlock(dtp, p);
729 		dt_proc_release(dtp, p);
730 		return (dt_set_errno(dtp, EDT_NOMEM));
731 	}
732 
733 	dmp->dm_libctfn = calloc(arg.dpa_count, sizeof (char *));
734 	if (dmp->dm_libctfn == NULL) {
735 		free(dmp->dm_libctfp);
736 		dt_proc_unlock(dtp, p);
737 		dt_proc_release(dtp, p);
738 		return (dt_set_errno(dtp, EDT_NOMEM));
739 	}
740 
741 	dmp->dm_nctflibs = arg.dpa_count;
742 
743 	arg.dpa_count = 0;
744 	if (Pobject_iter_resolved(p, dt_module_load_proc_build, &arg) != 0) {
745 		dt_proc_unlock(dtp, p);
746 		dt_module_unload(dtp, dmp);
747 		dt_proc_release(dtp, p);
748 		return (dt_set_errno(dtp, EDT_CANTLOAD));
749 	}
750 	assert(arg.dpa_count == dmp->dm_nctflibs);
751 	dt_dprintf("loaded %d ctf modules for pid %d\n", arg.dpa_count,
752 	    (int)dmp->dm_pid);
753 
754 	dt_proc_unlock(dtp, p);
755 	dt_proc_release(dtp, p);
756 	dmp->dm_flags |= DT_DM_LOADED;
757 
758 	return (0);
759 }
760 
761 int
762 dt_module_load(dtrace_hdl_t *dtp, dt_module_t *dmp)
763 {
764 	if (dmp->dm_flags & DT_DM_LOADED)
765 		return (0); /* module is already loaded */
766 
767 	if (dmp->dm_pid != 0)
768 		return (dt_module_load_proc(dtp, dmp));
769 
770 	dmp->dm_ctdata.cts_name = ".SUNW_ctf";
771 	dmp->dm_ctdata.cts_type = SHT_PROGBITS;
772 	dmp->dm_ctdata.cts_flags = 0;
773 	dmp->dm_ctdata.cts_data = NULL;
774 	dmp->dm_ctdata.cts_size = 0;
775 	dmp->dm_ctdata.cts_entsize = 0;
776 	dmp->dm_ctdata.cts_offset = 0;
777 
778 	dmp->dm_symtab.cts_name = ".symtab";
779 	dmp->dm_symtab.cts_type = SHT_SYMTAB;
780 	dmp->dm_symtab.cts_flags = 0;
781 	dmp->dm_symtab.cts_data = NULL;
782 	dmp->dm_symtab.cts_size = 0;
783 	dmp->dm_symtab.cts_entsize = dmp->dm_ops == &dt_modops_64 ?
784 	    sizeof (Elf64_Sym) : sizeof (Elf32_Sym);
785 	dmp->dm_symtab.cts_offset = 0;
786 
787 	dmp->dm_strtab.cts_name = ".strtab";
788 	dmp->dm_strtab.cts_type = SHT_STRTAB;
789 	dmp->dm_strtab.cts_flags = 0;
790 	dmp->dm_strtab.cts_data = NULL;
791 	dmp->dm_strtab.cts_size = 0;
792 	dmp->dm_strtab.cts_entsize = 0;
793 	dmp->dm_strtab.cts_offset = 0;
794 
795 	/*
796 	 * Attempt to load the module's CTF section, symbol table section, and
797 	 * string table section.  Note that modules may not contain CTF data:
798 	 * this will result in a successful load_sect but data of size zero.
799 	 * We will then fail if dt_module_getctf() is called, as shown below.
800 	 */
801 	if (dt_module_load_sect(dtp, dmp, &dmp->dm_ctdata) == -1 ||
802 	    dt_module_load_sect(dtp, dmp, &dmp->dm_symtab) == -1 ||
803 	    dt_module_load_sect(dtp, dmp, &dmp->dm_strtab) == -1) {
804 		dt_module_unload(dtp, dmp);
805 		return (-1); /* dt_errno is set for us */
806 	}
807 
808 	/*
809 	 * Allocate the hash chains and hash buckets for symbol name lookup.
810 	 * This is relatively simple since the symbol table is of fixed size
811 	 * and is known in advance.  We allocate one extra element since we
812 	 * use element indices instead of pointers and zero is our sentinel.
813 	 */
814 	dmp->dm_nsymelems =
815 	    dmp->dm_symtab.cts_size / dmp->dm_symtab.cts_entsize;
816 
817 	dmp->dm_nsymbuckets = _dtrace_strbuckets;
818 	dmp->dm_symfree = 1;		/* first free element is index 1 */
819 
820 	dmp->dm_symbuckets = calloc(dmp->dm_nsymbuckets, sizeof (uint_t));
821 	dmp->dm_symchains = calloc(dmp->dm_nsymelems + 1, sizeof (dt_sym_t));
822 
823 	if (dmp->dm_symbuckets == NULL || dmp->dm_symchains == NULL) {
824 		dt_module_unload(dtp, dmp);
825 		return (dt_set_errno(dtp, EDT_NOMEM));
826 	}
827 
828 	/*
829 	 * Iterate over the symbol table data buffer and insert each symbol
830 	 * name into the name hash if the name and type are valid.  Then
831 	 * allocate the address map, fill it in, and sort it.
832 	 */
833 	dmp->dm_asrsv = dmp->dm_ops->do_syminit(dmp);
834 
835 	dt_dprintf("hashed %s [%s] (%u symbols)\n",
836 	    dmp->dm_name, dmp->dm_symtab.cts_name, dmp->dm_symfree - 1);
837 
838 	if ((dmp->dm_asmap = malloc(sizeof (void *) * dmp->dm_asrsv)) == NULL) {
839 		dt_module_unload(dtp, dmp);
840 		return (dt_set_errno(dtp, EDT_NOMEM));
841 	}
842 
843 	dmp->dm_ops->do_symsort(dmp);
844 
845 	dt_dprintf("sorted %s [%s] (%u symbols)\n",
846 	    dmp->dm_name, dmp->dm_symtab.cts_name, dmp->dm_aslen);
847 
848 	dmp->dm_flags |= DT_DM_LOADED;
849 	return (0);
850 }
851 
852 int
853 dt_module_hasctf(dtrace_hdl_t *dtp, dt_module_t *dmp)
854 {
855 	if (dmp->dm_pid != 0 && dmp->dm_nctflibs > 0)
856 		return (1);
857 	return (dt_module_getctf(dtp, dmp) != NULL);
858 }
859 
860 ctf_file_t *
861 dt_module_getctf(dtrace_hdl_t *dtp, dt_module_t *dmp)
862 {
863 	const char *parent;
864 	dt_module_t *pmp;
865 	ctf_file_t *pfp;
866 	int model;
867 
868 	if (dmp->dm_ctfp != NULL || dt_module_load(dtp, dmp) != 0)
869 		return (dmp->dm_ctfp);
870 
871 	if (dmp->dm_ops == &dt_modops_64)
872 		model = CTF_MODEL_LP64;
873 	else
874 		model = CTF_MODEL_ILP32;
875 
876 	/*
877 	 * If the data model of the module does not match our program data
878 	 * model, then do not permit CTF from this module to be opened and
879 	 * returned to the compiler.  If we support mixed data models in the
880 	 * future for combined kernel/user tracing, this can be removed.
881 	 */
882 	if (dtp->dt_conf.dtc_ctfmodel != model) {
883 		(void) dt_set_errno(dtp, EDT_DATAMODEL);
884 		return (NULL);
885 	}
886 
887 	if (dmp->dm_ctdata.cts_size == 0) {
888 		(void) dt_set_errno(dtp, EDT_NOCTF);
889 		return (NULL);
890 	}
891 
892 	dmp->dm_ctfp = ctf_bufopen(&dmp->dm_ctdata,
893 	    &dmp->dm_symtab, &dmp->dm_strtab, &dtp->dt_ctferr);
894 
895 	if (dmp->dm_ctfp == NULL) {
896 		(void) dt_set_errno(dtp, EDT_CTF);
897 		return (NULL);
898 	}
899 
900 	(void) ctf_setmodel(dmp->dm_ctfp, model);
901 	ctf_setspecific(dmp->dm_ctfp, dmp);
902 
903 	if ((parent = ctf_parent_name(dmp->dm_ctfp)) != NULL) {
904 		if ((pmp = dt_module_create(dtp, parent)) == NULL ||
905 		    (pfp = dt_module_getctf(dtp, pmp)) == NULL) {
906 			if (pmp == NULL)
907 				(void) dt_set_errno(dtp, EDT_NOMEM);
908 			goto err;
909 		}
910 
911 		if (ctf_import(dmp->dm_ctfp, pfp) == CTF_ERR) {
912 			dtp->dt_ctferr = ctf_errno(dmp->dm_ctfp);
913 			(void) dt_set_errno(dtp, EDT_CTF);
914 			goto err;
915 		}
916 	}
917 
918 	dt_dprintf("loaded CTF container for %s (%p)\n",
919 	    dmp->dm_name, (void *)dmp->dm_ctfp);
920 
921 	return (dmp->dm_ctfp);
922 
923 err:
924 	ctf_close(dmp->dm_ctfp);
925 	dmp->dm_ctfp = NULL;
926 	return (NULL);
927 }
928 
929 /*ARGSUSED*/
930 void
931 dt_module_unload(dtrace_hdl_t *dtp, dt_module_t *dmp)
932 {
933 	int i;
934 
935 	ctf_close(dmp->dm_ctfp);
936 	dmp->dm_ctfp = NULL;
937 
938 #ifndef illumos
939 	if (dmp->dm_ctdata.cts_data != NULL) {
940 		free(dmp->dm_ctdata.cts_data);
941 	}
942 	if (dmp->dm_symtab.cts_data != NULL) {
943 		free(dmp->dm_symtab.cts_data);
944 	}
945 	if (dmp->dm_strtab.cts_data != NULL) {
946 		free(dmp->dm_strtab.cts_data);
947 	}
948 #endif
949 
950 	if (dmp->dm_libctfp != NULL) {
951 		for (i = 0; i < dmp->dm_nctflibs; i++) {
952 			ctf_close(dmp->dm_libctfp[i]);
953 			free(dmp->dm_libctfn[i]);
954 		}
955 		free(dmp->dm_libctfp);
956 		free(dmp->dm_libctfn);
957 		dmp->dm_libctfp = NULL;
958 		dmp->dm_nctflibs = 0;
959 	}
960 
961 	bzero(&dmp->dm_ctdata, sizeof (ctf_sect_t));
962 	bzero(&dmp->dm_symtab, sizeof (ctf_sect_t));
963 	bzero(&dmp->dm_strtab, sizeof (ctf_sect_t));
964 
965 	if (dmp->dm_symbuckets != NULL) {
966 		free(dmp->dm_symbuckets);
967 		dmp->dm_symbuckets = NULL;
968 	}
969 
970 	if (dmp->dm_symchains != NULL) {
971 		free(dmp->dm_symchains);
972 		dmp->dm_symchains = NULL;
973 	}
974 
975 	if (dmp->dm_asmap != NULL) {
976 		free(dmp->dm_asmap);
977 		dmp->dm_asmap = NULL;
978 	}
979 #if defined(__FreeBSD__)
980 	if (dmp->dm_sec_offsets != NULL) {
981 		free(dmp->dm_sec_offsets);
982 		dmp->dm_sec_offsets = NULL;
983 	}
984 #endif
985 	dmp->dm_symfree = 0;
986 	dmp->dm_nsymbuckets = 0;
987 	dmp->dm_nsymelems = 0;
988 	dmp->dm_asrsv = 0;
989 	dmp->dm_aslen = 0;
990 
991 	dmp->dm_text_va = 0;
992 	dmp->dm_text_size = 0;
993 	dmp->dm_data_va = 0;
994 	dmp->dm_data_size = 0;
995 	dmp->dm_bss_va = 0;
996 	dmp->dm_bss_size = 0;
997 
998 	if (dmp->dm_extern != NULL) {
999 		dt_idhash_destroy(dmp->dm_extern);
1000 		dmp->dm_extern = NULL;
1001 	}
1002 
1003 	(void) elf_end(dmp->dm_elf);
1004 	dmp->dm_elf = NULL;
1005 
1006 	dmp->dm_pid = 0;
1007 
1008 	dmp->dm_flags &= ~DT_DM_LOADED;
1009 }
1010 
1011 void
1012 dt_module_destroy(dtrace_hdl_t *dtp, dt_module_t *dmp)
1013 {
1014 	uint_t h = dt_strtab_hash(dmp->dm_name, NULL) % dtp->dt_modbuckets;
1015 	dt_module_t **dmpp = &dtp->dt_mods[h];
1016 
1017 	dt_list_delete(&dtp->dt_modlist, dmp);
1018 	assert(dtp->dt_nmods != 0);
1019 	dtp->dt_nmods--;
1020 
1021 	/*
1022 	 * Now remove this module from its hash chain.  We expect to always
1023 	 * find the module on its hash chain, so in this loop we assert that
1024 	 * we don't run off the end of the list.
1025 	 */
1026 	while (*dmpp != dmp) {
1027 		dmpp = &((*dmpp)->dm_next);
1028 		assert(*dmpp != NULL);
1029 	}
1030 
1031 	*dmpp = dmp->dm_next;
1032 
1033 	dt_module_unload(dtp, dmp);
1034 	free(dmp);
1035 }
1036 
1037 /*
1038  * Insert a new external symbol reference into the specified module.  The new
1039  * symbol will be marked as undefined and is assigned a symbol index beyond
1040  * any existing cached symbols from this module.  We use the ident's di_data
1041  * field to store a pointer to a copy of the dtrace_syminfo_t for this symbol.
1042  */
1043 dt_ident_t *
1044 dt_module_extern(dtrace_hdl_t *dtp, dt_module_t *dmp,
1045     const char *name, const dtrace_typeinfo_t *tip)
1046 {
1047 	dtrace_syminfo_t *sip;
1048 	dt_ident_t *idp;
1049 	uint_t id;
1050 
1051 	if (dmp->dm_extern == NULL && (dmp->dm_extern = dt_idhash_create(
1052 	    "extern", NULL, dmp->dm_nsymelems, UINT_MAX)) == NULL) {
1053 		(void) dt_set_errno(dtp, EDT_NOMEM);
1054 		return (NULL);
1055 	}
1056 
1057 	if (dt_idhash_nextid(dmp->dm_extern, &id) == -1) {
1058 		(void) dt_set_errno(dtp, EDT_SYMOFLOW);
1059 		return (NULL);
1060 	}
1061 
1062 	if ((sip = malloc(sizeof (dtrace_syminfo_t))) == NULL) {
1063 		(void) dt_set_errno(dtp, EDT_NOMEM);
1064 		return (NULL);
1065 	}
1066 
1067 	idp = dt_idhash_insert(dmp->dm_extern, name, DT_IDENT_SYMBOL, 0, id,
1068 	    _dtrace_symattr, 0, &dt_idops_thaw, NULL, dtp->dt_gen);
1069 
1070 	if (idp == NULL) {
1071 		(void) dt_set_errno(dtp, EDT_NOMEM);
1072 		free(sip);
1073 		return (NULL);
1074 	}
1075 
1076 	sip->dts_object = dmp->dm_name;
1077 	sip->dts_name = idp->di_name;
1078 	sip->dts_id = idp->di_id;
1079 
1080 	idp->di_data = sip;
1081 	idp->di_ctfp = tip->dtt_ctfp;
1082 	idp->di_type = tip->dtt_type;
1083 
1084 	return (idp);
1085 }
1086 
1087 const char *
1088 dt_module_modelname(dt_module_t *dmp)
1089 {
1090 	if (dmp->dm_ops == &dt_modops_64)
1091 		return ("64-bit");
1092 	else
1093 		return ("32-bit");
1094 }
1095 
1096 /* ARGSUSED */
1097 int
1098 dt_module_getlibid(dtrace_hdl_t *dtp, dt_module_t *dmp, const ctf_file_t *fp)
1099 {
1100 	int i;
1101 
1102 	for (i = 0; i < dmp->dm_nctflibs; i++) {
1103 		if (dmp->dm_libctfp[i] == fp)
1104 			return (i);
1105 	}
1106 
1107 	return (-1);
1108 }
1109 
1110 /* ARGSUSED */
1111 ctf_file_t *
1112 dt_module_getctflib(dtrace_hdl_t *dtp, dt_module_t *dmp, const char *name)
1113 {
1114 	int i;
1115 
1116 	for (i = 0; i < dmp->dm_nctflibs; i++) {
1117 		if (strcmp(dmp->dm_libctfn[i], name) == 0)
1118 			return (dmp->dm_libctfp[i]);
1119 	}
1120 
1121 	return (NULL);
1122 }
1123 
1124 /*
1125  * Update our module cache by adding an entry for the specified module 'name'.
1126  * We create the dt_module_t and populate it using /system/object/<name>/.
1127  *
1128  * On FreeBSD, the module name is passed as the full module file name,
1129  * including the path.
1130  */
1131 static void
1132 dt_module_update(dtrace_hdl_t *dtp, struct kld_file_stat *k_stat)
1133 {
1134 	char fname[MAXPATHLEN];
1135 	struct stat64 st;
1136 	int fd, err, bits;
1137 	struct module_stat ms;
1138 	dt_kmodule_t *dkmp;
1139 	uint_t h;
1140 	int modid;
1141 	dt_module_t *dmp;
1142 	const char *s;
1143 	size_t shstrs;
1144 	GElf_Shdr sh;
1145 	Elf_Data *dp;
1146 	Elf_Scn *sp;
1147 	GElf_Ehdr ehdr;
1148 	GElf_Phdr ph;
1149 	char name[MAXPATHLEN];
1150 	uintptr_t mapbase, alignmask;
1151 	int i = 0;
1152 	int is_elf_obj;
1153 
1154 	(void) strlcpy(name, k_stat->name, sizeof(name));
1155 	(void) strlcpy(fname, k_stat->pathname, sizeof(fname));
1156 
1157 	if ((fd = open(fname, O_RDONLY)) == -1 || fstat64(fd, &st) == -1 ||
1158 	    (dmp = dt_module_create(dtp, name)) == NULL) {
1159 		dt_dprintf("failed to open %s: %s\n", fname, strerror(errno));
1160 		(void) close(fd);
1161 		return;
1162 	}
1163 
1164 	(void) strlcpy(dmp->dm_file, fname, sizeof(dmp->dm_file));
1165 	dmp->dm_modid = k_stat->id;
1166 
1167 	/*
1168 	 * Since the module can unload out from under us (and /system/object
1169 	 * will return ENOENT), tell libelf to cook the entire file now and
1170 	 * then close the underlying file descriptor immediately.  If this
1171 	 * succeeds, we know that we can continue safely using dmp->dm_elf.
1172 	 */
1173 	dmp->dm_elf = elf_begin(fd, ELF_C_READ, NULL);
1174 	err = elf_cntl(dmp->dm_elf, ELF_C_FDREAD);
1175 	(void) close(fd);
1176 
1177 	if (dmp->dm_elf == NULL || err == -1 ||
1178 	    elf_getshdrstrndx(dmp->dm_elf, &shstrs) == -1) {
1179 		dt_dprintf("failed to load %s: %s\n",
1180 		    fname, elf_errmsg(elf_errno()));
1181 		dt_module_destroy(dtp, dmp);
1182 		return;
1183 	}
1184 
1185 	switch (gelf_getclass(dmp->dm_elf)) {
1186 	case ELFCLASS32:
1187 		dmp->dm_ops = &dt_modops_32;
1188 		bits = 32;
1189 		break;
1190 	case ELFCLASS64:
1191 		dmp->dm_ops = &dt_modops_64;
1192 		bits = 64;
1193 		break;
1194 	default:
1195 		dt_dprintf("failed to load %s: unknown ELF class\n", fname);
1196 		dt_module_destroy(dtp, dmp);
1197 		return;
1198 	}
1199 	mapbase = (uintptr_t)k_stat->address;
1200 	gelf_getehdr(dmp->dm_elf, &ehdr);
1201 	is_elf_obj = (ehdr.e_type == ET_REL);
1202 	if (is_elf_obj) {
1203 		dmp->dm_sec_offsets =
1204 		    malloc(ehdr.e_shnum * sizeof(*dmp->dm_sec_offsets));
1205 		if (dmp->dm_sec_offsets == NULL) {
1206 			dt_dprintf("failed to allocate memory\n");
1207 			dt_module_destroy(dtp, dmp);
1208 			return;
1209 		}
1210 	}
1211 	/*
1212 	 * Iterate over the section headers locating various sections of
1213 	 * interest and use their attributes to flesh out the dt_module_t.
1214 	 */
1215 	for (sp = NULL; (sp = elf_nextscn(dmp->dm_elf, sp)) != NULL; ) {
1216 		if (gelf_getshdr(sp, &sh) == NULL || sh.sh_type == SHT_NULL ||
1217 		    (s = elf_strptr(dmp->dm_elf, shstrs, sh.sh_name)) == NULL)
1218 			continue; /* skip any malformed sections */
1219 		if (sh.sh_size == 0)
1220 			continue;
1221 		if (sh.sh_type == SHT_PROGBITS || sh.sh_type == SHT_NOBITS) {
1222 			alignmask = sh.sh_addralign - 1;
1223 			mapbase += alignmask;
1224 			mapbase &= ~alignmask;
1225 			sh.sh_addr = mapbase;
1226 			if (is_elf_obj)
1227 				dmp->dm_sec_offsets[elf_ndxscn(sp)] = sh.sh_addr;
1228 			mapbase += sh.sh_size;
1229 		}
1230 		if (strcmp(s, ".text") == 0) {
1231 			dmp->dm_text_size = sh.sh_size;
1232 			dmp->dm_text_va = sh.sh_addr;
1233 		} else if (strcmp(s, ".data") == 0) {
1234 			dmp->dm_data_size = sh.sh_size;
1235 			dmp->dm_data_va = sh.sh_addr;
1236 		} else if (strcmp(s, ".bss") == 0) {
1237 			dmp->dm_bss_size = sh.sh_size;
1238 			dmp->dm_bss_va = sh.sh_addr;
1239 		} else if (strcmp(s, ".info") == 0 &&
1240 		    (dp = elf_getdata(sp, NULL)) != NULL) {
1241 			bcopy(dp->d_buf, &dmp->dm_info,
1242 			    MIN(sh.sh_size, sizeof (dmp->dm_info)));
1243 		}
1244 	}
1245 
1246 	dmp->dm_flags |= DT_DM_KERNEL;
1247 	/*
1248 	 * Include .rodata and special sections into .text.
1249 	 * This depends on default section layout produced by GNU ld
1250 	 * for ELF objects and libraries:
1251 	 * [Text][R/O data][R/W data][Dynamic][BSS][Non loadable]
1252 	 */
1253 	dmp->dm_text_size = dmp->dm_data_va - dmp->dm_text_va;
1254 #if defined(__i386__)
1255 	/*
1256 	 * Find the first load section and figure out the relocation
1257 	 * offset for the symbols. The kernel module will not need
1258 	 * relocation, but the kernel linker modules will.
1259 	 */
1260 	for (i = 0; gelf_getphdr(dmp->dm_elf, i, &ph) != NULL; i++) {
1261 		if (ph.p_type == PT_LOAD) {
1262 			dmp->dm_reloc_offset = k_stat->address - ph.p_vaddr;
1263 			break;
1264 		}
1265 	}
1266 #endif
1267 
1268 	if (dmp->dm_info.objfs_info_primary)
1269 		dmp->dm_flags |= DT_DM_PRIMARY;
1270 
1271 	ms.version = sizeof(ms);
1272 	for (modid = kldfirstmod(k_stat->id); modid > 0;
1273 	    modid = modnext(modid)) {
1274 		if (modstat(modid, &ms) != 0) {
1275 			dt_dprintf("modstat failed for id %d in %s: %s\n",
1276 			    modid, k_stat->name, strerror(errno));
1277 			continue;
1278 		}
1279 		if (dt_kmodule_lookup(dtp, ms.name) != NULL)
1280 			continue;
1281 
1282 		dkmp = malloc(sizeof (*dkmp));
1283 		if (dkmp == NULL) {
1284 			dt_dprintf("failed to allocate memory\n");
1285 			dt_module_destroy(dtp, dmp);
1286 			return;
1287 		}
1288 
1289 		h = dt_strtab_hash(ms.name, NULL) % dtp->dt_modbuckets;
1290 		dkmp->dkm_next = dtp->dt_kmods[h];
1291 		dkmp->dkm_name = strdup(ms.name);
1292 		dkmp->dkm_module = dmp;
1293 		dtp->dt_kmods[h] = dkmp;
1294 	}
1295 
1296 	dt_dprintf("opened %d-bit module %s (%s) [%d]\n",
1297 	    bits, dmp->dm_name, dmp->dm_file, dmp->dm_modid);
1298 }
1299 
1300 /*
1301  * Unload all the loaded modules and then refresh the module cache with the
1302  * latest list of loaded modules and their address ranges.
1303  */
1304 void
1305 dtrace_update(dtrace_hdl_t *dtp)
1306 {
1307 	dt_module_t *dmp;
1308 	DIR *dirp;
1309 #if defined(__FreeBSD__)
1310 	int fileid;
1311 #endif
1312 
1313 	for (dmp = dt_list_next(&dtp->dt_modlist);
1314 	    dmp != NULL; dmp = dt_list_next(dmp))
1315 		dt_module_unload(dtp, dmp);
1316 
1317 #ifdef illumos
1318 	/*
1319 	 * Open /system/object and attempt to create a libdtrace module for
1320 	 * each kernel module that is loaded on the current system.
1321 	 */
1322 	if (!(dtp->dt_oflags & DTRACE_O_NOSYS) &&
1323 	    (dirp = opendir(OBJFS_ROOT)) != NULL) {
1324 		struct dirent *dp;
1325 
1326 		while ((dp = readdir(dirp)) != NULL) {
1327 			if (dp->d_name[0] != '.')
1328 				dt_module_update(dtp, dp->d_name);
1329 		}
1330 
1331 		(void) closedir(dirp);
1332 	}
1333 #elif defined(__FreeBSD__)
1334 	/*
1335 	 * Use FreeBSD's kernel loader interface to discover what kernel
1336 	 * modules are loaded and create a libdtrace module for each one.
1337 	 */
1338 	for (fileid = kldnext(0); fileid > 0; fileid = kldnext(fileid)) {
1339 		struct kld_file_stat k_stat;
1340 		k_stat.version = sizeof(k_stat);
1341 		if (kldstat(fileid, &k_stat) == 0)
1342 			dt_module_update(dtp, &k_stat);
1343 	}
1344 #endif
1345 
1346 	/*
1347 	 * Look up all the macro identifiers and set di_id to the latest value.
1348 	 * This code collaborates with dt_lex.l on the use of di_id.  We will
1349 	 * need to implement something fancier if we need to support non-ints.
1350 	 */
1351 	dt_idhash_lookup(dtp->dt_macros, "egid")->di_id = getegid();
1352 	dt_idhash_lookup(dtp->dt_macros, "euid")->di_id = geteuid();
1353 	dt_idhash_lookup(dtp->dt_macros, "gid")->di_id = getgid();
1354 	dt_idhash_lookup(dtp->dt_macros, "pid")->di_id = getpid();
1355 	dt_idhash_lookup(dtp->dt_macros, "pgid")->di_id = getpgid(0);
1356 	dt_idhash_lookup(dtp->dt_macros, "ppid")->di_id = getppid();
1357 #ifdef illumos
1358 	dt_idhash_lookup(dtp->dt_macros, "projid")->di_id = getprojid();
1359 #endif
1360 	dt_idhash_lookup(dtp->dt_macros, "sid")->di_id = getsid(0);
1361 #ifdef illumos
1362 	dt_idhash_lookup(dtp->dt_macros, "taskid")->di_id = gettaskid();
1363 #endif
1364 	dt_idhash_lookup(dtp->dt_macros, "uid")->di_id = getuid();
1365 
1366 	/*
1367 	 * Cache the pointers to the modules representing the base executable
1368 	 * and the run-time linker in the dtrace client handle. Note that on
1369 	 * x86 krtld is folded into unix, so if we don't find it, use unix
1370 	 * instead.
1371 	 */
1372 	dtp->dt_exec = dt_module_lookup_by_name(dtp, "genunix");
1373 	dtp->dt_rtld = dt_module_lookup_by_name(dtp, "krtld");
1374 	if (dtp->dt_rtld == NULL)
1375 		dtp->dt_rtld = dt_module_lookup_by_name(dtp, "unix");
1376 
1377 	/*
1378 	 * If this is the first time we are initializing the module list,
1379 	 * remove the module for genunix from the module list and then move it
1380 	 * to the front of the module list.  We do this so that type and symbol
1381 	 * queries encounter genunix and thereby optimize for the common case
1382 	 * in dtrace_lookup_by_name() and dtrace_lookup_by_type(), below.
1383 	 */
1384 	if (dtp->dt_exec != NULL &&
1385 	    dtp->dt_cdefs == NULL && dtp->dt_ddefs == NULL) {
1386 		dt_list_delete(&dtp->dt_modlist, dtp->dt_exec);
1387 		dt_list_prepend(&dtp->dt_modlist, dtp->dt_exec);
1388 	}
1389 }
1390 
1391 static dt_module_t *
1392 dt_module_from_object(dtrace_hdl_t *dtp, const char *object)
1393 {
1394 	int err = EDT_NOMOD;
1395 	dt_module_t *dmp;
1396 
1397 	switch ((uintptr_t)object) {
1398 	case (uintptr_t)DTRACE_OBJ_EXEC:
1399 		dmp = dtp->dt_exec;
1400 		break;
1401 	case (uintptr_t)DTRACE_OBJ_RTLD:
1402 		dmp = dtp->dt_rtld;
1403 		break;
1404 	case (uintptr_t)DTRACE_OBJ_CDEFS:
1405 		dmp = dtp->dt_cdefs;
1406 		break;
1407 	case (uintptr_t)DTRACE_OBJ_DDEFS:
1408 		dmp = dtp->dt_ddefs;
1409 		break;
1410 	default:
1411 		dmp = dt_module_create(dtp, object);
1412 		err = EDT_NOMEM;
1413 	}
1414 
1415 	if (dmp == NULL)
1416 		(void) dt_set_errno(dtp, err);
1417 
1418 	return (dmp);
1419 }
1420 
1421 /*
1422  * Exported interface to look up a symbol by name.  We return the GElf_Sym and
1423  * complete symbol information for the matching symbol.
1424  */
1425 int
1426 dtrace_lookup_by_name(dtrace_hdl_t *dtp, const char *object, const char *name,
1427     GElf_Sym *symp, dtrace_syminfo_t *sip)
1428 {
1429 	dt_module_t *dmp;
1430 	dt_ident_t *idp;
1431 	uint_t n, id;
1432 	GElf_Sym sym;
1433 
1434 	uint_t mask = 0; /* mask of dt_module flags to match */
1435 	uint_t bits = 0; /* flag bits that must be present */
1436 
1437 	if (object != DTRACE_OBJ_EVERY &&
1438 	    object != DTRACE_OBJ_KMODS &&
1439 	    object != DTRACE_OBJ_UMODS) {
1440 		if ((dmp = dt_module_from_object(dtp, object)) == NULL)
1441 			return (-1); /* dt_errno is set for us */
1442 
1443 		if (dt_module_load(dtp, dmp) == -1)
1444 			return (-1); /* dt_errno is set for us */
1445 		n = 1;
1446 
1447 	} else {
1448 		if (object == DTRACE_OBJ_KMODS)
1449 			mask = bits = DT_DM_KERNEL;
1450 		else if (object == DTRACE_OBJ_UMODS)
1451 			mask = DT_DM_KERNEL;
1452 
1453 		dmp = dt_list_next(&dtp->dt_modlist);
1454 		n = dtp->dt_nmods;
1455 	}
1456 
1457 	if (symp == NULL)
1458 		symp = &sym;
1459 
1460 	for (; n > 0; n--, dmp = dt_list_next(dmp)) {
1461 		if ((dmp->dm_flags & mask) != bits)
1462 			continue; /* failed to match required attributes */
1463 
1464 		if (dt_module_load(dtp, dmp) == -1)
1465 			continue; /* failed to load symbol table */
1466 
1467 		if (dmp->dm_ops->do_symname(dmp, name, symp, &id) != NULL) {
1468 			if (sip != NULL) {
1469 				sip->dts_object = dmp->dm_name;
1470 				sip->dts_name = (const char *)
1471 				    dmp->dm_strtab.cts_data + symp->st_name;
1472 				sip->dts_id = id;
1473 			}
1474 			return (0);
1475 		}
1476 
1477 		if (dmp->dm_extern != NULL &&
1478 		    (idp = dt_idhash_lookup(dmp->dm_extern, name)) != NULL) {
1479 			if (symp != &sym) {
1480 				symp->st_name = (uintptr_t)idp->di_name;
1481 				symp->st_info =
1482 				    GELF_ST_INFO(STB_GLOBAL, STT_NOTYPE);
1483 				symp->st_other = 0;
1484 				symp->st_shndx = SHN_UNDEF;
1485 				symp->st_value = 0;
1486 				symp->st_size =
1487 				    ctf_type_size(idp->di_ctfp, idp->di_type);
1488 			}
1489 
1490 			if (sip != NULL) {
1491 				sip->dts_object = dmp->dm_name;
1492 				sip->dts_name = idp->di_name;
1493 				sip->dts_id = idp->di_id;
1494 			}
1495 
1496 			return (0);
1497 		}
1498 	}
1499 
1500 	return (dt_set_errno(dtp, EDT_NOSYM));
1501 }
1502 
1503 /*
1504  * Exported interface to look up a symbol by address.  We return the GElf_Sym
1505  * and complete symbol information for the matching symbol.
1506  */
1507 int
1508 dtrace_lookup_by_addr(dtrace_hdl_t *dtp, GElf_Addr addr,
1509     GElf_Sym *symp, dtrace_syminfo_t *sip)
1510 {
1511 	dt_module_t *dmp;
1512 	uint_t id;
1513 	const dtrace_vector_t *v = dtp->dt_vector;
1514 
1515 	if (v != NULL)
1516 		return (v->dtv_lookup_by_addr(dtp->dt_varg, addr, symp, sip));
1517 
1518 	for (dmp = dt_list_next(&dtp->dt_modlist); dmp != NULL;
1519 	    dmp = dt_list_next(dmp)) {
1520 		if (addr - dmp->dm_text_va < dmp->dm_text_size ||
1521 		    addr - dmp->dm_data_va < dmp->dm_data_size ||
1522 		    addr - dmp->dm_bss_va < dmp->dm_bss_size)
1523 			break;
1524 	}
1525 
1526 	if (dmp == NULL)
1527 		return (dt_set_errno(dtp, EDT_NOSYMADDR));
1528 
1529 	if (dt_module_load(dtp, dmp) == -1)
1530 		return (-1); /* dt_errno is set for us */
1531 
1532 	if (symp != NULL) {
1533 		if (dmp->dm_ops->do_symaddr(dmp, addr, symp, &id) == NULL)
1534 			return (dt_set_errno(dtp, EDT_NOSYMADDR));
1535 	}
1536 
1537 	if (sip != NULL) {
1538 		sip->dts_object = dmp->dm_name;
1539 
1540 		if (symp != NULL) {
1541 			sip->dts_name = (const char *)
1542 			    dmp->dm_strtab.cts_data + symp->st_name;
1543 			sip->dts_id = id;
1544 		} else {
1545 			sip->dts_name = NULL;
1546 			sip->dts_id = 0;
1547 		}
1548 	}
1549 
1550 	return (0);
1551 }
1552 
1553 int
1554 dtrace_lookup_by_type(dtrace_hdl_t *dtp, const char *object, const char *name,
1555     dtrace_typeinfo_t *tip)
1556 {
1557 	dtrace_typeinfo_t ti;
1558 	dt_module_t *dmp;
1559 	int found = 0;
1560 	ctf_id_t id;
1561 	uint_t n, i;
1562 	int justone;
1563 	ctf_file_t *fp;
1564 	char *buf, *p, *q;
1565 
1566 	uint_t mask = 0; /* mask of dt_module flags to match */
1567 	uint_t bits = 0; /* flag bits that must be present */
1568 
1569 	if (object != DTRACE_OBJ_EVERY &&
1570 	    object != DTRACE_OBJ_KMODS &&
1571 	    object != DTRACE_OBJ_UMODS) {
1572 		if ((dmp = dt_module_from_object(dtp, object)) == NULL)
1573 			return (-1); /* dt_errno is set for us */
1574 
1575 		if (dt_module_load(dtp, dmp) == -1)
1576 			return (-1); /* dt_errno is set for us */
1577 		n = 1;
1578 		justone = 1;
1579 	} else {
1580 		if (object == DTRACE_OBJ_KMODS)
1581 			mask = bits = DT_DM_KERNEL;
1582 		else if (object == DTRACE_OBJ_UMODS)
1583 			mask = DT_DM_KERNEL;
1584 
1585 		dmp = dt_list_next(&dtp->dt_modlist);
1586 		n = dtp->dt_nmods;
1587 		justone = 0;
1588 	}
1589 
1590 	if (tip == NULL)
1591 		tip = &ti;
1592 
1593 	for (; n > 0; n--, dmp = dt_list_next(dmp)) {
1594 		if ((dmp->dm_flags & mask) != bits)
1595 			continue; /* failed to match required attributes */
1596 
1597 		/*
1598 		 * If we can't load the CTF container, continue on to the next
1599 		 * module.  If our search was scoped to only one module then
1600 		 * return immediately leaving dt_errno unmodified.
1601 		 */
1602 		if (dt_module_hasctf(dtp, dmp) == 0) {
1603 			if (justone)
1604 				return (-1);
1605 			continue;
1606 		}
1607 
1608 		/*
1609 		 * Look up the type in the module's CTF container.  If our
1610 		 * match is a forward declaration tag, save this choice in
1611 		 * 'tip' and keep going in the hope that we will locate the
1612 		 * underlying structure definition.  Otherwise just return.
1613 		 */
1614 		if (dmp->dm_pid == 0) {
1615 			id = ctf_lookup_by_name(dmp->dm_ctfp, name);
1616 			fp = dmp->dm_ctfp;
1617 		} else {
1618 			if ((p = strchr(name, '`')) != NULL) {
1619 				buf = strdup(name);
1620 				if (buf == NULL)
1621 					return (dt_set_errno(dtp, EDT_NOMEM));
1622 				p = strchr(buf, '`');
1623 				if ((q = strchr(p + 1, '`')) != NULL)
1624 					p = q;
1625 				*p = '\0';
1626 				fp = dt_module_getctflib(dtp, dmp, buf);
1627 				if (fp == NULL || (id = ctf_lookup_by_name(fp,
1628 				    p + 1)) == CTF_ERR)
1629 					id = CTF_ERR;
1630 				free(buf);
1631 			} else {
1632 				for (i = 0; i < dmp->dm_nctflibs; i++) {
1633 					fp = dmp->dm_libctfp[i];
1634 					id = ctf_lookup_by_name(fp, name);
1635 					if (id != CTF_ERR)
1636 						break;
1637 				}
1638 			}
1639 		}
1640 		if (id != CTF_ERR) {
1641 			tip->dtt_object = dmp->dm_name;
1642 			tip->dtt_ctfp = fp;
1643 			tip->dtt_type = id;
1644 			if (ctf_type_kind(fp, ctf_type_resolve(fp, id)) !=
1645 			    CTF_K_FORWARD)
1646 				return (0);
1647 
1648 			found++;
1649 		}
1650 	}
1651 
1652 	if (found == 0)
1653 		return (dt_set_errno(dtp, EDT_NOTYPE));
1654 
1655 	return (0);
1656 }
1657 
1658 int
1659 dtrace_symbol_type(dtrace_hdl_t *dtp, const GElf_Sym *symp,
1660     const dtrace_syminfo_t *sip, dtrace_typeinfo_t *tip)
1661 {
1662 	dt_module_t *dmp;
1663 
1664 	tip->dtt_object = NULL;
1665 	tip->dtt_ctfp = NULL;
1666 	tip->dtt_type = CTF_ERR;
1667 	tip->dtt_flags = 0;
1668 
1669 	if ((dmp = dt_module_lookup_by_name(dtp, sip->dts_object)) == NULL)
1670 		return (dt_set_errno(dtp, EDT_NOMOD));
1671 
1672 	if (symp->st_shndx == SHN_UNDEF && dmp->dm_extern != NULL) {
1673 		dt_ident_t *idp =
1674 		    dt_idhash_lookup(dmp->dm_extern, sip->dts_name);
1675 
1676 		if (idp == NULL)
1677 			return (dt_set_errno(dtp, EDT_NOSYM));
1678 
1679 		tip->dtt_ctfp = idp->di_ctfp;
1680 		tip->dtt_type = idp->di_type;
1681 
1682 	} else if (GELF_ST_TYPE(symp->st_info) != STT_FUNC) {
1683 		if (dt_module_getctf(dtp, dmp) == NULL)
1684 			return (-1); /* errno is set for us */
1685 
1686 		tip->dtt_ctfp = dmp->dm_ctfp;
1687 		tip->dtt_type = ctf_lookup_by_symbol(dmp->dm_ctfp, sip->dts_id);
1688 
1689 		if (tip->dtt_type == CTF_ERR) {
1690 			dtp->dt_ctferr = ctf_errno(tip->dtt_ctfp);
1691 			return (dt_set_errno(dtp, EDT_CTF));
1692 		}
1693 
1694 	} else {
1695 		tip->dtt_ctfp = DT_FPTR_CTFP(dtp);
1696 		tip->dtt_type = DT_FPTR_TYPE(dtp);
1697 	}
1698 
1699 	tip->dtt_object = dmp->dm_name;
1700 	return (0);
1701 }
1702 
1703 static dtrace_objinfo_t *
1704 dt_module_info(const dt_module_t *dmp, dtrace_objinfo_t *dto)
1705 {
1706 	dto->dto_name = dmp->dm_name;
1707 	dto->dto_file = dmp->dm_file;
1708 	dto->dto_id = dmp->dm_modid;
1709 	dto->dto_flags = 0;
1710 
1711 	if (dmp->dm_flags & DT_DM_KERNEL)
1712 		dto->dto_flags |= DTRACE_OBJ_F_KERNEL;
1713 	if (dmp->dm_flags & DT_DM_PRIMARY)
1714 		dto->dto_flags |= DTRACE_OBJ_F_PRIMARY;
1715 
1716 	dto->dto_text_va = dmp->dm_text_va;
1717 	dto->dto_text_size = dmp->dm_text_size;
1718 	dto->dto_data_va = dmp->dm_data_va;
1719 	dto->dto_data_size = dmp->dm_data_size;
1720 	dto->dto_bss_va = dmp->dm_bss_va;
1721 	dto->dto_bss_size = dmp->dm_bss_size;
1722 
1723 	return (dto);
1724 }
1725 
1726 int
1727 dtrace_object_iter(dtrace_hdl_t *dtp, dtrace_obj_f *func, void *data)
1728 {
1729 	const dt_module_t *dmp = dt_list_next(&dtp->dt_modlist);
1730 	dtrace_objinfo_t dto;
1731 	int rv;
1732 
1733 	for (; dmp != NULL; dmp = dt_list_next(dmp)) {
1734 		if ((rv = (*func)(dtp, dt_module_info(dmp, &dto), data)) != 0)
1735 			return (rv);
1736 	}
1737 
1738 	return (0);
1739 }
1740 
1741 int
1742 dtrace_object_info(dtrace_hdl_t *dtp, const char *object, dtrace_objinfo_t *dto)
1743 {
1744 	dt_module_t *dmp;
1745 
1746 	if (object == DTRACE_OBJ_EVERY || object == DTRACE_OBJ_KMODS ||
1747 	    object == DTRACE_OBJ_UMODS || dto == NULL)
1748 		return (dt_set_errno(dtp, EINVAL));
1749 
1750 	if ((dmp = dt_module_from_object(dtp, object)) == NULL)
1751 		return (-1); /* dt_errno is set for us */
1752 
1753 	if (dt_module_load(dtp, dmp) == -1)
1754 		return (-1); /* dt_errno is set for us */
1755 
1756 	(void) dt_module_info(dmp, dto);
1757 	return (0);
1758 }
1759