[lld] r190120 - [PECOFF] Align section contents as specified by the object file's section header.

Rui Ueyama ruiu at google.com
Thu Sep 5 21:06:59 PDT 2013


Author: ruiu
Date: Thu Sep  5 23:06:58 2013
New Revision: 190120

URL: http://llvm.org/viewvc/llvm-project?rev=190120&view=rev
Log:
[PECOFF] Align section contents as specified by the object file's section header.

Modified:
    lld/trunk/lib/ReaderWriter/PECOFF/Atoms.h
    lld/trunk/lib/ReaderWriter/PECOFF/ReaderCOFF.cpp
    lld/trunk/lib/ReaderWriter/PECOFF/WriterPECOFF.cpp
    lld/trunk/test/pecoff/lib.test
    lld/trunk/test/pecoff/multi.test

Modified: lld/trunk/lib/ReaderWriter/PECOFF/Atoms.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/PECOFF/Atoms.h?rev=190120&r1=190119&r2=190120&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/PECOFF/Atoms.h (original)
+++ lld/trunk/lib/ReaderWriter/PECOFF/Atoms.h Thu Sep  5 23:06:58 2013
@@ -149,17 +149,20 @@ public:
                       ContentPermissions perms, uint64_t ordinal)
       : COFFBaseDefinedAtom(file, name, Kind::File), _sectionName(sectionName),
         _scope(scope), _contentType(contentType), _permissions(perms),
-        _ordinal(ordinal) {}
+        _ordinal(ordinal), _alignment(1) {}
 
   static bool classof(const COFFBaseDefinedAtom *atom) {
     return atom->getKind() == Kind::File;
   }
 
+  void setAlignment(Alignment val) { _alignment = val; };
+
   virtual StringRef getSectionName() const { return _sectionName; }
   virtual Scope scope() const { return _scope; }
   virtual ContentType contentType() const { return _contentType; }
   virtual ContentPermissions permissions() const { return _permissions; }
   virtual uint64_t ordinal() const { return _ordinal; }
+  virtual Alignment alignment() const { return _alignment; }
 
 private:
   StringRef _sectionName;
@@ -167,6 +170,7 @@ private:
   ContentType _contentType;
   ContentPermissions _permissions;
   uint64_t _ordinal;
+  Alignment _alignment;
   std::vector<std::unique_ptr<COFFReference>> _references;
 };
 

Modified: lld/trunk/lib/ReaderWriter/PECOFF/ReaderCOFF.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/PECOFF/ReaderCOFF.cpp?rev=190120&r1=190119&r2=190120&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/PECOFF/ReaderCOFF.cpp (original)
+++ lld/trunk/lib/ReaderWriter/PECOFF/ReaderCOFF.cpp Thu Sep  5 23:06:58 2013
@@ -82,6 +82,16 @@ DefinedAtom::ContentPermissions getPermi
   return DefinedAtom::perm___;
 }
 
+/// Returns the alignment of the section. The contents of the section must be
+/// aligned by this value in the resulting executable/DLL.
+DefinedAtom::Alignment getAlignment(const coff_section *section) {
+  if (section->Characteristics & llvm::COFF::IMAGE_SCN_TYPE_NO_PAD)
+    return DefinedAtom::Alignment(0);
+  // Bit [20:24] contains section alignment information.
+  int powerOf2 = (section->Characteristics >> 20) & 0xf;
+  return DefinedAtom::Alignment(powerOf2);
+}
+
 DefinedAtom::Merge getMerge(const coff_aux_section_definition *auxsym) {
   switch (auxsym->Selection) {
     case llvm::COFF::IMAGE_COMDAT_SELECT_NODUPLICATES:
@@ -449,6 +459,10 @@ private:
       _symbolAtom[*si] = atom;
       _definedAtomLocations[section][(*si)->Value] = atom;
     }
+
+    // Finally, set alignment to the first atom so that the section contents
+    // will be aligned as specified by the object section header.
+    _definedAtomLocations[section][0]->setAlignment(getAlignment(section));
     return error_code::success();
   }
 

Modified: lld/trunk/lib/ReaderWriter/PECOFF/WriterPECOFF.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/PECOFF/WriterPECOFF.cpp?rev=190120&r1=190119&r2=190120&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/PECOFF/WriterPECOFF.cpp (original)
+++ lld/trunk/lib/ReaderWriter/PECOFF/WriterPECOFF.cpp Thu Sep  5 23:06:58 2013
@@ -470,6 +470,11 @@ public:
   }
 
   void appendAtom(const DefinedAtom *atom) {
+    // Atom may have to be at a proper alignment boundary. If so, move the
+    // pointer to make a room after the last atom before adding new one.
+    _size = llvm::RoundUpToAlignment(_size, 1 << atom->alignment().powerOf2);
+
+    // Create an AtomLayout and move the current pointer.
     auto *layout = new (_alloc) AtomLayout(atom, _size, _size);
     _atomLayouts.push_back(layout);
     _size += atom->size();

Modified: lld/trunk/test/pecoff/lib.test
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/pecoff/lib.test?rev=190120&r1=190119&r2=190120&view=diff
==============================================================================
--- lld/trunk/test/pecoff/lib.test (original)
+++ lld/trunk/test/pecoff/lib.test Thu Sep  5 23:06:58 2013
@@ -7,6 +7,6 @@
 
 CHECK: Disassembly of section .text:
 CHECK: .text:
-CHECK:     1000: a1 04 20 40 00      movl 4202500, %eax
+CHECK:     1000: a1 08 20 40 00      movl 4202504, %eax
 CHECK:     1005: 03 05 00 20 40 00   addl 4202496, %eax
 CHECK:     100b: c3                  ret

Modified: lld/trunk/test/pecoff/multi.test
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/pecoff/multi.test?rev=190120&r1=190119&r2=190120&view=diff
==============================================================================
--- lld/trunk/test/pecoff/multi.test (original)
+++ lld/trunk/test/pecoff/multi.test Thu Sep  5 23:06:58 2013
@@ -9,6 +9,6 @@
 
 CHECK: Disassembly of section .text:
 CHECK: .text:
-CHECK:     1000: a1 04 20 40 00      movl 4202500, %eax
+CHECK:     1000: a1 08 20 40 00      movl 4202504, %eax
 CHECK:     1005: 03 05 00 20 40 00   addl 4202496, %eax
 CHECK:     100b: c3                  ret





More information about the llvm-commits mailing list