[lld] r337598 - [COFF] Sort .reloc before all other discardable sections

Martin Storsjo via llvm-commits llvm-commits at lists.llvm.org
Fri Jul 20 11:43:36 PDT 2018


Author: mstorsjo
Date: Fri Jul 20 11:43:35 2018
New Revision: 337598

URL: http://llvm.org/viewvc/llvm-project?rev=337598&view=rev
Log:
[COFF] Sort .reloc before all other discardable sections

If a binary is stripped, which can remove discardable sections (except
for the .reloc section, which also is marked as discardable as it isn't
loaded at runtime, only read by the loader), the .reloc section should
be first of them, in order not to create gaps in the image.

Previously, binaries with relocations were broken if they were stripped
by GNU binutils strip. Trying to execute such binaries produces an error
about "xx is not a valid win32 application".

This fixes GNU binutils bug 23348.

Prior to SVN r329370 (which didn't intend to have functional changes),
the code for moving discardable sections to the end didn't clearly
express how other discardable sections should be ordered compared to
.reloc, but the change retained the exact same end result as before.

After SVN r329370, the code (and comments) more clearly indicate that
it tries to make the .reloc section the absolutely last one; this patch
changes that.

This matches how GNU binutils ld sorts .reloc compared to dwarf debug
info sections.

Differential Revision: https://reviews.llvm.org/D49351

Signed-off-by: Martin Storsjö <martin at martin.st>

Modified:
    lld/trunk/COFF/Writer.cpp
    lld/trunk/test/COFF/sort-debug.test

Modified: lld/trunk/COFF/Writer.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/COFF/Writer.cpp?rev=337598&r1=337597&r2=337598&view=diff
==============================================================================
--- lld/trunk/COFF/Writer.cpp (original)
+++ lld/trunk/COFF/Writer.cpp Fri Jul 20 11:43:35 2018
@@ -470,12 +470,9 @@ void Writer::createSections() {
 
   // Finally, move some output sections to the end.
   auto SectionOrder = [&](OutputSection *S) {
-    // .reloc should come last of all since it refers to RVAs of data in the
-    // previous sections.
-    if (S == RelocSec)
-      return 3;
     // Move DISCARDABLE (or non-memory-mapped) sections to the end of file because
-    // the loader cannot handle holes.
+    // the loader cannot handle holes. Stripping can remove other discardable ones
+    // than .reloc, which is first of them (created early).
     if (S->Header.Characteristics & IMAGE_SCN_MEM_DISCARDABLE)
       return 2;
     // .rsrc should come at the end of the non-discardable sections because its

Modified: lld/trunk/test/COFF/sort-debug.test
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/COFF/sort-debug.test?rev=337598&r1=337597&r2=337598&view=diff
==============================================================================
--- lld/trunk/test/COFF/sort-debug.test (original)
+++ lld/trunk/test/COFF/sort-debug.test Fri Jul 20 11:43:35 2018
@@ -9,20 +9,20 @@
 # RUN: llvm-readobj -sections %t.exe | FileCheck -check-prefix=NODEBUG %s
 
 # CHECK: Name: .text
+# CHECK: Name: .reloc
 # CHECK: Name: .debug_abbrev
 # CHECK: Name: .debug_info
 # CHECK: Name: .debug_line
 # CHECK: Name: .debug_pubnames
 # CHECK: Name: .debug_pubtypes
-# CHECK: Name: .reloc
 
 # NODEBUG: Name: .text
+# NODEBUG: Name: .reloc
 # NODEBUG-NOT: Name: .debug_abbrev
 # NODEBUG-NOT: Name: .debug_info
 # NODEBUG-NOT: Name: .debug_line
 # NODEBUG-NOT: Name: .debug_pubnames
 # NODEBUG-NOT: Name: .debug_pubtypes
-# NODEBUG: Name: .reloc
 
 --- !COFF
 header:




More information about the llvm-commits mailing list