[lld] r372995 - [lld][mach-o] Avoid segfaulting when handling an empty section list.

Matt Davis via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 26 10:03:21 PDT 2019


Author: mattd
Date: Thu Sep 26 10:03:20 2019
New Revision: 372995

URL: http://llvm.org/viewvc/llvm-project?rev=372995&view=rev
Log:
[lld][mach-o] Avoid segfaulting when handling an empty section list.

Summary:
The following patch avoids segfaulting if the section list is empty when writing a mach-o MH_OBJECT.  I ran into this case from a more complicated example trying to dead_strip while using '-r' in lld.

I'm not sure if having empty sections is a legal mach-o, but it does seem that other llvm-binutils tools can ingest such a boring object with out issue.  Would it be better to emit an error, emit a warning, or do nothing?  It seems that adding a warning diagnostic might be helpful to users, as I did not expect to have a section-less object when the linker was done.

Reviewers: kledzik, ruiu

Subscribers: llvm-commits, jrm

Tags: #lld, #llvm

Differential Revision: https://reviews.llvm.org/D67735

Added:
    lld/trunk/test/mach-o/empty-sections.yaml
Modified:
    lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileBinaryWriter.cpp

Modified: lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileBinaryWriter.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileBinaryWriter.cpp?rev=372995&r1=372994&r2=372995&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileBinaryWriter.cpp (original)
+++ lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileBinaryWriter.cpp Thu Sep 26 10:03:20 2019
@@ -626,17 +626,19 @@ llvm::Error MachOFileLayout::writeSingle
                           + _file.sections.size() * sizeof(typename T::section);
   uint8_t *next = lc + seg->cmdsize;
   memset(seg->segname, 0, 16);
+  seg->flags = 0;
   seg->vmaddr = 0;
-  seg->vmsize = _file.sections.back().address
-              + _file.sections.back().content.size();
   seg->fileoff = _endOfLoadCommands;
-  seg->filesize = _sectInfo[&_file.sections.back()].fileOffset +
-                  _file.sections.back().content.size() -
-                  _sectInfo[&_file.sections.front()].fileOffset;
   seg->maxprot = VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE;
   seg->initprot = VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE;
   seg->nsects = _file.sections.size();
-  seg->flags = 0;
+  if (seg->nsects) {
+    seg->vmsize = _file.sections.back().address
+                + _file.sections.back().content.size();
+    seg->filesize = _sectInfo[&_file.sections.back()].fileOffset +
+                    _file.sections.back().content.size() -
+                    _sectInfo[&_file.sections.front()].fileOffset;
+  }
   if (_swap)
     swapStruct(*seg);
   typename T::section *sout = reinterpret_cast<typename T::section*>

Added: lld/trunk/test/mach-o/empty-sections.yaml
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/mach-o/empty-sections.yaml?rev=372995&view=auto
==============================================================================
--- lld/trunk/test/mach-o/empty-sections.yaml (added)
+++ lld/trunk/test/mach-o/empty-sections.yaml Thu Sep 26 10:03:20 2019
@@ -0,0 +1,9 @@
+# RUN: ld64.lld -arch x86_64 -r %s -o %t
+#
+# Test that writing empty mach-o sections does not segfault the linker.
+#
+
+--- !mach-o
+arch:            x86_64
+file-type:       MH_OBJECT
+...




More information about the llvm-commits mailing list