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