[lld] a39b14f - [ms] Add new /PDBSTREAM option to lld-link allowing injection of streams into PDB files.

Eric Astor via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 7 13:21:01 PDT 2020


Author: Eric Astor
Date: 2020-04-07T16:19:38-04:00
New Revision: a39b14f0b42d6bd835dec448506660d77bbbb7c7

URL: https://github.com/llvm/llvm-project/commit/a39b14f0b42d6bd835dec448506660d77bbbb7c7
DIFF: https://github.com/llvm/llvm-project/commit/a39b14f0b42d6bd835dec448506660d77bbbb7c7.diff

LOG: [ms] Add new /PDBSTREAM option to lld-link allowing injection of streams into PDB files.

Summary:
/PDBSTREAM:<name>=<file> adds the contents of <file> to stream <name> in the resulting PDB.

This allows native uses with workflows that (for example) add srcsrv streams to PDB files to provide a location for the build's source files.

Results should be equivalent to linking with lld-link, then running Microsoft's pdbstr tool with the command line:
pdbstr.exe -w -p:<PDB LOCATION> -s:<name> -i:<file>
except in cases where the named stream overlaps with a default named stream, such as "/names". In those cases, the added stream will be overridden, making the /pdbstream option a no-op.

Reviewers: thakis, rnk

Reviewed By: thakis

Differential Revision: https://reviews.llvm.org/D77310

Added: 
    lld/test/COFF/Inputs/stream.txt
    lld/test/COFF/pdbstream.test

Modified: 
    lld/COFF/Config.h
    lld/COFF/Driver.cpp
    lld/COFF/Options.td
    lld/COFF/PDB.cpp

Removed: 
    


################################################################################
diff  --git a/lld/COFF/Config.h b/lld/COFF/Config.h
index 93b732ea6821..4d7d9e252875 100644
--- a/lld/COFF/Config.h
+++ b/lld/COFF/Config.h
@@ -110,6 +110,7 @@ struct Configuration {
   bool showSummary = false;
   unsigned debugTypes = static_cast<unsigned>(DebugType::None);
   std::vector<std::string> natvisFiles;
+  llvm::StringMap<std::string> namedStreams;
   llvm::SmallString<128> pdbAltPath;
   llvm::SmallString<128> pdbPath;
   llvm::SmallString<128> pdbSourcePath;

diff  --git a/lld/COFF/Driver.cpp b/lld/COFF/Driver.cpp
index 931b8cf601a7..08415fa00984 100644
--- a/lld/COFF/Driver.cpp
+++ b/lld/COFF/Driver.cpp
@@ -1273,6 +1273,14 @@ void LinkerDriver::link(ArrayRef<const char *> argsArr) {
       config->pdbAltPath = arg->getValue();
     if (args.hasArg(OPT_natvis))
       config->natvisFiles = args.getAllArgValues(OPT_natvis);
+    if (args.hasArg(OPT_pdbstream)) {
+      for (const StringRef value : args.getAllArgValues(OPT_pdbstream)) {
+        const std::pair<StringRef, StringRef> nameFile = value.split("=");
+        const StringRef name = nameFile.first;
+        const std::string file = nameFile.second.str();
+        config->namedStreams[name] = file;
+      }
+    }
 
     if (auto *arg = args.getLastArg(OPT_pdb_source_path))
       config->pdbSourcePath = arg->getValue();

diff  --git a/lld/COFF/Options.td b/lld/COFF/Options.td
index f8fe6f9efc19..a6181e2d1b8b 100644
--- a/lld/COFF/Options.td
+++ b/lld/COFF/Options.td
@@ -66,6 +66,9 @@ def no_color_diagnostics: F<"no-color-diagnostics">,
 def pdb : P<"pdb", "PDB file path">;
 def pdbstripped : P<"pdbstripped", "Stripped PDB file path">;
 def pdbaltpath : P<"pdbaltpath", "PDB file path to embed in the image">;
+def pdbstream : Joined<["/", "-", "/?", "-?"], "pdbstream:">,
+    MetaVarName<"<name>=<file>">,
+    HelpText<"Embed the contents of <file> in the PDB as named stream <name>">;
 def section : P<"section", "Specify section attributes">;
 def stack   : P<"stack", "Size of the stack">;
 def stub    : P<"stub", "Specify DOS stub file">;

diff  --git a/lld/COFF/PDB.cpp b/lld/COFF/PDB.cpp
index 8ecfdca768a9..a182ed34165d 100644
--- a/lld/COFF/PDB.cpp
+++ b/lld/COFF/PDB.cpp
@@ -100,6 +100,9 @@ class PDBLinker {
   /// Add natvis files specified on the command line.
   void addNatvisFiles();
 
+  /// Add named streams specified on the command line.
+  void addNamedStreams();
+
   /// Link CodeView from each object file in the symbol table into the PDB.
   void addObjectsToPDB();
 
@@ -1437,6 +1440,19 @@ void PDBLinker::addNatvisFiles() {
   }
 }
 
+void PDBLinker::addNamedStreams() {
+  for (const auto &streamFile : config->namedStreams) {
+    const StringRef stream = streamFile.getKey(), file = streamFile.getValue();
+    ErrorOr<std::unique_ptr<MemoryBuffer>> dataOrErr =
+        MemoryBuffer::getFile(file);
+    if (!dataOrErr) {
+      warn("Cannot open input file: " + file);
+      continue;
+    }
+    exitOnErr(builder.addNamedStream(stream, (*dataOrErr)->getBuffer()));
+  }
+}
+
 static codeview::CPUType toCodeViewMachine(COFF::MachineTypes machine) {
   switch (machine) {
   case COFF::IMAGE_FILE_MACHINE_AMD64:
@@ -1692,6 +1708,7 @@ void lld::coff::createPDB(SymbolTable *symtab,
   pdb.addImportFilesToPDB(outputSections);
   pdb.addSections(outputSections, sectionTable);
   pdb.addNatvisFiles();
+  pdb.addNamedStreams();
 
   ScopedTimer t2(diskCommitTimer);
   codeview::GUID guid;

diff  --git a/lld/test/COFF/Inputs/stream.txt b/lld/test/COFF/Inputs/stream.txt
new file mode 100644
index 000000000000..6d35debedcb8
--- /dev/null
+++ b/lld/test/COFF/Inputs/stream.txt
@@ -0,0 +1,3 @@
+Stream contents
+Line 2
+Line 3

diff  --git a/lld/test/COFF/pdbstream.test b/lld/test/COFF/pdbstream.test
new file mode 100644
index 000000000000..a44f54892d70
--- /dev/null
+++ b/lld/test/COFF/pdbstream.test
@@ -0,0 +1,8 @@
+# RUN: yaml2obj %p/Inputs/empty.yaml > %t.obj
+
+# RUN: lld-link /entry:main %t.obj /out:%t.exe /debug /pdbstream:srcsrv=%p/Inputs/stream.txt
+# RUN: llvm-pdbutil export -stream=srcsrv -out=- %t.pdb | FileCheck %s
+
+CHECK: Stream contents
+CHECK-NEXT: Line 2
+CHECK-NEXT: Line 3


        


More information about the llvm-commits mailing list