Browse Source

Untangle map relocation from BTF relocation

Alessandro Decina 4 years ago
parent
commit
96db24e285
5 changed files with 18 additions and 22 deletions
  1. 9 2
      src/bpf.rs
  2. 5 3
      src/obj/btf/btf.rs
  3. 1 1
      src/obj/btf/mod.rs
  4. 2 2
      src/obj/mod.rs
  5. 1 14
      src/obj/relocation.rs

+ 9 - 2
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<Bpf, BpfError> {
         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}")]

+ 5 - 3
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;

+ 1 - 1
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::*;

+ 2 - 2
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;

+ 1 - 14
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()