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.
16 // A Type conventionally represents a pointer to any of the
17 // specific Type structures (CharType, StructType, etc.).
24 // A CommonType holds fields common to multiple types.
25 // If a field is not known or not applicable for a given type,
26 // the zero value is used.
27 type CommonType struct {
28 ByteSize int64 // size of value of this type, in bytes
29 Name string // name that can be used to refer to type
32 func (c *CommonType) Common() *CommonType { return c }
34 func (c *CommonType) Size() int64 { return c.ByteSize }
38 // A BasicType holds fields common to all basic types.
39 type BasicType struct {
45 func (b *BasicType) Basic() *BasicType { return b }
47 func (t *BasicType) String() string {
54 // A CharType represents a signed character type.
55 type CharType struct {
59 // A UcharType represents an unsigned character type.
60 type UcharType struct {
64 // An IntType represents a signed integer type.
69 // A UintType represents an unsigned integer type.
70 type UintType struct {
74 // A FloatType represents a floating point type.
75 type FloatType struct {
79 // A ComplexType represents a complex floating point type.
80 type ComplexType struct {
84 // A BoolType represents a boolean type.
85 type BoolType struct {
89 // An AddrType represents a machine address type.
90 type AddrType struct {
96 // A QualType represents a type that has the C/C++ "const", "restrict", or "volatile" qualifier.
97 type QualType struct {
103 func (t *QualType) String() string { return t.Qual + " " + t.Type.String() }
105 func (t *QualType) Size() int64 { return t.Type.Size() }
107 // An ArrayType represents a fixed size array type.
108 type ArrayType struct {
111 StrideBitSize int64 // if > 0, number of bits to hold each element
112 Count int64 // if == -1, an incomplete array, like char x[].
115 func (t *ArrayType) String() string {
116 return "[" + strconv.Itoa64(t.Count) + "]" + t.Type.String()
119 func (t *ArrayType) Size() int64 { return t.Count * t.Type.Size() }
121 // A VoidType represents the C void type.
122 type VoidType struct {
126 func (t *VoidType) String() string { return "void" }
128 // A PtrType represents a pointer type.
129 type PtrType struct {
134 func (t *PtrType) String() string { return "*" + t.Type.String() }
136 // A StructType represents a struct, union, or C++ class type.
137 type StructType struct {
140 Kind string // "struct", "union", or "class".
142 Incomplete bool // if true, struct, union, class is declared but not defined
145 // A StructField represents a field in a struct, union, or C++ class type.
146 type StructField struct {
151 BitOffset int64 // within the ByteSize bytes at ByteOffset
152 BitSize int64 // zero if not a bit field
155 func (t *StructType) String() string {
156 if t.StructName != "" {
157 return t.Kind + " " + t.StructName
162 func (t *StructType) Defn() string {
164 if t.StructName != "" {
165 s += " " + t.StructName
168 s += " /*incomplete*/"
172 for i, f := range t.Field {
176 s += f.Name + " " + f.Type.String()
177 s += "@" + strconv.Itoa64(f.ByteOffset)
179 s += " : " + strconv.Itoa64(f.BitSize)
180 s += "@" + strconv.Itoa64(f.BitOffset)
187 // An EnumType represents an enumerated type.
188 // The only indication of its native integer type is its ByteSize
189 // (inside CommonType).
190 type EnumType struct {
196 // An EnumValue represents a single enumeration value.
197 type EnumValue struct {
202 func (t *EnumType) String() string {
204 if t.EnumName != "" {
205 s += " " + t.EnumName
208 for i, v := range t.Val {
212 s += v.Name + "=" + strconv.Itoa64(v.Val)
218 // A FuncType represents a function type.
219 type FuncType struct {
225 func (t *FuncType) String() string {
227 for i, t := range t.ParamType {
234 if t.ReturnType != nil {
235 s += " " + t.ReturnType.String()
240 // A DotDotDotType represents the variadic ... function parameter.
241 type DotDotDotType struct {
245 func (t *DotDotDotType) String() string { return "..." }
247 // A TypedefType represents a named type.
248 type TypedefType struct {
253 func (t *TypedefType) String() string { return t.Name }
255 func (t *TypedefType) Size() int64 { return t.Type.Size() }
257 func (d *Data) Type(off Offset) (Type, os.Error) {
258 if t, ok := d.typeCache[off]; ok {
268 if e == nil || e.Offset != off {
269 return nil, DecodeError{"info", off, "no type at offset"}
272 // Parse type from Entry.
273 // Must always set d.typeCache[off] before calling
274 // d.Type recursively, to handle circular types correctly.
277 // Get next child; set err if error happens.
278 next := func() *Entry {
282 kid, err1 := r.Next()
288 err = DecodeError{"info", r.b.off, "unexpected end of DWARF entries"}
297 // Get Type referred to by Entry's AttrType field.
298 // Set err if error happens. Not having a type is an error.
299 typeOf := func(e *Entry) Type {
300 toff, ok := e.Val(AttrType).(Offset)
302 // It appears that no Type means "void".
306 if t, err = d.Type(toff); err != nil {
314 // Multi-dimensional array. (DWARF v2 §5.4)
316 // AttrType:subtype [required]
317 // AttrStrideSize: size in bits of each element of the array
318 // AttrByteSize: size of entire array
320 // TagSubrangeType or TagEnumerationType giving one dimension.
321 // dimensions are in left to right order.
325 if t.Type = typeOf(e); err != nil {
328 t.StrideBitSize, _ = e.Val(AttrStrideSize).(int64)
330 // Accumulate dimensions,
332 for kid := next(); kid != nil; kid = next() {
333 // TODO(rsc): Can also be TagEnumerationType
334 // but haven't seen that in the wild yet.
336 case TagSubrangeType:
337 max, ok := kid.Val(AttrUpperBound).(int64)
339 max = -2 // Count == -1, as in x[].
344 // Multidimensional array.
345 // Create new array type underneath this one.
346 t.Type = &ArrayType{Type: t.Type, Count: max + 1}
349 case TagEnumerationType:
350 err = DecodeError{"info", kid.Offset, "cannot handle enumeration type as array bound"}
355 err = DecodeError{"info", e.Offset, "missing dimension for array"}
360 // Basic type. (DWARF v2 §5.1)
362 // AttrName: name of base type in programming language of the compilation unit [required]
363 // AttrEncoding: encoding value for type (encFloat etc) [required]
364 // AttrByteSize: size of type in bytes [required]
365 // AttrBitOffset: for sub-byte types, size in bits
366 // AttrBitSize: for sub-byte types, bit offset of high order bit in the AttrByteSize bytes
367 name, _ := e.Val(AttrName).(string)
368 enc, ok := e.Val(AttrEncoding).(int64)
370 err = DecodeError{"info", e.Offset, "missing encoding attribute for " + name}
375 err = DecodeError{"info", e.Offset, "unrecognized encoding attribute value"}
382 case encComplexFloat:
383 typ = new(ComplexType)
392 case encUnsignedChar:
395 d.typeCache[off] = typ
396 t := typ.(interface {
400 t.BitSize, _ = e.Val(AttrBitSize).(int64)
401 t.BitOffset, _ = e.Val(AttrBitOffset).(int64)
403 case TagClassType, TagStructType, TagUnionType:
404 // Structure, union, or class type. (DWARF v2 §5.5)
406 // AttrName: name of struct, union, or class
407 // AttrByteSize: byte size [required]
408 // AttrDeclaration: if true, struct/union/class is incomplete
410 // TagMember to describe one member.
411 // AttrName: name of member [required]
412 // AttrType: type of member [required]
413 // AttrByteSize: size in bytes
414 // AttrBitOffset: bit offset within bytes for bit fields
415 // AttrBitSize: bit size for bit fields
416 // AttrDataMemberLoc: location within struct [required for struct, class]
417 // There is much more to handle C++, all ignored for now.
429 t.StructName, _ = e.Val(AttrName).(string)
430 t.Incomplete = e.Val(AttrDeclaration) != nil
431 t.Field = make([]*StructField, 0, 8)
432 for kid := next(); kid != nil; kid = next() {
433 if kid.Tag == TagMember {
434 f := new(StructField)
435 if f.Type = typeOf(kid); err != nil {
438 if loc, ok := kid.Val(AttrDataMemberLoc).([]byte); ok {
439 b := makeBuf(d, "location", 0, loc, d.addrsize)
440 if b.uint8() != opPlusUconst {
441 err = DecodeError{"info", kid.Offset, "unexpected opcode"}
444 f.ByteOffset = int64(b.uint())
450 f.Name, _ = kid.Val(AttrName).(string)
451 f.ByteSize, _ = kid.Val(AttrByteSize).(int64)
452 f.BitOffset, _ = kid.Val(AttrBitOffset).(int64)
453 f.BitSize, _ = kid.Val(AttrBitSize).(int64)
454 t.Field = append(t.Field, f)
458 case TagConstType, TagVolatileType, TagRestrictType:
459 // Type modifier (DWARF v2 §5.2)
465 if t.Type = typeOf(e); err != nil {
471 case TagRestrictType:
473 case TagVolatileType:
477 case TagEnumerationType:
478 // Enumeration type (DWARF v2 §5.6)
480 // AttrName: enum name if any
481 // AttrByteSize: bytes required to represent largest value
484 // AttrName: name of constant
485 // AttrConstValue: value of constant
489 t.EnumName, _ = e.Val(AttrName).(string)
490 t.Val = make([]*EnumValue, 0, 8)
491 for kid := next(); kid != nil; kid = next() {
492 if kid.Tag == TagEnumerator {
494 f.Name, _ = kid.Val(AttrName).(string)
495 f.Val, _ = kid.Val(AttrConstValue).(int64)
498 val := make([]*EnumValue, n, n*2)
502 t.Val = t.Val[0 : n+1]
508 // Type modifier (DWARF v2 §5.2)
510 // AttrType: subtype [not required! void* has no AttrType]
511 // AttrAddrClass: address class [ignored]
515 if e.Val(AttrType) == nil {
521 case TagSubroutineType:
522 // Subroutine type. (DWARF v2 §5.7)
524 // AttrType: type of return value if any
525 // AttrName: possible name of type [ignored]
526 // AttrPrototyped: whether used ANSI C prototye [ignored]
528 // TagFormalParameter: typed parameter
529 // AttrType: type of parameter
530 // TagUnspecifiedParameter: final ...
534 if t.ReturnType = typeOf(e); err != nil {
537 t.ParamType = make([]Type, 0, 8)
538 for kid := next(); kid != nil; kid = next() {
543 case TagFormalParameter:
544 if tkid = typeOf(kid); err != nil {
547 case TagUnspecifiedParameters:
548 tkid = &DotDotDotType{}
550 t.ParamType = append(t.ParamType, tkid)
554 // Typedef (DWARF v2 §5.3)
556 // AttrName: name [required]
557 // AttrType: type definition [required]
558 t := new(TypedefType)
561 t.Name, _ = e.Val(AttrName).(string)
569 b, ok := e.Val(AttrByteSize).(int64)
573 typ.Common().ByteSize = b
578 // If the parse fails, take the type out of the cache
579 // so that the next call with this offset doesn't hit
580 // the cache and return success.
581 d.typeCache[off] = nil, false