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
15 // Data represents the DWARF debugging information
16 // loaded from an executable file (for example, an ELF or Mach-O executable).
28 // New sections added in DWARF 5.
35 abbrevCache
map[uint64]abbrevTable
37 order binary
.ByteOrder
38 typeCache
map[Offset
]Type
39 typeSigs
map[uint64]*typeUnit
43 var errSegmentSelector
= errors
.New("non-zero segment_selector size not supported")
45 // New returns a new Data object initialized from the given parameters.
46 // Rather than calling this function directly, clients should typically use
47 // the DWARF method of the File type of the appropriate package debug/elf,
48 // debug/macho, debug/pe, or debug/xcoff.
50 // The []byte arguments are the data from the corresponding debug section
51 // in the object file; for example, for an ELF object, abbrev is the contents of
52 // the ".debug_abbrev" section.
53 func New(abbrev
, aranges
, frame
, info
, line
, pubnames
, ranges
, str
[]byte) (*Data
, error
) {
63 abbrevCache
: make(map[uint64]abbrevTable
),
64 typeCache
: make(map[Offset
]Type
),
65 typeSigs
: make(map[uint64]*typeUnit
),
68 // Sniff .debug_info to figure out byte order.
69 // 32-bit DWARF: 4 byte length, 2 byte version.
70 // 64-bit DWARf: 4 bytes of 0xff, 8 byte length, 2 byte version.
72 return nil, DecodeError
{"info", Offset(len(d
.info
)), "too short"}
75 if d
.info
[0] == 0xff && d
.info
[1] == 0xff && d
.info
[2] == 0xff && d
.info
[3] == 0xff {
77 return nil, DecodeError
{"info", Offset(len(d
.info
)), "too short"}
81 // Fetch the version, a tiny 16-bit number (1, 2, 3, 4, 5).
82 x
, y
:= d
.info
[offset
], d
.info
[offset
+1]
84 case x
== 0 && y
== 0:
85 return nil, DecodeError
{"info", 4, "unsupported version 0"}
88 d
.order
= binary
.BigEndian
91 d
.order
= binary
.LittleEndian
93 return nil, DecodeError
{"info", 4, "cannot determine byte order"}
96 u
, err
:= d
.parseUnits()
104 // AddTypes will add one .debug_types section to the DWARF data. A
105 // typical object with DWARF version 4 debug info will have multiple
106 // .debug_types sections. The name is used for error reporting only,
107 // and serves to distinguish one .debug_types section from another.
108 func (d
*Data
) AddTypes(name
string, types
[]byte) error
{
109 return d
.parseTypes(name
, types
)
112 // AddSection adds another DWARF section by name. The name should be a
113 // DWARF section name such as ".debug_addr", ".debug_str_offsets", and
114 // so forth. This approach is used for new DWARF sections added in
115 // DWARF 5 and later.
116 func (d
*Data
) AddSection(name
string, contents
[]byte) error
{
121 case ".debug_line_str":
123 case ".debug_str_offsets":
124 d
.strOffsets
= contents
125 case ".debug_rnglists":
126 d
.rngLists
= contents
128 // Just ignore names that we don't yet support.