[lld] r244691 - COFF: Align sections to 512-byte boundaries on disk.

Rui Ueyama via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 11 16:09:01 PDT 2015


Author: ruiu
Date: Tue Aug 11 18:09:00 2015
New Revision: 244691

URL: http://llvm.org/viewvc/llvm-project?rev=244691&view=rev
Log:
COFF: Align sections to 512-byte boundaries on disk.

Sections must start at page boundaries in memory, but they
can be aligned to sector boundaries (512-bytes) on disk.
We aligned them to 4096-byte boundaries even on disk, so we
wasted disk space a bit.

Modified:
    lld/trunk/COFF/Writer.cpp
    lld/trunk/test/COFF/baserel.test
    lld/trunk/test/COFF/hello32.test

Modified: lld/trunk/COFF/Writer.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/COFF/Writer.cpp?rev=244691&r1=244690&r2=244691&view=diff
==============================================================================
--- lld/trunk/COFF/Writer.cpp (original)
+++ lld/trunk/COFF/Writer.cpp Tue Aug 11 18:09:00 2015
@@ -37,8 +37,7 @@ using namespace lld;
 using namespace lld::coff;
 
 static const int PageSize = 4096;
-static const int FileAlignment = 512;
-static const int SectionAlignment = 4096;
+static const int SectorSize = 512;
 static const int DOSStubSize = 64;
 static const int NumberfOfDataDirectory = 16;
 
@@ -174,7 +173,7 @@ void OutputSection::addChunk(Chunk *C) {
   Off += C->getSize();
   Header.VirtualSize = Off;
   if (C->hasData())
-    Header.SizeOfRawData = RoundUpToAlignment(Off, FileAlignment);
+    Header.SizeOfRawData = RoundUpToAlignment(Off, SectorSize);
 }
 
 void OutputSection::addPermissions(uint32_t C) {
@@ -507,15 +506,14 @@ void Writer::createSymbolAndStringTable(
   // We position the symbol table to be adjacent to the end of the last section.
   uint64_t FileOff =
       LastSection->getFileOff() +
-      RoundUpToAlignment(LastSection->getRawSize(), FileAlignment);
+      RoundUpToAlignment(LastSection->getRawSize(), SectorSize);
   if (!OutputSymtab.empty()) {
     PointerToSymbolTable = FileOff;
     FileOff += OutputSymtab.size() * sizeof(coff_symbol16);
   }
   if (!Strtab.empty())
     FileOff += Strtab.size() + 4;
-  FileSize = SizeOfHeaders +
-             RoundUpToAlignment(FileOff - SizeOfHeaders, FileAlignment);
+  FileSize = RoundUpToAlignment(FileOff, SectorSize);
 }
 
 // Visits all sections to assign incremental, non-overlapping RVAs and
@@ -526,9 +524,9 @@ void Writer::assignAddresses() {
                   sizeof(coff_section) * OutputSections.size();
   SizeOfHeaders +=
       Config->is64() ? sizeof(pe32plus_header) : sizeof(pe32_header);
-  SizeOfHeaders = RoundUpToAlignment(SizeOfHeaders, PageSize);
+  SizeOfHeaders = RoundUpToAlignment(SizeOfHeaders, SectorSize);
   uint64_t RVA = 0x1000; // The first page is kept unmapped.
-  uint64_t FileOff = SizeOfHeaders;
+  FileSize = SizeOfHeaders;
   // Move DISCARDABLE (or non-memory-mapped) sections to the end of file because
   // the loader cannot handle holes.
   std::stable_partition(
@@ -539,13 +537,11 @@ void Writer::assignAddresses() {
     if (Sec->getName() == ".reloc")
       addBaserels(Sec);
     Sec->setRVA(RVA);
-    Sec->setFileOffset(FileOff);
+    Sec->setFileOffset(FileSize);
     RVA += RoundUpToAlignment(Sec->getVirtualSize(), PageSize);
-    FileOff += RoundUpToAlignment(Sec->getRawSize(), FileAlignment);
+    FileSize += RoundUpToAlignment(Sec->getRawSize(), SectorSize);
   }
   SizeOfImage = SizeOfHeaders + RoundUpToAlignment(RVA - 0x1000, PageSize);
-  FileSize = SizeOfHeaders +
-             RoundUpToAlignment(FileOff - SizeOfHeaders, FileAlignment);
 }
 
 template <typename PEHeaderTy> void Writer::writeHeader() {
@@ -584,8 +580,8 @@ template <typename PEHeaderTy> void Writ
   Buf += sizeof(*PE);
   PE->Magic = Config->is64() ? PE32Header::PE32_PLUS : PE32Header::PE32;
   PE->ImageBase = Config->ImageBase;
-  PE->SectionAlignment = SectionAlignment;
-  PE->FileAlignment = FileAlignment;
+  PE->SectionAlignment = PageSize;
+  PE->FileAlignment = SectorSize;
   PE->MajorImageVersion = Config->MajorImageVersion;
   PE->MinorImageVersion = Config->MinorImageVersion;
   PE->MajorOperatingSystemVersion = Config->MajorOSVersion;

Modified: lld/trunk/test/COFF/baserel.test
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/COFF/baserel.test?rev=244691&r1=244690&r2=244691&view=diff
==============================================================================
--- lld/trunk/test/COFF/baserel.test (original)
+++ lld/trunk/test/COFF/baserel.test Tue Aug 11 18:09:00 2015
@@ -61,7 +61,7 @@
 # BASEREL-HEADER-NEXT: VirtualSize: 0x20
 # BASEREL-HEADER-NEXT: VirtualAddress: 0x5000
 # BASEREL-HEADER-NEXT: RawDataSize: 512
-# BASEREL-HEADER-NEXT: PointerToRawData: 0x1800
+# BASEREL-HEADER-NEXT: PointerToRawData: 0xC00
 # BASEREL-HEADER-NEXT: PointerToRelocations: 0x0
 # BASEREL-HEADER-NEXT: PointerToLineNumbers: 0x0
 # BASEREL-HEADER-NEXT: RelocationCount: 0

Modified: lld/trunk/test/COFF/hello32.test
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/COFF/hello32.test?rev=244691&r1=244690&r2=244691&view=diff
==============================================================================
--- lld/trunk/test/COFF/hello32.test (original)
+++ lld/trunk/test/COFF/hello32.test Tue Aug 11 18:09:00 2015
@@ -38,8 +38,8 @@ HEADER-NEXT:   MajorImageVersion: 0
 HEADER-NEXT:   MinorImageVersion: 0
 HEADER-NEXT:   MajorSubsystemVersion: 6
 HEADER-NEXT:   MinorSubsystemVersion: 0
-HEADER-NEXT:   SizeOfImage: 20480
-HEADER-NEXT:   SizeOfHeaders: 4096
+HEADER-NEXT:   SizeOfImage: 16896
+HEADER-NEXT:   SizeOfHeaders: 512
 HEADER-NEXT:   Subsystem: IMAGE_SUBSYSTEM_WINDOWS_CUI (0x3)
 HEADER-NEXT:   Characteristics [ (0x8140)
 HEADER-NEXT:     IMAGE_DLL_CHARACTERISTICS_DYNAMIC_BASE (0x40)




More information about the llvm-commits mailing list