This repository has been archived by the owner on Jul 19, 2023. It is now read-only.
/
go_table.go
181 lines (153 loc) 路 4.11 KB
/
go_table.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
package elf
import (
"debug/elf"
"errors"
"fmt"
gosym2 "github.com/grafana/phlare/ebpf/symtab/gosym"
)
type GoTable struct {
Index gosym2.FlatFuncIndex
File *MMapedElfFile
gopclnSection elf.SectionHeader
funcNameOffset uint64
}
func (g *GoTable) IsDead() bool {
return g.File.err != nil
}
func (g *GoTable) DebugInfo() SymTabDebugInfo {
return SymTabDebugInfo{
Name: "GoTable",
Size: len(g.Index.Name),
File: g.File.fpath,
}
}
func (g *GoTable) Size() int {
return len(g.Index.Name)
}
func (g *GoTable) Refresh() {
}
func (g *GoTable) Resolve(addr uint64) string {
n := len(g.Index.Name)
if n == 0 {
return ""
}
if addr >= g.Index.End {
return ""
}
i := g.Index.Entry.FindIndex(addr)
if i == -1 {
return ""
}
name, _ := g.goSymbolName(i)
return name
}
func (g *GoTable) Cleanup() {
g.File.Close()
}
var (
errEmptyText = errors.New("empty .text")
errGoPCLNTabNotFound = errors.New(".gopclntab not found")
errGoTooOld = errors.New("gosymtab: go sym tab too old")
errGoParseFailed = errors.New("gosymtab: go sym tab parse failed")
errGoFailed = errors.New("gosymtab: go sym tab failed")
errGoOOB = fmt.Errorf("go table oob")
errGoSymbolsNotFound = errors.New("gosymtab: no go symbols found")
)
func (f *MMapedElfFile) NewGoTable() (*GoTable, error) {
obj := f
var err error
text := obj.Section(".text")
if text == nil {
return nil, errEmptyText
}
pclntab := obj.Section(".gopclntab")
if pclntab == nil {
return nil, errGoPCLNTabNotFound
}
if f.fd == nil {
return nil, fmt.Errorf("elf file not open")
}
pclntabReader := gosym2.NewFilePCLNData(f.fd, int(pclntab.Offset))
pclntabHeader := make([]byte, 64)
if err = pclntabReader.ReadAt(pclntabHeader, 0); err != nil {
return nil, err
}
textStart := gosym2.ParseRuntimeTextFromPclntab18(pclntabHeader)
if textStart == 0 {
// for older versions text.Addr is enough
// https://github.com/golang/go/commit/b38ab0ac5f78ac03a38052018ff629c03e36b864
textStart = text.Addr
}
if textStart < text.Addr || textStart >= text.Addr+text.Size {
return nil, fmt.Errorf(" runtime.text out of .text bounds %d %d %d", textStart, text.Addr, text.Size)
}
pcln := gosym2.NewLineTableStreaming(pclntabReader, textStart)
if !pcln.IsGo12() {
return nil, errGoTooOld
}
if pcln.IsFailed() {
return nil, errGoParseFailed
}
funcs := pcln.Go12Funcs()
if len(funcs.Name) == 0 || funcs.Entry.Length() == 0 || funcs.End == 0 {
return nil, errGoSymbolsNotFound
}
//if funcs.Entry32 == nil && funcs.Entry64 == nil {
// return nil, errGoParseFailed // this should not happen
//}
//if funcs.Entry32 != nil && funcs.Entry64 != nil {
// return nil, errGoParseFailed // this should not happen
//}
if funcs.Entry.Length() != len(funcs.Name) {
return nil, errGoParseFailed // this should not happen
}
funcNameOffset := pcln.FuncNameOffset()
return &GoTable{
Index: funcs,
File: f,
gopclnSection: *pclntab,
funcNameOffset: funcNameOffset,
}, nil
}
func (g *GoTable) goSymbolName(idx int) (string, error) {
offsetGpcln := g.gopclnSection.Offset
if idx >= len(g.Index.Name) {
return "", errGoOOB
}
offsetName := g.Index.Name[idx]
name, ok := g.File.getString(int(offsetGpcln) + int(g.funcNameOffset) + int(offsetName))
if !ok {
return "", errGoFailed
}
return name, nil
}
type GoTableWithFallback struct {
GoTable *GoTable
SymTable *SymbolTable
}
func (g *GoTableWithFallback) IsDead() bool {
return g.GoTable.File.err != nil
}
func (g *GoTableWithFallback) DebugInfo() SymTabDebugInfo {
return SymTabDebugInfo{
Name: "GoTableWithFallback",
Size: g.GoTable.Size() + g.SymTable.Size(),
File: g.GoTable.File.fpath,
}
}
func (g *GoTableWithFallback) Size() int {
return g.GoTable.Size() + g.SymTable.Size()
}
func (g *GoTableWithFallback) Refresh() {
}
func (g *GoTableWithFallback) Resolve(addr uint64) string {
name := g.GoTable.Resolve(addr)
if name != "" {
return name
}
return g.SymTable.Resolve(addr)
}
func (g *GoTableWithFallback) Cleanup() {
g.GoTable.Cleanup()
g.SymTable.Cleanup() // second call is no op now, but call anyway just in case
}