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.
5 // Buffered reading and decoding of DWARF data streams.
14 // Data buffer being decoded.
17 order binary
.ByteOrder
25 // Data format, other than byte order. This affects the handling of
26 // certain field formats.
27 type dataFormat
interface {
28 // DWARF version number. Zero means unknown.
31 // 64-bit DWARF format?
32 dwarf64() (dwarf64
bool, isKnown
bool)
34 // Size of an address, in bytes. Zero means unknown.
38 // Some parts of DWARF have no data format, e.g., abbrevs.
39 type unknownFormat
struct{}
41 func (u unknownFormat
) version() int {
45 func (u unknownFormat
) dwarf64() (bool, bool) {
49 func (u unknownFormat
) addrsize() int {
53 func makeBuf(d
*Data
, format dataFormat
, name
string, off Offset
, data
[]byte) buf
{
54 return buf
{d
, d
.order
, format
, name
, off
, data
, nil}
57 func (b
*buf
) uint8() uint8 {
68 func (b
*buf
) bytes(n
int) []byte {
79 func (b
*buf
) skip(n
int) { b
.bytes(n
) }
81 func (b
*buf
) string() string {
82 for i
:= 0; i
< len(b
.data
); i
++ {
84 s
:= string(b
.data
[0:i
])
86 b
.off
+= Offset(i
+ 1)
94 func (b
*buf
) uint16() uint16 {
99 return b
.order
.Uint16(a
)
102 func (b
*buf
) uint32() uint32 {
107 return b
.order
.Uint32(a
)
110 func (b
*buf
) uint64() uint64 {
115 return b
.order
.Uint64(a
)
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
++ {
123 c |
= uint64(byte&0x7F) << bits
126 b
.off
+= Offset(i
+ 1)
127 b
.data
= b
.data
[i
+1:]
134 // Unsigned int is just a varint.
135 func (b
*buf
) uint() uint64 {
140 // Signed int is a sign-extended varint.
141 func (b
*buf
) int() int64 {
142 ux
, bits
:= b
.varint()
144 if x
&(1<<(bits
-1)) != 0 {
150 // Address-sized uint.
151 func (b
*buf
) addr() uint64 {
152 switch b
.format
.addrsize() {
154 return uint64(b
.uint8())
156 return uint64(b
.uint16())
158 return uint64(b
.uint32())
162 b
.error("unknown address size")
166 func (b
*buf
) unitLength() (length Offset
, dwarf64
bool) {
167 length
= Offset(b
.uint32())
168 if length
== 0xffffffff {
170 length
= Offset(b
.uint64())
171 } else if length
>= 0xfffffff0 {
172 b
.error("unit length has reserved value")
177 func (b
*buf
) error(s
string) {
180 b
.err
= DecodeError
{b
.name
, b
.off
, s
}
184 type DecodeError
struct {
190 func (e DecodeError
) Error() string {
191 return "decoding dwarf section " + e
.Name
+ " at offset 0x" + strconv
.FormatInt(int64(e
.Offset
), 16) + ": " + e
.Err