ELF2: Implement OUTPUT() linker script directive.

llvm-svn: 249491
diff --git a/lld/ELF/Config.h b/lld/ELF/Config.h
index 4a4aab4..e1c2f75 100644
--- a/lld/ELF/Config.h
+++ b/lld/ELF/Config.h
@@ -22,7 +22,7 @@
   llvm::StringRef Entry;
   llvm::StringRef Fini = "_fini";
   llvm::StringRef Init = "_init";
-  llvm::StringRef OutputFile = "a.out";
+  llvm::StringRef OutputFile;
   llvm::StringRef SoName;
   llvm::StringRef Sysroot;
   std::string RPath;
diff --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp
index e2c4480..fb76f6a 100644
--- a/lld/ELF/Driver.cpp
+++ b/lld/ELF/Driver.cpp
@@ -167,6 +167,9 @@
   for (auto *Arg : Args.filtered(OPT_undefined))
     Symtab.addUndefinedSym(Arg->getValue());
 
+  if (Config->OutputFile.empty())
+    Config->OutputFile = "a.out";
+
   // Write the result.
   const ELFFileBase *FirstObj = Symtab.getFirstELF();
   switch (FirstObj->getELFKind()) {
diff --git a/lld/ELF/LinkerScript.cpp b/lld/ELF/LinkerScript.cpp
index 44c045a..474801f 100644
--- a/lld/ELF/LinkerScript.cpp
+++ b/lld/ELF/LinkerScript.cpp
@@ -38,6 +38,7 @@
 
   void readAsNeeded();
   void readGroup();
+  void readOutput();
   void readOutputFormat();
 
   std::vector<StringRef> Tokens;
@@ -50,6 +51,8 @@
     StringRef Tok = next();
     if (Tok == "GROUP") {
       readGroup();
+    } else if (Tok == "OUTPUT") {
+      readOutput();
     } else if (Tok == "OUTPUT_FORMAT") {
       readOutputFormat();
     } else {
@@ -142,6 +145,15 @@
   }
 }
 
+void LinkerScript::readOutput() {
+  // -o <file> takes predecence over OUTPUT(<file>).
+  expect("(");
+  StringRef Tok = next();
+  if (Config->OutputFile.empty())
+    Config->OutputFile = Tok;
+  expect(")");
+}
+
 void LinkerScript::readOutputFormat() {
   // Error checking only for now.
   expect("(");
diff --git a/lld/test/elf2/linkerscript.s b/lld/test/elf2/linkerscript.s
index ee2fb1d..4170f99 100644
--- a/lld/test/elf2/linkerscript.s
+++ b/lld/test/elf2/linkerscript.s
@@ -18,6 +18,11 @@
 # RUN: lld -flavor gnu2 -o %t2 %t.script
 # RUN: llvm-readobj %t2 > /dev/null
 
+# RUN: rm -f %t.out
+# RUN: echo "OUTPUT(" %t.out ")" > %t.script
+# RUN: lld -flavor gnu2 %t.script %t
+# RUN: llvm-readobj %t.out > /dev/null
+
 # RUN: echo "FOO(BAR)" > %t.script
 # RUN: not lld -flavor gnu2 -o foo %t.script > %t.log 2>&1
 # RUN: FileCheck -check-prefix=ERR1 %s < %t.log