title: Linker script error: section overlap
body: <p>I'm writing C code for the CASIO 9860GII calculator using the <strong>linker script</strong> (see at the end). Unfortunately I get following error:</p>
  
  <pre><code>$ sh3eb-elf-gcc -o build/crypt.elf build/crt0.o build/crypt.o build/supercoollibrary.o -m3 -mb -ffreestanding -nostdlib -I include -O3 -std=c99 -lc -lgcc -L lib -lfx -T src/crypt.ld -nostdlib
  sh3eb-elf/bin/ld: section .data._impl_stderr LMA [000000000030c308,000000000030c317] overlaps section .rodata._printf_ptr.str1.4 LMA [000000000030c308,000000000030c31b]
  sh3eb-elf/bin/ld: section .rodata.CSWTCH.44 LMA [000000000030c31c,000000000030c39f] overlaps section .data._impl_stdout LMA [000000000030c318,000000000030c327]
  sh3eb-elf/bin/ld: section C LMA [000000000030c328,000000000030c557] overlaps section .rodata.CSWTCH.44 LMA [000000000030c31c,000000000030c39f]
  sh3eb-elf/bin/ld: section .rodata.perror.str1.4 LMA [000000000030c3a0,000000000030c3bf] overlaps section C LMA [000000000030c328,000000000030c557]
  sh3eb-elf/bin/ld: warning: section `.bss' type changed to PROGBITS
  </code></pre>
  
  <p>Now i figured out that I have two sections parallel in ROM:</p>
  
  <pre><code>+-------------------------+----------------------------+
  | 0x0030c308              | 0x0030c308                 |
  |       .data_impl_stderr | .rodata._printf_ptr.str1.4 |
  | 0x0030c317              |                            |
  +-------------------------+ 0x0030c31b                 |
  | 0x0030c318              +----------------------------+
  |       .data_impl_stdout | 0x0030c31c                 |
  | 0x0030c327              |                            |
  +-------------------------+                            |
  | 0x0030c328              |          .rodata.CSWTCH.44 |
  |                         |                            |
  |                         |                            |
  |                         | 0x0030c39f                 |
  |                         +----------------------------+
  |                         | 0x0030c3a0                 |
  |                       C |      .rodata.perror.str1.4 |
  |                         | 0x0030c3bf                 |
  |                         +----------------------------|
  |                         |
  |                         |
  |                         |
  | 0x0030c557              | 
  +-------------------------+
  </code></pre>
  
  <p>Seemingly, <code>AT(_romdata)</code> makes the linker load <code>.data</code> into the <code>.rodata</code> section (ROM), even though I would expect it to be in RAM because of the region instruction <code>&gt; ram</code> for <code>.data</code>.</p>
  
  <p>I tried this linker script with very small sample code and it worked. After I inspected the <code>.elf</code> file using <code>readelf</code>, I noticed that there is no <code>.data</code> section. If I use my "real" code, the mentioned error appears. When I compile this code on Windows (using a different compiler), it works just fine.</p>
  
  <p>Could anyone please give me a hint what is going on? I'm pretty clueless, to be honest.</p>
  
  <p>Linker script:</p>
  
  <pre><code>OUTPUT_ARCH(sh3)
  ENTRY(initialize)
  MEMORY
  {
          rom  : o = 0x00300200, l = 512k
          ram  : o = 0x08100000, l = 64k  /* 64k pretty safe guess */
  
  
          /*
          rom start  0x00300200  (+512k = +0x0007D000)
              end    0x0037D200
  
          ram start  0x08100000  ( +64k = +0x0000FA00)
              end    0x0810FA00
          */
  }
  SECTIONS
  {
          .text : {
                  *(.pretext)     /* init stuff */
                  *(.text)
          } &gt; rom
          .rodata : {
                  *(.rodata)
                  *(.rodata.str1.4)
                  _romdata = . ;  /* symbol for initialization data */
          } &gt; rom
          .bss : {
                  _bbss = . ;
                  _bssdatasize = . ;
                  LONG(0);        /* bssdatasize */
                  *(.bss) *(COMMON);
                  _ebss = . ;
          } &gt; ram
  
          .data BLOCK(4) : AT(_romdata) {
                  _bdata = . ;
                  *(.data);
                  _edata = . ;
          } &gt; ram
  }
  </code></pre>