[PATCH] D54495: [LLD] [COFF] Remove empty sections before calculating the size of section headers

Martin Storsjö via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Nov 13 23:54:47 PST 2018


mstorsjo updated this revision to Diff 173989.
mstorsjo added a comment.

Added a testcase based on @pcc's suggestion.


https://reviews.llvm.org/D54495

Files:
  COFF/Writer.cpp
  test/COFF/header-size.s


Index: test/COFF/header-size.s
===================================================================
--- /dev/null
+++ test/COFF/header-size.s
@@ -0,0 +1,12 @@
+// REQUIRES: x86
+// RUN: llvm-mc -filetype=obj -triple=x86_64-windows %s -o %t.obj
+// RUN: lld-link -entry:main -subsystem:console %t.obj -out:%t.exe
+// RUN: llvm-readobj -sections %t.exe | FileCheck %s
+    .globl main
+main:
+    ret
+
+// Check that the first section data comes at 512 bytes in the file.
+// If the size allocated for headers would include size for section
+// headers which aren't written, PointerToRawData would be 0x400 instead.
+// CHECK: PointerToRawData: 0x200
Index: COFF/Writer.cpp
===================================================================
--- COFF/Writer.cpp
+++ COFF/Writer.cpp
@@ -500,8 +500,8 @@
   createExportTable();
   mergeSections();
   readRelocTargets();
-  finalizeAddresses();
   removeEmptySections();
+  finalizeAddresses();
   setSectionPermissions();
   createSymbolAndStringTable();
 
@@ -885,7 +885,12 @@
 // The Windows loader doesn't seem to like empty sections,
 // so we remove them if any.
 void Writer::removeEmptySections() {
-  auto IsEmpty = [](OutputSection *S) { return S->getVirtualSize() == 0; };
+  auto IsEmpty = [](OutputSection *S) {
+    for (Chunk *C : S->Chunks)
+      if (C->getSize() > 0)
+        return false;
+    return true;
+  };
   OutputSections.erase(
       std::remove_if(OutputSections.begin(), OutputSections.end(), IsEmpty),
       OutputSections.end());


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D54495.173989.patch
Type: text/x-patch
Size: 1512 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20181114/75ec62b3/attachment.bin>


More information about the llvm-commits mailing list