...
1
2
3
4
5
6
7
8 package dwarf
9
10 import (
11 "encoding/binary"
12 "errors"
13 )
14
15
16
17 type Data struct {
18
19 abbrev []byte
20 aranges []byte
21 frame []byte
22 info []byte
23 line []byte
24 pubnames []byte
25 ranges []byte
26 str []byte
27
28
29 addr []byte
30 lineStr []byte
31 strOffsets []byte
32 rngLists []byte
33
34
35 abbrevCache map[uint64]abbrevTable
36 bigEndian bool
37 order binary.ByteOrder
38 typeCache map[Offset]Type
39 typeSigs map[uint64]*typeUnit
40 unit []unit
41 }
42
43 var errSegmentSelector = errors.New("non-zero segment_selector size not supported")
44
45
46
47
48
49
50
51
52
53 func New(abbrev, aranges, frame, info, line, pubnames, ranges, str []byte) (*Data, error) {
54 d := &Data{
55 abbrev: abbrev,
56 aranges: aranges,
57 frame: frame,
58 info: info,
59 line: line,
60 pubnames: pubnames,
61 ranges: ranges,
62 str: str,
63 abbrevCache: make(map[uint64]abbrevTable),
64 typeCache: make(map[Offset]Type),
65 typeSigs: make(map[uint64]*typeUnit),
66 }
67
68
69
70
71 if len(d.info) < 6 {
72 return nil, DecodeError{"info", Offset(len(d.info)), "too short"}
73 }
74 offset := 4
75 if d.info[0] == 0xff && d.info[1] == 0xff && d.info[2] == 0xff && d.info[3] == 0xff {
76 if len(d.info) < 14 {
77 return nil, DecodeError{"info", Offset(len(d.info)), "too short"}
78 }
79 offset = 12
80 }
81
82 x, y := d.info[offset], d.info[offset+1]
83 switch {
84 case x == 0 && y == 0:
85 return nil, DecodeError{"info", 4, "unsupported version 0"}
86 case x == 0:
87 d.bigEndian = true
88 d.order = binary.BigEndian
89 case y == 0:
90 d.bigEndian = false
91 d.order = binary.LittleEndian
92 default:
93 return nil, DecodeError{"info", 4, "cannot determine byte order"}
94 }
95
96 u, err := d.parseUnits()
97 if err != nil {
98 return nil, err
99 }
100 d.unit = u
101 return d, nil
102 }
103
104
105
106
107
108 func (d *Data) AddTypes(name string, types []byte) error {
109 return d.parseTypes(name, types)
110 }
111
112
113
114
115
116 func (d *Data) AddSection(name string, contents []byte) error {
117 var err error
118 switch name {
119 case ".debug_addr":
120 d.addr = contents
121 case ".debug_line_str":
122 d.lineStr = contents
123 case ".debug_str_offsets":
124 d.strOffsets = contents
125 case ".debug_rnglists":
126 d.rngLists = contents
127 }
128
129 return err
130 }
131
View as plain text