链接描述文件-在内存区域的末尾放置一个节 [英] Linker Script - Placing a section at the end of a memory region

查看:161
本文介绍了链接描述文件-在内存区域的末尾放置一个节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经广泛搜索了如何执行此操作,但是没有找到答案.

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.

现在,我要做的是将一个部分(称为.persist)放入我的ram内存中.我希望它驻留在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的起始地址和大小,所以如果我知道节的大小,那么计算该节需要去的地方将是微不足道的.但是,根据 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.

这篇关于链接描述文件-在内存区域的末尾放置一个节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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