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