获得从子程序调用者的LR为C变量 - ARM [英] get the caller's lr from subroutine into C variable - arm

查看:745
本文介绍了获得从子程序调用者的LR为C变量 - ARM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个是应该获得致电者的LR寄存器的值到一个局部变量的C函数。

I've got a C function that's supposed to get the value of the caller's lr register into a local variable.

我试过以下code:

volatile long lr;
asm(
    "str %0, [sp, #4]\n" :
    "=r", (lr)
);

但是,这并不能改变什么。这并不是说我得到了错误的值,它只是在局部LR变量的值不会有任何变化(包括垃圾)。

However, this doesn't change anything. It's not that I get the wrong value, it's just that the value of the lr local variable doesn't change at all ( contains garbage ).

任何想法?

谢谢!

推荐答案

要直接回答这个问题,有两种解决办法。

To answer the question directly, there are two solutions.

    long lr;
    asm(" mov %0,lr\n" :: "=r" (lr));

这会,但在 LR变量的的 LR 值。你不妨这样做是为了分析的东西。如果你假设 LR 包含了返回地址,那么这是不真实的的伊戈尔解释说。在一个的非叶的函数,编译器可以使用 LR 作为临时当计算函数的参数,因为它是会被打一顿。

This will but the lr value in the lr variable. You may wish to do this to analyze 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");

此形式允许引用变量的 LR 的任何地方,编译器将使用活 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.

由于伊戈尔解释是否得到 LR 可不给你你所期望的是什么。而我们使用的机制的是的gcc 的具体。你不能在移动的方式做到这一点;但访问的 LR 在本质上是不可移植的。

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.

请参阅: ARM链接寄存器和帧指针问题更多一点关于使用 LR

See: ARM link register and frame pointer question for a little more on the use of lr.

这篇关于获得从子程序调用者的LR为C变量 - ARM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆