[PATCH] D38321: [ELF] Reset OutputSection size prior to processing linker script commands

James Henderson via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 27 09:18:25 PDT 2017


jhenderson created this revision.
Herald added a subscriber: emaste.

The size of an OutputSection is calculated early, to aid handling of compressed debug sections. However, subsequent to this point, unused synthetic sections are removed. In the event that an OutputSection, from which such an InputSection is removed, is still required (e.g. because it has a symbol assignment), and no longer has any InputSections, dot assignments, or BYTE()-family directives, the size member is never updated when processing the commands. If the removed InputSection had a non-zero size (such as a .got.plt section), the section ends up with the wrong size in the output.

The fix is to reset the OutputSection size prior to processing the linker script commands relating to that OutputSection. This ensures that the size is correct even in the above situation.


https://reviews.llvm.org/D38321

Files:
  ELF/LinkerScript.cpp
  test/ELF/linkerscript/unused-synthetic.s


Index: test/ELF/linkerscript/unused-synthetic.s
===================================================================
--- test/ELF/linkerscript/unused-synthetic.s
+++ test/ELF/linkerscript/unused-synthetic.s
@@ -13,6 +13,16 @@
 # CHECK:      .text
 # CHECK-NEXT: .dynsym
 
+# Test that the size of a removed unused synthetic input section does not get
+# added to the output section size, if the output section has symbol
+# assignments, preventing removal of the output section.
+# RUN: echo "SECTIONS { \
+# RUN:    .got.plt : { a_sym = .; *(.got.plt) } \
+# RUN:  }" > %t2.script
+# RUN: ld.lld -shared -o %t2.so --script %t2.script %t.o
+# RUN: llvm-objdump -section-headers %t2.so | FileCheck %s --check-prefix=CHECK2
+# CHECK2: .got.plt 00000000
+
 .global _start
 _start:
   nop
Index: ELF/LinkerScript.cpp
===================================================================
--- ELF/LinkerScript.cpp
+++ ELF/LinkerScript.cpp
@@ -624,6 +624,11 @@
   if (CurAddressState->OutSec->Flags & SHF_COMPRESSED)
     return;
 
+  // The Size was previously calculated for compressed debug sections. We now
+  // reset it, because unused synthetic sections may have since been removed,
+  // leaving the Size invalid.
+  Sec->Size = 0;
+
   for (BaseCommand *C : Sec->Commands)
     process(*C);
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D38321.116830.patch
Type: text/x-patch
Size: 1320 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170927/de8c51dc/attachment.bin>


More information about the llvm-commits mailing list