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.
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.
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]
23 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
26 #include <sys/types.h>
27 #include <sys/modctl.h>
29 #include <sys/kobj_impl.h>
30 #include <sys/sysmacros.h>
44 #include <dt_strtab.h>
45 #include <dt_module.h>
48 static const char *dt_module_strtab; /* active strtab for qsort callbacks */
51 dt_module_symhash_insert(dt_module_t *dmp, const char *name, uint_t id)
53 dt_sym_t *dsp = &dmp->dm_symchains[dmp->dm_symfree];
56 assert(dmp->dm_symfree < dmp->dm_nsymelems + 1);
59 h = dt_strtab_hash(name, NULL) % dmp->dm_nsymbuckets;
60 dsp->ds_next = dmp->dm_symbuckets[h];
61 dmp->dm_symbuckets[h] = dmp->dm_symfree++;
65 dt_module_syminit32(dt_module_t *dmp)
67 #if STT_NUM != (STT_TLS + 1)
68 #error "STT_NUM has grown. update dt_module_syminit32()"
71 const Elf32_Sym *sym = dmp->dm_symtab.cts_data;
72 const char *base = dmp->dm_strtab.cts_data;
73 size_t ss_size = dmp->dm_strtab.cts_size;
74 uint_t i, n = dmp->dm_nsymelems;
77 for (i = 0; i < n; i++, sym++) {
78 const char *name = base + sym->st_name;
79 uchar_t type = ELF32_ST_TYPE(sym->st_info);
81 if (type >= STT_NUM || type == STT_SECTION)
82 continue; /* skip sections and unknown types */
84 if (sym->st_name == 0 || sym->st_name >= ss_size)
85 continue; /* skip null or invalid names */
87 if (sym->st_value != 0 &&
88 (ELF32_ST_BIND(sym->st_info) != STB_LOCAL || sym->st_size))
89 asrsv++; /* reserve space in the address map */
91 dt_module_symhash_insert(dmp, name, i);
98 dt_module_syminit64(dt_module_t *dmp)
100 #if STT_NUM != (STT_TLS + 1)
101 #error "STT_NUM has grown. update dt_module_syminit64()"
104 const Elf64_Sym *sym = dmp->dm_symtab.cts_data;
105 const char *base = dmp->dm_strtab.cts_data;
106 size_t ss_size = dmp->dm_strtab.cts_size;
107 uint_t i, n = dmp->dm_nsymelems;
110 for (i = 0; i < n; i++, sym++) {
111 const char *name = base + sym->st_name;
112 uchar_t type = ELF64_ST_TYPE(sym->st_info);
114 if (type >= STT_NUM || type == STT_SECTION)
115 continue; /* skip sections and unknown types */
117 if (sym->st_name == 0 || sym->st_name >= ss_size)
118 continue; /* skip null or invalid names */
120 if (sym->st_value != 0 &&
121 (ELF64_ST_BIND(sym->st_info) != STB_LOCAL || sym->st_size))
122 asrsv++; /* reserve space in the address map */
124 dt_module_symhash_insert(dmp, name, i);
131 * Sort comparison function for 32-bit symbol address-to-name lookups. We sort
132 * symbols by value. If values are equal, we prefer the symbol that is
133 * non-zero sized, typed, not weak, or lexically first, in that order.
136 dt_module_symcomp32(const void *lp, const void *rp)
138 Elf32_Sym *lhs = *((Elf32_Sym **)lp);
139 Elf32_Sym *rhs = *((Elf32_Sym **)rp);
141 if (lhs->st_value != rhs->st_value)
142 return (lhs->st_value > rhs->st_value ? 1 : -1);
144 if ((lhs->st_size == 0) != (rhs->st_size == 0))
145 return (lhs->st_size == 0 ? 1 : -1);
147 if ((ELF32_ST_TYPE(lhs->st_info) == STT_NOTYPE) !=
148 (ELF32_ST_TYPE(rhs->st_info) == STT_NOTYPE))
149 return (ELF32_ST_TYPE(lhs->st_info) == STT_NOTYPE ? 1 : -1);
151 if ((ELF32_ST_BIND(lhs->st_info) == STB_WEAK) !=
152 (ELF32_ST_BIND(rhs->st_info) == STB_WEAK))
153 return (ELF32_ST_BIND(lhs->st_info) == STB_WEAK ? 1 : -1);
155 return (strcmp(dt_module_strtab + lhs->st_name,
156 dt_module_strtab + rhs->st_name));
160 * Sort comparison function for 64-bit symbol address-to-name lookups. We sort
161 * symbols by value. If values are equal, we prefer the symbol that is
162 * non-zero sized, typed, not weak, or lexically first, in that order.
165 dt_module_symcomp64(const void *lp, const void *rp)
167 Elf64_Sym *lhs = *((Elf64_Sym **)lp);
168 Elf64_Sym *rhs = *((Elf64_Sym **)rp);
170 if (lhs->st_value != rhs->st_value)
171 return (lhs->st_value > rhs->st_value ? 1 : -1);
173 if ((lhs->st_size == 0) != (rhs->st_size == 0))
174 return (lhs->st_size == 0 ? 1 : -1);
176 if ((ELF64_ST_TYPE(lhs->st_info) == STT_NOTYPE) !=
177 (ELF64_ST_TYPE(rhs->st_info) == STT_NOTYPE))
178 return (ELF64_ST_TYPE(lhs->st_info) == STT_NOTYPE ? 1 : -1);
180 if ((ELF64_ST_BIND(lhs->st_info) == STB_WEAK) !=
181 (ELF64_ST_BIND(rhs->st_info) == STB_WEAK))
182 return (ELF64_ST_BIND(lhs->st_info) == STB_WEAK ? 1 : -1);
184 return (strcmp(dt_module_strtab + lhs->st_name,
185 dt_module_strtab + rhs->st_name));
189 dt_module_symsort32(dt_module_t *dmp)
191 Elf32_Sym *symtab = (Elf32_Sym *)dmp->dm_symtab.cts_data;
192 Elf32_Sym **sympp = (Elf32_Sym **)dmp->dm_asmap;
193 const dt_sym_t *dsp = dmp->dm_symchains + 1;
194 uint_t i, n = dmp->dm_symfree;
196 for (i = 1; i < n; i++, dsp++) {
197 Elf32_Sym *sym = symtab + dsp->ds_symid;
198 if (sym->st_value != 0 &&
199 (ELF32_ST_BIND(sym->st_info) != STB_LOCAL || sym->st_size))
203 dmp->dm_aslen = (uint_t)(sympp - (Elf32_Sym **)dmp->dm_asmap);
204 assert(dmp->dm_aslen <= dmp->dm_asrsv);
206 dt_module_strtab = dmp->dm_strtab.cts_data;
207 qsort(dmp->dm_asmap, dmp->dm_aslen,
208 sizeof (Elf32_Sym *), dt_module_symcomp32);
209 dt_module_strtab = NULL;
213 dt_module_symsort64(dt_module_t *dmp)
215 Elf64_Sym *symtab = (Elf64_Sym *)dmp->dm_symtab.cts_data;
216 Elf64_Sym **sympp = (Elf64_Sym **)dmp->dm_asmap;
217 const dt_sym_t *dsp = dmp->dm_symchains + 1;
218 uint_t i, n = dmp->dm_symfree;
220 for (i = 1; i < n; i++, dsp++) {
221 Elf64_Sym *sym = symtab + dsp->ds_symid;
222 if (sym->st_value != 0 &&
223 (ELF64_ST_BIND(sym->st_info) != STB_LOCAL || sym->st_size))
227 dmp->dm_aslen = (uint_t)(sympp - (Elf64_Sym **)dmp->dm_asmap);
228 assert(dmp->dm_aslen <= dmp->dm_asrsv);
230 dt_module_strtab = dmp->dm_strtab.cts_data;
231 qsort(dmp->dm_asmap, dmp->dm_aslen,
232 sizeof (Elf64_Sym *), dt_module_symcomp64);
233 dt_module_strtab = NULL;
237 dt_module_symgelf32(const Elf32_Sym *src, GElf_Sym *dst)
240 dst->st_name = src->st_name;
241 dst->st_info = src->st_info;
242 dst->st_other = src->st_other;
243 dst->st_shndx = src->st_shndx;
244 dst->st_value = src->st_value;
245 dst->st_size = src->st_size;
252 dt_module_symgelf64(const Elf64_Sym *src, GElf_Sym *dst)
255 bcopy(src, dst, sizeof (GElf_Sym));
261 dt_module_symname32(dt_module_t *dmp, const char *name,
262 GElf_Sym *symp, uint_t *idp)
264 const Elf32_Sym *symtab = dmp->dm_symtab.cts_data;
265 const char *strtab = dmp->dm_strtab.cts_data;
267 const Elf32_Sym *sym;
271 if (dmp->dm_nsymelems == 0)
274 h = dt_strtab_hash(name, NULL) % dmp->dm_nsymbuckets;
276 for (i = dmp->dm_symbuckets[h]; i != 0; i = dsp->ds_next) {
277 dsp = &dmp->dm_symchains[i];
278 sym = symtab + dsp->ds_symid;
280 if (strcmp(name, strtab + sym->st_name) == 0) {
282 *idp = dsp->ds_symid;
283 return (dt_module_symgelf32(sym, symp));
291 dt_module_symname64(dt_module_t *dmp, const char *name,
292 GElf_Sym *symp, uint_t *idp)
294 const Elf64_Sym *symtab = dmp->dm_symtab.cts_data;
295 const char *strtab = dmp->dm_strtab.cts_data;
297 const Elf64_Sym *sym;
301 if (dmp->dm_nsymelems == 0)
304 h = dt_strtab_hash(name, NULL) % dmp->dm_nsymbuckets;
306 for (i = dmp->dm_symbuckets[h]; i != 0; i = dsp->ds_next) {
307 dsp = &dmp->dm_symchains[i];
308 sym = symtab + dsp->ds_symid;
310 if (strcmp(name, strtab + sym->st_name) == 0) {
312 *idp = dsp->ds_symid;
313 return (dt_module_symgelf64(sym, symp));
321 dt_module_symaddr32(dt_module_t *dmp, GElf_Addr addr,
322 GElf_Sym *symp, uint_t *idp)
324 const Elf32_Sym **asmap = (const Elf32_Sym **)dmp->dm_asmap;
325 const Elf32_Sym *symtab = dmp->dm_symtab.cts_data;
326 const Elf32_Sym *sym;
328 uint_t i, mid, lo = 0, hi = dmp->dm_aslen - 1;
331 if (dmp->dm_aslen == 0)
334 while (hi - lo > 1) {
336 if (addr >= asmap[mid]->st_value)
342 i = addr < asmap[hi]->st_value ? lo : hi;
347 * If the previous entry has the same value, improve our choice. The
348 * order of equal-valued symbols is determined by the comparison func.
350 while (i-- != 0 && asmap[i]->st_value == v)
353 if (addr - sym->st_value < MAX(sym->st_size, 1)) {
355 *idp = (uint_t)(sym - symtab);
356 return (dt_module_symgelf32(sym, symp));
363 dt_module_symaddr64(dt_module_t *dmp, GElf_Addr addr,
364 GElf_Sym *symp, uint_t *idp)
366 const Elf64_Sym **asmap = (const Elf64_Sym **)dmp->dm_asmap;
367 const Elf64_Sym *symtab = dmp->dm_symtab.cts_data;
368 const Elf64_Sym *sym;
370 uint_t i, mid, lo = 0, hi = dmp->dm_aslen - 1;
373 if (dmp->dm_aslen == 0)
376 while (hi - lo > 1) {
378 if (addr >= asmap[mid]->st_value)
384 i = addr < asmap[hi]->st_value ? lo : hi;
389 * If the previous entry has the same value, improve our choice. The
390 * order of equal-valued symbols is determined by the comparison func.
392 while (i-- != 0 && asmap[i]->st_value == v)
395 if (addr - sym->st_value < MAX(sym->st_size, 1)) {
397 *idp = (uint_t)(sym - symtab);
398 return (dt_module_symgelf64(sym, symp));
404 static const dt_modops_t dt_modops_32 = {
411 static const dt_modops_t dt_modops_64 = {
419 dt_module_create(dtrace_hdl_t *dtp, const char *name)
421 uint_t h = dt_strtab_hash(name, NULL) % dtp->dt_modbuckets;
424 for (dmp = dtp->dt_mods[h]; dmp != NULL; dmp = dmp->dm_next) {
425 if (strcmp(dmp->dm_name, name) == 0)
429 if ((dmp = malloc(sizeof (dt_module_t))) == NULL)
430 return (NULL); /* caller must handle allocation failure */
432 bzero(dmp, sizeof (dt_module_t));
433 (void) strlcpy(dmp->dm_name, name, sizeof (dmp->dm_name));
434 dt_list_append(&dtp->dt_modlist, dmp);
435 dmp->dm_next = dtp->dt_mods[h];
436 dtp->dt_mods[h] = dmp;
439 if (dtp->dt_conf.dtc_ctfmodel == CTF_MODEL_LP64)
440 dmp->dm_ops = &dt_modops_64;
442 dmp->dm_ops = &dt_modops_32;
448 dt_module_lookup_by_name(dtrace_hdl_t *dtp, const char *name)
450 uint_t h = dt_strtab_hash(name, NULL) % dtp->dt_modbuckets;
453 for (dmp = dtp->dt_mods[h]; dmp != NULL; dmp = dmp->dm_next) {
454 if (strcmp(dmp->dm_name, name) == 0)
463 dt_module_lookup_by_ctf(dtrace_hdl_t *dtp, ctf_file_t *ctfp)
465 return (ctfp ? ctf_getspecific(ctfp) : NULL);
469 dt_module_load_sect(dtrace_hdl_t *dtp, dt_module_t *dmp, ctf_sect_t *ctsp)
477 if (elf_getshdrstrndx(dmp->dm_elf, &shstrs) == -1)
478 return (dt_set_errno(dtp, EDT_NOTLOADED));
480 for (sp = NULL; (sp = elf_nextscn(dmp->dm_elf, sp)) != NULL; ) {
481 if (gelf_getshdr(sp, &sh) == NULL || sh.sh_type == SHT_NULL ||
482 (s = elf_strptr(dmp->dm_elf, shstrs, sh.sh_name)) == NULL)
483 continue; /* skip any malformed sections */
485 if (sh.sh_type == ctsp->cts_type &&
486 sh.sh_entsize == ctsp->cts_entsize &&
487 strcmp(s, ctsp->cts_name) == 0)
488 break; /* section matches specification */
492 * If the section isn't found, return success but leave cts_data set
493 * to NULL and cts_size set to zero for our caller.
495 if (sp == NULL || (dp = elf_getdata(sp, NULL)) == NULL)
498 ctsp->cts_data = dp->d_buf;
499 ctsp->cts_size = dp->d_size;
501 dt_dprintf("loaded %s [%s] (%lu bytes)\n",
502 dmp->dm_name, ctsp->cts_name, (ulong_t)ctsp->cts_size);
508 dt_module_load(dtrace_hdl_t *dtp, dt_module_t *dmp)
510 if (dmp->dm_flags & DT_DM_LOADED)
511 return (0); /* module is already loaded */
513 dmp->dm_ctdata.cts_name = ".SUNW_ctf";
514 dmp->dm_ctdata.cts_type = SHT_PROGBITS;
515 dmp->dm_ctdata.cts_flags = 0;
516 dmp->dm_ctdata.cts_data = NULL;
517 dmp->dm_ctdata.cts_size = 0;
518 dmp->dm_ctdata.cts_entsize = 0;
519 dmp->dm_ctdata.cts_offset = 0;
521 dmp->dm_symtab.cts_name = ".symtab";
522 dmp->dm_symtab.cts_type = SHT_SYMTAB;
523 dmp->dm_symtab.cts_flags = 0;
524 dmp->dm_symtab.cts_data = NULL;
525 dmp->dm_symtab.cts_size = 0;
526 dmp->dm_symtab.cts_entsize = dmp->dm_ops == &dt_modops_64 ?
527 sizeof (Elf64_Sym) : sizeof (Elf32_Sym);
528 dmp->dm_symtab.cts_offset = 0;
530 dmp->dm_strtab.cts_name = ".strtab";
531 dmp->dm_strtab.cts_type = SHT_STRTAB;
532 dmp->dm_strtab.cts_flags = 0;
533 dmp->dm_strtab.cts_data = NULL;
534 dmp->dm_strtab.cts_size = 0;
535 dmp->dm_strtab.cts_entsize = 0;
536 dmp->dm_strtab.cts_offset = 0;
539 * Attempt to load the module's CTF section, symbol table section, and
540 * string table section. Note that modules may not contain CTF data:
541 * this will result in a successful load_sect but data of size zero.
542 * We will then fail if dt_module_getctf() is called, as shown below.
544 if (dt_module_load_sect(dtp, dmp, &dmp->dm_ctdata) == -1 ||
545 dt_module_load_sect(dtp, dmp, &dmp->dm_symtab) == -1 ||
546 dt_module_load_sect(dtp, dmp, &dmp->dm_strtab) == -1) {
547 dt_module_unload(dtp, dmp);
548 return (-1); /* dt_errno is set for us */
552 * Allocate the hash chains and hash buckets for symbol name lookup.
553 * This is relatively simple since the symbol table is of fixed size
554 * and is known in advance. We allocate one extra element since we
555 * use element indices instead of pointers and zero is our sentinel.
558 dmp->dm_symtab.cts_size / dmp->dm_symtab.cts_entsize;
560 dmp->dm_nsymbuckets = _dtrace_strbuckets;
561 dmp->dm_symfree = 1; /* first free element is index 1 */
563 dmp->dm_symbuckets = malloc(sizeof (uint_t) * dmp->dm_nsymbuckets);
564 dmp->dm_symchains = malloc(sizeof (dt_sym_t) * dmp->dm_nsymelems + 1);
566 if (dmp->dm_symbuckets == NULL || dmp->dm_symchains == NULL) {
567 dt_module_unload(dtp, dmp);
568 return (dt_set_errno(dtp, EDT_NOMEM));
571 bzero(dmp->dm_symbuckets, sizeof (uint_t) * dmp->dm_nsymbuckets);
572 bzero(dmp->dm_symchains, sizeof (dt_sym_t) * dmp->dm_nsymelems + 1);
575 * Iterate over the symbol table data buffer and insert each symbol
576 * name into the name hash if the name and type are valid. Then
577 * allocate the address map, fill it in, and sort it.
579 dmp->dm_asrsv = dmp->dm_ops->do_syminit(dmp);
581 dt_dprintf("hashed %s [%s] (%u symbols)\n",
582 dmp->dm_name, dmp->dm_symtab.cts_name, dmp->dm_symfree - 1);
584 if ((dmp->dm_asmap = malloc(sizeof (void *) * dmp->dm_asrsv)) == NULL) {
585 dt_module_unload(dtp, dmp);
586 return (dt_set_errno(dtp, EDT_NOMEM));
589 dmp->dm_ops->do_symsort(dmp);
591 dt_dprintf("sorted %s [%s] (%u symbols)\n",
592 dmp->dm_name, dmp->dm_symtab.cts_name, dmp->dm_aslen);
594 dmp->dm_flags |= DT_DM_LOADED;
599 dt_module_getctf(dtrace_hdl_t *dtp, dt_module_t *dmp)
606 if (dmp->dm_ctfp != NULL || dt_module_load(dtp, dmp) != 0)
607 return (dmp->dm_ctfp);
609 if (dmp->dm_ops == &dt_modops_64)
610 model = CTF_MODEL_LP64;
612 model = CTF_MODEL_ILP32;
615 * If the data model of the module does not match our program data
616 * model, then do not permit CTF from this module to be opened and
617 * returned to the compiler. If we support mixed data models in the
618 * future for combined kernel/user tracing, this can be removed.
620 if (dtp->dt_conf.dtc_ctfmodel != model) {
621 (void) dt_set_errno(dtp, EDT_DATAMODEL);
625 if (dmp->dm_ctdata.cts_size == 0) {
626 (void) dt_set_errno(dtp, EDT_NOCTF);
630 dmp->dm_ctfp = ctf_bufopen(&dmp->dm_ctdata,
631 &dmp->dm_symtab, &dmp->dm_strtab, &dtp->dt_ctferr);
633 if (dmp->dm_ctfp == NULL) {
634 (void) dt_set_errno(dtp, EDT_CTF);
638 (void) ctf_setmodel(dmp->dm_ctfp, model);
639 ctf_setspecific(dmp->dm_ctfp, dmp);
641 if ((parent = ctf_parent_name(dmp->dm_ctfp)) != NULL) {
642 if ((pmp = dt_module_create(dtp, parent)) == NULL ||
643 (pfp = dt_module_getctf(dtp, pmp)) == NULL) {
645 (void) dt_set_errno(dtp, EDT_NOMEM);
649 if (ctf_import(dmp->dm_ctfp, pfp) == CTF_ERR) {
650 dtp->dt_ctferr = ctf_errno(dmp->dm_ctfp);
651 (void) dt_set_errno(dtp, EDT_CTF);
656 dt_dprintf("loaded CTF container for %s (%p)\n",
657 dmp->dm_name, (void *)dmp->dm_ctfp);
659 return (dmp->dm_ctfp);
662 ctf_close(dmp->dm_ctfp);
669 dt_module_unload(dtrace_hdl_t *dtp, dt_module_t *dmp)
671 ctf_close(dmp->dm_ctfp);
674 bzero(&dmp->dm_ctdata, sizeof (ctf_sect_t));
675 bzero(&dmp->dm_symtab, sizeof (ctf_sect_t));
676 bzero(&dmp->dm_strtab, sizeof (ctf_sect_t));
678 if (dmp->dm_symbuckets != NULL) {
679 free(dmp->dm_symbuckets);
680 dmp->dm_symbuckets = NULL;
683 if (dmp->dm_symchains != NULL) {
684 free(dmp->dm_symchains);
685 dmp->dm_symchains = NULL;
688 if (dmp->dm_asmap != NULL) {
690 dmp->dm_asmap = NULL;
694 dmp->dm_nsymbuckets = 0;
695 dmp->dm_nsymelems = 0;
699 dmp->dm_text_va = NULL;
700 dmp->dm_text_size = 0;
701 dmp->dm_data_va = NULL;
702 dmp->dm_data_size = 0;
703 dmp->dm_bss_va = NULL;
704 dmp->dm_bss_size = 0;
706 if (dmp->dm_extern != NULL) {
707 dt_idhash_destroy(dmp->dm_extern);
708 dmp->dm_extern = NULL;
711 (void) elf_end(dmp->dm_elf);
714 dmp->dm_flags &= ~DT_DM_LOADED;
718 dt_module_destroy(dtrace_hdl_t *dtp, dt_module_t *dmp)
720 uint_t h = dt_strtab_hash(dmp->dm_name, NULL) % dtp->dt_modbuckets;
721 dt_module_t **dmpp = &dtp->dt_mods[h];
723 dt_list_delete(&dtp->dt_modlist, dmp);
724 assert(dtp->dt_nmods != 0);
728 * Now remove this module from its hash chain. We expect to always
729 * find the module on its hash chain, so in this loop we assert that
730 * we don't run off the end of the list.
732 while (*dmpp != dmp) {
733 dmpp = &((*dmpp)->dm_next);
734 assert(*dmpp != NULL);
737 *dmpp = dmp->dm_next;
739 dt_module_unload(dtp, dmp);
744 * Insert a new external symbol reference into the specified module. The new
745 * symbol will be marked as undefined and is assigned a symbol index beyond
746 * any existing cached symbols from this module. We use the ident's di_data
747 * field to store a pointer to a copy of the dtrace_syminfo_t for this symbol.
750 dt_module_extern(dtrace_hdl_t *dtp, dt_module_t *dmp,
751 const char *name, const dtrace_typeinfo_t *tip)
753 dtrace_syminfo_t *sip;
757 if (dmp->dm_extern == NULL && (dmp->dm_extern = dt_idhash_create(
758 "extern", NULL, dmp->dm_nsymelems, UINT_MAX)) == NULL) {
759 (void) dt_set_errno(dtp, EDT_NOMEM);
763 if (dt_idhash_nextid(dmp->dm_extern, &id) == -1) {
764 (void) dt_set_errno(dtp, EDT_SYMOFLOW);
768 if ((sip = malloc(sizeof (dtrace_syminfo_t))) == NULL) {
769 (void) dt_set_errno(dtp, EDT_NOMEM);
773 idp = dt_idhash_insert(dmp->dm_extern, name, DT_IDENT_SYMBOL, 0, id,
774 _dtrace_symattr, 0, &dt_idops_thaw, NULL, dtp->dt_gen);
777 (void) dt_set_errno(dtp, EDT_NOMEM);
782 sip->dts_object = dmp->dm_name;
783 sip->dts_name = idp->di_name;
784 sip->dts_id = idp->di_id;
787 idp->di_ctfp = tip->dtt_ctfp;
788 idp->di_type = tip->dtt_type;
794 dt_module_modelname(dt_module_t *dmp)
796 if (dmp->dm_ops == &dt_modops_64)
803 * Update our module cache by adding an entry for the specified module 'name'.
804 * We create the dt_module_t and populate it using /system/object/<name>/.
807 dt_module_update(dtrace_hdl_t *dtp, const char *name)
809 char fname[MAXPATHLEN];
820 (void) snprintf(fname, sizeof (fname),
821 "%s/%s/object", OBJFS_ROOT, name);
823 if ((fd = open(fname, O_RDONLY)) == -1 || fstat64(fd, &st) == -1 ||
824 (dmp = dt_module_create(dtp, name)) == NULL) {
825 dt_dprintf("failed to open %s: %s\n", fname, strerror(errno));
831 * Since the module can unload out from under us (and /system/object
832 * will return ENOENT), tell libelf to cook the entire file now and
833 * then close the underlying file descriptor immediately. If this
834 * succeeds, we know that we can continue safely using dmp->dm_elf.
836 dmp->dm_elf = elf_begin(fd, ELF_C_READ, NULL);
837 err = elf_cntl(dmp->dm_elf, ELF_C_FDREAD);
840 if (dmp->dm_elf == NULL || err == -1 ||
841 elf_getshdrstrndx(dmp->dm_elf, &shstrs) == -1) {
842 dt_dprintf("failed to load %s: %s\n",
843 fname, elf_errmsg(elf_errno()));
844 dt_module_destroy(dtp, dmp);
848 switch (gelf_getclass(dmp->dm_elf)) {
850 dmp->dm_ops = &dt_modops_32;
854 dmp->dm_ops = &dt_modops_64;
858 dt_dprintf("failed to load %s: unknown ELF class\n", fname);
859 dt_module_destroy(dtp, dmp);
864 * Iterate over the section headers locating various sections of
865 * interest and use their attributes to flesh out the dt_module_t.
867 for (sp = NULL; (sp = elf_nextscn(dmp->dm_elf, sp)) != NULL; ) {
868 if (gelf_getshdr(sp, &sh) == NULL || sh.sh_type == SHT_NULL ||
869 (s = elf_strptr(dmp->dm_elf, shstrs, sh.sh_name)) == NULL)
870 continue; /* skip any malformed sections */
872 if (strcmp(s, ".text") == 0) {
873 dmp->dm_text_size = sh.sh_size;
874 dmp->dm_text_va = sh.sh_addr;
875 } else if (strcmp(s, ".data") == 0) {
876 dmp->dm_data_size = sh.sh_size;
877 dmp->dm_data_va = sh.sh_addr;
878 } else if (strcmp(s, ".bss") == 0) {
879 dmp->dm_bss_size = sh.sh_size;
880 dmp->dm_bss_va = sh.sh_addr;
881 } else if (strcmp(s, ".info") == 0 &&
882 (dp = elf_getdata(sp, NULL)) != NULL) {
883 bcopy(dp->d_buf, &dmp->dm_info,
884 MIN(sh.sh_size, sizeof (dmp->dm_info)));
885 } else if (strcmp(s, ".filename") == 0 &&
886 (dp = elf_getdata(sp, NULL)) != NULL) {
887 (void) strlcpy(dmp->dm_file,
888 dp->d_buf, sizeof (dmp->dm_file));
892 dmp->dm_flags |= DT_DM_KERNEL;
893 dmp->dm_modid = (int)OBJFS_MODID(st.st_ino);
895 if (dmp->dm_info.objfs_info_primary)
896 dmp->dm_flags |= DT_DM_PRIMARY;
898 dt_dprintf("opened %d-bit module %s (%s) [%d]\n",
899 bits, dmp->dm_name, dmp->dm_file, dmp->dm_modid);
903 * Unload all the loaded modules and then refresh the module cache with the
904 * latest list of loaded modules and their address ranges.
907 dtrace_update(dtrace_hdl_t *dtp)
912 for (dmp = dt_list_next(&dtp->dt_modlist);
913 dmp != NULL; dmp = dt_list_next(dmp))
914 dt_module_unload(dtp, dmp);
917 * Open /system/object and attempt to create a libdtrace module for
918 * each kernel module that is loaded on the current system.
920 if (!(dtp->dt_oflags & DTRACE_O_NOSYS) &&
921 (dirp = opendir(OBJFS_ROOT)) != NULL) {
924 while ((dp = readdir(dirp)) != NULL) {
925 if (dp->d_name[0] != '.')
926 dt_module_update(dtp, dp->d_name);
929 (void) closedir(dirp);
933 * Look up all the macro identifiers and set di_id to the latest value.
934 * This code collaborates with dt_lex.l on the use of di_id. We will
935 * need to implement something fancier if we need to support non-ints.
937 dt_idhash_lookup(dtp->dt_macros, "egid")->di_id = getegid();
938 dt_idhash_lookup(dtp->dt_macros, "euid")->di_id = geteuid();
939 dt_idhash_lookup(dtp->dt_macros, "gid")->di_id = getgid();
940 dt_idhash_lookup(dtp->dt_macros, "pid")->di_id = getpid();
941 dt_idhash_lookup(dtp->dt_macros, "pgid")->di_id = getpgid(0);
942 dt_idhash_lookup(dtp->dt_macros, "ppid")->di_id = getppid();
943 dt_idhash_lookup(dtp->dt_macros, "projid")->di_id = getprojid();
944 dt_idhash_lookup(dtp->dt_macros, "sid")->di_id = getsid(0);
945 dt_idhash_lookup(dtp->dt_macros, "taskid")->di_id = gettaskid();
946 dt_idhash_lookup(dtp->dt_macros, "uid")->di_id = getuid();
949 * Cache the pointers to the modules representing the base executable
950 * and the run-time linker in the dtrace client handle. Note that on
951 * x86 krtld is folded into unix, so if we don't find it, use unix
954 dtp->dt_exec = dt_module_lookup_by_name(dtp, "genunix");
955 dtp->dt_rtld = dt_module_lookup_by_name(dtp, "krtld");
956 if (dtp->dt_rtld == NULL)
957 dtp->dt_rtld = dt_module_lookup_by_name(dtp, "unix");
960 * If this is the first time we are initializing the module list,
961 * remove the module for genunix from the module list and then move it
962 * to the front of the module list. We do this so that type and symbol
963 * queries encounter genunix and thereby optimize for the common case
964 * in dtrace_lookup_by_name() and dtrace_lookup_by_type(), below.
966 if (dtp->dt_exec != NULL &&
967 dtp->dt_cdefs == NULL && dtp->dt_ddefs == NULL) {
968 dt_list_delete(&dtp->dt_modlist, dtp->dt_exec);
969 dt_list_prepend(&dtp->dt_modlist, dtp->dt_exec);
974 dt_module_from_object(dtrace_hdl_t *dtp, const char *object)
979 switch ((uintptr_t)object) {
980 case (uintptr_t)DTRACE_OBJ_EXEC:
983 case (uintptr_t)DTRACE_OBJ_RTLD:
986 case (uintptr_t)DTRACE_OBJ_CDEFS:
989 case (uintptr_t)DTRACE_OBJ_DDEFS:
993 dmp = dt_module_create(dtp, object);
998 (void) dt_set_errno(dtp, err);
1004 * Exported interface to look up a symbol by name. We return the GElf_Sym and
1005 * complete symbol information for the matching symbol.
1008 dtrace_lookup_by_name(dtrace_hdl_t *dtp, const char *object, const char *name,
1009 GElf_Sym *symp, dtrace_syminfo_t *sip)
1016 uint_t mask = 0; /* mask of dt_module flags to match */
1017 uint_t bits = 0; /* flag bits that must be present */
1019 if (object != DTRACE_OBJ_EVERY &&
1020 object != DTRACE_OBJ_KMODS &&
1021 object != DTRACE_OBJ_UMODS) {
1022 if ((dmp = dt_module_from_object(dtp, object)) == NULL)
1023 return (-1); /* dt_errno is set for us */
1025 if (dt_module_load(dtp, dmp) == -1)
1026 return (-1); /* dt_errno is set for us */
1030 if (object == DTRACE_OBJ_KMODS)
1031 mask = bits = DT_DM_KERNEL;
1032 else if (object == DTRACE_OBJ_UMODS)
1033 mask = DT_DM_KERNEL;
1035 dmp = dt_list_next(&dtp->dt_modlist);
1042 for (; n > 0; n--, dmp = dt_list_next(dmp)) {
1043 if ((dmp->dm_flags & mask) != bits)
1044 continue; /* failed to match required attributes */
1046 if (dt_module_load(dtp, dmp) == -1)
1047 continue; /* failed to load symbol table */
1049 if (dmp->dm_ops->do_symname(dmp, name, symp, &id) != NULL) {
1051 sip->dts_object = dmp->dm_name;
1052 sip->dts_name = (const char *)
1053 dmp->dm_strtab.cts_data + symp->st_name;
1059 if (dmp->dm_extern != NULL &&
1060 (idp = dt_idhash_lookup(dmp->dm_extern, name)) != NULL) {
1062 symp->st_name = (uintptr_t)idp->di_name;
1064 GELF_ST_INFO(STB_GLOBAL, STT_NOTYPE);
1066 symp->st_shndx = SHN_UNDEF;
1069 ctf_type_size(idp->di_ctfp, idp->di_type);
1073 sip->dts_object = dmp->dm_name;
1074 sip->dts_name = idp->di_name;
1075 sip->dts_id = idp->di_id;
1082 return (dt_set_errno(dtp, EDT_NOSYM));
1086 * Exported interface to look up a symbol by address. We return the GElf_Sym
1087 * and complete symbol information for the matching symbol.
1090 dtrace_lookup_by_addr(dtrace_hdl_t *dtp, GElf_Addr addr,
1091 GElf_Sym *symp, dtrace_syminfo_t *sip)
1095 const dtrace_vector_t *v = dtp->dt_vector;
1098 return (v->dtv_lookup_by_addr(dtp->dt_varg, addr, symp, sip));
1100 for (dmp = dt_list_next(&dtp->dt_modlist); dmp != NULL;
1101 dmp = dt_list_next(dmp)) {
1102 if (addr - dmp->dm_text_va < dmp->dm_text_size ||
1103 addr - dmp->dm_data_va < dmp->dm_data_size ||
1104 addr - dmp->dm_bss_va < dmp->dm_bss_size)
1109 return (dt_set_errno(dtp, EDT_NOSYMADDR));
1111 if (dt_module_load(dtp, dmp) == -1)
1112 return (-1); /* dt_errno is set for us */
1115 if (dmp->dm_ops->do_symaddr(dmp, addr, symp, &id) == NULL)
1116 return (dt_set_errno(dtp, EDT_NOSYMADDR));
1120 sip->dts_object = dmp->dm_name;
1123 sip->dts_name = (const char *)
1124 dmp->dm_strtab.cts_data + symp->st_name;
1127 sip->dts_name = NULL;
1136 dtrace_lookup_by_type(dtrace_hdl_t *dtp, const char *object, const char *name,
1137 dtrace_typeinfo_t *tip)
1139 dtrace_typeinfo_t ti;
1146 uint_t mask = 0; /* mask of dt_module flags to match */
1147 uint_t bits = 0; /* flag bits that must be present */
1149 if (object != DTRACE_OBJ_EVERY &&
1150 object != DTRACE_OBJ_KMODS &&
1151 object != DTRACE_OBJ_UMODS) {
1152 if ((dmp = dt_module_from_object(dtp, object)) == NULL)
1153 return (-1); /* dt_errno is set for us */
1155 if (dt_module_load(dtp, dmp) == -1)
1156 return (-1); /* dt_errno is set for us */
1161 if (object == DTRACE_OBJ_KMODS)
1162 mask = bits = DT_DM_KERNEL;
1163 else if (object == DTRACE_OBJ_UMODS)
1164 mask = DT_DM_KERNEL;
1166 dmp = dt_list_next(&dtp->dt_modlist);
1174 for (; n > 0; n--, dmp = dt_list_next(dmp)) {
1175 if ((dmp->dm_flags & mask) != bits)
1176 continue; /* failed to match required attributes */
1179 * If we can't load the CTF container, continue on to the next
1180 * module. If our search was scoped to only one module then
1181 * return immediately leaving dt_errno unmodified.
1183 if (dt_module_getctf(dtp, dmp) == NULL) {
1190 * Look up the type in the module's CTF container. If our
1191 * match is a forward declaration tag, save this choice in
1192 * 'tip' and keep going in the hope that we will locate the
1193 * underlying structure definition. Otherwise just return.
1195 if ((id = ctf_lookup_by_name(dmp->dm_ctfp, name)) != CTF_ERR) {
1196 tip->dtt_object = dmp->dm_name;
1197 tip->dtt_ctfp = dmp->dm_ctfp;
1200 if (ctf_type_kind(dmp->dm_ctfp, ctf_type_resolve(
1201 dmp->dm_ctfp, id)) != CTF_K_FORWARD)
1209 return (dt_set_errno(dtp, EDT_NOTYPE));
1215 dtrace_symbol_type(dtrace_hdl_t *dtp, const GElf_Sym *symp,
1216 const dtrace_syminfo_t *sip, dtrace_typeinfo_t *tip)
1220 tip->dtt_object = NULL;
1221 tip->dtt_ctfp = NULL;
1222 tip->dtt_type = CTF_ERR;
1224 if ((dmp = dt_module_lookup_by_name(dtp, sip->dts_object)) == NULL)
1225 return (dt_set_errno(dtp, EDT_NOMOD));
1227 if (symp->st_shndx == SHN_UNDEF && dmp->dm_extern != NULL) {
1229 dt_idhash_lookup(dmp->dm_extern, sip->dts_name);
1232 return (dt_set_errno(dtp, EDT_NOSYM));
1234 tip->dtt_ctfp = idp->di_ctfp;
1235 tip->dtt_type = idp->di_type;
1237 } else if (GELF_ST_TYPE(symp->st_info) != STT_FUNC) {
1238 if (dt_module_getctf(dtp, dmp) == NULL)
1239 return (-1); /* errno is set for us */
1241 tip->dtt_ctfp = dmp->dm_ctfp;
1242 tip->dtt_type = ctf_lookup_by_symbol(dmp->dm_ctfp, sip->dts_id);
1244 if (tip->dtt_type == CTF_ERR) {
1245 dtp->dt_ctferr = ctf_errno(tip->dtt_ctfp);
1246 return (dt_set_errno(dtp, EDT_CTF));
1250 tip->dtt_ctfp = DT_FPTR_CTFP(dtp);
1251 tip->dtt_type = DT_FPTR_TYPE(dtp);
1254 tip->dtt_object = dmp->dm_name;
1258 static dtrace_objinfo_t *
1259 dt_module_info(const dt_module_t *dmp, dtrace_objinfo_t *dto)
1261 dto->dto_name = dmp->dm_name;
1262 dto->dto_file = dmp->dm_file;
1263 dto->dto_id = dmp->dm_modid;
1266 if (dmp->dm_flags & DT_DM_KERNEL)
1267 dto->dto_flags |= DTRACE_OBJ_F_KERNEL;
1268 if (dmp->dm_flags & DT_DM_PRIMARY)
1269 dto->dto_flags |= DTRACE_OBJ_F_PRIMARY;
1271 dto->dto_text_va = dmp->dm_text_va;
1272 dto->dto_text_size = dmp->dm_text_size;
1273 dto->dto_data_va = dmp->dm_data_va;
1274 dto->dto_data_size = dmp->dm_data_size;
1275 dto->dto_bss_va = dmp->dm_bss_va;
1276 dto->dto_bss_size = dmp->dm_bss_size;
1282 dtrace_object_iter(dtrace_hdl_t *dtp, dtrace_obj_f *func, void *data)
1284 const dt_module_t *dmp = dt_list_next(&dtp->dt_modlist);
1285 dtrace_objinfo_t dto;
1288 for (; dmp != NULL; dmp = dt_list_next(dmp)) {
1289 if ((rv = (*func)(dtp, dt_module_info(dmp, &dto), data)) != 0)
1297 dtrace_object_info(dtrace_hdl_t *dtp, const char *object, dtrace_objinfo_t *dto)
1301 if (object == DTRACE_OBJ_EVERY || object == DTRACE_OBJ_KMODS ||
1302 object == DTRACE_OBJ_UMODS || dto == NULL)
1303 return (dt_set_errno(dtp, EINVAL));
1305 if ((dmp = dt_module_from_object(dtp, object)) == NULL)
1306 return (-1); /* dt_errno is set for us */
1308 if (dt_module_load(dtp, dmp) == -1)
1309 return (-1); /* dt_errno is set for us */
1311 (void) dt_module_info(dmp, dto);