Skip to content

Commit

Permalink
add support for cbz/cbnz instruction
Browse files Browse the repository at this point in the history
  • Loading branch information
ele7enxxh authored Oct 17, 2016
1 parent 535b914 commit eb06b3e
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion relocate.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ relocate instruction
author: ele7enxxh
mail: [email protected]
website: ele7enxxh.com
modified time: 2016-05-25
modified time: 2016-10-17
created time: 2015-01-17
*/

Expand All @@ -27,6 +27,10 @@ enum INSTRUCTION_TYPE {
// LDR Rt, <label>
LDR_THUMB16,

// CB{N}Z <Rn>, <label>
CB_THUMB16,


// BLX <label>
BLX_THUMB32,
// BL <label>
Expand Down Expand Up @@ -91,6 +95,9 @@ static int getTypeInThumb16(uint16_t instruction)
if ((instruction & 0xF800) == 0x4800) {
return LDR_THUMB16;
}
if ((instruction & 0xF500) == 0xB100) {
return CB_THUMB16;
}
return UNDEFINE;
}

Expand Down Expand Up @@ -242,6 +249,23 @@ static int relocateInstructionInThumb16(uint32_t pc, uint16_t instruction, uint1
trampoline_instructions[3] = value >> 16;
offset = 4;
}
else if (type == CB_THUMB16) {
int nonzero;
uint32_t imm32;
uint32_t value;

nonzero = (instruction & 0x800) >> 11;
imm32 = ((instruction & 0x200) >> 3) | ((instruction & 0xF8) >> 2);
value = pc + imm32 + 1;

trampoline_instructions[0] = instruction & 0xFD07;
trampoline_instructions[1] = 0xE003; // B PC, #6
trampoline_instructions[2] = 0xF8DF;
trampoline_instructions[3] = 0xF000; // LDR.W PC, [PC]
trampoline_instructions[4] = value & 0xFFFF;
trampoline_instructions[5] = value >> 16;
offset = 6;
}
else {
trampoline_instructions[0] = instruction;
trampoline_instructions[1] = 0xBF00; // NOP
Expand Down

0 comments on commit eb06b3e

Please sign in to comment.