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.
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.
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]
22 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #pragma ident "%Z%%M% %I% %E% SMI"
29 * DWARF to tdata conversion
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.
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
58 * There are, as always, a few special cases that are handled during the first
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.
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.
81 * XXX see if a post traverser will allow the elimination of repeated pass 2
95 #include "ctf_headers.h"
101 /* The version of DWARF which we support. */
102 #define DWARF_VERSION 2
105 * We need to define a couple of our own intrinsics, to smooth out some of the
106 * differences between the GCC and DevPro DWARF emitters. See the referenced
107 * routines and the special cases in the file comment for more details.
109 * Type IDs are 32 bits wide. We're going to use the top of that field to
110 * indicate types that we've created ourselves.
112 #define TID_FILEMAX 0x3fffffff /* highest tid from file */
113 #define TID_VOID 0x40000001 /* see die_void() */
114 #define TID_LONG 0x40000002 /* see die_array() */
116 #define TID_MFGTID_BASE 0x40000003 /* first mfg'd tid */
119 * To reduce the staggering amount of error-handling code that would otherwise
120 * be required, the attribute-retrieval routines handle most of their own
121 * errors. If the following flag is supplied as the value of the `req'
122 * argument, they will also handle the absence of a requested attribute by
123 * terminating the program.
125 #define DW_ATTR_REQ 1
127 #define TDESC_HASH_BUCKETS 511
129 typedef struct dwarf {
130 Dwarf_Debug dw_dw; /* for libdwarf */
131 Dwarf_Error dw_err; /* for libdwarf */
132 Dwarf_Off dw_maxoff; /* highest legal offset in this cu */
133 tdata_t *dw_td; /* root of the tdesc/iidesc tree */
134 hash_t *dw_tidhash; /* hash of tdescs by t_id */
135 hash_t *dw_fwdhash; /* hash of fwd decls by name */
136 hash_t *dw_enumhash; /* hash of memberless enums by name */
137 tdesc_t *dw_void; /* manufactured void type */
138 tdesc_t *dw_long; /* manufactured long type for arrays */
139 size_t dw_ptrsz; /* size of a pointer in this file */
140 tid_t dw_mfgtid_last; /* last mfg'd type ID used */
141 uint_t dw_nunres; /* count of unresolved types */
142 char *dw_cuname; /* name of compilation unit */
145 static void die_create_one(dwarf_t *, Dwarf_Die);
146 static void die_create(dwarf_t *, Dwarf_Die);
149 mfgtid_next(dwarf_t *dw)
151 return (++dw->dw_mfgtid_last);
155 tdesc_add(dwarf_t *dw, tdesc_t *tdp)
157 hash_add(dw->dw_tidhash, tdp);
161 tdesc_lookup(dwarf_t *dw, int tid)
168 if (hash_find(dw->dw_tidhash, &tmpl, &tdp))
175 * Resolve a tdesc down to a node which should have a size. Returns the size,
176 * zero if the size hasn't yet been determined.
179 tdesc_size(tdesc_t *tdp)
182 switch (tdp->t_type) {
190 return (tdp->t_size);
202 case 0: /* not yet defined */
206 terminate("tdp %u: tdesc_size on unknown type %d\n",
207 tdp->t_id, tdp->t_type);
213 tdesc_bitsize(tdesc_t *tdp)
216 switch (tdp->t_type) {
218 return (tdp->t_intr->intr_nbits);
226 return (tdp->t_size * NBBY);
238 case 0: /* not yet defined */
242 terminate("tdp %u: tdesc_bitsize on unknown type %d\n",
243 tdp->t_id, tdp->t_type);
249 tdesc_basetype(tdesc_t *tdp)
252 switch (tdp->t_type) {
259 case 0: /* not yet defined */
268 die_off(dwarf_t *dw, Dwarf_Die die)
272 if (dwarf_dieoffset(die, &off, &dw->dw_err) == DW_DLV_OK)
275 terminate("failed to get offset for die: %s\n",
276 dwarf_errmsg(&dw->dw_err));
282 die_sibling(dwarf_t *dw, Dwarf_Die die)
287 if ((rc = dwarf_siblingof(dw->dw_dw, die, &sib, &dw->dw_err)) ==
290 else if (rc == DW_DLV_NO_ENTRY)
293 terminate("die %llu: failed to find type sibling: %s\n",
294 die_off(dw, die), dwarf_errmsg(&dw->dw_err));
300 die_child(dwarf_t *dw, Dwarf_Die die)
305 if ((rc = dwarf_child(die, &child, &dw->dw_err)) == DW_DLV_OK)
307 else if (rc == DW_DLV_NO_ENTRY)
310 terminate("die %llu: failed to find type child: %s\n",
311 die_off(dw, die), dwarf_errmsg(&dw->dw_err));
317 die_tag(dwarf_t *dw, Dwarf_Die die)
321 if (dwarf_tag(die, &tag, &dw->dw_err) == DW_DLV_OK)
324 terminate("die %llu: failed to get tag for type: %s\n",
325 die_off(dw, die), dwarf_errmsg(&dw->dw_err));
330 static Dwarf_Attribute
331 die_attr(dwarf_t *dw, Dwarf_Die die, Dwarf_Half name, int req)
333 Dwarf_Attribute attr;
336 if ((rc = dwarf_attr(die, name, &attr, &dw->dw_err)) == DW_DLV_OK) {
338 } else if (rc == DW_DLV_NO_ENTRY) {
340 terminate("die %llu: no attr 0x%x\n", die_off(dw, die),
347 terminate("die %llu: failed to get attribute for type: %s\n",
348 die_off(dw, die), dwarf_errmsg(&dw->dw_err));
354 die_signed(dwarf_t *dw, Dwarf_Die die, Dwarf_Half name, Dwarf_Signed *valp,
358 if (dwarf_attrval_signed(die, name, valp, &dw->dw_err) != DWARF_E_NONE) {
360 terminate("die %llu: failed to get signed: %s\n",
361 die_off(dw, die), dwarf_errmsg(&dw->dw_err));
369 die_unsigned(dwarf_t *dw, Dwarf_Die die, Dwarf_Half name, Dwarf_Unsigned *valp,
373 if (dwarf_attrval_unsigned(die, name, valp, &dw->dw_err) != DWARF_E_NONE) {
375 terminate("die %llu: failed to get unsigned: %s\n",
376 die_off(dw, die), dwarf_errmsg(&dw->dw_err));
384 die_bool(dwarf_t *dw, Dwarf_Die die, Dwarf_Half name, Dwarf_Bool *valp, int req)
388 if (dwarf_attrval_flag(die, name, valp, &dw->dw_err) != DWARF_E_NONE) {
390 terminate("die %llu: failed to get flag: %s\n",
391 die_off(dw, die), dwarf_errmsg(&dw->dw_err));
399 die_string(dwarf_t *dw, Dwarf_Die die, Dwarf_Half name, char **strp, int req)
401 const char *str = NULL;
403 if (dwarf_attrval_string(die, name, &str, &dw->dw_err) != DWARF_E_NONE ||
406 terminate("die %llu: failed to get string: %s\n",
407 die_off(dw, die), dwarf_errmsg(&dw->dw_err));
412 *strp = xstrdup(str);
418 die_attr_ref(dwarf_t *dw, Dwarf_Die die, Dwarf_Half name)
422 if (dwarf_attrval_unsigned(die, name, &off, &dw->dw_err) != DWARF_E_NONE) {
423 terminate("die %llu: failed to get ref: %s\n",
424 die_off(dw, die), dwarf_errmsg(&dw->dw_err));
431 die_name(dwarf_t *dw, Dwarf_Die die)
435 (void) die_string(dw, die, DW_AT_name, &str, 0);
441 die_isdecl(dwarf_t *dw, Dwarf_Die die)
445 return (die_bool(dw, die, DW_AT_declaration, &val, 0) && val);
449 die_isglobal(dwarf_t *dw, Dwarf_Die die)
455 * Some compilers (gcc) use DW_AT_external to indicate function
456 * visibility. Others (Sun) use DW_AT_visibility.
458 if (die_signed(dw, die, DW_AT_visibility, &vis, 0))
459 return (vis == DW_VIS_exported);
461 return (die_bool(dw, die, DW_AT_external, &ext, 0) && ext);
465 die_add(dwarf_t *dw, Dwarf_Off off)
467 tdesc_t *tdp = xcalloc(sizeof (tdesc_t));
477 die_lookup_pass1(dwarf_t *dw, Dwarf_Die die, Dwarf_Half name)
479 Dwarf_Off ref = die_attr_ref(dw, die, name);
482 if ((tdp = tdesc_lookup(dw, ref)) != NULL)
485 return (die_add(dw, ref));
489 die_mem_offset(dwarf_t *dw, Dwarf_Die die, Dwarf_Half name,
490 Dwarf_Unsigned *valp, int req __unused)
492 Dwarf_Locdesc *loc = NULL;
493 Dwarf_Signed locnum = 0;
495 if (dwarf_locdesc(die, name, &loc, &locnum, &dw->dw_err) != DW_DLV_OK)
498 if (locnum != 1 || loc->ld_s->lr_atom != DW_OP_plus_uconst) {
499 terminate("die %llu: cannot parse member offset\n",
503 *valp = loc->ld_s->lr_number;
506 if (dwarf_locdesc_free(loc, &dw->dw_err) != DW_DLV_OK)
507 terminate("die %llu: cannot free location descriptor: %s\n",
508 die_off(dw, die), dwarf_errmsg(&dw->dw_err));
514 tdesc_intr_common(dwarf_t *dw, int tid, const char *name, size_t sz)
519 intr = xcalloc(sizeof (intr_t));
520 intr->intr_type = INTR_INT;
521 intr->intr_signed = 1;
522 intr->intr_nbits = sz * NBBY;
524 tdp = xcalloc(sizeof (tdesc_t));
525 tdp->t_name = xstrdup(name);
528 tdp->t_type = INTRINSIC;
530 tdp->t_flags = TDESC_F_RESOLVED;
538 * Manufacture a void type. Used for gcc-emitted stabs, where the lack of a
539 * type reference implies a reference to a void type. A void *, for example
540 * will be represented by a pointer die without a DW_AT_type. CTF requires
541 * that pointer nodes point to something, so we'll create a void for use as
542 * the target. Note that the DWARF data may already create a void type. Ours
543 * would then be a duplicate, but it'll be removed in the self-uniquification
544 * merge performed at the completion of DWARF->tdesc conversion.
547 tdesc_intr_void(dwarf_t *dw)
549 if (dw->dw_void == NULL)
550 dw->dw_void = tdesc_intr_common(dw, TID_VOID, "void", 0);
552 return (dw->dw_void);
556 tdesc_intr_long(dwarf_t *dw)
558 if (dw->dw_long == NULL) {
559 dw->dw_long = tdesc_intr_common(dw, TID_LONG, "long",
563 return (dw->dw_long);
567 * Used for creating bitfield types. We create a copy of an existing intrinsic,
568 * adjusting the size of the copy to match what the caller requested. The
569 * caller can then use the copy as the type for a bitfield structure member.
572 tdesc_intr_clone(dwarf_t *dw, tdesc_t *old, size_t bitsz)
574 tdesc_t *new = xcalloc(sizeof (tdesc_t));
576 if (!(old->t_flags & TDESC_F_RESOLVED)) {
577 terminate("tdp %u: attempt to make a bit field from an "
578 "unresolved type\n", old->t_id);
581 new->t_name = xstrdup(old->t_name);
582 new->t_size = old->t_size;
583 new->t_id = mfgtid_next(dw);
584 new->t_type = INTRINSIC;
585 new->t_flags = TDESC_F_RESOLVED;
587 new->t_intr = xcalloc(sizeof (intr_t));
588 bcopy(old->t_intr, new->t_intr, sizeof (intr_t));
589 new->t_intr->intr_nbits = bitsz;
597 tdesc_array_create(dwarf_t *dw, Dwarf_Die dim, tdesc_t *arrtdp,
602 tdesc_t *ctdp = NULL;
606 if ((dim2 = die_sibling(dw, dim)) == NULL) {
608 } else if (die_tag(dw, dim2) == DW_TAG_subrange_type) {
609 ctdp = xcalloc(sizeof (tdesc_t));
610 ctdp->t_id = mfgtid_next(dw);
611 debug(3, "die %llu: creating new type %u for sub-dimension\n",
612 die_off(dw, dim2), ctdp->t_id);
613 tdesc_array_create(dw, dim2, arrtdp, ctdp);
615 terminate("die %llu: unexpected non-subrange node in array\n",
619 dimtdp->t_type = ARRAY;
620 dimtdp->t_ardef = ar = xcalloc(sizeof (ardef_t));
623 * Array bounds can be signed or unsigned, but there are several kinds
624 * of signless forms (data1, data2, etc) that take their sign from the
625 * routine that is trying to interpret them. That is, data1 can be
626 * either signed or unsigned, depending on whether you use the signed or
627 * unsigned accessor function. GCC will use the signless forms to store
628 * unsigned values which have their high bit set, so we need to try to
629 * read them first as unsigned to get positive values. We could also
630 * try signed first, falling back to unsigned if we got a negative
633 if (die_unsigned(dw, dim, DW_AT_upper_bound, &uval, 0))
634 ar->ad_nelems = uval + 1;
635 else if (die_signed(dw, dim, DW_AT_upper_bound, &sval, 0))
636 ar->ad_nelems = sval + 1;
641 * Different compilers use different index types. Force the type to be
642 * a common, known value (long).
644 ar->ad_idxtype = tdesc_intr_long(dw);
645 ar->ad_contents = ctdp;
647 if (ar->ad_contents->t_size != 0) {
648 dimtdp->t_size = ar->ad_contents->t_size * ar->ad_nelems;
649 dimtdp->t_flags |= TDESC_F_RESOLVED;
654 * Create a tdesc from an array node. Some arrays will come with byte size
655 * attributes, and thus can be resolved immediately. Others don't, and will
656 * need to wait until the second pass for resolution.
659 die_array_create(dwarf_t *dw, Dwarf_Die arr, Dwarf_Off off, tdesc_t *tdp)
661 tdesc_t *arrtdp = die_lookup_pass1(dw, arr, DW_AT_type);
665 debug(3, "die %llu <%llx>: creating array\n", off, off);
667 if ((dim = die_child(dw, arr)) == NULL ||
668 die_tag(dw, dim) != DW_TAG_subrange_type)
669 terminate("die %llu: failed to retrieve array bounds\n", off);
671 tdesc_array_create(dw, dim, arrtdp, tdp);
673 if (die_unsigned(dw, arr, DW_AT_byte_size, &uval, 0)) {
677 /* Check for bogus gcc DW_AT_byte_size attribute */
678 if (uval == (unsigned)-1) {
679 printf("dwarf.c:%s() working around bogus -1 DW_AT_byte_size\n",
687 * Ensure that sub-dimensions have sizes too before marking
690 flags = TDESC_F_RESOLVED;
691 for (dimtdp = tdp->t_ardef->ad_contents;
692 dimtdp->t_type == ARRAY;
693 dimtdp = dimtdp->t_ardef->ad_contents) {
694 if (!(dimtdp->t_flags & TDESC_F_RESOLVED)) {
700 tdp->t_flags |= flags;
703 debug(3, "die %llu <%llx>: array nelems %u size %u\n", off, off,
704 tdp->t_ardef->ad_nelems, tdp->t_size);
709 die_array_resolve(tdesc_t *tdp, tdesc_t **tdpp __unused, void *private)
711 dwarf_t *dw = private;
714 if (tdp->t_flags & TDESC_F_RESOLVED)
717 debug(3, "trying to resolve array %d (cont %d)\n", tdp->t_id,
718 tdp->t_ardef->ad_contents->t_id);
720 if ((sz = tdesc_size(tdp->t_ardef->ad_contents)) == 0) {
721 debug(3, "unable to resolve array %s (%d) contents %d\n",
722 tdesc_name(tdp), tdp->t_id,
723 tdp->t_ardef->ad_contents->t_id);
729 tdp->t_size = sz * tdp->t_ardef->ad_nelems;
730 tdp->t_flags |= TDESC_F_RESOLVED;
732 debug(3, "resolved array %d: %u bytes\n", tdp->t_id, tdp->t_size);
739 die_array_failed(tdesc_t *tdp, tdesc_t **tdpp __unused, void *private __unused)
741 tdesc_t *cont = tdp->t_ardef->ad_contents;
743 if (tdp->t_flags & TDESC_F_RESOLVED)
746 fprintf(stderr, "Array %d: failed to size contents type %s (%d)\n",
747 tdp->t_id, tdesc_name(cont), cont->t_id);
753 * Most enums (those with members) will be resolved during this first pass.
754 * Others - those without members (see the file comment) - won't be, and will
755 * need to wait until the second pass when they can be matched with their full
759 die_enum_create(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp)
765 debug(3, "die %llu: creating enum\n", off);
769 (void) die_unsigned(dw, die, DW_AT_byte_size, &uval, DW_ATTR_REQ);
770 /* Check for bogus gcc DW_AT_byte_size attribute */
771 if (uval == (unsigned)-1) {
772 printf("dwarf.c:%s() working around bogus -1 DW_AT_byte_size\n",
778 if ((mem = die_child(dw, die)) != NULL) {
779 elist_t **elastp = &tdp->t_emem;
784 if (die_tag(dw, mem) != DW_TAG_enumerator) {
785 /* Nested type declaration */
786 die_create_one(dw, mem);
790 el = xcalloc(sizeof (elist_t));
791 el->el_name = die_name(dw, mem);
793 if (die_signed(dw, mem, DW_AT_const_value, &sval, 0)) {
794 el->el_number = sval;
795 } else if (die_unsigned(dw, mem, DW_AT_const_value,
797 el->el_number = uval;
799 terminate("die %llu: enum %llu: member without "
800 "value\n", off, die_off(dw, mem));
803 debug(3, "die %llu: enum %llu: created %s = %d\n", off,
804 die_off(dw, mem), el->el_name, el->el_number);
807 elastp = &el->el_next;
809 } while ((mem = die_sibling(dw, mem)) != NULL);
811 hash_add(dw->dw_enumhash, tdp);
813 tdp->t_flags |= TDESC_F_RESOLVED;
815 if (tdp->t_name != NULL) {
816 iidesc_t *ii = xcalloc(sizeof (iidesc_t));
817 ii->ii_type = II_SOU;
818 ii->ii_name = xstrdup(tdp->t_name);
821 iidesc_add(dw->dw_td->td_iihash, ii);
827 die_enum_match(void *arg1, void *arg2)
829 tdesc_t *tdp = arg1, **fullp = arg2;
831 if (tdp->t_emem != NULL) {
833 return (-1); /* stop the iteration */
841 die_enum_resolve(tdesc_t *tdp, tdesc_t **tdpp __unused, void *private)
843 dwarf_t *dw = private;
844 tdesc_t *full = NULL;
846 if (tdp->t_flags & TDESC_F_RESOLVED)
849 (void) hash_find_iter(dw->dw_enumhash, tdp, die_enum_match, &full);
852 * The answer to this one won't change from iteration to iteration,
856 terminate("tdp %u: enum %s has no members\n", tdp->t_id,
860 debug(3, "tdp %u: enum %s redirected to %u\n", tdp->t_id,
861 tdesc_name(tdp), full->t_id);
863 tdp->t_flags |= TDESC_F_RESOLVED;
869 die_fwd_map(void *arg1, void *arg2)
871 tdesc_t *fwd = arg1, *sou = arg2;
873 debug(3, "tdp %u: mapped forward %s to sou %u\n", fwd->t_id,
874 tdesc_name(fwd), sou->t_id);
881 * Structures and unions will never be resolved during the first pass, as we
882 * won't be able to fully determine the member sizes. The second pass, which
883 * have access to sizing information, will be able to complete the resolution.
886 die_sou_create(dwarf_t *dw, Dwarf_Die str, Dwarf_Off off, tdesc_t *tdp,
887 int type, const char *typename)
889 Dwarf_Unsigned sz, bitsz, bitoff, maxsz=0;
891 mlist_t *ml, **mlastp;
894 tdp->t_type = (die_isdecl(dw, str) ? FORWARD : type);
896 debug(3, "die %llu: creating %s %s\n", off,
897 (tdp->t_type == FORWARD ? "forward decl" : typename),
900 if (tdp->t_type == FORWARD) {
901 hash_add(dw->dw_fwdhash, tdp);
905 (void) hash_find_iter(dw->dw_fwdhash, tdp, die_fwd_map, tdp);
907 (void) die_unsigned(dw, str, DW_AT_byte_size, &sz, DW_ATTR_REQ);
911 * GCC allows empty SOUs as an extension.
913 if ((mem = die_child(dw, str)) == NULL) {
917 mlastp = &tdp->t_members;
920 Dwarf_Off memoff = die_off(dw, mem);
921 Dwarf_Half tag = die_tag(dw, mem);
922 Dwarf_Unsigned mloff;
924 if (tag != DW_TAG_member) {
925 /* Nested type declaration */
926 die_create_one(dw, mem);
930 debug(3, "die %llu: mem %llu: creating member\n", off, memoff);
932 ml = xcalloc(sizeof (mlist_t));
935 * This could be a GCC anon struct/union member, so we'll allow
936 * an empty name, even though nothing can really handle them
937 * properly. Note that some versions of GCC miss out debug
938 * info for anon structs, though recent versions are fixed (gcc
941 if ((ml->ml_name = die_name(dw, mem)) == NULL)
944 ml->ml_type = die_lookup_pass1(dw, mem, DW_AT_type);
945 debug(3, "die_sou_create(): ml_type = %p t_id = %d\n",
946 ml->ml_type, ml->ml_type->t_id);
948 if (die_mem_offset(dw, mem, DW_AT_data_member_location,
950 debug(3, "die %llu: got mloff %llx\n", off,
951 (u_longlong_t)mloff);
952 ml->ml_offset = mloff * 8;
955 if (die_unsigned(dw, mem, DW_AT_bit_size, &bitsz, 0))
958 ml->ml_size = tdesc_bitsize(ml->ml_type);
960 if (die_unsigned(dw, mem, DW_AT_bit_offset, &bitoff, 0)) {
961 #if BYTE_ORDER == _BIG_ENDIAN
962 ml->ml_offset += bitoff;
964 ml->ml_offset += tdesc_bitsize(ml->ml_type) - bitoff -
969 debug(3, "die %llu: mem %llu: created \"%s\" (off %u sz %u)\n",
970 off, memoff, ml->ml_name, ml->ml_offset, ml->ml_size);
973 mlastp = &ml->ml_next;
975 /* Find the size of the largest member to work around a gcc
976 * bug. See GCC Bugzilla 35998.
978 if (maxsz < ml->ml_size)
981 } while ((mem = die_sibling(dw, mem)) != NULL);
983 /* See if we got a bogus DW_AT_byte_size. GCC will sometimes
986 if (sz == (unsigned)-1) {
987 printf("dwarf.c:%s() working around bogus -1 DW_AT_byte_size\n",
989 tdp->t_size = maxsz / 8; /* maxsz is in bits, t_size is bytes */
993 * GCC will attempt to eliminate unused types, thus decreasing the
994 * size of the emitted dwarf. That is, if you declare a foo_t in your
995 * header, include said header in your source file, and neglect to
996 * actually use (directly or indirectly) the foo_t in the source file,
997 * the foo_t won't make it into the emitted DWARF. So, at least, goes
1000 * Occasionally, it'll emit the DW_TAG_structure_type for the foo_t,
1001 * and then neglect to emit the members. Strangely, the loner struct
1002 * tag will always be followed by a proper nested declaration of
1003 * something else. This is clearly a bug, but we're not going to have
1004 * time to get it fixed before this goo goes back, so we'll have to work
1005 * around it. If we see a no-membered struct with a nested declaration
1006 * (i.e. die_child of the struct tag won't be null), we'll ignore it.
1007 * Being paranoid, we won't simply remove it from the hash. Instead,
1008 * we'll decline to create an iidesc for it, thus ensuring that this
1009 * type won't make it into the output file. To be safe, we'll also
1012 if (tdp->t_members == NULL) {
1013 const char *old = tdesc_name(tdp);
1014 size_t newsz = 7 + strlen(old) + 1;
1015 char *new = xmalloc(newsz);
1016 (void) snprintf(new, newsz, "orphan %s", old);
1018 debug(3, "die %llu: worked around %s %s\n", off, typename, old);
1020 if (tdp->t_name != NULL)
1027 if (tdp->t_name != NULL) {
1028 ii = xcalloc(sizeof (iidesc_t));
1029 ii->ii_type = II_SOU;
1030 ii->ii_name = xstrdup(tdp->t_name);
1033 iidesc_add(dw->dw_td->td_iihash, ii);
1038 die_struct_create(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp)
1040 die_sou_create(dw, die, off, tdp, STRUCT, "struct");
1044 die_union_create(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp)
1046 die_sou_create(dw, die, off, tdp, UNION, "union");
1051 die_sou_resolve(tdesc_t *tdp, tdesc_t **tdpp __unused, void *private)
1053 dwarf_t *dw = private;
1057 if (tdp->t_flags & TDESC_F_RESOLVED)
1060 debug(3, "resolving sou %s\n", tdesc_name(tdp));
1062 for (ml = tdp->t_members; ml != NULL; ml = ml->ml_next) {
1063 if (ml->ml_size == 0) {
1064 mt = tdesc_basetype(ml->ml_type);
1066 if ((ml->ml_size = tdesc_bitsize(mt)) != 0)
1070 * For empty members, or GCC/C99 flexible array
1071 * members, a size of 0 is correct.
1073 if (mt->t_members == NULL)
1075 if (mt->t_type == ARRAY && mt->t_ardef->ad_nelems == 0)
1082 if ((mt = tdesc_basetype(ml->ml_type)) == NULL) {
1087 if (ml->ml_size != 0 && mt->t_type == INTRINSIC &&
1088 mt->t_intr->intr_nbits != (int)ml->ml_size) {
1090 * This member is a bitfield, and needs to reference
1091 * an intrinsic type with the same width. If the
1092 * currently-referenced type isn't of the same width,
1093 * we'll copy it, adjusting the width of the copy to
1094 * the size we'd like.
1096 debug(3, "tdp %u: creating bitfield for %d bits\n",
1097 tdp->t_id, ml->ml_size);
1099 ml->ml_type = tdesc_intr_clone(dw, mt, ml->ml_size);
1103 tdp->t_flags |= TDESC_F_RESOLVED;
1110 die_sou_failed(tdesc_t *tdp, tdesc_t **tdpp __unused, void *private __unused)
1112 const char *typename = (tdp->t_type == STRUCT ? "struct" : "union");
1115 if (tdp->t_flags & TDESC_F_RESOLVED)
1118 for (ml = tdp->t_members; ml != NULL; ml = ml->ml_next) {
1119 if (ml->ml_size == 0) {
1120 fprintf(stderr, "%s %d <%x>: failed to size member \"%s\" "
1121 "of type %s (%d <%x>)\n", typename, tdp->t_id,
1123 ml->ml_name, tdesc_name(ml->ml_type),
1124 ml->ml_type->t_id, ml->ml_type->t_id);
1132 die_funcptr_create(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp)
1134 Dwarf_Attribute attr;
1140 debug(3, "die %llu <%llx>: creating function pointer\n", off, off);
1143 * We'll begin by processing any type definition nodes that may be
1144 * lurking underneath this one.
1146 for (arg = die_child(dw, die); arg != NULL;
1147 arg = die_sibling(dw, arg)) {
1148 if ((tag = die_tag(dw, arg)) != DW_TAG_formal_parameter &&
1149 tag != DW_TAG_unspecified_parameters) {
1150 /* Nested type declaration */
1151 die_create_one(dw, arg);
1155 if (die_isdecl(dw, die)) {
1157 * This is a prototype. We don't add prototypes to the
1158 * tree, so we're going to drop the tdesc. Unfortunately,
1159 * it has already been added to the tree. Nobody will reference
1160 * it, though, and it will be leaked.
1165 fn = xcalloc(sizeof (fndef_t));
1167 tdp->t_type = FUNCTION;
1169 if ((attr = die_attr(dw, die, DW_AT_type, 0)) != NULL) {
1170 fn->fn_ret = die_lookup_pass1(dw, die, DW_AT_type);
1172 fn->fn_ret = tdesc_intr_void(dw);
1176 * Count the arguments to the function, then read them in.
1178 for (fn->fn_nargs = 0, arg = die_child(dw, die); arg != NULL;
1179 arg = die_sibling(dw, arg)) {
1180 if ((tag = die_tag(dw, arg)) == DW_TAG_formal_parameter)
1182 else if (tag == DW_TAG_unspecified_parameters &&
1187 if (fn->fn_nargs != 0) {
1188 debug(3, "die %llu: adding %d argument%s\n", off, fn->fn_nargs,
1189 (fn->fn_nargs > 1 ? "s" : ""));
1191 fn->fn_args = xcalloc(sizeof (tdesc_t *) * fn->fn_nargs);
1192 for (i = 0, arg = die_child(dw, die);
1193 arg != NULL && i < (int) fn->fn_nargs;
1194 arg = die_sibling(dw, arg)) {
1195 if (die_tag(dw, arg) != DW_TAG_formal_parameter)
1198 fn->fn_args[i++] = die_lookup_pass1(dw, arg,
1204 tdp->t_flags |= TDESC_F_RESOLVED;
1208 * GCC and DevPro use different names for the base types. While the terms are
1209 * the same, they are arranged in a different order. Some terms, such as int,
1210 * are implied in one, and explicitly named in the other. Given a base type
1211 * as input, this routine will return a common name, along with an intr_t
1212 * that reflects said name.
1215 die_base_name_parse(const char *name, char **newp)
1220 int nlong = 0, nshort = 0, nchar = 0, nint = 0;
1225 if (strlen(name) > sizeof (buf) - 1)
1226 terminate("base type name \"%s\" is too long\n", name);
1228 strncpy(buf, name, sizeof (buf));
1230 for (c = strtok(buf, " "); c != NULL; c = strtok(NULL, " ")) {
1231 if (strcmp(c, "signed") == 0)
1233 else if (strcmp(c, "unsigned") == 0)
1235 else if (strcmp(c, "long") == 0)
1237 else if (strcmp(c, "char") == 0) {
1240 } else if (strcmp(c, "short") == 0)
1242 else if (strcmp(c, "int") == 0)
1246 * If we don't recognize any of the tokens, we'll tell
1247 * the caller to fall back to the dwarf-provided
1248 * encoding information.
1254 if (nchar > 1 || nshort > 1 || nint > 1 || nlong > 2)
1258 if (nlong > 0 || nshort > 0 || nint > 0)
1263 } else if (nshort > 0) {
1269 } else if (nlong > 0) {
1276 intr = xcalloc(sizeof (intr_t));
1277 intr->intr_type = INTR_INT;
1278 intr->intr_signed = sign;
1279 intr->intr_iformat = fmt;
1281 snprintf(buf, sizeof (buf), "%s%s%s",
1282 (sign ? "" : "unsigned "),
1283 (nlong > 1 ? "long " : ""),
1286 *newp = xstrdup(buf);
1290 typedef struct fp_size_map {
1291 size_t fsm_typesz[2]; /* size of {32,64} type */
1292 uint_t fsm_enc[3]; /* CTF_FP_* for {bare,cplx,imagry} type */
1295 static const fp_size_map_t fp_encodings[] = {
1296 { { 4, 4 }, { CTF_FP_SINGLE, CTF_FP_CPLX, CTF_FP_IMAGRY } },
1297 { { 8, 8 }, { CTF_FP_DOUBLE, CTF_FP_DCPLX, CTF_FP_DIMAGRY } },
1299 { { 16, 16 }, { CTF_FP_LDOUBLE, CTF_FP_LDCPLX, CTF_FP_LDIMAGRY } },
1301 { { 12, 16 }, { CTF_FP_LDOUBLE, CTF_FP_LDCPLX, CTF_FP_LDIMAGRY } },
1303 { { 0, 0 }, { 0, 0, 0 } }
1307 die_base_type2enc(dwarf_t *dw, Dwarf_Off off, Dwarf_Signed enc, size_t sz)
1309 const fp_size_map_t *map = fp_encodings;
1310 uint_t szidx = dw->dw_ptrsz == sizeof (uint64_t);
1311 uint_t mult = 1, col = 0;
1313 if (enc == DW_ATE_complex_float) {
1316 } else if (enc == DW_ATE_imaginary_float
1318 || enc == DW_ATE_SUN_imaginary_float
1323 while (map->fsm_typesz[szidx] != 0) {
1324 if (map->fsm_typesz[szidx] * mult == sz)
1325 return (map->fsm_enc[col]);
1329 terminate("die %llu: unrecognized real type size %u\n", off, sz);
1335 die_base_from_dwarf(dwarf_t *dw, Dwarf_Die base, Dwarf_Off off, size_t sz)
1337 intr_t *intr = xcalloc(sizeof (intr_t));
1340 (void) die_signed(dw, base, DW_AT_encoding, &enc, DW_ATTR_REQ);
1343 case DW_ATE_unsigned:
1344 case DW_ATE_address:
1345 intr->intr_type = INTR_INT;
1347 case DW_ATE_unsigned_char:
1348 intr->intr_type = INTR_INT;
1349 intr->intr_iformat = 'c';
1352 intr->intr_type = INTR_INT;
1353 intr->intr_signed = 1;
1355 case DW_ATE_signed_char:
1356 intr->intr_type = INTR_INT;
1357 intr->intr_signed = 1;
1358 intr->intr_iformat = 'c';
1360 case DW_ATE_boolean:
1361 intr->intr_type = INTR_INT;
1362 intr->intr_signed = 1;
1363 intr->intr_iformat = 'b';
1366 case DW_ATE_complex_float:
1367 case DW_ATE_imaginary_float:
1369 case DW_ATE_SUN_imaginary_float:
1370 case DW_ATE_SUN_interval_float:
1372 intr->intr_type = INTR_REAL;
1373 intr->intr_signed = 1;
1374 intr->intr_fformat = die_base_type2enc(dw, off, enc, sz);
1377 terminate("die %llu: unknown base type encoding 0x%llx\n",
1385 die_base_create(dwarf_t *dw, Dwarf_Die base, Dwarf_Off off, tdesc_t *tdp)
1391 debug(3, "die %llu: creating base type\n", off);
1394 * The compilers have their own clever (internally inconsistent) ideas
1395 * as to what base types should look like. Some times gcc will, for
1396 * example, use DW_ATE_signed_char for char. Other times, however, it
1397 * will use DW_ATE_signed. Needless to say, this causes some problems
1398 * down the road, particularly with merging. We do, however, use the
1399 * DWARF idea of type sizes, as this allows us to avoid caring about
1402 (void) die_unsigned(dw, base, DW_AT_byte_size, &sz, DW_ATTR_REQ);
1404 /* Check for bogus gcc DW_AT_byte_size attribute */
1405 if (sz == (unsigned)-1) {
1406 printf("dwarf.c:%s() working around bogus -1 DW_AT_byte_size\n",
1411 if (tdp->t_name == NULL)
1412 terminate("die %llu: base type without name\n", off);
1414 /* XXX make a name parser for float too */
1415 if ((intr = die_base_name_parse(tdp->t_name, &new)) != NULL) {
1416 /* Found it. We'll use the parsed version */
1417 debug(3, "die %llu: name \"%s\" remapped to \"%s\"\n", off,
1418 tdesc_name(tdp), new);
1424 * We didn't recognize the type, so we'll create an intr_t
1425 * based on the DWARF data.
1427 debug(3, "die %llu: using dwarf data for base \"%s\"\n", off,
1430 intr = die_base_from_dwarf(dw, base, off, sz);
1433 intr->intr_nbits = sz * 8;
1435 tdp->t_type = INTRINSIC;
1439 tdp->t_flags |= TDESC_F_RESOLVED;
1443 die_through_create(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp,
1444 int type, const char *typename)
1446 Dwarf_Attribute attr;
1448 debug(3, "die %llu <%llx>: creating %s type %d\n", off, off, typename, type);
1452 if ((attr = die_attr(dw, die, DW_AT_type, 0)) != NULL) {
1453 tdp->t_tdesc = die_lookup_pass1(dw, die, DW_AT_type);
1455 tdp->t_tdesc = tdesc_intr_void(dw);
1458 if (type == POINTER)
1459 tdp->t_size = dw->dw_ptrsz;
1461 tdp->t_flags |= TDESC_F_RESOLVED;
1463 if (type == TYPEDEF) {
1464 iidesc_t *ii = xcalloc(sizeof (iidesc_t));
1465 ii->ii_type = II_TYPE;
1466 ii->ii_name = xstrdup(tdp->t_name);
1469 iidesc_add(dw->dw_td->td_iihash, ii);
1474 die_typedef_create(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp)
1476 die_through_create(dw, die, off, tdp, TYPEDEF, "typedef");
1480 die_const_create(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp)
1482 die_through_create(dw, die, off, tdp, CONST, "const");
1486 die_pointer_create(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp)
1488 die_through_create(dw, die, off, tdp, POINTER, "pointer");
1492 die_restrict_create(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp)
1494 die_through_create(dw, die, off, tdp, RESTRICT, "restrict");
1498 die_volatile_create(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp)
1500 die_through_create(dw, die, off, tdp, VOLATILE, "volatile");
1505 die_function_create(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp __unused)
1512 debug(3, "die %llu <%llx>: creating function definition\n", off, off);
1515 * We'll begin by processing any type definition nodes that may be
1516 * lurking underneath this one.
1518 for (arg = die_child(dw, die); arg != NULL;
1519 arg = die_sibling(dw, arg)) {
1520 if ((tag = die_tag(dw, arg)) != DW_TAG_formal_parameter &&
1521 tag != DW_TAG_variable) {
1522 /* Nested type declaration */
1523 die_create_one(dw, arg);
1527 if (die_isdecl(dw, die) || (name = die_name(dw, die)) == NULL) {
1529 * We process neither prototypes nor subprograms without
1535 ii = xcalloc(sizeof (iidesc_t));
1536 ii->ii_type = die_isglobal(dw, die) ? II_GFUN : II_SFUN;
1538 if (ii->ii_type == II_SFUN)
1539 ii->ii_owner = xstrdup(dw->dw_cuname);
1541 debug(3, "die %llu: function %s is %s\n", off, ii->ii_name,
1542 (ii->ii_type == II_GFUN ? "global" : "static"));
1544 if (die_attr(dw, die, DW_AT_type, 0) != NULL)
1545 ii->ii_dtype = die_lookup_pass1(dw, die, DW_AT_type);
1547 ii->ii_dtype = tdesc_intr_void(dw);
1549 for (arg = die_child(dw, die); arg != NULL;
1550 arg = die_sibling(dw, arg)) {
1553 debug(3, "die %llu: looking at sub member at %llu\n",
1554 off, die_off(dw, die));
1556 if (die_tag(dw, arg) != DW_TAG_formal_parameter)
1559 if ((name1 = die_name(dw, arg)) == NULL) {
1560 terminate("die %llu: func arg %d has no name\n",
1561 off, ii->ii_nargs + 1);
1564 if (strcmp(name1, "...") == 0) {
1573 if (ii->ii_nargs > 0) {
1576 debug(3, "die %llu: function has %d argument%s\n", off,
1577 ii->ii_nargs, (ii->ii_nargs == 1 ? "" : "s"));
1579 ii->ii_args = xcalloc(sizeof (tdesc_t) * ii->ii_nargs);
1581 for (arg = die_child(dw, die), i = 0;
1582 arg != NULL && i < ii->ii_nargs;
1583 arg = die_sibling(dw, arg)) {
1584 if (die_tag(dw, arg) != DW_TAG_formal_parameter)
1587 ii->ii_args[i++] = die_lookup_pass1(dw, arg,
1592 iidesc_add(dw->dw_td->td_iihash, ii);
1597 die_variable_create(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp __unused)
1602 debug(3, "die %llu: creating object definition\n", off);
1604 if (die_isdecl(dw, die) || (name = die_name(dw, die)) == NULL)
1605 return; /* skip prototypes and nameless objects */
1607 ii = xcalloc(sizeof (iidesc_t));
1608 ii->ii_type = die_isglobal(dw, die) ? II_GVAR : II_SVAR;
1610 ii->ii_dtype = die_lookup_pass1(dw, die, DW_AT_type);
1611 if (ii->ii_type == II_SVAR)
1612 ii->ii_owner = xstrdup(dw->dw_cuname);
1614 iidesc_add(dw->dw_td->td_iihash, ii);
1619 die_fwd_resolve(tdesc_t *fwd, tdesc_t **fwdp, void *private __unused)
1621 if (fwd->t_flags & TDESC_F_RESOLVED)
1624 if (fwd->t_tdesc != NULL) {
1625 debug(3, "tdp %u: unforwarded %s\n", fwd->t_id,
1627 *fwdp = fwd->t_tdesc;
1630 fwd->t_flags |= TDESC_F_RESOLVED;
1637 die_lexblk_descend(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off __unused, tdesc_t *tdp __unused)
1639 Dwarf_Die child = die_child(dw, die);
1642 die_create(dw, child);
1646 * Used to map the die to a routine which can parse it, using the tag to do the
1647 * mapping. While the processing of most tags entails the creation of a tdesc,
1648 * there are a few which don't - primarily those which result in the creation of
1649 * iidescs which refer to existing tdescs.
1652 #define DW_F_NOTDP 0x1 /* Don't create a tdesc for the creator */
1654 typedef struct die_creator {
1657 void (*dc_create)(dwarf_t *, Dwarf_Die, Dwarf_Off, tdesc_t *);
1660 static const die_creator_t die_creators[] = {
1661 { DW_TAG_array_type, 0, die_array_create },
1662 { DW_TAG_enumeration_type, 0, die_enum_create },
1663 { DW_TAG_lexical_block, DW_F_NOTDP, die_lexblk_descend },
1664 { DW_TAG_pointer_type, 0, die_pointer_create },
1665 { DW_TAG_structure_type, 0, die_struct_create },
1666 { DW_TAG_subroutine_type, 0, die_funcptr_create },
1667 { DW_TAG_typedef, 0, die_typedef_create },
1668 { DW_TAG_union_type, 0, die_union_create },
1669 { DW_TAG_base_type, 0, die_base_create },
1670 { DW_TAG_const_type, 0, die_const_create },
1671 { DW_TAG_subprogram, DW_F_NOTDP, die_function_create },
1672 { DW_TAG_variable, DW_F_NOTDP, die_variable_create },
1673 { DW_TAG_volatile_type, 0, die_volatile_create },
1674 { DW_TAG_restrict_type, 0, die_restrict_create },
1678 static const die_creator_t *
1679 die_tag2ctor(Dwarf_Half tag)
1681 const die_creator_t *dc;
1683 for (dc = die_creators; dc->dc_create != NULL; dc++) {
1684 if (dc->dc_tag == tag)
1692 die_create_one(dwarf_t *dw, Dwarf_Die die)
1694 Dwarf_Off off = die_off(dw, die);
1695 const die_creator_t *dc;
1699 debug(3, "die %llu <%llx>: create_one\n", off, off);
1701 if (off > dw->dw_maxoff) {
1702 terminate("illegal die offset %llu (max %llu)\n", off,
1706 tag = die_tag(dw, die);
1708 if ((dc = die_tag2ctor(tag)) == NULL) {
1709 debug(2, "die %llu: ignoring tag type %x\n", off, tag);
1713 if ((tdp = tdesc_lookup(dw, off)) == NULL &&
1714 !(dc->dc_flags & DW_F_NOTDP)) {
1715 tdp = xcalloc(sizeof (tdesc_t));
1721 tdp->t_name = die_name(dw, die);
1723 dc->dc_create(dw, die, off, tdp);
1727 die_create(dwarf_t *dw, Dwarf_Die die)
1730 die_create_one(dw, die);
1731 } while ((die = die_sibling(dw, die)) != NULL);
1734 static tdtrav_cb_f die_resolvers[] = {
1736 NULL, /* intrinsic */
1738 die_array_resolve, /* array */
1739 NULL, /* function */
1740 die_sou_resolve, /* struct */
1741 die_sou_resolve, /* union */
1742 die_enum_resolve, /* enum */
1743 die_fwd_resolve, /* forward */
1745 NULL, /* typedef unres */
1746 NULL, /* volatile */
1748 NULL, /* restrict */
1751 static tdtrav_cb_f die_fail_reporters[] = {
1753 NULL, /* intrinsic */
1755 die_array_failed, /* array */
1756 NULL, /* function */
1757 die_sou_failed, /* struct */
1758 die_sou_failed, /* union */
1762 NULL, /* typedef unres */
1763 NULL, /* volatile */
1765 NULL, /* restrict */
1769 die_resolve(dwarf_t *dw)
1778 (void) iitraverse_hash(dw->dw_td->td_iihash,
1779 &dw->dw_td->td_curvgen, NULL, NULL, die_resolvers, dw);
1781 debug(3, "resolve: pass %d, %u left\n", pass, dw->dw_nunres);
1783 if ((int) dw->dw_nunres == last) {
1784 fprintf(stderr, "%s: failed to resolve the following "
1785 "types:\n", progname);
1787 (void) iitraverse_hash(dw->dw_td->td_iihash,
1788 &dw->dw_td->td_curvgen, NULL, NULL,
1789 die_fail_reporters, dw);
1791 terminate("failed to resolve types\n");
1794 last = dw->dw_nunres;
1796 } while (dw->dw_nunres != 0);
1801 dw_read(tdata_t *td, Elf *elf, char *filename __unused)
1803 Dwarf_Unsigned abboff, hdrlen, nxthdr;
1804 Dwarf_Half vers, addrsz;
1806 Dwarf_Die child = 0;
1811 bzero(&dw, sizeof (dwarf_t));
1813 dw.dw_ptrsz = elf_ptrsz(elf);
1814 dw.dw_mfgtid_last = TID_MFGTID_BASE;
1815 dw.dw_tidhash = hash_new(TDESC_HASH_BUCKETS, tdesc_idhash, tdesc_idcmp);
1816 dw.dw_fwdhash = hash_new(TDESC_HASH_BUCKETS, tdesc_namehash,
1818 dw.dw_enumhash = hash_new(TDESC_HASH_BUCKETS, tdesc_namehash,
1821 if ((rc = dwarf_elf_init(elf, DW_DLC_READ, &dw.dw_dw,
1822 &dw.dw_err)) == DW_DLV_NO_ENTRY) {
1825 } else if (rc != DW_DLV_OK) {
1826 if (dwarf_errno(&dw.dw_err) == DW_DLE_DEBUG_INFO_NULL) {
1828 * There's no type data in the DWARF section, but
1829 * libdwarf is too clever to handle that properly.
1834 terminate("failed to initialize DWARF: %s\n",
1835 dwarf_errmsg(&dw.dw_err));
1838 if ((rc = dwarf_next_cu_header(dw.dw_dw, &hdrlen, &vers, &abboff,
1839 &addrsz, &nxthdr, &dw.dw_err)) != DW_DLV_OK)
1840 terminate("rc = %d %s\n", rc, dwarf_errmsg(&dw.dw_err));
1842 if ((cu = die_sibling(&dw, NULL)) == NULL)
1843 terminate("file does not contain dwarf type data "
1844 "(try compiling with -g)\n");
1846 dw.dw_maxoff = nxthdr - 1;
1848 if (dw.dw_maxoff > TID_FILEMAX)
1849 terminate("file contains too many types\n");
1851 debug(1, "DWARF version: %d\n", vers);
1852 if (vers != DWARF_VERSION) {
1853 terminate("file contains incompatible version %d DWARF code "
1854 "(version 2 required)\n", vers);
1857 if (die_string(&dw, cu, DW_AT_producer, &prod, 0)) {
1858 debug(1, "DWARF emitter: %s\n", prod);
1862 if ((dw.dw_cuname = die_name(&dw, cu)) != NULL) {
1863 char *base = xstrdup(basename(dw.dw_cuname));
1865 dw.dw_cuname = base;
1867 debug(1, "CU name: %s\n", dw.dw_cuname);
1870 if ((child = die_child(&dw, cu)) != NULL)
1871 die_create(&dw, child);
1873 if ((rc = dwarf_next_cu_header(dw.dw_dw, &hdrlen, &vers, &abboff,
1874 &addrsz, &nxthdr, &dw.dw_err)) != DW_DLV_NO_ENTRY)
1875 terminate("multiple compilation units not supported\n");
1877 (void) dwarf_finish(&dw.dw_dw, &dw.dw_err);
1881 cvt_fixups(td, dw.dw_ptrsz);
1883 /* leak the dwarf_t */