[JITLink] Refer to FDE's CIE (not the most recent CIE) when parsing eh-frame.

Frame Descriptor Entries (FDEs) have a pointer back to a Common Information
Entry (CIE) that describes how the rest FDE should be parsed. JITLink had been
assuming that FDEs always referred to the most recent CIE encountered, but the
spec allows them to point back to any previously encountered CIE. This patch
fixes JITLink to look up the correct CIE for the FDE.

The testcase is a MachO binary with an FDE that refers to a CIE that is not the
one immediately proceeding it (the layout can be viewed wit
'dwarfdump --eh-frame <testcase>'. This test case had to be a binary as llvm-mc
now sorts FDEs (as of r356216) to ensure FDEs *do* point to the most recent CIE.

llvm-svn: 359105
diff --git a/llvm/tools/llvm-jitlink/llvm-jitlink.cpp b/llvm/tools/llvm-jitlink/llvm-jitlink.cpp
index f3333f2..29516f0 100644
--- a/llvm/tools/llvm-jitlink/llvm-jitlink.cpp
+++ b/llvm/tools/llvm-jitlink/llvm-jitlink.cpp
@@ -326,14 +326,37 @@
 
 } // end namespace llvm
 
+Triple getFirstFileTriple() {
+  assert(!InputFiles.empty() && "InputFiles can not be empty");
+  auto ObjBuffer =
+      ExitOnErr(errorOrToExpected(MemoryBuffer::getFile(InputFiles.front())));
+  auto Obj = ExitOnErr(
+      object::ObjectFile::createObjectFile(ObjBuffer->getMemBufferRef()));
+  return Obj->makeTriple();
+}
+
+void setEntryPointNameIfNotProvided(const Session &S) {
+  if (EntryPointName.empty()) {
+    if (S.TT.getObjectFormat() == Triple::MachO)
+      EntryPointName = "_main";
+    else
+      EntryPointName = "main";
+  }
+}
+
 Error loadProcessSymbols(Session &S) {
   std::string ErrMsg;
   if (sys::DynamicLibrary::LoadLibraryPermanently(nullptr, &ErrMsg))
     return make_error<StringError>(std::move(ErrMsg), inconvertibleErrorCode());
 
   char GlobalPrefix = S.TT.getObjectFormat() == Triple::MachO ? '_' : '\0';
-  S.ES.getMainJITDylib().setGenerator(ExitOnErr(
-      orc::DynamicLibrarySearchGenerator::GetForCurrentProcess(GlobalPrefix)));
+  auto InternedEntryPointName = S.ES.intern(EntryPointName);
+  auto FilterMainEntryPoint = [InternedEntryPointName](SymbolStringPtr Name) {
+    return Name != InternedEntryPointName;
+  };
+  S.ES.getMainJITDylib().setGenerator(
+      ExitOnErr(orc::DynamicLibrarySearchGenerator::GetForCurrentProcess(
+          GlobalPrefix, FilterMainEntryPoint)));
 
   return Error::success();
 }
@@ -352,15 +375,6 @@
   return Error::success();
 }
 
-Triple getFirstFileTriple() {
-  assert(!InputFiles.empty() && "InputFiles can not be empty");
-  auto ObjBuffer =
-      ExitOnErr(errorOrToExpected(MemoryBuffer::getFile(InputFiles.front())));
-  auto Obj = ExitOnErr(
-      object::ObjectFile::createObjectFile(ObjBuffer->getMemBufferRef()));
-  return Obj->makeTriple();
-}
-
 Error loadObjects(Session &S) {
 
   std::map<unsigned, JITDylib *> IdxToJLD;
@@ -542,16 +556,6 @@
 }
 
 static Expected<JITEvaluatedSymbol> getMainEntryPoint(Session &S) {
-
-  // First, if the entry point has not been set, set it to a sensible default
-  // for this process.
-  if (EntryPointName.empty()) {
-    if (S.TT.getObjectFormat() == Triple::MachO)
-      EntryPointName = "_main";
-    else
-      EntryPointName = "main";
-  }
-
   return S.ES.lookup(S.JDSearchOrder, EntryPointName);
 }
 
@@ -584,6 +588,8 @@
 
   Session S(getFirstFileTriple());
 
+  setEntryPointNameIfNotProvided(S);
+
   if (!NoProcessSymbols)
     ExitOnErr(loadProcessSymbols(S));
   ExitOnErr(loadDylibs());