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.
27 * DWARF to tdata conversion
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.
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
56 * There are, as always, a few special cases that are handled during the first
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.
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.
79 * XXX see if a post traverser will allow the elimination of repeated pass 2
93 #include "ctf_headers.h"
99 /* The version of DWARF which we support. */
100 #define DWARF_VERSION 2
103 * We need to define a couple of our own intrinsics, to smooth out some of the
104 * differences between the GCC and DevPro DWARF emitters. See the referenced
105 * routines and the special cases in the file comment for more details.
107 * Type IDs are 32 bits wide. We're going to use the top of that field to
108 * indicate types that we've created ourselves.
110 #define TID_FILEMAX 0x3fffffff /* highest tid from file */
111 #define TID_VOID 0x40000001 /* see die_void() */
112 #define TID_LONG 0x40000002 /* see die_array() */
114 #define TID_MFGTID_BASE 0x40000003 /* first mfg'd tid */
117 * To reduce the staggering amount of error-handling code that would otherwise
118 * be required, the attribute-retrieval routines handle most of their own
119 * errors. If the following flag is supplied as the value of the `req'
120 * argument, they will also handle the absence of a requested attribute by
121 * terminating the program.
123 #define DW_ATTR_REQ 1
125 #define TDESC_HASH_BUCKETS 511
127 typedef struct dwarf {
128 Dwarf_Debug dw_dw; /* for libdwarf */
129 Dwarf_Error dw_err; /* for libdwarf */
130 Dwarf_Off dw_maxoff; /* highest legal offset in this cu */
131 tdata_t *dw_td; /* root of the tdesc/iidesc tree */
132 hash_t *dw_tidhash; /* hash of tdescs by t_id */
133 hash_t *dw_fwdhash; /* hash of fwd decls by name */
134 hash_t *dw_enumhash; /* hash of memberless enums by name */
135 tdesc_t *dw_void; /* manufactured void type */
136 tdesc_t *dw_long; /* manufactured long type for arrays */
137 size_t dw_ptrsz; /* size of a pointer in this file */
138 tid_t dw_mfgtid_last; /* last mfg'd type ID used */
139 uint_t dw_nunres; /* count of unresolved types */
140 char *dw_cuname; /* name of compilation unit */
143 static void die_create_one(dwarf_t *, Dwarf_Die);
144 static void die_create(dwarf_t *, Dwarf_Die);
147 mfgtid_next(dwarf_t *dw)
149 return (++dw->dw_mfgtid_last);
153 tdesc_add(dwarf_t *dw, tdesc_t *tdp)
155 hash_add(dw->dw_tidhash, tdp);
159 tdesc_lookup(dwarf_t *dw, int tid)
166 if (hash_find(dw->dw_tidhash, &tmpl, &tdp))
173 * Resolve a tdesc down to a node which should have a size. Returns the size,
174 * zero if the size hasn't yet been determined.
177 tdesc_size(tdesc_t *tdp)
180 switch (tdp->t_type) {
188 return (tdp->t_size);
200 case 0: /* not yet defined */
204 terminate("tdp %u: tdesc_size on unknown type %d\n",
205 tdp->t_id, tdp->t_type);
211 tdesc_bitsize(tdesc_t *tdp)
214 switch (tdp->t_type) {
216 return (tdp->t_intr->intr_nbits);
224 return (tdp->t_size * NBBY);
236 case 0: /* not yet defined */
240 terminate("tdp %u: tdesc_bitsize on unknown type %d\n",
241 tdp->t_id, tdp->t_type);
247 tdesc_basetype(tdesc_t *tdp)
250 switch (tdp->t_type) {
257 case 0: /* not yet defined */
266 die_off(dwarf_t *dw, Dwarf_Die die)
270 if (dwarf_dieoffset(die, &off, &dw->dw_err) == DW_DLV_OK)
273 terminate("failed to get offset for die: %s\n",
274 dwarf_errmsg(&dw->dw_err));
280 die_sibling(dwarf_t *dw, Dwarf_Die die)
285 if ((rc = dwarf_siblingof(dw->dw_dw, die, &sib, &dw->dw_err)) ==
288 else if (rc == DW_DLV_NO_ENTRY)
291 terminate("die %llu: failed to find type sibling: %s\n",
292 die_off(dw, die), dwarf_errmsg(&dw->dw_err));
298 die_child(dwarf_t *dw, Dwarf_Die die)
303 if ((rc = dwarf_child(die, &child, &dw->dw_err)) == DW_DLV_OK)
305 else if (rc == DW_DLV_NO_ENTRY)
308 terminate("die %llu: failed to find type child: %s\n",
309 die_off(dw, die), dwarf_errmsg(&dw->dw_err));
315 die_tag(dwarf_t *dw, Dwarf_Die die)
319 if (dwarf_tag(die, &tag, &dw->dw_err) == DW_DLV_OK)
322 terminate("die %llu: failed to get tag for type: %s\n",
323 die_off(dw, die), dwarf_errmsg(&dw->dw_err));
328 static Dwarf_Attribute
329 die_attr(dwarf_t *dw, Dwarf_Die die, Dwarf_Half name, int req)
331 Dwarf_Attribute attr;
334 if ((rc = dwarf_attr(die, name, &attr, &dw->dw_err)) == DW_DLV_OK) {
336 } else if (rc == DW_DLV_NO_ENTRY) {
338 terminate("die %llu: no attr 0x%x\n", die_off(dw, die),
345 terminate("die %llu: failed to get attribute for type: %s\n",
346 die_off(dw, die), dwarf_errmsg(&dw->dw_err));
352 die_signed(dwarf_t *dw, Dwarf_Die die, Dwarf_Half name, Dwarf_Signed *valp,
356 if (dwarf_attrval_signed(die, name, valp, &dw->dw_err) != DWARF_E_NONE) {
358 terminate("die %llu: failed to get signed: %s\n",
359 die_off(dw, die), dwarf_errmsg(&dw->dw_err));
367 die_unsigned(dwarf_t *dw, Dwarf_Die die, Dwarf_Half name, Dwarf_Unsigned *valp,
371 if (dwarf_attrval_unsigned(die, name, valp, &dw->dw_err) != DWARF_E_NONE) {
373 terminate("die %llu: failed to get unsigned: %s\n",
374 die_off(dw, die), dwarf_errmsg(&dw->dw_err));
382 die_bool(dwarf_t *dw, Dwarf_Die die, Dwarf_Half name, Dwarf_Bool *valp, int req)
386 if (dwarf_attrval_flag(die, name, valp, &dw->dw_err) != DWARF_E_NONE) {
388 terminate("die %llu: failed to get flag: %s\n",
389 die_off(dw, die), dwarf_errmsg(&dw->dw_err));
397 die_string(dwarf_t *dw, Dwarf_Die die, Dwarf_Half name, char **strp, int req)
399 const char *str = NULL;
401 if (dwarf_attrval_string(die, name, &str, &dw->dw_err) != DWARF_E_NONE ||
404 terminate("die %llu: failed to get string: %s\n",
405 die_off(dw, die), dwarf_errmsg(&dw->dw_err));
410 *strp = xstrdup(str);
416 die_attr_ref(dwarf_t *dw, Dwarf_Die die, Dwarf_Half name)
420 if (dwarf_attrval_unsigned(die, name, &off, &dw->dw_err) != DWARF_E_NONE) {
421 terminate("die %llu: failed to get ref: %s\n",
422 die_off(dw, die), dwarf_errmsg(&dw->dw_err));
429 die_name(dwarf_t *dw, Dwarf_Die die)
433 (void) die_string(dw, die, DW_AT_name, &str, 0);
439 die_isdecl(dwarf_t *dw, Dwarf_Die die)
443 return (die_bool(dw, die, DW_AT_declaration, &val, 0) && val);
447 die_isglobal(dwarf_t *dw, Dwarf_Die die)
453 * Some compilers (gcc) use DW_AT_external to indicate function
454 * visibility. Others (Sun) use DW_AT_visibility.
456 if (die_signed(dw, die, DW_AT_visibility, &vis, 0))
457 return (vis == DW_VIS_exported);
459 return (die_bool(dw, die, DW_AT_external, &ext, 0) && ext);
463 die_add(dwarf_t *dw, Dwarf_Off off)
465 tdesc_t *tdp = xcalloc(sizeof (tdesc_t));
475 die_lookup_pass1(dwarf_t *dw, Dwarf_Die die, Dwarf_Half name)
477 Dwarf_Off ref = die_attr_ref(dw, die, name);
480 if ((tdp = tdesc_lookup(dw, ref)) != NULL)
483 return (die_add(dw, ref));
487 die_mem_offset(dwarf_t *dw, Dwarf_Die die, Dwarf_Half name,
488 Dwarf_Unsigned *valp, int req __unused)
490 Dwarf_Locdesc *loc = NULL;
491 Dwarf_Signed locnum = 0;
495 if (name != DW_AT_data_member_location)
496 terminate("die %llu: can only process attribute "
497 "DW_AT_data_member_location\n", die_off(dw, die));
499 if ((at = die_attr(dw, die, name, 0)) == NULL)
502 if (dwarf_whatform(at, &form, &dw->dw_err) != DW_DLV_OK)
511 * GCC in base and Clang (3.3 or below) generates
512 * DW_AT_data_member_location attribute with DW_FORM_block*
513 * form. The attribute contains one DW_OP_plus_uconst
514 * operator. The member offset stores in the operand.
516 if (dwarf_locdesc(die, name, &loc, &locnum, &dw->dw_err) !=
519 if (locnum != 1 || loc->ld_s->lr_atom != DW_OP_plus_uconst) {
520 terminate("die %llu: cannot parse member offset\n",
523 *valp = loc->ld_s->lr_number;
532 * Clang 3.4 generates DW_AT_data_member_location attribute
533 * with DW_FORM_data* form (constant class). The attribute
534 * stores a contant value which is the member offset.
536 if (dwarf_attrval_unsigned(die, name, valp, &dw->dw_err) !=
542 terminate("die %llu: cannot parse member offset with form "
543 "%u\n", die_off(dw, die), form);
547 if (dwarf_locdesc_free(loc, &dw->dw_err) != DW_DLV_OK)
548 terminate("die %llu: cannot free location descriptor: %s\n",
549 die_off(dw, die), dwarf_errmsg(&dw->dw_err));
555 tdesc_intr_common(dwarf_t *dw, int tid, const char *name, size_t sz)
560 intr = xcalloc(sizeof (intr_t));
561 intr->intr_type = INTR_INT;
562 intr->intr_signed = 1;
563 intr->intr_nbits = sz * NBBY;
565 tdp = xcalloc(sizeof (tdesc_t));
566 tdp->t_name = xstrdup(name);
569 tdp->t_type = INTRINSIC;
571 tdp->t_flags = TDESC_F_RESOLVED;
579 * Manufacture a void type. Used for gcc-emitted stabs, where the lack of a
580 * type reference implies a reference to a void type. A void *, for example
581 * will be represented by a pointer die without a DW_AT_type. CTF requires
582 * that pointer nodes point to something, so we'll create a void for use as
583 * the target. Note that the DWARF data may already create a void type. Ours
584 * would then be a duplicate, but it'll be removed in the self-uniquification
585 * merge performed at the completion of DWARF->tdesc conversion.
588 tdesc_intr_void(dwarf_t *dw)
590 if (dw->dw_void == NULL)
591 dw->dw_void = tdesc_intr_common(dw, TID_VOID, "void", 0);
593 return (dw->dw_void);
597 tdesc_intr_long(dwarf_t *dw)
599 if (dw->dw_long == NULL) {
600 dw->dw_long = tdesc_intr_common(dw, TID_LONG, "long",
604 return (dw->dw_long);
608 * Used for creating bitfield types. We create a copy of an existing intrinsic,
609 * adjusting the size of the copy to match what the caller requested. The
610 * caller can then use the copy as the type for a bitfield structure member.
613 tdesc_intr_clone(dwarf_t *dw, tdesc_t *old, size_t bitsz)
615 tdesc_t *new = xcalloc(sizeof (tdesc_t));
617 if (!(old->t_flags & TDESC_F_RESOLVED)) {
618 terminate("tdp %u: attempt to make a bit field from an "
619 "unresolved type\n", old->t_id);
622 new->t_name = xstrdup(old->t_name);
623 new->t_size = old->t_size;
624 new->t_id = mfgtid_next(dw);
625 new->t_type = INTRINSIC;
626 new->t_flags = TDESC_F_RESOLVED;
628 new->t_intr = xcalloc(sizeof (intr_t));
629 bcopy(old->t_intr, new->t_intr, sizeof (intr_t));
630 new->t_intr->intr_nbits = bitsz;
638 tdesc_array_create(dwarf_t *dw, Dwarf_Die dim, tdesc_t *arrtdp,
643 tdesc_t *ctdp = NULL;
647 if ((dim2 = die_sibling(dw, dim)) == NULL) {
649 } else if (die_tag(dw, dim2) == DW_TAG_subrange_type) {
650 ctdp = xcalloc(sizeof (tdesc_t));
651 ctdp->t_id = mfgtid_next(dw);
652 debug(3, "die %llu: creating new type %u for sub-dimension\n",
653 die_off(dw, dim2), ctdp->t_id);
654 tdesc_array_create(dw, dim2, arrtdp, ctdp);
656 terminate("die %llu: unexpected non-subrange node in array\n",
660 dimtdp->t_type = ARRAY;
661 dimtdp->t_ardef = ar = xcalloc(sizeof (ardef_t));
664 * Array bounds can be signed or unsigned, but there are several kinds
665 * of signless forms (data1, data2, etc) that take their sign from the
666 * routine that is trying to interpret them. That is, data1 can be
667 * either signed or unsigned, depending on whether you use the signed or
668 * unsigned accessor function. GCC will use the signless forms to store
669 * unsigned values which have their high bit set, so we need to try to
670 * read them first as unsigned to get positive values. We could also
671 * try signed first, falling back to unsigned if we got a negative
674 if (die_unsigned(dw, dim, DW_AT_upper_bound, &uval, 0))
675 ar->ad_nelems = uval + 1;
676 else if (die_signed(dw, dim, DW_AT_upper_bound, &sval, 0))
677 ar->ad_nelems = sval + 1;
682 * Different compilers use different index types. Force the type to be
683 * a common, known value (long).
685 ar->ad_idxtype = tdesc_intr_long(dw);
686 ar->ad_contents = ctdp;
688 if (ar->ad_contents->t_size != 0) {
689 dimtdp->t_size = ar->ad_contents->t_size * ar->ad_nelems;
690 dimtdp->t_flags |= TDESC_F_RESOLVED;
695 * Create a tdesc from an array node. Some arrays will come with byte size
696 * attributes, and thus can be resolved immediately. Others don't, and will
697 * need to wait until the second pass for resolution.
700 die_array_create(dwarf_t *dw, Dwarf_Die arr, Dwarf_Off off, tdesc_t *tdp)
702 tdesc_t *arrtdp = die_lookup_pass1(dw, arr, DW_AT_type);
706 debug(3, "die %llu <%llx>: creating array\n", off, off);
708 if ((dim = die_child(dw, arr)) == NULL ||
709 die_tag(dw, dim) != DW_TAG_subrange_type)
710 terminate("die %llu: failed to retrieve array bounds\n", off);
712 tdesc_array_create(dw, dim, arrtdp, tdp);
714 if (die_unsigned(dw, arr, DW_AT_byte_size, &uval, 0)) {
721 * Ensure that sub-dimensions have sizes too before marking
724 flags = TDESC_F_RESOLVED;
725 for (dimtdp = tdp->t_ardef->ad_contents;
726 dimtdp->t_type == ARRAY;
727 dimtdp = dimtdp->t_ardef->ad_contents) {
728 if (!(dimtdp->t_flags & TDESC_F_RESOLVED)) {
734 tdp->t_flags |= flags;
737 debug(3, "die %llu <%llx>: array nelems %u size %u\n", off, off,
738 tdp->t_ardef->ad_nelems, tdp->t_size);
743 die_array_resolve(tdesc_t *tdp, tdesc_t **tdpp __unused, void *private)
745 dwarf_t *dw = private;
748 if (tdp->t_flags & TDESC_F_RESOLVED)
751 debug(3, "trying to resolve array %d (cont %d)\n", tdp->t_id,
752 tdp->t_ardef->ad_contents->t_id);
754 if ((sz = tdesc_size(tdp->t_ardef->ad_contents)) == 0) {
755 debug(3, "unable to resolve array %s (%d) contents %d\n",
756 tdesc_name(tdp), tdp->t_id,
757 tdp->t_ardef->ad_contents->t_id);
763 tdp->t_size = sz * tdp->t_ardef->ad_nelems;
764 tdp->t_flags |= TDESC_F_RESOLVED;
766 debug(3, "resolved array %d: %u bytes\n", tdp->t_id, tdp->t_size);
773 die_array_failed(tdesc_t *tdp, tdesc_t **tdpp __unused, void *private __unused)
775 tdesc_t *cont = tdp->t_ardef->ad_contents;
777 if (tdp->t_flags & TDESC_F_RESOLVED)
780 fprintf(stderr, "Array %d: failed to size contents type %s (%d)\n",
781 tdp->t_id, tdesc_name(cont), cont->t_id);
787 * Most enums (those with members) will be resolved during this first pass.
788 * Others - those without members (see the file comment) - won't be, and will
789 * need to wait until the second pass when they can be matched with their full
793 die_enum_create(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp)
799 debug(3, "die %llu: creating enum\n", off);
803 (void) die_unsigned(dw, die, DW_AT_byte_size, &uval, DW_ATTR_REQ);
806 if ((mem = die_child(dw, die)) != NULL) {
807 elist_t **elastp = &tdp->t_emem;
812 if (die_tag(dw, mem) != DW_TAG_enumerator) {
813 /* Nested type declaration */
814 die_create_one(dw, mem);
818 el = xcalloc(sizeof (elist_t));
819 el->el_name = die_name(dw, mem);
821 if (die_signed(dw, mem, DW_AT_const_value, &sval, 0)) {
822 el->el_number = sval;
823 } else if (die_unsigned(dw, mem, DW_AT_const_value,
825 el->el_number = uval;
827 terminate("die %llu: enum %llu: member without "
828 "value\n", off, die_off(dw, mem));
831 debug(3, "die %llu: enum %llu: created %s = %d\n", off,
832 die_off(dw, mem), el->el_name, el->el_number);
835 elastp = &el->el_next;
837 } while ((mem = die_sibling(dw, mem)) != NULL);
839 hash_add(dw->dw_enumhash, tdp);
841 tdp->t_flags |= TDESC_F_RESOLVED;
843 if (tdp->t_name != NULL) {
844 iidesc_t *ii = xcalloc(sizeof (iidesc_t));
845 ii->ii_type = II_SOU;
846 ii->ii_name = xstrdup(tdp->t_name);
849 iidesc_add(dw->dw_td->td_iihash, ii);
855 die_enum_match(void *arg1, void *arg2)
857 tdesc_t *tdp = arg1, **fullp = arg2;
859 if (tdp->t_emem != NULL) {
861 return (-1); /* stop the iteration */
869 die_enum_resolve(tdesc_t *tdp, tdesc_t **tdpp __unused, void *private)
871 dwarf_t *dw = private;
872 tdesc_t *full = NULL;
874 if (tdp->t_flags & TDESC_F_RESOLVED)
877 (void) hash_find_iter(dw->dw_enumhash, tdp, die_enum_match, &full);
880 * The answer to this one won't change from iteration to iteration,
884 terminate("tdp %u: enum %s has no members\n", tdp->t_id,
888 debug(3, "tdp %u: enum %s redirected to %u\n", tdp->t_id,
889 tdesc_name(tdp), full->t_id);
891 tdp->t_flags |= TDESC_F_RESOLVED;
897 die_fwd_map(void *arg1, void *arg2)
899 tdesc_t *fwd = arg1, *sou = arg2;
901 debug(3, "tdp %u: mapped forward %s to sou %u\n", fwd->t_id,
902 tdesc_name(fwd), sou->t_id);
909 * Structures and unions will never be resolved during the first pass, as we
910 * won't be able to fully determine the member sizes. The second pass, which
911 * have access to sizing information, will be able to complete the resolution.
914 die_sou_create(dwarf_t *dw, Dwarf_Die str, Dwarf_Off off, tdesc_t *tdp,
915 int type, const char *typename)
917 Dwarf_Unsigned sz, bitsz, bitoff;
918 #if BYTE_ORDER == _LITTLE_ENDIAN
922 mlist_t *ml, **mlastp;
925 tdp->t_type = (die_isdecl(dw, str) ? FORWARD : type);
927 debug(3, "die %llu: creating %s %s\n", off,
928 (tdp->t_type == FORWARD ? "forward decl" : typename),
931 if (tdp->t_type == FORWARD) {
932 hash_add(dw->dw_fwdhash, tdp);
936 (void) hash_find_iter(dw->dw_fwdhash, tdp, die_fwd_map, tdp);
938 (void) die_unsigned(dw, str, DW_AT_byte_size, &sz, DW_ATTR_REQ);
942 * GCC allows empty SOUs as an extension.
944 if ((mem = die_child(dw, str)) == NULL) {
948 mlastp = &tdp->t_members;
951 Dwarf_Off memoff = die_off(dw, mem);
952 Dwarf_Half tag = die_tag(dw, mem);
953 Dwarf_Unsigned mloff;
955 if (tag != DW_TAG_member) {
956 /* Nested type declaration */
957 die_create_one(dw, mem);
961 debug(3, "die %llu: mem %llu: creating member\n", off, memoff);
963 ml = xcalloc(sizeof (mlist_t));
966 * This could be a GCC anon struct/union member, so we'll allow
967 * an empty name, even though nothing can really handle them
968 * properly. Note that some versions of GCC miss out debug
969 * info for anon structs, though recent versions are fixed (gcc
972 if ((ml->ml_name = die_name(dw, mem)) == NULL)
975 ml->ml_type = die_lookup_pass1(dw, mem, DW_AT_type);
977 if (die_mem_offset(dw, mem, DW_AT_data_member_location,
979 debug(3, "die %llu: got mloff %llx\n", off,
980 (u_longlong_t)mloff);
981 ml->ml_offset = mloff * 8;
984 if (die_unsigned(dw, mem, DW_AT_bit_size, &bitsz, 0))
987 ml->ml_size = tdesc_bitsize(ml->ml_type);
989 if (die_unsigned(dw, mem, DW_AT_bit_offset, &bitoff, 0)) {
990 #if BYTE_ORDER == _BIG_ENDIAN
991 ml->ml_offset += bitoff;
994 * Note that Clang 3.4 will sometimes generate
995 * member DIE before generating the DIE for the
996 * member's type. The code can not handle this
997 * properly so that tdesc_bitsize(ml->ml_type) will
998 * return 0 because ml->ml_type is unknown. As a
999 * result, a wrong member offset will be calculated.
1000 * To workaround this, we can instead try to
1001 * retrieve the value of DW_AT_byte_size attribute
1002 * which stores the byte size of the space occupied
1003 * by the type. If this attribute exists, its value
1004 * should equal to tdesc_bitsize(ml->ml_type)/NBBY.
1006 if (die_unsigned(dw, mem, DW_AT_byte_size, &bysz, 0) &&
1008 ml->ml_offset += bysz * NBBY - bitoff -
1011 ml->ml_offset += tdesc_bitsize(ml->ml_type) -
1012 bitoff - ml->ml_size;
1016 debug(3, "die %llu: mem %llu: created \"%s\" (off %u sz %u)\n",
1017 off, memoff, ml->ml_name, ml->ml_offset, ml->ml_size);
1020 mlastp = &ml->ml_next;
1021 } while ((mem = die_sibling(dw, mem)) != NULL);
1024 * GCC will attempt to eliminate unused types, thus decreasing the
1025 * size of the emitted dwarf. That is, if you declare a foo_t in your
1026 * header, include said header in your source file, and neglect to
1027 * actually use (directly or indirectly) the foo_t in the source file,
1028 * the foo_t won't make it into the emitted DWARF. So, at least, goes
1031 * Occasionally, it'll emit the DW_TAG_structure_type for the foo_t,
1032 * and then neglect to emit the members. Strangely, the loner struct
1033 * tag will always be followed by a proper nested declaration of
1034 * something else. This is clearly a bug, but we're not going to have
1035 * time to get it fixed before this goo goes back, so we'll have to work
1036 * around it. If we see a no-membered struct with a nested declaration
1037 * (i.e. die_child of the struct tag won't be null), we'll ignore it.
1038 * Being paranoid, we won't simply remove it from the hash. Instead,
1039 * we'll decline to create an iidesc for it, thus ensuring that this
1040 * type won't make it into the output file. To be safe, we'll also
1043 if (tdp->t_members == NULL) {
1044 const char *old = tdesc_name(tdp);
1045 size_t newsz = 7 + strlen(old) + 1;
1046 char *new = xmalloc(newsz);
1047 (void) snprintf(new, newsz, "orphan %s", old);
1049 debug(3, "die %llu: worked around %s %s\n", off, typename, old);
1051 if (tdp->t_name != NULL)
1058 if (tdp->t_name != NULL) {
1059 ii = xcalloc(sizeof (iidesc_t));
1060 ii->ii_type = II_SOU;
1061 ii->ii_name = xstrdup(tdp->t_name);
1064 iidesc_add(dw->dw_td->td_iihash, ii);
1069 die_struct_create(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp)
1071 die_sou_create(dw, die, off, tdp, STRUCT, "struct");
1075 die_union_create(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp)
1077 die_sou_create(dw, die, off, tdp, UNION, "union");
1082 die_sou_resolve(tdesc_t *tdp, tdesc_t **tdpp __unused, void *private)
1084 dwarf_t *dw = private;
1088 if (tdp->t_flags & TDESC_F_RESOLVED)
1091 debug(3, "resolving sou %s\n", tdesc_name(tdp));
1093 for (ml = tdp->t_members; ml != NULL; ml = ml->ml_next) {
1094 if (ml->ml_size == 0) {
1095 mt = tdesc_basetype(ml->ml_type);
1097 if ((ml->ml_size = tdesc_bitsize(mt)) != 0)
1101 * For empty members, or GCC/C99 flexible array
1102 * members, a size of 0 is correct.
1104 if (mt->t_members == NULL)
1106 if (mt->t_type == ARRAY && mt->t_ardef->ad_nelems == 0)
1113 if ((mt = tdesc_basetype(ml->ml_type)) == NULL) {
1118 if (ml->ml_size != 0 && mt->t_type == INTRINSIC &&
1119 mt->t_intr->intr_nbits != ml->ml_size) {
1121 * This member is a bitfield, and needs to reference
1122 * an intrinsic type with the same width. If the
1123 * currently-referenced type isn't of the same width,
1124 * we'll copy it, adjusting the width of the copy to
1125 * the size we'd like.
1127 debug(3, "tdp %u: creating bitfield for %d bits\n",
1128 tdp->t_id, ml->ml_size);
1130 ml->ml_type = tdesc_intr_clone(dw, mt, ml->ml_size);
1134 tdp->t_flags |= TDESC_F_RESOLVED;
1141 die_sou_failed(tdesc_t *tdp, tdesc_t **tdpp __unused, void *private __unused)
1143 const char *typename = (tdp->t_type == STRUCT ? "struct" : "union");
1146 if (tdp->t_flags & TDESC_F_RESOLVED)
1149 for (ml = tdp->t_members; ml != NULL; ml = ml->ml_next) {
1150 if (ml->ml_size == 0) {
1151 fprintf(stderr, "%s %d <%x>: failed to size member \"%s\" "
1152 "of type %s (%d <%x>)\n", typename, tdp->t_id,
1154 ml->ml_name, tdesc_name(ml->ml_type),
1155 ml->ml_type->t_id, ml->ml_type->t_id);
1163 die_funcptr_create(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp)
1165 Dwarf_Attribute attr;
1171 debug(3, "die %llu <%llx>: creating function pointer\n", off, off);
1174 * We'll begin by processing any type definition nodes that may be
1175 * lurking underneath this one.
1177 for (arg = die_child(dw, die); arg != NULL;
1178 arg = die_sibling(dw, arg)) {
1179 if ((tag = die_tag(dw, arg)) != DW_TAG_formal_parameter &&
1180 tag != DW_TAG_unspecified_parameters) {
1181 /* Nested type declaration */
1182 die_create_one(dw, arg);
1186 if (die_isdecl(dw, die)) {
1188 * This is a prototype. We don't add prototypes to the
1189 * tree, so we're going to drop the tdesc. Unfortunately,
1190 * it has already been added to the tree. Nobody will reference
1191 * it, though, and it will be leaked.
1196 fn = xcalloc(sizeof (fndef_t));
1198 tdp->t_type = FUNCTION;
1200 if ((attr = die_attr(dw, die, DW_AT_type, 0)) != NULL) {
1201 fn->fn_ret = die_lookup_pass1(dw, die, DW_AT_type);
1203 fn->fn_ret = tdesc_intr_void(dw);
1207 * Count the arguments to the function, then read them in.
1209 for (fn->fn_nargs = 0, arg = die_child(dw, die); arg != NULL;
1210 arg = die_sibling(dw, arg)) {
1211 if ((tag = die_tag(dw, arg)) == DW_TAG_formal_parameter)
1213 else if (tag == DW_TAG_unspecified_parameters &&
1218 if (fn->fn_nargs != 0) {
1219 debug(3, "die %llu: adding %d argument%s\n", off, fn->fn_nargs,
1220 (fn->fn_nargs > 1 ? "s" : ""));
1222 fn->fn_args = xcalloc(sizeof (tdesc_t *) * fn->fn_nargs);
1223 for (i = 0, arg = die_child(dw, die);
1224 arg != NULL && i < (int) fn->fn_nargs;
1225 arg = die_sibling(dw, arg)) {
1226 if (die_tag(dw, arg) != DW_TAG_formal_parameter)
1229 fn->fn_args[i++] = die_lookup_pass1(dw, arg,
1235 tdp->t_flags |= TDESC_F_RESOLVED;
1239 * GCC and DevPro use different names for the base types. While the terms are
1240 * the same, they are arranged in a different order. Some terms, such as int,
1241 * are implied in one, and explicitly named in the other. Given a base type
1242 * as input, this routine will return a common name, along with an intr_t
1243 * that reflects said name.
1246 die_base_name_parse(const char *name, char **newp)
1251 int nlong = 0, nshort = 0, nchar = 0, nint = 0;
1256 if (strlen(name) > sizeof (buf) - 1)
1257 terminate("base type name \"%s\" is too long\n", name);
1259 strncpy(buf, name, sizeof (buf));
1261 for (c = strtok(buf, " "); c != NULL; c = strtok(NULL, " ")) {
1262 if (strcmp(c, "signed") == 0)
1264 else if (strcmp(c, "unsigned") == 0)
1266 else if (strcmp(c, "long") == 0)
1268 else if (strcmp(c, "char") == 0) {
1271 } else if (strcmp(c, "short") == 0)
1273 else if (strcmp(c, "int") == 0)
1277 * If we don't recognize any of the tokens, we'll tell
1278 * the caller to fall back to the dwarf-provided
1279 * encoding information.
1285 if (nchar > 1 || nshort > 1 || nint > 1 || nlong > 2)
1289 if (nlong > 0 || nshort > 0 || nint > 0)
1294 } else if (nshort > 0) {
1300 } else if (nlong > 0) {
1307 intr = xcalloc(sizeof (intr_t));
1308 intr->intr_type = INTR_INT;
1309 intr->intr_signed = sign;
1310 intr->intr_iformat = fmt;
1312 snprintf(buf, sizeof (buf), "%s%s%s",
1313 (sign ? "" : "unsigned "),
1314 (nlong > 1 ? "long " : ""),
1317 *newp = xstrdup(buf);
1321 typedef struct fp_size_map {
1322 size_t fsm_typesz[2]; /* size of {32,64} type */
1323 uint_t fsm_enc[3]; /* CTF_FP_* for {bare,cplx,imagry} type */
1326 static const fp_size_map_t fp_encodings[] = {
1327 { { 4, 4 }, { CTF_FP_SINGLE, CTF_FP_CPLX, CTF_FP_IMAGRY } },
1328 { { 8, 8 }, { CTF_FP_DOUBLE, CTF_FP_DCPLX, CTF_FP_DIMAGRY } },
1330 { { 16, 16 }, { CTF_FP_LDOUBLE, CTF_FP_LDCPLX, CTF_FP_LDIMAGRY } },
1332 { { 12, 16 }, { CTF_FP_LDOUBLE, CTF_FP_LDCPLX, CTF_FP_LDIMAGRY } },
1334 { { 0, 0 }, { 0, 0, 0 } }
1338 die_base_type2enc(dwarf_t *dw, Dwarf_Off off, Dwarf_Signed enc, size_t sz)
1340 const fp_size_map_t *map = fp_encodings;
1341 uint_t szidx = dw->dw_ptrsz == sizeof (uint64_t);
1342 uint_t mult = 1, col = 0;
1344 if (enc == DW_ATE_complex_float) {
1347 } else if (enc == DW_ATE_imaginary_float
1349 || enc == DW_ATE_SUN_imaginary_float
1354 while (map->fsm_typesz[szidx] != 0) {
1355 if (map->fsm_typesz[szidx] * mult == sz)
1356 return (map->fsm_enc[col]);
1360 terminate("die %llu: unrecognized real type size %u\n", off, sz);
1366 die_base_from_dwarf(dwarf_t *dw, Dwarf_Die base, Dwarf_Off off, size_t sz)
1368 intr_t *intr = xcalloc(sizeof (intr_t));
1371 (void) die_signed(dw, base, DW_AT_encoding, &enc, DW_ATTR_REQ);
1374 case DW_ATE_unsigned:
1375 case DW_ATE_address:
1376 intr->intr_type = INTR_INT;
1378 case DW_ATE_unsigned_char:
1379 intr->intr_type = INTR_INT;
1380 intr->intr_iformat = 'c';
1383 intr->intr_type = INTR_INT;
1384 intr->intr_signed = 1;
1386 case DW_ATE_signed_char:
1387 intr->intr_type = INTR_INT;
1388 intr->intr_signed = 1;
1389 intr->intr_iformat = 'c';
1391 case DW_ATE_boolean:
1392 intr->intr_type = INTR_INT;
1393 intr->intr_signed = 1;
1394 intr->intr_iformat = 'b';
1397 case DW_ATE_complex_float:
1398 case DW_ATE_imaginary_float:
1400 case DW_ATE_SUN_imaginary_float:
1401 case DW_ATE_SUN_interval_float:
1403 intr->intr_type = INTR_REAL;
1404 intr->intr_signed = 1;
1405 intr->intr_fformat = die_base_type2enc(dw, off, enc, sz);
1408 terminate("die %llu: unknown base type encoding 0x%llx\n",
1416 die_base_create(dwarf_t *dw, Dwarf_Die base, Dwarf_Off off, tdesc_t *tdp)
1422 debug(3, "die %llu: creating base type\n", off);
1425 * The compilers have their own clever (internally inconsistent) ideas
1426 * as to what base types should look like. Some times gcc will, for
1427 * example, use DW_ATE_signed_char for char. Other times, however, it
1428 * will use DW_ATE_signed. Needless to say, this causes some problems
1429 * down the road, particularly with merging. We do, however, use the
1430 * DWARF idea of type sizes, as this allows us to avoid caring about
1433 (void) die_unsigned(dw, base, DW_AT_byte_size, &sz, DW_ATTR_REQ);
1435 if (tdp->t_name == NULL)
1436 terminate("die %llu: base type without name\n", off);
1438 /* XXX make a name parser for float too */
1439 if ((intr = die_base_name_parse(tdp->t_name, &new)) != NULL) {
1440 /* Found it. We'll use the parsed version */
1441 debug(3, "die %llu: name \"%s\" remapped to \"%s\"\n", off,
1442 tdesc_name(tdp), new);
1448 * We didn't recognize the type, so we'll create an intr_t
1449 * based on the DWARF data.
1451 debug(3, "die %llu: using dwarf data for base \"%s\"\n", off,
1454 intr = die_base_from_dwarf(dw, base, off, sz);
1457 intr->intr_nbits = sz * 8;
1459 tdp->t_type = INTRINSIC;
1463 tdp->t_flags |= TDESC_F_RESOLVED;
1467 die_through_create(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp,
1468 int type, const char *typename)
1470 Dwarf_Attribute attr;
1472 debug(3, "die %llu <%llx>: creating %s type %d\n", off, off, typename, type);
1476 if ((attr = die_attr(dw, die, DW_AT_type, 0)) != NULL) {
1477 tdp->t_tdesc = die_lookup_pass1(dw, die, DW_AT_type);
1479 tdp->t_tdesc = tdesc_intr_void(dw);
1482 if (type == POINTER)
1483 tdp->t_size = dw->dw_ptrsz;
1485 tdp->t_flags |= TDESC_F_RESOLVED;
1487 if (type == TYPEDEF) {
1488 iidesc_t *ii = xcalloc(sizeof (iidesc_t));
1489 ii->ii_type = II_TYPE;
1490 ii->ii_name = xstrdup(tdp->t_name);
1493 iidesc_add(dw->dw_td->td_iihash, ii);
1498 die_typedef_create(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp)
1500 die_through_create(dw, die, off, tdp, TYPEDEF, "typedef");
1504 die_const_create(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp)
1506 die_through_create(dw, die, off, tdp, CONST, "const");
1510 die_pointer_create(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp)
1512 die_through_create(dw, die, off, tdp, POINTER, "pointer");
1516 die_restrict_create(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp)
1518 die_through_create(dw, die, off, tdp, RESTRICT, "restrict");
1522 die_volatile_create(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp)
1524 die_through_create(dw, die, off, tdp, VOLATILE, "volatile");
1529 die_function_create(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp __unused)
1536 debug(3, "die %llu <%llx>: creating function definition\n", off, off);
1539 * We'll begin by processing any type definition nodes that may be
1540 * lurking underneath this one.
1542 for (arg = die_child(dw, die); arg != NULL;
1543 arg = die_sibling(dw, arg)) {
1544 if ((tag = die_tag(dw, arg)) != DW_TAG_formal_parameter &&
1545 tag != DW_TAG_variable) {
1546 /* Nested type declaration */
1547 die_create_one(dw, arg);
1551 if (die_isdecl(dw, die) || (name = die_name(dw, die)) == NULL) {
1553 * We process neither prototypes nor subprograms without
1559 ii = xcalloc(sizeof (iidesc_t));
1560 ii->ii_type = die_isglobal(dw, die) ? II_GFUN : II_SFUN;
1562 if (ii->ii_type == II_SFUN)
1563 ii->ii_owner = xstrdup(dw->dw_cuname);
1565 debug(3, "die %llu: function %s is %s\n", off, ii->ii_name,
1566 (ii->ii_type == II_GFUN ? "global" : "static"));
1568 if (die_attr(dw, die, DW_AT_type, 0) != NULL)
1569 ii->ii_dtype = die_lookup_pass1(dw, die, DW_AT_type);
1571 ii->ii_dtype = tdesc_intr_void(dw);
1573 for (arg = die_child(dw, die); arg != NULL;
1574 arg = die_sibling(dw, arg)) {
1577 debug(3, "die %llu: looking at sub member at %llu\n",
1578 off, die_off(dw, die));
1580 if (die_tag(dw, arg) != DW_TAG_formal_parameter)
1583 if ((name1 = die_name(dw, arg)) == NULL) {
1584 terminate("die %llu: func arg %d has no name\n",
1585 off, ii->ii_nargs + 1);
1588 if (strcmp(name1, "...") == 0) {
1597 if (ii->ii_nargs > 0) {
1600 debug(3, "die %llu: function has %d argument%s\n", off,
1601 ii->ii_nargs, (ii->ii_nargs == 1 ? "" : "s"));
1603 ii->ii_args = xcalloc(sizeof (tdesc_t) * ii->ii_nargs);
1605 for (arg = die_child(dw, die), i = 0;
1606 arg != NULL && i < ii->ii_nargs;
1607 arg = die_sibling(dw, arg)) {
1608 if (die_tag(dw, arg) != DW_TAG_formal_parameter)
1611 ii->ii_args[i++] = die_lookup_pass1(dw, arg,
1616 iidesc_add(dw->dw_td->td_iihash, ii);
1621 die_variable_create(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off, tdesc_t *tdp __unused)
1626 debug(3, "die %llu: creating object definition\n", off);
1628 if (die_isdecl(dw, die) || (name = die_name(dw, die)) == NULL)
1629 return; /* skip prototypes and nameless objects */
1631 ii = xcalloc(sizeof (iidesc_t));
1632 ii->ii_type = die_isglobal(dw, die) ? II_GVAR : II_SVAR;
1634 ii->ii_dtype = die_lookup_pass1(dw, die, DW_AT_type);
1635 if (ii->ii_type == II_SVAR)
1636 ii->ii_owner = xstrdup(dw->dw_cuname);
1638 iidesc_add(dw->dw_td->td_iihash, ii);
1643 die_fwd_resolve(tdesc_t *fwd, tdesc_t **fwdp, void *private __unused)
1645 if (fwd->t_flags & TDESC_F_RESOLVED)
1648 if (fwd->t_tdesc != NULL) {
1649 debug(3, "tdp %u: unforwarded %s\n", fwd->t_id,
1651 *fwdp = fwd->t_tdesc;
1654 fwd->t_flags |= TDESC_F_RESOLVED;
1661 die_lexblk_descend(dwarf_t *dw, Dwarf_Die die, Dwarf_Off off __unused, tdesc_t *tdp __unused)
1663 Dwarf_Die child = die_child(dw, die);
1666 die_create(dw, child);
1670 * Used to map the die to a routine which can parse it, using the tag to do the
1671 * mapping. While the processing of most tags entails the creation of a tdesc,
1672 * there are a few which don't - primarily those which result in the creation of
1673 * iidescs which refer to existing tdescs.
1676 #define DW_F_NOTDP 0x1 /* Don't create a tdesc for the creator */
1678 typedef struct die_creator {
1681 void (*dc_create)(dwarf_t *, Dwarf_Die, Dwarf_Off, tdesc_t *);
1684 static const die_creator_t die_creators[] = {
1685 { DW_TAG_array_type, 0, die_array_create },
1686 { DW_TAG_enumeration_type, 0, die_enum_create },
1687 { DW_TAG_lexical_block, DW_F_NOTDP, die_lexblk_descend },
1688 { DW_TAG_pointer_type, 0, die_pointer_create },
1689 { DW_TAG_structure_type, 0, die_struct_create },
1690 { DW_TAG_subroutine_type, 0, die_funcptr_create },
1691 { DW_TAG_typedef, 0, die_typedef_create },
1692 { DW_TAG_union_type, 0, die_union_create },
1693 { DW_TAG_base_type, 0, die_base_create },
1694 { DW_TAG_const_type, 0, die_const_create },
1695 { DW_TAG_subprogram, DW_F_NOTDP, die_function_create },
1696 { DW_TAG_variable, DW_F_NOTDP, die_variable_create },
1697 { DW_TAG_volatile_type, 0, die_volatile_create },
1698 { DW_TAG_restrict_type, 0, die_restrict_create },
1702 static const die_creator_t *
1703 die_tag2ctor(Dwarf_Half tag)
1705 const die_creator_t *dc;
1707 for (dc = die_creators; dc->dc_create != NULL; dc++) {
1708 if (dc->dc_tag == tag)
1716 die_create_one(dwarf_t *dw, Dwarf_Die die)
1718 Dwarf_Off off = die_off(dw, die);
1719 const die_creator_t *dc;
1723 debug(3, "die %llu <%llx>: create_one\n", off, off);
1725 if (off > dw->dw_maxoff) {
1726 terminate("illegal die offset %llu (max %llu)\n", off,
1730 tag = die_tag(dw, die);
1732 if ((dc = die_tag2ctor(tag)) == NULL) {
1733 debug(2, "die %llu: ignoring tag type %x\n", off, tag);
1737 if ((tdp = tdesc_lookup(dw, off)) == NULL &&
1738 !(dc->dc_flags & DW_F_NOTDP)) {
1739 tdp = xcalloc(sizeof (tdesc_t));
1745 tdp->t_name = die_name(dw, die);
1747 dc->dc_create(dw, die, off, tdp);
1751 die_create(dwarf_t *dw, Dwarf_Die die)
1754 die_create_one(dw, die);
1755 } while ((die = die_sibling(dw, die)) != NULL);
1758 static tdtrav_cb_f die_resolvers[] = {
1760 NULL, /* intrinsic */
1762 die_array_resolve, /* array */
1763 NULL, /* function */
1764 die_sou_resolve, /* struct */
1765 die_sou_resolve, /* union */
1766 die_enum_resolve, /* enum */
1767 die_fwd_resolve, /* forward */
1769 NULL, /* typedef unres */
1770 NULL, /* volatile */
1772 NULL, /* restrict */
1775 static tdtrav_cb_f die_fail_reporters[] = {
1777 NULL, /* intrinsic */
1779 die_array_failed, /* array */
1780 NULL, /* function */
1781 die_sou_failed, /* struct */
1782 die_sou_failed, /* union */
1786 NULL, /* typedef unres */
1787 NULL, /* volatile */
1789 NULL, /* restrict */
1793 die_resolve(dwarf_t *dw)
1802 (void) iitraverse_hash(dw->dw_td->td_iihash,
1803 &dw->dw_td->td_curvgen, NULL, NULL, die_resolvers, dw);
1805 debug(3, "resolve: pass %d, %u left\n", pass, dw->dw_nunres);
1807 if ((int) dw->dw_nunres == last) {
1808 fprintf(stderr, "%s: failed to resolve the following "
1809 "types:\n", progname);
1811 (void) iitraverse_hash(dw->dw_td->td_iihash,
1812 &dw->dw_td->td_curvgen, NULL, NULL,
1813 die_fail_reporters, dw);
1815 terminate("failed to resolve types\n");
1818 last = dw->dw_nunres;
1820 } while (dw->dw_nunres != 0);
1824 * Any object containing a function or object symbol at any scope should also
1825 * contain DWARF data.
1828 should_have_dwarf(Elf *elf)
1830 Elf_Scn *scn = NULL;
1831 Elf_Data *data = NULL;
1836 boolean_t found = B_FALSE;
1838 while ((scn = elf_nextscn(elf, scn)) != NULL) {
1839 gelf_getshdr(scn, &shdr);
1841 if (shdr.sh_type == SHT_SYMTAB) {
1848 terminate("cannot convert stripped objects\n");
1850 data = elf_getdata(scn, NULL);
1851 nsyms = shdr.sh_size / shdr.sh_entsize;
1853 for (symdx = 0; symdx < nsyms; symdx++) {
1854 gelf_getsym(data, symdx, &sym);
1856 if ((GELF_ST_TYPE(sym.st_info) == STT_FUNC) ||
1857 (GELF_ST_TYPE(sym.st_info) == STT_TLS) ||
1858 (GELF_ST_TYPE(sym.st_info) == STT_OBJECT)) {
1861 name = elf_strptr(elf, shdr.sh_link, sym.st_name);
1863 /* Studio emits these local symbols regardless */
1864 if ((strcmp(name, "Bbss.bss") != 0) &&
1865 (strcmp(name, "Ttbss.bss") != 0) &&
1866 (strcmp(name, "Ddata.data") != 0) &&
1867 (strcmp(name, "Ttdata.data") != 0) &&
1868 (strcmp(name, "Drodata.rodata") != 0))
1878 dw_read(tdata_t *td, Elf *elf, char *filename __unused)
1880 Dwarf_Unsigned abboff, hdrlen, nxthdr;
1881 Dwarf_Half vers, addrsz;
1883 Dwarf_Die child = 0;
1888 bzero(&dw, sizeof (dwarf_t));
1890 dw.dw_ptrsz = elf_ptrsz(elf);
1891 dw.dw_mfgtid_last = TID_MFGTID_BASE;
1892 dw.dw_tidhash = hash_new(TDESC_HASH_BUCKETS, tdesc_idhash, tdesc_idcmp);
1893 dw.dw_fwdhash = hash_new(TDESC_HASH_BUCKETS, tdesc_namehash,
1895 dw.dw_enumhash = hash_new(TDESC_HASH_BUCKETS, tdesc_namehash,
1898 if ((rc = dwarf_elf_init(elf, DW_DLC_READ, &dw.dw_dw,
1899 &dw.dw_err)) == DW_DLV_NO_ENTRY) {
1900 if (should_have_dwarf(elf)) {
1906 } else if (rc != DW_DLV_OK) {
1907 if (dwarf_errno(&dw.dw_err) == DW_DLE_DEBUG_INFO_NULL) {
1909 * There's no type data in the DWARF section, but
1910 * libdwarf is too clever to handle that properly.
1915 terminate("failed to initialize DWARF: %s\n",
1916 dwarf_errmsg(&dw.dw_err));
1919 if ((rc = dwarf_next_cu_header(dw.dw_dw, &hdrlen, &vers, &abboff,
1920 &addrsz, &nxthdr, &dw.dw_err)) != DW_DLV_OK)
1921 terminate("rc = %d %s\n", rc, dwarf_errmsg(&dw.dw_err));
1923 if ((cu = die_sibling(&dw, NULL)) == NULL ||
1924 (((child = die_child(&dw, cu)) == NULL) &&
1925 should_have_dwarf(elf))) {
1926 terminate("file does not contain dwarf type data "
1927 "(try compiling with -g)\n");
1928 } else if (child == NULL) {
1932 dw.dw_maxoff = nxthdr - 1;
1934 if (dw.dw_maxoff > TID_FILEMAX)
1935 terminate("file contains too many types\n");
1937 debug(1, "DWARF version: %d\n", vers);
1938 if (vers != DWARF_VERSION) {
1939 terminate("file contains incompatible version %d DWARF code "
1940 "(version 2 required)\n", vers);
1943 if (die_string(&dw, cu, DW_AT_producer, &prod, 0)) {
1944 debug(1, "DWARF emitter: %s\n", prod);
1948 if ((dw.dw_cuname = die_name(&dw, cu)) != NULL) {
1949 char *base = xstrdup(basename(dw.dw_cuname));
1951 dw.dw_cuname = base;
1953 debug(1, "CU name: %s\n", dw.dw_cuname);
1956 if ((child = die_child(&dw, cu)) != NULL)
1957 die_create(&dw, child);
1959 if ((rc = dwarf_next_cu_header(dw.dw_dw, &hdrlen, &vers, &abboff,
1960 &addrsz, &nxthdr, &dw.dw_err)) != DW_DLV_NO_ENTRY)
1961 terminate("multiple compilation units not supported\n");
1963 (void) dwarf_finish(&dw.dw_dw, &dw.dw_err);
1967 cvt_fixups(td, dw.dw_ptrsz);
1969 /* leak the dwarf_t */