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