简体   繁体   中英

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 :

#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 ). But if I use a3 and don't use a4 , then a4 will not be discarded. I guess that's because it is placed in the same section as 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. The garbage collector will work as expected.

Is there any way to tell gcc to append the symbol name even when using __attribute__((section("..."))) ? For a4 in my case that would result in .fast.data.a4 . In the linker script I will catch all *(.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.

What about extending your macro?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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