简体   繁体   English

使用自定义节属性时附加符号名称

[英]Append symbol name when using custom section attribute

Below is a fragment of code I'm using for an embedded system. 以下是我用于嵌入式系统的代码片段。 I pass the -ffunction-sections and -fdata-sections options to gcc : 我将-ffunction-sections-fdata-sections选项传递给gcc

#define FAST_DATA __attribute__((section(".fast.data")))

          int a1 = 1;
          int a2 = 1;
FAST_DATA int a3 = 1;
FAST_DATA int a4 = 1;

The linker will allocate these symbols as below (map file): 链接器将如下分配这些符号(映射文件):

 .data.a1       0x20000020        0x4 ./main.o
                0x20000020                a1

 .data.a2       0x20000024        0x4 ./main.o
                0x20000024                a2

 .fast.data     0x10000010        0x8 ./main.o
                0x10000010                a4
                0x10000014                a3

If for example I don't use the variable a2 , the linker will discard it (I pass --gc-sections to ld ). 例如,如果我不使用变量a2 ,则链接器将丢弃它(我将--gc-sections传递给ld )。 But if I use a3 and don't use a4 , then a4 will not be discarded. 但是,如果我使用a3而不使用a4 ,那么a4将不会被丢弃。 I guess that's because it is placed in the same section as a3 . 我猜那是因为它和a3放在同一部分。

If I define a3 and a4 in separate .c files, they will be put in two different sections, with the same name .fast.data , but for each file. 如果我在单独的.c文件中定义a3a4 ,它们将被放在两个不同的部分中,它们具有相同的名称.fast.data ,但是对于每个文件。 The garbage collector will work as expected. 垃圾收集器将按预期工作。

Is there any way to tell gcc to append the symbol name even when using __attribute__((section("..."))) ? 即使使用__attribute__((section("...")))有什么办法告诉gcc附加符号名称吗? For a4 in my case that would result in .fast.data.a4 . 对于我来说, a4将导致.fast.data.a4 In the linker script I will catch all *(.fast.data*) . 在链接器脚本中,我将捕获所有*(.fast.data*)

I have a large code base using custom sections a lot and manual modifications to each declaration would not be desirable. 我有大量使用自定义部分的代码库,并且不希望对每个声明进行人工修改。

If no one else has a better idea, here is a kludge for you: 如果没有其他人有更好的主意,那么这里有个适合您的想法:

#define DECLARE_FAST_DATA(type, name) \
    __attribute__((section(".fast.data." #name))) type name

usage: 用法:

int a1 = 1;
int a2 = 1;
DECLARE_FAST_DATA(int, a3) = 1;
DECLARE_FAST_DATA(int, a4);

This uses the standard C features of " stringification " and " string literal concatenation " to synthesize the section attribute you want. 这使用“ 字符串化 ”和“ 字符串文字串联 ”的标准C功能来合成所需的section属性。

What about extending your macro? 扩展宏呢?

#define FAST_DATA(_a,_b,_c) \
  __attribute__((section(".fast.data." #_b))) _a _b = _c

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM