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 * Copyright 2012 Jason King. All rights reserved.
27 * Use is subject to license terms.
31 * DWARF to tdata conversion
33 * For the most part, conversion is straightforward, proceeding in two passes.
34 * On the first pass, we iterate through every die, creating new type nodes as
35 * necessary. Referenced tdesc_t's are created in an uninitialized state, thus
36 * allowing type reference pointers to be filled in. If the tdesc_t
37 * corresponding to a given die can be completely filled out (sizes and offsets
38 * calculated, and so forth) without using any referenced types, the tdesc_t is
39 * marked as resolved. Consider an array type. If the type corresponding to
40 * the array contents has not yet been processed, we will create a blank tdesc
41 * for the contents type (only the type ID will be filled in, relying upon the
42 * later portion of the first pass to encounter and complete the referenced
43 * type). We will then attempt to determine the size of the array. If the
44 * array has a byte size attribute, we will have completely characterized the
45 * array type, and will be able to mark it as resolved. The lack of a byte
46 * size attribute, on the other hand, will prevent us from fully resolving the
47 * type, as the size will only be calculable with reference to the contents
48 * type, which has not, as yet, been encountered. The array type will thus be
49 * left without the resolved flag, and the first pass will continue.
51 * When we begin the second pass, we will have created tdesc_t nodes for every
52 * type in the section. We will traverse the tree, from the iidescs down,
53 * processing each unresolved node. As the referenced nodes will have been
54 * populated, the array type used in our example above will be able to use the
55 * size of the referenced types (if available) to determine its own type. The
56 * traversal will be repeated until all types have been resolved or we have
57 * failed to make progress. When all tdescs have been resolved, the conversion
60 * There are, as always, a few special cases that are handled during the first
63 * 1. Empty enums - GCC will occasionally emit an enum without any members.
64 * Later on in the file, it will emit the same enum type, though this time
65 * with the full complement of members. All references to the memberless
66 * enum need to be redirected to the full definition. During the first
67 * pass, each enum is entered in dm_enumhash, along with a pointer to its
68 * corresponding tdesc_t. If, during the second pass, we encounter a
69 * memberless enum, we use the hash to locate the full definition. All
70 * tdescs referencing the empty enum are then redirected.
72 * 2. Forward declarations - If the compiler sees a forward declaration for
73 * a structure, followed by the definition of that structure, it will emit
74 * DWARF data for both the forward declaration and the definition. We need
75 * to resolve the forward declarations when possible, by redirecting
76 * forward-referencing tdescs to the actual struct/union definitions. This
77 * redirection is done completely within the first pass. We begin by
78 * recording all forward declarations in dw_fwdhash. When we define a
79 * structure, we check to see if there have been any corresponding forward
80 * declarations. If so, we redirect the tdescs which referenced the forward
81 * declarations to the structure or union definition.
83 * XXX see if a post traverser will allow the elimination of repeated pass 2
96 #include "ctf_headers.h"
100 #include "traverse.h"
102 /* The version of DWARF which we support. */
103 #define DWARF_VERSION 2
106 * We need to define a couple of our own intrinsics, to smooth out some of the
107 * differences between the GCC and DevPro DWARF emitters. See the referenced
108 * routines and the special cases in the file comment for more details.
110 * Type IDs are 32 bits wide. We're going to use the top of that field to
111 * indicate types that we've created ourselves.
113 #define TID_FILEMAX 0x3fffffff /* highest tid from file */
114 #define TID_VOID 0x40000001 /* see die_void() */
115 #define TID_LONG 0x40000002 /* see die_array() */
117 #define TID_MFGTID_BASE 0x40000003 /* first mfg'd tid */
120 * To reduce the staggering amount of error-handling code that would otherwise
121 * be required, the attribute-retrieval routines handle most of their own
122 * errors. If the following flag is supplied as the value of the `req'
123 * argument, they will also handle the absence of a requested attribute by
124 * terminating the program.
126 #define DW_ATTR_REQ 1
128 #define TDESC_HASH_BUCKETS 511
130 typedef struct dwarf
{
131 Dwarf_Debug dw_dw
; /* for libdwarf */
132 Dwarf_Error dw_err
; /* for libdwarf */
133 Dwarf_Unsigned dw_maxoff
; /* highest legal offset in this cu */
134 tdata_t
*dw_td
; /* root of the tdesc/iidesc tree */
135 hash_t
*dw_tidhash
; /* hash of tdescs by t_id */
136 hash_t
*dw_fwdhash
; /* hash of fwd decls by name */
137 hash_t
*dw_enumhash
; /* hash of memberless enums by name */
138 tdesc_t
*dw_void
; /* manufactured void type */
139 tdesc_t
*dw_long
; /* manufactured long type for arrays */
140 size_t dw_ptrsz
; /* size of a pointer in this file */
141 tid_t dw_mfgtid_last
; /* last mfg'd type ID used */
142 uint_t dw_nunres
; /* count of unresolved types */
143 char *dw_cuname
; /* name of compilation unit */
146 static void die_create_one(dwarf_t
*, Dwarf_Die
);
147 static void die_create(dwarf_t
*, Dwarf_Die
);
150 mfgtid_next(dwarf_t
*dw
)
152 return (++dw
->dw_mfgtid_last
);
156 tdesc_add(dwarf_t
*dw
, tdesc_t
*tdp
)
158 hash_add(dw
->dw_tidhash
, tdp
);
162 tdesc_lookup(dwarf_t
*dw
, int tid
)
168 if (hash_find(dw
->dw_tidhash
, &tmpl
, (void **)&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_attr_form(dwarf_t
*dw
, Dwarf_Attribute attr
)
358 if (dwarf_whatform(attr
, &form
, &dw
->dw_err
) == DW_DLV_OK
)
361 terminate("failed to get attribute form for type: %s\n",
362 dwarf_errmsg(dw
->dw_err
));
368 * the following functions lookup the value of an attribute in a DIE:
375 * They all take the same parameters (with the exception of valp which is
376 * a pointer to the type of the attribute we are looking up):
378 * dw - the dwarf object to look in
379 * die - the DIE we're interested in
380 * name - the name of the attribute to lookup
381 * valp - pointer to where the value of the attribute is placed
382 * req - if the value is required (0 / non-zero)
384 * If the attribute is not found, one of the following happens:
385 * - program terminates (req is non-zero)
386 * - function returns 0
388 * If the value is found, and in a form (class) we can handle, the function
391 * Currently, we can only handle attribute values that are stored as
392 * constants (immediate value). If an attribute has a form we cannot
393 * handle (for example VLAs may store the dimensions of the array
394 * as a DWARF expression that can compute it at runtime by reading
395 * values off the stack or other locations in memory), it is treated
396 * the same as if the attribute does not exist.
399 die_signed(dwarf_t
*dw
, Dwarf_Die die
, Dwarf_Half name
, Dwarf_Signed
*valp
,
402 Dwarf_Attribute attr
;
405 if ((attr
= die_attr(dw
, die
, name
, req
)) == NULL
)
406 return (0); /* die_attr will terminate for us if necessary */
408 if (dwarf_formsdata(attr
, &val
, &dw
->dw_err
) != DW_DLV_OK
) {
412 terminate("die %llu: failed to get signed (form 0x%x)\n",
413 die_off(dw
, die
), die_attr_form(dw
, attr
));
416 dwarf_dealloc(dw
->dw_dw
, attr
, DW_DLA_ATTR
);
423 die_unsigned(dwarf_t
*dw
, Dwarf_Die die
, Dwarf_Half name
, Dwarf_Unsigned
*valp
,
426 Dwarf_Attribute attr
;
429 if ((attr
= die_attr(dw
, die
, name
, req
)) == NULL
)
430 return (0); /* die_attr will terminate for us if necessary */
432 if (dwarf_formudata(attr
, &val
, &dw
->dw_err
) != DW_DLV_OK
) {
436 terminate("die %llu: failed to get unsigned (form 0x%x)\n",
437 die_off(dw
, die
), die_attr_form(dw
, attr
));
440 dwarf_dealloc(dw
->dw_dw
, attr
, DW_DLA_ATTR
);
447 die_bool(dwarf_t
*dw
, Dwarf_Die die
, Dwarf_Half name
, Dwarf_Bool
*valp
, int req
)
449 Dwarf_Attribute attr
;
452 if ((attr
= die_attr(dw
, die
, name
, req
)) == NULL
)
453 return (0); /* die_attr will terminate for us if necessary */
455 if (dwarf_formflag(attr
, &val
, &dw
->dw_err
) != DW_DLV_OK
) {
459 terminate("die %llu: failed to get bool (form 0x%x)\n",
460 die_off(dw
, die
), die_attr_form(dw
, attr
));
463 dwarf_dealloc(dw
->dw_dw
, attr
, DW_DLA_ATTR
);
470 die_string(dwarf_t
*dw
, Dwarf_Die die
, Dwarf_Half name
, char **strp
, int req
)
472 Dwarf_Attribute attr
;
475 if ((attr
= die_attr(dw
, die
, name
, req
)) == NULL
)
476 return (0); /* die_attr will terminate for us if necessary */
478 if (dwarf_formstring(attr
, &str
, &dw
->dw_err
) != DW_DLV_OK
) {
482 terminate("die %llu: failed to get string (form 0x%x)\n",
483 die_off(dw
, die
), die_attr_form(dw
, attr
));
486 *strp
= xstrdup(str
);
487 dwarf_dealloc(dw
->dw_dw
, str
, DW_DLA_STRING
);
493 die_attr_ref(dwarf_t
*dw
, Dwarf_Die die
, Dwarf_Half name
)
495 Dwarf_Attribute attr
;
498 attr
= die_attr(dw
, die
, name
, DW_ATTR_REQ
);
500 if (dwarf_formref(attr
, &off
, &dw
->dw_err
) != DW_DLV_OK
) {
501 terminate("die %llu: failed to get ref (form 0x%x)\n",
502 die_off(dw
, die
), die_attr_form(dw
, attr
));
505 dwarf_dealloc(dw
->dw_dw
, attr
, DW_DLA_ATTR
);
511 die_name(dwarf_t
*dw
, Dwarf_Die die
)
515 (void) die_string(dw
, die
, DW_AT_name
, &str
, 0);
521 die_isdecl(dwarf_t
*dw
, Dwarf_Die die
)
525 return (die_bool(dw
, die
, DW_AT_declaration
, &val
, 0) && val
);
529 die_isglobal(dwarf_t
*dw
, Dwarf_Die die
)
535 * Some compilers (gcc) use DW_AT_external to indicate function
536 * visibility. Others (Sun) use DW_AT_visibility.
538 if (die_signed(dw
, die
, DW_AT_visibility
, &vis
, 0))
539 return (vis
== DW_VIS_exported
);
541 return (die_bool(dw
, die
, DW_AT_external
, &ext
, 0) && ext
);
545 die_add(dwarf_t
*dw
, Dwarf_Off off
)
547 tdesc_t
*tdp
= xcalloc(sizeof (tdesc_t
));
557 die_lookup_pass1(dwarf_t
*dw
, Dwarf_Die die
, Dwarf_Half name
)
559 Dwarf_Off ref
= die_attr_ref(dw
, die
, name
);
562 if ((tdp
= tdesc_lookup(dw
, ref
)) != NULL
)
565 return (die_add(dw
, ref
));
569 die_mem_offset(dwarf_t
*dw
, Dwarf_Die die
, Dwarf_Half name
,
570 Dwarf_Unsigned
*valp
, int req
)
572 Dwarf_Attribute attr
;
576 if ((attr
= die_attr(dw
, die
, name
, req
)) == NULL
)
577 return (0); /* die_attr will terminate for us if necessary */
579 if (dwarf_loclist(attr
, &loc
, &locnum
, &dw
->dw_err
) != DW_DLV_OK
) {
580 terminate("die %llu: failed to get mem offset location list\n",
584 dwarf_dealloc(dw
->dw_dw
, attr
, DW_DLA_ATTR
);
586 if (locnum
!= 1 || loc
->ld_s
->lr_atom
!= DW_OP_plus_uconst
) {
587 terminate("die %llu: cannot parse member offset\n",
591 *valp
= loc
->ld_s
->lr_number
;
593 dwarf_dealloc(dw
->dw_dw
, loc
->ld_s
, DW_DLA_LOC_BLOCK
);
594 dwarf_dealloc(dw
->dw_dw
, loc
, DW_DLA_LOCDESC
);
600 tdesc_intr_common(dwarf_t
*dw
, int tid
, const char *name
, size_t sz
)
605 intr
= xcalloc(sizeof (intr_t
));
606 intr
->intr_type
= INTR_INT
;
607 intr
->intr_signed
= 1;
608 intr
->intr_nbits
= sz
* NBBY
;
610 tdp
= xcalloc(sizeof (tdesc_t
));
611 tdp
->t_name
= xstrdup(name
);
614 tdp
->t_type
= INTRINSIC
;
616 tdp
->t_flags
= TDESC_F_RESOLVED
;
624 * Manufacture a void type. Used for gcc-emitted stabs, where the lack of a
625 * type reference implies a reference to a void type. A void *, for example
626 * will be represented by a pointer die without a DW_AT_type. CTF requires
627 * that pointer nodes point to something, so we'll create a void for use as
628 * the target. Note that the DWARF data may already create a void type. Ours
629 * would then be a duplicate, but it'll be removed in the self-uniquification
630 * merge performed at the completion of DWARF->tdesc conversion.
633 tdesc_intr_void(dwarf_t
*dw
)
635 if (dw
->dw_void
== NULL
)
636 dw
->dw_void
= tdesc_intr_common(dw
, TID_VOID
, "void", 0);
638 return (dw
->dw_void
);
642 tdesc_intr_long(dwarf_t
*dw
)
644 if (dw
->dw_long
== NULL
) {
645 dw
->dw_long
= tdesc_intr_common(dw
, TID_LONG
, "long",
649 return (dw
->dw_long
);
653 * Used for creating bitfield types. We create a copy of an existing intrinsic,
654 * adjusting the size of the copy to match what the caller requested. The
655 * caller can then use the copy as the type for a bitfield structure member.
658 tdesc_intr_clone(dwarf_t
*dw
, tdesc_t
*old
, size_t bitsz
)
660 tdesc_t
*new = xcalloc(sizeof (tdesc_t
));
662 if (!(old
->t_flags
& TDESC_F_RESOLVED
)) {
663 terminate("tdp %u: attempt to make a bit field from an "
664 "unresolved type\n", old
->t_id
);
667 new->t_name
= xstrdup(old
->t_name
);
668 new->t_size
= old
->t_size
;
669 new->t_id
= mfgtid_next(dw
);
670 new->t_type
= INTRINSIC
;
671 new->t_flags
= TDESC_F_RESOLVED
;
673 new->t_intr
= xcalloc(sizeof (intr_t
));
674 bcopy(old
->t_intr
, new->t_intr
, sizeof (intr_t
));
675 new->t_intr
->intr_nbits
= bitsz
;
683 tdesc_array_create(dwarf_t
*dw
, Dwarf_Die dim
, tdesc_t
*arrtdp
,
692 if ((dim2
= die_sibling(dw
, dim
)) == NULL
) {
694 } else if (die_tag(dw
, dim2
) == DW_TAG_subrange_type
) {
695 ctdp
= xcalloc(sizeof (tdesc_t
));
696 ctdp
->t_id
= mfgtid_next(dw
);
697 debug(3, "die %llu: creating new type %u for sub-dimension\n",
698 die_off(dw
, dim2
), ctdp
->t_id
);
699 tdesc_array_create(dw
, dim2
, arrtdp
, ctdp
);
701 terminate("die %llu: unexpected non-subrange node in array\n",
705 dimtdp
->t_type
= ARRAY
;
706 dimtdp
->t_ardef
= ar
= xcalloc(sizeof (ardef_t
));
709 * Array bounds can be signed or unsigned, but there are several kinds
710 * of signless forms (data1, data2, etc) that take their sign from the
711 * routine that is trying to interpret them. That is, data1 can be
712 * either signed or unsigned, depending on whether you use the signed or
713 * unsigned accessor function. GCC will use the signless forms to store
714 * unsigned values which have their high bit set, so we need to try to
715 * read them first as unsigned to get positive values. We could also
716 * try signed first, falling back to unsigned if we got a negative
719 if (die_unsigned(dw
, dim
, DW_AT_upper_bound
, &uval
, 0))
720 ar
->ad_nelems
= uval
+ 1;
721 else if (die_signed(dw
, dim
, DW_AT_upper_bound
, &sval
, 0))
722 ar
->ad_nelems
= sval
+ 1;
727 * Different compilers use different index types. Force the type to be
728 * a common, known value (long).
730 ar
->ad_idxtype
= tdesc_intr_long(dw
);
731 ar
->ad_contents
= ctdp
;
733 if (ar
->ad_contents
->t_size
!= 0) {
734 dimtdp
->t_size
= ar
->ad_contents
->t_size
* ar
->ad_nelems
;
735 dimtdp
->t_flags
|= TDESC_F_RESOLVED
;
740 * Create a tdesc from an array node. Some arrays will come with byte size
741 * attributes, and thus can be resolved immediately. Others don't, and will
742 * need to wait until the second pass for resolution.
745 die_array_create(dwarf_t
*dw
, Dwarf_Die arr
, Dwarf_Off off
, tdesc_t
*tdp
)
747 tdesc_t
*arrtdp
= die_lookup_pass1(dw
, arr
, DW_AT_type
);
751 debug(3, "die %llu: creating array\n", off
);
753 if ((dim
= die_child(dw
, arr
)) == NULL
||
754 die_tag(dw
, dim
) != DW_TAG_subrange_type
)
755 terminate("die %llu: failed to retrieve array bounds\n", off
);
757 tdesc_array_create(dw
, dim
, arrtdp
, tdp
);
759 if (die_unsigned(dw
, arr
, DW_AT_byte_size
, &uval
, 0)) {
766 * Ensure that sub-dimensions have sizes too before marking
769 flags
= TDESC_F_RESOLVED
;
770 for (dimtdp
= tdp
->t_ardef
->ad_contents
;
771 dimtdp
->t_type
== ARRAY
;
772 dimtdp
= dimtdp
->t_ardef
->ad_contents
) {
773 if (!(dimtdp
->t_flags
& TDESC_F_RESOLVED
)) {
779 tdp
->t_flags
|= flags
;
782 debug(3, "die %llu: array nelems %u size %u\n", off
,
783 tdp
->t_ardef
->ad_nelems
, tdp
->t_size
);
788 die_array_resolve(tdesc_t
*tdp
, tdesc_t
**tdpp
, void *private)
790 dwarf_t
*dw
= private;
793 if (tdp
->t_flags
& TDESC_F_RESOLVED
)
796 debug(3, "trying to resolve array %d (cont %d)\n", tdp
->t_id
,
797 tdp
->t_ardef
->ad_contents
->t_id
);
799 if ((sz
= tdesc_size(tdp
->t_ardef
->ad_contents
)) == 0) {
800 debug(3, "unable to resolve array %s (%d) contents %d\n",
801 tdesc_name(tdp
), tdp
->t_id
,
802 tdp
->t_ardef
->ad_contents
->t_id
);
808 tdp
->t_size
= sz
* tdp
->t_ardef
->ad_nelems
;
809 tdp
->t_flags
|= TDESC_F_RESOLVED
;
811 debug(3, "resolved array %d: %u bytes\n", tdp
->t_id
, tdp
->t_size
);
818 die_array_failed(tdesc_t
*tdp
, tdesc_t
**tdpp
, void *private)
820 tdesc_t
*cont
= tdp
->t_ardef
->ad_contents
;
822 if (tdp
->t_flags
& TDESC_F_RESOLVED
)
825 fprintf(stderr
, "Array %d: failed to size contents type %s (%d)\n",
826 tdp
->t_id
, tdesc_name(cont
), cont
->t_id
);
832 * Most enums (those with members) will be resolved during this first pass.
833 * Others - those without members (see the file comment) - won't be, and will
834 * need to wait until the second pass when they can be matched with their full
838 die_enum_create(dwarf_t
*dw
, Dwarf_Die die
, Dwarf_Off off
, tdesc_t
*tdp
)
844 debug(3, "die %llu: creating enum\n", off
);
848 (void) die_unsigned(dw
, die
, DW_AT_byte_size
, &uval
, DW_ATTR_REQ
);
851 if ((mem
= die_child(dw
, die
)) != NULL
) {
852 elist_t
**elastp
= &tdp
->t_emem
;
857 if (die_tag(dw
, mem
) != DW_TAG_enumerator
) {
858 /* Nested type declaration */
859 die_create_one(dw
, mem
);
863 el
= xcalloc(sizeof (elist_t
));
864 el
->el_name
= die_name(dw
, mem
);
866 if (die_signed(dw
, mem
, DW_AT_const_value
, &sval
, 0)) {
867 el
->el_number
= sval
;
868 } else if (die_unsigned(dw
, mem
, DW_AT_const_value
,
870 el
->el_number
= uval
;
872 terminate("die %llu: enum %llu: member without "
873 "value\n", off
, die_off(dw
, mem
));
876 debug(3, "die %llu: enum %llu: created %s = %d\n", off
,
877 die_off(dw
, mem
), el
->el_name
, el
->el_number
);
880 elastp
= &el
->el_next
;
882 } while ((mem
= die_sibling(dw
, mem
)) != NULL
);
884 hash_add(dw
->dw_enumhash
, tdp
);
886 tdp
->t_flags
|= TDESC_F_RESOLVED
;
888 if (tdp
->t_name
!= NULL
) {
889 iidesc_t
*ii
= xcalloc(sizeof (iidesc_t
));
890 ii
->ii_type
= II_SOU
;
891 ii
->ii_name
= xstrdup(tdp
->t_name
);
894 iidesc_add(dw
->dw_td
->td_iihash
, ii
);
900 die_enum_match(void *arg1
, void *arg2
)
902 tdesc_t
*tdp
= arg1
, **fullp
= arg2
;
904 if (tdp
->t_emem
!= NULL
) {
906 return (-1); /* stop the iteration */
914 die_enum_resolve(tdesc_t
*tdp
, tdesc_t
**tdpp
, void *private)
916 dwarf_t
*dw
= private;
917 tdesc_t
*full
= NULL
;
919 if (tdp
->t_flags
& TDESC_F_RESOLVED
)
922 (void) hash_find_iter(dw
->dw_enumhash
, tdp
, die_enum_match
, &full
);
925 * The answer to this one won't change from iteration to iteration,
929 terminate("tdp %u: enum %s has no members\n", tdp
->t_id
,
933 debug(3, "tdp %u: enum %s redirected to %u\n", tdp
->t_id
,
934 tdesc_name(tdp
), full
->t_id
);
936 tdp
->t_flags
|= TDESC_F_RESOLVED
;
942 die_fwd_map(void *arg1
, void *arg2
)
944 tdesc_t
*fwd
= arg1
, *sou
= arg2
;
946 debug(3, "tdp %u: mapped forward %s to sou %u\n", fwd
->t_id
,
947 tdesc_name(fwd
), sou
->t_id
);
954 * Structures and unions will never be resolved during the first pass, as we
955 * won't be able to fully determine the member sizes. The second pass, which
956 * have access to sizing information, will be able to complete the resolution.
959 die_sou_create(dwarf_t
*dw
, Dwarf_Die str
, Dwarf_Off off
, tdesc_t
*tdp
,
960 int type
, const char *typename
)
962 Dwarf_Unsigned sz
, bitsz
, bitoff
;
964 mlist_t
*ml
, **mlastp
;
967 tdp
->t_type
= (die_isdecl(dw
, str
) ? FORWARD
: type
);
969 debug(3, "die %llu: creating %s %s\n", off
,
970 (tdp
->t_type
== FORWARD
? "forward decl" : typename
),
973 if (tdp
->t_type
== FORWARD
) {
974 hash_add(dw
->dw_fwdhash
, tdp
);
978 (void) hash_find_iter(dw
->dw_fwdhash
, tdp
, die_fwd_map
, tdp
);
980 (void) die_unsigned(dw
, str
, DW_AT_byte_size
, &sz
, DW_ATTR_REQ
);
984 * GCC allows empty SOUs as an extension.
986 if ((mem
= die_child(dw
, str
)) == NULL
)
989 mlastp
= &tdp
->t_members
;
992 Dwarf_Off memoff
= die_off(dw
, mem
);
993 Dwarf_Half tag
= die_tag(dw
, mem
);
994 Dwarf_Unsigned mloff
;
996 if (tag
!= DW_TAG_member
) {
997 /* Nested type declaration */
998 die_create_one(dw
, mem
);
1002 debug(3, "die %llu: mem %llu: creating member\n", off
, memoff
);
1004 ml
= xcalloc(sizeof (mlist_t
));
1007 * This could be a GCC anon struct/union member, so we'll allow
1008 * an empty name, even though nothing can really handle them
1009 * properly. Note that some versions of GCC miss out debug
1010 * info for anon structs, though recent versions are fixed (gcc
1013 if ((ml
->ml_name
= die_name(dw
, mem
)) == NULL
)
1016 ml
->ml_type
= die_lookup_pass1(dw
, mem
, DW_AT_type
);
1018 if (die_mem_offset(dw
, mem
, DW_AT_data_member_location
,
1020 debug(3, "die %llu: got mloff %llx\n", off
,
1021 (u_longlong_t
)mloff
);
1022 ml
->ml_offset
= mloff
* 8;
1025 if (die_unsigned(dw
, mem
, DW_AT_bit_size
, &bitsz
, 0))
1026 ml
->ml_size
= bitsz
;
1028 ml
->ml_size
= tdesc_bitsize(ml
->ml_type
);
1030 if (die_unsigned(dw
, mem
, DW_AT_bit_offset
, &bitoff
, 0)) {
1032 ml
->ml_offset
+= bitoff
;
1034 ml
->ml_offset
+= tdesc_bitsize(ml
->ml_type
) - bitoff
-
1039 debug(3, "die %llu: mem %llu: created \"%s\" (off %u sz %u)\n",
1040 off
, memoff
, ml
->ml_name
, ml
->ml_offset
, ml
->ml_size
);
1043 mlastp
= &ml
->ml_next
;
1044 } while ((mem
= die_sibling(dw
, mem
)) != NULL
);
1047 * GCC will attempt to eliminate unused types, thus decreasing the
1048 * size of the emitted dwarf. That is, if you declare a foo_t in your
1049 * header, include said header in your source file, and neglect to
1050 * actually use (directly or indirectly) the foo_t in the source file,
1051 * the foo_t won't make it into the emitted DWARF. So, at least, goes
1054 * Occasionally, it'll emit the DW_TAG_structure_type for the foo_t,
1055 * and then neglect to emit the members. Strangely, the loner struct
1056 * tag will always be followed by a proper nested declaration of
1057 * something else. This is clearly a bug, but we're not going to have
1058 * time to get it fixed before this goo goes back, so we'll have to work
1059 * around it. If we see a no-membered struct with a nested declaration
1060 * (i.e. die_child of the struct tag won't be null), we'll ignore it.
1061 * Being paranoid, we won't simply remove it from the hash. Instead,
1062 * we'll decline to create an iidesc for it, thus ensuring that this
1063 * type won't make it into the output file. To be safe, we'll also
1066 if (tdp
->t_members
== NULL
) {
1067 const char *old
= tdesc_name(tdp
);
1068 size_t newsz
= 7 + strlen(old
) + 1;
1069 char *new = xmalloc(newsz
);
1070 (void) snprintf(new, newsz
, "orphan %s", old
);
1072 debug(3, "die %llu: worked around %s %s\n", off
, typename
, old
);
1074 if (tdp
->t_name
!= NULL
)
1081 if (tdp
->t_name
!= NULL
) {
1082 ii
= xcalloc(sizeof (iidesc_t
));
1083 ii
->ii_type
= II_SOU
;
1084 ii
->ii_name
= xstrdup(tdp
->t_name
);
1087 iidesc_add(dw
->dw_td
->td_iihash
, ii
);
1092 die_struct_create(dwarf_t
*dw
, Dwarf_Die die
, Dwarf_Off off
, tdesc_t
*tdp
)
1094 die_sou_create(dw
, die
, off
, tdp
, STRUCT
, "struct");
1098 die_union_create(dwarf_t
*dw
, Dwarf_Die die
, Dwarf_Off off
, tdesc_t
*tdp
)
1100 die_sou_create(dw
, die
, off
, tdp
, UNION
, "union");
1105 die_sou_resolve(tdesc_t
*tdp
, tdesc_t
**tdpp
, void *private)
1107 dwarf_t
*dw
= private;
1111 if (tdp
->t_flags
& TDESC_F_RESOLVED
)
1114 debug(3, "resolving sou %s\n", tdesc_name(tdp
));
1116 for (ml
= tdp
->t_members
; ml
!= NULL
; ml
= ml
->ml_next
) {
1117 if (ml
->ml_size
== 0) {
1118 mt
= tdesc_basetype(ml
->ml_type
);
1120 if ((ml
->ml_size
= tdesc_bitsize(mt
)) != 0)
1124 * For empty members, or GCC/C99 flexible array
1125 * members, a size of 0 is correct.
1127 if (mt
->t_members
== NULL
)
1129 if (mt
->t_type
== ARRAY
&& mt
->t_ardef
->ad_nelems
== 0)
1136 if ((mt
= tdesc_basetype(ml
->ml_type
)) == NULL
) {
1141 if (ml
->ml_size
!= 0 && mt
->t_type
== INTRINSIC
&&
1142 mt
->t_intr
->intr_nbits
!= ml
->ml_size
) {
1144 * This member is a bitfield, and needs to reference
1145 * an intrinsic type with the same width. If the
1146 * currently-referenced type isn't of the same width,
1147 * we'll copy it, adjusting the width of the copy to
1148 * the size we'd like.
1150 debug(3, "tdp %u: creating bitfield for %d bits\n",
1151 tdp
->t_id
, ml
->ml_size
);
1153 ml
->ml_type
= tdesc_intr_clone(dw
, mt
, ml
->ml_size
);
1157 tdp
->t_flags
|= TDESC_F_RESOLVED
;
1164 die_sou_failed(tdesc_t
*tdp
, tdesc_t
**tdpp
, void *private)
1166 const char *typename
= (tdp
->t_type
== STRUCT
? "struct" : "union");
1169 if (tdp
->t_flags
& TDESC_F_RESOLVED
)
1172 for (ml
= tdp
->t_members
; ml
!= NULL
; ml
= ml
->ml_next
) {
1173 if (ml
->ml_size
== 0) {
1174 fprintf(stderr
, "%s %d: failed to size member \"%s\" "
1175 "of type %s (%d)\n", typename
, tdp
->t_id
,
1176 ml
->ml_name
, tdesc_name(ml
->ml_type
),
1185 die_funcptr_create(dwarf_t
*dw
, Dwarf_Die die
, Dwarf_Off off
, tdesc_t
*tdp
)
1187 Dwarf_Attribute attr
;
1193 debug(3, "die %llu: creating function pointer\n", off
);
1196 * We'll begin by processing any type definition nodes that may be
1197 * lurking underneath this one.
1199 for (arg
= die_child(dw
, die
); arg
!= NULL
;
1200 arg
= die_sibling(dw
, arg
)) {
1201 if ((tag
= die_tag(dw
, arg
)) != DW_TAG_formal_parameter
&&
1202 tag
!= DW_TAG_unspecified_parameters
) {
1203 /* Nested type declaration */
1204 die_create_one(dw
, arg
);
1208 if (die_isdecl(dw
, die
)) {
1210 * This is a prototype. We don't add prototypes to the
1211 * tree, so we're going to drop the tdesc. Unfortunately,
1212 * it has already been added to the tree. Nobody will reference
1213 * it, though, and it will be leaked.
1218 fn
= xcalloc(sizeof (fndef_t
));
1220 tdp
->t_type
= FUNCTION
;
1222 if ((attr
= die_attr(dw
, die
, DW_AT_type
, 0)) != NULL
) {
1223 dwarf_dealloc(dw
->dw_dw
, attr
, DW_DLA_ATTR
);
1224 fn
->fn_ret
= die_lookup_pass1(dw
, die
, DW_AT_type
);
1226 fn
->fn_ret
= tdesc_intr_void(dw
);
1230 * Count the arguments to the function, then read them in.
1232 for (fn
->fn_nargs
= 0, arg
= die_child(dw
, die
); arg
!= NULL
;
1233 arg
= die_sibling(dw
, arg
)) {
1234 if ((tag
= die_tag(dw
, arg
)) == DW_TAG_formal_parameter
)
1236 else if (tag
== DW_TAG_unspecified_parameters
&&
1241 if (fn
->fn_nargs
!= 0) {
1242 debug(3, "die %llu: adding %d argument%s\n", off
, fn
->fn_nargs
,
1243 (fn
->fn_nargs
> 1 ? "s" : ""));
1245 fn
->fn_args
= xcalloc(sizeof (tdesc_t
*) * fn
->fn_nargs
);
1246 for (i
= 0, arg
= die_child(dw
, die
);
1247 arg
!= NULL
&& i
< fn
->fn_nargs
;
1248 arg
= die_sibling(dw
, arg
)) {
1249 if (die_tag(dw
, arg
) != DW_TAG_formal_parameter
)
1252 fn
->fn_args
[i
++] = die_lookup_pass1(dw
, arg
,
1258 tdp
->t_flags
|= TDESC_F_RESOLVED
;
1262 * GCC and DevPro use different names for the base types. While the terms are
1263 * the same, they are arranged in a different order. Some terms, such as int,
1264 * are implied in one, and explicitly named in the other. Given a base type
1265 * as input, this routine will return a common name, along with an intr_t
1266 * that reflects said name.
1269 die_base_name_parse(const char *name
, char **newp
)
1273 int nlong
= 0, nshort
= 0, nchar
= 0, nint
= 0;
1278 if (strlen(name
) > sizeof (buf
) - 1)
1279 terminate("base type name \"%s\" is too long\n", name
);
1281 strncpy(buf
, name
, sizeof (buf
));
1283 for (c
= strtok(buf
, " "); c
!= NULL
; c
= strtok(NULL
, " ")) {
1284 if (strcmp(c
, "signed") == 0)
1286 else if (strcmp(c
, "unsigned") == 0)
1288 else if (strcmp(c
, "long") == 0)
1290 else if (strcmp(c
, "char") == 0) {
1293 } else if (strcmp(c
, "short") == 0)
1295 else if (strcmp(c
, "int") == 0)
1299 * If we don't recognize any of the tokens, we'll tell
1300 * the caller to fall back to the dwarf-provided
1301 * encoding information.
1307 if (nchar
> 1 || nshort
> 1 || nint
> 1 || nlong
> 2)
1311 if (nlong
> 0 || nshort
> 0 || nint
> 0)
1316 } else if (nshort
> 0) {
1322 } else if (nlong
> 0) {
1329 intr
= xcalloc(sizeof (intr_t
));
1330 intr
->intr_type
= INTR_INT
;
1331 intr
->intr_signed
= sign
;
1332 intr
->intr_iformat
= fmt
;
1334 snprintf(buf
, sizeof (buf
), "%s%s%s",
1335 (sign
? "" : "unsigned "),
1336 (nlong
> 1 ? "long " : ""),
1339 *newp
= xstrdup(buf
);
1343 typedef struct fp_size_map
{
1344 size_t fsm_typesz
[2]; /* size of {32,64} type */
1345 uint_t fsm_enc
[3]; /* CTF_FP_* for {bare,cplx,imagry} type */
1348 static const fp_size_map_t fp_encodings
[] = {
1349 { { 4, 4 }, { CTF_FP_SINGLE
, CTF_FP_CPLX
, CTF_FP_IMAGRY
} },
1350 { { 8, 8 }, { CTF_FP_DOUBLE
, CTF_FP_DCPLX
, CTF_FP_DIMAGRY
} },
1352 { { 16, 16 }, { CTF_FP_LDOUBLE
, CTF_FP_LDCPLX
, CTF_FP_LDIMAGRY
} },
1354 { { 12, 16 }, { CTF_FP_LDOUBLE
, CTF_FP_LDCPLX
, CTF_FP_LDIMAGRY
} },
1360 die_base_type2enc(dwarf_t
*dw
, Dwarf_Off off
, Dwarf_Signed enc
, size_t sz
)
1362 const fp_size_map_t
*map
= fp_encodings
;
1363 uint_t szidx
= dw
->dw_ptrsz
== sizeof (uint64_t);
1364 uint_t mult
= 1, col
= 0;
1366 if (enc
== DW_ATE_complex_float
) {
1369 } else if (enc
== DW_ATE_imaginary_float
||
1370 enc
== DW_ATE_SUN_imaginary_float
)
1373 while (map
->fsm_typesz
[szidx
] != 0) {
1374 if (map
->fsm_typesz
[szidx
] * mult
== sz
)
1375 return (map
->fsm_enc
[col
]);
1379 terminate("die %llu: unrecognized real type size %u\n", off
, sz
);
1385 die_base_from_dwarf(dwarf_t
*dw
, Dwarf_Die base
, Dwarf_Off off
, size_t sz
)
1387 intr_t
*intr
= xcalloc(sizeof (intr_t
));
1390 (void) die_signed(dw
, base
, DW_AT_encoding
, &enc
, DW_ATTR_REQ
);
1393 case DW_ATE_unsigned
:
1394 case DW_ATE_address
:
1395 intr
->intr_type
= INTR_INT
;
1397 case DW_ATE_unsigned_char
:
1398 intr
->intr_type
= INTR_INT
;
1399 intr
->intr_iformat
= 'c';
1402 intr
->intr_type
= INTR_INT
;
1403 intr
->intr_signed
= 1;
1405 case DW_ATE_signed_char
:
1406 intr
->intr_type
= INTR_INT
;
1407 intr
->intr_signed
= 1;
1408 intr
->intr_iformat
= 'c';
1410 case DW_ATE_boolean
:
1411 intr
->intr_type
= INTR_INT
;
1412 intr
->intr_signed
= 1;
1413 intr
->intr_iformat
= 'b';
1416 case DW_ATE_complex_float
:
1417 case DW_ATE_imaginary_float
:
1418 case DW_ATE_SUN_imaginary_float
:
1419 case DW_ATE_SUN_interval_float
:
1420 intr
->intr_type
= INTR_REAL
;
1421 intr
->intr_signed
= 1;
1422 intr
->intr_fformat
= die_base_type2enc(dw
, off
, enc
, sz
);
1425 terminate("die %llu: unknown base type encoding 0x%llx\n",
1433 die_base_create(dwarf_t
*dw
, Dwarf_Die base
, Dwarf_Off off
, tdesc_t
*tdp
)
1439 debug(3, "die %llu: creating base type\n", off
);
1442 * The compilers have their own clever (internally inconsistent) ideas
1443 * as to what base types should look like. Some times gcc will, for
1444 * example, use DW_ATE_signed_char for char. Other times, however, it
1445 * will use DW_ATE_signed. Needless to say, this causes some problems
1446 * down the road, particularly with merging. We do, however, use the
1447 * DWARF idea of type sizes, as this allows us to avoid caring about
1450 (void) die_unsigned(dw
, base
, DW_AT_byte_size
, &sz
, DW_ATTR_REQ
);
1452 if (tdp
->t_name
== NULL
)
1453 terminate("die %llu: base type without name\n", off
);
1455 /* XXX make a name parser for float too */
1456 if ((intr
= die_base_name_parse(tdp
->t_name
, &new)) != NULL
) {
1457 /* Found it. We'll use the parsed version */
1458 debug(3, "die %llu: name \"%s\" remapped to \"%s\"\n", off
,
1459 tdesc_name(tdp
), new);
1465 * We didn't recognize the type, so we'll create an intr_t
1466 * based on the DWARF data.
1468 debug(3, "die %llu: using dwarf data for base \"%s\"\n", off
,
1471 intr
= die_base_from_dwarf(dw
, base
, off
, sz
);
1474 intr
->intr_nbits
= sz
* 8;
1476 tdp
->t_type
= INTRINSIC
;
1480 tdp
->t_flags
|= TDESC_F_RESOLVED
;
1484 die_through_create(dwarf_t
*dw
, Dwarf_Die die
, Dwarf_Off off
, tdesc_t
*tdp
,
1485 int type
, const char *typename
)
1487 Dwarf_Attribute attr
;
1489 debug(3, "die %llu: creating %s\n", off
, typename
);
1493 if ((attr
= die_attr(dw
, die
, DW_AT_type
, 0)) != NULL
) {
1494 dwarf_dealloc(dw
->dw_dw
, attr
, DW_DLA_ATTR
);
1495 tdp
->t_tdesc
= die_lookup_pass1(dw
, die
, DW_AT_type
);
1497 tdp
->t_tdesc
= tdesc_intr_void(dw
);
1500 if (type
== POINTER
)
1501 tdp
->t_size
= dw
->dw_ptrsz
;
1503 tdp
->t_flags
|= TDESC_F_RESOLVED
;
1505 if (type
== TYPEDEF
) {
1506 iidesc_t
*ii
= xcalloc(sizeof (iidesc_t
));
1507 ii
->ii_type
= II_TYPE
;
1508 ii
->ii_name
= xstrdup(tdp
->t_name
);
1511 iidesc_add(dw
->dw_td
->td_iihash
, ii
);
1516 die_typedef_create(dwarf_t
*dw
, Dwarf_Die die
, Dwarf_Off off
, tdesc_t
*tdp
)
1518 die_through_create(dw
, die
, off
, tdp
, TYPEDEF
, "typedef");
1522 die_const_create(dwarf_t
*dw
, Dwarf_Die die
, Dwarf_Off off
, tdesc_t
*tdp
)
1524 die_through_create(dw
, die
, off
, tdp
, CONST
, "const");
1528 die_pointer_create(dwarf_t
*dw
, Dwarf_Die die
, Dwarf_Off off
, tdesc_t
*tdp
)
1530 die_through_create(dw
, die
, off
, tdp
, POINTER
, "pointer");
1534 die_restrict_create(dwarf_t
*dw
, Dwarf_Die die
, Dwarf_Off off
, tdesc_t
*tdp
)
1536 die_through_create(dw
, die
, off
, tdp
, RESTRICT
, "restrict");
1540 die_volatile_create(dwarf_t
*dw
, Dwarf_Die die
, Dwarf_Off off
, tdesc_t
*tdp
)
1542 die_through_create(dw
, die
, off
, tdp
, VOLATILE
, "volatile");
1547 die_function_create(dwarf_t
*dw
, Dwarf_Die die
, Dwarf_Off off
, tdesc_t
*tdp
)
1554 debug(3, "die %llu: creating function definition\n", off
);
1557 * We'll begin by processing any type definition nodes that may be
1558 * lurking underneath this one.
1560 for (arg
= die_child(dw
, die
); arg
!= NULL
;
1561 arg
= die_sibling(dw
, arg
)) {
1562 if ((tag
= die_tag(dw
, arg
)) != DW_TAG_formal_parameter
&&
1563 tag
!= DW_TAG_variable
) {
1564 /* Nested type declaration */
1565 die_create_one(dw
, arg
);
1569 if (die_isdecl(dw
, die
) || (name
= die_name(dw
, die
)) == NULL
) {
1571 * We process neither prototypes nor subprograms without
1577 ii
= xcalloc(sizeof (iidesc_t
));
1578 ii
->ii_type
= die_isglobal(dw
, die
) ? II_GFUN
: II_SFUN
;
1580 if (ii
->ii_type
== II_SFUN
)
1581 ii
->ii_owner
= xstrdup(dw
->dw_cuname
);
1583 debug(3, "die %llu: function %s is %s\n", off
, ii
->ii_name
,
1584 (ii
->ii_type
== II_GFUN
? "global" : "static"));
1586 if (die_attr(dw
, die
, DW_AT_type
, 0) != NULL
)
1587 ii
->ii_dtype
= die_lookup_pass1(dw
, die
, DW_AT_type
);
1589 ii
->ii_dtype
= tdesc_intr_void(dw
);
1591 for (arg
= die_child(dw
, die
); arg
!= NULL
;
1592 arg
= die_sibling(dw
, arg
)) {
1595 debug(3, "die %llu: looking at sub member at %llu\n",
1596 off
, die_off(dw
, die
));
1598 if (die_tag(dw
, arg
) != DW_TAG_formal_parameter
)
1601 if ((name
= die_name(dw
, arg
)) == NULL
) {
1602 terminate("die %llu: func arg %d has no name\n",
1603 off
, ii
->ii_nargs
+ 1);
1606 if (strcmp(name
, "...") == 0) {
1615 if (ii
->ii_nargs
> 0) {
1618 debug(3, "die %llu: function has %d argument%s\n", off
,
1619 ii
->ii_nargs
, (ii
->ii_nargs
== 1 ? "" : "s"));
1621 ii
->ii_args
= xcalloc(sizeof (tdesc_t
) * ii
->ii_nargs
);
1623 for (arg
= die_child(dw
, die
), i
= 0;
1624 arg
!= NULL
&& i
< ii
->ii_nargs
;
1625 arg
= die_sibling(dw
, arg
)) {
1626 if (die_tag(dw
, arg
) != DW_TAG_formal_parameter
)
1629 ii
->ii_args
[i
++] = die_lookup_pass1(dw
, arg
,
1634 iidesc_add(dw
->dw_td
->td_iihash
, ii
);
1639 die_variable_create(dwarf_t
*dw
, Dwarf_Die die
, Dwarf_Off off
, tdesc_t
*tdp
)
1644 debug(3, "die %llu: creating object definition\n", off
);
1646 if (die_isdecl(dw
, die
) || (name
= die_name(dw
, die
)) == NULL
)
1647 return; /* skip prototypes and nameless objects */
1649 ii
= xcalloc(sizeof (iidesc_t
));
1650 ii
->ii_type
= die_isglobal(dw
, die
) ? II_GVAR
: II_SVAR
;
1652 ii
->ii_dtype
= die_lookup_pass1(dw
, die
, DW_AT_type
);
1653 if (ii
->ii_type
== II_SVAR
)
1654 ii
->ii_owner
= xstrdup(dw
->dw_cuname
);
1656 iidesc_add(dw
->dw_td
->td_iihash
, ii
);
1661 die_fwd_resolve(tdesc_t
*fwd
, tdesc_t
**fwdp
, void *private)
1663 if (fwd
->t_flags
& TDESC_F_RESOLVED
)
1666 if (fwd
->t_tdesc
!= NULL
) {
1667 debug(3, "tdp %u: unforwarded %s\n", fwd
->t_id
,
1669 *fwdp
= fwd
->t_tdesc
;
1672 fwd
->t_flags
|= TDESC_F_RESOLVED
;
1679 die_lexblk_descend(dwarf_t
*dw
, Dwarf_Die die
, Dwarf_Off off
, tdesc_t
*tdp
)
1681 Dwarf_Die child
= die_child(dw
, die
);
1684 die_create(dw
, child
);
1688 * Used to map the die to a routine which can parse it, using the tag to do the
1689 * mapping. While the processing of most tags entails the creation of a tdesc,
1690 * there are a few which don't - primarily those which result in the creation of
1691 * iidescs which refer to existing tdescs.
1694 #define DW_F_NOTDP 0x1 /* Don't create a tdesc for the creator */
1696 typedef struct die_creator
{
1699 void (*dc_create
)(dwarf_t
*, Dwarf_Die
, Dwarf_Off
, tdesc_t
*);
1702 static const die_creator_t die_creators
[] = {
1703 { DW_TAG_array_type
, 0, die_array_create
},
1704 { DW_TAG_enumeration_type
, 0, die_enum_create
},
1705 { DW_TAG_lexical_block
, DW_F_NOTDP
, die_lexblk_descend
},
1706 { DW_TAG_pointer_type
, 0, die_pointer_create
},
1707 { DW_TAG_structure_type
, 0, die_struct_create
},
1708 { DW_TAG_subroutine_type
, 0, die_funcptr_create
},
1709 { DW_TAG_typedef
, 0, die_typedef_create
},
1710 { DW_TAG_union_type
, 0, die_union_create
},
1711 { DW_TAG_base_type
, 0, die_base_create
},
1712 { DW_TAG_const_type
, 0, die_const_create
},
1713 { DW_TAG_subprogram
, DW_F_NOTDP
, die_function_create
},
1714 { DW_TAG_variable
, DW_F_NOTDP
, die_variable_create
},
1715 { DW_TAG_volatile_type
, 0, die_volatile_create
},
1716 { DW_TAG_restrict_type
, 0, die_restrict_create
},
1720 static const die_creator_t
*
1721 die_tag2ctor(Dwarf_Half tag
)
1723 const die_creator_t
*dc
;
1725 for (dc
= die_creators
; dc
->dc_create
!= NULL
; dc
++) {
1726 if (dc
->dc_tag
== tag
)
1734 die_create_one(dwarf_t
*dw
, Dwarf_Die die
)
1736 Dwarf_Off off
= die_off(dw
, die
);
1737 const die_creator_t
*dc
;
1741 debug(3, "die %llu: create_one\n", off
);
1743 if (off
> dw
->dw_maxoff
) {
1744 terminate("illegal die offset %llu (max %llu)\n", off
,
1748 tag
= die_tag(dw
, die
);
1750 if ((dc
= die_tag2ctor(tag
)) == NULL
) {
1751 debug(2, "die %llu: ignoring tag type %x\n", off
, tag
);
1755 if ((tdp
= tdesc_lookup(dw
, off
)) == NULL
&&
1756 !(dc
->dc_flags
& DW_F_NOTDP
)) {
1757 tdp
= xcalloc(sizeof (tdesc_t
));
1763 tdp
->t_name
= die_name(dw
, die
);
1765 dc
->dc_create(dw
, die
, off
, tdp
);
1769 die_create(dwarf_t
*dw
, Dwarf_Die die
)
1772 die_create_one(dw
, die
);
1773 } while ((die
= die_sibling(dw
, die
)) != NULL
);
1776 static tdtrav_cb_f die_resolvers
[] = {
1778 NULL
, /* intrinsic */
1780 die_array_resolve
, /* array */
1781 NULL
, /* function */
1782 die_sou_resolve
, /* struct */
1783 die_sou_resolve
, /* union */
1784 die_enum_resolve
, /* enum */
1785 die_fwd_resolve
, /* forward */
1787 NULL
, /* typedef unres */
1788 NULL
, /* volatile */
1790 NULL
, /* restrict */
1793 static tdtrav_cb_f die_fail_reporters
[] = {
1795 NULL
, /* intrinsic */
1797 die_array_failed
, /* array */
1798 NULL
, /* function */
1799 die_sou_failed
, /* struct */
1800 die_sou_failed
, /* union */
1804 NULL
, /* typedef unres */
1805 NULL
, /* volatile */
1807 NULL
, /* restrict */
1811 die_resolve(dwarf_t
*dw
)
1820 (void) iitraverse_hash(dw
->dw_td
->td_iihash
,
1821 &dw
->dw_td
->td_curvgen
, NULL
, NULL
, die_resolvers
, dw
);
1823 debug(3, "resolve: pass %d, %u left\n", pass
, dw
->dw_nunres
);
1825 if (dw
->dw_nunres
== last
) {
1826 fprintf(stderr
, "%s: failed to resolve the following "
1827 "types:\n", progname
);
1829 (void) iitraverse_hash(dw
->dw_td
->td_iihash
,
1830 &dw
->dw_td
->td_curvgen
, NULL
, NULL
,
1831 die_fail_reporters
, dw
);
1833 terminate("failed to resolve types\n");
1836 last
= dw
->dw_nunres
;
1838 } while (dw
->dw_nunres
!= 0);
1842 * Any object containing a function or object symbol at any scope should also
1843 * contain DWARF data.
1846 should_have_dwarf(Elf
*elf
)
1848 Elf_Scn
*scn
= NULL
;
1849 Elf_Data
*data
= NULL
;
1854 boolean_t found
= B_FALSE
;
1856 while ((scn
= elf_nextscn(elf
, scn
)) != NULL
) {
1857 gelf_getshdr(scn
, &shdr
);
1859 if (shdr
.sh_type
== SHT_SYMTAB
) {
1866 terminate("cannot convert stripped objects\n");
1868 data
= elf_getdata(scn
, NULL
);
1869 nsyms
= shdr
.sh_size
/ shdr
.sh_entsize
;
1871 for (symdx
= 0; symdx
< nsyms
; symdx
++) {
1872 gelf_getsym(data
, symdx
, &sym
);
1874 if ((GELF_ST_TYPE(sym
.st_info
) == STT_FUNC
) ||
1875 (GELF_ST_TYPE(sym
.st_info
) == STT_TLS
) ||
1876 (GELF_ST_TYPE(sym
.st_info
) == STT_OBJECT
)) {
1879 name
= elf_strptr(elf
, shdr
.sh_link
, sym
.st_name
);
1881 /* Studio emits these local symbols regardless */
1882 if ((strcmp(name
, "Bbss.bss") != 0) &&
1883 (strcmp(name
, "Ttbss.bss") != 0) &&
1884 (strcmp(name
, "Ddata.data") != 0) &&
1885 (strcmp(name
, "Ttdata.data") != 0) &&
1886 (strcmp(name
, "Drodata.rodata") != 0))
1896 dw_read(tdata_t
*td
, Elf
*elf
, const char *filename
)
1898 Dwarf_Unsigned abboff
, hdrlen
, nxthdr
;
1899 Dwarf_Half vers
, addrsz
;
1900 Dwarf_Die cu
, child
;
1905 bzero(&dw
, sizeof (dwarf_t
));
1907 dw
.dw_ptrsz
= elf_ptrsz(elf
);
1908 dw
.dw_mfgtid_last
= TID_MFGTID_BASE
;
1909 dw
.dw_tidhash
= hash_new(TDESC_HASH_BUCKETS
, tdesc_idhash
, tdesc_idcmp
);
1910 dw
.dw_fwdhash
= hash_new(TDESC_HASH_BUCKETS
, tdesc_namehash
,
1912 dw
.dw_enumhash
= hash_new(TDESC_HASH_BUCKETS
, tdesc_namehash
,
1915 if ((rc
= dwarf_elf_init(elf
, DW_DLC_READ
, NULL
, NULL
, &dw
.dw_dw
,
1916 &dw
.dw_err
)) == DW_DLV_NO_ENTRY
) {
1917 if (should_have_dwarf(elf
)) {
1923 } else if (rc
!= DW_DLV_OK
) {
1924 if (dwarf_errno(dw
.dw_err
) == DW_DLE_DEBUG_INFO_NULL
) {
1926 * There's no type data in the DWARF section, but
1927 * libdwarf is too clever to handle that properly.
1932 terminate("failed to initialize DWARF: %s\n",
1933 dwarf_errmsg(dw
.dw_err
));
1936 if ((rc
= dwarf_next_cu_header(dw
.dw_dw
, &hdrlen
, &vers
, &abboff
,
1937 &addrsz
, &nxthdr
, &dw
.dw_err
)) != DW_DLV_OK
)
1938 terminate("file does not contain valid DWARF data: %s\n",
1939 dwarf_errmsg(dw
.dw_err
));
1942 * Some compilers emit no DWARF for empty files, others emit an empty
1945 if ((cu
= die_sibling(&dw
, NULL
)) == NULL
||
1946 ((child
= die_child(&dw
, cu
)) == NULL
) &&
1947 should_have_dwarf(elf
)) {
1948 terminate("file does not contain dwarf type data "
1949 "(try compiling with -g)\n");
1950 } else if (child
== NULL
) {
1954 dw
.dw_maxoff
= nxthdr
- 1;
1956 if (dw
.dw_maxoff
> TID_FILEMAX
)
1957 terminate("file contains too many types\n");
1959 debug(1, "DWARF version: %d\n", vers
);
1960 if (vers
!= DWARF_VERSION
) {
1961 terminate("file contains incompatible version %d DWARF code "
1962 "(version 2 required)\n", vers
);
1965 if (die_string(&dw
, cu
, DW_AT_producer
, &prod
, 0)) {
1966 debug(1, "DWARF emitter: %s\n", prod
);
1970 if ((dw
.dw_cuname
= die_name(&dw
, cu
)) != NULL
) {
1971 char *base
= xstrdup(basename(dw
.dw_cuname
));
1973 dw
.dw_cuname
= base
;
1975 debug(1, "CU name: %s\n", dw
.dw_cuname
);
1978 die_create(&dw
, child
);
1980 if ((rc
= dwarf_next_cu_header(dw
.dw_dw
, &hdrlen
, &vers
, &abboff
,
1981 &addrsz
, &nxthdr
, &dw
.dw_err
)) != DW_DLV_NO_ENTRY
)
1982 terminate("multiple compilation units not supported\n");
1984 (void) dwarf_finish(dw
.dw_dw
, &dw
.dw_err
);
1988 cvt_fixups(td
, dw
.dw_ptrsz
);
1990 /* leak the dwarf_t */