Skip to content

Commit

Permalink
Zero out relocations before disassembling
Browse files Browse the repository at this point in the history
Fixes an issue where an addi with relocation
can be disassembled as subi
  • Loading branch information
encounter committed Jan 6, 2024
1 parent dd23fef commit 3bcfaef
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/util/asm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ where
fn write_ins<W>(
w: &mut W,
symbols: &[ObjSymbol],
ins: Ins,
mut ins: Ins,
reloc: Option<&ObjReloc>,
file_offset: u64,
section_address: u64,
Expand All @@ -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) {
Expand Down

0 comments on commit 3bcfaef

Please sign in to comment.