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