From 986e765e91ee492bd1ad03f622642f4b7ee1c035 Mon Sep 17 00:00:00 2001
From: Dave Tucker <dave@dtucker.co.uk>
Date: Thu, 15 Sep 2022 12:06:26 +0000
Subject: [PATCH] aya: Fixup BTF STRUCT

Signed-off-by: Dave Tucker <dave@dtucker.co.uk>
---
 aya-obj/src/btf/btf.rs | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/aya-obj/src/btf/btf.rs b/aya-obj/src/btf/btf.rs
index 61fa6a52..a4e95971 100644
--- a/aya-obj/src/btf/btf.rs
+++ b/aya-obj/src/btf/btf.rs
@@ -425,6 +425,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));