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