[ELF] - Linkerscript: implement DEFINED() command.

DEFINED(symbol)
Return 1 if symbol is in the linker global symbol table and is defined before
the statement using DEFINED in the script, otherwise return 0.

Can be used to define default values for symbols. Found it in the wild.

Differential revision: https://reviews.llvm.org/D24858

llvm-svn: 282245
diff --git a/lld/ELF/LinkerScript.cpp b/lld/ELF/LinkerScript.cpp
index 1b240f4..0febb25 100644
--- a/lld/ELF/LinkerScript.cpp
+++ b/lld/ELF/LinkerScript.cpp
@@ -756,6 +756,10 @@
   return 0;
 }
 
+template <class ELFT> bool LinkerScript<ELFT>::isDefined(StringRef S) {
+  return Symtab<ELFT>::X->find(S) != nullptr;
+}
+
 // Returns indices of ELF headers containing specific section, identified
 // by Name. Each index is a zero based number of ELF header listed within
 // PHDRS {} script block.
@@ -1490,6 +1494,14 @@
     expect(")");
     return [=](uint64_t Dot) { return getConstant(Tok); };
   }
+  if (Tok == "DEFINED") {
+    expect("(");
+    StringRef Tok = next();
+    expect(")");
+    return [=](uint64_t Dot) {
+      return ScriptBase->isDefined(Tok) ? 1 : 0;
+    };
+  }
   if (Tok == "SEGMENT_START") {
     expect("(");
     next();