From c3a5ca6061e28c2c3535c62ec4d1aa3f16f8806b Mon Sep 17 00:00:00 2001 From: Dave Tucker Date: Thu, 15 Sep 2022 12:06:26 +0000 Subject: [PATCH] aya: Fixup BTF STRUCT The kernel doesn't like Rust idents. Until we fix the kernel, we should instead remove them from the BTF that gets loaded into the kernel. As type-id's are constant, it's not too much bother to recover the name from the original BTF should you need the it. Signed-off-by: Dave Tucker --- aya/src/obj/btf/btf.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/aya/src/obj/btf/btf.rs b/aya/src/obj/btf/btf.rs index 3290a817..5030de00 100644 --- a/aya/src/obj/btf/btf.rs +++ b/aya/src/obj/btf/btf.rs @@ -399,6 +399,18 @@ impl Btf { fixed_ty.name_offset = 0; types.types[i] = BtfType::Ptr(fixed_ty) } + // Fixup STRUCT for Rust + // LLVM emits names that include characters that aren't valid C idents. + // Rather than replace all unsupported characters, just omit the name entirely. + BtfType::Struct(s) => { + let name = self.string_at(s.name_offset)?.to_string(); + if name.contains(['<', '>', ' ', ':', ',']) { + debug!("STRUCT name {} not supported. Omitted from BTF", name); + let mut fixed_ty = s.clone(); + fixed_ty.name_offset = 0; + types.types[i] = BtfType::Struct(fixed_ty) + } + } // Sanitize VAR if they are not supported BtfType::Var(v) if !features.btf_datasec => { types.types[i] = BtfType::Int(Int::new(v.name_offset, 1, IntEncoding::None, 0));