[lld] r266974 - [ELF] - Get rid of SectionOrder array.

George Rimar via llvm-commits llvm-commits at lists.llvm.org
Thu Apr 21 03:22:02 PDT 2016


Author: grimar
Date: Thu Apr 21 05:22:02 2016
New Revision: 266974

URL: http://llvm.org/viewvc/llvm-project?rev=266974&view=rev
Log:
[ELF] - Get rid of SectionOrder array.

SectionOrder vector was a part of LinkerScript class.

It can be removed because Commands vector contains the 
same information and SectiorOrder is just a subset.

Differential revision: http://reviews.llvm.org/D19171


Added:
    lld/trunk/test/ELF/linkerscript-orphans.s
Modified:
    lld/trunk/ELF/LinkerScript.cpp
    lld/trunk/ELF/LinkerScript.h

Modified: lld/trunk/ELF/LinkerScript.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/LinkerScript.cpp?rev=266974&r1=266973&r2=266974&view=diff
==============================================================================
--- lld/trunk/ELF/LinkerScript.cpp (original)
+++ lld/trunk/ELF/LinkerScript.cpp Thu Apr 21 05:22:02 2016
@@ -204,8 +204,7 @@ void LinkerScript<ELFT>::assignAddresses
   // https://sourceware.org/binutils/docs/ld/Orphan-Sections.html#Orphan-Sections.
   for (OutputSectionBase<ELFT> *Sec : Sections) {
     StringRef Name = Sec->getName();
-    auto I = std::find(Opt.SectionOrder.begin(), Opt.SectionOrder.end(), Name);
-    if (I == Opt.SectionOrder.end())
+    if (getSectionOrder(Name) == (uint32_t)-1)
       Opt.Commands.push_back({SectionKind, {}, Name});
   }
 
@@ -249,15 +248,23 @@ ArrayRef<uint8_t> LinkerScript<ELFT>::ge
   return I->second;
 }
 
-// A compartor to sort output sections. Returns -1 or 1 if both
-// A and B are mentioned in linker scripts. Otherwise, returns 0
-// to use the default rule which is implemented in Writer.cpp.
+template <class ELFT>
+uint32_t LinkerScript<ELFT>::getSectionOrder(StringRef Name) {
+  auto Begin = Opt.Commands.begin();
+  auto End = Opt.Commands.end();
+  auto I = std::find_if(Begin, End, [&](SectionsCommand &N) {
+    return N.Kind == SectionKind && N.SectionName == Name;
+  });
+  return I == End ? (uint32_t)-1 : (I - Begin);
+}
+
+// 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) {
-  auto E = Opt.SectionOrder.end();
-  auto I = std::find(Opt.SectionOrder.begin(), E, A);
-  auto J = std::find(Opt.SectionOrder.begin(), E, B);
-  if (I == E || J == E)
+  uint32_t I = getSectionOrder(A);
+  uint32_t J = getSectionOrder(B);
+  if (I == (uint32_t)-1 && J == (uint32_t)-1)
     return 0;
   return I < J ? -1 : 1;
 }
@@ -509,7 +516,6 @@ void ScriptParser::readLocationCounterVa
 
 void ScriptParser::readOutputSectionDescription() {
   StringRef OutSec = next();
-  Opt.SectionOrder.push_back(OutSec);
   Opt.Commands.push_back({SectionKind, {}, OutSec});
   expect(":");
   expect("{");

Modified: lld/trunk/ELF/LinkerScript.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/LinkerScript.h?rev=266974&r1=266973&r2=266974&view=diff
==============================================================================
--- lld/trunk/ELF/LinkerScript.h (original)
+++ lld/trunk/ELF/LinkerScript.h Thu Apr 21 05:22:02 2016
@@ -61,9 +61,6 @@ struct ScriptConfiguration {
   // SECTIONS commands.
   std::vector<SectionRule> Sections;
 
-  // Output sections are sorted by this order.
-  std::vector<StringRef> SectionOrder;
-
   // Section fill attribute for each section.
   llvm::StringMap<std::vector<uint8_t>> Filler;
 
@@ -86,6 +83,7 @@ public:
   bool shouldKeep(InputSectionBase<ELFT> *S);
   void assignAddresses(std::vector<OutputSectionBase<ELFT> *> &S);
   int compareSections(StringRef A, StringRef B);
+  uint32_t getSectionOrder(StringRef Name);
 
 private:
   SectionRule *find(InputSectionBase<ELFT> *S);

Added: lld/trunk/test/ELF/linkerscript-orphans.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/linkerscript-orphans.s?rev=266974&view=auto
==============================================================================
--- lld/trunk/test/ELF/linkerscript-orphans.s (added)
+++ lld/trunk/test/ELF/linkerscript-orphans.s Thu Apr 21 05:22:02 2016
@@ -0,0 +1,31 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t
+
+# RUN: echo "SECTIONS { .writable : { *(.writable) } }" > %t.script
+# RUN: ld.lld -o %t.out --script %t.script %t
+# RUN: llvm-objdump -section-headers %t.out | \
+# RUN:   FileCheck -check-prefix=TEXTORPHAN %s
+
+# RUN: echo "SECTIONS { .text : { *(.text) } }" > %t.script
+# RUN: ld.lld -o %t.out --script %t.script %t
+# RUN: llvm-objdump -section-headers %t.out | \
+# RUN:   FileCheck -check-prefix=WRITABLEORPHAN %s
+
+# TEXTORPHAN:      Sections:
+# TEXTORPHAN-NEXT: Idx Name
+# TEXTORPHAN-NEXT:   0
+# TEXTORPHAN-NEXT:   1 .writable
+# TEXTORPHAN-NEXT:   2 .text
+
+# WRITABLEORPHAN:      Sections:
+# WRITABLEORPHAN-NEXT: Idx Name
+# WRITABLEORPHAN-NEXT:   0
+# WRITABLEORPHAN-NEXT:   1 .text
+# WRITABLEORPHAN-NEXT:   2 .writable
+
+.global _start
+_start:
+ nop
+
+.section .writable,"aw"
+ .zero 4




More information about the llvm-commits mailing list