Skip to content

armgit / gecko

Public Imported from GitHub
0

A GameCube/Wii emulator and debugger written in Rust.

ida symbols

Layleb9a59e56d22a
diff --git a/Cargo.lock b/Cargo.lock
index 2749626..23394da 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -153,6 +153,12 @@ dependencies = [
"windows-sys 0.61.2",
]
+[[package]]
+name = "anyhow"
+version = "1.0.102"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "7f202df86484c868dbad7eaa557ef785d5c66295e41b460ef922eca0723b842c"
+
[[package]]
name = "arboard"
version = "3.6.1"
@@ -239,6 +245,15 @@ version = "0.22.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6"
+[[package]]
+name = "bincode"
+version = "1.3.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b1f45e9417d87227c7a56d22e471c6206462cba514c7590c09aff4cf6d1ddcad"
+dependencies = [
+ "serde",
+]
+
[[package]]
name = "bit-set"
version = "0.8.0"
@@ -265,6 +280,9 @@ name = "bitflags"
version = "2.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "843867be96c8daad0d758b57df9392b6d8d271134fce549de6ce169ff98a92af"
+dependencies = [
+ "serde_core",
+]
[[package]]
name = "block"
@@ -1194,12 +1212,29 @@ version = "0.3.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7e3450815272ef58cec6d564423f6e755e25379b217b0bc688e295ba24df6b1d"
+[[package]]
+name = "futures-macro"
+version = "0.3.32"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e835b70203e41293343137df5c0664546da5745f82ec9b84d40be8336958447b"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "syn",
+]
+
[[package]]
name = "futures-task"
version = "0.3.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "037711b3d59c33004d3856fbdc83b99d4ff37a24768fa1be9ce3538a1cde4393"
+[[package]]
+name = "futures-timer"
+version = "3.0.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f288b0a4f20f9a56b5d1da57e2227c661b7b16168e2f72365f57b63326e29b24"
+
[[package]]
name = "futures-util"
version = "0.3.32"
@@ -1207,6 +1242,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "389ca41296e6190b48053de0321d02a77f32f8a5d2461dd38762c0593805c6d6"
dependencies = [
"futures-core",
+ "futures-macro",
"futures-task",
"pin-project-lite",
"slab",
@@ -1286,6 +1322,12 @@ dependencies = [
"encase",
]
+[[package]]
+name = "glob"
+version = "0.3.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "0cc23270f6e1808e30a928bdc84dea0b9b4136a8bc82338574f23baf47bbd280"
+
[[package]]
name = "glow"
version = "0.16.0"
@@ -1472,6 +1514,24 @@ dependencies = [
"zerovec",
]
+[[package]]
+name = "idb-rs"
+version = "0.1.13"
+source = "git+https://github.com/ioncodes/idb-rs.git#843ae819f16aec0051c13b77d3ffccbbc70a3318"
+dependencies = [
+ "anyhow",
+ "bincode",
+ "clap",
+ "flate2",
+ "num-traits",
+ "num_enum",
+ "ron",
+ "rstest",
+ "rustc-hash 2.1.1",
+ "serde",
+ "zstd",
+]
+
[[package]]
name = "idna"
version = "1.1.0"
@@ -1497,6 +1557,7 @@ dependencies = [
name = "image"
version = "0.1.0"
dependencies = [
+ "idb-rs",
"object",
"zerocopy",
]
@@ -2720,12 +2781,61 @@ version = "0.8.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dc897dd8d9e8bd1ed8cdad82b5966c3e0ecae09fb1907d58efaa013543185d0a"
+[[package]]
+name = "relative-path"
+version = "1.9.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ba39f3699c378cd8970968dcbff9c43159ea4cfbd88d43c00b22f2ef10a435d2"
+
[[package]]
name = "renderdoc-sys"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "19b30a45b0cd0bcca8037f3d0dc3421eaf95327a17cad11964fb8179b4fc4832"
+[[package]]
+name = "ron"
+version = "0.10.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "beceb6f7bf81c73e73aeef6dd1356d9a1b2b4909e1f0fc3e59b034f9572d7b7f"
+dependencies = [
+ "base64",
+ "bitflags 2.11.0",
+ "serde",
+ "serde_derive",
+ "unicode-ident",
+]
+
+[[package]]
+name = "rstest"
+version = "0.25.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "6fc39292f8613e913f7df8fa892b8944ceb47c247b78e1b1ae2f09e019be789d"
+dependencies = [
+ "futures-timer",
+ "futures-util",
+ "rstest_macros",
+ "rustc_version",
+]
+
+[[package]]
+name = "rstest_macros"
+version = "0.25.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "1f168d99749d307be9de54d23fd226628d99768225ef08f6ffb52e0182a27746"
+dependencies = [
+ "cfg-if",
+ "glob",
+ "proc-macro-crate",
+ "proc-macro2",
+ "quote",
+ "regex",
+ "relative-path",
+ "rustc_version",
+ "syn",
+ "unicode-ident",
+]
+
[[package]]
name = "rustc-hash"
version = "1.1.0"
@@ -4513,6 +4623,34 @@ dependencies = [
"syn",
]
+[[package]]
+name = "zstd"
+version = "0.13.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e91ee311a569c327171651566e07972200e76fcfe2242a4fa446149a3881c08a"
+dependencies = [
+ "zstd-safe",
+]
+
+[[package]]
+name = "zstd-safe"
+version = "7.2.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "8f49c4d5f0abb602a93fb8736af2a4f4dd9512e36f7f570d66e65ff867ed3b9d"
+dependencies = [
+ "zstd-sys",
+]
+
+[[package]]
+name = "zstd-sys"
+version = "2.0.16+zstd.1.5.7"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "91e19ebc2adc8f83e43039e79776e3fda8ca919132d68a1fed6a5faca2683748"
+dependencies = [
+ "cc",
+ "pkg-config",
+]
+
[[package]]
name = "zune-core"
version = "0.5.1"
diff --git a/crates/debugger/src/main.rs b/crates/debugger/src/main.rs
index dbbe567..6be5d98 100644
--- a/crates/debugger/src/main.rs
+++ b/crates/debugger/src/main.rs
@@ -93,9 +93,9 @@ struct Args {
#[arg(long)]
dsp: Option<String>,
- /// Path to a companion ELF file for symbol names
+ /// Path to a symbol file (ELF, IDA .idb, or .i64)
#[arg(long)]
- elf: Option<String>,
+ symbols: Option<String>,
/// Path to a Lua script for scripting hooks
#[arg(long)]
@@ -152,10 +152,10 @@ fn main() {
emulator.set_hook_host(Box::new(host));
}
- let symbols = args.elf.as_ref().map(|path| {
- let elf_data = std::fs::read(path).expect("failed to read ELF file");
- image::elf::parse_elf_symbols(&elf_data).expect("failed to parse ELF symbols")
- });
+ let symbols = args
+ .symbols
+ .as_ref()
+ .map(|path| image::loader::load_symbols(std::path::Path::new(path)).expect("failed to load symbols"));
emulator.add_primary_controller(PadStatus {
connected: true,
diff --git a/crates/image/Cargo.toml b/crates/image/Cargo.toml
index ae92478..e3a5b24 100644
--- a/crates/image/Cargo.toml
+++ b/crates/image/Cargo.toml
@@ -4,5 +4,6 @@ version = "0.1.0"
edition = "2024"
[dependencies]
+idb-rs = { git = "https://github.com/ioncodes/idb-rs.git" }
object = { version = "0.36.7", default-features = false, features = ["elf", "read"] }
zerocopy = { version = "0.8.48", features = ["derive"] }
diff --git a/crates/image/src/idb.rs b/crates/image/src/idb.rs
new file mode 100644
index 0000000..4915e03
--- /dev/null
+++ b/crates/image/src/idb.rs
@@ -0,0 +1,155 @@
+use crate::symbols::{Symbol, SymbolKind, SymbolTable};
+use idb_rs::addr_info::AddressInfo;
+use idb_rs::id0::function::IDBFunctionType;
+use idb_rs::{Address, IDAKind, IDAVariants, IDBFormat, IDBFormats, identify_idb_file};
+use std::io::{BufRead, BufReader, Cursor, Seek};
+
+#[derive(Debug)]
+pub enum IdbError {
+ Parse(String),
+}
+
+impl std::fmt::Display for IdbError {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ match self {
+ IdbError::Parse(msg) => write!(f, "IDB parse error: {msg}"),
+ }
+ }
+}
+
+impl std::error::Error for IdbError {}
+
+pub fn parse_idb_symbols(data: &[u8]) -> Result<SymbolTable, IdbError> {
+ let mut cursor = BufReader::new(Cursor::new(data));
+ let format = identify_idb_file(&mut cursor).map_err(|e| IdbError::Parse(e.to_string()))?;
+
+ match format {
+ IDBFormats::Separated(IDAVariants::IDA32(sections)) => extract_symbols(sections, &mut cursor),
+ IDBFormats::Separated(IDAVariants::IDA64(sections)) => extract_symbols(sections, &mut cursor),
+ IDBFormats::InlineUncompressed(sections) => extract_symbols(sections, &mut cursor),
+ IDBFormats::InlineCompressed(compressed) => {
+ let mut decompressed = Vec::new();
+ let sections = compressed
+ .decompress_into_memory(&mut cursor, &mut decompressed)
+ .map_err(|e| IdbError::Parse(e.to_string()))?;
+ extract_symbols(sections, Cursor::new(&decompressed[..]))
+ }
+ }
+}
+
+fn extract_symbols<K, F, I>(idb: F, mut input: I) -> Result<SymbolTable, IdbError>
+where
+ K: IDAKind,
+ F: IDBFormat<K>,
+ I: BufRead + Seek,
+ u64: From<K::Usize>,
+{
+ let id0_loc = idb
+ .id0_location()
+ .ok_or_else(|| IdbError::Parse("IDB has no ID0 section".into()))?;
+ let id1_loc = idb
+ .id1_location()
+ .ok_or_else(|| IdbError::Parse("IDB has no ID1 section".into()))?;
+ let id2_loc = idb.id2_location();
+
+ let id0 = idb
+ .read_id0(&mut input, id0_loc)
+ .map_err(|e| IdbError::Parse(e.to_string()))?;
+ let id1 = idb
+ .read_id1(&mut input, id1_loc)
+ .map_err(|e| IdbError::Parse(e.to_string()))?;
+ let id2 = id2_loc
+ .map(|loc| idb.read_id2(&mut input, loc))
+ .transpose()
+ .map_err(|e| IdbError::Parse(e.to_string()))?;
+
+ let root_info_idx = id0.root_node().map_err(|e| IdbError::Parse(e.to_string()))?;
+ let root_info = id0
+ .ida_info(root_info_idx)
+ .map_err(|e| IdbError::Parse(e.to_string()))?;
+ let netdelta = root_info.netdelta();
+
+ let mut symbols = Vec::new();
+
+ // extract function chunks with address ranges
+ if let Some(funcs_idx) = id0.funcs_idx().map_err(|e| IdbError::Parse(e.to_string()))? {
+ for func_result in id0.fchunks(funcs_idx) {
+ let func = match func_result {
+ Ok(f) => f,
+ Err(_) => continue,
+ };
+
+ // skip tail chunks, only take real function entries
+ if matches!(func.extra, IDBFunctionType::Tail(_)) {
+ continue;
+ }
+
+ let start: u64 = func.address.start.into_raw().into();
+ let end: u64 = func.address.end.into_raw().into();
+ let addr = start as u32;
+ let size = (end - start) as u32;
+
+ // resolve name via AddressInfo label
+ let name = AddressInfo::new(&id0, &id1, id2.as_ref(), netdelta, func.address.start)
+ .and_then(|info| info.label().ok().flatten())
+ .map(|s| s.as_utf8_lossy().into_owned());
+
+ // fall back to forced lookup (for labels outside mapped regions)
+ let name = name.or_else(|| {
+ AddressInfo::new_forced(&id0, netdelta, func.address.start)
+ .and_then(|info| info.label().ok().flatten())
+ .map(|s| s.as_utf8_lossy().into_owned())
+ });
+
+ if let Some(name) = name {
+ if !name.is_empty() {
+ symbols.push(Symbol {
+ name,
+ addr,
+ size,
+ kind: SymbolKind::Func,
+ });
+ }
+ }
+ }
+ }
+
+ // also pick up named addresses from the dirtree that aren't already covered
+ if let Ok(Some(dirtree)) = id0.dirtree_function_address() {
+ let mut entries = dirtree.entries;
+ while let Some(entry) = entries.pop() {
+ match entry {
+ idb_rs::id0::DirTreeEntry::Leaf(raw_addr) => {
+ let addr32: u64 = raw_addr.into();
+ let addr32 = addr32 as u32;
+
+ // skip if we already have a symbol at this address
+ if symbols.iter().any(|s| s.addr == addr32) {
+ continue;
+ }
+
+ let address = Address::<K>::from_raw(raw_addr);
+ let name = AddressInfo::new(&id0, &id1, id2.as_ref(), netdelta, address)
+ .and_then(|info| info.label().ok().flatten())
+ .map(|s| s.as_utf8_lossy().into_owned());
+
+ if let Some(name) = name {
+ if !name.is_empty() {
+ symbols.push(Symbol {
+ name,
+ addr: addr32,
+ size: 0,
+ kind: SymbolKind::Func,
+ });
+ }
+ }
+ }
+ idb_rs::id0::DirTreeEntry::Directory { entries: sub, .. } => {
+ entries.extend(sub);
+ }
+ }
+ }
+ }
+
+ Ok(SymbolTable::new(symbols))
+}
diff --git a/crates/image/src/lib.rs b/crates/image/src/lib.rs
index ebffd5d..cf9d5f5 100644
--- a/crates/image/src/lib.rs
+++ b/crates/image/src/lib.rs
@@ -1,5 +1,7 @@
pub mod dvd;
pub mod elf;
+pub mod idb;
+pub mod loader;
pub mod symbols;
pub struct Section {
diff --git a/crates/image/src/loader.rs b/crates/image/src/loader.rs
new file mode 100644
index 0000000..e71b52c
--- /dev/null
+++ b/crates/image/src/loader.rs
@@ -0,0 +1,56 @@
+use crate::elf::ElfError;
+use crate::idb::IdbError;
+use crate::symbols::SymbolTable;
+use std::path::Path;
+
+#[derive(Debug)]
+pub enum SymbolLoadError {
+ Io(std::io::Error),
+ UnknownFormat(String),
+ Elf(ElfError),
+ Idb(IdbError),
+}
+
+impl std::fmt::Display for SymbolLoadError {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ match self {
+ SymbolLoadError::Io(e) => write!(f, "I/O error: {e}"),
+ SymbolLoadError::UnknownFormat(ext) => {
+ write!(f, "unknown symbol file format: {ext}")
+ }
+ SymbolLoadError::Elf(e) => write!(f, "{e}"),
+ SymbolLoadError::Idb(e) => write!(f, "{e}"),
+ }
+ }
+}
+
+impl std::error::Error for SymbolLoadError {}
+
+impl From<std::io::Error> for SymbolLoadError {
+ fn from(e: std::io::Error) -> Self {
+ SymbolLoadError::Io(e)
+ }
+}
+
+impl From<ElfError> for SymbolLoadError {
+ fn from(e: ElfError) -> Self {
+ SymbolLoadError::Elf(e)
+ }
+}
+
+impl From<IdbError> for SymbolLoadError {
+ fn from(e: IdbError) -> Self {
+ SymbolLoadError::Idb(e)
+ }
+}
+
+pub fn load_symbols(path: &Path) -> Result<SymbolTable, SymbolLoadError> {
+ let data = std::fs::read(path)?;
+
+ match path.extension().and_then(|e| e.to_str()) {
+ Some("elf") => Ok(crate::elf::parse_elf_symbols(&data)?),
+ Some("idb") | Some("i64") => Ok(crate::idb::parse_idb_symbols(&data)?),
+ Some(ext) => Err(SymbolLoadError::UnknownFormat(ext.to_string())),
+ None => Err(SymbolLoadError::UnknownFormat("(no extension)".to_string())),
+ }
+}
diff --git a/crates/tinytracer/src/main.rs b/crates/tinytracer/src/main.rs
index 038cf61..2fe4170 100644
--- a/crates/tinytracer/src/main.rs
+++ b/crates/tinytracer/src/main.rs
@@ -52,9 +52,9 @@ struct Args {
#[arg(long, value_parser = parse_hex_addr)]
until: Option<u32>,
- /// Path to a companion ELF file for symbol names
+ /// Path to a symbol file (ELF, IDA .idb, or .i64)
#[arg(long)]
- elf: Option<String>,
+ symbols: Option<String>,
/// Suppress all stdout output (tracing is unaffected)
#[arg(long)]
@@ -121,10 +121,10 @@ fn main() {
emulator.set_hook_host(Box::new(host));
}
- let symbols = args.elf.as_ref().map(|path| {
- let elf_data = std::fs::read(path).expect("failed to read ELF file");
- image::elf::parse_elf_symbols(&elf_data).expect("failed to parse ELF symbols")
- });
+ let symbols = args
+ .symbols
+ .as_ref()
+ .map(|path| image::loader::load_symbols(std::path::Path::new(path)).expect("failed to load symbols"));
run_emulator(&mut emulator, &args, symbols.as_ref());