[lld] r333052 - ELF: Do not ICF two sections with different output sections.

Peter Collingbourne via llvm-commits llvm-commits at lists.llvm.org
Tue May 22 18:58:43 PDT 2018


Author: pcc
Date: Tue May 22 18:58:43 2018
New Revision: 333052

URL: http://llvm.org/viewvc/llvm-project?rev=333052&view=rev
Log:
ELF: Do not ICF two sections with different output sections.

Note that this doesn't do the right thing in the case where there is
a linker script. We probably need to move output section assignment
before ICF to get the correct behaviour here.

Differential Revision: https://reviews.llvm.org/D47241

Added:
    lld/trunk/test/ELF/icf-different-output-sections.s
Modified:
    lld/trunk/ELF/ICF.cpp
    lld/trunk/ELF/InputSection.cpp
    lld/trunk/ELF/InputSection.h
    lld/trunk/ELF/Writer.cpp
    lld/trunk/ELF/Writer.h

Modified: lld/trunk/ELF/ICF.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/ICF.cpp?rev=333052&r1=333051&r2=333052&view=diff
==============================================================================
--- lld/trunk/ELF/ICF.cpp (original)
+++ lld/trunk/ELF/ICF.cpp Tue May 22 18:58:43 2018
@@ -78,6 +78,7 @@
 #include "SymbolTable.h"
 #include "Symbols.h"
 #include "SyntheticSections.h"
+#include "Writer.h"
 #include "lld/Common/Threads.h"
 #include "llvm/ADT/Hashing.h"
 #include "llvm/BinaryFormat/ELF.h"
@@ -302,6 +303,13 @@ bool ICF<ELFT>::equalsConstant(const Inp
       A->getSize() != B->getSize() || A->Data != B->Data)
     return false;
 
+  // If two sections have different output sections, we cannot merge them.
+  // FIXME: This doesn't do the right thing in the case where there is a linker
+  // script. We probably need to move output section assignment before ICF to
+  // get the correct behaviour here.
+  if (getOutputSectionName(A) != getOutputSectionName(B))
+    return false;
+
   if (A->AreRelocsRela)
     return constantEq(A, A->template relas<ELFT>(), B,
                       B->template relas<ELFT>());

Modified: lld/trunk/ELF/InputSection.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/InputSection.cpp?rev=333052&r1=333051&r2=333052&view=diff
==============================================================================
--- lld/trunk/ELF/InputSection.cpp (original)
+++ lld/trunk/ELF/InputSection.cpp Tue May 22 18:58:43 2018
@@ -325,7 +325,7 @@ template <class ELFT> void InputSection:
     *To++ = Sections[Idx]->getOutputSection()->SectionIndex;
 }
 
-InputSectionBase *InputSection::getRelocatedSection() {
+InputSectionBase *InputSection::getRelocatedSection() const {
   if (!File || (Type != SHT_RELA && Type != SHT_REL))
     return nullptr;
   ArrayRef<InputSectionBase *> Sections = File->getSections();

Modified: lld/trunk/ELF/InputSection.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/InputSection.h?rev=333052&r1=333051&r2=333052&view=diff
==============================================================================
--- lld/trunk/ELF/InputSection.h (original)
+++ lld/trunk/ELF/InputSection.h Tue May 22 18:58:43 2018
@@ -317,7 +317,7 @@ public:
 
   static bool classof(const SectionBase *S);
 
-  InputSectionBase *getRelocatedSection();
+  InputSectionBase *getRelocatedSection() const;
 
   template <class ELFT, class RelTy>
   void relocateNonAlloc(uint8_t *Buf, llvm::ArrayRef<RelTy> Rels);

Modified: lld/trunk/ELF/Writer.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Writer.cpp?rev=333052&r1=333051&r2=333052&view=diff
==============================================================================
--- lld/trunk/ELF/Writer.cpp (original)
+++ lld/trunk/ELF/Writer.cpp Tue May 22 18:58:43 2018
@@ -92,7 +92,7 @@ static bool isSectionPrefix(StringRef Pr
   return Name.startswith(Prefix) || Name == Prefix.drop_back();
 }
 
-StringRef elf::getOutputSectionName(InputSectionBase *S) {
+StringRef elf::getOutputSectionName(const InputSectionBase *S) {
   if (Config->Relocatable)
     return S->Name;
 

Modified: lld/trunk/ELF/Writer.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Writer.h?rev=333052&r1=333051&r2=333052&view=diff
==============================================================================
--- lld/trunk/ELF/Writer.h (original)
+++ lld/trunk/ELF/Writer.h Tue May 22 18:58:43 2018
@@ -48,7 +48,7 @@ struct PhdrEntry {
 };
 
 void addReservedSymbols();
-llvm::StringRef getOutputSectionName(InputSectionBase *S);
+llvm::StringRef getOutputSectionName(const InputSectionBase *S);
 
 template <class ELFT> uint32_t calcMipsEFlags();
 

Added: lld/trunk/test/ELF/icf-different-output-sections.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/icf-different-output-sections.s?rev=333052&view=auto
==============================================================================
--- lld/trunk/test/ELF/icf-different-output-sections.s (added)
+++ lld/trunk/test/ELF/icf-different-output-sections.s Tue May 22 18:58:43 2018
@@ -0,0 +1,9 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t
+# RUN: ld.lld %t -o %t2 --icf=all --print-icf-sections | count 0
+
+.section foo,"ax"
+.byte 42
+
+.section bar,"ax"
+.byte 42




More information about the llvm-commits mailing list