use tracing::warn;
use object::Object;
use object::ObjectSection;
#[derive(Clone, Debug, Default)]
pub struct ElfMetadata {
pub version: String,
pub chip: Option<String>,
pub timeout: Option<u64>,
}
impl ElfMetadata {
pub fn from_elf(elf: &[u8]) -> Result<Self, object::Error> {
let file = object::File::parse(elf)?;
Self::from_object(&file)
}
pub fn from_object(file: &object::File) -> Result<Self, object::Error> {
let mut version = String::default();
let mut chip = None;
let mut timeout = None;
if let Some(section) = file.section_by_name(".probe-rs.version") {
let data = section.data()?;
if !data.is_empty() {
match String::from_utf8(data.to_vec()) {
Ok(s) => version = s,
Err(_) => warn!(".probe-rs.version contents are not a valid utf8 string."),
}
}
}
if let Some(section) = file.section_by_name(".probe-rs.chip") {
let data = section.data()?;
if !data.is_empty() {
match String::from_utf8(data.to_vec()) {
Ok(s) => chip = Some(s),
Err(_) => warn!(".probe-rs.chip contents are not a valid utf8 string."),
}
}
}
if let Some(section) = file.section_by_name(".probe-rs.timeout") {
let data = section.data()?;
if data.len() == 4 {
timeout = Some(u32::from_le_bytes(data.try_into().unwrap()) as u64)
} else {
warn!(".probe-rs.timeout contents are not a valid u32.")
}
}
Ok(Self {
version,
chip,
timeout,
})
}
}