Produce a undefined reference to _GLOBAL_OFFSET_TABLE_ when needed.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@115623 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/MC/ELFObjectWriter.cpp b/lib/MC/ELFObjectWriter.cpp
index b9d2ace..b33b047 100644
--- a/lib/MC/ELFObjectWriter.cpp
+++ b/lib/MC/ELFObjectWriter.cpp
@@ -70,6 +70,17 @@
   }
 }
 
+static bool RelocNeedsGOT(unsigned Type) {
+  switch (Type) {
+  default:
+    return false;
+  case ELF::R_X86_64_GOT32:
+  case ELF::R_X86_64_PLT32:
+  case ELF::R_X86_64_GOTPCREL:
+    return true;
+  }
+}
+
 namespace {
 
   class ELFObjectWriterImpl {
@@ -133,6 +144,8 @@
 
     int NumRegularSections;
 
+    bool NeedsGOT;
+
     ELFObjectWriter *Writer;
 
     raw_ostream &OS;
@@ -153,7 +166,7 @@
   public:
     ELFObjectWriterImpl(ELFObjectWriter *_Writer, bool _Is64Bit,
                         bool _HasRelAddend, Triple::OSType _OSType)
-      : Writer(_Writer), OS(Writer->getStream()),
+      : NeedsGOT(false), Writer(_Writer), OS(Writer->getStream()),
         Is64Bit(_Is64Bit), HasRelocationAddend(_HasRelAddend),
         OSType(_OSType) {
     }
@@ -647,6 +660,9 @@
     }
   }
 
+  if (RelocNeedsGOT(Type))
+    NeedsGOT = true;
+
   ELFRelocationEntry ERE;
 
   ERE.Index = Index;
@@ -677,6 +693,14 @@
 }
 
 void ELFObjectWriterImpl::ComputeSymbolTable(MCAssembler &Asm) {
+  // FIXME: Is this the correct place to do this?
+  if (NeedsGOT) {
+    llvm::StringRef Name = "_GLOBAL_OFFSET_TABLE_";
+    MCSymbol *Sym = Asm.getContext().GetOrCreateSymbol(Name);
+    MCSymbolData &Data = Asm.getOrCreateSymbolData(*Sym);
+    Data.setExternal(true);
+  }
+
   // Build section lookup table.
   NumRegularSections = Asm.size();
   DenseMap<const MCSection*, uint8_t> SectionIndexMap;