MachObjectWriter: optimize the string table for common suffices

This is a follow-up to r207670 (ELF) and r218636 (COFF).

Differential Revision: http://reviews.llvm.org/D5622

llvm-svn: 219126
diff --git a/llvm/lib/MC/StringTableBuilder.cpp b/llvm/lib/MC/StringTableBuilder.cpp
index 34e7e6f..9de9363 100644
--- a/llvm/lib/MC/StringTableBuilder.cpp
+++ b/llvm/lib/MC/StringTableBuilder.cpp
@@ -36,12 +36,16 @@
 
   std::sort(Strings.begin(), Strings.end(), compareBySuffix);
 
-  if (kind == ELF) {
+  switch (kind) {
+  case ELF:
+  case MachO:
     // Start the table with a NUL byte.
     StringTable += '\x00';
-  } else if (kind == WinCOFF) {
+    break;
+  case WinCOFF:
     // Make room to write the table size later.
     StringTable.append(4, '\x00');
+    break;
   }
 
   StringRef Previous;
@@ -60,11 +64,21 @@
     Previous = s;
   }
 
-  if (kind == WinCOFF) {
+  switch (kind) {
+  case ELF:
+    break;
+  case MachO:
+    // Pad to multiple of 4.
+    while (StringTable.size() % 4)
+      StringTable += '\x00';
+    break;
+  case WinCOFF:
+    // Write the table size in the first word.
     assert(StringTable.size() <= std::numeric_limits<uint32_t>::max());
     uint32_t size = static_cast<uint32_t>(StringTable.size());
     support::endian::write<uint32_t, support::little, support::unaligned>(
         StringTable.data(), size);
+    break;
   }
 }