Skip to content

Commit

Permalink
collection: unexport CollectionSpec.BTF
Browse files Browse the repository at this point in the history
It's currently not clear what the use case is for allowing the caller to
replace CollectionSpec.BTF, since the type graph is currently read-only.

Signed-off-by: Timo Beckers <timo@isovalent.com>
  • Loading branch information
ti-mo committed Feb 22, 2022
1 parent a95b28b commit b24b196
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 13 deletions.
2 changes: 1 addition & 1 deletion cmd/bpf2go/output.go
Expand Up @@ -255,7 +255,7 @@ func output(args outputArgs) error {
}

// Collect any types which we've been asked for explicitly.
cTypes, err := collectCTypes(spec.BTF, args.cTypes)
cTypes, err := collectCTypes(spec.BTF(), args.cTypes)
if err != nil {
return err
}
Expand Down
19 changes: 12 additions & 7 deletions collection.go
Expand Up @@ -27,12 +27,12 @@ type CollectionSpec struct {
Maps map[string]*MapSpec
Programs map[string]*ProgramSpec

// The BTF used by maps and programs.
BTF *btf.Spec

// ByteOrder specifies whether the ELF was compiled for
// big-endian or little-endian architectures.
ByteOrder binary.ByteOrder

// The BTF used by maps and programs.
btf *btf.Spec
}

// Copy returns a recursive copy of the spec.
Expand All @@ -45,7 +45,7 @@ func (cs *CollectionSpec) Copy() *CollectionSpec {
Maps: make(map[string]*MapSpec, len(cs.Maps)),
Programs: make(map[string]*ProgramSpec, len(cs.Programs)),
ByteOrder: cs.ByteOrder,
BTF: cs.BTF,
btf: cs.btf,
}

for name, spec := range cs.Maps {
Expand All @@ -59,6 +59,11 @@ func (cs *CollectionSpec) Copy() *CollectionSpec {
return &cpy
}

// BTF returns the BTF spec used by the CollectionSpec's Maps and Programs.
func (cs *CollectionSpec) BTF() *btf.Spec {
return cs.btf
}

// RewriteMaps replaces all references to specific maps.
//
// Use this function to use pre-existing maps instead of creating new ones
Expand Down Expand Up @@ -413,8 +418,8 @@ func (cl *collectionLoader) loadMap(mapName string) (*Map, error) {
return nil, fmt.Errorf("missing map %s", mapName)
}

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

m, err := newMapWithOptions(mapSpec, cl.opts.Maps, cl.handles)
Expand Down Expand Up @@ -442,7 +447,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.BTF != progSpec.BTF.Spec() {
if progSpec.BTF != nil && cl.coll.BTF() != progSpec.BTF.Spec() {
return nil, fmt.Errorf("program %s: BTF doesn't match collection", progName)
}

Expand Down
6 changes: 3 additions & 3 deletions collection_test.go
Expand Up @@ -67,12 +67,12 @@ func TestCollectionSpecCopy(t *testing.T) {
License: "MIT",
},
},
BTF: &btf.Spec{},
btf: &btf.Spec{},
}
cpy := cs.Copy()

if cpy == cs {
t.Error("Copy returned the same pointner")
t.Error("Copy returned the same pointer")
}

if cpy.Maps["my-map"] == cs.Maps["my-map"] {
Expand All @@ -83,7 +83,7 @@ func TestCollectionSpecCopy(t *testing.T) {
t.Error("Copy returned same Programs")
}

if cpy.BTF != cs.BTF {
if cpy.BTF() != cs.BTF() {
t.Error("Copy returned different BTF")
}
}
Expand Down
2 changes: 1 addition & 1 deletion elf_reader.go
Expand Up @@ -164,7 +164,7 @@ func LoadCollectionSpecFromReader(rd io.ReaderAt) (*CollectionSpec, error) {
return nil, fmt.Errorf("load programs: %w", err)
}

return &CollectionSpec{maps, progs, btfSpec, ec.ByteOrder}, nil
return &CollectionSpec{maps, progs, ec.ByteOrder, btfSpec}, nil
}

func loadLicense(sec *elf.Section) (string, error) {
Expand Down
2 changes: 1 addition & 1 deletion elf_reader_test.go
Expand Up @@ -143,7 +143,7 @@ func TestLoadCollectionSpec(t *testing.T) {
return false
}),
cmpopts.IgnoreTypes(new(btf.Map), new(btf.Program)),
cmpopts.IgnoreFields(CollectionSpec{}, "ByteOrder", "BTF"),
cmpopts.IgnoreFields(CollectionSpec{}, "ByteOrder", "btf"),
cmpopts.IgnoreFields(ProgramSpec{}, "Instructions", "ByteOrder"),
cmpopts.IgnoreUnexported(ProgramSpec{}),
cmpopts.IgnoreMapEntries(func(key string, _ *MapSpec) bool {
Expand Down

0 comments on commit b24b196

Please sign in to comment.