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