Skip to content

Commit

Permalink
Merge pull request #572 from alessandrod/reloc-fixes
Browse files Browse the repository at this point in the history
Add tests for relocations + fixes
  • Loading branch information
alessandrod authored Apr 13, 2023
2 parents 23ce42d + 3a8380d commit 542ada3
Show file tree
Hide file tree
Showing 21 changed files with 772 additions and 634 deletions.
4 changes: 1 addition & 3 deletions aya-obj/src/btf/btf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1045,9 +1045,7 @@ mod tests {
let name_offset = btf.add_string("&mut int".to_string());
let ptr_type_id = btf.add_type(BtfType::Ptr(Ptr::new(name_offset, int_type_id)));

let features = BtfFeatures {
..Default::default()
};
let features = Default::default();

btf.fixup_and_sanitize(&HashMap::new(), &HashMap::new(), &features)
.unwrap();
Expand Down
5 changes: 3 additions & 2 deletions aya-obj/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@
//! let bytes = std::fs::read("program.o").unwrap();
//! let mut object = Object::parse(&bytes).unwrap();
//! // Relocate the programs
//! object.relocate_calls().unwrap();
//! object.relocate_maps(std::iter::empty()).unwrap();
//! let text_sections = std::collections::HashSet::new();
//! object.relocate_calls(&text_sections).unwrap();
//! object.relocate_maps(std::iter::empty(), &text_sections).unwrap();
//!
//! // Run with rbpf
//! let instructions = &object.programs["prog_name"].function.instructions;
Expand Down
70 changes: 26 additions & 44 deletions aya-obj/src/maps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

use core::mem;

use crate::thiserror::{self, Error};
use crate::{
thiserror::{self, Error},
BpfSectionKind,
};
use alloc::vec::Vec;

/// Invalid map type encontered
Expand Down Expand Up @@ -139,33 +142,6 @@ pub struct bpf_map_def {
/// The first five __u32 of `bpf_map_def` must be defined.
pub(crate) const MINIMUM_MAP_SIZE: usize = mem::size_of::<u32>() * 5;

/// Kinds of maps
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum MapKind {
/// A map holding `.bss` section data
Bss,
/// A map holding `.data` section data
Data,
/// A map holding `.rodata` section data
Rodata,
/// Other maps
Other,
}

impl From<&str> for MapKind {
fn from(s: &str) -> Self {
if s == ".bss" {
MapKind::Bss
} else if s.starts_with(".data") {
MapKind::Data
} else if s.starts_with(".rodata") {
MapKind::Rodata
} else {
MapKind::Other
}
}
}

/// Map data defined in `maps` or `.maps` sections
#[derive(Debug, Clone)]
pub enum Map {
Expand Down Expand Up @@ -248,14 +224,6 @@ impl Map {
}
}

/// Returns the map kind
pub fn kind(&self) -> MapKind {
match self {
Map::Legacy(m) => m.kind,
Map::Btf(m) => m.kind,
}
}

/// Returns the section index
pub fn section_index(&self) -> usize {
match self {
Expand All @@ -264,11 +232,22 @@ impl Map {
}
}

/// Returns the symbol index
pub fn symbol_index(&self) -> usize {
/// Returns the section kind.
pub fn section_kind(&self) -> BpfSectionKind {
match self {
Map::Legacy(m) => m.section_kind,
Map::Btf(_) => BpfSectionKind::BtfMaps,
}
}

/// Returns the symbol index.
///
/// This is `None` for data maps (.bss, .data and .rodata) since those don't
/// need symbols in order to be relocated.
pub fn symbol_index(&self) -> Option<usize> {
match self {
Map::Legacy(m) => m.symbol_index,
Map::Btf(m) => m.symbol_index,
Map::Btf(m) => Some(m.symbol_index),
}
}
}
Expand All @@ -283,12 +262,16 @@ pub struct LegacyMap {
pub def: bpf_map_def,
/// The section index
pub section_index: usize,
/// The symbol index
pub symbol_index: usize,
/// The section kind
pub section_kind: BpfSectionKind,
/// The symbol index.
///
/// This is None for data maps (.bss .data and .rodata). We don't need
/// symbols to relocate those since they don't contain multiple maps, but
/// are just a flat array of bytes.
pub symbol_index: Option<usize>,
/// The map data
pub data: Vec<u8>,
/// The map kind
pub kind: MapKind,
}

/// A BTF-defined map, most likely from a `.maps` section.
Expand All @@ -298,6 +281,5 @@ pub struct BtfMap {
pub def: BtfMapDef,
pub(crate) section_index: usize,
pub(crate) symbol_index: usize,
pub(crate) kind: MapKind,
pub(crate) data: Vec<u8>,
}
Loading

0 comments on commit 542ada3

Please sign in to comment.