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

Rui Ueyama via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 12 19:17:34 PDT 2015


I didn't test to see if this change ever has a negative impact on memory
usage, but my guess is that's very unlikely because this layout is the same
as what MSVC linker creates. If this is inefficient, virtually all Windows
executables are being suffered by that, which is unlikely. My understanding
is that the kernel maps each section separately to a memory address, so
file offset of each section can be given independently from other sections.

On Wed, Aug 12, 2015 at 5:31 PM, Sean Silva <chisophugis at gmail.com> wrote:

>
>
> On Tue, Aug 11, 2015 at 4:09 PM, Rui Ueyama via llvm-commits <
> llvm-commits at lists.llvm.org> wrote:
>
>> 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.
>>
>
> 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.
>
> 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).
>
> -- Sean Silva
>
>
>>
>> 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)
>>
>>
>> _______________________________________________
>> llvm-commits mailing list
>> llvm-commits at lists.llvm.org
>> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits
>>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20150813/3e3be29f/attachment.html>


More information about the llvm-commits mailing list