[PATCH] D38361: [ELF] Stop setting output section size early

James Henderson via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Nov 20 04:06:43 PST 2017


jhenderson updated this revision to Diff 123561.
jhenderson edited the summary of this revision.
jhenderson added a comment.

- Removed changes to do with DT_MIPS_LOCAL_GOTNO. These are present in https://reviews.llvm.org/D39493, which must land first (otherwise this change breaks the MIPS GOT tests).
- Removed removal of MipsGot->finalizeContents(). This function is unnecessary, but harmless, so to make this diff simpler, I'm removing it for now. A later change could remove it if desired.


https://reviews.llvm.org/D38361

Files:
  ELF/InputSection.h
  ELF/OutputSections.cpp
  ELF/OutputSections.h
  test/ELF/linkerscript/unused-synthetic.s


Index: test/ELF/linkerscript/unused-synthetic.s
===================================================================
--- test/ELF/linkerscript/unused-synthetic.s
+++ test/ELF/linkerscript/unused-synthetic.s
@@ -13,6 +13,16 @@
 # CHECK:      .text
 # CHECK-NEXT: .dynsym
 
+# Test that the size of a removed unused synthetic input section does not get
+# added to the output section size, if the output section has symbol
+# assignments, preventing removal of the output section.
+# RUN: echo "SECTIONS { \
+# RUN:    .got.plt : { a_sym = .; *(.got.plt) } \
+# RUN:  }" > %t2.script
+# RUN: ld.lld -shared -o %t2.so --script %t2.script %t.o
+# RUN: llvm-objdump -section-headers %t2.so | FileCheck %s --check-prefix=CHECK2
+# CHECK2: .got.plt 00000000
+
 .global _start
 _start:
   nop
Index: ELF/OutputSections.h
===================================================================
--- ELF/OutputSections.h
+++ ELF/OutputSections.h
@@ -110,6 +110,10 @@
   std::vector<uint8_t> ZDebugHeader;
   llvm::SmallVector<char, 1> CompressedData;
 
+  // The number of input sections assigned to this section excluding
+  // late-inserted sections, such as thunk sections.
+  size_t InputSectionCount = 0;
+
   uint32_t getFiller();
 };
 
Index: ELF/OutputSections.cpp
===================================================================
--- ELF/OutputSections.cpp
+++ ELF/OutputSections.cpp
@@ -116,13 +116,7 @@
   IS->Parent = this;
   Flags |= IS->Flags;
   Alignment = std::max(Alignment, IS->Alignment);
-
-  // The actual offsets will be computed by assignAddresses. For now, use
-  // crude approximation so that it is at least easy for other code to know the
-  // section order. It is also used to calculate the output section size early
-  // for compressed debug sections.
-  IS->OutSecOff = alignTo(Size, IS->Alignment);
-  this->Size = IS->OutSecOff + IS->getSize();
+  IS->OutSecPos = InputSectionCount++;
 
   // If this section contains a table of fixed-size entries, sh_entsize
   // holds the element size. If it contains elements of different size we
@@ -189,6 +183,14 @@
       !Name.startswith(".debug_"))
     return;
 
+  // Calculate the section offsets and size pre-compression.
+  for (BaseCommand * Cmd : SectionCommands)
+    if (auto *ISD = dyn_cast<InputSectionDescription>(Cmd))
+      for (InputSection *IS : ISD->Sections) {
+         IS->OutSecOff = alignTo(Size, IS->Alignment);
+         this->Size = IS->OutSecOff + IS->getSize();
+      }
+
   // Create a section header.
   ZDebugHeader.resize(sizeof(Elf_Chdr));
   auto *Hdr = reinterpret_cast<Elf_Chdr *>(ZDebugHeader.data());
@@ -281,7 +283,9 @@
   OutputSection *BOut = LB->getParent();
   if (AOut != BOut)
     return AOut->SectionIndex < BOut->SectionIndex;
-  return LA->OutSecOff < LB->OutSecOff;
+  assert(LA->OutSecPos && LB->OutSecPos &&
+         "Cannot compare late-inserted section positions.");
+  return LA->OutSecPos < LB->OutSecPos;
 }
 
 template <class ELFT>
Index: ELF/InputSection.h
===================================================================
--- ELF/InputSection.h
+++ ELF/InputSection.h
@@ -324,6 +324,11 @@
   // to. The writer sets a value.
   uint64_t OutSecOff = 0;
 
+  // The order in which the section was added to its output section. This is
+  // used when ordering SHF_LINK_ORDER sections. It is not set for sections
+  // inserted late, such as thunk sections.
+  llvm::Optional<size_t> OutSecPos;
+
   static bool classof(const SectionBase *S);
 
   InputSectionBase *getRelocatedSection();


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D38361.123561.patch
Type: text/x-patch
Size: 3588 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20171120/3702b550/attachment.bin>


More information about the llvm-commits mailing list