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.
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.
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]
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
27 * Create and parse buffers containing CTF data.
30 #include <sys/types.h>
38 #include "ctf_headers.h"
44 * Name of the file currently being read, used to print error messages. We
45 * assume that only one file will be read at a time, and thus make no attempt
46 * to allow curfile to be used simultaneously by multiple threads.
48 * The value is only valid during a call to ctf_load.
52 #define CTF_BUF_CHUNK_SIZE (64 * 1024)
53 #define RES_BUF_CHUNK_SIZE (64 * 1024)
56 strtab_t ctb_strtab; /* string table */
57 caddr_t ctb_base; /* pointer to base of buffer */
58 caddr_t ctb_end; /* pointer to end of buffer */
59 caddr_t ctb_ptr; /* pointer to empty buffer space */
60 size_t ctb_size; /* size of buffer */
61 int nptent; /* number of processed types */
62 int ntholes; /* number of type holes */
67 parseterminate(const char *fmt, ...)
69 static char msgbuf[1024]; /* sigh */
73 vsnprintf(msgbuf, sizeof (msgbuf), fmt, ap);
76 terminate("%s: %s\n", curfile, msgbuf);
80 ctf_buf_grow(ctf_buf_t *b)
82 off_t ptroff = b->ctb_ptr - b->ctb_base;
84 b->ctb_size += CTF_BUF_CHUNK_SIZE;
85 b->ctb_base = xrealloc(b->ctb_base, b->ctb_size);
86 b->ctb_end = b->ctb_base + b->ctb_size;
87 b->ctb_ptr = b->ctb_base + ptroff;
93 ctf_buf_t *b = xcalloc(sizeof (ctf_buf_t));
95 strtab_create(&b->ctb_strtab);
102 ctf_buf_free(ctf_buf_t *b)
104 strtab_destroy(&b->ctb_strtab);
110 ctf_buf_cur(ctf_buf_t *b)
112 return (b->ctb_ptr - b->ctb_base);
116 ctf_buf_write(ctf_buf_t *b, void const *p, size_t n)
121 if (b->ctb_ptr == b->ctb_end)
124 len = MIN((size_t)(b->ctb_end - b->ctb_ptr), n);
125 bcopy(p, b->ctb_ptr, len);
128 p = (char const *)p + len;
134 write_label(void *arg1, void *arg2)
136 labelent_t *le = arg1;
140 ctl.ctl_label = strtab_insert(&b->ctb_strtab, le->le_name);
141 ctl.ctl_typeidx = le->le_idx;
143 ctf_buf_write(b, &ctl, sizeof (ctl));
149 write_objects(iidesc_t *idp, ctf_buf_t *b)
151 ushort_t id = (idp ? idp->ii_dtype->t_id : 0);
153 ctf_buf_write(b, &id, sizeof (id));
155 debug(3, "Wrote object %s (%d)\n", (idp ? idp->ii_name : "(null)"), id);
159 write_functions(iidesc_t *idp, ctf_buf_t *b)
168 ctf_buf_write(b, &fdata[0], sizeof (fdata[0]));
170 debug(3, "Wrote function (null)\n");
174 nargs = idp->ii_nargs + (idp->ii_vargs != 0);
176 if (nargs > CTF_MAX_VLEN) {
177 terminate("function %s has too many args: %d > %d\n",
178 idp->ii_name, nargs, CTF_MAX_VLEN);
181 fdata[0] = CTF_TYPE_INFO(CTF_K_FUNCTION, 1, nargs);
182 fdata[1] = idp->ii_dtype->t_id;
183 ctf_buf_write(b, fdata, sizeof (fdata));
185 for (i = 0; i < idp->ii_nargs; i++) {
186 id = idp->ii_args[i]->t_id;
187 ctf_buf_write(b, &id, sizeof (id));
192 ctf_buf_write(b, &id, sizeof (id));
195 debug(3, "Wrote function %s (%d args)\n", idp->ii_name, nargs);
199 * Depending on the size of the type being described, either a ctf_stype_t (for
200 * types with size < CTF_LSTRUCT_THRESH) or a ctf_type_t (all others) will be
201 * written. We isolate the determination here so the rest of the writer code
202 * doesn't need to care.
205 write_sized_type_rec(ctf_buf_t *b, ctf_type_t *ctt, size_t size)
207 if (size > CTF_MAX_SIZE) {
208 ctt->ctt_size = CTF_LSIZE_SENT;
209 ctt->ctt_lsizehi = CTF_SIZE_TO_LSIZE_HI(size);
210 ctt->ctt_lsizelo = CTF_SIZE_TO_LSIZE_LO(size);
211 ctf_buf_write(b, ctt, sizeof (*ctt));
213 ctf_stype_t *cts = (ctf_stype_t *)ctt;
215 cts->ctt_size = (ushort_t)size;
216 ctf_buf_write(b, cts, sizeof (*cts));
221 write_unsized_type_rec(ctf_buf_t *b, ctf_type_t *ctt)
223 ctf_stype_t *cts = (ctf_stype_t *)ctt;
225 ctf_buf_write(b, cts, sizeof (*cts));
229 write_type(void *arg1, void *arg2)
240 int isroot = tp->t_flags & TDESC_F_ISROOT;
253 * There shouldn't be any holes in the type list (where a hole is
254 * defined as two consecutive tdescs without consecutive ids), but
255 * check for them just in case. If we do find holes, we need to make
256 * fake entries to fill the holes, or we won't be able to reconstruct
257 * the tree from the written data.
259 if (++b->nptent < CTF_TYPE_TO_INDEX(tp->t_id)) {
260 debug(2, "genctf: type hole from %d < x < %d\n",
261 b->nptent - 1, CTF_TYPE_TO_INDEX(tp->t_id));
263 ctt.ctt_name = CTF_TYPE_NAME(CTF_STRTAB_0, 0);
264 ctt.ctt_info = CTF_TYPE_INFO(0, 0, 0);
265 while (b->nptent < CTF_TYPE_TO_INDEX(tp->t_id)) {
266 write_sized_type_rec(b, &ctt, 0);
271 offset = strtab_insert(&b->ctb_strtab, tp->t_name);
272 ctt.ctt_name = CTF_TYPE_NAME(CTF_STRTAB_0, offset);
274 switch (tp->t_type) {
277 if (ip->intr_type == INTR_INT)
278 ctt.ctt_info = CTF_TYPE_INFO(CTF_K_INTEGER,
281 ctt.ctt_info = CTF_TYPE_INFO(CTF_K_FLOAT, isroot, 1);
282 write_sized_type_rec(b, &ctt, tp->t_size);
286 if (ip->intr_type == INTR_INT) {
288 encoding |= CTF_INT_SIGNED;
289 if (ip->intr_iformat == 'c')
290 encoding |= CTF_INT_CHAR;
291 else if (ip->intr_iformat == 'b')
292 encoding |= CTF_INT_BOOL;
293 else if (ip->intr_iformat == 'v')
294 encoding |= CTF_INT_VARARGS;
296 encoding = ip->intr_fformat;
298 data = CTF_INT_DATA(encoding, ip->intr_offset, ip->intr_nbits);
299 ctf_buf_write(b, &data, sizeof (data));
303 ctt.ctt_info = CTF_TYPE_INFO(CTF_K_POINTER, isroot, 0);
304 ctt.ctt_type = tp->t_tdesc->t_id;
305 write_unsized_type_rec(b, &ctt);
309 ctt.ctt_info = CTF_TYPE_INFO(CTF_K_ARRAY, isroot, 1);
310 write_sized_type_rec(b, &ctt, tp->t_size);
312 cta.cta_contents = tp->t_ardef->ad_contents->t_id;
313 cta.cta_index = tp->t_ardef->ad_idxtype->t_id;
314 cta.cta_nelems = tp->t_ardef->ad_nelems;
315 ctf_buf_write(b, &cta, sizeof (cta));
320 for (i = 0, mp = tp->t_members; mp != NULL; mp = mp->ml_next)
321 i++; /* count up struct or union members */
323 if (i > CTF_MAX_VLEN) {
324 terminate("sou %s has too many members: %d > %d\n",
325 tdesc_name(tp), i, CTF_MAX_VLEN);
328 if (tp->t_type == STRUCT)
329 ctt.ctt_info = CTF_TYPE_INFO(CTF_K_STRUCT, isroot, i);
331 ctt.ctt_info = CTF_TYPE_INFO(CTF_K_UNION, isroot, i);
333 write_sized_type_rec(b, &ctt, tp->t_size);
335 if (tp->t_size < CTF_LSTRUCT_THRESH) {
336 for (mp = tp->t_members; mp != NULL; mp = mp->ml_next) {
337 offset = strtab_insert(&b->ctb_strtab,
340 ctm.ctm_name = CTF_TYPE_NAME(CTF_STRTAB_0,
342 ctm.ctm_type = mp->ml_type->t_id;
343 ctm.ctm_offset = mp->ml_offset;
344 ctf_buf_write(b, &ctm, sizeof (ctm));
347 for (mp = tp->t_members; mp != NULL; mp = mp->ml_next) {
348 offset = strtab_insert(&b->ctb_strtab,
351 ctlm.ctlm_name = CTF_TYPE_NAME(CTF_STRTAB_0,
353 ctlm.ctlm_type = mp->ml_type->t_id;
355 CTF_OFFSET_TO_LMEMHI(mp->ml_offset);
357 CTF_OFFSET_TO_LMEMLO(mp->ml_offset);
358 ctf_buf_write(b, &ctlm, sizeof (ctlm));
364 for (i = 0, ep = tp->t_emem; ep != NULL; ep = ep->el_next)
365 i++; /* count up enum members */
367 if (i > CTF_MAX_VLEN) {
368 warning("enum %s has too many values: %d > %d\n",
369 tdesc_name(tp), i, CTF_MAX_VLEN);
373 ctt.ctt_info = CTF_TYPE_INFO(CTF_K_ENUM, isroot, i);
374 write_sized_type_rec(b, &ctt, tp->t_size);
376 for (ep = tp->t_emem; ep != NULL && i > 0; ep = ep->el_next) {
377 offset = strtab_insert(&b->ctb_strtab, ep->el_name);
378 cte.cte_name = CTF_TYPE_NAME(CTF_STRTAB_0, offset);
379 cte.cte_value = ep->el_number;
380 ctf_buf_write(b, &cte, sizeof (cte));
386 ctt.ctt_info = CTF_TYPE_INFO(CTF_K_FORWARD, isroot, 0);
388 write_unsized_type_rec(b, &ctt);
392 ctt.ctt_info = CTF_TYPE_INFO(CTF_K_TYPEDEF, isroot, 0);
393 ctt.ctt_type = tp->t_tdesc->t_id;
394 write_unsized_type_rec(b, &ctt);
398 ctt.ctt_info = CTF_TYPE_INFO(CTF_K_VOLATILE, isroot, 0);
399 ctt.ctt_type = tp->t_tdesc->t_id;
400 write_unsized_type_rec(b, &ctt);
404 ctt.ctt_info = CTF_TYPE_INFO(CTF_K_CONST, isroot, 0);
405 ctt.ctt_type = tp->t_tdesc->t_id;
406 write_unsized_type_rec(b, &ctt);
410 i = tp->t_fndef->fn_nargs + tp->t_fndef->fn_vargs;
412 if (i > CTF_MAX_VLEN) {
413 terminate("function %s has too many args: %d > %d\n",
417 ctt.ctt_info = CTF_TYPE_INFO(CTF_K_FUNCTION, isroot, i);
418 ctt.ctt_type = tp->t_fndef->fn_ret->t_id;
419 write_unsized_type_rec(b, &ctt);
421 for (i = 0; i < (int) tp->t_fndef->fn_nargs; i++) {
422 id = tp->t_fndef->fn_args[i]->t_id;
423 ctf_buf_write(b, &id, sizeof (id));
426 if (tp->t_fndef->fn_vargs) {
428 ctf_buf_write(b, &id, sizeof (id));
434 ctf_buf_write(b, &id, sizeof (id));
439 ctt.ctt_info = CTF_TYPE_INFO(CTF_K_RESTRICT, isroot, 0);
440 ctt.ctt_type = tp->t_tdesc->t_id;
441 write_unsized_type_rec(b, &ctt);
445 warning("Can't write unknown type %d\n", tp->t_type);
448 debug(3, "Wrote type %d %s\n", tp->t_id, tdesc_name(tp));
453 typedef struct resbuf {
461 rbzs_grow(resbuf_t *rb)
463 off_t ptroff = (caddr_t)rb->rb_zstr.next_out - rb->rb_base;
465 rb->rb_size += RES_BUF_CHUNK_SIZE;
466 rb->rb_base = xrealloc(rb->rb_base, rb->rb_size);
467 rb->rb_ptr = rb->rb_base + ptroff;
468 rb->rb_zstr.next_out = (Bytef *)(rb->rb_ptr);
469 rb->rb_zstr.avail_out += RES_BUF_CHUNK_SIZE;
473 compress_start(resbuf_t *rb)
477 rb->rb_zstr.zalloc = (alloc_func)0;
478 rb->rb_zstr.zfree = (free_func)0;
479 rb->rb_zstr.opaque = (voidpf)0;
481 if ((rc = deflateInit(&rb->rb_zstr, Z_BEST_COMPRESSION)) != Z_OK)
482 parseterminate("zlib start failed: %s", zError(rc));
486 compress_buffer(void *buf, size_t n, void *data)
488 resbuf_t *rb = (resbuf_t *)data;
491 rb->rb_zstr.next_out = (Bytef *)rb->rb_ptr;
492 rb->rb_zstr.avail_out = rb->rb_size - (rb->rb_ptr - rb->rb_base);
493 rb->rb_zstr.next_in = buf;
494 rb->rb_zstr.avail_in = n;
496 while (rb->rb_zstr.avail_in) {
497 if (rb->rb_zstr.avail_out == 0)
500 if ((rc = deflate(&rb->rb_zstr, Z_NO_FLUSH)) != Z_OK)
501 parseterminate("zlib deflate failed: %s", zError(rc));
503 rb->rb_ptr = (caddr_t)rb->rb_zstr.next_out;
509 compress_flush(resbuf_t *rb, int type)
514 if (rb->rb_zstr.avail_out == 0)
517 rc = deflate(&rb->rb_zstr, type);
518 if ((type == Z_FULL_FLUSH && rc == Z_BUF_ERROR) ||
519 (type == Z_FINISH && rc == Z_STREAM_END))
522 parseterminate("zlib finish failed: %s", zError(rc));
524 rb->rb_ptr = (caddr_t)rb->rb_zstr.next_out;
528 compress_end(resbuf_t *rb)
532 compress_flush(rb, Z_FINISH);
534 if ((rc = deflateEnd(&rb->rb_zstr)) != Z_OK)
535 parseterminate("zlib end failed: %s", zError(rc));
539 * Pad the buffer to a power-of-2 boundary
542 pad_buffer(ctf_buf_t *buf, int align)
544 uint_t cur = ctf_buf_cur(buf);
545 ssize_t topad = (align - (cur % align)) % align;
546 static const char pad[8] = { 0 };
549 ctf_buf_write(buf, pad, (topad > 8 ? 8 : topad));
555 bcopy_data(void *buf, size_t n, void *data)
557 caddr_t *posp = (caddr_t *)data;
558 bcopy(buf, *posp, n);
564 write_buffer(ctf_header_t *h, ctf_buf_t *buf, size_t *resszp)
569 outbuf = xmalloc(sizeof (ctf_header_t) + (buf->ctb_ptr - buf->ctb_base)
570 + buf->ctb_strtab.str_size);
573 (void) bcopy_data(h, sizeof (ctf_header_t), &bufpos);
574 (void) bcopy_data(buf->ctb_base, buf->ctb_ptr - buf->ctb_base,
576 (void) strtab_write(&buf->ctb_strtab, bcopy_data, &bufpos);
577 *resszp = bufpos - outbuf;
582 * Create the compression buffer, and fill it with the CTF and string
583 * table data. We flush the compression state between the two so the
584 * dictionary used for the string tables won't be polluted with values
585 * that made sense for the CTF data.
588 write_compressed_buffer(ctf_header_t *h, ctf_buf_t *buf, size_t *resszp)
591 resbuf.rb_size = RES_BUF_CHUNK_SIZE;
592 resbuf.rb_base = xmalloc(resbuf.rb_size);
593 bcopy(h, resbuf.rb_base, sizeof (ctf_header_t));
594 resbuf.rb_ptr = resbuf.rb_base + sizeof (ctf_header_t);
596 compress_start(&resbuf);
597 (void) compress_buffer(buf->ctb_base, buf->ctb_ptr - buf->ctb_base,
599 compress_flush(&resbuf, Z_FULL_FLUSH);
600 (void) strtab_write(&buf->ctb_strtab, compress_buffer, &resbuf);
601 compress_end(&resbuf);
603 *resszp = (resbuf.rb_ptr - resbuf.rb_base);
604 return (resbuf.rb_base);
608 ctf_gen(iiburst_t *iiburst, size_t *resszp, int do_compress)
610 ctf_buf_t *buf = ctf_buf_new();
617 * Prepare the header, and create the CTF output buffers. The data
618 * object section and function section are both lists of 2-byte
619 * integers; we pad these out to the next 4-byte boundary if needed.
621 h.cth_magic = CTF_MAGIC;
622 h.cth_version = CTF_VERSION;
623 h.cth_flags = do_compress ? CTF_F_COMPRESS : 0;
624 h.cth_parlabel = strtab_insert(&buf->ctb_strtab,
625 iiburst->iib_td->td_parlabel);
626 h.cth_parname = strtab_insert(&buf->ctb_strtab,
627 iiburst->iib_td->td_parname);
630 (void) list_iter(iiburst->iib_td->td_labels, write_label,
634 h.cth_objtoff = ctf_buf_cur(buf);
635 for (i = 0; i < iiburst->iib_nobjts; i++)
636 write_objects(iiburst->iib_objts[i], buf);
639 h.cth_funcoff = ctf_buf_cur(buf);
640 for (i = 0; i < iiburst->iib_nfuncs; i++)
641 write_functions(iiburst->iib_funcs[i], buf);
644 h.cth_typeoff = ctf_buf_cur(buf);
645 (void) list_iter(iiburst->iib_types, write_type, buf);
647 debug(2, "CTF wrote %d types\n", list_count(iiburst->iib_types));
649 h.cth_stroff = ctf_buf_cur(buf);
650 h.cth_strlen = strtab_size(&buf->ctb_strtab);
653 * We only do compression for ctfmerge, as ctfconvert is only
654 * supposed to be used on intermediary build objects. This is
655 * significantly faster.
658 outbuf = write_compressed_buffer(&h, buf, resszp);
660 outbuf = write_buffer(&h, buf, resszp);
667 get_ctt_size(ctf_type_t *ctt, size_t *sizep, size_t *incrementp)
669 if (ctt->ctt_size == CTF_LSIZE_SENT) {
670 *sizep = (size_t)CTF_TYPE_LSIZE(ctt);
671 *incrementp = sizeof (ctf_type_t);
673 *sizep = ctt->ctt_size;
674 *incrementp = sizeof (ctf_stype_t);
679 count_types(ctf_header_t *h, caddr_t data)
681 caddr_t dptr = data + h->cth_typeoff;
684 dptr = data + h->cth_typeoff;
685 while (dptr < data + h->cth_stroff) {
686 void *v = (void *) dptr;
688 size_t vlen = CTF_INFO_VLEN(ctt->ctt_info);
689 size_t size, increment;
691 get_ctt_size(ctt, &size, &increment);
693 switch (CTF_INFO_KIND(ctt->ctt_info)) {
705 dptr += sizeof (ushort_t) * (vlen + (vlen & 1));
708 dptr += sizeof (ctf_array_t);
712 if (size < CTF_LSTRUCT_THRESH)
713 dptr += sizeof (ctf_member_t) * vlen;
715 dptr += sizeof (ctf_lmember_t) * vlen;
718 dptr += sizeof (ctf_enum_t) * vlen;
723 parseterminate("Unknown CTF type %d (#%d) at %#x",
724 CTF_INFO_KIND(ctt->ctt_info), count, dptr - data);
731 debug(3, "CTF read %d types\n", count);
737 * Resurrect the labels stored in the CTF data, returning the index associated
738 * with a label provided by the caller. There are several cases, outlined
739 * below. Note that, given two labels, the one associated with the lesser type
740 * index is considered to be older than the other.
742 * 1. matchlbl == NULL - return the index of the most recent label.
743 * 2. matchlbl == "BASE" - return the index of the oldest label.
744 * 3. matchlbl != NULL, but doesn't match any labels in the section - warn
745 * the user, and proceed as if matchlbl == "BASE" (for safety).
746 * 4. matchlbl != NULL, and matches one of the labels in the section - return
747 * the type index associated with the label.
750 resurrect_labels(ctf_header_t *h, tdata_t *td, caddr_t ctfdata, char *matchlbl)
752 caddr_t buf = ctfdata + h->cth_lbloff;
753 caddr_t sbuf = ctfdata + h->cth_stroff;
754 size_t bufsz = h->cth_objtoff - h->cth_lbloff;
755 int lastidx = 0, baseidx = -1;
756 char *baselabel = NULL;
758 void *v = (void *) buf;
760 for (ctl = v; (caddr_t)ctl < buf + bufsz; ctl++) {
761 char *label = sbuf + ctl->ctl_label;
763 lastidx = ctl->ctl_typeidx;
765 debug(3, "Resurrected label %s type idx %d\n", label, lastidx);
767 tdata_label_add(td, label, lastidx);
772 if (matchlbl != NULL && streq(matchlbl, "BASE"))
776 if (matchlbl != NULL && streq(label, matchlbl))
780 if (matchlbl != NULL) {
781 /* User provided a label that didn't match */
782 warning("%s: Cannot find label `%s' - using base (%s)\n",
783 curfile, matchlbl, (baselabel ? baselabel : "NONE"));
785 tdata_label_free(td);
786 tdata_label_add(td, baselabel, baseidx);
795 resurrect_objects(ctf_header_t *h, tdata_t *td, tdesc_t **tdarr, int tdsize,
796 caddr_t ctfdata, symit_data_t *si)
798 caddr_t buf = ctfdata + h->cth_objtoff;
799 size_t bufsz = h->cth_funcoff - h->cth_objtoff;
803 for (dptr = buf; dptr < buf + bufsz; dptr += 2) {
804 void *v = (void *) dptr;
805 ushort_t id = *((ushort_t *)v);
809 if (!(sym = symit_next(si, STT_OBJECT)) && id != 0) {
811 "Unexpected end of object symbols at %x of %x",
816 debug(3, "Skipping null object\n");
818 } else if (id >= tdsize) {
819 parseterminate("Reference to invalid type %d", id);
822 ii = iidesc_new(symit_name(si));
823 ii->ii_dtype = tdarr[id];
824 if (GELF_ST_BIND(sym->st_info) == STB_LOCAL) {
825 ii->ii_type = II_SVAR;
826 ii->ii_owner = xstrdup(symit_curfile(si));
828 ii->ii_type = II_GVAR;
829 hash_add(td->td_iihash, ii);
831 debug(3, "Resurrected %s object %s (%d) from %s\n",
832 (ii->ii_type == II_GVAR ? "global" : "static"),
833 ii->ii_name, id, (ii->ii_owner ? ii->ii_owner : "(none)"));
838 resurrect_functions(ctf_header_t *h, tdata_t *td, tdesc_t **tdarr, int tdsize,
839 caddr_t ctfdata, symit_data_t *si)
841 caddr_t buf = ctfdata + h->cth_funcoff;
842 size_t bufsz = h->cth_typeoff - h->cth_funcoff;
851 while (dptr < buf + bufsz) {
852 void *v = (void *) dptr;
853 info = *((ushort_t *)v);
856 if (!(sym = symit_next(si, STT_FUNC)) && info != 0)
857 parseterminate("Unexpected end of function symbols");
860 debug(3, "Skipping null function (%s)\n",
866 retid = *((ushort_t *)v);
870 parseterminate("Reference to invalid type %d", retid);
872 ii = iidesc_new(symit_name(si));
873 ii->ii_dtype = tdarr[retid];
874 if (GELF_ST_BIND(sym->st_info) == STB_LOCAL) {
875 ii->ii_type = II_SFUN;
876 ii->ii_owner = xstrdup(symit_curfile(si));
878 ii->ii_type = II_GFUN;
879 ii->ii_nargs = CTF_INFO_VLEN(info);
882 xmalloc(sizeof (tdesc_t *) * ii->ii_nargs);
884 for (i = 0; i < ii->ii_nargs; i++, dptr += 2) {
886 ushort_t id = *((ushort_t *)v);
888 parseterminate("Reference to invalid type %d",
890 ii->ii_args[i] = tdarr[id];
893 if (ii->ii_nargs && ii->ii_args[ii->ii_nargs - 1] == NULL) {
898 hash_add(td->td_iihash, ii);
900 debug(3, "Resurrected %s function %s (%d, %d args)\n",
901 (ii->ii_type == II_GFUN ? "global" : "static"),
902 ii->ii_name, retid, ii->ii_nargs);
907 resurrect_types(ctf_header_t *h, tdata_t *td, tdesc_t **tdarr, int tdsize,
908 caddr_t ctfdata, int maxid)
910 caddr_t buf = ctfdata + h->cth_typeoff;
911 size_t bufsz = h->cth_stroff - h->cth_typeoff;
912 caddr_t sbuf = ctfdata + h->cth_stroff;
917 size_t size, increment;
933 * A maxid of zero indicates a request to resurrect all types, so reset
934 * maxid to the maximum type id.
937 maxid = CTF_MAX_TYPE;
939 for (dptr = buf, tcnt = 0, tid = 1; dptr < buf + bufsz; tcnt++, tid++) {
944 parseterminate("Reference to invalid type %d", tid);
946 void *v = (void *) dptr;
949 get_ctt_size(ctt, &size, &increment);
954 if (CTF_NAME_STID(ctt->ctt_name) != CTF_STRTAB_0)
956 "Unable to cope with non-zero strtab id");
957 if (CTF_NAME_OFFSET(ctt->ctt_name) != 0) {
959 xstrdup(sbuf + CTF_NAME_OFFSET(ctt->ctt_name));
963 kind = CTF_INFO_KIND(ctt->ctt_info);
964 vlen = CTF_INFO_VLEN(ctt->ctt_info);
968 tdp->t_type = INTRINSIC;
972 data = *((uint_t *)v);
973 dptr += sizeof (uint_t);
974 encoding = CTF_INT_ENCODING(data);
976 ip = xmalloc(sizeof (intr_t));
977 ip->intr_type = INTR_INT;
978 ip->intr_signed = (encoding & CTF_INT_SIGNED) ? 1 : 0;
980 if (encoding & CTF_INT_CHAR)
981 ip->intr_iformat = 'c';
982 else if (encoding & CTF_INT_BOOL)
983 ip->intr_iformat = 'b';
984 else if (encoding & CTF_INT_VARARGS)
985 ip->intr_iformat = 'v';
987 ip->intr_iformat = '\0';
989 ip->intr_offset = CTF_INT_OFFSET(data);
990 ip->intr_nbits = CTF_INT_BITS(data);
995 tdp->t_type = INTRINSIC;
999 data = *((uint_t *)v);
1000 dptr += sizeof (uint_t);
1002 ip = xcalloc(sizeof (intr_t));
1003 ip->intr_type = INTR_REAL;
1004 ip->intr_fformat = CTF_FP_ENCODING(data);
1005 ip->intr_offset = CTF_FP_OFFSET(data);
1006 ip->intr_nbits = CTF_FP_BITS(data);
1011 tdp->t_type = POINTER;
1012 tdp->t_tdesc = tdarr[ctt->ctt_type];
1016 tdp->t_type = ARRAY;
1021 dptr += sizeof (ctf_array_t);
1023 tdp->t_ardef = xmalloc(sizeof (ardef_t));
1024 tdp->t_ardef->ad_contents = tdarr[cta->cta_contents];
1025 tdp->t_ardef->ad_idxtype = tdarr[cta->cta_index];
1026 tdp->t_ardef->ad_nelems = cta->cta_nelems;
1031 tdp->t_type = (kind == CTF_K_STRUCT ? STRUCT : UNION);
1034 if (size < CTF_LSTRUCT_THRESH) {
1035 for (i = 0, mpp = &tdp->t_members; i < vlen;
1036 i++, mpp = &((*mpp)->ml_next)) {
1038 ctf_member_t *ctm = v;
1039 dptr += sizeof (ctf_member_t);
1041 *mpp = xmalloc(sizeof (mlist_t));
1042 (*mpp)->ml_name = xstrdup(sbuf +
1044 (*mpp)->ml_type = tdarr[ctm->ctm_type];
1045 (*mpp)->ml_offset = ctm->ctm_offset;
1046 (*mpp)->ml_size = 0;
1049 for (i = 0, mpp = &tdp->t_members; i < vlen;
1050 i++, mpp = &((*mpp)->ml_next)) {
1052 ctf_lmember_t *ctlm = v;
1053 dptr += sizeof (ctf_lmember_t);
1055 *mpp = xmalloc(sizeof (mlist_t));
1056 (*mpp)->ml_name = xstrdup(sbuf +
1059 tdarr[ctlm->ctlm_type];
1061 (int)CTF_LMEM_OFFSET(ctlm);
1062 (*mpp)->ml_size = 0;
1073 for (i = 0, epp = &tdp->t_emem; i < vlen;
1074 i++, epp = &((*epp)->el_next)) {
1077 dptr += sizeof (ctf_enum_t);
1079 *epp = xmalloc(sizeof (elist_t));
1080 (*epp)->el_name = xstrdup(sbuf + cte->cte_name);
1081 (*epp)->el_number = cte->cte_value;
1087 tdp->t_type = FORWARD;
1088 list_add(&td->td_fwdlist, tdp);
1092 tdp->t_type = TYPEDEF;
1093 tdp->t_tdesc = tdarr[ctt->ctt_type];
1096 case CTF_K_VOLATILE:
1097 tdp->t_type = VOLATILE;
1098 tdp->t_tdesc = tdarr[ctt->ctt_type];
1102 tdp->t_type = CONST;
1103 tdp->t_tdesc = tdarr[ctt->ctt_type];
1106 case CTF_K_FUNCTION:
1107 tdp->t_type = FUNCTION;
1108 tdp->t_fndef = xcalloc(sizeof (fndef_t));
1109 tdp->t_fndef->fn_ret = tdarr[ctt->ctt_type];
1111 v = (void *) (dptr + (sizeof (ushort_t) * (vlen - 1)));
1112 if (vlen > 0 && *(ushort_t *)v == 0)
1113 tdp->t_fndef->fn_vargs = 1;
1115 tdp->t_fndef->fn_nargs = vlen - tdp->t_fndef->fn_vargs;
1116 tdp->t_fndef->fn_args = xcalloc(sizeof (tdesc_t) *
1117 vlen - tdp->t_fndef->fn_vargs);
1119 for (i = 0; i < vlen; i++) {
1121 argid = *(ushort_t *)v;
1122 dptr += sizeof (ushort_t);
1125 tdp->t_fndef->fn_args[i] = tdarr[argid];
1129 dptr += sizeof (ushort_t);
1132 case CTF_K_RESTRICT:
1133 tdp->t_type = RESTRICT;
1134 tdp->t_tdesc = tdarr[ctt->ctt_type];
1141 warning("Can't parse unknown CTF type %d\n", kind);
1144 if (CTF_INFO_ISROOT(ctt->ctt_info)) {
1145 iidesc_t *ii = iidesc_new(tdp->t_name);
1146 if (tdp->t_type == STRUCT || tdp->t_type == UNION ||
1147 tdp->t_type == ENUM)
1148 ii->ii_type = II_SOU;
1150 ii->ii_type = II_TYPE;
1152 hash_add(td->td_iihash, ii);
1157 debug(3, "Resurrected %d %stype %s (%d)\n", tdp->t_type,
1158 (CTF_INFO_ISROOT(ctt->ctt_info) ? "root " : ""),
1159 tdesc_name(tdp), tdp->t_id);
1162 debug(3, "Resurrected %d types (%d were roots)\n", tcnt, iicnt);
1166 * For lack of other inspiration, we're going to take the boring route. We
1167 * count the number of types. This lets us malloc that many tdesc structs
1168 * before we start filling them in. This has the advantage of allowing us to
1169 * avoid a merge-esque remap step.
1172 ctf_parse(ctf_header_t *h, caddr_t buf, symit_data_t *si, char *label)
1174 tdata_t *td = tdata_new();
1176 int ntypes = count_types(h, buf);
1180 tdarr = xcalloc(sizeof (tdesc_t *) * (ntypes + 1));
1182 for (i = 1; i <= ntypes; i++) {
1183 tdarr[i] = xcalloc(sizeof (tdesc_t));
1187 td->td_parlabel = xstrdup(buf + h->cth_stroff + h->cth_parlabel);
1189 /* we have the technology - we can rebuild them */
1190 idx = resurrect_labels(h, td, buf, label);
1192 resurrect_objects(h, td, tdarr, ntypes + 1, buf, si);
1193 resurrect_functions(h, td, tdarr, ntypes + 1, buf, si);
1194 resurrect_types(h, td, tdarr, ntypes + 1, buf, idx);
1198 td->td_nextid = ntypes + 1;
1204 decompress_ctf(caddr_t cbuf, size_t cbufsz, caddr_t dbuf, size_t dbufsz)
1209 zstr.zalloc = (alloc_func)0;
1210 zstr.zfree = (free_func)0;
1211 zstr.opaque = (voidpf)0;
1213 zstr.next_in = (Bytef *)cbuf;
1214 zstr.avail_in = cbufsz;
1215 zstr.next_out = (Bytef *)dbuf;
1216 zstr.avail_out = dbufsz;
1218 if ((rc = inflateInit(&zstr)) != Z_OK ||
1219 (rc = inflate(&zstr, Z_NO_FLUSH)) != Z_STREAM_END ||
1220 (rc = inflateEnd(&zstr)) != Z_OK) {
1221 warning("CTF decompress zlib error %s\n", zError(rc));
1225 debug(3, "reflated %lu bytes to %lu, pointer at %d\n",
1226 zstr.total_in, zstr.total_out, (caddr_t)zstr.next_in - cbuf);
1228 return (zstr.total_out);
1232 * Reconstruct the type tree from a given buffer of CTF data. Only the types
1233 * up to the type associated with the provided label, inclusive, will be
1234 * reconstructed. If a NULL label is provided, all types will be reconstructed.
1236 * This function won't work on files that have been uniquified.
1239 ctf_load(char *file, caddr_t buf, size_t bufsz, symit_data_t *si, char *label)
1248 if (bufsz < sizeof (ctf_header_t))
1249 parseterminate("Corrupt CTF - short header");
1251 void *v = (void *) buf;
1253 buf += sizeof (ctf_header_t);
1254 bufsz -= sizeof (ctf_header_t);
1256 if (h->cth_magic != CTF_MAGIC)
1257 parseterminate("Corrupt CTF - bad magic 0x%x", h->cth_magic);
1259 if (h->cth_version != CTF_VERSION)
1260 parseterminate("Unknown CTF version %d", h->cth_version);
1262 ctfdatasz = h->cth_stroff + h->cth_strlen;
1263 if (h->cth_flags & CTF_F_COMPRESS) {
1266 ctfdata = xmalloc(ctfdatasz);
1267 if ((actual = decompress_ctf(buf, bufsz, ctfdata, ctfdatasz)) !=
1269 parseterminate("Corrupt CTF - short decompression "
1270 "(was %d, expecting %d)", actual, ctfdatasz);
1277 td = ctf_parse(h, ctfdata, si, label);
1279 if (h->cth_flags & CTF_F_COMPRESS)