[lld] r373347 - ELF: Add .interp synthetic sections first in createSyntheticSections().

Peter Collingbourne via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 1 09:10:13 PDT 2019


Author: pcc
Date: Tue Oct  1 09:10:13 2019
New Revision: 373347

URL: http://llvm.org/viewvc/llvm-project?rev=373347&view=rev
Log:
ELF: Add .interp synthetic sections first in createSyntheticSections().

Our .interp section is not a SyntheticSection. As a result, it terminates the
loop in removeUnusedSyntheticSections(). This has at least two consequences:

- The synthetic .bss and .bss.rel.ro sections are always present in
  dynamically linked executables, even when they are not needed.
- The synthetic .ARM.exidx (and possibly other) sections are always present
  in partitions other than the last one, even when not needed.
  .ARM.exidx in particular is problematic because it assumes that its
  list of code sections is non-empty in getLinkOrderDep(), which can
  lead to a crash if the partition does not have any code sections.

Fix these problems by moving the creation of the .interp sections to the
top of createSyntheticSections(). While here, make the code a little less
error-prone by changing the add() lambdas to take a SyntheticSection instead
of an InputSectionBase.

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

Added:
    lld/trunk/test/ELF/partition-dynamic-linker.s
Modified:
    lld/trunk/ELF/SyntheticSections.cpp
    lld/trunk/ELF/Writer.cpp
    lld/trunk/test/ELF/Inputs/shared.s
    lld/trunk/test/ELF/dynamic-linker.s

Modified: lld/trunk/ELF/SyntheticSections.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/SyntheticSections.cpp?rev=373347&r1=373346&r2=373347&view=diff
==============================================================================
--- lld/trunk/ELF/SyntheticSections.cpp (original)
+++ lld/trunk/ELF/SyntheticSections.cpp Tue Oct  1 09:10:13 2019
@@ -257,10 +257,8 @@ InputSection *elf::createInterpSection()
   StringRef s = saver.save(config->dynamicLinker);
   ArrayRef<uint8_t> contents = {(const uint8_t *)s.data(), s.size() + 1};
 
-  auto *sec = make<InputSection>(nullptr, SHF_ALLOC, SHT_PROGBITS, 1, contents,
-                                 ".interp");
-  sec->markLive();
-  return sec;
+  return make<InputSection>(nullptr, SHF_ALLOC, SHT_PROGBITS, 1, contents,
+                            ".interp");
 }
 
 Defined *elf::addSyntheticLocal(StringRef name, uint8_t type, uint64_t value,

Modified: lld/trunk/ELF/Writer.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Writer.cpp?rev=373347&r1=373346&r2=373347&view=diff
==============================================================================
--- lld/trunk/ELF/Writer.cpp (original)
+++ lld/trunk/ELF/Writer.cpp Tue Oct  1 09:10:13 2019
@@ -314,7 +314,18 @@ template <class ELFT> void elf::createSy
   // 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); };
+  // Add the .interp section first because it is not a SyntheticSection.
+  // The removeUnusedSyntheticSections() function relies on the
+  // SyntheticSections coming last.
+  if (needsInterpSection()) {
+    for (size_t i = 1; i <= partitions.size(); ++i) {
+      InputSection *sec = createInterpSection();
+      sec->partition = i;
+      inputSections.push_back(sec);
+    }
+  }
+
+  auto add = [](SyntheticSection *sec) { inputSections.push_back(sec); };
 
   in.shStrTab = make<StringTableSection>(".shstrtab", false);
 
@@ -356,7 +367,7 @@ template <class ELFT> void elf::createSy
   StringRef relaDynName = config->isRela ? ".rela.dyn" : ".rel.dyn";
 
   for (Partition &part : partitions) {
-    auto add = [&](InputSectionBase *sec) {
+    auto add = [&](SyntheticSection *sec) {
       sec->partition = part.getNumber();
       inputSections.push_back(sec);
     };
@@ -384,9 +395,6 @@ template <class ELFT> void elf::createSy
       part.relaDyn =
           make<RelocationSection<ELFT>>(relaDynName, config->zCombreloc);
 
-    if (needsInterpSection())
-      add(createInterpSection());
-
     if (config->hasDynSymTab) {
       part.dynSymTab = make<SymbolTableSection<ELFT>>(*part.dynStrTab);
       add(part.dynSymTab);

Modified: lld/trunk/test/ELF/Inputs/shared.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/Inputs/shared.s?rev=373347&r1=373346&r2=373347&view=diff
==============================================================================
--- lld/trunk/test/ELF/Inputs/shared.s (original)
+++ lld/trunk/test/ELF/Inputs/shared.s Tue Oct  1 09:10:13 2019
@@ -1,9 +1,9 @@
 .global bar
-.type bar, @function
+.type bar, %function
 bar:
 
 .global bar2
-.type bar2, @function
+.type bar2, %function
 bar2:
 
 .global zed

Modified: lld/trunk/test/ELF/dynamic-linker.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/dynamic-linker.s?rev=373347&r1=373346&r2=373347&view=diff
==============================================================================
--- lld/trunk/test/ELF/dynamic-linker.s (original)
+++ lld/trunk/test/ELF/dynamic-linker.s Tue Oct  1 09:10:13 2019
@@ -4,10 +4,10 @@
 # RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
 
 # RUN: ld.lld --dynamic-linker foo %t.o %t.so -o %t
-# RUN: llvm-readelf -program-headers %t | FileCheck %s
+# RUN: llvm-readelf --program-headers --section-headers %t | FileCheck --implicit-check-not=.bss %s
 
 # RUN: ld.lld --dynamic-linker=foo %t.o %t.so -o %t
-# RUN: llvm-readelf -program-headers %t | FileCheck %s
+# RUN: llvm-readelf --program-headers --section-headers %t | FileCheck --implicit-check-not=.bss %s
 
 # CHECK: [Requesting program interpreter: foo]
 

Added: lld/trunk/test/ELF/partition-dynamic-linker.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/partition-dynamic-linker.s?rev=373347&view=auto
==============================================================================
--- lld/trunk/test/ELF/partition-dynamic-linker.s (added)
+++ lld/trunk/test/ELF/partition-dynamic-linker.s Tue Oct  1 09:10:13 2019
@@ -0,0 +1,24 @@
+## Test that we don't create a .ARM.exidx for the main partition.
+## Previously we were doing so, which is unnecessary and led to a crash.
+
+# RUN: llvm-mc -filetype=obj -triple=armv7a-none-linux-gnueabi %p/Inputs/shared.s -o %t1.o
+# RUN: ld.lld -shared %t1.o -o %t.so
+# RUN: llvm-mc -filetype=obj -triple=armv7a-none-linux-gnueabi %s -o %t.o
+
+# RUN: ld.lld -shared --gc-sections --dynamic-linker foo %t.o %t.so -o %t
+# RUN: llvm-readelf --section-headers %t | FileCheck %s
+
+# CHECK: .ARM.exidx
+# CHECK-NOT: .ARM.exidx
+
+.section .llvm_sympart,"",%llvm_sympart
+.asciz "part1"
+.4byte p1
+
+.section .text.p1,"ax",%progbits
+.globl p1
+p1:
+.fnstart
+bx lr
+.cantunwind
+.fnend




More information about the llvm-commits mailing list