I\'ve got a C function that\'s supposed to get the value of the caller\'s lr register into a local variable.
I\'ve tried the following code:
volatile
To answer the question directly, there are two solutions.
long lr;
asm(" mov %0,lr\n" :: "=r" (lr));
This will put the lr
value in the lr variable. You may wish to do this to analyse something. If you are assuming that the lr
contains a return address, then this is untrue as Igor explains. In a non-leaf function, the compiler may use the lr
as a temporary when computing a function argument as it is going to be clobbered.
register long lr asm("lr");
This form allows you to reference the variable lr anywhere and the compiler will use the live lr
value. The value may change through-out the function. For instance, when you call another function, it will usually be set to the return point in the currently active function.
As Igor explains whether getting the lr
may not give you exactly what you expect. And the mechanism's we are using are gcc specific. You can not do this in a portable way; but accessing an lr
is intrinsically non-portable.
See: ARM link register and frame pointer question for a little more on the use of lr
.
Your code will get the value at the address SP+4
. Whether that location contains the initial LR
depends on the compiler, optimization settings, specific function, or the place in the function where you put this code. In short, it would probably work only by accident.
EDIT: your code is not even reading anything relevant, it's just storing a value of uninitialized variable on the stack (hint: naming your variable lr
doesn't make it magically take the value of the register LR
). In the best case it won't do anything, in the worst you'll overwrite something important and it will crash.
I'll assume that you actually want to get the function's return address, not LR specifically. There exist compiler-specific options for that.
GCC offers the __builtin_return_address() intrinsic which returns the return address of the current function (if called with 0) or, possibly, other functions in the call stack (though I wouldn't rely on the latter).
Visual C++ has _ReturnAddress() and even _AddressOfReturnAddress() intrinsics.
I would recommend using one of the above (assuming you're using GCC or VC) and not rely on tricks which may stop working at any time.