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