Add read/write mode to ElfFile
authorPeter Griess <pgriess@fb.com>
Mon, 25 Mar 2013 16:31:49 +0000 (09:31 -0700)
committerOwen Yamauchi <oyamauchi@fb.com>
Wed, 27 Mar 2013 21:41:05 +0000 (14:41 -0700)
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
folly/experimental/symbolizer/Elf.h

index 420b8797bab67430e235127d12181da3841afd1f..f28ed07b016d8cafb0fa9287f3cf077190571f68 100644 (file)
@@ -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");
   }
index 9b1ddc49654e31c54c6ecaec7be156f923f487f1..256e359c1efc9bae347189bbd4c4a72580b00246 100644 (file)
@@ -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);