[lld] r326430 - [ELF] - Do not remove empty sections that use symbols in expressions.

George Rimar via llvm-commits llvm-commits at lists.llvm.org
Thu Mar 1 04:27:05 PST 2018


Author: grimar
Date: Thu Mar  1 04:27:04 2018
New Revision: 326430

URL: http://llvm.org/viewvc/llvm-project?rev=326430&view=rev
Log:
[ELF] - Do not remove empty sections that use symbols in expressions.

This is PR36515.

Currenly if we have a script like .debug_info 0 : { *(.debug_info) },
we would not remove this section and keep it in the output.
That does not work, because it is common case for
debug sections to have a zero address expression.
Patch changes behavior so that we remove only sections
that do not use symbols in its expressions.

Differential revision: https://reviews.llvm.org/D43863

Added:
    lld/trunk/test/ELF/linkerscript/empty-sections-expressions.s
Modified:
    lld/trunk/ELF/LinkerScript.cpp
    lld/trunk/ELF/OutputSections.h
    lld/trunk/ELF/ScriptParser.cpp

Modified: lld/trunk/ELF/LinkerScript.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/LinkerScript.cpp?rev=326430&r1=326429&r2=326430&view=diff
==============================================================================
--- lld/trunk/ELF/LinkerScript.cpp (original)
+++ lld/trunk/ELF/LinkerScript.cpp Thu Mar  1 04:27:04 2018
@@ -778,11 +778,10 @@ static bool isDiscardable(OutputSection
   if (!Sec.Phdrs.empty())
     return false;
 
-  // We do not want to remove sections that have custom address or align
-  // expressions set even if them are empty. We keep them because we
-  // want to be sure that any expressions can be evaluated and report
-  // an error otherwise.
-  if (Sec.AddrExpr || Sec.AlignExpr || Sec.LMAExpr)
+  // We do not want to remove sections that reference symbols in address and
+  // other expressions. We add script symbols as undefined, and want to ensure
+  // all of them are defined in the output, hence have to keep them.
+  if (Sec.ExpressionsUseSymbols)
     return false;
 
   for (BaseCommand *Base : Sec.SectionCommands)

Modified: lld/trunk/ELF/OutputSections.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/OutputSections.h?rev=326430&r1=326429&r2=326430&view=diff
==============================================================================
--- lld/trunk/ELF/OutputSections.h (original)
+++ lld/trunk/ELF/OutputSections.h Thu Mar  1 04:27:04 2018
@@ -101,6 +101,7 @@ public:
   std::string LMARegionName;
   bool NonAlloc = false;
   bool Noload = false;
+  bool ExpressionsUseSymbols = false;
 
   template <class ELFT> void finalize();
   template <class ELFT> void writeTo(uint8_t *Buf);

Modified: lld/trunk/ELF/ScriptParser.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/ScriptParser.cpp?rev=326430&r1=326429&r2=326430&view=diff
==============================================================================
--- lld/trunk/ELF/ScriptParser.cpp (original)
+++ lld/trunk/ELF/ScriptParser.cpp Thu Mar  1 04:27:04 2018
@@ -671,6 +671,8 @@ OutputSection *ScriptParser::readOutputS
   OutputSection *Cmd =
       Script->createOutputSection(OutSec, getCurrentLocation());
 
+  size_t SymbolsReferenced = Script->ReferencedSymbols.size();
+
   if (peek() != ":")
     readSectionAddressType(Cmd);
   expect(":");
@@ -737,6 +739,8 @@ OutputSection *ScriptParser::readOutputS
   // Consume optional comma following output section command.
   consume(",");
 
+  if (Script->ReferencedSymbols.size() > SymbolsReferenced)
+    Cmd->ExpressionsUseSymbols = true;
   return Cmd;
 }
 

Added: lld/trunk/test/ELF/linkerscript/empty-sections-expressions.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/linkerscript/empty-sections-expressions.s?rev=326430&view=auto
==============================================================================
--- lld/trunk/test/ELF/linkerscript/empty-sections-expressions.s (added)
+++ lld/trunk/test/ELF/linkerscript/empty-sections-expressions.s Thu Mar  1 04:27:04 2018
@@ -0,0 +1,18 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o
+
+## We remove empty sections that do not reference symbols in address,
+## LMA, align and subalign expressions. Here we check that.
+
+# RUN: echo "SECTIONS { .debug_info 0 : { *(.debug_info) } }" > %t.script
+# RUN: ld.lld -o %t --script %t.script %t.o
+# RUN: llvm-objdump -section-headers %t | FileCheck %s
+# CHECK-NOT: .debug_info
+
+# RUN: echo "SECTIONS { .debug_info foo : { *(.debug_info) } }" > %t2.script
+# RUN: ld.lld -o %t2 --script %t2.script %t.o
+# RUN: llvm-objdump -section-headers %t2 | FileCheck %s --check-prefix=SEC
+# SEC: .debug_info
+
+.globl foo
+foo:




More information about the llvm-commits mailing list