[PATCH] D33145: Clear output section from linker script command when it's removed.

Dmitry Mikulin via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon May 15 15:48:11 PDT 2017


dmikulin updated this revision to Diff 99071.
dmikulin added a comment.

Updated diff to address commands from the review and the online discussion.


https://reviews.llvm.org/D33145

Files:
  ELF/LinkerScript.cpp
  ELF/LinkerScript.h
  ELF/Writer.cpp
  test/ELF/linkerscript/arm-lscript.s


Index: test/ELF/linkerscript/arm-lscript.s
===================================================================
--- /dev/null
+++ test/ELF/linkerscript/arm-lscript.s
@@ -0,0 +1,9 @@
+// REQUIRES: arm
+// RUN: llvm-mc -filetype=obj -triple=armv7a-none-linux-gnueabi %s -o %t.o
+// RUN: echo  "SECTIONS { \
+// RUN:         .rel.dyn : {    } \
+// RUN:         .zed     : { PROVIDE_HIDDEN (foobar = .); } \
+// RUN:         }" > %t.script
+// This is a test case for PR33029. Making sure that linker can digest
+// the above script without dumping core.
+// RUN: ld.lld -emit-relocs -T %t.script %t.o -shared -o %t.so
Index: ELF/Writer.cpp
===================================================================
--- ELF/Writer.cpp
+++ ELF/Writer.cpp
@@ -1106,8 +1106,12 @@
     SS->Live = false;
     // If there are no other sections in the output section, remove it from the
     // output.
-    if (SS->OutSec->Sections.empty())
+    if (SS->OutSec->Sections.empty()) {
       V.erase(std::find(V.begin(), V.end(), SS->OutSec));
+      OutputSectionCommand *Cmd = Script->getCmd(SS->OutSec);
+      if (Cmd)
+        Cmd->Sec = nullptr;
+    }
   }
 }
 
Index: ELF/LinkerScript.h
===================================================================
--- ELF/LinkerScript.h
+++ ELF/LinkerScript.h
@@ -213,7 +213,6 @@
 
 class LinkerScript final {
   llvm::DenseMap<OutputSection *, OutputSectionCommand *> SecToCommand;
-  OutputSectionCommand *getCmd(OutputSection *Sec) const;
   void assignSymbol(SymbolAssignment *Cmd, bool InSec);
   void setDot(Expr E, const Twine &Loc, bool InSec);
 
@@ -244,6 +243,7 @@
   MemoryRegion *CurMemRegion = nullptr;
 
 public:
+  OutputSectionCommand *getCmd(OutputSection *Sec) const;
   bool hasPhdrsCommands() { return !Opt.PhdrsCommands.empty(); }
   uint64_t getDot() { return Dot; }
   OutputSection *getOutputSection(const Twine &Loc, StringRef S);
Index: ELF/LinkerScript.cpp
===================================================================
--- ELF/LinkerScript.cpp
+++ ELF/LinkerScript.cpp
@@ -687,22 +687,20 @@
   // '.' is assigned to, but creating these section should not have any bad
   // consequeces and gives us a section to put the symbol in.
   uint64_t Flags = SHF_ALLOC;
-  uint32_t Type = SHT_PROGBITS;
 
   for (int I = 0, E = Opt.Commands.size(); I != E; ++I) {
     auto *Cmd = dyn_cast<OutputSectionCommand>(Opt.Commands[I]);
     if (!Cmd)
       continue;
     if (OutputSection *Sec = Cmd->Sec) {
       Flags = Sec->Flags;
-      Type = Sec->Type;
       continue;
     }
 
     if (isAllSectionDescription(*Cmd))
       continue;
 
-    auto *OutSec = make<OutputSection>(Cmd->Name, Type, Flags);
+    auto *OutSec = make<OutputSection>(Cmd->Name, SHT_PROGBITS, Flags);
     OutSec->SectionIndex = I;
     OutputSections->push_back(OutSec);
     Cmd->Sec = OutSec;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D33145.99071.patch
Type: text/x-patch
Size: 2829 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170515/6115b0d7/attachment.bin>


More information about the llvm-commits mailing list