10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 51882Sjohnlev * Common Development and Distribution License (the "License"). 61882Sjohnlev * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 210Sstevel@tonic-gate /* 221882Sjohnlev * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 230Sstevel@tonic-gate * Use is subject to license terms. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate 260Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 270Sstevel@tonic-gate 280Sstevel@tonic-gate /* 290Sstevel@tonic-gate * Create and parse buffers containing CTF data. 300Sstevel@tonic-gate */ 310Sstevel@tonic-gate 320Sstevel@tonic-gate #include <sys/types.h> 330Sstevel@tonic-gate #include <stdio.h> 340Sstevel@tonic-gate #include <stdlib.h> 350Sstevel@tonic-gate #include <strings.h> 360Sstevel@tonic-gate #include <ctype.h> 370Sstevel@tonic-gate #include <zlib.h> 380Sstevel@tonic-gate #include <elf.h> 390Sstevel@tonic-gate 400Sstevel@tonic-gate #include "ctf_headers.h" 410Sstevel@tonic-gate #include "ctftools.h" 420Sstevel@tonic-gate #include "strtab.h" 430Sstevel@tonic-gate #include "memory.h" 440Sstevel@tonic-gate 450Sstevel@tonic-gate /* 460Sstevel@tonic-gate * Name of the file currently being read, used to print error messages. We 470Sstevel@tonic-gate * assume that only one file will be read at a time, and thus make no attempt 480Sstevel@tonic-gate * to allow curfile to be used simultaneously by multiple threads. 490Sstevel@tonic-gate * 500Sstevel@tonic-gate * The value is only valid during a call to ctf_load. 510Sstevel@tonic-gate */ 520Sstevel@tonic-gate char *curfile; 530Sstevel@tonic-gate 540Sstevel@tonic-gate #define CTF_BUF_CHUNK_SIZE (64 * 1024) 550Sstevel@tonic-gate #define RES_BUF_CHUNK_SIZE (64 * 1024) 560Sstevel@tonic-gate 570Sstevel@tonic-gate struct ctf_buf { 580Sstevel@tonic-gate strtab_t ctb_strtab; /* string table */ 590Sstevel@tonic-gate caddr_t ctb_base; /* pointer to base of buffer */ 600Sstevel@tonic-gate caddr_t ctb_end; /* pointer to end of buffer */ 610Sstevel@tonic-gate caddr_t ctb_ptr; /* pointer to empty buffer space */ 620Sstevel@tonic-gate size_t ctb_size; /* size of buffer */ 630Sstevel@tonic-gate int nptent; /* number of processed types */ 640Sstevel@tonic-gate int ntholes; /* number of type holes */ 650Sstevel@tonic-gate }; 660Sstevel@tonic-gate 670Sstevel@tonic-gate /*PRINTFLIKE1*/ 680Sstevel@tonic-gate static void 690Sstevel@tonic-gate parseterminate(char *fmt, ...) 700Sstevel@tonic-gate { 710Sstevel@tonic-gate static char msgbuf[1024]; /* sigh */ 720Sstevel@tonic-gate va_list ap; 730Sstevel@tonic-gate 740Sstevel@tonic-gate va_start(ap, fmt); 750Sstevel@tonic-gate vsnprintf(msgbuf, sizeof (msgbuf), fmt, ap); 760Sstevel@tonic-gate va_end(ap); 770Sstevel@tonic-gate 780Sstevel@tonic-gate terminate("%s: %s\n", curfile, msgbuf); 790Sstevel@tonic-gate } 800Sstevel@tonic-gate 810Sstevel@tonic-gate void 820Sstevel@tonic-gate ctf_buf_grow(ctf_buf_t *b) 830Sstevel@tonic-gate { 840Sstevel@tonic-gate off_t ptroff = b->ctb_ptr - b->ctb_base; 850Sstevel@tonic-gate 860Sstevel@tonic-gate b->ctb_size += CTF_BUF_CHUNK_SIZE; 870Sstevel@tonic-gate b->ctb_base = xrealloc(b->ctb_base, b->ctb_size); 880Sstevel@tonic-gate b->ctb_end = b->ctb_base + b->ctb_size; 890Sstevel@tonic-gate b->ctb_ptr = b->ctb_base + ptroff; 900Sstevel@tonic-gate } 910Sstevel@tonic-gate 920Sstevel@tonic-gate ctf_buf_t * 930Sstevel@tonic-gate ctf_buf_new(void) 940Sstevel@tonic-gate { 950Sstevel@tonic-gate ctf_buf_t *b = xcalloc(sizeof (ctf_buf_t)); 960Sstevel@tonic-gate 970Sstevel@tonic-gate strtab_create(&b->ctb_strtab); 980Sstevel@tonic-gate ctf_buf_grow(b); 990Sstevel@tonic-gate 1000Sstevel@tonic-gate return (b); 1010Sstevel@tonic-gate } 1020Sstevel@tonic-gate 1030Sstevel@tonic-gate void 1040Sstevel@tonic-gate ctf_buf_free(ctf_buf_t *b) 1050Sstevel@tonic-gate { 1060Sstevel@tonic-gate strtab_destroy(&b->ctb_strtab); 1070Sstevel@tonic-gate free(b->ctb_base); 1080Sstevel@tonic-gate free(b); 1090Sstevel@tonic-gate } 1100Sstevel@tonic-gate 1110Sstevel@tonic-gate uint_t 1120Sstevel@tonic-gate ctf_buf_cur(ctf_buf_t *b) 1130Sstevel@tonic-gate { 1140Sstevel@tonic-gate return (b->ctb_ptr - b->ctb_base); 1150Sstevel@tonic-gate } 1160Sstevel@tonic-gate 1170Sstevel@tonic-gate void 1180Sstevel@tonic-gate ctf_buf_write(ctf_buf_t *b, const void *p, size_t n) 1190Sstevel@tonic-gate { 1200Sstevel@tonic-gate size_t len; 1210Sstevel@tonic-gate 1220Sstevel@tonic-gate while (n != 0) { 1230Sstevel@tonic-gate if (b->ctb_ptr == b->ctb_end) 1240Sstevel@tonic-gate ctf_buf_grow(b); 1250Sstevel@tonic-gate 1260Sstevel@tonic-gate len = MIN((size_t)(b->ctb_end - b->ctb_ptr), n); 1270Sstevel@tonic-gate bcopy(p, b->ctb_ptr, len); 1280Sstevel@tonic-gate b->ctb_ptr += len; 1290Sstevel@tonic-gate 1300Sstevel@tonic-gate p = (char *)p + len; 1310Sstevel@tonic-gate n -= len; 1320Sstevel@tonic-gate } 1330Sstevel@tonic-gate } 1340Sstevel@tonic-gate 1350Sstevel@tonic-gate static int 1360Sstevel@tonic-gate write_label(labelent_t *le, ctf_buf_t *b) 1370Sstevel@tonic-gate { 1380Sstevel@tonic-gate ctf_lblent_t ctl; 1390Sstevel@tonic-gate 1400Sstevel@tonic-gate ctl.ctl_label = strtab_insert(&b->ctb_strtab, le->le_name); 1410Sstevel@tonic-gate ctl.ctl_typeidx = le->le_idx; 1420Sstevel@tonic-gate 1430Sstevel@tonic-gate ctf_buf_write(b, &ctl, sizeof (ctl)); 1440Sstevel@tonic-gate 1450Sstevel@tonic-gate return (1); 1460Sstevel@tonic-gate } 1470Sstevel@tonic-gate 1480Sstevel@tonic-gate static void 1490Sstevel@tonic-gate write_objects(iidesc_t *idp, ctf_buf_t *b) 1500Sstevel@tonic-gate { 1510Sstevel@tonic-gate ushort_t id = (idp ? idp->ii_dtype->t_id : 0); 1520Sstevel@tonic-gate 1530Sstevel@tonic-gate ctf_buf_write(b, &id, sizeof (id)); 1540Sstevel@tonic-gate 1550Sstevel@tonic-gate debug(3, "Wrote object %s (%d)\n", (idp ? idp->ii_name : "(null)"), id); 1560Sstevel@tonic-gate } 1570Sstevel@tonic-gate 1580Sstevel@tonic-gate static void 1590Sstevel@tonic-gate write_functions(iidesc_t *idp, ctf_buf_t *b) 1600Sstevel@tonic-gate { 1610Sstevel@tonic-gate ushort_t fdata[2]; 1620Sstevel@tonic-gate ushort_t id; 1630Sstevel@tonic-gate int nargs; 1640Sstevel@tonic-gate int i; 1650Sstevel@tonic-gate 1660Sstevel@tonic-gate if (!idp) { 1670Sstevel@tonic-gate fdata[0] = 0; 1680Sstevel@tonic-gate ctf_buf_write(b, &fdata[0], sizeof (fdata[0])); 1690Sstevel@tonic-gate 1700Sstevel@tonic-gate debug(3, "Wrote function (null)\n"); 1710Sstevel@tonic-gate return; 1720Sstevel@tonic-gate } 1730Sstevel@tonic-gate 1740Sstevel@tonic-gate nargs = idp->ii_nargs + (idp->ii_vargs != 0); 1750Sstevel@tonic-gate fdata[0] = CTF_TYPE_INFO(CTF_K_FUNCTION, 1, nargs); 1760Sstevel@tonic-gate fdata[1] = idp->ii_dtype->t_id; 1770Sstevel@tonic-gate ctf_buf_write(b, fdata, sizeof (fdata)); 1780Sstevel@tonic-gate 1790Sstevel@tonic-gate for (i = 0; i < idp->ii_nargs; i++) { 1800Sstevel@tonic-gate id = idp->ii_args[i]->t_id; 1810Sstevel@tonic-gate ctf_buf_write(b, &id, sizeof (id)); 1820Sstevel@tonic-gate } 1830Sstevel@tonic-gate 1840Sstevel@tonic-gate if (idp->ii_vargs) { 1850Sstevel@tonic-gate id = 0; 1860Sstevel@tonic-gate ctf_buf_write(b, &id, sizeof (id)); 1870Sstevel@tonic-gate } 1880Sstevel@tonic-gate 1890Sstevel@tonic-gate debug(3, "Wrote function %s (%d args)\n", idp->ii_name, nargs); 1900Sstevel@tonic-gate } 1910Sstevel@tonic-gate 1920Sstevel@tonic-gate /* 1930Sstevel@tonic-gate * Depending on the size of the type being described, either a ctf_stype_t (for 1940Sstevel@tonic-gate * types with size < CTF_LSTRUCT_THRESH) or a ctf_type_t (all others) will be 1950Sstevel@tonic-gate * written. We isolate the determination here so the rest of the writer code 1960Sstevel@tonic-gate * doesn't need to care. 1970Sstevel@tonic-gate */ 1980Sstevel@tonic-gate static void 1990Sstevel@tonic-gate write_sized_type_rec(ctf_buf_t *b, ctf_type_t *ctt, size_t size) 2000Sstevel@tonic-gate { 2010Sstevel@tonic-gate if (size > CTF_MAX_SIZE) { 2020Sstevel@tonic-gate ctt->ctt_size = CTF_LSIZE_SENT; 2030Sstevel@tonic-gate ctt->ctt_lsizehi = CTF_SIZE_TO_LSIZE_HI(size); 2040Sstevel@tonic-gate ctt->ctt_lsizelo = CTF_SIZE_TO_LSIZE_LO(size); 2050Sstevel@tonic-gate ctf_buf_write(b, ctt, sizeof (*ctt)); 2060Sstevel@tonic-gate } else { 2070Sstevel@tonic-gate ctf_stype_t *cts = (ctf_stype_t *)ctt; 2080Sstevel@tonic-gate 2090Sstevel@tonic-gate cts->ctt_size = (ushort_t)size; 2100Sstevel@tonic-gate ctf_buf_write(b, cts, sizeof (*cts)); 2110Sstevel@tonic-gate } 2120Sstevel@tonic-gate } 2130Sstevel@tonic-gate 2140Sstevel@tonic-gate static void 2150Sstevel@tonic-gate write_unsized_type_rec(ctf_buf_t *b, ctf_type_t *ctt) 2160Sstevel@tonic-gate { 2170Sstevel@tonic-gate ctf_stype_t *cts = (ctf_stype_t *)ctt; 2180Sstevel@tonic-gate 2190Sstevel@tonic-gate ctf_buf_write(b, cts, sizeof (*cts)); 2200Sstevel@tonic-gate } 2210Sstevel@tonic-gate 2220Sstevel@tonic-gate static int 2230Sstevel@tonic-gate write_type(tdesc_t *tp, ctf_buf_t *b) 2240Sstevel@tonic-gate { 2250Sstevel@tonic-gate elist_t *ep; 2260Sstevel@tonic-gate mlist_t *mp; 2270Sstevel@tonic-gate intr_t *ip; 2280Sstevel@tonic-gate 2290Sstevel@tonic-gate size_t offset; 2300Sstevel@tonic-gate uint_t encoding; 2310Sstevel@tonic-gate uint_t data; 2320Sstevel@tonic-gate int isroot = tp->t_flags & TDESC_F_ISROOT; 2330Sstevel@tonic-gate int i; 2340Sstevel@tonic-gate 2350Sstevel@tonic-gate ctf_type_t ctt; 2360Sstevel@tonic-gate ctf_array_t cta; 2370Sstevel@tonic-gate ctf_member_t ctm; 2380Sstevel@tonic-gate ctf_lmember_t ctlm; 2390Sstevel@tonic-gate ctf_enum_t cte; 2400Sstevel@tonic-gate ushort_t id; 2410Sstevel@tonic-gate 2420Sstevel@tonic-gate ctlm.ctlm_pad = 0; 2430Sstevel@tonic-gate 2440Sstevel@tonic-gate /* 2450Sstevel@tonic-gate * There shouldn't be any holes in the type list (where a hole is 2460Sstevel@tonic-gate * defined as two consecutive tdescs without consecutive ids), but 2470Sstevel@tonic-gate * check for them just in case. If we do find holes, we need to make 2480Sstevel@tonic-gate * fake entries to fill the holes, or we won't be able to reconstruct 2490Sstevel@tonic-gate * the tree from the written data. 2500Sstevel@tonic-gate */ 2510Sstevel@tonic-gate if (++b->nptent < CTF_TYPE_TO_INDEX(tp->t_id)) { 2520Sstevel@tonic-gate debug(2, "genctf: type hole from %d < x < %d\n", 2530Sstevel@tonic-gate b->nptent - 1, CTF_TYPE_TO_INDEX(tp->t_id)); 2540Sstevel@tonic-gate 2550Sstevel@tonic-gate ctt.ctt_name = CTF_TYPE_NAME(CTF_STRTAB_0, 0); 2560Sstevel@tonic-gate ctt.ctt_info = CTF_TYPE_INFO(0, 0, 0); 2570Sstevel@tonic-gate while (b->nptent < CTF_TYPE_TO_INDEX(tp->t_id)) { 2580Sstevel@tonic-gate write_sized_type_rec(b, &ctt, 0); 2590Sstevel@tonic-gate b->nptent++; 2600Sstevel@tonic-gate } 2610Sstevel@tonic-gate } 2620Sstevel@tonic-gate 2630Sstevel@tonic-gate offset = strtab_insert(&b->ctb_strtab, tp->t_name); 2640Sstevel@tonic-gate ctt.ctt_name = CTF_TYPE_NAME(CTF_STRTAB_0, offset); 2650Sstevel@tonic-gate 2660Sstevel@tonic-gate switch (tp->t_type) { 2670Sstevel@tonic-gate case INTRINSIC: 2680Sstevel@tonic-gate ip = tp->t_intr; 2690Sstevel@tonic-gate if (ip->intr_type == INTR_INT) 2700Sstevel@tonic-gate ctt.ctt_info = CTF_TYPE_INFO(CTF_K_INTEGER, 2710Sstevel@tonic-gate isroot, 1); 2720Sstevel@tonic-gate else 2730Sstevel@tonic-gate ctt.ctt_info = CTF_TYPE_INFO(CTF_K_FLOAT, isroot, 1); 2740Sstevel@tonic-gate write_sized_type_rec(b, &ctt, tp->t_size); 2750Sstevel@tonic-gate 2760Sstevel@tonic-gate encoding = 0; 2770Sstevel@tonic-gate 2780Sstevel@tonic-gate if (ip->intr_type == INTR_INT) { 2790Sstevel@tonic-gate if (ip->intr_signed) 2800Sstevel@tonic-gate encoding |= CTF_INT_SIGNED; 2810Sstevel@tonic-gate if (ip->intr_iformat == 'c') 2820Sstevel@tonic-gate encoding |= CTF_INT_CHAR; 2830Sstevel@tonic-gate else if (ip->intr_iformat == 'b') 2840Sstevel@tonic-gate encoding |= CTF_INT_BOOL; 2850Sstevel@tonic-gate else if (ip->intr_iformat == 'v') 2860Sstevel@tonic-gate encoding |= CTF_INT_VARARGS; 2870Sstevel@tonic-gate } else 2880Sstevel@tonic-gate encoding = ip->intr_fformat; 2890Sstevel@tonic-gate 2900Sstevel@tonic-gate data = CTF_INT_DATA(encoding, ip->intr_offset, ip->intr_nbits); 2910Sstevel@tonic-gate ctf_buf_write(b, &data, sizeof (data)); 2920Sstevel@tonic-gate break; 2930Sstevel@tonic-gate 2940Sstevel@tonic-gate case POINTER: 2950Sstevel@tonic-gate ctt.ctt_info = CTF_TYPE_INFO(CTF_K_POINTER, isroot, 0); 2960Sstevel@tonic-gate ctt.ctt_type = tp->t_tdesc->t_id; 2970Sstevel@tonic-gate write_unsized_type_rec(b, &ctt); 2980Sstevel@tonic-gate break; 2990Sstevel@tonic-gate 3000Sstevel@tonic-gate case ARRAY: 3010Sstevel@tonic-gate ctt.ctt_info = CTF_TYPE_INFO(CTF_K_ARRAY, isroot, 1); 3020Sstevel@tonic-gate write_sized_type_rec(b, &ctt, tp->t_size); 3030Sstevel@tonic-gate 3040Sstevel@tonic-gate cta.cta_contents = tp->t_ardef->ad_contents->t_id; 3050Sstevel@tonic-gate cta.cta_index = tp->t_ardef->ad_idxtype->t_id; 3060Sstevel@tonic-gate cta.cta_nelems = tp->t_ardef->ad_nelems; 3070Sstevel@tonic-gate ctf_buf_write(b, &cta, sizeof (cta)); 3080Sstevel@tonic-gate break; 3090Sstevel@tonic-gate 3100Sstevel@tonic-gate case STRUCT: 3110Sstevel@tonic-gate case UNION: 3120Sstevel@tonic-gate for (i = 0, mp = tp->t_members; mp != NULL; mp = mp->ml_next) 3130Sstevel@tonic-gate i++; /* count up struct or union members */ 3140Sstevel@tonic-gate 3150Sstevel@tonic-gate if (tp->t_type == STRUCT) 3160Sstevel@tonic-gate ctt.ctt_info = CTF_TYPE_INFO(CTF_K_STRUCT, isroot, i); 3170Sstevel@tonic-gate else 3180Sstevel@tonic-gate ctt.ctt_info = CTF_TYPE_INFO(CTF_K_UNION, isroot, i); 3190Sstevel@tonic-gate 3200Sstevel@tonic-gate write_sized_type_rec(b, &ctt, tp->t_size); 3210Sstevel@tonic-gate 3220Sstevel@tonic-gate if (tp->t_size < CTF_LSTRUCT_THRESH) { 3230Sstevel@tonic-gate for (mp = tp->t_members; mp != NULL; mp = mp->ml_next) { 3240Sstevel@tonic-gate offset = strtab_insert(&b->ctb_strtab, 3250Sstevel@tonic-gate mp->ml_name); 3260Sstevel@tonic-gate 3270Sstevel@tonic-gate ctm.ctm_name = CTF_TYPE_NAME(CTF_STRTAB_0, 3280Sstevel@tonic-gate offset); 3290Sstevel@tonic-gate ctm.ctm_type = mp->ml_type->t_id; 3300Sstevel@tonic-gate ctm.ctm_offset = mp->ml_offset; 3310Sstevel@tonic-gate ctf_buf_write(b, &ctm, sizeof (ctm)); 3320Sstevel@tonic-gate } 3330Sstevel@tonic-gate } else { 3340Sstevel@tonic-gate for (mp = tp->t_members; mp != NULL; mp = mp->ml_next) { 3350Sstevel@tonic-gate offset = strtab_insert(&b->ctb_strtab, 3360Sstevel@tonic-gate mp->ml_name); 3370Sstevel@tonic-gate 3380Sstevel@tonic-gate ctlm.ctlm_name = CTF_TYPE_NAME(CTF_STRTAB_0, 3390Sstevel@tonic-gate offset); 3400Sstevel@tonic-gate ctlm.ctlm_type = mp->ml_type->t_id; 3410Sstevel@tonic-gate ctlm.ctlm_offsethi = 3420Sstevel@tonic-gate CTF_OFFSET_TO_LMEMHI(mp->ml_offset); 3430Sstevel@tonic-gate ctlm.ctlm_offsetlo = 3440Sstevel@tonic-gate CTF_OFFSET_TO_LMEMLO(mp->ml_offset); 3450Sstevel@tonic-gate ctf_buf_write(b, &ctlm, sizeof (ctlm)); 3460Sstevel@tonic-gate } 3470Sstevel@tonic-gate } 3480Sstevel@tonic-gate break; 3490Sstevel@tonic-gate 3500Sstevel@tonic-gate case ENUM: 3510Sstevel@tonic-gate for (i = 0, ep = tp->t_emem; ep != NULL; ep = ep->el_next) 3520Sstevel@tonic-gate i++; /* count up enum members */ 3530Sstevel@tonic-gate 3540Sstevel@tonic-gate ctt.ctt_info = CTF_TYPE_INFO(CTF_K_ENUM, isroot, i); 3550Sstevel@tonic-gate write_sized_type_rec(b, &ctt, tp->t_size); 3560Sstevel@tonic-gate 3570Sstevel@tonic-gate for (ep = tp->t_emem; ep != NULL; ep = ep->el_next) { 3580Sstevel@tonic-gate offset = strtab_insert(&b->ctb_strtab, ep->el_name); 3590Sstevel@tonic-gate cte.cte_name = CTF_TYPE_NAME(CTF_STRTAB_0, offset); 3600Sstevel@tonic-gate cte.cte_value = ep->el_number; 3610Sstevel@tonic-gate ctf_buf_write(b, &cte, sizeof (cte)); 3620Sstevel@tonic-gate } 3630Sstevel@tonic-gate break; 3640Sstevel@tonic-gate 3650Sstevel@tonic-gate case FORWARD: 3660Sstevel@tonic-gate ctt.ctt_info = CTF_TYPE_INFO(CTF_K_FORWARD, isroot, 0); 3670Sstevel@tonic-gate ctt.ctt_type = 0; 3680Sstevel@tonic-gate write_unsized_type_rec(b, &ctt); 3690Sstevel@tonic-gate break; 3700Sstevel@tonic-gate 3710Sstevel@tonic-gate case TYPEDEF: 3720Sstevel@tonic-gate ctt.ctt_info = CTF_TYPE_INFO(CTF_K_TYPEDEF, isroot, 0); 3730Sstevel@tonic-gate ctt.ctt_type = tp->t_tdesc->t_id; 3740Sstevel@tonic-gate write_unsized_type_rec(b, &ctt); 3750Sstevel@tonic-gate break; 3760Sstevel@tonic-gate 3770Sstevel@tonic-gate case VOLATILE: 3780Sstevel@tonic-gate ctt.ctt_info = CTF_TYPE_INFO(CTF_K_VOLATILE, isroot, 0); 3790Sstevel@tonic-gate ctt.ctt_type = tp->t_tdesc->t_id; 3800Sstevel@tonic-gate write_unsized_type_rec(b, &ctt); 3810Sstevel@tonic-gate break; 3820Sstevel@tonic-gate 3830Sstevel@tonic-gate case CONST: 3840Sstevel@tonic-gate ctt.ctt_info = CTF_TYPE_INFO(CTF_K_CONST, isroot, 0); 3850Sstevel@tonic-gate ctt.ctt_type = tp->t_tdesc->t_id; 3860Sstevel@tonic-gate write_unsized_type_rec(b, &ctt); 3870Sstevel@tonic-gate break; 3880Sstevel@tonic-gate 3890Sstevel@tonic-gate case FUNCTION: 3900Sstevel@tonic-gate ctt.ctt_info = CTF_TYPE_INFO(CTF_K_FUNCTION, isroot, 3910Sstevel@tonic-gate tp->t_fndef->fn_nargs + tp->t_fndef->fn_vargs); 3920Sstevel@tonic-gate ctt.ctt_type = tp->t_fndef->fn_ret->t_id; 3930Sstevel@tonic-gate write_unsized_type_rec(b, &ctt); 3940Sstevel@tonic-gate 3950Sstevel@tonic-gate for (i = 0; i < tp->t_fndef->fn_nargs; i++) { 3960Sstevel@tonic-gate id = tp->t_fndef->fn_args[i]->t_id; 3970Sstevel@tonic-gate ctf_buf_write(b, &id, sizeof (id)); 3980Sstevel@tonic-gate } 3990Sstevel@tonic-gate 4000Sstevel@tonic-gate if (tp->t_fndef->fn_vargs) { 4010Sstevel@tonic-gate id = 0; 4020Sstevel@tonic-gate ctf_buf_write(b, &id, sizeof (id)); 4030Sstevel@tonic-gate i++; 4040Sstevel@tonic-gate } 4050Sstevel@tonic-gate 4060Sstevel@tonic-gate if (i & 1) { 4070Sstevel@tonic-gate id = 0; 4080Sstevel@tonic-gate ctf_buf_write(b, &id, sizeof (id)); 4090Sstevel@tonic-gate } 4100Sstevel@tonic-gate break; 4110Sstevel@tonic-gate 4120Sstevel@tonic-gate case RESTRICT: 4130Sstevel@tonic-gate ctt.ctt_info = CTF_TYPE_INFO(CTF_K_RESTRICT, isroot, 0); 4140Sstevel@tonic-gate ctt.ctt_type = tp->t_tdesc->t_id; 4150Sstevel@tonic-gate write_unsized_type_rec(b, &ctt); 4160Sstevel@tonic-gate break; 4170Sstevel@tonic-gate 4180Sstevel@tonic-gate default: 4190Sstevel@tonic-gate warning("Can't write unknown type %d\n", tp->t_type); 4200Sstevel@tonic-gate } 4210Sstevel@tonic-gate 4221882Sjohnlev debug(3, "Wrote type %d %s\n", tp->t_id, tdesc_name(tp)); 4230Sstevel@tonic-gate 4240Sstevel@tonic-gate return (1); 4250Sstevel@tonic-gate } 4260Sstevel@tonic-gate 4270Sstevel@tonic-gate typedef struct resbuf { 4280Sstevel@tonic-gate caddr_t rb_base; 4290Sstevel@tonic-gate caddr_t rb_ptr; 4300Sstevel@tonic-gate size_t rb_size; 4310Sstevel@tonic-gate z_stream rb_zstr; 4320Sstevel@tonic-gate } resbuf_t; 4330Sstevel@tonic-gate 4340Sstevel@tonic-gate static void 4350Sstevel@tonic-gate rbzs_grow(resbuf_t *rb) 4360Sstevel@tonic-gate { 4370Sstevel@tonic-gate off_t ptroff = (caddr_t)rb->rb_zstr.next_out - rb->rb_base; 4380Sstevel@tonic-gate 4390Sstevel@tonic-gate rb->rb_size += RES_BUF_CHUNK_SIZE; 4400Sstevel@tonic-gate rb->rb_base = xrealloc(rb->rb_base, rb->rb_size); 4410Sstevel@tonic-gate rb->rb_ptr = rb->rb_base + ptroff; 4420Sstevel@tonic-gate rb->rb_zstr.next_out = (Bytef *)(rb->rb_ptr); 4430Sstevel@tonic-gate rb->rb_zstr.avail_out += RES_BUF_CHUNK_SIZE; 4440Sstevel@tonic-gate } 4450Sstevel@tonic-gate 4460Sstevel@tonic-gate static void 4470Sstevel@tonic-gate compress_start(resbuf_t *rb) 4480Sstevel@tonic-gate { 4490Sstevel@tonic-gate int rc; 4500Sstevel@tonic-gate 4510Sstevel@tonic-gate rb->rb_zstr.zalloc = (alloc_func)0; 4520Sstevel@tonic-gate rb->rb_zstr.zfree = (free_func)0; 4530Sstevel@tonic-gate rb->rb_zstr.opaque = (voidpf)0; 4540Sstevel@tonic-gate 4550Sstevel@tonic-gate if ((rc = deflateInit(&rb->rb_zstr, Z_BEST_COMPRESSION)) != Z_OK) 4560Sstevel@tonic-gate parseterminate("zlib start failed: %s", zError(rc)); 4570Sstevel@tonic-gate } 4580Sstevel@tonic-gate 4591882Sjohnlev static ssize_t 4601882Sjohnlev compress_buffer(const void *buf, size_t n, void *data) 4610Sstevel@tonic-gate { 4621882Sjohnlev resbuf_t *rb = (resbuf_t *)data; 4630Sstevel@tonic-gate int rc; 4640Sstevel@tonic-gate 4650Sstevel@tonic-gate rb->rb_zstr.next_out = (Bytef *)rb->rb_ptr; 4660Sstevel@tonic-gate rb->rb_zstr.avail_out = rb->rb_size - (rb->rb_ptr - rb->rb_base); 4670Sstevel@tonic-gate rb->rb_zstr.next_in = (Bytef *)buf; 4680Sstevel@tonic-gate rb->rb_zstr.avail_in = n; 4690Sstevel@tonic-gate 4700Sstevel@tonic-gate while (rb->rb_zstr.avail_in) { 4710Sstevel@tonic-gate if (rb->rb_zstr.avail_out == 0) 4720Sstevel@tonic-gate rbzs_grow(rb); 4730Sstevel@tonic-gate 4740Sstevel@tonic-gate if ((rc = deflate(&rb->rb_zstr, Z_NO_FLUSH)) != Z_OK) 4750Sstevel@tonic-gate parseterminate("zlib deflate failed: %s", zError(rc)); 4760Sstevel@tonic-gate } 4770Sstevel@tonic-gate rb->rb_ptr = (caddr_t)rb->rb_zstr.next_out; 4781882Sjohnlev 4791882Sjohnlev return (n); 4800Sstevel@tonic-gate } 4810Sstevel@tonic-gate 4820Sstevel@tonic-gate static void 4830Sstevel@tonic-gate compress_flush(resbuf_t *rb, int type) 4840Sstevel@tonic-gate { 4850Sstevel@tonic-gate int rc; 4860Sstevel@tonic-gate 4870Sstevel@tonic-gate for (;;) { 4880Sstevel@tonic-gate if (rb->rb_zstr.avail_out == 0) 4890Sstevel@tonic-gate rbzs_grow(rb); 4900Sstevel@tonic-gate 4910Sstevel@tonic-gate rc = deflate(&rb->rb_zstr, type); 4920Sstevel@tonic-gate if ((type == Z_FULL_FLUSH && rc == Z_BUF_ERROR) || 4930Sstevel@tonic-gate (type == Z_FINISH && rc == Z_STREAM_END)) 4940Sstevel@tonic-gate break; 4950Sstevel@tonic-gate else if (rc != Z_OK) 4960Sstevel@tonic-gate parseterminate("zlib finish failed: %s", zError(rc)); 4970Sstevel@tonic-gate } 4980Sstevel@tonic-gate rb->rb_ptr = (caddr_t)rb->rb_zstr.next_out; 4990Sstevel@tonic-gate } 5000Sstevel@tonic-gate 5010Sstevel@tonic-gate static void 5020Sstevel@tonic-gate compress_end(resbuf_t *rb) 5030Sstevel@tonic-gate { 5040Sstevel@tonic-gate int rc; 5050Sstevel@tonic-gate 5060Sstevel@tonic-gate compress_flush(rb, Z_FINISH); 5070Sstevel@tonic-gate 5080Sstevel@tonic-gate if ((rc = deflateEnd(&rb->rb_zstr)) != Z_OK) 5090Sstevel@tonic-gate parseterminate("zlib end failed: %s", zError(rc)); 5100Sstevel@tonic-gate } 5110Sstevel@tonic-gate 5120Sstevel@tonic-gate /* 5130Sstevel@tonic-gate * Pad the buffer to a power-of-2 boundary 5140Sstevel@tonic-gate */ 5150Sstevel@tonic-gate static void 5160Sstevel@tonic-gate pad_buffer(ctf_buf_t *buf, int align) 5170Sstevel@tonic-gate { 5180Sstevel@tonic-gate uint_t cur = ctf_buf_cur(buf); 5190Sstevel@tonic-gate ssize_t topad = (align - (cur % align)) % align; 5200Sstevel@tonic-gate static const char pad[8] = { 0 }; 5210Sstevel@tonic-gate 5220Sstevel@tonic-gate while (topad > 0) { 5230Sstevel@tonic-gate ctf_buf_write(buf, pad, (topad > 8 ? 8 : topad)); 5240Sstevel@tonic-gate topad -= 8; 5250Sstevel@tonic-gate } 5260Sstevel@tonic-gate } 5270Sstevel@tonic-gate 5281882Sjohnlev static ssize_t 5291882Sjohnlev bcopy_data(const void *buf, size_t n, void *data) 5300Sstevel@tonic-gate { 5311882Sjohnlev caddr_t *posp = (caddr_t *)data; 5320Sstevel@tonic-gate bcopy(buf, *posp, n); 5330Sstevel@tonic-gate *posp += n; 5341882Sjohnlev return (n); 5350Sstevel@tonic-gate } 5360Sstevel@tonic-gate 5370Sstevel@tonic-gate static caddr_t 5380Sstevel@tonic-gate write_buffer(ctf_header_t *h, ctf_buf_t *buf, size_t *resszp) 5390Sstevel@tonic-gate { 5400Sstevel@tonic-gate caddr_t outbuf; 5410Sstevel@tonic-gate caddr_t bufpos; 5420Sstevel@tonic-gate 5430Sstevel@tonic-gate outbuf = xmalloc(sizeof (ctf_header_t) + (buf->ctb_ptr - buf->ctb_base) 5440Sstevel@tonic-gate + buf->ctb_strtab.str_size); 5450Sstevel@tonic-gate 5460Sstevel@tonic-gate bufpos = outbuf; 5471882Sjohnlev (void) bcopy_data(h, sizeof (ctf_header_t), &bufpos); 5481882Sjohnlev (void) bcopy_data(buf->ctb_base, buf->ctb_ptr - buf->ctb_base, 5490Sstevel@tonic-gate &bufpos); 550*1951Sjohnlev (void) strtab_write(&buf->ctb_strtab, bcopy_data, &bufpos); 5510Sstevel@tonic-gate *resszp = bufpos - outbuf; 5520Sstevel@tonic-gate return (outbuf); 5530Sstevel@tonic-gate } 5540Sstevel@tonic-gate 5550Sstevel@tonic-gate /* 5560Sstevel@tonic-gate * Create the compression buffer, and fill it with the CTF and string 5570Sstevel@tonic-gate * table data. We flush the compression state between the two so the 5580Sstevel@tonic-gate * dictionary used for the string tables won't be polluted with values 5590Sstevel@tonic-gate * that made sense for the CTF data. 5600Sstevel@tonic-gate */ 5610Sstevel@tonic-gate static caddr_t 5620Sstevel@tonic-gate write_compressed_buffer(ctf_header_t *h, ctf_buf_t *buf, size_t *resszp) 5630Sstevel@tonic-gate { 5640Sstevel@tonic-gate resbuf_t resbuf; 5650Sstevel@tonic-gate resbuf.rb_size = RES_BUF_CHUNK_SIZE; 5660Sstevel@tonic-gate resbuf.rb_base = xmalloc(resbuf.rb_size); 5670Sstevel@tonic-gate bcopy(h, resbuf.rb_base, sizeof (ctf_header_t)); 5680Sstevel@tonic-gate resbuf.rb_ptr = resbuf.rb_base + sizeof (ctf_header_t); 5690Sstevel@tonic-gate 5700Sstevel@tonic-gate compress_start(&resbuf); 5711882Sjohnlev (void) compress_buffer(buf->ctb_base, buf->ctb_ptr - buf->ctb_base, 5721882Sjohnlev &resbuf); 5730Sstevel@tonic-gate compress_flush(&resbuf, Z_FULL_FLUSH); 574*1951Sjohnlev (void) strtab_write(&buf->ctb_strtab, compress_buffer, &resbuf); 5750Sstevel@tonic-gate compress_end(&resbuf); 5760Sstevel@tonic-gate 5770Sstevel@tonic-gate *resszp = (resbuf.rb_ptr - resbuf.rb_base); 5780Sstevel@tonic-gate return (resbuf.rb_base); 5790Sstevel@tonic-gate } 5800Sstevel@tonic-gate 5810Sstevel@tonic-gate caddr_t 5820Sstevel@tonic-gate ctf_gen(iiburst_t *iiburst, size_t *resszp, int do_compress) 5830Sstevel@tonic-gate { 5840Sstevel@tonic-gate ctf_buf_t *buf = ctf_buf_new(); 5850Sstevel@tonic-gate ctf_header_t h; 5860Sstevel@tonic-gate caddr_t outbuf; 5870Sstevel@tonic-gate 5880Sstevel@tonic-gate int i; 5890Sstevel@tonic-gate 5900Sstevel@tonic-gate /* 5910Sstevel@tonic-gate * Prepare the header, and create the CTF output buffers. The data 5920Sstevel@tonic-gate * object section and function section are both lists of 2-byte 5930Sstevel@tonic-gate * integers; we pad these out to the next 4-byte boundary if needed. 5940Sstevel@tonic-gate */ 5950Sstevel@tonic-gate h.cth_magic = CTF_MAGIC; 5960Sstevel@tonic-gate h.cth_version = CTF_VERSION; 5970Sstevel@tonic-gate h.cth_flags = do_compress ? CTF_F_COMPRESS : 0; 5980Sstevel@tonic-gate h.cth_parlabel = strtab_insert(&buf->ctb_strtab, 5990Sstevel@tonic-gate iiburst->iib_td->td_parlabel); 6000Sstevel@tonic-gate h.cth_parname = strtab_insert(&buf->ctb_strtab, 6010Sstevel@tonic-gate iiburst->iib_td->td_parname); 6020Sstevel@tonic-gate 6030Sstevel@tonic-gate h.cth_lbloff = 0; 6040Sstevel@tonic-gate (void) list_iter(iiburst->iib_td->td_labels, (int (*)())write_label, 6050Sstevel@tonic-gate buf); 6060Sstevel@tonic-gate 6070Sstevel@tonic-gate pad_buffer(buf, 2); 6080Sstevel@tonic-gate h.cth_objtoff = ctf_buf_cur(buf); 6090Sstevel@tonic-gate for (i = 0; i < iiburst->iib_nobjts; i++) 6100Sstevel@tonic-gate write_objects(iiburst->iib_objts[i], buf); 6110Sstevel@tonic-gate 6120Sstevel@tonic-gate pad_buffer(buf, 2); 6130Sstevel@tonic-gate h.cth_funcoff = ctf_buf_cur(buf); 6140Sstevel@tonic-gate for (i = 0; i < iiburst->iib_nfuncs; i++) 6150Sstevel@tonic-gate write_functions(iiburst->iib_funcs[i], buf); 6160Sstevel@tonic-gate 6170Sstevel@tonic-gate pad_buffer(buf, 4); 6180Sstevel@tonic-gate h.cth_typeoff = ctf_buf_cur(buf); 6190Sstevel@tonic-gate (void) list_iter(iiburst->iib_types, (int (*)())write_type, buf); 6200Sstevel@tonic-gate 6210Sstevel@tonic-gate debug(2, "CTF wrote %d types\n", list_count(iiburst->iib_types)); 6220Sstevel@tonic-gate 6230Sstevel@tonic-gate h.cth_stroff = ctf_buf_cur(buf); 6240Sstevel@tonic-gate h.cth_strlen = strtab_size(&buf->ctb_strtab); 6250Sstevel@tonic-gate 6260Sstevel@tonic-gate /* 6270Sstevel@tonic-gate * We only do compression for ctfmerge, as ctfconvert is only 6280Sstevel@tonic-gate * supposed to be used on intermediary build objects. This is 6290Sstevel@tonic-gate * significantly faster. 6300Sstevel@tonic-gate */ 6310Sstevel@tonic-gate if (do_compress) 6320Sstevel@tonic-gate outbuf = write_compressed_buffer(&h, buf, resszp); 6330Sstevel@tonic-gate else 6340Sstevel@tonic-gate outbuf = write_buffer(&h, buf, resszp); 6350Sstevel@tonic-gate 6360Sstevel@tonic-gate ctf_buf_free(buf); 6370Sstevel@tonic-gate return (outbuf); 6380Sstevel@tonic-gate } 6390Sstevel@tonic-gate 6400Sstevel@tonic-gate void 6410Sstevel@tonic-gate get_ctt_size(ctf_type_t *ctt, size_t *sizep, size_t *incrementp) 6420Sstevel@tonic-gate { 6430Sstevel@tonic-gate if (ctt->ctt_size == CTF_LSIZE_SENT) { 6440Sstevel@tonic-gate *sizep = (size_t)CTF_TYPE_LSIZE(ctt); 6450Sstevel@tonic-gate *incrementp = sizeof (ctf_type_t); 6460Sstevel@tonic-gate } else { 6470Sstevel@tonic-gate *sizep = ctt->ctt_size; 6480Sstevel@tonic-gate *incrementp = sizeof (ctf_stype_t); 6490Sstevel@tonic-gate } 6500Sstevel@tonic-gate } 6510Sstevel@tonic-gate 6520Sstevel@tonic-gate static int 6530Sstevel@tonic-gate count_types(ctf_header_t *h, caddr_t data) 6540Sstevel@tonic-gate { 6550Sstevel@tonic-gate caddr_t dptr = data + h->cth_typeoff; 6560Sstevel@tonic-gate int count = 0; 6570Sstevel@tonic-gate 6580Sstevel@tonic-gate dptr = data + h->cth_typeoff; 6590Sstevel@tonic-gate while (dptr < data + h->cth_stroff) { 6600Sstevel@tonic-gate /* LINTED - pointer alignment */ 6610Sstevel@tonic-gate ctf_type_t *ctt = (ctf_type_t *)dptr; 6620Sstevel@tonic-gate size_t vlen = CTF_INFO_VLEN(ctt->ctt_info); 6630Sstevel@tonic-gate size_t size, increment; 6640Sstevel@tonic-gate 6650Sstevel@tonic-gate get_ctt_size(ctt, &size, &increment); 6660Sstevel@tonic-gate 6670Sstevel@tonic-gate switch (CTF_INFO_KIND(ctt->ctt_info)) { 6680Sstevel@tonic-gate case CTF_K_INTEGER: 6690Sstevel@tonic-gate case CTF_K_FLOAT: 6700Sstevel@tonic-gate dptr += 4; 6710Sstevel@tonic-gate break; 6720Sstevel@tonic-gate case CTF_K_POINTER: 6730Sstevel@tonic-gate case CTF_K_FORWARD: 6740Sstevel@tonic-gate case CTF_K_TYPEDEF: 6750Sstevel@tonic-gate case CTF_K_VOLATILE: 6760Sstevel@tonic-gate case CTF_K_CONST: 6770Sstevel@tonic-gate case CTF_K_RESTRICT: 6780Sstevel@tonic-gate case CTF_K_FUNCTION: 6790Sstevel@tonic-gate dptr += sizeof (ushort_t) * (vlen + (vlen & 1)); 6800Sstevel@tonic-gate break; 6810Sstevel@tonic-gate case CTF_K_ARRAY: 6820Sstevel@tonic-gate dptr += sizeof (ctf_array_t); 6830Sstevel@tonic-gate break; 6840Sstevel@tonic-gate case CTF_K_STRUCT: 6850Sstevel@tonic-gate case CTF_K_UNION: 6860Sstevel@tonic-gate if (size < CTF_LSTRUCT_THRESH) 6870Sstevel@tonic-gate dptr += sizeof (ctf_member_t) * vlen; 6880Sstevel@tonic-gate else 6890Sstevel@tonic-gate dptr += sizeof (ctf_lmember_t) * vlen; 6900Sstevel@tonic-gate break; 6910Sstevel@tonic-gate case CTF_K_ENUM: 6920Sstevel@tonic-gate dptr += sizeof (ctf_enum_t) * vlen; 6930Sstevel@tonic-gate break; 6940Sstevel@tonic-gate case CTF_K_UNKNOWN: 6950Sstevel@tonic-gate break; 6960Sstevel@tonic-gate default: 6970Sstevel@tonic-gate parseterminate("Unknown CTF type %d (#%d) at %#x", 6980Sstevel@tonic-gate CTF_INFO_KIND(ctt->ctt_info), count, dptr - data); 6990Sstevel@tonic-gate } 7000Sstevel@tonic-gate 7010Sstevel@tonic-gate dptr += increment; 7020Sstevel@tonic-gate count++; 7030Sstevel@tonic-gate } 7040Sstevel@tonic-gate 7050Sstevel@tonic-gate debug(3, "CTF read %d types\n", count); 7060Sstevel@tonic-gate 7070Sstevel@tonic-gate return (count); 7080Sstevel@tonic-gate } 7090Sstevel@tonic-gate 7100Sstevel@tonic-gate /* 7110Sstevel@tonic-gate * Resurrect the labels stored in the CTF data, returning the index associated 7120Sstevel@tonic-gate * with a label provided by the caller. There are several cases, outlined 7130Sstevel@tonic-gate * below. Note that, given two labels, the one associated with the lesser type 7140Sstevel@tonic-gate * index is considered to be older than the other. 7150Sstevel@tonic-gate * 7160Sstevel@tonic-gate * 1. matchlbl == NULL - return the index of the most recent label. 7170Sstevel@tonic-gate * 2. matchlbl == "BASE" - return the index of the oldest label. 7180Sstevel@tonic-gate * 3. matchlbl != NULL, but doesn't match any labels in the section - warn 7190Sstevel@tonic-gate * the user, and proceed as if matchlbl == "BASE" (for safety). 7200Sstevel@tonic-gate * 4. matchlbl != NULL, and matches one of the labels in the section - return 7210Sstevel@tonic-gate * the type index associated with the label. 7220Sstevel@tonic-gate */ 7230Sstevel@tonic-gate static int 7240Sstevel@tonic-gate resurrect_labels(ctf_header_t *h, tdata_t *td, caddr_t ctfdata, char *matchlbl) 7250Sstevel@tonic-gate { 7260Sstevel@tonic-gate caddr_t buf = ctfdata + h->cth_lbloff; 7270Sstevel@tonic-gate caddr_t sbuf = ctfdata + h->cth_stroff; 7280Sstevel@tonic-gate size_t bufsz = h->cth_objtoff - h->cth_lbloff; 7290Sstevel@tonic-gate int lastidx = 0, baseidx = -1; 7300Sstevel@tonic-gate char *baselabel; 7310Sstevel@tonic-gate ctf_lblent_t *ctl; 7320Sstevel@tonic-gate 7330Sstevel@tonic-gate /* LINTED - pointer alignment */ 7340Sstevel@tonic-gate for (ctl = (ctf_lblent_t *)buf; (caddr_t)ctl < buf + bufsz; ctl++) { 7350Sstevel@tonic-gate char *label = sbuf + ctl->ctl_label; 7360Sstevel@tonic-gate 7370Sstevel@tonic-gate lastidx = ctl->ctl_typeidx; 7380Sstevel@tonic-gate 7390Sstevel@tonic-gate debug(3, "Resurrected label %s type idx %d\n", label, lastidx); 7400Sstevel@tonic-gate 7410Sstevel@tonic-gate tdata_label_add(td, label, lastidx); 7420Sstevel@tonic-gate 7430Sstevel@tonic-gate if (baseidx == -1) { 7440Sstevel@tonic-gate baseidx = lastidx; 7450Sstevel@tonic-gate baselabel = label; 7460Sstevel@tonic-gate if (matchlbl != NULL && streq(matchlbl, "BASE")) 7470Sstevel@tonic-gate return (lastidx); 7480Sstevel@tonic-gate } 7490Sstevel@tonic-gate 7500Sstevel@tonic-gate if (matchlbl != NULL && streq(label, matchlbl)) 7510Sstevel@tonic-gate return (lastidx); 7520Sstevel@tonic-gate } 7530Sstevel@tonic-gate 7540Sstevel@tonic-gate if (matchlbl != NULL) { 7550Sstevel@tonic-gate /* User provided a label that didn't match */ 7560Sstevel@tonic-gate warning("%s: Cannot find label `%s' - using base (%s)\n", 7570Sstevel@tonic-gate curfile, matchlbl, (baselabel ? baselabel : "NONE")); 7580Sstevel@tonic-gate 7590Sstevel@tonic-gate tdata_label_free(td); 7600Sstevel@tonic-gate tdata_label_add(td, baselabel, baseidx); 7610Sstevel@tonic-gate 7620Sstevel@tonic-gate return (baseidx); 7630Sstevel@tonic-gate } 7640Sstevel@tonic-gate 7650Sstevel@tonic-gate return (lastidx); 7660Sstevel@tonic-gate } 7670Sstevel@tonic-gate 7680Sstevel@tonic-gate static void 7690Sstevel@tonic-gate resurrect_objects(ctf_header_t *h, tdata_t *td, tdesc_t **tdarr, int tdsize, 7700Sstevel@tonic-gate caddr_t ctfdata, symit_data_t *si) 7710Sstevel@tonic-gate { 7720Sstevel@tonic-gate caddr_t buf = ctfdata + h->cth_objtoff; 7730Sstevel@tonic-gate size_t bufsz = h->cth_funcoff - h->cth_objtoff; 7740Sstevel@tonic-gate caddr_t dptr; 7750Sstevel@tonic-gate 7760Sstevel@tonic-gate symit_reset(si); 7770Sstevel@tonic-gate for (dptr = buf; dptr < buf + bufsz; dptr += 2) { 7780Sstevel@tonic-gate /* LINTED - pointer alignment */ 7790Sstevel@tonic-gate ushort_t id = *((ushort_t *)dptr); 7800Sstevel@tonic-gate iidesc_t *ii; 7810Sstevel@tonic-gate GElf_Sym *sym; 7820Sstevel@tonic-gate 7830Sstevel@tonic-gate if (!(sym = symit_next(si, STT_OBJECT)) && id != 0) { 7840Sstevel@tonic-gate parseterminate( 7850Sstevel@tonic-gate "Unexpected end of object symbols at %x of %x", 7860Sstevel@tonic-gate dptr - buf, bufsz); 7870Sstevel@tonic-gate } 7880Sstevel@tonic-gate 7890Sstevel@tonic-gate if (id == 0) { 7900Sstevel@tonic-gate debug(3, "Skipping null object\n"); 7910Sstevel@tonic-gate continue; 7920Sstevel@tonic-gate } else if (id >= tdsize) { 7930Sstevel@tonic-gate parseterminate("Reference to invalid type %d", id); 7940Sstevel@tonic-gate } 7950Sstevel@tonic-gate 7960Sstevel@tonic-gate ii = iidesc_new(symit_name(si)); 7970Sstevel@tonic-gate ii->ii_dtype = tdarr[id]; 7980Sstevel@tonic-gate if (GELF_ST_BIND(sym->st_info) == STB_LOCAL) { 7990Sstevel@tonic-gate ii->ii_type = II_SVAR; 8000Sstevel@tonic-gate ii->ii_owner = xstrdup(symit_curfile(si)); 8010Sstevel@tonic-gate } else 8020Sstevel@tonic-gate ii->ii_type = II_GVAR; 8030Sstevel@tonic-gate hash_add(td->td_iihash, ii); 8040Sstevel@tonic-gate 8050Sstevel@tonic-gate debug(3, "Resurrected %s object %s (%d) from %s\n", 8060Sstevel@tonic-gate (ii->ii_type == II_GVAR ? "global" : "static"), 8070Sstevel@tonic-gate ii->ii_name, id, (ii->ii_owner ? ii->ii_owner : "(none)")); 8080Sstevel@tonic-gate } 8090Sstevel@tonic-gate } 8100Sstevel@tonic-gate 8110Sstevel@tonic-gate static void 8120Sstevel@tonic-gate resurrect_functions(ctf_header_t *h, tdata_t *td, tdesc_t **tdarr, int tdsize, 8130Sstevel@tonic-gate caddr_t ctfdata, symit_data_t *si) 8140Sstevel@tonic-gate { 8150Sstevel@tonic-gate caddr_t buf = ctfdata + h->cth_funcoff; 8160Sstevel@tonic-gate size_t bufsz = h->cth_typeoff - h->cth_funcoff; 8170Sstevel@tonic-gate caddr_t dptr = buf; 8180Sstevel@tonic-gate iidesc_t *ii; 8190Sstevel@tonic-gate ushort_t info; 8200Sstevel@tonic-gate ushort_t retid; 8210Sstevel@tonic-gate GElf_Sym *sym; 8220Sstevel@tonic-gate int i; 8230Sstevel@tonic-gate 8240Sstevel@tonic-gate symit_reset(si); 8250Sstevel@tonic-gate while (dptr < buf + bufsz) { 8260Sstevel@tonic-gate /* LINTED - pointer alignment */ 8270Sstevel@tonic-gate info = *((ushort_t *)dptr); 8280Sstevel@tonic-gate dptr += 2; 8290Sstevel@tonic-gate 8300Sstevel@tonic-gate if (!(sym = symit_next(si, STT_FUNC)) && info != 0) 8310Sstevel@tonic-gate parseterminate("Unexpected end of function symbols"); 8320Sstevel@tonic-gate 8330Sstevel@tonic-gate if (info == 0) { 8340Sstevel@tonic-gate debug(3, "Skipping null function (%s)\n", 8350Sstevel@tonic-gate symit_name(si)); 8360Sstevel@tonic-gate continue; 8370Sstevel@tonic-gate } 8380Sstevel@tonic-gate 8390Sstevel@tonic-gate /* LINTED - pointer alignment */ 8400Sstevel@tonic-gate retid = *((ushort_t *)dptr); 8410Sstevel@tonic-gate dptr += 2; 8420Sstevel@tonic-gate 8430Sstevel@tonic-gate if (retid >= tdsize) 8440Sstevel@tonic-gate parseterminate("Reference to invalid type %d", retid); 8450Sstevel@tonic-gate 8460Sstevel@tonic-gate ii = iidesc_new(symit_name(si)); 8470Sstevel@tonic-gate ii->ii_dtype = tdarr[retid]; 8480Sstevel@tonic-gate if (GELF_ST_BIND(sym->st_info) == STB_LOCAL) { 8490Sstevel@tonic-gate ii->ii_type = II_SFUN; 8500Sstevel@tonic-gate ii->ii_owner = xstrdup(symit_curfile(si)); 8510Sstevel@tonic-gate } else 8520Sstevel@tonic-gate ii->ii_type = II_GFUN; 8530Sstevel@tonic-gate ii->ii_nargs = CTF_INFO_VLEN(info); 8540Sstevel@tonic-gate if (ii->ii_nargs) 8550Sstevel@tonic-gate ii->ii_args = 8560Sstevel@tonic-gate xmalloc(sizeof (tdesc_t *) * ii->ii_nargs); 8570Sstevel@tonic-gate 8580Sstevel@tonic-gate for (i = 0; i < ii->ii_nargs; i++, dptr += 2) { 8590Sstevel@tonic-gate /* LINTED - pointer alignment */ 8600Sstevel@tonic-gate ushort_t id = *((ushort_t *)dptr); 8610Sstevel@tonic-gate if (id >= tdsize) 8620Sstevel@tonic-gate parseterminate("Reference to invalid type %d", 8630Sstevel@tonic-gate id); 8640Sstevel@tonic-gate ii->ii_args[i] = tdarr[id]; 8650Sstevel@tonic-gate } 8660Sstevel@tonic-gate 8670Sstevel@tonic-gate if (ii->ii_nargs && ii->ii_args[ii->ii_nargs - 1] == NULL) { 8680Sstevel@tonic-gate ii->ii_nargs--; 8690Sstevel@tonic-gate ii->ii_vargs = 1; 8700Sstevel@tonic-gate } 8710Sstevel@tonic-gate 8720Sstevel@tonic-gate hash_add(td->td_iihash, ii); 8730Sstevel@tonic-gate 8740Sstevel@tonic-gate debug(3, "Resurrected %s function %s (%d, %d args)\n", 8750Sstevel@tonic-gate (ii->ii_type == II_GFUN ? "global" : "static"), 8760Sstevel@tonic-gate ii->ii_name, retid, ii->ii_nargs); 8770Sstevel@tonic-gate } 8780Sstevel@tonic-gate } 8790Sstevel@tonic-gate 8800Sstevel@tonic-gate static void 8810Sstevel@tonic-gate resurrect_types(ctf_header_t *h, tdata_t *td, tdesc_t **tdarr, int tdsize, 8820Sstevel@tonic-gate caddr_t ctfdata, int maxid) 8830Sstevel@tonic-gate { 8840Sstevel@tonic-gate caddr_t buf = ctfdata + h->cth_typeoff; 8850Sstevel@tonic-gate size_t bufsz = h->cth_stroff - h->cth_typeoff; 8860Sstevel@tonic-gate caddr_t sbuf = ctfdata + h->cth_stroff; 8870Sstevel@tonic-gate caddr_t dptr = buf; 8880Sstevel@tonic-gate tdesc_t *tdp; 8890Sstevel@tonic-gate uint_t data; 8900Sstevel@tonic-gate uint_t encoding; 8910Sstevel@tonic-gate size_t size, increment; 8920Sstevel@tonic-gate int tcnt; 8930Sstevel@tonic-gate int iicnt = 0; 8940Sstevel@tonic-gate tid_t tid, argid; 8950Sstevel@tonic-gate int kind, vlen; 8960Sstevel@tonic-gate int i; 8970Sstevel@tonic-gate 8980Sstevel@tonic-gate elist_t **epp; 8990Sstevel@tonic-gate mlist_t **mpp; 9000Sstevel@tonic-gate intr_t *ip; 9010Sstevel@tonic-gate 9020Sstevel@tonic-gate ctf_type_t *ctt; 9030Sstevel@tonic-gate ctf_array_t *cta; 9040Sstevel@tonic-gate ctf_enum_t *cte; 9050Sstevel@tonic-gate 9060Sstevel@tonic-gate /* 9070Sstevel@tonic-gate * A maxid of zero indicates a request to resurrect all types, so reset 9080Sstevel@tonic-gate * maxid to the maximum type id. 9090Sstevel@tonic-gate */ 9100Sstevel@tonic-gate if (maxid == 0) 9110Sstevel@tonic-gate maxid = CTF_MAX_TYPE; 9120Sstevel@tonic-gate 9130Sstevel@tonic-gate for (dptr = buf, tcnt = 0, tid = 1; dptr < buf + bufsz; tcnt++, tid++) { 9140Sstevel@tonic-gate if (tid > maxid) 9150Sstevel@tonic-gate break; 9160Sstevel@tonic-gate 9170Sstevel@tonic-gate if (tid >= tdsize) 9180Sstevel@tonic-gate parseterminate("Reference to invalid type %d", tid); 9190Sstevel@tonic-gate 9200Sstevel@tonic-gate /* LINTED - pointer alignment */ 9210Sstevel@tonic-gate ctt = (ctf_type_t *)dptr; 9220Sstevel@tonic-gate 9230Sstevel@tonic-gate get_ctt_size(ctt, &size, &increment); 9240Sstevel@tonic-gate dptr += increment; 9250Sstevel@tonic-gate 9260Sstevel@tonic-gate tdp = tdarr[tid]; 9270Sstevel@tonic-gate 9280Sstevel@tonic-gate if (CTF_NAME_STID(ctt->ctt_name) != CTF_STRTAB_0) 9290Sstevel@tonic-gate parseterminate( 9300Sstevel@tonic-gate "Unable to cope with non-zero strtab id"); 9310Sstevel@tonic-gate if (CTF_NAME_OFFSET(ctt->ctt_name) != 0) { 9320Sstevel@tonic-gate tdp->t_name = 9330Sstevel@tonic-gate xstrdup(sbuf + CTF_NAME_OFFSET(ctt->ctt_name)); 9340Sstevel@tonic-gate } else 9350Sstevel@tonic-gate tdp->t_name = NULL; 9360Sstevel@tonic-gate 9370Sstevel@tonic-gate kind = CTF_INFO_KIND(ctt->ctt_info); 9380Sstevel@tonic-gate vlen = CTF_INFO_VLEN(ctt->ctt_info); 9390Sstevel@tonic-gate 9400Sstevel@tonic-gate switch (kind) { 9410Sstevel@tonic-gate case CTF_K_INTEGER: 9420Sstevel@tonic-gate tdp->t_type = INTRINSIC; 9430Sstevel@tonic-gate tdp->t_size = size; 9440Sstevel@tonic-gate 9450Sstevel@tonic-gate /* LINTED - pointer alignment */ 9460Sstevel@tonic-gate data = *((uint_t *)dptr); 9470Sstevel@tonic-gate dptr += sizeof (uint_t); 9480Sstevel@tonic-gate encoding = CTF_INT_ENCODING(data); 9490Sstevel@tonic-gate 9500Sstevel@tonic-gate ip = xmalloc(sizeof (intr_t)); 9510Sstevel@tonic-gate ip->intr_type = INTR_INT; 9520Sstevel@tonic-gate ip->intr_signed = (encoding & CTF_INT_SIGNED) ? 1 : 0; 9530Sstevel@tonic-gate 9540Sstevel@tonic-gate if (encoding & CTF_INT_CHAR) 9550Sstevel@tonic-gate ip->intr_iformat = 'c'; 9560Sstevel@tonic-gate else if (encoding & CTF_INT_BOOL) 9570Sstevel@tonic-gate ip->intr_iformat = 'b'; 9580Sstevel@tonic-gate else if (encoding & CTF_INT_VARARGS) 9590Sstevel@tonic-gate ip->intr_iformat = 'v'; 9600Sstevel@tonic-gate else 9610Sstevel@tonic-gate ip->intr_iformat = '\0'; 9620Sstevel@tonic-gate 9630Sstevel@tonic-gate ip->intr_offset = CTF_INT_OFFSET(data); 9640Sstevel@tonic-gate ip->intr_nbits = CTF_INT_BITS(data); 9650Sstevel@tonic-gate tdp->t_intr = ip; 9660Sstevel@tonic-gate break; 9670Sstevel@tonic-gate 9680Sstevel@tonic-gate case CTF_K_FLOAT: 9690Sstevel@tonic-gate tdp->t_type = INTRINSIC; 9700Sstevel@tonic-gate tdp->t_size = size; 9710Sstevel@tonic-gate 9720Sstevel@tonic-gate /* LINTED - pointer alignment */ 9730Sstevel@tonic-gate data = *((uint_t *)dptr); 9740Sstevel@tonic-gate dptr += sizeof (uint_t); 9750Sstevel@tonic-gate 9760Sstevel@tonic-gate ip = xcalloc(sizeof (intr_t)); 9770Sstevel@tonic-gate ip->intr_type = INTR_REAL; 9780Sstevel@tonic-gate ip->intr_fformat = CTF_FP_ENCODING(data); 9790Sstevel@tonic-gate ip->intr_offset = CTF_FP_OFFSET(data); 9800Sstevel@tonic-gate ip->intr_nbits = CTF_FP_BITS(data); 9810Sstevel@tonic-gate tdp->t_intr = ip; 9820Sstevel@tonic-gate break; 9830Sstevel@tonic-gate 9840Sstevel@tonic-gate case CTF_K_POINTER: 9850Sstevel@tonic-gate tdp->t_type = POINTER; 9860Sstevel@tonic-gate tdp->t_tdesc = tdarr[ctt->ctt_type]; 9870Sstevel@tonic-gate break; 9880Sstevel@tonic-gate 9890Sstevel@tonic-gate case CTF_K_ARRAY: 9900Sstevel@tonic-gate tdp->t_type = ARRAY; 9910Sstevel@tonic-gate tdp->t_size = size; 9920Sstevel@tonic-gate 9930Sstevel@tonic-gate /* LINTED - pointer alignment */ 9940Sstevel@tonic-gate cta = (ctf_array_t *)dptr; 9950Sstevel@tonic-gate dptr += sizeof (ctf_array_t); 9960Sstevel@tonic-gate 9970Sstevel@tonic-gate tdp->t_ardef = xmalloc(sizeof (ardef_t)); 9980Sstevel@tonic-gate tdp->t_ardef->ad_contents = tdarr[cta->cta_contents]; 9990Sstevel@tonic-gate tdp->t_ardef->ad_idxtype = tdarr[cta->cta_index]; 10000Sstevel@tonic-gate tdp->t_ardef->ad_nelems = cta->cta_nelems; 10010Sstevel@tonic-gate break; 10020Sstevel@tonic-gate 10030Sstevel@tonic-gate case CTF_K_STRUCT: 10040Sstevel@tonic-gate case CTF_K_UNION: 10050Sstevel@tonic-gate tdp->t_type = (kind == CTF_K_STRUCT ? STRUCT : UNION); 10060Sstevel@tonic-gate tdp->t_size = size; 10070Sstevel@tonic-gate 10080Sstevel@tonic-gate if (size < CTF_LSTRUCT_THRESH) { 10090Sstevel@tonic-gate for (i = 0, mpp = &tdp->t_members; i < vlen; 10100Sstevel@tonic-gate i++, mpp = &((*mpp)->ml_next)) { 10110Sstevel@tonic-gate /* LINTED - pointer alignment */ 10120Sstevel@tonic-gate ctf_member_t *ctm = (ctf_member_t *) 10130Sstevel@tonic-gate dptr; 10140Sstevel@tonic-gate dptr += sizeof (ctf_member_t); 10150Sstevel@tonic-gate 10160Sstevel@tonic-gate *mpp = xmalloc(sizeof (mlist_t)); 10170Sstevel@tonic-gate (*mpp)->ml_name = xstrdup(sbuf + 10180Sstevel@tonic-gate ctm->ctm_name); 10190Sstevel@tonic-gate (*mpp)->ml_type = tdarr[ctm->ctm_type]; 10200Sstevel@tonic-gate (*mpp)->ml_offset = ctm->ctm_offset; 10210Sstevel@tonic-gate (*mpp)->ml_size = 0; 10220Sstevel@tonic-gate } 10230Sstevel@tonic-gate } else { 10240Sstevel@tonic-gate for (i = 0, mpp = &tdp->t_members; i < vlen; 10250Sstevel@tonic-gate i++, mpp = &((*mpp)->ml_next)) { 10260Sstevel@tonic-gate /* LINTED - pointer alignment */ 10270Sstevel@tonic-gate ctf_lmember_t *ctlm = (ctf_lmember_t *) 10280Sstevel@tonic-gate dptr; 10290Sstevel@tonic-gate dptr += sizeof (ctf_lmember_t); 10300Sstevel@tonic-gate 10310Sstevel@tonic-gate *mpp = xmalloc(sizeof (mlist_t)); 10320Sstevel@tonic-gate (*mpp)->ml_name = xstrdup(sbuf + 10330Sstevel@tonic-gate ctlm->ctlm_name); 10340Sstevel@tonic-gate (*mpp)->ml_type = 10350Sstevel@tonic-gate tdarr[ctlm->ctlm_type]; 10360Sstevel@tonic-gate (*mpp)->ml_offset = 10370Sstevel@tonic-gate (int)CTF_LMEM_OFFSET(ctlm); 10380Sstevel@tonic-gate (*mpp)->ml_size = 0; 10390Sstevel@tonic-gate } 10400Sstevel@tonic-gate } 10410Sstevel@tonic-gate 10420Sstevel@tonic-gate *mpp = NULL; 10430Sstevel@tonic-gate break; 10440Sstevel@tonic-gate 10450Sstevel@tonic-gate case CTF_K_ENUM: 10460Sstevel@tonic-gate tdp->t_type = ENUM; 10470Sstevel@tonic-gate tdp->t_size = size; 10480Sstevel@tonic-gate 10490Sstevel@tonic-gate for (i = 0, epp = &tdp->t_emem; i < vlen; 10500Sstevel@tonic-gate i++, epp = &((*epp)->el_next)) { 10510Sstevel@tonic-gate /* LINTED - pointer alignment */ 10520Sstevel@tonic-gate cte = (ctf_enum_t *)dptr; 10530Sstevel@tonic-gate dptr += sizeof (ctf_enum_t); 10540Sstevel@tonic-gate 10550Sstevel@tonic-gate *epp = xmalloc(sizeof (elist_t)); 10560Sstevel@tonic-gate (*epp)->el_name = xstrdup(sbuf + cte->cte_name); 10570Sstevel@tonic-gate (*epp)->el_number = cte->cte_value; 10580Sstevel@tonic-gate } 10590Sstevel@tonic-gate *epp = NULL; 10600Sstevel@tonic-gate break; 10610Sstevel@tonic-gate 10620Sstevel@tonic-gate case CTF_K_FORWARD: 10630Sstevel@tonic-gate tdp->t_type = FORWARD; 10640Sstevel@tonic-gate list_add(&td->td_fwdlist, tdp); 10650Sstevel@tonic-gate break; 10660Sstevel@tonic-gate 10670Sstevel@tonic-gate case CTF_K_TYPEDEF: 10680Sstevel@tonic-gate tdp->t_type = TYPEDEF; 10690Sstevel@tonic-gate tdp->t_tdesc = tdarr[ctt->ctt_type]; 10700Sstevel@tonic-gate break; 10710Sstevel@tonic-gate 10720Sstevel@tonic-gate case CTF_K_VOLATILE: 10730Sstevel@tonic-gate tdp->t_type = VOLATILE; 10740Sstevel@tonic-gate tdp->t_tdesc = tdarr[ctt->ctt_type]; 10750Sstevel@tonic-gate break; 10760Sstevel@tonic-gate 10770Sstevel@tonic-gate case CTF_K_CONST: 10780Sstevel@tonic-gate tdp->t_type = CONST; 10790Sstevel@tonic-gate tdp->t_tdesc = tdarr[ctt->ctt_type]; 10800Sstevel@tonic-gate break; 10810Sstevel@tonic-gate 10820Sstevel@tonic-gate case CTF_K_FUNCTION: 10830Sstevel@tonic-gate tdp->t_type = FUNCTION; 10840Sstevel@tonic-gate tdp->t_fndef = xcalloc(sizeof (fndef_t)); 10850Sstevel@tonic-gate tdp->t_fndef->fn_ret = tdarr[ctt->ctt_type]; 10860Sstevel@tonic-gate 10870Sstevel@tonic-gate /* LINTED - pointer alignment */ 10880Sstevel@tonic-gate if (vlen > 0 && *(ushort_t *)(dptr + 10890Sstevel@tonic-gate (sizeof (ushort_t) * (vlen - 1))) == 0) 10900Sstevel@tonic-gate tdp->t_fndef->fn_vargs = 1; 10910Sstevel@tonic-gate 10920Sstevel@tonic-gate tdp->t_fndef->fn_nargs = vlen - tdp->t_fndef->fn_vargs; 10930Sstevel@tonic-gate tdp->t_fndef->fn_args = xcalloc(sizeof (tdesc_t) * 10940Sstevel@tonic-gate vlen - tdp->t_fndef->fn_vargs); 10950Sstevel@tonic-gate 10960Sstevel@tonic-gate for (i = 0; i < vlen; i++) { 10970Sstevel@tonic-gate /* LINTED - pointer alignment */ 10980Sstevel@tonic-gate argid = *(ushort_t *)dptr; 10990Sstevel@tonic-gate dptr += sizeof (ushort_t); 11000Sstevel@tonic-gate 11010Sstevel@tonic-gate if (argid != 0) 11020Sstevel@tonic-gate tdp->t_fndef->fn_args[i] = tdarr[argid]; 11030Sstevel@tonic-gate } 11040Sstevel@tonic-gate 11050Sstevel@tonic-gate if (vlen & 1) 11060Sstevel@tonic-gate dptr += sizeof (ushort_t); 11070Sstevel@tonic-gate break; 11080Sstevel@tonic-gate 11090Sstevel@tonic-gate case CTF_K_RESTRICT: 11100Sstevel@tonic-gate tdp->t_type = RESTRICT; 11110Sstevel@tonic-gate tdp->t_tdesc = tdarr[ctt->ctt_type]; 11120Sstevel@tonic-gate break; 11130Sstevel@tonic-gate 11140Sstevel@tonic-gate case CTF_K_UNKNOWN: 11150Sstevel@tonic-gate break; 11160Sstevel@tonic-gate 11170Sstevel@tonic-gate default: 11180Sstevel@tonic-gate warning("Can't parse unknown CTF type %d\n", kind); 11190Sstevel@tonic-gate } 11200Sstevel@tonic-gate 11210Sstevel@tonic-gate if (CTF_INFO_ISROOT(ctt->ctt_info)) { 11220Sstevel@tonic-gate iidesc_t *ii = iidesc_new(tdp->t_name); 11230Sstevel@tonic-gate if (tdp->t_type == STRUCT || tdp->t_type == UNION || 11240Sstevel@tonic-gate tdp->t_type == ENUM) 11250Sstevel@tonic-gate ii->ii_type = II_SOU; 11260Sstevel@tonic-gate else 11270Sstevel@tonic-gate ii->ii_type = II_TYPE; 11280Sstevel@tonic-gate ii->ii_dtype = tdp; 11290Sstevel@tonic-gate hash_add(td->td_iihash, ii); 11300Sstevel@tonic-gate 11310Sstevel@tonic-gate iicnt++; 11320Sstevel@tonic-gate } 11330Sstevel@tonic-gate 11340Sstevel@tonic-gate debug(3, "Resurrected %d %stype %s (%d)\n", tdp->t_type, 11350Sstevel@tonic-gate (CTF_INFO_ISROOT(ctt->ctt_info) ? "root " : ""), 11361882Sjohnlev tdesc_name(tdp), tdp->t_id); 11370Sstevel@tonic-gate } 11380Sstevel@tonic-gate 11390Sstevel@tonic-gate debug(3, "Resurrected %d types (%d were roots)\n", tcnt, iicnt); 11400Sstevel@tonic-gate } 11410Sstevel@tonic-gate 11420Sstevel@tonic-gate /* 11430Sstevel@tonic-gate * For lack of other inspiration, we're going to take the boring route. We 11440Sstevel@tonic-gate * count the number of types. This lets us malloc that many tdesc structs 11450Sstevel@tonic-gate * before we start filling them in. This has the advantage of allowing us to 11460Sstevel@tonic-gate * avoid a merge-esque remap step. 11470Sstevel@tonic-gate */ 11480Sstevel@tonic-gate static tdata_t * 11490Sstevel@tonic-gate ctf_parse(ctf_header_t *h, caddr_t buf, symit_data_t *si, char *label) 11500Sstevel@tonic-gate { 11510Sstevel@tonic-gate tdata_t *td = tdata_new(); 11520Sstevel@tonic-gate tdesc_t **tdarr; 11530Sstevel@tonic-gate int ntypes = count_types(h, buf); 11540Sstevel@tonic-gate int idx, i; 11550Sstevel@tonic-gate 11560Sstevel@tonic-gate /* shudder */ 11570Sstevel@tonic-gate tdarr = xcalloc(sizeof (tdesc_t *) * (ntypes + 1)); 11580Sstevel@tonic-gate tdarr[0] = NULL; 11590Sstevel@tonic-gate for (i = 1; i <= ntypes; i++) { 11600Sstevel@tonic-gate tdarr[i] = xcalloc(sizeof (tdesc_t)); 11610Sstevel@tonic-gate tdarr[i]->t_id = i; 11620Sstevel@tonic-gate } 11630Sstevel@tonic-gate 11640Sstevel@tonic-gate td->td_parlabel = xstrdup(buf + h->cth_stroff + h->cth_parlabel); 11650Sstevel@tonic-gate 11660Sstevel@tonic-gate /* we have the technology - we can rebuild them */ 11670Sstevel@tonic-gate idx = resurrect_labels(h, td, buf, label); 11680Sstevel@tonic-gate 11690Sstevel@tonic-gate resurrect_objects(h, td, tdarr, ntypes + 1, buf, si); 11700Sstevel@tonic-gate resurrect_functions(h, td, tdarr, ntypes + 1, buf, si); 11710Sstevel@tonic-gate resurrect_types(h, td, tdarr, ntypes + 1, buf, idx); 11720Sstevel@tonic-gate 11730Sstevel@tonic-gate free(tdarr); 11740Sstevel@tonic-gate 11750Sstevel@tonic-gate td->td_nextid = ntypes + 1; 11760Sstevel@tonic-gate 11770Sstevel@tonic-gate return (td); 11780Sstevel@tonic-gate } 11790Sstevel@tonic-gate 11800Sstevel@tonic-gate static size_t 11810Sstevel@tonic-gate decompress_ctf(caddr_t cbuf, size_t cbufsz, caddr_t dbuf, size_t dbufsz) 11820Sstevel@tonic-gate { 11830Sstevel@tonic-gate z_stream zstr; 11840Sstevel@tonic-gate int rc; 11850Sstevel@tonic-gate 11860Sstevel@tonic-gate zstr.zalloc = (alloc_func)0; 11870Sstevel@tonic-gate zstr.zfree = (free_func)0; 11880Sstevel@tonic-gate zstr.opaque = (voidpf)0; 11890Sstevel@tonic-gate 11900Sstevel@tonic-gate zstr.next_in = (Bytef *)cbuf; 11910Sstevel@tonic-gate zstr.avail_in = cbufsz; 11920Sstevel@tonic-gate zstr.next_out = (Bytef *)dbuf; 11930Sstevel@tonic-gate zstr.avail_out = dbufsz; 11940Sstevel@tonic-gate 11950Sstevel@tonic-gate if ((rc = inflateInit(&zstr)) != Z_OK || 11960Sstevel@tonic-gate (rc = inflate(&zstr, Z_NO_FLUSH)) != Z_STREAM_END || 11970Sstevel@tonic-gate (rc = inflateEnd(&zstr)) != Z_OK) { 11980Sstevel@tonic-gate warning("CTF decompress zlib error %s\n", zError(rc)); 11990Sstevel@tonic-gate return (NULL); 12000Sstevel@tonic-gate } 12010Sstevel@tonic-gate 12020Sstevel@tonic-gate debug(3, "reflated %lu bytes to %lu, pointer at %d\n", 12030Sstevel@tonic-gate zstr.total_in, zstr.total_out, (caddr_t)zstr.next_in - cbuf); 12040Sstevel@tonic-gate 12050Sstevel@tonic-gate return (zstr.total_out); 12060Sstevel@tonic-gate } 12070Sstevel@tonic-gate 12080Sstevel@tonic-gate /* 12090Sstevel@tonic-gate * Reconstruct the type tree from a given buffer of CTF data. Only the types 12100Sstevel@tonic-gate * up to the type associated with the provided label, inclusive, will be 12110Sstevel@tonic-gate * reconstructed. If a NULL label is provided, all types will be reconstructed. 12120Sstevel@tonic-gate * 12130Sstevel@tonic-gate * This function won't work on files that have been uniquified. 12140Sstevel@tonic-gate */ 12150Sstevel@tonic-gate tdata_t * 12160Sstevel@tonic-gate ctf_load(char *file, caddr_t buf, size_t bufsz, symit_data_t *si, char *label) 12170Sstevel@tonic-gate { 12180Sstevel@tonic-gate ctf_header_t *h; 12190Sstevel@tonic-gate caddr_t ctfdata; 12200Sstevel@tonic-gate size_t ctfdatasz; 12210Sstevel@tonic-gate tdata_t *td; 12220Sstevel@tonic-gate 12230Sstevel@tonic-gate curfile = file; 12240Sstevel@tonic-gate 12250Sstevel@tonic-gate if (bufsz < sizeof (ctf_header_t)) 12260Sstevel@tonic-gate parseterminate("Corrupt CTF - short header"); 12270Sstevel@tonic-gate 12280Sstevel@tonic-gate /* LINTED - pointer alignment */ 12290Sstevel@tonic-gate h = (ctf_header_t *)buf; 12300Sstevel@tonic-gate buf += sizeof (ctf_header_t); 12310Sstevel@tonic-gate bufsz -= sizeof (ctf_header_t); 12320Sstevel@tonic-gate 12330Sstevel@tonic-gate if (h->cth_magic != CTF_MAGIC) 12340Sstevel@tonic-gate parseterminate("Corrupt CTF - bad magic 0x%x", h->cth_magic); 12350Sstevel@tonic-gate 12360Sstevel@tonic-gate if (h->cth_version != CTF_VERSION) 12370Sstevel@tonic-gate parseterminate("Unknown CTF version %d", h->cth_version); 12380Sstevel@tonic-gate 12390Sstevel@tonic-gate ctfdatasz = h->cth_stroff + h->cth_strlen; 12400Sstevel@tonic-gate if (h->cth_flags & CTF_F_COMPRESS) { 12410Sstevel@tonic-gate size_t actual; 12420Sstevel@tonic-gate 12430Sstevel@tonic-gate ctfdata = xmalloc(ctfdatasz); 12440Sstevel@tonic-gate if ((actual = decompress_ctf(buf, bufsz, ctfdata, ctfdatasz)) != 12450Sstevel@tonic-gate ctfdatasz) { 12460Sstevel@tonic-gate parseterminate("Corrupt CTF - short decompression " 12470Sstevel@tonic-gate "(was %d, expecting %d)", actual, ctfdatasz); 12480Sstevel@tonic-gate } 12490Sstevel@tonic-gate } else { 12500Sstevel@tonic-gate ctfdata = buf; 12510Sstevel@tonic-gate ctfdatasz = bufsz; 12520Sstevel@tonic-gate } 12530Sstevel@tonic-gate 12540Sstevel@tonic-gate td = ctf_parse(h, ctfdata, si, label); 12550Sstevel@tonic-gate 12560Sstevel@tonic-gate if (h->cth_flags & CTF_F_COMPRESS) 12570Sstevel@tonic-gate free(ctfdata); 12580Sstevel@tonic-gate 12590Sstevel@tonic-gate curfile = NULL; 12600Sstevel@tonic-gate 12610Sstevel@tonic-gate return (td); 12620Sstevel@tonic-gate } 1263