opentitanlib/test_utils/
object.rs1use anyhow::{Context, Result};
6use object::{Object, ObjectSection, ObjectSymbol};
7
8pub fn symbol_data(object: &object::read::File, name: &str) -> Result<Vec<u8>> {
10 let mut symbols = object.symbols();
11 let symbol = symbols
12 .find(|symbol| symbol.name() == Ok(name))
13 .with_context(|| format!("could not find symbol {name} in ELF"))?;
14
15 let section_index = symbol
16 .section_index()
17 .with_context(|| format!("symbol {name} was did not have a section index"))?;
18 let section = object
19 .section_by_index(section_index)
20 .with_context(|| format!("could not find section containing {name}"))?;
21
22 let offset = (symbol.address() - section.address()) as usize;
23 let data = section.uncompressed_data()?;
24 let data = &data[offset..][..symbol.size() as usize];
25
26 Ok(data.to_vec())
27}
28
29pub fn symbol_addr(object: &object::read::File, name: &str) -> Result<u32> {
31 let addr = object
32 .symbols()
33 .find(|symbol| symbol.name() == Ok(name))
34 .with_context(|| format!("failed to find {name} symbol"))?
35 .address();
36 Ok(addr as u32)
37}