llvm-project/lldb/source/Utility/DataBufferLLVM.cpp
Jonas Devlieghere fc54427e76
[lldb] Refactor DataBuffer so we can map files as read-only
Currently, all data buffers are assumed to be writable. This is a
problem on macOS where it's not allowed to load unsigned binaries in
memory as writable. To be more precise, MAP_RESILIENT_CODESIGN and
MAP_RESILIENT_MEDIA need to be set for mapped (unsigned) binaries on our
platform.

Binaries are mapped through FileSystem::CreateDataBuffer which returns a
DataBufferLLVM. The latter is backed by a llvm::WritableMemoryBuffer
because every DataBuffer in LLDB is considered to be writable. In order
to use a read-only llvm::MemoryBuffer I had to split our abstraction
around it.

This patch distinguishes between a DataBuffer (read-only) and
WritableDataBuffer (read-write) and updates LLDB to use the appropriate
one.

rdar://74890607

Differential revision: https://reviews.llvm.org/D122856
2022-04-05 13:46:37 -07:00

52 lines
1.5 KiB
C++

//===-- DataBufferLLVM.cpp ------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "lldb/Utility/DataBufferLLVM.h"
#include "llvm/Support/MemoryBuffer.h"
#include <cassert>
using namespace lldb_private;
DataBufferLLVM::DataBufferLLVM(std::unique_ptr<llvm::MemoryBuffer> MemBuffer)
: Buffer(std::move(MemBuffer)) {
assert(Buffer != nullptr &&
"Cannot construct a DataBufferLLVM with a null buffer");
}
DataBufferLLVM::~DataBufferLLVM() = default;
const uint8_t *DataBufferLLVM::GetBytesImpl() const {
return reinterpret_cast<const uint8_t *>(Buffer->getBufferStart());
}
lldb::offset_t DataBufferLLVM::GetByteSize() const {
return Buffer->getBufferSize();
}
WritableDataBufferLLVM::WritableDataBufferLLVM(
std::unique_ptr<llvm::WritableMemoryBuffer> MemBuffer)
: Buffer(std::move(MemBuffer)) {
assert(Buffer != nullptr &&
"Cannot construct a WritableDataBufferLLVM with a null buffer");
}
WritableDataBufferLLVM::~WritableDataBufferLLVM() = default;
const uint8_t *WritableDataBufferLLVM::GetBytesImpl() const {
return reinterpret_cast<const uint8_t *>(Buffer->getBufferStart());
}
lldb::offset_t WritableDataBufferLLVM::GetByteSize() const {
return Buffer->getBufferSize();
}
char DataBufferLLVM::ID;
char WritableDataBufferLLVM::ID;