diff --git a/agb-addr2line/src/load_dwarf.rs b/agb-addr2line/src/load_dwarf.rs index 6282d531..6662f231 100644 --- a/agb-addr2line/src/load_dwarf.rs +++ b/agb-addr2line/src/load_dwarf.rs @@ -13,19 +13,29 @@ pub fn load_dwarf( return load_from_object(&object); } + // the file might have been padded, so ensure we skip any padding before continuing + let last_non_zero_byte = file_content + .iter() + .rposition(|&b| b != 0) + .ok_or_else(|| anyhow::anyhow!("Gba file is empty"))?; + + let file_content = &file_content[..last_non_zero_byte + 1]; + let last_8_bytes = &file_content[file_content.len() - 8..]; let len = u32::from_le_bytes(last_8_bytes[0..4].try_into()?) as usize; - let version = u32::from_le_bytes(last_8_bytes[4..].try_into()?) as usize; + let version = &last_8_bytes[4..]; - if version != 1 { - bail!("Only version 1 of the debug info is supported"); + if version != b"agb1" { + bail!("Failed to load debug information from ROM file, it might not have been included?"); } let compressed_debug_data = &file_content[file_content.len() - len - 8..file_content.len() - 8]; let decompressing_reader = lz4_flex::frame::FrameDecoder::new(Cursor::new(compressed_debug_data)); - let debug_info: HashMap> = rmp_serde::decode::from_read(decompressing_reader)?; + let debug_info: HashMap> = + rmp_serde::decode::from_read(decompressing_reader) + .map_err(|e| anyhow::anyhow!("Failed to load debug information: {e}"))?; let dwarf = gimli::Dwarf::load(|id| { let data = debug_info diff --git a/agb-gbafix/src/lib.rs b/agb-gbafix/src/lib.rs index f2636550..5cfe2d9d 100644 --- a/agb-gbafix/src/lib.rs +++ b/agb-gbafix/src/lib.rs @@ -173,7 +173,7 @@ fn write_debug( output.write_all(&debug_data)?; output.write_all(&(debug_data.len() as u32).to_le_bytes())?; - output.write_all(&(1u32).to_le_bytes())?; + output.write_all(b"agb1")?; Ok(debug_data.len() as u64 + 4) }