Branch to an address using the built-in ARM as

I want branchto a specific address (NOT a label) using ARM assemblywithout changing case LR. So I go with Binstead of BLor BX. I want this done in GCC inline asm.

There is documentation here, and here is what I tried:

#define JMP(addr) \
    __asm__("b %0" \
            : /*output*/ \
            : /*input*/ \
            "r" (addr) \
           );

This is a C macro that can be invoked with address. When I run it, I get the following error:

error: undefined reference to 'r3'

Error related to use "r". I looked into it a bit and I found that it could be a bug in gcc version 4.9. *.

By the way, I use Android/Linux Gcc 4.9 cross compiler, on OSX. Also, I don’t know what I should have downloaded something on Rm.

Hooray!

Edit: , undefined reference to r3 and r4:

#define JMP(addr) \
    __asm__("LDR r5,=%0\n\t" \
            "LDR r4,[r5]\n\t"\
            "ADD r4,#1\n\t" \
            "B r4" \
            : /*output*/ \
            : /*input*/ \
            "r" (addr) \
            : /*clobbered*/ \
            "r4" ,"r5" \
           );

: r5, r4. 1 LSB (emm, ARM?). , , .

+4
2

, . , (r15).

#define JMP(addr) \
    __asm__("mov pc,%0" \
            : /*output*/ \
            : /*input*/ \
            "r" (addr) \
           );
+3

C, C - : , , , :

((void (*)(void)) addr)();

: addr ( (*)) , ( void , ), ( void). , . Google "C- " .

, - , , , BX ( , , "Branch and Exchange" , , ( ) , , ).

:

  • B . , , , ( , ). - ( , LR ).
  • BX R0 ( ) , R0 . .
  • BL BLX R0 . , LR. , .

, :

asm("BX %0" : : "r"(addr));

, addr (r), . , (clobbered) .

. https://gcc.gnu.org/onlinedocs/gcc/Constraints.html .

, , , ARM:

  • PC R15. .
  • , . , .
  • , ARM, :
    • BX LR, , : (LR R14) ,
    • POP {R4-R11, PC} . PUSH {R4-R11, LR} : ( ) , ,
    • B , , .

, ,

+2

Source: https://habr.com/ru/post/1588908/


All Articles