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, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
8 *
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
13 *
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 *
20 * CDDL HEADER END
21 */
22
23 #ifdef HAVE_NBTOOL_CONFIG_H
24 #include "nbtool_config.h"
25 #endif
26 /*
27 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
28 * Use is subject to license terms.
29 */
30 /*
31 * Copyright (c) 2013, Joyent, Inc. All rights reserved.
32 */
33
34 #include <ctf_impl.h>
35 #include <sys/mman.h>
36 #include <sys/zmod.h>
37
38 static const ctf_dmodel_t _libctf_models[] = {
39 { "ILP32", CTF_MODEL_ILP32, 4, 1, 2, 4, 4 },
40 { "LP64", CTF_MODEL_LP64, 8, 1, 2, 4, 8 },
41 { NULL, 0, 0, 0, 0, 0, 0 }
42 };
43
44 const char _CTF_SECTION[] = ".SUNW_ctf";
45 const char _CTF_NULLSTR[] = "";
46
47 int _libctf_version = CTF_VERSION; /* library client version */
48 int _libctf_debug = 0; /* debugging messages enabled */
49
50 static uint_t
get_kind_v2(uint_t info)51 get_kind_v2(uint_t info)
52 {
53 return (CTF_V2_INFO_KIND((ushort_t)info));
54 }
55
56 static uint_t
get_root_v2(uint_t info)57 get_root_v2(uint_t info)
58 {
59 return (CTF_V2_INFO_ISROOT((ushort_t)info));
60 }
61
62 static uint_t
get_vlen_v2(uint_t info)63 get_vlen_v2(uint_t info)
64 {
65 return (CTF_V2_INFO_VLEN((ushort_t)info));
66 }
67
68 static uint_t
get_max_vlen_v2(void)69 get_max_vlen_v2(void)
70 {
71 return (CTF_V2_MAX_VLEN);
72 }
73
74 static uint_t
get_max_size_v2(void)75 get_max_size_v2(void)
76 {
77 return (CTF_V2_MAX_SIZE);
78 }
79
80 static uint_t
get_max_type_v2(void)81 get_max_type_v2(void)
82 {
83 return (CTF_V2_MAX_TYPE);
84 }
85
86 static uint_t
get_lsize_sent_v2(void)87 get_lsize_sent_v2(void)
88 {
89 return (CTF_V2_LSIZE_SENT);
90 }
91
92 static uint_t
get_lstruct_thresh_v2(void)93 get_lstruct_thresh_v2(void)
94 {
95 return (CTF_V2_LSTRUCT_THRESH);
96 }
97
98 static uint_t
type_info_v2(uint_t kind,uint_t isroot,uint_t len)99 type_info_v2(uint_t kind, uint_t isroot, uint_t len)
100 {
101 return (CTF_V2_TYPE_INFO(kind, isroot, len));
102 }
103
104 static int
type_isparent_v2(uint_t id)105 type_isparent_v2(uint_t id)
106 {
107 return (CTF_V2_TYPE_ISPARENT(id));
108 }
109
110 static int
type_ischild_v2(uint_t id)111 type_ischild_v2(uint_t id)
112 {
113 return (CTF_V2_TYPE_ISCHILD(id));
114 }
115
116 static uint_t
type_to_index_v2(uint_t t)117 type_to_index_v2(uint_t t)
118 {
119 return (CTF_V2_TYPE_TO_INDEX(t));
120 }
121
122 static uint_t
index_to_type_v2(uint_t id,uint_t child)123 index_to_type_v2(uint_t id, uint_t child)
124 {
125 return (CTF_V2_INDEX_TO_TYPE(id, child));
126 }
127
128 static uint_t
get_kind_v3(uint_t info)129 get_kind_v3(uint_t info)
130 {
131 return (CTF_V3_INFO_KIND(info));
132 }
133
134 static uint_t
get_root_v3(uint_t info)135 get_root_v3(uint_t info)
136 {
137 return (CTF_V3_INFO_ISROOT(info));
138 }
139
140 static uint_t
get_vlen_v3(uint_t info)141 get_vlen_v3(uint_t info)
142 {
143 return (CTF_V3_INFO_VLEN(info));
144 }
145
146 static uint_t
get_max_vlen_v3(void)147 get_max_vlen_v3(void)
148 {
149 return (CTF_V3_MAX_VLEN);
150 }
151
152 static uint_t
get_max_size_v3(void)153 get_max_size_v3(void)
154 {
155 return (CTF_V3_MAX_SIZE);
156 }
157
158 static uint_t
get_max_type_v3(void)159 get_max_type_v3(void)
160 {
161 return (CTF_V3_MAX_TYPE);
162 }
163
164 static uint_t
get_lsize_sent_v3(void)165 get_lsize_sent_v3(void)
166 {
167 return (CTF_V3_LSIZE_SENT);
168 }
169
170 static uint_t
get_lstruct_thresh_v3(void)171 get_lstruct_thresh_v3(void)
172 {
173 return (CTF_V3_LSTRUCT_THRESH);
174 }
175
176 static uint_t
type_info_v3(uint_t kind,uint_t isroot,uint_t len)177 type_info_v3(uint_t kind, uint_t isroot, uint_t len)
178 {
179 return (CTF_V3_TYPE_INFO(kind, isroot, len));
180 }
181
182 static int
type_isparent_v3(uint_t id)183 type_isparent_v3(uint_t id)
184 {
185 return (CTF_V3_TYPE_ISPARENT(id));
186 }
187
188 static int
type_ischild_v3(uint_t id)189 type_ischild_v3(uint_t id)
190 {
191 return (CTF_V3_TYPE_ISCHILD(id));
192 }
193
194 static uint_t
type_to_index_v3(uint_t t)195 type_to_index_v3(uint_t t)
196 {
197 return (CTF_V3_TYPE_TO_INDEX(t));
198 }
199
200 static uint_t
index_to_type_v3(uint_t id,uint_t child)201 index_to_type_v3(uint_t id, uint_t child)
202 {
203 return (CTF_V3_INDEX_TO_TYPE(id, child));
204 }
205
206 #define CTF_FILEOPS_ENTRY(v) \
207 { \
208 .ctfo_get_kind = get_kind_v ## v, \
209 .ctfo_get_root = get_root_v ## v, \
210 .ctfo_get_vlen = get_vlen_v ## v, \
211 .ctfo_get_max_vlen = get_max_vlen_v ## v, \
212 .ctfo_get_max_size = get_max_size_v ## v, \
213 .ctfo_get_max_type = get_max_type_v ## v, \
214 .ctfo_get_lsize_sent = get_lsize_sent_v ## v, \
215 .ctfo_get_lstruct_thresh = get_lstruct_thresh_v ## v, \
216 .ctfo_type_info = type_info_v ## v, \
217 .ctfo_type_isparent = type_isparent_v ## v, \
218 .ctfo_type_ischild = type_ischild_v ## v, \
219 .ctfo_type_to_index = type_to_index_v ## v, \
220 .ctfo_index_to_type = index_to_type_v ## v \
221 }
222
223 #define CTF_FILEOPS_NO_ENTRY() \
224 { \
225 .ctfo_get_kind = NULL, \
226 .ctfo_get_root = NULL, \
227 .ctfo_get_vlen = NULL, \
228 .ctfo_get_max_vlen = NULL, \
229 .ctfo_get_max_size = NULL, \
230 .ctfo_get_max_type = NULL, \
231 .ctfo_get_lsize_sent = NULL, \
232 .ctfo_get_lstruct_thresh = NULL, \
233 .ctfo_type_info = NULL, \
234 .ctfo_type_isparent = NULL, \
235 .ctfo_type_ischild = NULL, \
236 .ctfo_type_to_index = NULL, \
237 .ctfo_index_to_type = NULL \
238 }
239 static const ctf_fileops_t ctf_fileops[] = {
240 CTF_FILEOPS_NO_ENTRY(),
241 CTF_FILEOPS_NO_ENTRY(),
242 CTF_FILEOPS_ENTRY(2),
243 CTF_FILEOPS_ENTRY(3),
244 };
245
246 /*
247 * Convert a 32-bit ELF symbol into GElf (Elf64) and return a pointer to it.
248 */
249 static Elf64_Sym *
sym_to_gelf(const Elf32_Sym * src,Elf64_Sym * dst)250 sym_to_gelf(const Elf32_Sym *src, Elf64_Sym *dst)
251 {
252 dst->st_name = src->st_name;
253 dst->st_value = src->st_value;
254 dst->st_size = src->st_size;
255 dst->st_info = src->st_info;
256 dst->st_other = src->st_other;
257 dst->st_shndx = src->st_shndx;
258
259 return (dst);
260 }
261
262 /*
263 * Initialize the symtab translation table by filling each entry with the
264 * offset of the CTF type or function data corresponding to each STT_FUNC or
265 * STT_OBJECT entry in the symbol table.
266 */
267 static int
init_symtab(ctf_file_t * fp,const ctf_header_t * hp,const ctf_sect_t * sp,const ctf_sect_t * strp)268 init_symtab(ctf_file_t *fp, const ctf_header_t *hp,
269 const ctf_sect_t *sp, const ctf_sect_t *strp)
270 {
271 const uchar_t *symp = sp->cts_data;
272 uint_t *xp = fp->ctf_sxlate;
273 uint_t *xend = xp + fp->ctf_nsyms;
274
275 uint_t objtoff = hp->cth_objtoff;
276 uint_t funcoff = hp->cth_funcoff;
277
278 uint_t info, vlen;
279
280 Elf64_Sym sym, *gsp;
281 const char *name;
282
283 /*
284 * The CTF data object and function type sections are ordered to match
285 * the relative order of the respective symbol types in the symtab.
286 * If no type information is available for a symbol table entry, a
287 * pad is inserted in the CTF section. As a further optimization,
288 * anonymous or undefined symbols are omitted from the CTF data.
289 */
290 for (; xp < xend; xp++, symp += sp->cts_entsize) {
291 if (sp->cts_entsize == sizeof (Elf32_Sym))
292 gsp = sym_to_gelf((Elf32_Sym *)(uintptr_t)symp, &sym);
293 else
294 gsp = (Elf64_Sym *)(uintptr_t)symp;
295
296 if (gsp->st_name < strp->cts_size)
297 name = (const char *)strp->cts_data + gsp->st_name;
298 else
299 name = _CTF_NULLSTR;
300
301 if (gsp->st_name == 0 || gsp->st_shndx == SHN_UNDEF ||
302 strcmp(name, "_START_") == 0 ||
303 strcmp(name, "_END_") == 0) {
304 *xp = -1u;
305 continue;
306 }
307
308 switch (ELF64_ST_TYPE(gsp->st_info)) {
309 case STT_OBJECT:
310 if (objtoff >= hp->cth_funcoff ||
311 (gsp->st_shndx == SHN_ABS && gsp->st_value == 0)) {
312 *xp = -1u;
313 break;
314 }
315
316 *xp = objtoff;
317 objtoff += fp->ctf_idwidth;
318 break;
319
320 case STT_FUNC:
321 if (funcoff >= hp->cth_typeoff) {
322 *xp = -1u;
323 break;
324 }
325
326 *xp = funcoff;
327
328 info = *(uint_t *)((uintptr_t)fp->ctf_buf + funcoff);
329 vlen = LCTF_INFO_VLEN(fp, info);
330
331 /*
332 * If we encounter a zero pad at the end, just skip it.
333 * Otherwise skip over the function and its return type
334 * (+2) and the argument list (vlen).
335 */
336 if (LCTF_INFO_KIND(fp, info) == CTF_K_UNKNOWN &&
337 vlen == 0)
338 funcoff += fp->ctf_idwidth; /* skip pad */
339 else
340 funcoff += roundup2(fp->ctf_idwidth * (vlen + 2), 4);
341 break;
342
343 default:
344 *xp = -1u;
345 break;
346 }
347 }
348
349 ctf_dprintf("loaded %lu symtab entries\n", fp->ctf_nsyms);
350 return (0);
351 }
352
353 /*
354 * Initialize the type ID translation table with the byte offset of each type,
355 * and initialize the hash tables of each named type.
356 */
357 static int
init_types(ctf_file_t * fp,const ctf_header_t * cth)358 init_types(ctf_file_t *fp, const ctf_header_t *cth)
359 {
360 const void *tbuf = (const void *)(fp->ctf_buf + cth->cth_typeoff);
361 const void *tend = (const void *)(fp->ctf_buf + cth->cth_stroff);
362
363 ulong_t pop[CTF_K_MAX + 1] = { 0 };
364 const void *tp;
365 ctf_hash_t *hp;
366 uint_t id, dst;
367 uint_t *xp;
368
369 /*
370 * We initially determine whether the container is a child or a parent
371 * based on the value of cth_parname. To support containers that pre-
372 * date cth_parname, we also scan the types themselves for references
373 * to values in the range reserved for child types in our first pass.
374 */
375 int child = cth->cth_parname != 0;
376 int nlstructs = 0, nlunions = 0;
377 int err;
378
379 /*
380 * We make two passes through the entire type section. In this first
381 * pass, we count the number of each type and the total number of types.
382 */
383 for (tp = tbuf; tp < tend; fp->ctf_typemax++) {
384 ssize_t size, increment;
385
386 size_t vbytes;
387 uint_t kind, n, type, vlen;
388
389 (void) ctf_get_ctt_size(fp, tp, &size, &increment);
390 ctf_get_ctt_info(fp, tp, &kind, &vlen, NULL);
391 ctf_get_ctt_index(fp, tp, NULL, &type, NULL);
392
393 switch (kind) {
394 case CTF_K_INTEGER:
395 case CTF_K_FLOAT:
396 vbytes = sizeof (uint_t);
397 break;
398 case CTF_K_ARRAY:
399 if (fp->ctf_version == CTF_VERSION_2)
400 vbytes = sizeof (struct ctf_array_v2);
401 else
402 vbytes = sizeof (struct ctf_array_v3);
403 break;
404 case CTF_K_FUNCTION:
405 vbytes = roundup2(fp->ctf_idwidth * vlen, 4);
406 break;
407 case CTF_K_STRUCT:
408 case CTF_K_UNION: {
409 ssize_t increment1;
410 uint_t xtype;
411 const char *mp =
412 (const char *)((uintptr_t)tp + increment);
413
414 vbytes = 0;
415 for (n = vlen; n != 0; n--, mp += increment1) {
416 ctf_get_ctm_info(fp, mp, size, &increment1, &xtype,
417 NULL, NULL);
418 child |= LCTF_TYPE_ISCHILD(fp, xtype);
419 vbytes += increment1;
420 }
421 break;
422 }
423 case CTF_K_ENUM:
424 vbytes = sizeof (ctf_enum_t) * vlen;
425 break;
426 case CTF_K_FORWARD:
427 /*
428 * For forward declarations, ctt_type is the CTF_K_*
429 * kind for the tag, so bump that population count too.
430 * If ctt_type is unknown, treat the tag as a struct.
431 */
432 if (type == CTF_K_UNKNOWN || type >= CTF_K_MAX)
433 pop[CTF_K_STRUCT]++;
434 else
435 pop[type]++;
436 /*FALLTHRU*/
437 case CTF_K_UNKNOWN:
438 vbytes = 0;
439 break;
440 case CTF_K_POINTER:
441 case CTF_K_TYPEDEF:
442 case CTF_K_VOLATILE:
443 case CTF_K_CONST:
444 case CTF_K_RESTRICT:
445 child |= LCTF_TYPE_ISCHILD(fp, type);
446 vbytes = 0;
447 break;
448 default:
449 ctf_dprintf("detected invalid CTF kind -- %u\n", kind);
450 return (ECTF_CORRUPT);
451 }
452 tp = (const void *)((uintptr_t)tp + increment + vbytes);
453 pop[kind]++;
454 }
455
456 /*
457 * If we detected a reference to a child type ID, then we know this
458 * container is a child and may have a parent's types imported later.
459 */
460 if (child) {
461 ctf_dprintf("CTF container %p is a child\n", (void *)fp);
462 fp->ctf_flags |= LCTF_CHILD;
463 } else
464 ctf_dprintf("CTF container %p is a parent\n", (void *)fp);
465
466 /*
467 * Now that we've counted up the number of each type, we can allocate
468 * the hash tables, type translation table, and pointer table.
469 */
470 if ((err = ctf_hash_create(&fp->ctf_structs, pop[CTF_K_STRUCT])) != 0)
471 return (err);
472
473 if ((err = ctf_hash_create(&fp->ctf_unions, pop[CTF_K_UNION])) != 0)
474 return (err);
475
476 if ((err = ctf_hash_create(&fp->ctf_enums, pop[CTF_K_ENUM])) != 0)
477 return (err);
478
479 if ((err = ctf_hash_create(&fp->ctf_names,
480 pop[CTF_K_INTEGER] + pop[CTF_K_FLOAT] + pop[CTF_K_FUNCTION] +
481 pop[CTF_K_TYPEDEF] + pop[CTF_K_POINTER] + pop[CTF_K_VOLATILE] +
482 pop[CTF_K_CONST] + pop[CTF_K_RESTRICT])) != 0)
483 return (err);
484
485 fp->ctf_txlate = ctf_alloc(sizeof (uint_t) * (fp->ctf_typemax + 1));
486 fp->ctf_ptrtab = ctf_alloc(sizeof (uint_t) * (fp->ctf_typemax + 1));
487
488 if (fp->ctf_txlate == NULL || fp->ctf_ptrtab == NULL)
489 return (EAGAIN); /* memory allocation failed */
490
491 xp = fp->ctf_txlate;
492 *xp++ = 0; /* type id 0 is used as a sentinel value */
493
494 memset(fp->ctf_txlate, 0, sizeof (*fp->ctf_txlate) * (fp->ctf_typemax + 1));
495 memset(fp->ctf_ptrtab, 0, sizeof (*fp->ctf_ptrtab) * (fp->ctf_typemax + 1));
496
497 /*
498 * In the second pass through the types, we fill in each entry of the
499 * type and pointer tables and add names to the appropriate hashes.
500 */
501 for (id = 1, tp = tbuf; tp < tend; xp++, id++) {
502 const struct ctf_type_v3 *ctt = tp;
503 uint_t kind, type, vlen;
504 ssize_t size, increment;
505
506 const char *name;
507 size_t vbytes;
508 ctf_helem_t *hep;
509 ctf_encoding_t cte;
510
511 (void) ctf_get_ctt_size(fp, tp, &size, &increment);
512 ctf_get_ctt_info(fp, tp, &kind, &vlen, NULL);
513 ctf_get_ctt_index(fp, tp, NULL, &type, NULL);
514 name = ctf_type_rname(fp, tp);
515
516 switch (kind) {
517 case CTF_K_INTEGER:
518 case CTF_K_FLOAT:
519 /*
520 * Only insert a new integer base type definition if
521 * this type name has not been defined yet. We re-use
522 * the names with different encodings for bit-fields.
523 */
524 if ((hep = ctf_hash_lookup(&fp->ctf_names, fp,
525 name, strlen(name))) == NULL) {
526 err = ctf_hash_insert(&fp->ctf_names, fp,
527 LCTF_INDEX_TO_TYPE(fp, id, child),
528 ctt->ctt_name);
529 if (err != 0 && err != ECTF_STRTAB)
530 return (err);
531 } else if (ctf_type_encoding(fp, hep->h_type,
532 &cte) == 0 && cte.cte_bits == 0) {
533 /*
534 * Work-around SOS8 stabs bug: replace existing
535 * intrinsic w/ same name if it was zero bits.
536 */
537 hep->h_type = LCTF_INDEX_TO_TYPE(fp, id, child);
538 }
539 vbytes = sizeof (uint_t);
540 break;
541
542 case CTF_K_ARRAY:
543 if (fp->ctf_version == CTF_VERSION_2)
544 vbytes = sizeof (struct ctf_array_v2);
545 else
546 vbytes = sizeof (struct ctf_array_v3);
547 break;
548
549 case CTF_K_FUNCTION:
550 err = ctf_hash_insert(&fp->ctf_names, fp,
551 LCTF_INDEX_TO_TYPE(fp, id, child), ctt->ctt_name);
552 if (err != 0 && err != ECTF_STRTAB)
553 return (err);
554 vbytes = roundup2(fp->ctf_idwidth * vlen, 4);
555 break;
556
557 case CTF_K_STRUCT:
558 err = ctf_hash_define(&fp->ctf_structs, fp,
559 LCTF_INDEX_TO_TYPE(fp, id, child), ctt->ctt_name);
560
561 if (err != 0 && err != ECTF_STRTAB)
562 return (err);
563
564 if (fp->ctf_version == CTF_VERSION_2) {
565 if (size < LCTF_LSTRUCT_THRESH(fp))
566 vbytes = sizeof (struct ctf_member_v2) *
567 vlen;
568 else {
569 vbytes =
570 sizeof (struct ctf_lmember_v2) *
571 vlen;
572 nlstructs++;
573 }
574 } else {
575 if (size < LCTF_LSTRUCT_THRESH(fp))
576 vbytes = sizeof (struct ctf_member_v3) *
577 vlen;
578 else {
579 vbytes =
580 sizeof (struct ctf_lmember_v3) *
581 vlen;
582 nlstructs++;
583 }
584 }
585 break;
586
587 case CTF_K_UNION:
588 err = ctf_hash_define(&fp->ctf_unions, fp,
589 LCTF_INDEX_TO_TYPE(fp, id, child), ctt->ctt_name);
590
591 if (err != 0 && err != ECTF_STRTAB)
592 return (err);
593
594 if (fp->ctf_version == CTF_VERSION_2) {
595 if (size < LCTF_LSTRUCT_THRESH(fp))
596 vbytes = sizeof (struct ctf_member_v2) *
597 vlen;
598 else {
599 vbytes =
600 sizeof (struct ctf_lmember_v2) *
601 vlen;
602 nlunions++;
603 }
604 } else {
605 if (size < LCTF_LSTRUCT_THRESH(fp))
606 vbytes = sizeof (struct ctf_member_v3) *
607 vlen;
608 else {
609 vbytes =
610 sizeof (struct ctf_lmember_v3) *
611 vlen;
612 nlunions++;
613 }
614 }
615 break;
616
617 case CTF_K_ENUM:
618 err = ctf_hash_define(&fp->ctf_enums, fp,
619 LCTF_INDEX_TO_TYPE(fp, id, child), ctt->ctt_name);
620
621 if (err != 0 && err != ECTF_STRTAB)
622 return (err);
623
624 vbytes = sizeof (ctf_enum_t) * vlen;
625 break;
626
627 case CTF_K_TYPEDEF:
628 err = ctf_hash_insert(&fp->ctf_names, fp,
629 LCTF_INDEX_TO_TYPE(fp, id, child), ctt->ctt_name);
630 if (err != 0 && err != ECTF_STRTAB)
631 return (err);
632 vbytes = 0;
633 break;
634
635 case CTF_K_FORWARD:
636 /*
637 * Only insert forward tags into the given hash if the
638 * type or tag name is not already present.
639 */
640 switch (type) {
641 case CTF_K_STRUCT:
642 hp = &fp->ctf_structs;
643 break;
644 case CTF_K_UNION:
645 hp = &fp->ctf_unions;
646 break;
647 case CTF_K_ENUM:
648 hp = &fp->ctf_enums;
649 break;
650 default:
651 hp = &fp->ctf_structs;
652 }
653
654 if (ctf_hash_lookup(hp, fp,
655 name, strlen(name)) == NULL) {
656 err = ctf_hash_insert(hp, fp,
657 LCTF_INDEX_TO_TYPE(fp, id, child),
658 ctt->ctt_name);
659 if (err != 0 && err != ECTF_STRTAB)
660 return (err);
661 }
662 vbytes = 0;
663 break;
664
665 case CTF_K_POINTER:
666 /*
667 * If the type referenced by the pointer is in this CTF
668 * container, then store the index of the pointer type
669 * in fp->ctf_ptrtab[ index of referenced type ].
670 */
671 if (LCTF_TYPE_ISCHILD(fp, type) == child &&
672 LCTF_TYPE_TO_INDEX(fp, type) <= fp->ctf_typemax)
673 fp->ctf_ptrtab[
674 LCTF_TYPE_TO_INDEX(fp, type)] = id;
675 /*FALLTHRU*/
676
677 case CTF_K_VOLATILE:
678 case CTF_K_CONST:
679 case CTF_K_RESTRICT:
680 err = ctf_hash_insert(&fp->ctf_names, fp,
681 LCTF_INDEX_TO_TYPE(fp, id, child), ctt->ctt_name);
682 if (err != 0 && err != ECTF_STRTAB)
683 return (err);
684 /*FALLTHRU*/
685
686 default:
687 vbytes = 0;
688 break;
689 }
690
691 *xp = (uint_t)((uintptr_t)tp - (uintptr_t)fp->ctf_buf);
692 tp = (const void *)((uintptr_t)tp + increment + vbytes);
693 }
694
695 ctf_dprintf("%lu total types processed\n", fp->ctf_typemax);
696 ctf_dprintf("%u enum names hashed\n", ctf_hash_size(&fp->ctf_enums));
697 ctf_dprintf("%u struct names hashed (%d long)\n",
698 ctf_hash_size(&fp->ctf_structs), nlstructs);
699 ctf_dprintf("%u union names hashed (%d long)\n",
700 ctf_hash_size(&fp->ctf_unions), nlunions);
701 ctf_dprintf("%u base type names hashed\n",
702 ctf_hash_size(&fp->ctf_names));
703
704 /*
705 * Make an additional pass through the pointer table to find pointers
706 * that point to anonymous typedef nodes. If we find one, modify the
707 * pointer table so that the pointer is also known to point to the
708 * node that is referenced by the anonymous typedef node.
709 */
710 for (id = 1; id <= fp->ctf_typemax; id++) {
711 if ((dst = fp->ctf_ptrtab[id]) != 0) {
712 uint_t index, kind;
713 int ischild;
714
715 tp = LCTF_INDEX_TO_TYPEPTR(fp, id);
716 ctf_get_ctt_info(fp, tp, &kind, NULL, NULL);
717 ctf_get_ctt_index(fp, tp, &index, NULL, &ischild);
718
719 if (kind == CTF_K_TYPEDEF &&
720 strcmp(ctf_type_rname(fp, tp), "") == 0 &&
721 ischild == child && index <= fp->ctf_typemax)
722 fp->ctf_ptrtab[index] = dst;
723 }
724 }
725
726 return (0);
727 }
728
729 /*
730 * Decode the specified CTF buffer and optional symbol table and create a new
731 * CTF container representing the symbolic debugging information. This code
732 * can be used directly by the debugger, or it can be used as the engine for
733 * ctf_fdopen() or ctf_open(), below.
734 */
735 ctf_file_t *
ctf_bufopen(const ctf_sect_t * ctfsect,const ctf_sect_t * symsect,const ctf_sect_t * strsect,int * errp)736 ctf_bufopen(const ctf_sect_t *ctfsect, const ctf_sect_t *symsect,
737 const ctf_sect_t *strsect, int *errp)
738 {
739 const ctf_preamble_t *pp;
740 ctf_header_t hp;
741 ctf_file_t *fp;
742 void *buf, *base;
743 size_t size, hdrsz;
744 int err;
745
746 if (ctfsect == NULL || ((symsect == NULL) != (strsect == NULL)))
747 return (ctf_set_open_errno(errp, EINVAL));
748
749 if (symsect != NULL && symsect->cts_entsize != sizeof (Elf32_Sym) &&
750 symsect->cts_entsize != sizeof (Elf64_Sym))
751 return (ctf_set_open_errno(errp, ECTF_SYMTAB));
752
753 if (symsect != NULL && symsect->cts_data == NULL)
754 return (ctf_set_open_errno(errp, ECTF_SYMBAD));
755
756 if (strsect != NULL && strsect->cts_data == NULL)
757 return (ctf_set_open_errno(errp, ECTF_STRBAD));
758
759 if (ctfsect->cts_size < sizeof (ctf_preamble_t))
760 return (ctf_set_open_errno(errp, ECTF_NOCTFBUF));
761
762 pp = (const ctf_preamble_t *)ctfsect->cts_data;
763
764 ctf_dprintf("ctf_bufopen: magic=0x%x version=%u\n",
765 pp->ctp_magic, pp->ctp_version);
766
767 /*
768 * Validate each part of the CTF header (either V1 or V2).
769 * First, we validate the preamble (common to all versions). At that
770 * point, we know specific header version, and can validate the
771 * version-specific parts including section offsets and alignments.
772 */
773 if (pp->ctp_magic != CTF_MAGIC)
774 return (ctf_set_open_errno(errp, ECTF_NOCTFBUF));
775
776 if (pp->ctp_version == CTF_VERSION_2 ||
777 pp->ctp_version == CTF_VERSION_3) {
778 if (ctfsect->cts_size < sizeof (ctf_header_t))
779 return (ctf_set_open_errno(errp, ECTF_NOCTFBUF));
780
781 memcpy(&hp, ctfsect->cts_data, sizeof (hp));
782 hdrsz = sizeof (ctf_header_t);
783
784 } else
785 return (ctf_set_open_errno(errp, ECTF_CTFVERS));
786
787 size = hp.cth_stroff + hp.cth_strlen;
788
789 ctf_dprintf("ctf_bufopen: uncompressed size=%lu\n", (ulong_t)size);
790
791 if (hp.cth_lbloff > size || hp.cth_objtoff > size ||
792 hp.cth_funcoff > size || hp.cth_typeoff > size ||
793 hp.cth_stroff > size)
794 return (ctf_set_open_errno(errp, ECTF_CORRUPT));
795
796 if (hp.cth_lbloff > hp.cth_objtoff ||
797 hp.cth_objtoff > hp.cth_funcoff ||
798 hp.cth_funcoff > hp.cth_typeoff ||
799 hp.cth_typeoff > hp.cth_stroff)
800 return (ctf_set_open_errno(errp, ECTF_CORRUPT));
801
802 if ((hp.cth_lbloff & 3) || (hp.cth_objtoff & 1) ||
803 (hp.cth_funcoff & 1) || (hp.cth_typeoff & 3))
804 return (ctf_set_open_errno(errp, ECTF_CORRUPT));
805
806 /*
807 * Once everything is determined to be valid, attempt to decompress
808 * the CTF data buffer if it is compressed. Otherwise we just put
809 * the data section's buffer pointer into ctf_buf, below.
810 */
811 if (hp.cth_flags & CTF_F_COMPRESS) {
812 size_t srclen, dstlen;
813 const void *src;
814 int rc = Z_OK;
815
816 if (ctf_zopen(errp) == NULL)
817 return (NULL); /* errp is set for us */
818
819 if ((base = ctf_data_alloc(size + hdrsz)) == MAP_FAILED)
820 return (ctf_set_open_errno(errp, ECTF_ZALLOC));
821
822 memcpy(base, ctfsect->cts_data, hdrsz);
823 ((ctf_preamble_t *)base)->ctp_flags &= ~CTF_F_COMPRESS;
824 buf = (uchar_t *)base + hdrsz;
825
826 src = (uchar_t *)ctfsect->cts_data + hdrsz;
827 srclen = ctfsect->cts_size - hdrsz;
828 dstlen = size;
829
830 if ((rc = z_uncompress(buf, &dstlen, src, srclen)) != Z_OK) {
831 ctf_dprintf("zlib inflate err: %s\n", z_strerror(rc));
832 ctf_data_free(base, size + hdrsz);
833 return (ctf_set_open_errno(errp, ECTF_DECOMPRESS));
834 }
835
836 if (dstlen != size) {
837 ctf_dprintf("zlib inflate short -- got %lu of %lu "
838 "bytes\n", (ulong_t)dstlen, (ulong_t)size);
839 ctf_data_free(base, size + hdrsz);
840 return (ctf_set_open_errno(errp, ECTF_CORRUPT));
841 }
842
843 ctf_data_protect(base, size + hdrsz);
844
845 } else {
846 base = (void *)ctfsect->cts_data;
847 buf = (uchar_t *)base + hdrsz;
848 }
849
850 /*
851 * Once we have uncompressed and validated the CTF data buffer, we can
852 * proceed with allocating a ctf_file_t and initializing it.
853 */
854 if ((fp = ctf_alloc(sizeof (ctf_file_t))) == NULL)
855 return (ctf_set_open_errno(errp, EAGAIN));
856
857 memset(fp, 0, sizeof (ctf_file_t));
858 fp->ctf_version = hp.cth_version;
859 fp->ctf_idwidth = fp->ctf_version == CTF_VERSION_2 ? 2 : 4;
860 fp->ctf_fileops = &ctf_fileops[hp.cth_version];
861 memcpy(&fp->ctf_data, ctfsect, sizeof (ctf_sect_t));
862
863 if (symsect != NULL) {
864 memcpy(&fp->ctf_symtab, symsect, sizeof (ctf_sect_t));
865 memcpy(&fp->ctf_strtab, strsect, sizeof (ctf_sect_t));
866 }
867
868 if (fp->ctf_data.cts_name != NULL)
869 fp->ctf_data.cts_name = ctf_strdup(fp->ctf_data.cts_name);
870 if (fp->ctf_symtab.cts_name != NULL)
871 fp->ctf_symtab.cts_name = ctf_strdup(fp->ctf_symtab.cts_name);
872 if (fp->ctf_strtab.cts_name != NULL)
873 fp->ctf_strtab.cts_name = ctf_strdup(fp->ctf_strtab.cts_name);
874
875 if (fp->ctf_data.cts_name == NULL)
876 fp->ctf_data.cts_name = __UNCONST(_CTF_NULLSTR);
877 if (fp->ctf_symtab.cts_name == NULL)
878 fp->ctf_symtab.cts_name = __UNCONST(_CTF_NULLSTR);
879 if (fp->ctf_strtab.cts_name == NULL)
880 fp->ctf_strtab.cts_name = __UNCONST(_CTF_NULLSTR);
881
882 fp->ctf_str[CTF_STRTAB_0].cts_strs = (const char *)buf + hp.cth_stroff;
883 fp->ctf_str[CTF_STRTAB_0].cts_len = hp.cth_strlen;
884
885 if (strsect != NULL) {
886 fp->ctf_str[CTF_STRTAB_1].cts_strs = strsect->cts_data;
887 fp->ctf_str[CTF_STRTAB_1].cts_len = strsect->cts_size;
888 }
889
890 fp->ctf_base = base;
891 fp->ctf_buf = buf;
892 fp->ctf_size = size + hdrsz;
893
894 /*
895 * If we have a parent container name and label, store the relocated
896 * string pointers in the CTF container for easy access later.
897 */
898 if (hp.cth_parlabel != 0)
899 fp->ctf_parlabel = ctf_strptr(fp, hp.cth_parlabel);
900 if (hp.cth_parname != 0)
901 fp->ctf_parname = ctf_strptr(fp, hp.cth_parname);
902
903 ctf_dprintf("ctf_bufopen: parent name %s (label %s)\n",
904 fp->ctf_parname ? fp->ctf_parname : "<NULL>",
905 fp->ctf_parlabel ? fp->ctf_parlabel : "<NULL>");
906
907 /*
908 * If we have a symbol table section, allocate and initialize
909 * the symtab translation table, pointed to by ctf_sxlate.
910 */
911 if (symsect != NULL) {
912 fp->ctf_nsyms = symsect->cts_size / symsect->cts_entsize;
913 fp->ctf_sxlate = ctf_alloc(fp->ctf_nsyms * sizeof (uint_t));
914
915 if (fp->ctf_sxlate == NULL) {
916 (void) ctf_set_open_errno(errp, EAGAIN);
917 goto bad;
918 }
919
920 if ((err = init_symtab(fp, &hp, symsect, strsect)) != 0) {
921 (void) ctf_set_open_errno(errp, err);
922 goto bad;
923 }
924 }
925
926 if ((err = init_types(fp, &hp)) != 0) {
927 (void) ctf_set_open_errno(errp, err);
928 goto bad;
929 }
930
931 /*
932 * Initialize the ctf_lookup_by_name top-level dictionary. We keep an
933 * array of type name prefixes and the corresponding ctf_hash to use.
934 * NOTE: This code must be kept in sync with the code in ctf_update().
935 */
936 fp->ctf_lookups[0].ctl_prefix = "struct";
937 fp->ctf_lookups[0].ctl_len = strlen(fp->ctf_lookups[0].ctl_prefix);
938 fp->ctf_lookups[0].ctl_hash = &fp->ctf_structs;
939 fp->ctf_lookups[1].ctl_prefix = "union";
940 fp->ctf_lookups[1].ctl_len = strlen(fp->ctf_lookups[1].ctl_prefix);
941 fp->ctf_lookups[1].ctl_hash = &fp->ctf_unions;
942 fp->ctf_lookups[2].ctl_prefix = "enum";
943 fp->ctf_lookups[2].ctl_len = strlen(fp->ctf_lookups[2].ctl_prefix);
944 fp->ctf_lookups[2].ctl_hash = &fp->ctf_enums;
945 fp->ctf_lookups[3].ctl_prefix = _CTF_NULLSTR;
946 fp->ctf_lookups[3].ctl_len = strlen(fp->ctf_lookups[3].ctl_prefix);
947 fp->ctf_lookups[3].ctl_hash = &fp->ctf_names;
948 fp->ctf_lookups[4].ctl_prefix = NULL;
949 fp->ctf_lookups[4].ctl_len = 0;
950 fp->ctf_lookups[4].ctl_hash = NULL;
951
952 if (symsect != NULL) {
953 if (symsect->cts_entsize == sizeof (Elf64_Sym))
954 (void) ctf_setmodel(fp, CTF_MODEL_LP64);
955 else
956 (void) ctf_setmodel(fp, CTF_MODEL_ILP32);
957 } else
958 (void) ctf_setmodel(fp, CTF_MODEL_NATIVE);
959
960 fp->ctf_refcnt = 1;
961 return (fp);
962
963 bad:
964 ctf_close(fp);
965 return (NULL);
966 }
967
968 /*
969 * Dupliate a ctf_file_t and its underlying section information into a new
970 * container. This works by copying the three ctf_sect_t's of the original
971 * container if they exist and passing those into ctf_bufopen. To copy those, we
972 * mmap anonymous memory with ctf_data_alloc and memcpy the data across. It's not
973 * the cheapest thing, but it's what we've got.
974 */
975 ctf_file_t *
ctf_dup(ctf_file_t * ofp)976 ctf_dup(ctf_file_t *ofp)
977 {
978 ctf_file_t *fp;
979 ctf_sect_t ctfsect, symsect, strsect;
980 ctf_sect_t *ctp, *symp, *strp;
981 void *cbuf, *symbuf, *strbuf;
982 int err;
983
984 cbuf = symbuf = strbuf = NULL;
985 /*
986 * The ctfsect isn't allowed to not exist, but the symbol and string
987 * section might not. We only need to copy the data of the section, not
988 * the name, as ctf_bufopen will take care of that.
989 */
990 memcpy(&ctfsect, &ofp->ctf_data, sizeof (ctf_sect_t));
991 cbuf = ctf_data_alloc(ctfsect.cts_size);
992 if (cbuf == MAP_FAILED) {
993 (void) ctf_set_errno(ofp, ECTF_MMAP);
994 return (NULL);
995 }
996
997 memcpy(cbuf, ctfsect.cts_data, ctfsect.cts_size);
998 ctf_data_protect(cbuf, ctfsect.cts_size);
999 ctfsect.cts_data = cbuf;
1000 ctfsect.cts_offset = 0;
1001 ctp = &ctfsect;
1002
1003 if (ofp->ctf_symtab.cts_data != NULL) {
1004 memcpy(&symsect, &ofp->ctf_symtab, sizeof (ctf_sect_t));
1005 symbuf = ctf_data_alloc(symsect.cts_size);
1006 if (symbuf == MAP_FAILED) {
1007 (void) ctf_set_errno(ofp, ECTF_MMAP);
1008 goto err;
1009 }
1010 memcpy(symbuf, symsect.cts_data, symsect.cts_size);
1011 ctf_data_protect(symbuf, symsect.cts_size);
1012 symsect.cts_data = symbuf;
1013 symsect.cts_offset = 0;
1014 symp = &symsect;
1015 } else {
1016 symp = NULL;
1017 }
1018
1019 if (ofp->ctf_strtab.cts_data != NULL) {
1020 memcpy(&strsect, &ofp->ctf_strtab, sizeof (ctf_sect_t));
1021 strbuf = ctf_data_alloc(strsect.cts_size);
1022 if (strbuf == MAP_FAILED) {
1023 (void) ctf_set_errno(ofp, ECTF_MMAP);
1024 goto err;
1025 }
1026 memcpy(strbuf, strsect.cts_data, strsect.cts_size);
1027 ctf_data_protect(strbuf, strsect.cts_size);
1028 strsect.cts_data = strbuf;
1029 strsect.cts_offset = 0;
1030 strp = &strsect;
1031 } else {
1032 strp = NULL;
1033 }
1034
1035 fp = ctf_bufopen(ctp, symp, strp, &err);
1036 if (fp == NULL) {
1037 (void) ctf_set_errno(ofp, err);
1038 goto err;
1039 }
1040
1041 fp->ctf_flags |= LCTF_MMAP;
1042
1043 return (fp);
1044
1045 err:
1046 ctf_data_free(cbuf, ctfsect.cts_size);
1047 if (symbuf != NULL)
1048 ctf_data_free(symbuf, symsect.cts_size);
1049 if (strbuf != NULL)
1050 ctf_data_free(strbuf, strsect.cts_size);
1051 return (NULL);
1052 }
1053
1054 /*
1055 * Close the specified CTF container and free associated data structures. Note
1056 * that ctf_close() is a reference counted operation: if the specified file is
1057 * the parent of other active containers, its reference count will be greater
1058 * than one and it will be freed later when no active children exist.
1059 */
1060 void
ctf_close(ctf_file_t * fp)1061 ctf_close(ctf_file_t *fp)
1062 {
1063 ctf_dtdef_t *dtd, *ntd;
1064
1065 if (fp == NULL)
1066 return; /* allow ctf_close(NULL) to simplify caller code */
1067
1068 ctf_dprintf("ctf_close(%p) refcnt=%u\n", (void *)fp, fp->ctf_refcnt);
1069
1070 if (fp->ctf_refcnt > 1) {
1071 fp->ctf_refcnt--;
1072 return;
1073 }
1074
1075 if (fp->ctf_parent != NULL)
1076 ctf_close(fp->ctf_parent);
1077
1078 /*
1079 * Note, to work properly with reference counting on the dynamic
1080 * section, we must delete the list in reverse.
1081 */
1082 for (dtd = ctf_list_prev(&fp->ctf_dtdefs); dtd != NULL; dtd = ntd) {
1083 ntd = ctf_list_prev(dtd);
1084 ctf_dtd_delete(fp, dtd);
1085 }
1086
1087 ctf_free(fp->ctf_dthash, fp->ctf_dthashlen * sizeof (ctf_dtdef_t *));
1088
1089 if (fp->ctf_flags & LCTF_MMAP) {
1090 if (fp->ctf_data.cts_data != NULL)
1091 ctf_sect_munmap(&fp->ctf_data);
1092 if (fp->ctf_symtab.cts_data != NULL)
1093 ctf_sect_munmap(&fp->ctf_symtab);
1094 if (fp->ctf_strtab.cts_data != NULL)
1095 ctf_sect_munmap(&fp->ctf_strtab);
1096 }
1097
1098 if (fp->ctf_data.cts_name != _CTF_NULLSTR &&
1099 fp->ctf_data.cts_name != NULL) {
1100 ctf_free(__UNCONST(fp->ctf_data.cts_name),
1101 strlen(fp->ctf_data.cts_name) + 1);
1102 }
1103
1104 if (fp->ctf_symtab.cts_name != _CTF_NULLSTR &&
1105 fp->ctf_symtab.cts_name != NULL) {
1106 ctf_free(__UNCONST(fp->ctf_symtab.cts_name),
1107 strlen(fp->ctf_symtab.cts_name) + 1);
1108 }
1109
1110 if (fp->ctf_strtab.cts_name != _CTF_NULLSTR &&
1111 fp->ctf_strtab.cts_name != NULL) {
1112 ctf_free(__UNCONST(fp->ctf_strtab.cts_name),
1113 strlen(fp->ctf_strtab.cts_name) + 1);
1114 }
1115
1116 if (fp->ctf_base != fp->ctf_data.cts_data && fp->ctf_base != NULL)
1117 ctf_data_free(__UNCONST(fp->ctf_base), fp->ctf_size);
1118
1119 if (fp->ctf_sxlate != NULL)
1120 ctf_free(fp->ctf_sxlate, sizeof (uint_t) * fp->ctf_nsyms);
1121
1122 if (fp->ctf_txlate != NULL) {
1123 ctf_free(fp->ctf_txlate,
1124 sizeof (uint_t) * (fp->ctf_typemax + 1));
1125 }
1126
1127 if (fp->ctf_ptrtab != NULL) {
1128 ctf_free(fp->ctf_ptrtab,
1129 sizeof (uint_t) * (fp->ctf_typemax + 1));
1130 }
1131
1132 ctf_hash_destroy(&fp->ctf_structs);
1133 ctf_hash_destroy(&fp->ctf_unions);
1134 ctf_hash_destroy(&fp->ctf_enums);
1135 ctf_hash_destroy(&fp->ctf_names);
1136
1137 ctf_free(fp, sizeof (ctf_file_t));
1138 }
1139
1140 /*
1141 * Return the CTF handle for the parent CTF container, if one exists.
1142 * Otherwise return NULL to indicate this container has no imported parent.
1143 */
1144 ctf_file_t *
ctf_parent_file(ctf_file_t * fp)1145 ctf_parent_file(ctf_file_t *fp)
1146 {
1147 return (fp->ctf_parent);
1148 }
1149
1150 /*
1151 * Return the name of the parent CTF container, if one exists. Otherwise
1152 * return NULL to indicate this container is a root container.
1153 */
1154 const char *
ctf_parent_name(ctf_file_t * fp)1155 ctf_parent_name(ctf_file_t *fp)
1156 {
1157 return (fp->ctf_parname);
1158 }
1159
1160 /*
1161 * Import the types from the specified parent container by storing a pointer
1162 * to it in ctf_parent and incrementing its reference count. Only one parent
1163 * is allowed: if a parent already exists, it is replaced by the new parent.
1164 */
1165 int
ctf_import(ctf_file_t * fp,ctf_file_t * pfp)1166 ctf_import(ctf_file_t *fp, ctf_file_t *pfp)
1167 {
1168 if (fp == NULL || fp == pfp || (pfp != NULL && pfp->ctf_refcnt == 0))
1169 return (ctf_set_errno(fp, EINVAL));
1170
1171 if (pfp != NULL && pfp->ctf_dmodel != fp->ctf_dmodel)
1172 return (ctf_set_errno(fp, ECTF_DMODEL));
1173
1174 if (fp->ctf_parent != NULL)
1175 ctf_close(fp->ctf_parent);
1176
1177 if (pfp != NULL) {
1178 fp->ctf_flags |= LCTF_CHILD;
1179 pfp->ctf_refcnt++;
1180 }
1181
1182 fp->ctf_parent = pfp;
1183 return (0);
1184 }
1185
1186 /*
1187 * Set the data model constant for the CTF container.
1188 */
1189 int
ctf_setmodel(ctf_file_t * fp,int model)1190 ctf_setmodel(ctf_file_t *fp, int model)
1191 {
1192 const ctf_dmodel_t *dp;
1193
1194 for (dp = _libctf_models; dp->ctd_name != NULL; dp++) {
1195 if (dp->ctd_code == model) {
1196 fp->ctf_dmodel = dp;
1197 return (0);
1198 }
1199 }
1200
1201 return (ctf_set_errno(fp, EINVAL));
1202 }
1203
1204 /*
1205 * Return the data model constant for the CTF container.
1206 */
1207 int
ctf_getmodel(ctf_file_t * fp)1208 ctf_getmodel(ctf_file_t *fp)
1209 {
1210 return (fp->ctf_dmodel->ctd_code);
1211 }
1212
1213 void
ctf_setspecific(ctf_file_t * fp,void * data)1214 ctf_setspecific(ctf_file_t *fp, void *data)
1215 {
1216 fp->ctf_specific = data;
1217 }
1218
1219 void *
ctf_getspecific(ctf_file_t * fp)1220 ctf_getspecific(ctf_file_t *fp)
1221 {
1222 return (fp->ctf_specific);
1223 }
1224