将调用者的 lr 从子程序中获取到 C 变量 - arm [英] get the caller's lr from subroutine into C variable - arm

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

问题描述

我有一个 C 函数,它应该将调用者的 lr 寄存器的值放入一个局部变量中.

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

我试过以下代码:

volatile long lr;
asm(
    "str %0, [sp, #4]
" :
    "=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
" :: "=r" (lr));

这会将 lr 值放入 lr 变量.您可能希望这样做以分析某些内容.如果您假设 lr 包含返回地址,那么这是不正确的,因为 Igor 解释.在 非叶 函数中,编译器可能会在计算函数参数时使用 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");

此表单允许您在任何地方引用变量 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.

Igor 解释了获取 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天全站免登陆