<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Aug 11, 2015 at 4:09 PM, Rui Ueyama via llvm-commits <span dir="ltr"><<a href="mailto:llvm-commits@lists.llvm.org" target="_blank">llvm-commits@lists.llvm.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Author: ruiu<br>
Date: Tue Aug 11 18:09:00 2015<br>
New Revision: 244691<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=244691&view=rev" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project?rev=244691&view=rev</a><br>
Log:<br>
COFF: Align sections to 512-byte boundaries on disk.<br>
<br>
Sections must start at page boundaries in memory, but they<br>
can be aligned to sector boundaries (512-bytes) on disk.<br>
We aligned them to 4096-byte boundaries even on disk, so we<br>
wasted disk space a bit.<br></blockquote><div><br></div><div>This will likely force the kernel to copy or otherwise do unnecessary work when loading. Are you sure that isn't happening? The kernel ideally wants to just create a couple page table entries. But if it needs to move things around at <4K granularity to make them properly aligned to their load address when loading (like this patch I think causes) then it will need to do copies.</div><div><br></div><div>This can likely be checked by looking for an increase in real memory usage for the system when the new binaries are loaded (vs. the old page-aligned ones), since the kernel will have a copy sitting in page cache and a copy for alignment mapped into the process address space; alternatively, you can check for the slowdown from the kernel copies when faulting the memory into the process's address space (or (less likely) it may do the copies eagerly which should be easy to measure too).</div><div><br></div><div>-- Sean Silva</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
Modified:<br>
    lld/trunk/COFF/Writer.cpp<br>
    lld/trunk/test/COFF/baserel.test<br>
    lld/trunk/test/COFF/hello32.test<br>
<br>
Modified: lld/trunk/COFF/Writer.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/lld/trunk/COFF/Writer.cpp?rev=244691&r1=244690&r2=244691&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/lld/trunk/COFF/Writer.cpp?rev=244691&r1=244690&r2=244691&view=diff</a><br>
==============================================================================<br>
--- lld/trunk/COFF/Writer.cpp (original)<br>
+++ lld/trunk/COFF/Writer.cpp Tue Aug 11 18:09:00 2015<br>
@@ -37,8 +37,7 @@ using namespace lld;<br>
 using namespace lld::coff;<br>
<br>
 static const int PageSize = 4096;<br>
-static const int FileAlignment = 512;<br>
-static const int SectionAlignment = 4096;<br>
+static const int SectorSize = 512;<br>
 static const int DOSStubSize = 64;<br>
 static const int NumberfOfDataDirectory = 16;<br>
<br>
@@ -174,7 +173,7 @@ void OutputSection::addChunk(Chunk *C) {<br>
   Off += C->getSize();<br>
   Header.VirtualSize = Off;<br>
   if (C->hasData())<br>
-    Header.SizeOfRawData = RoundUpToAlignment(Off, FileAlignment);<br>
+    Header.SizeOfRawData = RoundUpToAlignment(Off, SectorSize);<br>
 }<br>
<br>
 void OutputSection::addPermissions(uint32_t C) {<br>
@@ -507,15 +506,14 @@ void Writer::createSymbolAndStringTable(<br>
   // We position the symbol table to be adjacent to the end of the last section.<br>
   uint64_t FileOff =<br>
       LastSection->getFileOff() +<br>
-      RoundUpToAlignment(LastSection->getRawSize(), FileAlignment);<br>
+      RoundUpToAlignment(LastSection->getRawSize(), SectorSize);<br>
   if (!OutputSymtab.empty()) {<br>
     PointerToSymbolTable = FileOff;<br>
     FileOff += OutputSymtab.size() * sizeof(coff_symbol16);<br>
   }<br>
   if (!Strtab.empty())<br>
     FileOff += Strtab.size() + 4;<br>
-  FileSize = SizeOfHeaders +<br>
-             RoundUpToAlignment(FileOff - SizeOfHeaders, FileAlignment);<br>
+  FileSize = RoundUpToAlignment(FileOff, SectorSize);<br>
 }<br>
<br>
 // Visits all sections to assign incremental, non-overlapping RVAs and<br>
@@ -526,9 +524,9 @@ void Writer::assignAddresses() {<br>
                   sizeof(coff_section) * OutputSections.size();<br>
   SizeOfHeaders +=<br>
       Config->is64() ? sizeof(pe32plus_header) : sizeof(pe32_header);<br>
-  SizeOfHeaders = RoundUpToAlignment(SizeOfHeaders, PageSize);<br>
+  SizeOfHeaders = RoundUpToAlignment(SizeOfHeaders, SectorSize);<br>
   uint64_t RVA = 0x1000; // The first page is kept unmapped.<br>
-  uint64_t FileOff = SizeOfHeaders;<br>
+  FileSize = SizeOfHeaders;<br>
   // Move DISCARDABLE (or non-memory-mapped) sections to the end of file because<br>
   // the loader cannot handle holes.<br>
   std::stable_partition(<br>
@@ -539,13 +537,11 @@ void Writer::assignAddresses() {<br>
     if (Sec->getName() == ".reloc")<br>
       addBaserels(Sec);<br>
     Sec->setRVA(RVA);<br>
-    Sec->setFileOffset(FileOff);<br>
+    Sec->setFileOffset(FileSize);<br>
     RVA += RoundUpToAlignment(Sec->getVirtualSize(), PageSize);<br>
-    FileOff += RoundUpToAlignment(Sec->getRawSize(), FileAlignment);<br>
+    FileSize += RoundUpToAlignment(Sec->getRawSize(), SectorSize);<br>
   }<br>
   SizeOfImage = SizeOfHeaders + RoundUpToAlignment(RVA - 0x1000, PageSize);<br>
-  FileSize = SizeOfHeaders +<br>
-             RoundUpToAlignment(FileOff - SizeOfHeaders, FileAlignment);<br>
 }<br>
<br>
 template <typename PEHeaderTy> void Writer::writeHeader() {<br>
@@ -584,8 +580,8 @@ template <typename PEHeaderTy> void Writ<br>
   Buf += sizeof(*PE);<br>
   PE->Magic = Config->is64() ? PE32Header::PE32_PLUS : PE32Header::PE32;<br>
   PE->ImageBase = Config->ImageBase;<br>
-  PE->SectionAlignment = SectionAlignment;<br>
-  PE->FileAlignment = FileAlignment;<br>
+  PE->SectionAlignment = PageSize;<br>
+  PE->FileAlignment = SectorSize;<br>
   PE->MajorImageVersion = Config->MajorImageVersion;<br>
   PE->MinorImageVersion = Config->MinorImageVersion;<br>
   PE->MajorOperatingSystemVersion = Config->MajorOSVersion;<br>
<br>
Modified: lld/trunk/test/COFF/baserel.test<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/lld/trunk/test/COFF/baserel.test?rev=244691&r1=244690&r2=244691&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/lld/trunk/test/COFF/baserel.test?rev=244691&r1=244690&r2=244691&view=diff</a><br>
==============================================================================<br>
--- lld/trunk/test/COFF/baserel.test (original)<br>
+++ lld/trunk/test/COFF/baserel.test Tue Aug 11 18:09:00 2015<br>
@@ -61,7 +61,7 @@<br>
 # BASEREL-HEADER-NEXT: VirtualSize: 0x20<br>
 # BASEREL-HEADER-NEXT: VirtualAddress: 0x5000<br>
 # BASEREL-HEADER-NEXT: RawDataSize: 512<br>
-# BASEREL-HEADER-NEXT: PointerToRawData: 0x1800<br>
+# BASEREL-HEADER-NEXT: PointerToRawData: 0xC00<br>
 # BASEREL-HEADER-NEXT: PointerToRelocations: 0x0<br>
 # BASEREL-HEADER-NEXT: PointerToLineNumbers: 0x0<br>
 # BASEREL-HEADER-NEXT: RelocationCount: 0<br>
<br>
Modified: lld/trunk/test/COFF/hello32.test<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/lld/trunk/test/COFF/hello32.test?rev=244691&r1=244690&r2=244691&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/lld/trunk/test/COFF/hello32.test?rev=244691&r1=244690&r2=244691&view=diff</a><br>
==============================================================================<br>
--- lld/trunk/test/COFF/hello32.test (original)<br>
+++ lld/trunk/test/COFF/hello32.test Tue Aug 11 18:09:00 2015<br>
@@ -38,8 +38,8 @@ HEADER-NEXT:   MajorImageVersion: 0<br>
 HEADER-NEXT:   MinorImageVersion: 0<br>
 HEADER-NEXT:   MajorSubsystemVersion: 6<br>
 HEADER-NEXT:   MinorSubsystemVersion: 0<br>
-HEADER-NEXT:   SizeOfImage: 20480<br>
-HEADER-NEXT:   SizeOfHeaders: 4096<br>
+HEADER-NEXT:   SizeOfImage: 16896<br>
+HEADER-NEXT:   SizeOfHeaders: 512<br>
 HEADER-NEXT:   Subsystem: IMAGE_SUBSYSTEM_WINDOWS_CUI (0x3)<br>
 HEADER-NEXT:   Characteristics [ (0x8140)<br>
 HEADER-NEXT:     IMAGE_DLL_CHARACTERISTICS_DYNAMIC_BASE (0x40)<br>
<br>
<br>
_______________________________________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@lists.llvm.org">llvm-commits@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits</a><br>
</blockquote></div><br></div></div>