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 <dave@dtucker.co.uk>
pull/252/head
Dave Tucker 3 years ago
parent 4e57d1fe32
commit d1f2215193

@ -393,7 +393,7 @@ impl<'a> BpfLoader<'a> {
maps.insert(name, map); maps.insert(name, map);
} }
obj.relocate_maps(maps.iter().map(|(name, map)| (name.as_str(), map)))?; obj.relocate_maps(&maps)?;
obj.relocate_calls()?; obj.relocate_calls()?;
let programs = obj let programs = obj

@ -170,6 +170,7 @@ mod tests {
section_index: 0, section_index: 0,
data: Vec::new(), data: Vec::new(),
kind: obj::MapKind::Other, kind: obj::MapKind::Other,
symbol_index: 0,
} }
} }
@ -221,6 +222,7 @@ mod tests {
..Default::default() ..Default::default()
}, },
section_index: 0, section_index: 0,
symbol_index: 0,
data: Vec::new(), data: Vec::new(),
kind: obj::MapKind::Other, kind: obj::MapKind::Other,
}, },
@ -281,6 +283,7 @@ mod tests {
..Default::default() ..Default::default()
}, },
section_index: 0, section_index: 0,
symbol_index: 0,
data: Vec::new(), data: Vec::new(),
kind: obj::MapKind::Other, kind: obj::MapKind::Other,
}, },

@ -239,6 +239,7 @@ mod tests {
..Default::default() ..Default::default()
}, },
section_index: 0, section_index: 0,
symbol_index: 0,
data: Vec::new(), data: Vec::new(),
kind: obj::MapKind::Other, kind: obj::MapKind::Other,
} }
@ -292,6 +293,7 @@ mod tests {
..Default::default() ..Default::default()
}, },
section_index: 0, section_index: 0,
symbol_index: 0,
data: Vec::new(), data: Vec::new(),
kind: obj::MapKind::Other, kind: obj::MapKind::Other,
}, },

@ -567,6 +567,7 @@ mod tests {
..Default::default() ..Default::default()
}, },
section_index: 0, section_index: 0,
symbol_index: 0,
data: Vec::new(), data: Vec::new(),
kind: MapKind::Other, kind: MapKind::Other,
} }

@ -76,6 +76,7 @@ impl From<&str> for MapKind {
pub struct Map { pub struct Map {
pub(crate) def: bpf_map_def, pub(crate) def: bpf_map_def,
pub(crate) section_index: usize, pub(crate) section_index: usize,
pub(crate) symbol_index: usize,
pub(crate) data: Vec<u8>, pub(crate) data: Vec<u8>,
pub(crate) kind: MapKind, pub(crate) kind: MapKind,
} }
@ -540,6 +541,7 @@ impl Object {
name.to_string(), name.to_string(),
Map { Map {
section_index: section.index.0, section_index: section.index.0,
symbol_index: sym.index,
def, def,
data: Vec::new(), data: Vec::new(),
kind: MapKind::Other, kind: MapKind::Other,
@ -847,6 +849,7 @@ fn parse_map(section: &Section, name: &str) -> Result<Map, ParseError> {
}; };
Ok(Map { Ok(Map {
section_index: section.index.0, section_index: section.index.0,
symbol_index: 0,
def, def,
data, data,
kind, kind,
@ -1115,6 +1118,7 @@ mod tests {
), ),
Ok(Map { Ok(Map {
section_index: 0, section_index: 0,
symbol_index: 0,
def: bpf_map_def { def: bpf_map_def {
map_type: _map_type, map_type: _map_type,
key_size: 4, key_size: 4,
@ -1649,6 +1653,7 @@ mod tests {
pinning: PinningType::None, pinning: PinningType::None,
}, },
section_index: 1, section_index: 1,
symbol_index: 1,
data: vec![0, 0, 0], data: vec![0, 0, 0],
kind: MapKind::Rodata, kind: MapKind::Rodata,
}, },

@ -62,12 +62,15 @@ pub(crate) struct Symbol {
} }
impl Object { impl Object {
pub fn relocate_maps<'a>( pub fn relocate_maps(&mut self, maps: &HashMap<String, Map>) -> Result<(), BpfError> {
&'a mut self,
maps: impl Iterator<Item = (&'a str, &'a Map)>,
) -> Result<(), BpfError> {
let maps_by_section = maps 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<_, _>>(); .collect::<HashMap<_, _>>();
let functions = self let functions = self
@ -82,6 +85,7 @@ impl Object {
function, function,
relocations.values(), relocations.values(),
&maps_by_section, &maps_by_section,
&maps_by_symbol,
&self.symbols_by_index, &self.symbols_by_index,
self.text_section_index, self.text_section_index,
) )
@ -119,6 +123,7 @@ fn relocate_maps<'a, I: Iterator<Item = &'a Relocation>>(
fun: &mut Function, fun: &mut Function,
relocations: I, relocations: I,
maps_by_section: &HashMap<usize, (&str, &Map)>, maps_by_section: &HashMap<usize, (&str, &Map)>,
maps_by_symbol: &HashMap<usize, (&str, &Map)>,
symbol_table: &HashMap<usize, Symbol>, symbol_table: &HashMap<usize, Symbol>,
text_section_index: Option<usize>, text_section_index: Option<usize>,
) -> Result<(), RelocationError> { ) -> Result<(), RelocationError> {
@ -161,14 +166,23 @@ fn relocate_maps<'a, I: Iterator<Item = &'a Relocation>>(
continue; 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 maps_by_section
.get(&section_index) .get(&section_index)
.ok_or(RelocationError::SectionNotFound { .ok_or(RelocationError::SectionNotFound {
symbol_index: rel.symbol_index, symbol_index: rel.symbol_index,
symbol_name: sym.name.clone(), symbol_name: sym.name.clone(),
section_index, section_index,
})?; })?
};
let map_fd = map.fd.ok_or_else(|| RelocationError::MapNotCreated { let map_fd = map.fd.ok_or_else(|| RelocationError::MapNotCreated {
name: (*name).into(), name: (*name).into(),

Loading…
Cancel
Save