btf: fix match arms

Don't match on kind and use if let...
Match on the BtfType

Fixes: #178

Signed-off-by: Dave Tucker <dave@dtucker.co.uk>
pull/175/head
Dave Tucker 3 years ago
parent 326825aab0
commit 99fa85eab8

@ -416,26 +416,24 @@ impl Btf {
) -> Result<(), BtfError> {
let mut types = mem::take(&mut self.types);
for i in 0..types.types.len() {
let kind = types.types.get(i).unwrap().kind()?.unwrap_or_default();
match kind {
let t = &types.types[i];
let kind = t.kind()?.unwrap_or_default();
match t {
// Fixup PTR for Rust
// LLVM emits names for Rust pointer types, which the kernel doesn't like
// While I figure out if this needs fixing in the Kernel or LLVM, we'll
// do a fixup here
BtfKind::Ptr => {
if let Some(BtfType::Ptr(ty)) = types.types.get_mut(i) {
ty.name_off = 0;
}
BtfType::Ptr(ty) => {
let mut fixed_ty = *ty;
fixed_ty.name_off = 0;
types.types[i] = BtfType::Ptr(fixed_ty)
}
// Sanitize VAR if they are not supported
BtfKind::Var if !features.btf_datasec => {
if let Some(BtfType::Var(ty, _)) = types.types.get(i) {
BtfType::Var(ty, _) if !features.btf_datasec => {
types.types[i] = BtfType::new_int(ty.name_off, 1, 0, 0);
}
}
// Sanitize DATASEC if they are not supported
BtfKind::DataSec if !features.btf_datasec => {
if let Some(BtfType::DataSec(ty, data)) = types.types.get(i) {
BtfType::DataSec(ty, data) if !features.btf_datasec => {
debug!("{}: not supported. replacing with STRUCT", kind);
let mut members = vec![];
for member in data {
@ -448,12 +446,10 @@ impl Btf {
}
types.types[i] = BtfType::new_struct(ty.name_off, members, 0);
}
}
// Fixup DATASEC
// DATASEC sizes aren't always set by LLVM
// we need to fix them here before loading the btf to the kernel
BtfKind::DataSec if features.btf_datasec => {
if let Some(BtfType::DataSec(ty, data)) = types.types.get(i) {
BtfType::DataSec(ty, data) if features.btf_datasec => {
// Start DataSec Fixups
let sec_name = self.string_at(ty.name_off)?;
let name = sec_name.to_string();
@ -469,10 +465,11 @@ impl Btf {
// We need to get the size of the section from the ELF file
// Fortunately, we cached these when parsing it initially
// and we can this up by name in section_sizes
let size = section_sizes.get(&name).ok_or_else(|| {
BtfError::UnknownSectionSize {
let size =
section_sizes
.get(&name)
.ok_or_else(|| BtfError::UnknownSectionSize {
section_name: name.clone(),
}
})?;
debug!("{} {}: fixup size to {}", kind, name, size);
fixed_ty.__bindgen_anon_1.size = *size as u32;
@ -511,20 +508,18 @@ impl Btf {
}
types.types[i] = BtfType::DataSec(fixed_ty, fixed_data);
}
}
// Fixup FUNC_PROTO
BtfKind::FuncProto if features.btf_func => {
if let Some(BtfType::FuncProto(_, params)) = types.types.get_mut(i) {
BtfType::FuncProto(ty, params) if features.btf_func => {
let mut params = params.clone();
for (i, mut param) in params.iter_mut().enumerate() {
if param.name_off == 0 && param.type_ != 0 {
param.name_off = self.add_string(format!("param{}", i));
}
}
}
types.types[i] = BtfType::FuncProto(*ty, params);
}
// Sanitize FUNC_PROTO
BtfKind::FuncProto if !features.btf_func => {
if let Some(BtfType::FuncProto(ty, vars)) = types.types.get(i) {
BtfType::FuncProto(ty, vars) if !features.btf_func => {
debug!("{}: not supported. replacing with ENUM", kind);
let members: Vec<btf_enum> = vars
.iter()
@ -536,54 +531,45 @@ impl Btf {
let enum_type = BtfType::new_enum(ty.name_off, members);
types.types[i] = enum_type;
}
}
// Sanitize FUNC
BtfKind::Func if !features.btf_func => {
if let Some(BtfType::Func(ty)) = types.types.get(i) {
BtfType::Func(ty) if !features.btf_func => {
debug!("{}: not supported. replacing with TYPEDEF", kind);
let typedef_type =
BtfType::new_typedef(ty.name_off, unsafe { ty.__bindgen_anon_1.type_ });
types.types[i] = typedef_type;
}
}
// Sanitize BTF_FUNC_GLOBAL
BtfKind::Func if !features.btf_func_global => {
if let Some(BtfType::Func(ty)) = types.types.get_mut(i) {
BtfType::Func(ty) if !features.btf_func_global => {
let mut fixed_ty = *ty;
if type_vlen(ty) == btf_func_linkage::BTF_FUNC_GLOBAL as usize {
debug!(
"{}: BTF_FUNC_GLOBAL not supported. replacing with BTF_FUNC_STATIC",
kind
);
ty.info = (ty.info & 0xFFFF0000)
fixed_ty.info = (ty.info & 0xFFFF0000)
| (btf_func_linkage::BTF_FUNC_STATIC as u32) & 0xFFFF;
}
}
types.types[i] = BtfType::Func(fixed_ty);
}
// Sanitize FLOAT
BtfKind::Float if !features.btf_float => {
if let Some(BtfType::Float(ty)) = types.types.get(i) {
BtfType::Float(ty) if !features.btf_float => {
debug!("{}: not supported. replacing with STRUCT", kind);
let struct_ty =
BtfType::new_struct(0, vec![], unsafe { ty.__bindgen_anon_1.size });
types.types[i] = struct_ty;
}
}
// Sanitize DECL_TAG
BtfKind::DeclTag if !features.btf_decl_tag => {
if let Some(BtfType::DeclTag(ty, _)) = types.types.get(i) {
BtfType::DeclTag(ty, _) if !features.btf_decl_tag => {
debug!("{}: not supported. replacing with INT", kind);
let int_type = BtfType::new_int(ty.name_off, 1, 0, 0);
types.types[i] = int_type;
}
}
// Sanitize TYPE_TAG
BtfKind::TypeTag if !features.btf_type_tag => {
if let Some(BtfType::TypeTag(ty)) = types.types.get(i) {
BtfType::TypeTag(ty) if !features.btf_type_tag => {
debug!("{}: not supported. replacing with CONST", kind);
let const_type = BtfType::new_const(unsafe { ty.__bindgen_anon_1.type_ });
types.types[i] = const_type;
}
}
// The type does not need fixing up or sanitization
_ => {}
}

Loading…
Cancel
Save