[PATCH] D49351: [LLD] [COFF] Sort .relocs before all other discardable sections

Martin Storsjö via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun Jul 15 13:38:17 PDT 2018


mstorsjo created this revision.
mstorsjo added reviewers: ruiu, pcc.
Herald added subscribers: JDevlieghere, aprantl.

If a binary is stripped, which can remove discardable sections (except for the .relocs section, which also is marked as discardable as it isn't loaded at runtime, only read by the loader), the .relocs 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 .relocs, even though the change probably retained the same end result as before.

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

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


Repository:
  rLLD LLVM Linker

https://reviews.llvm.org/D49351

Files:
  COFF/Writer.cpp
  test/COFF/sort-debug.test


Index: test/COFF/sort-debug.test
===================================================================
--- test/COFF/sort-debug.test
+++ test/COFF/sort-debug.test
@@ -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:
Index: COFF/Writer.cpp
===================================================================
--- COFF/Writer.cpp
+++ COFF/Writer.cpp
@@ -469,13 +469,14 @@
   // 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.
+    // previous sections, but before all other discardble sections which may
+    // later be removed by strip.
     if (S == RelocSec)
-      return 3;
+      return 2;
     // Move DISCARDABLE (or non-memory-mapped) sections to the end of file because
     // the loader cannot handle holes.
     if (S->Header.Characteristics & IMAGE_SCN_MEM_DISCARDABLE)
-      return 2;
+      return 3;
     // .rsrc should come at the end of the non-discardable sections because its
     // size may change by the Win32 UpdateResources() function, causing
     // subsequent sections to move (see https://crbug.com/827082).


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D49351.155595.patch
Type: text/x-patch
Size: 1725 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180715/f53abd4b/attachment.bin>


More information about the llvm-commits mailing list