[PATCH] D24762: Alternative fix for empty sections

Rafael Ávila de Espíndola via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 20 06:42:59 PDT 2016


rafael created this revision.
rafael added reviewers: evgeny777, ruiu.
rafael added a subscriber: llvm-commits.

This is an alternative to D24230.


https://reviews.llvm.org/D24762

Files:
  ELF/LinkerScript.cpp
  ELF/LinkerScript.h
  ELF/Writer.cpp
  test/ELF/linkerscript/symbol-only.s

Index: test/ELF/linkerscript/symbol-only.s
===================================================================
--- /dev/null
+++ test/ELF/linkerscript/symbol-only.s
@@ -0,0 +1,18 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t
+
+# RUN: echo "SECTIONS { \
+# RUN:  abc : { foo = .; } \
+# RUN:  . = ALIGN(0x1000); \
+# RUN:  .text : { *(.text) } \
+# RUN: }" > %t.script
+# RUN: ld.lld -o %t1 --script %t.script %t -shared
+# RUN: llvm-objdump -section-headers -t %t1 | FileCheck %s
+# CHECK:      Sections:
+# CHECK-NEXT: Idx Name          Size      Address
+# CHECK-NEXT:   0               00000000 0000000000000000
+# CHECK-NEXT:   1 abc           00000000 0000000000000200
+# CHECK-NEXT:   2 .text         00000000 0000000000001000 TEXT DATA
+
+# CHECK: SYMBOL TABLE:
+# CHECK:     0000000000000200         abc                00000000 foo
Index: ELF/Writer.cpp
===================================================================
--- ELF/Writer.cpp
+++ ELF/Writer.cpp
@@ -777,6 +777,9 @@
   // This function adds linker-created Out<ELFT>::* sections.
   addPredefinedSections();
 
+  if (ScriptConfig->HasSections)
+    Script<ELFT>::X->adjustSectionsBeforeSorting();
+
   std::stable_sort(OutputSections.begin(), OutputSections.end(),
                    compareSections<ELFT>);
 
Index: ELF/LinkerScript.h
===================================================================
--- ELF/LinkerScript.h
+++ ELF/LinkerScript.h
@@ -184,6 +184,7 @@
   ~LinkerScript();
   void processCommands(OutputSectionFactory<ELFT> &Factory);
   void createSections(OutputSectionFactory<ELFT> &Factory);
+  void adjustSectionsBeforeSorting();
 
   std::vector<PhdrEntry<ELFT>> createPhdrs();
   bool ignoreInterpSection();
Index: ELF/LinkerScript.cpp
===================================================================
--- ELF/LinkerScript.cpp
+++ ELF/LinkerScript.cpp
@@ -445,7 +445,7 @@
                 [this](std::unique_ptr<BaseCommand> &B) { process(*B.get()); });
 }
 
-template <class ELFT> void LinkerScript<ELFT>::assignAddresses() {
+template <class ELFT> void LinkerScript<ELFT>::adjustSectionsBeforeSorting() {
   // 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
@@ -469,6 +469,29 @@
       });
   Opt.Commands.erase(Pos, Opt.Commands.end());
 
+  // If the output section contains only symbol assignments, create a
+  // corresponding output section. The bfd linker seems to only create them if
+  // '.' is assigned to, but creating these section should not have any bad
+  // consequeces and gives us a section to put the symbol in.
+  uintX_t Flags = SHF_ALLOC;
+  for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) {
+    auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get());
+    if (!Cmd)
+      continue;
+    std::vector<OutputSectionBase<ELFT> *> Secs =
+        findSections(*Cmd, *OutputSections);
+    if (!Secs.empty()) {
+      Flags = Secs[0]->getFlags();
+      continue;
+    }
+
+    auto *OutSec = new OutputSection<ELFT>(Cmd->Name, /*Type*/ 0, Flags);
+    Out<ELFT>::Pool.emplace_back(OutSec);
+    OutputSections->push_back(OutSec);
+  }
+}
+
+template <class ELFT> void LinkerScript<ELFT>::assignAddresses() {
   // Orphan sections are sections present in the input files which
   // are not explicitly placed into the output file by the linker script.
   // We place orphan sections at end of file.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D24762.71924.patch
Type: text/x-patch
Size: 3544 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160920/d4862b74/attachment.bin>


More information about the llvm-commits mailing list