summaryrefslogtreecommitdiff
path: root/bpf/bpf.go
diff options
context:
space:
mode:
author Connor O'Brien <connoro@google.com> 2021-12-02 20:09:45 -0800
committer Connor O'Brien <connoro@google.com> 2022-01-27 12:00:45 -0800
commit2573965c5ec944d64777fec4b2a00b103fab400d (patch)
tree96e45d87937d28f9eca432030b4733a9437538b3 /bpf/bpf.go
parent1785fcb171071f97409aba4cde4fcfba39644ddb (diff)
Add option to generate BTF debug info for bpf programs
Add "btf" option that generates BTF debug info to support easier map inspection. This is accomplished by passing the "-g" flag to clang when compiling the BPF program. The "-g" option also generates a number of DWARF debug sections which are not necessary for loading BTF information, so strip these to avoid increasing file size unnecessarily. bpfloader currently only supports BTF info for maps, not programs, so we also strip the .BTF.ext section containing program BTF info. Bug: 203823368 Test: libbpf_load_test Test: verify time_in_state.o includes .BTF section iff "btf: true" is set Test: verify time_in_state.o still loads if BTF is enabled Change-Id: Ica25b253bace59d04130b0210350188399889bbe Signed-off-by: Connor O'Brien <connoro@google.com>
Diffstat (limited to 'bpf/bpf.go')
-rw-r--r--bpf/bpf.go33
1 files changed, 31 insertions, 2 deletions
diff --git a/bpf/bpf.go b/bpf/bpf.go
index 187b4db61..a4999e5e4 100644
--- a/bpf/bpf.go
+++ b/bpf/bpf.go
@@ -23,6 +23,7 @@ import (
_ "android/soong/cc/config"
"github.com/google/blueprint"
+ "github.com/google/blueprint/proptools"
)
func init() {
@@ -41,6 +42,14 @@ var (
CommandDeps: []string{"$ccCmd"},
},
"ccCmd", "cFlags")
+
+ stripRule = pctx.AndroidStaticRule("stripRule",
+ blueprint.RuleParams{
+ Command: `$stripCmd --strip-unneeded --remove-section=.rel.BTF ` +
+ `--remove-section=.rel.BTF.ext --remove-section=.BTF.ext $in -o $out`,
+ CommandDeps: []string{"$stripCmd"},
+ },
+ "stripCmd")
)
func registerBpfBuildComponents(ctx android.RegistrationContext) {
@@ -64,6 +73,8 @@ type BpfProperties struct {
Cflags []string
Include_dirs []string
Sub_dir string
+ // If set to true, generate BTF debug info for maps & programs
+ Btf *bool
}
type bpf struct {
@@ -99,10 +110,14 @@ func (bpf *bpf) GenerateAndroidBuildActions(ctx android.ModuleContext) {
cflags = append(cflags, bpf.properties.Cflags...)
+ if proptools.Bool(bpf.properties.Btf) {
+ cflags = append(cflags, "-g")
+ }
+
srcs := android.PathsForModuleSrc(ctx, bpf.properties.Srcs)
for _, src := range srcs {
- obj := android.ObjPathWithExt(ctx, "", src, "o")
+ obj := android.ObjPathWithExt(ctx, "unstripped", src, "o")
ctx.Build(pctx, android.BuildParams{
Rule: ccRule,
@@ -114,7 +129,21 @@ func (bpf *bpf) GenerateAndroidBuildActions(ctx android.ModuleContext) {
},
})
- bpf.objs = append(bpf.objs, obj.WithoutRel())
+ if proptools.Bool(bpf.properties.Btf) {
+ objStripped := android.ObjPathWithExt(ctx, "", src, "o")
+ ctx.Build(pctx, android.BuildParams{
+ Rule: stripRule,
+ Input: obj,
+ Output: objStripped,
+ Args: map[string]string{
+ "stripCmd": "${config.ClangBin}/llvm-strip",
+ },
+ })
+ bpf.objs = append(bpf.objs, objStripped.WithoutRel())
+ } else {
+ bpf.objs = append(bpf.objs, obj.WithoutRel())
+ }
+
}
}