Path: blob/main/cddl/contrib/opensolaris/lib/libdtrace/common/dt_module.c
39562 views
/*1* CDDL HEADER START2*3* The contents of this file are subject to the terms of the4* Common Development and Distribution License (the "License").5* You may not use this file except in compliance with the License.6*7* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE8* or http://www.opensolaris.org/os/licensing.9* See the License for the specific language governing permissions10* and limitations under the License.11*12* When distributing Covered Code, include this CDDL HEADER in each13* file and include the License file at usr/src/OPENSOLARIS.LICENSE.14* If applicable, add the following below this CDDL HEADER, with the15* fields enclosed by brackets "[]" replaced with your own identifying16* information: Portions Copyright [yyyy] [name of copyright owner]17*18* CDDL HEADER END19*/2021/*22* Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.23*/24/*25* Copyright (c) 2013, Joyent, Inc. All rights reserved.26* Copyright (c) 2016, Pedro Giffuni. All rights reserved.27*/2829#include <sys/types.h>30#ifdef illumos31#include <sys/modctl.h>32#include <sys/kobj.h>33#include <sys/kobj_impl.h>34#include <sys/sysmacros.h>35#include <sys/elf.h>36#include <sys/task.h>37#else38#include <sys/param.h>39#include <sys/linker.h>40#include <sys/module.h>41#include <sys/stat.h>42#endif4344#include <unistd.h>45#ifdef illumos46#include <project.h>47#endif48#include <strings.h>49#include <stdlib.h>50#include <libelf.h>51#include <limits.h>52#include <assert.h>53#include <errno.h>54#include <dirent.h>55#ifndef illumos56#include <fcntl.h>57#include <libproc_compat.h>58#endif5960#include <dt_strtab.h>61#include <dt_module.h>62#include <dt_impl.h>6364static const char *dt_module_strtab; /* active strtab for qsort callbacks */6566static void67dt_module_symhash_insert(dt_module_t *dmp, const char *name, uint_t id)68{69dt_sym_t *dsp = &dmp->dm_symchains[dmp->dm_symfree];70uint_t h;7172assert(dmp->dm_symfree < dmp->dm_nsymelems + 1);7374dsp->ds_symid = id;75h = dt_strtab_hash(name, NULL) % dmp->dm_nsymbuckets;76dsp->ds_next = dmp->dm_symbuckets[h];77dmp->dm_symbuckets[h] = dmp->dm_symfree++;78}7980static uint_t81dt_module_syminit32(dt_module_t *dmp)82{83#if STT_NUM != (STT_TLS + 1)84#error "STT_NUM has grown. update dt_module_syminit32()"85#endif8687Elf32_Sym *sym = dmp->dm_symtab.cts_data;88const char *base = dmp->dm_strtab.cts_data;89size_t ss_size = dmp->dm_strtab.cts_size;90uint_t i, n = dmp->dm_nsymelems;91uint_t asrsv = 0;9293#if defined(__FreeBSD__)94GElf_Ehdr ehdr;95int is_elf_obj;9697gelf_getehdr(dmp->dm_elf, &ehdr);98is_elf_obj = (ehdr.e_type == ET_REL);99#endif100101for (i = 0; i < n; i++, sym++) {102const char *name = base + sym->st_name;103uchar_t type = ELF32_ST_TYPE(sym->st_info);104105if (type >= STT_NUM || type == STT_SECTION)106continue; /* skip sections and unknown types */107108if (sym->st_name == 0 || sym->st_name >= ss_size)109continue; /* skip null or invalid names */110111if (ELF32_ST_BIND(sym->st_info) != STB_LOCAL || sym->st_size) {112asrsv++; /* reserve space in the address map */113114#if defined(__FreeBSD__)115sym->st_value += (Elf_Addr) dmp->dm_reloc_offset;116if (is_elf_obj && sym->st_shndx != SHN_UNDEF &&117sym->st_shndx < ehdr.e_shnum)118sym->st_value +=119dmp->dm_sec_offsets[sym->st_shndx];120#endif121}122123dt_module_symhash_insert(dmp, name, i);124}125126return (asrsv);127}128129static uint_t130dt_module_syminit64(dt_module_t *dmp)131{132#if STT_NUM != (STT_TLS + 1)133#error "STT_NUM has grown. update dt_module_syminit64()"134#endif135136Elf64_Sym *sym = dmp->dm_symtab.cts_data;137const char *base = dmp->dm_strtab.cts_data;138size_t ss_size = dmp->dm_strtab.cts_size;139uint_t i, n = dmp->dm_nsymelems;140uint_t asrsv = 0;141142#if defined(__FreeBSD__)143GElf_Ehdr ehdr;144int is_elf_obj;145146gelf_getehdr(dmp->dm_elf, &ehdr);147is_elf_obj = (ehdr.e_type == ET_REL);148#endif149150for (i = 0; i < n; i++, sym++) {151const char *name = base + sym->st_name;152uchar_t type = ELF64_ST_TYPE(sym->st_info);153154if (type >= STT_NUM || type == STT_SECTION)155continue; /* skip sections and unknown types */156157if (sym->st_name == 0 || sym->st_name >= ss_size)158continue; /* skip null or invalid names */159160if (ELF64_ST_BIND(sym->st_info) != STB_LOCAL || sym->st_size) {161asrsv++; /* reserve space in the address map */162#if defined(__FreeBSD__)163sym->st_value += (Elf_Addr) dmp->dm_reloc_offset;164if (is_elf_obj && sym->st_shndx != SHN_UNDEF &&165sym->st_shndx < ehdr.e_shnum)166sym->st_value +=167dmp->dm_sec_offsets[sym->st_shndx];168#endif169}170171dt_module_symhash_insert(dmp, name, i);172}173174return (asrsv);175}176177/*178* Sort comparison function for 32-bit symbol address-to-name lookups. We sort179* symbols by value. If values are equal, we prefer the symbol that is180* non-zero sized, typed, not weak, or lexically first, in that order.181*/182static int183dt_module_symcomp32(const void *lp, const void *rp)184{185Elf32_Sym *lhs = *((Elf32_Sym **)lp);186Elf32_Sym *rhs = *((Elf32_Sym **)rp);187188if (lhs->st_value != rhs->st_value)189return (lhs->st_value > rhs->st_value ? 1 : -1);190191if ((lhs->st_size == 0) != (rhs->st_size == 0))192return (lhs->st_size == 0 ? 1 : -1);193194if ((ELF32_ST_TYPE(lhs->st_info) == STT_NOTYPE) !=195(ELF32_ST_TYPE(rhs->st_info) == STT_NOTYPE))196return (ELF32_ST_TYPE(lhs->st_info) == STT_NOTYPE ? 1 : -1);197198if ((ELF32_ST_BIND(lhs->st_info) == STB_WEAK) !=199(ELF32_ST_BIND(rhs->st_info) == STB_WEAK))200return (ELF32_ST_BIND(lhs->st_info) == STB_WEAK ? 1 : -1);201202return (strcmp(dt_module_strtab + lhs->st_name,203dt_module_strtab + rhs->st_name));204}205206/*207* Sort comparison function for 64-bit symbol address-to-name lookups. We sort208* symbols by value. If values are equal, we prefer the symbol that is209* non-zero sized, typed, not weak, or lexically first, in that order.210*/211static int212dt_module_symcomp64(const void *lp, const void *rp)213{214Elf64_Sym *lhs = *((Elf64_Sym **)lp);215Elf64_Sym *rhs = *((Elf64_Sym **)rp);216217if (lhs->st_value != rhs->st_value)218return (lhs->st_value > rhs->st_value ? 1 : -1);219220if ((lhs->st_size == 0) != (rhs->st_size == 0))221return (lhs->st_size == 0 ? 1 : -1);222223if ((ELF64_ST_TYPE(lhs->st_info) == STT_NOTYPE) !=224(ELF64_ST_TYPE(rhs->st_info) == STT_NOTYPE))225return (ELF64_ST_TYPE(lhs->st_info) == STT_NOTYPE ? 1 : -1);226227if ((ELF64_ST_BIND(lhs->st_info) == STB_WEAK) !=228(ELF64_ST_BIND(rhs->st_info) == STB_WEAK))229return (ELF64_ST_BIND(lhs->st_info) == STB_WEAK ? 1 : -1);230231return (strcmp(dt_module_strtab + lhs->st_name,232dt_module_strtab + rhs->st_name));233}234235static void236dt_module_symsort32(dt_module_t *dmp)237{238Elf32_Sym *symtab = (Elf32_Sym *)dmp->dm_symtab.cts_data;239Elf32_Sym **sympp = (Elf32_Sym **)dmp->dm_asmap;240const dt_sym_t *dsp = dmp->dm_symchains + 1;241uint_t i, n = dmp->dm_symfree;242243for (i = 1; i < n; i++, dsp++) {244Elf32_Sym *sym = symtab + dsp->ds_symid;245if (ELF32_ST_BIND(sym->st_info) != STB_LOCAL || sym->st_size)246*sympp++ = sym;247}248249dmp->dm_aslen = (uint_t)(sympp - (Elf32_Sym **)dmp->dm_asmap);250assert(dmp->dm_aslen <= dmp->dm_asrsv);251252dt_module_strtab = dmp->dm_strtab.cts_data;253qsort(dmp->dm_asmap, dmp->dm_aslen,254sizeof (Elf32_Sym *), dt_module_symcomp32);255dt_module_strtab = NULL;256}257258static void259dt_module_symsort64(dt_module_t *dmp)260{261Elf64_Sym *symtab = (Elf64_Sym *)dmp->dm_symtab.cts_data;262Elf64_Sym **sympp = (Elf64_Sym **)dmp->dm_asmap;263const dt_sym_t *dsp = dmp->dm_symchains + 1;264uint_t i, n = dmp->dm_symfree;265266for (i = 1; i < n; i++, dsp++) {267Elf64_Sym *sym = symtab + dsp->ds_symid;268if (ELF64_ST_BIND(sym->st_info) != STB_LOCAL || sym->st_size)269*sympp++ = sym;270}271272dmp->dm_aslen = (uint_t)(sympp - (Elf64_Sym **)dmp->dm_asmap);273assert(dmp->dm_aslen <= dmp->dm_asrsv);274275dt_module_strtab = dmp->dm_strtab.cts_data;276qsort(dmp->dm_asmap, dmp->dm_aslen,277sizeof (Elf64_Sym *), dt_module_symcomp64);278dt_module_strtab = NULL;279}280281static GElf_Sym *282dt_module_symgelf32(const Elf32_Sym *src, GElf_Sym *dst)283{284if (dst != NULL) {285dst->st_name = src->st_name;286dst->st_info = src->st_info;287dst->st_other = src->st_other;288dst->st_shndx = src->st_shndx;289dst->st_value = src->st_value;290dst->st_size = src->st_size;291}292293return (dst);294}295296static GElf_Sym *297dt_module_symgelf64(const Elf64_Sym *src, GElf_Sym *dst)298{299if (dst != NULL)300bcopy(src, dst, sizeof (GElf_Sym));301302return (dst);303}304305static GElf_Sym *306dt_module_symname32(dt_module_t *dmp, const char *name,307GElf_Sym *symp, uint_t *idp)308{309const Elf32_Sym *symtab = dmp->dm_symtab.cts_data;310const char *strtab = dmp->dm_strtab.cts_data;311312const Elf32_Sym *sym;313const dt_sym_t *dsp;314uint_t i, h;315316if (dmp->dm_nsymelems == 0)317return (NULL);318319h = dt_strtab_hash(name, NULL) % dmp->dm_nsymbuckets;320321for (i = dmp->dm_symbuckets[h]; i != 0; i = dsp->ds_next) {322dsp = &dmp->dm_symchains[i];323sym = symtab + dsp->ds_symid;324325if (strcmp(name, strtab + sym->st_name) == 0) {326if (idp != NULL)327*idp = dsp->ds_symid;328return (dt_module_symgelf32(sym, symp));329}330}331332return (NULL);333}334335static GElf_Sym *336dt_module_symname64(dt_module_t *dmp, const char *name,337GElf_Sym *symp, uint_t *idp)338{339const Elf64_Sym *symtab = dmp->dm_symtab.cts_data;340const char *strtab = dmp->dm_strtab.cts_data;341342const Elf64_Sym *sym;343const dt_sym_t *dsp;344uint_t i, h;345346if (dmp->dm_nsymelems == 0)347return (NULL);348349h = dt_strtab_hash(name, NULL) % dmp->dm_nsymbuckets;350351for (i = dmp->dm_symbuckets[h]; i != 0; i = dsp->ds_next) {352dsp = &dmp->dm_symchains[i];353sym = symtab + dsp->ds_symid;354355if (strcmp(name, strtab + sym->st_name) == 0) {356if (idp != NULL)357*idp = dsp->ds_symid;358return (dt_module_symgelf64(sym, symp));359}360}361362return (NULL);363}364365static GElf_Sym *366dt_module_symaddr32(dt_module_t *dmp, GElf_Addr addr,367GElf_Sym *symp, uint_t *idp)368{369const Elf32_Sym **asmap = (const Elf32_Sym **)dmp->dm_asmap;370const Elf32_Sym *symtab = dmp->dm_symtab.cts_data;371const Elf32_Sym *sym;372373uint_t i, mid, lo = 0, hi = dmp->dm_aslen - 1;374Elf32_Addr v;375376if (dmp->dm_aslen == 0)377return (NULL);378379while (hi - lo > 1) {380mid = (lo + hi) / 2;381if (addr >= asmap[mid]->st_value)382lo = mid;383else384hi = mid;385}386387i = addr < asmap[hi]->st_value ? lo : hi;388sym = asmap[i];389v = sym->st_value;390391/*392* If the previous entry has the same value, improve our choice. The393* order of equal-valued symbols is determined by the comparison func.394*/395while (i-- != 0 && asmap[i]->st_value == v)396sym = asmap[i];397398if (addr - sym->st_value < MAX(sym->st_size, 1)) {399if (idp != NULL)400*idp = (uint_t)(sym - symtab);401return (dt_module_symgelf32(sym, symp));402}403404return (NULL);405}406407static GElf_Sym *408dt_module_symaddr64(dt_module_t *dmp, GElf_Addr addr,409GElf_Sym *symp, uint_t *idp)410{411const Elf64_Sym **asmap = (const Elf64_Sym **)dmp->dm_asmap;412const Elf64_Sym *symtab = dmp->dm_symtab.cts_data;413const Elf64_Sym *sym;414415uint_t i, mid, lo = 0, hi = dmp->dm_aslen - 1;416Elf64_Addr v;417418if (dmp->dm_aslen == 0)419return (NULL);420421while (hi - lo > 1) {422mid = (lo + hi) / 2;423if (addr >= asmap[mid]->st_value)424lo = mid;425else426hi = mid;427}428429i = addr < asmap[hi]->st_value ? lo : hi;430sym = asmap[i];431v = sym->st_value;432433/*434* If the previous entry has the same value, improve our choice. The435* order of equal-valued symbols is determined by the comparison func.436*/437while (i-- != 0 && asmap[i]->st_value == v)438sym = asmap[i];439440if (addr - sym->st_value < MAX(sym->st_size, 1)) {441if (idp != NULL)442*idp = (uint_t)(sym - symtab);443return (dt_module_symgelf64(sym, symp));444}445446return (NULL);447}448449static const dt_modops_t dt_modops_32 = {450.do_syminit = dt_module_syminit32,451.do_symsort = dt_module_symsort32,452.do_symname = dt_module_symname32,453.do_symaddr = dt_module_symaddr32454};455456static const dt_modops_t dt_modops_64 = {457.do_syminit = dt_module_syminit64,458.do_symsort = dt_module_symsort64,459.do_symname = dt_module_symname64,460.do_symaddr = dt_module_symaddr64461};462463dt_module_t *464dt_module_create(dtrace_hdl_t *dtp, const char *name)465{466long pid;467char *eptr;468dt_ident_t *idp;469uint_t h = dt_strtab_hash(name, NULL) % dtp->dt_modbuckets;470dt_module_t *dmp;471472for (dmp = dtp->dt_mods[h]; dmp != NULL; dmp = dmp->dm_next) {473if (strcmp(dmp->dm_name, name) == 0)474return (dmp);475}476477if ((dmp = malloc(sizeof (dt_module_t))) == NULL)478return (NULL); /* caller must handle allocation failure */479480bzero(dmp, sizeof (dt_module_t));481(void) strlcpy(dmp->dm_name, name, sizeof (dmp->dm_name));482dt_list_append(&dtp->dt_modlist, dmp);483dmp->dm_next = dtp->dt_mods[h];484dtp->dt_mods[h] = dmp;485dtp->dt_nmods++;486487if (dtp->dt_conf.dtc_ctfmodel == CTF_MODEL_LP64)488dmp->dm_ops = &dt_modops_64;489else490dmp->dm_ops = &dt_modops_32;491492/*493* Modules for userland processes are special. They always refer to a494* specific process and have a copy of their CTF data from a specific495* instant in time. Any dt_module_t that begins with 'pid' is a module496* for a specific process, much like how any probe description that497* begins with 'pid' is special. pid123 refers to process 123. A module498* that is just 'pid' refers specifically to pid$target. This is499* generally done as D does not currently allow for macros to be500* evaluated when working with types.501*/502if (strncmp(dmp->dm_name, "pid", 3) == 0) {503errno = 0;504if (dmp->dm_name[3] == '\0') {505idp = dt_idhash_lookup(dtp->dt_macros, "target");506if (idp != NULL && idp->di_id != 0)507dmp->dm_pid = idp->di_id;508} else {509pid = strtol(dmp->dm_name + 3, &eptr, 10);510if (errno == 0 && *eptr == '\0')511dmp->dm_pid = (pid_t)pid;512else513dt_dprintf("encountered malformed pid "514"module: %s\n", dmp->dm_name);515}516}517518return (dmp);519}520521dt_module_t *522dt_module_lookup_by_name(dtrace_hdl_t *dtp, const char *name)523{524uint_t h = dt_strtab_hash(name, NULL) % dtp->dt_modbuckets;525dt_module_t *dmp;526527for (dmp = dtp->dt_mods[h]; dmp != NULL; dmp = dmp->dm_next) {528if (strcmp(dmp->dm_name, name) == 0)529return (dmp);530}531532return (NULL);533}534535/*ARGSUSED*/536dt_module_t *537dt_module_lookup_by_ctf(dtrace_hdl_t *dtp, ctf_file_t *ctfp)538{539return (ctfp ? ctf_getspecific(ctfp) : NULL);540}541542#ifdef __FreeBSD__543dt_kmodule_t *544dt_kmodule_lookup(dtrace_hdl_t *dtp, const char *name)545{546uint_t h = dt_strtab_hash(name, NULL) % dtp->dt_modbuckets;547dt_kmodule_t *dkmp;548549for (dkmp = dtp->dt_kmods[h]; dkmp != NULL; dkmp = dkmp->dkm_next) {550if (strcmp(dkmp->dkm_name, name) == 0)551return (dkmp);552}553554return (NULL);555}556#endif557558static int559dt_module_load_sect(dtrace_hdl_t *dtp, dt_module_t *dmp, ctf_sect_t *ctsp)560{561const char *s;562size_t shstrs;563GElf_Shdr sh;564Elf_Data *dp;565Elf_Scn *sp;566567if (elf_getshdrstrndx(dmp->dm_elf, &shstrs) == -1)568return (dt_set_errno(dtp, EDT_NOTLOADED));569570for (sp = NULL; (sp = elf_nextscn(dmp->dm_elf, sp)) != NULL; ) {571if (gelf_getshdr(sp, &sh) == NULL || sh.sh_type == SHT_NULL ||572(s = elf_strptr(dmp->dm_elf, shstrs, sh.sh_name)) == NULL)573continue; /* skip any malformed sections */574575if (sh.sh_type == ctsp->cts_type &&576sh.sh_entsize == ctsp->cts_entsize &&577strcmp(s, ctsp->cts_name) == 0)578break; /* section matches specification */579}580581/*582* If the section isn't found, return success but leave cts_data set583* to NULL and cts_size set to zero for our caller.584*/585if (sp == NULL || (dp = elf_getdata(sp, NULL)) == NULL)586return (0);587588#ifdef illumos589ctsp->cts_data = dp->d_buf;590#else591if ((ctsp->cts_data = malloc(dp->d_size)) == NULL)592return (0);593memcpy(ctsp->cts_data, dp->d_buf, dp->d_size);594#endif595ctsp->cts_size = dp->d_size;596597dt_dprintf("loaded %s [%s] (%lu bytes)\n",598dmp->dm_name, ctsp->cts_name, (ulong_t)ctsp->cts_size);599600return (0);601}602603typedef struct dt_module_cb_arg {604struct ps_prochandle *dpa_proc;605dtrace_hdl_t *dpa_dtp;606dt_module_t *dpa_dmp;607uint_t dpa_count;608} dt_module_cb_arg_t;609610/* ARGSUSED */611static int612dt_module_load_proc_count(void *arg, const prmap_t *prmap, const char *obj)613{614ctf_file_t *fp;615dt_module_cb_arg_t *dcp = arg;616617/* Try to grab a ctf container if it exists */618fp = Pname_to_ctf(dcp->dpa_proc, obj);619if (fp != NULL)620dcp->dpa_count++;621return (0);622}623624/* ARGSUSED */625static int626dt_module_load_proc_build(void *arg, const prmap_t *prmap, const char *obj)627{628ctf_file_t *fp;629char buf[MAXPATHLEN], *p;630dt_module_cb_arg_t *dcp = arg;631int count = dcp->dpa_count;632Lmid_t lmid;633634fp = Pname_to_ctf(dcp->dpa_proc, obj);635if (fp == NULL)636return (0);637fp = ctf_dup(fp);638if (fp == NULL)639return (0);640dcp->dpa_dmp->dm_libctfp[count] = fp;641/*642* While it'd be nice to simply use objname here, because of our prior643* actions we'll always get a resolved object name to its on disk file.644* Like the pid provider, we need to tell a bit of a lie here. The type645* that the user thinks of is in terms of the libraries they requested,646* eg. libc.so.1, they don't care about the fact that it's647* libc_hwcap.so.1.648*/649(void) Pobjname(dcp->dpa_proc, prmap->pr_vaddr, buf, sizeof (buf));650if ((p = strrchr(buf, '/')) == NULL)651p = buf;652else653p++;654655/*656* If for some reason we can't find a link map id for this module, which657* would be really quite weird. We instead just say the link map id is658* zero.659*/660if (Plmid(dcp->dpa_proc, prmap->pr_vaddr, &lmid) != 0)661lmid = 0;662663if (lmid == 0)664dcp->dpa_dmp->dm_libctfn[count] = strdup(p);665else666(void) asprintf(&dcp->dpa_dmp->dm_libctfn[count],667"LM%x`%s", lmid, p);668if (dcp->dpa_dmp->dm_libctfn[count] == NULL)669return (1);670ctf_setspecific(fp, dcp->dpa_dmp);671dcp->dpa_count++;672return (0);673}674675/*676* We've been asked to load data that belongs to another process. As such we're677* going to pgrab it at this instant, load everything that we might ever care678* about, and then drive on. The reason for this is that the process that we're679* interested in might be changing. As long as we have grabbed it, then this680* can't be a problem for us.681*682* For now, we're actually going to punt on most things and just try to get CTF683* data, nothing else. Basically this is only useful as a source of type684* information, we can't go and do the stacktrace lookups, etc.685*/686static int687dt_module_load_proc(dtrace_hdl_t *dtp, dt_module_t *dmp)688{689struct ps_prochandle *p;690dt_module_cb_arg_t arg;691692/*693* Note that on success we do not release this hold. We must hold this694* for our life time.695*/696p = dt_proc_grab(dtp, dmp->dm_pid, 0, PGRAB_RDONLY | PGRAB_FORCE);697if (p == NULL) {698dt_dprintf("failed to grab pid: %d\n", (int)dmp->dm_pid);699return (dt_set_errno(dtp, EDT_CANTLOAD));700}701dt_proc_lock(dtp, p);702703arg.dpa_proc = p;704arg.dpa_dtp = dtp;705arg.dpa_dmp = dmp;706arg.dpa_count = 0;707if (Pobject_iter_resolved(p, dt_module_load_proc_count, &arg) != 0) {708dt_dprintf("failed to iterate objects\n");709dt_proc_unlock(dtp, p);710dt_proc_release(dtp, p);711return (dt_set_errno(dtp, EDT_CANTLOAD));712}713714if (arg.dpa_count == 0) {715dt_dprintf("no ctf data present\n");716dt_proc_unlock(dtp, p);717dt_proc_release(dtp, p);718return (dt_set_errno(dtp, EDT_CANTLOAD));719}720721dmp->dm_libctfp = calloc(arg.dpa_count, sizeof (ctf_file_t *));722if (dmp->dm_libctfp == NULL) {723dt_proc_unlock(dtp, p);724dt_proc_release(dtp, p);725return (dt_set_errno(dtp, EDT_NOMEM));726}727728dmp->dm_libctfn = calloc(arg.dpa_count, sizeof (char *));729if (dmp->dm_libctfn == NULL) {730free(dmp->dm_libctfp);731dt_proc_unlock(dtp, p);732dt_proc_release(dtp, p);733return (dt_set_errno(dtp, EDT_NOMEM));734}735736dmp->dm_nctflibs = arg.dpa_count;737738arg.dpa_count = 0;739if (Pobject_iter_resolved(p, dt_module_load_proc_build, &arg) != 0) {740dt_proc_unlock(dtp, p);741dt_module_unload(dtp, dmp);742dt_proc_release(dtp, p);743return (dt_set_errno(dtp, EDT_CANTLOAD));744}745assert(arg.dpa_count == dmp->dm_nctflibs);746dt_dprintf("loaded %d ctf modules for pid %d\n", arg.dpa_count,747(int)dmp->dm_pid);748749dt_proc_unlock(dtp, p);750dt_proc_release(dtp, p);751dmp->dm_flags |= DT_DM_LOADED;752753return (0);754}755756int757dt_module_load(dtrace_hdl_t *dtp, dt_module_t *dmp)758{759if (dmp->dm_flags & DT_DM_LOADED)760return (0); /* module is already loaded */761762if (dmp->dm_pid != 0)763return (dt_module_load_proc(dtp, dmp));764765dmp->dm_ctdata.cts_name = ".SUNW_ctf";766dmp->dm_ctdata.cts_type = SHT_PROGBITS;767dmp->dm_ctdata.cts_flags = 0;768dmp->dm_ctdata.cts_data = NULL;769dmp->dm_ctdata.cts_size = 0;770dmp->dm_ctdata.cts_entsize = 0;771dmp->dm_ctdata.cts_offset = 0;772773dmp->dm_symtab.cts_name = ".symtab";774dmp->dm_symtab.cts_type = SHT_SYMTAB;775dmp->dm_symtab.cts_flags = 0;776dmp->dm_symtab.cts_data = NULL;777dmp->dm_symtab.cts_size = 0;778dmp->dm_symtab.cts_entsize = dmp->dm_ops == &dt_modops_64 ?779sizeof (Elf64_Sym) : sizeof (Elf32_Sym);780dmp->dm_symtab.cts_offset = 0;781782dmp->dm_strtab.cts_name = ".strtab";783dmp->dm_strtab.cts_type = SHT_STRTAB;784dmp->dm_strtab.cts_flags = 0;785dmp->dm_strtab.cts_data = NULL;786dmp->dm_strtab.cts_size = 0;787dmp->dm_strtab.cts_entsize = 0;788dmp->dm_strtab.cts_offset = 0;789790/*791* Attempt to load the module's CTF section, symbol table section, and792* string table section. Note that modules may not contain CTF data:793* this will result in a successful load_sect but data of size zero.794* We will then fail if dt_module_getctf() is called, as shown below.795*/796if (dt_module_load_sect(dtp, dmp, &dmp->dm_ctdata) == -1 ||797dt_module_load_sect(dtp, dmp, &dmp->dm_symtab) == -1 ||798dt_module_load_sect(dtp, dmp, &dmp->dm_strtab) == -1) {799dt_module_unload(dtp, dmp);800return (-1); /* dt_errno is set for us */801}802803/*804* Allocate the hash chains and hash buckets for symbol name lookup.805* This is relatively simple since the symbol table is of fixed size806* and is known in advance. We allocate one extra element since we807* use element indices instead of pointers and zero is our sentinel.808*/809dmp->dm_nsymelems =810dmp->dm_symtab.cts_size / dmp->dm_symtab.cts_entsize;811812dmp->dm_nsymbuckets = _dtrace_strbuckets;813dmp->dm_symfree = 1; /* first free element is index 1 */814815dmp->dm_symbuckets = calloc(dmp->dm_nsymbuckets, sizeof (uint_t));816dmp->dm_symchains = calloc(dmp->dm_nsymelems + 1, sizeof (dt_sym_t));817818if (dmp->dm_symbuckets == NULL || dmp->dm_symchains == NULL) {819dt_module_unload(dtp, dmp);820return (dt_set_errno(dtp, EDT_NOMEM));821}822823/*824* Iterate over the symbol table data buffer and insert each symbol825* name into the name hash if the name and type are valid. Then826* allocate the address map, fill it in, and sort it.827*/828dmp->dm_asrsv = dmp->dm_ops->do_syminit(dmp);829830dt_dprintf("hashed %s [%s] (%u symbols)\n",831dmp->dm_name, dmp->dm_symtab.cts_name, dmp->dm_symfree - 1);832833if ((dmp->dm_asmap = malloc(sizeof (void *) * dmp->dm_asrsv)) == NULL) {834dt_module_unload(dtp, dmp);835return (dt_set_errno(dtp, EDT_NOMEM));836}837838dmp->dm_ops->do_symsort(dmp);839840dt_dprintf("sorted %s [%s] (%u symbols)\n",841dmp->dm_name, dmp->dm_symtab.cts_name, dmp->dm_aslen);842843dmp->dm_flags |= DT_DM_LOADED;844return (0);845}846847int848dt_module_hasctf(dtrace_hdl_t *dtp, dt_module_t *dmp)849{850if (dmp->dm_pid != 0 && dmp->dm_nctflibs > 0)851return (1);852return (dt_module_getctf(dtp, dmp) != NULL);853}854855ctf_file_t *856dt_module_getctf(dtrace_hdl_t *dtp, dt_module_t *dmp)857{858const char *parent;859dt_module_t *pmp;860ctf_file_t *pfp;861int model;862863if (dmp->dm_ctfp != NULL || dt_module_load(dtp, dmp) != 0)864return (dmp->dm_ctfp);865866if (dmp->dm_ops == &dt_modops_64)867model = CTF_MODEL_LP64;868else869model = CTF_MODEL_ILP32;870871/*872* If the data model of the module does not match our program data873* model, then do not permit CTF from this module to be opened and874* returned to the compiler. If we support mixed data models in the875* future for combined kernel/user tracing, this can be removed.876*/877if (dtp->dt_conf.dtc_ctfmodel != model) {878(void) dt_set_errno(dtp, EDT_DATAMODEL);879return (NULL);880}881882if (dmp->dm_ctdata.cts_size == 0) {883(void) dt_set_errno(dtp, EDT_NOCTF);884return (NULL);885}886887dmp->dm_ctfp = ctf_bufopen(&dmp->dm_ctdata,888&dmp->dm_symtab, &dmp->dm_strtab, &dtp->dt_ctferr);889890if (dmp->dm_ctfp == NULL) {891(void) dt_set_errno(dtp, EDT_CTF);892return (NULL);893}894895(void) ctf_setmodel(dmp->dm_ctfp, model);896ctf_setspecific(dmp->dm_ctfp, dmp);897898if ((parent = ctf_parent_name(dmp->dm_ctfp)) != NULL) {899if ((pmp = dt_module_create(dtp, parent)) == NULL ||900(pfp = dt_module_getctf(dtp, pmp)) == NULL) {901if (pmp == NULL)902(void) dt_set_errno(dtp, EDT_NOMEM);903goto err;904}905906if (ctf_import(dmp->dm_ctfp, pfp) == CTF_ERR) {907dtp->dt_ctferr = ctf_errno(dmp->dm_ctfp);908(void) dt_set_errno(dtp, EDT_CTF);909goto err;910}911}912913dt_dprintf("loaded CTF container for %s (%p)\n",914dmp->dm_name, (void *)dmp->dm_ctfp);915916return (dmp->dm_ctfp);917918err:919ctf_close(dmp->dm_ctfp);920dmp->dm_ctfp = NULL;921return (NULL);922}923924/*ARGSUSED*/925void926dt_module_unload(dtrace_hdl_t *dtp, dt_module_t *dmp)927{928int i;929930ctf_close(dmp->dm_ctfp);931dmp->dm_ctfp = NULL;932933#ifndef illumos934if (dmp->dm_ctdata.cts_data != NULL) {935free(dmp->dm_ctdata.cts_data);936}937if (dmp->dm_symtab.cts_data != NULL) {938free(dmp->dm_symtab.cts_data);939}940if (dmp->dm_strtab.cts_data != NULL) {941free(dmp->dm_strtab.cts_data);942}943#endif944945if (dmp->dm_libctfp != NULL) {946for (i = 0; i < dmp->dm_nctflibs; i++) {947ctf_close(dmp->dm_libctfp[i]);948free(dmp->dm_libctfn[i]);949}950free(dmp->dm_libctfp);951free(dmp->dm_libctfn);952dmp->dm_libctfp = NULL;953dmp->dm_nctflibs = 0;954}955956bzero(&dmp->dm_ctdata, sizeof (ctf_sect_t));957bzero(&dmp->dm_symtab, sizeof (ctf_sect_t));958bzero(&dmp->dm_strtab, sizeof (ctf_sect_t));959960if (dmp->dm_symbuckets != NULL) {961free(dmp->dm_symbuckets);962dmp->dm_symbuckets = NULL;963}964965if (dmp->dm_symchains != NULL) {966free(dmp->dm_symchains);967dmp->dm_symchains = NULL;968}969970if (dmp->dm_asmap != NULL) {971free(dmp->dm_asmap);972dmp->dm_asmap = NULL;973}974#if defined(__FreeBSD__)975if (dmp->dm_sec_offsets != NULL) {976free(dmp->dm_sec_offsets);977dmp->dm_sec_offsets = NULL;978}979#endif980dmp->dm_symfree = 0;981dmp->dm_nsymbuckets = 0;982dmp->dm_nsymelems = 0;983dmp->dm_asrsv = 0;984dmp->dm_aslen = 0;985986dmp->dm_text_va = 0;987dmp->dm_text_size = 0;988dmp->dm_data_va = 0;989dmp->dm_data_size = 0;990dmp->dm_bss_va = 0;991dmp->dm_bss_size = 0;992993if (dmp->dm_extern != NULL) {994dt_idhash_destroy(dmp->dm_extern);995dmp->dm_extern = NULL;996}997998(void) elf_end(dmp->dm_elf);999dmp->dm_elf = NULL;10001001dmp->dm_pid = 0;10021003dmp->dm_flags &= ~DT_DM_LOADED;1004}10051006void1007dt_module_destroy(dtrace_hdl_t *dtp, dt_module_t *dmp)1008{1009uint_t h = dt_strtab_hash(dmp->dm_name, NULL) % dtp->dt_modbuckets;1010dt_module_t **dmpp = &dtp->dt_mods[h];10111012dt_list_delete(&dtp->dt_modlist, dmp);1013assert(dtp->dt_nmods != 0);1014dtp->dt_nmods--;10151016/*1017* Now remove this module from its hash chain. We expect to always1018* find the module on its hash chain, so in this loop we assert that1019* we don't run off the end of the list.1020*/1021while (*dmpp != dmp) {1022dmpp = &((*dmpp)->dm_next);1023assert(*dmpp != NULL);1024}10251026*dmpp = dmp->dm_next;10271028dt_module_unload(dtp, dmp);1029free(dmp);1030}10311032/*1033* Insert a new external symbol reference into the specified module. The new1034* symbol will be marked as undefined and is assigned a symbol index beyond1035* any existing cached symbols from this module. We use the ident's di_data1036* field to store a pointer to a copy of the dtrace_syminfo_t for this symbol.1037*/1038dt_ident_t *1039dt_module_extern(dtrace_hdl_t *dtp, dt_module_t *dmp,1040const char *name, const dtrace_typeinfo_t *tip)1041{1042dtrace_syminfo_t *sip;1043dt_ident_t *idp;1044uint_t id;10451046if (dmp->dm_extern == NULL && (dmp->dm_extern = dt_idhash_create(1047"extern", NULL, dmp->dm_nsymelems, UINT_MAX)) == NULL) {1048(void) dt_set_errno(dtp, EDT_NOMEM);1049return (NULL);1050}10511052if (dt_idhash_nextid(dmp->dm_extern, &id) == -1) {1053(void) dt_set_errno(dtp, EDT_SYMOFLOW);1054return (NULL);1055}10561057if ((sip = malloc(sizeof (dtrace_syminfo_t))) == NULL) {1058(void) dt_set_errno(dtp, EDT_NOMEM);1059return (NULL);1060}10611062idp = dt_idhash_insert(dmp->dm_extern, name, DT_IDENT_SYMBOL, 0, id,1063_dtrace_symattr, 0, &dt_idops_thaw, NULL, dtp->dt_gen);10641065if (idp == NULL) {1066(void) dt_set_errno(dtp, EDT_NOMEM);1067free(sip);1068return (NULL);1069}10701071sip->dts_object = dmp->dm_name;1072sip->dts_name = idp->di_name;1073sip->dts_id = idp->di_id;10741075idp->di_data = sip;1076idp->di_ctfp = tip->dtt_ctfp;1077idp->di_type = tip->dtt_type;10781079return (idp);1080}10811082const char *1083dt_module_modelname(dt_module_t *dmp)1084{1085if (dmp->dm_ops == &dt_modops_64)1086return ("64-bit");1087else1088return ("32-bit");1089}10901091/* ARGSUSED */1092int1093dt_module_getlibid(dtrace_hdl_t *dtp, dt_module_t *dmp, const ctf_file_t *fp)1094{1095int i;10961097for (i = 0; i < dmp->dm_nctflibs; i++) {1098if (dmp->dm_libctfp[i] == fp)1099return (i);1100}11011102return (-1);1103}11041105/* ARGSUSED */1106ctf_file_t *1107dt_module_getctflib(dtrace_hdl_t *dtp, dt_module_t *dmp, const char *name)1108{1109int i;11101111for (i = 0; i < dmp->dm_nctflibs; i++) {1112if (strcmp(dmp->dm_libctfn[i], name) == 0)1113return (dmp->dm_libctfp[i]);1114}11151116return (NULL);1117}11181119/*1120* Update our module cache by adding an entry for the specified module 'name'.1121* We create the dt_module_t and populate it using /system/object/<name>/.1122*1123* On FreeBSD, the module name is passed as the full module file name,1124* including the path.1125*/1126static void1127dt_module_update(dtrace_hdl_t *dtp, struct kld_file_stat *k_stat)1128{1129char fname[MAXPATHLEN];1130struct stat64 st;1131int fd, err, bits;1132struct module_stat ms;1133dt_kmodule_t *dkmp;1134uint_t h;1135int modid;1136dt_module_t *dmp;1137const char *s;1138size_t shstrs;1139GElf_Shdr sh;1140Elf_Data *dp;1141Elf_Scn *sp;1142GElf_Ehdr ehdr;1143GElf_Phdr ph;1144char name[MAXPATHLEN];1145uintptr_t mapbase, alignmask;1146int i = 0;1147int is_elf_obj;11481149(void) strlcpy(name, k_stat->name, sizeof(name));1150(void) strlcpy(fname, k_stat->pathname, sizeof(fname));11511152if ((fd = open(fname, O_RDONLY)) == -1 || fstat64(fd, &st) == -1 ||1153(dmp = dt_module_create(dtp, name)) == NULL) {1154dt_dprintf("failed to open %s: %s\n", fname, strerror(errno));1155(void) close(fd);1156return;1157}11581159(void) strlcpy(dmp->dm_file, fname, sizeof(dmp->dm_file));1160dmp->dm_modid = k_stat->id;11611162/*1163* Since the module can unload out from under us (and /system/object1164* will return ENOENT), tell libelf to cook the entire file now and1165* then close the underlying file descriptor immediately. If this1166* succeeds, we know that we can continue safely using dmp->dm_elf.1167*/1168dmp->dm_elf = elf_begin(fd, ELF_C_READ, NULL);1169err = elf_cntl(dmp->dm_elf, ELF_C_FDREAD);1170(void) close(fd);11711172if (dmp->dm_elf == NULL || err == -1 ||1173elf_getshdrstrndx(dmp->dm_elf, &shstrs) == -1) {1174dt_dprintf("failed to load %s: %s\n",1175fname, elf_errmsg(elf_errno()));1176dt_module_destroy(dtp, dmp);1177return;1178}11791180switch (gelf_getclass(dmp->dm_elf)) {1181case ELFCLASS32:1182dmp->dm_ops = &dt_modops_32;1183bits = 32;1184break;1185case ELFCLASS64:1186dmp->dm_ops = &dt_modops_64;1187bits = 64;1188break;1189default:1190dt_dprintf("failed to load %s: unknown ELF class\n", fname);1191dt_module_destroy(dtp, dmp);1192return;1193}1194mapbase = (uintptr_t)k_stat->address;1195gelf_getehdr(dmp->dm_elf, &ehdr);1196is_elf_obj = (ehdr.e_type == ET_REL);1197if (is_elf_obj) {1198dmp->dm_sec_offsets =1199malloc(ehdr.e_shnum * sizeof(*dmp->dm_sec_offsets));1200if (dmp->dm_sec_offsets == NULL) {1201dt_dprintf("failed to allocate memory\n");1202dt_module_destroy(dtp, dmp);1203return;1204}1205}1206/*1207* Iterate over the section headers locating various sections of1208* interest and use their attributes to flesh out the dt_module_t.1209*/1210for (sp = NULL; (sp = elf_nextscn(dmp->dm_elf, sp)) != NULL; ) {1211if (gelf_getshdr(sp, &sh) == NULL || sh.sh_type == SHT_NULL ||1212(s = elf_strptr(dmp->dm_elf, shstrs, sh.sh_name)) == NULL)1213continue; /* skip any malformed sections */1214if (sh.sh_size == 0)1215continue;1216if (sh.sh_flags & SHF_ALLOC) {1217alignmask = sh.sh_addralign - 1;1218mapbase += alignmask;1219mapbase &= ~alignmask;1220sh.sh_addr = mapbase;1221if (is_elf_obj)1222dmp->dm_sec_offsets[elf_ndxscn(sp)] = sh.sh_addr;1223mapbase += sh.sh_size;1224}1225if (strcmp(s, ".text") == 0) {1226dmp->dm_text_size = sh.sh_size;1227dmp->dm_text_va = sh.sh_addr;1228} else if (strcmp(s, ".data") == 0) {1229dmp->dm_data_size = sh.sh_size;1230dmp->dm_data_va = sh.sh_addr;1231} else if (strcmp(s, ".bss") == 0) {1232dmp->dm_bss_size = sh.sh_size;1233dmp->dm_bss_va = sh.sh_addr;1234} else if (strcmp(s, ".info") == 0 &&1235(dp = elf_getdata(sp, NULL)) != NULL) {1236bcopy(dp->d_buf, &dmp->dm_info,1237MIN(sh.sh_size, sizeof (dmp->dm_info)));1238}1239}12401241dmp->dm_flags |= DT_DM_KERNEL;1242/*1243* Include .rodata and special sections into .text.1244* This depends on default section layout produced by GNU ld1245* for ELF objects and libraries:1246* [Text][R/O data][R/W data][Dynamic][BSS][Non loadable]1247*/1248dmp->dm_text_size = dmp->dm_data_va - dmp->dm_text_va;12491250if (!is_elf_obj) {1251/*1252* Find the first load section and figure out the relocation1253* offset for the symbols. The kernel module will not need1254* relocation, but the kernel linker modules will.1255*/1256for (i = 0; gelf_getphdr(dmp->dm_elf, i, &ph) != NULL; i++) {1257if (ph.p_type == PT_LOAD) {1258dmp->dm_reloc_offset =1259k_stat->address - ph.p_vaddr;1260break;1261}1262}1263}12641265if (dmp->dm_info.objfs_info_primary)1266dmp->dm_flags |= DT_DM_PRIMARY;12671268ms.version = sizeof(ms);1269for (modid = kldfirstmod(k_stat->id); modid > 0;1270modid = modnext(modid)) {1271if (modstat(modid, &ms) != 0) {1272dt_dprintf("modstat failed for id %d in %s: %s\n",1273modid, k_stat->name, strerror(errno));1274continue;1275}1276if (dt_kmodule_lookup(dtp, ms.name) != NULL)1277continue;12781279dkmp = malloc(sizeof (*dkmp));1280if (dkmp == NULL) {1281dt_dprintf("failed to allocate memory\n");1282dt_module_destroy(dtp, dmp);1283return;1284}12851286h = dt_strtab_hash(ms.name, NULL) % dtp->dt_modbuckets;1287dkmp->dkm_next = dtp->dt_kmods[h];1288dkmp->dkm_name = strdup(ms.name);1289dkmp->dkm_module = dmp;1290dtp->dt_kmods[h] = dkmp;1291}12921293dt_dprintf("opened %d-bit module %s (%s) [%d]\n",1294bits, dmp->dm_name, dmp->dm_file, dmp->dm_modid);1295}12961297/*1298* Unload all the loaded modules and then refresh the module cache with the1299* latest list of loaded modules and their address ranges.1300*/1301void1302dtrace_update(dtrace_hdl_t *dtp)1303{1304dt_module_t *dmp;1305DIR *dirp;1306#if defined(__FreeBSD__)1307int fileid;1308#endif13091310for (dmp = dt_list_next(&dtp->dt_modlist);1311dmp != NULL; dmp = dt_list_next(dmp))1312dt_module_unload(dtp, dmp);13131314#ifdef illumos1315/*1316* Open /system/object and attempt to create a libdtrace module for1317* each kernel module that is loaded on the current system.1318*/1319if (!(dtp->dt_oflags & DTRACE_O_NOSYS) &&1320(dirp = opendir(OBJFS_ROOT)) != NULL) {1321struct dirent *dp;13221323while ((dp = readdir(dirp)) != NULL) {1324if (dp->d_name[0] != '.')1325dt_module_update(dtp, dp->d_name);1326}13271328(void) closedir(dirp);1329}1330#elif defined(__FreeBSD__)1331/*1332* Use FreeBSD's kernel loader interface to discover what kernel1333* modules are loaded and create a libdtrace module for each one.1334*/1335for (fileid = kldnext(0); fileid > 0; fileid = kldnext(fileid)) {1336struct kld_file_stat k_stat;1337k_stat.version = sizeof(k_stat);1338if (kldstat(fileid, &k_stat) == 0)1339dt_module_update(dtp, &k_stat);1340}1341#endif13421343/*1344* Look up all the macro identifiers and set di_id to the latest value.1345* This code collaborates with dt_lex.l on the use of di_id. We will1346* need to implement something fancier if we need to support non-ints.1347*/1348dt_idhash_lookup(dtp->dt_macros, "egid")->di_id = getegid();1349dt_idhash_lookup(dtp->dt_macros, "euid")->di_id = geteuid();1350dt_idhash_lookup(dtp->dt_macros, "gid")->di_id = getgid();1351dt_idhash_lookup(dtp->dt_macros, "pid")->di_id = getpid();1352dt_idhash_lookup(dtp->dt_macros, "pgid")->di_id = getpgid(0);1353dt_idhash_lookup(dtp->dt_macros, "ppid")->di_id = getppid();1354#ifdef illumos1355dt_idhash_lookup(dtp->dt_macros, "projid")->di_id = getprojid();1356#endif1357dt_idhash_lookup(dtp->dt_macros, "sid")->di_id = getsid(0);1358#ifdef illumos1359dt_idhash_lookup(dtp->dt_macros, "taskid")->di_id = gettaskid();1360#endif1361dt_idhash_lookup(dtp->dt_macros, "uid")->di_id = getuid();13621363/*1364* Cache the pointers to the modules representing the base executable1365* and the run-time linker in the dtrace client handle. Note that on1366* x86 krtld is folded into unix, so if we don't find it, use unix1367* instead.1368*/1369dtp->dt_exec = dt_module_lookup_by_name(dtp, "genunix");1370dtp->dt_rtld = dt_module_lookup_by_name(dtp, "krtld");1371if (dtp->dt_rtld == NULL)1372dtp->dt_rtld = dt_module_lookup_by_name(dtp, "unix");13731374/*1375* If this is the first time we are initializing the module list,1376* remove the module for genunix from the module list and then move it1377* to the front of the module list. We do this so that type and symbol1378* queries encounter genunix and thereby optimize for the common case1379* in dtrace_lookup_by_name() and dtrace_lookup_by_type(), below.1380*/1381if (dtp->dt_exec != NULL &&1382dtp->dt_cdefs == NULL && dtp->dt_ddefs == NULL) {1383dt_list_delete(&dtp->dt_modlist, dtp->dt_exec);1384dt_list_prepend(&dtp->dt_modlist, dtp->dt_exec);1385}1386}13871388static dt_module_t *1389dt_module_from_object(dtrace_hdl_t *dtp, const char *object)1390{1391int err = EDT_NOMOD;1392dt_module_t *dmp;13931394switch ((uintptr_t)object) {1395case (uintptr_t)DTRACE_OBJ_EXEC:1396dmp = dtp->dt_exec;1397break;1398case (uintptr_t)DTRACE_OBJ_RTLD:1399dmp = dtp->dt_rtld;1400break;1401case (uintptr_t)DTRACE_OBJ_CDEFS:1402dmp = dtp->dt_cdefs;1403break;1404case (uintptr_t)DTRACE_OBJ_DDEFS:1405dmp = dtp->dt_ddefs;1406break;1407default:1408dmp = dt_module_create(dtp, object);1409err = EDT_NOMEM;1410}14111412if (dmp == NULL)1413(void) dt_set_errno(dtp, err);14141415return (dmp);1416}14171418/*1419* Exported interface to look up a symbol by name. We return the GElf_Sym and1420* complete symbol information for the matching symbol.1421*/1422int1423dtrace_lookup_by_name(dtrace_hdl_t *dtp, const char *object, const char *name,1424GElf_Sym *symp, dtrace_syminfo_t *sip)1425{1426dt_module_t *dmp;1427dt_ident_t *idp;1428uint_t n, id;1429GElf_Sym sym;14301431uint_t mask = 0; /* mask of dt_module flags to match */1432uint_t bits = 0; /* flag bits that must be present */14331434if (object != DTRACE_OBJ_EVERY &&1435object != DTRACE_OBJ_KMODS &&1436object != DTRACE_OBJ_UMODS) {1437if ((dmp = dt_module_from_object(dtp, object)) == NULL)1438return (-1); /* dt_errno is set for us */14391440if (dt_module_load(dtp, dmp) == -1)1441return (-1); /* dt_errno is set for us */1442n = 1;14431444} else {1445if (object == DTRACE_OBJ_KMODS)1446mask = bits = DT_DM_KERNEL;1447else if (object == DTRACE_OBJ_UMODS)1448mask = DT_DM_KERNEL;14491450dmp = dt_list_next(&dtp->dt_modlist);1451n = dtp->dt_nmods;1452}14531454if (symp == NULL)1455symp = &sym;14561457for (; n > 0; n--, dmp = dt_list_next(dmp)) {1458if ((dmp->dm_flags & mask) != bits)1459continue; /* failed to match required attributes */14601461if (dt_module_load(dtp, dmp) == -1)1462continue; /* failed to load symbol table */14631464if (dmp->dm_ops->do_symname(dmp, name, symp, &id) != NULL) {1465if (sip != NULL) {1466sip->dts_object = dmp->dm_name;1467sip->dts_name = (const char *)1468dmp->dm_strtab.cts_data + symp->st_name;1469sip->dts_id = id;1470}1471return (0);1472}14731474if (dmp->dm_extern != NULL &&1475(idp = dt_idhash_lookup(dmp->dm_extern, name)) != NULL) {1476if (symp != &sym) {1477symp->st_name = (uintptr_t)idp->di_name;1478symp->st_info =1479GELF_ST_INFO(STB_GLOBAL, STT_NOTYPE);1480symp->st_other = 0;1481symp->st_shndx = SHN_UNDEF;1482symp->st_value = 0;1483symp->st_size =1484ctf_type_size(idp->di_ctfp, idp->di_type);1485}14861487if (sip != NULL) {1488sip->dts_object = dmp->dm_name;1489sip->dts_name = idp->di_name;1490sip->dts_id = idp->di_id;1491}14921493return (0);1494}1495}14961497return (dt_set_errno(dtp, EDT_NOSYM));1498}14991500/*1501* Exported interface to look up a symbol by address. We return the GElf_Sym1502* and complete symbol information for the matching symbol.1503*/1504int1505dtrace_lookup_by_addr(dtrace_hdl_t *dtp, GElf_Addr addr,1506GElf_Sym *symp, dtrace_syminfo_t *sip)1507{1508dt_module_t *dmp;1509uint_t id;1510const dtrace_vector_t *v = dtp->dt_vector;15111512if (v != NULL)1513return (v->dtv_lookup_by_addr(dtp->dt_varg, addr, symp, sip));15141515for (dmp = dt_list_next(&dtp->dt_modlist); dmp != NULL;1516dmp = dt_list_next(dmp)) {1517if (addr - dmp->dm_text_va < dmp->dm_text_size ||1518addr - dmp->dm_data_va < dmp->dm_data_size ||1519addr - dmp->dm_bss_va < dmp->dm_bss_size)1520break;1521}15221523if (dmp == NULL)1524return (dt_set_errno(dtp, EDT_NOSYMADDR));15251526if (dt_module_load(dtp, dmp) == -1)1527return (-1); /* dt_errno is set for us */15281529if (symp != NULL) {1530if (dmp->dm_ops->do_symaddr(dmp, addr, symp, &id) == NULL)1531return (dt_set_errno(dtp, EDT_NOSYMADDR));1532}15331534if (sip != NULL) {1535sip->dts_object = dmp->dm_name;15361537if (symp != NULL) {1538sip->dts_name = (const char *)1539dmp->dm_strtab.cts_data + symp->st_name;1540sip->dts_id = id;1541} else {1542sip->dts_name = NULL;1543sip->dts_id = 0;1544}1545}15461547return (0);1548}15491550int1551dtrace_lookup_by_type(dtrace_hdl_t *dtp, const char *object, const char *name,1552dtrace_typeinfo_t *tip)1553{1554dtrace_typeinfo_t ti;1555dt_module_t *dmp;1556int found = 0;1557ctf_id_t id;1558uint_t n, i;1559int justone;1560ctf_file_t *fp;1561char *buf, *p, *q;15621563uint_t mask = 0; /* mask of dt_module flags to match */1564uint_t bits = 0; /* flag bits that must be present */15651566if (object != DTRACE_OBJ_EVERY &&1567object != DTRACE_OBJ_KMODS &&1568object != DTRACE_OBJ_UMODS) {1569if ((dmp = dt_module_from_object(dtp, object)) == NULL)1570return (-1); /* dt_errno is set for us */15711572if (dt_module_load(dtp, dmp) == -1)1573return (-1); /* dt_errno is set for us */1574n = 1;1575justone = 1;1576} else {1577if (object == DTRACE_OBJ_KMODS)1578mask = bits = DT_DM_KERNEL;1579else if (object == DTRACE_OBJ_UMODS)1580mask = DT_DM_KERNEL;15811582dmp = dt_list_next(&dtp->dt_modlist);1583n = dtp->dt_nmods;1584justone = 0;1585}15861587if (tip == NULL)1588tip = &ti;15891590for (; n > 0; n--, dmp = dt_list_next(dmp)) {1591if ((dmp->dm_flags & mask) != bits)1592continue; /* failed to match required attributes */15931594/*1595* If we can't load the CTF container, continue on to the next1596* module. If our search was scoped to only one module then1597* return immediately leaving dt_errno unmodified.1598*/1599if (dt_module_hasctf(dtp, dmp) == 0) {1600if (justone)1601return (-1);1602continue;1603}16041605/*1606* Look up the type in the module's CTF container. If our1607* match is a forward declaration tag, save this choice in1608* 'tip' and keep going in the hope that we will locate the1609* underlying structure definition. Otherwise just return.1610*/1611if (dmp->dm_pid == 0) {1612id = ctf_lookup_by_name(dmp->dm_ctfp, name);1613fp = dmp->dm_ctfp;1614} else {1615if ((p = strchr(name, '`')) != NULL) {1616buf = strdup(name);1617if (buf == NULL)1618return (dt_set_errno(dtp, EDT_NOMEM));1619p = strchr(buf, '`');1620if ((q = strchr(p + 1, '`')) != NULL)1621p = q;1622*p = '\0';1623fp = dt_module_getctflib(dtp, dmp, buf);1624if (fp == NULL || (id = ctf_lookup_by_name(fp,1625p + 1)) == CTF_ERR)1626id = CTF_ERR;1627free(buf);1628} else {1629for (i = 0; i < dmp->dm_nctflibs; i++) {1630fp = dmp->dm_libctfp[i];1631id = ctf_lookup_by_name(fp, name);1632if (id != CTF_ERR)1633break;1634}1635}1636}1637if (id != CTF_ERR) {1638tip->dtt_object = dmp->dm_name;1639tip->dtt_ctfp = fp;1640tip->dtt_type = id;1641if (ctf_type_kind(fp, ctf_type_resolve(fp, id)) !=1642CTF_K_FORWARD)1643return (0);16441645found++;1646}1647}16481649if (found == 0)1650return (dt_set_errno(dtp, EDT_NOTYPE));16511652return (0);1653}16541655int1656dtrace_symbol_type(dtrace_hdl_t *dtp, const GElf_Sym *symp,1657const dtrace_syminfo_t *sip, dtrace_typeinfo_t *tip)1658{1659dt_module_t *dmp;16601661tip->dtt_object = NULL;1662tip->dtt_ctfp = NULL;1663tip->dtt_type = CTF_ERR;1664tip->dtt_flags = 0;16651666if ((dmp = dt_module_lookup_by_name(dtp, sip->dts_object)) == NULL)1667return (dt_set_errno(dtp, EDT_NOMOD));16681669if (symp->st_shndx == SHN_UNDEF && dmp->dm_extern != NULL) {1670dt_ident_t *idp =1671dt_idhash_lookup(dmp->dm_extern, sip->dts_name);16721673if (idp == NULL)1674return (dt_set_errno(dtp, EDT_NOSYM));16751676tip->dtt_ctfp = idp->di_ctfp;1677tip->dtt_type = idp->di_type;16781679} else if (GELF_ST_TYPE(symp->st_info) != STT_FUNC) {1680if (dt_module_getctf(dtp, dmp) == NULL)1681return (-1); /* errno is set for us */16821683tip->dtt_ctfp = dmp->dm_ctfp;1684tip->dtt_type = ctf_lookup_by_symbol(dmp->dm_ctfp, sip->dts_id);16851686if (tip->dtt_type == CTF_ERR) {1687dtp->dt_ctferr = ctf_errno(tip->dtt_ctfp);1688return (dt_set_errno(dtp, EDT_CTF));1689}16901691} else {1692tip->dtt_ctfp = DT_FPTR_CTFP(dtp);1693tip->dtt_type = DT_FPTR_TYPE(dtp);1694}16951696tip->dtt_object = dmp->dm_name;1697return (0);1698}16991700static dtrace_objinfo_t *1701dt_module_info(const dt_module_t *dmp, dtrace_objinfo_t *dto)1702{1703dto->dto_name = dmp->dm_name;1704dto->dto_file = dmp->dm_file;1705dto->dto_id = dmp->dm_modid;1706dto->dto_flags = 0;17071708if (dmp->dm_flags & DT_DM_KERNEL)1709dto->dto_flags |= DTRACE_OBJ_F_KERNEL;1710if (dmp->dm_flags & DT_DM_PRIMARY)1711dto->dto_flags |= DTRACE_OBJ_F_PRIMARY;17121713dto->dto_text_va = dmp->dm_text_va;1714dto->dto_text_size = dmp->dm_text_size;1715dto->dto_data_va = dmp->dm_data_va;1716dto->dto_data_size = dmp->dm_data_size;1717dto->dto_bss_va = dmp->dm_bss_va;1718dto->dto_bss_size = dmp->dm_bss_size;17191720return (dto);1721}17221723int1724dtrace_object_iter(dtrace_hdl_t *dtp, dtrace_obj_f *func, void *data)1725{1726const dt_module_t *dmp = dt_list_next(&dtp->dt_modlist);1727dtrace_objinfo_t dto;1728int rv;17291730for (; dmp != NULL; dmp = dt_list_next(dmp)) {1731if ((rv = (*func)(dtp, dt_module_info(dmp, &dto), data)) != 0)1732return (rv);1733}17341735return (0);1736}17371738int1739dtrace_object_info(dtrace_hdl_t *dtp, const char *object, dtrace_objinfo_t *dto)1740{1741dt_module_t *dmp;17421743if (object == DTRACE_OBJ_EVERY || object == DTRACE_OBJ_KMODS ||1744object == DTRACE_OBJ_UMODS || dto == NULL)1745return (dt_set_errno(dtp, EINVAL));17461747if ((dmp = dt_module_from_object(dtp, object)) == NULL)1748return (-1); /* dt_errno is set for us */17491750if (dt_module_load(dtp, dmp) == -1)1751return (-1); /* dt_errno is set for us */17521753(void) dt_module_info(dmp, dto);1754return (0);1755}175617571758