|
|
|
@ -2,20 +2,22 @@ pub mod types;
|
|
|
|
|
|
|
|
|
|
use tool_helper::{Error, Input};
|
|
|
|
|
use std::io::BufRead;
|
|
|
|
|
use types::{Content, Section};
|
|
|
|
|
use types::{Content, MemoryMapInfo, Section};
|
|
|
|
|
|
|
|
|
|
pub fn scan(input: Input) -> Result<Vec<Section>, Error> {
|
|
|
|
|
let sections = process(input.lines().map(|l| l.unwrap()).into_iter())?;
|
|
|
|
|
pub fn scan(input: Input) -> Result<MemoryMapInfo, Error> {
|
|
|
|
|
let (sections, highest_address) = process(input.lines().map(|l| l.unwrap()).into_iter())?;
|
|
|
|
|
|
|
|
|
|
Ok(sections)
|
|
|
|
|
Ok(MemoryMapInfo{sections, highest_address: highest_address})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn process<F: std::iter::Iterator<Item=String>>(mut line_iter:F) -> Result<Vec<Section>, Error> {
|
|
|
|
|
let mut sections = Vec::new();
|
|
|
|
|
fn process<F: std::iter::Iterator<Item=String>>(mut line_iter:F) -> Result<(Vec<Section>, u64), Error> {
|
|
|
|
|
let mut sections = Vec::new();
|
|
|
|
|
let mut highest_address = 0u64;
|
|
|
|
|
|
|
|
|
|
while let Some(mut line) = line_iter.next() {
|
|
|
|
|
loop {
|
|
|
|
|
if line.starts_with(".") {
|
|
|
|
|
let (new_line, new_section) = process_section(line, &mut line_iter)?;
|
|
|
|
|
let (new_line, new_section) = process_section(&mut highest_address, line, &mut line_iter)?;
|
|
|
|
|
|
|
|
|
|
sections.push(new_section);
|
|
|
|
|
line = new_line;
|
|
|
|
@ -30,19 +32,19 @@ fn process<F: std::iter::Iterator<Item=String>>(mut line_iter:F) -> Result<Vec<S
|
|
|
|
|
sections.sort_by(|left, right| {
|
|
|
|
|
left.adr.cmp(&right.adr)
|
|
|
|
|
});
|
|
|
|
|
Ok(sections)
|
|
|
|
|
Ok((sections, highest_address))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn process_section<F: std::iter::Iterator<Item=String>>(line: String, line_iter:&mut F) -> Result<(String, Section), Error> {
|
|
|
|
|
fn process_section<F: std::iter::Iterator<Item=String>>(highest_address: &mut u64, line: String, line_iter:&mut F) -> Result<(String, Section), Error> {
|
|
|
|
|
let mut next_line_closure = || {
|
|
|
|
|
line_iter.next().ok_or(Error::from_str("Unexpected end of file"))
|
|
|
|
|
};
|
|
|
|
|
let (mut section, line) = read_section(line, &mut next_line_closure)?;
|
|
|
|
|
|
|
|
|
|
Ok((process_subsection(&mut section, line, &mut next_line_closure)?, section))
|
|
|
|
|
Ok((process_subsection(&mut section, highest_address, line, &mut next_line_closure)?, section))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn process_subsection<F:FnMut()-> Result<String, Error>>(section: &mut Section, line: Option<String>, next_line: &mut F) -> Result<String, Error> {
|
|
|
|
|
fn process_subsection<F:FnMut()-> Result<String, Error>>(section: &mut Section, highest_address: &mut u64, line: Option<String>, next_line: &mut F) -> Result<String, Error> {
|
|
|
|
|
fn push_sub_section(section: &mut Section, sub_section: Option<Section>) {
|
|
|
|
|
if let Some(sub_section) = sub_section {
|
|
|
|
|
section.content.push(Content::Section(sub_section));
|
|
|
|
@ -105,6 +107,10 @@ fn process_subsection<F:FnMut()-> Result<String, Error>>(section: &mut Section,
|
|
|
|
|
|
|
|
|
|
else if line.starts_with(" 0x") {
|
|
|
|
|
if let Some(symbol) = parse_symbol(read_symbol(line)?) {
|
|
|
|
|
if symbol.adr > *highest_address {
|
|
|
|
|
*highest_address = symbol.adr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sub_section = add_element(sub_section, section, symbol, |section, symbol| {
|
|
|
|
|
section.content.push(Content::Symbol(symbol));
|
|
|
|
|
});
|
|
|
|
|