Untangle map relocation from BTF relocation

pull/1/head
Alessandro Decina 4 years ago
parent 88d49927c2
commit 96db24e285

@ -8,7 +8,7 @@ use thiserror::Error;
use crate::{ use crate::{
maps::{Map, MapError}, maps::{Map, MapError},
obj::{Object, ParseError, RelocationError}, obj::{BtfRelocationError, Object, ParseError, RelocationError},
programs::{KProbe, Program, ProgramData, ProgramError, SocketFilter, TracePoint, UProbe, Xdp}, programs::{KProbe, Program, ProgramData, ProgramError, SocketFilter, TracePoint, UProbe, Xdp},
syscalls::bpf_map_update_elem_ptr, syscalls::bpf_map_update_elem_ptr,
}; };
@ -52,6 +52,8 @@ impl Bpf {
pub fn load(data: &[u8]) -> Result<Bpf, BpfError> { pub fn load(data: &[u8]) -> Result<Bpf, BpfError> {
let mut obj = Object::parse(data)?; let mut obj = Object::parse(data)?;
obj.relocate_btf()?;
let mut maps = Vec::new(); let mut maps = Vec::new();
for (_, obj) in obj.maps.drain() { for (_, obj) in obj.maps.drain() {
let mut map = Map { obj, fd: None }; let mut map = Map { obj, fd: None };
@ -63,7 +65,7 @@ impl Bpf {
maps.push(map); maps.push(map);
} }
obj.relocate(maps.as_slice())?; obj.relocate_maps(maps.as_slice())?;
let programs = obj let programs = obj
.programs .programs
@ -143,6 +145,11 @@ pub enum BpfError {
ParseError(#[from] ParseError), ParseError(#[from] ParseError),
#[error("error relocating BPF object: {0}")] #[error("error relocating BPF object: {0}")]
RelocationError(#[from] RelocationError), RelocationError(#[from] RelocationError),
#[error("BTF error: {error}")]
BtfRelocationError {
#[from]
error: BtfRelocationError,
},
#[error("map error: {0}")] #[error("map error: {0}")]
MapError(#[from] MapError), MapError(#[from] MapError),
#[error("program error: {0}")] #[error("program error: {0}")]

@ -8,9 +8,11 @@ use std::{
use object::Endianness; use object::Endianness;
use thiserror::Error; use thiserror::Error;
use crate::generated::{btf_ext_header, btf_header}; use crate::{
generated::{btf_ext_header, btf_header},
use super::{BtfType, Relocation}; obj::btf::relocation::Relocation,
obj::btf::BtfType,
};
pub(crate) const MAX_RESOLVE_DEPTH: u8 = 32; pub(crate) const MAX_RESOLVE_DEPTH: u8 = 32;
pub(crate) const MAX_SPEC_LEN: usize = 64; pub(crate) const MAX_SPEC_LEN: usize = 64;

@ -3,5 +3,5 @@ mod relocation;
mod types; mod types;
pub use btf::*; pub use btf::*;
pub(crate) use relocation::*; pub use relocation::RelocationError;
pub(crate) use types::*; pub(crate) use types::*;

@ -14,13 +14,13 @@ use std::{
}; };
use thiserror::Error; use thiserror::Error;
use btf::{Btf, BtfError, BtfExt}; pub use btf::RelocationError as BtfRelocationError;
pub use relocation::*; pub use relocation::*;
use crate::{ use crate::{
bpf_map_def, bpf_map_def,
generated::{bpf_insn, bpf_map_type::BPF_MAP_TYPE_ARRAY}, generated::{bpf_insn, bpf_map_type::BPF_MAP_TYPE_ARRAY},
obj::relocation::{Relocation, Symbol}, obj::btf::{Btf, BtfError, BtfExt},
}; };
const KERNEL_VERSION_ANY: u32 = 0xFFFF_FFFE; const KERNEL_VERSION_ANY: u32 = 0xFFFF_FFFE;

@ -6,7 +6,7 @@ use thiserror::Error;
use crate::{ use crate::{
generated::{bpf_insn, BPF_PSEUDO_MAP_FD, BPF_PSEUDO_MAP_VALUE}, generated::{bpf_insn, BPF_PSEUDO_MAP_FD, BPF_PSEUDO_MAP_VALUE},
maps::Map, maps::Map,
obj::{btf::RelocationError as BtfRelocationError, Object}, obj::Object,
}; };
#[derive(Debug, Error)] #[derive(Debug, Error)]
@ -35,12 +35,6 @@ pub enum RelocationError {
relocation_number: usize, relocation_number: usize,
}, },
#[error("BTF error: {error}")]
BtfRelocationError {
#[from]
error: BtfRelocationError,
},
#[error("IO error: {io_error}")] #[error("IO error: {io_error}")]
IO { IO {
#[from] #[from]
@ -64,13 +58,6 @@ pub(crate) struct Symbol {
} }
impl Object { impl Object {
pub fn relocate(&mut self, maps: &[Map]) -> Result<(), RelocationError> {
self.relocate_maps(maps)?;
self.relocate_btf()?;
Ok(())
}
pub fn relocate_maps(&mut self, maps: &[Map]) -> Result<(), RelocationError> { pub fn relocate_maps(&mut self, maps: &[Map]) -> Result<(), RelocationError> {
let maps_by_section = maps let maps_by_section = maps
.iter() .iter()

Loading…
Cancel
Save