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