diff --git a/lld/MachO/Driver.cpp b/lld/MachO/Driver.cpp
index 7d2a76ce9f4d3a7f835dff8fa22755af75e93230..2661c707121240a5120d4bd3d4293779441f5c7b 100644
--- a/lld/MachO/Driver.cpp
+++ b/lld/MachO/Driver.cpp
@@ -986,14 +986,11 @@ static void gatherInputSections() {
   int inputOrder = 0;
   for (const InputFile *file : inputFiles) {
     for (const Section *section : file->sections) {
-      const Subsections &subsections = section->subsections;
-      if (subsections.empty())
-        continue;
-      if (subsections[0].isec->getName() == section_names::compactUnwind)
+      if (section->name == section_names::compactUnwind)
         // Compact unwind entries require special handling elsewhere.
         continue;
       ConcatOutputSection *osec = nullptr;
-      for (const Subsection &subsection : subsections) {
+      for (const Subsection &subsection : section->subsections) {
         if (auto *isec = dyn_cast<ConcatInputSection>(subsection.isec)) {
           if (isec->isCoalescedWeak())
             continue;
diff --git a/lld/MachO/InputFiles.cpp b/lld/MachO/InputFiles.cpp
index 6d7769878cecef88203a9b05f8dbd4afb46151cd..26fa566b63c373770c51db34bc0f54140ad59bd7 100644
--- a/lld/MachO/InputFiles.cpp
+++ b/lld/MachO/InputFiles.cpp
@@ -95,6 +95,10 @@ std::string lld::toString(const InputFile *f) {
   return (f->archiveName + "(" + path::filename(f->getName()) + ")").str();
 }
 
+std::string lld::toString(const Section &sec) {
+  return (toString(sec.file) + ":(" + sec.name + ")").str();
+}
+
 SetVector<InputFile *> macho::inputFiles;
 std::unique_ptr<TarWriter> macho::tar;
 int InputFile::idCount = 0;
@@ -302,7 +306,7 @@ void ObjFile::parseSections(ArrayRef<SectionHeader> sectionHeaders) {
             " is too large");
       continue;
     }
-    const Section &section = *sections.back();
+    Section &section = *sections.back();
     uint32_t align = 1 << sec.align;
     ArrayRef<uint8_t> data = {isZeroFill(sec.flags) ? nullptr
                                                     : buf + sec.offset,
@@ -311,7 +315,7 @@ void ObjFile::parseSections(ArrayRef<SectionHeader> sectionHeaders) {
     auto splitRecords = [&](int recordSize) -> void {
       if (data.empty())
         return;
-      Subsections &subsections = sections.back()->subsections;
+      Subsections &subsections = section.subsections;
       subsections.reserve(data.size() / recordSize);
       for (uint64_t off = 0; off < data.size(); off += recordSize) {
         auto *isec = make<ConcatInputSection>(
@@ -336,11 +340,11 @@ void ObjFile::parseSections(ArrayRef<SectionHeader> sectionHeaders) {
       } else {
         isec = make<WordLiteralInputSection>(section, data, align);
       }
-      sections.back()->subsections.push_back({0, isec});
+      section.subsections.push_back({0, isec});
     } else if (auto recordSize = getRecordSize(segname, name)) {
       splitRecords(*recordSize);
       if (name == section_names::compactUnwind)
-        compactUnwindSection = sections.back();
+        compactUnwindSection = &section;
     } else if (segname == segment_names::llvm) {
       if (config->callGraphProfileSort && name == section_names::cgProfile)
         checkError(parseCallGraph(data, callGraph));
@@ -359,7 +363,7 @@ void ObjFile::parseSections(ArrayRef<SectionHeader> sectionHeaders) {
         // parsing their relocations unnecessarily.
         debugSections.push_back(isec);
       } else {
-        sections.back()->subsections.push_back({0, isec});
+        section.subsections.push_back({0, isec});
       }
     }
   }
@@ -724,8 +728,7 @@ void ObjFile::parseSymbols(ArrayRef<typename LP::section> sectionHeaders,
     Subsections &subsections = sections[i]->subsections;
     if (subsections.empty())
       continue;
-    InputSection *lastIsec = subsections.back().isec;
-    if (lastIsec->getName() == section_names::ehFrame) {
+    if (sections[i]->name == section_names::ehFrame) {
       // __TEXT,__eh_frame only has symbols and SUBTRACTOR relocs when ld64 -r
       // adds local "EH_Frame1" and "func.eh". Ignore them because they have
       // gone unused by Mac OS since Snow Leopard (10.6), vintage 2009.
@@ -738,7 +741,7 @@ void ObjFile::parseSymbols(ArrayRef<typename LP::section> sectionHeaders,
     // Record-based sections have already been split into subsections during
     // parseSections(), so we simply need to match Symbols to the corresponding
     // subsection here.
-    if (getRecordSize(lastIsec->getSegName(), lastIsec->getName())) {
+    if (getRecordSize(sections[i]->segname, sections[i]->name)) {
       for (size_t j = 0; j < symbolIndices.size(); ++j) {
         uint32_t symIndex = symbolIndices[j];
         const NList &sym = nList[symIndex];
@@ -747,7 +750,7 @@ void ObjFile::parseSymbols(ArrayRef<typename LP::section> sectionHeaders,
         InputSection *isec =
             findContainingSubsection(*sections[i], &symbolOffset);
         if (symbolOffset != 0) {
-          error(toString(lastIsec) + ":  symbol " + name +
+          error(toString(*sections[i]) + ":  symbol " + name +
                 " at misaligned offset");
           continue;
         }
diff --git a/lld/MachO/InputFiles.h b/lld/MachO/InputFiles.h
index 7dda230b3e1da60551fed25da31ba8cbc0bfa9b2..5d455d277b6f201edc5c39ae8de0a2e38196c210 100644
--- a/lld/MachO/InputFiles.h
+++ b/lld/MachO/InputFiles.h
@@ -307,6 +307,7 @@ std::vector<const CommandType *> findCommands(const void *anyHdr,
 } // namespace macho
 
 std::string toString(const macho::InputFile *file);
+std::string toString(const macho::Section &);
 } // namespace lld
 
 #endif