如何通过链接描述文件在特定地址处放置符号? [英] How to place a symbol at certain address trough the linker script?
问题描述
这是关于GNU链接器的。
It is about GNU Linker.
我有一个名为 myVar
的变量(可以说)。现在,我希望将变量放置在内存中的特定地址。
I have a (lets say) variable named myVar
. Now I want my variable to be placed at certain address in the memory.
我使用的编译器具有 __ attribute__((blablabla))
可以解决问题。但是,我决定使用链接描述文件。
The compiler I use, has an __attribute__ ((blablabla))
that does the trick. However I decide to use the linker script.
由于现在我设法将变量放在链接程序脚本的特定地址处,如下所示:
Since now I managed to place the variable at certain address from the linker script just like this:
myVar = 0xDEAD;
它确实有效。问题是链接器很愚蠢,有时会在我的地址上部署其他变量。两者的行为都像在联盟
中一样。
And it actually works. The problem is that the linker is stupid and occasionally deploys other variables over the address of mine. And both in a way act like they are in a union
.
我认为我需要以某种方式告诉链接器该地址的长度已被占用,不应被触摸。
I think that somehow I need to tell the linker that this address with this length is occupied and should not be touched.
无论如何,我想您已经明白了。有帮助吗?
Anyway I guess You get the idea. Any help?
推荐答案
我将开始合并c源代码和链接器。
I'd start to merge c source and linker.
在链接脚本中定义一个新部分,例如:
Define a new section into linker script, like:
MEMORY
{
...
my_data (rwx) : ORIGIN = your_start_addr, LENGTH = section_length
}
SECTIONS
{
/* ... */
.myVarSection section_address :
{
KEEP(*(.mySection))
} > my_data
/* ... */
}
然后,您可以在源文件中定义变量,例如:
Then you can define your variable in source file like:
uint16_t __attribute__((section (".mySection"))) myVar = 0xDEAD;
这篇关于如何通过链接描述文件在特定地址处放置符号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!