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