Skip to content

Commit

Permalink
Fix rip-relative adjustments with non-64bit effective widths
Browse files Browse the repository at this point in the history
( #1 )
  • Loading branch information
Nukem9 committed Jan 25, 2016
1 parent 509e335 commit 4c18f07
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
23 changes: 12 additions & 11 deletions src/Translator.cpp
Expand Up @@ -91,14 +91,14 @@ void InstructionToXed(Inst* Instruction, xed_state_t Mode, xed_encoder_instructi
xed_addr(XedInst, Instruction->AddressSizeOverride);
}

bool TryEncode(XEDPARSE* Parse, xed_state_t State, Inst* Instruction, unsigned int EffectiveWidth)
bool TryEncode(XEDPARSE* Parse, xed_state_t State, Inst* Instruction, unsigned int Bits)
{
// Convert this struct to XED's format
xed_encoder_instruction_t xedInst;
memset(&xedInst, 0, sizeof(xed_encoder_instruction_t));

// XEDParse instruction -> Xed instruction
InstructionToXed(Instruction, State, &xedInst, EffectiveWidth);
InstructionToXed(Instruction, State, &xedInst, Bits);

// Conversion request -> encoder request
xed_encoder_request_t encReq;
Expand All @@ -122,15 +122,16 @@ bool TryEncode(XEDPARSE* Parse, xed_state_t State, Inst* Instruction, unsigned i
return true;
}

bool TryEncode64(XEDPARSE* Parse, xed_state_t State, Inst* Instruction)
bool TryRecode(XEDPARSE* Parse, xed_state_t State, Inst* Instruction, unsigned int Bits)
{
if(!Parse->x64)
return false;

// First round, try regular encoding
if(!TryEncode(Parse, State, Instruction, 64))
if(!TryEncode(Parse, State, Instruction, Bits))
return false;

// 32-bit code returns immediately
if(!Parse->x64)
return true;

// Check if any operands are RIP-relative memory references
bool reEncode = false;

Expand All @@ -155,7 +156,7 @@ bool TryEncode64(XEDPARSE* Parse, xed_state_t State, Inst* Instruction)

// Operands were modified
if(reEncode)
return TryEncode(Parse, State, Instruction, 64);
return TryEncode(Parse, State, Instruction, Bits);

// Nothing was changed
return true;
Expand All @@ -173,15 +174,15 @@ bool Translate(XEDPARSE* Parse, xed_state_t State, Inst* Instruction)

// Try encoding with various different effective width values
// 32-bit
if(TryEncode(Parse, State, Instruction, 32))
if(TryRecode(Parse, State, Instruction, 32))
return true;

// 64-bit
if(TryEncode64(Parse, State, Instruction))
if(TryRecode(Parse, State, Instruction, 64))
return true;

// 16-bit
if(TryEncode(Parse, State, Instruction, 16))
if(TryRecode(Parse, State, Instruction, 16))
return true;

return false;
Expand Down
4 changes: 2 additions & 2 deletions src/Translator.h
Expand Up @@ -7,7 +7,7 @@
LONGLONG TranslateRelativeCip(XEDPARSE* Parse, ULONGLONG Value, bool Signed);
xed_encoder_operand_t OperandToXed(InstOperand* Operand);
void InstructionToXed(Inst* Instruction, xed_state_t Mode, xed_encoder_instruction_t* XedInst, unsigned int EffectiveWidth);
bool TryEncode(XEDPARSE* Parse, xed_state_t State, Inst* Instruction, unsigned int EffectiveWidth);
bool TryEncode64(XEDPARSE* Parse, xed_state_t State, Inst* Instruction);
bool TryEncode(XEDPARSE* Parse, xed_state_t State, Inst* Instruction, unsigned int Bits);
bool TryRecode(XEDPARSE* Parse, xed_state_t State, Inst* Instruction, unsigned int Bits);
bool Translate(XEDPARSE* Parse, xed_state_t State, Inst* Instruction);
#endif

0 comments on commit 4c18f07

Please sign in to comment.