[lld] r254796 - COFF: Create a PDB file with the correct file signature.

Rui Ueyama via llvm-commits llvm-commits at lists.llvm.org
Fri Dec 4 15:11:05 PST 2015


Author: ruiu
Date: Fri Dec  4 17:11:05 2015
New Revision: 254796

URL: http://llvm.org/viewvc/llvm-project?rev=254796&view=rev
Log:
COFF: Create a PDB file with the correct file signature.

Before this patch, we created an empty PDB file if /debug option is
specified. For MSVC linker, such PDB file is completely broken, and
linker exits without doing anything as soon as it finds an empty PDB
file.

A PDB file created in this patch has the correct file signature.
MSVC linker still thinks that the file is broken, but it then removes
and replaces with its output.

This is an initial patch to support PDB in LLD. We aim to support
PDB in order to make it 100% compatible with MSVC linker. PDB support
is the last missing piece.

Added:
    lld/trunk/COFF/PDB.cpp
Modified:
    lld/trunk/COFF/CMakeLists.txt
    lld/trunk/COFF/Driver.cpp
    lld/trunk/COFF/Driver.h

Modified: lld/trunk/COFF/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/COFF/CMakeLists.txt?rev=254796&r1=254795&r2=254796&view=diff
==============================================================================
--- lld/trunk/COFF/CMakeLists.txt (original)
+++ lld/trunk/COFF/CMakeLists.txt Fri Dec  4 17:11:05 2015
@@ -12,6 +12,7 @@ add_llvm_library(lldCOFF
   InputFiles.cpp
   MarkLive.cpp
   ModuleDef.cpp
+  PDB.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=254796&r1=254795&r2=254796&view=diff
==============================================================================
--- lld/trunk/COFF/Driver.cpp (original)
+++ lld/trunk/COFF/Driver.cpp Fri Dec  4 17:11:05 2015
@@ -648,7 +648,7 @@ void LinkerDriver::link(llvm::ArrayRef<c
 
   // Create a dummy PDB file to satisfy build sytem rules.
   if (auto *Arg = Args.getLastArg(OPT_pdb))
-    touchFile(Arg->getValue());
+    createPDB(Arg->getValue());
 
   // Identify unreferenced COMDAT sections.
   if (Config->DoGC)

Modified: lld/trunk/COFF/Driver.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/COFF/Driver.h?rev=254796&r1=254795&r2=254796&view=diff
==============================================================================
--- lld/trunk/COFF/Driver.h (original)
+++ lld/trunk/COFF/Driver.h Fri Dec  4 17:11:05 2015
@@ -164,6 +164,7 @@ std::unique_ptr<MemoryBuffer>
 convertResToCOFF(const std::vector<MemoryBufferRef> &MBs);
 
 void touchFile(StringRef Path);
+void createPDB(StringRef Path);
 
 // Create enum with OPT_xxx values for each option in Options.td
 enum {

Added: lld/trunk/COFF/PDB.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/COFF/PDB.cpp?rev=254796&view=auto
==============================================================================
--- lld/trunk/COFF/PDB.cpp (added)
+++ lld/trunk/COFF/PDB.cpp Fri Dec  4 17:11:05 2015
@@ -0,0 +1,32 @@
+//===- PDB.cpp ------------------------------------------------------------===//
+//
+//                             The LLVM Linker
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "Driver.h"
+#include "Error.h"
+#include "Symbols.h"
+#include "llvm/Support/FileOutputBuffer.h"
+#include <memory>
+
+using namespace llvm;
+
+const int PageSize = 4096;
+const uint8_t Magic[32] = "Microsoft C/C++ MSF 7.00\r\n\032DS\0\0";
+
+void lld::coff::createPDB(StringRef Path) {
+  // Create a file.
+  size_t FileSize = PageSize * 3;
+  ErrorOr<std::unique_ptr<FileOutputBuffer>> BufOrErr =
+      FileOutputBuffer::create(Path, FileSize);
+  error(BufOrErr, Twine("failed to open ") + Path);
+  std::unique_ptr<FileOutputBuffer> Buf = std::move(*BufOrErr);
+
+  // Write the file magic.
+  uint8_t *P = Buf->getBufferStart();
+  memcpy(P, Magic, sizeof(Magic));
+}




More information about the llvm-commits mailing list