title: GCC: why constant variables not placed in .rodata
body: <p>I've always believed that GCC would place a <code>static const</code> variable to <code>.rodata</code> segments (or to <code>.text</code> segments for optimizations) of an ELF or such file. But it seems not that case.</p>
  
  <p>I'm currently using <code>gcc (GCC) 4.7.0 20120505 (prerelease)</code> on a laptop with GNU/Linux. And it does place a static constant variable to <code>.bss</code> segment:</p>
  
  <pre><code>/*
   * this is a.c, and in its generated asm file a.s, the following line gives:
   *   .comm a,4,4 
   * which would place variable a in .bss but not .rodata(or .text)
   */
  static const int a;
  
  int main()
  {
      int *p = (int*)&amp;a;
      *p = 0;  /* since a is in .data, write access to that region */
               /* won't trigger an exception */
      return 0;
  }
  </code></pre>
  
  <p>So, is this a bug or a feature? I've decided to file this as a bug to bugzilla but it might be better to ask for help first.</p>
  
  <p>Are there any reasons that GCC can't place a const variable in <code>.rodata</code>?</p>
  
  <p><strong>UPDATED:</strong></p>
  
  <p>As tested, a constant variable with an explicit initialization(like <code>const int a = 0;</code>) would be placed into <code>.rodata</code> by GCC, while I left the variable uninitialized. Thus this question might be closed later -- I didn't present a correct question maybe.</p>
  
  <p>Also, in my previous words I wrote that the variable a is placed in '.data' section, which is incorrect. It's actually placed into <code>.bss</code> section since not initialized. Text above now is corrected.</p>