From d6e459ee654dd6bb250f0ca4d442b9f21fd0de36 Mon Sep 17 00:00:00 2001 From: Jaby Date: Thu, 20 Jul 2023 20:31:25 +0200 Subject: [PATCH] Collect Symbols --- src/Tools/readmap/src/lib.rs | 65 +++++++++++++++++++++++++++--- src/Tools/readmap/src/types/mod.rs | 9 ++++- 2 files changed, 66 insertions(+), 8 deletions(-) diff --git a/src/Tools/readmap/src/lib.rs b/src/Tools/readmap/src/lib.rs index 932a58d9..9dfd3c51 100644 --- a/src/Tools/readmap/src/lib.rs +++ b/src/Tools/readmap/src/lib.rs @@ -1,7 +1,7 @@ pub mod types; use tool_helper::Error; -use types::{MemoryMap, Section}; +use types::{MemoryMap, Section, Symbol}; pub fn scan Option>(mut next_line: F) -> Result { const SECTION_IDENTIFIER:&'static str = "Sections:"; @@ -19,23 +19,76 @@ pub fn scan Option>(mut next_line: F) -> Result ({})", memory_map.global.len()); for section in memory_map.sections { - println!("Name: {}", section.name); + println!("Name: {} ({})", section.name, section.symbols.len()); } - Err(Error::not_implemented("scan")) + Err(Error::from_str("Symbols should be sorted by address now - not implemented yet")) +} + +fn scan_symbols Option>(next_line: &mut F, memory_map: &mut MemoryMap) -> Result { + fn find_section<'a>(name: &str, memory_map: &'a mut MemoryMap) -> Option<&'a mut Section> { + for section in &mut memory_map.sections { + if section.name == name { + return Some(section); + } + } + + None + } + + fn skip_symbol(name: &str, section_name: &str) -> bool { + name == section_name || name.ends_with(".cpp") || name.ends_with(".c") || name.ends_with(".cxx") + } + + while let Some(line) = next_line() { + if line.chars().all(char::is_whitespace) { + return Ok(line); + } + + let split_count = line.split_whitespace().count(); + let mut split_line = line.split_whitespace(); + let adr = split_line_radix(&mut split_line, 16, "Address of Symbol")?; + let _type = split_line.next().ok_or(Error::from_str("Failed obtaining type of symbol"))?; + if split_count > 5 { + let _flag = split_line.next().ok_or(Error::from_str("Failed obtaining flag of symbol"))?; + } + let section = split_line.next().ok_or(Error::from_str("Failed obtaining section of symbol"))?; + let size = split_line_radix(&mut split_line, 16, "Size of Symbol")? as usize; + let name = split_line.next().ok_or(Error::from_str("Failed obtaining name of symbol"))?; + + if skip_symbol(name, section) { + // Not of interest + continue; + } + + let symbol = Symbol::new(name.to_string(), adr, size); + if let Some(section) = find_section(section, memory_map) { + if section.contains_adr(adr) { + section.symbols.push(symbol); + // Do not add to global cause they have a section + continue; + } + } + + memory_map.global.push(symbol); + } + + Err(Error::from_str("Failed obtaining all Symbols")) } fn scan_sections Option>(next_line: &mut F, sections: &mut Vec
) -> Result { while let Some(_) = next_line() { // We read every other line - let line = next_line().ok_or(Error::from_str("Failed skipping section line"))?; + let line = next_line().ok_or(Error::from_str("Failed skipping to next section"))?; let mut split_line = line.split_whitespace(); if let Ok(_) = split_line_radix(&mut split_line, 10, "") { let name = split_line.next().ok_or(Error::from_str("Failed reading Section Name"))?; @@ -50,7 +103,7 @@ fn scan_sections Option>(next_line: &mut F, sections: &mut } } - Err(Error::not_implemented("Failed obtaining complete section")) + Err(Error::from_str("Failed obtaining complete section")) } fn split_line_radix<'a>(split_line: &mut std::str::SplitWhitespace<'a>, radix: u32, value_name: &str) -> Result { diff --git a/src/Tools/readmap/src/types/mod.rs b/src/Tools/readmap/src/types/mod.rs index 80b60f7c..0509eda3 100644 --- a/src/Tools/readmap/src/types/mod.rs +++ b/src/Tools/readmap/src/types/mod.rs @@ -18,16 +18,21 @@ impl Section { pub fn new(name: String, adr: u64, size: usize) -> Section { Section{name, adr, size, symbols: Vec::new()} } + + pub fn contains_adr(&self, adr: u64) -> bool { + adr >= self.adr && adr < (self.adr + self.size as u64) + } } #[derive(Default)] pub struct Symbol { pub name: String, pub adr: u64, + pub size: usize } impl Symbol { - pub fn new(name: String, adr: u64) -> Symbol { - Symbol{name, adr} + pub fn new(name: String, adr: u64, size: usize) -> Symbol { + Symbol{name, adr, size} } } \ No newline at end of file