[PATCH] D30461: [ELF] - Check that we do not mix regular and synthetic sections.

George Rimar via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Feb 28 08:02:50 PST 2017


grimar created this revision.

Our current logic assumes we place synthetic sections
after all regular ones. I lost some time during writing another patch
because placed some code that added sections before code
that adds common section. That broke logic of removeUnusedSyntheticSections
for me.

There are few possible ways to avoid that.
One of them would be to remove dependency on such logic from
removeUnusedSyntheticSections().

The second is this patch, which introduces an assert check to ensure
that does not happen.


https://reviews.llvm.org/D30461

Files:
  ELF/Writer.cpp


Index: ELF/Writer.cpp
===================================================================
--- ELF/Writer.cpp
+++ ELF/Writer.cpp
@@ -307,7 +307,19 @@
   // you can call lld::elf::main more than once as a library.
   memset(&Out::First, 0, sizeof(Out));
 
-  auto Add = [](InputSectionBase *Sec) { InputSections.push_back(Sec); };
+  auto Add = [](InputSectionBase *Sec) {
+    // We assume all synthetic sections are placed to the end,
+    // after regular ones. Do not mix them. We allow merge sections
+    // because them are converted to synthetic mergeable sections later.
+    if (!InputSections.empty()) {
+      auto IsOk = [](InputSectionBase::Kind K) {
+        return K == InputSectionBase::Synthetic || K == InputSectionBase::Merge;
+      };
+      if (IsOk(InputSections.back()->kind()))
+        assert(IsOk(Sec->kind()));
+    }
+    InputSections.push_back(Sec);
+  };
 
   // Create singleton output sections.
   Out::Bss = make<OutputSection>(".bss", SHT_NOBITS, SHF_ALLOC | SHF_WRITE);
@@ -324,6 +336,12 @@
   Out::ProgramHeaders = make<OutputSection>("", 0, SHF_ALLOC);
   Out::ProgramHeaders->updateAlignment(sizeof(uintX_t));
 
+  InputSection *Common = createCommonSection<ELFT>();
+  if (!Common->Data.empty()) {
+    In<ELFT>::Common = Common;
+    Add(Common);
+  }
+
   if (needsInterpSection<ELFT>()) {
     In<ELFT>::Interp = createInterpSection();
     Add(In<ELFT>::Interp);
@@ -344,12 +362,6 @@
     Add(In<ELFT>::BuildId);
   }
 
-  InputSection *Common = createCommonSection<ELFT>();
-  if (!Common->Data.empty()) {
-    In<ELFT>::Common = Common;
-    Add(Common);
-  }
-
   // Add MIPS-specific sections.
   bool HasDynSymTab =
       !Symtab<ELFT>::X->getSharedFiles().empty() || Config->pic() ||


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D30461.90034.patch
Type: text/x-patch
Size: 1735 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170228/6f6d4225/attachment.bin>


More information about the llvm-commits mailing list