我知道 GCC 允许你使用 __attribute__((section(".mysection")))
对于一个函数定义来指定将代码放入哪个部分。我有一堆代码需要放在特定部分中,并且必须用它来装饰每个函数是相当乏味的。有没有办法编译文件,同时在命令行上全局指定代码放入哪个部分?
最佳答案
如果合适,您可以更改链接描述文件以将特定目标文件放入特定部分。
以下示例是从 this resource 推断出来的
The following example is a complete linker script. It tells the linker to read all of the sections from file
all.o
and place them at the start of output sectionoutputa
which starts at location0x10000
. All of section.input1
from filefoo.o
follows immediately, in the same output section. All of section.input2
from foo.o goes into output sectionoutputb
, followed by section.input1
fromfoo1.o
. All of the remaining .input1 and.input2
sections from any files are written to output sectionoutputc
.
SECTIONS
{
outputa 0x10000 :
{
all.o
foo.o (.input1)
}
outputb :
{
foo.o (.input2)
foo1.o (.input1)
}
outputc :
{
*(.input1)
*(.input2)
}
}
因此,您可以做的是设计您的项目,将特定部分的所有功能隔离到将放入所需部分的目标文件中。
关于gcc - 在 GCC 中为代码指定节名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38120764/