Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

aya: Relocate maps using symbol_index #252

Merged
merged 1 commit into from May 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion aya/src/bpf.rs
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions aya/src/maps/hash_map/hash_map.rs
Expand Up @@ -170,6 +170,7 @@ mod tests {
section_index: 0,
data: Vec::new(),
kind: obj::MapKind::Other,
symbol_index: 0,
}
}

Expand Down Expand Up @@ -221,6 +222,7 @@ mod tests {
..Default::default()
},
section_index: 0,
symbol_index: 0,
data: Vec::new(),
kind: obj::MapKind::Other,
},
Expand Down Expand Up @@ -281,6 +283,7 @@ mod tests {
..Default::default()
},
section_index: 0,
symbol_index: 0,
data: Vec::new(),
kind: obj::MapKind::Other,
},
Expand Down
2 changes: 2 additions & 0 deletions aya/src/maps/lpm_trie.rs
Expand Up @@ -239,6 +239,7 @@ mod tests {
..Default::default()
},
section_index: 0,
symbol_index: 0,
data: Vec::new(),
kind: obj::MapKind::Other,
}
Expand Down Expand Up @@ -292,6 +293,7 @@ mod tests {
..Default::default()
},
section_index: 0,
symbol_index: 0,
data: Vec::new(),
kind: obj::MapKind::Other,
},
Expand Down
1 change: 1 addition & 0 deletions aya/src/maps/mod.rs
Expand Up @@ -567,6 +567,7 @@ mod tests {
..Default::default()
},
section_index: 0,
symbol_index: 0,
data: Vec::new(),
kind: MapKind::Other,
}
Expand Down
5 changes: 5 additions & 0 deletions aya/src/obj/mod.rs
Expand Up @@ -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,
}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -1649,6 +1653,7 @@ mod tests {
pinning: PinningType::None,
},
section_index: 1,
symbol_index: 1,
data: vec![0, 0, 0],
kind: MapKind::Rodata,
},
Expand Down
28 changes: 21 additions & 7 deletions aya/src/obj/relocation.rs
Expand Up @@ -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
Expand All @@ -82,6 +85,7 @@ impl Object {
function,
relocations.values(),
&maps_by_section,
&maps_by_symbol,
&self.symbols_by_index,
self.text_section_index,
)
Expand Down Expand Up @@ -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> {
Expand Down Expand Up @@ -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(),
Expand Down