diff lib/ExecutionEngine/RuntimeDyld/RuntimeDyldMachO.cpp @ 120:1172e4bd9c6f

update 4.0.0
author mir3636
date Fri, 25 Nov 2016 19:14:25 +0900
parents 7d135dc70f03
children 803732b1fca8
line wrap: on
line diff
--- a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldMachO.cpp	Tue Jan 26 22:56:36 2016 +0900
+++ b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldMachO.cpp	Fri Nov 25 19:14:25 2016 +0900
@@ -50,7 +50,8 @@
   return static_cast<int64_t>(readBytesUnaligned(Src, NumBytes));
 }
 
-relocation_iterator RuntimeDyldMachO::processScatteredVANILLA(
+Expected<relocation_iterator>
+RuntimeDyldMachO::processScatteredVANILLA(
                           unsigned SectionID, relocation_iterator RelI,
                           const ObjectFile &BaseObjT,
                           RuntimeDyldMachO::ObjSectionToIDMap &ObjSectionToID) {
@@ -74,8 +75,12 @@
   uint64_t SectionBaseAddr = TargetSI->getAddress();
   SectionRef TargetSection = *TargetSI;
   bool IsCode = TargetSection.isText();
-  uint32_t TargetSectionID =
-    findOrEmitSection(Obj, TargetSection, IsCode, ObjSectionToID);
+  uint32_t TargetSectionID = ~0U;
+  if (auto TargetSectionIDOrErr =
+        findOrEmitSection(Obj, TargetSection, IsCode, ObjSectionToID))
+    TargetSectionID = *TargetSectionIDOrErr;
+  else
+    return TargetSectionIDOrErr.takeError();
 
   Addend -= SectionBaseAddr;
   RelocationEntry R(SectionID, Offset, RelocType, Addend, IsPCRel, Size);
@@ -86,7 +91,8 @@
 }
 
 
-RelocationValueRef RuntimeDyldMachO::getRelocationValueRef(
+Expected<RelocationValueRef>
+RuntimeDyldMachO::getRelocationValueRef(
     const ObjectFile &BaseTObj, const relocation_iterator &RI,
     const RelocationEntry &RE, ObjSectionToIDMap &ObjSectionToID) {
 
@@ -99,10 +105,11 @@
   bool IsExternal = Obj.getPlainRelocationExternal(RelInfo);
   if (IsExternal) {
     symbol_iterator Symbol = RI->getSymbol();
-    ErrorOr<StringRef> TargetNameOrErr = Symbol->getName();
-    if (std::error_code EC = TargetNameOrErr.getError())
-      report_fatal_error(EC.message());
-    StringRef TargetName = *TargetNameOrErr;
+    StringRef TargetName;
+    if (auto TargetNameOrErr = Symbol->getName())
+      TargetName = *TargetNameOrErr;
+    else
+      return TargetNameOrErr.takeError();
     RTDyldSymbolTable::const_iterator SI =
       GlobalSymbolTable.find(TargetName.data());
     if (SI != GlobalSymbolTable.end()) {
@@ -116,7 +123,11 @@
   } else {
     SectionRef Sec = Obj.getAnyRelocationSection(RelInfo);
     bool IsCode = Sec.isText();
-    Value.SectionID = findOrEmitSection(Obj, Sec, IsCode, ObjSectionToID);
+    if (auto SectionIDOrErr = findOrEmitSection(Obj, Sec, IsCode,
+                                                ObjSectionToID))
+      Value.SectionID = *SectionIDOrErr;
+    else
+      return SectionIDOrErr.takeError();
     uint64_t Addr = Sec.getAddress();
     Value.Offset = RE.Addend - Addr;
   }
@@ -164,7 +175,7 @@
 
 
 // Populate __pointers section.
-void RuntimeDyldMachO::populateIndirectSymbolPointersSection(
+Error RuntimeDyldMachO::populateIndirectSymbolPointersSection(
                                                     const MachOObjectFile &Obj,
                                                     const SectionRef &PTSection,
                                                     unsigned PTSectionID) {
@@ -191,10 +202,11 @@
     unsigned SymbolIndex =
       Obj.getIndirectSymbolTableEntry(DySymTabCmd, FirstIndirectSymbol + i);
     symbol_iterator SI = Obj.getSymbolByIndex(SymbolIndex);
-    ErrorOr<StringRef> IndirectSymbolNameOrErr = SI->getName();
-    if (std::error_code EC = IndirectSymbolNameOrErr.getError())
-      report_fatal_error(EC.message());
-    StringRef IndirectSymbolName = *IndirectSymbolNameOrErr;
+    StringRef IndirectSymbolName;
+    if (auto IndirectSymbolNameOrErr = SI->getName())
+      IndirectSymbolName = *IndirectSymbolNameOrErr;
+    else
+      return IndirectSymbolNameOrErr.takeError();
     DEBUG(dbgs() << "  " << IndirectSymbolName << ": index " << SymbolIndex
           << ", PT offset: " << PTEntryOffset << "\n");
     RelocationEntry RE(PTSectionID, PTEntryOffset,
@@ -202,6 +214,7 @@
     addRelocationForSymbol(RE, IndirectSymbolName);
     PTEntryOffset += PTEntrySize;
   }
+  return Error::success();
 }
 
 bool RuntimeDyldMachO::isCompatibleFile(const object::ObjectFile &Obj) const {
@@ -209,8 +222,9 @@
 }
 
 template <typename Impl>
-void RuntimeDyldMachOCRTPBase<Impl>::finalizeLoad(const ObjectFile &Obj,
-                                                  ObjSectionToIDMap &SectionMap) {
+Error
+RuntimeDyldMachOCRTPBase<Impl>::finalizeLoad(const ObjectFile &Obj,
+                                             ObjSectionToIDMap &SectionMap) {
   unsigned EHFrameSID = RTDYLD_INVALID_SECTION_ID;
   unsigned TextSID = RTDYLD_INVALID_SECTION_ID;
   unsigned ExceptTabSID = RTDYLD_INVALID_SECTION_ID;
@@ -222,20 +236,34 @@
     // Force emission of the __text, __eh_frame, and __gcc_except_tab sections
     // if they're present. Otherwise call down to the impl to handle other
     // sections that have already been emitted.
-    if (Name == "__text")
-      TextSID = findOrEmitSection(Obj, Section, true, SectionMap);
-    else if (Name == "__eh_frame")
-      EHFrameSID = findOrEmitSection(Obj, Section, false, SectionMap);
-    else if (Name == "__gcc_except_tab")
-      ExceptTabSID = findOrEmitSection(Obj, Section, true, SectionMap);
-    else {
+    if (Name == "__text") {
+      if (auto TextSIDOrErr = findOrEmitSection(Obj, Section, true, SectionMap))
+        TextSID = *TextSIDOrErr;
+      else
+        return TextSIDOrErr.takeError();
+    } else if (Name == "__eh_frame") {
+      if (auto EHFrameSIDOrErr = findOrEmitSection(Obj, Section, false,
+                                                   SectionMap))
+        EHFrameSID = *EHFrameSIDOrErr;
+      else
+        return EHFrameSIDOrErr.takeError();
+    } else if (Name == "__gcc_except_tab") {
+      if (auto ExceptTabSIDOrErr = findOrEmitSection(Obj, Section, true,
+                                                     SectionMap))
+        ExceptTabSID = *ExceptTabSIDOrErr;
+      else
+        return ExceptTabSIDOrErr.takeError();
+    } else {
       auto I = SectionMap.find(Section);
       if (I != SectionMap.end())
-        impl().finalizeSection(Obj, I->second, Section);
+        if (auto Err = impl().finalizeSection(Obj, I->second, Section))
+          return Err;
     }
   }
   UnregisteredEHFrameSections.push_back(
     EHFrameRelatedSections(EHFrameSID, TextSID, ExceptTabSID));
+
+  return Error::success();
 }
 
 template <typename Impl>
@@ -302,9 +330,9 @@
 
     uint8_t *P = EHFrame->getAddress();
     uint8_t *End = P + EHFrame->getSize();
-    do {
+    while (P != End) {
       P = processFDE(P, DeltaForText, DeltaForEH);
-    } while (P != End);
+    }
 
     MemMgr.registerEHFrames(EHFrame->getAddress(), EHFrame->getLoadAddress(),
                             EHFrame->getSize());
@@ -315,7 +343,7 @@
 std::unique_ptr<RuntimeDyldMachO>
 RuntimeDyldMachO::create(Triple::ArchType Arch,
                          RuntimeDyld::MemoryManager &MemMgr,
-                         RuntimeDyld::SymbolResolver &Resolver) {
+                         JITSymbolResolver &Resolver) {
   switch (Arch) {
   default:
     llvm_unreachable("Unsupported target for RuntimeDyldMachO.");
@@ -333,7 +361,15 @@
 
 std::unique_ptr<RuntimeDyld::LoadedObjectInfo>
 RuntimeDyldMachO::loadObject(const object::ObjectFile &O) {
-  return llvm::make_unique<LoadedMachOObjectInfo>(*this, loadObjectImpl(O));
+  if (auto ObjSectionToIDOrErr = loadObjectImpl(O))
+    return llvm::make_unique<LoadedMachOObjectInfo>(*this,
+                                                    *ObjSectionToIDOrErr);
+  else {
+    HasError = true;
+    raw_string_ostream ErrStream(ErrorStr);
+    logAllUnhandledErrors(ObjSectionToIDOrErr.takeError(), ErrStream, "");
+    return nullptr;
+  }
 }
 
 } // end namespace llvm