1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 #pragma ident "%Z%%M% %I% %E% SMI" 27 28 /* 29 * Create and parse buffers containing CTF data. 30 */ 31 32 #include <sys/types.h> 33 #include <stdio.h> 34 #include <stdlib.h> 35 #include <strings.h> 36 #include <ctype.h> 37 #include <zlib.h> 38 #include <elf.h> 39 40 #include "ctf_headers.h" 41 #include "ctftools.h" 42 #include "strtab.h" 43 #include "memory.h" 44 45 /* 46 * Name of the file currently being read, used to print error messages. We 47 * assume that only one file will be read at a time, and thus make no attempt 48 * to allow curfile to be used simultaneously by multiple threads. 49 * 50 * The value is only valid during a call to ctf_load. 51 */ 52 char *curfile; 53 54 #define CTF_BUF_CHUNK_SIZE (64 * 1024) 55 #define RES_BUF_CHUNK_SIZE (64 * 1024) 56 57 struct ctf_buf { 58 strtab_t ctb_strtab; /* string table */ 59 caddr_t ctb_base; /* pointer to base of buffer */ 60 caddr_t ctb_end; /* pointer to end of buffer */ 61 caddr_t ctb_ptr; /* pointer to empty buffer space */ 62 size_t ctb_size; /* size of buffer */ 63 int nptent; /* number of processed types */ 64 int ntholes; /* number of type holes */ 65 }; 66 67 /*PRINTFLIKE1*/ 68 static void 69 parseterminate(const char *fmt, ...) 70 { 71 static char msgbuf[1024]; /* sigh */ 72 va_list ap; 73 74 va_start(ap, fmt); 75 vsnprintf(msgbuf, sizeof (msgbuf), fmt, ap); 76 va_end(ap); 77 78 terminate("%s: %s\n", curfile, msgbuf); 79 } 80 81 static void 82 ctf_buf_grow(ctf_buf_t *b) 83 { 84 off_t ptroff = b->ctb_ptr - b->ctb_base; 85 86 b->ctb_size += CTF_BUF_CHUNK_SIZE; 87 b->ctb_base = xrealloc(b->ctb_base, b->ctb_size); 88 b->ctb_end = b->ctb_base + b->ctb_size; 89 b->ctb_ptr = b->ctb_base + ptroff; 90 } 91 92 static ctf_buf_t * 93 ctf_buf_new(void) 94 { 95 ctf_buf_t *b = xcalloc(sizeof (ctf_buf_t)); 96 97 strtab_create(&b->ctb_strtab); 98 ctf_buf_grow(b); 99 100 return (b); 101 } 102 103 static void 104 ctf_buf_free(ctf_buf_t *b) 105 { 106 strtab_destroy(&b->ctb_strtab); 107 free(b->ctb_base); 108 free(b); 109 } 110 111 static uint_t 112 ctf_buf_cur(ctf_buf_t *b) 113 { 114 return (b->ctb_ptr - b->ctb_base); 115 } 116 117 static void 118 ctf_buf_write(ctf_buf_t *b, void const *p, size_t n) 119 { 120 size_t len; 121 122 while (n != 0) { 123 if (b->ctb_ptr == b->ctb_end) 124 ctf_buf_grow(b); 125 126 len = MIN((size_t)(b->ctb_end - b->ctb_ptr), n); 127 bcopy(p, b->ctb_ptr, len); 128 b->ctb_ptr += len; 129 130 p = (char const *)p + len; 131 n -= len; 132 } 133 } 134 135 static int 136 write_label(void *arg1, void *arg2) 137 { 138 labelent_t *le = arg1; 139 ctf_buf_t *b = arg2; 140 ctf_lblent_t ctl; 141 142 ctl.ctl_label = strtab_insert(&b->ctb_strtab, le->le_name); 143 ctl.ctl_typeidx = le->le_idx; 144 145 ctf_buf_write(b, &ctl, sizeof (ctl)); 146 147 return (1); 148 } 149 150 static void 151 write_objects(iidesc_t *idp, ctf_buf_t *b) 152 { 153 ushort_t id = (idp ? idp->ii_dtype->t_id : 0); 154 155 ctf_buf_write(b, &id, sizeof (id)); 156 157 debug(3, "Wrote object %s (%d)\n", (idp ? idp->ii_name : "(null)"), id); 158 } 159 160 static void 161 write_functions(iidesc_t *idp, ctf_buf_t *b) 162 { 163 ushort_t fdata[2]; 164 ushort_t id; 165 int nargs; 166 int i; 167 168 if (!idp) { 169 fdata[0] = 0; 170 ctf_buf_write(b, &fdata[0], sizeof (fdata[0])); 171 172 debug(3, "Wrote function (null)\n"); 173 return; 174 } 175 176 nargs = idp->ii_nargs + (idp->ii_vargs != 0); 177 fdata[0] = CTF_TYPE_INFO(CTF_K_FUNCTION, 1, nargs); 178 fdata[1] = idp->ii_dtype->t_id; 179 ctf_buf_write(b, fdata, sizeof (fdata)); 180 181 for (i = 0; i < idp->ii_nargs; i++) { 182 id = idp->ii_args[i]->t_id; 183 ctf_buf_write(b, &id, sizeof (id)); 184 } 185 186 if (idp->ii_vargs) { 187 id = 0; 188 ctf_buf_write(b, &id, sizeof (id)); 189 } 190 191 debug(3, "Wrote function %s (%d args)\n", idp->ii_name, nargs); 192 } 193 194 /* 195 * Depending on the size of the type being described, either a ctf_stype_t (for 196 * types with size < CTF_LSTRUCT_THRESH) or a ctf_type_t (all others) will be 197 * written. We isolate the determination here so the rest of the writer code 198 * doesn't need to care. 199 */ 200 static void 201 write_sized_type_rec(ctf_buf_t *b, ctf_type_t *ctt, size_t size) 202 { 203 if (size > CTF_MAX_SIZE) { 204 ctt->ctt_size = CTF_LSIZE_SENT; 205 ctt->ctt_lsizehi = CTF_SIZE_TO_LSIZE_HI(size); 206 ctt->ctt_lsizelo = CTF_SIZE_TO_LSIZE_LO(size); 207 ctf_buf_write(b, ctt, sizeof (*ctt)); 208 } else { 209 ctf_stype_t *cts = (ctf_stype_t *)ctt; 210 211 cts->ctt_size = (ushort_t)size; 212 ctf_buf_write(b, cts, sizeof (*cts)); 213 } 214 } 215 216 static void 217 write_unsized_type_rec(ctf_buf_t *b, ctf_type_t *ctt) 218 { 219 ctf_stype_t *cts = (ctf_stype_t *)ctt; 220 221 ctf_buf_write(b, cts, sizeof (*cts)); 222 } 223 224 static int 225 write_type(void *arg1, void *arg2) 226 { 227 tdesc_t *tp = arg1; 228 ctf_buf_t *b = arg2; 229 elist_t *ep; 230 mlist_t *mp; 231 intr_t *ip; 232 233 size_t offset; 234 uint_t encoding; 235 uint_t data; 236 int isroot = tp->t_flags & TDESC_F_ISROOT; 237 int i; 238 239 ctf_type_t ctt; 240 ctf_array_t cta; 241 ctf_member_t ctm; 242 ctf_lmember_t ctlm; 243 ctf_enum_t cte; 244 ushort_t id; 245 246 ctlm.ctlm_pad = 0; 247 248 /* 249 * There shouldn't be any holes in the type list (where a hole is 250 * defined as two consecutive tdescs without consecutive ids), but 251 * check for them just in case. If we do find holes, we need to make 252 * fake entries to fill the holes, or we won't be able to reconstruct 253 * the tree from the written data. 254 */ 255 if (++b->nptent < CTF_TYPE_TO_INDEX(tp->t_id)) { 256 debug(2, "genctf: type hole from %d < x < %d\n", 257 b->nptent - 1, CTF_TYPE_TO_INDEX(tp->t_id)); 258 259 ctt.ctt_name = CTF_TYPE_NAME(CTF_STRTAB_0, 0); 260 ctt.ctt_info = CTF_TYPE_INFO(0, 0, 0); 261 while (b->nptent < CTF_TYPE_TO_INDEX(tp->t_id)) { 262 write_sized_type_rec(b, &ctt, 0); 263 b->nptent++; 264 } 265 } 266 267 offset = strtab_insert(&b->ctb_strtab, tp->t_name); 268 ctt.ctt_name = CTF_TYPE_NAME(CTF_STRTAB_0, offset); 269 270 switch (tp->t_type) { 271 case INTRINSIC: 272 ip = tp->t_intr; 273 if (ip->intr_type == INTR_INT) 274 ctt.ctt_info = CTF_TYPE_INFO(CTF_K_INTEGER, 275 isroot, 1); 276 else 277 ctt.ctt_info = CTF_TYPE_INFO(CTF_K_FLOAT, isroot, 1); 278 write_sized_type_rec(b, &ctt, tp->t_size); 279 280 encoding = 0; 281 282 if (ip->intr_type == INTR_INT) { 283 if (ip->intr_signed) 284 encoding |= CTF_INT_SIGNED; 285 if (ip->intr_iformat == 'c') 286 encoding |= CTF_INT_CHAR; 287 else if (ip->intr_iformat == 'b') 288 encoding |= CTF_INT_BOOL; 289 else if (ip->intr_iformat == 'v') 290 encoding |= CTF_INT_VARARGS; 291 } else 292 encoding = ip->intr_fformat; 293 294 data = CTF_INT_DATA(encoding, ip->intr_offset, ip->intr_nbits); 295 ctf_buf_write(b, &data, sizeof (data)); 296 break; 297 298 case POINTER: 299 ctt.ctt_info = CTF_TYPE_INFO(CTF_K_POINTER, isroot, 0); 300 ctt.ctt_type = tp->t_tdesc->t_id; 301 write_unsized_type_rec(b, &ctt); 302 break; 303 304 case ARRAY: 305 ctt.ctt_info = CTF_TYPE_INFO(CTF_K_ARRAY, isroot, 1); 306 write_sized_type_rec(b, &ctt, tp->t_size); 307 308 cta.cta_contents = tp->t_ardef->ad_contents->t_id; 309 cta.cta_index = tp->t_ardef->ad_idxtype->t_id; 310 cta.cta_nelems = tp->t_ardef->ad_nelems; 311 ctf_buf_write(b, &cta, sizeof (cta)); 312 break; 313 314 case STRUCT: 315 case UNION: 316 for (i = 0, mp = tp->t_members; mp != NULL; mp = mp->ml_next) 317 i++; /* count up struct or union members */ 318 319 if (tp->t_type == STRUCT) 320 ctt.ctt_info = CTF_TYPE_INFO(CTF_K_STRUCT, isroot, i); 321 else 322 ctt.ctt_info = CTF_TYPE_INFO(CTF_K_UNION, isroot, i); 323 324 write_sized_type_rec(b, &ctt, tp->t_size); 325 326 if (tp->t_size < CTF_LSTRUCT_THRESH) { 327 for (mp = tp->t_members; mp != NULL; mp = mp->ml_next) { 328 offset = strtab_insert(&b->ctb_strtab, 329 mp->ml_name); 330 331 ctm.ctm_name = CTF_TYPE_NAME(CTF_STRTAB_0, 332 offset); 333 ctm.ctm_type = mp->ml_type->t_id; 334 ctm.ctm_offset = mp->ml_offset; 335 ctf_buf_write(b, &ctm, sizeof (ctm)); 336 } 337 } else { 338 for (mp = tp->t_members; mp != NULL; mp = mp->ml_next) { 339 offset = strtab_insert(&b->ctb_strtab, 340 mp->ml_name); 341 342 ctlm.ctlm_name = CTF_TYPE_NAME(CTF_STRTAB_0, 343 offset); 344 ctlm.ctlm_type = mp->ml_type->t_id; 345 ctlm.ctlm_offsethi = 346 CTF_OFFSET_TO_LMEMHI(mp->ml_offset); 347 ctlm.ctlm_offsetlo = 348 CTF_OFFSET_TO_LMEMLO(mp->ml_offset); 349 ctf_buf_write(b, &ctlm, sizeof (ctlm)); 350 } 351 } 352 break; 353 354 case ENUM: 355 for (i = 0, ep = tp->t_emem; ep != NULL; ep = ep->el_next) 356 i++; /* count up enum members */ 357 358 if (i > CTF_MAX_VLEN) { 359 warning("enum %s has too many values: %d > %d\n", 360 tdesc_name(tp), i, CTF_MAX_VLEN); 361 i = CTF_MAX_VLEN; 362 } 363 364 ctt.ctt_info = CTF_TYPE_INFO(CTF_K_ENUM, isroot, i); 365 write_sized_type_rec(b, &ctt, tp->t_size); 366 367 for (ep = tp->t_emem; ep != NULL && i > 0; ep = ep->el_next) { 368 offset = strtab_insert(&b->ctb_strtab, ep->el_name); 369 cte.cte_name = CTF_TYPE_NAME(CTF_STRTAB_0, offset); 370 cte.cte_value = ep->el_number; 371 ctf_buf_write(b, &cte, sizeof (cte)); 372 i--; 373 } 374 break; 375 376 case FORWARD: 377 ctt.ctt_info = CTF_TYPE_INFO(CTF_K_FORWARD, isroot, 0); 378 ctt.ctt_type = 0; 379 write_unsized_type_rec(b, &ctt); 380 break; 381 382 case TYPEDEF: 383 ctt.ctt_info = CTF_TYPE_INFO(CTF_K_TYPEDEF, isroot, 0); 384 ctt.ctt_type = tp->t_tdesc->t_id; 385 write_unsized_type_rec(b, &ctt); 386 break; 387 388 case VOLATILE: 389 ctt.ctt_info = CTF_TYPE_INFO(CTF_K_VOLATILE, isroot, 0); 390 ctt.ctt_type = tp->t_tdesc->t_id; 391 write_unsized_type_rec(b, &ctt); 392 break; 393 394 case CONST: 395 ctt.ctt_info = CTF_TYPE_INFO(CTF_K_CONST, isroot, 0); 396 ctt.ctt_type = tp->t_tdesc->t_id; 397 write_unsized_type_rec(b, &ctt); 398 break; 399 400 case FUNCTION: 401 ctt.ctt_info = CTF_TYPE_INFO(CTF_K_FUNCTION, isroot, 402 tp->t_fndef->fn_nargs + tp->t_fndef->fn_vargs); 403 ctt.ctt_type = tp->t_fndef->fn_ret->t_id; 404 write_unsized_type_rec(b, &ctt); 405 406 for (i = 0; i < (int) tp->t_fndef->fn_nargs; i++) { 407 id = tp->t_fndef->fn_args[i]->t_id; 408 ctf_buf_write(b, &id, sizeof (id)); 409 } 410 411 if (tp->t_fndef->fn_vargs) { 412 id = 0; 413 ctf_buf_write(b, &id, sizeof (id)); 414 i++; 415 } 416 417 if (i & 1) { 418 id = 0; 419 ctf_buf_write(b, &id, sizeof (id)); 420 } 421 break; 422 423 case RESTRICT: 424 ctt.ctt_info = CTF_TYPE_INFO(CTF_K_RESTRICT, isroot, 0); 425 ctt.ctt_type = tp->t_tdesc->t_id; 426 write_unsized_type_rec(b, &ctt); 427 break; 428 429 default: 430 warning("Can't write unknown type %d\n", tp->t_type); 431 } 432 433 debug(3, "Wrote type %d %s\n", tp->t_id, tdesc_name(tp)); 434 435 return (1); 436 } 437 438 typedef struct resbuf { 439 caddr_t rb_base; 440 caddr_t rb_ptr; 441 size_t rb_size; 442 z_stream rb_zstr; 443 } resbuf_t; 444 445 static void 446 rbzs_grow(resbuf_t *rb) 447 { 448 off_t ptroff = (caddr_t)rb->rb_zstr.next_out - rb->rb_base; 449 450 rb->rb_size += RES_BUF_CHUNK_SIZE; 451 rb->rb_base = xrealloc(rb->rb_base, rb->rb_size); 452 rb->rb_ptr = rb->rb_base + ptroff; 453 rb->rb_zstr.next_out = (Bytef *)(rb->rb_ptr); 454 rb->rb_zstr.avail_out += RES_BUF_CHUNK_SIZE; 455 } 456 457 static void 458 compress_start(resbuf_t *rb) 459 { 460 int rc; 461 462 rb->rb_zstr.zalloc = (alloc_func)0; 463 rb->rb_zstr.zfree = (free_func)0; 464 rb->rb_zstr.opaque = (voidpf)0; 465 466 if ((rc = deflateInit(&rb->rb_zstr, Z_BEST_COMPRESSION)) != Z_OK) 467 parseterminate("zlib start failed: %s", zError(rc)); 468 } 469 470 static ssize_t 471 compress_buffer(void *buf, size_t n, void *data) 472 { 473 resbuf_t *rb = (resbuf_t *)data; 474 int rc; 475 476 rb->rb_zstr.next_out = (Bytef *)rb->rb_ptr; 477 rb->rb_zstr.avail_out = rb->rb_size - (rb->rb_ptr - rb->rb_base); 478 rb->rb_zstr.next_in = buf; 479 rb->rb_zstr.avail_in = n; 480 481 while (rb->rb_zstr.avail_in) { 482 if (rb->rb_zstr.avail_out == 0) 483 rbzs_grow(rb); 484 485 if ((rc = deflate(&rb->rb_zstr, Z_NO_FLUSH)) != Z_OK) 486 parseterminate("zlib deflate failed: %s", zError(rc)); 487 } 488 rb->rb_ptr = (caddr_t)rb->rb_zstr.next_out; 489 490 return (n); 491 } 492 493 static void 494 compress_flush(resbuf_t *rb, int type) 495 { 496 int rc; 497 498 for (;;) { 499 if (rb->rb_zstr.avail_out == 0) 500 rbzs_grow(rb); 501 502 rc = deflate(&rb->rb_zstr, type); 503 if ((type == Z_FULL_FLUSH && rc == Z_BUF_ERROR) || 504 (type == Z_FINISH && rc == Z_STREAM_END)) 505 break; 506 else if (rc != Z_OK) 507 parseterminate("zlib finish failed: %s", zError(rc)); 508 } 509 rb->rb_ptr = (caddr_t)rb->rb_zstr.next_out; 510 } 511 512 static void 513 compress_end(resbuf_t *rb) 514 { 515 int rc; 516 517 compress_flush(rb, Z_FINISH); 518 519 if ((rc = deflateEnd(&rb->rb_zstr)) != Z_OK) 520 parseterminate("zlib end failed: %s", zError(rc)); 521 } 522 523 /* 524 * Pad the buffer to a power-of-2 boundary 525 */ 526 static void 527 pad_buffer(ctf_buf_t *buf, int align) 528 { 529 uint_t cur = ctf_buf_cur(buf); 530 ssize_t topad = (align - (cur % align)) % align; 531 static const char pad[8] = { 0 }; 532 533 while (topad > 0) { 534 ctf_buf_write(buf, pad, (topad > 8 ? 8 : topad)); 535 topad -= 8; 536 } 537 } 538 539 static ssize_t 540 bcopy_data(void *buf, size_t n, void *data) 541 { 542 caddr_t *posp = (caddr_t *)data; 543 bcopy(buf, *posp, n); 544 *posp += n; 545 return (n); 546 } 547 548 static caddr_t 549 write_buffer(ctf_header_t *h, ctf_buf_t *buf, size_t *resszp) 550 { 551 caddr_t outbuf; 552 caddr_t bufpos; 553 554 outbuf = xmalloc(sizeof (ctf_header_t) + (buf->ctb_ptr - buf->ctb_base) 555 + buf->ctb_strtab.str_size); 556 557 bufpos = outbuf; 558 (void) bcopy_data(h, sizeof (ctf_header_t), &bufpos); 559 (void) bcopy_data(buf->ctb_base, buf->ctb_ptr - buf->ctb_base, 560 &bufpos); 561 (void) strtab_write(&buf->ctb_strtab, bcopy_data, &bufpos); 562 *resszp = bufpos - outbuf; 563 return (outbuf); 564 } 565 566 /* 567 * Create the compression buffer, and fill it with the CTF and string 568 * table data. We flush the compression state between the two so the 569 * dictionary used for the string tables won't be polluted with values 570 * that made sense for the CTF data. 571 */ 572 static caddr_t 573 write_compressed_buffer(ctf_header_t *h, ctf_buf_t *buf, size_t *resszp) 574 { 575 resbuf_t resbuf; 576 resbuf.rb_size = RES_BUF_CHUNK_SIZE; 577 resbuf.rb_base = xmalloc(resbuf.rb_size); 578 bcopy(h, resbuf.rb_base, sizeof (ctf_header_t)); 579 resbuf.rb_ptr = resbuf.rb_base + sizeof (ctf_header_t); 580 581 compress_start(&resbuf); 582 (void) compress_buffer(buf->ctb_base, buf->ctb_ptr - buf->ctb_base, 583 &resbuf); 584 compress_flush(&resbuf, Z_FULL_FLUSH); 585 (void) strtab_write(&buf->ctb_strtab, compress_buffer, &resbuf); 586 compress_end(&resbuf); 587 588 *resszp = (resbuf.rb_ptr - resbuf.rb_base); 589 return (resbuf.rb_base); 590 } 591 592 caddr_t 593 ctf_gen(iiburst_t *iiburst, size_t *resszp, int do_compress) 594 { 595 ctf_buf_t *buf = ctf_buf_new(); 596 ctf_header_t h; 597 caddr_t outbuf; 598 599 int i; 600 601 /* 602 * Prepare the header, and create the CTF output buffers. The data 603 * object section and function section are both lists of 2-byte 604 * integers; we pad these out to the next 4-byte boundary if needed. 605 */ 606 h.cth_magic = CTF_MAGIC; 607 h.cth_version = CTF_VERSION; 608 h.cth_flags = do_compress ? CTF_F_COMPRESS : 0; 609 h.cth_parlabel = strtab_insert(&buf->ctb_strtab, 610 iiburst->iib_td->td_parlabel); 611 h.cth_parname = strtab_insert(&buf->ctb_strtab, 612 iiburst->iib_td->td_parname); 613 614 h.cth_lbloff = 0; 615 (void) list_iter(iiburst->iib_td->td_labels, write_label, 616 buf); 617 618 pad_buffer(buf, 2); 619 h.cth_objtoff = ctf_buf_cur(buf); 620 for (i = 0; i < iiburst->iib_nobjts; i++) 621 write_objects(iiburst->iib_objts[i], buf); 622 623 pad_buffer(buf, 2); 624 h.cth_funcoff = ctf_buf_cur(buf); 625 for (i = 0; i < iiburst->iib_nfuncs; i++) 626 write_functions(iiburst->iib_funcs[i], buf); 627 628 pad_buffer(buf, 4); 629 h.cth_typeoff = ctf_buf_cur(buf); 630 (void) list_iter(iiburst->iib_types, write_type, buf); 631 632 debug(2, "CTF wrote %d types\n", list_count(iiburst->iib_types)); 633 634 h.cth_stroff = ctf_buf_cur(buf); 635 h.cth_strlen = strtab_size(&buf->ctb_strtab); 636 637 /* 638 * We only do compression for ctfmerge, as ctfconvert is only 639 * supposed to be used on intermediary build objects. This is 640 * significantly faster. 641 */ 642 if (do_compress) 643 outbuf = write_compressed_buffer(&h, buf, resszp); 644 else 645 outbuf = write_buffer(&h, buf, resszp); 646 647 ctf_buf_free(buf); 648 return (outbuf); 649 } 650 651 static void 652 get_ctt_size(ctf_type_t *ctt, size_t *sizep, size_t *incrementp) 653 { 654 if (ctt->ctt_size == CTF_LSIZE_SENT) { 655 *sizep = (size_t)CTF_TYPE_LSIZE(ctt); 656 *incrementp = sizeof (ctf_type_t); 657 } else { 658 *sizep = ctt->ctt_size; 659 *incrementp = sizeof (ctf_stype_t); 660 } 661 } 662 663 static int 664 count_types(ctf_header_t *h, caddr_t data) 665 { 666 caddr_t dptr = data + h->cth_typeoff; 667 int count = 0; 668 669 dptr = data + h->cth_typeoff; 670 while (dptr < data + h->cth_stroff) { 671 void *v = (void *) dptr; 672 ctf_type_t *ctt = v; 673 size_t vlen = CTF_INFO_VLEN(ctt->ctt_info); 674 size_t size, increment; 675 676 get_ctt_size(ctt, &size, &increment); 677 678 switch (CTF_INFO_KIND(ctt->ctt_info)) { 679 case CTF_K_INTEGER: 680 case CTF_K_FLOAT: 681 dptr += 4; 682 break; 683 case CTF_K_POINTER: 684 case CTF_K_FORWARD: 685 case CTF_K_TYPEDEF: 686 case CTF_K_VOLATILE: 687 case CTF_K_CONST: 688 case CTF_K_RESTRICT: 689 case CTF_K_FUNCTION: 690 dptr += sizeof (ushort_t) * (vlen + (vlen & 1)); 691 break; 692 case CTF_K_ARRAY: 693 dptr += sizeof (ctf_array_t); 694 break; 695 case CTF_K_STRUCT: 696 case CTF_K_UNION: 697 if (size < CTF_LSTRUCT_THRESH) 698 dptr += sizeof (ctf_member_t) * vlen; 699 else 700 dptr += sizeof (ctf_lmember_t) * vlen; 701 break; 702 case CTF_K_ENUM: 703 dptr += sizeof (ctf_enum_t) * vlen; 704 break; 705 case CTF_K_UNKNOWN: 706 break; 707 default: 708 parseterminate("Unknown CTF type %d (#%d) at %#x", 709 CTF_INFO_KIND(ctt->ctt_info), count, dptr - data); 710 } 711 712 dptr += increment; 713 count++; 714 } 715 716 debug(3, "CTF read %d types\n", count); 717 718 return (count); 719 } 720 721 /* 722 * Resurrect the labels stored in the CTF data, returning the index associated 723 * with a label provided by the caller. There are several cases, outlined 724 * below. Note that, given two labels, the one associated with the lesser type 725 * index is considered to be older than the other. 726 * 727 * 1. matchlbl == NULL - return the index of the most recent label. 728 * 2. matchlbl == "BASE" - return the index of the oldest label. 729 * 3. matchlbl != NULL, but doesn't match any labels in the section - warn 730 * the user, and proceed as if matchlbl == "BASE" (for safety). 731 * 4. matchlbl != NULL, and matches one of the labels in the section - return 732 * the type index associated with the label. 733 */ 734 static int 735 resurrect_labels(ctf_header_t *h, tdata_t *td, caddr_t ctfdata, char *matchlbl) 736 { 737 caddr_t buf = ctfdata + h->cth_lbloff; 738 caddr_t sbuf = ctfdata + h->cth_stroff; 739 size_t bufsz = h->cth_objtoff - h->cth_lbloff; 740 int lastidx = 0, baseidx = -1; 741 char *baselabel = NULL; 742 ctf_lblent_t *ctl; 743 void *v = (void *) buf; 744 745 for (ctl = v; (caddr_t)ctl < buf + bufsz; ctl++) { 746 char *label = sbuf + ctl->ctl_label; 747 748 lastidx = ctl->ctl_typeidx; 749 750 debug(3, "Resurrected label %s type idx %d\n", label, lastidx); 751 752 tdata_label_add(td, label, lastidx); 753 754 if (baseidx == -1) { 755 baseidx = lastidx; 756 baselabel = label; 757 if (matchlbl != NULL && streq(matchlbl, "BASE")) 758 return (lastidx); 759 } 760 761 if (matchlbl != NULL && streq(label, matchlbl)) 762 return (lastidx); 763 } 764 765 if (matchlbl != NULL) { 766 /* User provided a label that didn't match */ 767 warning("%s: Cannot find label `%s' - using base (%s)\n", 768 curfile, matchlbl, (baselabel ? baselabel : "NONE")); 769 770 tdata_label_free(td); 771 tdata_label_add(td, baselabel, baseidx); 772 773 return (baseidx); 774 } 775 776 return (lastidx); 777 } 778 779 static void 780 resurrect_objects(ctf_header_t *h, tdata_t *td, tdesc_t **tdarr, int tdsize, 781 caddr_t ctfdata, symit_data_t *si) 782 { 783 caddr_t buf = ctfdata + h->cth_objtoff; 784 size_t bufsz = h->cth_funcoff - h->cth_objtoff; 785 caddr_t dptr; 786 787 symit_reset(si); 788 for (dptr = buf; dptr < buf + bufsz; dptr += 2) { 789 void *v = (void *) dptr; 790 ushort_t id = *((ushort_t *)v); 791 iidesc_t *ii; 792 GElf_Sym *sym; 793 794 if (!(sym = symit_next(si, STT_OBJECT)) && id != 0) { 795 parseterminate( 796 "Unexpected end of object symbols at %x of %x", 797 dptr - buf, bufsz); 798 } 799 800 if (id == 0) { 801 debug(3, "Skipping null object\n"); 802 continue; 803 } else if (id >= tdsize) { 804 parseterminate("Reference to invalid type %d", id); 805 } 806 807 ii = iidesc_new(symit_name(si)); 808 ii->ii_dtype = tdarr[id]; 809 if (GELF_ST_BIND(sym->st_info) == STB_LOCAL) { 810 ii->ii_type = II_SVAR; 811 ii->ii_owner = xstrdup(symit_curfile(si)); 812 } else 813 ii->ii_type = II_GVAR; 814 hash_add(td->td_iihash, ii); 815 816 debug(3, "Resurrected %s object %s (%d) from %s\n", 817 (ii->ii_type == II_GVAR ? "global" : "static"), 818 ii->ii_name, id, (ii->ii_owner ? ii->ii_owner : "(none)")); 819 } 820 } 821 822 static void 823 resurrect_functions(ctf_header_t *h, tdata_t *td, tdesc_t **tdarr, int tdsize, 824 caddr_t ctfdata, symit_data_t *si) 825 { 826 caddr_t buf = ctfdata + h->cth_funcoff; 827 size_t bufsz = h->cth_typeoff - h->cth_funcoff; 828 caddr_t dptr = buf; 829 iidesc_t *ii; 830 ushort_t info; 831 ushort_t retid; 832 GElf_Sym *sym; 833 int i; 834 835 symit_reset(si); 836 while (dptr < buf + bufsz) { 837 void *v = (void *) dptr; 838 info = *((ushort_t *)v); 839 dptr += 2; 840 841 if (!(sym = symit_next(si, STT_FUNC)) && info != 0) 842 parseterminate("Unexpected end of function symbols"); 843 844 if (info == 0) { 845 debug(3, "Skipping null function (%s)\n", 846 symit_name(si)); 847 continue; 848 } 849 850 v = (void *) dptr; 851 retid = *((ushort_t *)v); 852 dptr += 2; 853 854 if (retid >= tdsize) 855 parseterminate("Reference to invalid type %d", retid); 856 857 ii = iidesc_new(symit_name(si)); 858 ii->ii_dtype = tdarr[retid]; 859 if (GELF_ST_BIND(sym->st_info) == STB_LOCAL) { 860 ii->ii_type = II_SFUN; 861 ii->ii_owner = xstrdup(symit_curfile(si)); 862 } else 863 ii->ii_type = II_GFUN; 864 ii->ii_nargs = CTF_INFO_VLEN(info); 865 if (ii->ii_nargs) 866 ii->ii_args = 867 xmalloc(sizeof (tdesc_t *) * ii->ii_nargs); 868 869 for (i = 0; i < ii->ii_nargs; i++, dptr += 2) { 870 v = (void *) dptr; 871 ushort_t id = *((ushort_t *)v); 872 if (id >= tdsize) 873 parseterminate("Reference to invalid type %d", 874 id); 875 ii->ii_args[i] = tdarr[id]; 876 } 877 878 if (ii->ii_nargs && ii->ii_args[ii->ii_nargs - 1] == NULL) { 879 ii->ii_nargs--; 880 ii->ii_vargs = 1; 881 } 882 883 hash_add(td->td_iihash, ii); 884 885 debug(3, "Resurrected %s function %s (%d, %d args)\n", 886 (ii->ii_type == II_GFUN ? "global" : "static"), 887 ii->ii_name, retid, ii->ii_nargs); 888 } 889 } 890 891 static void 892 resurrect_types(ctf_header_t *h, tdata_t *td, tdesc_t **tdarr, int tdsize, 893 caddr_t ctfdata, int maxid) 894 { 895 caddr_t buf = ctfdata + h->cth_typeoff; 896 size_t bufsz = h->cth_stroff - h->cth_typeoff; 897 caddr_t sbuf = ctfdata + h->cth_stroff; 898 caddr_t dptr = buf; 899 tdesc_t *tdp; 900 uint_t data; 901 uint_t encoding; 902 size_t size, increment; 903 int tcnt; 904 int iicnt = 0; 905 tid_t tid, argid; 906 int kind, vlen; 907 int i; 908 909 elist_t **epp; 910 mlist_t **mpp; 911 intr_t *ip; 912 913 ctf_type_t *ctt; 914 ctf_array_t *cta; 915 ctf_enum_t *cte; 916 917 /* 918 * A maxid of zero indicates a request to resurrect all types, so reset 919 * maxid to the maximum type id. 920 */ 921 if (maxid == 0) 922 maxid = CTF_MAX_TYPE; 923 924 for (dptr = buf, tcnt = 0, tid = 1; dptr < buf + bufsz; tcnt++, tid++) { 925 if (tid > maxid) 926 break; 927 928 if (tid >= tdsize) 929 parseterminate("Reference to invalid type %d", tid); 930 931 void *v = (void *) dptr; 932 ctt = v; 933 934 get_ctt_size(ctt, &size, &increment); 935 dptr += increment; 936 937 tdp = tdarr[tid]; 938 939 if (CTF_NAME_STID(ctt->ctt_name) != CTF_STRTAB_0) 940 parseterminate( 941 "Unable to cope with non-zero strtab id"); 942 if (CTF_NAME_OFFSET(ctt->ctt_name) != 0) { 943 tdp->t_name = 944 xstrdup(sbuf + CTF_NAME_OFFSET(ctt->ctt_name)); 945 } else 946 tdp->t_name = NULL; 947 948 kind = CTF_INFO_KIND(ctt->ctt_info); 949 vlen = CTF_INFO_VLEN(ctt->ctt_info); 950 951 switch (kind) { 952 case CTF_K_INTEGER: 953 tdp->t_type = INTRINSIC; 954 tdp->t_size = size; 955 956 v = (void *) dptr; 957 data = *((uint_t *)v); 958 dptr += sizeof (uint_t); 959 encoding = CTF_INT_ENCODING(data); 960 961 ip = xmalloc(sizeof (intr_t)); 962 ip->intr_type = INTR_INT; 963 ip->intr_signed = (encoding & CTF_INT_SIGNED) ? 1 : 0; 964 965 if (encoding & CTF_INT_CHAR) 966 ip->intr_iformat = 'c'; 967 else if (encoding & CTF_INT_BOOL) 968 ip->intr_iformat = 'b'; 969 else if (encoding & CTF_INT_VARARGS) 970 ip->intr_iformat = 'v'; 971 else 972 ip->intr_iformat = '\0'; 973 974 ip->intr_offset = CTF_INT_OFFSET(data); 975 ip->intr_nbits = CTF_INT_BITS(data); 976 tdp->t_intr = ip; 977 break; 978 979 case CTF_K_FLOAT: 980 tdp->t_type = INTRINSIC; 981 tdp->t_size = size; 982 983 v = (void *) dptr; 984 data = *((uint_t *)v); 985 dptr += sizeof (uint_t); 986 987 ip = xcalloc(sizeof (intr_t)); 988 ip->intr_type = INTR_REAL; 989 ip->intr_fformat = CTF_FP_ENCODING(data); 990 ip->intr_offset = CTF_FP_OFFSET(data); 991 ip->intr_nbits = CTF_FP_BITS(data); 992 tdp->t_intr = ip; 993 break; 994 995 case CTF_K_POINTER: 996 tdp->t_type = POINTER; 997 tdp->t_tdesc = tdarr[ctt->ctt_type]; 998 break; 999 1000 case CTF_K_ARRAY: 1001 tdp->t_type = ARRAY; 1002 tdp->t_size = size; 1003 1004 v = (void *) dptr; 1005 cta = v; 1006 dptr += sizeof (ctf_array_t); 1007 1008 tdp->t_ardef = xmalloc(sizeof (ardef_t)); 1009 tdp->t_ardef->ad_contents = tdarr[cta->cta_contents]; 1010 tdp->t_ardef->ad_idxtype = tdarr[cta->cta_index]; 1011 tdp->t_ardef->ad_nelems = cta->cta_nelems; 1012 break; 1013 1014 case CTF_K_STRUCT: 1015 case CTF_K_UNION: 1016 tdp->t_type = (kind == CTF_K_STRUCT ? STRUCT : UNION); 1017 tdp->t_size = size; 1018 1019 if (size < CTF_LSTRUCT_THRESH) { 1020 for (i = 0, mpp = &tdp->t_members; i < vlen; 1021 i++, mpp = &((*mpp)->ml_next)) { 1022 v = (void *) dptr; 1023 ctf_member_t *ctm = v; 1024 dptr += sizeof (ctf_member_t); 1025 1026 *mpp = xmalloc(sizeof (mlist_t)); 1027 (*mpp)->ml_name = xstrdup(sbuf + 1028 ctm->ctm_name); 1029 (*mpp)->ml_type = tdarr[ctm->ctm_type]; 1030 (*mpp)->ml_offset = ctm->ctm_offset; 1031 (*mpp)->ml_size = 0; 1032 } 1033 } else { 1034 for (i = 0, mpp = &tdp->t_members; i < vlen; 1035 i++, mpp = &((*mpp)->ml_next)) { 1036 v = (void *) dptr; 1037 ctf_lmember_t *ctlm = v; 1038 dptr += sizeof (ctf_lmember_t); 1039 1040 *mpp = xmalloc(sizeof (mlist_t)); 1041 (*mpp)->ml_name = xstrdup(sbuf + 1042 ctlm->ctlm_name); 1043 (*mpp)->ml_type = 1044 tdarr[ctlm->ctlm_type]; 1045 (*mpp)->ml_offset = 1046 (int)CTF_LMEM_OFFSET(ctlm); 1047 (*mpp)->ml_size = 0; 1048 } 1049 } 1050 1051 *mpp = NULL; 1052 break; 1053 1054 case CTF_K_ENUM: 1055 tdp->t_type = ENUM; 1056 tdp->t_size = size; 1057 1058 for (i = 0, epp = &tdp->t_emem; i < vlen; 1059 i++, epp = &((*epp)->el_next)) { 1060 v = (void *) dptr; 1061 cte = v; 1062 dptr += sizeof (ctf_enum_t); 1063 1064 *epp = xmalloc(sizeof (elist_t)); 1065 (*epp)->el_name = xstrdup(sbuf + cte->cte_name); 1066 (*epp)->el_number = cte->cte_value; 1067 } 1068 *epp = NULL; 1069 break; 1070 1071 case CTF_K_FORWARD: 1072 tdp->t_type = FORWARD; 1073 list_add(&td->td_fwdlist, tdp); 1074 break; 1075 1076 case CTF_K_TYPEDEF: 1077 tdp->t_type = TYPEDEF; 1078 tdp->t_tdesc = tdarr[ctt->ctt_type]; 1079 break; 1080 1081 case CTF_K_VOLATILE: 1082 tdp->t_type = VOLATILE; 1083 tdp->t_tdesc = tdarr[ctt->ctt_type]; 1084 break; 1085 1086 case CTF_K_CONST: 1087 tdp->t_type = CONST; 1088 tdp->t_tdesc = tdarr[ctt->ctt_type]; 1089 break; 1090 1091 case CTF_K_FUNCTION: 1092 tdp->t_type = FUNCTION; 1093 tdp->t_fndef = xcalloc(sizeof (fndef_t)); 1094 tdp->t_fndef->fn_ret = tdarr[ctt->ctt_type]; 1095 1096 v = (void *) (dptr + (sizeof (ushort_t) * (vlen - 1))); 1097 if (vlen > 0 && *(ushort_t *)v == 0) 1098 tdp->t_fndef->fn_vargs = 1; 1099 1100 tdp->t_fndef->fn_nargs = vlen - tdp->t_fndef->fn_vargs; 1101 tdp->t_fndef->fn_args = xcalloc(sizeof (tdesc_t) * 1102 vlen - tdp->t_fndef->fn_vargs); 1103 1104 for (i = 0; i < vlen; i++) { 1105 v = (void *) dptr; 1106 argid = *(ushort_t *)v; 1107 dptr += sizeof (ushort_t); 1108 1109 if (argid != 0) 1110 tdp->t_fndef->fn_args[i] = tdarr[argid]; 1111 } 1112 1113 if (vlen & 1) 1114 dptr += sizeof (ushort_t); 1115 break; 1116 1117 case CTF_K_RESTRICT: 1118 tdp->t_type = RESTRICT; 1119 tdp->t_tdesc = tdarr[ctt->ctt_type]; 1120 break; 1121 1122 case CTF_K_UNKNOWN: 1123 break; 1124 1125 default: 1126 warning("Can't parse unknown CTF type %d\n", kind); 1127 } 1128 1129 if (CTF_INFO_ISROOT(ctt->ctt_info)) { 1130 iidesc_t *ii = iidesc_new(tdp->t_name); 1131 if (tdp->t_type == STRUCT || tdp->t_type == UNION || 1132 tdp->t_type == ENUM) 1133 ii->ii_type = II_SOU; 1134 else 1135 ii->ii_type = II_TYPE; 1136 ii->ii_dtype = tdp; 1137 hash_add(td->td_iihash, ii); 1138 1139 iicnt++; 1140 } 1141 1142 debug(3, "Resurrected %d %stype %s (%d)\n", tdp->t_type, 1143 (CTF_INFO_ISROOT(ctt->ctt_info) ? "root " : ""), 1144 tdesc_name(tdp), tdp->t_id); 1145 } 1146 1147 debug(3, "Resurrected %d types (%d were roots)\n", tcnt, iicnt); 1148 } 1149 1150 /* 1151 * For lack of other inspiration, we're going to take the boring route. We 1152 * count the number of types. This lets us malloc that many tdesc structs 1153 * before we start filling them in. This has the advantage of allowing us to 1154 * avoid a merge-esque remap step. 1155 */ 1156 static tdata_t * 1157 ctf_parse(ctf_header_t *h, caddr_t buf, symit_data_t *si, char *label) 1158 { 1159 tdata_t *td = tdata_new(); 1160 tdesc_t **tdarr; 1161 int ntypes = count_types(h, buf); 1162 int idx, i; 1163 1164 /* shudder */ 1165 tdarr = xcalloc(sizeof (tdesc_t *) * (ntypes + 1)); 1166 tdarr[0] = NULL; 1167 for (i = 1; i <= ntypes; i++) { 1168 tdarr[i] = xcalloc(sizeof (tdesc_t)); 1169 tdarr[i]->t_id = i; 1170 } 1171 1172 td->td_parlabel = xstrdup(buf + h->cth_stroff + h->cth_parlabel); 1173 1174 /* we have the technology - we can rebuild them */ 1175 idx = resurrect_labels(h, td, buf, label); 1176 1177 resurrect_objects(h, td, tdarr, ntypes + 1, buf, si); 1178 resurrect_functions(h, td, tdarr, ntypes + 1, buf, si); 1179 resurrect_types(h, td, tdarr, ntypes + 1, buf, idx); 1180 1181 free(tdarr); 1182 1183 td->td_nextid = ntypes + 1; 1184 1185 return (td); 1186 } 1187 1188 static size_t 1189 decompress_ctf(caddr_t cbuf, size_t cbufsz, caddr_t dbuf, size_t dbufsz) 1190 { 1191 z_stream zstr; 1192 int rc; 1193 1194 zstr.zalloc = (alloc_func)0; 1195 zstr.zfree = (free_func)0; 1196 zstr.opaque = (voidpf)0; 1197 1198 zstr.next_in = (Bytef *)cbuf; 1199 zstr.avail_in = cbufsz; 1200 zstr.next_out = (Bytef *)dbuf; 1201 zstr.avail_out = dbufsz; 1202 1203 if ((rc = inflateInit(&zstr)) != Z_OK || 1204 (rc = inflate(&zstr, Z_NO_FLUSH)) != Z_STREAM_END || 1205 (rc = inflateEnd(&zstr)) != Z_OK) { 1206 warning("CTF decompress zlib error %s\n", zError(rc)); 1207 return (0); 1208 } 1209 1210 debug(3, "reflated %lu bytes to %lu, pointer at %d\n", 1211 zstr.total_in, zstr.total_out, (caddr_t)zstr.next_in - cbuf); 1212 1213 return (zstr.total_out); 1214 } 1215 1216 /* 1217 * Reconstruct the type tree from a given buffer of CTF data. Only the types 1218 * up to the type associated with the provided label, inclusive, will be 1219 * reconstructed. If a NULL label is provided, all types will be reconstructed. 1220 * 1221 * This function won't work on files that have been uniquified. 1222 */ 1223 tdata_t * 1224 ctf_load(char *file, caddr_t buf, size_t bufsz, symit_data_t *si, char *label) 1225 { 1226 ctf_header_t *h; 1227 caddr_t ctfdata; 1228 size_t ctfdatasz; 1229 tdata_t *td; 1230 1231 curfile = file; 1232 1233 if (bufsz < sizeof (ctf_header_t)) 1234 parseterminate("Corrupt CTF - short header"); 1235 1236 void *v = (void *) buf; 1237 h = v; 1238 buf += sizeof (ctf_header_t); 1239 bufsz -= sizeof (ctf_header_t); 1240 1241 if (h->cth_magic != CTF_MAGIC) 1242 parseterminate("Corrupt CTF - bad magic 0x%x", h->cth_magic); 1243 1244 if (h->cth_version != CTF_VERSION) 1245 parseterminate("Unknown CTF version %d", h->cth_version); 1246 1247 ctfdatasz = h->cth_stroff + h->cth_strlen; 1248 if (h->cth_flags & CTF_F_COMPRESS) { 1249 size_t actual; 1250 1251 ctfdata = xmalloc(ctfdatasz); 1252 if ((actual = decompress_ctf(buf, bufsz, ctfdata, ctfdatasz)) != 1253 ctfdatasz) { 1254 parseterminate("Corrupt CTF - short decompression " 1255 "(was %d, expecting %d)", actual, ctfdatasz); 1256 } 1257 } else { 1258 ctfdata = buf; 1259 ctfdatasz = bufsz; 1260 } 1261 1262 td = ctf_parse(h, ctfdata, si, label); 1263 1264 if (h->cth_flags & CTF_F_COMPRESS) 1265 free(ctfdata); 1266 1267 curfile = NULL; 1268 1269 return (td); 1270 } 1271