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 */
50249656Sedstatic char *curfile;
51178481Sjb
52178481Sjb#define	CTF_BUF_CHUNK_SIZE	(64 * 1024)
53178481Sjb#define	RES_BUF_CHUNK_SIZE	(64 * 1024)
54178481Sjb
55253678Spfgstatic int ntypes = 0;		/* The number of types. */
56253661Spfg
57178481Sjbstruct ctf_buf {
58178481Sjb	strtab_t ctb_strtab;	/* string table */
59178481Sjb	caddr_t ctb_base;	/* pointer to base of buffer */
60178481Sjb	caddr_t ctb_end;	/* pointer to end of buffer */
61178481Sjb	caddr_t ctb_ptr;	/* pointer to empty buffer space */
62178481Sjb	size_t ctb_size;	/* size of buffer */
63178481Sjb	int nptent;		/* number of processed types */
64178481Sjb	int ntholes;		/* number of type holes */
65178481Sjb};
66178481Sjb
67233407Sgonzo/*
68233407Sgonzo * Macros to reverse byte order
69233407Sgonzo */
70233407Sgonzo#define	BSWAP_8(x)	((x) & 0xff)
71233407Sgonzo#define	BSWAP_16(x)	((BSWAP_8(x) << 8) | BSWAP_8((x) >> 8))
72233407Sgonzo#define	BSWAP_32(x)	((BSWAP_16(x) << 16) | BSWAP_16((x) >> 16))
73233407Sgonzo
74233407Sgonzo#define	SWAP_16(x)	(x) = BSWAP_16(x)
75233407Sgonzo#define	SWAP_32(x)	(x) = BSWAP_32(x)
76233407Sgonzo
77233407Sgonzostatic int target_requires_swap;
78233407Sgonzo
79178481Sjb/*PRINTFLIKE1*/
80178481Sjbstatic void
81178546Sjbparseterminate(const char *fmt, ...)
82178481Sjb{
83178481Sjb	static char msgbuf[1024]; /* sigh */
84178481Sjb	va_list ap;
85178481Sjb
86178481Sjb	va_start(ap, fmt);
87178481Sjb	vsnprintf(msgbuf, sizeof (msgbuf), fmt, ap);
88178481Sjb	va_end(ap);
89178481Sjb
90178481Sjb	terminate("%s: %s\n", curfile, msgbuf);
91178481Sjb}
92178481Sjb
93178546Sjbstatic void
94178481Sjbctf_buf_grow(ctf_buf_t *b)
95178481Sjb{
96178481Sjb	off_t ptroff = b->ctb_ptr - b->ctb_base;
97178481Sjb
98178481Sjb	b->ctb_size += CTF_BUF_CHUNK_SIZE;
99178481Sjb	b->ctb_base = xrealloc(b->ctb_base, b->ctb_size);
100178481Sjb	b->ctb_end = b->ctb_base + b->ctb_size;
101178481Sjb	b->ctb_ptr = b->ctb_base + ptroff;
102178481Sjb}
103178481Sjb
104178546Sjbstatic ctf_buf_t *
105178481Sjbctf_buf_new(void)
106178481Sjb{
107178481Sjb	ctf_buf_t *b = xcalloc(sizeof (ctf_buf_t));
108178481Sjb
109178481Sjb	strtab_create(&b->ctb_strtab);
110178481Sjb	ctf_buf_grow(b);
111178481Sjb
112178481Sjb	return (b);
113178481Sjb}
114178481Sjb
115178546Sjbstatic void
116178481Sjbctf_buf_free(ctf_buf_t *b)
117178481Sjb{
118178481Sjb	strtab_destroy(&b->ctb_strtab);
119178481Sjb	free(b->ctb_base);
120178481Sjb	free(b);
121178481Sjb}
122178481Sjb
123178546Sjbstatic uint_t
124178481Sjbctf_buf_cur(ctf_buf_t *b)
125178481Sjb{
126178481Sjb	return (b->ctb_ptr - b->ctb_base);
127178481Sjb}
128178481Sjb
129178546Sjbstatic void
130178546Sjbctf_buf_write(ctf_buf_t *b, void const *p, size_t n)
131178481Sjb{
132178481Sjb	size_t len;
133178481Sjb
134178481Sjb	while (n != 0) {
135178481Sjb		if (b->ctb_ptr == b->ctb_end)
136178481Sjb			ctf_buf_grow(b);
137178481Sjb
138178481Sjb		len = MIN((size_t)(b->ctb_end - b->ctb_ptr), n);
139178481Sjb		bcopy(p, b->ctb_ptr, len);
140178481Sjb		b->ctb_ptr += len;
141178481Sjb
142178546Sjb		p = (char const *)p + len;
143178481Sjb		n -= len;
144178481Sjb	}
145178481Sjb}
146178481Sjb
147178481Sjbstatic int
148178546Sjbwrite_label(void *arg1, void *arg2)
149178481Sjb{
150178546Sjb	labelent_t *le = arg1;
151178546Sjb	ctf_buf_t *b = arg2;
152178481Sjb	ctf_lblent_t ctl;
153178481Sjb
154178481Sjb	ctl.ctl_label = strtab_insert(&b->ctb_strtab, le->le_name);
155178481Sjb	ctl.ctl_typeidx = le->le_idx;
156178481Sjb
157233407Sgonzo	if (target_requires_swap) {
158233407Sgonzo		SWAP_32(ctl.ctl_label);
159233407Sgonzo		SWAP_32(ctl.ctl_typeidx);
160233407Sgonzo	}
161233407Sgonzo
162178481Sjb	ctf_buf_write(b, &ctl, sizeof (ctl));
163178481Sjb
164178481Sjb	return (1);
165178481Sjb}
166178481Sjb
167178481Sjbstatic void
168178481Sjbwrite_objects(iidesc_t *idp, ctf_buf_t *b)
169178481Sjb{
170178481Sjb	ushort_t id = (idp ? idp->ii_dtype->t_id : 0);
171178481Sjb
172178481Sjb	ctf_buf_write(b, &id, sizeof (id));
173178481Sjb
174233407Sgonzo	if (target_requires_swap) {
175233407Sgonzo		SWAP_16(id);
176233407Sgonzo	}
177233407Sgonzo
178178481Sjb	debug(3, "Wrote object %s (%d)\n", (idp ? idp->ii_name : "(null)"), id);
179178481Sjb}
180178481Sjb
181178481Sjbstatic void
182178481Sjbwrite_functions(iidesc_t *idp, ctf_buf_t *b)
183178481Sjb{
184178481Sjb	ushort_t fdata[2];
185178481Sjb	ushort_t id;
186178481Sjb	int nargs;
187178481Sjb	int i;
188178481Sjb
189178481Sjb	if (!idp) {
190178481Sjb		fdata[0] = 0;
191178481Sjb		ctf_buf_write(b, &fdata[0], sizeof (fdata[0]));
192178481Sjb
193178481Sjb		debug(3, "Wrote function (null)\n");
194178481Sjb		return;
195178481Sjb	}
196178481Sjb
197178481Sjb	nargs = idp->ii_nargs + (idp->ii_vargs != 0);
198210767Srpaulo
199210767Srpaulo	if (nargs > CTF_MAX_VLEN) {
200210767Srpaulo		terminate("function %s has too many args: %d > %d\n",
201210767Srpaulo		    idp->ii_name, nargs, CTF_MAX_VLEN);
202210767Srpaulo	}
203210767Srpaulo
204178481Sjb	fdata[0] = CTF_TYPE_INFO(CTF_K_FUNCTION, 1, nargs);
205178481Sjb	fdata[1] = idp->ii_dtype->t_id;
206233407Sgonzo
207233407Sgonzo	if (target_requires_swap) {
208233407Sgonzo		SWAP_16(fdata[0]);
209233407Sgonzo		SWAP_16(fdata[1]);
210233407Sgonzo	}
211233407Sgonzo
212178481Sjb	ctf_buf_write(b, fdata, sizeof (fdata));
213178481Sjb
214178481Sjb	for (i = 0; i < idp->ii_nargs; i++) {
215178481Sjb		id = idp->ii_args[i]->t_id;
216233407Sgonzo
217233407Sgonzo		if (target_requires_swap) {
218233407Sgonzo			SWAP_16(id);
219233407Sgonzo		}
220233407Sgonzo
221178481Sjb		ctf_buf_write(b, &id, sizeof (id));
222178481Sjb	}
223178481Sjb
224178481Sjb	if (idp->ii_vargs) {
225178481Sjb		id = 0;
226178481Sjb		ctf_buf_write(b, &id, sizeof (id));
227178481Sjb	}
228178481Sjb
229178481Sjb	debug(3, "Wrote function %s (%d args)\n", idp->ii_name, nargs);
230178481Sjb}
231178481Sjb
232178481Sjb/*
233178481Sjb * Depending on the size of the type being described, either a ctf_stype_t (for
234178481Sjb * types with size < CTF_LSTRUCT_THRESH) or a ctf_type_t (all others) will be
235178481Sjb * written.  We isolate the determination here so the rest of the writer code
236178481Sjb * doesn't need to care.
237178481Sjb */
238178481Sjbstatic void
239178481Sjbwrite_sized_type_rec(ctf_buf_t *b, ctf_type_t *ctt, size_t size)
240178481Sjb{
241178481Sjb	if (size > CTF_MAX_SIZE) {
242178481Sjb		ctt->ctt_size = CTF_LSIZE_SENT;
243178481Sjb		ctt->ctt_lsizehi = CTF_SIZE_TO_LSIZE_HI(size);
244178481Sjb		ctt->ctt_lsizelo = CTF_SIZE_TO_LSIZE_LO(size);
245233407Sgonzo		if (target_requires_swap) {
246233407Sgonzo			SWAP_32(ctt->ctt_name);
247233407Sgonzo			SWAP_16(ctt->ctt_info);
248233407Sgonzo			SWAP_16(ctt->ctt_size);
249233407Sgonzo			SWAP_32(ctt->ctt_lsizehi);
250233407Sgonzo			SWAP_32(ctt->ctt_lsizelo);
251233407Sgonzo		}
252178481Sjb		ctf_buf_write(b, ctt, sizeof (*ctt));
253178481Sjb	} else {
254178481Sjb		ctf_stype_t *cts = (ctf_stype_t *)ctt;
255178481Sjb
256178481Sjb		cts->ctt_size = (ushort_t)size;
257233407Sgonzo
258233407Sgonzo		if (target_requires_swap) {
259233407Sgonzo			SWAP_32(cts->ctt_name);
260233407Sgonzo			SWAP_16(cts->ctt_info);
261233407Sgonzo			SWAP_16(cts->ctt_size);
262233407Sgonzo		}
263233407Sgonzo
264178481Sjb		ctf_buf_write(b, cts, sizeof (*cts));
265178481Sjb	}
266178481Sjb}
267178481Sjb
268178481Sjbstatic void
269178481Sjbwrite_unsized_type_rec(ctf_buf_t *b, ctf_type_t *ctt)
270178481Sjb{
271178481Sjb	ctf_stype_t *cts = (ctf_stype_t *)ctt;
272178481Sjb
273233407Sgonzo	if (target_requires_swap) {
274233407Sgonzo		SWAP_32(cts->ctt_name);
275233407Sgonzo		SWAP_16(cts->ctt_info);
276233407Sgonzo		SWAP_16(cts->ctt_size);
277233407Sgonzo	}
278233407Sgonzo
279178481Sjb	ctf_buf_write(b, cts, sizeof (*cts));
280178481Sjb}
281178481Sjb
282178481Sjbstatic int
283178546Sjbwrite_type(void *arg1, void *arg2)
284178481Sjb{
285178546Sjb	tdesc_t *tp = arg1;
286178546Sjb	ctf_buf_t *b = arg2;
287178481Sjb	elist_t *ep;
288178481Sjb	mlist_t *mp;
289178481Sjb	intr_t *ip;
290178481Sjb
291178481Sjb	size_t offset;
292178481Sjb	uint_t encoding;
293178481Sjb	uint_t data;
294178481Sjb	int isroot = tp->t_flags & TDESC_F_ISROOT;
295178481Sjb	int i;
296178481Sjb
297178481Sjb	ctf_type_t ctt;
298178481Sjb	ctf_array_t cta;
299178481Sjb	ctf_member_t ctm;
300178481Sjb	ctf_lmember_t ctlm;
301178481Sjb	ctf_enum_t cte;
302178481Sjb	ushort_t id;
303178481Sjb
304178481Sjb	ctlm.ctlm_pad = 0;
305178481Sjb
306178481Sjb	/*
307178481Sjb	 * There shouldn't be any holes in the type list (where a hole is
308178481Sjb	 * defined as two consecutive tdescs without consecutive ids), but
309178481Sjb	 * check for them just in case.  If we do find holes, we need to make
310178481Sjb	 * fake entries to fill the holes, or we won't be able to reconstruct
311178481Sjb	 * the tree from the written data.
312178481Sjb	 */
313178481Sjb	if (++b->nptent < CTF_TYPE_TO_INDEX(tp->t_id)) {
314178481Sjb		debug(2, "genctf: type hole from %d < x < %d\n",
315178481Sjb		    b->nptent - 1, CTF_TYPE_TO_INDEX(tp->t_id));
316178481Sjb
317178481Sjb		ctt.ctt_name = CTF_TYPE_NAME(CTF_STRTAB_0, 0);
318178481Sjb		ctt.ctt_info = CTF_TYPE_INFO(0, 0, 0);
319178481Sjb		while (b->nptent < CTF_TYPE_TO_INDEX(tp->t_id)) {
320178481Sjb			write_sized_type_rec(b, &ctt, 0);
321178481Sjb			b->nptent++;
322178481Sjb		}
323178481Sjb	}
324178481Sjb
325178481Sjb	offset = strtab_insert(&b->ctb_strtab, tp->t_name);
326178481Sjb	ctt.ctt_name = CTF_TYPE_NAME(CTF_STRTAB_0, offset);
327178481Sjb
328178481Sjb	switch (tp->t_type) {
329178481Sjb	case INTRINSIC:
330178481Sjb		ip = tp->t_intr;
331178481Sjb		if (ip->intr_type == INTR_INT)
332178481Sjb			ctt.ctt_info = CTF_TYPE_INFO(CTF_K_INTEGER,
333178481Sjb			    isroot, 1);
334178481Sjb		else
335178481Sjb			ctt.ctt_info = CTF_TYPE_INFO(CTF_K_FLOAT, isroot, 1);
336178481Sjb		write_sized_type_rec(b, &ctt, tp->t_size);
337178481Sjb
338178481Sjb		encoding = 0;
339178481Sjb
340178481Sjb		if (ip->intr_type == INTR_INT) {
341178481Sjb			if (ip->intr_signed)
342178481Sjb				encoding |= CTF_INT_SIGNED;
343178481Sjb			if (ip->intr_iformat == 'c')
344178481Sjb				encoding |= CTF_INT_CHAR;
345178481Sjb			else if (ip->intr_iformat == 'b')
346178481Sjb				encoding |= CTF_INT_BOOL;
347178481Sjb			else if (ip->intr_iformat == 'v')
348178481Sjb				encoding |= CTF_INT_VARARGS;
349178481Sjb		} else
350178481Sjb			encoding = ip->intr_fformat;
351178481Sjb
352178481Sjb		data = CTF_INT_DATA(encoding, ip->intr_offset, ip->intr_nbits);
353233407Sgonzo		if (target_requires_swap) {
354233407Sgonzo			SWAP_32(data);
355233407Sgonzo		}
356178481Sjb		ctf_buf_write(b, &data, sizeof (data));
357178481Sjb		break;
358178481Sjb
359178481Sjb	case POINTER:
360178481Sjb		ctt.ctt_info = CTF_TYPE_INFO(CTF_K_POINTER, isroot, 0);
361178481Sjb		ctt.ctt_type = tp->t_tdesc->t_id;
362178481Sjb		write_unsized_type_rec(b, &ctt);
363178481Sjb		break;
364178481Sjb
365178481Sjb	case ARRAY:
366178481Sjb		ctt.ctt_info = CTF_TYPE_INFO(CTF_K_ARRAY, isroot, 1);
367178481Sjb		write_sized_type_rec(b, &ctt, tp->t_size);
368178481Sjb
369178481Sjb		cta.cta_contents = tp->t_ardef->ad_contents->t_id;
370178481Sjb		cta.cta_index = tp->t_ardef->ad_idxtype->t_id;
371178481Sjb		cta.cta_nelems = tp->t_ardef->ad_nelems;
372233407Sgonzo		if (target_requires_swap) {
373233407Sgonzo			SWAP_16(cta.cta_contents);
374233407Sgonzo			SWAP_16(cta.cta_index);
375233407Sgonzo			SWAP_32(cta.cta_nelems);
376233407Sgonzo		}
377178481Sjb		ctf_buf_write(b, &cta, sizeof (cta));
378178481Sjb		break;
379178481Sjb
380178481Sjb	case STRUCT:
381178481Sjb	case UNION:
382178481Sjb		for (i = 0, mp = tp->t_members; mp != NULL; mp = mp->ml_next)
383178481Sjb			i++; /* count up struct or union members */
384178481Sjb
385210767Srpaulo		if (i > CTF_MAX_VLEN) {
386210767Srpaulo			terminate("sou %s has too many members: %d > %d\n",
387210767Srpaulo			    tdesc_name(tp), i, CTF_MAX_VLEN);
388210767Srpaulo		}
389210767Srpaulo
390178481Sjb		if (tp->t_type == STRUCT)
391178481Sjb			ctt.ctt_info = CTF_TYPE_INFO(CTF_K_STRUCT, isroot, i);
392178481Sjb		else
393178481Sjb			ctt.ctt_info = CTF_TYPE_INFO(CTF_K_UNION, isroot, i);
394178481Sjb
395178481Sjb		write_sized_type_rec(b, &ctt, tp->t_size);
396178481Sjb
397178481Sjb		if (tp->t_size < CTF_LSTRUCT_THRESH) {
398178481Sjb			for (mp = tp->t_members; mp != NULL; mp = mp->ml_next) {
399178481Sjb				offset = strtab_insert(&b->ctb_strtab,
400178481Sjb				    mp->ml_name);
401178481Sjb
402178481Sjb				ctm.ctm_name = CTF_TYPE_NAME(CTF_STRTAB_0,
403178481Sjb				    offset);
404178481Sjb				ctm.ctm_type = mp->ml_type->t_id;
405178481Sjb				ctm.ctm_offset = mp->ml_offset;
406233407Sgonzo				if (target_requires_swap) {
407233407Sgonzo					SWAP_32(ctm.ctm_name);
408233407Sgonzo					SWAP_16(ctm.ctm_type);
409233407Sgonzo					SWAP_16(ctm.ctm_offset);
410233407Sgonzo				}
411178481Sjb				ctf_buf_write(b, &ctm, sizeof (ctm));
412178481Sjb			}
413178481Sjb		} else {
414178481Sjb			for (mp = tp->t_members; mp != NULL; mp = mp->ml_next) {
415178481Sjb				offset = strtab_insert(&b->ctb_strtab,
416178481Sjb				    mp->ml_name);
417178481Sjb
418178481Sjb				ctlm.ctlm_name = CTF_TYPE_NAME(CTF_STRTAB_0,
419178481Sjb				    offset);
420178481Sjb				ctlm.ctlm_type = mp->ml_type->t_id;
421178481Sjb				ctlm.ctlm_offsethi =
422178481Sjb				    CTF_OFFSET_TO_LMEMHI(mp->ml_offset);
423178481Sjb				ctlm.ctlm_offsetlo =
424178481Sjb				    CTF_OFFSET_TO_LMEMLO(mp->ml_offset);
425233407Sgonzo
426233407Sgonzo				if (target_requires_swap) {
427233407Sgonzo					SWAP_32(ctlm.ctlm_name);
428233407Sgonzo					SWAP_16(ctlm.ctlm_type);
429233407Sgonzo					SWAP_32(ctlm.ctlm_offsethi);
430233407Sgonzo					SWAP_32(ctlm.ctlm_offsetlo);
431233407Sgonzo				}
432233407Sgonzo
433178481Sjb				ctf_buf_write(b, &ctlm, sizeof (ctlm));
434178481Sjb			}
435178481Sjb		}
436178481Sjb		break;
437178481Sjb
438178481Sjb	case ENUM:
439178481Sjb		for (i = 0, ep = tp->t_emem; ep != NULL; ep = ep->el_next)
440178481Sjb			i++; /* count up enum members */
441178481Sjb
442207578Skan		if (i > CTF_MAX_VLEN) {
443207578Skan			warning("enum %s has too many values: %d > %d\n",
444207578Skan			    tdesc_name(tp), i, CTF_MAX_VLEN);
445207578Skan			i = CTF_MAX_VLEN;
446207578Skan		}
447207578Skan
448178481Sjb		ctt.ctt_info = CTF_TYPE_INFO(CTF_K_ENUM, isroot, i);
449178481Sjb		write_sized_type_rec(b, &ctt, tp->t_size);
450178481Sjb
451207578Skan		for (ep = tp->t_emem; ep != NULL && i > 0; ep = ep->el_next) {
452178481Sjb			offset = strtab_insert(&b->ctb_strtab, ep->el_name);
453178481Sjb			cte.cte_name = CTF_TYPE_NAME(CTF_STRTAB_0, offset);
454178481Sjb			cte.cte_value = ep->el_number;
455233407Sgonzo
456233407Sgonzo			if (target_requires_swap) {
457233407Sgonzo				SWAP_32(cte.cte_name);
458233407Sgonzo				SWAP_32(cte.cte_value);
459233407Sgonzo			}
460233407Sgonzo
461178481Sjb			ctf_buf_write(b, &cte, sizeof (cte));
462207578Skan			i--;
463178481Sjb		}
464178481Sjb		break;
465178481Sjb
466178481Sjb	case FORWARD:
467178481Sjb		ctt.ctt_info = CTF_TYPE_INFO(CTF_K_FORWARD, isroot, 0);
468178481Sjb		ctt.ctt_type = 0;
469178481Sjb		write_unsized_type_rec(b, &ctt);
470178481Sjb		break;
471178481Sjb
472178481Sjb	case TYPEDEF:
473178481Sjb		ctt.ctt_info = CTF_TYPE_INFO(CTF_K_TYPEDEF, isroot, 0);
474178481Sjb		ctt.ctt_type = tp->t_tdesc->t_id;
475178481Sjb		write_unsized_type_rec(b, &ctt);
476178481Sjb		break;
477178481Sjb
478178481Sjb	case VOLATILE:
479178481Sjb		ctt.ctt_info = CTF_TYPE_INFO(CTF_K_VOLATILE, isroot, 0);
480178481Sjb		ctt.ctt_type = tp->t_tdesc->t_id;
481178481Sjb		write_unsized_type_rec(b, &ctt);
482178481Sjb		break;
483178481Sjb
484178481Sjb	case CONST:
485178481Sjb		ctt.ctt_info = CTF_TYPE_INFO(CTF_K_CONST, isroot, 0);
486178481Sjb		ctt.ctt_type = tp->t_tdesc->t_id;
487178481Sjb		write_unsized_type_rec(b, &ctt);
488178481Sjb		break;
489178481Sjb
490178481Sjb	case FUNCTION:
491210767Srpaulo		i = tp->t_fndef->fn_nargs + tp->t_fndef->fn_vargs;
492210767Srpaulo
493210767Srpaulo		if (i > CTF_MAX_VLEN) {
494210767Srpaulo			terminate("function %s has too many args: %d > %d\n",
495280201Smarkj			    tdesc_name(tp), i, CTF_MAX_VLEN);
496210767Srpaulo		}
497210767Srpaulo
498210767Srpaulo		ctt.ctt_info = CTF_TYPE_INFO(CTF_K_FUNCTION, isroot, i);
499178481Sjb		ctt.ctt_type = tp->t_fndef->fn_ret->t_id;
500178481Sjb		write_unsized_type_rec(b, &ctt);
501178481Sjb
502178546Sjb		for (i = 0; i < (int) tp->t_fndef->fn_nargs; i++) {
503178481Sjb			id = tp->t_fndef->fn_args[i]->t_id;
504233407Sgonzo
505233407Sgonzo			if (target_requires_swap) {
506233407Sgonzo				SWAP_16(id);
507233407Sgonzo			}
508233407Sgonzo
509178481Sjb			ctf_buf_write(b, &id, sizeof (id));
510178481Sjb		}
511178481Sjb
512178481Sjb		if (tp->t_fndef->fn_vargs) {
513178481Sjb			id = 0;
514178481Sjb			ctf_buf_write(b, &id, sizeof (id));
515178481Sjb			i++;
516178481Sjb		}
517178481Sjb
518178481Sjb		if (i & 1) {
519178481Sjb			id = 0;
520178481Sjb			ctf_buf_write(b, &id, sizeof (id));
521178481Sjb		}
522178481Sjb		break;
523178481Sjb
524178481Sjb	case RESTRICT:
525178481Sjb		ctt.ctt_info = CTF_TYPE_INFO(CTF_K_RESTRICT, isroot, 0);
526178481Sjb		ctt.ctt_type = tp->t_tdesc->t_id;
527178481Sjb		write_unsized_type_rec(b, &ctt);
528178481Sjb		break;
529178481Sjb
530178481Sjb	default:
531178481Sjb		warning("Can't write unknown type %d\n", tp->t_type);
532178481Sjb	}
533178481Sjb
534178481Sjb	debug(3, "Wrote type %d %s\n", tp->t_id, tdesc_name(tp));
535178481Sjb
536178481Sjb	return (1);
537178481Sjb}
538178481Sjb
539178481Sjbtypedef struct resbuf {
540178481Sjb	caddr_t rb_base;
541178481Sjb	caddr_t rb_ptr;
542178481Sjb	size_t rb_size;
543178481Sjb	z_stream rb_zstr;
544178481Sjb} resbuf_t;
545178481Sjb
546178481Sjbstatic void
547178481Sjbrbzs_grow(resbuf_t *rb)
548178481Sjb{
549178481Sjb	off_t ptroff = (caddr_t)rb->rb_zstr.next_out - rb->rb_base;
550178481Sjb
551178481Sjb	rb->rb_size += RES_BUF_CHUNK_SIZE;
552178481Sjb	rb->rb_base = xrealloc(rb->rb_base, rb->rb_size);
553178481Sjb	rb->rb_ptr = rb->rb_base + ptroff;
554178481Sjb	rb->rb_zstr.next_out = (Bytef *)(rb->rb_ptr);
555178481Sjb	rb->rb_zstr.avail_out += RES_BUF_CHUNK_SIZE;
556178481Sjb}
557178481Sjb
558178481Sjbstatic void
559178481Sjbcompress_start(resbuf_t *rb)
560178481Sjb{
561178481Sjb	int rc;
562178481Sjb
563178481Sjb	rb->rb_zstr.zalloc = (alloc_func)0;
564178481Sjb	rb->rb_zstr.zfree = (free_func)0;
565178481Sjb	rb->rb_zstr.opaque = (voidpf)0;
566178481Sjb
567178481Sjb	if ((rc = deflateInit(&rb->rb_zstr, Z_BEST_COMPRESSION)) != Z_OK)
568178481Sjb		parseterminate("zlib start failed: %s", zError(rc));
569178481Sjb}
570178481Sjb
571178481Sjbstatic ssize_t
572178546Sjbcompress_buffer(void *buf, size_t n, void *data)
573178481Sjb{
574178481Sjb	resbuf_t *rb = (resbuf_t *)data;
575178481Sjb	int rc;
576178481Sjb
577178481Sjb	rb->rb_zstr.next_out = (Bytef *)rb->rb_ptr;
578178481Sjb	rb->rb_zstr.avail_out = rb->rb_size - (rb->rb_ptr - rb->rb_base);
579178546Sjb	rb->rb_zstr.next_in = buf;
580178481Sjb	rb->rb_zstr.avail_in = n;
581178481Sjb
582178481Sjb	while (rb->rb_zstr.avail_in) {
583178481Sjb		if (rb->rb_zstr.avail_out == 0)
584178481Sjb			rbzs_grow(rb);
585178481Sjb
586178481Sjb		if ((rc = deflate(&rb->rb_zstr, Z_NO_FLUSH)) != Z_OK)
587178481Sjb			parseterminate("zlib deflate failed: %s", zError(rc));
588178481Sjb	}
589178481Sjb	rb->rb_ptr = (caddr_t)rb->rb_zstr.next_out;
590178481Sjb
591178481Sjb	return (n);
592178481Sjb}
593178481Sjb
594178481Sjbstatic void
595178481Sjbcompress_flush(resbuf_t *rb, int type)
596178481Sjb{
597178481Sjb	int rc;
598178481Sjb
599178481Sjb	for (;;) {
600178481Sjb		if (rb->rb_zstr.avail_out == 0)
601178481Sjb			rbzs_grow(rb);
602178481Sjb
603178481Sjb		rc = deflate(&rb->rb_zstr, type);
604178481Sjb		if ((type == Z_FULL_FLUSH && rc == Z_BUF_ERROR) ||
605178481Sjb		    (type == Z_FINISH && rc == Z_STREAM_END))
606178481Sjb			break;
607178481Sjb		else if (rc != Z_OK)
608178481Sjb			parseterminate("zlib finish failed: %s", zError(rc));
609178481Sjb	}
610178481Sjb	rb->rb_ptr = (caddr_t)rb->rb_zstr.next_out;
611178481Sjb}
612178481Sjb
613178481Sjbstatic void
614178481Sjbcompress_end(resbuf_t *rb)
615178481Sjb{
616178481Sjb	int rc;
617178481Sjb
618178481Sjb	compress_flush(rb, Z_FINISH);
619178481Sjb
620178481Sjb	if ((rc = deflateEnd(&rb->rb_zstr)) != Z_OK)
621178481Sjb		parseterminate("zlib end failed: %s", zError(rc));
622178481Sjb}
623178481Sjb
624178481Sjb/*
625178481Sjb * Pad the buffer to a power-of-2 boundary
626178481Sjb */
627178481Sjbstatic void
628178481Sjbpad_buffer(ctf_buf_t *buf, int align)
629178481Sjb{
630178481Sjb	uint_t cur = ctf_buf_cur(buf);
631178481Sjb	ssize_t topad = (align - (cur % align)) % align;
632178481Sjb	static const char pad[8] = { 0 };
633178481Sjb
634178481Sjb	while (topad > 0) {
635178481Sjb		ctf_buf_write(buf, pad, (topad > 8 ? 8 : topad));
636178481Sjb		topad -= 8;
637178481Sjb	}
638178481Sjb}
639178481Sjb
640178481Sjbstatic ssize_t
641178546Sjbbcopy_data(void *buf, size_t n, void *data)
642178481Sjb{
643178481Sjb	caddr_t *posp = (caddr_t *)data;
644178481Sjb	bcopy(buf, *posp, n);
645178481Sjb	*posp += n;
646178481Sjb	return (n);
647178481Sjb}
648178481Sjb
649178481Sjbstatic caddr_t
650178481Sjbwrite_buffer(ctf_header_t *h, ctf_buf_t *buf, size_t *resszp)
651178481Sjb{
652178481Sjb	caddr_t outbuf;
653178481Sjb	caddr_t bufpos;
654178481Sjb
655178481Sjb	outbuf = xmalloc(sizeof (ctf_header_t) + (buf->ctb_ptr - buf->ctb_base)
656178481Sjb	    + buf->ctb_strtab.str_size);
657178481Sjb
658178481Sjb	bufpos = outbuf;
659178481Sjb	(void) bcopy_data(h, sizeof (ctf_header_t), &bufpos);
660178481Sjb	(void) bcopy_data(buf->ctb_base, buf->ctb_ptr - buf->ctb_base,
661178481Sjb	    &bufpos);
662178481Sjb	(void) strtab_write(&buf->ctb_strtab, bcopy_data, &bufpos);
663178481Sjb	*resszp = bufpos - outbuf;
664178481Sjb	return (outbuf);
665178481Sjb}
666178481Sjb
667178481Sjb/*
668178481Sjb * Create the compression buffer, and fill it with the CTF and string
669178481Sjb * table data.  We flush the compression state between the two so the
670178481Sjb * dictionary used for the string tables won't be polluted with values
671178481Sjb * that made sense for the CTF data.
672178481Sjb */
673178481Sjbstatic caddr_t
674178481Sjbwrite_compressed_buffer(ctf_header_t *h, ctf_buf_t *buf, size_t *resszp)
675178481Sjb{
676178481Sjb	resbuf_t resbuf;
677178481Sjb	resbuf.rb_size = RES_BUF_CHUNK_SIZE;
678178481Sjb	resbuf.rb_base = xmalloc(resbuf.rb_size);
679178481Sjb	bcopy(h, resbuf.rb_base, sizeof (ctf_header_t));
680178481Sjb	resbuf.rb_ptr = resbuf.rb_base + sizeof (ctf_header_t);
681178481Sjb
682178481Sjb	compress_start(&resbuf);
683178481Sjb	(void) compress_buffer(buf->ctb_base, buf->ctb_ptr - buf->ctb_base,
684178481Sjb	    &resbuf);
685178481Sjb	compress_flush(&resbuf, Z_FULL_FLUSH);
686178481Sjb	(void) strtab_write(&buf->ctb_strtab, compress_buffer, &resbuf);
687178481Sjb	compress_end(&resbuf);
688178481Sjb
689178481Sjb	*resszp = (resbuf.rb_ptr - resbuf.rb_base);
690178481Sjb	return (resbuf.rb_base);
691178481Sjb}
692178481Sjb
693178481Sjbcaddr_t
694178481Sjbctf_gen(iiburst_t *iiburst, size_t *resszp, int do_compress)
695178481Sjb{
696178481Sjb	ctf_buf_t *buf = ctf_buf_new();
697178481Sjb	ctf_header_t h;
698178481Sjb	caddr_t outbuf;
699178481Sjb
700178481Sjb	int i;
701178481Sjb
702233407Sgonzo	target_requires_swap = do_compress & CTF_SWAP_BYTES;
703233407Sgonzo	do_compress &= ~CTF_SWAP_BYTES;
704233407Sgonzo
705178481Sjb	/*
706178481Sjb	 * Prepare the header, and create the CTF output buffers.  The data
707178481Sjb	 * object section and function section are both lists of 2-byte
708178481Sjb	 * integers; we pad these out to the next 4-byte boundary if needed.
709178481Sjb	 */
710178481Sjb	h.cth_magic = CTF_MAGIC;
711178481Sjb	h.cth_version = CTF_VERSION;
712178481Sjb	h.cth_flags = do_compress ? CTF_F_COMPRESS : 0;
713178481Sjb	h.cth_parlabel = strtab_insert(&buf->ctb_strtab,
714178481Sjb	    iiburst->iib_td->td_parlabel);
715178481Sjb	h.cth_parname = strtab_insert(&buf->ctb_strtab,
716178481Sjb	    iiburst->iib_td->td_parname);
717178481Sjb
718178481Sjb	h.cth_lbloff = 0;
719178546Sjb	(void) list_iter(iiburst->iib_td->td_labels, write_label,
720178481Sjb	    buf);
721178481Sjb
722178481Sjb	pad_buffer(buf, 2);
723178481Sjb	h.cth_objtoff = ctf_buf_cur(buf);
724178481Sjb	for (i = 0; i < iiburst->iib_nobjts; i++)
725178481Sjb		write_objects(iiburst->iib_objts[i], buf);
726178481Sjb
727178481Sjb	pad_buffer(buf, 2);
728178481Sjb	h.cth_funcoff = ctf_buf_cur(buf);
729178481Sjb	for (i = 0; i < iiburst->iib_nfuncs; i++)
730178481Sjb		write_functions(iiburst->iib_funcs[i], buf);
731178481Sjb
732178481Sjb	pad_buffer(buf, 4);
733178481Sjb	h.cth_typeoff = ctf_buf_cur(buf);
734178546Sjb	(void) list_iter(iiburst->iib_types, write_type, buf);
735178481Sjb
736178481Sjb	debug(2, "CTF wrote %d types\n", list_count(iiburst->iib_types));
737178481Sjb
738178481Sjb	h.cth_stroff = ctf_buf_cur(buf);
739178481Sjb	h.cth_strlen = strtab_size(&buf->ctb_strtab);
740178481Sjb
741233407Sgonzo	if (target_requires_swap) {
742233407Sgonzo		SWAP_16(h.cth_preamble.ctp_magic);
743233407Sgonzo		SWAP_32(h.cth_parlabel);
744233407Sgonzo		SWAP_32(h.cth_parname);
745233407Sgonzo		SWAP_32(h.cth_lbloff);
746233407Sgonzo		SWAP_32(h.cth_objtoff);
747233407Sgonzo		SWAP_32(h.cth_funcoff);
748233407Sgonzo		SWAP_32(h.cth_typeoff);
749233407Sgonzo		SWAP_32(h.cth_stroff);
750233407Sgonzo		SWAP_32(h.cth_strlen);
751233407Sgonzo	}
752233407Sgonzo
753178481Sjb	/*
754178481Sjb	 * We only do compression for ctfmerge, as ctfconvert is only
755178481Sjb	 * supposed to be used on intermediary build objects. This is
756178481Sjb	 * significantly faster.
757178481Sjb	 */
758178481Sjb	if (do_compress)
759178481Sjb		outbuf = write_compressed_buffer(&h, buf, resszp);
760178481Sjb	else
761178481Sjb		outbuf = write_buffer(&h, buf, resszp);
762178481Sjb
763178481Sjb	ctf_buf_free(buf);
764178481Sjb	return (outbuf);
765178481Sjb}
766178481Sjb
767178546Sjbstatic void
768178481Sjbget_ctt_size(ctf_type_t *ctt, size_t *sizep, size_t *incrementp)
769178481Sjb{
770178481Sjb	if (ctt->ctt_size == CTF_LSIZE_SENT) {
771178481Sjb		*sizep = (size_t)CTF_TYPE_LSIZE(ctt);
772178481Sjb		*incrementp = sizeof (ctf_type_t);
773178481Sjb	} else {
774178481Sjb		*sizep = ctt->ctt_size;
775178481Sjb		*incrementp = sizeof (ctf_stype_t);
776178481Sjb	}
777178481Sjb}
778178481Sjb
779178481Sjbstatic int
780178481Sjbcount_types(ctf_header_t *h, caddr_t data)
781178481Sjb{
782178481Sjb	caddr_t dptr = data + h->cth_typeoff;
783178481Sjb	int count = 0;
784178481Sjb
785178481Sjb	dptr = data + h->cth_typeoff;
786178481Sjb	while (dptr < data + h->cth_stroff) {
787178546Sjb		void *v = (void *) dptr;
788178546Sjb		ctf_type_t *ctt = v;
789178481Sjb		size_t vlen = CTF_INFO_VLEN(ctt->ctt_info);
790178481Sjb		size_t size, increment;
791178481Sjb
792178481Sjb		get_ctt_size(ctt, &size, &increment);
793178481Sjb
794178481Sjb		switch (CTF_INFO_KIND(ctt->ctt_info)) {
795178481Sjb		case CTF_K_INTEGER:
796178481Sjb		case CTF_K_FLOAT:
797178481Sjb			dptr += 4;
798178481Sjb			break;
799178481Sjb		case CTF_K_POINTER:
800178481Sjb		case CTF_K_FORWARD:
801178481Sjb		case CTF_K_TYPEDEF:
802178481Sjb		case CTF_K_VOLATILE:
803178481Sjb		case CTF_K_CONST:
804178481Sjb		case CTF_K_RESTRICT:
805178481Sjb		case CTF_K_FUNCTION:
806178481Sjb			dptr += sizeof (ushort_t) * (vlen + (vlen & 1));
807178481Sjb			break;
808178481Sjb		case CTF_K_ARRAY:
809178481Sjb			dptr += sizeof (ctf_array_t);
810178481Sjb			break;
811178481Sjb		case CTF_K_STRUCT:
812178481Sjb		case CTF_K_UNION:
813178481Sjb			if (size < CTF_LSTRUCT_THRESH)
814178481Sjb				dptr += sizeof (ctf_member_t) * vlen;
815178481Sjb			else
816178481Sjb				dptr += sizeof (ctf_lmember_t) * vlen;
817178481Sjb			break;
818178481Sjb		case CTF_K_ENUM:
819178481Sjb			dptr += sizeof (ctf_enum_t) * vlen;
820178481Sjb			break;
821178481Sjb		case CTF_K_UNKNOWN:
822178481Sjb			break;
823178481Sjb		default:
824178481Sjb			parseterminate("Unknown CTF type %d (#%d) at %#x",
825178481Sjb			    CTF_INFO_KIND(ctt->ctt_info), count, dptr - data);
826178481Sjb		}
827178481Sjb
828178481Sjb		dptr += increment;
829178481Sjb		count++;
830178481Sjb	}
831178481Sjb
832178481Sjb	debug(3, "CTF read %d types\n", count);
833178481Sjb
834178481Sjb	return (count);
835178481Sjb}
836178481Sjb
837178481Sjb/*
838178481Sjb * Resurrect the labels stored in the CTF data, returning the index associated
839178481Sjb * with a label provided by the caller.  There are several cases, outlined
840178481Sjb * below.  Note that, given two labels, the one associated with the lesser type
841178481Sjb * index is considered to be older than the other.
842178481Sjb *
843178481Sjb *  1. matchlbl == NULL - return the index of the most recent label.
844178481Sjb *  2. matchlbl == "BASE" - return the index of the oldest label.
845178481Sjb *  3. matchlbl != NULL, but doesn't match any labels in the section - warn
846178481Sjb *	the user, and proceed as if matchlbl == "BASE" (for safety).
847178481Sjb *  4. matchlbl != NULL, and matches one of the labels in the section - return
848178481Sjb *	the type index associated with the label.
849178481Sjb */
850178481Sjbstatic int
851178481Sjbresurrect_labels(ctf_header_t *h, tdata_t *td, caddr_t ctfdata, char *matchlbl)
852178481Sjb{
853178481Sjb	caddr_t buf = ctfdata + h->cth_lbloff;
854178481Sjb	caddr_t sbuf = ctfdata + h->cth_stroff;
855178481Sjb	size_t bufsz = h->cth_objtoff - h->cth_lbloff;
856178481Sjb	int lastidx = 0, baseidx = -1;
857178546Sjb	char *baselabel = NULL;
858178481Sjb	ctf_lblent_t *ctl;
859178546Sjb	void *v = (void *) buf;
860178481Sjb
861178546Sjb	for (ctl = v; (caddr_t)ctl < buf + bufsz; ctl++) {
862178481Sjb		char *label = sbuf + ctl->ctl_label;
863178481Sjb
864178481Sjb		lastidx = ctl->ctl_typeidx;
865178481Sjb
866178481Sjb		debug(3, "Resurrected label %s type idx %d\n", label, lastidx);
867178481Sjb
868178481Sjb		tdata_label_add(td, label, lastidx);
869178481Sjb
870178481Sjb		if (baseidx == -1) {
871178481Sjb			baseidx = lastidx;
872178481Sjb			baselabel = label;
873178481Sjb			if (matchlbl != NULL && streq(matchlbl, "BASE"))
874178481Sjb				return (lastidx);
875178481Sjb		}
876178481Sjb
877178481Sjb		if (matchlbl != NULL && streq(label, matchlbl))
878178481Sjb			return (lastidx);
879178481Sjb	}
880178481Sjb
881178481Sjb	if (matchlbl != NULL) {
882178481Sjb		/* User provided a label that didn't match */
883178481Sjb		warning("%s: Cannot find label `%s' - using base (%s)\n",
884178481Sjb		    curfile, matchlbl, (baselabel ? baselabel : "NONE"));
885178481Sjb
886178481Sjb		tdata_label_free(td);
887178481Sjb		tdata_label_add(td, baselabel, baseidx);
888178481Sjb
889178481Sjb		return (baseidx);
890178481Sjb	}
891178481Sjb
892178481Sjb	return (lastidx);
893178481Sjb}
894178481Sjb
895178481Sjbstatic void
896178481Sjbresurrect_objects(ctf_header_t *h, tdata_t *td, tdesc_t **tdarr, int tdsize,
897178481Sjb    caddr_t ctfdata, symit_data_t *si)
898178481Sjb{
899178481Sjb	caddr_t buf = ctfdata + h->cth_objtoff;
900178481Sjb	size_t bufsz = h->cth_funcoff - h->cth_objtoff;
901178481Sjb	caddr_t dptr;
902178481Sjb
903178481Sjb	symit_reset(si);
904178481Sjb	for (dptr = buf; dptr < buf + bufsz; dptr += 2) {
905178546Sjb		void *v = (void *) dptr;
906178546Sjb		ushort_t id = *((ushort_t *)v);
907178481Sjb		iidesc_t *ii;
908178481Sjb		GElf_Sym *sym;
909178481Sjb
910178481Sjb		if (!(sym = symit_next(si, STT_OBJECT)) && id != 0) {
911178481Sjb			parseterminate(
912178481Sjb			    "Unexpected end of object symbols at %x of %x",
913178481Sjb			    dptr - buf, bufsz);
914178481Sjb		}
915178481Sjb
916178481Sjb		if (id == 0) {
917178481Sjb			debug(3, "Skipping null object\n");
918178481Sjb			continue;
919178481Sjb		} else if (id >= tdsize) {
920178481Sjb			parseterminate("Reference to invalid type %d", id);
921178481Sjb		}
922178481Sjb
923178481Sjb		ii = iidesc_new(symit_name(si));
924178481Sjb		ii->ii_dtype = tdarr[id];
925178481Sjb		if (GELF_ST_BIND(sym->st_info) == STB_LOCAL) {
926178481Sjb			ii->ii_type = II_SVAR;
927178481Sjb			ii->ii_owner = xstrdup(symit_curfile(si));
928178481Sjb		} else
929178481Sjb			ii->ii_type = II_GVAR;
930178481Sjb		hash_add(td->td_iihash, ii);
931178481Sjb
932178481Sjb		debug(3, "Resurrected %s object %s (%d) from %s\n",
933178481Sjb		    (ii->ii_type == II_GVAR ? "global" : "static"),
934178481Sjb		    ii->ii_name, id, (ii->ii_owner ? ii->ii_owner : "(none)"));
935178481Sjb	}
936178481Sjb}
937178481Sjb
938178481Sjbstatic void
939178481Sjbresurrect_functions(ctf_header_t *h, tdata_t *td, tdesc_t **tdarr, int tdsize,
940178481Sjb    caddr_t ctfdata, symit_data_t *si)
941178481Sjb{
942178481Sjb	caddr_t buf = ctfdata + h->cth_funcoff;
943178481Sjb	size_t bufsz = h->cth_typeoff - h->cth_funcoff;
944178481Sjb	caddr_t dptr = buf;
945178481Sjb	iidesc_t *ii;
946178481Sjb	ushort_t info;
947178481Sjb	ushort_t retid;
948178481Sjb	GElf_Sym *sym;
949178481Sjb	int i;
950178481Sjb
951178481Sjb	symit_reset(si);
952178481Sjb	while (dptr < buf + bufsz) {
953178546Sjb		void *v = (void *) dptr;
954178546Sjb		info = *((ushort_t *)v);
955178481Sjb		dptr += 2;
956178481Sjb
957178481Sjb		if (!(sym = symit_next(si, STT_FUNC)) && info != 0)
958178481Sjb			parseterminate("Unexpected end of function symbols");
959178481Sjb
960178481Sjb		if (info == 0) {
961178481Sjb			debug(3, "Skipping null function (%s)\n",
962178481Sjb			    symit_name(si));
963178481Sjb			continue;
964178481Sjb		}
965178481Sjb
966178546Sjb		v = (void *) dptr;
967178546Sjb		retid = *((ushort_t *)v);
968178481Sjb		dptr += 2;
969178481Sjb
970178481Sjb		if (retid >= tdsize)
971178481Sjb			parseterminate("Reference to invalid type %d", retid);
972178481Sjb
973178481Sjb		ii = iidesc_new(symit_name(si));
974178481Sjb		ii->ii_dtype = tdarr[retid];
975178481Sjb		if (GELF_ST_BIND(sym->st_info) == STB_LOCAL) {
976178481Sjb			ii->ii_type = II_SFUN;
977178481Sjb			ii->ii_owner = xstrdup(symit_curfile(si));
978178481Sjb		} else
979178481Sjb			ii->ii_type = II_GFUN;
980178481Sjb		ii->ii_nargs = CTF_INFO_VLEN(info);
981178481Sjb		if (ii->ii_nargs)
982178481Sjb			ii->ii_args =
983178481Sjb			    xmalloc(sizeof (tdesc_t *) * ii->ii_nargs);
984178481Sjb
985178481Sjb		for (i = 0; i < ii->ii_nargs; i++, dptr += 2) {
986178546Sjb			v = (void *) dptr;
987178546Sjb			ushort_t id = *((ushort_t *)v);
988178481Sjb			if (id >= tdsize)
989178481Sjb				parseterminate("Reference to invalid type %d",
990178481Sjb				    id);
991178481Sjb			ii->ii_args[i] = tdarr[id];
992178481Sjb		}
993178481Sjb
994178481Sjb		if (ii->ii_nargs && ii->ii_args[ii->ii_nargs - 1] == NULL) {
995178481Sjb			ii->ii_nargs--;
996178481Sjb			ii->ii_vargs = 1;
997178481Sjb		}
998178481Sjb
999178481Sjb		hash_add(td->td_iihash, ii);
1000178481Sjb
1001178481Sjb		debug(3, "Resurrected %s function %s (%d, %d args)\n",
1002178481Sjb		    (ii->ii_type == II_GFUN ? "global" : "static"),
1003178481Sjb		    ii->ii_name, retid, ii->ii_nargs);
1004178481Sjb	}
1005178481Sjb}
1006178481Sjb
1007178481Sjbstatic void
1008178481Sjbresurrect_types(ctf_header_t *h, tdata_t *td, tdesc_t **tdarr, int tdsize,
1009178481Sjb    caddr_t ctfdata, int maxid)
1010178481Sjb{
1011178481Sjb	caddr_t buf = ctfdata + h->cth_typeoff;
1012178481Sjb	size_t bufsz = h->cth_stroff - h->cth_typeoff;
1013178481Sjb	caddr_t sbuf = ctfdata + h->cth_stroff;
1014178481Sjb	caddr_t dptr = buf;
1015178481Sjb	tdesc_t *tdp;
1016178481Sjb	uint_t data;
1017178481Sjb	uint_t encoding;
1018178481Sjb	size_t size, increment;
1019178481Sjb	int tcnt;
1020178481Sjb	int iicnt = 0;
1021178481Sjb	tid_t tid, argid;
1022178481Sjb	int kind, vlen;
1023178481Sjb	int i;
1024178481Sjb
1025178481Sjb	elist_t **epp;
1026178481Sjb	mlist_t **mpp;
1027178481Sjb	intr_t *ip;
1028178481Sjb
1029178481Sjb	ctf_type_t *ctt;
1030178481Sjb	ctf_array_t *cta;
1031178481Sjb	ctf_enum_t *cte;
1032178481Sjb
1033178481Sjb	/*
1034178481Sjb	 * A maxid of zero indicates a request to resurrect all types, so reset
1035178481Sjb	 * maxid to the maximum type id.
1036178481Sjb	 */
1037178481Sjb	if (maxid == 0)
1038178481Sjb		maxid = CTF_MAX_TYPE;
1039178481Sjb
1040178481Sjb	for (dptr = buf, tcnt = 0, tid = 1; dptr < buf + bufsz; tcnt++, tid++) {
1041178481Sjb		if (tid > maxid)
1042178481Sjb			break;
1043178481Sjb
1044178481Sjb		if (tid >= tdsize)
1045178481Sjb			parseterminate("Reference to invalid type %d", tid);
1046178481Sjb
1047178546Sjb		void *v = (void *) dptr;
1048178546Sjb		ctt = v;
1049178481Sjb
1050178481Sjb		get_ctt_size(ctt, &size, &increment);
1051178481Sjb		dptr += increment;
1052178481Sjb
1053178481Sjb		tdp = tdarr[tid];
1054178481Sjb
1055178481Sjb		if (CTF_NAME_STID(ctt->ctt_name) != CTF_STRTAB_0)
1056178481Sjb			parseterminate(
1057210767Srpaulo			    "Unable to cope with non-zero strtab id");
1058178481Sjb		if (CTF_NAME_OFFSET(ctt->ctt_name) != 0) {
1059178481Sjb			tdp->t_name =
1060178481Sjb			    xstrdup(sbuf + CTF_NAME_OFFSET(ctt->ctt_name));
1061178481Sjb		} else
1062178481Sjb			tdp->t_name = NULL;
1063178481Sjb
1064178481Sjb		kind = CTF_INFO_KIND(ctt->ctt_info);
1065178481Sjb		vlen = CTF_INFO_VLEN(ctt->ctt_info);
1066178481Sjb
1067178481Sjb		switch (kind) {
1068178481Sjb		case CTF_K_INTEGER:
1069178481Sjb			tdp->t_type = INTRINSIC;
1070178481Sjb			tdp->t_size = size;
1071178481Sjb
1072178546Sjb			v = (void *) dptr;
1073178546Sjb			data = *((uint_t *)v);
1074178481Sjb			dptr += sizeof (uint_t);
1075178481Sjb			encoding = CTF_INT_ENCODING(data);
1076178481Sjb
1077178481Sjb			ip = xmalloc(sizeof (intr_t));
1078178481Sjb			ip->intr_type = INTR_INT;
1079178481Sjb			ip->intr_signed = (encoding & CTF_INT_SIGNED) ? 1 : 0;
1080178481Sjb
1081178481Sjb			if (encoding & CTF_INT_CHAR)
1082178481Sjb				ip->intr_iformat = 'c';
1083178481Sjb			else if (encoding & CTF_INT_BOOL)
1084178481Sjb				ip->intr_iformat = 'b';
1085178481Sjb			else if (encoding & CTF_INT_VARARGS)
1086178481Sjb				ip->intr_iformat = 'v';
1087178481Sjb			else
1088178481Sjb				ip->intr_iformat = '\0';
1089178481Sjb
1090178481Sjb			ip->intr_offset = CTF_INT_OFFSET(data);
1091178481Sjb			ip->intr_nbits = CTF_INT_BITS(data);
1092178481Sjb			tdp->t_intr = ip;
1093178481Sjb			break;
1094178481Sjb
1095178481Sjb		case CTF_K_FLOAT:
1096178481Sjb			tdp->t_type = INTRINSIC;
1097178481Sjb			tdp->t_size = size;
1098178481Sjb
1099178546Sjb			v = (void *) dptr;
1100178546Sjb			data = *((uint_t *)v);
1101178481Sjb			dptr += sizeof (uint_t);
1102178481Sjb
1103178481Sjb			ip = xcalloc(sizeof (intr_t));
1104178481Sjb			ip->intr_type = INTR_REAL;
1105178481Sjb			ip->intr_fformat = CTF_FP_ENCODING(data);
1106178481Sjb			ip->intr_offset = CTF_FP_OFFSET(data);
1107178481Sjb			ip->intr_nbits = CTF_FP_BITS(data);
1108178481Sjb			tdp->t_intr = ip;
1109178481Sjb			break;
1110178481Sjb
1111178481Sjb		case CTF_K_POINTER:
1112178481Sjb			tdp->t_type = POINTER;
1113178481Sjb			tdp->t_tdesc = tdarr[ctt->ctt_type];
1114178481Sjb			break;
1115178481Sjb
1116178481Sjb		case CTF_K_ARRAY:
1117178481Sjb			tdp->t_type = ARRAY;
1118178481Sjb			tdp->t_size = size;
1119178481Sjb
1120178546Sjb			v = (void *) dptr;
1121178546Sjb			cta = v;
1122178481Sjb			dptr += sizeof (ctf_array_t);
1123178481Sjb
1124178481Sjb			tdp->t_ardef = xmalloc(sizeof (ardef_t));
1125178481Sjb			tdp->t_ardef->ad_contents = tdarr[cta->cta_contents];
1126178481Sjb			tdp->t_ardef->ad_idxtype = tdarr[cta->cta_index];
1127178481Sjb			tdp->t_ardef->ad_nelems = cta->cta_nelems;
1128178481Sjb			break;
1129178481Sjb
1130178481Sjb		case CTF_K_STRUCT:
1131178481Sjb		case CTF_K_UNION:
1132178481Sjb			tdp->t_type = (kind == CTF_K_STRUCT ? STRUCT : UNION);
1133178481Sjb			tdp->t_size = size;
1134178481Sjb
1135178481Sjb			if (size < CTF_LSTRUCT_THRESH) {
1136178481Sjb				for (i = 0, mpp = &tdp->t_members; i < vlen;
1137178481Sjb				    i++, mpp = &((*mpp)->ml_next)) {
1138178546Sjb					v = (void *) dptr;
1139178546Sjb					ctf_member_t *ctm = v;
1140178481Sjb					dptr += sizeof (ctf_member_t);
1141178481Sjb
1142178481Sjb					*mpp = xmalloc(sizeof (mlist_t));
1143178481Sjb					(*mpp)->ml_name = xstrdup(sbuf +
1144178481Sjb					    ctm->ctm_name);
1145178481Sjb					(*mpp)->ml_type = tdarr[ctm->ctm_type];
1146178481Sjb					(*mpp)->ml_offset = ctm->ctm_offset;
1147178481Sjb					(*mpp)->ml_size = 0;
1148253661Spfg					if (ctm->ctm_type > ntypes) {
1149253661Spfg						parseterminate("Invalid member type ctm_type=%d",
1150253661Spfg						    ctm->ctm_type);
1151253661Spfg					}
1152178481Sjb				}
1153178481Sjb			} else {
1154178481Sjb				for (i = 0, mpp = &tdp->t_members; i < vlen;
1155178481Sjb				    i++, mpp = &((*mpp)->ml_next)) {
1156178546Sjb					v = (void *) dptr;
1157178546Sjb					ctf_lmember_t *ctlm = v;
1158178481Sjb					dptr += sizeof (ctf_lmember_t);
1159178481Sjb
1160178481Sjb					*mpp = xmalloc(sizeof (mlist_t));
1161178481Sjb					(*mpp)->ml_name = xstrdup(sbuf +
1162178481Sjb					    ctlm->ctlm_name);
1163178481Sjb					(*mpp)->ml_type =
1164178481Sjb					    tdarr[ctlm->ctlm_type];
1165178481Sjb					(*mpp)->ml_offset =
1166178481Sjb					    (int)CTF_LMEM_OFFSET(ctlm);
1167178481Sjb					(*mpp)->ml_size = 0;
1168253661Spfg					if (ctlm->ctlm_type > ntypes) {
1169253661Spfg						parseterminate("Invalid lmember type ctlm_type=%d",
1170253661Spfg						    ctlm->ctlm_type);
1171253661Spfg					}
1172178481Sjb				}
1173178481Sjb			}
1174178481Sjb
1175178481Sjb			*mpp = NULL;
1176178481Sjb			break;
1177178481Sjb
1178178481Sjb		case CTF_K_ENUM:
1179178481Sjb			tdp->t_type = ENUM;
1180178481Sjb			tdp->t_size = size;
1181178481Sjb
1182178481Sjb			for (i = 0, epp = &tdp->t_emem; i < vlen;
1183178481Sjb			    i++, epp = &((*epp)->el_next)) {
1184178546Sjb				v = (void *) dptr;
1185178546Sjb				cte = v;
1186178481Sjb				dptr += sizeof (ctf_enum_t);
1187178481Sjb
1188178481Sjb				*epp = xmalloc(sizeof (elist_t));
1189178481Sjb				(*epp)->el_name = xstrdup(sbuf + cte->cte_name);
1190178481Sjb				(*epp)->el_number = cte->cte_value;
1191178481Sjb			}
1192178481Sjb			*epp = NULL;
1193178481Sjb			break;
1194178481Sjb
1195178481Sjb		case CTF_K_FORWARD:
1196178481Sjb			tdp->t_type = FORWARD;
1197178481Sjb			list_add(&td->td_fwdlist, tdp);
1198178481Sjb			break;
1199178481Sjb
1200178481Sjb		case CTF_K_TYPEDEF:
1201178481Sjb			tdp->t_type = TYPEDEF;
1202178481Sjb			tdp->t_tdesc = tdarr[ctt->ctt_type];
1203178481Sjb			break;
1204178481Sjb
1205178481Sjb		case CTF_K_VOLATILE:
1206178481Sjb			tdp->t_type = VOLATILE;
1207178481Sjb			tdp->t_tdesc = tdarr[ctt->ctt_type];
1208178481Sjb			break;
1209178481Sjb
1210178481Sjb		case CTF_K_CONST:
1211178481Sjb			tdp->t_type = CONST;
1212178481Sjb			tdp->t_tdesc = tdarr[ctt->ctt_type];
1213178481Sjb			break;
1214178481Sjb
1215178481Sjb		case CTF_K_FUNCTION:
1216178481Sjb			tdp->t_type = FUNCTION;
1217178481Sjb			tdp->t_fndef = xcalloc(sizeof (fndef_t));
1218178481Sjb			tdp->t_fndef->fn_ret = tdarr[ctt->ctt_type];
1219178481Sjb
1220178546Sjb			v = (void *) (dptr + (sizeof (ushort_t) * (vlen - 1)));
1221178546Sjb			if (vlen > 0 && *(ushort_t *)v == 0)
1222178481Sjb				tdp->t_fndef->fn_vargs = 1;
1223178481Sjb
1224178481Sjb			tdp->t_fndef->fn_nargs = vlen - tdp->t_fndef->fn_vargs;
1225178481Sjb			tdp->t_fndef->fn_args = xcalloc(sizeof (tdesc_t) *
1226178481Sjb			    vlen - tdp->t_fndef->fn_vargs);
1227178481Sjb
1228178481Sjb			for (i = 0; i < vlen; i++) {
1229178546Sjb				v = (void *) dptr;
1230178546Sjb				argid = *(ushort_t *)v;
1231178481Sjb				dptr += sizeof (ushort_t);
1232178481Sjb
1233178481Sjb				if (argid != 0)
1234178481Sjb					tdp->t_fndef->fn_args[i] = tdarr[argid];
1235178481Sjb			}
1236178481Sjb
1237178481Sjb			if (vlen & 1)
1238178481Sjb				dptr += sizeof (ushort_t);
1239178481Sjb			break;
1240178481Sjb
1241178481Sjb		case CTF_K_RESTRICT:
1242178481Sjb			tdp->t_type = RESTRICT;
1243178481Sjb			tdp->t_tdesc = tdarr[ctt->ctt_type];
1244178481Sjb			break;
1245178481Sjb
1246178481Sjb		case CTF_K_UNKNOWN:
1247178481Sjb			break;
1248178481Sjb
1249178481Sjb		default:
1250178481Sjb			warning("Can't parse unknown CTF type %d\n", kind);
1251178481Sjb		}
1252178481Sjb
1253178481Sjb		if (CTF_INFO_ISROOT(ctt->ctt_info)) {
1254178481Sjb			iidesc_t *ii = iidesc_new(tdp->t_name);
1255178481Sjb			if (tdp->t_type == STRUCT || tdp->t_type == UNION ||
1256178481Sjb			    tdp->t_type == ENUM)
1257178481Sjb				ii->ii_type = II_SOU;
1258178481Sjb			else
1259178481Sjb				ii->ii_type = II_TYPE;
1260178481Sjb			ii->ii_dtype = tdp;
1261178481Sjb			hash_add(td->td_iihash, ii);
1262178481Sjb
1263178481Sjb			iicnt++;
1264178481Sjb		}
1265178481Sjb
1266178481Sjb		debug(3, "Resurrected %d %stype %s (%d)\n", tdp->t_type,
1267178481Sjb		    (CTF_INFO_ISROOT(ctt->ctt_info) ? "root " : ""),
1268178481Sjb		    tdesc_name(tdp), tdp->t_id);
1269178481Sjb	}
1270178481Sjb
1271178481Sjb	debug(3, "Resurrected %d types (%d were roots)\n", tcnt, iicnt);
1272178481Sjb}
1273178481Sjb
1274178481Sjb/*
1275178481Sjb * For lack of other inspiration, we're going to take the boring route.  We
1276178481Sjb * count the number of types.  This lets us malloc that many tdesc structs
1277178481Sjb * before we start filling them in.  This has the advantage of allowing us to
1278178481Sjb * avoid a merge-esque remap step.
1279178481Sjb */
1280178481Sjbstatic tdata_t *
1281178481Sjbctf_parse(ctf_header_t *h, caddr_t buf, symit_data_t *si, char *label)
1282178481Sjb{
1283178481Sjb	tdata_t *td = tdata_new();
1284178481Sjb	tdesc_t **tdarr;
1285178481Sjb	int idx, i;
1286178481Sjb
1287253661Spfg	ntypes = count_types(h, buf);
1288253661Spfg
1289178481Sjb	/* shudder */
1290178481Sjb	tdarr = xcalloc(sizeof (tdesc_t *) * (ntypes + 1));
1291178481Sjb	tdarr[0] = NULL;
1292178481Sjb	for (i = 1; i <= ntypes; i++) {
1293178481Sjb		tdarr[i] = xcalloc(sizeof (tdesc_t));
1294178481Sjb		tdarr[i]->t_id = i;
1295178481Sjb	}
1296178481Sjb
1297178481Sjb	td->td_parlabel = xstrdup(buf + h->cth_stroff + h->cth_parlabel);
1298178481Sjb
1299178481Sjb	/* we have the technology - we can rebuild them */
1300178481Sjb	idx = resurrect_labels(h, td, buf, label);
1301178481Sjb
1302178481Sjb	resurrect_objects(h, td, tdarr, ntypes + 1, buf, si);
1303178481Sjb	resurrect_functions(h, td, tdarr, ntypes + 1, buf, si);
1304178481Sjb	resurrect_types(h, td, tdarr, ntypes + 1, buf, idx);
1305178481Sjb
1306178481Sjb	free(tdarr);
1307178481Sjb
1308178481Sjb	td->td_nextid = ntypes + 1;
1309178481Sjb
1310178481Sjb	return (td);
1311178481Sjb}
1312178481Sjb
1313178481Sjbstatic size_t
1314178481Sjbdecompress_ctf(caddr_t cbuf, size_t cbufsz, caddr_t dbuf, size_t dbufsz)
1315178481Sjb{
1316178481Sjb	z_stream zstr;
1317178481Sjb	int rc;
1318178481Sjb
1319178481Sjb	zstr.zalloc = (alloc_func)0;
1320178481Sjb	zstr.zfree = (free_func)0;
1321178481Sjb	zstr.opaque = (voidpf)0;
1322178481Sjb
1323178481Sjb	zstr.next_in = (Bytef *)cbuf;
1324178481Sjb	zstr.avail_in = cbufsz;
1325178481Sjb	zstr.next_out = (Bytef *)dbuf;
1326178481Sjb	zstr.avail_out = dbufsz;
1327178481Sjb
1328178481Sjb	if ((rc = inflateInit(&zstr)) != Z_OK ||
1329178481Sjb	    (rc = inflate(&zstr, Z_NO_FLUSH)) != Z_STREAM_END ||
1330178481Sjb	    (rc = inflateEnd(&zstr)) != Z_OK) {
1331178481Sjb		warning("CTF decompress zlib error %s\n", zError(rc));
1332178546Sjb		return (0);
1333178481Sjb	}
1334178481Sjb
1335178481Sjb	debug(3, "reflated %lu bytes to %lu, pointer at %d\n",
1336178481Sjb	    zstr.total_in, zstr.total_out, (caddr_t)zstr.next_in - cbuf);
1337178481Sjb
1338178481Sjb	return (zstr.total_out);
1339178481Sjb}
1340178481Sjb
1341178481Sjb/*
1342178481Sjb * Reconstruct the type tree from a given buffer of CTF data.  Only the types
1343178481Sjb * up to the type associated with the provided label, inclusive, will be
1344178481Sjb * reconstructed.  If a NULL label is provided, all types will be reconstructed.
1345178481Sjb *
1346178481Sjb * This function won't work on files that have been uniquified.
1347178481Sjb */
1348178481Sjbtdata_t *
1349178481Sjbctf_load(char *file, caddr_t buf, size_t bufsz, symit_data_t *si, char *label)
1350178481Sjb{
1351178481Sjb	ctf_header_t *h;
1352178481Sjb	caddr_t ctfdata;
1353178481Sjb	size_t ctfdatasz;
1354178481Sjb	tdata_t *td;
1355178481Sjb
1356178481Sjb	curfile = file;
1357178481Sjb
1358178481Sjb	if (bufsz < sizeof (ctf_header_t))
1359178481Sjb		parseterminate("Corrupt CTF - short header");
1360178481Sjb
1361178546Sjb	void *v = (void *) buf;
1362178546Sjb	h = v;
1363178481Sjb	buf += sizeof (ctf_header_t);
1364178481Sjb	bufsz -= sizeof (ctf_header_t);
1365178481Sjb
1366178481Sjb	if (h->cth_magic != CTF_MAGIC)
1367178481Sjb		parseterminate("Corrupt CTF - bad magic 0x%x", h->cth_magic);
1368178481Sjb
1369178481Sjb	if (h->cth_version != CTF_VERSION)
1370178481Sjb		parseterminate("Unknown CTF version %d", h->cth_version);
1371178481Sjb
1372178481Sjb	ctfdatasz = h->cth_stroff + h->cth_strlen;
1373178481Sjb	if (h->cth_flags & CTF_F_COMPRESS) {
1374178481Sjb		size_t actual;
1375178481Sjb
1376178481Sjb		ctfdata = xmalloc(ctfdatasz);
1377178481Sjb		if ((actual = decompress_ctf(buf, bufsz, ctfdata, ctfdatasz)) !=
1378178481Sjb		    ctfdatasz) {
1379178481Sjb			parseterminate("Corrupt CTF - short decompression "
1380178481Sjb			    "(was %d, expecting %d)", actual, ctfdatasz);
1381178481Sjb		}
1382178481Sjb	} else {
1383178481Sjb		ctfdata = buf;
1384178481Sjb		ctfdatasz = bufsz;
1385178481Sjb	}
1386178481Sjb
1387178481Sjb	td = ctf_parse(h, ctfdata, si, label);
1388178481Sjb
1389178481Sjb	if (h->cth_flags & CTF_F_COMPRESS)
1390178481Sjb		free(ctfdata);
1391178481Sjb
1392178481Sjb	curfile = NULL;
1393178481Sjb
1394178481Sjb	return (td);
1395178481Sjb}
1396