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]
22 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #pragma ident "%Z%%M% %I% %E% SMI"
28 #include <sys/types.h>
30 #include <sys/modctl.h>
32 #include <sys/kobj_impl.h>
33 #include <sys/sysmacros.h>
37 #include <sys/param.h>
38 #include <sys/linker.h>
57 #include <dt_strtab.h>
58 #include <dt_module.h>
61 static const char *dt_module_strtab; /* active strtab for qsort callbacks */
64 dt_module_symhash_insert(dt_module_t *dmp, const char *name, uint_t id)
66 dt_sym_t *dsp = &dmp->dm_symchains[dmp->dm_symfree];
69 assert(dmp->dm_symfree < dmp->dm_nsymelems + 1);
72 h = dt_strtab_hash(name, NULL) % dmp->dm_nsymbuckets;
73 dsp->ds_next = dmp->dm_symbuckets[h];
74 dmp->dm_symbuckets[h] = dmp->dm_symfree++;
78 dt_module_syminit32(dt_module_t *dmp)
80 Elf32_Sym *sym = dmp->dm_symtab.cts_data;
81 const char *base = dmp->dm_strtab.cts_data;
82 size_t ss_size = dmp->dm_strtab.cts_size;
83 uint_t i, n = dmp->dm_nsymelems;
86 for (i = 0; i < n; i++, sym++) {
87 const char *name = base + sym->st_name;
88 uchar_t type = ELF32_ST_TYPE(sym->st_info);
90 if (type >= STT_NUM || type == STT_SECTION)
91 continue; /* skip sections and unknown types */
93 if (sym->st_name == 0 || sym->st_name >= ss_size)
94 continue; /* skip null or invalid names */
96 if (sym->st_value != 0 &&
97 (ELF32_ST_BIND(sym->st_info) != STB_LOCAL || sym->st_size)) {
98 asrsv++; /* reserve space in the address map */
101 sym->st_value += (Elf_Addr) dmp->dm_reloc_offset;
105 dt_module_symhash_insert(dmp, name, i);
112 dt_module_syminit64(dt_module_t *dmp)
114 Elf64_Sym *sym = dmp->dm_symtab.cts_data;
115 const char *base = dmp->dm_strtab.cts_data;
116 size_t ss_size = dmp->dm_strtab.cts_size;
117 uint_t i, n = dmp->dm_nsymelems;
120 for (i = 0; i < n; i++, sym++) {
121 const char *name = base + sym->st_name;
122 uchar_t type = ELF64_ST_TYPE(sym->st_info);
124 if (type >= STT_NUM || type == STT_SECTION)
125 continue; /* skip sections and unknown types */
127 if (sym->st_name == 0 || sym->st_name >= ss_size)
128 continue; /* skip null or invalid names */
130 if (sym->st_value != 0 &&
131 (ELF64_ST_BIND(sym->st_info) != STB_LOCAL || sym->st_size)) {
132 asrsv++; /* reserve space in the address map */
135 sym->st_value += (Elf_Addr) dmp->dm_reloc_offset;
139 dt_module_symhash_insert(dmp, name, i);
146 * Sort comparison function for 32-bit symbol address-to-name lookups. We sort
147 * symbols by value. If values are equal, we prefer the symbol that is
148 * non-zero sized, typed, not weak, or lexically first, in that order.
151 dt_module_symcomp32(const void *lp, const void *rp)
153 Elf32_Sym *lhs = *((Elf32_Sym **)lp);
154 Elf32_Sym *rhs = *((Elf32_Sym **)rp);
156 if (lhs->st_value != rhs->st_value)
157 return (lhs->st_value > rhs->st_value ? 1 : -1);
159 if ((lhs->st_size == 0) != (rhs->st_size == 0))
160 return (lhs->st_size == 0 ? 1 : -1);
162 if ((ELF32_ST_TYPE(lhs->st_info) == STT_NOTYPE) !=
163 (ELF32_ST_TYPE(rhs->st_info) == STT_NOTYPE))
164 return (ELF32_ST_TYPE(lhs->st_info) == STT_NOTYPE ? 1 : -1);
166 if ((ELF32_ST_BIND(lhs->st_info) == STB_WEAK) !=
167 (ELF32_ST_BIND(rhs->st_info) == STB_WEAK))
168 return (ELF32_ST_BIND(lhs->st_info) == STB_WEAK ? 1 : -1);
170 return (strcmp(dt_module_strtab + lhs->st_name,
171 dt_module_strtab + rhs->st_name));
175 * Sort comparison function for 64-bit symbol address-to-name lookups. We sort
176 * symbols by value. If values are equal, we prefer the symbol that is
177 * non-zero sized, typed, not weak, or lexically first, in that order.
180 dt_module_symcomp64(const void *lp, const void *rp)
182 Elf64_Sym *lhs = *((Elf64_Sym **)lp);
183 Elf64_Sym *rhs = *((Elf64_Sym **)rp);
185 if (lhs->st_value != rhs->st_value)
186 return (lhs->st_value > rhs->st_value ? 1 : -1);
188 if ((lhs->st_size == 0) != (rhs->st_size == 0))
189 return (lhs->st_size == 0 ? 1 : -1);
191 if ((ELF64_ST_TYPE(lhs->st_info) == STT_NOTYPE) !=
192 (ELF64_ST_TYPE(rhs->st_info) == STT_NOTYPE))
193 return (ELF64_ST_TYPE(lhs->st_info) == STT_NOTYPE ? 1 : -1);
195 if ((ELF64_ST_BIND(lhs->st_info) == STB_WEAK) !=
196 (ELF64_ST_BIND(rhs->st_info) == STB_WEAK))
197 return (ELF64_ST_BIND(lhs->st_info) == STB_WEAK ? 1 : -1);
199 return (strcmp(dt_module_strtab + lhs->st_name,
200 dt_module_strtab + rhs->st_name));
204 dt_module_symsort32(dt_module_t *dmp)
206 Elf32_Sym *symtab = (Elf32_Sym *)dmp->dm_symtab.cts_data;
207 Elf32_Sym **sympp = (Elf32_Sym **)dmp->dm_asmap;
208 const dt_sym_t *dsp = dmp->dm_symchains + 1;
209 uint_t i, n = dmp->dm_symfree;
211 for (i = 1; i < n; i++, dsp++) {
212 Elf32_Sym *sym = symtab + dsp->ds_symid;
213 if (sym->st_value != 0 &&
214 (ELF32_ST_BIND(sym->st_info) != STB_LOCAL || sym->st_size))
218 dmp->dm_aslen = (uint_t)(sympp - (Elf32_Sym **)dmp->dm_asmap);
219 assert(dmp->dm_aslen <= dmp->dm_asrsv);
221 dt_module_strtab = dmp->dm_strtab.cts_data;
222 qsort(dmp->dm_asmap, dmp->dm_aslen,
223 sizeof (Elf32_Sym *), dt_module_symcomp32);
224 dt_module_strtab = NULL;
228 dt_module_symsort64(dt_module_t *dmp)
230 Elf64_Sym *symtab = (Elf64_Sym *)dmp->dm_symtab.cts_data;
231 Elf64_Sym **sympp = (Elf64_Sym **)dmp->dm_asmap;
232 const dt_sym_t *dsp = dmp->dm_symchains + 1;
233 uint_t i, n = dmp->dm_symfree;
235 for (i = 1; i < n; i++, dsp++) {
236 Elf64_Sym *sym = symtab + dsp->ds_symid;
237 if (sym->st_value != 0 &&
238 (ELF64_ST_BIND(sym->st_info) != STB_LOCAL || sym->st_size))
242 dmp->dm_aslen = (uint_t)(sympp - (Elf64_Sym **)dmp->dm_asmap);
243 assert(dmp->dm_aslen <= dmp->dm_asrsv);
245 dt_module_strtab = dmp->dm_strtab.cts_data;
246 qsort(dmp->dm_asmap, dmp->dm_aslen,
247 sizeof (Elf64_Sym *), dt_module_symcomp64);
248 dt_module_strtab = NULL;
252 dt_module_symgelf32(const Elf32_Sym *src, GElf_Sym *dst)
255 dst->st_name = src->st_name;
256 dst->st_info = src->st_info;
257 dst->st_other = src->st_other;
258 dst->st_shndx = src->st_shndx;
259 dst->st_value = src->st_value;
260 dst->st_size = src->st_size;
267 dt_module_symgelf64(const Elf64_Sym *src, GElf_Sym *dst)
270 bcopy(src, dst, sizeof (GElf_Sym));
276 dt_module_symname32(dt_module_t *dmp, const char *name,
277 GElf_Sym *symp, uint_t *idp)
279 const Elf32_Sym *symtab = dmp->dm_symtab.cts_data;
280 const char *strtab = dmp->dm_strtab.cts_data;
282 const Elf32_Sym *sym;
286 if (dmp->dm_nsymelems == 0)
289 h = dt_strtab_hash(name, NULL) % dmp->dm_nsymbuckets;
291 for (i = dmp->dm_symbuckets[h]; i != 0; i = dsp->ds_next) {
292 dsp = &dmp->dm_symchains[i];
293 sym = symtab + dsp->ds_symid;
295 if (strcmp(name, strtab + sym->st_name) == 0) {
297 *idp = dsp->ds_symid;
298 return (dt_module_symgelf32(sym, symp));
306 dt_module_symname64(dt_module_t *dmp, const char *name,
307 GElf_Sym *symp, uint_t *idp)
309 const Elf64_Sym *symtab = dmp->dm_symtab.cts_data;
310 const char *strtab = dmp->dm_strtab.cts_data;
312 const Elf64_Sym *sym;
316 if (dmp->dm_nsymelems == 0)
319 h = dt_strtab_hash(name, NULL) % dmp->dm_nsymbuckets;
321 for (i = dmp->dm_symbuckets[h]; i != 0; i = dsp->ds_next) {
322 dsp = &dmp->dm_symchains[i];
323 sym = symtab + dsp->ds_symid;
325 if (strcmp(name, strtab + sym->st_name) == 0) {
327 *idp = dsp->ds_symid;
328 return (dt_module_symgelf64(sym, symp));
336 dt_module_symaddr32(dt_module_t *dmp, GElf_Addr addr,
337 GElf_Sym *symp, uint_t *idp)
339 const Elf32_Sym **asmap = (const Elf32_Sym **)dmp->dm_asmap;
340 const Elf32_Sym *symtab = dmp->dm_symtab.cts_data;
341 const Elf32_Sym *sym;
343 uint_t i, mid, lo = 0, hi = dmp->dm_aslen - 1;
346 if (dmp->dm_aslen == 0)
349 while (hi - lo > 1) {
351 if (addr >= asmap[mid]->st_value)
357 i = addr < asmap[hi]->st_value ? lo : hi;
362 * If the previous entry has the same value, improve our choice. The
363 * order of equal-valued symbols is determined by the comparison func.
365 while (i-- != 0 && asmap[i]->st_value == v)
368 if (addr - sym->st_value < MAX(sym->st_size, 1)) {
370 *idp = (uint_t)(sym - symtab);
371 return (dt_module_symgelf32(sym, symp));
378 dt_module_symaddr64(dt_module_t *dmp, GElf_Addr addr,
379 GElf_Sym *symp, uint_t *idp)
381 const Elf64_Sym **asmap = (const Elf64_Sym **)dmp->dm_asmap;
382 const Elf64_Sym *symtab = dmp->dm_symtab.cts_data;
383 const Elf64_Sym *sym;
385 uint_t i, mid, lo = 0, hi = dmp->dm_aslen - 1;
388 if (dmp->dm_aslen == 0)
391 while (hi - lo > 1) {
393 if (addr >= asmap[mid]->st_value)
399 i = addr < asmap[hi]->st_value ? lo : hi;
404 * If the previous entry has the same value, improve our choice. The
405 * order of equal-valued symbols is determined by the comparison func.
407 while (i-- != 0 && asmap[i]->st_value == v)
410 if (addr - sym->st_value < MAX(sym->st_size, 1)) {
412 *idp = (uint_t)(sym - symtab);
413 return (dt_module_symgelf64(sym, symp));
419 static const dt_modops_t dt_modops_32 = {
426 static const dt_modops_t dt_modops_64 = {
434 dt_module_create(dtrace_hdl_t *dtp, const char *name)
436 uint_t h = dt_strtab_hash(name, NULL) % dtp->dt_modbuckets;
439 for (dmp = dtp->dt_mods[h]; dmp != NULL; dmp = dmp->dm_next) {
440 if (strcmp(dmp->dm_name, name) == 0)
444 if ((dmp = malloc(sizeof (dt_module_t))) == NULL)
445 return (NULL); /* caller must handle allocation failure */
447 bzero(dmp, sizeof (dt_module_t));
448 (void) strlcpy(dmp->dm_name, name, sizeof (dmp->dm_name));
449 dt_list_append(&dtp->dt_modlist, dmp);
450 dmp->dm_next = dtp->dt_mods[h];
451 dtp->dt_mods[h] = dmp;
454 if (dtp->dt_conf.dtc_ctfmodel == CTF_MODEL_LP64)
455 dmp->dm_ops = &dt_modops_64;
457 dmp->dm_ops = &dt_modops_32;
463 dt_module_lookup_by_name(dtrace_hdl_t *dtp, const char *name)
465 uint_t h = dt_strtab_hash(name, NULL) % dtp->dt_modbuckets;
468 for (dmp = dtp->dt_mods[h]; dmp != NULL; dmp = dmp->dm_next) {
469 if (strcmp(dmp->dm_name, name) == 0)
478 dt_module_lookup_by_ctf(dtrace_hdl_t *dtp, ctf_file_t *ctfp)
480 return (ctfp ? ctf_getspecific(ctfp) : NULL);
484 dt_module_load_sect(dtrace_hdl_t *dtp, dt_module_t *dmp, ctf_sect_t *ctsp)
492 if (elf_getshstrndx(dmp->dm_elf, &shstrs) == 0)
493 return (dt_set_errno(dtp, EDT_NOTLOADED));
495 for (sp = NULL; (sp = elf_nextscn(dmp->dm_elf, sp)) != NULL; ) {
496 if (gelf_getshdr(sp, &sh) == NULL || sh.sh_type == SHT_NULL ||
497 (s = elf_strptr(dmp->dm_elf, shstrs, sh.sh_name)) == NULL)
498 continue; /* skip any malformed sections */
500 if (sh.sh_type == ctsp->cts_type &&
501 sh.sh_entsize == ctsp->cts_entsize &&
502 strcmp(s, ctsp->cts_name) == 0)
503 break; /* section matches specification */
507 * If the section isn't found, return success but leave cts_data set
508 * to NULL and cts_size set to zero for our caller.
510 if (sp == NULL || (dp = elf_getdata(sp, NULL)) == NULL)
514 ctsp->cts_data = dp->d_buf;
516 if ((ctsp->cts_data = malloc(dp->d_size)) == NULL)
518 memcpy(ctsp->cts_data, dp->d_buf, dp->d_size);
520 ctsp->cts_size = dp->d_size;
522 dt_dprintf("loaded %s [%s] (%lu bytes)\n",
523 dmp->dm_name, ctsp->cts_name, (ulong_t)ctsp->cts_size);
529 dt_module_load(dtrace_hdl_t *dtp, dt_module_t *dmp)
531 if (dmp->dm_flags & DT_DM_LOADED)
532 return (0); /* module is already loaded */
534 dmp->dm_ctdata.cts_name = ".SUNW_ctf";
535 dmp->dm_ctdata.cts_type = SHT_PROGBITS;
536 dmp->dm_ctdata.cts_flags = 0;
537 dmp->dm_ctdata.cts_data = NULL;
538 dmp->dm_ctdata.cts_size = 0;
539 dmp->dm_ctdata.cts_entsize = 0;
540 dmp->dm_ctdata.cts_offset = 0;
542 dmp->dm_symtab.cts_name = ".symtab";
543 dmp->dm_symtab.cts_type = SHT_SYMTAB;
544 dmp->dm_symtab.cts_flags = 0;
545 dmp->dm_symtab.cts_data = NULL;
546 dmp->dm_symtab.cts_size = 0;
547 dmp->dm_symtab.cts_entsize = dmp->dm_ops == &dt_modops_64 ?
548 sizeof (Elf64_Sym) : sizeof (Elf32_Sym);
549 dmp->dm_symtab.cts_offset = 0;
551 dmp->dm_strtab.cts_name = ".strtab";
552 dmp->dm_strtab.cts_type = SHT_STRTAB;
553 dmp->dm_strtab.cts_flags = 0;
554 dmp->dm_strtab.cts_data = NULL;
555 dmp->dm_strtab.cts_size = 0;
556 dmp->dm_strtab.cts_entsize = 0;
557 dmp->dm_strtab.cts_offset = 0;
560 * Attempt to load the module's CTF section, symbol table section, and
561 * string table section. Note that modules may not contain CTF data:
562 * this will result in a successful load_sect but data of size zero.
563 * We will then fail if dt_module_getctf() is called, as shown below.
565 if (dt_module_load_sect(dtp, dmp, &dmp->dm_ctdata) == -1 ||
566 dt_module_load_sect(dtp, dmp, &dmp->dm_symtab) == -1 ||
567 dt_module_load_sect(dtp, dmp, &dmp->dm_strtab) == -1) {
568 dt_module_unload(dtp, dmp);
569 return (-1); /* dt_errno is set for us */
573 * Allocate the hash chains and hash buckets for symbol name lookup.
574 * This is relatively simple since the symbol table is of fixed size
575 * and is known in advance. We allocate one extra element since we
576 * use element indices instead of pointers and zero is our sentinel.
579 dmp->dm_symtab.cts_size / dmp->dm_symtab.cts_entsize;
581 dmp->dm_nsymbuckets = _dtrace_strbuckets;
582 dmp->dm_symfree = 1; /* first free element is index 1 */
584 dmp->dm_symbuckets = malloc(sizeof (uint_t) * dmp->dm_nsymbuckets);
585 dmp->dm_symchains = malloc(sizeof (dt_sym_t) * dmp->dm_nsymelems + 1);
587 if (dmp->dm_symbuckets == NULL || dmp->dm_symchains == NULL) {
588 dt_module_unload(dtp, dmp);
589 return (dt_set_errno(dtp, EDT_NOMEM));
592 bzero(dmp->dm_symbuckets, sizeof (uint_t) * dmp->dm_nsymbuckets);
593 bzero(dmp->dm_symchains, sizeof (dt_sym_t) * dmp->dm_nsymelems + 1);
596 * Iterate over the symbol table data buffer and insert each symbol
597 * name into the name hash if the name and type are valid. Then
598 * allocate the address map, fill it in, and sort it.
600 dmp->dm_asrsv = dmp->dm_ops->do_syminit(dmp);
602 dt_dprintf("hashed %s [%s] (%u symbols)\n",
603 dmp->dm_name, dmp->dm_symtab.cts_name, dmp->dm_symfree - 1);
605 if ((dmp->dm_asmap = malloc(sizeof (void *) * dmp->dm_asrsv)) == NULL) {
606 dt_module_unload(dtp, dmp);
607 return (dt_set_errno(dtp, EDT_NOMEM));
610 dmp->dm_ops->do_symsort(dmp);
612 dt_dprintf("sorted %s [%s] (%u symbols)\n",
613 dmp->dm_name, dmp->dm_symtab.cts_name, dmp->dm_aslen);
615 dmp->dm_flags |= DT_DM_LOADED;
620 dt_module_getctf(dtrace_hdl_t *dtp, dt_module_t *dmp)
627 if (dmp->dm_ctfp != NULL || dt_module_load(dtp, dmp) != 0)
628 return (dmp->dm_ctfp);
630 if (dmp->dm_ops == &dt_modops_64)
631 model = CTF_MODEL_LP64;
633 model = CTF_MODEL_ILP32;
636 * If the data model of the module does not match our program data
637 * model, then do not permit CTF from this module to be opened and
638 * returned to the compiler. If we support mixed data models in the
639 * future for combined kernel/user tracing, this can be removed.
641 if (dtp->dt_conf.dtc_ctfmodel != model) {
642 (void) dt_set_errno(dtp, EDT_DATAMODEL);
646 if (dmp->dm_ctdata.cts_size == 0) {
647 (void) dt_set_errno(dtp, EDT_NOCTF);
651 dmp->dm_ctfp = ctf_bufopen(&dmp->dm_ctdata,
652 &dmp->dm_symtab, &dmp->dm_strtab, &dtp->dt_ctferr);
654 if (dmp->dm_ctfp == NULL) {
655 (void) dt_set_errno(dtp, EDT_CTF);
659 (void) ctf_setmodel(dmp->dm_ctfp, model);
660 ctf_setspecific(dmp->dm_ctfp, dmp);
662 if ((parent = ctf_parent_name(dmp->dm_ctfp)) != NULL) {
663 if ((pmp = dt_module_create(dtp, parent)) == NULL ||
664 (pfp = dt_module_getctf(dtp, pmp)) == NULL) {
666 (void) dt_set_errno(dtp, EDT_NOMEM);
670 if (ctf_import(dmp->dm_ctfp, pfp) == CTF_ERR) {
671 dtp->dt_ctferr = ctf_errno(dmp->dm_ctfp);
672 (void) dt_set_errno(dtp, EDT_CTF);
677 dt_dprintf("loaded CTF container for %s (%p)\n",
678 dmp->dm_name, (void *)dmp->dm_ctfp);
680 return (dmp->dm_ctfp);
683 ctf_close(dmp->dm_ctfp);
690 dt_module_unload(dtrace_hdl_t *dtp, dt_module_t *dmp)
692 ctf_close(dmp->dm_ctfp);
696 if (dmp->dm_ctdata.cts_data != NULL) {
697 free(dmp->dm_ctdata.cts_data);
699 if (dmp->dm_symtab.cts_data != NULL) {
700 free(dmp->dm_symtab.cts_data);
702 if (dmp->dm_strtab.cts_data != NULL) {
703 free(dmp->dm_strtab.cts_data);
707 bzero(&dmp->dm_ctdata, sizeof (ctf_sect_t));
708 bzero(&dmp->dm_symtab, sizeof (ctf_sect_t));
709 bzero(&dmp->dm_strtab, sizeof (ctf_sect_t));
711 if (dmp->dm_symbuckets != NULL) {
712 free(dmp->dm_symbuckets);
713 dmp->dm_symbuckets = NULL;
716 if (dmp->dm_symchains != NULL) {
717 free(dmp->dm_symchains);
718 dmp->dm_symchains = NULL;
721 if (dmp->dm_asmap != NULL) {
723 dmp->dm_asmap = NULL;
727 dmp->dm_nsymbuckets = 0;
728 dmp->dm_nsymelems = 0;
733 dmp->dm_text_size = 0;
735 dmp->dm_data_size = 0;
737 dmp->dm_bss_size = 0;
739 if (dmp->dm_extern != NULL) {
740 dt_idhash_destroy(dmp->dm_extern);
741 dmp->dm_extern = NULL;
744 (void) elf_end(dmp->dm_elf);
747 dmp->dm_flags &= ~DT_DM_LOADED;
751 dt_module_destroy(dtrace_hdl_t *dtp, dt_module_t *dmp)
753 dt_list_delete(&dtp->dt_modlist, dmp);
754 assert(dtp->dt_nmods != 0);
757 dt_module_unload(dtp, dmp);
762 * Insert a new external symbol reference into the specified module. The new
763 * symbol will be marked as undefined and is assigned a symbol index beyond
764 * any existing cached symbols from this module. We use the ident's di_data
765 * field to store a pointer to a copy of the dtrace_syminfo_t for this symbol.
768 dt_module_extern(dtrace_hdl_t *dtp, dt_module_t *dmp,
769 const char *name, const dtrace_typeinfo_t *tip)
771 dtrace_syminfo_t *sip;
775 if (dmp->dm_extern == NULL && (dmp->dm_extern = dt_idhash_create(
776 "extern", NULL, dmp->dm_nsymelems, UINT_MAX)) == NULL) {
777 (void) dt_set_errno(dtp, EDT_NOMEM);
781 if (dt_idhash_nextid(dmp->dm_extern, &id) == -1) {
782 (void) dt_set_errno(dtp, EDT_SYMOFLOW);
786 if ((sip = malloc(sizeof (dtrace_syminfo_t))) == NULL) {
787 (void) dt_set_errno(dtp, EDT_NOMEM);
791 idp = dt_idhash_insert(dmp->dm_extern, name, DT_IDENT_SYMBOL, 0, id,
792 _dtrace_symattr, 0, &dt_idops_thaw, NULL, dtp->dt_gen);
795 (void) dt_set_errno(dtp, EDT_NOMEM);
800 sip->dts_object = dmp->dm_name;
801 sip->dts_name = idp->di_name;
802 sip->dts_id = idp->di_id;
805 idp->di_ctfp = tip->dtt_ctfp;
806 idp->di_type = tip->dtt_type;
812 dt_module_modelname(dt_module_t *dmp)
814 if (dmp->dm_ops == &dt_modops_64)
821 * Update our module cache by adding an entry for the specified module 'name'.
822 * We create the dt_module_t and populate it using /system/object/<name>/.
824 * On FreeBSD, the module name is passed as the full module file name,
825 * including the path.
829 dt_module_update(dtrace_hdl_t *dtp, const char *name)
831 dt_module_update(dtrace_hdl_t *dtp, struct kld_file_stat *k_stat)
834 char fname[MAXPATHLEN];
846 (void) snprintf(fname, sizeof (fname),
847 "%s/%s/object", OBJFS_ROOT, name);
850 char name[MAXPATHLEN];
853 (void) strlcpy(name, k_stat->name, sizeof(name));
854 (void) strlcpy(fname, k_stat->pathname, sizeof(fname));
857 if ((fd = open(fname, O_RDONLY)) == -1 || fstat64(fd, &st) == -1 ||
858 (dmp = dt_module_create(dtp, name)) == NULL) {
859 dt_dprintf("failed to open %s: %s\n", fname, strerror(errno));
865 * Since the module can unload out from under us (and /system/object
866 * will return ENOENT), tell libelf to cook the entire file now and
867 * then close the underlying file descriptor immediately. If this
868 * succeeds, we know that we can continue safely using dmp->dm_elf.
870 dmp->dm_elf = elf_begin(fd, ELF_C_READ, NULL);
871 err = elf_cntl(dmp->dm_elf, ELF_C_FDREAD);
874 if (dmp->dm_elf == NULL || err == -1 ||
875 elf_getshstrndx(dmp->dm_elf, &shstrs) == 0) {
876 dt_dprintf("failed to load %s: %s\n",
877 fname, elf_errmsg(elf_errno()));
878 dt_module_destroy(dtp, dmp);
882 switch (gelf_getclass(dmp->dm_elf)) {
884 dmp->dm_ops = &dt_modops_32;
888 dmp->dm_ops = &dt_modops_64;
892 dt_dprintf("failed to load %s: unknown ELF class\n", fname);
893 dt_module_destroy(dtp, dmp);
898 * Iterate over the section headers locating various sections of
899 * interest and use their attributes to flesh out the dt_module_t.
901 for (sp = NULL; (sp = elf_nextscn(dmp->dm_elf, sp)) != NULL; ) {
902 if (gelf_getshdr(sp, &sh) == NULL || sh.sh_type == SHT_NULL ||
903 (s = elf_strptr(dmp->dm_elf, shstrs, sh.sh_name)) == NULL)
904 continue; /* skip any malformed sections */
906 if (strcmp(s, ".text") == 0) {
907 dmp->dm_text_size = sh.sh_size;
908 dmp->dm_text_va = sh.sh_addr;
909 } else if (strcmp(s, ".data") == 0) {
910 dmp->dm_data_size = sh.sh_size;
911 dmp->dm_data_va = sh.sh_addr;
912 } else if (strcmp(s, ".bss") == 0) {
913 dmp->dm_bss_size = sh.sh_size;
914 dmp->dm_bss_va = sh.sh_addr;
915 } else if (strcmp(s, ".info") == 0 &&
916 (dp = elf_getdata(sp, NULL)) != NULL) {
917 bcopy(dp->d_buf, &dmp->dm_info,
918 MIN(sh.sh_size, sizeof (dmp->dm_info)));
919 } else if (strcmp(s, ".filename") == 0 &&
920 (dp = elf_getdata(sp, NULL)) != NULL) {
921 (void) strlcpy(dmp->dm_file,
922 dp->d_buf, sizeof (dmp->dm_file));
926 dmp->dm_flags |= DT_DM_KERNEL;
928 dmp->dm_modid = (int)OBJFS_MODID(st.st_ino);
930 #if defined(__i386__)
932 * Find the first load section and figure out the relocation
933 * offset for the symbols. The kernel module will not need
934 * relocation, but the kernel linker modules will.
936 for (i = 0; gelf_getphdr(dmp->dm_elf, i, &ph) != NULL; i++) {
937 if (ph.p_type == PT_LOAD) {
938 dmp->dm_reloc_offset = k_stat->address - ph.p_vaddr;
945 if (dmp->dm_info.objfs_info_primary)
946 dmp->dm_flags |= DT_DM_PRIMARY;
948 dt_dprintf("opened %d-bit module %s (%s) [%d]\n",
949 bits, dmp->dm_name, dmp->dm_file, dmp->dm_modid);
953 * Unload all the loaded modules and then refresh the module cache with the
954 * latest list of loaded modules and their address ranges.
957 dtrace_update(dtrace_hdl_t *dtp)
961 #if defined(__FreeBSD__)
965 for (dmp = dt_list_next(&dtp->dt_modlist);
966 dmp != NULL; dmp = dt_list_next(dmp))
967 dt_module_unload(dtp, dmp);
971 * Open /system/object and attempt to create a libdtrace module for
972 * each kernel module that is loaded on the current system.
974 if (!(dtp->dt_oflags & DTRACE_O_NOSYS) &&
975 (dirp = opendir(OBJFS_ROOT)) != NULL) {
978 while ((dp = readdir(dirp)) != NULL) {
979 if (dp->d_name[0] != '.')
980 dt_module_update(dtp, dp->d_name);
983 (void) closedir(dirp);
985 #elif defined(__FreeBSD__)
987 * Use FreeBSD's kernel loader interface to discover what kernel
988 * modules are loaded and create a libdtrace module for each one.
990 for (fileid = kldnext(0); fileid > 0; fileid = kldnext(fileid)) {
991 struct kld_file_stat k_stat;
992 k_stat.version = sizeof(k_stat);
993 if (kldstat(fileid, &k_stat) == 0)
994 dt_module_update(dtp, &k_stat);
999 * Look up all the macro identifiers and set di_id to the latest value.
1000 * This code collaborates with dt_lex.l on the use of di_id. We will
1001 * need to implement something fancier if we need to support non-ints.
1003 dt_idhash_lookup(dtp->dt_macros, "egid")->di_id = getegid();
1004 dt_idhash_lookup(dtp->dt_macros, "euid")->di_id = geteuid();
1005 dt_idhash_lookup(dtp->dt_macros, "gid")->di_id = getgid();
1006 dt_idhash_lookup(dtp->dt_macros, "pid")->di_id = getpid();
1007 dt_idhash_lookup(dtp->dt_macros, "pgid")->di_id = getpgid(0);
1008 dt_idhash_lookup(dtp->dt_macros, "ppid")->di_id = getppid();
1010 dt_idhash_lookup(dtp->dt_macros, "projid")->di_id = getprojid();
1012 dt_idhash_lookup(dtp->dt_macros, "sid")->di_id = getsid(0);
1014 dt_idhash_lookup(dtp->dt_macros, "taskid")->di_id = gettaskid();
1016 dt_idhash_lookup(dtp->dt_macros, "uid")->di_id = getuid();
1019 * Cache the pointers to the modules representing the base executable
1020 * and the run-time linker in the dtrace client handle. Note that on
1021 * x86 krtld is folded into unix, so if we don't find it, use unix
1024 dtp->dt_exec = dt_module_lookup_by_name(dtp, "genunix");
1025 dtp->dt_rtld = dt_module_lookup_by_name(dtp, "krtld");
1026 if (dtp->dt_rtld == NULL)
1027 dtp->dt_rtld = dt_module_lookup_by_name(dtp, "unix");
1030 * If this is the first time we are initializing the module list,
1031 * remove the module for genunix from the module list and then move it
1032 * to the front of the module list. We do this so that type and symbol
1033 * queries encounter genunix and thereby optimize for the common case
1034 * in dtrace_lookup_by_name() and dtrace_lookup_by_type(), below.
1036 if (dtp->dt_exec != NULL &&
1037 dtp->dt_cdefs == NULL && dtp->dt_ddefs == NULL) {
1038 dt_list_delete(&dtp->dt_modlist, dtp->dt_exec);
1039 dt_list_prepend(&dtp->dt_modlist, dtp->dt_exec);
1043 static dt_module_t *
1044 dt_module_from_object(dtrace_hdl_t *dtp, const char *object)
1046 int err = EDT_NOMOD;
1049 switch ((uintptr_t)object) {
1050 case (uintptr_t)DTRACE_OBJ_EXEC:
1053 case (uintptr_t)DTRACE_OBJ_RTLD:
1056 case (uintptr_t)DTRACE_OBJ_CDEFS:
1057 dmp = dtp->dt_cdefs;
1059 case (uintptr_t)DTRACE_OBJ_DDEFS:
1060 dmp = dtp->dt_ddefs;
1063 dmp = dt_module_create(dtp, object);
1068 (void) dt_set_errno(dtp, err);
1074 * Exported interface to look up a symbol by name. We return the GElf_Sym and
1075 * complete symbol information for the matching symbol.
1078 dtrace_lookup_by_name(dtrace_hdl_t *dtp, const char *object, const char *name,
1079 GElf_Sym *symp, dtrace_syminfo_t *sip)
1086 uint_t mask = 0; /* mask of dt_module flags to match */
1087 uint_t bits = 0; /* flag bits that must be present */
1089 if (object != DTRACE_OBJ_EVERY &&
1090 object != DTRACE_OBJ_KMODS &&
1091 object != DTRACE_OBJ_UMODS) {
1092 if ((dmp = dt_module_from_object(dtp, object)) == NULL)
1093 return (-1); /* dt_errno is set for us */
1095 if (dt_module_load(dtp, dmp) == -1)
1096 return (-1); /* dt_errno is set for us */
1100 if (object == DTRACE_OBJ_KMODS)
1101 mask = bits = DT_DM_KERNEL;
1102 else if (object == DTRACE_OBJ_UMODS)
1103 mask = DT_DM_KERNEL;
1105 dmp = dt_list_next(&dtp->dt_modlist);
1112 for (; n > 0; n--, dmp = dt_list_next(dmp)) {
1113 if ((dmp->dm_flags & mask) != bits)
1114 continue; /* failed to match required attributes */
1116 if (dt_module_load(dtp, dmp) == -1)
1117 continue; /* failed to load symbol table */
1119 if (dmp->dm_ops->do_symname(dmp, name, symp, &id) != NULL) {
1121 sip->dts_object = dmp->dm_name;
1122 sip->dts_name = (const char *)
1123 dmp->dm_strtab.cts_data + symp->st_name;
1129 if (dmp->dm_extern != NULL &&
1130 (idp = dt_idhash_lookup(dmp->dm_extern, name)) != NULL) {
1132 symp->st_name = (uintptr_t)idp->di_name;
1134 GELF_ST_INFO(STB_GLOBAL, STT_NOTYPE);
1136 symp->st_shndx = SHN_UNDEF;
1139 ctf_type_size(idp->di_ctfp, idp->di_type);
1143 sip->dts_object = dmp->dm_name;
1144 sip->dts_name = idp->di_name;
1145 sip->dts_id = idp->di_id;
1152 return (dt_set_errno(dtp, EDT_NOSYM));
1156 * Exported interface to look up a symbol by address. We return the GElf_Sym
1157 * and complete symbol information for the matching symbol.
1160 dtrace_lookup_by_addr(dtrace_hdl_t *dtp, GElf_Addr addr,
1161 GElf_Sym *symp, dtrace_syminfo_t *sip)
1165 const dtrace_vector_t *v = dtp->dt_vector;
1168 return (v->dtv_lookup_by_addr(dtp->dt_varg, addr, symp, sip));
1170 for (dmp = dt_list_next(&dtp->dt_modlist); dmp != NULL;
1171 dmp = dt_list_next(dmp)) {
1172 if (addr - dmp->dm_text_va < dmp->dm_text_size ||
1173 addr - dmp->dm_data_va < dmp->dm_data_size ||
1174 addr - dmp->dm_bss_va < dmp->dm_bss_size)
1179 return (dt_set_errno(dtp, EDT_NOSYMADDR));
1181 if (dt_module_load(dtp, dmp) == -1)
1182 return (-1); /* dt_errno is set for us */
1185 if (dmp->dm_ops->do_symaddr(dmp, addr, symp, &id) == NULL)
1186 return (dt_set_errno(dtp, EDT_NOSYMADDR));
1190 sip->dts_object = dmp->dm_name;
1193 sip->dts_name = (const char *)
1194 dmp->dm_strtab.cts_data + symp->st_name;
1197 sip->dts_name = NULL;
1206 dtrace_lookup_by_type(dtrace_hdl_t *dtp, const char *object, const char *name,
1207 dtrace_typeinfo_t *tip)
1209 dtrace_typeinfo_t ti;
1216 uint_t mask = 0; /* mask of dt_module flags to match */
1217 uint_t bits = 0; /* flag bits that must be present */
1219 if (object != DTRACE_OBJ_EVERY &&
1220 object != DTRACE_OBJ_KMODS &&
1221 object != DTRACE_OBJ_UMODS) {
1222 if ((dmp = dt_module_from_object(dtp, object)) == NULL)
1223 return (-1); /* dt_errno is set for us */
1225 if (dt_module_load(dtp, dmp) == -1)
1226 return (-1); /* dt_errno is set for us */
1231 if (object == DTRACE_OBJ_KMODS)
1232 mask = bits = DT_DM_KERNEL;
1233 else if (object == DTRACE_OBJ_UMODS)
1234 mask = DT_DM_KERNEL;
1236 dmp = dt_list_next(&dtp->dt_modlist);
1244 for (; n > 0; n--, dmp = dt_list_next(dmp)) {
1245 if ((dmp->dm_flags & mask) != bits)
1246 continue; /* failed to match required attributes */
1249 * If we can't load the CTF container, continue on to the next
1250 * module. If our search was scoped to only one module then
1251 * return immediately leaving dt_errno unmodified.
1253 if (dt_module_getctf(dtp, dmp) == NULL) {
1260 * Look up the type in the module's CTF container. If our
1261 * match is a forward declaration tag, save this choice in
1262 * 'tip' and keep going in the hope that we will locate the
1263 * underlying structure definition. Otherwise just return.
1265 if ((id = ctf_lookup_by_name(dmp->dm_ctfp, name)) != CTF_ERR) {
1266 tip->dtt_object = dmp->dm_name;
1267 tip->dtt_ctfp = dmp->dm_ctfp;
1270 if (ctf_type_kind(dmp->dm_ctfp, ctf_type_resolve(
1271 dmp->dm_ctfp, id)) != CTF_K_FORWARD)
1279 return (dt_set_errno(dtp, EDT_NOTYPE));
1285 dtrace_symbol_type(dtrace_hdl_t *dtp, const GElf_Sym *symp,
1286 const dtrace_syminfo_t *sip, dtrace_typeinfo_t *tip)
1290 tip->dtt_object = NULL;
1291 tip->dtt_ctfp = NULL;
1292 tip->dtt_type = CTF_ERR;
1294 if ((dmp = dt_module_lookup_by_name(dtp, sip->dts_object)) == NULL)
1295 return (dt_set_errno(dtp, EDT_NOMOD));
1297 if (symp->st_shndx == SHN_UNDEF && dmp->dm_extern != NULL) {
1299 dt_idhash_lookup(dmp->dm_extern, sip->dts_name);
1302 return (dt_set_errno(dtp, EDT_NOSYM));
1304 tip->dtt_ctfp = idp->di_ctfp;
1305 tip->dtt_type = idp->di_type;
1307 } else if (GELF_ST_TYPE(symp->st_info) != STT_FUNC) {
1308 if (dt_module_getctf(dtp, dmp) == NULL)
1309 return (-1); /* errno is set for us */
1311 tip->dtt_ctfp = dmp->dm_ctfp;
1312 tip->dtt_type = ctf_lookup_by_symbol(dmp->dm_ctfp, sip->dts_id);
1314 if (tip->dtt_type == CTF_ERR) {
1315 dtp->dt_ctferr = ctf_errno(tip->dtt_ctfp);
1316 return (dt_set_errno(dtp, EDT_CTF));
1320 tip->dtt_ctfp = DT_FPTR_CTFP(dtp);
1321 tip->dtt_type = DT_FPTR_TYPE(dtp);
1324 tip->dtt_object = dmp->dm_name;
1328 static dtrace_objinfo_t *
1329 dt_module_info(const dt_module_t *dmp, dtrace_objinfo_t *dto)
1331 dto->dto_name = dmp->dm_name;
1332 dto->dto_file = dmp->dm_file;
1333 dto->dto_id = dmp->dm_modid;
1336 if (dmp->dm_flags & DT_DM_KERNEL)
1337 dto->dto_flags |= DTRACE_OBJ_F_KERNEL;
1338 if (dmp->dm_flags & DT_DM_PRIMARY)
1339 dto->dto_flags |= DTRACE_OBJ_F_PRIMARY;
1341 dto->dto_text_va = dmp->dm_text_va;
1342 dto->dto_text_size = dmp->dm_text_size;
1343 dto->dto_data_va = dmp->dm_data_va;
1344 dto->dto_data_size = dmp->dm_data_size;
1345 dto->dto_bss_va = dmp->dm_bss_va;
1346 dto->dto_bss_size = dmp->dm_bss_size;
1352 dtrace_object_iter(dtrace_hdl_t *dtp, dtrace_obj_f *func, void *data)
1354 const dt_module_t *dmp = dt_list_next(&dtp->dt_modlist);
1355 dtrace_objinfo_t dto;
1358 for (; dmp != NULL; dmp = dt_list_next(dmp)) {
1359 if ((rv = (*func)(dtp, dt_module_info(dmp, &dto), data)) != 0)
1367 dtrace_object_info(dtrace_hdl_t *dtp, const char *object, dtrace_objinfo_t *dto)
1371 if (object == DTRACE_OBJ_EVERY || object == DTRACE_OBJ_KMODS ||
1372 object == DTRACE_OBJ_UMODS || dto == NULL)
1373 return (dt_set_errno(dtp, EINVAL));
1375 if ((dmp = dt_module_from_object(dtp, object)) == NULL)
1376 return (-1); /* dt_errno is set for us */
1378 if (dt_module_load(dtp, dmp) == -1)
1379 return (-1); /* dt_errno is set for us */
1381 (void) dt_module_info(dmp, dto);