GCC提供了一个扩展机制,使得程序员可以指定变量所处的段。如下例
__attribute__((section("FOO"))) int global = 43;
int main()
{
__attribute__((section("BAR"))) static int x;
}
对上述代码进行编译得到目标文件后查看段信息,可以看出变量 global 与 x 均是在对应指定的自定义段中的
其中:对 test.c 编译得到目标文件的指令为: gcc -c test.c -o test.o
查看目标文件中段信息的指令为:objdump -s test.o