xref: /titanic_41/usr/src/tools/ctf/cvt/ctf.c (revision 03831d35f7499c87d51205817c93e9a8d42c4bae)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * Create and parse buffers containing CTF data.
31  */
32 
33 #include <sys/types.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <strings.h>
37 #include <ctype.h>
38 #include <zlib.h>
39 #include <elf.h>
40 
41 #include "ctf_headers.h"
42 #include "ctftools.h"
43 #include "strtab.h"
44 #include "memory.h"
45 
46 /*
47  * Name of the file currently being read, used to print error messages.  We
48  * assume that only one file will be read at a time, and thus make no attempt
49  * to allow curfile to be used simultaneously by multiple threads.
50  *
51  * The value is only valid during a call to ctf_load.
52  */
53 char *curfile;
54 
55 #define	CTF_BUF_CHUNK_SIZE	(64 * 1024)
56 #define	RES_BUF_CHUNK_SIZE	(64 * 1024)
57 
58 struct ctf_buf {
59 	strtab_t ctb_strtab;	/* string table */
60 	caddr_t ctb_base;	/* pointer to base of buffer */
61 	caddr_t ctb_end;	/* pointer to end of buffer */
62 	caddr_t ctb_ptr;	/* pointer to empty buffer space */
63 	size_t ctb_size;	/* size of buffer */
64 	int nptent;		/* number of processed types */
65 	int ntholes;		/* number of type holes */
66 };
67 
68 /*PRINTFLIKE1*/
69 static void
70 parseterminate(char *fmt, ...)
71 {
72 	static char msgbuf[1024]; /* sigh */
73 	va_list ap;
74 
75 	va_start(ap, fmt);
76 	vsnprintf(msgbuf, sizeof (msgbuf), fmt, ap);
77 	va_end(ap);
78 
79 	terminate("%s: %s\n", curfile, msgbuf);
80 }
81 
82 void
83 ctf_buf_grow(ctf_buf_t *b)
84 {
85 	off_t ptroff = b->ctb_ptr - b->ctb_base;
86 
87 	b->ctb_size += CTF_BUF_CHUNK_SIZE;
88 	b->ctb_base = xrealloc(b->ctb_base, b->ctb_size);
89 	b->ctb_end = b->ctb_base + b->ctb_size;
90 	b->ctb_ptr = b->ctb_base + ptroff;
91 }
92 
93 ctf_buf_t *
94 ctf_buf_new(void)
95 {
96 	ctf_buf_t *b = xcalloc(sizeof (ctf_buf_t));
97 
98 	strtab_create(&b->ctb_strtab);
99 	ctf_buf_grow(b);
100 
101 	return (b);
102 }
103 
104 void
105 ctf_buf_free(ctf_buf_t *b)
106 {
107 	strtab_destroy(&b->ctb_strtab);
108 	free(b->ctb_base);
109 	free(b);
110 }
111 
112 uint_t
113 ctf_buf_cur(ctf_buf_t *b)
114 {
115 	return (b->ctb_ptr - b->ctb_base);
116 }
117 
118 void
119 ctf_buf_write(ctf_buf_t *b, const void *p, size_t n)
120 {
121 	size_t len;
122 
123 	while (n != 0) {
124 		if (b->ctb_ptr == b->ctb_end)
125 			ctf_buf_grow(b);
126 
127 		len = MIN((size_t)(b->ctb_end - b->ctb_ptr), n);
128 		bcopy(p, b->ctb_ptr, len);
129 		b->ctb_ptr += len;
130 
131 		p = (char *)p + len;
132 		n -= len;
133 	}
134 }
135 
136 static int
137 write_label(labelent_t *le, ctf_buf_t *b)
138 {
139 	ctf_lblent_t ctl;
140 
141 	ctl.ctl_label = strtab_insert(&b->ctb_strtab, le->le_name);
142 	ctl.ctl_typeidx = le->le_idx;
143 
144 	ctf_buf_write(b, &ctl, sizeof (ctl));
145 
146 	return (1);
147 }
148 
149 static void
150 write_objects(iidesc_t *idp, ctf_buf_t *b)
151 {
152 	ushort_t id = (idp ? idp->ii_dtype->t_id : 0);
153 
154 	ctf_buf_write(b, &id, sizeof (id));
155 
156 	debug(3, "Wrote object %s (%d)\n", (idp ? idp->ii_name : "(null)"), id);
157 }
158 
159 static void
160 write_functions(iidesc_t *idp, ctf_buf_t *b)
161 {
162 	ushort_t fdata[2];
163 	ushort_t id;
164 	int nargs;
165 	int i;
166 
167 	if (!idp) {
168 		fdata[0] = 0;
169 		ctf_buf_write(b, &fdata[0], sizeof (fdata[0]));
170 
171 		debug(3, "Wrote function (null)\n");
172 		return;
173 	}
174 
175 	nargs = idp->ii_nargs + (idp->ii_vargs != 0);
176 	fdata[0] = CTF_TYPE_INFO(CTF_K_FUNCTION, 1, nargs);
177 	fdata[1] = idp->ii_dtype->t_id;
178 	ctf_buf_write(b, fdata, sizeof (fdata));
179 
180 	for (i = 0; i < idp->ii_nargs; i++) {
181 		id = idp->ii_args[i]->t_id;
182 		ctf_buf_write(b, &id, sizeof (id));
183 	}
184 
185 	if (idp->ii_vargs) {
186 		id = 0;
187 		ctf_buf_write(b, &id, sizeof (id));
188 	}
189 
190 	debug(3, "Wrote function %s (%d args)\n", idp->ii_name, nargs);
191 }
192 
193 /*
194  * Depending on the size of the type being described, either a ctf_stype_t (for
195  * types with size < CTF_LSTRUCT_THRESH) or a ctf_type_t (all others) will be
196  * written.  We isolate the determination here so the rest of the writer code
197  * doesn't need to care.
198  */
199 static void
200 write_sized_type_rec(ctf_buf_t *b, ctf_type_t *ctt, size_t size)
201 {
202 	if (size > CTF_MAX_SIZE) {
203 		ctt->ctt_size = CTF_LSIZE_SENT;
204 		ctt->ctt_lsizehi = CTF_SIZE_TO_LSIZE_HI(size);
205 		ctt->ctt_lsizelo = CTF_SIZE_TO_LSIZE_LO(size);
206 		ctf_buf_write(b, ctt, sizeof (*ctt));
207 	} else {
208 		ctf_stype_t *cts = (ctf_stype_t *)ctt;
209 
210 		cts->ctt_size = (ushort_t)size;
211 		ctf_buf_write(b, cts, sizeof (*cts));
212 	}
213 }
214 
215 static void
216 write_unsized_type_rec(ctf_buf_t *b, ctf_type_t *ctt)
217 {
218 	ctf_stype_t *cts = (ctf_stype_t *)ctt;
219 
220 	ctf_buf_write(b, cts, sizeof (*cts));
221 }
222 
223 static int
224 write_type(tdesc_t *tp, ctf_buf_t *b)
225 {
226 	elist_t *ep;
227 	mlist_t *mp;
228 	intr_t *ip;
229 
230 	size_t offset;
231 	uint_t encoding;
232 	uint_t data;
233 	int isroot = tp->t_flags & TDESC_F_ISROOT;
234 	int i;
235 
236 	ctf_type_t ctt;
237 	ctf_array_t cta;
238 	ctf_member_t ctm;
239 	ctf_lmember_t ctlm;
240 	ctf_enum_t cte;
241 	ushort_t id;
242 
243 	ctlm.ctlm_pad = 0;
244 
245 	/*
246 	 * There shouldn't be any holes in the type list (where a hole is
247 	 * defined as two consecutive tdescs without consecutive ids), but
248 	 * check for them just in case.  If we do find holes, we need to make
249 	 * fake entries to fill the holes, or we won't be able to reconstruct
250 	 * the tree from the written data.
251 	 */
252 	if (++b->nptent < CTF_TYPE_TO_INDEX(tp->t_id)) {
253 		debug(2, "genctf: type hole from %d < x < %d\n",
254 		    b->nptent - 1, CTF_TYPE_TO_INDEX(tp->t_id));
255 
256 		ctt.ctt_name = CTF_TYPE_NAME(CTF_STRTAB_0, 0);
257 		ctt.ctt_info = CTF_TYPE_INFO(0, 0, 0);
258 		while (b->nptent < CTF_TYPE_TO_INDEX(tp->t_id)) {
259 			write_sized_type_rec(b, &ctt, 0);
260 			b->nptent++;
261 		}
262 	}
263 
264 	offset = strtab_insert(&b->ctb_strtab, tp->t_name);
265 	ctt.ctt_name = CTF_TYPE_NAME(CTF_STRTAB_0, offset);
266 
267 	switch (tp->t_type) {
268 	case INTRINSIC:
269 		ip = tp->t_intr;
270 		if (ip->intr_type == INTR_INT)
271 			ctt.ctt_info = CTF_TYPE_INFO(CTF_K_INTEGER,
272 			    isroot, 1);
273 		else
274 			ctt.ctt_info = CTF_TYPE_INFO(CTF_K_FLOAT, isroot, 1);
275 		write_sized_type_rec(b, &ctt, tp->t_size);
276 
277 		encoding = 0;
278 
279 		if (ip->intr_type == INTR_INT) {
280 			if (ip->intr_signed)
281 				encoding |= CTF_INT_SIGNED;
282 			if (ip->intr_iformat == 'c')
283 				encoding |= CTF_INT_CHAR;
284 			else if (ip->intr_iformat == 'b')
285 				encoding |= CTF_INT_BOOL;
286 			else if (ip->intr_iformat == 'v')
287 				encoding |= CTF_INT_VARARGS;
288 		} else
289 			encoding = ip->intr_fformat;
290 
291 		data = CTF_INT_DATA(encoding, ip->intr_offset, ip->intr_nbits);
292 		ctf_buf_write(b, &data, sizeof (data));
293 		break;
294 
295 	case POINTER:
296 		ctt.ctt_info = CTF_TYPE_INFO(CTF_K_POINTER, isroot, 0);
297 		ctt.ctt_type = tp->t_tdesc->t_id;
298 		write_unsized_type_rec(b, &ctt);
299 		break;
300 
301 	case ARRAY:
302 		ctt.ctt_info = CTF_TYPE_INFO(CTF_K_ARRAY, isroot, 1);
303 		write_sized_type_rec(b, &ctt, tp->t_size);
304 
305 		cta.cta_contents = tp->t_ardef->ad_contents->t_id;
306 		cta.cta_index = tp->t_ardef->ad_idxtype->t_id;
307 		cta.cta_nelems = tp->t_ardef->ad_nelems;
308 		ctf_buf_write(b, &cta, sizeof (cta));
309 		break;
310 
311 	case STRUCT:
312 	case UNION:
313 		for (i = 0, mp = tp->t_members; mp != NULL; mp = mp->ml_next)
314 			i++; /* count up struct or union members */
315 
316 		if (tp->t_type == STRUCT)
317 			ctt.ctt_info = CTF_TYPE_INFO(CTF_K_STRUCT, isroot, i);
318 		else
319 			ctt.ctt_info = CTF_TYPE_INFO(CTF_K_UNION, isroot, i);
320 
321 		write_sized_type_rec(b, &ctt, tp->t_size);
322 
323 		if (tp->t_size < CTF_LSTRUCT_THRESH) {
324 			for (mp = tp->t_members; mp != NULL; mp = mp->ml_next) {
325 				offset = strtab_insert(&b->ctb_strtab,
326 				    mp->ml_name);
327 
328 				ctm.ctm_name = CTF_TYPE_NAME(CTF_STRTAB_0,
329 				    offset);
330 				ctm.ctm_type = mp->ml_type->t_id;
331 				ctm.ctm_offset = mp->ml_offset;
332 				ctf_buf_write(b, &ctm, sizeof (ctm));
333 			}
334 		} else {
335 			for (mp = tp->t_members; mp != NULL; mp = mp->ml_next) {
336 				offset = strtab_insert(&b->ctb_strtab,
337 				    mp->ml_name);
338 
339 				ctlm.ctlm_name = CTF_TYPE_NAME(CTF_STRTAB_0,
340 				    offset);
341 				ctlm.ctlm_type = mp->ml_type->t_id;
342 				ctlm.ctlm_offsethi =
343 				    CTF_OFFSET_TO_LMEMHI(mp->ml_offset);
344 				ctlm.ctlm_offsetlo =
345 				    CTF_OFFSET_TO_LMEMLO(mp->ml_offset);
346 				ctf_buf_write(b, &ctlm, sizeof (ctlm));
347 			}
348 		}
349 		break;
350 
351 	case ENUM:
352 		for (i = 0, ep = tp->t_emem; ep != NULL; ep = ep->el_next)
353 			i++; /* count up enum members */
354 
355 		ctt.ctt_info = CTF_TYPE_INFO(CTF_K_ENUM, isroot, i);
356 		write_sized_type_rec(b, &ctt, tp->t_size);
357 
358 		for (ep = tp->t_emem; ep != NULL; ep = ep->el_next) {
359 			offset = strtab_insert(&b->ctb_strtab, ep->el_name);
360 			cte.cte_name = CTF_TYPE_NAME(CTF_STRTAB_0, offset);
361 			cte.cte_value = ep->el_number;
362 			ctf_buf_write(b, &cte, sizeof (cte));
363 		}
364 		break;
365 
366 	case FORWARD:
367 		ctt.ctt_info = CTF_TYPE_INFO(CTF_K_FORWARD, isroot, 0);
368 		ctt.ctt_type = 0;
369 		write_unsized_type_rec(b, &ctt);
370 		break;
371 
372 	case TYPEDEF:
373 		ctt.ctt_info = CTF_TYPE_INFO(CTF_K_TYPEDEF, isroot, 0);
374 		ctt.ctt_type = tp->t_tdesc->t_id;
375 		write_unsized_type_rec(b, &ctt);
376 		break;
377 
378 	case VOLATILE:
379 		ctt.ctt_info = CTF_TYPE_INFO(CTF_K_VOLATILE, isroot, 0);
380 		ctt.ctt_type = tp->t_tdesc->t_id;
381 		write_unsized_type_rec(b, &ctt);
382 		break;
383 
384 	case CONST:
385 		ctt.ctt_info = CTF_TYPE_INFO(CTF_K_CONST, isroot, 0);
386 		ctt.ctt_type = tp->t_tdesc->t_id;
387 		write_unsized_type_rec(b, &ctt);
388 		break;
389 
390 	case FUNCTION:
391 		ctt.ctt_info = CTF_TYPE_INFO(CTF_K_FUNCTION, isroot,
392 		    tp->t_fndef->fn_nargs + tp->t_fndef->fn_vargs);
393 		ctt.ctt_type = tp->t_fndef->fn_ret->t_id;
394 		write_unsized_type_rec(b, &ctt);
395 
396 		for (i = 0; i < tp->t_fndef->fn_nargs; i++) {
397 			id = tp->t_fndef->fn_args[i]->t_id;
398 			ctf_buf_write(b, &id, sizeof (id));
399 		}
400 
401 		if (tp->t_fndef->fn_vargs) {
402 			id = 0;
403 			ctf_buf_write(b, &id, sizeof (id));
404 			i++;
405 		}
406 
407 		if (i & 1) {
408 			id = 0;
409 			ctf_buf_write(b, &id, sizeof (id));
410 		}
411 		break;
412 
413 	case RESTRICT:
414 		ctt.ctt_info = CTF_TYPE_INFO(CTF_K_RESTRICT, isroot, 0);
415 		ctt.ctt_type = tp->t_tdesc->t_id;
416 		write_unsized_type_rec(b, &ctt);
417 		break;
418 
419 	default:
420 		warning("Can't write unknown type %d\n", tp->t_type);
421 	}
422 
423 	debug(3, "Wrote type %d %s\n", tp->t_id,
424 	    (tp->t_name ? tp->t_name : "(anon)"));
425 
426 	return (1);
427 }
428 
429 typedef struct resbuf {
430 	caddr_t rb_base;
431 	caddr_t rb_ptr;
432 	size_t rb_size;
433 	z_stream rb_zstr;
434 } resbuf_t;
435 
436 static void
437 rbzs_grow(resbuf_t *rb)
438 {
439 	off_t ptroff = (caddr_t)rb->rb_zstr.next_out - rb->rb_base;
440 
441 	rb->rb_size += RES_BUF_CHUNK_SIZE;
442 	rb->rb_base = xrealloc(rb->rb_base, rb->rb_size);
443 	rb->rb_ptr = rb->rb_base + ptroff;
444 	rb->rb_zstr.next_out = (Bytef *)(rb->rb_ptr);
445 	rb->rb_zstr.avail_out += RES_BUF_CHUNK_SIZE;
446 }
447 
448 static void
449 compress_start(resbuf_t *rb)
450 {
451 	int rc;
452 
453 	rb->rb_zstr.zalloc = (alloc_func)0;
454 	rb->rb_zstr.zfree = (free_func)0;
455 	rb->rb_zstr.opaque = (voidpf)0;
456 
457 	if ((rc = deflateInit(&rb->rb_zstr, Z_BEST_COMPRESSION)) != Z_OK)
458 		parseterminate("zlib start failed: %s", zError(rc));
459 }
460 
461 static void
462 compress_buffer(caddr_t buf, size_t n, resbuf_t *rb)
463 {
464 	int rc;
465 
466 	rb->rb_zstr.next_out = (Bytef *)rb->rb_ptr;
467 	rb->rb_zstr.avail_out = rb->rb_size - (rb->rb_ptr - rb->rb_base);
468 	rb->rb_zstr.next_in = (Bytef *)buf;
469 	rb->rb_zstr.avail_in = n;
470 
471 	while (rb->rb_zstr.avail_in) {
472 		if (rb->rb_zstr.avail_out == 0)
473 			rbzs_grow(rb);
474 
475 		if ((rc = deflate(&rb->rb_zstr, Z_NO_FLUSH)) != Z_OK)
476 			parseterminate("zlib deflate failed: %s", zError(rc));
477 	}
478 	rb->rb_ptr = (caddr_t)rb->rb_zstr.next_out;
479 }
480 
481 static void
482 compress_flush(resbuf_t *rb, int type)
483 {
484 	int rc;
485 
486 	for (;;) {
487 		if (rb->rb_zstr.avail_out == 0)
488 			rbzs_grow(rb);
489 
490 		rc = deflate(&rb->rb_zstr, type);
491 		if ((type == Z_FULL_FLUSH && rc == Z_BUF_ERROR) ||
492 		    (type == Z_FINISH && rc == Z_STREAM_END))
493 			break;
494 		else if (rc != Z_OK)
495 			parseterminate("zlib finish failed: %s", zError(rc));
496 	}
497 	rb->rb_ptr = (caddr_t)rb->rb_zstr.next_out;
498 }
499 
500 static void
501 compress_end(resbuf_t *rb)
502 {
503 	int rc;
504 
505 	compress_flush(rb, Z_FINISH);
506 
507 	if ((rc = deflateEnd(&rb->rb_zstr)) != Z_OK)
508 		parseterminate("zlib end failed: %s", zError(rc));
509 }
510 
511 /*
512  * Pad the buffer to a power-of-2 boundary
513  */
514 static void
515 pad_buffer(ctf_buf_t *buf, int align)
516 {
517 	uint_t cur = ctf_buf_cur(buf);
518 	ssize_t topad = (align - (cur % align)) % align;
519 	static const char pad[8] = { 0 };
520 
521 	while (topad > 0) {
522 		ctf_buf_write(buf, pad, (topad > 8 ? 8 : topad));
523 		topad -= 8;
524 	}
525 }
526 
527 static void
528 bcopy_data(void *buf, size_t n, caddr_t *posp)
529 {
530 	bcopy(buf, *posp, n);
531 	*posp += n;
532 }
533 
534 static caddr_t
535 write_buffer(ctf_header_t *h, ctf_buf_t *buf, size_t *resszp)
536 {
537 	caddr_t outbuf;
538 	caddr_t bufpos;
539 
540 	outbuf = xmalloc(sizeof (ctf_header_t) + (buf->ctb_ptr - buf->ctb_base)
541 	    + buf->ctb_strtab.str_size);
542 
543 	bufpos = outbuf;
544 	bcopy_data(h, sizeof (ctf_header_t), &bufpos);
545 	bcopy_data(buf->ctb_base, buf->ctb_ptr - buf->ctb_base,
546 	    &bufpos);
547 	if (strtab_write(&buf->ctb_strtab, (ssize_t (*)())bcopy_data,
548 	    &bufpos) < 0)
549 		terminate("strtab_write failed\n");
550 	*resszp = bufpos - outbuf;
551 	return (outbuf);
552 }
553 
554 /*
555  * Create the compression buffer, and fill it with the CTF and string
556  * table data.  We flush the compression state between the two so the
557  * dictionary used for the string tables won't be polluted with values
558  * that made sense for the CTF data.
559  */
560 static caddr_t
561 write_compressed_buffer(ctf_header_t *h, ctf_buf_t *buf, size_t *resszp)
562 {
563 	resbuf_t resbuf;
564 	resbuf.rb_size = RES_BUF_CHUNK_SIZE;
565 	resbuf.rb_base = xmalloc(resbuf.rb_size);
566 	bcopy(h, resbuf.rb_base, sizeof (ctf_header_t));
567 	resbuf.rb_ptr = resbuf.rb_base + sizeof (ctf_header_t);
568 
569 	compress_start(&resbuf);
570 	compress_buffer(buf->ctb_base, buf->ctb_ptr - buf->ctb_base, &resbuf);
571 	compress_flush(&resbuf, Z_FULL_FLUSH);
572 	if (strtab_write(&buf->ctb_strtab, (ssize_t (*)())compress_buffer,
573 	    &resbuf) < 0)
574 		terminate("strtab_write failed\n");
575 	compress_end(&resbuf);
576 
577 	*resszp = (resbuf.rb_ptr - resbuf.rb_base);
578 	return (resbuf.rb_base);
579 }
580 
581 caddr_t
582 ctf_gen(iiburst_t *iiburst, size_t *resszp, int do_compress)
583 {
584 	ctf_buf_t *buf = ctf_buf_new();
585 	ctf_header_t h;
586 	caddr_t outbuf;
587 
588 	int i;
589 
590 	/*
591 	 * Prepare the header, and create the CTF output buffers.  The data
592 	 * object section and function section are both lists of 2-byte
593 	 * integers; we pad these out to the next 4-byte boundary if needed.
594 	 */
595 	h.cth_magic = CTF_MAGIC;
596 	h.cth_version = CTF_VERSION;
597 	h.cth_flags = do_compress ? CTF_F_COMPRESS : 0;
598 	h.cth_parlabel = strtab_insert(&buf->ctb_strtab,
599 	    iiburst->iib_td->td_parlabel);
600 	h.cth_parname = strtab_insert(&buf->ctb_strtab,
601 	    iiburst->iib_td->td_parname);
602 
603 	h.cth_lbloff = 0;
604 	(void) list_iter(iiburst->iib_td->td_labels, (int (*)())write_label,
605 	    buf);
606 
607 	pad_buffer(buf, 2);
608 	h.cth_objtoff = ctf_buf_cur(buf);
609 	for (i = 0; i < iiburst->iib_nobjts; i++)
610 		write_objects(iiburst->iib_objts[i], buf);
611 
612 	pad_buffer(buf, 2);
613 	h.cth_funcoff = ctf_buf_cur(buf);
614 	for (i = 0; i < iiburst->iib_nfuncs; i++)
615 		write_functions(iiburst->iib_funcs[i], buf);
616 
617 	pad_buffer(buf, 4);
618 	h.cth_typeoff = ctf_buf_cur(buf);
619 	(void) list_iter(iiburst->iib_types, (int (*)())write_type, buf);
620 
621 	debug(2, "CTF wrote %d types\n", list_count(iiburst->iib_types));
622 
623 	h.cth_stroff = ctf_buf_cur(buf);
624 	h.cth_strlen = strtab_size(&buf->ctb_strtab);
625 
626 	/*
627 	 * We only do compression for ctfmerge, as ctfconvert is only
628 	 * supposed to be used on intermediary build objects. This is
629 	 * significantly faster.
630 	 */
631 	if (do_compress)
632 		outbuf = write_compressed_buffer(&h, buf, resszp);
633 	else
634 		outbuf = write_buffer(&h, buf, resszp);
635 
636 	ctf_buf_free(buf);
637 	return (outbuf);
638 }
639 
640 void
641 get_ctt_size(ctf_type_t *ctt, size_t *sizep, size_t *incrementp)
642 {
643 	if (ctt->ctt_size == CTF_LSIZE_SENT) {
644 		*sizep = (size_t)CTF_TYPE_LSIZE(ctt);
645 		*incrementp = sizeof (ctf_type_t);
646 	} else {
647 		*sizep = ctt->ctt_size;
648 		*incrementp = sizeof (ctf_stype_t);
649 	}
650 }
651 
652 static int
653 count_types(ctf_header_t *h, caddr_t data)
654 {
655 	caddr_t dptr = data + h->cth_typeoff;
656 	int count = 0;
657 
658 	dptr = data + h->cth_typeoff;
659 	while (dptr < data + h->cth_stroff) {
660 		/* LINTED - pointer alignment */
661 		ctf_type_t *ctt = (ctf_type_t *)dptr;
662 		size_t vlen = CTF_INFO_VLEN(ctt->ctt_info);
663 		size_t size, increment;
664 
665 		get_ctt_size(ctt, &size, &increment);
666 
667 		switch (CTF_INFO_KIND(ctt->ctt_info)) {
668 		case CTF_K_INTEGER:
669 		case CTF_K_FLOAT:
670 			dptr += 4;
671 			break;
672 		case CTF_K_POINTER:
673 		case CTF_K_FORWARD:
674 		case CTF_K_TYPEDEF:
675 		case CTF_K_VOLATILE:
676 		case CTF_K_CONST:
677 		case CTF_K_RESTRICT:
678 		case CTF_K_FUNCTION:
679 			dptr += sizeof (ushort_t) * (vlen + (vlen & 1));
680 			break;
681 		case CTF_K_ARRAY:
682 			dptr += sizeof (ctf_array_t);
683 			break;
684 		case CTF_K_STRUCT:
685 		case CTF_K_UNION:
686 			if (size < CTF_LSTRUCT_THRESH)
687 				dptr += sizeof (ctf_member_t) * vlen;
688 			else
689 				dptr += sizeof (ctf_lmember_t) * vlen;
690 			break;
691 		case CTF_K_ENUM:
692 			dptr += sizeof (ctf_enum_t) * vlen;
693 			break;
694 		case CTF_K_UNKNOWN:
695 			break;
696 		default:
697 			parseterminate("Unknown CTF type %d (#%d) at %#x",
698 			    CTF_INFO_KIND(ctt->ctt_info), count, dptr - data);
699 		}
700 
701 		dptr += increment;
702 		count++;
703 	}
704 
705 	debug(3, "CTF read %d types\n", count);
706 
707 	return (count);
708 }
709 
710 /*
711  * Resurrect the labels stored in the CTF data, returning the index associated
712  * with a label provided by the caller.  There are several cases, outlined
713  * below.  Note that, given two labels, the one associated with the lesser type
714  * index is considered to be older than the other.
715  *
716  *  1. matchlbl == NULL - return the index of the most recent label.
717  *  2. matchlbl == "BASE" - return the index of the oldest label.
718  *  3. matchlbl != NULL, but doesn't match any labels in the section - warn
719  *	the user, and proceed as if matchlbl == "BASE" (for safety).
720  *  4. matchlbl != NULL, and matches one of the labels in the section - return
721  *	the type index associated with the label.
722  */
723 static int
724 resurrect_labels(ctf_header_t *h, tdata_t *td, caddr_t ctfdata, char *matchlbl)
725 {
726 	caddr_t buf = ctfdata + h->cth_lbloff;
727 	caddr_t sbuf = ctfdata + h->cth_stroff;
728 	size_t bufsz = h->cth_objtoff - h->cth_lbloff;
729 	int lastidx = 0, baseidx = -1;
730 	char *baselabel;
731 	ctf_lblent_t *ctl;
732 
733 	/* LINTED - pointer alignment */
734 	for (ctl = (ctf_lblent_t *)buf; (caddr_t)ctl < buf + bufsz; ctl++) {
735 		char *label = sbuf + ctl->ctl_label;
736 
737 		lastidx = ctl->ctl_typeidx;
738 
739 		debug(3, "Resurrected label %s type idx %d\n", label, lastidx);
740 
741 		tdata_label_add(td, label, lastidx);
742 
743 		if (baseidx == -1) {
744 			baseidx = lastidx;
745 			baselabel = label;
746 			if (matchlbl != NULL && streq(matchlbl, "BASE"))
747 				return (lastidx);
748 		}
749 
750 		if (matchlbl != NULL && streq(label, matchlbl))
751 			return (lastidx);
752 	}
753 
754 	if (matchlbl != NULL) {
755 		/* User provided a label that didn't match */
756 		warning("%s: Cannot find label `%s' - using base (%s)\n",
757 		    curfile, matchlbl, (baselabel ? baselabel : "NONE"));
758 
759 		tdata_label_free(td);
760 		tdata_label_add(td, baselabel, baseidx);
761 
762 		return (baseidx);
763 	}
764 
765 	return (lastidx);
766 }
767 
768 static void
769 resurrect_objects(ctf_header_t *h, tdata_t *td, tdesc_t **tdarr, int tdsize,
770     caddr_t ctfdata, symit_data_t *si)
771 {
772 	caddr_t buf = ctfdata + h->cth_objtoff;
773 	size_t bufsz = h->cth_funcoff - h->cth_objtoff;
774 	caddr_t dptr;
775 
776 	symit_reset(si);
777 	for (dptr = buf; dptr < buf + bufsz; dptr += 2) {
778 		/* LINTED - pointer alignment */
779 		ushort_t id = *((ushort_t *)dptr);
780 		iidesc_t *ii;
781 		GElf_Sym *sym;
782 
783 		if (!(sym = symit_next(si, STT_OBJECT)) && id != 0) {
784 			parseterminate(
785 			    "Unexpected end of object symbols at %x of %x",
786 			    dptr - buf, bufsz);
787 		}
788 
789 		if (id == 0) {
790 			debug(3, "Skipping null object\n");
791 			continue;
792 		} else if (id >= tdsize) {
793 			parseterminate("Reference to invalid type %d", id);
794 		}
795 
796 		ii = iidesc_new(symit_name(si));
797 		ii->ii_dtype = tdarr[id];
798 		if (GELF_ST_BIND(sym->st_info) == STB_LOCAL) {
799 			ii->ii_type = II_SVAR;
800 			ii->ii_owner = xstrdup(symit_curfile(si));
801 		} else
802 			ii->ii_type = II_GVAR;
803 		hash_add(td->td_iihash, ii);
804 
805 		debug(3, "Resurrected %s object %s (%d) from %s\n",
806 		    (ii->ii_type == II_GVAR ? "global" : "static"),
807 		    ii->ii_name, id, (ii->ii_owner ? ii->ii_owner : "(none)"));
808 	}
809 }
810 
811 static void
812 resurrect_functions(ctf_header_t *h, tdata_t *td, tdesc_t **tdarr, int tdsize,
813     caddr_t ctfdata, symit_data_t *si)
814 {
815 	caddr_t buf = ctfdata + h->cth_funcoff;
816 	size_t bufsz = h->cth_typeoff - h->cth_funcoff;
817 	caddr_t dptr = buf;
818 	iidesc_t *ii;
819 	ushort_t info;
820 	ushort_t retid;
821 	GElf_Sym *sym;
822 	int i;
823 
824 	symit_reset(si);
825 	while (dptr < buf + bufsz) {
826 		/* LINTED - pointer alignment */
827 		info = *((ushort_t *)dptr);
828 		dptr += 2;
829 
830 		if (!(sym = symit_next(si, STT_FUNC)) && info != 0)
831 			parseterminate("Unexpected end of function symbols");
832 
833 		if (info == 0) {
834 			debug(3, "Skipping null function (%s)\n",
835 			    symit_name(si));
836 			continue;
837 		}
838 
839 		/* LINTED - pointer alignment */
840 		retid = *((ushort_t *)dptr);
841 		dptr += 2;
842 
843 		if (retid >= tdsize)
844 			parseterminate("Reference to invalid type %d", retid);
845 
846 		ii = iidesc_new(symit_name(si));
847 		ii->ii_dtype = tdarr[retid];
848 		if (GELF_ST_BIND(sym->st_info) == STB_LOCAL) {
849 			ii->ii_type = II_SFUN;
850 			ii->ii_owner = xstrdup(symit_curfile(si));
851 		} else
852 			ii->ii_type = II_GFUN;
853 		ii->ii_nargs = CTF_INFO_VLEN(info);
854 		if (ii->ii_nargs)
855 			ii->ii_args =
856 			    xmalloc(sizeof (tdesc_t *) * ii->ii_nargs);
857 
858 		for (i = 0; i < ii->ii_nargs; i++, dptr += 2) {
859 			/* LINTED - pointer alignment */
860 			ushort_t id = *((ushort_t *)dptr);
861 			if (id >= tdsize)
862 				parseterminate("Reference to invalid type %d",
863 				    id);
864 			ii->ii_args[i] = tdarr[id];
865 		}
866 
867 		if (ii->ii_nargs && ii->ii_args[ii->ii_nargs - 1] == NULL) {
868 			ii->ii_nargs--;
869 			ii->ii_vargs = 1;
870 		}
871 
872 		hash_add(td->td_iihash, ii);
873 
874 		debug(3, "Resurrected %s function %s (%d, %d args)\n",
875 		    (ii->ii_type == II_GFUN ? "global" : "static"),
876 		    ii->ii_name, retid, ii->ii_nargs);
877 	}
878 }
879 
880 static void
881 resurrect_types(ctf_header_t *h, tdata_t *td, tdesc_t **tdarr, int tdsize,
882     caddr_t ctfdata, int maxid)
883 {
884 	caddr_t buf = ctfdata + h->cth_typeoff;
885 	size_t bufsz = h->cth_stroff - h->cth_typeoff;
886 	caddr_t sbuf = ctfdata + h->cth_stroff;
887 	caddr_t dptr = buf;
888 	tdesc_t *tdp;
889 	uint_t data;
890 	uint_t encoding;
891 	size_t size, increment;
892 	int tcnt;
893 	int iicnt = 0;
894 	tid_t tid, argid;
895 	int kind, vlen;
896 	int i;
897 
898 	elist_t **epp;
899 	mlist_t **mpp;
900 	intr_t *ip;
901 
902 	ctf_type_t *ctt;
903 	ctf_array_t *cta;
904 	ctf_enum_t *cte;
905 
906 	/*
907 	 * A maxid of zero indicates a request to resurrect all types, so reset
908 	 * maxid to the maximum type id.
909 	 */
910 	if (maxid == 0)
911 		maxid = CTF_MAX_TYPE;
912 
913 	for (dptr = buf, tcnt = 0, tid = 1; dptr < buf + bufsz; tcnt++, tid++) {
914 		if (tid > maxid)
915 			break;
916 
917 		if (tid >= tdsize)
918 			parseterminate("Reference to invalid type %d", tid);
919 
920 		/* LINTED - pointer alignment */
921 		ctt = (ctf_type_t *)dptr;
922 
923 		get_ctt_size(ctt, &size, &increment);
924 		dptr += increment;
925 
926 		tdp = tdarr[tid];
927 
928 		if (CTF_NAME_STID(ctt->ctt_name) != CTF_STRTAB_0)
929 			parseterminate(
930 				"Unable to cope with non-zero strtab id");
931 		if (CTF_NAME_OFFSET(ctt->ctt_name) != 0) {
932 			tdp->t_name =
933 			    xstrdup(sbuf + CTF_NAME_OFFSET(ctt->ctt_name));
934 		} else
935 			tdp->t_name = NULL;
936 
937 		kind = CTF_INFO_KIND(ctt->ctt_info);
938 		vlen = CTF_INFO_VLEN(ctt->ctt_info);
939 
940 		switch (kind) {
941 		case CTF_K_INTEGER:
942 			tdp->t_type = INTRINSIC;
943 			tdp->t_size = size;
944 
945 			/* LINTED - pointer alignment */
946 			data = *((uint_t *)dptr);
947 			dptr += sizeof (uint_t);
948 			encoding = CTF_INT_ENCODING(data);
949 
950 			ip = xmalloc(sizeof (intr_t));
951 			ip->intr_type = INTR_INT;
952 			ip->intr_signed = (encoding & CTF_INT_SIGNED) ? 1 : 0;
953 
954 			if (encoding & CTF_INT_CHAR)
955 				ip->intr_iformat = 'c';
956 			else if (encoding & CTF_INT_BOOL)
957 				ip->intr_iformat = 'b';
958 			else if (encoding & CTF_INT_VARARGS)
959 				ip->intr_iformat = 'v';
960 			else
961 				ip->intr_iformat = '\0';
962 
963 			ip->intr_offset = CTF_INT_OFFSET(data);
964 			ip->intr_nbits = CTF_INT_BITS(data);
965 			tdp->t_intr = ip;
966 			break;
967 
968 		case CTF_K_FLOAT:
969 			tdp->t_type = INTRINSIC;
970 			tdp->t_size = size;
971 
972 			/* LINTED - pointer alignment */
973 			data = *((uint_t *)dptr);
974 			dptr += sizeof (uint_t);
975 
976 			ip = xcalloc(sizeof (intr_t));
977 			ip->intr_type = INTR_REAL;
978 			ip->intr_fformat = CTF_FP_ENCODING(data);
979 			ip->intr_offset = CTF_FP_OFFSET(data);
980 			ip->intr_nbits = CTF_FP_BITS(data);
981 			tdp->t_intr = ip;
982 			break;
983 
984 		case CTF_K_POINTER:
985 			tdp->t_type = POINTER;
986 			tdp->t_tdesc = tdarr[ctt->ctt_type];
987 			break;
988 
989 		case CTF_K_ARRAY:
990 			tdp->t_type = ARRAY;
991 			tdp->t_size = size;
992 
993 			/* LINTED - pointer alignment */
994 			cta = (ctf_array_t *)dptr;
995 			dptr += sizeof (ctf_array_t);
996 
997 			tdp->t_ardef = xmalloc(sizeof (ardef_t));
998 			tdp->t_ardef->ad_contents = tdarr[cta->cta_contents];
999 			tdp->t_ardef->ad_idxtype = tdarr[cta->cta_index];
1000 			tdp->t_ardef->ad_nelems = cta->cta_nelems;
1001 			break;
1002 
1003 		case CTF_K_STRUCT:
1004 		case CTF_K_UNION:
1005 			tdp->t_type = (kind == CTF_K_STRUCT ? STRUCT : UNION);
1006 			tdp->t_size = size;
1007 
1008 			if (size < CTF_LSTRUCT_THRESH) {
1009 				for (i = 0, mpp = &tdp->t_members; i < vlen;
1010 				    i++, mpp = &((*mpp)->ml_next)) {
1011 					/* LINTED - pointer alignment */
1012 					ctf_member_t *ctm = (ctf_member_t *)
1013 					    dptr;
1014 					dptr += sizeof (ctf_member_t);
1015 
1016 					*mpp = xmalloc(sizeof (mlist_t));
1017 					(*mpp)->ml_name = xstrdup(sbuf +
1018 					    ctm->ctm_name);
1019 					(*mpp)->ml_type = tdarr[ctm->ctm_type];
1020 					(*mpp)->ml_offset = ctm->ctm_offset;
1021 					(*mpp)->ml_size = 0;
1022 				}
1023 			} else {
1024 				for (i = 0, mpp = &tdp->t_members; i < vlen;
1025 				    i++, mpp = &((*mpp)->ml_next)) {
1026 					/* LINTED - pointer alignment */
1027 					ctf_lmember_t *ctlm = (ctf_lmember_t *)
1028 					    dptr;
1029 					dptr += sizeof (ctf_lmember_t);
1030 
1031 					*mpp = xmalloc(sizeof (mlist_t));
1032 					(*mpp)->ml_name = xstrdup(sbuf +
1033 					    ctlm->ctlm_name);
1034 					(*mpp)->ml_type =
1035 					    tdarr[ctlm->ctlm_type];
1036 					(*mpp)->ml_offset =
1037 					    (int)CTF_LMEM_OFFSET(ctlm);
1038 					(*mpp)->ml_size = 0;
1039 				}
1040 			}
1041 
1042 			*mpp = NULL;
1043 			break;
1044 
1045 		case CTF_K_ENUM:
1046 			tdp->t_type = ENUM;
1047 			tdp->t_size = size;
1048 
1049 			for (i = 0, epp = &tdp->t_emem; i < vlen;
1050 			    i++, epp = &((*epp)->el_next)) {
1051 				/* LINTED - pointer alignment */
1052 				cte = (ctf_enum_t *)dptr;
1053 				dptr += sizeof (ctf_enum_t);
1054 
1055 				*epp = xmalloc(sizeof (elist_t));
1056 				(*epp)->el_name = xstrdup(sbuf + cte->cte_name);
1057 				(*epp)->el_number = cte->cte_value;
1058 			}
1059 			*epp = NULL;
1060 			break;
1061 
1062 		case CTF_K_FORWARD:
1063 			tdp->t_type = FORWARD;
1064 			list_add(&td->td_fwdlist, tdp);
1065 			break;
1066 
1067 		case CTF_K_TYPEDEF:
1068 			tdp->t_type = TYPEDEF;
1069 			tdp->t_tdesc = tdarr[ctt->ctt_type];
1070 			break;
1071 
1072 		case CTF_K_VOLATILE:
1073 			tdp->t_type = VOLATILE;
1074 			tdp->t_tdesc = tdarr[ctt->ctt_type];
1075 			break;
1076 
1077 		case CTF_K_CONST:
1078 			tdp->t_type = CONST;
1079 			tdp->t_tdesc = tdarr[ctt->ctt_type];
1080 			break;
1081 
1082 		case CTF_K_FUNCTION:
1083 			tdp->t_type = FUNCTION;
1084 			tdp->t_fndef = xcalloc(sizeof (fndef_t));
1085 			tdp->t_fndef->fn_ret = tdarr[ctt->ctt_type];
1086 
1087 			/* LINTED - pointer alignment */
1088 			if (vlen > 0 && *(ushort_t *)(dptr +
1089 			    (sizeof (ushort_t) * (vlen - 1))) == 0)
1090 				tdp->t_fndef->fn_vargs = 1;
1091 
1092 			tdp->t_fndef->fn_nargs = vlen - tdp->t_fndef->fn_vargs;
1093 			tdp->t_fndef->fn_args = xcalloc(sizeof (tdesc_t) *
1094 			    vlen - tdp->t_fndef->fn_vargs);
1095 
1096 			for (i = 0; i < vlen; i++) {
1097 				/* LINTED - pointer alignment */
1098 				argid = *(ushort_t *)dptr;
1099 				dptr += sizeof (ushort_t);
1100 
1101 				if (argid != 0)
1102 					tdp->t_fndef->fn_args[i] = tdarr[argid];
1103 			}
1104 
1105 			if (vlen & 1)
1106 				dptr += sizeof (ushort_t);
1107 			break;
1108 
1109 		case CTF_K_RESTRICT:
1110 			tdp->t_type = RESTRICT;
1111 			tdp->t_tdesc = tdarr[ctt->ctt_type];
1112 			break;
1113 
1114 		case CTF_K_UNKNOWN:
1115 			break;
1116 
1117 		default:
1118 			warning("Can't parse unknown CTF type %d\n", kind);
1119 		}
1120 
1121 		if (CTF_INFO_ISROOT(ctt->ctt_info)) {
1122 			iidesc_t *ii = iidesc_new(tdp->t_name);
1123 			if (tdp->t_type == STRUCT || tdp->t_type == UNION ||
1124 			    tdp->t_type == ENUM)
1125 				ii->ii_type = II_SOU;
1126 			else
1127 				ii->ii_type = II_TYPE;
1128 			ii->ii_dtype = tdp;
1129 			hash_add(td->td_iihash, ii);
1130 
1131 			iicnt++;
1132 		}
1133 
1134 		debug(3, "Resurrected %d %stype %s (%d)\n", tdp->t_type,
1135 		    (CTF_INFO_ISROOT(ctt->ctt_info) ? "root " : ""),
1136 		    (tdp->t_name ? tdp->t_name : "(anon)"), tdp->t_id);
1137 	}
1138 
1139 	debug(3, "Resurrected %d types (%d were roots)\n", tcnt, iicnt);
1140 }
1141 
1142 /*
1143  * For lack of other inspiration, we're going to take the boring route.  We
1144  * count the number of types.  This lets us malloc that many tdesc structs
1145  * before we start filling them in.  This has the advantage of allowing us to
1146  * avoid a merge-esque remap step.
1147  */
1148 static tdata_t *
1149 ctf_parse(ctf_header_t *h, caddr_t buf, symit_data_t *si, char *label)
1150 {
1151 	tdata_t *td = tdata_new();
1152 	tdesc_t **tdarr;
1153 	int ntypes = count_types(h, buf);
1154 	int idx, i;
1155 
1156 	/* shudder */
1157 	tdarr = xcalloc(sizeof (tdesc_t *) * (ntypes + 1));
1158 	tdarr[0] = NULL;
1159 	for (i = 1; i <= ntypes; i++) {
1160 		tdarr[i] = xcalloc(sizeof (tdesc_t));
1161 		tdarr[i]->t_id = i;
1162 	}
1163 
1164 	td->td_parlabel = xstrdup(buf + h->cth_stroff + h->cth_parlabel);
1165 
1166 	/* we have the technology - we can rebuild them */
1167 	idx = resurrect_labels(h, td, buf, label);
1168 
1169 	resurrect_objects(h, td, tdarr, ntypes + 1, buf, si);
1170 	resurrect_functions(h, td, tdarr, ntypes + 1, buf, si);
1171 	resurrect_types(h, td, tdarr, ntypes + 1, buf, idx);
1172 
1173 	free(tdarr);
1174 
1175 	td->td_nextid = ntypes + 1;
1176 
1177 	return (td);
1178 }
1179 
1180 static size_t
1181 decompress_ctf(caddr_t cbuf, size_t cbufsz, caddr_t dbuf, size_t dbufsz)
1182 {
1183 	z_stream zstr;
1184 	int rc;
1185 
1186 	zstr.zalloc = (alloc_func)0;
1187 	zstr.zfree = (free_func)0;
1188 	zstr.opaque = (voidpf)0;
1189 
1190 	zstr.next_in = (Bytef *)cbuf;
1191 	zstr.avail_in = cbufsz;
1192 	zstr.next_out = (Bytef *)dbuf;
1193 	zstr.avail_out = dbufsz;
1194 
1195 	if ((rc = inflateInit(&zstr)) != Z_OK ||
1196 	    (rc = inflate(&zstr, Z_NO_FLUSH)) != Z_STREAM_END ||
1197 	    (rc = inflateEnd(&zstr)) != Z_OK) {
1198 		warning("CTF decompress zlib error %s\n", zError(rc));
1199 		return (NULL);
1200 	}
1201 
1202 	debug(3, "reflated %lu bytes to %lu, pointer at %d\n",
1203 	    zstr.total_in, zstr.total_out, (caddr_t)zstr.next_in - cbuf);
1204 
1205 	return (zstr.total_out);
1206 }
1207 
1208 /*
1209  * Reconstruct the type tree from a given buffer of CTF data.  Only the types
1210  * up to the type associated with the provided label, inclusive, will be
1211  * reconstructed.  If a NULL label is provided, all types will be reconstructed.
1212  *
1213  * This function won't work on files that have been uniquified.
1214  */
1215 tdata_t *
1216 ctf_load(char *file, caddr_t buf, size_t bufsz, symit_data_t *si, char *label)
1217 {
1218 	ctf_header_t *h;
1219 	caddr_t ctfdata;
1220 	size_t ctfdatasz;
1221 	tdata_t *td;
1222 
1223 	curfile = file;
1224 
1225 	if (bufsz < sizeof (ctf_header_t))
1226 		parseterminate("Corrupt CTF - short header");
1227 
1228 	/* LINTED - pointer alignment */
1229 	h = (ctf_header_t *)buf;
1230 	buf += sizeof (ctf_header_t);
1231 	bufsz -= sizeof (ctf_header_t);
1232 
1233 	if (h->cth_magic != CTF_MAGIC)
1234 		parseterminate("Corrupt CTF - bad magic 0x%x", h->cth_magic);
1235 
1236 	if (h->cth_version != CTF_VERSION)
1237 		parseterminate("Unknown CTF version %d", h->cth_version);
1238 
1239 	ctfdatasz = h->cth_stroff + h->cth_strlen;
1240 	if (h->cth_flags & CTF_F_COMPRESS) {
1241 		size_t actual;
1242 
1243 		ctfdata = xmalloc(ctfdatasz);
1244 		if ((actual = decompress_ctf(buf, bufsz, ctfdata, ctfdatasz)) !=
1245 		    ctfdatasz) {
1246 			parseterminate("Corrupt CTF - short decompression "
1247 			    "(was %d, expecting %d)", actual, ctfdatasz);
1248 		}
1249 	} else {
1250 		ctfdata = buf;
1251 		ctfdatasz = bufsz;
1252 	}
1253 
1254 	td = ctf_parse(h, ctfdata, si, label);
1255 
1256 	if (h->cth_flags & CTF_F_COMPRESS)
1257 		free(ctfdata);
1258 
1259 	curfile = NULL;
1260 
1261 	return (td);
1262 }
1263