[llvm-bugs] [Bug 35565] New: Incorrect section offset and contents when specifying linker script memory region and data commands

via llvm-bugs llvm-bugs at lists.llvm.org
Thu Dec 7 12:44:53 PST 2017


https://bugs.llvm.org/show_bug.cgi?id=35565

            Bug ID: 35565
           Summary: Incorrect section offset and contents when specifying
                    linker script memory region and data commands
           Product: lld
           Version: unspecified
          Hardware: All
                OS: All
            Status: NEW
          Severity: normal
          Priority: P
         Component: ELF
          Assignee: unassignedbugs at nondot.org
          Reporter: llvm at owenpshaw.net
                CC: llvm-bugs at lists.llvm.org

Created attachment 19527
  --> https://bugs.llvm.org/attachment.cgi?id=19527&action=edit
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 (http://llvm.o
...


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:
http://llvm.org/git/lld.git@b7835c6bfba20cc71a676060562d9d930e064e5b
git-svn-id: https://llvm.org/svn/llvm-project/lld/trunk@319863
91177308-0d34-0410-b5e6-96231b3b80d8


A proposed fix is attached.

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20171207/2f427a5d/attachment.html>


More information about the llvm-bugs mailing list