Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 

878 строки
21 KiB

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