[PATCH] D44376: [ELF] - Drop special flags for empty output sections.

George Rimar via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Mar 12 03:51:24 PDT 2018


grimar created this revision.
grimar added reviewers: ruiu, espindola.
Herald added subscribers: arichardson, emaste.

This fixes PR36598.

LLD currently crashes when we have empty output section
with SHF_LINK_ORDER  flag. This might happen if we place an empty
synthetic section in the linker script, but keep output section alive with the
use of additional symbol, for example.

The patch fixes the issue by dropping all special flags for empty sections.


https://reviews.llvm.org/D44376

Files:
  ELF/LinkerScript.cpp
  test/ELF/linkerscript/empty-link-order.test


Index: test/ELF/linkerscript/empty-link-order.test
===================================================================
--- test/ELF/linkerscript/empty-link-order.test
+++ test/ELF/linkerscript/empty-link-order.test
@@ -0,0 +1,10 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=arm-arm-none-eabi -o %t.o < /dev/null
+
+SECTIONS {
+  .foo : {
+    bar = .;
+    *(.ARM.exidx*)
+  }
+}
+# RUN: ld.lld %t.o -o %t --script %s
Index: ELF/LinkerScript.cpp
===================================================================
--- ELF/LinkerScript.cpp
+++ ELF/LinkerScript.cpp
@@ -813,7 +813,7 @@
   for (BaseCommand *Base : Sec.SectionCommands)
     if (!isa<InputSectionDescription>(*Base))
       return false;
-  return getInputSections(&Sec).empty();
+  return true;
 }
 
 void LinkerScript::adjustSectionsBeforeSorting() {
@@ -839,32 +839,34 @@
   // the previous sections. Only a few flags are needed to keep the impact low.
   uint64_t Flags = SHF_ALLOC;
 
-  for (BaseCommand *&Cmd : SectionCommands) {
+  for (BaseCommand *Cmd : SectionCommands) {
     auto *Sec = dyn_cast<OutputSection>(Cmd);
     if (!Sec)
       continue;
 
     // A live output section means that some input section was added to it. It
-    // might have been removed (gc, or empty synthetic section), but we at least
+    // might have been removed (if it was empty synthetic section), but we at least
     // know the flags.
     if (Sec->Live)
       Flags = Sec->Flags & (SHF_ALLOC | SHF_WRITE | SHF_EXECINSTR);
-    else
+
+    // We do not want to keep any special flags for output section in case it is empty.
+    bool IsEmpty = getInputSections(Sec).empty();
+    if (!Sec->Live || IsEmpty)
       Sec->Flags = Flags;
 
-    if (isDiscardable(*Sec)) {
-      Sec->Live = false;
-      Cmd = nullptr;
-    }
+    Sec->Live = !IsEmpty || !isDiscardable(*Sec);
   }
 
   // It is common practice to use very generic linker scripts. So for any
   // given run some of the output sections in the script will be empty.
   // We could create corresponding empty output sections, but that would
   // clutter the output.
   // We instead remove trivially empty sections. The bfd linker seems even
   // more aggressive at removing them.
-  llvm::erase_if(SectionCommands, [&](BaseCommand *Base) { return !Base; });
+  llvm::erase_if(SectionCommands, [&](BaseCommand *Base) {
+    return isa<OutputSection>(Base) && !cast<OutputSection>(Base)->Live;
+  });
 }
 
 void LinkerScript::adjustSectionsAfterSorting() {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D44376.137981.patch
Type: text/x-patch
Size: 2496 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180312/735ed090/attachment.bin>


More information about the llvm-commits mailing list