-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathdwarf.go
More file actions
166 lines (147 loc) · 3.89 KB
/
Copy pathdwarf.go
File metadata and controls
166 lines (147 loc) · 3.89 KB
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
// Copyright (C) 2019-2026 GoRE Authors.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package gore
import (
"bytes"
"debug/dwarf"
"encoding/binary"
)
const (
// official DWARF language ID for Go
// https://dwarfstd.org/languages.html
dwLangGo int64 = 0x0016
// DWARF operation; used to encode type offsets
dwOpAddr = 0x03
)
func getGoRootFromDwarf(fh fileHandler) (string, bool) {
return getDwarfString(fh, getDwarfStringCheck("runtime.defaultGOROOT"))
}
func getBuildVersionFromDwarf(fh fileHandler) (string, bool) {
return getDwarfString(fh, getDwarfStringCheck("runtime.buildVersion"))
}
// DWARF entry plus any associated children
type dwarfEntryPlus struct {
entry *dwarf.Entry
children []*dwarfEntryPlus
}
type dwarfwalkStatus uint8
const (
dwStop dwarfwalkStatus = iota + 1
dwContinue
dwFound
)
func getDwarfString(fh fileHandler, check func(fh fileHandler, entry *dwarfEntryPlus) (string, dwarfwalkStatus)) (string, bool) {
data, err := fh.getDwarf()
if err != nil {
return "", false
}
r := data.Reader()
// walk through compilation units
getValOuter:
for cu := dwarfReadEntry(r); cu != nil; cu = dwarfReadEntry(r) {
if langField := cu.entry.AttrField(dwarf.AttrLanguage); langField == nil || langField.Val != dwLangGo {
continue
}
getValInner:
for _, entry := range cu.children {
ret, status := check(fh, entry)
switch status {
case dwStop:
break getValOuter
case dwFound:
return ret, true
case dwContinue:
continue getValInner
}
}
}
return "", false
}
// get, by name, a DWARF entry corresponding to a string constant
func getDwarfStringCheck(name string) func(fh fileHandler, entry *dwarfEntryPlus) (string, dwarfwalkStatus) {
return func(fh fileHandler, d *dwarfEntryPlus) (string, dwarfwalkStatus) {
entry := d.entry
nameField := entry.AttrField(dwarf.AttrName)
if nameField == nil {
return "", dwContinue
}
if fieldName := nameField.Val.(string); fieldName != name {
return "", dwContinue
}
return commonStringCheck(fh, entry)
}
}
func commonStringCheck(fh fileHandler, entry *dwarf.Entry) (string, dwarfwalkStatus) {
locationField := entry.AttrField(dwarf.AttrLocation)
if locationField == nil {
// unexpected failure
return "", dwStop
}
location := locationField.Val.([]byte)
// DWARF address operation followed by the machine byte order encoded address
if location[0] != dwOpAddr {
return "", dwStop
}
var addr uint64
if fh.getFileInfo().WordSize == intSize32 {
addr = uint64(fh.getFileInfo().ByteOrder.Uint32(location[1:]))
} else {
addr = fh.getFileInfo().ByteOrder.Uint64(location[1:])
}
sectionBase, data, err := fh.getSectionDataFromAddress(addr)
if err != nil {
return "", dwStop
}
off := addr - sectionBase
r := bytes.NewReader(data[off:])
var stringData [2]uint64
if fh.getFileInfo().WordSize == intSize32 {
var stringData32 [2]uint32
err = binary.Read(r, fh.getFileInfo().ByteOrder, &stringData32)
if err != nil {
return "", dwStop
}
stringData[0] = uint64(stringData32[0])
stringData[1] = uint64(stringData32[1])
} else {
err = binary.Read(r, fh.getFileInfo().ByteOrder, &stringData)
if err != nil {
return "", dwStop
}
}
addr = stringData[0]
stringLen := stringData[1]
sectionBase, data, err = fh.getSectionDataFromAddress(addr)
if err != nil {
return "", dwStop
}
off = addr - sectionBase
raw := data[off : off+stringLen]
return string(raw), dwFound
}
func dwarfReadEntry(r *dwarf.Reader) *dwarfEntryPlus {
entry, _ := r.Next()
if entry == nil {
return nil
}
var children []*dwarfEntryPlus
if entry.Children {
children = dwarfReadChildren(r)
}
return &dwarfEntryPlus{
entry: entry,
children: children,
}
}
func dwarfReadChildren(r *dwarf.Reader) []*dwarfEntryPlus {
var ret []*dwarfEntryPlus
for {
e := dwarfReadEntry(r)
if e.entry.Tag == 0 {
return ret
}
ret = append(ret, e)
}
}