Skip to content

Commit

Permalink
btf: rename essentialname to essentialName
Browse files Browse the repository at this point in the history
  • Loading branch information
lmb authored and ti-mo committed Dec 16, 2021
1 parent 0bae83b commit e786263
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 21 deletions.
12 changes: 4 additions & 8 deletions internal/btf/btf.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@ var (
// ID represents the unique ID of a BTF object.
type ID uint32

// essentialName represents the name of a BTF type stripped of any flavor
// suffixes after a ___ delimiter.
type essentialname string

// Spec represents decoded BTF.
type Spec struct {
// Data from .BTF.
Expand All @@ -44,7 +40,7 @@ type Spec struct {

// Types indexed by essential name.
// Includes all struct flavors and types with the same name.
namedTypes map[essentialname][]Type
namedTypes map[essentialName][]Type

// Data from .BTF.ext.
funcInfos map[string]extInfo
Expand Down Expand Up @@ -396,10 +392,10 @@ func fixupDatasec(rawTypes []rawType, rawStrings stringTable, sectionSizes map[s
func (s *Spec) Copy() *Spec {
types, _ := copyTypes(s.types, nil)

namedTypes := make(map[essentialname][]Type)
namedTypes := make(map[essentialName][]Type)
for _, typ := range types {
if name := typ.TypeName(); name != "" {
en := essentialName(name)
en := newEssentialName(name)
namedTypes[en] = append(namedTypes[en], typ)
}
}
Expand Down Expand Up @@ -526,7 +522,7 @@ func (s *Spec) TypeByID(id TypeID) (Type, error) {
//
// Returns an error wrapping ErrNotFound if no matching Type exists in the Spec.
func (s *Spec) AnyTypesByName(name string) ([]Type, error) {
types := s.namedTypes[essentialName(name)]
types := s.namedTypes[newEssentialName(name)]
if len(types) == 0 {
return nil, fmt.Errorf("type name %s: %w", name, ErrNotFound)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/btf/btf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ func TestAnyTypesByName(t *testing.T) {
t.Fatal(err)
}

if len(types) != 2 {
t.Fatalf("expected to receive exactly 2 types from querying ambiguous type, got: %v", types)
if len(types) != 1 {
t.Fatalf("expected to receive exactly 1 types from querying ambiguous type, got: %v", types)
}

types, err = spec.AnyTypesByName("ambiguous___flavour")
Expand Down
8 changes: 4 additions & 4 deletions internal/btf/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ func coreRelocate(local, target *Spec, relos coreRelos) (COREFixups, error) {
}

relos := relosByID[id]
targets := target.namedTypes[essentialName(localTypeName)]
targets := target.namedTypes[newEssentialName(localTypeName)]
fixups, err := coreCalculateFixups(localType, targets, relos)
if err != nil {
return nil, fmt.Errorf("relocate %s: %w", localType, err)
Expand Down Expand Up @@ -704,9 +704,9 @@ func coreFindEnumValue(local Type, localAcc coreAccessor, target Type) (localVal
return nil, nil, errImpossibleRelocation
}

localName := essentialName(localValue.Name)
localName := newEssentialName(localValue.Name)
for i, targetValue := range targetEnum.Values {
if essentialName(targetValue.Name) != localName {
if newEssentialName(targetValue.Name) != localName {
continue
}

Expand Down Expand Up @@ -831,7 +831,7 @@ func coreAreMembersCompatible(localType Type, targetType Type) error {
return nil
}

if essentialName(a) == essentialName(b) {
if newEssentialName(a) == newEssentialName(b) {
return nil
}

Expand Down
18 changes: 11 additions & 7 deletions internal/btf/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -763,7 +763,7 @@ func (dq *typeDeque) all() []*Type {
// Returns a map of named types (so, where NameOff is non-zero) and a slice of types
// indexed by TypeID. Since BTF ignores compilation units, multiple types may share
// the same name. A Type may form a cyclic graph by pointing at itself.
func inflateRawTypes(rawTypes []rawType, rawStrings stringTable) (types []Type, namedTypes map[essentialname][]Type, err error) {
func inflateRawTypes(rawTypes []rawType, rawStrings stringTable) (types []Type, namedTypes map[essentialName][]Type, err error) {
type fixupDef struct {
id TypeID
expectedKind btfKind
Expand Down Expand Up @@ -802,7 +802,7 @@ func inflateRawTypes(rawTypes []rawType, rawStrings stringTable) (types []Type,

types = make([]Type, 0, len(rawTypes))
types = append(types, (*Void)(nil))
namedTypes = make(map[essentialname][]Type)
namedTypes = make(map[essentialName][]Type)

for i, raw := range rawTypes {
var (
Expand Down Expand Up @@ -946,7 +946,7 @@ func inflateRawTypes(rawTypes []rawType, rawStrings stringTable) (types []Type,

types = append(types, typ)

if name := essentialName(typ.TypeName()); name != "" {
if name := newEssentialName(typ.TypeName()); name != "" {
namedTypes[name] = append(namedTypes[name], typ)
}
}
Expand All @@ -973,16 +973,20 @@ func inflateRawTypes(rawTypes []rawType, rawStrings stringTable) (types []Type,
return types, namedTypes, nil
}

// essentialName returns name without a ___ suffix.
// essentialName represents the name of a BTF type stripped of any flavor
// suffixes after a ___ delimiter.
type essentialName string

// newEssentialName returns name without a ___ suffix.
//
// CO-RE has the concept of 'struct flavors', which are used to deal with
// changes in kernel data structures. Anything after three underscores
// in a type name is ignored for the purpose of finding a candidate type
// in the kernel's BTF.
func essentialName(name string) essentialname {
func newEssentialName(name string) essentialName {
lastIdx := strings.LastIndex(name, "___")
if lastIdx > 0 {
return essentialname(name[:lastIdx])
return essentialName(name[:lastIdx])
}
return essentialname(name)
return essentialName(name)
}

0 comments on commit e786263

Please sign in to comment.