[PECOFF] Return an error_code instead of calling report_fatal_error().
authorRui Ueyama <ruiu@google.com>
Fri, 2 Aug 2013 21:10:17 +0000 (21:10 +0000)
committerRui Ueyama <ruiu@google.com>
Fri, 2 Aug 2013 21:10:17 +0000 (21:10 +0000)
For an invalid input we should not call report_fatal_error(), because
when LLD is used as a library, we don't want to kill the whole app
because of a malformed input.

llvm-svn: 187673

lld/lib/ReaderWriter/PECOFF/ReaderCOFF.cpp

index 1148c2e..7621685 100644 (file)
@@ -314,13 +314,17 @@ private:
 
   /// Find the atom that is at \p targetOffset in \p section. It is assumed
   /// that \p atoms are sorted by position in the section.
-  COFFDefinedFileAtom *
-  findAtomAt(uint32_t targetOffset,
-             const vector<COFFDefinedFileAtom *> &atoms) const {
-    for (COFFDefinedFileAtom *atom : atoms)
-      if (targetOffset < atom->originalOffset() + atom->size())
-        return atom;
-    llvm::report_fatal_error("Relocation target out of range");
+  error_code findAtomAt(uint32_t targetOffset,
+                        const vector<COFFDefinedFileAtom *> &atoms,
+                        COFFDefinedFileAtom *&result) const {
+    for (COFFDefinedFileAtom *atom : atoms) {
+      if (targetOffset < atom->originalOffset() + atom->size()) {
+        result = atom;
+        return error_code::success();
+      }
+    }
+    // Relocation target is out of range
+    return llvm::object::object_error::parse_failed;
   }
 
   /// Find the atom for the symbol that was at the \p index in the symbol
@@ -351,7 +355,9 @@ private:
     if (error_code ec = getAtomBySymbolIndex(rel->SymbolTableIndex, targetAtom))
       return ec;
 
-    COFFDefinedFileAtom *atom = findAtomAt(rel->VirtualAddress, atoms);
+    COFFDefinedFileAtom *atom;
+    if (error_code ec = findAtomAt(rel->VirtualAddress, atoms, atom))
+      return ec;
     uint32_t offsetInAtom = itemAddress - atom->originalOffset();
     assert(offsetInAtom < atom->size());
     atom->addReference(std::unique_ptr<COFFReference>(