ELF2: LinkerScript: Implement INCLUDE directive.

llvm-svn: 249960
diff --git a/lld/ELF/LinkerScript.cpp b/lld/ELF/LinkerScript.cpp
index cf1b646..34fda2d 100644
--- a/lld/ELF/LinkerScript.cpp
+++ b/lld/ELF/LinkerScript.cpp
@@ -39,12 +39,14 @@
   void readAsNeeded();
   void readEntry();
   void readGroup();
+  void readInclude();
   void readOutput();
   void readOutputFormat();
   void readSearchDir();
 
   std::vector<StringRef> Tokens;
   size_t Pos = 0;
+  static std::vector<std::unique_ptr<MemoryBuffer>> OwningMBs;
 };
 }
 
@@ -55,6 +57,8 @@
       readEntry();
     } else if (Tok == "GROUP") {
       readGroup();
+    } else if (Tok == "INCLUDE") {
+      readInclude();
     } else if (Tok == "OUTPUT") {
       readOutput();
     } else if (Tok == "OUTPUT_FORMAT") {
@@ -160,6 +164,16 @@
   }
 }
 
+void LinkerScript::readInclude() {
+  StringRef Tok = next();
+  auto MBOrErr = MemoryBuffer::getFile(Tok);
+  error(MBOrErr, Twine("cannot open ") + Tok);
+  std::unique_ptr<MemoryBuffer> &MB = *MBOrErr;
+  std::vector<StringRef> V = tokenize(MB->getMemBufferRef().getBuffer());
+  Tokens.insert(Tokens.begin() + Pos, V.begin(), V.end());
+  OwningMBs.push_back(std::move(MB)); // keep ownership of MB
+}
+
 void LinkerScript::readOutput() {
   // -o <file> takes predecence over OUTPUT(<file>).
   expect("(");
@@ -182,6 +196,8 @@
   expect(")");
 }
 
+std::vector<std::unique_ptr<MemoryBuffer>> LinkerScript::OwningMBs;
+
 // Entry point. The other functions or classes are private to this file.
 void lld::elf2::readLinkerScript(MemoryBufferRef MB) {
   LinkerScript(MB.getBuffer()).run();