[mach-o] add representation for LC_ID_DYLIB to MachONormalizedFile

It still needs to be tied into BinaryReader, but this allows reasonably
sensible creation of SharedLibrary atoms on MachO.

llvm-svn: 212093
diff --git a/lld/lib/ReaderWriter/MachO/MachONormalizedFileBinaryReader.cpp b/lld/lib/ReaderWriter/MachO/MachONormalizedFileBinaryReader.cpp
index bf93a00..7c59915 100644
--- a/lld/lib/ReaderWriter/MachO/MachONormalizedFileBinaryReader.cpp
+++ b/lld/lib/ReaderWriter/MachO/MachONormalizedFileBinaryReader.cpp
@@ -386,6 +386,15 @@
         }
       }
     }
+    if (cmd == LC_ID_DYLIB) {
+      const dylib_command *dl = reinterpret_cast<const dylib_command*>(lc);
+      dylib_command tempDL;
+      if (swap) {
+        tempDL = *dl; swapStruct(tempDL); dl = &tempDL;
+      }
+
+      f->installName = lc + dl->dylib.name;
+    }
     return false;
   });
   if (ec)
diff --git a/lld/lib/ReaderWriter/MachO/MachONormalizedFileBinaryWriter.cpp b/lld/lib/ReaderWriter/MachO/MachONormalizedFileBinaryWriter.cpp
index 2f61607..755e9aa 100644
--- a/lld/lib/ReaderWriter/MachO/MachONormalizedFileBinaryWriter.cpp
+++ b/lld/lib/ReaderWriter/MachO/MachONormalizedFileBinaryWriter.cpp
@@ -324,6 +324,12 @@
   size += segCommandSize;
   ++count;
 
+  // If creating a dylib, add LC_ID_DYLIB.
+  if (_file.fileType == llvm::MachO::MH_DYLIB) {
+    size += sizeof(dylib_command) + pointerAlign(_file.installName.size() + 1);
+    ++count;
+  }
+
   // Add LC_DYLD_INFO
   size += sizeof(dyld_info_command);
   ++count;
@@ -621,6 +627,24 @@
     else
       ec = writeSegmentLoadCommands<MachO32Trait>(lc);
 
+    // Add LC_ID_DYLIB command for dynamic libraries.
+    if (_file.fileType == llvm::MachO::MH_DYLIB) {
+      dylib_command *dc = reinterpret_cast<dylib_command*>(lc);
+      StringRef path = _file.installName;
+      uint32_t size = sizeof(dylib_command) + pointerAlign(path.size() + 1);
+      dc->cmd                         = LC_ID_DYLIB;
+      dc->cmdsize                     = size;
+      dc->dylib.name                  = sizeof(dylib_command); // offset
+      dc->dylib.timestamp             = 0; // FIXME
+      dc->dylib.current_version       = 0; // FIXME
+      dc->dylib.compatibility_version = 0; // FIXME
+      if (_swap)
+        swapStruct(*dc);
+      memcpy(lc + sizeof(dylib_command), path.begin(), path.size());
+      lc[sizeof(dylib_command) + path.size()] = '\0';
+      lc += size;
+    }
+
     // Add LC_DYLD_INFO_ONLY.
     dyld_info_command* di = reinterpret_cast<dyld_info_command*>(lc);
     di->cmd            = LC_DYLD_INFO_ONLY;
@@ -720,7 +744,6 @@
       lc[sizeof(dylib_command)+dep.path.size()] = '\0';
       lc += size;
     }
-
   }
   return ec;
 }
diff --git a/lld/lib/ReaderWriter/MachO/MachONormalizedFileFromAtoms.cpp b/lld/lib/ReaderWriter/MachO/MachONormalizedFileFromAtoms.cpp
index 67ed62c..9f348a3 100644
--- a/lld/lib/ReaderWriter/MachO/MachONormalizedFileFromAtoms.cpp
+++ b/lld/lib/ReaderWriter/MachO/MachONormalizedFileFromAtoms.cpp
@@ -934,6 +934,7 @@
   f->arch = context.arch();
   f->fileType = context.outputMachOType();
   f->flags = util.fileFlags();
+  f->installName = context.installName();
   util.copySegmentInfo(normFile);
   util.copySections(normFile);
   util.addDependentDylibs(atomFile, normFile);
diff --git a/lld/lib/ReaderWriter/MachO/MachONormalizedFileToAtoms.cpp b/lld/lib/ReaderWriter/MachO/MachONormalizedFileToAtoms.cpp
index 417cabc..5846c3e 100644
--- a/lld/lib/ReaderWriter/MachO/MachONormalizedFileToAtoms.cpp
+++ b/lld/lib/ReaderWriter/MachO/MachONormalizedFileToAtoms.cpp
@@ -605,7 +605,8 @@
 ErrorOr<std::unique_ptr<lld::File>>
 normalizedDylibToAtoms(const NormalizedFile &normalizedFile, StringRef path,
                        bool copyRefs) {
-  std::unique_ptr<MachODylibFile> file(new MachODylibFile(path));
+  std::unique_ptr<MachODylibFile> file(
+      new MachODylibFile(normalizedFile.installName));
 
   for (auto &sym : normalizedFile.globalSymbols) {
     assert((sym.scope & N_EXT) && "only expect external symbols here");
diff --git a/lld/lib/ReaderWriter/MachO/MachONormalizedFileYAML.cpp b/lld/lib/ReaderWriter/MachO/MachONormalizedFileYAML.cpp
index 7477aea..ddc1fcc 100644
--- a/lld/lib/ReaderWriter/MachO/MachONormalizedFileYAML.cpp
+++ b/lld/lib/ReaderWriter/MachO/MachONormalizedFileYAML.cpp
@@ -508,7 +508,6 @@
   }
 };
 
-
 template <>
 struct ScalarEnumerationTraits<RebaseType> {
   static void enumeration(IO &io, RebaseType &value) {