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 // Package dwarf provides access to DWARF debugging information loaded from
6 // executable files, as defined in the DWARF 2.0 Standard at
7 // http://dwarfstd.org/doc/dwarf-2.0.0.pdf
10 import "encoding/binary"
12 // Data represents the DWARF debugging information
13 // loaded from an executable file (for example, an ELF or Mach-O executable).
26 abbrevCache
map[uint64]abbrevTable
27 order binary
.ByteOrder
28 typeCache
map[Offset
]Type
29 typeSigs
map[uint64]*typeUnit
33 // New returns a new Data object initialized from the given parameters.
34 // Rather than calling this function directly, clients should typically use
35 // the DWARF method of the File type of the appropriate package debug/elf,
36 // debug/macho, debug/pe, or debug/xcoff.
38 // The []byte arguments are the data from the corresponding debug section
39 // in the object file; for example, for an ELF object, abbrev is the contents of
40 // the ".debug_abbrev" section.
41 func New(abbrev
, aranges
, frame
, info
, line
, pubnames
, ranges
, str
[]byte) (*Data
, error
) {
51 abbrevCache
: make(map[uint64]abbrevTable
),
52 typeCache
: make(map[Offset
]Type
),
53 typeSigs
: make(map[uint64]*typeUnit
),
56 // Sniff .debug_info to figure out byte order.
57 // 32-bit DWARF: 4 byte length, 2 byte version.
58 // 64-bit DWARf: 4 bytes of 0xff, 8 byte length, 2 byte version.
60 return nil, DecodeError
{"info", Offset(len(d
.info
)), "too short"}
63 if d
.info
[0] == 0xff && d
.info
[1] == 0xff && d
.info
[2] == 0xff && d
.info
[3] == 0xff {
65 return nil, DecodeError
{"info", Offset(len(d
.info
)), "too short"}
69 // Fetch the version, a tiny 16-bit number (1, 2, 3, 4, 5).
70 x
, y
:= d
.info
[offset
], d
.info
[offset
+1]
72 case x
== 0 && y
== 0:
73 return nil, DecodeError
{"info", 4, "unsupported version 0"}
75 d
.order
= binary
.BigEndian
77 d
.order
= binary
.LittleEndian
79 return nil, DecodeError
{"info", 4, "cannot determine byte order"}
82 u
, err
:= d
.parseUnits()
90 // AddTypes will add one .debug_types section to the DWARF data. A
91 // typical object with DWARF version 4 debug info will have multiple
92 // .debug_types sections. The name is used for error reporting only,
93 // and serves to distinguish one .debug_types section from another.
94 func (d
*Data
) AddTypes(name
string, types
[]byte) error
{
95 return d
.parseTypes(name
, types
)