[PECOFF][Writer] Do not emit the empty section as Windows loader rejects such executable.
authorRui Ueyama <ruiu@google.com>
Mon, 1 Jul 2013 07:32:12 +0000 (07:32 +0000)
committerRui Ueyama <ruiu@google.com>
Mon, 1 Jul 2013 07:32:12 +0000 (07:32 +0000)
llvm-svn: 185308

lld/lib/ReaderWriter/PECOFF/WriterPECOFF.cpp
lld/test/pecoff/hello.test
lld/test/pecoff/trivial.test

index 1821c93..b0b31f0 100644 (file)
@@ -438,6 +438,11 @@ void SectionHeaderTableChunk::write(uint8_t *fileBuffer) {
   uint64_t offset = 0;
   fileBuffer += fileOffset();
   for (const auto &chunk : _sections) {
+    // Skip the empty section. Windows loader does not like a section
+    // of size zero and rejects such executable.
+    if (chunk->size() == 0)
+      continue;
+
     const llvm::object::coff_section &header = chunk->getSectionHeader();
     std::memcpy(fileBuffer + offset, &header, sizeof(header));
     offset += sizeof(header);
@@ -525,8 +530,12 @@ private:
     uint32_t va = offset;
     for (auto &cp : _chunks) {
       if (SectionChunk *chunk = dyn_cast<SectionChunk>(&*cp)) {
-        numSections++;
         chunk->setVirtualAddress(va);
+
+        // Skip the empty section.
+        if (chunk->size() == 0)
+          continue;
+        numSections++;
         va = llvm::RoundUpToAlignment(va + chunk->size(), PAGE_SIZE);
       }
     }
index 0676950..f69d8e2 100644 (file)
@@ -25,22 +25,6 @@ CHECK:     ]
 CHECK:   }
 CHECK:   Section {
 CHECK:     Number: 2
-CHECK:     Name: .rdata (2E 72 64 61 74 61 00 00)
-CHECK:     VirtualSize: 0x0
-CHECK:     VirtualAddress: 0x2000
-CHECK:     RawDataSize: 0
-CHECK:     PointerToRawData: 0x400
-CHECK:     PointerToRelocations: 0x0
-CHECK:     PointerToLineNumbers: 0x0
-CHECK:     RelocationCount: 0
-CHECK:     LineNumberCount: 0
-CHECK:     Characteristics [ (0x40000040)
-CHECK:       IMAGE_SCN_CNT_INITIALIZED_DATA (0x40)
-CHECK:       IMAGE_SCN_MEM_READ (0x40000000)
-CHECK:     ]
-CHECK:   }
-CHECK:   Section {
-CHECK:     Number: 3
 CHECK:     Name: .data (2E 64 61 74 61 00 00 00)
 CHECK:     VirtualSize: 0x18
 CHECK:     VirtualAddress: 0x2000
index a47dab0..2a19db8 100644 (file)
@@ -10,7 +10,7 @@ FILE: Arch: i386
 FILE: AddressSize: 32bit
 FILE: ImageFileHeader {
 FILE:   Machine: IMAGE_FILE_MACHINE_I386 (0x14C)
-FILE:   SectionCount: 3
+FILE:   SectionCount: 1
 FILE:   TimeDateStamp:
 FILE:   PointerToSymbolTable: 0x0
 FILE:   SymbolCount: 0
@@ -78,37 +78,4 @@ SECTIONS:       IMAGE_SCN_MEM_EXECUTE (0x20000000)
 SECTIONS:       IMAGE_SCN_MEM_READ (0x40000000)
 SECTIONS:     ]
 SECTIONS:   }
-SECTIONS:   Section {
-SECTIONS:     Number: 2
-SECTIONS:     Name: .rdata (2E 72 64 61 74 61 00 00)
-SECTIONS:     VirtualSize: 0x0
-SECTIONS:     VirtualAddress: 0x2000
-SECTIONS:     RawDataSize: 0
-SECTIONS:     PointerToRawData: 0x400
-SECTIONS:     PointerToRelocations: 0x0
-SECTIONS:     PointerToLineNumbers: 0x0
-SECTIONS:     RelocationCount: 0
-SECTIONS:     LineNumberCount: 0
-SECTIONS:     Characteristics [ (0x40000040)
-SECTIONS:       IMAGE_SCN_CNT_INITIALIZED_DATA (0x40)
-SECTIONS:       IMAGE_SCN_MEM_READ (0x40000000)
-SECTIONS:     ]
-SECTIONS:   }
-SECTIONS:   Section {
-SECTIONS:     Number: 3
-SECTIONS:     Name: .data (2E 64 61 74 61 00 00 00)
-SECTIONS:     VirtualSize: 0x0
-SECTIONS:     VirtualAddress: 0x2000
-SECTIONS:     RawDataSize: 0
-SECTIONS:     PointerToRawData: 0x400
-SECTIONS:     PointerToRelocations: 0x0
-SECTIONS:     PointerToLineNumbers: 0x0
-SECTIONS:     RelocationCount: 0
-SECTIONS:     LineNumberCount: 0
-SECTIONS:     Characteristics [ (0xC0000040)
-SECTIONS:       IMAGE_SCN_CNT_INITIALIZED_DATA (0x40)
-SECTIONS:       IMAGE_SCN_MEM_READ (0x40000000)
-SECTIONS:       IMAGE_SCN_MEM_WRITE (0x80000000)
-SECTIONS:     ]
-SECTIONS:   }
 SECTIONS: ]