[PATCH] [lld][PECOFF] Fix data directory entry RVA of base relocations section

Ron Ofir ron.ofir at gmail.com
Mon Sep 23 12:53:12 PDT 2013


Hi ruiu,

This patch changes WriterPECOFF to actually write down the address instead of ignoring it.
Also, it changes the order of adding the BaseReloc chunk as otherwise the address wasn't set yet.

I think a better way of doing it would be to change DataDirectoryAtom to create a Reference
instead of using a number, and to change IdataPass accordingly, but I'm not sure how to do that.

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

Files:
  lib/ReaderWriter/PECOFF/Atoms.h
  lib/ReaderWriter/PECOFF/WriterPECOFF.cpp
  test/pecoff/base-reloc.test

Index: lib/ReaderWriter/PECOFF/Atoms.h
===================================================================
--- lib/ReaderWriter/PECOFF/Atoms.h
+++ lib/ReaderWriter/PECOFF/Atoms.h
@@ -240,17 +240,20 @@
 // COFF header.
 class COFFDataDirectoryAtom : public COFFLinkerInternalAtom {
 public:
-  COFFDataDirectoryAtom(const File &file, uint64_t ordinal, uint32_t entrySize)
-      : COFFLinkerInternalAtom(file, assembleRawContent(entrySize)),
+  COFFDataDirectoryAtom(const File &file, uint64_t ordinal, uint32_t entrySize,
+                        uint32_t entryAddr = 0)
+      : COFFLinkerInternalAtom(file, assembleRawContent(entrySize, entryAddr)),
         _ordinal(ordinal) {}
 
   virtual uint64_t ordinal() const { return _ordinal; }
   virtual ContentType contentType() const { return typeDataDirectoryEntry; }
   virtual ContentPermissions permissions() const { return permR__; }
 
 private:
-  std::vector<uint8_t> assembleRawContent(uint32_t entrySize) {
+  std::vector<uint8_t> assembleRawContent(uint32_t entrySize, uint32_t entryAddr) {
     std::vector<uint8_t> data = std::vector<uint8_t>(8, 0);
+    
+    *(reinterpret_cast<uint32_t *>(&data[0])) = entryAddr;
     *(reinterpret_cast<uint32_t *>(&data[4])) = entrySize;
     return data;
   }
Index: lib/ReaderWriter/PECOFF/WriterPECOFF.cpp
===================================================================
--- lib/ReaderWriter/PECOFF/WriterPECOFF.cpp
+++ lib/ReaderWriter/PECOFF/WriterPECOFF.cpp
@@ -425,7 +425,8 @@
 
   void setBaseRelocField(uint32_t addr, uint32_t size) {
     auto *atom = new (_alloc) coff::COFFDataDirectoryAtom(
-        _file, llvm::COFF::DataDirectoryIndex::BASE_RELOCATION_TABLE, size);
+        _file, llvm::COFF::DataDirectoryIndex::BASE_RELOCATION_TABLE, size,
+        addr);
     uint64_t offset = atom->ordinal() * sizeof(llvm::object::data_directory);
     _atomLayouts.push_back(new (_alloc) AtomLayout(atom, offset, offset));
   }
@@ -820,9 +821,9 @@
     if (baseReloc) {
       baseReloc->setContents(_chunks);
       if (baseReloc->size()) {
+        addSectionChunk(baseReloc, sectionTable);
         dataDirectory->setBaseRelocField(baseReloc->getSectionRva(),
                                          baseReloc->rawSize());
-        addSectionChunk(baseReloc, sectionTable);
       }
     }
 
Index: test/pecoff/base-reloc.test
===================================================================
--- test/pecoff/base-reloc.test
+++ test/pecoff/base-reloc.test
@@ -31,6 +31,8 @@
 
 NOBASEREL-HEADER: IMAGE_FILE_RELOCS_STRIPPED
 
+BASEREL-HEADER:     BaseRelocationTableRVA: 0x3000
+BASEREL-HEADER:     BaseRelocationTableSize: 0xC
 BASEREL-HEADER:     Name: .reloc (2E 72 65 6C 6F 63 00 00)
 BASEREL-HEADER-NEXT:     VirtualSize: 0xC
 BASEREL-HEADER-NEXT:     VirtualAddress: 0x3000
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D1743.1.patch
Type: text/x-patch
Size: 2796 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20130923/6af2347f/attachment.bin>


More information about the llvm-commits mailing list