From 37107ac7392880d73f45068a4f2b8c9f8f2fa157 Mon Sep 17 00:00:00 2001 From: Peter Griess Date: Mon, 25 Mar 2013 09:31:49 -0700 Subject: [PATCH] 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 --- folly/experimental/symbolizer/Elf.cpp | 11 +++++++---- folly/experimental/symbolizer/Elf.h | 2 +- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/folly/experimental/symbolizer/Elf.cpp b/folly/experimental/symbolizer/Elf.cpp index 420b8797..f28ed07b 100644 --- a/folly/experimental/symbolizer/Elf.cpp +++ b/folly/experimental/symbolizer/Elf.cpp @@ -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(MAP_FAILED)), length_(0), baseAddress_(0) { @@ -56,8 +56,11 @@ ElfFile::ElfFile(const char* name) } length_ = st.st_size; - file_ = static_cast( - mmap(nullptr, length_, PROT_READ, MAP_SHARED, fd_, 0)); + int prot = PROT_READ; + if (!readOnly) { + prot |= PROT_WRITE; + } + file_ = static_cast(mmap(nullptr, length_, prot, MAP_SHARED, fd_, 0)); if (file_ == MAP_FAILED) { folly::throwSystemError("mmap"); } diff --git a/folly/experimental/symbolizer/Elf.h b/folly/experimental/symbolizer/Elf.h index 9b1ddc49..256e359c 100644 --- a/folly/experimental/symbolizer/Elf.h +++ b/folly/experimental/symbolizer/Elf.h @@ -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); -- 2.34.1