title: GCC error "<variable> causes a section type conflict"
body: <p>Why is there a compiler error when attempting to place two (RAM) variables, which only differ by their initialisation values, into the same section?</p>
  
  <h3>Problem</h3>
  
  <p>The C source:</p>
  
  <pre><code>int __attribute__((section(".in_my_data"))) _foo = 1;
  int __attribute__((section(".in_my_data"))) _bar = 0;
  </code></pre>
  
  <p>The (relevant) GCC compiler output:</p>
  
  <pre><code>mcve/main.c:75:45: error: _bar causes a section type conflict
  </code></pre>
  
  <p>The linker script contains the following line in the <code>SECTIONS</code> definition, but the (fatal) error is from the compiler, not the linker.</p>
  
  <pre><code>.my_data : { *(.in_my_data) } &gt; data
  </code></pre>
  
  <h3>Further Information</h3>
  
  <p>Changing the C source to permit the compiler to use two sections allows the compile to pass, but the linker then generates an error if the two input sections are mapped into the same output section.</p>
  
  <p>The C source:</p>
  
  <pre><code>int __attribute__((section(".in_my_data_nonzero"))) _foo = 1;
  int __attribute__((section(".in_my_data_zero"))) _bar = 0;
  </code></pre>
  
  <p>The linker script:</p>
  
  <pre><code>.my_data : { *(.in_my_data*) } &gt; data
  </code></pre>
  
  <p>The (relevant) linker output:</p>
  
  <pre><code>Link Error: attributes for input section '.in_my_data_nonzero' conflict
  with output section '.my_data'
  </code></pre>
  
  <p>Swapping the order of the lines in the C source only changes which section (the second to appear in the C source) is in error.</p>
  
  <h3>Question</h3>
  
  <p>What attribute(s) does the GCC compiler require of a section for a variable that is initialised with zero that it doesn't for one that is initialised with non-zero, or vice versa?</p>
  
  <p>Is the compiler trying to place the variables that are initialised to zero in the <code>.bss</code> section, rather than the <code>.data</code> section for initialised data?  Or is there another section for data that is initialised to zero?</p>
  
  <h3>Related questions</h3>
  
  <p>Similar questions appear cover issues with conflicts between memory types (ROM vs. RAM):</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/q/25246984/3581917">&quot;Section type conflict&quot; in arm embedded, what is it?</a></li>
  <li><a href="https://stackoverflow.com/q/10436759/3581917">How do I resolve a &quot;section type conflict&quot; compile error and best practices for using section attribute with gcc</a></li>
  <li><a href="https://stackoverflow.com/q/23923911/3581917">Getting a &quot;section type conflict&quot; using M2tklib and glcd</a></li>
  </ul>
  
  <p>... or putting initialised <code>const</code> data in NOLOAD output sections:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/q/30032468/3581917">&quot;Section type conflict&quot; due to macro definition in GCC 4.8.2</a></li>
  </ul>
  
  <p>... or remain a mystery as to the cause and may be related:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/q/30005764/3581917">Section type conflict for identically defined variables</a></li>
  </ul>
  
  <p>None of the above appear to have an answer that I can apply to issue in this question, as far as I can tell.</p>