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.