[lld] r282159 - Handle empty sections with symbol assignments.

Rafael Espindola via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 22 07:40:51 PDT 2016


Author: rafael
Date: Thu Sep 22 09:40:50 2016
New Revision: 282159

URL: http://llvm.org/viewvc/llvm-project?rev=282159&view=rev
Log:
Handle empty sections with symbol assignments.

Before the symbols were becoming undefined.

Added:
    lld/trunk/test/ELF/linkerscript/extend-pt-load.s
    lld/trunk/test/ELF/linkerscript/symbol-only.s
Modified:
    lld/trunk/ELF/LinkerScript.cpp
    lld/trunk/ELF/LinkerScript.h
    lld/trunk/ELF/Writer.cpp

Modified: lld/trunk/ELF/LinkerScript.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/LinkerScript.cpp?rev=282159&r1=282158&r2=282159&view=diff
==============================================================================
--- lld/trunk/ELF/LinkerScript.cpp (original)
+++ lld/trunk/ELF/LinkerScript.cpp Thu Sep 22 09:40:50 2016
@@ -466,7 +466,7 @@ void LinkerScript<ELFT>::assignOffsets(O
                 [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
@@ -490,6 +490,31 @@ template <class ELFT> void LinkerScript<
       });
   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;
+  uint32_t Type = 0;
+  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();
+      Type = Secs[0]->getType();
+      continue;
+    }
+
+    auto *OutSec = new OutputSection<ELFT>(Cmd->Name, Type, 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.

Modified: lld/trunk/ELF/LinkerScript.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/LinkerScript.h?rev=282159&r1=282158&r2=282159&view=diff
==============================================================================
--- lld/trunk/ELF/LinkerScript.h (original)
+++ lld/trunk/ELF/LinkerScript.h Thu Sep 22 09:40:50 2016
@@ -187,6 +187,7 @@ public:
   ~LinkerScript();
   void processCommands(OutputSectionFactory<ELFT> &Factory);
   void createSections(OutputSectionFactory<ELFT> &Factory);
+  void adjustSectionsBeforeSorting();
 
   std::vector<PhdrEntry<ELFT>> createPhdrs();
   bool ignoreInterpSection();

Modified: lld/trunk/ELF/Writer.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Writer.cpp?rev=282159&r1=282158&r2=282159&view=diff
==============================================================================
--- lld/trunk/ELF/Writer.cpp (original)
+++ lld/trunk/ELF/Writer.cpp Thu Sep 22 09:40:50 2016
@@ -723,6 +723,7 @@ template <class ELFT> void Writer<ELFT>:
                      compareSectionsNonScript<ELFT>);
     return;
   }
+  Script<ELFT>::X->adjustSectionsBeforeSorting();
 
   // The order of the sections in the script is arbitrary and may not agree with
   // compareSectionsNonScript. This means that we cannot easily define a

Added: lld/trunk/test/ELF/linkerscript/extend-pt-load.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/linkerscript/extend-pt-load.s?rev=282159&view=auto
==============================================================================
--- lld/trunk/test/ELF/linkerscript/extend-pt-load.s (added)
+++ lld/trunk/test/ELF/linkerscript/extend-pt-load.s Thu Sep 22 09:40:50 2016
@@ -0,0 +1,57 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o
+
+# This test demonstrates an odd consequence of the way we handle sections with just symbol
+# assignments.
+
+# First, run a test with no such section.
+
+# RUN: echo "SECTIONS { \
+# RUN:  .text : { *(.text) } \
+# RUN:  . = ALIGN(0x1000); \
+# RUN:  .data.rel.ro : { *(.data.rel.ro) } \
+# RUN: }" > %t.script
+# RUN: ld.lld -o %t1 --script %t.script %t.o -shared
+# RUN: llvm-readobj --elf-output-style=GNU -l -s %t1 | FileCheck --check-prefix=CHECK1 %s
+
+# CHECK1:      .text        PROGBITS 00000000000001bc 0001bc 000001 00 AX
+# CHECK1-NEXT: .data.rel.ro PROGBITS 0000000000001000 001000 000001 00 WA
+
+# CHECK1:      LOAD 0x000000 0x0000000000000000 0x0000000000000000 0x0001bd 0x0001bd R E
+# CHECK1-NEXT: LOAD 0x001000 0x0000000000001000 0x0000000000001000 0x000068 0x000068 RW
+
+# Then add the section bar. Note how bar is given AX flags, which causes the PT_LOAD to now
+# cover the padding bits created by ALIGN.
+
+# RUN: echo "SECTIONS { \
+# RUN:  .text : { *(.text) } \
+# RUN:  . = ALIGN(0x1000); \
+# RUN:  bar : { HIDDEN(bar_sym = .); } \
+# RUN:  .data.rel.ro : { *(.data.rel.ro) } \
+# RUN: }" > %t.script
+# RUN: ld.lld -o %t2 --script %t.script %t.o -shared
+# RUN: llvm-readobj --elf-output-style=GNU -l -s %t2 | FileCheck --check-prefix=CHECK2 %s
+
+# CHECK2:      .text        PROGBITS 00000000000001bc 0001bc 000001 00 AX
+# CHECK2-NEXT: bar          PROGBITS 0000000000001000 001000 000000 00 AX
+# CHECK2-NEXT: .data.rel.ro PROGBITS 0000000000001000 001000 000001 00 WA
+
+# CHECK2:      LOAD 0x000000 0x0000000000000000 0x0000000000000000 0x001000 0x001000 R E
+# CHECK2-NEXT: LOAD 0x001000 0x0000000000001000 0x0000000000001000 0x000068 0x000068 RW
+
+# If the current behavior becomes a problem we should consider just moving the commands out
+# of the section. That is, handle the above like the following test.
+
+# RUN: echo "SECTIONS { \
+# RUN:  .text : { *(.text) } \
+# RUN:  . = ALIGN(0x1000); \
+# RUN:  HIDDEN(bar_sym = .); \
+# RUN:  .data.rel.ro : { *(.data.rel.ro) } \
+# RUN: }" > %t.script
+# RUN: ld.lld -o %t3 --script %t.script %t.o -shared
+# RUN: llvm-readobj --elf-output-style=GNU -l -s %t3 | FileCheck --check-prefix=CHECK1 %s
+
+nop
+
+.section .data.rel.ro, "aw"
+.byte 0

Added: lld/trunk/test/ELF/linkerscript/symbol-only.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/linkerscript/symbol-only.s?rev=282159&view=auto
==============================================================================
--- lld/trunk/test/ELF/linkerscript/symbol-only.s (added)
+++ lld/trunk/test/ELF/linkerscript/symbol-only.s Thu Sep 22 09:40:50 2016
@@ -0,0 +1,20 @@
+# 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:  bar : { *(bar) } \
+# 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 [[ADDR:[0-9a-f]*]]
+# CHECK-NEXT:   2 bar           00000000 0000000000001000 DATA
+
+# CHECK: SYMBOL TABLE:
+# CHECK:     [[ADDR]]         abc                00000000 foo
+
+.section bar, "a"




More information about the llvm-commits mailing list