I am trying to understand the implications of .rdata section vs .text section. I am trying a simple program as below

int main()
{
    const int a = 10;
    printf("%d\n", a);
    return 0;
}

When I build and dump the map file through gcc -o a.out sample.c -Wl,Map,test.map and search for sample.o, I find the following allocations

.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 modify my program slightly to make a a global variable as

const int a = 10;
int main()
{
     printf("%d\n", a);
     return 0;
}

By repeating the same step as above, I observe that the allocations are as below

.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

Wherein it clearly shows a is allocated into .rdata section as

.rdata         0x00403064        0xc sample.o
               0x00403064            a

From these experiments, I understand the global const are alocated into .rdata section, whereas the .text section size has come down. Hence, I presume that a was allocated into .text section in the first example.

My questions are:

  1. Is scope of a const variable considered while determining it's place in .rdata or .text?

  2. From my experiment, I observe that the variable required 8 bytes when it was alloca

Edit
Report