ctf.c revision 233407
1178481Sjb/* 2178481Sjb * CDDL HEADER START 3178481Sjb * 4178481Sjb * The contents of this file are subject to the terms of the 5178481Sjb * Common Development and Distribution License (the "License"). 6178481Sjb * You may not use this file except in compliance with the License. 7178481Sjb * 8178481Sjb * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9178481Sjb * or http://www.opensolaris.org/os/licensing. 10178481Sjb * See the License for the specific language governing permissions 11178481Sjb * and limitations under the License. 12178481Sjb * 13178481Sjb * When distributing Covered Code, include this CDDL HEADER in each 14178481Sjb * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15178481Sjb * If applicable, add the following below this CDDL HEADER, with the 16178481Sjb * fields enclosed by brackets "[]" replaced with your own identifying 17178481Sjb * information: Portions Copyright [yyyy] [name of copyright owner] 18178481Sjb * 19178481Sjb * CDDL HEADER END 20178481Sjb */ 21178481Sjb/* 22210767Srpaulo * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 23178481Sjb * Use is subject to license terms. 24178481Sjb */ 25178481Sjb 26178481Sjb/* 27178481Sjb * Create and parse buffers containing CTF data. 28178481Sjb */ 29178481Sjb 30178481Sjb#include <sys/types.h> 31178481Sjb#include <stdio.h> 32178481Sjb#include <stdlib.h> 33178481Sjb#include <strings.h> 34178481Sjb#include <ctype.h> 35178481Sjb#include <zlib.h> 36178481Sjb#include <elf.h> 37178481Sjb 38178481Sjb#include "ctf_headers.h" 39178481Sjb#include "ctftools.h" 40178481Sjb#include "strtab.h" 41178481Sjb#include "memory.h" 42178481Sjb 43178481Sjb/* 44178481Sjb * Name of the file currently being read, used to print error messages. We 45178481Sjb * assume that only one file will be read at a time, and thus make no attempt 46178481Sjb * to allow curfile to be used simultaneously by multiple threads. 47178481Sjb * 48178481Sjb * The value is only valid during a call to ctf_load. 49178481Sjb */ 50178481Sjbchar *curfile; 51178481Sjb 52178481Sjb#define CTF_BUF_CHUNK_SIZE (64 * 1024) 53178481Sjb#define RES_BUF_CHUNK_SIZE (64 * 1024) 54178481Sjb 55178481Sjbstruct ctf_buf { 56178481Sjb strtab_t ctb_strtab; /* string table */ 57178481Sjb caddr_t ctb_base; /* pointer to base of buffer */ 58178481Sjb caddr_t ctb_end; /* pointer to end of buffer */ 59178481Sjb caddr_t ctb_ptr; /* pointer to empty buffer space */ 60178481Sjb size_t ctb_size; /* size of buffer */ 61178481Sjb int nptent; /* number of processed types */ 62178481Sjb int ntholes; /* number of type holes */ 63178481Sjb}; 64178481Sjb 65233407Sgonzo/* 66233407Sgonzo * Macros to reverse byte order 67233407Sgonzo */ 68233407Sgonzo#define BSWAP_8(x) ((x) & 0xff) 69233407Sgonzo#define BSWAP_16(x) ((BSWAP_8(x) << 8) | BSWAP_8((x) >> 8)) 70233407Sgonzo#define BSWAP_32(x) ((BSWAP_16(x) << 16) | BSWAP_16((x) >> 16)) 71233407Sgonzo 72233407Sgonzo#define SWAP_16(x) (x) = BSWAP_16(x) 73233407Sgonzo#define SWAP_32(x) (x) = BSWAP_32(x) 74233407Sgonzo 75233407Sgonzostatic int target_requires_swap; 76233407Sgonzo 77178481Sjb/*PRINTFLIKE1*/ 78178481Sjbstatic void 79178546Sjbparseterminate(const char *fmt, ...) 80178481Sjb{ 81178481Sjb static char msgbuf[1024]; /* sigh */ 82178481Sjb va_list ap; 83178481Sjb 84178481Sjb va_start(ap, fmt); 85178481Sjb vsnprintf(msgbuf, sizeof (msgbuf), fmt, ap); 86178481Sjb va_end(ap); 87178481Sjb 88178481Sjb terminate("%s: %s\n", curfile, msgbuf); 89178481Sjb} 90178481Sjb 91178546Sjbstatic void 92178481Sjbctf_buf_grow(ctf_buf_t *b) 93178481Sjb{ 94178481Sjb off_t ptroff = b->ctb_ptr - b->ctb_base; 95178481Sjb 96178481Sjb b->ctb_size += CTF_BUF_CHUNK_SIZE; 97178481Sjb b->ctb_base = xrealloc(b->ctb_base, b->ctb_size); 98178481Sjb b->ctb_end = b->ctb_base + b->ctb_size; 99178481Sjb b->ctb_ptr = b->ctb_base + ptroff; 100178481Sjb} 101178481Sjb 102178546Sjbstatic ctf_buf_t * 103178481Sjbctf_buf_new(void) 104178481Sjb{ 105178481Sjb ctf_buf_t *b = xcalloc(sizeof (ctf_buf_t)); 106178481Sjb 107178481Sjb strtab_create(&b->ctb_strtab); 108178481Sjb ctf_buf_grow(b); 109178481Sjb 110178481Sjb return (b); 111178481Sjb} 112178481Sjb 113178546Sjbstatic void 114178481Sjbctf_buf_free(ctf_buf_t *b) 115178481Sjb{ 116178481Sjb strtab_destroy(&b->ctb_strtab); 117178481Sjb free(b->ctb_base); 118178481Sjb free(b); 119178481Sjb} 120178481Sjb 121178546Sjbstatic uint_t 122178481Sjbctf_buf_cur(ctf_buf_t *b) 123178481Sjb{ 124178481Sjb return (b->ctb_ptr - b->ctb_base); 125178481Sjb} 126178481Sjb 127178546Sjbstatic void 128178546Sjbctf_buf_write(ctf_buf_t *b, void const *p, size_t n) 129178481Sjb{ 130178481Sjb size_t len; 131178481Sjb 132178481Sjb while (n != 0) { 133178481Sjb if (b->ctb_ptr == b->ctb_end) 134178481Sjb ctf_buf_grow(b); 135178481Sjb 136178481Sjb len = MIN((size_t)(b->ctb_end - b->ctb_ptr), n); 137178481Sjb bcopy(p, b->ctb_ptr, len); 138178481Sjb b->ctb_ptr += len; 139178481Sjb 140178546Sjb p = (char const *)p + len; 141178481Sjb n -= len; 142178481Sjb } 143178481Sjb} 144178481Sjb 145178481Sjbstatic int 146178546Sjbwrite_label(void *arg1, void *arg2) 147178481Sjb{ 148178546Sjb labelent_t *le = arg1; 149178546Sjb ctf_buf_t *b = arg2; 150178481Sjb ctf_lblent_t ctl; 151178481Sjb 152178481Sjb ctl.ctl_label = strtab_insert(&b->ctb_strtab, le->le_name); 153178481Sjb ctl.ctl_typeidx = le->le_idx; 154178481Sjb 155233407Sgonzo if (target_requires_swap) { 156233407Sgonzo SWAP_32(ctl.ctl_label); 157233407Sgonzo SWAP_32(ctl.ctl_typeidx); 158233407Sgonzo } 159233407Sgonzo 160178481Sjb ctf_buf_write(b, &ctl, sizeof (ctl)); 161178481Sjb 162178481Sjb return (1); 163178481Sjb} 164178481Sjb 165178481Sjbstatic void 166178481Sjbwrite_objects(iidesc_t *idp, ctf_buf_t *b) 167178481Sjb{ 168178481Sjb ushort_t id = (idp ? idp->ii_dtype->t_id : 0); 169178481Sjb 170178481Sjb ctf_buf_write(b, &id, sizeof (id)); 171178481Sjb 172233407Sgonzo if (target_requires_swap) { 173233407Sgonzo SWAP_16(id); 174233407Sgonzo } 175233407Sgonzo 176178481Sjb debug(3, "Wrote object %s (%d)\n", (idp ? idp->ii_name : "(null)"), id); 177178481Sjb} 178178481Sjb 179178481Sjbstatic void 180178481Sjbwrite_functions(iidesc_t *idp, ctf_buf_t *b) 181178481Sjb{ 182178481Sjb ushort_t fdata[2]; 183178481Sjb ushort_t id; 184178481Sjb int nargs; 185178481Sjb int i; 186178481Sjb 187178481Sjb if (!idp) { 188178481Sjb fdata[0] = 0; 189178481Sjb ctf_buf_write(b, &fdata[0], sizeof (fdata[0])); 190178481Sjb 191178481Sjb debug(3, "Wrote function (null)\n"); 192178481Sjb return; 193178481Sjb } 194178481Sjb 195178481Sjb nargs = idp->ii_nargs + (idp->ii_vargs != 0); 196210767Srpaulo 197210767Srpaulo if (nargs > CTF_MAX_VLEN) { 198210767Srpaulo terminate("function %s has too many args: %d > %d\n", 199210767Srpaulo idp->ii_name, nargs, CTF_MAX_VLEN); 200210767Srpaulo } 201210767Srpaulo 202178481Sjb fdata[0] = CTF_TYPE_INFO(CTF_K_FUNCTION, 1, nargs); 203178481Sjb fdata[1] = idp->ii_dtype->t_id; 204233407Sgonzo 205233407Sgonzo if (target_requires_swap) { 206233407Sgonzo SWAP_16(fdata[0]); 207233407Sgonzo SWAP_16(fdata[1]); 208233407Sgonzo } 209233407Sgonzo 210178481Sjb ctf_buf_write(b, fdata, sizeof (fdata)); 211178481Sjb 212178481Sjb for (i = 0; i < idp->ii_nargs; i++) { 213178481Sjb id = idp->ii_args[i]->t_id; 214233407Sgonzo 215233407Sgonzo if (target_requires_swap) { 216233407Sgonzo SWAP_16(id); 217233407Sgonzo } 218233407Sgonzo 219178481Sjb ctf_buf_write(b, &id, sizeof (id)); 220178481Sjb } 221178481Sjb 222178481Sjb if (idp->ii_vargs) { 223178481Sjb id = 0; 224178481Sjb ctf_buf_write(b, &id, sizeof (id)); 225178481Sjb } 226178481Sjb 227178481Sjb debug(3, "Wrote function %s (%d args)\n", idp->ii_name, nargs); 228178481Sjb} 229178481Sjb 230178481Sjb/* 231178481Sjb * Depending on the size of the type being described, either a ctf_stype_t (for 232178481Sjb * types with size < CTF_LSTRUCT_THRESH) or a ctf_type_t (all others) will be 233178481Sjb * written. We isolate the determination here so the rest of the writer code 234178481Sjb * doesn't need to care. 235178481Sjb */ 236178481Sjbstatic void 237178481Sjbwrite_sized_type_rec(ctf_buf_t *b, ctf_type_t *ctt, size_t size) 238178481Sjb{ 239178481Sjb if (size > CTF_MAX_SIZE) { 240178481Sjb ctt->ctt_size = CTF_LSIZE_SENT; 241178481Sjb ctt->ctt_lsizehi = CTF_SIZE_TO_LSIZE_HI(size); 242178481Sjb ctt->ctt_lsizelo = CTF_SIZE_TO_LSIZE_LO(size); 243233407Sgonzo if (target_requires_swap) { 244233407Sgonzo SWAP_32(ctt->ctt_name); 245233407Sgonzo SWAP_16(ctt->ctt_info); 246233407Sgonzo SWAP_16(ctt->ctt_size); 247233407Sgonzo SWAP_32(ctt->ctt_lsizehi); 248233407Sgonzo SWAP_32(ctt->ctt_lsizelo); 249233407Sgonzo } 250178481Sjb ctf_buf_write(b, ctt, sizeof (*ctt)); 251178481Sjb } else { 252178481Sjb ctf_stype_t *cts = (ctf_stype_t *)ctt; 253178481Sjb 254178481Sjb cts->ctt_size = (ushort_t)size; 255233407Sgonzo 256233407Sgonzo if (target_requires_swap) { 257233407Sgonzo SWAP_32(cts->ctt_name); 258233407Sgonzo SWAP_16(cts->ctt_info); 259233407Sgonzo SWAP_16(cts->ctt_size); 260233407Sgonzo } 261233407Sgonzo 262178481Sjb ctf_buf_write(b, cts, sizeof (*cts)); 263178481Sjb } 264178481Sjb} 265178481Sjb 266178481Sjbstatic void 267178481Sjbwrite_unsized_type_rec(ctf_buf_t *b, ctf_type_t *ctt) 268178481Sjb{ 269178481Sjb ctf_stype_t *cts = (ctf_stype_t *)ctt; 270178481Sjb 271233407Sgonzo if (target_requires_swap) { 272233407Sgonzo SWAP_32(cts->ctt_name); 273233407Sgonzo SWAP_16(cts->ctt_info); 274233407Sgonzo SWAP_16(cts->ctt_size); 275233407Sgonzo } 276233407Sgonzo 277178481Sjb ctf_buf_write(b, cts, sizeof (*cts)); 278178481Sjb} 279178481Sjb 280178481Sjbstatic int 281178546Sjbwrite_type(void *arg1, void *arg2) 282178481Sjb{ 283178546Sjb tdesc_t *tp = arg1; 284178546Sjb ctf_buf_t *b = arg2; 285178481Sjb elist_t *ep; 286178481Sjb mlist_t *mp; 287178481Sjb intr_t *ip; 288178481Sjb 289178481Sjb size_t offset; 290178481Sjb uint_t encoding; 291178481Sjb uint_t data; 292178481Sjb int isroot = tp->t_flags & TDESC_F_ISROOT; 293178481Sjb int i; 294178481Sjb 295178481Sjb ctf_type_t ctt; 296178481Sjb ctf_array_t cta; 297178481Sjb ctf_member_t ctm; 298178481Sjb ctf_lmember_t ctlm; 299178481Sjb ctf_enum_t cte; 300178481Sjb ushort_t id; 301178481Sjb 302178481Sjb ctlm.ctlm_pad = 0; 303178481Sjb 304178481Sjb /* 305178481Sjb * There shouldn't be any holes in the type list (where a hole is 306178481Sjb * defined as two consecutive tdescs without consecutive ids), but 307178481Sjb * check for them just in case. If we do find holes, we need to make 308178481Sjb * fake entries to fill the holes, or we won't be able to reconstruct 309178481Sjb * the tree from the written data. 310178481Sjb */ 311178481Sjb if (++b->nptent < CTF_TYPE_TO_INDEX(tp->t_id)) { 312178481Sjb debug(2, "genctf: type hole from %d < x < %d\n", 313178481Sjb b->nptent - 1, CTF_TYPE_TO_INDEX(tp->t_id)); 314178481Sjb 315178481Sjb ctt.ctt_name = CTF_TYPE_NAME(CTF_STRTAB_0, 0); 316178481Sjb ctt.ctt_info = CTF_TYPE_INFO(0, 0, 0); 317178481Sjb while (b->nptent < CTF_TYPE_TO_INDEX(tp->t_id)) { 318178481Sjb write_sized_type_rec(b, &ctt, 0); 319178481Sjb b->nptent++; 320178481Sjb } 321178481Sjb } 322178481Sjb 323178481Sjb offset = strtab_insert(&b->ctb_strtab, tp->t_name); 324178481Sjb ctt.ctt_name = CTF_TYPE_NAME(CTF_STRTAB_0, offset); 325178481Sjb 326178481Sjb switch (tp->t_type) { 327178481Sjb case INTRINSIC: 328178481Sjb ip = tp->t_intr; 329178481Sjb if (ip->intr_type == INTR_INT) 330178481Sjb ctt.ctt_info = CTF_TYPE_INFO(CTF_K_INTEGER, 331178481Sjb isroot, 1); 332178481Sjb else 333178481Sjb ctt.ctt_info = CTF_TYPE_INFO(CTF_K_FLOAT, isroot, 1); 334178481Sjb write_sized_type_rec(b, &ctt, tp->t_size); 335178481Sjb 336178481Sjb encoding = 0; 337178481Sjb 338178481Sjb if (ip->intr_type == INTR_INT) { 339178481Sjb if (ip->intr_signed) 340178481Sjb encoding |= CTF_INT_SIGNED; 341178481Sjb if (ip->intr_iformat == 'c') 342178481Sjb encoding |= CTF_INT_CHAR; 343178481Sjb else if (ip->intr_iformat == 'b') 344178481Sjb encoding |= CTF_INT_BOOL; 345178481Sjb else if (ip->intr_iformat == 'v') 346178481Sjb encoding |= CTF_INT_VARARGS; 347178481Sjb } else 348178481Sjb encoding = ip->intr_fformat; 349178481Sjb 350178481Sjb data = CTF_INT_DATA(encoding, ip->intr_offset, ip->intr_nbits); 351233407Sgonzo if (target_requires_swap) { 352233407Sgonzo SWAP_32(data); 353233407Sgonzo } 354178481Sjb ctf_buf_write(b, &data, sizeof (data)); 355178481Sjb break; 356178481Sjb 357178481Sjb case POINTER: 358178481Sjb ctt.ctt_info = CTF_TYPE_INFO(CTF_K_POINTER, isroot, 0); 359178481Sjb ctt.ctt_type = tp->t_tdesc->t_id; 360178481Sjb write_unsized_type_rec(b, &ctt); 361178481Sjb break; 362178481Sjb 363178481Sjb case ARRAY: 364178481Sjb ctt.ctt_info = CTF_TYPE_INFO(CTF_K_ARRAY, isroot, 1); 365178481Sjb write_sized_type_rec(b, &ctt, tp->t_size); 366178481Sjb 367178481Sjb cta.cta_contents = tp->t_ardef->ad_contents->t_id; 368178481Sjb cta.cta_index = tp->t_ardef->ad_idxtype->t_id; 369178481Sjb cta.cta_nelems = tp->t_ardef->ad_nelems; 370233407Sgonzo if (target_requires_swap) { 371233407Sgonzo SWAP_16(cta.cta_contents); 372233407Sgonzo SWAP_16(cta.cta_index); 373233407Sgonzo SWAP_32(cta.cta_nelems); 374233407Sgonzo } 375178481Sjb ctf_buf_write(b, &cta, sizeof (cta)); 376178481Sjb break; 377178481Sjb 378178481Sjb case STRUCT: 379178481Sjb case UNION: 380178481Sjb for (i = 0, mp = tp->t_members; mp != NULL; mp = mp->ml_next) 381178481Sjb i++; /* count up struct or union members */ 382178481Sjb 383210767Srpaulo if (i > CTF_MAX_VLEN) { 384210767Srpaulo terminate("sou %s has too many members: %d > %d\n", 385210767Srpaulo tdesc_name(tp), i, CTF_MAX_VLEN); 386210767Srpaulo } 387210767Srpaulo 388178481Sjb if (tp->t_type == STRUCT) 389178481Sjb ctt.ctt_info = CTF_TYPE_INFO(CTF_K_STRUCT, isroot, i); 390178481Sjb else 391178481Sjb ctt.ctt_info = CTF_TYPE_INFO(CTF_K_UNION, isroot, i); 392178481Sjb 393178481Sjb write_sized_type_rec(b, &ctt, tp->t_size); 394178481Sjb 395178481Sjb if (tp->t_size < CTF_LSTRUCT_THRESH) { 396178481Sjb for (mp = tp->t_members; mp != NULL; mp = mp->ml_next) { 397178481Sjb offset = strtab_insert(&b->ctb_strtab, 398178481Sjb mp->ml_name); 399178481Sjb 400178481Sjb ctm.ctm_name = CTF_TYPE_NAME(CTF_STRTAB_0, 401178481Sjb offset); 402178481Sjb ctm.ctm_type = mp->ml_type->t_id; 403178481Sjb ctm.ctm_offset = mp->ml_offset; 404233407Sgonzo if (target_requires_swap) { 405233407Sgonzo SWAP_32(ctm.ctm_name); 406233407Sgonzo SWAP_16(ctm.ctm_type); 407233407Sgonzo SWAP_16(ctm.ctm_offset); 408233407Sgonzo } 409178481Sjb ctf_buf_write(b, &ctm, sizeof (ctm)); 410178481Sjb } 411178481Sjb } else { 412178481Sjb for (mp = tp->t_members; mp != NULL; mp = mp->ml_next) { 413178481Sjb offset = strtab_insert(&b->ctb_strtab, 414178481Sjb mp->ml_name); 415178481Sjb 416178481Sjb ctlm.ctlm_name = CTF_TYPE_NAME(CTF_STRTAB_0, 417178481Sjb offset); 418178481Sjb ctlm.ctlm_type = mp->ml_type->t_id; 419178481Sjb ctlm.ctlm_offsethi = 420178481Sjb CTF_OFFSET_TO_LMEMHI(mp->ml_offset); 421178481Sjb ctlm.ctlm_offsetlo = 422178481Sjb CTF_OFFSET_TO_LMEMLO(mp->ml_offset); 423233407Sgonzo 424233407Sgonzo if (target_requires_swap) { 425233407Sgonzo SWAP_32(ctlm.ctlm_name); 426233407Sgonzo SWAP_16(ctlm.ctlm_type); 427233407Sgonzo SWAP_32(ctlm.ctlm_offsethi); 428233407Sgonzo SWAP_32(ctlm.ctlm_offsetlo); 429233407Sgonzo } 430233407Sgonzo 431178481Sjb ctf_buf_write(b, &ctlm, sizeof (ctlm)); 432178481Sjb } 433178481Sjb } 434178481Sjb break; 435178481Sjb 436178481Sjb case ENUM: 437178481Sjb for (i = 0, ep = tp->t_emem; ep != NULL; ep = ep->el_next) 438178481Sjb i++; /* count up enum members */ 439178481Sjb 440207578Skan if (i > CTF_MAX_VLEN) { 441207578Skan warning("enum %s has too many values: %d > %d\n", 442207578Skan tdesc_name(tp), i, CTF_MAX_VLEN); 443207578Skan i = CTF_MAX_VLEN; 444207578Skan } 445207578Skan 446178481Sjb ctt.ctt_info = CTF_TYPE_INFO(CTF_K_ENUM, isroot, i); 447178481Sjb write_sized_type_rec(b, &ctt, tp->t_size); 448178481Sjb 449207578Skan for (ep = tp->t_emem; ep != NULL && i > 0; ep = ep->el_next) { 450178481Sjb offset = strtab_insert(&b->ctb_strtab, ep->el_name); 451178481Sjb cte.cte_name = CTF_TYPE_NAME(CTF_STRTAB_0, offset); 452178481Sjb cte.cte_value = ep->el_number; 453233407Sgonzo 454233407Sgonzo if (target_requires_swap) { 455233407Sgonzo SWAP_32(cte.cte_name); 456233407Sgonzo SWAP_32(cte.cte_value); 457233407Sgonzo } 458233407Sgonzo 459178481Sjb ctf_buf_write(b, &cte, sizeof (cte)); 460207578Skan i--; 461178481Sjb } 462178481Sjb break; 463178481Sjb 464178481Sjb case FORWARD: 465178481Sjb ctt.ctt_info = CTF_TYPE_INFO(CTF_K_FORWARD, isroot, 0); 466178481Sjb ctt.ctt_type = 0; 467178481Sjb write_unsized_type_rec(b, &ctt); 468178481Sjb break; 469178481Sjb 470178481Sjb case TYPEDEF: 471178481Sjb ctt.ctt_info = CTF_TYPE_INFO(CTF_K_TYPEDEF, isroot, 0); 472178481Sjb ctt.ctt_type = tp->t_tdesc->t_id; 473178481Sjb write_unsized_type_rec(b, &ctt); 474178481Sjb break; 475178481Sjb 476178481Sjb case VOLATILE: 477178481Sjb ctt.ctt_info = CTF_TYPE_INFO(CTF_K_VOLATILE, isroot, 0); 478178481Sjb ctt.ctt_type = tp->t_tdesc->t_id; 479178481Sjb write_unsized_type_rec(b, &ctt); 480178481Sjb break; 481178481Sjb 482178481Sjb case CONST: 483178481Sjb ctt.ctt_info = CTF_TYPE_INFO(CTF_K_CONST, isroot, 0); 484178481Sjb ctt.ctt_type = tp->t_tdesc->t_id; 485178481Sjb write_unsized_type_rec(b, &ctt); 486178481Sjb break; 487178481Sjb 488178481Sjb case FUNCTION: 489210767Srpaulo i = tp->t_fndef->fn_nargs + tp->t_fndef->fn_vargs; 490210767Srpaulo 491210767Srpaulo if (i > CTF_MAX_VLEN) { 492210767Srpaulo terminate("function %s has too many args: %d > %d\n", 493210767Srpaulo i, CTF_MAX_VLEN); 494210767Srpaulo } 495210767Srpaulo 496210767Srpaulo ctt.ctt_info = CTF_TYPE_INFO(CTF_K_FUNCTION, isroot, i); 497178481Sjb ctt.ctt_type = tp->t_fndef->fn_ret->t_id; 498178481Sjb write_unsized_type_rec(b, &ctt); 499178481Sjb 500178546Sjb for (i = 0; i < (int) tp->t_fndef->fn_nargs; i++) { 501178481Sjb id = tp->t_fndef->fn_args[i]->t_id; 502233407Sgonzo 503233407Sgonzo if (target_requires_swap) { 504233407Sgonzo SWAP_16(id); 505233407Sgonzo } 506233407Sgonzo 507178481Sjb ctf_buf_write(b, &id, sizeof (id)); 508178481Sjb } 509178481Sjb 510178481Sjb if (tp->t_fndef->fn_vargs) { 511178481Sjb id = 0; 512178481Sjb ctf_buf_write(b, &id, sizeof (id)); 513178481Sjb i++; 514178481Sjb } 515178481Sjb 516178481Sjb if (i & 1) { 517178481Sjb id = 0; 518178481Sjb ctf_buf_write(b, &id, sizeof (id)); 519178481Sjb } 520178481Sjb break; 521178481Sjb 522178481Sjb case RESTRICT: 523178481Sjb ctt.ctt_info = CTF_TYPE_INFO(CTF_K_RESTRICT, isroot, 0); 524178481Sjb ctt.ctt_type = tp->t_tdesc->t_id; 525178481Sjb write_unsized_type_rec(b, &ctt); 526178481Sjb break; 527178481Sjb 528178481Sjb default: 529178481Sjb warning("Can't write unknown type %d\n", tp->t_type); 530178481Sjb } 531178481Sjb 532178481Sjb debug(3, "Wrote type %d %s\n", tp->t_id, tdesc_name(tp)); 533178481Sjb 534178481Sjb return (1); 535178481Sjb} 536178481Sjb 537178481Sjbtypedef struct resbuf { 538178481Sjb caddr_t rb_base; 539178481Sjb caddr_t rb_ptr; 540178481Sjb size_t rb_size; 541178481Sjb z_stream rb_zstr; 542178481Sjb} resbuf_t; 543178481Sjb 544178481Sjbstatic void 545178481Sjbrbzs_grow(resbuf_t *rb) 546178481Sjb{ 547178481Sjb off_t ptroff = (caddr_t)rb->rb_zstr.next_out - rb->rb_base; 548178481Sjb 549178481Sjb rb->rb_size += RES_BUF_CHUNK_SIZE; 550178481Sjb rb->rb_base = xrealloc(rb->rb_base, rb->rb_size); 551178481Sjb rb->rb_ptr = rb->rb_base + ptroff; 552178481Sjb rb->rb_zstr.next_out = (Bytef *)(rb->rb_ptr); 553178481Sjb rb->rb_zstr.avail_out += RES_BUF_CHUNK_SIZE; 554178481Sjb} 555178481Sjb 556178481Sjbstatic void 557178481Sjbcompress_start(resbuf_t *rb) 558178481Sjb{ 559178481Sjb int rc; 560178481Sjb 561178481Sjb rb->rb_zstr.zalloc = (alloc_func)0; 562178481Sjb rb->rb_zstr.zfree = (free_func)0; 563178481Sjb rb->rb_zstr.opaque = (voidpf)0; 564178481Sjb 565178481Sjb if ((rc = deflateInit(&rb->rb_zstr, Z_BEST_COMPRESSION)) != Z_OK) 566178481Sjb parseterminate("zlib start failed: %s", zError(rc)); 567178481Sjb} 568178481Sjb 569178481Sjbstatic ssize_t 570178546Sjbcompress_buffer(void *buf, size_t n, void *data) 571178481Sjb{ 572178481Sjb resbuf_t *rb = (resbuf_t *)data; 573178481Sjb int rc; 574178481Sjb 575178481Sjb rb->rb_zstr.next_out = (Bytef *)rb->rb_ptr; 576178481Sjb rb->rb_zstr.avail_out = rb->rb_size - (rb->rb_ptr - rb->rb_base); 577178546Sjb rb->rb_zstr.next_in = buf; 578178481Sjb rb->rb_zstr.avail_in = n; 579178481Sjb 580178481Sjb while (rb->rb_zstr.avail_in) { 581178481Sjb if (rb->rb_zstr.avail_out == 0) 582178481Sjb rbzs_grow(rb); 583178481Sjb 584178481Sjb if ((rc = deflate(&rb->rb_zstr, Z_NO_FLUSH)) != Z_OK) 585178481Sjb parseterminate("zlib deflate failed: %s", zError(rc)); 586178481Sjb } 587178481Sjb rb->rb_ptr = (caddr_t)rb->rb_zstr.next_out; 588178481Sjb 589178481Sjb return (n); 590178481Sjb} 591178481Sjb 592178481Sjbstatic void 593178481Sjbcompress_flush(resbuf_t *rb, int type) 594178481Sjb{ 595178481Sjb int rc; 596178481Sjb 597178481Sjb for (;;) { 598178481Sjb if (rb->rb_zstr.avail_out == 0) 599178481Sjb rbzs_grow(rb); 600178481Sjb 601178481Sjb rc = deflate(&rb->rb_zstr, type); 602178481Sjb if ((type == Z_FULL_FLUSH && rc == Z_BUF_ERROR) || 603178481Sjb (type == Z_FINISH && rc == Z_STREAM_END)) 604178481Sjb break; 605178481Sjb else if (rc != Z_OK) 606178481Sjb parseterminate("zlib finish failed: %s", zError(rc)); 607178481Sjb } 608178481Sjb rb->rb_ptr = (caddr_t)rb->rb_zstr.next_out; 609178481Sjb} 610178481Sjb 611178481Sjbstatic void 612178481Sjbcompress_end(resbuf_t *rb) 613178481Sjb{ 614178481Sjb int rc; 615178481Sjb 616178481Sjb compress_flush(rb, Z_FINISH); 617178481Sjb 618178481Sjb if ((rc = deflateEnd(&rb->rb_zstr)) != Z_OK) 619178481Sjb parseterminate("zlib end failed: %s", zError(rc)); 620178481Sjb} 621178481Sjb 622178481Sjb/* 623178481Sjb * Pad the buffer to a power-of-2 boundary 624178481Sjb */ 625178481Sjbstatic void 626178481Sjbpad_buffer(ctf_buf_t *buf, int align) 627178481Sjb{ 628178481Sjb uint_t cur = ctf_buf_cur(buf); 629178481Sjb ssize_t topad = (align - (cur % align)) % align; 630178481Sjb static const char pad[8] = { 0 }; 631178481Sjb 632178481Sjb while (topad > 0) { 633178481Sjb ctf_buf_write(buf, pad, (topad > 8 ? 8 : topad)); 634178481Sjb topad -= 8; 635178481Sjb } 636178481Sjb} 637178481Sjb 638178481Sjbstatic ssize_t 639178546Sjbbcopy_data(void *buf, size_t n, void *data) 640178481Sjb{ 641178481Sjb caddr_t *posp = (caddr_t *)data; 642178481Sjb bcopy(buf, *posp, n); 643178481Sjb *posp += n; 644178481Sjb return (n); 645178481Sjb} 646178481Sjb 647178481Sjbstatic caddr_t 648178481Sjbwrite_buffer(ctf_header_t *h, ctf_buf_t *buf, size_t *resszp) 649178481Sjb{ 650178481Sjb caddr_t outbuf; 651178481Sjb caddr_t bufpos; 652178481Sjb 653178481Sjb outbuf = xmalloc(sizeof (ctf_header_t) + (buf->ctb_ptr - buf->ctb_base) 654178481Sjb + buf->ctb_strtab.str_size); 655178481Sjb 656178481Sjb bufpos = outbuf; 657178481Sjb (void) bcopy_data(h, sizeof (ctf_header_t), &bufpos); 658178481Sjb (void) bcopy_data(buf->ctb_base, buf->ctb_ptr - buf->ctb_base, 659178481Sjb &bufpos); 660178481Sjb (void) strtab_write(&buf->ctb_strtab, bcopy_data, &bufpos); 661178481Sjb *resszp = bufpos - outbuf; 662178481Sjb return (outbuf); 663178481Sjb} 664178481Sjb 665178481Sjb/* 666178481Sjb * Create the compression buffer, and fill it with the CTF and string 667178481Sjb * table data. We flush the compression state between the two so the 668178481Sjb * dictionary used for the string tables won't be polluted with values 669178481Sjb * that made sense for the CTF data. 670178481Sjb */ 671178481Sjbstatic caddr_t 672178481Sjbwrite_compressed_buffer(ctf_header_t *h, ctf_buf_t *buf, size_t *resszp) 673178481Sjb{ 674178481Sjb resbuf_t resbuf; 675178481Sjb resbuf.rb_size = RES_BUF_CHUNK_SIZE; 676178481Sjb resbuf.rb_base = xmalloc(resbuf.rb_size); 677178481Sjb bcopy(h, resbuf.rb_base, sizeof (ctf_header_t)); 678178481Sjb resbuf.rb_ptr = resbuf.rb_base + sizeof (ctf_header_t); 679178481Sjb 680178481Sjb compress_start(&resbuf); 681178481Sjb (void) compress_buffer(buf->ctb_base, buf->ctb_ptr - buf->ctb_base, 682178481Sjb &resbuf); 683178481Sjb compress_flush(&resbuf, Z_FULL_FLUSH); 684178481Sjb (void) strtab_write(&buf->ctb_strtab, compress_buffer, &resbuf); 685178481Sjb compress_end(&resbuf); 686178481Sjb 687178481Sjb *resszp = (resbuf.rb_ptr - resbuf.rb_base); 688178481Sjb return (resbuf.rb_base); 689178481Sjb} 690178481Sjb 691178481Sjbcaddr_t 692178481Sjbctf_gen(iiburst_t *iiburst, size_t *resszp, int do_compress) 693178481Sjb{ 694178481Sjb ctf_buf_t *buf = ctf_buf_new(); 695178481Sjb ctf_header_t h; 696178481Sjb caddr_t outbuf; 697178481Sjb 698178481Sjb int i; 699178481Sjb 700233407Sgonzo target_requires_swap = do_compress & CTF_SWAP_BYTES; 701233407Sgonzo do_compress &= ~CTF_SWAP_BYTES; 702233407Sgonzo 703178481Sjb /* 704178481Sjb * Prepare the header, and create the CTF output buffers. The data 705178481Sjb * object section and function section are both lists of 2-byte 706178481Sjb * integers; we pad these out to the next 4-byte boundary if needed. 707178481Sjb */ 708178481Sjb h.cth_magic = CTF_MAGIC; 709178481Sjb h.cth_version = CTF_VERSION; 710178481Sjb h.cth_flags = do_compress ? CTF_F_COMPRESS : 0; 711178481Sjb h.cth_parlabel = strtab_insert(&buf->ctb_strtab, 712178481Sjb iiburst->iib_td->td_parlabel); 713178481Sjb h.cth_parname = strtab_insert(&buf->ctb_strtab, 714178481Sjb iiburst->iib_td->td_parname); 715178481Sjb 716178481Sjb h.cth_lbloff = 0; 717178546Sjb (void) list_iter(iiburst->iib_td->td_labels, write_label, 718178481Sjb buf); 719178481Sjb 720178481Sjb pad_buffer(buf, 2); 721178481Sjb h.cth_objtoff = ctf_buf_cur(buf); 722178481Sjb for (i = 0; i < iiburst->iib_nobjts; i++) 723178481Sjb write_objects(iiburst->iib_objts[i], buf); 724178481Sjb 725178481Sjb pad_buffer(buf, 2); 726178481Sjb h.cth_funcoff = ctf_buf_cur(buf); 727178481Sjb for (i = 0; i < iiburst->iib_nfuncs; i++) 728178481Sjb write_functions(iiburst->iib_funcs[i], buf); 729178481Sjb 730178481Sjb pad_buffer(buf, 4); 731178481Sjb h.cth_typeoff = ctf_buf_cur(buf); 732178546Sjb (void) list_iter(iiburst->iib_types, write_type, buf); 733178481Sjb 734178481Sjb debug(2, "CTF wrote %d types\n", list_count(iiburst->iib_types)); 735178481Sjb 736178481Sjb h.cth_stroff = ctf_buf_cur(buf); 737178481Sjb h.cth_strlen = strtab_size(&buf->ctb_strtab); 738178481Sjb 739233407Sgonzo if (target_requires_swap) { 740233407Sgonzo SWAP_16(h.cth_preamble.ctp_magic); 741233407Sgonzo SWAP_32(h.cth_parlabel); 742233407Sgonzo SWAP_32(h.cth_parname); 743233407Sgonzo SWAP_32(h.cth_lbloff); 744233407Sgonzo SWAP_32(h.cth_objtoff); 745233407Sgonzo SWAP_32(h.cth_funcoff); 746233407Sgonzo SWAP_32(h.cth_typeoff); 747233407Sgonzo SWAP_32(h.cth_stroff); 748233407Sgonzo SWAP_32(h.cth_strlen); 749233407Sgonzo } 750233407Sgonzo 751178481Sjb /* 752178481Sjb * We only do compression for ctfmerge, as ctfconvert is only 753178481Sjb * supposed to be used on intermediary build objects. This is 754178481Sjb * significantly faster. 755178481Sjb */ 756178481Sjb if (do_compress) 757178481Sjb outbuf = write_compressed_buffer(&h, buf, resszp); 758178481Sjb else 759178481Sjb outbuf = write_buffer(&h, buf, resszp); 760178481Sjb 761178481Sjb ctf_buf_free(buf); 762178481Sjb return (outbuf); 763178481Sjb} 764178481Sjb 765178546Sjbstatic void 766178481Sjbget_ctt_size(ctf_type_t *ctt, size_t *sizep, size_t *incrementp) 767178481Sjb{ 768178481Sjb if (ctt->ctt_size == CTF_LSIZE_SENT) { 769178481Sjb *sizep = (size_t)CTF_TYPE_LSIZE(ctt); 770178481Sjb *incrementp = sizeof (ctf_type_t); 771178481Sjb } else { 772178481Sjb *sizep = ctt->ctt_size; 773178481Sjb *incrementp = sizeof (ctf_stype_t); 774178481Sjb } 775178481Sjb} 776178481Sjb 777178481Sjbstatic int 778178481Sjbcount_types(ctf_header_t *h, caddr_t data) 779178481Sjb{ 780178481Sjb caddr_t dptr = data + h->cth_typeoff; 781178481Sjb int count = 0; 782178481Sjb 783178481Sjb dptr = data + h->cth_typeoff; 784178481Sjb while (dptr < data + h->cth_stroff) { 785178546Sjb void *v = (void *) dptr; 786178546Sjb ctf_type_t *ctt = v; 787178481Sjb size_t vlen = CTF_INFO_VLEN(ctt->ctt_info); 788178481Sjb size_t size, increment; 789178481Sjb 790178481Sjb get_ctt_size(ctt, &size, &increment); 791178481Sjb 792178481Sjb switch (CTF_INFO_KIND(ctt->ctt_info)) { 793178481Sjb case CTF_K_INTEGER: 794178481Sjb case CTF_K_FLOAT: 795178481Sjb dptr += 4; 796178481Sjb break; 797178481Sjb case CTF_K_POINTER: 798178481Sjb case CTF_K_FORWARD: 799178481Sjb case CTF_K_TYPEDEF: 800178481Sjb case CTF_K_VOLATILE: 801178481Sjb case CTF_K_CONST: 802178481Sjb case CTF_K_RESTRICT: 803178481Sjb case CTF_K_FUNCTION: 804178481Sjb dptr += sizeof (ushort_t) * (vlen + (vlen & 1)); 805178481Sjb break; 806178481Sjb case CTF_K_ARRAY: 807178481Sjb dptr += sizeof (ctf_array_t); 808178481Sjb break; 809178481Sjb case CTF_K_STRUCT: 810178481Sjb case CTF_K_UNION: 811178481Sjb if (size < CTF_LSTRUCT_THRESH) 812178481Sjb dptr += sizeof (ctf_member_t) * vlen; 813178481Sjb else 814178481Sjb dptr += sizeof (ctf_lmember_t) * vlen; 815178481Sjb break; 816178481Sjb case CTF_K_ENUM: 817178481Sjb dptr += sizeof (ctf_enum_t) * vlen; 818178481Sjb break; 819178481Sjb case CTF_K_UNKNOWN: 820178481Sjb break; 821178481Sjb default: 822178481Sjb parseterminate("Unknown CTF type %d (#%d) at %#x", 823178481Sjb CTF_INFO_KIND(ctt->ctt_info), count, dptr - data); 824178481Sjb } 825178481Sjb 826178481Sjb dptr += increment; 827178481Sjb count++; 828178481Sjb } 829178481Sjb 830178481Sjb debug(3, "CTF read %d types\n", count); 831178481Sjb 832178481Sjb return (count); 833178481Sjb} 834178481Sjb 835178481Sjb/* 836178481Sjb * Resurrect the labels stored in the CTF data, returning the index associated 837178481Sjb * with a label provided by the caller. There are several cases, outlined 838178481Sjb * below. Note that, given two labels, the one associated with the lesser type 839178481Sjb * index is considered to be older than the other. 840178481Sjb * 841178481Sjb * 1. matchlbl == NULL - return the index of the most recent label. 842178481Sjb * 2. matchlbl == "BASE" - return the index of the oldest label. 843178481Sjb * 3. matchlbl != NULL, but doesn't match any labels in the section - warn 844178481Sjb * the user, and proceed as if matchlbl == "BASE" (for safety). 845178481Sjb * 4. matchlbl != NULL, and matches one of the labels in the section - return 846178481Sjb * the type index associated with the label. 847178481Sjb */ 848178481Sjbstatic int 849178481Sjbresurrect_labels(ctf_header_t *h, tdata_t *td, caddr_t ctfdata, char *matchlbl) 850178481Sjb{ 851178481Sjb caddr_t buf = ctfdata + h->cth_lbloff; 852178481Sjb caddr_t sbuf = ctfdata + h->cth_stroff; 853178481Sjb size_t bufsz = h->cth_objtoff - h->cth_lbloff; 854178481Sjb int lastidx = 0, baseidx = -1; 855178546Sjb char *baselabel = NULL; 856178481Sjb ctf_lblent_t *ctl; 857178546Sjb void *v = (void *) buf; 858178481Sjb 859178546Sjb for (ctl = v; (caddr_t)ctl < buf + bufsz; ctl++) { 860178481Sjb char *label = sbuf + ctl->ctl_label; 861178481Sjb 862178481Sjb lastidx = ctl->ctl_typeidx; 863178481Sjb 864178481Sjb debug(3, "Resurrected label %s type idx %d\n", label, lastidx); 865178481Sjb 866178481Sjb tdata_label_add(td, label, lastidx); 867178481Sjb 868178481Sjb if (baseidx == -1) { 869178481Sjb baseidx = lastidx; 870178481Sjb baselabel = label; 871178481Sjb if (matchlbl != NULL && streq(matchlbl, "BASE")) 872178481Sjb return (lastidx); 873178481Sjb } 874178481Sjb 875178481Sjb if (matchlbl != NULL && streq(label, matchlbl)) 876178481Sjb return (lastidx); 877178481Sjb } 878178481Sjb 879178481Sjb if (matchlbl != NULL) { 880178481Sjb /* User provided a label that didn't match */ 881178481Sjb warning("%s: Cannot find label `%s' - using base (%s)\n", 882178481Sjb curfile, matchlbl, (baselabel ? baselabel : "NONE")); 883178481Sjb 884178481Sjb tdata_label_free(td); 885178481Sjb tdata_label_add(td, baselabel, baseidx); 886178481Sjb 887178481Sjb return (baseidx); 888178481Sjb } 889178481Sjb 890178481Sjb return (lastidx); 891178481Sjb} 892178481Sjb 893178481Sjbstatic void 894178481Sjbresurrect_objects(ctf_header_t *h, tdata_t *td, tdesc_t **tdarr, int tdsize, 895178481Sjb caddr_t ctfdata, symit_data_t *si) 896178481Sjb{ 897178481Sjb caddr_t buf = ctfdata + h->cth_objtoff; 898178481Sjb size_t bufsz = h->cth_funcoff - h->cth_objtoff; 899178481Sjb caddr_t dptr; 900178481Sjb 901178481Sjb symit_reset(si); 902178481Sjb for (dptr = buf; dptr < buf + bufsz; dptr += 2) { 903178546Sjb void *v = (void *) dptr; 904178546Sjb ushort_t id = *((ushort_t *)v); 905178481Sjb iidesc_t *ii; 906178481Sjb GElf_Sym *sym; 907178481Sjb 908178481Sjb if (!(sym = symit_next(si, STT_OBJECT)) && id != 0) { 909178481Sjb parseterminate( 910178481Sjb "Unexpected end of object symbols at %x of %x", 911178481Sjb dptr - buf, bufsz); 912178481Sjb } 913178481Sjb 914178481Sjb if (id == 0) { 915178481Sjb debug(3, "Skipping null object\n"); 916178481Sjb continue; 917178481Sjb } else if (id >= tdsize) { 918178481Sjb parseterminate("Reference to invalid type %d", id); 919178481Sjb } 920178481Sjb 921178481Sjb ii = iidesc_new(symit_name(si)); 922178481Sjb ii->ii_dtype = tdarr[id]; 923178481Sjb if (GELF_ST_BIND(sym->st_info) == STB_LOCAL) { 924178481Sjb ii->ii_type = II_SVAR; 925178481Sjb ii->ii_owner = xstrdup(symit_curfile(si)); 926178481Sjb } else 927178481Sjb ii->ii_type = II_GVAR; 928178481Sjb hash_add(td->td_iihash, ii); 929178481Sjb 930178481Sjb debug(3, "Resurrected %s object %s (%d) from %s\n", 931178481Sjb (ii->ii_type == II_GVAR ? "global" : "static"), 932178481Sjb ii->ii_name, id, (ii->ii_owner ? ii->ii_owner : "(none)")); 933178481Sjb } 934178481Sjb} 935178481Sjb 936178481Sjbstatic void 937178481Sjbresurrect_functions(ctf_header_t *h, tdata_t *td, tdesc_t **tdarr, int tdsize, 938178481Sjb caddr_t ctfdata, symit_data_t *si) 939178481Sjb{ 940178481Sjb caddr_t buf = ctfdata + h->cth_funcoff; 941178481Sjb size_t bufsz = h->cth_typeoff - h->cth_funcoff; 942178481Sjb caddr_t dptr = buf; 943178481Sjb iidesc_t *ii; 944178481Sjb ushort_t info; 945178481Sjb ushort_t retid; 946178481Sjb GElf_Sym *sym; 947178481Sjb int i; 948178481Sjb 949178481Sjb symit_reset(si); 950178481Sjb while (dptr < buf + bufsz) { 951178546Sjb void *v = (void *) dptr; 952178546Sjb info = *((ushort_t *)v); 953178481Sjb dptr += 2; 954178481Sjb 955178481Sjb if (!(sym = symit_next(si, STT_FUNC)) && info != 0) 956178481Sjb parseterminate("Unexpected end of function symbols"); 957178481Sjb 958178481Sjb if (info == 0) { 959178481Sjb debug(3, "Skipping null function (%s)\n", 960178481Sjb symit_name(si)); 961178481Sjb continue; 962178481Sjb } 963178481Sjb 964178546Sjb v = (void *) dptr; 965178546Sjb retid = *((ushort_t *)v); 966178481Sjb dptr += 2; 967178481Sjb 968178481Sjb if (retid >= tdsize) 969178481Sjb parseterminate("Reference to invalid type %d", retid); 970178481Sjb 971178481Sjb ii = iidesc_new(symit_name(si)); 972178481Sjb ii->ii_dtype = tdarr[retid]; 973178481Sjb if (GELF_ST_BIND(sym->st_info) == STB_LOCAL) { 974178481Sjb ii->ii_type = II_SFUN; 975178481Sjb ii->ii_owner = xstrdup(symit_curfile(si)); 976178481Sjb } else 977178481Sjb ii->ii_type = II_GFUN; 978178481Sjb ii->ii_nargs = CTF_INFO_VLEN(info); 979178481Sjb if (ii->ii_nargs) 980178481Sjb ii->ii_args = 981178481Sjb xmalloc(sizeof (tdesc_t *) * ii->ii_nargs); 982178481Sjb 983178481Sjb for (i = 0; i < ii->ii_nargs; i++, dptr += 2) { 984178546Sjb v = (void *) dptr; 985178546Sjb ushort_t id = *((ushort_t *)v); 986178481Sjb if (id >= tdsize) 987178481Sjb parseterminate("Reference to invalid type %d", 988178481Sjb id); 989178481Sjb ii->ii_args[i] = tdarr[id]; 990178481Sjb } 991178481Sjb 992178481Sjb if (ii->ii_nargs && ii->ii_args[ii->ii_nargs - 1] == NULL) { 993178481Sjb ii->ii_nargs--; 994178481Sjb ii->ii_vargs = 1; 995178481Sjb } 996178481Sjb 997178481Sjb hash_add(td->td_iihash, ii); 998178481Sjb 999178481Sjb debug(3, "Resurrected %s function %s (%d, %d args)\n", 1000178481Sjb (ii->ii_type == II_GFUN ? "global" : "static"), 1001178481Sjb ii->ii_name, retid, ii->ii_nargs); 1002178481Sjb } 1003178481Sjb} 1004178481Sjb 1005178481Sjbstatic void 1006178481Sjbresurrect_types(ctf_header_t *h, tdata_t *td, tdesc_t **tdarr, int tdsize, 1007178481Sjb caddr_t ctfdata, int maxid) 1008178481Sjb{ 1009178481Sjb caddr_t buf = ctfdata + h->cth_typeoff; 1010178481Sjb size_t bufsz = h->cth_stroff - h->cth_typeoff; 1011178481Sjb caddr_t sbuf = ctfdata + h->cth_stroff; 1012178481Sjb caddr_t dptr = buf; 1013178481Sjb tdesc_t *tdp; 1014178481Sjb uint_t data; 1015178481Sjb uint_t encoding; 1016178481Sjb size_t size, increment; 1017178481Sjb int tcnt; 1018178481Sjb int iicnt = 0; 1019178481Sjb tid_t tid, argid; 1020178481Sjb int kind, vlen; 1021178481Sjb int i; 1022178481Sjb 1023178481Sjb elist_t **epp; 1024178481Sjb mlist_t **mpp; 1025178481Sjb intr_t *ip; 1026178481Sjb 1027178481Sjb ctf_type_t *ctt; 1028178481Sjb ctf_array_t *cta; 1029178481Sjb ctf_enum_t *cte; 1030178481Sjb 1031178481Sjb /* 1032178481Sjb * A maxid of zero indicates a request to resurrect all types, so reset 1033178481Sjb * maxid to the maximum type id. 1034178481Sjb */ 1035178481Sjb if (maxid == 0) 1036178481Sjb maxid = CTF_MAX_TYPE; 1037178481Sjb 1038178481Sjb for (dptr = buf, tcnt = 0, tid = 1; dptr < buf + bufsz; tcnt++, tid++) { 1039178481Sjb if (tid > maxid) 1040178481Sjb break; 1041178481Sjb 1042178481Sjb if (tid >= tdsize) 1043178481Sjb parseterminate("Reference to invalid type %d", tid); 1044178481Sjb 1045178546Sjb void *v = (void *) dptr; 1046178546Sjb ctt = v; 1047178481Sjb 1048178481Sjb get_ctt_size(ctt, &size, &increment); 1049178481Sjb dptr += increment; 1050178481Sjb 1051178481Sjb tdp = tdarr[tid]; 1052178481Sjb 1053178481Sjb if (CTF_NAME_STID(ctt->ctt_name) != CTF_STRTAB_0) 1054178481Sjb parseterminate( 1055210767Srpaulo "Unable to cope with non-zero strtab id"); 1056178481Sjb if (CTF_NAME_OFFSET(ctt->ctt_name) != 0) { 1057178481Sjb tdp->t_name = 1058178481Sjb xstrdup(sbuf + CTF_NAME_OFFSET(ctt->ctt_name)); 1059178481Sjb } else 1060178481Sjb tdp->t_name = NULL; 1061178481Sjb 1062178481Sjb kind = CTF_INFO_KIND(ctt->ctt_info); 1063178481Sjb vlen = CTF_INFO_VLEN(ctt->ctt_info); 1064178481Sjb 1065178481Sjb switch (kind) { 1066178481Sjb case CTF_K_INTEGER: 1067178481Sjb tdp->t_type = INTRINSIC; 1068178481Sjb tdp->t_size = size; 1069178481Sjb 1070178546Sjb v = (void *) dptr; 1071178546Sjb data = *((uint_t *)v); 1072178481Sjb dptr += sizeof (uint_t); 1073178481Sjb encoding = CTF_INT_ENCODING(data); 1074178481Sjb 1075178481Sjb ip = xmalloc(sizeof (intr_t)); 1076178481Sjb ip->intr_type = INTR_INT; 1077178481Sjb ip->intr_signed = (encoding & CTF_INT_SIGNED) ? 1 : 0; 1078178481Sjb 1079178481Sjb if (encoding & CTF_INT_CHAR) 1080178481Sjb ip->intr_iformat = 'c'; 1081178481Sjb else if (encoding & CTF_INT_BOOL) 1082178481Sjb ip->intr_iformat = 'b'; 1083178481Sjb else if (encoding & CTF_INT_VARARGS) 1084178481Sjb ip->intr_iformat = 'v'; 1085178481Sjb else 1086178481Sjb ip->intr_iformat = '\0'; 1087178481Sjb 1088178481Sjb ip->intr_offset = CTF_INT_OFFSET(data); 1089178481Sjb ip->intr_nbits = CTF_INT_BITS(data); 1090178481Sjb tdp->t_intr = ip; 1091178481Sjb break; 1092178481Sjb 1093178481Sjb case CTF_K_FLOAT: 1094178481Sjb tdp->t_type = INTRINSIC; 1095178481Sjb tdp->t_size = size; 1096178481Sjb 1097178546Sjb v = (void *) dptr; 1098178546Sjb data = *((uint_t *)v); 1099178481Sjb dptr += sizeof (uint_t); 1100178481Sjb 1101178481Sjb ip = xcalloc(sizeof (intr_t)); 1102178481Sjb ip->intr_type = INTR_REAL; 1103178481Sjb ip->intr_fformat = CTF_FP_ENCODING(data); 1104178481Sjb ip->intr_offset = CTF_FP_OFFSET(data); 1105178481Sjb ip->intr_nbits = CTF_FP_BITS(data); 1106178481Sjb tdp->t_intr = ip; 1107178481Sjb break; 1108178481Sjb 1109178481Sjb case CTF_K_POINTER: 1110178481Sjb tdp->t_type = POINTER; 1111178481Sjb tdp->t_tdesc = tdarr[ctt->ctt_type]; 1112178481Sjb break; 1113178481Sjb 1114178481Sjb case CTF_K_ARRAY: 1115178481Sjb tdp->t_type = ARRAY; 1116178481Sjb tdp->t_size = size; 1117178481Sjb 1118178546Sjb v = (void *) dptr; 1119178546Sjb cta = v; 1120178481Sjb dptr += sizeof (ctf_array_t); 1121178481Sjb 1122178481Sjb tdp->t_ardef = xmalloc(sizeof (ardef_t)); 1123178481Sjb tdp->t_ardef->ad_contents = tdarr[cta->cta_contents]; 1124178481Sjb tdp->t_ardef->ad_idxtype = tdarr[cta->cta_index]; 1125178481Sjb tdp->t_ardef->ad_nelems = cta->cta_nelems; 1126178481Sjb break; 1127178481Sjb 1128178481Sjb case CTF_K_STRUCT: 1129178481Sjb case CTF_K_UNION: 1130178481Sjb tdp->t_type = (kind == CTF_K_STRUCT ? STRUCT : UNION); 1131178481Sjb tdp->t_size = size; 1132178481Sjb 1133178481Sjb if (size < CTF_LSTRUCT_THRESH) { 1134178481Sjb for (i = 0, mpp = &tdp->t_members; i < vlen; 1135178481Sjb i++, mpp = &((*mpp)->ml_next)) { 1136178546Sjb v = (void *) dptr; 1137178546Sjb ctf_member_t *ctm = v; 1138178481Sjb dptr += sizeof (ctf_member_t); 1139178481Sjb 1140178481Sjb *mpp = xmalloc(sizeof (mlist_t)); 1141178481Sjb (*mpp)->ml_name = xstrdup(sbuf + 1142178481Sjb ctm->ctm_name); 1143178481Sjb (*mpp)->ml_type = tdarr[ctm->ctm_type]; 1144178481Sjb (*mpp)->ml_offset = ctm->ctm_offset; 1145178481Sjb (*mpp)->ml_size = 0; 1146178481Sjb } 1147178481Sjb } else { 1148178481Sjb for (i = 0, mpp = &tdp->t_members; i < vlen; 1149178481Sjb i++, mpp = &((*mpp)->ml_next)) { 1150178546Sjb v = (void *) dptr; 1151178546Sjb ctf_lmember_t *ctlm = v; 1152178481Sjb dptr += sizeof (ctf_lmember_t); 1153178481Sjb 1154178481Sjb *mpp = xmalloc(sizeof (mlist_t)); 1155178481Sjb (*mpp)->ml_name = xstrdup(sbuf + 1156178481Sjb ctlm->ctlm_name); 1157178481Sjb (*mpp)->ml_type = 1158178481Sjb tdarr[ctlm->ctlm_type]; 1159178481Sjb (*mpp)->ml_offset = 1160178481Sjb (int)CTF_LMEM_OFFSET(ctlm); 1161178481Sjb (*mpp)->ml_size = 0; 1162178481Sjb } 1163178481Sjb } 1164178481Sjb 1165178481Sjb *mpp = NULL; 1166178481Sjb break; 1167178481Sjb 1168178481Sjb case CTF_K_ENUM: 1169178481Sjb tdp->t_type = ENUM; 1170178481Sjb tdp->t_size = size; 1171178481Sjb 1172178481Sjb for (i = 0, epp = &tdp->t_emem; i < vlen; 1173178481Sjb i++, epp = &((*epp)->el_next)) { 1174178546Sjb v = (void *) dptr; 1175178546Sjb cte = v; 1176178481Sjb dptr += sizeof (ctf_enum_t); 1177178481Sjb 1178178481Sjb *epp = xmalloc(sizeof (elist_t)); 1179178481Sjb (*epp)->el_name = xstrdup(sbuf + cte->cte_name); 1180178481Sjb (*epp)->el_number = cte->cte_value; 1181178481Sjb } 1182178481Sjb *epp = NULL; 1183178481Sjb break; 1184178481Sjb 1185178481Sjb case CTF_K_FORWARD: 1186178481Sjb tdp->t_type = FORWARD; 1187178481Sjb list_add(&td->td_fwdlist, tdp); 1188178481Sjb break; 1189178481Sjb 1190178481Sjb case CTF_K_TYPEDEF: 1191178481Sjb tdp->t_type = TYPEDEF; 1192178481Sjb tdp->t_tdesc = tdarr[ctt->ctt_type]; 1193178481Sjb break; 1194178481Sjb 1195178481Sjb case CTF_K_VOLATILE: 1196178481Sjb tdp->t_type = VOLATILE; 1197178481Sjb tdp->t_tdesc = tdarr[ctt->ctt_type]; 1198178481Sjb break; 1199178481Sjb 1200178481Sjb case CTF_K_CONST: 1201178481Sjb tdp->t_type = CONST; 1202178481Sjb tdp->t_tdesc = tdarr[ctt->ctt_type]; 1203178481Sjb break; 1204178481Sjb 1205178481Sjb case CTF_K_FUNCTION: 1206178481Sjb tdp->t_type = FUNCTION; 1207178481Sjb tdp->t_fndef = xcalloc(sizeof (fndef_t)); 1208178481Sjb tdp->t_fndef->fn_ret = tdarr[ctt->ctt_type]; 1209178481Sjb 1210178546Sjb v = (void *) (dptr + (sizeof (ushort_t) * (vlen - 1))); 1211178546Sjb if (vlen > 0 && *(ushort_t *)v == 0) 1212178481Sjb tdp->t_fndef->fn_vargs = 1; 1213178481Sjb 1214178481Sjb tdp->t_fndef->fn_nargs = vlen - tdp->t_fndef->fn_vargs; 1215178481Sjb tdp->t_fndef->fn_args = xcalloc(sizeof (tdesc_t) * 1216178481Sjb vlen - tdp->t_fndef->fn_vargs); 1217178481Sjb 1218178481Sjb for (i = 0; i < vlen; i++) { 1219178546Sjb v = (void *) dptr; 1220178546Sjb argid = *(ushort_t *)v; 1221178481Sjb dptr += sizeof (ushort_t); 1222178481Sjb 1223178481Sjb if (argid != 0) 1224178481Sjb tdp->t_fndef->fn_args[i] = tdarr[argid]; 1225178481Sjb } 1226178481Sjb 1227178481Sjb if (vlen & 1) 1228178481Sjb dptr += sizeof (ushort_t); 1229178481Sjb break; 1230178481Sjb 1231178481Sjb case CTF_K_RESTRICT: 1232178481Sjb tdp->t_type = RESTRICT; 1233178481Sjb tdp->t_tdesc = tdarr[ctt->ctt_type]; 1234178481Sjb break; 1235178481Sjb 1236178481Sjb case CTF_K_UNKNOWN: 1237178481Sjb break; 1238178481Sjb 1239178481Sjb default: 1240178481Sjb warning("Can't parse unknown CTF type %d\n", kind); 1241178481Sjb } 1242178481Sjb 1243178481Sjb if (CTF_INFO_ISROOT(ctt->ctt_info)) { 1244178481Sjb iidesc_t *ii = iidesc_new(tdp->t_name); 1245178481Sjb if (tdp->t_type == STRUCT || tdp->t_type == UNION || 1246178481Sjb tdp->t_type == ENUM) 1247178481Sjb ii->ii_type = II_SOU; 1248178481Sjb else 1249178481Sjb ii->ii_type = II_TYPE; 1250178481Sjb ii->ii_dtype = tdp; 1251178481Sjb hash_add(td->td_iihash, ii); 1252178481Sjb 1253178481Sjb iicnt++; 1254178481Sjb } 1255178481Sjb 1256178481Sjb debug(3, "Resurrected %d %stype %s (%d)\n", tdp->t_type, 1257178481Sjb (CTF_INFO_ISROOT(ctt->ctt_info) ? "root " : ""), 1258178481Sjb tdesc_name(tdp), tdp->t_id); 1259178481Sjb } 1260178481Sjb 1261178481Sjb debug(3, "Resurrected %d types (%d were roots)\n", tcnt, iicnt); 1262178481Sjb} 1263178481Sjb 1264178481Sjb/* 1265178481Sjb * For lack of other inspiration, we're going to take the boring route. We 1266178481Sjb * count the number of types. This lets us malloc that many tdesc structs 1267178481Sjb * before we start filling them in. This has the advantage of allowing us to 1268178481Sjb * avoid a merge-esque remap step. 1269178481Sjb */ 1270178481Sjbstatic tdata_t * 1271178481Sjbctf_parse(ctf_header_t *h, caddr_t buf, symit_data_t *si, char *label) 1272178481Sjb{ 1273178481Sjb tdata_t *td = tdata_new(); 1274178481Sjb tdesc_t **tdarr; 1275178481Sjb int ntypes = count_types(h, buf); 1276178481Sjb int idx, i; 1277178481Sjb 1278178481Sjb /* shudder */ 1279178481Sjb tdarr = xcalloc(sizeof (tdesc_t *) * (ntypes + 1)); 1280178481Sjb tdarr[0] = NULL; 1281178481Sjb for (i = 1; i <= ntypes; i++) { 1282178481Sjb tdarr[i] = xcalloc(sizeof (tdesc_t)); 1283178481Sjb tdarr[i]->t_id = i; 1284178481Sjb } 1285178481Sjb 1286178481Sjb td->td_parlabel = xstrdup(buf + h->cth_stroff + h->cth_parlabel); 1287178481Sjb 1288178481Sjb /* we have the technology - we can rebuild them */ 1289178481Sjb idx = resurrect_labels(h, td, buf, label); 1290178481Sjb 1291178481Sjb resurrect_objects(h, td, tdarr, ntypes + 1, buf, si); 1292178481Sjb resurrect_functions(h, td, tdarr, ntypes + 1, buf, si); 1293178481Sjb resurrect_types(h, td, tdarr, ntypes + 1, buf, idx); 1294178481Sjb 1295178481Sjb free(tdarr); 1296178481Sjb 1297178481Sjb td->td_nextid = ntypes + 1; 1298178481Sjb 1299178481Sjb return (td); 1300178481Sjb} 1301178481Sjb 1302178481Sjbstatic size_t 1303178481Sjbdecompress_ctf(caddr_t cbuf, size_t cbufsz, caddr_t dbuf, size_t dbufsz) 1304178481Sjb{ 1305178481Sjb z_stream zstr; 1306178481Sjb int rc; 1307178481Sjb 1308178481Sjb zstr.zalloc = (alloc_func)0; 1309178481Sjb zstr.zfree = (free_func)0; 1310178481Sjb zstr.opaque = (voidpf)0; 1311178481Sjb 1312178481Sjb zstr.next_in = (Bytef *)cbuf; 1313178481Sjb zstr.avail_in = cbufsz; 1314178481Sjb zstr.next_out = (Bytef *)dbuf; 1315178481Sjb zstr.avail_out = dbufsz; 1316178481Sjb 1317178481Sjb if ((rc = inflateInit(&zstr)) != Z_OK || 1318178481Sjb (rc = inflate(&zstr, Z_NO_FLUSH)) != Z_STREAM_END || 1319178481Sjb (rc = inflateEnd(&zstr)) != Z_OK) { 1320178481Sjb warning("CTF decompress zlib error %s\n", zError(rc)); 1321178546Sjb return (0); 1322178481Sjb } 1323178481Sjb 1324178481Sjb debug(3, "reflated %lu bytes to %lu, pointer at %d\n", 1325178481Sjb zstr.total_in, zstr.total_out, (caddr_t)zstr.next_in - cbuf); 1326178481Sjb 1327178481Sjb return (zstr.total_out); 1328178481Sjb} 1329178481Sjb 1330178481Sjb/* 1331178481Sjb * Reconstruct the type tree from a given buffer of CTF data. Only the types 1332178481Sjb * up to the type associated with the provided label, inclusive, will be 1333178481Sjb * reconstructed. If a NULL label is provided, all types will be reconstructed. 1334178481Sjb * 1335178481Sjb * This function won't work on files that have been uniquified. 1336178481Sjb */ 1337178481Sjbtdata_t * 1338178481Sjbctf_load(char *file, caddr_t buf, size_t bufsz, symit_data_t *si, char *label) 1339178481Sjb{ 1340178481Sjb ctf_header_t *h; 1341178481Sjb caddr_t ctfdata; 1342178481Sjb size_t ctfdatasz; 1343178481Sjb tdata_t *td; 1344178481Sjb 1345178481Sjb curfile = file; 1346178481Sjb 1347178481Sjb if (bufsz < sizeof (ctf_header_t)) 1348178481Sjb parseterminate("Corrupt CTF - short header"); 1349178481Sjb 1350178546Sjb void *v = (void *) buf; 1351178546Sjb h = v; 1352178481Sjb buf += sizeof (ctf_header_t); 1353178481Sjb bufsz -= sizeof (ctf_header_t); 1354178481Sjb 1355178481Sjb if (h->cth_magic != CTF_MAGIC) 1356178481Sjb parseterminate("Corrupt CTF - bad magic 0x%x", h->cth_magic); 1357178481Sjb 1358178481Sjb if (h->cth_version != CTF_VERSION) 1359178481Sjb parseterminate("Unknown CTF version %d", h->cth_version); 1360178481Sjb 1361178481Sjb ctfdatasz = h->cth_stroff + h->cth_strlen; 1362178481Sjb if (h->cth_flags & CTF_F_COMPRESS) { 1363178481Sjb size_t actual; 1364178481Sjb 1365178481Sjb ctfdata = xmalloc(ctfdatasz); 1366178481Sjb if ((actual = decompress_ctf(buf, bufsz, ctfdata, ctfdatasz)) != 1367178481Sjb ctfdatasz) { 1368178481Sjb parseterminate("Corrupt CTF - short decompression " 1369178481Sjb "(was %d, expecting %d)", actual, ctfdatasz); 1370178481Sjb } 1371178481Sjb } else { 1372178481Sjb ctfdata = buf; 1373178481Sjb ctfdatasz = bufsz; 1374178481Sjb } 1375178481Sjb 1376178481Sjb td = ctf_parse(h, ctfdata, si, label); 1377178481Sjb 1378178481Sjb if (h->cth_flags & CTF_F_COMPRESS) 1379178481Sjb free(ctfdata); 1380178481Sjb 1381178481Sjb curfile = NULL; 1382178481Sjb 1383178481Sjb return (td); 1384178481Sjb} 1385