Convert a few macho reader/writer helpers to new error handling.  NFC.

These methods were responsible for some of the few remaining calls
to llvm::errorCodeToError.  Converting them makes us have more Error's
in the api and fewer error_code's.

llvm-svn: 264974
diff --git a/lld/lib/ReaderWriter/MachO/MachONormalizedFileBinaryWriter.cpp b/lld/lib/ReaderWriter/MachO/MachONormalizedFileBinaryWriter.cpp
index b8c7396..0ec7a40 100644
--- a/lld/lib/ReaderWriter/MachO/MachONormalizedFileBinaryWriter.cpp
+++ b/lld/lib/ReaderWriter/MachO/MachONormalizedFileBinaryWriter.cpp
@@ -139,7 +139,7 @@
   uint32_t    loadCommandsSize(uint32_t &count);
   void        buildFileOffsets();
   void        writeMachHeader();
-  std::error_code writeLoadCommands();
+  llvm::Error writeLoadCommands();
   void        writeSectionContent();
   void        writeRelocations();
   void        writeSymbolTable();
@@ -179,8 +179,8 @@
   };
 
   template <typename T>
-  std::error_code writeSingleSegmentLoadCommand(uint8_t *&lc);
-  template <typename T> std::error_code writeSegmentLoadCommands(uint8_t *&lc);
+  llvm::Error writeSingleSegmentLoadCommand(uint8_t *&lc);
+  template <typename T> llvm::Error writeSegmentLoadCommands(uint8_t *&lc);
 
   uint32_t pointerAlign(uint32_t value);
   static StringRef dyldPath();
@@ -628,7 +628,7 @@
 }
 
 template <typename T>
-std::error_code MachOFileLayout::writeSingleSegmentLoadCommand(uint8_t *&lc) {
+llvm::Error MachOFileLayout::writeSingleSegmentLoadCommand(uint8_t *&lc) {
   typename T::command* seg = reinterpret_cast<typename T::command*>(lc);
   seg->cmd = T::LC;
   seg->cmdsize = sizeof(typename T::command)
@@ -668,11 +668,11 @@
     ++sout;
   }
   lc = next;
-  return std::error_code();
+  return llvm::Error();
 }
 
 template <typename T>
-std::error_code MachOFileLayout::writeSegmentLoadCommands(uint8_t *&lc) {
+llvm::Error MachOFileLayout::writeSegmentLoadCommands(uint8_t *&lc) {
   uint32_t indirectSymRunningIndex = 0;
   for (const Segment &seg : _file.segments) {
     // Link edit has no sections and a custom range of address, so handle it
@@ -738,7 +738,7 @@
     }
     lc = reinterpret_cast<uint8_t*>(next);
   }
-  return std::error_code();
+  return llvm::Error();
 }
 
 static void writeVersionMinLoadCommand(const NormalizedFile &_file,
@@ -773,15 +773,17 @@
   lc += sizeof(version_min_command);
 }
 
-std::error_code MachOFileLayout::writeLoadCommands() {
-  std::error_code ec;
+llvm::Error MachOFileLayout::writeLoadCommands() {
   uint8_t *lc = &_buffer[_startOfLoadCommands];
   if (_file.fileType == llvm::MachO::MH_OBJECT) {
     // Object files have one unnamed segment which holds all sections.
-    if (_is64)
-      ec = writeSingleSegmentLoadCommand<MachO64Trait>(lc);
-    else
-      ec = writeSingleSegmentLoadCommand<MachO32Trait>(lc);
+    if (_is64) {
+     if (auto ec = writeSingleSegmentLoadCommand<MachO64Trait>(lc))
+       return std::move(ec);
+    } else {
+      if (auto ec = writeSingleSegmentLoadCommand<MachO32Trait>(lc))
+        return std::move(ec);
+    }
     // Add LC_SYMTAB with symbol table info
     symtab_command* st = reinterpret_cast<symtab_command*>(lc);
     st->cmd     = LC_SYMTAB;
@@ -824,10 +826,13 @@
     }
   } else {
     // Final linked images have sections under segments.
-    if (_is64)
-      ec = writeSegmentLoadCommands<MachO64Trait>(lc);
-    else
-      ec = writeSegmentLoadCommands<MachO32Trait>(lc);
+    if (_is64) {
+      if (auto ec = writeSegmentLoadCommands<MachO64Trait>(lc))
+        return std::move(ec);
+    } else {
+      if (auto ec = writeSegmentLoadCommands<MachO32Trait>(lc))
+        return std::move(ec);
+    }
 
     // Add LC_ID_DYLIB command for dynamic libraries.
     if (_file.fileType == llvm::MachO::MH_DYLIB) {
@@ -1012,7 +1017,7 @@
       lc += sizeof(linkedit_data_command);
     }
   }
-  return ec;
+  return llvm::Error();
 }
 
 void MachOFileLayout::writeSectionContent() {
@@ -1475,9 +1480,8 @@
   // Write content.
   _buffer = fob->getBufferStart();
   writeMachHeader();
-  std::error_code ec = writeLoadCommands();
-  if (ec)
-    return llvm::errorCodeToError(ec);
+  if (auto ec = writeLoadCommands())
+    return std::move(ec);
   writeSectionContent();
   writeLinkEditContent();
   fob->commit();