如何在C中的链接描述文件中访问变量定义? [英] How to access variable define in linker script in c?

查看:70
本文介绍了如何在C中的链接描述文件中访问变量定义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在链接描述文件中,我定义了 PROVIDE(__ KERNEL_BEGIN__ =.); .

In the linker script, I defined PROVIDE(__KERNEL_BEGIN__ = .);.

可以从以下地址访问地址:

The address can be accessed from:

extern uint32_t __KERNEL_BEGIN__[];

但是,以下操作不起作用(给出了错误的地址):

But, the following does not work (gives an incorrect address):

extern uint32_t * __KERNEL_BEGIN__;

我看了看大会.第一种方法 __ KERNEL_BEGIN __ 提供了确切的地址.第二个 __ KERNEL_BEGIN__ = [address] ,在该地址处查找一个值.

I looked at the assembly. The first method, __KERNEL_BEGIN__, provides the exact address. The second one, __KERNEL_BEGIN__ = [address], looks up a value at the address.

那么,一定要这样吗?我无法分辨出差异.

So, must it be this way? I cannot tell the difference.

推荐答案

您需要使用 extern 变量的地址.它并不完全直观,但在手册中进行了解释.

You need to take the address of the extern variable. It is not completely intuitive but is explained in the manual.

理论上, extern 可以是任何原始数据类型.由于我不知道的原因,约定是使用 char :

In theory, the extern can be any primitive data type. For reasons which I am unaware, the convention is to use a char:

extern char __KERNEL_BEGIN__;

然后要获取从链接描述文件中导出的地址,请使用 __ KERNEL_BEGIN __ 的地址:

Then to get the address exported from the linker script, take the address of __KERNEL_BEGIN__:

printf("Address: %08x\n", &__KERNEL_BEGIN__);

您可以在 ld .

这篇关于如何在C中的链接描述文件中访问变量定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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