aya: clean up resolve_symbol a bit

Instead of using intermediate values to extend the lifetime of the
object::File, we just separate the branches.
reviewable/pr1264/r8
Omri Steiner 2 weeks ago committed by Tamir Duberstein
parent 3aded0e0a5
commit 647100faa7

@ -689,14 +689,12 @@ fn resolve_symbol(path: &Path, symbol: &str) -> Result<u64, ResolveSymbolError>
let data = fs::read(path)?;
let obj = object::read::File::parse(&*data)?;
let mut debug_data = Vec::default();
let mut debug_obj_keeper = None;
let sym = find_symbol_in_object(&obj, symbol).map_or_else(
|| {
if let Some(sym) = find_symbol_in_object(&obj, symbol) {
symbol_translated_address(&obj, sym, symbol)
} else {
// Only search in the debug object if the symbol was not found in the main object
let debug_path = find_debug_path_in_object(&obj, path, symbol)?;
debug_data = fs::read(&debug_path).map_err(|e| {
let debug_data = fs::read(&debug_path).map_err(|e| {
ResolveSymbolError::DebuglinkAccessError(
debug_path
.to_str()
@ -709,13 +707,18 @@ fn resolve_symbol(path: &Path, symbol: &str) -> Result<u64, ResolveSymbolError>
verify_build_ids(&obj, &debug_obj, symbol)?;
debug_obj_keeper = Some(debug_obj);
find_symbol_in_object(debug_obj_keeper.as_ref().unwrap(), symbol)
.ok_or_else(|| ResolveSymbolError::Unknown(symbol.to_string()))
},
Ok,
)?;
let sym = find_symbol_in_object(&debug_obj, symbol)
.ok_or_else(|| ResolveSymbolError::Unknown(symbol.to_string()))?;
symbol_translated_address(&debug_obj, sym, symbol)
}
}
fn symbol_translated_address(
obj: &object::File<'_>,
sym: Symbol<'_, '_>,
symbol_name: &str,
) -> Result<u64, ResolveSymbolError> {
let needs_addr_translation = matches!(
obj.kind(),
object::ObjectKind::Dynamic | object::ObjectKind::Executable
@ -725,11 +728,11 @@ fn resolve_symbol(path: &Path, symbol: &str) -> Result<u64, ResolveSymbolError>
} else {
let index = sym
.section_index()
.ok_or_else(|| ResolveSymbolError::NotInSection(symbol.to_string()))?;
.ok_or_else(|| ResolveSymbolError::NotInSection(symbol_name.to_string()))?;
let section = obj.section_by_index(index)?;
let (offset, _size) = section.file_range().ok_or_else(|| {
ResolveSymbolError::SectionFileRangeNone(
symbol.to_string(),
symbol_name.to_string(),
section.name().map(str::to_owned),
)
})?;

Loading…
Cancel
Save