title: When is a variable placed in `.rdata` section and not in `.text` section?
body: <p>I am trying to understand the implications of <code>.rdata</code> section vs <code>.text</code> section. I am trying a simple program as below</p>
  
  <pre><code>int main()
  {
      const int a = 10;
      printf("%d\n", a);
      return 0;
  }
  </code></pre>
  
  <p>When I build and dump the <code>map</code> file through <code>gcc -o a.out sample.c -Wl,Map,test.map</code> and search for <code>sample.o</code>, I find the following allocations</p>
  
  <pre><code>.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
  </code></pre>
  
  <p>Now, if I modify my program slightly to make <code>a</code> a global variable as </p>
  
  <pre><code>const int a = 10;
  int main()
  {
       printf("%d\n", a);
       return 0;
  }
  </code></pre>
  
  <p>By repeating the same step as above, I observe that the allocations are as below</p>
  
  <pre><code>.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
  </code></pre>
  
  <p>Wherein it clearly shows <code>a</code> is allocated into <code>.rdata</code> section as</p>
  
  <pre><code>.rdata         0x00403064        0xc sample.o
                 0x00403064            a
  </code></pre>
  
  <p>From these experiments, I understand the <code>global</code> <code>const</code> are alocated into <code>.rdata</code> section, whereas the <code>.text</code> section size has come down. Hence, I presume that <code>a</code> was allocated into <code>.text</code> section in the first example. </p>
  
  <p>My questions are:</p>
  
  <ol>
  <li><p>Is scope of a <code>const</code> variable considered while determining it's place in <code>.rdata</code> or <code>.text</code>?</p></li>
  <li><p>From my experiment, I observe that the variable required 8 bytes when it was allocated into <code>.text</code> section as compared to 4 bytes in <code>.rdata</code> section. What is the reason for this difference?</p></li>
  <li><p>If there are too many local <code>const</code> variables, then the size of corresponding <code>.text</code> section will increase significantly. What is the recommended programming practice in this scenario?</p></li>
  </ol>
  
  <p>Many thanks in advance.</p>