Skip to content

Commit

Permalink
Support big endian
Browse files Browse the repository at this point in the history
  • Loading branch information
imbillow committed Jun 16, 2024
1 parent 444c6b2 commit 34b4c81
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
22 changes: 13 additions & 9 deletions arch/Xtensa/XtensaDisassembler.c
Original file line number Diff line number Diff line change
Expand Up @@ -233,18 +233,23 @@ static DecodeStatus decodeMem32Operand(MCInst *Inst, uint64_t Imm,
}

/// Read three bytes from the ArrayRef and return 24 bit data
static DecodeStatus readInstruction24(const uint8_t *Bytes,
const unsigned BytesSize, uint64_t *Size,
uint32_t *Insn)
static DecodeStatus readInstruction24(MCInst *MI, uint64_t *SizeOut,
const uint8_t *Bytes,
const unsigned BytesSize, uint32_t *Insn)
{
// We want to read exactly 3 Bytes of data.
if (BytesSize < 3) {
*Size = 0;
*SizeOut = 0;
return MCDisassembler_Fail;
}

*Insn = (Bytes[2] << 16) | (Bytes[1] << 8) | (Bytes[0] << 0);
*Size = 3;
if (MODE_IS_BIG_ENDIAN(MI->csh->mode))
*Insn = (Bytes[2]) | (Bytes[1] << 8) |
((uint32_t)Bytes[0] << 16);
else
*Insn = (Bytes[2] << 16) | (Bytes[1] << 8) |
((uint32_t)Bytes[0]);
*SizeOut = 3;
return MCDisassembler_Success;
}

Expand All @@ -255,17 +260,16 @@ DecodeToMCInst(decode_to_MCInst, field_from_inst, uint32_t);
DecodeInstruction(decodeInstruction, field_from_inst, decode_to_MCInst,
uint32_t);

static DecodeStatus getInstruction(MCInst *MI, uint64_t *Size,
static DecodeStatus getInstruction(MCInst *MI, uint64_t *SizeOut,
const uint8_t *Bytes, unsigned BytesSize,
uint64_t Address)
{
uint32_t Insn;
DecodeStatus Result;

Result = readInstruction24(Bytes, BytesSize, Size, &Insn);
Result = readInstruction24(MI, SizeOut, Bytes, BytesSize, &Insn);
if (Result == MCDisassembler_Fail)
return MCDisassembler_Fail;
// LLVM_DEBUG(dbgs() << "Trying Xtensa 24-bit instruction table :\n");
Result = decodeInstruction(DecoderTable24, MI, Insn, Address, NULL);
return Result;
}
Expand Down
4 changes: 1 addition & 3 deletions cs.c
Original file line number Diff line number Diff line change
Expand Up @@ -212,9 +212,7 @@ typedef struct cs_arch_config {
}
#define CS_ARCH_CONFIG_XTENSA \
{ \
Xtensa_global_init,\
Xtensa_option,\
~(CS_MODE_LITTLE_ENDIAN | CS_MODE_XTENSA),\
Xtensa_global_init, Xtensa_option, ~(CS_MODE_XTENSA), \
}

#ifdef CAPSTONE_USE_ARCH_REGISTRATION
Expand Down

0 comments on commit 34b4c81

Please sign in to comment.