<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Tue, Nov 18, 2014 at 7:51 PM, Shankar Easwaran <span dir="ltr"><<a href="mailto:shankare@codeaurora.org" target="_blank">shankare@codeaurora.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Author: shankare<br>
Date: Tue Nov 18 21:51:48 2014<br>
New Revision: 222312<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=222312&view=rev" target="_blank">http://llvm.org/viewvc/llvm-project?rev=222312&view=rev</a><br>
Log:<br>
[ELF] Fix creation of segments.<br>
<br>
Linker was creating a separate output segment in some cases if input sections<br>
had huge alignments. This patch fixes the issue.<br>
<br>
Added:<br>
    lld/trunk/test/elf/X86_64/outputsegments.test<br>
Modified:<br>
    lld/trunk/lib/ReaderWriter/ELF/SegmentChunks.h<br>
    lld/trunk/test/elf/X86_64/alignoffset.test<br>
    lld/trunk/test/elf/phdr.test<br>
<br>
Modified: lld/trunk/lib/ReaderWriter/ELF/SegmentChunks.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/ELF/SegmentChunks.h?rev=222312&r1=222311&r2=222312&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/ELF/SegmentChunks.h?rev=222312&r1=222311&r2=222312&view=diff</a><br>
==============================================================================<br>
--- lld/trunk/lib/ReaderWriter/ELF/SegmentChunks.h (original)<br>
+++ lld/trunk/lib/ReaderWriter/ELF/SegmentChunks.h Tue Nov 18 21:51:48 2014<br>
@@ -406,6 +406,8 @@ void Segment<ELFT>::assignFileOffsets(ui<br>
   for (auto &slice : slices()) {<br>
     bool isFirstSection = true;<br>
     for (auto section : slice->sections()) {<br>
+      // Align fileoffset to the alignment of the section.<br>
+      fileOffset = llvm::RoundUpToAlignment(fileOffset, section->align2());<br></blockquote><div><br></div><div>Is this correct? align2 seems to imply that's an exponent. If so, it had to be used as 1 << section->align2().</div><div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
       // If the linker outputmagic is set to OutputMagic::NMAGIC, align the Data<br>
       // to a page boundary<br>
       if (isFirstSection &&<br>
@@ -430,8 +432,7 @@ void Segment<ELFT>::assignFileOffsets(ui<br>
         fileOffset =<br>
             llvm::RoundUpToAlignment(fileOffset, this->_context.getPageSize());<br>
         isDataPageAlignedForNMagic = true;<br>
-      } else<br>
-        fileOffset = llvm::RoundUpToAlignment(fileOffset, section->align2());<br>
+      }<br>
       if (isFirstSection) {<br>
         slice->setFileOffset(fileOffset);<br>
         isFirstSection = false;<br>
@@ -467,6 +468,7 @@ template <class ELFT> void Segment<ELFT><br>
   SegmentSlice<ELFT> *slice = nullptr;<br>
   uint64_t tlsStartAddr = 0;<br>
   bool alignSegments = this->_context.alignSegments();<br>
+  StringRef prevOutputSectionName;<br>
<br>
   for (auto si = _sections.begin(); si != _sections.end(); ++si) {<br>
     // If this is first section in the segment, page align the section start<br>
@@ -522,9 +524,18 @@ template <class ELFT> void Segment<ELFT><br>
         isDataPageAlignedForNMagic = true;<br>
       }<br>
       uint64_t newAddr = llvm::RoundUpToAlignment(curAddr, (*si)->align2());<br>
+      Section<ELFT> *sec = dyn_cast<Section<ELFT>>(*si);<br>
+      StringRef curOutputSectionName =<br>
+          sec ? sec->outputSectionName() : (*si)->name();<br>
+      bool autoCreateSlice = true;<br>
+      if (curOutputSectionName == prevOutputSectionName)<br>
+        autoCreateSlice = false;<br>
       // If the newAddress computed is more than a page away, let's create<br>
       // a separate segment, so that memory is not used up while running.<br>
-      if (((newAddr - curAddr) > this->_context.getPageSize()) &&<br>
+      // Dont create a slice, if the new section falls in the same output<br>
+      // section as the previous section.<br>
+      if (autoCreateSlice &&<br>
+          ((newAddr - curAddr) > this->_context.getPageSize()) &&<br>
           (_outputMagic != ELFLinkingContext::OutputMagic::NMAGIC &&<br>
            _outputMagic != ELFLinkingContext::OutputMagic::OMAGIC)) {<br>
         auto sliceIter =<br>
@@ -577,6 +588,7 @@ template <class ELFT> void Segment<ELFT><br>
         else<br>
           curSliceSize = newAddr - curSliceAddress;<br>
       }<br>
+      prevOutputSectionName = curOutputSectionName;<br>
     }<br>
     currSection++;<br>
   }<br>
<br>
Modified: lld/trunk/test/elf/X86_64/alignoffset.test<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/lld/trunk/test/elf/X86_64/alignoffset.test?rev=222312&r1=222311&r2=222312&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/lld/trunk/test/elf/X86_64/alignoffset.test?rev=222312&r1=222311&r2=222312&view=diff</a><br>
==============================================================================<br>
--- lld/trunk/test/elf/X86_64/alignoffset.test (original)<br>
+++ lld/trunk/test/elf/X86_64/alignoffset.test Tue Nov 18 21:51:48 2014<br>
@@ -7,7 +7,7 @@<br>
 # RUN: --no-align-segments --rosegment --noinhibit-exec<br>
 # RUN: llvm-readobj -program-headers %t.exe | FileCheck %s<br>
 #<br>
-#CHECK:    Offset: 0x115C<br>
+#CHECK:    Offset: 0x15C<br>
 #CHECK:    VirtualAddress: 0x40015C<br>
 #CHECK:    PhysicalAddress: 0x40015C<br>
 #<br>
<br>
Added: lld/trunk/test/elf/X86_64/outputsegments.test<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/lld/trunk/test/elf/X86_64/outputsegments.test?rev=222312&view=auto" target="_blank">http://llvm.org/viewvc/llvm-project/lld/trunk/test/elf/X86_64/outputsegments.test?rev=222312&view=auto</a><br>
==============================================================================<br>
--- lld/trunk/test/elf/X86_64/outputsegments.test (added)<br>
+++ lld/trunk/test/elf/X86_64/outputsegments.test Tue Nov 18 21:51:48 2014<br>
@@ -0,0 +1,189 @@<br>
+# Tests that lld doesnot create separate segment if the input sections are part<br>
+# of the same output section<br>
+<br>
+# Build executable<br>
+# RUN: yaml2obj -format=elf -docnum 1 %s -o %t.o<br>
+# RUN: lld -flavor gnu -target x86_64 %t.o -o %t1.exe -static \<br>
+# RUN: --no-align-segments --noinhibit-exec<br>
+# RUN: lld -flavor gnu -target x86_64 %t.o -o %t2.exe -static \<br>
+# RUN: --noinhibit-exec<br>
+# RUN: llvm-readobj -program-headers %t1.exe | FileCheck %s -check-prefix=SEGMENTS<br>
+# RUN: llvm-readobj -program-headers %t2.exe | FileCheck %s -check-prefix=SEGMENTS<br>
+#<br>
+#SEGMENTS:    VirtualAddress: 0x400000<br>
+#SEGMENTS:    PhysicalAddress: 0x400000<br>
+#SEGMENTS:    FileSize: 288<br>
+#SEGMENTS:    MemSize: 288<br>
+#SEGMENTS:    VirtualAddress: 0x404000<br>
+#SEGMENTS:    PhysicalAddress: 0x404000<br>
+#SEGMENTS:    FileSize: 16608<br>
+#SEGMENTS:    MemSize: 16608<br>
+#SEGMENTS:    Alignment: 16384<br>
+#<br>
+# object<br>
+---<br>
+FileHeader:<br>
+  Class:           ELFCLASS64<br>
+  Data:            ELFDATA2LSB<br>
+  OSABI:           ELFOSABI_GNU<br>
+  Type:            ET_REL<br>
+  Machine:         EM_X86_64<br>
+Sections:<br>
+  - Name:            .text<br>
+    Type:            SHT_PROGBITS<br>
+    Flags:           [ SHF_ALLOC, SHF_EXECINSTR ]<br>
+    AddressAlign:    0x0000000000000004<br>
+    Content:         ''<br>
+  - Name:            .data<br>
+    Type:            SHT_PROGBITS<br>
+    Flags:           [ SHF_WRITE, SHF_ALLOC ]<br>
+    AddressAlign:    0x0000000000000004<br>
+    Content:         ''<br>
+  - Name:            .bss<br>
+    Type:            SHT_NOBITS<br>
+    Flags:           [ SHF_WRITE, SHF_ALLOC ]<br>
+    AddressAlign:    0x0000000000000004<br>
+    Content:         ''<br>
+  - Name:            .text.foo<br>
+    Type:            SHT_PROGBITS<br>
+    Flags:           [ SHF_ALLOC, SHF_EXECINSTR ]<br>
+    AddressAlign:    0x0000000000002000<br>
+    Content:         554889E54883EC1048BF0000000000000000B000E800000000B9000000008945FC89C84883C4105DC3<br>
+  - Name:            .rela.text.foo<br>
+    Type:            SHT_RELA<br>
+    Link:            .symtab<br>
+    AddressAlign:    0x0000000000000008<br>
+    Info:            .text.foo<br>
+    Relocations:<br>
+      - Offset:          0x000000000000000A<br>
+        Symbol:          .rodata.str1.1<br>
+        Type:            R_X86_64_64<br>
+        Addend:          0<br>
+      - Offset:          0x0000000000000015<br>
+        Symbol:          printf<br>
+        Type:            R_X86_64_PC32<br>
+        Addend:          -4<br>
+  - Name:            .text.bar<br>
+    Type:            SHT_PROGBITS<br>
+    Flags:           [ SHF_ALLOC, SHF_EXECINSTR ]<br>
+    AddressAlign:    0x0000000000004000<br>
+    Content:         554889E54883EC1048BF0000000000000000B000E800000000B9000000008945FC89C84883C4105DC3<br>
+  - Name:            .rela.text.bar<br>
+    Type:            SHT_RELA<br>
+    Link:            .symtab<br>
+    AddressAlign:    0x0000000000000008<br>
+    Info:            .text.bar<br>
+    Relocations:<br>
+      - Offset:          0x000000000000000A<br>
+        Symbol:          .rodata.str1.1<br>
+        Type:            R_X86_64_64<br>
+        Addend:          7<br>
+      - Offset:          0x0000000000000015<br>
+        Symbol:          printf<br>
+        Type:            R_X86_64_PC32<br>
+        Addend:          -4<br>
+  - Name:            .text.main<br>
+    Type:            SHT_PROGBITS<br>
+    Flags:           [ SHF_ALLOC, SHF_EXECINSTR ]<br>
+    AddressAlign:    0x0000000000000010<br>
+    Content:         554889E54883EC10C745FC00000000E8000000008945F8E8000000008B4DF801C189C84883C4105DC3<br>
+  - Name:            .rela.text.main<br>
+    Type:            SHT_RELA<br>
+    Link:            .symtab<br>
+    AddressAlign:    0x0000000000000008<br>
+    Info:            .text.main<br>
+    Relocations:<br>
+      - Offset:          0x0000000000000010<br>
+        Symbol:          foo<br>
+        Type:            R_X86_64_PC32<br>
+        Addend:          -4<br>
+      - Offset:          0x0000000000000018<br>
+        Symbol:          bar<br>
+        Type:            R_X86_64_PC32<br>
+        Addend:          -4<br>
+  - Name:            .rodata.str1.1<br>
+    Type:            SHT_PROGBITS<br>
+    Flags:           [ SHF_ALLOC, SHF_MERGE, SHF_STRINGS ]<br>
+    AddressAlign:    0x0000000000000001<br>
+    Content:         48656C6C6F0A00576F726C640A00<br>
+  - Name:            .comment<br>
+    Type:            SHT_PROGBITS<br>
+    Flags:           [ SHF_MERGE, SHF_STRINGS ]<br>
+    AddressAlign:    0x0000000000000001<br>
+    Content:         005562756E747520636C616E672076657273696F6E20332E352E302D73766E3231373330342D317E6578703120286272616E636865732F72656C656173655F33352920286261736564206F6E204C4C564D20332E352E302900<br>
+  - Name:            .note.GNU-stack<br>
+    Type:            SHT_PROGBITS<br>
+    AddressAlign:    0x0000000000000001<br>
+    Content:         ''<br>
+  - Name:            .eh_frame<br>
+    Type:            SHT_PROGBITS<br>
+    Flags:           [ SHF_ALLOC ]<br>
+    AddressAlign:    0x0000000000000008<br>
+    Content:         1400000000000000037A5200017810011B0C070890010000180000001C000000000000002900000000410E108602430D060000001800000038000000000000002900000000410E108602430D060000001C00000054000000000000002900000000410E108602430D0600000000000000<br>
+  - Name:            .rela.eh_frame<br>
+    Type:            SHT_RELA<br>
+    Link:            .symtab<br>
+    AddressAlign:    0x0000000000000008<br>
+    Info:            .eh_frame<br>
+    Relocations:<br>
+      - Offset:          0x0000000000000020<br>
+        Symbol:          .text.foo<br>
+        Type:            R_X86_64_PC32<br>
+        Addend:          0<br>
+      - Offset:          0x000000000000003C<br>
+        Symbol:          .text.bar<br>
+        Type:            R_X86_64_PC32<br>
+        Addend:          0<br>
+      - Offset:          0x0000000000000058<br>
+        Symbol:          .text.main<br>
+        Type:            R_X86_64_PC32<br>
+        Addend:          0<br>
+Symbols:<br>
+  Local:<br>
+    - Name:            1.c<br>
+      Type:            STT_FILE<br>
+    - Name:            .text<br>
+      Type:            STT_SECTION<br>
+      Section:         .text<br>
+    - Name:            .data<br>
+      Type:            STT_SECTION<br>
+      Section:         .data<br>
+    - Name:            .bss<br>
+      Type:            STT_SECTION<br>
+      Section:         .bss<br>
+    - Name:            .text.foo<br>
+      Type:            STT_SECTION<br>
+      Section:         .text.foo<br>
+    - Name:            .text.bar<br>
+      Type:            STT_SECTION<br>
+      Section:         .text.bar<br>
+    - Name:            .text.main<br>
+      Type:            STT_SECTION<br>
+      Section:         .text.main<br>
+    - Name:            .rodata.str1.1<br>
+      Type:            STT_SECTION<br>
+      Section:         .rodata.str1.1<br>
+    - Name:            .comment<br>
+      Type:            STT_SECTION<br>
+      Section:         .comment<br>
+    - Name:            .note.GNU-stack<br>
+      Type:            STT_SECTION<br>
+      Section:         .note.GNU-stack<br>
+    - Name:            .eh_frame<br>
+      Type:            STT_SECTION<br>
+      Section:         .eh_frame<br>
+  Global:<br>
+    - Name:            bar<br>
+      Type:            STT_FUNC<br>
+      Section:         .text.bar<br>
+      Size:            0x0000000000000029<br>
+    - Name:            foo<br>
+      Type:            STT_FUNC<br>
+      Section:         .text.foo<br>
+      Size:            0x0000000000000029<br>
+    - Name:            main<br>
+      Type:            STT_FUNC<br>
+      Section:         .text.main<br>
+      Size:            0x0000000000000029<br>
+    - Name:            printf<br>
+...<br>
<br>
Modified: lld/trunk/test/elf/phdr.test<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/lld/trunk/test/elf/phdr.test?rev=222312&r1=222311&r2=222312&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/lld/trunk/test/elf/phdr.test?rev=222312&r1=222311&r2=222312&view=diff</a><br>
==============================================================================<br>
--- lld/trunk/test/elf/phdr.test (original)<br>
+++ lld/trunk/test/elf/phdr.test Tue Nov 18 21:51:48 2014<br>
@@ -59,7 +59,7 @@ I386-NEXT:     Alignment: 4096<br>
 I386-NEXT:   }<br>
 I386-NEXT:   ProgramHeader {<br>
 I386-NEXT:     Type: PT_LOAD (0x1)<br>
-I386-NEXT:     Offset: 0x2000<br>
+I386-NEXT:     Offset: 0x4000<br>
 I386-NEXT:     VirtualAddress: 0x4000<br>
 I386-NEXT:     PhysicalAddress: 0x4000<br>
 I386-NEXT:     FileSize: 4<br>
<br>
<br>
_______________________________________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@cs.uiuc.edu">llvm-commits@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits</a><br>
</blockquote></div><br></div></div>