From 4e33fa011e87cdc2fc59025b9e531b4872651cd0 Mon Sep 17 00:00:00 2001 From: Mary Date: Mon, 17 Apr 2023 17:15:46 +0200 Subject: [PATCH] aya-obj: fix DATASEC to STRUCT conversion This fix the following issues: - Previously the DATASEC name wasn't sanitized resulting on "Invalid name" returned by old kernels. - The newly created BTF struct had a size of 0 making old kernels refuse it. This was tested on Debian 10 with kernel 4.19.0-21. --- aya-obj/src/btf/btf.rs | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/aya-obj/src/btf/btf.rs b/aya-obj/src/btf/btf.rs index ac915bfd..1e04a28c 100644 --- a/aya-obj/src/btf/btf.rs +++ b/aya-obj/src/btf/btf.rs @@ -432,6 +432,19 @@ impl Btf { // Sanitize DATASEC if they are not supported BtfType::DataSec(d) if !features.btf_datasec => { debug!("{}: not supported. replacing with STRUCT", kind); + + // STRUCT aren't allowed to have "." in their name, fixup this if needed. + let mut name_offset = t.name_offset(); + let sec_name = self.string_at(name_offset)?; + let name = sec_name.to_string(); + + // Handle any "." characters in struct names + // Example: ".maps" + let fixed_name = name.replace('.', "_"); + if fixed_name != name { + name_offset = self.add_string(fixed_name); + } + let mut members = vec![]; for member in d.entries.iter() { let mt = types.type_by_id(member.btf_type).unwrap(); @@ -441,7 +454,9 @@ impl Btf { offset: member.offset * 8, }) } - types.types[i] = BtfType::Struct(Struct::new(t.name_offset(), members, 0)); + + types.types[i] = + BtfType::Struct(Struct::new(name_offset, members, d.entries.len() as u32)); } // Fixup DATASEC // DATASEC sizes aren't always set by LLVM @@ -1116,7 +1131,7 @@ mod tests { VarLinkage::Static, ))); - let name_offset = btf.add_string(".data".to_string()); + let name_offset = btf.add_string("data".to_string()); let variables = vec![DataSecEntry { btf_type: var_type_id, offset: 0,