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 2007 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26#pragma ident "@(#)dwarf.c 1.9 07/01/10 SMI" 27 28/* 29 * DWARF to tdata conversion 30 * 31 * For the most part, conversion is straightforward, proceeding in two passes. 32 * On the first pass, we iterate through every die, creating new type nodes as 33 * necessary. Referenced tdesc_t's are created in an uninitialized state, thus 34 * allowing type reference pointers to be filled in. If the tdesc_t 35 * corresponding to a given die can be completely filled out (sizes and offsets 36 * calculated, and so forth) without using any referenced types, the tdesc_t is 37 * marked as resolved. Consider an array type. If the type corresponding to 38 * the array contents has not yet been processed, we will create a blank tdesc 39 * for the contents type (only the type ID will be filled in, relying upon the 40 * later portion of the first pass to encounter and complete the referenced 41 * type). We will then attempt to determine the size of the array. If the 42 * array has a byte size attribute, we will have completely characterized the 43 * array type, and will be able to mark it as resolved. The lack of a byte 44 * size attribute, on the other hand, will prevent us from fully resolving the 45 * type, as the size will only be calculable with reference to the contents 46 * type, which has not, as yet, been encountered. The array type will thus be 47 * left without the resolved flag, and the first pass will continue. 48 * 49 * When we begin the second pass, we will have created tdesc_t nodes for every 50 * type in the section. We will traverse the tree, from the iidescs down, 51 * processing each unresolved node. As the referenced nodes will have been 52 * populated, the array type used in our example above will be able to use the 53 * size of the referenced types (if available) to determine its own type. The 54 * traversal will be repeated until all types have been resolved or we have 55 * failed to make progress. When all tdescs have been resolved, the conversion 56 * is complete. 57 * 58 * There are, as always, a few special cases that are handled during the first 59 * and second passes: 60 * 61 * 1. Empty enums - GCC will occasionally emit an enum without any members. 62 * Later on in the file, it will emit the same enum type, though this time 63 * with the full complement of members. All references to the memberless 64 * enum need to be redirected to the full definition. During the first 65 * pass, each enum is entered in dm_enumhash, along with a pointer to its 66 * corresponding tdesc_t. If, during the second pass, we encounter a 67 * memberless enum, we use the hash to locate the full definition. All 68 * tdescs referencing the empty enum are then redirected. 69 * 70 * 2. Forward declarations - If the compiler sees a forward declaration for 71 * a structure, followed by the definition of that structure, it will emit 72 * DWARF data for both the forward declaration and the definition. We need 73 * to resolve the forward declarations when possible, by redirecting 74 * forward-referencing tdescs to the actual struct/union definitions. This 75 * redirection is done completely within the first pass. We begin by 76 * recording all forward declarations in dw_fwdhash. When we define a 77 * structure, we check to see if there have been any corresponding forward 78 * declarations. If so, we redirect the tdescs which referenced the forward 79 * declarations to the structure or union definition. 80 * 81 * XXX see if a post traverser will allow the elimination of repeated pass 2 82 * traversals. 83 */ 84 85#include <assert.h> 86#include <stdio.h> 87#include <stdlib.h> 88#include <strings.h> 89#include <errno.h> 90#include <libelf.h> 91#include <libdwarf.h> 92#include <libgen.h> 93#include <dwarf.h> 94 95#include "ctf_headers.h" 96#include "ctftools.h" 97#include "memory.h" 98#include "list.h" 99#include "traverse.h" 100 101#if defined(__APPLE__) 102/* Sun extensions not present in Darwin's dwarf.h */ 103#define DW_ATE_SUN_interval_float 0x91 104#define DW_ATE_SUN_imaginary_float 0x92 /* Obsolete: See DW_ATE_imaginary_float */ 105#endif /* __APPLE__ */ 106 107/* The version of DWARF which we support. */ 108#define DWARF_VERSION 2 109#define DWARF_VERSION3 3 110#define DWARF_VERSION4 4 111 112/* 113 * We need to define a couple of our own intrinsics, to smooth out some of the 114 * differences between the GCC and DevPro DWARF emitters. See the referenced 115 * routines and the special cases in the file comment for more details. 116 * 117 * Type IDs are 32 bits wide. We're going to use the top of that field to 118 * indicate types that we've created ourselves. 119 */ 120#define TID_FILEMAX 0x3fffffff /* highest tid from file */ 121#define TID_VOID 0x40000001 /* see die_void() */ 122#define TID_LONG 0x40000002 /* see die_array() */ 123 124#define TID_MFGTID_BASE 0x40000003 /* first mfg'd tid */ 125 126/* 127 * To reduce the staggering amount of error-handling code that would otherwise 128 * be required, the attribute-retrieval routines handle most of their own 129 * errors. If the following flag is supplied as the value of the `req' 130 * argument, they will also handle the absence of a requested attribute by 131 * terminating the program. 132 */ 133#define DW_ATTR_REQ 1 134 135#define TDESC_HASH_BUCKETS 511 136 137typedef struct dwarf { 138 Dwarf_Debug dw_dw; /* for libdwarf */ 139 Dwarf_Error dw_err; /* for libdwarf */ 140 Dwarf_Unsigned dw_maxoff; /* highest legal offset in this cu */ 141 tdata_t *dw_td; /* root of the tdesc/iidesc tree */ 142 hash_t *dw_tidhash; /* hash of tdescs by t_id */ 143 hash_t *dw_fwdhash; /* hash of fwd decls by name */ 144 hash_t *dw_enumhash; /* hash of memberless enums by name */ 145 tdesc_t *dw_void; /* manufactured void type */ 146 tdesc_t *dw_long; /* manufactured long type for arrays */ 147 size_t dw_ptrsz; /* size of a pointer in this file */ 148 tid_t dw_mfgtid_last; /* last mfg'd type ID used */ 149 uint_t dw_nunres; /* count of unresolved types */ 150 char *dw_cuname; /* name of compilation unit */ 151} dwarf_t; 152 153static void die_create_one(dwarf_t *, Dwarf_Die); 154static void die_create(dwarf_t *, Dwarf_Die); 155 156static tid_t 157mfgtid_next(dwarf_t *dw) 158{ 159 return (++dw->dw_mfgtid_last); 160} 161 162static void 163tdesc_add(dwarf_t *dw, tdesc_t *tdp) 164{ 165 hash_add(dw->dw_tidhash, tdp); 166} 167 168static tdesc_t * 169tdesc_lookup(dwarf_t *dw, int tid) 170{ 171 tdesc_t tmpl, *tdp; 172 173 tmpl.t_id = tid; 174 175 if (hash_find(dw->dw_tidhash, &tmpl, (void **)&tdp)) 176 return (tdp); 177 else 178 return (NULL); 179} 180 181/* 182 * Resolve a tdesc down to a node which should have a size. Returns the size, 183 * zero if the size hasn't yet been determined. 184 */ 185static size_t 186tdesc_size(tdesc_t *tdp) 187{ 188 for (;;) { 189 switch (tdp->t_type) { 190 case INTRINSIC: 191 case POINTER: 192 case ARRAY: 193 case FUNCTION: 194 case STRUCT: 195 case UNION: 196 case ENUM: 197 return (tdp->t_size); 198 199 case FORWARD: 200 return (0); 201 202 case TYPEDEF: 203 case VOLATILE: 204 case CONST: 205 case RESTRICT: 206 tdp = tdp->t_tdesc; 207 continue; 208 209 case 0: /* not yet defined */ 210 return (0); 211 212 default: 213 terminate("tdp %u: tdesc_size on unknown type %d\n", 214 tdp->t_id, tdp->t_type); 215 } 216 } 217} 218 219static size_t 220tdesc_bitsize(tdesc_t *tdp) 221{ 222 for (;;) { 223 switch (tdp->t_type) { 224 case INTRINSIC: 225 return (tdp->t_intr->intr_nbits); 226 227 case ARRAY: 228 case FUNCTION: 229 case STRUCT: 230 case UNION: 231 case ENUM: 232 case POINTER: 233 return (tdp->t_size * NBBY); 234 235 case FORWARD: 236 return (0); 237 238 case TYPEDEF: 239 case VOLATILE: 240 case RESTRICT: 241 case CONST: 242 tdp = tdp->t_tdesc; 243 continue; 244 245 case 0: /* not yet defined */ 246 return (0); 247 248 default: 249 terminate("tdp %u: tdesc_bitsize on unknown type %d\n", 250 tdp->t_id, tdp->t_type); 251 } 252 } 253} 254 255static tdesc_t * 256tdesc_basetype(tdesc_t *tdp) 257{ 258 for (;;) { 259 switch (tdp->t_type) { 260 case TYPEDEF: 261 case VOLATILE: 262 case RESTRICT: 263 case CONST: 264 tdp = tdp->t_tdesc; 265 break; 266 case 0: /* not yet defined */ 267 return (NULL); 268 default: 269 return (tdp); 270 } 271 } 272} 273 274static Dwarf_Off 275die_off(dwarf_t *dw, Dwarf_Die die) 276{ 277 Dwarf_Off off; 278 279 if (dwarf_dieoffset(die, &off, &dw->dw_err) == DW_DLV_OK) 280 return (off); 281 282 terminate("failed to get offset for die: %s\n", 283 dwarf_errmsg(dw->dw_err)); 284 /*NOTREACHED*/ 285 return (0); 286} 287 288static Dwarf_Die 289die_sibling(dwarf_t *dw, Dwarf_Die die) 290{ 291 Dwarf_Die sib; 292 int rc; 293 294 if ((rc = dwarf_siblingof(dw->dw_dw, die, &sib, &dw->dw_err)) == 295 DW_DLV_OK) 296 return (sib); 297 else if (rc == DW_DLV_NO_ENTRY) 298 return (NULL); 299 300 terminate("die %llu: failed to find type sibling: %s\n", 301 die_off(dw, die), dwarf_errmsg(dw->dw_err)); 302 /*NOTREACHED*/ 303 return (NULL); 304} 305 306static Dwarf_Die 307die_child(dwarf_t *dw, Dwarf_Die die) 308{ 309 Dwarf_Die child; 310 int rc; 311 312 if ((rc = dwarf_child(die, &child, &dw->dw_err)) == DW_DLV_OK) 313 return (child); 314 else if (rc == DW_DLV_NO_ENTRY) 315 return (NULL); 316 317 terminate("die %llu: failed to find type child: %s\n", 318 die_off(dw, die), dwarf_errmsg(dw->dw_err)); 319 /*NOTREACHED*/ 320 return (NULL); 321} 322 323static Dwarf_Half 324die_tag(dwarf_t *dw, Dwarf_Die die) 325{ 326 Dwarf_Half tag; 327 328 if (dwarf_tag(die, &tag, &dw->dw_err) == DW_DLV_OK) 329 return (tag); 330 331 terminate("die %llu: failed to get tag for type: %s\n", 332 die_off(dw, die), dwarf_errmsg(dw->dw_err)); 333 /*NOTREACHED*/ 334 return (0); 335} 336 337static Dwarf_Attribute 338die_attr(dwarf_t *dw, Dwarf_Die die, Dwarf_Half name, int req) 339{ 340 Dwarf_Attribute attr; 341 int rc; 342 343 if ((rc = dwarf_attr(die, name, &attr, &dw->dw_err)) == DW_DLV_OK) { 344 return (attr); 345 } else if (rc == DW_DLV_NO_ENTRY) { 346 if (req) { 347 terminate("die %llu: no attr 0x%x\n", die_off(dw, die), 348 name); 349 } else { 350 return (NULL); 351 } 352 } 353 354 terminate("die %llu: failed to get attribute for type: %s\n", 355 die_off(dw, die), dwarf_errmsg(dw->dw_err)); 356 /*NOTREACHED*/ 357 return (NULL); 358} 359 360static Dwarf_Half 361die_attr_form(dwarf_t *dw, Dwarf_Attribute attr) 362{ 363 Dwarf_Half form; 364 365 if (dwarf_whatform(attr, &form, &dw->dw_err) == DW_DLV_OK) 366 return (form); 367 368 terminate("failed to get attribute form for type: %s\n", 369 dwarf_errmsg(dw->dw_err)); 370 /*NOTREACHED*/ 371 return (0); 372} 373 374static int 375die_signed(dwarf_t *dw, Dwarf_Die die, Dwarf_Half name, Dwarf_Signed *valp, 376 int req) 377{ 378 Dwarf_Attribute attr; 379 Dwarf_Signed val; 380 381 if ((attr = die_attr(dw, die, name, req)) == NULL) 382 return (0); /* die_attr will terminate for us if necessary */ 383 384 if (dwarf_formsdata(attr, &val, &dw->dw_err) != DW_DLV_OK) { 385 terminate("die %llu: failed to get signed (form 0x%x)\n", 386 die_off(dw, die), die_attr_form(dw, attr)); 387 } 388 389 dwarf_dealloc(dw->dw_dw, attr, DW_DLA_ATTR); 390 391 *valp = val; 392 return (1); 393} 394 395static int 396die_unsigned(dwarf_t *dw, Dwarf_Die die, Dwarf_Half name, Dwarf_Unsigned *valp, 397 int req) 398{ 399 Dwarf_Attribute attr; 400 Dwarf_Unsigned val; 401 402 if ((attr = die_attr(dw, die, name, req)) == NULL) 403 return (0); /* die_attr will terminate for us if necessary */ 404 405 if (dwarf_formudata(attr, &val, &dw->dw_err) != DW_DLV_OK) { 406 terminate("die %llu: failed to get unsigned (form 0x%x)\n", 407 die_off(dw, die), die_attr_form(dw, attr)); 408 } 409 410 dwarf_dealloc(dw->dw_dw, attr, DW_DLA_ATTR); 411 412 *valp = val; 413 return (1); 414} 415 416static int 417die_bool(dwarf_t *dw, Dwarf_Die die, Dwarf_Half name, Dwarf_Bool *valp, int req) 418{ 419 Dwarf_Attribute attr; 420 Dwarf_Bool val; 421 422 if ((attr = die_attr(dw, die, name, req)) == NULL) 423 return (0); /* die_attr will terminate for us if necessary */ 424 425 if (dwarf_formflag(attr, &val, &dw->dw_err) != DW_DLV_OK) { 426 terminate("die %llu: failed to get bool (form 0x%x)\n", 427 die_off(dw, die), die_attr_form(dw, attr)); 428 } 429 430 dwarf_dealloc(dw->dw_dw, attr, DW_DLA_ATTR); 431 432 *valp = val; 433 return (1); 434} 435 436static int 437die_string(dwarf_t *dw, Dwarf_Die die, Dwarf_Half name, char **strp, int req) 438{ 439 Dwarf_Attribute attr; 440 char *str; 441 442 if ((attr = die_attr(dw, die, name, req)) == NULL) 443 return (0); /* die_attr will terminate for us if necessary */ 444 445 if (dwarf_formstring(attr, &str, &dw->dw_err) != DW_DLV_OK) { 446 terminate("die %llu: failed to get string (form 0x%x)\n", 447 die_off(dw, die), die_attr_form(dw, attr)); 448 } 449 450 *strp = xstrdup(str); 451 dwarf_dealloc(dw->dw_dw, str, DW_DLA_STRING); 452 453 return (1); 454} 455 456static Dwarf_Off 457die_attr_ref(dwarf_t *dw, Dwarf_Die die, Dwarf_Half name) 458{ 459 Dwarf_Attribute attr; 460 Dwarf_Off off; 461 462 attr = die_attr(dw, die, name, DW_ATTR_REQ); 463 464 if (dwarf_formref(attr, &off, &dw->dw_err) != DW_DLV_OK) { 465 terminate("die %llu: failed to get ref (form 0x%x)\n", 466 die_off(dw, die), die_attr_form(dw, attr)); 467 } 468 469 dwarf_dealloc(dw->dw_dw, attr, DW_DLA_ATTR); 470 471 return (off); 472} 473 474static char * 475die_name(dwarf_t *dw, Dwarf_Die die) 476{ 477 char *str = NULL; 478 479 (void) die_string(dw, die, DW_AT_name, &str, 0); 480 481 return (str); 482} 483 484static int 485die_isdecl(dwarf_t *dw, Dwarf_Die die) 486{ 487 Dwarf_Bool val; 488 489 return (die_bool(dw, die, DW_AT_declaration, &val, 0) && val); 490} 491 492#if defined(__APPLE__) 493static char * 494die_linkage_name(dwarf_t *dw, Dwarf_Die die) 495{ 496 char *str = NULL; 497 498 (void) die_string(dw, die, DW_AT_MIPS_linkage_name, &str, 0); 499 500 return (str); 501} 502 503static Dwarf_Die 504die_specification_die(dwarf_t *dw, Dwarf_Die die) 505{ 506 Dwarf_Attribute attr; 507 Dwarf_Off ref; 508 Dwarf_Die new_die; 509 int rc; 510 511 if ((attr = die_attr(dw, die, DW_AT_specification, 0)) == NULL) 512 return (NULL); 513 514 ref = die_attr_ref(dw, die, DW_AT_specification); 515 516 if ((rc = dwarf_offdie(dw->dw_dw, ref, &new_die, &dw->dw_err)) != DW_DLV_OK) { 517 terminate("die %llu: failed to get DW_AT_specificaiton ref\n", die); 518 } 519 520 return (new_die); 521} 522 523static int 524die_isvirtual(dwarf_t *dw, Dwarf_Die die) 525{ 526 Dwarf_Signed val; 527 528 return (die_signed(dw, die, DW_AT_virtuality, &val, 0) && val); 529} 530#endif /* __APPLE__ */ 531 532static int 533die_isglobal(dwarf_t *dw, Dwarf_Die die) 534{ 535 Dwarf_Signed vis; 536 Dwarf_Bool ext; 537 538 /* 539 * Some compilers (gcc) use DW_AT_external to indicate function 540 * visibility. Others (Sun) use DW_AT_visibility. 541 */ 542 if (die_signed(dw, die, DW_AT_visibility, &vis, 0)) 543 return (vis == DW_VIS_exported); 544 else 545 return (die_bool(dw, die, DW_AT_external, &ext, 0) && ext); 546} 547 548static tdesc_t * 549die_add(dwarf_t *dw, Dwarf_Off off) 550{ 551 tdesc_t *tdp = xcalloc(sizeof (tdesc_t)); 552 553 tdp->t_id = off; 554 555 tdesc_add(dw, tdp); 556 557 return (tdp); 558} 559 560static tdesc_t * 561die_lookup_pass1(dwarf_t *dw, Dwarf_Die die, Dwarf_Half name) 562{ 563 Dwarf_Off ref = die_attr_ref(dw, die, name); 564 tdesc_t *tdp; 565 566 if ((tdp = tdesc_lookup(dw, ref)) != NULL) 567 return (tdp); 568 569 return (die_add(dw, ref)); 570} 571 572static int 573die_mem_offset(dwarf_t *dw, Dwarf_Die die, Dwarf_Half name, 574 Dwarf_Unsigned *valp, int req) 575{ 576 Dwarf_Attribute attr; 577 Dwarf_Locdesc *loc; 578 Dwarf_Signed locnum; 579 580 if ((attr = die_attr(dw, die, name, req)) == NULL) 581 return (0); /* die_attr will terminate for us if necessary */ 582 583 if (dwarf_loclist(attr, &loc, &locnum, &dw->dw_err) != DW_DLV_OK) { 584 terminate("die %llu: failed to get mem offset location list\n", 585 die_off(dw, die)); 586 } 587 588 dwarf_dealloc(dw->dw_dw, attr, DW_DLA_ATTR); 589 590 if (locnum != 1 || loc->ld_s->lr_atom != DW_OP_plus_uconst) { 591 terminate("die %llu: cannot parse member offset\n", 592 die_off(dw, die)); 593 } 594 595 *valp = loc->ld_s->lr_number; 596 597 dwarf_dealloc(dw->dw_dw, loc->ld_s, DW_DLA_LOC_BLOCK); 598 dwarf_dealloc(dw->dw_dw, loc, DW_DLA_LOCDESC); 599 600 return (1); 601} 602 603static tdesc_t * 604tdesc_intr_common(dwarf_t *dw, int tid, const char *name, size_t sz) 605{ 606 tdesc_t *tdp; 607 intr_t *intr; 608 609 intr = xcalloc(sizeof (intr_t)); 610 intr->intr_type = INTR_INT; 611 intr->intr_signed = 1; 612 intr->intr_nbits = sz * NBBY; 613 614 tdp = xcalloc(sizeof (tdesc_t)); 615 tdp->t_name = xstrdup(name); 616 tdp->t_size = sz; 617 tdp->t_id = tid; 618 tdp->t_type = INTRINSIC; 619 tdp->t_intr = intr; 620 tdp->t_flags = TDESC_F_RESOLVED; 621 622 tdesc_add(dw, tdp); 623 624 return (tdp); 625} 626 627/* 628 * Manufacture a void type. Used for gcc-emitted stabs, where the lack of a 629 * type reference implies a reference to a void type. A void *, for example 630 * will be represented by a pointer die without a DW_AT_type. CTF requires 631 * that pointer nodes point to something, so we'll create a void for use as 632 * the target. Note that the DWARF data may already create a void type. Ours 633 * would then be a duplicate, but it'll be removed in the self-uniquification 634 * merge performed at the completion of DWARF->tdesc conversion. 635 */ 636static tdesc_t * 637tdesc_intr_void(dwarf_t *dw) 638{ 639 if (dw->dw_void == NULL) 640 dw->dw_void = tdesc_intr_common(dw, TID_VOID, "void", 0); 641 642 return (dw->dw_void); 643} 644 645static tdesc_t * 646tdesc_intr_long(dwarf_t *dw) 647{ 648 if (dw->dw_long == NULL) { 649 dw->dw_long = tdesc_intr_common(dw, TID_LONG, "long", 650 dw->dw_ptrsz); 651 } 652 653 return (dw->dw_long); 654} 655 656/* 657 * Used for creating bitfield types. We create a copy of an existing intrinsic, 658 * adjusting the size of the copy to match what the caller requested. The 659 * caller can then use the copy as the type for a bitfield structure member. 660 */ 661static tdesc_t * 662tdesc_intr_clone(dwarf_t *dw, tdesc_t *old, size_t bitsz) 663{ 664 tdesc_t *new = xcalloc(sizeof (tdesc_t)); 665 666 if (!(old->t_flags & TDESC_F_RESOLVED)) { 667 terminate("tdp %u: attempt to make a bit field from an " 668 "unresolved type\n", old->t_id); 669 } 670 671 new->t_name = xstrdup(old->t_name); 672 new->t_size = old->t_size; 673 new->t_id = mfgtid_next(dw); 674 new->t_type = INTRINSIC; 675 new->t_flags = TDESC_F_RESOLVED; 676 677 new->t_intr = xcalloc(sizeof (intr_t)); 678 bcopy(old->t_intr, new->t_intr, sizeof (intr_t)); 679 new->t_intr->intr_nbits = bitsz; 680 681 tdesc_add(dw, new); 682 683 return (new); 684} 685 686static void 687tdesc_array_create(dwarf_t *dw, Dwarf_Die dim, tdesc_t *arrtdp, 688 tdesc_t *dimtdp) 689{ 690 Dwarf_Unsigned uval; 691 Dwarf_Signed sval; 692 tdesc_t *ctdp; 693 Dwarf_Die dim2; 694 ardef_t *ar; 695 696 if ((dim2 = die_sibling(dw, dim)) == NULL) { 697 ctdp = arrtdp; 698 } else if (die_tag(dw, dim2) == DW_TAG_subrange_type) { 699 ctdp = xcalloc(sizeof (tdesc_t)); 700 ctdp->t_id = mfgtid_next(dw); 701 debug(3, "die %llu: creating new type %u for sub-dimension\n", 702 die_off(dw, dim2), ctdp->t_id); 703 tdesc_array_create(dw, dim2, arrtdp, ctdp); 704 } else { 705 terminate("die %llu: unexpected non-subrange node in array\n", 706 die_off(dw, dim2)); 707 } 708 709 dimtdp->t_type = ARRAY; 710 dimtdp->t_ardef = ar = xcalloc(sizeof (ardef_t)); 711 712 /* 713 * Array bounds can be signed or unsigned, but there are several kinds 714 * of signless forms (data1, data2, etc) that take their sign from the 715 * routine that is trying to interpret them. That is, data1 can be 716 * either signed or unsigned, depending on whether you use the signed or 717 * unsigned accessor function. GCC will use the signless forms to store 718 * unsigned values which have their high bit set, so we need to try to 719 * read them first as unsigned to get positive values. We could also 720 * try signed first, falling back to unsigned if we got a negative 721 * value. 722 */ 723 if (die_unsigned(dw, dim, DW_AT_upper_bound, &uval, 0)) 724 ar->ad_nelems = uval + 1; 725 else if (die_signed(dw, dim, DW_AT_upper_bound, &sval, 0)) 726 ar->ad_nelems = sval + 1; 727 else 728 ar->ad_nelems = 0; 729 730 /* 731 * Different compilers use different index types. Force the type to be 732 * a common, known value (long). 733 */ 734 ar->ad_idxtype = tdesc_intr_long(dw); 735 ar->ad_contents = ctdp; 736 737 if (ar->ad_contents->t_size != 0) { 738 dimtdp->t_size = ar->ad_contents->t_size * ar->ad_nelems; 739 dimtdp->t_flags |= TDESC_F_RESOLVED; 740 } 741} 742 743/* 744 * Create a tdesc from an array node. Some arrays will come with byte size 745 * attributes, and thus can be resolved immediately. Others don't, and will 746 * need to wait until the second pass for resolution. 747 */ 748static void 749die_array_create(dwarf_t *dw, Dwarf_Die arr, Dwarf_Off off, tdesc_t *tdp) 750{ 751 tdesc_t *arrtdp = die_lookup_pass1(dw, arr, DW_AT_type); 752 Dwarf_Unsigned uval; 753 Dwarf_Die dim; 754 755 debug(3, "die %llu: creating array\n", off); 756 757 if ((dim = die_child(dw, arr)) == NULL || 758 die_tag(dw, dim) != DW_TAG_subrange_type) 759 terminate("die %llu: failed to retrieve array bounds\n", off); 760 761 tdesc_array_create(dw, dim, arrtdp, tdp); 762 763 if (die_unsigned(dw, arr, DW_AT_byte_size, &uval, 0)) { 764 tdesc_t *dimtdp; 765 int flags; 766 767 tdp->t_size = uval; 768 769 /* 770 * Ensure that sub-dimensions have sizes too before marking 771 * as resolved. 772 */ 773 flags = TDESC_F_RESOLVED; 774 for (dimtdp = tdp->t_ardef->ad_contents; 775 dimtdp->t_type == ARRAY; 776 dimtdp = dimtdp->t_ardef->ad_contents) { 777 if (!(dimtdp->t_flags & TDESC_F_RESOLVED)) { 778 flags = 0; 779 break; 780 } 781 } 782 783 tdp->t_flags |= flags; 784 } 785 786 debug(3, "die %llu: array nelems %u size %u\n", off, 787 tdp->t_ardef->ad_nelems, tdp->t_size); 788} 789 790/*ARGSUSED1*/ 791static int 792die_array_resolve(tdesc_t *tdp, tdesc_t **tdpp, void *private) 793{ 794 dwarf_t *dw = private; 795 size_t sz; 796 797 if (tdp->t_flags & TDESC_F_RESOLVED) 798 return (1); 799 800 debug(3, "trying to resolve array %d (cont %d)\n", tdp->t_id, 801 tdp->t_ardef->ad_contents->t_id); 802 803 if ((sz = tdesc_size(tdp->t_ardef->ad_contents)) == 0) { 804 debug(3, "unable to resolve array %s (%d) contents %d\n", 805 tdesc_name(tdp), tdp->t_id, 806 tdp->t_ardef->ad_contents->t_id); 807 808 dw->dw_nunres++; 809 return (1); 810 } 811 812 tdp->t_size = sz * tdp->t_ardef->ad_nelems; 813 tdp->t_flags |= TDESC_F_RESOLVED; 814 815 debug(3, "resolved array %d: %u bytes\n", tdp->t_id, tdp->t_size); 816 817 return (1); 818} 819 820/*ARGSUSED1*/ 821static int 822die_array_failed(tdesc_t *tdp, tdesc_t **tdpp, void *private) 823{ 824 tdesc_t *cont = tdp->t_ardef->ad_contents; 825 826 if (tdp->t_flags & TDESC_F_RESOLVED) 827 return (1); 828 829 fprintf(stderr, "Array %d: failed to size contents type %s (%d)\n", 830 tdp->t_id, tdesc_name(cont), cont->t_id); 831 832 return (1); 833} 834 835/* 836 * Most enums (those with members) will be resolved during this first pass. 837 * Others - those without members (see the file comment) - won't be, and will 838 * need to wait until the second pass when they can be matched with their full 839 * definitions. 840 */ 841static void 842die_enum_create(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp) 843{ 844 Dwarf_Die mem; 845 Dwarf_Unsigned uval; 846 Dwarf_Signed sval; 847 848 debug(3, "die %llu: creating enum\n", off); 849 850 tdp->t_type = ENUM; 851 852 (void) die_unsigned(dw, die, DW_AT_byte_size, &uval, DW_ATTR_REQ); 853 tdp->t_size = uval; 854 855 if ((mem = die_child(dw, die)) != NULL) { 856 elist_t **elastp = &tdp->t_emem; 857 858 do { 859 elist_t *el; 860 861 if (die_tag(dw, mem) != DW_TAG_enumerator) { 862 /* Nested type declaration */ 863 die_create_one(dw, mem); 864 continue; 865 } 866 867 el = xcalloc(sizeof (elist_t)); 868 el->el_name = die_name(dw, mem); 869 870 if (die_signed(dw, mem, DW_AT_const_value, &sval, 0)) { 871 el->el_number = sval; 872 } else if (die_unsigned(dw, mem, DW_AT_const_value, 873 &uval, 0)) { 874 el->el_number = uval; 875 } else { 876 terminate("die %llu: enum %llu: member without " 877 "value\n", off, die_off(dw, mem)); 878 } 879 880 debug(3, "die %llu: enum %llu: created %s = %d\n", off, 881 die_off(dw, mem), el->el_name, el->el_number); 882 883 *elastp = el; 884 elastp = &el->el_next; 885 886 } while ((mem = die_sibling(dw, mem)) != NULL); 887 888 hash_add(dw->dw_enumhash, tdp); 889 890 tdp->t_flags |= TDESC_F_RESOLVED; 891 892 if (tdp->t_name != NULL) { 893 iidesc_t *ii = xcalloc(sizeof (iidesc_t)); 894 ii->ii_type = II_SOU; 895 ii->ii_name = xstrdup(tdp->t_name); 896 ii->ii_dtype = tdp; 897 898 iidesc_add(dw->dw_td->td_iihash, ii); 899 } 900 } 901} 902 903static int 904die_enum_match(void *arg1, void *arg2) 905{ 906 tdesc_t *tdp = arg1, **fullp = arg2; 907 908 if (tdp->t_emem != NULL) { 909 *fullp = tdp; 910 return (-1); /* stop the iteration */ 911 } 912 913 return (0); 914} 915 916/*ARGSUSED1*/ 917static int 918die_enum_resolve(tdesc_t *tdp, tdesc_t **tdpp, void *private) 919{ 920 dwarf_t *dw = private; 921 tdesc_t *full = NULL; 922 923 if (tdp->t_flags & TDESC_F_RESOLVED) 924 return (1); 925 926 (void) hash_find_iter(dw->dw_enumhash, tdp, die_enum_match, &full); 927 928 /* 929 * The answer to this one won't change from iteration to iteration, 930 * so don't even try. 931 */ 932 if (full == NULL) { 933 terminate("tdp %u: enum %s has no members\n", tdp->t_id, 934 tdesc_name(tdp)); 935 } 936 937 debug(3, "tdp %u: enum %s redirected to %u\n", tdp->t_id, 938 tdesc_name(tdp), full->t_id); 939 940 tdp->t_flags |= TDESC_F_RESOLVED; 941 942 return (1); 943} 944 945static int 946die_fwd_map(void *arg1, void *arg2) 947{ 948 tdesc_t *fwd = arg1, *sou = arg2; 949 950 debug(3, "tdp %u: mapped forward %s to sou %u\n", fwd->t_id, 951 tdesc_name(fwd), sou->t_id); 952 fwd->t_tdesc = sou; 953 954 return (0); 955} 956 957/* 958 * Structures and unions will never be resolved during the first pass, as we 959 * won't be able to fully determine the member sizes. The second pass, which 960 * have access to sizing information, will be able to complete the resolution. 961 */ 962static void 963die_sou_create(dwarf_t *dw, Dwarf_Die str, Dwarf_Off off, tdesc_t *tdp, 964 int type, const char *typename) 965{ 966 Dwarf_Unsigned sz, bitsz, bitoff; 967 Dwarf_Die mem; 968 mlist_t *ml, **mlastp; 969 iidesc_t *ii; 970 971 tdp->t_type = (die_isdecl(dw, str) ? FORWARD : type); 972 973 debug(3, "die %llu: creating %s %s\n", off, 974 (tdp->t_type == FORWARD ? "forward decl" : typename), 975 tdesc_name(tdp)); 976 977 if (tdp->t_type == FORWARD) { 978 hash_add(dw->dw_fwdhash, tdp); 979 return; 980 } 981 982 (void) hash_find_iter(dw->dw_fwdhash, tdp, die_fwd_map, tdp); 983 984 (void) die_unsigned(dw, str, DW_AT_byte_size, &sz, DW_ATTR_REQ); 985 tdp->t_size = sz; 986 987 /* 988 * GCC allows empty SOUs as an extension. 989 */ 990 if ((mem = die_child(dw, str)) == NULL) 991 goto out; 992 993 mlastp = &tdp->t_members; 994 995 do { 996 Dwarf_Off memoff = die_off(dw, mem); 997 Dwarf_Half tag = die_tag(dw, mem); 998 Dwarf_Unsigned mloff; 999 1000 if (tag != DW_TAG_member) { 1001 /* Nested type declaration */ 1002 die_create_one(dw, mem); 1003 continue; 1004 } 1005 1006 debug(3, "die %llu: mem %llu: creating member\n", off, memoff); 1007 1008 ml = xcalloc(sizeof (mlist_t)); 1009 1010 /* 1011 * This could be a GCC anon struct/union member, so we'll allow 1012 * an empty name, even though nothing can really handle them 1013 * properly. Note that some versions of GCC miss out debug 1014 * info for anon structs, though recent versions are fixed (gcc 1015 * bug 11816). 1016 */ 1017 if ((ml->ml_name = die_name(dw, mem)) == NULL) 1018 ml->ml_name = ""; 1019 1020 ml->ml_type = die_lookup_pass1(dw, mem, DW_AT_type); 1021 1022 if (die_mem_offset(dw, mem, DW_AT_data_member_location, 1023 &mloff, 0)) { 1024 debug(3, "die %llu: got mloff %llx\n", off, 1025 (u_longlong_t)mloff); 1026 ml->ml_offset = mloff * 8; 1027 } 1028 1029 if (die_unsigned(dw, mem, DW_AT_bit_size, &bitsz, 0)) 1030 ml->ml_size = bitsz; 1031 else 1032 ml->ml_size = tdesc_bitsize(ml->ml_type); 1033 1034 if (die_unsigned(dw, mem, DW_AT_bit_offset, &bitoff, 0)) { 1035#ifdef _BIG_ENDIAN 1036 ml->ml_offset += bitoff; 1037#else 1038 /* 1039 * A DW_AT_byte_size attribute whose value (see Section 2.19) is the 1040 * number of bytes that contain an instance of the bit field and any 1041 * padding bits. 1042 * 1043 * The byte size attribute may be omitted if the size of the object 1044 * containing the bit field can be inferred from the type attribute of 1045 * the data member containing the bit field. 1046 * 1047 * There is some cases where we don't know, yet, the 1048 * size of some intrinsic types. We must be sure that 1049 * in this case we have a AT_byte_size attribute. 1050 */ 1051 Dwarf_Unsigned bytesz = 0; 1052 if (die_unsigned(dw, mem, DW_AT_byte_size, &bytesz, 0)) { 1053 ml->ml_offset += (bytesz * 8) - bitoff - ml->ml_size; 1054 } else { 1055 size_t bitsz = tdesc_bitsize(ml->ml_type); 1056 assert((bitsz != 0) && "AT_byte_size missing and cannot figure the bitfield size"); 1057 1058 ml->ml_offset += bitsz - bitoff - ml->ml_size; 1059 } 1060#endif 1061 } 1062 1063 debug(3, "die %llu: mem %llu: created \"%s\" (off %u sz %u)\n", 1064 off, memoff, ml->ml_name, ml->ml_offset, ml->ml_size); 1065 1066 *mlastp = ml; 1067 mlastp = &ml->ml_next; 1068 } while ((mem = die_sibling(dw, mem)) != NULL); 1069 1070 /* 1071 * GCC will attempt to eliminate unused types, thus decreasing the 1072 * size of the emitted dwarf. That is, if you declare a foo_t in your 1073 * header, include said header in your source file, and neglect to 1074 * actually use (directly or indirectly) the foo_t in the source file, 1075 * the foo_t won't make it into the emitted DWARF. So, at least, goes 1076 * the theory. 1077 * 1078 * Occasionally, it'll emit the DW_TAG_structure_type for the foo_t, 1079 * and then neglect to emit the members. Strangely, the loner struct 1080 * tag will always be followed by a proper nested declaration of 1081 * something else. This is clearly a bug, but we're not going to have 1082 * time to get it fixed before this goo goes back, so we'll have to work 1083 * around it. If we see a no-membered struct with a nested declaration 1084 * (i.e. die_child of the struct tag won't be null), we'll ignore it. 1085 * Being paranoid, we won't simply remove it from the hash. Instead, 1086 * we'll decline to create an iidesc for it, thus ensuring that this 1087 * type won't make it into the output file. To be safe, we'll also 1088 * change the name. 1089 */ 1090 if (tdp->t_members == NULL) { 1091 const char *old = tdesc_name(tdp); 1092 size_t newsz = 7 + strlen(old) + 1; 1093 char *new = xmalloc(newsz); 1094 (void) snprintf(new, newsz, "orphan %s", old); 1095 1096 debug(3, "die %llu: worked around %s %s\n", off, typename, old); 1097 1098 if (tdp->t_name != NULL) 1099 free(tdp->t_name); 1100 tdp->t_name = new; 1101 return; 1102 } 1103 1104out: 1105 if (tdp->t_name != NULL) { 1106 ii = xcalloc(sizeof (iidesc_t)); 1107 ii->ii_type = II_SOU; 1108 ii->ii_name = xstrdup(tdp->t_name); 1109 ii->ii_dtype = tdp; 1110 1111 iidesc_add(dw->dw_td->td_iihash, ii); 1112 } 1113} 1114 1115static void 1116die_struct_create(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp) 1117{ 1118 die_sou_create(dw, die, off, tdp, STRUCT, "struct"); 1119} 1120 1121static void 1122die_union_create(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp) 1123{ 1124 die_sou_create(dw, die, off, tdp, UNION, "union"); 1125} 1126 1127/*ARGSUSED1*/ 1128static int 1129die_sou_resolve(tdesc_t *tdp, tdesc_t **tdpp, void *private) 1130{ 1131 dwarf_t *dw = private; 1132 mlist_t *ml; 1133 tdesc_t *mt; 1134 1135 if (tdp->t_flags & TDESC_F_RESOLVED) 1136 return (1); 1137 1138 debug(3, "resolving sou %s\n", tdesc_name(tdp)); 1139 1140 for (ml = tdp->t_members; ml != NULL; ml = ml->ml_next) { 1141 1142#if defined(__APPLE__) 1143 // Check for NULL mt before the size check. 1144 // Seeing lots of crashes due to mt being NULL in 1145 // the code below. 1146 if ((mt = tdesc_basetype(ml->ml_type)) == NULL) { 1147 dw->dw_nunres++; 1148 return (1); 1149 } 1150#endif 1151 if (ml->ml_size == 0) { 1152 mt = tdesc_basetype(ml->ml_type); 1153 1154 if ((ml->ml_size = tdesc_bitsize(mt)) != 0) 1155 continue; 1156 1157 /* 1158 * For empty members, or GCC/C99 flexible array 1159 * members, a size of 0 is correct. 1160 */ 1161 if (mt->t_members == NULL) 1162 continue; 1163 if (mt->t_type == ARRAY && mt->t_ardef->ad_nelems == 0) 1164 continue; 1165 1166 dw->dw_nunres++; 1167 return (1); 1168 } 1169 1170#if defined(__APPLE__) 1171#else 1172 if ((mt = tdesc_basetype(ml->ml_type)) == NULL) { 1173 dw->dw_nunres++; 1174 return (1); 1175 } 1176#endif 1177 1178 if (ml->ml_size != 0 && mt->t_type == INTRINSIC && 1179 mt->t_intr->intr_nbits != ml->ml_size) { 1180 /* 1181 * This member is a bitfield, and needs to reference 1182 * an intrinsic type with the same width. If the 1183 * currently-referenced type isn't of the same width, 1184 * we'll copy it, adjusting the width of the copy to 1185 * the size we'd like. 1186 */ 1187 debug(3, "tdp %u: creating bitfield for %d bits\n", 1188 tdp->t_id, ml->ml_size); 1189 1190 ml->ml_type = tdesc_intr_clone(dw, mt, ml->ml_size); 1191 } 1192 } 1193 1194 tdp->t_flags |= TDESC_F_RESOLVED; 1195 1196 return (1); 1197} 1198 1199/*ARGSUSED1*/ 1200static int 1201die_sou_failed(tdesc_t *tdp, tdesc_t **tdpp, void *private) 1202{ 1203 const char *typename = (tdp->t_type == STRUCT ? "struct" : "union"); 1204 mlist_t *ml; 1205 1206 if (tdp->t_flags & TDESC_F_RESOLVED) 1207 return (1); 1208 1209 for (ml = tdp->t_members; ml != NULL; ml = ml->ml_next) { 1210 if (ml->ml_size == 0) { 1211 fprintf(stderr, "%s %d: failed to size member \"%s\" " 1212 "of type %s (%d)\n", typename, tdp->t_id, 1213 ml->ml_name, tdesc_name(ml->ml_type), 1214 ml->ml_type->t_id); 1215 } 1216 } 1217 1218 return (1); 1219} 1220 1221static void 1222die_funcptr_create(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp) 1223{ 1224 Dwarf_Attribute attr; 1225 Dwarf_Half tag; 1226 Dwarf_Die arg; 1227 fndef_t *fn; 1228 int i; 1229 1230 debug(3, "die %llu: creating function pointer\n", off); 1231 1232 /* 1233 * We'll begin by processing any type definition nodes that may be 1234 * lurking underneath this one. 1235 */ 1236 for (arg = die_child(dw, die); arg != NULL; 1237 arg = die_sibling(dw, arg)) { 1238 if ((tag = die_tag(dw, arg)) != DW_TAG_formal_parameter && 1239 tag != DW_TAG_unspecified_parameters) { 1240 /* Nested type declaration */ 1241 die_create_one(dw, arg); 1242 } 1243 } 1244 1245 if (die_isdecl(dw, die)) { 1246 /* 1247 * This is a prototype. We don't add prototypes to the 1248 * tree, so we're going to drop the tdesc. Unfortunately, 1249 * it has already been added to the tree. Nobody will reference 1250 * it, though, and it will be leaked. 1251 */ 1252 return; 1253 } 1254 1255 fn = xcalloc(sizeof (fndef_t)); 1256 1257 tdp->t_type = FUNCTION; 1258 1259 if ((attr = die_attr(dw, die, DW_AT_type, 0)) != NULL) { 1260 dwarf_dealloc(dw->dw_dw, attr, DW_DLA_ATTR); 1261 fn->fn_ret = die_lookup_pass1(dw, die, DW_AT_type); 1262 } else { 1263 fn->fn_ret = tdesc_intr_void(dw); 1264 } 1265 1266 /* 1267 * Count the arguments to the function, then read them in. 1268 */ 1269 for (fn->fn_nargs = 0, arg = die_child(dw, die); arg != NULL; 1270 arg = die_sibling(dw, arg)) { 1271 if ((tag = die_tag(dw, arg)) == DW_TAG_formal_parameter) 1272 fn->fn_nargs++; 1273 else if (tag == DW_TAG_unspecified_parameters && 1274 fn->fn_nargs > 0) 1275 fn->fn_vargs = 1; 1276 } 1277 1278 if (fn->fn_nargs != 0) { 1279 debug(3, "die %llu: adding %d argument%s\n", off, fn->fn_nargs, 1280 (fn->fn_nargs > 1 ? "s" : "")); 1281 1282 fn->fn_args = xcalloc(sizeof (tdesc_t *) * fn->fn_nargs); 1283 for (i = 0, arg = die_child(dw, die); 1284 arg != NULL && i < fn->fn_nargs; 1285 arg = die_sibling(dw, arg)) { 1286 if (die_tag(dw, arg) != DW_TAG_formal_parameter) 1287 continue; 1288 1289 fn->fn_args[i++] = die_lookup_pass1(dw, arg, 1290 DW_AT_type); 1291 } 1292 } 1293 1294 tdp->t_fndef = fn; 1295 tdp->t_flags |= TDESC_F_RESOLVED; 1296} 1297 1298/* 1299 * GCC and DevPro use different names for the base types. While the terms are 1300 * the same, they are arranged in a different order. Some terms, such as int, 1301 * are implied in one, and explicitly named in the other. Given a base type 1302 * as input, this routine will return a common name, along with an intr_t 1303 * that reflects said name. 1304 */ 1305static intr_t * 1306die_base_name_parse(const char *name, char **newp) 1307{ 1308 char buf[100]; 1309 char *base, *c; 1310 int nlong = 0, nshort = 0, nchar = 0, nint = 0; 1311 int sign = 1; 1312 char fmt = '\0'; 1313 intr_t *intr; 1314 1315 if (strlen(name) > sizeof (buf) - 1) 1316 terminate("base type name \"%s\" is too long\n", name); 1317 1318 strncpy(buf, name, sizeof (buf)); 1319 1320 for (c = strtok(buf, " "); c != NULL; c = strtok(NULL, " ")) { 1321 if (strcmp(c, "signed") == 0) 1322 sign = 1; 1323 else if (strcmp(c, "unsigned") == 0) 1324 sign = 0; 1325 else if (strcmp(c, "long") == 0) 1326 nlong++; 1327 else if (strcmp(c, "char") == 0) { 1328 nchar++; 1329 fmt = 'c'; 1330 } else if (strcmp(c, "short") == 0) 1331 nshort++; 1332 else if (strcmp(c, "int") == 0) 1333 nint++; 1334 else { 1335 /* 1336 * If we don't recognize any of the tokens, we'll tell 1337 * the caller to fall back to the dwarf-provided 1338 * encoding information. 1339 */ 1340 return (NULL); 1341 } 1342 } 1343 1344 if (nchar > 1 || nshort > 1 || nint > 1 || nlong > 2) 1345 return (NULL); 1346 1347 if (nchar > 0) { 1348 if (nlong > 0 || nshort > 0 || nint > 0) 1349 return (NULL); 1350 1351 base = "char"; 1352 1353 } else if (nshort > 0) { 1354 if (nlong > 0) 1355 return (NULL); 1356 1357 base = "short"; 1358 1359 } else if (nlong > 0) { 1360 base = "long"; 1361 1362 } else { 1363 base = "int"; 1364 } 1365 1366 intr = xcalloc(sizeof (intr_t)); 1367 intr->intr_type = INTR_INT; 1368 intr->intr_signed = sign; 1369 intr->intr_iformat = fmt; 1370 1371 snprintf(buf, sizeof (buf), "%s%s%s", 1372 (sign ? "" : "unsigned "), 1373 (nlong > 1 ? "long " : ""), 1374 base); 1375 1376 *newp = xstrdup(buf); 1377 return (intr); 1378} 1379 1380typedef struct fp_size_map { 1381 size_t fsm_typesz[2]; /* size of {32,64} type */ 1382 uint_t fsm_enc[3]; /* CTF_FP_* for {bare,cplx,imagry} type */ 1383} fp_size_map_t; 1384 1385static const fp_size_map_t fp_encodings[] = { 1386 { { 4, 4 }, { CTF_FP_SINGLE, CTF_FP_CPLX, CTF_FP_IMAGRY } }, 1387 { { 8, 8 }, { CTF_FP_DOUBLE, CTF_FP_DCPLX, CTF_FP_DIMAGRY } }, 1388#ifdef __sparc 1389 { { 16, 16 }, { CTF_FP_LDOUBLE, CTF_FP_LDCPLX, CTF_FP_LDIMAGRY } }, 1390#else 1391 { { 12, 16 }, { CTF_FP_LDOUBLE, CTF_FP_LDCPLX, CTF_FP_LDIMAGRY } }, 1392#endif 1393 { { 0, 0 } } 1394}; 1395 1396static uint_t 1397die_base_type2enc(dwarf_t *dw, Dwarf_Off off, Dwarf_Signed enc, size_t sz) 1398{ 1399 const fp_size_map_t *map = fp_encodings; 1400 uint_t szidx = dw->dw_ptrsz == sizeof (uint64_t); 1401 uint_t mult = 1, col = 0; 1402 1403 if (enc == DW_ATE_complex_float) { 1404 mult = 2; 1405 col = 1; 1406 } else if (enc == DW_ATE_imaginary_float || 1407 enc == DW_ATE_SUN_imaginary_float) 1408 col = 2; 1409 1410 while (map->fsm_typesz[szidx] != 0) { 1411 if (map->fsm_typesz[szidx] * mult == sz) 1412 return (map->fsm_enc[col]); 1413 map++; 1414 } 1415 1416 terminate("die %llu: unrecognized real type size %u\n", off, sz); 1417 /*NOTREACHED*/ 1418 return (0); 1419} 1420 1421static intr_t * 1422die_base_from_dwarf(dwarf_t *dw, Dwarf_Die base, Dwarf_Off off, size_t sz) 1423{ 1424 intr_t *intr = xcalloc(sizeof (intr_t)); 1425 Dwarf_Signed enc; 1426 1427 (void) die_signed(dw, base, DW_AT_encoding, &enc, DW_ATTR_REQ); 1428 1429 switch (enc) { 1430 case DW_ATE_unsigned: 1431 case DW_ATE_address: 1432 intr->intr_type = INTR_INT; 1433 break; 1434 case DW_ATE_unsigned_char: 1435 intr->intr_type = INTR_INT; 1436 intr->intr_iformat = 'c'; 1437 break; 1438 case DW_ATE_signed: 1439 intr->intr_type = INTR_INT; 1440 intr->intr_signed = 1; 1441 break; 1442 case DW_ATE_signed_char: 1443 intr->intr_type = INTR_INT; 1444 intr->intr_signed = 1; 1445 intr->intr_iformat = 'c'; 1446 break; 1447 case DW_ATE_boolean: 1448 intr->intr_type = INTR_INT; 1449 intr->intr_signed = 1; 1450 intr->intr_iformat = 'b'; 1451 break; 1452 case DW_ATE_float: 1453 case DW_ATE_complex_float: 1454 case DW_ATE_imaginary_float: 1455 case DW_ATE_SUN_imaginary_float: 1456 case DW_ATE_SUN_interval_float: 1457 intr->intr_type = INTR_REAL; 1458 intr->intr_signed = 1; 1459 intr->intr_fformat = die_base_type2enc(dw, off, enc, sz); 1460 break; 1461 default: 1462 terminate("die %llu: unknown base type encoding 0x%llx\n", 1463 off, enc); 1464 } 1465 1466 return (intr); 1467} 1468 1469static void 1470die_base_create(dwarf_t *dw, Dwarf_Die base, Dwarf_Off off, tdesc_t *tdp) 1471{ 1472 Dwarf_Unsigned sz; 1473 intr_t *intr; 1474 char *new; 1475 1476 debug(3, "die %llu: creating base type\n", off); 1477 1478 /* 1479 * The compilers have their own clever (internally inconsistent) ideas 1480 * as to what base types should look like. Some times gcc will, for 1481 * example, use DW_ATE_signed_char for char. Other times, however, it 1482 * will use DW_ATE_signed. Needless to say, this causes some problems 1483 * down the road, particularly with merging. We do, however, use the 1484 * DWARF idea of type sizes, as this allows us to avoid caring about 1485 * the data model. 1486 */ 1487 (void) die_unsigned(dw, base, DW_AT_byte_size, &sz, DW_ATTR_REQ); 1488 1489 if (tdp->t_name == NULL) 1490 terminate("die %llu: base type without name\n", off); 1491 1492 /* XXX make a name parser for float too */ 1493 if ((intr = die_base_name_parse(tdp->t_name, &new)) != NULL) { 1494 /* Found it. We'll use the parsed version */ 1495 debug(3, "die %llu: name \"%s\" remapped to \"%s\"\n", off, 1496 tdesc_name(tdp), new); 1497 1498 free(tdp->t_name); 1499 tdp->t_name = new; 1500 } else { 1501 /* 1502 * We didn't recognize the type, so we'll create an intr_t 1503 * based on the DWARF data. 1504 */ 1505 debug(3, "die %llu: using dwarf data for base \"%s\"\n", off, 1506 tdesc_name(tdp)); 1507 1508 intr = die_base_from_dwarf(dw, base, off, sz); 1509 } 1510 1511 intr->intr_nbits = sz * 8; 1512 1513 tdp->t_type = INTRINSIC; 1514 tdp->t_intr = intr; 1515 tdp->t_size = sz; 1516 1517 tdp->t_flags |= TDESC_F_RESOLVED; 1518} 1519 1520static void 1521die_through_create(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp, 1522 int type, const char *typename) 1523{ 1524 Dwarf_Attribute attr; 1525 1526 debug(3, "die %llu: creating %s\n", off, typename); 1527 1528 tdp->t_type = type; 1529 1530 if ((attr = die_attr(dw, die, DW_AT_type, 0)) != NULL) { 1531 dwarf_dealloc(dw->dw_dw, attr, DW_DLA_ATTR); 1532 tdp->t_tdesc = die_lookup_pass1(dw, die, DW_AT_type); 1533 } else { 1534 tdp->t_tdesc = tdesc_intr_void(dw); 1535 } 1536 1537 if (type == POINTER) 1538 tdp->t_size = dw->dw_ptrsz; 1539 1540 tdp->t_flags |= TDESC_F_RESOLVED; 1541 1542 if (type == TYPEDEF) { 1543 iidesc_t *ii = xcalloc(sizeof (iidesc_t)); 1544 ii->ii_type = II_TYPE; 1545 ii->ii_name = xstrdup(tdp->t_name); 1546 ii->ii_dtype = tdp; 1547 1548 iidesc_add(dw->dw_td->td_iihash, ii); 1549 } 1550} 1551 1552static void 1553die_typedef_create(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp) 1554{ 1555 die_through_create(dw, die, off, tdp, TYPEDEF, "typedef"); 1556} 1557 1558static void 1559die_const_create(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp) 1560{ 1561 die_through_create(dw, die, off, tdp, CONST, "const"); 1562} 1563 1564static void 1565die_pointer_create(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp) 1566{ 1567 die_through_create(dw, die, off, tdp, POINTER, "pointer"); 1568} 1569 1570static void 1571die_restrict_create(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp) 1572{ 1573 die_through_create(dw, die, off, tdp, RESTRICT, "restrict"); 1574} 1575 1576static void 1577die_volatile_create(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp) 1578{ 1579 die_through_create(dw, die, off, tdp, VOLATILE, "volatile"); 1580} 1581 1582/*ARGSUSED3*/ 1583static void 1584die_function_create(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp) 1585{ 1586 Dwarf_Die arg; 1587 Dwarf_Half tag; 1588 iidesc_t *ii; 1589 char *name; 1590 1591 debug(3, "die %llu: creating function definition\n", off); 1592 1593 /* 1594 * We'll begin by processing any type definition nodes that may be 1595 * lurking underneath this one. 1596 */ 1597 for (arg = die_child(dw, die); arg != NULL; 1598 arg = die_sibling(dw, arg)) { 1599 if ((tag = die_tag(dw, arg)) != DW_TAG_formal_parameter && 1600 tag != DW_TAG_variable) { 1601 /* Nested type declaration */ 1602 die_create_one(dw, arg); 1603 } 1604 } 1605 1606#if defined(__APPLE__) 1607 if (die_isdecl(dw, die)) { 1608 return; /* Prototype. */ 1609 } 1610 1611 Dwarf_Die spec_die = die_specification_die(dw, die); // AT_specification indirection? 1612 if (spec_die) 1613 die = spec_die; // and we'll deallocate through spec_die below. 1614 1615 if ((name = die_linkage_name(dw, die)) != NULL) { 1616 /* and press on with this die describing a C++ method ... */ 1617 } else { 1618 name = die_name(dw, die); 1619 } 1620 1621 if (name == NULL) { 1622 return; /* Subprogram without name. */ 1623 } 1624#else 1625 if (die_isdecl(dw, die) || (name = die_name(dw, die)) == NULL) { 1626 /* 1627 * We process neither prototypes nor subprograms without 1628 * names. 1629 */ 1630 return; 1631 } 1632#endif 1633 1634 ii = xcalloc(sizeof (iidesc_t)); 1635 ii->ii_type = die_isglobal(dw, die) ? II_GFUN : II_SFUN; 1636 ii->ii_name = name; 1637 if (ii->ii_type == II_SFUN) 1638 ii->ii_owner = xstrdup(dw->dw_cuname); 1639 1640 debug(3, "die %llu: function %s is %s\n", off, ii->ii_name, 1641 (ii->ii_type == II_GFUN ? "global" : "static")); 1642 1643 if (die_attr(dw, die, DW_AT_type, 0) != NULL) 1644 ii->ii_dtype = die_lookup_pass1(dw, die, DW_AT_type); 1645 else 1646 ii->ii_dtype = tdesc_intr_void(dw); 1647 1648 for (arg = die_child(dw, die); arg != NULL; 1649 arg = die_sibling(dw, arg)) { 1650 char *name; 1651 1652 debug(3, "die %llu: looking at sub member at %llu\n", 1653 off, die_off(dw, die)); 1654 1655 if (die_tag(dw, arg) != DW_TAG_formal_parameter) 1656 continue; 1657#if defined(__APPLE__) 1658 if (die_attr(dw, die, DW_AT_type, 0) == NULL) 1659 continue; /* C++ "this" and "meta" can land here. */ 1660#endif /* __APPLE__ */ 1661 1662#if !defined(__APPLE__) 1663 if ((name = die_name(dw, arg)) == NULL) { 1664 terminate("die %llu: func arg %d has no name\n", 1665 off, ii->ii_nargs + 1); 1666 } 1667#else 1668 if ((name = die_name(dw, arg)) == NULL) { 1669 /* C++ admits this, IOKit does it, we'll allow it. */ 1670 debug(1, "die %llu: func arg %d has no name\n", 1671 off, ii->ii_nargs + 1); 1672 ii->ii_nargs++; 1673 continue; 1674 } 1675#endif /* __APPLE__ */ 1676 1677 if (strcmp(name, "...") == 0) { 1678 free(name); 1679 ii->ii_vargs = 1; 1680 continue; 1681 } 1682 1683 ii->ii_nargs++; 1684 } 1685 1686 if (ii->ii_nargs > 0) { 1687 int i; 1688 1689 debug(3, "die %llu: function has %d argument%s\n", off, 1690 ii->ii_nargs, (ii->ii_nargs == 1 ? "" : "s")); 1691 1692 ii->ii_args = xcalloc(sizeof (tdesc_t) * ii->ii_nargs); 1693 1694 for (arg = die_child(dw, die), i = 0; 1695 arg != NULL && i < ii->ii_nargs; 1696 arg = die_sibling(dw, arg)) { 1697 if (die_tag(dw, arg) != DW_TAG_formal_parameter) 1698 continue; 1699#if defined(__APPLE__) 1700 if (die_attr(dw, die, DW_AT_type, 0) == NULL) 1701 continue; /* C++ "this" and "meta" can land here. */ 1702#endif /* __APPLE__ */ 1703 ii->ii_args[i++] = die_lookup_pass1(dw, arg, 1704 DW_AT_type); 1705 } 1706 } 1707 1708 iidesc_add(dw->dw_td->td_iihash, ii); 1709 1710#if defined(__APPLE__) 1711 if (spec_die) 1712 dwarf_dealloc(dw->dw_dw, spec_die, DW_DLA_DIE); 1713#endif /* __APPLE__ */ 1714} 1715 1716/*ARGSUSED3*/ 1717static void 1718die_variable_create(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp) 1719{ 1720 iidesc_t *ii; 1721 char *name; 1722 1723 debug(3, "die %llu: creating object definition\n", off); 1724 1725 if (die_isdecl(dw, die) || (name = die_name(dw, die)) == NULL) 1726 return; /* skip prototypes and nameless objects */ 1727 1728 ii = xcalloc(sizeof (iidesc_t)); 1729 ii->ii_type = die_isglobal(dw, die) ? II_GVAR : II_SVAR; 1730 ii->ii_name = name; 1731 ii->ii_dtype = die_lookup_pass1(dw, die, DW_AT_type); 1732 if (ii->ii_type == II_SVAR) 1733 ii->ii_owner = xstrdup(dw->dw_cuname); 1734 1735 iidesc_add(dw->dw_td->td_iihash, ii); 1736} 1737 1738/*ARGSUSED2*/ 1739static int 1740die_fwd_resolve(tdesc_t *fwd, tdesc_t **fwdp, void *private) 1741{ 1742 if (fwd->t_flags & TDESC_F_RESOLVED) 1743 return (1); 1744 1745 if (fwd->t_tdesc != NULL) { 1746 debug(3, "tdp %u: unforwarded %s\n", fwd->t_id, 1747 tdesc_name(fwd)); 1748 *fwdp = fwd->t_tdesc; 1749 } 1750 1751 fwd->t_flags |= TDESC_F_RESOLVED; 1752 1753 return (1); 1754} 1755 1756/*ARGSUSED*/ 1757static void 1758die_lexblk_descend(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp) 1759{ 1760 Dwarf_Die child = die_child(dw, die); 1761 1762 if (child != NULL) 1763 die_create(dw, child); 1764} 1765 1766/* 1767 * Used to map the die to a routine which can parse it, using the tag to do the 1768 * mapping. While the processing of most tags entails the creation of a tdesc, 1769 * there are a few which don't - primarily those which result in the creation of 1770 * iidescs which refer to existing tdescs. 1771 */ 1772 1773#define DW_F_NOTDP 0x1 /* Don't create a tdesc for the creator */ 1774 1775typedef struct die_creator { 1776 Dwarf_Half dc_tag; 1777 uint16_t dc_flags; 1778 void (*dc_create)(dwarf_t *, Dwarf_Die, Dwarf_Off, tdesc_t *); 1779} die_creator_t; 1780 1781static const die_creator_t die_creators[] = { 1782 { DW_TAG_array_type, 0, die_array_create }, 1783 { DW_TAG_enumeration_type, 0, die_enum_create }, 1784 { DW_TAG_lexical_block, DW_F_NOTDP, die_lexblk_descend }, 1785 { DW_TAG_pointer_type, 0, die_pointer_create }, 1786 { DW_TAG_structure_type, 0, die_struct_create }, 1787 { DW_TAG_subroutine_type, 0, die_funcptr_create }, 1788 { DW_TAG_typedef, 0, die_typedef_create }, 1789 { DW_TAG_union_type, 0, die_union_create }, 1790 { DW_TAG_base_type, 0, die_base_create }, 1791 { DW_TAG_const_type, 0, die_const_create }, 1792 { DW_TAG_subprogram, DW_F_NOTDP, die_function_create }, 1793 { DW_TAG_variable, DW_F_NOTDP, die_variable_create }, 1794 { DW_TAG_volatile_type, 0, die_volatile_create }, 1795 { DW_TAG_restrict_type, 0, die_restrict_create }, 1796 { 0, NULL } 1797}; 1798 1799static const die_creator_t * 1800die_tag2ctor(Dwarf_Half tag) 1801{ 1802 const die_creator_t *dc; 1803 1804 for (dc = die_creators; dc->dc_create != NULL; dc++) { 1805 if (dc->dc_tag == tag) 1806 return (dc); 1807 } 1808 1809 return (NULL); 1810} 1811 1812static void 1813die_create_one(dwarf_t *dw, Dwarf_Die die) 1814{ 1815 Dwarf_Off off = die_off(dw, die); 1816 const die_creator_t *dc; 1817 Dwarf_Half tag; 1818 tdesc_t *tdp; 1819 1820 debug(3, "die %llu: create_one\n", off); 1821 1822 if (off > dw->dw_maxoff) { 1823 terminate("illegal die offset %llu (max %llu)\n", off, 1824 dw->dw_maxoff); 1825 } 1826 1827 tag = die_tag(dw, die); 1828 1829 if ((dc = die_tag2ctor(tag)) == NULL) { 1830 debug(2, "die %llu: ignoring tag type %x\n", off, tag); 1831 return; 1832 } 1833 1834 if ((tdp = tdesc_lookup(dw, off)) == NULL && 1835 !(dc->dc_flags & DW_F_NOTDP)) { 1836 tdp = xcalloc(sizeof (tdesc_t)); 1837 tdp->t_id = off; 1838 tdesc_add(dw, tdp); 1839 } 1840 1841 if (tdp != NULL) 1842 tdp->t_name = die_name(dw, die); 1843 1844#if defined(__APPLE__) 1845#warning BOGUS workaround (nameless base die emitted by gcc)! 1846 /* gcc 5402 emits nameless die that are base types. Workaround imminent failure. */ 1847 if (tdp != NULL && NULL == tdp->t_name && dc->dc_create == die_base_create) 1848 tdp->t_name = xstrdup("unsigned int"); 1849#endif /* __APPLE__ */ 1850 1851 dc->dc_create(dw, die, off, tdp); 1852} 1853 1854static void 1855die_create(dwarf_t *dw, Dwarf_Die die) 1856{ 1857 do { 1858 die_create_one(dw, die); 1859 } while ((die = die_sibling(dw, die)) != NULL); 1860} 1861 1862static tdtrav_cb_f die_resolvers[] = { 1863 NULL, 1864 NULL, /* intrinsic */ 1865 NULL, /* pointer */ 1866 die_array_resolve, /* array */ 1867 NULL, /* function */ 1868 die_sou_resolve, /* struct */ 1869 die_sou_resolve, /* union */ 1870 die_enum_resolve, /* enum */ 1871 die_fwd_resolve, /* forward */ 1872 NULL, /* typedef */ 1873 NULL, /* typedef unres */ 1874 NULL, /* volatile */ 1875 NULL, /* const */ 1876 NULL, /* restrict */ 1877}; 1878 1879static tdtrav_cb_f die_fail_reporters[] = { 1880 NULL, 1881 NULL, /* intrinsic */ 1882 NULL, /* pointer */ 1883 die_array_failed, /* array */ 1884 NULL, /* function */ 1885 die_sou_failed, /* struct */ 1886 die_sou_failed, /* union */ 1887 NULL, /* enum */ 1888 NULL, /* forward */ 1889 NULL, /* typedef */ 1890 NULL, /* typedef unres */ 1891 NULL, /* volatile */ 1892 NULL, /* const */ 1893 NULL, /* restrict */ 1894}; 1895 1896static void 1897die_resolve(dwarf_t *dw) 1898{ 1899 int last = -1; 1900 int pass = 0; 1901 1902 do { 1903 pass++; 1904 dw->dw_nunres = 0; 1905 1906 (void) iitraverse_hash(dw->dw_td->td_iihash, 1907 &dw->dw_td->td_curvgen, NULL, NULL, die_resolvers, dw); 1908 1909 debug(3, "resolve: pass %d, %u left\n", pass, dw->dw_nunres); 1910 1911 if (dw->dw_nunres == last) { 1912 fprintf(stderr, "%s: failed to resolve the following " 1913 "types:\n", progname); 1914 1915 (void) iitraverse_hash(dw->dw_td->td_iihash, 1916 &dw->dw_td->td_curvgen, NULL, NULL, 1917 die_fail_reporters, dw); 1918 1919 terminate("failed to resolve types\n"); 1920 } 1921 1922 last = dw->dw_nunres; 1923 1924 } while (dw->dw_nunres != 0); 1925} 1926 1927/*ARGSUSED*/ 1928int 1929dw_read(tdata_t *td, Elf *elf, const char *filename) 1930{ 1931 Dwarf_Unsigned abboff, hdrlen, nxthdr; 1932 Dwarf_Half vers, addrsz; 1933 Dwarf_Die cu, child; 1934 dwarf_t dw; 1935 char *prod = NULL; 1936 int rc; 1937 1938 bzero(&dw, sizeof (dwarf_t)); 1939 dw.dw_td = td; 1940 dw.dw_ptrsz = elf_ptrsz(elf); 1941 dw.dw_mfgtid_last = TID_MFGTID_BASE; 1942 dw.dw_tidhash = hash_new(TDESC_HASH_BUCKETS, tdesc_idhash, tdesc_idcmp); 1943 dw.dw_fwdhash = hash_new(TDESC_HASH_BUCKETS, tdesc_namehash, 1944 tdesc_namecmp); 1945 dw.dw_enumhash = hash_new(TDESC_HASH_BUCKETS, tdesc_namehash, 1946 tdesc_namecmp); 1947 1948 if ((rc = dwarf_elf_init(elf, DW_DLC_READ, NULL, NULL, &dw.dw_dw, 1949 &dw.dw_err)) == DW_DLV_NO_ENTRY) { 1950 errno = ENOENT; 1951 return (-1); 1952 } else if (rc != DW_DLV_OK) { 1953 if (dwarf_errno(dw.dw_err) == DW_DLE_DEBUG_INFO_NULL) { 1954 /* 1955 * There's no type data in the DWARF section, but 1956 * libdwarf is too clever to handle that properly. 1957 */ 1958 return (0); 1959 } 1960 1961 terminate("failed to initialize DWARF: %s\n", 1962 dwarf_errmsg(dw.dw_err)); 1963 } 1964 1965 if ((rc = dwarf_next_cu_header(dw.dw_dw, &hdrlen, &vers, &abboff, 1966 &addrsz, &nxthdr, &dw.dw_err)) != DW_DLV_OK || 1967 (cu = die_sibling(&dw, NULL)) == NULL || 1968 (child = die_child(&dw, cu)) == NULL) 1969 terminate("file does not contain dwarf type data " 1970 "(try compiling with -g)\n"); 1971 1972 dw.dw_maxoff = nxthdr - 1; 1973 1974 if (dw.dw_maxoff > TID_FILEMAX) 1975 terminate("file contains too many types\n"); 1976 1977 debug(1, "DWARF version: %d\n", vers); 1978 if (vers != DWARF_VERSION && vers != DWARF_VERSION3 && vers != DWARF_VERSION4) { 1979 terminate("file contains incompatible version %d DWARF code " 1980 "(version 2 required)\n", vers); 1981 } 1982 1983 if (die_string(&dw, cu, DW_AT_producer, &prod, 0)) { 1984 debug(1, "DWARF emitter: %s\n", prod); 1985 free(prod); 1986 } 1987 1988 if ((dw.dw_cuname = die_name(&dw, cu)) != NULL) { 1989 char *base = xstrdup(basename(dw.dw_cuname)); 1990 free(dw.dw_cuname); 1991 dw.dw_cuname = base; 1992 1993 debug(1, "CU name: %s\n", dw.dw_cuname); 1994 } 1995 1996 die_create(&dw, child); 1997 1998 if ((rc = dwarf_next_cu_header(dw.dw_dw, &hdrlen, &vers, &abboff, 1999 &addrsz, &nxthdr, &dw.dw_err)) != DW_DLV_NO_ENTRY) 2000 terminate("multiple compilation units not supported\n"); 2001 2002 (void) dwarf_finish(dw.dw_dw, &dw.dw_err); 2003 2004 die_resolve(&dw); 2005 2006#if !defined(__APPLE__) 2007 cvt_fixups(td, dw.dw_ptrsz); 2008#else 2009 /* Ignore Solaris gore. See on-src-20080707/usr/src/tools/ctf/cvt/fixup_tdescs.c */ 2010#endif 2011 2012 /* leak the dwarf_t */ 2013 2014 return (0); 2015} 2016