[Dwarf-Discuss] How to get union member offset?

David Anderson davea42 at earthlink.net
Thu Apr 2 08:51:02 PDT 2009

Jun Koi wrote:
> On Fri, Mar 27, 2009 at 10:49 AM, Roland McGrath <roland at redhat.com> wrote:
>>> I am trying to get the offset of a member in an union. However, there
>>> is no attribute DW_AT_data_member_location attached to it!
>> All members of a union start at offset 0.  I think the C standard may
>> require that this is always true because of its pointer casting rules.
>> But regardless DWARF 3 section 5.6.6 says: "The location description for
>> a data member of a union may be omitted, since all data members of a
>> union begin at the same address."
>> What I'd consider wise consumer behavior is always to look for
>> DW_AT_data_member_location as for struct, class, etc. as you already do.
>> If it's not found and the containing type is a union, treat it as if
>> you'd found the attribute with DW_FORM_udata and value 0.
> Excellent, but still there is a question: given a Dwarf_Die, how can
> we find its container? Is there any function for that?
Hmm. That looks suspiciously like libdwarf (which has
a type Dwarf_Die but libdwarf  is not supported by the DWARF committee).   

If you refer to libdwarf the answer is no.  As part of the process of
reading thru the debugging information entries of a compilation unit
you would have seen (and noted in some data structures
of your own, one assumes) all the relationships
and that would have made clear the 'container' of the Dwarf_Die.  

If you refer to libdwarf we should probably take this discussion off the
dwarf-discuss list: email me directly.

More information about the Dwarf-Discuss mailing list