diff --git a/objdiff-core/src/arch/superh/mod.rs b/objdiff-core/src/arch/superh/mod.rs index 6dc37355..fad267f9 100644 --- a/objdiff-core/src/arch/superh/mod.rs +++ b/objdiff-core/src/arch/superh/mod.rs @@ -1,7 +1,7 @@ use alloc::{collections::BTreeMap, format, vec, vec::Vec}; use anyhow::Result; -use object::elf; +use object::{Endian as _, Object as _, elf}; use crate::{ arch::{Arch, superh::disasm::sh2_disasm}, @@ -12,10 +12,13 @@ use crate::{ pub mod disasm; #[derive(Debug)] -pub struct ArchSuperH {} +pub struct ArchSuperH { + /// Byte order of the object's code: big-endian on the Saturn, little-endian on the Dreamcast. + endianness: object::Endianness, +} impl ArchSuperH { - pub fn new(_file: &object::File) -> Result { Ok(Self {}) } + pub fn new(file: &object::File) -> Result { Ok(Self { endianness: file.endianness() }) } } struct DataInfo { @@ -36,7 +39,7 @@ impl Arch for ArchSuperH { let mut offset = address; for chunk in code.as_chunks::<2>().0 { - let opcode = u16::from_be_bytes(*chunk); + let opcode = self.endianness.read_u16(*chunk); let mut parts: Vec = vec![]; let resolved: ResolvedInstructionRef = Default::default(); let mut branch_dest: Option = None; @@ -66,7 +69,7 @@ impl Arch for ArchSuperH { _diff_config: &DiffObjConfig, cb: &mut dyn FnMut(InstructionPart) -> Result<()>, ) -> Result<()> { - let opcode = u16::from_be_bytes(resolved.code.try_into().unwrap()); + let opcode = self.endianness.read_u16(resolved.code.try_into().unwrap()); let mut parts: Vec = vec![]; let mut branch_dest: Option = None; @@ -81,7 +84,7 @@ impl Arch for ArchSuperH { let mut pos: u64 = 0; for chunk in symbol_data.as_chunks::<2>().0 { - let opcode = u16::from_be_bytes(*chunk); + let opcode = self.endianness.read_u16(*chunk); // mov.w if (opcode & 0xf000) == 0x9000 { let target = (opcode as u64 & 0xff) * 2 + 4 + pos; @@ -102,7 +105,7 @@ impl Arch for ArchSuperH { // add the data info if let Some(value) = data_offsets.get(&pos) { if value.size == 2 && value.address as usize + 1 < symbol_data.len() { - let data = u16::from_be_bytes( + let data = self.endianness.read_u16( symbol_data[value.address as usize..value.address as usize + 2] .try_into() .unwrap(), @@ -112,7 +115,7 @@ impl Arch for ArchSuperH { parts.push(InstructionPart::basic(format!("{data:04X}"))); parts.push(InstructionPart::basic(" */")); } else if value.size == 4 && value.address as usize + 3 < symbol_data.len() { - let data = u32::from_be_bytes( + let data = self.endianness.read_u32( symbol_data[value.address as usize..value.address as usize + 4] .try_into() .unwrap(), @@ -217,7 +220,7 @@ mod test { #[test] fn test_sh2_display_instruction_basic_ops() { - let arch = ArchSuperH {}; + let arch = ArchSuperH { endianness: object::Endianness::Big }; let ops: [(u16, &str); 8] = [ (0x0008, "clrt "), (0x0028, "clrmac "), @@ -254,7 +257,7 @@ mod test { #[test] fn test_sh2_display_instruction_f0ff_ops() { - let arch = ArchSuperH {}; + let arch = ArchSuperH { endianness: object::Endianness::Big }; let ops: [(u16, &str); 49] = [ (0x4015, "cmp/pl r0"), (0x4115, "cmp/pl r1"), @@ -332,7 +335,7 @@ mod test { #[test] fn test_sh2_display_instructions_f00f() { - let arch = ArchSuperH {}; + let arch = ArchSuperH { endianness: object::Endianness::Big }; let ops: [(u16, &str); 54] = [ (0x300c, "add r0, r0"), (0x300e, "addc r0, r0"), @@ -415,7 +418,7 @@ mod test { #[test] fn test_sh2_display_instruction_mov_immediate_offset() { - let arch = ArchSuperH {}; + let arch = ArchSuperH { endianness: object::Endianness::Big }; let ops: [(u16, &str); 8] = [ (0x8000, "mov.b r0, @(0x0, r0)"), (0x8011, "mov.b r0, @(0x1, r1)"), @@ -452,7 +455,7 @@ mod test { #[test] fn test_sh2_display_instruction_gbr_and_branches() { - let arch = ArchSuperH {}; + let arch = ArchSuperH { endianness: object::Endianness::Big }; let ops: &[(u16, u32, &str)] = &[ (0xc000, 0x0000, "mov.b r0, @(0x0, gbr)"), (0xc07f, 0x0000, "mov.b r0, @(0x7f, gbr)"), @@ -506,7 +509,7 @@ mod test { #[test] fn test_sh2_display_instruction_mov_l() { - let arch = ArchSuperH {}; + let arch = ArchSuperH { endianness: object::Endianness::Big }; let ops: &[(u16, u32, &str)] = &[ // mov.l rX, @(0xXXX, rY) (0x1000, 0x0000, "mov.l r0, @(0x0, r0)"), @@ -547,7 +550,7 @@ mod test { #[test] fn test_sh2_display_instruction_bra_bsr() { - let arch: ArchSuperH = ArchSuperH {}; + let arch: ArchSuperH = ArchSuperH { endianness: object::Endianness::Big }; let ops: &[(u16, u32, &str)] = &[ // bra (0xa000, 0x0000, "bra 0x4"), @@ -591,7 +594,7 @@ mod test { #[test] fn test_sh2_display_instruction_operations() { - let arch = ArchSuperH {}; + let arch = ArchSuperH { endianness: object::Endianness::Big }; let ops: &[(u16, u32, &str)] = &[ (0xcdff, 0x0000, "and.b #0xff, @(r0, gbr)"), (0xcfff, 0x0000, "or.b #0xff, @(r0, gbr)"), @@ -635,7 +638,7 @@ mod test { #[test] fn test_sh2_add_mov_unknown_instructions() { - let arch = ArchSuperH {}; + let arch = ArchSuperH { endianness: object::Endianness::Big }; let ops: &[(u16, u32, &str)] = &[ (0x70FF, 0x0000, "add #0xff, r0"), (0xE0FF, 0x0000, "mov #0xff, r0"), @@ -672,7 +675,7 @@ mod test { #[test] fn test_sh2_mov_instructions_with_labels() { - let arch = ArchSuperH {}; + let arch = ArchSuperH { endianness: object::Endianness::Big }; let ops: &[(u16, u32, &str)] = &[(0x9000, 0x0000, "mov.w @(0x4, pc), r0"), (0xd000, 0x0000, "mov.l @(0x4, pc), r0")]; @@ -706,7 +709,7 @@ mod test { #[test] fn test_func_0606_f378_mov_w_data_labeling() { - let arch = ArchSuperH {}; + let arch = ArchSuperH { endianness: object::Endianness::Big }; let ops: &[(u16, u32, &str)] = &[(0x9000, 0x0606F378, "mov.w @(0x4, pc), r0 /* 0x00B0 */")]; let mut code = Vec::new(); @@ -754,7 +757,7 @@ mod test { #[test] fn test_func_0606_f378_mov_l_data_labeling() { - let arch = ArchSuperH {}; + let arch = ArchSuperH { endianness: object::Endianness::Big }; let ops: &[(u16, u32, &str)] = &[(0xd000, 0x0606F378, "mov.l @(0x4, pc), r0 /* 0x00B000B0 */")]; @@ -801,4 +804,77 @@ mod test { assert_eq!(joined_str, expected_str.to_string()); } } + + /// A little-endian function (Dreamcast) and its literal pool: the same code as big-endian + /// (Saturn) must scan to the same instructions and display the same. + #[test] + fn test_sh4_little_endian_matches_big_endian() { + const ADDRESS: u64 = 0x8C010000; + let ops: [(u16, &str); 6] = [ + (0xd102, "mov.l @(0xc, pc), r1 /* 0x8C0FF920 */"), + (0xa000, "bra 0x4"), // to ADDRESS + 6 + (0x0009, "nop "), + (0x000b, "rts "), + (0x0009, "nop "), + (0x0009, "nop "), // pads the literal pool to 4 bytes + ]; + let pool: u32 = 0x8C0FF920; + + for endianness in [object::Endianness::Little, object::Endianness::Big] { + let arch = ArchSuperH { endianness }; + let mut code = Vec::new(); + for (opcode, _) in ops { + code.extend_from_slice(&match endianness { + object::Endianness::Little => opcode.to_le_bytes(), + object::Endianness::Big => opcode.to_be_bytes(), + }); + } + let code_len = code.len(); + code.extend_from_slice(&match endianness { + object::Endianness::Little => pool.to_le_bytes(), + object::Endianness::Big => pool.to_be_bytes(), + }); + + let instructions = arch + .scan_instructions_internal( + ADDRESS, + &code[..code_len], + 0, + &[], + &DiffObjConfig::default(), + ) + .unwrap(); + assert_eq!(instructions.len(), ops.len()); + assert_eq!(instructions[1].branch_dest, Some(ADDRESS + 6), "{endianness:?}"); + + let symbol = Symbol { address: ADDRESS, size: code.len() as u64, ..Default::default() }; + let section = Section { + address: ADDRESS, + size: code.len() as u64, + data: SectionData(code.clone()), + ..Default::default() + }; + for (ins_ref, (_, expected_str)) in instructions.iter().zip(ops) { + let offset = (ins_ref.address - ADDRESS) as usize; + let mut parts = Vec::new(); + arch.display_instruction( + ResolvedInstructionRef { + ins_ref: *ins_ref, + code: &code[offset..offset + 2], + symbol: &symbol, + section: §ion, + ..Default::default() + }, + &DiffObjConfig::default(), + &mut |part| { + parts.push(part.into_static()); + Ok(()) + }, + ) + .unwrap(); + let joined_str: String = parts.iter().map(<_>::to_string).collect(); + assert_eq!(joined_str, expected_str, "{endianness:?}"); + } + } + } }