1178481Sjb/*
2178481Sjb * CDDL HEADER START
3178481Sjb *
4178481Sjb * The contents of this file are subject to the terms of the
5178481Sjb * Common Development and Distribution License (the "License").
6178481Sjb * You may not use this file except in compliance with the License.
7178481Sjb *
8178481Sjb * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9178481Sjb * or http://www.opensolaris.org/os/licensing.
10178481Sjb * See the License for the specific language governing permissions
11178481Sjb * and limitations under the License.
12178481Sjb *
13178481Sjb * When distributing Covered Code, include this CDDL HEADER in each
14178481Sjb * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15178481Sjb * If applicable, add the following below this CDDL HEADER, with the
16178481Sjb * fields enclosed by brackets "[]" replaced with your own identifying
17178481Sjb * information: Portions Copyright [yyyy] [name of copyright owner]
18178481Sjb *
19178481Sjb * CDDL HEADER END
20178481Sjb */
21178481Sjb/*
22210767Srpaulo * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23178481Sjb * Use is subject to license terms.
24178481Sjb */
25178481Sjb
26178481Sjb/*
27178481Sjb * Create and parse buffers containing CTF data.
28178481Sjb */
29178481Sjb
30178481Sjb#include <sys/types.h>
31178481Sjb#include <stdio.h>
32178481Sjb#include <stdlib.h>
33178481Sjb#include <strings.h>
34178481Sjb#include <ctype.h>
35178481Sjb#include <zlib.h>
36178481Sjb#include <elf.h>
37178481Sjb
38178481Sjb#include "ctf_headers.h"
39178481Sjb#include "ctftools.h"
40178481Sjb#include "strtab.h"
41178481Sjb#include "memory.h"
42178481Sjb
43178481Sjb/*
44178481Sjb * Name of the file currently being read, used to print error messages.  We
45178481Sjb * assume that only one file will be read at a time, and thus make no attempt
46178481Sjb * to allow curfile to be used simultaneously by multiple threads.
47178481Sjb *
48178481Sjb * The value is only valid during a call to ctf_load.
49178481Sjb */
50178481Sjbchar *curfile;
51178481Sjb
52178481Sjb#define	CTF_BUF_CHUNK_SIZE	(64 * 1024)
53178481Sjb#define	RES_BUF_CHUNK_SIZE	(64 * 1024)
54178481Sjb
55178481Sjbstruct ctf_buf {
56178481Sjb	strtab_t ctb_strtab;	/* string table */
57178481Sjb	caddr_t ctb_base;	/* pointer to base of buffer */
58178481Sjb	caddr_t ctb_end;	/* pointer to end of buffer */
59178481Sjb	caddr_t ctb_ptr;	/* pointer to empty buffer space */
60178481Sjb	size_t ctb_size;	/* size of buffer */
61178481Sjb	int nptent;		/* number of processed types */
62178481Sjb	int ntholes;		/* number of type holes */
63178481Sjb};
64178481Sjb
65178481Sjb/*PRINTFLIKE1*/
66178481Sjbstatic void
67178546Sjbparseterminate(const char *fmt, ...)
68178481Sjb{
69178481Sjb	static char msgbuf[1024]; /* sigh */
70178481Sjb	va_list ap;
71178481Sjb
72178481Sjb	va_start(ap, fmt);
73178481Sjb	vsnprintf(msgbuf, sizeof (msgbuf), fmt, ap);
74178481Sjb	va_end(ap);
75178481Sjb
76178481Sjb	terminate("%s: %s\n", curfile, msgbuf);
77178481Sjb}
78178481Sjb
79178546Sjbstatic void
80178481Sjbctf_buf_grow(ctf_buf_t *b)
81178481Sjb{
82178481Sjb	off_t ptroff = b->ctb_ptr - b->ctb_base;
83178481Sjb
84178481Sjb	b->ctb_size += CTF_BUF_CHUNK_SIZE;
85178481Sjb	b->ctb_base = xrealloc(b->ctb_base, b->ctb_size);
86178481Sjb	b->ctb_end = b->ctb_base + b->ctb_size;
87178481Sjb	b->ctb_ptr = b->ctb_base + ptroff;
88178481Sjb}
89178481Sjb
90178546Sjbstatic ctf_buf_t *
91178481Sjbctf_buf_new(void)
92178481Sjb{
93178481Sjb	ctf_buf_t *b = xcalloc(sizeof (ctf_buf_t));
94178481Sjb
95178481Sjb	strtab_create(&b->ctb_strtab);
96178481Sjb	ctf_buf_grow(b);
97178481Sjb
98178481Sjb	return (b);
99178481Sjb}
100178481Sjb
101178546Sjbstatic void
102178481Sjbctf_buf_free(ctf_buf_t *b)
103178481Sjb{
104178481Sjb	strtab_destroy(&b->ctb_strtab);
105178481Sjb	free(b->ctb_base);
106178481Sjb	free(b);
107178481Sjb}
108178481Sjb
109178546Sjbstatic uint_t
110178481Sjbctf_buf_cur(ctf_buf_t *b)
111178481Sjb{
112178481Sjb	return (b->ctb_ptr - b->ctb_base);
113178481Sjb}
114178481Sjb
115178546Sjbstatic void
116178546Sjbctf_buf_write(ctf_buf_t *b, void const *p, size_t n)
117178481Sjb{
118178481Sjb	size_t len;
119178481Sjb
120178481Sjb	while (n != 0) {
121178481Sjb		if (b->ctb_ptr == b->ctb_end)
122178481Sjb			ctf_buf_grow(b);
123178481Sjb
124178481Sjb		len = MIN((size_t)(b->ctb_end - b->ctb_ptr), n);
125178481Sjb		bcopy(p, b->ctb_ptr, len);
126178481Sjb		b->ctb_ptr += len;
127178481Sjb
128178546Sjb		p = (char const *)p + len;
129178481Sjb		n -= len;
130178481Sjb	}
131178481Sjb}
132178481Sjb
133178481Sjbstatic int
134178546Sjbwrite_label(void *arg1, void *arg2)
135178481Sjb{
136178546Sjb	labelent_t *le = arg1;
137178546Sjb	ctf_buf_t *b = arg2;
138178481Sjb	ctf_lblent_t ctl;
139178481Sjb
140178481Sjb	ctl.ctl_label = strtab_insert(&b->ctb_strtab, le->le_name);
141178481Sjb	ctl.ctl_typeidx = le->le_idx;
142178481Sjb
143178481Sjb	ctf_buf_write(b, &ctl, sizeof (ctl));
144178481Sjb
145178481Sjb	return (1);
146178481Sjb}
147178481Sjb
148178481Sjbstatic void
149178481Sjbwrite_objects(iidesc_t *idp, ctf_buf_t *b)
150178481Sjb{
151178481Sjb	ushort_t id = (idp ? idp->ii_dtype->t_id : 0);
152178481Sjb
153178481Sjb	ctf_buf_write(b, &id, sizeof (id));
154178481Sjb
155178481Sjb	debug(3, "Wrote object %s (%d)\n", (idp ? idp->ii_name : "(null)"), id);
156178481Sjb}
157178481Sjb
158178481Sjbstatic void
159178481Sjbwrite_functions(iidesc_t *idp, ctf_buf_t *b)
160178481Sjb{
161178481Sjb	ushort_t fdata[2];
162178481Sjb	ushort_t id;
163178481Sjb	int nargs;
164178481Sjb	int i;
165178481Sjb
166178481Sjb	if (!idp) {
167178481Sjb		fdata[0] = 0;
168178481Sjb		ctf_buf_write(b, &fdata[0], sizeof (fdata[0]));
169178481Sjb
170178481Sjb		debug(3, "Wrote function (null)\n");
171178481Sjb		return;
172178481Sjb	}
173178481Sjb
174178481Sjb	nargs = idp->ii_nargs + (idp->ii_vargs != 0);
175210767Srpaulo
176210767Srpaulo	if (nargs > CTF_MAX_VLEN) {
177210767Srpaulo		terminate("function %s has too many args: %d > %d\n",
178210767Srpaulo		    idp->ii_name, nargs, CTF_MAX_VLEN);
179210767Srpaulo	}
180210767Srpaulo
181178481Sjb	fdata[0] = CTF_TYPE_INFO(CTF_K_FUNCTION, 1, nargs);
182178481Sjb	fdata[1] = idp->ii_dtype->t_id;
183178481Sjb	ctf_buf_write(b, fdata, sizeof (fdata));
184178481Sjb
185178481Sjb	for (i = 0; i < idp->ii_nargs; i++) {
186178481Sjb		id = idp->ii_args[i]->t_id;
187178481Sjb		ctf_buf_write(b, &id, sizeof (id));
188178481Sjb	}
189178481Sjb
190178481Sjb	if (idp->ii_vargs) {
191178481Sjb		id = 0;
192178481Sjb		ctf_buf_write(b, &id, sizeof (id));
193178481Sjb	}
194178481Sjb
195178481Sjb	debug(3, "Wrote function %s (%d args)\n", idp->ii_name, nargs);
196178481Sjb}
197178481Sjb
198178481Sjb/*
199178481Sjb * Depending on the size of the type being described, either a ctf_stype_t (for
200178481Sjb * types with size < CTF_LSTRUCT_THRESH) or a ctf_type_t (all others) will be
201178481Sjb * written.  We isolate the determination here so the rest of the writer code
202178481Sjb * doesn't need to care.
203178481Sjb */
204178481Sjbstatic void
205178481Sjbwrite_sized_type_rec(ctf_buf_t *b, ctf_type_t *ctt, size_t size)
206178481Sjb{
207178481Sjb	if (size > CTF_MAX_SIZE) {
208178481Sjb		ctt->ctt_size = CTF_LSIZE_SENT;
209178481Sjb		ctt->ctt_lsizehi = CTF_SIZE_TO_LSIZE_HI(size);
210178481Sjb		ctt->ctt_lsizelo = CTF_SIZE_TO_LSIZE_LO(size);
211178481Sjb		ctf_buf_write(b, ctt, sizeof (*ctt));
212178481Sjb	} else {
213178481Sjb		ctf_stype_t *cts = (ctf_stype_t *)ctt;
214178481Sjb
215178481Sjb		cts->ctt_size = (ushort_t)size;
216178481Sjb		ctf_buf_write(b, cts, sizeof (*cts));
217178481Sjb	}
218178481Sjb}
219178481Sjb
220178481Sjbstatic void
221178481Sjbwrite_unsized_type_rec(ctf_buf_t *b, ctf_type_t *ctt)
222178481Sjb{
223178481Sjb	ctf_stype_t *cts = (ctf_stype_t *)ctt;
224178481Sjb
225178481Sjb	ctf_buf_write(b, cts, sizeof (*cts));
226178481Sjb}
227178481Sjb
228178481Sjbstatic int
229178546Sjbwrite_type(void *arg1, void *arg2)
230178481Sjb{
231178546Sjb	tdesc_t *tp = arg1;
232178546Sjb	ctf_buf_t *b = arg2;
233178481Sjb	elist_t *ep;
234178481Sjb	mlist_t *mp;
235178481Sjb	intr_t *ip;
236178481Sjb
237178481Sjb	size_t offset;
238178481Sjb	uint_t encoding;
239178481Sjb	uint_t data;
240178481Sjb	int isroot = tp->t_flags & TDESC_F_ISROOT;
241178481Sjb	int i;
242178481Sjb
243178481Sjb	ctf_type_t ctt;
244178481Sjb	ctf_array_t cta;
245178481Sjb	ctf_member_t ctm;
246178481Sjb	ctf_lmember_t ctlm;
247178481Sjb	ctf_enum_t cte;
248178481Sjb	ushort_t id;
249178481Sjb
250178481Sjb	ctlm.ctlm_pad = 0;
251178481Sjb
252178481Sjb	/*
253178481Sjb	 * There shouldn't be any holes in the type list (where a hole is
254178481Sjb	 * defined as two consecutive tdescs without consecutive ids), but
255178481Sjb	 * check for them just in case.  If we do find holes, we need to make
256178481Sjb	 * fake entries to fill the holes, or we won't be able to reconstruct
257178481Sjb	 * the tree from the written data.
258178481Sjb	 */
259178481Sjb	if (++b->nptent < CTF_TYPE_TO_INDEX(tp->t_id)) {
260178481Sjb		debug(2, "genctf: type hole from %d < x < %d\n",
261178481Sjb		    b->nptent - 1, CTF_TYPE_TO_INDEX(tp->t_id));
262178481Sjb
263178481Sjb		ctt.ctt_name = CTF_TYPE_NAME(CTF_STRTAB_0, 0);
264178481Sjb		ctt.ctt_info = CTF_TYPE_INFO(0, 0, 0);
265178481Sjb		while (b->nptent < CTF_TYPE_TO_INDEX(tp->t_id)) {
266178481Sjb			write_sized_type_rec(b, &ctt, 0);
267178481Sjb			b->nptent++;
268178481Sjb		}
269178481Sjb	}
270178481Sjb
271178481Sjb	offset = strtab_insert(&b->ctb_strtab, tp->t_name);
272178481Sjb	ctt.ctt_name = CTF_TYPE_NAME(CTF_STRTAB_0, offset);
273178481Sjb
274178481Sjb	switch (tp->t_type) {
275178481Sjb	case INTRINSIC:
276178481Sjb		ip = tp->t_intr;
277178481Sjb		if (ip->intr_type == INTR_INT)
278178481Sjb			ctt.ctt_info = CTF_TYPE_INFO(CTF_K_INTEGER,
279178481Sjb			    isroot, 1);
280178481Sjb		else
281178481Sjb			ctt.ctt_info = CTF_TYPE_INFO(CTF_K_FLOAT, isroot, 1);
282178481Sjb		write_sized_type_rec(b, &ctt, tp->t_size);
283178481Sjb
284178481Sjb		encoding = 0;
285178481Sjb
286178481Sjb		if (ip->intr_type == INTR_INT) {
287178481Sjb			if (ip->intr_signed)
288178481Sjb				encoding |= CTF_INT_SIGNED;
289178481Sjb			if (ip->intr_iformat == 'c')
290178481Sjb				encoding |= CTF_INT_CHAR;
291178481Sjb			else if (ip->intr_iformat == 'b')
292178481Sjb				encoding |= CTF_INT_BOOL;
293178481Sjb			else if (ip->intr_iformat == 'v')
294178481Sjb				encoding |= CTF_INT_VARARGS;
295178481Sjb		} else
296178481Sjb			encoding = ip->intr_fformat;
297178481Sjb
298178481Sjb		data = CTF_INT_DATA(encoding, ip->intr_offset, ip->intr_nbits);
299178481Sjb		ctf_buf_write(b, &data, sizeof (data));
300178481Sjb		break;
301178481Sjb
302178481Sjb	case POINTER:
303178481Sjb		ctt.ctt_info = CTF_TYPE_INFO(CTF_K_POINTER, isroot, 0);
304178481Sjb		ctt.ctt_type = tp->t_tdesc->t_id;
305178481Sjb		write_unsized_type_rec(b, &ctt);
306178481Sjb		break;
307178481Sjb
308178481Sjb	case ARRAY:
309178481Sjb		ctt.ctt_info = CTF_TYPE_INFO(CTF_K_ARRAY, isroot, 1);
310178481Sjb		write_sized_type_rec(b, &ctt, tp->t_size);
311178481Sjb
312178481Sjb		cta.cta_contents = tp->t_ardef->ad_contents->t_id;
313178481Sjb		cta.cta_index = tp->t_ardef->ad_idxtype->t_id;
314178481Sjb		cta.cta_nelems = tp->t_ardef->ad_nelems;
315178481Sjb		ctf_buf_write(b, &cta, sizeof (cta));
316178481Sjb		break;
317178481Sjb
318178481Sjb	case STRUCT:
319178481Sjb	case UNION:
320178481Sjb		for (i = 0, mp = tp->t_members; mp != NULL; mp = mp->ml_next)
321178481Sjb			i++; /* count up struct or union members */
322178481Sjb
323210767Srpaulo		if (i > CTF_MAX_VLEN) {
324210767Srpaulo			terminate("sou %s has too many members: %d > %d\n",
325210767Srpaulo			    tdesc_name(tp), i, CTF_MAX_VLEN);
326210767Srpaulo		}
327210767Srpaulo
328178481Sjb		if (tp->t_type == STRUCT)
329178481Sjb			ctt.ctt_info = CTF_TYPE_INFO(CTF_K_STRUCT, isroot, i);
330178481Sjb		else
331178481Sjb			ctt.ctt_info = CTF_TYPE_INFO(CTF_K_UNION, isroot, i);
332178481Sjb
333178481Sjb		write_sized_type_rec(b, &ctt, tp->t_size);
334178481Sjb
335178481Sjb		if (tp->t_size < CTF_LSTRUCT_THRESH) {
336178481Sjb			for (mp = tp->t_members; mp != NULL; mp = mp->ml_next) {
337178481Sjb				offset = strtab_insert(&b->ctb_strtab,
338178481Sjb				    mp->ml_name);
339178481Sjb
340178481Sjb				ctm.ctm_name = CTF_TYPE_NAME(CTF_STRTAB_0,
341178481Sjb				    offset);
342178481Sjb				ctm.ctm_type = mp->ml_type->t_id;
343178481Sjb				ctm.ctm_offset = mp->ml_offset;
344178481Sjb				ctf_buf_write(b, &ctm, sizeof (ctm));
345178481Sjb			}
346178481Sjb		} else {
347178481Sjb			for (mp = tp->t_members; mp != NULL; mp = mp->ml_next) {
348178481Sjb				offset = strtab_insert(&b->ctb_strtab,
349178481Sjb				    mp->ml_name);
350178481Sjb
351178481Sjb				ctlm.ctlm_name = CTF_TYPE_NAME(CTF_STRTAB_0,
352178481Sjb				    offset);
353178481Sjb				ctlm.ctlm_type = mp->ml_type->t_id;
354178481Sjb				ctlm.ctlm_offsethi =
355178481Sjb				    CTF_OFFSET_TO_LMEMHI(mp->ml_offset);
356178481Sjb				ctlm.ctlm_offsetlo =
357178481Sjb				    CTF_OFFSET_TO_LMEMLO(mp->ml_offset);
358178481Sjb				ctf_buf_write(b, &ctlm, sizeof (ctlm));
359178481Sjb			}
360178481Sjb		}
361178481Sjb		break;
362178481Sjb
363178481Sjb	case ENUM:
364178481Sjb		for (i = 0, ep = tp->t_emem; ep != NULL; ep = ep->el_next)
365178481Sjb			i++; /* count up enum members */
366178481Sjb
367207578Skan		if (i > CTF_MAX_VLEN) {
368207578Skan			warning("enum %s has too many values: %d > %d\n",
369207578Skan			    tdesc_name(tp), i, CTF_MAX_VLEN);
370207578Skan			i = CTF_MAX_VLEN;
371207578Skan		}
372207578Skan
373178481Sjb		ctt.ctt_info = CTF_TYPE_INFO(CTF_K_ENUM, isroot, i);
374178481Sjb		write_sized_type_rec(b, &ctt, tp->t_size);
375178481Sjb
376207578Skan		for (ep = tp->t_emem; ep != NULL && i > 0; ep = ep->el_next) {
377178481Sjb			offset = strtab_insert(&b->ctb_strtab, ep->el_name);
378178481Sjb			cte.cte_name = CTF_TYPE_NAME(CTF_STRTAB_0, offset);
379178481Sjb			cte.cte_value = ep->el_number;
380178481Sjb			ctf_buf_write(b, &cte, sizeof (cte));
381207578Skan			i--;
382178481Sjb		}
383178481Sjb		break;
384178481Sjb
385178481Sjb	case FORWARD:
386178481Sjb		ctt.ctt_info = CTF_TYPE_INFO(CTF_K_FORWARD, isroot, 0);
387178481Sjb		ctt.ctt_type = 0;
388178481Sjb		write_unsized_type_rec(b, &ctt);
389178481Sjb		break;
390178481Sjb
391178481Sjb	case TYPEDEF:
392178481Sjb		ctt.ctt_info = CTF_TYPE_INFO(CTF_K_TYPEDEF, isroot, 0);
393178481Sjb		ctt.ctt_type = tp->t_tdesc->t_id;
394178481Sjb		write_unsized_type_rec(b, &ctt);
395178481Sjb		break;
396178481Sjb
397178481Sjb	case VOLATILE:
398178481Sjb		ctt.ctt_info = CTF_TYPE_INFO(CTF_K_VOLATILE, isroot, 0);
399178481Sjb		ctt.ctt_type = tp->t_tdesc->t_id;
400178481Sjb		write_unsized_type_rec(b, &ctt);
401178481Sjb		break;
402178481Sjb
403178481Sjb	case CONST:
404178481Sjb		ctt.ctt_info = CTF_TYPE_INFO(CTF_K_CONST, isroot, 0);
405178481Sjb		ctt.ctt_type = tp->t_tdesc->t_id;
406178481Sjb		write_unsized_type_rec(b, &ctt);
407178481Sjb		break;
408178481Sjb
409178481Sjb	case FUNCTION:
410210767Srpaulo		i = tp->t_fndef->fn_nargs + tp->t_fndef->fn_vargs;
411210767Srpaulo
412210767Srpaulo		if (i > CTF_MAX_VLEN) {
413210767Srpaulo			terminate("function %s has too many args: %d > %d\n",
414210767Srpaulo			    i, CTF_MAX_VLEN);
415210767Srpaulo		}
416210767Srpaulo
417210767Srpaulo		ctt.ctt_info = CTF_TYPE_INFO(CTF_K_FUNCTION, isroot, i);
418178481Sjb		ctt.ctt_type = tp->t_fndef->fn_ret->t_id;
419178481Sjb		write_unsized_type_rec(b, &ctt);
420178481Sjb
421178546Sjb		for (i = 0; i < (int) tp->t_fndef->fn_nargs; i++) {
422178481Sjb			id = tp->t_fndef->fn_args[i]->t_id;
423178481Sjb			ctf_buf_write(b, &id, sizeof (id));
424178481Sjb		}
425178481Sjb
426178481Sjb		if (tp->t_fndef->fn_vargs) {
427178481Sjb			id = 0;
428178481Sjb			ctf_buf_write(b, &id, sizeof (id));
429178481Sjb			i++;
430178481Sjb		}
431178481Sjb
432178481Sjb		if (i & 1) {
433178481Sjb			id = 0;
434178481Sjb			ctf_buf_write(b, &id, sizeof (id));
435178481Sjb		}
436178481Sjb		break;
437178481Sjb
438178481Sjb	case RESTRICT:
439178481Sjb		ctt.ctt_info = CTF_TYPE_INFO(CTF_K_RESTRICT, isroot, 0);
440178481Sjb		ctt.ctt_type = tp->t_tdesc->t_id;
441178481Sjb		write_unsized_type_rec(b, &ctt);
442178481Sjb		break;
443178481Sjb
444178481Sjb	default:
445178481Sjb		warning("Can't write unknown type %d\n", tp->t_type);
446178481Sjb	}
447178481Sjb
448178481Sjb	debug(3, "Wrote type %d %s\n", tp->t_id, tdesc_name(tp));
449178481Sjb
450178481Sjb	return (1);
451178481Sjb}
452178481Sjb
453178481Sjbtypedef struct resbuf {
454178481Sjb	caddr_t rb_base;
455178481Sjb	caddr_t rb_ptr;
456178481Sjb	size_t rb_size;
457178481Sjb	z_stream rb_zstr;
458178481Sjb} resbuf_t;
459178481Sjb
460178481Sjbstatic void
461178481Sjbrbzs_grow(resbuf_t *rb)
462178481Sjb{
463178481Sjb	off_t ptroff = (caddr_t)rb->rb_zstr.next_out - rb->rb_base;
464178481Sjb
465178481Sjb	rb->rb_size += RES_BUF_CHUNK_SIZE;
466178481Sjb	rb->rb_base = xrealloc(rb->rb_base, rb->rb_size);
467178481Sjb	rb->rb_ptr = rb->rb_base + ptroff;
468178481Sjb	rb->rb_zstr.next_out = (Bytef *)(rb->rb_ptr);
469178481Sjb	rb->rb_zstr.avail_out += RES_BUF_CHUNK_SIZE;
470178481Sjb}
471178481Sjb
472178481Sjbstatic void
473178481Sjbcompress_start(resbuf_t *rb)
474178481Sjb{
475178481Sjb	int rc;
476178481Sjb
477178481Sjb	rb->rb_zstr.zalloc = (alloc_func)0;
478178481Sjb	rb->rb_zstr.zfree = (free_func)0;
479178481Sjb	rb->rb_zstr.opaque = (voidpf)0;
480178481Sjb
481178481Sjb	if ((rc = deflateInit(&rb->rb_zstr, Z_BEST_COMPRESSION)) != Z_OK)
482178481Sjb		parseterminate("zlib start failed: %s", zError(rc));
483178481Sjb}
484178481Sjb
485178481Sjbstatic ssize_t
486178546Sjbcompress_buffer(void *buf, size_t n, void *data)
487178481Sjb{
488178481Sjb	resbuf_t *rb = (resbuf_t *)data;
489178481Sjb	int rc;
490178481Sjb
491178481Sjb	rb->rb_zstr.next_out = (Bytef *)rb->rb_ptr;
492178481Sjb	rb->rb_zstr.avail_out = rb->rb_size - (rb->rb_ptr - rb->rb_base);
493178546Sjb	rb->rb_zstr.next_in = buf;
494178481Sjb	rb->rb_zstr.avail_in = n;
495178481Sjb
496178481Sjb	while (rb->rb_zstr.avail_in) {
497178481Sjb		if (rb->rb_zstr.avail_out == 0)
498178481Sjb			rbzs_grow(rb);
499178481Sjb
500178481Sjb		if ((rc = deflate(&rb->rb_zstr, Z_NO_FLUSH)) != Z_OK)
501178481Sjb			parseterminate("zlib deflate failed: %s", zError(rc));
502178481Sjb	}
503178481Sjb	rb->rb_ptr = (caddr_t)rb->rb_zstr.next_out;
504178481Sjb
505178481Sjb	return (n);
506178481Sjb}
507178481Sjb
508178481Sjbstatic void
509178481Sjbcompress_flush(resbuf_t *rb, int type)
510178481Sjb{
511178481Sjb	int rc;
512178481Sjb
513178481Sjb	for (;;) {
514178481Sjb		if (rb->rb_zstr.avail_out == 0)
515178481Sjb			rbzs_grow(rb);
516178481Sjb
517178481Sjb		rc = deflate(&rb->rb_zstr, type);
518178481Sjb		if ((type == Z_FULL_FLUSH && rc == Z_BUF_ERROR) ||
519178481Sjb		    (type == Z_FINISH && rc == Z_STREAM_END))
520178481Sjb			break;
521178481Sjb		else if (rc != Z_OK)
522178481Sjb			parseterminate("zlib finish failed: %s", zError(rc));
523178481Sjb	}
524178481Sjb	rb->rb_ptr = (caddr_t)rb->rb_zstr.next_out;
525178481Sjb}
526178481Sjb
527178481Sjbstatic void
528178481Sjbcompress_end(resbuf_t *rb)
529178481Sjb{
530178481Sjb	int rc;
531178481Sjb
532178481Sjb	compress_flush(rb, Z_FINISH);
533178481Sjb
534178481Sjb	if ((rc = deflateEnd(&rb->rb_zstr)) != Z_OK)
535178481Sjb		parseterminate("zlib end failed: %s", zError(rc));
536178481Sjb}
537178481Sjb
538178481Sjb/*
539178481Sjb * Pad the buffer to a power-of-2 boundary
540178481Sjb */
541178481Sjbstatic void
542178481Sjbpad_buffer(ctf_buf_t *buf, int align)
543178481Sjb{
544178481Sjb	uint_t cur = ctf_buf_cur(buf);
545178481Sjb	ssize_t topad = (align - (cur % align)) % align;
546178481Sjb	static const char pad[8] = { 0 };
547178481Sjb
548178481Sjb	while (topad > 0) {
549178481Sjb		ctf_buf_write(buf, pad, (topad > 8 ? 8 : topad));
550178481Sjb		topad -= 8;
551178481Sjb	}
552178481Sjb}
553178481Sjb
554178481Sjbstatic ssize_t
555178546Sjbbcopy_data(void *buf, size_t n, void *data)
556178481Sjb{
557178481Sjb	caddr_t *posp = (caddr_t *)data;
558178481Sjb	bcopy(buf, *posp, n);
559178481Sjb	*posp += n;
560178481Sjb	return (n);
561178481Sjb}
562178481Sjb
563178481Sjbstatic caddr_t
564178481Sjbwrite_buffer(ctf_header_t *h, ctf_buf_t *buf, size_t *resszp)
565178481Sjb{
566178481Sjb	caddr_t outbuf;
567178481Sjb	caddr_t bufpos;
568178481Sjb
569178481Sjb	outbuf = xmalloc(sizeof (ctf_header_t) + (buf->ctb_ptr - buf->ctb_base)
570178481Sjb	    + buf->ctb_strtab.str_size);
571178481Sjb
572178481Sjb	bufpos = outbuf;
573178481Sjb	(void) bcopy_data(h, sizeof (ctf_header_t), &bufpos);
574178481Sjb	(void) bcopy_data(buf->ctb_base, buf->ctb_ptr - buf->ctb_base,
575178481Sjb	    &bufpos);
576178481Sjb	(void) strtab_write(&buf->ctb_strtab, bcopy_data, &bufpos);
577178481Sjb	*resszp = bufpos - outbuf;
578178481Sjb	return (outbuf);
579178481Sjb}
580178481Sjb
581178481Sjb/*
582178481Sjb * Create the compression buffer, and fill it with the CTF and string
583178481Sjb * table data.  We flush the compression state between the two so the
584178481Sjb * dictionary used for the string tables won't be polluted with values
585178481Sjb * that made sense for the CTF data.
586178481Sjb */
587178481Sjbstatic caddr_t
588178481Sjbwrite_compressed_buffer(ctf_header_t *h, ctf_buf_t *buf, size_t *resszp)
589178481Sjb{
590178481Sjb	resbuf_t resbuf;
591178481Sjb	resbuf.rb_size = RES_BUF_CHUNK_SIZE;
592178481Sjb	resbuf.rb_base = xmalloc(resbuf.rb_size);
593178481Sjb	bcopy(h, resbuf.rb_base, sizeof (ctf_header_t));
594178481Sjb	resbuf.rb_ptr = resbuf.rb_base + sizeof (ctf_header_t);
595178481Sjb
596178481Sjb	compress_start(&resbuf);
597178481Sjb	(void) compress_buffer(buf->ctb_base, buf->ctb_ptr - buf->ctb_base,
598178481Sjb	    &resbuf);
599178481Sjb	compress_flush(&resbuf, Z_FULL_FLUSH);
600178481Sjb	(void) strtab_write(&buf->ctb_strtab, compress_buffer, &resbuf);
601178481Sjb	compress_end(&resbuf);
602178481Sjb
603178481Sjb	*resszp = (resbuf.rb_ptr - resbuf.rb_base);
604178481Sjb	return (resbuf.rb_base);
605178481Sjb}
606178481Sjb
607178481Sjbcaddr_t
608178481Sjbctf_gen(iiburst_t *iiburst, size_t *resszp, int do_compress)
609178481Sjb{
610178481Sjb	ctf_buf_t *buf = ctf_buf_new();
611178481Sjb	ctf_header_t h;
612178481Sjb	caddr_t outbuf;
613178481Sjb
614178481Sjb	int i;
615178481Sjb
616178481Sjb	/*
617178481Sjb	 * Prepare the header, and create the CTF output buffers.  The data
618178481Sjb	 * object section and function section are both lists of 2-byte
619178481Sjb	 * integers; we pad these out to the next 4-byte boundary if needed.
620178481Sjb	 */
621178481Sjb	h.cth_magic = CTF_MAGIC;
622178481Sjb	h.cth_version = CTF_VERSION;
623178481Sjb	h.cth_flags = do_compress ? CTF_F_COMPRESS : 0;
624178481Sjb	h.cth_parlabel = strtab_insert(&buf->ctb_strtab,
625178481Sjb	    iiburst->iib_td->td_parlabel);
626178481Sjb	h.cth_parname = strtab_insert(&buf->ctb_strtab,
627178481Sjb	    iiburst->iib_td->td_parname);
628178481Sjb
629178481Sjb	h.cth_lbloff = 0;
630178546Sjb	(void) list_iter(iiburst->iib_td->td_labels, write_label,
631178481Sjb	    buf);
632178481Sjb
633178481Sjb	pad_buffer(buf, 2);
634178481Sjb	h.cth_objtoff = ctf_buf_cur(buf);
635178481Sjb	for (i = 0; i < iiburst->iib_nobjts; i++)
636178481Sjb		write_objects(iiburst->iib_objts[i], buf);
637178481Sjb
638178481Sjb	pad_buffer(buf, 2);
639178481Sjb	h.cth_funcoff = ctf_buf_cur(buf);
640178481Sjb	for (i = 0; i < iiburst->iib_nfuncs; i++)
641178481Sjb		write_functions(iiburst->iib_funcs[i], buf);
642178481Sjb
643178481Sjb	pad_buffer(buf, 4);
644178481Sjb	h.cth_typeoff = ctf_buf_cur(buf);
645178546Sjb	(void) list_iter(iiburst->iib_types, write_type, buf);
646178481Sjb
647178481Sjb	debug(2, "CTF wrote %d types\n", list_count(iiburst->iib_types));
648178481Sjb
649178481Sjb	h.cth_stroff = ctf_buf_cur(buf);
650178481Sjb	h.cth_strlen = strtab_size(&buf->ctb_strtab);
651178481Sjb
652178481Sjb	/*
653178481Sjb	 * We only do compression for ctfmerge, as ctfconvert is only
654178481Sjb	 * supposed to be used on intermediary build objects. This is
655178481Sjb	 * significantly faster.
656178481Sjb	 */
657178481Sjb	if (do_compress)
658178481Sjb		outbuf = write_compressed_buffer(&h, buf, resszp);
659178481Sjb	else
660178481Sjb		outbuf = write_buffer(&h, buf, resszp);
661178481Sjb
662178481Sjb	ctf_buf_free(buf);
663178481Sjb	return (outbuf);
664178481Sjb}
665178481Sjb
666178546Sjbstatic void
667178481Sjbget_ctt_size(ctf_type_t *ctt, size_t *sizep, size_t *incrementp)
668178481Sjb{
669178481Sjb	if (ctt->ctt_size == CTF_LSIZE_SENT) {
670178481Sjb		*sizep = (size_t)CTF_TYPE_LSIZE(ctt);
671178481Sjb		*incrementp = sizeof (ctf_type_t);
672178481Sjb	} else {
673178481Sjb		*sizep = ctt->ctt_size;
674178481Sjb		*incrementp = sizeof (ctf_stype_t);
675178481Sjb	}
676178481Sjb}
677178481Sjb
678178481Sjbstatic int
679178481Sjbcount_types(ctf_header_t *h, caddr_t data)
680178481Sjb{
681178481Sjb	caddr_t dptr = data + h->cth_typeoff;
682178481Sjb	int count = 0;
683178481Sjb
684178481Sjb	dptr = data + h->cth_typeoff;
685178481Sjb	while (dptr < data + h->cth_stroff) {
686178546Sjb		void *v = (void *) dptr;
687178546Sjb		ctf_type_t *ctt = v;
688178481Sjb		size_t vlen = CTF_INFO_VLEN(ctt->ctt_info);
689178481Sjb		size_t size, increment;
690178481Sjb
691178481Sjb		get_ctt_size(ctt, &size, &increment);
692178481Sjb
693178481Sjb		switch (CTF_INFO_KIND(ctt->ctt_info)) {
694178481Sjb		case CTF_K_INTEGER:
695178481Sjb		case CTF_K_FLOAT:
696178481Sjb			dptr += 4;
697178481Sjb			break;
698178481Sjb		case CTF_K_POINTER:
699178481Sjb		case CTF_K_FORWARD:
700178481Sjb		case CTF_K_TYPEDEF:
701178481Sjb		case CTF_K_VOLATILE:
702178481Sjb		case CTF_K_CONST:
703178481Sjb		case CTF_K_RESTRICT:
704178481Sjb		case CTF_K_FUNCTION:
705178481Sjb			dptr += sizeof (ushort_t) * (vlen + (vlen & 1));
706178481Sjb			break;
707178481Sjb		case CTF_K_ARRAY:
708178481Sjb			dptr += sizeof (ctf_array_t);
709178481Sjb			break;
710178481Sjb		case CTF_K_STRUCT:
711178481Sjb		case CTF_K_UNION:
712178481Sjb			if (size < CTF_LSTRUCT_THRESH)
713178481Sjb				dptr += sizeof (ctf_member_t) * vlen;
714178481Sjb			else
715178481Sjb				dptr += sizeof (ctf_lmember_t) * vlen;
716178481Sjb			break;
717178481Sjb		case CTF_K_ENUM:
718178481Sjb			dptr += sizeof (ctf_enum_t) * vlen;
719178481Sjb			break;
720178481Sjb		case CTF_K_UNKNOWN:
721178481Sjb			break;
722178481Sjb		default:
723178481Sjb			parseterminate("Unknown CTF type %d (#%d) at %#x",
724178481Sjb			    CTF_INFO_KIND(ctt->ctt_info), count, dptr - data);
725178481Sjb		}
726178481Sjb
727178481Sjb		dptr += increment;
728178481Sjb		count++;
729178481Sjb	}
730178481Sjb
731178481Sjb	debug(3, "CTF read %d types\n", count);
732178481Sjb
733178481Sjb	return (count);
734178481Sjb}
735178481Sjb
736178481Sjb/*
737178481Sjb * Resurrect the labels stored in the CTF data, returning the index associated
738178481Sjb * with a label provided by the caller.  There are several cases, outlined
739178481Sjb * below.  Note that, given two labels, the one associated with the lesser type
740178481Sjb * index is considered to be older than the other.
741178481Sjb *
742178481Sjb *  1. matchlbl == NULL - return the index of the most recent label.
743178481Sjb *  2. matchlbl == "BASE" - return the index of the oldest label.
744178481Sjb *  3. matchlbl != NULL, but doesn't match any labels in the section - warn
745178481Sjb *	the user, and proceed as if matchlbl == "BASE" (for safety).
746178481Sjb *  4. matchlbl != NULL, and matches one of the labels in the section - return
747178481Sjb *	the type index associated with the label.
748178481Sjb */
749178481Sjbstatic int
750178481Sjbresurrect_labels(ctf_header_t *h, tdata_t *td, caddr_t ctfdata, char *matchlbl)
751178481Sjb{
752178481Sjb	caddr_t buf = ctfdata + h->cth_lbloff;
753178481Sjb	caddr_t sbuf = ctfdata + h->cth_stroff;
754178481Sjb	size_t bufsz = h->cth_objtoff - h->cth_lbloff;
755178481Sjb	int lastidx = 0, baseidx = -1;
756178546Sjb	char *baselabel = NULL;
757178481Sjb	ctf_lblent_t *ctl;
758178546Sjb	void *v = (void *) buf;
759178481Sjb
760178546Sjb	for (ctl = v; (caddr_t)ctl < buf + bufsz; ctl++) {
761178481Sjb		char *label = sbuf + ctl->ctl_label;
762178481Sjb
763178481Sjb		lastidx = ctl->ctl_typeidx;
764178481Sjb
765178481Sjb		debug(3, "Resurrected label %s type idx %d\n", label, lastidx);
766178481Sjb
767178481Sjb		tdata_label_add(td, label, lastidx);
768178481Sjb
769178481Sjb		if (baseidx == -1) {
770178481Sjb			baseidx = lastidx;
771178481Sjb			baselabel = label;
772178481Sjb			if (matchlbl != NULL && streq(matchlbl, "BASE"))
773178481Sjb				return (lastidx);
774178481Sjb		}
775178481Sjb
776178481Sjb		if (matchlbl != NULL && streq(label, matchlbl))
777178481Sjb			return (lastidx);
778178481Sjb	}
779178481Sjb
780178481Sjb	if (matchlbl != NULL) {
781178481Sjb		/* User provided a label that didn't match */
782178481Sjb		warning("%s: Cannot find label `%s' - using base (%s)\n",
783178481Sjb		    curfile, matchlbl, (baselabel ? baselabel : "NONE"));
784178481Sjb
785178481Sjb		tdata_label_free(td);
786178481Sjb		tdata_label_add(td, baselabel, baseidx);
787178481Sjb
788178481Sjb		return (baseidx);
789178481Sjb	}
790178481Sjb
791178481Sjb	return (lastidx);
792178481Sjb}
793178481Sjb
794178481Sjbstatic void
795178481Sjbresurrect_objects(ctf_header_t *h, tdata_t *td, tdesc_t **tdarr, int tdsize,
796178481Sjb    caddr_t ctfdata, symit_data_t *si)
797178481Sjb{
798178481Sjb	caddr_t buf = ctfdata + h->cth_objtoff;
799178481Sjb	size_t bufsz = h->cth_funcoff - h->cth_objtoff;
800178481Sjb	caddr_t dptr;
801178481Sjb
802178481Sjb	symit_reset(si);
803178481Sjb	for (dptr = buf; dptr < buf + bufsz; dptr += 2) {
804178546Sjb		void *v = (void *) dptr;
805178546Sjb		ushort_t id = *((ushort_t *)v);
806178481Sjb		iidesc_t *ii;
807178481Sjb		GElf_Sym *sym;
808178481Sjb
809178481Sjb		if (!(sym = symit_next(si, STT_OBJECT)) && id != 0) {
810178481Sjb			parseterminate(
811178481Sjb			    "Unexpected end of object symbols at %x of %x",
812178481Sjb			    dptr - buf, bufsz);
813178481Sjb		}
814178481Sjb
815178481Sjb		if (id == 0) {
816178481Sjb			debug(3, "Skipping null object\n");
817178481Sjb			continue;
818178481Sjb		} else if (id >= tdsize) {
819178481Sjb			parseterminate("Reference to invalid type %d", id);
820178481Sjb		}
821178481Sjb
822178481Sjb		ii = iidesc_new(symit_name(si));
823178481Sjb		ii->ii_dtype = tdarr[id];
824178481Sjb		if (GELF_ST_BIND(sym->st_info) == STB_LOCAL) {
825178481Sjb			ii->ii_type = II_SVAR;
826178481Sjb			ii->ii_owner = xstrdup(symit_curfile(si));
827178481Sjb		} else
828178481Sjb			ii->ii_type = II_GVAR;
829178481Sjb		hash_add(td->td_iihash, ii);
830178481Sjb
831178481Sjb		debug(3, "Resurrected %s object %s (%d) from %s\n",
832178481Sjb		    (ii->ii_type == II_GVAR ? "global" : "static"),
833178481Sjb		    ii->ii_name, id, (ii->ii_owner ? ii->ii_owner : "(none)"));
834178481Sjb	}
835178481Sjb}
836178481Sjb
837178481Sjbstatic void
838178481Sjbresurrect_functions(ctf_header_t *h, tdata_t *td, tdesc_t **tdarr, int tdsize,
839178481Sjb    caddr_t ctfdata, symit_data_t *si)
840178481Sjb{
841178481Sjb	caddr_t buf = ctfdata + h->cth_funcoff;
842178481Sjb	size_t bufsz = h->cth_typeoff - h->cth_funcoff;
843178481Sjb	caddr_t dptr = buf;
844178481Sjb	iidesc_t *ii;
845178481Sjb	ushort_t info;
846178481Sjb	ushort_t retid;
847178481Sjb	GElf_Sym *sym;
848178481Sjb	int i;
849178481Sjb
850178481Sjb	symit_reset(si);
851178481Sjb	while (dptr < buf + bufsz) {
852178546Sjb		void *v = (void *) dptr;
853178546Sjb		info = *((ushort_t *)v);
854178481Sjb		dptr += 2;
855178481Sjb
856178481Sjb		if (!(sym = symit_next(si, STT_FUNC)) && info != 0)
857178481Sjb			parseterminate("Unexpected end of function symbols");
858178481Sjb
859178481Sjb		if (info == 0) {
860178481Sjb			debug(3, "Skipping null function (%s)\n",
861178481Sjb			    symit_name(si));
862178481Sjb			continue;
863178481Sjb		}
864178481Sjb
865178546Sjb		v = (void *) dptr;
866178546Sjb		retid = *((ushort_t *)v);
867178481Sjb		dptr += 2;
868178481Sjb
869178481Sjb		if (retid >= tdsize)
870178481Sjb			parseterminate("Reference to invalid type %d", retid);
871178481Sjb
872178481Sjb		ii = iidesc_new(symit_name(si));
873178481Sjb		ii->ii_dtype = tdarr[retid];
874178481Sjb		if (GELF_ST_BIND(sym->st_info) == STB_LOCAL) {
875178481Sjb			ii->ii_type = II_SFUN;
876178481Sjb			ii->ii_owner = xstrdup(symit_curfile(si));
877178481Sjb		} else
878178481Sjb			ii->ii_type = II_GFUN;
879178481Sjb		ii->ii_nargs = CTF_INFO_VLEN(info);
880178481Sjb		if (ii->ii_nargs)
881178481Sjb			ii->ii_args =
882178481Sjb			    xmalloc(sizeof (tdesc_t *) * ii->ii_nargs);
883178481Sjb
884178481Sjb		for (i = 0; i < ii->ii_nargs; i++, dptr += 2) {
885178546Sjb			v = (void *) dptr;
886178546Sjb			ushort_t id = *((ushort_t *)v);
887178481Sjb			if (id >= tdsize)
888178481Sjb				parseterminate("Reference to invalid type %d",
889178481Sjb				    id);
890178481Sjb			ii->ii_args[i] = tdarr[id];
891178481Sjb		}
892178481Sjb
893178481Sjb		if (ii->ii_nargs && ii->ii_args[ii->ii_nargs - 1] == NULL) {
894178481Sjb			ii->ii_nargs--;
895178481Sjb			ii->ii_vargs = 1;
896178481Sjb		}
897178481Sjb
898178481Sjb		hash_add(td->td_iihash, ii);
899178481Sjb
900178481Sjb		debug(3, "Resurrected %s function %s (%d, %d args)\n",
901178481Sjb		    (ii->ii_type == II_GFUN ? "global" : "static"),
902178481Sjb		    ii->ii_name, retid, ii->ii_nargs);
903178481Sjb	}
904178481Sjb}
905178481Sjb
906178481Sjbstatic void
907178481Sjbresurrect_types(ctf_header_t *h, tdata_t *td, tdesc_t **tdarr, int tdsize,
908178481Sjb    caddr_t ctfdata, int maxid)
909178481Sjb{
910178481Sjb	caddr_t buf = ctfdata + h->cth_typeoff;
911178481Sjb	size_t bufsz = h->cth_stroff - h->cth_typeoff;
912178481Sjb	caddr_t sbuf = ctfdata + h->cth_stroff;
913178481Sjb	caddr_t dptr = buf;
914178481Sjb	tdesc_t *tdp;
915178481Sjb	uint_t data;
916178481Sjb	uint_t encoding;
917178481Sjb	size_t size, increment;
918178481Sjb	int tcnt;
919178481Sjb	int iicnt = 0;
920178481Sjb	tid_t tid, argid;
921178481Sjb	int kind, vlen;
922178481Sjb	int i;
923178481Sjb
924178481Sjb	elist_t **epp;
925178481Sjb	mlist_t **mpp;
926178481Sjb	intr_t *ip;
927178481Sjb
928178481Sjb	ctf_type_t *ctt;
929178481Sjb	ctf_array_t *cta;
930178481Sjb	ctf_enum_t *cte;
931178481Sjb
932178481Sjb	/*
933178481Sjb	 * A maxid of zero indicates a request to resurrect all types, so reset
934178481Sjb	 * maxid to the maximum type id.
935178481Sjb	 */
936178481Sjb	if (maxid == 0)
937178481Sjb		maxid = CTF_MAX_TYPE;
938178481Sjb
939178481Sjb	for (dptr = buf, tcnt = 0, tid = 1; dptr < buf + bufsz; tcnt++, tid++) {
940178481Sjb		if (tid > maxid)
941178481Sjb			break;
942178481Sjb
943178481Sjb		if (tid >= tdsize)
944178481Sjb			parseterminate("Reference to invalid type %d", tid);
945178481Sjb
946178546Sjb		void *v = (void *) dptr;
947178546Sjb		ctt = v;
948178481Sjb
949178481Sjb		get_ctt_size(ctt, &size, &increment);
950178481Sjb		dptr += increment;
951178481Sjb
952178481Sjb		tdp = tdarr[tid];
953178481Sjb
954178481Sjb		if (CTF_NAME_STID(ctt->ctt_name) != CTF_STRTAB_0)
955178481Sjb			parseterminate(
956210767Srpaulo			    "Unable to cope with non-zero strtab id");
957178481Sjb		if (CTF_NAME_OFFSET(ctt->ctt_name) != 0) {
958178481Sjb			tdp->t_name =
959178481Sjb			    xstrdup(sbuf + CTF_NAME_OFFSET(ctt->ctt_name));
960178481Sjb		} else
961178481Sjb			tdp->t_name = NULL;
962178481Sjb
963178481Sjb		kind = CTF_INFO_KIND(ctt->ctt_info);
964178481Sjb		vlen = CTF_INFO_VLEN(ctt->ctt_info);
965178481Sjb
966178481Sjb		switch (kind) {
967178481Sjb		case CTF_K_INTEGER:
968178481Sjb			tdp->t_type = INTRINSIC;
969178481Sjb			tdp->t_size = size;
970178481Sjb
971178546Sjb			v = (void *) dptr;
972178546Sjb			data = *((uint_t *)v);
973178481Sjb			dptr += sizeof (uint_t);
974178481Sjb			encoding = CTF_INT_ENCODING(data);
975178481Sjb
976178481Sjb			ip = xmalloc(sizeof (intr_t));
977178481Sjb			ip->intr_type = INTR_INT;
978178481Sjb			ip->intr_signed = (encoding & CTF_INT_SIGNED) ? 1 : 0;
979178481Sjb
980178481Sjb			if (encoding & CTF_INT_CHAR)
981178481Sjb				ip->intr_iformat = 'c';
982178481Sjb			else if (encoding & CTF_INT_BOOL)
983178481Sjb				ip->intr_iformat = 'b';
984178481Sjb			else if (encoding & CTF_INT_VARARGS)
985178481Sjb				ip->intr_iformat = 'v';
986178481Sjb			else
987178481Sjb				ip->intr_iformat = '\0';
988178481Sjb
989178481Sjb			ip->intr_offset = CTF_INT_OFFSET(data);
990178481Sjb			ip->intr_nbits = CTF_INT_BITS(data);
991178481Sjb			tdp->t_intr = ip;
992178481Sjb			break;
993178481Sjb
994178481Sjb		case CTF_K_FLOAT:
995178481Sjb			tdp->t_type = INTRINSIC;
996178481Sjb			tdp->t_size = size;
997178481Sjb
998178546Sjb			v = (void *) dptr;
999178546Sjb			data = *((uint_t *)v);
1000178481Sjb			dptr += sizeof (uint_t);
1001178481Sjb
1002178481Sjb			ip = xcalloc(sizeof (intr_t));
1003178481Sjb			ip->intr_type = INTR_REAL;
1004178481Sjb			ip->intr_fformat = CTF_FP_ENCODING(data);
1005178481Sjb			ip->intr_offset = CTF_FP_OFFSET(data);
1006178481Sjb			ip->intr_nbits = CTF_FP_BITS(data);
1007178481Sjb			tdp->t_intr = ip;
1008178481Sjb			break;
1009178481Sjb
1010178481Sjb		case CTF_K_POINTER:
1011178481Sjb			tdp->t_type = POINTER;
1012178481Sjb			tdp->t_tdesc = tdarr[ctt->ctt_type];
1013178481Sjb			break;
1014178481Sjb
1015178481Sjb		case CTF_K_ARRAY:
1016178481Sjb			tdp->t_type = ARRAY;
1017178481Sjb			tdp->t_size = size;
1018178481Sjb
1019178546Sjb			v = (void *) dptr;
1020178546Sjb			cta = v;
1021178481Sjb			dptr += sizeof (ctf_array_t);
1022178481Sjb
1023178481Sjb			tdp->t_ardef = xmalloc(sizeof (ardef_t));
1024178481Sjb			tdp->t_ardef->ad_contents = tdarr[cta->cta_contents];
1025178481Sjb			tdp->t_ardef->ad_idxtype = tdarr[cta->cta_index];
1026178481Sjb			tdp->t_ardef->ad_nelems = cta->cta_nelems;
1027178481Sjb			break;
1028178481Sjb
1029178481Sjb		case CTF_K_STRUCT:
1030178481Sjb		case CTF_K_UNION:
1031178481Sjb			tdp->t_type = (kind == CTF_K_STRUCT ? STRUCT : UNION);
1032178481Sjb			tdp->t_size = size;
1033178481Sjb
1034178481Sjb			if (size < CTF_LSTRUCT_THRESH) {
1035178481Sjb				for (i = 0, mpp = &tdp->t_members; i < vlen;
1036178481Sjb				    i++, mpp = &((*mpp)->ml_next)) {
1037178546Sjb					v = (void *) dptr;
1038178546Sjb					ctf_member_t *ctm = v;
1039178481Sjb					dptr += sizeof (ctf_member_t);
1040178481Sjb
1041178481Sjb					*mpp = xmalloc(sizeof (mlist_t));
1042178481Sjb					(*mpp)->ml_name = xstrdup(sbuf +
1043178481Sjb					    ctm->ctm_name);
1044178481Sjb					(*mpp)->ml_type = tdarr[ctm->ctm_type];
1045178481Sjb					(*mpp)->ml_offset = ctm->ctm_offset;
1046178481Sjb					(*mpp)->ml_size = 0;
1047178481Sjb				}
1048178481Sjb			} else {
1049178481Sjb				for (i = 0, mpp = &tdp->t_members; i < vlen;
1050178481Sjb				    i++, mpp = &((*mpp)->ml_next)) {
1051178546Sjb					v = (void *) dptr;
1052178546Sjb					ctf_lmember_t *ctlm = v;
1053178481Sjb					dptr += sizeof (ctf_lmember_t);
1054178481Sjb
1055178481Sjb					*mpp = xmalloc(sizeof (mlist_t));
1056178481Sjb					(*mpp)->ml_name = xstrdup(sbuf +
1057178481Sjb					    ctlm->ctlm_name);
1058178481Sjb					(*mpp)->ml_type =
1059178481Sjb					    tdarr[ctlm->ctlm_type];
1060178481Sjb					(*mpp)->ml_offset =
1061178481Sjb					    (int)CTF_LMEM_OFFSET(ctlm);
1062178481Sjb					(*mpp)->ml_size = 0;
1063178481Sjb				}
1064178481Sjb			}
1065178481Sjb
1066178481Sjb			*mpp = NULL;
1067178481Sjb			break;
1068178481Sjb
1069178481Sjb		case CTF_K_ENUM:
1070178481Sjb			tdp->t_type = ENUM;
1071178481Sjb			tdp->t_size = size;
1072178481Sjb
1073178481Sjb			for (i = 0, epp = &tdp->t_emem; i < vlen;
1074178481Sjb			    i++, epp = &((*epp)->el_next)) {
1075178546Sjb				v = (void *) dptr;
1076178546Sjb				cte = v;
1077178481Sjb				dptr += sizeof (ctf_enum_t);
1078178481Sjb
1079178481Sjb				*epp = xmalloc(sizeof (elist_t));
1080178481Sjb				(*epp)->el_name = xstrdup(sbuf + cte->cte_name);
1081178481Sjb				(*epp)->el_number = cte->cte_value;
1082178481Sjb			}
1083178481Sjb			*epp = NULL;
1084178481Sjb			break;
1085178481Sjb
1086178481Sjb		case CTF_K_FORWARD:
1087178481Sjb			tdp->t_type = FORWARD;
1088178481Sjb			list_add(&td->td_fwdlist, tdp);
1089178481Sjb			break;
1090178481Sjb
1091178481Sjb		case CTF_K_TYPEDEF:
1092178481Sjb			tdp->t_type = TYPEDEF;
1093178481Sjb			tdp->t_tdesc = tdarr[ctt->ctt_type];
1094178481Sjb			break;
1095178481Sjb
1096178481Sjb		case CTF_K_VOLATILE:
1097178481Sjb			tdp->t_type = VOLATILE;
1098178481Sjb			tdp->t_tdesc = tdarr[ctt->ctt_type];
1099178481Sjb			break;
1100178481Sjb
1101178481Sjb		case CTF_K_CONST:
1102178481Sjb			tdp->t_type = CONST;
1103178481Sjb			tdp->t_tdesc = tdarr[ctt->ctt_type];
1104178481Sjb			break;
1105178481Sjb
1106178481Sjb		case CTF_K_FUNCTION:
1107178481Sjb			tdp->t_type = FUNCTION;
1108178481Sjb			tdp->t_fndef = xcalloc(sizeof (fndef_t));
1109178481Sjb			tdp->t_fndef->fn_ret = tdarr[ctt->ctt_type];
1110178481Sjb
1111178546Sjb			v = (void *) (dptr + (sizeof (ushort_t) * (vlen - 1)));
1112178546Sjb			if (vlen > 0 && *(ushort_t *)v == 0)
1113178481Sjb				tdp->t_fndef->fn_vargs = 1;
1114178481Sjb
1115178481Sjb			tdp->t_fndef->fn_nargs = vlen - tdp->t_fndef->fn_vargs;
1116178481Sjb			tdp->t_fndef->fn_args = xcalloc(sizeof (tdesc_t) *
1117178481Sjb			    vlen - tdp->t_fndef->fn_vargs);
1118178481Sjb
1119178481Sjb			for (i = 0; i < vlen; i++) {
1120178546Sjb				v = (void *) dptr;
1121178546Sjb				argid = *(ushort_t *)v;
1122178481Sjb				dptr += sizeof (ushort_t);
1123178481Sjb
1124178481Sjb				if (argid != 0)
1125178481Sjb					tdp->t_fndef->fn_args[i] = tdarr[argid];
1126178481Sjb			}
1127178481Sjb
1128178481Sjb			if (vlen & 1)
1129178481Sjb				dptr += sizeof (ushort_t);
1130178481Sjb			break;
1131178481Sjb
1132178481Sjb		case CTF_K_RESTRICT:
1133178481Sjb			tdp->t_type = RESTRICT;
1134178481Sjb			tdp->t_tdesc = tdarr[ctt->ctt_type];
1135178481Sjb			break;
1136178481Sjb
1137178481Sjb		case CTF_K_UNKNOWN:
1138178481Sjb			break;
1139178481Sjb
1140178481Sjb		default:
1141178481Sjb			warning("Can't parse unknown CTF type %d\n", kind);
1142178481Sjb		}
1143178481Sjb
1144178481Sjb		if (CTF_INFO_ISROOT(ctt->ctt_info)) {
1145178481Sjb			iidesc_t *ii = iidesc_new(tdp->t_name);
1146178481Sjb			if (tdp->t_type == STRUCT || tdp->t_type == UNION ||
1147178481Sjb			    tdp->t_type == ENUM)
1148178481Sjb				ii->ii_type = II_SOU;
1149178481Sjb			else
1150178481Sjb				ii->ii_type = II_TYPE;
1151178481Sjb			ii->ii_dtype = tdp;
1152178481Sjb			hash_add(td->td_iihash, ii);
1153178481Sjb
1154178481Sjb			iicnt++;
1155178481Sjb		}
1156178481Sjb
1157178481Sjb		debug(3, "Resurrected %d %stype %s (%d)\n", tdp->t_type,
1158178481Sjb		    (CTF_INFO_ISROOT(ctt->ctt_info) ? "root " : ""),
1159178481Sjb		    tdesc_name(tdp), tdp->t_id);
1160178481Sjb	}
1161178481Sjb
1162178481Sjb	debug(3, "Resurrected %d types (%d were roots)\n", tcnt, iicnt);
1163178481Sjb}
1164178481Sjb
1165178481Sjb/*
1166178481Sjb * For lack of other inspiration, we're going to take the boring route.  We
1167178481Sjb * count the number of types.  This lets us malloc that many tdesc structs
1168178481Sjb * before we start filling them in.  This has the advantage of allowing us to
1169178481Sjb * avoid a merge-esque remap step.
1170178481Sjb */
1171178481Sjbstatic tdata_t *
1172178481Sjbctf_parse(ctf_header_t *h, caddr_t buf, symit_data_t *si, char *label)
1173178481Sjb{
1174178481Sjb	tdata_t *td = tdata_new();
1175178481Sjb	tdesc_t **tdarr;
1176178481Sjb	int ntypes = count_types(h, buf);
1177178481Sjb	int idx, i;
1178178481Sjb
1179178481Sjb	/* shudder */
1180178481Sjb	tdarr = xcalloc(sizeof (tdesc_t *) * (ntypes + 1));
1181178481Sjb	tdarr[0] = NULL;
1182178481Sjb	for (i = 1; i <= ntypes; i++) {
1183178481Sjb		tdarr[i] = xcalloc(sizeof (tdesc_t));
1184178481Sjb		tdarr[i]->t_id = i;
1185178481Sjb	}
1186178481Sjb
1187178481Sjb	td->td_parlabel = xstrdup(buf + h->cth_stroff + h->cth_parlabel);
1188178481Sjb
1189178481Sjb	/* we have the technology - we can rebuild them */
1190178481Sjb	idx = resurrect_labels(h, td, buf, label);
1191178481Sjb
1192178481Sjb	resurrect_objects(h, td, tdarr, ntypes + 1, buf, si);
1193178481Sjb	resurrect_functions(h, td, tdarr, ntypes + 1, buf, si);
1194178481Sjb	resurrect_types(h, td, tdarr, ntypes + 1, buf, idx);
1195178481Sjb
1196178481Sjb	free(tdarr);
1197178481Sjb
1198178481Sjb	td->td_nextid = ntypes + 1;
1199178481Sjb
1200178481Sjb	return (td);
1201178481Sjb}
1202178481Sjb
1203178481Sjbstatic size_t
1204178481Sjbdecompress_ctf(caddr_t cbuf, size_t cbufsz, caddr_t dbuf, size_t dbufsz)
1205178481Sjb{
1206178481Sjb	z_stream zstr;
1207178481Sjb	int rc;
1208178481Sjb
1209178481Sjb	zstr.zalloc = (alloc_func)0;
1210178481Sjb	zstr.zfree = (free_func)0;
1211178481Sjb	zstr.opaque = (voidpf)0;
1212178481Sjb
1213178481Sjb	zstr.next_in = (Bytef *)cbuf;
1214178481Sjb	zstr.avail_in = cbufsz;
1215178481Sjb	zstr.next_out = (Bytef *)dbuf;
1216178481Sjb	zstr.avail_out = dbufsz;
1217178481Sjb
1218178481Sjb	if ((rc = inflateInit(&zstr)) != Z_OK ||
1219178481Sjb	    (rc = inflate(&zstr, Z_NO_FLUSH)) != Z_STREAM_END ||
1220178481Sjb	    (rc = inflateEnd(&zstr)) != Z_OK) {
1221178481Sjb		warning("CTF decompress zlib error %s\n", zError(rc));
1222178546Sjb		return (0);
1223178481Sjb	}
1224178481Sjb
1225178481Sjb	debug(3, "reflated %lu bytes to %lu, pointer at %d\n",
1226178481Sjb	    zstr.total_in, zstr.total_out, (caddr_t)zstr.next_in - cbuf);
1227178481Sjb
1228178481Sjb	return (zstr.total_out);
1229178481Sjb}
1230178481Sjb
1231178481Sjb/*
1232178481Sjb * Reconstruct the type tree from a given buffer of CTF data.  Only the types
1233178481Sjb * up to the type associated with the provided label, inclusive, will be
1234178481Sjb * reconstructed.  If a NULL label is provided, all types will be reconstructed.
1235178481Sjb *
1236178481Sjb * This function won't work on files that have been uniquified.
1237178481Sjb */
1238178481Sjbtdata_t *
1239178481Sjbctf_load(char *file, caddr_t buf, size_t bufsz, symit_data_t *si, char *label)
1240178481Sjb{
1241178481Sjb	ctf_header_t *h;
1242178481Sjb	caddr_t ctfdata;
1243178481Sjb	size_t ctfdatasz;
1244178481Sjb	tdata_t *td;
1245178481Sjb
1246178481Sjb	curfile = file;
1247178481Sjb
1248178481Sjb	if (bufsz < sizeof (ctf_header_t))
1249178481Sjb		parseterminate("Corrupt CTF - short header");
1250178481Sjb
1251178546Sjb	void *v = (void *) buf;
1252178546Sjb	h = v;
1253178481Sjb	buf += sizeof (ctf_header_t);
1254178481Sjb	bufsz -= sizeof (ctf_header_t);
1255178481Sjb
1256178481Sjb	if (h->cth_magic != CTF_MAGIC)
1257178481Sjb		parseterminate("Corrupt CTF - bad magic 0x%x", h->cth_magic);
1258178481Sjb
1259178481Sjb	if (h->cth_version != CTF_VERSION)
1260178481Sjb		parseterminate("Unknown CTF version %d", h->cth_version);
1261178481Sjb
1262178481Sjb	ctfdatasz = h->cth_stroff + h->cth_strlen;
1263178481Sjb	if (h->cth_flags & CTF_F_COMPRESS) {
1264178481Sjb		size_t actual;
1265178481Sjb
1266178481Sjb		ctfdata = xmalloc(ctfdatasz);
1267178481Sjb		if ((actual = decompress_ctf(buf, bufsz, ctfdata, ctfdatasz)) !=
1268178481Sjb		    ctfdatasz) {
1269178481Sjb			parseterminate("Corrupt CTF - short decompression "
1270178481Sjb			    "(was %d, expecting %d)", actual, ctfdatasz);
1271178481Sjb		}
1272178481Sjb	} else {
1273178481Sjb		ctfdata = buf;
1274178481Sjb		ctfdatasz = bufsz;
1275178481Sjb	}
1276178481Sjb
1277178481Sjb	td = ctf_parse(h, ctfdata, si, label);
1278178481Sjb
1279178481Sjb	if (h->cth_flags & CTF_F_COMPRESS)
1280178481Sjb		free(ctfdata);
1281178481Sjb
1282178481Sjb	curfile = NULL;
1283178481Sjb
1284178481Sjb	return (td);
1285178481Sjb}
1286