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 2006 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #pragma ident "%Z%%M% %I% %E% SMI"
29 * Create and parse buffers containing CTF data.
32 #include <sys/types.h>
40 #include "ctf_headers.h"
46 * Name of the file currently being read, used to print error messages. We
47 * assume that only one file will be read at a time, and thus make no attempt
48 * to allow curfile to be used simultaneously by multiple threads.
50 * The value is only valid during a call to ctf_load.
54 #define CTF_BUF_CHUNK_SIZE (64 * 1024)
55 #define RES_BUF_CHUNK_SIZE (64 * 1024)
58 strtab_t ctb_strtab; /* string table */
59 caddr_t ctb_base; /* pointer to base of buffer */
60 caddr_t ctb_end; /* pointer to end of buffer */
61 caddr_t ctb_ptr; /* pointer to empty buffer space */
62 size_t ctb_size; /* size of buffer */
63 int nptent; /* number of processed types */
64 int ntholes; /* number of type holes */
69 parseterminate(const char *fmt, ...)
71 static char msgbuf[1024]; /* sigh */
75 vsnprintf(msgbuf, sizeof (msgbuf), fmt, ap);
78 terminate("%s: %s\n", curfile, msgbuf);
82 ctf_buf_grow(ctf_buf_t *b)
84 off_t ptroff = b->ctb_ptr - b->ctb_base;
86 b->ctb_size += CTF_BUF_CHUNK_SIZE;
87 b->ctb_base = xrealloc(b->ctb_base, b->ctb_size);
88 b->ctb_end = b->ctb_base + b->ctb_size;
89 b->ctb_ptr = b->ctb_base + ptroff;
95 ctf_buf_t *b = xcalloc(sizeof (ctf_buf_t));
97 strtab_create(&b->ctb_strtab);
104 ctf_buf_free(ctf_buf_t *b)
106 strtab_destroy(&b->ctb_strtab);
112 ctf_buf_cur(ctf_buf_t *b)
114 return (b->ctb_ptr - b->ctb_base);
118 ctf_buf_write(ctf_buf_t *b, void const *p, size_t n)
123 if (b->ctb_ptr == b->ctb_end)
126 len = MIN((size_t)(b->ctb_end - b->ctb_ptr), n);
127 bcopy(p, b->ctb_ptr, len);
130 p = (char const *)p + len;
136 write_label(void *arg1, void *arg2)
138 labelent_t *le = arg1;
142 ctl.ctl_label = strtab_insert(&b->ctb_strtab, le->le_name);
143 ctl.ctl_typeidx = le->le_idx;
145 ctf_buf_write(b, &ctl, sizeof (ctl));
151 write_objects(iidesc_t *idp, ctf_buf_t *b)
153 ushort_t id = (idp ? idp->ii_dtype->t_id : 0);
155 ctf_buf_write(b, &id, sizeof (id));
157 debug(3, "Wrote object %s (%d)\n", (idp ? idp->ii_name : "(null)"), id);
161 write_functions(iidesc_t *idp, ctf_buf_t *b)
170 ctf_buf_write(b, &fdata[0], sizeof (fdata[0]));
172 debug(3, "Wrote function (null)\n");
176 nargs = idp->ii_nargs + (idp->ii_vargs != 0);
177 fdata[0] = CTF_TYPE_INFO(CTF_K_FUNCTION, 1, nargs);
178 fdata[1] = idp->ii_dtype->t_id;
179 ctf_buf_write(b, fdata, sizeof (fdata));
181 for (i = 0; i < idp->ii_nargs; i++) {
182 id = idp->ii_args[i]->t_id;
183 ctf_buf_write(b, &id, sizeof (id));
188 ctf_buf_write(b, &id, sizeof (id));
191 debug(3, "Wrote function %s (%d args)\n", idp->ii_name, nargs);
195 * Depending on the size of the type being described, either a ctf_stype_t (for
196 * types with size < CTF_LSTRUCT_THRESH) or a ctf_type_t (all others) will be
197 * written. We isolate the determination here so the rest of the writer code
198 * doesn't need to care.
201 write_sized_type_rec(ctf_buf_t *b, ctf_type_t *ctt, size_t size)
203 if (size > CTF_MAX_SIZE) {
204 ctt->ctt_size = CTF_LSIZE_SENT;
205 ctt->ctt_lsizehi = CTF_SIZE_TO_LSIZE_HI(size);
206 ctt->ctt_lsizelo = CTF_SIZE_TO_LSIZE_LO(size);
207 ctf_buf_write(b, ctt, sizeof (*ctt));
209 ctf_stype_t *cts = (ctf_stype_t *)ctt;
211 cts->ctt_size = (ushort_t)size;
212 ctf_buf_write(b, cts, sizeof (*cts));
217 write_unsized_type_rec(ctf_buf_t *b, ctf_type_t *ctt)
219 ctf_stype_t *cts = (ctf_stype_t *)ctt;
221 ctf_buf_write(b, cts, sizeof (*cts));
225 write_type(void *arg1, void *arg2)
236 int isroot = tp->t_flags & TDESC_F_ISROOT;
249 * There shouldn't be any holes in the type list (where a hole is
250 * defined as two consecutive tdescs without consecutive ids), but
251 * check for them just in case. If we do find holes, we need to make
252 * fake entries to fill the holes, or we won't be able to reconstruct
253 * the tree from the written data.
255 if (++b->nptent < CTF_TYPE_TO_INDEX(tp->t_id)) {
256 debug(2, "genctf: type hole from %d < x < %d\n",
257 b->nptent - 1, CTF_TYPE_TO_INDEX(tp->t_id));
259 ctt.ctt_name = CTF_TYPE_NAME(CTF_STRTAB_0, 0);
260 ctt.ctt_info = CTF_TYPE_INFO(0, 0, 0);
261 while (b->nptent < CTF_TYPE_TO_INDEX(tp->t_id)) {
262 write_sized_type_rec(b, &ctt, 0);
267 offset = strtab_insert(&b->ctb_strtab, tp->t_name);
268 ctt.ctt_name = CTF_TYPE_NAME(CTF_STRTAB_0, offset);
270 switch (tp->t_type) {
273 if (ip->intr_type == INTR_INT)
274 ctt.ctt_info = CTF_TYPE_INFO(CTF_K_INTEGER,
277 ctt.ctt_info = CTF_TYPE_INFO(CTF_K_FLOAT, isroot, 1);
278 write_sized_type_rec(b, &ctt, tp->t_size);
282 if (ip->intr_type == INTR_INT) {
284 encoding |= CTF_INT_SIGNED;
285 if (ip->intr_iformat == 'c')
286 encoding |= CTF_INT_CHAR;
287 else if (ip->intr_iformat == 'b')
288 encoding |= CTF_INT_BOOL;
289 else if (ip->intr_iformat == 'v')
290 encoding |= CTF_INT_VARARGS;
292 encoding = ip->intr_fformat;
294 data = CTF_INT_DATA(encoding, ip->intr_offset, ip->intr_nbits);
295 ctf_buf_write(b, &data, sizeof (data));
299 ctt.ctt_info = CTF_TYPE_INFO(CTF_K_POINTER, isroot, 0);
300 ctt.ctt_type = tp->t_tdesc->t_id;
301 write_unsized_type_rec(b, &ctt);
305 ctt.ctt_info = CTF_TYPE_INFO(CTF_K_ARRAY, isroot, 1);
306 write_sized_type_rec(b, &ctt, tp->t_size);
308 cta.cta_contents = tp->t_ardef->ad_contents->t_id;
309 cta.cta_index = tp->t_ardef->ad_idxtype->t_id;
310 cta.cta_nelems = tp->t_ardef->ad_nelems;
311 ctf_buf_write(b, &cta, sizeof (cta));
316 for (i = 0, mp = tp->t_members; mp != NULL; mp = mp->ml_next)
317 i++; /* count up struct or union members */
319 if (tp->t_type == STRUCT)
320 ctt.ctt_info = CTF_TYPE_INFO(CTF_K_STRUCT, isroot, i);
322 ctt.ctt_info = CTF_TYPE_INFO(CTF_K_UNION, isroot, i);
324 write_sized_type_rec(b, &ctt, tp->t_size);
326 if (tp->t_size < CTF_LSTRUCT_THRESH) {
327 for (mp = tp->t_members; mp != NULL; mp = mp->ml_next) {
328 offset = strtab_insert(&b->ctb_strtab,
331 ctm.ctm_name = CTF_TYPE_NAME(CTF_STRTAB_0,
333 ctm.ctm_type = mp->ml_type->t_id;
334 ctm.ctm_offset = mp->ml_offset;
335 ctf_buf_write(b, &ctm, sizeof (ctm));
338 for (mp = tp->t_members; mp != NULL; mp = mp->ml_next) {
339 offset = strtab_insert(&b->ctb_strtab,
342 ctlm.ctlm_name = CTF_TYPE_NAME(CTF_STRTAB_0,
344 ctlm.ctlm_type = mp->ml_type->t_id;
346 CTF_OFFSET_TO_LMEMHI(mp->ml_offset);
348 CTF_OFFSET_TO_LMEMLO(mp->ml_offset);
349 ctf_buf_write(b, &ctlm, sizeof (ctlm));
355 for (i = 0, ep = tp->t_emem; ep != NULL; ep = ep->el_next)
356 i++; /* count up enum members */
358 ctt.ctt_info = CTF_TYPE_INFO(CTF_K_ENUM, isroot, i);
359 write_sized_type_rec(b, &ctt, tp->t_size);
361 for (ep = tp->t_emem; ep != NULL; ep = ep->el_next) {
362 offset = strtab_insert(&b->ctb_strtab, ep->el_name);
363 cte.cte_name = CTF_TYPE_NAME(CTF_STRTAB_0, offset);
364 cte.cte_value = ep->el_number;
365 ctf_buf_write(b, &cte, sizeof (cte));
370 ctt.ctt_info = CTF_TYPE_INFO(CTF_K_FORWARD, isroot, 0);
372 write_unsized_type_rec(b, &ctt);
376 ctt.ctt_info = CTF_TYPE_INFO(CTF_K_TYPEDEF, isroot, 0);
377 ctt.ctt_type = tp->t_tdesc->t_id;
378 write_unsized_type_rec(b, &ctt);
382 ctt.ctt_info = CTF_TYPE_INFO(CTF_K_VOLATILE, isroot, 0);
383 ctt.ctt_type = tp->t_tdesc->t_id;
384 write_unsized_type_rec(b, &ctt);
388 ctt.ctt_info = CTF_TYPE_INFO(CTF_K_CONST, isroot, 0);
389 ctt.ctt_type = tp->t_tdesc->t_id;
390 write_unsized_type_rec(b, &ctt);
394 ctt.ctt_info = CTF_TYPE_INFO(CTF_K_FUNCTION, isroot,
395 tp->t_fndef->fn_nargs + tp->t_fndef->fn_vargs);
396 ctt.ctt_type = tp->t_fndef->fn_ret->t_id;
397 write_unsized_type_rec(b, &ctt);
399 for (i = 0; i < (int) tp->t_fndef->fn_nargs; i++) {
400 id = tp->t_fndef->fn_args[i]->t_id;
401 ctf_buf_write(b, &id, sizeof (id));
404 if (tp->t_fndef->fn_vargs) {
406 ctf_buf_write(b, &id, sizeof (id));
412 ctf_buf_write(b, &id, sizeof (id));
417 ctt.ctt_info = CTF_TYPE_INFO(CTF_K_RESTRICT, isroot, 0);
418 ctt.ctt_type = tp->t_tdesc->t_id;
419 write_unsized_type_rec(b, &ctt);
423 warning("Can't write unknown type %d\n", tp->t_type);
426 debug(3, "Wrote type %d %s\n", tp->t_id, tdesc_name(tp));
431 typedef struct resbuf {
439 rbzs_grow(resbuf_t *rb)
441 off_t ptroff = (caddr_t)rb->rb_zstr.next_out - rb->rb_base;
443 rb->rb_size += RES_BUF_CHUNK_SIZE;
444 rb->rb_base = xrealloc(rb->rb_base, rb->rb_size);
445 rb->rb_ptr = rb->rb_base + ptroff;
446 rb->rb_zstr.next_out = (Bytef *)(rb->rb_ptr);
447 rb->rb_zstr.avail_out += RES_BUF_CHUNK_SIZE;
451 compress_start(resbuf_t *rb)
455 rb->rb_zstr.zalloc = (alloc_func)0;
456 rb->rb_zstr.zfree = (free_func)0;
457 rb->rb_zstr.opaque = (voidpf)0;
459 if ((rc = deflateInit(&rb->rb_zstr, Z_BEST_COMPRESSION)) != Z_OK)
460 parseterminate("zlib start failed: %s", zError(rc));
464 compress_buffer(void *buf, size_t n, void *data)
466 resbuf_t *rb = (resbuf_t *)data;
469 rb->rb_zstr.next_out = (Bytef *)rb->rb_ptr;
470 rb->rb_zstr.avail_out = rb->rb_size - (rb->rb_ptr - rb->rb_base);
471 rb->rb_zstr.next_in = buf;
472 rb->rb_zstr.avail_in = n;
474 while (rb->rb_zstr.avail_in) {
475 if (rb->rb_zstr.avail_out == 0)
478 if ((rc = deflate(&rb->rb_zstr, Z_NO_FLUSH)) != Z_OK)
479 parseterminate("zlib deflate failed: %s", zError(rc));
481 rb->rb_ptr = (caddr_t)rb->rb_zstr.next_out;
487 compress_flush(resbuf_t *rb, int type)
492 if (rb->rb_zstr.avail_out == 0)
495 rc = deflate(&rb->rb_zstr, type);
496 if ((type == Z_FULL_FLUSH && rc == Z_BUF_ERROR) ||
497 (type == Z_FINISH && rc == Z_STREAM_END))
500 parseterminate("zlib finish failed: %s", zError(rc));
502 rb->rb_ptr = (caddr_t)rb->rb_zstr.next_out;
506 compress_end(resbuf_t *rb)
510 compress_flush(rb, Z_FINISH);
512 if ((rc = deflateEnd(&rb->rb_zstr)) != Z_OK)
513 parseterminate("zlib end failed: %s", zError(rc));
517 * Pad the buffer to a power-of-2 boundary
520 pad_buffer(ctf_buf_t *buf, int align)
522 uint_t cur = ctf_buf_cur(buf);
523 ssize_t topad = (align - (cur % align)) % align;
524 static const char pad[8] = { 0 };
527 ctf_buf_write(buf, pad, (topad > 8 ? 8 : topad));
533 bcopy_data(void *buf, size_t n, void *data)
535 caddr_t *posp = (caddr_t *)data;
536 bcopy(buf, *posp, n);
542 write_buffer(ctf_header_t *h, ctf_buf_t *buf, size_t *resszp)
547 outbuf = xmalloc(sizeof (ctf_header_t) + (buf->ctb_ptr - buf->ctb_base)
548 + buf->ctb_strtab.str_size);
551 (void) bcopy_data(h, sizeof (ctf_header_t), &bufpos);
552 (void) bcopy_data(buf->ctb_base, buf->ctb_ptr - buf->ctb_base,
554 (void) strtab_write(&buf->ctb_strtab, bcopy_data, &bufpos);
555 *resszp = bufpos - outbuf;
560 * Create the compression buffer, and fill it with the CTF and string
561 * table data. We flush the compression state between the two so the
562 * dictionary used for the string tables won't be polluted with values
563 * that made sense for the CTF data.
566 write_compressed_buffer(ctf_header_t *h, ctf_buf_t *buf, size_t *resszp)
569 resbuf.rb_size = RES_BUF_CHUNK_SIZE;
570 resbuf.rb_base = xmalloc(resbuf.rb_size);
571 bcopy(h, resbuf.rb_base, sizeof (ctf_header_t));
572 resbuf.rb_ptr = resbuf.rb_base + sizeof (ctf_header_t);
574 compress_start(&resbuf);
575 (void) compress_buffer(buf->ctb_base, buf->ctb_ptr - buf->ctb_base,
577 compress_flush(&resbuf, Z_FULL_FLUSH);
578 (void) strtab_write(&buf->ctb_strtab, compress_buffer, &resbuf);
579 compress_end(&resbuf);
581 *resszp = (resbuf.rb_ptr - resbuf.rb_base);
582 return (resbuf.rb_base);
586 ctf_gen(iiburst_t *iiburst, size_t *resszp, int do_compress)
588 ctf_buf_t *buf = ctf_buf_new();
595 * Prepare the header, and create the CTF output buffers. The data
596 * object section and function section are both lists of 2-byte
597 * integers; we pad these out to the next 4-byte boundary if needed.
599 h.cth_magic = CTF_MAGIC;
600 h.cth_version = CTF_VERSION;
601 h.cth_flags = do_compress ? CTF_F_COMPRESS : 0;
602 h.cth_parlabel = strtab_insert(&buf->ctb_strtab,
603 iiburst->iib_td->td_parlabel);
604 h.cth_parname = strtab_insert(&buf->ctb_strtab,
605 iiburst->iib_td->td_parname);
608 (void) list_iter(iiburst->iib_td->td_labels, write_label,
612 h.cth_objtoff = ctf_buf_cur(buf);
613 for (i = 0; i < iiburst->iib_nobjts; i++)
614 write_objects(iiburst->iib_objts[i], buf);
617 h.cth_funcoff = ctf_buf_cur(buf);
618 for (i = 0; i < iiburst->iib_nfuncs; i++)
619 write_functions(iiburst->iib_funcs[i], buf);
622 h.cth_typeoff = ctf_buf_cur(buf);
623 (void) list_iter(iiburst->iib_types, write_type, buf);
625 debug(2, "CTF wrote %d types\n", list_count(iiburst->iib_types));
627 h.cth_stroff = ctf_buf_cur(buf);
628 h.cth_strlen = strtab_size(&buf->ctb_strtab);
631 * We only do compression for ctfmerge, as ctfconvert is only
632 * supposed to be used on intermediary build objects. This is
633 * significantly faster.
636 outbuf = write_compressed_buffer(&h, buf, resszp);
638 outbuf = write_buffer(&h, buf, resszp);
645 get_ctt_size(ctf_type_t *ctt, size_t *sizep, size_t *incrementp)
647 if (ctt->ctt_size == CTF_LSIZE_SENT) {
648 *sizep = (size_t)CTF_TYPE_LSIZE(ctt);
649 *incrementp = sizeof (ctf_type_t);
651 *sizep = ctt->ctt_size;
652 *incrementp = sizeof (ctf_stype_t);
657 count_types(ctf_header_t *h, caddr_t data)
659 caddr_t dptr = data + h->cth_typeoff;
662 dptr = data + h->cth_typeoff;
663 while (dptr < data + h->cth_stroff) {
664 void *v = (void *) dptr;
666 size_t vlen = CTF_INFO_VLEN(ctt->ctt_info);
667 size_t size, increment;
669 get_ctt_size(ctt, &size, &increment);
671 switch (CTF_INFO_KIND(ctt->ctt_info)) {
683 dptr += sizeof (ushort_t) * (vlen + (vlen & 1));
686 dptr += sizeof (ctf_array_t);
690 if (size < CTF_LSTRUCT_THRESH)
691 dptr += sizeof (ctf_member_t) * vlen;
693 dptr += sizeof (ctf_lmember_t) * vlen;
696 dptr += sizeof (ctf_enum_t) * vlen;
701 parseterminate("Unknown CTF type %d (#%d) at %#x",
702 CTF_INFO_KIND(ctt->ctt_info), count, dptr - data);
709 debug(3, "CTF read %d types\n", count);
715 * Resurrect the labels stored in the CTF data, returning the index associated
716 * with a label provided by the caller. There are several cases, outlined
717 * below. Note that, given two labels, the one associated with the lesser type
718 * index is considered to be older than the other.
720 * 1. matchlbl == NULL - return the index of the most recent label.
721 * 2. matchlbl == "BASE" - return the index of the oldest label.
722 * 3. matchlbl != NULL, but doesn't match any labels in the section - warn
723 * the user, and proceed as if matchlbl == "BASE" (for safety).
724 * 4. matchlbl != NULL, and matches one of the labels in the section - return
725 * the type index associated with the label.
728 resurrect_labels(ctf_header_t *h, tdata_t *td, caddr_t ctfdata, char *matchlbl)
730 caddr_t buf = ctfdata + h->cth_lbloff;
731 caddr_t sbuf = ctfdata + h->cth_stroff;
732 size_t bufsz = h->cth_objtoff - h->cth_lbloff;
733 int lastidx = 0, baseidx = -1;
734 char *baselabel = NULL;
736 void *v = (void *) buf;
738 for (ctl = v; (caddr_t)ctl < buf + bufsz; ctl++) {
739 char *label = sbuf + ctl->ctl_label;
741 lastidx = ctl->ctl_typeidx;
743 debug(3, "Resurrected label %s type idx %d\n", label, lastidx);
745 tdata_label_add(td, label, lastidx);
750 if (matchlbl != NULL && streq(matchlbl, "BASE"))
754 if (matchlbl != NULL && streq(label, matchlbl))
758 if (matchlbl != NULL) {
759 /* User provided a label that didn't match */
760 warning("%s: Cannot find label `%s' - using base (%s)\n",
761 curfile, matchlbl, (baselabel ? baselabel : "NONE"));
763 tdata_label_free(td);
764 tdata_label_add(td, baselabel, baseidx);
773 resurrect_objects(ctf_header_t *h, tdata_t *td, tdesc_t **tdarr, int tdsize,
774 caddr_t ctfdata, symit_data_t *si)
776 caddr_t buf = ctfdata + h->cth_objtoff;
777 size_t bufsz = h->cth_funcoff - h->cth_objtoff;
781 for (dptr = buf; dptr < buf + bufsz; dptr += 2) {
782 void *v = (void *) dptr;
783 ushort_t id = *((ushort_t *)v);
787 if (!(sym = symit_next(si, STT_OBJECT)) && id != 0) {
789 "Unexpected end of object symbols at %x of %x",
794 debug(3, "Skipping null object\n");
796 } else if (id >= tdsize) {
797 parseterminate("Reference to invalid type %d", id);
800 ii = iidesc_new(symit_name(si));
801 ii->ii_dtype = tdarr[id];
802 if (GELF_ST_BIND(sym->st_info) == STB_LOCAL) {
803 ii->ii_type = II_SVAR;
804 ii->ii_owner = xstrdup(symit_curfile(si));
806 ii->ii_type = II_GVAR;
807 hash_add(td->td_iihash, ii);
809 debug(3, "Resurrected %s object %s (%d) from %s\n",
810 (ii->ii_type == II_GVAR ? "global" : "static"),
811 ii->ii_name, id, (ii->ii_owner ? ii->ii_owner : "(none)"));
816 resurrect_functions(ctf_header_t *h, tdata_t *td, tdesc_t **tdarr, int tdsize,
817 caddr_t ctfdata, symit_data_t *si)
819 caddr_t buf = ctfdata + h->cth_funcoff;
820 size_t bufsz = h->cth_typeoff - h->cth_funcoff;
829 while (dptr < buf + bufsz) {
830 void *v = (void *) dptr;
831 info = *((ushort_t *)v);
834 if (!(sym = symit_next(si, STT_FUNC)) && info != 0)
835 parseterminate("Unexpected end of function symbols");
838 debug(3, "Skipping null function (%s)\n",
844 retid = *((ushort_t *)v);
848 parseterminate("Reference to invalid type %d", retid);
850 ii = iidesc_new(symit_name(si));
851 ii->ii_dtype = tdarr[retid];
852 if (GELF_ST_BIND(sym->st_info) == STB_LOCAL) {
853 ii->ii_type = II_SFUN;
854 ii->ii_owner = xstrdup(symit_curfile(si));
856 ii->ii_type = II_GFUN;
857 ii->ii_nargs = CTF_INFO_VLEN(info);
860 xmalloc(sizeof (tdesc_t *) * ii->ii_nargs);
862 for (i = 0; i < ii->ii_nargs; i++, dptr += 2) {
864 ushort_t id = *((ushort_t *)v);
866 parseterminate("Reference to invalid type %d",
868 ii->ii_args[i] = tdarr[id];
871 if (ii->ii_nargs && ii->ii_args[ii->ii_nargs - 1] == NULL) {
876 hash_add(td->td_iihash, ii);
878 debug(3, "Resurrected %s function %s (%d, %d args)\n",
879 (ii->ii_type == II_GFUN ? "global" : "static"),
880 ii->ii_name, retid, ii->ii_nargs);
885 resurrect_types(ctf_header_t *h, tdata_t *td, tdesc_t **tdarr, int tdsize,
886 caddr_t ctfdata, int maxid)
888 caddr_t buf = ctfdata + h->cth_typeoff;
889 size_t bufsz = h->cth_stroff - h->cth_typeoff;
890 caddr_t sbuf = ctfdata + h->cth_stroff;
895 size_t size, increment;
911 * A maxid of zero indicates a request to resurrect all types, so reset
912 * maxid to the maximum type id.
915 maxid = CTF_MAX_TYPE;
917 for (dptr = buf, tcnt = 0, tid = 1; dptr < buf + bufsz; tcnt++, tid++) {
922 parseterminate("Reference to invalid type %d", tid);
924 void *v = (void *) dptr;
927 get_ctt_size(ctt, &size, &increment);
932 if (CTF_NAME_STID(ctt->ctt_name) != CTF_STRTAB_0)
934 "Unable to cope with non-zero strtab id");
935 if (CTF_NAME_OFFSET(ctt->ctt_name) != 0) {
937 xstrdup(sbuf + CTF_NAME_OFFSET(ctt->ctt_name));
941 kind = CTF_INFO_KIND(ctt->ctt_info);
942 vlen = CTF_INFO_VLEN(ctt->ctt_info);
946 tdp->t_type = INTRINSIC;
950 data = *((uint_t *)v);
951 dptr += sizeof (uint_t);
952 encoding = CTF_INT_ENCODING(data);
954 ip = xmalloc(sizeof (intr_t));
955 ip->intr_type = INTR_INT;
956 ip->intr_signed = (encoding & CTF_INT_SIGNED) ? 1 : 0;
958 if (encoding & CTF_INT_CHAR)
959 ip->intr_iformat = 'c';
960 else if (encoding & CTF_INT_BOOL)
961 ip->intr_iformat = 'b';
962 else if (encoding & CTF_INT_VARARGS)
963 ip->intr_iformat = 'v';
965 ip->intr_iformat = '\0';
967 ip->intr_offset = CTF_INT_OFFSET(data);
968 ip->intr_nbits = CTF_INT_BITS(data);
973 tdp->t_type = INTRINSIC;
977 data = *((uint_t *)v);
978 dptr += sizeof (uint_t);
980 ip = xcalloc(sizeof (intr_t));
981 ip->intr_type = INTR_REAL;
982 ip->intr_fformat = CTF_FP_ENCODING(data);
983 ip->intr_offset = CTF_FP_OFFSET(data);
984 ip->intr_nbits = CTF_FP_BITS(data);
989 tdp->t_type = POINTER;
990 tdp->t_tdesc = tdarr[ctt->ctt_type];
999 dptr += sizeof (ctf_array_t);
1001 tdp->t_ardef = xmalloc(sizeof (ardef_t));
1002 tdp->t_ardef->ad_contents = tdarr[cta->cta_contents];
1003 tdp->t_ardef->ad_idxtype = tdarr[cta->cta_index];
1004 tdp->t_ardef->ad_nelems = cta->cta_nelems;
1009 tdp->t_type = (kind == CTF_K_STRUCT ? STRUCT : UNION);
1012 if (size < CTF_LSTRUCT_THRESH) {
1013 for (i = 0, mpp = &tdp->t_members; i < vlen;
1014 i++, mpp = &((*mpp)->ml_next)) {
1016 ctf_member_t *ctm = v;
1017 dptr += sizeof (ctf_member_t);
1019 *mpp = xmalloc(sizeof (mlist_t));
1020 (*mpp)->ml_name = xstrdup(sbuf +
1022 (*mpp)->ml_type = tdarr[ctm->ctm_type];
1023 (*mpp)->ml_offset = ctm->ctm_offset;
1024 (*mpp)->ml_size = 0;
1027 for (i = 0, mpp = &tdp->t_members; i < vlen;
1028 i++, mpp = &((*mpp)->ml_next)) {
1030 ctf_lmember_t *ctlm = v;
1031 dptr += sizeof (ctf_lmember_t);
1033 *mpp = xmalloc(sizeof (mlist_t));
1034 (*mpp)->ml_name = xstrdup(sbuf +
1037 tdarr[ctlm->ctlm_type];
1039 (int)CTF_LMEM_OFFSET(ctlm);
1040 (*mpp)->ml_size = 0;
1051 for (i = 0, epp = &tdp->t_emem; i < vlen;
1052 i++, epp = &((*epp)->el_next)) {
1055 dptr += sizeof (ctf_enum_t);
1057 *epp = xmalloc(sizeof (elist_t));
1058 (*epp)->el_name = xstrdup(sbuf + cte->cte_name);
1059 (*epp)->el_number = cte->cte_value;
1065 tdp->t_type = FORWARD;
1066 list_add(&td->td_fwdlist, tdp);
1070 tdp->t_type = TYPEDEF;
1071 tdp->t_tdesc = tdarr[ctt->ctt_type];
1074 case CTF_K_VOLATILE:
1075 tdp->t_type = VOLATILE;
1076 tdp->t_tdesc = tdarr[ctt->ctt_type];
1080 tdp->t_type = CONST;
1081 tdp->t_tdesc = tdarr[ctt->ctt_type];
1084 case CTF_K_FUNCTION:
1085 tdp->t_type = FUNCTION;
1086 tdp->t_fndef = xcalloc(sizeof (fndef_t));
1087 tdp->t_fndef->fn_ret = tdarr[ctt->ctt_type];
1089 v = (void *) (dptr + (sizeof (ushort_t) * (vlen - 1)));
1090 if (vlen > 0 && *(ushort_t *)v == 0)
1091 tdp->t_fndef->fn_vargs = 1;
1093 tdp->t_fndef->fn_nargs = vlen - tdp->t_fndef->fn_vargs;
1094 tdp->t_fndef->fn_args = xcalloc(sizeof (tdesc_t) *
1095 vlen - tdp->t_fndef->fn_vargs);
1097 for (i = 0; i < vlen; i++) {
1099 argid = *(ushort_t *)v;
1100 dptr += sizeof (ushort_t);
1103 tdp->t_fndef->fn_args[i] = tdarr[argid];
1107 dptr += sizeof (ushort_t);
1110 case CTF_K_RESTRICT:
1111 tdp->t_type = RESTRICT;
1112 tdp->t_tdesc = tdarr[ctt->ctt_type];
1119 warning("Can't parse unknown CTF type %d\n", kind);
1122 if (CTF_INFO_ISROOT(ctt->ctt_info)) {
1123 iidesc_t *ii = iidesc_new(tdp->t_name);
1124 if (tdp->t_type == STRUCT || tdp->t_type == UNION ||
1125 tdp->t_type == ENUM)
1126 ii->ii_type = II_SOU;
1128 ii->ii_type = II_TYPE;
1130 hash_add(td->td_iihash, ii);
1135 debug(3, "Resurrected %d %stype %s (%d)\n", tdp->t_type,
1136 (CTF_INFO_ISROOT(ctt->ctt_info) ? "root " : ""),
1137 tdesc_name(tdp), tdp->t_id);
1140 debug(3, "Resurrected %d types (%d were roots)\n", tcnt, iicnt);
1144 * For lack of other inspiration, we're going to take the boring route. We
1145 * count the number of types. This lets us malloc that many tdesc structs
1146 * before we start filling them in. This has the advantage of allowing us to
1147 * avoid a merge-esque remap step.
1150 ctf_parse(ctf_header_t *h, caddr_t buf, symit_data_t *si, char *label)
1152 tdata_t *td = tdata_new();
1154 int ntypes = count_types(h, buf);
1158 tdarr = xcalloc(sizeof (tdesc_t *) * (ntypes + 1));
1160 for (i = 1; i <= ntypes; i++) {
1161 tdarr[i] = xcalloc(sizeof (tdesc_t));
1165 td->td_parlabel = xstrdup(buf + h->cth_stroff + h->cth_parlabel);
1167 /* we have the technology - we can rebuild them */
1168 idx = resurrect_labels(h, td, buf, label);
1170 resurrect_objects(h, td, tdarr, ntypes + 1, buf, si);
1171 resurrect_functions(h, td, tdarr, ntypes + 1, buf, si);
1172 resurrect_types(h, td, tdarr, ntypes + 1, buf, idx);
1176 td->td_nextid = ntypes + 1;
1182 decompress_ctf(caddr_t cbuf, size_t cbufsz, caddr_t dbuf, size_t dbufsz)
1187 zstr.zalloc = (alloc_func)0;
1188 zstr.zfree = (free_func)0;
1189 zstr.opaque = (voidpf)0;
1191 zstr.next_in = (Bytef *)cbuf;
1192 zstr.avail_in = cbufsz;
1193 zstr.next_out = (Bytef *)dbuf;
1194 zstr.avail_out = dbufsz;
1196 if ((rc = inflateInit(&zstr)) != Z_OK ||
1197 (rc = inflate(&zstr, Z_NO_FLUSH)) != Z_STREAM_END ||
1198 (rc = inflateEnd(&zstr)) != Z_OK) {
1199 warning("CTF decompress zlib error %s\n", zError(rc));
1203 debug(3, "reflated %lu bytes to %lu, pointer at %d\n",
1204 zstr.total_in, zstr.total_out, (caddr_t)zstr.next_in - cbuf);
1206 return (zstr.total_out);
1210 * Reconstruct the type tree from a given buffer of CTF data. Only the types
1211 * up to the type associated with the provided label, inclusive, will be
1212 * reconstructed. If a NULL label is provided, all types will be reconstructed.
1214 * This function won't work on files that have been uniquified.
1217 ctf_load(char *file, caddr_t buf, size_t bufsz, symit_data_t *si, char *label)
1226 if (bufsz < sizeof (ctf_header_t))
1227 parseterminate("Corrupt CTF - short header");
1229 void *v = (void *) buf;
1231 buf += sizeof (ctf_header_t);
1232 bufsz -= sizeof (ctf_header_t);
1234 if (h->cth_magic != CTF_MAGIC)
1235 parseterminate("Corrupt CTF - bad magic 0x%x", h->cth_magic);
1237 if (h->cth_version != CTF_VERSION)
1238 parseterminate("Unknown CTF version %d", h->cth_version);
1240 ctfdatasz = h->cth_stroff + h->cth_strlen;
1241 if (h->cth_flags & CTF_F_COMPRESS) {
1244 ctfdata = xmalloc(ctfdatasz);
1245 if ((actual = decompress_ctf(buf, bufsz, ctfdata, ctfdatasz)) !=
1247 parseterminate("Corrupt CTF - short decompression "
1248 "(was %d, expecting %d)", actual, ctfdatasz);
1255 td = ctf_parse(h, ctfdata, si, label);
1257 if (h->cth_flags & CTF_F_COMPRESS)