[PATCH] [lld][PECOFF] Fix base relocation block alignment

Ron Ofir ron.ofir at gmail.com
Wed Sep 18 08:41:06 PDT 2013


Hi rui314,

Base relocation block should be aligned on a 32-bit boundary. While the PECOFF spec mentions only aligning the blocks, and not padding them, link.exe seems to add an extra IMAGE_REL_I386_ABSOLUTE entry (just a zeroed WORD) in order to pad the blocks.

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

Files:
  lib/ReaderWriter/PECOFF/WriterPECOFF.cpp

Index: lib/ReaderWriter/PECOFF/WriterPECOFF.cpp
===================================================================
--- lib/ReaderWriter/PECOFF/WriterPECOFF.cpp
+++ lib/ReaderWriter/PECOFF/WriterPECOFF.cpp
@@ -665,6 +666,8 @@
       : COFFLinkerInternalAtom(file, std::move(data)) {}
 
   virtual ContentType contentType() const { return typeData; }
+  
+  virtual Alignment alignment() const { return Alignment(2); };
 };
 
 /// A BaseRelocChunk represents ".reloc" section.
@@ -736,8 +739,10 @@
   // Create the content of a relocation block.
   DefinedAtom *createBaseRelocBlock(const File &file, uint64_t pageAddr,
                                     const std::vector<uint16_t> &offsets) {
-    uint32_t size = sizeof(ulittle32_t) * 2
-        + sizeof(ulittle16_t) * offsets.size();
+    // Relocation blocks should be padded with IMAGE_REL_I386_ABSOLUTE to be
+    // aligned to a DWORD size boundary.
+    uint32_t size = llvm::RoundUpToAlignment(sizeof(ulittle32_t) * 2
+        + sizeof(ulittle16_t) * offsets.size(), sizeof(ulittle32_t));
     std::vector<uint8_t> contents(size);
     uint8_t *ptr = &contents[0];
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D1709.1.patch
Type: text/x-patch
Size: 1129 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20130918/90d6786e/attachment.bin>


More information about the llvm-commits mailing list