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
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.
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]
24 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
25 * Use is subject to license terms.
28 * Copyright (c) 2013, Joyent, Inc. All rights reserved.
35 static const ctf_dmodel_t _libctf_models[] = {
36 { "ILP32", CTF_MODEL_ILP32, 4, 1, 2, 4, 4 },
37 { "LP64", CTF_MODEL_LP64, 8, 1, 2, 4, 8 },
38 { NULL, 0, 0, 0, 0, 0, 0 }
41 const char _CTF_SECTION[] = ".SUNW_ctf";
42 const char _CTF_NULLSTR[] = "";
44 int _libctf_version = CTF_VERSION; /* library client version */
45 int _libctf_debug = 0; /* debugging messages enabled */
48 get_kind_v2(uint_t info)
50 return (CTF_V2_INFO_KIND((ushort_t)info));
54 get_root_v2(uint_t info)
56 return (CTF_V2_INFO_ISROOT((ushort_t)info));
60 get_vlen_v2(uint_t info)
62 return (CTF_V2_INFO_VLEN((ushort_t)info));
68 return (CTF_V2_MAX_VLEN);
74 return (CTF_V2_MAX_SIZE);
80 return (CTF_V2_MAX_TYPE);
84 get_lsize_sent_v2(void)
86 return (CTF_V2_LSIZE_SENT);
90 get_lstruct_thresh_v2(void)
92 return (CTF_V2_LSTRUCT_THRESH);
96 type_info_v2(uint_t kind, uint_t isroot, uint_t len)
98 return (CTF_V2_TYPE_INFO(kind, isroot, len));
102 type_isparent_v2(uint_t id)
104 return (CTF_V2_TYPE_ISPARENT(id));
108 type_ischild_v2(uint_t id)
110 return (CTF_V2_TYPE_ISCHILD(id));
114 type_to_index_v2(uint_t t)
116 return (CTF_V2_TYPE_TO_INDEX(t));
120 index_to_type_v2(uint_t id, uint_t child)
122 return (CTF_V2_INDEX_TO_TYPE(id, child));
126 get_kind_v3(uint_t info)
128 return (CTF_V3_INFO_KIND(info));
132 get_root_v3(uint_t info)
134 return (CTF_V3_INFO_ISROOT(info));
138 get_vlen_v3(uint_t info)
140 return (CTF_V3_INFO_VLEN(info));
144 get_max_vlen_v3(void)
146 return (CTF_V3_MAX_VLEN);
150 get_max_size_v3(void)
152 return (CTF_V3_MAX_SIZE);
156 get_max_type_v3(void)
158 return (CTF_V3_MAX_TYPE);
162 get_lsize_sent_v3(void)
164 return (CTF_V3_LSIZE_SENT);
168 get_lstruct_thresh_v3(void)
170 return (CTF_V3_LSTRUCT_THRESH);
174 type_info_v3(uint_t kind, uint_t isroot, uint_t len)
176 return (CTF_V3_TYPE_INFO(kind, isroot, len));
180 type_isparent_v3(uint_t id)
182 return (CTF_V3_TYPE_ISPARENT(id));
186 type_ischild_v3(uint_t id)
188 return (CTF_V3_TYPE_ISCHILD(id));
192 type_to_index_v3(uint_t t)
194 return (CTF_V3_TYPE_TO_INDEX(t));
198 index_to_type_v3(uint_t id, uint_t child)
200 return (CTF_V3_INDEX_TO_TYPE(id, child));
203 #define CTF_FILEOPS_ENTRY(v) \
205 .ctfo_get_kind = get_kind_v ## v, \
206 .ctfo_get_root = get_root_v ## v, \
207 .ctfo_get_vlen = get_vlen_v ## v, \
208 .ctfo_get_max_vlen = get_max_vlen_v ## v, \
209 .ctfo_get_max_size = get_max_size_v ## v, \
210 .ctfo_get_max_type = get_max_type_v ## v, \
211 .ctfo_get_lsize_sent = get_lsize_sent_v ## v, \
212 .ctfo_get_lstruct_thresh = get_lstruct_thresh_v ## v, \
213 .ctfo_type_info = type_info_v ## v, \
214 .ctfo_type_isparent = type_isparent_v ## v, \
215 .ctfo_type_ischild = type_ischild_v ## v, \
216 .ctfo_type_to_index = type_to_index_v ## v, \
217 .ctfo_index_to_type = index_to_type_v ## v \
220 static const ctf_fileops_t ctf_fileops[] = {
223 CTF_FILEOPS_ENTRY(2),
224 CTF_FILEOPS_ENTRY(3),
228 * Convert a 32-bit ELF symbol into GElf (Elf64) and return a pointer to it.
231 sym_to_gelf(const Elf32_Sym *src, Elf64_Sym *dst)
233 dst->st_name = src->st_name;
234 dst->st_value = src->st_value;
235 dst->st_size = src->st_size;
236 dst->st_info = src->st_info;
237 dst->st_other = src->st_other;
238 dst->st_shndx = src->st_shndx;
244 * Initialize the symtab translation table by filling each entry with the
245 * offset of the CTF type or function data corresponding to each STT_FUNC or
246 * STT_OBJECT entry in the symbol table.
249 init_symtab(ctf_file_t *fp, const ctf_header_t *hp,
250 const ctf_sect_t *sp, const ctf_sect_t *strp)
252 const uchar_t *symp = sp->cts_data;
253 uint_t *xp = fp->ctf_sxlate;
254 uint_t *xend = xp + fp->ctf_nsyms;
256 uint_t objtoff = hp->cth_objtoff;
257 uint_t funcoff = hp->cth_funcoff;
265 * The CTF data object and function type sections are ordered to match
266 * the relative order of the respective symbol types in the symtab.
267 * If no type information is available for a symbol table entry, a
268 * pad is inserted in the CTF section. As a further optimization,
269 * anonymous or undefined symbols are omitted from the CTF data.
271 for (; xp < xend; xp++, symp += sp->cts_entsize) {
272 if (sp->cts_entsize == sizeof (Elf32_Sym))
273 gsp = sym_to_gelf((Elf32_Sym *)(uintptr_t)symp, &sym);
275 gsp = (Elf64_Sym *)(uintptr_t)symp;
277 if (gsp->st_name < strp->cts_size)
278 name = (const char *)strp->cts_data + gsp->st_name;
282 if (gsp->st_name == 0 || gsp->st_shndx == SHN_UNDEF ||
283 strcmp(name, "_START_") == 0 ||
284 strcmp(name, "_END_") == 0) {
289 switch (ELF64_ST_TYPE(gsp->st_info)) {
291 if (objtoff >= hp->cth_funcoff ||
292 (gsp->st_shndx == SHN_ABS && gsp->st_value == 0)) {
298 objtoff += fp->ctf_idwidth;
302 if (funcoff >= hp->cth_typeoff) {
309 info = *(uint_t *)((uintptr_t)fp->ctf_buf + funcoff);
310 vlen = LCTF_INFO_VLEN(fp, info);
313 * If we encounter a zero pad at the end, just skip it.
314 * Otherwise skip over the function and its return type
315 * (+2) and the argument list (vlen).
317 if (LCTF_INFO_KIND(fp, info) == CTF_K_UNKNOWN &&
319 funcoff += fp->ctf_idwidth;
322 roundup2(fp->ctf_idwidth * (vlen + 2), 4);
331 ctf_dprintf("loaded %lu symtab entries\n", fp->ctf_nsyms);
336 * Initialize the type ID translation table with the byte offset of each type,
337 * and initialize the hash tables of each named type.
340 init_types(ctf_file_t *fp, const ctf_header_t *cth)
342 const void *tbuf = (const void *)(fp->ctf_buf + cth->cth_typeoff);
343 const void *tend = (const void *)(fp->ctf_buf + cth->cth_stroff);
345 ulong_t pop[CTF_K_MAX + 1] = { 0 };
352 * We initially determine whether the container is a child or a parent
353 * based on the value of cth_parname. To support containers that pre-
354 * date cth_parname, we also scan the types themselves for references
355 * to values in the range reserved for child types in our first pass.
357 int child = cth->cth_parname != 0;
358 int nlstructs = 0, nlunions = 0;
362 * We make two passes through the entire type section. In this first
363 * pass, we count the number of each type and the total number of types.
365 for (tp = tbuf; tp < tend; fp->ctf_typemax++) {
366 ssize_t size, increment;
369 uint_t kind, n, type, vlen;
371 (void) ctf_get_ctt_size(fp, tp, &size, &increment);
372 ctf_get_ctt_info(fp, tp, &kind, &vlen, NULL);
373 ctf_get_ctt_index(fp, tp, NULL, &type, NULL);
378 vbytes = sizeof (uint_t);
381 if (fp->ctf_version == CTF_VERSION_2)
382 vbytes = sizeof (struct ctf_array_v2);
384 vbytes = sizeof (struct ctf_array_v3);
387 vbytes = roundup2(fp->ctf_idwidth * vlen, 4);
394 (const void *)((uintptr_t)tp + increment);
397 for (n = vlen; n != 0; n--, mp += increment1) {
398 ctf_get_ctm_info(fp, mp, size, &increment1, &type,
400 child |= LCTF_TYPE_ISCHILD(fp, type);
401 vbytes += increment1;
406 vbytes = sizeof (ctf_enum_t) * vlen;
410 * For forward declarations, ctt_type is the CTF_K_*
411 * kind for the tag, so bump that population count too.
412 * If ctt_type is unknown, treat the tag as a struct.
414 if (type == CTF_K_UNKNOWN || type >= CTF_K_MAX)
427 child |= LCTF_TYPE_ISCHILD(fp, type);
431 ctf_dprintf("detected invalid CTF kind -- %u\n", kind);
432 return (ECTF_CORRUPT);
434 tp = (const void *)((uintptr_t)tp + increment + vbytes);
439 * If we detected a reference to a child type ID, then we know this
440 * container is a child and may have a parent's types imported later.
443 ctf_dprintf("CTF container %p is a child\n", (void *)fp);
444 fp->ctf_flags |= LCTF_CHILD;
446 ctf_dprintf("CTF container %p is a parent\n", (void *)fp);
449 * Now that we've counted up the number of each type, we can allocate
450 * the hash tables, type translation table, and pointer table.
452 if ((err = ctf_hash_create(&fp->ctf_structs, pop[CTF_K_STRUCT])) != 0)
455 if ((err = ctf_hash_create(&fp->ctf_unions, pop[CTF_K_UNION])) != 0)
458 if ((err = ctf_hash_create(&fp->ctf_enums, pop[CTF_K_ENUM])) != 0)
461 if ((err = ctf_hash_create(&fp->ctf_names,
462 pop[CTF_K_INTEGER] + pop[CTF_K_FLOAT] + pop[CTF_K_FUNCTION] +
463 pop[CTF_K_TYPEDEF] + pop[CTF_K_POINTER] + pop[CTF_K_VOLATILE] +
464 pop[CTF_K_CONST] + pop[CTF_K_RESTRICT])) != 0)
467 fp->ctf_txlate = ctf_alloc(sizeof (uint_t) * (fp->ctf_typemax + 1));
468 fp->ctf_ptrtab = ctf_alloc(sizeof (uint_t) * (fp->ctf_typemax + 1));
470 if (fp->ctf_txlate == NULL || fp->ctf_ptrtab == NULL)
471 return (EAGAIN); /* memory allocation failed */
474 *xp++ = 0; /* type id 0 is used as a sentinel value */
476 bzero(fp->ctf_txlate, sizeof (uint_t) * (fp->ctf_typemax + 1));
477 bzero(fp->ctf_ptrtab, sizeof (uint_t) * (fp->ctf_typemax + 1));
480 * In the second pass through the types, we fill in each entry of the
481 * type and pointer tables and add names to the appropriate hashes.
483 for (id = 1, tp = tbuf; tp < tend; xp++, id++) {
484 const struct ctf_type_v3 *ctt = tp;
485 uint_t kind, type, vlen;
486 ssize_t size, increment;
493 (void) ctf_get_ctt_size(fp, tp, &size, &increment);
494 ctf_get_ctt_info(fp, tp, &kind, &vlen, NULL);
495 ctf_get_ctt_index(fp, tp, NULL, &type, NULL);
496 name = ctf_type_rname(fp, tp);
502 * Only insert a new integer base type definition if
503 * this type name has not been defined yet. We re-use
504 * the names with different encodings for bit-fields.
506 if ((hep = ctf_hash_lookup(&fp->ctf_names, fp,
507 name, strlen(name))) == NULL) {
508 err = ctf_hash_insert(&fp->ctf_names, fp,
509 LCTF_INDEX_TO_TYPE(fp, id, child),
511 if (err != 0 && err != ECTF_STRTAB)
513 } else if (ctf_type_encoding(fp, hep->h_type,
514 &cte) == 0 && cte.cte_bits == 0) {
516 * Work-around SOS8 stabs bug: replace existing
517 * intrinsic w/ same name if it was zero bits.
519 hep->h_type = LCTF_INDEX_TO_TYPE(fp, id, child);
521 vbytes = sizeof (uint_t);
525 if (fp->ctf_version == CTF_VERSION_2)
526 vbytes = sizeof (struct ctf_array_v2);
528 vbytes = sizeof (struct ctf_array_v3);
532 err = ctf_hash_insert(&fp->ctf_names, fp,
533 LCTF_INDEX_TO_TYPE(fp, id, child), ctt->ctt_name);
534 if (err != 0 && err != ECTF_STRTAB)
536 vbytes = roundup2(fp->ctf_idwidth * vlen, 4);
540 err = ctf_hash_define(&fp->ctf_structs, fp,
541 LCTF_INDEX_TO_TYPE(fp, id, child), ctt->ctt_name);
543 if (err != 0 && err != ECTF_STRTAB)
546 if (fp->ctf_version == CTF_VERSION_2) {
547 if (size < LCTF_LSTRUCT_THRESH(fp))
548 vbytes = sizeof (struct ctf_member_v2) *
552 sizeof (struct ctf_lmember_v2) *
557 if (size < LCTF_LSTRUCT_THRESH(fp))
558 vbytes = sizeof (struct ctf_member_v3) *
562 sizeof (struct ctf_lmember_v3) *
570 err = ctf_hash_define(&fp->ctf_unions, fp,
571 LCTF_INDEX_TO_TYPE(fp, id, child), ctt->ctt_name);
573 if (err != 0 && err != ECTF_STRTAB)
576 if (fp->ctf_version == CTF_VERSION_2) {
577 if (size < LCTF_LSTRUCT_THRESH(fp))
578 vbytes = sizeof (struct ctf_member_v2) *
582 sizeof (struct ctf_lmember_v2) *
587 if (size < LCTF_LSTRUCT_THRESH(fp))
588 vbytes = sizeof (struct ctf_member_v3) *
592 sizeof (struct ctf_lmember_v3) *
600 err = ctf_hash_define(&fp->ctf_enums, fp,
601 LCTF_INDEX_TO_TYPE(fp, id, child), ctt->ctt_name);
603 if (err != 0 && err != ECTF_STRTAB)
606 vbytes = sizeof (ctf_enum_t) * vlen;
610 err = ctf_hash_insert(&fp->ctf_names, fp,
611 LCTF_INDEX_TO_TYPE(fp, id, child), ctt->ctt_name);
612 if (err != 0 && err != ECTF_STRTAB)
619 * Only insert forward tags into the given hash if the
620 * type or tag name is not already present.
624 hp = &fp->ctf_structs;
627 hp = &fp->ctf_unions;
633 hp = &fp->ctf_structs;
636 if (ctf_hash_lookup(hp, fp,
637 name, strlen(name)) == NULL) {
638 err = ctf_hash_insert(hp, fp,
639 LCTF_INDEX_TO_TYPE(fp, id, child),
641 if (err != 0 && err != ECTF_STRTAB)
649 * If the type referenced by the pointer is in this CTF
650 * container, then store the index of the pointer type
651 * in fp->ctf_ptrtab[ index of referenced type ].
653 if (LCTF_TYPE_ISCHILD(fp, type) == child &&
654 LCTF_TYPE_TO_INDEX(fp, type) <= fp->ctf_typemax)
656 LCTF_TYPE_TO_INDEX(fp, type)] = id;
662 err = ctf_hash_insert(&fp->ctf_names, fp,
663 LCTF_INDEX_TO_TYPE(fp, id, child), ctt->ctt_name);
664 if (err != 0 && err != ECTF_STRTAB)
673 *xp = (uint_t)((uintptr_t)tp - (uintptr_t)fp->ctf_buf);
674 tp = (const void *)((uintptr_t)tp + increment + vbytes);
677 ctf_dprintf("%lu total types processed\n", fp->ctf_typemax);
678 ctf_dprintf("%u enum names hashed\n", ctf_hash_size(&fp->ctf_enums));
679 ctf_dprintf("%u struct names hashed (%d long)\n",
680 ctf_hash_size(&fp->ctf_structs), nlstructs);
681 ctf_dprintf("%u union names hashed (%d long)\n",
682 ctf_hash_size(&fp->ctf_unions), nlunions);
683 ctf_dprintf("%u base type names hashed\n",
684 ctf_hash_size(&fp->ctf_names));
687 * Make an additional pass through the pointer table to find pointers
688 * that point to anonymous typedef nodes. If we find one, modify the
689 * pointer table so that the pointer is also known to point to the
690 * node that is referenced by the anonymous typedef node.
692 for (id = 1; id <= fp->ctf_typemax; id++) {
693 if ((dst = fp->ctf_ptrtab[id]) != 0) {
697 tp = LCTF_INDEX_TO_TYPEPTR(fp, id);
698 ctf_get_ctt_info(fp, tp, &kind, NULL, NULL);
699 ctf_get_ctt_index(fp, tp, &index, NULL, &ischild);
701 if (kind == CTF_K_TYPEDEF &&
702 strcmp(ctf_type_rname(fp, tp), "") == 0 &&
703 ischild == child && index <= fp->ctf_typemax)
704 fp->ctf_ptrtab[index] = dst;
712 * Decode the specified CTF buffer and optional symbol table and create a new
713 * CTF container representing the symbolic debugging information. This code
714 * can be used directly by the debugger, or it can be used as the engine for
715 * ctf_fdopen() or ctf_open(), below.
718 ctf_bufopen(const ctf_sect_t *ctfsect, const ctf_sect_t *symsect,
719 const ctf_sect_t *strsect, int *errp)
721 const ctf_preamble_t *pp;
728 if (ctfsect == NULL || ((symsect == NULL) != (strsect == NULL)))
729 return (ctf_set_open_errno(errp, EINVAL));
731 if (symsect != NULL && symsect->cts_entsize != sizeof (Elf32_Sym) &&
732 symsect->cts_entsize != sizeof (Elf64_Sym))
733 return (ctf_set_open_errno(errp, ECTF_SYMTAB));
735 if (symsect != NULL && symsect->cts_data == NULL)
736 return (ctf_set_open_errno(errp, ECTF_SYMBAD));
738 if (strsect != NULL && strsect->cts_data == NULL)
739 return (ctf_set_open_errno(errp, ECTF_STRBAD));
741 if (ctfsect->cts_size < sizeof (ctf_preamble_t))
742 return (ctf_set_open_errno(errp, ECTF_NOCTFBUF));
744 pp = (const ctf_preamble_t *)ctfsect->cts_data;
746 ctf_dprintf("ctf_bufopen: magic=0x%x version=%u\n",
747 pp->ctp_magic, pp->ctp_version);
750 * Validate each part of the CTF header (either V1 or V2).
751 * First, we validate the preamble (common to all versions). At that
752 * point, we know specific header version, and can validate the
753 * version-specific parts including section offsets and alignments.
755 if (pp->ctp_magic != CTF_MAGIC)
756 return (ctf_set_open_errno(errp, ECTF_NOCTFBUF));
758 if (pp->ctp_version == CTF_VERSION_2 ||
759 pp->ctp_version == CTF_VERSION_3) {
760 if (ctfsect->cts_size < sizeof (ctf_header_t))
761 return (ctf_set_open_errno(errp, ECTF_NOCTFBUF));
763 bcopy(ctfsect->cts_data, &hp, sizeof (hp));
764 hdrsz = sizeof (ctf_header_t);
767 return (ctf_set_open_errno(errp, ECTF_CTFVERS));
769 size = hp.cth_stroff + hp.cth_strlen;
771 ctf_dprintf("ctf_bufopen: uncompressed size=%lu\n", (ulong_t)size);
773 if (hp.cth_lbloff > size || hp.cth_objtoff > size ||
774 hp.cth_funcoff > size || hp.cth_typeoff > size ||
775 hp.cth_stroff > size)
776 return (ctf_set_open_errno(errp, ECTF_CORRUPT));
778 if (hp.cth_lbloff > hp.cth_objtoff ||
779 hp.cth_objtoff > hp.cth_funcoff ||
780 hp.cth_funcoff > hp.cth_typeoff ||
781 hp.cth_typeoff > hp.cth_stroff)
782 return (ctf_set_open_errno(errp, ECTF_CORRUPT));
784 if ((hp.cth_lbloff & 3) || (hp.cth_objtoff & 1) ||
785 (hp.cth_funcoff & 1) || (hp.cth_typeoff & 3))
786 return (ctf_set_open_errno(errp, ECTF_CORRUPT));
789 * Once everything is determined to be valid, attempt to decompress
790 * the CTF data buffer if it is compressed. Otherwise we just put
791 * the data section's buffer pointer into ctf_buf, below.
793 if (hp.cth_flags & CTF_F_COMPRESS) {
794 size_t srclen, dstlen;
798 if (ctf_zopen(errp) == NULL)
799 return (NULL); /* errp is set for us */
801 if ((base = ctf_data_alloc(size + hdrsz)) == MAP_FAILED)
802 return (ctf_set_open_errno(errp, ECTF_ZALLOC));
804 bcopy(ctfsect->cts_data, base, hdrsz);
805 ((ctf_preamble_t *)base)->ctp_flags &= ~CTF_F_COMPRESS;
806 buf = (uchar_t *)base + hdrsz;
808 src = (uchar_t *)ctfsect->cts_data + hdrsz;
809 srclen = ctfsect->cts_size - hdrsz;
812 if ((rc = z_uncompress(buf, &dstlen, src, srclen)) != Z_OK) {
813 ctf_dprintf("zlib inflate err: %s\n", z_strerror(rc));
814 ctf_data_free(base, size + hdrsz);
815 return (ctf_set_open_errno(errp, ECTF_DECOMPRESS));
818 if (dstlen != size) {
819 ctf_dprintf("zlib inflate short -- got %lu of %lu "
820 "bytes\n", (ulong_t)dstlen, (ulong_t)size);
821 ctf_data_free(base, size + hdrsz);
822 return (ctf_set_open_errno(errp, ECTF_CORRUPT));
825 ctf_data_protect(base, size + hdrsz);
828 base = (void *)ctfsect->cts_data;
829 buf = (uchar_t *)base + hdrsz;
833 * Once we have uncompressed and validated the CTF data buffer, we can
834 * proceed with allocating a ctf_file_t and initializing it.
836 if ((fp = ctf_alloc(sizeof (ctf_file_t))) == NULL)
837 return (ctf_set_open_errno(errp, EAGAIN));
839 bzero(fp, sizeof (ctf_file_t));
840 fp->ctf_version = hp.cth_version;
841 fp->ctf_idwidth = fp->ctf_version == CTF_VERSION_2 ? 2 : 4;
842 fp->ctf_fileops = &ctf_fileops[hp.cth_version];
843 bcopy(ctfsect, &fp->ctf_data, sizeof (ctf_sect_t));
845 if (symsect != NULL) {
846 bcopy(symsect, &fp->ctf_symtab, sizeof (ctf_sect_t));
847 bcopy(strsect, &fp->ctf_strtab, sizeof (ctf_sect_t));
850 if (fp->ctf_data.cts_name != NULL)
851 fp->ctf_data.cts_name = ctf_strdup(fp->ctf_data.cts_name);
852 if (fp->ctf_symtab.cts_name != NULL)
853 fp->ctf_symtab.cts_name = ctf_strdup(fp->ctf_symtab.cts_name);
854 if (fp->ctf_strtab.cts_name != NULL)
855 fp->ctf_strtab.cts_name = ctf_strdup(fp->ctf_strtab.cts_name);
857 if (fp->ctf_data.cts_name == NULL)
858 fp->ctf_data.cts_name = _CTF_NULLSTR;
859 if (fp->ctf_symtab.cts_name == NULL)
860 fp->ctf_symtab.cts_name = _CTF_NULLSTR;
861 if (fp->ctf_strtab.cts_name == NULL)
862 fp->ctf_strtab.cts_name = _CTF_NULLSTR;
864 fp->ctf_str[CTF_STRTAB_0].cts_strs = (const char *)buf + hp.cth_stroff;
865 fp->ctf_str[CTF_STRTAB_0].cts_len = hp.cth_strlen;
867 if (strsect != NULL) {
868 fp->ctf_str[CTF_STRTAB_1].cts_strs = strsect->cts_data;
869 fp->ctf_str[CTF_STRTAB_1].cts_len = strsect->cts_size;
874 fp->ctf_size = size + hdrsz;
877 * If we have a parent container name and label, store the relocated
878 * string pointers in the CTF container for easy access later.
880 if (hp.cth_parlabel != 0)
881 fp->ctf_parlabel = ctf_strptr(fp, hp.cth_parlabel);
882 if (hp.cth_parname != 0)
883 fp->ctf_parname = ctf_strptr(fp, hp.cth_parname);
885 ctf_dprintf("ctf_bufopen: parent name %s (label %s)\n",
886 fp->ctf_parname ? fp->ctf_parname : "<NULL>",
887 fp->ctf_parlabel ? fp->ctf_parlabel : "<NULL>");
890 * If we have a symbol table section, allocate and initialize
891 * the symtab translation table, pointed to by ctf_sxlate.
893 if (symsect != NULL) {
894 fp->ctf_nsyms = symsect->cts_size / symsect->cts_entsize;
895 fp->ctf_sxlate = ctf_alloc(fp->ctf_nsyms * sizeof (uint_t));
897 if (fp->ctf_sxlate == NULL) {
898 (void) ctf_set_open_errno(errp, EAGAIN);
902 if ((err = init_symtab(fp, &hp, symsect, strsect)) != 0) {
903 (void) ctf_set_open_errno(errp, err);
908 if ((err = init_types(fp, &hp)) != 0) {
909 (void) ctf_set_open_errno(errp, err);
914 * Initialize the ctf_lookup_by_name top-level dictionary. We keep an
915 * array of type name prefixes and the corresponding ctf_hash to use.
916 * NOTE: This code must be kept in sync with the code in ctf_update().
918 fp->ctf_lookups[0].ctl_prefix = "struct";
919 fp->ctf_lookups[0].ctl_len = strlen(fp->ctf_lookups[0].ctl_prefix);
920 fp->ctf_lookups[0].ctl_hash = &fp->ctf_structs;
921 fp->ctf_lookups[1].ctl_prefix = "union";
922 fp->ctf_lookups[1].ctl_len = strlen(fp->ctf_lookups[1].ctl_prefix);
923 fp->ctf_lookups[1].ctl_hash = &fp->ctf_unions;
924 fp->ctf_lookups[2].ctl_prefix = "enum";
925 fp->ctf_lookups[2].ctl_len = strlen(fp->ctf_lookups[2].ctl_prefix);
926 fp->ctf_lookups[2].ctl_hash = &fp->ctf_enums;
927 fp->ctf_lookups[3].ctl_prefix = _CTF_NULLSTR;
928 fp->ctf_lookups[3].ctl_len = strlen(fp->ctf_lookups[3].ctl_prefix);
929 fp->ctf_lookups[3].ctl_hash = &fp->ctf_names;
930 fp->ctf_lookups[4].ctl_prefix = NULL;
931 fp->ctf_lookups[4].ctl_len = 0;
932 fp->ctf_lookups[4].ctl_hash = NULL;
934 if (symsect != NULL) {
935 if (symsect->cts_entsize == sizeof (Elf64_Sym))
936 (void) ctf_setmodel(fp, CTF_MODEL_LP64);
938 (void) ctf_setmodel(fp, CTF_MODEL_ILP32);
940 (void) ctf_setmodel(fp, CTF_MODEL_NATIVE);
951 * Dupliate a ctf_file_t and its underlying section information into a new
952 * container. This works by copying the three ctf_sect_t's of the original
953 * container if they exist and passing those into ctf_bufopen. To copy those, we
954 * mmap anonymous memory with ctf_data_alloc and bcopy the data across. It's not
955 * the cheapest thing, but it's what we've got.
958 ctf_dup(ctf_file_t *ofp)
961 ctf_sect_t ctfsect, symsect, strsect;
962 ctf_sect_t *ctp, *symp, *strp;
963 void *cbuf, *symbuf, *strbuf;
966 cbuf = symbuf = strbuf = NULL;
968 * The ctfsect isn't allowed to not exist, but the symbol and string
969 * section might not. We only need to copy the data of the section, not
970 * the name, as ctf_bufopen will take care of that.
972 bcopy(&ofp->ctf_data, &ctfsect, sizeof (ctf_sect_t));
973 cbuf = ctf_data_alloc(ctfsect.cts_size);
975 (void) ctf_set_errno(ofp, ECTF_MMAP);
979 bcopy(ctfsect.cts_data, cbuf, ctfsect.cts_size);
980 ctf_data_protect(cbuf, ctfsect.cts_size);
981 ctfsect.cts_data = cbuf;
982 ctfsect.cts_offset = 0;
985 if (ofp->ctf_symtab.cts_data != NULL) {
986 bcopy(&ofp->ctf_symtab, &symsect, sizeof (ctf_sect_t));
987 symbuf = ctf_data_alloc(symsect.cts_size);
988 if (symbuf == NULL) {
989 (void) ctf_set_errno(ofp, ECTF_MMAP);
992 bcopy(symsect.cts_data, symbuf, symsect.cts_size);
993 ctf_data_protect(symbuf, symsect.cts_size);
994 symsect.cts_data = symbuf;
995 symsect.cts_offset = 0;
1001 if (ofp->ctf_strtab.cts_data != NULL) {
1002 bcopy(&ofp->ctf_strtab, &strsect, sizeof (ctf_sect_t));
1003 strbuf = ctf_data_alloc(strsect.cts_size);
1004 if (strbuf == NULL) {
1005 (void) ctf_set_errno(ofp, ECTF_MMAP);
1008 bcopy(strsect.cts_data, strbuf, strsect.cts_size);
1009 ctf_data_protect(strbuf, strsect.cts_size);
1010 strsect.cts_data = strbuf;
1011 strsect.cts_offset = 0;
1017 fp = ctf_bufopen(ctp, symp, strp, &err);
1019 (void) ctf_set_errno(ofp, err);
1023 fp->ctf_flags |= LCTF_MMAP;
1028 ctf_data_free(cbuf, ctfsect.cts_size);
1030 ctf_data_free(symbuf, symsect.cts_size);
1032 ctf_data_free(strbuf, strsect.cts_size);
1037 * Close the specified CTF container and free associated data structures. Note
1038 * that ctf_close() is a reference counted operation: if the specified file is
1039 * the parent of other active containers, its reference count will be greater
1040 * than one and it will be freed later when no active children exist.
1043 ctf_close(ctf_file_t *fp)
1045 ctf_dtdef_t *dtd, *ntd;
1048 return; /* allow ctf_close(NULL) to simplify caller code */
1050 ctf_dprintf("ctf_close(%p) refcnt=%u\n", (void *)fp, fp->ctf_refcnt);
1052 if (fp->ctf_refcnt > 1) {
1057 if (fp->ctf_parent != NULL)
1058 ctf_close(fp->ctf_parent);
1061 * Note, to work properly with reference counting on the dynamic
1062 * section, we must delete the list in reverse.
1064 for (dtd = ctf_list_prev(&fp->ctf_dtdefs); dtd != NULL; dtd = ntd) {
1065 ntd = ctf_list_prev(dtd);
1066 ctf_dtd_delete(fp, dtd);
1069 ctf_free(fp->ctf_dthash, fp->ctf_dthashlen * sizeof (ctf_dtdef_t *));
1071 if (fp->ctf_flags & LCTF_MMAP) {
1072 if (fp->ctf_data.cts_data != NULL)
1073 ctf_sect_munmap(&fp->ctf_data);
1074 if (fp->ctf_symtab.cts_data != NULL)
1075 ctf_sect_munmap(&fp->ctf_symtab);
1076 if (fp->ctf_strtab.cts_data != NULL)
1077 ctf_sect_munmap(&fp->ctf_strtab);
1080 if (fp->ctf_data.cts_name != _CTF_NULLSTR &&
1081 fp->ctf_data.cts_name != NULL) {
1082 ctf_free((char *)fp->ctf_data.cts_name,
1083 strlen(fp->ctf_data.cts_name) + 1);
1086 if (fp->ctf_symtab.cts_name != _CTF_NULLSTR &&
1087 fp->ctf_symtab.cts_name != NULL) {
1088 ctf_free((char *)fp->ctf_symtab.cts_name,
1089 strlen(fp->ctf_symtab.cts_name) + 1);
1092 if (fp->ctf_strtab.cts_name != _CTF_NULLSTR &&
1093 fp->ctf_strtab.cts_name != NULL) {
1094 ctf_free((char *)fp->ctf_strtab.cts_name,
1095 strlen(fp->ctf_strtab.cts_name) + 1);
1098 if (fp->ctf_base != fp->ctf_data.cts_data && fp->ctf_base != NULL)
1099 ctf_data_free((void *)fp->ctf_base, fp->ctf_size);
1101 if (fp->ctf_sxlate != NULL)
1102 ctf_free(fp->ctf_sxlate, sizeof (uint_t) * fp->ctf_nsyms);
1104 if (fp->ctf_txlate != NULL) {
1105 ctf_free(fp->ctf_txlate,
1106 sizeof (uint_t) * (fp->ctf_typemax + 1));
1109 if (fp->ctf_ptrtab != NULL) {
1110 ctf_free(fp->ctf_ptrtab,
1111 sizeof (uint_t) * (fp->ctf_typemax + 1));
1114 ctf_hash_destroy(&fp->ctf_structs);
1115 ctf_hash_destroy(&fp->ctf_unions);
1116 ctf_hash_destroy(&fp->ctf_enums);
1117 ctf_hash_destroy(&fp->ctf_names);
1119 ctf_free(fp, sizeof (ctf_file_t));
1123 * Return the CTF handle for the parent CTF container, if one exists.
1124 * Otherwise return NULL to indicate this container has no imported parent.
1127 ctf_parent_file(ctf_file_t *fp)
1129 return (fp->ctf_parent);
1133 * Return the name of the parent CTF container, if one exists. Otherwise
1134 * return NULL to indicate this container is a root container.
1137 ctf_parent_name(ctf_file_t *fp)
1139 return (fp->ctf_parname);
1143 * Import the types from the specified parent container by storing a pointer
1144 * to it in ctf_parent and incrementing its reference count. Only one parent
1145 * is allowed: if a parent already exists, it is replaced by the new parent.
1148 ctf_import(ctf_file_t *fp, ctf_file_t *pfp)
1150 if (fp == NULL || fp == pfp || (pfp != NULL && pfp->ctf_refcnt == 0))
1151 return (ctf_set_errno(fp, EINVAL));
1153 if (pfp != NULL && pfp->ctf_dmodel != fp->ctf_dmodel)
1154 return (ctf_set_errno(fp, ECTF_DMODEL));
1156 if (fp->ctf_parent != NULL)
1157 ctf_close(fp->ctf_parent);
1160 fp->ctf_flags |= LCTF_CHILD;
1164 fp->ctf_parent = pfp;
1169 * Set the data model constant for the CTF container.
1172 ctf_setmodel(ctf_file_t *fp, int model)
1174 const ctf_dmodel_t *dp;
1176 for (dp = _libctf_models; dp->ctd_name != NULL; dp++) {
1177 if (dp->ctd_code == model) {
1178 fp->ctf_dmodel = dp;
1183 return (ctf_set_errno(fp, EINVAL));
1187 * Return the data model constant for the CTF container.
1190 ctf_getmodel(ctf_file_t *fp)
1192 return (fp->ctf_dmodel->ctd_code);
1196 ctf_setspecific(ctf_file_t *fp, void *data)
1198 fp->ctf_specific = data;
1202 ctf_getspecific(ctf_file_t *fp)
1204 return (fp->ctf_specific);