COFF: Teach Chunk to write to a mmap'ed output file.

Previously Writer directly handles writes to a file.
Chunks needed to give Writer a continuous chunk of memory.
That was inefficent if you construct data in chunks because
it would require two memory copies (one to construct a chunk
and the other is to write that to a file).

This patch teaches chunk to write directly to a file.
From readability point of view, this is also good because
you no longer have to call hasData() before calling getData().

llvm-svn: 238464
diff --git a/lld/COFF/Chunks.cpp b/lld/COFF/Chunks.cpp
index f7ec37b..f37e935 100644
--- a/lld/COFF/Chunks.cpp
+++ b/lld/COFF/Chunks.cpp
@@ -35,11 +35,11 @@
   Align = uint32_t(1) << Shift;
 }
 
-const uint8_t *SectionChunk::getData() const {
+void SectionChunk::writeTo(uint8_t *Buf) {
   assert(hasData());
   ArrayRef<uint8_t> Data;
   File->getCOFFObj()->getSectionContents(Header, Data);
-  return Data.data();
+  memcpy(Buf + FileOff, Data.data(), Data.size());
 }
 
 // Returns true if this chunk should be considered as a GC root.
@@ -157,9 +157,12 @@
          IMAGE_SCN_MEM_WRITE;
 }
 
-StringChunk::StringChunk(StringRef S) : Data(S.size() + 1) {
-  memcpy(Data.data(), S.data(), S.size());
-  Data[S.size()] = 0;
+void StringChunk::writeTo(uint8_t *Buf) {
+  memcpy(Buf + FileOff, Str.data(), Str.size());
+}
+
+void ImportThunkChunk::writeTo(uint8_t *Buf) {
+  memcpy(Buf + FileOff, ImportThunkData, sizeof(ImportThunkData));
 }
 
 void ImportThunkChunk::applyRelocations(uint8_t *Buf) {
@@ -168,9 +171,12 @@
   write32le(Buf + FileOff + 2, Operand);
 }
 
-HintNameChunk::HintNameChunk(StringRef Name)
-    : Data(RoundUpToAlignment(Name.size() + 4, 2)) {
-  memcpy(&Data[2], Name.data(), Name.size());
+HintNameChunk::HintNameChunk(StringRef N)
+    : Name(N), Size(RoundUpToAlignment(Name.size() + 4, 2)) {}
+
+void HintNameChunk::writeTo(uint8_t *Buf) {
+  // The first two bytes is Hint/Name field.
+  memcpy(Buf + FileOff + 2, Name.data(), Name.size());
 }
 
 void LookupChunk::applyRelocations(uint8_t *Buf) {