链接器脚本 - 在内存区域的末尾放置一个节
我已经广泛搜索了如何做到这一点,但未能找到答案.
I have searched far and wide for how to do this and have failed to come up with an answer.
我的内存布局如下:
Fake Address | Section
0 | text
7 | relocate
15 | bss
23 | stack
在堆栈的末尾,我放置了堆.它长大了,堆栈是我正在使用的 ARM 芯片的完整降序堆栈.
At the end of the Stack I place the Heap. Which grows up and the stack is a full descending stack for the ARM chip I am using.
现在,我要做的是在我的 RAM 内存中放置一个单独的部分,我们称之为 .persist
.我希望它位于 RAM 的最末端,并且我想将它编程到我的链接器脚本中.但是,这个 .persist
部分的大小不是由我定义的,而是由编译器根据它包含的符号计算得出的.
Now, what I want to do is place a single section, let's call it .persist
, into my ram memory. I want it to reside at the very end of RAM and I want to program this into my linker script. However, this .persist
section's size is not defined by me but is computed by the compiler from the symbols that it contains.
到目前为止,我还没有想出一个好的方法来做到这一点.因为我知道 RAM 起始地址和 SIZE,所以如果我知道部分大小,计算部分需要去的位置将是微不足道的.但是,根据 GNU 链接器文档(第 74 页) 看来:
So far I've not come up with a good way to do it. Since I know the RAM start address and SIZE it would be trivial to calculate where the section needs to go if I knew the section size. However, according to the GNU linker documentation (pg 74) it seems that:
SIZEOF(section) 返回命名的字节大小部分,如果该部分已被分配.如果在评估时该节尚未分配,则链接器将报错.
SIZEOF(section) Returns the size in bytes of the named section, if that section has been allocated. If the section has not been allocated when this is evaluated, the linker will report an error.
所以我无法计算链接描述文件中部分的大小(因为我想在放置/分配它之前计算大小).
so I can't work out the size of the section in the linker script (since I want to calculate the size BEFORE I place it/allocate it).
有谁知道这样做的好方法吗?
Does anyone know a good way to do this?
推荐答案
我能够通过两步过程来完成类似的事情.首先,我将相关部分编译为它自己的目标文件.就我而言,我有一个从程序集文件生成的元数据部分.gcc -c
将源代码编译成目标文件,但不链接它们.
I was able to accomplish something similar by making linking a two-step process.
First I compile the section in question to its own object file. In my case I had a metadata section generated from an assembly file. gcc -c
will compile the source into object files, but not link them.
gcc -c metadata.s -o metadata.o
您也可以构建整个程序,然后使用 objcopy
仅提取有问题的部分.
You could also build your whole program, then extract just the section in question with objcopy
.
gcc -c main.cc -o main.o
objcopy --only-section=.metadata main.o metadata.o
现在我构建并链接程序的其余部分,并将目标文件包含在链接器的输入中.
Now I build and link the rest of the program, and include the object file among the linker's input.
gcc metadata.o ../main.o -o Program.elf -T linkerscript.ld
链接器从目标文件中读取.metadata
部分,我可以在链接器脚本中引用它的大小.
The linker reads the section .metadata
from the object file, and I can reference its size in the linker script.
相关文章