[lld] r282035 - Revert "Revert "Only restrict order if both sections are in the script.""

Rafael Espindola via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 20 15:43:15 PDT 2016


Author: rafael
Date: Tue Sep 20 17:43:15 2016
New Revision: 282035

URL: http://llvm.org/viewvc/llvm-project?rev=282035&view=rev
Log:
Revert "Revert "Only restrict order if both sections are in the script.""
This reverts commit r282021, bringing back r282015.

The problem was that the comparison function was not a strict weak
ordering anymore, which this patch fixes.

Original message:

Only restrict order if both sections are in the script.

This matches gold and bfd behavior and is required to handle some scripts.

The script has to assume where PT_LOADs start in order to align that
spot. If we don't allow section it doesn't know about to move to the
middle, we can need more PT_LOADs and those will not be aligned.

Added:
    lld/trunk/test/ELF/linkerscript/sort-non-script.s
Modified:
    lld/trunk/ELF/LinkerScript.cpp
    lld/trunk/ELF/LinkerScript.h
    lld/trunk/ELF/Writer.cpp
    lld/trunk/test/ELF/linkerscript/orphans.s
    lld/trunk/test/ELF/linkerscript/repsection-symbol.s

Modified: lld/trunk/ELF/LinkerScript.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/LinkerScript.cpp?rev=282035&r1=282034&r2=282035&view=diff
==============================================================================
--- lld/trunk/ELF/LinkerScript.cpp (original)
+++ lld/trunk/ELF/LinkerScript.cpp Tue Sep 20 17:43:15 2016
@@ -663,17 +663,6 @@ template <class ELFT> int LinkerScript<E
   return INT_MAX;
 }
 
-// A compartor to sort output sections. Returns -1 or 1 if
-// A or B are mentioned in linker script. Otherwise, returns 0.
-template <class ELFT>
-int LinkerScript<ELFT>::compareSections(StringRef A, StringRef B) {
-  int I = getSectionIndex(A);
-  int J = getSectionIndex(B);
-  if (I == INT_MAX && J == INT_MAX)
-    return 0;
-  return I < J ? -1 : 1;
-}
-
 template <class ELFT> bool LinkerScript<ELFT>::hasPhdrsCommands() {
   return !Opt.PhdrsCommands.empty();
 }

Modified: lld/trunk/ELF/LinkerScript.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/LinkerScript.h?rev=282035&r1=282034&r2=282035&view=diff
==============================================================================
--- lld/trunk/ELF/LinkerScript.h (original)
+++ lld/trunk/ELF/LinkerScript.h Tue Sep 20 17:43:15 2016
@@ -193,7 +193,6 @@ public:
   bool shouldKeep(InputSectionBase<ELFT> *S);
   void assignOffsets(OutputSectionCommand *Cmd);
   void assignAddresses();
-  int compareSections(StringRef A, StringRef B);
   bool hasPhdrsCommands();
   uint64_t getOutputSectionAddress(StringRef Name) override;
   uint64_t getOutputSectionSize(StringRef Name) override;
@@ -203,6 +202,8 @@ public:
 
   std::vector<OutputSectionBase<ELFT> *> *OutputSections;
 
+  int getSectionIndex(StringRef Name);
+
 private:
   void computeInputSections(InputSectionDescription *);
 
@@ -216,7 +217,6 @@ private:
   // "ScriptConfig" is a bit too long, so define a short name for it.
   ScriptConfiguration &Opt = *ScriptConfig;
 
-  int getSectionIndex(StringRef Name);
   std::vector<size_t> getPhdrIndices(StringRef SectionName);
   size_t getPhdrIndex(StringRef PhdrName);
 

Modified: lld/trunk/ELF/Writer.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Writer.cpp?rev=282035&r1=282034&r2=282035&view=diff
==============================================================================
--- lld/trunk/ELF/Writer.cpp (original)
+++ lld/trunk/ELF/Writer.cpp Tue Sep 20 17:43:15 2016
@@ -451,14 +451,19 @@ static bool compareSections(OutputSectio
   if (AIsAlloc != BIsAlloc)
     return AIsAlloc;
 
-  int Comp = Script<ELFT>::X->compareSections(A->getName(), B->getName());
-  if (Comp != 0)
-    return Comp < 0;
-
-  // We don't have any special requirements for the relative order of
-  // two non allocatable sections.
+  // If  A and B are mentioned in linker script, use that order.
+  int AIndex = Script<ELFT>::X->getSectionIndex(A->getName());
+  int BIndex = Script<ELFT>::X->getSectionIndex(B->getName());
+  bool AInScript = AIndex != INT_MAX;
+  bool BInScript = BIndex != INT_MAX;
+  if (AInScript && BInScript)
+    return AIndex < BIndex;
+
+  // We don't have any special requirements for the relative order of two non
+  // allocatable sections.
+  // Just put linker script sections first to satisfy strict weak ordering.
   if (!AIsAlloc)
-    return false;
+    return AInScript;
 
   // We want the read only sections first so that they go in the PT_LOAD
   // covering the program headers at the start of the file.
@@ -507,7 +512,8 @@ static bool compareSections(OutputSectio
     return getPPC64SectionRank(A->getName()) <
            getPPC64SectionRank(B->getName());
 
-  return false;
+  // Just put linker script sections first to satisfy strict weak ordering.
+  return AInScript;
 }
 
 template <class ELFT> static bool isDiscarded(InputSectionBase<ELFT> *S) {

Modified: lld/trunk/test/ELF/linkerscript/orphans.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/linkerscript/orphans.s?rev=282035&r1=282034&r2=282035&view=diff
==============================================================================
--- lld/trunk/test/ELF/linkerscript/orphans.s (original)
+++ lld/trunk/test/ELF/linkerscript/orphans.s Tue Sep 20 17:43:15 2016
@@ -14,8 +14,8 @@
 # TEXTORPHAN:      Sections:
 # TEXTORPHAN-NEXT: Idx Name
 # TEXTORPHAN-NEXT:   0
-# TEXTORPHAN-NEXT:   1 .writable
-# TEXTORPHAN-NEXT:   2 .text
+# TEXTORPHAN-NEXT:   1 .text
+# TEXTORPHAN-NEXT:   2 .writable
 
 # WRITABLEORPHAN:      Sections:
 # WRITABLEORPHAN-NEXT: Idx Name

Modified: lld/trunk/test/ELF/linkerscript/repsection-symbol.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/linkerscript/repsection-symbol.s?rev=282035&r1=282034&r2=282035&view=diff
==============================================================================
--- lld/trunk/test/ELF/linkerscript/repsection-symbol.s (original)
+++ lld/trunk/test/ELF/linkerscript/repsection-symbol.s Tue Sep 20 17:43:15 2016
@@ -6,13 +6,13 @@
 # RUN: llvm-readobj -t %t1 | FileCheck %s
 
 # CHECK:      Name: foo1
-# CHECK-NEXT: Value: 0x200
+# CHECK-NEXT: Value: 0x288
 
 # CHECK:      Name: foo2
-# CHECK-NEXT: Value: 0x208
+# CHECK-NEXT: Value: 0x290
 
 # CHECK:      Name: foo3
-# CHECK-NEXT: Value: 0x20C
+# CHECK-NEXT: Value: 0x294
 
 .section .foo.1,"a"
  .long 1

Added: lld/trunk/test/ELF/linkerscript/sort-non-script.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/linkerscript/sort-non-script.s?rev=282035&view=auto
==============================================================================
--- lld/trunk/test/ELF/linkerscript/sort-non-script.s (added)
+++ lld/trunk/test/ELF/linkerscript/sort-non-script.s Tue Sep 20 17:43:15 2016
@@ -0,0 +1,16 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t
+
+# RUN: echo "SECTIONS { foo : {*(foo)} }" > %t.script
+# RUN: ld.lld -o %t1 --script %t.script %t -shared
+# RUN: llvm-readobj -elf-output-style=GNU -s %t1 | FileCheck %s
+
+# CHECK:      .dynsym  {{.*}}   A
+# CHECK-NEXT: .hash    {{.*}}   A
+# CHECK-NEXT: .dynstr  {{.*}}   A
+# CHECK-NEXT: .text    {{.*}}   AX
+# CHECK-NEXT: .dynamic {{.*}}  WA
+# CHECK-NEXT: foo      {{.*}}  WA
+
+.section foo, "aw"
+.byte 0




More information about the llvm-commits mailing list