Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use instruction metadata to simplify linking and CO-RE relocation #606

Merged
merged 5 commits into from
May 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 4 additions & 8 deletions cmd/bpf2go/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,16 +343,12 @@ func collectCTypes(types *btf.Spec, names []string) ([]btf.Type, error) {
func collectMapTypes(maps map[string]*ebpf.MapSpec) []btf.Type {
var result []btf.Type
for _, m := range maps {
if m.BTF == nil {
continue
}

if m.BTF.Key != nil && m.BTF.Key.TypeName() != "" {
result = append(result, m.BTF.Key)
if m.Key != nil && m.Key.TypeName() != "" {
result = append(result, m.Key)
}

if m.BTF.Value != nil && m.BTF.Value.TypeName() != "" {
result = append(result, m.BTF.Value)
if m.Value != nil && m.Value.TypeName() != "" {
result = append(result, m.Value)
}
}
return result
Expand Down
6 changes: 3 additions & 3 deletions collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func (cs *CollectionSpec) RewriteConstants(consts map[string]interface{}) error
buf := make([]byte, len(value))
copy(buf, value)

err := patchValue(buf, rodata.BTF.Value, consts)
err := patchValue(buf, rodata.Value, consts)
if err != nil {
return err
}
Expand Down Expand Up @@ -454,7 +454,7 @@ func (cl *collectionLoader) loadMap(mapName string) (*Map, error) {
return nil, fmt.Errorf("missing map %s", mapName)
}

if mapSpec.BTF != nil && cl.coll.Types != mapSpec.BTF.Spec {
if mapSpec.BTF != nil && cl.coll.Types != mapSpec.BTF {
return nil, fmt.Errorf("map %s: BTF doesn't match collection", mapName)
}

Expand Down Expand Up @@ -494,7 +494,7 @@ func (cl *collectionLoader) loadProgram(progName string) (*Program, error) {
return nil, fmt.Errorf("cannot load program %s: program type is unspecified", progName)
}

if progSpec.BTF != nil && cl.coll.Types != progSpec.BTF.Spec() {
if progSpec.BTF != nil && cl.coll.Types != progSpec.BTF {
return nil, fmt.Errorf("program %s: BTF doesn't match collection", progName)
}

Expand Down
24 changes: 14 additions & 10 deletions elf_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type elfCode struct {
license string
version uint32
btf *btf.Spec
extInfo *btf.ExtInfos
}

// LoadCollectionSpec parses an ELF file into a CollectionSpec.
Expand Down Expand Up @@ -94,7 +95,7 @@ func LoadCollectionSpecFromReader(rd io.ReaderAt) (*CollectionSpec, error) {
return nil, fmt.Errorf("load version: %w", err)
}

btfSpec, err := btf.LoadSpecFromReader(rd)
btfSpec, btfExtInfo, err := btf.LoadSpecAndExtInfosFromReader(rd)
if err != nil && !errors.Is(err, btf.ErrNotFound) {
return nil, fmt.Errorf("load BTF: %w", err)
}
Expand All @@ -105,6 +106,7 @@ func LoadCollectionSpecFromReader(rd io.ReaderAt) (*CollectionSpec, error) {
license: license,
version: version,
btf: btfSpec,
extInfo: btfExtInfo,
}

symbols, err := f.Symbols()
Expand Down Expand Up @@ -309,13 +311,7 @@ func (ec *elfCode) loadProgramSections() (map[string]*ProgramSpec, error) {
KernelVersion: ec.version,
Instructions: insns,
ByteOrder: ec.ByteOrder,
}

if ec.btf != nil {
spec.BTF, err = ec.btf.Program(name)
if err != nil && !errors.Is(err, btf.ErrNoExtendedInfo) {
return nil, fmt.Errorf("program %s: %w", name, err)
}
BTF: ec.btf,
}

// Function names must be unique within a single ELF blob.
Expand Down Expand Up @@ -383,6 +379,10 @@ func (ec *elfCode) loadFunctions(section *elfSection) (map[string]asm.Instructio
}
}

if ec.extInfo != nil {
ec.extInfo.Assign(insns, section.Name)
}

return splitSymbols(insns)
}

Expand Down Expand Up @@ -904,7 +904,9 @@ func mapSpecFromBTF(es *elfSection, vs *btf.VarSecinfo, def *btf.Struct, spec *b
ValueSize: valueSize,
MaxEntries: maxEntries,
Flags: flags,
BTF: &btf.Map{Spec: spec, Key: key, Value: value},
Key: key,
Value: value,
BTF: spec,
Pinning: pinType,
InnerMap: innerMapSpec,
Contents: contents,
Expand Down Expand Up @@ -1039,7 +1041,9 @@ func (ec *elfCode) loadDataSections(maps map[string]*MapSpec) error {
ValueSize: uint32(len(data)),
MaxEntries: 1,
Contents: []MapKV{{uint32(0), data}},
BTF: &btf.Map{Spec: ec.btf, Key: &btf.Void{}, Value: datasec},
Key: &btf.Void{},
Value: datasec,
BTF: ec.btf,
}

switch sec.Name {
Expand Down
3 changes: 2 additions & 1 deletion elf_reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,10 @@ func TestLoadCollectionSpec(t *testing.T) {
}
return false
}),
cmpopts.IgnoreTypes(new(btf.Map), new(btf.Program)),
cmpopts.IgnoreTypes(new(btf.Spec)),
cmpopts.IgnoreFields(CollectionSpec{}, "ByteOrder", "Types"),
cmpopts.IgnoreFields(ProgramSpec{}, "Instructions", "ByteOrder"),
cmpopts.IgnoreFields(MapSpec{}, "Key", "Value"),
cmpopts.IgnoreUnexported(ProgramSpec{}),
cmpopts.IgnoreMapEntries(func(key string, _ *MapSpec) bool {
switch key {
Expand Down