Skip to content

Commit 0e6f2cf

Browse files
committed
btf: only take ELF file handle in loadSpecFromELF
Move symbol scanning into loadSpecFromELF() to prevent passing in an empty list. This removes the flexibility for the caller to skip processing symbols (e.g. when parsing vmlinux, which doesn't need datasec fixups), but there should be no assumption of whether LoadSpecFromReader will receive vmlinux or a program ELF. As a future optimization, symbol parsing could be deferred until the BTF parser actually comes across a datasec that needs fixing up. Signed-off-by: Timo Beckers <[email protected]>
1 parent 9f2992b commit 0e6f2cf

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

internal/btf/btf.go

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,17 @@ func LoadSpecFromReader(rd io.ReaderAt) (*Spec, error) {
9090
}
9191
defer file.Close()
9292

93+
return loadSpecFromELF(file)
94+
}
95+
96+
// variableOffsets extracts all symbols offsets from an ELF and indexes them by
97+
// section and variable name.
98+
//
99+
// References to variables in BTF data sections carry unsigned 32-bit offsets.
100+
// Some ELF symbols (e.g. in vmlinux) may point to virtual memory that is well
101+
// beyond this range. Since these symbols cannot be described by BTF info,
102+
// ignore them here.
103+
func variableOffsets(file *internal.SafeELFFile) (map[variable]uint32, error) {
93104
symbols, err := file.Symbols()
94105
if err != nil {
95106
return nil, fmt.Errorf("can't read symbols: %v", err)
@@ -115,10 +126,10 @@ func LoadSpecFromReader(rd io.ReaderAt) (*Spec, error) {
115126
variableOffsets[variable{secName, symbol.Name}] = uint32(symbol.Value)
116127
}
117128

118-
return loadSpecFromELF(file, variableOffsets)
129+
return variableOffsets, nil
119130
}
120131

121-
func loadSpecFromELF(file *internal.SafeELFFile, variableOffsets map[variable]uint32) (*Spec, error) {
132+
func loadSpecFromELF(file *internal.SafeELFFile) (*Spec, error) {
122133
var (
123134
btfSection *elf.Section
124135
btfExtSection *elf.Section
@@ -148,7 +159,12 @@ func loadSpecFromELF(file *internal.SafeELFFile, variableOffsets map[variable]ui
148159
return nil, fmt.Errorf("btf: %w", ErrNotFound)
149160
}
150161

151-
spec, err := loadRawSpec(btfSection.Open(), file.ByteOrder, sectionSizes, variableOffsets)
162+
vars, err := variableOffsets(file)
163+
if err != nil {
164+
return nil, err
165+
}
166+
167+
spec, err := loadRawSpec(btfSection.Open(), file.ByteOrder, sectionSizes, vars)
152168
if err != nil {
153169
return nil, err
154170
}
@@ -339,7 +355,7 @@ func loadKernelSpec() (*Spec, error) {
339355
}
340356
defer file.Close()
341357

342-
return loadSpecFromELF(file, nil)
358+
return loadSpecFromELF(file)
343359
}
344360

345361
return nil, fmt.Errorf("no BTF for kernel version %s: %w", release, internal.ErrNotSupported)

0 commit comments

Comments
 (0)