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