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