[lld] r211371 - PE/COFF: move PAGE_SIZE into the PECOFFLinkingContext.

Tim Northover tnorthover at apple.com
Fri Jun 20 09:45:16 PDT 2014


Author: tnorthover
Date: Fri Jun 20 11:45:16 2014
New Revision: 211371

URL: http://llvm.org/viewvc/llvm-project?rev=211371&view=rev
Log:
PE/COFF: move PAGE_SIZE into the PECOFFLinkingContext.

A refactoring, with the added benefit of helping OS X builds.

Modified:
    lld/trunk/include/lld/ReaderWriter/PECOFFLinkingContext.h
    lld/trunk/lib/ReaderWriter/PECOFF/WriterPECOFF.cpp

Modified: lld/trunk/include/lld/ReaderWriter/PECOFFLinkingContext.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/include/lld/ReaderWriter/PECOFFLinkingContext.h?rev=211371&r1=211370&r2=211371&view=diff
==============================================================================
--- lld/trunk/include/lld/ReaderWriter/PECOFFLinkingContext.h (original)
+++ lld/trunk/include/lld/ReaderWriter/PECOFFLinkingContext.h Fri Jun 20 11:45:16 2014
@@ -88,6 +88,12 @@ public:
     return _machineType == llvm::COFF::IMAGE_FILE_MACHINE_AMD64;
   }
 
+  /// Page size of x86 processor. Some data needs to be aligned at page boundary
+  /// when loaded into memory.
+  uint64_t getPageSize() const {
+    return 0x1000;
+  }
+
   void appendInputSearchPath(StringRef dirPath) {
     _inputSearchPaths.push_back(dirPath);
   }

Modified: lld/trunk/lib/ReaderWriter/PECOFF/WriterPECOFF.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/PECOFF/WriterPECOFF.cpp?rev=211371&r1=211370&r2=211371&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/PECOFF/WriterPECOFF.cpp (original)
+++ lld/trunk/lib/ReaderWriter/PECOFF/WriterPECOFF.cpp Fri Jun 20 11:45:16 2014
@@ -55,10 +55,6 @@ using llvm::support::ulittle64_t;
 namespace lld {
 namespace pecoff {
 
-// Page size of x86 processor. Some data needs to be aligned at page boundary
-// when loaded into memory.
-static const int PAGE_SIZE = 4096;
-
 // Disk sector size. Some data needs to be aligned at disk sector boundary in
 // file.
 static const int SECTOR_SIZE = 512;
@@ -286,9 +282,9 @@ class BaseRelocChunk : public SectionChu
   typedef std::map<uint64_t, std::vector<uint16_t> > PageOffsetT;
 
 public:
-  BaseRelocChunk(ChunkVectorT &chunks)
+  BaseRelocChunk(ChunkVectorT &chunks, const PECOFFLinkingContext &ctx)
       : SectionChunk(kindSection, ".reloc", characteristics),
-        _contents(createContents(chunks)) {}
+        _ctx(ctx), _contents(createContents(chunks)) {}
 
   void write(uint8_t *buffer) override {
     std::memcpy(buffer, &_contents[0], _contents.size());
@@ -317,6 +313,7 @@ private:
   createBaseRelocBlock(uint64_t pageAddr,
                        const std::vector<uint16_t> &offsets) const;
 
+  const PECOFFLinkingContext &_ctx;
   std::vector<uint8_t> _contents;
 };
 
@@ -787,7 +784,7 @@ BaseRelocChunk::listRelocSites(ChunkVect
 BaseRelocChunk::PageOffsetT
 BaseRelocChunk::groupByPage(const std::vector<uint64_t> &relocSites) const {
   PageOffsetT blocks;
-  uint64_t mask = static_cast<uint64_t>(PAGE_SIZE) - 1;
+  uint64_t mask = _ctx.getPageSize() - 1;
   for (uint64_t addr : relocSites)
     blocks[addr & ~mask].push_back(addr & mask);
   return blocks;
@@ -815,7 +812,7 @@ std::vector<uint8_t> BaseRelocChunk::cre
 
   // The rest of the block consists of offsets in the page.
   for (uint16_t offset : offsets) {
-    assert(offset < PAGE_SIZE);
+    assert(offset < _ctx.getPageSize());
     uint16_t val = (llvm::COFF::IMAGE_REL_BASED_HIGHLOW << 12) | offset;
     *reinterpret_cast<ulittle16_t *>(ptr) = val;
     ptr += sizeof(ulittle16_t);
@@ -828,7 +825,7 @@ std::vector<uint8_t> BaseRelocChunk::cre
 class PECOFFWriter : public Writer {
 public:
   explicit PECOFFWriter(const PECOFFLinkingContext &context)
-      : _ctx(context), _numSections(0), _imageSizeInMemory(PAGE_SIZE),
+      : _ctx(context), _numSections(0), _imageSizeInMemory(_ctx.getPageSize()),
         _imageSizeOnDisk(0) {}
 
   template <class PEHeader> void build(const File &linkedFile);
@@ -863,10 +860,10 @@ private:
   const PECOFFLinkingContext &_ctx;
   uint32_t _numSections;
 
-  // The size of the image in memory. This is initialized with PAGE_SIZE, as the
-  // first page starting at ImageBase is usually left unmapped. IIUC there's no
-  // technical reason to do so, but we'll follow that convention so that we
-  // don't produce odd-looking binary.
+  // The size of the image in memory. This is initialized with
+  // _ctx.getPageSize(), as the first page starting at ImageBase is usually left
+  // unmapped. IIUC there's no technical reason to do so, but we'll follow that
+  // convention so that we don't produce odd-looking binary.
   uint32_t _imageSizeInMemory;
 
   // The size of the image on disk. This is basically the sum of all chunks in
@@ -948,7 +945,7 @@ void PECOFFWriter::build(const File &lin
   // relocated. So we can create the ".reloc" section which contains all the
   // relocation sites.
   if (_ctx.getBaseRelocationEnabled()) {
-    BaseRelocChunk *baseReloc = new BaseRelocChunk(_chunks);
+    BaseRelocChunk *baseReloc = new BaseRelocChunk(_chunks, _ctx);
     if (baseReloc->size()) {
       addSectionChunk(baseReloc, sectionTable);
       dataDirectory->setField(DataDirectoryIndex::BASE_RELOCATION_TABLE,
@@ -1132,8 +1129,8 @@ void PECOFFWriter::addSectionChunk(Secti
   // memory. They are different from positions on disk because sections need
   // to be sector-aligned on disk but page-aligned in memory.
   chunk->setVirtualAddress(_imageSizeInMemory);
-  _imageSizeInMemory =
-      llvm::RoundUpToAlignment(_imageSizeInMemory + chunk->size(), PAGE_SIZE);
+  _imageSizeInMemory = llvm::RoundUpToAlignment(
+      _imageSizeInMemory + chunk->size(), _ctx.getPageSize());
 }
 
 void PECOFFWriter::setImageSizeOnDisk() {





More information about the llvm-commits mailing list