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