Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 

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