[lld] r239212 - COFF: Read linker directives from bitcode files.

Peter Collingbourne peter at pcc.me.uk
Fri Jun 5 19:00:45 PDT 2015


Author: pcc
Date: Fri Jun  5 21:00:45 2015
New Revision: 239212

URL: http://llvm.org/viewvc/llvm-project?rev=239212&view=rev
Log:
COFF: Read linker directives from bitcode files.

Differential Revision: http://reviews.llvm.org/D10285

Added:
    lld/trunk/test/COFF/lto-linker-opts.ll
Modified:
    lld/trunk/COFF/InputFiles.cpp
    lld/trunk/COFF/InputFiles.h
    lld/trunk/COFF/SymbolTable.cpp
    lld/trunk/COFF/SymbolTable.h

Modified: lld/trunk/COFF/InputFiles.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/COFF/InputFiles.cpp?rev=239212&r1=239211&r2=239212&view=diff
==============================================================================
--- lld/trunk/COFF/InputFiles.cpp (original)
+++ lld/trunk/COFF/InputFiles.cpp Fri Jun  5 21:00:45 2015
@@ -263,6 +263,23 @@ std::error_code BitcodeFile::parse() {
       SymbolBodies.push_back(new (Alloc) DefinedBitcode(SymName));
     }
   }
+
+  // Extract any linker directives from the bitcode file, which are represented
+  // as module flags with the key "Linker Options".
+  llvm::SmallVector<llvm::Module::ModuleFlagEntry, 8> Flags;
+  M->getModule().getModuleFlagsMetadata(Flags);
+  for (auto &&Flag : Flags) {
+    if (Flag.Key->getString() != "Linker Options")
+      continue;
+
+    for (llvm::Metadata *Op : cast<llvm::MDNode>(Flag.Val)->operands()) {
+      for (llvm::Metadata *InnerOp : cast<llvm::MDNode>(Op)->operands()) {
+        Directives += " ";
+        Directives += cast<llvm::MDString>(InnerOp)->getString();
+      }
+    }
+  }
+
   return std::error_code();
 }
 

Modified: lld/trunk/COFF/InputFiles.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/COFF/InputFiles.h?rev=239212&r1=239211&r2=239212&view=diff
==============================================================================
--- lld/trunk/COFF/InputFiles.h (original)
+++ lld/trunk/COFF/InputFiles.h Fri Jun  5 21:00:45 2015
@@ -170,6 +170,9 @@ public:
   LTOModule *getModule() const { return M.get(); }
   LTOModule *releaseModule() { return M.release(); }
 
+  // Returns linker directives from module flags metadata if present.
+  StringRef getDirectives() { return Directives; }
+
 private:
   std::error_code parse() override;
 
@@ -177,6 +180,7 @@ private:
   std::vector<SymbolBody *> SymbolBodies;
   llvm::BumpPtrAllocator Alloc;
   std::unique_ptr<LTOModule> M;
+  std::string Directives;
 };
 
 } // namespace coff

Modified: lld/trunk/COFF/SymbolTable.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/COFF/SymbolTable.cpp?rev=239212&r1=239211&r2=239212&view=diff
==============================================================================
--- lld/trunk/COFF/SymbolTable.cpp (original)
+++ lld/trunk/COFF/SymbolTable.cpp Fri Jun  5 21:00:45 2015
@@ -40,6 +40,17 @@ std::error_code SymbolTable::addFile(std
   return addImport(cast<ImportFile>(FileP));
 }
 
+std::error_code SymbolTable::addDirectives(StringRef Dir) {
+  if (Dir.empty())
+    return std::error_code();
+  std::vector<std::unique_ptr<InputFile>> Libs;
+  if (auto EC = Driver->parseDirectives(Dir, &Libs))
+    return EC;
+  for (std::unique_ptr<InputFile> &Lib : Libs)
+    addFile(std::move(Lib));
+  return std::error_code();
+}
+
 std::error_code SymbolTable::addObject(ObjectFile *File) {
   ObjectFiles.emplace_back(File);
   for (SymbolBody *Body : File->getSymbols())
@@ -49,15 +60,7 @@ std::error_code SymbolTable::addObject(O
 
   // If an object file contains .drectve section, read it and add
   // files listed in the section.
-  StringRef Dir = File->getDirectives();
-  if (!Dir.empty()) {
-    std::vector<std::unique_ptr<InputFile>> Libs;
-    if (auto EC = Driver->parseDirectives(Dir, &Libs))
-      return EC;
-    for (std::unique_ptr<InputFile> &Lib : Libs)
-      addFile(std::move(Lib));
-  }
-  return std::error_code();
+  return addDirectives(File->getDirectives());
 }
 
 std::error_code SymbolTable::addArchive(ArchiveFile *File) {
@@ -75,8 +78,8 @@ std::error_code SymbolTable::addBitcode(
       if (auto EC = resolve(Body))
         return EC;
 
-  // TODO: Handle linker directives.
-  return std::error_code();
+  // Add any linker directives from the module flags metadata.
+  return addDirectives(File->getDirectives());
 }
 
 std::error_code SymbolTable::addImport(ImportFile *File) {

Modified: lld/trunk/COFF/SymbolTable.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/COFF/SymbolTable.h?rev=239212&r1=239211&r2=239212&view=diff
==============================================================================
--- lld/trunk/COFF/SymbolTable.h (original)
+++ lld/trunk/COFF/SymbolTable.h Fri Jun  5 21:00:45 2015
@@ -77,6 +77,8 @@ public:
   std::error_code rename(StringRef From, StringRef To);
 
 private:
+  std::error_code addDirectives(StringRef Dir);
+
   std::error_code addObject(ObjectFile *File);
   std::error_code addArchive(ArchiveFile *File);
   std::error_code addImport(ImportFile *File);

Added: lld/trunk/test/COFF/lto-linker-opts.ll
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/COFF/lto-linker-opts.ll?rev=239212&view=auto
==============================================================================
--- lld/trunk/test/COFF/lto-linker-opts.ll (added)
+++ lld/trunk/test/COFF/lto-linker-opts.ll Fri Jun  5 21:00:45 2015
@@ -0,0 +1,11 @@
+; RUN: llvm-as -o %T/lto-linker-opts.obj %s
+; RUN: env LIB=%S/Inputs lld -flavor link2 /out:%T/lto-linker-opts.exe /entry:main /subsystem:console %T/lto-linker-opts.obj
+
+target datalayout = "e-m:w-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-pc-windows-msvc"
+
+!llvm.module.flags = !{!0}
+
+!0 = !{i32 6, !"Linker Options", !1}
+!1 = !{!2}
+!2 = !{!"/DEFAULTLIB:ret42.lib"}





More information about the llvm-commits mailing list