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