[mach-o] parse literal sections into atoms

llvm-svn: 209379
diff --git a/lld/lib/ReaderWriter/MachO/MachONormalizedFileFromAtoms.cpp b/lld/lib/ReaderWriter/MachO/MachONormalizedFileFromAtoms.cpp
index b5b5718..072f40d 100644
--- a/lld/lib/ReaderWriter/MachO/MachONormalizedFileFromAtoms.cpp
+++ b/lld/lib/ReaderWriter/MachO/MachONormalizedFileFromAtoms.cpp
@@ -179,6 +179,15 @@
   case DefinedAtom::typeZeroFill:
      return new (_allocator) SectionInfo("__DATA", "__bss",
                             S_ZEROFILL);
+  case DefinedAtom::typeLiteral4:
+     return new (_allocator) SectionInfo("__TEXT", "__literal4",
+                            S_4BYTE_LITERALS);
+  case DefinedAtom::typeLiteral8:
+     return new (_allocator) SectionInfo("__TEXT", "__literal8",
+                            S_8BYTE_LITERALS);
+  case DefinedAtom::typeLiteral16:
+     return new (_allocator) SectionInfo("__TEXT", "__literal16",
+                            S_16BYTE_LITERALS);
   default:
     llvm_unreachable("TO DO: add support for more sections");
     break;
diff --git a/lld/lib/ReaderWriter/MachO/MachONormalizedFileToAtoms.cpp b/lld/lib/ReaderWriter/MachO/MachONormalizedFileToAtoms.cpp
index ab29f85..d40321b 100644
--- a/lld/lib/ReaderWriter/MachO/MachONormalizedFileToAtoms.cpp
+++ b/lld/lib/ReaderWriter/MachO/MachONormalizedFileToAtoms.cpp
@@ -107,23 +107,80 @@
   }
 }
 
+static void processSection(MachOFile &file, const Section &section,
+                           bool copyRefs) {
+  unsigned offset = 0;
+  switch (section.type) {
+  case llvm::MachO::S_REGULAR:
+  case llvm::MachO::S_COALESCED:
+  case llvm::MachO::S_ZEROFILL:
+    // These sections are broken in to atoms based on symbols.
+    break;
+  case llvm::MachO::S_CSTRING_LITERALS:
+    for (size_t i = 0, e = section.content.size(); i != e; ++i) {
+      if (section.content[i] == 0) {
+        unsigned size = i - offset + 1;
+        ArrayRef<uint8_t> strContent = section.content.slice(offset, size);
+        file.addDefinedAtom(StringRef(), DefinedAtom::scopeLinkageUnit,
+                            DefinedAtom::typeCString, strContent, copyRefs);
+        offset = i + 1;
+      }
+    }
+    break;
+  case llvm::MachO::S_4BYTE_LITERALS:
+    assert((section.content.size() % 4) == 0);
+    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,
+                          DefinedAtom::typeLiteral4, byteContent, copyRefs);
+      offset += 4;
+    }
+    break;
+  case llvm::MachO::S_8BYTE_LITERALS:
+    assert((section.content.size() % 8) == 0);
+    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,
+                          DefinedAtom::typeLiteral8, byteContent, copyRefs);
+      offset += 8;
+    }
+    break;
+  case llvm::MachO::S_16BYTE_LITERALS:
+    assert((section.content.size() % 16) == 0);
+    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,
+                          DefinedAtom::typeLiteral16, byteContent, copyRefs);
+      offset += 16;
+    }
+    break;
+  default:
+    llvm_unreachable("mach-o section type not supported");
+    break;
+  }
+}
 
 static ErrorOr<std::unique_ptr<lld::File>>
 normalizedObjectToAtoms(const NormalizedFile &normalizedFile, StringRef path,
                         bool copyRefs) {
   std::unique_ptr<MachOFile> file(new MachOFile(path));
-
+  
+  // Create atoms from global symbols.
   for (const Symbol &sym : normalizedFile.globalSymbols) {
     processSymbol(normalizedFile, *file, sym, copyRefs);
   }
-
+  // Create atoms from local symbols.
   for (const Symbol &sym : normalizedFile.localSymbols) {
     processSymbol(normalizedFile, *file, sym, copyRefs);
   }
-
+  // Create atoms from undefinded symbols.
   for (auto &sym : normalizedFile.undefinedSymbols) {
     processUndefindeSymbol(*file, sym, copyRefs);
   }
+  // Create atoms from sections that don't have symbols.
+  for (auto &sect : normalizedFile.sections) {
+    processSection(*file, sect, copyRefs);
+  }
 
   return std::unique_ptr<File>(std::move(file));
 }