[lld] r209469 - [mach-o] Fix so that mach-o semantic errors return an error rather than assert

Nick Kledzik kledzik at apple.com
Thu May 22 13:05:44 PDT 2014


Author: kledzik
Date: Thu May 22 15:05:43 2014
New Revision: 209469

URL: http://llvm.org/viewvc/llvm-project?rev=209469&view=rev
Log:
[mach-o] Fix so that mach-o semantic errors return an error rather than assert

Added:
    lld/trunk/test/mach-o/parse-literals-error.yaml
Modified:
    lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileToAtoms.cpp
    lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileYAML.cpp

Modified: lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileToAtoms.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileToAtoms.cpp?rev=209469&r1=209468&r2=209469&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileToAtoms.cpp (original)
+++ lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileToAtoms.cpp Thu May 22 15:05:43 2014
@@ -107,8 +107,8 @@ static void processUndefindeSymbol(MachO
   }
 }
 
-static void processSection(MachOFile &file, const Section &section,
-                           bool copyRefs) {
+static error_code processSection(MachOFile &file, const Section &section,
+                                 bool copyRefs) {
   unsigned offset = 0;
   switch (section.type) {
   case llvm::MachO::S_REGULAR:
@@ -128,7 +128,8 @@ static void processSection(MachOFile &fi
     }
     break;
   case llvm::MachO::S_4BYTE_LITERALS:
-    assert((section.content.size() % 4) == 0);
+    if ((section.content.size() % 4) != 0)
+      return llvm::make_error_code(llvm::errc::executable_format_error);
     for (size_t i = 0, e = section.content.size(); i != e; i += 4) {
       ArrayRef<uint8_t> byteContent = section.content.slice(offset, 4);
       file.addDefinedAtom(StringRef(), DefinedAtom::scopeLinkageUnit,
@@ -137,7 +138,8 @@ static void processSection(MachOFile &fi
     }
     break;
   case llvm::MachO::S_8BYTE_LITERALS:
-    assert((section.content.size() % 8) == 0);
+    if ((section.content.size() % 8) != 0)
+      return llvm::make_error_code(llvm::errc::executable_format_error);
     for (size_t i = 0, e = section.content.size(); i != e; i += 8) {
       ArrayRef<uint8_t> byteContent = section.content.slice(offset, 8);
       file.addDefinedAtom(StringRef(), DefinedAtom::scopeLinkageUnit,
@@ -146,7 +148,8 @@ static void processSection(MachOFile &fi
     }
     break;
   case llvm::MachO::S_16BYTE_LITERALS:
-    assert((section.content.size() % 16) == 0);
+    if ((section.content.size() % 16) != 0)
+      return llvm::make_error_code(llvm::errc::executable_format_error);
     for (size_t i = 0, e = section.content.size(); i != e; i += 16) {
       ArrayRef<uint8_t> byteContent = section.content.slice(offset, 16);
       file.addDefinedAtom(StringRef(), DefinedAtom::scopeLinkageUnit,
@@ -155,9 +158,10 @@ static void processSection(MachOFile &fi
     }
     break;
   default:
-    llvm_unreachable("mach-o section type not supported");
+    llvm_unreachable("mach-o section type not supported yet");
     break;
   }
+  return error_code::success();
 }
 
 static ErrorOr<std::unique_ptr<lld::File>>
@@ -179,7 +183,8 @@ normalizedObjectToAtoms(const Normalized
   }
   // Create atoms from sections that don't have symbols.
   for (auto &sect : normalizedFile.sections) {
-    processSection(*file, sect, copyRefs);
+    if (error_code ec = processSection(*file, sect, copyRefs))
+      return ec;
   }
 
   return std::unique_ptr<File>(std::move(file));

Modified: lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileYAML.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileYAML.cpp?rev=209469&r1=209468&r2=209469&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileYAML.cpp (original)
+++ lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileYAML.cpp Thu May 22 15:05:43 2014
@@ -659,8 +659,10 @@ bool MachOYamlIOTaggedDocumentHandler::h
     std::unique_ptr<lld::File> f = std::move(foe.get());
     file = f.release();
     return true;
+  } else {
+    io.setError(foe.getError().message());
+    return false;
   }
-  return false;
 }
 
 

Added: lld/trunk/test/mach-o/parse-literals-error.yaml
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/mach-o/parse-literals-error.yaml?rev=209469&view=auto
==============================================================================
--- lld/trunk/test/mach-o/parse-literals-error.yaml (added)
+++ lld/trunk/test/mach-o/parse-literals-error.yaml Thu May 22 15:05:43 2014
@@ -0,0 +1,25 @@
+# RUN: not lld -flavor darwin -arch x86_64 -r -print_atoms %s -o %t 2> %t.err
+# RUN: FileCheck %s < %t.err
+#
+# Test for error if literal section is not correct size mulitple.
+#
+
+--- !mach-o
+arch:            x86_64
+file-type:       MH_OBJECT
+flags:           [ MH_SUBSECTIONS_VIA_SYMBOLS ]
+has-UUID:        false
+OS:              unknown
+sections:
+  - segment:         __TEXT
+    section:         __literal8
+    type:            S_8BYTE_LITERALS
+    attributes:      [  ]
+    alignment:       0
+    address:         0x0000000000000120
+    content:         [ 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
+                       0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D ]
+...
+
+# CHECK:       error: 
+





More information about the llvm-commits mailing list