gcc - 在 GCC 中为代码指定节名称

标签 gcc

我知道 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 section outputa which starts at location 0x10000. All of section .input1 from file foo.o follows immediately, in the same output section. All of section .input2 from foo.o goes into output section outputb, followed by section .input1 from foo1.o. All of the remaining .input1 and .input2 sections from any files are written to output section outputc.


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/

相关文章:

c++ - 海湾合作委员会错误 "/usr/bin/ld: cannot find -lstdc++"

c++ - Cygwin:使用asm标签编译cpp文件

c - PellesC 是否严格符合标准 C99?

c - Debian 中 gcc 的奇怪行为

c - 如何以编程方式在正在运行的应用程序中查找符号

c - 从汇编器到 C 编译器

c++ - g++ 链接到外部库创建 undefined reference

linux -/usr/bin/ld : cannot open output file assimp2json: Is a directory

gcc - 为什么 mingw 找不到 gcc 找不到的库?

c - 局部同义变量到非精确类型