diff --git a/aya/src/bpf.rs b/aya/src/bpf.rs index 9de74f5e1..1d244af9b 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 b66f66d3b..bb454acff 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 dad13c09b..3266a8bf7 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 59c7b0c9f..81a0ec357 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 0aaa39994..7888b9067 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 612440e73..af316a841 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(),