<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - LLD . assignment outside of SECTIONS can overflow MEMORY Region spuriously"
   href="https://bugs.llvm.org/show_bug.cgi?id=41357">41357</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>LLD . assignment outside of SECTIONS can overflow MEMORY Region spuriously
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>lld
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>unspecified
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>All
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>ELF
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>peter.smith@linaro.org
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org, peter.smith@linaro.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Consider the following example:
cat memregions.lds
MEMORY {
    FLASH (rx) : ORIGIN = (0x0 + 0x0), LENGTH = (256*1K - 0x0)
    SRAM (wx) : ORIGIN = 0x20000000, LENGTH = (64 * 1K)
}


SECTIONS {
 .text : { *(.text) } > FLASH
 . = 0x20000000;
 .data : { *(.data) } > SRAM AT> FLASH
}

cat memregion.s
.text 
 nop

.data
.word 0

ld.lld memregion.o -T memregion.lds
ld.lld: error: section '.text' will not fit in region 'FLASH': overflowed by
536608768 bytes
ld.lld: error: section '.data' will not fit in region 'FLASH': overflowed by
536608768 bytes
ld.lld: error: section '.data' will not fit in region 'FLASH': overflowed by
536608770 bytes

I tracked this down to LinkerScript::setDot(), in particular:
  // Update to location counter means update to section size.
  if (InSec)
    expandOutputSection(Val - Dot);
  else
    expandMemoryRegions(Val - Dot);

The . = 0x20000000; in the linker script which is supposed to set dot for the
next memory region is adding 512 Megabytes to the size of the .text
OutputSection. This does not happen with ld.bfd.

 I believe this part of the code was added for
<a href="https://bugs.llvm.org//show_bug.cgi?id=37836">https://bugs.llvm.org//show_bug.cgi?id=37836</a> which had a linker script with
something like:

    MEMORY
    {
      ram (wxa) : ORIGIN = 0x42000, LENGTH = 0x100000
    }

    SECTIONS
    {
      .text :
      {
        *(.text*)
      }

      aligned_dot = ALIGN(0x10 * 1024);  

      .data aligned_dot :
      {
        *(.data*)
      }
    }

I would certainly not expect assigning to . to add anything to a memory region,
what I would expect is that the effect of assigning to . would affect the start
address of the next OutputSection and that would trigger a corresponding change
in the Memory Region. So I think we may need to add/alter the fix for pr37836.

The linker script was cut down from the Zephyr RTOS Linker Script so this is a
use case from a real program.</pre>
        </div>
      </p>


      <hr>
      <span>You are receiving this mail because:</span>

      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>