Make sure DataBufferLLVM contents are writable

Summary:
We sometimes need to write to the object file we've mapped into memory,
generally to apply relocations to debug info sections. We've had that
ability before, but with the introduction of DataBufferLLVM, we have
lost it, as the underlying llvm class (MemoryBuffer) only supports
read-only mappings.

This switches DataBufferLLVM to use the new llvm::WritableMemoryBuffer
class as a back-end, as this one guarantees to return a writable buffer.

This removes the need for the "Private" flag to the DataBufferLLVM
creation functions, as it was really used to mean "writable". The LLVM
function also does not have the NullTerminate flag, so I've modified our
clients to not require this feature and removed that flag as well.

Reviewers: zturner, clayborg, jingham

Subscribers: emaste, aprantl, arichardson, krytarowski, lldb-commits

Differential Revision: https://reviews.llvm.org/D40079

llvm-svn: 321255
diff --git a/lldb/source/Utility/DataBufferLLVM.cpp b/lldb/source/Utility/DataBufferLLVM.cpp
index bebcafb..713c3c2 100644
--- a/lldb/source/Utility/DataBufferLLVM.cpp
+++ b/lldb/source/Utility/DataBufferLLVM.cpp
@@ -18,7 +18,8 @@
 
 using namespace lldb_private;
 
-DataBufferLLVM::DataBufferLLVM(std::unique_ptr<llvm::MemoryBuffer> MemBuffer)
+DataBufferLLVM::DataBufferLLVM(
+    std::unique_ptr<llvm::WritableMemoryBuffer> MemBuffer)
     : Buffer(std::move(MemBuffer)) {
   assert(Buffer != nullptr &&
          "Cannot construct a DataBufferLLVM with a null buffer");
@@ -28,13 +29,13 @@
 
 std::shared_ptr<DataBufferLLVM>
 DataBufferLLVM::CreateSliceFromPath(const llvm::Twine &Path, uint64_t Size,
-                               uint64_t Offset, bool Private) {
+                                    uint64_t Offset) {
   // If the file resides non-locally, pass the volatile flag so that we don't
   // mmap it.
-  if (!Private)
-    Private = !llvm::sys::fs::is_local(Path);
+  bool IsVolatile = !llvm::sys::fs::is_local(Path);
 
-  auto Buffer = llvm::MemoryBuffer::getFileSlice(Path, Size, Offset, Private);
+  auto Buffer =
+      llvm::WritableMemoryBuffer::getFileSlice(Path, Size, Offset, IsVolatile);
   if (!Buffer)
     return nullptr;
   return std::shared_ptr<DataBufferLLVM>(
@@ -42,13 +43,12 @@
 }
 
 std::shared_ptr<DataBufferLLVM>
-DataBufferLLVM::CreateFromPath(const llvm::Twine &Path, bool NullTerminate, bool Private) {
+DataBufferLLVM::CreateFromPath(const llvm::Twine &Path) {
   // If the file resides non-locally, pass the volatile flag so that we don't
   // mmap it.
-  if (!Private)
-    Private = !llvm::sys::fs::is_local(Path);
+  bool IsVolatile = !llvm::sys::fs::is_local(Path);
 
-  auto Buffer = llvm::MemoryBuffer::getFile(Path, -1, NullTerminate, Private);
+  auto Buffer = llvm::WritableMemoryBuffer::getFile(Path, -1, IsVolatile);
   if (!Buffer)
     return nullptr;
   return std::shared_ptr<DataBufferLLVM>(
@@ -56,15 +56,13 @@
 }
 
 uint8_t *DataBufferLLVM::GetBytes() {
-  return const_cast<uint8_t *>(GetBuffer());
+  return reinterpret_cast<uint8_t *>(Buffer->getBufferStart());
 }
 
-const uint8_t *DataBufferLLVM::GetBytes() const { return GetBuffer(); }
+const uint8_t *DataBufferLLVM::GetBytes() const {
+  return reinterpret_cast<const uint8_t *>(Buffer->getBufferStart());
+}
 
 lldb::offset_t DataBufferLLVM::GetByteSize() const {
   return Buffer->getBufferSize();
 }
-
-const uint8_t *DataBufferLLVM::GetBuffer() const {
-  return reinterpret_cast<const uint8_t *>(Buffer->getBufferStart());
-}