Collect Symbols

This commit is contained in:
Jaby 2023-07-20 20:31:25 +02:00
parent 2ab4e8b155
commit 8d1dda56ef
2 changed files with 66 additions and 8 deletions

View File

@ -1,7 +1,7 @@
pub mod types;
use tool_helper::Error;
use types::{MemoryMap, Section};
use types::{MemoryMap, Section, Symbol};
pub fn scan<F: FnMut() -> Option<String>>(mut next_line: F) -> Result<MemoryMap, Error> {
const SECTION_IDENTIFIER:&'static str = "Sections:";
@ -19,23 +19,76 @@ pub fn scan<F: FnMut() -> Option<String>>(mut next_line: F) -> Result<MemoryMap,
if line == SYMBOL_TABLE_IDENTIFIER {
// Process Symbol table here
line = scan_symbols(&mut next_line, &mut memory_map)?;
continue;
}
break;
}
}
// Print them off for now!
println!("Name: <Global> ({})", 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<F: FnMut() -> Option<String>>(next_line: &mut F, memory_map: &mut MemoryMap) -> Result<String, Error> {
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<F: FnMut() -> Option<String>>(next_line: &mut F, sections: &mut Vec<Section>) -> Result<String, Error> {
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<F: FnMut() -> Option<String>>(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<u64, Error> {

View File

@ -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}
}
}