forked from aquasecurity/libbpfgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
elf.go
61 lines (53 loc) · 1.12 KB
/
elf.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
package libbpfgo
import (
"debug/elf"
"encoding/binary"
"errors"
"strings"
)
type Symbol struct {
name string
size int
offset int
sectionName string
byteOrder binary.ByteOrder
}
func getGlobalVariableSymbol(elf *elf.File, varName string) (*Symbol, error) {
regularSymbols, err := elf.Symbols()
if err != nil {
return nil, err
}
var symbols []Symbol
for _, s := range regularSymbols {
i := int(s.Section)
if i >= len(elf.Sections) {
continue
}
sectionName := elf.Sections[i].Name
if isGlobalVariableSection(sectionName) {
symbols = append(symbols, Symbol{
name: s.Name,
size: int(s.Size),
offset: int(s.Value),
sectionName: sectionName,
byteOrder: elf.ByteOrder,
})
}
}
for _, s := range symbols {
if s.name == varName {
return &s, nil
}
}
return nil, errors.New("symbol not found")
}
func isGlobalVariableSection(sectionName string) bool {
if sectionName == ".data" || sectionName == ".rodata" {
return true
}
if strings.HasPrefix(sectionName, ".data.") ||
strings.HasPrefix(sectionName, ".rodata.") {
return true
}
return false
}