Add a flag to InputSectionBase for linker script.
authorRui Ueyama <ruiu@google.com>
Sun, 20 Nov 2016 23:15:52 +0000 (23:15 +0000)
committerRui Ueyama <ruiu@google.com>
Sun, 20 Nov 2016 23:15:52 +0000 (23:15 +0000)
Previously, we set (uintptr_t)-1 to InputSectionBase::OutSec to record
that a section has already been set to be assigned to some output section
by linker scripts. Later, we restored nullptr to the pointer to use
the field for the original purpose. That overloading is not very easy to
understand.

This patch adds a bit flag for that purpose, so that we don't need
to piggyback the flag on an unrelated pointer.

llvm-svn: 287508

lld/ELF/InputSection.h
lld/ELF/LinkerScript.cpp

index 5b0c6fc..bb780fd 100644 (file)
@@ -45,8 +45,8 @@ public:
   // If GC is disabled, all sections are considered live by default.
   InputSectionData(Kind SectionKind, StringRef Name, ArrayRef<uint8_t> Data,
                    bool Compressed, bool Live)
-      : SectionKind(SectionKind), Live(Live), Compressed(Compressed),
-        Name(Name), Data(Data) {}
+      : SectionKind(SectionKind), Live(Live), Assigned(false),
+        Compressed(Compressed), Name(Name), Data(Data) {}
 
 private:
   unsigned SectionKind : 3;
@@ -54,8 +54,9 @@ private:
 public:
   Kind kind() const { return (Kind)SectionKind; }
 
-  unsigned Live : 1; // for garbage collection
-  unsigned Compressed : 1;
+  unsigned Live : 1;       // for garbage collection
+  unsigned Assigned : 1;   // for linker script
+  unsigned Compressed : 1; // true if section data is compressed
   uint32_t Alignment;
   StringRef Name;
   ArrayRef<uint8_t> Data;
index 03a0349..388640c 100644 (file)
@@ -199,7 +199,7 @@ void LinkerScript<ELFT>::computeInputSections(InputSectionDescription *I) {
     size_t SizeBefore = I->Sections.size();
 
     for (InputSectionBase<ELFT> *S : Symtab<ELFT>::X->Sections) {
-      if (!S->Live || S->OutSec)
+      if (!S->Live || S->Assigned)
         continue;
 
       StringRef Filename;
@@ -207,8 +207,10 @@ void LinkerScript<ELFT>::computeInputSections(InputSectionDescription *I) {
         Filename = sys::path::filename(F->getName());
 
       if (I->FilePat.match(Filename) && !Pat.ExcludedFilePat.match(Filename) &&
-          Pat.SectionPat.match(S->Name))
+          Pat.SectionPat.match(S->Name)) {
         I->Sections.push_back(S);
+        S->Assigned = true;
+      }
     }
 
     // Sort sections as instructed by SORT-family commands and --sort-section
@@ -232,13 +234,6 @@ void LinkerScript<ELFT>::computeInputSections(InputSectionDescription *I) {
       sortSections(Begin, End, Pat.SortOuter);
     }
   }
-
-  // We do not add duplicate input sections, so mark them with a dummy output
-  // section for now.
-  for (InputSectionData *S : I->Sections) {
-    auto *S2 = static_cast<InputSectionBase<ELFT> *>(S);
-    S2->OutSec = (OutputSectionBase *)-1;
-  }
 }
 
 template <class ELFT>
@@ -263,12 +258,6 @@ LinkerScript<ELFT>::createInputSectionList(OutputSectionCommand &OutCmd) {
       Ret.push_back(static_cast<InputSectionBase<ELFT> *>(S));
   }
 
-  // After we created final list we should now set OutSec pointer to null,
-  // instead of -1. Otherwise we may get a crash when writing relocs, in
-  // case section is discarded by linker script
-  for (InputSectionBase<ELFT> *S : Ret)
-    S->OutSec = nullptr;
-
   return Ret;
 }
 
@@ -343,7 +332,7 @@ void LinkerScript<ELFT>::processCommands(OutputSectionFactory<ELFT> &Factory) {
 
       if (!matchConstraints<ELFT>(V, Cmd->Constraint)) {
         for (InputSectionBase<ELFT> *S : V)
-          S->OutSec = nullptr;
+          S->Assigned = false;
         Opt.Commands.erase(Iter);
         --I;
         continue;