You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

182 lines
4.3 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. package dwarf
  15. import (
  16. "fmt"
  17. "strconv"
  18. )
  19. // Parse the type units stored in a DWARF4 .debug_types section. Each
  20. // type unit defines a single primary type and an 8-byte signature.
  21. // Other sections may then use formRefSig8 to refer to the type.
  22. // The typeUnit format is a single type with a signature. It holds
  23. // the same data as a compilation unit.
  24. type typeUnit struct {
  25. unit
  26. toff Offset // Offset to signature type within data.
  27. name string // Name of .debug_type section.
  28. cache Type // Cache the type, nil to start.
  29. }
  30. // Parse a .debug_types section.
  31. func (d *Data) parseTypes(name string, types []byte) error {
  32. b := makeBuf(d, unknownFormat{}, name, 0, types)
  33. for len(b.data) > 0 {
  34. base := b.off
  35. dwarf64 := false
  36. n := b.uint32()
  37. if n == 0xffffffff {
  38. n64 := b.uint64()
  39. if n64 != uint64(uint32(n64)) {
  40. b.error("type unit length overflow")
  41. return b.err
  42. }
  43. n = uint32(n64)
  44. dwarf64 = true
  45. }
  46. hdroff := b.off
  47. vers := b.uint16()
  48. if vers != 4 {
  49. b.error("unsupported DWARF version " + strconv.Itoa(int(vers)))
  50. return b.err
  51. }
  52. var ao uint32
  53. if !dwarf64 {
  54. ao = b.uint32()
  55. } else {
  56. ao64 := b.uint64()
  57. if ao64 != uint64(uint32(ao64)) {
  58. b.error("type unit abbrev offset overflow")
  59. return b.err
  60. }
  61. ao = uint32(ao64)
  62. }
  63. atable, err := d.parseAbbrev(ao)
  64. if err != nil {
  65. return err
  66. }
  67. asize := b.uint8()
  68. sig := b.uint64()
  69. var toff uint32
  70. if !dwarf64 {
  71. toff = b.uint32()
  72. } else {
  73. to64 := b.uint64()
  74. if to64 != uint64(uint32(to64)) {
  75. b.error("type unit type offset overflow")
  76. return b.err
  77. }
  78. toff = uint32(to64)
  79. }
  80. boff := b.off
  81. d.typeSigs[sig] = &typeUnit{
  82. unit: unit{
  83. base: base,
  84. off: boff,
  85. data: b.bytes(int(Offset(n) - (b.off - hdroff))),
  86. atable: atable,
  87. asize: int(asize),
  88. vers: int(vers),
  89. is64: dwarf64,
  90. },
  91. toff: Offset(toff),
  92. name: name,
  93. }
  94. if b.err != nil {
  95. return b.err
  96. }
  97. }
  98. return nil
  99. }
  100. // Return the type for a type signature.
  101. func (d *Data) sigToType(sig uint64) (Type, error) {
  102. tu := d.typeSigs[sig]
  103. if tu == nil {
  104. return nil, fmt.Errorf("no type unit with signature %v", sig)
  105. }
  106. if tu.cache != nil {
  107. return tu.cache, nil
  108. }
  109. b := makeBuf(d, tu, tu.name, tu.off, tu.data)
  110. r := &typeUnitReader{d: d, tu: tu, b: b}
  111. t, err := d.readType(tu.name, r, Offset(tu.toff), make(map[Offset]Type))
  112. if err != nil {
  113. return nil, err
  114. }
  115. tu.cache = t
  116. return t, nil
  117. }
  118. // typeUnitReader is a typeReader for a tagTypeUnit.
  119. type typeUnitReader struct {
  120. d *Data
  121. tu *typeUnit
  122. b buf
  123. err error
  124. }
  125. // Seek to a new position in the type unit.
  126. func (tur *typeUnitReader) Seek(off Offset) {
  127. tur.err = nil
  128. doff := off - tur.tu.off
  129. if doff < 0 || doff >= Offset(len(tur.tu.data)) {
  130. tur.err = fmt.Errorf("%s: offset %d out of range; max %d", tur.tu.name, doff, len(tur.tu.data))
  131. return
  132. }
  133. tur.b = makeBuf(tur.d, tur.tu, tur.tu.name, off, tur.tu.data[doff:])
  134. }
  135. // AddressSize returns the size in bytes of addresses in the current type unit.
  136. func (tur *typeUnitReader) AddressSize() int {
  137. return tur.tu.unit.asize
  138. }
  139. // Next reads the next Entry from the type unit.
  140. func (tur *typeUnitReader) Next() (*Entry, error) {
  141. if tur.err != nil {
  142. return nil, tur.err
  143. }
  144. if len(tur.tu.data) == 0 {
  145. return nil, nil
  146. }
  147. e := tur.b.entry(tur.tu.atable, tur.tu.base)
  148. if tur.b.err != nil {
  149. tur.err = tur.b.err
  150. return nil, tur.err
  151. }
  152. return e, nil
  153. }
  154. // clone returns a new reader for the type unit.
  155. func (tur *typeUnitReader) clone() typeReader {
  156. return &typeUnitReader{
  157. d: tur.d,
  158. tu: tur.tu,
  159. b: makeBuf(tur.d, tur.tu, tur.tu.name, tur.tu.off, tur.tu.data),
  160. }
  161. }
  162. // offset returns the current offset.
  163. func (tur *typeUnitReader) offset() Offset {
  164. return tur.b.off
  165. }