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