[lld] r257841 - Error check the size of the __objc_imageinfo section

Pete Cooper via llvm-commits llvm-commits at lists.llvm.org
Thu Jan 14 15:35:10 PST 2016


Author: pete
Date: Thu Jan 14 17:35:05 2016
New Revision: 257841

URL: http://llvm.org/viewvc/llvm-project?rev=257841&view=rev
Log:
Error check the size of the __objc_imageinfo section

Added:
    lld/trunk/test/mach-o/objc-image-info-invalid-size.yaml
Modified:
    lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileToAtoms.cpp

Modified: lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileToAtoms.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileToAtoms.cpp?rev=257841&r1=257840&r2=257841&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileToAtoms.cpp (original)
+++ lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileToAtoms.cpp Thu Jan 14 17:35:05 2016
@@ -867,6 +867,45 @@ std::error_code addEHFrameReferences(con
   return ehFrameErr;
 }
 
+std::error_code parseObjCImageInfo(const NormalizedFile &normalizedFile,
+                                   MachOFile &file) {
+
+  const Section *imageInfoSection = nullptr;
+  for (auto &section : normalizedFile.sections) {
+    if (section.segmentName == "__OBJC" &&
+        section.sectionName == "__image_info") {
+      imageInfoSection = §ion;
+      break;
+    }
+    if (section.segmentName == "__DATA" &&
+        section.sectionName == "__objc_imageinfo") {
+      imageInfoSection = §ion;
+      break;
+    }
+  }
+
+  // No image info section so nothing to do.
+  if (!imageInfoSection)
+    return std::error_code();
+
+  //	struct objc_image_info  {
+  //		uint32_t	version;	// initially 0
+  //		uint32_t	flags;
+  //	};
+  // #define OBJC_IMAGE_SUPPORTS_GC   2
+  // #define OBJC_IMAGE_GC_ONLY       4
+  // #define OBJC_IMAGE_IS_SIMULATED  32
+  //
+  ArrayRef<uint8_t> content = imageInfoSection->content;
+  if (content.size() != 8)
+    return make_dynamic_error_code(imageInfoSection->segmentName + "/" +
+                                   imageInfoSection->sectionName +
+                                   " in file " + file.path() +
+                                   " should be 8 bytes in size");
+
+  return std::error_code();
+}
+
 
 /// Converts normalized mach-o file into an lld::File and lld::Atoms.
 ErrorOr<std::unique_ptr<lld::File>>
@@ -948,6 +987,11 @@ normalizedObjectToAtoms(MachOFile *file,
   if (std::error_code ec = addEHFrameReferences(normalizedFile, *file, *handler))
     return ec;
 
+  // If the file contains an objc_image_info struct, then we should parse the
+  // ObjC flags and Swift version.
+  if (std::error_code ec = parseObjCImageInfo(normalizedFile, *file))
+    return ec;
+
   // Process mach-o data-in-code regions array. That information is encoded in
   // atoms as References at each transition point.
   unsigned nextIndex = 0;

Added: lld/trunk/test/mach-o/objc-image-info-invalid-size.yaml
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/mach-o/objc-image-info-invalid-size.yaml?rev=257841&view=auto
==============================================================================
--- lld/trunk/test/mach-o/objc-image-info-invalid-size.yaml (added)
+++ lld/trunk/test/mach-o/objc-image-info-invalid-size.yaml Thu Jan 14 17:35:05 2016
@@ -0,0 +1,20 @@
+# RUN: not lld -flavor darwin -arch x86_64 -r %s %p/Inputs/hello-world-x86_64.yaml 2>&1 | FileCheck %s
+
+--- !mach-o
+arch:            x86_64
+file-type:       MH_OBJECT
+flags:           [ MH_SUBSECTIONS_VIA_SYMBOLS ]
+compat-version:  0.0
+current-version: 0.0
+has-UUID:        false
+OS:              unknown
+sections:
+  - segment:         __DATA
+    section:         __objc_imageinfo
+    type:            S_REGULAR
+    attributes:      [ S_ATTR_NO_DEAD_STRIP ]
+    address:         0x0000000000000100
+    content:         [ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 ]
+...
+
+# CHECK: error: __DATA/__objc_imageinfo in file /Volumes/Data/llvm/tools/lld/test/mach-o/objc-image-info-invalid-size.yaml should be 8 bytes in size
\ No newline at end of file




More information about the llvm-commits mailing list