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