-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
tree.go
263 lines (230 loc) · 5.53 KB
/
tree.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
package godwarf
import (
"debug/dwarf"
"fmt"
"sort"
)
// Entry represents a debug_info entry.
// When calling Val, if the entry does not have the specified attribute, the
// entry specified by DW_AT_abstract_origin will be searched recursively.
type Entry interface {
Val(dwarf.Attr) interface{}
}
type compositeEntry []*dwarf.Entry
func (ce compositeEntry) Val(attr dwarf.Attr) interface{} {
for _, e := range ce {
if r := e.Val(attr); r != nil {
return r
}
}
return nil
}
// LoadAbstractOrigin loads the entry corresponding to the
// DW_AT_abstract_origin of entry and returns a combination of entry and its
// abstract origin.
func LoadAbstractOrigin(entry *dwarf.Entry, aordr *dwarf.Reader) (Entry, dwarf.Offset) {
ao, ok := entry.Val(dwarf.AttrAbstractOrigin).(dwarf.Offset)
if !ok {
return entry, entry.Offset
}
r := []*dwarf.Entry{entry}
for {
aordr.Seek(ao)
e, _ := aordr.Next()
if e == nil {
break
}
r = append(r, e)
ao, ok = e.Val(dwarf.AttrAbstractOrigin).(dwarf.Offset)
if !ok {
break
}
}
return compositeEntry(r), entry.Offset
}
// Tree represents a tree of dwarf objects.
type Tree struct {
Entry
typ Type
Tag dwarf.Tag
Offset dwarf.Offset
Ranges [][2]uint64
Children []*Tree
}
// LoadTree returns the tree of DIE rooted at offset 'off'.
// Abstract origins are automatically loaded, if present.
// DIE ranges are bubbled up automatically, if the child of a DIE covers a
// range of addresses that is not covered by its parent LoadTree will fix
// the parent entry.
func LoadTree(off dwarf.Offset, dw *dwarf.Data, staticBase uint64) (*Tree, error) {
rdr := dw.Reader()
rdr.Seek(off)
e, err := rdr.Next()
if err != nil {
return nil, err
}
r := entryToTreeInternal(e)
r.Children, err = loadTreeChildren(e, rdr)
if err != nil {
return nil, err
}
err = r.resolveRanges(dw, staticBase)
if err != nil {
return nil, err
}
r.resolveAbstractEntries(rdr)
return r, nil
}
// EntryToTree converts a single entry, without children, to a *Tree object.
func EntryToTree(entry *dwarf.Entry) *Tree {
if entry.Children {
panic(fmt.Sprintf("EntryToTree called on entry with children; " +
"LoadTree should have been used instead. entry: %+v", entry))
}
return entryToTreeInternal(entry)
}
func entryToTreeInternal(entry *dwarf.Entry) *Tree {
return &Tree{Entry: entry, Offset: entry.Offset, Tag: entry.Tag}
}
func loadTreeChildren(e *dwarf.Entry, rdr *dwarf.Reader) ([]*Tree, error) {
if !e.Children {
return nil, nil
}
children := []*Tree{}
for {
e, err := rdr.Next()
if err != nil {
return nil, err
}
if e.Tag == 0 {
break
}
child := entryToTreeInternal(e)
child.Children, err = loadTreeChildren(e, rdr)
if err != nil {
return nil, err
}
children = append(children, child)
}
return children, nil
}
func (n *Tree) resolveRanges(dw *dwarf.Data, staticBase uint64) error {
var err error
n.Ranges, err = dw.Ranges(n.Entry.(*dwarf.Entry))
if err != nil {
return err
}
for i := range n.Ranges {
n.Ranges[i][0] += staticBase
n.Ranges[i][1] += staticBase
}
n.Ranges = normalizeRanges(n.Ranges)
for _, child := range n.Children {
err := child.resolveRanges(dw, staticBase)
if err != nil {
return err
}
n.Ranges = fuseRanges(n.Ranges, child.Ranges)
}
return nil
}
// normalizeRanges sorts rngs by starting point and fuses overlapping entries.
func normalizeRanges(rngs [][2]uint64) [][2]uint64 {
const (
start = 0
end = 1
)
if len(rngs) == 0 {
return rngs
}
sort.Slice(rngs, func(i, j int) bool {
return rngs[i][start] <= rngs[j][start]
})
// eliminate invalid entries
out := rngs[:0]
for i := range rngs {
if rngs[i][start] < rngs[i][end] {
out = append(out, rngs[i])
}
}
rngs = out
// fuse overlapping entries
out = rngs[:1]
for i := 1; i < len(rngs); i++ {
cur := rngs[i]
if cur[start] <= out[len(out)-1][end] {
out[len(out)-1][end] = max(cur[end], out[len(out)-1][end])
} else {
out = append(out, cur)
}
}
return out
}
func max(a, b uint64) uint64 {
if a > b {
return a
}
return b
}
// fuseRanges fuses rngs2 into rngs1, it's the equivalent of
// normalizeRanges(append(rngs1, rngs2))
// but more efficent.
func fuseRanges(rngs1, rngs2 [][2]uint64) [][2]uint64 {
if rangesContains(rngs1, rngs2) {
return rngs1
}
return normalizeRanges(append(rngs1, rngs2...))
}
// rangesContains checks that rngs1 is a superset of rngs2.
func rangesContains(rngs1, rngs2 [][2]uint64) bool {
i, j := 0, 0
for {
if i >= len(rngs1) {
return false
}
if j >= len(rngs2) {
return true
}
if rangeContains(rngs1[i], rngs2[j]) {
j++
} else {
i++
}
}
}
// rangeContains checks that a contains b.
func rangeContains(a, b [2]uint64) bool {
return a[0] <= b[0] && a[1] >= b[1]
}
func (n *Tree) resolveAbstractEntries(rdr *dwarf.Reader) {
n.Entry, n.Offset = LoadAbstractOrigin(n.Entry.(*dwarf.Entry), rdr)
for _, child := range n.Children {
child.resolveAbstractEntries(rdr)
}
}
// ContainsPC returns true if the ranges of this DIE contains PC.
func (n *Tree) ContainsPC(pc uint64) bool {
for _, rng := range n.Ranges {
if rng[0] > pc {
return false
}
if rng[0] <= pc && pc < rng[1] {
return true
}
}
return false
}
func (n *Tree) Type(dw *dwarf.Data, index int, typeCache map[dwarf.Offset]Type) (Type, error) {
if n.typ == nil {
offset, ok := n.Val(dwarf.AttrType).(dwarf.Offset)
if !ok {
return nil, fmt.Errorf("malformed variable DIE (offset)")
}
var err error
n.typ, err = ReadType(dw, index, offset, typeCache)
if err != nil {
return nil, err
}
}
return n.typ, nil
}