<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 - Incorrect section offset and contents when specifying linker script memory region and data commands"
   href="https://bugs.llvm.org/show_bug.cgi?id=35565">35565</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Incorrect section offset and contents when specifying linker script memory region and data commands
          </td>
        </tr>

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

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

        <tr>
          <th>Hardware</th>
          <td>All
          </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>llvm@owenpshaw.net
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Created <span class=""><a href="attachment.cgi?id=19527" name="attach_19527" title="Fix by advancing memory region offset when outputting data command">attachment 19527</a> <a href="attachment.cgi?id=19527&action=edit" title="Fix by advancing memory region offset when outputting data command">[details]</a></span>
Fix by advancing memory region offset when outputting data command

If a linker script both a) specifies MEMORY regions and b) uses data commands
like BYTE or LONG, the resulting elf has incorrect section offsets and some
section contents overwrite preceding section contents.

1. Add a MEMORY region to the linker script used in
lld/test/ELF/linkerscript/data-commands.s and map the output sections to that
region, resulting in:

MEMORY
{
    rom (rwx) : ORIGIN = 0x00, LENGTH = 2K
}
SECTIONS
{
  .foo : {
    *(.foo.1)
    BYTE(0x11)
    *(.foo.2)
    SHORT(0x1122)
    *(.foo.3)
    LONG(0x11223344)
    *(.foo.4)
    QUAD(0x1122334455667788)
  } > rom
  .bar : {
    *(.bar.1)
    BYTE(a + 1)
    *(.bar.2)
    SHORT(b)
    *(.bar.3)
    LONG(c + 2)
    *(.bar.4)
    QUAD(d)
  } > rom
}

2. Build data-commands.s and link using this new script. The resulting elf
sections are:

Contents of section .foo:
 0000 ff11ff22 ff12ff22 4c696e6b 65723a20  ..."..."Linker: 
 0010 4c4c44                               LLD
Contents of section .bar:
 0004 ff12ff22 4c696e6b 65723a20 4c4c4420  ..."Linker: LLD 
 0014 362e30                               6.0
Contents of section .comment:
 0000 4c696e6b 65723a20 4c4c4420 362e302e  Linker: LLD 6.0.
 0010 30202868 7474703a 2f2f6c6c 766d2e6f  0 (<a href="http://llvm.o">http://llvm.o</a>
...


Note how the .bar section starts at 0x0004 instead of after .foo at 0x0013. 
Also the contents of .bar clearly contain data from the .comment section, and
.foo contains
data from .bar.


Expected:

Without the memory regions in the linker script, the elf sections are properly
offset, and do not overwrite each other.  This is also the result that gcc ld
gives regardless of the memory regions' presence:

Contents of section .foo:
 0000 ff11ff22 11ff4433 2211ff88 77665544  ..."..D3"...wfUD
 0010 332211                               3".
Contents of section .bar:
 0013 ff12ff22 11ff4633 2211ff88 77665500  ..."..F3"...wfU.
 0023 000000                               ...
....


I'm using a local build of lld trunk pulled on Dec 6:
<a href="http://llvm.org/git/lld.git@b7835c6bfba20cc71a676060562d9d930e064e5b">http://llvm.org/git/lld.git@b7835c6bfba20cc71a676060562d9d930e064e5b</a>
git-svn-id: <a href="https://llvm.org/svn/llvm-project/lld/trunk@319863">https://llvm.org/svn/llvm-project/lld/trunk@319863</a>
91177308-0d34-0410-b5e6-96231b3b80d8


A proposed fix is attached.</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>