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.
27 * Portions Copyright 2016 Pedro Giffuni. All rights reserved.
30 #include <sys/types.h>
32 #include <sys/modctl.h>
34 #include <sys/kobj_impl.h>
35 #include <sys/sysmacros.h>
39 #include <sys/param.h>
40 #include <sys/linker.h>
59 #include <dt_strtab.h>
60 #include <dt_module.h>
63 static const char *dt_module_strtab; /* active strtab for qsort callbacks */
66 dt_module_symhash_insert(dt_module_t *dmp, const char *name, uint_t id)
68 dt_sym_t *dsp = &dmp->dm_symchains[dmp->dm_symfree];
71 assert(dmp->dm_symfree < dmp->dm_nsymelems + 1);
74 h = dt_strtab_hash(name, NULL) % dmp->dm_nsymbuckets;
75 dsp->ds_next = dmp->dm_symbuckets[h];
76 dmp->dm_symbuckets[h] = dmp->dm_symfree++;
80 dt_module_syminit32(dt_module_t *dmp)
82 #if STT_NUM != (STT_TLS + 1)
83 #error "STT_NUM has grown. update dt_module_syminit32()"
86 Elf32_Sym *sym = dmp->dm_symtab.cts_data;
87 const char *base = dmp->dm_strtab.cts_data;
88 size_t ss_size = dmp->dm_strtab.cts_size;
89 uint_t i, n = dmp->dm_nsymelems;
92 #if defined(__FreeBSD__)
96 gelf_getehdr(dmp->dm_elf, &ehdr);
97 is_elf_obj = (ehdr.e_type == ET_REL);
100 for (i = 0; i < n; i++, sym++) {
101 const char *name = base + sym->st_name;
102 uchar_t type = ELF32_ST_TYPE(sym->st_info);
104 if (type >= STT_NUM || type == STT_SECTION)
105 continue; /* skip sections and unknown types */
107 if (sym->st_name == 0 || sym->st_name >= ss_size)
108 continue; /* skip null or invalid names */
110 if (sym->st_value != 0 &&
111 (ELF32_ST_BIND(sym->st_info) != STB_LOCAL || sym->st_size)) {
112 asrsv++; /* reserve space in the address map */
114 #if defined(__FreeBSD__)
115 sym->st_value += (Elf_Addr) dmp->dm_reloc_offset;
116 if (is_elf_obj && sym->st_shndx != SHN_UNDEF &&
117 sym->st_shndx < ehdr.e_shnum)
119 dmp->dm_sec_offsets[sym->st_shndx];
123 dt_module_symhash_insert(dmp, name, i);
130 dt_module_syminit64(dt_module_t *dmp)
132 #if STT_NUM != (STT_TLS + 1)
133 #error "STT_NUM has grown. update dt_module_syminit64()"
136 Elf64_Sym *sym = dmp->dm_symtab.cts_data;
137 const char *base = dmp->dm_strtab.cts_data;
138 size_t ss_size = dmp->dm_strtab.cts_size;
139 uint_t i, n = dmp->dm_nsymelems;
142 #if defined(__FreeBSD__)
146 gelf_getehdr(dmp->dm_elf, &ehdr);
147 is_elf_obj = (ehdr.e_type == ET_REL);
150 for (i = 0; i < n; i++, sym++) {
151 const char *name = base + sym->st_name;
152 uchar_t type = ELF64_ST_TYPE(sym->st_info);
154 if (type >= STT_NUM || type == STT_SECTION)
155 continue; /* skip sections and unknown types */
157 if (sym->st_name == 0 || sym->st_name >= ss_size)
158 continue; /* skip null or invalid names */
160 if (sym->st_value != 0 &&
161 (ELF64_ST_BIND(sym->st_info) != STB_LOCAL || sym->st_size)) {
162 asrsv++; /* reserve space in the address map */
163 #if defined(__FreeBSD__)
164 sym->st_value += (Elf_Addr) dmp->dm_reloc_offset;
165 if (is_elf_obj && sym->st_shndx != SHN_UNDEF &&
166 sym->st_shndx < ehdr.e_shnum)
168 dmp->dm_sec_offsets[sym->st_shndx];
172 dt_module_symhash_insert(dmp, name, i);
179 * Sort comparison function for 32-bit symbol address-to-name lookups. We sort
180 * symbols by value. If values are equal, we prefer the symbol that is
181 * non-zero sized, typed, not weak, or lexically first, in that order.
184 dt_module_symcomp32(const void *lp, const void *rp)
186 Elf32_Sym *lhs = *((Elf32_Sym **)lp);
187 Elf32_Sym *rhs = *((Elf32_Sym **)rp);
189 if (lhs->st_value != rhs->st_value)
190 return (lhs->st_value > rhs->st_value ? 1 : -1);
192 if ((lhs->st_size == 0) != (rhs->st_size == 0))
193 return (lhs->st_size == 0 ? 1 : -1);
195 if ((ELF32_ST_TYPE(lhs->st_info) == STT_NOTYPE) !=
196 (ELF32_ST_TYPE(rhs->st_info) == STT_NOTYPE))
197 return (ELF32_ST_TYPE(lhs->st_info) == STT_NOTYPE ? 1 : -1);
199 if ((ELF32_ST_BIND(lhs->st_info) == STB_WEAK) !=
200 (ELF32_ST_BIND(rhs->st_info) == STB_WEAK))
201 return (ELF32_ST_BIND(lhs->st_info) == STB_WEAK ? 1 : -1);
203 return (strcmp(dt_module_strtab + lhs->st_name,
204 dt_module_strtab + rhs->st_name));
208 * Sort comparison function for 64-bit symbol address-to-name lookups. We sort
209 * symbols by value. If values are equal, we prefer the symbol that is
210 * non-zero sized, typed, not weak, or lexically first, in that order.
213 dt_module_symcomp64(const void *lp, const void *rp)
215 Elf64_Sym *lhs = *((Elf64_Sym **)lp);
216 Elf64_Sym *rhs = *((Elf64_Sym **)rp);
218 if (lhs->st_value != rhs->st_value)
219 return (lhs->st_value > rhs->st_value ? 1 : -1);
221 if ((lhs->st_size == 0) != (rhs->st_size == 0))
222 return (lhs->st_size == 0 ? 1 : -1);
224 if ((ELF64_ST_TYPE(lhs->st_info) == STT_NOTYPE) !=
225 (ELF64_ST_TYPE(rhs->st_info) == STT_NOTYPE))
226 return (ELF64_ST_TYPE(lhs->st_info) == STT_NOTYPE ? 1 : -1);
228 if ((ELF64_ST_BIND(lhs->st_info) == STB_WEAK) !=
229 (ELF64_ST_BIND(rhs->st_info) == STB_WEAK))
230 return (ELF64_ST_BIND(lhs->st_info) == STB_WEAK ? 1 : -1);
232 return (strcmp(dt_module_strtab + lhs->st_name,
233 dt_module_strtab + rhs->st_name));
237 dt_module_symsort32(dt_module_t *dmp)
239 Elf32_Sym *symtab = (Elf32_Sym *)dmp->dm_symtab.cts_data;
240 Elf32_Sym **sympp = (Elf32_Sym **)dmp->dm_asmap;
241 const dt_sym_t *dsp = dmp->dm_symchains + 1;
242 uint_t i, n = dmp->dm_symfree;
244 for (i = 1; i < n; i++, dsp++) {
245 Elf32_Sym *sym = symtab + dsp->ds_symid;
246 if (sym->st_value != 0 &&
247 (ELF32_ST_BIND(sym->st_info) != STB_LOCAL || sym->st_size))
251 dmp->dm_aslen = (uint_t)(sympp - (Elf32_Sym **)dmp->dm_asmap);
252 assert(dmp->dm_aslen <= dmp->dm_asrsv);
254 dt_module_strtab = dmp->dm_strtab.cts_data;
255 qsort(dmp->dm_asmap, dmp->dm_aslen,
256 sizeof (Elf32_Sym *), dt_module_symcomp32);
257 dt_module_strtab = NULL;
261 dt_module_symsort64(dt_module_t *dmp)
263 Elf64_Sym *symtab = (Elf64_Sym *)dmp->dm_symtab.cts_data;
264 Elf64_Sym **sympp = (Elf64_Sym **)dmp->dm_asmap;
265 const dt_sym_t *dsp = dmp->dm_symchains + 1;
266 uint_t i, n = dmp->dm_symfree;
268 for (i = 1; i < n; i++, dsp++) {
269 Elf64_Sym *sym = symtab + dsp->ds_symid;
270 if (sym->st_value != 0 &&
271 (ELF64_ST_BIND(sym->st_info) != STB_LOCAL || sym->st_size))
275 dmp->dm_aslen = (uint_t)(sympp - (Elf64_Sym **)dmp->dm_asmap);
276 assert(dmp->dm_aslen <= dmp->dm_asrsv);
278 dt_module_strtab = dmp->dm_strtab.cts_data;
279 qsort(dmp->dm_asmap, dmp->dm_aslen,
280 sizeof (Elf64_Sym *), dt_module_symcomp64);
281 dt_module_strtab = NULL;
285 dt_module_symgelf32(const Elf32_Sym *src, GElf_Sym *dst)
288 dst->st_name = src->st_name;
289 dst->st_info = src->st_info;
290 dst->st_other = src->st_other;
291 dst->st_shndx = src->st_shndx;
292 dst->st_value = src->st_value;
293 dst->st_size = src->st_size;
300 dt_module_symgelf64(const Elf64_Sym *src, GElf_Sym *dst)
303 bcopy(src, dst, sizeof (GElf_Sym));
309 dt_module_symname32(dt_module_t *dmp, const char *name,
310 GElf_Sym *symp, uint_t *idp)
312 const Elf32_Sym *symtab = dmp->dm_symtab.cts_data;
313 const char *strtab = dmp->dm_strtab.cts_data;
315 const Elf32_Sym *sym;
319 if (dmp->dm_nsymelems == 0)
322 h = dt_strtab_hash(name, NULL) % dmp->dm_nsymbuckets;
324 for (i = dmp->dm_symbuckets[h]; i != 0; i = dsp->ds_next) {
325 dsp = &dmp->dm_symchains[i];
326 sym = symtab + dsp->ds_symid;
328 if (strcmp(name, strtab + sym->st_name) == 0) {
330 *idp = dsp->ds_symid;
331 return (dt_module_symgelf32(sym, symp));
339 dt_module_symname64(dt_module_t *dmp, const char *name,
340 GElf_Sym *symp, uint_t *idp)
342 const Elf64_Sym *symtab = dmp->dm_symtab.cts_data;
343 const char *strtab = dmp->dm_strtab.cts_data;
345 const Elf64_Sym *sym;
349 if (dmp->dm_nsymelems == 0)
352 h = dt_strtab_hash(name, NULL) % dmp->dm_nsymbuckets;
354 for (i = dmp->dm_symbuckets[h]; i != 0; i = dsp->ds_next) {
355 dsp = &dmp->dm_symchains[i];
356 sym = symtab + dsp->ds_symid;
358 if (strcmp(name, strtab + sym->st_name) == 0) {
360 *idp = dsp->ds_symid;
361 return (dt_module_symgelf64(sym, symp));
369 dt_module_symaddr32(dt_module_t *dmp, GElf_Addr addr,
370 GElf_Sym *symp, uint_t *idp)
372 const Elf32_Sym **asmap = (const Elf32_Sym **)dmp->dm_asmap;
373 const Elf32_Sym *symtab = dmp->dm_symtab.cts_data;
374 const Elf32_Sym *sym;
376 uint_t i, mid, lo = 0, hi = dmp->dm_aslen - 1;
379 if (dmp->dm_aslen == 0)
382 while (hi - lo > 1) {
384 if (addr >= asmap[mid]->st_value)
390 i = addr < asmap[hi]->st_value ? lo : hi;
395 * If the previous entry has the same value, improve our choice. The
396 * order of equal-valued symbols is determined by the comparison func.
398 while (i-- != 0 && asmap[i]->st_value == v)
401 if (addr - sym->st_value < MAX(sym->st_size, 1)) {
403 *idp = (uint_t)(sym - symtab);
404 return (dt_module_symgelf32(sym, symp));
411 dt_module_symaddr64(dt_module_t *dmp, GElf_Addr addr,
412 GElf_Sym *symp, uint_t *idp)
414 const Elf64_Sym **asmap = (const Elf64_Sym **)dmp->dm_asmap;
415 const Elf64_Sym *symtab = dmp->dm_symtab.cts_data;
416 const Elf64_Sym *sym;
418 uint_t i, mid, lo = 0, hi = dmp->dm_aslen - 1;
421 if (dmp->dm_aslen == 0)
424 while (hi - lo > 1) {
426 if (addr >= asmap[mid]->st_value)
432 i = addr < asmap[hi]->st_value ? lo : hi;
437 * If the previous entry has the same value, improve our choice. The
438 * order of equal-valued symbols is determined by the comparison func.
440 while (i-- != 0 && asmap[i]->st_value == v)
443 if (addr - sym->st_value < MAX(sym->st_size, 1)) {
445 *idp = (uint_t)(sym - symtab);
446 return (dt_module_symgelf64(sym, symp));
452 static const dt_modops_t dt_modops_32 = {
459 static const dt_modops_t dt_modops_64 = {
467 dt_module_create(dtrace_hdl_t *dtp, const char *name)
469 uint_t h = dt_strtab_hash(name, NULL) % dtp->dt_modbuckets;
472 for (dmp = dtp->dt_mods[h]; dmp != NULL; dmp = dmp->dm_next) {
473 if (strcmp(dmp->dm_name, name) == 0)
477 if ((dmp = malloc(sizeof (dt_module_t))) == NULL)
478 return (NULL); /* caller must handle allocation failure */
480 bzero(dmp, sizeof (dt_module_t));
481 (void) strlcpy(dmp->dm_name, name, sizeof (dmp->dm_name));
482 dt_list_append(&dtp->dt_modlist, dmp);
483 dmp->dm_next = dtp->dt_mods[h];
484 dtp->dt_mods[h] = dmp;
487 if (dtp->dt_conf.dtc_ctfmodel == CTF_MODEL_LP64)
488 dmp->dm_ops = &dt_modops_64;
490 dmp->dm_ops = &dt_modops_32;
496 dt_module_lookup_by_name(dtrace_hdl_t *dtp, const char *name)
498 uint_t h = dt_strtab_hash(name, NULL) % dtp->dt_modbuckets;
501 for (dmp = dtp->dt_mods[h]; dmp != NULL; dmp = dmp->dm_next) {
502 if (strcmp(dmp->dm_name, name) == 0)
511 dt_module_lookup_by_ctf(dtrace_hdl_t *dtp, ctf_file_t *ctfp)
513 return (ctfp ? ctf_getspecific(ctfp) : NULL);
517 dt_module_load_sect(dtrace_hdl_t *dtp, dt_module_t *dmp, ctf_sect_t *ctsp)
525 if (elf_getshdrstrndx(dmp->dm_elf, &shstrs) == -1)
526 return (dt_set_errno(dtp, EDT_NOTLOADED));
528 for (sp = NULL; (sp = elf_nextscn(dmp->dm_elf, sp)) != NULL; ) {
529 if (gelf_getshdr(sp, &sh) == NULL || sh.sh_type == SHT_NULL ||
530 (s = elf_strptr(dmp->dm_elf, shstrs, sh.sh_name)) == NULL)
531 continue; /* skip any malformed sections */
533 if (sh.sh_type == ctsp->cts_type &&
534 sh.sh_entsize == ctsp->cts_entsize &&
535 strcmp(s, ctsp->cts_name) == 0)
536 break; /* section matches specification */
540 * If the section isn't found, return success but leave cts_data set
541 * to NULL and cts_size set to zero for our caller.
543 if (sp == NULL || (dp = elf_getdata(sp, NULL)) == NULL)
547 ctsp->cts_data = dp->d_buf;
549 if ((ctsp->cts_data = malloc(dp->d_size)) == NULL)
551 memcpy(ctsp->cts_data, dp->d_buf, dp->d_size);
553 ctsp->cts_size = dp->d_size;
555 dt_dprintf("loaded %s [%s] (%lu bytes)\n",
556 dmp->dm_name, ctsp->cts_name, (ulong_t)ctsp->cts_size);
562 dt_module_load(dtrace_hdl_t *dtp, dt_module_t *dmp)
564 if (dmp->dm_flags & DT_DM_LOADED)
565 return (0); /* module is already loaded */
567 dmp->dm_ctdata.cts_name = ".SUNW_ctf";
568 dmp->dm_ctdata.cts_type = SHT_PROGBITS;
569 dmp->dm_ctdata.cts_flags = 0;
570 dmp->dm_ctdata.cts_data = NULL;
571 dmp->dm_ctdata.cts_size = 0;
572 dmp->dm_ctdata.cts_entsize = 0;
573 dmp->dm_ctdata.cts_offset = 0;
575 dmp->dm_symtab.cts_name = ".symtab";
576 dmp->dm_symtab.cts_type = SHT_SYMTAB;
577 dmp->dm_symtab.cts_flags = 0;
578 dmp->dm_symtab.cts_data = NULL;
579 dmp->dm_symtab.cts_size = 0;
580 dmp->dm_symtab.cts_entsize = dmp->dm_ops == &dt_modops_64 ?
581 sizeof (Elf64_Sym) : sizeof (Elf32_Sym);
582 dmp->dm_symtab.cts_offset = 0;
584 dmp->dm_strtab.cts_name = ".strtab";
585 dmp->dm_strtab.cts_type = SHT_STRTAB;
586 dmp->dm_strtab.cts_flags = 0;
587 dmp->dm_strtab.cts_data = NULL;
588 dmp->dm_strtab.cts_size = 0;
589 dmp->dm_strtab.cts_entsize = 0;
590 dmp->dm_strtab.cts_offset = 0;
593 * Attempt to load the module's CTF section, symbol table section, and
594 * string table section. Note that modules may not contain CTF data:
595 * this will result in a successful load_sect but data of size zero.
596 * We will then fail if dt_module_getctf() is called, as shown below.
598 if (dt_module_load_sect(dtp, dmp, &dmp->dm_ctdata) == -1 ||
599 dt_module_load_sect(dtp, dmp, &dmp->dm_symtab) == -1 ||
600 dt_module_load_sect(dtp, dmp, &dmp->dm_strtab) == -1) {
601 dt_module_unload(dtp, dmp);
602 return (-1); /* dt_errno is set for us */
606 * Allocate the hash chains and hash buckets for symbol name lookup.
607 * This is relatively simple since the symbol table is of fixed size
608 * and is known in advance. We allocate one extra element since we
609 * use element indices instead of pointers and zero is our sentinel.
612 dmp->dm_symtab.cts_size / dmp->dm_symtab.cts_entsize;
614 dmp->dm_nsymbuckets = _dtrace_strbuckets;
615 dmp->dm_symfree = 1; /* first free element is index 1 */
617 dmp->dm_symbuckets = calloc(dmp->dm_nsymbuckets, sizeof (uint_t));
618 dmp->dm_symchains = calloc(dmp->dm_nsymelems + 1, sizeof (dt_sym_t));
620 if (dmp->dm_symbuckets == NULL || dmp->dm_symchains == NULL) {
621 dt_module_unload(dtp, dmp);
622 return (dt_set_errno(dtp, EDT_NOMEM));
626 * Iterate over the symbol table data buffer and insert each symbol
627 * name into the name hash if the name and type are valid. Then
628 * allocate the address map, fill it in, and sort it.
630 dmp->dm_asrsv = dmp->dm_ops->do_syminit(dmp);
632 dt_dprintf("hashed %s [%s] (%u symbols)\n",
633 dmp->dm_name, dmp->dm_symtab.cts_name, dmp->dm_symfree - 1);
635 if ((dmp->dm_asmap = malloc(sizeof (void *) * dmp->dm_asrsv)) == NULL) {
636 dt_module_unload(dtp, dmp);
637 return (dt_set_errno(dtp, EDT_NOMEM));
640 dmp->dm_ops->do_symsort(dmp);
642 dt_dprintf("sorted %s [%s] (%u symbols)\n",
643 dmp->dm_name, dmp->dm_symtab.cts_name, dmp->dm_aslen);
645 dmp->dm_flags |= DT_DM_LOADED;
650 dt_module_getctf(dtrace_hdl_t *dtp, dt_module_t *dmp)
657 if (dmp->dm_ctfp != NULL || dt_module_load(dtp, dmp) != 0)
658 return (dmp->dm_ctfp);
660 if (dmp->dm_ops == &dt_modops_64)
661 model = CTF_MODEL_LP64;
663 model = CTF_MODEL_ILP32;
666 * If the data model of the module does not match our program data
667 * model, then do not permit CTF from this module to be opened and
668 * returned to the compiler. If we support mixed data models in the
669 * future for combined kernel/user tracing, this can be removed.
671 if (dtp->dt_conf.dtc_ctfmodel != model) {
672 (void) dt_set_errno(dtp, EDT_DATAMODEL);
676 if (dmp->dm_ctdata.cts_size == 0) {
677 (void) dt_set_errno(dtp, EDT_NOCTF);
681 dmp->dm_ctfp = ctf_bufopen(&dmp->dm_ctdata,
682 &dmp->dm_symtab, &dmp->dm_strtab, &dtp->dt_ctferr);
684 if (dmp->dm_ctfp == NULL) {
685 (void) dt_set_errno(dtp, EDT_CTF);
689 (void) ctf_setmodel(dmp->dm_ctfp, model);
690 ctf_setspecific(dmp->dm_ctfp, dmp);
692 if ((parent = ctf_parent_name(dmp->dm_ctfp)) != NULL) {
693 if ((pmp = dt_module_create(dtp, parent)) == NULL ||
694 (pfp = dt_module_getctf(dtp, pmp)) == NULL) {
696 (void) dt_set_errno(dtp, EDT_NOMEM);
700 if (ctf_import(dmp->dm_ctfp, pfp) == CTF_ERR) {
701 dtp->dt_ctferr = ctf_errno(dmp->dm_ctfp);
702 (void) dt_set_errno(dtp, EDT_CTF);
707 dt_dprintf("loaded CTF container for %s (%p)\n",
708 dmp->dm_name, (void *)dmp->dm_ctfp);
710 return (dmp->dm_ctfp);
713 ctf_close(dmp->dm_ctfp);
720 dt_module_unload(dtrace_hdl_t *dtp, dt_module_t *dmp)
722 ctf_close(dmp->dm_ctfp);
726 if (dmp->dm_ctdata.cts_data != NULL) {
727 free(dmp->dm_ctdata.cts_data);
729 if (dmp->dm_symtab.cts_data != NULL) {
730 free(dmp->dm_symtab.cts_data);
732 if (dmp->dm_strtab.cts_data != NULL) {
733 free(dmp->dm_strtab.cts_data);
737 bzero(&dmp->dm_ctdata, sizeof (ctf_sect_t));
738 bzero(&dmp->dm_symtab, sizeof (ctf_sect_t));
739 bzero(&dmp->dm_strtab, sizeof (ctf_sect_t));
741 if (dmp->dm_symbuckets != NULL) {
742 free(dmp->dm_symbuckets);
743 dmp->dm_symbuckets = NULL;
746 if (dmp->dm_symchains != NULL) {
747 free(dmp->dm_symchains);
748 dmp->dm_symchains = NULL;
751 if (dmp->dm_asmap != NULL) {
753 dmp->dm_asmap = NULL;
755 #if defined(__FreeBSD__)
756 if (dmp->dm_sec_offsets != NULL) {
757 free(dmp->dm_sec_offsets);
758 dmp->dm_sec_offsets = NULL;
762 dmp->dm_nsymbuckets = 0;
763 dmp->dm_nsymelems = 0;
768 dmp->dm_text_size = 0;
770 dmp->dm_data_size = 0;
772 dmp->dm_bss_size = 0;
774 if (dmp->dm_extern != NULL) {
775 dt_idhash_destroy(dmp->dm_extern);
776 dmp->dm_extern = NULL;
779 (void) elf_end(dmp->dm_elf);
782 dmp->dm_flags &= ~DT_DM_LOADED;
786 dt_module_destroy(dtrace_hdl_t *dtp, dt_module_t *dmp)
788 uint_t h = dt_strtab_hash(dmp->dm_name, NULL) % dtp->dt_modbuckets;
789 dt_module_t **dmpp = &dtp->dt_mods[h];
791 dt_list_delete(&dtp->dt_modlist, dmp);
792 assert(dtp->dt_nmods != 0);
796 * Now remove this module from its hash chain. We expect to always
797 * find the module on its hash chain, so in this loop we assert that
798 * we don't run off the end of the list.
800 while (*dmpp != dmp) {
801 dmpp = &((*dmpp)->dm_next);
802 assert(*dmpp != NULL);
805 *dmpp = dmp->dm_next;
807 dt_module_unload(dtp, dmp);
812 * Insert a new external symbol reference into the specified module. The new
813 * symbol will be marked as undefined and is assigned a symbol index beyond
814 * any existing cached symbols from this module. We use the ident's di_data
815 * field to store a pointer to a copy of the dtrace_syminfo_t for this symbol.
818 dt_module_extern(dtrace_hdl_t *dtp, dt_module_t *dmp,
819 const char *name, const dtrace_typeinfo_t *tip)
821 dtrace_syminfo_t *sip;
825 if (dmp->dm_extern == NULL && (dmp->dm_extern = dt_idhash_create(
826 "extern", NULL, dmp->dm_nsymelems, UINT_MAX)) == NULL) {
827 (void) dt_set_errno(dtp, EDT_NOMEM);
831 if (dt_idhash_nextid(dmp->dm_extern, &id) == -1) {
832 (void) dt_set_errno(dtp, EDT_SYMOFLOW);
836 if ((sip = malloc(sizeof (dtrace_syminfo_t))) == NULL) {
837 (void) dt_set_errno(dtp, EDT_NOMEM);
841 idp = dt_idhash_insert(dmp->dm_extern, name, DT_IDENT_SYMBOL, 0, id,
842 _dtrace_symattr, 0, &dt_idops_thaw, NULL, dtp->dt_gen);
845 (void) dt_set_errno(dtp, EDT_NOMEM);
850 sip->dts_object = dmp->dm_name;
851 sip->dts_name = idp->di_name;
852 sip->dts_id = idp->di_id;
855 idp->di_ctfp = tip->dtt_ctfp;
856 idp->di_type = tip->dtt_type;
862 dt_module_modelname(dt_module_t *dmp)
864 if (dmp->dm_ops == &dt_modops_64)
871 * Update our module cache by adding an entry for the specified module 'name'.
872 * We create the dt_module_t and populate it using /system/object/<name>/.
874 * On FreeBSD, the module name is passed as the full module file name,
875 * including the path.
879 dt_module_update(dtrace_hdl_t *dtp, const char *name)
881 dt_module_update(dtrace_hdl_t *dtp, struct kld_file_stat *k_stat)
884 char fname[MAXPATHLEN];
896 (void) snprintf(fname, sizeof (fname),
897 "%s/%s/object", OBJFS_ROOT, name);
901 char name[MAXPATHLEN];
902 uintptr_t mapbase, alignmask;
906 (void) strlcpy(name, k_stat->name, sizeof(name));
907 (void) strlcpy(fname, k_stat->pathname, sizeof(fname));
910 if ((fd = open(fname, O_RDONLY)) == -1 || fstat64(fd, &st) == -1 ||
911 (dmp = dt_module_create(dtp, name)) == NULL) {
912 dt_dprintf("failed to open %s: %s\n", fname, strerror(errno));
918 * Since the module can unload out from under us (and /system/object
919 * will return ENOENT), tell libelf to cook the entire file now and
920 * then close the underlying file descriptor immediately. If this
921 * succeeds, we know that we can continue safely using dmp->dm_elf.
923 dmp->dm_elf = elf_begin(fd, ELF_C_READ, NULL);
924 err = elf_cntl(dmp->dm_elf, ELF_C_FDREAD);
927 if (dmp->dm_elf == NULL || err == -1 ||
928 elf_getshdrstrndx(dmp->dm_elf, &shstrs) == -1) {
929 dt_dprintf("failed to load %s: %s\n",
930 fname, elf_errmsg(elf_errno()));
931 dt_module_destroy(dtp, dmp);
935 switch (gelf_getclass(dmp->dm_elf)) {
937 dmp->dm_ops = &dt_modops_32;
941 dmp->dm_ops = &dt_modops_64;
945 dt_dprintf("failed to load %s: unknown ELF class\n", fname);
946 dt_module_destroy(dtp, dmp);
949 #if defined(__FreeBSD__)
950 mapbase = (uintptr_t)k_stat->address;
951 gelf_getehdr(dmp->dm_elf, &ehdr);
952 is_elf_obj = (ehdr.e_type == ET_REL);
954 dmp->dm_sec_offsets =
955 malloc(ehdr.e_shnum * sizeof(*dmp->dm_sec_offsets));
956 if (dmp->dm_sec_offsets == NULL) {
957 dt_dprintf("failed to allocate memory\n");
958 dt_module_destroy(dtp, dmp);
964 * Iterate over the section headers locating various sections of
965 * interest and use their attributes to flesh out the dt_module_t.
967 for (sp = NULL; (sp = elf_nextscn(dmp->dm_elf, sp)) != NULL; ) {
968 if (gelf_getshdr(sp, &sh) == NULL || sh.sh_type == SHT_NULL ||
969 (s = elf_strptr(dmp->dm_elf, shstrs, sh.sh_name)) == NULL)
970 continue; /* skip any malformed sections */
971 #if defined(__FreeBSD__)
974 if (sh.sh_type == SHT_PROGBITS || sh.sh_type == SHT_NOBITS) {
975 alignmask = sh.sh_addralign - 1;
976 mapbase += alignmask;
977 mapbase &= ~alignmask;
978 sh.sh_addr = mapbase;
980 dmp->dm_sec_offsets[elf_ndxscn(sp)] = sh.sh_addr;
981 mapbase += sh.sh_size;
984 if (strcmp(s, ".text") == 0) {
985 dmp->dm_text_size = sh.sh_size;
986 dmp->dm_text_va = sh.sh_addr;
987 } else if (strcmp(s, ".data") == 0) {
988 dmp->dm_data_size = sh.sh_size;
989 dmp->dm_data_va = sh.sh_addr;
990 } else if (strcmp(s, ".bss") == 0) {
991 dmp->dm_bss_size = sh.sh_size;
992 dmp->dm_bss_va = sh.sh_addr;
993 } else if (strcmp(s, ".info") == 0 &&
994 (dp = elf_getdata(sp, NULL)) != NULL) {
995 bcopy(dp->d_buf, &dmp->dm_info,
996 MIN(sh.sh_size, sizeof (dmp->dm_info)));
997 } else if (strcmp(s, ".filename") == 0 &&
998 (dp = elf_getdata(sp, NULL)) != NULL) {
999 (void) strlcpy(dmp->dm_file,
1000 dp->d_buf, sizeof (dmp->dm_file));
1004 dmp->dm_flags |= DT_DM_KERNEL;
1006 dmp->dm_modid = (int)OBJFS_MODID(st.st_ino);
1009 * Include .rodata and special sections into .text.
1010 * This depends on default section layout produced by GNU ld
1011 * for ELF objects and libraries:
1012 * [Text][R/O data][R/W data][Dynamic][BSS][Non loadable]
1014 dmp->dm_text_size = dmp->dm_data_va - dmp->dm_text_va;
1015 #if defined(__i386__)
1017 * Find the first load section and figure out the relocation
1018 * offset for the symbols. The kernel module will not need
1019 * relocation, but the kernel linker modules will.
1021 for (i = 0; gelf_getphdr(dmp->dm_elf, i, &ph) != NULL; i++) {
1022 if (ph.p_type == PT_LOAD) {
1023 dmp->dm_reloc_offset = k_stat->address - ph.p_vaddr;
1030 if (dmp->dm_info.objfs_info_primary)
1031 dmp->dm_flags |= DT_DM_PRIMARY;
1033 dt_dprintf("opened %d-bit module %s (%s) [%d]\n",
1034 bits, dmp->dm_name, dmp->dm_file, dmp->dm_modid);
1038 * Unload all the loaded modules and then refresh the module cache with the
1039 * latest list of loaded modules and their address ranges.
1042 dtrace_update(dtrace_hdl_t *dtp)
1046 #if defined(__FreeBSD__)
1050 for (dmp = dt_list_next(&dtp->dt_modlist);
1051 dmp != NULL; dmp = dt_list_next(dmp))
1052 dt_module_unload(dtp, dmp);
1056 * Open /system/object and attempt to create a libdtrace module for
1057 * each kernel module that is loaded on the current system.
1059 if (!(dtp->dt_oflags & DTRACE_O_NOSYS) &&
1060 (dirp = opendir(OBJFS_ROOT)) != NULL) {
1063 while ((dp = readdir(dirp)) != NULL) {
1064 if (dp->d_name[0] != '.')
1065 dt_module_update(dtp, dp->d_name);
1068 (void) closedir(dirp);
1070 #elif defined(__FreeBSD__)
1072 * Use FreeBSD's kernel loader interface to discover what kernel
1073 * modules are loaded and create a libdtrace module for each one.
1075 for (fileid = kldnext(0); fileid > 0; fileid = kldnext(fileid)) {
1076 struct kld_file_stat k_stat;
1077 k_stat.version = sizeof(k_stat);
1078 if (kldstat(fileid, &k_stat) == 0)
1079 dt_module_update(dtp, &k_stat);
1084 * Look up all the macro identifiers and set di_id to the latest value.
1085 * This code collaborates with dt_lex.l on the use of di_id. We will
1086 * need to implement something fancier if we need to support non-ints.
1088 dt_idhash_lookup(dtp->dt_macros, "egid")->di_id = getegid();
1089 dt_idhash_lookup(dtp->dt_macros, "euid")->di_id = geteuid();
1090 dt_idhash_lookup(dtp->dt_macros, "gid")->di_id = getgid();
1091 dt_idhash_lookup(dtp->dt_macros, "pid")->di_id = getpid();
1092 dt_idhash_lookup(dtp->dt_macros, "pgid")->di_id = getpgid(0);
1093 dt_idhash_lookup(dtp->dt_macros, "ppid")->di_id = getppid();
1095 dt_idhash_lookup(dtp->dt_macros, "projid")->di_id = getprojid();
1097 dt_idhash_lookup(dtp->dt_macros, "sid")->di_id = getsid(0);
1099 dt_idhash_lookup(dtp->dt_macros, "taskid")->di_id = gettaskid();
1101 dt_idhash_lookup(dtp->dt_macros, "uid")->di_id = getuid();
1104 * Cache the pointers to the modules representing the base executable
1105 * and the run-time linker in the dtrace client handle. Note that on
1106 * x86 krtld is folded into unix, so if we don't find it, use unix
1109 dtp->dt_exec = dt_module_lookup_by_name(dtp, "genunix");
1110 dtp->dt_rtld = dt_module_lookup_by_name(dtp, "krtld");
1111 if (dtp->dt_rtld == NULL)
1112 dtp->dt_rtld = dt_module_lookup_by_name(dtp, "unix");
1115 * If this is the first time we are initializing the module list,
1116 * remove the module for genunix from the module list and then move it
1117 * to the front of the module list. We do this so that type and symbol
1118 * queries encounter genunix and thereby optimize for the common case
1119 * in dtrace_lookup_by_name() and dtrace_lookup_by_type(), below.
1121 if (dtp->dt_exec != NULL &&
1122 dtp->dt_cdefs == NULL && dtp->dt_ddefs == NULL) {
1123 dt_list_delete(&dtp->dt_modlist, dtp->dt_exec);
1124 dt_list_prepend(&dtp->dt_modlist, dtp->dt_exec);
1128 static dt_module_t *
1129 dt_module_from_object(dtrace_hdl_t *dtp, const char *object)
1131 int err = EDT_NOMOD;
1134 switch ((uintptr_t)object) {
1135 case (uintptr_t)DTRACE_OBJ_EXEC:
1138 case (uintptr_t)DTRACE_OBJ_RTLD:
1141 case (uintptr_t)DTRACE_OBJ_CDEFS:
1142 dmp = dtp->dt_cdefs;
1144 case (uintptr_t)DTRACE_OBJ_DDEFS:
1145 dmp = dtp->dt_ddefs;
1148 dmp = dt_module_create(dtp, object);
1153 (void) dt_set_errno(dtp, err);
1159 * Exported interface to look up a symbol by name. We return the GElf_Sym and
1160 * complete symbol information for the matching symbol.
1163 dtrace_lookup_by_name(dtrace_hdl_t *dtp, const char *object, const char *name,
1164 GElf_Sym *symp, dtrace_syminfo_t *sip)
1171 uint_t mask = 0; /* mask of dt_module flags to match */
1172 uint_t bits = 0; /* flag bits that must be present */
1174 if (object != DTRACE_OBJ_EVERY &&
1175 object != DTRACE_OBJ_KMODS &&
1176 object != DTRACE_OBJ_UMODS) {
1177 if ((dmp = dt_module_from_object(dtp, object)) == NULL)
1178 return (-1); /* dt_errno is set for us */
1180 if (dt_module_load(dtp, dmp) == -1)
1181 return (-1); /* dt_errno is set for us */
1185 if (object == DTRACE_OBJ_KMODS)
1186 mask = bits = DT_DM_KERNEL;
1187 else if (object == DTRACE_OBJ_UMODS)
1188 mask = DT_DM_KERNEL;
1190 dmp = dt_list_next(&dtp->dt_modlist);
1197 for (; n > 0; n--, dmp = dt_list_next(dmp)) {
1198 if ((dmp->dm_flags & mask) != bits)
1199 continue; /* failed to match required attributes */
1201 if (dt_module_load(dtp, dmp) == -1)
1202 continue; /* failed to load symbol table */
1204 if (dmp->dm_ops->do_symname(dmp, name, symp, &id) != NULL) {
1206 sip->dts_object = dmp->dm_name;
1207 sip->dts_name = (const char *)
1208 dmp->dm_strtab.cts_data + symp->st_name;
1214 if (dmp->dm_extern != NULL &&
1215 (idp = dt_idhash_lookup(dmp->dm_extern, name)) != NULL) {
1217 symp->st_name = (uintptr_t)idp->di_name;
1219 GELF_ST_INFO(STB_GLOBAL, STT_NOTYPE);
1221 symp->st_shndx = SHN_UNDEF;
1224 ctf_type_size(idp->di_ctfp, idp->di_type);
1228 sip->dts_object = dmp->dm_name;
1229 sip->dts_name = idp->di_name;
1230 sip->dts_id = idp->di_id;
1237 return (dt_set_errno(dtp, EDT_NOSYM));
1241 * Exported interface to look up a symbol by address. We return the GElf_Sym
1242 * and complete symbol information for the matching symbol.
1245 dtrace_lookup_by_addr(dtrace_hdl_t *dtp, GElf_Addr addr,
1246 GElf_Sym *symp, dtrace_syminfo_t *sip)
1250 const dtrace_vector_t *v = dtp->dt_vector;
1253 return (v->dtv_lookup_by_addr(dtp->dt_varg, addr, symp, sip));
1255 for (dmp = dt_list_next(&dtp->dt_modlist); dmp != NULL;
1256 dmp = dt_list_next(dmp)) {
1257 if (addr - dmp->dm_text_va < dmp->dm_text_size ||
1258 addr - dmp->dm_data_va < dmp->dm_data_size ||
1259 addr - dmp->dm_bss_va < dmp->dm_bss_size)
1264 return (dt_set_errno(dtp, EDT_NOSYMADDR));
1266 if (dt_module_load(dtp, dmp) == -1)
1267 return (-1); /* dt_errno is set for us */
1270 if (dmp->dm_ops->do_symaddr(dmp, addr, symp, &id) == NULL)
1271 return (dt_set_errno(dtp, EDT_NOSYMADDR));
1275 sip->dts_object = dmp->dm_name;
1278 sip->dts_name = (const char *)
1279 dmp->dm_strtab.cts_data + symp->st_name;
1282 sip->dts_name = NULL;
1291 dtrace_lookup_by_type(dtrace_hdl_t *dtp, const char *object, const char *name,
1292 dtrace_typeinfo_t *tip)
1294 dtrace_typeinfo_t ti;
1301 uint_t mask = 0; /* mask of dt_module flags to match */
1302 uint_t bits = 0; /* flag bits that must be present */
1304 if (object != DTRACE_OBJ_EVERY &&
1305 object != DTRACE_OBJ_KMODS &&
1306 object != DTRACE_OBJ_UMODS) {
1307 if ((dmp = dt_module_from_object(dtp, object)) == NULL)
1308 return (-1); /* dt_errno is set for us */
1310 if (dt_module_load(dtp, dmp) == -1)
1311 return (-1); /* dt_errno is set for us */
1316 if (object == DTRACE_OBJ_KMODS)
1317 mask = bits = DT_DM_KERNEL;
1318 else if (object == DTRACE_OBJ_UMODS)
1319 mask = DT_DM_KERNEL;
1321 dmp = dt_list_next(&dtp->dt_modlist);
1329 for (; n > 0; n--, dmp = dt_list_next(dmp)) {
1330 if ((dmp->dm_flags & mask) != bits)
1331 continue; /* failed to match required attributes */
1334 * If we can't load the CTF container, continue on to the next
1335 * module. If our search was scoped to only one module then
1336 * return immediately leaving dt_errno unmodified.
1338 if (dt_module_getctf(dtp, dmp) == NULL) {
1345 * Look up the type in the module's CTF container. If our
1346 * match is a forward declaration tag, save this choice in
1347 * 'tip' and keep going in the hope that we will locate the
1348 * underlying structure definition. Otherwise just return.
1350 if ((id = ctf_lookup_by_name(dmp->dm_ctfp, name)) != CTF_ERR) {
1351 tip->dtt_object = dmp->dm_name;
1352 tip->dtt_ctfp = dmp->dm_ctfp;
1355 if (ctf_type_kind(dmp->dm_ctfp, ctf_type_resolve(
1356 dmp->dm_ctfp, id)) != CTF_K_FORWARD)
1364 return (dt_set_errno(dtp, EDT_NOTYPE));
1370 dtrace_symbol_type(dtrace_hdl_t *dtp, const GElf_Sym *symp,
1371 const dtrace_syminfo_t *sip, dtrace_typeinfo_t *tip)
1375 tip->dtt_object = NULL;
1376 tip->dtt_ctfp = NULL;
1377 tip->dtt_type = CTF_ERR;
1379 if ((dmp = dt_module_lookup_by_name(dtp, sip->dts_object)) == NULL)
1380 return (dt_set_errno(dtp, EDT_NOMOD));
1382 if (symp->st_shndx == SHN_UNDEF && dmp->dm_extern != NULL) {
1384 dt_idhash_lookup(dmp->dm_extern, sip->dts_name);
1387 return (dt_set_errno(dtp, EDT_NOSYM));
1389 tip->dtt_ctfp = idp->di_ctfp;
1390 tip->dtt_type = idp->di_type;
1392 } else if (GELF_ST_TYPE(symp->st_info) != STT_FUNC) {
1393 if (dt_module_getctf(dtp, dmp) == NULL)
1394 return (-1); /* errno is set for us */
1396 tip->dtt_ctfp = dmp->dm_ctfp;
1397 tip->dtt_type = ctf_lookup_by_symbol(dmp->dm_ctfp, sip->dts_id);
1399 if (tip->dtt_type == CTF_ERR) {
1400 dtp->dt_ctferr = ctf_errno(tip->dtt_ctfp);
1401 return (dt_set_errno(dtp, EDT_CTF));
1405 tip->dtt_ctfp = DT_FPTR_CTFP(dtp);
1406 tip->dtt_type = DT_FPTR_TYPE(dtp);
1409 tip->dtt_object = dmp->dm_name;
1413 static dtrace_objinfo_t *
1414 dt_module_info(const dt_module_t *dmp, dtrace_objinfo_t *dto)
1416 dto->dto_name = dmp->dm_name;
1417 dto->dto_file = dmp->dm_file;
1418 dto->dto_id = dmp->dm_modid;
1421 if (dmp->dm_flags & DT_DM_KERNEL)
1422 dto->dto_flags |= DTRACE_OBJ_F_KERNEL;
1423 if (dmp->dm_flags & DT_DM_PRIMARY)
1424 dto->dto_flags |= DTRACE_OBJ_F_PRIMARY;
1426 dto->dto_text_va = dmp->dm_text_va;
1427 dto->dto_text_size = dmp->dm_text_size;
1428 dto->dto_data_va = dmp->dm_data_va;
1429 dto->dto_data_size = dmp->dm_data_size;
1430 dto->dto_bss_va = dmp->dm_bss_va;
1431 dto->dto_bss_size = dmp->dm_bss_size;
1437 dtrace_object_iter(dtrace_hdl_t *dtp, dtrace_obj_f *func, void *data)
1439 const dt_module_t *dmp = dt_list_next(&dtp->dt_modlist);
1440 dtrace_objinfo_t dto;
1443 for (; dmp != NULL; dmp = dt_list_next(dmp)) {
1444 if ((rv = (*func)(dtp, dt_module_info(dmp, &dto), data)) != 0)
1452 dtrace_object_info(dtrace_hdl_t *dtp, const char *object, dtrace_objinfo_t *dto)
1456 if (object == DTRACE_OBJ_EVERY || object == DTRACE_OBJ_KMODS ||
1457 object == DTRACE_OBJ_UMODS || dto == NULL)
1458 return (dt_set_errno(dtp, EINVAL));
1460 if ((dmp = dt_module_from_object(dtp, object)) == NULL)
1461 return (-1); /* dt_errno is set for us */
1463 if (dt_module_load(dtp, dmp) == -1)
1464 return (-1); /* dt_errno is set for us */
1466 (void) dt_module_info(dmp, dto);