xref: /titanic_52/usr/src/tools/ctf/cvt/ctf.c (revision ba4e3c84e6b9390bbf7df80b5f1d11dec34cc525)
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 	(void) strtab_write(&buf->ctb_strtab, bcopy_data, &bufpos);
551 	*resszp = bufpos - outbuf;
552 	return (outbuf);
553 }
554 
555 /*
556  * Create the compression buffer, and fill it with the CTF and string
557  * table data.  We flush the compression state between the two so the
558  * dictionary used for the string tables won't be polluted with values
559  * that made sense for the CTF data.
560  */
561 static caddr_t
562 write_compressed_buffer(ctf_header_t *h, ctf_buf_t *buf, size_t *resszp)
563 {
564 	resbuf_t resbuf;
565 	resbuf.rb_size = RES_BUF_CHUNK_SIZE;
566 	resbuf.rb_base = xmalloc(resbuf.rb_size);
567 	bcopy(h, resbuf.rb_base, sizeof (ctf_header_t));
568 	resbuf.rb_ptr = resbuf.rb_base + sizeof (ctf_header_t);
569 
570 	compress_start(&resbuf);
571 	(void) compress_buffer(buf->ctb_base, buf->ctb_ptr - buf->ctb_base,
572 	    &resbuf);
573 	compress_flush(&resbuf, Z_FULL_FLUSH);
574 	(void) strtab_write(&buf->ctb_strtab, compress_buffer, &resbuf);
575 	compress_end(&resbuf);
576 
577 	*resszp = (resbuf.rb_ptr - resbuf.rb_base);
578 	return (resbuf.rb_base);
579 }
580 
581 caddr_t
582 ctf_gen(iiburst_t *iiburst, size_t *resszp, int do_compress)
583 {
584 	ctf_buf_t *buf = ctf_buf_new();
585 	ctf_header_t h;
586 	caddr_t outbuf;
587 
588 	int i;
589 
590 	/*
591 	 * Prepare the header, and create the CTF output buffers.  The data
592 	 * object section and function section are both lists of 2-byte
593 	 * integers; we pad these out to the next 4-byte boundary if needed.
594 	 */
595 	h.cth_magic = CTF_MAGIC;
596 	h.cth_version = CTF_VERSION;
597 	h.cth_flags = do_compress ? CTF_F_COMPRESS : 0;
598 	h.cth_parlabel = strtab_insert(&buf->ctb_strtab,
599 	    iiburst->iib_td->td_parlabel);
600 	h.cth_parname = strtab_insert(&buf->ctb_strtab,
601 	    iiburst->iib_td->td_parname);
602 
603 	h.cth_lbloff = 0;
604 	(void) list_iter(iiburst->iib_td->td_labels, (int (*)())write_label,
605 	    buf);
606 
607 	pad_buffer(buf, 2);
608 	h.cth_objtoff = ctf_buf_cur(buf);
609 	for (i = 0; i < iiburst->iib_nobjts; i++)
610 		write_objects(iiburst->iib_objts[i], buf);
611 
612 	pad_buffer(buf, 2);
613 	h.cth_funcoff = ctf_buf_cur(buf);
614 	for (i = 0; i < iiburst->iib_nfuncs; i++)
615 		write_functions(iiburst->iib_funcs[i], buf);
616 
617 	pad_buffer(buf, 4);
618 	h.cth_typeoff = ctf_buf_cur(buf);
619 	(void) list_iter(iiburst->iib_types, (int (*)())write_type, buf);
620 
621 	debug(2, "CTF wrote %d types\n", list_count(iiburst->iib_types));
622 
623 	h.cth_stroff = ctf_buf_cur(buf);
624 	h.cth_strlen = strtab_size(&buf->ctb_strtab);
625 
626 	/*
627 	 * We only do compression for ctfmerge, as ctfconvert is only
628 	 * supposed to be used on intermediary build objects. This is
629 	 * significantly faster.
630 	 */
631 	if (do_compress)
632 		outbuf = write_compressed_buffer(&h, buf, resszp);
633 	else
634 		outbuf = write_buffer(&h, buf, resszp);
635 
636 	ctf_buf_free(buf);
637 	return (outbuf);
638 }
639 
640 void
641 get_ctt_size(ctf_type_t *ctt, size_t *sizep, size_t *incrementp)
642 {
643 	if (ctt->ctt_size == CTF_LSIZE_SENT) {
644 		*sizep = (size_t)CTF_TYPE_LSIZE(ctt);
645 		*incrementp = sizeof (ctf_type_t);
646 	} else {
647 		*sizep = ctt->ctt_size;
648 		*incrementp = sizeof (ctf_stype_t);
649 	}
650 }
651 
652 static int
653 count_types(ctf_header_t *h, caddr_t data)
654 {
655 	caddr_t dptr = data + h->cth_typeoff;
656 	int count = 0;
657 
658 	dptr = data + h->cth_typeoff;
659 	while (dptr < data + h->cth_stroff) {
660 		/* LINTED - pointer alignment */
661 		ctf_type_t *ctt = (ctf_type_t *)dptr;
662 		size_t vlen = CTF_INFO_VLEN(ctt->ctt_info);
663 		size_t size, increment;
664 
665 		get_ctt_size(ctt, &size, &increment);
666 
667 		switch (CTF_INFO_KIND(ctt->ctt_info)) {
668 		case CTF_K_INTEGER:
669 		case CTF_K_FLOAT:
670 			dptr += 4;
671 			break;
672 		case CTF_K_POINTER:
673 		case CTF_K_FORWARD:
674 		case CTF_K_TYPEDEF:
675 		case CTF_K_VOLATILE:
676 		case CTF_K_CONST:
677 		case CTF_K_RESTRICT:
678 		case CTF_K_FUNCTION:
679 			dptr += sizeof (ushort_t) * (vlen + (vlen & 1));
680 			break;
681 		case CTF_K_ARRAY:
682 			dptr += sizeof (ctf_array_t);
683 			break;
684 		case CTF_K_STRUCT:
685 		case CTF_K_UNION:
686 			if (size < CTF_LSTRUCT_THRESH)
687 				dptr += sizeof (ctf_member_t) * vlen;
688 			else
689 				dptr += sizeof (ctf_lmember_t) * vlen;
690 			break;
691 		case CTF_K_ENUM:
692 			dptr += sizeof (ctf_enum_t) * vlen;
693 			break;
694 		case CTF_K_UNKNOWN:
695 			break;
696 		default:
697 			parseterminate("Unknown CTF type %d (#%d) at %#x",
698 			    CTF_INFO_KIND(ctt->ctt_info), count, dptr - data);
699 		}
700 
701 		dptr += increment;
702 		count++;
703 	}
704 
705 	debug(3, "CTF read %d types\n", count);
706 
707 	return (count);
708 }
709 
710 /*
711  * Resurrect the labels stored in the CTF data, returning the index associated
712  * with a label provided by the caller.  There are several cases, outlined
713  * below.  Note that, given two labels, the one associated with the lesser type
714  * index is considered to be older than the other.
715  *
716  *  1. matchlbl == NULL - return the index of the most recent label.
717  *  2. matchlbl == "BASE" - return the index of the oldest label.
718  *  3. matchlbl != NULL, but doesn't match any labels in the section - warn
719  *	the user, and proceed as if matchlbl == "BASE" (for safety).
720  *  4. matchlbl != NULL, and matches one of the labels in the section - return
721  *	the type index associated with the label.
722  */
723 static int
724 resurrect_labels(ctf_header_t *h, tdata_t *td, caddr_t ctfdata, char *matchlbl)
725 {
726 	caddr_t buf = ctfdata + h->cth_lbloff;
727 	caddr_t sbuf = ctfdata + h->cth_stroff;
728 	size_t bufsz = h->cth_objtoff - h->cth_lbloff;
729 	int lastidx = 0, baseidx = -1;
730 	char *baselabel;
731 	ctf_lblent_t *ctl;
732 
733 	/* LINTED - pointer alignment */
734 	for (ctl = (ctf_lblent_t *)buf; (caddr_t)ctl < buf + bufsz; ctl++) {
735 		char *label = sbuf + ctl->ctl_label;
736 
737 		lastidx = ctl->ctl_typeidx;
738 
739 		debug(3, "Resurrected label %s type idx %d\n", label, lastidx);
740 
741 		tdata_label_add(td, label, lastidx);
742 
743 		if (baseidx == -1) {
744 			baseidx = lastidx;
745 			baselabel = label;
746 			if (matchlbl != NULL && streq(matchlbl, "BASE"))
747 				return (lastidx);
748 		}
749 
750 		if (matchlbl != NULL && streq(label, matchlbl))
751 			return (lastidx);
752 	}
753 
754 	if (matchlbl != NULL) {
755 		/* User provided a label that didn't match */
756 		warning("%s: Cannot find label `%s' - using base (%s)\n",
757 		    curfile, matchlbl, (baselabel ? baselabel : "NONE"));
758 
759 		tdata_label_free(td);
760 		tdata_label_add(td, baselabel, baseidx);
761 
762 		return (baseidx);
763 	}
764 
765 	return (lastidx);
766 }
767 
768 static void
769 resurrect_objects(ctf_header_t *h, tdata_t *td, tdesc_t **tdarr, int tdsize,
770     caddr_t ctfdata, symit_data_t *si)
771 {
772 	caddr_t buf = ctfdata + h->cth_objtoff;
773 	size_t bufsz = h->cth_funcoff - h->cth_objtoff;
774 	caddr_t dptr;
775 
776 	symit_reset(si);
777 	for (dptr = buf; dptr < buf + bufsz; dptr += 2) {
778 		/* LINTED - pointer alignment */
779 		ushort_t id = *((ushort_t *)dptr);
780 		iidesc_t *ii;
781 		GElf_Sym *sym;
782 
783 		if (!(sym = symit_next(si, STT_OBJECT)) && id != 0) {
784 			parseterminate(
785 			    "Unexpected end of object symbols at %x of %x",
786 			    dptr - buf, bufsz);
787 		}
788 
789 		if (id == 0) {
790 			debug(3, "Skipping null object\n");
791 			continue;
792 		} else if (id >= tdsize) {
793 			parseterminate("Reference to invalid type %d", id);
794 		}
795 
796 		ii = iidesc_new(symit_name(si));
797 		ii->ii_dtype = tdarr[id];
798 		if (GELF_ST_BIND(sym->st_info) == STB_LOCAL) {
799 			ii->ii_type = II_SVAR;
800 			ii->ii_owner = xstrdup(symit_curfile(si));
801 		} else
802 			ii->ii_type = II_GVAR;
803 		hash_add(td->td_iihash, ii);
804 
805 		debug(3, "Resurrected %s object %s (%d) from %s\n",
806 		    (ii->ii_type == II_GVAR ? "global" : "static"),
807 		    ii->ii_name, id, (ii->ii_owner ? ii->ii_owner : "(none)"));
808 	}
809 }
810 
811 static void
812 resurrect_functions(ctf_header_t *h, tdata_t *td, tdesc_t **tdarr, int tdsize,
813     caddr_t ctfdata, symit_data_t *si)
814 {
815 	caddr_t buf = ctfdata + h->cth_funcoff;
816 	size_t bufsz = h->cth_typeoff - h->cth_funcoff;
817 	caddr_t dptr = buf;
818 	iidesc_t *ii;
819 	ushort_t info;
820 	ushort_t retid;
821 	GElf_Sym *sym;
822 	int i;
823 
824 	symit_reset(si);
825 	while (dptr < buf + bufsz) {
826 		/* LINTED - pointer alignment */
827 		info = *((ushort_t *)dptr);
828 		dptr += 2;
829 
830 		if (!(sym = symit_next(si, STT_FUNC)) && info != 0)
831 			parseterminate("Unexpected end of function symbols");
832 
833 		if (info == 0) {
834 			debug(3, "Skipping null function (%s)\n",
835 			    symit_name(si));
836 			continue;
837 		}
838 
839 		/* LINTED - pointer alignment */
840 		retid = *((ushort_t *)dptr);
841 		dptr += 2;
842 
843 		if (retid >= tdsize)
844 			parseterminate("Reference to invalid type %d", retid);
845 
846 		ii = iidesc_new(symit_name(si));
847 		ii->ii_dtype = tdarr[retid];
848 		if (GELF_ST_BIND(sym->st_info) == STB_LOCAL) {
849 			ii->ii_type = II_SFUN;
850 			ii->ii_owner = xstrdup(symit_curfile(si));
851 		} else
852 			ii->ii_type = II_GFUN;
853 		ii->ii_nargs = CTF_INFO_VLEN(info);
854 		if (ii->ii_nargs)
855 			ii->ii_args =
856 			    xmalloc(sizeof (tdesc_t *) * ii->ii_nargs);
857 
858 		for (i = 0; i < ii->ii_nargs; i++, dptr += 2) {
859 			/* LINTED - pointer alignment */
860 			ushort_t id = *((ushort_t *)dptr);
861 			if (id >= tdsize)
862 				parseterminate("Reference to invalid type %d",
863 				    id);
864 			ii->ii_args[i] = tdarr[id];
865 		}
866 
867 		if (ii->ii_nargs && ii->ii_args[ii->ii_nargs - 1] == NULL) {
868 			ii->ii_nargs--;
869 			ii->ii_vargs = 1;
870 		}
871 
872 		hash_add(td->td_iihash, ii);
873 
874 		debug(3, "Resurrected %s function %s (%d, %d args)\n",
875 		    (ii->ii_type == II_GFUN ? "global" : "static"),
876 		    ii->ii_name, retid, ii->ii_nargs);
877 	}
878 }
879 
880 static void
881 resurrect_types(ctf_header_t *h, tdata_t *td, tdesc_t **tdarr, int tdsize,
882     caddr_t ctfdata, int maxid)
883 {
884 	caddr_t buf = ctfdata + h->cth_typeoff;
885 	size_t bufsz = h->cth_stroff - h->cth_typeoff;
886 	caddr_t sbuf = ctfdata + h->cth_stroff;
887 	caddr_t dptr = buf;
888 	tdesc_t *tdp;
889 	uint_t data;
890 	uint_t encoding;
891 	size_t size, increment;
892 	int tcnt;
893 	int iicnt = 0;
894 	tid_t tid, argid;
895 	int kind, vlen;
896 	int i;
897 
898 	elist_t **epp;
899 	mlist_t **mpp;
900 	intr_t *ip;
901 
902 	ctf_type_t *ctt;
903 	ctf_array_t *cta;
904 	ctf_enum_t *cte;
905 
906 	/*
907 	 * A maxid of zero indicates a request to resurrect all types, so reset
908 	 * maxid to the maximum type id.
909 	 */
910 	if (maxid == 0)
911 		maxid = CTF_MAX_TYPE;
912 
913 	for (dptr = buf, tcnt = 0, tid = 1; dptr < buf + bufsz; tcnt++, tid++) {
914 		if (tid > maxid)
915 			break;
916 
917 		if (tid >= tdsize)
918 			parseterminate("Reference to invalid type %d", tid);
919 
920 		/* LINTED - pointer alignment */
921 		ctt = (ctf_type_t *)dptr;
922 
923 		get_ctt_size(ctt, &size, &increment);
924 		dptr += increment;
925 
926 		tdp = tdarr[tid];
927 
928 		if (CTF_NAME_STID(ctt->ctt_name) != CTF_STRTAB_0)
929 			parseterminate(
930 				"Unable to cope with non-zero strtab id");
931 		if (CTF_NAME_OFFSET(ctt->ctt_name) != 0) {
932 			tdp->t_name =
933 			    xstrdup(sbuf + CTF_NAME_OFFSET(ctt->ctt_name));
934 		} else
935 			tdp->t_name = NULL;
936 
937 		kind = CTF_INFO_KIND(ctt->ctt_info);
938 		vlen = CTF_INFO_VLEN(ctt->ctt_info);
939 
940 		switch (kind) {
941 		case CTF_K_INTEGER:
942 			tdp->t_type = INTRINSIC;
943 			tdp->t_size = size;
944 
945 			/* LINTED - pointer alignment */
946 			data = *((uint_t *)dptr);
947 			dptr += sizeof (uint_t);
948 			encoding = CTF_INT_ENCODING(data);
949 
950 			ip = xmalloc(sizeof (intr_t));
951 			ip->intr_type = INTR_INT;
952 			ip->intr_signed = (encoding & CTF_INT_SIGNED) ? 1 : 0;
953 
954 			if (encoding & CTF_INT_CHAR)
955 				ip->intr_iformat = 'c';
956 			else if (encoding & CTF_INT_BOOL)
957 				ip->intr_iformat = 'b';
958 			else if (encoding & CTF_INT_VARARGS)
959 				ip->intr_iformat = 'v';
960 			else
961 				ip->intr_iformat = '\0';
962 
963 			ip->intr_offset = CTF_INT_OFFSET(data);
964 			ip->intr_nbits = CTF_INT_BITS(data);
965 			tdp->t_intr = ip;
966 			break;
967 
968 		case CTF_K_FLOAT:
969 			tdp->t_type = INTRINSIC;
970 			tdp->t_size = size;
971 
972 			/* LINTED - pointer alignment */
973 			data = *((uint_t *)dptr);
974 			dptr += sizeof (uint_t);
975 
976 			ip = xcalloc(sizeof (intr_t));
977 			ip->intr_type = INTR_REAL;
978 			ip->intr_fformat = CTF_FP_ENCODING(data);
979 			ip->intr_offset = CTF_FP_OFFSET(data);
980 			ip->intr_nbits = CTF_FP_BITS(data);
981 			tdp->t_intr = ip;
982 			break;
983 
984 		case CTF_K_POINTER:
985 			tdp->t_type = POINTER;
986 			tdp->t_tdesc = tdarr[ctt->ctt_type];
987 			break;
988 
989 		case CTF_K_ARRAY:
990 			tdp->t_type = ARRAY;
991 			tdp->t_size = size;
992 
993 			/* LINTED - pointer alignment */
994 			cta = (ctf_array_t *)dptr;
995 			dptr += sizeof (ctf_array_t);
996 
997 			tdp->t_ardef = xmalloc(sizeof (ardef_t));
998 			tdp->t_ardef->ad_contents = tdarr[cta->cta_contents];
999 			tdp->t_ardef->ad_idxtype = tdarr[cta->cta_index];
1000 			tdp->t_ardef->ad_nelems = cta->cta_nelems;
1001 			break;
1002 
1003 		case CTF_K_STRUCT:
1004 		case CTF_K_UNION:
1005 			tdp->t_type = (kind == CTF_K_STRUCT ? STRUCT : UNION);
1006 			tdp->t_size = size;
1007 
1008 			if (size < CTF_LSTRUCT_THRESH) {
1009 				for (i = 0, mpp = &tdp->t_members; i < vlen;
1010 				    i++, mpp = &((*mpp)->ml_next)) {
1011 					/* LINTED - pointer alignment */
1012 					ctf_member_t *ctm = (ctf_member_t *)
1013 					    dptr;
1014 					dptr += sizeof (ctf_member_t);
1015 
1016 					*mpp = xmalloc(sizeof (mlist_t));
1017 					(*mpp)->ml_name = xstrdup(sbuf +
1018 					    ctm->ctm_name);
1019 					(*mpp)->ml_type = tdarr[ctm->ctm_type];
1020 					(*mpp)->ml_offset = ctm->ctm_offset;
1021 					(*mpp)->ml_size = 0;
1022 				}
1023 			} else {
1024 				for (i = 0, mpp = &tdp->t_members; i < vlen;
1025 				    i++, mpp = &((*mpp)->ml_next)) {
1026 					/* LINTED - pointer alignment */
1027 					ctf_lmember_t *ctlm = (ctf_lmember_t *)
1028 					    dptr;
1029 					dptr += sizeof (ctf_lmember_t);
1030 
1031 					*mpp = xmalloc(sizeof (mlist_t));
1032 					(*mpp)->ml_name = xstrdup(sbuf +
1033 					    ctlm->ctlm_name);
1034 					(*mpp)->ml_type =
1035 					    tdarr[ctlm->ctlm_type];
1036 					(*mpp)->ml_offset =
1037 					    (int)CTF_LMEM_OFFSET(ctlm);
1038 					(*mpp)->ml_size = 0;
1039 				}
1040 			}
1041 
1042 			*mpp = NULL;
1043 			break;
1044 
1045 		case CTF_K_ENUM:
1046 			tdp->t_type = ENUM;
1047 			tdp->t_size = size;
1048 
1049 			for (i = 0, epp = &tdp->t_emem; i < vlen;
1050 			    i++, epp = &((*epp)->el_next)) {
1051 				/* LINTED - pointer alignment */
1052 				cte = (ctf_enum_t *)dptr;
1053 				dptr += sizeof (ctf_enum_t);
1054 
1055 				*epp = xmalloc(sizeof (elist_t));
1056 				(*epp)->el_name = xstrdup(sbuf + cte->cte_name);
1057 				(*epp)->el_number = cte->cte_value;
1058 			}
1059 			*epp = NULL;
1060 			break;
1061 
1062 		case CTF_K_FORWARD:
1063 			tdp->t_type = FORWARD;
1064 			list_add(&td->td_fwdlist, tdp);
1065 			break;
1066 
1067 		case CTF_K_TYPEDEF:
1068 			tdp->t_type = TYPEDEF;
1069 			tdp->t_tdesc = tdarr[ctt->ctt_type];
1070 			break;
1071 
1072 		case CTF_K_VOLATILE:
1073 			tdp->t_type = VOLATILE;
1074 			tdp->t_tdesc = tdarr[ctt->ctt_type];
1075 			break;
1076 
1077 		case CTF_K_CONST:
1078 			tdp->t_type = CONST;
1079 			tdp->t_tdesc = tdarr[ctt->ctt_type];
1080 			break;
1081 
1082 		case CTF_K_FUNCTION:
1083 			tdp->t_type = FUNCTION;
1084 			tdp->t_fndef = xcalloc(sizeof (fndef_t));
1085 			tdp->t_fndef->fn_ret = tdarr[ctt->ctt_type];
1086 
1087 			/* LINTED - pointer alignment */
1088 			if (vlen > 0 && *(ushort_t *)(dptr +
1089 			    (sizeof (ushort_t) * (vlen - 1))) == 0)
1090 				tdp->t_fndef->fn_vargs = 1;
1091 
1092 			tdp->t_fndef->fn_nargs = vlen - tdp->t_fndef->fn_vargs;
1093 			tdp->t_fndef->fn_args = xcalloc(sizeof (tdesc_t) *
1094 			    vlen - tdp->t_fndef->fn_vargs);
1095 
1096 			for (i = 0; i < vlen; i++) {
1097 				/* LINTED - pointer alignment */
1098 				argid = *(ushort_t *)dptr;
1099 				dptr += sizeof (ushort_t);
1100 
1101 				if (argid != 0)
1102 					tdp->t_fndef->fn_args[i] = tdarr[argid];
1103 			}
1104 
1105 			if (vlen & 1)
1106 				dptr += sizeof (ushort_t);
1107 			break;
1108 
1109 		case CTF_K_RESTRICT:
1110 			tdp->t_type = RESTRICT;
1111 			tdp->t_tdesc = tdarr[ctt->ctt_type];
1112 			break;
1113 
1114 		case CTF_K_UNKNOWN:
1115 			break;
1116 
1117 		default:
1118 			warning("Can't parse unknown CTF type %d\n", kind);
1119 		}
1120 
1121 		if (CTF_INFO_ISROOT(ctt->ctt_info)) {
1122 			iidesc_t *ii = iidesc_new(tdp->t_name);
1123 			if (tdp->t_type == STRUCT || tdp->t_type == UNION ||
1124 			    tdp->t_type == ENUM)
1125 				ii->ii_type = II_SOU;
1126 			else
1127 				ii->ii_type = II_TYPE;
1128 			ii->ii_dtype = tdp;
1129 			hash_add(td->td_iihash, ii);
1130 
1131 			iicnt++;
1132 		}
1133 
1134 		debug(3, "Resurrected %d %stype %s (%d)\n", tdp->t_type,
1135 		    (CTF_INFO_ISROOT(ctt->ctt_info) ? "root " : ""),
1136 		    tdesc_name(tdp), tdp->t_id);
1137 	}
1138 
1139 	debug(3, "Resurrected %d types (%d were roots)\n", tcnt, iicnt);
1140 }
1141 
1142 /*
1143  * For lack of other inspiration, we're going to take the boring route.  We
1144  * count the number of types.  This lets us malloc that many tdesc structs
1145  * before we start filling them in.  This has the advantage of allowing us to
1146  * avoid a merge-esque remap step.
1147  */
1148 static tdata_t *
1149 ctf_parse(ctf_header_t *h, caddr_t buf, symit_data_t *si, char *label)
1150 {
1151 	tdata_t *td = tdata_new();
1152 	tdesc_t **tdarr;
1153 	int ntypes = count_types(h, buf);
1154 	int idx, i;
1155 
1156 	/* shudder */
1157 	tdarr = xcalloc(sizeof (tdesc_t *) * (ntypes + 1));
1158 	tdarr[0] = NULL;
1159 	for (i = 1; i <= ntypes; i++) {
1160 		tdarr[i] = xcalloc(sizeof (tdesc_t));
1161 		tdarr[i]->t_id = i;
1162 	}
1163 
1164 	td->td_parlabel = xstrdup(buf + h->cth_stroff + h->cth_parlabel);
1165 
1166 	/* we have the technology - we can rebuild them */
1167 	idx = resurrect_labels(h, td, buf, label);
1168 
1169 	resurrect_objects(h, td, tdarr, ntypes + 1, buf, si);
1170 	resurrect_functions(h, td, tdarr, ntypes + 1, buf, si);
1171 	resurrect_types(h, td, tdarr, ntypes + 1, buf, idx);
1172 
1173 	free(tdarr);
1174 
1175 	td->td_nextid = ntypes + 1;
1176 
1177 	return (td);
1178 }
1179 
1180 static size_t
1181 decompress_ctf(caddr_t cbuf, size_t cbufsz, caddr_t dbuf, size_t dbufsz)
1182 {
1183 	z_stream zstr;
1184 	int rc;
1185 
1186 	zstr.zalloc = (alloc_func)0;
1187 	zstr.zfree = (free_func)0;
1188 	zstr.opaque = (voidpf)0;
1189 
1190 	zstr.next_in = (Bytef *)cbuf;
1191 	zstr.avail_in = cbufsz;
1192 	zstr.next_out = (Bytef *)dbuf;
1193 	zstr.avail_out = dbufsz;
1194 
1195 	if ((rc = inflateInit(&zstr)) != Z_OK ||
1196 	    (rc = inflate(&zstr, Z_NO_FLUSH)) != Z_STREAM_END ||
1197 	    (rc = inflateEnd(&zstr)) != Z_OK) {
1198 		warning("CTF decompress zlib error %s\n", zError(rc));
1199 		return (NULL);
1200 	}
1201 
1202 	debug(3, "reflated %lu bytes to %lu, pointer at %d\n",
1203 	    zstr.total_in, zstr.total_out, (caddr_t)zstr.next_in - cbuf);
1204 
1205 	return (zstr.total_out);
1206 }
1207 
1208 /*
1209  * Reconstruct the type tree from a given buffer of CTF data.  Only the types
1210  * up to the type associated with the provided label, inclusive, will be
1211  * reconstructed.  If a NULL label is provided, all types will be reconstructed.
1212  *
1213  * This function won't work on files that have been uniquified.
1214  */
1215 tdata_t *
1216 ctf_load(char *file, caddr_t buf, size_t bufsz, symit_data_t *si, char *label)
1217 {
1218 	ctf_header_t *h;
1219 	caddr_t ctfdata;
1220 	size_t ctfdatasz;
1221 	tdata_t *td;
1222 
1223 	curfile = file;
1224 
1225 	if (bufsz < sizeof (ctf_header_t))
1226 		parseterminate("Corrupt CTF - short header");
1227 
1228 	/* LINTED - pointer alignment */
1229 	h = (ctf_header_t *)buf;
1230 	buf += sizeof (ctf_header_t);
1231 	bufsz -= sizeof (ctf_header_t);
1232 
1233 	if (h->cth_magic != CTF_MAGIC)
1234 		parseterminate("Corrupt CTF - bad magic 0x%x", h->cth_magic);
1235 
1236 	if (h->cth_version != CTF_VERSION)
1237 		parseterminate("Unknown CTF version %d", h->cth_version);
1238 
1239 	ctfdatasz = h->cth_stroff + h->cth_strlen;
1240 	if (h->cth_flags & CTF_F_COMPRESS) {
1241 		size_t actual;
1242 
1243 		ctfdata = xmalloc(ctfdatasz);
1244 		if ((actual = decompress_ctf(buf, bufsz, ctfdata, ctfdatasz)) !=
1245 		    ctfdatasz) {
1246 			parseterminate("Corrupt CTF - short decompression "
1247 			    "(was %d, expecting %d)", actual, ctfdatasz);
1248 		}
1249 	} else {
1250 		ctfdata = buf;
1251 		ctfdatasz = bufsz;
1252 	}
1253 
1254 	td = ctf_parse(h, ctfdata, si, label);
1255 
1256 	if (h->cth_flags & CTF_F_COMPRESS)
1257 		free(ctfdata);
1258 
1259 	curfile = NULL;
1260 
1261 	return (td);
1262 }
1263