1

I have an ARM binary in which I want to patch a function to always return 0. My understanding is this means I need to set r0 register to 0.

The disassembly looks like this

STMFD           SP!, {R4-R6,LR}
<lots of code>
LDMFD           SP!, {R4-R6,PC}

Can I overwrite all of this with a mov r0, 0 -> 0000A0E3 followed by a return (mov pc, lr -> 0EF0A0E1)?

1 Answer 1

2

As you stated, returning in ARM/Thumb is to set the R0 register and return.

You can patch the program a number of ways to do what you want. You can replace the top most instructions with mov r0, 0 and mov pc, lr and leaving the remaining code as is would make it not execute those instructions after mov pc, lr and just return to the calling function.

MOV R0, 0
MOV PC, LR
// leave the rest as is, it wont execute

You can also patch starting after STMFD SP!, {R4-R6,LR} with MOV R0, 0 and then nopping all the way to the return statement LDMFD SP!, {R4-R6,PC}.

STMFD SP!, {R4-R6,LR}
MOV R0, 0
// NOP ALL THE WAY TO:
LDMFD SP!, {R4-R6,PC}

You can also just patch starting after STMFD SP!, {R4-R6,LR} with MOV R0, 0 and then replacing the following instruction with LDMFD SP!, {R4-R6,PC} to return early making the rest of the code below it un-executed.

STMFD SP!, {R4-R6,LR}
MOV R0, 0
LDMFD SP!, {R4-R6,PC}
// leave the rest as is, it wont execute
1
  • Yes that is what I expected but was unsure if the STMFD / LDMFD was required to balance the stack
    – Remko
    Dec 13, 2016 at 7:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.