4
0
Эх сурвалжийг харах

Merge pull request #252 from dave-tucker/multimap-relo

aya: Relocate maps using symbol_index
Dave Tucker 3 жил өмнө
parent
commit
4afc5ea711

+ 1 - 1
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

+ 3 - 0
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,
             },

+ 2 - 0
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,
             },

+ 1 - 0
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,
         }

+ 5 - 0
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<u8>,
     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<Map, ParseError> {
     };
     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,
             },

+ 21 - 7
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<Item = (&'a str, &'a Map)>,
-    ) -> Result<(), BpfError> {
+    pub fn relocate_maps(&mut self, maps: &HashMap<String, Map>) -> 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::<HashMap<_, _>>();
+
+        let maps_by_symbol = maps
+            .iter()
+            .map(|(name, map)| (map.obj.symbol_index, (name.as_str(), map)))
             .collect::<HashMap<_, _>>();
 
         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<Item = &'a Relocation>>(
     fun: &mut Function,
     relocations: I,
     maps_by_section: &HashMap<usize, (&str, &Map)>,
+    maps_by_symbol: &HashMap<usize, (&str, &Map)>,
     symbol_table: &HashMap<usize, Symbol>,
     text_section_index: Option<usize>,
 ) -> Result<(), RelocationError> {
@@ -161,14 +166,23 @@ fn relocate_maps<'a, I: Iterator<Item = &'a Relocation>>(
             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(&section_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(),