1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // DWARF type information structures.
6 // The format is heavily biased toward C, but for simplicity
7 // the String methods use a pseudo-Go syntax.
13 // A Type conventionally represents a pointer to any of the
14 // specific Type structures (CharType, StructType, etc.).
21 // A CommonType holds fields common to multiple types.
22 // If a field is not known or not applicable for a given type,
23 // the zero value is used.
24 type CommonType
struct {
25 ByteSize
int64 // size of value of this type, in bytes
26 Name
string // name that can be used to refer to type
29 func (c
*CommonType
) Common() *CommonType
{ return c
}
31 func (c
*CommonType
) Size() int64 { return c
.ByteSize
}
35 // A BasicType holds fields common to all basic types.
36 type BasicType
struct {
42 func (b
*BasicType
) Basic() *BasicType
{ return b
}
44 func (t
*BasicType
) String() string {
51 // A CharType represents a signed character type.
52 type CharType
struct {
56 // A UcharType represents an unsigned character type.
57 type UcharType
struct {
61 // An IntType represents a signed integer type.
66 // A UintType represents an unsigned integer type.
67 type UintType
struct {
71 // A FloatType represents a floating point type.
72 type FloatType
struct {
76 // A ComplexType represents a complex floating point type.
77 type ComplexType
struct {
81 // A BoolType represents a boolean type.
82 type BoolType
struct {
86 // An AddrType represents a machine address type.
87 type AddrType
struct {
91 // An UnspecifiedType represents an implicit, unknown, ambiguous or nonexistent type.
92 type UnspecifiedType
struct {
98 // A QualType represents a type that has the C/C++ "const", "restrict", or "volatile" qualifier.
99 type QualType
struct {
105 func (t
*QualType
) String() string { return t
.Qual
+ " " + t
.Type
.String() }
107 func (t
*QualType
) Size() int64 { return t
.Type
.Size() }
109 // An ArrayType represents a fixed size array type.
110 type ArrayType
struct {
113 StrideBitSize
int64 // if > 0, number of bits to hold each element
114 Count
int64 // if == -1, an incomplete array, like char x[].
117 func (t
*ArrayType
) String() string {
118 return "[" + strconv
.FormatInt(t
.Count
, 10) + "]" + t
.Type
.String()
121 func (t
*ArrayType
) Size() int64 {
125 return t
.Count
* t
.Type
.Size()
128 // A VoidType represents the C void type.
129 type VoidType
struct {
133 func (t
*VoidType
) String() string { return "void" }
135 // A PtrType represents a pointer type.
136 type PtrType
struct {
141 func (t
*PtrType
) String() string { return "*" + t
.Type
.String() }
143 // A StructType represents a struct, union, or C++ class type.
144 type StructType
struct {
147 Kind
string // "struct", "union", or "class".
149 Incomplete
bool // if true, struct, union, class is declared but not defined
152 // A StructField represents a field in a struct, union, or C++ class type.
153 type StructField
struct {
157 ByteSize
int64 // usually zero; use Type.Size() for normal fields
158 BitOffset
int64 // within the ByteSize bytes at ByteOffset
159 BitSize
int64 // zero if not a bit field
162 func (t
*StructType
) String() string {
163 if t
.StructName
!= "" {
164 return t
.Kind
+ " " + t
.StructName
169 func (t
*StructType
) Defn() string {
171 if t
.StructName
!= "" {
172 s
+= " " + t
.StructName
175 s
+= " /*incomplete*/"
179 for i
, f
:= range t
.Field
{
183 s
+= f
.Name
+ " " + f
.Type
.String()
184 s
+= "@" + strconv
.FormatInt(f
.ByteOffset
, 10)
186 s
+= " : " + strconv
.FormatInt(f
.BitSize
, 10)
187 s
+= "@" + strconv
.FormatInt(f
.BitOffset
, 10)
194 // An EnumType represents an enumerated type.
195 // The only indication of its native integer type is its ByteSize
196 // (inside CommonType).
197 type EnumType
struct {
203 // An EnumValue represents a single enumeration value.
204 type EnumValue
struct {
209 func (t
*EnumType
) String() string {
211 if t
.EnumName
!= "" {
212 s
+= " " + t
.EnumName
215 for i
, v
:= range t
.Val
{
219 s
+= v
.Name
+ "=" + strconv
.FormatInt(v
.Val
, 10)
225 // A FuncType represents a function type.
226 type FuncType
struct {
232 func (t
*FuncType
) String() string {
234 for i
, t
:= range t
.ParamType
{
241 if t
.ReturnType
!= nil {
242 s
+= " " + t
.ReturnType
.String()
247 // A DotDotDotType represents the variadic ... function parameter.
248 type DotDotDotType
struct {
252 func (t
*DotDotDotType
) String() string { return "..." }
254 // A TypedefType represents a named type.
255 type TypedefType
struct {
260 func (t
*TypedefType
) String() string { return t
.Name
}
262 func (t
*TypedefType
) Size() int64 { return t
.Type
.Size() }
264 // typeReader is used to read from either the info section or the
266 type typeReader
interface {
268 Next() (*Entry
, error
)
271 // AddressSize returns the size in bytes of addresses in the current
276 // Type reads the type at off in the DWARF ``info'' section.
277 func (d
*Data
) Type(off Offset
) (Type
, error
) {
278 return d
.readType("info", d
.Reader(), off
, d
.typeCache
, nil)
281 // readType reads a type from r at off of name. It adds types to the
282 // type cache, appends new typedef types to typedefs, and computes the
283 // sizes of types. Callers should pass nil for typedefs; this is used
284 // for internal recursion.
285 func (d
*Data
) readType(name
string, r typeReader
, off Offset
, typeCache
map[Offset
]Type
, typedefs
*[]*TypedefType
) (Type
, error
) {
286 if t
, ok
:= typeCache
[off
]; ok
{
294 addressSize
:= r
.AddressSize()
295 if e
== nil || e
.Offset
!= off
{
296 return nil, DecodeError
{name
, off
, "no type at offset"}
299 // If this is the root of the recursion, prepare to resolve
300 // typedef sizes once the recursion is done. This must be done
301 // after the type graph is constructed because it may need to
302 // resolve cycles in a different order than readType
305 var typedefList
[]*TypedefType
307 for _
, t
:= range typedefList
{
308 t
.Common().ByteSize
= t
.Type
.Size()
311 typedefs
= &typedefList
314 // Parse type from Entry.
315 // Must always set typeCache[off] before calling
316 // d.readType recursively, to handle circular types correctly.
321 // Get next child; set err if error happens.
322 next
:= func() *Entry
{
326 // Only return direct children.
327 // Skip over composite entries that happen to be nested
328 // inside this one. Most DWARF generators wouldn't generate
329 // such a thing, but clang does.
330 // See golang.org/issue/6472.
332 kid
, err1
:= r
.Next()
338 err
= DecodeError
{name
, r
.offset(), "unexpected end of DWARF entries"}
358 // Get Type referred to by Entry's AttrType field.
359 // Set err if error happens. Not having a type is an error.
360 typeOf
:= func(e
*Entry
) Type
{
361 tval
:= e
.Val(AttrType
)
363 switch toff
:= tval
.(type) {
365 if t
, err
= d
.readType(name
, r
.clone(), toff
, typeCache
, typedefs
); err
!= nil {
369 if t
, err
= d
.sigToType(toff
); err
!= nil {
373 // It appears that no Type means "void".
381 // Multi-dimensional array. (DWARF v2 §5.4)
383 // AttrType:subtype [required]
384 // AttrStrideSize: size in bits of each element of the array
385 // AttrByteSize: size of entire array
387 // TagSubrangeType or TagEnumerationType giving one dimension.
388 // dimensions are in left to right order.
392 if t
.Type
= typeOf(e
); err
!= nil {
395 t
.StrideBitSize
, _
= e
.Val(AttrStrideSize
).(int64)
397 // Accumulate dimensions,
399 for kid
:= next(); kid
!= nil; kid
= next() {
400 // TODO(rsc): Can also be TagEnumerationType
401 // but haven't seen that in the wild yet.
403 case TagSubrangeType
:
404 count
, ok
:= kid
.Val(AttrCount
).(int64)
406 // Old binaries may have an upper bound instead.
407 count
, ok
= kid
.Val(AttrUpperBound
).(int64)
409 count
++ // Length is one more than upper bound.
410 } else if len(dims
) == 0 {
411 count
= -1 // As in x[].
414 dims
= append(dims
, count
)
415 case TagEnumerationType
:
416 err
= DecodeError
{name
, kid
.Offset
, "cannot handle enumeration type as array bound"}
421 // LLVM generates this for x[].
426 for i
:= len(dims
) - 1; i
>= 1; i
-- {
427 t
.Type
= &ArrayType
{Type
: t
.Type
, Count
: dims
[i
]}
431 // Basic type. (DWARF v2 §5.1)
433 // AttrName: name of base type in programming language of the compilation unit [required]
434 // AttrEncoding: encoding value for type (encFloat etc) [required]
435 // AttrByteSize: size of type in bytes [required]
436 // AttrBitOffset: for sub-byte types, size in bits
437 // AttrBitSize: for sub-byte types, bit offset of high order bit in the AttrByteSize bytes
438 name
, _
:= e
.Val(AttrName
).(string)
439 enc
, ok
:= e
.Val(AttrEncoding
).(int64)
441 err
= DecodeError
{name
, e
.Offset
, "missing encoding attribute for " + name
}
446 err
= DecodeError
{name
, e
.Offset
, "unrecognized encoding attribute value"}
453 case encComplexFloat
:
454 typ
= new(ComplexType
)
455 if name
== "complex" {
456 // clang writes out 'complex' instead of 'complex float' or 'complex double'.
457 // clang also writes out a byte size that we can use to distinguish.
459 switch byteSize
, _
:= e
.Val(AttrByteSize
).(int64); byteSize
{
461 name
= "complex float"
463 name
= "complex double"
474 case encUnsignedChar
:
478 t
:= typ
.(interface {
482 t
.BitSize
, _
= e
.Val(AttrBitSize
).(int64)
483 t
.BitOffset
, _
= e
.Val(AttrBitOffset
).(int64)
485 case TagClassType
, TagStructType
, TagUnionType
:
486 // Structure, union, or class type. (DWARF v2 §5.5)
488 // AttrName: name of struct, union, or class
489 // AttrByteSize: byte size [required]
490 // AttrDeclaration: if true, struct/union/class is incomplete
492 // TagMember to describe one member.
493 // AttrName: name of member [required]
494 // AttrType: type of member [required]
495 // AttrByteSize: size in bytes
496 // AttrBitOffset: bit offset within bytes for bit fields
497 // AttrBitSize: bit size for bit fields
498 // AttrDataMemberLoc: location within struct [required for struct, class]
499 // There is much more to handle C++, all ignored for now.
511 t
.StructName
, _
= e
.Val(AttrName
).(string)
512 t
.Incomplete
= e
.Val(AttrDeclaration
) != nil
513 t
.Field
= make([]*StructField
, 0, 8)
514 var lastFieldType
*Type
515 var lastFieldBitOffset
int64
516 for kid
:= next(); kid
!= nil; kid
= next() {
517 if kid
.Tag
!= TagMember
{
520 f
:= new(StructField
)
521 if f
.Type
= typeOf(kid
); err
!= nil {
524 switch loc
:= kid
.Val(AttrDataMemberLoc
).(type) {
526 // TODO: Should have original compilation
527 // unit here, not unknownFormat.
528 b
:= makeBuf(d
, unknownFormat
{}, "location", 0, loc
)
529 if b
.uint8() != opPlusUconst
{
530 err
= DecodeError
{name
, kid
.Offset
, "unexpected opcode"}
533 f
.ByteOffset
= int64(b
.uint())
542 haveBitOffset
:= false
543 f
.Name
, _
= kid
.Val(AttrName
).(string)
544 f
.ByteSize
, _
= kid
.Val(AttrByteSize
).(int64)
545 f
.BitOffset
, haveBitOffset
= kid
.Val(AttrBitOffset
).(int64)
546 f
.BitSize
, _
= kid
.Val(AttrBitSize
).(int64)
547 t
.Field
= append(t
.Field
, f
)
551 bito
= f
.ByteOffset
* 8
553 if bito
== lastFieldBitOffset
&& t
.Kind
!= "union" {
554 // Last field was zero width. Fix array length.
555 // (DWARF writes out 0-length arrays as if they were 1-length arrays.)
556 zeroArray(lastFieldType
)
558 lastFieldType
= &f
.Type
559 lastFieldBitOffset
= bito
561 if t
.Kind
!= "union" {
562 b
, ok
:= e
.Val(AttrByteSize
).(int64)
563 if ok
&& b
*8 == lastFieldBitOffset
{
564 // Final field must be zero width. Fix array length.
565 zeroArray(lastFieldType
)
569 case TagConstType
, TagVolatileType
, TagRestrictType
:
570 // Type modifier (DWARF v2 §5.2)
576 if t
.Type
= typeOf(e
); err
!= nil {
582 case TagRestrictType
:
584 case TagVolatileType
:
588 case TagEnumerationType
:
589 // Enumeration type (DWARF v2 §5.6)
591 // AttrName: enum name if any
592 // AttrByteSize: bytes required to represent largest value
595 // AttrName: name of constant
596 // AttrConstValue: value of constant
600 t
.EnumName
, _
= e
.Val(AttrName
).(string)
601 t
.Val
= make([]*EnumValue
, 0, 8)
602 for kid
:= next(); kid
!= nil; kid
= next() {
603 if kid
.Tag
== TagEnumerator
{
605 f
.Name
, _
= kid
.Val(AttrName
).(string)
606 f
.Val
, _
= kid
.Val(AttrConstValue
).(int64)
609 val
:= make([]*EnumValue
, n
, n
*2)
613 t
.Val
= t
.Val
[0 : n
+1]
619 // Type modifier (DWARF v2 §5.2)
621 // AttrType: subtype [not required! void* has no AttrType]
622 // AttrAddrClass: address class [ignored]
626 if e
.Val(AttrType
) == nil {
632 case TagSubroutineType
:
633 // Subroutine type. (DWARF v2 §5.7)
635 // AttrType: type of return value if any
636 // AttrName: possible name of type [ignored]
637 // AttrPrototyped: whether used ANSI C prototype [ignored]
639 // TagFormalParameter: typed parameter
640 // AttrType: type of parameter
641 // TagUnspecifiedParameter: final ...
645 if t
.ReturnType
= typeOf(e
); err
!= nil {
648 t
.ParamType
= make([]Type
, 0, 8)
649 for kid
:= next(); kid
!= nil; kid
= next() {
654 case TagFormalParameter
:
655 if tkid
= typeOf(kid
); err
!= nil {
658 case TagUnspecifiedParameters
:
659 tkid
= &DotDotDotType
{}
661 t
.ParamType
= append(t
.ParamType
, tkid
)
665 // Typedef (DWARF v2 §5.3)
667 // AttrName: name [required]
668 // AttrType: type definition [required]
669 t
:= new(TypedefType
)
672 t
.Name
, _
= e
.Val(AttrName
).(string)
675 case TagUnspecifiedType
:
676 // Unspecified type (DWARF v3 §5.2)
679 t
:= new(UnspecifiedType
)
682 t
.Name
, _
= e
.Val(AttrName
).(string)
690 b
, ok
:= e
.Val(AttrByteSize
).(int64)
693 switch t
:= typ
.(type) {
695 // Record that we need to resolve this
696 // type's size once the type graph is
698 *typedefs
= append(*typedefs
, t
)
700 b
= int64(addressSize
)
703 typ
.Common().ByteSize
= b
708 // If the parse fails, take the type out of the cache
709 // so that the next call with this offset doesn't hit
710 // the cache and return success.
711 delete(typeCache
, off
)
715 func zeroArray(t
*Type
) {
719 at
, ok
:= (*t
).(*ArrayType
)
720 if !ok || at
.Type
.Size() == 0 {
723 // Make a copy to avoid invalidating typeCache.