[lld] r311930 - Keep an instance of COFFOptTable alive as long as InputArgList is alive.

Rui Ueyama via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 28 13:46:30 PDT 2017


Author: ruiu
Date: Mon Aug 28 13:46:30 2017
New Revision: 311930

URL: http://llvm.org/viewvc/llvm-project?rev=311930&view=rev
Log:
Keep an instance of COFFOptTable alive as long as InputArgList is alive.

Summary:
ArgParser created an instance of COFFOptTable on stack to use it to
parser command line arguments. Parsed arguments were then returned from
the function as InputArgList. This was safe because InputArgList referred
only statically-allocated InfoTable.

That is not a safe assumption after https://reviews.llvm.org/D36782,
which changes the type of its internal table from ArrayRef to std::vector.
To make lld work with that patch, we need to keep an instance of
COFFOptTable at least as long as an InputArgList is alive. This patch
does that.

Reviewers: yamaguchi

Subscribers: llvm-commits

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

Modified:
    lld/trunk/COFF/Driver.cpp
    lld/trunk/COFF/Driver.h
    lld/trunk/COFF/DriverUtils.cpp

Modified: lld/trunk/COFF/Driver.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/COFF/Driver.cpp?rev=311930&r1=311929&r2=311930&view=diff
==============================================================================
--- lld/trunk/COFF/Driver.cpp (original)
+++ lld/trunk/COFF/Driver.cpp Mon Aug 28 13:46:30 2017
@@ -197,6 +197,7 @@ static bool isDecorated(StringRef Sym) {
 // Parses .drectve section contents and returns a list of files
 // specified by /defaultlib.
 void LinkerDriver::parseDirectives(StringRef S) {
+  ArgParser Parser;
   opt::InputArgList Args = Parser.parse(S);
 
   for (auto *Arg : Args) {
@@ -691,6 +692,7 @@ void LinkerDriver::link(ArrayRef<const c
   InitializeAllDisassemblers();
 
   // Parse command line options.
+  ArgParser Parser;
   opt::InputArgList Args = Parser.parseLINK(ArgsArr.slice(1));
 
   // Parse and evaluate -mllvm options.

Modified: lld/trunk/COFF/Driver.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/COFF/Driver.h?rev=311930&r1=311929&r2=311930&view=diff
==============================================================================
--- lld/trunk/COFF/Driver.h (original)
+++ lld/trunk/COFF/Driver.h Mon Aug 28 13:46:30 2017
@@ -41,6 +41,11 @@ void markLive(const std::vector<Chunk *>
 // Implemented in ICF.cpp.
 void doICF(const std::vector<Chunk *> &Chunks);
 
+class COFFOptTable : public llvm::opt::OptTable {
+public:
+  COFFOptTable();
+};
+
 class ArgParser {
 public:
   // Parses command line options.
@@ -54,8 +59,9 @@ public:
 
 private:
   std::vector<const char *> tokenize(StringRef S);
-
   std::vector<const char *> replaceResponseFiles(std::vector<const char *>);
+
+  COFFOptTable Table;
 };
 
 class LinkerDriver {
@@ -71,7 +77,6 @@ public:
                             StringRef ParentName);
 
 private:
-  ArgParser Parser;
   SymbolTable Symtab;
 
   std::unique_ptr<llvm::TarWriter> Tar; // for /linkrepro

Modified: lld/trunk/COFF/DriverUtils.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/COFF/DriverUtils.cpp?rev=311930&r1=311929&r2=311930&view=diff
==============================================================================
--- lld/trunk/COFF/DriverUtils.cpp (original)
+++ lld/trunk/COFF/DriverUtils.cpp Mon Aug 28 13:46:30 2017
@@ -716,10 +716,7 @@ static const llvm::opt::OptTable::Info I
 #undef OPTION
 };
 
-class COFFOptTable : public llvm::opt::OptTable {
-public:
-  COFFOptTable() : OptTable(InfoTable, true) {}
-};
+COFFOptTable::COFFOptTable() : OptTable(InfoTable, true) {}
 
 // Parses a given list of options.
 opt::InputArgList ArgParser::parse(ArrayRef<const char *> ArgsArr) {
@@ -727,7 +724,6 @@ opt::InputArgList ArgParser::parse(Array
   std::vector<const char *> Argv = replaceResponseFiles(ArgsArr);
 
   // Make InputArgList from string vectors.
-  COFFOptTable Table;
   unsigned MissingIndex;
   unsigned MissingCount;
   opt::InputArgList Args = Table.ParseArgs(Argv, MissingIndex, MissingCount);




More information about the llvm-commits mailing list