[PATCH] [lld][PECOFF] Calculate SizeOfHeaders field instead of using a hard-coded value

Ron Ofir ron.ofir at gmail.com
Wed Sep 18 07:56:59 PDT 2013


Hi rui314,

This patch changes WritePECOFF to calculate the value of the SizeOfHeaders PE header field instead of just using 512.

http://llvm-reviews.chandlerc.com/D1708

Files:
  lib/ReaderWriter/PECOFF/WriterPECOFF.cpp
  test/pecoff/hello.test

Index: lib/ReaderWriter/PECOFF/WriterPECOFF.cpp
===================================================================
--- lib/ReaderWriter/PECOFF/WriterPECOFF.cpp
+++ lib/ReaderWriter/PECOFF/WriterPECOFF.cpp
@@ -188,10 +188,6 @@
     _peHeader.MajorSubsystemVersion = minOSVersion.majorVersion;
     _peHeader.MinorSubsystemVersion = minOSVersion.minorVersion;
 
-    // The combined size of the DOS, PE and section headers including garbage
-    // between the end of the header and the beginning of the first section.
-    // Must be multiple of FileAlignment.
-    _peHeader.SizeOfHeaders = 512;
     _peHeader.Subsystem = context.getSubsystem();
 
     // Despite its name, DLL characteristics field has meaning both for
@@ -226,6 +222,11 @@
     fileBuffer += sizeof(_coffHeader);
     std::memcpy(fileBuffer, &_peHeader, sizeof(_peHeader));
   }
+  
+  virtual void setSizeOfHeaders(uint64_t size) {
+    // Must be multiple of FileAlignment.
+    _peHeader.SizeOfHeaders = llvm::RoundUpToAlignment(size, SECTOR_SIZE);
+  }
 
   virtual void setSizeOfCode(uint64_t size) {
     _peHeader.SizeOfCode = size;
@@ -832,6 +837,11 @@
     peHeader->setSizeOfUninitializedData(calcSizeOfUninitializedData());
     peHeader->setNumberOfSections(_numSections);
     peHeader->setSizeOfImage(_imageSizeInMemory);
+    
+    // The combined size of the DOS, PE and section headers including garbage
+    // between the end of the header and the beginning of the first section.
+    peHeader->setSizeOfHeaders(dosStub->size() + peHeader->size() +
+        sectionTable->size() + dataDirectory->size());
 
     setAddressOfEntryPoint(text, peHeader);
   }
Index: test/pecoff/hello.test
===================================================================
--- test/pecoff/hello.test
+++ test/pecoff/hello.test
@@ -6,6 +6,7 @@
 
 FILE: ImageOptionalHeader {
 FILE:   SizeOfInitializedData: 1024
+FILE:   SizeOfHeaders: 512
 FILE: }
 
 # RUN: lld -flavor link /out:%t1 /subsystem:console /force /opt:noref \
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D1708.1.patch
Type: text/x-patch
Size: 1995 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20130918/eea332cf/attachment.bin>


More information about the llvm-commits mailing list