Merge pull request #173 from alessandrod/func-proto-fixup

Add fixup for FuncProto
pull/176/head
Dave Tucker 3 years ago committed by GitHub
commit d9496df3a7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -15,9 +15,7 @@ use object::Endianness;
use thiserror::Error; use thiserror::Error;
use crate::{ use crate::{
generated::{ generated::{btf_enum, btf_ext_header, btf_func_linkage, btf_header, btf_member},
btf_enum, btf_ext_header, btf_func_linkage, btf_header, btf_member, btf_var_secinfo,
},
obj::btf::{relocation::Relocation, BtfKind, BtfType}, obj::btf::{relocation::Relocation, BtfKind, BtfType},
util::bytes_of, util::bytes_of,
Features, Features,
@ -437,11 +435,8 @@ impl Btf {
section_sizes: &HashMap<String, u64>, section_sizes: &HashMap<String, u64>,
symbol_offsets: &HashMap<String, u64>, symbol_offsets: &HashMap<String, u64>,
) -> Result<(), BtfError> { ) -> Result<(), BtfError> {
// FIXME: there is probably a more elegant way of doing operation in place let mut types = self.types.split_off(0);
// for now, and to keep on the good side of the borrow checker, we'll create for t in &mut types {
// a new Vec and populate it as we go
let mut types = vec![];
for t in &self.types {
let kind = t.kind()?.unwrap_or_default(); let kind = t.kind()?.unwrap_or_default();
// datasec sizes aren't set by llvm // datasec sizes aren't set by llvm
// we need to fix them here before loading the btf to the kernel // we need to fix them here before loading the btf to the kernel
@ -451,18 +446,16 @@ impl Btf {
// While I figure out if this needs fixing in the Kernel or LLVM, we'll // While I figure out if this needs fixing in the Kernel or LLVM, we'll
// do a fixup here // do a fixup here
ty.name_off = 0; ty.name_off = 0;
types.push(BtfType::Ptr(ty));
} }
BtfType::DataSec(mut ty, data) => { BtfType::DataSec(mut ty, data) => {
// Start DataSec Fixups // Start DataSec Fixups
let sec_name = self.type_name(t)?.ok_or(BtfError::InvalidTypeInfo)?; let sec_name = self.string_at(ty.name_off)?;
let name = sec_name.to_string(); let name = sec_name.to_string();
// There are cases when the compiler does indeed populate the // There are cases when the compiler does indeed populate the
// size. If we hit this case, push to the types vector and // size. If we hit this case, push to the types vector and
// continue // continue
if unsafe { ty.__bindgen_anon_1.size > 0 } { if unsafe { ty.__bindgen_anon_1.size > 0 } {
debug!("{} {}: fixup not required", kind, name); debug!("{} {}: fixup not required", kind, name);
types.push(BtfType::DataSec(ty, data.clone()));
continue; continue;
} }
@ -481,7 +474,6 @@ impl Btf {
// we need to get the offset from the ELF file. // we need to get the offset from the ELF file.
// This was also cached during initial parsing and // This was also cached during initial parsing and
// we can query by name in symbol_offsets // we can query by name in symbol_offsets
let mut adjusted_data: Vec<btf_var_secinfo> = vec![];
for d in data { for d in data {
let var_type = self.type_by_id(d.type_)?; let var_type = self.type_by_id(d.type_)?;
let var_kind = var_type.kind()?.unwrap(); let var_kind = var_type.kind()?.unwrap();
@ -492,7 +484,6 @@ impl Btf {
"{} {}: {} {}: fixup not required", "{} {}: {} {}: fixup not required",
kind, name, var_kind, var_name kind, name, var_kind, var_name
); );
adjusted_data.push(*d);
continue; continue;
} }
@ -501,11 +492,7 @@ impl Btf {
symbol_name: var_name.clone(), symbol_name: var_name.clone(),
}, },
)?; )?;
adjusted_data.push(btf_var_secinfo { d.offset = *offset as u32;
type_: d.type_,
offset: *offset as u32,
size: d.size,
});
debug!( debug!(
"{} {}: {} {}: fixup offset {}", "{} {}: {} {}: fixup offset {}",
kind, name, var_kind, var_name, offset kind, name, var_kind, var_name, offset
@ -514,11 +501,16 @@ impl Btf {
return Err(BtfError::InvalidDatasec); return Err(BtfError::InvalidDatasec);
} }
} }
types.push(BtfType::DataSec(ty, adjusted_data)) }
BtfType::FuncProto(_ty, params) => {
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));
}
}
} }
// The type does not need fixing up // The type does not need fixing up
// Push it to the new types vec unmodified _ => {}
ty => types.push(ty.clone()),
} }
} }
self.types = types; self.types = types;
@ -874,7 +866,7 @@ pub(crate) struct SecInfo<'a> {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::generated::{btf_param, BTF_INT_SIGNED, BTF_VAR_STATIC}; use crate::generated::{btf_param, btf_var_secinfo, BTF_INT_SIGNED, BTF_VAR_STATIC};
use super::*; use super::*;

Loading…
Cancel
Save