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 = ENUM; 771 772 (void) die_unsigned(dw, die, DW_AT_byte_size, &uval, DW_ATTR_REQ); 773 /* Check for bogus gcc DW_AT_byte_size attribute */ 774 if (uval == 0xffffffff) { 775 printf("dwarf.c:%s() working around bogus DW_AT_byte_size = 0xffffffff\n", __func__); 776 uval = 0; 777 } 778 tdp->t_size = uval; 779 780 if ((mem = die_child(dw, die)) != NULL) { 781 elist_t **elastp = &tdp->t_emem; 782 783 do { 784 elist_t *el; 785 786 if (die_tag(dw, mem) != DW_TAG_enumerator) { 787 /* Nested type declaration */ 788 die_create_one(dw, mem); 789 continue; 790 } 791 792 el = xcalloc(sizeof (elist_t)); 793 el->el_name = die_name(dw, mem); 794 795 if (die_signed(dw, mem, DW_AT_const_value, &sval, 0)) { 796 el->el_number = sval; 797 } else if (die_unsigned(dw, mem, DW_AT_const_value, 798 &uval, 0)) { 799 el->el_number = uval; 800 } else { 801 terminate("die %llu: enum %llu: member without " 802 "value\n", off, die_off(dw, mem)); 803 } 804 805 debug(3, "die %llu: enum %llu: created %s = %d\n", off, 806 die_off(dw, mem), el->el_name, el->el_number); 807 808 *elastp = el; 809 elastp = &el->el_next; 810 811 } while ((mem = die_sibling(dw, mem)) != NULL); 812 813 hash_add(dw->dw_enumhash, tdp); 814 815 tdp->t_flags |= TDESC_F_RESOLVED; 816 817 if (tdp->t_name != NULL) { 818 iidesc_t *ii = xcalloc(sizeof (iidesc_t)); 819 ii->ii_type = II_SOU; 820 ii->ii_name = xstrdup(tdp->t_name); 821 ii->ii_dtype = tdp; 822 823 iidesc_add(dw->dw_td->td_iihash, ii); 824 } 825 } 826 } 827 828 static int 829 die_enum_match(void *arg1, void *arg2) 830 { 831 tdesc_t *tdp = arg1, **fullp = arg2; 832 833 if (tdp->t_emem != NULL) { 834 *fullp = tdp; 835 return (-1); /* stop the iteration */ 836 } 837 838 return (0); 839 } 840 841 /*ARGSUSED1*/ 842 static int 843 die_enum_resolve(tdesc_t *tdp, tdesc_t **tdpp __unused, void *private) 844 { 845 dwarf_t *dw = private; 846 tdesc_t *full = NULL; 847 848 if (tdp->t_flags & TDESC_F_RESOLVED) 849 return (1); 850 851 (void) hash_find_iter(dw->dw_enumhash, tdp, die_enum_match, &full); 852 853 /* 854 * The answer to this one won't change from iteration to iteration, 855 * so don't even try. 856 */ 857 if (full == NULL) { 858 terminate("tdp %u: enum %s has no members\n", tdp->t_id, 859 tdesc_name(tdp)); 860 } 861 862 debug(3, "tdp %u: enum %s redirected to %u\n", tdp->t_id, 863 tdesc_name(tdp), full->t_id); 864 865 tdp->t_flags |= TDESC_F_RESOLVED; 866 867 return (1); 868 } 869 870 static int 871 die_fwd_map(void *arg1, void *arg2) 872 { 873 tdesc_t *fwd = arg1, *sou = arg2; 874 875 debug(3, "tdp %u: mapped forward %s to sou %u\n", fwd->t_id, 876 tdesc_name(fwd), sou->t_id); 877 fwd->t_tdesc = sou; 878 879 return (0); 880 } 881 882 /* 883 * Structures and unions will never be resolved during the first pass, as we 884 * won't be able to fully determine the member sizes. The second pass, which 885 * have access to sizing information, will be able to complete the resolution. 886 */ 887 static void 888 die_sou_create(dwarf_t *dw, Dwarf_Die str, Dwarf_Off off, tdesc_t *tdp, 889 int type, const char *typename) 890 { 891 Dwarf_Unsigned sz, bitsz, bitoff, maxsz=0; 892 Dwarf_Die mem; 893 mlist_t *ml, **mlastp; 894 iidesc_t *ii; 895 896 tdp->t_type = (die_isdecl(dw, str) ? FORWARD : type); 897 898 debug(3, "die %llu: creating %s %s\n", off, 899 (tdp->t_type == FORWARD ? "forward decl" : typename), 900 tdesc_name(tdp)); 901 902 if (tdp->t_type == FORWARD) { 903 hash_add(dw->dw_fwdhash, tdp); 904 return; 905 } 906 907 (void) hash_find_iter(dw->dw_fwdhash, tdp, die_fwd_map, tdp); 908 909 (void) die_unsigned(dw, str, DW_AT_byte_size, &sz, DW_ATTR_REQ); 910 tdp->t_size = sz; 911 912 /* 913 * GCC allows empty SOUs as an extension. 914 */ 915 if ((mem = die_child(dw, str)) == NULL) { 916 goto out; 917 } 918 919 mlastp = &tdp->t_members; 920 921 do { 922 Dwarf_Off memoff = die_off(dw, mem); 923 Dwarf_Half tag = die_tag(dw, mem); 924 Dwarf_Unsigned mloff; 925 926 if (tag != DW_TAG_member) { 927 /* Nested type declaration */ 928 die_create_one(dw, mem); 929 continue; 930 } 931 932 debug(3, "die %llu: mem %llu: creating member\n", off, memoff); 933 934 ml = xcalloc(sizeof (mlist_t)); 935 936 /* 937 * This could be a GCC anon struct/union member, so we'll allow 938 * an empty name, even though nothing can really handle them 939 * properly. Note that some versions of GCC miss out debug 940 * info for anon structs, though recent versions are fixed (gcc 941 * bug 11816). 942 */ 943 if ((ml->ml_name = die_name(dw, mem)) == NULL) 944 ml->ml_name = NULL; 945 946 ml->ml_type = die_lookup_pass1(dw, mem, DW_AT_type); 947 debug(3, "die_sou_create(): ml_type = %p t_id = %d\n", ml->ml_type, 948 ml->ml_type->t_id); 949 950 if (die_mem_offset(dw, mem, DW_AT_data_member_location, 951 &mloff, 0)) { 952 debug(3, "die %llu: got mloff %llx\n", off, 953 (u_longlong_t)mloff); 954 ml->ml_offset = mloff * 8; 955 } 956 957 if (die_unsigned(dw, mem, DW_AT_bit_size, &bitsz, 0)) 958 ml->ml_size = bitsz; 959 else 960 ml->ml_size = tdesc_bitsize(ml->ml_type); 961 962 if (die_unsigned(dw, mem, DW_AT_bit_offset, &bitoff, 0)) { 963 #if BYTE_ORDER == _BIG_ENDIAN 964 ml->ml_offset += bitoff; 965 #else 966 ml->ml_offset += tdesc_bitsize(ml->ml_type) - bitoff - 967 ml->ml_size; 968 #endif 969 } 970 971 debug(3, "die %llu: mem %llu: created \"%s\" (off %u sz %u)\n", 972 off, memoff, ml->ml_name, ml->ml_offset, ml->ml_size); 973 974 *mlastp = ml; 975 mlastp = &ml->ml_next; 976 977 /* work out the size of the largest member to work around a gcc bug */ 978 if (maxsz < ml->ml_size) { 979 maxsz = ml->ml_size; 980 } 981 } while ((mem = die_sibling(dw, mem)) != NULL); 982 983 /* See if we got a bogus DW_AT_byte_size. GCC will sometimes 984 * emit this. 985 */ 986 if (sz == 0xffffffff) { 987 printf("dwarf.c:%s() working around bogus DW_AT_byte_size = 0xffffffff\n", __func__); 988 tdp->t_size = maxsz / 8; /* maxsz is in bits, t_size is bytes */ 989 } 990 991 /* 992 * GCC will attempt to eliminate unused types, thus decreasing the 993 * size of the emitted dwarf. That is, if you declare a foo_t in your 994 * header, include said header in your source file, and neglect to 995 * actually use (directly or indirectly) the foo_t in the source file, 996 * the foo_t won't make it into the emitted DWARF. So, at least, goes 997 * the theory. 998 * 999 * Occasionally, it'll emit the DW_TAG_structure_type for the foo_t, 1000 * and then neglect to emit the members. Strangely, the loner struct 1001 * tag will always be followed by a proper nested declaration of 1002 * something else. This is clearly a bug, but we're not going to have 1003 * time to get it fixed before this goo goes back, so we'll have to work 1004 * around it. If we see a no-membered struct with a nested declaration 1005 * (i.e. die_child of the struct tag won't be null), we'll ignore it. 1006 * Being paranoid, we won't simply remove it from the hash. Instead, 1007 * we'll decline to create an iidesc for it, thus ensuring that this 1008 * type won't make it into the output file. To be safe, we'll also 1009 * change the name. 1010 */ 1011 if (tdp->t_members == NULL) { 1012 const char *old = tdesc_name(tdp); 1013 size_t newsz = 7 + strlen(old) + 1; 1014 char *new = xmalloc(newsz); 1015 (void) snprintf(new, newsz, "orphan %s", old); 1016 1017 debug(3, "die %llu: worked around %s %s\n", off, typename, old); 1018 1019 if (tdp->t_name != NULL) 1020 free(tdp->t_name); 1021 tdp->t_name = new; 1022 return; 1023 } 1024 1025 out: 1026 if (tdp->t_name != NULL) { 1027 ii = xcalloc(sizeof (iidesc_t)); 1028 ii->ii_type = II_SOU; 1029 ii->ii_name = xstrdup(tdp->t_name); 1030 ii->ii_dtype = tdp; 1031 1032 iidesc_add(dw->dw_td->td_iihash, ii); 1033 } 1034 } 1035 1036 static void 1037 die_struct_create(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp) 1038 { 1039 die_sou_create(dw, die, off, tdp, STRUCT, "struct"); 1040 } 1041 1042 static void 1043 die_union_create(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp) 1044 { 1045 die_sou_create(dw, die, off, tdp, UNION, "union"); 1046 } 1047 1048 /*ARGSUSED1*/ 1049 static int 1050 die_sou_resolve(tdesc_t *tdp, tdesc_t **tdpp __unused, void *private) 1051 { 1052 dwarf_t *dw = private; 1053 mlist_t *ml; 1054 tdesc_t *mt; 1055 1056 if (tdp->t_flags & TDESC_F_RESOLVED) 1057 return (1); 1058 1059 debug(3, "resolving sou %s [%d]\n", tdesc_name(tdp), tdp->t_id); 1060 1061 for (ml = tdp->t_members; ml != NULL; ml = ml->ml_next) { 1062 if (ml->ml_size == 0) { 1063 mt = tdesc_basetype(ml->ml_type); 1064 1065 if ((ml->ml_size = tdesc_bitsize(mt)) != 0) 1066 continue; 1067 1068 /* 1069 * For empty members, or GCC/C99 flexible array 1070 * members, a size of 0 is correct. 1071 */ 1072 if (mt->t_members == NULL) 1073 continue; 1074 if (mt->t_type == ARRAY && mt->t_ardef->ad_nelems == 0) 1075 continue; 1076 if (mt->t_type == STRUCT && 1077 mt->t_members != NULL && 1078 mt->t_members->ml_type->t_type == ARRAY && 1079 mt->t_members->ml_type->t_ardef->ad_nelems == 0) { 1080 /* struct with zero sized array */ 1081 continue; 1082 } 1083 1084 printf("%s unresolved type = %d (%s)\n", tdesc_name(tdp), 1085 mt->t_type, tdesc_name(mt)); 1086 dw->dw_nunres++; 1087 return (1); 1088 } 1089 1090 if ((mt = tdesc_basetype(ml->ml_type)) == NULL) { 1091 dw->dw_nunres++; 1092 return (1); 1093 } 1094 1095 if (ml->ml_size != 0 && mt->t_type == INTRINSIC && 1096 mt->t_intr->intr_nbits != ml->ml_size) { 1097 /* 1098 * This member is a bitfield, and needs to reference 1099 * an intrinsic type with the same width. If the 1100 * currently-referenced type isn't of the same width, 1101 * we'll copy it, adjusting the width of the copy to 1102 * the size we'd like. 1103 */ 1104 debug(3, "tdp %u: creating bitfield for %d bits\n", 1105 tdp->t_id, ml->ml_size); 1106 1107 ml->ml_type = tdesc_intr_clone(dw, mt, ml->ml_size); 1108 } 1109 } 1110 1111 tdp->t_flags |= TDESC_F_RESOLVED; 1112 1113 return (1); 1114 } 1115 1116 /*ARGSUSED1*/ 1117 static int 1118 die_sou_failed(tdesc_t *tdp, tdesc_t **tdpp __unused, void *private __unused) 1119 { 1120 const char *typename = (tdp->t_type == STRUCT ? "struct" : "union"); 1121 mlist_t *ml; 1122 1123 if (tdp->t_flags & TDESC_F_RESOLVED) 1124 return (1); 1125 1126 for (ml = tdp->t_members; ml != NULL; ml = ml->ml_next) { 1127 if (ml->ml_size == 0) { 1128 fprintf(stderr, "%s %d <%x>: failed to size member \"%s\" " 1129 "of type %s (%d <%x>)\n", typename, tdp->t_id, 1130 tdp->t_id, 1131 ml->ml_name, tdesc_name(ml->ml_type), 1132 ml->ml_type->t_id, ml->ml_type->t_id); 1133 } 1134 } 1135 1136 return (1); 1137 } 1138 1139 static void 1140 die_funcptr_create(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp) 1141 { 1142 Dwarf_Attribute attr; 1143 Dwarf_Half tag; 1144 Dwarf_Die arg; 1145 fndef_t *fn; 1146 int i; 1147 1148 debug(3, "die %llu <%llx>: creating function pointer\n", off, off); 1149 1150 /* 1151 * We'll begin by processing any type definition nodes that may be 1152 * lurking underneath this one. 1153 */ 1154 for (arg = die_child(dw, die); arg != NULL; 1155 arg = die_sibling(dw, arg)) { 1156 if ((tag = die_tag(dw, arg)) != DW_TAG_formal_parameter && 1157 tag != DW_TAG_unspecified_parameters) { 1158 /* Nested type declaration */ 1159 die_create_one(dw, arg); 1160 } 1161 } 1162 1163 if (die_isdecl(dw, die)) { 1164 /* 1165 * This is a prototype. We don't add prototypes to the 1166 * tree, so we're going to drop the tdesc. Unfortunately, 1167 * it has already been added to the tree. Nobody will reference 1168 * it, though, and it will be leaked. 1169 */ 1170 return; 1171 } 1172 1173 fn = xcalloc(sizeof (fndef_t)); 1174 1175 tdp->t_type = FUNCTION; 1176 1177 if ((attr = die_attr(dw, die, DW_AT_type, 0)) != NULL) { 1178 fn->fn_ret = die_lookup_pass1(dw, die, DW_AT_type); 1179 } else { 1180 fn->fn_ret = tdesc_intr_void(dw); 1181 } 1182 1183 /* 1184 * Count the arguments to the function, then read them in. 1185 */ 1186 for (fn->fn_nargs = 0, arg = die_child(dw, die); arg != NULL; 1187 arg = die_sibling(dw, arg)) { 1188 if ((tag = die_tag(dw, arg)) == DW_TAG_formal_parameter) 1189 fn->fn_nargs++; 1190 else if (tag == DW_TAG_unspecified_parameters && 1191 fn->fn_nargs > 0) 1192 fn->fn_vargs = 1; 1193 } 1194 1195 if (fn->fn_nargs != 0) { 1196 debug(3, "die %llu: adding %d argument%s\n", off, fn->fn_nargs, 1197 (fn->fn_nargs > 1 ? "s" : "")); 1198 1199 fn->fn_args = xcalloc(sizeof (tdesc_t *) * fn->fn_nargs); 1200 for (i = 0, arg = die_child(dw, die); 1201 arg != NULL && i < (int) fn->fn_nargs; 1202 arg = die_sibling(dw, arg)) { 1203 if (die_tag(dw, arg) != DW_TAG_formal_parameter) 1204 continue; 1205 1206 fn->fn_args[i++] = die_lookup_pass1(dw, arg, 1207 DW_AT_type); 1208 } 1209 } 1210 1211 tdp->t_fndef = fn; 1212 tdp->t_flags |= TDESC_F_RESOLVED; 1213 } 1214 1215 /* 1216 * GCC and DevPro use different names for the base types. While the terms are 1217 * the same, they are arranged in a different order. Some terms, such as int, 1218 * are implied in one, and explicitly named in the other. Given a base type 1219 * as input, this routine will return a common name, along with an intr_t 1220 * that reflects said name. 1221 */ 1222 static intr_t * 1223 die_base_name_parse(const char *name, char **newp) 1224 { 1225 char buf[100]; 1226 char const *base; 1227 char *c; 1228 int nlong = 0, nshort = 0, nchar = 0, nint = 0; 1229 int sign = 1; 1230 char fmt = '\0'; 1231 intr_t *intr; 1232 1233 if (strlen(name) > sizeof (buf) - 1) 1234 terminate("base type name \"%s\" is too long\n", name); 1235 1236 strncpy(buf, name, sizeof (buf)); 1237 1238 for (c = strtok(buf, " "); c != NULL; c = strtok(NULL, " ")) { 1239 if (strcmp(c, "signed") == 0) 1240 sign = 1; 1241 else if (strcmp(c, "unsigned") == 0) 1242 sign = 0; 1243 else if (strcmp(c, "long") == 0) 1244 nlong++; 1245 else if (strcmp(c, "char") == 0) { 1246 nchar++; 1247 fmt = 'c'; 1248 } else if (strcmp(c, "short") == 0) 1249 nshort++; 1250 else if (strcmp(c, "int") == 0) 1251 nint++; 1252 else { 1253 /* 1254 * If we don't recognize any of the tokens, we'll tell 1255 * the caller to fall back to the dwarf-provided 1256 * encoding information. 1257 */ 1258 return (NULL); 1259 } 1260 } 1261 1262 if (nchar > 1 || nshort > 1 || nint > 1 || nlong > 2) 1263 return (NULL); 1264 1265 if (nchar > 0) { 1266 if (nlong > 0 || nshort > 0 || nint > 0) 1267 return (NULL); 1268 1269 base = "char"; 1270 1271 } else if (nshort > 0) { 1272 if (nlong > 0) 1273 return (NULL); 1274 1275 base = "short"; 1276 1277 } else if (nlong > 0) { 1278 base = "long"; 1279 1280 } else { 1281 base = "int"; 1282 } 1283 1284 intr = xcalloc(sizeof (intr_t)); 1285 intr->intr_type = INTR_INT; 1286 intr->intr_signed = sign; 1287 intr->intr_iformat = fmt; 1288 1289 snprintf(buf, sizeof (buf), "%s%s%s", 1290 (sign ? "" : "unsigned "), 1291 (nlong > 1 ? "long " : ""), 1292 base); 1293 1294 *newp = xstrdup(buf); 1295 return (intr); 1296 } 1297 1298 typedef struct fp_size_map { 1299 size_t fsm_typesz[2]; /* size of {32,64} type */ 1300 uint_t fsm_enc[3]; /* CTF_FP_* for {bare,cplx,imagry} type */ 1301 } fp_size_map_t; 1302 1303 static const fp_size_map_t fp_encodings[] = { 1304 { { 4, 4 }, { CTF_FP_SINGLE, CTF_FP_CPLX, CTF_FP_IMAGRY } }, 1305 { { 8, 8 }, { CTF_FP_DOUBLE, CTF_FP_DCPLX, CTF_FP_DIMAGRY } }, 1306 #ifdef __sparc 1307 { { 16, 16 }, { CTF_FP_LDOUBLE, CTF_FP_LDCPLX, CTF_FP_LDIMAGRY } }, 1308 #else 1309 { { 12, 16 }, { CTF_FP_LDOUBLE, CTF_FP_LDCPLX, CTF_FP_LDIMAGRY } }, 1310 #endif 1311 { { 0, 0 }, { 0, 0, 0 } } 1312 }; 1313 1314 static uint_t 1315 die_base_type2enc(dwarf_t *dw, Dwarf_Off off, Dwarf_Signed enc, size_t sz) 1316 { 1317 const fp_size_map_t *map = fp_encodings; 1318 uint_t szidx = dw->dw_ptrsz == sizeof (uint64_t); 1319 uint_t mult = 1, col = 0; 1320 1321 if (enc == DW_ATE_complex_float) { 1322 mult = 2; 1323 col = 1; 1324 } else if (enc == DW_ATE_imaginary_float 1325 #if defined(sun) 1326 || enc == DW_ATE_SUN_imaginary_float 1327 #endif 1328 ) 1329 col = 2; 1330 1331 while (map->fsm_typesz[szidx] != 0) { 1332 if (map->fsm_typesz[szidx] * mult == sz) 1333 return (map->fsm_enc[col]); 1334 map++; 1335 } 1336 1337 terminate("die %llu: unrecognized real type size %u\n", off, sz); 1338 /*NOTREACHED*/ 1339 return (0); 1340 } 1341 1342 static intr_t * 1343 die_base_from_dwarf(dwarf_t *dw, Dwarf_Die base, Dwarf_Off off, size_t sz) 1344 { 1345 intr_t *intr = xcalloc(sizeof (intr_t)); 1346 Dwarf_Signed enc; 1347 1348 (void) die_signed(dw, base, DW_AT_encoding, &enc, DW_ATTR_REQ); 1349 1350 switch (enc) { 1351 case DW_ATE_unsigned: 1352 case DW_ATE_address: 1353 intr->intr_type = INTR_INT; 1354 break; 1355 case DW_ATE_unsigned_char: 1356 intr->intr_type = INTR_INT; 1357 intr->intr_iformat = 'c'; 1358 break; 1359 case DW_ATE_signed: 1360 intr->intr_type = INTR_INT; 1361 intr->intr_signed = 1; 1362 break; 1363 case DW_ATE_signed_char: 1364 intr->intr_type = INTR_INT; 1365 intr->intr_signed = 1; 1366 intr->intr_iformat = 'c'; 1367 break; 1368 case DW_ATE_boolean: 1369 intr->intr_type = INTR_INT; 1370 intr->intr_signed = 1; 1371 intr->intr_iformat = 'b'; 1372 break; 1373 case DW_ATE_float: 1374 case DW_ATE_complex_float: 1375 case DW_ATE_imaginary_float: 1376 #if defined(sun) 1377 case DW_ATE_SUN_imaginary_float: 1378 case DW_ATE_SUN_interval_float: 1379 #endif 1380 intr->intr_type = INTR_REAL; 1381 intr->intr_signed = 1; 1382 intr->intr_fformat = die_base_type2enc(dw, off, enc, sz); 1383 break; 1384 default: 1385 terminate("die %llu: unknown base type encoding 0x%llx\n", 1386 off, enc); 1387 } 1388 1389 return (intr); 1390 } 1391 1392 static void 1393 die_base_create(dwarf_t *dw, Dwarf_Die base, Dwarf_Off off, tdesc_t *tdp) 1394 { 1395 Dwarf_Unsigned sz; 1396 intr_t *intr; 1397 char *new; 1398 1399 debug(3, "die %llu: creating base type\n", off); 1400 1401 /* 1402 * The compilers have their own clever (internally inconsistent) ideas 1403 * as to what base types should look like. Some times gcc will, for 1404 * example, use DW_ATE_signed_char for char. Other times, however, it 1405 * will use DW_ATE_signed. Needless to say, this causes some problems 1406 * down the road, particularly with merging. We do, however, use the 1407 * DWARF idea of type sizes, as this allows us to avoid caring about 1408 * the data model. 1409 */ 1410 (void) die_unsigned(dw, base, DW_AT_byte_size, &sz, DW_ATTR_REQ); 1411 1412 /* Check for bogus gcc DW_AT_byte_size attribute */ 1413 if (sz == 0xffffffff) { 1414 printf("dwarf.c:%s() working around bogus DW_AT_byte_size = 0xffffffff\n", __func__); 1415 sz = 0; 1416 } 1417 1418 if (tdp->t_name == NULL) 1419 terminate("die %llu: base type without name\n", off); 1420 1421 /* XXX make a name parser for float too */ 1422 if ((intr = die_base_name_parse(tdp->t_name, &new)) != NULL) { 1423 /* Found it. We'll use the parsed version */ 1424 debug(3, "die %llu: name \"%s\" remapped to \"%s\"\n", off, 1425 tdesc_name(tdp), new); 1426 1427 free(tdp->t_name); 1428 tdp->t_name = new; 1429 } else { 1430 /* 1431 * We didn't recognize the type, so we'll create an intr_t 1432 * based on the DWARF data. 1433 */ 1434 debug(3, "die %llu: using dwarf data for base \"%s\"\n", off, 1435 tdesc_name(tdp)); 1436 1437 intr = die_base_from_dwarf(dw, base, off, sz); 1438 } 1439 1440 intr->intr_nbits = sz * 8; 1441 1442 tdp->t_type = INTRINSIC; 1443 tdp->t_intr = intr; 1444 tdp->t_size = sz; 1445 1446 tdp->t_flags |= TDESC_F_RESOLVED; 1447 } 1448 1449 static void 1450 die_through_create(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp, 1451 int type, const char *typename) 1452 { 1453 Dwarf_Attribute attr; 1454 1455 debug(3, "die %llu <%llx>: creating %s type %d\n", off, off, typename, type); 1456 1457 tdp->t_type = type; 1458 1459 if ((attr = die_attr(dw, die, DW_AT_type, 0)) != NULL) { 1460 tdp->t_tdesc = die_lookup_pass1(dw, die, DW_AT_type); 1461 } else { 1462 tdp->t_tdesc = tdesc_intr_void(dw); 1463 } 1464 1465 if (type == POINTER) 1466 tdp->t_size = dw->dw_ptrsz; 1467 1468 tdp->t_flags |= TDESC_F_RESOLVED; 1469 1470 if (type == TYPEDEF) { 1471 iidesc_t *ii = xcalloc(sizeof (iidesc_t)); 1472 ii->ii_type = II_TYPE; 1473 ii->ii_name = xstrdup(tdp->t_name); 1474 ii->ii_dtype = tdp; 1475 1476 iidesc_add(dw->dw_td->td_iihash, ii); 1477 } 1478 } 1479 1480 static void 1481 die_typedef_create(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp) 1482 { 1483 die_through_create(dw, die, off, tdp, TYPEDEF, "typedef"); 1484 } 1485 1486 static void 1487 die_const_create(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp) 1488 { 1489 die_through_create(dw, die, off, tdp, CONST, "const"); 1490 } 1491 1492 static void 1493 die_pointer_create(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp) 1494 { 1495 die_through_create(dw, die, off, tdp, POINTER, "pointer"); 1496 } 1497 1498 static void 1499 die_restrict_create(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp) 1500 { 1501 die_through_create(dw, die, off, tdp, RESTRICT, "restrict"); 1502 } 1503 1504 static void 1505 die_volatile_create(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp) 1506 { 1507 die_through_create(dw, die, off, tdp, VOLATILE, "volatile"); 1508 } 1509 1510 /*ARGSUSED3*/ 1511 static void 1512 die_function_create(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp __unused) 1513 { 1514 Dwarf_Die arg; 1515 Dwarf_Half tag; 1516 iidesc_t *ii; 1517 char *name; 1518 1519 debug(3, "die %llu <%llx>: creating function definition\n", off, off); 1520 1521 /* 1522 * We'll begin by processing any type definition nodes that may be 1523 * lurking underneath this one. 1524 */ 1525 for (arg = die_child(dw, die); arg != NULL; 1526 arg = die_sibling(dw, arg)) { 1527 if ((tag = die_tag(dw, arg)) != DW_TAG_formal_parameter && 1528 tag != DW_TAG_variable) { 1529 /* Nested type declaration */ 1530 die_create_one(dw, arg); 1531 } 1532 } 1533 1534 if (die_isdecl(dw, die) || (name = die_name(dw, die)) == NULL) { 1535 /* 1536 * We process neither prototypes nor subprograms without 1537 * names. 1538 */ 1539 return; 1540 } 1541 1542 ii = xcalloc(sizeof (iidesc_t)); 1543 ii->ii_type = die_isglobal(dw, die) ? II_GFUN : II_SFUN; 1544 ii->ii_name = name; 1545 if (ii->ii_type == II_SFUN) 1546 ii->ii_owner = xstrdup(dw->dw_cuname); 1547 1548 debug(3, "die %llu: function %s is %s\n", off, ii->ii_name, 1549 (ii->ii_type == II_GFUN ? "global" : "static")); 1550 1551 if (die_attr(dw, die, DW_AT_type, 0) != NULL) 1552 ii->ii_dtype = die_lookup_pass1(dw, die, DW_AT_type); 1553 else 1554 ii->ii_dtype = tdesc_intr_void(dw); 1555 1556 for (arg = die_child(dw, die); arg != NULL; 1557 arg = die_sibling(dw, arg)) { 1558 char *name1; 1559 1560 debug(3, "die %llu: looking at sub member at %llu\n", 1561 off, die_off(dw, die)); 1562 1563 if (die_tag(dw, arg) != DW_TAG_formal_parameter) 1564 continue; 1565 1566 if ((name1 = die_name(dw, arg)) == NULL) { 1567 terminate("die %llu: func arg %d has no name\n", 1568 off, ii->ii_nargs + 1); 1569 } 1570 1571 if (strcmp(name1, "...") == 0) { 1572 free(name1); 1573 ii->ii_vargs = 1; 1574 continue; 1575 } 1576 1577 ii->ii_nargs++; 1578 } 1579 1580 if (ii->ii_nargs > 0) { 1581 int i; 1582 1583 debug(3, "die %llu: function has %d argument%s\n", off, 1584 ii->ii_nargs, (ii->ii_nargs == 1 ? "" : "s")); 1585 1586 ii->ii_args = xcalloc(sizeof (tdesc_t) * ii->ii_nargs); 1587 1588 for (arg = die_child(dw, die), i = 0; 1589 arg != NULL && i < ii->ii_nargs; 1590 arg = die_sibling(dw, arg)) { 1591 if (die_tag(dw, arg) != DW_TAG_formal_parameter) 1592 continue; 1593 1594 ii->ii_args[i++] = die_lookup_pass1(dw, arg, 1595 DW_AT_type); 1596 } 1597 } 1598 1599 iidesc_add(dw->dw_td->td_iihash, ii); 1600 } 1601 1602 /*ARGSUSED3*/ 1603 static void 1604 die_variable_create(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp __unused) 1605 { 1606 iidesc_t *ii; 1607 char *name; 1608 1609 debug(3, "die %llu: creating object definition\n", off); 1610 1611 if (die_isdecl(dw, die) || (name = die_name(dw, die)) == NULL) 1612 return; /* skip prototypes and nameless objects */ 1613 1614 ii = xcalloc(sizeof (iidesc_t)); 1615 ii->ii_type = die_isglobal(dw, die) ? II_GVAR : II_SVAR; 1616 ii->ii_name = name; 1617 ii->ii_dtype = die_lookup_pass1(dw, die, DW_AT_type); 1618 if (ii->ii_type == II_SVAR) 1619 ii->ii_owner = xstrdup(dw->dw_cuname); 1620 1621 iidesc_add(dw->dw_td->td_iihash, ii); 1622 } 1623 1624 /*ARGSUSED2*/ 1625 static int 1626 die_fwd_resolve(tdesc_t *fwd, tdesc_t **fwdp, void *private __unused) 1627 { 1628 if (fwd->t_flags & TDESC_F_RESOLVED) 1629 return (1); 1630 1631 if (fwd->t_tdesc != NULL) { 1632 debug(3, "tdp %u: unforwarded %s\n", fwd->t_id, 1633 tdesc_name(fwd)); 1634 *fwdp = fwd->t_tdesc; 1635 } 1636 1637 fwd->t_flags |= TDESC_F_RESOLVED; 1638 1639 return (1); 1640 } 1641 1642 /*ARGSUSED*/ 1643 static void 1644 die_lexblk_descend(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off __unused, tdesc_t *tdp __unused) 1645 { 1646 Dwarf_Die child = die_child(dw, die); 1647 1648 if (child != NULL) 1649 die_create(dw, child); 1650 } 1651 1652 /* 1653 * Used to map the die to a routine which can parse it, using the tag to do the 1654 * mapping. While the processing of most tags entails the creation of a tdesc, 1655 * there are a few which don't - primarily those which result in the creation of 1656 * iidescs which refer to existing tdescs. 1657 */ 1658 1659 #define DW_F_NOTDP 0x1 /* Don't create a tdesc for the creator */ 1660 1661 typedef struct die_creator { 1662 Dwarf_Half dc_tag; 1663 uint16_t dc_flags; 1664 void (*dc_create)(dwarf_t *, Dwarf_Die, Dwarf_Off, tdesc_t *); 1665 } die_creator_t; 1666 1667 static const die_creator_t die_creators[] = { 1668 { DW_TAG_array_type, 0, die_array_create }, 1669 { DW_TAG_enumeration_type, 0, die_enum_create }, 1670 { DW_TAG_lexical_block, DW_F_NOTDP, die_lexblk_descend }, 1671 { DW_TAG_pointer_type, 0, die_pointer_create }, 1672 { DW_TAG_structure_type, 0, die_struct_create }, 1673 { DW_TAG_subroutine_type, 0, die_funcptr_create }, 1674 { DW_TAG_typedef, 0, die_typedef_create }, 1675 { DW_TAG_union_type, 0, die_union_create }, 1676 { DW_TAG_base_type, 0, die_base_create }, 1677 { DW_TAG_const_type, 0, die_const_create }, 1678 { DW_TAG_subprogram, DW_F_NOTDP, die_function_create }, 1679 { DW_TAG_variable, DW_F_NOTDP, die_variable_create }, 1680 { DW_TAG_volatile_type, 0, die_volatile_create }, 1681 { DW_TAG_restrict_type, 0, die_restrict_create }, 1682 { 0, 0, NULL } 1683 }; 1684 1685 static const die_creator_t * 1686 die_tag2ctor(Dwarf_Half tag) 1687 { 1688 const die_creator_t *dc; 1689 1690 for (dc = die_creators; dc->dc_create != NULL; dc++) { 1691 if (dc->dc_tag == tag) 1692 return (dc); 1693 } 1694 1695 return (NULL); 1696 } 1697 1698 static void 1699 die_create_one(dwarf_t *dw, Dwarf_Die die) 1700 { 1701 Dwarf_Off off = die_off(dw, die); 1702 const die_creator_t *dc; 1703 Dwarf_Half tag; 1704 tdesc_t *tdp; 1705 1706 debug(3, "die %llu <%llx>: create_one\n", off, off); 1707 1708 if (off > dw->dw_maxoff) { 1709 terminate("illegal die offset %llu (max %llu)\n", off, 1710 dw->dw_maxoff); 1711 } 1712 1713 tag = die_tag(dw, die); 1714 1715 if ((dc = die_tag2ctor(tag)) == NULL) { 1716 debug(2, "die %llu: ignoring tag type %x\n", off, tag); 1717 return; 1718 } 1719 1720 if ((tdp = tdesc_lookup(dw, off)) == NULL && 1721 !(dc->dc_flags & DW_F_NOTDP)) { 1722 tdp = xcalloc(sizeof (tdesc_t)); 1723 tdp->t_id = off; 1724 tdesc_add(dw, tdp); 1725 } 1726 1727 if (tdp != NULL) 1728 tdp->t_name = die_name(dw, die); 1729 1730 dc->dc_create(dw, die, off, tdp); 1731 } 1732 1733 static void 1734 die_create(dwarf_t *dw, Dwarf_Die die) 1735 { 1736 do { 1737 die_create_one(dw, die); 1738 } while ((die = die_sibling(dw, die)) != NULL); 1739 } 1740 1741 static tdtrav_cb_f die_resolvers[] = { 1742 NULL, 1743 NULL, /* intrinsic */ 1744 NULL, /* pointer */ 1745 die_array_resolve, /* array */ 1746 NULL, /* function */ 1747 die_sou_resolve, /* struct */ 1748 die_sou_resolve, /* union */ 1749 die_enum_resolve, /* enum */ 1750 die_fwd_resolve, /* forward */ 1751 NULL, /* typedef */ 1752 NULL, /* typedef unres */ 1753 NULL, /* volatile */ 1754 NULL, /* const */ 1755 NULL, /* restrict */ 1756 }; 1757 1758 static tdtrav_cb_f die_fail_reporters[] = { 1759 NULL, 1760 NULL, /* intrinsic */ 1761 NULL, /* pointer */ 1762 die_array_failed, /* array */ 1763 NULL, /* function */ 1764 die_sou_failed, /* struct */ 1765 die_sou_failed, /* union */ 1766 NULL, /* enum */ 1767 NULL, /* forward */ 1768 NULL, /* typedef */ 1769 NULL, /* typedef unres */ 1770 NULL, /* volatile */ 1771 NULL, /* const */ 1772 NULL, /* restrict */ 1773 }; 1774 1775 static void 1776 die_resolve(dwarf_t *dw) 1777 { 1778 int last = -1; 1779 int pass = 0; 1780 1781 do { 1782 pass++; 1783 dw->dw_nunres = 0; 1784 1785 (void) iitraverse_hash(dw->dw_td->td_iihash, 1786 &dw->dw_td->td_curvgen, NULL, NULL, die_resolvers, dw); 1787 1788 debug(3, "resolve: pass %d, %u left\n", pass, dw->dw_nunres); 1789 1790 if ((int) dw->dw_nunres == last) { 1791 fprintf(stderr, "%s: failed to resolve the following " 1792 "types:\n", progname); 1793 1794 (void) iitraverse_hash(dw->dw_td->td_iihash, 1795 &dw->dw_td->td_curvgen, NULL, NULL, 1796 die_fail_reporters, dw); 1797 1798 terminate("failed to resolve types\n"); 1799 } 1800 1801 last = dw->dw_nunres; 1802 1803 } while (dw->dw_nunres != 0); 1804 } 1805 1806 /*ARGSUSED*/ 1807 int 1808 dw_read(tdata_t *td, Elf *elf, char *filename __unused) 1809 { 1810 Dwarf_Unsigned abboff, hdrlen, nxthdr; 1811 Dwarf_Half vers, addrsz; 1812 Dwarf_Die cu = 0; 1813 Dwarf_Die child = 0; 1814 dwarf_t dw; 1815 char *prod = NULL; 1816 int rc; 1817 1818 bzero(&dw, sizeof (dwarf_t)); 1819 dw.dw_td = td; 1820 dw.dw_ptrsz = elf_ptrsz(elf); 1821 dw.dw_mfgtid_last = TID_MFGTID_BASE; 1822 dw.dw_tidhash = hash_new(TDESC_HASH_BUCKETS, tdesc_idhash, tdesc_idcmp); 1823 dw.dw_fwdhash = hash_new(TDESC_HASH_BUCKETS, tdesc_namehash, 1824 tdesc_namecmp); 1825 dw.dw_enumhash = hash_new(TDESC_HASH_BUCKETS, tdesc_namehash, 1826 tdesc_namecmp); 1827 1828 if ((rc = dwarf_elf_init(elf, DW_DLC_READ, &dw.dw_dw, 1829 &dw.dw_err)) == DW_DLV_NO_ENTRY) { 1830 errno = ENOENT; 1831 return (-1); 1832 } else if (rc != DW_DLV_OK) { 1833 if (dwarf_errno(&dw.dw_err) == DW_DLE_DEBUG_INFO_NULL) { 1834 /* 1835 * There's no type data in the DWARF section, but 1836 * libdwarf is too clever to handle that properly. 1837 */ 1838 return (0); 1839 } 1840 1841 terminate("failed to initialize DWARF: %s\n", 1842 dwarf_errmsg(&dw.dw_err)); 1843 } 1844 1845 if ((rc = dwarf_next_cu_header(dw.dw_dw, &hdrlen, &vers, &abboff, 1846 &addrsz, &nxthdr, &dw.dw_err)) != DW_DLV_OK) { 1847 if (rc == DW_DLV_NO_ENTRY) { 1848 /* no compilation unit in the DWARF section */ 1849 return 0; 1850 } 1851 terminate("rc = %d %s\n", rc, dwarf_errmsg(&dw.dw_err)); 1852 } 1853 1854 if ((cu = die_sibling(&dw, NULL)) == NULL) 1855 terminate("file does not contain dwarf type data " 1856 "(try compiling with -g)\n"); 1857 1858 dw.dw_maxoff = nxthdr - 1; 1859 1860 if (dw.dw_maxoff > TID_FILEMAX) 1861 terminate("file contains too many types\n"); 1862 1863 debug(1, "DWARF version: %d\n", vers); 1864 if (vers != DWARF_VERSION) { 1865 terminate("file contains incompatible version %d DWARF code " 1866 "(version 2 required)\n", vers); 1867 } 1868 1869 if (die_string(&dw, cu, DW_AT_producer, &prod, 0)) { 1870 debug(1, "DWARF emitter: %s\n", prod); 1871 free(prod); 1872 } 1873 1874 if ((dw.dw_cuname = die_name(&dw, cu)) != NULL) { 1875 char *base = xstrdup(basename(dw.dw_cuname)); 1876 free(dw.dw_cuname); 1877 dw.dw_cuname = base; 1878 1879 debug(1, "CU name: %s\n", dw.dw_cuname); 1880 } 1881 1882 if ((child = die_child(&dw, cu)) != NULL) 1883 die_create(&dw, child); 1884 1885 if ((rc = dwarf_next_cu_header(dw.dw_dw, &hdrlen, &vers, &abboff, 1886 &addrsz, &nxthdr, &dw.dw_err)) != DW_DLV_NO_ENTRY) 1887 terminate("multiple compilation units not supported\n"); 1888 1889 (void) dwarf_finish(&dw.dw_dw, &dw.dw_err); 1890 1891 die_resolve(&dw); 1892 1893 cvt_fixups(td, dw.dw_ptrsz); 1894 1895 /* leak the dwarf_t */ 1896 1897 return (0); 1898 } 1899