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