From 96db24e2851318f77449b1716e825a10f681f26b Mon Sep 17 00:00:00 2001 From: Alessandro Decina Date: Fri, 5 Feb 2021 20:26:57 +0000 Subject: [PATCH] Untangle map relocation from BTF relocation --- src/bpf.rs | 11 +++++++++-- src/obj/btf/btf.rs | 8 +++++--- src/obj/btf/mod.rs | 2 +- src/obj/mod.rs | 4 ++-- src/obj/relocation.rs | 15 +-------------- 5 files changed, 18 insertions(+), 22 deletions(-) diff --git a/src/bpf.rs b/src/bpf.rs index 3f7c8f32..04364540 100644 --- a/src/bpf.rs +++ b/src/bpf.rs @@ -8,7 +8,7 @@ use thiserror::Error; use crate::{ maps::{Map, MapError}, - obj::{Object, ParseError, RelocationError}, + obj::{BtfRelocationError, Object, ParseError, RelocationError}, programs::{KProbe, Program, ProgramData, ProgramError, SocketFilter, TracePoint, UProbe, Xdp}, syscalls::bpf_map_update_elem_ptr, }; @@ -52,6 +52,8 @@ impl Bpf { pub fn load(data: &[u8]) -> Result { let mut obj = Object::parse(data)?; + obj.relocate_btf()?; + let mut maps = Vec::new(); for (_, obj) in obj.maps.drain() { let mut map = Map { obj, fd: None }; @@ -63,7 +65,7 @@ impl Bpf { maps.push(map); } - obj.relocate(maps.as_slice())?; + obj.relocate_maps(maps.as_slice())?; let programs = obj .programs @@ -143,6 +145,11 @@ pub enum BpfError { ParseError(#[from] ParseError), #[error("error relocating BPF object: {0}")] RelocationError(#[from] RelocationError), + #[error("BTF error: {error}")] + BtfRelocationError { + #[from] + error: BtfRelocationError, + }, #[error("map error: {0}")] MapError(#[from] MapError), #[error("program error: {0}")] diff --git a/src/obj/btf/btf.rs b/src/obj/btf/btf.rs index 301c69d5..abb3f389 100644 --- a/src/obj/btf/btf.rs +++ b/src/obj/btf/btf.rs @@ -8,9 +8,11 @@ use std::{ use object::Endianness; use thiserror::Error; -use crate::generated::{btf_ext_header, btf_header}; - -use super::{BtfType, Relocation}; +use crate::{ + generated::{btf_ext_header, btf_header}, + obj::btf::relocation::Relocation, + obj::btf::BtfType, +}; pub(crate) const MAX_RESOLVE_DEPTH: u8 = 32; pub(crate) const MAX_SPEC_LEN: usize = 64; diff --git a/src/obj/btf/mod.rs b/src/obj/btf/mod.rs index 02c1266b..80b3efb3 100644 --- a/src/obj/btf/mod.rs +++ b/src/obj/btf/mod.rs @@ -3,5 +3,5 @@ mod relocation; mod types; pub use btf::*; -pub(crate) use relocation::*; +pub use relocation::RelocationError; pub(crate) use types::*; diff --git a/src/obj/mod.rs b/src/obj/mod.rs index 1c458662..6e2a6e82 100644 --- a/src/obj/mod.rs +++ b/src/obj/mod.rs @@ -14,13 +14,13 @@ use std::{ }; use thiserror::Error; -use btf::{Btf, BtfError, BtfExt}; +pub use btf::RelocationError as BtfRelocationError; pub use relocation::*; use crate::{ bpf_map_def, 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; diff --git a/src/obj/relocation.rs b/src/obj/relocation.rs index b3bffacd..2b670c87 100644 --- a/src/obj/relocation.rs +++ b/src/obj/relocation.rs @@ -6,7 +6,7 @@ use thiserror::Error; use crate::{ generated::{bpf_insn, BPF_PSEUDO_MAP_FD, BPF_PSEUDO_MAP_VALUE}, maps::Map, - obj::{btf::RelocationError as BtfRelocationError, Object}, + obj::Object, }; #[derive(Debug, Error)] @@ -35,12 +35,6 @@ pub enum RelocationError { relocation_number: usize, }, - #[error("BTF error: {error}")] - BtfRelocationError { - #[from] - error: BtfRelocationError, - }, - #[error("IO error: {io_error}")] IO { #[from] @@ -64,13 +58,6 @@ pub(crate) struct Symbol { } 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> { let maps_by_section = maps .iter()