[PATCH] or1k: Fix disassembly for litte endian binaries
Stafford Horne
shorne@gmail.com
Sun Jun 15 04:32:33 GMT 2025
There are some OpenRISC CPU's that have their binaries stored in litte
endian format. Using objdump to disassemble these is problematic as
shown below, some instructions fail to disassmble.
objdump -D -b binary -EB -m or1k test_be.bin
0: 18 60 07 27 l.movhi r3,0x727
4: a8 63 0e 00 l.ori r3,r3,0xe00
8: 9c 63 ff ff l.addi r3,r3,-1
c: bc 43 00 00 l.sfgtui r3,0
10: 13 ff ff fe l.bf 0x8
14: 44 00 48 00 l.jr r9
objdump -D -b binary -EL -m or1k test_le.bin
0: 27 07 60 18 *unknown*
4: 00 0e 63 a8 l.ori r3,r3,0xe00
8: ff ff 63 9c *unknown*
c: 00 00 43 bc l.sfgtui r3,0
10: fe ff ff 13 *unknown*
14: 00 48 00 44 l.jr r9
It was found that the hash function was using the still little-endian
buffer to extract the opcode used for the has lookup. This didn't work
as it was pulling the wrong hashcode causing instruction lookup to fail.
Fix the hash function by using the normalized/byte-swapped value instead
of the buffer.
Signed-off-by: Stafford Horne <shorne@gmail.com>
---
cpu/or1k.opc | 2 +-
opcodes/or1k-opc.h | 2 +-
2 files changed, 2 insertions(+), 2 deletions(-)
diff --git a/cpu/or1k.opc b/cpu/or1k.opc
index 5d20a1f33a7..748187edcdf 100644
--- a/cpu/or1k.opc
+++ b/cpu/or1k.opc
@@ -38,7 +38,7 @@
#undef CGEN_DIS_HASH_SIZE
#define CGEN_DIS_HASH_SIZE 256
#undef CGEN_DIS_HASH
-#define CGEN_DIS_HASH(buffer, value) (((unsigned char *) (buffer))[0] >> 2)
+#define CGEN_DIS_HASH(buffer, value) ((value >> 26) & 0xff)
/* Check applicability of instructions against machines. */
#define CGEN_VALIDATE_INSN_SUPPORTED
diff --git a/opcodes/or1k-opc.h b/opcodes/or1k-opc.h
index d3d084b1bec..6eec8006828 100644
--- a/opcodes/or1k-opc.h
+++ b/opcodes/or1k-opc.h
@@ -35,7 +35,7 @@ extern "C" {
#undef CGEN_DIS_HASH_SIZE
#define CGEN_DIS_HASH_SIZE 256
#undef CGEN_DIS_HASH
-#define CGEN_DIS_HASH(buffer, value) (((unsigned char *) (buffer))[0] >> 2)
+#define CGEN_DIS_HASH(buffer, value) ((value >> 26) & 0xff)
/* Check applicability of instructions against machines. */
#define CGEN_VALIDATE_INSN_SUPPORTED
--
2.49.0
More information about the Binutils
mailing list