/
symbols.go
257 lines (222 loc) · 7.17 KB
/
symbols.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
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package elf
import (
"bufio"
"debug/elf"
"encoding/binary"
"errors"
"fmt"
"io"
"sort"
"strings"
)
const (
dataSection = ".data"
mapSection = "maps"
btfMapSection = ".maps"
nullTerminator = byte(0)
relocSectionPrefix = ".rel"
// nMapRelocations is an approximation of the number of offsets in an
// ELF relating to map names that need to be adjusted. It's used for
// map initialization within 'Symbols'.
//
// $ readelf -a bpf/bpf_lxc.o | grep "cilium_.*_" | grep "^0000" | wc -l
// 51
nMapRelocations = 64
)
type symbolKind uint32
const (
symbolData = symbolKind(1)
symbolString = symbolKind(2)
)
func (k symbolKind) String() string {
switch k {
case symbolData:
return "data"
case symbolString:
return "string"
}
return "unknown"
}
// symbol stores the location and type of a symbol within the ELF file.
type symbol struct {
name string
kind symbolKind
offset uint64
offsetBTF uint64
size uint64
}
func newSymbol(name string, kind symbolKind, offset, size uint64) symbol {
return symbol{
name: name,
kind: kind,
offset: offset,
size: size,
}
}
func newVariable(name string, offset, size uint64) symbol {
return newSymbol(name, symbolData, offset, size)
}
func newString(name string, offset uint64) symbol {
return newSymbol(name, symbolString, offset, uint64(len(name)))
}
type symbolSlice []symbol
// sort a slice of symbols by offset.
func (c symbolSlice) sort() symbolSlice {
sort.Slice(c, func(i, j int) bool { return c[i].offset < c[j].offset })
return c
}
type symbols struct {
// data caches static 32-bit variables by name.
data map[string]symbol
// strings caches string symbols by name.
strings map[string]symbol
}
func (s *symbols) sort() symbolSlice {
result := make(symbolSlice, 0)
for _, c := range s.data {
result = append(result, c)
}
for _, c := range s.strings {
result = append(result, c)
}
return result.sort()
}
// isGlobalData returns true if the symbol meets all of the following criteria:
// - symbol is of type STT_NOTYPE or STT_OBJECT
// - symbol has a global binding (STB_GLOBAL)
// - symbol has default visibility (STV_DEFAULT)
func isGlobalData(sym elf.Symbol) bool {
return (elf.ST_TYPE(sym.Info) == elf.STT_NOTYPE ||
elf.ST_TYPE(sym.Info) == elf.STT_OBJECT) &&
elf.ST_BIND(sym.Info) == elf.STB_GLOBAL &&
elf.ST_VISIBILITY(sym.Other) == elf.STV_DEFAULT
}
func readStringOffset(e *elf.File, r io.ReadSeeker, symbolOffset int64) (uint64, error) {
if _, err := r.Seek(symbolOffset, io.SeekStart); err != nil {
return 0, err
}
switch e.Class {
case elf.ELFCLASS32:
var sym32 elf.Sym32
if err := binary.Read(r, e.ByteOrder, &sym32); err != nil {
return 0, err
}
return uint64(sym32.Name), nil
case elf.ELFCLASS64:
var sym64 elf.Sym64
if err := binary.Read(r, e.ByteOrder, &sym64); err != nil {
return 0, err
}
return uint64(sym64.Name), nil
}
return 0, fmt.Errorf("unsupported ELF type %d", e.Class)
}
// extractFrom processes the specified ELF and populates the received symbols
// object with data and string offsets in the file, for later substitution.
func (s *symbols) extractFrom(e *elf.File) error {
dataOffsets := make(map[string]symbol)
stringOffsets := make(map[string]symbol, nMapRelocations)
symbols, err := e.Symbols()
if err != nil {
return err
}
symtab := e.SectionByType(elf.SHT_SYMTAB)
strtab := e.Sections[symtab.Link]
// Scan symbol table for offsets of static data and symbol names.
symbolReader := symtab.Open()
for i, sym := range symbols {
// BTF extensions like line info not recognized by normal ELF parsers
if elf.ST_TYPE(sym.Info) == elf.STT_FILE {
continue
}
// Get ELF section reference by its index encoded in the symbol.
section := e.Sections[sym.Section]
switch {
case section.Flags&elf.SHF_COMPRESSED > 0:
return fmt.Errorf("compressed %s section not supported", section.Name)
// Skip local symbols that are objects or untyped. These are usually
// program segments referred to using long jumps in bytecode.
case !isGlobalData(sym):
// LBB is a common llvm symbol prefix (basic block);
// Don't flood the logs with messages about it.
if !strings.HasPrefix(sym.Name, "LBB") {
log.Debugf("Skipping %s", sym.Name)
}
continue
// Find the absolute offset to the variable's value this symbol represents
// in the .data section.
// This implements substitution of static data.
case section.Name == dataSection:
// Offset from start of binary to variable inside .data
offset := section.Offset + sym.Value
dataOffsets[sym.Name] = newVariable(sym.Name, offset, sym.Size)
log.WithField(fieldSymbol, sym.Name).Debugf("Found variable with offset %d", offset)
// Find the absolute offset to the map's entry in .strtab.
// This implements renaming maps.
case section.Name == mapSection || section.Name == btfMapSection:
// From the Golang Documentation:
// "For compatibility with Go 1.0, Symbols omits the
// the null symbol at index 0."
// We must reverse this when reading directly.
symbolOffset := int64(i+1) * int64(symtab.Entsize)
symOffsetInStrtab, err := readStringOffset(e, symbolReader, symbolOffset)
if err != nil {
return err
}
// Offset from start of binary to name inside .strtab
symOffset := strtab.Offset + symOffsetInStrtab
stringOffsets[sym.Name] = newString(sym.Name, symOffset)
log.WithField(fieldSymbol, sym.Name).Debugf("Found symbol with offset %d", symOffset)
default:
log.WithField(fieldSymbol, sym.Name).Debugf("Found symbol referring to unknown section id %d", sym.Section)
}
}
// Scan string table for offsets of section names.
stringReader := bufio.NewReader(strtab.Open())
var elfString string
for off := uint64(0); off < strtab.Size; off += uint64(len(elfString)) {
// off is the offset within the string table.
elfString, err = stringReader.ReadString(nullTerminator)
if errors.Is(err, io.EOF) {
break
}
if err != nil {
return err
}
// We only need to worry about sections with relocations.
if !strings.HasPrefix(elfString, relocSectionPrefix) {
continue
}
elfEnd := len(elfString)
if elfString[elfEnd-1] == nullTerminator {
elfEnd--
}
relocOffset := uint64(len(relocSectionPrefix))
secName := elfString[relocOffset:elfEnd]
if sec := e.Section(secName); sec != nil {
// Offset from start of binary to name inside .strtab
globalOffset := strtab.Offset + off + relocOffset
stringOffsets[secName] = newString(secName, globalOffset)
log.WithField(fieldSymbol, secName).Debugf("Found section with offset %d", globalOffset)
}
}
// If .BTF section is present in the ELF, get the offset of each symbol
// into the BTF string table to be able to overwrite them later.
btfSec := e.Section(".BTF")
if btfSec != nil {
// Read BTF header from the .BTF section.
h, err := readBTFHeader(btfSec, e.ByteOrder)
if err != nil {
return fmt.Errorf("reading BTF section header: %w", err)
}
// Populate offsetBTF fields of all symbols.
if err := findBTFSymbols(stringOffsets, btfSec, h); err != nil {
return fmt.Errorf("finding BTF string offsets: %w", err)
}
}
s.data = dataOffsets
s.strings = stringOffsets
return nil
}