Commit 37107ac7 authored by Peter Griess's avatar Peter Griess Committed by Owen Yamauchi

Add read/write mode to ElfFile

Summary:
- Add a mode to ElfFile that allows opening the file for read/write
access via PROT_WRITE.

Test Plan:
- Used it in some other code

Reviewed By: simpkins@fb.com

FB internal diff: D740184
parent ae95c1d5
......@@ -40,8 +40,8 @@ ElfFile::ElfFile()
baseAddress_(0) {
}
ElfFile::ElfFile(const char* name)
: fd_(open(name, O_RDONLY)),
ElfFile::ElfFile(const char* name, bool readOnly)
: fd_(open(name, (readOnly) ? O_RDONLY : O_RDWR)),
file_(static_cast<char*>(MAP_FAILED)),
length_(0),
baseAddress_(0) {
......@@ -56,8 +56,11 @@ ElfFile::ElfFile(const char* name)
}
length_ = st.st_size;
file_ = static_cast<char*>(
mmap(nullptr, length_, PROT_READ, MAP_SHARED, fd_, 0));
int prot = PROT_READ;
if (!readOnly) {
prot |= PROT_WRITE;
}
file_ = static_cast<char*>(mmap(nullptr, length_, prot, MAP_SHARED, fd_, 0));
if (file_ == MAP_FAILED) {
folly::throwSystemError("mmap");
}
......
......@@ -50,7 +50,7 @@ inline void enforce(bool v, Args... args) {
class ElfFile {
public:
ElfFile();
explicit ElfFile(const char* name);
explicit ElfFile(const char* name, bool readOnly=true);
~ElfFile();
ElfFile(ElfFile&& other);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment