[lld] r191218 - [PECOFF] Add /align command line option.

Rui Ueyama ruiu at google.com
Mon Sep 23 12:52:36 PDT 2013


Author: ruiu
Date: Mon Sep 23 14:52:35 2013
New Revision: 191218

URL: http://llvm.org/viewvc/llvm-project?rev=191218&view=rev
Log:
[PECOFF] Add /align command line option.

Modified:
    lld/trunk/include/lld/ReaderWriter/PECOFFLinkingContext.h
    lld/trunk/lib/Driver/WinLinkDriver.cpp
    lld/trunk/lib/Driver/WinLinkOptions.td
    lld/trunk/lib/ReaderWriter/PECOFF/PECOFFLinkingContext.cpp
    lld/trunk/lib/ReaderWriter/PECOFF/WriterPECOFF.cpp
    lld/trunk/unittests/DriverTests/WinLinkDriverTest.cpp

Modified: lld/trunk/include/lld/ReaderWriter/PECOFFLinkingContext.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/include/lld/ReaderWriter/PECOFFLinkingContext.h?rev=191218&r1=191217&r2=191218&view=diff
==============================================================================
--- lld/trunk/include/lld/ReaderWriter/PECOFFLinkingContext.h (original)
+++ lld/trunk/include/lld/ReaderWriter/PECOFFLinkingContext.h Mon Sep 23 14:52:35 2013
@@ -29,7 +29,7 @@ class PECOFFLinkingContext : public Link
 public:
   PECOFFLinkingContext()
       : _baseAddress(0x400000), _stackReserve(1024 * 1024), _stackCommit(4096),
-        _heapReserve(1024 * 1024), _heapCommit(4096),
+        _heapReserve(1024 * 1024), _heapCommit(4096), _sectionAlignment(4096),
         _subsystem(llvm::COFF::IMAGE_SUBSYSTEM_UNKNOWN),
         _machineType(llvm::COFF::IMAGE_FILE_MACHINE_I386), _imageVersion(0, 0),
         _minOSVersion(6, 0), _nxCompat(true), _largeAddressAware(false),
@@ -102,6 +102,9 @@ public:
   uint64_t getHeapReserve() const { return _heapReserve; }
   uint64_t getHeapCommit() const { return _heapCommit; }
 
+  void setSectionAlignment(uint32_t val) { _sectionAlignment = val; }
+  uint32_t getSectionAlignment() const { return _sectionAlignment; }
+
   void setSubsystem(WindowsSubsystem ss) { _subsystem = ss; }
   WindowsSubsystem getSubsystem() const { return _subsystem; }
 
@@ -164,6 +167,7 @@ private:
   uint64_t _stackCommit;
   uint64_t _heapReserve;
   uint64_t _heapCommit;
+  uint32_t _sectionAlignment;
   WindowsSubsystem _subsystem;
   MachineTypes _machineType;
   Version _imageVersion;

Modified: lld/trunk/lib/Driver/WinLinkDriver.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/Driver/WinLinkDriver.cpp?rev=191218&r1=191217&r2=191218&view=diff
==============================================================================
--- lld/trunk/lib/Driver/WinLinkDriver.cpp (original)
+++ lld/trunk/lib/Driver/WinLinkDriver.cpp Mon Sep 23 14:52:35 2013
@@ -330,6 +330,16 @@ bool WinLinkDriver::parse(int argc, cons
       ctx.setHeapCommit(commit);
       break;
     }
+    case OPT_align: {
+      uint32_t align;
+      StringRef arg = inputArg->getValue();
+      if (arg.getAsInteger(10, align)) {
+        diagnostics << "error: invalid value for /align: " << arg << "\n";
+        return true;
+      }
+      ctx.setSectionAlignment(align);
+      break;
+    }
     case OPT_machine: {
       StringRef arg = inputArg->getValue();
       llvm::COFF::MachineTypes type = stringToMachineType(arg);

Modified: lld/trunk/lib/Driver/WinLinkOptions.td
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/Driver/WinLinkOptions.td?rev=191218&r1=191217&r2=191218&view=diff
==============================================================================
--- lld/trunk/lib/Driver/WinLinkOptions.td (original)
+++ lld/trunk/lib/Driver/WinLinkOptions.td Mon Sep 23 14:52:35 2013
@@ -15,6 +15,7 @@ defm entry   : P<"entry", "Name of entry
 // No help text because /failifmismatch is not intended to be used by the user.
 defm failifmismatch : P<"failifmismatch", "">;
 defm heap    : P<"heap", "Size of the heap">;
+defm align   : P<"align", "Section alignment">;
 defm libpath : P<"libpath", "Additional library search path">;
 defm mllvm   : P<"mllvm", "Options to pass to LLVM">;
 defm out     : P<"out", "Path to file to write output">;

Modified: lld/trunk/lib/ReaderWriter/PECOFF/PECOFFLinkingContext.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/PECOFF/PECOFFLinkingContext.cpp?rev=191218&r1=191217&r2=191218&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/PECOFF/PECOFFLinkingContext.cpp (original)
+++ lld/trunk/lib/ReaderWriter/PECOFF/PECOFFLinkingContext.cpp Mon Sep 23 14:52:35 2013
@@ -23,6 +23,8 @@
 #include "lld/ReaderWriter/Simple.h"
 #include "lld/ReaderWriter/Writer.h"
 
+#include <bitset>
+
 namespace lld {
 
 namespace {} // anonymous namespace
@@ -55,6 +57,13 @@ bool PECOFFLinkingContext::validateImpl(
     return true;
   }
 
+  std::bitset<64> alignment(_sectionAlignment);
+  if (alignment.count() != 1) {
+    diagnostics << "Section alignment must be a power of 2, but got "
+                << _sectionAlignment << "\n";
+    return true;
+  }
+
   // Architectures other than i386 is not supported yet.
   if (_machineType != llvm::COFF::IMAGE_FILE_MACHINE_I386) {
     diagnostics << "Machine type other than x86 is not supported.\n";

Modified: lld/trunk/lib/ReaderWriter/PECOFF/WriterPECOFF.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/PECOFF/WriterPECOFF.cpp?rev=191218&r1=191217&r2=191218&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/PECOFF/WriterPECOFF.cpp (original)
+++ lld/trunk/lib/ReaderWriter/PECOFF/WriterPECOFF.cpp Mon Sep 23 14:52:35 2013
@@ -174,7 +174,7 @@ public:
 
     // Sections should be page-aligned when loaded into memory, which is 4KB on
     // x86.
-    _peHeader.SectionAlignment = PAGE_SIZE;
+    _peHeader.SectionAlignment = context.getSectionAlignment();
 
     // Sections in an executable file on disk should be sector-aligned (512 byte).
     _peHeader.FileAlignment = SECTOR_SIZE;

Modified: lld/trunk/unittests/DriverTests/WinLinkDriverTest.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/unittests/DriverTests/WinLinkDriverTest.cpp?rev=191218&r1=191217&r2=191218&view=diff
==============================================================================
--- lld/trunk/unittests/DriverTests/WinLinkDriverTest.cpp (original)
+++ lld/trunk/unittests/DriverTests/WinLinkDriverTest.cpp Mon Sep 23 14:52:35 2013
@@ -49,6 +49,7 @@ TEST_F(WinLinkParserTest, Basic) {
   EXPECT_EQ(0x400000U, _context.getBaseAddress());
   EXPECT_EQ(1024 * 1024U, _context.getStackReserve());
   EXPECT_EQ(4096U, _context.getStackCommit());
+  EXPECT_EQ(4096U, _context.getSectionAlignment());
   EXPECT_FALSE(_context.allowRemainingUndefines());
   EXPECT_TRUE(_context.isNxCompat());
   EXPECT_FALSE(_context.getLargeAddressAware());
@@ -175,6 +176,11 @@ TEST_F(WinLinkParserTest, HeapReserveAnd
   EXPECT_EQ(8192U, _context.getHeapCommit());
 }
 
+TEST_F(WinLinkParserTest, SectionAlignment) {
+  EXPECT_FALSE(parse("link.exe", "/align:8192", "a.obj", nullptr));
+  EXPECT_EQ(8192U, _context.getSectionAlignment());
+}
+
 TEST_F(WinLinkParserTest, Force) {
   EXPECT_FALSE(parse("link.exe", "/force", "a.obj", nullptr));
   EXPECT_TRUE(_context.allowRemainingUndefines());





More information about the llvm-commits mailing list