[lld] r239929 - COFF: Support module-definition files.

Rui Ueyama ruiu at google.com
Wed Jun 17 12:19:26 PDT 2015


Author: ruiu
Date: Wed Jun 17 14:19:25 2015
New Revision: 239929

URL: http://llvm.org/viewvc/llvm-project?rev=239929&view=rev
Log:
COFF: Support module-definition files.

Module-definition files (.def files) are yet another way to
specify parameters to the linker. You can write a list of dllexported
symbols in module-definition files instead of using /export command
line option. It also supports a few more directives.

The parser code is taken from lib/Driver/WinLinkModuleDef.cpp
with the following modifications.

 - variable names are updated to comply with the LLVM coding style.
 - Instead of returning parsing results as "directive" objects,
   it updates Config object directly.

Added:
    lld/trunk/COFF/ModuleDef.cpp
Modified:
    lld/trunk/COFF/CMakeLists.txt
    lld/trunk/COFF/Driver.cpp
    lld/trunk/COFF/Driver.h
    lld/trunk/test/COFF/export.test
    lld/trunk/test/COFF/heap.test
    lld/trunk/test/COFF/stack.test

Modified: lld/trunk/COFF/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/COFF/CMakeLists.txt?rev=239929&r1=239928&r2=239929&view=diff
==============================================================================
--- lld/trunk/COFF/CMakeLists.txt (original)
+++ lld/trunk/COFF/CMakeLists.txt Wed Jun 17 14:19:25 2015
@@ -8,6 +8,7 @@ add_llvm_library(lldCOFF
   Driver.cpp
   DriverUtils.cpp
   InputFiles.cpp
+  ModuleDef.cpp
   SymbolTable.cpp
   Symbols.cpp
   Writer.cpp

Modified: lld/trunk/COFF/Driver.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/COFF/Driver.cpp?rev=239929&r1=239928&r2=239929&view=diff
==============================================================================
--- lld/trunk/COFF/Driver.cpp (original)
+++ lld/trunk/COFF/Driver.cpp Wed Jun 17 14:19:25 2015
@@ -355,6 +355,18 @@ bool LinkerDriver::link(int Argc, const
     return false;
   }
 
+  // Handle /def
+  if (auto *Arg = Args->getLastArg(OPT_deffile)) {
+    ErrorOr<MemoryBufferRef> MBOrErr = openFile(Arg->getValue());
+    if (auto EC = MBOrErr.getError()) {
+      llvm::errs() << "/def: " << EC.message() << "\n";
+      return false;
+    }
+    // parseModuleDefs mutates Config object.
+    if (parseModuleDefs(MBOrErr.get()))
+      return false;
+  }
+
   // Handle miscellaneous boolean flags.
   if (Args->hasArg(OPT_allowbind_no))      Config->AllowBind = false;
   if (Args->hasArg(OPT_allowisolation_no)) Config->AllowIsolation = false;

Modified: lld/trunk/COFF/Driver.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/COFF/Driver.h?rev=239929&r1=239928&r2=239929&view=diff
==============================================================================
--- lld/trunk/COFF/Driver.h (original)
+++ lld/trunk/COFF/Driver.h Wed Jun 17 14:19:25 2015
@@ -97,6 +97,8 @@ private:
   std::vector<std::unique_ptr<MemoryBuffer>> OwningMBs;
 };
 
+std::error_code parseModuleDefs(MemoryBufferRef MB);
+
 // Functions below this line are defined in DriverUtils.cpp.
 
 void printHelp(const char *Argv0);

Added: lld/trunk/COFF/ModuleDef.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/COFF/ModuleDef.cpp?rev=239929&view=auto
==============================================================================
--- lld/trunk/COFF/ModuleDef.cpp (added)
+++ lld/trunk/COFF/ModuleDef.cpp Wed Jun 17 14:19:25 2015
@@ -0,0 +1,311 @@
+//===- COFF/ModuleDef.cpp -------------------------------------------------===//
+//
+//                             The LLVM Linker
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// Windows-specific.
+// A parser for the module-definition file (.def file).
+// Parsed results are directly written to Config global variable.
+//
+// The format of module-definition files are described in this document:
+// https://msdn.microsoft.com/en-us/library/28d6s79h.aspx
+//
+//===----------------------------------------------------------------------===//
+
+#include "Config.h"
+#include "Error.h"
+#include "llvm/ADT/StringRef.h"
+#include "llvm/ADT/StringSwitch.h"
+#include "llvm/Support/raw_ostream.h"
+#include <system_error>
+
+using namespace llvm;
+
+namespace lld {
+namespace coff {
+namespace {
+
+enum Kind {
+  Unknown,
+  Eof,
+  Identifier,
+  Comma,
+  Equal,
+  KwBase,
+  KwData,
+  KwExports,
+  KwHeapsize,
+  KwLibrary,
+  KwName,
+  KwNoname,
+  KwPrivate,
+  KwStacksize,
+  KwVersion,
+};
+
+struct Token {
+  explicit Token(Kind T = Unknown, StringRef S = "") : K(T), Value(S) {}
+  Kind K;
+  StringRef Value;
+};
+
+class Lexer {
+public:
+  explicit Lexer(StringRef S) : Buf(S) {}
+
+  Token lex() {
+    Buf = Buf.trim();
+    if (Buf.empty())
+      return Token(Eof);
+
+    switch (Buf[0]) {
+    case '\0':
+      return Token(Eof);
+    case ';': {
+      size_t End = Buf.find('\n');
+      Buf = (End == Buf.npos) ? "" : Buf.drop_front(End);
+      return lex();
+    }
+    case '=':
+      Buf = Buf.drop_front();
+      return Token(Equal, "=");
+    case ',':
+      Buf = Buf.drop_front();
+      return Token(Comma, ",");
+    case '"': {
+      StringRef S;
+      std::tie(S, Buf) = Buf.substr(1).split('"');
+      return Token(Identifier, S);
+    }
+    default: {
+      size_t End = Buf.find_first_not_of(
+          "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
+          "0123456789_.*~+!@#$%^&*()/");
+      StringRef Word = Buf.substr(0, End);
+      Kind K = llvm::StringSwitch<Kind>(Word)
+                   .Case("BASE", KwBase)
+                   .Case("DATA", KwData)
+                   .Case("EXPORTS", KwExports)
+                   .Case("HEAPSIZE", KwHeapsize)
+                   .Case("LIBRARY", KwLibrary)
+                   .Case("NAME", KwName)
+                   .Case("NONAME", KwNoname)
+                   .Case("PRIVATE", KwPrivate)
+                   .Case("STACKSIZE", KwStacksize)
+                   .Case("VERSION", KwVersion)
+                   .Default(Identifier);
+      Buf = (End == Buf.npos) ? "" : Buf.drop_front(End);
+      return Token(K, Word);
+    }
+    }
+  }
+
+private:
+  StringRef Buf;
+};
+
+class Parser {
+public:
+  explicit Parser(StringRef S) : Lex(S) {}
+
+  std::error_code parse() {
+    do {
+      if (auto EC = parseOne())
+        return EC;
+    } while (Tok.K != Eof);
+    return std::error_code();
+  }
+
+private:
+  void read() {
+    if (Stack.empty()) {
+      Tok = Lex.lex();
+      return;
+    }
+    Tok = Stack.back();
+    Stack.pop_back();
+  }
+
+  std::error_code readAsInt(uint64_t *I) {
+    read();
+    if (Tok.K != Identifier || Tok.Value.getAsInteger(10, *I)) {
+      llvm::errs() << "integer expected\n";
+      return make_error_code(LLDError::InvalidOption);
+    }
+    return std::error_code();
+  }
+
+  std::error_code expect(Kind Expected, StringRef Msg) {
+    read();
+    if (Tok.K != Expected) {
+      llvm::errs() << Msg << "\n";
+      return make_error_code(LLDError::InvalidOption);
+    }
+    return std::error_code();
+  }
+
+  void unget() { Stack.push_back(Tok); }
+
+  std::error_code parseOne() {
+    read();
+    switch (Tok.K) {
+    case Eof:
+      return std::error_code();
+    case KwExports:
+      for (;;) {
+        read();
+        if (Tok.K != Identifier) {
+          unget();
+          return std::error_code();
+        }
+        if (auto EC = parseExport())
+          return EC;
+      }
+    case KwHeapsize:
+      if (auto EC = parseNumbers(&Config->HeapReserve, &Config->HeapCommit))
+        return EC;
+      return std::error_code();
+    case KwLibrary:
+      if (auto EC = parseName(&Config->OutputFile, &Config->ImageBase))
+        return EC;
+      if (!StringRef(Config->OutputFile).endswith_lower(".dll"))
+        Config->OutputFile += ".dll";
+      return std::error_code();
+    case KwStacksize:
+      if (auto EC = parseNumbers(&Config->StackReserve, &Config->StackCommit))
+        return EC;
+      return std::error_code();
+    case KwName:
+      if (auto EC = parseName(&Config->OutputFile, &Config->ImageBase))
+        return EC;
+      return std::error_code();
+    case KwVersion:
+      if (auto EC = parseVersion(&Config->MajorImageVersion,
+                                 &Config->MinorImageVersion))
+        return EC;
+      return std::error_code();
+    default:
+      llvm::errs() << "unknown directive: " << Tok.Value << "\n";
+      return make_error_code(LLDError::InvalidOption);
+    }
+  }
+
+  std::error_code parseExport() {
+    Export E;
+    E.ExtName = Tok.Value;
+    read();
+    if (Tok.K == Equal) {
+      read();
+      if (Tok.K != Identifier) {
+        llvm::errs() << "identifier expected, but got " << Tok.Value << "\n";
+        return make_error_code(LLDError::InvalidOption);
+      }
+      E.Name = Tok.Value;
+    } else {
+      unget();
+      E.Name = E.ExtName;
+    }
+
+    for (;;) {
+      read();
+      if (Tok.K == Identifier && Tok.Value[0] == '@') {
+        Tok.Value.drop_front().getAsInteger(10, E.Ordinal);
+        read();
+        if (Tok.K == KwNoname) {
+          E.Noname = true;
+        } else {
+          unget();
+        }
+        continue;
+      }
+      if (Tok.K == KwData) {
+        E.Data = true;
+        continue;
+      }
+      if (Tok.K == KwPrivate) {
+        E.Private = true;
+        continue;
+      }
+      unget();
+      Config->Exports.push_back(E);
+      return std::error_code();
+    }
+  }
+
+  // HEAPSIZE/STACKSIZE reserve[,commit]
+  std::error_code parseNumbers(uint64_t *Reserve, uint64_t *Commit) {
+    if (auto EC = readAsInt(Reserve))
+      return EC;
+    read();
+    if (Tok.K != Comma) {
+      unget();
+      Commit = 0;
+      return std::error_code();
+    }
+    if (auto EC = readAsInt(Commit))
+      return EC;
+    return std::error_code();
+  }
+
+  // NAME outputPath [BASE=address]
+  std::error_code parseName(std::string *Out, uint64_t *Baseaddr) {
+    read();
+    if (Tok.K == Identifier) {
+      *Out = Tok.Value;
+    } else {
+      *Out = "";
+      unget();
+      return std::error_code();
+    }
+    read();
+    if (Tok.K == KwBase) {
+      if (auto EC = expect(Equal, "'=' expected"))
+        return EC;
+      if (auto EC = readAsInt(Baseaddr))
+        return EC;
+    } else {
+      unget();
+      *Baseaddr = 0;
+    }
+    return std::error_code();
+  }
+
+  // VERSION major[.minor]
+  std::error_code parseVersion(uint32_t *Major, uint32_t *Minor) {
+    read();
+    if (Tok.K != Identifier) {
+      llvm::errs() << "identifier expected, but got " << Tok.Value << "\n";
+      return make_error_code(LLDError::InvalidOption);
+    }
+    StringRef V1, V2;
+    std::tie(V1, V2) = Tok.Value.split('.');
+    if (V1.getAsInteger(10, *Major)) {
+      llvm::errs() << "integer expected, but got " << Tok.Value << "\n";
+      return make_error_code(LLDError::InvalidOption);
+    }
+    if (V2.empty()) {
+      *Minor = 0;
+    } else if (V2.getAsInteger(10, *Minor)) {
+      llvm::errs() << "integer expected, but got " << Tok.Value << "\n";
+      return make_error_code(LLDError::InvalidOption);
+    }
+    return std::error_code();
+  }
+
+  Lexer Lex;
+  Token Tok;
+  std::vector<Token> Stack;
+};
+
+} // anonymous namespace
+
+std::error_code parseModuleDefs(MemoryBufferRef MB) {
+  return Parser(MB.getBuffer()).parse();
+}
+
+} // namespace coff
+} // namespace lld

Modified: lld/trunk/test/COFF/export.test
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/COFF/export.test?rev=239929&r1=239928&r2=239929&view=diff
==============================================================================
--- lld/trunk/test/COFF/export.test (original)
+++ lld/trunk/test/COFF/export.test Wed Jun 17 14:19:25 2015
@@ -39,7 +39,7 @@ CHECK3-NEXT:       4        0
 CHECK3-NEXT:       5   0x1008
 CHECK3-NEXT:       6   0x1010  exportfn2
 
-# RUN: lld -flavor link2 /out:%t.dll /dll %t.obj /export:f1=exportfn1 /export:f2 at 4=exportfn2
+# RUN: lld -flavor link2 /out:%t.dll /dll %t.obj /export:f1=exportfn1 /export:f2=exportfn2
 # RUN: llvm-objdump -p %t.dll | FileCheck -check-prefix=CHECK4 %s
 
 CHECK4:      Export Table:
@@ -49,3 +49,17 @@ CHECK4-NEXT:       0        0
 CHECK4-NEXT:       1   0x1010  exportfn3
 CHECK4-NEXT:       2   0x1008  f1
 CHECK4-NEXT:       3   0x1010  f2
+
+# RUN: echo "EXPORTS exportfn1 @3" > %t.def
+# RUN: echo "fn2=exportfn2 @2" >> %t.def
+# RUN: lld -flavor link2 /out:%t.dll /dll %t.obj /def:%t.def
+# RUN: llvm-objdump -p %t.dll | FileCheck -check-prefix=CHECK5 %s
+
+CHECK5:      Export Table:
+CHECK5:      DLL name: export.test.tmp.dll
+CHECK5:      Ordinal      RVA  Name
+CHECK5-NEXT:       0        0
+CHECK5-NEXT:       1        0
+CHECK5-NEXT:       2   0x1010  fn2
+CHECK5-NEXT:       3   0x1008  exportfn1
+CHECK5-NEXT:       4   0x1010  exportfn3

Modified: lld/trunk/test/COFF/heap.test
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/COFF/heap.test?rev=239929&r1=239928&r2=239929&view=diff
==============================================================================
--- lld/trunk/test/COFF/heap.test (original)
+++ lld/trunk/test/COFF/heap.test Wed Jun 17 14:19:25 2015
@@ -8,12 +8,18 @@ DEFAULT: SizeOfHeapCommit: 4096
 
 # RUN: lld -flavor link2 /out:%t.exe /heap:0x3000 %t.obj
 # RUN: llvm-readobj -file-headers %t.exe | FileCheck -check-prefix=CHECK1 %s
+# RUN: echo "HEAPSIZE 12288" > %t.def
+# RUN: lld -flavor link2 /out:%t.exe /def:%t.def %t.obj
+# RUN: llvm-readobj -file-headers %t.exe | FileCheck -check-prefix=CHECK1 %s
 
 CHECK1: SizeOfHeapReserve: 12288
 CHECK1: SizeOfHeapCommit: 4096
 
 # RUN: lld -flavor link2 /out:%t.exe /heap:0x5000,0x3000 %t.obj
 # RUN: llvm-readobj -file-headers %t.exe | FileCheck -check-prefix=CHECK2 %s
+# RUN: echo "HEAPSIZE 20480,12288" > %t.def
+# RUN: lld -flavor link2 /out:%t.exe /def:%t.def %t.obj
+# RUN: llvm-readobj -file-headers %t.exe | FileCheck -check-prefix=CHECK2 %s
 
 CHECK2: SizeOfHeapReserve: 20480
 CHECK2: SizeOfHeapCommit: 12288

Modified: lld/trunk/test/COFF/stack.test
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/COFF/stack.test?rev=239929&r1=239928&r2=239929&view=diff
==============================================================================
--- lld/trunk/test/COFF/stack.test (original)
+++ lld/trunk/test/COFF/stack.test Wed Jun 17 14:19:25 2015
@@ -8,12 +8,18 @@ DEFAULT: SizeOfStackCommit: 4096
 
 # RUN: lld -flavor link2 /out:%t.exe %t.obj /stack:0x3000
 # RUN: llvm-readobj -file-headers %t.exe | FileCheck -check-prefix=CHECK1 %s
+# RUN: echo "STACKSIZE 12288" > %t.def
+# RUN: lld -flavor link2 /out:%t.exe /def:%t.def %t.obj
+# RUN: llvm-readobj -file-headers %t.exe | FileCheck -check-prefix=CHECK1 %s
 
 CHECK1: SizeOfStackReserve: 12288
 CHECK1: SizeOfStackCommit: 4096
 
 # RUN: lld -flavor link2 /out:%t.exe %t.obj /stack:0x5000,0x3000
 # RUN: llvm-readobj -file-headers %t.exe | FileCheck -check-prefix=CHECK2 %s
+# RUN: echo "STACKSIZE 20480,12288" > %t.def
+# RUN: lld -flavor link2 /out:%t.exe /def:%t.def %t.obj
+# RUN: llvm-readobj -file-headers %t.exe | FileCheck -check-prefix=CHECK2 %s
 
 CHECK2: SizeOfStackReserve: 20480
 CHECK2: SizeOfStackCommit: 12288





More information about the llvm-commits mailing list