From d1f22151935edebed13e0baaa04f25a96ddb30f0 Mon Sep 17 00:00:00 2001 From: Dave Tucker Date: Thu, 5 May 2022 22:10:14 +0100 Subject: [PATCH] aya: Relocate maps using symbol_index Since we support multiple maps in the same section, the section_index is no longer a unique way to identify maps. This commit uses the symbol index as the identifier, but falls back to section_index for rodata and bss maps since we don't retrieve the symbol_index during parsing. Signed-off-by: Dave Tucker --- aya/src/bpf.rs | 2 +- aya/src/maps/hash_map/hash_map.rs | 3 +++ aya/src/maps/lpm_trie.rs | 2 ++ aya/src/maps/mod.rs | 1 + aya/src/obj/mod.rs | 5 +++++ aya/src/obj/relocation.rs | 28 +++++++++++++++++++++------- 6 files changed, 33 insertions(+), 8 deletions(-) diff --git a/aya/src/bpf.rs b/aya/src/bpf.rs index 9de74f5e..1d244af9 100644 --- a/aya/src/bpf.rs +++ b/aya/src/bpf.rs @@ -393,7 +393,7 @@ impl<'a> BpfLoader<'a> { maps.insert(name, map); } - obj.relocate_maps(maps.iter().map(|(name, map)| (name.as_str(), map)))?; + obj.relocate_maps(&maps)?; obj.relocate_calls()?; let programs = obj diff --git a/aya/src/maps/hash_map/hash_map.rs b/aya/src/maps/hash_map/hash_map.rs index b66f66d3..bb454acf 100644 --- a/aya/src/maps/hash_map/hash_map.rs +++ b/aya/src/maps/hash_map/hash_map.rs @@ -170,6 +170,7 @@ mod tests { section_index: 0, data: Vec::new(), kind: obj::MapKind::Other, + symbol_index: 0, } } @@ -221,6 +222,7 @@ mod tests { ..Default::default() }, section_index: 0, + symbol_index: 0, data: Vec::new(), kind: obj::MapKind::Other, }, @@ -281,6 +283,7 @@ mod tests { ..Default::default() }, section_index: 0, + symbol_index: 0, data: Vec::new(), kind: obj::MapKind::Other, }, diff --git a/aya/src/maps/lpm_trie.rs b/aya/src/maps/lpm_trie.rs index dad13c09..3266a8bf 100644 --- a/aya/src/maps/lpm_trie.rs +++ b/aya/src/maps/lpm_trie.rs @@ -239,6 +239,7 @@ mod tests { ..Default::default() }, section_index: 0, + symbol_index: 0, data: Vec::new(), kind: obj::MapKind::Other, } @@ -292,6 +293,7 @@ mod tests { ..Default::default() }, section_index: 0, + symbol_index: 0, data: Vec::new(), kind: obj::MapKind::Other, }, diff --git a/aya/src/maps/mod.rs b/aya/src/maps/mod.rs index 59c7b0c9..81a0ec35 100644 --- a/aya/src/maps/mod.rs +++ b/aya/src/maps/mod.rs @@ -567,6 +567,7 @@ mod tests { ..Default::default() }, section_index: 0, + symbol_index: 0, data: Vec::new(), kind: MapKind::Other, } diff --git a/aya/src/obj/mod.rs b/aya/src/obj/mod.rs index 0aaa3999..7888b906 100644 --- a/aya/src/obj/mod.rs +++ b/aya/src/obj/mod.rs @@ -76,6 +76,7 @@ impl From<&str> for MapKind { pub struct Map { pub(crate) def: bpf_map_def, pub(crate) section_index: usize, + pub(crate) symbol_index: usize, pub(crate) data: Vec, pub(crate) kind: MapKind, } @@ -540,6 +541,7 @@ impl Object { name.to_string(), Map { section_index: section.index.0, + symbol_index: sym.index, def, data: Vec::new(), kind: MapKind::Other, @@ -847,6 +849,7 @@ fn parse_map(section: &Section, name: &str) -> Result { }; Ok(Map { section_index: section.index.0, + symbol_index: 0, def, data, kind, @@ -1115,6 +1118,7 @@ mod tests { ), Ok(Map { section_index: 0, + symbol_index: 0, def: bpf_map_def { map_type: _map_type, key_size: 4, @@ -1649,6 +1653,7 @@ mod tests { pinning: PinningType::None, }, section_index: 1, + symbol_index: 1, data: vec![0, 0, 0], kind: MapKind::Rodata, }, diff --git a/aya/src/obj/relocation.rs b/aya/src/obj/relocation.rs index 612440e7..af316a84 100644 --- a/aya/src/obj/relocation.rs +++ b/aya/src/obj/relocation.rs @@ -62,12 +62,15 @@ pub(crate) struct Symbol { } impl Object { - pub fn relocate_maps<'a>( - &'a mut self, - maps: impl Iterator, - ) -> Result<(), BpfError> { + pub fn relocate_maps(&mut self, maps: &HashMap) -> Result<(), BpfError> { let maps_by_section = maps - .map(|(name, map)| (map.obj.section_index, (name, map))) + .iter() + .map(|(name, map)| (map.obj.section_index, (name.as_str(), map))) + .collect::>(); + + let maps_by_symbol = maps + .iter() + .map(|(name, map)| (map.obj.symbol_index, (name.as_str(), map))) .collect::>(); let functions = self @@ -82,6 +85,7 @@ impl Object { function, relocations.values(), &maps_by_section, + &maps_by_symbol, &self.symbols_by_index, self.text_section_index, ) @@ -119,6 +123,7 @@ fn relocate_maps<'a, I: Iterator>( fun: &mut Function, relocations: I, maps_by_section: &HashMap, + maps_by_symbol: &HashMap, symbol_table: &HashMap, text_section_index: Option, ) -> Result<(), RelocationError> { @@ -161,14 +166,23 @@ fn relocate_maps<'a, I: Iterator>( continue; } - let (name, map) = + let (name, map) = if maps_by_symbol.contains_key(&rel.symbol_index) { + maps_by_symbol + .get(&rel.symbol_index) + .ok_or(RelocationError::SectionNotFound { + symbol_index: rel.symbol_index, + symbol_name: sym.name.clone(), + section_index, + })? + } else { maps_by_section .get(§ion_index) .ok_or(RelocationError::SectionNotFound { symbol_index: rel.symbol_index, symbol_name: sym.name.clone(), section_index, - })?; + })? + }; let map_fd = map.fd.ok_or_else(|| RelocationError::MapNotCreated { name: (*name).into(),