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, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 23 /* 24 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 25 * Use is subject to license terms. 26 */ 27 /* 28 * Copyright (c) 2013, Joyent, Inc. All rights reserved. 29 */ 30 31 #include <ctf_impl.h> 32 #include <sys/mman.h> 33 #include <sys/zmod.h> 34 35 static const ctf_dmodel_t _libctf_models[] = { 36 { "ILP32", CTF_MODEL_ILP32, 4, 1, 2, 4, 4 }, 37 { "LP64", CTF_MODEL_LP64, 8, 1, 2, 4, 8 }, 38 { NULL, 0, 0, 0, 0, 0, 0 } 39 }; 40 41 const char _CTF_SECTION[] = ".SUNW_ctf"; 42 const char _CTF_NULLSTR[] = ""; 43 44 int _libctf_version = CTF_VERSION; /* library client version */ 45 int _libctf_debug = 0; /* debugging messages enabled */ 46 47 static ushort_t 48 get_kind_v2(ushort_t info) 49 { 50 return (CTF_INFO_KIND(info)); 51 } 52 53 static ushort_t 54 get_root_v2(ushort_t info) 55 { 56 return (CTF_INFO_ISROOT(info)); 57 } 58 59 static ushort_t 60 get_vlen_v2(ushort_t info) 61 { 62 return (CTF_INFO_VLEN(info)); 63 } 64 65 static const ctf_fileops_t ctf_fileops[] = { 66 { NULL, NULL }, 67 { NULL, NULL }, 68 { get_kind_v2, get_root_v2, get_vlen_v2 }, 69 }; 70 71 /* 72 * Convert a 32-bit ELF symbol into GElf (Elf64) and return a pointer to it. 73 */ 74 static Elf64_Sym * 75 sym_to_gelf(const Elf32_Sym *src, Elf64_Sym *dst) 76 { 77 dst->st_name = src->st_name; 78 dst->st_value = src->st_value; 79 dst->st_size = src->st_size; 80 dst->st_info = src->st_info; 81 dst->st_other = src->st_other; 82 dst->st_shndx = src->st_shndx; 83 84 return (dst); 85 } 86 87 /* 88 * Initialize the symtab translation table by filling each entry with the 89 * offset of the CTF type or function data corresponding to each STT_FUNC or 90 * STT_OBJECT entry in the symbol table. 91 */ 92 static int 93 init_symtab(ctf_file_t *fp, const ctf_header_t *hp, 94 const ctf_sect_t *sp, const ctf_sect_t *strp) 95 { 96 const uchar_t *symp = sp->cts_data; 97 uint_t *xp = fp->ctf_sxlate; 98 uint_t *xend = xp + fp->ctf_nsyms; 99 100 uint_t objtoff = hp->cth_objtoff; 101 uint_t funcoff = hp->cth_funcoff; 102 103 ushort_t info, vlen; 104 Elf64_Sym sym, *gsp; 105 const char *name; 106 107 /* 108 * The CTF data object and function type sections are ordered to match 109 * the relative order of the respective symbol types in the symtab. 110 * If no type information is available for a symbol table entry, a 111 * pad is inserted in the CTF section. As a further optimization, 112 * anonymous or undefined symbols are omitted from the CTF data. 113 */ 114 for (; xp < xend; xp++, symp += sp->cts_entsize) { 115 if (sp->cts_entsize == sizeof (Elf32_Sym)) 116 gsp = sym_to_gelf((Elf32_Sym *)(uintptr_t)symp, &sym); 117 else 118 gsp = (Elf64_Sym *)(uintptr_t)symp; 119 120 if (gsp->st_name < strp->cts_size) 121 name = (const char *)strp->cts_data + gsp->st_name; 122 else 123 name = _CTF_NULLSTR; 124 125 if (gsp->st_name == 0 || gsp->st_shndx == SHN_UNDEF || 126 strcmp(name, "_START_") == 0 || 127 strcmp(name, "_END_") == 0) { 128 *xp = -1u; 129 continue; 130 } 131 132 switch (ELF64_ST_TYPE(gsp->st_info)) { 133 case STT_OBJECT: 134 if (objtoff >= hp->cth_funcoff || 135 (gsp->st_shndx == SHN_ABS && gsp->st_value == 0)) { 136 *xp = -1u; 137 break; 138 } 139 140 *xp = objtoff; 141 objtoff += sizeof (ushort_t); 142 break; 143 144 case STT_FUNC: 145 if (funcoff >= hp->cth_typeoff) { 146 *xp = -1u; 147 break; 148 } 149 150 *xp = funcoff; 151 152 info = *(ushort_t *)((uintptr_t)fp->ctf_buf + funcoff); 153 vlen = LCTF_INFO_VLEN(fp, info); 154 155 /* 156 * If we encounter a zero pad at the end, just skip it. 157 * Otherwise skip over the function and its return type 158 * (+2) and the argument list (vlen). 159 */ 160 if (LCTF_INFO_KIND(fp, info) == CTF_K_UNKNOWN && 161 vlen == 0) 162 funcoff += sizeof (ushort_t); /* skip pad */ 163 else 164 funcoff += sizeof (ushort_t) * (vlen + 2); 165 break; 166 167 default: 168 *xp = -1u; 169 break; 170 } 171 } 172 173 ctf_dprintf("loaded %lu symtab entries\n", fp->ctf_nsyms); 174 return (0); 175 } 176 177 /* 178 * Initialize the type ID translation table with the byte offset of each type, 179 * and initialize the hash tables of each named type. 180 */ 181 static int 182 init_types(ctf_file_t *fp, const ctf_header_t *cth) 183 { 184 /* LINTED - pointer alignment */ 185 const ctf_type_t *tbuf = (ctf_type_t *)(fp->ctf_buf + cth->cth_typeoff); 186 /* LINTED - pointer alignment */ 187 const ctf_type_t *tend = (ctf_type_t *)(fp->ctf_buf + cth->cth_stroff); 188 189 ulong_t pop[CTF_K_MAX + 1] = { 0 }; 190 const ctf_type_t *tp; 191 ctf_hash_t *hp; 192 ushort_t id, dst; 193 uint_t *xp; 194 195 /* 196 * We initially determine whether the container is a child or a parent 197 * based on the value of cth_parname. To support containers that pre- 198 * date cth_parname, we also scan the types themselves for references 199 * to values in the range reserved for child types in our first pass. 200 */ 201 int child = cth->cth_parname != 0; 202 int nlstructs = 0, nlunions = 0; 203 int err; 204 205 /* 206 * We make two passes through the entire type section. In this first 207 * pass, we count the number of each type and the total number of types. 208 */ 209 for (tp = tbuf; tp < tend; fp->ctf_typemax++) { 210 ushort_t kind = LCTF_INFO_KIND(fp, tp->ctt_info); 211 ulong_t vlen = LCTF_INFO_VLEN(fp, tp->ctt_info); 212 ssize_t size, increment; 213 214 size_t vbytes; 215 uint_t n; 216 217 (void) ctf_get_ctt_size(fp, tp, &size, &increment); 218 219 switch (kind) { 220 case CTF_K_INTEGER: 221 case CTF_K_FLOAT: 222 vbytes = sizeof (uint_t); 223 break; 224 case CTF_K_ARRAY: 225 vbytes = sizeof (ctf_array_t); 226 break; 227 case CTF_K_FUNCTION: 228 vbytes = sizeof (ushort_t) * (vlen + (vlen & 1)); 229 break; 230 case CTF_K_STRUCT: 231 case CTF_K_UNION: 232 if (fp->ctf_version == CTF_VERSION_1 || 233 size < CTF_LSTRUCT_THRESH) { 234 ctf_member_t *mp = (ctf_member_t *) 235 ((uintptr_t)tp + increment); 236 237 vbytes = sizeof (ctf_member_t) * vlen; 238 for (n = vlen; n != 0; n--, mp++) 239 child |= CTF_TYPE_ISCHILD(mp->ctm_type); 240 } else { 241 ctf_lmember_t *lmp = (ctf_lmember_t *) 242 ((uintptr_t)tp + increment); 243 244 vbytes = sizeof (ctf_lmember_t) * vlen; 245 for (n = vlen; n != 0; n--, lmp++) 246 child |= 247 CTF_TYPE_ISCHILD(lmp->ctlm_type); 248 } 249 break; 250 case CTF_K_ENUM: 251 vbytes = sizeof (ctf_enum_t) * vlen; 252 break; 253 case CTF_K_FORWARD: 254 /* 255 * For forward declarations, ctt_type is the CTF_K_* 256 * kind for the tag, so bump that population count too. 257 * If ctt_type is unknown, treat the tag as a struct. 258 */ 259 if (tp->ctt_type == CTF_K_UNKNOWN || 260 tp->ctt_type >= CTF_K_MAX) 261 pop[CTF_K_STRUCT]++; 262 else 263 pop[tp->ctt_type]++; 264 /*FALLTHRU*/ 265 case CTF_K_UNKNOWN: 266 vbytes = 0; 267 break; 268 case CTF_K_POINTER: 269 case CTF_K_TYPEDEF: 270 case CTF_K_VOLATILE: 271 case CTF_K_CONST: 272 case CTF_K_RESTRICT: 273 child |= CTF_TYPE_ISCHILD(tp->ctt_type); 274 vbytes = 0; 275 break; 276 default: 277 ctf_dprintf("detected invalid CTF kind -- %u\n", kind); 278 return (ECTF_CORRUPT); 279 } 280 tp = (ctf_type_t *)((uintptr_t)tp + increment + vbytes); 281 pop[kind]++; 282 } 283 284 /* 285 * If we detected a reference to a child type ID, then we know this 286 * container is a child and may have a parent's types imported later. 287 */ 288 if (child) { 289 ctf_dprintf("CTF container %p is a child\n", (void *)fp); 290 fp->ctf_flags |= LCTF_CHILD; 291 } else 292 ctf_dprintf("CTF container %p is a parent\n", (void *)fp); 293 294 /* 295 * Now that we've counted up the number of each type, we can allocate 296 * the hash tables, type translation table, and pointer table. 297 */ 298 if ((err = ctf_hash_create(&fp->ctf_structs, pop[CTF_K_STRUCT])) != 0) 299 return (err); 300 301 if ((err = ctf_hash_create(&fp->ctf_unions, pop[CTF_K_UNION])) != 0) 302 return (err); 303 304 if ((err = ctf_hash_create(&fp->ctf_enums, pop[CTF_K_ENUM])) != 0) 305 return (err); 306 307 if ((err = ctf_hash_create(&fp->ctf_names, 308 pop[CTF_K_INTEGER] + pop[CTF_K_FLOAT] + pop[CTF_K_FUNCTION] + 309 pop[CTF_K_TYPEDEF] + pop[CTF_K_POINTER] + pop[CTF_K_VOLATILE] + 310 pop[CTF_K_CONST] + pop[CTF_K_RESTRICT])) != 0) 311 return (err); 312 313 fp->ctf_txlate = ctf_alloc(sizeof (uint_t) * (fp->ctf_typemax + 1)); 314 fp->ctf_ptrtab = ctf_alloc(sizeof (ushort_t) * (fp->ctf_typemax + 1)); 315 316 if (fp->ctf_txlate == NULL || fp->ctf_ptrtab == NULL) 317 return (EAGAIN); /* memory allocation failed */ 318 319 xp = fp->ctf_txlate; 320 *xp++ = 0; /* type id 0 is used as a sentinel value */ 321 322 bzero(fp->ctf_txlate, sizeof (uint_t) * (fp->ctf_typemax + 1)); 323 bzero(fp->ctf_ptrtab, sizeof (ushort_t) * (fp->ctf_typemax + 1)); 324 325 /* 326 * In the second pass through the types, we fill in each entry of the 327 * type and pointer tables and add names to the appropriate hashes. 328 */ 329 for (id = 1, tp = tbuf; tp < tend; xp++, id++) { 330 ushort_t kind = LCTF_INFO_KIND(fp, tp->ctt_info); 331 ulong_t vlen = LCTF_INFO_VLEN(fp, tp->ctt_info); 332 ssize_t size, increment; 333 334 const char *name; 335 size_t vbytes; 336 ctf_helem_t *hep; 337 ctf_encoding_t cte; 338 339 (void) ctf_get_ctt_size(fp, tp, &size, &increment); 340 name = ctf_strptr(fp, tp->ctt_name); 341 342 switch (kind) { 343 case CTF_K_INTEGER: 344 case CTF_K_FLOAT: 345 /* 346 * Only insert a new integer base type definition if 347 * this type name has not been defined yet. We re-use 348 * the names with different encodings for bit-fields. 349 */ 350 if ((hep = ctf_hash_lookup(&fp->ctf_names, fp, 351 name, strlen(name))) == NULL) { 352 err = ctf_hash_insert(&fp->ctf_names, fp, 353 CTF_INDEX_TO_TYPE(id, child), tp->ctt_name); 354 if (err != 0 && err != ECTF_STRTAB) 355 return (err); 356 } else if (ctf_type_encoding(fp, hep->h_type, 357 &cte) == 0 && cte.cte_bits == 0) { 358 /* 359 * Work-around SOS8 stabs bug: replace existing 360 * intrinsic w/ same name if it was zero bits. 361 */ 362 hep->h_type = CTF_INDEX_TO_TYPE(id, child); 363 } 364 vbytes = sizeof (uint_t); 365 break; 366 367 case CTF_K_ARRAY: 368 vbytes = sizeof (ctf_array_t); 369 break; 370 371 case CTF_K_FUNCTION: 372 err = ctf_hash_insert(&fp->ctf_names, fp, 373 CTF_INDEX_TO_TYPE(id, child), tp->ctt_name); 374 if (err != 0 && err != ECTF_STRTAB) 375 return (err); 376 vbytes = sizeof (ushort_t) * (vlen + (vlen & 1)); 377 break; 378 379 case CTF_K_STRUCT: 380 err = ctf_hash_define(&fp->ctf_structs, fp, 381 CTF_INDEX_TO_TYPE(id, child), tp->ctt_name); 382 383 if (err != 0 && err != ECTF_STRTAB) 384 return (err); 385 386 if (fp->ctf_version == CTF_VERSION_1 || 387 size < CTF_LSTRUCT_THRESH) 388 vbytes = sizeof (ctf_member_t) * vlen; 389 else { 390 vbytes = sizeof (ctf_lmember_t) * vlen; 391 nlstructs++; 392 } 393 break; 394 395 case CTF_K_UNION: 396 err = ctf_hash_define(&fp->ctf_unions, fp, 397 CTF_INDEX_TO_TYPE(id, child), tp->ctt_name); 398 399 if (err != 0 && err != ECTF_STRTAB) 400 return (err); 401 402 if (fp->ctf_version == CTF_VERSION_1 || 403 size < CTF_LSTRUCT_THRESH) 404 vbytes = sizeof (ctf_member_t) * vlen; 405 else { 406 vbytes = sizeof (ctf_lmember_t) * vlen; 407 nlunions++; 408 } 409 break; 410 411 case CTF_K_ENUM: 412 err = ctf_hash_define(&fp->ctf_enums, fp, 413 CTF_INDEX_TO_TYPE(id, child), tp->ctt_name); 414 415 if (err != 0 && err != ECTF_STRTAB) 416 return (err); 417 418 vbytes = sizeof (ctf_enum_t) * vlen; 419 break; 420 421 case CTF_K_TYPEDEF: 422 err = ctf_hash_insert(&fp->ctf_names, fp, 423 CTF_INDEX_TO_TYPE(id, child), tp->ctt_name); 424 if (err != 0 && err != ECTF_STRTAB) 425 return (err); 426 vbytes = 0; 427 break; 428 429 case CTF_K_FORWARD: 430 /* 431 * Only insert forward tags into the given hash if the 432 * type or tag name is not already present. 433 */ 434 switch (tp->ctt_type) { 435 case CTF_K_STRUCT: 436 hp = &fp->ctf_structs; 437 break; 438 case CTF_K_UNION: 439 hp = &fp->ctf_unions; 440 break; 441 case CTF_K_ENUM: 442 hp = &fp->ctf_enums; 443 break; 444 default: 445 hp = &fp->ctf_structs; 446 } 447 448 if (ctf_hash_lookup(hp, fp, 449 name, strlen(name)) == NULL) { 450 err = ctf_hash_insert(hp, fp, 451 CTF_INDEX_TO_TYPE(id, child), tp->ctt_name); 452 if (err != 0 && err != ECTF_STRTAB) 453 return (err); 454 } 455 vbytes = 0; 456 break; 457 458 case CTF_K_POINTER: 459 /* 460 * If the type referenced by the pointer is in this CTF 461 * container, then store the index of the pointer type 462 * in fp->ctf_ptrtab[ index of referenced type ]. 463 */ 464 if (CTF_TYPE_ISCHILD(tp->ctt_type) == child && 465 CTF_TYPE_TO_INDEX(tp->ctt_type) <= fp->ctf_typemax) 466 fp->ctf_ptrtab[ 467 CTF_TYPE_TO_INDEX(tp->ctt_type)] = id; 468 /*FALLTHRU*/ 469 470 case CTF_K_VOLATILE: 471 case CTF_K_CONST: 472 case CTF_K_RESTRICT: 473 err = ctf_hash_insert(&fp->ctf_names, fp, 474 CTF_INDEX_TO_TYPE(id, child), tp->ctt_name); 475 if (err != 0 && err != ECTF_STRTAB) 476 return (err); 477 /*FALLTHRU*/ 478 479 default: 480 vbytes = 0; 481 break; 482 } 483 484 *xp = (uint_t)((uintptr_t)tp - (uintptr_t)fp->ctf_buf); 485 tp = (ctf_type_t *)((uintptr_t)tp + increment + vbytes); 486 } 487 488 ctf_dprintf("%lu total types processed\n", fp->ctf_typemax); 489 ctf_dprintf("%u enum names hashed\n", ctf_hash_size(&fp->ctf_enums)); 490 ctf_dprintf("%u struct names hashed (%d long)\n", 491 ctf_hash_size(&fp->ctf_structs), nlstructs); 492 ctf_dprintf("%u union names hashed (%d long)\n", 493 ctf_hash_size(&fp->ctf_unions), nlunions); 494 ctf_dprintf("%u base type names hashed\n", 495 ctf_hash_size(&fp->ctf_names)); 496 497 /* 498 * Make an additional pass through the pointer table to find pointers 499 * that point to anonymous typedef nodes. If we find one, modify the 500 * pointer table so that the pointer is also known to point to the 501 * node that is referenced by the anonymous typedef node. 502 */ 503 for (id = 1; id <= fp->ctf_typemax; id++) { 504 if ((dst = fp->ctf_ptrtab[id]) != 0) { 505 tp = LCTF_INDEX_TO_TYPEPTR(fp, id); 506 507 if (LCTF_INFO_KIND(fp, tp->ctt_info) == CTF_K_TYPEDEF && 508 strcmp(ctf_strptr(fp, tp->ctt_name), "") == 0 && 509 CTF_TYPE_ISCHILD(tp->ctt_type) == child && 510 CTF_TYPE_TO_INDEX(tp->ctt_type) <= fp->ctf_typemax) 511 fp->ctf_ptrtab[ 512 CTF_TYPE_TO_INDEX(tp->ctt_type)] = dst; 513 } 514 } 515 516 return (0); 517 } 518 519 /* 520 * Decode the specified CTF buffer and optional symbol table and create a new 521 * CTF container representing the symbolic debugging information. This code 522 * can be used directly by the debugger, or it can be used as the engine for 523 * ctf_fdopen() or ctf_open(), below. 524 */ 525 ctf_file_t * 526 ctf_bufopen(const ctf_sect_t *ctfsect, const ctf_sect_t *symsect, 527 const ctf_sect_t *strsect, int *errp) 528 { 529 const ctf_preamble_t *pp; 530 ctf_header_t hp; 531 ctf_file_t *fp; 532 void *buf, *base; 533 size_t size, hdrsz; 534 int err; 535 536 if (ctfsect == NULL || ((symsect == NULL) != (strsect == NULL))) 537 return (ctf_set_open_errno(errp, EINVAL)); 538 539 if (symsect != NULL && symsect->cts_entsize != sizeof (Elf32_Sym) && 540 symsect->cts_entsize != sizeof (Elf64_Sym)) 541 return (ctf_set_open_errno(errp, ECTF_SYMTAB)); 542 543 if (symsect != NULL && symsect->cts_data == NULL) 544 return (ctf_set_open_errno(errp, ECTF_SYMBAD)); 545 546 if (strsect != NULL && strsect->cts_data == NULL) 547 return (ctf_set_open_errno(errp, ECTF_STRBAD)); 548 549 if (ctfsect->cts_size < sizeof (ctf_preamble_t)) 550 return (ctf_set_open_errno(errp, ECTF_NOCTFBUF)); 551 552 pp = (const ctf_preamble_t *)ctfsect->cts_data; 553 554 ctf_dprintf("ctf_bufopen: magic=0x%x version=%u\n", 555 pp->ctp_magic, pp->ctp_version); 556 557 /* 558 * Validate each part of the CTF header (either V1 or V2). 559 * First, we validate the preamble (common to all versions). At that 560 * point, we know specific header version, and can validate the 561 * version-specific parts including section offsets and alignments. 562 */ 563 if (pp->ctp_magic != CTF_MAGIC) 564 return (ctf_set_open_errno(errp, ECTF_NOCTFBUF)); 565 566 if (pp->ctp_version == CTF_VERSION_2) { 567 if (ctfsect->cts_size < sizeof (ctf_header_t)) 568 return (ctf_set_open_errno(errp, ECTF_NOCTFBUF)); 569 570 bcopy(ctfsect->cts_data, &hp, sizeof (hp)); 571 hdrsz = sizeof (ctf_header_t); 572 573 } else 574 return (ctf_set_open_errno(errp, ECTF_CTFVERS)); 575 576 size = hp.cth_stroff + hp.cth_strlen; 577 578 ctf_dprintf("ctf_bufopen: uncompressed size=%lu\n", (ulong_t)size); 579 580 if (hp.cth_lbloff > size || hp.cth_objtoff > size || 581 hp.cth_funcoff > size || hp.cth_typeoff > size || 582 hp.cth_stroff > size) 583 return (ctf_set_open_errno(errp, ECTF_CORRUPT)); 584 585 if (hp.cth_lbloff > hp.cth_objtoff || 586 hp.cth_objtoff > hp.cth_funcoff || 587 hp.cth_funcoff > hp.cth_typeoff || 588 hp.cth_typeoff > hp.cth_stroff) 589 return (ctf_set_open_errno(errp, ECTF_CORRUPT)); 590 591 if ((hp.cth_lbloff & 3) || (hp.cth_objtoff & 1) || 592 (hp.cth_funcoff & 1) || (hp.cth_typeoff & 3)) 593 return (ctf_set_open_errno(errp, ECTF_CORRUPT)); 594 595 /* 596 * Once everything is determined to be valid, attempt to decompress 597 * the CTF data buffer if it is compressed. Otherwise we just put 598 * the data section's buffer pointer into ctf_buf, below. 599 */ 600 if (hp.cth_flags & CTF_F_COMPRESS) { 601 size_t srclen, dstlen; 602 const void *src; 603 int rc = Z_OK; 604 605 if (ctf_zopen(errp) == NULL) 606 return (NULL); /* errp is set for us */ 607 608 if ((base = ctf_data_alloc(size + hdrsz)) == MAP_FAILED) 609 return (ctf_set_open_errno(errp, ECTF_ZALLOC)); 610 611 bcopy(ctfsect->cts_data, base, hdrsz); 612 ((ctf_preamble_t *)base)->ctp_flags &= ~CTF_F_COMPRESS; 613 buf = (uchar_t *)base + hdrsz; 614 615 src = (uchar_t *)ctfsect->cts_data + hdrsz; 616 srclen = ctfsect->cts_size - hdrsz; 617 dstlen = size; 618 619 if ((rc = z_uncompress(buf, &dstlen, src, srclen)) != Z_OK) { 620 ctf_dprintf("zlib inflate err: %s\n", z_strerror(rc)); 621 ctf_data_free(base, size + hdrsz); 622 return (ctf_set_open_errno(errp, ECTF_DECOMPRESS)); 623 } 624 625 if (dstlen != size) { 626 ctf_dprintf("zlib inflate short -- got %lu of %lu " 627 "bytes\n", (ulong_t)dstlen, (ulong_t)size); 628 ctf_data_free(base, size + hdrsz); 629 return (ctf_set_open_errno(errp, ECTF_CORRUPT)); 630 } 631 632 ctf_data_protect(base, size + hdrsz); 633 634 } else { 635 base = (void *)ctfsect->cts_data; 636 buf = (uchar_t *)base + hdrsz; 637 } 638 639 /* 640 * Once we have uncompressed and validated the CTF data buffer, we can 641 * proceed with allocating a ctf_file_t and initializing it. 642 */ 643 if ((fp = ctf_alloc(sizeof (ctf_file_t))) == NULL) 644 return (ctf_set_open_errno(errp, EAGAIN)); 645 646 bzero(fp, sizeof (ctf_file_t)); 647 fp->ctf_version = hp.cth_version; 648 fp->ctf_fileops = &ctf_fileops[hp.cth_version]; 649 bcopy(ctfsect, &fp->ctf_data, sizeof (ctf_sect_t)); 650 651 if (symsect != NULL) { 652 bcopy(symsect, &fp->ctf_symtab, sizeof (ctf_sect_t)); 653 bcopy(strsect, &fp->ctf_strtab, sizeof (ctf_sect_t)); 654 } 655 656 if (fp->ctf_data.cts_name != NULL) 657 fp->ctf_data.cts_name = ctf_strdup(fp->ctf_data.cts_name); 658 if (fp->ctf_symtab.cts_name != NULL) 659 fp->ctf_symtab.cts_name = ctf_strdup(fp->ctf_symtab.cts_name); 660 if (fp->ctf_strtab.cts_name != NULL) 661 fp->ctf_strtab.cts_name = ctf_strdup(fp->ctf_strtab.cts_name); 662 663 if (fp->ctf_data.cts_name == NULL) 664 fp->ctf_data.cts_name = _CTF_NULLSTR; 665 if (fp->ctf_symtab.cts_name == NULL) 666 fp->ctf_symtab.cts_name = _CTF_NULLSTR; 667 if (fp->ctf_strtab.cts_name == NULL) 668 fp->ctf_strtab.cts_name = _CTF_NULLSTR; 669 670 fp->ctf_str[CTF_STRTAB_0].cts_strs = (const char *)buf + hp.cth_stroff; 671 fp->ctf_str[CTF_STRTAB_0].cts_len = hp.cth_strlen; 672 673 if (strsect != NULL) { 674 fp->ctf_str[CTF_STRTAB_1].cts_strs = strsect->cts_data; 675 fp->ctf_str[CTF_STRTAB_1].cts_len = strsect->cts_size; 676 } 677 678 fp->ctf_base = base; 679 fp->ctf_buf = buf; 680 fp->ctf_size = size + hdrsz; 681 682 /* 683 * If we have a parent container name and label, store the relocated 684 * string pointers in the CTF container for easy access later. 685 */ 686 if (hp.cth_parlabel != 0) 687 fp->ctf_parlabel = ctf_strptr(fp, hp.cth_parlabel); 688 if (hp.cth_parname != 0) 689 fp->ctf_parname = ctf_strptr(fp, hp.cth_parname); 690 691 ctf_dprintf("ctf_bufopen: parent name %s (label %s)\n", 692 fp->ctf_parname ? fp->ctf_parname : "<NULL>", 693 fp->ctf_parlabel ? fp->ctf_parlabel : "<NULL>"); 694 695 /* 696 * If we have a symbol table section, allocate and initialize 697 * the symtab translation table, pointed to by ctf_sxlate. 698 */ 699 if (symsect != NULL) { 700 fp->ctf_nsyms = symsect->cts_size / symsect->cts_entsize; 701 fp->ctf_sxlate = ctf_alloc(fp->ctf_nsyms * sizeof (uint_t)); 702 703 if (fp->ctf_sxlate == NULL) { 704 (void) ctf_set_open_errno(errp, EAGAIN); 705 goto bad; 706 } 707 708 if ((err = init_symtab(fp, &hp, symsect, strsect)) != 0) { 709 (void) ctf_set_open_errno(errp, err); 710 goto bad; 711 } 712 } 713 714 if ((err = init_types(fp, &hp)) != 0) { 715 (void) ctf_set_open_errno(errp, err); 716 goto bad; 717 } 718 719 /* 720 * Initialize the ctf_lookup_by_name top-level dictionary. We keep an 721 * array of type name prefixes and the corresponding ctf_hash to use. 722 * NOTE: This code must be kept in sync with the code in ctf_update(). 723 */ 724 fp->ctf_lookups[0].ctl_prefix = "struct"; 725 fp->ctf_lookups[0].ctl_len = strlen(fp->ctf_lookups[0].ctl_prefix); 726 fp->ctf_lookups[0].ctl_hash = &fp->ctf_structs; 727 fp->ctf_lookups[1].ctl_prefix = "union"; 728 fp->ctf_lookups[1].ctl_len = strlen(fp->ctf_lookups[1].ctl_prefix); 729 fp->ctf_lookups[1].ctl_hash = &fp->ctf_unions; 730 fp->ctf_lookups[2].ctl_prefix = "enum"; 731 fp->ctf_lookups[2].ctl_len = strlen(fp->ctf_lookups[2].ctl_prefix); 732 fp->ctf_lookups[2].ctl_hash = &fp->ctf_enums; 733 fp->ctf_lookups[3].ctl_prefix = _CTF_NULLSTR; 734 fp->ctf_lookups[3].ctl_len = strlen(fp->ctf_lookups[3].ctl_prefix); 735 fp->ctf_lookups[3].ctl_hash = &fp->ctf_names; 736 fp->ctf_lookups[4].ctl_prefix = NULL; 737 fp->ctf_lookups[4].ctl_len = 0; 738 fp->ctf_lookups[4].ctl_hash = NULL; 739 740 if (symsect != NULL) { 741 if (symsect->cts_entsize == sizeof (Elf64_Sym)) 742 (void) ctf_setmodel(fp, CTF_MODEL_LP64); 743 else 744 (void) ctf_setmodel(fp, CTF_MODEL_ILP32); 745 } else 746 (void) ctf_setmodel(fp, CTF_MODEL_NATIVE); 747 748 fp->ctf_refcnt = 1; 749 return (fp); 750 751 bad: 752 ctf_close(fp); 753 return (NULL); 754 } 755 756 /* 757 * Dupliate a ctf_file_t and its underlying section information into a new 758 * container. This works by copying the three ctf_sect_t's of the original 759 * container if they exist and passing those into ctf_bufopen. To copy those, we 760 * mmap anonymous memory with ctf_data_alloc and bcopy the data across. It's not 761 * the cheapest thing, but it's what we've got. 762 */ 763 ctf_file_t * 764 ctf_dup(ctf_file_t *ofp) 765 { 766 ctf_file_t *fp; 767 ctf_sect_t ctfsect, symsect, strsect; 768 ctf_sect_t *ctp, *symp, *strp; 769 void *cbuf, *symbuf, *strbuf; 770 int err; 771 772 cbuf = symbuf = strbuf = NULL; 773 /* 774 * The ctfsect isn't allowed to not exist, but the symbol and string 775 * section might not. We only need to copy the data of the section, not 776 * the name, as ctf_bufopen will take care of that. 777 */ 778 bcopy(&ofp->ctf_data, &ctfsect, sizeof (ctf_sect_t)); 779 cbuf = ctf_data_alloc(ctfsect.cts_size); 780 if (cbuf == NULL) { 781 (void) ctf_set_errno(ofp, ECTF_MMAP); 782 return (NULL); 783 } 784 785 bcopy(ctfsect.cts_data, cbuf, ctfsect.cts_size); 786 ctf_data_protect(cbuf, ctfsect.cts_size); 787 ctfsect.cts_data = cbuf; 788 ctfsect.cts_offset = 0; 789 ctp = &ctfsect; 790 791 if (ofp->ctf_symtab.cts_data != NULL) { 792 bcopy(&ofp->ctf_symtab, &symsect, sizeof (ctf_sect_t)); 793 symbuf = ctf_data_alloc(symsect.cts_size); 794 if (symbuf == NULL) { 795 (void) ctf_set_errno(ofp, ECTF_MMAP); 796 goto err; 797 } 798 bcopy(symsect.cts_data, symbuf, symsect.cts_size); 799 ctf_data_protect(symbuf, symsect.cts_size); 800 symsect.cts_data = symbuf; 801 symsect.cts_offset = 0; 802 symp = &symsect; 803 } else { 804 symp = NULL; 805 } 806 807 if (ofp->ctf_strtab.cts_data != NULL) { 808 bcopy(&ofp->ctf_strtab, &strsect, sizeof (ctf_sect_t)); 809 strbuf = ctf_data_alloc(strsect.cts_size); 810 if (strbuf == NULL) { 811 (void) ctf_set_errno(ofp, ECTF_MMAP); 812 goto err; 813 } 814 bcopy(strsect.cts_data, strbuf, strsect.cts_size); 815 ctf_data_protect(strbuf, strsect.cts_size); 816 strsect.cts_data = strbuf; 817 strsect.cts_offset = 0; 818 strp = &strsect; 819 } else { 820 strp = NULL; 821 } 822 823 fp = ctf_bufopen(ctp, symp, strp, &err); 824 if (fp == NULL) { 825 (void) ctf_set_errno(ofp, err); 826 goto err; 827 } 828 829 fp->ctf_flags |= LCTF_MMAP; 830 831 return (fp); 832 833 err: 834 ctf_data_free(cbuf, ctfsect.cts_size); 835 if (symbuf != NULL) 836 ctf_data_free(symbuf, symsect.cts_size); 837 if (strbuf != NULL) 838 ctf_data_free(strbuf, strsect.cts_size); 839 return (NULL); 840 } 841 842 /* 843 * Close the specified CTF container and free associated data structures. Note 844 * that ctf_close() is a reference counted operation: if the specified file is 845 * the parent of other active containers, its reference count will be greater 846 * than one and it will be freed later when no active children exist. 847 */ 848 void 849 ctf_close(ctf_file_t *fp) 850 { 851 ctf_dtdef_t *dtd, *ntd; 852 853 if (fp == NULL) 854 return; /* allow ctf_close(NULL) to simplify caller code */ 855 856 ctf_dprintf("ctf_close(%p) refcnt=%u\n", (void *)fp, fp->ctf_refcnt); 857 858 if (fp->ctf_refcnt > 1) { 859 fp->ctf_refcnt--; 860 return; 861 } 862 863 if (fp->ctf_parent != NULL) 864 ctf_close(fp->ctf_parent); 865 866 /* 867 * Note, to work properly with reference counting on the dynamic 868 * section, we must delete the list in reverse. 869 */ 870 for (dtd = ctf_list_prev(&fp->ctf_dtdefs); dtd != NULL; dtd = ntd) { 871 ntd = ctf_list_prev(dtd); 872 ctf_dtd_delete(fp, dtd); 873 } 874 875 ctf_free(fp->ctf_dthash, fp->ctf_dthashlen * sizeof (ctf_dtdef_t *)); 876 877 if (fp->ctf_flags & LCTF_MMAP) { 878 if (fp->ctf_data.cts_data != NULL) 879 ctf_sect_munmap(&fp->ctf_data); 880 if (fp->ctf_symtab.cts_data != NULL) 881 ctf_sect_munmap(&fp->ctf_symtab); 882 if (fp->ctf_strtab.cts_data != NULL) 883 ctf_sect_munmap(&fp->ctf_strtab); 884 } 885 886 if (fp->ctf_data.cts_name != _CTF_NULLSTR && 887 fp->ctf_data.cts_name != NULL) { 888 ctf_free((char *)fp->ctf_data.cts_name, 889 strlen(fp->ctf_data.cts_name) + 1); 890 } 891 892 if (fp->ctf_symtab.cts_name != _CTF_NULLSTR && 893 fp->ctf_symtab.cts_name != NULL) { 894 ctf_free((char *)fp->ctf_symtab.cts_name, 895 strlen(fp->ctf_symtab.cts_name) + 1); 896 } 897 898 if (fp->ctf_strtab.cts_name != _CTF_NULLSTR && 899 fp->ctf_strtab.cts_name != NULL) { 900 ctf_free((char *)fp->ctf_strtab.cts_name, 901 strlen(fp->ctf_strtab.cts_name) + 1); 902 } 903 904 if (fp->ctf_base != fp->ctf_data.cts_data && fp->ctf_base != NULL) 905 ctf_data_free((void *)fp->ctf_base, fp->ctf_size); 906 907 if (fp->ctf_sxlate != NULL) 908 ctf_free(fp->ctf_sxlate, sizeof (uint_t) * fp->ctf_nsyms); 909 910 if (fp->ctf_txlate != NULL) { 911 ctf_free(fp->ctf_txlate, 912 sizeof (uint_t) * (fp->ctf_typemax + 1)); 913 } 914 915 if (fp->ctf_ptrtab != NULL) { 916 ctf_free(fp->ctf_ptrtab, 917 sizeof (ushort_t) * (fp->ctf_typemax + 1)); 918 } 919 920 ctf_hash_destroy(&fp->ctf_structs); 921 ctf_hash_destroy(&fp->ctf_unions); 922 ctf_hash_destroy(&fp->ctf_enums); 923 ctf_hash_destroy(&fp->ctf_names); 924 925 ctf_free(fp, sizeof (ctf_file_t)); 926 } 927 928 /* 929 * Return the CTF handle for the parent CTF container, if one exists. 930 * Otherwise return NULL to indicate this container has no imported parent. 931 */ 932 ctf_file_t * 933 ctf_parent_file(ctf_file_t *fp) 934 { 935 return (fp->ctf_parent); 936 } 937 938 /* 939 * Return the name of the parent CTF container, if one exists. Otherwise 940 * return NULL to indicate this container is a root container. 941 */ 942 const char * 943 ctf_parent_name(ctf_file_t *fp) 944 { 945 return (fp->ctf_parname); 946 } 947 948 /* 949 * Import the types from the specified parent container by storing a pointer 950 * to it in ctf_parent and incrementing its reference count. Only one parent 951 * is allowed: if a parent already exists, it is replaced by the new parent. 952 */ 953 int 954 ctf_import(ctf_file_t *fp, ctf_file_t *pfp) 955 { 956 if (fp == NULL || fp == pfp || (pfp != NULL && pfp->ctf_refcnt == 0)) 957 return (ctf_set_errno(fp, EINVAL)); 958 959 if (pfp != NULL && pfp->ctf_dmodel != fp->ctf_dmodel) 960 return (ctf_set_errno(fp, ECTF_DMODEL)); 961 962 if (fp->ctf_parent != NULL) 963 ctf_close(fp->ctf_parent); 964 965 if (pfp != NULL) { 966 fp->ctf_flags |= LCTF_CHILD; 967 pfp->ctf_refcnt++; 968 } 969 970 fp->ctf_parent = pfp; 971 return (0); 972 } 973 974 /* 975 * Set the data model constant for the CTF container. 976 */ 977 int 978 ctf_setmodel(ctf_file_t *fp, int model) 979 { 980 const ctf_dmodel_t *dp; 981 982 for (dp = _libctf_models; dp->ctd_name != NULL; dp++) { 983 if (dp->ctd_code == model) { 984 fp->ctf_dmodel = dp; 985 return (0); 986 } 987 } 988 989 return (ctf_set_errno(fp, EINVAL)); 990 } 991 992 /* 993 * Return the data model constant for the CTF container. 994 */ 995 int 996 ctf_getmodel(ctf_file_t *fp) 997 { 998 return (fp->ctf_dmodel->ctd_code); 999 } 1000 1001 void 1002 ctf_setspecific(ctf_file_t *fp, void *data) 1003 { 1004 fp->ctf_specific = data; 1005 } 1006 1007 void * 1008 ctf_getspecific(ctf_file_t *fp) 1009 { 1010 return (fp->ctf_specific); 1011 } 1012