From 3bcfaef4fe4706f1938237d08e2c453903be337a Mon Sep 17 00:00:00 2001 From: Luke Street Date: Sat, 6 Jan 2024 10:27:07 -0700 Subject: [PATCH] Zero out relocations before disassembling Fixes an issue where an addi with relocation can be disassembled as subi --- src/util/asm.rs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/util/asm.rs b/src/util/asm.rs index 3a30f78..ca77ee8 100644 --- a/src/util/asm.rs +++ b/src/util/asm.rs @@ -254,7 +254,7 @@ where fn write_ins( w: &mut W, symbols: &[ObjSymbol], - ins: Ins, + mut ins: Ins, reloc: Option<&ObjReloc>, file_offset: u64, section_address: u64, @@ -273,6 +273,19 @@ where ins.code & 0xFF )?; + if let Some(reloc) = reloc { + // Zero out relocations + ins.code = match reloc.kind { + ObjRelocKind::Absolute => 0, + ObjRelocKind::PpcEmbSda21 => ins.code & !0x1FFFFF, + ObjRelocKind::PpcRel24 => ins.code & !0x3FFFFFC, + ObjRelocKind::PpcRel14 => ins.code & !0xFFFC, + ObjRelocKind::PpcAddr16Hi + | ObjRelocKind::PpcAddr16Ha + | ObjRelocKind::PpcAddr16Lo => ins.code & !0xFFFF, + }; + } + if ins.op == Opcode::Illegal { write!(w, ".4byte {:#010X} /* invalid */", ins.code)?; } else if is_illegal_instruction(ins.code) {