use crate::structures::common::{self, StructureError};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct VxWorksSymbolTableEntry {
pub size: usize,
pub name: usize,
pub value: usize,
pub symtype: String,
}
pub fn parse_symtab_entry(
symbol_data: &[u8],
endianness: &str,
) -> Result<VxWorksSymbolTableEntry, StructureError> {
let symtab_structure = vec![
("name_ptr", "u32"),
("value_ptr", "u32"),
("type", "u32"),
("group", "u32"),
];
let allowed_symbol_types: HashMap<usize, String> = HashMap::from([
(0x500, "function".to_string()),
(0x700, "initialized data".to_string()),
(0x900, "uninitialized data".to_string()),
]);
let symtab_structure_size: usize = common::size(&symtab_structure);
if let Ok(symbol_entry) = common::parse(symbol_data, &symtab_structure, endianness) {
if allowed_symbol_types.contains_key(&symbol_entry["type"])
&& symbol_entry["name_ptr"] != 0
&& symbol_entry["value_ptr"] != 0
{
return Ok(VxWorksSymbolTableEntry {
size: symtab_structure_size,
name: symbol_entry["name_ptr"],
value: symbol_entry["value_ptr"],
symtype: allowed_symbol_types[&symbol_entry["type"]].clone(),
});
}
}
Err(StructureError)
}
pub fn get_symtab_endianness(symbol_data: &[u8]) -> Result<String, StructureError> {
const TYPE_FIELD_OFFSET: usize = 9;
let mut endianness = "little";
if let Some(offset_field) = symbol_data.get(TYPE_FIELD_OFFSET) {
if *offset_field == 0 {
endianness = "big";
}
return Ok(endianness.to_string());
}
Err(StructureError)
}