Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions objdiff-core/src/arch/superh/disasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ fn match_ni_f(
parts.push(InstructionPart::basic("#"));
parts.push(InstructionPart::unsigned(op & 0xff));
parts.push(InstructionPart::separator());
parts.push(InstructionPart::basic(reg));
parts.push(InstructionPart::opaque(reg));
}
0xe000 => {
// MOV #imm,Rn
Expand Down Expand Up @@ -574,23 +574,23 @@ fn match_nmd_f(
match op & 0xf000 {
0x1000 => {
parts.push(InstructionPart::opcode("mov.l", Ops::MovLRmAtDispRn as u16));
parts.push(InstructionPart::basic(reg_m));
parts.push(InstructionPart::opaque(reg_m));
parts.push(InstructionPart::separator());
parts.push(InstructionPart::basic("@("));
parts.push(InstructionPart::unsigned((op & 0xf) * 4));
parts.push(InstructionPart::separator());
parts.push(InstructionPart::basic(reg_n));
parts.push(InstructionPart::opaque(reg_n));
parts.push(InstructionPart::basic(")"));
}
0x5000 => {
parts.push(InstructionPart::opcode("mov.l", Ops::MovLRDispRmRn as u16));
parts.push(InstructionPart::basic("@("));
parts.push(InstructionPart::unsigned((op & 0xf) * 4));
parts.push(InstructionPart::separator());
parts.push(InstructionPart::basic(reg_m));
parts.push(InstructionPart::opaque(reg_m));
parts.push(InstructionPart::basic(")"));
parts.push(InstructionPart::separator());
parts.push(InstructionPart::basic(reg_n));
parts.push(InstructionPart::opaque(reg_n));
}
_ => match_d_f(v_addr, op, mode, parts, resolved, branch_dest),
}
Expand Down Expand Up @@ -1194,8 +1194,9 @@ fn match_f0ff(
parts.push(InstructionPart::opaque(reg));
}
0x401b => {
// TAS.B
// TAS.B @Rn
parts.push(InstructionPart::opcode("tas.b", Ops::TasB as u16));
parts.push(InstructionPart::basic("@"));
parts.push(InstructionPart::opaque(reg));
}
0x4003 => {
Expand Down
159 changes: 138 additions & 21 deletions objdiff-core/src/arch/superh/mod.rs
Original file line number Diff line number Diff line change
@@ -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},
Expand All @@ -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<Self> { Ok(Self {}) }
pub fn new(file: &object::File) -> Result<Self> { Ok(Self { endianness: file.endianness() }) }
}

struct DataInfo {
Expand All @@ -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<InstructionPart> = vec![];
let resolved: ResolvedInstructionRef = Default::default();
let mut branch_dest: Option<u64> = None;
Expand Down Expand Up @@ -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<InstructionPart> = vec![];
let mut branch_dest: Option<u64> = None;

Expand All @@ -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;
Expand All @@ -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(),
Expand All @@ -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(),
Expand Down Expand Up @@ -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 "),
Expand Down Expand Up @@ -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"),
Expand Down Expand Up @@ -283,7 +286,7 @@ mod test {
(0x000a, "sts mach, r0"),
(0x001a, "sts macl, r0"),
(0x402a, "lds r0, pr"),
(0x401b, "tas.b r0"),
(0x401b, "tas.b @r0"),
(0x4003, "stc.l sr, @-r0"),
(0x4013, "stc.l gbr, @-r0"),
(0x4023, "stc.l vbr, @-r0"),
Expand Down Expand Up @@ -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"),
Expand Down Expand Up @@ -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)"),
Expand Down Expand Up @@ -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)"),
Expand Down Expand Up @@ -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)"),
Expand Down Expand Up @@ -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"),
Expand Down Expand Up @@ -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)"),
Expand Down Expand Up @@ -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"),
Expand Down Expand Up @@ -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")];

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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 */")];

Expand Down Expand Up @@ -801,4 +804,118 @@ 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: &section,
..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:?}");
}
}
}
/// Every register of an SH-2 instruction is an argument, so that a different one is a diff.
#[test]
fn test_sh2_registers_are_arguments() {
let arch = ArchSuperH { endianness: object::Endianness::Big };
let ops: [(u16, &str, &[&str]); 4] = [
(0x7401, "add #0x1, r4", &["0x1", "r4"]),
(0x1412, "mov.l r1, @(0x8, r4)", &["r1", "0x8", "r4"]),
(0x5412, "mov.l @(0x8, r1), r4", &["0x8", "r1", "r4"]),
(0x411b, "tas.b @r1", &["r1"]),
];

for (opcode, expected_str, expected_args) in ops {
let code = opcode.to_be_bytes();
let mut parts = Vec::new();

arch.display_instruction(
ResolvedInstructionRef {
ins_ref: InstructionRef { address: 0x1000, size: 2, opcode, branch_dest: None },
code: &code,
..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);
let args: Vec<String> = parts
.iter()
.filter_map(|part| match part {
InstructionPart::Arg(arg) => Some(arg.to_string()),
_ => None,
})
.collect();
assert_eq!(args, expected_args, "{expected_str}");
}
}
}
Loading