github.com/shijuvar/go@v0.0.0-20141209052335-e8f13700b70c/src/debug/dwarf/type.go (about) 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. 4 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. 8 9 package dwarf 10 11 import "strconv" 12 13 // A Type conventionally represents a pointer to any of the 14 // specific Type structures (CharType, StructType, etc.). 15 type Type interface { 16 Common() *CommonType 17 String() string 18 Size() int64 19 } 20 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 27 } 28 29 func (c *CommonType) Common() *CommonType { return c } 30 31 func (c *CommonType) Size() int64 { return c.ByteSize } 32 33 // Basic types 34 35 // A BasicType holds fields common to all basic types. 36 type BasicType struct { 37 CommonType 38 BitSize int64 39 BitOffset int64 40 } 41 42 func (b *BasicType) Basic() *BasicType { return b } 43 44 func (t *BasicType) String() string { 45 if t.Name != "" { 46 return t.Name 47 } 48 return "?" 49 } 50 51 // A CharType represents a signed character type. 52 type CharType struct { 53 BasicType 54 } 55 56 // A UcharType represents an unsigned character type. 57 type UcharType struct { 58 BasicType 59 } 60 61 // An IntType represents a signed integer type. 62 type IntType struct { 63 BasicType 64 } 65 66 // A UintType represents an unsigned integer type. 67 type UintType struct { 68 BasicType 69 } 70 71 // A FloatType represents a floating point type. 72 type FloatType struct { 73 BasicType 74 } 75 76 // A ComplexType represents a complex floating point type. 77 type ComplexType struct { 78 BasicType 79 } 80 81 // A BoolType represents a boolean type. 82 type BoolType struct { 83 BasicType 84 } 85 86 // An AddrType represents a machine address type. 87 type AddrType struct { 88 BasicType 89 } 90 91 // An UnspecifiedType represents an implicit, unknown, ambiguous or nonexistent type. 92 type UnspecifiedType struct { 93 BasicType 94 } 95 96 // qualifiers 97 98 // A QualType represents a type that has the C/C++ "const", "restrict", or "volatile" qualifier. 99 type QualType struct { 100 CommonType 101 Qual string 102 Type Type 103 } 104 105 func (t *QualType) String() string { return t.Qual + " " + t.Type.String() } 106 107 func (t *QualType) Size() int64 { return t.Type.Size() } 108 109 // An ArrayType represents a fixed size array type. 110 type ArrayType struct { 111 CommonType 112 Type Type 113 StrideBitSize int64 // if > 0, number of bits to hold each element 114 Count int64 // if == -1, an incomplete array, like char x[]. 115 } 116 117 func (t *ArrayType) String() string { 118 return "[" + strconv.FormatInt(t.Count, 10) + "]" + t.Type.String() 119 } 120 121 func (t *ArrayType) Size() int64 { 122 if t.Count == -1 { 123 return 0 124 } 125 return t.Count * t.Type.Size() 126 } 127 128 // A VoidType represents the C void type. 129 type VoidType struct { 130 CommonType 131 } 132 133 func (t *VoidType) String() string { return "void" } 134 135 // A PtrType represents a pointer type. 136 type PtrType struct { 137 CommonType 138 Type Type 139 } 140 141 func (t *PtrType) String() string { return "*" + t.Type.String() } 142 143 // A StructType represents a struct, union, or C++ class type. 144 type StructType struct { 145 CommonType 146 StructName string 147 Kind string // "struct", "union", or "class". 148 Field []*StructField 149 Incomplete bool // if true, struct, union, class is declared but not defined 150 } 151 152 // A StructField represents a field in a struct, union, or C++ class type. 153 type StructField struct { 154 Name string 155 Type Type 156 ByteOffset int64 157 ByteSize int64 158 BitOffset int64 // within the ByteSize bytes at ByteOffset 159 BitSize int64 // zero if not a bit field 160 } 161 162 func (t *StructType) String() string { 163 if t.StructName != "" { 164 return t.Kind + " " + t.StructName 165 } 166 return t.Defn() 167 } 168 169 func (t *StructType) Defn() string { 170 s := t.Kind 171 if t.StructName != "" { 172 s += " " + t.StructName 173 } 174 if t.Incomplete { 175 s += " /*incomplete*/" 176 return s 177 } 178 s += " {" 179 for i, f := range t.Field { 180 if i > 0 { 181 s += "; " 182 } 183 s += f.Name + " " + f.Type.String() 184 s += "@" + strconv.FormatInt(f.ByteOffset, 10) 185 if f.BitSize > 0 { 186 s += " : " + strconv.FormatInt(f.BitSize, 10) 187 s += "@" + strconv.FormatInt(f.BitOffset, 10) 188 } 189 } 190 s += "}" 191 return s 192 } 193 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 { 198 CommonType 199 EnumName string 200 Val []*EnumValue 201 } 202 203 // An EnumValue represents a single enumeration value. 204 type EnumValue struct { 205 Name string 206 Val int64 207 } 208 209 func (t *EnumType) String() string { 210 s := "enum" 211 if t.EnumName != "" { 212 s += " " + t.EnumName 213 } 214 s += " {" 215 for i, v := range t.Val { 216 if i > 0 { 217 s += "; " 218 } 219 s += v.Name + "=" + strconv.FormatInt(v.Val, 10) 220 } 221 s += "}" 222 return s 223 } 224 225 // A FuncType represents a function type. 226 type FuncType struct { 227 CommonType 228 ReturnType Type 229 ParamType []Type 230 } 231 232 func (t *FuncType) String() string { 233 s := "func(" 234 for i, t := range t.ParamType { 235 if i > 0 { 236 s += ", " 237 } 238 s += t.String() 239 } 240 s += ")" 241 if t.ReturnType != nil { 242 s += " " + t.ReturnType.String() 243 } 244 return s 245 } 246 247 // A DotDotDotType represents the variadic ... function parameter. 248 type DotDotDotType struct { 249 CommonType 250 } 251 252 func (t *DotDotDotType) String() string { return "..." } 253 254 // A TypedefType represents a named type. 255 type TypedefType struct { 256 CommonType 257 Type Type 258 } 259 260 func (t *TypedefType) String() string { return t.Name } 261 262 func (t *TypedefType) Size() int64 { return t.Type.Size() } 263 264 // typeReader is used to read from either the info section or the 265 // types section. 266 type typeReader interface { 267 Seek(Offset) 268 Next() (*Entry, error) 269 clone() typeReader 270 offset() Offset 271 } 272 273 // Type reads the type at off in the DWARF ``info'' section. 274 func (d *Data) Type(off Offset) (Type, error) { 275 return d.readType("info", d.Reader(), off, d.typeCache) 276 } 277 278 // readType reads a type from r at off of name using and updating a 279 // type cache. 280 func (d *Data) readType(name string, r typeReader, off Offset, typeCache map[Offset]Type) (Type, error) { 281 if t, ok := typeCache[off]; ok { 282 return t, nil 283 } 284 r.Seek(off) 285 e, err := r.Next() 286 if err != nil { 287 return nil, err 288 } 289 if e == nil || e.Offset != off { 290 return nil, DecodeError{name, off, "no type at offset"} 291 } 292 293 // Parse type from Entry. 294 // Must always set typeCache[off] before calling 295 // d.Type recursively, to handle circular types correctly. 296 var typ Type 297 298 nextDepth := 0 299 300 // Get next child; set err if error happens. 301 next := func() *Entry { 302 if !e.Children { 303 return nil 304 } 305 // Only return direct children. 306 // Skip over composite entries that happen to be nested 307 // inside this one. Most DWARF generators wouldn't generate 308 // such a thing, but clang does. 309 // See golang.org/issue/6472. 310 for { 311 kid, err1 := r.Next() 312 if err1 != nil { 313 err = err1 314 return nil 315 } 316 if kid == nil { 317 err = DecodeError{name, r.offset(), "unexpected end of DWARF entries"} 318 return nil 319 } 320 if kid.Tag == 0 { 321 if nextDepth > 0 { 322 nextDepth-- 323 continue 324 } 325 return nil 326 } 327 if kid.Children { 328 nextDepth++ 329 } 330 if nextDepth > 0 { 331 continue 332 } 333 return kid 334 } 335 } 336 337 // Get Type referred to by Entry's AttrType field. 338 // Set err if error happens. Not having a type is an error. 339 typeOf := func(e *Entry) Type { 340 tval := e.Val(AttrType) 341 var t Type 342 switch toff := tval.(type) { 343 case Offset: 344 if t, err = d.readType(name, r.clone(), toff, typeCache); err != nil { 345 return nil 346 } 347 case uint64: 348 if t, err = d.sigToType(toff); err != nil { 349 return nil 350 } 351 default: 352 // It appears that no Type means "void". 353 return new(VoidType) 354 } 355 return t 356 } 357 358 switch e.Tag { 359 case TagArrayType: 360 // Multi-dimensional array. (DWARF v2 §5.4) 361 // Attributes: 362 // AttrType:subtype [required] 363 // AttrStrideSize: size in bits of each element of the array 364 // AttrByteSize: size of entire array 365 // Children: 366 // TagSubrangeType or TagEnumerationType giving one dimension. 367 // dimensions are in left to right order. 368 t := new(ArrayType) 369 typ = t 370 typeCache[off] = t 371 if t.Type = typeOf(e); err != nil { 372 goto Error 373 } 374 t.StrideBitSize, _ = e.Val(AttrStrideSize).(int64) 375 376 // Accumulate dimensions, 377 var dims []int64 378 for kid := next(); kid != nil; kid = next() { 379 // TODO(rsc): Can also be TagEnumerationType 380 // but haven't seen that in the wild yet. 381 switch kid.Tag { 382 case TagSubrangeType: 383 count, ok := kid.Val(AttrCount).(int64) 384 if !ok { 385 // Old binaries may have an upper bound instead. 386 count, ok = kid.Val(AttrUpperBound).(int64) 387 if ok { 388 count++ // Length is one more than upper bound. 389 } else if len(dims) == 0 { 390 count = -1 // As in x[]. 391 } 392 } 393 dims = append(dims, count) 394 case TagEnumerationType: 395 err = DecodeError{name, kid.Offset, "cannot handle enumeration type as array bound"} 396 goto Error 397 } 398 } 399 if len(dims) == 0 { 400 // LLVM generates this for x[]. 401 dims = []int64{-1} 402 } 403 404 t.Count = dims[0] 405 for i := len(dims) - 1; i >= 1; i-- { 406 t.Type = &ArrayType{Type: t.Type, Count: dims[i]} 407 } 408 409 case TagBaseType: 410 // Basic type. (DWARF v2 §5.1) 411 // Attributes: 412 // AttrName: name of base type in programming language of the compilation unit [required] 413 // AttrEncoding: encoding value for type (encFloat etc) [required] 414 // AttrByteSize: size of type in bytes [required] 415 // AttrBitOffset: for sub-byte types, size in bits 416 // AttrBitSize: for sub-byte types, bit offset of high order bit in the AttrByteSize bytes 417 name, _ := e.Val(AttrName).(string) 418 enc, ok := e.Val(AttrEncoding).(int64) 419 if !ok { 420 err = DecodeError{name, e.Offset, "missing encoding attribute for " + name} 421 goto Error 422 } 423 switch enc { 424 default: 425 err = DecodeError{name, e.Offset, "unrecognized encoding attribute value"} 426 goto Error 427 428 case encAddress: 429 typ = new(AddrType) 430 case encBoolean: 431 typ = new(BoolType) 432 case encComplexFloat: 433 typ = new(ComplexType) 434 if name == "complex" { 435 // clang writes out 'complex' instead of 'complex float' or 'complex double'. 436 // clang also writes out a byte size that we can use to distinguish. 437 // See issue 8694. 438 switch byteSize, _ := e.Val(AttrByteSize).(int64); byteSize { 439 case 8: 440 name = "complex float" 441 case 16: 442 name = "complex double" 443 } 444 } 445 case encFloat: 446 typ = new(FloatType) 447 case encSigned: 448 typ = new(IntType) 449 case encUnsigned: 450 typ = new(UintType) 451 case encSignedChar: 452 typ = new(CharType) 453 case encUnsignedChar: 454 typ = new(UcharType) 455 } 456 typeCache[off] = typ 457 t := typ.(interface { 458 Basic() *BasicType 459 }).Basic() 460 t.Name = name 461 t.BitSize, _ = e.Val(AttrBitSize).(int64) 462 t.BitOffset, _ = e.Val(AttrBitOffset).(int64) 463 464 case TagClassType, TagStructType, TagUnionType: 465 // Structure, union, or class type. (DWARF v2 §5.5) 466 // Attributes: 467 // AttrName: name of struct, union, or class 468 // AttrByteSize: byte size [required] 469 // AttrDeclaration: if true, struct/union/class is incomplete 470 // Children: 471 // TagMember to describe one member. 472 // AttrName: name of member [required] 473 // AttrType: type of member [required] 474 // AttrByteSize: size in bytes 475 // AttrBitOffset: bit offset within bytes for bit fields 476 // AttrBitSize: bit size for bit fields 477 // AttrDataMemberLoc: location within struct [required for struct, class] 478 // There is much more to handle C++, all ignored for now. 479 t := new(StructType) 480 typ = t 481 typeCache[off] = t 482 switch e.Tag { 483 case TagClassType: 484 t.Kind = "class" 485 case TagStructType: 486 t.Kind = "struct" 487 case TagUnionType: 488 t.Kind = "union" 489 } 490 t.StructName, _ = e.Val(AttrName).(string) 491 t.Incomplete = e.Val(AttrDeclaration) != nil 492 t.Field = make([]*StructField, 0, 8) 493 var lastFieldType *Type 494 var lastFieldBitOffset int64 495 for kid := next(); kid != nil; kid = next() { 496 if kid.Tag == TagMember { 497 f := new(StructField) 498 if f.Type = typeOf(kid); err != nil { 499 goto Error 500 } 501 switch loc := kid.Val(AttrDataMemberLoc).(type) { 502 case []byte: 503 // TODO: Should have original compilation 504 // unit here, not unknownFormat. 505 b := makeBuf(d, unknownFormat{}, "location", 0, loc) 506 if b.uint8() != opPlusUconst { 507 err = DecodeError{name, kid.Offset, "unexpected opcode"} 508 goto Error 509 } 510 f.ByteOffset = int64(b.uint()) 511 if b.err != nil { 512 err = b.err 513 goto Error 514 } 515 case int64: 516 f.ByteOffset = loc 517 } 518 519 haveBitOffset := false 520 f.Name, _ = kid.Val(AttrName).(string) 521 f.ByteSize, _ = kid.Val(AttrByteSize).(int64) 522 f.BitOffset, haveBitOffset = kid.Val(AttrBitOffset).(int64) 523 f.BitSize, _ = kid.Val(AttrBitSize).(int64) 524 t.Field = append(t.Field, f) 525 526 bito := f.BitOffset 527 if !haveBitOffset { 528 bito = f.ByteOffset * 8 529 } 530 if bito == lastFieldBitOffset && t.Kind != "union" { 531 // Last field was zero width. Fix array length. 532 // (DWARF writes out 0-length arrays as if they were 1-length arrays.) 533 zeroArray(lastFieldType) 534 } 535 lastFieldType = &f.Type 536 lastFieldBitOffset = bito 537 } 538 } 539 if t.Kind != "union" { 540 b, ok := e.Val(AttrByteSize).(int64) 541 if ok && b*8 == lastFieldBitOffset { 542 // Final field must be zero width. Fix array length. 543 zeroArray(lastFieldType) 544 } 545 } 546 547 case TagConstType, TagVolatileType, TagRestrictType: 548 // Type modifier (DWARF v2 §5.2) 549 // Attributes: 550 // AttrType: subtype 551 t := new(QualType) 552 typ = t 553 typeCache[off] = t 554 if t.Type = typeOf(e); err != nil { 555 goto Error 556 } 557 switch e.Tag { 558 case TagConstType: 559 t.Qual = "const" 560 case TagRestrictType: 561 t.Qual = "restrict" 562 case TagVolatileType: 563 t.Qual = "volatile" 564 } 565 566 case TagEnumerationType: 567 // Enumeration type (DWARF v2 §5.6) 568 // Attributes: 569 // AttrName: enum name if any 570 // AttrByteSize: bytes required to represent largest value 571 // Children: 572 // TagEnumerator: 573 // AttrName: name of constant 574 // AttrConstValue: value of constant 575 t := new(EnumType) 576 typ = t 577 typeCache[off] = t 578 t.EnumName, _ = e.Val(AttrName).(string) 579 t.Val = make([]*EnumValue, 0, 8) 580 for kid := next(); kid != nil; kid = next() { 581 if kid.Tag == TagEnumerator { 582 f := new(EnumValue) 583 f.Name, _ = kid.Val(AttrName).(string) 584 f.Val, _ = kid.Val(AttrConstValue).(int64) 585 n := len(t.Val) 586 if n >= cap(t.Val) { 587 val := make([]*EnumValue, n, n*2) 588 copy(val, t.Val) 589 t.Val = val 590 } 591 t.Val = t.Val[0 : n+1] 592 t.Val[n] = f 593 } 594 } 595 596 case TagPointerType: 597 // Type modifier (DWARF v2 §5.2) 598 // Attributes: 599 // AttrType: subtype [not required! void* has no AttrType] 600 // AttrAddrClass: address class [ignored] 601 t := new(PtrType) 602 typ = t 603 typeCache[off] = t 604 if e.Val(AttrType) == nil { 605 t.Type = &VoidType{} 606 break 607 } 608 t.Type = typeOf(e) 609 610 case TagSubroutineType: 611 // Subroutine type. (DWARF v2 §5.7) 612 // Attributes: 613 // AttrType: type of return value if any 614 // AttrName: possible name of type [ignored] 615 // AttrPrototyped: whether used ANSI C prototype [ignored] 616 // Children: 617 // TagFormalParameter: typed parameter 618 // AttrType: type of parameter 619 // TagUnspecifiedParameter: final ... 620 t := new(FuncType) 621 typ = t 622 typeCache[off] = t 623 if t.ReturnType = typeOf(e); err != nil { 624 goto Error 625 } 626 t.ParamType = make([]Type, 0, 8) 627 for kid := next(); kid != nil; kid = next() { 628 var tkid Type 629 switch kid.Tag { 630 default: 631 continue 632 case TagFormalParameter: 633 if tkid = typeOf(kid); err != nil { 634 goto Error 635 } 636 case TagUnspecifiedParameters: 637 tkid = &DotDotDotType{} 638 } 639 t.ParamType = append(t.ParamType, tkid) 640 } 641 642 case TagTypedef: 643 // Typedef (DWARF v2 §5.3) 644 // Attributes: 645 // AttrName: name [required] 646 // AttrType: type definition [required] 647 t := new(TypedefType) 648 typ = t 649 typeCache[off] = t 650 t.Name, _ = e.Val(AttrName).(string) 651 t.Type = typeOf(e) 652 653 case TagUnspecifiedType: 654 // Unspecified type (DWARF v3 §5.2) 655 // Attributes: 656 // AttrName: name 657 t := new(UnspecifiedType) 658 typ = t 659 typeCache[off] = t 660 t.Name, _ = e.Val(AttrName).(string) 661 } 662 663 if err != nil { 664 goto Error 665 } 666 667 { 668 b, ok := e.Val(AttrByteSize).(int64) 669 if !ok { 670 b = -1 671 } 672 typ.Common().ByteSize = b 673 } 674 return typ, nil 675 676 Error: 677 // If the parse fails, take the type out of the cache 678 // so that the next call with this offset doesn't hit 679 // the cache and return success. 680 delete(typeCache, off) 681 return nil, err 682 } 683 684 func zeroArray(t *Type) { 685 if t == nil { 686 return 687 } 688 at, ok := (*t).(*ArrayType) 689 if !ok || at.Type.Size() == 0 { 690 return 691 } 692 // Make a copy to avoid invalidating typeCache. 693 tt := *at 694 tt.Count = 0 695 *t = &tt 696 }