[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 13:36:28 PST 2018


mstorsjo created this revision.
mstorsjo added reviewers: ruiu, pcc, rnk.

The number of sections is used in assignAddresses (in finalizeAddresses) and the space for all sections is permanent from that point on, even if we later decide we won't write some of them.

The VirtualSize field also gets calculated in assignAddresses, so we need to manually check whether the section is empty here instead.


Repository:
  rLLD LLVM Linker

https://reviews.llvm.org/D54495

Files:
  COFF/Writer.cpp


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.173933.patch
Type: text/x-patch
Size: 863 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20181113/d043fd61/attachment-0001.bin>


More information about the llvm-commits mailing list