Update to elf v0.1.0

This commit is contained in:
pjht 2022-10-17 13:19:54 -05:00
parent 6c70bdbf9e
commit 690a8b366b
3 changed files with 181 additions and 61 deletions

156
Cargo.lock generated
View File

@ -51,9 +51,9 @@ checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
name = "bitvec"
version = "1.0.0"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1489fcb93a5bb47da0462ca93ad252ad6af2145cce58d10d46a83931ba9f016b"
checksum = "1bc2832c24239b0141d5674bb9174f9d68a8b5b3f2753311927c172ca46f7e9c"
dependencies = [
"funty",
"radium",
@ -118,6 +118,16 @@ dependencies = [
"os_str_bytes",
]
[[package]]
name = "codespan-reporting"
version = "0.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3538270d33cc669650c4b093848450d380def10c331d38c768e34cac80576e6e"
dependencies = [
"termcolor",
"unicode-width",
]
[[package]]
name = "core-foundation-sys"
version = "0.8.3"
@ -152,14 +162,58 @@ dependencies = [
[[package]]
name = "ctor"
version = "0.1.22"
version = "0.1.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f877be4f7c9f246b183111634f75baa039715e3f46ce860677d3b19a69fb229c"
checksum = "cdffe87e1d521a10f9696f833fe502293ea446d7f256c06128293a4119bdf4cb"
dependencies = [
"quote",
"syn",
]
[[package]]
name = "cxx"
version = "1.0.79"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3f83d0ebf42c6eafb8d7c52f7e5f2d3003b89c7aa4fd2b79229209459a849af8"
dependencies = [
"cc",
"cxxbridge-flags",
"cxxbridge-macro",
"link-cplusplus",
]
[[package]]
name = "cxx-build"
version = "1.0.79"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "07d050484b55975889284352b0ffc2ecbda25c0c55978017c132b29ba0818a86"
dependencies = [
"cc",
"codespan-reporting",
"once_cell",
"proc-macro2",
"quote",
"scratch",
"syn",
]
[[package]]
name = "cxxbridge-flags"
version = "1.0.79"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "99d2199b00553eda8012dfec8d3b1c75fce747cf27c169a270b3b99e3448ab78"
[[package]]
name = "cxxbridge-macro"
version = "1.0.79"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dcb67a6de1f602736dd7eaead0080cf3435df806c61b24b13328db128c58868f"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "derive-try-from-primitive"
version = "1.0.0"
@ -179,8 +233,9 @@ checksum = "90e5c1c8368803113bf0c9584fc495a58b86dc8a29edbf8fe877d21d9507e797"
[[package]]
name = "elf"
version = "0.0.12"
source = "git+https://github.com/cole14/rust-elf?rev=a4a747071316b1a900962dd83cbf2ab4ca4665fa#a4a747071316b1a900962dd83cbf2ab4ca4665fa"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "843028ba5ad08ee96716a1fc43b861837be1bc7a87fc0c6aa659ca5ddea6ee48"
[[package]]
name = "errno"
@ -260,18 +315,28 @@ checksum = "d0fb489e3108b684bba475a4cb9804260365870e2f60058265e3d0a3b309bdb1"
[[package]]
name = "iana-time-zone"
version = "0.1.48"
version = "0.1.51"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "237a0714f28b1ee39ccec0770ccb544eb02c9ef2c82bb096230eefcffa6468b0"
checksum = "f5a6ef98976b22b3b7f2f3a806f858cb862044cfa66805aa3ad84cb3d3b785ed"
dependencies = [
"android_system_properties",
"core-foundation-sys",
"iana-time-zone-haiku",
"js-sys",
"once_cell",
"wasm-bindgen",
"winapi",
]
[[package]]
name = "iana-time-zone-haiku"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0703ae284fc167426161c2e3f1da3ea71d94b21bedbcc9494e92b28e334e3dca"
dependencies = [
"cxx",
"cxx-build",
]
[[package]]
name = "indexmap"
version = "1.9.1"
@ -284,9 +349,9 @@ dependencies = [
[[package]]
name = "inventory"
version = "0.3.1"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "30a61b8101d87996f82d725ba701b1987b7afc72f481c13513a30b855b9c9133"
checksum = "e21e0a36a4dc4b469422ee17f715e8313f4a637675656d6a13637954278c6f55"
dependencies = [
"ctor",
"ghost",
@ -309,9 +374,9 @@ dependencies = [
[[package]]
name = "itoa"
version = "1.0.3"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6c8af84674fe1f223a982c933a0ee1086ac4d4052aa0fb8060c12c6ad838e754"
checksum = "4217ad341ebadf8d8e724e264f13e593e0648f5b3e94b3896a5df283be015ecc"
[[package]]
name = "js-sys"
@ -324,9 +389,18 @@ dependencies = [
[[package]]
name = "libc"
version = "0.2.133"
version = "0.2.135"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c0f80d65747a3e43d1596c7c5492d95d5edddaabd45a7fcdb02b95f644164966"
checksum = "68783febc7782c6c5cb401fbda4de5a9898be1762314da0bb2c10ced61f18b0c"
[[package]]
name = "link-cplusplus"
version = "1.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9272ab7b96c9046fbc5bc56c06c117cb639fe2d509df0c421cad82d2915cf369"
dependencies = [
"cc",
]
[[package]]
name = "linux-raw-sys"
@ -482,18 +556,18 @@ checksum = "b1de2e551fb905ac83f73f7aedf2f0cb4a0da7e35efa24a202a936269f1f18e1"
[[package]]
name = "proc-macro2"
version = "1.0.38"
version = "1.0.47"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9027b48e9d4c9175fa2218adf3557f91c1137021739951d4932f5f8268ac48aa"
checksum = "5ea3d908b0e36316caf9e9e2c4625cdde190a7e6f440d794667ed17a1855e725"
dependencies = [
"unicode-xid",
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.18"
version = "1.0.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1feb54ed693b93a84e14094943b84b7c4eae204c512b7ccb95ab0c66d278ad1"
checksum = "bbe448f377a7d6961e30f5955f9b8d106c3f5e449d493ee1b125c1d43c2b5179"
dependencies = [
"proc-macro2",
]
@ -563,9 +637,9 @@ checksum = "a3f87b73ce11b1619a3c6332f45341e0047173771e8b8b73f87bfeefb7b56244"
[[package]]
name = "rustix"
version = "0.35.10"
version = "0.35.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "af895b90e5c071badc3136fc10ff0bcfc98747eadbaf43ed8f214e07ba8f8477"
checksum = "fbb2fda4666def1433b1b05431ab402e42a1084285477222b72d6c564c417cef"
dependencies = [
"bitflags",
"errno",
@ -594,19 +668,25 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd"
[[package]]
name = "serde"
version = "1.0.144"
name = "scratch"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0f747710de3dcd43b88c9168773254e809d8ddbdf9653b84e2554ab219f17860"
checksum = "9c8132065adcfd6e02db789d9285a0deb2f3fcb04002865ab67d5fb103533898"
[[package]]
name = "serde"
version = "1.0.145"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "728eb6351430bccb993660dfffc5a72f91ccc1295abaa8ce19b27ebe4f75568b"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.144"
version = "1.0.145"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "94ed3a816fb1d101812f83e789f888322c34e291f894f19590dc310963e87a00"
checksum = "81fa1584d3d1bcacd84c277a0dfe21f5b0f6accf4a23d04d4c6d61f1af522b4c"
dependencies = [
"proc-macro2",
"quote",
@ -658,9 +738,9 @@ dependencies = [
[[package]]
name = "smallvec"
version = "1.9.0"
version = "1.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2fd0db749597d91ff862fd1d55ea87f7855a744a8425a64695b6fca237d1dad1"
checksum = "a507befe795404456341dfab10cef66ead4c041f62b8b11bbb92bffe5d0953e0"
[[package]]
name = "strip-ansi-escapes"
@ -698,13 +778,13 @@ dependencies = [
[[package]]
name = "syn"
version = "1.0.93"
version = "1.0.102"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "04066589568b72ec65f42d65a1a52436e954b168773148893c020269563decf2"
checksum = "3fcd952facd492f9be3ef0d0b7032a6e442ee9b361d4acc2b1d0c4aaa5f613a1"
dependencies = [
"proc-macro2",
"quote",
"unicode-xid",
"unicode-ident",
]
[[package]]
@ -739,6 +819,12 @@ dependencies = [
"winapi",
]
[[package]]
name = "unicode-ident"
version = "1.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6ceab39d59e4c9499d4e5a8ee0e2735b891bb7308ac83dfb4e80cad195c9f6f3"
[[package]]
name = "unicode-segmentation"
version = "1.10.0"
@ -751,12 +837,6 @@ version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c0edd1e5b14653f783770bce4a4dabb4a5108a5370a5f5d8cfe8710c361f6c8b"
[[package]]
name = "unicode-xid"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "957e51f3646910546462e67d5f7599b9e4fb8acdd304b087a6494730f9eebf04"
[[package]]
name = "unsafe-libyaml"
version = "0.2.4"

View File

@ -8,7 +8,7 @@ edition = "2021"
[dependencies]
bitvec = "1.0.0"
derive-try-from-primitive = "1.0.0"
elf = { git = "https://github.com/cole14/rust-elf", rev = "a4a747071316b1a900962dd83cbf2ab4ca4665fa" }
elf = "0.1.0"
human-repr = { version = "1.0.1", features = ["iec", "space"] }
inventory = "0.3.1"
itertools = "0.10.5"

View File

@ -16,7 +16,7 @@ use crate::{
use disas::DisassemblyError;
use elf::{
gabi::{STT_FILE, STT_SECTION},
symbol::Symbol,
symbol::Symbol as ElfSymbol,
};
use itertools::Itertools;
use parse_int::parse;
@ -26,8 +26,15 @@ use reedline_repl_rs::{
};
use serde_yaml::Mapping;
use std::{
collections::HashMap, convert::TryFrom, error, fmt::Display, fs, num::ParseIntError,
path::Path, process,
collections::HashMap,
convert::TryFrom,
error,
fmt::Display,
fs::{self, File},
io,
num::ParseIntError,
path::Path,
process,
};
#[derive(Debug)]
@ -42,6 +49,13 @@ enum Error {
MiscDyn(Box<dyn error::Error>),
InvalidSymbolTable,
InvalidSymbolName,
Io(io::Error),
}
impl From<io::Error> for Error {
fn from(v: io::Error) -> Self {
Self::Io(v)
}
}
impl From<Box<dyn error::Error>> for Error {
@ -87,6 +101,7 @@ impl Display for Error {
Self::MiscDyn(e) => e.fmt(f),
Self::InvalidSymbolTable => f.write_str("Invalid symbol table"),
Self::InvalidSymbolName => f.write_str("Invalid symbol name"),
Self::Io(e) => e.fmt(f),
}
}
}
@ -217,6 +232,31 @@ impl<'a> Display for LocationDisplayer<'a> {
}
}
struct Symbol {
section: u16,
value: u32,
size: u32,
}
impl From<ElfSymbol> for Symbol {
fn from(sym: ElfSymbol) -> Self {
Self {
section: sym.st_shndx,
value: sym.st_value as u32,
size: sym.st_size as u32,
}
}
}
impl Display for Symbol {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_fmt(format_args!(
"Value: {:#010x} Size: {:#06x} Section: {}",
self.value, self.size, self.section
))
}
}
struct EmuState {
cpu: M68K,
symbol_tables: HashMap<String, HashMap<String, Symbol>>,
@ -514,8 +554,8 @@ fn main() -> Result<(), ReplError> {
for (table_name, table) in state.symbol_tables.iter() {
out += table_name;
out += "\n";
for symbol in table.values() {
out += &format!("{}\n", symbol);
for (name, symbol) in table.iter() {
out += &format!("{name}: {symbol}\n");
}
}
out.pop(); // Remove trailing newline
@ -557,28 +597,28 @@ fn load_symbol_table(
append: bool,
symbol_tables: &mut HashMap<String, HashMap<String, Symbol>>,
) -> Result<(), Error> {
let file = elf::File::open_path(path).map_err(<Box<dyn error::Error>>::from)?;
let symtab = file
.get_section(".symtab")
.ok_or(Error::Misc("Could not find symbol table section"))?;
let symbols = file
.get_symbols(&symtab)
let file =
elf::File::open_stream(&mut File::open(path)?).map_err(<Box<dyn error::Error>>::from)?;
let (symtab, symstrtab) = file
.symbol_table()
.map_err(<Box<dyn error::Error>>::from)?
.into_iter()
.skip(1) // The first symbol is a useless null symbol
.filter(|sym| sym.symtype.0 != STT_FILE && sym.symtype.0 != STT_SECTION)
.map(|sym| (sym.name.clone(), sym))
.ok_or(Error::Misc("No symbol table in file"))?;
let symbols = symtab
.iter()
.skip(1)
.filter(|sym| sym.st_symtype().0 != STT_FILE && sym.st_symtype().0 != STT_SECTION)
.map(|sym| {
(
symstrtab.get(sym.st_name as usize).unwrap().to_string(),
Symbol::from(sym),
)
})
.collect::<HashMap<_, _>>();
let basename = Path::new(&path).file_name().unwrap().to_str().unwrap();
if append {
if let Some(_entry) = symbol_tables.get_mut(basename) {
} else {
symbol_tables.insert(basename.to_string(), symbols);
}
} else {
if !append {
symbol_tables.clear();
symbol_tables.insert(basename.to_string(), symbols);
}
symbol_tables.insert(basename.to_string(), symbols);
Ok(())
}