17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
54d232658Sjohnlev * Common Development and Distribution License (the "License").
64d232658Sjohnlev * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
217c478bd9Sstevel@tonic-gate /*
221c4f4ba6SJohn Levon * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
237c478bd9Sstevel@tonic-gate * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate */
257c478bd9Sstevel@tonic-gate
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate * Create and parse buffers containing CTF data.
287c478bd9Sstevel@tonic-gate */
297c478bd9Sstevel@tonic-gate
307c478bd9Sstevel@tonic-gate #include <sys/types.h>
317c478bd9Sstevel@tonic-gate #include <stdio.h>
327c478bd9Sstevel@tonic-gate #include <stdlib.h>
337c478bd9Sstevel@tonic-gate #include <strings.h>
347c478bd9Sstevel@tonic-gate #include <ctype.h>
357c478bd9Sstevel@tonic-gate #include <zlib.h>
367c478bd9Sstevel@tonic-gate #include <elf.h>
377c478bd9Sstevel@tonic-gate
387c478bd9Sstevel@tonic-gate #include "ctf_headers.h"
397c478bd9Sstevel@tonic-gate #include "ctftools.h"
407c478bd9Sstevel@tonic-gate #include "strtab.h"
417c478bd9Sstevel@tonic-gate #include "memory.h"
427c478bd9Sstevel@tonic-gate
437c478bd9Sstevel@tonic-gate /*
447c478bd9Sstevel@tonic-gate * Name of the file currently being read, used to print error messages. We
457c478bd9Sstevel@tonic-gate * assume that only one file will be read at a time, and thus make no attempt
467c478bd9Sstevel@tonic-gate * to allow curfile to be used simultaneously by multiple threads.
477c478bd9Sstevel@tonic-gate *
487c478bd9Sstevel@tonic-gate * The value is only valid during a call to ctf_load.
497c478bd9Sstevel@tonic-gate */
507c478bd9Sstevel@tonic-gate char *curfile;
517c478bd9Sstevel@tonic-gate
527c478bd9Sstevel@tonic-gate #define CTF_BUF_CHUNK_SIZE (64 * 1024)
537c478bd9Sstevel@tonic-gate #define RES_BUF_CHUNK_SIZE (64 * 1024)
547c478bd9Sstevel@tonic-gate
557c478bd9Sstevel@tonic-gate struct ctf_buf {
567c478bd9Sstevel@tonic-gate strtab_t ctb_strtab; /* string table */
577c478bd9Sstevel@tonic-gate caddr_t ctb_base; /* pointer to base of buffer */
587c478bd9Sstevel@tonic-gate caddr_t ctb_end; /* pointer to end of buffer */
597c478bd9Sstevel@tonic-gate caddr_t ctb_ptr; /* pointer to empty buffer space */
607c478bd9Sstevel@tonic-gate size_t ctb_size; /* size of buffer */
617c478bd9Sstevel@tonic-gate int nptent; /* number of processed types */
627c478bd9Sstevel@tonic-gate int ntholes; /* number of type holes */
637c478bd9Sstevel@tonic-gate };
647c478bd9Sstevel@tonic-gate
657c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
667c478bd9Sstevel@tonic-gate static void
parseterminate(char * fmt,...)677c478bd9Sstevel@tonic-gate parseterminate(char *fmt, ...)
687c478bd9Sstevel@tonic-gate {
697c478bd9Sstevel@tonic-gate static char msgbuf[1024]; /* sigh */
707c478bd9Sstevel@tonic-gate va_list ap;
717c478bd9Sstevel@tonic-gate
727c478bd9Sstevel@tonic-gate va_start(ap, fmt);
737c478bd9Sstevel@tonic-gate vsnprintf(msgbuf, sizeof (msgbuf), fmt, ap);
747c478bd9Sstevel@tonic-gate va_end(ap);
757c478bd9Sstevel@tonic-gate
767c478bd9Sstevel@tonic-gate terminate("%s: %s\n", curfile, msgbuf);
777c478bd9Sstevel@tonic-gate }
787c478bd9Sstevel@tonic-gate
797c478bd9Sstevel@tonic-gate void
ctf_buf_grow(ctf_buf_t * b)807c478bd9Sstevel@tonic-gate ctf_buf_grow(ctf_buf_t *b)
817c478bd9Sstevel@tonic-gate {
827c478bd9Sstevel@tonic-gate off_t ptroff = b->ctb_ptr - b->ctb_base;
837c478bd9Sstevel@tonic-gate
847c478bd9Sstevel@tonic-gate b->ctb_size += CTF_BUF_CHUNK_SIZE;
857c478bd9Sstevel@tonic-gate b->ctb_base = xrealloc(b->ctb_base, b->ctb_size);
867c478bd9Sstevel@tonic-gate b->ctb_end = b->ctb_base + b->ctb_size;
877c478bd9Sstevel@tonic-gate b->ctb_ptr = b->ctb_base + ptroff;
887c478bd9Sstevel@tonic-gate }
897c478bd9Sstevel@tonic-gate
907c478bd9Sstevel@tonic-gate ctf_buf_t *
ctf_buf_new(void)917c478bd9Sstevel@tonic-gate ctf_buf_new(void)
927c478bd9Sstevel@tonic-gate {
937c478bd9Sstevel@tonic-gate ctf_buf_t *b = xcalloc(sizeof (ctf_buf_t));
947c478bd9Sstevel@tonic-gate
957c478bd9Sstevel@tonic-gate strtab_create(&b->ctb_strtab);
967c478bd9Sstevel@tonic-gate ctf_buf_grow(b);
977c478bd9Sstevel@tonic-gate
987c478bd9Sstevel@tonic-gate return (b);
997c478bd9Sstevel@tonic-gate }
1007c478bd9Sstevel@tonic-gate
1017c478bd9Sstevel@tonic-gate void
ctf_buf_free(ctf_buf_t * b)1027c478bd9Sstevel@tonic-gate ctf_buf_free(ctf_buf_t *b)
1037c478bd9Sstevel@tonic-gate {
1047c478bd9Sstevel@tonic-gate strtab_destroy(&b->ctb_strtab);
1057c478bd9Sstevel@tonic-gate free(b->ctb_base);
1067c478bd9Sstevel@tonic-gate free(b);
1077c478bd9Sstevel@tonic-gate }
1087c478bd9Sstevel@tonic-gate
1097c478bd9Sstevel@tonic-gate uint_t
ctf_buf_cur(ctf_buf_t * b)1107c478bd9Sstevel@tonic-gate ctf_buf_cur(ctf_buf_t *b)
1117c478bd9Sstevel@tonic-gate {
1127c478bd9Sstevel@tonic-gate return (b->ctb_ptr - b->ctb_base);
1137c478bd9Sstevel@tonic-gate }
1147c478bd9Sstevel@tonic-gate
1157c478bd9Sstevel@tonic-gate void
ctf_buf_write(ctf_buf_t * b,const void * p,size_t n)1167c478bd9Sstevel@tonic-gate ctf_buf_write(ctf_buf_t *b, const void *p, size_t n)
1177c478bd9Sstevel@tonic-gate {
1187c478bd9Sstevel@tonic-gate size_t len;
1197c478bd9Sstevel@tonic-gate
1207c478bd9Sstevel@tonic-gate while (n != 0) {
1217c478bd9Sstevel@tonic-gate if (b->ctb_ptr == b->ctb_end)
1227c478bd9Sstevel@tonic-gate ctf_buf_grow(b);
1237c478bd9Sstevel@tonic-gate
1247c478bd9Sstevel@tonic-gate len = MIN((size_t)(b->ctb_end - b->ctb_ptr), n);
1257c478bd9Sstevel@tonic-gate bcopy(p, b->ctb_ptr, len);
1267c478bd9Sstevel@tonic-gate b->ctb_ptr += len;
1277c478bd9Sstevel@tonic-gate
1287c478bd9Sstevel@tonic-gate p = (char *)p + len;
1297c478bd9Sstevel@tonic-gate n -= len;
1307c478bd9Sstevel@tonic-gate }
1317c478bd9Sstevel@tonic-gate }
1327c478bd9Sstevel@tonic-gate
1337c478bd9Sstevel@tonic-gate static int
write_label(labelent_t * le,ctf_buf_t * b)1347c478bd9Sstevel@tonic-gate write_label(labelent_t *le, ctf_buf_t *b)
1357c478bd9Sstevel@tonic-gate {
1367c478bd9Sstevel@tonic-gate ctf_lblent_t ctl;
1377c478bd9Sstevel@tonic-gate
1387c478bd9Sstevel@tonic-gate ctl.ctl_label = strtab_insert(&b->ctb_strtab, le->le_name);
1397c478bd9Sstevel@tonic-gate ctl.ctl_typeidx = le->le_idx;
1407c478bd9Sstevel@tonic-gate
1417c478bd9Sstevel@tonic-gate ctf_buf_write(b, &ctl, sizeof (ctl));
1427c478bd9Sstevel@tonic-gate
1437c478bd9Sstevel@tonic-gate return (1);
1447c478bd9Sstevel@tonic-gate }
1457c478bd9Sstevel@tonic-gate
1467c478bd9Sstevel@tonic-gate static void
write_objects(iidesc_t * idp,ctf_buf_t * b)1477c478bd9Sstevel@tonic-gate write_objects(iidesc_t *idp, ctf_buf_t *b)
1487c478bd9Sstevel@tonic-gate {
1497c478bd9Sstevel@tonic-gate ushort_t id = (idp ? idp->ii_dtype->t_id : 0);
1507c478bd9Sstevel@tonic-gate
1517c478bd9Sstevel@tonic-gate ctf_buf_write(b, &id, sizeof (id));
1527c478bd9Sstevel@tonic-gate
1537c478bd9Sstevel@tonic-gate debug(3, "Wrote object %s (%d)\n", (idp ? idp->ii_name : "(null)"), id);
1547c478bd9Sstevel@tonic-gate }
1557c478bd9Sstevel@tonic-gate
1567c478bd9Sstevel@tonic-gate static void
write_functions(iidesc_t * idp,ctf_buf_t * b)1577c478bd9Sstevel@tonic-gate write_functions(iidesc_t *idp, ctf_buf_t *b)
1587c478bd9Sstevel@tonic-gate {
1597c478bd9Sstevel@tonic-gate ushort_t fdata[2];
1607c478bd9Sstevel@tonic-gate ushort_t id;
1617c478bd9Sstevel@tonic-gate int nargs;
1627c478bd9Sstevel@tonic-gate int i;
1637c478bd9Sstevel@tonic-gate
1647c478bd9Sstevel@tonic-gate if (!idp) {
1657c478bd9Sstevel@tonic-gate fdata[0] = 0;
1667c478bd9Sstevel@tonic-gate ctf_buf_write(b, &fdata[0], sizeof (fdata[0]));
1677c478bd9Sstevel@tonic-gate
1687c478bd9Sstevel@tonic-gate debug(3, "Wrote function (null)\n");
1697c478bd9Sstevel@tonic-gate return;
1707c478bd9Sstevel@tonic-gate }
1717c478bd9Sstevel@tonic-gate
1727c478bd9Sstevel@tonic-gate nargs = idp->ii_nargs + (idp->ii_vargs != 0);
1731c4f4ba6SJohn Levon
1741c4f4ba6SJohn Levon if (nargs > CTF_MAX_VLEN) {
1751c4f4ba6SJohn Levon terminate("function %s has too many args: %d > %d\n",
1761c4f4ba6SJohn Levon idp->ii_name, nargs, CTF_MAX_VLEN);
1771c4f4ba6SJohn Levon }
1781c4f4ba6SJohn Levon
1797c478bd9Sstevel@tonic-gate fdata[0] = CTF_TYPE_INFO(CTF_K_FUNCTION, 1, nargs);
1807c478bd9Sstevel@tonic-gate fdata[1] = idp->ii_dtype->t_id;
1817c478bd9Sstevel@tonic-gate ctf_buf_write(b, fdata, sizeof (fdata));
1827c478bd9Sstevel@tonic-gate
1837c478bd9Sstevel@tonic-gate for (i = 0; i < idp->ii_nargs; i++) {
1847c478bd9Sstevel@tonic-gate id = idp->ii_args[i]->t_id;
1857c478bd9Sstevel@tonic-gate ctf_buf_write(b, &id, sizeof (id));
1867c478bd9Sstevel@tonic-gate }
1877c478bd9Sstevel@tonic-gate
1887c478bd9Sstevel@tonic-gate if (idp->ii_vargs) {
1897c478bd9Sstevel@tonic-gate id = 0;
1907c478bd9Sstevel@tonic-gate ctf_buf_write(b, &id, sizeof (id));
1917c478bd9Sstevel@tonic-gate }
1927c478bd9Sstevel@tonic-gate
1937c478bd9Sstevel@tonic-gate debug(3, "Wrote function %s (%d args)\n", idp->ii_name, nargs);
1947c478bd9Sstevel@tonic-gate }
1957c478bd9Sstevel@tonic-gate
1967c478bd9Sstevel@tonic-gate /*
1977c478bd9Sstevel@tonic-gate * Depending on the size of the type being described, either a ctf_stype_t (for
1987c478bd9Sstevel@tonic-gate * types with size < CTF_LSTRUCT_THRESH) or a ctf_type_t (all others) will be
1997c478bd9Sstevel@tonic-gate * written. We isolate the determination here so the rest of the writer code
2007c478bd9Sstevel@tonic-gate * doesn't need to care.
2017c478bd9Sstevel@tonic-gate */
2027c478bd9Sstevel@tonic-gate static void
write_sized_type_rec(ctf_buf_t * b,ctf_type_t * ctt,size_t size)2037c478bd9Sstevel@tonic-gate write_sized_type_rec(ctf_buf_t *b, ctf_type_t *ctt, size_t size)
2047c478bd9Sstevel@tonic-gate {
2057c478bd9Sstevel@tonic-gate if (size > CTF_MAX_SIZE) {
2067c478bd9Sstevel@tonic-gate ctt->ctt_size = CTF_LSIZE_SENT;
2077c478bd9Sstevel@tonic-gate ctt->ctt_lsizehi = CTF_SIZE_TO_LSIZE_HI(size);
2087c478bd9Sstevel@tonic-gate ctt->ctt_lsizelo = CTF_SIZE_TO_LSIZE_LO(size);
2097c478bd9Sstevel@tonic-gate ctf_buf_write(b, ctt, sizeof (*ctt));
2107c478bd9Sstevel@tonic-gate } else {
2117c478bd9Sstevel@tonic-gate ctf_stype_t *cts = (ctf_stype_t *)ctt;
2127c478bd9Sstevel@tonic-gate
2137c478bd9Sstevel@tonic-gate cts->ctt_size = (ushort_t)size;
2147c478bd9Sstevel@tonic-gate ctf_buf_write(b, cts, sizeof (*cts));
2157c478bd9Sstevel@tonic-gate }
2167c478bd9Sstevel@tonic-gate }
2177c478bd9Sstevel@tonic-gate
2187c478bd9Sstevel@tonic-gate static void
write_unsized_type_rec(ctf_buf_t * b,ctf_type_t * ctt)2197c478bd9Sstevel@tonic-gate write_unsized_type_rec(ctf_buf_t *b, ctf_type_t *ctt)
2207c478bd9Sstevel@tonic-gate {
2217c478bd9Sstevel@tonic-gate ctf_stype_t *cts = (ctf_stype_t *)ctt;
2227c478bd9Sstevel@tonic-gate
2237c478bd9Sstevel@tonic-gate ctf_buf_write(b, cts, sizeof (*cts));
2247c478bd9Sstevel@tonic-gate }
2257c478bd9Sstevel@tonic-gate
2267c478bd9Sstevel@tonic-gate static int
write_type(tdesc_t * tp,ctf_buf_t * b)2277c478bd9Sstevel@tonic-gate write_type(tdesc_t *tp, ctf_buf_t *b)
2287c478bd9Sstevel@tonic-gate {
2297c478bd9Sstevel@tonic-gate elist_t *ep;
2307c478bd9Sstevel@tonic-gate mlist_t *mp;
2317c478bd9Sstevel@tonic-gate intr_t *ip;
2327c478bd9Sstevel@tonic-gate
2337c478bd9Sstevel@tonic-gate size_t offset;
2347c478bd9Sstevel@tonic-gate uint_t encoding;
2357c478bd9Sstevel@tonic-gate uint_t data;
2367c478bd9Sstevel@tonic-gate int isroot = tp->t_flags & TDESC_F_ISROOT;
2377c478bd9Sstevel@tonic-gate int i;
2387c478bd9Sstevel@tonic-gate
2397c478bd9Sstevel@tonic-gate ctf_type_t ctt;
2407c478bd9Sstevel@tonic-gate ctf_array_t cta;
2417c478bd9Sstevel@tonic-gate ctf_member_t ctm;
2427c478bd9Sstevel@tonic-gate ctf_lmember_t ctlm;
2437c478bd9Sstevel@tonic-gate ctf_enum_t cte;
2447c478bd9Sstevel@tonic-gate ushort_t id;
2457c478bd9Sstevel@tonic-gate
2467c478bd9Sstevel@tonic-gate ctlm.ctlm_pad = 0;
2477c478bd9Sstevel@tonic-gate
2487c478bd9Sstevel@tonic-gate /*
2497c478bd9Sstevel@tonic-gate * There shouldn't be any holes in the type list (where a hole is
2507c478bd9Sstevel@tonic-gate * defined as two consecutive tdescs without consecutive ids), but
2517c478bd9Sstevel@tonic-gate * check for them just in case. If we do find holes, we need to make
2527c478bd9Sstevel@tonic-gate * fake entries to fill the holes, or we won't be able to reconstruct
2537c478bd9Sstevel@tonic-gate * the tree from the written data.
2547c478bd9Sstevel@tonic-gate */
2557c478bd9Sstevel@tonic-gate if (++b->nptent < CTF_TYPE_TO_INDEX(tp->t_id)) {
2567c478bd9Sstevel@tonic-gate debug(2, "genctf: type hole from %d < x < %d\n",
2577c478bd9Sstevel@tonic-gate b->nptent - 1, CTF_TYPE_TO_INDEX(tp->t_id));
2587c478bd9Sstevel@tonic-gate
2597c478bd9Sstevel@tonic-gate ctt.ctt_name = CTF_TYPE_NAME(CTF_STRTAB_0, 0);
2607c478bd9Sstevel@tonic-gate ctt.ctt_info = CTF_TYPE_INFO(0, 0, 0);
2617c478bd9Sstevel@tonic-gate while (b->nptent < CTF_TYPE_TO_INDEX(tp->t_id)) {
2627c478bd9Sstevel@tonic-gate write_sized_type_rec(b, &ctt, 0);
2637c478bd9Sstevel@tonic-gate b->nptent++;
2647c478bd9Sstevel@tonic-gate }
2657c478bd9Sstevel@tonic-gate }
2667c478bd9Sstevel@tonic-gate
2677c478bd9Sstevel@tonic-gate offset = strtab_insert(&b->ctb_strtab, tp->t_name);
2687c478bd9Sstevel@tonic-gate ctt.ctt_name = CTF_TYPE_NAME(CTF_STRTAB_0, offset);
2697c478bd9Sstevel@tonic-gate
2707c478bd9Sstevel@tonic-gate switch (tp->t_type) {
2717c478bd9Sstevel@tonic-gate case INTRINSIC:
2727c478bd9Sstevel@tonic-gate ip = tp->t_intr;
2737c478bd9Sstevel@tonic-gate if (ip->intr_type == INTR_INT)
2747c478bd9Sstevel@tonic-gate ctt.ctt_info = CTF_TYPE_INFO(CTF_K_INTEGER,
2757c478bd9Sstevel@tonic-gate isroot, 1);
2767c478bd9Sstevel@tonic-gate else
2777c478bd9Sstevel@tonic-gate ctt.ctt_info = CTF_TYPE_INFO(CTF_K_FLOAT, isroot, 1);
2787c478bd9Sstevel@tonic-gate write_sized_type_rec(b, &ctt, tp->t_size);
2797c478bd9Sstevel@tonic-gate
2807c478bd9Sstevel@tonic-gate encoding = 0;
2817c478bd9Sstevel@tonic-gate
2827c478bd9Sstevel@tonic-gate if (ip->intr_type == INTR_INT) {
2837c478bd9Sstevel@tonic-gate if (ip->intr_signed)
2847c478bd9Sstevel@tonic-gate encoding |= CTF_INT_SIGNED;
2857c478bd9Sstevel@tonic-gate if (ip->intr_iformat == 'c')
2867c478bd9Sstevel@tonic-gate encoding |= CTF_INT_CHAR;
2877c478bd9Sstevel@tonic-gate else if (ip->intr_iformat == 'b')
2887c478bd9Sstevel@tonic-gate encoding |= CTF_INT_BOOL;
2897c478bd9Sstevel@tonic-gate else if (ip->intr_iformat == 'v')
2907c478bd9Sstevel@tonic-gate encoding |= CTF_INT_VARARGS;
2917c478bd9Sstevel@tonic-gate } else
2927c478bd9Sstevel@tonic-gate encoding = ip->intr_fformat;
2937c478bd9Sstevel@tonic-gate
2947c478bd9Sstevel@tonic-gate data = CTF_INT_DATA(encoding, ip->intr_offset, ip->intr_nbits);
2957c478bd9Sstevel@tonic-gate ctf_buf_write(b, &data, sizeof (data));
2967c478bd9Sstevel@tonic-gate break;
2977c478bd9Sstevel@tonic-gate
2987c478bd9Sstevel@tonic-gate case POINTER:
2997c478bd9Sstevel@tonic-gate ctt.ctt_info = CTF_TYPE_INFO(CTF_K_POINTER, isroot, 0);
3007c478bd9Sstevel@tonic-gate ctt.ctt_type = tp->t_tdesc->t_id;
3017c478bd9Sstevel@tonic-gate write_unsized_type_rec(b, &ctt);
3027c478bd9Sstevel@tonic-gate break;
3037c478bd9Sstevel@tonic-gate
3047c478bd9Sstevel@tonic-gate case ARRAY:
3057c478bd9Sstevel@tonic-gate ctt.ctt_info = CTF_TYPE_INFO(CTF_K_ARRAY, isroot, 1);
3067c478bd9Sstevel@tonic-gate write_sized_type_rec(b, &ctt, tp->t_size);
3077c478bd9Sstevel@tonic-gate
3087c478bd9Sstevel@tonic-gate cta.cta_contents = tp->t_ardef->ad_contents->t_id;
3097c478bd9Sstevel@tonic-gate cta.cta_index = tp->t_ardef->ad_idxtype->t_id;
3107c478bd9Sstevel@tonic-gate cta.cta_nelems = tp->t_ardef->ad_nelems;
3117c478bd9Sstevel@tonic-gate ctf_buf_write(b, &cta, sizeof (cta));
3127c478bd9Sstevel@tonic-gate break;
3137c478bd9Sstevel@tonic-gate
3147c478bd9Sstevel@tonic-gate case STRUCT:
3157c478bd9Sstevel@tonic-gate case UNION:
3167c478bd9Sstevel@tonic-gate for (i = 0, mp = tp->t_members; mp != NULL; mp = mp->ml_next)
3177c478bd9Sstevel@tonic-gate i++; /* count up struct or union members */
3187c478bd9Sstevel@tonic-gate
3191c4f4ba6SJohn Levon if (i > CTF_MAX_VLEN) {
3201c4f4ba6SJohn Levon terminate("sou %s has too many members: %d > %d\n",
3211c4f4ba6SJohn Levon tdesc_name(tp), i, CTF_MAX_VLEN);
3221c4f4ba6SJohn Levon }
3231c4f4ba6SJohn Levon
3247c478bd9Sstevel@tonic-gate if (tp->t_type == STRUCT)
3257c478bd9Sstevel@tonic-gate ctt.ctt_info = CTF_TYPE_INFO(CTF_K_STRUCT, isroot, i);
3267c478bd9Sstevel@tonic-gate else
3277c478bd9Sstevel@tonic-gate ctt.ctt_info = CTF_TYPE_INFO(CTF_K_UNION, isroot, i);
3287c478bd9Sstevel@tonic-gate
3297c478bd9Sstevel@tonic-gate write_sized_type_rec(b, &ctt, tp->t_size);
3307c478bd9Sstevel@tonic-gate
3317c478bd9Sstevel@tonic-gate if (tp->t_size < CTF_LSTRUCT_THRESH) {
3327c478bd9Sstevel@tonic-gate for (mp = tp->t_members; mp != NULL; mp = mp->ml_next) {
3337c478bd9Sstevel@tonic-gate offset = strtab_insert(&b->ctb_strtab,
3347c478bd9Sstevel@tonic-gate mp->ml_name);
3357c478bd9Sstevel@tonic-gate
3367c478bd9Sstevel@tonic-gate ctm.ctm_name = CTF_TYPE_NAME(CTF_STRTAB_0,
3377c478bd9Sstevel@tonic-gate offset);
3387c478bd9Sstevel@tonic-gate ctm.ctm_type = mp->ml_type->t_id;
3397c478bd9Sstevel@tonic-gate ctm.ctm_offset = mp->ml_offset;
3407c478bd9Sstevel@tonic-gate ctf_buf_write(b, &ctm, sizeof (ctm));
3417c478bd9Sstevel@tonic-gate }
3427c478bd9Sstevel@tonic-gate } else {
3437c478bd9Sstevel@tonic-gate for (mp = tp->t_members; mp != NULL; mp = mp->ml_next) {
3447c478bd9Sstevel@tonic-gate offset = strtab_insert(&b->ctb_strtab,
3457c478bd9Sstevel@tonic-gate mp->ml_name);
3467c478bd9Sstevel@tonic-gate
3477c478bd9Sstevel@tonic-gate ctlm.ctlm_name = CTF_TYPE_NAME(CTF_STRTAB_0,
3487c478bd9Sstevel@tonic-gate offset);
3497c478bd9Sstevel@tonic-gate ctlm.ctlm_type = mp->ml_type->t_id;
3507c478bd9Sstevel@tonic-gate ctlm.ctlm_offsethi =
3517c478bd9Sstevel@tonic-gate CTF_OFFSET_TO_LMEMHI(mp->ml_offset);
3527c478bd9Sstevel@tonic-gate ctlm.ctlm_offsetlo =
3537c478bd9Sstevel@tonic-gate CTF_OFFSET_TO_LMEMLO(mp->ml_offset);
3547c478bd9Sstevel@tonic-gate ctf_buf_write(b, &ctlm, sizeof (ctlm));
3557c478bd9Sstevel@tonic-gate }
3567c478bd9Sstevel@tonic-gate }
3577c478bd9Sstevel@tonic-gate break;
3587c478bd9Sstevel@tonic-gate
3597c478bd9Sstevel@tonic-gate case ENUM:
3607c478bd9Sstevel@tonic-gate for (i = 0, ep = tp->t_emem; ep != NULL; ep = ep->el_next)
3617c478bd9Sstevel@tonic-gate i++; /* count up enum members */
3627c478bd9Sstevel@tonic-gate
3631c4f4ba6SJohn Levon if (i > CTF_MAX_VLEN) {
3641c4f4ba6SJohn Levon terminate("enum %s has too many values: %d > %d\n",
3651c4f4ba6SJohn Levon tdesc_name(tp), i, CTF_MAX_VLEN);
3661c4f4ba6SJohn Levon }
3671c4f4ba6SJohn Levon
3687c478bd9Sstevel@tonic-gate ctt.ctt_info = CTF_TYPE_INFO(CTF_K_ENUM, isroot, i);
3697c478bd9Sstevel@tonic-gate write_sized_type_rec(b, &ctt, tp->t_size);
3707c478bd9Sstevel@tonic-gate
3717c478bd9Sstevel@tonic-gate for (ep = tp->t_emem; ep != NULL; ep = ep->el_next) {
3727c478bd9Sstevel@tonic-gate offset = strtab_insert(&b->ctb_strtab, ep->el_name);
3737c478bd9Sstevel@tonic-gate cte.cte_name = CTF_TYPE_NAME(CTF_STRTAB_0, offset);
3747c478bd9Sstevel@tonic-gate cte.cte_value = ep->el_number;
3757c478bd9Sstevel@tonic-gate ctf_buf_write(b, &cte, sizeof (cte));
3767c478bd9Sstevel@tonic-gate }
3777c478bd9Sstevel@tonic-gate break;
3787c478bd9Sstevel@tonic-gate
3797c478bd9Sstevel@tonic-gate case FORWARD:
3807c478bd9Sstevel@tonic-gate ctt.ctt_info = CTF_TYPE_INFO(CTF_K_FORWARD, isroot, 0);
3817c478bd9Sstevel@tonic-gate ctt.ctt_type = 0;
3827c478bd9Sstevel@tonic-gate write_unsized_type_rec(b, &ctt);
3837c478bd9Sstevel@tonic-gate break;
3847c478bd9Sstevel@tonic-gate
3857c478bd9Sstevel@tonic-gate case TYPEDEF:
3867c478bd9Sstevel@tonic-gate ctt.ctt_info = CTF_TYPE_INFO(CTF_K_TYPEDEF, isroot, 0);
3877c478bd9Sstevel@tonic-gate ctt.ctt_type = tp->t_tdesc->t_id;
3887c478bd9Sstevel@tonic-gate write_unsized_type_rec(b, &ctt);
3897c478bd9Sstevel@tonic-gate break;
3907c478bd9Sstevel@tonic-gate
3917c478bd9Sstevel@tonic-gate case VOLATILE:
3927c478bd9Sstevel@tonic-gate ctt.ctt_info = CTF_TYPE_INFO(CTF_K_VOLATILE, isroot, 0);
3937c478bd9Sstevel@tonic-gate ctt.ctt_type = tp->t_tdesc->t_id;
3947c478bd9Sstevel@tonic-gate write_unsized_type_rec(b, &ctt);
3957c478bd9Sstevel@tonic-gate break;
3967c478bd9Sstevel@tonic-gate
3977c478bd9Sstevel@tonic-gate case CONST:
3987c478bd9Sstevel@tonic-gate ctt.ctt_info = CTF_TYPE_INFO(CTF_K_CONST, isroot, 0);
3997c478bd9Sstevel@tonic-gate ctt.ctt_type = tp->t_tdesc->t_id;
4007c478bd9Sstevel@tonic-gate write_unsized_type_rec(b, &ctt);
4017c478bd9Sstevel@tonic-gate break;
4027c478bd9Sstevel@tonic-gate
4037c478bd9Sstevel@tonic-gate case FUNCTION:
4041c4f4ba6SJohn Levon i = tp->t_fndef->fn_nargs + tp->t_fndef->fn_vargs;
4051c4f4ba6SJohn Levon
4061c4f4ba6SJohn Levon if (i > CTF_MAX_VLEN) {
4071c4f4ba6SJohn Levon terminate("function %s has too many args: %d > %d\n",
4081c4f4ba6SJohn Levon i, CTF_MAX_VLEN);
4091c4f4ba6SJohn Levon }
4101c4f4ba6SJohn Levon
4111c4f4ba6SJohn Levon ctt.ctt_info = CTF_TYPE_INFO(CTF_K_FUNCTION, isroot, i);
4127c478bd9Sstevel@tonic-gate ctt.ctt_type = tp->t_fndef->fn_ret->t_id;
4137c478bd9Sstevel@tonic-gate write_unsized_type_rec(b, &ctt);
4147c478bd9Sstevel@tonic-gate
4157c478bd9Sstevel@tonic-gate for (i = 0; i < tp->t_fndef->fn_nargs; i++) {
4167c478bd9Sstevel@tonic-gate id = tp->t_fndef->fn_args[i]->t_id;
4177c478bd9Sstevel@tonic-gate ctf_buf_write(b, &id, sizeof (id));
4187c478bd9Sstevel@tonic-gate }
4197c478bd9Sstevel@tonic-gate
4207c478bd9Sstevel@tonic-gate if (tp->t_fndef->fn_vargs) {
4217c478bd9Sstevel@tonic-gate id = 0;
4227c478bd9Sstevel@tonic-gate ctf_buf_write(b, &id, sizeof (id));
4237c478bd9Sstevel@tonic-gate i++;
4247c478bd9Sstevel@tonic-gate }
4257c478bd9Sstevel@tonic-gate
4267c478bd9Sstevel@tonic-gate if (i & 1) {
4277c478bd9Sstevel@tonic-gate id = 0;
4287c478bd9Sstevel@tonic-gate ctf_buf_write(b, &id, sizeof (id));
4297c478bd9Sstevel@tonic-gate }
4307c478bd9Sstevel@tonic-gate break;
4317c478bd9Sstevel@tonic-gate
4327c478bd9Sstevel@tonic-gate case RESTRICT:
4337c478bd9Sstevel@tonic-gate ctt.ctt_info = CTF_TYPE_INFO(CTF_K_RESTRICT, isroot, 0);
4347c478bd9Sstevel@tonic-gate ctt.ctt_type = tp->t_tdesc->t_id;
4357c478bd9Sstevel@tonic-gate write_unsized_type_rec(b, &ctt);
4367c478bd9Sstevel@tonic-gate break;
4377c478bd9Sstevel@tonic-gate
4387c478bd9Sstevel@tonic-gate default:
4397c478bd9Sstevel@tonic-gate warning("Can't write unknown type %d\n", tp->t_type);
4407c478bd9Sstevel@tonic-gate }
4417c478bd9Sstevel@tonic-gate
4424d232658Sjohnlev debug(3, "Wrote type %d %s\n", tp->t_id, tdesc_name(tp));
4437c478bd9Sstevel@tonic-gate
4447c478bd9Sstevel@tonic-gate return (1);
4457c478bd9Sstevel@tonic-gate }
4467c478bd9Sstevel@tonic-gate
4477c478bd9Sstevel@tonic-gate typedef struct resbuf {
4487c478bd9Sstevel@tonic-gate caddr_t rb_base;
4497c478bd9Sstevel@tonic-gate caddr_t rb_ptr;
4507c478bd9Sstevel@tonic-gate size_t rb_size;
4517c478bd9Sstevel@tonic-gate z_stream rb_zstr;
4527c478bd9Sstevel@tonic-gate } resbuf_t;
4537c478bd9Sstevel@tonic-gate
4547c478bd9Sstevel@tonic-gate static void
rbzs_grow(resbuf_t * rb)4557c478bd9Sstevel@tonic-gate rbzs_grow(resbuf_t *rb)
4567c478bd9Sstevel@tonic-gate {
4577c478bd9Sstevel@tonic-gate off_t ptroff = (caddr_t)rb->rb_zstr.next_out - rb->rb_base;
4587c478bd9Sstevel@tonic-gate
4597c478bd9Sstevel@tonic-gate rb->rb_size += RES_BUF_CHUNK_SIZE;
4607c478bd9Sstevel@tonic-gate rb->rb_base = xrealloc(rb->rb_base, rb->rb_size);
4617c478bd9Sstevel@tonic-gate rb->rb_ptr = rb->rb_base + ptroff;
4627c478bd9Sstevel@tonic-gate rb->rb_zstr.next_out = (Bytef *)(rb->rb_ptr);
4637c478bd9Sstevel@tonic-gate rb->rb_zstr.avail_out += RES_BUF_CHUNK_SIZE;
4647c478bd9Sstevel@tonic-gate }
4657c478bd9Sstevel@tonic-gate
4667c478bd9Sstevel@tonic-gate static void
compress_start(resbuf_t * rb)4677c478bd9Sstevel@tonic-gate compress_start(resbuf_t *rb)
4687c478bd9Sstevel@tonic-gate {
4697c478bd9Sstevel@tonic-gate int rc;
4707c478bd9Sstevel@tonic-gate
4717c478bd9Sstevel@tonic-gate rb->rb_zstr.zalloc = (alloc_func)0;
4727c478bd9Sstevel@tonic-gate rb->rb_zstr.zfree = (free_func)0;
4737c478bd9Sstevel@tonic-gate rb->rb_zstr.opaque = (voidpf)0;
4747c478bd9Sstevel@tonic-gate
4757c478bd9Sstevel@tonic-gate if ((rc = deflateInit(&rb->rb_zstr, Z_BEST_COMPRESSION)) != Z_OK)
4767c478bd9Sstevel@tonic-gate parseterminate("zlib start failed: %s", zError(rc));
4777c478bd9Sstevel@tonic-gate }
4787c478bd9Sstevel@tonic-gate
4794d232658Sjohnlev static ssize_t
compress_buffer(const void * buf,size_t n,void * data)4804d232658Sjohnlev compress_buffer(const void *buf, size_t n, void *data)
4817c478bd9Sstevel@tonic-gate {
4824d232658Sjohnlev resbuf_t *rb = (resbuf_t *)data;
4837c478bd9Sstevel@tonic-gate int rc;
4847c478bd9Sstevel@tonic-gate
4857c478bd9Sstevel@tonic-gate rb->rb_zstr.next_out = (Bytef *)rb->rb_ptr;
4867c478bd9Sstevel@tonic-gate rb->rb_zstr.avail_out = rb->rb_size - (rb->rb_ptr - rb->rb_base);
4877c478bd9Sstevel@tonic-gate rb->rb_zstr.next_in = (Bytef *)buf;
4887c478bd9Sstevel@tonic-gate rb->rb_zstr.avail_in = n;
4897c478bd9Sstevel@tonic-gate
4907c478bd9Sstevel@tonic-gate while (rb->rb_zstr.avail_in) {
4917c478bd9Sstevel@tonic-gate if (rb->rb_zstr.avail_out == 0)
4927c478bd9Sstevel@tonic-gate rbzs_grow(rb);
4937c478bd9Sstevel@tonic-gate
4947c478bd9Sstevel@tonic-gate if ((rc = deflate(&rb->rb_zstr, Z_NO_FLUSH)) != Z_OK)
4957c478bd9Sstevel@tonic-gate parseterminate("zlib deflate failed: %s", zError(rc));
4967c478bd9Sstevel@tonic-gate }
4977c478bd9Sstevel@tonic-gate rb->rb_ptr = (caddr_t)rb->rb_zstr.next_out;
4984d232658Sjohnlev
4994d232658Sjohnlev return (n);
5007c478bd9Sstevel@tonic-gate }
5017c478bd9Sstevel@tonic-gate
5027c478bd9Sstevel@tonic-gate static void
compress_flush(resbuf_t * rb,int type)5037c478bd9Sstevel@tonic-gate compress_flush(resbuf_t *rb, int type)
5047c478bd9Sstevel@tonic-gate {
5057c478bd9Sstevel@tonic-gate int rc;
5067c478bd9Sstevel@tonic-gate
5077c478bd9Sstevel@tonic-gate for (;;) {
5087c478bd9Sstevel@tonic-gate if (rb->rb_zstr.avail_out == 0)
5097c478bd9Sstevel@tonic-gate rbzs_grow(rb);
5107c478bd9Sstevel@tonic-gate
5117c478bd9Sstevel@tonic-gate rc = deflate(&rb->rb_zstr, type);
5127c478bd9Sstevel@tonic-gate if ((type == Z_FULL_FLUSH && rc == Z_BUF_ERROR) ||
5137c478bd9Sstevel@tonic-gate (type == Z_FINISH && rc == Z_STREAM_END))
5147c478bd9Sstevel@tonic-gate break;
5157c478bd9Sstevel@tonic-gate else if (rc != Z_OK)
5167c478bd9Sstevel@tonic-gate parseterminate("zlib finish failed: %s", zError(rc));
5177c478bd9Sstevel@tonic-gate }
5187c478bd9Sstevel@tonic-gate rb->rb_ptr = (caddr_t)rb->rb_zstr.next_out;
5197c478bd9Sstevel@tonic-gate }
5207c478bd9Sstevel@tonic-gate
5217c478bd9Sstevel@tonic-gate static void
compress_end(resbuf_t * rb)5227c478bd9Sstevel@tonic-gate compress_end(resbuf_t *rb)
5237c478bd9Sstevel@tonic-gate {
5247c478bd9Sstevel@tonic-gate int rc;
5257c478bd9Sstevel@tonic-gate
5267c478bd9Sstevel@tonic-gate compress_flush(rb, Z_FINISH);
5277c478bd9Sstevel@tonic-gate
5287c478bd9Sstevel@tonic-gate if ((rc = deflateEnd(&rb->rb_zstr)) != Z_OK)
5297c478bd9Sstevel@tonic-gate parseterminate("zlib end failed: %s", zError(rc));
5307c478bd9Sstevel@tonic-gate }
5317c478bd9Sstevel@tonic-gate
5327c478bd9Sstevel@tonic-gate /*
5337c478bd9Sstevel@tonic-gate * Pad the buffer to a power-of-2 boundary
5347c478bd9Sstevel@tonic-gate */
5357c478bd9Sstevel@tonic-gate static void
pad_buffer(ctf_buf_t * buf,int align)5367c478bd9Sstevel@tonic-gate pad_buffer(ctf_buf_t *buf, int align)
5377c478bd9Sstevel@tonic-gate {
5387c478bd9Sstevel@tonic-gate uint_t cur = ctf_buf_cur(buf);
5397c478bd9Sstevel@tonic-gate ssize_t topad = (align - (cur % align)) % align;
5407c478bd9Sstevel@tonic-gate static const char pad[8] = { 0 };
5417c478bd9Sstevel@tonic-gate
5427c478bd9Sstevel@tonic-gate while (topad > 0) {
5437c478bd9Sstevel@tonic-gate ctf_buf_write(buf, pad, (topad > 8 ? 8 : topad));
5447c478bd9Sstevel@tonic-gate topad -= 8;
5457c478bd9Sstevel@tonic-gate }
5467c478bd9Sstevel@tonic-gate }
5477c478bd9Sstevel@tonic-gate
5484d232658Sjohnlev static ssize_t
bcopy_data(const void * buf,size_t n,void * data)5494d232658Sjohnlev bcopy_data(const void *buf, size_t n, void *data)
5507c478bd9Sstevel@tonic-gate {
5514d232658Sjohnlev caddr_t *posp = (caddr_t *)data;
5527c478bd9Sstevel@tonic-gate bcopy(buf, *posp, n);
5537c478bd9Sstevel@tonic-gate *posp += n;
5544d232658Sjohnlev return (n);
5557c478bd9Sstevel@tonic-gate }
5567c478bd9Sstevel@tonic-gate
5577c478bd9Sstevel@tonic-gate static caddr_t
write_buffer(ctf_header_t * h,ctf_buf_t * buf,size_t * resszp)5587c478bd9Sstevel@tonic-gate write_buffer(ctf_header_t *h, ctf_buf_t *buf, size_t *resszp)
5597c478bd9Sstevel@tonic-gate {
5607c478bd9Sstevel@tonic-gate caddr_t outbuf;
5617c478bd9Sstevel@tonic-gate caddr_t bufpos;
5627c478bd9Sstevel@tonic-gate
5637c478bd9Sstevel@tonic-gate outbuf = xmalloc(sizeof (ctf_header_t) + (buf->ctb_ptr - buf->ctb_base)
5647c478bd9Sstevel@tonic-gate + buf->ctb_strtab.str_size);
5657c478bd9Sstevel@tonic-gate
5667c478bd9Sstevel@tonic-gate bufpos = outbuf;
5674d232658Sjohnlev (void) bcopy_data(h, sizeof (ctf_header_t), &bufpos);
5684d232658Sjohnlev (void) bcopy_data(buf->ctb_base, buf->ctb_ptr - buf->ctb_base,
5697c478bd9Sstevel@tonic-gate &bufpos);
570c168da27Sjohnlev (void) strtab_write(&buf->ctb_strtab, bcopy_data, &bufpos);
5717c478bd9Sstevel@tonic-gate *resszp = bufpos - outbuf;
5727c478bd9Sstevel@tonic-gate return (outbuf);
5737c478bd9Sstevel@tonic-gate }
5747c478bd9Sstevel@tonic-gate
5757c478bd9Sstevel@tonic-gate /*
5767c478bd9Sstevel@tonic-gate * Create the compression buffer, and fill it with the CTF and string
5777c478bd9Sstevel@tonic-gate * table data. We flush the compression state between the two so the
5787c478bd9Sstevel@tonic-gate * dictionary used for the string tables won't be polluted with values
5797c478bd9Sstevel@tonic-gate * that made sense for the CTF data.
5807c478bd9Sstevel@tonic-gate */
5817c478bd9Sstevel@tonic-gate static caddr_t
write_compressed_buffer(ctf_header_t * h,ctf_buf_t * buf,size_t * resszp)5827c478bd9Sstevel@tonic-gate write_compressed_buffer(ctf_header_t *h, ctf_buf_t *buf, size_t *resszp)
5837c478bd9Sstevel@tonic-gate {
5847c478bd9Sstevel@tonic-gate resbuf_t resbuf;
5857c478bd9Sstevel@tonic-gate resbuf.rb_size = RES_BUF_CHUNK_SIZE;
5867c478bd9Sstevel@tonic-gate resbuf.rb_base = xmalloc(resbuf.rb_size);
5877c478bd9Sstevel@tonic-gate bcopy(h, resbuf.rb_base, sizeof (ctf_header_t));
5887c478bd9Sstevel@tonic-gate resbuf.rb_ptr = resbuf.rb_base + sizeof (ctf_header_t);
5897c478bd9Sstevel@tonic-gate
5907c478bd9Sstevel@tonic-gate compress_start(&resbuf);
5914d232658Sjohnlev (void) compress_buffer(buf->ctb_base, buf->ctb_ptr - buf->ctb_base,
5924d232658Sjohnlev &resbuf);
5937c478bd9Sstevel@tonic-gate compress_flush(&resbuf, Z_FULL_FLUSH);
594c168da27Sjohnlev (void) strtab_write(&buf->ctb_strtab, compress_buffer, &resbuf);
5957c478bd9Sstevel@tonic-gate compress_end(&resbuf);
5967c478bd9Sstevel@tonic-gate
5977c478bd9Sstevel@tonic-gate *resszp = (resbuf.rb_ptr - resbuf.rb_base);
5987c478bd9Sstevel@tonic-gate return (resbuf.rb_base);
5997c478bd9Sstevel@tonic-gate }
6007c478bd9Sstevel@tonic-gate
6017c478bd9Sstevel@tonic-gate caddr_t
ctf_gen(iiburst_t * iiburst,size_t * resszp,int do_compress)6027c478bd9Sstevel@tonic-gate ctf_gen(iiburst_t *iiburst, size_t *resszp, int do_compress)
6037c478bd9Sstevel@tonic-gate {
6047c478bd9Sstevel@tonic-gate ctf_buf_t *buf = ctf_buf_new();
6057c478bd9Sstevel@tonic-gate ctf_header_t h;
6067c478bd9Sstevel@tonic-gate caddr_t outbuf;
6077c478bd9Sstevel@tonic-gate
6087c478bd9Sstevel@tonic-gate int i;
6097c478bd9Sstevel@tonic-gate
6107c478bd9Sstevel@tonic-gate /*
6117c478bd9Sstevel@tonic-gate * Prepare the header, and create the CTF output buffers. The data
6127c478bd9Sstevel@tonic-gate * object section and function section are both lists of 2-byte
6137c478bd9Sstevel@tonic-gate * integers; we pad these out to the next 4-byte boundary if needed.
6147c478bd9Sstevel@tonic-gate */
6157c478bd9Sstevel@tonic-gate h.cth_magic = CTF_MAGIC;
6167c478bd9Sstevel@tonic-gate h.cth_version = CTF_VERSION;
6177c478bd9Sstevel@tonic-gate h.cth_flags = do_compress ? CTF_F_COMPRESS : 0;
6187c478bd9Sstevel@tonic-gate h.cth_parlabel = strtab_insert(&buf->ctb_strtab,
6197c478bd9Sstevel@tonic-gate iiburst->iib_td->td_parlabel);
6207c478bd9Sstevel@tonic-gate h.cth_parname = strtab_insert(&buf->ctb_strtab,
6217c478bd9Sstevel@tonic-gate iiburst->iib_td->td_parname);
6227c478bd9Sstevel@tonic-gate
6237c478bd9Sstevel@tonic-gate h.cth_lbloff = 0;
6247c478bd9Sstevel@tonic-gate (void) list_iter(iiburst->iib_td->td_labels, (int (*)())write_label,
6257c478bd9Sstevel@tonic-gate buf);
6267c478bd9Sstevel@tonic-gate
6277c478bd9Sstevel@tonic-gate pad_buffer(buf, 2);
6287c478bd9Sstevel@tonic-gate h.cth_objtoff = ctf_buf_cur(buf);
6297c478bd9Sstevel@tonic-gate for (i = 0; i < iiburst->iib_nobjts; i++)
6307c478bd9Sstevel@tonic-gate write_objects(iiburst->iib_objts[i], buf);
6317c478bd9Sstevel@tonic-gate
6327c478bd9Sstevel@tonic-gate pad_buffer(buf, 2);
6337c478bd9Sstevel@tonic-gate h.cth_funcoff = ctf_buf_cur(buf);
6347c478bd9Sstevel@tonic-gate for (i = 0; i < iiburst->iib_nfuncs; i++)
6357c478bd9Sstevel@tonic-gate write_functions(iiburst->iib_funcs[i], buf);
6367c478bd9Sstevel@tonic-gate
6377c478bd9Sstevel@tonic-gate pad_buffer(buf, 4);
6387c478bd9Sstevel@tonic-gate h.cth_typeoff = ctf_buf_cur(buf);
6397c478bd9Sstevel@tonic-gate (void) list_iter(iiburst->iib_types, (int (*)())write_type, buf);
6407c478bd9Sstevel@tonic-gate
6417c478bd9Sstevel@tonic-gate debug(2, "CTF wrote %d types\n", list_count(iiburst->iib_types));
6427c478bd9Sstevel@tonic-gate
6437c478bd9Sstevel@tonic-gate h.cth_stroff = ctf_buf_cur(buf);
6447c478bd9Sstevel@tonic-gate h.cth_strlen = strtab_size(&buf->ctb_strtab);
6457c478bd9Sstevel@tonic-gate
6467c478bd9Sstevel@tonic-gate /*
6477c478bd9Sstevel@tonic-gate * We only do compression for ctfmerge, as ctfconvert is only
6487c478bd9Sstevel@tonic-gate * supposed to be used on intermediary build objects. This is
6497c478bd9Sstevel@tonic-gate * significantly faster.
6507c478bd9Sstevel@tonic-gate */
6517c478bd9Sstevel@tonic-gate if (do_compress)
6527c478bd9Sstevel@tonic-gate outbuf = write_compressed_buffer(&h, buf, resszp);
6537c478bd9Sstevel@tonic-gate else
6547c478bd9Sstevel@tonic-gate outbuf = write_buffer(&h, buf, resszp);
6557c478bd9Sstevel@tonic-gate
6567c478bd9Sstevel@tonic-gate ctf_buf_free(buf);
6577c478bd9Sstevel@tonic-gate return (outbuf);
6587c478bd9Sstevel@tonic-gate }
6597c478bd9Sstevel@tonic-gate
6607c478bd9Sstevel@tonic-gate void
get_ctt_size(ctf_type_t * ctt,size_t * sizep,size_t * incrementp)6617c478bd9Sstevel@tonic-gate get_ctt_size(ctf_type_t *ctt, size_t *sizep, size_t *incrementp)
6627c478bd9Sstevel@tonic-gate {
6637c478bd9Sstevel@tonic-gate if (ctt->ctt_size == CTF_LSIZE_SENT) {
6647c478bd9Sstevel@tonic-gate *sizep = (size_t)CTF_TYPE_LSIZE(ctt);
6657c478bd9Sstevel@tonic-gate *incrementp = sizeof (ctf_type_t);
6667c478bd9Sstevel@tonic-gate } else {
6677c478bd9Sstevel@tonic-gate *sizep = ctt->ctt_size;
6687c478bd9Sstevel@tonic-gate *incrementp = sizeof (ctf_stype_t);
6697c478bd9Sstevel@tonic-gate }
6707c478bd9Sstevel@tonic-gate }
6717c478bd9Sstevel@tonic-gate
6727c478bd9Sstevel@tonic-gate static int
count_types(ctf_header_t * h,caddr_t data)6737c478bd9Sstevel@tonic-gate count_types(ctf_header_t *h, caddr_t data)
6747c478bd9Sstevel@tonic-gate {
6757c478bd9Sstevel@tonic-gate caddr_t dptr = data + h->cth_typeoff;
6767c478bd9Sstevel@tonic-gate int count = 0;
6777c478bd9Sstevel@tonic-gate
6787c478bd9Sstevel@tonic-gate dptr = data + h->cth_typeoff;
6797c478bd9Sstevel@tonic-gate while (dptr < data + h->cth_stroff) {
6807c478bd9Sstevel@tonic-gate /* LINTED - pointer alignment */
6817c478bd9Sstevel@tonic-gate ctf_type_t *ctt = (ctf_type_t *)dptr;
6827c478bd9Sstevel@tonic-gate size_t vlen = CTF_INFO_VLEN(ctt->ctt_info);
6837c478bd9Sstevel@tonic-gate size_t size, increment;
6847c478bd9Sstevel@tonic-gate
6857c478bd9Sstevel@tonic-gate get_ctt_size(ctt, &size, &increment);
6867c478bd9Sstevel@tonic-gate
6877c478bd9Sstevel@tonic-gate switch (CTF_INFO_KIND(ctt->ctt_info)) {
6887c478bd9Sstevel@tonic-gate case CTF_K_INTEGER:
6897c478bd9Sstevel@tonic-gate case CTF_K_FLOAT:
6907c478bd9Sstevel@tonic-gate dptr += 4;
6917c478bd9Sstevel@tonic-gate break;
6927c478bd9Sstevel@tonic-gate case CTF_K_POINTER:
6937c478bd9Sstevel@tonic-gate case CTF_K_FORWARD:
6947c478bd9Sstevel@tonic-gate case CTF_K_TYPEDEF:
6957c478bd9Sstevel@tonic-gate case CTF_K_VOLATILE:
6967c478bd9Sstevel@tonic-gate case CTF_K_CONST:
6977c478bd9Sstevel@tonic-gate case CTF_K_RESTRICT:
6987c478bd9Sstevel@tonic-gate case CTF_K_FUNCTION:
6997c478bd9Sstevel@tonic-gate dptr += sizeof (ushort_t) * (vlen + (vlen & 1));
7007c478bd9Sstevel@tonic-gate break;
7017c478bd9Sstevel@tonic-gate case CTF_K_ARRAY:
7027c478bd9Sstevel@tonic-gate dptr += sizeof (ctf_array_t);
7037c478bd9Sstevel@tonic-gate break;
7047c478bd9Sstevel@tonic-gate case CTF_K_STRUCT:
7057c478bd9Sstevel@tonic-gate case CTF_K_UNION:
7067c478bd9Sstevel@tonic-gate if (size < CTF_LSTRUCT_THRESH)
7077c478bd9Sstevel@tonic-gate dptr += sizeof (ctf_member_t) * vlen;
7087c478bd9Sstevel@tonic-gate else
7097c478bd9Sstevel@tonic-gate dptr += sizeof (ctf_lmember_t) * vlen;
7107c478bd9Sstevel@tonic-gate break;
7117c478bd9Sstevel@tonic-gate case CTF_K_ENUM:
7127c478bd9Sstevel@tonic-gate dptr += sizeof (ctf_enum_t) * vlen;
7137c478bd9Sstevel@tonic-gate break;
7147c478bd9Sstevel@tonic-gate case CTF_K_UNKNOWN:
7157c478bd9Sstevel@tonic-gate break;
7167c478bd9Sstevel@tonic-gate default:
7177c478bd9Sstevel@tonic-gate parseterminate("Unknown CTF type %d (#%d) at %#x",
7187c478bd9Sstevel@tonic-gate CTF_INFO_KIND(ctt->ctt_info), count, dptr - data);
7197c478bd9Sstevel@tonic-gate }
7207c478bd9Sstevel@tonic-gate
7217c478bd9Sstevel@tonic-gate dptr += increment;
7227c478bd9Sstevel@tonic-gate count++;
7237c478bd9Sstevel@tonic-gate }
7247c478bd9Sstevel@tonic-gate
7257c478bd9Sstevel@tonic-gate debug(3, "CTF read %d types\n", count);
7267c478bd9Sstevel@tonic-gate
7277c478bd9Sstevel@tonic-gate return (count);
7287c478bd9Sstevel@tonic-gate }
7297c478bd9Sstevel@tonic-gate
7307c478bd9Sstevel@tonic-gate /*
7317c478bd9Sstevel@tonic-gate * Resurrect the labels stored in the CTF data, returning the index associated
7327c478bd9Sstevel@tonic-gate * with a label provided by the caller. There are several cases, outlined
7337c478bd9Sstevel@tonic-gate * below. Note that, given two labels, the one associated with the lesser type
7347c478bd9Sstevel@tonic-gate * index is considered to be older than the other.
7357c478bd9Sstevel@tonic-gate *
7367c478bd9Sstevel@tonic-gate * 1. matchlbl == NULL - return the index of the most recent label.
7377c478bd9Sstevel@tonic-gate * 2. matchlbl == "BASE" - return the index of the oldest label.
7387c478bd9Sstevel@tonic-gate * 3. matchlbl != NULL, but doesn't match any labels in the section - warn
7397c478bd9Sstevel@tonic-gate * the user, and proceed as if matchlbl == "BASE" (for safety).
7407c478bd9Sstevel@tonic-gate * 4. matchlbl != NULL, and matches one of the labels in the section - return
7417c478bd9Sstevel@tonic-gate * the type index associated with the label.
7427c478bd9Sstevel@tonic-gate */
7437c478bd9Sstevel@tonic-gate static int
resurrect_labels(ctf_header_t * h,tdata_t * td,caddr_t ctfdata,char * matchlbl)7447c478bd9Sstevel@tonic-gate resurrect_labels(ctf_header_t *h, tdata_t *td, caddr_t ctfdata, char *matchlbl)
7457c478bd9Sstevel@tonic-gate {
7467c478bd9Sstevel@tonic-gate caddr_t buf = ctfdata + h->cth_lbloff;
7477c478bd9Sstevel@tonic-gate caddr_t sbuf = ctfdata + h->cth_stroff;
7487c478bd9Sstevel@tonic-gate size_t bufsz = h->cth_objtoff - h->cth_lbloff;
7497c478bd9Sstevel@tonic-gate int lastidx = 0, baseidx = -1;
7507c478bd9Sstevel@tonic-gate char *baselabel;
7517c478bd9Sstevel@tonic-gate ctf_lblent_t *ctl;
7527c478bd9Sstevel@tonic-gate
7537c478bd9Sstevel@tonic-gate /* LINTED - pointer alignment */
7547c478bd9Sstevel@tonic-gate for (ctl = (ctf_lblent_t *)buf; (caddr_t)ctl < buf + bufsz; ctl++) {
7557c478bd9Sstevel@tonic-gate char *label = sbuf + ctl->ctl_label;
7567c478bd9Sstevel@tonic-gate
7577c478bd9Sstevel@tonic-gate lastidx = ctl->ctl_typeidx;
7587c478bd9Sstevel@tonic-gate
7597c478bd9Sstevel@tonic-gate debug(3, "Resurrected label %s type idx %d\n", label, lastidx);
7607c478bd9Sstevel@tonic-gate
7617c478bd9Sstevel@tonic-gate tdata_label_add(td, label, lastidx);
7627c478bd9Sstevel@tonic-gate
7637c478bd9Sstevel@tonic-gate if (baseidx == -1) {
7647c478bd9Sstevel@tonic-gate baseidx = lastidx;
7657c478bd9Sstevel@tonic-gate baselabel = label;
7667c478bd9Sstevel@tonic-gate if (matchlbl != NULL && streq(matchlbl, "BASE"))
7677c478bd9Sstevel@tonic-gate return (lastidx);
7687c478bd9Sstevel@tonic-gate }
7697c478bd9Sstevel@tonic-gate
7707c478bd9Sstevel@tonic-gate if (matchlbl != NULL && streq(label, matchlbl))
7717c478bd9Sstevel@tonic-gate return (lastidx);
7727c478bd9Sstevel@tonic-gate }
7737c478bd9Sstevel@tonic-gate
7747c478bd9Sstevel@tonic-gate if (matchlbl != NULL) {
7757c478bd9Sstevel@tonic-gate /* User provided a label that didn't match */
7767c478bd9Sstevel@tonic-gate warning("%s: Cannot find label `%s' - using base (%s)\n",
7777c478bd9Sstevel@tonic-gate curfile, matchlbl, (baselabel ? baselabel : "NONE"));
7787c478bd9Sstevel@tonic-gate
7797c478bd9Sstevel@tonic-gate tdata_label_free(td);
7807c478bd9Sstevel@tonic-gate tdata_label_add(td, baselabel, baseidx);
7817c478bd9Sstevel@tonic-gate
7827c478bd9Sstevel@tonic-gate return (baseidx);
7837c478bd9Sstevel@tonic-gate }
7847c478bd9Sstevel@tonic-gate
7857c478bd9Sstevel@tonic-gate return (lastidx);
7867c478bd9Sstevel@tonic-gate }
7877c478bd9Sstevel@tonic-gate
7887c478bd9Sstevel@tonic-gate static void
resurrect_objects(ctf_header_t * h,tdata_t * td,tdesc_t ** tdarr,int tdsize,caddr_t ctfdata,symit_data_t * si)7897c478bd9Sstevel@tonic-gate resurrect_objects(ctf_header_t *h, tdata_t *td, tdesc_t **tdarr, int tdsize,
7907c478bd9Sstevel@tonic-gate caddr_t ctfdata, symit_data_t *si)
7917c478bd9Sstevel@tonic-gate {
7927c478bd9Sstevel@tonic-gate caddr_t buf = ctfdata + h->cth_objtoff;
7937c478bd9Sstevel@tonic-gate size_t bufsz = h->cth_funcoff - h->cth_objtoff;
7947c478bd9Sstevel@tonic-gate caddr_t dptr;
7957c478bd9Sstevel@tonic-gate
7967c478bd9Sstevel@tonic-gate symit_reset(si);
7977c478bd9Sstevel@tonic-gate for (dptr = buf; dptr < buf + bufsz; dptr += 2) {
7987c478bd9Sstevel@tonic-gate /* LINTED - pointer alignment */
7997c478bd9Sstevel@tonic-gate ushort_t id = *((ushort_t *)dptr);
8007c478bd9Sstevel@tonic-gate iidesc_t *ii;
8017c478bd9Sstevel@tonic-gate GElf_Sym *sym;
8027c478bd9Sstevel@tonic-gate
8037c478bd9Sstevel@tonic-gate if (!(sym = symit_next(si, STT_OBJECT)) && id != 0) {
8047c478bd9Sstevel@tonic-gate parseterminate(
8057c478bd9Sstevel@tonic-gate "Unexpected end of object symbols at %x of %x",
8067c478bd9Sstevel@tonic-gate dptr - buf, bufsz);
8077c478bd9Sstevel@tonic-gate }
8087c478bd9Sstevel@tonic-gate
8097c478bd9Sstevel@tonic-gate if (id == 0) {
8107c478bd9Sstevel@tonic-gate debug(3, "Skipping null object\n");
8117c478bd9Sstevel@tonic-gate continue;
8127c478bd9Sstevel@tonic-gate } else if (id >= tdsize) {
8137c478bd9Sstevel@tonic-gate parseterminate("Reference to invalid type %d", id);
8147c478bd9Sstevel@tonic-gate }
8157c478bd9Sstevel@tonic-gate
8167c478bd9Sstevel@tonic-gate ii = iidesc_new(symit_name(si));
8177c478bd9Sstevel@tonic-gate ii->ii_dtype = tdarr[id];
8187c478bd9Sstevel@tonic-gate if (GELF_ST_BIND(sym->st_info) == STB_LOCAL) {
8197c478bd9Sstevel@tonic-gate ii->ii_type = II_SVAR;
8207c478bd9Sstevel@tonic-gate ii->ii_owner = xstrdup(symit_curfile(si));
8217c478bd9Sstevel@tonic-gate } else
8227c478bd9Sstevel@tonic-gate ii->ii_type = II_GVAR;
8237c478bd9Sstevel@tonic-gate hash_add(td->td_iihash, ii);
8247c478bd9Sstevel@tonic-gate
8257c478bd9Sstevel@tonic-gate debug(3, "Resurrected %s object %s (%d) from %s\n",
8267c478bd9Sstevel@tonic-gate (ii->ii_type == II_GVAR ? "global" : "static"),
8277c478bd9Sstevel@tonic-gate ii->ii_name, id, (ii->ii_owner ? ii->ii_owner : "(none)"));
8287c478bd9Sstevel@tonic-gate }
8297c478bd9Sstevel@tonic-gate }
8307c478bd9Sstevel@tonic-gate
8317c478bd9Sstevel@tonic-gate static void
resurrect_functions(ctf_header_t * h,tdata_t * td,tdesc_t ** tdarr,int tdsize,caddr_t ctfdata,symit_data_t * si)8327c478bd9Sstevel@tonic-gate resurrect_functions(ctf_header_t *h, tdata_t *td, tdesc_t **tdarr, int tdsize,
8337c478bd9Sstevel@tonic-gate caddr_t ctfdata, symit_data_t *si)
8347c478bd9Sstevel@tonic-gate {
8357c478bd9Sstevel@tonic-gate caddr_t buf = ctfdata + h->cth_funcoff;
8367c478bd9Sstevel@tonic-gate size_t bufsz = h->cth_typeoff - h->cth_funcoff;
8377c478bd9Sstevel@tonic-gate caddr_t dptr = buf;
8387c478bd9Sstevel@tonic-gate iidesc_t *ii;
8397c478bd9Sstevel@tonic-gate ushort_t info;
8407c478bd9Sstevel@tonic-gate ushort_t retid;
8417c478bd9Sstevel@tonic-gate GElf_Sym *sym;
8427c478bd9Sstevel@tonic-gate int i;
8437c478bd9Sstevel@tonic-gate
8447c478bd9Sstevel@tonic-gate symit_reset(si);
8457c478bd9Sstevel@tonic-gate while (dptr < buf + bufsz) {
8467c478bd9Sstevel@tonic-gate /* LINTED - pointer alignment */
8477c478bd9Sstevel@tonic-gate info = *((ushort_t *)dptr);
8487c478bd9Sstevel@tonic-gate dptr += 2;
8497c478bd9Sstevel@tonic-gate
8507c478bd9Sstevel@tonic-gate if (!(sym = symit_next(si, STT_FUNC)) && info != 0)
8517c478bd9Sstevel@tonic-gate parseterminate("Unexpected end of function symbols");
8527c478bd9Sstevel@tonic-gate
8537c478bd9Sstevel@tonic-gate if (info == 0) {
8547c478bd9Sstevel@tonic-gate debug(3, "Skipping null function (%s)\n",
8557c478bd9Sstevel@tonic-gate symit_name(si));
8567c478bd9Sstevel@tonic-gate continue;
8577c478bd9Sstevel@tonic-gate }
8587c478bd9Sstevel@tonic-gate
8597c478bd9Sstevel@tonic-gate /* LINTED - pointer alignment */
8607c478bd9Sstevel@tonic-gate retid = *((ushort_t *)dptr);
8617c478bd9Sstevel@tonic-gate dptr += 2;
8627c478bd9Sstevel@tonic-gate
8637c478bd9Sstevel@tonic-gate if (retid >= tdsize)
8647c478bd9Sstevel@tonic-gate parseterminate("Reference to invalid type %d", retid);
8657c478bd9Sstevel@tonic-gate
8667c478bd9Sstevel@tonic-gate ii = iidesc_new(symit_name(si));
8677c478bd9Sstevel@tonic-gate ii->ii_dtype = tdarr[retid];
8687c478bd9Sstevel@tonic-gate if (GELF_ST_BIND(sym->st_info) == STB_LOCAL) {
8697c478bd9Sstevel@tonic-gate ii->ii_type = II_SFUN;
8707c478bd9Sstevel@tonic-gate ii->ii_owner = xstrdup(symit_curfile(si));
8717c478bd9Sstevel@tonic-gate } else
8727c478bd9Sstevel@tonic-gate ii->ii_type = II_GFUN;
8737c478bd9Sstevel@tonic-gate ii->ii_nargs = CTF_INFO_VLEN(info);
8747c478bd9Sstevel@tonic-gate if (ii->ii_nargs)
8757c478bd9Sstevel@tonic-gate ii->ii_args =
8767c478bd9Sstevel@tonic-gate xmalloc(sizeof (tdesc_t *) * ii->ii_nargs);
8777c478bd9Sstevel@tonic-gate
8787c478bd9Sstevel@tonic-gate for (i = 0; i < ii->ii_nargs; i++, dptr += 2) {
8797c478bd9Sstevel@tonic-gate /* LINTED - pointer alignment */
8807c478bd9Sstevel@tonic-gate ushort_t id = *((ushort_t *)dptr);
8817c478bd9Sstevel@tonic-gate if (id >= tdsize)
8827c478bd9Sstevel@tonic-gate parseterminate("Reference to invalid type %d",
8837c478bd9Sstevel@tonic-gate id);
8847c478bd9Sstevel@tonic-gate ii->ii_args[i] = tdarr[id];
8857c478bd9Sstevel@tonic-gate }
8867c478bd9Sstevel@tonic-gate
8877c478bd9Sstevel@tonic-gate if (ii->ii_nargs && ii->ii_args[ii->ii_nargs - 1] == NULL) {
8887c478bd9Sstevel@tonic-gate ii->ii_nargs--;
8897c478bd9Sstevel@tonic-gate ii->ii_vargs = 1;
8907c478bd9Sstevel@tonic-gate }
8917c478bd9Sstevel@tonic-gate
8927c478bd9Sstevel@tonic-gate hash_add(td->td_iihash, ii);
8937c478bd9Sstevel@tonic-gate
8947c478bd9Sstevel@tonic-gate debug(3, "Resurrected %s function %s (%d, %d args)\n",
8957c478bd9Sstevel@tonic-gate (ii->ii_type == II_GFUN ? "global" : "static"),
8967c478bd9Sstevel@tonic-gate ii->ii_name, retid, ii->ii_nargs);
8977c478bd9Sstevel@tonic-gate }
8987c478bd9Sstevel@tonic-gate }
8997c478bd9Sstevel@tonic-gate
9007c478bd9Sstevel@tonic-gate static void
resurrect_types(ctf_header_t * h,tdata_t * td,tdesc_t ** tdarr,int tdsize,caddr_t ctfdata,int maxid)9017c478bd9Sstevel@tonic-gate resurrect_types(ctf_header_t *h, tdata_t *td, tdesc_t **tdarr, int tdsize,
9027c478bd9Sstevel@tonic-gate caddr_t ctfdata, int maxid)
9037c478bd9Sstevel@tonic-gate {
9047c478bd9Sstevel@tonic-gate caddr_t buf = ctfdata + h->cth_typeoff;
9057c478bd9Sstevel@tonic-gate size_t bufsz = h->cth_stroff - h->cth_typeoff;
9067c478bd9Sstevel@tonic-gate caddr_t sbuf = ctfdata + h->cth_stroff;
9077c478bd9Sstevel@tonic-gate caddr_t dptr = buf;
9087c478bd9Sstevel@tonic-gate tdesc_t *tdp;
9097c478bd9Sstevel@tonic-gate uint_t data;
9107c478bd9Sstevel@tonic-gate uint_t encoding;
9117c478bd9Sstevel@tonic-gate size_t size, increment;
9127c478bd9Sstevel@tonic-gate int tcnt;
9137c478bd9Sstevel@tonic-gate int iicnt = 0;
9147c478bd9Sstevel@tonic-gate tid_t tid, argid;
9157c478bd9Sstevel@tonic-gate int kind, vlen;
9167c478bd9Sstevel@tonic-gate int i;
9177c478bd9Sstevel@tonic-gate
9187c478bd9Sstevel@tonic-gate elist_t **epp;
9197c478bd9Sstevel@tonic-gate mlist_t **mpp;
9207c478bd9Sstevel@tonic-gate intr_t *ip;
9217c478bd9Sstevel@tonic-gate
9227c478bd9Sstevel@tonic-gate ctf_type_t *ctt;
9237c478bd9Sstevel@tonic-gate ctf_array_t *cta;
9247c478bd9Sstevel@tonic-gate ctf_enum_t *cte;
9257c478bd9Sstevel@tonic-gate
9267c478bd9Sstevel@tonic-gate /*
9277c478bd9Sstevel@tonic-gate * A maxid of zero indicates a request to resurrect all types, so reset
9287c478bd9Sstevel@tonic-gate * maxid to the maximum type id.
9297c478bd9Sstevel@tonic-gate */
9307c478bd9Sstevel@tonic-gate if (maxid == 0)
9317c478bd9Sstevel@tonic-gate maxid = CTF_MAX_TYPE;
9327c478bd9Sstevel@tonic-gate
9337c478bd9Sstevel@tonic-gate for (dptr = buf, tcnt = 0, tid = 1; dptr < buf + bufsz; tcnt++, tid++) {
9347c478bd9Sstevel@tonic-gate if (tid > maxid)
9357c478bd9Sstevel@tonic-gate break;
9367c478bd9Sstevel@tonic-gate
9377c478bd9Sstevel@tonic-gate if (tid >= tdsize)
9387c478bd9Sstevel@tonic-gate parseterminate("Reference to invalid type %d", tid);
9397c478bd9Sstevel@tonic-gate
9407c478bd9Sstevel@tonic-gate /* LINTED - pointer alignment */
9417c478bd9Sstevel@tonic-gate ctt = (ctf_type_t *)dptr;
9427c478bd9Sstevel@tonic-gate
9437c478bd9Sstevel@tonic-gate get_ctt_size(ctt, &size, &increment);
9447c478bd9Sstevel@tonic-gate dptr += increment;
9457c478bd9Sstevel@tonic-gate
9467c478bd9Sstevel@tonic-gate tdp = tdarr[tid];
9477c478bd9Sstevel@tonic-gate
9487c478bd9Sstevel@tonic-gate if (CTF_NAME_STID(ctt->ctt_name) != CTF_STRTAB_0)
9497c478bd9Sstevel@tonic-gate parseterminate(
9507c478bd9Sstevel@tonic-gate "Unable to cope with non-zero strtab id");
9517c478bd9Sstevel@tonic-gate if (CTF_NAME_OFFSET(ctt->ctt_name) != 0) {
9527c478bd9Sstevel@tonic-gate tdp->t_name =
9537c478bd9Sstevel@tonic-gate xstrdup(sbuf + CTF_NAME_OFFSET(ctt->ctt_name));
9547c478bd9Sstevel@tonic-gate } else
9557c478bd9Sstevel@tonic-gate tdp->t_name = NULL;
9567c478bd9Sstevel@tonic-gate
9577c478bd9Sstevel@tonic-gate kind = CTF_INFO_KIND(ctt->ctt_info);
9587c478bd9Sstevel@tonic-gate vlen = CTF_INFO_VLEN(ctt->ctt_info);
9597c478bd9Sstevel@tonic-gate
9607c478bd9Sstevel@tonic-gate switch (kind) {
9617c478bd9Sstevel@tonic-gate case CTF_K_INTEGER:
9627c478bd9Sstevel@tonic-gate tdp->t_type = INTRINSIC;
9637c478bd9Sstevel@tonic-gate tdp->t_size = size;
9647c478bd9Sstevel@tonic-gate
9657c478bd9Sstevel@tonic-gate /* LINTED - pointer alignment */
9667c478bd9Sstevel@tonic-gate data = *((uint_t *)dptr);
9677c478bd9Sstevel@tonic-gate dptr += sizeof (uint_t);
9687c478bd9Sstevel@tonic-gate encoding = CTF_INT_ENCODING(data);
9697c478bd9Sstevel@tonic-gate
9707c478bd9Sstevel@tonic-gate ip = xmalloc(sizeof (intr_t));
9717c478bd9Sstevel@tonic-gate ip->intr_type = INTR_INT;
9727c478bd9Sstevel@tonic-gate ip->intr_signed = (encoding & CTF_INT_SIGNED) ? 1 : 0;
9737c478bd9Sstevel@tonic-gate
9747c478bd9Sstevel@tonic-gate if (encoding & CTF_INT_CHAR)
9757c478bd9Sstevel@tonic-gate ip->intr_iformat = 'c';
9767c478bd9Sstevel@tonic-gate else if (encoding & CTF_INT_BOOL)
9777c478bd9Sstevel@tonic-gate ip->intr_iformat = 'b';
9787c478bd9Sstevel@tonic-gate else if (encoding & CTF_INT_VARARGS)
9797c478bd9Sstevel@tonic-gate ip->intr_iformat = 'v';
9807c478bd9Sstevel@tonic-gate else
9817c478bd9Sstevel@tonic-gate ip->intr_iformat = '\0';
9827c478bd9Sstevel@tonic-gate
9837c478bd9Sstevel@tonic-gate ip->intr_offset = CTF_INT_OFFSET(data);
9847c478bd9Sstevel@tonic-gate ip->intr_nbits = CTF_INT_BITS(data);
9857c478bd9Sstevel@tonic-gate tdp->t_intr = ip;
9867c478bd9Sstevel@tonic-gate break;
9877c478bd9Sstevel@tonic-gate
9887c478bd9Sstevel@tonic-gate case CTF_K_FLOAT:
9897c478bd9Sstevel@tonic-gate tdp->t_type = INTRINSIC;
9907c478bd9Sstevel@tonic-gate tdp->t_size = size;
9917c478bd9Sstevel@tonic-gate
9927c478bd9Sstevel@tonic-gate /* LINTED - pointer alignment */
9937c478bd9Sstevel@tonic-gate data = *((uint_t *)dptr);
9947c478bd9Sstevel@tonic-gate dptr += sizeof (uint_t);
9957c478bd9Sstevel@tonic-gate
9967c478bd9Sstevel@tonic-gate ip = xcalloc(sizeof (intr_t));
9977c478bd9Sstevel@tonic-gate ip->intr_type = INTR_REAL;
9987c478bd9Sstevel@tonic-gate ip->intr_fformat = CTF_FP_ENCODING(data);
9997c478bd9Sstevel@tonic-gate ip->intr_offset = CTF_FP_OFFSET(data);
10007c478bd9Sstevel@tonic-gate ip->intr_nbits = CTF_FP_BITS(data);
10017c478bd9Sstevel@tonic-gate tdp->t_intr = ip;
10027c478bd9Sstevel@tonic-gate break;
10037c478bd9Sstevel@tonic-gate
10047c478bd9Sstevel@tonic-gate case CTF_K_POINTER:
10057c478bd9Sstevel@tonic-gate tdp->t_type = POINTER;
10067c478bd9Sstevel@tonic-gate tdp->t_tdesc = tdarr[ctt->ctt_type];
10077c478bd9Sstevel@tonic-gate break;
10087c478bd9Sstevel@tonic-gate
10097c478bd9Sstevel@tonic-gate case CTF_K_ARRAY:
10107c478bd9Sstevel@tonic-gate tdp->t_type = ARRAY;
10117c478bd9Sstevel@tonic-gate tdp->t_size = size;
10127c478bd9Sstevel@tonic-gate
10137c478bd9Sstevel@tonic-gate /* LINTED - pointer alignment */
10147c478bd9Sstevel@tonic-gate cta = (ctf_array_t *)dptr;
10157c478bd9Sstevel@tonic-gate dptr += sizeof (ctf_array_t);
10167c478bd9Sstevel@tonic-gate
10177c478bd9Sstevel@tonic-gate tdp->t_ardef = xmalloc(sizeof (ardef_t));
10187c478bd9Sstevel@tonic-gate tdp->t_ardef->ad_contents = tdarr[cta->cta_contents];
10197c478bd9Sstevel@tonic-gate tdp->t_ardef->ad_idxtype = tdarr[cta->cta_index];
10207c478bd9Sstevel@tonic-gate tdp->t_ardef->ad_nelems = cta->cta_nelems;
10217c478bd9Sstevel@tonic-gate break;
10227c478bd9Sstevel@tonic-gate
10237c478bd9Sstevel@tonic-gate case CTF_K_STRUCT:
10247c478bd9Sstevel@tonic-gate case CTF_K_UNION:
10257c478bd9Sstevel@tonic-gate tdp->t_type = (kind == CTF_K_STRUCT ? STRUCT : UNION);
10267c478bd9Sstevel@tonic-gate tdp->t_size = size;
10277c478bd9Sstevel@tonic-gate
10287c478bd9Sstevel@tonic-gate if (size < CTF_LSTRUCT_THRESH) {
10297c478bd9Sstevel@tonic-gate for (i = 0, mpp = &tdp->t_members; i < vlen;
10307c478bd9Sstevel@tonic-gate i++, mpp = &((*mpp)->ml_next)) {
10317c478bd9Sstevel@tonic-gate /* LINTED - pointer alignment */
10327c478bd9Sstevel@tonic-gate ctf_member_t *ctm = (ctf_member_t *)
10337c478bd9Sstevel@tonic-gate dptr;
10347c478bd9Sstevel@tonic-gate dptr += sizeof (ctf_member_t);
10357c478bd9Sstevel@tonic-gate
10367c478bd9Sstevel@tonic-gate *mpp = xmalloc(sizeof (mlist_t));
10377c478bd9Sstevel@tonic-gate (*mpp)->ml_name = xstrdup(sbuf +
10387c478bd9Sstevel@tonic-gate ctm->ctm_name);
10397c478bd9Sstevel@tonic-gate (*mpp)->ml_type = tdarr[ctm->ctm_type];
10407c478bd9Sstevel@tonic-gate (*mpp)->ml_offset = ctm->ctm_offset;
10417c478bd9Sstevel@tonic-gate (*mpp)->ml_size = 0;
10427c478bd9Sstevel@tonic-gate }
10437c478bd9Sstevel@tonic-gate } else {
10447c478bd9Sstevel@tonic-gate for (i = 0, mpp = &tdp->t_members; i < vlen;
10457c478bd9Sstevel@tonic-gate i++, mpp = &((*mpp)->ml_next)) {
10467c478bd9Sstevel@tonic-gate /* LINTED - pointer alignment */
10477c478bd9Sstevel@tonic-gate ctf_lmember_t *ctlm = (ctf_lmember_t *)
10487c478bd9Sstevel@tonic-gate dptr;
10497c478bd9Sstevel@tonic-gate dptr += sizeof (ctf_lmember_t);
10507c478bd9Sstevel@tonic-gate
10517c478bd9Sstevel@tonic-gate *mpp = xmalloc(sizeof (mlist_t));
10527c478bd9Sstevel@tonic-gate (*mpp)->ml_name = xstrdup(sbuf +
10537c478bd9Sstevel@tonic-gate ctlm->ctlm_name);
10547c478bd9Sstevel@tonic-gate (*mpp)->ml_type =
10557c478bd9Sstevel@tonic-gate tdarr[ctlm->ctlm_type];
10567c478bd9Sstevel@tonic-gate (*mpp)->ml_offset =
10577c478bd9Sstevel@tonic-gate (int)CTF_LMEM_OFFSET(ctlm);
10587c478bd9Sstevel@tonic-gate (*mpp)->ml_size = 0;
10597c478bd9Sstevel@tonic-gate }
10607c478bd9Sstevel@tonic-gate }
10617c478bd9Sstevel@tonic-gate
10627c478bd9Sstevel@tonic-gate *mpp = NULL;
10637c478bd9Sstevel@tonic-gate break;
10647c478bd9Sstevel@tonic-gate
10657c478bd9Sstevel@tonic-gate case CTF_K_ENUM:
10667c478bd9Sstevel@tonic-gate tdp->t_type = ENUM;
10677c478bd9Sstevel@tonic-gate tdp->t_size = size;
10687c478bd9Sstevel@tonic-gate
10697c478bd9Sstevel@tonic-gate for (i = 0, epp = &tdp->t_emem; i < vlen;
10707c478bd9Sstevel@tonic-gate i++, epp = &((*epp)->el_next)) {
10717c478bd9Sstevel@tonic-gate /* LINTED - pointer alignment */
10727c478bd9Sstevel@tonic-gate cte = (ctf_enum_t *)dptr;
10737c478bd9Sstevel@tonic-gate dptr += sizeof (ctf_enum_t);
10747c478bd9Sstevel@tonic-gate
10757c478bd9Sstevel@tonic-gate *epp = xmalloc(sizeof (elist_t));
10767c478bd9Sstevel@tonic-gate (*epp)->el_name = xstrdup(sbuf + cte->cte_name);
10777c478bd9Sstevel@tonic-gate (*epp)->el_number = cte->cte_value;
10787c478bd9Sstevel@tonic-gate }
10797c478bd9Sstevel@tonic-gate *epp = NULL;
10807c478bd9Sstevel@tonic-gate break;
10817c478bd9Sstevel@tonic-gate
10827c478bd9Sstevel@tonic-gate case CTF_K_FORWARD:
10837c478bd9Sstevel@tonic-gate tdp->t_type = FORWARD;
10847c478bd9Sstevel@tonic-gate list_add(&td->td_fwdlist, tdp);
10857c478bd9Sstevel@tonic-gate break;
10867c478bd9Sstevel@tonic-gate
10877c478bd9Sstevel@tonic-gate case CTF_K_TYPEDEF:
10887c478bd9Sstevel@tonic-gate tdp->t_type = TYPEDEF;
10897c478bd9Sstevel@tonic-gate tdp->t_tdesc = tdarr[ctt->ctt_type];
10907c478bd9Sstevel@tonic-gate break;
10917c478bd9Sstevel@tonic-gate
10927c478bd9Sstevel@tonic-gate case CTF_K_VOLATILE:
10937c478bd9Sstevel@tonic-gate tdp->t_type = VOLATILE;
10947c478bd9Sstevel@tonic-gate tdp->t_tdesc = tdarr[ctt->ctt_type];
10957c478bd9Sstevel@tonic-gate break;
10967c478bd9Sstevel@tonic-gate
10977c478bd9Sstevel@tonic-gate case CTF_K_CONST:
10987c478bd9Sstevel@tonic-gate tdp->t_type = CONST;
10997c478bd9Sstevel@tonic-gate tdp->t_tdesc = tdarr[ctt->ctt_type];
11007c478bd9Sstevel@tonic-gate break;
11017c478bd9Sstevel@tonic-gate
11027c478bd9Sstevel@tonic-gate case CTF_K_FUNCTION:
11037c478bd9Sstevel@tonic-gate tdp->t_type = FUNCTION;
11047c478bd9Sstevel@tonic-gate tdp->t_fndef = xcalloc(sizeof (fndef_t));
11057c478bd9Sstevel@tonic-gate tdp->t_fndef->fn_ret = tdarr[ctt->ctt_type];
11067c478bd9Sstevel@tonic-gate
11077c478bd9Sstevel@tonic-gate /* LINTED - pointer alignment */
11087c478bd9Sstevel@tonic-gate if (vlen > 0 && *(ushort_t *)(dptr +
11097c478bd9Sstevel@tonic-gate (sizeof (ushort_t) * (vlen - 1))) == 0)
11107c478bd9Sstevel@tonic-gate tdp->t_fndef->fn_vargs = 1;
11117c478bd9Sstevel@tonic-gate
11127c478bd9Sstevel@tonic-gate tdp->t_fndef->fn_nargs = vlen - tdp->t_fndef->fn_vargs;
11137c478bd9Sstevel@tonic-gate tdp->t_fndef->fn_args = xcalloc(sizeof (tdesc_t) *
11147c478bd9Sstevel@tonic-gate vlen - tdp->t_fndef->fn_vargs);
11157c478bd9Sstevel@tonic-gate
11167c478bd9Sstevel@tonic-gate for (i = 0; i < vlen; i++) {
11177c478bd9Sstevel@tonic-gate /* LINTED - pointer alignment */
11187c478bd9Sstevel@tonic-gate argid = *(ushort_t *)dptr;
11197c478bd9Sstevel@tonic-gate dptr += sizeof (ushort_t);
11207c478bd9Sstevel@tonic-gate
11217c478bd9Sstevel@tonic-gate if (argid != 0)
11227c478bd9Sstevel@tonic-gate tdp->t_fndef->fn_args[i] = tdarr[argid];
11237c478bd9Sstevel@tonic-gate }
11247c478bd9Sstevel@tonic-gate
11257c478bd9Sstevel@tonic-gate if (vlen & 1)
11267c478bd9Sstevel@tonic-gate dptr += sizeof (ushort_t);
11277c478bd9Sstevel@tonic-gate break;
11287c478bd9Sstevel@tonic-gate
11297c478bd9Sstevel@tonic-gate case CTF_K_RESTRICT:
11307c478bd9Sstevel@tonic-gate tdp->t_type = RESTRICT;
11317c478bd9Sstevel@tonic-gate tdp->t_tdesc = tdarr[ctt->ctt_type];
11327c478bd9Sstevel@tonic-gate break;
11337c478bd9Sstevel@tonic-gate
11347c478bd9Sstevel@tonic-gate case CTF_K_UNKNOWN:
11357c478bd9Sstevel@tonic-gate break;
11367c478bd9Sstevel@tonic-gate
11377c478bd9Sstevel@tonic-gate default:
11387c478bd9Sstevel@tonic-gate warning("Can't parse unknown CTF type %d\n", kind);
11397c478bd9Sstevel@tonic-gate }
11407c478bd9Sstevel@tonic-gate
11417c478bd9Sstevel@tonic-gate if (CTF_INFO_ISROOT(ctt->ctt_info)) {
11427c478bd9Sstevel@tonic-gate iidesc_t *ii = iidesc_new(tdp->t_name);
11437c478bd9Sstevel@tonic-gate if (tdp->t_type == STRUCT || tdp->t_type == UNION ||
11447c478bd9Sstevel@tonic-gate tdp->t_type == ENUM)
11457c478bd9Sstevel@tonic-gate ii->ii_type = II_SOU;
11467c478bd9Sstevel@tonic-gate else
11477c478bd9Sstevel@tonic-gate ii->ii_type = II_TYPE;
11487c478bd9Sstevel@tonic-gate ii->ii_dtype = tdp;
11497c478bd9Sstevel@tonic-gate hash_add(td->td_iihash, ii);
11507c478bd9Sstevel@tonic-gate
11517c478bd9Sstevel@tonic-gate iicnt++;
11527c478bd9Sstevel@tonic-gate }
11537c478bd9Sstevel@tonic-gate
11547c478bd9Sstevel@tonic-gate debug(3, "Resurrected %d %stype %s (%d)\n", tdp->t_type,
11557c478bd9Sstevel@tonic-gate (CTF_INFO_ISROOT(ctt->ctt_info) ? "root " : ""),
11564d232658Sjohnlev tdesc_name(tdp), tdp->t_id);
11577c478bd9Sstevel@tonic-gate }
11587c478bd9Sstevel@tonic-gate
11597c478bd9Sstevel@tonic-gate debug(3, "Resurrected %d types (%d were roots)\n", tcnt, iicnt);
11607c478bd9Sstevel@tonic-gate }
11617c478bd9Sstevel@tonic-gate
11627c478bd9Sstevel@tonic-gate /*
11637c478bd9Sstevel@tonic-gate * For lack of other inspiration, we're going to take the boring route. We
11647c478bd9Sstevel@tonic-gate * count the number of types. This lets us malloc that many tdesc structs
11657c478bd9Sstevel@tonic-gate * before we start filling them in. This has the advantage of allowing us to
11667c478bd9Sstevel@tonic-gate * avoid a merge-esque remap step.
11677c478bd9Sstevel@tonic-gate */
11687c478bd9Sstevel@tonic-gate static tdata_t *
ctf_parse(ctf_header_t * h,caddr_t buf,symit_data_t * si,char * label)11697c478bd9Sstevel@tonic-gate ctf_parse(ctf_header_t *h, caddr_t buf, symit_data_t *si, char *label)
11707c478bd9Sstevel@tonic-gate {
11717c478bd9Sstevel@tonic-gate tdata_t *td = tdata_new();
11727c478bd9Sstevel@tonic-gate tdesc_t **tdarr;
11737c478bd9Sstevel@tonic-gate int ntypes = count_types(h, buf);
11747c478bd9Sstevel@tonic-gate int idx, i;
11757c478bd9Sstevel@tonic-gate
11767c478bd9Sstevel@tonic-gate /* shudder */
11777c478bd9Sstevel@tonic-gate tdarr = xcalloc(sizeof (tdesc_t *) * (ntypes + 1));
11787c478bd9Sstevel@tonic-gate tdarr[0] = NULL;
11797c478bd9Sstevel@tonic-gate for (i = 1; i <= ntypes; i++) {
11807c478bd9Sstevel@tonic-gate tdarr[i] = xcalloc(sizeof (tdesc_t));
11817c478bd9Sstevel@tonic-gate tdarr[i]->t_id = i;
11827c478bd9Sstevel@tonic-gate }
11837c478bd9Sstevel@tonic-gate
11847c478bd9Sstevel@tonic-gate td->td_parlabel = xstrdup(buf + h->cth_stroff + h->cth_parlabel);
11857c478bd9Sstevel@tonic-gate
11867c478bd9Sstevel@tonic-gate /* we have the technology - we can rebuild them */
11877c478bd9Sstevel@tonic-gate idx = resurrect_labels(h, td, buf, label);
11887c478bd9Sstevel@tonic-gate
11897c478bd9Sstevel@tonic-gate resurrect_objects(h, td, tdarr, ntypes + 1, buf, si);
11907c478bd9Sstevel@tonic-gate resurrect_functions(h, td, tdarr, ntypes + 1, buf, si);
11917c478bd9Sstevel@tonic-gate resurrect_types(h, td, tdarr, ntypes + 1, buf, idx);
11927c478bd9Sstevel@tonic-gate
11937c478bd9Sstevel@tonic-gate free(tdarr);
11947c478bd9Sstevel@tonic-gate
11957c478bd9Sstevel@tonic-gate td->td_nextid = ntypes + 1;
11967c478bd9Sstevel@tonic-gate
11977c478bd9Sstevel@tonic-gate return (td);
11987c478bd9Sstevel@tonic-gate }
11997c478bd9Sstevel@tonic-gate
12007c478bd9Sstevel@tonic-gate static size_t
decompress_ctf(caddr_t cbuf,size_t cbufsz,caddr_t dbuf,size_t dbufsz)12017c478bd9Sstevel@tonic-gate decompress_ctf(caddr_t cbuf, size_t cbufsz, caddr_t dbuf, size_t dbufsz)
12027c478bd9Sstevel@tonic-gate {
12037c478bd9Sstevel@tonic-gate z_stream zstr;
12047c478bd9Sstevel@tonic-gate int rc;
12057c478bd9Sstevel@tonic-gate
12067c478bd9Sstevel@tonic-gate zstr.zalloc = (alloc_func)0;
12077c478bd9Sstevel@tonic-gate zstr.zfree = (free_func)0;
12087c478bd9Sstevel@tonic-gate zstr.opaque = (voidpf)0;
12097c478bd9Sstevel@tonic-gate
12107c478bd9Sstevel@tonic-gate zstr.next_in = (Bytef *)cbuf;
12117c478bd9Sstevel@tonic-gate zstr.avail_in = cbufsz;
12127c478bd9Sstevel@tonic-gate zstr.next_out = (Bytef *)dbuf;
12137c478bd9Sstevel@tonic-gate zstr.avail_out = dbufsz;
12147c478bd9Sstevel@tonic-gate
12157c478bd9Sstevel@tonic-gate if ((rc = inflateInit(&zstr)) != Z_OK ||
12167c478bd9Sstevel@tonic-gate (rc = inflate(&zstr, Z_NO_FLUSH)) != Z_STREAM_END ||
12177c478bd9Sstevel@tonic-gate (rc = inflateEnd(&zstr)) != Z_OK) {
12187c478bd9Sstevel@tonic-gate warning("CTF decompress zlib error %s\n", zError(rc));
1219*ad0b1ea5SRichard PALO return (0);
12207c478bd9Sstevel@tonic-gate }
12217c478bd9Sstevel@tonic-gate
12227c478bd9Sstevel@tonic-gate debug(3, "reflated %lu bytes to %lu, pointer at %d\n",
12237c478bd9Sstevel@tonic-gate zstr.total_in, zstr.total_out, (caddr_t)zstr.next_in - cbuf);
12247c478bd9Sstevel@tonic-gate
12257c478bd9Sstevel@tonic-gate return (zstr.total_out);
12267c478bd9Sstevel@tonic-gate }
12277c478bd9Sstevel@tonic-gate
12287c478bd9Sstevel@tonic-gate /*
12297c478bd9Sstevel@tonic-gate * Reconstruct the type tree from a given buffer of CTF data. Only the types
12307c478bd9Sstevel@tonic-gate * up to the type associated with the provided label, inclusive, will be
12317c478bd9Sstevel@tonic-gate * reconstructed. If a NULL label is provided, all types will be reconstructed.
12327c478bd9Sstevel@tonic-gate *
12337c478bd9Sstevel@tonic-gate * This function won't work on files that have been uniquified.
12347c478bd9Sstevel@tonic-gate */
12357c478bd9Sstevel@tonic-gate tdata_t *
ctf_load(char * file,caddr_t buf,size_t bufsz,symit_data_t * si,char * label)12367c478bd9Sstevel@tonic-gate ctf_load(char *file, caddr_t buf, size_t bufsz, symit_data_t *si, char *label)
12377c478bd9Sstevel@tonic-gate {
12387c478bd9Sstevel@tonic-gate ctf_header_t *h;
12397c478bd9Sstevel@tonic-gate caddr_t ctfdata;
12407c478bd9Sstevel@tonic-gate size_t ctfdatasz;
12417c478bd9Sstevel@tonic-gate tdata_t *td;
12427c478bd9Sstevel@tonic-gate
12437c478bd9Sstevel@tonic-gate curfile = file;
12447c478bd9Sstevel@tonic-gate
12457c478bd9Sstevel@tonic-gate if (bufsz < sizeof (ctf_header_t))
12467c478bd9Sstevel@tonic-gate parseterminate("Corrupt CTF - short header");
12477c478bd9Sstevel@tonic-gate
12487c478bd9Sstevel@tonic-gate /* LINTED - pointer alignment */
12497c478bd9Sstevel@tonic-gate h = (ctf_header_t *)buf;
12507c478bd9Sstevel@tonic-gate buf += sizeof (ctf_header_t);
12517c478bd9Sstevel@tonic-gate bufsz -= sizeof (ctf_header_t);
12527c478bd9Sstevel@tonic-gate
12537c478bd9Sstevel@tonic-gate if (h->cth_magic != CTF_MAGIC)
12547c478bd9Sstevel@tonic-gate parseterminate("Corrupt CTF - bad magic 0x%x", h->cth_magic);
12557c478bd9Sstevel@tonic-gate
12567c478bd9Sstevel@tonic-gate if (h->cth_version != CTF_VERSION)
12577c478bd9Sstevel@tonic-gate parseterminate("Unknown CTF version %d", h->cth_version);
12587c478bd9Sstevel@tonic-gate
12597c478bd9Sstevel@tonic-gate ctfdatasz = h->cth_stroff + h->cth_strlen;
12607c478bd9Sstevel@tonic-gate if (h->cth_flags & CTF_F_COMPRESS) {
12617c478bd9Sstevel@tonic-gate size_t actual;
12627c478bd9Sstevel@tonic-gate
12637c478bd9Sstevel@tonic-gate ctfdata = xmalloc(ctfdatasz);
12647c478bd9Sstevel@tonic-gate if ((actual = decompress_ctf(buf, bufsz, ctfdata, ctfdatasz)) !=
12657c478bd9Sstevel@tonic-gate ctfdatasz) {
12667c478bd9Sstevel@tonic-gate parseterminate("Corrupt CTF - short decompression "
12677c478bd9Sstevel@tonic-gate "(was %d, expecting %d)", actual, ctfdatasz);
12687c478bd9Sstevel@tonic-gate }
12697c478bd9Sstevel@tonic-gate } else {
12707c478bd9Sstevel@tonic-gate ctfdata = buf;
12717c478bd9Sstevel@tonic-gate ctfdatasz = bufsz;
12727c478bd9Sstevel@tonic-gate }
12737c478bd9Sstevel@tonic-gate
12747c478bd9Sstevel@tonic-gate td = ctf_parse(h, ctfdata, si, label);
12757c478bd9Sstevel@tonic-gate
12767c478bd9Sstevel@tonic-gate if (h->cth_flags & CTF_F_COMPRESS)
12777c478bd9Sstevel@tonic-gate free(ctfdata);
12787c478bd9Sstevel@tonic-gate
12797c478bd9Sstevel@tonic-gate curfile = NULL;
12807c478bd9Sstevel@tonic-gate
12817c478bd9Sstevel@tonic-gate return (td);
12827c478bd9Sstevel@tonic-gate }
1283