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