1 /*
   2  * CDDL HEADER START
   3  *
   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.
   7  *
   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.
  12  *
  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]
  18  *
  19  * CDDL HEADER END
  20  */
  21 
  22 /*
  23  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
  24  */
  25 /*
  26  * Copyright (c) 2013, Joyent, Inc.  All rights reserved.
  27  */
  28 
  29 #include <sys/types.h>
  30 #include <sys/modctl.h>
  31 #include <sys/kobj.h>
  32 #include <sys/kobj_impl.h>
  33 #include <sys/sysmacros.h>
  34 #include <sys/elf.h>
  35 #include <sys/task.h>
  36 
  37 #include <unistd.h>
  38 #include <project.h>
  39 #include <strings.h>
  40 #include <stdlib.h>
  41 #include <libelf.h>
  42 #include <limits.h>
  43 #include <assert.h>
  44 #include <errno.h>
  45 #include <dirent.h>
  46 
  47 #include <dt_strtab.h>
  48 #include <dt_module.h>
  49 #include <dt_impl.h>
  50 
  51 static const char *dt_module_strtab; /* active strtab for qsort callbacks */
  52 
  53 static void
  54 dt_module_symhash_insert(dt_module_t *dmp, const char *name, uint_t id)
  55 {
  56         dt_sym_t *dsp = &dmp->dm_symchains[dmp->dm_symfree];
  57         uint_t h;
  58 
  59         assert(dmp->dm_symfree < dmp->dm_nsymelems + 1);
  60 
  61         dsp->ds_symid = id;
  62         h = dt_strtab_hash(name, NULL) % dmp->dm_nsymbuckets;
  63         dsp->ds_next = dmp->dm_symbuckets[h];
  64         dmp->dm_symbuckets[h] = dmp->dm_symfree++;
  65 }
  66 
  67 static uint_t
  68 dt_module_syminit32(dt_module_t *dmp)
  69 {
  70 #if STT_NUM != (STT_TLS + 1)
  71 #error "STT_NUM has grown. update dt_module_syminit32()"
  72 #endif
  73 
  74         const Elf32_Sym *sym = dmp->dm_symtab.cts_data;
  75         const char *base = dmp->dm_strtab.cts_data;
  76         size_t ss_size = dmp->dm_strtab.cts_size;
  77         uint_t i, n = dmp->dm_nsymelems;
  78         uint_t asrsv = 0;
  79 
  80         for (i = 0; i < n; i++, sym++) {
  81                 const char *name = base + sym->st_name;
  82                 uchar_t type = ELF32_ST_TYPE(sym->st_info);
  83 
  84                 if (type >= STT_NUM || type == STT_SECTION)
  85                         continue; /* skip sections and unknown types */
  86 
  87                 if (sym->st_name == 0 || sym->st_name >= ss_size)
  88                         continue; /* skip null or invalid names */
  89 
  90                 if (sym->st_value != 0 &&
  91                     (ELF32_ST_BIND(sym->st_info) != STB_LOCAL || sym->st_size))
  92                         asrsv++; /* reserve space in the address map */
  93 
  94                 dt_module_symhash_insert(dmp, name, i);
  95         }
  96 
  97         return (asrsv);
  98 }
  99 
 100 static uint_t
 101 dt_module_syminit64(dt_module_t *dmp)
 102 {
 103 #if STT_NUM != (STT_TLS + 1)
 104 #error "STT_NUM has grown. update dt_module_syminit64()"
 105 #endif
 106 
 107         const Elf64_Sym *sym = dmp->dm_symtab.cts_data;
 108         const char *base = dmp->dm_strtab.cts_data;
 109         size_t ss_size = dmp->dm_strtab.cts_size;
 110         uint_t i, n = dmp->dm_nsymelems;
 111         uint_t asrsv = 0;
 112 
 113         for (i = 0; i < n; i++, sym++) {
 114                 const char *name = base + sym->st_name;
 115                 uchar_t type = ELF64_ST_TYPE(sym->st_info);
 116 
 117                 if (type >= STT_NUM || type == STT_SECTION)
 118                         continue; /* skip sections and unknown types */
 119 
 120                 if (sym->st_name == 0 || sym->st_name >= ss_size)
 121                         continue; /* skip null or invalid names */
 122 
 123                 if (sym->st_value != 0 &&
 124                     (ELF64_ST_BIND(sym->st_info) != STB_LOCAL || sym->st_size))
 125                         asrsv++; /* reserve space in the address map */
 126 
 127                 dt_module_symhash_insert(dmp, name, i);
 128         }
 129 
 130         return (asrsv);
 131 }
 132 
 133 /*
 134  * Sort comparison function for 32-bit symbol address-to-name lookups.  We sort
 135  * symbols by value.  If values are equal, we prefer the symbol that is
 136  * non-zero sized, typed, not weak, or lexically first, in that order.
 137  */
 138 static int
 139 dt_module_symcomp32(const void *lp, const void *rp)
 140 {
 141         Elf32_Sym *lhs = *((Elf32_Sym **)lp);
 142         Elf32_Sym *rhs = *((Elf32_Sym **)rp);
 143 
 144         if (lhs->st_value != rhs->st_value)
 145                 return (lhs->st_value > rhs->st_value ? 1 : -1);
 146 
 147         if ((lhs->st_size == 0) != (rhs->st_size == 0))
 148                 return (lhs->st_size == 0 ? 1 : -1);
 149 
 150         if ((ELF32_ST_TYPE(lhs->st_info) == STT_NOTYPE) !=
 151             (ELF32_ST_TYPE(rhs->st_info) == STT_NOTYPE))
 152                 return (ELF32_ST_TYPE(lhs->st_info) == STT_NOTYPE ? 1 : -1);
 153 
 154         if ((ELF32_ST_BIND(lhs->st_info) == STB_WEAK) !=
 155             (ELF32_ST_BIND(rhs->st_info) == STB_WEAK))
 156                 return (ELF32_ST_BIND(lhs->st_info) == STB_WEAK ? 1 : -1);
 157 
 158         return (strcmp(dt_module_strtab + lhs->st_name,
 159             dt_module_strtab + rhs->st_name));
 160 }
 161 
 162 /*
 163  * Sort comparison function for 64-bit symbol address-to-name lookups.  We sort
 164  * symbols by value.  If values are equal, we prefer the symbol that is
 165  * non-zero sized, typed, not weak, or lexically first, in that order.
 166  */
 167 static int
 168 dt_module_symcomp64(const void *lp, const void *rp)
 169 {
 170         Elf64_Sym *lhs = *((Elf64_Sym **)lp);
 171         Elf64_Sym *rhs = *((Elf64_Sym **)rp);
 172 
 173         if (lhs->st_value != rhs->st_value)
 174                 return (lhs->st_value > rhs->st_value ? 1 : -1);
 175 
 176         if ((lhs->st_size == 0) != (rhs->st_size == 0))
 177                 return (lhs->st_size == 0 ? 1 : -1);
 178 
 179         if ((ELF64_ST_TYPE(lhs->st_info) == STT_NOTYPE) !=
 180             (ELF64_ST_TYPE(rhs->st_info) == STT_NOTYPE))
 181                 return (ELF64_ST_TYPE(lhs->st_info) == STT_NOTYPE ? 1 : -1);
 182 
 183         if ((ELF64_ST_BIND(lhs->st_info) == STB_WEAK) !=
 184             (ELF64_ST_BIND(rhs->st_info) == STB_WEAK))
 185                 return (ELF64_ST_BIND(lhs->st_info) == STB_WEAK ? 1 : -1);
 186 
 187         return (strcmp(dt_module_strtab + lhs->st_name,
 188             dt_module_strtab + rhs->st_name));
 189 }
 190 
 191 static void
 192 dt_module_symsort32(dt_module_t *dmp)
 193 {
 194         Elf32_Sym *symtab = (Elf32_Sym *)dmp->dm_symtab.cts_data;
 195         Elf32_Sym **sympp = (Elf32_Sym **)dmp->dm_asmap;
 196         const dt_sym_t *dsp = dmp->dm_symchains + 1;
 197         uint_t i, n = dmp->dm_symfree;
 198 
 199         for (i = 1; i < n; i++, dsp++) {
 200                 Elf32_Sym *sym = symtab + dsp->ds_symid;
 201                 if (sym->st_value != 0 &&
 202                     (ELF32_ST_BIND(sym->st_info) != STB_LOCAL || sym->st_size))
 203                         *sympp++ = sym;
 204         }
 205 
 206         dmp->dm_aslen = (uint_t)(sympp - (Elf32_Sym **)dmp->dm_asmap);
 207         assert(dmp->dm_aslen <= dmp->dm_asrsv);
 208 
 209         dt_module_strtab = dmp->dm_strtab.cts_data;
 210         qsort(dmp->dm_asmap, dmp->dm_aslen,
 211             sizeof (Elf32_Sym *), dt_module_symcomp32);
 212         dt_module_strtab = NULL;
 213 }
 214 
 215 static void
 216 dt_module_symsort64(dt_module_t *dmp)
 217 {
 218         Elf64_Sym *symtab = (Elf64_Sym *)dmp->dm_symtab.cts_data;
 219         Elf64_Sym **sympp = (Elf64_Sym **)dmp->dm_asmap;
 220         const dt_sym_t *dsp = dmp->dm_symchains + 1;
 221         uint_t i, n = dmp->dm_symfree;
 222 
 223         for (i = 1; i < n; i++, dsp++) {
 224                 Elf64_Sym *sym = symtab + dsp->ds_symid;
 225                 if (sym->st_value != 0 &&
 226                     (ELF64_ST_BIND(sym->st_info) != STB_LOCAL || sym->st_size))
 227                         *sympp++ = sym;
 228         }
 229 
 230         dmp->dm_aslen = (uint_t)(sympp - (Elf64_Sym **)dmp->dm_asmap);
 231         assert(dmp->dm_aslen <= dmp->dm_asrsv);
 232 
 233         dt_module_strtab = dmp->dm_strtab.cts_data;
 234         qsort(dmp->dm_asmap, dmp->dm_aslen,
 235             sizeof (Elf64_Sym *), dt_module_symcomp64);
 236         dt_module_strtab = NULL;
 237 }
 238 
 239 static GElf_Sym *
 240 dt_module_symgelf32(const Elf32_Sym *src, GElf_Sym *dst)
 241 {
 242         if (dst != NULL) {
 243                 dst->st_name = src->st_name;
 244                 dst->st_info = src->st_info;
 245                 dst->st_other = src->st_other;
 246                 dst->st_shndx = src->st_shndx;
 247                 dst->st_value = src->st_value;
 248                 dst->st_size = src->st_size;
 249         }
 250 
 251         return (dst);
 252 }
 253 
 254 static GElf_Sym *
 255 dt_module_symgelf64(const Elf64_Sym *src, GElf_Sym *dst)
 256 {
 257         if (dst != NULL)
 258                 bcopy(src, dst, sizeof (GElf_Sym));
 259 
 260         return (dst);
 261 }
 262 
 263 static GElf_Sym *
 264 dt_module_symname32(dt_module_t *dmp, const char *name,
 265     GElf_Sym *symp, uint_t *idp)
 266 {
 267         const Elf32_Sym *symtab = dmp->dm_symtab.cts_data;
 268         const char *strtab = dmp->dm_strtab.cts_data;
 269 
 270         const Elf32_Sym *sym;
 271         const dt_sym_t *dsp;
 272         uint_t i, h;
 273 
 274         if (dmp->dm_nsymelems == 0)
 275                 return (NULL);
 276 
 277         h = dt_strtab_hash(name, NULL) % dmp->dm_nsymbuckets;
 278 
 279         for (i = dmp->dm_symbuckets[h]; i != 0; i = dsp->ds_next) {
 280                 dsp = &dmp->dm_symchains[i];
 281                 sym = symtab + dsp->ds_symid;
 282 
 283                 if (strcmp(name, strtab + sym->st_name) == 0) {
 284                         if (idp != NULL)
 285                                 *idp = dsp->ds_symid;
 286                         return (dt_module_symgelf32(sym, symp));
 287                 }
 288         }
 289 
 290         return (NULL);
 291 }
 292 
 293 static GElf_Sym *
 294 dt_module_symname64(dt_module_t *dmp, const char *name,
 295     GElf_Sym *symp, uint_t *idp)
 296 {
 297         const Elf64_Sym *symtab = dmp->dm_symtab.cts_data;
 298         const char *strtab = dmp->dm_strtab.cts_data;
 299 
 300         const Elf64_Sym *sym;
 301         const dt_sym_t *dsp;
 302         uint_t i, h;
 303 
 304         if (dmp->dm_nsymelems == 0)
 305                 return (NULL);
 306 
 307         h = dt_strtab_hash(name, NULL) % dmp->dm_nsymbuckets;
 308 
 309         for (i = dmp->dm_symbuckets[h]; i != 0; i = dsp->ds_next) {
 310                 dsp = &dmp->dm_symchains[i];
 311                 sym = symtab + dsp->ds_symid;
 312 
 313                 if (strcmp(name, strtab + sym->st_name) == 0) {
 314                         if (idp != NULL)
 315                                 *idp = dsp->ds_symid;
 316                         return (dt_module_symgelf64(sym, symp));
 317                 }
 318         }
 319 
 320         return (NULL);
 321 }
 322 
 323 static GElf_Sym *
 324 dt_module_symaddr32(dt_module_t *dmp, GElf_Addr addr,
 325     GElf_Sym *symp, uint_t *idp)
 326 {
 327         const Elf32_Sym **asmap = (const Elf32_Sym **)dmp->dm_asmap;
 328         const Elf32_Sym *symtab = dmp->dm_symtab.cts_data;
 329         const Elf32_Sym *sym;
 330 
 331         uint_t i, mid, lo = 0, hi = dmp->dm_aslen - 1;
 332         Elf32_Addr v;
 333 
 334         if (dmp->dm_aslen == 0)
 335                 return (NULL);
 336 
 337         while (hi - lo > 1) {
 338                 mid = (lo + hi) / 2;
 339                 if (addr >= asmap[mid]->st_value)
 340                         lo = mid;
 341                 else
 342                         hi = mid;
 343         }
 344 
 345         i = addr < asmap[hi]->st_value ? lo : hi;
 346         sym = asmap[i];
 347         v = sym->st_value;
 348 
 349         /*
 350          * If the previous entry has the same value, improve our choice.  The
 351          * order of equal-valued symbols is determined by the comparison func.
 352          */
 353         while (i-- != 0 && asmap[i]->st_value == v)
 354                 sym = asmap[i];
 355 
 356         if (addr - sym->st_value < MAX(sym->st_size, 1)) {
 357                 if (idp != NULL)
 358                         *idp = (uint_t)(sym - symtab);
 359                 return (dt_module_symgelf32(sym, symp));
 360         }
 361 
 362         return (NULL);
 363 }
 364 
 365 static GElf_Sym *
 366 dt_module_symaddr64(dt_module_t *dmp, GElf_Addr addr,
 367     GElf_Sym *symp, uint_t *idp)
 368 {
 369         const Elf64_Sym **asmap = (const Elf64_Sym **)dmp->dm_asmap;
 370         const Elf64_Sym *symtab = dmp->dm_symtab.cts_data;
 371         const Elf64_Sym *sym;
 372 
 373         uint_t i, mid, lo = 0, hi = dmp->dm_aslen - 1;
 374         Elf64_Addr v;
 375 
 376         if (dmp->dm_aslen == 0)
 377                 return (NULL);
 378 
 379         while (hi - lo > 1) {
 380                 mid = (lo + hi) / 2;
 381                 if (addr >= asmap[mid]->st_value)
 382                         lo = mid;
 383                 else
 384                         hi = mid;
 385         }
 386 
 387         i = addr < asmap[hi]->st_value ? lo : hi;
 388         sym = asmap[i];
 389         v = sym->st_value;
 390 
 391         /*
 392          * If the previous entry has the same value, improve our choice.  The
 393          * order of equal-valued symbols is determined by the comparison func.
 394          */
 395         while (i-- != 0 && asmap[i]->st_value == v)
 396                 sym = asmap[i];
 397 
 398         if (addr - sym->st_value < MAX(sym->st_size, 1)) {
 399                 if (idp != NULL)
 400                         *idp = (uint_t)(sym - symtab);
 401                 return (dt_module_symgelf64(sym, symp));
 402         }
 403 
 404         return (NULL);
 405 }
 406 
 407 static const dt_modops_t dt_modops_32 = {
 408         dt_module_syminit32,
 409         dt_module_symsort32,
 410         dt_module_symname32,
 411         dt_module_symaddr32
 412 };
 413 
 414 static const dt_modops_t dt_modops_64 = {
 415         dt_module_syminit64,
 416         dt_module_symsort64,
 417         dt_module_symname64,
 418         dt_module_symaddr64
 419 };
 420 
 421 dt_module_t *
 422 dt_module_create(dtrace_hdl_t *dtp, const char *name)
 423 {
 424         long pid;
 425         char *eptr;
 426         dt_ident_t *idp;
 427         uint_t h = dt_strtab_hash(name, NULL) % dtp->dt_modbuckets;
 428         dt_module_t *dmp;
 429 
 430         for (dmp = dtp->dt_mods[h]; dmp != NULL; dmp = dmp->dm_next) {
 431                 if (strcmp(dmp->dm_name, name) == 0)
 432                         return (dmp);
 433         }
 434 
 435         if ((dmp = malloc(sizeof (dt_module_t))) == NULL)
 436                 return (NULL); /* caller must handle allocation failure */
 437 
 438         bzero(dmp, sizeof (dt_module_t));
 439         (void) strlcpy(dmp->dm_name, name, sizeof (dmp->dm_name));
 440         dt_list_append(&dtp->dt_modlist, dmp);
 441         dmp->dm_next = dtp->dt_mods[h];
 442         dtp->dt_mods[h] = dmp;
 443         dtp->dt_nmods++;
 444 
 445         if (dtp->dt_conf.dtc_ctfmodel == CTF_MODEL_LP64)
 446                 dmp->dm_ops = &dt_modops_64;
 447         else
 448                 dmp->dm_ops = &dt_modops_32;
 449 
 450         /*
 451          * Modules for userland processes are special. They always refer to a
 452          * specific process and have a copy of their CTF data from a specific
 453          * instant in time. Any dt_module_t that begins with 'pid' is a module
 454          * for a specific process, much like how any probe description that
 455          * begins with 'pid' is special. pid123 refers to process 123. A module
 456          * that is just 'pid' refers specifically to pid$target. This is
 457          * generally done as D does not currently allow for macros to be
 458          * evaluated when working with types.
 459          */
 460         if (strncmp(dmp->dm_name, "pid", 3) == 0) {
 461                 errno = 0;
 462                 if (dmp->dm_name[3] == '\0') {
 463                         idp = dt_idhash_lookup(dtp->dt_macros, "target");
 464                         if (idp != NULL && idp->di_id != 0)
 465                                 dmp->dm_pid = idp->di_id;
 466                 } else {
 467                         pid = strtol(dmp->dm_name + 3, &eptr, 10);
 468                         if (errno == 0 && *eptr == '\0')
 469                                 dmp->dm_pid = (pid_t)pid;
 470                         else
 471                                 dt_dprintf("encountered malformed pid "
 472                                     "module: %s\n", dmp->dm_name);
 473                 }
 474         }
 475 
 476         return (dmp);
 477 }
 478 
 479 dt_module_t *
 480 dt_module_lookup_by_name(dtrace_hdl_t *dtp, const char *name)
 481 {
 482         uint_t h = dt_strtab_hash(name, NULL) % dtp->dt_modbuckets;
 483         dt_module_t *dmp;
 484 
 485         for (dmp = dtp->dt_mods[h]; dmp != NULL; dmp = dmp->dm_next) {
 486                 if (strcmp(dmp->dm_name, name) == 0)
 487                         return (dmp);
 488         }
 489 
 490         return (NULL);
 491 }
 492 
 493 /*ARGSUSED*/
 494 dt_module_t *
 495 dt_module_lookup_by_ctf(dtrace_hdl_t *dtp, ctf_file_t *ctfp)
 496 {
 497         return (ctfp ? ctf_getspecific(ctfp) : NULL);
 498 }
 499 
 500 static int
 501 dt_module_load_sect(dtrace_hdl_t *dtp, dt_module_t *dmp, ctf_sect_t *ctsp)
 502 {
 503         const char *s;
 504         size_t shstrs;
 505         GElf_Shdr sh;
 506         Elf_Data *dp;
 507         Elf_Scn *sp;
 508 
 509         if (elf_getshdrstrndx(dmp->dm_elf, &shstrs) == -1)
 510                 return (dt_set_errno(dtp, EDT_NOTLOADED));
 511 
 512         for (sp = NULL; (sp = elf_nextscn(dmp->dm_elf, sp)) != NULL; ) {
 513                 if (gelf_getshdr(sp, &sh) == NULL || sh.sh_type == SHT_NULL ||
 514                     (s = elf_strptr(dmp->dm_elf, shstrs, sh.sh_name)) == NULL)
 515                         continue; /* skip any malformed sections */
 516 
 517                 if (sh.sh_type == ctsp->cts_type &&
 518                     sh.sh_entsize == ctsp->cts_entsize &&
 519                     strcmp(s, ctsp->cts_name) == 0)
 520                         break; /* section matches specification */
 521         }
 522 
 523         /*
 524          * If the section isn't found, return success but leave cts_data set
 525          * to NULL and cts_size set to zero for our caller.
 526          */
 527         if (sp == NULL || (dp = elf_getdata(sp, NULL)) == NULL)
 528                 return (0);
 529 
 530         ctsp->cts_data = dp->d_buf;
 531         ctsp->cts_size = dp->d_size;
 532 
 533         dt_dprintf("loaded %s [%s] (%lu bytes)\n",
 534             dmp->dm_name, ctsp->cts_name, (ulong_t)ctsp->cts_size);
 535 
 536         return (0);
 537 }
 538 
 539 typedef struct dt_module_cb_arg {
 540         struct ps_prochandle *dpa_proc;
 541         dtrace_hdl_t *dpa_dtp;
 542         dt_module_t *dpa_dmp;
 543         uint_t dpa_count;
 544 } dt_module_cb_arg_t;
 545 
 546 /* ARGSUSED */
 547 static int
 548 dt_module_load_proc_count(void *arg, const prmap_t *prmap, const char *obj)
 549 {
 550         ctf_file_t *fp;
 551         dt_module_cb_arg_t *dcp = arg;
 552 
 553         /* Try to grab a ctf container if it exists */
 554         fp = Pname_to_ctf(dcp->dpa_proc, obj);
 555         if (fp != NULL)
 556                 dcp->dpa_count++;
 557         return (0);
 558 }
 559 
 560 /* ARGSUSED */
 561 static int
 562 dt_module_load_proc_build(void *arg, const prmap_t *prmap, const char *obj)
 563 {
 564         ctf_file_t *fp;
 565         char buf[MAXPATHLEN], *p;
 566         dt_module_cb_arg_t *dcp = arg;
 567         int count = dcp->dpa_count;
 568         Lmid_t lmid;
 569 
 570         fp = Pname_to_ctf(dcp->dpa_proc, obj);
 571         if (fp == NULL)
 572                 return (0);
 573         fp = ctf_dup(fp);
 574         if (fp == NULL)
 575                 return (0);
 576         dcp->dpa_dmp->dm_libctfp[count] = fp;
 577         /*
 578          * While it'd be nice to simply use objname here, because of our prior
 579          * actions we'll always get a resolved object name to its on disk file.
 580          * Like the pid provider, we need to tell a bit of a lie here. The type
 581          * that the user thinks of is in terms of the libraries they requested,
 582          * eg. libc.so.1, they don't care about the fact that it's
 583          * libc_hwcap.so.1.
 584          */
 585         (void) Pobjname(dcp->dpa_proc, prmap->pr_vaddr, buf, sizeof (buf));
 586         if ((p = strrchr(buf, '/')) == NULL)
 587                 p = buf;
 588         else
 589                 p++;
 590 
 591         /*
 592          * If for some reason we can't find a link map id for this module, which
 593          * would be really quite weird. We instead just say the link map id is
 594          * zero.
 595          */
 596         if (Plmid(dcp->dpa_proc, prmap->pr_vaddr, &lmid) != 0)
 597                 lmid = 0;
 598 
 599         if (lmid == 0)
 600                 dcp->dpa_dmp->dm_libctfn[count] = strdup(p);
 601         else
 602                 (void) asprintf(&dcp->dpa_dmp->dm_libctfn[count],
 603                     "LM%lx`%s", lmid, p);
 604         if (dcp->dpa_dmp->dm_libctfn[count] == NULL)
 605                 return (1);
 606         ctf_setspecific(fp, dcp->dpa_dmp);
 607         dcp->dpa_count++;
 608         return (0);
 609 }
 610 
 611 /*
 612  * We've been asked to load data that belongs to another process. As such we're
 613  * going to pgrab it at this instant, load everything that we might ever care
 614  * about, and then drive on. The reason for this is that the process that we're
 615  * interested in might be changing. As long as we have grabbed it, then this
 616  * can't be a problem for us.
 617  *
 618  * For now, we're actually going to punt on most things and just try to get CTF
 619  * data, nothing else. Basically this is only useful as a source of type
 620  * information, we can't go and do the stacktrace lookups, etc.
 621  */
 622 static int
 623 dt_module_load_proc(dtrace_hdl_t *dtp, dt_module_t *dmp)
 624 {
 625         struct ps_prochandle *p;
 626         dt_module_cb_arg_t arg;
 627 
 628         /*
 629          * Note that on success we do not release this hold. We must hold this
 630          * for our life time.
 631          */
 632         p = dt_proc_grab(dtp, dmp->dm_pid, 0, PGRAB_RDONLY | PGRAB_FORCE);
 633         if (p == NULL) {
 634                 dt_dprintf("failed to grab pid: %d\n", (int)dmp->dm_pid);
 635                 return (dt_set_errno(dtp, EDT_CANTLOAD));
 636         }
 637         dt_proc_lock(dtp, p);
 638 
 639         arg.dpa_proc = p;
 640         arg.dpa_dtp = dtp;
 641         arg.dpa_dmp = dmp;
 642         arg.dpa_count = 0;
 643         if (Pobject_iter_resolved(p, dt_module_load_proc_count, &arg) != 0) {
 644                 dt_dprintf("failed to iterate objects\n");
 645                 dt_proc_release(dtp, p);
 646                 return (dt_set_errno(dtp, EDT_CANTLOAD));
 647         }
 648 
 649         if (arg.dpa_count == 0) {
 650                 dt_dprintf("no ctf data present\n");
 651                 dt_proc_unlock(dtp, p);
 652                 dt_proc_release(dtp, p);
 653                 return (dt_set_errno(dtp, EDT_CANTLOAD));
 654         }
 655 
 656         dmp->dm_libctfp = malloc(sizeof (ctf_file_t *) * arg.dpa_count);
 657         if (dmp->dm_libctfp == NULL) {
 658                 dt_proc_unlock(dtp, p);
 659                 dt_proc_release(dtp, p);
 660                 return (dt_set_errno(dtp, EDT_NOMEM));
 661         }
 662         bzero(dmp->dm_libctfp, sizeof (ctf_file_t *) * arg.dpa_count);
 663 
 664         dmp->dm_libctfn = malloc(sizeof (char *) * arg.dpa_count);
 665         if (dmp->dm_libctfn == NULL) {
 666                 free(dmp->dm_libctfp);
 667                 dt_proc_unlock(dtp, p);
 668                 dt_proc_release(dtp, p);
 669                 return (dt_set_errno(dtp, EDT_NOMEM));
 670         }
 671         bzero(dmp->dm_libctfn, sizeof (char *) * arg.dpa_count);
 672 
 673         dmp->dm_nctflibs = arg.dpa_count;
 674 
 675         arg.dpa_count = 0;
 676         if (Pobject_iter_resolved(p, dt_module_load_proc_build, &arg) != 0) {
 677                 dt_proc_unlock(dtp, p);
 678                 dt_module_unload(dtp, dmp);
 679                 dt_proc_release(dtp, p);
 680                 return (dt_set_errno(dtp, EDT_CANTLOAD));
 681         }
 682         assert(arg.dpa_count == dmp->dm_nctflibs);
 683         dt_dprintf("loaded %d ctf modules for pid %d\n", arg.dpa_count,
 684             (int)dmp->dm_pid);
 685 
 686         dt_proc_unlock(dtp, p);
 687         dt_proc_release(dtp, p);
 688         dmp->dm_flags |= DT_DM_LOADED;
 689 
 690         return (0);
 691 }
 692 
 693 int
 694 dt_module_load(dtrace_hdl_t *dtp, dt_module_t *dmp)
 695 {
 696         if (dmp->dm_flags & DT_DM_LOADED)
 697                 return (0); /* module is already loaded */
 698 
 699         if (dmp->dm_pid != 0)
 700                 return (dt_module_load_proc(dtp, dmp));
 701 
 702         dmp->dm_ctdata.cts_name = ".SUNW_ctf";
 703         dmp->dm_ctdata.cts_type = SHT_PROGBITS;
 704         dmp->dm_ctdata.cts_flags = 0;
 705         dmp->dm_ctdata.cts_data = NULL;
 706         dmp->dm_ctdata.cts_size = 0;
 707         dmp->dm_ctdata.cts_entsize = 0;
 708         dmp->dm_ctdata.cts_offset = 0;
 709 
 710         dmp->dm_symtab.cts_name = ".symtab";
 711         dmp->dm_symtab.cts_type = SHT_SYMTAB;
 712         dmp->dm_symtab.cts_flags = 0;
 713         dmp->dm_symtab.cts_data = NULL;
 714         dmp->dm_symtab.cts_size = 0;
 715         dmp->dm_symtab.cts_entsize = dmp->dm_ops == &dt_modops_64 ?
 716             sizeof (Elf64_Sym) : sizeof (Elf32_Sym);
 717         dmp->dm_symtab.cts_offset = 0;
 718 
 719         dmp->dm_strtab.cts_name = ".strtab";
 720         dmp->dm_strtab.cts_type = SHT_STRTAB;
 721         dmp->dm_strtab.cts_flags = 0;
 722         dmp->dm_strtab.cts_data = NULL;
 723         dmp->dm_strtab.cts_size = 0;
 724         dmp->dm_strtab.cts_entsize = 0;
 725         dmp->dm_strtab.cts_offset = 0;
 726 
 727         /*
 728          * Attempt to load the module's CTF section, symbol table section, and
 729          * string table section.  Note that modules may not contain CTF data:
 730          * this will result in a successful load_sect but data of size zero.
 731          * We will then fail if dt_module_getctf() is called, as shown below.
 732          */
 733         if (dt_module_load_sect(dtp, dmp, &dmp->dm_ctdata) == -1 ||
 734             dt_module_load_sect(dtp, dmp, &dmp->dm_symtab) == -1 ||
 735             dt_module_load_sect(dtp, dmp, &dmp->dm_strtab) == -1) {
 736                 dt_module_unload(dtp, dmp);
 737                 return (-1); /* dt_errno is set for us */
 738         }
 739 
 740         /*
 741          * Allocate the hash chains and hash buckets for symbol name lookup.
 742          * This is relatively simple since the symbol table is of fixed size
 743          * and is known in advance.  We allocate one extra element since we
 744          * use element indices instead of pointers and zero is our sentinel.
 745          */
 746         dmp->dm_nsymelems =
 747             dmp->dm_symtab.cts_size / dmp->dm_symtab.cts_entsize;
 748 
 749         dmp->dm_nsymbuckets = _dtrace_strbuckets;
 750         dmp->dm_symfree = 1;         /* first free element is index 1 */
 751 
 752         dmp->dm_symbuckets = malloc(sizeof (uint_t) * dmp->dm_nsymbuckets);
 753         dmp->dm_symchains = malloc(sizeof (dt_sym_t) * dmp->dm_nsymelems + 1);
 754 
 755         if (dmp->dm_symbuckets == NULL || dmp->dm_symchains == NULL) {
 756                 dt_module_unload(dtp, dmp);
 757                 return (dt_set_errno(dtp, EDT_NOMEM));
 758         }
 759 
 760         bzero(dmp->dm_symbuckets, sizeof (uint_t) * dmp->dm_nsymbuckets);
 761         bzero(dmp->dm_symchains, sizeof (dt_sym_t) * dmp->dm_nsymelems + 1);
 762 
 763         /*
 764          * Iterate over the symbol table data buffer and insert each symbol
 765          * name into the name hash if the name and type are valid.  Then
 766          * allocate the address map, fill it in, and sort it.
 767          */
 768         dmp->dm_asrsv = dmp->dm_ops->do_syminit(dmp);
 769 
 770         dt_dprintf("hashed %s [%s] (%u symbols)\n",
 771             dmp->dm_name, dmp->dm_symtab.cts_name, dmp->dm_symfree - 1);
 772 
 773         if ((dmp->dm_asmap = malloc(sizeof (void *) * dmp->dm_asrsv)) == NULL) {
 774                 dt_module_unload(dtp, dmp);
 775                 return (dt_set_errno(dtp, EDT_NOMEM));
 776         }
 777 
 778         dmp->dm_ops->do_symsort(dmp);
 779 
 780         dt_dprintf("sorted %s [%s] (%u symbols)\n",
 781             dmp->dm_name, dmp->dm_symtab.cts_name, dmp->dm_aslen);
 782 
 783         dmp->dm_flags |= DT_DM_LOADED;
 784         return (0);
 785 }
 786 
 787 int
 788 dt_module_hasctf(dtrace_hdl_t *dtp, dt_module_t *dmp)
 789 {
 790         if (dmp->dm_pid != 0 && dmp->dm_nctflibs > 0)
 791                 return (1);
 792         return (dt_module_getctf(dtp, dmp) != NULL);
 793 }
 794 
 795 ctf_file_t *
 796 dt_module_getctf(dtrace_hdl_t *dtp, dt_module_t *dmp)
 797 {
 798         const char *parent;
 799         dt_module_t *pmp;
 800         ctf_file_t *pfp;
 801         int model;
 802 
 803         if (dmp->dm_ctfp != NULL || dt_module_load(dtp, dmp) != 0)
 804                 return (dmp->dm_ctfp);
 805 
 806         if (dmp->dm_ops == &dt_modops_64)
 807                 model = CTF_MODEL_LP64;
 808         else
 809                 model = CTF_MODEL_ILP32;
 810 
 811         /*
 812          * If the data model of the module does not match our program data
 813          * model, then do not permit CTF from this module to be opened and
 814          * returned to the compiler.  If we support mixed data models in the
 815          * future for combined kernel/user tracing, this can be removed.
 816          */
 817         if (dtp->dt_conf.dtc_ctfmodel != model) {
 818                 (void) dt_set_errno(dtp, EDT_DATAMODEL);
 819                 return (NULL);
 820         }
 821 
 822         if (dmp->dm_ctdata.cts_size == 0) {
 823                 (void) dt_set_errno(dtp, EDT_NOCTF);
 824                 return (NULL);
 825         }
 826 
 827         dmp->dm_ctfp = ctf_bufopen(&dmp->dm_ctdata,
 828             &dmp->dm_symtab, &dmp->dm_strtab, &dtp->dt_ctferr);
 829 
 830         if (dmp->dm_ctfp == NULL) {
 831                 (void) dt_set_errno(dtp, EDT_CTF);
 832                 return (NULL);
 833         }
 834 
 835         (void) ctf_setmodel(dmp->dm_ctfp, model);
 836         ctf_setspecific(dmp->dm_ctfp, dmp);
 837 
 838         if ((parent = ctf_parent_name(dmp->dm_ctfp)) != NULL) {
 839                 if ((pmp = dt_module_create(dtp, parent)) == NULL ||
 840                     (pfp = dt_module_getctf(dtp, pmp)) == NULL) {
 841                         if (pmp == NULL)
 842                                 (void) dt_set_errno(dtp, EDT_NOMEM);
 843                         goto err;
 844                 }
 845 
 846                 /*
 847                  * If the label we claim the parent must have is not actually
 848                  * present in the parent module, ignore the CTF entirely
 849                  * rather than acquiring possibly bad type references.
 850                  */
 851                 if (ctf_label_info(pfp, ctf_parent_label(dmp->dm_ctfp),
 852                     NULL) == CTF_ERR) {
 853                         (void) dt_set_errno(dtp, EDT_BADCTF);
 854                         goto err;
 855                 }
 856 
 857                 if (ctf_import(dmp->dm_ctfp, pfp) == CTF_ERR) {
 858                         dtp->dt_ctferr = ctf_errno(dmp->dm_ctfp);
 859                         (void) dt_set_errno(dtp, EDT_CTF);
 860                         goto err;
 861                 }
 862         }
 863 
 864         dt_dprintf("loaded CTF container for %s (%p)\n",
 865             dmp->dm_name, (void *)dmp->dm_ctfp);
 866 
 867         return (dmp->dm_ctfp);
 868 
 869 err:
 870         dt_dprintf("could not load CTF container for %s: %s\n",
 871             dmp->dm_name, dtrace_errmsg(dtp, dtrace_errno(dtp)));
 872         ctf_close(dmp->dm_ctfp);
 873         dmp->dm_ctfp = NULL;
 874         return (NULL);
 875 }
 876 
 877 /*ARGSUSED*/
 878 void
 879 dt_module_unload(dtrace_hdl_t *dtp, dt_module_t *dmp)
 880 {
 881         int i;
 882 
 883         ctf_close(dmp->dm_ctfp);
 884         dmp->dm_ctfp = NULL;
 885 
 886         if (dmp->dm_libctfp != NULL) {
 887                 for (i = 0; i < dmp->dm_nctflibs; i++) {
 888                         ctf_close(dmp->dm_libctfp[i]);
 889                         free(dmp->dm_libctfn[i]);
 890                 }
 891                 free(dmp->dm_libctfp);
 892                 free(dmp->dm_libctfn);
 893                 dmp->dm_libctfp = NULL;
 894                 dmp->dm_nctflibs = 0;
 895         }
 896 
 897         bzero(&dmp->dm_ctdata, sizeof (ctf_sect_t));
 898         bzero(&dmp->dm_symtab, sizeof (ctf_sect_t));
 899         bzero(&dmp->dm_strtab, sizeof (ctf_sect_t));
 900 
 901         if (dmp->dm_symbuckets != NULL) {
 902                 free(dmp->dm_symbuckets);
 903                 dmp->dm_symbuckets = NULL;
 904         }
 905 
 906         if (dmp->dm_symchains != NULL) {
 907                 free(dmp->dm_symchains);
 908                 dmp->dm_symchains = NULL;
 909         }
 910 
 911         if (dmp->dm_asmap != NULL) {
 912                 free(dmp->dm_asmap);
 913                 dmp->dm_asmap = NULL;
 914         }
 915 
 916         dmp->dm_symfree = 0;
 917         dmp->dm_nsymbuckets = 0;
 918         dmp->dm_nsymelems = 0;
 919         dmp->dm_asrsv = 0;
 920         dmp->dm_aslen = 0;
 921 
 922         dmp->dm_text_va = NULL;
 923         dmp->dm_text_size = 0;
 924         dmp->dm_data_va = NULL;
 925         dmp->dm_data_size = 0;
 926         dmp->dm_bss_va = NULL;
 927         dmp->dm_bss_size = 0;
 928 
 929         if (dmp->dm_extern != NULL) {
 930                 dt_idhash_destroy(dmp->dm_extern);
 931                 dmp->dm_extern = NULL;
 932         }
 933 
 934         (void) elf_end(dmp->dm_elf);
 935         dmp->dm_elf = NULL;
 936 
 937         dmp->dm_pid = 0;
 938 
 939         dmp->dm_flags &= ~DT_DM_LOADED;
 940 }
 941 
 942 void
 943 dt_module_destroy(dtrace_hdl_t *dtp, dt_module_t *dmp)
 944 {
 945         uint_t h = dt_strtab_hash(dmp->dm_name, NULL) % dtp->dt_modbuckets;
 946         dt_module_t **dmpp = &dtp->dt_mods[h];
 947 
 948         dt_list_delete(&dtp->dt_modlist, dmp);
 949         assert(dtp->dt_nmods != 0);
 950         dtp->dt_nmods--;
 951 
 952         /*
 953          * Now remove this module from its hash chain.  We expect to always
 954          * find the module on its hash chain, so in this loop we assert that
 955          * we don't run off the end of the list.
 956          */
 957         while (*dmpp != dmp) {
 958                 dmpp = &((*dmpp)->dm_next);
 959                 assert(*dmpp != NULL);
 960         }
 961 
 962         *dmpp = dmp->dm_next;
 963 
 964         dt_module_unload(dtp, dmp);
 965         free(dmp);
 966 }
 967 
 968 /*
 969  * Insert a new external symbol reference into the specified module.  The new
 970  * symbol will be marked as undefined and is assigned a symbol index beyond
 971  * any existing cached symbols from this module.  We use the ident's di_data
 972  * field to store a pointer to a copy of the dtrace_syminfo_t for this symbol.
 973  */
 974 dt_ident_t *
 975 dt_module_extern(dtrace_hdl_t *dtp, dt_module_t *dmp,
 976     const char *name, const dtrace_typeinfo_t *tip)
 977 {
 978         dtrace_syminfo_t *sip;
 979         dt_ident_t *idp;
 980         uint_t id;
 981 
 982         if (dmp->dm_extern == NULL && (dmp->dm_extern = dt_idhash_create(
 983             "extern", NULL, dmp->dm_nsymelems, UINT_MAX)) == NULL) {
 984                 (void) dt_set_errno(dtp, EDT_NOMEM);
 985                 return (NULL);
 986         }
 987 
 988         if (dt_idhash_nextid(dmp->dm_extern, &id) == -1) {
 989                 (void) dt_set_errno(dtp, EDT_SYMOFLOW);
 990                 return (NULL);
 991         }
 992 
 993         if ((sip = malloc(sizeof (dtrace_syminfo_t))) == NULL) {
 994                 (void) dt_set_errno(dtp, EDT_NOMEM);
 995                 return (NULL);
 996         }
 997 
 998         idp = dt_idhash_insert(dmp->dm_extern, name, DT_IDENT_SYMBOL, 0, id,
 999             _dtrace_symattr, 0, &dt_idops_thaw, NULL, dtp->dt_gen);
1000 
1001         if (idp == NULL) {
1002                 (void) dt_set_errno(dtp, EDT_NOMEM);
1003                 free(sip);
1004                 return (NULL);
1005         }
1006 
1007         sip->dts_object = dmp->dm_name;
1008         sip->dts_name = idp->di_name;
1009         sip->dts_id = idp->di_id;
1010 
1011         idp->di_data = sip;
1012         idp->di_ctfp = tip->dtt_ctfp;
1013         idp->di_type = tip->dtt_type;
1014 
1015         return (idp);
1016 }
1017 
1018 const char *
1019 dt_module_modelname(dt_module_t *dmp)
1020 {
1021         if (dmp->dm_ops == &dt_modops_64)
1022                 return ("64-bit");
1023         else
1024                 return ("32-bit");
1025 }
1026 
1027 /* ARGSUSED */
1028 int
1029 dt_module_getlibid(dtrace_hdl_t *dtp, dt_module_t *dmp, const ctf_file_t *fp)
1030 {
1031         int i;
1032 
1033         for (i = 0; i < dmp->dm_nctflibs; i++) {
1034                 if (dmp->dm_libctfp[i] == fp)
1035                         return (i);
1036         }
1037 
1038         return (-1);
1039 }
1040 
1041 /* ARGSUSED */
1042 ctf_file_t *
1043 dt_module_getctflib(dtrace_hdl_t *dtp, dt_module_t *dmp, const char *name)
1044 {
1045         int i;
1046 
1047         for (i = 0; i < dmp->dm_nctflibs; i++) {
1048                 if (strcmp(dmp->dm_libctfn[i], name) == 0)
1049                         return (dmp->dm_libctfp[i]);
1050         }
1051 
1052         return (NULL);
1053 }
1054 
1055 /*
1056  * Update our module cache by adding an entry for the specified module 'name'.
1057  * We create the dt_module_t and populate it using /system/object/<name>/.
1058  */
1059 static void
1060 dt_module_update(dtrace_hdl_t *dtp, const char *name)
1061 {
1062         char fname[MAXPATHLEN];
1063         struct stat64 st;
1064         int fd, err, bits;
1065 
1066         dt_module_t *dmp;
1067         const char *s;
1068         size_t shstrs;
1069         GElf_Shdr sh;
1070         Elf_Data *dp;
1071         Elf_Scn *sp;
1072 
1073         (void) snprintf(fname, sizeof (fname),
1074             "%s/%s/object", OBJFS_ROOT, name);
1075 
1076         if ((fd = open(fname, O_RDONLY)) == -1 || fstat64(fd, &st) == -1 ||
1077             (dmp = dt_module_create(dtp, name)) == NULL) {
1078                 dt_dprintf("failed to open %s: %s\n", fname, strerror(errno));
1079                 (void) close(fd);
1080                 return;
1081         }
1082 
1083         /*
1084          * Since the module can unload out from under us (and /system/object
1085          * will return ENOENT), tell libelf to cook the entire file now and
1086          * then close the underlying file descriptor immediately.  If this
1087          * succeeds, we know that we can continue safely using dmp->dm_elf.
1088          */
1089         dmp->dm_elf = elf_begin(fd, ELF_C_READ, NULL);
1090         err = elf_cntl(dmp->dm_elf, ELF_C_FDREAD);
1091         (void) close(fd);
1092 
1093         if (dmp->dm_elf == NULL || err == -1 ||
1094             elf_getshdrstrndx(dmp->dm_elf, &shstrs) == -1) {
1095                 dt_dprintf("failed to load %s: %s\n",
1096                     fname, elf_errmsg(elf_errno()));
1097                 dt_module_destroy(dtp, dmp);
1098                 return;
1099         }
1100 
1101         switch (gelf_getclass(dmp->dm_elf)) {
1102         case ELFCLASS32:
1103                 dmp->dm_ops = &dt_modops_32;
1104                 bits = 32;
1105                 break;
1106         case ELFCLASS64:
1107                 dmp->dm_ops = &dt_modops_64;
1108                 bits = 64;
1109                 break;
1110         default:
1111                 dt_dprintf("failed to load %s: unknown ELF class\n", fname);
1112                 dt_module_destroy(dtp, dmp);
1113                 return;
1114         }
1115 
1116         /*
1117          * Iterate over the section headers locating various sections of
1118          * interest and use their attributes to flesh out the dt_module_t.
1119          */
1120         for (sp = NULL; (sp = elf_nextscn(dmp->dm_elf, sp)) != NULL; ) {
1121                 if (gelf_getshdr(sp, &sh) == NULL || sh.sh_type == SHT_NULL ||
1122                     (s = elf_strptr(dmp->dm_elf, shstrs, sh.sh_name)) == NULL)
1123                         continue; /* skip any malformed sections */
1124 
1125                 if (strcmp(s, ".text") == 0) {
1126                         dmp->dm_text_size = sh.sh_size;
1127                         dmp->dm_text_va = sh.sh_addr;
1128                 } else if (strcmp(s, ".data") == 0) {
1129                         dmp->dm_data_size = sh.sh_size;
1130                         dmp->dm_data_va = sh.sh_addr;
1131                 } else if (strcmp(s, ".bss") == 0) {
1132                         dmp->dm_bss_size = sh.sh_size;
1133                         dmp->dm_bss_va = sh.sh_addr;
1134                 } else if (strcmp(s, ".info") == 0 &&
1135                     (dp = elf_getdata(sp, NULL)) != NULL) {
1136                         bcopy(dp->d_buf, &dmp->dm_info,
1137                             MIN(sh.sh_size, sizeof (dmp->dm_info)));
1138                 } else if (strcmp(s, ".filename") == 0 &&
1139                     (dp = elf_getdata(sp, NULL)) != NULL) {
1140                         (void) strlcpy(dmp->dm_file,
1141                             dp->d_buf, sizeof (dmp->dm_file));
1142                 }
1143         }
1144 
1145         dmp->dm_flags |= DT_DM_KERNEL;
1146         dmp->dm_modid = (int)OBJFS_MODID(st.st_ino);
1147 
1148         if (dmp->dm_info.objfs_info_primary)
1149                 dmp->dm_flags |= DT_DM_PRIMARY;
1150 
1151         dt_dprintf("opened %d-bit module %s (%s) [%d]\n",
1152             bits, dmp->dm_name, dmp->dm_file, dmp->dm_modid);
1153 }
1154 
1155 /*
1156  * Unload all the loaded modules and then refresh the module cache with the
1157  * latest list of loaded modules and their address ranges.
1158  */
1159 void
1160 dtrace_update(dtrace_hdl_t *dtp)
1161 {
1162         dt_module_t *dmp;
1163         DIR *dirp;
1164 
1165         for (dmp = dt_list_next(&dtp->dt_modlist);
1166             dmp != NULL; dmp = dt_list_next(dmp))
1167                 dt_module_unload(dtp, dmp);
1168 
1169         /*
1170          * Open /system/object and attempt to create a libdtrace module for
1171          * each kernel module that is loaded on the current system.
1172          */
1173         if (!(dtp->dt_oflags & DTRACE_O_NOSYS) &&
1174             (dirp = opendir(OBJFS_ROOT)) != NULL) {
1175                 struct dirent *dp;
1176 
1177                 while ((dp = readdir(dirp)) != NULL) {
1178                         if (dp->d_name[0] != '.')
1179                                 dt_module_update(dtp, dp->d_name);
1180                 }
1181 
1182                 (void) closedir(dirp);
1183         }
1184 
1185         /*
1186          * Look up all the macro identifiers and set di_id to the latest value.
1187          * This code collaborates with dt_lex.l on the use of di_id.  We will
1188          * need to implement something fancier if we need to support non-ints.
1189          */
1190         dt_idhash_lookup(dtp->dt_macros, "egid")->di_id = getegid();
1191         dt_idhash_lookup(dtp->dt_macros, "euid")->di_id = geteuid();
1192         dt_idhash_lookup(dtp->dt_macros, "gid")->di_id = getgid();
1193         dt_idhash_lookup(dtp->dt_macros, "pid")->di_id = getpid();
1194         dt_idhash_lookup(dtp->dt_macros, "pgid")->di_id = getpgid(0);
1195         dt_idhash_lookup(dtp->dt_macros, "ppid")->di_id = getppid();
1196         dt_idhash_lookup(dtp->dt_macros, "projid")->di_id = getprojid();
1197         dt_idhash_lookup(dtp->dt_macros, "sid")->di_id = getsid(0);
1198         dt_idhash_lookup(dtp->dt_macros, "taskid")->di_id = gettaskid();
1199         dt_idhash_lookup(dtp->dt_macros, "uid")->di_id = getuid();
1200 
1201         /*
1202          * Cache the pointers to the modules representing the base executable
1203          * and the run-time linker in the dtrace client handle. Note that on
1204          * x86 krtld is folded into unix, so if we don't find it, use unix
1205          * instead.
1206          */
1207         dtp->dt_exec = dt_module_lookup_by_name(dtp, "genunix");
1208         dtp->dt_rtld = dt_module_lookup_by_name(dtp, "krtld");
1209         if (dtp->dt_rtld == NULL)
1210                 dtp->dt_rtld = dt_module_lookup_by_name(dtp, "unix");
1211 
1212         /*
1213          * If this is the first time we are initializing the module list,
1214          * remove the module for genunix from the module list and then move it
1215          * to the front of the module list.  We do this so that type and symbol
1216          * queries encounter genunix and thereby optimize for the common case
1217          * in dtrace_lookup_by_name() and dtrace_lookup_by_type(), below.
1218          */
1219         if (dtp->dt_exec != NULL &&
1220             dtp->dt_cdefs == NULL && dtp->dt_ddefs == NULL) {
1221                 dt_list_delete(&dtp->dt_modlist, dtp->dt_exec);
1222                 dt_list_prepend(&dtp->dt_modlist, dtp->dt_exec);
1223         }
1224 }
1225 
1226 static dt_module_t *
1227 dt_module_from_object(dtrace_hdl_t *dtp, const char *object)
1228 {
1229         int err = EDT_NOMOD;
1230         dt_module_t *dmp;
1231 
1232         switch ((uintptr_t)object) {
1233         case (uintptr_t)DTRACE_OBJ_EXEC:
1234                 dmp = dtp->dt_exec;
1235                 break;
1236         case (uintptr_t)DTRACE_OBJ_RTLD:
1237                 dmp = dtp->dt_rtld;
1238                 break;
1239         case (uintptr_t)DTRACE_OBJ_CDEFS:
1240                 dmp = dtp->dt_cdefs;
1241                 break;
1242         case (uintptr_t)DTRACE_OBJ_DDEFS:
1243                 dmp = dtp->dt_ddefs;
1244                 break;
1245         default:
1246                 dmp = dt_module_create(dtp, object);
1247                 err = EDT_NOMEM;
1248         }
1249 
1250         if (dmp == NULL)
1251                 (void) dt_set_errno(dtp, err);
1252 
1253         return (dmp);
1254 }
1255 
1256 /*
1257  * Exported interface to look up a symbol by name.  We return the GElf_Sym and
1258  * complete symbol information for the matching symbol.
1259  */
1260 int
1261 dtrace_lookup_by_name(dtrace_hdl_t *dtp, const char *object, const char *name,
1262     GElf_Sym *symp, dtrace_syminfo_t *sip)
1263 {
1264         dt_module_t *dmp;
1265         dt_ident_t *idp;
1266         uint_t n, id;
1267         GElf_Sym sym;
1268 
1269         uint_t mask = 0; /* mask of dt_module flags to match */
1270         uint_t bits = 0; /* flag bits that must be present */
1271 
1272         if (object != DTRACE_OBJ_EVERY &&
1273             object != DTRACE_OBJ_KMODS &&
1274             object != DTRACE_OBJ_UMODS) {
1275                 if ((dmp = dt_module_from_object(dtp, object)) == NULL)
1276                         return (-1); /* dt_errno is set for us */
1277 
1278                 if (dt_module_load(dtp, dmp) == -1)
1279                         return (-1); /* dt_errno is set for us */
1280                 n = 1;
1281 
1282         } else {
1283                 if (object == DTRACE_OBJ_KMODS)
1284                         mask = bits = DT_DM_KERNEL;
1285                 else if (object == DTRACE_OBJ_UMODS)
1286                         mask = DT_DM_KERNEL;
1287 
1288                 dmp = dt_list_next(&dtp->dt_modlist);
1289                 n = dtp->dt_nmods;
1290         }
1291 
1292         if (symp == NULL)
1293                 symp = &sym;
1294 
1295         for (; n > 0; n--, dmp = dt_list_next(dmp)) {
1296                 if ((dmp->dm_flags & mask) != bits)
1297                         continue; /* failed to match required attributes */
1298 
1299                 if (dt_module_load(dtp, dmp) == -1)
1300                         continue; /* failed to load symbol table */
1301 
1302                 if (dmp->dm_ops->do_symname(dmp, name, symp, &id) != NULL) {
1303                         if (sip != NULL) {
1304                                 sip->dts_object = dmp->dm_name;
1305                                 sip->dts_name = (const char *)
1306                                     dmp->dm_strtab.cts_data + symp->st_name;
1307                                 sip->dts_id = id;
1308                         }
1309                         return (0);
1310                 }
1311 
1312                 if (dmp->dm_extern != NULL &&
1313                     (idp = dt_idhash_lookup(dmp->dm_extern, name)) != NULL) {
1314                         if (symp != &sym) {
1315                                 symp->st_name = (uintptr_t)idp->di_name;
1316                                 symp->st_info =
1317                                     GELF_ST_INFO(STB_GLOBAL, STT_NOTYPE);
1318                                 symp->st_other = 0;
1319                                 symp->st_shndx = SHN_UNDEF;
1320                                 symp->st_value = 0;
1321                                 symp->st_size =
1322                                     ctf_type_size(idp->di_ctfp, idp->di_type);
1323                         }
1324 
1325                         if (sip != NULL) {
1326                                 sip->dts_object = dmp->dm_name;
1327                                 sip->dts_name = idp->di_name;
1328                                 sip->dts_id = idp->di_id;
1329                         }
1330 
1331                         return (0);
1332                 }
1333         }
1334 
1335         return (dt_set_errno(dtp, EDT_NOSYM));
1336 }
1337 
1338 /*
1339  * Exported interface to look up a symbol by address.  We return the GElf_Sym
1340  * and complete symbol information for the matching symbol.
1341  */
1342 int
1343 dtrace_lookup_by_addr(dtrace_hdl_t *dtp, GElf_Addr addr,
1344     GElf_Sym *symp, dtrace_syminfo_t *sip)
1345 {
1346         dt_module_t *dmp;
1347         uint_t id;
1348         const dtrace_vector_t *v = dtp->dt_vector;
1349 
1350         if (v != NULL)
1351                 return (v->dtv_lookup_by_addr(dtp->dt_varg, addr, symp, sip));
1352 
1353         for (dmp = dt_list_next(&dtp->dt_modlist); dmp != NULL;
1354             dmp = dt_list_next(dmp)) {
1355                 if (addr - dmp->dm_text_va < dmp->dm_text_size ||
1356                     addr - dmp->dm_data_va < dmp->dm_data_size ||
1357                     addr - dmp->dm_bss_va < dmp->dm_bss_size)
1358                         break;
1359         }
1360 
1361         if (dmp == NULL)
1362                 return (dt_set_errno(dtp, EDT_NOSYMADDR));
1363 
1364         if (dt_module_load(dtp, dmp) == -1)
1365                 return (-1); /* dt_errno is set for us */
1366 
1367         if (symp != NULL) {
1368                 if (dmp->dm_ops->do_symaddr(dmp, addr, symp, &id) == NULL)
1369                         return (dt_set_errno(dtp, EDT_NOSYMADDR));
1370         }
1371 
1372         if (sip != NULL) {
1373                 sip->dts_object = dmp->dm_name;
1374 
1375                 if (symp != NULL) {
1376                         sip->dts_name = (const char *)
1377                             dmp->dm_strtab.cts_data + symp->st_name;
1378                         sip->dts_id = id;
1379                 } else {
1380                         sip->dts_name = NULL;
1381                         sip->dts_id = 0;
1382                 }
1383         }
1384 
1385         return (0);
1386 }
1387 
1388 boolean_t
1389 dt_is_forward_decl(ctf_file_t *file, ctf_id_t type)
1390 {
1391         ctf_id_t kind = ctf_type_kind(file, type);
1392 
1393         while ((type = ctf_type_reference(file, type)) != CTF_ERR) {
1394                 type = ctf_type_resolve(file, type);
1395                 kind = ctf_type_kind(file, type);
1396         }
1397 
1398         return (kind == CTF_K_FORWARD);
1399 }
1400 
1401 void
1402 dt_resolve_forward_decl(ctf_file_t **ctfp, ctf_id_t *type)
1403 {
1404         char name[DT_TYPE_NAMELEN];
1405 
1406         while (dt_is_forward_decl(*ctfp, *type)) {
1407                 char *tag = ctf_type_name(*ctfp, *type, name, sizeof (name));
1408                 dtrace_typeinfo_t dtt;
1409 
1410                 if (tag != NULL && dt_type_lookup(tag, &dtt) == 0 &&
1411                     (dtt.dtt_ctfp != *ctfp) || dtt.dtt_type != *type) {
1412                         *ctfp = dtt.dtt_ctfp;
1413                         *type = dtt.dtt_type;
1414                 } else {
1415                         /* All we have is the forward definition */
1416                         break;
1417                 }
1418         }
1419 }
1420 
1421 int
1422 dtrace_lookup_by_type(dtrace_hdl_t *dtp, const char *object, const char *name,
1423     dtrace_typeinfo_t *tip)
1424 {
1425         dtrace_typeinfo_t ti;
1426         dt_module_t *dmp;
1427         int found = 0;
1428         ctf_id_t id;
1429         uint_t n, i;
1430         int justone;
1431         ctf_file_t *fp;
1432         char *buf, *p, *q;
1433 
1434         uint_t mask = 0; /* mask of dt_module flags to match */
1435         uint_t bits = 0; /* flag bits that must be present */
1436 
1437         if (object != DTRACE_OBJ_EVERY &&
1438             object != DTRACE_OBJ_KMODS &&
1439             object != DTRACE_OBJ_UMODS) {
1440                 if ((dmp = dt_module_from_object(dtp, object)) == NULL)
1441                         return (-1); /* dt_errno is set for us */
1442 
1443                 if (dt_module_load(dtp, dmp) == -1)
1444                         return (-1); /* dt_errno is set for us */
1445                 n = 1;
1446                 justone = 1;
1447         } else {
1448                 if (object == DTRACE_OBJ_KMODS)
1449                         mask = bits = DT_DM_KERNEL;
1450                 else if (object == DTRACE_OBJ_UMODS)
1451                         mask = DT_DM_KERNEL;
1452 
1453                 dmp = dt_list_next(&dtp->dt_modlist);
1454                 n = dtp->dt_nmods;
1455                 justone = 0;
1456         }
1457 
1458         if (tip == NULL)
1459                 tip = &ti;
1460 
1461         for (; n > 0; n--, dmp = dt_list_next(dmp)) {
1462                 if ((dmp->dm_flags & mask) != bits)
1463                         continue; /* failed to match required attributes */
1464 
1465                 /*
1466                  * If we can't load the CTF container, continue on to the next
1467                  * module.  If our search was scoped to only one module then
1468                  * return immediately leaving dt_errno unmodified.
1469                  */
1470                 if (dt_module_hasctf(dtp, dmp) == 0) {
1471                         if (justone)
1472                                 return (-1);
1473                         continue;
1474                 }
1475 
1476                 /*
1477                  * Look up the type in the module's CTF container.  If our
1478                  * match is a forward declaration tag, save this choice in
1479                  * 'tip' and keep going in the hope that we will locate the
1480                  * underlying structure definition.  Otherwise just return.
1481                  */
1482                 if (dmp->dm_pid == 0) {
1483                         id = ctf_lookup_by_name(dmp->dm_ctfp, name);
1484                         fp = dmp->dm_ctfp;
1485                 } else {
1486                         if ((p = strchr(name, '`')) != NULL) {
1487                                 buf = strdup(name);
1488                                 if (buf == NULL)
1489                                         return (dt_set_errno(dtp, EDT_NOMEM));
1490                                 p = strchr(buf, '`');
1491                                 if ((q = strchr(p + 1, '`')) != NULL)
1492                                         p = q;
1493                                 *p = '\0';
1494                                 fp = dt_module_getctflib(dtp, dmp, buf);
1495                                 if (fp == NULL || (id = ctf_lookup_by_name(fp,
1496                                     p + 1)) == CTF_ERR)
1497                                         id = CTF_ERR;
1498                                 free(buf);
1499                         } else {
1500                                 for (i = 0; i < dmp->dm_nctflibs; i++) {
1501                                         fp = dmp->dm_libctfp[i];
1502                                         id = ctf_lookup_by_name(fp, name);
1503                                         if (id != CTF_ERR)
1504                                                 break;
1505                                 }
1506                         }
1507                 }
1508                 if (id != CTF_ERR) {
1509                         tip->dtt_object = dmp->dm_name;
1510                         tip->dtt_ctfp = fp;
1511                         tip->dtt_type = id;
1512                         if (!dt_is_forward_decl(fp, ctf_type_resolve(fp, id)))
1513                                 return (0);
1514 
1515                         found++;
1516                 }
1517         }
1518 
1519         if (found == 0)
1520                 return (dt_set_errno(dtp, EDT_NOTYPE));
1521 
1522         return (0);
1523 }
1524 
1525 int
1526 dtrace_symbol_type(dtrace_hdl_t *dtp, const GElf_Sym *symp,
1527     const dtrace_syminfo_t *sip, dtrace_typeinfo_t *tip)
1528 {
1529         dt_module_t *dmp;
1530 
1531         tip->dtt_object = NULL;
1532         tip->dtt_ctfp = NULL;
1533         tip->dtt_type = CTF_ERR;
1534         tip->dtt_flags = 0;
1535 
1536         if ((dmp = dt_module_lookup_by_name(dtp, sip->dts_object)) == NULL)
1537                 return (dt_set_errno(dtp, EDT_NOMOD));
1538 
1539         if (symp->st_shndx == SHN_UNDEF && dmp->dm_extern != NULL) {
1540                 dt_ident_t *idp =
1541                     dt_idhash_lookup(dmp->dm_extern, sip->dts_name);
1542 
1543                 if (idp == NULL)
1544                         return (dt_set_errno(dtp, EDT_NOSYM));
1545 
1546                 tip->dtt_ctfp = idp->di_ctfp;
1547                 tip->dtt_type = idp->di_type;
1548 
1549         } else if (GELF_ST_TYPE(symp->st_info) != STT_FUNC) {
1550                 if (dt_module_getctf(dtp, dmp) == NULL)
1551                         return (-1); /* errno is set for us */
1552 
1553                 tip->dtt_ctfp = dmp->dm_ctfp;
1554                 tip->dtt_type = ctf_lookup_by_symbol(dmp->dm_ctfp, sip->dts_id);
1555 
1556                 if (tip->dtt_type == CTF_ERR) {
1557                         dtp->dt_ctferr = ctf_errno(tip->dtt_ctfp);
1558                         return (dt_set_errno(dtp, EDT_CTF));
1559                 }
1560 
1561         } else {
1562                 tip->dtt_ctfp = DT_FPTR_CTFP(dtp);
1563                 tip->dtt_type = DT_FPTR_TYPE(dtp);
1564         }
1565 
1566         tip->dtt_object = dmp->dm_name;
1567         return (0);
1568 }
1569 
1570 static dtrace_objinfo_t *
1571 dt_module_info(const dt_module_t *dmp, dtrace_objinfo_t *dto)
1572 {
1573         dto->dto_name = dmp->dm_name;
1574         dto->dto_file = dmp->dm_file;
1575         dto->dto_id = dmp->dm_modid;
1576         dto->dto_flags = 0;
1577 
1578         if (dmp->dm_flags & DT_DM_KERNEL)
1579                 dto->dto_flags |= DTRACE_OBJ_F_KERNEL;
1580         if (dmp->dm_flags & DT_DM_PRIMARY)
1581                 dto->dto_flags |= DTRACE_OBJ_F_PRIMARY;
1582 
1583         dto->dto_text_va = dmp->dm_text_va;
1584         dto->dto_text_size = dmp->dm_text_size;
1585         dto->dto_data_va = dmp->dm_data_va;
1586         dto->dto_data_size = dmp->dm_data_size;
1587         dto->dto_bss_va = dmp->dm_bss_va;
1588         dto->dto_bss_size = dmp->dm_bss_size;
1589 
1590         return (dto);
1591 }
1592 
1593 int
1594 dtrace_object_iter(dtrace_hdl_t *dtp, dtrace_obj_f *func, void *data)
1595 {
1596         const dt_module_t *dmp = dt_list_next(&dtp->dt_modlist);
1597         dtrace_objinfo_t dto;
1598         int rv;
1599 
1600         for (; dmp != NULL; dmp = dt_list_next(dmp)) {
1601                 if ((rv = (*func)(dtp, dt_module_info(dmp, &dto), data)) != 0)
1602                         return (rv);
1603         }
1604 
1605         return (0);
1606 }
1607 
1608 int
1609 dtrace_object_info(dtrace_hdl_t *dtp, const char *object, dtrace_objinfo_t *dto)
1610 {
1611         dt_module_t *dmp;
1612 
1613         if (object == DTRACE_OBJ_EVERY || object == DTRACE_OBJ_KMODS ||
1614             object == DTRACE_OBJ_UMODS || dto == NULL)
1615                 return (dt_set_errno(dtp, EINVAL));
1616 
1617         if ((dmp = dt_module_from_object(dtp, object)) == NULL)
1618                 return (-1); /* dt_errno is set for us */
1619 
1620         if (dt_module_load(dtp, dmp) == -1)
1621                 return (-1); /* dt_errno is set for us */
1622 
1623         (void) dt_module_info(dmp, dto);
1624         return (0);
1625 }