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