Skip to content

Commit

Permalink
btf: expose Var and Func linkage
Browse files Browse the repository at this point in the history
Expose the linkage of Var and Func types. Of course, BTF encodes these
in different ways and has separate types for them. So we inherit this
decision and introduce separate types for var and func linkage.
  • Loading branch information
lmb committed Jun 29, 2021
1 parent 6821d4e commit 37b4af7
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 19 deletions.
4 changes: 2 additions & 2 deletions internal/btf/btf.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ func (s *Spec) marshal(opts marshalOpts) ([]byte, error) {
for _, raw := range s.rawTypes {
switch {
case opts.StripFuncLinkage && raw.Kind() == kindFunc:
raw.SetLinkage(linkageStatic)
raw.SetLinkage(StaticFunc)
}

if err := raw.Marshal(&buf, opts.ByteOrder); err != nil {
Expand Down Expand Up @@ -779,7 +779,7 @@ var haveFuncLinkage = internal.FeatureTest("BTF func linkage", "5.6", func() err
types.Func.SetKind(kindFunc)
types.Func.SizeType = 1 // aka FuncProto
types.Func.NameOff = 1
types.Func.SetLinkage(linkageGlobal)
types.Func.SetLinkage(GlobalFunc)

btf := marshalBTF(&types, strings, internal.NativeEndian)

Expand Down
22 changes: 20 additions & 2 deletions internal/btf/btf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func TestParseVmlinux(t *testing.T) {
if u8int.Offset != 0 {
t.Fatalf("incorrect int offset of an __u8 int: expected: 0 actual: %d", u8int.Offset)
}
return
break
}
}
}
Expand All @@ -79,7 +79,7 @@ func TestParseCurrentKernelBTF(t *testing.T) {
}

func TestLoadSpecFromElf(t *testing.T) {
testutils.Files(t, testutils.Glob(t, "../../testdata/loader-clang-9-*.elf"), func(t *testing.T, file string) {
testutils.Files(t, testutils.Glob(t, "../../testdata/loader-e*.elf"), func(t *testing.T, file string) {
fh, err := os.Open(file)
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -117,6 +117,24 @@ func TestLoadSpecFromElf(t *testing.T) {
t.Error("FindType doesn't return ErrNotFound:", err)
}

var fn Func
if err := spec.FindType("global_fn", &fn); err != nil {
t.Error("Can't find global_fn():", err)
} else {
if fn.Linkage != GlobalFunc {
t.Error("Expected global linkage:", fn)
}
}

var v Var
if err := spec.FindType("key3", &v); err != nil {
t.Error("Cant find key3:", err)
} else {
if v.Linkage != GlobalVar {
t.Error("Expected global linkage:", v)
}
}

if spec.byteOrder != internal.NativeEndian {
return
}
Expand Down
27 changes: 19 additions & 8 deletions internal/btf/btf_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"io"
)

//go:generate stringer -linecomment -output=btf_types_string.go -type=FuncLinkage,VarLinkage

// btfKind describes a Type.
type btfKind uint8

Expand All @@ -31,14 +33,23 @@ const (
kindDatasec
)

// btfFuncLinkage describes BTF function linkage metadata.
type btfFuncLinkage uint8
// FuncLinkage describes BTF function linkage metadata.
type FuncLinkage int

// Equivalent of enum btf_func_linkage.
const (
linkageStatic btfFuncLinkage = iota
linkageGlobal
// linkageExtern // Currently unused in libbpf.
StaticFunc FuncLinkage = iota // static
GlobalFunc // global
ExternFunc // extern
)

// VarLinkage describes BTF variable linkage metadata.
type VarLinkage int

const (
StaticVar VarLinkage = iota // static
GlobalVar // global
ExternVar // extern
)

const (
Expand Down Expand Up @@ -144,11 +155,11 @@ func (bt *btfType) KindFlag() bool {
return bt.info(btfTypeKindFlagMask, btfTypeKindFlagShift) == 1
}

func (bt *btfType) Linkage() btfFuncLinkage {
return btfFuncLinkage(bt.info(btfTypeVlenMask, btfTypeVlenShift))
func (bt *btfType) Linkage() FuncLinkage {
return FuncLinkage(bt.info(btfTypeVlenMask, btfTypeVlenShift))
}

func (bt *btfType) SetLinkage(linkage btfFuncLinkage) {
func (bt *btfType) SetLinkage(linkage FuncLinkage) {
bt.setInfo(uint32(linkage), btfTypeVlenMask, btfTypeVlenShift)
}

Expand Down
44 changes: 44 additions & 0 deletions internal/btf/btf_types_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 9 additions & 7 deletions internal/btf/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,11 +380,12 @@ func (r *Restrict) copy() Type {
type Func struct {
TypeID
Name
Type Type
Type Type
Linkage FuncLinkage
}

func (f *Func) String() string {
return fmt.Sprintf("func#%d[%q proto=#%d]", f.TypeID, f.Name, f.Type.ID())
return fmt.Sprintf("func#%d[%s %q proto=#%d]", f.TypeID, f.Linkage, f.Name, f.Type.ID())
}

func (f *Func) walk(tdq *typeDeque) { tdq.push(&f.Type) }
Expand Down Expand Up @@ -433,12 +434,12 @@ type FuncParam struct {
type Var struct {
TypeID
Name
Type Type
Type Type
Linkage VarLinkage
}

func (v *Var) String() string {
// TODO: Linkage
return fmt.Sprintf("var#%d[%q]", v.TypeID, v.Name)
return fmt.Sprintf("var#%d[%s %q]", v.TypeID, v.Linkage, v.Name)
}

func (v *Var) walk(tdq *typeDeque) { tdq.push(&v.Type) }
Expand Down Expand Up @@ -803,7 +804,7 @@ func inflateRawTypes(rawTypes []rawType, rawStrings stringTable) (types []Type,
typ = restrict

case kindFunc:
fn := &Func{id, name, nil}
fn := &Func{id, name, nil, raw.Linkage()}
fixup(raw.Type(), kindFuncProto, &fn.Type)
typ = fn

Expand All @@ -828,7 +829,8 @@ func inflateRawTypes(rawTypes []rawType, rawStrings stringTable) (types []Type,
typ = fp

case kindVar:
v := &Var{id, name, nil}
variable := raw.data.(*btfVariable)
v := &Var{id, name, nil, VarLinkage(variable.Linkage)}
fixup(raw.Type(), kindUnknown, &v.Type)
typ = v

Expand Down

0 comments on commit 37b4af7

Please sign in to comment.