I am trying to understand the consequences of the .rdata vs .text section. I am trying a simple program as shown below
int main() { const int a = 10; printf("%d\n", a); return 0; }
When I create and delete the map file via gcc -o a.out sample.c -Wl,Map,test.map and search for sample.o , I find the following distributions
.text 0x0040138c 0x34 sample.o .data 0x00402000 0x0 sample.o .rdata 0x00403064 0x8 sample.o .eh_frame 0x00404060 0x38 sample.o .bss 0x00405020 0x0 sample.o
Now, if I change my program a bit to make a global variable like
const int a = 10; int main() { printf("%d\n", a); return 0; }
Repeating the same step as above, I noticed that the distributions are lower
.text 0x0040138c 0x2c sample.o .data 0x00402000 0x0 sample.o .rdata 0x00403064 0xc sample.o .eh_frame 0x00404060 0x38 sample.o .bss 0x00405020 0x0 sample.o
It clearly shows that a stands out in .rdata as
.rdata 0x00403064 0xc sample.o 0x00403064 a
From these experiments, I understand that the global const located in the .rdata section, while the size of the .text section has decreased. Therefore, I assume that a was highlighted in the .text section in the first example.
My questions:
The scope of the const variable is considered when determining its place in .rdata or .text ?
From my experiment, I noticed that the variable required 8 bytes when it was allocated to the .text section compared to 4 bytes in the .rdata section. What is the reason for this difference?
If there are too many local const variables, the size of the corresponding .text section will increase significantly. What is the recommended programming practice in this scenario?
Thank you very much in advance.
source share