[PATCH] D121398: Return an error when dsymutil might produce an invalid mach-o file.

Greg Clayton via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Mar 10 14:12:11 PST 2022


clayborg updated this revision to Diff 414498.
clayborg added a comment.

Remove old code that was incorrectly checking for a total 4GB byte size of the final produced file. This code didn't work as intended for universal mach-o files since it was ok for these files to be larger than 4GB, and it didn't work for skinny mach-o files since they can actually be larger than 4GB as long as no uint32_t file offset would exceed UINT32_MAX.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D121398/new/

https://reviews.llvm.org/D121398

Files:
  llvm/tools/dsymutil/MachOUtils.cpp
  llvm/tools/dsymutil/dsymutil.cpp


Index: llvm/tools/dsymutil/dsymutil.cpp
===================================================================
--- llvm/tools/dsymutil/dsymutil.cpp
+++ llvm/tools/dsymutil/dsymutil.cpp
@@ -735,19 +735,6 @@
                                                Options.LinkOpts, SDKPath))
         return EXIT_FAILURE;
     }
-
-    // The Mach-O object file format is limited to 4GB. Make sure that we print
-    // an error when we emit an invalid Mach-O companion file. Leave the
-    // invalid object file around on disk for inspection.
-    ErrorOr<vfs::Status> stat =
-        Options.LinkOpts.VFS->status(OutputLocationOrErr->DWARFFile);
-    if (stat) {
-      if (stat->getSize() > std::numeric_limits<uint32_t>::max()) {
-        WithColor::error() << "the linked debug info exceeds the 4GB Mach-O "
-                              "object file format.";
-        return EXIT_FAILURE;
-      }
-    }
   }
 
   return EXIT_SUCCESS;
Index: llvm/tools/dsymutil/MachOUtils.cpp
===================================================================
--- llvm/tools/dsymutil/MachOUtils.cpp
+++ llvm/tools/dsymutil/MachOUtils.cpp
@@ -304,7 +304,7 @@
 }
 
 // Write the __DWARF segment load command to the output file.
-static void createDwarfSegment(uint64_t VMAddr, uint64_t FileOffset,
+static bool createDwarfSegment(uint64_t VMAddr, uint64_t FileOffset,
                                uint64_t FileSize, unsigned NumSections,
                                MCAsmLayout &Layout, MachObjectWriter &Writer) {
   Writer.writeSegmentLoadCommand("__DWARF", NumSections, VMAddr,
@@ -321,12 +321,15 @@
     if (Align > 1) {
       VMAddr = alignTo(VMAddr, Align);
       FileOffset = alignTo(FileOffset, Align);
+      if (FileOffset > UINT32_MAX)
+        return error("section " + Sec->getName() + "'s file offset exceeds 4GB and will produce an invalid mach-o file");
     }
     Writer.writeSection(Layout, *Sec, VMAddr, FileOffset, 0, 0, 0);
 
     FileOffset += Layout.getSectionAddressSize(Sec);
     VMAddr += Layout.getSectionAddressSize(Sec);
   }
+  return true;
 }
 
 static bool isExecutable(const object::MachOObjectFile &Obj) {
@@ -564,8 +567,9 @@
   }
 
   // Write the load command for the __DWARF segment.
-  createDwarfSegment(DwarfVMAddr, DwarfSegmentStart, DwarfSegmentSize,
-                     NumDwarfSections, Layout, Writer);
+  if (!createDwarfSegment(DwarfVMAddr, DwarfSegmentStart, DwarfSegmentSize,
+                          NumDwarfSections, Layout, Writer))
+    return false;
 
   assert(OutFile.tell() == LoadCommandSize + HeaderSize);
   OutFile.write_zeros(SymtabStart - (LoadCommandSize + HeaderSize));


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D121398.414498.patch
Type: text/x-patch
Size: 2630 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220310/0ad8448a/attachment.bin>


More information about the llvm-commits mailing list