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>
28 #include <sys/modctl.h>
30 #include <sys/kobj_impl.h>
31 #include <sys/sysmacros.h>
35 #include <sys/param.h>
36 #include <sys/linker.h>
55 #include <dt_strtab.h>
56 #include <dt_module.h>
59 static const char *dt_module_strtab; /* active strtab for qsort callbacks */
62 dt_module_symhash_insert(dt_module_t *dmp, const char *name, uint_t id)
64 dt_sym_t *dsp = &dmp->dm_symchains[dmp->dm_symfree];
67 assert(dmp->dm_symfree < dmp->dm_nsymelems + 1);
70 h = dt_strtab_hash(name, NULL) % dmp->dm_nsymbuckets;
71 dsp->ds_next = dmp->dm_symbuckets[h];
72 dmp->dm_symbuckets[h] = dmp->dm_symfree++;
76 dt_module_syminit32(dt_module_t *dmp)
78 #if STT_NUM != (STT_TLS + 1)
79 #error "STT_NUM has grown. update dt_module_syminit32()"
82 Elf32_Sym *sym = dmp->dm_symtab.cts_data;
83 const char *base = dmp->dm_strtab.cts_data;
84 size_t ss_size = dmp->dm_strtab.cts_size;
85 uint_t i, n = dmp->dm_nsymelems;
88 #if defined(__FreeBSD__)
92 gelf_getehdr(dmp->dm_elf, &ehdr);
93 is_elf_obj = (ehdr.e_type == ET_REL);
96 for (i = 0; i < n; i++, sym++) {
97 const char *name = base + sym->st_name;
98 uchar_t type = ELF32_ST_TYPE(sym->st_info);
100 if (type >= STT_NUM || type == STT_SECTION)
101 continue; /* skip sections and unknown types */
103 if (sym->st_name == 0 || sym->st_name >= ss_size)
104 continue; /* skip null or invalid names */
106 if (sym->st_value != 0 &&
107 (ELF32_ST_BIND(sym->st_info) != STB_LOCAL || sym->st_size)) {
108 asrsv++; /* reserve space in the address map */
110 #if defined(__FreeBSD__)
111 sym->st_value += (Elf_Addr) dmp->dm_reloc_offset;
112 if (is_elf_obj && sym->st_shndx != SHN_UNDEF &&
113 sym->st_shndx < ehdr.e_shnum)
115 dmp->dm_sec_offsets[sym->st_shndx];
119 dt_module_symhash_insert(dmp, name, i);
126 dt_module_syminit64(dt_module_t *dmp)
128 #if STT_NUM != (STT_TLS + 1)
129 #error "STT_NUM has grown. update dt_module_syminit64()"
132 Elf64_Sym *sym = dmp->dm_symtab.cts_data;
133 const char *base = dmp->dm_strtab.cts_data;
134 size_t ss_size = dmp->dm_strtab.cts_size;
135 uint_t i, n = dmp->dm_nsymelems;
138 #if defined(__FreeBSD__)
142 gelf_getehdr(dmp->dm_elf, &ehdr);
143 is_elf_obj = (ehdr.e_type == ET_REL);
146 for (i = 0; i < n; i++, sym++) {
147 const char *name = base + sym->st_name;
148 uchar_t type = ELF64_ST_TYPE(sym->st_info);
150 if (type >= STT_NUM || type == STT_SECTION)
151 continue; /* skip sections and unknown types */
153 if (sym->st_name == 0 || sym->st_name >= ss_size)
154 continue; /* skip null or invalid names */
156 if (sym->st_value != 0 &&
157 (ELF64_ST_BIND(sym->st_info) != STB_LOCAL || sym->st_size)) {
158 asrsv++; /* reserve space in the address map */
159 #if defined(__FreeBSD__)
160 sym->st_value += (Elf_Addr) dmp->dm_reloc_offset;
161 if (is_elf_obj && sym->st_shndx != SHN_UNDEF &&
162 sym->st_shndx < ehdr.e_shnum)
164 dmp->dm_sec_offsets[sym->st_shndx];
168 dt_module_symhash_insert(dmp, name, i);
175 * Sort comparison function for 32-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_symcomp32(const void *lp, const void *rp)
182 Elf32_Sym *lhs = *((Elf32_Sym **)lp);
183 Elf32_Sym *rhs = *((Elf32_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 ((ELF32_ST_TYPE(lhs->st_info) == STT_NOTYPE) !=
192 (ELF32_ST_TYPE(rhs->st_info) == STT_NOTYPE))
193 return (ELF32_ST_TYPE(lhs->st_info) == STT_NOTYPE ? 1 : -1);
195 if ((ELF32_ST_BIND(lhs->st_info) == STB_WEAK) !=
196 (ELF32_ST_BIND(rhs->st_info) == STB_WEAK))
197 return (ELF32_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 * Sort comparison function for 64-bit symbol address-to-name lookups. We sort
205 * symbols by value. If values are equal, we prefer the symbol that is
206 * non-zero sized, typed, not weak, or lexically first, in that order.
209 dt_module_symcomp64(const void *lp, const void *rp)
211 Elf64_Sym *lhs = *((Elf64_Sym **)lp);
212 Elf64_Sym *rhs = *((Elf64_Sym **)rp);
214 if (lhs->st_value != rhs->st_value)
215 return (lhs->st_value > rhs->st_value ? 1 : -1);
217 if ((lhs->st_size == 0) != (rhs->st_size == 0))
218 return (lhs->st_size == 0 ? 1 : -1);
220 if ((ELF64_ST_TYPE(lhs->st_info) == STT_NOTYPE) !=
221 (ELF64_ST_TYPE(rhs->st_info) == STT_NOTYPE))
222 return (ELF64_ST_TYPE(lhs->st_info) == STT_NOTYPE ? 1 : -1);
224 if ((ELF64_ST_BIND(lhs->st_info) == STB_WEAK) !=
225 (ELF64_ST_BIND(rhs->st_info) == STB_WEAK))
226 return (ELF64_ST_BIND(lhs->st_info) == STB_WEAK ? 1 : -1);
228 return (strcmp(dt_module_strtab + lhs->st_name,
229 dt_module_strtab + rhs->st_name));
233 dt_module_symsort32(dt_module_t *dmp)
235 Elf32_Sym *symtab = (Elf32_Sym *)dmp->dm_symtab.cts_data;
236 Elf32_Sym **sympp = (Elf32_Sym **)dmp->dm_asmap;
237 const dt_sym_t *dsp = dmp->dm_symchains + 1;
238 uint_t i, n = dmp->dm_symfree;
240 for (i = 1; i < n; i++, dsp++) {
241 Elf32_Sym *sym = symtab + dsp->ds_symid;
242 if (sym->st_value != 0 &&
243 (ELF32_ST_BIND(sym->st_info) != STB_LOCAL || sym->st_size))
247 dmp->dm_aslen = (uint_t)(sympp - (Elf32_Sym **)dmp->dm_asmap);
248 assert(dmp->dm_aslen <= dmp->dm_asrsv);
250 dt_module_strtab = dmp->dm_strtab.cts_data;
251 qsort(dmp->dm_asmap, dmp->dm_aslen,
252 sizeof (Elf32_Sym *), dt_module_symcomp32);
253 dt_module_strtab = NULL;
257 dt_module_symsort64(dt_module_t *dmp)
259 Elf64_Sym *symtab = (Elf64_Sym *)dmp->dm_symtab.cts_data;
260 Elf64_Sym **sympp = (Elf64_Sym **)dmp->dm_asmap;
261 const dt_sym_t *dsp = dmp->dm_symchains + 1;
262 uint_t i, n = dmp->dm_symfree;
264 for (i = 1; i < n; i++, dsp++) {
265 Elf64_Sym *sym = symtab + dsp->ds_symid;
266 if (sym->st_value != 0 &&
267 (ELF64_ST_BIND(sym->st_info) != STB_LOCAL || sym->st_size))
271 dmp->dm_aslen = (uint_t)(sympp - (Elf64_Sym **)dmp->dm_asmap);
272 assert(dmp->dm_aslen <= dmp->dm_asrsv);
274 dt_module_strtab = dmp->dm_strtab.cts_data;
275 qsort(dmp->dm_asmap, dmp->dm_aslen,
276 sizeof (Elf64_Sym *), dt_module_symcomp64);
277 dt_module_strtab = NULL;
281 dt_module_symgelf32(const Elf32_Sym *src, GElf_Sym *dst)
284 dst->st_name = src->st_name;
285 dst->st_info = src->st_info;
286 dst->st_other = src->st_other;
287 dst->st_shndx = src->st_shndx;
288 dst->st_value = src->st_value;
289 dst->st_size = src->st_size;
296 dt_module_symgelf64(const Elf64_Sym *src, GElf_Sym *dst)
299 bcopy(src, dst, sizeof (GElf_Sym));
305 dt_module_symname32(dt_module_t *dmp, const char *name,
306 GElf_Sym *symp, uint_t *idp)
308 const Elf32_Sym *symtab = dmp->dm_symtab.cts_data;
309 const char *strtab = dmp->dm_strtab.cts_data;
311 const Elf32_Sym *sym;
315 if (dmp->dm_nsymelems == 0)
318 h = dt_strtab_hash(name, NULL) % dmp->dm_nsymbuckets;
320 for (i = dmp->dm_symbuckets[h]; i != 0; i = dsp->ds_next) {
321 dsp = &dmp->dm_symchains[i];
322 sym = symtab + dsp->ds_symid;
324 if (strcmp(name, strtab + sym->st_name) == 0) {
326 *idp = dsp->ds_symid;
327 return (dt_module_symgelf32(sym, symp));
335 dt_module_symname64(dt_module_t *dmp, const char *name,
336 GElf_Sym *symp, uint_t *idp)
338 const Elf64_Sym *symtab = dmp->dm_symtab.cts_data;
339 const char *strtab = dmp->dm_strtab.cts_data;
341 const Elf64_Sym *sym;
345 if (dmp->dm_nsymelems == 0)
348 h = dt_strtab_hash(name, NULL) % dmp->dm_nsymbuckets;
350 for (i = dmp->dm_symbuckets[h]; i != 0; i = dsp->ds_next) {
351 dsp = &dmp->dm_symchains[i];
352 sym = symtab + dsp->ds_symid;
354 if (strcmp(name, strtab + sym->st_name) == 0) {
356 *idp = dsp->ds_symid;
357 return (dt_module_symgelf64(sym, symp));
365 dt_module_symaddr32(dt_module_t *dmp, GElf_Addr addr,
366 GElf_Sym *symp, uint_t *idp)
368 const Elf32_Sym **asmap = (const Elf32_Sym **)dmp->dm_asmap;
369 const Elf32_Sym *symtab = dmp->dm_symtab.cts_data;
370 const Elf32_Sym *sym;
372 uint_t i, mid, lo = 0, hi = dmp->dm_aslen - 1;
375 if (dmp->dm_aslen == 0)
378 while (hi - lo > 1) {
380 if (addr >= asmap[mid]->st_value)
386 i = addr < asmap[hi]->st_value ? lo : hi;
391 * If the previous entry has the same value, improve our choice. The
392 * order of equal-valued symbols is determined by the comparison func.
394 while (i-- != 0 && asmap[i]->st_value == v)
397 if (addr - sym->st_value < MAX(sym->st_size, 1)) {
399 *idp = (uint_t)(sym - symtab);
400 return (dt_module_symgelf32(sym, symp));
407 dt_module_symaddr64(dt_module_t *dmp, GElf_Addr addr,
408 GElf_Sym *symp, uint_t *idp)
410 const Elf64_Sym **asmap = (const Elf64_Sym **)dmp->dm_asmap;
411 const Elf64_Sym *symtab = dmp->dm_symtab.cts_data;
412 const Elf64_Sym *sym;
414 uint_t i, mid, lo = 0, hi = dmp->dm_aslen - 1;
417 if (dmp->dm_aslen == 0)
420 while (hi - lo > 1) {
422 if (addr >= asmap[mid]->st_value)
428 i = addr < asmap[hi]->st_value ? lo : hi;
433 * If the previous entry has the same value, improve our choice. The
434 * order of equal-valued symbols is determined by the comparison func.
436 while (i-- != 0 && asmap[i]->st_value == v)
439 if (addr - sym->st_value < MAX(sym->st_size, 1)) {
441 *idp = (uint_t)(sym - symtab);
442 return (dt_module_symgelf64(sym, symp));
448 static const dt_modops_t dt_modops_32 = {
455 static const dt_modops_t dt_modops_64 = {
463 dt_module_create(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)
473 if ((dmp = malloc(sizeof (dt_module_t))) == NULL)
474 return (NULL); /* caller must handle allocation failure */
476 bzero(dmp, sizeof (dt_module_t));
477 (void) strlcpy(dmp->dm_name, name, sizeof (dmp->dm_name));
478 dt_list_append(&dtp->dt_modlist, dmp);
479 dmp->dm_next = dtp->dt_mods[h];
480 dtp->dt_mods[h] = dmp;
483 if (dtp->dt_conf.dtc_ctfmodel == CTF_MODEL_LP64)
484 dmp->dm_ops = &dt_modops_64;
486 dmp->dm_ops = &dt_modops_32;
492 dt_module_lookup_by_name(dtrace_hdl_t *dtp, const char *name)
494 uint_t h = dt_strtab_hash(name, NULL) % dtp->dt_modbuckets;
497 for (dmp = dtp->dt_mods[h]; dmp != NULL; dmp = dmp->dm_next) {
498 if (strcmp(dmp->dm_name, name) == 0)
507 dt_module_lookup_by_ctf(dtrace_hdl_t *dtp, ctf_file_t *ctfp)
509 return (ctfp ? ctf_getspecific(ctfp) : NULL);
513 dt_module_load_sect(dtrace_hdl_t *dtp, dt_module_t *dmp, ctf_sect_t *ctsp)
521 if (elf_getshdrstrndx(dmp->dm_elf, &shstrs) == -1)
522 return (dt_set_errno(dtp, EDT_NOTLOADED));
524 for (sp = NULL; (sp = elf_nextscn(dmp->dm_elf, sp)) != NULL; ) {
525 if (gelf_getshdr(sp, &sh) == NULL || sh.sh_type == SHT_NULL ||
526 (s = elf_strptr(dmp->dm_elf, shstrs, sh.sh_name)) == NULL)
527 continue; /* skip any malformed sections */
529 if (sh.sh_type == ctsp->cts_type &&
530 sh.sh_entsize == ctsp->cts_entsize &&
531 strcmp(s, ctsp->cts_name) == 0)
532 break; /* section matches specification */
536 * If the section isn't found, return success but leave cts_data set
537 * to NULL and cts_size set to zero for our caller.
539 if (sp == NULL || (dp = elf_getdata(sp, NULL)) == NULL)
543 ctsp->cts_data = dp->d_buf;
545 if ((ctsp->cts_data = malloc(dp->d_size)) == NULL)
547 memcpy(ctsp->cts_data, dp->d_buf, dp->d_size);
549 ctsp->cts_size = dp->d_size;
551 dt_dprintf("loaded %s [%s] (%lu bytes)\n",
552 dmp->dm_name, ctsp->cts_name, (ulong_t)ctsp->cts_size);
558 dt_module_load(dtrace_hdl_t *dtp, dt_module_t *dmp)
560 if (dmp->dm_flags & DT_DM_LOADED)
561 return (0); /* module is already loaded */
563 dmp->dm_ctdata.cts_name = ".SUNW_ctf";
564 dmp->dm_ctdata.cts_type = SHT_PROGBITS;
565 dmp->dm_ctdata.cts_flags = 0;
566 dmp->dm_ctdata.cts_data = NULL;
567 dmp->dm_ctdata.cts_size = 0;
568 dmp->dm_ctdata.cts_entsize = 0;
569 dmp->dm_ctdata.cts_offset = 0;
571 dmp->dm_symtab.cts_name = ".symtab";
572 dmp->dm_symtab.cts_type = SHT_SYMTAB;
573 dmp->dm_symtab.cts_flags = 0;
574 dmp->dm_symtab.cts_data = NULL;
575 dmp->dm_symtab.cts_size = 0;
576 dmp->dm_symtab.cts_entsize = dmp->dm_ops == &dt_modops_64 ?
577 sizeof (Elf64_Sym) : sizeof (Elf32_Sym);
578 dmp->dm_symtab.cts_offset = 0;
580 dmp->dm_strtab.cts_name = ".strtab";
581 dmp->dm_strtab.cts_type = SHT_STRTAB;
582 dmp->dm_strtab.cts_flags = 0;
583 dmp->dm_strtab.cts_data = NULL;
584 dmp->dm_strtab.cts_size = 0;
585 dmp->dm_strtab.cts_entsize = 0;
586 dmp->dm_strtab.cts_offset = 0;
589 * Attempt to load the module's CTF section, symbol table section, and
590 * string table section. Note that modules may not contain CTF data:
591 * this will result in a successful load_sect but data of size zero.
592 * We will then fail if dt_module_getctf() is called, as shown below.
594 if (dt_module_load_sect(dtp, dmp, &dmp->dm_ctdata) == -1 ||
595 dt_module_load_sect(dtp, dmp, &dmp->dm_symtab) == -1 ||
596 dt_module_load_sect(dtp, dmp, &dmp->dm_strtab) == -1) {
597 dt_module_unload(dtp, dmp);
598 return (-1); /* dt_errno is set for us */
602 * Allocate the hash chains and hash buckets for symbol name lookup.
603 * This is relatively simple since the symbol table is of fixed size
604 * and is known in advance. We allocate one extra element since we
605 * use element indices instead of pointers and zero is our sentinel.
608 dmp->dm_symtab.cts_size / dmp->dm_symtab.cts_entsize;
610 dmp->dm_nsymbuckets = _dtrace_strbuckets;
611 dmp->dm_symfree = 1; /* first free element is index 1 */
613 dmp->dm_symbuckets = malloc(sizeof (uint_t) * dmp->dm_nsymbuckets);
614 dmp->dm_symchains = malloc(sizeof (dt_sym_t) * dmp->dm_nsymelems + 1);
616 if (dmp->dm_symbuckets == NULL || dmp->dm_symchains == NULL) {
617 dt_module_unload(dtp, dmp);
618 return (dt_set_errno(dtp, EDT_NOMEM));
621 bzero(dmp->dm_symbuckets, sizeof (uint_t) * dmp->dm_nsymbuckets);
622 bzero(dmp->dm_symchains, sizeof (dt_sym_t) * dmp->dm_nsymelems + 1);
625 * Iterate over the symbol table data buffer and insert each symbol
626 * name into the name hash if the name and type are valid. Then
627 * allocate the address map, fill it in, and sort it.
629 dmp->dm_asrsv = dmp->dm_ops->do_syminit(dmp);
631 dt_dprintf("hashed %s [%s] (%u symbols)\n",
632 dmp->dm_name, dmp->dm_symtab.cts_name, dmp->dm_symfree - 1);
634 if ((dmp->dm_asmap = malloc(sizeof (void *) * dmp->dm_asrsv)) == NULL) {
635 dt_module_unload(dtp, dmp);
636 return (dt_set_errno(dtp, EDT_NOMEM));
639 dmp->dm_ops->do_symsort(dmp);
641 dt_dprintf("sorted %s [%s] (%u symbols)\n",
642 dmp->dm_name, dmp->dm_symtab.cts_name, dmp->dm_aslen);
644 dmp->dm_flags |= DT_DM_LOADED;
649 dt_module_getctf(dtrace_hdl_t *dtp, dt_module_t *dmp)
656 if (dmp->dm_ctfp != NULL || dt_module_load(dtp, dmp) != 0)
657 return (dmp->dm_ctfp);
659 if (dmp->dm_ops == &dt_modops_64)
660 model = CTF_MODEL_LP64;
662 model = CTF_MODEL_ILP32;
665 * If the data model of the module does not match our program data
666 * model, then do not permit CTF from this module to be opened and
667 * returned to the compiler. If we support mixed data models in the
668 * future for combined kernel/user tracing, this can be removed.
670 if (dtp->dt_conf.dtc_ctfmodel != model) {
671 (void) dt_set_errno(dtp, EDT_DATAMODEL);
675 if (dmp->dm_ctdata.cts_size == 0) {
676 (void) dt_set_errno(dtp, EDT_NOCTF);
680 dmp->dm_ctfp = ctf_bufopen(&dmp->dm_ctdata,
681 &dmp->dm_symtab, &dmp->dm_strtab, &dtp->dt_ctferr);
683 if (dmp->dm_ctfp == NULL) {
684 (void) dt_set_errno(dtp, EDT_CTF);
688 (void) ctf_setmodel(dmp->dm_ctfp, model);
689 ctf_setspecific(dmp->dm_ctfp, dmp);
691 if ((parent = ctf_parent_name(dmp->dm_ctfp)) != NULL) {
692 if ((pmp = dt_module_create(dtp, parent)) == NULL ||
693 (pfp = dt_module_getctf(dtp, pmp)) == NULL) {
695 (void) dt_set_errno(dtp, EDT_NOMEM);
699 if (ctf_import(dmp->dm_ctfp, pfp) == CTF_ERR) {
700 dtp->dt_ctferr = ctf_errno(dmp->dm_ctfp);
701 (void) dt_set_errno(dtp, EDT_CTF);
706 dt_dprintf("loaded CTF container for %s (%p)\n",
707 dmp->dm_name, (void *)dmp->dm_ctfp);
709 return (dmp->dm_ctfp);
712 ctf_close(dmp->dm_ctfp);
719 dt_module_unload(dtrace_hdl_t *dtp, dt_module_t *dmp)
721 ctf_close(dmp->dm_ctfp);
725 if (dmp->dm_ctdata.cts_data != NULL) {
726 free(dmp->dm_ctdata.cts_data);
728 if (dmp->dm_symtab.cts_data != NULL) {
729 free(dmp->dm_symtab.cts_data);
731 if (dmp->dm_strtab.cts_data != NULL) {
732 free(dmp->dm_strtab.cts_data);
736 bzero(&dmp->dm_ctdata, sizeof (ctf_sect_t));
737 bzero(&dmp->dm_symtab, sizeof (ctf_sect_t));
738 bzero(&dmp->dm_strtab, sizeof (ctf_sect_t));
740 if (dmp->dm_symbuckets != NULL) {
741 free(dmp->dm_symbuckets);
742 dmp->dm_symbuckets = NULL;
745 if (dmp->dm_symchains != NULL) {
746 free(dmp->dm_symchains);
747 dmp->dm_symchains = NULL;
750 if (dmp->dm_asmap != NULL) {
752 dmp->dm_asmap = NULL;
754 #if defined(__FreeBSD__)
755 if (dmp->dm_sec_offsets != NULL) {
756 free(dmp->dm_sec_offsets);
757 dmp->dm_sec_offsets = NULL;
761 dmp->dm_nsymbuckets = 0;
762 dmp->dm_nsymelems = 0;
767 dmp->dm_text_size = 0;
769 dmp->dm_data_size = 0;
771 dmp->dm_bss_size = 0;
773 if (dmp->dm_extern != NULL) {
774 dt_idhash_destroy(dmp->dm_extern);
775 dmp->dm_extern = NULL;
778 (void) elf_end(dmp->dm_elf);
781 dmp->dm_flags &= ~DT_DM_LOADED;
785 dt_module_destroy(dtrace_hdl_t *dtp, dt_module_t *dmp)
787 uint_t h = dt_strtab_hash(dmp->dm_name, NULL) % dtp->dt_modbuckets;
788 dt_module_t **dmpp = &dtp->dt_mods[h];
790 dt_list_delete(&dtp->dt_modlist, dmp);
791 assert(dtp->dt_nmods != 0);
795 * Now remove this module from its hash chain. We expect to always
796 * find the module on its hash chain, so in this loop we assert that
797 * we don't run off the end of the list.
799 while (*dmpp != dmp) {
800 dmpp = &((*dmpp)->dm_next);
801 assert(*dmpp != NULL);
804 *dmpp = dmp->dm_next;
806 dt_module_unload(dtp, dmp);
811 * Insert a new external symbol reference into the specified module. The new
812 * symbol will be marked as undefined and is assigned a symbol index beyond
813 * any existing cached symbols from this module. We use the ident's di_data
814 * field to store a pointer to a copy of the dtrace_syminfo_t for this symbol.
817 dt_module_extern(dtrace_hdl_t *dtp, dt_module_t *dmp,
818 const char *name, const dtrace_typeinfo_t *tip)
820 dtrace_syminfo_t *sip;
824 if (dmp->dm_extern == NULL && (dmp->dm_extern = dt_idhash_create(
825 "extern", NULL, dmp->dm_nsymelems, UINT_MAX)) == NULL) {
826 (void) dt_set_errno(dtp, EDT_NOMEM);
830 if (dt_idhash_nextid(dmp->dm_extern, &id) == -1) {
831 (void) dt_set_errno(dtp, EDT_SYMOFLOW);
835 if ((sip = malloc(sizeof (dtrace_syminfo_t))) == NULL) {
836 (void) dt_set_errno(dtp, EDT_NOMEM);
840 idp = dt_idhash_insert(dmp->dm_extern, name, DT_IDENT_SYMBOL, 0, id,
841 _dtrace_symattr, 0, &dt_idops_thaw, NULL, dtp->dt_gen);
844 (void) dt_set_errno(dtp, EDT_NOMEM);
849 sip->dts_object = dmp->dm_name;
850 sip->dts_name = idp->di_name;
851 sip->dts_id = idp->di_id;
854 idp->di_ctfp = tip->dtt_ctfp;
855 idp->di_type = tip->dtt_type;
861 dt_module_modelname(dt_module_t *dmp)
863 if (dmp->dm_ops == &dt_modops_64)
870 * Update our module cache by adding an entry for the specified module 'name'.
871 * We create the dt_module_t and populate it using /system/object/<name>/.
873 * On FreeBSD, the module name is passed as the full module file name,
874 * including the path.
878 dt_module_update(dtrace_hdl_t *dtp, const char *name)
880 dt_module_update(dtrace_hdl_t *dtp, struct kld_file_stat *k_stat)
883 char fname[MAXPATHLEN];
895 (void) snprintf(fname, sizeof (fname),
896 "%s/%s/object", OBJFS_ROOT, name);
900 char name[MAXPATHLEN];
901 uintptr_t mapbase, alignmask;
905 (void) strlcpy(name, k_stat->name, sizeof(name));
906 (void) strlcpy(fname, k_stat->pathname, sizeof(fname));
909 if ((fd = open(fname, O_RDONLY)) == -1 || fstat64(fd, &st) == -1 ||
910 (dmp = dt_module_create(dtp, name)) == NULL) {
911 dt_dprintf("failed to open %s: %s\n", fname, strerror(errno));
917 * Since the module can unload out from under us (and /system/object
918 * will return ENOENT), tell libelf to cook the entire file now and
919 * then close the underlying file descriptor immediately. If this
920 * succeeds, we know that we can continue safely using dmp->dm_elf.
922 dmp->dm_elf = elf_begin(fd, ELF_C_READ, NULL);
923 err = elf_cntl(dmp->dm_elf, ELF_C_FDREAD);
926 if (dmp->dm_elf == NULL || err == -1 ||
927 elf_getshdrstrndx(dmp->dm_elf, &shstrs) == -1) {
928 dt_dprintf("failed to load %s: %s\n",
929 fname, elf_errmsg(elf_errno()));
930 dt_module_destroy(dtp, dmp);
934 switch (gelf_getclass(dmp->dm_elf)) {
936 dmp->dm_ops = &dt_modops_32;
940 dmp->dm_ops = &dt_modops_64;
944 dt_dprintf("failed to load %s: unknown ELF class\n", fname);
945 dt_module_destroy(dtp, dmp);
948 #if defined(__FreeBSD__)
949 mapbase = (uintptr_t)k_stat->address;
950 gelf_getehdr(dmp->dm_elf, &ehdr);
951 is_elf_obj = (ehdr.e_type == ET_REL);
953 dmp->dm_sec_offsets =
954 malloc(ehdr.e_shnum * sizeof(*dmp->dm_sec_offsets));
955 if (dmp->dm_sec_offsets == NULL) {
956 dt_dprintf("failed to allocate memory\n");
957 dt_module_destroy(dtp, dmp);
963 * Iterate over the section headers locating various sections of
964 * interest and use their attributes to flesh out the dt_module_t.
966 for (sp = NULL; (sp = elf_nextscn(dmp->dm_elf, sp)) != NULL; ) {
967 if (gelf_getshdr(sp, &sh) == NULL || sh.sh_type == SHT_NULL ||
968 (s = elf_strptr(dmp->dm_elf, shstrs, sh.sh_name)) == NULL)
969 continue; /* skip any malformed sections */
970 #if defined(__FreeBSD__)
973 if (is_elf_obj && (sh.sh_type == SHT_PROGBITS ||
974 sh.sh_type == SHT_NOBITS)) {
975 alignmask = sh.sh_addralign - 1;
976 mapbase += alignmask;
977 mapbase &= ~alignmask;
978 sh.sh_addr = mapbase;
979 dmp->dm_sec_offsets[elf_ndxscn(sp)] = sh.sh_addr;
980 mapbase += sh.sh_size;
983 if (strcmp(s, ".text") == 0) {
984 dmp->dm_text_size = sh.sh_size;
985 dmp->dm_text_va = sh.sh_addr;
986 } else if (strcmp(s, ".data") == 0) {
987 dmp->dm_data_size = sh.sh_size;
988 dmp->dm_data_va = sh.sh_addr;
989 } else if (strcmp(s, ".bss") == 0) {
990 dmp->dm_bss_size = sh.sh_size;
991 dmp->dm_bss_va = sh.sh_addr;
992 } else if (strcmp(s, ".info") == 0 &&
993 (dp = elf_getdata(sp, NULL)) != NULL) {
994 bcopy(dp->d_buf, &dmp->dm_info,
995 MIN(sh.sh_size, sizeof (dmp->dm_info)));
996 } else if (strcmp(s, ".filename") == 0 &&
997 (dp = elf_getdata(sp, NULL)) != NULL) {
998 (void) strlcpy(dmp->dm_file,
999 dp->d_buf, sizeof (dmp->dm_file));
1003 dmp->dm_flags |= DT_DM_KERNEL;
1005 dmp->dm_modid = (int)OBJFS_MODID(st.st_ino);
1008 * Include .rodata and special sections into .text.
1009 * This depends on default section layout produced by GNU ld
1010 * for ELF objects and libraries:
1011 * [Text][R/O data][R/W data][Dynamic][BSS][Non loadable]
1013 dmp->dm_text_size = dmp->dm_data_va - dmp->dm_text_va;
1014 #if defined(__i386__)
1016 * Find the first load section and figure out the relocation
1017 * offset for the symbols. The kernel module will not need
1018 * relocation, but the kernel linker modules will.
1020 for (i = 0; gelf_getphdr(dmp->dm_elf, i, &ph) != NULL; i++) {
1021 if (ph.p_type == PT_LOAD) {
1022 dmp->dm_reloc_offset = k_stat->address - ph.p_vaddr;
1029 if (dmp->dm_info.objfs_info_primary)
1030 dmp->dm_flags |= DT_DM_PRIMARY;
1032 dt_dprintf("opened %d-bit module %s (%s) [%d]\n",
1033 bits, dmp->dm_name, dmp->dm_file, dmp->dm_modid);
1037 * Unload all the loaded modules and then refresh the module cache with the
1038 * latest list of loaded modules and their address ranges.
1041 dtrace_update(dtrace_hdl_t *dtp)
1045 #if defined(__FreeBSD__)
1049 for (dmp = dt_list_next(&dtp->dt_modlist);
1050 dmp != NULL; dmp = dt_list_next(dmp))
1051 dt_module_unload(dtp, dmp);
1055 * Open /system/object and attempt to create a libdtrace module for
1056 * each kernel module that is loaded on the current system.
1058 if (!(dtp->dt_oflags & DTRACE_O_NOSYS) &&
1059 (dirp = opendir(OBJFS_ROOT)) != NULL) {
1062 while ((dp = readdir(dirp)) != NULL) {
1063 if (dp->d_name[0] != '.')
1064 dt_module_update(dtp, dp->d_name);
1067 (void) closedir(dirp);
1069 #elif defined(__FreeBSD__)
1071 * Use FreeBSD's kernel loader interface to discover what kernel
1072 * modules are loaded and create a libdtrace module for each one.
1074 for (fileid = kldnext(0); fileid > 0; fileid = kldnext(fileid)) {
1075 struct kld_file_stat k_stat;
1076 k_stat.version = sizeof(k_stat);
1077 if (kldstat(fileid, &k_stat) == 0)
1078 dt_module_update(dtp, &k_stat);
1083 * Look up all the macro identifiers and set di_id to the latest value.
1084 * This code collaborates with dt_lex.l on the use of di_id. We will
1085 * need to implement something fancier if we need to support non-ints.
1087 dt_idhash_lookup(dtp->dt_macros, "egid")->di_id = getegid();
1088 dt_idhash_lookup(dtp->dt_macros, "euid")->di_id = geteuid();
1089 dt_idhash_lookup(dtp->dt_macros, "gid")->di_id = getgid();
1090 dt_idhash_lookup(dtp->dt_macros, "pid")->di_id = getpid();
1091 dt_idhash_lookup(dtp->dt_macros, "pgid")->di_id = getpgid(0);
1092 dt_idhash_lookup(dtp->dt_macros, "ppid")->di_id = getppid();
1094 dt_idhash_lookup(dtp->dt_macros, "projid")->di_id = getprojid();
1096 dt_idhash_lookup(dtp->dt_macros, "sid")->di_id = getsid(0);
1098 dt_idhash_lookup(dtp->dt_macros, "taskid")->di_id = gettaskid();
1100 dt_idhash_lookup(dtp->dt_macros, "uid")->di_id = getuid();
1103 * Cache the pointers to the modules representing the base executable
1104 * and the run-time linker in the dtrace client handle. Note that on
1105 * x86 krtld is folded into unix, so if we don't find it, use unix
1108 dtp->dt_exec = dt_module_lookup_by_name(dtp, "genunix");
1109 dtp->dt_rtld = dt_module_lookup_by_name(dtp, "krtld");
1110 if (dtp->dt_rtld == NULL)
1111 dtp->dt_rtld = dt_module_lookup_by_name(dtp, "unix");
1114 * If this is the first time we are initializing the module list,
1115 * remove the module for genunix from the module list and then move it
1116 * to the front of the module list. We do this so that type and symbol
1117 * queries encounter genunix and thereby optimize for the common case
1118 * in dtrace_lookup_by_name() and dtrace_lookup_by_type(), below.
1120 if (dtp->dt_exec != NULL &&
1121 dtp->dt_cdefs == NULL && dtp->dt_ddefs == NULL) {
1122 dt_list_delete(&dtp->dt_modlist, dtp->dt_exec);
1123 dt_list_prepend(&dtp->dt_modlist, dtp->dt_exec);
1127 static dt_module_t *
1128 dt_module_from_object(dtrace_hdl_t *dtp, const char *object)
1130 int err = EDT_NOMOD;
1133 switch ((uintptr_t)object) {
1134 case (uintptr_t)DTRACE_OBJ_EXEC:
1137 case (uintptr_t)DTRACE_OBJ_RTLD:
1140 case (uintptr_t)DTRACE_OBJ_CDEFS:
1141 dmp = dtp->dt_cdefs;
1143 case (uintptr_t)DTRACE_OBJ_DDEFS:
1144 dmp = dtp->dt_ddefs;
1147 dmp = dt_module_create(dtp, object);
1152 (void) dt_set_errno(dtp, err);
1158 * Exported interface to look up a symbol by name. We return the GElf_Sym and
1159 * complete symbol information for the matching symbol.
1162 dtrace_lookup_by_name(dtrace_hdl_t *dtp, const char *object, const char *name,
1163 GElf_Sym *symp, dtrace_syminfo_t *sip)
1170 uint_t mask = 0; /* mask of dt_module flags to match */
1171 uint_t bits = 0; /* flag bits that must be present */
1173 if (object != DTRACE_OBJ_EVERY &&
1174 object != DTRACE_OBJ_KMODS &&
1175 object != DTRACE_OBJ_UMODS) {
1176 if ((dmp = dt_module_from_object(dtp, object)) == NULL)
1177 return (-1); /* dt_errno is set for us */
1179 if (dt_module_load(dtp, dmp) == -1)
1180 return (-1); /* dt_errno is set for us */
1184 if (object == DTRACE_OBJ_KMODS)
1185 mask = bits = DT_DM_KERNEL;
1186 else if (object == DTRACE_OBJ_UMODS)
1187 mask = DT_DM_KERNEL;
1189 dmp = dt_list_next(&dtp->dt_modlist);
1196 for (; n > 0; n--, dmp = dt_list_next(dmp)) {
1197 if ((dmp->dm_flags & mask) != bits)
1198 continue; /* failed to match required attributes */
1200 if (dt_module_load(dtp, dmp) == -1)
1201 continue; /* failed to load symbol table */
1203 if (dmp->dm_ops->do_symname(dmp, name, symp, &id) != NULL) {
1205 sip->dts_object = dmp->dm_name;
1206 sip->dts_name = (const char *)
1207 dmp->dm_strtab.cts_data + symp->st_name;
1213 if (dmp->dm_extern != NULL &&
1214 (idp = dt_idhash_lookup(dmp->dm_extern, name)) != NULL) {
1216 symp->st_name = (uintptr_t)idp->di_name;
1218 GELF_ST_INFO(STB_GLOBAL, STT_NOTYPE);
1220 symp->st_shndx = SHN_UNDEF;
1223 ctf_type_size(idp->di_ctfp, idp->di_type);
1227 sip->dts_object = dmp->dm_name;
1228 sip->dts_name = idp->di_name;
1229 sip->dts_id = idp->di_id;
1236 return (dt_set_errno(dtp, EDT_NOSYM));
1240 * Exported interface to look up a symbol by address. We return the GElf_Sym
1241 * and complete symbol information for the matching symbol.
1244 dtrace_lookup_by_addr(dtrace_hdl_t *dtp, GElf_Addr addr,
1245 GElf_Sym *symp, dtrace_syminfo_t *sip)
1249 const dtrace_vector_t *v = dtp->dt_vector;
1252 return (v->dtv_lookup_by_addr(dtp->dt_varg, addr, symp, sip));
1254 for (dmp = dt_list_next(&dtp->dt_modlist); dmp != NULL;
1255 dmp = dt_list_next(dmp)) {
1256 if (addr - dmp->dm_text_va < dmp->dm_text_size ||
1257 addr - dmp->dm_data_va < dmp->dm_data_size ||
1258 addr - dmp->dm_bss_va < dmp->dm_bss_size)
1263 return (dt_set_errno(dtp, EDT_NOSYMADDR));
1265 if (dt_module_load(dtp, dmp) == -1)
1266 return (-1); /* dt_errno is set for us */
1269 if (dmp->dm_ops->do_symaddr(dmp, addr, symp, &id) == NULL)
1270 return (dt_set_errno(dtp, EDT_NOSYMADDR));
1274 sip->dts_object = dmp->dm_name;
1277 sip->dts_name = (const char *)
1278 dmp->dm_strtab.cts_data + symp->st_name;
1281 sip->dts_name = NULL;
1290 dtrace_lookup_by_type(dtrace_hdl_t *dtp, const char *object, const char *name,
1291 dtrace_typeinfo_t *tip)
1293 dtrace_typeinfo_t ti;
1300 uint_t mask = 0; /* mask of dt_module flags to match */
1301 uint_t bits = 0; /* flag bits that must be present */
1303 if (object != DTRACE_OBJ_EVERY &&
1304 object != DTRACE_OBJ_KMODS &&
1305 object != DTRACE_OBJ_UMODS) {
1306 if ((dmp = dt_module_from_object(dtp, object)) == NULL)
1307 return (-1); /* dt_errno is set for us */
1309 if (dt_module_load(dtp, dmp) == -1)
1310 return (-1); /* dt_errno is set for us */
1315 if (object == DTRACE_OBJ_KMODS)
1316 mask = bits = DT_DM_KERNEL;
1317 else if (object == DTRACE_OBJ_UMODS)
1318 mask = DT_DM_KERNEL;
1320 dmp = dt_list_next(&dtp->dt_modlist);
1328 for (; n > 0; n--, dmp = dt_list_next(dmp)) {
1329 if ((dmp->dm_flags & mask) != bits)
1330 continue; /* failed to match required attributes */
1333 * If we can't load the CTF container, continue on to the next
1334 * module. If our search was scoped to only one module then
1335 * return immediately leaving dt_errno unmodified.
1337 if (dt_module_getctf(dtp, dmp) == NULL) {
1344 * Look up the type in the module's CTF container. If our
1345 * match is a forward declaration tag, save this choice in
1346 * 'tip' and keep going in the hope that we will locate the
1347 * underlying structure definition. Otherwise just return.
1349 if ((id = ctf_lookup_by_name(dmp->dm_ctfp, name)) != CTF_ERR) {
1350 tip->dtt_object = dmp->dm_name;
1351 tip->dtt_ctfp = dmp->dm_ctfp;
1354 if (ctf_type_kind(dmp->dm_ctfp, ctf_type_resolve(
1355 dmp->dm_ctfp, id)) != CTF_K_FORWARD)
1363 return (dt_set_errno(dtp, EDT_NOTYPE));
1369 dtrace_symbol_type(dtrace_hdl_t *dtp, const GElf_Sym *symp,
1370 const dtrace_syminfo_t *sip, dtrace_typeinfo_t *tip)
1374 tip->dtt_object = NULL;
1375 tip->dtt_ctfp = NULL;
1376 tip->dtt_type = CTF_ERR;
1378 if ((dmp = dt_module_lookup_by_name(dtp, sip->dts_object)) == NULL)
1379 return (dt_set_errno(dtp, EDT_NOMOD));
1381 if (symp->st_shndx == SHN_UNDEF && dmp->dm_extern != NULL) {
1383 dt_idhash_lookup(dmp->dm_extern, sip->dts_name);
1386 return (dt_set_errno(dtp, EDT_NOSYM));
1388 tip->dtt_ctfp = idp->di_ctfp;
1389 tip->dtt_type = idp->di_type;
1391 } else if (GELF_ST_TYPE(symp->st_info) != STT_FUNC) {
1392 if (dt_module_getctf(dtp, dmp) == NULL)
1393 return (-1); /* errno is set for us */
1395 tip->dtt_ctfp = dmp->dm_ctfp;
1396 tip->dtt_type = ctf_lookup_by_symbol(dmp->dm_ctfp, sip->dts_id);
1398 if (tip->dtt_type == CTF_ERR) {
1399 dtp->dt_ctferr = ctf_errno(tip->dtt_ctfp);
1400 return (dt_set_errno(dtp, EDT_CTF));
1404 tip->dtt_ctfp = DT_FPTR_CTFP(dtp);
1405 tip->dtt_type = DT_FPTR_TYPE(dtp);
1408 tip->dtt_object = dmp->dm_name;
1412 static dtrace_objinfo_t *
1413 dt_module_info(const dt_module_t *dmp, dtrace_objinfo_t *dto)
1415 dto->dto_name = dmp->dm_name;
1416 dto->dto_file = dmp->dm_file;
1417 dto->dto_id = dmp->dm_modid;
1420 if (dmp->dm_flags & DT_DM_KERNEL)
1421 dto->dto_flags |= DTRACE_OBJ_F_KERNEL;
1422 if (dmp->dm_flags & DT_DM_PRIMARY)
1423 dto->dto_flags |= DTRACE_OBJ_F_PRIMARY;
1425 dto->dto_text_va = dmp->dm_text_va;
1426 dto->dto_text_size = dmp->dm_text_size;
1427 dto->dto_data_va = dmp->dm_data_va;
1428 dto->dto_data_size = dmp->dm_data_size;
1429 dto->dto_bss_va = dmp->dm_bss_va;
1430 dto->dto_bss_size = dmp->dm_bss_size;
1436 dtrace_object_iter(dtrace_hdl_t *dtp, dtrace_obj_f *func, void *data)
1438 const dt_module_t *dmp = dt_list_next(&dtp->dt_modlist);
1439 dtrace_objinfo_t dto;
1442 for (; dmp != NULL; dmp = dt_list_next(dmp)) {
1443 if ((rv = (*func)(dtp, dt_module_info(dmp, &dto), data)) != 0)
1451 dtrace_object_info(dtrace_hdl_t *dtp, const char *object, dtrace_objinfo_t *dto)
1455 if (object == DTRACE_OBJ_EVERY || object == DTRACE_OBJ_KMODS ||
1456 object == DTRACE_OBJ_UMODS || dto == NULL)
1457 return (dt_set_errno(dtp, EINVAL));
1459 if ((dmp = dt_module_from_object(dtp, object)) == NULL)
1460 return (-1); /* dt_errno is set for us */
1462 if (dt_module_load(dtp, dmp) == -1)
1463 return (-1); /* dt_errno is set for us */
1465 (void) dt_module_info(dmp, dto);