buf.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. // Buffered reading and decoding of DWARF data streams.
  5. package dwarf
  6. import (
  7. "bytes"
  8. "encoding/binary"
  9. "strconv"
  10. )
  11. // Data buffer being decoded.
  12. type buf struct {
  13. dwarf *Data
  14. order binary.ByteOrder
  15. format dataFormat
  16. name string
  17. off Offset
  18. data []byte
  19. err error
  20. }
  21. // Data format, other than byte order. This affects the handling of
  22. // certain field formats.
  23. type dataFormat interface {
  24. // DWARF version number. Zero means unknown.
  25. version() int
  26. // 64-bit DWARF format?
  27. dwarf64() (dwarf64 bool, isKnown bool)
  28. // Size of an address, in bytes. Zero means unknown.
  29. addrsize() int
  30. }
  31. // Some parts of DWARF have no data format, e.g., abbrevs.
  32. type unknownFormat struct{}
  33. func (u unknownFormat) version() int {
  34. return 0
  35. }
  36. func (u unknownFormat) dwarf64() (bool, bool) {
  37. return false, false
  38. }
  39. func (u unknownFormat) addrsize() int {
  40. return 0
  41. }
  42. func makeBuf(d *Data, format dataFormat, name string, off Offset, data []byte) buf {
  43. return buf{d, d.order, format, name, off, data, nil}
  44. }
  45. func (b *buf) uint8() uint8 {
  46. if len(b.data) < 1 {
  47. b.error("underflow")
  48. return 0
  49. }
  50. val := b.data[0]
  51. b.data = b.data[1:]
  52. b.off++
  53. return val
  54. }
  55. func (b *buf) bytes(n int) []byte {
  56. if len(b.data) < n {
  57. b.error("underflow")
  58. return nil
  59. }
  60. data := b.data[0:n]
  61. b.data = b.data[n:]
  62. b.off += Offset(n)
  63. return data
  64. }
  65. func (b *buf) skip(n int) { b.bytes(n) }
  66. func (b *buf) string() string {
  67. i := bytes.IndexByte(b.data, 0)
  68. if i < 0 {
  69. b.error("underflow")
  70. return ""
  71. }
  72. s := string(b.data[0:i])
  73. b.data = b.data[i+1:]
  74. b.off += Offset(i + 1)
  75. return s
  76. }
  77. func (b *buf) uint16() uint16 {
  78. a := b.bytes(2)
  79. if a == nil {
  80. return 0
  81. }
  82. return b.order.Uint16(a)
  83. }
  84. func (b *buf) uint24() uint32 {
  85. a := b.bytes(3)
  86. if a == nil {
  87. return 0
  88. }
  89. if b.dwarf.bigEndian {
  90. return uint32(a[2]) | uint32(a[1])<<8 | uint32(a[0])<<16
  91. } else {
  92. return uint32(a[0]) | uint32(a[1])<<8 | uint32(a[2])<<16
  93. }
  94. }
  95. func (b *buf) uint32() uint32 {
  96. a := b.bytes(4)
  97. if a == nil {
  98. return 0
  99. }
  100. return b.order.Uint32(a)
  101. }
  102. func (b *buf) uint64() uint64 {
  103. a := b.bytes(8)
  104. if a == nil {
  105. return 0
  106. }
  107. return b.order.Uint64(a)
  108. }
  109. // Read a varint, which is 7 bits per byte, little endian.
  110. // the 0x80 bit means read another byte.
  111. func (b *buf) varint() (c uint64, bits uint) {
  112. for i := 0; i < len(b.data); i++ {
  113. byte := b.data[i]
  114. c |= uint64(byte&0x7F) << bits
  115. bits += 7
  116. if byte&0x80 == 0 {
  117. b.off += Offset(i + 1)
  118. b.data = b.data[i+1:]
  119. return c, bits
  120. }
  121. }
  122. return 0, 0
  123. }
  124. // Unsigned int is just a varint.
  125. func (b *buf) uint() uint64 {
  126. x, _ := b.varint()
  127. return x
  128. }
  129. // Signed int is a sign-extended varint.
  130. func (b *buf) int() int64 {
  131. ux, bits := b.varint()
  132. x := int64(ux)
  133. if x&(1<<(bits-1)) != 0 {
  134. x |= -1 << bits
  135. }
  136. return x
  137. }
  138. // Address-sized uint.
  139. func (b *buf) addr() uint64 {
  140. switch b.format.addrsize() {
  141. case 1:
  142. return uint64(b.uint8())
  143. case 2:
  144. return uint64(b.uint16())
  145. case 4:
  146. return uint64(b.uint32())
  147. case 8:
  148. return b.uint64()
  149. }
  150. b.error("unknown address size")
  151. return 0
  152. }
  153. func (b *buf) unitLength() (length Offset, dwarf64 bool) {
  154. length = Offset(b.uint32())
  155. if length == 0xffffffff {
  156. dwarf64 = true
  157. length = Offset(b.uint64())
  158. } else if length >= 0xfffffff0 {
  159. b.error("unit length has reserved value")
  160. }
  161. return
  162. }
  163. func (b *buf) error(s string) {
  164. if b.err == nil {
  165. b.data = nil
  166. b.err = DecodeError{b.name, b.off, s}
  167. }
  168. }
  169. type DecodeError struct {
  170. Name string
  171. Offset Offset
  172. Err string
  173. }
  174. func (e DecodeError) Error() string {
  175. return "decoding dwarf section " + e.Name + " at offset 0x" + strconv.FormatInt(int64(e.Offset), 16) + ": " + e.Err
  176. }