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);
}
obj.relocate_maps(maps.iter().map(|(name, map)| (name.as_str(), map)))?;
obj.relocate_maps(&maps)?;
obj.relocate_calls()?;
let programs = obj

@ -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,
},

@ -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,
},

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

@ -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,
},

@ -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(),

Loading…
Cancel
Save