MachObjectWriter: optimize the string table for common suffices
[oota-llvm.git] / lib / MC / StringTableBuilder.cpp
index 34e7e6fdfbbb7069f81125f3dc51e2c57740763a..9de9363611e67ee3510f427d3bd6492ae375a12f 100644 (file)
@@ -36,12 +36,16 @@ void StringTableBuilder::finalize(Kind kind) {
 
   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 @@ void StringTableBuilder::finalize(Kind kind) {
     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;
   }
 }