diff options
author | Martin Storsjo <martin@martin.st> | 2018-11-27 20:48:09 +0000 |
---|---|---|
committer | Martin Storsjo <martin@martin.st> | 2018-11-27 20:48:09 +0000 |
commit | 333e0d180f34e665aa68776416b4ccb4d9153309 (patch) | |
tree | 642b92f4f265abe284d0234528ed98e79695b515 | |
parent | 5db3d33938f6268bd7a1bdbbe0525cb28d77acf8 (diff) | |
download | bcm5719-llvm-333e0d180f34e665aa68776416b4ccb4d9153309.tar.gz bcm5719-llvm-333e0d180f34e665aa68776416b4ccb4d9153309.zip |
[COFF] Remove empty sections before calculating the size of section headers
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.
Differential Revision: https://reviews.llvm.org/D54495
llvm-svn: 347704
-rw-r--r-- | lld/COFF/Writer.cpp | 17 | ||||
-rw-r--r-- | lld/test/COFF/header-size.s | 12 |
2 files changed, 29 insertions, 0 deletions
diff --git a/lld/COFF/Writer.cpp b/lld/COFF/Writer.cpp index e06af1240ef..258796ea605 100644 --- a/lld/COFF/Writer.cpp +++ b/lld/COFF/Writer.cpp @@ -173,6 +173,7 @@ private: void createExportTable(); void mergeSections(); void readRelocTargets(); + void removeUnusedSections(); void assignAddresses(); void finalizeAddresses(); void removeEmptySections(); @@ -500,6 +501,7 @@ void Writer::run() { createExportTable(); mergeSections(); readRelocTargets(); + removeUnusedSections(); finalizeAddresses(); removeEmptySections(); setSectionPermissions(); @@ -882,6 +884,21 @@ void Writer::createExportTable() { EdataSec->addChunk(C); } +void Writer::removeUnusedSections() { + // Remove sections that we can be sure won't get content, to avoid + // allocating space for their section headers. + auto IsUnused = [this](OutputSection *S) { + if (S == RelocSec) + return false; // This section is populated later. + // MergeChunks have zero size at this point, as their size is finalized + // later. Only remove sections that have no Chunks at all. + return S->Chunks.empty(); + }; + OutputSections.erase( + std::remove_if(OutputSections.begin(), OutputSections.end(), IsUnused), + OutputSections.end()); +} + // The Windows loader doesn't seem to like empty sections, // so we remove them if any. void Writer::removeEmptySections() { diff --git a/lld/test/COFF/header-size.s b/lld/test/COFF/header-size.s new file mode 100644 index 00000000000..3373cdea84f --- /dev/null +++ b/lld/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 |