Implement the -dynamic-linker option.

With this dynamic executables can be executed with just ./t instead of
/lib64/ld-2.20.so ./t

llvm-svn: 247446
diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp
index 5dc04c2..3dac96c 100644
--- a/lld/ELF/Writer.cpp
+++ b/lld/ELF/Writer.cpp
@@ -111,6 +111,20 @@
 
 namespace {
 template <bool Is64Bits>
+class InterpSection final : public OutputSectionBase<Is64Bits> {
+public:
+  InterpSection()
+      : OutputSectionBase<Is64Bits>(".interp", SHT_PROGBITS, SHF_ALLOC) {
+    this->Header.sh_size = Config->DynamicLinker.size() + 1;
+    this->Header.sh_addralign = 1;
+  }
+
+  void writeTo(uint8_t *Buf) override {
+    memcpy(Buf, Config->DynamicLinker.data(), Config->DynamicLinker.size());
+  }
+};
+
+template <bool Is64Bits>
 class StringTableSection final : public OutputSectionBase<Is64Bits> {
 public:
   typedef typename OutputSectionBase<Is64Bits>::uintX_t uintX_t;
@@ -169,7 +183,7 @@
 
   void writeTo(uint8_t *Buf) override;
 
-  const SymbolTable &getSymTable() { return Table; }
+  const SymbolTable &getSymTable() const { return Table; }
 
   void addSymbol(StringRef Name) {
     StrTabSec.add(Name);
@@ -280,6 +294,10 @@
   void openFile(StringRef OutputPath);
   void writeHeader();
   void writeSections();
+  bool needsInterpSection() const {
+    return !SymTabSec.getSymTable().getSharedFiles().empty() &&
+           !Config->DynamicLinker.empty();
+  }
 
   std::unique_ptr<llvm::FileOutputBuffer> Buffer;
   llvm::SpecificBumpPtrAllocator<OutputSection<ELFT>> CAlloc;
@@ -299,6 +317,8 @@
 
   DynamicSection<ELFT> DynamicSec;
 
+  InterpSection<ELFT::Is64Bits> InterpSec;
+
   OutputSection<ELFT> *BSSSec = nullptr;
 };
 } // anonymous namespace
@@ -611,6 +631,8 @@
   OutputSections.push_back(&StrTabSec);
 
   if (!SharedFiles.empty()) {
+    if (needsInterpSection())
+      OutputSections.push_back(&InterpSec);
     OutputSections.push_back(&DynSymSec);
     OutputSections.push_back(&DynamicSec);
     OutputSections.push_back(&DynStrSec);
@@ -642,6 +664,11 @@
   VA = RoundUpToAlignment(VA, PageSize);
 
   NumPhdrs = 0;
+
+  // Add a PHDR for PT_INTERP.
+  if (needsInterpSection())
+    ++NumPhdrs;
+
   // Add a PHDR for the elf header and program headers. Some dynamic linkers
   // (musl at least) require them to be covered by a PT_LOAD.
   ++NumPhdrs;
@@ -692,6 +719,18 @@
   return Ret;
 }
 
+template <class ELFT>
+static void setValuesFromSection(typename ELFFile<ELFT>::Elf_Phdr &P,
+                                 OutputSectionBase<ELFT::Is64Bits> &S) {
+  P.p_flags = convertSectionFlagsToPHDRFlags(S.getFlags());
+  P.p_offset = S.getFileOff();
+  P.p_vaddr = S.getVA();
+  P.p_paddr = P.p_vaddr;
+  P.p_filesz = S.getSize();
+  P.p_memsz = P.p_filesz;
+  P.p_align = S.getAlign();
+}
+
 template <class ELFT> void Writer<ELFT>::writeHeader() {
   uint8_t *Buf = Buffer->getBufferStart();
   auto *EHdr = reinterpret_cast<Elf_Ehdr *>(Buf);
@@ -726,6 +765,12 @@
   EHdr->e_shstrndx = StrTabSec.getSectionIndex();
 
   auto PHdrs = reinterpret_cast<Elf_Phdr *>(Buf + EHdr->e_phoff);
+  if (needsInterpSection()) {
+    PHdrs->p_type = PT_INTERP;
+    setValuesFromSection<ELFT>(*PHdrs, InterpSec);
+    ++PHdrs;
+  }
+
   PHdrs->p_type = PT_LOAD;
   PHdrs->p_flags = PF_R;
   PHdrs->p_offset = 0;
@@ -752,13 +797,7 @@
 
   if (HasDynamicSegment) {
     PHdrs->p_type = PT_DYNAMIC;
-    PHdrs->p_flags = convertSectionFlagsToPHDRFlags(DynamicSec.getFlags());
-    PHdrs->p_offset = DynamicSec.getFileOff();
-    PHdrs->p_vaddr = DynamicSec.getVA();
-    PHdrs->p_paddr = PHdrs->p_vaddr;
-    PHdrs->p_filesz = DynamicSec.getSize();
-    PHdrs->p_memsz = DynamicSec.getSize();
-    PHdrs->p_align = DynamicSec.getAlign();
+    setValuesFromSection<ELFT>(*PHdrs, DynamicSec);
   }
 
   auto SHdrs = reinterpret_cast<Elf_Shdr *>(Buf + EHdr->e_shoff);