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